blob: 391b8d42c22f7c20117341b912979ba4c89180a5 [file] [log] [blame]
Jerry Yu3cc4c2a2021-08-06 16:29:08 +08001/*
Jerry Yub9930e72021-08-06 17:11:51 +08002 * TLS 1.3 server-side functions
Jerry Yu3cc4c2a2021-08-06 16:29:08 +08003 *
4 * Copyright The Mbed TLS Contributors
Dave Rodgman16799db2023-11-02 19:47:20 +00005 * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
Gilles Peskine449bd832023-01-11 14:50:10 +01006 */
Jerry Yu3cc4c2a2021-08-06 16:29:08 +08007
8#include "common.h"
9
Jerry Yufb4b6472022-01-27 15:03:26 +080010#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_PROTO_TLS1_3)
Jerry Yu3cc4c2a2021-08-06 16:29:08 +080011
Valerio Settib4f50762024-01-17 10:24:52 +010012#include "debug_internal.h"
Xiaofei Baicba64af2022-02-15 10:00:56 +000013#include "mbedtls/error.h"
14#include "mbedtls/platform.h"
Jerry Yu1c105562022-07-10 06:32:38 +000015#include "mbedtls/constant_time.h"
Manuel Pégourié-Gonnardd55d66f2023-06-20 10:14:58 +020016#include "mbedtls/oid.h"
Valerio Setti384fbde2024-01-02 13:26:40 +010017#include "mbedtls/psa_util.h"
Jerry Yu3bf2c642022-03-30 22:02:12 +080018
Xiaofei Bai9ca09d42022-02-14 12:57:18 +000019#include "ssl_misc.h"
20#include "ssl_tls13_keys.h"
21#include "ssl_debug_helpers.h"
22
Jerry Yuf35ba382022-08-23 17:58:26 +080023
Jerry Yuc5a23a02022-08-25 10:51:44 +080024static const mbedtls_ssl_ciphersuite_t *ssl_tls13_validate_peer_ciphersuite(
Gilles Peskine449bd832023-01-11 14:50:10 +010025 mbedtls_ssl_context *ssl,
26 unsigned int cipher_suite)
Jerry Yuf35ba382022-08-23 17:58:26 +080027{
28 const mbedtls_ssl_ciphersuite_t *ciphersuite_info;
Gilles Peskine449bd832023-01-11 14:50:10 +010029 if (!mbedtls_ssl_tls13_cipher_suite_is_offered(ssl, cipher_suite)) {
30 return NULL;
Jerry Yuf35ba382022-08-23 17:58:26 +080031 }
Gilles Peskine449bd832023-01-11 14:50:10 +010032
33 ciphersuite_info = mbedtls_ssl_ciphersuite_from_id(cipher_suite);
34 if ((mbedtls_ssl_validate_ciphersuite(ssl, ciphersuite_info,
35 ssl->tls_version,
36 ssl->tls_version) != 0)) {
37 return NULL;
38 }
39 return ciphersuite_info;
Jerry Yuf35ba382022-08-23 17:58:26 +080040}
41
Ronald Cron89089cc2024-02-14 18:18:08 +010042static void ssl_tls13_select_ciphersuite(
43 mbedtls_ssl_context *ssl,
44 const unsigned char *cipher_suites,
45 const unsigned char *cipher_suites_end,
46 int psk_ciphersuite_id,
47 psa_algorithm_t psk_hash_alg,
48 const mbedtls_ssl_ciphersuite_t **selected_ciphersuite_info)
49{
50 *selected_ciphersuite_info = NULL;
51
52 /*
53 * This function assumes that the length of the list of ciphersuites is
54 * even and it should have been checked it is so in the main ClientHello
55 * parsing function. Double check here.
56 */
57 if ((cipher_suites_end - cipher_suites) & 1) {
58 return;
59 }
60
61 for (const unsigned char *p = cipher_suites;
62 p < cipher_suites_end; p += 2) {
63 /*
64 * "cipher_suites_end - p is even" is an invariant of the loop. As
65 * cipher_suites_end - p > 0, we have cipher_suites_end - p >= 2 and it
66 * is thus safe to read two bytes.
67 */
68 uint16_t id = MBEDTLS_GET_UINT16_BE(p, 0);
69
70 const mbedtls_ssl_ciphersuite_t *info =
71 ssl_tls13_validate_peer_ciphersuite(ssl, id);
72 if (info == NULL) {
73 continue;
74 }
75
76 /*
77 * If a valid PSK ciphersuite identifier has been passed in, we seek
78 * for an exact match.
79 */
80 if (psk_ciphersuite_id != 0) {
81 if (id != psk_ciphersuite_id) {
82 continue;
83 }
84 } else if (psk_hash_alg != PSA_ALG_NONE) {
85 if (mbedtls_md_psa_alg_from_type((mbedtls_md_type_t) info->mac) !=
86 psk_hash_alg) {
87 continue;
88 }
89 }
90
91 *selected_ciphersuite_info = info;
92 return;
93 }
94
95 MBEDTLS_SSL_DEBUG_MSG(2, ("No matched ciphersuite"));
96}
97
Ronald Cron41a443a2022-10-04 16:38:25 +020098#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED)
Jerry Yue19e3b92022-07-08 12:04:51 +000099/* From RFC 8446:
100 *
101 * enum { psk_ke(0), psk_dhe_ke(1), (255) } PskKeyExchangeMode;
102 * struct {
103 * PskKeyExchangeMode ke_modes<1..255>;
104 * } PskKeyExchangeModes;
105 */
Jerry Yu6e74a7e2022-07-20 20:49:32 +0800106MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100107static int ssl_tls13_parse_key_exchange_modes_ext(mbedtls_ssl_context *ssl,
108 const unsigned char *buf,
109 const unsigned char *end)
Jerry Yue19e3b92022-07-08 12:04:51 +0000110{
Jerry Yu299e31f2022-07-13 23:06:36 +0800111 const unsigned char *p = buf;
Jerry Yue19e3b92022-07-08 12:04:51 +0000112 size_t ke_modes_len;
113 int ke_modes = 0;
114
Jerry Yu854dd9e2022-07-15 14:28:27 +0800115 /* Read ke_modes length (1 Byte) */
Gilles Peskine449bd832023-01-11 14:50:10 +0100116 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, 1);
Jerry Yu299e31f2022-07-13 23:06:36 +0800117 ke_modes_len = *p++;
Jerry Yue19e3b92022-07-08 12:04:51 +0000118 /* Currently, there are only two PSK modes, so even without looking
119 * at the content, something's wrong if the list has more than 2 items. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100120 if (ke_modes_len > 2) {
121 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER,
122 MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER);
123 return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
Jerry Yu299e31f2022-07-13 23:06:36 +0800124 }
Jerry Yue19e3b92022-07-08 12:04:51 +0000125
Gilles Peskine449bd832023-01-11 14:50:10 +0100126 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, ke_modes_len);
Jerry Yue19e3b92022-07-08 12:04:51 +0000127
Gilles Peskine449bd832023-01-11 14:50:10 +0100128 while (ke_modes_len-- != 0) {
129 switch (*p++) {
130 case MBEDTLS_SSL_TLS1_3_PSK_MODE_PURE:
131 ke_modes |= MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK;
132 MBEDTLS_SSL_DEBUG_MSG(3, ("Found PSK KEX MODE"));
133 break;
134 case MBEDTLS_SSL_TLS1_3_PSK_MODE_ECDHE:
135 ke_modes |= MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK_EPHEMERAL;
136 MBEDTLS_SSL_DEBUG_MSG(3, ("Found PSK_EPHEMERAL KEX MODE"));
137 break;
138 default:
139 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER,
140 MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER);
141 return MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER;
Jerry Yue19e3b92022-07-08 12:04:51 +0000142 }
143 }
144
145 ssl->handshake->tls13_kex_modes = ke_modes;
Gilles Peskine449bd832023-01-11 14:50:10 +0100146 return 0;
Jerry Yue19e3b92022-07-08 12:04:51 +0000147}
Jerry Yu1c105562022-07-10 06:32:38 +0000148
Ronald Cronfbae94a2023-12-05 18:15:14 +0100149#define SSL_TLS1_3_PSK_IDENTITY_DOES_NOT_MATCH 2
150#define SSL_TLS1_3_PSK_IDENTITY_MATCH_BUT_PSK_NOT_USABLE 1
151#define SSL_TLS1_3_PSK_IDENTITY_MATCH 0
Jerry Yu95699e72022-08-21 19:22:23 +0800152
153#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Pengyu Lv29daf4a2023-10-30 17:13:30 +0800154MBEDTLS_CHECK_RETURN_CRITICAL
Pengyu Lvbc4aab72023-12-01 15:37:24 +0800155static int ssl_tls13_key_exchange_is_psk_available(mbedtls_ssl_context *ssl);
Pengyu Lv29daf4a2023-10-30 17:13:30 +0800156MBEDTLS_CHECK_RETURN_CRITICAL
Pengyu Lvbc4aab72023-12-01 15:37:24 +0800157static int ssl_tls13_key_exchange_is_psk_ephemeral_available(mbedtls_ssl_context *ssl);
Jerry Yu95699e72022-08-21 19:22:23 +0800158
159MBEDTLS_CHECK_RETURN_CRITICAL
160static int ssl_tls13_offered_psks_check_identity_match_ticket(
Gilles Peskine449bd832023-01-11 14:50:10 +0100161 mbedtls_ssl_context *ssl,
162 const unsigned char *identity,
163 size_t identity_len,
164 uint32_t obfuscated_ticket_age,
165 mbedtls_ssl_session *session)
Jerry Yu95699e72022-08-21 19:22:23 +0800166{
Jerry Yufd310eb2022-09-06 09:16:35 +0800167 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yu95699e72022-08-21 19:22:23 +0800168 unsigned char *ticket_buffer;
Jerry Yu8d4bbba2022-09-13 14:15:48 +0800169#if defined(MBEDTLS_HAVE_TIME)
Jerry Yucebffc32022-12-15 18:00:05 +0800170 mbedtls_ms_time_t now;
171 mbedtls_ms_time_t server_age;
Jerry Yu713ce1f2023-11-17 15:32:12 +0800172 uint32_t client_age;
Jerry Yucebffc32022-12-15 18:00:05 +0800173 mbedtls_ms_time_t age_diff;
Jerry Yu8d4bbba2022-09-13 14:15:48 +0800174#endif
Jerry Yu95699e72022-08-21 19:22:23 +0800175
176 ((void) obfuscated_ticket_age);
177
Gilles Peskine449bd832023-01-11 14:50:10 +0100178 MBEDTLS_SSL_DEBUG_MSG(2, ("=> check_identity_match_ticket"));
Jerry Yu95699e72022-08-21 19:22:23 +0800179
Jerry Yufd310eb2022-09-06 09:16:35 +0800180 /* Ticket parser is not configured, Skip */
Gilles Peskine449bd832023-01-11 14:50:10 +0100181 if (ssl->conf->f_ticket_parse == NULL || identity_len == 0) {
Ronald Cronfbae94a2023-12-05 18:15:14 +0100182 return SSL_TLS1_3_PSK_IDENTITY_DOES_NOT_MATCH;
Gilles Peskine449bd832023-01-11 14:50:10 +0100183 }
Jerry Yu95699e72022-08-21 19:22:23 +0800184
Jerry Yu4746b102022-09-13 11:11:48 +0800185 /* We create a copy of the encrypted ticket since the ticket parsing
186 * function is allowed to use its input buffer as an output buffer
187 * (in-place decryption). We do, however, need the original buffer for
188 * computing the PSK binder value.
Jerry Yu95699e72022-08-21 19:22:23 +0800189 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100190 ticket_buffer = mbedtls_calloc(1, identity_len);
191 if (ticket_buffer == NULL) {
192 MBEDTLS_SSL_DEBUG_MSG(1, ("buffer too small"));
193 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
Jerry Yu95699e72022-08-21 19:22:23 +0800194 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100195 memcpy(ticket_buffer, identity, identity_len);
Jerry Yu95699e72022-08-21 19:22:23 +0800196
Ronald Cronfbae94a2023-12-05 18:15:14 +0100197 ret = ssl->conf->f_ticket_parse(ssl->conf->p_ticket,
198 session,
199 ticket_buffer, identity_len);
200 if (ret == 0) {
201 ret = SSL_TLS1_3_PSK_IDENTITY_MATCH;
202 } else {
203 if (ret == MBEDTLS_ERR_SSL_SESSION_TICKET_EXPIRED) {
Gilles Peskine449bd832023-01-11 14:50:10 +0100204 MBEDTLS_SSL_DEBUG_MSG(3, ("ticket is expired"));
Ronald Cronfbae94a2023-12-05 18:15:14 +0100205 ret = SSL_TLS1_3_PSK_IDENTITY_MATCH_BUT_PSK_NOT_USABLE;
Gilles Peskine449bd832023-01-11 14:50:10 +0100206 } else {
Ronald Cronfbae94a2023-12-05 18:15:14 +0100207 if (ret == MBEDTLS_ERR_SSL_INVALID_MAC) {
208 MBEDTLS_SSL_DEBUG_MSG(3, ("ticket is not authentic"));
209 } else {
210 MBEDTLS_SSL_DEBUG_RET(1, "ticket_parse", ret);
211 }
212 ret = SSL_TLS1_3_PSK_IDENTITY_DOES_NOT_MATCH;
Gilles Peskine449bd832023-01-11 14:50:10 +0100213 }
Jerry Yu95699e72022-08-21 19:22:23 +0800214 }
215
216 /* We delete the temporary buffer */
Gilles Peskine449bd832023-01-11 14:50:10 +0100217 mbedtls_free(ticket_buffer);
Jerry Yu95699e72022-08-21 19:22:23 +0800218
Ronald Cronfbae94a2023-12-05 18:15:14 +0100219 if (ret != SSL_TLS1_3_PSK_IDENTITY_MATCH) {
220 goto exit;
Jerry Yuce3b95e2023-10-31 16:02:04 +0800221 }
Jerry Yuce3b95e2023-10-31 16:02:04 +0800222
Ronald Cronfbae94a2023-12-05 18:15:14 +0100223 ret = SSL_TLS1_3_PSK_IDENTITY_MATCH_BUT_PSK_NOT_USABLE;
224
225 if (session->tls_version != MBEDTLS_SSL_VERSION_TLS1_3) {
226 MBEDTLS_SSL_DEBUG_MSG(3, ("Ticket TLS version is not 1.3."));
Jerry Yu8d4bbba2022-09-13 14:15:48 +0800227 goto exit;
228 }
Jerry Yu95699e72022-08-21 19:22:23 +0800229
Gilles Peskine449bd832023-01-11 14:50:10 +0100230#if defined(MBEDTLS_HAVE_TIME)
Jerry Yucebffc32022-12-15 18:00:05 +0800231 now = mbedtls_ms_time();
Gilles Peskine449bd832023-01-11 14:50:10 +0100232
Jerry Yu25ba4d42023-11-10 14:12:20 +0800233 if (now < session->ticket_creation_time) {
Gilles Peskine449bd832023-01-11 14:50:10 +0100234 MBEDTLS_SSL_DEBUG_MSG(
Jerry Yu713ce1f2023-11-17 15:32:12 +0800235 3, ("Invalid ticket creation time ( now = %" MBEDTLS_PRINTF_MS_TIME
236 ", creation_time = %" MBEDTLS_PRINTF_MS_TIME " )",
Jerry Yu25ba4d42023-11-10 14:12:20 +0800237 now, session->ticket_creation_time));
Gilles Peskine449bd832023-01-11 14:50:10 +0100238 goto exit;
239 }
240
Jerry Yu25ba4d42023-11-10 14:12:20 +0800241 server_age = now - session->ticket_creation_time;
Jerry Yu95699e72022-08-21 19:22:23 +0800242
Jerry Yu8d4bbba2022-09-13 14:15:48 +0800243 /* RFC 8446 section 4.6.1
244 *
245 * Servers MUST NOT use any value greater than 604800 seconds (7 days).
246 *
247 * RFC 8446 section 4.2.11.1
248 *
249 * Clients MUST NOT attempt to use tickets which have ages greater than
250 * the "ticket_lifetime" value which was provided with the ticket.
251 *
Jerry Yu8d4bbba2022-09-13 14:15:48 +0800252 */
Jerry Yu8e0174a2023-11-10 13:58:16 +0800253 if (server_age > MBEDTLS_SSL_TLS1_3_MAX_ALLOWED_TICKET_LIFETIME * 1000) {
Jerry Yu8d4bbba2022-09-13 14:15:48 +0800254 MBEDTLS_SSL_DEBUG_MSG(
Jerry Yud84c14f2023-11-16 13:33:57 +0800255 3, ("Ticket age exceeds limitation ticket_age = %" MBEDTLS_PRINTF_MS_TIME,
Jerry Yucebffc32022-12-15 18:00:05 +0800256 server_age));
Jerry Yu8d4bbba2022-09-13 14:15:48 +0800257 goto exit;
258 }
Jerry Yu95699e72022-08-21 19:22:23 +0800259
Jerry Yu8d4bbba2022-09-13 14:15:48 +0800260 /* RFC 8446 section 4.2.10
261 *
262 * For PSKs provisioned via NewSessionTicket, a server MUST validate that
263 * the ticket age for the selected PSK identity (computed by subtracting
264 * ticket_age_add from PskIdentity.obfuscated_ticket_age modulo 2^32) is
265 * within a small tolerance of the time since the ticket was issued.
Jerry Yuf7dad3c2022-09-14 22:31:39 +0800266 *
Jerry Yu31b601a2023-11-10 11:27:21 +0800267 * NOTE: The typical accuracy of an RTC crystal is ±100 to ±20 parts per
268 * million (360 to 72 milliseconds per hour). Default tolerance
Jerry Yu9cb953a2023-11-15 09:54:11 +0800269 * window is 6s, thus in the worst case clients and servers must
Jerry Yu31b601a2023-11-10 11:27:21 +0800270 * sync up their system time every 6000/360/2~=8 hours.
Jerry Yu8d4bbba2022-09-13 14:15:48 +0800271 */
Jerry Yucebffc32022-12-15 18:00:05 +0800272 client_age = obfuscated_ticket_age - session->ticket_age_add;
Jerry Yu60e99722023-11-20 09:55:24 +0800273 age_diff = server_age - (mbedtls_ms_time_t) client_age;
Jerry Yu28e7c552023-11-10 12:05:56 +0800274 if (age_diff < -MBEDTLS_SSL_TLS1_3_TICKET_AGE_TOLERANCE ||
Jerry Yucebffc32022-12-15 18:00:05 +0800275 age_diff > MBEDTLS_SSL_TLS1_3_TICKET_AGE_TOLERANCE) {
Jerry Yu8d4bbba2022-09-13 14:15:48 +0800276 MBEDTLS_SSL_DEBUG_MSG(
Jerry Yuf16efbc2023-10-30 11:06:24 +0800277 3, ("Ticket age outside tolerance window ( diff = %"
278 MBEDTLS_PRINTF_MS_TIME ")",
Jerry Yucebffc32022-12-15 18:00:05 +0800279 age_diff));
Jerry Yu8d4bbba2022-09-13 14:15:48 +0800280 goto exit;
281 }
Jerry Yu95699e72022-08-21 19:22:23 +0800282#endif /* MBEDTLS_HAVE_TIME */
Jerry Yu8d4bbba2022-09-13 14:15:48 +0800283
Ronald Cronfbae94a2023-12-05 18:15:14 +0100284 ret = SSL_TLS1_3_PSK_IDENTITY_MATCH;
285
Jerry Yu8d4bbba2022-09-13 14:15:48 +0800286exit:
Ronald Cronfbae94a2023-12-05 18:15:14 +0100287 if (ret != SSL_TLS1_3_PSK_IDENTITY_MATCH) {
Gilles Peskine449bd832023-01-11 14:50:10 +0100288 mbedtls_ssl_session_free(session);
289 }
Jerry Yu95699e72022-08-21 19:22:23 +0800290
Gilles Peskine449bd832023-01-11 14:50:10 +0100291 MBEDTLS_SSL_DEBUG_MSG(2, ("<= check_identity_match_ticket"));
292 return ret;
Jerry Yu95699e72022-08-21 19:22:23 +0800293}
294#endif /* MBEDTLS_SSL_SESSION_TICKETS */
295
Jerry Yu6e74a7e2022-07-20 20:49:32 +0800296MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yu1c105562022-07-10 06:32:38 +0000297static int ssl_tls13_offered_psks_check_identity_match(
Gilles Peskine449bd832023-01-11 14:50:10 +0100298 mbedtls_ssl_context *ssl,
299 const unsigned char *identity,
300 size_t identity_len,
301 uint32_t obfuscated_ticket_age,
302 int *psk_type,
303 mbedtls_ssl_session *session)
Jerry Yu1c105562022-07-10 06:32:38 +0000304{
Ronald Cron606671e2023-02-17 11:36:33 +0100305 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
306
Jerry Yu95699e72022-08-21 19:22:23 +0800307 ((void) session);
308 ((void) obfuscated_ticket_age);
Jerry Yu5725f1c2022-08-21 17:27:16 +0800309 *psk_type = MBEDTLS_SSL_TLS1_3_PSK_EXTERNAL;
Jerry Yu95699e72022-08-21 19:22:23 +0800310
Gilles Peskine449bd832023-01-11 14:50:10 +0100311 MBEDTLS_SSL_DEBUG_BUF(4, "identity", identity, identity_len);
Jerry Yu95699e72022-08-21 19:22:23 +0800312
Jerry Yu95699e72022-08-21 19:22:23 +0800313#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Ronald Cron7a30cf52024-02-23 14:38:59 +0100314 ret = ssl_tls13_offered_psks_check_identity_match_ticket(
315 ssl, identity, identity_len, obfuscated_ticket_age, session);
316 if (ret == SSL_TLS1_3_PSK_IDENTITY_MATCH) {
Jerry Yu95699e72022-08-21 19:22:23 +0800317 *psk_type = MBEDTLS_SSL_TLS1_3_PSK_RESUMPTION;
Ronald Cron606671e2023-02-17 11:36:33 +0100318 ret = mbedtls_ssl_set_hs_psk(ssl,
319 session->resumption_key,
320 session->resumption_key_len);
321 if (ret != 0) {
322 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_set_hs_psk", ret);
323 return ret;
324 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100325
326 MBEDTLS_SSL_DEBUG_BUF(4, "Ticket-resumed PSK:",
327 session->resumption_key,
328 session->resumption_key_len);
329 MBEDTLS_SSL_DEBUG_MSG(4, ("ticket: obfuscated_ticket_age: %u",
330 (unsigned) obfuscated_ticket_age));
Ronald Cronfbae94a2023-12-05 18:15:14 +0100331 return SSL_TLS1_3_PSK_IDENTITY_MATCH;
Ronald Cron7a30cf52024-02-23 14:38:59 +0100332 } else if (ret == SSL_TLS1_3_PSK_IDENTITY_MATCH_BUT_PSK_NOT_USABLE) {
333 return SSL_TLS1_3_PSK_IDENTITY_MATCH_BUT_PSK_NOT_USABLE;
Jerry Yu95699e72022-08-21 19:22:23 +0800334 }
335#endif /* MBEDTLS_SSL_SESSION_TICKETS */
336
Jerry Yu1c105562022-07-10 06:32:38 +0000337 /* Check identity with external configured function */
Gilles Peskine449bd832023-01-11 14:50:10 +0100338 if (ssl->conf->f_psk != NULL) {
339 if (ssl->conf->f_psk(
340 ssl->conf->p_psk, ssl, identity, identity_len) == 0) {
Ronald Cronfbae94a2023-12-05 18:15:14 +0100341 return SSL_TLS1_3_PSK_IDENTITY_MATCH;
Jerry Yu1c105562022-07-10 06:32:38 +0000342 }
Ronald Cronfbae94a2023-12-05 18:15:14 +0100343 return SSL_TLS1_3_PSK_IDENTITY_DOES_NOT_MATCH;
Jerry Yu1c105562022-07-10 06:32:38 +0000344 }
345
Gilles Peskine449bd832023-01-11 14:50:10 +0100346 MBEDTLS_SSL_DEBUG_BUF(5, "identity", identity, identity_len);
Jerry Yu1c105562022-07-10 06:32:38 +0000347 /* Check identity with pre-configured psk */
Gilles Peskine449bd832023-01-11 14:50:10 +0100348 if (ssl->conf->psk_identity != NULL &&
Jerry Yu2f0abc92022-07-22 19:34:48 +0800349 identity_len == ssl->conf->psk_identity_len &&
Gilles Peskine449bd832023-01-11 14:50:10 +0100350 mbedtls_ct_memcmp(ssl->conf->psk_identity,
351 identity, identity_len) == 0) {
Ronald Cron606671e2023-02-17 11:36:33 +0100352 ret = mbedtls_ssl_set_hs_psk(ssl, ssl->conf->psk, ssl->conf->psk_len);
353 if (ret != 0) {
354 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_set_hs_psk", ret);
355 return ret;
356 }
Ronald Cronfbae94a2023-12-05 18:15:14 +0100357 return SSL_TLS1_3_PSK_IDENTITY_MATCH;
Jerry Yu1c105562022-07-10 06:32:38 +0000358 }
359
Ronald Cronfbae94a2023-12-05 18:15:14 +0100360 return SSL_TLS1_3_PSK_IDENTITY_DOES_NOT_MATCH;
Jerry Yu1c105562022-07-10 06:32:38 +0000361}
362
Ronald Cron6e311272023-12-05 17:57:01 +0100363#define SSL_TLS1_3_BINDER_DOES_NOT_MATCH 1
364#define SSL_TLS1_3_BINDER_MATCH 0
Jerry Yu6e74a7e2022-07-20 20:49:32 +0800365MBEDTLS_CHECK_RETURN_CRITICAL
Xiaokang Qian9f1747b2023-03-30 02:50:04 +0000366static int ssl_tls13_offered_psks_check_binder_match(
367 mbedtls_ssl_context *ssl,
368 const unsigned char *binder, size_t binder_len,
369 int psk_type, psa_algorithm_t psk_hash_alg)
Jerry Yu1c105562022-07-10 06:32:38 +0000370{
371 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yu96a2e362022-07-21 15:11:34 +0800372
Jerry Yudaf375a2022-07-20 21:31:43 +0800373 unsigned char transcript[PSA_HASH_MAX_SIZE];
Jerry Yu1c105562022-07-10 06:32:38 +0000374 size_t transcript_len;
Jerry Yu2f0abc92022-07-22 19:34:48 +0800375 unsigned char *psk;
Jerry Yu96a2e362022-07-21 15:11:34 +0800376 size_t psk_len;
Jerry Yudaf375a2022-07-20 21:31:43 +0800377 unsigned char server_computed_binder[PSA_HASH_MAX_SIZE];
Jerry Yu1c105562022-07-10 06:32:38 +0000378
Jerry Yu1c105562022-07-10 06:32:38 +0000379 /* Get current state of handshake transcript. */
Jerry Yu29d9faa2022-08-23 17:52:45 +0800380 ret = mbedtls_ssl_get_handshake_transcript(
Manuel Pégourié-Gonnard2d6d9932023-03-28 11:38:08 +0200381 ssl, mbedtls_md_type_from_psa_alg(psk_hash_alg),
Gilles Peskine449bd832023-01-11 14:50:10 +0100382 transcript, sizeof(transcript), &transcript_len);
383 if (ret != 0) {
384 return ret;
385 }
Jerry Yu1c105562022-07-10 06:32:38 +0000386
Gilles Peskine449bd832023-01-11 14:50:10 +0100387 ret = mbedtls_ssl_tls13_export_handshake_psk(ssl, &psk, &psk_len);
388 if (ret != 0) {
389 return ret;
390 }
Jerry Yu96a2e362022-07-21 15:11:34 +0800391
Gilles Peskine449bd832023-01-11 14:50:10 +0100392 ret = mbedtls_ssl_tls13_create_psk_binder(ssl, psk_hash_alg,
393 psk, psk_len, psk_type,
394 transcript,
395 server_computed_binder);
Jerry Yu96a2e362022-07-21 15:11:34 +0800396#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +0100397 mbedtls_free((void *) psk);
Jerry Yu96a2e362022-07-21 15:11:34 +0800398#endif
Gilles Peskine449bd832023-01-11 14:50:10 +0100399 if (ret != 0) {
400 MBEDTLS_SSL_DEBUG_MSG(1, ("PSK binder calculation failed."));
401 return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
Jerry Yu1c105562022-07-10 06:32:38 +0000402 }
403
Gilles Peskine449bd832023-01-11 14:50:10 +0100404 MBEDTLS_SSL_DEBUG_BUF(3, "psk binder ( computed ): ",
405 server_computed_binder, transcript_len);
406 MBEDTLS_SSL_DEBUG_BUF(3, "psk binder ( received ): ", binder, binder_len);
Jerry Yu1c105562022-07-10 06:32:38 +0000407
Gilles Peskine449bd832023-01-11 14:50:10 +0100408 if (mbedtls_ct_memcmp(server_computed_binder, binder, binder_len) == 0) {
Ronald Cron6e311272023-12-05 17:57:01 +0100409 return SSL_TLS1_3_BINDER_MATCH;
Jerry Yu1c105562022-07-10 06:32:38 +0000410 }
411
Gilles Peskine449bd832023-01-11 14:50:10 +0100412 mbedtls_platform_zeroize(server_computed_binder,
413 sizeof(server_computed_binder));
Ronald Cron6e311272023-12-05 17:57:01 +0100414 return SSL_TLS1_3_BINDER_DOES_NOT_MATCH;
Jerry Yu1c105562022-07-10 06:32:38 +0000415}
Jerry Yu96a2e362022-07-21 15:11:34 +0800416
Jerry Yu82534862022-08-30 10:42:33 +0800417#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Jerry Yuf35ba382022-08-23 17:58:26 +0800418MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100419static int ssl_tls13_session_copy_ticket(mbedtls_ssl_session *dst,
420 const mbedtls_ssl_session *src)
Jerry Yu82534862022-08-30 10:42:33 +0800421{
Jerry Yu82534862022-08-30 10:42:33 +0800422 dst->ticket_age_add = src->ticket_age_add;
423 dst->ticket_flags = src->ticket_flags;
424 dst->resumption_key_len = src->resumption_key_len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100425 if (src->resumption_key_len == 0) {
426 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
427 }
428 memcpy(dst->resumption_key, src->resumption_key, src->resumption_key_len);
Jerry Yu4746b102022-09-13 11:11:48 +0800429
Jerry Yu33bf2402022-12-12 16:01:43 +0800430#if defined(MBEDTLS_SSL_EARLY_DATA)
431 dst->max_early_data_size = src->max_early_data_size;
432#endif
433
Gilles Peskine449bd832023-01-11 14:50:10 +0100434 return 0;
Jerry Yu82534862022-08-30 10:42:33 +0800435}
436#endif /* MBEDTLS_SSL_SESSION_TICKETS */
437
Ronald Cron79cdd412024-02-20 17:19:28 +0100438struct psk_attributes {
439 int type;
440 int key_exchange_mode;
441};
442#define PSK_ATTRIBUTES_INIT { 0, 0 }
443
Jerry Yu1c105562022-07-10 06:32:38 +0000444/* Parser for pre_shared_key extension in client hello
445 * struct {
446 * opaque identity<1..2^16-1>;
447 * uint32 obfuscated_ticket_age;
448 * } PskIdentity;
449 *
450 * opaque PskBinderEntry<32..255>;
451 *
452 * struct {
453 * PskIdentity identities<7..2^16-1>;
454 * PskBinderEntry binders<33..2^16-1>;
455 * } OfferedPsks;
456 *
457 * struct {
458 * select (Handshake.msg_type) {
459 * case client_hello: OfferedPsks;
460 * ....
461 * };
462 * } PreSharedKeyExtension;
463 */
Jerry Yu6e74a7e2022-07-20 20:49:32 +0800464MBEDTLS_CHECK_RETURN_CRITICAL
Xiaokang Qian9f1747b2023-03-30 02:50:04 +0000465static int ssl_tls13_parse_pre_shared_key_ext(
466 mbedtls_ssl_context *ssl,
467 const unsigned char *pre_shared_key_ext,
468 const unsigned char *pre_shared_key_ext_end,
469 const unsigned char *ciphersuites,
Ronald Cron79cdd412024-02-20 17:19:28 +0100470 const unsigned char *ciphersuites_end,
471 struct psk_attributes *psk)
Jerry Yu1c105562022-07-10 06:32:38 +0000472{
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +0100473 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yu29d9faa2022-08-23 17:52:45 +0800474 const unsigned char *identities = pre_shared_key_ext;
Jerry Yu568ec252022-07-22 21:27:34 +0800475 const unsigned char *p_identity_len;
476 size_t identities_len;
Jerry Yu1c105562022-07-10 06:32:38 +0000477 const unsigned char *identities_end;
Jerry Yu568ec252022-07-22 21:27:34 +0800478 const unsigned char *binders;
479 const unsigned char *p_binder_len;
480 size_t binders_len;
Jerry Yu1c105562022-07-10 06:32:38 +0000481 const unsigned char *binders_end;
Jerry Yu96a2e362022-07-21 15:11:34 +0800482 int matched_identity = -1;
483 int identity_id = -1;
Jerry Yu1c105562022-07-10 06:32:38 +0000484
Gilles Peskine449bd832023-01-11 14:50:10 +0100485 MBEDTLS_SSL_DEBUG_BUF(3, "pre_shared_key extension",
486 pre_shared_key_ext,
487 pre_shared_key_ext_end - pre_shared_key_ext);
Jerry Yu1c105562022-07-10 06:32:38 +0000488
Jerry Yu96a2e362022-07-21 15:11:34 +0800489 /* identities_len 2 bytes
490 * identities_data >= 7 bytes
491 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100492 MBEDTLS_SSL_CHK_BUF_READ_PTR(identities, pre_shared_key_ext_end, 7 + 2);
493 identities_len = MBEDTLS_GET_UINT16_BE(identities, 0);
Jerry Yu568ec252022-07-22 21:27:34 +0800494 p_identity_len = identities + 2;
Gilles Peskine449bd832023-01-11 14:50:10 +0100495 MBEDTLS_SSL_CHK_BUF_READ_PTR(p_identity_len, pre_shared_key_ext_end,
496 identities_len);
Jerry Yu568ec252022-07-22 21:27:34 +0800497 identities_end = p_identity_len + identities_len;
Jerry Yu1c105562022-07-10 06:32:38 +0000498
Jerry Yu96a2e362022-07-21 15:11:34 +0800499 /* binders_len 2 bytes
500 * binders >= 33 bytes
501 */
Jerry Yu568ec252022-07-22 21:27:34 +0800502 binders = identities_end;
Gilles Peskine449bd832023-01-11 14:50:10 +0100503 MBEDTLS_SSL_CHK_BUF_READ_PTR(binders, pre_shared_key_ext_end, 33 + 2);
504 binders_len = MBEDTLS_GET_UINT16_BE(binders, 0);
Jerry Yu568ec252022-07-22 21:27:34 +0800505 p_binder_len = binders + 2;
Gilles Peskine449bd832023-01-11 14:50:10 +0100506 MBEDTLS_SSL_CHK_BUF_READ_PTR(p_binder_len, pre_shared_key_ext_end, binders_len);
Jerry Yu568ec252022-07-22 21:27:34 +0800507 binders_end = p_binder_len + binders_len;
Jerry Yu1c105562022-07-10 06:32:38 +0000508
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +0100509 ret = ssl->handshake->update_checksum(ssl, pre_shared_key_ext,
510 identities_end - pre_shared_key_ext);
511 if (0 != ret) {
512 MBEDTLS_SSL_DEBUG_RET(1, ("update_checksum"), ret);
513 return ret;
514 }
Jerry Yu96a2e362022-07-21 15:11:34 +0800515
Gilles Peskine449bd832023-01-11 14:50:10 +0100516 while (p_identity_len < identities_end && p_binder_len < binders_end) {
Jerry Yu1c105562022-07-10 06:32:38 +0000517 const unsigned char *identity;
Jerry Yu568ec252022-07-22 21:27:34 +0800518 size_t identity_len;
Jerry Yu82534862022-08-30 10:42:33 +0800519 uint32_t obfuscated_ticket_age;
Jerry Yu96a2e362022-07-21 15:11:34 +0800520 const unsigned char *binder;
Jerry Yu568ec252022-07-22 21:27:34 +0800521 size_t binder_len;
Ronald Cron89089cc2024-02-14 18:18:08 +0100522 int psk_ciphersuite_id;
523 psa_algorithm_t psk_hash_alg;
Ronald Croncf284562024-02-16 18:54:10 +0100524 int allowed_key_exchange_modes;
Jerry Yu29d9faa2022-08-23 17:52:45 +0800525 const mbedtls_ssl_ciphersuite_t *ciphersuite_info;
Jerry Yu82534862022-08-30 10:42:33 +0800526#if defined(MBEDTLS_SSL_SESSION_TICKETS)
527 mbedtls_ssl_session session;
Gilles Peskine449bd832023-01-11 14:50:10 +0100528 mbedtls_ssl_session_init(&session);
Jerry Yu82534862022-08-30 10:42:33 +0800529#endif
Jerry Yubb852022022-07-20 21:10:44 +0800530
Gilles Peskine449bd832023-01-11 14:50:10 +0100531 MBEDTLS_SSL_CHK_BUF_READ_PTR(p_identity_len, identities_end, 2 + 1 + 4);
532 identity_len = MBEDTLS_GET_UINT16_BE(p_identity_len, 0);
Jerry Yu568ec252022-07-22 21:27:34 +0800533 identity = p_identity_len + 2;
Gilles Peskine449bd832023-01-11 14:50:10 +0100534 MBEDTLS_SSL_CHK_BUF_READ_PTR(identity, identities_end, identity_len + 4);
535 obfuscated_ticket_age = MBEDTLS_GET_UINT32_BE(identity, identity_len);
Jerry Yu568ec252022-07-22 21:27:34 +0800536 p_identity_len += identity_len + 6;
Jerry Yu1c105562022-07-10 06:32:38 +0000537
Gilles Peskine449bd832023-01-11 14:50:10 +0100538 MBEDTLS_SSL_CHK_BUF_READ_PTR(p_binder_len, binders_end, 1 + 32);
Jerry Yu568ec252022-07-22 21:27:34 +0800539 binder_len = *p_binder_len;
540 binder = p_binder_len + 1;
Gilles Peskine449bd832023-01-11 14:50:10 +0100541 MBEDTLS_SSL_CHK_BUF_READ_PTR(binder, binders_end, binder_len);
Jerry Yu568ec252022-07-22 21:27:34 +0800542 p_binder_len += binder_len + 1;
Jerry Yu96a2e362022-07-21 15:11:34 +0800543
Jerry Yu96a2e362022-07-21 15:11:34 +0800544 identity_id++;
Gilles Peskine449bd832023-01-11 14:50:10 +0100545 if (matched_identity != -1) {
Jerry Yu1c105562022-07-10 06:32:38 +0000546 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +0100547 }
Jerry Yu1c105562022-07-10 06:32:38 +0000548
Jerry Yu96a2e362022-07-21 15:11:34 +0800549 ret = ssl_tls13_offered_psks_check_identity_match(
Gilles Peskine449bd832023-01-11 14:50:10 +0100550 ssl, identity, identity_len, obfuscated_ticket_age,
Ronald Cron79cdd412024-02-20 17:19:28 +0100551 &psk->type, &session);
Ronald Cronfbae94a2023-12-05 18:15:14 +0100552 if (ret != SSL_TLS1_3_PSK_IDENTITY_MATCH) {
Jerry Yu96a2e362022-07-21 15:11:34 +0800553 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +0100554 }
Jerry Yu96a2e362022-07-21 15:11:34 +0800555
Gilles Peskine449bd832023-01-11 14:50:10 +0100556 MBEDTLS_SSL_DEBUG_MSG(4, ("found matched identity"));
Ronald Cron89089cc2024-02-14 18:18:08 +0100557
Ronald Cron79cdd412024-02-20 17:19:28 +0100558 switch (psk->type) {
Jerry Yu0baf9072022-08-25 11:21:04 +0800559 case MBEDTLS_SSL_TLS1_3_PSK_EXTERNAL:
Ronald Cron89089cc2024-02-14 18:18:08 +0100560 psk_ciphersuite_id = 0;
561 psk_hash_alg = PSA_ALG_SHA_256;
Ronald Croncf284562024-02-16 18:54:10 +0100562 allowed_key_exchange_modes =
563 MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK_ALL;
Jerry Yu0baf9072022-08-25 11:21:04 +0800564 break;
Jerry Yu82534862022-08-30 10:42:33 +0800565#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Ronald Cronf7e99162024-02-14 16:04:02 +0100566 case MBEDTLS_SSL_TLS1_3_PSK_RESUMPTION:
Ronald Cron89089cc2024-02-14 18:18:08 +0100567 psk_ciphersuite_id = session.ciphersuite;
568 psk_hash_alg = PSA_ALG_NONE;
Ronald Croncf284562024-02-16 18:54:10 +0100569 ssl->session_negotiate->ticket_flags = session.ticket_flags;
570 allowed_key_exchange_modes =
571 session.ticket_flags &
572 MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK_ALL;
Jerry Yu0baf9072022-08-25 11:21:04 +0800573 break;
Ronald Cronf7e99162024-02-14 16:04:02 +0100574#endif
Jerry Yu0baf9072022-08-25 11:21:04 +0800575 default:
Gilles Peskine449bd832023-01-11 14:50:10 +0100576 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Jerry Yu0baf9072022-08-25 11:21:04 +0800577 }
Ronald Cron89089cc2024-02-14 18:18:08 +0100578
Ronald Cron79cdd412024-02-20 17:19:28 +0100579 psk->key_exchange_mode = MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_NONE;
580
Ronald Croncf284562024-02-16 18:54:10 +0100581 if ((allowed_key_exchange_modes &
582 MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK_EPHEMERAL) &&
583 ssl_tls13_key_exchange_is_psk_ephemeral_available(ssl)) {
Ronald Cron79cdd412024-02-20 17:19:28 +0100584 psk->key_exchange_mode = MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK_EPHEMERAL;
Ronald Croncf284562024-02-16 18:54:10 +0100585 } else if ((allowed_key_exchange_modes &
586 MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK) &&
587 ssl_tls13_key_exchange_is_psk_available(ssl)) {
Ronald Cron79cdd412024-02-20 17:19:28 +0100588 psk->key_exchange_mode = MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK;
Ronald Croncf284562024-02-16 18:54:10 +0100589 }
590
Ronald Cron79cdd412024-02-20 17:19:28 +0100591 if (psk->key_exchange_mode == MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_NONE) {
Ronald Croncf284562024-02-16 18:54:10 +0100592 MBEDTLS_SSL_DEBUG_MSG(3, ("No suitable PSK key exchange mode"));
593 continue;
594 }
595
Ronald Cron89089cc2024-02-14 18:18:08 +0100596 ssl_tls13_select_ciphersuite(ssl, ciphersuites, ciphersuites_end,
597 psk_ciphersuite_id, psk_hash_alg,
598 &ciphersuite_info);
599
600 if (ciphersuite_info == NULL) {
601#if defined(MBEDTLS_SSL_SESSION_TICKETS)
602 mbedtls_ssl_session_free(&session);
603#endif
604 /*
605 * We consider finding a ciphersuite suitable for the PSK as part
606 * of the validation of its binder. Thus if we do not find one, we
607 * abort the handshake with a decrypt_error alert.
608 */
Jerry Yuf35ba382022-08-23 17:58:26 +0800609 MBEDTLS_SSL_PEND_FATAL_ALERT(
610 MBEDTLS_SSL_ALERT_MSG_DECRYPT_ERROR,
Gilles Peskine449bd832023-01-11 14:50:10 +0100611 MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE);
Ronald Cron89089cc2024-02-14 18:18:08 +0100612 return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
Jerry Yu5725f1c2022-08-21 17:27:16 +0800613 }
614
Jerry Yu96a2e362022-07-21 15:11:34 +0800615 ret = ssl_tls13_offered_psks_check_binder_match(
Ronald Cron79cdd412024-02-20 17:19:28 +0100616 ssl, binder, binder_len, psk->type,
Dave Rodgman2eab4622023-10-05 13:30:37 +0100617 mbedtls_md_psa_alg_from_type((mbedtls_md_type_t) ciphersuite_info->mac));
Ronald Cron6e311272023-12-05 17:57:01 +0100618 if (ret != SSL_TLS1_3_BINDER_MATCH) {
Jerry Yuc5a23a02022-08-25 10:51:44 +0800619 /* For security reasons, the handshake should be aborted when we
620 * fail to validate a binder value. See RFC 8446 section 4.2.11.2
621 * and appendix E.6. */
Jerry Yu82534862022-08-30 10:42:33 +0800622#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Gilles Peskine449bd832023-01-11 14:50:10 +0100623 mbedtls_ssl_session_free(&session);
Jerry Yu82534862022-08-30 10:42:33 +0800624#endif
Gilles Peskine449bd832023-01-11 14:50:10 +0100625 MBEDTLS_SSL_DEBUG_MSG(3, ("Invalid binder."));
Xiaokang Qian9f1747b2023-03-30 02:50:04 +0000626 MBEDTLS_SSL_DEBUG_RET(
627 1, "ssl_tls13_offered_psks_check_binder_match", ret);
Jerry Yu96a2e362022-07-21 15:11:34 +0800628 MBEDTLS_SSL_PEND_FATAL_ALERT(
629 MBEDTLS_SSL_ALERT_MSG_DECRYPT_ERROR,
Gilles Peskine449bd832023-01-11 14:50:10 +0100630 MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE);
631 return ret;
Jerry Yu96a2e362022-07-21 15:11:34 +0800632 }
Jerry Yu96a2e362022-07-21 15:11:34 +0800633
634 matched_identity = identity_id;
Jerry Yu5725f1c2022-08-21 17:27:16 +0800635
636 /* Update handshake parameters */
Jerry Yue5834fd2022-08-29 20:16:09 +0800637 ssl->handshake->ciphersuite_info = ciphersuite_info;
Ronald Cron89089cc2024-02-14 18:18:08 +0100638 ssl->session_negotiate->ciphersuite = ciphersuite_info->id;
Gilles Peskine449bd832023-01-11 14:50:10 +0100639 MBEDTLS_SSL_DEBUG_MSG(2, ("overwrite ciphersuite: %04x - %s",
Ronald Cron89089cc2024-02-14 18:18:08 +0100640 ((unsigned) ciphersuite_info->id),
641 ciphersuite_info->name));
Jerry Yu82534862022-08-30 10:42:33 +0800642#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Ronald Cron79cdd412024-02-20 17:19:28 +0100643 if (psk->type == MBEDTLS_SSL_TLS1_3_PSK_RESUMPTION) {
Gilles Peskine449bd832023-01-11 14:50:10 +0100644 ret = ssl_tls13_session_copy_ticket(ssl->session_negotiate,
645 &session);
646 mbedtls_ssl_session_free(&session);
647 if (ret != 0) {
648 return ret;
649 }
Jerry Yu82534862022-08-30 10:42:33 +0800650 }
651#endif /* MBEDTLS_SSL_SESSION_TICKETS */
Jerry Yu1c105562022-07-10 06:32:38 +0000652 }
653
Gilles Peskine449bd832023-01-11 14:50:10 +0100654 if (p_identity_len != identities_end || p_binder_len != binders_end) {
655 MBEDTLS_SSL_DEBUG_MSG(3, ("pre_shared_key extension decode error"));
656 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR,
657 MBEDTLS_ERR_SSL_DECODE_ERROR);
658 return MBEDTLS_ERR_SSL_DECODE_ERROR;
Jerry Yu1c105562022-07-10 06:32:38 +0000659 }
660
Jerry Yu6f1db3f2022-07-22 23:05:59 +0800661 /* Update the handshake transcript with the binder list. */
Xiaokang Qian9f1747b2023-03-30 02:50:04 +0000662 ret = ssl->handshake->update_checksum(
663 ssl, identities_end, (size_t) (binders_end - identities_end));
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +0100664 if (0 != ret) {
665 MBEDTLS_SSL_DEBUG_RET(1, ("update_checksum"), ret);
666 return ret;
667 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100668 if (matched_identity == -1) {
Ronald Croncf284562024-02-16 18:54:10 +0100669 MBEDTLS_SSL_DEBUG_MSG(3, ("No usable PSK or ticket."));
Gilles Peskine449bd832023-01-11 14:50:10 +0100670 return MBEDTLS_ERR_SSL_UNKNOWN_IDENTITY;
Jerry Yu1c105562022-07-10 06:32:38 +0000671 }
672
Gilles Peskine449bd832023-01-11 14:50:10 +0100673 ssl->handshake->selected_identity = (uint16_t) matched_identity;
674 MBEDTLS_SSL_DEBUG_MSG(3, ("Pre shared key found"));
Jerry Yu1c105562022-07-10 06:32:38 +0000675
Gilles Peskine449bd832023-01-11 14:50:10 +0100676 return 0;
Jerry Yu1c105562022-07-10 06:32:38 +0000677}
Jerry Yu032b15ce2022-07-11 06:10:03 +0000678
679/*
680 * struct {
681 * select ( Handshake.msg_type ) {
682 * ....
683 * case server_hello:
684 * uint16 selected_identity;
685 * }
686 * } PreSharedKeyExtension;
687 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100688static int ssl_tls13_write_server_pre_shared_key_ext(mbedtls_ssl_context *ssl,
689 unsigned char *buf,
690 unsigned char *end,
691 size_t *olen)
Jerry Yu032b15ce2022-07-11 06:10:03 +0000692{
Gilles Peskine449bd832023-01-11 14:50:10 +0100693 unsigned char *p = (unsigned char *) buf;
Jerry Yu032b15ce2022-07-11 06:10:03 +0000694
695 *olen = 0;
696
David Horstmann21b89762022-10-06 18:34:28 +0100697 int not_using_psk = 0;
Jerry Yu032b15ce2022-07-11 06:10:03 +0000698#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +0100699 not_using_psk = (mbedtls_svc_key_id_is_null(ssl->handshake->psk_opaque));
Jerry Yu032b15ce2022-07-11 06:10:03 +0000700#else
Gilles Peskine449bd832023-01-11 14:50:10 +0100701 not_using_psk = (ssl->handshake->psk == NULL);
Jerry Yu032b15ce2022-07-11 06:10:03 +0000702#endif
Gilles Peskine449bd832023-01-11 14:50:10 +0100703 if (not_using_psk) {
Jerry Yu032b15ce2022-07-11 06:10:03 +0000704 /* We shouldn't have called this extension writer unless we've
705 * chosen to use a PSK. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100706 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Jerry Yu032b15ce2022-07-11 06:10:03 +0000707 }
708
Gilles Peskine449bd832023-01-11 14:50:10 +0100709 MBEDTLS_SSL_DEBUG_MSG(3, ("server hello, adding pre_shared_key extension"));
710 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 6);
Jerry Yu032b15ce2022-07-11 06:10:03 +0000711
Gilles Peskine449bd832023-01-11 14:50:10 +0100712 MBEDTLS_PUT_UINT16_BE(MBEDTLS_TLS_EXT_PRE_SHARED_KEY, p, 0);
713 MBEDTLS_PUT_UINT16_BE(2, p, 2);
Jerry Yu032b15ce2022-07-11 06:10:03 +0000714
Gilles Peskine449bd832023-01-11 14:50:10 +0100715 MBEDTLS_PUT_UINT16_BE(ssl->handshake->selected_identity, p, 4);
Jerry Yu032b15ce2022-07-11 06:10:03 +0000716
717 *olen = 6;
718
Gilles Peskine449bd832023-01-11 14:50:10 +0100719 MBEDTLS_SSL_DEBUG_MSG(4, ("sent selected_identity: %u",
720 ssl->handshake->selected_identity));
Jerry Yu032b15ce2022-07-11 06:10:03 +0000721
Gilles Peskine449bd832023-01-11 14:50:10 +0100722 mbedtls_ssl_tls13_set_hs_sent_ext_mask(ssl, MBEDTLS_TLS_EXT_PRE_SHARED_KEY);
Jerry Yub95dd362022-11-08 21:19:34 +0800723
Gilles Peskine449bd832023-01-11 14:50:10 +0100724 return 0;
Jerry Yu032b15ce2022-07-11 06:10:03 +0000725}
726
Ronald Cron41a443a2022-10-04 16:38:25 +0200727#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED */
Jerry Yue19e3b92022-07-08 12:04:51 +0000728
XiaokangQian7807f9f2022-02-15 10:04:37 +0000729/* From RFC 8446:
730 * struct {
XiaokangQiancfd925f2022-04-14 07:10:37 +0000731 * ProtocolVersion versions<2..254>;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000732 * } SupportedVersions;
733 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200734MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100735static int ssl_tls13_parse_supported_versions_ext(mbedtls_ssl_context *ssl,
736 const unsigned char *buf,
737 const unsigned char *end)
XiaokangQian7807f9f2022-02-15 10:04:37 +0000738{
XiaokangQian7807f9f2022-02-15 10:04:37 +0000739 const unsigned char *p = buf;
XiaokangQian8f9dfe42022-04-15 02:52:39 +0000740 size_t versions_len;
XiaokangQian4080a7f2022-04-11 09:55:18 +0000741 const unsigned char *versions_end;
XiaokangQian0a1b54e2022-04-21 03:01:38 +0000742 uint16_t tls_version;
Ronald Cron5af4c7f2023-03-07 20:46:59 +0100743 int found_supported_version = 0;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000744
Gilles Peskine449bd832023-01-11 14:50:10 +0100745 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, 1);
XiaokangQian4080a7f2022-04-11 09:55:18 +0000746 versions_len = p[0];
XiaokangQian7807f9f2022-02-15 10:04:37 +0000747 p += 1;
748
Gilles Peskine449bd832023-01-11 14:50:10 +0100749 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, versions_len);
XiaokangQian4080a7f2022-04-11 09:55:18 +0000750 versions_end = p + versions_len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100751 while (p < versions_end) {
752 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, versions_end, 2);
753 tls_version = mbedtls_ssl_read_version(p, ssl->conf->transport);
XiaokangQianb67384d2022-04-19 00:02:38 +0000754 p += 2;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000755
Ronald Cron3bd2b022023-04-03 16:45:39 +0200756 if (MBEDTLS_SSL_VERSION_TLS1_3 == tls_version) {
Ronald Cron5af4c7f2023-03-07 20:46:59 +0100757 found_supported_version = 1;
758 break;
759 }
760
Ronald Cron3bd2b022023-04-03 16:45:39 +0200761 if ((MBEDTLS_SSL_VERSION_TLS1_2 == tls_version) &&
762 mbedtls_ssl_conf_is_tls12_enabled(ssl->conf)) {
Ronald Cron5af4c7f2023-03-07 20:46:59 +0100763 found_supported_version = 1;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000764 break;
765 }
XiaokangQian7807f9f2022-02-15 10:04:37 +0000766 }
767
Ronald Cron5af4c7f2023-03-07 20:46:59 +0100768 if (!found_supported_version) {
769 MBEDTLS_SSL_DEBUG_MSG(1, ("No supported version found."));
XiaokangQian7807f9f2022-02-15 10:04:37 +0000770
Gilles Peskine449bd832023-01-11 14:50:10 +0100771 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_PROTOCOL_VERSION,
772 MBEDTLS_ERR_SSL_BAD_PROTOCOL_VERSION);
773 return MBEDTLS_ERR_SSL_BAD_PROTOCOL_VERSION;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000774 }
775
Ronald Cron5af4c7f2023-03-07 20:46:59 +0100776 MBEDTLS_SSL_DEBUG_MSG(1, ("Negotiated version: [%04x]",
Gilles Peskine449bd832023-01-11 14:50:10 +0100777 (unsigned int) tls_version));
XiaokangQian7807f9f2022-02-15 10:04:37 +0000778
Ronald Cron5af4c7f2023-03-07 20:46:59 +0100779 return (int) tls_version;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000780}
781
Przemek Stekiel8c0a9532023-06-15 16:48:19 +0200782#if defined(PSA_WANT_ALG_ECDH) || defined(PSA_WANT_ALG_FFDH)
XiaokangQiane8ff3502022-04-22 02:34:40 +0000783/*
XiaokangQian7807f9f2022-02-15 10:04:37 +0000784 *
785 * From RFC 8446:
786 * enum {
787 * ... (0xFFFF)
788 * } NamedGroup;
789 * struct {
790 * NamedGroup named_group_list<2..2^16-1>;
791 * } NamedGroupList;
792 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200793MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100794static int ssl_tls13_parse_supported_groups_ext(mbedtls_ssl_context *ssl,
795 const unsigned char *buf,
796 const unsigned char *end)
XiaokangQian7807f9f2022-02-15 10:04:37 +0000797{
XiaokangQian7807f9f2022-02-15 10:04:37 +0000798 const unsigned char *p = buf;
XiaokangQian84823772022-04-19 07:57:30 +0000799 size_t named_group_list_len;
XiaokangQian8f9dfe42022-04-15 02:52:39 +0000800 const unsigned char *named_group_list_end;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000801
Gilles Peskine449bd832023-01-11 14:50:10 +0100802 MBEDTLS_SSL_DEBUG_BUF(3, "supported_groups extension", p, end - buf);
803 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, 2);
804 named_group_list_len = MBEDTLS_GET_UINT16_BE(p, 0);
XiaokangQian7807f9f2022-02-15 10:04:37 +0000805 p += 2;
Gilles Peskine449bd832023-01-11 14:50:10 +0100806 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, named_group_list_len);
XiaokangQian8f9dfe42022-04-15 02:52:39 +0000807 named_group_list_end = p + named_group_list_len;
XiaokangQian4e8cd7b2022-04-21 09:48:09 +0000808 ssl->handshake->hrr_selected_group = 0;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000809
Gilles Peskine449bd832023-01-11 14:50:10 +0100810 while (p < named_group_list_end) {
XiaokangQian08037552022-04-20 07:16:41 +0000811 uint16_t named_group;
Gilles Peskine449bd832023-01-11 14:50:10 +0100812 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, named_group_list_end, 2);
813 named_group = MBEDTLS_GET_UINT16_BE(p, 0);
XiaokangQian08037552022-04-20 07:16:41 +0000814 p += 2;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000815
Gilles Peskine449bd832023-01-11 14:50:10 +0100816 MBEDTLS_SSL_DEBUG_MSG(2,
817 ("got named group: %s(%04x)",
818 mbedtls_ssl_named_group_to_str(named_group),
819 named_group));
XiaokangQian08037552022-04-20 07:16:41 +0000820
Gilles Peskine449bd832023-01-11 14:50:10 +0100821 if (!mbedtls_ssl_named_group_is_offered(ssl, named_group) ||
822 !mbedtls_ssl_named_group_is_supported(named_group) ||
823 ssl->handshake->hrr_selected_group != 0) {
XiaokangQian08037552022-04-20 07:16:41 +0000824 continue;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000825 }
826
Gilles Peskine449bd832023-01-11 14:50:10 +0100827 MBEDTLS_SSL_DEBUG_MSG(2,
828 ("add named group %s(%04x) into received list.",
829 mbedtls_ssl_named_group_to_str(named_group),
830 named_group));
Jerry Yuc1be19f2022-04-23 16:11:39 +0800831
XiaokangQian4e8cd7b2022-04-21 09:48:09 +0000832 ssl->handshake->hrr_selected_group = named_group;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000833 }
834
Gilles Peskine449bd832023-01-11 14:50:10 +0100835 return 0;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000836
837}
Przemek Stekiel8c0a9532023-06-15 16:48:19 +0200838#endif /* PSA_WANT_ALG_ECDH || PSA_WANT_ALG_FFDH */
XiaokangQian7807f9f2022-02-15 10:04:37 +0000839
XiaokangQian08037552022-04-20 07:16:41 +0000840#define SSL_TLS1_3_PARSE_KEY_SHARES_EXT_NO_MATCH 1
841
Valerio Settic9ae8622023-07-25 11:23:50 +0200842#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_EPHEMERAL_ENABLED)
XiaokangQian7807f9f2022-02-15 10:04:37 +0000843/*
844 * ssl_tls13_parse_key_shares_ext() verifies whether the information in the
Xiaokang Qian9f1747b2023-03-30 02:50:04 +0000845 * extension is correct and stores the first acceptable key share and its
846 * associated group.
XiaokangQian7807f9f2022-02-15 10:04:37 +0000847 *
848 * Possible return values are:
849 * - 0: Successful processing of the client provided key share extension.
Xiaokang Qian9f1747b2023-03-30 02:50:04 +0000850 * - SSL_TLS1_3_PARSE_KEY_SHARES_EXT_NO_MATCH: The key shares provided by
851 * the client does not match a group supported by the server. A
852 * HelloRetryRequest will be needed.
XiaokangQiane8ff3502022-04-22 02:34:40 +0000853 * - A negative value for fatal errors.
Jerry Yuc1be19f2022-04-23 16:11:39 +0800854 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200855MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100856static int ssl_tls13_parse_key_shares_ext(mbedtls_ssl_context *ssl,
857 const unsigned char *buf,
858 const unsigned char *end)
XiaokangQian7807f9f2022-02-15 10:04:37 +0000859{
XiaokangQianb67384d2022-04-19 00:02:38 +0000860 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000861 unsigned char const *p = buf;
XiaokangQian8f9dfe42022-04-15 02:52:39 +0000862 unsigned char const *client_shares_end;
Jerry Yuc1be19f2022-04-23 16:11:39 +0800863 size_t client_shares_len;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000864
865 /* From RFC 8446:
866 *
867 * struct {
868 * KeyShareEntry client_shares<0..2^16-1>;
869 * } KeyShareClientHello;
870 *
871 */
872
Gilles Peskine449bd832023-01-11 14:50:10 +0100873 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, 2);
874 client_shares_len = MBEDTLS_GET_UINT16_BE(p, 0);
XiaokangQian7807f9f2022-02-15 10:04:37 +0000875 p += 2;
Gilles Peskine449bd832023-01-11 14:50:10 +0100876 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, client_shares_len);
XiaokangQian7807f9f2022-02-15 10:04:37 +0000877
878 ssl->handshake->offered_group_id = 0;
XiaokangQian8f9dfe42022-04-15 02:52:39 +0000879 client_shares_end = p + client_shares_len;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000880
881 /* We try to find a suitable key share entry and copy it to the
882 * handshake context. Later, we have to find out whether we can do
883 * something with the provided key share or whether we have to
XiaokangQianc5763b52022-04-02 03:34:37 +0000884 * dismiss it and send a HelloRetryRequest message.
885 */
XiaokangQian7807f9f2022-02-15 10:04:37 +0000886
Gilles Peskine449bd832023-01-11 14:50:10 +0100887 while (p < client_shares_end) {
XiaokangQian9b5d04b2022-04-10 10:20:43 +0000888 uint16_t group;
Jerry Yuc1be19f2022-04-23 16:11:39 +0800889 size_t key_exchange_len;
Jerry Yu086edc22022-05-05 10:50:38 +0800890 const unsigned char *key_exchange;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000891
892 /*
893 * struct {
894 * NamedGroup group;
895 * opaque key_exchange<1..2^16-1>;
896 * } KeyShareEntry;
897 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100898 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, client_shares_end, 4);
899 group = MBEDTLS_GET_UINT16_BE(p, 0);
900 key_exchange_len = MBEDTLS_GET_UINT16_BE(p, 2);
Jerry Yuc1be19f2022-04-23 16:11:39 +0800901 p += 4;
Jerry Yu086edc22022-05-05 10:50:38 +0800902 key_exchange = p;
Gilles Peskine449bd832023-01-11 14:50:10 +0100903 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, client_shares_end, key_exchange_len);
Jerry Yu086edc22022-05-05 10:50:38 +0800904 p += key_exchange_len;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000905
906 /* Continue parsing even if we have already found a match,
XiaokangQianc5763b52022-04-02 03:34:37 +0000907 * for input validation purposes.
908 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100909 if (!mbedtls_ssl_named_group_is_offered(ssl, group) ||
910 !mbedtls_ssl_named_group_is_supported(group) ||
911 ssl->handshake->offered_group_id != 0) {
XiaokangQian060d8672022-04-21 09:24:56 +0000912 continue;
913 }
XiaokangQian060d8672022-04-21 09:24:56 +0000914
XiaokangQian7807f9f2022-02-15 10:04:37 +0000915 /*
Przemek Stekielc89f3ea2023-05-18 15:45:53 +0200916 * ECDHE and FFDHE groups are supported
XiaokangQian7807f9f2022-02-15 10:04:37 +0000917 */
Przemek Stekielc89f3ea2023-05-18 15:45:53 +0200918 if (mbedtls_ssl_tls13_named_group_is_ecdhe(group) ||
Przemek Stekield5f79e72023-06-29 09:08:43 +0200919 mbedtls_ssl_tls13_named_group_is_ffdh(group)) {
Przemek Stekielc89f3ea2023-05-18 15:45:53 +0200920 MBEDTLS_SSL_DEBUG_MSG(2, ("ECDH/FFDH group: %s (%04x)",
Gilles Peskine449bd832023-01-11 14:50:10 +0100921 mbedtls_ssl_named_group_to_str(group),
922 group));
Przemek Stekiel7ac93be2023-07-04 10:02:38 +0200923 ret = mbedtls_ssl_tls13_read_public_xxdhe_share(
Gilles Peskine449bd832023-01-11 14:50:10 +0100924 ssl, key_exchange - 2, key_exchange_len + 2);
925 if (ret != 0) {
926 return ret;
927 }
XiaokangQian4e8cd7b2022-04-21 09:48:09 +0000928
Gilles Peskine449bd832023-01-11 14:50:10 +0100929 } else {
930 MBEDTLS_SSL_DEBUG_MSG(4, ("Unrecognized NamedGroup %u",
931 (unsigned) group));
XiaokangQian7807f9f2022-02-15 10:04:37 +0000932 continue;
933 }
934
XiaokangQian9b5d04b2022-04-10 10:20:43 +0000935 ssl->handshake->offered_group_id = group;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000936 }
937
Jerry Yuc1be19f2022-04-23 16:11:39 +0800938
Gilles Peskine449bd832023-01-11 14:50:10 +0100939 if (ssl->handshake->offered_group_id == 0) {
940 MBEDTLS_SSL_DEBUG_MSG(1, ("no matching key share"));
941 return SSL_TLS1_3_PARSE_KEY_SHARES_EXT_NO_MATCH;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000942 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100943 return 0;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000944}
Valerio Settic9ae8622023-07-25 11:23:50 +0200945#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_EPHEMERAL_ENABLED */
XiaokangQian7807f9f2022-02-15 10:04:37 +0000946
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200947MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100948static int ssl_tls13_client_hello_has_exts(mbedtls_ssl_context *ssl,
949 int exts_mask)
XiaokangQian7807f9f2022-02-15 10:04:37 +0000950{
Jerry Yu0c354a22022-08-29 15:25:36 +0800951 int masked = ssl->handshake->received_extensions & exts_mask;
Gilles Peskine449bd832023-01-11 14:50:10 +0100952 return masked == exts_mask;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000953}
954
Jerry Yue5991322022-11-07 14:03:44 +0800955#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200956MBEDTLS_CHECK_RETURN_CRITICAL
XiaokangQianb67384d2022-04-19 00:02:38 +0000957static int ssl_tls13_client_hello_has_exts_for_ephemeral_key_exchange(
Gilles Peskine449bd832023-01-11 14:50:10 +0100958 mbedtls_ssl_context *ssl)
XiaokangQian7807f9f2022-02-15 10:04:37 +0000959{
Gilles Peskine449bd832023-01-11 14:50:10 +0100960 return ssl_tls13_client_hello_has_exts(
961 ssl,
962 MBEDTLS_SSL_EXT_MASK(SUPPORTED_GROUPS) |
963 MBEDTLS_SSL_EXT_MASK(KEY_SHARE) |
964 MBEDTLS_SSL_EXT_MASK(SIG_ALG));
XiaokangQian7807f9f2022-02-15 10:04:37 +0000965}
Jerry Yue5991322022-11-07 14:03:44 +0800966#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED */
XiaokangQian7807f9f2022-02-15 10:04:37 +0000967
Jerry Yue5991322022-11-07 14:03:44 +0800968#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK_ENABLED)
Jerry Yu77f01482022-07-11 07:03:24 +0000969MBEDTLS_CHECK_RETURN_CRITICAL
970static int ssl_tls13_client_hello_has_exts_for_psk_key_exchange(
Gilles Peskine449bd832023-01-11 14:50:10 +0100971 mbedtls_ssl_context *ssl)
Jerry Yu77f01482022-07-11 07:03:24 +0000972{
Gilles Peskine449bd832023-01-11 14:50:10 +0100973 return ssl_tls13_client_hello_has_exts(
974 ssl,
975 MBEDTLS_SSL_EXT_MASK(PRE_SHARED_KEY) |
976 MBEDTLS_SSL_EXT_MASK(PSK_KEY_EXCHANGE_MODES));
Jerry Yu77f01482022-07-11 07:03:24 +0000977}
Jerry Yue5991322022-11-07 14:03:44 +0800978#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK_ENABLED */
Jerry Yu77f01482022-07-11 07:03:24 +0000979
Jerry Yue5991322022-11-07 14:03:44 +0800980#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK_EPHEMERAL_ENABLED)
Jerry Yu77f01482022-07-11 07:03:24 +0000981MBEDTLS_CHECK_RETURN_CRITICAL
982static int ssl_tls13_client_hello_has_exts_for_psk_ephemeral_key_exchange(
Gilles Peskine449bd832023-01-11 14:50:10 +0100983 mbedtls_ssl_context *ssl)
Jerry Yu77f01482022-07-11 07:03:24 +0000984{
Gilles Peskine449bd832023-01-11 14:50:10 +0100985 return ssl_tls13_client_hello_has_exts(
986 ssl,
987 MBEDTLS_SSL_EXT_MASK(SUPPORTED_GROUPS) |
988 MBEDTLS_SSL_EXT_MASK(KEY_SHARE) |
989 MBEDTLS_SSL_EXT_MASK(PRE_SHARED_KEY) |
990 MBEDTLS_SSL_EXT_MASK(PSK_KEY_EXCHANGE_MODES));
Jerry Yu77f01482022-07-11 07:03:24 +0000991}
Jerry Yue5991322022-11-07 14:03:44 +0800992#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK_EPHEMERAL_ENABLED */
Jerry Yu77f01482022-07-11 07:03:24 +0000993
Pengyu Lv306a01d2023-02-02 16:35:47 +0800994#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED)
995MBEDTLS_CHECK_RETURN_CRITICAL
Pengyu Lvbc4aab72023-12-01 15:37:24 +0800996static int ssl_tls13_key_exchange_is_psk_available(mbedtls_ssl_context *ssl)
Jerry Yu77f01482022-07-11 07:03:24 +0000997{
Jerry Yue5991322022-11-07 14:03:44 +0800998#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK_ENABLED)
Ronald Crone8c162d2024-02-21 10:15:44 +0100999 return mbedtls_ssl_conf_tls13_is_psk_enabled(ssl) &&
Pengyu Lvb2cfafb2023-11-14 13:56:13 +08001000 mbedtls_ssl_tls13_is_psk_supported(ssl) &&
Gilles Peskine449bd832023-01-11 14:50:10 +01001001 ssl_tls13_client_hello_has_exts_for_psk_key_exchange(ssl);
Jerry Yu77f01482022-07-11 07:03:24 +00001002#else
1003 ((void) ssl);
Gilles Peskine449bd832023-01-11 14:50:10 +01001004 return 0;
Jerry Yu77f01482022-07-11 07:03:24 +00001005#endif
1006}
XiaokangQian7807f9f2022-02-15 10:04:37 +00001007
Jerry Yu77f01482022-07-11 07:03:24 +00001008MBEDTLS_CHECK_RETURN_CRITICAL
Pengyu Lvbc4aab72023-12-01 15:37:24 +08001009static int ssl_tls13_key_exchange_is_psk_ephemeral_available(mbedtls_ssl_context *ssl)
Jerry Yu77f01482022-07-11 07:03:24 +00001010{
Jerry Yue5991322022-11-07 14:03:44 +08001011#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK_EPHEMERAL_ENABLED)
Ronald Crone8c162d2024-02-21 10:15:44 +01001012 return mbedtls_ssl_conf_tls13_is_psk_ephemeral_enabled(ssl) &&
Pengyu Lvb2cfafb2023-11-14 13:56:13 +08001013 mbedtls_ssl_tls13_is_psk_ephemeral_supported(ssl) &&
Gilles Peskine449bd832023-01-11 14:50:10 +01001014 ssl_tls13_client_hello_has_exts_for_psk_ephemeral_key_exchange(ssl);
Jerry Yu77f01482022-07-11 07:03:24 +00001015#else
1016 ((void) ssl);
Gilles Peskine449bd832023-01-11 14:50:10 +01001017 return 0;
Jerry Yu77f01482022-07-11 07:03:24 +00001018#endif
1019}
Ronald Cron79cdd412024-02-20 17:19:28 +01001020#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED */
Jerry Yu77f01482022-07-11 07:03:24 +00001021
Ronald Cron79cdd412024-02-20 17:19:28 +01001022MBEDTLS_CHECK_RETURN_CRITICAL
1023static int ssl_tls13_key_exchange_is_ephemeral_available(mbedtls_ssl_context *ssl)
Jerry Yu77f01482022-07-11 07:03:24 +00001024{
Ronald Cron79cdd412024-02-20 17:19:28 +01001025#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED)
1026 return mbedtls_ssl_conf_tls13_is_ephemeral_enabled(ssl) &&
1027 ssl_tls13_client_hello_has_exts_for_ephemeral_key_exchange(ssl);
1028#else
1029 ((void) ssl);
Gilles Peskine449bd832023-01-11 14:50:10 +01001030 return 0;
Ronald Cron79cdd412024-02-20 17:19:28 +01001031#endif
XiaokangQian7807f9f2022-02-15 10:04:37 +00001032}
1033
XiaokangQian81802f42022-06-10 13:25:22 +00001034#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
Ronald Cron928cbd32022-10-04 16:14:26 +02001035 defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED)
Ronald Cron67ea2542022-09-15 17:34:42 +02001036
1037#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01001038static psa_algorithm_t ssl_tls13_iana_sig_alg_to_psa_alg(uint16_t sig_alg)
Ronald Cron67ea2542022-09-15 17:34:42 +02001039{
Gilles Peskine449bd832023-01-11 14:50:10 +01001040 switch (sig_alg) {
Ronald Cron67ea2542022-09-15 17:34:42 +02001041 case MBEDTLS_TLS1_3_SIG_ECDSA_SECP256R1_SHA256:
Gilles Peskine449bd832023-01-11 14:50:10 +01001042 return PSA_ALG_ECDSA(PSA_ALG_SHA_256);
Ronald Cron67ea2542022-09-15 17:34:42 +02001043 case MBEDTLS_TLS1_3_SIG_ECDSA_SECP384R1_SHA384:
Gilles Peskine449bd832023-01-11 14:50:10 +01001044 return PSA_ALG_ECDSA(PSA_ALG_SHA_384);
Ronald Cron67ea2542022-09-15 17:34:42 +02001045 case MBEDTLS_TLS1_3_SIG_ECDSA_SECP521R1_SHA512:
Gilles Peskine449bd832023-01-11 14:50:10 +01001046 return PSA_ALG_ECDSA(PSA_ALG_SHA_512);
Ronald Cron67ea2542022-09-15 17:34:42 +02001047 case MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA256:
Gilles Peskine449bd832023-01-11 14:50:10 +01001048 return PSA_ALG_RSA_PSS(PSA_ALG_SHA_256);
Ronald Cron67ea2542022-09-15 17:34:42 +02001049 case MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA384:
Gilles Peskine449bd832023-01-11 14:50:10 +01001050 return PSA_ALG_RSA_PSS(PSA_ALG_SHA_384);
Ronald Cron67ea2542022-09-15 17:34:42 +02001051 case MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA512:
Gilles Peskine449bd832023-01-11 14:50:10 +01001052 return PSA_ALG_RSA_PSS(PSA_ALG_SHA_512);
Ronald Cron67ea2542022-09-15 17:34:42 +02001053 case MBEDTLS_TLS1_3_SIG_RSA_PKCS1_SHA256:
Gilles Peskine449bd832023-01-11 14:50:10 +01001054 return PSA_ALG_RSA_PKCS1V15_SIGN(PSA_ALG_SHA_256);
Ronald Cron67ea2542022-09-15 17:34:42 +02001055 case MBEDTLS_TLS1_3_SIG_RSA_PKCS1_SHA384:
Gilles Peskine449bd832023-01-11 14:50:10 +01001056 return PSA_ALG_RSA_PKCS1V15_SIGN(PSA_ALG_SHA_384);
Ronald Cron67ea2542022-09-15 17:34:42 +02001057 case MBEDTLS_TLS1_3_SIG_RSA_PKCS1_SHA512:
Gilles Peskine449bd832023-01-11 14:50:10 +01001058 return PSA_ALG_RSA_PKCS1V15_SIGN(PSA_ALG_SHA_512);
Ronald Cron67ea2542022-09-15 17:34:42 +02001059 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01001060 return PSA_ALG_NONE;
Ronald Cron67ea2542022-09-15 17:34:42 +02001061 }
1062}
1063#endif /* MBEDTLS_USE_PSA_CRYPTO */
1064
XiaokangQian23c5be62022-06-07 02:04:34 +00001065/*
XiaokangQianfb665a82022-06-15 03:57:21 +00001066 * Pick best ( private key, certificate chain ) pair based on the signature
1067 * algorithms supported by the client.
XiaokangQian23c5be62022-06-07 02:04:34 +00001068 */
Ronald Cronce7d76e2022-07-08 18:56:49 +02001069MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01001070static int ssl_tls13_pick_key_cert(mbedtls_ssl_context *ssl)
XiaokangQian23c5be62022-06-07 02:04:34 +00001071{
XiaokangQianfb665a82022-06-15 03:57:21 +00001072 mbedtls_ssl_key_cert *key_cert, *key_cert_list;
XiaokangQian81802f42022-06-10 13:25:22 +00001073 const uint16_t *sig_alg = ssl->handshake->received_sig_algs;
XiaokangQian23c5be62022-06-07 02:04:34 +00001074
1075#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Gilles Peskine449bd832023-01-11 14:50:10 +01001076 if (ssl->handshake->sni_key_cert != NULL) {
XiaokangQianfb665a82022-06-15 03:57:21 +00001077 key_cert_list = ssl->handshake->sni_key_cert;
Gilles Peskine449bd832023-01-11 14:50:10 +01001078 } else
XiaokangQian23c5be62022-06-07 02:04:34 +00001079#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
Gilles Peskine449bd832023-01-11 14:50:10 +01001080 key_cert_list = ssl->conf->key_cert;
XiaokangQian23c5be62022-06-07 02:04:34 +00001081
Gilles Peskine449bd832023-01-11 14:50:10 +01001082 if (key_cert_list == NULL) {
1083 MBEDTLS_SSL_DEBUG_MSG(3, ("server has no certificate"));
1084 return -1;
XiaokangQian23c5be62022-06-07 02:04:34 +00001085 }
1086
Gilles Peskine449bd832023-01-11 14:50:10 +01001087 for (; *sig_alg != MBEDTLS_TLS1_3_SIG_NONE; sig_alg++) {
1088 if (!mbedtls_ssl_sig_alg_is_offered(ssl, *sig_alg)) {
Ronald Cron67ea2542022-09-15 17:34:42 +02001089 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +01001090 }
Ronald Cron67ea2542022-09-15 17:34:42 +02001091
Gilles Peskine449bd832023-01-11 14:50:10 +01001092 if (!mbedtls_ssl_tls13_sig_alg_for_cert_verify_is_supported(*sig_alg)) {
Ronald Cron67ea2542022-09-15 17:34:42 +02001093 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +01001094 }
Ronald Cron67ea2542022-09-15 17:34:42 +02001095
Gilles Peskine449bd832023-01-11 14:50:10 +01001096 for (key_cert = key_cert_list; key_cert != NULL;
1097 key_cert = key_cert->next) {
Ronald Cron67ea2542022-09-15 17:34:42 +02001098#if defined(MBEDTLS_USE_PSA_CRYPTO)
1099 psa_algorithm_t psa_alg = PSA_ALG_NONE;
1100#endif /* MBEDTLS_USE_PSA_CRYPTO */
1101
Gilles Peskine449bd832023-01-11 14:50:10 +01001102 MBEDTLS_SSL_DEBUG_CRT(3, "certificate (chain) candidate",
1103 key_cert->cert);
XiaokangQian81802f42022-06-10 13:25:22 +00001104
1105 /*
Gilles Peskine449bd832023-01-11 14:50:10 +01001106 * This avoids sending the client a cert it'll reject based on
1107 * keyUsage or other extensions.
1108 */
1109 if (mbedtls_x509_crt_check_key_usage(
1110 key_cert->cert, MBEDTLS_X509_KU_DIGITAL_SIGNATURE) != 0 ||
XiaokangQian81802f42022-06-10 13:25:22 +00001111 mbedtls_x509_crt_check_extended_key_usage(
XiaokangQianfb665a82022-06-15 03:57:21 +00001112 key_cert->cert, MBEDTLS_OID_SERVER_AUTH,
Gilles Peskine449bd832023-01-11 14:50:10 +01001113 MBEDTLS_OID_SIZE(MBEDTLS_OID_SERVER_AUTH)) != 0) {
1114 MBEDTLS_SSL_DEBUG_MSG(3, ("certificate mismatch: "
1115 "(extended) key usage extension"));
XiaokangQian81802f42022-06-10 13:25:22 +00001116 continue;
1117 }
XiaokangQian23c5be62022-06-07 02:04:34 +00001118
Gilles Peskine449bd832023-01-11 14:50:10 +01001119 MBEDTLS_SSL_DEBUG_MSG(3,
1120 ("ssl_tls13_pick_key_cert:"
1121 "check signature algorithm %s [%04x]",
1122 mbedtls_ssl_sig_alg_to_str(*sig_alg),
1123 *sig_alg));
Ronald Cron67ea2542022-09-15 17:34:42 +02001124#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01001125 psa_alg = ssl_tls13_iana_sig_alg_to_psa_alg(*sig_alg);
Ronald Cron67ea2542022-09-15 17:34:42 +02001126#endif /* MBEDTLS_USE_PSA_CRYPTO */
1127
Gilles Peskine449bd832023-01-11 14:50:10 +01001128 if (mbedtls_ssl_tls13_check_sig_alg_cert_key_match(
1129 *sig_alg, &key_cert->cert->pk)
Ronald Cron67ea2542022-09-15 17:34:42 +02001130#if defined(MBEDTLS_USE_PSA_CRYPTO)
1131 && psa_alg != PSA_ALG_NONE &&
Gilles Peskine449bd832023-01-11 14:50:10 +01001132 mbedtls_pk_can_do_ext(&key_cert->cert->pk, psa_alg,
1133 PSA_KEY_USAGE_SIGN_HASH) == 1
Ronald Cron67ea2542022-09-15 17:34:42 +02001134#endif /* MBEDTLS_USE_PSA_CRYPTO */
Gilles Peskine449bd832023-01-11 14:50:10 +01001135 ) {
XiaokangQianfb665a82022-06-15 03:57:21 +00001136 ssl->handshake->key_cert = key_cert;
Gilles Peskine449bd832023-01-11 14:50:10 +01001137 MBEDTLS_SSL_DEBUG_MSG(3,
1138 ("ssl_tls13_pick_key_cert:"
1139 "selected signature algorithm"
1140 " %s [%04x]",
1141 mbedtls_ssl_sig_alg_to_str(*sig_alg),
1142 *sig_alg));
XiaokangQianfb665a82022-06-15 03:57:21 +00001143 MBEDTLS_SSL_DEBUG_CRT(
Gilles Peskine449bd832023-01-11 14:50:10 +01001144 3, "selected certificate (chain)",
1145 ssl->handshake->key_cert->cert);
1146 return 0;
XiaokangQian81802f42022-06-10 13:25:22 +00001147 }
XiaokangQian81802f42022-06-10 13:25:22 +00001148 }
XiaokangQian23c5be62022-06-07 02:04:34 +00001149 }
1150
Gilles Peskine449bd832023-01-11 14:50:10 +01001151 MBEDTLS_SSL_DEBUG_MSG(2, ("ssl_tls13_pick_key_cert:"
1152 "no suitable certificate found"));
1153 return -1;
XiaokangQian23c5be62022-06-07 02:04:34 +00001154}
XiaokangQian81802f42022-06-10 13:25:22 +00001155#endif /* MBEDTLS_X509_CRT_PARSE_C &&
Ronald Cron928cbd32022-10-04 16:14:26 +02001156 MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED */
XiaokangQian23c5be62022-06-07 02:04:34 +00001157
XiaokangQian4080a7f2022-04-11 09:55:18 +00001158/*
XiaokangQianed582dd2022-04-13 08:21:05 +00001159 *
1160 * STATE HANDLING: ClientHello
1161 *
XiaokangQianb67384d2022-04-19 00:02:38 +00001162 * There are three possible classes of outcomes when parsing the ClientHello:
XiaokangQianed582dd2022-04-13 08:21:05 +00001163 *
XiaokangQianb67384d2022-04-19 00:02:38 +00001164 * 1) The ClientHello was well-formed and matched the server's configuration.
XiaokangQianed582dd2022-04-13 08:21:05 +00001165 *
1166 * In this case, the server progresses to sending its ServerHello.
1167 *
XiaokangQianb67384d2022-04-19 00:02:38 +00001168 * 2) The ClientHello was well-formed but didn't match the server's
1169 * configuration.
XiaokangQianed582dd2022-04-13 08:21:05 +00001170 *
1171 * For example, the client might not have offered a key share which
1172 * the server supports, or the server might require a cookie.
1173 *
1174 * In this case, the server sends a HelloRetryRequest.
1175 *
XiaokangQianb67384d2022-04-19 00:02:38 +00001176 * 3) The ClientHello was ill-formed
XiaokangQianed582dd2022-04-13 08:21:05 +00001177 *
1178 * In this case, we abort the handshake.
1179 *
1180 */
1181
1182/*
XiaokangQian4080a7f2022-04-11 09:55:18 +00001183 * Structure of this message:
1184 *
XiaokangQiane8ff3502022-04-22 02:34:40 +00001185 * uint16 ProtocolVersion;
1186 * opaque Random[32];
1187 * uint8 CipherSuite[2]; // Cryptographic suite selector
XiaokangQian4080a7f2022-04-11 09:55:18 +00001188 *
XiaokangQiane8ff3502022-04-22 02:34:40 +00001189 * struct {
1190 * ProtocolVersion legacy_version = 0x0303; // TLS v1.2
1191 * Random random;
1192 * opaque legacy_session_id<0..32>;
1193 * CipherSuite cipher_suites<2..2^16-2>;
1194 * opaque legacy_compression_methods<1..2^8-1>;
1195 * Extension extensions<8..2^16-1>;
1196 * } ClientHello;
XiaokangQian4080a7f2022-04-11 09:55:18 +00001197 */
XiaokangQianed582dd2022-04-13 08:21:05 +00001198
1199#define SSL_CLIENT_HELLO_OK 0
1200#define SSL_CLIENT_HELLO_HRR_REQUIRED 1
Ronald Cron5af4c7f2023-03-07 20:46:59 +01001201#define SSL_CLIENT_HELLO_TLS1_2 2
XiaokangQianed582dd2022-04-13 08:21:05 +00001202
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001203MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01001204static int ssl_tls13_parse_client_hello(mbedtls_ssl_context *ssl,
1205 const unsigned char *buf,
1206 const unsigned char *end)
XiaokangQian7807f9f2022-02-15 10:04:37 +00001207{
XiaokangQianb67384d2022-04-19 00:02:38 +00001208 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1209 const unsigned char *p = buf;
Ronald Crond540d992023-03-07 09:41:48 +01001210 const unsigned char *random;
XiaokangQian4080a7f2022-04-11 09:55:18 +00001211 size_t legacy_session_id_len;
Ronald Croncada4102023-03-07 09:51:39 +01001212 const unsigned char *legacy_session_id;
XiaokangQian318dc762022-04-20 09:43:51 +00001213 size_t cipher_suites_len;
Ronald Cron2f16b4e2023-03-07 10:07:32 +01001214 const unsigned char *cipher_suites;
XiaokangQian060d8672022-04-21 09:24:56 +00001215 const unsigned char *cipher_suites_end;
XiaokangQianb67384d2022-04-19 00:02:38 +00001216 size_t extensions_len;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001217 const unsigned char *extensions_end;
Ronald Croneff56732023-04-03 17:36:31 +02001218 const unsigned char *supported_versions_data;
1219 const unsigned char *supported_versions_data_end;
Jerry Yuc4bf5d62022-10-29 09:08:47 +08001220 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
Jerry Yu49ca9282022-05-05 11:05:22 +08001221 int hrr_required = 0;
Ronald Cron8a74f072023-06-14 17:59:29 +02001222 int no_usable_share_for_key_agreement = 0;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001223
Ronald Cron41a443a2022-10-04 16:38:25 +02001224#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED)
Ronald Cron79cdd412024-02-20 17:19:28 +01001225 int got_psk = 0;
1226 struct psk_attributes psk = PSK_ATTRIBUTES_INIT;
Jerry Yu29d9faa2022-08-23 17:52:45 +08001227 const unsigned char *pre_shared_key_ext = NULL;
Jerry Yu1c105562022-07-10 06:32:38 +00001228 const unsigned char *pre_shared_key_ext_end = NULL;
Ronald Cron41a443a2022-10-04 16:38:25 +02001229#endif
XiaokangQian7807f9f2022-02-15 10:04:37 +00001230
XiaokangQian7807f9f2022-02-15 10:04:37 +00001231 /*
XiaokangQianb67384d2022-04-19 00:02:38 +00001232 * ClientHello layout:
XiaokangQian7807f9f2022-02-15 10:04:37 +00001233 * 0 . 1 protocol version
XiaokangQiane8ff3502022-04-22 02:34:40 +00001234 * 2 . 33 random bytes
XiaokangQianc5763b52022-04-02 03:34:37 +00001235 * 34 . 34 session id length ( 1 byte )
XiaokangQian7807f9f2022-02-15 10:04:37 +00001236 * 35 . 34+x session id
XiaokangQian7807f9f2022-02-15 10:04:37 +00001237 * .. . .. ciphersuite list length ( 2 bytes )
1238 * .. . .. ciphersuite list
1239 * .. . .. compression alg. list length ( 1 byte )
1240 * .. . .. compression alg. list
1241 * .. . .. extensions length ( 2 bytes, optional )
1242 * .. . .. extensions ( optional )
1243 */
1244
XiaokangQianb67384d2022-04-19 00:02:38 +00001245 /*
bootstrap-prime6dbbf442022-05-17 19:30:44 -04001246 * Minimal length ( with everything empty and extensions omitted ) is
XiaokangQian7807f9f2022-02-15 10:04:37 +00001247 * 2 + 32 + 1 + 2 + 1 = 38 bytes. Check that first, so that we can
1248 * read at least up to session id length without worrying.
1249 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001250 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, 38);
XiaokangQian7807f9f2022-02-15 10:04:37 +00001251
1252 /* ...
1253 * ProtocolVersion legacy_version = 0x0303; // TLS 1.2
1254 * ...
1255 * with ProtocolVersion defined as:
1256 * uint16 ProtocolVersion;
1257 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001258 if (mbedtls_ssl_read_version(p, ssl->conf->transport) !=
1259 MBEDTLS_SSL_VERSION_TLS1_2) {
1260 MBEDTLS_SSL_DEBUG_MSG(1, ("Unsupported version of TLS."));
1261 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_PROTOCOL_VERSION,
1262 MBEDTLS_ERR_SSL_BAD_PROTOCOL_VERSION);
1263 return MBEDTLS_ERR_SSL_BAD_PROTOCOL_VERSION;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001264 }
1265 p += 2;
1266
Jerry Yuc1be19f2022-04-23 16:11:39 +08001267 /* ...
1268 * Random random;
1269 * ...
XiaokangQianb67384d2022-04-19 00:02:38 +00001270 * with Random defined as:
1271 * opaque Random[32];
XiaokangQian7807f9f2022-02-15 10:04:37 +00001272 */
Ronald Crond540d992023-03-07 09:41:48 +01001273 random = p;
XiaokangQian08037552022-04-20 07:16:41 +00001274 p += MBEDTLS_CLIENT_HELLO_RANDOM_LEN;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001275
Jerry Yuc1be19f2022-04-23 16:11:39 +08001276 /* ...
XiaokangQianb67384d2022-04-19 00:02:38 +00001277 * opaque legacy_session_id<0..32>;
Jerry Yuc1be19f2022-04-23 16:11:39 +08001278 * ...
XiaokangQian7807f9f2022-02-15 10:04:37 +00001279 */
Ronald Croncada4102023-03-07 09:51:39 +01001280 legacy_session_id_len = *(p++);
1281 legacy_session_id = p;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001282
XiaokangQianb67384d2022-04-19 00:02:38 +00001283 /*
1284 * Check we have enough data for the legacy session identifier
Jerry Yue95c8af2022-07-26 15:48:20 +08001285 * and the ciphersuite list length.
XiaokangQianb67384d2022-04-19 00:02:38 +00001286 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001287 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, legacy_session_id_len + 2);
XiaokangQian4080a7f2022-04-11 09:55:18 +00001288 p += legacy_session_id_len;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001289
Ronald Cron2f16b4e2023-03-07 10:07:32 +01001290 /* ...
1291 * CipherSuite cipher_suites<2..2^16-2>;
1292 * ...
1293 * with CipherSuite defined as:
1294 * uint8 CipherSuite[2];
1295 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001296 cipher_suites_len = MBEDTLS_GET_UINT16_BE(p, 0);
XiaokangQian7807f9f2022-02-15 10:04:37 +00001297 p += 2;
Ronald Cron2f16b4e2023-03-07 10:07:32 +01001298 cipher_suites = p;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001299
Ronald Cronfc7ae872023-02-16 15:32:19 +01001300 /*
1301 * The length of the ciphersuite list has to be even.
1302 */
1303 if (cipher_suites_len & 1) {
1304 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR,
1305 MBEDTLS_ERR_SSL_DECODE_ERROR);
1306 return MBEDTLS_ERR_SSL_DECODE_ERROR;
1307 }
1308
XiaokangQianb67384d2022-04-19 00:02:38 +00001309 /* Check we have enough data for the ciphersuite list, the legacy
1310 * compression methods and the length of the extensions.
Jerry Yue95c8af2022-07-26 15:48:20 +08001311 *
1312 * cipher_suites cipher_suites_len bytes
1313 * legacy_compression_methods 2 bytes
1314 * extensions_len 2 bytes
XiaokangQianb67384d2022-04-19 00:02:38 +00001315 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001316 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, cipher_suites_len + 2 + 2);
Ronald Cron8c527d02023-03-07 15:47:47 +01001317 p += cipher_suites_len;
1318 cipher_suites_end = p;
Jerry Yu5725f1c2022-08-21 17:27:16 +08001319
1320 /*
Ronald Cron8c527d02023-03-07 15:47:47 +01001321 * Search for the supported versions extension and parse it to determine
1322 * if the client supports TLS 1.3.
1323 */
1324 ret = mbedtls_ssl_tls13_is_supported_versions_ext_present_in_exts(
1325 ssl, p + 2, end,
Ronald Croneff56732023-04-03 17:36:31 +02001326 &supported_versions_data, &supported_versions_data_end);
Ronald Cron8c527d02023-03-07 15:47:47 +01001327 if (ret < 0) {
1328 MBEDTLS_SSL_DEBUG_RET(1,
1329 ("mbedtls_ssl_tls13_is_supported_versions_ext_present_in_exts"), ret);
1330 return ret;
1331 }
1332
1333 if (ret == 0) {
Ronald Cron5af4c7f2023-03-07 20:46:59 +01001334 return SSL_CLIENT_HELLO_TLS1_2;
Ronald Cron8c527d02023-03-07 15:47:47 +01001335 }
1336
Ronald Cron5af4c7f2023-03-07 20:46:59 +01001337 if (ret == 1) {
1338 ret = ssl_tls13_parse_supported_versions_ext(ssl,
Ronald Croneff56732023-04-03 17:36:31 +02001339 supported_versions_data,
1340 supported_versions_data_end);
Ronald Cron5af4c7f2023-03-07 20:46:59 +01001341 if (ret < 0) {
1342 MBEDTLS_SSL_DEBUG_RET(1,
1343 ("ssl_tls13_parse_supported_versions_ext"), ret);
1344 return ret;
1345 }
1346
Ronald Cronb828c7d2023-04-03 16:37:22 +02001347 /*
1348 * The supported versions extension was parsed successfully as the
1349 * value returned by ssl_tls13_parse_supported_versions_ext() is
1350 * positive. The return value is then equal to
1351 * MBEDTLS_SSL_VERSION_TLS1_2 or MBEDTLS_SSL_VERSION_TLS1_3, defining
1352 * the TLS version to negotiate.
1353 */
Ronald Cron5af4c7f2023-03-07 20:46:59 +01001354 if (MBEDTLS_SSL_VERSION_TLS1_2 == ret) {
1355 return SSL_CLIENT_HELLO_TLS1_2;
1356 }
Ronald Cron8c527d02023-03-07 15:47:47 +01001357 }
1358
1359 /*
1360 * We negotiate TLS 1.3.
Ronald Cron64582392023-03-07 09:21:40 +01001361 */
1362 ssl->tls_version = MBEDTLS_SSL_VERSION_TLS1_3;
Ronald Cron64582392023-03-07 09:21:40 +01001363 ssl->session_negotiate->tls_version = MBEDTLS_SSL_VERSION_TLS1_3;
1364 ssl->session_negotiate->endpoint = ssl->conf->endpoint;
Ronald Cron64582392023-03-07 09:21:40 +01001365
1366 /*
Ronald Crondad02b22023-04-06 09:57:52 +02001367 * We are negotiating the version 1.3 of the protocol. Do what we have
Ronald Croncada4102023-03-07 09:51:39 +01001368 * postponed: copy of the client random bytes, copy of the legacy session
Ronald Cron2f16b4e2023-03-07 10:07:32 +01001369 * identifier and selection of the TLS 1.3 cipher suite.
Ronald Crond540d992023-03-07 09:41:48 +01001370 */
1371 MBEDTLS_SSL_DEBUG_BUF(3, "client hello, random bytes",
1372 random, MBEDTLS_CLIENT_HELLO_RANDOM_LEN);
1373 memcpy(&handshake->randbytes[0], random, MBEDTLS_CLIENT_HELLO_RANDOM_LEN);
1374
Ronald Croncada4102023-03-07 09:51:39 +01001375 if (legacy_session_id_len > sizeof(ssl->session_negotiate->id)) {
1376 MBEDTLS_SSL_DEBUG_MSG(1, ("bad client hello message"));
1377 return MBEDTLS_ERR_SSL_DECODE_ERROR;
1378 }
1379 ssl->session_negotiate->id_len = legacy_session_id_len;
1380 MBEDTLS_SSL_DEBUG_BUF(3, "client hello, session id",
1381 legacy_session_id, legacy_session_id_len);
1382 memcpy(&ssl->session_negotiate->id[0],
1383 legacy_session_id, legacy_session_id_len);
1384
Ronald Crond540d992023-03-07 09:41:48 +01001385 /*
Jerry Yu5725f1c2022-08-21 17:27:16 +08001386 * Search for a matching ciphersuite
1387 */
Ronald Cron2f16b4e2023-03-07 10:07:32 +01001388 MBEDTLS_SSL_DEBUG_BUF(3, "client hello, list of cipher suites",
1389 cipher_suites, cipher_suites_len);
Jerry Yu5725f1c2022-08-21 17:27:16 +08001390
Ronald Cron89089cc2024-02-14 18:18:08 +01001391 ssl_tls13_select_ciphersuite(ssl, cipher_suites, cipher_suites_end,
1392 0, PSA_ALG_NONE, &handshake->ciphersuite_info);
Jerry Yu5725f1c2022-08-21 17:27:16 +08001393
Gilles Peskine449bd832023-01-11 14:50:10 +01001394 if (handshake->ciphersuite_info == NULL) {
1395 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE,
1396 MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE);
1397 return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
Jerry Yu5725f1c2022-08-21 17:27:16 +08001398 }
Ronald Cron89089cc2024-02-14 18:18:08 +01001399 ssl->session_negotiate->ciphersuite = handshake->ciphersuite_info->id;
1400
1401 MBEDTLS_SSL_DEBUG_MSG(2, ("selected ciphersuite: %04x - %s",
1402 ((unsigned) handshake->ciphersuite_info->id),
1403 handshake->ciphersuite_info->name));
Jerry Yuc1be19f2022-04-23 16:11:39 +08001404
XiaokangQian4080a7f2022-04-11 09:55:18 +00001405 /* ...
XiaokangQianb67384d2022-04-19 00:02:38 +00001406 * opaque legacy_compression_methods<1..2^8-1>;
XiaokangQian4080a7f2022-04-11 09:55:18 +00001407 * ...
XiaokangQian7807f9f2022-02-15 10:04:37 +00001408 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001409 if (p[0] != 1 || p[1] != MBEDTLS_SSL_COMPRESS_NULL) {
1410 MBEDTLS_SSL_DEBUG_MSG(1, ("bad legacy compression method"));
1411 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER,
1412 MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER);
1413 return MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001414 }
XiaokangQianb67384d2022-04-19 00:02:38 +00001415 p += 2;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001416
Jerry Yuc1be19f2022-04-23 16:11:39 +08001417 /* ...
XiaokangQianb67384d2022-04-19 00:02:38 +00001418 * Extension extensions<8..2^16-1>;
Jerry Yuc1be19f2022-04-23 16:11:39 +08001419 * ...
XiaokangQianb67384d2022-04-19 00:02:38 +00001420 * with Extension defined as:
1421 * struct {
1422 * ExtensionType extension_type;
1423 * opaque extension_data<0..2^16-1>;
1424 * } Extension;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001425 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001426 extensions_len = MBEDTLS_GET_UINT16_BE(p, 0);
XiaokangQian7807f9f2022-02-15 10:04:37 +00001427 p += 2;
Gilles Peskine449bd832023-01-11 14:50:10 +01001428 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, extensions_len);
XiaokangQiane8ff3502022-04-22 02:34:40 +00001429 extensions_end = p + extensions_len;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001430
Gilles Peskine449bd832023-01-11 14:50:10 +01001431 MBEDTLS_SSL_DEBUG_BUF(3, "client hello extensions", p, extensions_len);
Jerry Yu63a459c2022-10-31 13:38:40 +08001432 handshake->received_extensions = MBEDTLS_SSL_EXT_MASK_NONE;
Jerry Yu0c354a22022-08-29 15:25:36 +08001433
Gilles Peskine449bd832023-01-11 14:50:10 +01001434 while (p < extensions_end) {
XiaokangQian7807f9f2022-02-15 10:04:37 +00001435 unsigned int extension_type;
1436 size_t extension_data_len;
1437 const unsigned char *extension_data_end;
Jerry Yu263dbf72022-10-26 10:51:27 +08001438 uint32_t allowed_exts = MBEDTLS_SSL_TLS1_3_ALLOWED_EXTS_OF_CH;
1439
Ronald Cron5fbd2702024-02-14 10:03:36 +01001440 if (ssl->handshake->hello_retry_request_flag) {
Jerry Yu263dbf72022-10-26 10:51:27 +08001441 /* Do not accept early data extension in 2nd ClientHello */
1442 allowed_exts &= ~MBEDTLS_SSL_EXT_MASK(EARLY_DATA);
1443 }
XiaokangQian7807f9f2022-02-15 10:04:37 +00001444
Jerry Yu0c354a22022-08-29 15:25:36 +08001445 /* RFC 8446, section 4.2.11
Jerry Yu1c9247c2022-07-21 12:37:39 +08001446 *
1447 * The "pre_shared_key" extension MUST be the last extension in the
1448 * ClientHello (this facilitates implementation as described below).
1449 * Servers MUST check that it is the last extension and otherwise fail
1450 * the handshake with an "illegal_parameter" alert.
1451 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001452 if (handshake->received_extensions & MBEDTLS_SSL_EXT_MASK(PRE_SHARED_KEY)) {
Jerry Yu1c9247c2022-07-21 12:37:39 +08001453 MBEDTLS_SSL_DEBUG_MSG(
Gilles Peskine449bd832023-01-11 14:50:10 +01001454 3, ("pre_shared_key is not last extension."));
Jerry Yu1c9247c2022-07-21 12:37:39 +08001455 MBEDTLS_SSL_PEND_FATAL_ALERT(
1456 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER,
Gilles Peskine449bd832023-01-11 14:50:10 +01001457 MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER);
1458 return MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER;
Jerry Yu1c9247c2022-07-21 12:37:39 +08001459 }
1460
Gilles Peskine449bd832023-01-11 14:50:10 +01001461 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, extensions_end, 4);
1462 extension_type = MBEDTLS_GET_UINT16_BE(p, 0);
1463 extension_data_len = MBEDTLS_GET_UINT16_BE(p, 2);
XiaokangQian7807f9f2022-02-15 10:04:37 +00001464 p += 4;
1465
Gilles Peskine449bd832023-01-11 14:50:10 +01001466 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, extensions_end, extension_data_len);
XiaokangQian7807f9f2022-02-15 10:04:37 +00001467 extension_data_end = p + extension_data_len;
1468
Jerry Yuc4bf5d62022-10-29 09:08:47 +08001469 ret = mbedtls_ssl_tls13_check_received_extension(
Gilles Peskine449bd832023-01-11 14:50:10 +01001470 ssl, MBEDTLS_SSL_HS_CLIENT_HELLO, extension_type,
Jerry Yu263dbf72022-10-26 10:51:27 +08001471 allowed_exts);
Gilles Peskine449bd832023-01-11 14:50:10 +01001472 if (ret != 0) {
1473 return ret;
1474 }
Jerry Yue18dc7e2022-08-04 16:29:22 +08001475
Gilles Peskine449bd832023-01-11 14:50:10 +01001476 switch (extension_type) {
XiaokangQian40a35232022-05-07 09:02:40 +00001477#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
1478 case MBEDTLS_TLS_EXT_SERVERNAME:
Gilles Peskine449bd832023-01-11 14:50:10 +01001479 MBEDTLS_SSL_DEBUG_MSG(3, ("found ServerName extension"));
1480 ret = mbedtls_ssl_parse_server_name_ext(ssl, p,
1481 extension_data_end);
1482 if (ret != 0) {
XiaokangQian40a35232022-05-07 09:02:40 +00001483 MBEDTLS_SSL_DEBUG_RET(
Gilles Peskine449bd832023-01-11 14:50:10 +01001484 1, "mbedtls_ssl_parse_servername_ext", ret);
1485 return ret;
XiaokangQian40a35232022-05-07 09:02:40 +00001486 }
XiaokangQian40a35232022-05-07 09:02:40 +00001487 break;
1488#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
1489
Przemek Stekiel8c0a9532023-06-15 16:48:19 +02001490#if defined(PSA_WANT_ALG_ECDH) || defined(PSA_WANT_ALG_FFDH)
XiaokangQian7807f9f2022-02-15 10:04:37 +00001491 case MBEDTLS_TLS_EXT_SUPPORTED_GROUPS:
Gilles Peskine449bd832023-01-11 14:50:10 +01001492 MBEDTLS_SSL_DEBUG_MSG(3, ("found supported group extension"));
XiaokangQian7807f9f2022-02-15 10:04:37 +00001493
1494 /* Supported Groups Extension
1495 *
1496 * When sent by the client, the "supported_groups" extension
1497 * indicates the named groups which the client supports,
1498 * ordered from most preferred to least preferred.
1499 */
Jerry Yuc1be19f2022-04-23 16:11:39 +08001500 ret = ssl_tls13_parse_supported_groups_ext(
Gilles Peskine449bd832023-01-11 14:50:10 +01001501 ssl, p, extension_data_end);
1502 if (ret != 0) {
Xiaokang Qian9f1747b2023-03-30 02:50:04 +00001503 MBEDTLS_SSL_DEBUG_RET(
Xiaokang Qian8bce0e62023-04-04 10:15:48 +00001504 1, "ssl_tls13_parse_supported_groups_ext", ret);
Gilles Peskine449bd832023-01-11 14:50:10 +01001505 return ret;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001506 }
1507
XiaokangQian7807f9f2022-02-15 10:04:37 +00001508 break;
Przemek Stekiel8c0a9532023-06-15 16:48:19 +02001509#endif /* PSA_WANT_ALG_ECDH || PSA_WANT_ALG_FFDH*/
XiaokangQian7807f9f2022-02-15 10:04:37 +00001510
Valerio Settic9ae8622023-07-25 11:23:50 +02001511#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_EPHEMERAL_ENABLED)
XiaokangQian7807f9f2022-02-15 10:04:37 +00001512 case MBEDTLS_TLS_EXT_KEY_SHARE:
Gilles Peskine449bd832023-01-11 14:50:10 +01001513 MBEDTLS_SSL_DEBUG_MSG(3, ("found key share extension"));
XiaokangQian7807f9f2022-02-15 10:04:37 +00001514
1515 /*
1516 * Key Share Extension
1517 *
1518 * When sent by the client, the "key_share" extension
1519 * contains the endpoint's cryptographic parameters for
1520 * ECDHE/DHE key establishment methods.
1521 */
Jerry Yuc1be19f2022-04-23 16:11:39 +08001522 ret = ssl_tls13_parse_key_shares_ext(
Gilles Peskine449bd832023-01-11 14:50:10 +01001523 ssl, p, extension_data_end);
1524 if (ret == SSL_TLS1_3_PARSE_KEY_SHARES_EXT_NO_MATCH) {
Ronald Cron8a74f072023-06-14 17:59:29 +02001525 MBEDTLS_SSL_DEBUG_MSG(2, ("No usable share for key agreement."));
1526 no_usable_share_for_key_agreement = 1;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001527 }
1528
Gilles Peskine449bd832023-01-11 14:50:10 +01001529 if (ret < 0) {
Jerry Yu582dd062022-04-22 21:59:01 +08001530 MBEDTLS_SSL_DEBUG_RET(
Gilles Peskine449bd832023-01-11 14:50:10 +01001531 1, "ssl_tls13_parse_key_shares_ext", ret);
1532 return ret;
Jerry Yu582dd062022-04-22 21:59:01 +08001533 }
XiaokangQian7807f9f2022-02-15 10:04:37 +00001534
XiaokangQian7807f9f2022-02-15 10:04:37 +00001535 break;
Valerio Settic9ae8622023-07-25 11:23:50 +02001536#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_EPHEMERAL_ENABLED */
XiaokangQian7807f9f2022-02-15 10:04:37 +00001537
1538 case MBEDTLS_TLS_EXT_SUPPORTED_VERSIONS:
Ronald Cron8c527d02023-03-07 15:47:47 +01001539 /* Already parsed */
XiaokangQian7807f9f2022-02-15 10:04:37 +00001540 break;
1541
Ronald Cron41a443a2022-10-04 16:38:25 +02001542#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED)
Jerry Yue19e3b92022-07-08 12:04:51 +00001543 case MBEDTLS_TLS_EXT_PSK_KEY_EXCHANGE_MODES:
Xiaokang Qian9f1747b2023-03-30 02:50:04 +00001544 MBEDTLS_SSL_DEBUG_MSG(
1545 3, ("found psk key exchange modes extension"));
Jerry Yue19e3b92022-07-08 12:04:51 +00001546
1547 ret = ssl_tls13_parse_key_exchange_modes_ext(
Gilles Peskine449bd832023-01-11 14:50:10 +01001548 ssl, p, extension_data_end);
1549 if (ret != 0) {
Jerry Yue19e3b92022-07-08 12:04:51 +00001550 MBEDTLS_SSL_DEBUG_RET(
Gilles Peskine449bd832023-01-11 14:50:10 +01001551 1, "ssl_tls13_parse_key_exchange_modes_ext", ret);
1552 return ret;
Jerry Yue19e3b92022-07-08 12:04:51 +00001553 }
1554
Jerry Yue19e3b92022-07-08 12:04:51 +00001555 break;
Ronald Cron41a443a2022-10-04 16:38:25 +02001556#endif
Jerry Yue19e3b92022-07-08 12:04:51 +00001557
Jerry Yu1c105562022-07-10 06:32:38 +00001558 case MBEDTLS_TLS_EXT_PRE_SHARED_KEY:
Gilles Peskine449bd832023-01-11 14:50:10 +01001559 MBEDTLS_SSL_DEBUG_MSG(3, ("found pre_shared_key extension"));
1560 if ((handshake->received_extensions &
1561 MBEDTLS_SSL_EXT_MASK(PSK_KEY_EXCHANGE_MODES)) == 0) {
Jerry Yu13ab81d2022-07-22 23:17:11 +08001562 MBEDTLS_SSL_PEND_FATAL_ALERT(
1563 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER,
Gilles Peskine449bd832023-01-11 14:50:10 +01001564 MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER);
1565 return MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER;
Jerry Yu13ab81d2022-07-22 23:17:11 +08001566 }
Ronald Cron41a443a2022-10-04 16:38:25 +02001567#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED)
Jerry Yu1c105562022-07-10 06:32:38 +00001568 /* Delay processing of the PSK identity once we have
1569 * found out which algorithms to use. We keep a pointer
1570 * to the buffer and the size for later processing.
1571 */
Jerry Yu29d9faa2022-08-23 17:52:45 +08001572 pre_shared_key_ext = p;
Jerry Yu1c105562022-07-10 06:32:38 +00001573 pre_shared_key_ext_end = extension_data_end;
Jerry Yue18dc7e2022-08-04 16:29:22 +08001574#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED */
Jerry Yu1c105562022-07-10 06:32:38 +00001575 break;
Jerry Yu1c105562022-07-10 06:32:38 +00001576
XiaokangQianacb39922022-06-17 10:18:48 +00001577#if defined(MBEDTLS_SSL_ALPN)
1578 case MBEDTLS_TLS_EXT_ALPN:
Gilles Peskine449bd832023-01-11 14:50:10 +01001579 MBEDTLS_SSL_DEBUG_MSG(3, ("found alpn extension"));
XiaokangQianacb39922022-06-17 10:18:48 +00001580
Gilles Peskine449bd832023-01-11 14:50:10 +01001581 ret = mbedtls_ssl_parse_alpn_ext(ssl, p, extension_data_end);
1582 if (ret != 0) {
XiaokangQianacb39922022-06-17 10:18:48 +00001583 MBEDTLS_SSL_DEBUG_RET(
Gilles Peskine449bd832023-01-11 14:50:10 +01001584 1, ("mbedtls_ssl_parse_alpn_ext"), ret);
1585 return ret;
XiaokangQianacb39922022-06-17 10:18:48 +00001586 }
XiaokangQianacb39922022-06-17 10:18:48 +00001587 break;
1588#endif /* MBEDTLS_SSL_ALPN */
1589
Ronald Cron928cbd32022-10-04 16:14:26 +02001590#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED)
XiaokangQian7807f9f2022-02-15 10:04:37 +00001591 case MBEDTLS_TLS_EXT_SIG_ALG:
Gilles Peskine449bd832023-01-11 14:50:10 +01001592 MBEDTLS_SSL_DEBUG_MSG(3, ("found signature_algorithms extension"));
XiaokangQian7807f9f2022-02-15 10:04:37 +00001593
Gabor Mezei078e8032022-04-27 21:17:56 +02001594 ret = mbedtls_ssl_parse_sig_alg_ext(
Gilles Peskine449bd832023-01-11 14:50:10 +01001595 ssl, p, extension_data_end);
1596 if (ret != 0) {
Xiaokang Qian49f39c12023-04-06 02:31:02 +00001597 MBEDTLS_SSL_DEBUG_RET(
1598 1, "mbedtls_ssl_parse_sig_alg_ext", ret);
Gilles Peskine449bd832023-01-11 14:50:10 +01001599 return ret;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001600 }
XiaokangQian7807f9f2022-02-15 10:04:37 +00001601 break;
Ronald Cron928cbd32022-10-04 16:14:26 +02001602#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED */
XiaokangQian7807f9f2022-02-15 10:04:37 +00001603
Jan Bruckner151f6422023-02-10 12:45:19 +01001604#if defined(MBEDTLS_SSL_RECORD_SIZE_LIMIT)
1605 case MBEDTLS_TLS_EXT_RECORD_SIZE_LIMIT:
1606 MBEDTLS_SSL_DEBUG_MSG(3, ("found record_size_limit extension"));
1607
Xiaokang Qian9f1747b2023-03-30 02:50:04 +00001608 ret = mbedtls_ssl_tls13_parse_record_size_limit_ext(
1609 ssl, p, extension_data_end);
Jan Brucknerf482dcc2023-03-15 09:09:06 +01001610 if (ret != 0) {
1611 MBEDTLS_SSL_DEBUG_RET(
1612 1, ("mbedtls_ssl_tls13_parse_record_size_limit_ext"), ret);
1613 return ret;
1614 }
Jan Bruckner151f6422023-02-10 12:45:19 +01001615 break;
1616#endif /* MBEDTLS_SSL_RECORD_SIZE_LIMIT */
1617
XiaokangQian7807f9f2022-02-15 10:04:37 +00001618 default:
Jerry Yu79aa7212022-11-08 21:30:21 +08001619 MBEDTLS_SSL_PRINT_EXT(
Jerry Yu63a459c2022-10-31 13:38:40 +08001620 3, MBEDTLS_SSL_HS_CLIENT_HELLO,
Gilles Peskine449bd832023-01-11 14:50:10 +01001621 extension_type, "( ignored )");
Jerry Yu0c354a22022-08-29 15:25:36 +08001622 break;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001623 }
1624
1625 p += extension_data_len;
1626 }
1627
Gilles Peskine449bd832023-01-11 14:50:10 +01001628 MBEDTLS_SSL_PRINT_EXTS(3, MBEDTLS_SSL_HS_CLIENT_HELLO,
1629 handshake->received_extensions);
Jerry Yue18dc7e2022-08-04 16:29:22 +08001630
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +01001631 ret = mbedtls_ssl_add_hs_hdr_to_checksum(ssl,
1632 MBEDTLS_SSL_HS_CLIENT_HELLO,
1633 p - buf);
1634 if (0 != ret) {
1635 MBEDTLS_SSL_DEBUG_RET(1, ("mbedtls_ssl_add_hs_hdr_to_checksum"), ret);
1636 return ret;
1637 }
Jerry Yu032b15ce2022-07-11 06:10:03 +00001638
Ronald Cron41a443a2022-10-04 16:38:25 +02001639#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED)
XiaokangQian7807f9f2022-02-15 10:04:37 +00001640 /* Update checksum with either
1641 * - The entire content of the CH message, if no PSK extension is present
1642 * - The content up to but excluding the PSK extension, if present.
Ronald Cron12e72f12024-02-14 15:49:38 +01001643 * Always parse the pre-shared key extension when present in the
1644 * ClientHello even if some pre-requisites for PSK key exchange modes are
1645 * not met. That way we always validate the syntax of the extension.
XiaokangQian7807f9f2022-02-15 10:04:37 +00001646 */
Ronald Cron12e72f12024-02-14 15:49:38 +01001647 if (handshake->received_extensions & MBEDTLS_SSL_EXT_MASK(PRE_SHARED_KEY)) {
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +01001648 ret = handshake->update_checksum(ssl, buf,
1649 pre_shared_key_ext - buf);
1650 if (0 != ret) {
1651 MBEDTLS_SSL_DEBUG_RET(1, ("update_checksum"), ret);
1652 return ret;
1653 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001654 ret = ssl_tls13_parse_pre_shared_key_ext(ssl,
1655 pre_shared_key_ext,
1656 pre_shared_key_ext_end,
1657 cipher_suites,
Ronald Cron79cdd412024-02-20 17:19:28 +01001658 cipher_suites_end,
1659 &psk);
1660 if (ret == 0) {
1661 got_psk = 1;
1662 } else if (ret != MBEDTLS_ERR_SSL_UNKNOWN_IDENTITY) {
Jerry Yu63a459c2022-10-31 13:38:40 +08001663 MBEDTLS_SSL_DEBUG_RET(
Gilles Peskine449bd832023-01-11 14:50:10 +01001664 1, "ssl_tls13_parse_pre_shared_key_ext", ret);
1665 return ret;
Jerry Yu1c105562022-07-10 06:32:38 +00001666 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001667 } else
Ronald Cron41a443a2022-10-04 16:38:25 +02001668#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED */
Jerry Yu1c105562022-07-10 06:32:38 +00001669 {
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +01001670 ret = handshake->update_checksum(ssl, buf, p - buf);
1671 if (0 != ret) {
1672 MBEDTLS_SSL_DEBUG_RET(1, ("update_checksum"), ret);
1673 return ret;
1674 }
Jerry Yu1c105562022-07-10 06:32:38 +00001675 }
XiaokangQian7807f9f2022-02-15 10:04:37 +00001676
Ronald Cron79cdd412024-02-20 17:19:28 +01001677 /*
1678 * Determine the key exchange algorithm to use.
1679 * There are three types of key exchanges supported in TLS 1.3:
1680 * - (EC)DH with ECDSA,
1681 * - (EC)DH with PSK,
1682 * - plain PSK.
1683 *
1684 * The PSK-based key exchanges may additionally be used with 0-RTT.
1685 *
1686 * Our built-in order of preference is
1687 * 1 ) (EC)DHE-PSK Mode ( psk_ephemeral )
1688 * 2 ) Certificate Mode ( ephemeral )
1689 * 3 ) Plain PSK Mode ( psk )
1690 */
1691#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED)
1692 if (got_psk && (psk.key_exchange_mode ==
1693 MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK_EPHEMERAL)) {
1694 handshake->key_exchange_mode =
1695 MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK_EPHEMERAL;
1696 MBEDTLS_SSL_DEBUG_MSG(2, ("key exchange mode: psk_ephemeral"));
1697
1698 } else
1699#endif
1700 if (ssl_tls13_key_exchange_is_ephemeral_available(ssl)) {
1701 handshake->key_exchange_mode =
1702 MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL;
1703 MBEDTLS_SSL_DEBUG_MSG(2, ("key exchange mode: ephemeral"));
1704
1705 }
1706#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED)
1707 else if (got_psk && (psk.key_exchange_mode ==
1708 MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK)) {
1709 handshake->key_exchange_mode = MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK;
1710 MBEDTLS_SSL_DEBUG_MSG(2, ("key exchange mode: psk"));
1711 }
1712#endif
1713 else {
1714 MBEDTLS_SSL_DEBUG_MSG(
1715 1,
1716 ("ClientHello message misses mandatory extensions."));
1717 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_MISSING_EXTENSION,
1718 MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER);
1719 return MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER;
Gilles Peskine449bd832023-01-11 14:50:10 +01001720 }
XiaokangQian7807f9f2022-02-15 10:04:37 +00001721
Ronald Cron3e47eec2024-02-21 10:29:24 +01001722#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED)
1723 if ((handshake->key_exchange_mode !=
1724 MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL) &&
1725 (psk.type == MBEDTLS_SSL_TLS1_3_PSK_RESUMPTION)) {
1726 handshake->resume = 1;
Ronald Cron79cdd412024-02-20 17:19:28 +01001727 }
Ronald Cron3e47eec2024-02-21 10:29:24 +01001728#endif
Ronald Cron79cdd412024-02-20 17:19:28 +01001729
1730 if (handshake->key_exchange_mode !=
Ronald Cron8a74f072023-06-14 17:59:29 +02001731 MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK) {
1732 hrr_required = (no_usable_share_for_key_agreement != 0);
1733 }
1734
Gilles Peskine449bd832023-01-11 14:50:10 +01001735 mbedtls_ssl_optimize_checksum(ssl, handshake->ciphersuite_info);
Jerry Yue5834fd2022-08-29 20:16:09 +08001736
Gilles Peskine449bd832023-01-11 14:50:10 +01001737 return hrr_required ? SSL_CLIENT_HELLO_HRR_REQUIRED : SSL_CLIENT_HELLO_OK;
XiaokangQian75fe8c72022-06-15 09:42:45 +00001738}
1739
Jerry Yuab0da372023-02-08 13:55:24 +08001740#if defined(MBEDTLS_SSL_EARLY_DATA)
Jerry Yu4caf3ca2023-11-15 16:13:47 +08001741static int ssl_tls13_check_early_data_requirements(mbedtls_ssl_context *ssl)
Jerry Yuab0da372023-02-08 13:55:24 +08001742{
1743 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
1744
Jerry Yu985c9672022-12-04 14:06:30 +08001745 if (ssl->conf->early_data_enabled == MBEDTLS_SSL_EARLY_DATA_DISABLED) {
1746 MBEDTLS_SSL_DEBUG_MSG(
Jerry Yu985c9672022-12-04 14:06:30 +08001747 1,
Jerry Yu454dda32023-10-31 15:13:54 +08001748 ("EarlyData: rejected, feature disabled in server configuration."));
Jerry Yu4caf3ca2023-11-15 16:13:47 +08001749 return -1;
Ronald Cron7b6ee942024-01-12 10:29:55 +01001750 }
1751
Jerry Yu454dda32023-10-31 15:13:54 +08001752 if (!handshake->resume) {
1753 /* We currently support early data only in the case of PSKs established
1754 via a NewSessionTicket message thus in the case of a session
1755 resumption. */
Jerry Yu985c9672022-12-04 14:06:30 +08001756 MBEDTLS_SSL_DEBUG_MSG(
Jerry Yu7ef9fd82023-11-07 14:30:38 +08001757 1, ("EarlyData: rejected, not a session resumption."));
Jerry Yu4caf3ca2023-11-15 16:13:47 +08001758 return -1;
Jerry Yu985c9672022-12-04 14:06:30 +08001759 }
1760
Jerry Yu82fd6c12023-10-31 16:32:19 +08001761 /* RFC 8446 4.2.10
1762 *
1763 * In order to accept early data, the server MUST have accepted a PSK cipher
1764 * suite and selected the first key offered in the client's "pre_shared_key"
1765 * extension. In addition, it MUST verify that the following values are the
1766 * same as those associated with the selected PSK:
1767 * - The TLS version number
1768 * - The selected cipher suite
1769 * - The selected ALPN [RFC7301] protocol, if any
1770 *
1771 * NOTE:
Jerry Yu7ef9fd82023-11-07 14:30:38 +08001772 * - The TLS version number is checked in
1773 * ssl_tls13_offered_psks_check_identity_match_ticket().
1774 * - ALPN is not checked for the time being (TODO).
Jerry Yu82fd6c12023-10-31 16:32:19 +08001775 */
1776
1777 if (handshake->selected_identity != 0) {
1778 MBEDTLS_SSL_DEBUG_MSG(
Jerry Yu7ef9fd82023-11-07 14:30:38 +08001779 1, ("EarlyData: rejected, the selected key in "
1780 "`pre_shared_key` is not the first one."));
Jerry Yu4caf3ca2023-11-15 16:13:47 +08001781 return -1;
Jerry Yu82fd6c12023-10-31 16:32:19 +08001782 }
1783
1784 if (handshake->ciphersuite_info->id !=
1785 ssl->session_negotiate->ciphersuite) {
1786 MBEDTLS_SSL_DEBUG_MSG(
Jerry Yu7ef9fd82023-11-07 14:30:38 +08001787 1, ("EarlyData: rejected, the selected ciphersuite is not the one "
1788 "of the selected pre-shared key."));
Jerry Yu4caf3ca2023-11-15 16:13:47 +08001789 return -1;
Jerry Yu82fd6c12023-10-31 16:32:19 +08001790
1791 }
Jerry Yu985c9672022-12-04 14:06:30 +08001792
Pengyu Lv94a42cc2023-12-06 10:04:17 +08001793 if (!mbedtls_ssl_tls13_session_ticket_allow_early_data(ssl->session_negotiate)) {
Jerry Yufceddb32022-12-12 15:30:34 +08001794 MBEDTLS_SSL_DEBUG_MSG(
1795 1,
Jerry Yuea96ac32023-11-21 17:06:36 +08001796 ("EarlyData: rejected, early_data not allowed in ticket "
1797 "permission bits."));
Jerry Yu4caf3ca2023-11-15 16:13:47 +08001798 return -1;
Jerry Yufceddb32022-12-12 15:30:34 +08001799 }
Jerry Yu985c9672022-12-04 14:06:30 +08001800
Jerry Yu4caf3ca2023-11-15 16:13:47 +08001801 return 0;
Jerry Yuab0da372023-02-08 13:55:24 +08001802}
1803#endif /* MBEDTLS_SSL_EARLY_DATA */
1804
XiaokangQian75fe8c72022-06-15 09:42:45 +00001805/* Update the handshake state machine */
1806
Ronald Cronce7d76e2022-07-08 18:56:49 +02001807MBEDTLS_CHECK_RETURN_CRITICAL
Ronald Cron7b6ee942024-01-12 10:29:55 +01001808static int ssl_tls13_postprocess_client_hello(mbedtls_ssl_context *ssl,
1809 int hrr_required)
XiaokangQian75fe8c72022-06-15 09:42:45 +00001810{
1811 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1812
XiaokangQian7807f9f2022-02-15 10:04:37 +00001813 /*
XiaokangQianfb665a82022-06-15 03:57:21 +00001814 * Server certificate selection
1815 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001816 if (ssl->conf->f_cert_cb && (ret = ssl->conf->f_cert_cb(ssl)) != 0) {
1817 MBEDTLS_SSL_DEBUG_RET(1, "f_cert_cb", ret);
1818 return ret;
XiaokangQianfb665a82022-06-15 03:57:21 +00001819 }
1820#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
1821 ssl->handshake->sni_name = NULL;
1822 ssl->handshake->sni_name_len = 0;
1823#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
XiaokangQian7807f9f2022-02-15 10:04:37 +00001824
Gilles Peskine449bd832023-01-11 14:50:10 +01001825 ret = mbedtls_ssl_tls13_key_schedule_stage_early(ssl);
1826 if (ret != 0) {
1827 MBEDTLS_SSL_DEBUG_RET(1,
1828 "mbedtls_ssl_tls1_3_key_schedule_stage_early", ret);
1829 return ret;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001830 }
1831
Jerry Yuab0da372023-02-08 13:55:24 +08001832#if defined(MBEDTLS_SSL_EARLY_DATA)
Jerry Yu4caf3ca2023-11-15 16:13:47 +08001833 if (ssl->handshake->received_extensions & MBEDTLS_SSL_EXT_MASK(EARLY_DATA)) {
Ronald Cron31e2d832024-02-05 16:45:57 +01001834 ssl->handshake->early_data_accepted =
1835 (!hrr_required) && (ssl_tls13_check_early_data_requirements(ssl) == 0);
Jerry Yu4e9b70e2022-12-04 14:08:02 +08001836
Jerry Yu4caf3ca2023-11-15 16:13:47 +08001837 if (ssl->handshake->early_data_accepted) {
1838 ret = mbedtls_ssl_tls13_compute_early_transform(ssl);
1839 if (ret != 0) {
1840 MBEDTLS_SSL_DEBUG_RET(
1841 1, "mbedtls_ssl_tls13_compute_early_transform", ret);
1842 return ret;
1843 }
1844 } else {
1845 ssl->discard_early_data_record =
1846 hrr_required ?
1847 MBEDTLS_SSL_EARLY_DATA_DISCARD :
1848 MBEDTLS_SSL_EARLY_DATA_TRY_TO_DEPROTECT_AND_DISCARD;
Jerry Yu4e9b70e2022-12-04 14:08:02 +08001849 }
1850 }
Ronald Cron7b6ee942024-01-12 10:29:55 +01001851#else
1852 ((void) hrr_required);
Jerry Yuab0da372023-02-08 13:55:24 +08001853#endif /* MBEDTLS_SSL_EARLY_DATA */
1854
Gilles Peskine449bd832023-01-11 14:50:10 +01001855 return 0;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001856}
1857
1858/*
XiaokangQianed582dd2022-04-13 08:21:05 +00001859 * Main entry point from the state machine; orchestrates the otherfunctions.
1860 */
1861
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001862MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01001863static int ssl_tls13_process_client_hello(mbedtls_ssl_context *ssl)
XiaokangQianed582dd2022-04-13 08:21:05 +00001864{
1865
XiaokangQian08037552022-04-20 07:16:41 +00001866 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine449bd832023-01-11 14:50:10 +01001867 unsigned char *buf = NULL;
XiaokangQianed582dd2022-04-13 08:21:05 +00001868 size_t buflen = 0;
Jerry Yu4ca91402022-05-09 15:50:57 +08001869 int parse_client_hello_ret;
1870
Gilles Peskine449bd832023-01-11 14:50:10 +01001871 MBEDTLS_SSL_DEBUG_MSG(2, ("=> parse client hello"));
XiaokangQianed582dd2022-04-13 08:21:05 +00001872
Gilles Peskine449bd832023-01-11 14:50:10 +01001873 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_tls13_fetch_handshake_msg(
1874 ssl, MBEDTLS_SSL_HS_CLIENT_HELLO,
1875 &buf, &buflen));
XiaokangQianed582dd2022-04-13 08:21:05 +00001876
Gilles Peskine449bd832023-01-11 14:50:10 +01001877 MBEDTLS_SSL_PROC_CHK_NEG(ssl_tls13_parse_client_hello(ssl, buf,
1878 buf + buflen));
Ronald Cron5af4c7f2023-03-07 20:46:59 +01001879 parse_client_hello_ret = ret; /* Store positive return value of
1880 * parse_client_hello,
1881 * as negative error codes are handled
Jerry Yuf41553b2022-05-09 22:20:30 +08001882 * by MBEDTLS_SSL_PROC_CHK_NEG. */
Jerry Yu4ca91402022-05-09 15:50:57 +08001883
Ronald Cronb828c7d2023-04-03 16:37:22 +02001884 /*
Yanray Wang90acdc62023-12-08 10:29:42 +08001885 * Version 1.2 of the protocol has to be used for the handshake.
1886 * If TLS 1.2 is not supported, abort the handshake. Otherwise, set the
Ronald Cronb828c7d2023-04-03 16:37:22 +02001887 * ssl->keep_current_message flag for the ClientHello to be kept and parsed
1888 * as a TLS 1.2 ClientHello. We also change ssl->tls_version to
1889 * MBEDTLS_SSL_VERSION_TLS1_2 thus from now on mbedtls_ssl_handshake_step()
1890 * will dispatch to the TLS 1.2 state machine.
1891 */
Ronald Cron5af4c7f2023-03-07 20:46:59 +01001892 if (SSL_CLIENT_HELLO_TLS1_2 == parse_client_hello_ret) {
Yanray Wangfb0f47b2023-12-04 15:27:28 +08001893 /* Check if server supports TLS 1.2 */
Yanray Wang408ba6f2023-12-08 10:18:03 +08001894 if (!mbedtls_ssl_conf_is_tls12_enabled(ssl->conf)) {
Yanray Wangfb0f47b2023-12-04 15:27:28 +08001895 MBEDTLS_SSL_DEBUG_MSG(
Yanray Wang177e49a2023-12-08 10:51:04 +08001896 1, ("TLS 1.2 not supported."));
Yanray Wangfb0f47b2023-12-04 15:27:28 +08001897 MBEDTLS_SSL_PEND_FATAL_ALERT(
Yanray Wang2bef9172023-12-08 10:21:53 +08001898 MBEDTLS_SSL_ALERT_MSG_PROTOCOL_VERSION,
1899 MBEDTLS_ERR_SSL_BAD_PROTOCOL_VERSION);
1900 return MBEDTLS_ERR_SSL_BAD_PROTOCOL_VERSION;
Yanray Wangfb0f47b2023-12-04 15:27:28 +08001901 }
Ronald Cron5af4c7f2023-03-07 20:46:59 +01001902 ssl->keep_current_message = 1;
1903 ssl->tls_version = MBEDTLS_SSL_VERSION_TLS1_2;
1904 return 0;
1905 }
1906
Ronald Cron7b6ee942024-01-12 10:29:55 +01001907 MBEDTLS_SSL_PROC_CHK(
1908 ssl_tls13_postprocess_client_hello(ssl, parse_client_hello_ret ==
1909 SSL_CLIENT_HELLO_HRR_REQUIRED));
Jerry Yu582dd062022-04-22 21:59:01 +08001910
Ronald Cron5af4c7f2023-03-07 20:46:59 +01001911 if (SSL_CLIENT_HELLO_OK == parse_client_hello_ret) {
Gilles Peskine449bd832023-01-11 14:50:10 +01001912 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_SERVER_HELLO);
1913 } else {
1914 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_HELLO_RETRY_REQUEST);
1915 }
XiaokangQianed582dd2022-04-13 08:21:05 +00001916
1917cleanup:
1918
Gilles Peskine449bd832023-01-11 14:50:10 +01001919 MBEDTLS_SSL_DEBUG_MSG(2, ("<= parse client hello"));
1920 return ret;
XiaokangQianed582dd2022-04-13 08:21:05 +00001921}
1922
1923/*
Jerry Yu1c3e6882022-04-20 21:23:40 +08001924 * Handler for MBEDTLS_SSL_SERVER_HELLO
Jerry Yu5b64ae92022-03-30 17:15:02 +08001925 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001926MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01001927static int ssl_tls13_prepare_server_hello(mbedtls_ssl_context *ssl)
Jerry Yuf4b27e42022-03-30 17:32:21 +08001928{
Jerry Yu637a3f12022-04-20 21:37:58 +08001929 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1930 unsigned char *server_randbytes =
Gilles Peskine449bd832023-01-11 14:50:10 +01001931 ssl->handshake->randbytes + MBEDTLS_CLIENT_HELLO_RANDOM_LEN;
1932 if (ssl->conf->f_rng == NULL) {
1933 MBEDTLS_SSL_DEBUG_MSG(1, ("no RNG provided"));
1934 return MBEDTLS_ERR_SSL_NO_RNG;
Jerry Yuf4b27e42022-03-30 17:32:21 +08001935 }
1936
Gilles Peskine449bd832023-01-11 14:50:10 +01001937 if ((ret = ssl->conf->f_rng(ssl->conf->p_rng, server_randbytes,
1938 MBEDTLS_SERVER_HELLO_RANDOM_LEN)) != 0) {
1939 MBEDTLS_SSL_DEBUG_RET(1, "f_rng", ret);
1940 return ret;
Jerry Yuf4b27e42022-03-30 17:32:21 +08001941 }
1942
Gilles Peskine449bd832023-01-11 14:50:10 +01001943 MBEDTLS_SSL_DEBUG_BUF(3, "server hello, random bytes", server_randbytes,
1944 MBEDTLS_SERVER_HELLO_RANDOM_LEN);
Jerry Yuf4b27e42022-03-30 17:32:21 +08001945
1946#if defined(MBEDTLS_HAVE_TIME)
YxCda609132023-05-22 12:08:12 -07001947 ssl->session_negotiate->start = mbedtls_time(NULL);
Jerry Yuf4b27e42022-03-30 17:32:21 +08001948#endif /* MBEDTLS_HAVE_TIME */
1949
Gilles Peskine449bd832023-01-11 14:50:10 +01001950 return ret;
Jerry Yuf4b27e42022-03-30 17:32:21 +08001951}
1952
Jerry Yu3bf2c642022-03-30 22:02:12 +08001953/*
Jerry Yue74e04a2022-04-21 09:23:16 +08001954 * ssl_tls13_write_server_hello_supported_versions_ext ():
Jerry Yu3bf2c642022-03-30 22:02:12 +08001955 *
1956 * struct {
Jerry Yufb9f54d2022-04-06 10:08:34 +08001957 * ProtocolVersion selected_version;
Jerry Yu3bf2c642022-03-30 22:02:12 +08001958 * } SupportedVersions;
1959 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001960MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yue74e04a2022-04-21 09:23:16 +08001961static int ssl_tls13_write_server_hello_supported_versions_ext(
Gilles Peskine449bd832023-01-11 14:50:10 +01001962 mbedtls_ssl_context *ssl,
1963 unsigned char *buf,
1964 unsigned char *end,
1965 size_t *out_len)
Jerry Yu3bf2c642022-03-30 22:02:12 +08001966{
Jerry Yu3bf2c642022-03-30 22:02:12 +08001967 *out_len = 0;
1968
Gilles Peskine449bd832023-01-11 14:50:10 +01001969 MBEDTLS_SSL_DEBUG_MSG(3, ("server hello, write selected version"));
Jerry Yu3bf2c642022-03-30 22:02:12 +08001970
1971 /* Check if we have space to write the extension:
1972 * - extension_type (2 bytes)
1973 * - extension_data_length (2 bytes)
Jerry Yu349a6132022-04-14 20:52:56 +08001974 * - selected_version (2 bytes)
Jerry Yu3bf2c642022-03-30 22:02:12 +08001975 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001976 MBEDTLS_SSL_CHK_BUF_PTR(buf, end, 6);
Jerry Yu3bf2c642022-03-30 22:02:12 +08001977
Gilles Peskine449bd832023-01-11 14:50:10 +01001978 MBEDTLS_PUT_UINT16_BE(MBEDTLS_TLS_EXT_SUPPORTED_VERSIONS, buf, 0);
Jerry Yu3bf2c642022-03-30 22:02:12 +08001979
Gilles Peskine449bd832023-01-11 14:50:10 +01001980 MBEDTLS_PUT_UINT16_BE(2, buf, 2);
Jerry Yu3bf2c642022-03-30 22:02:12 +08001981
Gilles Peskine449bd832023-01-11 14:50:10 +01001982 mbedtls_ssl_write_version(buf + 4,
1983 ssl->conf->transport,
1984 ssl->tls_version);
Jerry Yu3bf2c642022-03-30 22:02:12 +08001985
Gilles Peskine449bd832023-01-11 14:50:10 +01001986 MBEDTLS_SSL_DEBUG_MSG(3, ("supported version: [%04x]",
1987 ssl->tls_version));
Jerry Yu3bf2c642022-03-30 22:02:12 +08001988
Jerry Yu349a6132022-04-14 20:52:56 +08001989 *out_len = 6;
Jerry Yu3bf2c642022-03-30 22:02:12 +08001990
Jerry Yub95dd362022-11-08 21:19:34 +08001991 mbedtls_ssl_tls13_set_hs_sent_ext_mask(
Gilles Peskine449bd832023-01-11 14:50:10 +01001992 ssl, MBEDTLS_TLS_EXT_SUPPORTED_VERSIONS);
Jerry Yub95dd362022-11-08 21:19:34 +08001993
Gilles Peskine449bd832023-01-11 14:50:10 +01001994 return 0;
Jerry Yu3bf2c642022-03-30 22:02:12 +08001995}
1996
Jerry Yud9436a12022-04-20 22:28:09 +08001997
Jerry Yu3bf2c642022-03-30 22:02:12 +08001998
1999/* Generate and export a single key share. For hybrid KEMs, this can
2000 * be called multiple times with the different components of the hybrid. */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002001MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002002static int ssl_tls13_generate_and_write_key_share(mbedtls_ssl_context *ssl,
2003 uint16_t named_group,
2004 unsigned char *buf,
2005 unsigned char *end,
2006 size_t *out_len)
Jerry Yu3bf2c642022-03-30 22:02:12 +08002007{
2008 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yu955ddd72022-04-22 22:27:33 +08002009
2010 *out_len = 0;
2011
Valerio Settic9ae8622023-07-25 11:23:50 +02002012#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_EPHEMERAL_ENABLED)
Przemek Stekiel29c219c2023-05-31 15:21:04 +02002013 if (mbedtls_ssl_tls13_named_group_is_ecdhe(named_group) ||
Przemek Stekield5f79e72023-06-29 09:08:43 +02002014 mbedtls_ssl_tls13_named_group_is_ffdh(named_group)) {
Przemek Stekiel408569f2023-07-06 11:26:44 +02002015 ret = mbedtls_ssl_tls13_generate_and_write_xxdh_key_exchange(
Gilles Peskine449bd832023-01-11 14:50:10 +01002016 ssl, named_group, buf, end, out_len);
2017 if (ret != 0) {
Jerry Yu89e103c2022-03-30 22:43:29 +08002018 MBEDTLS_SSL_DEBUG_RET(
Przemek Stekiel408569f2023-07-06 11:26:44 +02002019 1, "mbedtls_ssl_tls13_generate_and_write_xxdh_key_exchange",
Gilles Peskine449bd832023-01-11 14:50:10 +01002020 ret);
2021 return ret;
Jerry Yu3bf2c642022-03-30 22:02:12 +08002022 }
Gilles Peskine449bd832023-01-11 14:50:10 +01002023 } else
Valerio Settic9ae8622023-07-25 11:23:50 +02002024#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_EPHEMERAL_ENABLED */
Gilles Peskine449bd832023-01-11 14:50:10 +01002025 if (0 /* Other kinds of KEMs */) {
2026 } else {
Jerry Yu955ddd72022-04-22 22:27:33 +08002027 ((void) ssl);
2028 ((void) named_group);
2029 ((void) buf);
2030 ((void) end);
Jerry Yu3bf2c642022-03-30 22:02:12 +08002031 ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
2032 }
2033
Gilles Peskine449bd832023-01-11 14:50:10 +01002034 return ret;
Jerry Yu3bf2c642022-03-30 22:02:12 +08002035}
2036
2037/*
2038 * ssl_tls13_write_key_share_ext
2039 *
2040 * Structure of key_share extension in ServerHello:
2041 *
Jerry Yu1c3e6882022-04-20 21:23:40 +08002042 * struct {
2043 * NamedGroup group;
2044 * opaque key_exchange<1..2^16-1>;
2045 * } KeyShareEntry;
2046 * struct {
2047 * KeyShareEntry server_share;
2048 * } KeyShareServerHello;
Jerry Yu3bf2c642022-03-30 22:02:12 +08002049 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002050MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002051static int ssl_tls13_write_key_share_ext(mbedtls_ssl_context *ssl,
2052 unsigned char *buf,
2053 unsigned char *end,
2054 size_t *out_len)
Jerry Yu3bf2c642022-03-30 22:02:12 +08002055{
Jerry Yu955ddd72022-04-22 22:27:33 +08002056 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yu3bf2c642022-03-30 22:02:12 +08002057 unsigned char *p = buf;
Jerry Yu57d48412022-04-20 21:50:42 +08002058 uint16_t group = ssl->handshake->offered_group_id;
Jerry Yu3bf2c642022-03-30 22:02:12 +08002059 unsigned char *server_share = buf + 4;
Jerry Yu3bf2c642022-03-30 22:02:12 +08002060 size_t key_exchange_length;
Jerry Yu3bf2c642022-03-30 22:02:12 +08002061
2062 *out_len = 0;
2063
Gilles Peskine449bd832023-01-11 14:50:10 +01002064 MBEDTLS_SSL_DEBUG_MSG(3, ("server hello, adding key share extension"));
Jerry Yu3bf2c642022-03-30 22:02:12 +08002065
Gilles Peskine449bd832023-01-11 14:50:10 +01002066 MBEDTLS_SSL_DEBUG_MSG(2, ("server hello, write selected_group: %s (%04x)",
2067 mbedtls_ssl_named_group_to_str(group),
2068 group));
Jerry Yu58af2332022-09-06 11:19:31 +08002069
Jerry Yu3bf2c642022-03-30 22:02:12 +08002070 /* Check if we have space for header and length fields:
2071 * - extension_type (2 bytes)
2072 * - extension_data_length (2 bytes)
2073 * - group (2 bytes)
2074 * - key_exchange_length (2 bytes)
2075 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002076 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 8);
2077 MBEDTLS_PUT_UINT16_BE(MBEDTLS_TLS_EXT_KEY_SHARE, p, 0);
2078 MBEDTLS_PUT_UINT16_BE(group, server_share, 0);
Jerry Yu3bf2c642022-03-30 22:02:12 +08002079 p += 8;
Jerry Yu57d48412022-04-20 21:50:42 +08002080
Jerry Yu3bf2c642022-03-30 22:02:12 +08002081 /* When we introduce PQC-ECDHE hybrids, we'll want to call this
2082 * function multiple times. */
Jerry Yu955ddd72022-04-22 22:27:33 +08002083 ret = ssl_tls13_generate_and_write_key_share(
Gilles Peskine449bd832023-01-11 14:50:10 +01002084 ssl, group, server_share + 4, end, &key_exchange_length);
2085 if (ret != 0) {
2086 return ret;
2087 }
Jerry Yu3bf2c642022-03-30 22:02:12 +08002088 p += key_exchange_length;
Jerry Yuc1be19f2022-04-23 16:11:39 +08002089
Gilles Peskine449bd832023-01-11 14:50:10 +01002090 MBEDTLS_PUT_UINT16_BE(key_exchange_length, server_share + 2, 0);
Jerry Yu3bf2c642022-03-30 22:02:12 +08002091
Gilles Peskine449bd832023-01-11 14:50:10 +01002092 MBEDTLS_PUT_UINT16_BE(p - server_share, buf, 2);
Jerry Yu3bf2c642022-03-30 22:02:12 +08002093
Jerry Yu57d48412022-04-20 21:50:42 +08002094 *out_len = p - buf;
Jerry Yu955ddd72022-04-22 22:27:33 +08002095
Gilles Peskine449bd832023-01-11 14:50:10 +01002096 mbedtls_ssl_tls13_set_hs_sent_ext_mask(ssl, MBEDTLS_TLS_EXT_KEY_SHARE);
Jerry Yub95dd362022-11-08 21:19:34 +08002097
Gilles Peskine449bd832023-01-11 14:50:10 +01002098 return 0;
Jerry Yu3bf2c642022-03-30 22:02:12 +08002099}
Jerry Yud9436a12022-04-20 22:28:09 +08002100
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002101MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002102static int ssl_tls13_write_hrr_key_share_ext(mbedtls_ssl_context *ssl,
2103 unsigned char *buf,
2104 unsigned char *end,
2105 size_t *out_len)
Jerry Yu23f7a6f2022-04-23 15:16:45 +08002106{
2107 uint16_t selected_group = ssl->handshake->hrr_selected_group;
2108 /* key_share Extension
2109 *
2110 * struct {
2111 * select (Handshake.msg_type) {
2112 * ...
2113 * case hello_retry_request:
2114 * NamedGroup selected_group;
2115 * ...
2116 * };
2117 * } KeyShare;
2118 */
2119
2120 *out_len = 0;
2121
Jerry Yub0ac10b2022-05-05 11:10:08 +08002122 /*
2123 * For a pure PSK key exchange, there is no group to agree upon. The purpose
2124 * of the HRR is then to transmit a cookie to force the client to demonstrate
2125 * reachability at their apparent network address (primarily useful for DTLS).
2126 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002127 if (!mbedtls_ssl_tls13_key_exchange_mode_with_ephemeral(ssl)) {
2128 return 0;
2129 }
Jerry Yu23f7a6f2022-04-23 15:16:45 +08002130
2131 /* We should only send the key_share extension if the client's initial
2132 * key share was not acceptable. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002133 if (ssl->handshake->offered_group_id != 0) {
2134 MBEDTLS_SSL_DEBUG_MSG(4, ("Skip key_share extension in HRR"));
2135 return 0;
Jerry Yu23f7a6f2022-04-23 15:16:45 +08002136 }
2137
Gilles Peskine449bd832023-01-11 14:50:10 +01002138 if (selected_group == 0) {
2139 MBEDTLS_SSL_DEBUG_MSG(1, ("no matching named group found"));
2140 return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
Jerry Yu23f7a6f2022-04-23 15:16:45 +08002141 }
2142
Jerry Yub0ac10b2022-05-05 11:10:08 +08002143 /* Check if we have enough space:
2144 * - extension_type (2 bytes)
2145 * - extension_data_length (2 bytes)
2146 * - selected_group (2 bytes)
2147 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002148 MBEDTLS_SSL_CHK_BUF_PTR(buf, end, 6);
Jerry Yu23f7a6f2022-04-23 15:16:45 +08002149
Gilles Peskine449bd832023-01-11 14:50:10 +01002150 MBEDTLS_PUT_UINT16_BE(MBEDTLS_TLS_EXT_KEY_SHARE, buf, 0);
2151 MBEDTLS_PUT_UINT16_BE(2, buf, 2);
2152 MBEDTLS_PUT_UINT16_BE(selected_group, buf, 4);
Jerry Yu23f7a6f2022-04-23 15:16:45 +08002153
Gilles Peskine449bd832023-01-11 14:50:10 +01002154 MBEDTLS_SSL_DEBUG_MSG(3,
2155 ("HRR selected_group: %s (%x)",
2156 mbedtls_ssl_named_group_to_str(selected_group),
2157 selected_group));
Jerry Yu23f7a6f2022-04-23 15:16:45 +08002158
2159 *out_len = 6;
Jerry Yu23f7a6f2022-04-23 15:16:45 +08002160
Gilles Peskine449bd832023-01-11 14:50:10 +01002161 mbedtls_ssl_tls13_set_hs_sent_ext_mask(ssl, MBEDTLS_TLS_EXT_KEY_SHARE);
Jerry Yub95dd362022-11-08 21:19:34 +08002162
Gilles Peskine449bd832023-01-11 14:50:10 +01002163 return 0;
Jerry Yu23f7a6f2022-04-23 15:16:45 +08002164}
Jerry Yu3bf2c642022-03-30 22:02:12 +08002165
2166/*
2167 * Structure of ServerHello message:
2168 *
2169 * struct {
2170 * ProtocolVersion legacy_version = 0x0303; // TLS v1.2
2171 * Random random;
2172 * opaque legacy_session_id_echo<0..32>;
2173 * CipherSuite cipher_suite;
2174 * uint8 legacy_compression_method = 0;
2175 * Extension extensions<6..2^16-1>;
2176 * } ServerHello;
2177 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002178MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002179static int ssl_tls13_write_server_hello_body(mbedtls_ssl_context *ssl,
2180 unsigned char *buf,
2181 unsigned char *end,
2182 size_t *out_len,
2183 int is_hrr)
Jerry Yu56404d72022-03-30 17:36:13 +08002184{
Jerry Yu955ddd72022-04-22 22:27:33 +08002185 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yu3bf2c642022-03-30 22:02:12 +08002186 unsigned char *p = buf;
Jerry Yud9436a12022-04-20 22:28:09 +08002187 unsigned char *p_extensions_len;
Jerry Yufbe3e642022-04-25 19:31:51 +08002188 size_t output_len;
Jerry Yu3bf2c642022-03-30 22:02:12 +08002189
2190 *out_len = 0;
Jerry Yu50e00e32022-10-31 14:45:01 +08002191 ssl->handshake->sent_extensions = MBEDTLS_SSL_EXT_MASK_NONE;
Jerry Yu3bf2c642022-03-30 22:02:12 +08002192
Jerry Yucfc04b32022-04-21 09:31:58 +08002193 /* ...
2194 * ProtocolVersion legacy_version = 0x0303; // TLS 1.2
2195 * ...
2196 * with ProtocolVersion defined as:
2197 * uint16 ProtocolVersion;
Jerry Yu3bf2c642022-03-30 22:02:12 +08002198 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002199 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 2);
2200 MBEDTLS_PUT_UINT16_BE(0x0303, p, 0);
Jerry Yu3bf2c642022-03-30 22:02:12 +08002201 p += 2;
2202
Jerry Yu1c3e6882022-04-20 21:23:40 +08002203 /* ...
2204 * Random random;
2205 * ...
Jerry Yucfc04b32022-04-21 09:31:58 +08002206 * with Random defined as:
2207 * opaque Random[MBEDTLS_SERVER_HELLO_RANDOM_LEN];
Jerry Yu1c3e6882022-04-20 21:23:40 +08002208 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002209 MBEDTLS_SSL_CHK_BUF_PTR(p, end, MBEDTLS_SERVER_HELLO_RANDOM_LEN);
2210 if (is_hrr) {
2211 memcpy(p, mbedtls_ssl_tls13_hello_retry_request_magic,
2212 MBEDTLS_SERVER_HELLO_RANDOM_LEN);
2213 } else {
2214 memcpy(p, &ssl->handshake->randbytes[MBEDTLS_CLIENT_HELLO_RANDOM_LEN],
2215 MBEDTLS_SERVER_HELLO_RANDOM_LEN);
Jerry Yu23f7a6f2022-04-23 15:16:45 +08002216 }
Gilles Peskine449bd832023-01-11 14:50:10 +01002217 MBEDTLS_SSL_DEBUG_BUF(3, "server hello, random bytes",
2218 p, MBEDTLS_SERVER_HELLO_RANDOM_LEN);
Jerry Yu3bf2c642022-03-30 22:02:12 +08002219 p += MBEDTLS_SERVER_HELLO_RANDOM_LEN;
2220
Jerry Yucfc04b32022-04-21 09:31:58 +08002221 /* ...
2222 * opaque legacy_session_id_echo<0..32>;
2223 * ...
Jerry Yu3bf2c642022-03-30 22:02:12 +08002224 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002225 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 1 + ssl->session_negotiate->id_len);
2226 *p++ = (unsigned char) ssl->session_negotiate->id_len;
2227 if (ssl->session_negotiate->id_len > 0) {
2228 memcpy(p, &ssl->session_negotiate->id[0],
2229 ssl->session_negotiate->id_len);
Jerry Yu3bf2c642022-03-30 22:02:12 +08002230 p += ssl->session_negotiate->id_len;
Jerry Yu955ddd72022-04-22 22:27:33 +08002231
Gilles Peskine449bd832023-01-11 14:50:10 +01002232 MBEDTLS_SSL_DEBUG_BUF(3, "session id", ssl->session_negotiate->id,
2233 ssl->session_negotiate->id_len);
Jerry Yu3bf2c642022-03-30 22:02:12 +08002234 }
2235
Jerry Yucfc04b32022-04-21 09:31:58 +08002236 /* ...
2237 * CipherSuite cipher_suite;
2238 * ...
2239 * with CipherSuite defined as:
2240 * uint8 CipherSuite[2];
Jerry Yu3bf2c642022-03-30 22:02:12 +08002241 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002242 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 2);
2243 MBEDTLS_PUT_UINT16_BE(ssl->session_negotiate->ciphersuite, p, 0);
Jerry Yu3bf2c642022-03-30 22:02:12 +08002244 p += 2;
Gilles Peskine449bd832023-01-11 14:50:10 +01002245 MBEDTLS_SSL_DEBUG_MSG(3,
2246 ("server hello, chosen ciphersuite: %s ( id=%d )",
2247 mbedtls_ssl_get_ciphersuite_name(
2248 ssl->session_negotiate->ciphersuite),
2249 ssl->session_negotiate->ciphersuite));
Jerry Yu3bf2c642022-03-30 22:02:12 +08002250
Jerry Yucfc04b32022-04-21 09:31:58 +08002251 /* ...
2252 * uint8 legacy_compression_method = 0;
2253 * ...
2254 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002255 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 1);
Thomas Daubney31e03a82022-07-25 15:59:25 +01002256 *p++ = MBEDTLS_SSL_COMPRESS_NULL;
Jerry Yu3bf2c642022-03-30 22:02:12 +08002257
Jerry Yucfc04b32022-04-21 09:31:58 +08002258 /* ...
2259 * Extension extensions<6..2^16-1>;
2260 * ...
2261 * struct {
2262 * ExtensionType extension_type; (2 bytes)
2263 * opaque extension_data<0..2^16-1>;
2264 * } Extension;
2265 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002266 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 2);
Jerry Yud9436a12022-04-20 22:28:09 +08002267 p_extensions_len = p;
Jerry Yu3bf2c642022-03-30 22:02:12 +08002268 p += 2;
2269
Gilles Peskine449bd832023-01-11 14:50:10 +01002270 if ((ret = ssl_tls13_write_server_hello_supported_versions_ext(
2271 ssl, p, end, &output_len)) != 0) {
Jerry Yu955ddd72022-04-22 22:27:33 +08002272 MBEDTLS_SSL_DEBUG_RET(
Gilles Peskine449bd832023-01-11 14:50:10 +01002273 1, "ssl_tls13_write_server_hello_supported_versions_ext", ret);
2274 return ret;
Jerry Yu3bf2c642022-03-30 22:02:12 +08002275 }
2276 p += output_len;
2277
Gilles Peskine449bd832023-01-11 14:50:10 +01002278 if (mbedtls_ssl_tls13_key_exchange_mode_with_ephemeral(ssl)) {
2279 if (is_hrr) {
2280 ret = ssl_tls13_write_hrr_key_share_ext(ssl, p, end, &output_len);
2281 } else {
2282 ret = ssl_tls13_write_key_share_ext(ssl, p, end, &output_len);
2283 }
2284 if (ret != 0) {
2285 return ret;
2286 }
Jerry Yu3bf2c642022-03-30 22:02:12 +08002287 p += output_len;
2288 }
Jerry Yu3bf2c642022-03-30 22:02:12 +08002289
Ronald Cron41a443a2022-10-04 16:38:25 +02002290#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01002291 if (!is_hrr && mbedtls_ssl_tls13_key_exchange_mode_with_psk(ssl)) {
2292 ret = ssl_tls13_write_server_pre_shared_key_ext(ssl, p, end, &output_len);
2293 if (ret != 0) {
2294 MBEDTLS_SSL_DEBUG_RET(1, "ssl_tls13_write_server_pre_shared_key_ext",
2295 ret);
2296 return ret;
Jerry Yu032b15ce2022-07-11 06:10:03 +00002297 }
2298 p += output_len;
2299 }
Ronald Cron41a443a2022-10-04 16:38:25 +02002300#endif
Jerry Yu032b15ce2022-07-11 06:10:03 +00002301
Gilles Peskine449bd832023-01-11 14:50:10 +01002302 MBEDTLS_PUT_UINT16_BE(p - p_extensions_len - 2, p_extensions_len, 0);
Jerry Yu3bf2c642022-03-30 22:02:12 +08002303
Gilles Peskine449bd832023-01-11 14:50:10 +01002304 MBEDTLS_SSL_DEBUG_BUF(4, "server hello extensions",
2305 p_extensions_len, p - p_extensions_len);
Jerry Yu3bf2c642022-03-30 22:02:12 +08002306
Jerry Yud9436a12022-04-20 22:28:09 +08002307 *out_len = p - buf;
Jerry Yu3bf2c642022-03-30 22:02:12 +08002308
Gilles Peskine449bd832023-01-11 14:50:10 +01002309 MBEDTLS_SSL_DEBUG_BUF(3, "server hello", buf, *out_len);
Jerry Yu3bf2c642022-03-30 22:02:12 +08002310
Jerry Yu7de2ff02022-11-08 21:43:46 +08002311 MBEDTLS_SSL_PRINT_EXTS(
Jerry Yu4b8f2f72022-10-31 13:31:22 +08002312 3, is_hrr ? MBEDTLS_SSL_TLS1_3_HS_HELLO_RETRY_REQUEST :
Gilles Peskine449bd832023-01-11 14:50:10 +01002313 MBEDTLS_SSL_HS_SERVER_HELLO,
2314 ssl->handshake->sent_extensions);
Jerry Yu4b8f2f72022-10-31 13:31:22 +08002315
Gilles Peskine449bd832023-01-11 14:50:10 +01002316 return ret;
Jerry Yu56404d72022-03-30 17:36:13 +08002317}
2318
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002319MBEDTLS_CHECK_RETURN_CRITICAL
Xiaokang Qian934ce6f2023-02-06 10:23:04 +00002320static int ssl_tls13_finalize_server_hello(mbedtls_ssl_context *ssl)
Jerry Yue110d252022-05-05 10:19:22 +08002321{
2322 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine449bd832023-01-11 14:50:10 +01002323 ret = mbedtls_ssl_tls13_compute_handshake_transform(ssl);
2324 if (ret != 0) {
2325 MBEDTLS_SSL_DEBUG_RET(1,
2326 "mbedtls_ssl_tls13_compute_handshake_transform",
2327 ret);
2328 return ret;
Jerry Yue110d252022-05-05 10:19:22 +08002329 }
2330
Gilles Peskine449bd832023-01-11 14:50:10 +01002331 return ret;
Jerry Yue110d252022-05-05 10:19:22 +08002332}
2333
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002334MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002335static int ssl_tls13_write_server_hello(mbedtls_ssl_context *ssl)
Jerry Yu5b64ae92022-03-30 17:15:02 +08002336{
Jerry Yu637a3f12022-04-20 21:37:58 +08002337 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yuf4b27e42022-03-30 17:32:21 +08002338 unsigned char *buf;
2339 size_t buf_len, msg_len;
2340
Gilles Peskine449bd832023-01-11 14:50:10 +01002341 MBEDTLS_SSL_DEBUG_MSG(2, ("=> write server hello"));
Jerry Yuf4b27e42022-03-30 17:32:21 +08002342
Gilles Peskine449bd832023-01-11 14:50:10 +01002343 MBEDTLS_SSL_PROC_CHK(ssl_tls13_prepare_server_hello(ssl));
Jerry Yuf4b27e42022-03-30 17:32:21 +08002344
Xiaokang Qian9f1747b2023-03-30 02:50:04 +00002345 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_start_handshake_msg(
2346 ssl, MBEDTLS_SSL_HS_SERVER_HELLO, &buf, &buf_len));
Jerry Yuf4b27e42022-03-30 17:32:21 +08002347
Gilles Peskine449bd832023-01-11 14:50:10 +01002348 MBEDTLS_SSL_PROC_CHK(ssl_tls13_write_server_hello_body(ssl, buf,
2349 buf + buf_len,
2350 &msg_len,
2351 0));
Jerry Yuf4b27e42022-03-30 17:32:21 +08002352
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +01002353 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_add_hs_msg_to_checksum(
Manuel Pégourié-Gonnard43cc1272023-02-06 11:48:19 +01002354 ssl, MBEDTLS_SSL_HS_SERVER_HELLO, buf, msg_len));
Jerry Yuf4b27e42022-03-30 17:32:21 +08002355
Gilles Peskine449bd832023-01-11 14:50:10 +01002356 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_finish_handshake_msg(
2357 ssl, buf_len, msg_len));
Jerry Yu637a3f12022-04-20 21:37:58 +08002358
Xiaokang Qian934ce6f2023-02-06 10:23:04 +00002359 MBEDTLS_SSL_PROC_CHK(ssl_tls13_finalize_server_hello(ssl));
Jerry Yue110d252022-05-05 10:19:22 +08002360
Gabor Mezei7b39bf12022-05-24 16:04:14 +02002361#if defined(MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE)
2362 /* The server sends a dummy change_cipher_spec record immediately
2363 * after its first handshake message. This may either be after
2364 * a ServerHello or a HelloRetryRequest.
2365 */
Gabor Mezei96ae9262022-06-28 11:45:18 +02002366 mbedtls_ssl_handshake_set_state(
Gilles Peskine449bd832023-01-11 14:50:10 +01002367 ssl, MBEDTLS_SSL_SERVER_CCS_AFTER_SERVER_HELLO);
Gabor Mezei7b39bf12022-05-24 16:04:14 +02002368#else
Gilles Peskine449bd832023-01-11 14:50:10 +01002369 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_ENCRYPTED_EXTENSIONS);
Gabor Mezei7b39bf12022-05-24 16:04:14 +02002370#endif /* MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE */
Jerry Yue110d252022-05-05 10:19:22 +08002371
Jerry Yuf4b27e42022-03-30 17:32:21 +08002372cleanup:
2373
Gilles Peskine449bd832023-01-11 14:50:10 +01002374 MBEDTLS_SSL_DEBUG_MSG(2, ("<= write server hello"));
2375 return ret;
Jerry Yu5b64ae92022-03-30 17:15:02 +08002376}
2377
Jerry Yu23d1a252022-05-12 18:08:59 +08002378
2379/*
2380 * Handler for MBEDTLS_SSL_HELLO_RETRY_REQUEST
2381 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002382MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002383static int ssl_tls13_prepare_hello_retry_request(mbedtls_ssl_context *ssl)
Jerry Yu23d1a252022-05-12 18:08:59 +08002384{
2385 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Ronald Cron5fbd2702024-02-14 10:03:36 +01002386 if (ssl->handshake->hello_retry_request_flag) {
Gilles Peskine449bd832023-01-11 14:50:10 +01002387 MBEDTLS_SSL_DEBUG_MSG(1, ("Too many HRRs"));
2388 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE,
2389 MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE);
2390 return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
Jerry Yu23d1a252022-05-12 18:08:59 +08002391 }
2392
2393 /*
2394 * Create stateless transcript hash for HRR
2395 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002396 MBEDTLS_SSL_DEBUG_MSG(4, ("Reset transcript for HRR"));
2397 ret = mbedtls_ssl_reset_transcript_for_hrr(ssl);
2398 if (ret != 0) {
2399 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_reset_transcript_for_hrr", ret);
2400 return ret;
Jerry Yu23d1a252022-05-12 18:08:59 +08002401 }
Gilles Peskine449bd832023-01-11 14:50:10 +01002402 mbedtls_ssl_session_reset_msg_layer(ssl, 0);
Jerry Yu23d1a252022-05-12 18:08:59 +08002403
Gilles Peskine449bd832023-01-11 14:50:10 +01002404 return 0;
Jerry Yu23d1a252022-05-12 18:08:59 +08002405}
2406
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002407MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002408static int ssl_tls13_write_hello_retry_request(mbedtls_ssl_context *ssl)
Jerry Yu23d1a252022-05-12 18:08:59 +08002409{
2410 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
2411 unsigned char *buf;
2412 size_t buf_len, msg_len;
2413
Gilles Peskine449bd832023-01-11 14:50:10 +01002414 MBEDTLS_SSL_DEBUG_MSG(2, ("=> write hello retry request"));
Jerry Yu23d1a252022-05-12 18:08:59 +08002415
Gilles Peskine449bd832023-01-11 14:50:10 +01002416 MBEDTLS_SSL_PROC_CHK(ssl_tls13_prepare_hello_retry_request(ssl));
Jerry Yu23d1a252022-05-12 18:08:59 +08002417
Gilles Peskine449bd832023-01-11 14:50:10 +01002418 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_start_handshake_msg(
2419 ssl, MBEDTLS_SSL_HS_SERVER_HELLO,
2420 &buf, &buf_len));
Jerry Yu23d1a252022-05-12 18:08:59 +08002421
Gilles Peskine449bd832023-01-11 14:50:10 +01002422 MBEDTLS_SSL_PROC_CHK(ssl_tls13_write_server_hello_body(ssl, buf,
2423 buf + buf_len,
2424 &msg_len,
2425 1));
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +01002426 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_add_hs_msg_to_checksum(
Manuel Pégourié-Gonnard43cc1272023-02-06 11:48:19 +01002427 ssl, MBEDTLS_SSL_HS_SERVER_HELLO, buf, msg_len));
Jerry Yu23d1a252022-05-12 18:08:59 +08002428
2429
Gilles Peskine449bd832023-01-11 14:50:10 +01002430 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_finish_handshake_msg(ssl, buf_len,
2431 msg_len));
Jerry Yu23d1a252022-05-12 18:08:59 +08002432
Ronald Cron5fbd2702024-02-14 10:03:36 +01002433 ssl->handshake->hello_retry_request_flag = 1;
Jerry Yu23d1a252022-05-12 18:08:59 +08002434
Gabor Mezei7b39bf12022-05-24 16:04:14 +02002435#if defined(MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE)
2436 /* The server sends a dummy change_cipher_spec record immediately
2437 * after its first handshake message. This may either be after
2438 * a ServerHello or a HelloRetryRequest.
2439 */
Gabor Mezei96ae9262022-06-28 11:45:18 +02002440 mbedtls_ssl_handshake_set_state(
Gilles Peskine449bd832023-01-11 14:50:10 +01002441 ssl, MBEDTLS_SSL_SERVER_CCS_AFTER_HELLO_RETRY_REQUEST);
Gabor Mezei7b39bf12022-05-24 16:04:14 +02002442#else
Gilles Peskine449bd832023-01-11 14:50:10 +01002443 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_CLIENT_HELLO);
Gabor Mezei7b39bf12022-05-24 16:04:14 +02002444#endif /* MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE */
Jerry Yu23d1a252022-05-12 18:08:59 +08002445
2446cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +01002447 MBEDTLS_SSL_DEBUG_MSG(2, ("<= write hello retry request"));
2448 return ret;
Jerry Yu23d1a252022-05-12 18:08:59 +08002449}
2450
Jerry Yu5b64ae92022-03-30 17:15:02 +08002451/*
Jerry Yu4d3841a2022-04-16 12:37:19 +08002452 * Handler for MBEDTLS_SSL_ENCRYPTED_EXTENSIONS
2453 */
Jerry Yu4d3841a2022-04-16 12:37:19 +08002454
2455/*
2456 * struct {
2457 * Extension extensions<0..2 ^ 16 - 1>;
2458 * } EncryptedExtensions;
2459 *
2460 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002461MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002462static int ssl_tls13_write_encrypted_extensions_body(mbedtls_ssl_context *ssl,
2463 unsigned char *buf,
2464 unsigned char *end,
2465 size_t *out_len)
Jerry Yu4d3841a2022-04-16 12:37:19 +08002466{
XiaokangQianacb39922022-06-17 10:18:48 +00002467 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yu4d3841a2022-04-16 12:37:19 +08002468 unsigned char *p = buf;
2469 size_t extensions_len = 0;
Jerry Yu8937eb42022-05-03 12:12:14 +08002470 unsigned char *p_extensions_len;
XiaokangQianacb39922022-06-17 10:18:48 +00002471 size_t output_len;
Jerry Yu9da5e5a2022-05-03 15:46:09 +08002472
Jerry Yu4d3841a2022-04-16 12:37:19 +08002473 *out_len = 0;
2474
Gilles Peskine449bd832023-01-11 14:50:10 +01002475 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 2);
Jerry Yu8937eb42022-05-03 12:12:14 +08002476 p_extensions_len = p;
Jerry Yu4d3841a2022-04-16 12:37:19 +08002477 p += 2;
2478
Jerry Yu8937eb42022-05-03 12:12:14 +08002479 ((void) ssl);
XiaokangQianacb39922022-06-17 10:18:48 +00002480 ((void) ret);
2481 ((void) output_len);
2482
2483#if defined(MBEDTLS_SSL_ALPN)
Gilles Peskine449bd832023-01-11 14:50:10 +01002484 ret = mbedtls_ssl_write_alpn_ext(ssl, p, end, &output_len);
2485 if (ret != 0) {
2486 return ret;
2487 }
XiaokangQian95d5f542022-06-24 02:29:26 +00002488 p += output_len;
XiaokangQianacb39922022-06-17 10:18:48 +00002489#endif /* MBEDTLS_SSL_ALPN */
Jerry Yu4d3841a2022-04-16 12:37:19 +08002490
Jerry Yu71c14f12022-12-12 11:10:35 +08002491#if defined(MBEDTLS_SSL_EARLY_DATA)
Ronald Cron78a38f62024-02-01 18:30:31 +01002492 if (ssl->handshake->early_data_accepted) {
Jerry Yu52335392023-11-23 18:06:06 +08002493 ret = mbedtls_ssl_tls13_write_early_data_ext(
Jerry Yuc59c5862023-12-05 10:40:49 +08002494 ssl, 0, p, end, &output_len);
Jerry Yu71c14f12022-12-12 11:10:35 +08002495 if (ret != 0) {
2496 return ret;
2497 }
2498 p += output_len;
2499 }
2500#endif /* MBEDTLS_SSL_EARLY_DATA */
2501
Waleed Elmelegy47d29462024-01-03 17:31:52 +00002502#if defined(MBEDTLS_SSL_RECORD_SIZE_LIMIT)
Waleed Elmelegyfbe42742024-01-05 18:11:10 +00002503 if (ssl->handshake->received_extensions & MBEDTLS_SSL_EXT_MASK(RECORD_SIZE_LIMIT)) {
Waleed Elmelegyd2fc90e2024-01-04 18:04:53 +00002504 ret = mbedtls_ssl_tls13_write_record_size_limit_ext(
2505 ssl, p, end, &output_len);
2506 if (ret != 0) {
2507 return ret;
2508 }
2509 p += output_len;
Waleed Elmelegy47d29462024-01-03 17:31:52 +00002510 }
Waleed Elmelegy47d29462024-01-03 17:31:52 +00002511#endif
2512
Gilles Peskine449bd832023-01-11 14:50:10 +01002513 extensions_len = (p - p_extensions_len) - 2;
2514 MBEDTLS_PUT_UINT16_BE(extensions_len, p_extensions_len, 0);
Jerry Yu8937eb42022-05-03 12:12:14 +08002515
2516 *out_len = p - buf;
Jerry Yu4d3841a2022-04-16 12:37:19 +08002517
Gilles Peskine449bd832023-01-11 14:50:10 +01002518 MBEDTLS_SSL_DEBUG_BUF(4, "encrypted extensions", buf, *out_len);
Jerry Yu4d3841a2022-04-16 12:37:19 +08002519
Jerry Yu7de2ff02022-11-08 21:43:46 +08002520 MBEDTLS_SSL_PRINT_EXTS(
Gilles Peskine449bd832023-01-11 14:50:10 +01002521 3, MBEDTLS_SSL_HS_ENCRYPTED_EXTENSIONS, ssl->handshake->sent_extensions);
Jerry Yu4b8f2f72022-10-31 13:31:22 +08002522
Gilles Peskine449bd832023-01-11 14:50:10 +01002523 return 0;
Jerry Yu4d3841a2022-04-16 12:37:19 +08002524}
2525
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002526MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002527static int ssl_tls13_write_encrypted_extensions(mbedtls_ssl_context *ssl)
Jerry Yu4d3841a2022-04-16 12:37:19 +08002528{
2529 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
2530 unsigned char *buf;
Jerry Yu39730a72022-05-03 12:14:04 +08002531 size_t buf_len, msg_len;
Jerry Yu4d3841a2022-04-16 12:37:19 +08002532
Gilles Peskine449bd832023-01-11 14:50:10 +01002533 mbedtls_ssl_set_outbound_transform(ssl,
2534 ssl->handshake->transform_handshake);
Gabor Mezei54719122022-06-28 11:34:56 +02002535 MBEDTLS_SSL_DEBUG_MSG(
Gilles Peskine449bd832023-01-11 14:50:10 +01002536 3, ("switching to handshake transform for outbound data"));
Gabor Mezei54719122022-06-28 11:34:56 +02002537
Gilles Peskine449bd832023-01-11 14:50:10 +01002538 MBEDTLS_SSL_DEBUG_MSG(2, ("=> write encrypted extensions"));
Jerry Yu4d3841a2022-04-16 12:37:19 +08002539
Xiaokang Qian9f1747b2023-03-30 02:50:04 +00002540 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_start_handshake_msg(
2541 ssl, MBEDTLS_SSL_HS_ENCRYPTED_EXTENSIONS,
2542 &buf, &buf_len));
Jerry Yu4d3841a2022-04-16 12:37:19 +08002543
Gilles Peskine449bd832023-01-11 14:50:10 +01002544 MBEDTLS_SSL_PROC_CHK(ssl_tls13_write_encrypted_extensions_body(
2545 ssl, buf, buf + buf_len, &msg_len));
Jerry Yu4d3841a2022-04-16 12:37:19 +08002546
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +01002547 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_add_hs_msg_to_checksum(
Xiaokang Qian9f1747b2023-03-30 02:50:04 +00002548 ssl, MBEDTLS_SSL_HS_ENCRYPTED_EXTENSIONS,
2549 buf, msg_len));
Jerry Yu4d3841a2022-04-16 12:37:19 +08002550
Gilles Peskine449bd832023-01-11 14:50:10 +01002551 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_finish_handshake_msg(
2552 ssl, buf_len, msg_len));
Jerry Yu4d3841a2022-04-16 12:37:19 +08002553
Ronald Cron928cbd32022-10-04 16:14:26 +02002554#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01002555 if (mbedtls_ssl_tls13_key_exchange_mode_with_psk(ssl)) {
2556 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_SERVER_FINISHED);
2557 } else {
2558 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_CERTIFICATE_REQUEST);
2559 }
Jerry Yu8937eb42022-05-03 12:12:14 +08002560#else
Gilles Peskine449bd832023-01-11 14:50:10 +01002561 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_SERVER_FINISHED);
Jerry Yu8937eb42022-05-03 12:12:14 +08002562#endif
2563
Jerry Yu4d3841a2022-04-16 12:37:19 +08002564cleanup:
2565
Gilles Peskine449bd832023-01-11 14:50:10 +01002566 MBEDTLS_SSL_DEBUG_MSG(2, ("<= write encrypted extensions"));
2567 return ret;
Jerry Yu4d3841a2022-04-16 12:37:19 +08002568}
2569
Ronald Cron928cbd32022-10-04 16:14:26 +02002570#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED)
XiaokangQiana987e1d2022-05-07 01:25:58 +00002571#define SSL_CERTIFICATE_REQUEST_SEND_REQUEST 0
2572#define SSL_CERTIFICATE_REQUEST_SKIP 1
2573/* Coordination:
2574 * Check whether a CertificateRequest message should be written.
2575 * Returns a negative code on failure, or
2576 * - SSL_CERTIFICATE_REQUEST_SEND_REQUEST
2577 * - SSL_CERTIFICATE_REQUEST_SKIP
2578 * indicating if the writing of the CertificateRequest
2579 * should be skipped or not.
2580 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002581MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002582static int ssl_tls13_certificate_request_coordinate(mbedtls_ssl_context *ssl)
XiaokangQiana987e1d2022-05-07 01:25:58 +00002583{
2584 int authmode;
2585
XiaokangQian40a35232022-05-07 09:02:40 +00002586#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Gilles Peskine449bd832023-01-11 14:50:10 +01002587 if (ssl->handshake->sni_authmode != MBEDTLS_SSL_VERIFY_UNSET) {
XiaokangQian40a35232022-05-07 09:02:40 +00002588 authmode = ssl->handshake->sni_authmode;
Gilles Peskine449bd832023-01-11 14:50:10 +01002589 } else
XiaokangQian40a35232022-05-07 09:02:40 +00002590#endif
XiaokangQiana987e1d2022-05-07 01:25:58 +00002591 authmode = ssl->conf->authmode;
2592
Gilles Peskine449bd832023-01-11 14:50:10 +01002593 if (authmode == MBEDTLS_SSL_VERIFY_NONE) {
Ronald Croneac00ad2022-09-13 10:16:31 +02002594 ssl->session_negotiate->verify_result = MBEDTLS_X509_BADCERT_SKIP_VERIFY;
Gilles Peskine449bd832023-01-11 14:50:10 +01002595 return SSL_CERTIFICATE_REQUEST_SKIP;
Ronald Croneac00ad2022-09-13 10:16:31 +02002596 }
XiaokangQiana987e1d2022-05-07 01:25:58 +00002597
XiaokangQianc3017f62022-05-13 05:55:41 +00002598 ssl->handshake->certificate_request_sent = 1;
XiaokangQian189ded22022-05-10 08:12:17 +00002599
Gilles Peskine449bd832023-01-11 14:50:10 +01002600 return SSL_CERTIFICATE_REQUEST_SEND_REQUEST;
XiaokangQiana987e1d2022-05-07 01:25:58 +00002601}
2602
XiaokangQiancec9ae62022-05-06 07:28:50 +00002603/*
2604 * struct {
2605 * opaque certificate_request_context<0..2^8-1>;
2606 * Extension extensions<2..2^16-1>;
2607 * } CertificateRequest;
2608 *
2609 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002610MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002611static int ssl_tls13_write_certificate_request_body(mbedtls_ssl_context *ssl,
2612 unsigned char *buf,
2613 const unsigned char *end,
2614 size_t *out_len)
XiaokangQiancec9ae62022-05-06 07:28:50 +00002615{
2616 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
2617 unsigned char *p = buf;
XiaokangQianec6efb92022-05-06 09:53:10 +00002618 size_t output_len = 0;
XiaokangQiancec9ae62022-05-06 07:28:50 +00002619 unsigned char *p_extensions_len;
2620
2621 *out_len = 0;
2622
2623 /* Check if we have enough space:
2624 * - certificate_request_context (1 byte)
2625 * - extensions length (2 bytes)
2626 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002627 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 3);
XiaokangQiancec9ae62022-05-06 07:28:50 +00002628
2629 /*
2630 * Write certificate_request_context
2631 */
2632 /*
2633 * We use a zero length context for the normal handshake
2634 * messages. For post-authentication handshake messages
2635 * this request context would be set to a non-zero value.
2636 */
2637 *p++ = 0x0;
2638
2639 /*
2640 * Write extensions
2641 */
2642 /* The extensions must contain the signature_algorithms. */
2643 p_extensions_len = p;
2644 p += 2;
Gilles Peskine449bd832023-01-11 14:50:10 +01002645 ret = mbedtls_ssl_write_sig_alg_ext(ssl, p, end, &output_len);
2646 if (ret != 0) {
2647 return ret;
2648 }
XiaokangQiancec9ae62022-05-06 07:28:50 +00002649
XiaokangQianec6efb92022-05-06 09:53:10 +00002650 p += output_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01002651 MBEDTLS_PUT_UINT16_BE(p - p_extensions_len - 2, p_extensions_len, 0);
XiaokangQiancec9ae62022-05-06 07:28:50 +00002652
2653 *out_len = p - buf;
2654
Jerry Yu7de2ff02022-11-08 21:43:46 +08002655 MBEDTLS_SSL_PRINT_EXTS(
Gilles Peskine449bd832023-01-11 14:50:10 +01002656 3, MBEDTLS_SSL_HS_CERTIFICATE_REQUEST, ssl->handshake->sent_extensions);
Jerry Yu4b8f2f72022-10-31 13:31:22 +08002657
Gilles Peskine449bd832023-01-11 14:50:10 +01002658 return 0;
XiaokangQiancec9ae62022-05-06 07:28:50 +00002659}
2660
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002661MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002662static int ssl_tls13_write_certificate_request(mbedtls_ssl_context *ssl)
XiaokangQiancec9ae62022-05-06 07:28:50 +00002663{
2664 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
2665
Gilles Peskine449bd832023-01-11 14:50:10 +01002666 MBEDTLS_SSL_DEBUG_MSG(2, ("=> write certificate request"));
XiaokangQiancec9ae62022-05-06 07:28:50 +00002667
Gilles Peskine449bd832023-01-11 14:50:10 +01002668 MBEDTLS_SSL_PROC_CHK_NEG(ssl_tls13_certificate_request_coordinate(ssl));
XiaokangQiancec9ae62022-05-06 07:28:50 +00002669
Gilles Peskine449bd832023-01-11 14:50:10 +01002670 if (ret == SSL_CERTIFICATE_REQUEST_SEND_REQUEST) {
XiaokangQiancec9ae62022-05-06 07:28:50 +00002671 unsigned char *buf;
2672 size_t buf_len, msg_len;
2673
Xiaokang Qian9f1747b2023-03-30 02:50:04 +00002674 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_start_handshake_msg(
2675 ssl, MBEDTLS_SSL_HS_CERTIFICATE_REQUEST,
2676 &buf, &buf_len));
XiaokangQiancec9ae62022-05-06 07:28:50 +00002677
Gilles Peskine449bd832023-01-11 14:50:10 +01002678 MBEDTLS_SSL_PROC_CHK(ssl_tls13_write_certificate_request_body(
2679 ssl, buf, buf + buf_len, &msg_len));
XiaokangQiancec9ae62022-05-06 07:28:50 +00002680
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +01002681 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_add_hs_msg_to_checksum(
Xiaokang Qian9f1747b2023-03-30 02:50:04 +00002682 ssl, MBEDTLS_SSL_HS_CERTIFICATE_REQUEST,
2683 buf, msg_len));
XiaokangQiancec9ae62022-05-06 07:28:50 +00002684
Gilles Peskine449bd832023-01-11 14:50:10 +01002685 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_finish_handshake_msg(
2686 ssl, buf_len, msg_len));
2687 } else if (ret == SSL_CERTIFICATE_REQUEST_SKIP) {
2688 MBEDTLS_SSL_DEBUG_MSG(2, ("<= skip write certificate request"));
XiaokangQiancec9ae62022-05-06 07:28:50 +00002689 ret = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01002690 } else {
2691 MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
XiaokangQiancec9ae62022-05-06 07:28:50 +00002692 ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
2693 goto cleanup;
2694 }
2695
Gilles Peskine449bd832023-01-11 14:50:10 +01002696 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_SERVER_CERTIFICATE);
XiaokangQiancec9ae62022-05-06 07:28:50 +00002697cleanup:
2698
Gilles Peskine449bd832023-01-11 14:50:10 +01002699 MBEDTLS_SSL_DEBUG_MSG(2, ("<= write certificate request"));
2700 return ret;
XiaokangQiancec9ae62022-05-06 07:28:50 +00002701}
XiaokangQiancec9ae62022-05-06 07:28:50 +00002702
Jerry Yu4d3841a2022-04-16 12:37:19 +08002703/*
Jerry Yuc6e6dbf2022-04-16 19:42:57 +08002704 * Handler for MBEDTLS_SSL_SERVER_CERTIFICATE
Jerry Yu83da34e2022-04-16 13:59:52 +08002705 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002706MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002707static int ssl_tls13_write_server_certificate(mbedtls_ssl_context *ssl)
Jerry Yu83da34e2022-04-16 13:59:52 +08002708{
Jerry Yu5a26f302022-05-10 20:46:40 +08002709 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
XiaokangQianfb665a82022-06-15 03:57:21 +00002710
2711#if defined(MBEDTLS_X509_CRT_PARSE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01002712 if ((ssl_tls13_pick_key_cert(ssl) != 0) ||
2713 mbedtls_ssl_own_cert(ssl) == NULL) {
2714 MBEDTLS_SSL_DEBUG_MSG(2, ("No certificate available."));
2715 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE,
2716 MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE);
2717 return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
Jerry Yu5a26f302022-05-10 20:46:40 +08002718 }
XiaokangQianfb665a82022-06-15 03:57:21 +00002719#endif /* MBEDTLS_X509_CRT_PARSE_C */
Jerry Yu5a26f302022-05-10 20:46:40 +08002720
Gilles Peskine449bd832023-01-11 14:50:10 +01002721 ret = mbedtls_ssl_tls13_write_certificate(ssl);
2722 if (ret != 0) {
2723 return ret;
2724 }
2725 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_CERTIFICATE_VERIFY);
2726 return 0;
Jerry Yu83da34e2022-04-16 13:59:52 +08002727}
2728
2729/*
Jerry Yuc6e6dbf2022-04-16 19:42:57 +08002730 * Handler for MBEDTLS_SSL_CERTIFICATE_VERIFY
Jerry Yu83da34e2022-04-16 13:59:52 +08002731 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002732MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002733static int ssl_tls13_write_certificate_verify(mbedtls_ssl_context *ssl)
Jerry Yu83da34e2022-04-16 13:59:52 +08002734{
Gilles Peskine449bd832023-01-11 14:50:10 +01002735 int ret = mbedtls_ssl_tls13_write_certificate_verify(ssl);
2736 if (ret != 0) {
2737 return ret;
2738 }
2739 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_SERVER_FINISHED);
2740 return 0;
Jerry Yu83da34e2022-04-16 13:59:52 +08002741}
Ronald Cron928cbd32022-10-04 16:14:26 +02002742#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED */
Jerry Yu83da34e2022-04-16 13:59:52 +08002743
Jerry Yu9b72e392023-12-01 16:27:08 +08002744/*
2745 * RFC 8446 section A.2
2746 *
2747 * | Send ServerHello
2748 * | K_send = handshake
2749 * | Send EncryptedExtensions
2750 * | [Send CertificateRequest]
2751 * Can send | [Send Certificate + CertificateVerify]
2752 * app data | Send Finished
2753 * after --> | K_send = application
2754 * here +--------+--------+
2755 * No 0-RTT | | 0-RTT
2756 * | |
2757 * K_recv = handshake | | K_recv = early data
2758 * [Skip decrypt errors] | +------> WAIT_EOED -+
2759 * | | Recv | | Recv EndOfEarlyData
2760 * | | early data | | K_recv = handshake
2761 * | +------------+ |
2762 * | |
2763 * +> WAIT_FLIGHT2 <--------+
2764 * |
2765 * +--------+--------+
2766 * No auth | | Client auth
2767 * | |
2768 * | v
2769 * | WAIT_CERT
2770 * | Recv | | Recv Certificate
2771 * | empty | v
2772 * | Certificate | WAIT_CV
2773 * | | | Recv
2774 * | v | CertificateVerify
2775 * +-> WAIT_FINISHED <---+
2776 * | Recv Finished
2777 *
2778 *
2779 * The following function handles the state changes after WAIT_FLIGHT2 in the
Jerry Yu3be85072023-12-04 09:58:54 +08002780 * above diagram. We are not going to receive early data related messages
2781 * anymore, prepare to receive the first handshake message of the client
2782 * second flight.
Jerry Yu9b72e392023-12-01 16:27:08 +08002783 */
Jerry Yu3be85072023-12-04 09:58:54 +08002784static void ssl_tls13_prepare_for_handshake_second_flight(
2785 mbedtls_ssl_context *ssl)
Jerry Yu9b72e392023-12-01 16:27:08 +08002786{
Jerry Yu9b72e392023-12-01 16:27:08 +08002787 if (ssl->handshake->certificate_request_sent) {
2788 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_CLIENT_CERTIFICATE);
2789 } else {
Jerry Yu42020fb2023-12-05 17:35:53 +08002790 MBEDTLS_SSL_DEBUG_MSG(2, ("skip parse certificate"));
Jerry Yuebb1b1d2023-12-05 11:02:15 +08002791 MBEDTLS_SSL_DEBUG_MSG(2, ("skip parse certificate verify"));
Jerry Yuebb1b1d2023-12-05 11:02:15 +08002792
Jerry Yu9b72e392023-12-01 16:27:08 +08002793 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_CLIENT_FINISHED);
2794 }
Jerry Yu9b72e392023-12-01 16:27:08 +08002795}
2796
Jerry Yu83da34e2022-04-16 13:59:52 +08002797/*
Jerry Yud6e253d2022-05-18 13:59:24 +08002798 * Handler for MBEDTLS_SSL_SERVER_FINISHED
Jerry Yu69dd8d42022-04-16 12:51:26 +08002799 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002800MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002801static int ssl_tls13_write_server_finished(mbedtls_ssl_context *ssl)
Jerry Yu69dd8d42022-04-16 12:51:26 +08002802{
Jerry Yud6e253d2022-05-18 13:59:24 +08002803 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yu27bdc7c2022-04-16 13:33:27 +08002804
Gilles Peskine449bd832023-01-11 14:50:10 +01002805 ret = mbedtls_ssl_tls13_write_finished_message(ssl);
2806 if (ret != 0) {
2807 return ret;
2808 }
Jerry Yu27bdc7c2022-04-16 13:33:27 +08002809
Gilles Peskine449bd832023-01-11 14:50:10 +01002810 ret = mbedtls_ssl_tls13_compute_application_transform(ssl);
2811 if (ret != 0) {
Jerry Yue3d67cb2022-05-19 15:33:10 +08002812 MBEDTLS_SSL_PEND_FATAL_ALERT(
Gilles Peskine449bd832023-01-11 14:50:10 +01002813 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE,
2814 MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE);
2815 return ret;
Jerry Yue3d67cb2022-05-19 15:33:10 +08002816 }
XiaokangQianc3017f62022-05-13 05:55:41 +00002817
Jerry Yu87b5ed42022-12-12 13:07:07 +08002818#if defined(MBEDTLS_SSL_EARLY_DATA)
Ronald Cron78a38f62024-02-01 18:30:31 +01002819 if (ssl->handshake->early_data_accepted) {
Jerry Yu0af63dc2023-12-01 17:14:51 +08002820 /* See RFC 8446 section A.2 for more information */
Jerry Yu87b5ed42022-12-12 13:07:07 +08002821 MBEDTLS_SSL_DEBUG_MSG(
2822 1, ("Switch to early keys for inbound traffic. "
2823 "( K_recv = early data )"));
2824 mbedtls_ssl_set_inbound_transform(
2825 ssl, ssl->handshake->transform_earlydata);
2826 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_END_OF_EARLY_DATA);
2827 return 0;
2828 }
2829#endif /* MBEDTLS_SSL_EARLY_DATA */
Jerry Yu0af63dc2023-12-01 17:14:51 +08002830 MBEDTLS_SSL_DEBUG_MSG(
2831 1, ("Switch to handshake keys for inbound traffic "
2832 "( K_recv = handshake )"));
Gilles Peskine449bd832023-01-11 14:50:10 +01002833 mbedtls_ssl_set_inbound_transform(ssl, ssl->handshake->transform_handshake);
XiaokangQian189ded22022-05-10 08:12:17 +00002834
Jerry Yu3be85072023-12-04 09:58:54 +08002835 ssl_tls13_prepare_for_handshake_second_flight(ssl);
Jerry Yu7d8c3fe2022-12-12 12:59:44 +08002836
2837 return 0;
2838}
2839
Jerry Yu87b5ed42022-12-12 13:07:07 +08002840#if defined(MBEDTLS_SSL_EARLY_DATA)
2841/*
Jerry Yu59d420f2023-12-01 16:30:34 +08002842 * Handler for MBEDTLS_SSL_END_OF_EARLY_DATA
Jerry Yu87b5ed42022-12-12 13:07:07 +08002843 */
Jerry Yu3be85072023-12-04 09:58:54 +08002844#define SSL_GOT_END_OF_EARLY_DATA 0
Jerry Yub55f9eb2023-12-05 10:27:17 +08002845#define SSL_GOT_EARLY_DATA 1
Jerry Yud5c34962023-12-01 16:32:31 +08002846/* Coordination:
Jerry Yu3be85072023-12-04 09:58:54 +08002847 * Deals with the ambiguity of not knowing if the next message is an
2848 * EndOfEarlyData message or an application message containing early data.
Jerry Yud5c34962023-12-01 16:32:31 +08002849 * Returns a negative code on failure, or
Jerry Yu3be85072023-12-04 09:58:54 +08002850 * - SSL_GOT_END_OF_EARLY_DATA
Jerry Yub55f9eb2023-12-05 10:27:17 +08002851 * - SSL_GOT_EARLY_DATA
Jerry Yud5c34962023-12-01 16:32:31 +08002852 * indicating which message is received.
2853 */
2854MBEDTLS_CHECK_RETURN_CRITICAL
2855static int ssl_tls13_end_of_early_data_coordinate(mbedtls_ssl_context *ssl)
2856{
2857 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yub4ed4602023-12-01 16:34:00 +08002858
2859 if ((ret = mbedtls_ssl_read_record(ssl, 0)) != 0) {
2860 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_read_record", ret);
2861 return ret;
2862 }
2863 ssl->keep_current_message = 1;
2864
2865 if (ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
2866 ssl->in_msg[0] == MBEDTLS_SSL_HS_END_OF_EARLY_DATA) {
Jerry Yub55f9eb2023-12-05 10:27:17 +08002867 MBEDTLS_SSL_DEBUG_MSG(3, ("Received an end_of_early_data message."));
Jerry Yu3be85072023-12-04 09:58:54 +08002868 return SSL_GOT_END_OF_EARLY_DATA;
Jerry Yub4ed4602023-12-01 16:34:00 +08002869 }
2870
2871 if (ssl->in_msgtype == MBEDTLS_SSL_MSG_APPLICATION_DATA) {
Jerry Yub55f9eb2023-12-05 10:27:17 +08002872 MBEDTLS_SSL_DEBUG_MSG(3, ("Received early data"));
Jerry Yu6a5904d2023-12-06 17:11:12 +08002873 /* RFC 8446 section 4.6.1
2874 *
2875 * A server receiving more than max_early_data_size bytes of 0-RTT data
2876 * SHOULD terminate the connection with an "unexpected_message" alert.
2877 *
2878 * TODO: Add received data size check here.
2879 */
Ronald Croned7d4bf2024-01-31 07:55:19 +01002880 if (ssl->in_offt == NULL) {
2881 /* Set the reading pointer */
2882 ssl->in_offt = ssl->in_msg;
2883 }
Jerry Yub55f9eb2023-12-05 10:27:17 +08002884 return SSL_GOT_EARLY_DATA;
Jerry Yub4ed4602023-12-01 16:34:00 +08002885 }
2886
Jerry Yu7bb40a32023-12-04 10:04:15 +08002887 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE,
2888 MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE);
Jerry Yub4ed4602023-12-01 16:34:00 +08002889 return MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE;
Jerry Yud5c34962023-12-01 16:32:31 +08002890}
2891
2892MBEDTLS_CHECK_RETURN_CRITICAL
2893static int ssl_tls13_parse_end_of_early_data(mbedtls_ssl_context *ssl,
2894 const unsigned char *buf,
2895 const unsigned char *end)
2896{
Jerry Yu75c9ab72023-12-01 16:41:40 +08002897 /* RFC 8446 section 4.5
2898 *
2899 * struct {} EndOfEarlyData;
2900 */
Jerry Yufbf03992023-12-04 10:00:37 +08002901 if (buf != end) {
2902 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR,
2903 MBEDTLS_ERR_SSL_DECODE_ERROR);
2904 return MBEDTLS_ERR_SSL_DECODE_ERROR;
2905 }
2906 return 0;
Jerry Yud5c34962023-12-01 16:32:31 +08002907}
2908
Jerry Yud5c34962023-12-01 16:32:31 +08002909/*
2910 * RFC 8446 section A.2
2911 *
2912 * | Send ServerHello
2913 * | K_send = handshake
2914 * | Send EncryptedExtensions
2915 * | [Send CertificateRequest]
2916 * Can send | [Send Certificate + CertificateVerify]
2917 * app data | Send Finished
2918 * after --> | K_send = application
2919 * here +--------+--------+
2920 * No 0-RTT | | 0-RTT
2921 * | |
2922 * K_recv = handshake | | K_recv = early data
2923 * [Skip decrypt errors] | +------> WAIT_EOED -+
2924 * | | Recv | | Recv EndOfEarlyData
2925 * | | early data | | K_recv = handshake
2926 * | +------------+ |
2927 * | |
2928 * +> WAIT_FLIGHT2 <--------+
2929 * |
2930 * +--------+--------+
2931 * No auth | | Client auth
2932 * | |
2933 * | v
2934 * | WAIT_CERT
2935 * | Recv | | Recv Certificate
2936 * | empty | v
2937 * | Certificate | WAIT_CV
2938 * | | | Recv
2939 * | v | CertificateVerify
2940 * +-> WAIT_FINISHED <---+
2941 * | Recv Finished
2942 *
2943 * The function handles actions and state changes from 0-RTT to WAIT_FLIGHT2 in
2944 * the above diagram.
2945 */
Jerry Yu87b5ed42022-12-12 13:07:07 +08002946MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yu59d420f2023-12-01 16:30:34 +08002947static int ssl_tls13_process_end_of_early_data(mbedtls_ssl_context *ssl)
Jerry Yu87b5ed42022-12-12 13:07:07 +08002948{
2949 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yud5c34962023-12-01 16:32:31 +08002950
2951 MBEDTLS_SSL_DEBUG_MSG(2, ("=> ssl_tls13_process_end_of_early_data"));
2952
2953 MBEDTLS_SSL_PROC_CHK_NEG(ssl_tls13_end_of_early_data_coordinate(ssl));
2954
Jerry Yu3be85072023-12-04 09:58:54 +08002955 if (ret == SSL_GOT_END_OF_EARLY_DATA) {
Jerry Yud5c34962023-12-01 16:32:31 +08002956 unsigned char *buf;
2957 size_t buf_len;
2958
2959 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_tls13_fetch_handshake_msg(
2960 ssl, MBEDTLS_SSL_HS_END_OF_EARLY_DATA,
2961 &buf, &buf_len));
2962
2963 MBEDTLS_SSL_PROC_CHK(ssl_tls13_parse_end_of_early_data(
2964 ssl, buf, buf + buf_len));
2965
Jerry Yue9655122023-12-01 16:44:40 +08002966 MBEDTLS_SSL_DEBUG_MSG(
2967 1, ("Switch to handshake keys for inbound traffic"
2968 "( K_recv = handshake )"));
2969 mbedtls_ssl_set_inbound_transform(
2970 ssl, ssl->handshake->transform_handshake);
2971
Jerry Yud5c34962023-12-01 16:32:31 +08002972 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_add_hs_msg_to_checksum(
2973 ssl, MBEDTLS_SSL_HS_END_OF_EARLY_DATA,
2974 buf, buf_len));
Jerry Yue9655122023-12-01 16:44:40 +08002975
Jerry Yu3be85072023-12-04 09:58:54 +08002976 ssl_tls13_prepare_for_handshake_second_flight(ssl);
Jerry Yud5c34962023-12-01 16:32:31 +08002977
Jerry Yub55f9eb2023-12-05 10:27:17 +08002978 } else if (ret == SSL_GOT_EARLY_DATA) {
Jerry Yud9ca3542023-12-06 17:23:52 +08002979 ret = MBEDTLS_ERR_SSL_RECEIVED_EARLY_DATA;
2980 goto cleanup;
Jerry Yud5c34962023-12-01 16:32:31 +08002981 } else {
2982 MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
2983 ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
2984 goto cleanup;
2985 }
2986
Jerry Yud5c34962023-12-01 16:32:31 +08002987cleanup:
2988 MBEDTLS_SSL_DEBUG_MSG(2, ("<= ssl_tls13_process_end_of_early_data"));
Jerry Yu87b5ed42022-12-12 13:07:07 +08002989 return ret;
2990}
2991#endif /* MBEDTLS_SSL_EARLY_DATA */
2992
Jerry Yu69dd8d42022-04-16 12:51:26 +08002993/*
Jerry Yud6e253d2022-05-18 13:59:24 +08002994 * Handler for MBEDTLS_SSL_CLIENT_FINISHED
Jerry Yu69dd8d42022-04-16 12:51:26 +08002995 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002996MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002997static int ssl_tls13_process_client_finished(mbedtls_ssl_context *ssl)
Jerry Yu69dd8d42022-04-16 12:51:26 +08002998{
Jerry Yud6e253d2022-05-18 13:59:24 +08002999 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
3000
Gilles Peskine449bd832023-01-11 14:50:10 +01003001 ret = mbedtls_ssl_tls13_process_finished_message(ssl);
3002 if (ret != 0) {
3003 return ret;
Jerry Yue3d67cb2022-05-19 15:33:10 +08003004 }
3005
Gilles Peskine449bd832023-01-11 14:50:10 +01003006 ret = mbedtls_ssl_tls13_compute_resumption_master_secret(ssl);
3007 if (ret != 0) {
Xiaokang Qian9f1747b2023-03-30 02:50:04 +00003008 MBEDTLS_SSL_DEBUG_RET(
3009 1, "mbedtls_ssl_tls13_compute_resumption_master_secret", ret);
Gilles Peskine449bd832023-01-11 14:50:10 +01003010 }
3011
3012 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_HANDSHAKE_WRAPUP);
3013 return 0;
Jerry Yu69dd8d42022-04-16 12:51:26 +08003014}
3015
3016/*
Jerry Yud6e253d2022-05-18 13:59:24 +08003017 * Handler for MBEDTLS_SSL_HANDSHAKE_WRAPUP
Jerry Yu69dd8d42022-04-16 12:51:26 +08003018 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02003019MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01003020static int ssl_tls13_handshake_wrapup(mbedtls_ssl_context *ssl)
Jerry Yu69dd8d42022-04-16 12:51:26 +08003021{
Gilles Peskine449bd832023-01-11 14:50:10 +01003022 MBEDTLS_SSL_DEBUG_MSG(2, ("handshake: done"));
Jerry Yu03ed50b2022-04-16 17:13:30 +08003023
Gilles Peskine449bd832023-01-11 14:50:10 +01003024 mbedtls_ssl_tls13_handshake_wrapup(ssl);
Jerry Yue67bef42022-07-07 07:29:42 +00003025
Pengyu Lv3643fdb2023-01-12 16:46:28 +08003026#if defined(MBEDTLS_SSL_SESSION_TICKETS) && \
3027 defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED)
Pengyu Lva1aa31b2022-12-13 13:49:59 +08003028/* TODO: Remove the check of SOME_PSK_ENABLED since SESSION_TICKETS requires
3029 * SOME_PSK_ENABLED to be enabled. Here is just to make CI happy. It is
3030 * expected to be resolved with issue#6395.
3031 */
Pengyu Lvc7af2c42022-12-01 16:33:00 +08003032 /* Sent NewSessionTicket message only when client supports PSK */
Pengyu Lvb2cfafb2023-11-14 13:56:13 +08003033 if (mbedtls_ssl_tls13_is_some_psk_supported(ssl)) {
Xiaokang Qian9f1747b2023-03-30 02:50:04 +00003034 mbedtls_ssl_handshake_set_state(
3035 ssl, MBEDTLS_SSL_TLS1_3_NEW_SESSION_TICKET);
Pengyu Lvc7af2c42022-12-01 16:33:00 +08003036 } else
Jerry Yufca4d572022-07-21 10:37:48 +08003037#endif
Pengyu Lv3643fdb2023-01-12 16:46:28 +08003038 {
3039 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_HANDSHAKE_OVER);
3040 }
Gilles Peskine449bd832023-01-11 14:50:10 +01003041 return 0;
Jerry Yu69dd8d42022-04-16 12:51:26 +08003042}
3043
3044/*
Jerry Yua8d3c502022-10-30 14:51:23 +08003045 * Handler for MBEDTLS_SSL_TLS1_3_NEW_SESSION_TICKET
Jerry Yue67bef42022-07-07 07:29:42 +00003046 */
3047#define SSL_NEW_SESSION_TICKET_SKIP 0
3048#define SSL_NEW_SESSION_TICKET_WRITE 1
3049MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01003050static int ssl_tls13_write_new_session_ticket_coordinate(mbedtls_ssl_context *ssl)
Jerry Yue67bef42022-07-07 07:29:42 +00003051{
3052 /* Check whether the use of session tickets is enabled */
Gilles Peskine449bd832023-01-11 14:50:10 +01003053 if (ssl->conf->f_ticket_write == NULL) {
3054 MBEDTLS_SSL_DEBUG_MSG(2, ("NewSessionTicket: disabled,"
3055 " callback is not set"));
3056 return SSL_NEW_SESSION_TICKET_SKIP;
Jerry Yud0766ec2022-09-22 10:46:57 +08003057 }
Gilles Peskine449bd832023-01-11 14:50:10 +01003058 if (ssl->conf->new_session_tickets_count == 0) {
3059 MBEDTLS_SSL_DEBUG_MSG(2, ("NewSessionTicket: disabled,"
3060 " configured count is zero"));
3061 return SSL_NEW_SESSION_TICKET_SKIP;
Jerry Yud0766ec2022-09-22 10:46:57 +08003062 }
3063
Gilles Peskine449bd832023-01-11 14:50:10 +01003064 if (ssl->handshake->new_session_tickets_count == 0) {
3065 MBEDTLS_SSL_DEBUG_MSG(2, ("NewSessionTicket: all tickets have "
3066 "been sent."));
3067 return SSL_NEW_SESSION_TICKET_SKIP;
Jerry Yue67bef42022-07-07 07:29:42 +00003068 }
3069
Gilles Peskine449bd832023-01-11 14:50:10 +01003070 return SSL_NEW_SESSION_TICKET_WRITE;
Jerry Yue67bef42022-07-07 07:29:42 +00003071}
3072
3073#if defined(MBEDTLS_SSL_SESSION_TICKETS)
3074MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01003075static int ssl_tls13_prepare_new_session_ticket(mbedtls_ssl_context *ssl,
3076 unsigned char *ticket_nonce,
3077 size_t ticket_nonce_size)
Jerry Yue67bef42022-07-07 07:29:42 +00003078{
3079 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
3080 mbedtls_ssl_session *session = ssl->session;
3081 mbedtls_ssl_ciphersuite_t *ciphersuite_info;
3082 psa_algorithm_t psa_hash_alg;
3083 int hash_length;
3084
Gilles Peskine449bd832023-01-11 14:50:10 +01003085 MBEDTLS_SSL_DEBUG_MSG(2, ("=> prepare NewSessionTicket msg"));
Jerry Yue67bef42022-07-07 07:29:42 +00003086
Pengyu Lv9f926952022-11-17 15:22:33 +08003087 /* Set ticket_flags depends on the advertised psk key exchange mode */
Pengyu Lv94a42cc2023-12-06 10:04:17 +08003088 mbedtls_ssl_tls13_session_clear_ticket_flags(
Pengyu Lv9eacb442022-12-09 14:39:19 +08003089 session, MBEDTLS_SSL_TLS1_3_TICKET_FLAGS_MASK);
Pengyu Lve6487fe2022-12-06 09:30:29 +08003090#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED)
Pengyu Lv94a42cc2023-12-06 10:04:17 +08003091 mbedtls_ssl_tls13_session_set_ticket_flags(
Pengyu Lv9eacb442022-12-09 14:39:19 +08003092 session, ssl->handshake->tls13_kex_modes);
Pengyu Lve6487fe2022-12-06 09:30:29 +08003093#endif
Jerry Yu95648b02023-12-06 15:03:34 +08003094
3095#if defined(MBEDTLS_SSL_EARLY_DATA)
3096 if (ssl->conf->early_data_enabled == MBEDTLS_SSL_EARLY_DATA_ENABLED &&
3097 ssl->conf->max_early_data_size > 0) {
Pengyu Lv94a42cc2023-12-06 10:04:17 +08003098 mbedtls_ssl_tls13_session_set_ticket_flags(
Jerry Yu95648b02023-12-06 15:03:34 +08003099 session, MBEDTLS_SSL_TLS1_3_TICKET_ALLOW_EARLY_DATA);
3100 }
3101#endif /* MBEDTLS_SSL_EARLY_DATA */
3102
Pengyu Lv4938a562023-01-16 11:28:49 +08003103 MBEDTLS_SSL_PRINT_TICKET_FLAGS(4, session->ticket_flags);
Pengyu Lv9f926952022-11-17 15:22:33 +08003104
Jerry Yue67bef42022-07-07 07:29:42 +00003105 /* Generate ticket_age_add */
Gilles Peskine449bd832023-01-11 14:50:10 +01003106 if ((ret = ssl->conf->f_rng(ssl->conf->p_rng,
3107 (unsigned char *) &session->ticket_age_add,
3108 sizeof(session->ticket_age_add)) != 0)) {
3109 MBEDTLS_SSL_DEBUG_RET(1, "generate_ticket_age_add", ret);
3110 return ret;
Jerry Yue67bef42022-07-07 07:29:42 +00003111 }
Gilles Peskine449bd832023-01-11 14:50:10 +01003112 MBEDTLS_SSL_DEBUG_MSG(3, ("ticket_age_add: %u",
3113 (unsigned int) session->ticket_age_add));
Jerry Yue67bef42022-07-07 07:29:42 +00003114
3115 /* Generate ticket_nonce */
Gilles Peskine449bd832023-01-11 14:50:10 +01003116 ret = ssl->conf->f_rng(ssl->conf->p_rng, ticket_nonce, ticket_nonce_size);
3117 if (ret != 0) {
3118 MBEDTLS_SSL_DEBUG_RET(1, "generate_ticket_nonce", ret);
3119 return ret;
Jerry Yue67bef42022-07-07 07:29:42 +00003120 }
Gilles Peskine449bd832023-01-11 14:50:10 +01003121 MBEDTLS_SSL_DEBUG_BUF(3, "ticket_nonce:",
3122 ticket_nonce, ticket_nonce_size);
Jerry Yue67bef42022-07-07 07:29:42 +00003123
3124 ciphersuite_info =
Gilles Peskine449bd832023-01-11 14:50:10 +01003125 (mbedtls_ssl_ciphersuite_t *) ssl->handshake->ciphersuite_info;
Dave Rodgman2eab4622023-10-05 13:30:37 +01003126 psa_hash_alg = mbedtls_md_psa_alg_from_type((mbedtls_md_type_t) ciphersuite_info->mac);
Gilles Peskine449bd832023-01-11 14:50:10 +01003127 hash_length = PSA_HASH_LENGTH(psa_hash_alg);
3128 if (hash_length == -1 ||
3129 (size_t) hash_length > sizeof(session->resumption_key)) {
3130 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Jerry Yufca4d572022-07-21 10:37:48 +08003131 }
3132
Jerry Yue67bef42022-07-07 07:29:42 +00003133 /* In this code the psk key length equals the length of the hash */
3134 session->resumption_key_len = hash_length;
3135 session->ciphersuite = ciphersuite_info->id;
3136
3137 /* Compute resumption key
3138 *
3139 * HKDF-Expand-Label( resumption_master_secret,
3140 * "resumption", ticket_nonce, Hash.length )
3141 */
3142 ret = mbedtls_ssl_tls13_hkdf_expand_label(
Gilles Peskine449bd832023-01-11 14:50:10 +01003143 psa_hash_alg,
3144 session->app_secrets.resumption_master_secret,
3145 hash_length,
3146 MBEDTLS_SSL_TLS1_3_LBL_WITH_LEN(resumption),
3147 ticket_nonce,
3148 ticket_nonce_size,
3149 session->resumption_key,
3150 hash_length);
Jerry Yue67bef42022-07-07 07:29:42 +00003151
Gilles Peskine449bd832023-01-11 14:50:10 +01003152 if (ret != 0) {
3153 MBEDTLS_SSL_DEBUG_RET(2,
3154 "Creating the ticket-resumed PSK failed",
3155 ret);
3156 return ret;
Jerry Yue67bef42022-07-07 07:29:42 +00003157 }
Gilles Peskine449bd832023-01-11 14:50:10 +01003158 MBEDTLS_SSL_DEBUG_BUF(3, "Ticket-resumed PSK",
3159 session->resumption_key,
3160 session->resumption_key_len);
Jerry Yue67bef42022-07-07 07:29:42 +00003161
Gilles Peskine449bd832023-01-11 14:50:10 +01003162 MBEDTLS_SSL_DEBUG_BUF(3, "resumption_master_secret",
3163 session->app_secrets.resumption_master_secret,
3164 hash_length);
Jerry Yue67bef42022-07-07 07:29:42 +00003165
Gilles Peskine449bd832023-01-11 14:50:10 +01003166 return 0;
Jerry Yue67bef42022-07-07 07:29:42 +00003167}
3168
3169/* This function creates a NewSessionTicket message in the following format:
3170 *
3171 * struct {
3172 * uint32 ticket_lifetime;
3173 * uint32 ticket_age_add;
3174 * opaque ticket_nonce<0..255>;
3175 * opaque ticket<1..2^16-1>;
3176 * Extension extensions<0..2^16-2>;
3177 * } NewSessionTicket;
3178 *
3179 * The ticket inside the NewSessionTicket message is an encrypted container
3180 * carrying the necessary information so that the server is later able to
3181 * re-start the communication.
3182 *
3183 * The following fields are placed inside the ticket by the
3184 * f_ticket_write() function:
3185 *
Jerry Yu0069abc2023-11-23 21:07:28 +08003186 * - creation time (ticket_creation_time)
3187 * - flags (ticket_flags)
Jerry Yue67bef42022-07-07 07:29:42 +00003188 * - age add (ticket_age_add)
Jerry Yu0069abc2023-11-23 21:07:28 +08003189 * - key (resumption_key)
3190 * - key length (resumption_key_len)
Jerry Yue67bef42022-07-07 07:29:42 +00003191 * - ciphersuite (ciphersuite)
Jerry Yu0069abc2023-11-23 21:07:28 +08003192 * - max_early_data_size (max_early_data_size)
Jerry Yue67bef42022-07-07 07:29:42 +00003193 */
3194MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01003195static int ssl_tls13_write_new_session_ticket_body(mbedtls_ssl_context *ssl,
3196 unsigned char *buf,
3197 unsigned char *end,
3198 size_t *out_len,
3199 unsigned char *ticket_nonce,
3200 size_t ticket_nonce_size)
Jerry Yue67bef42022-07-07 07:29:42 +00003201{
3202 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
3203 unsigned char *p = buf;
3204 mbedtls_ssl_session *session = ssl->session;
3205 size_t ticket_len;
3206 uint32_t ticket_lifetime;
Jerry Yu01da35e2022-12-12 15:09:22 +08003207 unsigned char *p_extensions_len;
Jerry Yue67bef42022-07-07 07:29:42 +00003208
3209 *out_len = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01003210 MBEDTLS_SSL_DEBUG_MSG(2, ("=> write NewSessionTicket msg"));
Jerry Yue67bef42022-07-07 07:29:42 +00003211
3212 /*
3213 * ticket_lifetime 4 bytes
3214 * ticket_age_add 4 bytes
3215 * ticket_nonce 1 + ticket_nonce_size bytes
3216 * ticket >=2 bytes
3217 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003218 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 4 + 4 + 1 + ticket_nonce_size + 2);
Jerry Yue67bef42022-07-07 07:29:42 +00003219
3220 /* Generate ticket and ticket_lifetime */
Ronald Cron3c0072b2023-11-22 10:00:14 +01003221#if defined(MBEDTLS_HAVE_TIME)
3222 session->ticket_creation_time = mbedtls_ms_time();
3223#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01003224 ret = ssl->conf->f_ticket_write(ssl->conf->p_ticket,
3225 session,
3226 p + 9 + ticket_nonce_size + 2,
3227 end,
3228 &ticket_len,
3229 &ticket_lifetime);
3230 if (ret != 0) {
3231 MBEDTLS_SSL_DEBUG_RET(1, "write_ticket", ret);
3232 return ret;
Jerry Yue67bef42022-07-07 07:29:42 +00003233 }
3234 /* RFC 8446 4.6.1
3235 * ticket_lifetime: Indicates the lifetime in seconds as a 32-bit
3236 * unsigned integer in network byte order from the time of ticket
3237 * issuance. Servers MUST NOT use any value greater than
3238 * 604800 seconds (7 days). The value of zero indicates that the
3239 * ticket should be discarded immediately. Clients MUST NOT cache
3240 * tickets for longer than 7 days, regardless of the ticket_lifetime,
3241 * and MAY delete tickets earlier based on local policy. A server
3242 * MAY treat a ticket as valid for a shorter period of time than what
3243 * is stated in the ticket_lifetime.
3244 */
Jerry Yu8e0174a2023-11-10 13:58:16 +08003245 if (ticket_lifetime > MBEDTLS_SSL_TLS1_3_MAX_ALLOWED_TICKET_LIFETIME) {
3246 ticket_lifetime = MBEDTLS_SSL_TLS1_3_MAX_ALLOWED_TICKET_LIFETIME;
Gilles Peskine449bd832023-01-11 14:50:10 +01003247 }
3248 MBEDTLS_PUT_UINT32_BE(ticket_lifetime, p, 0);
3249 MBEDTLS_SSL_DEBUG_MSG(3, ("ticket_lifetime: %u",
3250 (unsigned int) ticket_lifetime));
Jerry Yue67bef42022-07-07 07:29:42 +00003251
3252 /* Write ticket_age_add */
Gilles Peskine449bd832023-01-11 14:50:10 +01003253 MBEDTLS_PUT_UINT32_BE(session->ticket_age_add, p, 4);
3254 MBEDTLS_SSL_DEBUG_MSG(3, ("ticket_age_add: %u",
3255 (unsigned int) session->ticket_age_add));
Jerry Yue67bef42022-07-07 07:29:42 +00003256
3257 /* Write ticket_nonce */
Gilles Peskine449bd832023-01-11 14:50:10 +01003258 p[8] = (unsigned char) ticket_nonce_size;
3259 if (ticket_nonce_size > 0) {
3260 memcpy(p + 9, ticket_nonce, ticket_nonce_size);
Jerry Yue67bef42022-07-07 07:29:42 +00003261 }
3262 p += 9 + ticket_nonce_size;
3263
3264 /* Write ticket */
Gilles Peskine449bd832023-01-11 14:50:10 +01003265 MBEDTLS_PUT_UINT16_BE(ticket_len, p, 0);
Jerry Yue67bef42022-07-07 07:29:42 +00003266 p += 2;
Gilles Peskine449bd832023-01-11 14:50:10 +01003267 MBEDTLS_SSL_DEBUG_BUF(4, "ticket", p, ticket_len);
Jerry Yue67bef42022-07-07 07:29:42 +00003268 p += ticket_len;
3269
3270 /* Ticket Extensions
3271 *
Jerry Yu01da35e2022-12-12 15:09:22 +08003272 * Extension extensions<0..2^16-2>;
Jerry Yue67bef42022-07-07 07:29:42 +00003273 */
Jerry Yuedab6372022-10-31 14:37:31 +08003274 ssl->handshake->sent_extensions = MBEDTLS_SSL_EXT_MASK_NONE;
3275
Gilles Peskine449bd832023-01-11 14:50:10 +01003276 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 2);
Jerry Yu01da35e2022-12-12 15:09:22 +08003277 p_extensions_len = p;
Jerry Yue67bef42022-07-07 07:29:42 +00003278 p += 2;
3279
Jerry Yu01da35e2022-12-12 15:09:22 +08003280#if defined(MBEDTLS_SSL_EARLY_DATA)
Pengyu Lv94a42cc2023-12-06 10:04:17 +08003281 if (mbedtls_ssl_tls13_session_ticket_allow_early_data(session)) {
Jerry Yu95648b02023-12-06 15:03:34 +08003282 size_t output_len;
3283
Jerry Yuf135bac2023-11-23 18:10:51 +08003284 if ((ret = mbedtls_ssl_tls13_write_early_data_ext(
Jerry Yuc59c5862023-12-05 10:40:49 +08003285 ssl, 1, p, end, &output_len)) != 0) {
Jerry Yuf135bac2023-11-23 18:10:51 +08003286 MBEDTLS_SSL_DEBUG_RET(
3287 1, "mbedtls_ssl_tls13_write_early_data_ext", ret);
3288 return ret;
3289 }
3290 p += output_len;
Jerry Yu9e7f9bc2023-11-27 16:52:07 +08003291 } else {
3292 MBEDTLS_SSL_DEBUG_MSG(
3293 4, ("early_data not allowed, "
3294 "skip early_data extension in NewSessionTicket"));
Jerry Yu01da35e2022-12-12 15:09:22 +08003295 }
Jerry Yuf135bac2023-11-23 18:10:51 +08003296
Jerry Yu01da35e2022-12-12 15:09:22 +08003297#endif /* MBEDTLS_SSL_EARLY_DATA */
3298
3299 MBEDTLS_PUT_UINT16_BE(p - p_extensions_len - 2, p_extensions_len, 0);
3300
Jerry Yue67bef42022-07-07 07:29:42 +00003301 *out_len = p - buf;
Gilles Peskine449bd832023-01-11 14:50:10 +01003302 MBEDTLS_SSL_DEBUG_BUF(4, "ticket", buf, *out_len);
3303 MBEDTLS_SSL_DEBUG_MSG(2, ("<= write new session ticket"));
Jerry Yue67bef42022-07-07 07:29:42 +00003304
Jerry Yu7de2ff02022-11-08 21:43:46 +08003305 MBEDTLS_SSL_PRINT_EXTS(
Gilles Peskine449bd832023-01-11 14:50:10 +01003306 3, MBEDTLS_SSL_HS_NEW_SESSION_TICKET, ssl->handshake->sent_extensions);
Jerry Yu4b8f2f72022-10-31 13:31:22 +08003307
Gilles Peskine449bd832023-01-11 14:50:10 +01003308 return 0;
Jerry Yue67bef42022-07-07 07:29:42 +00003309}
3310
3311/*
Jerry Yua8d3c502022-10-30 14:51:23 +08003312 * Handler for MBEDTLS_SSL_TLS1_3_NEW_SESSION_TICKET
Jerry Yue67bef42022-07-07 07:29:42 +00003313 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003314static int ssl_tls13_write_new_session_ticket(mbedtls_ssl_context *ssl)
Jerry Yue67bef42022-07-07 07:29:42 +00003315{
3316 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
3317
Gilles Peskine449bd832023-01-11 14:50:10 +01003318 MBEDTLS_SSL_PROC_CHK_NEG(ssl_tls13_write_new_session_ticket_coordinate(ssl));
Jerry Yue67bef42022-07-07 07:29:42 +00003319
Gilles Peskine449bd832023-01-11 14:50:10 +01003320 if (ret == SSL_NEW_SESSION_TICKET_WRITE) {
Jerry Yue67bef42022-07-07 07:29:42 +00003321 unsigned char ticket_nonce[MBEDTLS_SSL_TLS1_3_TICKET_NONCE_LENGTH];
3322 unsigned char *buf;
3323 size_t buf_len, msg_len;
3324
Gilles Peskine449bd832023-01-11 14:50:10 +01003325 MBEDTLS_SSL_PROC_CHK(ssl_tls13_prepare_new_session_ticket(
3326 ssl, ticket_nonce, sizeof(ticket_nonce)));
Jerry Yue67bef42022-07-07 07:29:42 +00003327
Xiaokang Qian9f1747b2023-03-30 02:50:04 +00003328 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_start_handshake_msg(
3329 ssl, MBEDTLS_SSL_HS_NEW_SESSION_TICKET,
3330 &buf, &buf_len));
Jerry Yue67bef42022-07-07 07:29:42 +00003331
Gilles Peskine449bd832023-01-11 14:50:10 +01003332 MBEDTLS_SSL_PROC_CHK(ssl_tls13_write_new_session_ticket_body(
3333 ssl, buf, buf + buf_len, &msg_len,
3334 ticket_nonce, sizeof(ticket_nonce)));
Jerry Yue67bef42022-07-07 07:29:42 +00003335
Gilles Peskine449bd832023-01-11 14:50:10 +01003336 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_finish_handshake_msg(
3337 ssl, buf_len, msg_len));
Jerry Yufca4d572022-07-21 10:37:48 +08003338
Jerry Yu359e65f2022-09-22 23:47:43 +08003339 /* Limit session tickets count to one when resumption connection.
3340 *
3341 * See document of mbedtls_ssl_conf_new_session_tickets.
3342 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003343 if (ssl->handshake->resume == 1) {
Jerry Yu359e65f2022-09-22 23:47:43 +08003344 ssl->handshake->new_session_tickets_count = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01003345 } else {
Jerry Yu359e65f2022-09-22 23:47:43 +08003346 ssl->handshake->new_session_tickets_count--;
Gilles Peskine449bd832023-01-11 14:50:10 +01003347 }
Jerry Yub7e3fa72022-09-22 11:07:18 +08003348
Jerry Yua8d3c502022-10-30 14:51:23 +08003349 mbedtls_ssl_handshake_set_state(
Gilles Peskine449bd832023-01-11 14:50:10 +01003350 ssl, MBEDTLS_SSL_TLS1_3_NEW_SESSION_TICKET_FLUSH);
3351 } else {
3352 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_HANDSHAKE_OVER);
Jerry Yue67bef42022-07-07 07:29:42 +00003353 }
3354
Jerry Yue67bef42022-07-07 07:29:42 +00003355cleanup:
3356
Gilles Peskine449bd832023-01-11 14:50:10 +01003357 return ret;
Jerry Yue67bef42022-07-07 07:29:42 +00003358}
3359#endif /* MBEDTLS_SSL_SESSION_TICKETS */
3360
3361/*
XiaokangQiane8ff3502022-04-22 02:34:40 +00003362 * TLS 1.3 State Machine -- server side
XiaokangQian7807f9f2022-02-15 10:04:37 +00003363 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003364int mbedtls_ssl_tls13_handshake_server_step(mbedtls_ssl_context *ssl)
Jerry Yub9930e72021-08-06 17:11:51 +08003365{
XiaokangQian08037552022-04-20 07:16:41 +00003366 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
XiaokangQian7807f9f2022-02-15 10:04:37 +00003367
Gilles Peskine449bd832023-01-11 14:50:10 +01003368 if (ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER || ssl->handshake == NULL) {
3369 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
3370 }
XiaokangQian7807f9f2022-02-15 10:04:37 +00003371
Gilles Peskine449bd832023-01-11 14:50:10 +01003372 MBEDTLS_SSL_DEBUG_MSG(2, ("tls13 server state: %s(%d)",
Dave Rodgman2eab4622023-10-05 13:30:37 +01003373 mbedtls_ssl_states_str((mbedtls_ssl_states) ssl->state),
Gilles Peskine449bd832023-01-11 14:50:10 +01003374 ssl->state));
Jerry Yu6e81b272021-09-27 11:16:17 +08003375
Gilles Peskine449bd832023-01-11 14:50:10 +01003376 switch (ssl->state) {
XiaokangQian7807f9f2022-02-15 10:04:37 +00003377 /* start state */
3378 case MBEDTLS_SSL_HELLO_REQUEST:
Gilles Peskine449bd832023-01-11 14:50:10 +01003379 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_CLIENT_HELLO);
XiaokangQian08037552022-04-20 07:16:41 +00003380 ret = 0;
XiaokangQian7807f9f2022-02-15 10:04:37 +00003381 break;
3382
XiaokangQian7807f9f2022-02-15 10:04:37 +00003383 case MBEDTLS_SSL_CLIENT_HELLO:
Gilles Peskine449bd832023-01-11 14:50:10 +01003384 ret = ssl_tls13_process_client_hello(ssl);
3385 if (ret != 0) {
3386 MBEDTLS_SSL_DEBUG_RET(1, "ssl_tls13_process_client_hello", ret);
3387 }
Jerry Yuf41553b2022-05-09 22:20:30 +08003388 break;
XiaokangQian7807f9f2022-02-15 10:04:37 +00003389
Jerry Yuf41553b2022-05-09 22:20:30 +08003390 case MBEDTLS_SSL_HELLO_RETRY_REQUEST:
Gilles Peskine449bd832023-01-11 14:50:10 +01003391 ret = ssl_tls13_write_hello_retry_request(ssl);
3392 if (ret != 0) {
3393 MBEDTLS_SSL_DEBUG_RET(1, "ssl_tls13_write_hello_retry_request", ret);
3394 return ret;
Jerry Yuf41553b2022-05-09 22:20:30 +08003395 }
XiaokangQian7807f9f2022-02-15 10:04:37 +00003396 break;
3397
Jerry Yu5b64ae92022-03-30 17:15:02 +08003398 case MBEDTLS_SSL_SERVER_HELLO:
Gilles Peskine449bd832023-01-11 14:50:10 +01003399 ret = ssl_tls13_write_server_hello(ssl);
Jerry Yu5b64ae92022-03-30 17:15:02 +08003400 break;
3401
Xiaofei Baicba64af2022-02-15 10:00:56 +00003402 case MBEDTLS_SSL_ENCRYPTED_EXTENSIONS:
Gilles Peskine449bd832023-01-11 14:50:10 +01003403 ret = ssl_tls13_write_encrypted_extensions(ssl);
3404 if (ret != 0) {
3405 MBEDTLS_SSL_DEBUG_RET(1, "ssl_tls13_write_encrypted_extensions", ret);
3406 return ret;
Xiaofei Baicba64af2022-02-15 10:00:56 +00003407 }
Jerry Yu48330562022-05-06 21:35:44 +08003408 break;
Jerry Yu6a2cd9e2022-05-05 11:14:19 +08003409
Ronald Cron928cbd32022-10-04 16:14:26 +02003410#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED)
Xiaofei Bai9ca09d42022-02-14 12:57:18 +00003411 case MBEDTLS_SSL_CERTIFICATE_REQUEST:
Gilles Peskine449bd832023-01-11 14:50:10 +01003412 ret = ssl_tls13_write_certificate_request(ssl);
Xiaofei Bai9ca09d42022-02-14 12:57:18 +00003413 break;
Xiaofei Bai9ca09d42022-02-14 12:57:18 +00003414
Jerry Yu83da34e2022-04-16 13:59:52 +08003415 case MBEDTLS_SSL_SERVER_CERTIFICATE:
Gilles Peskine449bd832023-01-11 14:50:10 +01003416 ret = ssl_tls13_write_server_certificate(ssl);
Jerry Yu83da34e2022-04-16 13:59:52 +08003417 break;
3418
3419 case MBEDTLS_SSL_CERTIFICATE_VERIFY:
Gilles Peskine449bd832023-01-11 14:50:10 +01003420 ret = ssl_tls13_write_certificate_verify(ssl);
Jerry Yu83da34e2022-04-16 13:59:52 +08003421 break;
Ronald Cron928cbd32022-10-04 16:14:26 +02003422#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED */
Jerry Yu83da34e2022-04-16 13:59:52 +08003423
Gilles Peskine449bd832023-01-11 14:50:10 +01003424 /*
3425 * Injection of dummy-CCS's for middlebox compatibility
3426 */
Gabor Mezei7b39bf12022-05-24 16:04:14 +02003427#if defined(MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE)
Gabor Mezeif7044ea2022-06-28 16:01:49 +02003428 case MBEDTLS_SSL_SERVER_CCS_AFTER_HELLO_RETRY_REQUEST:
Gilles Peskine449bd832023-01-11 14:50:10 +01003429 ret = mbedtls_ssl_tls13_write_change_cipher_spec(ssl);
3430 if (ret == 0) {
3431 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_CLIENT_HELLO);
3432 }
Gabor Mezei7b39bf12022-05-24 16:04:14 +02003433 break;
3434
3435 case MBEDTLS_SSL_SERVER_CCS_AFTER_SERVER_HELLO:
Ronald Crone273f722024-02-13 18:22:26 +01003436 ret = mbedtls_ssl_tls13_write_change_cipher_spec(ssl);
3437 if (ret != 0) {
3438 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01003439 }
Ronald Cronfe59ff72024-01-24 14:31:50 +01003440 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_ENCRYPTED_EXTENSIONS);
Gabor Mezei7b39bf12022-05-24 16:04:14 +02003441 break;
3442#endif /* MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE */
3443
Jerry Yu69dd8d42022-04-16 12:51:26 +08003444 case MBEDTLS_SSL_SERVER_FINISHED:
Gilles Peskine449bd832023-01-11 14:50:10 +01003445 ret = ssl_tls13_write_server_finished(ssl);
Jerry Yu69dd8d42022-04-16 12:51:26 +08003446 break;
3447
Jerry Yu87b5ed42022-12-12 13:07:07 +08003448#if defined(MBEDTLS_SSL_EARLY_DATA)
3449 case MBEDTLS_SSL_END_OF_EARLY_DATA:
Jerry Yu59d420f2023-12-01 16:30:34 +08003450 ret = ssl_tls13_process_end_of_early_data(ssl);
Jerry Yu87b5ed42022-12-12 13:07:07 +08003451 break;
3452#endif /* MBEDTLS_SSL_EARLY_DATA */
3453
Jerry Yu69dd8d42022-04-16 12:51:26 +08003454 case MBEDTLS_SSL_CLIENT_FINISHED:
Gilles Peskine449bd832023-01-11 14:50:10 +01003455 ret = ssl_tls13_process_client_finished(ssl);
Jerry Yu69dd8d42022-04-16 12:51:26 +08003456 break;
3457
Jerry Yu69dd8d42022-04-16 12:51:26 +08003458 case MBEDTLS_SSL_HANDSHAKE_WRAPUP:
Gilles Peskine449bd832023-01-11 14:50:10 +01003459 ret = ssl_tls13_handshake_wrapup(ssl);
Jerry Yu69dd8d42022-04-16 12:51:26 +08003460 break;
3461
Ronald Cron766c0cd2022-10-18 12:17:11 +02003462#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED)
XiaokangQian6b916b12022-04-25 07:29:34 +00003463 case MBEDTLS_SSL_CLIENT_CERTIFICATE:
Gilles Peskine449bd832023-01-11 14:50:10 +01003464 ret = mbedtls_ssl_tls13_process_certificate(ssl);
3465 if (ret == 0) {
3466 if (ssl->session_negotiate->peer_cert != NULL) {
Ronald Cron209cae92022-06-07 10:30:19 +02003467 mbedtls_ssl_handshake_set_state(
Gilles Peskine449bd832023-01-11 14:50:10 +01003468 ssl, MBEDTLS_SSL_CLIENT_CERTIFICATE_VERIFY);
3469 } else {
3470 MBEDTLS_SSL_DEBUG_MSG(2, ("skip parse certificate verify"));
Ronald Cron209cae92022-06-07 10:30:19 +02003471 mbedtls_ssl_handshake_set_state(
Gilles Peskine449bd832023-01-11 14:50:10 +01003472 ssl, MBEDTLS_SSL_CLIENT_FINISHED);
Ronald Cron19385882022-06-15 16:26:13 +02003473 }
XiaokangQian6b916b12022-04-25 07:29:34 +00003474 }
3475 break;
3476
3477 case MBEDTLS_SSL_CLIENT_CERTIFICATE_VERIFY:
Gilles Peskine449bd832023-01-11 14:50:10 +01003478 ret = mbedtls_ssl_tls13_process_certificate_verify(ssl);
3479 if (ret == 0) {
XiaokangQian6b916b12022-04-25 07:29:34 +00003480 mbedtls_ssl_handshake_set_state(
Gilles Peskine449bd832023-01-11 14:50:10 +01003481 ssl, MBEDTLS_SSL_CLIENT_FINISHED);
XiaokangQian6b916b12022-04-25 07:29:34 +00003482 }
3483 break;
Ronald Cron766c0cd2022-10-18 12:17:11 +02003484#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED */
XiaokangQian6b916b12022-04-25 07:29:34 +00003485
Jerry Yue67bef42022-07-07 07:29:42 +00003486#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Jerry Yua8d3c502022-10-30 14:51:23 +08003487 case MBEDTLS_SSL_TLS1_3_NEW_SESSION_TICKET:
Gilles Peskine449bd832023-01-11 14:50:10 +01003488 ret = ssl_tls13_write_new_session_ticket(ssl);
3489 if (ret != 0) {
3490 MBEDTLS_SSL_DEBUG_RET(1,
3491 "ssl_tls13_write_new_session_ticket ",
3492 ret);
Jerry Yue67bef42022-07-07 07:29:42 +00003493 }
3494 break;
Jerry Yua8d3c502022-10-30 14:51:23 +08003495 case MBEDTLS_SSL_TLS1_3_NEW_SESSION_TICKET_FLUSH:
Jerry Yue67bef42022-07-07 07:29:42 +00003496 /* This state is necessary to do the flush of the New Session
Jerry Yua8d3c502022-10-30 14:51:23 +08003497 * Ticket message written in MBEDTLS_SSL_TLS1_3_NEW_SESSION_TICKET
Jerry Yue67bef42022-07-07 07:29:42 +00003498 * as part of ssl_prepare_handshake_step.
3499 */
3500 ret = 0;
Jerry Yud4e75002022-08-09 13:33:50 +08003501
Gilles Peskine449bd832023-01-11 14:50:10 +01003502 if (ssl->handshake->new_session_tickets_count == 0) {
3503 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_HANDSHAKE_OVER);
3504 } else {
Xiaokang Qian9f1747b2023-03-30 02:50:04 +00003505 mbedtls_ssl_handshake_set_state(
3506 ssl, MBEDTLS_SSL_TLS1_3_NEW_SESSION_TICKET);
Gilles Peskine449bd832023-01-11 14:50:10 +01003507 }
Jerry Yue67bef42022-07-07 07:29:42 +00003508 break;
3509
3510#endif /* MBEDTLS_SSL_SESSION_TICKETS */
3511
XiaokangQian7807f9f2022-02-15 10:04:37 +00003512 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01003513 MBEDTLS_SSL_DEBUG_MSG(1, ("invalid state %d", ssl->state));
3514 return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
XiaokangQian7807f9f2022-02-15 10:04:37 +00003515 }
3516
Gilles Peskine449bd832023-01-11 14:50:10 +01003517 return ret;
Jerry Yub9930e72021-08-06 17:11:51 +08003518}
Jerry Yu3cc4c2a2021-08-06 16:29:08 +08003519
Jerry Yufb4b6472022-01-27 15:03:26 +08003520#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_PROTO_TLS1_3 */