blob: ab27c94efcbb27a79d37a7e2935ebbc9eea6ce55 [file] [log] [blame]
Jerry Yu3cc4c2a2021-08-06 16:29:08 +08001/*
Jerry Yub9930e72021-08-06 17:11:51 +08002 * TLS 1.3 server-side functions
Jerry Yu3cc4c2a2021-08-06 16:29:08 +08003 *
4 * Copyright The Mbed TLS Contributors
Dave Rodgman16799db2023-11-02 19:47:20 +00005 * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
Gilles Peskine449bd832023-01-11 14:50:10 +01006 */
Jerry Yu3cc4c2a2021-08-06 16:29:08 +08007
Harry Ramsey0f6bc412024-10-04 10:36:54 +01008#include "ssl_misc.h"
Jerry Yu3cc4c2a2021-08-06 16:29:08 +08009
Jerry Yufb4b6472022-01-27 15:03:26 +080010#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_PROTO_TLS1_3)
Jerry Yu3cc4c2a2021-08-06 16:29:08 +080011
Valerio Settib4f50762024-01-17 10:24:52 +010012#include "debug_internal.h"
Xiaofei Baicba64af2022-02-15 10:00:56 +000013#include "mbedtls/error.h"
14#include "mbedtls/platform.h"
Jerry Yu1c105562022-07-10 06:32:38 +000015#include "mbedtls/constant_time.h"
Manuel Pégourié-Gonnardd55d66f2023-06-20 10:14:58 +020016#include "mbedtls/oid.h"
Valerio Setti384fbde2024-01-02 13:26:40 +010017#include "mbedtls/psa_util.h"
Jerry Yu3bf2c642022-03-30 22:02:12 +080018
Xiaofei Bai9ca09d42022-02-14 12:57:18 +000019#include "ssl_tls13_keys.h"
20#include "ssl_debug_helpers.h"
21
Jerry Yuf35ba382022-08-23 17:58:26 +080022
Jerry Yuc5a23a02022-08-25 10:51:44 +080023static const mbedtls_ssl_ciphersuite_t *ssl_tls13_validate_peer_ciphersuite(
Gilles Peskine449bd832023-01-11 14:50:10 +010024 mbedtls_ssl_context *ssl,
25 unsigned int cipher_suite)
Jerry Yuf35ba382022-08-23 17:58:26 +080026{
27 const mbedtls_ssl_ciphersuite_t *ciphersuite_info;
Gilles Peskine449bd832023-01-11 14:50:10 +010028 if (!mbedtls_ssl_tls13_cipher_suite_is_offered(ssl, cipher_suite)) {
29 return NULL;
Jerry Yuf35ba382022-08-23 17:58:26 +080030 }
Gilles Peskine449bd832023-01-11 14:50:10 +010031
32 ciphersuite_info = mbedtls_ssl_ciphersuite_from_id(cipher_suite);
33 if ((mbedtls_ssl_validate_ciphersuite(ssl, ciphersuite_info,
34 ssl->tls_version,
35 ssl->tls_version) != 0)) {
36 return NULL;
37 }
38 return ciphersuite_info;
Jerry Yuf35ba382022-08-23 17:58:26 +080039}
40
Ronald Cron89089cc2024-02-14 18:18:08 +010041static void ssl_tls13_select_ciphersuite(
42 mbedtls_ssl_context *ssl,
43 const unsigned char *cipher_suites,
44 const unsigned char *cipher_suites_end,
45 int psk_ciphersuite_id,
46 psa_algorithm_t psk_hash_alg,
47 const mbedtls_ssl_ciphersuite_t **selected_ciphersuite_info)
48{
49 *selected_ciphersuite_info = NULL;
50
51 /*
Ronald Cron38117652024-03-05 09:05:46 +010052 * In a compliant ClientHello the byte-length of the list of ciphersuites
53 * is even and this function relies on this fact. This should have been
54 * checked in the main ClientHello parsing function. Double check here.
Ronald Cron89089cc2024-02-14 18:18:08 +010055 */
56 if ((cipher_suites_end - cipher_suites) & 1) {
57 return;
58 }
59
60 for (const unsigned char *p = cipher_suites;
61 p < cipher_suites_end; p += 2) {
62 /*
63 * "cipher_suites_end - p is even" is an invariant of the loop. As
64 * cipher_suites_end - p > 0, we have cipher_suites_end - p >= 2 and it
65 * is thus safe to read two bytes.
66 */
67 uint16_t id = MBEDTLS_GET_UINT16_BE(p, 0);
68
69 const mbedtls_ssl_ciphersuite_t *info =
70 ssl_tls13_validate_peer_ciphersuite(ssl, id);
71 if (info == NULL) {
72 continue;
73 }
74
75 /*
Ronald Cron7cab4f82024-03-07 15:09:09 +010076 * If a valid PSK ciphersuite identifier has been passed in, we want
77 * an exact match.
Ronald Cron89089cc2024-02-14 18:18:08 +010078 */
79 if (psk_ciphersuite_id != 0) {
80 if (id != psk_ciphersuite_id) {
81 continue;
82 }
83 } else if (psk_hash_alg != PSA_ALG_NONE) {
84 if (mbedtls_md_psa_alg_from_type((mbedtls_md_type_t) info->mac) !=
85 psk_hash_alg) {
86 continue;
87 }
88 }
89
90 *selected_ciphersuite_info = info;
91 return;
92 }
93
Gilles Peskinea9d4ef02024-06-03 22:16:23 +020094 MBEDTLS_SSL_DEBUG_MSG(2, ("No matched ciphersuite, psk_ciphersuite_id=%x, psk_hash_alg=%lx",
95 (unsigned) psk_ciphersuite_id,
96 (unsigned long) psk_hash_alg));
Ronald Cron89089cc2024-02-14 18:18:08 +010097}
98
Ronald Cron41a443a2022-10-04 16:38:25 +020099#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED)
Jerry Yue19e3b92022-07-08 12:04:51 +0000100/* From RFC 8446:
101 *
102 * enum { psk_ke(0), psk_dhe_ke(1), (255) } PskKeyExchangeMode;
103 * struct {
104 * PskKeyExchangeMode ke_modes<1..255>;
105 * } PskKeyExchangeModes;
106 */
Jerry Yu6e74a7e2022-07-20 20:49:32 +0800107MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100108static int ssl_tls13_parse_key_exchange_modes_ext(mbedtls_ssl_context *ssl,
109 const unsigned char *buf,
110 const unsigned char *end)
Jerry Yue19e3b92022-07-08 12:04:51 +0000111{
Jerry Yu299e31f2022-07-13 23:06:36 +0800112 const unsigned char *p = buf;
Jerry Yue19e3b92022-07-08 12:04:51 +0000113 size_t ke_modes_len;
114 int ke_modes = 0;
115
Jerry Yu854dd9e2022-07-15 14:28:27 +0800116 /* Read ke_modes length (1 Byte) */
Gilles Peskine449bd832023-01-11 14:50:10 +0100117 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, 1);
Jerry Yu299e31f2022-07-13 23:06:36 +0800118 ke_modes_len = *p++;
Jerry Yue19e3b92022-07-08 12:04:51 +0000119 /* Currently, there are only two PSK modes, so even without looking
120 * at the content, something's wrong if the list has more than 2 items. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100121 if (ke_modes_len > 2) {
122 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER,
123 MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER);
124 return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
Jerry Yu299e31f2022-07-13 23:06:36 +0800125 }
Jerry Yue19e3b92022-07-08 12:04:51 +0000126
Gilles Peskine449bd832023-01-11 14:50:10 +0100127 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, ke_modes_len);
Jerry Yue19e3b92022-07-08 12:04:51 +0000128
Gilles Peskine449bd832023-01-11 14:50:10 +0100129 while (ke_modes_len-- != 0) {
130 switch (*p++) {
131 case MBEDTLS_SSL_TLS1_3_PSK_MODE_PURE:
132 ke_modes |= MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK;
133 MBEDTLS_SSL_DEBUG_MSG(3, ("Found PSK KEX MODE"));
134 break;
135 case MBEDTLS_SSL_TLS1_3_PSK_MODE_ECDHE:
136 ke_modes |= MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK_EPHEMERAL;
137 MBEDTLS_SSL_DEBUG_MSG(3, ("Found PSK_EPHEMERAL KEX MODE"));
138 break;
139 default:
140 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER,
141 MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER);
142 return MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER;
Jerry Yue19e3b92022-07-08 12:04:51 +0000143 }
144 }
145
146 ssl->handshake->tls13_kex_modes = ke_modes;
Gilles Peskine449bd832023-01-11 14:50:10 +0100147 return 0;
Jerry Yue19e3b92022-07-08 12:04:51 +0000148}
Jerry Yu1c105562022-07-10 06:32:38 +0000149
Ronald Cron38117652024-03-05 09:05:46 +0100150/*
151 * Non-error return values of
152 * ssl_tls13_offered_psks_check_identity_match_ticket() and
153 * ssl_tls13_offered_psks_check_identity_match(). They are positive to
154 * not collide with error codes that are negative. Zero
155 * (SSL_TLS1_3_PSK_IDENTITY_MATCH) in case of success as it may be propagated
156 * up by the callers of this function as a generic success condition.
157 *
158 * The return value SSL_TLS1_3_PSK_IDENTITY_MATCH_BUT_PSK_NOT_USABLE means
Ronald Cron7cab4f82024-03-07 15:09:09 +0100159 * that the pre-shared-key identity matches that of a ticket or an externally-
Ronald Cron38117652024-03-05 09:05:46 +0100160 * provisioned pre-shared-key. We have thus been able to retrieve the
161 * attributes of the pre-shared-key but at least one of them does not meet
162 * some criteria and the pre-shared-key cannot be used. For example, a ticket
163 * is expired or its version is not TLS 1.3. Note eventually that the return
164 * value SSL_TLS1_3_PSK_IDENTITY_MATCH_BUT_PSK_NOT_USABLE does not have
165 * anything to do with binder check. A binder check is done only when a
166 * suitable pre-shared-key has been selected and only for that selected
167 * pre-shared-key: if the binder check fails, we fail the handshake and we do
168 * not try to find another pre-shared-key for which the binder check would
169 * succeed as recommended by the specification.
170 */
Ronald Cronfbae94a2023-12-05 18:15:14 +0100171#define SSL_TLS1_3_PSK_IDENTITY_DOES_NOT_MATCH 2
172#define SSL_TLS1_3_PSK_IDENTITY_MATCH_BUT_PSK_NOT_USABLE 1
173#define SSL_TLS1_3_PSK_IDENTITY_MATCH 0
Jerry Yu95699e72022-08-21 19:22:23 +0800174
Pengyu Lv29daf4a2023-10-30 17:13:30 +0800175MBEDTLS_CHECK_RETURN_CRITICAL
Pengyu Lvbc4aab72023-12-01 15:37:24 +0800176static int ssl_tls13_key_exchange_is_psk_available(mbedtls_ssl_context *ssl);
Pengyu Lv29daf4a2023-10-30 17:13:30 +0800177MBEDTLS_CHECK_RETURN_CRITICAL
Pengyu Lvbc4aab72023-12-01 15:37:24 +0800178static int ssl_tls13_key_exchange_is_psk_ephemeral_available(mbedtls_ssl_context *ssl);
Jerry Yu95699e72022-08-21 19:22:23 +0800179
Ronald Cron1f045f32024-03-25 13:37:07 +0100180#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Jerry Yu95699e72022-08-21 19:22:23 +0800181MBEDTLS_CHECK_RETURN_CRITICAL
182static int ssl_tls13_offered_psks_check_identity_match_ticket(
Gilles Peskine449bd832023-01-11 14:50:10 +0100183 mbedtls_ssl_context *ssl,
184 const unsigned char *identity,
185 size_t identity_len,
186 uint32_t obfuscated_ticket_age,
187 mbedtls_ssl_session *session)
Jerry Yu95699e72022-08-21 19:22:23 +0800188{
Jerry Yufd310eb2022-09-06 09:16:35 +0800189 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yu95699e72022-08-21 19:22:23 +0800190 unsigned char *ticket_buffer;
Jerry Yu8d4bbba2022-09-13 14:15:48 +0800191#if defined(MBEDTLS_HAVE_TIME)
Jerry Yucebffc32022-12-15 18:00:05 +0800192 mbedtls_ms_time_t now;
193 mbedtls_ms_time_t server_age;
Jerry Yu713ce1f2023-11-17 15:32:12 +0800194 uint32_t client_age;
Jerry Yucebffc32022-12-15 18:00:05 +0800195 mbedtls_ms_time_t age_diff;
Jerry Yu8d4bbba2022-09-13 14:15:48 +0800196#endif
Jerry Yu95699e72022-08-21 19:22:23 +0800197
198 ((void) obfuscated_ticket_age);
199
Gilles Peskine449bd832023-01-11 14:50:10 +0100200 MBEDTLS_SSL_DEBUG_MSG(2, ("=> check_identity_match_ticket"));
Jerry Yu95699e72022-08-21 19:22:23 +0800201
Jerry Yufd310eb2022-09-06 09:16:35 +0800202 /* Ticket parser is not configured, Skip */
Gilles Peskine449bd832023-01-11 14:50:10 +0100203 if (ssl->conf->f_ticket_parse == NULL || identity_len == 0) {
Ronald Cronfbae94a2023-12-05 18:15:14 +0100204 return SSL_TLS1_3_PSK_IDENTITY_DOES_NOT_MATCH;
Gilles Peskine449bd832023-01-11 14:50:10 +0100205 }
Jerry Yu95699e72022-08-21 19:22:23 +0800206
Jerry Yu4746b102022-09-13 11:11:48 +0800207 /* We create a copy of the encrypted ticket since the ticket parsing
208 * function is allowed to use its input buffer as an output buffer
209 * (in-place decryption). We do, however, need the original buffer for
210 * computing the PSK binder value.
Jerry Yu95699e72022-08-21 19:22:23 +0800211 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100212 ticket_buffer = mbedtls_calloc(1, identity_len);
213 if (ticket_buffer == NULL) {
Gilles Peskine449bd832023-01-11 14:50:10 +0100214 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
Jerry Yu95699e72022-08-21 19:22:23 +0800215 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100216 memcpy(ticket_buffer, identity, identity_len);
Jerry Yu95699e72022-08-21 19:22:23 +0800217
Ronald Cronfbae94a2023-12-05 18:15:14 +0100218 ret = ssl->conf->f_ticket_parse(ssl->conf->p_ticket,
219 session,
220 ticket_buffer, identity_len);
Ronald Cronf602f7b2024-03-05 09:11:55 +0100221 switch (ret) {
222 case 0:
223 ret = SSL_TLS1_3_PSK_IDENTITY_MATCH;
224 break;
225
226 case MBEDTLS_ERR_SSL_SESSION_TICKET_EXPIRED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100227 MBEDTLS_SSL_DEBUG_MSG(3, ("ticket is expired"));
Ronald Cronfbae94a2023-12-05 18:15:14 +0100228 ret = SSL_TLS1_3_PSK_IDENTITY_MATCH_BUT_PSK_NOT_USABLE;
Ronald Cronf602f7b2024-03-05 09:11:55 +0100229 break;
230
231 case MBEDTLS_ERR_SSL_INVALID_MAC:
232 MBEDTLS_SSL_DEBUG_MSG(3, ("ticket is not authentic"));
Ronald Cronfbae94a2023-12-05 18:15:14 +0100233 ret = SSL_TLS1_3_PSK_IDENTITY_DOES_NOT_MATCH;
Ronald Cronf602f7b2024-03-05 09:11:55 +0100234 break;
235
236 default:
Gilles Peskine449bd832023-01-11 14:50:10 +0100237 MBEDTLS_SSL_DEBUG_RET(1, "ticket_parse", ret);
Ronald Cronf602f7b2024-03-05 09:11:55 +0100238 ret = SSL_TLS1_3_PSK_IDENTITY_DOES_NOT_MATCH;
Jerry Yu95699e72022-08-21 19:22:23 +0800239 }
240
241 /* We delete the temporary buffer */
Gilles Peskine449bd832023-01-11 14:50:10 +0100242 mbedtls_free(ticket_buffer);
Jerry Yu95699e72022-08-21 19:22:23 +0800243
Ronald Cronfbae94a2023-12-05 18:15:14 +0100244 if (ret != SSL_TLS1_3_PSK_IDENTITY_MATCH) {
Jerry Yu8d4bbba2022-09-13 14:15:48 +0800245 goto exit;
246 }
Jerry Yu95699e72022-08-21 19:22:23 +0800247
Ronald Cron38117652024-03-05 09:05:46 +0100248 /*
249 * The identity matches that of a ticket. Now check that it has suitable
250 * attributes and bet it will not be the case.
Pengyu Lv3eb49be2022-12-05 16:35:12 +0800251 */
Ronald Cronfbae94a2023-12-05 18:15:14 +0100252 ret = SSL_TLS1_3_PSK_IDENTITY_MATCH_BUT_PSK_NOT_USABLE;
Pengyu Lv29daf4a2023-10-30 17:13:30 +0800253
Ronald Cronfbae94a2023-12-05 18:15:14 +0100254 if (session->tls_version != MBEDTLS_SSL_VERSION_TLS1_3) {
255 MBEDTLS_SSL_DEBUG_MSG(3, ("Ticket TLS version is not 1.3."));
Pengyu Lv3eb49be2022-12-05 16:35:12 +0800256 goto exit;
257 }
258
Gilles Peskine449bd832023-01-11 14:50:10 +0100259#if defined(MBEDTLS_HAVE_TIME)
Jerry Yucebffc32022-12-15 18:00:05 +0800260 now = mbedtls_ms_time();
Gilles Peskine449bd832023-01-11 14:50:10 +0100261
Jerry Yu25ba4d42023-11-10 14:12:20 +0800262 if (now < session->ticket_creation_time) {
Gilles Peskine449bd832023-01-11 14:50:10 +0100263 MBEDTLS_SSL_DEBUG_MSG(
Jerry Yu713ce1f2023-11-17 15:32:12 +0800264 3, ("Invalid ticket creation time ( now = %" MBEDTLS_PRINTF_MS_TIME
265 ", creation_time = %" MBEDTLS_PRINTF_MS_TIME " )",
Jerry Yu25ba4d42023-11-10 14:12:20 +0800266 now, session->ticket_creation_time));
Gilles Peskine449bd832023-01-11 14:50:10 +0100267 goto exit;
268 }
269
Jerry Yu25ba4d42023-11-10 14:12:20 +0800270 server_age = now - session->ticket_creation_time;
Jerry Yu95699e72022-08-21 19:22:23 +0800271
Jerry Yu8d4bbba2022-09-13 14:15:48 +0800272 /* RFC 8446 section 4.6.1
273 *
274 * Servers MUST NOT use any value greater than 604800 seconds (7 days).
275 *
276 * RFC 8446 section 4.2.11.1
277 *
278 * Clients MUST NOT attempt to use tickets which have ages greater than
279 * the "ticket_lifetime" value which was provided with the ticket.
280 *
Jerry Yu8d4bbba2022-09-13 14:15:48 +0800281 */
Jerry Yu8e0174a2023-11-10 13:58:16 +0800282 if (server_age > MBEDTLS_SSL_TLS1_3_MAX_ALLOWED_TICKET_LIFETIME * 1000) {
Jerry Yu8d4bbba2022-09-13 14:15:48 +0800283 MBEDTLS_SSL_DEBUG_MSG(
Jerry Yud84c14f2023-11-16 13:33:57 +0800284 3, ("Ticket age exceeds limitation ticket_age = %" MBEDTLS_PRINTF_MS_TIME,
Jerry Yucebffc32022-12-15 18:00:05 +0800285 server_age));
Jerry Yu8d4bbba2022-09-13 14:15:48 +0800286 goto exit;
287 }
Jerry Yu95699e72022-08-21 19:22:23 +0800288
Jerry Yu8d4bbba2022-09-13 14:15:48 +0800289 /* RFC 8446 section 4.2.10
290 *
291 * For PSKs provisioned via NewSessionTicket, a server MUST validate that
292 * the ticket age for the selected PSK identity (computed by subtracting
293 * ticket_age_add from PskIdentity.obfuscated_ticket_age modulo 2^32) is
294 * within a small tolerance of the time since the ticket was issued.
Jerry Yuf7dad3c2022-09-14 22:31:39 +0800295 *
Jerry Yu31b601a2023-11-10 11:27:21 +0800296 * NOTE: The typical accuracy of an RTC crystal is ±100 to ±20 parts per
297 * million (360 to 72 milliseconds per hour). Default tolerance
Jerry Yu9cb953a2023-11-15 09:54:11 +0800298 * window is 6s, thus in the worst case clients and servers must
Jerry Yu31b601a2023-11-10 11:27:21 +0800299 * sync up their system time every 6000/360/2~=8 hours.
Jerry Yu8d4bbba2022-09-13 14:15:48 +0800300 */
Jerry Yucebffc32022-12-15 18:00:05 +0800301 client_age = obfuscated_ticket_age - session->ticket_age_add;
Jerry Yu60e99722023-11-20 09:55:24 +0800302 age_diff = server_age - (mbedtls_ms_time_t) client_age;
Jerry Yu28e7c552023-11-10 12:05:56 +0800303 if (age_diff < -MBEDTLS_SSL_TLS1_3_TICKET_AGE_TOLERANCE ||
Jerry Yucebffc32022-12-15 18:00:05 +0800304 age_diff > MBEDTLS_SSL_TLS1_3_TICKET_AGE_TOLERANCE) {
Jerry Yu8d4bbba2022-09-13 14:15:48 +0800305 MBEDTLS_SSL_DEBUG_MSG(
Jerry Yuf16efbc2023-10-30 11:06:24 +0800306 3, ("Ticket age outside tolerance window ( diff = %"
307 MBEDTLS_PRINTF_MS_TIME ")",
Jerry Yucebffc32022-12-15 18:00:05 +0800308 age_diff));
Jerry Yu8d4bbba2022-09-13 14:15:48 +0800309 goto exit;
310 }
Jerry Yu95699e72022-08-21 19:22:23 +0800311#endif /* MBEDTLS_HAVE_TIME */
Jerry Yu8d4bbba2022-09-13 14:15:48 +0800312
Ronald Cron38117652024-03-05 09:05:46 +0100313 /*
314 * All good, we have found a suitable ticket.
315 */
Ronald Cronfbae94a2023-12-05 18:15:14 +0100316 ret = SSL_TLS1_3_PSK_IDENTITY_MATCH;
317
Jerry Yu8d4bbba2022-09-13 14:15:48 +0800318exit:
Ronald Cronfbae94a2023-12-05 18:15:14 +0100319 if (ret != SSL_TLS1_3_PSK_IDENTITY_MATCH) {
Gilles Peskine449bd832023-01-11 14:50:10 +0100320 mbedtls_ssl_session_free(session);
321 }
Jerry Yu95699e72022-08-21 19:22:23 +0800322
Gilles Peskine449bd832023-01-11 14:50:10 +0100323 MBEDTLS_SSL_DEBUG_MSG(2, ("<= check_identity_match_ticket"));
324 return ret;
Jerry Yu95699e72022-08-21 19:22:23 +0800325}
326#endif /* MBEDTLS_SSL_SESSION_TICKETS */
327
Jerry Yu6e74a7e2022-07-20 20:49:32 +0800328MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yu1c105562022-07-10 06:32:38 +0000329static int ssl_tls13_offered_psks_check_identity_match(
Gilles Peskine449bd832023-01-11 14:50:10 +0100330 mbedtls_ssl_context *ssl,
331 const unsigned char *identity,
332 size_t identity_len,
333 uint32_t obfuscated_ticket_age,
334 int *psk_type,
335 mbedtls_ssl_session *session)
Jerry Yu1c105562022-07-10 06:32:38 +0000336{
Ronald Cron606671e2023-02-17 11:36:33 +0100337 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
338
Jerry Yu95699e72022-08-21 19:22:23 +0800339 ((void) session);
340 ((void) obfuscated_ticket_age);
Jerry Yu5725f1c2022-08-21 17:27:16 +0800341 *psk_type = MBEDTLS_SSL_TLS1_3_PSK_EXTERNAL;
Jerry Yu95699e72022-08-21 19:22:23 +0800342
Gilles Peskine449bd832023-01-11 14:50:10 +0100343 MBEDTLS_SSL_DEBUG_BUF(4, "identity", identity, identity_len);
Jerry Yu95699e72022-08-21 19:22:23 +0800344
Jerry Yu95699e72022-08-21 19:22:23 +0800345#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Ronald Cron7a30cf52024-02-23 14:38:59 +0100346 ret = ssl_tls13_offered_psks_check_identity_match_ticket(
347 ssl, identity, identity_len, obfuscated_ticket_age, session);
348 if (ret == SSL_TLS1_3_PSK_IDENTITY_MATCH) {
Jerry Yu95699e72022-08-21 19:22:23 +0800349 *psk_type = MBEDTLS_SSL_TLS1_3_PSK_RESUMPTION;
Ronald Cron606671e2023-02-17 11:36:33 +0100350 ret = mbedtls_ssl_set_hs_psk(ssl,
351 session->resumption_key,
352 session->resumption_key_len);
353 if (ret != 0) {
354 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_set_hs_psk", ret);
355 return ret;
356 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100357
358 MBEDTLS_SSL_DEBUG_BUF(4, "Ticket-resumed PSK:",
359 session->resumption_key,
360 session->resumption_key_len);
361 MBEDTLS_SSL_DEBUG_MSG(4, ("ticket: obfuscated_ticket_age: %u",
362 (unsigned) obfuscated_ticket_age));
Ronald Cronfbae94a2023-12-05 18:15:14 +0100363 return SSL_TLS1_3_PSK_IDENTITY_MATCH;
Ronald Cron7a30cf52024-02-23 14:38:59 +0100364 } else if (ret == SSL_TLS1_3_PSK_IDENTITY_MATCH_BUT_PSK_NOT_USABLE) {
365 return SSL_TLS1_3_PSK_IDENTITY_MATCH_BUT_PSK_NOT_USABLE;
Jerry Yu95699e72022-08-21 19:22:23 +0800366 }
367#endif /* MBEDTLS_SSL_SESSION_TICKETS */
368
Jerry Yu1c105562022-07-10 06:32:38 +0000369 /* Check identity with external configured function */
Gilles Peskine449bd832023-01-11 14:50:10 +0100370 if (ssl->conf->f_psk != NULL) {
371 if (ssl->conf->f_psk(
372 ssl->conf->p_psk, ssl, identity, identity_len) == 0) {
Ronald Cronfbae94a2023-12-05 18:15:14 +0100373 return SSL_TLS1_3_PSK_IDENTITY_MATCH;
Jerry Yu1c105562022-07-10 06:32:38 +0000374 }
Ronald Cronfbae94a2023-12-05 18:15:14 +0100375 return SSL_TLS1_3_PSK_IDENTITY_DOES_NOT_MATCH;
Jerry Yu1c105562022-07-10 06:32:38 +0000376 }
377
Gilles Peskine449bd832023-01-11 14:50:10 +0100378 MBEDTLS_SSL_DEBUG_BUF(5, "identity", identity, identity_len);
Jerry Yu1c105562022-07-10 06:32:38 +0000379 /* Check identity with pre-configured psk */
Gilles Peskine449bd832023-01-11 14:50:10 +0100380 if (ssl->conf->psk_identity != NULL &&
Jerry Yu2f0abc92022-07-22 19:34:48 +0800381 identity_len == ssl->conf->psk_identity_len &&
Gilles Peskine449bd832023-01-11 14:50:10 +0100382 mbedtls_ct_memcmp(ssl->conf->psk_identity,
383 identity, identity_len) == 0) {
Ronald Cron606671e2023-02-17 11:36:33 +0100384 ret = mbedtls_ssl_set_hs_psk(ssl, ssl->conf->psk, ssl->conf->psk_len);
385 if (ret != 0) {
386 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_set_hs_psk", ret);
387 return ret;
388 }
Ronald Cronfbae94a2023-12-05 18:15:14 +0100389 return SSL_TLS1_3_PSK_IDENTITY_MATCH;
Jerry Yu1c105562022-07-10 06:32:38 +0000390 }
391
Ronald Cronfbae94a2023-12-05 18:15:14 +0100392 return SSL_TLS1_3_PSK_IDENTITY_DOES_NOT_MATCH;
Jerry Yu1c105562022-07-10 06:32:38 +0000393}
394
Ronald Cron38117652024-03-05 09:05:46 +0100395/*
396 * Non-error return values of ssl_tls13_offered_psks_check_binder_match().
397 * They are positive to not collide with error codes that are negative. Zero
398 * (SSL_TLS1_3_BINDER_MATCH) in case of success as it may be propagated up
399 * by the callers of this function as a generic success condition.
400 */
Ronald Cron6e311272023-12-05 17:57:01 +0100401#define SSL_TLS1_3_BINDER_DOES_NOT_MATCH 1
402#define SSL_TLS1_3_BINDER_MATCH 0
Jerry Yu6e74a7e2022-07-20 20:49:32 +0800403MBEDTLS_CHECK_RETURN_CRITICAL
Xiaokang Qian9f1747b2023-03-30 02:50:04 +0000404static int ssl_tls13_offered_psks_check_binder_match(
405 mbedtls_ssl_context *ssl,
406 const unsigned char *binder, size_t binder_len,
407 int psk_type, psa_algorithm_t psk_hash_alg)
Jerry Yu1c105562022-07-10 06:32:38 +0000408{
409 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yu96a2e362022-07-21 15:11:34 +0800410
Jerry Yudaf375a2022-07-20 21:31:43 +0800411 unsigned char transcript[PSA_HASH_MAX_SIZE];
Jerry Yu1c105562022-07-10 06:32:38 +0000412 size_t transcript_len;
Jerry Yu2f0abc92022-07-22 19:34:48 +0800413 unsigned char *psk;
Jerry Yu96a2e362022-07-21 15:11:34 +0800414 size_t psk_len;
Jerry Yudaf375a2022-07-20 21:31:43 +0800415 unsigned char server_computed_binder[PSA_HASH_MAX_SIZE];
Jerry Yu1c105562022-07-10 06:32:38 +0000416
Ronald Crona5c5c582024-03-19 13:54:15 +0100417 if (binder_len != PSA_HASH_LENGTH(psk_hash_alg)) {
418 return SSL_TLS1_3_BINDER_DOES_NOT_MATCH;
419 }
420
Jerry Yu1c105562022-07-10 06:32:38 +0000421 /* Get current state of handshake transcript. */
Jerry Yu29d9faa2022-08-23 17:52:45 +0800422 ret = mbedtls_ssl_get_handshake_transcript(
Manuel Pégourié-Gonnard2d6d9932023-03-28 11:38:08 +0200423 ssl, mbedtls_md_type_from_psa_alg(psk_hash_alg),
Gilles Peskine449bd832023-01-11 14:50:10 +0100424 transcript, sizeof(transcript), &transcript_len);
425 if (ret != 0) {
426 return ret;
427 }
Jerry Yu1c105562022-07-10 06:32:38 +0000428
Gilles Peskine449bd832023-01-11 14:50:10 +0100429 ret = mbedtls_ssl_tls13_export_handshake_psk(ssl, &psk, &psk_len);
430 if (ret != 0) {
431 return ret;
432 }
Jerry Yu96a2e362022-07-21 15:11:34 +0800433
Gilles Peskine449bd832023-01-11 14:50:10 +0100434 ret = mbedtls_ssl_tls13_create_psk_binder(ssl, psk_hash_alg,
435 psk, psk_len, psk_type,
436 transcript,
437 server_computed_binder);
Jerry Yu96a2e362022-07-21 15:11:34 +0800438#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +0100439 mbedtls_free((void *) psk);
Jerry Yu96a2e362022-07-21 15:11:34 +0800440#endif
Gilles Peskine449bd832023-01-11 14:50:10 +0100441 if (ret != 0) {
442 MBEDTLS_SSL_DEBUG_MSG(1, ("PSK binder calculation failed."));
443 return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
Jerry Yu1c105562022-07-10 06:32:38 +0000444 }
445
Gilles Peskine449bd832023-01-11 14:50:10 +0100446 MBEDTLS_SSL_DEBUG_BUF(3, "psk binder ( computed ): ",
447 server_computed_binder, transcript_len);
448 MBEDTLS_SSL_DEBUG_BUF(3, "psk binder ( received ): ", binder, binder_len);
Jerry Yu1c105562022-07-10 06:32:38 +0000449
Ronald Crona5c5c582024-03-19 13:54:15 +0100450 if (mbedtls_ct_memcmp(server_computed_binder,
451 binder,
452 PSA_HASH_LENGTH(psk_hash_alg)) == 0) {
Ronald Cron6e311272023-12-05 17:57:01 +0100453 return SSL_TLS1_3_BINDER_MATCH;
Jerry Yu1c105562022-07-10 06:32:38 +0000454 }
455
Gilles Peskine449bd832023-01-11 14:50:10 +0100456 mbedtls_platform_zeroize(server_computed_binder,
457 sizeof(server_computed_binder));
Ronald Cron6e311272023-12-05 17:57:01 +0100458 return SSL_TLS1_3_BINDER_DOES_NOT_MATCH;
Jerry Yuf35ba382022-08-23 17:58:26 +0800459}
Jerry Yu5725f1c2022-08-21 17:27:16 +0800460
Jerry Yu82534862022-08-30 10:42:33 +0800461#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Jerry Yuf35ba382022-08-23 17:58:26 +0800462MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100463static int ssl_tls13_session_copy_ticket(mbedtls_ssl_session *dst,
464 const mbedtls_ssl_session *src)
Jerry Yu82534862022-08-30 10:42:33 +0800465{
Jerry Yu82534862022-08-30 10:42:33 +0800466 dst->ticket_age_add = src->ticket_age_add;
467 dst->ticket_flags = src->ticket_flags;
468 dst->resumption_key_len = src->resumption_key_len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100469 if (src->resumption_key_len == 0) {
470 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
471 }
472 memcpy(dst->resumption_key, src->resumption_key, src->resumption_key_len);
Jerry Yu4746b102022-09-13 11:11:48 +0800473
Jerry Yu33bf2402022-12-12 16:01:43 +0800474#if defined(MBEDTLS_SSL_EARLY_DATA)
475 dst->max_early_data_size = src->max_early_data_size;
Waleed Elmelegy2824a202024-02-23 17:51:47 +0000476
477#if defined(MBEDTLS_SSL_ALPN)
Waleed Elmelegy5bc52632024-03-12 16:25:08 +0000478 int ret = mbedtls_ssl_session_set_ticket_alpn(dst, src->ticket_alpn);
Waleed Elmelegy883f77c2024-03-06 19:09:41 +0000479 if (ret != 0) {
480 return ret;
Waleed Elmelegy2824a202024-02-23 17:51:47 +0000481 }
482#endif /* MBEDTLS_SSL_ALPN */
483#endif /* MBEDTLS_SSL_EARLY_DATA*/
Jerry Yu33bf2402022-12-12 16:01:43 +0800484
Gilles Peskine449bd832023-01-11 14:50:10 +0100485 return 0;
Jerry Yu82534862022-08-30 10:42:33 +0800486}
487#endif /* MBEDTLS_SSL_SESSION_TICKETS */
488
Ronald Cron79cdd412024-02-20 17:19:28 +0100489struct psk_attributes {
490 int type;
491 int key_exchange_mode;
Ronald Cron74a16292024-02-23 17:07:41 +0100492 const mbedtls_ssl_ciphersuite_t *ciphersuite_info;
Ronald Cron79cdd412024-02-20 17:19:28 +0100493};
Ronald Cron16cc3702024-03-07 15:04:56 +0100494#define PSK_ATTRIBUTES_INIT { 0, 0, NULL }
Ronald Cron79cdd412024-02-20 17:19:28 +0100495
Jerry Yu1c105562022-07-10 06:32:38 +0000496/* Parser for pre_shared_key extension in client hello
497 * struct {
498 * opaque identity<1..2^16-1>;
499 * uint32 obfuscated_ticket_age;
500 * } PskIdentity;
501 *
502 * opaque PskBinderEntry<32..255>;
503 *
504 * struct {
505 * PskIdentity identities<7..2^16-1>;
506 * PskBinderEntry binders<33..2^16-1>;
507 * } OfferedPsks;
508 *
509 * struct {
510 * select (Handshake.msg_type) {
511 * case client_hello: OfferedPsks;
512 * ....
513 * };
514 * } PreSharedKeyExtension;
515 */
Jerry Yu6e74a7e2022-07-20 20:49:32 +0800516MBEDTLS_CHECK_RETURN_CRITICAL
Xiaokang Qian9f1747b2023-03-30 02:50:04 +0000517static int ssl_tls13_parse_pre_shared_key_ext(
518 mbedtls_ssl_context *ssl,
519 const unsigned char *pre_shared_key_ext,
520 const unsigned char *pre_shared_key_ext_end,
521 const unsigned char *ciphersuites,
Ronald Cron79cdd412024-02-20 17:19:28 +0100522 const unsigned char *ciphersuites_end,
523 struct psk_attributes *psk)
Jerry Yu1c105562022-07-10 06:32:38 +0000524{
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +0100525 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yu29d9faa2022-08-23 17:52:45 +0800526 const unsigned char *identities = pre_shared_key_ext;
Jerry Yu568ec252022-07-22 21:27:34 +0800527 const unsigned char *p_identity_len;
528 size_t identities_len;
Jerry Yu1c105562022-07-10 06:32:38 +0000529 const unsigned char *identities_end;
Jerry Yu568ec252022-07-22 21:27:34 +0800530 const unsigned char *binders;
531 const unsigned char *p_binder_len;
532 size_t binders_len;
Jerry Yu1c105562022-07-10 06:32:38 +0000533 const unsigned char *binders_end;
Jerry Yu96a2e362022-07-21 15:11:34 +0800534 int matched_identity = -1;
535 int identity_id = -1;
Jerry Yu1c105562022-07-10 06:32:38 +0000536
Gilles Peskine449bd832023-01-11 14:50:10 +0100537 MBEDTLS_SSL_DEBUG_BUF(3, "pre_shared_key extension",
538 pre_shared_key_ext,
539 pre_shared_key_ext_end - pre_shared_key_ext);
Jerry Yu1c105562022-07-10 06:32:38 +0000540
Jerry Yu96a2e362022-07-21 15:11:34 +0800541 /* identities_len 2 bytes
542 * identities_data >= 7 bytes
543 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100544 MBEDTLS_SSL_CHK_BUF_READ_PTR(identities, pre_shared_key_ext_end, 7 + 2);
545 identities_len = MBEDTLS_GET_UINT16_BE(identities, 0);
Jerry Yu568ec252022-07-22 21:27:34 +0800546 p_identity_len = identities + 2;
Gilles Peskine449bd832023-01-11 14:50:10 +0100547 MBEDTLS_SSL_CHK_BUF_READ_PTR(p_identity_len, pre_shared_key_ext_end,
548 identities_len);
Jerry Yu568ec252022-07-22 21:27:34 +0800549 identities_end = p_identity_len + identities_len;
Jerry Yu1c105562022-07-10 06:32:38 +0000550
Jerry Yu96a2e362022-07-21 15:11:34 +0800551 /* binders_len 2 bytes
552 * binders >= 33 bytes
553 */
Jerry Yu568ec252022-07-22 21:27:34 +0800554 binders = identities_end;
Gilles Peskine449bd832023-01-11 14:50:10 +0100555 MBEDTLS_SSL_CHK_BUF_READ_PTR(binders, pre_shared_key_ext_end, 33 + 2);
556 binders_len = MBEDTLS_GET_UINT16_BE(binders, 0);
Jerry Yu568ec252022-07-22 21:27:34 +0800557 p_binder_len = binders + 2;
Gilles Peskine449bd832023-01-11 14:50:10 +0100558 MBEDTLS_SSL_CHK_BUF_READ_PTR(p_binder_len, pre_shared_key_ext_end, binders_len);
Jerry Yu568ec252022-07-22 21:27:34 +0800559 binders_end = p_binder_len + binders_len;
Jerry Yu1c105562022-07-10 06:32:38 +0000560
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +0100561 ret = ssl->handshake->update_checksum(ssl, pre_shared_key_ext,
562 identities_end - pre_shared_key_ext);
563 if (0 != ret) {
564 MBEDTLS_SSL_DEBUG_RET(1, ("update_checksum"), ret);
565 return ret;
566 }
Jerry Yu96a2e362022-07-21 15:11:34 +0800567
Gilles Peskine449bd832023-01-11 14:50:10 +0100568 while (p_identity_len < identities_end && p_binder_len < binders_end) {
Jerry Yu1c105562022-07-10 06:32:38 +0000569 const unsigned char *identity;
Jerry Yu568ec252022-07-22 21:27:34 +0800570 size_t identity_len;
Jerry Yu82534862022-08-30 10:42:33 +0800571 uint32_t obfuscated_ticket_age;
Jerry Yu96a2e362022-07-21 15:11:34 +0800572 const unsigned char *binder;
Jerry Yu568ec252022-07-22 21:27:34 +0800573 size_t binder_len;
Ronald Cron89089cc2024-02-14 18:18:08 +0100574 int psk_ciphersuite_id;
575 psa_algorithm_t psk_hash_alg;
Ronald Croncf284562024-02-16 18:54:10 +0100576 int allowed_key_exchange_modes;
Ronald Cron74a16292024-02-23 17:07:41 +0100577
Jerry Yu82534862022-08-30 10:42:33 +0800578 mbedtls_ssl_session session;
Gilles Peskine449bd832023-01-11 14:50:10 +0100579 mbedtls_ssl_session_init(&session);
Jerry Yubb852022022-07-20 21:10:44 +0800580
Gilles Peskine449bd832023-01-11 14:50:10 +0100581 MBEDTLS_SSL_CHK_BUF_READ_PTR(p_identity_len, identities_end, 2 + 1 + 4);
582 identity_len = MBEDTLS_GET_UINT16_BE(p_identity_len, 0);
Jerry Yu568ec252022-07-22 21:27:34 +0800583 identity = p_identity_len + 2;
Gilles Peskine449bd832023-01-11 14:50:10 +0100584 MBEDTLS_SSL_CHK_BUF_READ_PTR(identity, identities_end, identity_len + 4);
585 obfuscated_ticket_age = MBEDTLS_GET_UINT32_BE(identity, identity_len);
Jerry Yu568ec252022-07-22 21:27:34 +0800586 p_identity_len += identity_len + 6;
Jerry Yu1c105562022-07-10 06:32:38 +0000587
Gilles Peskine449bd832023-01-11 14:50:10 +0100588 MBEDTLS_SSL_CHK_BUF_READ_PTR(p_binder_len, binders_end, 1 + 32);
Jerry Yu568ec252022-07-22 21:27:34 +0800589 binder_len = *p_binder_len;
590 binder = p_binder_len + 1;
Gilles Peskine449bd832023-01-11 14:50:10 +0100591 MBEDTLS_SSL_CHK_BUF_READ_PTR(binder, binders_end, binder_len);
Jerry Yu568ec252022-07-22 21:27:34 +0800592 p_binder_len += binder_len + 1;
Jerry Yu96a2e362022-07-21 15:11:34 +0800593
Jerry Yu96a2e362022-07-21 15:11:34 +0800594 identity_id++;
Gilles Peskine449bd832023-01-11 14:50:10 +0100595 if (matched_identity != -1) {
Jerry Yu1c105562022-07-10 06:32:38 +0000596 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +0100597 }
Jerry Yu1c105562022-07-10 06:32:38 +0000598
Jerry Yu96a2e362022-07-21 15:11:34 +0800599 ret = ssl_tls13_offered_psks_check_identity_match(
Gilles Peskine449bd832023-01-11 14:50:10 +0100600 ssl, identity, identity_len, obfuscated_ticket_age,
Ronald Cron79cdd412024-02-20 17:19:28 +0100601 &psk->type, &session);
Ronald Cronfbae94a2023-12-05 18:15:14 +0100602 if (ret != SSL_TLS1_3_PSK_IDENTITY_MATCH) {
Jerry Yu96a2e362022-07-21 15:11:34 +0800603 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +0100604 }
Jerry Yu96a2e362022-07-21 15:11:34 +0800605
Gilles Peskine449bd832023-01-11 14:50:10 +0100606 MBEDTLS_SSL_DEBUG_MSG(4, ("found matched identity"));
Ronald Cron89089cc2024-02-14 18:18:08 +0100607
Ronald Cron79cdd412024-02-20 17:19:28 +0100608 switch (psk->type) {
Jerry Yu0baf9072022-08-25 11:21:04 +0800609 case MBEDTLS_SSL_TLS1_3_PSK_EXTERNAL:
Ronald Cron89089cc2024-02-14 18:18:08 +0100610 psk_ciphersuite_id = 0;
611 psk_hash_alg = PSA_ALG_SHA_256;
Ronald Croncf284562024-02-16 18:54:10 +0100612 allowed_key_exchange_modes =
613 MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK_ALL;
Jerry Yu0baf9072022-08-25 11:21:04 +0800614 break;
Jerry Yu82534862022-08-30 10:42:33 +0800615#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Ronald Cronf7e99162024-02-14 16:04:02 +0100616 case MBEDTLS_SSL_TLS1_3_PSK_RESUMPTION:
Ronald Cron89089cc2024-02-14 18:18:08 +0100617 psk_ciphersuite_id = session.ciphersuite;
618 psk_hash_alg = PSA_ALG_NONE;
Ronald Croncf284562024-02-16 18:54:10 +0100619 ssl->session_negotiate->ticket_flags = session.ticket_flags;
620 allowed_key_exchange_modes =
621 session.ticket_flags &
622 MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK_ALL;
Jerry Yu0baf9072022-08-25 11:21:04 +0800623 break;
Ronald Cronf7e99162024-02-14 16:04:02 +0100624#endif
Jerry Yu0baf9072022-08-25 11:21:04 +0800625 default:
Gilles Peskine449bd832023-01-11 14:50:10 +0100626 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Jerry Yu0baf9072022-08-25 11:21:04 +0800627 }
Ronald Cron89089cc2024-02-14 18:18:08 +0100628
Ronald Cron79cdd412024-02-20 17:19:28 +0100629 psk->key_exchange_mode = MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_NONE;
630
Ronald Croncf284562024-02-16 18:54:10 +0100631 if ((allowed_key_exchange_modes &
632 MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK_EPHEMERAL) &&
633 ssl_tls13_key_exchange_is_psk_ephemeral_available(ssl)) {
Ronald Cron79cdd412024-02-20 17:19:28 +0100634 psk->key_exchange_mode = MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK_EPHEMERAL;
Ronald Croncf284562024-02-16 18:54:10 +0100635 } else if ((allowed_key_exchange_modes &
636 MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK) &&
637 ssl_tls13_key_exchange_is_psk_available(ssl)) {
Ronald Cron79cdd412024-02-20 17:19:28 +0100638 psk->key_exchange_mode = MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK;
Ronald Croncf284562024-02-16 18:54:10 +0100639 }
640
Ronald Cron79cdd412024-02-20 17:19:28 +0100641 if (psk->key_exchange_mode == MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_NONE) {
Ronald Croncf284562024-02-16 18:54:10 +0100642 MBEDTLS_SSL_DEBUG_MSG(3, ("No suitable PSK key exchange mode"));
643 continue;
644 }
645
Ronald Cron89089cc2024-02-14 18:18:08 +0100646 ssl_tls13_select_ciphersuite(ssl, ciphersuites, ciphersuites_end,
647 psk_ciphersuite_id, psk_hash_alg,
Ronald Cron74a16292024-02-23 17:07:41 +0100648 &psk->ciphersuite_info);
Ronald Cron89089cc2024-02-14 18:18:08 +0100649
Ronald Cron74a16292024-02-23 17:07:41 +0100650 if (psk->ciphersuite_info == NULL) {
Ronald Cron89089cc2024-02-14 18:18:08 +0100651#if defined(MBEDTLS_SSL_SESSION_TICKETS)
652 mbedtls_ssl_session_free(&session);
653#endif
654 /*
655 * We consider finding a ciphersuite suitable for the PSK as part
656 * of the validation of its binder. Thus if we do not find one, we
657 * abort the handshake with a decrypt_error alert.
658 */
Jerry Yuf35ba382022-08-23 17:58:26 +0800659 MBEDTLS_SSL_PEND_FATAL_ALERT(
660 MBEDTLS_SSL_ALERT_MSG_DECRYPT_ERROR,
Gilles Peskine449bd832023-01-11 14:50:10 +0100661 MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE);
Ronald Cron89089cc2024-02-14 18:18:08 +0100662 return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
Jerry Yu5725f1c2022-08-21 17:27:16 +0800663 }
664
Jerry Yu96a2e362022-07-21 15:11:34 +0800665 ret = ssl_tls13_offered_psks_check_binder_match(
Ronald Cron79cdd412024-02-20 17:19:28 +0100666 ssl, binder, binder_len, psk->type,
Ronald Cron74a16292024-02-23 17:07:41 +0100667 mbedtls_md_psa_alg_from_type((mbedtls_md_type_t) psk->ciphersuite_info->mac));
Ronald Cron6e311272023-12-05 17:57:01 +0100668 if (ret != SSL_TLS1_3_BINDER_MATCH) {
Jerry Yuc5a23a02022-08-25 10:51:44 +0800669 /* For security reasons, the handshake should be aborted when we
670 * fail to validate a binder value. See RFC 8446 section 4.2.11.2
671 * and appendix E.6. */
Jerry Yu82534862022-08-30 10:42:33 +0800672#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Gilles Peskine449bd832023-01-11 14:50:10 +0100673 mbedtls_ssl_session_free(&session);
Jerry Yu82534862022-08-30 10:42:33 +0800674#endif
Gilles Peskine449bd832023-01-11 14:50:10 +0100675 MBEDTLS_SSL_DEBUG_MSG(3, ("Invalid binder."));
Xiaokang Qian9f1747b2023-03-30 02:50:04 +0000676 MBEDTLS_SSL_DEBUG_RET(
677 1, "ssl_tls13_offered_psks_check_binder_match", ret);
Jerry Yu96a2e362022-07-21 15:11:34 +0800678 MBEDTLS_SSL_PEND_FATAL_ALERT(
679 MBEDTLS_SSL_ALERT_MSG_DECRYPT_ERROR,
Gilles Peskine449bd832023-01-11 14:50:10 +0100680 MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE);
681 return ret;
Jerry Yu96a2e362022-07-21 15:11:34 +0800682 }
Jerry Yu96a2e362022-07-21 15:11:34 +0800683
684 matched_identity = identity_id;
Jerry Yu5725f1c2022-08-21 17:27:16 +0800685
Jerry Yu82534862022-08-30 10:42:33 +0800686#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Ronald Cron79cdd412024-02-20 17:19:28 +0100687 if (psk->type == MBEDTLS_SSL_TLS1_3_PSK_RESUMPTION) {
Gilles Peskine449bd832023-01-11 14:50:10 +0100688 ret = ssl_tls13_session_copy_ticket(ssl->session_negotiate,
689 &session);
690 mbedtls_ssl_session_free(&session);
691 if (ret != 0) {
692 return ret;
693 }
Jerry Yu82534862022-08-30 10:42:33 +0800694 }
695#endif /* MBEDTLS_SSL_SESSION_TICKETS */
Jerry Yu1c105562022-07-10 06:32:38 +0000696 }
697
Gilles Peskine449bd832023-01-11 14:50:10 +0100698 if (p_identity_len != identities_end || p_binder_len != binders_end) {
699 MBEDTLS_SSL_DEBUG_MSG(3, ("pre_shared_key extension decode error"));
700 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR,
701 MBEDTLS_ERR_SSL_DECODE_ERROR);
702 return MBEDTLS_ERR_SSL_DECODE_ERROR;
Jerry Yu1c105562022-07-10 06:32:38 +0000703 }
704
Jerry Yu6f1db3f2022-07-22 23:05:59 +0800705 /* Update the handshake transcript with the binder list. */
Xiaokang Qian9f1747b2023-03-30 02:50:04 +0000706 ret = ssl->handshake->update_checksum(
707 ssl, identities_end, (size_t) (binders_end - identities_end));
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +0100708 if (0 != ret) {
709 MBEDTLS_SSL_DEBUG_RET(1, ("update_checksum"), ret);
710 return ret;
711 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100712 if (matched_identity == -1) {
Ronald Croncf284562024-02-16 18:54:10 +0100713 MBEDTLS_SSL_DEBUG_MSG(3, ("No usable PSK or ticket."));
Gilles Peskine449bd832023-01-11 14:50:10 +0100714 return MBEDTLS_ERR_SSL_UNKNOWN_IDENTITY;
Jerry Yu1c105562022-07-10 06:32:38 +0000715 }
716
Gilles Peskine449bd832023-01-11 14:50:10 +0100717 ssl->handshake->selected_identity = (uint16_t) matched_identity;
718 MBEDTLS_SSL_DEBUG_MSG(3, ("Pre shared key found"));
Jerry Yu1c105562022-07-10 06:32:38 +0000719
Gilles Peskine449bd832023-01-11 14:50:10 +0100720 return 0;
Jerry Yu1c105562022-07-10 06:32:38 +0000721}
Jerry Yu032b15ce2022-07-11 06:10:03 +0000722
723/*
724 * struct {
725 * select ( Handshake.msg_type ) {
726 * ....
727 * case server_hello:
728 * uint16 selected_identity;
729 * }
730 * } PreSharedKeyExtension;
731 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100732static int ssl_tls13_write_server_pre_shared_key_ext(mbedtls_ssl_context *ssl,
733 unsigned char *buf,
734 unsigned char *end,
735 size_t *olen)
Jerry Yu032b15ce2022-07-11 06:10:03 +0000736{
Gilles Peskine449bd832023-01-11 14:50:10 +0100737 unsigned char *p = (unsigned char *) buf;
Jerry Yu032b15ce2022-07-11 06:10:03 +0000738
739 *olen = 0;
740
David Horstmann21b89762022-10-06 18:34:28 +0100741 int not_using_psk = 0;
Jerry Yu032b15ce2022-07-11 06:10:03 +0000742#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +0100743 not_using_psk = (mbedtls_svc_key_id_is_null(ssl->handshake->psk_opaque));
Jerry Yu032b15ce2022-07-11 06:10:03 +0000744#else
Gilles Peskine449bd832023-01-11 14:50:10 +0100745 not_using_psk = (ssl->handshake->psk == NULL);
Jerry Yu032b15ce2022-07-11 06:10:03 +0000746#endif
Gilles Peskine449bd832023-01-11 14:50:10 +0100747 if (not_using_psk) {
Jerry Yu032b15ce2022-07-11 06:10:03 +0000748 /* We shouldn't have called this extension writer unless we've
749 * chosen to use a PSK. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100750 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Jerry Yu032b15ce2022-07-11 06:10:03 +0000751 }
752
Gilles Peskine449bd832023-01-11 14:50:10 +0100753 MBEDTLS_SSL_DEBUG_MSG(3, ("server hello, adding pre_shared_key extension"));
754 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 6);
Jerry Yu032b15ce2022-07-11 06:10:03 +0000755
Gilles Peskine449bd832023-01-11 14:50:10 +0100756 MBEDTLS_PUT_UINT16_BE(MBEDTLS_TLS_EXT_PRE_SHARED_KEY, p, 0);
757 MBEDTLS_PUT_UINT16_BE(2, p, 2);
Jerry Yu032b15ce2022-07-11 06:10:03 +0000758
Gilles Peskine449bd832023-01-11 14:50:10 +0100759 MBEDTLS_PUT_UINT16_BE(ssl->handshake->selected_identity, p, 4);
Jerry Yu032b15ce2022-07-11 06:10:03 +0000760
761 *olen = 6;
762
Gilles Peskine449bd832023-01-11 14:50:10 +0100763 MBEDTLS_SSL_DEBUG_MSG(4, ("sent selected_identity: %u",
764 ssl->handshake->selected_identity));
Jerry Yu032b15ce2022-07-11 06:10:03 +0000765
Gilles Peskine449bd832023-01-11 14:50:10 +0100766 mbedtls_ssl_tls13_set_hs_sent_ext_mask(ssl, MBEDTLS_TLS_EXT_PRE_SHARED_KEY);
Jerry Yub95dd362022-11-08 21:19:34 +0800767
Gilles Peskine449bd832023-01-11 14:50:10 +0100768 return 0;
Jerry Yu032b15ce2022-07-11 06:10:03 +0000769}
770
Ronald Cron41a443a2022-10-04 16:38:25 +0200771#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED */
Jerry Yue19e3b92022-07-08 12:04:51 +0000772
XiaokangQian7807f9f2022-02-15 10:04:37 +0000773/* From RFC 8446:
774 * struct {
XiaokangQiancfd925f2022-04-14 07:10:37 +0000775 * ProtocolVersion versions<2..254>;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000776 * } SupportedVersions;
777 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200778MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100779static int ssl_tls13_parse_supported_versions_ext(mbedtls_ssl_context *ssl,
780 const unsigned char *buf,
781 const unsigned char *end)
XiaokangQian7807f9f2022-02-15 10:04:37 +0000782{
XiaokangQian7807f9f2022-02-15 10:04:37 +0000783 const unsigned char *p = buf;
XiaokangQian8f9dfe42022-04-15 02:52:39 +0000784 size_t versions_len;
XiaokangQian4080a7f2022-04-11 09:55:18 +0000785 const unsigned char *versions_end;
XiaokangQian0a1b54e2022-04-21 03:01:38 +0000786 uint16_t tls_version;
Ronald Cron5af4c7f2023-03-07 20:46:59 +0100787 int found_supported_version = 0;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000788
Gilles Peskine449bd832023-01-11 14:50:10 +0100789 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, 1);
XiaokangQian4080a7f2022-04-11 09:55:18 +0000790 versions_len = p[0];
XiaokangQian7807f9f2022-02-15 10:04:37 +0000791 p += 1;
792
Gilles Peskine449bd832023-01-11 14:50:10 +0100793 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, versions_len);
XiaokangQian4080a7f2022-04-11 09:55:18 +0000794 versions_end = p + versions_len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100795 while (p < versions_end) {
796 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, versions_end, 2);
797 tls_version = mbedtls_ssl_read_version(p, ssl->conf->transport);
XiaokangQianb67384d2022-04-19 00:02:38 +0000798 p += 2;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000799
Ronald Cron3bd2b022023-04-03 16:45:39 +0200800 if (MBEDTLS_SSL_VERSION_TLS1_3 == tls_version) {
Ronald Cron5af4c7f2023-03-07 20:46:59 +0100801 found_supported_version = 1;
802 break;
803 }
804
Ronald Cron3bd2b022023-04-03 16:45:39 +0200805 if ((MBEDTLS_SSL_VERSION_TLS1_2 == tls_version) &&
806 mbedtls_ssl_conf_is_tls12_enabled(ssl->conf)) {
Ronald Cron5af4c7f2023-03-07 20:46:59 +0100807 found_supported_version = 1;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000808 break;
809 }
XiaokangQian7807f9f2022-02-15 10:04:37 +0000810 }
811
Ronald Cron5af4c7f2023-03-07 20:46:59 +0100812 if (!found_supported_version) {
813 MBEDTLS_SSL_DEBUG_MSG(1, ("No supported version found."));
XiaokangQian7807f9f2022-02-15 10:04:37 +0000814
Gilles Peskine449bd832023-01-11 14:50:10 +0100815 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_PROTOCOL_VERSION,
816 MBEDTLS_ERR_SSL_BAD_PROTOCOL_VERSION);
817 return MBEDTLS_ERR_SSL_BAD_PROTOCOL_VERSION;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000818 }
819
Ronald Cron5af4c7f2023-03-07 20:46:59 +0100820 MBEDTLS_SSL_DEBUG_MSG(1, ("Negotiated version: [%04x]",
Gilles Peskine449bd832023-01-11 14:50:10 +0100821 (unsigned int) tls_version));
XiaokangQian7807f9f2022-02-15 10:04:37 +0000822
Ronald Cron5af4c7f2023-03-07 20:46:59 +0100823 return (int) tls_version;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000824}
825
Przemek Stekiel8c0a9532023-06-15 16:48:19 +0200826#if defined(PSA_WANT_ALG_ECDH) || defined(PSA_WANT_ALG_FFDH)
XiaokangQiane8ff3502022-04-22 02:34:40 +0000827/*
XiaokangQian7807f9f2022-02-15 10:04:37 +0000828 *
829 * From RFC 8446:
830 * enum {
831 * ... (0xFFFF)
832 * } NamedGroup;
833 * struct {
834 * NamedGroup named_group_list<2..2^16-1>;
835 * } NamedGroupList;
836 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200837MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100838static int ssl_tls13_parse_supported_groups_ext(mbedtls_ssl_context *ssl,
839 const unsigned char *buf,
840 const unsigned char *end)
XiaokangQian7807f9f2022-02-15 10:04:37 +0000841{
XiaokangQian7807f9f2022-02-15 10:04:37 +0000842 const unsigned char *p = buf;
XiaokangQian84823772022-04-19 07:57:30 +0000843 size_t named_group_list_len;
XiaokangQian8f9dfe42022-04-15 02:52:39 +0000844 const unsigned char *named_group_list_end;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000845
Gilles Peskine449bd832023-01-11 14:50:10 +0100846 MBEDTLS_SSL_DEBUG_BUF(3, "supported_groups extension", p, end - buf);
847 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, 2);
848 named_group_list_len = MBEDTLS_GET_UINT16_BE(p, 0);
XiaokangQian7807f9f2022-02-15 10:04:37 +0000849 p += 2;
Gilles Peskine449bd832023-01-11 14:50:10 +0100850 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, named_group_list_len);
XiaokangQian8f9dfe42022-04-15 02:52:39 +0000851 named_group_list_end = p + named_group_list_len;
XiaokangQian4e8cd7b2022-04-21 09:48:09 +0000852 ssl->handshake->hrr_selected_group = 0;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000853
Gilles Peskine449bd832023-01-11 14:50:10 +0100854 while (p < named_group_list_end) {
XiaokangQian08037552022-04-20 07:16:41 +0000855 uint16_t named_group;
Gilles Peskine449bd832023-01-11 14:50:10 +0100856 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, named_group_list_end, 2);
857 named_group = MBEDTLS_GET_UINT16_BE(p, 0);
XiaokangQian08037552022-04-20 07:16:41 +0000858 p += 2;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000859
Gilles Peskine449bd832023-01-11 14:50:10 +0100860 MBEDTLS_SSL_DEBUG_MSG(2,
861 ("got named group: %s(%04x)",
862 mbedtls_ssl_named_group_to_str(named_group),
863 named_group));
XiaokangQian08037552022-04-20 07:16:41 +0000864
Gilles Peskine449bd832023-01-11 14:50:10 +0100865 if (!mbedtls_ssl_named_group_is_offered(ssl, named_group) ||
866 !mbedtls_ssl_named_group_is_supported(named_group) ||
867 ssl->handshake->hrr_selected_group != 0) {
XiaokangQian08037552022-04-20 07:16:41 +0000868 continue;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000869 }
870
Gilles Peskine449bd832023-01-11 14:50:10 +0100871 MBEDTLS_SSL_DEBUG_MSG(2,
872 ("add named group %s(%04x) into received list.",
873 mbedtls_ssl_named_group_to_str(named_group),
874 named_group));
Jerry Yuc1be19f2022-04-23 16:11:39 +0800875
XiaokangQian4e8cd7b2022-04-21 09:48:09 +0000876 ssl->handshake->hrr_selected_group = named_group;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000877 }
878
Gilles Peskine449bd832023-01-11 14:50:10 +0100879 return 0;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000880
881}
Przemek Stekiel8c0a9532023-06-15 16:48:19 +0200882#endif /* PSA_WANT_ALG_ECDH || PSA_WANT_ALG_FFDH */
XiaokangQian7807f9f2022-02-15 10:04:37 +0000883
XiaokangQian08037552022-04-20 07:16:41 +0000884#define SSL_TLS1_3_PARSE_KEY_SHARES_EXT_NO_MATCH 1
885
Valerio Settic9ae8622023-07-25 11:23:50 +0200886#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_EPHEMERAL_ENABLED)
XiaokangQian7807f9f2022-02-15 10:04:37 +0000887/*
888 * ssl_tls13_parse_key_shares_ext() verifies whether the information in the
Xiaokang Qian9f1747b2023-03-30 02:50:04 +0000889 * extension is correct and stores the first acceptable key share and its
890 * associated group.
XiaokangQian7807f9f2022-02-15 10:04:37 +0000891 *
892 * Possible return values are:
893 * - 0: Successful processing of the client provided key share extension.
Xiaokang Qian9f1747b2023-03-30 02:50:04 +0000894 * - SSL_TLS1_3_PARSE_KEY_SHARES_EXT_NO_MATCH: The key shares provided by
895 * the client does not match a group supported by the server. A
896 * HelloRetryRequest will be needed.
XiaokangQiane8ff3502022-04-22 02:34:40 +0000897 * - A negative value for fatal errors.
Jerry Yuc1be19f2022-04-23 16:11:39 +0800898 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200899MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100900static int ssl_tls13_parse_key_shares_ext(mbedtls_ssl_context *ssl,
901 const unsigned char *buf,
902 const unsigned char *end)
XiaokangQian7807f9f2022-02-15 10:04:37 +0000903{
XiaokangQianb67384d2022-04-19 00:02:38 +0000904 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000905 unsigned char const *p = buf;
XiaokangQian8f9dfe42022-04-15 02:52:39 +0000906 unsigned char const *client_shares_end;
Jerry Yuc1be19f2022-04-23 16:11:39 +0800907 size_t client_shares_len;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000908
909 /* From RFC 8446:
910 *
911 * struct {
912 * KeyShareEntry client_shares<0..2^16-1>;
913 * } KeyShareClientHello;
914 *
915 */
916
Gilles Peskine449bd832023-01-11 14:50:10 +0100917 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, 2);
918 client_shares_len = MBEDTLS_GET_UINT16_BE(p, 0);
XiaokangQian7807f9f2022-02-15 10:04:37 +0000919 p += 2;
Gilles Peskine449bd832023-01-11 14:50:10 +0100920 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, client_shares_len);
XiaokangQian7807f9f2022-02-15 10:04:37 +0000921
922 ssl->handshake->offered_group_id = 0;
XiaokangQian8f9dfe42022-04-15 02:52:39 +0000923 client_shares_end = p + client_shares_len;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000924
925 /* We try to find a suitable key share entry and copy it to the
926 * handshake context. Later, we have to find out whether we can do
927 * something with the provided key share or whether we have to
XiaokangQianc5763b52022-04-02 03:34:37 +0000928 * dismiss it and send a HelloRetryRequest message.
929 */
XiaokangQian7807f9f2022-02-15 10:04:37 +0000930
Gilles Peskine449bd832023-01-11 14:50:10 +0100931 while (p < client_shares_end) {
XiaokangQian9b5d04b2022-04-10 10:20:43 +0000932 uint16_t group;
Jerry Yuc1be19f2022-04-23 16:11:39 +0800933 size_t key_exchange_len;
Jerry Yu086edc22022-05-05 10:50:38 +0800934 const unsigned char *key_exchange;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000935
936 /*
937 * struct {
938 * NamedGroup group;
939 * opaque key_exchange<1..2^16-1>;
940 * } KeyShareEntry;
941 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100942 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, client_shares_end, 4);
943 group = MBEDTLS_GET_UINT16_BE(p, 0);
944 key_exchange_len = MBEDTLS_GET_UINT16_BE(p, 2);
Jerry Yuc1be19f2022-04-23 16:11:39 +0800945 p += 4;
Jerry Yu086edc22022-05-05 10:50:38 +0800946 key_exchange = p;
Gilles Peskine449bd832023-01-11 14:50:10 +0100947 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, client_shares_end, key_exchange_len);
Jerry Yu086edc22022-05-05 10:50:38 +0800948 p += key_exchange_len;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000949
950 /* Continue parsing even if we have already found a match,
XiaokangQianc5763b52022-04-02 03:34:37 +0000951 * for input validation purposes.
952 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100953 if (!mbedtls_ssl_named_group_is_offered(ssl, group) ||
954 !mbedtls_ssl_named_group_is_supported(group) ||
955 ssl->handshake->offered_group_id != 0) {
XiaokangQian060d8672022-04-21 09:24:56 +0000956 continue;
957 }
XiaokangQian060d8672022-04-21 09:24:56 +0000958
XiaokangQian7807f9f2022-02-15 10:04:37 +0000959 /*
Przemek Stekielc89f3ea2023-05-18 15:45:53 +0200960 * ECDHE and FFDHE groups are supported
XiaokangQian7807f9f2022-02-15 10:04:37 +0000961 */
Przemek Stekielc89f3ea2023-05-18 15:45:53 +0200962 if (mbedtls_ssl_tls13_named_group_is_ecdhe(group) ||
Przemek Stekield5f79e72023-06-29 09:08:43 +0200963 mbedtls_ssl_tls13_named_group_is_ffdh(group)) {
Przemek Stekielc89f3ea2023-05-18 15:45:53 +0200964 MBEDTLS_SSL_DEBUG_MSG(2, ("ECDH/FFDH group: %s (%04x)",
Gilles Peskine449bd832023-01-11 14:50:10 +0100965 mbedtls_ssl_named_group_to_str(group),
966 group));
Przemek Stekiel7ac93be2023-07-04 10:02:38 +0200967 ret = mbedtls_ssl_tls13_read_public_xxdhe_share(
Gilles Peskine449bd832023-01-11 14:50:10 +0100968 ssl, key_exchange - 2, key_exchange_len + 2);
969 if (ret != 0) {
970 return ret;
971 }
XiaokangQian4e8cd7b2022-04-21 09:48:09 +0000972
Gilles Peskine449bd832023-01-11 14:50:10 +0100973 } else {
974 MBEDTLS_SSL_DEBUG_MSG(4, ("Unrecognized NamedGroup %u",
975 (unsigned) group));
XiaokangQian7807f9f2022-02-15 10:04:37 +0000976 continue;
977 }
978
XiaokangQian9b5d04b2022-04-10 10:20:43 +0000979 ssl->handshake->offered_group_id = group;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000980 }
981
Jerry Yuc1be19f2022-04-23 16:11:39 +0800982
Gilles Peskine449bd832023-01-11 14:50:10 +0100983 if (ssl->handshake->offered_group_id == 0) {
984 MBEDTLS_SSL_DEBUG_MSG(1, ("no matching key share"));
985 return SSL_TLS1_3_PARSE_KEY_SHARES_EXT_NO_MATCH;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000986 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100987 return 0;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000988}
Valerio Settic9ae8622023-07-25 11:23:50 +0200989#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_EPHEMERAL_ENABLED */
XiaokangQian7807f9f2022-02-15 10:04:37 +0000990
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200991MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100992static int ssl_tls13_client_hello_has_exts(mbedtls_ssl_context *ssl,
993 int exts_mask)
XiaokangQian7807f9f2022-02-15 10:04:37 +0000994{
Jerry Yu0c354a22022-08-29 15:25:36 +0800995 int masked = ssl->handshake->received_extensions & exts_mask;
Gilles Peskine449bd832023-01-11 14:50:10 +0100996 return masked == exts_mask;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000997}
998
Jerry Yue5991322022-11-07 14:03:44 +0800999#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001000MBEDTLS_CHECK_RETURN_CRITICAL
XiaokangQianb67384d2022-04-19 00:02:38 +00001001static int ssl_tls13_client_hello_has_exts_for_ephemeral_key_exchange(
Gilles Peskine449bd832023-01-11 14:50:10 +01001002 mbedtls_ssl_context *ssl)
XiaokangQian7807f9f2022-02-15 10:04:37 +00001003{
Gilles Peskine449bd832023-01-11 14:50:10 +01001004 return ssl_tls13_client_hello_has_exts(
1005 ssl,
1006 MBEDTLS_SSL_EXT_MASK(SUPPORTED_GROUPS) |
1007 MBEDTLS_SSL_EXT_MASK(KEY_SHARE) |
1008 MBEDTLS_SSL_EXT_MASK(SIG_ALG));
XiaokangQian7807f9f2022-02-15 10:04:37 +00001009}
Jerry Yue5991322022-11-07 14:03:44 +08001010#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED */
XiaokangQian7807f9f2022-02-15 10:04:37 +00001011
Jerry Yue5991322022-11-07 14:03:44 +08001012#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK_ENABLED)
Jerry Yu77f01482022-07-11 07:03:24 +00001013MBEDTLS_CHECK_RETURN_CRITICAL
1014static int ssl_tls13_client_hello_has_exts_for_psk_key_exchange(
Gilles Peskine449bd832023-01-11 14:50:10 +01001015 mbedtls_ssl_context *ssl)
Jerry Yu77f01482022-07-11 07:03:24 +00001016{
Gilles Peskine449bd832023-01-11 14:50:10 +01001017 return ssl_tls13_client_hello_has_exts(
1018 ssl,
1019 MBEDTLS_SSL_EXT_MASK(PRE_SHARED_KEY) |
1020 MBEDTLS_SSL_EXT_MASK(PSK_KEY_EXCHANGE_MODES));
Jerry Yu77f01482022-07-11 07:03:24 +00001021}
Jerry Yue5991322022-11-07 14:03:44 +08001022#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK_ENABLED */
Jerry Yu77f01482022-07-11 07:03:24 +00001023
Jerry Yue5991322022-11-07 14:03:44 +08001024#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK_EPHEMERAL_ENABLED)
Jerry Yu77f01482022-07-11 07:03:24 +00001025MBEDTLS_CHECK_RETURN_CRITICAL
1026static int ssl_tls13_client_hello_has_exts_for_psk_ephemeral_key_exchange(
Gilles Peskine449bd832023-01-11 14:50:10 +01001027 mbedtls_ssl_context *ssl)
Jerry Yu77f01482022-07-11 07:03:24 +00001028{
Gilles Peskine449bd832023-01-11 14:50:10 +01001029 return ssl_tls13_client_hello_has_exts(
1030 ssl,
1031 MBEDTLS_SSL_EXT_MASK(SUPPORTED_GROUPS) |
1032 MBEDTLS_SSL_EXT_MASK(KEY_SHARE) |
1033 MBEDTLS_SSL_EXT_MASK(PRE_SHARED_KEY) |
1034 MBEDTLS_SSL_EXT_MASK(PSK_KEY_EXCHANGE_MODES));
Jerry Yu77f01482022-07-11 07:03:24 +00001035}
Jerry Yue5991322022-11-07 14:03:44 +08001036#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK_EPHEMERAL_ENABLED */
Jerry Yu77f01482022-07-11 07:03:24 +00001037
Pengyu Lv306a01d2023-02-02 16:35:47 +08001038#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED)
1039MBEDTLS_CHECK_RETURN_CRITICAL
Pengyu Lvbc4aab72023-12-01 15:37:24 +08001040static int ssl_tls13_key_exchange_is_psk_available(mbedtls_ssl_context *ssl)
Pengyu Lv306a01d2023-02-02 16:35:47 +08001041{
Jerry Yue5991322022-11-07 14:03:44 +08001042#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK_ENABLED)
Ronald Crone8c162d2024-02-21 10:15:44 +01001043 return mbedtls_ssl_conf_tls13_is_psk_enabled(ssl) &&
Pengyu Lvb2cfafb2023-11-14 13:56:13 +08001044 mbedtls_ssl_tls13_is_psk_supported(ssl) &&
Gilles Peskine449bd832023-01-11 14:50:10 +01001045 ssl_tls13_client_hello_has_exts_for_psk_key_exchange(ssl);
XiaokangQian7807f9f2022-02-15 10:04:37 +00001046#else
1047 ((void) ssl);
Gilles Peskine449bd832023-01-11 14:50:10 +01001048 return 0;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001049#endif
Jerry Yu77f01482022-07-11 07:03:24 +00001050}
XiaokangQian7807f9f2022-02-15 10:04:37 +00001051
Jerry Yu77f01482022-07-11 07:03:24 +00001052MBEDTLS_CHECK_RETURN_CRITICAL
Pengyu Lvbc4aab72023-12-01 15:37:24 +08001053static int ssl_tls13_key_exchange_is_psk_ephemeral_available(mbedtls_ssl_context *ssl)
Jerry Yu77f01482022-07-11 07:03:24 +00001054{
Jerry Yue5991322022-11-07 14:03:44 +08001055#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK_EPHEMERAL_ENABLED)
Ronald Crone8c162d2024-02-21 10:15:44 +01001056 return mbedtls_ssl_conf_tls13_is_psk_ephemeral_enabled(ssl) &&
Pengyu Lvb2cfafb2023-11-14 13:56:13 +08001057 mbedtls_ssl_tls13_is_psk_ephemeral_supported(ssl) &&
Gilles Peskine449bd832023-01-11 14:50:10 +01001058 ssl_tls13_client_hello_has_exts_for_psk_ephemeral_key_exchange(ssl);
Jerry Yu77f01482022-07-11 07:03:24 +00001059#else
1060 ((void) ssl);
Gilles Peskine449bd832023-01-11 14:50:10 +01001061 return 0;
Jerry Yu77f01482022-07-11 07:03:24 +00001062#endif
XiaokangQian7807f9f2022-02-15 10:04:37 +00001063}
1064#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED */
1065
1066MBEDTLS_CHECK_RETURN_CRITICAL
1067static int ssl_tls13_key_exchange_is_ephemeral_available(mbedtls_ssl_context *ssl)
1068{
1069#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED)
1070 return mbedtls_ssl_conf_tls13_is_ephemeral_enabled(ssl) &&
1071 ssl_tls13_client_hello_has_exts_for_ephemeral_key_exchange(ssl);
1072#else
1073 ((void) ssl);
1074 return 0;
1075#endif
1076}
1077
XiaokangQian81802f42022-06-10 13:25:22 +00001078#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
Ronald Cron928cbd32022-10-04 16:14:26 +02001079 defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED)
Ronald Cron67ea2542022-09-15 17:34:42 +02001080
1081#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01001082static psa_algorithm_t ssl_tls13_iana_sig_alg_to_psa_alg(uint16_t sig_alg)
Ronald Cron67ea2542022-09-15 17:34:42 +02001083{
Gilles Peskine449bd832023-01-11 14:50:10 +01001084 switch (sig_alg) {
Ronald Cron67ea2542022-09-15 17:34:42 +02001085 case MBEDTLS_TLS1_3_SIG_ECDSA_SECP256R1_SHA256:
Gilles Peskine449bd832023-01-11 14:50:10 +01001086 return PSA_ALG_ECDSA(PSA_ALG_SHA_256);
Ronald Cron67ea2542022-09-15 17:34:42 +02001087 case MBEDTLS_TLS1_3_SIG_ECDSA_SECP384R1_SHA384:
Gilles Peskine449bd832023-01-11 14:50:10 +01001088 return PSA_ALG_ECDSA(PSA_ALG_SHA_384);
Ronald Cron67ea2542022-09-15 17:34:42 +02001089 case MBEDTLS_TLS1_3_SIG_ECDSA_SECP521R1_SHA512:
Gilles Peskine449bd832023-01-11 14:50:10 +01001090 return PSA_ALG_ECDSA(PSA_ALG_SHA_512);
Ronald Cron67ea2542022-09-15 17:34:42 +02001091 case MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA256:
Gilles Peskine449bd832023-01-11 14:50:10 +01001092 return PSA_ALG_RSA_PSS(PSA_ALG_SHA_256);
Ronald Cron67ea2542022-09-15 17:34:42 +02001093 case MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA384:
Gilles Peskine449bd832023-01-11 14:50:10 +01001094 return PSA_ALG_RSA_PSS(PSA_ALG_SHA_384);
Ronald Cron67ea2542022-09-15 17:34:42 +02001095 case MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA512:
Gilles Peskine449bd832023-01-11 14:50:10 +01001096 return PSA_ALG_RSA_PSS(PSA_ALG_SHA_512);
Ronald Cron67ea2542022-09-15 17:34:42 +02001097 case MBEDTLS_TLS1_3_SIG_RSA_PKCS1_SHA256:
Gilles Peskine449bd832023-01-11 14:50:10 +01001098 return PSA_ALG_RSA_PKCS1V15_SIGN(PSA_ALG_SHA_256);
Ronald Cron67ea2542022-09-15 17:34:42 +02001099 case MBEDTLS_TLS1_3_SIG_RSA_PKCS1_SHA384:
Gilles Peskine449bd832023-01-11 14:50:10 +01001100 return PSA_ALG_RSA_PKCS1V15_SIGN(PSA_ALG_SHA_384);
Ronald Cron67ea2542022-09-15 17:34:42 +02001101 case MBEDTLS_TLS1_3_SIG_RSA_PKCS1_SHA512:
Gilles Peskine449bd832023-01-11 14:50:10 +01001102 return PSA_ALG_RSA_PKCS1V15_SIGN(PSA_ALG_SHA_512);
Ronald Cron67ea2542022-09-15 17:34:42 +02001103 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01001104 return PSA_ALG_NONE;
Ronald Cron67ea2542022-09-15 17:34:42 +02001105 }
1106}
1107#endif /* MBEDTLS_USE_PSA_CRYPTO */
1108
XiaokangQian23c5be62022-06-07 02:04:34 +00001109/*
XiaokangQianfb665a82022-06-15 03:57:21 +00001110 * Pick best ( private key, certificate chain ) pair based on the signature
1111 * algorithms supported by the client.
XiaokangQian23c5be62022-06-07 02:04:34 +00001112 */
Ronald Cronce7d76e2022-07-08 18:56:49 +02001113MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01001114static int ssl_tls13_pick_key_cert(mbedtls_ssl_context *ssl)
XiaokangQian23c5be62022-06-07 02:04:34 +00001115{
XiaokangQianfb665a82022-06-15 03:57:21 +00001116 mbedtls_ssl_key_cert *key_cert, *key_cert_list;
XiaokangQian81802f42022-06-10 13:25:22 +00001117 const uint16_t *sig_alg = ssl->handshake->received_sig_algs;
XiaokangQian23c5be62022-06-07 02:04:34 +00001118
1119#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Gilles Peskine449bd832023-01-11 14:50:10 +01001120 if (ssl->handshake->sni_key_cert != NULL) {
XiaokangQianfb665a82022-06-15 03:57:21 +00001121 key_cert_list = ssl->handshake->sni_key_cert;
Gilles Peskine449bd832023-01-11 14:50:10 +01001122 } else
XiaokangQian23c5be62022-06-07 02:04:34 +00001123#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
Gilles Peskine449bd832023-01-11 14:50:10 +01001124 key_cert_list = ssl->conf->key_cert;
XiaokangQian23c5be62022-06-07 02:04:34 +00001125
Gilles Peskine449bd832023-01-11 14:50:10 +01001126 if (key_cert_list == NULL) {
1127 MBEDTLS_SSL_DEBUG_MSG(3, ("server has no certificate"));
1128 return -1;
XiaokangQian23c5be62022-06-07 02:04:34 +00001129 }
1130
Gilles Peskine449bd832023-01-11 14:50:10 +01001131 for (; *sig_alg != MBEDTLS_TLS1_3_SIG_NONE; sig_alg++) {
1132 if (!mbedtls_ssl_sig_alg_is_offered(ssl, *sig_alg)) {
Ronald Cron67ea2542022-09-15 17:34:42 +02001133 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +01001134 }
Ronald Cron67ea2542022-09-15 17:34:42 +02001135
Gilles Peskine449bd832023-01-11 14:50:10 +01001136 if (!mbedtls_ssl_tls13_sig_alg_for_cert_verify_is_supported(*sig_alg)) {
Ronald Cron67ea2542022-09-15 17:34:42 +02001137 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +01001138 }
Ronald Cron67ea2542022-09-15 17:34:42 +02001139
Gilles Peskine449bd832023-01-11 14:50:10 +01001140 for (key_cert = key_cert_list; key_cert != NULL;
1141 key_cert = key_cert->next) {
Ronald Cron67ea2542022-09-15 17:34:42 +02001142#if defined(MBEDTLS_USE_PSA_CRYPTO)
1143 psa_algorithm_t psa_alg = PSA_ALG_NONE;
1144#endif /* MBEDTLS_USE_PSA_CRYPTO */
1145
Gilles Peskine449bd832023-01-11 14:50:10 +01001146 MBEDTLS_SSL_DEBUG_CRT(3, "certificate (chain) candidate",
1147 key_cert->cert);
XiaokangQian81802f42022-06-10 13:25:22 +00001148
1149 /*
Gilles Peskine449bd832023-01-11 14:50:10 +01001150 * This avoids sending the client a cert it'll reject based on
1151 * keyUsage or other extensions.
1152 */
1153 if (mbedtls_x509_crt_check_key_usage(
1154 key_cert->cert, MBEDTLS_X509_KU_DIGITAL_SIGNATURE) != 0 ||
XiaokangQian81802f42022-06-10 13:25:22 +00001155 mbedtls_x509_crt_check_extended_key_usage(
XiaokangQianfb665a82022-06-15 03:57:21 +00001156 key_cert->cert, MBEDTLS_OID_SERVER_AUTH,
Gilles Peskine449bd832023-01-11 14:50:10 +01001157 MBEDTLS_OID_SIZE(MBEDTLS_OID_SERVER_AUTH)) != 0) {
1158 MBEDTLS_SSL_DEBUG_MSG(3, ("certificate mismatch: "
1159 "(extended) key usage extension"));
XiaokangQian81802f42022-06-10 13:25:22 +00001160 continue;
1161 }
XiaokangQian23c5be62022-06-07 02:04:34 +00001162
Gilles Peskine449bd832023-01-11 14:50:10 +01001163 MBEDTLS_SSL_DEBUG_MSG(3,
1164 ("ssl_tls13_pick_key_cert:"
1165 "check signature algorithm %s [%04x]",
1166 mbedtls_ssl_sig_alg_to_str(*sig_alg),
1167 *sig_alg));
Ronald Cron67ea2542022-09-15 17:34:42 +02001168#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01001169 psa_alg = ssl_tls13_iana_sig_alg_to_psa_alg(*sig_alg);
Ronald Cron67ea2542022-09-15 17:34:42 +02001170#endif /* MBEDTLS_USE_PSA_CRYPTO */
1171
Gilles Peskine449bd832023-01-11 14:50:10 +01001172 if (mbedtls_ssl_tls13_check_sig_alg_cert_key_match(
1173 *sig_alg, &key_cert->cert->pk)
Ronald Cron67ea2542022-09-15 17:34:42 +02001174#if defined(MBEDTLS_USE_PSA_CRYPTO)
1175 && psa_alg != PSA_ALG_NONE &&
Gilles Peskine449bd832023-01-11 14:50:10 +01001176 mbedtls_pk_can_do_ext(&key_cert->cert->pk, psa_alg,
1177 PSA_KEY_USAGE_SIGN_HASH) == 1
Ronald Cron67ea2542022-09-15 17:34:42 +02001178#endif /* MBEDTLS_USE_PSA_CRYPTO */
Gilles Peskine449bd832023-01-11 14:50:10 +01001179 ) {
XiaokangQianfb665a82022-06-15 03:57:21 +00001180 ssl->handshake->key_cert = key_cert;
Gilles Peskine449bd832023-01-11 14:50:10 +01001181 MBEDTLS_SSL_DEBUG_MSG(3,
1182 ("ssl_tls13_pick_key_cert:"
1183 "selected signature algorithm"
1184 " %s [%04x]",
1185 mbedtls_ssl_sig_alg_to_str(*sig_alg),
1186 *sig_alg));
XiaokangQianfb665a82022-06-15 03:57:21 +00001187 MBEDTLS_SSL_DEBUG_CRT(
Gilles Peskine449bd832023-01-11 14:50:10 +01001188 3, "selected certificate (chain)",
1189 ssl->handshake->key_cert->cert);
1190 return 0;
XiaokangQian81802f42022-06-10 13:25:22 +00001191 }
XiaokangQian81802f42022-06-10 13:25:22 +00001192 }
XiaokangQian23c5be62022-06-07 02:04:34 +00001193 }
1194
Gilles Peskine449bd832023-01-11 14:50:10 +01001195 MBEDTLS_SSL_DEBUG_MSG(2, ("ssl_tls13_pick_key_cert:"
1196 "no suitable certificate found"));
1197 return -1;
XiaokangQian23c5be62022-06-07 02:04:34 +00001198}
XiaokangQian81802f42022-06-10 13:25:22 +00001199#endif /* MBEDTLS_X509_CRT_PARSE_C &&
Ronald Cron928cbd32022-10-04 16:14:26 +02001200 MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED */
XiaokangQian23c5be62022-06-07 02:04:34 +00001201
XiaokangQian4080a7f2022-04-11 09:55:18 +00001202/*
XiaokangQianed582dd2022-04-13 08:21:05 +00001203 *
1204 * STATE HANDLING: ClientHello
1205 *
XiaokangQianb67384d2022-04-19 00:02:38 +00001206 * There are three possible classes of outcomes when parsing the ClientHello:
XiaokangQianed582dd2022-04-13 08:21:05 +00001207 *
XiaokangQianb67384d2022-04-19 00:02:38 +00001208 * 1) The ClientHello was well-formed and matched the server's configuration.
XiaokangQianed582dd2022-04-13 08:21:05 +00001209 *
1210 * In this case, the server progresses to sending its ServerHello.
1211 *
XiaokangQianb67384d2022-04-19 00:02:38 +00001212 * 2) The ClientHello was well-formed but didn't match the server's
1213 * configuration.
XiaokangQianed582dd2022-04-13 08:21:05 +00001214 *
1215 * For example, the client might not have offered a key share which
1216 * the server supports, or the server might require a cookie.
1217 *
1218 * In this case, the server sends a HelloRetryRequest.
1219 *
XiaokangQianb67384d2022-04-19 00:02:38 +00001220 * 3) The ClientHello was ill-formed
XiaokangQianed582dd2022-04-13 08:21:05 +00001221 *
1222 * In this case, we abort the handshake.
1223 *
1224 */
1225
1226/*
XiaokangQian4080a7f2022-04-11 09:55:18 +00001227 * Structure of this message:
1228 *
XiaokangQiane8ff3502022-04-22 02:34:40 +00001229 * uint16 ProtocolVersion;
1230 * opaque Random[32];
1231 * uint8 CipherSuite[2]; // Cryptographic suite selector
XiaokangQian4080a7f2022-04-11 09:55:18 +00001232 *
XiaokangQiane8ff3502022-04-22 02:34:40 +00001233 * struct {
1234 * ProtocolVersion legacy_version = 0x0303; // TLS v1.2
1235 * Random random;
1236 * opaque legacy_session_id<0..32>;
1237 * CipherSuite cipher_suites<2..2^16-2>;
1238 * opaque legacy_compression_methods<1..2^8-1>;
1239 * Extension extensions<8..2^16-1>;
1240 * } ClientHello;
XiaokangQian4080a7f2022-04-11 09:55:18 +00001241 */
XiaokangQianed582dd2022-04-13 08:21:05 +00001242
1243#define SSL_CLIENT_HELLO_OK 0
1244#define SSL_CLIENT_HELLO_HRR_REQUIRED 1
Ronald Cron5af4c7f2023-03-07 20:46:59 +01001245#define SSL_CLIENT_HELLO_TLS1_2 2
XiaokangQianed582dd2022-04-13 08:21:05 +00001246
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001247MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01001248static int ssl_tls13_parse_client_hello(mbedtls_ssl_context *ssl,
1249 const unsigned char *buf,
1250 const unsigned char *end)
XiaokangQian7807f9f2022-02-15 10:04:37 +00001251{
XiaokangQianb67384d2022-04-19 00:02:38 +00001252 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1253 const unsigned char *p = buf;
Ronald Crond540d992023-03-07 09:41:48 +01001254 const unsigned char *random;
XiaokangQian4080a7f2022-04-11 09:55:18 +00001255 size_t legacy_session_id_len;
Ronald Croncada4102023-03-07 09:51:39 +01001256 const unsigned char *legacy_session_id;
XiaokangQian318dc762022-04-20 09:43:51 +00001257 size_t cipher_suites_len;
Ronald Cron2f16b4e2023-03-07 10:07:32 +01001258 const unsigned char *cipher_suites;
XiaokangQian060d8672022-04-21 09:24:56 +00001259 const unsigned char *cipher_suites_end;
XiaokangQianb67384d2022-04-19 00:02:38 +00001260 size_t extensions_len;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001261 const unsigned char *extensions_end;
Ronald Croneff56732023-04-03 17:36:31 +02001262 const unsigned char *supported_versions_data;
1263 const unsigned char *supported_versions_data_end;
Jerry Yuc4bf5d62022-10-29 09:08:47 +08001264 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
Jerry Yu49ca9282022-05-05 11:05:22 +08001265 int hrr_required = 0;
Ronald Cron8a74f072023-06-14 17:59:29 +02001266 int no_usable_share_for_key_agreement = 0;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001267
Ronald Cron41a443a2022-10-04 16:38:25 +02001268#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED)
Ronald Cron79cdd412024-02-20 17:19:28 +01001269 int got_psk = 0;
1270 struct psk_attributes psk = PSK_ATTRIBUTES_INIT;
Jerry Yu29d9faa2022-08-23 17:52:45 +08001271 const unsigned char *pre_shared_key_ext = NULL;
Jerry Yu1c105562022-07-10 06:32:38 +00001272 const unsigned char *pre_shared_key_ext_end = NULL;
Ronald Cron41a443a2022-10-04 16:38:25 +02001273#endif
XiaokangQian7807f9f2022-02-15 10:04:37 +00001274
XiaokangQian7807f9f2022-02-15 10:04:37 +00001275 /*
XiaokangQianb67384d2022-04-19 00:02:38 +00001276 * ClientHello layout:
XiaokangQian7807f9f2022-02-15 10:04:37 +00001277 * 0 . 1 protocol version
XiaokangQiane8ff3502022-04-22 02:34:40 +00001278 * 2 . 33 random bytes
XiaokangQianc5763b52022-04-02 03:34:37 +00001279 * 34 . 34 session id length ( 1 byte )
XiaokangQian7807f9f2022-02-15 10:04:37 +00001280 * 35 . 34+x session id
XiaokangQian7807f9f2022-02-15 10:04:37 +00001281 * .. . .. ciphersuite list length ( 2 bytes )
1282 * .. . .. ciphersuite list
1283 * .. . .. compression alg. list length ( 1 byte )
1284 * .. . .. compression alg. list
1285 * .. . .. extensions length ( 2 bytes, optional )
1286 * .. . .. extensions ( optional )
1287 */
1288
XiaokangQianb67384d2022-04-19 00:02:38 +00001289 /*
bootstrap-prime6dbbf442022-05-17 19:30:44 -04001290 * Minimal length ( with everything empty and extensions omitted ) is
XiaokangQian7807f9f2022-02-15 10:04:37 +00001291 * 2 + 32 + 1 + 2 + 1 = 38 bytes. Check that first, so that we can
1292 * read at least up to session id length without worrying.
1293 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001294 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, 38);
XiaokangQian7807f9f2022-02-15 10:04:37 +00001295
1296 /* ...
1297 * ProtocolVersion legacy_version = 0x0303; // TLS 1.2
1298 * ...
1299 * with ProtocolVersion defined as:
1300 * uint16 ProtocolVersion;
1301 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001302 if (mbedtls_ssl_read_version(p, ssl->conf->transport) !=
1303 MBEDTLS_SSL_VERSION_TLS1_2) {
1304 MBEDTLS_SSL_DEBUG_MSG(1, ("Unsupported version of TLS."));
1305 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_PROTOCOL_VERSION,
1306 MBEDTLS_ERR_SSL_BAD_PROTOCOL_VERSION);
1307 return MBEDTLS_ERR_SSL_BAD_PROTOCOL_VERSION;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001308 }
1309 p += 2;
1310
Jerry Yuc1be19f2022-04-23 16:11:39 +08001311 /* ...
1312 * Random random;
1313 * ...
XiaokangQianb67384d2022-04-19 00:02:38 +00001314 * with Random defined as:
1315 * opaque Random[32];
XiaokangQian7807f9f2022-02-15 10:04:37 +00001316 */
Ronald Crond540d992023-03-07 09:41:48 +01001317 random = p;
XiaokangQian08037552022-04-20 07:16:41 +00001318 p += MBEDTLS_CLIENT_HELLO_RANDOM_LEN;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001319
Jerry Yuc1be19f2022-04-23 16:11:39 +08001320 /* ...
XiaokangQianb67384d2022-04-19 00:02:38 +00001321 * opaque legacy_session_id<0..32>;
Jerry Yuc1be19f2022-04-23 16:11:39 +08001322 * ...
XiaokangQian7807f9f2022-02-15 10:04:37 +00001323 */
Ronald Croncada4102023-03-07 09:51:39 +01001324 legacy_session_id_len = *(p++);
1325 legacy_session_id = p;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001326
XiaokangQianb67384d2022-04-19 00:02:38 +00001327 /*
1328 * Check we have enough data for the legacy session identifier
Jerry Yue95c8af2022-07-26 15:48:20 +08001329 * and the ciphersuite list length.
XiaokangQianb67384d2022-04-19 00:02:38 +00001330 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001331 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, legacy_session_id_len + 2);
XiaokangQian4080a7f2022-04-11 09:55:18 +00001332 p += legacy_session_id_len;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001333
Ronald Cron2f16b4e2023-03-07 10:07:32 +01001334 /* ...
1335 * CipherSuite cipher_suites<2..2^16-2>;
1336 * ...
1337 * with CipherSuite defined as:
1338 * uint8 CipherSuite[2];
1339 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001340 cipher_suites_len = MBEDTLS_GET_UINT16_BE(p, 0);
XiaokangQian7807f9f2022-02-15 10:04:37 +00001341 p += 2;
Ronald Cron2f16b4e2023-03-07 10:07:32 +01001342 cipher_suites = p;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001343
Ronald Cronfc7ae872023-02-16 15:32:19 +01001344 /*
1345 * The length of the ciphersuite list has to be even.
1346 */
1347 if (cipher_suites_len & 1) {
1348 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR,
1349 MBEDTLS_ERR_SSL_DECODE_ERROR);
1350 return MBEDTLS_ERR_SSL_DECODE_ERROR;
1351 }
1352
XiaokangQianb67384d2022-04-19 00:02:38 +00001353 /* Check we have enough data for the ciphersuite list, the legacy
1354 * compression methods and the length of the extensions.
Jerry Yue95c8af2022-07-26 15:48:20 +08001355 *
1356 * cipher_suites cipher_suites_len bytes
Waleed Elmelegye2a6aa52024-06-25 18:16:16 +01001357 * legacy_compression_methods length 1 byte
XiaokangQianb67384d2022-04-19 00:02:38 +00001358 */
Waleed Elmelegye2a6aa52024-06-25 18:16:16 +01001359 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, cipher_suites_len + 1);
Ronald Cron8c527d02023-03-07 15:47:47 +01001360 p += cipher_suites_len;
1361 cipher_suites_end = p;
Jerry Yu5725f1c2022-08-21 17:27:16 +08001362
Waleed Elmelegy0a9e8a32024-06-25 10:22:49 +01001363 /* Check if we have enough data for legacy_compression_methods
Waleed Elmelegye2a6aa52024-06-25 18:16:16 +01001364 * and the length of the extensions (2 bytes).
Waleed Elmelegya5842ac2024-06-19 15:09:48 +01001365 */
Waleed Elmelegye2a6aa52024-06-25 18:16:16 +01001366 MBEDTLS_SSL_CHK_BUF_READ_PTR(p + 1, end, p[0] + 2);
Waleed Elmelegyb6e73312024-06-11 18:44:48 +01001367
Jerry Yu5725f1c2022-08-21 17:27:16 +08001368 /*
Ronald Cron8c527d02023-03-07 15:47:47 +01001369 * Search for the supported versions extension and parse it to determine
1370 * if the client supports TLS 1.3.
1371 */
1372 ret = mbedtls_ssl_tls13_is_supported_versions_ext_present_in_exts(
Waleed Elmelegya5842ac2024-06-19 15:09:48 +01001373 ssl, p + 1 + p[0], end,
Ronald Croneff56732023-04-03 17:36:31 +02001374 &supported_versions_data, &supported_versions_data_end);
Ronald Cron8c527d02023-03-07 15:47:47 +01001375 if (ret < 0) {
1376 MBEDTLS_SSL_DEBUG_RET(1,
1377 ("mbedtls_ssl_tls13_is_supported_versions_ext_present_in_exts"), ret);
1378 return ret;
1379 }
1380
1381 if (ret == 0) {
Ronald Cron5af4c7f2023-03-07 20:46:59 +01001382 return SSL_CLIENT_HELLO_TLS1_2;
Ronald Cron8c527d02023-03-07 15:47:47 +01001383 }
1384
Ronald Cron5af4c7f2023-03-07 20:46:59 +01001385 if (ret == 1) {
1386 ret = ssl_tls13_parse_supported_versions_ext(ssl,
Ronald Croneff56732023-04-03 17:36:31 +02001387 supported_versions_data,
1388 supported_versions_data_end);
Ronald Cron5af4c7f2023-03-07 20:46:59 +01001389 if (ret < 0) {
1390 MBEDTLS_SSL_DEBUG_RET(1,
1391 ("ssl_tls13_parse_supported_versions_ext"), ret);
1392 return ret;
1393 }
1394
Ronald Cronb828c7d2023-04-03 16:37:22 +02001395 /*
1396 * The supported versions extension was parsed successfully as the
1397 * value returned by ssl_tls13_parse_supported_versions_ext() is
1398 * positive. The return value is then equal to
1399 * MBEDTLS_SSL_VERSION_TLS1_2 or MBEDTLS_SSL_VERSION_TLS1_3, defining
1400 * the TLS version to negotiate.
1401 */
Ronald Cron5af4c7f2023-03-07 20:46:59 +01001402 if (MBEDTLS_SSL_VERSION_TLS1_2 == ret) {
1403 return SSL_CLIENT_HELLO_TLS1_2;
1404 }
Ronald Cron8c527d02023-03-07 15:47:47 +01001405 }
1406
1407 /*
1408 * We negotiate TLS 1.3.
Ronald Cron64582392023-03-07 09:21:40 +01001409 */
1410 ssl->tls_version = MBEDTLS_SSL_VERSION_TLS1_3;
Ronald Cron64582392023-03-07 09:21:40 +01001411 ssl->session_negotiate->tls_version = MBEDTLS_SSL_VERSION_TLS1_3;
1412 ssl->session_negotiate->endpoint = ssl->conf->endpoint;
Ronald Cron64582392023-03-07 09:21:40 +01001413
1414 /*
Ronald Crondad02b22023-04-06 09:57:52 +02001415 * We are negotiating the version 1.3 of the protocol. Do what we have
Ronald Croncada4102023-03-07 09:51:39 +01001416 * postponed: copy of the client random bytes, copy of the legacy session
Ronald Cron2f16b4e2023-03-07 10:07:32 +01001417 * identifier and selection of the TLS 1.3 cipher suite.
Ronald Crond540d992023-03-07 09:41:48 +01001418 */
1419 MBEDTLS_SSL_DEBUG_BUF(3, "client hello, random bytes",
1420 random, MBEDTLS_CLIENT_HELLO_RANDOM_LEN);
1421 memcpy(&handshake->randbytes[0], random, MBEDTLS_CLIENT_HELLO_RANDOM_LEN);
1422
Ronald Croncada4102023-03-07 09:51:39 +01001423 if (legacy_session_id_len > sizeof(ssl->session_negotiate->id)) {
1424 MBEDTLS_SSL_DEBUG_MSG(1, ("bad client hello message"));
1425 return MBEDTLS_ERR_SSL_DECODE_ERROR;
1426 }
1427 ssl->session_negotiate->id_len = legacy_session_id_len;
1428 MBEDTLS_SSL_DEBUG_BUF(3, "client hello, session id",
1429 legacy_session_id, legacy_session_id_len);
1430 memcpy(&ssl->session_negotiate->id[0],
1431 legacy_session_id, legacy_session_id_len);
1432
Ronald Crond540d992023-03-07 09:41:48 +01001433 /*
Jerry Yu5725f1c2022-08-21 17:27:16 +08001434 * Search for a matching ciphersuite
1435 */
Ronald Cron2f16b4e2023-03-07 10:07:32 +01001436 MBEDTLS_SSL_DEBUG_BUF(3, "client hello, list of cipher suites",
1437 cipher_suites, cipher_suites_len);
Jerry Yu5725f1c2022-08-21 17:27:16 +08001438
Ronald Cron89089cc2024-02-14 18:18:08 +01001439 ssl_tls13_select_ciphersuite(ssl, cipher_suites, cipher_suites_end,
1440 0, PSA_ALG_NONE, &handshake->ciphersuite_info);
Jerry Yu5725f1c2022-08-21 17:27:16 +08001441
Gilles Peskine449bd832023-01-11 14:50:10 +01001442 if (handshake->ciphersuite_info == NULL) {
1443 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE,
1444 MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE);
1445 return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
Jerry Yu5725f1c2022-08-21 17:27:16 +08001446 }
Ronald Cron89089cc2024-02-14 18:18:08 +01001447 ssl->session_negotiate->ciphersuite = handshake->ciphersuite_info->id;
1448
1449 MBEDTLS_SSL_DEBUG_MSG(2, ("selected ciphersuite: %04x - %s",
1450 ((unsigned) handshake->ciphersuite_info->id),
1451 handshake->ciphersuite_info->name));
Jerry Yuc1be19f2022-04-23 16:11:39 +08001452
XiaokangQian4080a7f2022-04-11 09:55:18 +00001453 /* ...
XiaokangQianb67384d2022-04-19 00:02:38 +00001454 * opaque legacy_compression_methods<1..2^8-1>;
XiaokangQian4080a7f2022-04-11 09:55:18 +00001455 * ...
XiaokangQian7807f9f2022-02-15 10:04:37 +00001456 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001457 if (p[0] != 1 || p[1] != MBEDTLS_SSL_COMPRESS_NULL) {
1458 MBEDTLS_SSL_DEBUG_MSG(1, ("bad legacy compression method"));
1459 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER,
1460 MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER);
1461 return MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001462 }
XiaokangQianb67384d2022-04-19 00:02:38 +00001463 p += 2;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001464
Jerry Yuc1be19f2022-04-23 16:11:39 +08001465 /* ...
XiaokangQianb67384d2022-04-19 00:02:38 +00001466 * Extension extensions<8..2^16-1>;
Jerry Yuc1be19f2022-04-23 16:11:39 +08001467 * ...
XiaokangQianb67384d2022-04-19 00:02:38 +00001468 * with Extension defined as:
1469 * struct {
1470 * ExtensionType extension_type;
1471 * opaque extension_data<0..2^16-1>;
1472 * } Extension;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001473 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001474 extensions_len = MBEDTLS_GET_UINT16_BE(p, 0);
XiaokangQian7807f9f2022-02-15 10:04:37 +00001475 p += 2;
Gilles Peskine449bd832023-01-11 14:50:10 +01001476 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, extensions_len);
XiaokangQiane8ff3502022-04-22 02:34:40 +00001477 extensions_end = p + extensions_len;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001478
Gilles Peskine449bd832023-01-11 14:50:10 +01001479 MBEDTLS_SSL_DEBUG_BUF(3, "client hello extensions", p, extensions_len);
Jerry Yu63a459c2022-10-31 13:38:40 +08001480 handshake->received_extensions = MBEDTLS_SSL_EXT_MASK_NONE;
Jerry Yu0c354a22022-08-29 15:25:36 +08001481
Gilles Peskine449bd832023-01-11 14:50:10 +01001482 while (p < extensions_end) {
XiaokangQian7807f9f2022-02-15 10:04:37 +00001483 unsigned int extension_type;
1484 size_t extension_data_len;
1485 const unsigned char *extension_data_end;
Jerry Yu263dbf72022-10-26 10:51:27 +08001486 uint32_t allowed_exts = MBEDTLS_SSL_TLS1_3_ALLOWED_EXTS_OF_CH;
1487
Ronald Cron5fbd2702024-02-14 10:03:36 +01001488 if (ssl->handshake->hello_retry_request_flag) {
Jerry Yu263dbf72022-10-26 10:51:27 +08001489 /* Do not accept early data extension in 2nd ClientHello */
1490 allowed_exts &= ~MBEDTLS_SSL_EXT_MASK(EARLY_DATA);
1491 }
XiaokangQian7807f9f2022-02-15 10:04:37 +00001492
Jerry Yu0c354a22022-08-29 15:25:36 +08001493 /* RFC 8446, section 4.2.11
Jerry Yu1c9247c2022-07-21 12:37:39 +08001494 *
1495 * The "pre_shared_key" extension MUST be the last extension in the
1496 * ClientHello (this facilitates implementation as described below).
1497 * Servers MUST check that it is the last extension and otherwise fail
1498 * the handshake with an "illegal_parameter" alert.
1499 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001500 if (handshake->received_extensions & MBEDTLS_SSL_EXT_MASK(PRE_SHARED_KEY)) {
Jerry Yu1c9247c2022-07-21 12:37:39 +08001501 MBEDTLS_SSL_DEBUG_MSG(
Gilles Peskine449bd832023-01-11 14:50:10 +01001502 3, ("pre_shared_key is not last extension."));
Jerry Yu1c9247c2022-07-21 12:37:39 +08001503 MBEDTLS_SSL_PEND_FATAL_ALERT(
1504 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER,
Gilles Peskine449bd832023-01-11 14:50:10 +01001505 MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER);
1506 return MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER;
Jerry Yu1c9247c2022-07-21 12:37:39 +08001507 }
1508
Gilles Peskine449bd832023-01-11 14:50:10 +01001509 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, extensions_end, 4);
1510 extension_type = MBEDTLS_GET_UINT16_BE(p, 0);
1511 extension_data_len = MBEDTLS_GET_UINT16_BE(p, 2);
XiaokangQian7807f9f2022-02-15 10:04:37 +00001512 p += 4;
1513
Gilles Peskine449bd832023-01-11 14:50:10 +01001514 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, extensions_end, extension_data_len);
XiaokangQian7807f9f2022-02-15 10:04:37 +00001515 extension_data_end = p + extension_data_len;
1516
Jerry Yuc4bf5d62022-10-29 09:08:47 +08001517 ret = mbedtls_ssl_tls13_check_received_extension(
Gilles Peskine449bd832023-01-11 14:50:10 +01001518 ssl, MBEDTLS_SSL_HS_CLIENT_HELLO, extension_type,
Jerry Yu263dbf72022-10-26 10:51:27 +08001519 allowed_exts);
Gilles Peskine449bd832023-01-11 14:50:10 +01001520 if (ret != 0) {
1521 return ret;
1522 }
Jerry Yue18dc7e2022-08-04 16:29:22 +08001523
Gilles Peskine449bd832023-01-11 14:50:10 +01001524 switch (extension_type) {
XiaokangQian40a35232022-05-07 09:02:40 +00001525#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
1526 case MBEDTLS_TLS_EXT_SERVERNAME:
Gilles Peskine449bd832023-01-11 14:50:10 +01001527 MBEDTLS_SSL_DEBUG_MSG(3, ("found ServerName extension"));
1528 ret = mbedtls_ssl_parse_server_name_ext(ssl, p,
1529 extension_data_end);
1530 if (ret != 0) {
XiaokangQian40a35232022-05-07 09:02:40 +00001531 MBEDTLS_SSL_DEBUG_RET(
Gilles Peskine449bd832023-01-11 14:50:10 +01001532 1, "mbedtls_ssl_parse_servername_ext", ret);
1533 return ret;
XiaokangQian40a35232022-05-07 09:02:40 +00001534 }
XiaokangQian40a35232022-05-07 09:02:40 +00001535 break;
1536#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
1537
Przemek Stekiel8c0a9532023-06-15 16:48:19 +02001538#if defined(PSA_WANT_ALG_ECDH) || defined(PSA_WANT_ALG_FFDH)
XiaokangQian7807f9f2022-02-15 10:04:37 +00001539 case MBEDTLS_TLS_EXT_SUPPORTED_GROUPS:
Gilles Peskine449bd832023-01-11 14:50:10 +01001540 MBEDTLS_SSL_DEBUG_MSG(3, ("found supported group extension"));
XiaokangQian7807f9f2022-02-15 10:04:37 +00001541
1542 /* Supported Groups Extension
1543 *
1544 * When sent by the client, the "supported_groups" extension
1545 * indicates the named groups which the client supports,
1546 * ordered from most preferred to least preferred.
1547 */
Jerry Yuc1be19f2022-04-23 16:11:39 +08001548 ret = ssl_tls13_parse_supported_groups_ext(
Gilles Peskine449bd832023-01-11 14:50:10 +01001549 ssl, p, extension_data_end);
1550 if (ret != 0) {
Xiaokang Qian9f1747b2023-03-30 02:50:04 +00001551 MBEDTLS_SSL_DEBUG_RET(
Xiaokang Qian8bce0e62023-04-04 10:15:48 +00001552 1, "ssl_tls13_parse_supported_groups_ext", ret);
Gilles Peskine449bd832023-01-11 14:50:10 +01001553 return ret;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001554 }
1555
XiaokangQian7807f9f2022-02-15 10:04:37 +00001556 break;
Przemek Stekiel8c0a9532023-06-15 16:48:19 +02001557#endif /* PSA_WANT_ALG_ECDH || PSA_WANT_ALG_FFDH*/
XiaokangQian7807f9f2022-02-15 10:04:37 +00001558
Valerio Settic9ae8622023-07-25 11:23:50 +02001559#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_EPHEMERAL_ENABLED)
XiaokangQian7807f9f2022-02-15 10:04:37 +00001560 case MBEDTLS_TLS_EXT_KEY_SHARE:
Gilles Peskine449bd832023-01-11 14:50:10 +01001561 MBEDTLS_SSL_DEBUG_MSG(3, ("found key share extension"));
XiaokangQian7807f9f2022-02-15 10:04:37 +00001562
1563 /*
1564 * Key Share Extension
1565 *
1566 * When sent by the client, the "key_share" extension
1567 * contains the endpoint's cryptographic parameters for
1568 * ECDHE/DHE key establishment methods.
1569 */
Jerry Yuc1be19f2022-04-23 16:11:39 +08001570 ret = ssl_tls13_parse_key_shares_ext(
Gilles Peskine449bd832023-01-11 14:50:10 +01001571 ssl, p, extension_data_end);
1572 if (ret == SSL_TLS1_3_PARSE_KEY_SHARES_EXT_NO_MATCH) {
Ronald Cron8a74f072023-06-14 17:59:29 +02001573 MBEDTLS_SSL_DEBUG_MSG(2, ("No usable share for key agreement."));
1574 no_usable_share_for_key_agreement = 1;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001575 }
1576
Gilles Peskine449bd832023-01-11 14:50:10 +01001577 if (ret < 0) {
Jerry Yu582dd062022-04-22 21:59:01 +08001578 MBEDTLS_SSL_DEBUG_RET(
Gilles Peskine449bd832023-01-11 14:50:10 +01001579 1, "ssl_tls13_parse_key_shares_ext", ret);
1580 return ret;
Jerry Yu582dd062022-04-22 21:59:01 +08001581 }
XiaokangQian7807f9f2022-02-15 10:04:37 +00001582
XiaokangQian7807f9f2022-02-15 10:04:37 +00001583 break;
Valerio Settic9ae8622023-07-25 11:23:50 +02001584#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_EPHEMERAL_ENABLED */
XiaokangQian7807f9f2022-02-15 10:04:37 +00001585
1586 case MBEDTLS_TLS_EXT_SUPPORTED_VERSIONS:
Ronald Cron8c527d02023-03-07 15:47:47 +01001587 /* Already parsed */
XiaokangQian7807f9f2022-02-15 10:04:37 +00001588 break;
1589
Ronald Cron41a443a2022-10-04 16:38:25 +02001590#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED)
Jerry Yue19e3b92022-07-08 12:04:51 +00001591 case MBEDTLS_TLS_EXT_PSK_KEY_EXCHANGE_MODES:
Xiaokang Qian9f1747b2023-03-30 02:50:04 +00001592 MBEDTLS_SSL_DEBUG_MSG(
1593 3, ("found psk key exchange modes extension"));
Jerry Yue19e3b92022-07-08 12:04:51 +00001594
1595 ret = ssl_tls13_parse_key_exchange_modes_ext(
Gilles Peskine449bd832023-01-11 14:50:10 +01001596 ssl, p, extension_data_end);
1597 if (ret != 0) {
Jerry Yue19e3b92022-07-08 12:04:51 +00001598 MBEDTLS_SSL_DEBUG_RET(
Gilles Peskine449bd832023-01-11 14:50:10 +01001599 1, "ssl_tls13_parse_key_exchange_modes_ext", ret);
1600 return ret;
Jerry Yue19e3b92022-07-08 12:04:51 +00001601 }
1602
Jerry Yue19e3b92022-07-08 12:04:51 +00001603 break;
Ronald Cron41a443a2022-10-04 16:38:25 +02001604#endif
Jerry Yue19e3b92022-07-08 12:04:51 +00001605
Jerry Yu1c105562022-07-10 06:32:38 +00001606 case MBEDTLS_TLS_EXT_PRE_SHARED_KEY:
Gilles Peskine449bd832023-01-11 14:50:10 +01001607 MBEDTLS_SSL_DEBUG_MSG(3, ("found pre_shared_key extension"));
1608 if ((handshake->received_extensions &
1609 MBEDTLS_SSL_EXT_MASK(PSK_KEY_EXCHANGE_MODES)) == 0) {
Jerry Yu13ab81d2022-07-22 23:17:11 +08001610 MBEDTLS_SSL_PEND_FATAL_ALERT(
1611 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER,
Gilles Peskine449bd832023-01-11 14:50:10 +01001612 MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER);
1613 return MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER;
Jerry Yu13ab81d2022-07-22 23:17:11 +08001614 }
Ronald Cron41a443a2022-10-04 16:38:25 +02001615#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED)
Jerry Yu1c105562022-07-10 06:32:38 +00001616 /* Delay processing of the PSK identity once we have
1617 * found out which algorithms to use. We keep a pointer
1618 * to the buffer and the size for later processing.
1619 */
Jerry Yu29d9faa2022-08-23 17:52:45 +08001620 pre_shared_key_ext = p;
Jerry Yu1c105562022-07-10 06:32:38 +00001621 pre_shared_key_ext_end = extension_data_end;
Jerry Yue18dc7e2022-08-04 16:29:22 +08001622#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED */
Jerry Yu1c105562022-07-10 06:32:38 +00001623 break;
Jerry Yu1c105562022-07-10 06:32:38 +00001624
XiaokangQianacb39922022-06-17 10:18:48 +00001625#if defined(MBEDTLS_SSL_ALPN)
1626 case MBEDTLS_TLS_EXT_ALPN:
Gilles Peskine449bd832023-01-11 14:50:10 +01001627 MBEDTLS_SSL_DEBUG_MSG(3, ("found alpn extension"));
XiaokangQianacb39922022-06-17 10:18:48 +00001628
Gilles Peskine449bd832023-01-11 14:50:10 +01001629 ret = mbedtls_ssl_parse_alpn_ext(ssl, p, extension_data_end);
1630 if (ret != 0) {
XiaokangQianacb39922022-06-17 10:18:48 +00001631 MBEDTLS_SSL_DEBUG_RET(
Gilles Peskine449bd832023-01-11 14:50:10 +01001632 1, ("mbedtls_ssl_parse_alpn_ext"), ret);
1633 return ret;
XiaokangQianacb39922022-06-17 10:18:48 +00001634 }
XiaokangQianacb39922022-06-17 10:18:48 +00001635 break;
1636#endif /* MBEDTLS_SSL_ALPN */
1637
Ronald Cron928cbd32022-10-04 16:14:26 +02001638#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED)
XiaokangQian7807f9f2022-02-15 10:04:37 +00001639 case MBEDTLS_TLS_EXT_SIG_ALG:
Gilles Peskine449bd832023-01-11 14:50:10 +01001640 MBEDTLS_SSL_DEBUG_MSG(3, ("found signature_algorithms extension"));
XiaokangQian7807f9f2022-02-15 10:04:37 +00001641
Gabor Mezei078e8032022-04-27 21:17:56 +02001642 ret = mbedtls_ssl_parse_sig_alg_ext(
Gilles Peskine449bd832023-01-11 14:50:10 +01001643 ssl, p, extension_data_end);
1644 if (ret != 0) {
Xiaokang Qian49f39c12023-04-06 02:31:02 +00001645 MBEDTLS_SSL_DEBUG_RET(
1646 1, "mbedtls_ssl_parse_sig_alg_ext", ret);
Gilles Peskine449bd832023-01-11 14:50:10 +01001647 return ret;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001648 }
XiaokangQian7807f9f2022-02-15 10:04:37 +00001649 break;
Ronald Cron928cbd32022-10-04 16:14:26 +02001650#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED */
XiaokangQian7807f9f2022-02-15 10:04:37 +00001651
Jan Bruckner151f6422023-02-10 12:45:19 +01001652#if defined(MBEDTLS_SSL_RECORD_SIZE_LIMIT)
1653 case MBEDTLS_TLS_EXT_RECORD_SIZE_LIMIT:
1654 MBEDTLS_SSL_DEBUG_MSG(3, ("found record_size_limit extension"));
1655
Xiaokang Qian9f1747b2023-03-30 02:50:04 +00001656 ret = mbedtls_ssl_tls13_parse_record_size_limit_ext(
1657 ssl, p, extension_data_end);
Jan Brucknerf482dcc2023-03-15 09:09:06 +01001658 if (ret != 0) {
1659 MBEDTLS_SSL_DEBUG_RET(
1660 1, ("mbedtls_ssl_tls13_parse_record_size_limit_ext"), ret);
1661 return ret;
1662 }
Jan Bruckner151f6422023-02-10 12:45:19 +01001663 break;
1664#endif /* MBEDTLS_SSL_RECORD_SIZE_LIMIT */
1665
XiaokangQian7807f9f2022-02-15 10:04:37 +00001666 default:
Jerry Yu79aa7212022-11-08 21:30:21 +08001667 MBEDTLS_SSL_PRINT_EXT(
Jerry Yu63a459c2022-10-31 13:38:40 +08001668 3, MBEDTLS_SSL_HS_CLIENT_HELLO,
Gilles Peskine449bd832023-01-11 14:50:10 +01001669 extension_type, "( ignored )");
Jerry Yu0c354a22022-08-29 15:25:36 +08001670 break;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001671 }
1672
1673 p += extension_data_len;
1674 }
1675
Gilles Peskine449bd832023-01-11 14:50:10 +01001676 MBEDTLS_SSL_PRINT_EXTS(3, MBEDTLS_SSL_HS_CLIENT_HELLO,
1677 handshake->received_extensions);
Jerry Yue18dc7e2022-08-04 16:29:22 +08001678
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +01001679 ret = mbedtls_ssl_add_hs_hdr_to_checksum(ssl,
1680 MBEDTLS_SSL_HS_CLIENT_HELLO,
1681 p - buf);
1682 if (0 != ret) {
1683 MBEDTLS_SSL_DEBUG_RET(1, ("mbedtls_ssl_add_hs_hdr_to_checksum"), ret);
1684 return ret;
1685 }
Jerry Yu032b15ce2022-07-11 06:10:03 +00001686
Ronald Cron41a443a2022-10-04 16:38:25 +02001687#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED)
XiaokangQian7807f9f2022-02-15 10:04:37 +00001688 /* Update checksum with either
1689 * - The entire content of the CH message, if no PSK extension is present
1690 * - The content up to but excluding the PSK extension, if present.
Ronald Cron7cab4f82024-03-07 15:09:09 +01001691 * Always parse the pre-shared-key extension when present in the
Ronald Cron12e72f12024-02-14 15:49:38 +01001692 * ClientHello even if some pre-requisites for PSK key exchange modes are
1693 * not met. That way we always validate the syntax of the extension.
XiaokangQian7807f9f2022-02-15 10:04:37 +00001694 */
Ronald Cron12e72f12024-02-14 15:49:38 +01001695 if (handshake->received_extensions & MBEDTLS_SSL_EXT_MASK(PRE_SHARED_KEY)) {
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +01001696 ret = handshake->update_checksum(ssl, buf,
1697 pre_shared_key_ext - buf);
1698 if (0 != ret) {
1699 MBEDTLS_SSL_DEBUG_RET(1, ("update_checksum"), ret);
1700 return ret;
1701 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001702 ret = ssl_tls13_parse_pre_shared_key_ext(ssl,
1703 pre_shared_key_ext,
1704 pre_shared_key_ext_end,
1705 cipher_suites,
Ronald Cron79cdd412024-02-20 17:19:28 +01001706 cipher_suites_end,
1707 &psk);
1708 if (ret == 0) {
1709 got_psk = 1;
1710 } else if (ret != MBEDTLS_ERR_SSL_UNKNOWN_IDENTITY) {
Jerry Yu63a459c2022-10-31 13:38:40 +08001711 MBEDTLS_SSL_DEBUG_RET(
Gilles Peskine449bd832023-01-11 14:50:10 +01001712 1, "ssl_tls13_parse_pre_shared_key_ext", ret);
1713 return ret;
Jerry Yu1c105562022-07-10 06:32:38 +00001714 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001715 } else
Ronald Cron41a443a2022-10-04 16:38:25 +02001716#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED */
Jerry Yu1c105562022-07-10 06:32:38 +00001717 {
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +01001718 ret = handshake->update_checksum(ssl, buf, p - buf);
1719 if (0 != ret) {
1720 MBEDTLS_SSL_DEBUG_RET(1, ("update_checksum"), ret);
1721 return ret;
1722 }
Jerry Yu1c105562022-07-10 06:32:38 +00001723 }
XiaokangQian7807f9f2022-02-15 10:04:37 +00001724
Ronald Cron79cdd412024-02-20 17:19:28 +01001725 /*
1726 * Determine the key exchange algorithm to use.
1727 * There are three types of key exchanges supported in TLS 1.3:
1728 * - (EC)DH with ECDSA,
1729 * - (EC)DH with PSK,
1730 * - plain PSK.
1731 *
1732 * The PSK-based key exchanges may additionally be used with 0-RTT.
1733 *
1734 * Our built-in order of preference is
1735 * 1 ) (EC)DHE-PSK Mode ( psk_ephemeral )
1736 * 2 ) Certificate Mode ( ephemeral )
1737 * 3 ) Plain PSK Mode ( psk )
1738 */
1739#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED)
1740 if (got_psk && (psk.key_exchange_mode ==
1741 MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK_EPHEMERAL)) {
1742 handshake->key_exchange_mode =
1743 MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK_EPHEMERAL;
1744 MBEDTLS_SSL_DEBUG_MSG(2, ("key exchange mode: psk_ephemeral"));
1745
1746 } else
1747#endif
1748 if (ssl_tls13_key_exchange_is_ephemeral_available(ssl)) {
1749 handshake->key_exchange_mode =
1750 MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL;
1751 MBEDTLS_SSL_DEBUG_MSG(2, ("key exchange mode: ephemeral"));
1752
1753 }
1754#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED)
1755 else if (got_psk && (psk.key_exchange_mode ==
1756 MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK)) {
1757 handshake->key_exchange_mode = MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK;
1758 MBEDTLS_SSL_DEBUG_MSG(2, ("key exchange mode: psk"));
1759 }
1760#endif
1761 else {
1762 MBEDTLS_SSL_DEBUG_MSG(
1763 1,
1764 ("ClientHello message misses mandatory extensions."));
1765 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_MISSING_EXTENSION,
1766 MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER);
1767 return MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER;
Gilles Peskine449bd832023-01-11 14:50:10 +01001768 }
XiaokangQian7807f9f2022-02-15 10:04:37 +00001769
Ronald Cron3e47eec2024-02-21 10:29:24 +01001770#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED)
Ronald Cron74a16292024-02-23 17:07:41 +01001771 if (handshake->key_exchange_mode &
1772 MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK_ALL) {
1773 handshake->ciphersuite_info = psk.ciphersuite_info;
1774 ssl->session_negotiate->ciphersuite = psk.ciphersuite_info->id;
1775
1776 MBEDTLS_SSL_DEBUG_MSG(2, ("Select PSK ciphersuite: %04x - %s",
1777 ((unsigned) psk.ciphersuite_info->id),
1778 psk.ciphersuite_info->name));
1779
1780 if (psk.type == MBEDTLS_SSL_TLS1_3_PSK_RESUMPTION) {
1781 handshake->resume = 1;
1782 }
Ronald Cron79cdd412024-02-20 17:19:28 +01001783 }
Ronald Cron3e47eec2024-02-21 10:29:24 +01001784#endif
Ronald Cron79cdd412024-02-20 17:19:28 +01001785
1786 if (handshake->key_exchange_mode !=
Ronald Cron8a74f072023-06-14 17:59:29 +02001787 MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK) {
1788 hrr_required = (no_usable_share_for_key_agreement != 0);
1789 }
1790
Gilles Peskine449bd832023-01-11 14:50:10 +01001791 mbedtls_ssl_optimize_checksum(ssl, handshake->ciphersuite_info);
Jerry Yue5834fd2022-08-29 20:16:09 +08001792
Gilles Peskine449bd832023-01-11 14:50:10 +01001793 return hrr_required ? SSL_CLIENT_HELLO_HRR_REQUIRED : SSL_CLIENT_HELLO_OK;
XiaokangQian75fe8c72022-06-15 09:42:45 +00001794}
1795
Jerry Yuab0da372023-02-08 13:55:24 +08001796#if defined(MBEDTLS_SSL_EARLY_DATA)
Jerry Yu4caf3ca2023-11-15 16:13:47 +08001797static int ssl_tls13_check_early_data_requirements(mbedtls_ssl_context *ssl)
Jerry Yuab0da372023-02-08 13:55:24 +08001798{
1799 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
1800
Jerry Yu985c9672022-12-04 14:06:30 +08001801 if (ssl->conf->early_data_enabled == MBEDTLS_SSL_EARLY_DATA_DISABLED) {
1802 MBEDTLS_SSL_DEBUG_MSG(
Jerry Yu985c9672022-12-04 14:06:30 +08001803 1,
Jerry Yu454dda32023-10-31 15:13:54 +08001804 ("EarlyData: rejected, feature disabled in server configuration."));
Jerry Yu4caf3ca2023-11-15 16:13:47 +08001805 return -1;
Ronald Cron7b6ee942024-01-12 10:29:55 +01001806 }
1807
Jerry Yu454dda32023-10-31 15:13:54 +08001808 if (!handshake->resume) {
1809 /* We currently support early data only in the case of PSKs established
1810 via a NewSessionTicket message thus in the case of a session
1811 resumption. */
Jerry Yu985c9672022-12-04 14:06:30 +08001812 MBEDTLS_SSL_DEBUG_MSG(
Jerry Yu7ef9fd82023-11-07 14:30:38 +08001813 1, ("EarlyData: rejected, not a session resumption."));
Jerry Yu4caf3ca2023-11-15 16:13:47 +08001814 return -1;
Jerry Yu985c9672022-12-04 14:06:30 +08001815 }
1816
Jerry Yu82fd6c12023-10-31 16:32:19 +08001817 /* RFC 8446 4.2.10
1818 *
1819 * In order to accept early data, the server MUST have accepted a PSK cipher
1820 * suite and selected the first key offered in the client's "pre_shared_key"
1821 * extension. In addition, it MUST verify that the following values are the
1822 * same as those associated with the selected PSK:
1823 * - The TLS version number
1824 * - The selected cipher suite
1825 * - The selected ALPN [RFC7301] protocol, if any
1826 *
1827 * NOTE:
Jerry Yu7ef9fd82023-11-07 14:30:38 +08001828 * - The TLS version number is checked in
1829 * ssl_tls13_offered_psks_check_identity_match_ticket().
Jerry Yu82fd6c12023-10-31 16:32:19 +08001830 */
1831
1832 if (handshake->selected_identity != 0) {
1833 MBEDTLS_SSL_DEBUG_MSG(
Jerry Yu7ef9fd82023-11-07 14:30:38 +08001834 1, ("EarlyData: rejected, the selected key in "
1835 "`pre_shared_key` is not the first one."));
Jerry Yu4caf3ca2023-11-15 16:13:47 +08001836 return -1;
Jerry Yu82fd6c12023-10-31 16:32:19 +08001837 }
1838
1839 if (handshake->ciphersuite_info->id !=
1840 ssl->session_negotiate->ciphersuite) {
1841 MBEDTLS_SSL_DEBUG_MSG(
Jerry Yu7ef9fd82023-11-07 14:30:38 +08001842 1, ("EarlyData: rejected, the selected ciphersuite is not the one "
1843 "of the selected pre-shared key."));
Jerry Yu4caf3ca2023-11-15 16:13:47 +08001844 return -1;
Jerry Yu82fd6c12023-10-31 16:32:19 +08001845
1846 }
Jerry Yu985c9672022-12-04 14:06:30 +08001847
Pengyu Lv94a42cc2023-12-06 10:04:17 +08001848 if (!mbedtls_ssl_tls13_session_ticket_allow_early_data(ssl->session_negotiate)) {
Jerry Yufceddb32022-12-12 15:30:34 +08001849 MBEDTLS_SSL_DEBUG_MSG(
1850 1,
Jerry Yuea96ac32023-11-21 17:06:36 +08001851 ("EarlyData: rejected, early_data not allowed in ticket "
1852 "permission bits."));
Jerry Yu4caf3ca2023-11-15 16:13:47 +08001853 return -1;
Jerry Yufceddb32022-12-12 15:30:34 +08001854 }
Jerry Yu985c9672022-12-04 14:06:30 +08001855
Waleed Elmelegy4dfb0e72024-03-14 01:48:40 +00001856#if defined(MBEDTLS_SSL_ALPN)
1857 const char *alpn = mbedtls_ssl_get_alpn_protocol(ssl);
1858 size_t alpn_len;
1859
1860 if (alpn == NULL && ssl->session_negotiate->ticket_alpn == NULL) {
1861 return 0;
1862 }
1863
1864 if (alpn != NULL) {
1865 alpn_len = strlen(alpn);
1866 }
1867
1868 if (alpn == NULL ||
1869 ssl->session_negotiate->ticket_alpn == NULL ||
1870 alpn_len != strlen(ssl->session_negotiate->ticket_alpn) ||
1871 (memcmp(alpn, ssl->session_negotiate->ticket_alpn, alpn_len) != 0)) {
1872 MBEDTLS_SSL_DEBUG_MSG(1, ("EarlyData: rejected, the selected ALPN is different "
1873 "from the one associated with the pre-shared key."));
1874 return -1;
1875 }
1876#endif
1877
Jerry Yu4caf3ca2023-11-15 16:13:47 +08001878 return 0;
Jerry Yuab0da372023-02-08 13:55:24 +08001879}
1880#endif /* MBEDTLS_SSL_EARLY_DATA */
1881
XiaokangQian75fe8c72022-06-15 09:42:45 +00001882/* Update the handshake state machine */
1883
Ronald Cronce7d76e2022-07-08 18:56:49 +02001884MBEDTLS_CHECK_RETURN_CRITICAL
Ronald Cron7b6ee942024-01-12 10:29:55 +01001885static int ssl_tls13_postprocess_client_hello(mbedtls_ssl_context *ssl,
1886 int hrr_required)
XiaokangQian75fe8c72022-06-15 09:42:45 +00001887{
1888 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1889
XiaokangQian7807f9f2022-02-15 10:04:37 +00001890 /*
XiaokangQianfb665a82022-06-15 03:57:21 +00001891 * Server certificate selection
1892 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001893 if (ssl->conf->f_cert_cb && (ret = ssl->conf->f_cert_cb(ssl)) != 0) {
1894 MBEDTLS_SSL_DEBUG_RET(1, "f_cert_cb", ret);
1895 return ret;
XiaokangQianfb665a82022-06-15 03:57:21 +00001896 }
1897#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
1898 ssl->handshake->sni_name = NULL;
1899 ssl->handshake->sni_name_len = 0;
1900#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
XiaokangQian7807f9f2022-02-15 10:04:37 +00001901
Gilles Peskine449bd832023-01-11 14:50:10 +01001902 ret = mbedtls_ssl_tls13_key_schedule_stage_early(ssl);
1903 if (ret != 0) {
1904 MBEDTLS_SSL_DEBUG_RET(1,
1905 "mbedtls_ssl_tls1_3_key_schedule_stage_early", ret);
1906 return ret;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001907 }
1908
Jerry Yuab0da372023-02-08 13:55:24 +08001909#if defined(MBEDTLS_SSL_EARLY_DATA)
Jerry Yu4caf3ca2023-11-15 16:13:47 +08001910 if (ssl->handshake->received_extensions & MBEDTLS_SSL_EXT_MASK(EARLY_DATA)) {
Ronald Cron31e2d832024-02-05 16:45:57 +01001911 ssl->handshake->early_data_accepted =
1912 (!hrr_required) && (ssl_tls13_check_early_data_requirements(ssl) == 0);
Jerry Yu4e9b70e2022-12-04 14:08:02 +08001913
Jerry Yu4caf3ca2023-11-15 16:13:47 +08001914 if (ssl->handshake->early_data_accepted) {
1915 ret = mbedtls_ssl_tls13_compute_early_transform(ssl);
1916 if (ret != 0) {
1917 MBEDTLS_SSL_DEBUG_RET(
1918 1, "mbedtls_ssl_tls13_compute_early_transform", ret);
1919 return ret;
1920 }
1921 } else {
1922 ssl->discard_early_data_record =
1923 hrr_required ?
1924 MBEDTLS_SSL_EARLY_DATA_DISCARD :
1925 MBEDTLS_SSL_EARLY_DATA_TRY_TO_DEPROTECT_AND_DISCARD;
Jerry Yu4e9b70e2022-12-04 14:08:02 +08001926 }
1927 }
Ronald Cron7b6ee942024-01-12 10:29:55 +01001928#else
1929 ((void) hrr_required);
Jerry Yuab0da372023-02-08 13:55:24 +08001930#endif /* MBEDTLS_SSL_EARLY_DATA */
1931
Gilles Peskine449bd832023-01-11 14:50:10 +01001932 return 0;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001933}
1934
1935/*
XiaokangQianed582dd2022-04-13 08:21:05 +00001936 * Main entry point from the state machine; orchestrates the otherfunctions.
1937 */
1938
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001939MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01001940static int ssl_tls13_process_client_hello(mbedtls_ssl_context *ssl)
XiaokangQianed582dd2022-04-13 08:21:05 +00001941{
1942
XiaokangQian08037552022-04-20 07:16:41 +00001943 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine449bd832023-01-11 14:50:10 +01001944 unsigned char *buf = NULL;
XiaokangQianed582dd2022-04-13 08:21:05 +00001945 size_t buflen = 0;
Jerry Yu4ca91402022-05-09 15:50:57 +08001946 int parse_client_hello_ret;
1947
Gilles Peskine449bd832023-01-11 14:50:10 +01001948 MBEDTLS_SSL_DEBUG_MSG(2, ("=> parse client hello"));
XiaokangQianed582dd2022-04-13 08:21:05 +00001949
Gilles Peskine449bd832023-01-11 14:50:10 +01001950 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_tls13_fetch_handshake_msg(
1951 ssl, MBEDTLS_SSL_HS_CLIENT_HELLO,
1952 &buf, &buflen));
XiaokangQianed582dd2022-04-13 08:21:05 +00001953
Gilles Peskine449bd832023-01-11 14:50:10 +01001954 MBEDTLS_SSL_PROC_CHK_NEG(ssl_tls13_parse_client_hello(ssl, buf,
1955 buf + buflen));
Ronald Cron5af4c7f2023-03-07 20:46:59 +01001956 parse_client_hello_ret = ret; /* Store positive return value of
1957 * parse_client_hello,
1958 * as negative error codes are handled
Jerry Yuf41553b2022-05-09 22:20:30 +08001959 * by MBEDTLS_SSL_PROC_CHK_NEG. */
Jerry Yu4ca91402022-05-09 15:50:57 +08001960
Ronald Cronb828c7d2023-04-03 16:37:22 +02001961 /*
Yanray Wang90acdc62023-12-08 10:29:42 +08001962 * Version 1.2 of the protocol has to be used for the handshake.
1963 * If TLS 1.2 is not supported, abort the handshake. Otherwise, set the
Ronald Cronb828c7d2023-04-03 16:37:22 +02001964 * ssl->keep_current_message flag for the ClientHello to be kept and parsed
1965 * as a TLS 1.2 ClientHello. We also change ssl->tls_version to
1966 * MBEDTLS_SSL_VERSION_TLS1_2 thus from now on mbedtls_ssl_handshake_step()
1967 * will dispatch to the TLS 1.2 state machine.
1968 */
Ronald Cron5af4c7f2023-03-07 20:46:59 +01001969 if (SSL_CLIENT_HELLO_TLS1_2 == parse_client_hello_ret) {
Yanray Wangfb0f47b2023-12-04 15:27:28 +08001970 /* Check if server supports TLS 1.2 */
Yanray Wang408ba6f2023-12-08 10:18:03 +08001971 if (!mbedtls_ssl_conf_is_tls12_enabled(ssl->conf)) {
Yanray Wangfb0f47b2023-12-04 15:27:28 +08001972 MBEDTLS_SSL_DEBUG_MSG(
Yanray Wang177e49a2023-12-08 10:51:04 +08001973 1, ("TLS 1.2 not supported."));
Yanray Wangfb0f47b2023-12-04 15:27:28 +08001974 MBEDTLS_SSL_PEND_FATAL_ALERT(
Yanray Wang2bef9172023-12-08 10:21:53 +08001975 MBEDTLS_SSL_ALERT_MSG_PROTOCOL_VERSION,
1976 MBEDTLS_ERR_SSL_BAD_PROTOCOL_VERSION);
1977 return MBEDTLS_ERR_SSL_BAD_PROTOCOL_VERSION;
Yanray Wangfb0f47b2023-12-04 15:27:28 +08001978 }
Ronald Cron5af4c7f2023-03-07 20:46:59 +01001979 ssl->keep_current_message = 1;
1980 ssl->tls_version = MBEDTLS_SSL_VERSION_TLS1_2;
1981 return 0;
1982 }
1983
Ronald Cron7b6ee942024-01-12 10:29:55 +01001984 MBEDTLS_SSL_PROC_CHK(
1985 ssl_tls13_postprocess_client_hello(ssl, parse_client_hello_ret ==
1986 SSL_CLIENT_HELLO_HRR_REQUIRED));
Jerry Yu582dd062022-04-22 21:59:01 +08001987
Ronald Cron5af4c7f2023-03-07 20:46:59 +01001988 if (SSL_CLIENT_HELLO_OK == parse_client_hello_ret) {
Gilles Peskine449bd832023-01-11 14:50:10 +01001989 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_SERVER_HELLO);
1990 } else {
1991 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_HELLO_RETRY_REQUEST);
1992 }
XiaokangQianed582dd2022-04-13 08:21:05 +00001993
1994cleanup:
1995
Gilles Peskine449bd832023-01-11 14:50:10 +01001996 MBEDTLS_SSL_DEBUG_MSG(2, ("<= parse client hello"));
1997 return ret;
XiaokangQianed582dd2022-04-13 08:21:05 +00001998}
1999
2000/*
Jerry Yu1c3e6882022-04-20 21:23:40 +08002001 * Handler for MBEDTLS_SSL_SERVER_HELLO
Jerry Yu5b64ae92022-03-30 17:15:02 +08002002 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002003MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002004static int ssl_tls13_prepare_server_hello(mbedtls_ssl_context *ssl)
Jerry Yuf4b27e42022-03-30 17:32:21 +08002005{
Jerry Yu637a3f12022-04-20 21:37:58 +08002006 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
2007 unsigned char *server_randbytes =
Gilles Peskine449bd832023-01-11 14:50:10 +01002008 ssl->handshake->randbytes + MBEDTLS_CLIENT_HELLO_RANDOM_LEN;
Jerry Yuf4b27e42022-03-30 17:32:21 +08002009
Gilles Peskine449bd832023-01-11 14:50:10 +01002010 if ((ret = ssl->conf->f_rng(ssl->conf->p_rng, server_randbytes,
2011 MBEDTLS_SERVER_HELLO_RANDOM_LEN)) != 0) {
2012 MBEDTLS_SSL_DEBUG_RET(1, "f_rng", ret);
2013 return ret;
Jerry Yuf4b27e42022-03-30 17:32:21 +08002014 }
2015
Gilles Peskine449bd832023-01-11 14:50:10 +01002016 MBEDTLS_SSL_DEBUG_BUF(3, "server hello, random bytes", server_randbytes,
2017 MBEDTLS_SERVER_HELLO_RANDOM_LEN);
Jerry Yuf4b27e42022-03-30 17:32:21 +08002018
2019#if defined(MBEDTLS_HAVE_TIME)
YxCda609132023-05-22 12:08:12 -07002020 ssl->session_negotiate->start = mbedtls_time(NULL);
Jerry Yuf4b27e42022-03-30 17:32:21 +08002021#endif /* MBEDTLS_HAVE_TIME */
2022
Gilles Peskine449bd832023-01-11 14:50:10 +01002023 return ret;
Jerry Yuf4b27e42022-03-30 17:32:21 +08002024}
2025
Jerry Yu3bf2c642022-03-30 22:02:12 +08002026/*
Jerry Yue74e04a2022-04-21 09:23:16 +08002027 * ssl_tls13_write_server_hello_supported_versions_ext ():
Jerry Yu3bf2c642022-03-30 22:02:12 +08002028 *
2029 * struct {
Jerry Yufb9f54d2022-04-06 10:08:34 +08002030 * ProtocolVersion selected_version;
Jerry Yu3bf2c642022-03-30 22:02:12 +08002031 * } SupportedVersions;
2032 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002033MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yue74e04a2022-04-21 09:23:16 +08002034static int ssl_tls13_write_server_hello_supported_versions_ext(
Gilles Peskine449bd832023-01-11 14:50:10 +01002035 mbedtls_ssl_context *ssl,
2036 unsigned char *buf,
2037 unsigned char *end,
2038 size_t *out_len)
Jerry Yu3bf2c642022-03-30 22:02:12 +08002039{
Jerry Yu3bf2c642022-03-30 22:02:12 +08002040 *out_len = 0;
2041
Gilles Peskine449bd832023-01-11 14:50:10 +01002042 MBEDTLS_SSL_DEBUG_MSG(3, ("server hello, write selected version"));
Jerry Yu3bf2c642022-03-30 22:02:12 +08002043
2044 /* Check if we have space to write the extension:
2045 * - extension_type (2 bytes)
2046 * - extension_data_length (2 bytes)
Jerry Yu349a6132022-04-14 20:52:56 +08002047 * - selected_version (2 bytes)
Jerry Yu3bf2c642022-03-30 22:02:12 +08002048 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002049 MBEDTLS_SSL_CHK_BUF_PTR(buf, end, 6);
Jerry Yu3bf2c642022-03-30 22:02:12 +08002050
Gilles Peskine449bd832023-01-11 14:50:10 +01002051 MBEDTLS_PUT_UINT16_BE(MBEDTLS_TLS_EXT_SUPPORTED_VERSIONS, buf, 0);
Jerry Yu3bf2c642022-03-30 22:02:12 +08002052
Gilles Peskine449bd832023-01-11 14:50:10 +01002053 MBEDTLS_PUT_UINT16_BE(2, buf, 2);
Jerry Yu3bf2c642022-03-30 22:02:12 +08002054
Gilles Peskine449bd832023-01-11 14:50:10 +01002055 mbedtls_ssl_write_version(buf + 4,
2056 ssl->conf->transport,
2057 ssl->tls_version);
Jerry Yu3bf2c642022-03-30 22:02:12 +08002058
Gilles Peskine449bd832023-01-11 14:50:10 +01002059 MBEDTLS_SSL_DEBUG_MSG(3, ("supported version: [%04x]",
2060 ssl->tls_version));
Jerry Yu3bf2c642022-03-30 22:02:12 +08002061
Jerry Yu349a6132022-04-14 20:52:56 +08002062 *out_len = 6;
Jerry Yu3bf2c642022-03-30 22:02:12 +08002063
Jerry Yub95dd362022-11-08 21:19:34 +08002064 mbedtls_ssl_tls13_set_hs_sent_ext_mask(
Gilles Peskine449bd832023-01-11 14:50:10 +01002065 ssl, MBEDTLS_TLS_EXT_SUPPORTED_VERSIONS);
Jerry Yub95dd362022-11-08 21:19:34 +08002066
Gilles Peskine449bd832023-01-11 14:50:10 +01002067 return 0;
Jerry Yu3bf2c642022-03-30 22:02:12 +08002068}
2069
Jerry Yud9436a12022-04-20 22:28:09 +08002070
Jerry Yu3bf2c642022-03-30 22:02:12 +08002071
2072/* Generate and export a single key share. For hybrid KEMs, this can
2073 * be called multiple times with the different components of the hybrid. */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002074MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002075static int ssl_tls13_generate_and_write_key_share(mbedtls_ssl_context *ssl,
2076 uint16_t named_group,
2077 unsigned char *buf,
2078 unsigned char *end,
2079 size_t *out_len)
Jerry Yu3bf2c642022-03-30 22:02:12 +08002080{
2081 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yu955ddd72022-04-22 22:27:33 +08002082
2083 *out_len = 0;
2084
Valerio Settic9ae8622023-07-25 11:23:50 +02002085#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_EPHEMERAL_ENABLED)
Przemek Stekiel29c219c2023-05-31 15:21:04 +02002086 if (mbedtls_ssl_tls13_named_group_is_ecdhe(named_group) ||
Przemek Stekield5f79e72023-06-29 09:08:43 +02002087 mbedtls_ssl_tls13_named_group_is_ffdh(named_group)) {
Przemek Stekiel408569f2023-07-06 11:26:44 +02002088 ret = mbedtls_ssl_tls13_generate_and_write_xxdh_key_exchange(
Gilles Peskine449bd832023-01-11 14:50:10 +01002089 ssl, named_group, buf, end, out_len);
2090 if (ret != 0) {
Jerry Yu89e103c2022-03-30 22:43:29 +08002091 MBEDTLS_SSL_DEBUG_RET(
Przemek Stekiel408569f2023-07-06 11:26:44 +02002092 1, "mbedtls_ssl_tls13_generate_and_write_xxdh_key_exchange",
Gilles Peskine449bd832023-01-11 14:50:10 +01002093 ret);
2094 return ret;
Jerry Yu3bf2c642022-03-30 22:02:12 +08002095 }
Gilles Peskine449bd832023-01-11 14:50:10 +01002096 } else
Valerio Settic9ae8622023-07-25 11:23:50 +02002097#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_EPHEMERAL_ENABLED */
Gilles Peskine449bd832023-01-11 14:50:10 +01002098 if (0 /* Other kinds of KEMs */) {
2099 } else {
Jerry Yu955ddd72022-04-22 22:27:33 +08002100 ((void) ssl);
2101 ((void) named_group);
2102 ((void) buf);
2103 ((void) end);
Jerry Yu3bf2c642022-03-30 22:02:12 +08002104 ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
2105 }
2106
Gilles Peskine449bd832023-01-11 14:50:10 +01002107 return ret;
Jerry Yu3bf2c642022-03-30 22:02:12 +08002108}
2109
2110/*
2111 * ssl_tls13_write_key_share_ext
2112 *
2113 * Structure of key_share extension in ServerHello:
2114 *
Jerry Yu1c3e6882022-04-20 21:23:40 +08002115 * struct {
2116 * NamedGroup group;
2117 * opaque key_exchange<1..2^16-1>;
2118 * } KeyShareEntry;
2119 * struct {
2120 * KeyShareEntry server_share;
2121 * } KeyShareServerHello;
Jerry Yu3bf2c642022-03-30 22:02:12 +08002122 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002123MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002124static int ssl_tls13_write_key_share_ext(mbedtls_ssl_context *ssl,
2125 unsigned char *buf,
2126 unsigned char *end,
2127 size_t *out_len)
Jerry Yu3bf2c642022-03-30 22:02:12 +08002128{
Jerry Yu955ddd72022-04-22 22:27:33 +08002129 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yu3bf2c642022-03-30 22:02:12 +08002130 unsigned char *p = buf;
Jerry Yu57d48412022-04-20 21:50:42 +08002131 uint16_t group = ssl->handshake->offered_group_id;
Jerry Yu3bf2c642022-03-30 22:02:12 +08002132 unsigned char *server_share = buf + 4;
Jerry Yu3bf2c642022-03-30 22:02:12 +08002133 size_t key_exchange_length;
Jerry Yu3bf2c642022-03-30 22:02:12 +08002134
2135 *out_len = 0;
2136
Gilles Peskine449bd832023-01-11 14:50:10 +01002137 MBEDTLS_SSL_DEBUG_MSG(3, ("server hello, adding key share extension"));
Jerry Yu3bf2c642022-03-30 22:02:12 +08002138
Gilles Peskine449bd832023-01-11 14:50:10 +01002139 MBEDTLS_SSL_DEBUG_MSG(2, ("server hello, write selected_group: %s (%04x)",
2140 mbedtls_ssl_named_group_to_str(group),
2141 group));
Jerry Yu58af2332022-09-06 11:19:31 +08002142
Jerry Yu3bf2c642022-03-30 22:02:12 +08002143 /* Check if we have space for header and length fields:
2144 * - extension_type (2 bytes)
2145 * - extension_data_length (2 bytes)
2146 * - group (2 bytes)
2147 * - key_exchange_length (2 bytes)
2148 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002149 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 8);
2150 MBEDTLS_PUT_UINT16_BE(MBEDTLS_TLS_EXT_KEY_SHARE, p, 0);
2151 MBEDTLS_PUT_UINT16_BE(group, server_share, 0);
Jerry Yu3bf2c642022-03-30 22:02:12 +08002152 p += 8;
Jerry Yu57d48412022-04-20 21:50:42 +08002153
Jerry Yu3bf2c642022-03-30 22:02:12 +08002154 /* When we introduce PQC-ECDHE hybrids, we'll want to call this
2155 * function multiple times. */
Jerry Yu955ddd72022-04-22 22:27:33 +08002156 ret = ssl_tls13_generate_and_write_key_share(
Gilles Peskine449bd832023-01-11 14:50:10 +01002157 ssl, group, server_share + 4, end, &key_exchange_length);
2158 if (ret != 0) {
2159 return ret;
2160 }
Jerry Yu3bf2c642022-03-30 22:02:12 +08002161 p += key_exchange_length;
Jerry Yuc1be19f2022-04-23 16:11:39 +08002162
Gilles Peskine449bd832023-01-11 14:50:10 +01002163 MBEDTLS_PUT_UINT16_BE(key_exchange_length, server_share + 2, 0);
Jerry Yu3bf2c642022-03-30 22:02:12 +08002164
Gilles Peskine449bd832023-01-11 14:50:10 +01002165 MBEDTLS_PUT_UINT16_BE(p - server_share, buf, 2);
Jerry Yu3bf2c642022-03-30 22:02:12 +08002166
Jerry Yu57d48412022-04-20 21:50:42 +08002167 *out_len = p - buf;
Jerry Yu955ddd72022-04-22 22:27:33 +08002168
Gilles Peskine449bd832023-01-11 14:50:10 +01002169 mbedtls_ssl_tls13_set_hs_sent_ext_mask(ssl, MBEDTLS_TLS_EXT_KEY_SHARE);
Jerry Yub95dd362022-11-08 21:19:34 +08002170
Gilles Peskine449bd832023-01-11 14:50:10 +01002171 return 0;
Jerry Yu3bf2c642022-03-30 22:02:12 +08002172}
Jerry Yud9436a12022-04-20 22:28:09 +08002173
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002174MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002175static int ssl_tls13_write_hrr_key_share_ext(mbedtls_ssl_context *ssl,
2176 unsigned char *buf,
2177 unsigned char *end,
2178 size_t *out_len)
Jerry Yu23f7a6f2022-04-23 15:16:45 +08002179{
2180 uint16_t selected_group = ssl->handshake->hrr_selected_group;
2181 /* key_share Extension
2182 *
2183 * struct {
2184 * select (Handshake.msg_type) {
2185 * ...
2186 * case hello_retry_request:
2187 * NamedGroup selected_group;
2188 * ...
2189 * };
2190 * } KeyShare;
2191 */
2192
2193 *out_len = 0;
2194
Jerry Yub0ac10b2022-05-05 11:10:08 +08002195 /*
2196 * For a pure PSK key exchange, there is no group to agree upon. The purpose
2197 * of the HRR is then to transmit a cookie to force the client to demonstrate
2198 * reachability at their apparent network address (primarily useful for DTLS).
2199 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002200 if (!mbedtls_ssl_tls13_key_exchange_mode_with_ephemeral(ssl)) {
2201 return 0;
2202 }
Jerry Yu23f7a6f2022-04-23 15:16:45 +08002203
2204 /* We should only send the key_share extension if the client's initial
2205 * key share was not acceptable. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002206 if (ssl->handshake->offered_group_id != 0) {
2207 MBEDTLS_SSL_DEBUG_MSG(4, ("Skip key_share extension in HRR"));
2208 return 0;
Jerry Yu23f7a6f2022-04-23 15:16:45 +08002209 }
2210
Gilles Peskine449bd832023-01-11 14:50:10 +01002211 if (selected_group == 0) {
2212 MBEDTLS_SSL_DEBUG_MSG(1, ("no matching named group found"));
2213 return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
Jerry Yu23f7a6f2022-04-23 15:16:45 +08002214 }
2215
Jerry Yub0ac10b2022-05-05 11:10:08 +08002216 /* Check if we have enough space:
2217 * - extension_type (2 bytes)
2218 * - extension_data_length (2 bytes)
2219 * - selected_group (2 bytes)
2220 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002221 MBEDTLS_SSL_CHK_BUF_PTR(buf, end, 6);
Jerry Yu23f7a6f2022-04-23 15:16:45 +08002222
Gilles Peskine449bd832023-01-11 14:50:10 +01002223 MBEDTLS_PUT_UINT16_BE(MBEDTLS_TLS_EXT_KEY_SHARE, buf, 0);
2224 MBEDTLS_PUT_UINT16_BE(2, buf, 2);
2225 MBEDTLS_PUT_UINT16_BE(selected_group, buf, 4);
Jerry Yu23f7a6f2022-04-23 15:16:45 +08002226
Gilles Peskine449bd832023-01-11 14:50:10 +01002227 MBEDTLS_SSL_DEBUG_MSG(3,
2228 ("HRR selected_group: %s (%x)",
2229 mbedtls_ssl_named_group_to_str(selected_group),
2230 selected_group));
Jerry Yu23f7a6f2022-04-23 15:16:45 +08002231
2232 *out_len = 6;
Jerry Yu23f7a6f2022-04-23 15:16:45 +08002233
Gilles Peskine449bd832023-01-11 14:50:10 +01002234 mbedtls_ssl_tls13_set_hs_sent_ext_mask(ssl, MBEDTLS_TLS_EXT_KEY_SHARE);
Jerry Yub95dd362022-11-08 21:19:34 +08002235
Gilles Peskine449bd832023-01-11 14:50:10 +01002236 return 0;
Jerry Yu23f7a6f2022-04-23 15:16:45 +08002237}
Jerry Yu3bf2c642022-03-30 22:02:12 +08002238
2239/*
2240 * Structure of ServerHello message:
2241 *
2242 * struct {
2243 * ProtocolVersion legacy_version = 0x0303; // TLS v1.2
2244 * Random random;
2245 * opaque legacy_session_id_echo<0..32>;
2246 * CipherSuite cipher_suite;
2247 * uint8 legacy_compression_method = 0;
2248 * Extension extensions<6..2^16-1>;
2249 * } ServerHello;
2250 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002251MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002252static int ssl_tls13_write_server_hello_body(mbedtls_ssl_context *ssl,
2253 unsigned char *buf,
2254 unsigned char *end,
2255 size_t *out_len,
2256 int is_hrr)
Jerry Yu56404d72022-03-30 17:36:13 +08002257{
Jerry Yu955ddd72022-04-22 22:27:33 +08002258 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yu3bf2c642022-03-30 22:02:12 +08002259 unsigned char *p = buf;
Jerry Yud9436a12022-04-20 22:28:09 +08002260 unsigned char *p_extensions_len;
Jerry Yufbe3e642022-04-25 19:31:51 +08002261 size_t output_len;
Jerry Yu3bf2c642022-03-30 22:02:12 +08002262
2263 *out_len = 0;
Jerry Yu50e00e32022-10-31 14:45:01 +08002264 ssl->handshake->sent_extensions = MBEDTLS_SSL_EXT_MASK_NONE;
Jerry Yu3bf2c642022-03-30 22:02:12 +08002265
Jerry Yucfc04b32022-04-21 09:31:58 +08002266 /* ...
2267 * ProtocolVersion legacy_version = 0x0303; // TLS 1.2
2268 * ...
2269 * with ProtocolVersion defined as:
2270 * uint16 ProtocolVersion;
Jerry Yu3bf2c642022-03-30 22:02:12 +08002271 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002272 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 2);
2273 MBEDTLS_PUT_UINT16_BE(0x0303, p, 0);
Jerry Yu3bf2c642022-03-30 22:02:12 +08002274 p += 2;
2275
Jerry Yu1c3e6882022-04-20 21:23:40 +08002276 /* ...
2277 * Random random;
2278 * ...
Jerry Yucfc04b32022-04-21 09:31:58 +08002279 * with Random defined as:
2280 * opaque Random[MBEDTLS_SERVER_HELLO_RANDOM_LEN];
Jerry Yu1c3e6882022-04-20 21:23:40 +08002281 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002282 MBEDTLS_SSL_CHK_BUF_PTR(p, end, MBEDTLS_SERVER_HELLO_RANDOM_LEN);
2283 if (is_hrr) {
2284 memcpy(p, mbedtls_ssl_tls13_hello_retry_request_magic,
2285 MBEDTLS_SERVER_HELLO_RANDOM_LEN);
2286 } else {
2287 memcpy(p, &ssl->handshake->randbytes[MBEDTLS_CLIENT_HELLO_RANDOM_LEN],
2288 MBEDTLS_SERVER_HELLO_RANDOM_LEN);
Jerry Yu23f7a6f2022-04-23 15:16:45 +08002289 }
Gilles Peskine449bd832023-01-11 14:50:10 +01002290 MBEDTLS_SSL_DEBUG_BUF(3, "server hello, random bytes",
2291 p, MBEDTLS_SERVER_HELLO_RANDOM_LEN);
Jerry Yu3bf2c642022-03-30 22:02:12 +08002292 p += MBEDTLS_SERVER_HELLO_RANDOM_LEN;
2293
Jerry Yucfc04b32022-04-21 09:31:58 +08002294 /* ...
2295 * opaque legacy_session_id_echo<0..32>;
2296 * ...
Jerry Yu3bf2c642022-03-30 22:02:12 +08002297 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002298 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 1 + ssl->session_negotiate->id_len);
2299 *p++ = (unsigned char) ssl->session_negotiate->id_len;
2300 if (ssl->session_negotiate->id_len > 0) {
2301 memcpy(p, &ssl->session_negotiate->id[0],
2302 ssl->session_negotiate->id_len);
Jerry Yu3bf2c642022-03-30 22:02:12 +08002303 p += ssl->session_negotiate->id_len;
Jerry Yu955ddd72022-04-22 22:27:33 +08002304
Gilles Peskine449bd832023-01-11 14:50:10 +01002305 MBEDTLS_SSL_DEBUG_BUF(3, "session id", ssl->session_negotiate->id,
2306 ssl->session_negotiate->id_len);
Jerry Yu3bf2c642022-03-30 22:02:12 +08002307 }
2308
Jerry Yucfc04b32022-04-21 09:31:58 +08002309 /* ...
2310 * CipherSuite cipher_suite;
2311 * ...
2312 * with CipherSuite defined as:
2313 * uint8 CipherSuite[2];
Jerry Yu3bf2c642022-03-30 22:02:12 +08002314 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002315 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 2);
2316 MBEDTLS_PUT_UINT16_BE(ssl->session_negotiate->ciphersuite, p, 0);
Jerry Yu3bf2c642022-03-30 22:02:12 +08002317 p += 2;
Gilles Peskine449bd832023-01-11 14:50:10 +01002318 MBEDTLS_SSL_DEBUG_MSG(3,
2319 ("server hello, chosen ciphersuite: %s ( id=%d )",
2320 mbedtls_ssl_get_ciphersuite_name(
2321 ssl->session_negotiate->ciphersuite),
2322 ssl->session_negotiate->ciphersuite));
Jerry Yu3bf2c642022-03-30 22:02:12 +08002323
Jerry Yucfc04b32022-04-21 09:31:58 +08002324 /* ...
2325 * uint8 legacy_compression_method = 0;
2326 * ...
2327 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002328 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 1);
Thomas Daubney31e03a82022-07-25 15:59:25 +01002329 *p++ = MBEDTLS_SSL_COMPRESS_NULL;
Jerry Yu3bf2c642022-03-30 22:02:12 +08002330
Jerry Yucfc04b32022-04-21 09:31:58 +08002331 /* ...
2332 * Extension extensions<6..2^16-1>;
2333 * ...
2334 * struct {
2335 * ExtensionType extension_type; (2 bytes)
2336 * opaque extension_data<0..2^16-1>;
2337 * } Extension;
2338 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002339 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 2);
Jerry Yud9436a12022-04-20 22:28:09 +08002340 p_extensions_len = p;
Jerry Yu3bf2c642022-03-30 22:02:12 +08002341 p += 2;
2342
Gilles Peskine449bd832023-01-11 14:50:10 +01002343 if ((ret = ssl_tls13_write_server_hello_supported_versions_ext(
2344 ssl, p, end, &output_len)) != 0) {
Jerry Yu955ddd72022-04-22 22:27:33 +08002345 MBEDTLS_SSL_DEBUG_RET(
Gilles Peskine449bd832023-01-11 14:50:10 +01002346 1, "ssl_tls13_write_server_hello_supported_versions_ext", ret);
2347 return ret;
Jerry Yu3bf2c642022-03-30 22:02:12 +08002348 }
2349 p += output_len;
2350
Gilles Peskine449bd832023-01-11 14:50:10 +01002351 if (mbedtls_ssl_tls13_key_exchange_mode_with_ephemeral(ssl)) {
2352 if (is_hrr) {
2353 ret = ssl_tls13_write_hrr_key_share_ext(ssl, p, end, &output_len);
2354 } else {
2355 ret = ssl_tls13_write_key_share_ext(ssl, p, end, &output_len);
2356 }
2357 if (ret != 0) {
2358 return ret;
2359 }
Jerry Yu3bf2c642022-03-30 22:02:12 +08002360 p += output_len;
2361 }
Jerry Yu3bf2c642022-03-30 22:02:12 +08002362
Ronald Cron41a443a2022-10-04 16:38:25 +02002363#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01002364 if (!is_hrr && mbedtls_ssl_tls13_key_exchange_mode_with_psk(ssl)) {
2365 ret = ssl_tls13_write_server_pre_shared_key_ext(ssl, p, end, &output_len);
2366 if (ret != 0) {
2367 MBEDTLS_SSL_DEBUG_RET(1, "ssl_tls13_write_server_pre_shared_key_ext",
2368 ret);
2369 return ret;
Jerry Yu032b15ce2022-07-11 06:10:03 +00002370 }
2371 p += output_len;
2372 }
Ronald Cron41a443a2022-10-04 16:38:25 +02002373#endif
Jerry Yu032b15ce2022-07-11 06:10:03 +00002374
Gilles Peskine449bd832023-01-11 14:50:10 +01002375 MBEDTLS_PUT_UINT16_BE(p - p_extensions_len - 2, p_extensions_len, 0);
Jerry Yu3bf2c642022-03-30 22:02:12 +08002376
Gilles Peskine449bd832023-01-11 14:50:10 +01002377 MBEDTLS_SSL_DEBUG_BUF(4, "server hello extensions",
2378 p_extensions_len, p - p_extensions_len);
Jerry Yu3bf2c642022-03-30 22:02:12 +08002379
Jerry Yud9436a12022-04-20 22:28:09 +08002380 *out_len = p - buf;
Jerry Yu3bf2c642022-03-30 22:02:12 +08002381
Gilles Peskine449bd832023-01-11 14:50:10 +01002382 MBEDTLS_SSL_DEBUG_BUF(3, "server hello", buf, *out_len);
Jerry Yu3bf2c642022-03-30 22:02:12 +08002383
Jerry Yu7de2ff02022-11-08 21:43:46 +08002384 MBEDTLS_SSL_PRINT_EXTS(
Jerry Yu4b8f2f72022-10-31 13:31:22 +08002385 3, is_hrr ? MBEDTLS_SSL_TLS1_3_HS_HELLO_RETRY_REQUEST :
Gilles Peskine449bd832023-01-11 14:50:10 +01002386 MBEDTLS_SSL_HS_SERVER_HELLO,
2387 ssl->handshake->sent_extensions);
Jerry Yu4b8f2f72022-10-31 13:31:22 +08002388
Gilles Peskine449bd832023-01-11 14:50:10 +01002389 return ret;
Jerry Yu56404d72022-03-30 17:36:13 +08002390}
2391
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002392MBEDTLS_CHECK_RETURN_CRITICAL
Xiaokang Qian934ce6f2023-02-06 10:23:04 +00002393static int ssl_tls13_finalize_server_hello(mbedtls_ssl_context *ssl)
Jerry Yue110d252022-05-05 10:19:22 +08002394{
2395 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine449bd832023-01-11 14:50:10 +01002396 ret = mbedtls_ssl_tls13_compute_handshake_transform(ssl);
2397 if (ret != 0) {
2398 MBEDTLS_SSL_DEBUG_RET(1,
2399 "mbedtls_ssl_tls13_compute_handshake_transform",
2400 ret);
2401 return ret;
Jerry Yue110d252022-05-05 10:19:22 +08002402 }
2403
Gilles Peskine449bd832023-01-11 14:50:10 +01002404 return ret;
Jerry Yue110d252022-05-05 10:19:22 +08002405}
2406
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002407MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002408static int ssl_tls13_write_server_hello(mbedtls_ssl_context *ssl)
Jerry Yu5b64ae92022-03-30 17:15:02 +08002409{
Jerry Yu637a3f12022-04-20 21:37:58 +08002410 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yuf4b27e42022-03-30 17:32:21 +08002411 unsigned char *buf;
2412 size_t buf_len, msg_len;
2413
Gilles Peskine449bd832023-01-11 14:50:10 +01002414 MBEDTLS_SSL_DEBUG_MSG(2, ("=> write server hello"));
Jerry Yuf4b27e42022-03-30 17:32:21 +08002415
Gilles Peskine449bd832023-01-11 14:50:10 +01002416 MBEDTLS_SSL_PROC_CHK(ssl_tls13_prepare_server_hello(ssl));
Jerry Yuf4b27e42022-03-30 17:32:21 +08002417
Xiaokang Qian9f1747b2023-03-30 02:50:04 +00002418 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_start_handshake_msg(
2419 ssl, MBEDTLS_SSL_HS_SERVER_HELLO, &buf, &buf_len));
Jerry Yuf4b27e42022-03-30 17:32:21 +08002420
Gilles Peskine449bd832023-01-11 14:50:10 +01002421 MBEDTLS_SSL_PROC_CHK(ssl_tls13_write_server_hello_body(ssl, buf,
2422 buf + buf_len,
2423 &msg_len,
2424 0));
Jerry Yuf4b27e42022-03-30 17:32:21 +08002425
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +01002426 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_add_hs_msg_to_checksum(
Manuel Pégourié-Gonnard43cc1272023-02-06 11:48:19 +01002427 ssl, MBEDTLS_SSL_HS_SERVER_HELLO, buf, msg_len));
Jerry Yuf4b27e42022-03-30 17:32:21 +08002428
Gilles Peskine449bd832023-01-11 14:50:10 +01002429 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_finish_handshake_msg(
2430 ssl, buf_len, msg_len));
Jerry Yu637a3f12022-04-20 21:37:58 +08002431
Xiaokang Qian934ce6f2023-02-06 10:23:04 +00002432 MBEDTLS_SSL_PROC_CHK(ssl_tls13_finalize_server_hello(ssl));
Jerry Yue110d252022-05-05 10:19:22 +08002433
Gabor Mezei7b39bf12022-05-24 16:04:14 +02002434#if defined(MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE)
2435 /* The server sends a dummy change_cipher_spec record immediately
2436 * after its first handshake message. This may either be after
2437 * a ServerHello or a HelloRetryRequest.
2438 */
Gabor Mezei96ae9262022-06-28 11:45:18 +02002439 mbedtls_ssl_handshake_set_state(
Gilles Peskine449bd832023-01-11 14:50:10 +01002440 ssl, MBEDTLS_SSL_SERVER_CCS_AFTER_SERVER_HELLO);
Gabor Mezei7b39bf12022-05-24 16:04:14 +02002441#else
Gilles Peskine449bd832023-01-11 14:50:10 +01002442 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_ENCRYPTED_EXTENSIONS);
Gabor Mezei7b39bf12022-05-24 16:04:14 +02002443#endif /* MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE */
Jerry Yue110d252022-05-05 10:19:22 +08002444
Jerry Yuf4b27e42022-03-30 17:32:21 +08002445cleanup:
2446
Gilles Peskine449bd832023-01-11 14:50:10 +01002447 MBEDTLS_SSL_DEBUG_MSG(2, ("<= write server hello"));
2448 return ret;
Jerry Yu5b64ae92022-03-30 17:15:02 +08002449}
2450
Jerry Yu23d1a252022-05-12 18:08:59 +08002451
2452/*
2453 * Handler for MBEDTLS_SSL_HELLO_RETRY_REQUEST
2454 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002455MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002456static int ssl_tls13_prepare_hello_retry_request(mbedtls_ssl_context *ssl)
Jerry Yu23d1a252022-05-12 18:08:59 +08002457{
2458 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Ronald Cron5fbd2702024-02-14 10:03:36 +01002459 if (ssl->handshake->hello_retry_request_flag) {
Gilles Peskine449bd832023-01-11 14:50:10 +01002460 MBEDTLS_SSL_DEBUG_MSG(1, ("Too many HRRs"));
2461 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE,
2462 MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE);
2463 return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
Jerry Yu23d1a252022-05-12 18:08:59 +08002464 }
2465
2466 /*
2467 * Create stateless transcript hash for HRR
2468 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002469 MBEDTLS_SSL_DEBUG_MSG(4, ("Reset transcript for HRR"));
2470 ret = mbedtls_ssl_reset_transcript_for_hrr(ssl);
2471 if (ret != 0) {
2472 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_reset_transcript_for_hrr", ret);
2473 return ret;
Jerry Yu23d1a252022-05-12 18:08:59 +08002474 }
Gilles Peskine449bd832023-01-11 14:50:10 +01002475 mbedtls_ssl_session_reset_msg_layer(ssl, 0);
Jerry Yu23d1a252022-05-12 18:08:59 +08002476
Gilles Peskine449bd832023-01-11 14:50:10 +01002477 return 0;
Jerry Yu23d1a252022-05-12 18:08:59 +08002478}
2479
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002480MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002481static int ssl_tls13_write_hello_retry_request(mbedtls_ssl_context *ssl)
Jerry Yu23d1a252022-05-12 18:08:59 +08002482{
2483 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
2484 unsigned char *buf;
2485 size_t buf_len, msg_len;
2486
Gilles Peskine449bd832023-01-11 14:50:10 +01002487 MBEDTLS_SSL_DEBUG_MSG(2, ("=> write hello retry request"));
Jerry Yu23d1a252022-05-12 18:08:59 +08002488
Gilles Peskine449bd832023-01-11 14:50:10 +01002489 MBEDTLS_SSL_PROC_CHK(ssl_tls13_prepare_hello_retry_request(ssl));
Jerry Yu23d1a252022-05-12 18:08:59 +08002490
Gilles Peskine449bd832023-01-11 14:50:10 +01002491 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_start_handshake_msg(
2492 ssl, MBEDTLS_SSL_HS_SERVER_HELLO,
2493 &buf, &buf_len));
Jerry Yu23d1a252022-05-12 18:08:59 +08002494
Gilles Peskine449bd832023-01-11 14:50:10 +01002495 MBEDTLS_SSL_PROC_CHK(ssl_tls13_write_server_hello_body(ssl, buf,
2496 buf + buf_len,
2497 &msg_len,
2498 1));
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +01002499 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_add_hs_msg_to_checksum(
Manuel Pégourié-Gonnard43cc1272023-02-06 11:48:19 +01002500 ssl, MBEDTLS_SSL_HS_SERVER_HELLO, buf, msg_len));
Jerry Yu23d1a252022-05-12 18:08:59 +08002501
2502
Gilles Peskine449bd832023-01-11 14:50:10 +01002503 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_finish_handshake_msg(ssl, buf_len,
2504 msg_len));
Jerry Yu23d1a252022-05-12 18:08:59 +08002505
Ronald Cron5fbd2702024-02-14 10:03:36 +01002506 ssl->handshake->hello_retry_request_flag = 1;
Jerry Yu23d1a252022-05-12 18:08:59 +08002507
Gabor Mezei7b39bf12022-05-24 16:04:14 +02002508#if defined(MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE)
2509 /* The server sends a dummy change_cipher_spec record immediately
2510 * after its first handshake message. This may either be after
2511 * a ServerHello or a HelloRetryRequest.
2512 */
Gabor Mezei96ae9262022-06-28 11:45:18 +02002513 mbedtls_ssl_handshake_set_state(
Gilles Peskine449bd832023-01-11 14:50:10 +01002514 ssl, MBEDTLS_SSL_SERVER_CCS_AFTER_HELLO_RETRY_REQUEST);
Gabor Mezei7b39bf12022-05-24 16:04:14 +02002515#else
Gilles Peskine449bd832023-01-11 14:50:10 +01002516 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_CLIENT_HELLO);
Gabor Mezei7b39bf12022-05-24 16:04:14 +02002517#endif /* MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE */
Jerry Yu23d1a252022-05-12 18:08:59 +08002518
2519cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +01002520 MBEDTLS_SSL_DEBUG_MSG(2, ("<= write hello retry request"));
2521 return ret;
Jerry Yu23d1a252022-05-12 18:08:59 +08002522}
2523
Jerry Yu5b64ae92022-03-30 17:15:02 +08002524/*
Jerry Yu4d3841a2022-04-16 12:37:19 +08002525 * Handler for MBEDTLS_SSL_ENCRYPTED_EXTENSIONS
2526 */
Jerry Yu4d3841a2022-04-16 12:37:19 +08002527
2528/*
2529 * struct {
2530 * Extension extensions<0..2 ^ 16 - 1>;
2531 * } EncryptedExtensions;
2532 *
2533 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002534MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002535static int ssl_tls13_write_encrypted_extensions_body(mbedtls_ssl_context *ssl,
2536 unsigned char *buf,
2537 unsigned char *end,
2538 size_t *out_len)
Jerry Yu4d3841a2022-04-16 12:37:19 +08002539{
XiaokangQianacb39922022-06-17 10:18:48 +00002540 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yu4d3841a2022-04-16 12:37:19 +08002541 unsigned char *p = buf;
2542 size_t extensions_len = 0;
Jerry Yu8937eb42022-05-03 12:12:14 +08002543 unsigned char *p_extensions_len;
XiaokangQianacb39922022-06-17 10:18:48 +00002544 size_t output_len;
Jerry Yu9da5e5a2022-05-03 15:46:09 +08002545
Jerry Yu4d3841a2022-04-16 12:37:19 +08002546 *out_len = 0;
2547
Gilles Peskine449bd832023-01-11 14:50:10 +01002548 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 2);
Jerry Yu8937eb42022-05-03 12:12:14 +08002549 p_extensions_len = p;
Jerry Yu4d3841a2022-04-16 12:37:19 +08002550 p += 2;
2551
Jerry Yu8937eb42022-05-03 12:12:14 +08002552 ((void) ssl);
XiaokangQianacb39922022-06-17 10:18:48 +00002553 ((void) ret);
2554 ((void) output_len);
2555
2556#if defined(MBEDTLS_SSL_ALPN)
Gilles Peskine449bd832023-01-11 14:50:10 +01002557 ret = mbedtls_ssl_write_alpn_ext(ssl, p, end, &output_len);
2558 if (ret != 0) {
2559 return ret;
2560 }
XiaokangQian95d5f542022-06-24 02:29:26 +00002561 p += output_len;
XiaokangQianacb39922022-06-17 10:18:48 +00002562#endif /* MBEDTLS_SSL_ALPN */
Jerry Yu4d3841a2022-04-16 12:37:19 +08002563
Jerry Yu71c14f12022-12-12 11:10:35 +08002564#if defined(MBEDTLS_SSL_EARLY_DATA)
Ronald Cron78a38f62024-02-01 18:30:31 +01002565 if (ssl->handshake->early_data_accepted) {
Jerry Yu52335392023-11-23 18:06:06 +08002566 ret = mbedtls_ssl_tls13_write_early_data_ext(
Jerry Yuc59c5862023-12-05 10:40:49 +08002567 ssl, 0, p, end, &output_len);
Jerry Yu71c14f12022-12-12 11:10:35 +08002568 if (ret != 0) {
2569 return ret;
2570 }
2571 p += output_len;
2572 }
2573#endif /* MBEDTLS_SSL_EARLY_DATA */
2574
Waleed Elmelegy47d29462024-01-03 17:31:52 +00002575#if defined(MBEDTLS_SSL_RECORD_SIZE_LIMIT)
Waleed Elmelegyfbe42742024-01-05 18:11:10 +00002576 if (ssl->handshake->received_extensions & MBEDTLS_SSL_EXT_MASK(RECORD_SIZE_LIMIT)) {
Waleed Elmelegyd2fc90e2024-01-04 18:04:53 +00002577 ret = mbedtls_ssl_tls13_write_record_size_limit_ext(
2578 ssl, p, end, &output_len);
2579 if (ret != 0) {
2580 return ret;
2581 }
2582 p += output_len;
Waleed Elmelegy47d29462024-01-03 17:31:52 +00002583 }
Waleed Elmelegy47d29462024-01-03 17:31:52 +00002584#endif
2585
Gilles Peskine449bd832023-01-11 14:50:10 +01002586 extensions_len = (p - p_extensions_len) - 2;
2587 MBEDTLS_PUT_UINT16_BE(extensions_len, p_extensions_len, 0);
Jerry Yu8937eb42022-05-03 12:12:14 +08002588
2589 *out_len = p - buf;
Jerry Yu4d3841a2022-04-16 12:37:19 +08002590
Gilles Peskine449bd832023-01-11 14:50:10 +01002591 MBEDTLS_SSL_DEBUG_BUF(4, "encrypted extensions", buf, *out_len);
Jerry Yu4d3841a2022-04-16 12:37:19 +08002592
Jerry Yu7de2ff02022-11-08 21:43:46 +08002593 MBEDTLS_SSL_PRINT_EXTS(
Gilles Peskine449bd832023-01-11 14:50:10 +01002594 3, MBEDTLS_SSL_HS_ENCRYPTED_EXTENSIONS, ssl->handshake->sent_extensions);
Jerry Yu4b8f2f72022-10-31 13:31:22 +08002595
Gilles Peskine449bd832023-01-11 14:50:10 +01002596 return 0;
Jerry Yu4d3841a2022-04-16 12:37:19 +08002597}
2598
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002599MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002600static int ssl_tls13_write_encrypted_extensions(mbedtls_ssl_context *ssl)
Jerry Yu4d3841a2022-04-16 12:37:19 +08002601{
2602 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
2603 unsigned char *buf;
Jerry Yu39730a72022-05-03 12:14:04 +08002604 size_t buf_len, msg_len;
Jerry Yu4d3841a2022-04-16 12:37:19 +08002605
Gilles Peskine449bd832023-01-11 14:50:10 +01002606 mbedtls_ssl_set_outbound_transform(ssl,
2607 ssl->handshake->transform_handshake);
Gabor Mezei54719122022-06-28 11:34:56 +02002608 MBEDTLS_SSL_DEBUG_MSG(
Gilles Peskine449bd832023-01-11 14:50:10 +01002609 3, ("switching to handshake transform for outbound data"));
Gabor Mezei54719122022-06-28 11:34:56 +02002610
Gilles Peskine449bd832023-01-11 14:50:10 +01002611 MBEDTLS_SSL_DEBUG_MSG(2, ("=> write encrypted extensions"));
Jerry Yu4d3841a2022-04-16 12:37:19 +08002612
Xiaokang Qian9f1747b2023-03-30 02:50:04 +00002613 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_start_handshake_msg(
2614 ssl, MBEDTLS_SSL_HS_ENCRYPTED_EXTENSIONS,
2615 &buf, &buf_len));
Jerry Yu4d3841a2022-04-16 12:37:19 +08002616
Gilles Peskine449bd832023-01-11 14:50:10 +01002617 MBEDTLS_SSL_PROC_CHK(ssl_tls13_write_encrypted_extensions_body(
2618 ssl, buf, buf + buf_len, &msg_len));
Jerry Yu4d3841a2022-04-16 12:37:19 +08002619
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +01002620 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_add_hs_msg_to_checksum(
Xiaokang Qian9f1747b2023-03-30 02:50:04 +00002621 ssl, MBEDTLS_SSL_HS_ENCRYPTED_EXTENSIONS,
2622 buf, msg_len));
Jerry Yu4d3841a2022-04-16 12:37:19 +08002623
Gilles Peskine449bd832023-01-11 14:50:10 +01002624 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_finish_handshake_msg(
2625 ssl, buf_len, msg_len));
Jerry Yu4d3841a2022-04-16 12:37:19 +08002626
Ronald Cron928cbd32022-10-04 16:14:26 +02002627#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01002628 if (mbedtls_ssl_tls13_key_exchange_mode_with_psk(ssl)) {
2629 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_SERVER_FINISHED);
2630 } else {
2631 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_CERTIFICATE_REQUEST);
2632 }
Jerry Yu8937eb42022-05-03 12:12:14 +08002633#else
Gilles Peskine449bd832023-01-11 14:50:10 +01002634 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_SERVER_FINISHED);
Jerry Yu8937eb42022-05-03 12:12:14 +08002635#endif
2636
Jerry Yu4d3841a2022-04-16 12:37:19 +08002637cleanup:
2638
Gilles Peskine449bd832023-01-11 14:50:10 +01002639 MBEDTLS_SSL_DEBUG_MSG(2, ("<= write encrypted extensions"));
2640 return ret;
Jerry Yu4d3841a2022-04-16 12:37:19 +08002641}
2642
Ronald Cron928cbd32022-10-04 16:14:26 +02002643#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED)
XiaokangQiana987e1d2022-05-07 01:25:58 +00002644#define SSL_CERTIFICATE_REQUEST_SEND_REQUEST 0
2645#define SSL_CERTIFICATE_REQUEST_SKIP 1
2646/* Coordination:
2647 * Check whether a CertificateRequest message should be written.
2648 * Returns a negative code on failure, or
2649 * - SSL_CERTIFICATE_REQUEST_SEND_REQUEST
2650 * - SSL_CERTIFICATE_REQUEST_SKIP
2651 * indicating if the writing of the CertificateRequest
2652 * should be skipped or not.
2653 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002654MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002655static int ssl_tls13_certificate_request_coordinate(mbedtls_ssl_context *ssl)
XiaokangQiana987e1d2022-05-07 01:25:58 +00002656{
2657 int authmode;
2658
XiaokangQian40a35232022-05-07 09:02:40 +00002659#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Gilles Peskine449bd832023-01-11 14:50:10 +01002660 if (ssl->handshake->sni_authmode != MBEDTLS_SSL_VERIFY_UNSET) {
XiaokangQian40a35232022-05-07 09:02:40 +00002661 authmode = ssl->handshake->sni_authmode;
Gilles Peskine449bd832023-01-11 14:50:10 +01002662 } else
XiaokangQian40a35232022-05-07 09:02:40 +00002663#endif
XiaokangQiana987e1d2022-05-07 01:25:58 +00002664 authmode = ssl->conf->authmode;
2665
Gilles Peskine449bd832023-01-11 14:50:10 +01002666 if (authmode == MBEDTLS_SSL_VERIFY_NONE) {
Ronald Croneac00ad2022-09-13 10:16:31 +02002667 ssl->session_negotiate->verify_result = MBEDTLS_X509_BADCERT_SKIP_VERIFY;
Gilles Peskine449bd832023-01-11 14:50:10 +01002668 return SSL_CERTIFICATE_REQUEST_SKIP;
Ronald Croneac00ad2022-09-13 10:16:31 +02002669 }
XiaokangQiana987e1d2022-05-07 01:25:58 +00002670
XiaokangQianc3017f62022-05-13 05:55:41 +00002671 ssl->handshake->certificate_request_sent = 1;
XiaokangQian189ded22022-05-10 08:12:17 +00002672
Gilles Peskine449bd832023-01-11 14:50:10 +01002673 return SSL_CERTIFICATE_REQUEST_SEND_REQUEST;
XiaokangQiana987e1d2022-05-07 01:25:58 +00002674}
2675
XiaokangQiancec9ae62022-05-06 07:28:50 +00002676/*
2677 * struct {
2678 * opaque certificate_request_context<0..2^8-1>;
2679 * Extension extensions<2..2^16-1>;
2680 * } CertificateRequest;
2681 *
2682 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002683MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002684static int ssl_tls13_write_certificate_request_body(mbedtls_ssl_context *ssl,
2685 unsigned char *buf,
2686 const unsigned char *end,
2687 size_t *out_len)
XiaokangQiancec9ae62022-05-06 07:28:50 +00002688{
2689 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
2690 unsigned char *p = buf;
XiaokangQianec6efb92022-05-06 09:53:10 +00002691 size_t output_len = 0;
XiaokangQiancec9ae62022-05-06 07:28:50 +00002692 unsigned char *p_extensions_len;
2693
2694 *out_len = 0;
2695
2696 /* Check if we have enough space:
2697 * - certificate_request_context (1 byte)
2698 * - extensions length (2 bytes)
2699 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002700 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 3);
XiaokangQiancec9ae62022-05-06 07:28:50 +00002701
2702 /*
2703 * Write certificate_request_context
2704 */
2705 /*
2706 * We use a zero length context for the normal handshake
2707 * messages. For post-authentication handshake messages
2708 * this request context would be set to a non-zero value.
2709 */
2710 *p++ = 0x0;
2711
2712 /*
2713 * Write extensions
2714 */
2715 /* The extensions must contain the signature_algorithms. */
2716 p_extensions_len = p;
2717 p += 2;
Gilles Peskine449bd832023-01-11 14:50:10 +01002718 ret = mbedtls_ssl_write_sig_alg_ext(ssl, p, end, &output_len);
2719 if (ret != 0) {
2720 return ret;
2721 }
XiaokangQiancec9ae62022-05-06 07:28:50 +00002722
XiaokangQianec6efb92022-05-06 09:53:10 +00002723 p += output_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01002724 MBEDTLS_PUT_UINT16_BE(p - p_extensions_len - 2, p_extensions_len, 0);
XiaokangQiancec9ae62022-05-06 07:28:50 +00002725
2726 *out_len = p - buf;
2727
Jerry Yu7de2ff02022-11-08 21:43:46 +08002728 MBEDTLS_SSL_PRINT_EXTS(
Gilles Peskine449bd832023-01-11 14:50:10 +01002729 3, MBEDTLS_SSL_HS_CERTIFICATE_REQUEST, ssl->handshake->sent_extensions);
Jerry Yu4b8f2f72022-10-31 13:31:22 +08002730
Gilles Peskine449bd832023-01-11 14:50:10 +01002731 return 0;
XiaokangQiancec9ae62022-05-06 07:28:50 +00002732}
2733
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002734MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002735static int ssl_tls13_write_certificate_request(mbedtls_ssl_context *ssl)
XiaokangQiancec9ae62022-05-06 07:28:50 +00002736{
2737 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
2738
Gilles Peskine449bd832023-01-11 14:50:10 +01002739 MBEDTLS_SSL_DEBUG_MSG(2, ("=> write certificate request"));
XiaokangQiancec9ae62022-05-06 07:28:50 +00002740
Gilles Peskine449bd832023-01-11 14:50:10 +01002741 MBEDTLS_SSL_PROC_CHK_NEG(ssl_tls13_certificate_request_coordinate(ssl));
XiaokangQiancec9ae62022-05-06 07:28:50 +00002742
Gilles Peskine449bd832023-01-11 14:50:10 +01002743 if (ret == SSL_CERTIFICATE_REQUEST_SEND_REQUEST) {
XiaokangQiancec9ae62022-05-06 07:28:50 +00002744 unsigned char *buf;
2745 size_t buf_len, msg_len;
2746
Xiaokang Qian9f1747b2023-03-30 02:50:04 +00002747 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_start_handshake_msg(
2748 ssl, MBEDTLS_SSL_HS_CERTIFICATE_REQUEST,
2749 &buf, &buf_len));
XiaokangQiancec9ae62022-05-06 07:28:50 +00002750
Gilles Peskine449bd832023-01-11 14:50:10 +01002751 MBEDTLS_SSL_PROC_CHK(ssl_tls13_write_certificate_request_body(
2752 ssl, buf, buf + buf_len, &msg_len));
XiaokangQiancec9ae62022-05-06 07:28:50 +00002753
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +01002754 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_add_hs_msg_to_checksum(
Xiaokang Qian9f1747b2023-03-30 02:50:04 +00002755 ssl, MBEDTLS_SSL_HS_CERTIFICATE_REQUEST,
2756 buf, msg_len));
XiaokangQiancec9ae62022-05-06 07:28:50 +00002757
Gilles Peskine449bd832023-01-11 14:50:10 +01002758 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_finish_handshake_msg(
2759 ssl, buf_len, msg_len));
2760 } else if (ret == SSL_CERTIFICATE_REQUEST_SKIP) {
2761 MBEDTLS_SSL_DEBUG_MSG(2, ("<= skip write certificate request"));
XiaokangQiancec9ae62022-05-06 07:28:50 +00002762 ret = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01002763 } else {
2764 MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
XiaokangQiancec9ae62022-05-06 07:28:50 +00002765 ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
2766 goto cleanup;
2767 }
2768
Gilles Peskine449bd832023-01-11 14:50:10 +01002769 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_SERVER_CERTIFICATE);
XiaokangQiancec9ae62022-05-06 07:28:50 +00002770cleanup:
2771
Gilles Peskine449bd832023-01-11 14:50:10 +01002772 MBEDTLS_SSL_DEBUG_MSG(2, ("<= write certificate request"));
2773 return ret;
XiaokangQiancec9ae62022-05-06 07:28:50 +00002774}
XiaokangQiancec9ae62022-05-06 07:28:50 +00002775
Jerry Yu4d3841a2022-04-16 12:37:19 +08002776/*
Jerry Yuc6e6dbf2022-04-16 19:42:57 +08002777 * Handler for MBEDTLS_SSL_SERVER_CERTIFICATE
Jerry Yu83da34e2022-04-16 13:59:52 +08002778 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002779MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002780static int ssl_tls13_write_server_certificate(mbedtls_ssl_context *ssl)
Jerry Yu83da34e2022-04-16 13:59:52 +08002781{
Jerry Yu5a26f302022-05-10 20:46:40 +08002782 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
XiaokangQianfb665a82022-06-15 03:57:21 +00002783
2784#if defined(MBEDTLS_X509_CRT_PARSE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01002785 if ((ssl_tls13_pick_key_cert(ssl) != 0) ||
2786 mbedtls_ssl_own_cert(ssl) == NULL) {
2787 MBEDTLS_SSL_DEBUG_MSG(2, ("No certificate available."));
2788 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE,
2789 MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE);
2790 return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
Jerry Yu5a26f302022-05-10 20:46:40 +08002791 }
XiaokangQianfb665a82022-06-15 03:57:21 +00002792#endif /* MBEDTLS_X509_CRT_PARSE_C */
Jerry Yu5a26f302022-05-10 20:46:40 +08002793
Gilles Peskine449bd832023-01-11 14:50:10 +01002794 ret = mbedtls_ssl_tls13_write_certificate(ssl);
2795 if (ret != 0) {
2796 return ret;
2797 }
2798 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_CERTIFICATE_VERIFY);
2799 return 0;
Jerry Yu83da34e2022-04-16 13:59:52 +08002800}
2801
2802/*
Jerry Yuc6e6dbf2022-04-16 19:42:57 +08002803 * Handler for MBEDTLS_SSL_CERTIFICATE_VERIFY
Jerry Yu83da34e2022-04-16 13:59:52 +08002804 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002805MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002806static int ssl_tls13_write_certificate_verify(mbedtls_ssl_context *ssl)
Jerry Yu83da34e2022-04-16 13:59:52 +08002807{
Gilles Peskine449bd832023-01-11 14:50:10 +01002808 int ret = mbedtls_ssl_tls13_write_certificate_verify(ssl);
2809 if (ret != 0) {
2810 return ret;
2811 }
2812 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_SERVER_FINISHED);
2813 return 0;
Jerry Yu83da34e2022-04-16 13:59:52 +08002814}
Ronald Cron928cbd32022-10-04 16:14:26 +02002815#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED */
Jerry Yu83da34e2022-04-16 13:59:52 +08002816
Jerry Yu9b72e392023-12-01 16:27:08 +08002817/*
2818 * RFC 8446 section A.2
2819 *
2820 * | Send ServerHello
2821 * | K_send = handshake
2822 * | Send EncryptedExtensions
2823 * | [Send CertificateRequest]
2824 * Can send | [Send Certificate + CertificateVerify]
2825 * app data | Send Finished
2826 * after --> | K_send = application
2827 * here +--------+--------+
2828 * No 0-RTT | | 0-RTT
2829 * | |
2830 * K_recv = handshake | | K_recv = early data
2831 * [Skip decrypt errors] | +------> WAIT_EOED -+
2832 * | | Recv | | Recv EndOfEarlyData
2833 * | | early data | | K_recv = handshake
2834 * | +------------+ |
2835 * | |
2836 * +> WAIT_FLIGHT2 <--------+
2837 * |
2838 * +--------+--------+
2839 * No auth | | Client auth
2840 * | |
2841 * | v
2842 * | WAIT_CERT
2843 * | Recv | | Recv Certificate
2844 * | empty | v
2845 * | Certificate | WAIT_CV
2846 * | | | Recv
2847 * | v | CertificateVerify
2848 * +-> WAIT_FINISHED <---+
2849 * | Recv Finished
2850 *
2851 *
2852 * The following function handles the state changes after WAIT_FLIGHT2 in the
Jerry Yu3be85072023-12-04 09:58:54 +08002853 * above diagram. We are not going to receive early data related messages
2854 * anymore, prepare to receive the first handshake message of the client
2855 * second flight.
Jerry Yu9b72e392023-12-01 16:27:08 +08002856 */
Jerry Yu3be85072023-12-04 09:58:54 +08002857static void ssl_tls13_prepare_for_handshake_second_flight(
2858 mbedtls_ssl_context *ssl)
Jerry Yu9b72e392023-12-01 16:27:08 +08002859{
Jerry Yu9b72e392023-12-01 16:27:08 +08002860 if (ssl->handshake->certificate_request_sent) {
2861 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_CLIENT_CERTIFICATE);
2862 } else {
Jerry Yu42020fb2023-12-05 17:35:53 +08002863 MBEDTLS_SSL_DEBUG_MSG(2, ("skip parse certificate"));
Jerry Yuebb1b1d2023-12-05 11:02:15 +08002864 MBEDTLS_SSL_DEBUG_MSG(2, ("skip parse certificate verify"));
Jerry Yuebb1b1d2023-12-05 11:02:15 +08002865
Jerry Yu9b72e392023-12-01 16:27:08 +08002866 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_CLIENT_FINISHED);
2867 }
Jerry Yu9b72e392023-12-01 16:27:08 +08002868}
2869
Jerry Yu83da34e2022-04-16 13:59:52 +08002870/*
Jerry Yud6e253d2022-05-18 13:59:24 +08002871 * Handler for MBEDTLS_SSL_SERVER_FINISHED
Jerry Yu69dd8d42022-04-16 12:51:26 +08002872 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002873MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002874static int ssl_tls13_write_server_finished(mbedtls_ssl_context *ssl)
Jerry Yu69dd8d42022-04-16 12:51:26 +08002875{
Jerry Yud6e253d2022-05-18 13:59:24 +08002876 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yu27bdc7c2022-04-16 13:33:27 +08002877
Gilles Peskine449bd832023-01-11 14:50:10 +01002878 ret = mbedtls_ssl_tls13_write_finished_message(ssl);
2879 if (ret != 0) {
2880 return ret;
2881 }
Jerry Yu27bdc7c2022-04-16 13:33:27 +08002882
Gilles Peskine449bd832023-01-11 14:50:10 +01002883 ret = mbedtls_ssl_tls13_compute_application_transform(ssl);
2884 if (ret != 0) {
Jerry Yue3d67cb2022-05-19 15:33:10 +08002885 MBEDTLS_SSL_PEND_FATAL_ALERT(
Gilles Peskine449bd832023-01-11 14:50:10 +01002886 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE,
2887 MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE);
2888 return ret;
Jerry Yue3d67cb2022-05-19 15:33:10 +08002889 }
XiaokangQianc3017f62022-05-13 05:55:41 +00002890
Jerry Yu87b5ed42022-12-12 13:07:07 +08002891#if defined(MBEDTLS_SSL_EARLY_DATA)
Ronald Cron78a38f62024-02-01 18:30:31 +01002892 if (ssl->handshake->early_data_accepted) {
Jerry Yu0af63dc2023-12-01 17:14:51 +08002893 /* See RFC 8446 section A.2 for more information */
Jerry Yu87b5ed42022-12-12 13:07:07 +08002894 MBEDTLS_SSL_DEBUG_MSG(
2895 1, ("Switch to early keys for inbound traffic. "
2896 "( K_recv = early data )"));
2897 mbedtls_ssl_set_inbound_transform(
2898 ssl, ssl->handshake->transform_earlydata);
2899 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_END_OF_EARLY_DATA);
2900 return 0;
2901 }
2902#endif /* MBEDTLS_SSL_EARLY_DATA */
Jerry Yu0af63dc2023-12-01 17:14:51 +08002903 MBEDTLS_SSL_DEBUG_MSG(
2904 1, ("Switch to handshake keys for inbound traffic "
2905 "( K_recv = handshake )"));
Gilles Peskine449bd832023-01-11 14:50:10 +01002906 mbedtls_ssl_set_inbound_transform(ssl, ssl->handshake->transform_handshake);
XiaokangQian189ded22022-05-10 08:12:17 +00002907
Jerry Yu3be85072023-12-04 09:58:54 +08002908 ssl_tls13_prepare_for_handshake_second_flight(ssl);
Jerry Yu7d8c3fe2022-12-12 12:59:44 +08002909
2910 return 0;
2911}
2912
Jerry Yu87b5ed42022-12-12 13:07:07 +08002913#if defined(MBEDTLS_SSL_EARLY_DATA)
2914/*
Jerry Yu59d420f2023-12-01 16:30:34 +08002915 * Handler for MBEDTLS_SSL_END_OF_EARLY_DATA
Jerry Yu87b5ed42022-12-12 13:07:07 +08002916 */
Jerry Yu3be85072023-12-04 09:58:54 +08002917#define SSL_GOT_END_OF_EARLY_DATA 0
Jerry Yub55f9eb2023-12-05 10:27:17 +08002918#define SSL_GOT_EARLY_DATA 1
Jerry Yud5c34962023-12-01 16:32:31 +08002919/* Coordination:
Jerry Yu3be85072023-12-04 09:58:54 +08002920 * Deals with the ambiguity of not knowing if the next message is an
2921 * EndOfEarlyData message or an application message containing early data.
Jerry Yud5c34962023-12-01 16:32:31 +08002922 * Returns a negative code on failure, or
Jerry Yu3be85072023-12-04 09:58:54 +08002923 * - SSL_GOT_END_OF_EARLY_DATA
Jerry Yub55f9eb2023-12-05 10:27:17 +08002924 * - SSL_GOT_EARLY_DATA
Jerry Yud5c34962023-12-01 16:32:31 +08002925 * indicating which message is received.
2926 */
2927MBEDTLS_CHECK_RETURN_CRITICAL
2928static int ssl_tls13_end_of_early_data_coordinate(mbedtls_ssl_context *ssl)
2929{
2930 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yub4ed4602023-12-01 16:34:00 +08002931
2932 if ((ret = mbedtls_ssl_read_record(ssl, 0)) != 0) {
2933 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_read_record", ret);
2934 return ret;
2935 }
2936 ssl->keep_current_message = 1;
2937
2938 if (ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
2939 ssl->in_msg[0] == MBEDTLS_SSL_HS_END_OF_EARLY_DATA) {
Jerry Yub55f9eb2023-12-05 10:27:17 +08002940 MBEDTLS_SSL_DEBUG_MSG(3, ("Received an end_of_early_data message."));
Jerry Yu3be85072023-12-04 09:58:54 +08002941 return SSL_GOT_END_OF_EARLY_DATA;
Jerry Yub4ed4602023-12-01 16:34:00 +08002942 }
2943
2944 if (ssl->in_msgtype == MBEDTLS_SSL_MSG_APPLICATION_DATA) {
Ronald Croned7d4bf2024-01-31 07:55:19 +01002945 if (ssl->in_offt == NULL) {
Ronald Cron85718042024-02-22 10:22:09 +01002946 MBEDTLS_SSL_DEBUG_MSG(3, ("Received early data"));
Ronald Croned7d4bf2024-01-31 07:55:19 +01002947 /* Set the reading pointer */
2948 ssl->in_offt = ssl->in_msg;
Ronald Cron85718042024-02-22 10:22:09 +01002949 ret = mbedtls_ssl_tls13_check_early_data_len(ssl, ssl->in_msglen);
2950 if (ret != 0) {
2951 return ret;
2952 }
Ronald Croned7d4bf2024-01-31 07:55:19 +01002953 }
Jerry Yub55f9eb2023-12-05 10:27:17 +08002954 return SSL_GOT_EARLY_DATA;
Jerry Yub4ed4602023-12-01 16:34:00 +08002955 }
2956
Jerry Yu7bb40a32023-12-04 10:04:15 +08002957 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE,
2958 MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE);
Jerry Yub4ed4602023-12-01 16:34:00 +08002959 return MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE;
Jerry Yud5c34962023-12-01 16:32:31 +08002960}
2961
2962MBEDTLS_CHECK_RETURN_CRITICAL
2963static int ssl_tls13_parse_end_of_early_data(mbedtls_ssl_context *ssl,
2964 const unsigned char *buf,
2965 const unsigned char *end)
2966{
Jerry Yu75c9ab72023-12-01 16:41:40 +08002967 /* RFC 8446 section 4.5
2968 *
2969 * struct {} EndOfEarlyData;
2970 */
Jerry Yufbf03992023-12-04 10:00:37 +08002971 if (buf != end) {
2972 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR,
2973 MBEDTLS_ERR_SSL_DECODE_ERROR);
2974 return MBEDTLS_ERR_SSL_DECODE_ERROR;
2975 }
2976 return 0;
Jerry Yud5c34962023-12-01 16:32:31 +08002977}
2978
Jerry Yud5c34962023-12-01 16:32:31 +08002979/*
2980 * RFC 8446 section A.2
2981 *
2982 * | Send ServerHello
2983 * | K_send = handshake
2984 * | Send EncryptedExtensions
2985 * | [Send CertificateRequest]
2986 * Can send | [Send Certificate + CertificateVerify]
2987 * app data | Send Finished
2988 * after --> | K_send = application
2989 * here +--------+--------+
2990 * No 0-RTT | | 0-RTT
2991 * | |
2992 * K_recv = handshake | | K_recv = early data
2993 * [Skip decrypt errors] | +------> WAIT_EOED -+
2994 * | | Recv | | Recv EndOfEarlyData
2995 * | | early data | | K_recv = handshake
2996 * | +------------+ |
2997 * | |
2998 * +> WAIT_FLIGHT2 <--------+
2999 * |
3000 * +--------+--------+
3001 * No auth | | Client auth
3002 * | |
3003 * | v
3004 * | WAIT_CERT
3005 * | Recv | | Recv Certificate
3006 * | empty | v
3007 * | Certificate | WAIT_CV
3008 * | | | Recv
3009 * | v | CertificateVerify
3010 * +-> WAIT_FINISHED <---+
3011 * | Recv Finished
3012 *
3013 * The function handles actions and state changes from 0-RTT to WAIT_FLIGHT2 in
3014 * the above diagram.
3015 */
Jerry Yu87b5ed42022-12-12 13:07:07 +08003016MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yu59d420f2023-12-01 16:30:34 +08003017static int ssl_tls13_process_end_of_early_data(mbedtls_ssl_context *ssl)
Jerry Yu87b5ed42022-12-12 13:07:07 +08003018{
3019 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yud5c34962023-12-01 16:32:31 +08003020
3021 MBEDTLS_SSL_DEBUG_MSG(2, ("=> ssl_tls13_process_end_of_early_data"));
3022
3023 MBEDTLS_SSL_PROC_CHK_NEG(ssl_tls13_end_of_early_data_coordinate(ssl));
3024
Jerry Yu3be85072023-12-04 09:58:54 +08003025 if (ret == SSL_GOT_END_OF_EARLY_DATA) {
Jerry Yud5c34962023-12-01 16:32:31 +08003026 unsigned char *buf;
3027 size_t buf_len;
3028
3029 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_tls13_fetch_handshake_msg(
3030 ssl, MBEDTLS_SSL_HS_END_OF_EARLY_DATA,
3031 &buf, &buf_len));
3032
3033 MBEDTLS_SSL_PROC_CHK(ssl_tls13_parse_end_of_early_data(
3034 ssl, buf, buf + buf_len));
3035
Jerry Yue9655122023-12-01 16:44:40 +08003036 MBEDTLS_SSL_DEBUG_MSG(
3037 1, ("Switch to handshake keys for inbound traffic"
3038 "( K_recv = handshake )"));
3039 mbedtls_ssl_set_inbound_transform(
3040 ssl, ssl->handshake->transform_handshake);
3041
Jerry Yud5c34962023-12-01 16:32:31 +08003042 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_add_hs_msg_to_checksum(
3043 ssl, MBEDTLS_SSL_HS_END_OF_EARLY_DATA,
3044 buf, buf_len));
Jerry Yue9655122023-12-01 16:44:40 +08003045
Jerry Yu3be85072023-12-04 09:58:54 +08003046 ssl_tls13_prepare_for_handshake_second_flight(ssl);
Jerry Yud5c34962023-12-01 16:32:31 +08003047
Jerry Yub55f9eb2023-12-05 10:27:17 +08003048 } else if (ret == SSL_GOT_EARLY_DATA) {
Jerry Yud9ca3542023-12-06 17:23:52 +08003049 ret = MBEDTLS_ERR_SSL_RECEIVED_EARLY_DATA;
3050 goto cleanup;
Jerry Yud5c34962023-12-01 16:32:31 +08003051 } else {
3052 MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
3053 ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
3054 goto cleanup;
3055 }
3056
Jerry Yud5c34962023-12-01 16:32:31 +08003057cleanup:
3058 MBEDTLS_SSL_DEBUG_MSG(2, ("<= ssl_tls13_process_end_of_early_data"));
Jerry Yu87b5ed42022-12-12 13:07:07 +08003059 return ret;
3060}
3061#endif /* MBEDTLS_SSL_EARLY_DATA */
3062
Jerry Yu69dd8d42022-04-16 12:51:26 +08003063/*
Jerry Yud6e253d2022-05-18 13:59:24 +08003064 * Handler for MBEDTLS_SSL_CLIENT_FINISHED
Jerry Yu69dd8d42022-04-16 12:51:26 +08003065 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02003066MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01003067static int ssl_tls13_process_client_finished(mbedtls_ssl_context *ssl)
Jerry Yu69dd8d42022-04-16 12:51:26 +08003068{
Jerry Yud6e253d2022-05-18 13:59:24 +08003069 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
3070
Gilles Peskine449bd832023-01-11 14:50:10 +01003071 ret = mbedtls_ssl_tls13_process_finished_message(ssl);
3072 if (ret != 0) {
3073 return ret;
Jerry Yue3d67cb2022-05-19 15:33:10 +08003074 }
3075
Gilles Peskine449bd832023-01-11 14:50:10 +01003076 ret = mbedtls_ssl_tls13_compute_resumption_master_secret(ssl);
3077 if (ret != 0) {
Xiaokang Qian9f1747b2023-03-30 02:50:04 +00003078 MBEDTLS_SSL_DEBUG_RET(
3079 1, "mbedtls_ssl_tls13_compute_resumption_master_secret", ret);
Gilles Peskine449bd832023-01-11 14:50:10 +01003080 }
3081
3082 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_HANDSHAKE_WRAPUP);
3083 return 0;
Jerry Yu69dd8d42022-04-16 12:51:26 +08003084}
3085
3086/*
Jerry Yud6e253d2022-05-18 13:59:24 +08003087 * Handler for MBEDTLS_SSL_HANDSHAKE_WRAPUP
Jerry Yu69dd8d42022-04-16 12:51:26 +08003088 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02003089MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01003090static int ssl_tls13_handshake_wrapup(mbedtls_ssl_context *ssl)
Jerry Yu69dd8d42022-04-16 12:51:26 +08003091{
Gilles Peskine449bd832023-01-11 14:50:10 +01003092 MBEDTLS_SSL_DEBUG_MSG(2, ("handshake: done"));
Jerry Yu03ed50b2022-04-16 17:13:30 +08003093
Gilles Peskine449bd832023-01-11 14:50:10 +01003094 mbedtls_ssl_tls13_handshake_wrapup(ssl);
Jerry Yue67bef42022-07-07 07:29:42 +00003095
Pengyu Lv3643fdb2023-01-12 16:46:28 +08003096#if defined(MBEDTLS_SSL_SESSION_TICKETS) && \
3097 defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED)
Pengyu Lva1aa31b2022-12-13 13:49:59 +08003098/* TODO: Remove the check of SOME_PSK_ENABLED since SESSION_TICKETS requires
3099 * SOME_PSK_ENABLED to be enabled. Here is just to make CI happy. It is
3100 * expected to be resolved with issue#6395.
3101 */
Pengyu Lvc7af2c42022-12-01 16:33:00 +08003102 /* Sent NewSessionTicket message only when client supports PSK */
Pengyu Lvb2cfafb2023-11-14 13:56:13 +08003103 if (mbedtls_ssl_tls13_is_some_psk_supported(ssl)) {
Xiaokang Qian9f1747b2023-03-30 02:50:04 +00003104 mbedtls_ssl_handshake_set_state(
3105 ssl, MBEDTLS_SSL_TLS1_3_NEW_SESSION_TICKET);
Pengyu Lvc7af2c42022-12-01 16:33:00 +08003106 } else
Jerry Yufca4d572022-07-21 10:37:48 +08003107#endif
Pengyu Lv3643fdb2023-01-12 16:46:28 +08003108 {
3109 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_HANDSHAKE_OVER);
3110 }
Gilles Peskine449bd832023-01-11 14:50:10 +01003111 return 0;
Jerry Yu69dd8d42022-04-16 12:51:26 +08003112}
3113
Norbert Fabritius96eed722023-01-23 15:24:59 +01003114#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Jerry Yu69dd8d42022-04-16 12:51:26 +08003115/*
Jerry Yua8d3c502022-10-30 14:51:23 +08003116 * Handler for MBEDTLS_SSL_TLS1_3_NEW_SESSION_TICKET
Jerry Yue67bef42022-07-07 07:29:42 +00003117 */
3118#define SSL_NEW_SESSION_TICKET_SKIP 0
3119#define SSL_NEW_SESSION_TICKET_WRITE 1
3120MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01003121static int ssl_tls13_write_new_session_ticket_coordinate(mbedtls_ssl_context *ssl)
Jerry Yue67bef42022-07-07 07:29:42 +00003122{
3123 /* Check whether the use of session tickets is enabled */
Gilles Peskine449bd832023-01-11 14:50:10 +01003124 if (ssl->conf->f_ticket_write == NULL) {
3125 MBEDTLS_SSL_DEBUG_MSG(2, ("NewSessionTicket: disabled,"
3126 " callback is not set"));
3127 return SSL_NEW_SESSION_TICKET_SKIP;
Jerry Yud0766ec2022-09-22 10:46:57 +08003128 }
Gilles Peskine449bd832023-01-11 14:50:10 +01003129 if (ssl->conf->new_session_tickets_count == 0) {
3130 MBEDTLS_SSL_DEBUG_MSG(2, ("NewSessionTicket: disabled,"
3131 " configured count is zero"));
3132 return SSL_NEW_SESSION_TICKET_SKIP;
Jerry Yud0766ec2022-09-22 10:46:57 +08003133 }
3134
Gilles Peskine449bd832023-01-11 14:50:10 +01003135 if (ssl->handshake->new_session_tickets_count == 0) {
3136 MBEDTLS_SSL_DEBUG_MSG(2, ("NewSessionTicket: all tickets have "
3137 "been sent."));
3138 return SSL_NEW_SESSION_TICKET_SKIP;
Jerry Yue67bef42022-07-07 07:29:42 +00003139 }
3140
Gilles Peskine449bd832023-01-11 14:50:10 +01003141 return SSL_NEW_SESSION_TICKET_WRITE;
Jerry Yue67bef42022-07-07 07:29:42 +00003142}
3143
Jerry Yue67bef42022-07-07 07:29:42 +00003144MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01003145static int ssl_tls13_prepare_new_session_ticket(mbedtls_ssl_context *ssl,
3146 unsigned char *ticket_nonce,
3147 size_t ticket_nonce_size)
Jerry Yue67bef42022-07-07 07:29:42 +00003148{
3149 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
3150 mbedtls_ssl_session *session = ssl->session;
3151 mbedtls_ssl_ciphersuite_t *ciphersuite_info;
3152 psa_algorithm_t psa_hash_alg;
3153 int hash_length;
3154
Gilles Peskine449bd832023-01-11 14:50:10 +01003155 MBEDTLS_SSL_DEBUG_MSG(2, ("=> prepare NewSessionTicket msg"));
Jerry Yue67bef42022-07-07 07:29:42 +00003156
Pengyu Lv9f926952022-11-17 15:22:33 +08003157 /* Set ticket_flags depends on the advertised psk key exchange mode */
Pengyu Lv94a42cc2023-12-06 10:04:17 +08003158 mbedtls_ssl_tls13_session_clear_ticket_flags(
Pengyu Lv9eacb442022-12-09 14:39:19 +08003159 session, MBEDTLS_SSL_TLS1_3_TICKET_FLAGS_MASK);
Pengyu Lve6487fe2022-12-06 09:30:29 +08003160#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED)
Pengyu Lv94a42cc2023-12-06 10:04:17 +08003161 mbedtls_ssl_tls13_session_set_ticket_flags(
Pengyu Lv9eacb442022-12-09 14:39:19 +08003162 session, ssl->handshake->tls13_kex_modes);
Pengyu Lve6487fe2022-12-06 09:30:29 +08003163#endif
Jerry Yu95648b02023-12-06 15:03:34 +08003164
3165#if defined(MBEDTLS_SSL_EARLY_DATA)
3166 if (ssl->conf->early_data_enabled == MBEDTLS_SSL_EARLY_DATA_ENABLED &&
3167 ssl->conf->max_early_data_size > 0) {
Pengyu Lv94a42cc2023-12-06 10:04:17 +08003168 mbedtls_ssl_tls13_session_set_ticket_flags(
Jerry Yu95648b02023-12-06 15:03:34 +08003169 session, MBEDTLS_SSL_TLS1_3_TICKET_ALLOW_EARLY_DATA);
Ronald Cronc2865192024-02-07 14:01:03 +01003170 session->max_early_data_size = ssl->conf->max_early_data_size;
Jerry Yu95648b02023-12-06 15:03:34 +08003171 }
3172#endif /* MBEDTLS_SSL_EARLY_DATA */
3173
Pengyu Lv4938a562023-01-16 11:28:49 +08003174 MBEDTLS_SSL_PRINT_TICKET_FLAGS(4, session->ticket_flags);
Pengyu Lv9f926952022-11-17 15:22:33 +08003175
Waleed Elmelegy2824a202024-02-23 17:51:47 +00003176#if defined(MBEDTLS_SSL_EARLY_DATA) && defined(MBEDTLS_SSL_ALPN)
Waleed Elmelegy5bc52632024-03-12 16:25:08 +00003177 if (session->ticket_alpn == NULL) {
3178 ret = mbedtls_ssl_session_set_ticket_alpn(session, ssl->alpn_chosen);
3179 if (ret != 0) {
3180 return ret;
3181 }
Waleed Elmelegy2824a202024-02-23 17:51:47 +00003182 }
3183#endif
3184
Jerry Yue67bef42022-07-07 07:29:42 +00003185 /* Generate ticket_age_add */
Gilles Peskine449bd832023-01-11 14:50:10 +01003186 if ((ret = ssl->conf->f_rng(ssl->conf->p_rng,
3187 (unsigned char *) &session->ticket_age_add,
3188 sizeof(session->ticket_age_add)) != 0)) {
3189 MBEDTLS_SSL_DEBUG_RET(1, "generate_ticket_age_add", ret);
3190 return ret;
Jerry Yue67bef42022-07-07 07:29:42 +00003191 }
Gilles Peskine449bd832023-01-11 14:50:10 +01003192 MBEDTLS_SSL_DEBUG_MSG(3, ("ticket_age_add: %u",
3193 (unsigned int) session->ticket_age_add));
Jerry Yue67bef42022-07-07 07:29:42 +00003194
3195 /* Generate ticket_nonce */
Gilles Peskine449bd832023-01-11 14:50:10 +01003196 ret = ssl->conf->f_rng(ssl->conf->p_rng, ticket_nonce, ticket_nonce_size);
3197 if (ret != 0) {
3198 MBEDTLS_SSL_DEBUG_RET(1, "generate_ticket_nonce", ret);
3199 return ret;
Jerry Yue67bef42022-07-07 07:29:42 +00003200 }
Gilles Peskine449bd832023-01-11 14:50:10 +01003201 MBEDTLS_SSL_DEBUG_BUF(3, "ticket_nonce:",
3202 ticket_nonce, ticket_nonce_size);
Jerry Yue67bef42022-07-07 07:29:42 +00003203
3204 ciphersuite_info =
Gilles Peskine449bd832023-01-11 14:50:10 +01003205 (mbedtls_ssl_ciphersuite_t *) ssl->handshake->ciphersuite_info;
Dave Rodgman2eab4622023-10-05 13:30:37 +01003206 psa_hash_alg = mbedtls_md_psa_alg_from_type((mbedtls_md_type_t) ciphersuite_info->mac);
Gilles Peskine449bd832023-01-11 14:50:10 +01003207 hash_length = PSA_HASH_LENGTH(psa_hash_alg);
3208 if (hash_length == -1 ||
3209 (size_t) hash_length > sizeof(session->resumption_key)) {
3210 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Jerry Yufca4d572022-07-21 10:37:48 +08003211 }
3212
Jerry Yue67bef42022-07-07 07:29:42 +00003213 /* In this code the psk key length equals the length of the hash */
3214 session->resumption_key_len = hash_length;
3215 session->ciphersuite = ciphersuite_info->id;
3216
3217 /* Compute resumption key
3218 *
3219 * HKDF-Expand-Label( resumption_master_secret,
3220 * "resumption", ticket_nonce, Hash.length )
3221 */
3222 ret = mbedtls_ssl_tls13_hkdf_expand_label(
Gilles Peskine449bd832023-01-11 14:50:10 +01003223 psa_hash_alg,
3224 session->app_secrets.resumption_master_secret,
3225 hash_length,
3226 MBEDTLS_SSL_TLS1_3_LBL_WITH_LEN(resumption),
3227 ticket_nonce,
3228 ticket_nonce_size,
3229 session->resumption_key,
3230 hash_length);
Jerry Yue67bef42022-07-07 07:29:42 +00003231
Gilles Peskine449bd832023-01-11 14:50:10 +01003232 if (ret != 0) {
3233 MBEDTLS_SSL_DEBUG_RET(2,
3234 "Creating the ticket-resumed PSK failed",
3235 ret);
3236 return ret;
Jerry Yue67bef42022-07-07 07:29:42 +00003237 }
Gilles Peskine449bd832023-01-11 14:50:10 +01003238 MBEDTLS_SSL_DEBUG_BUF(3, "Ticket-resumed PSK",
3239 session->resumption_key,
3240 session->resumption_key_len);
Jerry Yue67bef42022-07-07 07:29:42 +00003241
Gilles Peskine449bd832023-01-11 14:50:10 +01003242 MBEDTLS_SSL_DEBUG_BUF(3, "resumption_master_secret",
3243 session->app_secrets.resumption_master_secret,
3244 hash_length);
Jerry Yue67bef42022-07-07 07:29:42 +00003245
Gilles Peskine449bd832023-01-11 14:50:10 +01003246 return 0;
Jerry Yue67bef42022-07-07 07:29:42 +00003247}
3248
3249/* This function creates a NewSessionTicket message in the following format:
3250 *
3251 * struct {
3252 * uint32 ticket_lifetime;
3253 * uint32 ticket_age_add;
3254 * opaque ticket_nonce<0..255>;
3255 * opaque ticket<1..2^16-1>;
3256 * Extension extensions<0..2^16-2>;
3257 * } NewSessionTicket;
3258 *
3259 * The ticket inside the NewSessionTicket message is an encrypted container
3260 * carrying the necessary information so that the server is later able to
3261 * re-start the communication.
3262 *
3263 * The following fields are placed inside the ticket by the
3264 * f_ticket_write() function:
3265 *
Jerry Yu0069abc2023-11-23 21:07:28 +08003266 * - creation time (ticket_creation_time)
3267 * - flags (ticket_flags)
Jerry Yue67bef42022-07-07 07:29:42 +00003268 * - age add (ticket_age_add)
Jerry Yu0069abc2023-11-23 21:07:28 +08003269 * - key (resumption_key)
3270 * - key length (resumption_key_len)
Jerry Yue67bef42022-07-07 07:29:42 +00003271 * - ciphersuite (ciphersuite)
Jerry Yu0069abc2023-11-23 21:07:28 +08003272 * - max_early_data_size (max_early_data_size)
Jerry Yue67bef42022-07-07 07:29:42 +00003273 */
3274MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01003275static int ssl_tls13_write_new_session_ticket_body(mbedtls_ssl_context *ssl,
3276 unsigned char *buf,
3277 unsigned char *end,
3278 size_t *out_len,
3279 unsigned char *ticket_nonce,
3280 size_t ticket_nonce_size)
Jerry Yue67bef42022-07-07 07:29:42 +00003281{
3282 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
3283 unsigned char *p = buf;
3284 mbedtls_ssl_session *session = ssl->session;
3285 size_t ticket_len;
3286 uint32_t ticket_lifetime;
Jerry Yu01da35e2022-12-12 15:09:22 +08003287 unsigned char *p_extensions_len;
Jerry Yue67bef42022-07-07 07:29:42 +00003288
3289 *out_len = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01003290 MBEDTLS_SSL_DEBUG_MSG(2, ("=> write NewSessionTicket msg"));
Jerry Yue67bef42022-07-07 07:29:42 +00003291
3292 /*
3293 * ticket_lifetime 4 bytes
3294 * ticket_age_add 4 bytes
3295 * ticket_nonce 1 + ticket_nonce_size bytes
3296 * ticket >=2 bytes
3297 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003298 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 4 + 4 + 1 + ticket_nonce_size + 2);
Jerry Yue67bef42022-07-07 07:29:42 +00003299
3300 /* Generate ticket and ticket_lifetime */
Ronald Cron3c0072b2023-11-22 10:00:14 +01003301#if defined(MBEDTLS_HAVE_TIME)
3302 session->ticket_creation_time = mbedtls_ms_time();
3303#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01003304 ret = ssl->conf->f_ticket_write(ssl->conf->p_ticket,
3305 session,
3306 p + 9 + ticket_nonce_size + 2,
3307 end,
3308 &ticket_len,
3309 &ticket_lifetime);
3310 if (ret != 0) {
3311 MBEDTLS_SSL_DEBUG_RET(1, "write_ticket", ret);
3312 return ret;
Jerry Yue67bef42022-07-07 07:29:42 +00003313 }
Jerry Yuce794882023-11-22 15:01:18 +08003314
3315 /* RFC 8446 section 4.6.1
3316 *
Jerry Yue67bef42022-07-07 07:29:42 +00003317 * ticket_lifetime: Indicates the lifetime in seconds as a 32-bit
Jerry Yuce794882023-11-22 15:01:18 +08003318 * unsigned integer in network byte order from the time of ticket
3319 * issuance. Servers MUST NOT use any value greater than
3320 * 604800 seconds (7 days) ...
Jerry Yue67bef42022-07-07 07:29:42 +00003321 */
Jerry Yu8e0174a2023-11-10 13:58:16 +08003322 if (ticket_lifetime > MBEDTLS_SSL_TLS1_3_MAX_ALLOWED_TICKET_LIFETIME) {
Jerry Yuce794882023-11-22 15:01:18 +08003323 MBEDTLS_SSL_DEBUG_MSG(
3324 1, ("Ticket lifetime (%u) is greater than 7 days.",
3325 (unsigned int) ticket_lifetime));
3326 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Gilles Peskine449bd832023-01-11 14:50:10 +01003327 }
Jerry Yuce794882023-11-22 15:01:18 +08003328
Gilles Peskine449bd832023-01-11 14:50:10 +01003329 MBEDTLS_PUT_UINT32_BE(ticket_lifetime, p, 0);
3330 MBEDTLS_SSL_DEBUG_MSG(3, ("ticket_lifetime: %u",
3331 (unsigned int) ticket_lifetime));
Jerry Yue67bef42022-07-07 07:29:42 +00003332
3333 /* Write ticket_age_add */
Gilles Peskine449bd832023-01-11 14:50:10 +01003334 MBEDTLS_PUT_UINT32_BE(session->ticket_age_add, p, 4);
3335 MBEDTLS_SSL_DEBUG_MSG(3, ("ticket_age_add: %u",
3336 (unsigned int) session->ticket_age_add));
Jerry Yue67bef42022-07-07 07:29:42 +00003337
3338 /* Write ticket_nonce */
Gilles Peskine449bd832023-01-11 14:50:10 +01003339 p[8] = (unsigned char) ticket_nonce_size;
3340 if (ticket_nonce_size > 0) {
3341 memcpy(p + 9, ticket_nonce, ticket_nonce_size);
Jerry Yue67bef42022-07-07 07:29:42 +00003342 }
3343 p += 9 + ticket_nonce_size;
3344
3345 /* Write ticket */
Gilles Peskine449bd832023-01-11 14:50:10 +01003346 MBEDTLS_PUT_UINT16_BE(ticket_len, p, 0);
Jerry Yue67bef42022-07-07 07:29:42 +00003347 p += 2;
Gilles Peskine449bd832023-01-11 14:50:10 +01003348 MBEDTLS_SSL_DEBUG_BUF(4, "ticket", p, ticket_len);
Jerry Yue67bef42022-07-07 07:29:42 +00003349 p += ticket_len;
3350
3351 /* Ticket Extensions
3352 *
Jerry Yu01da35e2022-12-12 15:09:22 +08003353 * Extension extensions<0..2^16-2>;
Jerry Yue67bef42022-07-07 07:29:42 +00003354 */
Jerry Yuedab6372022-10-31 14:37:31 +08003355 ssl->handshake->sent_extensions = MBEDTLS_SSL_EXT_MASK_NONE;
3356
Gilles Peskine449bd832023-01-11 14:50:10 +01003357 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 2);
Jerry Yu01da35e2022-12-12 15:09:22 +08003358 p_extensions_len = p;
Jerry Yue67bef42022-07-07 07:29:42 +00003359 p += 2;
3360
Jerry Yu01da35e2022-12-12 15:09:22 +08003361#if defined(MBEDTLS_SSL_EARLY_DATA)
Pengyu Lv94a42cc2023-12-06 10:04:17 +08003362 if (mbedtls_ssl_tls13_session_ticket_allow_early_data(session)) {
Jerry Yu95648b02023-12-06 15:03:34 +08003363 size_t output_len;
3364
Jerry Yuf135bac2023-11-23 18:10:51 +08003365 if ((ret = mbedtls_ssl_tls13_write_early_data_ext(
Jerry Yuc59c5862023-12-05 10:40:49 +08003366 ssl, 1, p, end, &output_len)) != 0) {
Jerry Yuf135bac2023-11-23 18:10:51 +08003367 MBEDTLS_SSL_DEBUG_RET(
3368 1, "mbedtls_ssl_tls13_write_early_data_ext", ret);
3369 return ret;
3370 }
3371 p += output_len;
Jerry Yu9e7f9bc2023-11-27 16:52:07 +08003372 } else {
3373 MBEDTLS_SSL_DEBUG_MSG(
3374 4, ("early_data not allowed, "
3375 "skip early_data extension in NewSessionTicket"));
Jerry Yu01da35e2022-12-12 15:09:22 +08003376 }
Jerry Yuf135bac2023-11-23 18:10:51 +08003377
Jerry Yu01da35e2022-12-12 15:09:22 +08003378#endif /* MBEDTLS_SSL_EARLY_DATA */
3379
3380 MBEDTLS_PUT_UINT16_BE(p - p_extensions_len - 2, p_extensions_len, 0);
3381
Jerry Yue67bef42022-07-07 07:29:42 +00003382 *out_len = p - buf;
Gilles Peskine449bd832023-01-11 14:50:10 +01003383 MBEDTLS_SSL_DEBUG_BUF(4, "ticket", buf, *out_len);
3384 MBEDTLS_SSL_DEBUG_MSG(2, ("<= write new session ticket"));
Jerry Yue67bef42022-07-07 07:29:42 +00003385
Jerry Yu7de2ff02022-11-08 21:43:46 +08003386 MBEDTLS_SSL_PRINT_EXTS(
Gilles Peskine449bd832023-01-11 14:50:10 +01003387 3, MBEDTLS_SSL_HS_NEW_SESSION_TICKET, ssl->handshake->sent_extensions);
Jerry Yu4b8f2f72022-10-31 13:31:22 +08003388
Gilles Peskine449bd832023-01-11 14:50:10 +01003389 return 0;
Jerry Yue67bef42022-07-07 07:29:42 +00003390}
3391
3392/*
Jerry Yua8d3c502022-10-30 14:51:23 +08003393 * Handler for MBEDTLS_SSL_TLS1_3_NEW_SESSION_TICKET
Jerry Yue67bef42022-07-07 07:29:42 +00003394 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003395static int ssl_tls13_write_new_session_ticket(mbedtls_ssl_context *ssl)
Jerry Yue67bef42022-07-07 07:29:42 +00003396{
3397 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
3398
Gilles Peskine449bd832023-01-11 14:50:10 +01003399 MBEDTLS_SSL_PROC_CHK_NEG(ssl_tls13_write_new_session_ticket_coordinate(ssl));
Jerry Yue67bef42022-07-07 07:29:42 +00003400
Gilles Peskine449bd832023-01-11 14:50:10 +01003401 if (ret == SSL_NEW_SESSION_TICKET_WRITE) {
Jerry Yue67bef42022-07-07 07:29:42 +00003402 unsigned char ticket_nonce[MBEDTLS_SSL_TLS1_3_TICKET_NONCE_LENGTH];
3403 unsigned char *buf;
3404 size_t buf_len, msg_len;
3405
Gilles Peskine449bd832023-01-11 14:50:10 +01003406 MBEDTLS_SSL_PROC_CHK(ssl_tls13_prepare_new_session_ticket(
3407 ssl, ticket_nonce, sizeof(ticket_nonce)));
Jerry Yue67bef42022-07-07 07:29:42 +00003408
Xiaokang Qian9f1747b2023-03-30 02:50:04 +00003409 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_start_handshake_msg(
3410 ssl, MBEDTLS_SSL_HS_NEW_SESSION_TICKET,
3411 &buf, &buf_len));
Jerry Yue67bef42022-07-07 07:29:42 +00003412
Gilles Peskine449bd832023-01-11 14:50:10 +01003413 MBEDTLS_SSL_PROC_CHK(ssl_tls13_write_new_session_ticket_body(
3414 ssl, buf, buf + buf_len, &msg_len,
3415 ticket_nonce, sizeof(ticket_nonce)));
Jerry Yue67bef42022-07-07 07:29:42 +00003416
Gilles Peskine449bd832023-01-11 14:50:10 +01003417 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_finish_handshake_msg(
3418 ssl, buf_len, msg_len));
Jerry Yufca4d572022-07-21 10:37:48 +08003419
Jerry Yu359e65f2022-09-22 23:47:43 +08003420 /* Limit session tickets count to one when resumption connection.
3421 *
3422 * See document of mbedtls_ssl_conf_new_session_tickets.
3423 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003424 if (ssl->handshake->resume == 1) {
Jerry Yu359e65f2022-09-22 23:47:43 +08003425 ssl->handshake->new_session_tickets_count = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01003426 } else {
Jerry Yu359e65f2022-09-22 23:47:43 +08003427 ssl->handshake->new_session_tickets_count--;
Gilles Peskine449bd832023-01-11 14:50:10 +01003428 }
Jerry Yub7e3fa72022-09-22 11:07:18 +08003429
Jerry Yua8d3c502022-10-30 14:51:23 +08003430 mbedtls_ssl_handshake_set_state(
Gilles Peskine449bd832023-01-11 14:50:10 +01003431 ssl, MBEDTLS_SSL_TLS1_3_NEW_SESSION_TICKET_FLUSH);
3432 } else {
3433 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_HANDSHAKE_OVER);
Jerry Yue67bef42022-07-07 07:29:42 +00003434 }
3435
Jerry Yue67bef42022-07-07 07:29:42 +00003436cleanup:
3437
Gilles Peskine449bd832023-01-11 14:50:10 +01003438 return ret;
Jerry Yue67bef42022-07-07 07:29:42 +00003439}
3440#endif /* MBEDTLS_SSL_SESSION_TICKETS */
3441
3442/*
XiaokangQiane8ff3502022-04-22 02:34:40 +00003443 * TLS 1.3 State Machine -- server side
XiaokangQian7807f9f2022-02-15 10:04:37 +00003444 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003445int mbedtls_ssl_tls13_handshake_server_step(mbedtls_ssl_context *ssl)
Jerry Yub9930e72021-08-06 17:11:51 +08003446{
XiaokangQian08037552022-04-20 07:16:41 +00003447 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
XiaokangQian7807f9f2022-02-15 10:04:37 +00003448
Gilles Peskine449bd832023-01-11 14:50:10 +01003449 if (ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER || ssl->handshake == NULL) {
3450 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
3451 }
XiaokangQian7807f9f2022-02-15 10:04:37 +00003452
Gilles Peskine449bd832023-01-11 14:50:10 +01003453 MBEDTLS_SSL_DEBUG_MSG(2, ("tls13 server state: %s(%d)",
Dave Rodgman2eab4622023-10-05 13:30:37 +01003454 mbedtls_ssl_states_str((mbedtls_ssl_states) ssl->state),
Gilles Peskine449bd832023-01-11 14:50:10 +01003455 ssl->state));
Jerry Yu6e81b272021-09-27 11:16:17 +08003456
Gilles Peskine449bd832023-01-11 14:50:10 +01003457 switch (ssl->state) {
XiaokangQian7807f9f2022-02-15 10:04:37 +00003458 /* start state */
3459 case MBEDTLS_SSL_HELLO_REQUEST:
Gilles Peskine449bd832023-01-11 14:50:10 +01003460 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_CLIENT_HELLO);
XiaokangQian08037552022-04-20 07:16:41 +00003461 ret = 0;
XiaokangQian7807f9f2022-02-15 10:04:37 +00003462 break;
3463
XiaokangQian7807f9f2022-02-15 10:04:37 +00003464 case MBEDTLS_SSL_CLIENT_HELLO:
Gilles Peskine449bd832023-01-11 14:50:10 +01003465 ret = ssl_tls13_process_client_hello(ssl);
3466 if (ret != 0) {
3467 MBEDTLS_SSL_DEBUG_RET(1, "ssl_tls13_process_client_hello", ret);
3468 }
Jerry Yuf41553b2022-05-09 22:20:30 +08003469 break;
XiaokangQian7807f9f2022-02-15 10:04:37 +00003470
Jerry Yuf41553b2022-05-09 22:20:30 +08003471 case MBEDTLS_SSL_HELLO_RETRY_REQUEST:
Gilles Peskine449bd832023-01-11 14:50:10 +01003472 ret = ssl_tls13_write_hello_retry_request(ssl);
3473 if (ret != 0) {
3474 MBEDTLS_SSL_DEBUG_RET(1, "ssl_tls13_write_hello_retry_request", ret);
3475 return ret;
Jerry Yuf41553b2022-05-09 22:20:30 +08003476 }
XiaokangQian7807f9f2022-02-15 10:04:37 +00003477 break;
3478
Jerry Yu5b64ae92022-03-30 17:15:02 +08003479 case MBEDTLS_SSL_SERVER_HELLO:
Gilles Peskine449bd832023-01-11 14:50:10 +01003480 ret = ssl_tls13_write_server_hello(ssl);
Jerry Yu5b64ae92022-03-30 17:15:02 +08003481 break;
3482
Xiaofei Baicba64af2022-02-15 10:00:56 +00003483 case MBEDTLS_SSL_ENCRYPTED_EXTENSIONS:
Gilles Peskine449bd832023-01-11 14:50:10 +01003484 ret = ssl_tls13_write_encrypted_extensions(ssl);
3485 if (ret != 0) {
3486 MBEDTLS_SSL_DEBUG_RET(1, "ssl_tls13_write_encrypted_extensions", ret);
3487 return ret;
Xiaofei Baicba64af2022-02-15 10:00:56 +00003488 }
Jerry Yu48330562022-05-06 21:35:44 +08003489 break;
Jerry Yu6a2cd9e2022-05-05 11:14:19 +08003490
Ronald Cron928cbd32022-10-04 16:14:26 +02003491#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED)
Xiaofei Bai9ca09d42022-02-14 12:57:18 +00003492 case MBEDTLS_SSL_CERTIFICATE_REQUEST:
Gilles Peskine449bd832023-01-11 14:50:10 +01003493 ret = ssl_tls13_write_certificate_request(ssl);
Xiaofei Bai9ca09d42022-02-14 12:57:18 +00003494 break;
Xiaofei Bai9ca09d42022-02-14 12:57:18 +00003495
Jerry Yu83da34e2022-04-16 13:59:52 +08003496 case MBEDTLS_SSL_SERVER_CERTIFICATE:
Gilles Peskine449bd832023-01-11 14:50:10 +01003497 ret = ssl_tls13_write_server_certificate(ssl);
Jerry Yu83da34e2022-04-16 13:59:52 +08003498 break;
3499
3500 case MBEDTLS_SSL_CERTIFICATE_VERIFY:
Gilles Peskine449bd832023-01-11 14:50:10 +01003501 ret = ssl_tls13_write_certificate_verify(ssl);
Jerry Yu83da34e2022-04-16 13:59:52 +08003502 break;
Ronald Cron928cbd32022-10-04 16:14:26 +02003503#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED */
Jerry Yu83da34e2022-04-16 13:59:52 +08003504
Gilles Peskine449bd832023-01-11 14:50:10 +01003505 /*
3506 * Injection of dummy-CCS's for middlebox compatibility
3507 */
Gabor Mezei7b39bf12022-05-24 16:04:14 +02003508#if defined(MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE)
Gabor Mezeif7044ea2022-06-28 16:01:49 +02003509 case MBEDTLS_SSL_SERVER_CCS_AFTER_HELLO_RETRY_REQUEST:
Gilles Peskine449bd832023-01-11 14:50:10 +01003510 ret = mbedtls_ssl_tls13_write_change_cipher_spec(ssl);
3511 if (ret == 0) {
3512 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_CLIENT_HELLO);
3513 }
Gabor Mezei7b39bf12022-05-24 16:04:14 +02003514 break;
3515
3516 case MBEDTLS_SSL_SERVER_CCS_AFTER_SERVER_HELLO:
Ronald Crone273f722024-02-13 18:22:26 +01003517 ret = mbedtls_ssl_tls13_write_change_cipher_spec(ssl);
3518 if (ret != 0) {
3519 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01003520 }
Ronald Cronfe59ff72024-01-24 14:31:50 +01003521 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_ENCRYPTED_EXTENSIONS);
Gabor Mezei7b39bf12022-05-24 16:04:14 +02003522 break;
3523#endif /* MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE */
3524
Jerry Yu69dd8d42022-04-16 12:51:26 +08003525 case MBEDTLS_SSL_SERVER_FINISHED:
Gilles Peskine449bd832023-01-11 14:50:10 +01003526 ret = ssl_tls13_write_server_finished(ssl);
Jerry Yu69dd8d42022-04-16 12:51:26 +08003527 break;
3528
Jerry Yu87b5ed42022-12-12 13:07:07 +08003529#if defined(MBEDTLS_SSL_EARLY_DATA)
3530 case MBEDTLS_SSL_END_OF_EARLY_DATA:
Jerry Yu59d420f2023-12-01 16:30:34 +08003531 ret = ssl_tls13_process_end_of_early_data(ssl);
Jerry Yu87b5ed42022-12-12 13:07:07 +08003532 break;
3533#endif /* MBEDTLS_SSL_EARLY_DATA */
3534
Jerry Yu69dd8d42022-04-16 12:51:26 +08003535 case MBEDTLS_SSL_CLIENT_FINISHED:
Gilles Peskine449bd832023-01-11 14:50:10 +01003536 ret = ssl_tls13_process_client_finished(ssl);
Jerry Yu69dd8d42022-04-16 12:51:26 +08003537 break;
3538
Jerry Yu69dd8d42022-04-16 12:51:26 +08003539 case MBEDTLS_SSL_HANDSHAKE_WRAPUP:
Gilles Peskine449bd832023-01-11 14:50:10 +01003540 ret = ssl_tls13_handshake_wrapup(ssl);
Jerry Yu69dd8d42022-04-16 12:51:26 +08003541 break;
3542
Ronald Cron766c0cd2022-10-18 12:17:11 +02003543#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED)
XiaokangQian6b916b12022-04-25 07:29:34 +00003544 case MBEDTLS_SSL_CLIENT_CERTIFICATE:
Gilles Peskine449bd832023-01-11 14:50:10 +01003545 ret = mbedtls_ssl_tls13_process_certificate(ssl);
3546 if (ret == 0) {
3547 if (ssl->session_negotiate->peer_cert != NULL) {
Ronald Cron209cae92022-06-07 10:30:19 +02003548 mbedtls_ssl_handshake_set_state(
Gilles Peskine449bd832023-01-11 14:50:10 +01003549 ssl, MBEDTLS_SSL_CLIENT_CERTIFICATE_VERIFY);
3550 } else {
3551 MBEDTLS_SSL_DEBUG_MSG(2, ("skip parse certificate verify"));
Ronald Cron209cae92022-06-07 10:30:19 +02003552 mbedtls_ssl_handshake_set_state(
Gilles Peskine449bd832023-01-11 14:50:10 +01003553 ssl, MBEDTLS_SSL_CLIENT_FINISHED);
Ronald Cron19385882022-06-15 16:26:13 +02003554 }
XiaokangQian6b916b12022-04-25 07:29:34 +00003555 }
3556 break;
3557
3558 case MBEDTLS_SSL_CLIENT_CERTIFICATE_VERIFY:
Gilles Peskine449bd832023-01-11 14:50:10 +01003559 ret = mbedtls_ssl_tls13_process_certificate_verify(ssl);
3560 if (ret == 0) {
XiaokangQian6b916b12022-04-25 07:29:34 +00003561 mbedtls_ssl_handshake_set_state(
Gilles Peskine449bd832023-01-11 14:50:10 +01003562 ssl, MBEDTLS_SSL_CLIENT_FINISHED);
XiaokangQian6b916b12022-04-25 07:29:34 +00003563 }
3564 break;
Ronald Cron766c0cd2022-10-18 12:17:11 +02003565#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED */
XiaokangQian6b916b12022-04-25 07:29:34 +00003566
Jerry Yue67bef42022-07-07 07:29:42 +00003567#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Jerry Yua8d3c502022-10-30 14:51:23 +08003568 case MBEDTLS_SSL_TLS1_3_NEW_SESSION_TICKET:
Gilles Peskine449bd832023-01-11 14:50:10 +01003569 ret = ssl_tls13_write_new_session_ticket(ssl);
3570 if (ret != 0) {
3571 MBEDTLS_SSL_DEBUG_RET(1,
3572 "ssl_tls13_write_new_session_ticket ",
3573 ret);
Jerry Yue67bef42022-07-07 07:29:42 +00003574 }
3575 break;
Jerry Yua8d3c502022-10-30 14:51:23 +08003576 case MBEDTLS_SSL_TLS1_3_NEW_SESSION_TICKET_FLUSH:
Jerry Yue67bef42022-07-07 07:29:42 +00003577 /* This state is necessary to do the flush of the New Session
Jerry Yua8d3c502022-10-30 14:51:23 +08003578 * Ticket message written in MBEDTLS_SSL_TLS1_3_NEW_SESSION_TICKET
Jerry Yue67bef42022-07-07 07:29:42 +00003579 * as part of ssl_prepare_handshake_step.
3580 */
3581 ret = 0;
Jerry Yud4e75002022-08-09 13:33:50 +08003582
Gilles Peskine449bd832023-01-11 14:50:10 +01003583 if (ssl->handshake->new_session_tickets_count == 0) {
3584 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_HANDSHAKE_OVER);
3585 } else {
Xiaokang Qian9f1747b2023-03-30 02:50:04 +00003586 mbedtls_ssl_handshake_set_state(
3587 ssl, MBEDTLS_SSL_TLS1_3_NEW_SESSION_TICKET);
Gilles Peskine449bd832023-01-11 14:50:10 +01003588 }
Jerry Yue67bef42022-07-07 07:29:42 +00003589 break;
3590
3591#endif /* MBEDTLS_SSL_SESSION_TICKETS */
3592
XiaokangQian7807f9f2022-02-15 10:04:37 +00003593 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01003594 MBEDTLS_SSL_DEBUG_MSG(1, ("invalid state %d", ssl->state));
3595 return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
XiaokangQian7807f9f2022-02-15 10:04:37 +00003596 }
3597
Gilles Peskine449bd832023-01-11 14:50:10 +01003598 return ret;
Jerry Yub9930e72021-08-06 17:11:51 +08003599}
Jerry Yu3cc4c2a2021-08-06 16:29:08 +08003600
Jerry Yufb4b6472022-01-27 15:03:26 +08003601#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_PROTO_TLS1_3 */