blob: ad1be2f229caf1c35d9d5c78ff5925a3de4f1946 [file] [log] [blame]
Jerry Yu3cc4c2a2021-08-06 16:29:08 +08001/*
Jerry Yub9930e72021-08-06 17:11:51 +08002 * TLS 1.3 server-side functions
Jerry Yu3cc4c2a2021-08-06 16:29:08 +08003 *
4 * Copyright The Mbed TLS Contributors
Dave Rodgman16799db2023-11-02 19:47:20 +00005 * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
Gilles Peskine449bd832023-01-11 14:50:10 +01006 */
Jerry Yu3cc4c2a2021-08-06 16:29:08 +08007
8#include "common.h"
9
Jerry Yufb4b6472022-01-27 15:03:26 +080010#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_PROTO_TLS1_3)
Jerry Yu3cc4c2a2021-08-06 16:29:08 +080011
Valerio Settib4f50762024-01-17 10:24:52 +010012#include "debug_internal.h"
Xiaofei Baicba64af2022-02-15 10:00:56 +000013#include "mbedtls/error.h"
14#include "mbedtls/platform.h"
Jerry Yu1c105562022-07-10 06:32:38 +000015#include "mbedtls/constant_time.h"
Manuel Pégourié-Gonnardd55d66f2023-06-20 10:14:58 +020016#include "mbedtls/oid.h"
Valerio Setti384fbde2024-01-02 13:26:40 +010017#include "mbedtls/psa_util.h"
Jerry Yu3bf2c642022-03-30 22:02:12 +080018
Xiaofei Bai9ca09d42022-02-14 12:57:18 +000019#include "ssl_misc.h"
20#include "ssl_tls13_keys.h"
21#include "ssl_debug_helpers.h"
22
Jerry Yuf35ba382022-08-23 17:58:26 +080023
Jerry Yuc5a23a02022-08-25 10:51:44 +080024static const mbedtls_ssl_ciphersuite_t *ssl_tls13_validate_peer_ciphersuite(
Gilles Peskine449bd832023-01-11 14:50:10 +010025 mbedtls_ssl_context *ssl,
26 unsigned int cipher_suite)
Jerry Yuf35ba382022-08-23 17:58:26 +080027{
28 const mbedtls_ssl_ciphersuite_t *ciphersuite_info;
Gilles Peskine449bd832023-01-11 14:50:10 +010029 if (!mbedtls_ssl_tls13_cipher_suite_is_offered(ssl, cipher_suite)) {
30 return NULL;
Jerry Yuf35ba382022-08-23 17:58:26 +080031 }
Gilles Peskine449bd832023-01-11 14:50:10 +010032
33 ciphersuite_info = mbedtls_ssl_ciphersuite_from_id(cipher_suite);
34 if ((mbedtls_ssl_validate_ciphersuite(ssl, ciphersuite_info,
35 ssl->tls_version,
36 ssl->tls_version) != 0)) {
37 return NULL;
38 }
39 return ciphersuite_info;
Jerry Yuf35ba382022-08-23 17:58:26 +080040}
41
Ronald Cron89089cc2024-02-14 18:18:08 +010042static void ssl_tls13_select_ciphersuite(
43 mbedtls_ssl_context *ssl,
44 const unsigned char *cipher_suites,
45 const unsigned char *cipher_suites_end,
46 int psk_ciphersuite_id,
47 psa_algorithm_t psk_hash_alg,
48 const mbedtls_ssl_ciphersuite_t **selected_ciphersuite_info)
49{
50 *selected_ciphersuite_info = NULL;
51
52 /*
53 * This function assumes that the length of the list of ciphersuites is
54 * even and it should have been checked it is so in the main ClientHello
55 * parsing function. Double check here.
56 */
57 if ((cipher_suites_end - cipher_suites) & 1) {
58 return;
59 }
60
61 for (const unsigned char *p = cipher_suites;
62 p < cipher_suites_end; p += 2) {
63 /*
64 * "cipher_suites_end - p is even" is an invariant of the loop. As
65 * cipher_suites_end - p > 0, we have cipher_suites_end - p >= 2 and it
66 * is thus safe to read two bytes.
67 */
68 uint16_t id = MBEDTLS_GET_UINT16_BE(p, 0);
69
70 const mbedtls_ssl_ciphersuite_t *info =
71 ssl_tls13_validate_peer_ciphersuite(ssl, id);
72 if (info == NULL) {
73 continue;
74 }
75
76 /*
77 * If a valid PSK ciphersuite identifier has been passed in, we seek
78 * for an exact match.
79 */
80 if (psk_ciphersuite_id != 0) {
81 if (id != psk_ciphersuite_id) {
82 continue;
83 }
84 } else if (psk_hash_alg != PSA_ALG_NONE) {
85 if (mbedtls_md_psa_alg_from_type((mbedtls_md_type_t) info->mac) !=
86 psk_hash_alg) {
87 continue;
88 }
89 }
90
91 *selected_ciphersuite_info = info;
92 return;
93 }
94
95 MBEDTLS_SSL_DEBUG_MSG(2, ("No matched ciphersuite"));
96}
97
Ronald Cron41a443a2022-10-04 16:38:25 +020098#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED)
Jerry Yue19e3b92022-07-08 12:04:51 +000099/* From RFC 8446:
100 *
101 * enum { psk_ke(0), psk_dhe_ke(1), (255) } PskKeyExchangeMode;
102 * struct {
103 * PskKeyExchangeMode ke_modes<1..255>;
104 * } PskKeyExchangeModes;
105 */
Jerry Yu6e74a7e2022-07-20 20:49:32 +0800106MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100107static int ssl_tls13_parse_key_exchange_modes_ext(mbedtls_ssl_context *ssl,
108 const unsigned char *buf,
109 const unsigned char *end)
Jerry Yue19e3b92022-07-08 12:04:51 +0000110{
Jerry Yu299e31f2022-07-13 23:06:36 +0800111 const unsigned char *p = buf;
Jerry Yue19e3b92022-07-08 12:04:51 +0000112 size_t ke_modes_len;
113 int ke_modes = 0;
114
Jerry Yu854dd9e2022-07-15 14:28:27 +0800115 /* Read ke_modes length (1 Byte) */
Gilles Peskine449bd832023-01-11 14:50:10 +0100116 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, 1);
Jerry Yu299e31f2022-07-13 23:06:36 +0800117 ke_modes_len = *p++;
Jerry Yue19e3b92022-07-08 12:04:51 +0000118 /* Currently, there are only two PSK modes, so even without looking
119 * at the content, something's wrong if the list has more than 2 items. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100120 if (ke_modes_len > 2) {
121 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER,
122 MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER);
123 return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
Jerry Yu299e31f2022-07-13 23:06:36 +0800124 }
Jerry Yue19e3b92022-07-08 12:04:51 +0000125
Gilles Peskine449bd832023-01-11 14:50:10 +0100126 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, ke_modes_len);
Jerry Yue19e3b92022-07-08 12:04:51 +0000127
Gilles Peskine449bd832023-01-11 14:50:10 +0100128 while (ke_modes_len-- != 0) {
129 switch (*p++) {
130 case MBEDTLS_SSL_TLS1_3_PSK_MODE_PURE:
131 ke_modes |= MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK;
132 MBEDTLS_SSL_DEBUG_MSG(3, ("Found PSK KEX MODE"));
133 break;
134 case MBEDTLS_SSL_TLS1_3_PSK_MODE_ECDHE:
135 ke_modes |= MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK_EPHEMERAL;
136 MBEDTLS_SSL_DEBUG_MSG(3, ("Found PSK_EPHEMERAL KEX MODE"));
137 break;
138 default:
139 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER,
140 MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER);
141 return MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER;
Jerry Yue19e3b92022-07-08 12:04:51 +0000142 }
143 }
144
145 ssl->handshake->tls13_kex_modes = ke_modes;
Gilles Peskine449bd832023-01-11 14:50:10 +0100146 return 0;
Jerry Yue19e3b92022-07-08 12:04:51 +0000147}
Jerry Yu1c105562022-07-10 06:32:38 +0000148
Ronald Cronfbae94a2023-12-05 18:15:14 +0100149#define SSL_TLS1_3_PSK_IDENTITY_DOES_NOT_MATCH 2
150#define SSL_TLS1_3_PSK_IDENTITY_MATCH_BUT_PSK_NOT_USABLE 1
151#define SSL_TLS1_3_PSK_IDENTITY_MATCH 0
Jerry Yu95699e72022-08-21 19:22:23 +0800152
153#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Pengyu Lv29daf4a2023-10-30 17:13:30 +0800154MBEDTLS_CHECK_RETURN_CRITICAL
Pengyu Lvbc4aab72023-12-01 15:37:24 +0800155static int ssl_tls13_key_exchange_is_psk_available(mbedtls_ssl_context *ssl);
Pengyu Lv29daf4a2023-10-30 17:13:30 +0800156MBEDTLS_CHECK_RETURN_CRITICAL
Pengyu Lvbc4aab72023-12-01 15:37:24 +0800157static int ssl_tls13_key_exchange_is_psk_ephemeral_available(mbedtls_ssl_context *ssl);
Jerry Yu95699e72022-08-21 19:22:23 +0800158
159MBEDTLS_CHECK_RETURN_CRITICAL
160static int ssl_tls13_offered_psks_check_identity_match_ticket(
Gilles Peskine449bd832023-01-11 14:50:10 +0100161 mbedtls_ssl_context *ssl,
162 const unsigned char *identity,
163 size_t identity_len,
164 uint32_t obfuscated_ticket_age,
165 mbedtls_ssl_session *session)
Jerry Yu95699e72022-08-21 19:22:23 +0800166{
Jerry Yufd310eb2022-09-06 09:16:35 +0800167 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yu95699e72022-08-21 19:22:23 +0800168 unsigned char *ticket_buffer;
Jerry Yu8d4bbba2022-09-13 14:15:48 +0800169#if defined(MBEDTLS_HAVE_TIME)
Jerry Yucebffc32022-12-15 18:00:05 +0800170 mbedtls_ms_time_t now;
171 mbedtls_ms_time_t server_age;
Jerry Yu713ce1f2023-11-17 15:32:12 +0800172 uint32_t client_age;
Jerry Yucebffc32022-12-15 18:00:05 +0800173 mbedtls_ms_time_t age_diff;
Jerry Yu8d4bbba2022-09-13 14:15:48 +0800174#endif
Jerry Yu95699e72022-08-21 19:22:23 +0800175
176 ((void) obfuscated_ticket_age);
177
Gilles Peskine449bd832023-01-11 14:50:10 +0100178 MBEDTLS_SSL_DEBUG_MSG(2, ("=> check_identity_match_ticket"));
Jerry Yu95699e72022-08-21 19:22:23 +0800179
Jerry Yufd310eb2022-09-06 09:16:35 +0800180 /* Ticket parser is not configured, Skip */
Gilles Peskine449bd832023-01-11 14:50:10 +0100181 if (ssl->conf->f_ticket_parse == NULL || identity_len == 0) {
Ronald Cronfbae94a2023-12-05 18:15:14 +0100182 return SSL_TLS1_3_PSK_IDENTITY_DOES_NOT_MATCH;
Gilles Peskine449bd832023-01-11 14:50:10 +0100183 }
Jerry Yu95699e72022-08-21 19:22:23 +0800184
Jerry Yu4746b102022-09-13 11:11:48 +0800185 /* We create a copy of the encrypted ticket since the ticket parsing
186 * function is allowed to use its input buffer as an output buffer
187 * (in-place decryption). We do, however, need the original buffer for
188 * computing the PSK binder value.
Jerry Yu95699e72022-08-21 19:22:23 +0800189 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100190 ticket_buffer = mbedtls_calloc(1, identity_len);
191 if (ticket_buffer == NULL) {
192 MBEDTLS_SSL_DEBUG_MSG(1, ("buffer too small"));
193 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
Jerry Yu95699e72022-08-21 19:22:23 +0800194 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100195 memcpy(ticket_buffer, identity, identity_len);
Jerry Yu95699e72022-08-21 19:22:23 +0800196
Ronald Cronfbae94a2023-12-05 18:15:14 +0100197 ret = ssl->conf->f_ticket_parse(ssl->conf->p_ticket,
198 session,
199 ticket_buffer, identity_len);
200 if (ret == 0) {
201 ret = SSL_TLS1_3_PSK_IDENTITY_MATCH;
202 } else {
203 if (ret == MBEDTLS_ERR_SSL_SESSION_TICKET_EXPIRED) {
Gilles Peskine449bd832023-01-11 14:50:10 +0100204 MBEDTLS_SSL_DEBUG_MSG(3, ("ticket is expired"));
Ronald Cronfbae94a2023-12-05 18:15:14 +0100205 ret = SSL_TLS1_3_PSK_IDENTITY_MATCH_BUT_PSK_NOT_USABLE;
Gilles Peskine449bd832023-01-11 14:50:10 +0100206 } else {
Ronald Cronfbae94a2023-12-05 18:15:14 +0100207 if (ret == MBEDTLS_ERR_SSL_INVALID_MAC) {
208 MBEDTLS_SSL_DEBUG_MSG(3, ("ticket is not authentic"));
209 } else {
210 MBEDTLS_SSL_DEBUG_RET(1, "ticket_parse", ret);
211 }
212 ret = SSL_TLS1_3_PSK_IDENTITY_DOES_NOT_MATCH;
Gilles Peskine449bd832023-01-11 14:50:10 +0100213 }
Jerry Yu95699e72022-08-21 19:22:23 +0800214 }
215
216 /* We delete the temporary buffer */
Gilles Peskine449bd832023-01-11 14:50:10 +0100217 mbedtls_free(ticket_buffer);
Jerry Yu95699e72022-08-21 19:22:23 +0800218
Ronald Cronfbae94a2023-12-05 18:15:14 +0100219 if (ret != SSL_TLS1_3_PSK_IDENTITY_MATCH) {
220 goto exit;
Jerry Yuce3b95e2023-10-31 16:02:04 +0800221 }
Jerry Yuce3b95e2023-10-31 16:02:04 +0800222
Ronald Cronfbae94a2023-12-05 18:15:14 +0100223 ret = SSL_TLS1_3_PSK_IDENTITY_MATCH_BUT_PSK_NOT_USABLE;
224
225 if (session->tls_version != MBEDTLS_SSL_VERSION_TLS1_3) {
226 MBEDTLS_SSL_DEBUG_MSG(3, ("Ticket TLS version is not 1.3."));
Jerry Yu8d4bbba2022-09-13 14:15:48 +0800227 goto exit;
228 }
Jerry Yu95699e72022-08-21 19:22:23 +0800229
Gilles Peskine449bd832023-01-11 14:50:10 +0100230#if defined(MBEDTLS_HAVE_TIME)
Jerry Yucebffc32022-12-15 18:00:05 +0800231 now = mbedtls_ms_time();
Gilles Peskine449bd832023-01-11 14:50:10 +0100232
Jerry Yu25ba4d42023-11-10 14:12:20 +0800233 if (now < session->ticket_creation_time) {
Gilles Peskine449bd832023-01-11 14:50:10 +0100234 MBEDTLS_SSL_DEBUG_MSG(
Jerry Yu713ce1f2023-11-17 15:32:12 +0800235 3, ("Invalid ticket creation time ( now = %" MBEDTLS_PRINTF_MS_TIME
236 ", creation_time = %" MBEDTLS_PRINTF_MS_TIME " )",
Jerry Yu25ba4d42023-11-10 14:12:20 +0800237 now, session->ticket_creation_time));
Gilles Peskine449bd832023-01-11 14:50:10 +0100238 goto exit;
239 }
240
Jerry Yu25ba4d42023-11-10 14:12:20 +0800241 server_age = now - session->ticket_creation_time;
Jerry Yu95699e72022-08-21 19:22:23 +0800242
Jerry Yu8d4bbba2022-09-13 14:15:48 +0800243 /* RFC 8446 section 4.6.1
244 *
245 * Servers MUST NOT use any value greater than 604800 seconds (7 days).
246 *
247 * RFC 8446 section 4.2.11.1
248 *
249 * Clients MUST NOT attempt to use tickets which have ages greater than
250 * the "ticket_lifetime" value which was provided with the ticket.
251 *
Jerry Yu8d4bbba2022-09-13 14:15:48 +0800252 */
Jerry Yu8e0174a2023-11-10 13:58:16 +0800253 if (server_age > MBEDTLS_SSL_TLS1_3_MAX_ALLOWED_TICKET_LIFETIME * 1000) {
Jerry Yu8d4bbba2022-09-13 14:15:48 +0800254 MBEDTLS_SSL_DEBUG_MSG(
Jerry Yud84c14f2023-11-16 13:33:57 +0800255 3, ("Ticket age exceeds limitation ticket_age = %" MBEDTLS_PRINTF_MS_TIME,
Jerry Yucebffc32022-12-15 18:00:05 +0800256 server_age));
Jerry Yu8d4bbba2022-09-13 14:15:48 +0800257 goto exit;
258 }
Jerry Yu95699e72022-08-21 19:22:23 +0800259
Jerry Yu8d4bbba2022-09-13 14:15:48 +0800260 /* RFC 8446 section 4.2.10
261 *
262 * For PSKs provisioned via NewSessionTicket, a server MUST validate that
263 * the ticket age for the selected PSK identity (computed by subtracting
264 * ticket_age_add from PskIdentity.obfuscated_ticket_age modulo 2^32) is
265 * within a small tolerance of the time since the ticket was issued.
Jerry Yuf7dad3c2022-09-14 22:31:39 +0800266 *
Jerry Yu31b601a2023-11-10 11:27:21 +0800267 * NOTE: The typical accuracy of an RTC crystal is ±100 to ±20 parts per
268 * million (360 to 72 milliseconds per hour). Default tolerance
Jerry Yu9cb953a2023-11-15 09:54:11 +0800269 * window is 6s, thus in the worst case clients and servers must
Jerry Yu31b601a2023-11-10 11:27:21 +0800270 * sync up their system time every 6000/360/2~=8 hours.
Jerry Yu8d4bbba2022-09-13 14:15:48 +0800271 */
Jerry Yucebffc32022-12-15 18:00:05 +0800272 client_age = obfuscated_ticket_age - session->ticket_age_add;
Jerry Yu60e99722023-11-20 09:55:24 +0800273 age_diff = server_age - (mbedtls_ms_time_t) client_age;
Jerry Yu28e7c552023-11-10 12:05:56 +0800274 if (age_diff < -MBEDTLS_SSL_TLS1_3_TICKET_AGE_TOLERANCE ||
Jerry Yucebffc32022-12-15 18:00:05 +0800275 age_diff > MBEDTLS_SSL_TLS1_3_TICKET_AGE_TOLERANCE) {
Jerry Yu8d4bbba2022-09-13 14:15:48 +0800276 MBEDTLS_SSL_DEBUG_MSG(
Jerry Yuf16efbc2023-10-30 11:06:24 +0800277 3, ("Ticket age outside tolerance window ( diff = %"
278 MBEDTLS_PRINTF_MS_TIME ")",
Jerry Yucebffc32022-12-15 18:00:05 +0800279 age_diff));
Jerry Yu8d4bbba2022-09-13 14:15:48 +0800280 goto exit;
281 }
Jerry Yu95699e72022-08-21 19:22:23 +0800282#endif /* MBEDTLS_HAVE_TIME */
Jerry Yu8d4bbba2022-09-13 14:15:48 +0800283
Ronald Cronfbae94a2023-12-05 18:15:14 +0100284 ret = SSL_TLS1_3_PSK_IDENTITY_MATCH;
285
Jerry Yu8d4bbba2022-09-13 14:15:48 +0800286exit:
Ronald Cronfbae94a2023-12-05 18:15:14 +0100287 if (ret != SSL_TLS1_3_PSK_IDENTITY_MATCH) {
Gilles Peskine449bd832023-01-11 14:50:10 +0100288 mbedtls_ssl_session_free(session);
289 }
Jerry Yu95699e72022-08-21 19:22:23 +0800290
Gilles Peskine449bd832023-01-11 14:50:10 +0100291 MBEDTLS_SSL_DEBUG_MSG(2, ("<= check_identity_match_ticket"));
292 return ret;
Jerry Yu95699e72022-08-21 19:22:23 +0800293}
294#endif /* MBEDTLS_SSL_SESSION_TICKETS */
295
Jerry Yu6e74a7e2022-07-20 20:49:32 +0800296MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yu1c105562022-07-10 06:32:38 +0000297static int ssl_tls13_offered_psks_check_identity_match(
Gilles Peskine449bd832023-01-11 14:50:10 +0100298 mbedtls_ssl_context *ssl,
299 const unsigned char *identity,
300 size_t identity_len,
301 uint32_t obfuscated_ticket_age,
302 int *psk_type,
303 mbedtls_ssl_session *session)
Jerry Yu1c105562022-07-10 06:32:38 +0000304{
Ronald Cron606671e2023-02-17 11:36:33 +0100305 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
306
Jerry Yu95699e72022-08-21 19:22:23 +0800307 ((void) session);
308 ((void) obfuscated_ticket_age);
Jerry Yu5725f1c2022-08-21 17:27:16 +0800309 *psk_type = MBEDTLS_SSL_TLS1_3_PSK_EXTERNAL;
Jerry Yu95699e72022-08-21 19:22:23 +0800310
Gilles Peskine449bd832023-01-11 14:50:10 +0100311 MBEDTLS_SSL_DEBUG_BUF(4, "identity", identity, identity_len);
Jerry Yu95699e72022-08-21 19:22:23 +0800312
Jerry Yu95699e72022-08-21 19:22:23 +0800313#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Ronald Cron7a30cf52024-02-23 14:38:59 +0100314 ret = ssl_tls13_offered_psks_check_identity_match_ticket(
315 ssl, identity, identity_len, obfuscated_ticket_age, session);
316 if (ret == SSL_TLS1_3_PSK_IDENTITY_MATCH) {
Jerry Yu95699e72022-08-21 19:22:23 +0800317 *psk_type = MBEDTLS_SSL_TLS1_3_PSK_RESUMPTION;
Ronald Cron606671e2023-02-17 11:36:33 +0100318 ret = mbedtls_ssl_set_hs_psk(ssl,
319 session->resumption_key,
320 session->resumption_key_len);
321 if (ret != 0) {
322 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_set_hs_psk", ret);
323 return ret;
324 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100325
326 MBEDTLS_SSL_DEBUG_BUF(4, "Ticket-resumed PSK:",
327 session->resumption_key,
328 session->resumption_key_len);
329 MBEDTLS_SSL_DEBUG_MSG(4, ("ticket: obfuscated_ticket_age: %u",
330 (unsigned) obfuscated_ticket_age));
Ronald Cronfbae94a2023-12-05 18:15:14 +0100331 return SSL_TLS1_3_PSK_IDENTITY_MATCH;
Ronald Cron7a30cf52024-02-23 14:38:59 +0100332 } else if (ret == SSL_TLS1_3_PSK_IDENTITY_MATCH_BUT_PSK_NOT_USABLE) {
333 return SSL_TLS1_3_PSK_IDENTITY_MATCH_BUT_PSK_NOT_USABLE;
Jerry Yu95699e72022-08-21 19:22:23 +0800334 }
335#endif /* MBEDTLS_SSL_SESSION_TICKETS */
336
Jerry Yu1c105562022-07-10 06:32:38 +0000337 /* Check identity with external configured function */
Gilles Peskine449bd832023-01-11 14:50:10 +0100338 if (ssl->conf->f_psk != NULL) {
339 if (ssl->conf->f_psk(
340 ssl->conf->p_psk, ssl, identity, identity_len) == 0) {
Ronald Cronfbae94a2023-12-05 18:15:14 +0100341 return SSL_TLS1_3_PSK_IDENTITY_MATCH;
Jerry Yu1c105562022-07-10 06:32:38 +0000342 }
Ronald Cronfbae94a2023-12-05 18:15:14 +0100343 return SSL_TLS1_3_PSK_IDENTITY_DOES_NOT_MATCH;
Jerry Yu1c105562022-07-10 06:32:38 +0000344 }
345
Gilles Peskine449bd832023-01-11 14:50:10 +0100346 MBEDTLS_SSL_DEBUG_BUF(5, "identity", identity, identity_len);
Jerry Yu1c105562022-07-10 06:32:38 +0000347 /* Check identity with pre-configured psk */
Gilles Peskine449bd832023-01-11 14:50:10 +0100348 if (ssl->conf->psk_identity != NULL &&
Jerry Yu2f0abc92022-07-22 19:34:48 +0800349 identity_len == ssl->conf->psk_identity_len &&
Gilles Peskine449bd832023-01-11 14:50:10 +0100350 mbedtls_ct_memcmp(ssl->conf->psk_identity,
351 identity, identity_len) == 0) {
Ronald Cron606671e2023-02-17 11:36:33 +0100352 ret = mbedtls_ssl_set_hs_psk(ssl, ssl->conf->psk, ssl->conf->psk_len);
353 if (ret != 0) {
354 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_set_hs_psk", ret);
355 return ret;
356 }
Ronald Cronfbae94a2023-12-05 18:15:14 +0100357 return SSL_TLS1_3_PSK_IDENTITY_MATCH;
Jerry Yu1c105562022-07-10 06:32:38 +0000358 }
359
Ronald Cronfbae94a2023-12-05 18:15:14 +0100360 return SSL_TLS1_3_PSK_IDENTITY_DOES_NOT_MATCH;
Jerry Yu1c105562022-07-10 06:32:38 +0000361}
362
Ronald Cron6e311272023-12-05 17:57:01 +0100363#define SSL_TLS1_3_BINDER_DOES_NOT_MATCH 1
364#define SSL_TLS1_3_BINDER_MATCH 0
Jerry Yu6e74a7e2022-07-20 20:49:32 +0800365MBEDTLS_CHECK_RETURN_CRITICAL
Xiaokang Qian9f1747b2023-03-30 02:50:04 +0000366static int ssl_tls13_offered_psks_check_binder_match(
367 mbedtls_ssl_context *ssl,
368 const unsigned char *binder, size_t binder_len,
369 int psk_type, psa_algorithm_t psk_hash_alg)
Jerry Yu1c105562022-07-10 06:32:38 +0000370{
371 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yu96a2e362022-07-21 15:11:34 +0800372
Jerry Yudaf375a2022-07-20 21:31:43 +0800373 unsigned char transcript[PSA_HASH_MAX_SIZE];
Jerry Yu1c105562022-07-10 06:32:38 +0000374 size_t transcript_len;
Jerry Yu2f0abc92022-07-22 19:34:48 +0800375 unsigned char *psk;
Jerry Yu96a2e362022-07-21 15:11:34 +0800376 size_t psk_len;
Jerry Yudaf375a2022-07-20 21:31:43 +0800377 unsigned char server_computed_binder[PSA_HASH_MAX_SIZE];
Jerry Yu1c105562022-07-10 06:32:38 +0000378
Jerry Yu1c105562022-07-10 06:32:38 +0000379 /* Get current state of handshake transcript. */
Jerry Yu29d9faa2022-08-23 17:52:45 +0800380 ret = mbedtls_ssl_get_handshake_transcript(
Manuel Pégourié-Gonnard2d6d9932023-03-28 11:38:08 +0200381 ssl, mbedtls_md_type_from_psa_alg(psk_hash_alg),
Gilles Peskine449bd832023-01-11 14:50:10 +0100382 transcript, sizeof(transcript), &transcript_len);
383 if (ret != 0) {
384 return ret;
385 }
Jerry Yu1c105562022-07-10 06:32:38 +0000386
Gilles Peskine449bd832023-01-11 14:50:10 +0100387 ret = mbedtls_ssl_tls13_export_handshake_psk(ssl, &psk, &psk_len);
388 if (ret != 0) {
389 return ret;
390 }
Jerry Yu96a2e362022-07-21 15:11:34 +0800391
Gilles Peskine449bd832023-01-11 14:50:10 +0100392 ret = mbedtls_ssl_tls13_create_psk_binder(ssl, psk_hash_alg,
393 psk, psk_len, psk_type,
394 transcript,
395 server_computed_binder);
Jerry Yu96a2e362022-07-21 15:11:34 +0800396#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +0100397 mbedtls_free((void *) psk);
Jerry Yu96a2e362022-07-21 15:11:34 +0800398#endif
Gilles Peskine449bd832023-01-11 14:50:10 +0100399 if (ret != 0) {
400 MBEDTLS_SSL_DEBUG_MSG(1, ("PSK binder calculation failed."));
401 return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
Jerry Yu1c105562022-07-10 06:32:38 +0000402 }
403
Gilles Peskine449bd832023-01-11 14:50:10 +0100404 MBEDTLS_SSL_DEBUG_BUF(3, "psk binder ( computed ): ",
405 server_computed_binder, transcript_len);
406 MBEDTLS_SSL_DEBUG_BUF(3, "psk binder ( received ): ", binder, binder_len);
Jerry Yu1c105562022-07-10 06:32:38 +0000407
Gilles Peskine449bd832023-01-11 14:50:10 +0100408 if (mbedtls_ct_memcmp(server_computed_binder, binder, binder_len) == 0) {
Ronald Cron6e311272023-12-05 17:57:01 +0100409 return SSL_TLS1_3_BINDER_MATCH;
Jerry Yu1c105562022-07-10 06:32:38 +0000410 }
411
Gilles Peskine449bd832023-01-11 14:50:10 +0100412 mbedtls_platform_zeroize(server_computed_binder,
413 sizeof(server_computed_binder));
Ronald Cron6e311272023-12-05 17:57:01 +0100414 return SSL_TLS1_3_BINDER_DOES_NOT_MATCH;
Jerry Yu1c105562022-07-10 06:32:38 +0000415}
Jerry Yu96a2e362022-07-21 15:11:34 +0800416
Jerry Yu82534862022-08-30 10:42:33 +0800417#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Jerry Yuf35ba382022-08-23 17:58:26 +0800418MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100419static int ssl_tls13_session_copy_ticket(mbedtls_ssl_session *dst,
420 const mbedtls_ssl_session *src)
Jerry Yu82534862022-08-30 10:42:33 +0800421{
Jerry Yu82534862022-08-30 10:42:33 +0800422 dst->ticket_age_add = src->ticket_age_add;
423 dst->ticket_flags = src->ticket_flags;
424 dst->resumption_key_len = src->resumption_key_len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100425 if (src->resumption_key_len == 0) {
426 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
427 }
428 memcpy(dst->resumption_key, src->resumption_key, src->resumption_key_len);
Jerry Yu4746b102022-09-13 11:11:48 +0800429
Jerry Yu33bf2402022-12-12 16:01:43 +0800430#if defined(MBEDTLS_SSL_EARLY_DATA)
431 dst->max_early_data_size = src->max_early_data_size;
432#endif
433
Gilles Peskine449bd832023-01-11 14:50:10 +0100434 return 0;
Jerry Yu82534862022-08-30 10:42:33 +0800435}
436#endif /* MBEDTLS_SSL_SESSION_TICKETS */
437
Ronald Cron79cdd412024-02-20 17:19:28 +0100438struct psk_attributes {
439 int type;
440 int key_exchange_mode;
Ronald Cron74a16292024-02-23 17:07:41 +0100441 const mbedtls_ssl_ciphersuite_t *ciphersuite_info;
Ronald Cron79cdd412024-02-20 17:19:28 +0100442};
Ronald Cron74a16292024-02-23 17:07:41 +0100443#define PSK_ATTRIBUTES_INIT { 0, 0, 0 }
Ronald Cron79cdd412024-02-20 17:19:28 +0100444
Jerry Yu1c105562022-07-10 06:32:38 +0000445/* Parser for pre_shared_key extension in client hello
446 * struct {
447 * opaque identity<1..2^16-1>;
448 * uint32 obfuscated_ticket_age;
449 * } PskIdentity;
450 *
451 * opaque PskBinderEntry<32..255>;
452 *
453 * struct {
454 * PskIdentity identities<7..2^16-1>;
455 * PskBinderEntry binders<33..2^16-1>;
456 * } OfferedPsks;
457 *
458 * struct {
459 * select (Handshake.msg_type) {
460 * case client_hello: OfferedPsks;
461 * ....
462 * };
463 * } PreSharedKeyExtension;
464 */
Jerry Yu6e74a7e2022-07-20 20:49:32 +0800465MBEDTLS_CHECK_RETURN_CRITICAL
Xiaokang Qian9f1747b2023-03-30 02:50:04 +0000466static int ssl_tls13_parse_pre_shared_key_ext(
467 mbedtls_ssl_context *ssl,
468 const unsigned char *pre_shared_key_ext,
469 const unsigned char *pre_shared_key_ext_end,
470 const unsigned char *ciphersuites,
Ronald Cron79cdd412024-02-20 17:19:28 +0100471 const unsigned char *ciphersuites_end,
472 struct psk_attributes *psk)
Jerry Yu1c105562022-07-10 06:32:38 +0000473{
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +0100474 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yu29d9faa2022-08-23 17:52:45 +0800475 const unsigned char *identities = pre_shared_key_ext;
Jerry Yu568ec252022-07-22 21:27:34 +0800476 const unsigned char *p_identity_len;
477 size_t identities_len;
Jerry Yu1c105562022-07-10 06:32:38 +0000478 const unsigned char *identities_end;
Jerry Yu568ec252022-07-22 21:27:34 +0800479 const unsigned char *binders;
480 const unsigned char *p_binder_len;
481 size_t binders_len;
Jerry Yu1c105562022-07-10 06:32:38 +0000482 const unsigned char *binders_end;
Jerry Yu96a2e362022-07-21 15:11:34 +0800483 int matched_identity = -1;
484 int identity_id = -1;
Jerry Yu1c105562022-07-10 06:32:38 +0000485
Gilles Peskine449bd832023-01-11 14:50:10 +0100486 MBEDTLS_SSL_DEBUG_BUF(3, "pre_shared_key extension",
487 pre_shared_key_ext,
488 pre_shared_key_ext_end - pre_shared_key_ext);
Jerry Yu1c105562022-07-10 06:32:38 +0000489
Jerry Yu96a2e362022-07-21 15:11:34 +0800490 /* identities_len 2 bytes
491 * identities_data >= 7 bytes
492 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100493 MBEDTLS_SSL_CHK_BUF_READ_PTR(identities, pre_shared_key_ext_end, 7 + 2);
494 identities_len = MBEDTLS_GET_UINT16_BE(identities, 0);
Jerry Yu568ec252022-07-22 21:27:34 +0800495 p_identity_len = identities + 2;
Gilles Peskine449bd832023-01-11 14:50:10 +0100496 MBEDTLS_SSL_CHK_BUF_READ_PTR(p_identity_len, pre_shared_key_ext_end,
497 identities_len);
Jerry Yu568ec252022-07-22 21:27:34 +0800498 identities_end = p_identity_len + identities_len;
Jerry Yu1c105562022-07-10 06:32:38 +0000499
Jerry Yu96a2e362022-07-21 15:11:34 +0800500 /* binders_len 2 bytes
501 * binders >= 33 bytes
502 */
Jerry Yu568ec252022-07-22 21:27:34 +0800503 binders = identities_end;
Gilles Peskine449bd832023-01-11 14:50:10 +0100504 MBEDTLS_SSL_CHK_BUF_READ_PTR(binders, pre_shared_key_ext_end, 33 + 2);
505 binders_len = MBEDTLS_GET_UINT16_BE(binders, 0);
Jerry Yu568ec252022-07-22 21:27:34 +0800506 p_binder_len = binders + 2;
Gilles Peskine449bd832023-01-11 14:50:10 +0100507 MBEDTLS_SSL_CHK_BUF_READ_PTR(p_binder_len, pre_shared_key_ext_end, binders_len);
Jerry Yu568ec252022-07-22 21:27:34 +0800508 binders_end = p_binder_len + binders_len;
Jerry Yu1c105562022-07-10 06:32:38 +0000509
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +0100510 ret = ssl->handshake->update_checksum(ssl, pre_shared_key_ext,
511 identities_end - pre_shared_key_ext);
512 if (0 != ret) {
513 MBEDTLS_SSL_DEBUG_RET(1, ("update_checksum"), ret);
514 return ret;
515 }
Jerry Yu96a2e362022-07-21 15:11:34 +0800516
Gilles Peskine449bd832023-01-11 14:50:10 +0100517 while (p_identity_len < identities_end && p_binder_len < binders_end) {
Jerry Yu1c105562022-07-10 06:32:38 +0000518 const unsigned char *identity;
Jerry Yu568ec252022-07-22 21:27:34 +0800519 size_t identity_len;
Jerry Yu82534862022-08-30 10:42:33 +0800520 uint32_t obfuscated_ticket_age;
Jerry Yu96a2e362022-07-21 15:11:34 +0800521 const unsigned char *binder;
Jerry Yu568ec252022-07-22 21:27:34 +0800522 size_t binder_len;
Ronald Cron89089cc2024-02-14 18:18:08 +0100523 int psk_ciphersuite_id;
524 psa_algorithm_t psk_hash_alg;
Ronald Croncf284562024-02-16 18:54:10 +0100525 int allowed_key_exchange_modes;
Ronald Cron74a16292024-02-23 17:07:41 +0100526
Jerry Yu82534862022-08-30 10:42:33 +0800527#if defined(MBEDTLS_SSL_SESSION_TICKETS)
528 mbedtls_ssl_session session;
Gilles Peskine449bd832023-01-11 14:50:10 +0100529 mbedtls_ssl_session_init(&session);
Jerry Yu82534862022-08-30 10:42:33 +0800530#endif
Jerry Yubb852022022-07-20 21:10:44 +0800531
Gilles Peskine449bd832023-01-11 14:50:10 +0100532 MBEDTLS_SSL_CHK_BUF_READ_PTR(p_identity_len, identities_end, 2 + 1 + 4);
533 identity_len = MBEDTLS_GET_UINT16_BE(p_identity_len, 0);
Jerry Yu568ec252022-07-22 21:27:34 +0800534 identity = p_identity_len + 2;
Gilles Peskine449bd832023-01-11 14:50:10 +0100535 MBEDTLS_SSL_CHK_BUF_READ_PTR(identity, identities_end, identity_len + 4);
536 obfuscated_ticket_age = MBEDTLS_GET_UINT32_BE(identity, identity_len);
Jerry Yu568ec252022-07-22 21:27:34 +0800537 p_identity_len += identity_len + 6;
Jerry Yu1c105562022-07-10 06:32:38 +0000538
Gilles Peskine449bd832023-01-11 14:50:10 +0100539 MBEDTLS_SSL_CHK_BUF_READ_PTR(p_binder_len, binders_end, 1 + 32);
Jerry Yu568ec252022-07-22 21:27:34 +0800540 binder_len = *p_binder_len;
541 binder = p_binder_len + 1;
Gilles Peskine449bd832023-01-11 14:50:10 +0100542 MBEDTLS_SSL_CHK_BUF_READ_PTR(binder, binders_end, binder_len);
Jerry Yu568ec252022-07-22 21:27:34 +0800543 p_binder_len += binder_len + 1;
Jerry Yu96a2e362022-07-21 15:11:34 +0800544
Jerry Yu96a2e362022-07-21 15:11:34 +0800545 identity_id++;
Gilles Peskine449bd832023-01-11 14:50:10 +0100546 if (matched_identity != -1) {
Jerry Yu1c105562022-07-10 06:32:38 +0000547 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +0100548 }
Jerry Yu1c105562022-07-10 06:32:38 +0000549
Jerry Yu96a2e362022-07-21 15:11:34 +0800550 ret = ssl_tls13_offered_psks_check_identity_match(
Gilles Peskine449bd832023-01-11 14:50:10 +0100551 ssl, identity, identity_len, obfuscated_ticket_age,
Ronald Cron79cdd412024-02-20 17:19:28 +0100552 &psk->type, &session);
Ronald Cronfbae94a2023-12-05 18:15:14 +0100553 if (ret != SSL_TLS1_3_PSK_IDENTITY_MATCH) {
Jerry Yu96a2e362022-07-21 15:11:34 +0800554 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +0100555 }
Jerry Yu96a2e362022-07-21 15:11:34 +0800556
Gilles Peskine449bd832023-01-11 14:50:10 +0100557 MBEDTLS_SSL_DEBUG_MSG(4, ("found matched identity"));
Ronald Cron89089cc2024-02-14 18:18:08 +0100558
Ronald Cron79cdd412024-02-20 17:19:28 +0100559 switch (psk->type) {
Jerry Yu0baf9072022-08-25 11:21:04 +0800560 case MBEDTLS_SSL_TLS1_3_PSK_EXTERNAL:
Ronald Cron89089cc2024-02-14 18:18:08 +0100561 psk_ciphersuite_id = 0;
562 psk_hash_alg = PSA_ALG_SHA_256;
Ronald Croncf284562024-02-16 18:54:10 +0100563 allowed_key_exchange_modes =
564 MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK_ALL;
Jerry Yu0baf9072022-08-25 11:21:04 +0800565 break;
Jerry Yu82534862022-08-30 10:42:33 +0800566#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Ronald Cronf7e99162024-02-14 16:04:02 +0100567 case MBEDTLS_SSL_TLS1_3_PSK_RESUMPTION:
Ronald Cron89089cc2024-02-14 18:18:08 +0100568 psk_ciphersuite_id = session.ciphersuite;
569 psk_hash_alg = PSA_ALG_NONE;
Ronald Croncf284562024-02-16 18:54:10 +0100570 ssl->session_negotiate->ticket_flags = session.ticket_flags;
571 allowed_key_exchange_modes =
572 session.ticket_flags &
573 MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK_ALL;
Jerry Yu0baf9072022-08-25 11:21:04 +0800574 break;
Ronald Cronf7e99162024-02-14 16:04:02 +0100575#endif
Jerry Yu0baf9072022-08-25 11:21:04 +0800576 default:
Gilles Peskine449bd832023-01-11 14:50:10 +0100577 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Jerry Yu0baf9072022-08-25 11:21:04 +0800578 }
Ronald Cron89089cc2024-02-14 18:18:08 +0100579
Ronald Cron79cdd412024-02-20 17:19:28 +0100580 psk->key_exchange_mode = MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_NONE;
581
Ronald Croncf284562024-02-16 18:54:10 +0100582 if ((allowed_key_exchange_modes &
583 MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK_EPHEMERAL) &&
584 ssl_tls13_key_exchange_is_psk_ephemeral_available(ssl)) {
Ronald Cron79cdd412024-02-20 17:19:28 +0100585 psk->key_exchange_mode = MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK_EPHEMERAL;
Ronald Croncf284562024-02-16 18:54:10 +0100586 } else if ((allowed_key_exchange_modes &
587 MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK) &&
588 ssl_tls13_key_exchange_is_psk_available(ssl)) {
Ronald Cron79cdd412024-02-20 17:19:28 +0100589 psk->key_exchange_mode = MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK;
Ronald Croncf284562024-02-16 18:54:10 +0100590 }
591
Ronald Cron79cdd412024-02-20 17:19:28 +0100592 if (psk->key_exchange_mode == MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_NONE) {
Ronald Croncf284562024-02-16 18:54:10 +0100593 MBEDTLS_SSL_DEBUG_MSG(3, ("No suitable PSK key exchange mode"));
594 continue;
595 }
596
Ronald Cron89089cc2024-02-14 18:18:08 +0100597 ssl_tls13_select_ciphersuite(ssl, ciphersuites, ciphersuites_end,
598 psk_ciphersuite_id, psk_hash_alg,
Ronald Cron74a16292024-02-23 17:07:41 +0100599 &psk->ciphersuite_info);
Ronald Cron89089cc2024-02-14 18:18:08 +0100600
Ronald Cron74a16292024-02-23 17:07:41 +0100601 if (psk->ciphersuite_info == NULL) {
Ronald Cron89089cc2024-02-14 18:18:08 +0100602#if defined(MBEDTLS_SSL_SESSION_TICKETS)
603 mbedtls_ssl_session_free(&session);
604#endif
605 /*
606 * We consider finding a ciphersuite suitable for the PSK as part
607 * of the validation of its binder. Thus if we do not find one, we
608 * abort the handshake with a decrypt_error alert.
609 */
Jerry Yuf35ba382022-08-23 17:58:26 +0800610 MBEDTLS_SSL_PEND_FATAL_ALERT(
611 MBEDTLS_SSL_ALERT_MSG_DECRYPT_ERROR,
Gilles Peskine449bd832023-01-11 14:50:10 +0100612 MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE);
Ronald Cron89089cc2024-02-14 18:18:08 +0100613 return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
Jerry Yu5725f1c2022-08-21 17:27:16 +0800614 }
615
Jerry Yu96a2e362022-07-21 15:11:34 +0800616 ret = ssl_tls13_offered_psks_check_binder_match(
Ronald Cron79cdd412024-02-20 17:19:28 +0100617 ssl, binder, binder_len, psk->type,
Ronald Cron74a16292024-02-23 17:07:41 +0100618 mbedtls_md_psa_alg_from_type((mbedtls_md_type_t) psk->ciphersuite_info->mac));
Ronald Cron6e311272023-12-05 17:57:01 +0100619 if (ret != SSL_TLS1_3_BINDER_MATCH) {
Jerry Yuc5a23a02022-08-25 10:51:44 +0800620 /* For security reasons, the handshake should be aborted when we
621 * fail to validate a binder value. See RFC 8446 section 4.2.11.2
622 * and appendix E.6. */
Jerry Yu82534862022-08-30 10:42:33 +0800623#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Gilles Peskine449bd832023-01-11 14:50:10 +0100624 mbedtls_ssl_session_free(&session);
Jerry Yu82534862022-08-30 10:42:33 +0800625#endif
Gilles Peskine449bd832023-01-11 14:50:10 +0100626 MBEDTLS_SSL_DEBUG_MSG(3, ("Invalid binder."));
Xiaokang Qian9f1747b2023-03-30 02:50:04 +0000627 MBEDTLS_SSL_DEBUG_RET(
628 1, "ssl_tls13_offered_psks_check_binder_match", ret);
Jerry Yu96a2e362022-07-21 15:11:34 +0800629 MBEDTLS_SSL_PEND_FATAL_ALERT(
630 MBEDTLS_SSL_ALERT_MSG_DECRYPT_ERROR,
Gilles Peskine449bd832023-01-11 14:50:10 +0100631 MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE);
632 return ret;
Jerry Yu96a2e362022-07-21 15:11:34 +0800633 }
Jerry Yu96a2e362022-07-21 15:11:34 +0800634
635 matched_identity = identity_id;
Jerry Yu5725f1c2022-08-21 17:27:16 +0800636
Jerry Yu82534862022-08-30 10:42:33 +0800637#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Ronald Cron79cdd412024-02-20 17:19:28 +0100638 if (psk->type == MBEDTLS_SSL_TLS1_3_PSK_RESUMPTION) {
Gilles Peskine449bd832023-01-11 14:50:10 +0100639 ret = ssl_tls13_session_copy_ticket(ssl->session_negotiate,
640 &session);
641 mbedtls_ssl_session_free(&session);
642 if (ret != 0) {
643 return ret;
644 }
Jerry Yu82534862022-08-30 10:42:33 +0800645 }
646#endif /* MBEDTLS_SSL_SESSION_TICKETS */
Jerry Yu1c105562022-07-10 06:32:38 +0000647 }
648
Gilles Peskine449bd832023-01-11 14:50:10 +0100649 if (p_identity_len != identities_end || p_binder_len != binders_end) {
650 MBEDTLS_SSL_DEBUG_MSG(3, ("pre_shared_key extension decode error"));
651 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR,
652 MBEDTLS_ERR_SSL_DECODE_ERROR);
653 return MBEDTLS_ERR_SSL_DECODE_ERROR;
Jerry Yu1c105562022-07-10 06:32:38 +0000654 }
655
Jerry Yu6f1db3f2022-07-22 23:05:59 +0800656 /* Update the handshake transcript with the binder list. */
Xiaokang Qian9f1747b2023-03-30 02:50:04 +0000657 ret = ssl->handshake->update_checksum(
658 ssl, identities_end, (size_t) (binders_end - identities_end));
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +0100659 if (0 != ret) {
660 MBEDTLS_SSL_DEBUG_RET(1, ("update_checksum"), ret);
661 return ret;
662 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100663 if (matched_identity == -1) {
Ronald Croncf284562024-02-16 18:54:10 +0100664 MBEDTLS_SSL_DEBUG_MSG(3, ("No usable PSK or ticket."));
Gilles Peskine449bd832023-01-11 14:50:10 +0100665 return MBEDTLS_ERR_SSL_UNKNOWN_IDENTITY;
Jerry Yu1c105562022-07-10 06:32:38 +0000666 }
667
Gilles Peskine449bd832023-01-11 14:50:10 +0100668 ssl->handshake->selected_identity = (uint16_t) matched_identity;
669 MBEDTLS_SSL_DEBUG_MSG(3, ("Pre shared key found"));
Jerry Yu1c105562022-07-10 06:32:38 +0000670
Gilles Peskine449bd832023-01-11 14:50:10 +0100671 return 0;
Jerry Yu1c105562022-07-10 06:32:38 +0000672}
Jerry Yu032b15ce2022-07-11 06:10:03 +0000673
674/*
675 * struct {
676 * select ( Handshake.msg_type ) {
677 * ....
678 * case server_hello:
679 * uint16 selected_identity;
680 * }
681 * } PreSharedKeyExtension;
682 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100683static int ssl_tls13_write_server_pre_shared_key_ext(mbedtls_ssl_context *ssl,
684 unsigned char *buf,
685 unsigned char *end,
686 size_t *olen)
Jerry Yu032b15ce2022-07-11 06:10:03 +0000687{
Gilles Peskine449bd832023-01-11 14:50:10 +0100688 unsigned char *p = (unsigned char *) buf;
Jerry Yu032b15ce2022-07-11 06:10:03 +0000689
690 *olen = 0;
691
David Horstmann21b89762022-10-06 18:34:28 +0100692 int not_using_psk = 0;
Jerry Yu032b15ce2022-07-11 06:10:03 +0000693#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +0100694 not_using_psk = (mbedtls_svc_key_id_is_null(ssl->handshake->psk_opaque));
Jerry Yu032b15ce2022-07-11 06:10:03 +0000695#else
Gilles Peskine449bd832023-01-11 14:50:10 +0100696 not_using_psk = (ssl->handshake->psk == NULL);
Jerry Yu032b15ce2022-07-11 06:10:03 +0000697#endif
Gilles Peskine449bd832023-01-11 14:50:10 +0100698 if (not_using_psk) {
Jerry Yu032b15ce2022-07-11 06:10:03 +0000699 /* We shouldn't have called this extension writer unless we've
700 * chosen to use a PSK. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100701 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Jerry Yu032b15ce2022-07-11 06:10:03 +0000702 }
703
Gilles Peskine449bd832023-01-11 14:50:10 +0100704 MBEDTLS_SSL_DEBUG_MSG(3, ("server hello, adding pre_shared_key extension"));
705 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 6);
Jerry Yu032b15ce2022-07-11 06:10:03 +0000706
Gilles Peskine449bd832023-01-11 14:50:10 +0100707 MBEDTLS_PUT_UINT16_BE(MBEDTLS_TLS_EXT_PRE_SHARED_KEY, p, 0);
708 MBEDTLS_PUT_UINT16_BE(2, p, 2);
Jerry Yu032b15ce2022-07-11 06:10:03 +0000709
Gilles Peskine449bd832023-01-11 14:50:10 +0100710 MBEDTLS_PUT_UINT16_BE(ssl->handshake->selected_identity, p, 4);
Jerry Yu032b15ce2022-07-11 06:10:03 +0000711
712 *olen = 6;
713
Gilles Peskine449bd832023-01-11 14:50:10 +0100714 MBEDTLS_SSL_DEBUG_MSG(4, ("sent selected_identity: %u",
715 ssl->handshake->selected_identity));
Jerry Yu032b15ce2022-07-11 06:10:03 +0000716
Gilles Peskine449bd832023-01-11 14:50:10 +0100717 mbedtls_ssl_tls13_set_hs_sent_ext_mask(ssl, MBEDTLS_TLS_EXT_PRE_SHARED_KEY);
Jerry Yub95dd362022-11-08 21:19:34 +0800718
Gilles Peskine449bd832023-01-11 14:50:10 +0100719 return 0;
Jerry Yu032b15ce2022-07-11 06:10:03 +0000720}
721
Ronald Cron41a443a2022-10-04 16:38:25 +0200722#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED */
Jerry Yue19e3b92022-07-08 12:04:51 +0000723
XiaokangQian7807f9f2022-02-15 10:04:37 +0000724/* From RFC 8446:
725 * struct {
XiaokangQiancfd925f2022-04-14 07:10:37 +0000726 * ProtocolVersion versions<2..254>;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000727 * } SupportedVersions;
728 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200729MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100730static int ssl_tls13_parse_supported_versions_ext(mbedtls_ssl_context *ssl,
731 const unsigned char *buf,
732 const unsigned char *end)
XiaokangQian7807f9f2022-02-15 10:04:37 +0000733{
XiaokangQian7807f9f2022-02-15 10:04:37 +0000734 const unsigned char *p = buf;
XiaokangQian8f9dfe42022-04-15 02:52:39 +0000735 size_t versions_len;
XiaokangQian4080a7f2022-04-11 09:55:18 +0000736 const unsigned char *versions_end;
XiaokangQian0a1b54e2022-04-21 03:01:38 +0000737 uint16_t tls_version;
Ronald Cron5af4c7f2023-03-07 20:46:59 +0100738 int found_supported_version = 0;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000739
Gilles Peskine449bd832023-01-11 14:50:10 +0100740 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, 1);
XiaokangQian4080a7f2022-04-11 09:55:18 +0000741 versions_len = p[0];
XiaokangQian7807f9f2022-02-15 10:04:37 +0000742 p += 1;
743
Gilles Peskine449bd832023-01-11 14:50:10 +0100744 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, versions_len);
XiaokangQian4080a7f2022-04-11 09:55:18 +0000745 versions_end = p + versions_len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100746 while (p < versions_end) {
747 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, versions_end, 2);
748 tls_version = mbedtls_ssl_read_version(p, ssl->conf->transport);
XiaokangQianb67384d2022-04-19 00:02:38 +0000749 p += 2;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000750
Ronald Cron3bd2b022023-04-03 16:45:39 +0200751 if (MBEDTLS_SSL_VERSION_TLS1_3 == tls_version) {
Ronald Cron5af4c7f2023-03-07 20:46:59 +0100752 found_supported_version = 1;
753 break;
754 }
755
Ronald Cron3bd2b022023-04-03 16:45:39 +0200756 if ((MBEDTLS_SSL_VERSION_TLS1_2 == tls_version) &&
757 mbedtls_ssl_conf_is_tls12_enabled(ssl->conf)) {
Ronald Cron5af4c7f2023-03-07 20:46:59 +0100758 found_supported_version = 1;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000759 break;
760 }
XiaokangQian7807f9f2022-02-15 10:04:37 +0000761 }
762
Ronald Cron5af4c7f2023-03-07 20:46:59 +0100763 if (!found_supported_version) {
764 MBEDTLS_SSL_DEBUG_MSG(1, ("No supported version found."));
XiaokangQian7807f9f2022-02-15 10:04:37 +0000765
Gilles Peskine449bd832023-01-11 14:50:10 +0100766 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_PROTOCOL_VERSION,
767 MBEDTLS_ERR_SSL_BAD_PROTOCOL_VERSION);
768 return MBEDTLS_ERR_SSL_BAD_PROTOCOL_VERSION;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000769 }
770
Ronald Cron5af4c7f2023-03-07 20:46:59 +0100771 MBEDTLS_SSL_DEBUG_MSG(1, ("Negotiated version: [%04x]",
Gilles Peskine449bd832023-01-11 14:50:10 +0100772 (unsigned int) tls_version));
XiaokangQian7807f9f2022-02-15 10:04:37 +0000773
Ronald Cron5af4c7f2023-03-07 20:46:59 +0100774 return (int) tls_version;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000775}
776
Przemek Stekiel8c0a9532023-06-15 16:48:19 +0200777#if defined(PSA_WANT_ALG_ECDH) || defined(PSA_WANT_ALG_FFDH)
XiaokangQiane8ff3502022-04-22 02:34:40 +0000778/*
XiaokangQian7807f9f2022-02-15 10:04:37 +0000779 *
780 * From RFC 8446:
781 * enum {
782 * ... (0xFFFF)
783 * } NamedGroup;
784 * struct {
785 * NamedGroup named_group_list<2..2^16-1>;
786 * } NamedGroupList;
787 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200788MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100789static int ssl_tls13_parse_supported_groups_ext(mbedtls_ssl_context *ssl,
790 const unsigned char *buf,
791 const unsigned char *end)
XiaokangQian7807f9f2022-02-15 10:04:37 +0000792{
XiaokangQian7807f9f2022-02-15 10:04:37 +0000793 const unsigned char *p = buf;
XiaokangQian84823772022-04-19 07:57:30 +0000794 size_t named_group_list_len;
XiaokangQian8f9dfe42022-04-15 02:52:39 +0000795 const unsigned char *named_group_list_end;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000796
Gilles Peskine449bd832023-01-11 14:50:10 +0100797 MBEDTLS_SSL_DEBUG_BUF(3, "supported_groups extension", p, end - buf);
798 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, 2);
799 named_group_list_len = MBEDTLS_GET_UINT16_BE(p, 0);
XiaokangQian7807f9f2022-02-15 10:04:37 +0000800 p += 2;
Gilles Peskine449bd832023-01-11 14:50:10 +0100801 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, named_group_list_len);
XiaokangQian8f9dfe42022-04-15 02:52:39 +0000802 named_group_list_end = p + named_group_list_len;
XiaokangQian4e8cd7b2022-04-21 09:48:09 +0000803 ssl->handshake->hrr_selected_group = 0;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000804
Gilles Peskine449bd832023-01-11 14:50:10 +0100805 while (p < named_group_list_end) {
XiaokangQian08037552022-04-20 07:16:41 +0000806 uint16_t named_group;
Gilles Peskine449bd832023-01-11 14:50:10 +0100807 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, named_group_list_end, 2);
808 named_group = MBEDTLS_GET_UINT16_BE(p, 0);
XiaokangQian08037552022-04-20 07:16:41 +0000809 p += 2;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000810
Gilles Peskine449bd832023-01-11 14:50:10 +0100811 MBEDTLS_SSL_DEBUG_MSG(2,
812 ("got named group: %s(%04x)",
813 mbedtls_ssl_named_group_to_str(named_group),
814 named_group));
XiaokangQian08037552022-04-20 07:16:41 +0000815
Gilles Peskine449bd832023-01-11 14:50:10 +0100816 if (!mbedtls_ssl_named_group_is_offered(ssl, named_group) ||
817 !mbedtls_ssl_named_group_is_supported(named_group) ||
818 ssl->handshake->hrr_selected_group != 0) {
XiaokangQian08037552022-04-20 07:16:41 +0000819 continue;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000820 }
821
Gilles Peskine449bd832023-01-11 14:50:10 +0100822 MBEDTLS_SSL_DEBUG_MSG(2,
823 ("add named group %s(%04x) into received list.",
824 mbedtls_ssl_named_group_to_str(named_group),
825 named_group));
Jerry Yuc1be19f2022-04-23 16:11:39 +0800826
XiaokangQian4e8cd7b2022-04-21 09:48:09 +0000827 ssl->handshake->hrr_selected_group = named_group;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000828 }
829
Gilles Peskine449bd832023-01-11 14:50:10 +0100830 return 0;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000831
832}
Przemek Stekiel8c0a9532023-06-15 16:48:19 +0200833#endif /* PSA_WANT_ALG_ECDH || PSA_WANT_ALG_FFDH */
XiaokangQian7807f9f2022-02-15 10:04:37 +0000834
XiaokangQian08037552022-04-20 07:16:41 +0000835#define SSL_TLS1_3_PARSE_KEY_SHARES_EXT_NO_MATCH 1
836
Valerio Settic9ae8622023-07-25 11:23:50 +0200837#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_EPHEMERAL_ENABLED)
XiaokangQian7807f9f2022-02-15 10:04:37 +0000838/*
839 * ssl_tls13_parse_key_shares_ext() verifies whether the information in the
Xiaokang Qian9f1747b2023-03-30 02:50:04 +0000840 * extension is correct and stores the first acceptable key share and its
841 * associated group.
XiaokangQian7807f9f2022-02-15 10:04:37 +0000842 *
843 * Possible return values are:
844 * - 0: Successful processing of the client provided key share extension.
Xiaokang Qian9f1747b2023-03-30 02:50:04 +0000845 * - SSL_TLS1_3_PARSE_KEY_SHARES_EXT_NO_MATCH: The key shares provided by
846 * the client does not match a group supported by the server. A
847 * HelloRetryRequest will be needed.
XiaokangQiane8ff3502022-04-22 02:34:40 +0000848 * - A negative value for fatal errors.
Jerry Yuc1be19f2022-04-23 16:11:39 +0800849 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200850MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100851static int ssl_tls13_parse_key_shares_ext(mbedtls_ssl_context *ssl,
852 const unsigned char *buf,
853 const unsigned char *end)
XiaokangQian7807f9f2022-02-15 10:04:37 +0000854{
XiaokangQianb67384d2022-04-19 00:02:38 +0000855 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000856 unsigned char const *p = buf;
XiaokangQian8f9dfe42022-04-15 02:52:39 +0000857 unsigned char const *client_shares_end;
Jerry Yuc1be19f2022-04-23 16:11:39 +0800858 size_t client_shares_len;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000859
860 /* From RFC 8446:
861 *
862 * struct {
863 * KeyShareEntry client_shares<0..2^16-1>;
864 * } KeyShareClientHello;
865 *
866 */
867
Gilles Peskine449bd832023-01-11 14:50:10 +0100868 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, 2);
869 client_shares_len = MBEDTLS_GET_UINT16_BE(p, 0);
XiaokangQian7807f9f2022-02-15 10:04:37 +0000870 p += 2;
Gilles Peskine449bd832023-01-11 14:50:10 +0100871 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, client_shares_len);
XiaokangQian7807f9f2022-02-15 10:04:37 +0000872
873 ssl->handshake->offered_group_id = 0;
XiaokangQian8f9dfe42022-04-15 02:52:39 +0000874 client_shares_end = p + client_shares_len;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000875
876 /* We try to find a suitable key share entry and copy it to the
877 * handshake context. Later, we have to find out whether we can do
878 * something with the provided key share or whether we have to
XiaokangQianc5763b52022-04-02 03:34:37 +0000879 * dismiss it and send a HelloRetryRequest message.
880 */
XiaokangQian7807f9f2022-02-15 10:04:37 +0000881
Gilles Peskine449bd832023-01-11 14:50:10 +0100882 while (p < client_shares_end) {
XiaokangQian9b5d04b2022-04-10 10:20:43 +0000883 uint16_t group;
Jerry Yuc1be19f2022-04-23 16:11:39 +0800884 size_t key_exchange_len;
Jerry Yu086edc22022-05-05 10:50:38 +0800885 const unsigned char *key_exchange;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000886
887 /*
888 * struct {
889 * NamedGroup group;
890 * opaque key_exchange<1..2^16-1>;
891 * } KeyShareEntry;
892 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100893 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, client_shares_end, 4);
894 group = MBEDTLS_GET_UINT16_BE(p, 0);
895 key_exchange_len = MBEDTLS_GET_UINT16_BE(p, 2);
Jerry Yuc1be19f2022-04-23 16:11:39 +0800896 p += 4;
Jerry Yu086edc22022-05-05 10:50:38 +0800897 key_exchange = p;
Gilles Peskine449bd832023-01-11 14:50:10 +0100898 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, client_shares_end, key_exchange_len);
Jerry Yu086edc22022-05-05 10:50:38 +0800899 p += key_exchange_len;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000900
901 /* Continue parsing even if we have already found a match,
XiaokangQianc5763b52022-04-02 03:34:37 +0000902 * for input validation purposes.
903 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100904 if (!mbedtls_ssl_named_group_is_offered(ssl, group) ||
905 !mbedtls_ssl_named_group_is_supported(group) ||
906 ssl->handshake->offered_group_id != 0) {
XiaokangQian060d8672022-04-21 09:24:56 +0000907 continue;
908 }
XiaokangQian060d8672022-04-21 09:24:56 +0000909
XiaokangQian7807f9f2022-02-15 10:04:37 +0000910 /*
Przemek Stekielc89f3ea2023-05-18 15:45:53 +0200911 * ECDHE and FFDHE groups are supported
XiaokangQian7807f9f2022-02-15 10:04:37 +0000912 */
Przemek Stekielc89f3ea2023-05-18 15:45:53 +0200913 if (mbedtls_ssl_tls13_named_group_is_ecdhe(group) ||
Przemek Stekield5f79e72023-06-29 09:08:43 +0200914 mbedtls_ssl_tls13_named_group_is_ffdh(group)) {
Przemek Stekielc89f3ea2023-05-18 15:45:53 +0200915 MBEDTLS_SSL_DEBUG_MSG(2, ("ECDH/FFDH group: %s (%04x)",
Gilles Peskine449bd832023-01-11 14:50:10 +0100916 mbedtls_ssl_named_group_to_str(group),
917 group));
Przemek Stekiel7ac93be2023-07-04 10:02:38 +0200918 ret = mbedtls_ssl_tls13_read_public_xxdhe_share(
Gilles Peskine449bd832023-01-11 14:50:10 +0100919 ssl, key_exchange - 2, key_exchange_len + 2);
920 if (ret != 0) {
921 return ret;
922 }
XiaokangQian4e8cd7b2022-04-21 09:48:09 +0000923
Gilles Peskine449bd832023-01-11 14:50:10 +0100924 } else {
925 MBEDTLS_SSL_DEBUG_MSG(4, ("Unrecognized NamedGroup %u",
926 (unsigned) group));
XiaokangQian7807f9f2022-02-15 10:04:37 +0000927 continue;
928 }
929
XiaokangQian9b5d04b2022-04-10 10:20:43 +0000930 ssl->handshake->offered_group_id = group;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000931 }
932
Jerry Yuc1be19f2022-04-23 16:11:39 +0800933
Gilles Peskine449bd832023-01-11 14:50:10 +0100934 if (ssl->handshake->offered_group_id == 0) {
935 MBEDTLS_SSL_DEBUG_MSG(1, ("no matching key share"));
936 return SSL_TLS1_3_PARSE_KEY_SHARES_EXT_NO_MATCH;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000937 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100938 return 0;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000939}
Valerio Settic9ae8622023-07-25 11:23:50 +0200940#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_EPHEMERAL_ENABLED */
XiaokangQian7807f9f2022-02-15 10:04:37 +0000941
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200942MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100943static int ssl_tls13_client_hello_has_exts(mbedtls_ssl_context *ssl,
944 int exts_mask)
XiaokangQian7807f9f2022-02-15 10:04:37 +0000945{
Jerry Yu0c354a22022-08-29 15:25:36 +0800946 int masked = ssl->handshake->received_extensions & exts_mask;
Gilles Peskine449bd832023-01-11 14:50:10 +0100947 return masked == exts_mask;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000948}
949
Jerry Yue5991322022-11-07 14:03:44 +0800950#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200951MBEDTLS_CHECK_RETURN_CRITICAL
XiaokangQianb67384d2022-04-19 00:02:38 +0000952static int ssl_tls13_client_hello_has_exts_for_ephemeral_key_exchange(
Gilles Peskine449bd832023-01-11 14:50:10 +0100953 mbedtls_ssl_context *ssl)
XiaokangQian7807f9f2022-02-15 10:04:37 +0000954{
Gilles Peskine449bd832023-01-11 14:50:10 +0100955 return ssl_tls13_client_hello_has_exts(
956 ssl,
957 MBEDTLS_SSL_EXT_MASK(SUPPORTED_GROUPS) |
958 MBEDTLS_SSL_EXT_MASK(KEY_SHARE) |
959 MBEDTLS_SSL_EXT_MASK(SIG_ALG));
XiaokangQian7807f9f2022-02-15 10:04:37 +0000960}
Jerry Yue5991322022-11-07 14:03:44 +0800961#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED */
XiaokangQian7807f9f2022-02-15 10:04:37 +0000962
Jerry Yue5991322022-11-07 14:03:44 +0800963#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK_ENABLED)
Jerry Yu77f01482022-07-11 07:03:24 +0000964MBEDTLS_CHECK_RETURN_CRITICAL
965static int ssl_tls13_client_hello_has_exts_for_psk_key_exchange(
Gilles Peskine449bd832023-01-11 14:50:10 +0100966 mbedtls_ssl_context *ssl)
Jerry Yu77f01482022-07-11 07:03:24 +0000967{
Gilles Peskine449bd832023-01-11 14:50:10 +0100968 return ssl_tls13_client_hello_has_exts(
969 ssl,
970 MBEDTLS_SSL_EXT_MASK(PRE_SHARED_KEY) |
971 MBEDTLS_SSL_EXT_MASK(PSK_KEY_EXCHANGE_MODES));
Jerry Yu77f01482022-07-11 07:03:24 +0000972}
Jerry Yue5991322022-11-07 14:03:44 +0800973#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK_ENABLED */
Jerry Yu77f01482022-07-11 07:03:24 +0000974
Jerry Yue5991322022-11-07 14:03:44 +0800975#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK_EPHEMERAL_ENABLED)
Jerry Yu77f01482022-07-11 07:03:24 +0000976MBEDTLS_CHECK_RETURN_CRITICAL
977static int ssl_tls13_client_hello_has_exts_for_psk_ephemeral_key_exchange(
Gilles Peskine449bd832023-01-11 14:50:10 +0100978 mbedtls_ssl_context *ssl)
Jerry Yu77f01482022-07-11 07:03:24 +0000979{
Gilles Peskine449bd832023-01-11 14:50:10 +0100980 return ssl_tls13_client_hello_has_exts(
981 ssl,
982 MBEDTLS_SSL_EXT_MASK(SUPPORTED_GROUPS) |
983 MBEDTLS_SSL_EXT_MASK(KEY_SHARE) |
984 MBEDTLS_SSL_EXT_MASK(PRE_SHARED_KEY) |
985 MBEDTLS_SSL_EXT_MASK(PSK_KEY_EXCHANGE_MODES));
Jerry Yu77f01482022-07-11 07:03:24 +0000986}
Jerry Yue5991322022-11-07 14:03:44 +0800987#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK_EPHEMERAL_ENABLED */
Jerry Yu77f01482022-07-11 07:03:24 +0000988
Pengyu Lv306a01d2023-02-02 16:35:47 +0800989#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED)
990MBEDTLS_CHECK_RETURN_CRITICAL
Pengyu Lvbc4aab72023-12-01 15:37:24 +0800991static int ssl_tls13_key_exchange_is_psk_available(mbedtls_ssl_context *ssl)
Jerry Yu77f01482022-07-11 07:03:24 +0000992{
Jerry Yue5991322022-11-07 14:03:44 +0800993#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK_ENABLED)
Ronald Crone8c162d2024-02-21 10:15:44 +0100994 return mbedtls_ssl_conf_tls13_is_psk_enabled(ssl) &&
Pengyu Lvb2cfafb2023-11-14 13:56:13 +0800995 mbedtls_ssl_tls13_is_psk_supported(ssl) &&
Gilles Peskine449bd832023-01-11 14:50:10 +0100996 ssl_tls13_client_hello_has_exts_for_psk_key_exchange(ssl);
Jerry Yu77f01482022-07-11 07:03:24 +0000997#else
998 ((void) ssl);
Gilles Peskine449bd832023-01-11 14:50:10 +0100999 return 0;
Jerry Yu77f01482022-07-11 07:03:24 +00001000#endif
1001}
XiaokangQian7807f9f2022-02-15 10:04:37 +00001002
Jerry Yu77f01482022-07-11 07:03:24 +00001003MBEDTLS_CHECK_RETURN_CRITICAL
Pengyu Lvbc4aab72023-12-01 15:37:24 +08001004static int ssl_tls13_key_exchange_is_psk_ephemeral_available(mbedtls_ssl_context *ssl)
Jerry Yu77f01482022-07-11 07:03:24 +00001005{
Jerry Yue5991322022-11-07 14:03:44 +08001006#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK_EPHEMERAL_ENABLED)
Ronald Crone8c162d2024-02-21 10:15:44 +01001007 return mbedtls_ssl_conf_tls13_is_psk_ephemeral_enabled(ssl) &&
Pengyu Lvb2cfafb2023-11-14 13:56:13 +08001008 mbedtls_ssl_tls13_is_psk_ephemeral_supported(ssl) &&
Gilles Peskine449bd832023-01-11 14:50:10 +01001009 ssl_tls13_client_hello_has_exts_for_psk_ephemeral_key_exchange(ssl);
Jerry Yu77f01482022-07-11 07:03:24 +00001010#else
1011 ((void) ssl);
Gilles Peskine449bd832023-01-11 14:50:10 +01001012 return 0;
Jerry Yu77f01482022-07-11 07:03:24 +00001013#endif
1014}
Ronald Cron79cdd412024-02-20 17:19:28 +01001015#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED */
Jerry Yu77f01482022-07-11 07:03:24 +00001016
Ronald Cron79cdd412024-02-20 17:19:28 +01001017MBEDTLS_CHECK_RETURN_CRITICAL
1018static int ssl_tls13_key_exchange_is_ephemeral_available(mbedtls_ssl_context *ssl)
Jerry Yu77f01482022-07-11 07:03:24 +00001019{
Ronald Cron79cdd412024-02-20 17:19:28 +01001020#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED)
1021 return mbedtls_ssl_conf_tls13_is_ephemeral_enabled(ssl) &&
1022 ssl_tls13_client_hello_has_exts_for_ephemeral_key_exchange(ssl);
1023#else
1024 ((void) ssl);
Gilles Peskine449bd832023-01-11 14:50:10 +01001025 return 0;
Ronald Cron79cdd412024-02-20 17:19:28 +01001026#endif
XiaokangQian7807f9f2022-02-15 10:04:37 +00001027}
1028
XiaokangQian81802f42022-06-10 13:25:22 +00001029#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
Ronald Cron928cbd32022-10-04 16:14:26 +02001030 defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED)
Ronald Cron67ea2542022-09-15 17:34:42 +02001031
1032#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01001033static psa_algorithm_t ssl_tls13_iana_sig_alg_to_psa_alg(uint16_t sig_alg)
Ronald Cron67ea2542022-09-15 17:34:42 +02001034{
Gilles Peskine449bd832023-01-11 14:50:10 +01001035 switch (sig_alg) {
Ronald Cron67ea2542022-09-15 17:34:42 +02001036 case MBEDTLS_TLS1_3_SIG_ECDSA_SECP256R1_SHA256:
Gilles Peskine449bd832023-01-11 14:50:10 +01001037 return PSA_ALG_ECDSA(PSA_ALG_SHA_256);
Ronald Cron67ea2542022-09-15 17:34:42 +02001038 case MBEDTLS_TLS1_3_SIG_ECDSA_SECP384R1_SHA384:
Gilles Peskine449bd832023-01-11 14:50:10 +01001039 return PSA_ALG_ECDSA(PSA_ALG_SHA_384);
Ronald Cron67ea2542022-09-15 17:34:42 +02001040 case MBEDTLS_TLS1_3_SIG_ECDSA_SECP521R1_SHA512:
Gilles Peskine449bd832023-01-11 14:50:10 +01001041 return PSA_ALG_ECDSA(PSA_ALG_SHA_512);
Ronald Cron67ea2542022-09-15 17:34:42 +02001042 case MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA256:
Gilles Peskine449bd832023-01-11 14:50:10 +01001043 return PSA_ALG_RSA_PSS(PSA_ALG_SHA_256);
Ronald Cron67ea2542022-09-15 17:34:42 +02001044 case MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA384:
Gilles Peskine449bd832023-01-11 14:50:10 +01001045 return PSA_ALG_RSA_PSS(PSA_ALG_SHA_384);
Ronald Cron67ea2542022-09-15 17:34:42 +02001046 case MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA512:
Gilles Peskine449bd832023-01-11 14:50:10 +01001047 return PSA_ALG_RSA_PSS(PSA_ALG_SHA_512);
Ronald Cron67ea2542022-09-15 17:34:42 +02001048 case MBEDTLS_TLS1_3_SIG_RSA_PKCS1_SHA256:
Gilles Peskine449bd832023-01-11 14:50:10 +01001049 return PSA_ALG_RSA_PKCS1V15_SIGN(PSA_ALG_SHA_256);
Ronald Cron67ea2542022-09-15 17:34:42 +02001050 case MBEDTLS_TLS1_3_SIG_RSA_PKCS1_SHA384:
Gilles Peskine449bd832023-01-11 14:50:10 +01001051 return PSA_ALG_RSA_PKCS1V15_SIGN(PSA_ALG_SHA_384);
Ronald Cron67ea2542022-09-15 17:34:42 +02001052 case MBEDTLS_TLS1_3_SIG_RSA_PKCS1_SHA512:
Gilles Peskine449bd832023-01-11 14:50:10 +01001053 return PSA_ALG_RSA_PKCS1V15_SIGN(PSA_ALG_SHA_512);
Ronald Cron67ea2542022-09-15 17:34:42 +02001054 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01001055 return PSA_ALG_NONE;
Ronald Cron67ea2542022-09-15 17:34:42 +02001056 }
1057}
1058#endif /* MBEDTLS_USE_PSA_CRYPTO */
1059
XiaokangQian23c5be62022-06-07 02:04:34 +00001060/*
XiaokangQianfb665a82022-06-15 03:57:21 +00001061 * Pick best ( private key, certificate chain ) pair based on the signature
1062 * algorithms supported by the client.
XiaokangQian23c5be62022-06-07 02:04:34 +00001063 */
Ronald Cronce7d76e2022-07-08 18:56:49 +02001064MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01001065static int ssl_tls13_pick_key_cert(mbedtls_ssl_context *ssl)
XiaokangQian23c5be62022-06-07 02:04:34 +00001066{
XiaokangQianfb665a82022-06-15 03:57:21 +00001067 mbedtls_ssl_key_cert *key_cert, *key_cert_list;
XiaokangQian81802f42022-06-10 13:25:22 +00001068 const uint16_t *sig_alg = ssl->handshake->received_sig_algs;
XiaokangQian23c5be62022-06-07 02:04:34 +00001069
1070#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Gilles Peskine449bd832023-01-11 14:50:10 +01001071 if (ssl->handshake->sni_key_cert != NULL) {
XiaokangQianfb665a82022-06-15 03:57:21 +00001072 key_cert_list = ssl->handshake->sni_key_cert;
Gilles Peskine449bd832023-01-11 14:50:10 +01001073 } else
XiaokangQian23c5be62022-06-07 02:04:34 +00001074#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
Gilles Peskine449bd832023-01-11 14:50:10 +01001075 key_cert_list = ssl->conf->key_cert;
XiaokangQian23c5be62022-06-07 02:04:34 +00001076
Gilles Peskine449bd832023-01-11 14:50:10 +01001077 if (key_cert_list == NULL) {
1078 MBEDTLS_SSL_DEBUG_MSG(3, ("server has no certificate"));
1079 return -1;
XiaokangQian23c5be62022-06-07 02:04:34 +00001080 }
1081
Gilles Peskine449bd832023-01-11 14:50:10 +01001082 for (; *sig_alg != MBEDTLS_TLS1_3_SIG_NONE; sig_alg++) {
1083 if (!mbedtls_ssl_sig_alg_is_offered(ssl, *sig_alg)) {
Ronald Cron67ea2542022-09-15 17:34:42 +02001084 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +01001085 }
Ronald Cron67ea2542022-09-15 17:34:42 +02001086
Gilles Peskine449bd832023-01-11 14:50:10 +01001087 if (!mbedtls_ssl_tls13_sig_alg_for_cert_verify_is_supported(*sig_alg)) {
Ronald Cron67ea2542022-09-15 17:34:42 +02001088 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +01001089 }
Ronald Cron67ea2542022-09-15 17:34:42 +02001090
Gilles Peskine449bd832023-01-11 14:50:10 +01001091 for (key_cert = key_cert_list; key_cert != NULL;
1092 key_cert = key_cert->next) {
Ronald Cron67ea2542022-09-15 17:34:42 +02001093#if defined(MBEDTLS_USE_PSA_CRYPTO)
1094 psa_algorithm_t psa_alg = PSA_ALG_NONE;
1095#endif /* MBEDTLS_USE_PSA_CRYPTO */
1096
Gilles Peskine449bd832023-01-11 14:50:10 +01001097 MBEDTLS_SSL_DEBUG_CRT(3, "certificate (chain) candidate",
1098 key_cert->cert);
XiaokangQian81802f42022-06-10 13:25:22 +00001099
1100 /*
Gilles Peskine449bd832023-01-11 14:50:10 +01001101 * This avoids sending the client a cert it'll reject based on
1102 * keyUsage or other extensions.
1103 */
1104 if (mbedtls_x509_crt_check_key_usage(
1105 key_cert->cert, MBEDTLS_X509_KU_DIGITAL_SIGNATURE) != 0 ||
XiaokangQian81802f42022-06-10 13:25:22 +00001106 mbedtls_x509_crt_check_extended_key_usage(
XiaokangQianfb665a82022-06-15 03:57:21 +00001107 key_cert->cert, MBEDTLS_OID_SERVER_AUTH,
Gilles Peskine449bd832023-01-11 14:50:10 +01001108 MBEDTLS_OID_SIZE(MBEDTLS_OID_SERVER_AUTH)) != 0) {
1109 MBEDTLS_SSL_DEBUG_MSG(3, ("certificate mismatch: "
1110 "(extended) key usage extension"));
XiaokangQian81802f42022-06-10 13:25:22 +00001111 continue;
1112 }
XiaokangQian23c5be62022-06-07 02:04:34 +00001113
Gilles Peskine449bd832023-01-11 14:50:10 +01001114 MBEDTLS_SSL_DEBUG_MSG(3,
1115 ("ssl_tls13_pick_key_cert:"
1116 "check signature algorithm %s [%04x]",
1117 mbedtls_ssl_sig_alg_to_str(*sig_alg),
1118 *sig_alg));
Ronald Cron67ea2542022-09-15 17:34:42 +02001119#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01001120 psa_alg = ssl_tls13_iana_sig_alg_to_psa_alg(*sig_alg);
Ronald Cron67ea2542022-09-15 17:34:42 +02001121#endif /* MBEDTLS_USE_PSA_CRYPTO */
1122
Gilles Peskine449bd832023-01-11 14:50:10 +01001123 if (mbedtls_ssl_tls13_check_sig_alg_cert_key_match(
1124 *sig_alg, &key_cert->cert->pk)
Ronald Cron67ea2542022-09-15 17:34:42 +02001125#if defined(MBEDTLS_USE_PSA_CRYPTO)
1126 && psa_alg != PSA_ALG_NONE &&
Gilles Peskine449bd832023-01-11 14:50:10 +01001127 mbedtls_pk_can_do_ext(&key_cert->cert->pk, psa_alg,
1128 PSA_KEY_USAGE_SIGN_HASH) == 1
Ronald Cron67ea2542022-09-15 17:34:42 +02001129#endif /* MBEDTLS_USE_PSA_CRYPTO */
Gilles Peskine449bd832023-01-11 14:50:10 +01001130 ) {
XiaokangQianfb665a82022-06-15 03:57:21 +00001131 ssl->handshake->key_cert = key_cert;
Gilles Peskine449bd832023-01-11 14:50:10 +01001132 MBEDTLS_SSL_DEBUG_MSG(3,
1133 ("ssl_tls13_pick_key_cert:"
1134 "selected signature algorithm"
1135 " %s [%04x]",
1136 mbedtls_ssl_sig_alg_to_str(*sig_alg),
1137 *sig_alg));
XiaokangQianfb665a82022-06-15 03:57:21 +00001138 MBEDTLS_SSL_DEBUG_CRT(
Gilles Peskine449bd832023-01-11 14:50:10 +01001139 3, "selected certificate (chain)",
1140 ssl->handshake->key_cert->cert);
1141 return 0;
XiaokangQian81802f42022-06-10 13:25:22 +00001142 }
XiaokangQian81802f42022-06-10 13:25:22 +00001143 }
XiaokangQian23c5be62022-06-07 02:04:34 +00001144 }
1145
Gilles Peskine449bd832023-01-11 14:50:10 +01001146 MBEDTLS_SSL_DEBUG_MSG(2, ("ssl_tls13_pick_key_cert:"
1147 "no suitable certificate found"));
1148 return -1;
XiaokangQian23c5be62022-06-07 02:04:34 +00001149}
XiaokangQian81802f42022-06-10 13:25:22 +00001150#endif /* MBEDTLS_X509_CRT_PARSE_C &&
Ronald Cron928cbd32022-10-04 16:14:26 +02001151 MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED */
XiaokangQian23c5be62022-06-07 02:04:34 +00001152
XiaokangQian4080a7f2022-04-11 09:55:18 +00001153/*
XiaokangQianed582dd2022-04-13 08:21:05 +00001154 *
1155 * STATE HANDLING: ClientHello
1156 *
XiaokangQianb67384d2022-04-19 00:02:38 +00001157 * There are three possible classes of outcomes when parsing the ClientHello:
XiaokangQianed582dd2022-04-13 08:21:05 +00001158 *
XiaokangQianb67384d2022-04-19 00:02:38 +00001159 * 1) The ClientHello was well-formed and matched the server's configuration.
XiaokangQianed582dd2022-04-13 08:21:05 +00001160 *
1161 * In this case, the server progresses to sending its ServerHello.
1162 *
XiaokangQianb67384d2022-04-19 00:02:38 +00001163 * 2) The ClientHello was well-formed but didn't match the server's
1164 * configuration.
XiaokangQianed582dd2022-04-13 08:21:05 +00001165 *
1166 * For example, the client might not have offered a key share which
1167 * the server supports, or the server might require a cookie.
1168 *
1169 * In this case, the server sends a HelloRetryRequest.
1170 *
XiaokangQianb67384d2022-04-19 00:02:38 +00001171 * 3) The ClientHello was ill-formed
XiaokangQianed582dd2022-04-13 08:21:05 +00001172 *
1173 * In this case, we abort the handshake.
1174 *
1175 */
1176
1177/*
XiaokangQian4080a7f2022-04-11 09:55:18 +00001178 * Structure of this message:
1179 *
XiaokangQiane8ff3502022-04-22 02:34:40 +00001180 * uint16 ProtocolVersion;
1181 * opaque Random[32];
1182 * uint8 CipherSuite[2]; // Cryptographic suite selector
XiaokangQian4080a7f2022-04-11 09:55:18 +00001183 *
XiaokangQiane8ff3502022-04-22 02:34:40 +00001184 * struct {
1185 * ProtocolVersion legacy_version = 0x0303; // TLS v1.2
1186 * Random random;
1187 * opaque legacy_session_id<0..32>;
1188 * CipherSuite cipher_suites<2..2^16-2>;
1189 * opaque legacy_compression_methods<1..2^8-1>;
1190 * Extension extensions<8..2^16-1>;
1191 * } ClientHello;
XiaokangQian4080a7f2022-04-11 09:55:18 +00001192 */
XiaokangQianed582dd2022-04-13 08:21:05 +00001193
1194#define SSL_CLIENT_HELLO_OK 0
1195#define SSL_CLIENT_HELLO_HRR_REQUIRED 1
Ronald Cron5af4c7f2023-03-07 20:46:59 +01001196#define SSL_CLIENT_HELLO_TLS1_2 2
XiaokangQianed582dd2022-04-13 08:21:05 +00001197
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001198MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01001199static int ssl_tls13_parse_client_hello(mbedtls_ssl_context *ssl,
1200 const unsigned char *buf,
1201 const unsigned char *end)
XiaokangQian7807f9f2022-02-15 10:04:37 +00001202{
XiaokangQianb67384d2022-04-19 00:02:38 +00001203 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1204 const unsigned char *p = buf;
Ronald Crond540d992023-03-07 09:41:48 +01001205 const unsigned char *random;
XiaokangQian4080a7f2022-04-11 09:55:18 +00001206 size_t legacy_session_id_len;
Ronald Croncada4102023-03-07 09:51:39 +01001207 const unsigned char *legacy_session_id;
XiaokangQian318dc762022-04-20 09:43:51 +00001208 size_t cipher_suites_len;
Ronald Cron2f16b4e2023-03-07 10:07:32 +01001209 const unsigned char *cipher_suites;
XiaokangQian060d8672022-04-21 09:24:56 +00001210 const unsigned char *cipher_suites_end;
XiaokangQianb67384d2022-04-19 00:02:38 +00001211 size_t extensions_len;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001212 const unsigned char *extensions_end;
Ronald Croneff56732023-04-03 17:36:31 +02001213 const unsigned char *supported_versions_data;
1214 const unsigned char *supported_versions_data_end;
Jerry Yuc4bf5d62022-10-29 09:08:47 +08001215 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
Jerry Yu49ca9282022-05-05 11:05:22 +08001216 int hrr_required = 0;
Ronald Cron8a74f072023-06-14 17:59:29 +02001217 int no_usable_share_for_key_agreement = 0;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001218
Ronald Cron41a443a2022-10-04 16:38:25 +02001219#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED)
Ronald Cron79cdd412024-02-20 17:19:28 +01001220 int got_psk = 0;
1221 struct psk_attributes psk = PSK_ATTRIBUTES_INIT;
Jerry Yu29d9faa2022-08-23 17:52:45 +08001222 const unsigned char *pre_shared_key_ext = NULL;
Jerry Yu1c105562022-07-10 06:32:38 +00001223 const unsigned char *pre_shared_key_ext_end = NULL;
Ronald Cron41a443a2022-10-04 16:38:25 +02001224#endif
XiaokangQian7807f9f2022-02-15 10:04:37 +00001225
XiaokangQian7807f9f2022-02-15 10:04:37 +00001226 /*
XiaokangQianb67384d2022-04-19 00:02:38 +00001227 * ClientHello layout:
XiaokangQian7807f9f2022-02-15 10:04:37 +00001228 * 0 . 1 protocol version
XiaokangQiane8ff3502022-04-22 02:34:40 +00001229 * 2 . 33 random bytes
XiaokangQianc5763b52022-04-02 03:34:37 +00001230 * 34 . 34 session id length ( 1 byte )
XiaokangQian7807f9f2022-02-15 10:04:37 +00001231 * 35 . 34+x session id
XiaokangQian7807f9f2022-02-15 10:04:37 +00001232 * .. . .. ciphersuite list length ( 2 bytes )
1233 * .. . .. ciphersuite list
1234 * .. . .. compression alg. list length ( 1 byte )
1235 * .. . .. compression alg. list
1236 * .. . .. extensions length ( 2 bytes, optional )
1237 * .. . .. extensions ( optional )
1238 */
1239
XiaokangQianb67384d2022-04-19 00:02:38 +00001240 /*
bootstrap-prime6dbbf442022-05-17 19:30:44 -04001241 * Minimal length ( with everything empty and extensions omitted ) is
XiaokangQian7807f9f2022-02-15 10:04:37 +00001242 * 2 + 32 + 1 + 2 + 1 = 38 bytes. Check that first, so that we can
1243 * read at least up to session id length without worrying.
1244 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001245 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, 38);
XiaokangQian7807f9f2022-02-15 10:04:37 +00001246
1247 /* ...
1248 * ProtocolVersion legacy_version = 0x0303; // TLS 1.2
1249 * ...
1250 * with ProtocolVersion defined as:
1251 * uint16 ProtocolVersion;
1252 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001253 if (mbedtls_ssl_read_version(p, ssl->conf->transport) !=
1254 MBEDTLS_SSL_VERSION_TLS1_2) {
1255 MBEDTLS_SSL_DEBUG_MSG(1, ("Unsupported version of TLS."));
1256 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_PROTOCOL_VERSION,
1257 MBEDTLS_ERR_SSL_BAD_PROTOCOL_VERSION);
1258 return MBEDTLS_ERR_SSL_BAD_PROTOCOL_VERSION;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001259 }
1260 p += 2;
1261
Jerry Yuc1be19f2022-04-23 16:11:39 +08001262 /* ...
1263 * Random random;
1264 * ...
XiaokangQianb67384d2022-04-19 00:02:38 +00001265 * with Random defined as:
1266 * opaque Random[32];
XiaokangQian7807f9f2022-02-15 10:04:37 +00001267 */
Ronald Crond540d992023-03-07 09:41:48 +01001268 random = p;
XiaokangQian08037552022-04-20 07:16:41 +00001269 p += MBEDTLS_CLIENT_HELLO_RANDOM_LEN;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001270
Jerry Yuc1be19f2022-04-23 16:11:39 +08001271 /* ...
XiaokangQianb67384d2022-04-19 00:02:38 +00001272 * opaque legacy_session_id<0..32>;
Jerry Yuc1be19f2022-04-23 16:11:39 +08001273 * ...
XiaokangQian7807f9f2022-02-15 10:04:37 +00001274 */
Ronald Croncada4102023-03-07 09:51:39 +01001275 legacy_session_id_len = *(p++);
1276 legacy_session_id = p;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001277
XiaokangQianb67384d2022-04-19 00:02:38 +00001278 /*
1279 * Check we have enough data for the legacy session identifier
Jerry Yue95c8af2022-07-26 15:48:20 +08001280 * and the ciphersuite list length.
XiaokangQianb67384d2022-04-19 00:02:38 +00001281 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001282 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, legacy_session_id_len + 2);
XiaokangQian4080a7f2022-04-11 09:55:18 +00001283 p += legacy_session_id_len;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001284
Ronald Cron2f16b4e2023-03-07 10:07:32 +01001285 /* ...
1286 * CipherSuite cipher_suites<2..2^16-2>;
1287 * ...
1288 * with CipherSuite defined as:
1289 * uint8 CipherSuite[2];
1290 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001291 cipher_suites_len = MBEDTLS_GET_UINT16_BE(p, 0);
XiaokangQian7807f9f2022-02-15 10:04:37 +00001292 p += 2;
Ronald Cron2f16b4e2023-03-07 10:07:32 +01001293 cipher_suites = p;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001294
Ronald Cronfc7ae872023-02-16 15:32:19 +01001295 /*
1296 * The length of the ciphersuite list has to be even.
1297 */
1298 if (cipher_suites_len & 1) {
1299 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR,
1300 MBEDTLS_ERR_SSL_DECODE_ERROR);
1301 return MBEDTLS_ERR_SSL_DECODE_ERROR;
1302 }
1303
XiaokangQianb67384d2022-04-19 00:02:38 +00001304 /* Check we have enough data for the ciphersuite list, the legacy
1305 * compression methods and the length of the extensions.
Jerry Yue95c8af2022-07-26 15:48:20 +08001306 *
1307 * cipher_suites cipher_suites_len bytes
1308 * legacy_compression_methods 2 bytes
1309 * extensions_len 2 bytes
XiaokangQianb67384d2022-04-19 00:02:38 +00001310 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001311 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, cipher_suites_len + 2 + 2);
Ronald Cron8c527d02023-03-07 15:47:47 +01001312 p += cipher_suites_len;
1313 cipher_suites_end = p;
Jerry Yu5725f1c2022-08-21 17:27:16 +08001314
1315 /*
Ronald Cron8c527d02023-03-07 15:47:47 +01001316 * Search for the supported versions extension and parse it to determine
1317 * if the client supports TLS 1.3.
1318 */
1319 ret = mbedtls_ssl_tls13_is_supported_versions_ext_present_in_exts(
1320 ssl, p + 2, end,
Ronald Croneff56732023-04-03 17:36:31 +02001321 &supported_versions_data, &supported_versions_data_end);
Ronald Cron8c527d02023-03-07 15:47:47 +01001322 if (ret < 0) {
1323 MBEDTLS_SSL_DEBUG_RET(1,
1324 ("mbedtls_ssl_tls13_is_supported_versions_ext_present_in_exts"), ret);
1325 return ret;
1326 }
1327
1328 if (ret == 0) {
Ronald Cron5af4c7f2023-03-07 20:46:59 +01001329 return SSL_CLIENT_HELLO_TLS1_2;
Ronald Cron8c527d02023-03-07 15:47:47 +01001330 }
1331
Ronald Cron5af4c7f2023-03-07 20:46:59 +01001332 if (ret == 1) {
1333 ret = ssl_tls13_parse_supported_versions_ext(ssl,
Ronald Croneff56732023-04-03 17:36:31 +02001334 supported_versions_data,
1335 supported_versions_data_end);
Ronald Cron5af4c7f2023-03-07 20:46:59 +01001336 if (ret < 0) {
1337 MBEDTLS_SSL_DEBUG_RET(1,
1338 ("ssl_tls13_parse_supported_versions_ext"), ret);
1339 return ret;
1340 }
1341
Ronald Cronb828c7d2023-04-03 16:37:22 +02001342 /*
1343 * The supported versions extension was parsed successfully as the
1344 * value returned by ssl_tls13_parse_supported_versions_ext() is
1345 * positive. The return value is then equal to
1346 * MBEDTLS_SSL_VERSION_TLS1_2 or MBEDTLS_SSL_VERSION_TLS1_3, defining
1347 * the TLS version to negotiate.
1348 */
Ronald Cron5af4c7f2023-03-07 20:46:59 +01001349 if (MBEDTLS_SSL_VERSION_TLS1_2 == ret) {
1350 return SSL_CLIENT_HELLO_TLS1_2;
1351 }
Ronald Cron8c527d02023-03-07 15:47:47 +01001352 }
1353
1354 /*
1355 * We negotiate TLS 1.3.
Ronald Cron64582392023-03-07 09:21:40 +01001356 */
1357 ssl->tls_version = MBEDTLS_SSL_VERSION_TLS1_3;
Ronald Cron64582392023-03-07 09:21:40 +01001358 ssl->session_negotiate->tls_version = MBEDTLS_SSL_VERSION_TLS1_3;
1359 ssl->session_negotiate->endpoint = ssl->conf->endpoint;
Ronald Cron64582392023-03-07 09:21:40 +01001360
1361 /*
Ronald Crondad02b22023-04-06 09:57:52 +02001362 * We are negotiating the version 1.3 of the protocol. Do what we have
Ronald Croncada4102023-03-07 09:51:39 +01001363 * postponed: copy of the client random bytes, copy of the legacy session
Ronald Cron2f16b4e2023-03-07 10:07:32 +01001364 * identifier and selection of the TLS 1.3 cipher suite.
Ronald Crond540d992023-03-07 09:41:48 +01001365 */
1366 MBEDTLS_SSL_DEBUG_BUF(3, "client hello, random bytes",
1367 random, MBEDTLS_CLIENT_HELLO_RANDOM_LEN);
1368 memcpy(&handshake->randbytes[0], random, MBEDTLS_CLIENT_HELLO_RANDOM_LEN);
1369
Ronald Croncada4102023-03-07 09:51:39 +01001370 if (legacy_session_id_len > sizeof(ssl->session_negotiate->id)) {
1371 MBEDTLS_SSL_DEBUG_MSG(1, ("bad client hello message"));
1372 return MBEDTLS_ERR_SSL_DECODE_ERROR;
1373 }
1374 ssl->session_negotiate->id_len = legacy_session_id_len;
1375 MBEDTLS_SSL_DEBUG_BUF(3, "client hello, session id",
1376 legacy_session_id, legacy_session_id_len);
1377 memcpy(&ssl->session_negotiate->id[0],
1378 legacy_session_id, legacy_session_id_len);
1379
Ronald Crond540d992023-03-07 09:41:48 +01001380 /*
Jerry Yu5725f1c2022-08-21 17:27:16 +08001381 * Search for a matching ciphersuite
1382 */
Ronald Cron2f16b4e2023-03-07 10:07:32 +01001383 MBEDTLS_SSL_DEBUG_BUF(3, "client hello, list of cipher suites",
1384 cipher_suites, cipher_suites_len);
Jerry Yu5725f1c2022-08-21 17:27:16 +08001385
Ronald Cron89089cc2024-02-14 18:18:08 +01001386 ssl_tls13_select_ciphersuite(ssl, cipher_suites, cipher_suites_end,
1387 0, PSA_ALG_NONE, &handshake->ciphersuite_info);
Jerry Yu5725f1c2022-08-21 17:27:16 +08001388
Gilles Peskine449bd832023-01-11 14:50:10 +01001389 if (handshake->ciphersuite_info == NULL) {
1390 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE,
1391 MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE);
1392 return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
Jerry Yu5725f1c2022-08-21 17:27:16 +08001393 }
Ronald Cron89089cc2024-02-14 18:18:08 +01001394 ssl->session_negotiate->ciphersuite = handshake->ciphersuite_info->id;
1395
1396 MBEDTLS_SSL_DEBUG_MSG(2, ("selected ciphersuite: %04x - %s",
1397 ((unsigned) handshake->ciphersuite_info->id),
1398 handshake->ciphersuite_info->name));
Jerry Yuc1be19f2022-04-23 16:11:39 +08001399
XiaokangQian4080a7f2022-04-11 09:55:18 +00001400 /* ...
XiaokangQianb67384d2022-04-19 00:02:38 +00001401 * opaque legacy_compression_methods<1..2^8-1>;
XiaokangQian4080a7f2022-04-11 09:55:18 +00001402 * ...
XiaokangQian7807f9f2022-02-15 10:04:37 +00001403 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001404 if (p[0] != 1 || p[1] != MBEDTLS_SSL_COMPRESS_NULL) {
1405 MBEDTLS_SSL_DEBUG_MSG(1, ("bad legacy compression method"));
1406 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER,
1407 MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER);
1408 return MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001409 }
XiaokangQianb67384d2022-04-19 00:02:38 +00001410 p += 2;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001411
Jerry Yuc1be19f2022-04-23 16:11:39 +08001412 /* ...
XiaokangQianb67384d2022-04-19 00:02:38 +00001413 * Extension extensions<8..2^16-1>;
Jerry Yuc1be19f2022-04-23 16:11:39 +08001414 * ...
XiaokangQianb67384d2022-04-19 00:02:38 +00001415 * with Extension defined as:
1416 * struct {
1417 * ExtensionType extension_type;
1418 * opaque extension_data<0..2^16-1>;
1419 * } Extension;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001420 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001421 extensions_len = MBEDTLS_GET_UINT16_BE(p, 0);
XiaokangQian7807f9f2022-02-15 10:04:37 +00001422 p += 2;
Gilles Peskine449bd832023-01-11 14:50:10 +01001423 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, extensions_len);
XiaokangQiane8ff3502022-04-22 02:34:40 +00001424 extensions_end = p + extensions_len;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001425
Gilles Peskine449bd832023-01-11 14:50:10 +01001426 MBEDTLS_SSL_DEBUG_BUF(3, "client hello extensions", p, extensions_len);
Jerry Yu63a459c2022-10-31 13:38:40 +08001427 handshake->received_extensions = MBEDTLS_SSL_EXT_MASK_NONE;
Jerry Yu0c354a22022-08-29 15:25:36 +08001428
Gilles Peskine449bd832023-01-11 14:50:10 +01001429 while (p < extensions_end) {
XiaokangQian7807f9f2022-02-15 10:04:37 +00001430 unsigned int extension_type;
1431 size_t extension_data_len;
1432 const unsigned char *extension_data_end;
Jerry Yu263dbf72022-10-26 10:51:27 +08001433 uint32_t allowed_exts = MBEDTLS_SSL_TLS1_3_ALLOWED_EXTS_OF_CH;
1434
Ronald Cron5fbd2702024-02-14 10:03:36 +01001435 if (ssl->handshake->hello_retry_request_flag) {
Jerry Yu263dbf72022-10-26 10:51:27 +08001436 /* Do not accept early data extension in 2nd ClientHello */
1437 allowed_exts &= ~MBEDTLS_SSL_EXT_MASK(EARLY_DATA);
1438 }
XiaokangQian7807f9f2022-02-15 10:04:37 +00001439
Jerry Yu0c354a22022-08-29 15:25:36 +08001440 /* RFC 8446, section 4.2.11
Jerry Yu1c9247c2022-07-21 12:37:39 +08001441 *
1442 * The "pre_shared_key" extension MUST be the last extension in the
1443 * ClientHello (this facilitates implementation as described below).
1444 * Servers MUST check that it is the last extension and otherwise fail
1445 * the handshake with an "illegal_parameter" alert.
1446 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001447 if (handshake->received_extensions & MBEDTLS_SSL_EXT_MASK(PRE_SHARED_KEY)) {
Jerry Yu1c9247c2022-07-21 12:37:39 +08001448 MBEDTLS_SSL_DEBUG_MSG(
Gilles Peskine449bd832023-01-11 14:50:10 +01001449 3, ("pre_shared_key is not last extension."));
Jerry Yu1c9247c2022-07-21 12:37:39 +08001450 MBEDTLS_SSL_PEND_FATAL_ALERT(
1451 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER,
Gilles Peskine449bd832023-01-11 14:50:10 +01001452 MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER);
1453 return MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER;
Jerry Yu1c9247c2022-07-21 12:37:39 +08001454 }
1455
Gilles Peskine449bd832023-01-11 14:50:10 +01001456 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, extensions_end, 4);
1457 extension_type = MBEDTLS_GET_UINT16_BE(p, 0);
1458 extension_data_len = MBEDTLS_GET_UINT16_BE(p, 2);
XiaokangQian7807f9f2022-02-15 10:04:37 +00001459 p += 4;
1460
Gilles Peskine449bd832023-01-11 14:50:10 +01001461 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, extensions_end, extension_data_len);
XiaokangQian7807f9f2022-02-15 10:04:37 +00001462 extension_data_end = p + extension_data_len;
1463
Jerry Yuc4bf5d62022-10-29 09:08:47 +08001464 ret = mbedtls_ssl_tls13_check_received_extension(
Gilles Peskine449bd832023-01-11 14:50:10 +01001465 ssl, MBEDTLS_SSL_HS_CLIENT_HELLO, extension_type,
Jerry Yu263dbf72022-10-26 10:51:27 +08001466 allowed_exts);
Gilles Peskine449bd832023-01-11 14:50:10 +01001467 if (ret != 0) {
1468 return ret;
1469 }
Jerry Yue18dc7e2022-08-04 16:29:22 +08001470
Gilles Peskine449bd832023-01-11 14:50:10 +01001471 switch (extension_type) {
XiaokangQian40a35232022-05-07 09:02:40 +00001472#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
1473 case MBEDTLS_TLS_EXT_SERVERNAME:
Gilles Peskine449bd832023-01-11 14:50:10 +01001474 MBEDTLS_SSL_DEBUG_MSG(3, ("found ServerName extension"));
1475 ret = mbedtls_ssl_parse_server_name_ext(ssl, p,
1476 extension_data_end);
1477 if (ret != 0) {
XiaokangQian40a35232022-05-07 09:02:40 +00001478 MBEDTLS_SSL_DEBUG_RET(
Gilles Peskine449bd832023-01-11 14:50:10 +01001479 1, "mbedtls_ssl_parse_servername_ext", ret);
1480 return ret;
XiaokangQian40a35232022-05-07 09:02:40 +00001481 }
XiaokangQian40a35232022-05-07 09:02:40 +00001482 break;
1483#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
1484
Przemek Stekiel8c0a9532023-06-15 16:48:19 +02001485#if defined(PSA_WANT_ALG_ECDH) || defined(PSA_WANT_ALG_FFDH)
XiaokangQian7807f9f2022-02-15 10:04:37 +00001486 case MBEDTLS_TLS_EXT_SUPPORTED_GROUPS:
Gilles Peskine449bd832023-01-11 14:50:10 +01001487 MBEDTLS_SSL_DEBUG_MSG(3, ("found supported group extension"));
XiaokangQian7807f9f2022-02-15 10:04:37 +00001488
1489 /* Supported Groups Extension
1490 *
1491 * When sent by the client, the "supported_groups" extension
1492 * indicates the named groups which the client supports,
1493 * ordered from most preferred to least preferred.
1494 */
Jerry Yuc1be19f2022-04-23 16:11:39 +08001495 ret = ssl_tls13_parse_supported_groups_ext(
Gilles Peskine449bd832023-01-11 14:50:10 +01001496 ssl, p, extension_data_end);
1497 if (ret != 0) {
Xiaokang Qian9f1747b2023-03-30 02:50:04 +00001498 MBEDTLS_SSL_DEBUG_RET(
Xiaokang Qian8bce0e62023-04-04 10:15:48 +00001499 1, "ssl_tls13_parse_supported_groups_ext", ret);
Gilles Peskine449bd832023-01-11 14:50:10 +01001500 return ret;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001501 }
1502
XiaokangQian7807f9f2022-02-15 10:04:37 +00001503 break;
Przemek Stekiel8c0a9532023-06-15 16:48:19 +02001504#endif /* PSA_WANT_ALG_ECDH || PSA_WANT_ALG_FFDH*/
XiaokangQian7807f9f2022-02-15 10:04:37 +00001505
Valerio Settic9ae8622023-07-25 11:23:50 +02001506#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_EPHEMERAL_ENABLED)
XiaokangQian7807f9f2022-02-15 10:04:37 +00001507 case MBEDTLS_TLS_EXT_KEY_SHARE:
Gilles Peskine449bd832023-01-11 14:50:10 +01001508 MBEDTLS_SSL_DEBUG_MSG(3, ("found key share extension"));
XiaokangQian7807f9f2022-02-15 10:04:37 +00001509
1510 /*
1511 * Key Share Extension
1512 *
1513 * When sent by the client, the "key_share" extension
1514 * contains the endpoint's cryptographic parameters for
1515 * ECDHE/DHE key establishment methods.
1516 */
Jerry Yuc1be19f2022-04-23 16:11:39 +08001517 ret = ssl_tls13_parse_key_shares_ext(
Gilles Peskine449bd832023-01-11 14:50:10 +01001518 ssl, p, extension_data_end);
1519 if (ret == SSL_TLS1_3_PARSE_KEY_SHARES_EXT_NO_MATCH) {
Ronald Cron8a74f072023-06-14 17:59:29 +02001520 MBEDTLS_SSL_DEBUG_MSG(2, ("No usable share for key agreement."));
1521 no_usable_share_for_key_agreement = 1;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001522 }
1523
Gilles Peskine449bd832023-01-11 14:50:10 +01001524 if (ret < 0) {
Jerry Yu582dd062022-04-22 21:59:01 +08001525 MBEDTLS_SSL_DEBUG_RET(
Gilles Peskine449bd832023-01-11 14:50:10 +01001526 1, "ssl_tls13_parse_key_shares_ext", ret);
1527 return ret;
Jerry Yu582dd062022-04-22 21:59:01 +08001528 }
XiaokangQian7807f9f2022-02-15 10:04:37 +00001529
XiaokangQian7807f9f2022-02-15 10:04:37 +00001530 break;
Valerio Settic9ae8622023-07-25 11:23:50 +02001531#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_EPHEMERAL_ENABLED */
XiaokangQian7807f9f2022-02-15 10:04:37 +00001532
1533 case MBEDTLS_TLS_EXT_SUPPORTED_VERSIONS:
Ronald Cron8c527d02023-03-07 15:47:47 +01001534 /* Already parsed */
XiaokangQian7807f9f2022-02-15 10:04:37 +00001535 break;
1536
Ronald Cron41a443a2022-10-04 16:38:25 +02001537#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED)
Jerry Yue19e3b92022-07-08 12:04:51 +00001538 case MBEDTLS_TLS_EXT_PSK_KEY_EXCHANGE_MODES:
Xiaokang Qian9f1747b2023-03-30 02:50:04 +00001539 MBEDTLS_SSL_DEBUG_MSG(
1540 3, ("found psk key exchange modes extension"));
Jerry Yue19e3b92022-07-08 12:04:51 +00001541
1542 ret = ssl_tls13_parse_key_exchange_modes_ext(
Gilles Peskine449bd832023-01-11 14:50:10 +01001543 ssl, p, extension_data_end);
1544 if (ret != 0) {
Jerry Yue19e3b92022-07-08 12:04:51 +00001545 MBEDTLS_SSL_DEBUG_RET(
Gilles Peskine449bd832023-01-11 14:50:10 +01001546 1, "ssl_tls13_parse_key_exchange_modes_ext", ret);
1547 return ret;
Jerry Yue19e3b92022-07-08 12:04:51 +00001548 }
1549
Jerry Yue19e3b92022-07-08 12:04:51 +00001550 break;
Ronald Cron41a443a2022-10-04 16:38:25 +02001551#endif
Jerry Yue19e3b92022-07-08 12:04:51 +00001552
Jerry Yu1c105562022-07-10 06:32:38 +00001553 case MBEDTLS_TLS_EXT_PRE_SHARED_KEY:
Gilles Peskine449bd832023-01-11 14:50:10 +01001554 MBEDTLS_SSL_DEBUG_MSG(3, ("found pre_shared_key extension"));
1555 if ((handshake->received_extensions &
1556 MBEDTLS_SSL_EXT_MASK(PSK_KEY_EXCHANGE_MODES)) == 0) {
Jerry Yu13ab81d2022-07-22 23:17:11 +08001557 MBEDTLS_SSL_PEND_FATAL_ALERT(
1558 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER,
Gilles Peskine449bd832023-01-11 14:50:10 +01001559 MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER);
1560 return MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER;
Jerry Yu13ab81d2022-07-22 23:17:11 +08001561 }
Ronald Cron41a443a2022-10-04 16:38:25 +02001562#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED)
Jerry Yu1c105562022-07-10 06:32:38 +00001563 /* Delay processing of the PSK identity once we have
1564 * found out which algorithms to use. We keep a pointer
1565 * to the buffer and the size for later processing.
1566 */
Jerry Yu29d9faa2022-08-23 17:52:45 +08001567 pre_shared_key_ext = p;
Jerry Yu1c105562022-07-10 06:32:38 +00001568 pre_shared_key_ext_end = extension_data_end;
Jerry Yue18dc7e2022-08-04 16:29:22 +08001569#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED */
Jerry Yu1c105562022-07-10 06:32:38 +00001570 break;
Jerry Yu1c105562022-07-10 06:32:38 +00001571
XiaokangQianacb39922022-06-17 10:18:48 +00001572#if defined(MBEDTLS_SSL_ALPN)
1573 case MBEDTLS_TLS_EXT_ALPN:
Gilles Peskine449bd832023-01-11 14:50:10 +01001574 MBEDTLS_SSL_DEBUG_MSG(3, ("found alpn extension"));
XiaokangQianacb39922022-06-17 10:18:48 +00001575
Gilles Peskine449bd832023-01-11 14:50:10 +01001576 ret = mbedtls_ssl_parse_alpn_ext(ssl, p, extension_data_end);
1577 if (ret != 0) {
XiaokangQianacb39922022-06-17 10:18:48 +00001578 MBEDTLS_SSL_DEBUG_RET(
Gilles Peskine449bd832023-01-11 14:50:10 +01001579 1, ("mbedtls_ssl_parse_alpn_ext"), ret);
1580 return ret;
XiaokangQianacb39922022-06-17 10:18:48 +00001581 }
XiaokangQianacb39922022-06-17 10:18:48 +00001582 break;
1583#endif /* MBEDTLS_SSL_ALPN */
1584
Ronald Cron928cbd32022-10-04 16:14:26 +02001585#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED)
XiaokangQian7807f9f2022-02-15 10:04:37 +00001586 case MBEDTLS_TLS_EXT_SIG_ALG:
Gilles Peskine449bd832023-01-11 14:50:10 +01001587 MBEDTLS_SSL_DEBUG_MSG(3, ("found signature_algorithms extension"));
XiaokangQian7807f9f2022-02-15 10:04:37 +00001588
Gabor Mezei078e8032022-04-27 21:17:56 +02001589 ret = mbedtls_ssl_parse_sig_alg_ext(
Gilles Peskine449bd832023-01-11 14:50:10 +01001590 ssl, p, extension_data_end);
1591 if (ret != 0) {
Xiaokang Qian49f39c12023-04-06 02:31:02 +00001592 MBEDTLS_SSL_DEBUG_RET(
1593 1, "mbedtls_ssl_parse_sig_alg_ext", ret);
Gilles Peskine449bd832023-01-11 14:50:10 +01001594 return ret;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001595 }
XiaokangQian7807f9f2022-02-15 10:04:37 +00001596 break;
Ronald Cron928cbd32022-10-04 16:14:26 +02001597#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED */
XiaokangQian7807f9f2022-02-15 10:04:37 +00001598
Jan Bruckner151f6422023-02-10 12:45:19 +01001599#if defined(MBEDTLS_SSL_RECORD_SIZE_LIMIT)
1600 case MBEDTLS_TLS_EXT_RECORD_SIZE_LIMIT:
1601 MBEDTLS_SSL_DEBUG_MSG(3, ("found record_size_limit extension"));
1602
Xiaokang Qian9f1747b2023-03-30 02:50:04 +00001603 ret = mbedtls_ssl_tls13_parse_record_size_limit_ext(
1604 ssl, p, extension_data_end);
Jan Brucknerf482dcc2023-03-15 09:09:06 +01001605 if (ret != 0) {
1606 MBEDTLS_SSL_DEBUG_RET(
1607 1, ("mbedtls_ssl_tls13_parse_record_size_limit_ext"), ret);
1608 return ret;
1609 }
Jan Bruckner151f6422023-02-10 12:45:19 +01001610 break;
1611#endif /* MBEDTLS_SSL_RECORD_SIZE_LIMIT */
1612
XiaokangQian7807f9f2022-02-15 10:04:37 +00001613 default:
Jerry Yu79aa7212022-11-08 21:30:21 +08001614 MBEDTLS_SSL_PRINT_EXT(
Jerry Yu63a459c2022-10-31 13:38:40 +08001615 3, MBEDTLS_SSL_HS_CLIENT_HELLO,
Gilles Peskine449bd832023-01-11 14:50:10 +01001616 extension_type, "( ignored )");
Jerry Yu0c354a22022-08-29 15:25:36 +08001617 break;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001618 }
1619
1620 p += extension_data_len;
1621 }
1622
Gilles Peskine449bd832023-01-11 14:50:10 +01001623 MBEDTLS_SSL_PRINT_EXTS(3, MBEDTLS_SSL_HS_CLIENT_HELLO,
1624 handshake->received_extensions);
Jerry Yue18dc7e2022-08-04 16:29:22 +08001625
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +01001626 ret = mbedtls_ssl_add_hs_hdr_to_checksum(ssl,
1627 MBEDTLS_SSL_HS_CLIENT_HELLO,
1628 p - buf);
1629 if (0 != ret) {
1630 MBEDTLS_SSL_DEBUG_RET(1, ("mbedtls_ssl_add_hs_hdr_to_checksum"), ret);
1631 return ret;
1632 }
Jerry Yu032b15ce2022-07-11 06:10:03 +00001633
Ronald Cron41a443a2022-10-04 16:38:25 +02001634#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED)
XiaokangQian7807f9f2022-02-15 10:04:37 +00001635 /* Update checksum with either
1636 * - The entire content of the CH message, if no PSK extension is present
1637 * - The content up to but excluding the PSK extension, if present.
Ronald Cron12e72f12024-02-14 15:49:38 +01001638 * Always parse the pre-shared key extension when present in the
1639 * ClientHello even if some pre-requisites for PSK key exchange modes are
1640 * not met. That way we always validate the syntax of the extension.
XiaokangQian7807f9f2022-02-15 10:04:37 +00001641 */
Ronald Cron12e72f12024-02-14 15:49:38 +01001642 if (handshake->received_extensions & MBEDTLS_SSL_EXT_MASK(PRE_SHARED_KEY)) {
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +01001643 ret = handshake->update_checksum(ssl, buf,
1644 pre_shared_key_ext - buf);
1645 if (0 != ret) {
1646 MBEDTLS_SSL_DEBUG_RET(1, ("update_checksum"), ret);
1647 return ret;
1648 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001649 ret = ssl_tls13_parse_pre_shared_key_ext(ssl,
1650 pre_shared_key_ext,
1651 pre_shared_key_ext_end,
1652 cipher_suites,
Ronald Cron79cdd412024-02-20 17:19:28 +01001653 cipher_suites_end,
1654 &psk);
1655 if (ret == 0) {
1656 got_psk = 1;
1657 } else if (ret != MBEDTLS_ERR_SSL_UNKNOWN_IDENTITY) {
Jerry Yu63a459c2022-10-31 13:38:40 +08001658 MBEDTLS_SSL_DEBUG_RET(
Gilles Peskine449bd832023-01-11 14:50:10 +01001659 1, "ssl_tls13_parse_pre_shared_key_ext", ret);
1660 return ret;
Jerry Yu1c105562022-07-10 06:32:38 +00001661 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001662 } else
Ronald Cron41a443a2022-10-04 16:38:25 +02001663#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED */
Jerry Yu1c105562022-07-10 06:32:38 +00001664 {
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +01001665 ret = handshake->update_checksum(ssl, buf, p - buf);
1666 if (0 != ret) {
1667 MBEDTLS_SSL_DEBUG_RET(1, ("update_checksum"), ret);
1668 return ret;
1669 }
Jerry Yu1c105562022-07-10 06:32:38 +00001670 }
XiaokangQian7807f9f2022-02-15 10:04:37 +00001671
Ronald Cron79cdd412024-02-20 17:19:28 +01001672 /*
1673 * Determine the key exchange algorithm to use.
1674 * There are three types of key exchanges supported in TLS 1.3:
1675 * - (EC)DH with ECDSA,
1676 * - (EC)DH with PSK,
1677 * - plain PSK.
1678 *
1679 * The PSK-based key exchanges may additionally be used with 0-RTT.
1680 *
1681 * Our built-in order of preference is
1682 * 1 ) (EC)DHE-PSK Mode ( psk_ephemeral )
1683 * 2 ) Certificate Mode ( ephemeral )
1684 * 3 ) Plain PSK Mode ( psk )
1685 */
1686#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED)
1687 if (got_psk && (psk.key_exchange_mode ==
1688 MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK_EPHEMERAL)) {
1689 handshake->key_exchange_mode =
1690 MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK_EPHEMERAL;
1691 MBEDTLS_SSL_DEBUG_MSG(2, ("key exchange mode: psk_ephemeral"));
1692
1693 } else
1694#endif
1695 if (ssl_tls13_key_exchange_is_ephemeral_available(ssl)) {
1696 handshake->key_exchange_mode =
1697 MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL;
1698 MBEDTLS_SSL_DEBUG_MSG(2, ("key exchange mode: ephemeral"));
1699
1700 }
1701#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED)
1702 else if (got_psk && (psk.key_exchange_mode ==
1703 MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK)) {
1704 handshake->key_exchange_mode = MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK;
1705 MBEDTLS_SSL_DEBUG_MSG(2, ("key exchange mode: psk"));
1706 }
1707#endif
1708 else {
1709 MBEDTLS_SSL_DEBUG_MSG(
1710 1,
1711 ("ClientHello message misses mandatory extensions."));
1712 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_MISSING_EXTENSION,
1713 MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER);
1714 return MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER;
Gilles Peskine449bd832023-01-11 14:50:10 +01001715 }
XiaokangQian7807f9f2022-02-15 10:04:37 +00001716
Ronald Cron3e47eec2024-02-21 10:29:24 +01001717#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED)
Ronald Cron74a16292024-02-23 17:07:41 +01001718 if (handshake->key_exchange_mode &
1719 MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK_ALL) {
1720 handshake->ciphersuite_info = psk.ciphersuite_info;
1721 ssl->session_negotiate->ciphersuite = psk.ciphersuite_info->id;
1722
1723 MBEDTLS_SSL_DEBUG_MSG(2, ("Select PSK ciphersuite: %04x - %s",
1724 ((unsigned) psk.ciphersuite_info->id),
1725 psk.ciphersuite_info->name));
1726
1727 if (psk.type == MBEDTLS_SSL_TLS1_3_PSK_RESUMPTION) {
1728 handshake->resume = 1;
1729 }
Ronald Cron79cdd412024-02-20 17:19:28 +01001730 }
Ronald Cron3e47eec2024-02-21 10:29:24 +01001731#endif
Ronald Cron79cdd412024-02-20 17:19:28 +01001732
1733 if (handshake->key_exchange_mode !=
Ronald Cron8a74f072023-06-14 17:59:29 +02001734 MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK) {
1735 hrr_required = (no_usable_share_for_key_agreement != 0);
1736 }
1737
Gilles Peskine449bd832023-01-11 14:50:10 +01001738 mbedtls_ssl_optimize_checksum(ssl, handshake->ciphersuite_info);
Jerry Yue5834fd2022-08-29 20:16:09 +08001739
Gilles Peskine449bd832023-01-11 14:50:10 +01001740 return hrr_required ? SSL_CLIENT_HELLO_HRR_REQUIRED : SSL_CLIENT_HELLO_OK;
XiaokangQian75fe8c72022-06-15 09:42:45 +00001741}
1742
Jerry Yuab0da372023-02-08 13:55:24 +08001743#if defined(MBEDTLS_SSL_EARLY_DATA)
Jerry Yu4caf3ca2023-11-15 16:13:47 +08001744static int ssl_tls13_check_early_data_requirements(mbedtls_ssl_context *ssl)
Jerry Yuab0da372023-02-08 13:55:24 +08001745{
1746 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
1747
Jerry Yu985c9672022-12-04 14:06:30 +08001748 if (ssl->conf->early_data_enabled == MBEDTLS_SSL_EARLY_DATA_DISABLED) {
1749 MBEDTLS_SSL_DEBUG_MSG(
Jerry Yu985c9672022-12-04 14:06:30 +08001750 1,
Jerry Yu454dda32023-10-31 15:13:54 +08001751 ("EarlyData: rejected, feature disabled in server configuration."));
Jerry Yu4caf3ca2023-11-15 16:13:47 +08001752 return -1;
Ronald Cron7b6ee942024-01-12 10:29:55 +01001753 }
1754
Jerry Yu454dda32023-10-31 15:13:54 +08001755 if (!handshake->resume) {
1756 /* We currently support early data only in the case of PSKs established
1757 via a NewSessionTicket message thus in the case of a session
1758 resumption. */
Jerry Yu985c9672022-12-04 14:06:30 +08001759 MBEDTLS_SSL_DEBUG_MSG(
Jerry Yu7ef9fd82023-11-07 14:30:38 +08001760 1, ("EarlyData: rejected, not a session resumption."));
Jerry Yu4caf3ca2023-11-15 16:13:47 +08001761 return -1;
Jerry Yu985c9672022-12-04 14:06:30 +08001762 }
1763
Jerry Yu82fd6c12023-10-31 16:32:19 +08001764 /* RFC 8446 4.2.10
1765 *
1766 * In order to accept early data, the server MUST have accepted a PSK cipher
1767 * suite and selected the first key offered in the client's "pre_shared_key"
1768 * extension. In addition, it MUST verify that the following values are the
1769 * same as those associated with the selected PSK:
1770 * - The TLS version number
1771 * - The selected cipher suite
1772 * - The selected ALPN [RFC7301] protocol, if any
1773 *
1774 * NOTE:
Jerry Yu7ef9fd82023-11-07 14:30:38 +08001775 * - The TLS version number is checked in
1776 * ssl_tls13_offered_psks_check_identity_match_ticket().
1777 * - ALPN is not checked for the time being (TODO).
Jerry Yu82fd6c12023-10-31 16:32:19 +08001778 */
1779
1780 if (handshake->selected_identity != 0) {
1781 MBEDTLS_SSL_DEBUG_MSG(
Jerry Yu7ef9fd82023-11-07 14:30:38 +08001782 1, ("EarlyData: rejected, the selected key in "
1783 "`pre_shared_key` is not the first one."));
Jerry Yu4caf3ca2023-11-15 16:13:47 +08001784 return -1;
Jerry Yu82fd6c12023-10-31 16:32:19 +08001785 }
1786
1787 if (handshake->ciphersuite_info->id !=
1788 ssl->session_negotiate->ciphersuite) {
1789 MBEDTLS_SSL_DEBUG_MSG(
Jerry Yu7ef9fd82023-11-07 14:30:38 +08001790 1, ("EarlyData: rejected, the selected ciphersuite is not the one "
1791 "of the selected pre-shared key."));
Jerry Yu4caf3ca2023-11-15 16:13:47 +08001792 return -1;
Jerry Yu82fd6c12023-10-31 16:32:19 +08001793
1794 }
Jerry Yu985c9672022-12-04 14:06:30 +08001795
Pengyu Lv94a42cc2023-12-06 10:04:17 +08001796 if (!mbedtls_ssl_tls13_session_ticket_allow_early_data(ssl->session_negotiate)) {
Jerry Yufceddb32022-12-12 15:30:34 +08001797 MBEDTLS_SSL_DEBUG_MSG(
1798 1,
Jerry Yuea96ac32023-11-21 17:06:36 +08001799 ("EarlyData: rejected, early_data not allowed in ticket "
1800 "permission bits."));
Jerry Yu4caf3ca2023-11-15 16:13:47 +08001801 return -1;
Jerry Yufceddb32022-12-12 15:30:34 +08001802 }
Jerry Yu985c9672022-12-04 14:06:30 +08001803
Jerry Yu4caf3ca2023-11-15 16:13:47 +08001804 return 0;
Jerry Yuab0da372023-02-08 13:55:24 +08001805}
1806#endif /* MBEDTLS_SSL_EARLY_DATA */
1807
XiaokangQian75fe8c72022-06-15 09:42:45 +00001808/* Update the handshake state machine */
1809
Ronald Cronce7d76e2022-07-08 18:56:49 +02001810MBEDTLS_CHECK_RETURN_CRITICAL
Ronald Cron7b6ee942024-01-12 10:29:55 +01001811static int ssl_tls13_postprocess_client_hello(mbedtls_ssl_context *ssl,
1812 int hrr_required)
XiaokangQian75fe8c72022-06-15 09:42:45 +00001813{
1814 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1815
XiaokangQian7807f9f2022-02-15 10:04:37 +00001816 /*
XiaokangQianfb665a82022-06-15 03:57:21 +00001817 * Server certificate selection
1818 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001819 if (ssl->conf->f_cert_cb && (ret = ssl->conf->f_cert_cb(ssl)) != 0) {
1820 MBEDTLS_SSL_DEBUG_RET(1, "f_cert_cb", ret);
1821 return ret;
XiaokangQianfb665a82022-06-15 03:57:21 +00001822 }
1823#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
1824 ssl->handshake->sni_name = NULL;
1825 ssl->handshake->sni_name_len = 0;
1826#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
XiaokangQian7807f9f2022-02-15 10:04:37 +00001827
Gilles Peskine449bd832023-01-11 14:50:10 +01001828 ret = mbedtls_ssl_tls13_key_schedule_stage_early(ssl);
1829 if (ret != 0) {
1830 MBEDTLS_SSL_DEBUG_RET(1,
1831 "mbedtls_ssl_tls1_3_key_schedule_stage_early", ret);
1832 return ret;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001833 }
1834
Jerry Yuab0da372023-02-08 13:55:24 +08001835#if defined(MBEDTLS_SSL_EARLY_DATA)
Jerry Yu4caf3ca2023-11-15 16:13:47 +08001836 if (ssl->handshake->received_extensions & MBEDTLS_SSL_EXT_MASK(EARLY_DATA)) {
Ronald Cron31e2d832024-02-05 16:45:57 +01001837 ssl->handshake->early_data_accepted =
1838 (!hrr_required) && (ssl_tls13_check_early_data_requirements(ssl) == 0);
Jerry Yu4e9b70e2022-12-04 14:08:02 +08001839
Jerry Yu4caf3ca2023-11-15 16:13:47 +08001840 if (ssl->handshake->early_data_accepted) {
1841 ret = mbedtls_ssl_tls13_compute_early_transform(ssl);
1842 if (ret != 0) {
1843 MBEDTLS_SSL_DEBUG_RET(
1844 1, "mbedtls_ssl_tls13_compute_early_transform", ret);
1845 return ret;
1846 }
1847 } else {
1848 ssl->discard_early_data_record =
1849 hrr_required ?
1850 MBEDTLS_SSL_EARLY_DATA_DISCARD :
1851 MBEDTLS_SSL_EARLY_DATA_TRY_TO_DEPROTECT_AND_DISCARD;
Jerry Yu4e9b70e2022-12-04 14:08:02 +08001852 }
1853 }
Ronald Cron7b6ee942024-01-12 10:29:55 +01001854#else
1855 ((void) hrr_required);
Jerry Yuab0da372023-02-08 13:55:24 +08001856#endif /* MBEDTLS_SSL_EARLY_DATA */
1857
Gilles Peskine449bd832023-01-11 14:50:10 +01001858 return 0;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001859}
1860
1861/*
XiaokangQianed582dd2022-04-13 08:21:05 +00001862 * Main entry point from the state machine; orchestrates the otherfunctions.
1863 */
1864
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001865MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01001866static int ssl_tls13_process_client_hello(mbedtls_ssl_context *ssl)
XiaokangQianed582dd2022-04-13 08:21:05 +00001867{
1868
XiaokangQian08037552022-04-20 07:16:41 +00001869 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine449bd832023-01-11 14:50:10 +01001870 unsigned char *buf = NULL;
XiaokangQianed582dd2022-04-13 08:21:05 +00001871 size_t buflen = 0;
Jerry Yu4ca91402022-05-09 15:50:57 +08001872 int parse_client_hello_ret;
1873
Gilles Peskine449bd832023-01-11 14:50:10 +01001874 MBEDTLS_SSL_DEBUG_MSG(2, ("=> parse client hello"));
XiaokangQianed582dd2022-04-13 08:21:05 +00001875
Gilles Peskine449bd832023-01-11 14:50:10 +01001876 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_tls13_fetch_handshake_msg(
1877 ssl, MBEDTLS_SSL_HS_CLIENT_HELLO,
1878 &buf, &buflen));
XiaokangQianed582dd2022-04-13 08:21:05 +00001879
Gilles Peskine449bd832023-01-11 14:50:10 +01001880 MBEDTLS_SSL_PROC_CHK_NEG(ssl_tls13_parse_client_hello(ssl, buf,
1881 buf + buflen));
Ronald Cron5af4c7f2023-03-07 20:46:59 +01001882 parse_client_hello_ret = ret; /* Store positive return value of
1883 * parse_client_hello,
1884 * as negative error codes are handled
Jerry Yuf41553b2022-05-09 22:20:30 +08001885 * by MBEDTLS_SSL_PROC_CHK_NEG. */
Jerry Yu4ca91402022-05-09 15:50:57 +08001886
Ronald Cronb828c7d2023-04-03 16:37:22 +02001887 /*
Yanray Wang90acdc62023-12-08 10:29:42 +08001888 * Version 1.2 of the protocol has to be used for the handshake.
1889 * If TLS 1.2 is not supported, abort the handshake. Otherwise, set the
Ronald Cronb828c7d2023-04-03 16:37:22 +02001890 * ssl->keep_current_message flag for the ClientHello to be kept and parsed
1891 * as a TLS 1.2 ClientHello. We also change ssl->tls_version to
1892 * MBEDTLS_SSL_VERSION_TLS1_2 thus from now on mbedtls_ssl_handshake_step()
1893 * will dispatch to the TLS 1.2 state machine.
1894 */
Ronald Cron5af4c7f2023-03-07 20:46:59 +01001895 if (SSL_CLIENT_HELLO_TLS1_2 == parse_client_hello_ret) {
Yanray Wangfb0f47b2023-12-04 15:27:28 +08001896 /* Check if server supports TLS 1.2 */
Yanray Wang408ba6f2023-12-08 10:18:03 +08001897 if (!mbedtls_ssl_conf_is_tls12_enabled(ssl->conf)) {
Yanray Wangfb0f47b2023-12-04 15:27:28 +08001898 MBEDTLS_SSL_DEBUG_MSG(
Yanray Wang177e49a2023-12-08 10:51:04 +08001899 1, ("TLS 1.2 not supported."));
Yanray Wangfb0f47b2023-12-04 15:27:28 +08001900 MBEDTLS_SSL_PEND_FATAL_ALERT(
Yanray Wang2bef9172023-12-08 10:21:53 +08001901 MBEDTLS_SSL_ALERT_MSG_PROTOCOL_VERSION,
1902 MBEDTLS_ERR_SSL_BAD_PROTOCOL_VERSION);
1903 return MBEDTLS_ERR_SSL_BAD_PROTOCOL_VERSION;
Yanray Wangfb0f47b2023-12-04 15:27:28 +08001904 }
Ronald Cron5af4c7f2023-03-07 20:46:59 +01001905 ssl->keep_current_message = 1;
1906 ssl->tls_version = MBEDTLS_SSL_VERSION_TLS1_2;
1907 return 0;
1908 }
1909
Ronald Cron7b6ee942024-01-12 10:29:55 +01001910 MBEDTLS_SSL_PROC_CHK(
1911 ssl_tls13_postprocess_client_hello(ssl, parse_client_hello_ret ==
1912 SSL_CLIENT_HELLO_HRR_REQUIRED));
Jerry Yu582dd062022-04-22 21:59:01 +08001913
Ronald Cron5af4c7f2023-03-07 20:46:59 +01001914 if (SSL_CLIENT_HELLO_OK == parse_client_hello_ret) {
Gilles Peskine449bd832023-01-11 14:50:10 +01001915 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_SERVER_HELLO);
1916 } else {
1917 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_HELLO_RETRY_REQUEST);
1918 }
XiaokangQianed582dd2022-04-13 08:21:05 +00001919
1920cleanup:
1921
Gilles Peskine449bd832023-01-11 14:50:10 +01001922 MBEDTLS_SSL_DEBUG_MSG(2, ("<= parse client hello"));
1923 return ret;
XiaokangQianed582dd2022-04-13 08:21:05 +00001924}
1925
1926/*
Jerry Yu1c3e6882022-04-20 21:23:40 +08001927 * Handler for MBEDTLS_SSL_SERVER_HELLO
Jerry Yu5b64ae92022-03-30 17:15:02 +08001928 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001929MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01001930static int ssl_tls13_prepare_server_hello(mbedtls_ssl_context *ssl)
Jerry Yuf4b27e42022-03-30 17:32:21 +08001931{
Jerry Yu637a3f12022-04-20 21:37:58 +08001932 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1933 unsigned char *server_randbytes =
Gilles Peskine449bd832023-01-11 14:50:10 +01001934 ssl->handshake->randbytes + MBEDTLS_CLIENT_HELLO_RANDOM_LEN;
1935 if (ssl->conf->f_rng == NULL) {
1936 MBEDTLS_SSL_DEBUG_MSG(1, ("no RNG provided"));
1937 return MBEDTLS_ERR_SSL_NO_RNG;
Jerry Yuf4b27e42022-03-30 17:32:21 +08001938 }
1939
Gilles Peskine449bd832023-01-11 14:50:10 +01001940 if ((ret = ssl->conf->f_rng(ssl->conf->p_rng, server_randbytes,
1941 MBEDTLS_SERVER_HELLO_RANDOM_LEN)) != 0) {
1942 MBEDTLS_SSL_DEBUG_RET(1, "f_rng", ret);
1943 return ret;
Jerry Yuf4b27e42022-03-30 17:32:21 +08001944 }
1945
Gilles Peskine449bd832023-01-11 14:50:10 +01001946 MBEDTLS_SSL_DEBUG_BUF(3, "server hello, random bytes", server_randbytes,
1947 MBEDTLS_SERVER_HELLO_RANDOM_LEN);
Jerry Yuf4b27e42022-03-30 17:32:21 +08001948
1949#if defined(MBEDTLS_HAVE_TIME)
YxCda609132023-05-22 12:08:12 -07001950 ssl->session_negotiate->start = mbedtls_time(NULL);
Jerry Yuf4b27e42022-03-30 17:32:21 +08001951#endif /* MBEDTLS_HAVE_TIME */
1952
Gilles Peskine449bd832023-01-11 14:50:10 +01001953 return ret;
Jerry Yuf4b27e42022-03-30 17:32:21 +08001954}
1955
Jerry Yu3bf2c642022-03-30 22:02:12 +08001956/*
Jerry Yue74e04a2022-04-21 09:23:16 +08001957 * ssl_tls13_write_server_hello_supported_versions_ext ():
Jerry Yu3bf2c642022-03-30 22:02:12 +08001958 *
1959 * struct {
Jerry Yufb9f54d2022-04-06 10:08:34 +08001960 * ProtocolVersion selected_version;
Jerry Yu3bf2c642022-03-30 22:02:12 +08001961 * } SupportedVersions;
1962 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001963MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yue74e04a2022-04-21 09:23:16 +08001964static int ssl_tls13_write_server_hello_supported_versions_ext(
Gilles Peskine449bd832023-01-11 14:50:10 +01001965 mbedtls_ssl_context *ssl,
1966 unsigned char *buf,
1967 unsigned char *end,
1968 size_t *out_len)
Jerry Yu3bf2c642022-03-30 22:02:12 +08001969{
Jerry Yu3bf2c642022-03-30 22:02:12 +08001970 *out_len = 0;
1971
Gilles Peskine449bd832023-01-11 14:50:10 +01001972 MBEDTLS_SSL_DEBUG_MSG(3, ("server hello, write selected version"));
Jerry Yu3bf2c642022-03-30 22:02:12 +08001973
1974 /* Check if we have space to write the extension:
1975 * - extension_type (2 bytes)
1976 * - extension_data_length (2 bytes)
Jerry Yu349a6132022-04-14 20:52:56 +08001977 * - selected_version (2 bytes)
Jerry Yu3bf2c642022-03-30 22:02:12 +08001978 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001979 MBEDTLS_SSL_CHK_BUF_PTR(buf, end, 6);
Jerry Yu3bf2c642022-03-30 22:02:12 +08001980
Gilles Peskine449bd832023-01-11 14:50:10 +01001981 MBEDTLS_PUT_UINT16_BE(MBEDTLS_TLS_EXT_SUPPORTED_VERSIONS, buf, 0);
Jerry Yu3bf2c642022-03-30 22:02:12 +08001982
Gilles Peskine449bd832023-01-11 14:50:10 +01001983 MBEDTLS_PUT_UINT16_BE(2, buf, 2);
Jerry Yu3bf2c642022-03-30 22:02:12 +08001984
Gilles Peskine449bd832023-01-11 14:50:10 +01001985 mbedtls_ssl_write_version(buf + 4,
1986 ssl->conf->transport,
1987 ssl->tls_version);
Jerry Yu3bf2c642022-03-30 22:02:12 +08001988
Gilles Peskine449bd832023-01-11 14:50:10 +01001989 MBEDTLS_SSL_DEBUG_MSG(3, ("supported version: [%04x]",
1990 ssl->tls_version));
Jerry Yu3bf2c642022-03-30 22:02:12 +08001991
Jerry Yu349a6132022-04-14 20:52:56 +08001992 *out_len = 6;
Jerry Yu3bf2c642022-03-30 22:02:12 +08001993
Jerry Yub95dd362022-11-08 21:19:34 +08001994 mbedtls_ssl_tls13_set_hs_sent_ext_mask(
Gilles Peskine449bd832023-01-11 14:50:10 +01001995 ssl, MBEDTLS_TLS_EXT_SUPPORTED_VERSIONS);
Jerry Yub95dd362022-11-08 21:19:34 +08001996
Gilles Peskine449bd832023-01-11 14:50:10 +01001997 return 0;
Jerry Yu3bf2c642022-03-30 22:02:12 +08001998}
1999
Jerry Yud9436a12022-04-20 22:28:09 +08002000
Jerry Yu3bf2c642022-03-30 22:02:12 +08002001
2002/* Generate and export a single key share. For hybrid KEMs, this can
2003 * be called multiple times with the different components of the hybrid. */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002004MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002005static int ssl_tls13_generate_and_write_key_share(mbedtls_ssl_context *ssl,
2006 uint16_t named_group,
2007 unsigned char *buf,
2008 unsigned char *end,
2009 size_t *out_len)
Jerry Yu3bf2c642022-03-30 22:02:12 +08002010{
2011 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yu955ddd72022-04-22 22:27:33 +08002012
2013 *out_len = 0;
2014
Valerio Settic9ae8622023-07-25 11:23:50 +02002015#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_EPHEMERAL_ENABLED)
Przemek Stekiel29c219c2023-05-31 15:21:04 +02002016 if (mbedtls_ssl_tls13_named_group_is_ecdhe(named_group) ||
Przemek Stekield5f79e72023-06-29 09:08:43 +02002017 mbedtls_ssl_tls13_named_group_is_ffdh(named_group)) {
Przemek Stekiel408569f2023-07-06 11:26:44 +02002018 ret = mbedtls_ssl_tls13_generate_and_write_xxdh_key_exchange(
Gilles Peskine449bd832023-01-11 14:50:10 +01002019 ssl, named_group, buf, end, out_len);
2020 if (ret != 0) {
Jerry Yu89e103c2022-03-30 22:43:29 +08002021 MBEDTLS_SSL_DEBUG_RET(
Przemek Stekiel408569f2023-07-06 11:26:44 +02002022 1, "mbedtls_ssl_tls13_generate_and_write_xxdh_key_exchange",
Gilles Peskine449bd832023-01-11 14:50:10 +01002023 ret);
2024 return ret;
Jerry Yu3bf2c642022-03-30 22:02:12 +08002025 }
Gilles Peskine449bd832023-01-11 14:50:10 +01002026 } else
Valerio Settic9ae8622023-07-25 11:23:50 +02002027#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_EPHEMERAL_ENABLED */
Gilles Peskine449bd832023-01-11 14:50:10 +01002028 if (0 /* Other kinds of KEMs */) {
2029 } else {
Jerry Yu955ddd72022-04-22 22:27:33 +08002030 ((void) ssl);
2031 ((void) named_group);
2032 ((void) buf);
2033 ((void) end);
Jerry Yu3bf2c642022-03-30 22:02:12 +08002034 ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
2035 }
2036
Gilles Peskine449bd832023-01-11 14:50:10 +01002037 return ret;
Jerry Yu3bf2c642022-03-30 22:02:12 +08002038}
2039
2040/*
2041 * ssl_tls13_write_key_share_ext
2042 *
2043 * Structure of key_share extension in ServerHello:
2044 *
Jerry Yu1c3e6882022-04-20 21:23:40 +08002045 * struct {
2046 * NamedGroup group;
2047 * opaque key_exchange<1..2^16-1>;
2048 * } KeyShareEntry;
2049 * struct {
2050 * KeyShareEntry server_share;
2051 * } KeyShareServerHello;
Jerry Yu3bf2c642022-03-30 22:02:12 +08002052 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002053MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002054static int ssl_tls13_write_key_share_ext(mbedtls_ssl_context *ssl,
2055 unsigned char *buf,
2056 unsigned char *end,
2057 size_t *out_len)
Jerry Yu3bf2c642022-03-30 22:02:12 +08002058{
Jerry Yu955ddd72022-04-22 22:27:33 +08002059 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yu3bf2c642022-03-30 22:02:12 +08002060 unsigned char *p = buf;
Jerry Yu57d48412022-04-20 21:50:42 +08002061 uint16_t group = ssl->handshake->offered_group_id;
Jerry Yu3bf2c642022-03-30 22:02:12 +08002062 unsigned char *server_share = buf + 4;
Jerry Yu3bf2c642022-03-30 22:02:12 +08002063 size_t key_exchange_length;
Jerry Yu3bf2c642022-03-30 22:02:12 +08002064
2065 *out_len = 0;
2066
Gilles Peskine449bd832023-01-11 14:50:10 +01002067 MBEDTLS_SSL_DEBUG_MSG(3, ("server hello, adding key share extension"));
Jerry Yu3bf2c642022-03-30 22:02:12 +08002068
Gilles Peskine449bd832023-01-11 14:50:10 +01002069 MBEDTLS_SSL_DEBUG_MSG(2, ("server hello, write selected_group: %s (%04x)",
2070 mbedtls_ssl_named_group_to_str(group),
2071 group));
Jerry Yu58af2332022-09-06 11:19:31 +08002072
Jerry Yu3bf2c642022-03-30 22:02:12 +08002073 /* Check if we have space for header and length fields:
2074 * - extension_type (2 bytes)
2075 * - extension_data_length (2 bytes)
2076 * - group (2 bytes)
2077 * - key_exchange_length (2 bytes)
2078 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002079 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 8);
2080 MBEDTLS_PUT_UINT16_BE(MBEDTLS_TLS_EXT_KEY_SHARE, p, 0);
2081 MBEDTLS_PUT_UINT16_BE(group, server_share, 0);
Jerry Yu3bf2c642022-03-30 22:02:12 +08002082 p += 8;
Jerry Yu57d48412022-04-20 21:50:42 +08002083
Jerry Yu3bf2c642022-03-30 22:02:12 +08002084 /* When we introduce PQC-ECDHE hybrids, we'll want to call this
2085 * function multiple times. */
Jerry Yu955ddd72022-04-22 22:27:33 +08002086 ret = ssl_tls13_generate_and_write_key_share(
Gilles Peskine449bd832023-01-11 14:50:10 +01002087 ssl, group, server_share + 4, end, &key_exchange_length);
2088 if (ret != 0) {
2089 return ret;
2090 }
Jerry Yu3bf2c642022-03-30 22:02:12 +08002091 p += key_exchange_length;
Jerry Yuc1be19f2022-04-23 16:11:39 +08002092
Gilles Peskine449bd832023-01-11 14:50:10 +01002093 MBEDTLS_PUT_UINT16_BE(key_exchange_length, server_share + 2, 0);
Jerry Yu3bf2c642022-03-30 22:02:12 +08002094
Gilles Peskine449bd832023-01-11 14:50:10 +01002095 MBEDTLS_PUT_UINT16_BE(p - server_share, buf, 2);
Jerry Yu3bf2c642022-03-30 22:02:12 +08002096
Jerry Yu57d48412022-04-20 21:50:42 +08002097 *out_len = p - buf;
Jerry Yu955ddd72022-04-22 22:27:33 +08002098
Gilles Peskine449bd832023-01-11 14:50:10 +01002099 mbedtls_ssl_tls13_set_hs_sent_ext_mask(ssl, MBEDTLS_TLS_EXT_KEY_SHARE);
Jerry Yub95dd362022-11-08 21:19:34 +08002100
Gilles Peskine449bd832023-01-11 14:50:10 +01002101 return 0;
Jerry Yu3bf2c642022-03-30 22:02:12 +08002102}
Jerry Yud9436a12022-04-20 22:28:09 +08002103
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002104MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002105static int ssl_tls13_write_hrr_key_share_ext(mbedtls_ssl_context *ssl,
2106 unsigned char *buf,
2107 unsigned char *end,
2108 size_t *out_len)
Jerry Yu23f7a6f2022-04-23 15:16:45 +08002109{
2110 uint16_t selected_group = ssl->handshake->hrr_selected_group;
2111 /* key_share Extension
2112 *
2113 * struct {
2114 * select (Handshake.msg_type) {
2115 * ...
2116 * case hello_retry_request:
2117 * NamedGroup selected_group;
2118 * ...
2119 * };
2120 * } KeyShare;
2121 */
2122
2123 *out_len = 0;
2124
Jerry Yub0ac10b2022-05-05 11:10:08 +08002125 /*
2126 * For a pure PSK key exchange, there is no group to agree upon. The purpose
2127 * of the HRR is then to transmit a cookie to force the client to demonstrate
2128 * reachability at their apparent network address (primarily useful for DTLS).
2129 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002130 if (!mbedtls_ssl_tls13_key_exchange_mode_with_ephemeral(ssl)) {
2131 return 0;
2132 }
Jerry Yu23f7a6f2022-04-23 15:16:45 +08002133
2134 /* We should only send the key_share extension if the client's initial
2135 * key share was not acceptable. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002136 if (ssl->handshake->offered_group_id != 0) {
2137 MBEDTLS_SSL_DEBUG_MSG(4, ("Skip key_share extension in HRR"));
2138 return 0;
Jerry Yu23f7a6f2022-04-23 15:16:45 +08002139 }
2140
Gilles Peskine449bd832023-01-11 14:50:10 +01002141 if (selected_group == 0) {
2142 MBEDTLS_SSL_DEBUG_MSG(1, ("no matching named group found"));
2143 return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
Jerry Yu23f7a6f2022-04-23 15:16:45 +08002144 }
2145
Jerry Yub0ac10b2022-05-05 11:10:08 +08002146 /* Check if we have enough space:
2147 * - extension_type (2 bytes)
2148 * - extension_data_length (2 bytes)
2149 * - selected_group (2 bytes)
2150 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002151 MBEDTLS_SSL_CHK_BUF_PTR(buf, end, 6);
Jerry Yu23f7a6f2022-04-23 15:16:45 +08002152
Gilles Peskine449bd832023-01-11 14:50:10 +01002153 MBEDTLS_PUT_UINT16_BE(MBEDTLS_TLS_EXT_KEY_SHARE, buf, 0);
2154 MBEDTLS_PUT_UINT16_BE(2, buf, 2);
2155 MBEDTLS_PUT_UINT16_BE(selected_group, buf, 4);
Jerry Yu23f7a6f2022-04-23 15:16:45 +08002156
Gilles Peskine449bd832023-01-11 14:50:10 +01002157 MBEDTLS_SSL_DEBUG_MSG(3,
2158 ("HRR selected_group: %s (%x)",
2159 mbedtls_ssl_named_group_to_str(selected_group),
2160 selected_group));
Jerry Yu23f7a6f2022-04-23 15:16:45 +08002161
2162 *out_len = 6;
Jerry Yu23f7a6f2022-04-23 15:16:45 +08002163
Gilles Peskine449bd832023-01-11 14:50:10 +01002164 mbedtls_ssl_tls13_set_hs_sent_ext_mask(ssl, MBEDTLS_TLS_EXT_KEY_SHARE);
Jerry Yub95dd362022-11-08 21:19:34 +08002165
Gilles Peskine449bd832023-01-11 14:50:10 +01002166 return 0;
Jerry Yu23f7a6f2022-04-23 15:16:45 +08002167}
Jerry Yu3bf2c642022-03-30 22:02:12 +08002168
2169/*
2170 * Structure of ServerHello message:
2171 *
2172 * struct {
2173 * ProtocolVersion legacy_version = 0x0303; // TLS v1.2
2174 * Random random;
2175 * opaque legacy_session_id_echo<0..32>;
2176 * CipherSuite cipher_suite;
2177 * uint8 legacy_compression_method = 0;
2178 * Extension extensions<6..2^16-1>;
2179 * } ServerHello;
2180 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002181MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002182static int ssl_tls13_write_server_hello_body(mbedtls_ssl_context *ssl,
2183 unsigned char *buf,
2184 unsigned char *end,
2185 size_t *out_len,
2186 int is_hrr)
Jerry Yu56404d72022-03-30 17:36:13 +08002187{
Jerry Yu955ddd72022-04-22 22:27:33 +08002188 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yu3bf2c642022-03-30 22:02:12 +08002189 unsigned char *p = buf;
Jerry Yud9436a12022-04-20 22:28:09 +08002190 unsigned char *p_extensions_len;
Jerry Yufbe3e642022-04-25 19:31:51 +08002191 size_t output_len;
Jerry Yu3bf2c642022-03-30 22:02:12 +08002192
2193 *out_len = 0;
Jerry Yu50e00e32022-10-31 14:45:01 +08002194 ssl->handshake->sent_extensions = MBEDTLS_SSL_EXT_MASK_NONE;
Jerry Yu3bf2c642022-03-30 22:02:12 +08002195
Jerry Yucfc04b32022-04-21 09:31:58 +08002196 /* ...
2197 * ProtocolVersion legacy_version = 0x0303; // TLS 1.2
2198 * ...
2199 * with ProtocolVersion defined as:
2200 * uint16 ProtocolVersion;
Jerry Yu3bf2c642022-03-30 22:02:12 +08002201 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002202 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 2);
2203 MBEDTLS_PUT_UINT16_BE(0x0303, p, 0);
Jerry Yu3bf2c642022-03-30 22:02:12 +08002204 p += 2;
2205
Jerry Yu1c3e6882022-04-20 21:23:40 +08002206 /* ...
2207 * Random random;
2208 * ...
Jerry Yucfc04b32022-04-21 09:31:58 +08002209 * with Random defined as:
2210 * opaque Random[MBEDTLS_SERVER_HELLO_RANDOM_LEN];
Jerry Yu1c3e6882022-04-20 21:23:40 +08002211 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002212 MBEDTLS_SSL_CHK_BUF_PTR(p, end, MBEDTLS_SERVER_HELLO_RANDOM_LEN);
2213 if (is_hrr) {
2214 memcpy(p, mbedtls_ssl_tls13_hello_retry_request_magic,
2215 MBEDTLS_SERVER_HELLO_RANDOM_LEN);
2216 } else {
2217 memcpy(p, &ssl->handshake->randbytes[MBEDTLS_CLIENT_HELLO_RANDOM_LEN],
2218 MBEDTLS_SERVER_HELLO_RANDOM_LEN);
Jerry Yu23f7a6f2022-04-23 15:16:45 +08002219 }
Gilles Peskine449bd832023-01-11 14:50:10 +01002220 MBEDTLS_SSL_DEBUG_BUF(3, "server hello, random bytes",
2221 p, MBEDTLS_SERVER_HELLO_RANDOM_LEN);
Jerry Yu3bf2c642022-03-30 22:02:12 +08002222 p += MBEDTLS_SERVER_HELLO_RANDOM_LEN;
2223
Jerry Yucfc04b32022-04-21 09:31:58 +08002224 /* ...
2225 * opaque legacy_session_id_echo<0..32>;
2226 * ...
Jerry Yu3bf2c642022-03-30 22:02:12 +08002227 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002228 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 1 + ssl->session_negotiate->id_len);
2229 *p++ = (unsigned char) ssl->session_negotiate->id_len;
2230 if (ssl->session_negotiate->id_len > 0) {
2231 memcpy(p, &ssl->session_negotiate->id[0],
2232 ssl->session_negotiate->id_len);
Jerry Yu3bf2c642022-03-30 22:02:12 +08002233 p += ssl->session_negotiate->id_len;
Jerry Yu955ddd72022-04-22 22:27:33 +08002234
Gilles Peskine449bd832023-01-11 14:50:10 +01002235 MBEDTLS_SSL_DEBUG_BUF(3, "session id", ssl->session_negotiate->id,
2236 ssl->session_negotiate->id_len);
Jerry Yu3bf2c642022-03-30 22:02:12 +08002237 }
2238
Jerry Yucfc04b32022-04-21 09:31:58 +08002239 /* ...
2240 * CipherSuite cipher_suite;
2241 * ...
2242 * with CipherSuite defined as:
2243 * uint8 CipherSuite[2];
Jerry Yu3bf2c642022-03-30 22:02:12 +08002244 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002245 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 2);
2246 MBEDTLS_PUT_UINT16_BE(ssl->session_negotiate->ciphersuite, p, 0);
Jerry Yu3bf2c642022-03-30 22:02:12 +08002247 p += 2;
Gilles Peskine449bd832023-01-11 14:50:10 +01002248 MBEDTLS_SSL_DEBUG_MSG(3,
2249 ("server hello, chosen ciphersuite: %s ( id=%d )",
2250 mbedtls_ssl_get_ciphersuite_name(
2251 ssl->session_negotiate->ciphersuite),
2252 ssl->session_negotiate->ciphersuite));
Jerry Yu3bf2c642022-03-30 22:02:12 +08002253
Jerry Yucfc04b32022-04-21 09:31:58 +08002254 /* ...
2255 * uint8 legacy_compression_method = 0;
2256 * ...
2257 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002258 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 1);
Thomas Daubney31e03a82022-07-25 15:59:25 +01002259 *p++ = MBEDTLS_SSL_COMPRESS_NULL;
Jerry Yu3bf2c642022-03-30 22:02:12 +08002260
Jerry Yucfc04b32022-04-21 09:31:58 +08002261 /* ...
2262 * Extension extensions<6..2^16-1>;
2263 * ...
2264 * struct {
2265 * ExtensionType extension_type; (2 bytes)
2266 * opaque extension_data<0..2^16-1>;
2267 * } Extension;
2268 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002269 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 2);
Jerry Yud9436a12022-04-20 22:28:09 +08002270 p_extensions_len = p;
Jerry Yu3bf2c642022-03-30 22:02:12 +08002271 p += 2;
2272
Gilles Peskine449bd832023-01-11 14:50:10 +01002273 if ((ret = ssl_tls13_write_server_hello_supported_versions_ext(
2274 ssl, p, end, &output_len)) != 0) {
Jerry Yu955ddd72022-04-22 22:27:33 +08002275 MBEDTLS_SSL_DEBUG_RET(
Gilles Peskine449bd832023-01-11 14:50:10 +01002276 1, "ssl_tls13_write_server_hello_supported_versions_ext", ret);
2277 return ret;
Jerry Yu3bf2c642022-03-30 22:02:12 +08002278 }
2279 p += output_len;
2280
Gilles Peskine449bd832023-01-11 14:50:10 +01002281 if (mbedtls_ssl_tls13_key_exchange_mode_with_ephemeral(ssl)) {
2282 if (is_hrr) {
2283 ret = ssl_tls13_write_hrr_key_share_ext(ssl, p, end, &output_len);
2284 } else {
2285 ret = ssl_tls13_write_key_share_ext(ssl, p, end, &output_len);
2286 }
2287 if (ret != 0) {
2288 return ret;
2289 }
Jerry Yu3bf2c642022-03-30 22:02:12 +08002290 p += output_len;
2291 }
Jerry Yu3bf2c642022-03-30 22:02:12 +08002292
Ronald Cron41a443a2022-10-04 16:38:25 +02002293#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01002294 if (!is_hrr && mbedtls_ssl_tls13_key_exchange_mode_with_psk(ssl)) {
2295 ret = ssl_tls13_write_server_pre_shared_key_ext(ssl, p, end, &output_len);
2296 if (ret != 0) {
2297 MBEDTLS_SSL_DEBUG_RET(1, "ssl_tls13_write_server_pre_shared_key_ext",
2298 ret);
2299 return ret;
Jerry Yu032b15ce2022-07-11 06:10:03 +00002300 }
2301 p += output_len;
2302 }
Ronald Cron41a443a2022-10-04 16:38:25 +02002303#endif
Jerry Yu032b15ce2022-07-11 06:10:03 +00002304
Gilles Peskine449bd832023-01-11 14:50:10 +01002305 MBEDTLS_PUT_UINT16_BE(p - p_extensions_len - 2, p_extensions_len, 0);
Jerry Yu3bf2c642022-03-30 22:02:12 +08002306
Gilles Peskine449bd832023-01-11 14:50:10 +01002307 MBEDTLS_SSL_DEBUG_BUF(4, "server hello extensions",
2308 p_extensions_len, p - p_extensions_len);
Jerry Yu3bf2c642022-03-30 22:02:12 +08002309
Jerry Yud9436a12022-04-20 22:28:09 +08002310 *out_len = p - buf;
Jerry Yu3bf2c642022-03-30 22:02:12 +08002311
Gilles Peskine449bd832023-01-11 14:50:10 +01002312 MBEDTLS_SSL_DEBUG_BUF(3, "server hello", buf, *out_len);
Jerry Yu3bf2c642022-03-30 22:02:12 +08002313
Jerry Yu7de2ff02022-11-08 21:43:46 +08002314 MBEDTLS_SSL_PRINT_EXTS(
Jerry Yu4b8f2f72022-10-31 13:31:22 +08002315 3, is_hrr ? MBEDTLS_SSL_TLS1_3_HS_HELLO_RETRY_REQUEST :
Gilles Peskine449bd832023-01-11 14:50:10 +01002316 MBEDTLS_SSL_HS_SERVER_HELLO,
2317 ssl->handshake->sent_extensions);
Jerry Yu4b8f2f72022-10-31 13:31:22 +08002318
Gilles Peskine449bd832023-01-11 14:50:10 +01002319 return ret;
Jerry Yu56404d72022-03-30 17:36:13 +08002320}
2321
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002322MBEDTLS_CHECK_RETURN_CRITICAL
Xiaokang Qian934ce6f2023-02-06 10:23:04 +00002323static int ssl_tls13_finalize_server_hello(mbedtls_ssl_context *ssl)
Jerry Yue110d252022-05-05 10:19:22 +08002324{
2325 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine449bd832023-01-11 14:50:10 +01002326 ret = mbedtls_ssl_tls13_compute_handshake_transform(ssl);
2327 if (ret != 0) {
2328 MBEDTLS_SSL_DEBUG_RET(1,
2329 "mbedtls_ssl_tls13_compute_handshake_transform",
2330 ret);
2331 return ret;
Jerry Yue110d252022-05-05 10:19:22 +08002332 }
2333
Gilles Peskine449bd832023-01-11 14:50:10 +01002334 return ret;
Jerry Yue110d252022-05-05 10:19:22 +08002335}
2336
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002337MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002338static int ssl_tls13_write_server_hello(mbedtls_ssl_context *ssl)
Jerry Yu5b64ae92022-03-30 17:15:02 +08002339{
Jerry Yu637a3f12022-04-20 21:37:58 +08002340 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yuf4b27e42022-03-30 17:32:21 +08002341 unsigned char *buf;
2342 size_t buf_len, msg_len;
2343
Gilles Peskine449bd832023-01-11 14:50:10 +01002344 MBEDTLS_SSL_DEBUG_MSG(2, ("=> write server hello"));
Jerry Yuf4b27e42022-03-30 17:32:21 +08002345
Gilles Peskine449bd832023-01-11 14:50:10 +01002346 MBEDTLS_SSL_PROC_CHK(ssl_tls13_prepare_server_hello(ssl));
Jerry Yuf4b27e42022-03-30 17:32:21 +08002347
Xiaokang Qian9f1747b2023-03-30 02:50:04 +00002348 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_start_handshake_msg(
2349 ssl, MBEDTLS_SSL_HS_SERVER_HELLO, &buf, &buf_len));
Jerry Yuf4b27e42022-03-30 17:32:21 +08002350
Gilles Peskine449bd832023-01-11 14:50:10 +01002351 MBEDTLS_SSL_PROC_CHK(ssl_tls13_write_server_hello_body(ssl, buf,
2352 buf + buf_len,
2353 &msg_len,
2354 0));
Jerry Yuf4b27e42022-03-30 17:32:21 +08002355
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +01002356 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_add_hs_msg_to_checksum(
Manuel Pégourié-Gonnard43cc1272023-02-06 11:48:19 +01002357 ssl, MBEDTLS_SSL_HS_SERVER_HELLO, buf, msg_len));
Jerry Yuf4b27e42022-03-30 17:32:21 +08002358
Gilles Peskine449bd832023-01-11 14:50:10 +01002359 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_finish_handshake_msg(
2360 ssl, buf_len, msg_len));
Jerry Yu637a3f12022-04-20 21:37:58 +08002361
Xiaokang Qian934ce6f2023-02-06 10:23:04 +00002362 MBEDTLS_SSL_PROC_CHK(ssl_tls13_finalize_server_hello(ssl));
Jerry Yue110d252022-05-05 10:19:22 +08002363
Gabor Mezei7b39bf12022-05-24 16:04:14 +02002364#if defined(MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE)
2365 /* The server sends a dummy change_cipher_spec record immediately
2366 * after its first handshake message. This may either be after
2367 * a ServerHello or a HelloRetryRequest.
2368 */
Gabor Mezei96ae9262022-06-28 11:45:18 +02002369 mbedtls_ssl_handshake_set_state(
Gilles Peskine449bd832023-01-11 14:50:10 +01002370 ssl, MBEDTLS_SSL_SERVER_CCS_AFTER_SERVER_HELLO);
Gabor Mezei7b39bf12022-05-24 16:04:14 +02002371#else
Gilles Peskine449bd832023-01-11 14:50:10 +01002372 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_ENCRYPTED_EXTENSIONS);
Gabor Mezei7b39bf12022-05-24 16:04:14 +02002373#endif /* MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE */
Jerry Yue110d252022-05-05 10:19:22 +08002374
Jerry Yuf4b27e42022-03-30 17:32:21 +08002375cleanup:
2376
Gilles Peskine449bd832023-01-11 14:50:10 +01002377 MBEDTLS_SSL_DEBUG_MSG(2, ("<= write server hello"));
2378 return ret;
Jerry Yu5b64ae92022-03-30 17:15:02 +08002379}
2380
Jerry Yu23d1a252022-05-12 18:08:59 +08002381
2382/*
2383 * Handler for MBEDTLS_SSL_HELLO_RETRY_REQUEST
2384 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002385MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002386static int ssl_tls13_prepare_hello_retry_request(mbedtls_ssl_context *ssl)
Jerry Yu23d1a252022-05-12 18:08:59 +08002387{
2388 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Ronald Cron5fbd2702024-02-14 10:03:36 +01002389 if (ssl->handshake->hello_retry_request_flag) {
Gilles Peskine449bd832023-01-11 14:50:10 +01002390 MBEDTLS_SSL_DEBUG_MSG(1, ("Too many HRRs"));
2391 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE,
2392 MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE);
2393 return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
Jerry Yu23d1a252022-05-12 18:08:59 +08002394 }
2395
2396 /*
2397 * Create stateless transcript hash for HRR
2398 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002399 MBEDTLS_SSL_DEBUG_MSG(4, ("Reset transcript for HRR"));
2400 ret = mbedtls_ssl_reset_transcript_for_hrr(ssl);
2401 if (ret != 0) {
2402 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_reset_transcript_for_hrr", ret);
2403 return ret;
Jerry Yu23d1a252022-05-12 18:08:59 +08002404 }
Gilles Peskine449bd832023-01-11 14:50:10 +01002405 mbedtls_ssl_session_reset_msg_layer(ssl, 0);
Jerry Yu23d1a252022-05-12 18:08:59 +08002406
Gilles Peskine449bd832023-01-11 14:50:10 +01002407 return 0;
Jerry Yu23d1a252022-05-12 18:08:59 +08002408}
2409
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002410MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002411static int ssl_tls13_write_hello_retry_request(mbedtls_ssl_context *ssl)
Jerry Yu23d1a252022-05-12 18:08:59 +08002412{
2413 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
2414 unsigned char *buf;
2415 size_t buf_len, msg_len;
2416
Gilles Peskine449bd832023-01-11 14:50:10 +01002417 MBEDTLS_SSL_DEBUG_MSG(2, ("=> write hello retry request"));
Jerry Yu23d1a252022-05-12 18:08:59 +08002418
Gilles Peskine449bd832023-01-11 14:50:10 +01002419 MBEDTLS_SSL_PROC_CHK(ssl_tls13_prepare_hello_retry_request(ssl));
Jerry Yu23d1a252022-05-12 18:08:59 +08002420
Gilles Peskine449bd832023-01-11 14:50:10 +01002421 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_start_handshake_msg(
2422 ssl, MBEDTLS_SSL_HS_SERVER_HELLO,
2423 &buf, &buf_len));
Jerry Yu23d1a252022-05-12 18:08:59 +08002424
Gilles Peskine449bd832023-01-11 14:50:10 +01002425 MBEDTLS_SSL_PROC_CHK(ssl_tls13_write_server_hello_body(ssl, buf,
2426 buf + buf_len,
2427 &msg_len,
2428 1));
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +01002429 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_add_hs_msg_to_checksum(
Manuel Pégourié-Gonnard43cc1272023-02-06 11:48:19 +01002430 ssl, MBEDTLS_SSL_HS_SERVER_HELLO, buf, msg_len));
Jerry Yu23d1a252022-05-12 18:08:59 +08002431
2432
Gilles Peskine449bd832023-01-11 14:50:10 +01002433 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_finish_handshake_msg(ssl, buf_len,
2434 msg_len));
Jerry Yu23d1a252022-05-12 18:08:59 +08002435
Ronald Cron5fbd2702024-02-14 10:03:36 +01002436 ssl->handshake->hello_retry_request_flag = 1;
Jerry Yu23d1a252022-05-12 18:08:59 +08002437
Gabor Mezei7b39bf12022-05-24 16:04:14 +02002438#if defined(MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE)
2439 /* The server sends a dummy change_cipher_spec record immediately
2440 * after its first handshake message. This may either be after
2441 * a ServerHello or a HelloRetryRequest.
2442 */
Gabor Mezei96ae9262022-06-28 11:45:18 +02002443 mbedtls_ssl_handshake_set_state(
Gilles Peskine449bd832023-01-11 14:50:10 +01002444 ssl, MBEDTLS_SSL_SERVER_CCS_AFTER_HELLO_RETRY_REQUEST);
Gabor Mezei7b39bf12022-05-24 16:04:14 +02002445#else
Gilles Peskine449bd832023-01-11 14:50:10 +01002446 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_CLIENT_HELLO);
Gabor Mezei7b39bf12022-05-24 16:04:14 +02002447#endif /* MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE */
Jerry Yu23d1a252022-05-12 18:08:59 +08002448
2449cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +01002450 MBEDTLS_SSL_DEBUG_MSG(2, ("<= write hello retry request"));
2451 return ret;
Jerry Yu23d1a252022-05-12 18:08:59 +08002452}
2453
Jerry Yu5b64ae92022-03-30 17:15:02 +08002454/*
Jerry Yu4d3841a2022-04-16 12:37:19 +08002455 * Handler for MBEDTLS_SSL_ENCRYPTED_EXTENSIONS
2456 */
Jerry Yu4d3841a2022-04-16 12:37:19 +08002457
2458/*
2459 * struct {
2460 * Extension extensions<0..2 ^ 16 - 1>;
2461 * } EncryptedExtensions;
2462 *
2463 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002464MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002465static int ssl_tls13_write_encrypted_extensions_body(mbedtls_ssl_context *ssl,
2466 unsigned char *buf,
2467 unsigned char *end,
2468 size_t *out_len)
Jerry Yu4d3841a2022-04-16 12:37:19 +08002469{
XiaokangQianacb39922022-06-17 10:18:48 +00002470 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yu4d3841a2022-04-16 12:37:19 +08002471 unsigned char *p = buf;
2472 size_t extensions_len = 0;
Jerry Yu8937eb42022-05-03 12:12:14 +08002473 unsigned char *p_extensions_len;
XiaokangQianacb39922022-06-17 10:18:48 +00002474 size_t output_len;
Jerry Yu9da5e5a2022-05-03 15:46:09 +08002475
Jerry Yu4d3841a2022-04-16 12:37:19 +08002476 *out_len = 0;
2477
Gilles Peskine449bd832023-01-11 14:50:10 +01002478 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 2);
Jerry Yu8937eb42022-05-03 12:12:14 +08002479 p_extensions_len = p;
Jerry Yu4d3841a2022-04-16 12:37:19 +08002480 p += 2;
2481
Jerry Yu8937eb42022-05-03 12:12:14 +08002482 ((void) ssl);
XiaokangQianacb39922022-06-17 10:18:48 +00002483 ((void) ret);
2484 ((void) output_len);
2485
2486#if defined(MBEDTLS_SSL_ALPN)
Gilles Peskine449bd832023-01-11 14:50:10 +01002487 ret = mbedtls_ssl_write_alpn_ext(ssl, p, end, &output_len);
2488 if (ret != 0) {
2489 return ret;
2490 }
XiaokangQian95d5f542022-06-24 02:29:26 +00002491 p += output_len;
XiaokangQianacb39922022-06-17 10:18:48 +00002492#endif /* MBEDTLS_SSL_ALPN */
Jerry Yu4d3841a2022-04-16 12:37:19 +08002493
Jerry Yu71c14f12022-12-12 11:10:35 +08002494#if defined(MBEDTLS_SSL_EARLY_DATA)
Ronald Cron78a38f62024-02-01 18:30:31 +01002495 if (ssl->handshake->early_data_accepted) {
Jerry Yu52335392023-11-23 18:06:06 +08002496 ret = mbedtls_ssl_tls13_write_early_data_ext(
Jerry Yuc59c5862023-12-05 10:40:49 +08002497 ssl, 0, p, end, &output_len);
Jerry Yu71c14f12022-12-12 11:10:35 +08002498 if (ret != 0) {
2499 return ret;
2500 }
2501 p += output_len;
2502 }
2503#endif /* MBEDTLS_SSL_EARLY_DATA */
2504
Waleed Elmelegy47d29462024-01-03 17:31:52 +00002505#if defined(MBEDTLS_SSL_RECORD_SIZE_LIMIT)
Waleed Elmelegyfbe42742024-01-05 18:11:10 +00002506 if (ssl->handshake->received_extensions & MBEDTLS_SSL_EXT_MASK(RECORD_SIZE_LIMIT)) {
Waleed Elmelegyd2fc90e2024-01-04 18:04:53 +00002507 ret = mbedtls_ssl_tls13_write_record_size_limit_ext(
2508 ssl, p, end, &output_len);
2509 if (ret != 0) {
2510 return ret;
2511 }
2512 p += output_len;
Waleed Elmelegy47d29462024-01-03 17:31:52 +00002513 }
Waleed Elmelegy47d29462024-01-03 17:31:52 +00002514#endif
2515
Gilles Peskine449bd832023-01-11 14:50:10 +01002516 extensions_len = (p - p_extensions_len) - 2;
2517 MBEDTLS_PUT_UINT16_BE(extensions_len, p_extensions_len, 0);
Jerry Yu8937eb42022-05-03 12:12:14 +08002518
2519 *out_len = p - buf;
Jerry Yu4d3841a2022-04-16 12:37:19 +08002520
Gilles Peskine449bd832023-01-11 14:50:10 +01002521 MBEDTLS_SSL_DEBUG_BUF(4, "encrypted extensions", buf, *out_len);
Jerry Yu4d3841a2022-04-16 12:37:19 +08002522
Jerry Yu7de2ff02022-11-08 21:43:46 +08002523 MBEDTLS_SSL_PRINT_EXTS(
Gilles Peskine449bd832023-01-11 14:50:10 +01002524 3, MBEDTLS_SSL_HS_ENCRYPTED_EXTENSIONS, ssl->handshake->sent_extensions);
Jerry Yu4b8f2f72022-10-31 13:31:22 +08002525
Gilles Peskine449bd832023-01-11 14:50:10 +01002526 return 0;
Jerry Yu4d3841a2022-04-16 12:37:19 +08002527}
2528
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002529MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002530static int ssl_tls13_write_encrypted_extensions(mbedtls_ssl_context *ssl)
Jerry Yu4d3841a2022-04-16 12:37:19 +08002531{
2532 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
2533 unsigned char *buf;
Jerry Yu39730a72022-05-03 12:14:04 +08002534 size_t buf_len, msg_len;
Jerry Yu4d3841a2022-04-16 12:37:19 +08002535
Gilles Peskine449bd832023-01-11 14:50:10 +01002536 mbedtls_ssl_set_outbound_transform(ssl,
2537 ssl->handshake->transform_handshake);
Gabor Mezei54719122022-06-28 11:34:56 +02002538 MBEDTLS_SSL_DEBUG_MSG(
Gilles Peskine449bd832023-01-11 14:50:10 +01002539 3, ("switching to handshake transform for outbound data"));
Gabor Mezei54719122022-06-28 11:34:56 +02002540
Gilles Peskine449bd832023-01-11 14:50:10 +01002541 MBEDTLS_SSL_DEBUG_MSG(2, ("=> write encrypted extensions"));
Jerry Yu4d3841a2022-04-16 12:37:19 +08002542
Xiaokang Qian9f1747b2023-03-30 02:50:04 +00002543 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_start_handshake_msg(
2544 ssl, MBEDTLS_SSL_HS_ENCRYPTED_EXTENSIONS,
2545 &buf, &buf_len));
Jerry Yu4d3841a2022-04-16 12:37:19 +08002546
Gilles Peskine449bd832023-01-11 14:50:10 +01002547 MBEDTLS_SSL_PROC_CHK(ssl_tls13_write_encrypted_extensions_body(
2548 ssl, buf, buf + buf_len, &msg_len));
Jerry Yu4d3841a2022-04-16 12:37:19 +08002549
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +01002550 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_add_hs_msg_to_checksum(
Xiaokang Qian9f1747b2023-03-30 02:50:04 +00002551 ssl, MBEDTLS_SSL_HS_ENCRYPTED_EXTENSIONS,
2552 buf, msg_len));
Jerry Yu4d3841a2022-04-16 12:37:19 +08002553
Gilles Peskine449bd832023-01-11 14:50:10 +01002554 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_finish_handshake_msg(
2555 ssl, buf_len, msg_len));
Jerry Yu4d3841a2022-04-16 12:37:19 +08002556
Ronald Cron928cbd32022-10-04 16:14:26 +02002557#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01002558 if (mbedtls_ssl_tls13_key_exchange_mode_with_psk(ssl)) {
2559 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_SERVER_FINISHED);
2560 } else {
2561 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_CERTIFICATE_REQUEST);
2562 }
Jerry Yu8937eb42022-05-03 12:12:14 +08002563#else
Gilles Peskine449bd832023-01-11 14:50:10 +01002564 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_SERVER_FINISHED);
Jerry Yu8937eb42022-05-03 12:12:14 +08002565#endif
2566
Jerry Yu4d3841a2022-04-16 12:37:19 +08002567cleanup:
2568
Gilles Peskine449bd832023-01-11 14:50:10 +01002569 MBEDTLS_SSL_DEBUG_MSG(2, ("<= write encrypted extensions"));
2570 return ret;
Jerry Yu4d3841a2022-04-16 12:37:19 +08002571}
2572
Ronald Cron928cbd32022-10-04 16:14:26 +02002573#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED)
XiaokangQiana987e1d2022-05-07 01:25:58 +00002574#define SSL_CERTIFICATE_REQUEST_SEND_REQUEST 0
2575#define SSL_CERTIFICATE_REQUEST_SKIP 1
2576/* Coordination:
2577 * Check whether a CertificateRequest message should be written.
2578 * Returns a negative code on failure, or
2579 * - SSL_CERTIFICATE_REQUEST_SEND_REQUEST
2580 * - SSL_CERTIFICATE_REQUEST_SKIP
2581 * indicating if the writing of the CertificateRequest
2582 * should be skipped or not.
2583 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002584MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002585static int ssl_tls13_certificate_request_coordinate(mbedtls_ssl_context *ssl)
XiaokangQiana987e1d2022-05-07 01:25:58 +00002586{
2587 int authmode;
2588
XiaokangQian40a35232022-05-07 09:02:40 +00002589#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Gilles Peskine449bd832023-01-11 14:50:10 +01002590 if (ssl->handshake->sni_authmode != MBEDTLS_SSL_VERIFY_UNSET) {
XiaokangQian40a35232022-05-07 09:02:40 +00002591 authmode = ssl->handshake->sni_authmode;
Gilles Peskine449bd832023-01-11 14:50:10 +01002592 } else
XiaokangQian40a35232022-05-07 09:02:40 +00002593#endif
XiaokangQiana987e1d2022-05-07 01:25:58 +00002594 authmode = ssl->conf->authmode;
2595
Gilles Peskine449bd832023-01-11 14:50:10 +01002596 if (authmode == MBEDTLS_SSL_VERIFY_NONE) {
Ronald Croneac00ad2022-09-13 10:16:31 +02002597 ssl->session_negotiate->verify_result = MBEDTLS_X509_BADCERT_SKIP_VERIFY;
Gilles Peskine449bd832023-01-11 14:50:10 +01002598 return SSL_CERTIFICATE_REQUEST_SKIP;
Ronald Croneac00ad2022-09-13 10:16:31 +02002599 }
XiaokangQiana987e1d2022-05-07 01:25:58 +00002600
XiaokangQianc3017f62022-05-13 05:55:41 +00002601 ssl->handshake->certificate_request_sent = 1;
XiaokangQian189ded22022-05-10 08:12:17 +00002602
Gilles Peskine449bd832023-01-11 14:50:10 +01002603 return SSL_CERTIFICATE_REQUEST_SEND_REQUEST;
XiaokangQiana987e1d2022-05-07 01:25:58 +00002604}
2605
XiaokangQiancec9ae62022-05-06 07:28:50 +00002606/*
2607 * struct {
2608 * opaque certificate_request_context<0..2^8-1>;
2609 * Extension extensions<2..2^16-1>;
2610 * } CertificateRequest;
2611 *
2612 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002613MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002614static int ssl_tls13_write_certificate_request_body(mbedtls_ssl_context *ssl,
2615 unsigned char *buf,
2616 const unsigned char *end,
2617 size_t *out_len)
XiaokangQiancec9ae62022-05-06 07:28:50 +00002618{
2619 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
2620 unsigned char *p = buf;
XiaokangQianec6efb92022-05-06 09:53:10 +00002621 size_t output_len = 0;
XiaokangQiancec9ae62022-05-06 07:28:50 +00002622 unsigned char *p_extensions_len;
2623
2624 *out_len = 0;
2625
2626 /* Check if we have enough space:
2627 * - certificate_request_context (1 byte)
2628 * - extensions length (2 bytes)
2629 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002630 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 3);
XiaokangQiancec9ae62022-05-06 07:28:50 +00002631
2632 /*
2633 * Write certificate_request_context
2634 */
2635 /*
2636 * We use a zero length context for the normal handshake
2637 * messages. For post-authentication handshake messages
2638 * this request context would be set to a non-zero value.
2639 */
2640 *p++ = 0x0;
2641
2642 /*
2643 * Write extensions
2644 */
2645 /* The extensions must contain the signature_algorithms. */
2646 p_extensions_len = p;
2647 p += 2;
Gilles Peskine449bd832023-01-11 14:50:10 +01002648 ret = mbedtls_ssl_write_sig_alg_ext(ssl, p, end, &output_len);
2649 if (ret != 0) {
2650 return ret;
2651 }
XiaokangQiancec9ae62022-05-06 07:28:50 +00002652
XiaokangQianec6efb92022-05-06 09:53:10 +00002653 p += output_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01002654 MBEDTLS_PUT_UINT16_BE(p - p_extensions_len - 2, p_extensions_len, 0);
XiaokangQiancec9ae62022-05-06 07:28:50 +00002655
2656 *out_len = p - buf;
2657
Jerry Yu7de2ff02022-11-08 21:43:46 +08002658 MBEDTLS_SSL_PRINT_EXTS(
Gilles Peskine449bd832023-01-11 14:50:10 +01002659 3, MBEDTLS_SSL_HS_CERTIFICATE_REQUEST, ssl->handshake->sent_extensions);
Jerry Yu4b8f2f72022-10-31 13:31:22 +08002660
Gilles Peskine449bd832023-01-11 14:50:10 +01002661 return 0;
XiaokangQiancec9ae62022-05-06 07:28:50 +00002662}
2663
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002664MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002665static int ssl_tls13_write_certificate_request(mbedtls_ssl_context *ssl)
XiaokangQiancec9ae62022-05-06 07:28:50 +00002666{
2667 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
2668
Gilles Peskine449bd832023-01-11 14:50:10 +01002669 MBEDTLS_SSL_DEBUG_MSG(2, ("=> write certificate request"));
XiaokangQiancec9ae62022-05-06 07:28:50 +00002670
Gilles Peskine449bd832023-01-11 14:50:10 +01002671 MBEDTLS_SSL_PROC_CHK_NEG(ssl_tls13_certificate_request_coordinate(ssl));
XiaokangQiancec9ae62022-05-06 07:28:50 +00002672
Gilles Peskine449bd832023-01-11 14:50:10 +01002673 if (ret == SSL_CERTIFICATE_REQUEST_SEND_REQUEST) {
XiaokangQiancec9ae62022-05-06 07:28:50 +00002674 unsigned char *buf;
2675 size_t buf_len, msg_len;
2676
Xiaokang Qian9f1747b2023-03-30 02:50:04 +00002677 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_start_handshake_msg(
2678 ssl, MBEDTLS_SSL_HS_CERTIFICATE_REQUEST,
2679 &buf, &buf_len));
XiaokangQiancec9ae62022-05-06 07:28:50 +00002680
Gilles Peskine449bd832023-01-11 14:50:10 +01002681 MBEDTLS_SSL_PROC_CHK(ssl_tls13_write_certificate_request_body(
2682 ssl, buf, buf + buf_len, &msg_len));
XiaokangQiancec9ae62022-05-06 07:28:50 +00002683
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +01002684 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_add_hs_msg_to_checksum(
Xiaokang Qian9f1747b2023-03-30 02:50:04 +00002685 ssl, MBEDTLS_SSL_HS_CERTIFICATE_REQUEST,
2686 buf, msg_len));
XiaokangQiancec9ae62022-05-06 07:28:50 +00002687
Gilles Peskine449bd832023-01-11 14:50:10 +01002688 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_finish_handshake_msg(
2689 ssl, buf_len, msg_len));
2690 } else if (ret == SSL_CERTIFICATE_REQUEST_SKIP) {
2691 MBEDTLS_SSL_DEBUG_MSG(2, ("<= skip write certificate request"));
XiaokangQiancec9ae62022-05-06 07:28:50 +00002692 ret = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01002693 } else {
2694 MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
XiaokangQiancec9ae62022-05-06 07:28:50 +00002695 ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
2696 goto cleanup;
2697 }
2698
Gilles Peskine449bd832023-01-11 14:50:10 +01002699 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_SERVER_CERTIFICATE);
XiaokangQiancec9ae62022-05-06 07:28:50 +00002700cleanup:
2701
Gilles Peskine449bd832023-01-11 14:50:10 +01002702 MBEDTLS_SSL_DEBUG_MSG(2, ("<= write certificate request"));
2703 return ret;
XiaokangQiancec9ae62022-05-06 07:28:50 +00002704}
XiaokangQiancec9ae62022-05-06 07:28:50 +00002705
Jerry Yu4d3841a2022-04-16 12:37:19 +08002706/*
Jerry Yuc6e6dbf2022-04-16 19:42:57 +08002707 * Handler for MBEDTLS_SSL_SERVER_CERTIFICATE
Jerry Yu83da34e2022-04-16 13:59:52 +08002708 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002709MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002710static int ssl_tls13_write_server_certificate(mbedtls_ssl_context *ssl)
Jerry Yu83da34e2022-04-16 13:59:52 +08002711{
Jerry Yu5a26f302022-05-10 20:46:40 +08002712 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
XiaokangQianfb665a82022-06-15 03:57:21 +00002713
2714#if defined(MBEDTLS_X509_CRT_PARSE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01002715 if ((ssl_tls13_pick_key_cert(ssl) != 0) ||
2716 mbedtls_ssl_own_cert(ssl) == NULL) {
2717 MBEDTLS_SSL_DEBUG_MSG(2, ("No certificate available."));
2718 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE,
2719 MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE);
2720 return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
Jerry Yu5a26f302022-05-10 20:46:40 +08002721 }
XiaokangQianfb665a82022-06-15 03:57:21 +00002722#endif /* MBEDTLS_X509_CRT_PARSE_C */
Jerry Yu5a26f302022-05-10 20:46:40 +08002723
Gilles Peskine449bd832023-01-11 14:50:10 +01002724 ret = mbedtls_ssl_tls13_write_certificate(ssl);
2725 if (ret != 0) {
2726 return ret;
2727 }
2728 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_CERTIFICATE_VERIFY);
2729 return 0;
Jerry Yu83da34e2022-04-16 13:59:52 +08002730}
2731
2732/*
Jerry Yuc6e6dbf2022-04-16 19:42:57 +08002733 * Handler for MBEDTLS_SSL_CERTIFICATE_VERIFY
Jerry Yu83da34e2022-04-16 13:59:52 +08002734 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002735MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002736static int ssl_tls13_write_certificate_verify(mbedtls_ssl_context *ssl)
Jerry Yu83da34e2022-04-16 13:59:52 +08002737{
Gilles Peskine449bd832023-01-11 14:50:10 +01002738 int ret = mbedtls_ssl_tls13_write_certificate_verify(ssl);
2739 if (ret != 0) {
2740 return ret;
2741 }
2742 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_SERVER_FINISHED);
2743 return 0;
Jerry Yu83da34e2022-04-16 13:59:52 +08002744}
Ronald Cron928cbd32022-10-04 16:14:26 +02002745#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED */
Jerry Yu83da34e2022-04-16 13:59:52 +08002746
Jerry Yu9b72e392023-12-01 16:27:08 +08002747/*
2748 * RFC 8446 section A.2
2749 *
2750 * | Send ServerHello
2751 * | K_send = handshake
2752 * | Send EncryptedExtensions
2753 * | [Send CertificateRequest]
2754 * Can send | [Send Certificate + CertificateVerify]
2755 * app data | Send Finished
2756 * after --> | K_send = application
2757 * here +--------+--------+
2758 * No 0-RTT | | 0-RTT
2759 * | |
2760 * K_recv = handshake | | K_recv = early data
2761 * [Skip decrypt errors] | +------> WAIT_EOED -+
2762 * | | Recv | | Recv EndOfEarlyData
2763 * | | early data | | K_recv = handshake
2764 * | +------------+ |
2765 * | |
2766 * +> WAIT_FLIGHT2 <--------+
2767 * |
2768 * +--------+--------+
2769 * No auth | | Client auth
2770 * | |
2771 * | v
2772 * | WAIT_CERT
2773 * | Recv | | Recv Certificate
2774 * | empty | v
2775 * | Certificate | WAIT_CV
2776 * | | | Recv
2777 * | v | CertificateVerify
2778 * +-> WAIT_FINISHED <---+
2779 * | Recv Finished
2780 *
2781 *
2782 * The following function handles the state changes after WAIT_FLIGHT2 in the
Jerry Yu3be85072023-12-04 09:58:54 +08002783 * above diagram. We are not going to receive early data related messages
2784 * anymore, prepare to receive the first handshake message of the client
2785 * second flight.
Jerry Yu9b72e392023-12-01 16:27:08 +08002786 */
Jerry Yu3be85072023-12-04 09:58:54 +08002787static void ssl_tls13_prepare_for_handshake_second_flight(
2788 mbedtls_ssl_context *ssl)
Jerry Yu9b72e392023-12-01 16:27:08 +08002789{
Jerry Yu9b72e392023-12-01 16:27:08 +08002790 if (ssl->handshake->certificate_request_sent) {
2791 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_CLIENT_CERTIFICATE);
2792 } else {
Jerry Yu42020fb2023-12-05 17:35:53 +08002793 MBEDTLS_SSL_DEBUG_MSG(2, ("skip parse certificate"));
Jerry Yuebb1b1d2023-12-05 11:02:15 +08002794 MBEDTLS_SSL_DEBUG_MSG(2, ("skip parse certificate verify"));
Jerry Yuebb1b1d2023-12-05 11:02:15 +08002795
Jerry Yu9b72e392023-12-01 16:27:08 +08002796 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_CLIENT_FINISHED);
2797 }
Jerry Yu9b72e392023-12-01 16:27:08 +08002798}
2799
Jerry Yu83da34e2022-04-16 13:59:52 +08002800/*
Jerry Yud6e253d2022-05-18 13:59:24 +08002801 * Handler for MBEDTLS_SSL_SERVER_FINISHED
Jerry Yu69dd8d42022-04-16 12:51:26 +08002802 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002803MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002804static int ssl_tls13_write_server_finished(mbedtls_ssl_context *ssl)
Jerry Yu69dd8d42022-04-16 12:51:26 +08002805{
Jerry Yud6e253d2022-05-18 13:59:24 +08002806 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yu27bdc7c2022-04-16 13:33:27 +08002807
Gilles Peskine449bd832023-01-11 14:50:10 +01002808 ret = mbedtls_ssl_tls13_write_finished_message(ssl);
2809 if (ret != 0) {
2810 return ret;
2811 }
Jerry Yu27bdc7c2022-04-16 13:33:27 +08002812
Gilles Peskine449bd832023-01-11 14:50:10 +01002813 ret = mbedtls_ssl_tls13_compute_application_transform(ssl);
2814 if (ret != 0) {
Jerry Yue3d67cb2022-05-19 15:33:10 +08002815 MBEDTLS_SSL_PEND_FATAL_ALERT(
Gilles Peskine449bd832023-01-11 14:50:10 +01002816 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE,
2817 MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE);
2818 return ret;
Jerry Yue3d67cb2022-05-19 15:33:10 +08002819 }
XiaokangQianc3017f62022-05-13 05:55:41 +00002820
Jerry Yu87b5ed42022-12-12 13:07:07 +08002821#if defined(MBEDTLS_SSL_EARLY_DATA)
Ronald Cron78a38f62024-02-01 18:30:31 +01002822 if (ssl->handshake->early_data_accepted) {
Jerry Yu0af63dc2023-12-01 17:14:51 +08002823 /* See RFC 8446 section A.2 for more information */
Jerry Yu87b5ed42022-12-12 13:07:07 +08002824 MBEDTLS_SSL_DEBUG_MSG(
2825 1, ("Switch to early keys for inbound traffic. "
2826 "( K_recv = early data )"));
2827 mbedtls_ssl_set_inbound_transform(
2828 ssl, ssl->handshake->transform_earlydata);
2829 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_END_OF_EARLY_DATA);
2830 return 0;
2831 }
2832#endif /* MBEDTLS_SSL_EARLY_DATA */
Jerry Yu0af63dc2023-12-01 17:14:51 +08002833 MBEDTLS_SSL_DEBUG_MSG(
2834 1, ("Switch to handshake keys for inbound traffic "
2835 "( K_recv = handshake )"));
Gilles Peskine449bd832023-01-11 14:50:10 +01002836 mbedtls_ssl_set_inbound_transform(ssl, ssl->handshake->transform_handshake);
XiaokangQian189ded22022-05-10 08:12:17 +00002837
Jerry Yu3be85072023-12-04 09:58:54 +08002838 ssl_tls13_prepare_for_handshake_second_flight(ssl);
Jerry Yu7d8c3fe2022-12-12 12:59:44 +08002839
2840 return 0;
2841}
2842
Jerry Yu87b5ed42022-12-12 13:07:07 +08002843#if defined(MBEDTLS_SSL_EARLY_DATA)
2844/*
Jerry Yu59d420f2023-12-01 16:30:34 +08002845 * Handler for MBEDTLS_SSL_END_OF_EARLY_DATA
Jerry Yu87b5ed42022-12-12 13:07:07 +08002846 */
Jerry Yu3be85072023-12-04 09:58:54 +08002847#define SSL_GOT_END_OF_EARLY_DATA 0
Jerry Yub55f9eb2023-12-05 10:27:17 +08002848#define SSL_GOT_EARLY_DATA 1
Jerry Yud5c34962023-12-01 16:32:31 +08002849/* Coordination:
Jerry Yu3be85072023-12-04 09:58:54 +08002850 * Deals with the ambiguity of not knowing if the next message is an
2851 * EndOfEarlyData message or an application message containing early data.
Jerry Yud5c34962023-12-01 16:32:31 +08002852 * Returns a negative code on failure, or
Jerry Yu3be85072023-12-04 09:58:54 +08002853 * - SSL_GOT_END_OF_EARLY_DATA
Jerry Yub55f9eb2023-12-05 10:27:17 +08002854 * - SSL_GOT_EARLY_DATA
Jerry Yud5c34962023-12-01 16:32:31 +08002855 * indicating which message is received.
2856 */
2857MBEDTLS_CHECK_RETURN_CRITICAL
2858static int ssl_tls13_end_of_early_data_coordinate(mbedtls_ssl_context *ssl)
2859{
2860 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yub4ed4602023-12-01 16:34:00 +08002861
2862 if ((ret = mbedtls_ssl_read_record(ssl, 0)) != 0) {
2863 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_read_record", ret);
2864 return ret;
2865 }
2866 ssl->keep_current_message = 1;
2867
2868 if (ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
2869 ssl->in_msg[0] == MBEDTLS_SSL_HS_END_OF_EARLY_DATA) {
Jerry Yub55f9eb2023-12-05 10:27:17 +08002870 MBEDTLS_SSL_DEBUG_MSG(3, ("Received an end_of_early_data message."));
Jerry Yu3be85072023-12-04 09:58:54 +08002871 return SSL_GOT_END_OF_EARLY_DATA;
Jerry Yub4ed4602023-12-01 16:34:00 +08002872 }
2873
2874 if (ssl->in_msgtype == MBEDTLS_SSL_MSG_APPLICATION_DATA) {
Jerry Yub55f9eb2023-12-05 10:27:17 +08002875 MBEDTLS_SSL_DEBUG_MSG(3, ("Received early data"));
Jerry Yu6a5904d2023-12-06 17:11:12 +08002876 /* RFC 8446 section 4.6.1
2877 *
2878 * A server receiving more than max_early_data_size bytes of 0-RTT data
2879 * SHOULD terminate the connection with an "unexpected_message" alert.
2880 *
2881 * TODO: Add received data size check here.
2882 */
Ronald Croned7d4bf2024-01-31 07:55:19 +01002883 if (ssl->in_offt == NULL) {
2884 /* Set the reading pointer */
2885 ssl->in_offt = ssl->in_msg;
2886 }
Jerry Yub55f9eb2023-12-05 10:27:17 +08002887 return SSL_GOT_EARLY_DATA;
Jerry Yub4ed4602023-12-01 16:34:00 +08002888 }
2889
Jerry Yu7bb40a32023-12-04 10:04:15 +08002890 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE,
2891 MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE);
Jerry Yub4ed4602023-12-01 16:34:00 +08002892 return MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE;
Jerry Yud5c34962023-12-01 16:32:31 +08002893}
2894
2895MBEDTLS_CHECK_RETURN_CRITICAL
2896static int ssl_tls13_parse_end_of_early_data(mbedtls_ssl_context *ssl,
2897 const unsigned char *buf,
2898 const unsigned char *end)
2899{
Jerry Yu75c9ab72023-12-01 16:41:40 +08002900 /* RFC 8446 section 4.5
2901 *
2902 * struct {} EndOfEarlyData;
2903 */
Jerry Yufbf03992023-12-04 10:00:37 +08002904 if (buf != end) {
2905 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR,
2906 MBEDTLS_ERR_SSL_DECODE_ERROR);
2907 return MBEDTLS_ERR_SSL_DECODE_ERROR;
2908 }
2909 return 0;
Jerry Yud5c34962023-12-01 16:32:31 +08002910}
2911
Jerry Yud5c34962023-12-01 16:32:31 +08002912/*
2913 * RFC 8446 section A.2
2914 *
2915 * | Send ServerHello
2916 * | K_send = handshake
2917 * | Send EncryptedExtensions
2918 * | [Send CertificateRequest]
2919 * Can send | [Send Certificate + CertificateVerify]
2920 * app data | Send Finished
2921 * after --> | K_send = application
2922 * here +--------+--------+
2923 * No 0-RTT | | 0-RTT
2924 * | |
2925 * K_recv = handshake | | K_recv = early data
2926 * [Skip decrypt errors] | +------> WAIT_EOED -+
2927 * | | Recv | | Recv EndOfEarlyData
2928 * | | early data | | K_recv = handshake
2929 * | +------------+ |
2930 * | |
2931 * +> WAIT_FLIGHT2 <--------+
2932 * |
2933 * +--------+--------+
2934 * No auth | | Client auth
2935 * | |
2936 * | v
2937 * | WAIT_CERT
2938 * | Recv | | Recv Certificate
2939 * | empty | v
2940 * | Certificate | WAIT_CV
2941 * | | | Recv
2942 * | v | CertificateVerify
2943 * +-> WAIT_FINISHED <---+
2944 * | Recv Finished
2945 *
2946 * The function handles actions and state changes from 0-RTT to WAIT_FLIGHT2 in
2947 * the above diagram.
2948 */
Jerry Yu87b5ed42022-12-12 13:07:07 +08002949MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yu59d420f2023-12-01 16:30:34 +08002950static int ssl_tls13_process_end_of_early_data(mbedtls_ssl_context *ssl)
Jerry Yu87b5ed42022-12-12 13:07:07 +08002951{
2952 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yud5c34962023-12-01 16:32:31 +08002953
2954 MBEDTLS_SSL_DEBUG_MSG(2, ("=> ssl_tls13_process_end_of_early_data"));
2955
2956 MBEDTLS_SSL_PROC_CHK_NEG(ssl_tls13_end_of_early_data_coordinate(ssl));
2957
Jerry Yu3be85072023-12-04 09:58:54 +08002958 if (ret == SSL_GOT_END_OF_EARLY_DATA) {
Jerry Yud5c34962023-12-01 16:32:31 +08002959 unsigned char *buf;
2960 size_t buf_len;
2961
2962 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_tls13_fetch_handshake_msg(
2963 ssl, MBEDTLS_SSL_HS_END_OF_EARLY_DATA,
2964 &buf, &buf_len));
2965
2966 MBEDTLS_SSL_PROC_CHK(ssl_tls13_parse_end_of_early_data(
2967 ssl, buf, buf + buf_len));
2968
Jerry Yue9655122023-12-01 16:44:40 +08002969 MBEDTLS_SSL_DEBUG_MSG(
2970 1, ("Switch to handshake keys for inbound traffic"
2971 "( K_recv = handshake )"));
2972 mbedtls_ssl_set_inbound_transform(
2973 ssl, ssl->handshake->transform_handshake);
2974
Jerry Yud5c34962023-12-01 16:32:31 +08002975 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_add_hs_msg_to_checksum(
2976 ssl, MBEDTLS_SSL_HS_END_OF_EARLY_DATA,
2977 buf, buf_len));
Jerry Yue9655122023-12-01 16:44:40 +08002978
Jerry Yu3be85072023-12-04 09:58:54 +08002979 ssl_tls13_prepare_for_handshake_second_flight(ssl);
Jerry Yud5c34962023-12-01 16:32:31 +08002980
Jerry Yub55f9eb2023-12-05 10:27:17 +08002981 } else if (ret == SSL_GOT_EARLY_DATA) {
Jerry Yud9ca3542023-12-06 17:23:52 +08002982 ret = MBEDTLS_ERR_SSL_RECEIVED_EARLY_DATA;
2983 goto cleanup;
Jerry Yud5c34962023-12-01 16:32:31 +08002984 } else {
2985 MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
2986 ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
2987 goto cleanup;
2988 }
2989
Jerry Yud5c34962023-12-01 16:32:31 +08002990cleanup:
2991 MBEDTLS_SSL_DEBUG_MSG(2, ("<= ssl_tls13_process_end_of_early_data"));
Jerry Yu87b5ed42022-12-12 13:07:07 +08002992 return ret;
2993}
2994#endif /* MBEDTLS_SSL_EARLY_DATA */
2995
Jerry Yu69dd8d42022-04-16 12:51:26 +08002996/*
Jerry Yud6e253d2022-05-18 13:59:24 +08002997 * Handler for MBEDTLS_SSL_CLIENT_FINISHED
Jerry Yu69dd8d42022-04-16 12:51:26 +08002998 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002999MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01003000static int ssl_tls13_process_client_finished(mbedtls_ssl_context *ssl)
Jerry Yu69dd8d42022-04-16 12:51:26 +08003001{
Jerry Yud6e253d2022-05-18 13:59:24 +08003002 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
3003
Gilles Peskine449bd832023-01-11 14:50:10 +01003004 ret = mbedtls_ssl_tls13_process_finished_message(ssl);
3005 if (ret != 0) {
3006 return ret;
Jerry Yue3d67cb2022-05-19 15:33:10 +08003007 }
3008
Gilles Peskine449bd832023-01-11 14:50:10 +01003009 ret = mbedtls_ssl_tls13_compute_resumption_master_secret(ssl);
3010 if (ret != 0) {
Xiaokang Qian9f1747b2023-03-30 02:50:04 +00003011 MBEDTLS_SSL_DEBUG_RET(
3012 1, "mbedtls_ssl_tls13_compute_resumption_master_secret", ret);
Gilles Peskine449bd832023-01-11 14:50:10 +01003013 }
3014
3015 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_HANDSHAKE_WRAPUP);
3016 return 0;
Jerry Yu69dd8d42022-04-16 12:51:26 +08003017}
3018
3019/*
Jerry Yud6e253d2022-05-18 13:59:24 +08003020 * Handler for MBEDTLS_SSL_HANDSHAKE_WRAPUP
Jerry Yu69dd8d42022-04-16 12:51:26 +08003021 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02003022MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01003023static int ssl_tls13_handshake_wrapup(mbedtls_ssl_context *ssl)
Jerry Yu69dd8d42022-04-16 12:51:26 +08003024{
Gilles Peskine449bd832023-01-11 14:50:10 +01003025 MBEDTLS_SSL_DEBUG_MSG(2, ("handshake: done"));
Jerry Yu03ed50b2022-04-16 17:13:30 +08003026
Gilles Peskine449bd832023-01-11 14:50:10 +01003027 mbedtls_ssl_tls13_handshake_wrapup(ssl);
Jerry Yue67bef42022-07-07 07:29:42 +00003028
Pengyu Lv3643fdb2023-01-12 16:46:28 +08003029#if defined(MBEDTLS_SSL_SESSION_TICKETS) && \
3030 defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED)
Pengyu Lva1aa31b2022-12-13 13:49:59 +08003031/* TODO: Remove the check of SOME_PSK_ENABLED since SESSION_TICKETS requires
3032 * SOME_PSK_ENABLED to be enabled. Here is just to make CI happy. It is
3033 * expected to be resolved with issue#6395.
3034 */
Pengyu Lvc7af2c42022-12-01 16:33:00 +08003035 /* Sent NewSessionTicket message only when client supports PSK */
Pengyu Lvb2cfafb2023-11-14 13:56:13 +08003036 if (mbedtls_ssl_tls13_is_some_psk_supported(ssl)) {
Xiaokang Qian9f1747b2023-03-30 02:50:04 +00003037 mbedtls_ssl_handshake_set_state(
3038 ssl, MBEDTLS_SSL_TLS1_3_NEW_SESSION_TICKET);
Pengyu Lvc7af2c42022-12-01 16:33:00 +08003039 } else
Jerry Yufca4d572022-07-21 10:37:48 +08003040#endif
Pengyu Lv3643fdb2023-01-12 16:46:28 +08003041 {
3042 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_HANDSHAKE_OVER);
3043 }
Gilles Peskine449bd832023-01-11 14:50:10 +01003044 return 0;
Jerry Yu69dd8d42022-04-16 12:51:26 +08003045}
3046
3047/*
Jerry Yua8d3c502022-10-30 14:51:23 +08003048 * Handler for MBEDTLS_SSL_TLS1_3_NEW_SESSION_TICKET
Jerry Yue67bef42022-07-07 07:29:42 +00003049 */
3050#define SSL_NEW_SESSION_TICKET_SKIP 0
3051#define SSL_NEW_SESSION_TICKET_WRITE 1
3052MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01003053static int ssl_tls13_write_new_session_ticket_coordinate(mbedtls_ssl_context *ssl)
Jerry Yue67bef42022-07-07 07:29:42 +00003054{
3055 /* Check whether the use of session tickets is enabled */
Gilles Peskine449bd832023-01-11 14:50:10 +01003056 if (ssl->conf->f_ticket_write == NULL) {
3057 MBEDTLS_SSL_DEBUG_MSG(2, ("NewSessionTicket: disabled,"
3058 " callback is not set"));
3059 return SSL_NEW_SESSION_TICKET_SKIP;
Jerry Yud0766ec2022-09-22 10:46:57 +08003060 }
Gilles Peskine449bd832023-01-11 14:50:10 +01003061 if (ssl->conf->new_session_tickets_count == 0) {
3062 MBEDTLS_SSL_DEBUG_MSG(2, ("NewSessionTicket: disabled,"
3063 " configured count is zero"));
3064 return SSL_NEW_SESSION_TICKET_SKIP;
Jerry Yud0766ec2022-09-22 10:46:57 +08003065 }
3066
Gilles Peskine449bd832023-01-11 14:50:10 +01003067 if (ssl->handshake->new_session_tickets_count == 0) {
3068 MBEDTLS_SSL_DEBUG_MSG(2, ("NewSessionTicket: all tickets have "
3069 "been sent."));
3070 return SSL_NEW_SESSION_TICKET_SKIP;
Jerry Yue67bef42022-07-07 07:29:42 +00003071 }
3072
Gilles Peskine449bd832023-01-11 14:50:10 +01003073 return SSL_NEW_SESSION_TICKET_WRITE;
Jerry Yue67bef42022-07-07 07:29:42 +00003074}
3075
3076#if defined(MBEDTLS_SSL_SESSION_TICKETS)
3077MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01003078static int ssl_tls13_prepare_new_session_ticket(mbedtls_ssl_context *ssl,
3079 unsigned char *ticket_nonce,
3080 size_t ticket_nonce_size)
Jerry Yue67bef42022-07-07 07:29:42 +00003081{
3082 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
3083 mbedtls_ssl_session *session = ssl->session;
3084 mbedtls_ssl_ciphersuite_t *ciphersuite_info;
3085 psa_algorithm_t psa_hash_alg;
3086 int hash_length;
3087
Gilles Peskine449bd832023-01-11 14:50:10 +01003088 MBEDTLS_SSL_DEBUG_MSG(2, ("=> prepare NewSessionTicket msg"));
Jerry Yue67bef42022-07-07 07:29:42 +00003089
Pengyu Lv9f926952022-11-17 15:22:33 +08003090 /* Set ticket_flags depends on the advertised psk key exchange mode */
Pengyu Lv94a42cc2023-12-06 10:04:17 +08003091 mbedtls_ssl_tls13_session_clear_ticket_flags(
Pengyu Lv9eacb442022-12-09 14:39:19 +08003092 session, MBEDTLS_SSL_TLS1_3_TICKET_FLAGS_MASK);
Pengyu Lve6487fe2022-12-06 09:30:29 +08003093#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED)
Pengyu Lv94a42cc2023-12-06 10:04:17 +08003094 mbedtls_ssl_tls13_session_set_ticket_flags(
Pengyu Lv9eacb442022-12-09 14:39:19 +08003095 session, ssl->handshake->tls13_kex_modes);
Pengyu Lve6487fe2022-12-06 09:30:29 +08003096#endif
Jerry Yu95648b02023-12-06 15:03:34 +08003097
3098#if defined(MBEDTLS_SSL_EARLY_DATA)
3099 if (ssl->conf->early_data_enabled == MBEDTLS_SSL_EARLY_DATA_ENABLED &&
3100 ssl->conf->max_early_data_size > 0) {
Pengyu Lv94a42cc2023-12-06 10:04:17 +08003101 mbedtls_ssl_tls13_session_set_ticket_flags(
Jerry Yu95648b02023-12-06 15:03:34 +08003102 session, MBEDTLS_SSL_TLS1_3_TICKET_ALLOW_EARLY_DATA);
3103 }
3104#endif /* MBEDTLS_SSL_EARLY_DATA */
3105
Pengyu Lv4938a562023-01-16 11:28:49 +08003106 MBEDTLS_SSL_PRINT_TICKET_FLAGS(4, session->ticket_flags);
Pengyu Lv9f926952022-11-17 15:22:33 +08003107
Jerry Yue67bef42022-07-07 07:29:42 +00003108 /* Generate ticket_age_add */
Gilles Peskine449bd832023-01-11 14:50:10 +01003109 if ((ret = ssl->conf->f_rng(ssl->conf->p_rng,
3110 (unsigned char *) &session->ticket_age_add,
3111 sizeof(session->ticket_age_add)) != 0)) {
3112 MBEDTLS_SSL_DEBUG_RET(1, "generate_ticket_age_add", ret);
3113 return ret;
Jerry Yue67bef42022-07-07 07:29:42 +00003114 }
Gilles Peskine449bd832023-01-11 14:50:10 +01003115 MBEDTLS_SSL_DEBUG_MSG(3, ("ticket_age_add: %u",
3116 (unsigned int) session->ticket_age_add));
Jerry Yue67bef42022-07-07 07:29:42 +00003117
3118 /* Generate ticket_nonce */
Gilles Peskine449bd832023-01-11 14:50:10 +01003119 ret = ssl->conf->f_rng(ssl->conf->p_rng, ticket_nonce, ticket_nonce_size);
3120 if (ret != 0) {
3121 MBEDTLS_SSL_DEBUG_RET(1, "generate_ticket_nonce", ret);
3122 return ret;
Jerry Yue67bef42022-07-07 07:29:42 +00003123 }
Gilles Peskine449bd832023-01-11 14:50:10 +01003124 MBEDTLS_SSL_DEBUG_BUF(3, "ticket_nonce:",
3125 ticket_nonce, ticket_nonce_size);
Jerry Yue67bef42022-07-07 07:29:42 +00003126
3127 ciphersuite_info =
Gilles Peskine449bd832023-01-11 14:50:10 +01003128 (mbedtls_ssl_ciphersuite_t *) ssl->handshake->ciphersuite_info;
Dave Rodgman2eab4622023-10-05 13:30:37 +01003129 psa_hash_alg = mbedtls_md_psa_alg_from_type((mbedtls_md_type_t) ciphersuite_info->mac);
Gilles Peskine449bd832023-01-11 14:50:10 +01003130 hash_length = PSA_HASH_LENGTH(psa_hash_alg);
3131 if (hash_length == -1 ||
3132 (size_t) hash_length > sizeof(session->resumption_key)) {
3133 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Jerry Yufca4d572022-07-21 10:37:48 +08003134 }
3135
Jerry Yue67bef42022-07-07 07:29:42 +00003136 /* In this code the psk key length equals the length of the hash */
3137 session->resumption_key_len = hash_length;
3138 session->ciphersuite = ciphersuite_info->id;
3139
3140 /* Compute resumption key
3141 *
3142 * HKDF-Expand-Label( resumption_master_secret,
3143 * "resumption", ticket_nonce, Hash.length )
3144 */
3145 ret = mbedtls_ssl_tls13_hkdf_expand_label(
Gilles Peskine449bd832023-01-11 14:50:10 +01003146 psa_hash_alg,
3147 session->app_secrets.resumption_master_secret,
3148 hash_length,
3149 MBEDTLS_SSL_TLS1_3_LBL_WITH_LEN(resumption),
3150 ticket_nonce,
3151 ticket_nonce_size,
3152 session->resumption_key,
3153 hash_length);
Jerry Yue67bef42022-07-07 07:29:42 +00003154
Gilles Peskine449bd832023-01-11 14:50:10 +01003155 if (ret != 0) {
3156 MBEDTLS_SSL_DEBUG_RET(2,
3157 "Creating the ticket-resumed PSK failed",
3158 ret);
3159 return ret;
Jerry Yue67bef42022-07-07 07:29:42 +00003160 }
Gilles Peskine449bd832023-01-11 14:50:10 +01003161 MBEDTLS_SSL_DEBUG_BUF(3, "Ticket-resumed PSK",
3162 session->resumption_key,
3163 session->resumption_key_len);
Jerry Yue67bef42022-07-07 07:29:42 +00003164
Gilles Peskine449bd832023-01-11 14:50:10 +01003165 MBEDTLS_SSL_DEBUG_BUF(3, "resumption_master_secret",
3166 session->app_secrets.resumption_master_secret,
3167 hash_length);
Jerry Yue67bef42022-07-07 07:29:42 +00003168
Gilles Peskine449bd832023-01-11 14:50:10 +01003169 return 0;
Jerry Yue67bef42022-07-07 07:29:42 +00003170}
3171
3172/* This function creates a NewSessionTicket message in the following format:
3173 *
3174 * struct {
3175 * uint32 ticket_lifetime;
3176 * uint32 ticket_age_add;
3177 * opaque ticket_nonce<0..255>;
3178 * opaque ticket<1..2^16-1>;
3179 * Extension extensions<0..2^16-2>;
3180 * } NewSessionTicket;
3181 *
3182 * The ticket inside the NewSessionTicket message is an encrypted container
3183 * carrying the necessary information so that the server is later able to
3184 * re-start the communication.
3185 *
3186 * The following fields are placed inside the ticket by the
3187 * f_ticket_write() function:
3188 *
Jerry Yu0069abc2023-11-23 21:07:28 +08003189 * - creation time (ticket_creation_time)
3190 * - flags (ticket_flags)
Jerry Yue67bef42022-07-07 07:29:42 +00003191 * - age add (ticket_age_add)
Jerry Yu0069abc2023-11-23 21:07:28 +08003192 * - key (resumption_key)
3193 * - key length (resumption_key_len)
Jerry Yue67bef42022-07-07 07:29:42 +00003194 * - ciphersuite (ciphersuite)
Jerry Yu0069abc2023-11-23 21:07:28 +08003195 * - max_early_data_size (max_early_data_size)
Jerry Yue67bef42022-07-07 07:29:42 +00003196 */
3197MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01003198static int ssl_tls13_write_new_session_ticket_body(mbedtls_ssl_context *ssl,
3199 unsigned char *buf,
3200 unsigned char *end,
3201 size_t *out_len,
3202 unsigned char *ticket_nonce,
3203 size_t ticket_nonce_size)
Jerry Yue67bef42022-07-07 07:29:42 +00003204{
3205 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
3206 unsigned char *p = buf;
3207 mbedtls_ssl_session *session = ssl->session;
3208 size_t ticket_len;
3209 uint32_t ticket_lifetime;
Jerry Yu01da35e2022-12-12 15:09:22 +08003210 unsigned char *p_extensions_len;
Jerry Yue67bef42022-07-07 07:29:42 +00003211
3212 *out_len = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01003213 MBEDTLS_SSL_DEBUG_MSG(2, ("=> write NewSessionTicket msg"));
Jerry Yue67bef42022-07-07 07:29:42 +00003214
3215 /*
3216 * ticket_lifetime 4 bytes
3217 * ticket_age_add 4 bytes
3218 * ticket_nonce 1 + ticket_nonce_size bytes
3219 * ticket >=2 bytes
3220 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003221 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 4 + 4 + 1 + ticket_nonce_size + 2);
Jerry Yue67bef42022-07-07 07:29:42 +00003222
3223 /* Generate ticket and ticket_lifetime */
Ronald Cron3c0072b2023-11-22 10:00:14 +01003224#if defined(MBEDTLS_HAVE_TIME)
3225 session->ticket_creation_time = mbedtls_ms_time();
3226#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01003227 ret = ssl->conf->f_ticket_write(ssl->conf->p_ticket,
3228 session,
3229 p + 9 + ticket_nonce_size + 2,
3230 end,
3231 &ticket_len,
3232 &ticket_lifetime);
3233 if (ret != 0) {
3234 MBEDTLS_SSL_DEBUG_RET(1, "write_ticket", ret);
3235 return ret;
Jerry Yue67bef42022-07-07 07:29:42 +00003236 }
3237 /* RFC 8446 4.6.1
3238 * ticket_lifetime: Indicates the lifetime in seconds as a 32-bit
3239 * unsigned integer in network byte order from the time of ticket
3240 * issuance. Servers MUST NOT use any value greater than
3241 * 604800 seconds (7 days). The value of zero indicates that the
3242 * ticket should be discarded immediately. Clients MUST NOT cache
3243 * tickets for longer than 7 days, regardless of the ticket_lifetime,
3244 * and MAY delete tickets earlier based on local policy. A server
3245 * MAY treat a ticket as valid for a shorter period of time than what
3246 * is stated in the ticket_lifetime.
3247 */
Jerry Yu8e0174a2023-11-10 13:58:16 +08003248 if (ticket_lifetime > MBEDTLS_SSL_TLS1_3_MAX_ALLOWED_TICKET_LIFETIME) {
3249 ticket_lifetime = MBEDTLS_SSL_TLS1_3_MAX_ALLOWED_TICKET_LIFETIME;
Gilles Peskine449bd832023-01-11 14:50:10 +01003250 }
3251 MBEDTLS_PUT_UINT32_BE(ticket_lifetime, p, 0);
3252 MBEDTLS_SSL_DEBUG_MSG(3, ("ticket_lifetime: %u",
3253 (unsigned int) ticket_lifetime));
Jerry Yue67bef42022-07-07 07:29:42 +00003254
3255 /* Write ticket_age_add */
Gilles Peskine449bd832023-01-11 14:50:10 +01003256 MBEDTLS_PUT_UINT32_BE(session->ticket_age_add, p, 4);
3257 MBEDTLS_SSL_DEBUG_MSG(3, ("ticket_age_add: %u",
3258 (unsigned int) session->ticket_age_add));
Jerry Yue67bef42022-07-07 07:29:42 +00003259
3260 /* Write ticket_nonce */
Gilles Peskine449bd832023-01-11 14:50:10 +01003261 p[8] = (unsigned char) ticket_nonce_size;
3262 if (ticket_nonce_size > 0) {
3263 memcpy(p + 9, ticket_nonce, ticket_nonce_size);
Jerry Yue67bef42022-07-07 07:29:42 +00003264 }
3265 p += 9 + ticket_nonce_size;
3266
3267 /* Write ticket */
Gilles Peskine449bd832023-01-11 14:50:10 +01003268 MBEDTLS_PUT_UINT16_BE(ticket_len, p, 0);
Jerry Yue67bef42022-07-07 07:29:42 +00003269 p += 2;
Gilles Peskine449bd832023-01-11 14:50:10 +01003270 MBEDTLS_SSL_DEBUG_BUF(4, "ticket", p, ticket_len);
Jerry Yue67bef42022-07-07 07:29:42 +00003271 p += ticket_len;
3272
3273 /* Ticket Extensions
3274 *
Jerry Yu01da35e2022-12-12 15:09:22 +08003275 * Extension extensions<0..2^16-2>;
Jerry Yue67bef42022-07-07 07:29:42 +00003276 */
Jerry Yuedab6372022-10-31 14:37:31 +08003277 ssl->handshake->sent_extensions = MBEDTLS_SSL_EXT_MASK_NONE;
3278
Gilles Peskine449bd832023-01-11 14:50:10 +01003279 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 2);
Jerry Yu01da35e2022-12-12 15:09:22 +08003280 p_extensions_len = p;
Jerry Yue67bef42022-07-07 07:29:42 +00003281 p += 2;
3282
Jerry Yu01da35e2022-12-12 15:09:22 +08003283#if defined(MBEDTLS_SSL_EARLY_DATA)
Pengyu Lv94a42cc2023-12-06 10:04:17 +08003284 if (mbedtls_ssl_tls13_session_ticket_allow_early_data(session)) {
Jerry Yu95648b02023-12-06 15:03:34 +08003285 size_t output_len;
3286
Jerry Yuf135bac2023-11-23 18:10:51 +08003287 if ((ret = mbedtls_ssl_tls13_write_early_data_ext(
Jerry Yuc59c5862023-12-05 10:40:49 +08003288 ssl, 1, p, end, &output_len)) != 0) {
Jerry Yuf135bac2023-11-23 18:10:51 +08003289 MBEDTLS_SSL_DEBUG_RET(
3290 1, "mbedtls_ssl_tls13_write_early_data_ext", ret);
3291 return ret;
3292 }
3293 p += output_len;
Jerry Yu9e7f9bc2023-11-27 16:52:07 +08003294 } else {
3295 MBEDTLS_SSL_DEBUG_MSG(
3296 4, ("early_data not allowed, "
3297 "skip early_data extension in NewSessionTicket"));
Jerry Yu01da35e2022-12-12 15:09:22 +08003298 }
Jerry Yuf135bac2023-11-23 18:10:51 +08003299
Jerry Yu01da35e2022-12-12 15:09:22 +08003300#endif /* MBEDTLS_SSL_EARLY_DATA */
3301
3302 MBEDTLS_PUT_UINT16_BE(p - p_extensions_len - 2, p_extensions_len, 0);
3303
Jerry Yue67bef42022-07-07 07:29:42 +00003304 *out_len = p - buf;
Gilles Peskine449bd832023-01-11 14:50:10 +01003305 MBEDTLS_SSL_DEBUG_BUF(4, "ticket", buf, *out_len);
3306 MBEDTLS_SSL_DEBUG_MSG(2, ("<= write new session ticket"));
Jerry Yue67bef42022-07-07 07:29:42 +00003307
Jerry Yu7de2ff02022-11-08 21:43:46 +08003308 MBEDTLS_SSL_PRINT_EXTS(
Gilles Peskine449bd832023-01-11 14:50:10 +01003309 3, MBEDTLS_SSL_HS_NEW_SESSION_TICKET, ssl->handshake->sent_extensions);
Jerry Yu4b8f2f72022-10-31 13:31:22 +08003310
Gilles Peskine449bd832023-01-11 14:50:10 +01003311 return 0;
Jerry Yue67bef42022-07-07 07:29:42 +00003312}
3313
3314/*
Jerry Yua8d3c502022-10-30 14:51:23 +08003315 * Handler for MBEDTLS_SSL_TLS1_3_NEW_SESSION_TICKET
Jerry Yue67bef42022-07-07 07:29:42 +00003316 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003317static int ssl_tls13_write_new_session_ticket(mbedtls_ssl_context *ssl)
Jerry Yue67bef42022-07-07 07:29:42 +00003318{
3319 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
3320
Gilles Peskine449bd832023-01-11 14:50:10 +01003321 MBEDTLS_SSL_PROC_CHK_NEG(ssl_tls13_write_new_session_ticket_coordinate(ssl));
Jerry Yue67bef42022-07-07 07:29:42 +00003322
Gilles Peskine449bd832023-01-11 14:50:10 +01003323 if (ret == SSL_NEW_SESSION_TICKET_WRITE) {
Jerry Yue67bef42022-07-07 07:29:42 +00003324 unsigned char ticket_nonce[MBEDTLS_SSL_TLS1_3_TICKET_NONCE_LENGTH];
3325 unsigned char *buf;
3326 size_t buf_len, msg_len;
3327
Gilles Peskine449bd832023-01-11 14:50:10 +01003328 MBEDTLS_SSL_PROC_CHK(ssl_tls13_prepare_new_session_ticket(
3329 ssl, ticket_nonce, sizeof(ticket_nonce)));
Jerry Yue67bef42022-07-07 07:29:42 +00003330
Xiaokang Qian9f1747b2023-03-30 02:50:04 +00003331 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_start_handshake_msg(
3332 ssl, MBEDTLS_SSL_HS_NEW_SESSION_TICKET,
3333 &buf, &buf_len));
Jerry Yue67bef42022-07-07 07:29:42 +00003334
Gilles Peskine449bd832023-01-11 14:50:10 +01003335 MBEDTLS_SSL_PROC_CHK(ssl_tls13_write_new_session_ticket_body(
3336 ssl, buf, buf + buf_len, &msg_len,
3337 ticket_nonce, sizeof(ticket_nonce)));
Jerry Yue67bef42022-07-07 07:29:42 +00003338
Gilles Peskine449bd832023-01-11 14:50:10 +01003339 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_finish_handshake_msg(
3340 ssl, buf_len, msg_len));
Jerry Yufca4d572022-07-21 10:37:48 +08003341
Jerry Yu359e65f2022-09-22 23:47:43 +08003342 /* Limit session tickets count to one when resumption connection.
3343 *
3344 * See document of mbedtls_ssl_conf_new_session_tickets.
3345 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003346 if (ssl->handshake->resume == 1) {
Jerry Yu359e65f2022-09-22 23:47:43 +08003347 ssl->handshake->new_session_tickets_count = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01003348 } else {
Jerry Yu359e65f2022-09-22 23:47:43 +08003349 ssl->handshake->new_session_tickets_count--;
Gilles Peskine449bd832023-01-11 14:50:10 +01003350 }
Jerry Yub7e3fa72022-09-22 11:07:18 +08003351
Jerry Yua8d3c502022-10-30 14:51:23 +08003352 mbedtls_ssl_handshake_set_state(
Gilles Peskine449bd832023-01-11 14:50:10 +01003353 ssl, MBEDTLS_SSL_TLS1_3_NEW_SESSION_TICKET_FLUSH);
3354 } else {
3355 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_HANDSHAKE_OVER);
Jerry Yue67bef42022-07-07 07:29:42 +00003356 }
3357
Jerry Yue67bef42022-07-07 07:29:42 +00003358cleanup:
3359
Gilles Peskine449bd832023-01-11 14:50:10 +01003360 return ret;
Jerry Yue67bef42022-07-07 07:29:42 +00003361}
3362#endif /* MBEDTLS_SSL_SESSION_TICKETS */
3363
3364/*
XiaokangQiane8ff3502022-04-22 02:34:40 +00003365 * TLS 1.3 State Machine -- server side
XiaokangQian7807f9f2022-02-15 10:04:37 +00003366 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003367int mbedtls_ssl_tls13_handshake_server_step(mbedtls_ssl_context *ssl)
Jerry Yub9930e72021-08-06 17:11:51 +08003368{
XiaokangQian08037552022-04-20 07:16:41 +00003369 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
XiaokangQian7807f9f2022-02-15 10:04:37 +00003370
Gilles Peskine449bd832023-01-11 14:50:10 +01003371 if (ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER || ssl->handshake == NULL) {
3372 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
3373 }
XiaokangQian7807f9f2022-02-15 10:04:37 +00003374
Gilles Peskine449bd832023-01-11 14:50:10 +01003375 MBEDTLS_SSL_DEBUG_MSG(2, ("tls13 server state: %s(%d)",
Dave Rodgman2eab4622023-10-05 13:30:37 +01003376 mbedtls_ssl_states_str((mbedtls_ssl_states) ssl->state),
Gilles Peskine449bd832023-01-11 14:50:10 +01003377 ssl->state));
Jerry Yu6e81b272021-09-27 11:16:17 +08003378
Gilles Peskine449bd832023-01-11 14:50:10 +01003379 switch (ssl->state) {
XiaokangQian7807f9f2022-02-15 10:04:37 +00003380 /* start state */
3381 case MBEDTLS_SSL_HELLO_REQUEST:
Gilles Peskine449bd832023-01-11 14:50:10 +01003382 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_CLIENT_HELLO);
XiaokangQian08037552022-04-20 07:16:41 +00003383 ret = 0;
XiaokangQian7807f9f2022-02-15 10:04:37 +00003384 break;
3385
XiaokangQian7807f9f2022-02-15 10:04:37 +00003386 case MBEDTLS_SSL_CLIENT_HELLO:
Gilles Peskine449bd832023-01-11 14:50:10 +01003387 ret = ssl_tls13_process_client_hello(ssl);
3388 if (ret != 0) {
3389 MBEDTLS_SSL_DEBUG_RET(1, "ssl_tls13_process_client_hello", ret);
3390 }
Jerry Yuf41553b2022-05-09 22:20:30 +08003391 break;
XiaokangQian7807f9f2022-02-15 10:04:37 +00003392
Jerry Yuf41553b2022-05-09 22:20:30 +08003393 case MBEDTLS_SSL_HELLO_RETRY_REQUEST:
Gilles Peskine449bd832023-01-11 14:50:10 +01003394 ret = ssl_tls13_write_hello_retry_request(ssl);
3395 if (ret != 0) {
3396 MBEDTLS_SSL_DEBUG_RET(1, "ssl_tls13_write_hello_retry_request", ret);
3397 return ret;
Jerry Yuf41553b2022-05-09 22:20:30 +08003398 }
XiaokangQian7807f9f2022-02-15 10:04:37 +00003399 break;
3400
Jerry Yu5b64ae92022-03-30 17:15:02 +08003401 case MBEDTLS_SSL_SERVER_HELLO:
Gilles Peskine449bd832023-01-11 14:50:10 +01003402 ret = ssl_tls13_write_server_hello(ssl);
Jerry Yu5b64ae92022-03-30 17:15:02 +08003403 break;
3404
Xiaofei Baicba64af2022-02-15 10:00:56 +00003405 case MBEDTLS_SSL_ENCRYPTED_EXTENSIONS:
Gilles Peskine449bd832023-01-11 14:50:10 +01003406 ret = ssl_tls13_write_encrypted_extensions(ssl);
3407 if (ret != 0) {
3408 MBEDTLS_SSL_DEBUG_RET(1, "ssl_tls13_write_encrypted_extensions", ret);
3409 return ret;
Xiaofei Baicba64af2022-02-15 10:00:56 +00003410 }
Jerry Yu48330562022-05-06 21:35:44 +08003411 break;
Jerry Yu6a2cd9e2022-05-05 11:14:19 +08003412
Ronald Cron928cbd32022-10-04 16:14:26 +02003413#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED)
Xiaofei Bai9ca09d42022-02-14 12:57:18 +00003414 case MBEDTLS_SSL_CERTIFICATE_REQUEST:
Gilles Peskine449bd832023-01-11 14:50:10 +01003415 ret = ssl_tls13_write_certificate_request(ssl);
Xiaofei Bai9ca09d42022-02-14 12:57:18 +00003416 break;
Xiaofei Bai9ca09d42022-02-14 12:57:18 +00003417
Jerry Yu83da34e2022-04-16 13:59:52 +08003418 case MBEDTLS_SSL_SERVER_CERTIFICATE:
Gilles Peskine449bd832023-01-11 14:50:10 +01003419 ret = ssl_tls13_write_server_certificate(ssl);
Jerry Yu83da34e2022-04-16 13:59:52 +08003420 break;
3421
3422 case MBEDTLS_SSL_CERTIFICATE_VERIFY:
Gilles Peskine449bd832023-01-11 14:50:10 +01003423 ret = ssl_tls13_write_certificate_verify(ssl);
Jerry Yu83da34e2022-04-16 13:59:52 +08003424 break;
Ronald Cron928cbd32022-10-04 16:14:26 +02003425#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED */
Jerry Yu83da34e2022-04-16 13:59:52 +08003426
Gilles Peskine449bd832023-01-11 14:50:10 +01003427 /*
3428 * Injection of dummy-CCS's for middlebox compatibility
3429 */
Gabor Mezei7b39bf12022-05-24 16:04:14 +02003430#if defined(MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE)
Gabor Mezeif7044ea2022-06-28 16:01:49 +02003431 case MBEDTLS_SSL_SERVER_CCS_AFTER_HELLO_RETRY_REQUEST:
Gilles Peskine449bd832023-01-11 14:50:10 +01003432 ret = mbedtls_ssl_tls13_write_change_cipher_spec(ssl);
3433 if (ret == 0) {
3434 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_CLIENT_HELLO);
3435 }
Gabor Mezei7b39bf12022-05-24 16:04:14 +02003436 break;
3437
3438 case MBEDTLS_SSL_SERVER_CCS_AFTER_SERVER_HELLO:
Ronald Crone273f722024-02-13 18:22:26 +01003439 ret = mbedtls_ssl_tls13_write_change_cipher_spec(ssl);
3440 if (ret != 0) {
3441 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01003442 }
Ronald Cronfe59ff72024-01-24 14:31:50 +01003443 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_ENCRYPTED_EXTENSIONS);
Gabor Mezei7b39bf12022-05-24 16:04:14 +02003444 break;
3445#endif /* MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE */
3446
Jerry Yu69dd8d42022-04-16 12:51:26 +08003447 case MBEDTLS_SSL_SERVER_FINISHED:
Gilles Peskine449bd832023-01-11 14:50:10 +01003448 ret = ssl_tls13_write_server_finished(ssl);
Jerry Yu69dd8d42022-04-16 12:51:26 +08003449 break;
3450
Jerry Yu87b5ed42022-12-12 13:07:07 +08003451#if defined(MBEDTLS_SSL_EARLY_DATA)
3452 case MBEDTLS_SSL_END_OF_EARLY_DATA:
Jerry Yu59d420f2023-12-01 16:30:34 +08003453 ret = ssl_tls13_process_end_of_early_data(ssl);
Jerry Yu87b5ed42022-12-12 13:07:07 +08003454 break;
3455#endif /* MBEDTLS_SSL_EARLY_DATA */
3456
Jerry Yu69dd8d42022-04-16 12:51:26 +08003457 case MBEDTLS_SSL_CLIENT_FINISHED:
Gilles Peskine449bd832023-01-11 14:50:10 +01003458 ret = ssl_tls13_process_client_finished(ssl);
Jerry Yu69dd8d42022-04-16 12:51:26 +08003459 break;
3460
Jerry Yu69dd8d42022-04-16 12:51:26 +08003461 case MBEDTLS_SSL_HANDSHAKE_WRAPUP:
Gilles Peskine449bd832023-01-11 14:50:10 +01003462 ret = ssl_tls13_handshake_wrapup(ssl);
Jerry Yu69dd8d42022-04-16 12:51:26 +08003463 break;
3464
Ronald Cron766c0cd2022-10-18 12:17:11 +02003465#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED)
XiaokangQian6b916b12022-04-25 07:29:34 +00003466 case MBEDTLS_SSL_CLIENT_CERTIFICATE:
Gilles Peskine449bd832023-01-11 14:50:10 +01003467 ret = mbedtls_ssl_tls13_process_certificate(ssl);
3468 if (ret == 0) {
3469 if (ssl->session_negotiate->peer_cert != NULL) {
Ronald Cron209cae92022-06-07 10:30:19 +02003470 mbedtls_ssl_handshake_set_state(
Gilles Peskine449bd832023-01-11 14:50:10 +01003471 ssl, MBEDTLS_SSL_CLIENT_CERTIFICATE_VERIFY);
3472 } else {
3473 MBEDTLS_SSL_DEBUG_MSG(2, ("skip parse certificate verify"));
Ronald Cron209cae92022-06-07 10:30:19 +02003474 mbedtls_ssl_handshake_set_state(
Gilles Peskine449bd832023-01-11 14:50:10 +01003475 ssl, MBEDTLS_SSL_CLIENT_FINISHED);
Ronald Cron19385882022-06-15 16:26:13 +02003476 }
XiaokangQian6b916b12022-04-25 07:29:34 +00003477 }
3478 break;
3479
3480 case MBEDTLS_SSL_CLIENT_CERTIFICATE_VERIFY:
Gilles Peskine449bd832023-01-11 14:50:10 +01003481 ret = mbedtls_ssl_tls13_process_certificate_verify(ssl);
3482 if (ret == 0) {
XiaokangQian6b916b12022-04-25 07:29:34 +00003483 mbedtls_ssl_handshake_set_state(
Gilles Peskine449bd832023-01-11 14:50:10 +01003484 ssl, MBEDTLS_SSL_CLIENT_FINISHED);
XiaokangQian6b916b12022-04-25 07:29:34 +00003485 }
3486 break;
Ronald Cron766c0cd2022-10-18 12:17:11 +02003487#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED */
XiaokangQian6b916b12022-04-25 07:29:34 +00003488
Jerry Yue67bef42022-07-07 07:29:42 +00003489#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Jerry Yua8d3c502022-10-30 14:51:23 +08003490 case MBEDTLS_SSL_TLS1_3_NEW_SESSION_TICKET:
Gilles Peskine449bd832023-01-11 14:50:10 +01003491 ret = ssl_tls13_write_new_session_ticket(ssl);
3492 if (ret != 0) {
3493 MBEDTLS_SSL_DEBUG_RET(1,
3494 "ssl_tls13_write_new_session_ticket ",
3495 ret);
Jerry Yue67bef42022-07-07 07:29:42 +00003496 }
3497 break;
Jerry Yua8d3c502022-10-30 14:51:23 +08003498 case MBEDTLS_SSL_TLS1_3_NEW_SESSION_TICKET_FLUSH:
Jerry Yue67bef42022-07-07 07:29:42 +00003499 /* This state is necessary to do the flush of the New Session
Jerry Yua8d3c502022-10-30 14:51:23 +08003500 * Ticket message written in MBEDTLS_SSL_TLS1_3_NEW_SESSION_TICKET
Jerry Yue67bef42022-07-07 07:29:42 +00003501 * as part of ssl_prepare_handshake_step.
3502 */
3503 ret = 0;
Jerry Yud4e75002022-08-09 13:33:50 +08003504
Gilles Peskine449bd832023-01-11 14:50:10 +01003505 if (ssl->handshake->new_session_tickets_count == 0) {
3506 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_HANDSHAKE_OVER);
3507 } else {
Xiaokang Qian9f1747b2023-03-30 02:50:04 +00003508 mbedtls_ssl_handshake_set_state(
3509 ssl, MBEDTLS_SSL_TLS1_3_NEW_SESSION_TICKET);
Gilles Peskine449bd832023-01-11 14:50:10 +01003510 }
Jerry Yue67bef42022-07-07 07:29:42 +00003511 break;
3512
3513#endif /* MBEDTLS_SSL_SESSION_TICKETS */
3514
XiaokangQian7807f9f2022-02-15 10:04:37 +00003515 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01003516 MBEDTLS_SSL_DEBUG_MSG(1, ("invalid state %d", ssl->state));
3517 return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
XiaokangQian7807f9f2022-02-15 10:04:37 +00003518 }
3519
Gilles Peskine449bd832023-01-11 14:50:10 +01003520 return ret;
Jerry Yub9930e72021-08-06 17:11:51 +08003521}
Jerry Yu3cc4c2a2021-08-06 16:29:08 +08003522
Jerry Yufb4b6472022-01-27 15:03:26 +08003523#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_PROTO_TLS1_3 */