blob: 1dde4ab3c9fdbfe10756ca3932401c1b6956406b [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
Manuel Pégourié-Gonnard1038b222025-03-05 11:53:09 +010094 MBEDTLS_SSL_DEBUG_MSG(1, ("No matched ciphersuite, psk_ciphersuite_id=%x, psk_hash_alg=%lx",
Gilles Peskinea9d4ef02024-06-03 22:16:23 +020095 (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) {
Manuel Pégourié-Gonnard6637ef72025-02-11 13:19:45 +01001368 MBEDTLS_SSL_DEBUG_MSG(2, ("no supported_versions extension"));
Ronald Cron5af4c7f2023-03-07 20:46:59 +01001369 return SSL_CLIENT_HELLO_TLS1_2;
Ronald Cron8c527d02023-03-07 15:47:47 +01001370 }
1371
Ronald Cron5af4c7f2023-03-07 20:46:59 +01001372 if (ret == 1) {
1373 ret = ssl_tls13_parse_supported_versions_ext(ssl,
Ronald Croneff56732023-04-03 17:36:31 +02001374 supported_versions_data,
1375 supported_versions_data_end);
Ronald Cron5af4c7f2023-03-07 20:46:59 +01001376 if (ret < 0) {
1377 MBEDTLS_SSL_DEBUG_RET(1,
1378 ("ssl_tls13_parse_supported_versions_ext"), ret);
1379 return ret;
1380 }
1381
Ronald Cronb828c7d2023-04-03 16:37:22 +02001382 /*
1383 * The supported versions extension was parsed successfully as the
1384 * value returned by ssl_tls13_parse_supported_versions_ext() is
1385 * positive. The return value is then equal to
1386 * MBEDTLS_SSL_VERSION_TLS1_2 or MBEDTLS_SSL_VERSION_TLS1_3, defining
1387 * the TLS version to negotiate.
1388 */
Ronald Cron5af4c7f2023-03-07 20:46:59 +01001389 if (MBEDTLS_SSL_VERSION_TLS1_2 == ret) {
Manuel Pégourié-Gonnard6637ef72025-02-11 13:19:45 +01001390 MBEDTLS_SSL_DEBUG_MSG(2, ("supported_versions without 1.3"));
Ronald Cron5af4c7f2023-03-07 20:46:59 +01001391 return SSL_CLIENT_HELLO_TLS1_2;
1392 }
Ronald Cron8c527d02023-03-07 15:47:47 +01001393 }
1394
1395 /*
1396 * We negotiate TLS 1.3.
Ronald Cron64582392023-03-07 09:21:40 +01001397 */
1398 ssl->tls_version = MBEDTLS_SSL_VERSION_TLS1_3;
Ronald Cron64582392023-03-07 09:21:40 +01001399 ssl->session_negotiate->tls_version = MBEDTLS_SSL_VERSION_TLS1_3;
1400 ssl->session_negotiate->endpoint = ssl->conf->endpoint;
Ronald Cron64582392023-03-07 09:21:40 +01001401
1402 /*
Ronald Crondad02b22023-04-06 09:57:52 +02001403 * We are negotiating the version 1.3 of the protocol. Do what we have
Ronald Croncada4102023-03-07 09:51:39 +01001404 * postponed: copy of the client random bytes, copy of the legacy session
Ronald Cron2f16b4e2023-03-07 10:07:32 +01001405 * identifier and selection of the TLS 1.3 cipher suite.
Ronald Crond540d992023-03-07 09:41:48 +01001406 */
1407 MBEDTLS_SSL_DEBUG_BUF(3, "client hello, random bytes",
1408 random, MBEDTLS_CLIENT_HELLO_RANDOM_LEN);
1409 memcpy(&handshake->randbytes[0], random, MBEDTLS_CLIENT_HELLO_RANDOM_LEN);
1410
Ronald Croncada4102023-03-07 09:51:39 +01001411 if (legacy_session_id_len > sizeof(ssl->session_negotiate->id)) {
1412 MBEDTLS_SSL_DEBUG_MSG(1, ("bad client hello message"));
1413 return MBEDTLS_ERR_SSL_DECODE_ERROR;
1414 }
1415 ssl->session_negotiate->id_len = legacy_session_id_len;
1416 MBEDTLS_SSL_DEBUG_BUF(3, "client hello, session id",
1417 legacy_session_id, legacy_session_id_len);
1418 memcpy(&ssl->session_negotiate->id[0],
1419 legacy_session_id, legacy_session_id_len);
1420
Ronald Crond540d992023-03-07 09:41:48 +01001421 /*
Jerry Yu5725f1c2022-08-21 17:27:16 +08001422 * Search for a matching ciphersuite
1423 */
Ronald Cron2f16b4e2023-03-07 10:07:32 +01001424 MBEDTLS_SSL_DEBUG_BUF(3, "client hello, list of cipher suites",
1425 cipher_suites, cipher_suites_len);
Jerry Yu5725f1c2022-08-21 17:27:16 +08001426
Ronald Cron89089cc2024-02-14 18:18:08 +01001427 ssl_tls13_select_ciphersuite(ssl, cipher_suites, cipher_suites_end,
1428 0, PSA_ALG_NONE, &handshake->ciphersuite_info);
Jerry Yu5725f1c2022-08-21 17:27:16 +08001429
Gilles Peskine449bd832023-01-11 14:50:10 +01001430 if (handshake->ciphersuite_info == NULL) {
1431 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE,
1432 MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE);
1433 return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
Jerry Yu5725f1c2022-08-21 17:27:16 +08001434 }
Ronald Cron89089cc2024-02-14 18:18:08 +01001435 ssl->session_negotiate->ciphersuite = handshake->ciphersuite_info->id;
1436
1437 MBEDTLS_SSL_DEBUG_MSG(2, ("selected ciphersuite: %04x - %s",
1438 ((unsigned) handshake->ciphersuite_info->id),
1439 handshake->ciphersuite_info->name));
Jerry Yuc1be19f2022-04-23 16:11:39 +08001440
XiaokangQian4080a7f2022-04-11 09:55:18 +00001441 /* ...
XiaokangQianb67384d2022-04-19 00:02:38 +00001442 * opaque legacy_compression_methods<1..2^8-1>;
XiaokangQian4080a7f2022-04-11 09:55:18 +00001443 * ...
XiaokangQian7807f9f2022-02-15 10:04:37 +00001444 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001445 if (p[0] != 1 || p[1] != MBEDTLS_SSL_COMPRESS_NULL) {
1446 MBEDTLS_SSL_DEBUG_MSG(1, ("bad legacy compression method"));
1447 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER,
1448 MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER);
1449 return MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001450 }
XiaokangQianb67384d2022-04-19 00:02:38 +00001451 p += 2;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001452
Jerry Yuc1be19f2022-04-23 16:11:39 +08001453 /* ...
XiaokangQianb67384d2022-04-19 00:02:38 +00001454 * Extension extensions<8..2^16-1>;
Jerry Yuc1be19f2022-04-23 16:11:39 +08001455 * ...
XiaokangQianb67384d2022-04-19 00:02:38 +00001456 * with Extension defined as:
1457 * struct {
1458 * ExtensionType extension_type;
1459 * opaque extension_data<0..2^16-1>;
1460 * } Extension;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001461 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001462 extensions_len = MBEDTLS_GET_UINT16_BE(p, 0);
XiaokangQian7807f9f2022-02-15 10:04:37 +00001463 p += 2;
Gilles Peskine449bd832023-01-11 14:50:10 +01001464 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, extensions_len);
XiaokangQiane8ff3502022-04-22 02:34:40 +00001465 extensions_end = p + extensions_len;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001466
Gilles Peskine449bd832023-01-11 14:50:10 +01001467 MBEDTLS_SSL_DEBUG_BUF(3, "client hello extensions", p, extensions_len);
Jerry Yu63a459c2022-10-31 13:38:40 +08001468 handshake->received_extensions = MBEDTLS_SSL_EXT_MASK_NONE;
Jerry Yu0c354a22022-08-29 15:25:36 +08001469
Gilles Peskine449bd832023-01-11 14:50:10 +01001470 while (p < extensions_end) {
XiaokangQian7807f9f2022-02-15 10:04:37 +00001471 unsigned int extension_type;
1472 size_t extension_data_len;
1473 const unsigned char *extension_data_end;
Jerry Yu263dbf72022-10-26 10:51:27 +08001474 uint32_t allowed_exts = MBEDTLS_SSL_TLS1_3_ALLOWED_EXTS_OF_CH;
1475
Ronald Cron5fbd2702024-02-14 10:03:36 +01001476 if (ssl->handshake->hello_retry_request_flag) {
Jerry Yu263dbf72022-10-26 10:51:27 +08001477 /* Do not accept early data extension in 2nd ClientHello */
1478 allowed_exts &= ~MBEDTLS_SSL_EXT_MASK(EARLY_DATA);
1479 }
XiaokangQian7807f9f2022-02-15 10:04:37 +00001480
Jerry Yu0c354a22022-08-29 15:25:36 +08001481 /* RFC 8446, section 4.2.11
Jerry Yu1c9247c2022-07-21 12:37:39 +08001482 *
1483 * The "pre_shared_key" extension MUST be the last extension in the
1484 * ClientHello (this facilitates implementation as described below).
1485 * Servers MUST check that it is the last extension and otherwise fail
1486 * the handshake with an "illegal_parameter" alert.
1487 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001488 if (handshake->received_extensions & MBEDTLS_SSL_EXT_MASK(PRE_SHARED_KEY)) {
Jerry Yu1c9247c2022-07-21 12:37:39 +08001489 MBEDTLS_SSL_DEBUG_MSG(
Gilles Peskine449bd832023-01-11 14:50:10 +01001490 3, ("pre_shared_key is not last extension."));
Jerry Yu1c9247c2022-07-21 12:37:39 +08001491 MBEDTLS_SSL_PEND_FATAL_ALERT(
1492 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER,
Gilles Peskine449bd832023-01-11 14:50:10 +01001493 MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER);
1494 return MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER;
Jerry Yu1c9247c2022-07-21 12:37:39 +08001495 }
1496
Gilles Peskine449bd832023-01-11 14:50:10 +01001497 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, extensions_end, 4);
1498 extension_type = MBEDTLS_GET_UINT16_BE(p, 0);
1499 extension_data_len = MBEDTLS_GET_UINT16_BE(p, 2);
XiaokangQian7807f9f2022-02-15 10:04:37 +00001500 p += 4;
1501
Gilles Peskine449bd832023-01-11 14:50:10 +01001502 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, extensions_end, extension_data_len);
XiaokangQian7807f9f2022-02-15 10:04:37 +00001503 extension_data_end = p + extension_data_len;
1504
Jerry Yuc4bf5d62022-10-29 09:08:47 +08001505 ret = mbedtls_ssl_tls13_check_received_extension(
Gilles Peskine449bd832023-01-11 14:50:10 +01001506 ssl, MBEDTLS_SSL_HS_CLIENT_HELLO, extension_type,
Jerry Yu263dbf72022-10-26 10:51:27 +08001507 allowed_exts);
Gilles Peskine449bd832023-01-11 14:50:10 +01001508 if (ret != 0) {
1509 return ret;
1510 }
Jerry Yue18dc7e2022-08-04 16:29:22 +08001511
Gilles Peskine449bd832023-01-11 14:50:10 +01001512 switch (extension_type) {
XiaokangQian40a35232022-05-07 09:02:40 +00001513#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
1514 case MBEDTLS_TLS_EXT_SERVERNAME:
Gilles Peskine449bd832023-01-11 14:50:10 +01001515 MBEDTLS_SSL_DEBUG_MSG(3, ("found ServerName extension"));
1516 ret = mbedtls_ssl_parse_server_name_ext(ssl, p,
1517 extension_data_end);
1518 if (ret != 0) {
XiaokangQian40a35232022-05-07 09:02:40 +00001519 MBEDTLS_SSL_DEBUG_RET(
Gilles Peskine449bd832023-01-11 14:50:10 +01001520 1, "mbedtls_ssl_parse_servername_ext", ret);
1521 return ret;
XiaokangQian40a35232022-05-07 09:02:40 +00001522 }
XiaokangQian40a35232022-05-07 09:02:40 +00001523 break;
1524#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
1525
Przemek Stekiel8c0a9532023-06-15 16:48:19 +02001526#if defined(PSA_WANT_ALG_ECDH) || defined(PSA_WANT_ALG_FFDH)
XiaokangQian7807f9f2022-02-15 10:04:37 +00001527 case MBEDTLS_TLS_EXT_SUPPORTED_GROUPS:
Gilles Peskine449bd832023-01-11 14:50:10 +01001528 MBEDTLS_SSL_DEBUG_MSG(3, ("found supported group extension"));
XiaokangQian7807f9f2022-02-15 10:04:37 +00001529
1530 /* Supported Groups Extension
1531 *
1532 * When sent by the client, the "supported_groups" extension
1533 * indicates the named groups which the client supports,
1534 * ordered from most preferred to least preferred.
1535 */
Jerry Yuc1be19f2022-04-23 16:11:39 +08001536 ret = ssl_tls13_parse_supported_groups_ext(
Gilles Peskine449bd832023-01-11 14:50:10 +01001537 ssl, p, extension_data_end);
1538 if (ret != 0) {
Xiaokang Qian9f1747b2023-03-30 02:50:04 +00001539 MBEDTLS_SSL_DEBUG_RET(
Xiaokang Qian8bce0e62023-04-04 10:15:48 +00001540 1, "ssl_tls13_parse_supported_groups_ext", ret);
Gilles Peskine449bd832023-01-11 14:50:10 +01001541 return ret;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001542 }
1543
XiaokangQian7807f9f2022-02-15 10:04:37 +00001544 break;
Przemek Stekiel8c0a9532023-06-15 16:48:19 +02001545#endif /* PSA_WANT_ALG_ECDH || PSA_WANT_ALG_FFDH*/
XiaokangQian7807f9f2022-02-15 10:04:37 +00001546
Valerio Settic9ae8622023-07-25 11:23:50 +02001547#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_EPHEMERAL_ENABLED)
XiaokangQian7807f9f2022-02-15 10:04:37 +00001548 case MBEDTLS_TLS_EXT_KEY_SHARE:
Gilles Peskine449bd832023-01-11 14:50:10 +01001549 MBEDTLS_SSL_DEBUG_MSG(3, ("found key share extension"));
XiaokangQian7807f9f2022-02-15 10:04:37 +00001550
1551 /*
1552 * Key Share Extension
1553 *
1554 * When sent by the client, the "key_share" extension
1555 * contains the endpoint's cryptographic parameters for
1556 * ECDHE/DHE key establishment methods.
1557 */
Jerry Yuc1be19f2022-04-23 16:11:39 +08001558 ret = ssl_tls13_parse_key_shares_ext(
Gilles Peskine449bd832023-01-11 14:50:10 +01001559 ssl, p, extension_data_end);
1560 if (ret == SSL_TLS1_3_PARSE_KEY_SHARES_EXT_NO_MATCH) {
Ronald Cron8a74f072023-06-14 17:59:29 +02001561 MBEDTLS_SSL_DEBUG_MSG(2, ("No usable share for key agreement."));
1562 no_usable_share_for_key_agreement = 1;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001563 }
1564
Gilles Peskine449bd832023-01-11 14:50:10 +01001565 if (ret < 0) {
Jerry Yu582dd062022-04-22 21:59:01 +08001566 MBEDTLS_SSL_DEBUG_RET(
Gilles Peskine449bd832023-01-11 14:50:10 +01001567 1, "ssl_tls13_parse_key_shares_ext", ret);
1568 return ret;
Jerry Yu582dd062022-04-22 21:59:01 +08001569 }
XiaokangQian7807f9f2022-02-15 10:04:37 +00001570
XiaokangQian7807f9f2022-02-15 10:04:37 +00001571 break;
Valerio Settic9ae8622023-07-25 11:23:50 +02001572#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_EPHEMERAL_ENABLED */
XiaokangQian7807f9f2022-02-15 10:04:37 +00001573
1574 case MBEDTLS_TLS_EXT_SUPPORTED_VERSIONS:
Ronald Cron8c527d02023-03-07 15:47:47 +01001575 /* Already parsed */
XiaokangQian7807f9f2022-02-15 10:04:37 +00001576 break;
1577
Ronald Cron41a443a2022-10-04 16:38:25 +02001578#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED)
Jerry Yue19e3b92022-07-08 12:04:51 +00001579 case MBEDTLS_TLS_EXT_PSK_KEY_EXCHANGE_MODES:
Xiaokang Qian9f1747b2023-03-30 02:50:04 +00001580 MBEDTLS_SSL_DEBUG_MSG(
1581 3, ("found psk key exchange modes extension"));
Jerry Yue19e3b92022-07-08 12:04:51 +00001582
1583 ret = ssl_tls13_parse_key_exchange_modes_ext(
Gilles Peskine449bd832023-01-11 14:50:10 +01001584 ssl, p, extension_data_end);
1585 if (ret != 0) {
Jerry Yue19e3b92022-07-08 12:04:51 +00001586 MBEDTLS_SSL_DEBUG_RET(
Gilles Peskine449bd832023-01-11 14:50:10 +01001587 1, "ssl_tls13_parse_key_exchange_modes_ext", ret);
1588 return ret;
Jerry Yue19e3b92022-07-08 12:04:51 +00001589 }
1590
Jerry Yue19e3b92022-07-08 12:04:51 +00001591 break;
Ronald Cron41a443a2022-10-04 16:38:25 +02001592#endif
Jerry Yue19e3b92022-07-08 12:04:51 +00001593
Jerry Yu1c105562022-07-10 06:32:38 +00001594 case MBEDTLS_TLS_EXT_PRE_SHARED_KEY:
Gilles Peskine449bd832023-01-11 14:50:10 +01001595 MBEDTLS_SSL_DEBUG_MSG(3, ("found pre_shared_key extension"));
1596 if ((handshake->received_extensions &
1597 MBEDTLS_SSL_EXT_MASK(PSK_KEY_EXCHANGE_MODES)) == 0) {
Jerry Yu13ab81d2022-07-22 23:17:11 +08001598 MBEDTLS_SSL_PEND_FATAL_ALERT(
1599 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER,
Gilles Peskine449bd832023-01-11 14:50:10 +01001600 MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER);
1601 return MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER;
Jerry Yu13ab81d2022-07-22 23:17:11 +08001602 }
Ronald Cron41a443a2022-10-04 16:38:25 +02001603#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED)
Jerry Yu1c105562022-07-10 06:32:38 +00001604 /* Delay processing of the PSK identity once we have
1605 * found out which algorithms to use. We keep a pointer
1606 * to the buffer and the size for later processing.
1607 */
Jerry Yu29d9faa2022-08-23 17:52:45 +08001608 pre_shared_key_ext = p;
Jerry Yu1c105562022-07-10 06:32:38 +00001609 pre_shared_key_ext_end = extension_data_end;
Jerry Yue18dc7e2022-08-04 16:29:22 +08001610#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED */
Jerry Yu1c105562022-07-10 06:32:38 +00001611 break;
Jerry Yu1c105562022-07-10 06:32:38 +00001612
XiaokangQianacb39922022-06-17 10:18:48 +00001613#if defined(MBEDTLS_SSL_ALPN)
1614 case MBEDTLS_TLS_EXT_ALPN:
Gilles Peskine449bd832023-01-11 14:50:10 +01001615 MBEDTLS_SSL_DEBUG_MSG(3, ("found alpn extension"));
XiaokangQianacb39922022-06-17 10:18:48 +00001616
Gilles Peskine449bd832023-01-11 14:50:10 +01001617 ret = mbedtls_ssl_parse_alpn_ext(ssl, p, extension_data_end);
1618 if (ret != 0) {
XiaokangQianacb39922022-06-17 10:18:48 +00001619 MBEDTLS_SSL_DEBUG_RET(
Gilles Peskine449bd832023-01-11 14:50:10 +01001620 1, ("mbedtls_ssl_parse_alpn_ext"), ret);
1621 return ret;
XiaokangQianacb39922022-06-17 10:18:48 +00001622 }
XiaokangQianacb39922022-06-17 10:18:48 +00001623 break;
1624#endif /* MBEDTLS_SSL_ALPN */
1625
Ronald Cron928cbd32022-10-04 16:14:26 +02001626#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED)
XiaokangQian7807f9f2022-02-15 10:04:37 +00001627 case MBEDTLS_TLS_EXT_SIG_ALG:
Gilles Peskine449bd832023-01-11 14:50:10 +01001628 MBEDTLS_SSL_DEBUG_MSG(3, ("found signature_algorithms extension"));
XiaokangQian7807f9f2022-02-15 10:04:37 +00001629
Gabor Mezei078e8032022-04-27 21:17:56 +02001630 ret = mbedtls_ssl_parse_sig_alg_ext(
Gilles Peskine449bd832023-01-11 14:50:10 +01001631 ssl, p, extension_data_end);
1632 if (ret != 0) {
Xiaokang Qian49f39c12023-04-06 02:31:02 +00001633 MBEDTLS_SSL_DEBUG_RET(
1634 1, "mbedtls_ssl_parse_sig_alg_ext", ret);
Gilles Peskine449bd832023-01-11 14:50:10 +01001635 return ret;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001636 }
XiaokangQian7807f9f2022-02-15 10:04:37 +00001637 break;
Ronald Cron928cbd32022-10-04 16:14:26 +02001638#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED */
XiaokangQian7807f9f2022-02-15 10:04:37 +00001639
Jan Bruckner151f6422023-02-10 12:45:19 +01001640#if defined(MBEDTLS_SSL_RECORD_SIZE_LIMIT)
1641 case MBEDTLS_TLS_EXT_RECORD_SIZE_LIMIT:
1642 MBEDTLS_SSL_DEBUG_MSG(3, ("found record_size_limit extension"));
1643
Xiaokang Qian9f1747b2023-03-30 02:50:04 +00001644 ret = mbedtls_ssl_tls13_parse_record_size_limit_ext(
1645 ssl, p, extension_data_end);
Jan Brucknerf482dcc2023-03-15 09:09:06 +01001646 if (ret != 0) {
1647 MBEDTLS_SSL_DEBUG_RET(
1648 1, ("mbedtls_ssl_tls13_parse_record_size_limit_ext"), ret);
1649 return ret;
1650 }
Jan Bruckner151f6422023-02-10 12:45:19 +01001651 break;
1652#endif /* MBEDTLS_SSL_RECORD_SIZE_LIMIT */
1653
XiaokangQian7807f9f2022-02-15 10:04:37 +00001654 default:
Jerry Yu79aa7212022-11-08 21:30:21 +08001655 MBEDTLS_SSL_PRINT_EXT(
Jerry Yu63a459c2022-10-31 13:38:40 +08001656 3, MBEDTLS_SSL_HS_CLIENT_HELLO,
Gilles Peskine449bd832023-01-11 14:50:10 +01001657 extension_type, "( ignored )");
Jerry Yu0c354a22022-08-29 15:25:36 +08001658 break;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001659 }
1660
1661 p += extension_data_len;
1662 }
1663
Gilles Peskine449bd832023-01-11 14:50:10 +01001664 MBEDTLS_SSL_PRINT_EXTS(3, MBEDTLS_SSL_HS_CLIENT_HELLO,
1665 handshake->received_extensions);
Jerry Yue18dc7e2022-08-04 16:29:22 +08001666
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +01001667 ret = mbedtls_ssl_add_hs_hdr_to_checksum(ssl,
1668 MBEDTLS_SSL_HS_CLIENT_HELLO,
1669 p - buf);
1670 if (0 != ret) {
1671 MBEDTLS_SSL_DEBUG_RET(1, ("mbedtls_ssl_add_hs_hdr_to_checksum"), ret);
1672 return ret;
1673 }
Jerry Yu032b15ce2022-07-11 06:10:03 +00001674
Ronald Cron41a443a2022-10-04 16:38:25 +02001675#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED)
XiaokangQian7807f9f2022-02-15 10:04:37 +00001676 /* Update checksum with either
1677 * - The entire content of the CH message, if no PSK extension is present
1678 * - The content up to but excluding the PSK extension, if present.
Ronald Cron7cab4f82024-03-07 15:09:09 +01001679 * Always parse the pre-shared-key extension when present in the
Ronald Cron12e72f12024-02-14 15:49:38 +01001680 * ClientHello even if some pre-requisites for PSK key exchange modes are
1681 * not met. That way we always validate the syntax of the extension.
XiaokangQian7807f9f2022-02-15 10:04:37 +00001682 */
Ronald Cron12e72f12024-02-14 15:49:38 +01001683 if (handshake->received_extensions & MBEDTLS_SSL_EXT_MASK(PRE_SHARED_KEY)) {
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +01001684 ret = handshake->update_checksum(ssl, buf,
1685 pre_shared_key_ext - buf);
1686 if (0 != ret) {
1687 MBEDTLS_SSL_DEBUG_RET(1, ("update_checksum"), ret);
1688 return ret;
1689 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001690 ret = ssl_tls13_parse_pre_shared_key_ext(ssl,
1691 pre_shared_key_ext,
1692 pre_shared_key_ext_end,
1693 cipher_suites,
Ronald Cron79cdd412024-02-20 17:19:28 +01001694 cipher_suites_end,
1695 &psk);
1696 if (ret == 0) {
1697 got_psk = 1;
1698 } else if (ret != MBEDTLS_ERR_SSL_UNKNOWN_IDENTITY) {
Jerry Yu63a459c2022-10-31 13:38:40 +08001699 MBEDTLS_SSL_DEBUG_RET(
Gilles Peskine449bd832023-01-11 14:50:10 +01001700 1, "ssl_tls13_parse_pre_shared_key_ext", ret);
1701 return ret;
Jerry Yu1c105562022-07-10 06:32:38 +00001702 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001703 } else
Ronald Cron41a443a2022-10-04 16:38:25 +02001704#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED */
Jerry Yu1c105562022-07-10 06:32:38 +00001705 {
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +01001706 ret = handshake->update_checksum(ssl, buf, p - buf);
1707 if (0 != ret) {
1708 MBEDTLS_SSL_DEBUG_RET(1, ("update_checksum"), ret);
1709 return ret;
1710 }
Jerry Yu1c105562022-07-10 06:32:38 +00001711 }
XiaokangQian7807f9f2022-02-15 10:04:37 +00001712
Ronald Cron79cdd412024-02-20 17:19:28 +01001713 /*
1714 * Determine the key exchange algorithm to use.
1715 * There are three types of key exchanges supported in TLS 1.3:
1716 * - (EC)DH with ECDSA,
1717 * - (EC)DH with PSK,
1718 * - plain PSK.
1719 *
1720 * The PSK-based key exchanges may additionally be used with 0-RTT.
1721 *
1722 * Our built-in order of preference is
1723 * 1 ) (EC)DHE-PSK Mode ( psk_ephemeral )
1724 * 2 ) Certificate Mode ( ephemeral )
1725 * 3 ) Plain PSK Mode ( psk )
1726 */
1727#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED)
1728 if (got_psk && (psk.key_exchange_mode ==
1729 MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK_EPHEMERAL)) {
1730 handshake->key_exchange_mode =
1731 MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK_EPHEMERAL;
1732 MBEDTLS_SSL_DEBUG_MSG(2, ("key exchange mode: psk_ephemeral"));
1733
1734 } else
1735#endif
1736 if (ssl_tls13_key_exchange_is_ephemeral_available(ssl)) {
1737 handshake->key_exchange_mode =
1738 MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL;
1739 MBEDTLS_SSL_DEBUG_MSG(2, ("key exchange mode: ephemeral"));
1740
1741 }
1742#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED)
1743 else if (got_psk && (psk.key_exchange_mode ==
1744 MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK)) {
1745 handshake->key_exchange_mode = MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK;
1746 MBEDTLS_SSL_DEBUG_MSG(2, ("key exchange mode: psk"));
1747 }
1748#endif
1749 else {
1750 MBEDTLS_SSL_DEBUG_MSG(
1751 1,
1752 ("ClientHello message misses mandatory extensions."));
1753 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_MISSING_EXTENSION,
1754 MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER);
1755 return MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER;
Gilles Peskine449bd832023-01-11 14:50:10 +01001756 }
XiaokangQian7807f9f2022-02-15 10:04:37 +00001757
Ronald Cron3e47eec2024-02-21 10:29:24 +01001758#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED)
Ronald Cron74a16292024-02-23 17:07:41 +01001759 if (handshake->key_exchange_mode &
1760 MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK_ALL) {
1761 handshake->ciphersuite_info = psk.ciphersuite_info;
1762 ssl->session_negotiate->ciphersuite = psk.ciphersuite_info->id;
1763
1764 MBEDTLS_SSL_DEBUG_MSG(2, ("Select PSK ciphersuite: %04x - %s",
1765 ((unsigned) psk.ciphersuite_info->id),
1766 psk.ciphersuite_info->name));
1767
1768 if (psk.type == MBEDTLS_SSL_TLS1_3_PSK_RESUMPTION) {
1769 handshake->resume = 1;
1770 }
Ronald Cron79cdd412024-02-20 17:19:28 +01001771 }
Ronald Cron3e47eec2024-02-21 10:29:24 +01001772#endif
Ronald Cron79cdd412024-02-20 17:19:28 +01001773
1774 if (handshake->key_exchange_mode !=
Ronald Cron8a74f072023-06-14 17:59:29 +02001775 MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK) {
1776 hrr_required = (no_usable_share_for_key_agreement != 0);
1777 }
1778
Gilles Peskine449bd832023-01-11 14:50:10 +01001779 mbedtls_ssl_optimize_checksum(ssl, handshake->ciphersuite_info);
Jerry Yue5834fd2022-08-29 20:16:09 +08001780
Gilles Peskine449bd832023-01-11 14:50:10 +01001781 return hrr_required ? SSL_CLIENT_HELLO_HRR_REQUIRED : SSL_CLIENT_HELLO_OK;
XiaokangQian75fe8c72022-06-15 09:42:45 +00001782}
1783
Jerry Yuab0da372023-02-08 13:55:24 +08001784#if defined(MBEDTLS_SSL_EARLY_DATA)
Jerry Yu4caf3ca2023-11-15 16:13:47 +08001785static int ssl_tls13_check_early_data_requirements(mbedtls_ssl_context *ssl)
Jerry Yuab0da372023-02-08 13:55:24 +08001786{
1787 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
1788
Jerry Yu985c9672022-12-04 14:06:30 +08001789 if (ssl->conf->early_data_enabled == MBEDTLS_SSL_EARLY_DATA_DISABLED) {
1790 MBEDTLS_SSL_DEBUG_MSG(
Jerry Yu985c9672022-12-04 14:06:30 +08001791 1,
Jerry Yu454dda32023-10-31 15:13:54 +08001792 ("EarlyData: rejected, feature disabled in server configuration."));
Jerry Yu4caf3ca2023-11-15 16:13:47 +08001793 return -1;
Ronald Cron7b6ee942024-01-12 10:29:55 +01001794 }
1795
Jerry Yu454dda32023-10-31 15:13:54 +08001796 if (!handshake->resume) {
1797 /* We currently support early data only in the case of PSKs established
1798 via a NewSessionTicket message thus in the case of a session
1799 resumption. */
Jerry Yu985c9672022-12-04 14:06:30 +08001800 MBEDTLS_SSL_DEBUG_MSG(
Jerry Yu7ef9fd82023-11-07 14:30:38 +08001801 1, ("EarlyData: rejected, not a session resumption."));
Jerry Yu4caf3ca2023-11-15 16:13:47 +08001802 return -1;
Jerry Yu985c9672022-12-04 14:06:30 +08001803 }
1804
Jerry Yu82fd6c12023-10-31 16:32:19 +08001805 /* RFC 8446 4.2.10
1806 *
1807 * In order to accept early data, the server MUST have accepted a PSK cipher
1808 * suite and selected the first key offered in the client's "pre_shared_key"
1809 * extension. In addition, it MUST verify that the following values are the
1810 * same as those associated with the selected PSK:
1811 * - The TLS version number
1812 * - The selected cipher suite
1813 * - The selected ALPN [RFC7301] protocol, if any
1814 *
1815 * NOTE:
Jerry Yu7ef9fd82023-11-07 14:30:38 +08001816 * - The TLS version number is checked in
1817 * ssl_tls13_offered_psks_check_identity_match_ticket().
Jerry Yu82fd6c12023-10-31 16:32:19 +08001818 */
1819
1820 if (handshake->selected_identity != 0) {
1821 MBEDTLS_SSL_DEBUG_MSG(
Jerry Yu7ef9fd82023-11-07 14:30:38 +08001822 1, ("EarlyData: rejected, the selected key in "
1823 "`pre_shared_key` is not the first one."));
Jerry Yu4caf3ca2023-11-15 16:13:47 +08001824 return -1;
Jerry Yu82fd6c12023-10-31 16:32:19 +08001825 }
1826
1827 if (handshake->ciphersuite_info->id !=
1828 ssl->session_negotiate->ciphersuite) {
1829 MBEDTLS_SSL_DEBUG_MSG(
Jerry Yu7ef9fd82023-11-07 14:30:38 +08001830 1, ("EarlyData: rejected, the selected ciphersuite is not the one "
1831 "of the selected pre-shared key."));
Jerry Yu4caf3ca2023-11-15 16:13:47 +08001832 return -1;
Jerry Yu82fd6c12023-10-31 16:32:19 +08001833
1834 }
Jerry Yu985c9672022-12-04 14:06:30 +08001835
Pengyu Lv94a42cc2023-12-06 10:04:17 +08001836 if (!mbedtls_ssl_tls13_session_ticket_allow_early_data(ssl->session_negotiate)) {
Jerry Yufceddb32022-12-12 15:30:34 +08001837 MBEDTLS_SSL_DEBUG_MSG(
1838 1,
Jerry Yuea96ac32023-11-21 17:06:36 +08001839 ("EarlyData: rejected, early_data not allowed in ticket "
1840 "permission bits."));
Jerry Yu4caf3ca2023-11-15 16:13:47 +08001841 return -1;
Jerry Yufceddb32022-12-12 15:30:34 +08001842 }
Jerry Yu985c9672022-12-04 14:06:30 +08001843
Waleed Elmelegy4dfb0e72024-03-14 01:48:40 +00001844#if defined(MBEDTLS_SSL_ALPN)
1845 const char *alpn = mbedtls_ssl_get_alpn_protocol(ssl);
1846 size_t alpn_len;
1847
1848 if (alpn == NULL && ssl->session_negotiate->ticket_alpn == NULL) {
1849 return 0;
1850 }
1851
1852 if (alpn != NULL) {
1853 alpn_len = strlen(alpn);
1854 }
1855
1856 if (alpn == NULL ||
1857 ssl->session_negotiate->ticket_alpn == NULL ||
1858 alpn_len != strlen(ssl->session_negotiate->ticket_alpn) ||
1859 (memcmp(alpn, ssl->session_negotiate->ticket_alpn, alpn_len) != 0)) {
1860 MBEDTLS_SSL_DEBUG_MSG(1, ("EarlyData: rejected, the selected ALPN is different "
1861 "from the one associated with the pre-shared key."));
1862 return -1;
1863 }
1864#endif
1865
Jerry Yu4caf3ca2023-11-15 16:13:47 +08001866 return 0;
Jerry Yuab0da372023-02-08 13:55:24 +08001867}
1868#endif /* MBEDTLS_SSL_EARLY_DATA */
1869
XiaokangQian75fe8c72022-06-15 09:42:45 +00001870/* Update the handshake state machine */
1871
Ronald Cronce7d76e2022-07-08 18:56:49 +02001872MBEDTLS_CHECK_RETURN_CRITICAL
Ronald Cron7b6ee942024-01-12 10:29:55 +01001873static int ssl_tls13_postprocess_client_hello(mbedtls_ssl_context *ssl,
1874 int hrr_required)
XiaokangQian75fe8c72022-06-15 09:42:45 +00001875{
1876 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1877
XiaokangQian7807f9f2022-02-15 10:04:37 +00001878 /*
XiaokangQianfb665a82022-06-15 03:57:21 +00001879 * Server certificate selection
1880 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001881 if (ssl->conf->f_cert_cb && (ret = ssl->conf->f_cert_cb(ssl)) != 0) {
1882 MBEDTLS_SSL_DEBUG_RET(1, "f_cert_cb", ret);
1883 return ret;
XiaokangQianfb665a82022-06-15 03:57:21 +00001884 }
1885#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
1886 ssl->handshake->sni_name = NULL;
1887 ssl->handshake->sni_name_len = 0;
1888#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
XiaokangQian7807f9f2022-02-15 10:04:37 +00001889
Gilles Peskine449bd832023-01-11 14:50:10 +01001890 ret = mbedtls_ssl_tls13_key_schedule_stage_early(ssl);
1891 if (ret != 0) {
1892 MBEDTLS_SSL_DEBUG_RET(1,
1893 "mbedtls_ssl_tls1_3_key_schedule_stage_early", ret);
1894 return ret;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001895 }
1896
Jerry Yuab0da372023-02-08 13:55:24 +08001897#if defined(MBEDTLS_SSL_EARLY_DATA)
Jerry Yu4caf3ca2023-11-15 16:13:47 +08001898 if (ssl->handshake->received_extensions & MBEDTLS_SSL_EXT_MASK(EARLY_DATA)) {
Ronald Cron31e2d832024-02-05 16:45:57 +01001899 ssl->handshake->early_data_accepted =
1900 (!hrr_required) && (ssl_tls13_check_early_data_requirements(ssl) == 0);
Jerry Yu4e9b70e2022-12-04 14:08:02 +08001901
Jerry Yu4caf3ca2023-11-15 16:13:47 +08001902 if (ssl->handshake->early_data_accepted) {
1903 ret = mbedtls_ssl_tls13_compute_early_transform(ssl);
1904 if (ret != 0) {
1905 MBEDTLS_SSL_DEBUG_RET(
1906 1, "mbedtls_ssl_tls13_compute_early_transform", ret);
1907 return ret;
1908 }
1909 } else {
1910 ssl->discard_early_data_record =
1911 hrr_required ?
1912 MBEDTLS_SSL_EARLY_DATA_DISCARD :
1913 MBEDTLS_SSL_EARLY_DATA_TRY_TO_DEPROTECT_AND_DISCARD;
Jerry Yu4e9b70e2022-12-04 14:08:02 +08001914 }
1915 }
Ronald Cron7b6ee942024-01-12 10:29:55 +01001916#else
1917 ((void) hrr_required);
Jerry Yuab0da372023-02-08 13:55:24 +08001918#endif /* MBEDTLS_SSL_EARLY_DATA */
1919
Gilles Peskine449bd832023-01-11 14:50:10 +01001920 return 0;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001921}
1922
1923/*
XiaokangQianed582dd2022-04-13 08:21:05 +00001924 * Main entry point from the state machine; orchestrates the otherfunctions.
1925 */
1926
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001927MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01001928static int ssl_tls13_process_client_hello(mbedtls_ssl_context *ssl)
XiaokangQianed582dd2022-04-13 08:21:05 +00001929{
1930
XiaokangQian08037552022-04-20 07:16:41 +00001931 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine449bd832023-01-11 14:50:10 +01001932 unsigned char *buf = NULL;
XiaokangQianed582dd2022-04-13 08:21:05 +00001933 size_t buflen = 0;
Jerry Yu4ca91402022-05-09 15:50:57 +08001934 int parse_client_hello_ret;
1935
Gilles Peskine449bd832023-01-11 14:50:10 +01001936 MBEDTLS_SSL_DEBUG_MSG(2, ("=> parse client hello"));
XiaokangQianed582dd2022-04-13 08:21:05 +00001937
Gilles Peskine449bd832023-01-11 14:50:10 +01001938 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_tls13_fetch_handshake_msg(
1939 ssl, MBEDTLS_SSL_HS_CLIENT_HELLO,
1940 &buf, &buflen));
XiaokangQianed582dd2022-04-13 08:21:05 +00001941
Gilles Peskine449bd832023-01-11 14:50:10 +01001942 MBEDTLS_SSL_PROC_CHK_NEG(ssl_tls13_parse_client_hello(ssl, buf,
1943 buf + buflen));
Ronald Cron5af4c7f2023-03-07 20:46:59 +01001944 parse_client_hello_ret = ret; /* Store positive return value of
1945 * parse_client_hello,
1946 * as negative error codes are handled
Jerry Yuf41553b2022-05-09 22:20:30 +08001947 * by MBEDTLS_SSL_PROC_CHK_NEG. */
Jerry Yu4ca91402022-05-09 15:50:57 +08001948
Ronald Cronb828c7d2023-04-03 16:37:22 +02001949 /*
Yanray Wang90acdc62023-12-08 10:29:42 +08001950 * Version 1.2 of the protocol has to be used for the handshake.
1951 * If TLS 1.2 is not supported, abort the handshake. Otherwise, set the
Ronald Cronb828c7d2023-04-03 16:37:22 +02001952 * ssl->keep_current_message flag for the ClientHello to be kept and parsed
1953 * as a TLS 1.2 ClientHello. We also change ssl->tls_version to
1954 * MBEDTLS_SSL_VERSION_TLS1_2 thus from now on mbedtls_ssl_handshake_step()
1955 * will dispatch to the TLS 1.2 state machine.
1956 */
Ronald Cron5af4c7f2023-03-07 20:46:59 +01001957 if (SSL_CLIENT_HELLO_TLS1_2 == parse_client_hello_ret) {
Yanray Wangfb0f47b2023-12-04 15:27:28 +08001958 /* Check if server supports TLS 1.2 */
Yanray Wang408ba6f2023-12-08 10:18:03 +08001959 if (!mbedtls_ssl_conf_is_tls12_enabled(ssl->conf)) {
Yanray Wangfb0f47b2023-12-04 15:27:28 +08001960 MBEDTLS_SSL_DEBUG_MSG(
Yanray Wang177e49a2023-12-08 10:51:04 +08001961 1, ("TLS 1.2 not supported."));
Yanray Wangfb0f47b2023-12-04 15:27:28 +08001962 MBEDTLS_SSL_PEND_FATAL_ALERT(
Yanray Wang2bef9172023-12-08 10:21:53 +08001963 MBEDTLS_SSL_ALERT_MSG_PROTOCOL_VERSION,
1964 MBEDTLS_ERR_SSL_BAD_PROTOCOL_VERSION);
1965 return MBEDTLS_ERR_SSL_BAD_PROTOCOL_VERSION;
Yanray Wangfb0f47b2023-12-04 15:27:28 +08001966 }
Ronald Cron5af4c7f2023-03-07 20:46:59 +01001967 ssl->keep_current_message = 1;
1968 ssl->tls_version = MBEDTLS_SSL_VERSION_TLS1_2;
Manuel Pégourié-Gonnard6637ef72025-02-11 13:19:45 +01001969 MBEDTLS_SSL_DEBUG_MSG(1, ("non-1.3 ClientHello left for later processing"));
Ronald Cron5af4c7f2023-03-07 20:46:59 +01001970 return 0;
1971 }
1972
Ronald Cron7b6ee942024-01-12 10:29:55 +01001973 MBEDTLS_SSL_PROC_CHK(
1974 ssl_tls13_postprocess_client_hello(ssl, parse_client_hello_ret ==
1975 SSL_CLIENT_HELLO_HRR_REQUIRED));
Jerry Yu582dd062022-04-22 21:59:01 +08001976
Ronald Cron5af4c7f2023-03-07 20:46:59 +01001977 if (SSL_CLIENT_HELLO_OK == parse_client_hello_ret) {
Gilles Peskine449bd832023-01-11 14:50:10 +01001978 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_SERVER_HELLO);
1979 } else {
1980 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_HELLO_RETRY_REQUEST);
1981 }
XiaokangQianed582dd2022-04-13 08:21:05 +00001982
1983cleanup:
1984
Gilles Peskine449bd832023-01-11 14:50:10 +01001985 MBEDTLS_SSL_DEBUG_MSG(2, ("<= parse client hello"));
1986 return ret;
XiaokangQianed582dd2022-04-13 08:21:05 +00001987}
1988
1989/*
Jerry Yu1c3e6882022-04-20 21:23:40 +08001990 * Handler for MBEDTLS_SSL_SERVER_HELLO
Jerry Yu5b64ae92022-03-30 17:15:02 +08001991 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001992MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01001993static int ssl_tls13_prepare_server_hello(mbedtls_ssl_context *ssl)
Jerry Yuf4b27e42022-03-30 17:32:21 +08001994{
Jerry Yu637a3f12022-04-20 21:37:58 +08001995 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1996 unsigned char *server_randbytes =
Gilles Peskine449bd832023-01-11 14:50:10 +01001997 ssl->handshake->randbytes + MBEDTLS_CLIENT_HELLO_RANDOM_LEN;
Jerry Yuf4b27e42022-03-30 17:32:21 +08001998
Gilles Peskine449bd832023-01-11 14:50:10 +01001999 if ((ret = ssl->conf->f_rng(ssl->conf->p_rng, server_randbytes,
2000 MBEDTLS_SERVER_HELLO_RANDOM_LEN)) != 0) {
2001 MBEDTLS_SSL_DEBUG_RET(1, "f_rng", ret);
2002 return ret;
Jerry Yuf4b27e42022-03-30 17:32:21 +08002003 }
2004
Gilles Peskine449bd832023-01-11 14:50:10 +01002005 MBEDTLS_SSL_DEBUG_BUF(3, "server hello, random bytes", server_randbytes,
2006 MBEDTLS_SERVER_HELLO_RANDOM_LEN);
Jerry Yuf4b27e42022-03-30 17:32:21 +08002007
2008#if defined(MBEDTLS_HAVE_TIME)
YxCda609132023-05-22 12:08:12 -07002009 ssl->session_negotiate->start = mbedtls_time(NULL);
Jerry Yuf4b27e42022-03-30 17:32:21 +08002010#endif /* MBEDTLS_HAVE_TIME */
2011
Gilles Peskine449bd832023-01-11 14:50:10 +01002012 return ret;
Jerry Yuf4b27e42022-03-30 17:32:21 +08002013}
2014
Jerry Yu3bf2c642022-03-30 22:02:12 +08002015/*
Jerry Yue74e04a2022-04-21 09:23:16 +08002016 * ssl_tls13_write_server_hello_supported_versions_ext ():
Jerry Yu3bf2c642022-03-30 22:02:12 +08002017 *
2018 * struct {
Jerry Yufb9f54d2022-04-06 10:08:34 +08002019 * ProtocolVersion selected_version;
Jerry Yu3bf2c642022-03-30 22:02:12 +08002020 * } SupportedVersions;
2021 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002022MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yue74e04a2022-04-21 09:23:16 +08002023static int ssl_tls13_write_server_hello_supported_versions_ext(
Gilles Peskine449bd832023-01-11 14:50:10 +01002024 mbedtls_ssl_context *ssl,
2025 unsigned char *buf,
2026 unsigned char *end,
2027 size_t *out_len)
Jerry Yu3bf2c642022-03-30 22:02:12 +08002028{
Jerry Yu3bf2c642022-03-30 22:02:12 +08002029 *out_len = 0;
2030
Gilles Peskine449bd832023-01-11 14:50:10 +01002031 MBEDTLS_SSL_DEBUG_MSG(3, ("server hello, write selected version"));
Jerry Yu3bf2c642022-03-30 22:02:12 +08002032
2033 /* Check if we have space to write the extension:
2034 * - extension_type (2 bytes)
2035 * - extension_data_length (2 bytes)
Jerry Yu349a6132022-04-14 20:52:56 +08002036 * - selected_version (2 bytes)
Jerry Yu3bf2c642022-03-30 22:02:12 +08002037 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002038 MBEDTLS_SSL_CHK_BUF_PTR(buf, end, 6);
Jerry Yu3bf2c642022-03-30 22:02:12 +08002039
Gilles Peskine449bd832023-01-11 14:50:10 +01002040 MBEDTLS_PUT_UINT16_BE(MBEDTLS_TLS_EXT_SUPPORTED_VERSIONS, buf, 0);
Jerry Yu3bf2c642022-03-30 22:02:12 +08002041
Gilles Peskine449bd832023-01-11 14:50:10 +01002042 MBEDTLS_PUT_UINT16_BE(2, buf, 2);
Jerry Yu3bf2c642022-03-30 22:02:12 +08002043
Gilles Peskine449bd832023-01-11 14:50:10 +01002044 mbedtls_ssl_write_version(buf + 4,
2045 ssl->conf->transport,
2046 ssl->tls_version);
Jerry Yu3bf2c642022-03-30 22:02:12 +08002047
Gilles Peskine449bd832023-01-11 14:50:10 +01002048 MBEDTLS_SSL_DEBUG_MSG(3, ("supported version: [%04x]",
2049 ssl->tls_version));
Jerry Yu3bf2c642022-03-30 22:02:12 +08002050
Jerry Yu349a6132022-04-14 20:52:56 +08002051 *out_len = 6;
Jerry Yu3bf2c642022-03-30 22:02:12 +08002052
Jerry Yub95dd362022-11-08 21:19:34 +08002053 mbedtls_ssl_tls13_set_hs_sent_ext_mask(
Gilles Peskine449bd832023-01-11 14:50:10 +01002054 ssl, MBEDTLS_TLS_EXT_SUPPORTED_VERSIONS);
Jerry Yub95dd362022-11-08 21:19:34 +08002055
Gilles Peskine449bd832023-01-11 14:50:10 +01002056 return 0;
Jerry Yu3bf2c642022-03-30 22:02:12 +08002057}
2058
Jerry Yud9436a12022-04-20 22:28:09 +08002059
Jerry Yu3bf2c642022-03-30 22:02:12 +08002060
2061/* Generate and export a single key share. For hybrid KEMs, this can
2062 * be called multiple times with the different components of the hybrid. */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002063MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002064static int ssl_tls13_generate_and_write_key_share(mbedtls_ssl_context *ssl,
2065 uint16_t named_group,
2066 unsigned char *buf,
2067 unsigned char *end,
2068 size_t *out_len)
Jerry Yu3bf2c642022-03-30 22:02:12 +08002069{
2070 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yu955ddd72022-04-22 22:27:33 +08002071
2072 *out_len = 0;
2073
Valerio Settic9ae8622023-07-25 11:23:50 +02002074#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_EPHEMERAL_ENABLED)
Przemek Stekiel29c219c2023-05-31 15:21:04 +02002075 if (mbedtls_ssl_tls13_named_group_is_ecdhe(named_group) ||
Przemek Stekield5f79e72023-06-29 09:08:43 +02002076 mbedtls_ssl_tls13_named_group_is_ffdh(named_group)) {
Przemek Stekiel408569f2023-07-06 11:26:44 +02002077 ret = mbedtls_ssl_tls13_generate_and_write_xxdh_key_exchange(
Gilles Peskine449bd832023-01-11 14:50:10 +01002078 ssl, named_group, buf, end, out_len);
2079 if (ret != 0) {
Jerry Yu89e103c2022-03-30 22:43:29 +08002080 MBEDTLS_SSL_DEBUG_RET(
Przemek Stekiel408569f2023-07-06 11:26:44 +02002081 1, "mbedtls_ssl_tls13_generate_and_write_xxdh_key_exchange",
Gilles Peskine449bd832023-01-11 14:50:10 +01002082 ret);
2083 return ret;
Jerry Yu3bf2c642022-03-30 22:02:12 +08002084 }
Gilles Peskine449bd832023-01-11 14:50:10 +01002085 } else
Valerio Settic9ae8622023-07-25 11:23:50 +02002086#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_EPHEMERAL_ENABLED */
Gilles Peskine449bd832023-01-11 14:50:10 +01002087 if (0 /* Other kinds of KEMs */) {
2088 } else {
Jerry Yu955ddd72022-04-22 22:27:33 +08002089 ((void) ssl);
2090 ((void) named_group);
2091 ((void) buf);
2092 ((void) end);
Jerry Yu3bf2c642022-03-30 22:02:12 +08002093 ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
2094 }
2095
Gilles Peskine449bd832023-01-11 14:50:10 +01002096 return ret;
Jerry Yu3bf2c642022-03-30 22:02:12 +08002097}
2098
2099/*
2100 * ssl_tls13_write_key_share_ext
2101 *
2102 * Structure of key_share extension in ServerHello:
2103 *
Jerry Yu1c3e6882022-04-20 21:23:40 +08002104 * struct {
2105 * NamedGroup group;
2106 * opaque key_exchange<1..2^16-1>;
2107 * } KeyShareEntry;
2108 * struct {
2109 * KeyShareEntry server_share;
2110 * } KeyShareServerHello;
Jerry Yu3bf2c642022-03-30 22:02:12 +08002111 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002112MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002113static int ssl_tls13_write_key_share_ext(mbedtls_ssl_context *ssl,
2114 unsigned char *buf,
2115 unsigned char *end,
2116 size_t *out_len)
Jerry Yu3bf2c642022-03-30 22:02:12 +08002117{
Jerry Yu955ddd72022-04-22 22:27:33 +08002118 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yu3bf2c642022-03-30 22:02:12 +08002119 unsigned char *p = buf;
Jerry Yu57d48412022-04-20 21:50:42 +08002120 uint16_t group = ssl->handshake->offered_group_id;
Jerry Yu3bf2c642022-03-30 22:02:12 +08002121 unsigned char *server_share = buf + 4;
Jerry Yu3bf2c642022-03-30 22:02:12 +08002122 size_t key_exchange_length;
Jerry Yu3bf2c642022-03-30 22:02:12 +08002123
2124 *out_len = 0;
2125
Gilles Peskine449bd832023-01-11 14:50:10 +01002126 MBEDTLS_SSL_DEBUG_MSG(3, ("server hello, adding key share extension"));
Jerry Yu3bf2c642022-03-30 22:02:12 +08002127
Gilles Peskine449bd832023-01-11 14:50:10 +01002128 MBEDTLS_SSL_DEBUG_MSG(2, ("server hello, write selected_group: %s (%04x)",
2129 mbedtls_ssl_named_group_to_str(group),
2130 group));
Jerry Yu58af2332022-09-06 11:19:31 +08002131
Jerry Yu3bf2c642022-03-30 22:02:12 +08002132 /* Check if we have space for header and length fields:
2133 * - extension_type (2 bytes)
2134 * - extension_data_length (2 bytes)
2135 * - group (2 bytes)
2136 * - key_exchange_length (2 bytes)
2137 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002138 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 8);
2139 MBEDTLS_PUT_UINT16_BE(MBEDTLS_TLS_EXT_KEY_SHARE, p, 0);
2140 MBEDTLS_PUT_UINT16_BE(group, server_share, 0);
Jerry Yu3bf2c642022-03-30 22:02:12 +08002141 p += 8;
Jerry Yu57d48412022-04-20 21:50:42 +08002142
Jerry Yu3bf2c642022-03-30 22:02:12 +08002143 /* When we introduce PQC-ECDHE hybrids, we'll want to call this
2144 * function multiple times. */
Jerry Yu955ddd72022-04-22 22:27:33 +08002145 ret = ssl_tls13_generate_and_write_key_share(
Gilles Peskine449bd832023-01-11 14:50:10 +01002146 ssl, group, server_share + 4, end, &key_exchange_length);
2147 if (ret != 0) {
2148 return ret;
2149 }
Jerry Yu3bf2c642022-03-30 22:02:12 +08002150 p += key_exchange_length;
Jerry Yuc1be19f2022-04-23 16:11:39 +08002151
Gilles Peskine449bd832023-01-11 14:50:10 +01002152 MBEDTLS_PUT_UINT16_BE(key_exchange_length, server_share + 2, 0);
Jerry Yu3bf2c642022-03-30 22:02:12 +08002153
Gilles Peskine449bd832023-01-11 14:50:10 +01002154 MBEDTLS_PUT_UINT16_BE(p - server_share, buf, 2);
Jerry Yu3bf2c642022-03-30 22:02:12 +08002155
Jerry Yu57d48412022-04-20 21:50:42 +08002156 *out_len = p - buf;
Jerry Yu955ddd72022-04-22 22:27:33 +08002157
Gilles Peskine449bd832023-01-11 14:50:10 +01002158 mbedtls_ssl_tls13_set_hs_sent_ext_mask(ssl, MBEDTLS_TLS_EXT_KEY_SHARE);
Jerry Yub95dd362022-11-08 21:19:34 +08002159
Gilles Peskine449bd832023-01-11 14:50:10 +01002160 return 0;
Jerry Yu3bf2c642022-03-30 22:02:12 +08002161}
Jerry Yud9436a12022-04-20 22:28:09 +08002162
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002163MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002164static int ssl_tls13_write_hrr_key_share_ext(mbedtls_ssl_context *ssl,
2165 unsigned char *buf,
2166 unsigned char *end,
2167 size_t *out_len)
Jerry Yu23f7a6f2022-04-23 15:16:45 +08002168{
2169 uint16_t selected_group = ssl->handshake->hrr_selected_group;
2170 /* key_share Extension
2171 *
2172 * struct {
2173 * select (Handshake.msg_type) {
2174 * ...
2175 * case hello_retry_request:
2176 * NamedGroup selected_group;
2177 * ...
2178 * };
2179 * } KeyShare;
2180 */
2181
2182 *out_len = 0;
2183
Jerry Yub0ac10b2022-05-05 11:10:08 +08002184 /*
2185 * For a pure PSK key exchange, there is no group to agree upon. The purpose
2186 * of the HRR is then to transmit a cookie to force the client to demonstrate
2187 * reachability at their apparent network address (primarily useful for DTLS).
2188 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002189 if (!mbedtls_ssl_tls13_key_exchange_mode_with_ephemeral(ssl)) {
2190 return 0;
2191 }
Jerry Yu23f7a6f2022-04-23 15:16:45 +08002192
2193 /* We should only send the key_share extension if the client's initial
2194 * key share was not acceptable. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002195 if (ssl->handshake->offered_group_id != 0) {
2196 MBEDTLS_SSL_DEBUG_MSG(4, ("Skip key_share extension in HRR"));
2197 return 0;
Jerry Yu23f7a6f2022-04-23 15:16:45 +08002198 }
2199
Gilles Peskine449bd832023-01-11 14:50:10 +01002200 if (selected_group == 0) {
2201 MBEDTLS_SSL_DEBUG_MSG(1, ("no matching named group found"));
2202 return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
Jerry Yu23f7a6f2022-04-23 15:16:45 +08002203 }
2204
Jerry Yub0ac10b2022-05-05 11:10:08 +08002205 /* Check if we have enough space:
2206 * - extension_type (2 bytes)
2207 * - extension_data_length (2 bytes)
2208 * - selected_group (2 bytes)
2209 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002210 MBEDTLS_SSL_CHK_BUF_PTR(buf, end, 6);
Jerry Yu23f7a6f2022-04-23 15:16:45 +08002211
Gilles Peskine449bd832023-01-11 14:50:10 +01002212 MBEDTLS_PUT_UINT16_BE(MBEDTLS_TLS_EXT_KEY_SHARE, buf, 0);
2213 MBEDTLS_PUT_UINT16_BE(2, buf, 2);
2214 MBEDTLS_PUT_UINT16_BE(selected_group, buf, 4);
Jerry Yu23f7a6f2022-04-23 15:16:45 +08002215
Gilles Peskine449bd832023-01-11 14:50:10 +01002216 MBEDTLS_SSL_DEBUG_MSG(3,
2217 ("HRR selected_group: %s (%x)",
2218 mbedtls_ssl_named_group_to_str(selected_group),
2219 selected_group));
Jerry Yu23f7a6f2022-04-23 15:16:45 +08002220
2221 *out_len = 6;
Jerry Yu23f7a6f2022-04-23 15:16:45 +08002222
Gilles Peskine449bd832023-01-11 14:50:10 +01002223 mbedtls_ssl_tls13_set_hs_sent_ext_mask(ssl, MBEDTLS_TLS_EXT_KEY_SHARE);
Jerry Yub95dd362022-11-08 21:19:34 +08002224
Gilles Peskine449bd832023-01-11 14:50:10 +01002225 return 0;
Jerry Yu23f7a6f2022-04-23 15:16:45 +08002226}
Jerry Yu3bf2c642022-03-30 22:02:12 +08002227
2228/*
2229 * Structure of ServerHello message:
2230 *
2231 * struct {
2232 * ProtocolVersion legacy_version = 0x0303; // TLS v1.2
2233 * Random random;
2234 * opaque legacy_session_id_echo<0..32>;
2235 * CipherSuite cipher_suite;
2236 * uint8 legacy_compression_method = 0;
2237 * Extension extensions<6..2^16-1>;
2238 * } ServerHello;
2239 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002240MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002241static int ssl_tls13_write_server_hello_body(mbedtls_ssl_context *ssl,
2242 unsigned char *buf,
2243 unsigned char *end,
2244 size_t *out_len,
2245 int is_hrr)
Jerry Yu56404d72022-03-30 17:36:13 +08002246{
Jerry Yu955ddd72022-04-22 22:27:33 +08002247 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yu3bf2c642022-03-30 22:02:12 +08002248 unsigned char *p = buf;
Jerry Yud9436a12022-04-20 22:28:09 +08002249 unsigned char *p_extensions_len;
Jerry Yufbe3e642022-04-25 19:31:51 +08002250 size_t output_len;
Jerry Yu3bf2c642022-03-30 22:02:12 +08002251
2252 *out_len = 0;
Jerry Yu50e00e32022-10-31 14:45:01 +08002253 ssl->handshake->sent_extensions = MBEDTLS_SSL_EXT_MASK_NONE;
Jerry Yu3bf2c642022-03-30 22:02:12 +08002254
Jerry Yucfc04b32022-04-21 09:31:58 +08002255 /* ...
2256 * ProtocolVersion legacy_version = 0x0303; // TLS 1.2
2257 * ...
2258 * with ProtocolVersion defined as:
2259 * uint16 ProtocolVersion;
Jerry Yu3bf2c642022-03-30 22:02:12 +08002260 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002261 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 2);
2262 MBEDTLS_PUT_UINT16_BE(0x0303, p, 0);
Jerry Yu3bf2c642022-03-30 22:02:12 +08002263 p += 2;
2264
Jerry Yu1c3e6882022-04-20 21:23:40 +08002265 /* ...
2266 * Random random;
2267 * ...
Jerry Yucfc04b32022-04-21 09:31:58 +08002268 * with Random defined as:
2269 * opaque Random[MBEDTLS_SERVER_HELLO_RANDOM_LEN];
Jerry Yu1c3e6882022-04-20 21:23:40 +08002270 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002271 MBEDTLS_SSL_CHK_BUF_PTR(p, end, MBEDTLS_SERVER_HELLO_RANDOM_LEN);
2272 if (is_hrr) {
2273 memcpy(p, mbedtls_ssl_tls13_hello_retry_request_magic,
2274 MBEDTLS_SERVER_HELLO_RANDOM_LEN);
2275 } else {
2276 memcpy(p, &ssl->handshake->randbytes[MBEDTLS_CLIENT_HELLO_RANDOM_LEN],
2277 MBEDTLS_SERVER_HELLO_RANDOM_LEN);
Jerry Yu23f7a6f2022-04-23 15:16:45 +08002278 }
Gilles Peskine449bd832023-01-11 14:50:10 +01002279 MBEDTLS_SSL_DEBUG_BUF(3, "server hello, random bytes",
2280 p, MBEDTLS_SERVER_HELLO_RANDOM_LEN);
Jerry Yu3bf2c642022-03-30 22:02:12 +08002281 p += MBEDTLS_SERVER_HELLO_RANDOM_LEN;
2282
Jerry Yucfc04b32022-04-21 09:31:58 +08002283 /* ...
2284 * opaque legacy_session_id_echo<0..32>;
2285 * ...
Jerry Yu3bf2c642022-03-30 22:02:12 +08002286 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002287 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 1 + ssl->session_negotiate->id_len);
2288 *p++ = (unsigned char) ssl->session_negotiate->id_len;
2289 if (ssl->session_negotiate->id_len > 0) {
2290 memcpy(p, &ssl->session_negotiate->id[0],
2291 ssl->session_negotiate->id_len);
Jerry Yu3bf2c642022-03-30 22:02:12 +08002292 p += ssl->session_negotiate->id_len;
Jerry Yu955ddd72022-04-22 22:27:33 +08002293
Gilles Peskine449bd832023-01-11 14:50:10 +01002294 MBEDTLS_SSL_DEBUG_BUF(3, "session id", ssl->session_negotiate->id,
2295 ssl->session_negotiate->id_len);
Jerry Yu3bf2c642022-03-30 22:02:12 +08002296 }
2297
Jerry Yucfc04b32022-04-21 09:31:58 +08002298 /* ...
2299 * CipherSuite cipher_suite;
2300 * ...
2301 * with CipherSuite defined as:
2302 * uint8 CipherSuite[2];
Jerry Yu3bf2c642022-03-30 22:02:12 +08002303 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002304 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 2);
2305 MBEDTLS_PUT_UINT16_BE(ssl->session_negotiate->ciphersuite, p, 0);
Jerry Yu3bf2c642022-03-30 22:02:12 +08002306 p += 2;
Gilles Peskine449bd832023-01-11 14:50:10 +01002307 MBEDTLS_SSL_DEBUG_MSG(3,
2308 ("server hello, chosen ciphersuite: %s ( id=%d )",
2309 mbedtls_ssl_get_ciphersuite_name(
2310 ssl->session_negotiate->ciphersuite),
2311 ssl->session_negotiate->ciphersuite));
Jerry Yu3bf2c642022-03-30 22:02:12 +08002312
Jerry Yucfc04b32022-04-21 09:31:58 +08002313 /* ...
2314 * uint8 legacy_compression_method = 0;
2315 * ...
2316 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002317 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 1);
Thomas Daubney31e03a82022-07-25 15:59:25 +01002318 *p++ = MBEDTLS_SSL_COMPRESS_NULL;
Jerry Yu3bf2c642022-03-30 22:02:12 +08002319
Jerry Yucfc04b32022-04-21 09:31:58 +08002320 /* ...
2321 * Extension extensions<6..2^16-1>;
2322 * ...
2323 * struct {
2324 * ExtensionType extension_type; (2 bytes)
2325 * opaque extension_data<0..2^16-1>;
2326 * } Extension;
2327 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002328 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 2);
Jerry Yud9436a12022-04-20 22:28:09 +08002329 p_extensions_len = p;
Jerry Yu3bf2c642022-03-30 22:02:12 +08002330 p += 2;
2331
Gilles Peskine449bd832023-01-11 14:50:10 +01002332 if ((ret = ssl_tls13_write_server_hello_supported_versions_ext(
2333 ssl, p, end, &output_len)) != 0) {
Jerry Yu955ddd72022-04-22 22:27:33 +08002334 MBEDTLS_SSL_DEBUG_RET(
Gilles Peskine449bd832023-01-11 14:50:10 +01002335 1, "ssl_tls13_write_server_hello_supported_versions_ext", ret);
2336 return ret;
Jerry Yu3bf2c642022-03-30 22:02:12 +08002337 }
2338 p += output_len;
2339
Gilles Peskine449bd832023-01-11 14:50:10 +01002340 if (mbedtls_ssl_tls13_key_exchange_mode_with_ephemeral(ssl)) {
2341 if (is_hrr) {
2342 ret = ssl_tls13_write_hrr_key_share_ext(ssl, p, end, &output_len);
2343 } else {
2344 ret = ssl_tls13_write_key_share_ext(ssl, p, end, &output_len);
2345 }
2346 if (ret != 0) {
2347 return ret;
2348 }
Jerry Yu3bf2c642022-03-30 22:02:12 +08002349 p += output_len;
2350 }
Jerry Yu3bf2c642022-03-30 22:02:12 +08002351
Ronald Cron41a443a2022-10-04 16:38:25 +02002352#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01002353 if (!is_hrr && mbedtls_ssl_tls13_key_exchange_mode_with_psk(ssl)) {
2354 ret = ssl_tls13_write_server_pre_shared_key_ext(ssl, p, end, &output_len);
2355 if (ret != 0) {
2356 MBEDTLS_SSL_DEBUG_RET(1, "ssl_tls13_write_server_pre_shared_key_ext",
2357 ret);
2358 return ret;
Jerry Yu032b15ce2022-07-11 06:10:03 +00002359 }
2360 p += output_len;
2361 }
Ronald Cron41a443a2022-10-04 16:38:25 +02002362#endif
Jerry Yu032b15ce2022-07-11 06:10:03 +00002363
Gilles Peskine449bd832023-01-11 14:50:10 +01002364 MBEDTLS_PUT_UINT16_BE(p - p_extensions_len - 2, p_extensions_len, 0);
Jerry Yu3bf2c642022-03-30 22:02:12 +08002365
Gilles Peskine449bd832023-01-11 14:50:10 +01002366 MBEDTLS_SSL_DEBUG_BUF(4, "server hello extensions",
2367 p_extensions_len, p - p_extensions_len);
Jerry Yu3bf2c642022-03-30 22:02:12 +08002368
Jerry Yud9436a12022-04-20 22:28:09 +08002369 *out_len = p - buf;
Jerry Yu3bf2c642022-03-30 22:02:12 +08002370
Gilles Peskine449bd832023-01-11 14:50:10 +01002371 MBEDTLS_SSL_DEBUG_BUF(3, "server hello", buf, *out_len);
Jerry Yu3bf2c642022-03-30 22:02:12 +08002372
Jerry Yu7de2ff02022-11-08 21:43:46 +08002373 MBEDTLS_SSL_PRINT_EXTS(
Jerry Yu4b8f2f72022-10-31 13:31:22 +08002374 3, is_hrr ? MBEDTLS_SSL_TLS1_3_HS_HELLO_RETRY_REQUEST :
Gilles Peskine449bd832023-01-11 14:50:10 +01002375 MBEDTLS_SSL_HS_SERVER_HELLO,
2376 ssl->handshake->sent_extensions);
Jerry Yu4b8f2f72022-10-31 13:31:22 +08002377
Gilles Peskine449bd832023-01-11 14:50:10 +01002378 return ret;
Jerry Yu56404d72022-03-30 17:36:13 +08002379}
2380
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002381MBEDTLS_CHECK_RETURN_CRITICAL
Xiaokang Qian934ce6f2023-02-06 10:23:04 +00002382static int ssl_tls13_finalize_server_hello(mbedtls_ssl_context *ssl)
Jerry Yue110d252022-05-05 10:19:22 +08002383{
2384 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine449bd832023-01-11 14:50:10 +01002385 ret = mbedtls_ssl_tls13_compute_handshake_transform(ssl);
2386 if (ret != 0) {
2387 MBEDTLS_SSL_DEBUG_RET(1,
2388 "mbedtls_ssl_tls13_compute_handshake_transform",
2389 ret);
2390 return ret;
Jerry Yue110d252022-05-05 10:19:22 +08002391 }
2392
Gilles Peskine449bd832023-01-11 14:50:10 +01002393 return ret;
Jerry Yue110d252022-05-05 10:19:22 +08002394}
2395
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002396MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002397static int ssl_tls13_write_server_hello(mbedtls_ssl_context *ssl)
Jerry Yu5b64ae92022-03-30 17:15:02 +08002398{
Jerry Yu637a3f12022-04-20 21:37:58 +08002399 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yuf4b27e42022-03-30 17:32:21 +08002400 unsigned char *buf;
2401 size_t buf_len, msg_len;
2402
Gilles Peskine449bd832023-01-11 14:50:10 +01002403 MBEDTLS_SSL_DEBUG_MSG(2, ("=> write server hello"));
Jerry Yuf4b27e42022-03-30 17:32:21 +08002404
Gilles Peskine449bd832023-01-11 14:50:10 +01002405 MBEDTLS_SSL_PROC_CHK(ssl_tls13_prepare_server_hello(ssl));
Jerry Yuf4b27e42022-03-30 17:32:21 +08002406
Xiaokang Qian9f1747b2023-03-30 02:50:04 +00002407 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_start_handshake_msg(
2408 ssl, MBEDTLS_SSL_HS_SERVER_HELLO, &buf, &buf_len));
Jerry Yuf4b27e42022-03-30 17:32:21 +08002409
Gilles Peskine449bd832023-01-11 14:50:10 +01002410 MBEDTLS_SSL_PROC_CHK(ssl_tls13_write_server_hello_body(ssl, buf,
2411 buf + buf_len,
2412 &msg_len,
2413 0));
Jerry Yuf4b27e42022-03-30 17:32:21 +08002414
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +01002415 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_add_hs_msg_to_checksum(
Manuel Pégourié-Gonnard43cc1272023-02-06 11:48:19 +01002416 ssl, MBEDTLS_SSL_HS_SERVER_HELLO, buf, msg_len));
Jerry Yuf4b27e42022-03-30 17:32:21 +08002417
Gilles Peskine449bd832023-01-11 14:50:10 +01002418 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_finish_handshake_msg(
2419 ssl, buf_len, msg_len));
Jerry Yu637a3f12022-04-20 21:37:58 +08002420
Xiaokang Qian934ce6f2023-02-06 10:23:04 +00002421 MBEDTLS_SSL_PROC_CHK(ssl_tls13_finalize_server_hello(ssl));
Jerry Yue110d252022-05-05 10:19:22 +08002422
Gabor Mezei7b39bf12022-05-24 16:04:14 +02002423#if defined(MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE)
2424 /* The server sends a dummy change_cipher_spec record immediately
2425 * after its first handshake message. This may either be after
2426 * a ServerHello or a HelloRetryRequest.
2427 */
Gabor Mezei96ae9262022-06-28 11:45:18 +02002428 mbedtls_ssl_handshake_set_state(
Gilles Peskine449bd832023-01-11 14:50:10 +01002429 ssl, MBEDTLS_SSL_SERVER_CCS_AFTER_SERVER_HELLO);
Gabor Mezei7b39bf12022-05-24 16:04:14 +02002430#else
Gilles Peskine449bd832023-01-11 14:50:10 +01002431 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_ENCRYPTED_EXTENSIONS);
Gabor Mezei7b39bf12022-05-24 16:04:14 +02002432#endif /* MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE */
Jerry Yue110d252022-05-05 10:19:22 +08002433
Jerry Yuf4b27e42022-03-30 17:32:21 +08002434cleanup:
2435
Gilles Peskine449bd832023-01-11 14:50:10 +01002436 MBEDTLS_SSL_DEBUG_MSG(2, ("<= write server hello"));
2437 return ret;
Jerry Yu5b64ae92022-03-30 17:15:02 +08002438}
2439
Jerry Yu23d1a252022-05-12 18:08:59 +08002440
2441/*
2442 * Handler for MBEDTLS_SSL_HELLO_RETRY_REQUEST
2443 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002444MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002445static int ssl_tls13_prepare_hello_retry_request(mbedtls_ssl_context *ssl)
Jerry Yu23d1a252022-05-12 18:08:59 +08002446{
2447 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Ronald Cron5fbd2702024-02-14 10:03:36 +01002448 if (ssl->handshake->hello_retry_request_flag) {
Gilles Peskine449bd832023-01-11 14:50:10 +01002449 MBEDTLS_SSL_DEBUG_MSG(1, ("Too many HRRs"));
2450 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE,
2451 MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE);
2452 return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
Jerry Yu23d1a252022-05-12 18:08:59 +08002453 }
2454
2455 /*
2456 * Create stateless transcript hash for HRR
2457 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002458 MBEDTLS_SSL_DEBUG_MSG(4, ("Reset transcript for HRR"));
2459 ret = mbedtls_ssl_reset_transcript_for_hrr(ssl);
2460 if (ret != 0) {
2461 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_reset_transcript_for_hrr", ret);
2462 return ret;
Jerry Yu23d1a252022-05-12 18:08:59 +08002463 }
Gilles Peskine449bd832023-01-11 14:50:10 +01002464 mbedtls_ssl_session_reset_msg_layer(ssl, 0);
Jerry Yu23d1a252022-05-12 18:08:59 +08002465
Gilles Peskine449bd832023-01-11 14:50:10 +01002466 return 0;
Jerry Yu23d1a252022-05-12 18:08:59 +08002467}
2468
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002469MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002470static int ssl_tls13_write_hello_retry_request(mbedtls_ssl_context *ssl)
Jerry Yu23d1a252022-05-12 18:08:59 +08002471{
2472 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
2473 unsigned char *buf;
2474 size_t buf_len, msg_len;
2475
Gilles Peskine449bd832023-01-11 14:50:10 +01002476 MBEDTLS_SSL_DEBUG_MSG(2, ("=> write hello retry request"));
Jerry Yu23d1a252022-05-12 18:08:59 +08002477
Gilles Peskine449bd832023-01-11 14:50:10 +01002478 MBEDTLS_SSL_PROC_CHK(ssl_tls13_prepare_hello_retry_request(ssl));
Jerry Yu23d1a252022-05-12 18:08:59 +08002479
Gilles Peskine449bd832023-01-11 14:50:10 +01002480 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_start_handshake_msg(
2481 ssl, MBEDTLS_SSL_HS_SERVER_HELLO,
2482 &buf, &buf_len));
Jerry Yu23d1a252022-05-12 18:08:59 +08002483
Gilles Peskine449bd832023-01-11 14:50:10 +01002484 MBEDTLS_SSL_PROC_CHK(ssl_tls13_write_server_hello_body(ssl, buf,
2485 buf + buf_len,
2486 &msg_len,
2487 1));
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +01002488 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_add_hs_msg_to_checksum(
Manuel Pégourié-Gonnard43cc1272023-02-06 11:48:19 +01002489 ssl, MBEDTLS_SSL_HS_SERVER_HELLO, buf, msg_len));
Jerry Yu23d1a252022-05-12 18:08:59 +08002490
2491
Gilles Peskine449bd832023-01-11 14:50:10 +01002492 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_finish_handshake_msg(ssl, buf_len,
2493 msg_len));
Jerry Yu23d1a252022-05-12 18:08:59 +08002494
Ronald Cron5fbd2702024-02-14 10:03:36 +01002495 ssl->handshake->hello_retry_request_flag = 1;
Jerry Yu23d1a252022-05-12 18:08:59 +08002496
Gabor Mezei7b39bf12022-05-24 16:04:14 +02002497#if defined(MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE)
2498 /* The server sends a dummy change_cipher_spec record immediately
2499 * after its first handshake message. This may either be after
2500 * a ServerHello or a HelloRetryRequest.
2501 */
Gabor Mezei96ae9262022-06-28 11:45:18 +02002502 mbedtls_ssl_handshake_set_state(
Gilles Peskine449bd832023-01-11 14:50:10 +01002503 ssl, MBEDTLS_SSL_SERVER_CCS_AFTER_HELLO_RETRY_REQUEST);
Gabor Mezei7b39bf12022-05-24 16:04:14 +02002504#else
Gilles Peskine449bd832023-01-11 14:50:10 +01002505 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_CLIENT_HELLO);
Gabor Mezei7b39bf12022-05-24 16:04:14 +02002506#endif /* MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE */
Jerry Yu23d1a252022-05-12 18:08:59 +08002507
2508cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +01002509 MBEDTLS_SSL_DEBUG_MSG(2, ("<= write hello retry request"));
2510 return ret;
Jerry Yu23d1a252022-05-12 18:08:59 +08002511}
2512
Jerry Yu5b64ae92022-03-30 17:15:02 +08002513/*
Jerry Yu4d3841a2022-04-16 12:37:19 +08002514 * Handler for MBEDTLS_SSL_ENCRYPTED_EXTENSIONS
2515 */
Jerry Yu4d3841a2022-04-16 12:37:19 +08002516
2517/*
2518 * struct {
2519 * Extension extensions<0..2 ^ 16 - 1>;
2520 * } EncryptedExtensions;
2521 *
2522 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002523MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002524static int ssl_tls13_write_encrypted_extensions_body(mbedtls_ssl_context *ssl,
2525 unsigned char *buf,
2526 unsigned char *end,
2527 size_t *out_len)
Jerry Yu4d3841a2022-04-16 12:37:19 +08002528{
XiaokangQianacb39922022-06-17 10:18:48 +00002529 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yu4d3841a2022-04-16 12:37:19 +08002530 unsigned char *p = buf;
2531 size_t extensions_len = 0;
Jerry Yu8937eb42022-05-03 12:12:14 +08002532 unsigned char *p_extensions_len;
XiaokangQianacb39922022-06-17 10:18:48 +00002533 size_t output_len;
Jerry Yu9da5e5a2022-05-03 15:46:09 +08002534
Jerry Yu4d3841a2022-04-16 12:37:19 +08002535 *out_len = 0;
2536
Gilles Peskine449bd832023-01-11 14:50:10 +01002537 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 2);
Jerry Yu8937eb42022-05-03 12:12:14 +08002538 p_extensions_len = p;
Jerry Yu4d3841a2022-04-16 12:37:19 +08002539 p += 2;
2540
Jerry Yu8937eb42022-05-03 12:12:14 +08002541 ((void) ssl);
XiaokangQianacb39922022-06-17 10:18:48 +00002542 ((void) ret);
2543 ((void) output_len);
2544
2545#if defined(MBEDTLS_SSL_ALPN)
Gilles Peskine449bd832023-01-11 14:50:10 +01002546 ret = mbedtls_ssl_write_alpn_ext(ssl, p, end, &output_len);
2547 if (ret != 0) {
2548 return ret;
2549 }
XiaokangQian95d5f542022-06-24 02:29:26 +00002550 p += output_len;
XiaokangQianacb39922022-06-17 10:18:48 +00002551#endif /* MBEDTLS_SSL_ALPN */
Jerry Yu4d3841a2022-04-16 12:37:19 +08002552
Jerry Yu71c14f12022-12-12 11:10:35 +08002553#if defined(MBEDTLS_SSL_EARLY_DATA)
Ronald Cron78a38f62024-02-01 18:30:31 +01002554 if (ssl->handshake->early_data_accepted) {
Jerry Yu52335392023-11-23 18:06:06 +08002555 ret = mbedtls_ssl_tls13_write_early_data_ext(
Jerry Yuc59c5862023-12-05 10:40:49 +08002556 ssl, 0, p, end, &output_len);
Jerry Yu71c14f12022-12-12 11:10:35 +08002557 if (ret != 0) {
2558 return ret;
2559 }
2560 p += output_len;
2561 }
2562#endif /* MBEDTLS_SSL_EARLY_DATA */
2563
Waleed Elmelegy47d29462024-01-03 17:31:52 +00002564#if defined(MBEDTLS_SSL_RECORD_SIZE_LIMIT)
Waleed Elmelegyfbe42742024-01-05 18:11:10 +00002565 if (ssl->handshake->received_extensions & MBEDTLS_SSL_EXT_MASK(RECORD_SIZE_LIMIT)) {
Waleed Elmelegyd2fc90e2024-01-04 18:04:53 +00002566 ret = mbedtls_ssl_tls13_write_record_size_limit_ext(
2567 ssl, p, end, &output_len);
2568 if (ret != 0) {
2569 return ret;
2570 }
2571 p += output_len;
Waleed Elmelegy47d29462024-01-03 17:31:52 +00002572 }
Waleed Elmelegy47d29462024-01-03 17:31:52 +00002573#endif
2574
Gilles Peskine449bd832023-01-11 14:50:10 +01002575 extensions_len = (p - p_extensions_len) - 2;
2576 MBEDTLS_PUT_UINT16_BE(extensions_len, p_extensions_len, 0);
Jerry Yu8937eb42022-05-03 12:12:14 +08002577
2578 *out_len = p - buf;
Jerry Yu4d3841a2022-04-16 12:37:19 +08002579
Gilles Peskine449bd832023-01-11 14:50:10 +01002580 MBEDTLS_SSL_DEBUG_BUF(4, "encrypted extensions", buf, *out_len);
Jerry Yu4d3841a2022-04-16 12:37:19 +08002581
Jerry Yu7de2ff02022-11-08 21:43:46 +08002582 MBEDTLS_SSL_PRINT_EXTS(
Gilles Peskine449bd832023-01-11 14:50:10 +01002583 3, MBEDTLS_SSL_HS_ENCRYPTED_EXTENSIONS, ssl->handshake->sent_extensions);
Jerry Yu4b8f2f72022-10-31 13:31:22 +08002584
Gilles Peskine449bd832023-01-11 14:50:10 +01002585 return 0;
Jerry Yu4d3841a2022-04-16 12:37:19 +08002586}
2587
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002588MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002589static int ssl_tls13_write_encrypted_extensions(mbedtls_ssl_context *ssl)
Jerry Yu4d3841a2022-04-16 12:37:19 +08002590{
2591 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
2592 unsigned char *buf;
Jerry Yu39730a72022-05-03 12:14:04 +08002593 size_t buf_len, msg_len;
Jerry Yu4d3841a2022-04-16 12:37:19 +08002594
Gilles Peskine449bd832023-01-11 14:50:10 +01002595 mbedtls_ssl_set_outbound_transform(ssl,
2596 ssl->handshake->transform_handshake);
Gabor Mezei54719122022-06-28 11:34:56 +02002597 MBEDTLS_SSL_DEBUG_MSG(
Gilles Peskine449bd832023-01-11 14:50:10 +01002598 3, ("switching to handshake transform for outbound data"));
Gabor Mezei54719122022-06-28 11:34:56 +02002599
Gilles Peskine449bd832023-01-11 14:50:10 +01002600 MBEDTLS_SSL_DEBUG_MSG(2, ("=> write encrypted extensions"));
Jerry Yu4d3841a2022-04-16 12:37:19 +08002601
Xiaokang Qian9f1747b2023-03-30 02:50:04 +00002602 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_start_handshake_msg(
2603 ssl, MBEDTLS_SSL_HS_ENCRYPTED_EXTENSIONS,
2604 &buf, &buf_len));
Jerry Yu4d3841a2022-04-16 12:37:19 +08002605
Gilles Peskine449bd832023-01-11 14:50:10 +01002606 MBEDTLS_SSL_PROC_CHK(ssl_tls13_write_encrypted_extensions_body(
2607 ssl, buf, buf + buf_len, &msg_len));
Jerry Yu4d3841a2022-04-16 12:37:19 +08002608
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +01002609 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_add_hs_msg_to_checksum(
Xiaokang Qian9f1747b2023-03-30 02:50:04 +00002610 ssl, MBEDTLS_SSL_HS_ENCRYPTED_EXTENSIONS,
2611 buf, msg_len));
Jerry Yu4d3841a2022-04-16 12:37:19 +08002612
Gilles Peskine449bd832023-01-11 14:50:10 +01002613 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_finish_handshake_msg(
2614 ssl, buf_len, msg_len));
Jerry Yu4d3841a2022-04-16 12:37:19 +08002615
Ronald Cron928cbd32022-10-04 16:14:26 +02002616#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01002617 if (mbedtls_ssl_tls13_key_exchange_mode_with_psk(ssl)) {
2618 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_SERVER_FINISHED);
2619 } else {
2620 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_CERTIFICATE_REQUEST);
2621 }
Jerry Yu8937eb42022-05-03 12:12:14 +08002622#else
Gilles Peskine449bd832023-01-11 14:50:10 +01002623 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_SERVER_FINISHED);
Jerry Yu8937eb42022-05-03 12:12:14 +08002624#endif
2625
Jerry Yu4d3841a2022-04-16 12:37:19 +08002626cleanup:
2627
Gilles Peskine449bd832023-01-11 14:50:10 +01002628 MBEDTLS_SSL_DEBUG_MSG(2, ("<= write encrypted extensions"));
2629 return ret;
Jerry Yu4d3841a2022-04-16 12:37:19 +08002630}
2631
Ronald Cron928cbd32022-10-04 16:14:26 +02002632#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED)
XiaokangQiana987e1d2022-05-07 01:25:58 +00002633#define SSL_CERTIFICATE_REQUEST_SEND_REQUEST 0
2634#define SSL_CERTIFICATE_REQUEST_SKIP 1
2635/* Coordination:
2636 * Check whether a CertificateRequest message should be written.
2637 * Returns a negative code on failure, or
2638 * - SSL_CERTIFICATE_REQUEST_SEND_REQUEST
2639 * - SSL_CERTIFICATE_REQUEST_SKIP
2640 * indicating if the writing of the CertificateRequest
2641 * should be skipped or not.
2642 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002643MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002644static int ssl_tls13_certificate_request_coordinate(mbedtls_ssl_context *ssl)
XiaokangQiana987e1d2022-05-07 01:25:58 +00002645{
2646 int authmode;
2647
XiaokangQian40a35232022-05-07 09:02:40 +00002648#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Gilles Peskine449bd832023-01-11 14:50:10 +01002649 if (ssl->handshake->sni_authmode != MBEDTLS_SSL_VERIFY_UNSET) {
XiaokangQian40a35232022-05-07 09:02:40 +00002650 authmode = ssl->handshake->sni_authmode;
Gilles Peskine449bd832023-01-11 14:50:10 +01002651 } else
XiaokangQian40a35232022-05-07 09:02:40 +00002652#endif
XiaokangQiana987e1d2022-05-07 01:25:58 +00002653 authmode = ssl->conf->authmode;
2654
Gilles Peskine449bd832023-01-11 14:50:10 +01002655 if (authmode == MBEDTLS_SSL_VERIFY_NONE) {
Ronald Croneac00ad2022-09-13 10:16:31 +02002656 ssl->session_negotiate->verify_result = MBEDTLS_X509_BADCERT_SKIP_VERIFY;
Gilles Peskine449bd832023-01-11 14:50:10 +01002657 return SSL_CERTIFICATE_REQUEST_SKIP;
Ronald Croneac00ad2022-09-13 10:16:31 +02002658 }
XiaokangQiana987e1d2022-05-07 01:25:58 +00002659
XiaokangQianc3017f62022-05-13 05:55:41 +00002660 ssl->handshake->certificate_request_sent = 1;
XiaokangQian189ded22022-05-10 08:12:17 +00002661
Gilles Peskine449bd832023-01-11 14:50:10 +01002662 return SSL_CERTIFICATE_REQUEST_SEND_REQUEST;
XiaokangQiana987e1d2022-05-07 01:25:58 +00002663}
2664
XiaokangQiancec9ae62022-05-06 07:28:50 +00002665/*
2666 * struct {
2667 * opaque certificate_request_context<0..2^8-1>;
2668 * Extension extensions<2..2^16-1>;
2669 * } CertificateRequest;
2670 *
2671 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002672MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002673static int ssl_tls13_write_certificate_request_body(mbedtls_ssl_context *ssl,
2674 unsigned char *buf,
2675 const unsigned char *end,
2676 size_t *out_len)
XiaokangQiancec9ae62022-05-06 07:28:50 +00002677{
2678 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
2679 unsigned char *p = buf;
XiaokangQianec6efb92022-05-06 09:53:10 +00002680 size_t output_len = 0;
XiaokangQiancec9ae62022-05-06 07:28:50 +00002681 unsigned char *p_extensions_len;
2682
2683 *out_len = 0;
2684
2685 /* Check if we have enough space:
2686 * - certificate_request_context (1 byte)
2687 * - extensions length (2 bytes)
2688 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002689 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 3);
XiaokangQiancec9ae62022-05-06 07:28:50 +00002690
2691 /*
2692 * Write certificate_request_context
2693 */
2694 /*
2695 * We use a zero length context for the normal handshake
2696 * messages. For post-authentication handshake messages
2697 * this request context would be set to a non-zero value.
2698 */
2699 *p++ = 0x0;
2700
2701 /*
2702 * Write extensions
2703 */
2704 /* The extensions must contain the signature_algorithms. */
2705 p_extensions_len = p;
2706 p += 2;
Gilles Peskine449bd832023-01-11 14:50:10 +01002707 ret = mbedtls_ssl_write_sig_alg_ext(ssl, p, end, &output_len);
2708 if (ret != 0) {
2709 return ret;
2710 }
XiaokangQiancec9ae62022-05-06 07:28:50 +00002711
XiaokangQianec6efb92022-05-06 09:53:10 +00002712 p += output_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01002713 MBEDTLS_PUT_UINT16_BE(p - p_extensions_len - 2, p_extensions_len, 0);
XiaokangQiancec9ae62022-05-06 07:28:50 +00002714
2715 *out_len = p - buf;
2716
Jerry Yu7de2ff02022-11-08 21:43:46 +08002717 MBEDTLS_SSL_PRINT_EXTS(
Gilles Peskine449bd832023-01-11 14:50:10 +01002718 3, MBEDTLS_SSL_HS_CERTIFICATE_REQUEST, ssl->handshake->sent_extensions);
Jerry Yu4b8f2f72022-10-31 13:31:22 +08002719
Gilles Peskine449bd832023-01-11 14:50:10 +01002720 return 0;
XiaokangQiancec9ae62022-05-06 07:28:50 +00002721}
2722
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002723MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002724static int ssl_tls13_write_certificate_request(mbedtls_ssl_context *ssl)
XiaokangQiancec9ae62022-05-06 07:28:50 +00002725{
2726 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
2727
Gilles Peskine449bd832023-01-11 14:50:10 +01002728 MBEDTLS_SSL_DEBUG_MSG(2, ("=> write certificate request"));
XiaokangQiancec9ae62022-05-06 07:28:50 +00002729
Gilles Peskine449bd832023-01-11 14:50:10 +01002730 MBEDTLS_SSL_PROC_CHK_NEG(ssl_tls13_certificate_request_coordinate(ssl));
XiaokangQiancec9ae62022-05-06 07:28:50 +00002731
Gilles Peskine449bd832023-01-11 14:50:10 +01002732 if (ret == SSL_CERTIFICATE_REQUEST_SEND_REQUEST) {
XiaokangQiancec9ae62022-05-06 07:28:50 +00002733 unsigned char *buf;
2734 size_t buf_len, msg_len;
2735
Xiaokang Qian9f1747b2023-03-30 02:50:04 +00002736 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_start_handshake_msg(
2737 ssl, MBEDTLS_SSL_HS_CERTIFICATE_REQUEST,
2738 &buf, &buf_len));
XiaokangQiancec9ae62022-05-06 07:28:50 +00002739
Gilles Peskine449bd832023-01-11 14:50:10 +01002740 MBEDTLS_SSL_PROC_CHK(ssl_tls13_write_certificate_request_body(
2741 ssl, buf, buf + buf_len, &msg_len));
XiaokangQiancec9ae62022-05-06 07:28:50 +00002742
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +01002743 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_add_hs_msg_to_checksum(
Xiaokang Qian9f1747b2023-03-30 02:50:04 +00002744 ssl, MBEDTLS_SSL_HS_CERTIFICATE_REQUEST,
2745 buf, msg_len));
XiaokangQiancec9ae62022-05-06 07:28:50 +00002746
Gilles Peskine449bd832023-01-11 14:50:10 +01002747 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_finish_handshake_msg(
2748 ssl, buf_len, msg_len));
2749 } else if (ret == SSL_CERTIFICATE_REQUEST_SKIP) {
2750 MBEDTLS_SSL_DEBUG_MSG(2, ("<= skip write certificate request"));
XiaokangQiancec9ae62022-05-06 07:28:50 +00002751 ret = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01002752 } else {
2753 MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
XiaokangQiancec9ae62022-05-06 07:28:50 +00002754 ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
2755 goto cleanup;
2756 }
2757
Gilles Peskine449bd832023-01-11 14:50:10 +01002758 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_SERVER_CERTIFICATE);
XiaokangQiancec9ae62022-05-06 07:28:50 +00002759cleanup:
2760
Gilles Peskine449bd832023-01-11 14:50:10 +01002761 MBEDTLS_SSL_DEBUG_MSG(2, ("<= write certificate request"));
2762 return ret;
XiaokangQiancec9ae62022-05-06 07:28:50 +00002763}
XiaokangQiancec9ae62022-05-06 07:28:50 +00002764
Jerry Yu4d3841a2022-04-16 12:37:19 +08002765/*
Jerry Yuc6e6dbf2022-04-16 19:42:57 +08002766 * Handler for MBEDTLS_SSL_SERVER_CERTIFICATE
Jerry Yu83da34e2022-04-16 13:59:52 +08002767 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002768MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002769static int ssl_tls13_write_server_certificate(mbedtls_ssl_context *ssl)
Jerry Yu83da34e2022-04-16 13:59:52 +08002770{
Jerry Yu5a26f302022-05-10 20:46:40 +08002771 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
XiaokangQianfb665a82022-06-15 03:57:21 +00002772
2773#if defined(MBEDTLS_X509_CRT_PARSE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01002774 if ((ssl_tls13_pick_key_cert(ssl) != 0) ||
2775 mbedtls_ssl_own_cert(ssl) == NULL) {
2776 MBEDTLS_SSL_DEBUG_MSG(2, ("No certificate available."));
2777 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE,
2778 MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE);
2779 return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
Jerry Yu5a26f302022-05-10 20:46:40 +08002780 }
XiaokangQianfb665a82022-06-15 03:57:21 +00002781#endif /* MBEDTLS_X509_CRT_PARSE_C */
Jerry Yu5a26f302022-05-10 20:46:40 +08002782
Gilles Peskine449bd832023-01-11 14:50:10 +01002783 ret = mbedtls_ssl_tls13_write_certificate(ssl);
2784 if (ret != 0) {
2785 return ret;
2786 }
2787 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_CERTIFICATE_VERIFY);
2788 return 0;
Jerry Yu83da34e2022-04-16 13:59:52 +08002789}
2790
2791/*
Jerry Yuc6e6dbf2022-04-16 19:42:57 +08002792 * Handler for MBEDTLS_SSL_CERTIFICATE_VERIFY
Jerry Yu83da34e2022-04-16 13:59:52 +08002793 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002794MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002795static int ssl_tls13_write_certificate_verify(mbedtls_ssl_context *ssl)
Jerry Yu83da34e2022-04-16 13:59:52 +08002796{
Gilles Peskine449bd832023-01-11 14:50:10 +01002797 int ret = mbedtls_ssl_tls13_write_certificate_verify(ssl);
2798 if (ret != 0) {
2799 return ret;
2800 }
2801 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_SERVER_FINISHED);
2802 return 0;
Jerry Yu83da34e2022-04-16 13:59:52 +08002803}
Ronald Cron928cbd32022-10-04 16:14:26 +02002804#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED */
Jerry Yu83da34e2022-04-16 13:59:52 +08002805
Jerry Yu9b72e392023-12-01 16:27:08 +08002806/*
2807 * RFC 8446 section A.2
2808 *
2809 * | Send ServerHello
2810 * | K_send = handshake
2811 * | Send EncryptedExtensions
2812 * | [Send CertificateRequest]
2813 * Can send | [Send Certificate + CertificateVerify]
2814 * app data | Send Finished
2815 * after --> | K_send = application
2816 * here +--------+--------+
2817 * No 0-RTT | | 0-RTT
2818 * | |
2819 * K_recv = handshake | | K_recv = early data
2820 * [Skip decrypt errors] | +------> WAIT_EOED -+
2821 * | | Recv | | Recv EndOfEarlyData
2822 * | | early data | | K_recv = handshake
2823 * | +------------+ |
2824 * | |
2825 * +> WAIT_FLIGHT2 <--------+
2826 * |
2827 * +--------+--------+
2828 * No auth | | Client auth
2829 * | |
2830 * | v
2831 * | WAIT_CERT
2832 * | Recv | | Recv Certificate
2833 * | empty | v
2834 * | Certificate | WAIT_CV
2835 * | | | Recv
2836 * | v | CertificateVerify
2837 * +-> WAIT_FINISHED <---+
2838 * | Recv Finished
2839 *
2840 *
2841 * The following function handles the state changes after WAIT_FLIGHT2 in the
Jerry Yu3be85072023-12-04 09:58:54 +08002842 * above diagram. We are not going to receive early data related messages
2843 * anymore, prepare to receive the first handshake message of the client
2844 * second flight.
Jerry Yu9b72e392023-12-01 16:27:08 +08002845 */
Jerry Yu3be85072023-12-04 09:58:54 +08002846static void ssl_tls13_prepare_for_handshake_second_flight(
2847 mbedtls_ssl_context *ssl)
Jerry Yu9b72e392023-12-01 16:27:08 +08002848{
Jerry Yu9b72e392023-12-01 16:27:08 +08002849 if (ssl->handshake->certificate_request_sent) {
2850 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_CLIENT_CERTIFICATE);
2851 } else {
Jerry Yu42020fb2023-12-05 17:35:53 +08002852 MBEDTLS_SSL_DEBUG_MSG(2, ("skip parse certificate"));
Jerry Yuebb1b1d2023-12-05 11:02:15 +08002853 MBEDTLS_SSL_DEBUG_MSG(2, ("skip parse certificate verify"));
Jerry Yuebb1b1d2023-12-05 11:02:15 +08002854
Jerry Yu9b72e392023-12-01 16:27:08 +08002855 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_CLIENT_FINISHED);
2856 }
Jerry Yu9b72e392023-12-01 16:27:08 +08002857}
2858
Jerry Yu83da34e2022-04-16 13:59:52 +08002859/*
Jerry Yud6e253d2022-05-18 13:59:24 +08002860 * Handler for MBEDTLS_SSL_SERVER_FINISHED
Jerry Yu69dd8d42022-04-16 12:51:26 +08002861 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002862MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002863static int ssl_tls13_write_server_finished(mbedtls_ssl_context *ssl)
Jerry Yu69dd8d42022-04-16 12:51:26 +08002864{
Jerry Yud6e253d2022-05-18 13:59:24 +08002865 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yu27bdc7c2022-04-16 13:33:27 +08002866
Gilles Peskine449bd832023-01-11 14:50:10 +01002867 ret = mbedtls_ssl_tls13_write_finished_message(ssl);
2868 if (ret != 0) {
2869 return ret;
2870 }
Jerry Yu27bdc7c2022-04-16 13:33:27 +08002871
Gilles Peskine449bd832023-01-11 14:50:10 +01002872 ret = mbedtls_ssl_tls13_compute_application_transform(ssl);
2873 if (ret != 0) {
Jerry Yue3d67cb2022-05-19 15:33:10 +08002874 MBEDTLS_SSL_PEND_FATAL_ALERT(
Gilles Peskine449bd832023-01-11 14:50:10 +01002875 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE,
2876 MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE);
2877 return ret;
Jerry Yue3d67cb2022-05-19 15:33:10 +08002878 }
XiaokangQianc3017f62022-05-13 05:55:41 +00002879
Jerry Yu87b5ed42022-12-12 13:07:07 +08002880#if defined(MBEDTLS_SSL_EARLY_DATA)
Ronald Cron78a38f62024-02-01 18:30:31 +01002881 if (ssl->handshake->early_data_accepted) {
Jerry Yu0af63dc2023-12-01 17:14:51 +08002882 /* See RFC 8446 section A.2 for more information */
Jerry Yu87b5ed42022-12-12 13:07:07 +08002883 MBEDTLS_SSL_DEBUG_MSG(
2884 1, ("Switch to early keys for inbound traffic. "
2885 "( K_recv = early data )"));
2886 mbedtls_ssl_set_inbound_transform(
2887 ssl, ssl->handshake->transform_earlydata);
2888 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_END_OF_EARLY_DATA);
2889 return 0;
2890 }
2891#endif /* MBEDTLS_SSL_EARLY_DATA */
Jerry Yu0af63dc2023-12-01 17:14:51 +08002892 MBEDTLS_SSL_DEBUG_MSG(
2893 1, ("Switch to handshake keys for inbound traffic "
2894 "( K_recv = handshake )"));
Gilles Peskine449bd832023-01-11 14:50:10 +01002895 mbedtls_ssl_set_inbound_transform(ssl, ssl->handshake->transform_handshake);
XiaokangQian189ded22022-05-10 08:12:17 +00002896
Jerry Yu3be85072023-12-04 09:58:54 +08002897 ssl_tls13_prepare_for_handshake_second_flight(ssl);
Jerry Yu7d8c3fe2022-12-12 12:59:44 +08002898
2899 return 0;
2900}
2901
Jerry Yu87b5ed42022-12-12 13:07:07 +08002902#if defined(MBEDTLS_SSL_EARLY_DATA)
2903/*
Jerry Yu59d420f2023-12-01 16:30:34 +08002904 * Handler for MBEDTLS_SSL_END_OF_EARLY_DATA
Jerry Yu87b5ed42022-12-12 13:07:07 +08002905 */
Jerry Yu3be85072023-12-04 09:58:54 +08002906#define SSL_GOT_END_OF_EARLY_DATA 0
Jerry Yub55f9eb2023-12-05 10:27:17 +08002907#define SSL_GOT_EARLY_DATA 1
Jerry Yud5c34962023-12-01 16:32:31 +08002908/* Coordination:
Jerry Yu3be85072023-12-04 09:58:54 +08002909 * Deals with the ambiguity of not knowing if the next message is an
2910 * EndOfEarlyData message or an application message containing early data.
Jerry Yud5c34962023-12-01 16:32:31 +08002911 * Returns a negative code on failure, or
Jerry Yu3be85072023-12-04 09:58:54 +08002912 * - SSL_GOT_END_OF_EARLY_DATA
Jerry Yub55f9eb2023-12-05 10:27:17 +08002913 * - SSL_GOT_EARLY_DATA
Jerry Yud5c34962023-12-01 16:32:31 +08002914 * indicating which message is received.
2915 */
2916MBEDTLS_CHECK_RETURN_CRITICAL
2917static int ssl_tls13_end_of_early_data_coordinate(mbedtls_ssl_context *ssl)
2918{
2919 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yub4ed4602023-12-01 16:34:00 +08002920
2921 if ((ret = mbedtls_ssl_read_record(ssl, 0)) != 0) {
2922 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_read_record", ret);
2923 return ret;
2924 }
2925 ssl->keep_current_message = 1;
2926
2927 if (ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
2928 ssl->in_msg[0] == MBEDTLS_SSL_HS_END_OF_EARLY_DATA) {
Jerry Yub55f9eb2023-12-05 10:27:17 +08002929 MBEDTLS_SSL_DEBUG_MSG(3, ("Received an end_of_early_data message."));
Jerry Yu3be85072023-12-04 09:58:54 +08002930 return SSL_GOT_END_OF_EARLY_DATA;
Jerry Yub4ed4602023-12-01 16:34:00 +08002931 }
2932
2933 if (ssl->in_msgtype == MBEDTLS_SSL_MSG_APPLICATION_DATA) {
Ronald Croned7d4bf2024-01-31 07:55:19 +01002934 if (ssl->in_offt == NULL) {
Ronald Cron85718042024-02-22 10:22:09 +01002935 MBEDTLS_SSL_DEBUG_MSG(3, ("Received early data"));
Ronald Croned7d4bf2024-01-31 07:55:19 +01002936 /* Set the reading pointer */
2937 ssl->in_offt = ssl->in_msg;
Ronald Cron85718042024-02-22 10:22:09 +01002938 ret = mbedtls_ssl_tls13_check_early_data_len(ssl, ssl->in_msglen);
2939 if (ret != 0) {
2940 return ret;
2941 }
Ronald Croned7d4bf2024-01-31 07:55:19 +01002942 }
Jerry Yub55f9eb2023-12-05 10:27:17 +08002943 return SSL_GOT_EARLY_DATA;
Jerry Yub4ed4602023-12-01 16:34:00 +08002944 }
2945
Jerry Yu7bb40a32023-12-04 10:04:15 +08002946 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE,
2947 MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE);
Jerry Yub4ed4602023-12-01 16:34:00 +08002948 return MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE;
Jerry Yud5c34962023-12-01 16:32:31 +08002949}
2950
2951MBEDTLS_CHECK_RETURN_CRITICAL
2952static int ssl_tls13_parse_end_of_early_data(mbedtls_ssl_context *ssl,
2953 const unsigned char *buf,
2954 const unsigned char *end)
2955{
Jerry Yu75c9ab72023-12-01 16:41:40 +08002956 /* RFC 8446 section 4.5
2957 *
2958 * struct {} EndOfEarlyData;
2959 */
Jerry Yufbf03992023-12-04 10:00:37 +08002960 if (buf != end) {
2961 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR,
2962 MBEDTLS_ERR_SSL_DECODE_ERROR);
2963 return MBEDTLS_ERR_SSL_DECODE_ERROR;
2964 }
2965 return 0;
Jerry Yud5c34962023-12-01 16:32:31 +08002966}
2967
Jerry Yud5c34962023-12-01 16:32:31 +08002968/*
2969 * RFC 8446 section A.2
2970 *
2971 * | Send ServerHello
2972 * | K_send = handshake
2973 * | Send EncryptedExtensions
2974 * | [Send CertificateRequest]
2975 * Can send | [Send Certificate + CertificateVerify]
2976 * app data | Send Finished
2977 * after --> | K_send = application
2978 * here +--------+--------+
2979 * No 0-RTT | | 0-RTT
2980 * | |
2981 * K_recv = handshake | | K_recv = early data
2982 * [Skip decrypt errors] | +------> WAIT_EOED -+
2983 * | | Recv | | Recv EndOfEarlyData
2984 * | | early data | | K_recv = handshake
2985 * | +------------+ |
2986 * | |
2987 * +> WAIT_FLIGHT2 <--------+
2988 * |
2989 * +--------+--------+
2990 * No auth | | Client auth
2991 * | |
2992 * | v
2993 * | WAIT_CERT
2994 * | Recv | | Recv Certificate
2995 * | empty | v
2996 * | Certificate | WAIT_CV
2997 * | | | Recv
2998 * | v | CertificateVerify
2999 * +-> WAIT_FINISHED <---+
3000 * | Recv Finished
3001 *
3002 * The function handles actions and state changes from 0-RTT to WAIT_FLIGHT2 in
3003 * the above diagram.
3004 */
Jerry Yu87b5ed42022-12-12 13:07:07 +08003005MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yu59d420f2023-12-01 16:30:34 +08003006static int ssl_tls13_process_end_of_early_data(mbedtls_ssl_context *ssl)
Jerry Yu87b5ed42022-12-12 13:07:07 +08003007{
3008 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yud5c34962023-12-01 16:32:31 +08003009
3010 MBEDTLS_SSL_DEBUG_MSG(2, ("=> ssl_tls13_process_end_of_early_data"));
3011
3012 MBEDTLS_SSL_PROC_CHK_NEG(ssl_tls13_end_of_early_data_coordinate(ssl));
3013
Jerry Yu3be85072023-12-04 09:58:54 +08003014 if (ret == SSL_GOT_END_OF_EARLY_DATA) {
Jerry Yud5c34962023-12-01 16:32:31 +08003015 unsigned char *buf;
3016 size_t buf_len;
3017
3018 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_tls13_fetch_handshake_msg(
3019 ssl, MBEDTLS_SSL_HS_END_OF_EARLY_DATA,
3020 &buf, &buf_len));
3021
3022 MBEDTLS_SSL_PROC_CHK(ssl_tls13_parse_end_of_early_data(
3023 ssl, buf, buf + buf_len));
3024
Jerry Yue9655122023-12-01 16:44:40 +08003025 MBEDTLS_SSL_DEBUG_MSG(
3026 1, ("Switch to handshake keys for inbound traffic"
3027 "( K_recv = handshake )"));
3028 mbedtls_ssl_set_inbound_transform(
3029 ssl, ssl->handshake->transform_handshake);
3030
Jerry Yud5c34962023-12-01 16:32:31 +08003031 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_add_hs_msg_to_checksum(
3032 ssl, MBEDTLS_SSL_HS_END_OF_EARLY_DATA,
3033 buf, buf_len));
Jerry Yue9655122023-12-01 16:44:40 +08003034
Jerry Yu3be85072023-12-04 09:58:54 +08003035 ssl_tls13_prepare_for_handshake_second_flight(ssl);
Jerry Yud5c34962023-12-01 16:32:31 +08003036
Jerry Yub55f9eb2023-12-05 10:27:17 +08003037 } else if (ret == SSL_GOT_EARLY_DATA) {
Jerry Yud9ca3542023-12-06 17:23:52 +08003038 ret = MBEDTLS_ERR_SSL_RECEIVED_EARLY_DATA;
3039 goto cleanup;
Jerry Yud5c34962023-12-01 16:32:31 +08003040 } else {
3041 MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
3042 ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
3043 goto cleanup;
3044 }
3045
Jerry Yud5c34962023-12-01 16:32:31 +08003046cleanup:
3047 MBEDTLS_SSL_DEBUG_MSG(2, ("<= ssl_tls13_process_end_of_early_data"));
Jerry Yu87b5ed42022-12-12 13:07:07 +08003048 return ret;
3049}
3050#endif /* MBEDTLS_SSL_EARLY_DATA */
3051
Jerry Yu69dd8d42022-04-16 12:51:26 +08003052/*
Jerry Yud6e253d2022-05-18 13:59:24 +08003053 * Handler for MBEDTLS_SSL_CLIENT_FINISHED
Jerry Yu69dd8d42022-04-16 12:51:26 +08003054 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02003055MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01003056static int ssl_tls13_process_client_finished(mbedtls_ssl_context *ssl)
Jerry Yu69dd8d42022-04-16 12:51:26 +08003057{
Jerry Yud6e253d2022-05-18 13:59:24 +08003058 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
3059
Gilles Peskine449bd832023-01-11 14:50:10 +01003060 ret = mbedtls_ssl_tls13_process_finished_message(ssl);
3061 if (ret != 0) {
3062 return ret;
Jerry Yue3d67cb2022-05-19 15:33:10 +08003063 }
3064
Gilles Peskine449bd832023-01-11 14:50:10 +01003065 ret = mbedtls_ssl_tls13_compute_resumption_master_secret(ssl);
3066 if (ret != 0) {
Xiaokang Qian9f1747b2023-03-30 02:50:04 +00003067 MBEDTLS_SSL_DEBUG_RET(
3068 1, "mbedtls_ssl_tls13_compute_resumption_master_secret", ret);
Gilles Peskine449bd832023-01-11 14:50:10 +01003069 }
3070
3071 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_HANDSHAKE_WRAPUP);
3072 return 0;
Jerry Yu69dd8d42022-04-16 12:51:26 +08003073}
3074
3075/*
Jerry Yud6e253d2022-05-18 13:59:24 +08003076 * Handler for MBEDTLS_SSL_HANDSHAKE_WRAPUP
Jerry Yu69dd8d42022-04-16 12:51:26 +08003077 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02003078MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01003079static int ssl_tls13_handshake_wrapup(mbedtls_ssl_context *ssl)
Jerry Yu69dd8d42022-04-16 12:51:26 +08003080{
Gilles Peskine449bd832023-01-11 14:50:10 +01003081 MBEDTLS_SSL_DEBUG_MSG(2, ("handshake: done"));
Jerry Yu03ed50b2022-04-16 17:13:30 +08003082
Gilles Peskine449bd832023-01-11 14:50:10 +01003083 mbedtls_ssl_tls13_handshake_wrapup(ssl);
Jerry Yue67bef42022-07-07 07:29:42 +00003084
Pengyu Lv3643fdb2023-01-12 16:46:28 +08003085#if defined(MBEDTLS_SSL_SESSION_TICKETS) && \
3086 defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED)
Pengyu Lva1aa31b2022-12-13 13:49:59 +08003087/* TODO: Remove the check of SOME_PSK_ENABLED since SESSION_TICKETS requires
3088 * SOME_PSK_ENABLED to be enabled. Here is just to make CI happy. It is
3089 * expected to be resolved with issue#6395.
3090 */
Pengyu Lvc7af2c42022-12-01 16:33:00 +08003091 /* Sent NewSessionTicket message only when client supports PSK */
Pengyu Lvb2cfafb2023-11-14 13:56:13 +08003092 if (mbedtls_ssl_tls13_is_some_psk_supported(ssl)) {
Xiaokang Qian9f1747b2023-03-30 02:50:04 +00003093 mbedtls_ssl_handshake_set_state(
3094 ssl, MBEDTLS_SSL_TLS1_3_NEW_SESSION_TICKET);
Pengyu Lvc7af2c42022-12-01 16:33:00 +08003095 } else
Jerry Yufca4d572022-07-21 10:37:48 +08003096#endif
Pengyu Lv3643fdb2023-01-12 16:46:28 +08003097 {
3098 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_HANDSHAKE_OVER);
3099 }
Gilles Peskine449bd832023-01-11 14:50:10 +01003100 return 0;
Jerry Yu69dd8d42022-04-16 12:51:26 +08003101}
3102
Norbert Fabritius96eed722023-01-23 15:24:59 +01003103#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Jerry Yu69dd8d42022-04-16 12:51:26 +08003104/*
Jerry Yua8d3c502022-10-30 14:51:23 +08003105 * Handler for MBEDTLS_SSL_TLS1_3_NEW_SESSION_TICKET
Jerry Yue67bef42022-07-07 07:29:42 +00003106 */
3107#define SSL_NEW_SESSION_TICKET_SKIP 0
3108#define SSL_NEW_SESSION_TICKET_WRITE 1
3109MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01003110static int ssl_tls13_write_new_session_ticket_coordinate(mbedtls_ssl_context *ssl)
Jerry Yue67bef42022-07-07 07:29:42 +00003111{
3112 /* Check whether the use of session tickets is enabled */
Gilles Peskine449bd832023-01-11 14:50:10 +01003113 if (ssl->conf->f_ticket_write == NULL) {
3114 MBEDTLS_SSL_DEBUG_MSG(2, ("NewSessionTicket: disabled,"
3115 " callback is not set"));
3116 return SSL_NEW_SESSION_TICKET_SKIP;
Jerry Yud0766ec2022-09-22 10:46:57 +08003117 }
Gilles Peskine449bd832023-01-11 14:50:10 +01003118 if (ssl->conf->new_session_tickets_count == 0) {
3119 MBEDTLS_SSL_DEBUG_MSG(2, ("NewSessionTicket: disabled,"
3120 " configured count is zero"));
3121 return SSL_NEW_SESSION_TICKET_SKIP;
Jerry Yud0766ec2022-09-22 10:46:57 +08003122 }
3123
Gilles Peskine449bd832023-01-11 14:50:10 +01003124 if (ssl->handshake->new_session_tickets_count == 0) {
3125 MBEDTLS_SSL_DEBUG_MSG(2, ("NewSessionTicket: all tickets have "
3126 "been sent."));
3127 return SSL_NEW_SESSION_TICKET_SKIP;
Jerry Yue67bef42022-07-07 07:29:42 +00003128 }
3129
Gilles Peskine449bd832023-01-11 14:50:10 +01003130 return SSL_NEW_SESSION_TICKET_WRITE;
Jerry Yue67bef42022-07-07 07:29:42 +00003131}
3132
Jerry Yue67bef42022-07-07 07:29:42 +00003133MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01003134static int ssl_tls13_prepare_new_session_ticket(mbedtls_ssl_context *ssl,
3135 unsigned char *ticket_nonce,
3136 size_t ticket_nonce_size)
Jerry Yue67bef42022-07-07 07:29:42 +00003137{
3138 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
3139 mbedtls_ssl_session *session = ssl->session;
3140 mbedtls_ssl_ciphersuite_t *ciphersuite_info;
3141 psa_algorithm_t psa_hash_alg;
3142 int hash_length;
3143
Gilles Peskine449bd832023-01-11 14:50:10 +01003144 MBEDTLS_SSL_DEBUG_MSG(2, ("=> prepare NewSessionTicket msg"));
Jerry Yue67bef42022-07-07 07:29:42 +00003145
Pengyu Lv9f926952022-11-17 15:22:33 +08003146 /* Set ticket_flags depends on the advertised psk key exchange mode */
Pengyu Lv94a42cc2023-12-06 10:04:17 +08003147 mbedtls_ssl_tls13_session_clear_ticket_flags(
Pengyu Lv9eacb442022-12-09 14:39:19 +08003148 session, MBEDTLS_SSL_TLS1_3_TICKET_FLAGS_MASK);
Pengyu Lve6487fe2022-12-06 09:30:29 +08003149#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED)
Pengyu Lv94a42cc2023-12-06 10:04:17 +08003150 mbedtls_ssl_tls13_session_set_ticket_flags(
Pengyu Lv9eacb442022-12-09 14:39:19 +08003151 session, ssl->handshake->tls13_kex_modes);
Pengyu Lve6487fe2022-12-06 09:30:29 +08003152#endif
Jerry Yu95648b02023-12-06 15:03:34 +08003153
3154#if defined(MBEDTLS_SSL_EARLY_DATA)
3155 if (ssl->conf->early_data_enabled == MBEDTLS_SSL_EARLY_DATA_ENABLED &&
3156 ssl->conf->max_early_data_size > 0) {
Pengyu Lv94a42cc2023-12-06 10:04:17 +08003157 mbedtls_ssl_tls13_session_set_ticket_flags(
Jerry Yu95648b02023-12-06 15:03:34 +08003158 session, MBEDTLS_SSL_TLS1_3_TICKET_ALLOW_EARLY_DATA);
Ronald Cronc2865192024-02-07 14:01:03 +01003159 session->max_early_data_size = ssl->conf->max_early_data_size;
Jerry Yu95648b02023-12-06 15:03:34 +08003160 }
3161#endif /* MBEDTLS_SSL_EARLY_DATA */
3162
Pengyu Lv4938a562023-01-16 11:28:49 +08003163 MBEDTLS_SSL_PRINT_TICKET_FLAGS(4, session->ticket_flags);
Pengyu Lv9f926952022-11-17 15:22:33 +08003164
Waleed Elmelegy2824a202024-02-23 17:51:47 +00003165#if defined(MBEDTLS_SSL_EARLY_DATA) && defined(MBEDTLS_SSL_ALPN)
Waleed Elmelegy5bc52632024-03-12 16:25:08 +00003166 if (session->ticket_alpn == NULL) {
3167 ret = mbedtls_ssl_session_set_ticket_alpn(session, ssl->alpn_chosen);
3168 if (ret != 0) {
3169 return ret;
3170 }
Waleed Elmelegy2824a202024-02-23 17:51:47 +00003171 }
3172#endif
3173
Jerry Yue67bef42022-07-07 07:29:42 +00003174 /* Generate ticket_age_add */
Gilles Peskine449bd832023-01-11 14:50:10 +01003175 if ((ret = ssl->conf->f_rng(ssl->conf->p_rng,
3176 (unsigned char *) &session->ticket_age_add,
3177 sizeof(session->ticket_age_add)) != 0)) {
3178 MBEDTLS_SSL_DEBUG_RET(1, "generate_ticket_age_add", ret);
3179 return ret;
Jerry Yue67bef42022-07-07 07:29:42 +00003180 }
Gilles Peskine449bd832023-01-11 14:50:10 +01003181 MBEDTLS_SSL_DEBUG_MSG(3, ("ticket_age_add: %u",
3182 (unsigned int) session->ticket_age_add));
Jerry Yue67bef42022-07-07 07:29:42 +00003183
3184 /* Generate ticket_nonce */
Gilles Peskine449bd832023-01-11 14:50:10 +01003185 ret = ssl->conf->f_rng(ssl->conf->p_rng, ticket_nonce, ticket_nonce_size);
3186 if (ret != 0) {
3187 MBEDTLS_SSL_DEBUG_RET(1, "generate_ticket_nonce", ret);
3188 return ret;
Jerry Yue67bef42022-07-07 07:29:42 +00003189 }
Gilles Peskine449bd832023-01-11 14:50:10 +01003190 MBEDTLS_SSL_DEBUG_BUF(3, "ticket_nonce:",
3191 ticket_nonce, ticket_nonce_size);
Jerry Yue67bef42022-07-07 07:29:42 +00003192
3193 ciphersuite_info =
Gilles Peskine449bd832023-01-11 14:50:10 +01003194 (mbedtls_ssl_ciphersuite_t *) ssl->handshake->ciphersuite_info;
Dave Rodgman2eab4622023-10-05 13:30:37 +01003195 psa_hash_alg = mbedtls_md_psa_alg_from_type((mbedtls_md_type_t) ciphersuite_info->mac);
Gilles Peskine449bd832023-01-11 14:50:10 +01003196 hash_length = PSA_HASH_LENGTH(psa_hash_alg);
3197 if (hash_length == -1 ||
3198 (size_t) hash_length > sizeof(session->resumption_key)) {
3199 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Jerry Yufca4d572022-07-21 10:37:48 +08003200 }
3201
Jerry Yue67bef42022-07-07 07:29:42 +00003202 /* In this code the psk key length equals the length of the hash */
3203 session->resumption_key_len = hash_length;
3204 session->ciphersuite = ciphersuite_info->id;
3205
3206 /* Compute resumption key
3207 *
3208 * HKDF-Expand-Label( resumption_master_secret,
3209 * "resumption", ticket_nonce, Hash.length )
3210 */
3211 ret = mbedtls_ssl_tls13_hkdf_expand_label(
Gilles Peskine449bd832023-01-11 14:50:10 +01003212 psa_hash_alg,
3213 session->app_secrets.resumption_master_secret,
3214 hash_length,
3215 MBEDTLS_SSL_TLS1_3_LBL_WITH_LEN(resumption),
3216 ticket_nonce,
3217 ticket_nonce_size,
3218 session->resumption_key,
3219 hash_length);
Jerry Yue67bef42022-07-07 07:29:42 +00003220
Gilles Peskine449bd832023-01-11 14:50:10 +01003221 if (ret != 0) {
3222 MBEDTLS_SSL_DEBUG_RET(2,
3223 "Creating the ticket-resumed PSK failed",
3224 ret);
3225 return ret;
Jerry Yue67bef42022-07-07 07:29:42 +00003226 }
Gilles Peskine449bd832023-01-11 14:50:10 +01003227 MBEDTLS_SSL_DEBUG_BUF(3, "Ticket-resumed PSK",
3228 session->resumption_key,
3229 session->resumption_key_len);
Jerry Yue67bef42022-07-07 07:29:42 +00003230
Gilles Peskine449bd832023-01-11 14:50:10 +01003231 MBEDTLS_SSL_DEBUG_BUF(3, "resumption_master_secret",
3232 session->app_secrets.resumption_master_secret,
3233 hash_length);
Jerry Yue67bef42022-07-07 07:29:42 +00003234
Gilles Peskine449bd832023-01-11 14:50:10 +01003235 return 0;
Jerry Yue67bef42022-07-07 07:29:42 +00003236}
3237
3238/* This function creates a NewSessionTicket message in the following format:
3239 *
3240 * struct {
3241 * uint32 ticket_lifetime;
3242 * uint32 ticket_age_add;
3243 * opaque ticket_nonce<0..255>;
3244 * opaque ticket<1..2^16-1>;
3245 * Extension extensions<0..2^16-2>;
3246 * } NewSessionTicket;
3247 *
3248 * The ticket inside the NewSessionTicket message is an encrypted container
3249 * carrying the necessary information so that the server is later able to
3250 * re-start the communication.
3251 *
3252 * The following fields are placed inside the ticket by the
3253 * f_ticket_write() function:
3254 *
Jerry Yu0069abc2023-11-23 21:07:28 +08003255 * - creation time (ticket_creation_time)
3256 * - flags (ticket_flags)
Jerry Yue67bef42022-07-07 07:29:42 +00003257 * - age add (ticket_age_add)
Jerry Yu0069abc2023-11-23 21:07:28 +08003258 * - key (resumption_key)
3259 * - key length (resumption_key_len)
Jerry Yue67bef42022-07-07 07:29:42 +00003260 * - ciphersuite (ciphersuite)
Jerry Yu0069abc2023-11-23 21:07:28 +08003261 * - max_early_data_size (max_early_data_size)
Jerry Yue67bef42022-07-07 07:29:42 +00003262 */
3263MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01003264static int ssl_tls13_write_new_session_ticket_body(mbedtls_ssl_context *ssl,
3265 unsigned char *buf,
3266 unsigned char *end,
3267 size_t *out_len,
3268 unsigned char *ticket_nonce,
3269 size_t ticket_nonce_size)
Jerry Yue67bef42022-07-07 07:29:42 +00003270{
3271 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
3272 unsigned char *p = buf;
3273 mbedtls_ssl_session *session = ssl->session;
3274 size_t ticket_len;
3275 uint32_t ticket_lifetime;
Jerry Yu01da35e2022-12-12 15:09:22 +08003276 unsigned char *p_extensions_len;
Jerry Yue67bef42022-07-07 07:29:42 +00003277
3278 *out_len = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01003279 MBEDTLS_SSL_DEBUG_MSG(2, ("=> write NewSessionTicket msg"));
Jerry Yue67bef42022-07-07 07:29:42 +00003280
3281 /*
3282 * ticket_lifetime 4 bytes
3283 * ticket_age_add 4 bytes
3284 * ticket_nonce 1 + ticket_nonce_size bytes
3285 * ticket >=2 bytes
3286 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003287 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 4 + 4 + 1 + ticket_nonce_size + 2);
Jerry Yue67bef42022-07-07 07:29:42 +00003288
3289 /* Generate ticket and ticket_lifetime */
Ronald Cron3c0072b2023-11-22 10:00:14 +01003290#if defined(MBEDTLS_HAVE_TIME)
3291 session->ticket_creation_time = mbedtls_ms_time();
3292#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01003293 ret = ssl->conf->f_ticket_write(ssl->conf->p_ticket,
3294 session,
3295 p + 9 + ticket_nonce_size + 2,
3296 end,
3297 &ticket_len,
3298 &ticket_lifetime);
3299 if (ret != 0) {
3300 MBEDTLS_SSL_DEBUG_RET(1, "write_ticket", ret);
3301 return ret;
Jerry Yue67bef42022-07-07 07:29:42 +00003302 }
Jerry Yuce794882023-11-22 15:01:18 +08003303
3304 /* RFC 8446 section 4.6.1
3305 *
Jerry Yue67bef42022-07-07 07:29:42 +00003306 * ticket_lifetime: Indicates the lifetime in seconds as a 32-bit
Jerry Yuce794882023-11-22 15:01:18 +08003307 * unsigned integer in network byte order from the time of ticket
3308 * issuance. Servers MUST NOT use any value greater than
3309 * 604800 seconds (7 days) ...
Jerry Yue67bef42022-07-07 07:29:42 +00003310 */
Jerry Yu8e0174a2023-11-10 13:58:16 +08003311 if (ticket_lifetime > MBEDTLS_SSL_TLS1_3_MAX_ALLOWED_TICKET_LIFETIME) {
Jerry Yuce794882023-11-22 15:01:18 +08003312 MBEDTLS_SSL_DEBUG_MSG(
3313 1, ("Ticket lifetime (%u) is greater than 7 days.",
3314 (unsigned int) ticket_lifetime));
3315 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Gilles Peskine449bd832023-01-11 14:50:10 +01003316 }
Jerry Yuce794882023-11-22 15:01:18 +08003317
Gilles Peskine449bd832023-01-11 14:50:10 +01003318 MBEDTLS_PUT_UINT32_BE(ticket_lifetime, p, 0);
3319 MBEDTLS_SSL_DEBUG_MSG(3, ("ticket_lifetime: %u",
3320 (unsigned int) ticket_lifetime));
Jerry Yue67bef42022-07-07 07:29:42 +00003321
3322 /* Write ticket_age_add */
Gilles Peskine449bd832023-01-11 14:50:10 +01003323 MBEDTLS_PUT_UINT32_BE(session->ticket_age_add, p, 4);
3324 MBEDTLS_SSL_DEBUG_MSG(3, ("ticket_age_add: %u",
3325 (unsigned int) session->ticket_age_add));
Jerry Yue67bef42022-07-07 07:29:42 +00003326
3327 /* Write ticket_nonce */
Gilles Peskine449bd832023-01-11 14:50:10 +01003328 p[8] = (unsigned char) ticket_nonce_size;
3329 if (ticket_nonce_size > 0) {
3330 memcpy(p + 9, ticket_nonce, ticket_nonce_size);
Jerry Yue67bef42022-07-07 07:29:42 +00003331 }
3332 p += 9 + ticket_nonce_size;
3333
3334 /* Write ticket */
Gilles Peskine449bd832023-01-11 14:50:10 +01003335 MBEDTLS_PUT_UINT16_BE(ticket_len, p, 0);
Jerry Yue67bef42022-07-07 07:29:42 +00003336 p += 2;
Gilles Peskine449bd832023-01-11 14:50:10 +01003337 MBEDTLS_SSL_DEBUG_BUF(4, "ticket", p, ticket_len);
Jerry Yue67bef42022-07-07 07:29:42 +00003338 p += ticket_len;
3339
3340 /* Ticket Extensions
3341 *
Jerry Yu01da35e2022-12-12 15:09:22 +08003342 * Extension extensions<0..2^16-2>;
Jerry Yue67bef42022-07-07 07:29:42 +00003343 */
Jerry Yuedab6372022-10-31 14:37:31 +08003344 ssl->handshake->sent_extensions = MBEDTLS_SSL_EXT_MASK_NONE;
3345
Gilles Peskine449bd832023-01-11 14:50:10 +01003346 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 2);
Jerry Yu01da35e2022-12-12 15:09:22 +08003347 p_extensions_len = p;
Jerry Yue67bef42022-07-07 07:29:42 +00003348 p += 2;
3349
Jerry Yu01da35e2022-12-12 15:09:22 +08003350#if defined(MBEDTLS_SSL_EARLY_DATA)
Pengyu Lv94a42cc2023-12-06 10:04:17 +08003351 if (mbedtls_ssl_tls13_session_ticket_allow_early_data(session)) {
Jerry Yu95648b02023-12-06 15:03:34 +08003352 size_t output_len;
3353
Jerry Yuf135bac2023-11-23 18:10:51 +08003354 if ((ret = mbedtls_ssl_tls13_write_early_data_ext(
Jerry Yuc59c5862023-12-05 10:40:49 +08003355 ssl, 1, p, end, &output_len)) != 0) {
Jerry Yuf135bac2023-11-23 18:10:51 +08003356 MBEDTLS_SSL_DEBUG_RET(
3357 1, "mbedtls_ssl_tls13_write_early_data_ext", ret);
3358 return ret;
3359 }
3360 p += output_len;
Jerry Yu9e7f9bc2023-11-27 16:52:07 +08003361 } else {
3362 MBEDTLS_SSL_DEBUG_MSG(
3363 4, ("early_data not allowed, "
3364 "skip early_data extension in NewSessionTicket"));
Jerry Yu01da35e2022-12-12 15:09:22 +08003365 }
Jerry Yuf135bac2023-11-23 18:10:51 +08003366
Jerry Yu01da35e2022-12-12 15:09:22 +08003367#endif /* MBEDTLS_SSL_EARLY_DATA */
3368
3369 MBEDTLS_PUT_UINT16_BE(p - p_extensions_len - 2, p_extensions_len, 0);
3370
Jerry Yue67bef42022-07-07 07:29:42 +00003371 *out_len = p - buf;
Gilles Peskine449bd832023-01-11 14:50:10 +01003372 MBEDTLS_SSL_DEBUG_BUF(4, "ticket", buf, *out_len);
3373 MBEDTLS_SSL_DEBUG_MSG(2, ("<= write new session ticket"));
Jerry Yue67bef42022-07-07 07:29:42 +00003374
Jerry Yu7de2ff02022-11-08 21:43:46 +08003375 MBEDTLS_SSL_PRINT_EXTS(
Gilles Peskine449bd832023-01-11 14:50:10 +01003376 3, MBEDTLS_SSL_HS_NEW_SESSION_TICKET, ssl->handshake->sent_extensions);
Jerry Yu4b8f2f72022-10-31 13:31:22 +08003377
Gilles Peskine449bd832023-01-11 14:50:10 +01003378 return 0;
Jerry Yue67bef42022-07-07 07:29:42 +00003379}
3380
3381/*
Jerry Yua8d3c502022-10-30 14:51:23 +08003382 * Handler for MBEDTLS_SSL_TLS1_3_NEW_SESSION_TICKET
Jerry Yue67bef42022-07-07 07:29:42 +00003383 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003384static int ssl_tls13_write_new_session_ticket(mbedtls_ssl_context *ssl)
Jerry Yue67bef42022-07-07 07:29:42 +00003385{
3386 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
3387
Gilles Peskine449bd832023-01-11 14:50:10 +01003388 MBEDTLS_SSL_PROC_CHK_NEG(ssl_tls13_write_new_session_ticket_coordinate(ssl));
Jerry Yue67bef42022-07-07 07:29:42 +00003389
Gilles Peskine449bd832023-01-11 14:50:10 +01003390 if (ret == SSL_NEW_SESSION_TICKET_WRITE) {
Jerry Yue67bef42022-07-07 07:29:42 +00003391 unsigned char ticket_nonce[MBEDTLS_SSL_TLS1_3_TICKET_NONCE_LENGTH];
3392 unsigned char *buf;
3393 size_t buf_len, msg_len;
3394
Gilles Peskine449bd832023-01-11 14:50:10 +01003395 MBEDTLS_SSL_PROC_CHK(ssl_tls13_prepare_new_session_ticket(
3396 ssl, ticket_nonce, sizeof(ticket_nonce)));
Jerry Yue67bef42022-07-07 07:29:42 +00003397
Xiaokang Qian9f1747b2023-03-30 02:50:04 +00003398 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_start_handshake_msg(
3399 ssl, MBEDTLS_SSL_HS_NEW_SESSION_TICKET,
3400 &buf, &buf_len));
Jerry Yue67bef42022-07-07 07:29:42 +00003401
Gilles Peskine449bd832023-01-11 14:50:10 +01003402 MBEDTLS_SSL_PROC_CHK(ssl_tls13_write_new_session_ticket_body(
3403 ssl, buf, buf + buf_len, &msg_len,
3404 ticket_nonce, sizeof(ticket_nonce)));
Jerry Yue67bef42022-07-07 07:29:42 +00003405
Gilles Peskine449bd832023-01-11 14:50:10 +01003406 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_finish_handshake_msg(
3407 ssl, buf_len, msg_len));
Jerry Yufca4d572022-07-21 10:37:48 +08003408
Jerry Yu359e65f2022-09-22 23:47:43 +08003409 /* Limit session tickets count to one when resumption connection.
3410 *
3411 * See document of mbedtls_ssl_conf_new_session_tickets.
3412 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003413 if (ssl->handshake->resume == 1) {
Jerry Yu359e65f2022-09-22 23:47:43 +08003414 ssl->handshake->new_session_tickets_count = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01003415 } else {
Jerry Yu359e65f2022-09-22 23:47:43 +08003416 ssl->handshake->new_session_tickets_count--;
Gilles Peskine449bd832023-01-11 14:50:10 +01003417 }
Jerry Yub7e3fa72022-09-22 11:07:18 +08003418
Jerry Yua8d3c502022-10-30 14:51:23 +08003419 mbedtls_ssl_handshake_set_state(
Gilles Peskine449bd832023-01-11 14:50:10 +01003420 ssl, MBEDTLS_SSL_TLS1_3_NEW_SESSION_TICKET_FLUSH);
3421 } else {
3422 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_HANDSHAKE_OVER);
Jerry Yue67bef42022-07-07 07:29:42 +00003423 }
3424
Jerry Yue67bef42022-07-07 07:29:42 +00003425cleanup:
3426
Gilles Peskine449bd832023-01-11 14:50:10 +01003427 return ret;
Jerry Yue67bef42022-07-07 07:29:42 +00003428}
3429#endif /* MBEDTLS_SSL_SESSION_TICKETS */
3430
3431/*
XiaokangQiane8ff3502022-04-22 02:34:40 +00003432 * TLS 1.3 State Machine -- server side
XiaokangQian7807f9f2022-02-15 10:04:37 +00003433 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003434int mbedtls_ssl_tls13_handshake_server_step(mbedtls_ssl_context *ssl)
Jerry Yub9930e72021-08-06 17:11:51 +08003435{
XiaokangQian08037552022-04-20 07:16:41 +00003436 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
XiaokangQian7807f9f2022-02-15 10:04:37 +00003437
Gilles Peskine449bd832023-01-11 14:50:10 +01003438 if (ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER || ssl->handshake == NULL) {
3439 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
3440 }
XiaokangQian7807f9f2022-02-15 10:04:37 +00003441
Gilles Peskine449bd832023-01-11 14:50:10 +01003442 MBEDTLS_SSL_DEBUG_MSG(2, ("tls13 server state: %s(%d)",
Dave Rodgman2eab4622023-10-05 13:30:37 +01003443 mbedtls_ssl_states_str((mbedtls_ssl_states) ssl->state),
Gilles Peskine449bd832023-01-11 14:50:10 +01003444 ssl->state));
Jerry Yu6e81b272021-09-27 11:16:17 +08003445
Gilles Peskine449bd832023-01-11 14:50:10 +01003446 switch (ssl->state) {
XiaokangQian7807f9f2022-02-15 10:04:37 +00003447 /* start state */
3448 case MBEDTLS_SSL_HELLO_REQUEST:
Gilles Peskine449bd832023-01-11 14:50:10 +01003449 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_CLIENT_HELLO);
XiaokangQian08037552022-04-20 07:16:41 +00003450 ret = 0;
XiaokangQian7807f9f2022-02-15 10:04:37 +00003451 break;
3452
XiaokangQian7807f9f2022-02-15 10:04:37 +00003453 case MBEDTLS_SSL_CLIENT_HELLO:
Gilles Peskine449bd832023-01-11 14:50:10 +01003454 ret = ssl_tls13_process_client_hello(ssl);
3455 if (ret != 0) {
3456 MBEDTLS_SSL_DEBUG_RET(1, "ssl_tls13_process_client_hello", ret);
3457 }
Jerry Yuf41553b2022-05-09 22:20:30 +08003458 break;
XiaokangQian7807f9f2022-02-15 10:04:37 +00003459
Jerry Yuf41553b2022-05-09 22:20:30 +08003460 case MBEDTLS_SSL_HELLO_RETRY_REQUEST:
Gilles Peskine449bd832023-01-11 14:50:10 +01003461 ret = ssl_tls13_write_hello_retry_request(ssl);
3462 if (ret != 0) {
3463 MBEDTLS_SSL_DEBUG_RET(1, "ssl_tls13_write_hello_retry_request", ret);
3464 return ret;
Jerry Yuf41553b2022-05-09 22:20:30 +08003465 }
XiaokangQian7807f9f2022-02-15 10:04:37 +00003466 break;
3467
Jerry Yu5b64ae92022-03-30 17:15:02 +08003468 case MBEDTLS_SSL_SERVER_HELLO:
Gilles Peskine449bd832023-01-11 14:50:10 +01003469 ret = ssl_tls13_write_server_hello(ssl);
Jerry Yu5b64ae92022-03-30 17:15:02 +08003470 break;
3471
Xiaofei Baicba64af2022-02-15 10:00:56 +00003472 case MBEDTLS_SSL_ENCRYPTED_EXTENSIONS:
Gilles Peskine449bd832023-01-11 14:50:10 +01003473 ret = ssl_tls13_write_encrypted_extensions(ssl);
3474 if (ret != 0) {
3475 MBEDTLS_SSL_DEBUG_RET(1, "ssl_tls13_write_encrypted_extensions", ret);
3476 return ret;
Xiaofei Baicba64af2022-02-15 10:00:56 +00003477 }
Jerry Yu48330562022-05-06 21:35:44 +08003478 break;
Jerry Yu6a2cd9e2022-05-05 11:14:19 +08003479
Ronald Cron928cbd32022-10-04 16:14:26 +02003480#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED)
Xiaofei Bai9ca09d42022-02-14 12:57:18 +00003481 case MBEDTLS_SSL_CERTIFICATE_REQUEST:
Gilles Peskine449bd832023-01-11 14:50:10 +01003482 ret = ssl_tls13_write_certificate_request(ssl);
Xiaofei Bai9ca09d42022-02-14 12:57:18 +00003483 break;
Xiaofei Bai9ca09d42022-02-14 12:57:18 +00003484
Jerry Yu83da34e2022-04-16 13:59:52 +08003485 case MBEDTLS_SSL_SERVER_CERTIFICATE:
Gilles Peskine449bd832023-01-11 14:50:10 +01003486 ret = ssl_tls13_write_server_certificate(ssl);
Jerry Yu83da34e2022-04-16 13:59:52 +08003487 break;
3488
3489 case MBEDTLS_SSL_CERTIFICATE_VERIFY:
Gilles Peskine449bd832023-01-11 14:50:10 +01003490 ret = ssl_tls13_write_certificate_verify(ssl);
Jerry Yu83da34e2022-04-16 13:59:52 +08003491 break;
Ronald Cron928cbd32022-10-04 16:14:26 +02003492#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED */
Jerry Yu83da34e2022-04-16 13:59:52 +08003493
Gilles Peskine449bd832023-01-11 14:50:10 +01003494 /*
3495 * Injection of dummy-CCS's for middlebox compatibility
3496 */
Gabor Mezei7b39bf12022-05-24 16:04:14 +02003497#if defined(MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE)
Gabor Mezeif7044ea2022-06-28 16:01:49 +02003498 case MBEDTLS_SSL_SERVER_CCS_AFTER_HELLO_RETRY_REQUEST:
Gilles Peskine449bd832023-01-11 14:50:10 +01003499 ret = mbedtls_ssl_tls13_write_change_cipher_spec(ssl);
3500 if (ret == 0) {
3501 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_CLIENT_HELLO);
3502 }
Gabor Mezei7b39bf12022-05-24 16:04:14 +02003503 break;
3504
3505 case MBEDTLS_SSL_SERVER_CCS_AFTER_SERVER_HELLO:
Ronald Crone273f722024-02-13 18:22:26 +01003506 ret = mbedtls_ssl_tls13_write_change_cipher_spec(ssl);
3507 if (ret != 0) {
3508 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01003509 }
Ronald Cronfe59ff72024-01-24 14:31:50 +01003510 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_ENCRYPTED_EXTENSIONS);
Gabor Mezei7b39bf12022-05-24 16:04:14 +02003511 break;
3512#endif /* MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE */
3513
Jerry Yu69dd8d42022-04-16 12:51:26 +08003514 case MBEDTLS_SSL_SERVER_FINISHED:
Gilles Peskine449bd832023-01-11 14:50:10 +01003515 ret = ssl_tls13_write_server_finished(ssl);
Jerry Yu69dd8d42022-04-16 12:51:26 +08003516 break;
3517
Jerry Yu87b5ed42022-12-12 13:07:07 +08003518#if defined(MBEDTLS_SSL_EARLY_DATA)
3519 case MBEDTLS_SSL_END_OF_EARLY_DATA:
Jerry Yu59d420f2023-12-01 16:30:34 +08003520 ret = ssl_tls13_process_end_of_early_data(ssl);
Jerry Yu87b5ed42022-12-12 13:07:07 +08003521 break;
3522#endif /* MBEDTLS_SSL_EARLY_DATA */
3523
Jerry Yu69dd8d42022-04-16 12:51:26 +08003524 case MBEDTLS_SSL_CLIENT_FINISHED:
Gilles Peskine449bd832023-01-11 14:50:10 +01003525 ret = ssl_tls13_process_client_finished(ssl);
Jerry Yu69dd8d42022-04-16 12:51:26 +08003526 break;
3527
Jerry Yu69dd8d42022-04-16 12:51:26 +08003528 case MBEDTLS_SSL_HANDSHAKE_WRAPUP:
Gilles Peskine449bd832023-01-11 14:50:10 +01003529 ret = ssl_tls13_handshake_wrapup(ssl);
Jerry Yu69dd8d42022-04-16 12:51:26 +08003530 break;
3531
Ronald Cron766c0cd2022-10-18 12:17:11 +02003532#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED)
XiaokangQian6b916b12022-04-25 07:29:34 +00003533 case MBEDTLS_SSL_CLIENT_CERTIFICATE:
Gilles Peskine449bd832023-01-11 14:50:10 +01003534 ret = mbedtls_ssl_tls13_process_certificate(ssl);
3535 if (ret == 0) {
3536 if (ssl->session_negotiate->peer_cert != NULL) {
Ronald Cron209cae92022-06-07 10:30:19 +02003537 mbedtls_ssl_handshake_set_state(
Gilles Peskine449bd832023-01-11 14:50:10 +01003538 ssl, MBEDTLS_SSL_CLIENT_CERTIFICATE_VERIFY);
3539 } else {
3540 MBEDTLS_SSL_DEBUG_MSG(2, ("skip parse certificate verify"));
Ronald Cron209cae92022-06-07 10:30:19 +02003541 mbedtls_ssl_handshake_set_state(
Gilles Peskine449bd832023-01-11 14:50:10 +01003542 ssl, MBEDTLS_SSL_CLIENT_FINISHED);
Ronald Cron19385882022-06-15 16:26:13 +02003543 }
XiaokangQian6b916b12022-04-25 07:29:34 +00003544 }
3545 break;
3546
3547 case MBEDTLS_SSL_CLIENT_CERTIFICATE_VERIFY:
Gilles Peskine449bd832023-01-11 14:50:10 +01003548 ret = mbedtls_ssl_tls13_process_certificate_verify(ssl);
3549 if (ret == 0) {
XiaokangQian6b916b12022-04-25 07:29:34 +00003550 mbedtls_ssl_handshake_set_state(
Gilles Peskine449bd832023-01-11 14:50:10 +01003551 ssl, MBEDTLS_SSL_CLIENT_FINISHED);
XiaokangQian6b916b12022-04-25 07:29:34 +00003552 }
3553 break;
Ronald Cron766c0cd2022-10-18 12:17:11 +02003554#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED */
XiaokangQian6b916b12022-04-25 07:29:34 +00003555
Jerry Yue67bef42022-07-07 07:29:42 +00003556#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Jerry Yua8d3c502022-10-30 14:51:23 +08003557 case MBEDTLS_SSL_TLS1_3_NEW_SESSION_TICKET:
Gilles Peskine449bd832023-01-11 14:50:10 +01003558 ret = ssl_tls13_write_new_session_ticket(ssl);
3559 if (ret != 0) {
3560 MBEDTLS_SSL_DEBUG_RET(1,
3561 "ssl_tls13_write_new_session_ticket ",
3562 ret);
Jerry Yue67bef42022-07-07 07:29:42 +00003563 }
3564 break;
Jerry Yua8d3c502022-10-30 14:51:23 +08003565 case MBEDTLS_SSL_TLS1_3_NEW_SESSION_TICKET_FLUSH:
Jerry Yue67bef42022-07-07 07:29:42 +00003566 /* This state is necessary to do the flush of the New Session
Jerry Yua8d3c502022-10-30 14:51:23 +08003567 * Ticket message written in MBEDTLS_SSL_TLS1_3_NEW_SESSION_TICKET
Jerry Yue67bef42022-07-07 07:29:42 +00003568 * as part of ssl_prepare_handshake_step.
3569 */
3570 ret = 0;
Jerry Yud4e75002022-08-09 13:33:50 +08003571
Gilles Peskine449bd832023-01-11 14:50:10 +01003572 if (ssl->handshake->new_session_tickets_count == 0) {
3573 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_HANDSHAKE_OVER);
3574 } else {
Xiaokang Qian9f1747b2023-03-30 02:50:04 +00003575 mbedtls_ssl_handshake_set_state(
3576 ssl, MBEDTLS_SSL_TLS1_3_NEW_SESSION_TICKET);
Gilles Peskine449bd832023-01-11 14:50:10 +01003577 }
Jerry Yue67bef42022-07-07 07:29:42 +00003578 break;
3579
3580#endif /* MBEDTLS_SSL_SESSION_TICKETS */
3581
XiaokangQian7807f9f2022-02-15 10:04:37 +00003582 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01003583 MBEDTLS_SSL_DEBUG_MSG(1, ("invalid state %d", ssl->state));
3584 return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
XiaokangQian7807f9f2022-02-15 10:04:37 +00003585 }
3586
Gilles Peskine449bd832023-01-11 14:50:10 +01003587 return ret;
Jerry Yub9930e72021-08-06 17:11:51 +08003588}
Jerry Yu3cc4c2a2021-08-06 16:29:08 +08003589
Jerry Yufb4b6472022-01-27 15:03:26 +08003590#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_PROTO_TLS1_3 */