blob: 9aec2275b11d17df00aa46894baee220b109e745 [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 ssl->handshake->resume = 0;
313
Jerry Yu95699e72022-08-21 19:22:23 +0800314#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Ronald Cron7a30cf52024-02-23 14:38:59 +0100315 ret = ssl_tls13_offered_psks_check_identity_match_ticket(
316 ssl, identity, identity_len, obfuscated_ticket_age, session);
317 if (ret == SSL_TLS1_3_PSK_IDENTITY_MATCH) {
Jerry Yu95699e72022-08-21 19:22:23 +0800318 ssl->handshake->resume = 1;
319 *psk_type = MBEDTLS_SSL_TLS1_3_PSK_RESUMPTION;
Ronald Cron606671e2023-02-17 11:36:33 +0100320 ret = mbedtls_ssl_set_hs_psk(ssl,
321 session->resumption_key,
322 session->resumption_key_len);
323 if (ret != 0) {
324 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_set_hs_psk", ret);
325 return ret;
326 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100327
328 MBEDTLS_SSL_DEBUG_BUF(4, "Ticket-resumed PSK:",
329 session->resumption_key,
330 session->resumption_key_len);
331 MBEDTLS_SSL_DEBUG_MSG(4, ("ticket: obfuscated_ticket_age: %u",
332 (unsigned) obfuscated_ticket_age));
Ronald Cronfbae94a2023-12-05 18:15:14 +0100333 return SSL_TLS1_3_PSK_IDENTITY_MATCH;
Ronald Cron7a30cf52024-02-23 14:38:59 +0100334 } else if (ret == SSL_TLS1_3_PSK_IDENTITY_MATCH_BUT_PSK_NOT_USABLE) {
335 return SSL_TLS1_3_PSK_IDENTITY_MATCH_BUT_PSK_NOT_USABLE;
Jerry Yu95699e72022-08-21 19:22:23 +0800336 }
337#endif /* MBEDTLS_SSL_SESSION_TICKETS */
338
Jerry Yu1c105562022-07-10 06:32:38 +0000339 /* Check identity with external configured function */
Gilles Peskine449bd832023-01-11 14:50:10 +0100340 if (ssl->conf->f_psk != NULL) {
341 if (ssl->conf->f_psk(
342 ssl->conf->p_psk, ssl, identity, identity_len) == 0) {
Ronald Cronfbae94a2023-12-05 18:15:14 +0100343 return SSL_TLS1_3_PSK_IDENTITY_MATCH;
Jerry Yu1c105562022-07-10 06:32:38 +0000344 }
Ronald Cronfbae94a2023-12-05 18:15:14 +0100345 return SSL_TLS1_3_PSK_IDENTITY_DOES_NOT_MATCH;
Jerry Yu1c105562022-07-10 06:32:38 +0000346 }
347
Gilles Peskine449bd832023-01-11 14:50:10 +0100348 MBEDTLS_SSL_DEBUG_BUF(5, "identity", identity, identity_len);
Jerry Yu1c105562022-07-10 06:32:38 +0000349 /* Check identity with pre-configured psk */
Gilles Peskine449bd832023-01-11 14:50:10 +0100350 if (ssl->conf->psk_identity != NULL &&
Jerry Yu2f0abc92022-07-22 19:34:48 +0800351 identity_len == ssl->conf->psk_identity_len &&
Gilles Peskine449bd832023-01-11 14:50:10 +0100352 mbedtls_ct_memcmp(ssl->conf->psk_identity,
353 identity, identity_len) == 0) {
Ronald Cron606671e2023-02-17 11:36:33 +0100354 ret = mbedtls_ssl_set_hs_psk(ssl, ssl->conf->psk, ssl->conf->psk_len);
355 if (ret != 0) {
356 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_set_hs_psk", ret);
357 return ret;
358 }
Ronald Cronfbae94a2023-12-05 18:15:14 +0100359 return SSL_TLS1_3_PSK_IDENTITY_MATCH;
Jerry Yu1c105562022-07-10 06:32:38 +0000360 }
361
Ronald Cronfbae94a2023-12-05 18:15:14 +0100362 return SSL_TLS1_3_PSK_IDENTITY_DOES_NOT_MATCH;
Jerry Yu1c105562022-07-10 06:32:38 +0000363}
364
Ronald Cron6e311272023-12-05 17:57:01 +0100365#define SSL_TLS1_3_BINDER_DOES_NOT_MATCH 1
366#define SSL_TLS1_3_BINDER_MATCH 0
Jerry Yu6e74a7e2022-07-20 20:49:32 +0800367MBEDTLS_CHECK_RETURN_CRITICAL
Xiaokang Qian9f1747b2023-03-30 02:50:04 +0000368static int ssl_tls13_offered_psks_check_binder_match(
369 mbedtls_ssl_context *ssl,
370 const unsigned char *binder, size_t binder_len,
371 int psk_type, psa_algorithm_t psk_hash_alg)
Jerry Yu1c105562022-07-10 06:32:38 +0000372{
373 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yu96a2e362022-07-21 15:11:34 +0800374
Jerry Yudaf375a2022-07-20 21:31:43 +0800375 unsigned char transcript[PSA_HASH_MAX_SIZE];
Jerry Yu1c105562022-07-10 06:32:38 +0000376 size_t transcript_len;
Jerry Yu2f0abc92022-07-22 19:34:48 +0800377 unsigned char *psk;
Jerry Yu96a2e362022-07-21 15:11:34 +0800378 size_t psk_len;
Jerry Yudaf375a2022-07-20 21:31:43 +0800379 unsigned char server_computed_binder[PSA_HASH_MAX_SIZE];
Jerry Yu1c105562022-07-10 06:32:38 +0000380
Jerry Yu1c105562022-07-10 06:32:38 +0000381 /* Get current state of handshake transcript. */
Jerry Yu29d9faa2022-08-23 17:52:45 +0800382 ret = mbedtls_ssl_get_handshake_transcript(
Manuel Pégourié-Gonnard2d6d9932023-03-28 11:38:08 +0200383 ssl, mbedtls_md_type_from_psa_alg(psk_hash_alg),
Gilles Peskine449bd832023-01-11 14:50:10 +0100384 transcript, sizeof(transcript), &transcript_len);
385 if (ret != 0) {
386 return ret;
387 }
Jerry Yu1c105562022-07-10 06:32:38 +0000388
Gilles Peskine449bd832023-01-11 14:50:10 +0100389 ret = mbedtls_ssl_tls13_export_handshake_psk(ssl, &psk, &psk_len);
390 if (ret != 0) {
391 return ret;
392 }
Jerry Yu96a2e362022-07-21 15:11:34 +0800393
Gilles Peskine449bd832023-01-11 14:50:10 +0100394 ret = mbedtls_ssl_tls13_create_psk_binder(ssl, psk_hash_alg,
395 psk, psk_len, psk_type,
396 transcript,
397 server_computed_binder);
Jerry Yu96a2e362022-07-21 15:11:34 +0800398#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +0100399 mbedtls_free((void *) psk);
Jerry Yu96a2e362022-07-21 15:11:34 +0800400#endif
Gilles Peskine449bd832023-01-11 14:50:10 +0100401 if (ret != 0) {
402 MBEDTLS_SSL_DEBUG_MSG(1, ("PSK binder calculation failed."));
403 return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
Jerry Yu1c105562022-07-10 06:32:38 +0000404 }
405
Gilles Peskine449bd832023-01-11 14:50:10 +0100406 MBEDTLS_SSL_DEBUG_BUF(3, "psk binder ( computed ): ",
407 server_computed_binder, transcript_len);
408 MBEDTLS_SSL_DEBUG_BUF(3, "psk binder ( received ): ", binder, binder_len);
Jerry Yu1c105562022-07-10 06:32:38 +0000409
Gilles Peskine449bd832023-01-11 14:50:10 +0100410 if (mbedtls_ct_memcmp(server_computed_binder, binder, binder_len) == 0) {
Ronald Cron6e311272023-12-05 17:57:01 +0100411 return SSL_TLS1_3_BINDER_MATCH;
Jerry Yu1c105562022-07-10 06:32:38 +0000412 }
413
Gilles Peskine449bd832023-01-11 14:50:10 +0100414 mbedtls_platform_zeroize(server_computed_binder,
415 sizeof(server_computed_binder));
Ronald Cron6e311272023-12-05 17:57:01 +0100416 return SSL_TLS1_3_BINDER_DOES_NOT_MATCH;
Jerry Yu1c105562022-07-10 06:32:38 +0000417}
Jerry Yu96a2e362022-07-21 15:11:34 +0800418
Jerry Yu82534862022-08-30 10:42:33 +0800419#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Jerry Yuf35ba382022-08-23 17:58:26 +0800420MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100421static int ssl_tls13_session_copy_ticket(mbedtls_ssl_session *dst,
422 const mbedtls_ssl_session *src)
Jerry Yu82534862022-08-30 10:42:33 +0800423{
Jerry Yu82534862022-08-30 10:42:33 +0800424 dst->ticket_age_add = src->ticket_age_add;
425 dst->ticket_flags = src->ticket_flags;
426 dst->resumption_key_len = src->resumption_key_len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100427 if (src->resumption_key_len == 0) {
428 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
429 }
430 memcpy(dst->resumption_key, src->resumption_key, src->resumption_key_len);
Jerry Yu4746b102022-09-13 11:11:48 +0800431
Jerry Yu33bf2402022-12-12 16:01:43 +0800432#if defined(MBEDTLS_SSL_EARLY_DATA)
433 dst->max_early_data_size = src->max_early_data_size;
434#endif
435
Gilles Peskine449bd832023-01-11 14:50:10 +0100436 return 0;
Jerry Yu82534862022-08-30 10:42:33 +0800437}
438#endif /* MBEDTLS_SSL_SESSION_TICKETS */
439
Jerry Yu1c105562022-07-10 06:32:38 +0000440/* Parser for pre_shared_key extension in client hello
441 * struct {
442 * opaque identity<1..2^16-1>;
443 * uint32 obfuscated_ticket_age;
444 * } PskIdentity;
445 *
446 * opaque PskBinderEntry<32..255>;
447 *
448 * struct {
449 * PskIdentity identities<7..2^16-1>;
450 * PskBinderEntry binders<33..2^16-1>;
451 * } OfferedPsks;
452 *
453 * struct {
454 * select (Handshake.msg_type) {
455 * case client_hello: OfferedPsks;
456 * ....
457 * };
458 * } PreSharedKeyExtension;
459 */
Jerry Yu6e74a7e2022-07-20 20:49:32 +0800460MBEDTLS_CHECK_RETURN_CRITICAL
Xiaokang Qian9f1747b2023-03-30 02:50:04 +0000461static int ssl_tls13_parse_pre_shared_key_ext(
462 mbedtls_ssl_context *ssl,
463 const unsigned char *pre_shared_key_ext,
464 const unsigned char *pre_shared_key_ext_end,
465 const unsigned char *ciphersuites,
466 const unsigned char *ciphersuites_end)
Jerry Yu1c105562022-07-10 06:32:38 +0000467{
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +0100468 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yu29d9faa2022-08-23 17:52:45 +0800469 const unsigned char *identities = pre_shared_key_ext;
Jerry Yu568ec252022-07-22 21:27:34 +0800470 const unsigned char *p_identity_len;
471 size_t identities_len;
Jerry Yu1c105562022-07-10 06:32:38 +0000472 const unsigned char *identities_end;
Jerry Yu568ec252022-07-22 21:27:34 +0800473 const unsigned char *binders;
474 const unsigned char *p_binder_len;
475 size_t binders_len;
Jerry Yu1c105562022-07-10 06:32:38 +0000476 const unsigned char *binders_end;
Jerry Yu96a2e362022-07-21 15:11:34 +0800477 int matched_identity = -1;
478 int identity_id = -1;
Jerry Yu1c105562022-07-10 06:32:38 +0000479
Gilles Peskine449bd832023-01-11 14:50:10 +0100480 MBEDTLS_SSL_DEBUG_BUF(3, "pre_shared_key extension",
481 pre_shared_key_ext,
482 pre_shared_key_ext_end - pre_shared_key_ext);
Jerry Yu1c105562022-07-10 06:32:38 +0000483
Jerry Yu96a2e362022-07-21 15:11:34 +0800484 /* identities_len 2 bytes
485 * identities_data >= 7 bytes
486 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100487 MBEDTLS_SSL_CHK_BUF_READ_PTR(identities, pre_shared_key_ext_end, 7 + 2);
488 identities_len = MBEDTLS_GET_UINT16_BE(identities, 0);
Jerry Yu568ec252022-07-22 21:27:34 +0800489 p_identity_len = identities + 2;
Gilles Peskine449bd832023-01-11 14:50:10 +0100490 MBEDTLS_SSL_CHK_BUF_READ_PTR(p_identity_len, pre_shared_key_ext_end,
491 identities_len);
Jerry Yu568ec252022-07-22 21:27:34 +0800492 identities_end = p_identity_len + identities_len;
Jerry Yu1c105562022-07-10 06:32:38 +0000493
Jerry Yu96a2e362022-07-21 15:11:34 +0800494 /* binders_len 2 bytes
495 * binders >= 33 bytes
496 */
Jerry Yu568ec252022-07-22 21:27:34 +0800497 binders = identities_end;
Gilles Peskine449bd832023-01-11 14:50:10 +0100498 MBEDTLS_SSL_CHK_BUF_READ_PTR(binders, pre_shared_key_ext_end, 33 + 2);
499 binders_len = MBEDTLS_GET_UINT16_BE(binders, 0);
Jerry Yu568ec252022-07-22 21:27:34 +0800500 p_binder_len = binders + 2;
Gilles Peskine449bd832023-01-11 14:50:10 +0100501 MBEDTLS_SSL_CHK_BUF_READ_PTR(p_binder_len, pre_shared_key_ext_end, binders_len);
Jerry Yu568ec252022-07-22 21:27:34 +0800502 binders_end = p_binder_len + binders_len;
Jerry Yu1c105562022-07-10 06:32:38 +0000503
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +0100504 ret = ssl->handshake->update_checksum(ssl, pre_shared_key_ext,
505 identities_end - pre_shared_key_ext);
506 if (0 != ret) {
507 MBEDTLS_SSL_DEBUG_RET(1, ("update_checksum"), ret);
508 return ret;
509 }
Jerry Yu96a2e362022-07-21 15:11:34 +0800510
Gilles Peskine449bd832023-01-11 14:50:10 +0100511 while (p_identity_len < identities_end && p_binder_len < binders_end) {
Jerry Yu1c105562022-07-10 06:32:38 +0000512 const unsigned char *identity;
Jerry Yu568ec252022-07-22 21:27:34 +0800513 size_t identity_len;
Jerry Yu82534862022-08-30 10:42:33 +0800514 uint32_t obfuscated_ticket_age;
Jerry Yu96a2e362022-07-21 15:11:34 +0800515 const unsigned char *binder;
Jerry Yu568ec252022-07-22 21:27:34 +0800516 size_t binder_len;
Jerry Yu5725f1c2022-08-21 17:27:16 +0800517 int psk_type;
Ronald Cron89089cc2024-02-14 18:18:08 +0100518 int psk_ciphersuite_id;
519 psa_algorithm_t psk_hash_alg;
Ronald Croncf284562024-02-16 18:54:10 +0100520 int allowed_key_exchange_modes;
521 int key_exchange_mode = MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_NONE;
Jerry Yu29d9faa2022-08-23 17:52:45 +0800522 const mbedtls_ssl_ciphersuite_t *ciphersuite_info;
Jerry Yu82534862022-08-30 10:42:33 +0800523#if defined(MBEDTLS_SSL_SESSION_TICKETS)
524 mbedtls_ssl_session session;
Gilles Peskine449bd832023-01-11 14:50:10 +0100525 mbedtls_ssl_session_init(&session);
Jerry Yu82534862022-08-30 10:42:33 +0800526#endif
Jerry Yubb852022022-07-20 21:10:44 +0800527
Gilles Peskine449bd832023-01-11 14:50:10 +0100528 MBEDTLS_SSL_CHK_BUF_READ_PTR(p_identity_len, identities_end, 2 + 1 + 4);
529 identity_len = MBEDTLS_GET_UINT16_BE(p_identity_len, 0);
Jerry Yu568ec252022-07-22 21:27:34 +0800530 identity = p_identity_len + 2;
Gilles Peskine449bd832023-01-11 14:50:10 +0100531 MBEDTLS_SSL_CHK_BUF_READ_PTR(identity, identities_end, identity_len + 4);
532 obfuscated_ticket_age = MBEDTLS_GET_UINT32_BE(identity, identity_len);
Jerry Yu568ec252022-07-22 21:27:34 +0800533 p_identity_len += identity_len + 6;
Jerry Yu1c105562022-07-10 06:32:38 +0000534
Gilles Peskine449bd832023-01-11 14:50:10 +0100535 MBEDTLS_SSL_CHK_BUF_READ_PTR(p_binder_len, binders_end, 1 + 32);
Jerry Yu568ec252022-07-22 21:27:34 +0800536 binder_len = *p_binder_len;
537 binder = p_binder_len + 1;
Gilles Peskine449bd832023-01-11 14:50:10 +0100538 MBEDTLS_SSL_CHK_BUF_READ_PTR(binder, binders_end, binder_len);
Jerry Yu568ec252022-07-22 21:27:34 +0800539 p_binder_len += binder_len + 1;
Jerry Yu96a2e362022-07-21 15:11:34 +0800540
Jerry Yu96a2e362022-07-21 15:11:34 +0800541 identity_id++;
Gilles Peskine449bd832023-01-11 14:50:10 +0100542 if (matched_identity != -1) {
Jerry Yu1c105562022-07-10 06:32:38 +0000543 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +0100544 }
Jerry Yu1c105562022-07-10 06:32:38 +0000545
Jerry Yu96a2e362022-07-21 15:11:34 +0800546 ret = ssl_tls13_offered_psks_check_identity_match(
Gilles Peskine449bd832023-01-11 14:50:10 +0100547 ssl, identity, identity_len, obfuscated_ticket_age,
548 &psk_type, &session);
Ronald Cronfbae94a2023-12-05 18:15:14 +0100549 if (ret != SSL_TLS1_3_PSK_IDENTITY_MATCH) {
Jerry Yu96a2e362022-07-21 15:11:34 +0800550 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +0100551 }
Jerry Yu96a2e362022-07-21 15:11:34 +0800552
Gilles Peskine449bd832023-01-11 14:50:10 +0100553 MBEDTLS_SSL_DEBUG_MSG(4, ("found matched identity"));
Ronald Cron89089cc2024-02-14 18:18:08 +0100554
Gilles Peskine449bd832023-01-11 14:50:10 +0100555 switch (psk_type) {
Jerry Yu0baf9072022-08-25 11:21:04 +0800556 case MBEDTLS_SSL_TLS1_3_PSK_EXTERNAL:
Ronald Cron89089cc2024-02-14 18:18:08 +0100557 psk_ciphersuite_id = 0;
558 psk_hash_alg = PSA_ALG_SHA_256;
Ronald Croncf284562024-02-16 18:54:10 +0100559 allowed_key_exchange_modes =
560 MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK_ALL;
Jerry Yu0baf9072022-08-25 11:21:04 +0800561 break;
Jerry Yu82534862022-08-30 10:42:33 +0800562#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Ronald Cronf7e99162024-02-14 16:04:02 +0100563 case MBEDTLS_SSL_TLS1_3_PSK_RESUMPTION:
Ronald Cron89089cc2024-02-14 18:18:08 +0100564 psk_ciphersuite_id = session.ciphersuite;
565 psk_hash_alg = PSA_ALG_NONE;
Ronald Croncf284562024-02-16 18:54:10 +0100566 ssl->session_negotiate->ticket_flags = session.ticket_flags;
567 allowed_key_exchange_modes =
568 session.ticket_flags &
569 MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK_ALL;
Jerry Yu0baf9072022-08-25 11:21:04 +0800570 break;
Ronald Cronf7e99162024-02-14 16:04:02 +0100571#endif
Jerry Yu0baf9072022-08-25 11:21:04 +0800572 default:
Gilles Peskine449bd832023-01-11 14:50:10 +0100573 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Jerry Yu0baf9072022-08-25 11:21:04 +0800574 }
Ronald Cron89089cc2024-02-14 18:18:08 +0100575
Ronald Croncf284562024-02-16 18:54:10 +0100576 if ((allowed_key_exchange_modes &
577 MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK_EPHEMERAL) &&
578 ssl_tls13_key_exchange_is_psk_ephemeral_available(ssl)) {
579 key_exchange_mode = MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK_EPHEMERAL;
580 } else if ((allowed_key_exchange_modes &
581 MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK) &&
582 ssl_tls13_key_exchange_is_psk_available(ssl)) {
583 key_exchange_mode = MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK;
584 }
585
586 if (key_exchange_mode == MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_NONE) {
587 MBEDTLS_SSL_DEBUG_MSG(3, ("No suitable PSK key exchange mode"));
588 continue;
589 }
590
Ronald Cron89089cc2024-02-14 18:18:08 +0100591 ssl_tls13_select_ciphersuite(ssl, ciphersuites, ciphersuites_end,
592 psk_ciphersuite_id, psk_hash_alg,
593 &ciphersuite_info);
594
595 if (ciphersuite_info == NULL) {
596#if defined(MBEDTLS_SSL_SESSION_TICKETS)
597 mbedtls_ssl_session_free(&session);
598#endif
599 /*
600 * We consider finding a ciphersuite suitable for the PSK as part
601 * of the validation of its binder. Thus if we do not find one, we
602 * abort the handshake with a decrypt_error alert.
603 */
Jerry Yuf35ba382022-08-23 17:58:26 +0800604 MBEDTLS_SSL_PEND_FATAL_ALERT(
605 MBEDTLS_SSL_ALERT_MSG_DECRYPT_ERROR,
Gilles Peskine449bd832023-01-11 14:50:10 +0100606 MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE);
Ronald Cron89089cc2024-02-14 18:18:08 +0100607 return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
Jerry Yu5725f1c2022-08-21 17:27:16 +0800608 }
609
Jerry Yu96a2e362022-07-21 15:11:34 +0800610 ret = ssl_tls13_offered_psks_check_binder_match(
Gilles Peskine449bd832023-01-11 14:50:10 +0100611 ssl, binder, binder_len, psk_type,
Dave Rodgman2eab4622023-10-05 13:30:37 +0100612 mbedtls_md_psa_alg_from_type((mbedtls_md_type_t) ciphersuite_info->mac));
Ronald Cron6e311272023-12-05 17:57:01 +0100613 if (ret != SSL_TLS1_3_BINDER_MATCH) {
Jerry Yuc5a23a02022-08-25 10:51:44 +0800614 /* For security reasons, the handshake should be aborted when we
615 * fail to validate a binder value. See RFC 8446 section 4.2.11.2
616 * and appendix E.6. */
Jerry Yu82534862022-08-30 10:42:33 +0800617#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Gilles Peskine449bd832023-01-11 14:50:10 +0100618 mbedtls_ssl_session_free(&session);
Jerry Yu82534862022-08-30 10:42:33 +0800619#endif
Gilles Peskine449bd832023-01-11 14:50:10 +0100620 MBEDTLS_SSL_DEBUG_MSG(3, ("Invalid binder."));
Xiaokang Qian9f1747b2023-03-30 02:50:04 +0000621 MBEDTLS_SSL_DEBUG_RET(
622 1, "ssl_tls13_offered_psks_check_binder_match", ret);
Jerry Yu96a2e362022-07-21 15:11:34 +0800623 MBEDTLS_SSL_PEND_FATAL_ALERT(
624 MBEDTLS_SSL_ALERT_MSG_DECRYPT_ERROR,
Gilles Peskine449bd832023-01-11 14:50:10 +0100625 MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE);
626 return ret;
Jerry Yu96a2e362022-07-21 15:11:34 +0800627 }
Jerry Yu96a2e362022-07-21 15:11:34 +0800628
629 matched_identity = identity_id;
Jerry Yu5725f1c2022-08-21 17:27:16 +0800630
631 /* Update handshake parameters */
Jerry Yue5834fd2022-08-29 20:16:09 +0800632 ssl->handshake->ciphersuite_info = ciphersuite_info;
Ronald Cron89089cc2024-02-14 18:18:08 +0100633 ssl->session_negotiate->ciphersuite = ciphersuite_info->id;
Gilles Peskine449bd832023-01-11 14:50:10 +0100634 MBEDTLS_SSL_DEBUG_MSG(2, ("overwrite ciphersuite: %04x - %s",
Ronald Cron89089cc2024-02-14 18:18:08 +0100635 ((unsigned) ciphersuite_info->id),
636 ciphersuite_info->name));
Jerry Yu82534862022-08-30 10:42:33 +0800637#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Gilles Peskine449bd832023-01-11 14:50:10 +0100638 if (psk_type == MBEDTLS_SSL_TLS1_3_PSK_RESUMPTION) {
639 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 Lvd72e8582023-11-10 10:37:18 +0800991static int ssl_tls13_ticket_is_kex_mode_permitted(mbedtls_ssl_context *ssl,
992 unsigned int kex_mode)
Pengyu Lv306a01d2023-02-02 16:35:47 +0800993{
994#if defined(MBEDTLS_SSL_SESSION_TICKETS)
995 if (ssl->handshake->resume) {
Pengyu Lv94a42cc2023-12-06 10:04:17 +0800996 if (!mbedtls_ssl_tls13_session_ticket_has_flags(
Pengyu Lv306a01d2023-02-02 16:35:47 +0800997 ssl->session_negotiate, kex_mode)) {
998 return 0;
999 }
1000 }
1001#else
1002 ((void) ssl);
1003 ((void) kex_mode);
1004#endif
1005 return 1;
1006}
1007#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED */
1008
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001009MBEDTLS_CHECK_RETURN_CRITICAL
Pengyu Lvbc4aab72023-12-01 15:37:24 +08001010static int ssl_tls13_key_exchange_is_ephemeral_available(mbedtls_ssl_context *ssl)
XiaokangQian7807f9f2022-02-15 10:04:37 +00001011{
Jerry Yue5991322022-11-07 14:03:44 +08001012#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED)
Pengyu Lv0a1ff2b2023-11-14 11:03:32 +08001013 return mbedtls_ssl_conf_tls13_is_ephemeral_enabled(ssl) &&
Gilles Peskine449bd832023-01-11 14:50:10 +01001014 ssl_tls13_client_hello_has_exts_for_ephemeral_key_exchange(ssl);
Jerry Yue5991322022-11-07 14:03:44 +08001015#else
1016 ((void) ssl);
Gilles Peskine449bd832023-01-11 14:50:10 +01001017 return 0;
Jerry Yue5991322022-11-07 14:03:44 +08001018#endif
Jerry Yu77f01482022-07-11 07:03:24 +00001019}
XiaokangQian7807f9f2022-02-15 10:04:37 +00001020
Jerry Yu77f01482022-07-11 07:03:24 +00001021MBEDTLS_CHECK_RETURN_CRITICAL
Pengyu Lvbc4aab72023-12-01 15:37:24 +08001022static int ssl_tls13_key_exchange_is_psk_available(mbedtls_ssl_context *ssl)
Jerry Yu77f01482022-07-11 07:03:24 +00001023{
Jerry Yue5991322022-11-07 14:03:44 +08001024#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK_ENABLED)
Pengyu Lvd72e8582023-11-10 10:37:18 +08001025 return ssl_tls13_ticket_is_kex_mode_permitted(
Pengyu Lv306a01d2023-02-02 16:35:47 +08001026 ssl, MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK) &&
Pengyu Lv0a1ff2b2023-11-14 11:03:32 +08001027 mbedtls_ssl_conf_tls13_is_psk_enabled(ssl) &&
Pengyu Lvb2cfafb2023-11-14 13:56:13 +08001028 mbedtls_ssl_tls13_is_psk_supported(ssl) &&
Gilles Peskine449bd832023-01-11 14:50:10 +01001029 ssl_tls13_client_hello_has_exts_for_psk_key_exchange(ssl);
Jerry Yu77f01482022-07-11 07:03:24 +00001030#else
1031 ((void) ssl);
Gilles Peskine449bd832023-01-11 14:50:10 +01001032 return 0;
Jerry Yu77f01482022-07-11 07:03:24 +00001033#endif
1034}
XiaokangQian7807f9f2022-02-15 10:04:37 +00001035
Jerry Yu77f01482022-07-11 07:03:24 +00001036MBEDTLS_CHECK_RETURN_CRITICAL
Pengyu Lvbc4aab72023-12-01 15:37:24 +08001037static int ssl_tls13_key_exchange_is_psk_ephemeral_available(mbedtls_ssl_context *ssl)
Jerry Yu77f01482022-07-11 07:03:24 +00001038{
Jerry Yue5991322022-11-07 14:03:44 +08001039#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK_EPHEMERAL_ENABLED)
Pengyu Lvd72e8582023-11-10 10:37:18 +08001040 return ssl_tls13_ticket_is_kex_mode_permitted(
Pengyu Lv306a01d2023-02-02 16:35:47 +08001041 ssl, MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK_EPHEMERAL) &&
Pengyu Lv0a1ff2b2023-11-14 11:03:32 +08001042 mbedtls_ssl_conf_tls13_is_psk_ephemeral_enabled(ssl) &&
Pengyu Lvb2cfafb2023-11-14 13:56:13 +08001043 mbedtls_ssl_tls13_is_psk_ephemeral_supported(ssl) &&
Gilles Peskine449bd832023-01-11 14:50:10 +01001044 ssl_tls13_client_hello_has_exts_for_psk_ephemeral_key_exchange(ssl);
Jerry Yu77f01482022-07-11 07:03:24 +00001045#else
1046 ((void) ssl);
Gilles Peskine449bd832023-01-11 14:50:10 +01001047 return 0;
Jerry Yu77f01482022-07-11 07:03:24 +00001048#endif
1049}
1050
Gilles Peskine449bd832023-01-11 14:50:10 +01001051static int ssl_tls13_determine_key_exchange_mode(mbedtls_ssl_context *ssl)
Jerry Yu77f01482022-07-11 07:03:24 +00001052{
1053 /*
1054 * Determine the key exchange algorithm to use.
1055 * There are three types of key exchanges supported in TLS 1.3:
1056 * - (EC)DH with ECDSA,
1057 * - (EC)DH with PSK,
1058 * - plain PSK.
1059 *
1060 * The PSK-based key exchanges may additionally be used with 0-RTT.
1061 *
1062 * Our built-in order of preference is
Jerry Yuce6ed702022-07-22 21:49:53 +08001063 * 1 ) (EC)DHE-PSK Mode ( psk_ephemeral )
1064 * 2 ) Certificate Mode ( ephemeral )
1065 * 3 ) Plain PSK Mode ( psk )
Jerry Yu77f01482022-07-11 07:03:24 +00001066 */
1067
Xiaokang Qian9f1747b2023-03-30 02:50:04 +00001068 ssl->handshake->key_exchange_mode =
1069 MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_NONE;
Jerry Yu77f01482022-07-11 07:03:24 +00001070
Pengyu Lvbc4aab72023-12-01 15:37:24 +08001071 if (ssl_tls13_key_exchange_is_psk_ephemeral_available(ssl)) {
Jerry Yu77f01482022-07-11 07:03:24 +00001072 ssl->handshake->key_exchange_mode =
1073 MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK_EPHEMERAL;
Gilles Peskine449bd832023-01-11 14:50:10 +01001074 MBEDTLS_SSL_DEBUG_MSG(2, ("key exchange mode: psk_ephemeral"));
1075 } else
Pengyu Lvbc4aab72023-12-01 15:37:24 +08001076 if (ssl_tls13_key_exchange_is_ephemeral_available(ssl)) {
Ronald Cron1f63fe42024-02-23 15:49:12 +01001077 ssl->handshake->resume = 0;
Jerry Yu77f01482022-07-11 07:03:24 +00001078 ssl->handshake->key_exchange_mode =
1079 MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL;
Gilles Peskine449bd832023-01-11 14:50:10 +01001080 MBEDTLS_SSL_DEBUG_MSG(2, ("key exchange mode: ephemeral"));
1081 } else
Pengyu Lvbc4aab72023-12-01 15:37:24 +08001082 if (ssl_tls13_key_exchange_is_psk_available(ssl)) {
Jerry Yuce6ed702022-07-22 21:49:53 +08001083 ssl->handshake->key_exchange_mode =
1084 MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK;
Gilles Peskine449bd832023-01-11 14:50:10 +01001085 MBEDTLS_SSL_DEBUG_MSG(2, ("key exchange mode: psk"));
1086 } else {
Jerry Yu77f01482022-07-11 07:03:24 +00001087 MBEDTLS_SSL_DEBUG_MSG(
Gilles Peskine449bd832023-01-11 14:50:10 +01001088 1,
1089 ("ClientHello message misses mandatory extensions."));
1090 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_MISSING_EXTENSION,
1091 MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER);
1092 return MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER;
Jerry Yu77f01482022-07-11 07:03:24 +00001093 }
1094
Gilles Peskine449bd832023-01-11 14:50:10 +01001095 return 0;
Jerry Yu77f01482022-07-11 07:03:24 +00001096
XiaokangQian7807f9f2022-02-15 10:04:37 +00001097}
1098
XiaokangQian81802f42022-06-10 13:25:22 +00001099#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
Ronald Cron928cbd32022-10-04 16:14:26 +02001100 defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED)
Ronald Cron67ea2542022-09-15 17:34:42 +02001101
1102#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01001103static psa_algorithm_t ssl_tls13_iana_sig_alg_to_psa_alg(uint16_t sig_alg)
Ronald Cron67ea2542022-09-15 17:34:42 +02001104{
Gilles Peskine449bd832023-01-11 14:50:10 +01001105 switch (sig_alg) {
Ronald Cron67ea2542022-09-15 17:34:42 +02001106 case MBEDTLS_TLS1_3_SIG_ECDSA_SECP256R1_SHA256:
Gilles Peskine449bd832023-01-11 14:50:10 +01001107 return PSA_ALG_ECDSA(PSA_ALG_SHA_256);
Ronald Cron67ea2542022-09-15 17:34:42 +02001108 case MBEDTLS_TLS1_3_SIG_ECDSA_SECP384R1_SHA384:
Gilles Peskine449bd832023-01-11 14:50:10 +01001109 return PSA_ALG_ECDSA(PSA_ALG_SHA_384);
Ronald Cron67ea2542022-09-15 17:34:42 +02001110 case MBEDTLS_TLS1_3_SIG_ECDSA_SECP521R1_SHA512:
Gilles Peskine449bd832023-01-11 14:50:10 +01001111 return PSA_ALG_ECDSA(PSA_ALG_SHA_512);
Ronald Cron67ea2542022-09-15 17:34:42 +02001112 case MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA256:
Gilles Peskine449bd832023-01-11 14:50:10 +01001113 return PSA_ALG_RSA_PSS(PSA_ALG_SHA_256);
Ronald Cron67ea2542022-09-15 17:34:42 +02001114 case MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA384:
Gilles Peskine449bd832023-01-11 14:50:10 +01001115 return PSA_ALG_RSA_PSS(PSA_ALG_SHA_384);
Ronald Cron67ea2542022-09-15 17:34:42 +02001116 case MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA512:
Gilles Peskine449bd832023-01-11 14:50:10 +01001117 return PSA_ALG_RSA_PSS(PSA_ALG_SHA_512);
Ronald Cron67ea2542022-09-15 17:34:42 +02001118 case MBEDTLS_TLS1_3_SIG_RSA_PKCS1_SHA256:
Gilles Peskine449bd832023-01-11 14:50:10 +01001119 return PSA_ALG_RSA_PKCS1V15_SIGN(PSA_ALG_SHA_256);
Ronald Cron67ea2542022-09-15 17:34:42 +02001120 case MBEDTLS_TLS1_3_SIG_RSA_PKCS1_SHA384:
Gilles Peskine449bd832023-01-11 14:50:10 +01001121 return PSA_ALG_RSA_PKCS1V15_SIGN(PSA_ALG_SHA_384);
Ronald Cron67ea2542022-09-15 17:34:42 +02001122 case MBEDTLS_TLS1_3_SIG_RSA_PKCS1_SHA512:
Gilles Peskine449bd832023-01-11 14:50:10 +01001123 return PSA_ALG_RSA_PKCS1V15_SIGN(PSA_ALG_SHA_512);
Ronald Cron67ea2542022-09-15 17:34:42 +02001124 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01001125 return PSA_ALG_NONE;
Ronald Cron67ea2542022-09-15 17:34:42 +02001126 }
1127}
1128#endif /* MBEDTLS_USE_PSA_CRYPTO */
1129
XiaokangQian23c5be62022-06-07 02:04:34 +00001130/*
XiaokangQianfb665a82022-06-15 03:57:21 +00001131 * Pick best ( private key, certificate chain ) pair based on the signature
1132 * algorithms supported by the client.
XiaokangQian23c5be62022-06-07 02:04:34 +00001133 */
Ronald Cronce7d76e2022-07-08 18:56:49 +02001134MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01001135static int ssl_tls13_pick_key_cert(mbedtls_ssl_context *ssl)
XiaokangQian23c5be62022-06-07 02:04:34 +00001136{
XiaokangQianfb665a82022-06-15 03:57:21 +00001137 mbedtls_ssl_key_cert *key_cert, *key_cert_list;
XiaokangQian81802f42022-06-10 13:25:22 +00001138 const uint16_t *sig_alg = ssl->handshake->received_sig_algs;
XiaokangQian23c5be62022-06-07 02:04:34 +00001139
1140#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Gilles Peskine449bd832023-01-11 14:50:10 +01001141 if (ssl->handshake->sni_key_cert != NULL) {
XiaokangQianfb665a82022-06-15 03:57:21 +00001142 key_cert_list = ssl->handshake->sni_key_cert;
Gilles Peskine449bd832023-01-11 14:50:10 +01001143 } else
XiaokangQian23c5be62022-06-07 02:04:34 +00001144#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
Gilles Peskine449bd832023-01-11 14:50:10 +01001145 key_cert_list = ssl->conf->key_cert;
XiaokangQian23c5be62022-06-07 02:04:34 +00001146
Gilles Peskine449bd832023-01-11 14:50:10 +01001147 if (key_cert_list == NULL) {
1148 MBEDTLS_SSL_DEBUG_MSG(3, ("server has no certificate"));
1149 return -1;
XiaokangQian23c5be62022-06-07 02:04:34 +00001150 }
1151
Gilles Peskine449bd832023-01-11 14:50:10 +01001152 for (; *sig_alg != MBEDTLS_TLS1_3_SIG_NONE; sig_alg++) {
1153 if (!mbedtls_ssl_sig_alg_is_offered(ssl, *sig_alg)) {
Ronald Cron67ea2542022-09-15 17:34:42 +02001154 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +01001155 }
Ronald Cron67ea2542022-09-15 17:34:42 +02001156
Gilles Peskine449bd832023-01-11 14:50:10 +01001157 if (!mbedtls_ssl_tls13_sig_alg_for_cert_verify_is_supported(*sig_alg)) {
Ronald Cron67ea2542022-09-15 17:34:42 +02001158 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +01001159 }
Ronald Cron67ea2542022-09-15 17:34:42 +02001160
Gilles Peskine449bd832023-01-11 14:50:10 +01001161 for (key_cert = key_cert_list; key_cert != NULL;
1162 key_cert = key_cert->next) {
Ronald Cron67ea2542022-09-15 17:34:42 +02001163#if defined(MBEDTLS_USE_PSA_CRYPTO)
1164 psa_algorithm_t psa_alg = PSA_ALG_NONE;
1165#endif /* MBEDTLS_USE_PSA_CRYPTO */
1166
Gilles Peskine449bd832023-01-11 14:50:10 +01001167 MBEDTLS_SSL_DEBUG_CRT(3, "certificate (chain) candidate",
1168 key_cert->cert);
XiaokangQian81802f42022-06-10 13:25:22 +00001169
1170 /*
Gilles Peskine449bd832023-01-11 14:50:10 +01001171 * This avoids sending the client a cert it'll reject based on
1172 * keyUsage or other extensions.
1173 */
1174 if (mbedtls_x509_crt_check_key_usage(
1175 key_cert->cert, MBEDTLS_X509_KU_DIGITAL_SIGNATURE) != 0 ||
XiaokangQian81802f42022-06-10 13:25:22 +00001176 mbedtls_x509_crt_check_extended_key_usage(
XiaokangQianfb665a82022-06-15 03:57:21 +00001177 key_cert->cert, MBEDTLS_OID_SERVER_AUTH,
Gilles Peskine449bd832023-01-11 14:50:10 +01001178 MBEDTLS_OID_SIZE(MBEDTLS_OID_SERVER_AUTH)) != 0) {
1179 MBEDTLS_SSL_DEBUG_MSG(3, ("certificate mismatch: "
1180 "(extended) key usage extension"));
XiaokangQian81802f42022-06-10 13:25:22 +00001181 continue;
1182 }
XiaokangQian23c5be62022-06-07 02:04:34 +00001183
Gilles Peskine449bd832023-01-11 14:50:10 +01001184 MBEDTLS_SSL_DEBUG_MSG(3,
1185 ("ssl_tls13_pick_key_cert:"
1186 "check signature algorithm %s [%04x]",
1187 mbedtls_ssl_sig_alg_to_str(*sig_alg),
1188 *sig_alg));
Ronald Cron67ea2542022-09-15 17:34:42 +02001189#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01001190 psa_alg = ssl_tls13_iana_sig_alg_to_psa_alg(*sig_alg);
Ronald Cron67ea2542022-09-15 17:34:42 +02001191#endif /* MBEDTLS_USE_PSA_CRYPTO */
1192
Gilles Peskine449bd832023-01-11 14:50:10 +01001193 if (mbedtls_ssl_tls13_check_sig_alg_cert_key_match(
1194 *sig_alg, &key_cert->cert->pk)
Ronald Cron67ea2542022-09-15 17:34:42 +02001195#if defined(MBEDTLS_USE_PSA_CRYPTO)
1196 && psa_alg != PSA_ALG_NONE &&
Gilles Peskine449bd832023-01-11 14:50:10 +01001197 mbedtls_pk_can_do_ext(&key_cert->cert->pk, psa_alg,
1198 PSA_KEY_USAGE_SIGN_HASH) == 1
Ronald Cron67ea2542022-09-15 17:34:42 +02001199#endif /* MBEDTLS_USE_PSA_CRYPTO */
Gilles Peskine449bd832023-01-11 14:50:10 +01001200 ) {
XiaokangQianfb665a82022-06-15 03:57:21 +00001201 ssl->handshake->key_cert = key_cert;
Gilles Peskine449bd832023-01-11 14:50:10 +01001202 MBEDTLS_SSL_DEBUG_MSG(3,
1203 ("ssl_tls13_pick_key_cert:"
1204 "selected signature algorithm"
1205 " %s [%04x]",
1206 mbedtls_ssl_sig_alg_to_str(*sig_alg),
1207 *sig_alg));
XiaokangQianfb665a82022-06-15 03:57:21 +00001208 MBEDTLS_SSL_DEBUG_CRT(
Gilles Peskine449bd832023-01-11 14:50:10 +01001209 3, "selected certificate (chain)",
1210 ssl->handshake->key_cert->cert);
1211 return 0;
XiaokangQian81802f42022-06-10 13:25:22 +00001212 }
XiaokangQian81802f42022-06-10 13:25:22 +00001213 }
XiaokangQian23c5be62022-06-07 02:04:34 +00001214 }
1215
Gilles Peskine449bd832023-01-11 14:50:10 +01001216 MBEDTLS_SSL_DEBUG_MSG(2, ("ssl_tls13_pick_key_cert:"
1217 "no suitable certificate found"));
1218 return -1;
XiaokangQian23c5be62022-06-07 02:04:34 +00001219}
XiaokangQian81802f42022-06-10 13:25:22 +00001220#endif /* MBEDTLS_X509_CRT_PARSE_C &&
Ronald Cron928cbd32022-10-04 16:14:26 +02001221 MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED */
XiaokangQian23c5be62022-06-07 02:04:34 +00001222
XiaokangQian4080a7f2022-04-11 09:55:18 +00001223/*
XiaokangQianed582dd2022-04-13 08:21:05 +00001224 *
1225 * STATE HANDLING: ClientHello
1226 *
XiaokangQianb67384d2022-04-19 00:02:38 +00001227 * There are three possible classes of outcomes when parsing the ClientHello:
XiaokangQianed582dd2022-04-13 08:21:05 +00001228 *
XiaokangQianb67384d2022-04-19 00:02:38 +00001229 * 1) The ClientHello was well-formed and matched the server's configuration.
XiaokangQianed582dd2022-04-13 08:21:05 +00001230 *
1231 * In this case, the server progresses to sending its ServerHello.
1232 *
XiaokangQianb67384d2022-04-19 00:02:38 +00001233 * 2) The ClientHello was well-formed but didn't match the server's
1234 * configuration.
XiaokangQianed582dd2022-04-13 08:21:05 +00001235 *
1236 * For example, the client might not have offered a key share which
1237 * the server supports, or the server might require a cookie.
1238 *
1239 * In this case, the server sends a HelloRetryRequest.
1240 *
XiaokangQianb67384d2022-04-19 00:02:38 +00001241 * 3) The ClientHello was ill-formed
XiaokangQianed582dd2022-04-13 08:21:05 +00001242 *
1243 * In this case, we abort the handshake.
1244 *
1245 */
1246
1247/*
XiaokangQian4080a7f2022-04-11 09:55:18 +00001248 * Structure of this message:
1249 *
XiaokangQiane8ff3502022-04-22 02:34:40 +00001250 * uint16 ProtocolVersion;
1251 * opaque Random[32];
1252 * uint8 CipherSuite[2]; // Cryptographic suite selector
XiaokangQian4080a7f2022-04-11 09:55:18 +00001253 *
XiaokangQiane8ff3502022-04-22 02:34:40 +00001254 * struct {
1255 * ProtocolVersion legacy_version = 0x0303; // TLS v1.2
1256 * Random random;
1257 * opaque legacy_session_id<0..32>;
1258 * CipherSuite cipher_suites<2..2^16-2>;
1259 * opaque legacy_compression_methods<1..2^8-1>;
1260 * Extension extensions<8..2^16-1>;
1261 * } ClientHello;
XiaokangQian4080a7f2022-04-11 09:55:18 +00001262 */
XiaokangQianed582dd2022-04-13 08:21:05 +00001263
1264#define SSL_CLIENT_HELLO_OK 0
1265#define SSL_CLIENT_HELLO_HRR_REQUIRED 1
Ronald Cron5af4c7f2023-03-07 20:46:59 +01001266#define SSL_CLIENT_HELLO_TLS1_2 2
XiaokangQianed582dd2022-04-13 08:21:05 +00001267
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001268MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01001269static int ssl_tls13_parse_client_hello(mbedtls_ssl_context *ssl,
1270 const unsigned char *buf,
1271 const unsigned char *end)
XiaokangQian7807f9f2022-02-15 10:04:37 +00001272{
XiaokangQianb67384d2022-04-19 00:02:38 +00001273 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1274 const unsigned char *p = buf;
Ronald Crond540d992023-03-07 09:41:48 +01001275 const unsigned char *random;
XiaokangQian4080a7f2022-04-11 09:55:18 +00001276 size_t legacy_session_id_len;
Ronald Croncada4102023-03-07 09:51:39 +01001277 const unsigned char *legacy_session_id;
XiaokangQian318dc762022-04-20 09:43:51 +00001278 size_t cipher_suites_len;
Ronald Cron2f16b4e2023-03-07 10:07:32 +01001279 const unsigned char *cipher_suites;
XiaokangQian060d8672022-04-21 09:24:56 +00001280 const unsigned char *cipher_suites_end;
XiaokangQianb67384d2022-04-19 00:02:38 +00001281 size_t extensions_len;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001282 const unsigned char *extensions_end;
Ronald Croneff56732023-04-03 17:36:31 +02001283 const unsigned char *supported_versions_data;
1284 const unsigned char *supported_versions_data_end;
Jerry Yuc4bf5d62022-10-29 09:08:47 +08001285 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
Jerry Yu49ca9282022-05-05 11:05:22 +08001286 int hrr_required = 0;
Ronald Cron8a74f072023-06-14 17:59:29 +02001287 int no_usable_share_for_key_agreement = 0;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001288
Ronald Cron41a443a2022-10-04 16:38:25 +02001289#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED)
Jerry Yu29d9faa2022-08-23 17:52:45 +08001290 const unsigned char *pre_shared_key_ext = NULL;
Jerry Yu1c105562022-07-10 06:32:38 +00001291 const unsigned char *pre_shared_key_ext_end = NULL;
Ronald Cron41a443a2022-10-04 16:38:25 +02001292#endif
XiaokangQian7807f9f2022-02-15 10:04:37 +00001293
XiaokangQian7807f9f2022-02-15 10:04:37 +00001294 /*
XiaokangQianb67384d2022-04-19 00:02:38 +00001295 * ClientHello layout:
XiaokangQian7807f9f2022-02-15 10:04:37 +00001296 * 0 . 1 protocol version
XiaokangQiane8ff3502022-04-22 02:34:40 +00001297 * 2 . 33 random bytes
XiaokangQianc5763b52022-04-02 03:34:37 +00001298 * 34 . 34 session id length ( 1 byte )
XiaokangQian7807f9f2022-02-15 10:04:37 +00001299 * 35 . 34+x session id
XiaokangQian7807f9f2022-02-15 10:04:37 +00001300 * .. . .. ciphersuite list length ( 2 bytes )
1301 * .. . .. ciphersuite list
1302 * .. . .. compression alg. list length ( 1 byte )
1303 * .. . .. compression alg. list
1304 * .. . .. extensions length ( 2 bytes, optional )
1305 * .. . .. extensions ( optional )
1306 */
1307
XiaokangQianb67384d2022-04-19 00:02:38 +00001308 /*
bootstrap-prime6dbbf442022-05-17 19:30:44 -04001309 * Minimal length ( with everything empty and extensions omitted ) is
XiaokangQian7807f9f2022-02-15 10:04:37 +00001310 * 2 + 32 + 1 + 2 + 1 = 38 bytes. Check that first, so that we can
1311 * read at least up to session id length without worrying.
1312 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001313 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, 38);
XiaokangQian7807f9f2022-02-15 10:04:37 +00001314
1315 /* ...
1316 * ProtocolVersion legacy_version = 0x0303; // TLS 1.2
1317 * ...
1318 * with ProtocolVersion defined as:
1319 * uint16 ProtocolVersion;
1320 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001321 if (mbedtls_ssl_read_version(p, ssl->conf->transport) !=
1322 MBEDTLS_SSL_VERSION_TLS1_2) {
1323 MBEDTLS_SSL_DEBUG_MSG(1, ("Unsupported version of TLS."));
1324 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_PROTOCOL_VERSION,
1325 MBEDTLS_ERR_SSL_BAD_PROTOCOL_VERSION);
1326 return MBEDTLS_ERR_SSL_BAD_PROTOCOL_VERSION;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001327 }
1328 p += 2;
1329
Jerry Yuc1be19f2022-04-23 16:11:39 +08001330 /* ...
1331 * Random random;
1332 * ...
XiaokangQianb67384d2022-04-19 00:02:38 +00001333 * with Random defined as:
1334 * opaque Random[32];
XiaokangQian7807f9f2022-02-15 10:04:37 +00001335 */
Ronald Crond540d992023-03-07 09:41:48 +01001336 random = p;
XiaokangQian08037552022-04-20 07:16:41 +00001337 p += MBEDTLS_CLIENT_HELLO_RANDOM_LEN;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001338
Jerry Yuc1be19f2022-04-23 16:11:39 +08001339 /* ...
XiaokangQianb67384d2022-04-19 00:02:38 +00001340 * opaque legacy_session_id<0..32>;
Jerry Yuc1be19f2022-04-23 16:11:39 +08001341 * ...
XiaokangQian7807f9f2022-02-15 10:04:37 +00001342 */
Ronald Croncada4102023-03-07 09:51:39 +01001343 legacy_session_id_len = *(p++);
1344 legacy_session_id = p;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001345
XiaokangQianb67384d2022-04-19 00:02:38 +00001346 /*
1347 * Check we have enough data for the legacy session identifier
Jerry Yue95c8af2022-07-26 15:48:20 +08001348 * and the ciphersuite list length.
XiaokangQianb67384d2022-04-19 00:02:38 +00001349 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001350 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, legacy_session_id_len + 2);
XiaokangQian4080a7f2022-04-11 09:55:18 +00001351 p += legacy_session_id_len;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001352
Ronald Cron2f16b4e2023-03-07 10:07:32 +01001353 /* ...
1354 * CipherSuite cipher_suites<2..2^16-2>;
1355 * ...
1356 * with CipherSuite defined as:
1357 * uint8 CipherSuite[2];
1358 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001359 cipher_suites_len = MBEDTLS_GET_UINT16_BE(p, 0);
XiaokangQian7807f9f2022-02-15 10:04:37 +00001360 p += 2;
Ronald Cron2f16b4e2023-03-07 10:07:32 +01001361 cipher_suites = p;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001362
Ronald Cronfc7ae872023-02-16 15:32:19 +01001363 /*
1364 * The length of the ciphersuite list has to be even.
1365 */
1366 if (cipher_suites_len & 1) {
1367 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR,
1368 MBEDTLS_ERR_SSL_DECODE_ERROR);
1369 return MBEDTLS_ERR_SSL_DECODE_ERROR;
1370 }
1371
XiaokangQianb67384d2022-04-19 00:02:38 +00001372 /* Check we have enough data for the ciphersuite list, the legacy
1373 * compression methods and the length of the extensions.
Jerry Yue95c8af2022-07-26 15:48:20 +08001374 *
1375 * cipher_suites cipher_suites_len bytes
1376 * legacy_compression_methods 2 bytes
1377 * extensions_len 2 bytes
XiaokangQianb67384d2022-04-19 00:02:38 +00001378 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001379 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, cipher_suites_len + 2 + 2);
Ronald Cron8c527d02023-03-07 15:47:47 +01001380 p += cipher_suites_len;
1381 cipher_suites_end = p;
Jerry Yu5725f1c2022-08-21 17:27:16 +08001382
1383 /*
Ronald Cron8c527d02023-03-07 15:47:47 +01001384 * Search for the supported versions extension and parse it to determine
1385 * if the client supports TLS 1.3.
1386 */
1387 ret = mbedtls_ssl_tls13_is_supported_versions_ext_present_in_exts(
1388 ssl, p + 2, end,
Ronald Croneff56732023-04-03 17:36:31 +02001389 &supported_versions_data, &supported_versions_data_end);
Ronald Cron8c527d02023-03-07 15:47:47 +01001390 if (ret < 0) {
1391 MBEDTLS_SSL_DEBUG_RET(1,
1392 ("mbedtls_ssl_tls13_is_supported_versions_ext_present_in_exts"), ret);
1393 return ret;
1394 }
1395
1396 if (ret == 0) {
Ronald Cron5af4c7f2023-03-07 20:46:59 +01001397 return SSL_CLIENT_HELLO_TLS1_2;
Ronald Cron8c527d02023-03-07 15:47:47 +01001398 }
1399
Ronald Cron5af4c7f2023-03-07 20:46:59 +01001400 if (ret == 1) {
1401 ret = ssl_tls13_parse_supported_versions_ext(ssl,
Ronald Croneff56732023-04-03 17:36:31 +02001402 supported_versions_data,
1403 supported_versions_data_end);
Ronald Cron5af4c7f2023-03-07 20:46:59 +01001404 if (ret < 0) {
1405 MBEDTLS_SSL_DEBUG_RET(1,
1406 ("ssl_tls13_parse_supported_versions_ext"), ret);
1407 return ret;
1408 }
1409
Ronald Cronb828c7d2023-04-03 16:37:22 +02001410 /*
1411 * The supported versions extension was parsed successfully as the
1412 * value returned by ssl_tls13_parse_supported_versions_ext() is
1413 * positive. The return value is then equal to
1414 * MBEDTLS_SSL_VERSION_TLS1_2 or MBEDTLS_SSL_VERSION_TLS1_3, defining
1415 * the TLS version to negotiate.
1416 */
Ronald Cron5af4c7f2023-03-07 20:46:59 +01001417 if (MBEDTLS_SSL_VERSION_TLS1_2 == ret) {
1418 return SSL_CLIENT_HELLO_TLS1_2;
1419 }
Ronald Cron8c527d02023-03-07 15:47:47 +01001420 }
1421
1422 /*
1423 * We negotiate TLS 1.3.
Ronald Cron64582392023-03-07 09:21:40 +01001424 */
1425 ssl->tls_version = MBEDTLS_SSL_VERSION_TLS1_3;
Ronald Cron64582392023-03-07 09:21:40 +01001426 ssl->session_negotiate->tls_version = MBEDTLS_SSL_VERSION_TLS1_3;
1427 ssl->session_negotiate->endpoint = ssl->conf->endpoint;
Ronald Cron64582392023-03-07 09:21:40 +01001428
1429 /*
Ronald Crondad02b22023-04-06 09:57:52 +02001430 * We are negotiating the version 1.3 of the protocol. Do what we have
Ronald Croncada4102023-03-07 09:51:39 +01001431 * postponed: copy of the client random bytes, copy of the legacy session
Ronald Cron2f16b4e2023-03-07 10:07:32 +01001432 * identifier and selection of the TLS 1.3 cipher suite.
Ronald Crond540d992023-03-07 09:41:48 +01001433 */
1434 MBEDTLS_SSL_DEBUG_BUF(3, "client hello, random bytes",
1435 random, MBEDTLS_CLIENT_HELLO_RANDOM_LEN);
1436 memcpy(&handshake->randbytes[0], random, MBEDTLS_CLIENT_HELLO_RANDOM_LEN);
1437
Ronald Croncada4102023-03-07 09:51:39 +01001438 if (legacy_session_id_len > sizeof(ssl->session_negotiate->id)) {
1439 MBEDTLS_SSL_DEBUG_MSG(1, ("bad client hello message"));
1440 return MBEDTLS_ERR_SSL_DECODE_ERROR;
1441 }
1442 ssl->session_negotiate->id_len = legacy_session_id_len;
1443 MBEDTLS_SSL_DEBUG_BUF(3, "client hello, session id",
1444 legacy_session_id, legacy_session_id_len);
1445 memcpy(&ssl->session_negotiate->id[0],
1446 legacy_session_id, legacy_session_id_len);
1447
Ronald Crond540d992023-03-07 09:41:48 +01001448 /*
Jerry Yu5725f1c2022-08-21 17:27:16 +08001449 * Search for a matching ciphersuite
1450 */
Ronald Cron2f16b4e2023-03-07 10:07:32 +01001451 MBEDTLS_SSL_DEBUG_BUF(3, "client hello, list of cipher suites",
1452 cipher_suites, cipher_suites_len);
Jerry Yu5725f1c2022-08-21 17:27:16 +08001453
Ronald Cron89089cc2024-02-14 18:18:08 +01001454 ssl_tls13_select_ciphersuite(ssl, cipher_suites, cipher_suites_end,
1455 0, PSA_ALG_NONE, &handshake->ciphersuite_info);
Jerry Yu5725f1c2022-08-21 17:27:16 +08001456
Gilles Peskine449bd832023-01-11 14:50:10 +01001457 if (handshake->ciphersuite_info == NULL) {
1458 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE,
1459 MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE);
1460 return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
Jerry Yu5725f1c2022-08-21 17:27:16 +08001461 }
Ronald Cron89089cc2024-02-14 18:18:08 +01001462 ssl->session_negotiate->ciphersuite = handshake->ciphersuite_info->id;
1463
1464 MBEDTLS_SSL_DEBUG_MSG(2, ("selected ciphersuite: %04x - %s",
1465 ((unsigned) handshake->ciphersuite_info->id),
1466 handshake->ciphersuite_info->name));
Jerry Yuc1be19f2022-04-23 16:11:39 +08001467
XiaokangQian4080a7f2022-04-11 09:55:18 +00001468 /* ...
XiaokangQianb67384d2022-04-19 00:02:38 +00001469 * opaque legacy_compression_methods<1..2^8-1>;
XiaokangQian4080a7f2022-04-11 09:55:18 +00001470 * ...
XiaokangQian7807f9f2022-02-15 10:04:37 +00001471 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001472 if (p[0] != 1 || p[1] != MBEDTLS_SSL_COMPRESS_NULL) {
1473 MBEDTLS_SSL_DEBUG_MSG(1, ("bad legacy compression method"));
1474 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER,
1475 MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER);
1476 return MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001477 }
XiaokangQianb67384d2022-04-19 00:02:38 +00001478 p += 2;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001479
Jerry Yuc1be19f2022-04-23 16:11:39 +08001480 /* ...
XiaokangQianb67384d2022-04-19 00:02:38 +00001481 * Extension extensions<8..2^16-1>;
Jerry Yuc1be19f2022-04-23 16:11:39 +08001482 * ...
XiaokangQianb67384d2022-04-19 00:02:38 +00001483 * with Extension defined as:
1484 * struct {
1485 * ExtensionType extension_type;
1486 * opaque extension_data<0..2^16-1>;
1487 * } Extension;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001488 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001489 extensions_len = MBEDTLS_GET_UINT16_BE(p, 0);
XiaokangQian7807f9f2022-02-15 10:04:37 +00001490 p += 2;
Gilles Peskine449bd832023-01-11 14:50:10 +01001491 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, extensions_len);
XiaokangQiane8ff3502022-04-22 02:34:40 +00001492 extensions_end = p + extensions_len;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001493
Gilles Peskine449bd832023-01-11 14:50:10 +01001494 MBEDTLS_SSL_DEBUG_BUF(3, "client hello extensions", p, extensions_len);
Jerry Yu63a459c2022-10-31 13:38:40 +08001495 handshake->received_extensions = MBEDTLS_SSL_EXT_MASK_NONE;
Jerry Yu0c354a22022-08-29 15:25:36 +08001496
Gilles Peskine449bd832023-01-11 14:50:10 +01001497 while (p < extensions_end) {
XiaokangQian7807f9f2022-02-15 10:04:37 +00001498 unsigned int extension_type;
1499 size_t extension_data_len;
1500 const unsigned char *extension_data_end;
Jerry Yu263dbf72022-10-26 10:51:27 +08001501 uint32_t allowed_exts = MBEDTLS_SSL_TLS1_3_ALLOWED_EXTS_OF_CH;
1502
Ronald Cron5fbd2702024-02-14 10:03:36 +01001503 if (ssl->handshake->hello_retry_request_flag) {
Jerry Yu263dbf72022-10-26 10:51:27 +08001504 /* Do not accept early data extension in 2nd ClientHello */
1505 allowed_exts &= ~MBEDTLS_SSL_EXT_MASK(EARLY_DATA);
1506 }
XiaokangQian7807f9f2022-02-15 10:04:37 +00001507
Jerry Yu0c354a22022-08-29 15:25:36 +08001508 /* RFC 8446, section 4.2.11
Jerry Yu1c9247c2022-07-21 12:37:39 +08001509 *
1510 * The "pre_shared_key" extension MUST be the last extension in the
1511 * ClientHello (this facilitates implementation as described below).
1512 * Servers MUST check that it is the last extension and otherwise fail
1513 * the handshake with an "illegal_parameter" alert.
1514 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001515 if (handshake->received_extensions & MBEDTLS_SSL_EXT_MASK(PRE_SHARED_KEY)) {
Jerry Yu1c9247c2022-07-21 12:37:39 +08001516 MBEDTLS_SSL_DEBUG_MSG(
Gilles Peskine449bd832023-01-11 14:50:10 +01001517 3, ("pre_shared_key is not last extension."));
Jerry Yu1c9247c2022-07-21 12:37:39 +08001518 MBEDTLS_SSL_PEND_FATAL_ALERT(
1519 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER,
Gilles Peskine449bd832023-01-11 14:50:10 +01001520 MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER);
1521 return MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER;
Jerry Yu1c9247c2022-07-21 12:37:39 +08001522 }
1523
Gilles Peskine449bd832023-01-11 14:50:10 +01001524 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, extensions_end, 4);
1525 extension_type = MBEDTLS_GET_UINT16_BE(p, 0);
1526 extension_data_len = MBEDTLS_GET_UINT16_BE(p, 2);
XiaokangQian7807f9f2022-02-15 10:04:37 +00001527 p += 4;
1528
Gilles Peskine449bd832023-01-11 14:50:10 +01001529 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, extensions_end, extension_data_len);
XiaokangQian7807f9f2022-02-15 10:04:37 +00001530 extension_data_end = p + extension_data_len;
1531
Jerry Yuc4bf5d62022-10-29 09:08:47 +08001532 ret = mbedtls_ssl_tls13_check_received_extension(
Gilles Peskine449bd832023-01-11 14:50:10 +01001533 ssl, MBEDTLS_SSL_HS_CLIENT_HELLO, extension_type,
Jerry Yu263dbf72022-10-26 10:51:27 +08001534 allowed_exts);
Gilles Peskine449bd832023-01-11 14:50:10 +01001535 if (ret != 0) {
1536 return ret;
1537 }
Jerry Yue18dc7e2022-08-04 16:29:22 +08001538
Gilles Peskine449bd832023-01-11 14:50:10 +01001539 switch (extension_type) {
XiaokangQian40a35232022-05-07 09:02:40 +00001540#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
1541 case MBEDTLS_TLS_EXT_SERVERNAME:
Gilles Peskine449bd832023-01-11 14:50:10 +01001542 MBEDTLS_SSL_DEBUG_MSG(3, ("found ServerName extension"));
1543 ret = mbedtls_ssl_parse_server_name_ext(ssl, p,
1544 extension_data_end);
1545 if (ret != 0) {
XiaokangQian40a35232022-05-07 09:02:40 +00001546 MBEDTLS_SSL_DEBUG_RET(
Gilles Peskine449bd832023-01-11 14:50:10 +01001547 1, "mbedtls_ssl_parse_servername_ext", ret);
1548 return ret;
XiaokangQian40a35232022-05-07 09:02:40 +00001549 }
XiaokangQian40a35232022-05-07 09:02:40 +00001550 break;
1551#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
1552
Przemek Stekiel8c0a9532023-06-15 16:48:19 +02001553#if defined(PSA_WANT_ALG_ECDH) || defined(PSA_WANT_ALG_FFDH)
XiaokangQian7807f9f2022-02-15 10:04:37 +00001554 case MBEDTLS_TLS_EXT_SUPPORTED_GROUPS:
Gilles Peskine449bd832023-01-11 14:50:10 +01001555 MBEDTLS_SSL_DEBUG_MSG(3, ("found supported group extension"));
XiaokangQian7807f9f2022-02-15 10:04:37 +00001556
1557 /* Supported Groups Extension
1558 *
1559 * When sent by the client, the "supported_groups" extension
1560 * indicates the named groups which the client supports,
1561 * ordered from most preferred to least preferred.
1562 */
Jerry Yuc1be19f2022-04-23 16:11:39 +08001563 ret = ssl_tls13_parse_supported_groups_ext(
Gilles Peskine449bd832023-01-11 14:50:10 +01001564 ssl, p, extension_data_end);
1565 if (ret != 0) {
Xiaokang Qian9f1747b2023-03-30 02:50:04 +00001566 MBEDTLS_SSL_DEBUG_RET(
Xiaokang Qian8bce0e62023-04-04 10:15:48 +00001567 1, "ssl_tls13_parse_supported_groups_ext", ret);
Gilles Peskine449bd832023-01-11 14:50:10 +01001568 return ret;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001569 }
1570
XiaokangQian7807f9f2022-02-15 10:04:37 +00001571 break;
Przemek Stekiel8c0a9532023-06-15 16:48:19 +02001572#endif /* PSA_WANT_ALG_ECDH || PSA_WANT_ALG_FFDH*/
XiaokangQian7807f9f2022-02-15 10:04:37 +00001573
Valerio Settic9ae8622023-07-25 11:23:50 +02001574#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_EPHEMERAL_ENABLED)
XiaokangQian7807f9f2022-02-15 10:04:37 +00001575 case MBEDTLS_TLS_EXT_KEY_SHARE:
Gilles Peskine449bd832023-01-11 14:50:10 +01001576 MBEDTLS_SSL_DEBUG_MSG(3, ("found key share extension"));
XiaokangQian7807f9f2022-02-15 10:04:37 +00001577
1578 /*
1579 * Key Share Extension
1580 *
1581 * When sent by the client, the "key_share" extension
1582 * contains the endpoint's cryptographic parameters for
1583 * ECDHE/DHE key establishment methods.
1584 */
Jerry Yuc1be19f2022-04-23 16:11:39 +08001585 ret = ssl_tls13_parse_key_shares_ext(
Gilles Peskine449bd832023-01-11 14:50:10 +01001586 ssl, p, extension_data_end);
1587 if (ret == SSL_TLS1_3_PARSE_KEY_SHARES_EXT_NO_MATCH) {
Ronald Cron8a74f072023-06-14 17:59:29 +02001588 MBEDTLS_SSL_DEBUG_MSG(2, ("No usable share for key agreement."));
1589 no_usable_share_for_key_agreement = 1;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001590 }
1591
Gilles Peskine449bd832023-01-11 14:50:10 +01001592 if (ret < 0) {
Jerry Yu582dd062022-04-22 21:59:01 +08001593 MBEDTLS_SSL_DEBUG_RET(
Gilles Peskine449bd832023-01-11 14:50:10 +01001594 1, "ssl_tls13_parse_key_shares_ext", ret);
1595 return ret;
Jerry Yu582dd062022-04-22 21:59:01 +08001596 }
XiaokangQian7807f9f2022-02-15 10:04:37 +00001597
XiaokangQian7807f9f2022-02-15 10:04:37 +00001598 break;
Valerio Settic9ae8622023-07-25 11:23:50 +02001599#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_EPHEMERAL_ENABLED */
XiaokangQian7807f9f2022-02-15 10:04:37 +00001600
1601 case MBEDTLS_TLS_EXT_SUPPORTED_VERSIONS:
Ronald Cron8c527d02023-03-07 15:47:47 +01001602 /* Already parsed */
XiaokangQian7807f9f2022-02-15 10:04:37 +00001603 break;
1604
Ronald Cron41a443a2022-10-04 16:38:25 +02001605#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED)
Jerry Yue19e3b92022-07-08 12:04:51 +00001606 case MBEDTLS_TLS_EXT_PSK_KEY_EXCHANGE_MODES:
Xiaokang Qian9f1747b2023-03-30 02:50:04 +00001607 MBEDTLS_SSL_DEBUG_MSG(
1608 3, ("found psk key exchange modes extension"));
Jerry Yue19e3b92022-07-08 12:04:51 +00001609
1610 ret = ssl_tls13_parse_key_exchange_modes_ext(
Gilles Peskine449bd832023-01-11 14:50:10 +01001611 ssl, p, extension_data_end);
1612 if (ret != 0) {
Jerry Yue19e3b92022-07-08 12:04:51 +00001613 MBEDTLS_SSL_DEBUG_RET(
Gilles Peskine449bd832023-01-11 14:50:10 +01001614 1, "ssl_tls13_parse_key_exchange_modes_ext", ret);
1615 return ret;
Jerry Yue19e3b92022-07-08 12:04:51 +00001616 }
1617
Jerry Yue19e3b92022-07-08 12:04:51 +00001618 break;
Ronald Cron41a443a2022-10-04 16:38:25 +02001619#endif
Jerry Yue19e3b92022-07-08 12:04:51 +00001620
Jerry Yu1c105562022-07-10 06:32:38 +00001621 case MBEDTLS_TLS_EXT_PRE_SHARED_KEY:
Gilles Peskine449bd832023-01-11 14:50:10 +01001622 MBEDTLS_SSL_DEBUG_MSG(3, ("found pre_shared_key extension"));
1623 if ((handshake->received_extensions &
1624 MBEDTLS_SSL_EXT_MASK(PSK_KEY_EXCHANGE_MODES)) == 0) {
Jerry Yu13ab81d2022-07-22 23:17:11 +08001625 MBEDTLS_SSL_PEND_FATAL_ALERT(
1626 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER,
Gilles Peskine449bd832023-01-11 14:50:10 +01001627 MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER);
1628 return MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER;
Jerry Yu13ab81d2022-07-22 23:17:11 +08001629 }
Ronald Cron41a443a2022-10-04 16:38:25 +02001630#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED)
Jerry Yu1c105562022-07-10 06:32:38 +00001631 /* Delay processing of the PSK identity once we have
1632 * found out which algorithms to use. We keep a pointer
1633 * to the buffer and the size for later processing.
1634 */
Jerry Yu29d9faa2022-08-23 17:52:45 +08001635 pre_shared_key_ext = p;
Jerry Yu1c105562022-07-10 06:32:38 +00001636 pre_shared_key_ext_end = extension_data_end;
Jerry Yue18dc7e2022-08-04 16:29:22 +08001637#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED */
Jerry Yu1c105562022-07-10 06:32:38 +00001638 break;
Jerry Yu1c105562022-07-10 06:32:38 +00001639
XiaokangQianacb39922022-06-17 10:18:48 +00001640#if defined(MBEDTLS_SSL_ALPN)
1641 case MBEDTLS_TLS_EXT_ALPN:
Gilles Peskine449bd832023-01-11 14:50:10 +01001642 MBEDTLS_SSL_DEBUG_MSG(3, ("found alpn extension"));
XiaokangQianacb39922022-06-17 10:18:48 +00001643
Gilles Peskine449bd832023-01-11 14:50:10 +01001644 ret = mbedtls_ssl_parse_alpn_ext(ssl, p, extension_data_end);
1645 if (ret != 0) {
XiaokangQianacb39922022-06-17 10:18:48 +00001646 MBEDTLS_SSL_DEBUG_RET(
Gilles Peskine449bd832023-01-11 14:50:10 +01001647 1, ("mbedtls_ssl_parse_alpn_ext"), ret);
1648 return ret;
XiaokangQianacb39922022-06-17 10:18:48 +00001649 }
XiaokangQianacb39922022-06-17 10:18:48 +00001650 break;
1651#endif /* MBEDTLS_SSL_ALPN */
1652
Ronald Cron928cbd32022-10-04 16:14:26 +02001653#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED)
XiaokangQian7807f9f2022-02-15 10:04:37 +00001654 case MBEDTLS_TLS_EXT_SIG_ALG:
Gilles Peskine449bd832023-01-11 14:50:10 +01001655 MBEDTLS_SSL_DEBUG_MSG(3, ("found signature_algorithms extension"));
XiaokangQian7807f9f2022-02-15 10:04:37 +00001656
Gabor Mezei078e8032022-04-27 21:17:56 +02001657 ret = mbedtls_ssl_parse_sig_alg_ext(
Gilles Peskine449bd832023-01-11 14:50:10 +01001658 ssl, p, extension_data_end);
1659 if (ret != 0) {
Xiaokang Qian49f39c12023-04-06 02:31:02 +00001660 MBEDTLS_SSL_DEBUG_RET(
1661 1, "mbedtls_ssl_parse_sig_alg_ext", ret);
Gilles Peskine449bd832023-01-11 14:50:10 +01001662 return ret;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001663 }
XiaokangQian7807f9f2022-02-15 10:04:37 +00001664 break;
Ronald Cron928cbd32022-10-04 16:14:26 +02001665#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED */
XiaokangQian7807f9f2022-02-15 10:04:37 +00001666
Jan Bruckner151f6422023-02-10 12:45:19 +01001667#if defined(MBEDTLS_SSL_RECORD_SIZE_LIMIT)
1668 case MBEDTLS_TLS_EXT_RECORD_SIZE_LIMIT:
1669 MBEDTLS_SSL_DEBUG_MSG(3, ("found record_size_limit extension"));
1670
Xiaokang Qian9f1747b2023-03-30 02:50:04 +00001671 ret = mbedtls_ssl_tls13_parse_record_size_limit_ext(
1672 ssl, p, extension_data_end);
Jan Brucknerf482dcc2023-03-15 09:09:06 +01001673 if (ret != 0) {
1674 MBEDTLS_SSL_DEBUG_RET(
1675 1, ("mbedtls_ssl_tls13_parse_record_size_limit_ext"), ret);
1676 return ret;
1677 }
Jan Bruckner151f6422023-02-10 12:45:19 +01001678 break;
1679#endif /* MBEDTLS_SSL_RECORD_SIZE_LIMIT */
1680
XiaokangQian7807f9f2022-02-15 10:04:37 +00001681 default:
Jerry Yu79aa7212022-11-08 21:30:21 +08001682 MBEDTLS_SSL_PRINT_EXT(
Jerry Yu63a459c2022-10-31 13:38:40 +08001683 3, MBEDTLS_SSL_HS_CLIENT_HELLO,
Gilles Peskine449bd832023-01-11 14:50:10 +01001684 extension_type, "( ignored )");
Jerry Yu0c354a22022-08-29 15:25:36 +08001685 break;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001686 }
1687
1688 p += extension_data_len;
1689 }
1690
Gilles Peskine449bd832023-01-11 14:50:10 +01001691 MBEDTLS_SSL_PRINT_EXTS(3, MBEDTLS_SSL_HS_CLIENT_HELLO,
1692 handshake->received_extensions);
Jerry Yue18dc7e2022-08-04 16:29:22 +08001693
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +01001694 ret = mbedtls_ssl_add_hs_hdr_to_checksum(ssl,
1695 MBEDTLS_SSL_HS_CLIENT_HELLO,
1696 p - buf);
1697 if (0 != ret) {
1698 MBEDTLS_SSL_DEBUG_RET(1, ("mbedtls_ssl_add_hs_hdr_to_checksum"), ret);
1699 return ret;
1700 }
Jerry Yu032b15ce2022-07-11 06:10:03 +00001701
Ronald Cron41a443a2022-10-04 16:38:25 +02001702#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED)
XiaokangQian7807f9f2022-02-15 10:04:37 +00001703 /* Update checksum with either
1704 * - The entire content of the CH message, if no PSK extension is present
1705 * - The content up to but excluding the PSK extension, if present.
Ronald Cron12e72f12024-02-14 15:49:38 +01001706 * Always parse the pre-shared key extension when present in the
1707 * ClientHello even if some pre-requisites for PSK key exchange modes are
1708 * not met. That way we always validate the syntax of the extension.
XiaokangQian7807f9f2022-02-15 10:04:37 +00001709 */
Ronald Cron12e72f12024-02-14 15:49:38 +01001710 if (handshake->received_extensions & MBEDTLS_SSL_EXT_MASK(PRE_SHARED_KEY)) {
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +01001711 ret = handshake->update_checksum(ssl, buf,
1712 pre_shared_key_ext - buf);
1713 if (0 != ret) {
1714 MBEDTLS_SSL_DEBUG_RET(1, ("update_checksum"), ret);
1715 return ret;
1716 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001717 ret = ssl_tls13_parse_pre_shared_key_ext(ssl,
1718 pre_shared_key_ext,
1719 pre_shared_key_ext_end,
1720 cipher_suites,
1721 cipher_suites_end);
1722 if (ret == MBEDTLS_ERR_SSL_UNKNOWN_IDENTITY) {
1723 handshake->received_extensions &= ~MBEDTLS_SSL_EXT_MASK(PRE_SHARED_KEY);
1724 } else if (ret != 0) {
Jerry Yu63a459c2022-10-31 13:38:40 +08001725 MBEDTLS_SSL_DEBUG_RET(
Gilles Peskine449bd832023-01-11 14:50:10 +01001726 1, "ssl_tls13_parse_pre_shared_key_ext", ret);
1727 return ret;
Jerry Yu1c105562022-07-10 06:32:38 +00001728 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001729 } else
Ronald Cron41a443a2022-10-04 16:38:25 +02001730#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED */
Jerry Yu1c105562022-07-10 06:32:38 +00001731 {
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +01001732 ret = handshake->update_checksum(ssl, buf, p - buf);
1733 if (0 != ret) {
1734 MBEDTLS_SSL_DEBUG_RET(1, ("update_checksum"), ret);
1735 return ret;
1736 }
Jerry Yu1c105562022-07-10 06:32:38 +00001737 }
XiaokangQian7807f9f2022-02-15 10:04:37 +00001738
Gilles Peskine449bd832023-01-11 14:50:10 +01001739 ret = ssl_tls13_determine_key_exchange_mode(ssl);
1740 if (ret < 0) {
1741 return ret;
1742 }
XiaokangQian7807f9f2022-02-15 10:04:37 +00001743
Ronald Cron8a74f072023-06-14 17:59:29 +02001744 if (ssl->handshake->key_exchange_mode !=
1745 MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK) {
1746 hrr_required = (no_usable_share_for_key_agreement != 0);
1747 }
1748
Gilles Peskine449bd832023-01-11 14:50:10 +01001749 mbedtls_ssl_optimize_checksum(ssl, handshake->ciphersuite_info);
Jerry Yue5834fd2022-08-29 20:16:09 +08001750
Gilles Peskine449bd832023-01-11 14:50:10 +01001751 return hrr_required ? SSL_CLIENT_HELLO_HRR_REQUIRED : SSL_CLIENT_HELLO_OK;
XiaokangQian75fe8c72022-06-15 09:42:45 +00001752}
1753
Jerry Yuab0da372023-02-08 13:55:24 +08001754#if defined(MBEDTLS_SSL_EARLY_DATA)
Jerry Yu4caf3ca2023-11-15 16:13:47 +08001755static int ssl_tls13_check_early_data_requirements(mbedtls_ssl_context *ssl)
Jerry Yuab0da372023-02-08 13:55:24 +08001756{
1757 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
1758
Jerry Yu985c9672022-12-04 14:06:30 +08001759 if (ssl->conf->early_data_enabled == MBEDTLS_SSL_EARLY_DATA_DISABLED) {
1760 MBEDTLS_SSL_DEBUG_MSG(
Jerry Yu985c9672022-12-04 14:06:30 +08001761 1,
Jerry Yu454dda32023-10-31 15:13:54 +08001762 ("EarlyData: rejected, feature disabled in server configuration."));
Jerry Yu4caf3ca2023-11-15 16:13:47 +08001763 return -1;
Ronald Cron7b6ee942024-01-12 10:29:55 +01001764 }
1765
Jerry Yu454dda32023-10-31 15:13:54 +08001766 if (!handshake->resume) {
1767 /* We currently support early data only in the case of PSKs established
1768 via a NewSessionTicket message thus in the case of a session
1769 resumption. */
Jerry Yu985c9672022-12-04 14:06:30 +08001770 MBEDTLS_SSL_DEBUG_MSG(
Jerry Yu7ef9fd82023-11-07 14:30:38 +08001771 1, ("EarlyData: rejected, not a session resumption."));
Jerry Yu4caf3ca2023-11-15 16:13:47 +08001772 return -1;
Jerry Yu985c9672022-12-04 14:06:30 +08001773 }
1774
Jerry Yu82fd6c12023-10-31 16:32:19 +08001775 /* RFC 8446 4.2.10
1776 *
1777 * In order to accept early data, the server MUST have accepted a PSK cipher
1778 * suite and selected the first key offered in the client's "pre_shared_key"
1779 * extension. In addition, it MUST verify that the following values are the
1780 * same as those associated with the selected PSK:
1781 * - The TLS version number
1782 * - The selected cipher suite
1783 * - The selected ALPN [RFC7301] protocol, if any
1784 *
1785 * NOTE:
Jerry Yu7ef9fd82023-11-07 14:30:38 +08001786 * - The TLS version number is checked in
1787 * ssl_tls13_offered_psks_check_identity_match_ticket().
1788 * - ALPN is not checked for the time being (TODO).
Jerry Yu82fd6c12023-10-31 16:32:19 +08001789 */
1790
1791 if (handshake->selected_identity != 0) {
1792 MBEDTLS_SSL_DEBUG_MSG(
Jerry Yu7ef9fd82023-11-07 14:30:38 +08001793 1, ("EarlyData: rejected, the selected key in "
1794 "`pre_shared_key` is not the first one."));
Jerry Yu4caf3ca2023-11-15 16:13:47 +08001795 return -1;
Jerry Yu82fd6c12023-10-31 16:32:19 +08001796 }
1797
1798 if (handshake->ciphersuite_info->id !=
1799 ssl->session_negotiate->ciphersuite) {
1800 MBEDTLS_SSL_DEBUG_MSG(
Jerry Yu7ef9fd82023-11-07 14:30:38 +08001801 1, ("EarlyData: rejected, the selected ciphersuite is not the one "
1802 "of the selected pre-shared key."));
Jerry Yu4caf3ca2023-11-15 16:13:47 +08001803 return -1;
Jerry Yu82fd6c12023-10-31 16:32:19 +08001804
1805 }
Jerry Yu985c9672022-12-04 14:06:30 +08001806
Pengyu Lv94a42cc2023-12-06 10:04:17 +08001807 if (!mbedtls_ssl_tls13_session_ticket_allow_early_data(ssl->session_negotiate)) {
Jerry Yufceddb32022-12-12 15:30:34 +08001808 MBEDTLS_SSL_DEBUG_MSG(
1809 1,
Jerry Yuea96ac32023-11-21 17:06:36 +08001810 ("EarlyData: rejected, early_data not allowed in ticket "
1811 "permission bits."));
Jerry Yu4caf3ca2023-11-15 16:13:47 +08001812 return -1;
Jerry Yufceddb32022-12-12 15:30:34 +08001813 }
Jerry Yu985c9672022-12-04 14:06:30 +08001814
Jerry Yu4caf3ca2023-11-15 16:13:47 +08001815 return 0;
Jerry Yuab0da372023-02-08 13:55:24 +08001816}
1817#endif /* MBEDTLS_SSL_EARLY_DATA */
1818
XiaokangQian75fe8c72022-06-15 09:42:45 +00001819/* Update the handshake state machine */
1820
Ronald Cronce7d76e2022-07-08 18:56:49 +02001821MBEDTLS_CHECK_RETURN_CRITICAL
Ronald Cron7b6ee942024-01-12 10:29:55 +01001822static int ssl_tls13_postprocess_client_hello(mbedtls_ssl_context *ssl,
1823 int hrr_required)
XiaokangQian75fe8c72022-06-15 09:42:45 +00001824{
1825 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1826
XiaokangQian7807f9f2022-02-15 10:04:37 +00001827 /*
XiaokangQianfb665a82022-06-15 03:57:21 +00001828 * Server certificate selection
1829 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001830 if (ssl->conf->f_cert_cb && (ret = ssl->conf->f_cert_cb(ssl)) != 0) {
1831 MBEDTLS_SSL_DEBUG_RET(1, "f_cert_cb", ret);
1832 return ret;
XiaokangQianfb665a82022-06-15 03:57:21 +00001833 }
1834#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
1835 ssl->handshake->sni_name = NULL;
1836 ssl->handshake->sni_name_len = 0;
1837#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
XiaokangQian7807f9f2022-02-15 10:04:37 +00001838
Gilles Peskine449bd832023-01-11 14:50:10 +01001839 ret = mbedtls_ssl_tls13_key_schedule_stage_early(ssl);
1840 if (ret != 0) {
1841 MBEDTLS_SSL_DEBUG_RET(1,
1842 "mbedtls_ssl_tls1_3_key_schedule_stage_early", ret);
1843 return ret;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001844 }
1845
Jerry Yuab0da372023-02-08 13:55:24 +08001846#if defined(MBEDTLS_SSL_EARLY_DATA)
Jerry Yu4caf3ca2023-11-15 16:13:47 +08001847 if (ssl->handshake->received_extensions & MBEDTLS_SSL_EXT_MASK(EARLY_DATA)) {
Ronald Cron31e2d832024-02-05 16:45:57 +01001848 ssl->handshake->early_data_accepted =
1849 (!hrr_required) && (ssl_tls13_check_early_data_requirements(ssl) == 0);
Jerry Yu4e9b70e2022-12-04 14:08:02 +08001850
Jerry Yu4caf3ca2023-11-15 16:13:47 +08001851 if (ssl->handshake->early_data_accepted) {
1852 ret = mbedtls_ssl_tls13_compute_early_transform(ssl);
1853 if (ret != 0) {
1854 MBEDTLS_SSL_DEBUG_RET(
1855 1, "mbedtls_ssl_tls13_compute_early_transform", ret);
1856 return ret;
1857 }
1858 } else {
1859 ssl->discard_early_data_record =
1860 hrr_required ?
1861 MBEDTLS_SSL_EARLY_DATA_DISCARD :
1862 MBEDTLS_SSL_EARLY_DATA_TRY_TO_DEPROTECT_AND_DISCARD;
Jerry Yu4e9b70e2022-12-04 14:08:02 +08001863 }
1864 }
Ronald Cron7b6ee942024-01-12 10:29:55 +01001865#else
1866 ((void) hrr_required);
Jerry Yuab0da372023-02-08 13:55:24 +08001867#endif /* MBEDTLS_SSL_EARLY_DATA */
1868
Gilles Peskine449bd832023-01-11 14:50:10 +01001869 return 0;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001870}
1871
1872/*
XiaokangQianed582dd2022-04-13 08:21:05 +00001873 * Main entry point from the state machine; orchestrates the otherfunctions.
1874 */
1875
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001876MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01001877static int ssl_tls13_process_client_hello(mbedtls_ssl_context *ssl)
XiaokangQianed582dd2022-04-13 08:21:05 +00001878{
1879
XiaokangQian08037552022-04-20 07:16:41 +00001880 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine449bd832023-01-11 14:50:10 +01001881 unsigned char *buf = NULL;
XiaokangQianed582dd2022-04-13 08:21:05 +00001882 size_t buflen = 0;
Jerry Yu4ca91402022-05-09 15:50:57 +08001883 int parse_client_hello_ret;
1884
Gilles Peskine449bd832023-01-11 14:50:10 +01001885 MBEDTLS_SSL_DEBUG_MSG(2, ("=> parse client hello"));
XiaokangQianed582dd2022-04-13 08:21:05 +00001886
Gilles Peskine449bd832023-01-11 14:50:10 +01001887 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_tls13_fetch_handshake_msg(
1888 ssl, MBEDTLS_SSL_HS_CLIENT_HELLO,
1889 &buf, &buflen));
XiaokangQianed582dd2022-04-13 08:21:05 +00001890
Gilles Peskine449bd832023-01-11 14:50:10 +01001891 MBEDTLS_SSL_PROC_CHK_NEG(ssl_tls13_parse_client_hello(ssl, buf,
1892 buf + buflen));
Ronald Cron5af4c7f2023-03-07 20:46:59 +01001893 parse_client_hello_ret = ret; /* Store positive return value of
1894 * parse_client_hello,
1895 * as negative error codes are handled
Jerry Yuf41553b2022-05-09 22:20:30 +08001896 * by MBEDTLS_SSL_PROC_CHK_NEG. */
Jerry Yu4ca91402022-05-09 15:50:57 +08001897
Ronald Cronb828c7d2023-04-03 16:37:22 +02001898 /*
Yanray Wang90acdc62023-12-08 10:29:42 +08001899 * Version 1.2 of the protocol has to be used for the handshake.
1900 * If TLS 1.2 is not supported, abort the handshake. Otherwise, set the
Ronald Cronb828c7d2023-04-03 16:37:22 +02001901 * ssl->keep_current_message flag for the ClientHello to be kept and parsed
1902 * as a TLS 1.2 ClientHello. We also change ssl->tls_version to
1903 * MBEDTLS_SSL_VERSION_TLS1_2 thus from now on mbedtls_ssl_handshake_step()
1904 * will dispatch to the TLS 1.2 state machine.
1905 */
Ronald Cron5af4c7f2023-03-07 20:46:59 +01001906 if (SSL_CLIENT_HELLO_TLS1_2 == parse_client_hello_ret) {
Yanray Wangfb0f47b2023-12-04 15:27:28 +08001907 /* Check if server supports TLS 1.2 */
Yanray Wang408ba6f2023-12-08 10:18:03 +08001908 if (!mbedtls_ssl_conf_is_tls12_enabled(ssl->conf)) {
Yanray Wangfb0f47b2023-12-04 15:27:28 +08001909 MBEDTLS_SSL_DEBUG_MSG(
Yanray Wang177e49a2023-12-08 10:51:04 +08001910 1, ("TLS 1.2 not supported."));
Yanray Wangfb0f47b2023-12-04 15:27:28 +08001911 MBEDTLS_SSL_PEND_FATAL_ALERT(
Yanray Wang2bef9172023-12-08 10:21:53 +08001912 MBEDTLS_SSL_ALERT_MSG_PROTOCOL_VERSION,
1913 MBEDTLS_ERR_SSL_BAD_PROTOCOL_VERSION);
1914 return MBEDTLS_ERR_SSL_BAD_PROTOCOL_VERSION;
Yanray Wangfb0f47b2023-12-04 15:27:28 +08001915 }
Ronald Cron5af4c7f2023-03-07 20:46:59 +01001916 ssl->keep_current_message = 1;
1917 ssl->tls_version = MBEDTLS_SSL_VERSION_TLS1_2;
1918 return 0;
1919 }
1920
Ronald Cron7b6ee942024-01-12 10:29:55 +01001921 MBEDTLS_SSL_PROC_CHK(
1922 ssl_tls13_postprocess_client_hello(ssl, parse_client_hello_ret ==
1923 SSL_CLIENT_HELLO_HRR_REQUIRED));
Jerry Yu582dd062022-04-22 21:59:01 +08001924
Ronald Cron5af4c7f2023-03-07 20:46:59 +01001925 if (SSL_CLIENT_HELLO_OK == parse_client_hello_ret) {
Gilles Peskine449bd832023-01-11 14:50:10 +01001926 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_SERVER_HELLO);
1927 } else {
1928 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_HELLO_RETRY_REQUEST);
1929 }
XiaokangQianed582dd2022-04-13 08:21:05 +00001930
1931cleanup:
1932
Gilles Peskine449bd832023-01-11 14:50:10 +01001933 MBEDTLS_SSL_DEBUG_MSG(2, ("<= parse client hello"));
1934 return ret;
XiaokangQianed582dd2022-04-13 08:21:05 +00001935}
1936
1937/*
Jerry Yu1c3e6882022-04-20 21:23:40 +08001938 * Handler for MBEDTLS_SSL_SERVER_HELLO
Jerry Yu5b64ae92022-03-30 17:15:02 +08001939 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001940MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01001941static int ssl_tls13_prepare_server_hello(mbedtls_ssl_context *ssl)
Jerry Yuf4b27e42022-03-30 17:32:21 +08001942{
Jerry Yu637a3f12022-04-20 21:37:58 +08001943 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1944 unsigned char *server_randbytes =
Gilles Peskine449bd832023-01-11 14:50:10 +01001945 ssl->handshake->randbytes + MBEDTLS_CLIENT_HELLO_RANDOM_LEN;
1946 if (ssl->conf->f_rng == NULL) {
1947 MBEDTLS_SSL_DEBUG_MSG(1, ("no RNG provided"));
1948 return MBEDTLS_ERR_SSL_NO_RNG;
Jerry Yuf4b27e42022-03-30 17:32:21 +08001949 }
1950
Gilles Peskine449bd832023-01-11 14:50:10 +01001951 if ((ret = ssl->conf->f_rng(ssl->conf->p_rng, server_randbytes,
1952 MBEDTLS_SERVER_HELLO_RANDOM_LEN)) != 0) {
1953 MBEDTLS_SSL_DEBUG_RET(1, "f_rng", ret);
1954 return ret;
Jerry Yuf4b27e42022-03-30 17:32:21 +08001955 }
1956
Gilles Peskine449bd832023-01-11 14:50:10 +01001957 MBEDTLS_SSL_DEBUG_BUF(3, "server hello, random bytes", server_randbytes,
1958 MBEDTLS_SERVER_HELLO_RANDOM_LEN);
Jerry Yuf4b27e42022-03-30 17:32:21 +08001959
1960#if defined(MBEDTLS_HAVE_TIME)
YxCda609132023-05-22 12:08:12 -07001961 ssl->session_negotiate->start = mbedtls_time(NULL);
Jerry Yuf4b27e42022-03-30 17:32:21 +08001962#endif /* MBEDTLS_HAVE_TIME */
1963
Gilles Peskine449bd832023-01-11 14:50:10 +01001964 return ret;
Jerry Yuf4b27e42022-03-30 17:32:21 +08001965}
1966
Jerry Yu3bf2c642022-03-30 22:02:12 +08001967/*
Jerry Yue74e04a2022-04-21 09:23:16 +08001968 * ssl_tls13_write_server_hello_supported_versions_ext ():
Jerry Yu3bf2c642022-03-30 22:02:12 +08001969 *
1970 * struct {
Jerry Yufb9f54d2022-04-06 10:08:34 +08001971 * ProtocolVersion selected_version;
Jerry Yu3bf2c642022-03-30 22:02:12 +08001972 * } SupportedVersions;
1973 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001974MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yue74e04a2022-04-21 09:23:16 +08001975static int ssl_tls13_write_server_hello_supported_versions_ext(
Gilles Peskine449bd832023-01-11 14:50:10 +01001976 mbedtls_ssl_context *ssl,
1977 unsigned char *buf,
1978 unsigned char *end,
1979 size_t *out_len)
Jerry Yu3bf2c642022-03-30 22:02:12 +08001980{
Jerry Yu3bf2c642022-03-30 22:02:12 +08001981 *out_len = 0;
1982
Gilles Peskine449bd832023-01-11 14:50:10 +01001983 MBEDTLS_SSL_DEBUG_MSG(3, ("server hello, write selected version"));
Jerry Yu3bf2c642022-03-30 22:02:12 +08001984
1985 /* Check if we have space to write the extension:
1986 * - extension_type (2 bytes)
1987 * - extension_data_length (2 bytes)
Jerry Yu349a6132022-04-14 20:52:56 +08001988 * - selected_version (2 bytes)
Jerry Yu3bf2c642022-03-30 22:02:12 +08001989 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001990 MBEDTLS_SSL_CHK_BUF_PTR(buf, end, 6);
Jerry Yu3bf2c642022-03-30 22:02:12 +08001991
Gilles Peskine449bd832023-01-11 14:50:10 +01001992 MBEDTLS_PUT_UINT16_BE(MBEDTLS_TLS_EXT_SUPPORTED_VERSIONS, buf, 0);
Jerry Yu3bf2c642022-03-30 22:02:12 +08001993
Gilles Peskine449bd832023-01-11 14:50:10 +01001994 MBEDTLS_PUT_UINT16_BE(2, buf, 2);
Jerry Yu3bf2c642022-03-30 22:02:12 +08001995
Gilles Peskine449bd832023-01-11 14:50:10 +01001996 mbedtls_ssl_write_version(buf + 4,
1997 ssl->conf->transport,
1998 ssl->tls_version);
Jerry Yu3bf2c642022-03-30 22:02:12 +08001999
Gilles Peskine449bd832023-01-11 14:50:10 +01002000 MBEDTLS_SSL_DEBUG_MSG(3, ("supported version: [%04x]",
2001 ssl->tls_version));
Jerry Yu3bf2c642022-03-30 22:02:12 +08002002
Jerry Yu349a6132022-04-14 20:52:56 +08002003 *out_len = 6;
Jerry Yu3bf2c642022-03-30 22:02:12 +08002004
Jerry Yub95dd362022-11-08 21:19:34 +08002005 mbedtls_ssl_tls13_set_hs_sent_ext_mask(
Gilles Peskine449bd832023-01-11 14:50:10 +01002006 ssl, MBEDTLS_TLS_EXT_SUPPORTED_VERSIONS);
Jerry Yub95dd362022-11-08 21:19:34 +08002007
Gilles Peskine449bd832023-01-11 14:50:10 +01002008 return 0;
Jerry Yu3bf2c642022-03-30 22:02:12 +08002009}
2010
Jerry Yud9436a12022-04-20 22:28:09 +08002011
Jerry Yu3bf2c642022-03-30 22:02:12 +08002012
2013/* Generate and export a single key share. For hybrid KEMs, this can
2014 * be called multiple times with the different components of the hybrid. */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002015MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002016static int ssl_tls13_generate_and_write_key_share(mbedtls_ssl_context *ssl,
2017 uint16_t named_group,
2018 unsigned char *buf,
2019 unsigned char *end,
2020 size_t *out_len)
Jerry Yu3bf2c642022-03-30 22:02:12 +08002021{
2022 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yu955ddd72022-04-22 22:27:33 +08002023
2024 *out_len = 0;
2025
Valerio Settic9ae8622023-07-25 11:23:50 +02002026#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_EPHEMERAL_ENABLED)
Przemek Stekiel29c219c2023-05-31 15:21:04 +02002027 if (mbedtls_ssl_tls13_named_group_is_ecdhe(named_group) ||
Przemek Stekield5f79e72023-06-29 09:08:43 +02002028 mbedtls_ssl_tls13_named_group_is_ffdh(named_group)) {
Przemek Stekiel408569f2023-07-06 11:26:44 +02002029 ret = mbedtls_ssl_tls13_generate_and_write_xxdh_key_exchange(
Gilles Peskine449bd832023-01-11 14:50:10 +01002030 ssl, named_group, buf, end, out_len);
2031 if (ret != 0) {
Jerry Yu89e103c2022-03-30 22:43:29 +08002032 MBEDTLS_SSL_DEBUG_RET(
Przemek Stekiel408569f2023-07-06 11:26:44 +02002033 1, "mbedtls_ssl_tls13_generate_and_write_xxdh_key_exchange",
Gilles Peskine449bd832023-01-11 14:50:10 +01002034 ret);
2035 return ret;
Jerry Yu3bf2c642022-03-30 22:02:12 +08002036 }
Gilles Peskine449bd832023-01-11 14:50:10 +01002037 } else
Valerio Settic9ae8622023-07-25 11:23:50 +02002038#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_EPHEMERAL_ENABLED */
Gilles Peskine449bd832023-01-11 14:50:10 +01002039 if (0 /* Other kinds of KEMs */) {
2040 } else {
Jerry Yu955ddd72022-04-22 22:27:33 +08002041 ((void) ssl);
2042 ((void) named_group);
2043 ((void) buf);
2044 ((void) end);
Jerry Yu3bf2c642022-03-30 22:02:12 +08002045 ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
2046 }
2047
Gilles Peskine449bd832023-01-11 14:50:10 +01002048 return ret;
Jerry Yu3bf2c642022-03-30 22:02:12 +08002049}
2050
2051/*
2052 * ssl_tls13_write_key_share_ext
2053 *
2054 * Structure of key_share extension in ServerHello:
2055 *
Jerry Yu1c3e6882022-04-20 21:23:40 +08002056 * struct {
2057 * NamedGroup group;
2058 * opaque key_exchange<1..2^16-1>;
2059 * } KeyShareEntry;
2060 * struct {
2061 * KeyShareEntry server_share;
2062 * } KeyShareServerHello;
Jerry Yu3bf2c642022-03-30 22:02:12 +08002063 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002064MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002065static int ssl_tls13_write_key_share_ext(mbedtls_ssl_context *ssl,
2066 unsigned char *buf,
2067 unsigned char *end,
2068 size_t *out_len)
Jerry Yu3bf2c642022-03-30 22:02:12 +08002069{
Jerry Yu955ddd72022-04-22 22:27:33 +08002070 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yu3bf2c642022-03-30 22:02:12 +08002071 unsigned char *p = buf;
Jerry Yu57d48412022-04-20 21:50:42 +08002072 uint16_t group = ssl->handshake->offered_group_id;
Jerry Yu3bf2c642022-03-30 22:02:12 +08002073 unsigned char *server_share = buf + 4;
Jerry Yu3bf2c642022-03-30 22:02:12 +08002074 size_t key_exchange_length;
Jerry Yu3bf2c642022-03-30 22:02:12 +08002075
2076 *out_len = 0;
2077
Gilles Peskine449bd832023-01-11 14:50:10 +01002078 MBEDTLS_SSL_DEBUG_MSG(3, ("server hello, adding key share extension"));
Jerry Yu3bf2c642022-03-30 22:02:12 +08002079
Gilles Peskine449bd832023-01-11 14:50:10 +01002080 MBEDTLS_SSL_DEBUG_MSG(2, ("server hello, write selected_group: %s (%04x)",
2081 mbedtls_ssl_named_group_to_str(group),
2082 group));
Jerry Yu58af2332022-09-06 11:19:31 +08002083
Jerry Yu3bf2c642022-03-30 22:02:12 +08002084 /* Check if we have space for header and length fields:
2085 * - extension_type (2 bytes)
2086 * - extension_data_length (2 bytes)
2087 * - group (2 bytes)
2088 * - key_exchange_length (2 bytes)
2089 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002090 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 8);
2091 MBEDTLS_PUT_UINT16_BE(MBEDTLS_TLS_EXT_KEY_SHARE, p, 0);
2092 MBEDTLS_PUT_UINT16_BE(group, server_share, 0);
Jerry Yu3bf2c642022-03-30 22:02:12 +08002093 p += 8;
Jerry Yu57d48412022-04-20 21:50:42 +08002094
Jerry Yu3bf2c642022-03-30 22:02:12 +08002095 /* When we introduce PQC-ECDHE hybrids, we'll want to call this
2096 * function multiple times. */
Jerry Yu955ddd72022-04-22 22:27:33 +08002097 ret = ssl_tls13_generate_and_write_key_share(
Gilles Peskine449bd832023-01-11 14:50:10 +01002098 ssl, group, server_share + 4, end, &key_exchange_length);
2099 if (ret != 0) {
2100 return ret;
2101 }
Jerry Yu3bf2c642022-03-30 22:02:12 +08002102 p += key_exchange_length;
Jerry Yuc1be19f2022-04-23 16:11:39 +08002103
Gilles Peskine449bd832023-01-11 14:50:10 +01002104 MBEDTLS_PUT_UINT16_BE(key_exchange_length, server_share + 2, 0);
Jerry Yu3bf2c642022-03-30 22:02:12 +08002105
Gilles Peskine449bd832023-01-11 14:50:10 +01002106 MBEDTLS_PUT_UINT16_BE(p - server_share, buf, 2);
Jerry Yu3bf2c642022-03-30 22:02:12 +08002107
Jerry Yu57d48412022-04-20 21:50:42 +08002108 *out_len = p - buf;
Jerry Yu955ddd72022-04-22 22:27:33 +08002109
Gilles Peskine449bd832023-01-11 14:50:10 +01002110 mbedtls_ssl_tls13_set_hs_sent_ext_mask(ssl, MBEDTLS_TLS_EXT_KEY_SHARE);
Jerry Yub95dd362022-11-08 21:19:34 +08002111
Gilles Peskine449bd832023-01-11 14:50:10 +01002112 return 0;
Jerry Yu3bf2c642022-03-30 22:02:12 +08002113}
Jerry Yud9436a12022-04-20 22:28:09 +08002114
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002115MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002116static int ssl_tls13_write_hrr_key_share_ext(mbedtls_ssl_context *ssl,
2117 unsigned char *buf,
2118 unsigned char *end,
2119 size_t *out_len)
Jerry Yu23f7a6f2022-04-23 15:16:45 +08002120{
2121 uint16_t selected_group = ssl->handshake->hrr_selected_group;
2122 /* key_share Extension
2123 *
2124 * struct {
2125 * select (Handshake.msg_type) {
2126 * ...
2127 * case hello_retry_request:
2128 * NamedGroup selected_group;
2129 * ...
2130 * };
2131 * } KeyShare;
2132 */
2133
2134 *out_len = 0;
2135
Jerry Yub0ac10b2022-05-05 11:10:08 +08002136 /*
2137 * For a pure PSK key exchange, there is no group to agree upon. The purpose
2138 * of the HRR is then to transmit a cookie to force the client to demonstrate
2139 * reachability at their apparent network address (primarily useful for DTLS).
2140 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002141 if (!mbedtls_ssl_tls13_key_exchange_mode_with_ephemeral(ssl)) {
2142 return 0;
2143 }
Jerry Yu23f7a6f2022-04-23 15:16:45 +08002144
2145 /* We should only send the key_share extension if the client's initial
2146 * key share was not acceptable. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002147 if (ssl->handshake->offered_group_id != 0) {
2148 MBEDTLS_SSL_DEBUG_MSG(4, ("Skip key_share extension in HRR"));
2149 return 0;
Jerry Yu23f7a6f2022-04-23 15:16:45 +08002150 }
2151
Gilles Peskine449bd832023-01-11 14:50:10 +01002152 if (selected_group == 0) {
2153 MBEDTLS_SSL_DEBUG_MSG(1, ("no matching named group found"));
2154 return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
Jerry Yu23f7a6f2022-04-23 15:16:45 +08002155 }
2156
Jerry Yub0ac10b2022-05-05 11:10:08 +08002157 /* Check if we have enough space:
2158 * - extension_type (2 bytes)
2159 * - extension_data_length (2 bytes)
2160 * - selected_group (2 bytes)
2161 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002162 MBEDTLS_SSL_CHK_BUF_PTR(buf, end, 6);
Jerry Yu23f7a6f2022-04-23 15:16:45 +08002163
Gilles Peskine449bd832023-01-11 14:50:10 +01002164 MBEDTLS_PUT_UINT16_BE(MBEDTLS_TLS_EXT_KEY_SHARE, buf, 0);
2165 MBEDTLS_PUT_UINT16_BE(2, buf, 2);
2166 MBEDTLS_PUT_UINT16_BE(selected_group, buf, 4);
Jerry Yu23f7a6f2022-04-23 15:16:45 +08002167
Gilles Peskine449bd832023-01-11 14:50:10 +01002168 MBEDTLS_SSL_DEBUG_MSG(3,
2169 ("HRR selected_group: %s (%x)",
2170 mbedtls_ssl_named_group_to_str(selected_group),
2171 selected_group));
Jerry Yu23f7a6f2022-04-23 15:16:45 +08002172
2173 *out_len = 6;
Jerry Yu23f7a6f2022-04-23 15:16:45 +08002174
Gilles Peskine449bd832023-01-11 14:50:10 +01002175 mbedtls_ssl_tls13_set_hs_sent_ext_mask(ssl, MBEDTLS_TLS_EXT_KEY_SHARE);
Jerry Yub95dd362022-11-08 21:19:34 +08002176
Gilles Peskine449bd832023-01-11 14:50:10 +01002177 return 0;
Jerry Yu23f7a6f2022-04-23 15:16:45 +08002178}
Jerry Yu3bf2c642022-03-30 22:02:12 +08002179
2180/*
2181 * Structure of ServerHello message:
2182 *
2183 * struct {
2184 * ProtocolVersion legacy_version = 0x0303; // TLS v1.2
2185 * Random random;
2186 * opaque legacy_session_id_echo<0..32>;
2187 * CipherSuite cipher_suite;
2188 * uint8 legacy_compression_method = 0;
2189 * Extension extensions<6..2^16-1>;
2190 * } ServerHello;
2191 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002192MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002193static int ssl_tls13_write_server_hello_body(mbedtls_ssl_context *ssl,
2194 unsigned char *buf,
2195 unsigned char *end,
2196 size_t *out_len,
2197 int is_hrr)
Jerry Yu56404d72022-03-30 17:36:13 +08002198{
Jerry Yu955ddd72022-04-22 22:27:33 +08002199 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yu3bf2c642022-03-30 22:02:12 +08002200 unsigned char *p = buf;
Jerry Yud9436a12022-04-20 22:28:09 +08002201 unsigned char *p_extensions_len;
Jerry Yufbe3e642022-04-25 19:31:51 +08002202 size_t output_len;
Jerry Yu3bf2c642022-03-30 22:02:12 +08002203
2204 *out_len = 0;
Jerry Yu50e00e32022-10-31 14:45:01 +08002205 ssl->handshake->sent_extensions = MBEDTLS_SSL_EXT_MASK_NONE;
Jerry Yu3bf2c642022-03-30 22:02:12 +08002206
Jerry Yucfc04b32022-04-21 09:31:58 +08002207 /* ...
2208 * ProtocolVersion legacy_version = 0x0303; // TLS 1.2
2209 * ...
2210 * with ProtocolVersion defined as:
2211 * uint16 ProtocolVersion;
Jerry Yu3bf2c642022-03-30 22:02:12 +08002212 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002213 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 2);
2214 MBEDTLS_PUT_UINT16_BE(0x0303, p, 0);
Jerry Yu3bf2c642022-03-30 22:02:12 +08002215 p += 2;
2216
Jerry Yu1c3e6882022-04-20 21:23:40 +08002217 /* ...
2218 * Random random;
2219 * ...
Jerry Yucfc04b32022-04-21 09:31:58 +08002220 * with Random defined as:
2221 * opaque Random[MBEDTLS_SERVER_HELLO_RANDOM_LEN];
Jerry Yu1c3e6882022-04-20 21:23:40 +08002222 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002223 MBEDTLS_SSL_CHK_BUF_PTR(p, end, MBEDTLS_SERVER_HELLO_RANDOM_LEN);
2224 if (is_hrr) {
2225 memcpy(p, mbedtls_ssl_tls13_hello_retry_request_magic,
2226 MBEDTLS_SERVER_HELLO_RANDOM_LEN);
2227 } else {
2228 memcpy(p, &ssl->handshake->randbytes[MBEDTLS_CLIENT_HELLO_RANDOM_LEN],
2229 MBEDTLS_SERVER_HELLO_RANDOM_LEN);
Jerry Yu23f7a6f2022-04-23 15:16:45 +08002230 }
Gilles Peskine449bd832023-01-11 14:50:10 +01002231 MBEDTLS_SSL_DEBUG_BUF(3, "server hello, random bytes",
2232 p, MBEDTLS_SERVER_HELLO_RANDOM_LEN);
Jerry Yu3bf2c642022-03-30 22:02:12 +08002233 p += MBEDTLS_SERVER_HELLO_RANDOM_LEN;
2234
Jerry Yucfc04b32022-04-21 09:31:58 +08002235 /* ...
2236 * opaque legacy_session_id_echo<0..32>;
2237 * ...
Jerry Yu3bf2c642022-03-30 22:02:12 +08002238 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002239 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 1 + ssl->session_negotiate->id_len);
2240 *p++ = (unsigned char) ssl->session_negotiate->id_len;
2241 if (ssl->session_negotiate->id_len > 0) {
2242 memcpy(p, &ssl->session_negotiate->id[0],
2243 ssl->session_negotiate->id_len);
Jerry Yu3bf2c642022-03-30 22:02:12 +08002244 p += ssl->session_negotiate->id_len;
Jerry Yu955ddd72022-04-22 22:27:33 +08002245
Gilles Peskine449bd832023-01-11 14:50:10 +01002246 MBEDTLS_SSL_DEBUG_BUF(3, "session id", ssl->session_negotiate->id,
2247 ssl->session_negotiate->id_len);
Jerry Yu3bf2c642022-03-30 22:02:12 +08002248 }
2249
Jerry Yucfc04b32022-04-21 09:31:58 +08002250 /* ...
2251 * CipherSuite cipher_suite;
2252 * ...
2253 * with CipherSuite defined as:
2254 * uint8 CipherSuite[2];
Jerry Yu3bf2c642022-03-30 22:02:12 +08002255 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002256 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 2);
2257 MBEDTLS_PUT_UINT16_BE(ssl->session_negotiate->ciphersuite, p, 0);
Jerry Yu3bf2c642022-03-30 22:02:12 +08002258 p += 2;
Gilles Peskine449bd832023-01-11 14:50:10 +01002259 MBEDTLS_SSL_DEBUG_MSG(3,
2260 ("server hello, chosen ciphersuite: %s ( id=%d )",
2261 mbedtls_ssl_get_ciphersuite_name(
2262 ssl->session_negotiate->ciphersuite),
2263 ssl->session_negotiate->ciphersuite));
Jerry Yu3bf2c642022-03-30 22:02:12 +08002264
Jerry Yucfc04b32022-04-21 09:31:58 +08002265 /* ...
2266 * uint8 legacy_compression_method = 0;
2267 * ...
2268 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002269 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 1);
Thomas Daubney31e03a82022-07-25 15:59:25 +01002270 *p++ = MBEDTLS_SSL_COMPRESS_NULL;
Jerry Yu3bf2c642022-03-30 22:02:12 +08002271
Jerry Yucfc04b32022-04-21 09:31:58 +08002272 /* ...
2273 * Extension extensions<6..2^16-1>;
2274 * ...
2275 * struct {
2276 * ExtensionType extension_type; (2 bytes)
2277 * opaque extension_data<0..2^16-1>;
2278 * } Extension;
2279 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002280 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 2);
Jerry Yud9436a12022-04-20 22:28:09 +08002281 p_extensions_len = p;
Jerry Yu3bf2c642022-03-30 22:02:12 +08002282 p += 2;
2283
Gilles Peskine449bd832023-01-11 14:50:10 +01002284 if ((ret = ssl_tls13_write_server_hello_supported_versions_ext(
2285 ssl, p, end, &output_len)) != 0) {
Jerry Yu955ddd72022-04-22 22:27:33 +08002286 MBEDTLS_SSL_DEBUG_RET(
Gilles Peskine449bd832023-01-11 14:50:10 +01002287 1, "ssl_tls13_write_server_hello_supported_versions_ext", ret);
2288 return ret;
Jerry Yu3bf2c642022-03-30 22:02:12 +08002289 }
2290 p += output_len;
2291
Gilles Peskine449bd832023-01-11 14:50:10 +01002292 if (mbedtls_ssl_tls13_key_exchange_mode_with_ephemeral(ssl)) {
2293 if (is_hrr) {
2294 ret = ssl_tls13_write_hrr_key_share_ext(ssl, p, end, &output_len);
2295 } else {
2296 ret = ssl_tls13_write_key_share_ext(ssl, p, end, &output_len);
2297 }
2298 if (ret != 0) {
2299 return ret;
2300 }
Jerry Yu3bf2c642022-03-30 22:02:12 +08002301 p += output_len;
2302 }
Jerry Yu3bf2c642022-03-30 22:02:12 +08002303
Ronald Cron41a443a2022-10-04 16:38:25 +02002304#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01002305 if (!is_hrr && mbedtls_ssl_tls13_key_exchange_mode_with_psk(ssl)) {
2306 ret = ssl_tls13_write_server_pre_shared_key_ext(ssl, p, end, &output_len);
2307 if (ret != 0) {
2308 MBEDTLS_SSL_DEBUG_RET(1, "ssl_tls13_write_server_pre_shared_key_ext",
2309 ret);
2310 return ret;
Jerry Yu032b15ce2022-07-11 06:10:03 +00002311 }
2312 p += output_len;
2313 }
Ronald Cron41a443a2022-10-04 16:38:25 +02002314#endif
Jerry Yu032b15ce2022-07-11 06:10:03 +00002315
Gilles Peskine449bd832023-01-11 14:50:10 +01002316 MBEDTLS_PUT_UINT16_BE(p - p_extensions_len - 2, p_extensions_len, 0);
Jerry Yu3bf2c642022-03-30 22:02:12 +08002317
Gilles Peskine449bd832023-01-11 14:50:10 +01002318 MBEDTLS_SSL_DEBUG_BUF(4, "server hello extensions",
2319 p_extensions_len, p - p_extensions_len);
Jerry Yu3bf2c642022-03-30 22:02:12 +08002320
Jerry Yud9436a12022-04-20 22:28:09 +08002321 *out_len = p - buf;
Jerry Yu3bf2c642022-03-30 22:02:12 +08002322
Gilles Peskine449bd832023-01-11 14:50:10 +01002323 MBEDTLS_SSL_DEBUG_BUF(3, "server hello", buf, *out_len);
Jerry Yu3bf2c642022-03-30 22:02:12 +08002324
Jerry Yu7de2ff02022-11-08 21:43:46 +08002325 MBEDTLS_SSL_PRINT_EXTS(
Jerry Yu4b8f2f72022-10-31 13:31:22 +08002326 3, is_hrr ? MBEDTLS_SSL_TLS1_3_HS_HELLO_RETRY_REQUEST :
Gilles Peskine449bd832023-01-11 14:50:10 +01002327 MBEDTLS_SSL_HS_SERVER_HELLO,
2328 ssl->handshake->sent_extensions);
Jerry Yu4b8f2f72022-10-31 13:31:22 +08002329
Gilles Peskine449bd832023-01-11 14:50:10 +01002330 return ret;
Jerry Yu56404d72022-03-30 17:36:13 +08002331}
2332
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002333MBEDTLS_CHECK_RETURN_CRITICAL
Xiaokang Qian934ce6f2023-02-06 10:23:04 +00002334static int ssl_tls13_finalize_server_hello(mbedtls_ssl_context *ssl)
Jerry Yue110d252022-05-05 10:19:22 +08002335{
2336 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine449bd832023-01-11 14:50:10 +01002337 ret = mbedtls_ssl_tls13_compute_handshake_transform(ssl);
2338 if (ret != 0) {
2339 MBEDTLS_SSL_DEBUG_RET(1,
2340 "mbedtls_ssl_tls13_compute_handshake_transform",
2341 ret);
2342 return ret;
Jerry Yue110d252022-05-05 10:19:22 +08002343 }
2344
Gilles Peskine449bd832023-01-11 14:50:10 +01002345 return ret;
Jerry Yue110d252022-05-05 10:19:22 +08002346}
2347
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002348MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002349static int ssl_tls13_write_server_hello(mbedtls_ssl_context *ssl)
Jerry Yu5b64ae92022-03-30 17:15:02 +08002350{
Jerry Yu637a3f12022-04-20 21:37:58 +08002351 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yuf4b27e42022-03-30 17:32:21 +08002352 unsigned char *buf;
2353 size_t buf_len, msg_len;
2354
Gilles Peskine449bd832023-01-11 14:50:10 +01002355 MBEDTLS_SSL_DEBUG_MSG(2, ("=> write server hello"));
Jerry Yuf4b27e42022-03-30 17:32:21 +08002356
Gilles Peskine449bd832023-01-11 14:50:10 +01002357 MBEDTLS_SSL_PROC_CHK(ssl_tls13_prepare_server_hello(ssl));
Jerry Yuf4b27e42022-03-30 17:32:21 +08002358
Xiaokang Qian9f1747b2023-03-30 02:50:04 +00002359 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_start_handshake_msg(
2360 ssl, MBEDTLS_SSL_HS_SERVER_HELLO, &buf, &buf_len));
Jerry Yuf4b27e42022-03-30 17:32:21 +08002361
Gilles Peskine449bd832023-01-11 14:50:10 +01002362 MBEDTLS_SSL_PROC_CHK(ssl_tls13_write_server_hello_body(ssl, buf,
2363 buf + buf_len,
2364 &msg_len,
2365 0));
Jerry Yuf4b27e42022-03-30 17:32:21 +08002366
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +01002367 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_add_hs_msg_to_checksum(
Manuel Pégourié-Gonnard43cc1272023-02-06 11:48:19 +01002368 ssl, MBEDTLS_SSL_HS_SERVER_HELLO, buf, msg_len));
Jerry Yuf4b27e42022-03-30 17:32:21 +08002369
Gilles Peskine449bd832023-01-11 14:50:10 +01002370 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_finish_handshake_msg(
2371 ssl, buf_len, msg_len));
Jerry Yu637a3f12022-04-20 21:37:58 +08002372
Xiaokang Qian934ce6f2023-02-06 10:23:04 +00002373 MBEDTLS_SSL_PROC_CHK(ssl_tls13_finalize_server_hello(ssl));
Jerry Yue110d252022-05-05 10:19:22 +08002374
Gabor Mezei7b39bf12022-05-24 16:04:14 +02002375#if defined(MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE)
2376 /* The server sends a dummy change_cipher_spec record immediately
2377 * after its first handshake message. This may either be after
2378 * a ServerHello or a HelloRetryRequest.
2379 */
Gabor Mezei96ae9262022-06-28 11:45:18 +02002380 mbedtls_ssl_handshake_set_state(
Gilles Peskine449bd832023-01-11 14:50:10 +01002381 ssl, MBEDTLS_SSL_SERVER_CCS_AFTER_SERVER_HELLO);
Gabor Mezei7b39bf12022-05-24 16:04:14 +02002382#else
Gilles Peskine449bd832023-01-11 14:50:10 +01002383 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_ENCRYPTED_EXTENSIONS);
Gabor Mezei7b39bf12022-05-24 16:04:14 +02002384#endif /* MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE */
Jerry Yue110d252022-05-05 10:19:22 +08002385
Jerry Yuf4b27e42022-03-30 17:32:21 +08002386cleanup:
2387
Gilles Peskine449bd832023-01-11 14:50:10 +01002388 MBEDTLS_SSL_DEBUG_MSG(2, ("<= write server hello"));
2389 return ret;
Jerry Yu5b64ae92022-03-30 17:15:02 +08002390}
2391
Jerry Yu23d1a252022-05-12 18:08:59 +08002392
2393/*
2394 * Handler for MBEDTLS_SSL_HELLO_RETRY_REQUEST
2395 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002396MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002397static int ssl_tls13_prepare_hello_retry_request(mbedtls_ssl_context *ssl)
Jerry Yu23d1a252022-05-12 18:08:59 +08002398{
2399 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Ronald Cron5fbd2702024-02-14 10:03:36 +01002400 if (ssl->handshake->hello_retry_request_flag) {
Gilles Peskine449bd832023-01-11 14:50:10 +01002401 MBEDTLS_SSL_DEBUG_MSG(1, ("Too many HRRs"));
2402 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE,
2403 MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE);
2404 return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
Jerry Yu23d1a252022-05-12 18:08:59 +08002405 }
2406
2407 /*
2408 * Create stateless transcript hash for HRR
2409 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002410 MBEDTLS_SSL_DEBUG_MSG(4, ("Reset transcript for HRR"));
2411 ret = mbedtls_ssl_reset_transcript_for_hrr(ssl);
2412 if (ret != 0) {
2413 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_reset_transcript_for_hrr", ret);
2414 return ret;
Jerry Yu23d1a252022-05-12 18:08:59 +08002415 }
Gilles Peskine449bd832023-01-11 14:50:10 +01002416 mbedtls_ssl_session_reset_msg_layer(ssl, 0);
Jerry Yu23d1a252022-05-12 18:08:59 +08002417
Gilles Peskine449bd832023-01-11 14:50:10 +01002418 return 0;
Jerry Yu23d1a252022-05-12 18:08:59 +08002419}
2420
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002421MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002422static int ssl_tls13_write_hello_retry_request(mbedtls_ssl_context *ssl)
Jerry Yu23d1a252022-05-12 18:08:59 +08002423{
2424 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
2425 unsigned char *buf;
2426 size_t buf_len, msg_len;
2427
Gilles Peskine449bd832023-01-11 14:50:10 +01002428 MBEDTLS_SSL_DEBUG_MSG(2, ("=> write hello retry request"));
Jerry Yu23d1a252022-05-12 18:08:59 +08002429
Gilles Peskine449bd832023-01-11 14:50:10 +01002430 MBEDTLS_SSL_PROC_CHK(ssl_tls13_prepare_hello_retry_request(ssl));
Jerry Yu23d1a252022-05-12 18:08:59 +08002431
Gilles Peskine449bd832023-01-11 14:50:10 +01002432 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_start_handshake_msg(
2433 ssl, MBEDTLS_SSL_HS_SERVER_HELLO,
2434 &buf, &buf_len));
Jerry Yu23d1a252022-05-12 18:08:59 +08002435
Gilles Peskine449bd832023-01-11 14:50:10 +01002436 MBEDTLS_SSL_PROC_CHK(ssl_tls13_write_server_hello_body(ssl, buf,
2437 buf + buf_len,
2438 &msg_len,
2439 1));
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +01002440 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_add_hs_msg_to_checksum(
Manuel Pégourié-Gonnard43cc1272023-02-06 11:48:19 +01002441 ssl, MBEDTLS_SSL_HS_SERVER_HELLO, buf, msg_len));
Jerry Yu23d1a252022-05-12 18:08:59 +08002442
2443
Gilles Peskine449bd832023-01-11 14:50:10 +01002444 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_finish_handshake_msg(ssl, buf_len,
2445 msg_len));
Jerry Yu23d1a252022-05-12 18:08:59 +08002446
Ronald Cron5fbd2702024-02-14 10:03:36 +01002447 ssl->handshake->hello_retry_request_flag = 1;
Jerry Yu23d1a252022-05-12 18:08:59 +08002448
Gabor Mezei7b39bf12022-05-24 16:04:14 +02002449#if defined(MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE)
2450 /* The server sends a dummy change_cipher_spec record immediately
2451 * after its first handshake message. This may either be after
2452 * a ServerHello or a HelloRetryRequest.
2453 */
Gabor Mezei96ae9262022-06-28 11:45:18 +02002454 mbedtls_ssl_handshake_set_state(
Gilles Peskine449bd832023-01-11 14:50:10 +01002455 ssl, MBEDTLS_SSL_SERVER_CCS_AFTER_HELLO_RETRY_REQUEST);
Gabor Mezei7b39bf12022-05-24 16:04:14 +02002456#else
Gilles Peskine449bd832023-01-11 14:50:10 +01002457 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_CLIENT_HELLO);
Gabor Mezei7b39bf12022-05-24 16:04:14 +02002458#endif /* MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE */
Jerry Yu23d1a252022-05-12 18:08:59 +08002459
2460cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +01002461 MBEDTLS_SSL_DEBUG_MSG(2, ("<= write hello retry request"));
2462 return ret;
Jerry Yu23d1a252022-05-12 18:08:59 +08002463}
2464
Jerry Yu5b64ae92022-03-30 17:15:02 +08002465/*
Jerry Yu4d3841a2022-04-16 12:37:19 +08002466 * Handler for MBEDTLS_SSL_ENCRYPTED_EXTENSIONS
2467 */
Jerry Yu4d3841a2022-04-16 12:37:19 +08002468
2469/*
2470 * struct {
2471 * Extension extensions<0..2 ^ 16 - 1>;
2472 * } EncryptedExtensions;
2473 *
2474 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002475MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002476static int ssl_tls13_write_encrypted_extensions_body(mbedtls_ssl_context *ssl,
2477 unsigned char *buf,
2478 unsigned char *end,
2479 size_t *out_len)
Jerry Yu4d3841a2022-04-16 12:37:19 +08002480{
XiaokangQianacb39922022-06-17 10:18:48 +00002481 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yu4d3841a2022-04-16 12:37:19 +08002482 unsigned char *p = buf;
2483 size_t extensions_len = 0;
Jerry Yu8937eb42022-05-03 12:12:14 +08002484 unsigned char *p_extensions_len;
XiaokangQianacb39922022-06-17 10:18:48 +00002485 size_t output_len;
Jerry Yu9da5e5a2022-05-03 15:46:09 +08002486
Jerry Yu4d3841a2022-04-16 12:37:19 +08002487 *out_len = 0;
2488
Gilles Peskine449bd832023-01-11 14:50:10 +01002489 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 2);
Jerry Yu8937eb42022-05-03 12:12:14 +08002490 p_extensions_len = p;
Jerry Yu4d3841a2022-04-16 12:37:19 +08002491 p += 2;
2492
Jerry Yu8937eb42022-05-03 12:12:14 +08002493 ((void) ssl);
XiaokangQianacb39922022-06-17 10:18:48 +00002494 ((void) ret);
2495 ((void) output_len);
2496
2497#if defined(MBEDTLS_SSL_ALPN)
Gilles Peskine449bd832023-01-11 14:50:10 +01002498 ret = mbedtls_ssl_write_alpn_ext(ssl, p, end, &output_len);
2499 if (ret != 0) {
2500 return ret;
2501 }
XiaokangQian95d5f542022-06-24 02:29:26 +00002502 p += output_len;
XiaokangQianacb39922022-06-17 10:18:48 +00002503#endif /* MBEDTLS_SSL_ALPN */
Jerry Yu4d3841a2022-04-16 12:37:19 +08002504
Jerry Yu71c14f12022-12-12 11:10:35 +08002505#if defined(MBEDTLS_SSL_EARLY_DATA)
Ronald Cron78a38f62024-02-01 18:30:31 +01002506 if (ssl->handshake->early_data_accepted) {
Jerry Yu52335392023-11-23 18:06:06 +08002507 ret = mbedtls_ssl_tls13_write_early_data_ext(
Jerry Yuc59c5862023-12-05 10:40:49 +08002508 ssl, 0, p, end, &output_len);
Jerry Yu71c14f12022-12-12 11:10:35 +08002509 if (ret != 0) {
2510 return ret;
2511 }
2512 p += output_len;
2513 }
2514#endif /* MBEDTLS_SSL_EARLY_DATA */
2515
Waleed Elmelegy47d29462024-01-03 17:31:52 +00002516#if defined(MBEDTLS_SSL_RECORD_SIZE_LIMIT)
Waleed Elmelegyfbe42742024-01-05 18:11:10 +00002517 if (ssl->handshake->received_extensions & MBEDTLS_SSL_EXT_MASK(RECORD_SIZE_LIMIT)) {
Waleed Elmelegyd2fc90e2024-01-04 18:04:53 +00002518 ret = mbedtls_ssl_tls13_write_record_size_limit_ext(
2519 ssl, p, end, &output_len);
2520 if (ret != 0) {
2521 return ret;
2522 }
2523 p += output_len;
Waleed Elmelegy47d29462024-01-03 17:31:52 +00002524 }
Waleed Elmelegy47d29462024-01-03 17:31:52 +00002525#endif
2526
Gilles Peskine449bd832023-01-11 14:50:10 +01002527 extensions_len = (p - p_extensions_len) - 2;
2528 MBEDTLS_PUT_UINT16_BE(extensions_len, p_extensions_len, 0);
Jerry Yu8937eb42022-05-03 12:12:14 +08002529
2530 *out_len = p - buf;
Jerry Yu4d3841a2022-04-16 12:37:19 +08002531
Gilles Peskine449bd832023-01-11 14:50:10 +01002532 MBEDTLS_SSL_DEBUG_BUF(4, "encrypted extensions", buf, *out_len);
Jerry Yu4d3841a2022-04-16 12:37:19 +08002533
Jerry Yu7de2ff02022-11-08 21:43:46 +08002534 MBEDTLS_SSL_PRINT_EXTS(
Gilles Peskine449bd832023-01-11 14:50:10 +01002535 3, MBEDTLS_SSL_HS_ENCRYPTED_EXTENSIONS, ssl->handshake->sent_extensions);
Jerry Yu4b8f2f72022-10-31 13:31:22 +08002536
Gilles Peskine449bd832023-01-11 14:50:10 +01002537 return 0;
Jerry Yu4d3841a2022-04-16 12:37:19 +08002538}
2539
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002540MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002541static int ssl_tls13_write_encrypted_extensions(mbedtls_ssl_context *ssl)
Jerry Yu4d3841a2022-04-16 12:37:19 +08002542{
2543 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
2544 unsigned char *buf;
Jerry Yu39730a72022-05-03 12:14:04 +08002545 size_t buf_len, msg_len;
Jerry Yu4d3841a2022-04-16 12:37:19 +08002546
Gilles Peskine449bd832023-01-11 14:50:10 +01002547 mbedtls_ssl_set_outbound_transform(ssl,
2548 ssl->handshake->transform_handshake);
Gabor Mezei54719122022-06-28 11:34:56 +02002549 MBEDTLS_SSL_DEBUG_MSG(
Gilles Peskine449bd832023-01-11 14:50:10 +01002550 3, ("switching to handshake transform for outbound data"));
Gabor Mezei54719122022-06-28 11:34:56 +02002551
Gilles Peskine449bd832023-01-11 14:50:10 +01002552 MBEDTLS_SSL_DEBUG_MSG(2, ("=> write encrypted extensions"));
Jerry Yu4d3841a2022-04-16 12:37:19 +08002553
Xiaokang Qian9f1747b2023-03-30 02:50:04 +00002554 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_start_handshake_msg(
2555 ssl, MBEDTLS_SSL_HS_ENCRYPTED_EXTENSIONS,
2556 &buf, &buf_len));
Jerry Yu4d3841a2022-04-16 12:37:19 +08002557
Gilles Peskine449bd832023-01-11 14:50:10 +01002558 MBEDTLS_SSL_PROC_CHK(ssl_tls13_write_encrypted_extensions_body(
2559 ssl, buf, buf + buf_len, &msg_len));
Jerry Yu4d3841a2022-04-16 12:37:19 +08002560
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +01002561 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_add_hs_msg_to_checksum(
Xiaokang Qian9f1747b2023-03-30 02:50:04 +00002562 ssl, MBEDTLS_SSL_HS_ENCRYPTED_EXTENSIONS,
2563 buf, msg_len));
Jerry Yu4d3841a2022-04-16 12:37:19 +08002564
Gilles Peskine449bd832023-01-11 14:50:10 +01002565 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_finish_handshake_msg(
2566 ssl, buf_len, msg_len));
Jerry Yu4d3841a2022-04-16 12:37:19 +08002567
Ronald Cron928cbd32022-10-04 16:14:26 +02002568#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01002569 if (mbedtls_ssl_tls13_key_exchange_mode_with_psk(ssl)) {
2570 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_SERVER_FINISHED);
2571 } else {
2572 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_CERTIFICATE_REQUEST);
2573 }
Jerry Yu8937eb42022-05-03 12:12:14 +08002574#else
Gilles Peskine449bd832023-01-11 14:50:10 +01002575 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_SERVER_FINISHED);
Jerry Yu8937eb42022-05-03 12:12:14 +08002576#endif
2577
Jerry Yu4d3841a2022-04-16 12:37:19 +08002578cleanup:
2579
Gilles Peskine449bd832023-01-11 14:50:10 +01002580 MBEDTLS_SSL_DEBUG_MSG(2, ("<= write encrypted extensions"));
2581 return ret;
Jerry Yu4d3841a2022-04-16 12:37:19 +08002582}
2583
Ronald Cron928cbd32022-10-04 16:14:26 +02002584#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED)
XiaokangQiana987e1d2022-05-07 01:25:58 +00002585#define SSL_CERTIFICATE_REQUEST_SEND_REQUEST 0
2586#define SSL_CERTIFICATE_REQUEST_SKIP 1
2587/* Coordination:
2588 * Check whether a CertificateRequest message should be written.
2589 * Returns a negative code on failure, or
2590 * - SSL_CERTIFICATE_REQUEST_SEND_REQUEST
2591 * - SSL_CERTIFICATE_REQUEST_SKIP
2592 * indicating if the writing of the CertificateRequest
2593 * should be skipped or not.
2594 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002595MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002596static int ssl_tls13_certificate_request_coordinate(mbedtls_ssl_context *ssl)
XiaokangQiana987e1d2022-05-07 01:25:58 +00002597{
2598 int authmode;
2599
XiaokangQian40a35232022-05-07 09:02:40 +00002600#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Gilles Peskine449bd832023-01-11 14:50:10 +01002601 if (ssl->handshake->sni_authmode != MBEDTLS_SSL_VERIFY_UNSET) {
XiaokangQian40a35232022-05-07 09:02:40 +00002602 authmode = ssl->handshake->sni_authmode;
Gilles Peskine449bd832023-01-11 14:50:10 +01002603 } else
XiaokangQian40a35232022-05-07 09:02:40 +00002604#endif
XiaokangQiana987e1d2022-05-07 01:25:58 +00002605 authmode = ssl->conf->authmode;
2606
Gilles Peskine449bd832023-01-11 14:50:10 +01002607 if (authmode == MBEDTLS_SSL_VERIFY_NONE) {
Ronald Croneac00ad2022-09-13 10:16:31 +02002608 ssl->session_negotiate->verify_result = MBEDTLS_X509_BADCERT_SKIP_VERIFY;
Gilles Peskine449bd832023-01-11 14:50:10 +01002609 return SSL_CERTIFICATE_REQUEST_SKIP;
Ronald Croneac00ad2022-09-13 10:16:31 +02002610 }
XiaokangQiana987e1d2022-05-07 01:25:58 +00002611
XiaokangQianc3017f62022-05-13 05:55:41 +00002612 ssl->handshake->certificate_request_sent = 1;
XiaokangQian189ded22022-05-10 08:12:17 +00002613
Gilles Peskine449bd832023-01-11 14:50:10 +01002614 return SSL_CERTIFICATE_REQUEST_SEND_REQUEST;
XiaokangQiana987e1d2022-05-07 01:25:58 +00002615}
2616
XiaokangQiancec9ae62022-05-06 07:28:50 +00002617/*
2618 * struct {
2619 * opaque certificate_request_context<0..2^8-1>;
2620 * Extension extensions<2..2^16-1>;
2621 * } CertificateRequest;
2622 *
2623 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002624MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002625static int ssl_tls13_write_certificate_request_body(mbedtls_ssl_context *ssl,
2626 unsigned char *buf,
2627 const unsigned char *end,
2628 size_t *out_len)
XiaokangQiancec9ae62022-05-06 07:28:50 +00002629{
2630 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
2631 unsigned char *p = buf;
XiaokangQianec6efb92022-05-06 09:53:10 +00002632 size_t output_len = 0;
XiaokangQiancec9ae62022-05-06 07:28:50 +00002633 unsigned char *p_extensions_len;
2634
2635 *out_len = 0;
2636
2637 /* Check if we have enough space:
2638 * - certificate_request_context (1 byte)
2639 * - extensions length (2 bytes)
2640 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002641 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 3);
XiaokangQiancec9ae62022-05-06 07:28:50 +00002642
2643 /*
2644 * Write certificate_request_context
2645 */
2646 /*
2647 * We use a zero length context for the normal handshake
2648 * messages. For post-authentication handshake messages
2649 * this request context would be set to a non-zero value.
2650 */
2651 *p++ = 0x0;
2652
2653 /*
2654 * Write extensions
2655 */
2656 /* The extensions must contain the signature_algorithms. */
2657 p_extensions_len = p;
2658 p += 2;
Gilles Peskine449bd832023-01-11 14:50:10 +01002659 ret = mbedtls_ssl_write_sig_alg_ext(ssl, p, end, &output_len);
2660 if (ret != 0) {
2661 return ret;
2662 }
XiaokangQiancec9ae62022-05-06 07:28:50 +00002663
XiaokangQianec6efb92022-05-06 09:53:10 +00002664 p += output_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01002665 MBEDTLS_PUT_UINT16_BE(p - p_extensions_len - 2, p_extensions_len, 0);
XiaokangQiancec9ae62022-05-06 07:28:50 +00002666
2667 *out_len = p - buf;
2668
Jerry Yu7de2ff02022-11-08 21:43:46 +08002669 MBEDTLS_SSL_PRINT_EXTS(
Gilles Peskine449bd832023-01-11 14:50:10 +01002670 3, MBEDTLS_SSL_HS_CERTIFICATE_REQUEST, ssl->handshake->sent_extensions);
Jerry Yu4b8f2f72022-10-31 13:31:22 +08002671
Gilles Peskine449bd832023-01-11 14:50:10 +01002672 return 0;
XiaokangQiancec9ae62022-05-06 07:28:50 +00002673}
2674
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002675MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002676static int ssl_tls13_write_certificate_request(mbedtls_ssl_context *ssl)
XiaokangQiancec9ae62022-05-06 07:28:50 +00002677{
2678 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
2679
Gilles Peskine449bd832023-01-11 14:50:10 +01002680 MBEDTLS_SSL_DEBUG_MSG(2, ("=> write certificate request"));
XiaokangQiancec9ae62022-05-06 07:28:50 +00002681
Gilles Peskine449bd832023-01-11 14:50:10 +01002682 MBEDTLS_SSL_PROC_CHK_NEG(ssl_tls13_certificate_request_coordinate(ssl));
XiaokangQiancec9ae62022-05-06 07:28:50 +00002683
Gilles Peskine449bd832023-01-11 14:50:10 +01002684 if (ret == SSL_CERTIFICATE_REQUEST_SEND_REQUEST) {
XiaokangQiancec9ae62022-05-06 07:28:50 +00002685 unsigned char *buf;
2686 size_t buf_len, msg_len;
2687
Xiaokang Qian9f1747b2023-03-30 02:50:04 +00002688 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_start_handshake_msg(
2689 ssl, MBEDTLS_SSL_HS_CERTIFICATE_REQUEST,
2690 &buf, &buf_len));
XiaokangQiancec9ae62022-05-06 07:28:50 +00002691
Gilles Peskine449bd832023-01-11 14:50:10 +01002692 MBEDTLS_SSL_PROC_CHK(ssl_tls13_write_certificate_request_body(
2693 ssl, buf, buf + buf_len, &msg_len));
XiaokangQiancec9ae62022-05-06 07:28:50 +00002694
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +01002695 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_add_hs_msg_to_checksum(
Xiaokang Qian9f1747b2023-03-30 02:50:04 +00002696 ssl, MBEDTLS_SSL_HS_CERTIFICATE_REQUEST,
2697 buf, msg_len));
XiaokangQiancec9ae62022-05-06 07:28:50 +00002698
Gilles Peskine449bd832023-01-11 14:50:10 +01002699 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_finish_handshake_msg(
2700 ssl, buf_len, msg_len));
2701 } else if (ret == SSL_CERTIFICATE_REQUEST_SKIP) {
2702 MBEDTLS_SSL_DEBUG_MSG(2, ("<= skip write certificate request"));
XiaokangQiancec9ae62022-05-06 07:28:50 +00002703 ret = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01002704 } else {
2705 MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
XiaokangQiancec9ae62022-05-06 07:28:50 +00002706 ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
2707 goto cleanup;
2708 }
2709
Gilles Peskine449bd832023-01-11 14:50:10 +01002710 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_SERVER_CERTIFICATE);
XiaokangQiancec9ae62022-05-06 07:28:50 +00002711cleanup:
2712
Gilles Peskine449bd832023-01-11 14:50:10 +01002713 MBEDTLS_SSL_DEBUG_MSG(2, ("<= write certificate request"));
2714 return ret;
XiaokangQiancec9ae62022-05-06 07:28:50 +00002715}
XiaokangQiancec9ae62022-05-06 07:28:50 +00002716
Jerry Yu4d3841a2022-04-16 12:37:19 +08002717/*
Jerry Yuc6e6dbf2022-04-16 19:42:57 +08002718 * Handler for MBEDTLS_SSL_SERVER_CERTIFICATE
Jerry Yu83da34e2022-04-16 13:59:52 +08002719 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002720MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002721static int ssl_tls13_write_server_certificate(mbedtls_ssl_context *ssl)
Jerry Yu83da34e2022-04-16 13:59:52 +08002722{
Jerry Yu5a26f302022-05-10 20:46:40 +08002723 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
XiaokangQianfb665a82022-06-15 03:57:21 +00002724
2725#if defined(MBEDTLS_X509_CRT_PARSE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01002726 if ((ssl_tls13_pick_key_cert(ssl) != 0) ||
2727 mbedtls_ssl_own_cert(ssl) == NULL) {
2728 MBEDTLS_SSL_DEBUG_MSG(2, ("No certificate available."));
2729 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE,
2730 MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE);
2731 return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
Jerry Yu5a26f302022-05-10 20:46:40 +08002732 }
XiaokangQianfb665a82022-06-15 03:57:21 +00002733#endif /* MBEDTLS_X509_CRT_PARSE_C */
Jerry Yu5a26f302022-05-10 20:46:40 +08002734
Gilles Peskine449bd832023-01-11 14:50:10 +01002735 ret = mbedtls_ssl_tls13_write_certificate(ssl);
2736 if (ret != 0) {
2737 return ret;
2738 }
2739 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_CERTIFICATE_VERIFY);
2740 return 0;
Jerry Yu83da34e2022-04-16 13:59:52 +08002741}
2742
2743/*
Jerry Yuc6e6dbf2022-04-16 19:42:57 +08002744 * Handler for MBEDTLS_SSL_CERTIFICATE_VERIFY
Jerry Yu83da34e2022-04-16 13:59:52 +08002745 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002746MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002747static int ssl_tls13_write_certificate_verify(mbedtls_ssl_context *ssl)
Jerry Yu83da34e2022-04-16 13:59:52 +08002748{
Gilles Peskine449bd832023-01-11 14:50:10 +01002749 int ret = mbedtls_ssl_tls13_write_certificate_verify(ssl);
2750 if (ret != 0) {
2751 return ret;
2752 }
2753 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_SERVER_FINISHED);
2754 return 0;
Jerry Yu83da34e2022-04-16 13:59:52 +08002755}
Ronald Cron928cbd32022-10-04 16:14:26 +02002756#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED */
Jerry Yu83da34e2022-04-16 13:59:52 +08002757
Jerry Yu9b72e392023-12-01 16:27:08 +08002758/*
2759 * RFC 8446 section A.2
2760 *
2761 * | Send ServerHello
2762 * | K_send = handshake
2763 * | Send EncryptedExtensions
2764 * | [Send CertificateRequest]
2765 * Can send | [Send Certificate + CertificateVerify]
2766 * app data | Send Finished
2767 * after --> | K_send = application
2768 * here +--------+--------+
2769 * No 0-RTT | | 0-RTT
2770 * | |
2771 * K_recv = handshake | | K_recv = early data
2772 * [Skip decrypt errors] | +------> WAIT_EOED -+
2773 * | | Recv | | Recv EndOfEarlyData
2774 * | | early data | | K_recv = handshake
2775 * | +------------+ |
2776 * | |
2777 * +> WAIT_FLIGHT2 <--------+
2778 * |
2779 * +--------+--------+
2780 * No auth | | Client auth
2781 * | |
2782 * | v
2783 * | WAIT_CERT
2784 * | Recv | | Recv Certificate
2785 * | empty | v
2786 * | Certificate | WAIT_CV
2787 * | | | Recv
2788 * | v | CertificateVerify
2789 * +-> WAIT_FINISHED <---+
2790 * | Recv Finished
2791 *
2792 *
2793 * The following function handles the state changes after WAIT_FLIGHT2 in the
Jerry Yu3be85072023-12-04 09:58:54 +08002794 * above diagram. We are not going to receive early data related messages
2795 * anymore, prepare to receive the first handshake message of the client
2796 * second flight.
Jerry Yu9b72e392023-12-01 16:27:08 +08002797 */
Jerry Yu3be85072023-12-04 09:58:54 +08002798static void ssl_tls13_prepare_for_handshake_second_flight(
2799 mbedtls_ssl_context *ssl)
Jerry Yu9b72e392023-12-01 16:27:08 +08002800{
Jerry Yu9b72e392023-12-01 16:27:08 +08002801 if (ssl->handshake->certificate_request_sent) {
2802 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_CLIENT_CERTIFICATE);
2803 } else {
Jerry Yu42020fb2023-12-05 17:35:53 +08002804 MBEDTLS_SSL_DEBUG_MSG(2, ("skip parse certificate"));
Jerry Yuebb1b1d2023-12-05 11:02:15 +08002805 MBEDTLS_SSL_DEBUG_MSG(2, ("skip parse certificate verify"));
Jerry Yuebb1b1d2023-12-05 11:02:15 +08002806
Jerry Yu9b72e392023-12-01 16:27:08 +08002807 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_CLIENT_FINISHED);
2808 }
Jerry Yu9b72e392023-12-01 16:27:08 +08002809}
2810
Jerry Yu83da34e2022-04-16 13:59:52 +08002811/*
Jerry Yud6e253d2022-05-18 13:59:24 +08002812 * Handler for MBEDTLS_SSL_SERVER_FINISHED
Jerry Yu69dd8d42022-04-16 12:51:26 +08002813 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002814MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002815static int ssl_tls13_write_server_finished(mbedtls_ssl_context *ssl)
Jerry Yu69dd8d42022-04-16 12:51:26 +08002816{
Jerry Yud6e253d2022-05-18 13:59:24 +08002817 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yu27bdc7c2022-04-16 13:33:27 +08002818
Gilles Peskine449bd832023-01-11 14:50:10 +01002819 ret = mbedtls_ssl_tls13_write_finished_message(ssl);
2820 if (ret != 0) {
2821 return ret;
2822 }
Jerry Yu27bdc7c2022-04-16 13:33:27 +08002823
Gilles Peskine449bd832023-01-11 14:50:10 +01002824 ret = mbedtls_ssl_tls13_compute_application_transform(ssl);
2825 if (ret != 0) {
Jerry Yue3d67cb2022-05-19 15:33:10 +08002826 MBEDTLS_SSL_PEND_FATAL_ALERT(
Gilles Peskine449bd832023-01-11 14:50:10 +01002827 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE,
2828 MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE);
2829 return ret;
Jerry Yue3d67cb2022-05-19 15:33:10 +08002830 }
XiaokangQianc3017f62022-05-13 05:55:41 +00002831
Jerry Yu87b5ed42022-12-12 13:07:07 +08002832#if defined(MBEDTLS_SSL_EARLY_DATA)
Ronald Cron78a38f62024-02-01 18:30:31 +01002833 if (ssl->handshake->early_data_accepted) {
Jerry Yu0af63dc2023-12-01 17:14:51 +08002834 /* See RFC 8446 section A.2 for more information */
Jerry Yu87b5ed42022-12-12 13:07:07 +08002835 MBEDTLS_SSL_DEBUG_MSG(
2836 1, ("Switch to early keys for inbound traffic. "
2837 "( K_recv = early data )"));
2838 mbedtls_ssl_set_inbound_transform(
2839 ssl, ssl->handshake->transform_earlydata);
2840 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_END_OF_EARLY_DATA);
2841 return 0;
2842 }
2843#endif /* MBEDTLS_SSL_EARLY_DATA */
Jerry Yu0af63dc2023-12-01 17:14:51 +08002844 MBEDTLS_SSL_DEBUG_MSG(
2845 1, ("Switch to handshake keys for inbound traffic "
2846 "( K_recv = handshake )"));
Gilles Peskine449bd832023-01-11 14:50:10 +01002847 mbedtls_ssl_set_inbound_transform(ssl, ssl->handshake->transform_handshake);
XiaokangQian189ded22022-05-10 08:12:17 +00002848
Jerry Yu3be85072023-12-04 09:58:54 +08002849 ssl_tls13_prepare_for_handshake_second_flight(ssl);
Jerry Yu7d8c3fe2022-12-12 12:59:44 +08002850
2851 return 0;
2852}
2853
Jerry Yu87b5ed42022-12-12 13:07:07 +08002854#if defined(MBEDTLS_SSL_EARLY_DATA)
2855/*
Jerry Yu59d420f2023-12-01 16:30:34 +08002856 * Handler for MBEDTLS_SSL_END_OF_EARLY_DATA
Jerry Yu87b5ed42022-12-12 13:07:07 +08002857 */
Jerry Yu3be85072023-12-04 09:58:54 +08002858#define SSL_GOT_END_OF_EARLY_DATA 0
Jerry Yub55f9eb2023-12-05 10:27:17 +08002859#define SSL_GOT_EARLY_DATA 1
Jerry Yud5c34962023-12-01 16:32:31 +08002860/* Coordination:
Jerry Yu3be85072023-12-04 09:58:54 +08002861 * Deals with the ambiguity of not knowing if the next message is an
2862 * EndOfEarlyData message or an application message containing early data.
Jerry Yud5c34962023-12-01 16:32:31 +08002863 * Returns a negative code on failure, or
Jerry Yu3be85072023-12-04 09:58:54 +08002864 * - SSL_GOT_END_OF_EARLY_DATA
Jerry Yub55f9eb2023-12-05 10:27:17 +08002865 * - SSL_GOT_EARLY_DATA
Jerry Yud5c34962023-12-01 16:32:31 +08002866 * indicating which message is received.
2867 */
2868MBEDTLS_CHECK_RETURN_CRITICAL
2869static int ssl_tls13_end_of_early_data_coordinate(mbedtls_ssl_context *ssl)
2870{
2871 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yub4ed4602023-12-01 16:34:00 +08002872
2873 if ((ret = mbedtls_ssl_read_record(ssl, 0)) != 0) {
2874 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_read_record", ret);
2875 return ret;
2876 }
2877 ssl->keep_current_message = 1;
2878
2879 if (ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
2880 ssl->in_msg[0] == MBEDTLS_SSL_HS_END_OF_EARLY_DATA) {
Jerry Yub55f9eb2023-12-05 10:27:17 +08002881 MBEDTLS_SSL_DEBUG_MSG(3, ("Received an end_of_early_data message."));
Jerry Yu3be85072023-12-04 09:58:54 +08002882 return SSL_GOT_END_OF_EARLY_DATA;
Jerry Yub4ed4602023-12-01 16:34:00 +08002883 }
2884
2885 if (ssl->in_msgtype == MBEDTLS_SSL_MSG_APPLICATION_DATA) {
Jerry Yub55f9eb2023-12-05 10:27:17 +08002886 MBEDTLS_SSL_DEBUG_MSG(3, ("Received early data"));
Jerry Yu6a5904d2023-12-06 17:11:12 +08002887 /* RFC 8446 section 4.6.1
2888 *
2889 * A server receiving more than max_early_data_size bytes of 0-RTT data
2890 * SHOULD terminate the connection with an "unexpected_message" alert.
2891 *
2892 * TODO: Add received data size check here.
2893 */
Ronald Croned7d4bf2024-01-31 07:55:19 +01002894 if (ssl->in_offt == NULL) {
2895 /* Set the reading pointer */
2896 ssl->in_offt = ssl->in_msg;
2897 }
Jerry Yub55f9eb2023-12-05 10:27:17 +08002898 return SSL_GOT_EARLY_DATA;
Jerry Yub4ed4602023-12-01 16:34:00 +08002899 }
2900
Jerry Yu7bb40a32023-12-04 10:04:15 +08002901 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE,
2902 MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE);
Jerry Yub4ed4602023-12-01 16:34:00 +08002903 return MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE;
Jerry Yud5c34962023-12-01 16:32:31 +08002904}
2905
2906MBEDTLS_CHECK_RETURN_CRITICAL
2907static int ssl_tls13_parse_end_of_early_data(mbedtls_ssl_context *ssl,
2908 const unsigned char *buf,
2909 const unsigned char *end)
2910{
Jerry Yu75c9ab72023-12-01 16:41:40 +08002911 /* RFC 8446 section 4.5
2912 *
2913 * struct {} EndOfEarlyData;
2914 */
Jerry Yufbf03992023-12-04 10:00:37 +08002915 if (buf != end) {
2916 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR,
2917 MBEDTLS_ERR_SSL_DECODE_ERROR);
2918 return MBEDTLS_ERR_SSL_DECODE_ERROR;
2919 }
2920 return 0;
Jerry Yud5c34962023-12-01 16:32:31 +08002921}
2922
Jerry Yud5c34962023-12-01 16:32:31 +08002923/*
2924 * RFC 8446 section A.2
2925 *
2926 * | Send ServerHello
2927 * | K_send = handshake
2928 * | Send EncryptedExtensions
2929 * | [Send CertificateRequest]
2930 * Can send | [Send Certificate + CertificateVerify]
2931 * app data | Send Finished
2932 * after --> | K_send = application
2933 * here +--------+--------+
2934 * No 0-RTT | | 0-RTT
2935 * | |
2936 * K_recv = handshake | | K_recv = early data
2937 * [Skip decrypt errors] | +------> WAIT_EOED -+
2938 * | | Recv | | Recv EndOfEarlyData
2939 * | | early data | | K_recv = handshake
2940 * | +------------+ |
2941 * | |
2942 * +> WAIT_FLIGHT2 <--------+
2943 * |
2944 * +--------+--------+
2945 * No auth | | Client auth
2946 * | |
2947 * | v
2948 * | WAIT_CERT
2949 * | Recv | | Recv Certificate
2950 * | empty | v
2951 * | Certificate | WAIT_CV
2952 * | | | Recv
2953 * | v | CertificateVerify
2954 * +-> WAIT_FINISHED <---+
2955 * | Recv Finished
2956 *
2957 * The function handles actions and state changes from 0-RTT to WAIT_FLIGHT2 in
2958 * the above diagram.
2959 */
Jerry Yu87b5ed42022-12-12 13:07:07 +08002960MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yu59d420f2023-12-01 16:30:34 +08002961static int ssl_tls13_process_end_of_early_data(mbedtls_ssl_context *ssl)
Jerry Yu87b5ed42022-12-12 13:07:07 +08002962{
2963 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yud5c34962023-12-01 16:32:31 +08002964
2965 MBEDTLS_SSL_DEBUG_MSG(2, ("=> ssl_tls13_process_end_of_early_data"));
2966
2967 MBEDTLS_SSL_PROC_CHK_NEG(ssl_tls13_end_of_early_data_coordinate(ssl));
2968
Jerry Yu3be85072023-12-04 09:58:54 +08002969 if (ret == SSL_GOT_END_OF_EARLY_DATA) {
Jerry Yud5c34962023-12-01 16:32:31 +08002970 unsigned char *buf;
2971 size_t buf_len;
2972
2973 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_tls13_fetch_handshake_msg(
2974 ssl, MBEDTLS_SSL_HS_END_OF_EARLY_DATA,
2975 &buf, &buf_len));
2976
2977 MBEDTLS_SSL_PROC_CHK(ssl_tls13_parse_end_of_early_data(
2978 ssl, buf, buf + buf_len));
2979
Jerry Yue9655122023-12-01 16:44:40 +08002980 MBEDTLS_SSL_DEBUG_MSG(
2981 1, ("Switch to handshake keys for inbound traffic"
2982 "( K_recv = handshake )"));
2983 mbedtls_ssl_set_inbound_transform(
2984 ssl, ssl->handshake->transform_handshake);
2985
Jerry Yud5c34962023-12-01 16:32:31 +08002986 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_add_hs_msg_to_checksum(
2987 ssl, MBEDTLS_SSL_HS_END_OF_EARLY_DATA,
2988 buf, buf_len));
Jerry Yue9655122023-12-01 16:44:40 +08002989
Jerry Yu3be85072023-12-04 09:58:54 +08002990 ssl_tls13_prepare_for_handshake_second_flight(ssl);
Jerry Yud5c34962023-12-01 16:32:31 +08002991
Jerry Yub55f9eb2023-12-05 10:27:17 +08002992 } else if (ret == SSL_GOT_EARLY_DATA) {
Jerry Yud9ca3542023-12-06 17:23:52 +08002993 ret = MBEDTLS_ERR_SSL_RECEIVED_EARLY_DATA;
2994 goto cleanup;
Jerry Yud5c34962023-12-01 16:32:31 +08002995 } else {
2996 MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
2997 ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
2998 goto cleanup;
2999 }
3000
Jerry Yud5c34962023-12-01 16:32:31 +08003001cleanup:
3002 MBEDTLS_SSL_DEBUG_MSG(2, ("<= ssl_tls13_process_end_of_early_data"));
Jerry Yu87b5ed42022-12-12 13:07:07 +08003003 return ret;
3004}
3005#endif /* MBEDTLS_SSL_EARLY_DATA */
3006
Jerry Yu69dd8d42022-04-16 12:51:26 +08003007/*
Jerry Yud6e253d2022-05-18 13:59:24 +08003008 * Handler for MBEDTLS_SSL_CLIENT_FINISHED
Jerry Yu69dd8d42022-04-16 12:51:26 +08003009 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02003010MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01003011static int ssl_tls13_process_client_finished(mbedtls_ssl_context *ssl)
Jerry Yu69dd8d42022-04-16 12:51:26 +08003012{
Jerry Yud6e253d2022-05-18 13:59:24 +08003013 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
3014
Gilles Peskine449bd832023-01-11 14:50:10 +01003015 ret = mbedtls_ssl_tls13_process_finished_message(ssl);
3016 if (ret != 0) {
3017 return ret;
Jerry Yue3d67cb2022-05-19 15:33:10 +08003018 }
3019
Gilles Peskine449bd832023-01-11 14:50:10 +01003020 ret = mbedtls_ssl_tls13_compute_resumption_master_secret(ssl);
3021 if (ret != 0) {
Xiaokang Qian9f1747b2023-03-30 02:50:04 +00003022 MBEDTLS_SSL_DEBUG_RET(
3023 1, "mbedtls_ssl_tls13_compute_resumption_master_secret", ret);
Gilles Peskine449bd832023-01-11 14:50:10 +01003024 }
3025
3026 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_HANDSHAKE_WRAPUP);
3027 return 0;
Jerry Yu69dd8d42022-04-16 12:51:26 +08003028}
3029
3030/*
Jerry Yud6e253d2022-05-18 13:59:24 +08003031 * Handler for MBEDTLS_SSL_HANDSHAKE_WRAPUP
Jerry Yu69dd8d42022-04-16 12:51:26 +08003032 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02003033MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01003034static int ssl_tls13_handshake_wrapup(mbedtls_ssl_context *ssl)
Jerry Yu69dd8d42022-04-16 12:51:26 +08003035{
Gilles Peskine449bd832023-01-11 14:50:10 +01003036 MBEDTLS_SSL_DEBUG_MSG(2, ("handshake: done"));
Jerry Yu03ed50b2022-04-16 17:13:30 +08003037
Gilles Peskine449bd832023-01-11 14:50:10 +01003038 mbedtls_ssl_tls13_handshake_wrapup(ssl);
Jerry Yue67bef42022-07-07 07:29:42 +00003039
Pengyu Lv3643fdb2023-01-12 16:46:28 +08003040#if defined(MBEDTLS_SSL_SESSION_TICKETS) && \
3041 defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED)
Pengyu Lva1aa31b2022-12-13 13:49:59 +08003042/* TODO: Remove the check of SOME_PSK_ENABLED since SESSION_TICKETS requires
3043 * SOME_PSK_ENABLED to be enabled. Here is just to make CI happy. It is
3044 * expected to be resolved with issue#6395.
3045 */
Pengyu Lvc7af2c42022-12-01 16:33:00 +08003046 /* Sent NewSessionTicket message only when client supports PSK */
Pengyu Lvb2cfafb2023-11-14 13:56:13 +08003047 if (mbedtls_ssl_tls13_is_some_psk_supported(ssl)) {
Xiaokang Qian9f1747b2023-03-30 02:50:04 +00003048 mbedtls_ssl_handshake_set_state(
3049 ssl, MBEDTLS_SSL_TLS1_3_NEW_SESSION_TICKET);
Pengyu Lvc7af2c42022-12-01 16:33:00 +08003050 } else
Jerry Yufca4d572022-07-21 10:37:48 +08003051#endif
Pengyu Lv3643fdb2023-01-12 16:46:28 +08003052 {
3053 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_HANDSHAKE_OVER);
3054 }
Gilles Peskine449bd832023-01-11 14:50:10 +01003055 return 0;
Jerry Yu69dd8d42022-04-16 12:51:26 +08003056}
3057
3058/*
Jerry Yua8d3c502022-10-30 14:51:23 +08003059 * Handler for MBEDTLS_SSL_TLS1_3_NEW_SESSION_TICKET
Jerry Yue67bef42022-07-07 07:29:42 +00003060 */
3061#define SSL_NEW_SESSION_TICKET_SKIP 0
3062#define SSL_NEW_SESSION_TICKET_WRITE 1
3063MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01003064static int ssl_tls13_write_new_session_ticket_coordinate(mbedtls_ssl_context *ssl)
Jerry Yue67bef42022-07-07 07:29:42 +00003065{
3066 /* Check whether the use of session tickets is enabled */
Gilles Peskine449bd832023-01-11 14:50:10 +01003067 if (ssl->conf->f_ticket_write == NULL) {
3068 MBEDTLS_SSL_DEBUG_MSG(2, ("NewSessionTicket: disabled,"
3069 " callback is not set"));
3070 return SSL_NEW_SESSION_TICKET_SKIP;
Jerry Yud0766ec2022-09-22 10:46:57 +08003071 }
Gilles Peskine449bd832023-01-11 14:50:10 +01003072 if (ssl->conf->new_session_tickets_count == 0) {
3073 MBEDTLS_SSL_DEBUG_MSG(2, ("NewSessionTicket: disabled,"
3074 " configured count is zero"));
3075 return SSL_NEW_SESSION_TICKET_SKIP;
Jerry Yud0766ec2022-09-22 10:46:57 +08003076 }
3077
Gilles Peskine449bd832023-01-11 14:50:10 +01003078 if (ssl->handshake->new_session_tickets_count == 0) {
3079 MBEDTLS_SSL_DEBUG_MSG(2, ("NewSessionTicket: all tickets have "
3080 "been sent."));
3081 return SSL_NEW_SESSION_TICKET_SKIP;
Jerry Yue67bef42022-07-07 07:29:42 +00003082 }
3083
Gilles Peskine449bd832023-01-11 14:50:10 +01003084 return SSL_NEW_SESSION_TICKET_WRITE;
Jerry Yue67bef42022-07-07 07:29:42 +00003085}
3086
3087#if defined(MBEDTLS_SSL_SESSION_TICKETS)
3088MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01003089static int ssl_tls13_prepare_new_session_ticket(mbedtls_ssl_context *ssl,
3090 unsigned char *ticket_nonce,
3091 size_t ticket_nonce_size)
Jerry Yue67bef42022-07-07 07:29:42 +00003092{
3093 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
3094 mbedtls_ssl_session *session = ssl->session;
3095 mbedtls_ssl_ciphersuite_t *ciphersuite_info;
3096 psa_algorithm_t psa_hash_alg;
3097 int hash_length;
3098
Gilles Peskine449bd832023-01-11 14:50:10 +01003099 MBEDTLS_SSL_DEBUG_MSG(2, ("=> prepare NewSessionTicket msg"));
Jerry Yue67bef42022-07-07 07:29:42 +00003100
Pengyu Lv9f926952022-11-17 15:22:33 +08003101 /* Set ticket_flags depends on the advertised psk key exchange mode */
Pengyu Lv94a42cc2023-12-06 10:04:17 +08003102 mbedtls_ssl_tls13_session_clear_ticket_flags(
Pengyu Lv9eacb442022-12-09 14:39:19 +08003103 session, MBEDTLS_SSL_TLS1_3_TICKET_FLAGS_MASK);
Pengyu Lve6487fe2022-12-06 09:30:29 +08003104#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED)
Pengyu Lv94a42cc2023-12-06 10:04:17 +08003105 mbedtls_ssl_tls13_session_set_ticket_flags(
Pengyu Lv9eacb442022-12-09 14:39:19 +08003106 session, ssl->handshake->tls13_kex_modes);
Pengyu Lve6487fe2022-12-06 09:30:29 +08003107#endif
Jerry Yu95648b02023-12-06 15:03:34 +08003108
3109#if defined(MBEDTLS_SSL_EARLY_DATA)
3110 if (ssl->conf->early_data_enabled == MBEDTLS_SSL_EARLY_DATA_ENABLED &&
3111 ssl->conf->max_early_data_size > 0) {
Pengyu Lv94a42cc2023-12-06 10:04:17 +08003112 mbedtls_ssl_tls13_session_set_ticket_flags(
Jerry Yu95648b02023-12-06 15:03:34 +08003113 session, MBEDTLS_SSL_TLS1_3_TICKET_ALLOW_EARLY_DATA);
3114 }
3115#endif /* MBEDTLS_SSL_EARLY_DATA */
3116
Pengyu Lv4938a562023-01-16 11:28:49 +08003117 MBEDTLS_SSL_PRINT_TICKET_FLAGS(4, session->ticket_flags);
Pengyu Lv9f926952022-11-17 15:22:33 +08003118
Jerry Yue67bef42022-07-07 07:29:42 +00003119 /* Generate ticket_age_add */
Gilles Peskine449bd832023-01-11 14:50:10 +01003120 if ((ret = ssl->conf->f_rng(ssl->conf->p_rng,
3121 (unsigned char *) &session->ticket_age_add,
3122 sizeof(session->ticket_age_add)) != 0)) {
3123 MBEDTLS_SSL_DEBUG_RET(1, "generate_ticket_age_add", ret);
3124 return ret;
Jerry Yue67bef42022-07-07 07:29:42 +00003125 }
Gilles Peskine449bd832023-01-11 14:50:10 +01003126 MBEDTLS_SSL_DEBUG_MSG(3, ("ticket_age_add: %u",
3127 (unsigned int) session->ticket_age_add));
Jerry Yue67bef42022-07-07 07:29:42 +00003128
3129 /* Generate ticket_nonce */
Gilles Peskine449bd832023-01-11 14:50:10 +01003130 ret = ssl->conf->f_rng(ssl->conf->p_rng, ticket_nonce, ticket_nonce_size);
3131 if (ret != 0) {
3132 MBEDTLS_SSL_DEBUG_RET(1, "generate_ticket_nonce", ret);
3133 return ret;
Jerry Yue67bef42022-07-07 07:29:42 +00003134 }
Gilles Peskine449bd832023-01-11 14:50:10 +01003135 MBEDTLS_SSL_DEBUG_BUF(3, "ticket_nonce:",
3136 ticket_nonce, ticket_nonce_size);
Jerry Yue67bef42022-07-07 07:29:42 +00003137
3138 ciphersuite_info =
Gilles Peskine449bd832023-01-11 14:50:10 +01003139 (mbedtls_ssl_ciphersuite_t *) ssl->handshake->ciphersuite_info;
Dave Rodgman2eab4622023-10-05 13:30:37 +01003140 psa_hash_alg = mbedtls_md_psa_alg_from_type((mbedtls_md_type_t) ciphersuite_info->mac);
Gilles Peskine449bd832023-01-11 14:50:10 +01003141 hash_length = PSA_HASH_LENGTH(psa_hash_alg);
3142 if (hash_length == -1 ||
3143 (size_t) hash_length > sizeof(session->resumption_key)) {
3144 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Jerry Yufca4d572022-07-21 10:37:48 +08003145 }
3146
Jerry Yue67bef42022-07-07 07:29:42 +00003147 /* In this code the psk key length equals the length of the hash */
3148 session->resumption_key_len = hash_length;
3149 session->ciphersuite = ciphersuite_info->id;
3150
3151 /* Compute resumption key
3152 *
3153 * HKDF-Expand-Label( resumption_master_secret,
3154 * "resumption", ticket_nonce, Hash.length )
3155 */
3156 ret = mbedtls_ssl_tls13_hkdf_expand_label(
Gilles Peskine449bd832023-01-11 14:50:10 +01003157 psa_hash_alg,
3158 session->app_secrets.resumption_master_secret,
3159 hash_length,
3160 MBEDTLS_SSL_TLS1_3_LBL_WITH_LEN(resumption),
3161 ticket_nonce,
3162 ticket_nonce_size,
3163 session->resumption_key,
3164 hash_length);
Jerry Yue67bef42022-07-07 07:29:42 +00003165
Gilles Peskine449bd832023-01-11 14:50:10 +01003166 if (ret != 0) {
3167 MBEDTLS_SSL_DEBUG_RET(2,
3168 "Creating the ticket-resumed PSK failed",
3169 ret);
3170 return ret;
Jerry Yue67bef42022-07-07 07:29:42 +00003171 }
Gilles Peskine449bd832023-01-11 14:50:10 +01003172 MBEDTLS_SSL_DEBUG_BUF(3, "Ticket-resumed PSK",
3173 session->resumption_key,
3174 session->resumption_key_len);
Jerry Yue67bef42022-07-07 07:29:42 +00003175
Gilles Peskine449bd832023-01-11 14:50:10 +01003176 MBEDTLS_SSL_DEBUG_BUF(3, "resumption_master_secret",
3177 session->app_secrets.resumption_master_secret,
3178 hash_length);
Jerry Yue67bef42022-07-07 07:29:42 +00003179
Gilles Peskine449bd832023-01-11 14:50:10 +01003180 return 0;
Jerry Yue67bef42022-07-07 07:29:42 +00003181}
3182
3183/* This function creates a NewSessionTicket message in the following format:
3184 *
3185 * struct {
3186 * uint32 ticket_lifetime;
3187 * uint32 ticket_age_add;
3188 * opaque ticket_nonce<0..255>;
3189 * opaque ticket<1..2^16-1>;
3190 * Extension extensions<0..2^16-2>;
3191 * } NewSessionTicket;
3192 *
3193 * The ticket inside the NewSessionTicket message is an encrypted container
3194 * carrying the necessary information so that the server is later able to
3195 * re-start the communication.
3196 *
3197 * The following fields are placed inside the ticket by the
3198 * f_ticket_write() function:
3199 *
Jerry Yu0069abc2023-11-23 21:07:28 +08003200 * - creation time (ticket_creation_time)
3201 * - flags (ticket_flags)
Jerry Yue67bef42022-07-07 07:29:42 +00003202 * - age add (ticket_age_add)
Jerry Yu0069abc2023-11-23 21:07:28 +08003203 * - key (resumption_key)
3204 * - key length (resumption_key_len)
Jerry Yue67bef42022-07-07 07:29:42 +00003205 * - ciphersuite (ciphersuite)
Jerry Yu0069abc2023-11-23 21:07:28 +08003206 * - max_early_data_size (max_early_data_size)
Jerry Yue67bef42022-07-07 07:29:42 +00003207 */
3208MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01003209static int ssl_tls13_write_new_session_ticket_body(mbedtls_ssl_context *ssl,
3210 unsigned char *buf,
3211 unsigned char *end,
3212 size_t *out_len,
3213 unsigned char *ticket_nonce,
3214 size_t ticket_nonce_size)
Jerry Yue67bef42022-07-07 07:29:42 +00003215{
3216 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
3217 unsigned char *p = buf;
3218 mbedtls_ssl_session *session = ssl->session;
3219 size_t ticket_len;
3220 uint32_t ticket_lifetime;
Jerry Yu01da35e2022-12-12 15:09:22 +08003221 unsigned char *p_extensions_len;
Jerry Yue67bef42022-07-07 07:29:42 +00003222
3223 *out_len = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01003224 MBEDTLS_SSL_DEBUG_MSG(2, ("=> write NewSessionTicket msg"));
Jerry Yue67bef42022-07-07 07:29:42 +00003225
3226 /*
3227 * ticket_lifetime 4 bytes
3228 * ticket_age_add 4 bytes
3229 * ticket_nonce 1 + ticket_nonce_size bytes
3230 * ticket >=2 bytes
3231 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003232 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 4 + 4 + 1 + ticket_nonce_size + 2);
Jerry Yue67bef42022-07-07 07:29:42 +00003233
3234 /* Generate ticket and ticket_lifetime */
Ronald Cron3c0072b2023-11-22 10:00:14 +01003235#if defined(MBEDTLS_HAVE_TIME)
3236 session->ticket_creation_time = mbedtls_ms_time();
3237#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01003238 ret = ssl->conf->f_ticket_write(ssl->conf->p_ticket,
3239 session,
3240 p + 9 + ticket_nonce_size + 2,
3241 end,
3242 &ticket_len,
3243 &ticket_lifetime);
3244 if (ret != 0) {
3245 MBEDTLS_SSL_DEBUG_RET(1, "write_ticket", ret);
3246 return ret;
Jerry Yue67bef42022-07-07 07:29:42 +00003247 }
3248 /* RFC 8446 4.6.1
3249 * ticket_lifetime: Indicates the lifetime in seconds as a 32-bit
3250 * unsigned integer in network byte order from the time of ticket
3251 * issuance. Servers MUST NOT use any value greater than
3252 * 604800 seconds (7 days). The value of zero indicates that the
3253 * ticket should be discarded immediately. Clients MUST NOT cache
3254 * tickets for longer than 7 days, regardless of the ticket_lifetime,
3255 * and MAY delete tickets earlier based on local policy. A server
3256 * MAY treat a ticket as valid for a shorter period of time than what
3257 * is stated in the ticket_lifetime.
3258 */
Jerry Yu8e0174a2023-11-10 13:58:16 +08003259 if (ticket_lifetime > MBEDTLS_SSL_TLS1_3_MAX_ALLOWED_TICKET_LIFETIME) {
3260 ticket_lifetime = MBEDTLS_SSL_TLS1_3_MAX_ALLOWED_TICKET_LIFETIME;
Gilles Peskine449bd832023-01-11 14:50:10 +01003261 }
3262 MBEDTLS_PUT_UINT32_BE(ticket_lifetime, p, 0);
3263 MBEDTLS_SSL_DEBUG_MSG(3, ("ticket_lifetime: %u",
3264 (unsigned int) ticket_lifetime));
Jerry Yue67bef42022-07-07 07:29:42 +00003265
3266 /* Write ticket_age_add */
Gilles Peskine449bd832023-01-11 14:50:10 +01003267 MBEDTLS_PUT_UINT32_BE(session->ticket_age_add, p, 4);
3268 MBEDTLS_SSL_DEBUG_MSG(3, ("ticket_age_add: %u",
3269 (unsigned int) session->ticket_age_add));
Jerry Yue67bef42022-07-07 07:29:42 +00003270
3271 /* Write ticket_nonce */
Gilles Peskine449bd832023-01-11 14:50:10 +01003272 p[8] = (unsigned char) ticket_nonce_size;
3273 if (ticket_nonce_size > 0) {
3274 memcpy(p + 9, ticket_nonce, ticket_nonce_size);
Jerry Yue67bef42022-07-07 07:29:42 +00003275 }
3276 p += 9 + ticket_nonce_size;
3277
3278 /* Write ticket */
Gilles Peskine449bd832023-01-11 14:50:10 +01003279 MBEDTLS_PUT_UINT16_BE(ticket_len, p, 0);
Jerry Yue67bef42022-07-07 07:29:42 +00003280 p += 2;
Gilles Peskine449bd832023-01-11 14:50:10 +01003281 MBEDTLS_SSL_DEBUG_BUF(4, "ticket", p, ticket_len);
Jerry Yue67bef42022-07-07 07:29:42 +00003282 p += ticket_len;
3283
3284 /* Ticket Extensions
3285 *
Jerry Yu01da35e2022-12-12 15:09:22 +08003286 * Extension extensions<0..2^16-2>;
Jerry Yue67bef42022-07-07 07:29:42 +00003287 */
Jerry Yuedab6372022-10-31 14:37:31 +08003288 ssl->handshake->sent_extensions = MBEDTLS_SSL_EXT_MASK_NONE;
3289
Gilles Peskine449bd832023-01-11 14:50:10 +01003290 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 2);
Jerry Yu01da35e2022-12-12 15:09:22 +08003291 p_extensions_len = p;
Jerry Yue67bef42022-07-07 07:29:42 +00003292 p += 2;
3293
Jerry Yu01da35e2022-12-12 15:09:22 +08003294#if defined(MBEDTLS_SSL_EARLY_DATA)
Pengyu Lv94a42cc2023-12-06 10:04:17 +08003295 if (mbedtls_ssl_tls13_session_ticket_allow_early_data(session)) {
Jerry Yu95648b02023-12-06 15:03:34 +08003296 size_t output_len;
3297
Jerry Yuf135bac2023-11-23 18:10:51 +08003298 if ((ret = mbedtls_ssl_tls13_write_early_data_ext(
Jerry Yuc59c5862023-12-05 10:40:49 +08003299 ssl, 1, p, end, &output_len)) != 0) {
Jerry Yuf135bac2023-11-23 18:10:51 +08003300 MBEDTLS_SSL_DEBUG_RET(
3301 1, "mbedtls_ssl_tls13_write_early_data_ext", ret);
3302 return ret;
3303 }
3304 p += output_len;
Jerry Yu9e7f9bc2023-11-27 16:52:07 +08003305 } else {
3306 MBEDTLS_SSL_DEBUG_MSG(
3307 4, ("early_data not allowed, "
3308 "skip early_data extension in NewSessionTicket"));
Jerry Yu01da35e2022-12-12 15:09:22 +08003309 }
Jerry Yuf135bac2023-11-23 18:10:51 +08003310
Jerry Yu01da35e2022-12-12 15:09:22 +08003311#endif /* MBEDTLS_SSL_EARLY_DATA */
3312
3313 MBEDTLS_PUT_UINT16_BE(p - p_extensions_len - 2, p_extensions_len, 0);
3314
Jerry Yue67bef42022-07-07 07:29:42 +00003315 *out_len = p - buf;
Gilles Peskine449bd832023-01-11 14:50:10 +01003316 MBEDTLS_SSL_DEBUG_BUF(4, "ticket", buf, *out_len);
3317 MBEDTLS_SSL_DEBUG_MSG(2, ("<= write new session ticket"));
Jerry Yue67bef42022-07-07 07:29:42 +00003318
Jerry Yu7de2ff02022-11-08 21:43:46 +08003319 MBEDTLS_SSL_PRINT_EXTS(
Gilles Peskine449bd832023-01-11 14:50:10 +01003320 3, MBEDTLS_SSL_HS_NEW_SESSION_TICKET, ssl->handshake->sent_extensions);
Jerry Yu4b8f2f72022-10-31 13:31:22 +08003321
Gilles Peskine449bd832023-01-11 14:50:10 +01003322 return 0;
Jerry Yue67bef42022-07-07 07:29:42 +00003323}
3324
3325/*
Jerry Yua8d3c502022-10-30 14:51:23 +08003326 * Handler for MBEDTLS_SSL_TLS1_3_NEW_SESSION_TICKET
Jerry Yue67bef42022-07-07 07:29:42 +00003327 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003328static int ssl_tls13_write_new_session_ticket(mbedtls_ssl_context *ssl)
Jerry Yue67bef42022-07-07 07:29:42 +00003329{
3330 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
3331
Gilles Peskine449bd832023-01-11 14:50:10 +01003332 MBEDTLS_SSL_PROC_CHK_NEG(ssl_tls13_write_new_session_ticket_coordinate(ssl));
Jerry Yue67bef42022-07-07 07:29:42 +00003333
Gilles Peskine449bd832023-01-11 14:50:10 +01003334 if (ret == SSL_NEW_SESSION_TICKET_WRITE) {
Jerry Yue67bef42022-07-07 07:29:42 +00003335 unsigned char ticket_nonce[MBEDTLS_SSL_TLS1_3_TICKET_NONCE_LENGTH];
3336 unsigned char *buf;
3337 size_t buf_len, msg_len;
3338
Gilles Peskine449bd832023-01-11 14:50:10 +01003339 MBEDTLS_SSL_PROC_CHK(ssl_tls13_prepare_new_session_ticket(
3340 ssl, ticket_nonce, sizeof(ticket_nonce)));
Jerry Yue67bef42022-07-07 07:29:42 +00003341
Xiaokang Qian9f1747b2023-03-30 02:50:04 +00003342 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_start_handshake_msg(
3343 ssl, MBEDTLS_SSL_HS_NEW_SESSION_TICKET,
3344 &buf, &buf_len));
Jerry Yue67bef42022-07-07 07:29:42 +00003345
Gilles Peskine449bd832023-01-11 14:50:10 +01003346 MBEDTLS_SSL_PROC_CHK(ssl_tls13_write_new_session_ticket_body(
3347 ssl, buf, buf + buf_len, &msg_len,
3348 ticket_nonce, sizeof(ticket_nonce)));
Jerry Yue67bef42022-07-07 07:29:42 +00003349
Gilles Peskine449bd832023-01-11 14:50:10 +01003350 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_finish_handshake_msg(
3351 ssl, buf_len, msg_len));
Jerry Yufca4d572022-07-21 10:37:48 +08003352
Jerry Yu359e65f2022-09-22 23:47:43 +08003353 /* Limit session tickets count to one when resumption connection.
3354 *
3355 * See document of mbedtls_ssl_conf_new_session_tickets.
3356 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003357 if (ssl->handshake->resume == 1) {
Jerry Yu359e65f2022-09-22 23:47:43 +08003358 ssl->handshake->new_session_tickets_count = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01003359 } else {
Jerry Yu359e65f2022-09-22 23:47:43 +08003360 ssl->handshake->new_session_tickets_count--;
Gilles Peskine449bd832023-01-11 14:50:10 +01003361 }
Jerry Yub7e3fa72022-09-22 11:07:18 +08003362
Jerry Yua8d3c502022-10-30 14:51:23 +08003363 mbedtls_ssl_handshake_set_state(
Gilles Peskine449bd832023-01-11 14:50:10 +01003364 ssl, MBEDTLS_SSL_TLS1_3_NEW_SESSION_TICKET_FLUSH);
3365 } else {
3366 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_HANDSHAKE_OVER);
Jerry Yue67bef42022-07-07 07:29:42 +00003367 }
3368
Jerry Yue67bef42022-07-07 07:29:42 +00003369cleanup:
3370
Gilles Peskine449bd832023-01-11 14:50:10 +01003371 return ret;
Jerry Yue67bef42022-07-07 07:29:42 +00003372}
3373#endif /* MBEDTLS_SSL_SESSION_TICKETS */
3374
3375/*
XiaokangQiane8ff3502022-04-22 02:34:40 +00003376 * TLS 1.3 State Machine -- server side
XiaokangQian7807f9f2022-02-15 10:04:37 +00003377 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003378int mbedtls_ssl_tls13_handshake_server_step(mbedtls_ssl_context *ssl)
Jerry Yub9930e72021-08-06 17:11:51 +08003379{
XiaokangQian08037552022-04-20 07:16:41 +00003380 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
XiaokangQian7807f9f2022-02-15 10:04:37 +00003381
Gilles Peskine449bd832023-01-11 14:50:10 +01003382 if (ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER || ssl->handshake == NULL) {
3383 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
3384 }
XiaokangQian7807f9f2022-02-15 10:04:37 +00003385
Gilles Peskine449bd832023-01-11 14:50:10 +01003386 MBEDTLS_SSL_DEBUG_MSG(2, ("tls13 server state: %s(%d)",
Dave Rodgman2eab4622023-10-05 13:30:37 +01003387 mbedtls_ssl_states_str((mbedtls_ssl_states) ssl->state),
Gilles Peskine449bd832023-01-11 14:50:10 +01003388 ssl->state));
Jerry Yu6e81b272021-09-27 11:16:17 +08003389
Gilles Peskine449bd832023-01-11 14:50:10 +01003390 switch (ssl->state) {
XiaokangQian7807f9f2022-02-15 10:04:37 +00003391 /* start state */
3392 case MBEDTLS_SSL_HELLO_REQUEST:
Gilles Peskine449bd832023-01-11 14:50:10 +01003393 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_CLIENT_HELLO);
XiaokangQian08037552022-04-20 07:16:41 +00003394 ret = 0;
XiaokangQian7807f9f2022-02-15 10:04:37 +00003395 break;
3396
XiaokangQian7807f9f2022-02-15 10:04:37 +00003397 case MBEDTLS_SSL_CLIENT_HELLO:
Gilles Peskine449bd832023-01-11 14:50:10 +01003398 ret = ssl_tls13_process_client_hello(ssl);
3399 if (ret != 0) {
3400 MBEDTLS_SSL_DEBUG_RET(1, "ssl_tls13_process_client_hello", ret);
3401 }
Jerry Yuf41553b2022-05-09 22:20:30 +08003402 break;
XiaokangQian7807f9f2022-02-15 10:04:37 +00003403
Jerry Yuf41553b2022-05-09 22:20:30 +08003404 case MBEDTLS_SSL_HELLO_RETRY_REQUEST:
Gilles Peskine449bd832023-01-11 14:50:10 +01003405 ret = ssl_tls13_write_hello_retry_request(ssl);
3406 if (ret != 0) {
3407 MBEDTLS_SSL_DEBUG_RET(1, "ssl_tls13_write_hello_retry_request", ret);
3408 return ret;
Jerry Yuf41553b2022-05-09 22:20:30 +08003409 }
XiaokangQian7807f9f2022-02-15 10:04:37 +00003410 break;
3411
Jerry Yu5b64ae92022-03-30 17:15:02 +08003412 case MBEDTLS_SSL_SERVER_HELLO:
Gilles Peskine449bd832023-01-11 14:50:10 +01003413 ret = ssl_tls13_write_server_hello(ssl);
Jerry Yu5b64ae92022-03-30 17:15:02 +08003414 break;
3415
Xiaofei Baicba64af2022-02-15 10:00:56 +00003416 case MBEDTLS_SSL_ENCRYPTED_EXTENSIONS:
Gilles Peskine449bd832023-01-11 14:50:10 +01003417 ret = ssl_tls13_write_encrypted_extensions(ssl);
3418 if (ret != 0) {
3419 MBEDTLS_SSL_DEBUG_RET(1, "ssl_tls13_write_encrypted_extensions", ret);
3420 return ret;
Xiaofei Baicba64af2022-02-15 10:00:56 +00003421 }
Jerry Yu48330562022-05-06 21:35:44 +08003422 break;
Jerry Yu6a2cd9e2022-05-05 11:14:19 +08003423
Ronald Cron928cbd32022-10-04 16:14:26 +02003424#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED)
Xiaofei Bai9ca09d42022-02-14 12:57:18 +00003425 case MBEDTLS_SSL_CERTIFICATE_REQUEST:
Gilles Peskine449bd832023-01-11 14:50:10 +01003426 ret = ssl_tls13_write_certificate_request(ssl);
Xiaofei Bai9ca09d42022-02-14 12:57:18 +00003427 break;
Xiaofei Bai9ca09d42022-02-14 12:57:18 +00003428
Jerry Yu83da34e2022-04-16 13:59:52 +08003429 case MBEDTLS_SSL_SERVER_CERTIFICATE:
Gilles Peskine449bd832023-01-11 14:50:10 +01003430 ret = ssl_tls13_write_server_certificate(ssl);
Jerry Yu83da34e2022-04-16 13:59:52 +08003431 break;
3432
3433 case MBEDTLS_SSL_CERTIFICATE_VERIFY:
Gilles Peskine449bd832023-01-11 14:50:10 +01003434 ret = ssl_tls13_write_certificate_verify(ssl);
Jerry Yu83da34e2022-04-16 13:59:52 +08003435 break;
Ronald Cron928cbd32022-10-04 16:14:26 +02003436#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED */
Jerry Yu83da34e2022-04-16 13:59:52 +08003437
Gilles Peskine449bd832023-01-11 14:50:10 +01003438 /*
3439 * Injection of dummy-CCS's for middlebox compatibility
3440 */
Gabor Mezei7b39bf12022-05-24 16:04:14 +02003441#if defined(MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE)
Gabor Mezeif7044ea2022-06-28 16:01:49 +02003442 case MBEDTLS_SSL_SERVER_CCS_AFTER_HELLO_RETRY_REQUEST:
Gilles Peskine449bd832023-01-11 14:50:10 +01003443 ret = mbedtls_ssl_tls13_write_change_cipher_spec(ssl);
3444 if (ret == 0) {
3445 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_CLIENT_HELLO);
3446 }
Gabor Mezei7b39bf12022-05-24 16:04:14 +02003447 break;
3448
3449 case MBEDTLS_SSL_SERVER_CCS_AFTER_SERVER_HELLO:
Ronald Crone273f722024-02-13 18:22:26 +01003450 ret = mbedtls_ssl_tls13_write_change_cipher_spec(ssl);
3451 if (ret != 0) {
3452 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01003453 }
Ronald Cronfe59ff72024-01-24 14:31:50 +01003454 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_ENCRYPTED_EXTENSIONS);
Gabor Mezei7b39bf12022-05-24 16:04:14 +02003455 break;
3456#endif /* MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE */
3457
Jerry Yu69dd8d42022-04-16 12:51:26 +08003458 case MBEDTLS_SSL_SERVER_FINISHED:
Gilles Peskine449bd832023-01-11 14:50:10 +01003459 ret = ssl_tls13_write_server_finished(ssl);
Jerry Yu69dd8d42022-04-16 12:51:26 +08003460 break;
3461
Jerry Yu87b5ed42022-12-12 13:07:07 +08003462#if defined(MBEDTLS_SSL_EARLY_DATA)
3463 case MBEDTLS_SSL_END_OF_EARLY_DATA:
Jerry Yu59d420f2023-12-01 16:30:34 +08003464 ret = ssl_tls13_process_end_of_early_data(ssl);
Jerry Yu87b5ed42022-12-12 13:07:07 +08003465 break;
3466#endif /* MBEDTLS_SSL_EARLY_DATA */
3467
Jerry Yu69dd8d42022-04-16 12:51:26 +08003468 case MBEDTLS_SSL_CLIENT_FINISHED:
Gilles Peskine449bd832023-01-11 14:50:10 +01003469 ret = ssl_tls13_process_client_finished(ssl);
Jerry Yu69dd8d42022-04-16 12:51:26 +08003470 break;
3471
Jerry Yu69dd8d42022-04-16 12:51:26 +08003472 case MBEDTLS_SSL_HANDSHAKE_WRAPUP:
Gilles Peskine449bd832023-01-11 14:50:10 +01003473 ret = ssl_tls13_handshake_wrapup(ssl);
Jerry Yu69dd8d42022-04-16 12:51:26 +08003474 break;
3475
Ronald Cron766c0cd2022-10-18 12:17:11 +02003476#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED)
XiaokangQian6b916b12022-04-25 07:29:34 +00003477 case MBEDTLS_SSL_CLIENT_CERTIFICATE:
Gilles Peskine449bd832023-01-11 14:50:10 +01003478 ret = mbedtls_ssl_tls13_process_certificate(ssl);
3479 if (ret == 0) {
3480 if (ssl->session_negotiate->peer_cert != NULL) {
Ronald Cron209cae92022-06-07 10:30:19 +02003481 mbedtls_ssl_handshake_set_state(
Gilles Peskine449bd832023-01-11 14:50:10 +01003482 ssl, MBEDTLS_SSL_CLIENT_CERTIFICATE_VERIFY);
3483 } else {
3484 MBEDTLS_SSL_DEBUG_MSG(2, ("skip parse certificate verify"));
Ronald Cron209cae92022-06-07 10:30:19 +02003485 mbedtls_ssl_handshake_set_state(
Gilles Peskine449bd832023-01-11 14:50:10 +01003486 ssl, MBEDTLS_SSL_CLIENT_FINISHED);
Ronald Cron19385882022-06-15 16:26:13 +02003487 }
XiaokangQian6b916b12022-04-25 07:29:34 +00003488 }
3489 break;
3490
3491 case MBEDTLS_SSL_CLIENT_CERTIFICATE_VERIFY:
Gilles Peskine449bd832023-01-11 14:50:10 +01003492 ret = mbedtls_ssl_tls13_process_certificate_verify(ssl);
3493 if (ret == 0) {
XiaokangQian6b916b12022-04-25 07:29:34 +00003494 mbedtls_ssl_handshake_set_state(
Gilles Peskine449bd832023-01-11 14:50:10 +01003495 ssl, MBEDTLS_SSL_CLIENT_FINISHED);
XiaokangQian6b916b12022-04-25 07:29:34 +00003496 }
3497 break;
Ronald Cron766c0cd2022-10-18 12:17:11 +02003498#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED */
XiaokangQian6b916b12022-04-25 07:29:34 +00003499
Jerry Yue67bef42022-07-07 07:29:42 +00003500#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Jerry Yua8d3c502022-10-30 14:51:23 +08003501 case MBEDTLS_SSL_TLS1_3_NEW_SESSION_TICKET:
Gilles Peskine449bd832023-01-11 14:50:10 +01003502 ret = ssl_tls13_write_new_session_ticket(ssl);
3503 if (ret != 0) {
3504 MBEDTLS_SSL_DEBUG_RET(1,
3505 "ssl_tls13_write_new_session_ticket ",
3506 ret);
Jerry Yue67bef42022-07-07 07:29:42 +00003507 }
3508 break;
Jerry Yua8d3c502022-10-30 14:51:23 +08003509 case MBEDTLS_SSL_TLS1_3_NEW_SESSION_TICKET_FLUSH:
Jerry Yue67bef42022-07-07 07:29:42 +00003510 /* This state is necessary to do the flush of the New Session
Jerry Yua8d3c502022-10-30 14:51:23 +08003511 * Ticket message written in MBEDTLS_SSL_TLS1_3_NEW_SESSION_TICKET
Jerry Yue67bef42022-07-07 07:29:42 +00003512 * as part of ssl_prepare_handshake_step.
3513 */
3514 ret = 0;
Jerry Yud4e75002022-08-09 13:33:50 +08003515
Gilles Peskine449bd832023-01-11 14:50:10 +01003516 if (ssl->handshake->new_session_tickets_count == 0) {
3517 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_HANDSHAKE_OVER);
3518 } else {
Xiaokang Qian9f1747b2023-03-30 02:50:04 +00003519 mbedtls_ssl_handshake_set_state(
3520 ssl, MBEDTLS_SSL_TLS1_3_NEW_SESSION_TICKET);
Gilles Peskine449bd832023-01-11 14:50:10 +01003521 }
Jerry Yue67bef42022-07-07 07:29:42 +00003522 break;
3523
3524#endif /* MBEDTLS_SSL_SESSION_TICKETS */
3525
XiaokangQian7807f9f2022-02-15 10:04:37 +00003526 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01003527 MBEDTLS_SSL_DEBUG_MSG(1, ("invalid state %d", ssl->state));
3528 return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
XiaokangQian7807f9f2022-02-15 10:04:37 +00003529 }
3530
Gilles Peskine449bd832023-01-11 14:50:10 +01003531 return ret;
Jerry Yub9930e72021-08-06 17:11:51 +08003532}
Jerry Yu3cc4c2a2021-08-06 16:29:08 +08003533
Jerry Yufb4b6472022-01-27 15:03:26 +08003534#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_PROTO_TLS1_3 */