blob: d6dd4810d9806446cbab56a40bbb76f2b8a4e8c7 [file] [log] [blame]
Jerry Yu3cc4c2a2021-08-06 16:29:08 +08001/*
Jerry Yub9930e72021-08-06 17:11:51 +08002 * TLS 1.3 server-side functions
Jerry Yu3cc4c2a2021-08-06 16:29:08 +08003 *
4 * Copyright The Mbed TLS Contributors
Dave Rodgman16799db2023-11-02 19:47:20 +00005 * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
Gilles Peskine449bd832023-01-11 14:50:10 +01006 */
Jerry Yu3cc4c2a2021-08-06 16:29:08 +08007
8#include "common.h"
9
Jerry Yufb4b6472022-01-27 15:03:26 +080010#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_PROTO_TLS1_3)
Jerry Yu3cc4c2a2021-08-06 16:29:08 +080011
Valerio Settib4f50762024-01-17 10:24:52 +010012#include "debug_internal.h"
Xiaofei Baicba64af2022-02-15 10:00:56 +000013#include "mbedtls/error.h"
14#include "mbedtls/platform.h"
Jerry Yu1c105562022-07-10 06:32:38 +000015#include "mbedtls/constant_time.h"
Manuel Pégourié-Gonnardd55d66f2023-06-20 10:14:58 +020016#include "mbedtls/oid.h"
Valerio Setti384fbde2024-01-02 13:26:40 +010017#include "mbedtls/psa_util.h"
Jerry Yu3bf2c642022-03-30 22:02:12 +080018
Xiaofei Bai9ca09d42022-02-14 12:57:18 +000019#include "ssl_misc.h"
20#include "ssl_tls13_keys.h"
21#include "ssl_debug_helpers.h"
22
Jerry Yuf35ba382022-08-23 17:58:26 +080023
Jerry Yuc5a23a02022-08-25 10:51:44 +080024static const mbedtls_ssl_ciphersuite_t *ssl_tls13_validate_peer_ciphersuite(
Gilles Peskine449bd832023-01-11 14:50:10 +010025 mbedtls_ssl_context *ssl,
26 unsigned int cipher_suite)
Jerry Yuf35ba382022-08-23 17:58:26 +080027{
28 const mbedtls_ssl_ciphersuite_t *ciphersuite_info;
Gilles Peskine449bd832023-01-11 14:50:10 +010029 if (!mbedtls_ssl_tls13_cipher_suite_is_offered(ssl, cipher_suite)) {
30 return NULL;
Jerry Yuf35ba382022-08-23 17:58:26 +080031 }
Gilles Peskine449bd832023-01-11 14:50:10 +010032
33 ciphersuite_info = mbedtls_ssl_ciphersuite_from_id(cipher_suite);
34 if ((mbedtls_ssl_validate_ciphersuite(ssl, ciphersuite_info,
35 ssl->tls_version,
36 ssl->tls_version) != 0)) {
37 return NULL;
38 }
39 return ciphersuite_info;
Jerry Yuf35ba382022-08-23 17:58:26 +080040}
41
Ronald Cron89089cc2024-02-14 18:18:08 +010042static void ssl_tls13_select_ciphersuite(
43 mbedtls_ssl_context *ssl,
44 const unsigned char *cipher_suites,
45 const unsigned char *cipher_suites_end,
46 int psk_ciphersuite_id,
47 psa_algorithm_t psk_hash_alg,
48 const mbedtls_ssl_ciphersuite_t **selected_ciphersuite_info)
49{
50 *selected_ciphersuite_info = NULL;
51
52 /*
53 * This function assumes that the length of the list of ciphersuites is
54 * even and it should have been checked it is so in the main ClientHello
55 * parsing function. Double check here.
56 */
57 if ((cipher_suites_end - cipher_suites) & 1) {
58 return;
59 }
60
61 for (const unsigned char *p = cipher_suites;
62 p < cipher_suites_end; p += 2) {
63 /*
64 * "cipher_suites_end - p is even" is an invariant of the loop. As
65 * cipher_suites_end - p > 0, we have cipher_suites_end - p >= 2 and it
66 * is thus safe to read two bytes.
67 */
68 uint16_t id = MBEDTLS_GET_UINT16_BE(p, 0);
69
70 const mbedtls_ssl_ciphersuite_t *info =
71 ssl_tls13_validate_peer_ciphersuite(ssl, id);
72 if (info == NULL) {
73 continue;
74 }
75
76 /*
77 * If a valid PSK ciphersuite identifier has been passed in, we seek
78 * for an exact match.
79 */
80 if (psk_ciphersuite_id != 0) {
81 if (id != psk_ciphersuite_id) {
82 continue;
83 }
84 } else if (psk_hash_alg != PSA_ALG_NONE) {
85 if (mbedtls_md_psa_alg_from_type((mbedtls_md_type_t) info->mac) !=
86 psk_hash_alg) {
87 continue;
88 }
89 }
90
91 *selected_ciphersuite_info = info;
92 return;
93 }
94
95 MBEDTLS_SSL_DEBUG_MSG(2, ("No matched ciphersuite"));
96}
97
Ronald Cron41a443a2022-10-04 16:38:25 +020098#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED)
Jerry Yue19e3b92022-07-08 12:04:51 +000099/* From RFC 8446:
100 *
101 * enum { psk_ke(0), psk_dhe_ke(1), (255) } PskKeyExchangeMode;
102 * struct {
103 * PskKeyExchangeMode ke_modes<1..255>;
104 * } PskKeyExchangeModes;
105 */
Jerry Yu6e74a7e2022-07-20 20:49:32 +0800106MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100107static int ssl_tls13_parse_key_exchange_modes_ext(mbedtls_ssl_context *ssl,
108 const unsigned char *buf,
109 const unsigned char *end)
Jerry Yue19e3b92022-07-08 12:04:51 +0000110{
Jerry Yu299e31f2022-07-13 23:06:36 +0800111 const unsigned char *p = buf;
Jerry Yue19e3b92022-07-08 12:04:51 +0000112 size_t ke_modes_len;
113 int ke_modes = 0;
114
Jerry Yu854dd9e2022-07-15 14:28:27 +0800115 /* Read ke_modes length (1 Byte) */
Gilles Peskine449bd832023-01-11 14:50:10 +0100116 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, 1);
Jerry Yu299e31f2022-07-13 23:06:36 +0800117 ke_modes_len = *p++;
Jerry Yue19e3b92022-07-08 12:04:51 +0000118 /* Currently, there are only two PSK modes, so even without looking
119 * at the content, something's wrong if the list has more than 2 items. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100120 if (ke_modes_len > 2) {
121 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER,
122 MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER);
123 return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
Jerry Yu299e31f2022-07-13 23:06:36 +0800124 }
Jerry Yue19e3b92022-07-08 12:04:51 +0000125
Gilles Peskine449bd832023-01-11 14:50:10 +0100126 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, ke_modes_len);
Jerry Yue19e3b92022-07-08 12:04:51 +0000127
Gilles Peskine449bd832023-01-11 14:50:10 +0100128 while (ke_modes_len-- != 0) {
129 switch (*p++) {
130 case MBEDTLS_SSL_TLS1_3_PSK_MODE_PURE:
131 ke_modes |= MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK;
132 MBEDTLS_SSL_DEBUG_MSG(3, ("Found PSK KEX MODE"));
133 break;
134 case MBEDTLS_SSL_TLS1_3_PSK_MODE_ECDHE:
135 ke_modes |= MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK_EPHEMERAL;
136 MBEDTLS_SSL_DEBUG_MSG(3, ("Found PSK_EPHEMERAL KEX MODE"));
137 break;
138 default:
139 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER,
140 MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER);
141 return MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER;
Jerry Yue19e3b92022-07-08 12:04:51 +0000142 }
143 }
144
145 ssl->handshake->tls13_kex_modes = ke_modes;
Gilles Peskine449bd832023-01-11 14:50:10 +0100146 return 0;
Jerry Yue19e3b92022-07-08 12:04:51 +0000147}
Jerry Yu1c105562022-07-10 06:32:38 +0000148
Ronald Cronfbae94a2023-12-05 18:15:14 +0100149#define SSL_TLS1_3_PSK_IDENTITY_DOES_NOT_MATCH 2
150#define SSL_TLS1_3_PSK_IDENTITY_MATCH_BUT_PSK_NOT_USABLE 1
151#define SSL_TLS1_3_PSK_IDENTITY_MATCH 0
Jerry Yu95699e72022-08-21 19:22:23 +0800152
153#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Pengyu Lv29daf4a2023-10-30 17:13:30 +0800154MBEDTLS_CHECK_RETURN_CRITICAL
Pengyu Lvbc4aab72023-12-01 15:37:24 +0800155static int ssl_tls13_key_exchange_is_psk_available(mbedtls_ssl_context *ssl);
Pengyu Lv29daf4a2023-10-30 17:13:30 +0800156MBEDTLS_CHECK_RETURN_CRITICAL
Pengyu Lvbc4aab72023-12-01 15:37:24 +0800157static int ssl_tls13_key_exchange_is_psk_ephemeral_available(mbedtls_ssl_context *ssl);
Jerry Yu95699e72022-08-21 19:22:23 +0800158
159MBEDTLS_CHECK_RETURN_CRITICAL
160static int ssl_tls13_offered_psks_check_identity_match_ticket(
Gilles Peskine449bd832023-01-11 14:50:10 +0100161 mbedtls_ssl_context *ssl,
162 const unsigned char *identity,
163 size_t identity_len,
164 uint32_t obfuscated_ticket_age,
165 mbedtls_ssl_session *session)
Jerry Yu95699e72022-08-21 19:22:23 +0800166{
Jerry Yufd310eb2022-09-06 09:16:35 +0800167 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yu95699e72022-08-21 19:22:23 +0800168 unsigned char *ticket_buffer;
Jerry Yu8d4bbba2022-09-13 14:15:48 +0800169#if defined(MBEDTLS_HAVE_TIME)
Jerry Yucebffc32022-12-15 18:00:05 +0800170 mbedtls_ms_time_t now;
171 mbedtls_ms_time_t server_age;
Jerry Yu713ce1f2023-11-17 15:32:12 +0800172 uint32_t client_age;
Jerry Yucebffc32022-12-15 18:00:05 +0800173 mbedtls_ms_time_t age_diff;
Jerry Yu8d4bbba2022-09-13 14:15:48 +0800174#endif
Jerry Yu95699e72022-08-21 19:22:23 +0800175
176 ((void) obfuscated_ticket_age);
177
Gilles Peskine449bd832023-01-11 14:50:10 +0100178 MBEDTLS_SSL_DEBUG_MSG(2, ("=> check_identity_match_ticket"));
Jerry Yu95699e72022-08-21 19:22:23 +0800179
Jerry Yufd310eb2022-09-06 09:16:35 +0800180 /* Ticket parser is not configured, Skip */
Gilles Peskine449bd832023-01-11 14:50:10 +0100181 if (ssl->conf->f_ticket_parse == NULL || identity_len == 0) {
Ronald Cronfbae94a2023-12-05 18:15:14 +0100182 return SSL_TLS1_3_PSK_IDENTITY_DOES_NOT_MATCH;
Gilles Peskine449bd832023-01-11 14:50:10 +0100183 }
Jerry Yu95699e72022-08-21 19:22:23 +0800184
Jerry Yu4746b102022-09-13 11:11:48 +0800185 /* We create a copy of the encrypted ticket since the ticket parsing
186 * function is allowed to use its input buffer as an output buffer
187 * (in-place decryption). We do, however, need the original buffer for
188 * computing the PSK binder value.
Jerry Yu95699e72022-08-21 19:22:23 +0800189 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100190 ticket_buffer = mbedtls_calloc(1, identity_len);
191 if (ticket_buffer == NULL) {
192 MBEDTLS_SSL_DEBUG_MSG(1, ("buffer too small"));
193 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
Jerry Yu95699e72022-08-21 19:22:23 +0800194 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100195 memcpy(ticket_buffer, identity, identity_len);
Jerry Yu95699e72022-08-21 19:22:23 +0800196
Ronald Cronfbae94a2023-12-05 18:15:14 +0100197 ret = ssl->conf->f_ticket_parse(ssl->conf->p_ticket,
198 session,
199 ticket_buffer, identity_len);
200 if (ret == 0) {
201 ret = SSL_TLS1_3_PSK_IDENTITY_MATCH;
202 } else {
203 if (ret == MBEDTLS_ERR_SSL_SESSION_TICKET_EXPIRED) {
Gilles Peskine449bd832023-01-11 14:50:10 +0100204 MBEDTLS_SSL_DEBUG_MSG(3, ("ticket is expired"));
Ronald Cronfbae94a2023-12-05 18:15:14 +0100205 ret = SSL_TLS1_3_PSK_IDENTITY_MATCH_BUT_PSK_NOT_USABLE;
Gilles Peskine449bd832023-01-11 14:50:10 +0100206 } else {
Ronald Cronfbae94a2023-12-05 18:15:14 +0100207 if (ret == MBEDTLS_ERR_SSL_INVALID_MAC) {
208 MBEDTLS_SSL_DEBUG_MSG(3, ("ticket is not authentic"));
209 } else {
210 MBEDTLS_SSL_DEBUG_RET(1, "ticket_parse", ret);
211 }
212 ret = SSL_TLS1_3_PSK_IDENTITY_DOES_NOT_MATCH;
Gilles Peskine449bd832023-01-11 14:50:10 +0100213 }
Jerry Yu95699e72022-08-21 19:22:23 +0800214 }
215
216 /* We delete the temporary buffer */
Gilles Peskine449bd832023-01-11 14:50:10 +0100217 mbedtls_free(ticket_buffer);
Jerry Yu95699e72022-08-21 19:22:23 +0800218
Ronald Cronfbae94a2023-12-05 18:15:14 +0100219 if (ret != SSL_TLS1_3_PSK_IDENTITY_MATCH) {
220 goto exit;
Jerry Yuce3b95e2023-10-31 16:02:04 +0800221 }
Jerry Yuce3b95e2023-10-31 16:02:04 +0800222
Ronald Cronfbae94a2023-12-05 18:15:14 +0100223 ret = SSL_TLS1_3_PSK_IDENTITY_MATCH_BUT_PSK_NOT_USABLE;
224
225 if (session->tls_version != MBEDTLS_SSL_VERSION_TLS1_3) {
226 MBEDTLS_SSL_DEBUG_MSG(3, ("Ticket TLS version is not 1.3."));
Jerry Yu8d4bbba2022-09-13 14:15:48 +0800227 goto exit;
228 }
Jerry Yu95699e72022-08-21 19:22:23 +0800229
Gilles Peskine449bd832023-01-11 14:50:10 +0100230#if defined(MBEDTLS_HAVE_TIME)
Jerry Yucebffc32022-12-15 18:00:05 +0800231 now = mbedtls_ms_time();
Gilles Peskine449bd832023-01-11 14:50:10 +0100232
Jerry Yu25ba4d42023-11-10 14:12:20 +0800233 if (now < session->ticket_creation_time) {
Gilles Peskine449bd832023-01-11 14:50:10 +0100234 MBEDTLS_SSL_DEBUG_MSG(
Jerry Yu713ce1f2023-11-17 15:32:12 +0800235 3, ("Invalid ticket creation time ( now = %" MBEDTLS_PRINTF_MS_TIME
236 ", creation_time = %" MBEDTLS_PRINTF_MS_TIME " )",
Jerry Yu25ba4d42023-11-10 14:12:20 +0800237 now, session->ticket_creation_time));
Gilles Peskine449bd832023-01-11 14:50:10 +0100238 goto exit;
239 }
240
Jerry Yu25ba4d42023-11-10 14:12:20 +0800241 server_age = now - session->ticket_creation_time;
Jerry Yu95699e72022-08-21 19:22:23 +0800242
Jerry Yu8d4bbba2022-09-13 14:15:48 +0800243 /* RFC 8446 section 4.6.1
244 *
245 * Servers MUST NOT use any value greater than 604800 seconds (7 days).
246 *
247 * RFC 8446 section 4.2.11.1
248 *
249 * Clients MUST NOT attempt to use tickets which have ages greater than
250 * the "ticket_lifetime" value which was provided with the ticket.
251 *
Jerry Yu8d4bbba2022-09-13 14:15:48 +0800252 */
Jerry Yu8e0174a2023-11-10 13:58:16 +0800253 if (server_age > MBEDTLS_SSL_TLS1_3_MAX_ALLOWED_TICKET_LIFETIME * 1000) {
Jerry Yu8d4bbba2022-09-13 14:15:48 +0800254 MBEDTLS_SSL_DEBUG_MSG(
Jerry Yud84c14f2023-11-16 13:33:57 +0800255 3, ("Ticket age exceeds limitation ticket_age = %" MBEDTLS_PRINTF_MS_TIME,
Jerry Yucebffc32022-12-15 18:00:05 +0800256 server_age));
Jerry Yu8d4bbba2022-09-13 14:15:48 +0800257 goto exit;
258 }
Jerry Yu95699e72022-08-21 19:22:23 +0800259
Jerry Yu8d4bbba2022-09-13 14:15:48 +0800260 /* RFC 8446 section 4.2.10
261 *
262 * For PSKs provisioned via NewSessionTicket, a server MUST validate that
263 * the ticket age for the selected PSK identity (computed by subtracting
264 * ticket_age_add from PskIdentity.obfuscated_ticket_age modulo 2^32) is
265 * within a small tolerance of the time since the ticket was issued.
Jerry Yuf7dad3c2022-09-14 22:31:39 +0800266 *
Jerry Yu31b601a2023-11-10 11:27:21 +0800267 * NOTE: The typical accuracy of an RTC crystal is ±100 to ±20 parts per
268 * million (360 to 72 milliseconds per hour). Default tolerance
Jerry Yu9cb953a2023-11-15 09:54:11 +0800269 * window is 6s, thus in the worst case clients and servers must
Jerry Yu31b601a2023-11-10 11:27:21 +0800270 * sync up their system time every 6000/360/2~=8 hours.
Jerry Yu8d4bbba2022-09-13 14:15:48 +0800271 */
Jerry Yucebffc32022-12-15 18:00:05 +0800272 client_age = obfuscated_ticket_age - session->ticket_age_add;
Jerry Yu60e99722023-11-20 09:55:24 +0800273 age_diff = server_age - (mbedtls_ms_time_t) client_age;
Jerry Yu28e7c552023-11-10 12:05:56 +0800274 if (age_diff < -MBEDTLS_SSL_TLS1_3_TICKET_AGE_TOLERANCE ||
Jerry Yucebffc32022-12-15 18:00:05 +0800275 age_diff > MBEDTLS_SSL_TLS1_3_TICKET_AGE_TOLERANCE) {
Jerry Yu8d4bbba2022-09-13 14:15:48 +0800276 MBEDTLS_SSL_DEBUG_MSG(
Jerry Yuf16efbc2023-10-30 11:06:24 +0800277 3, ("Ticket age outside tolerance window ( diff = %"
278 MBEDTLS_PRINTF_MS_TIME ")",
Jerry Yucebffc32022-12-15 18:00:05 +0800279 age_diff));
Jerry Yu8d4bbba2022-09-13 14:15:48 +0800280 goto exit;
281 }
Jerry Yu95699e72022-08-21 19:22:23 +0800282#endif /* MBEDTLS_HAVE_TIME */
Jerry Yu8d4bbba2022-09-13 14:15:48 +0800283
Ronald Cronfbae94a2023-12-05 18:15:14 +0100284 ret = SSL_TLS1_3_PSK_IDENTITY_MATCH;
285
Jerry Yu8d4bbba2022-09-13 14:15:48 +0800286exit:
Ronald Cronfbae94a2023-12-05 18:15:14 +0100287 if (ret != SSL_TLS1_3_PSK_IDENTITY_MATCH) {
Gilles Peskine449bd832023-01-11 14:50:10 +0100288 mbedtls_ssl_session_free(session);
289 }
Jerry Yu95699e72022-08-21 19:22:23 +0800290
Gilles Peskine449bd832023-01-11 14:50:10 +0100291 MBEDTLS_SSL_DEBUG_MSG(2, ("<= check_identity_match_ticket"));
292 return ret;
Jerry Yu95699e72022-08-21 19:22:23 +0800293}
294#endif /* MBEDTLS_SSL_SESSION_TICKETS */
295
Jerry Yu6e74a7e2022-07-20 20:49:32 +0800296MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yu1c105562022-07-10 06:32:38 +0000297static int ssl_tls13_offered_psks_check_identity_match(
Gilles Peskine449bd832023-01-11 14:50:10 +0100298 mbedtls_ssl_context *ssl,
299 const unsigned char *identity,
300 size_t identity_len,
301 uint32_t obfuscated_ticket_age,
302 int *psk_type,
303 mbedtls_ssl_session *session)
Jerry Yu1c105562022-07-10 06:32:38 +0000304{
Ronald Cron606671e2023-02-17 11:36:33 +0100305 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
306
Jerry Yu95699e72022-08-21 19:22:23 +0800307 ((void) session);
308 ((void) obfuscated_ticket_age);
Jerry Yu5725f1c2022-08-21 17:27:16 +0800309 *psk_type = MBEDTLS_SSL_TLS1_3_PSK_EXTERNAL;
Jerry Yu95699e72022-08-21 19:22:23 +0800310
Gilles Peskine449bd832023-01-11 14:50:10 +0100311 MBEDTLS_SSL_DEBUG_BUF(4, "identity", identity, identity_len);
Jerry Yu95699e72022-08-21 19:22:23 +0800312 ssl->handshake->resume = 0;
313
Jerry Yu95699e72022-08-21 19:22:23 +0800314#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Ronald Cron7a30cf52024-02-23 14:38:59 +0100315 ret = ssl_tls13_offered_psks_check_identity_match_ticket(
316 ssl, identity, identity_len, obfuscated_ticket_age, session);
317 if (ret == SSL_TLS1_3_PSK_IDENTITY_MATCH) {
Jerry Yu95699e72022-08-21 19:22:23 +0800318 ssl->handshake->resume = 1;
319 *psk_type = MBEDTLS_SSL_TLS1_3_PSK_RESUMPTION;
Ronald Cron606671e2023-02-17 11:36:33 +0100320 ret = mbedtls_ssl_set_hs_psk(ssl,
321 session->resumption_key,
322 session->resumption_key_len);
323 if (ret != 0) {
324 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_set_hs_psk", ret);
325 return ret;
326 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100327
328 MBEDTLS_SSL_DEBUG_BUF(4, "Ticket-resumed PSK:",
329 session->resumption_key,
330 session->resumption_key_len);
331 MBEDTLS_SSL_DEBUG_MSG(4, ("ticket: obfuscated_ticket_age: %u",
332 (unsigned) obfuscated_ticket_age));
Ronald Cronfbae94a2023-12-05 18:15:14 +0100333 return SSL_TLS1_3_PSK_IDENTITY_MATCH;
Ronald Cron7a30cf52024-02-23 14:38:59 +0100334 } else if (ret == SSL_TLS1_3_PSK_IDENTITY_MATCH_BUT_PSK_NOT_USABLE) {
335 return SSL_TLS1_3_PSK_IDENTITY_MATCH_BUT_PSK_NOT_USABLE;
Jerry Yu95699e72022-08-21 19:22:23 +0800336 }
337#endif /* MBEDTLS_SSL_SESSION_TICKETS */
338
Jerry Yu1c105562022-07-10 06:32:38 +0000339 /* Check identity with external configured function */
Gilles Peskine449bd832023-01-11 14:50:10 +0100340 if (ssl->conf->f_psk != NULL) {
341 if (ssl->conf->f_psk(
342 ssl->conf->p_psk, ssl, identity, identity_len) == 0) {
Ronald Cronfbae94a2023-12-05 18:15:14 +0100343 return SSL_TLS1_3_PSK_IDENTITY_MATCH;
Jerry Yu1c105562022-07-10 06:32:38 +0000344 }
Ronald Cronfbae94a2023-12-05 18:15:14 +0100345 return SSL_TLS1_3_PSK_IDENTITY_DOES_NOT_MATCH;
Jerry Yu1c105562022-07-10 06:32:38 +0000346 }
347
Gilles Peskine449bd832023-01-11 14:50:10 +0100348 MBEDTLS_SSL_DEBUG_BUF(5, "identity", identity, identity_len);
Jerry Yu1c105562022-07-10 06:32:38 +0000349 /* Check identity with pre-configured psk */
Gilles Peskine449bd832023-01-11 14:50:10 +0100350 if (ssl->conf->psk_identity != NULL &&
Jerry Yu2f0abc92022-07-22 19:34:48 +0800351 identity_len == ssl->conf->psk_identity_len &&
Gilles Peskine449bd832023-01-11 14:50:10 +0100352 mbedtls_ct_memcmp(ssl->conf->psk_identity,
353 identity, identity_len) == 0) {
Ronald Cron606671e2023-02-17 11:36:33 +0100354 ret = mbedtls_ssl_set_hs_psk(ssl, ssl->conf->psk, ssl->conf->psk_len);
355 if (ret != 0) {
356 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_set_hs_psk", ret);
357 return ret;
358 }
Ronald Cronfbae94a2023-12-05 18:15:14 +0100359 return SSL_TLS1_3_PSK_IDENTITY_MATCH;
Jerry Yu1c105562022-07-10 06:32:38 +0000360 }
361
Ronald Cronfbae94a2023-12-05 18:15:14 +0100362 return SSL_TLS1_3_PSK_IDENTITY_DOES_NOT_MATCH;
Jerry Yu1c105562022-07-10 06:32:38 +0000363}
364
Ronald Cron6e311272023-12-05 17:57:01 +0100365#define SSL_TLS1_3_BINDER_DOES_NOT_MATCH 1
366#define SSL_TLS1_3_BINDER_MATCH 0
Jerry Yu6e74a7e2022-07-20 20:49:32 +0800367MBEDTLS_CHECK_RETURN_CRITICAL
Xiaokang Qian9f1747b2023-03-30 02:50:04 +0000368static int ssl_tls13_offered_psks_check_binder_match(
369 mbedtls_ssl_context *ssl,
370 const unsigned char *binder, size_t binder_len,
371 int psk_type, psa_algorithm_t psk_hash_alg)
Jerry Yu1c105562022-07-10 06:32:38 +0000372{
373 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yu96a2e362022-07-21 15:11:34 +0800374
Jerry Yudaf375a2022-07-20 21:31:43 +0800375 unsigned char transcript[PSA_HASH_MAX_SIZE];
Jerry Yu1c105562022-07-10 06:32:38 +0000376 size_t transcript_len;
Jerry Yu2f0abc92022-07-22 19:34:48 +0800377 unsigned char *psk;
Jerry Yu96a2e362022-07-21 15:11:34 +0800378 size_t psk_len;
Jerry Yudaf375a2022-07-20 21:31:43 +0800379 unsigned char server_computed_binder[PSA_HASH_MAX_SIZE];
Jerry Yu1c105562022-07-10 06:32:38 +0000380
Jerry Yu1c105562022-07-10 06:32:38 +0000381 /* Get current state of handshake transcript. */
Jerry Yu29d9faa2022-08-23 17:52:45 +0800382 ret = mbedtls_ssl_get_handshake_transcript(
Manuel Pégourié-Gonnard2d6d9932023-03-28 11:38:08 +0200383 ssl, mbedtls_md_type_from_psa_alg(psk_hash_alg),
Gilles Peskine449bd832023-01-11 14:50:10 +0100384 transcript, sizeof(transcript), &transcript_len);
385 if (ret != 0) {
386 return ret;
387 }
Jerry Yu1c105562022-07-10 06:32:38 +0000388
Gilles Peskine449bd832023-01-11 14:50:10 +0100389 ret = mbedtls_ssl_tls13_export_handshake_psk(ssl, &psk, &psk_len);
390 if (ret != 0) {
391 return ret;
392 }
Jerry Yu96a2e362022-07-21 15:11:34 +0800393
Gilles Peskine449bd832023-01-11 14:50:10 +0100394 ret = mbedtls_ssl_tls13_create_psk_binder(ssl, psk_hash_alg,
395 psk, psk_len, psk_type,
396 transcript,
397 server_computed_binder);
Jerry Yu96a2e362022-07-21 15:11:34 +0800398#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +0100399 mbedtls_free((void *) psk);
Jerry Yu96a2e362022-07-21 15:11:34 +0800400#endif
Gilles Peskine449bd832023-01-11 14:50:10 +0100401 if (ret != 0) {
402 MBEDTLS_SSL_DEBUG_MSG(1, ("PSK binder calculation failed."));
403 return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
Jerry Yu1c105562022-07-10 06:32:38 +0000404 }
405
Gilles Peskine449bd832023-01-11 14:50:10 +0100406 MBEDTLS_SSL_DEBUG_BUF(3, "psk binder ( computed ): ",
407 server_computed_binder, transcript_len);
408 MBEDTLS_SSL_DEBUG_BUF(3, "psk binder ( received ): ", binder, binder_len);
Jerry Yu1c105562022-07-10 06:32:38 +0000409
Gilles Peskine449bd832023-01-11 14:50:10 +0100410 if (mbedtls_ct_memcmp(server_computed_binder, binder, binder_len) == 0) {
Ronald Cron6e311272023-12-05 17:57:01 +0100411 return SSL_TLS1_3_BINDER_MATCH;
Jerry Yu1c105562022-07-10 06:32:38 +0000412 }
413
Gilles Peskine449bd832023-01-11 14:50:10 +0100414 mbedtls_platform_zeroize(server_computed_binder,
415 sizeof(server_computed_binder));
Ronald Cron6e311272023-12-05 17:57:01 +0100416 return SSL_TLS1_3_BINDER_DOES_NOT_MATCH;
Jerry Yu1c105562022-07-10 06:32:38 +0000417}
Jerry Yu96a2e362022-07-21 15:11:34 +0800418
Jerry Yu82534862022-08-30 10:42:33 +0800419#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Jerry Yuf35ba382022-08-23 17:58:26 +0800420MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100421static int ssl_tls13_session_copy_ticket(mbedtls_ssl_session *dst,
422 const mbedtls_ssl_session *src)
Jerry Yu82534862022-08-30 10:42:33 +0800423{
Jerry Yu82534862022-08-30 10:42:33 +0800424 dst->ticket_age_add = src->ticket_age_add;
425 dst->ticket_flags = src->ticket_flags;
426 dst->resumption_key_len = src->resumption_key_len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100427 if (src->resumption_key_len == 0) {
428 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
429 }
430 memcpy(dst->resumption_key, src->resumption_key, src->resumption_key_len);
Jerry Yu4746b102022-09-13 11:11:48 +0800431
Jerry Yu33bf2402022-12-12 16:01:43 +0800432#if defined(MBEDTLS_SSL_EARLY_DATA)
433 dst->max_early_data_size = src->max_early_data_size;
434#endif
435
Gilles Peskine449bd832023-01-11 14:50:10 +0100436 return 0;
Jerry Yu82534862022-08-30 10:42:33 +0800437}
438#endif /* MBEDTLS_SSL_SESSION_TICKETS */
439
Ronald Cron79cdd412024-02-20 17:19:28 +0100440struct psk_attributes {
441 int type;
442 int key_exchange_mode;
443};
444#define PSK_ATTRIBUTES_INIT { 0, 0 }
445
Jerry Yu1c105562022-07-10 06:32:38 +0000446/* Parser for pre_shared_key extension in client hello
447 * struct {
448 * opaque identity<1..2^16-1>;
449 * uint32 obfuscated_ticket_age;
450 * } PskIdentity;
451 *
452 * opaque PskBinderEntry<32..255>;
453 *
454 * struct {
455 * PskIdentity identities<7..2^16-1>;
456 * PskBinderEntry binders<33..2^16-1>;
457 * } OfferedPsks;
458 *
459 * struct {
460 * select (Handshake.msg_type) {
461 * case client_hello: OfferedPsks;
462 * ....
463 * };
464 * } PreSharedKeyExtension;
465 */
Jerry Yu6e74a7e2022-07-20 20:49:32 +0800466MBEDTLS_CHECK_RETURN_CRITICAL
Xiaokang Qian9f1747b2023-03-30 02:50:04 +0000467static int ssl_tls13_parse_pre_shared_key_ext(
468 mbedtls_ssl_context *ssl,
469 const unsigned char *pre_shared_key_ext,
470 const unsigned char *pre_shared_key_ext_end,
471 const unsigned char *ciphersuites,
Ronald Cron79cdd412024-02-20 17:19:28 +0100472 const unsigned char *ciphersuites_end,
473 struct psk_attributes *psk)
Jerry Yu1c105562022-07-10 06:32:38 +0000474{
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +0100475 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yu29d9faa2022-08-23 17:52:45 +0800476 const unsigned char *identities = pre_shared_key_ext;
Jerry Yu568ec252022-07-22 21:27:34 +0800477 const unsigned char *p_identity_len;
478 size_t identities_len;
Jerry Yu1c105562022-07-10 06:32:38 +0000479 const unsigned char *identities_end;
Jerry Yu568ec252022-07-22 21:27:34 +0800480 const unsigned char *binders;
481 const unsigned char *p_binder_len;
482 size_t binders_len;
Jerry Yu1c105562022-07-10 06:32:38 +0000483 const unsigned char *binders_end;
Jerry Yu96a2e362022-07-21 15:11:34 +0800484 int matched_identity = -1;
485 int identity_id = -1;
Jerry Yu1c105562022-07-10 06:32:38 +0000486
Gilles Peskine449bd832023-01-11 14:50:10 +0100487 MBEDTLS_SSL_DEBUG_BUF(3, "pre_shared_key extension",
488 pre_shared_key_ext,
489 pre_shared_key_ext_end - pre_shared_key_ext);
Jerry Yu1c105562022-07-10 06:32:38 +0000490
Jerry Yu96a2e362022-07-21 15:11:34 +0800491 /* identities_len 2 bytes
492 * identities_data >= 7 bytes
493 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100494 MBEDTLS_SSL_CHK_BUF_READ_PTR(identities, pre_shared_key_ext_end, 7 + 2);
495 identities_len = MBEDTLS_GET_UINT16_BE(identities, 0);
Jerry Yu568ec252022-07-22 21:27:34 +0800496 p_identity_len = identities + 2;
Gilles Peskine449bd832023-01-11 14:50:10 +0100497 MBEDTLS_SSL_CHK_BUF_READ_PTR(p_identity_len, pre_shared_key_ext_end,
498 identities_len);
Jerry Yu568ec252022-07-22 21:27:34 +0800499 identities_end = p_identity_len + identities_len;
Jerry Yu1c105562022-07-10 06:32:38 +0000500
Jerry Yu96a2e362022-07-21 15:11:34 +0800501 /* binders_len 2 bytes
502 * binders >= 33 bytes
503 */
Jerry Yu568ec252022-07-22 21:27:34 +0800504 binders = identities_end;
Gilles Peskine449bd832023-01-11 14:50:10 +0100505 MBEDTLS_SSL_CHK_BUF_READ_PTR(binders, pre_shared_key_ext_end, 33 + 2);
506 binders_len = MBEDTLS_GET_UINT16_BE(binders, 0);
Jerry Yu568ec252022-07-22 21:27:34 +0800507 p_binder_len = binders + 2;
Gilles Peskine449bd832023-01-11 14:50:10 +0100508 MBEDTLS_SSL_CHK_BUF_READ_PTR(p_binder_len, pre_shared_key_ext_end, binders_len);
Jerry Yu568ec252022-07-22 21:27:34 +0800509 binders_end = p_binder_len + binders_len;
Jerry Yu1c105562022-07-10 06:32:38 +0000510
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +0100511 ret = ssl->handshake->update_checksum(ssl, pre_shared_key_ext,
512 identities_end - pre_shared_key_ext);
513 if (0 != ret) {
514 MBEDTLS_SSL_DEBUG_RET(1, ("update_checksum"), ret);
515 return ret;
516 }
Jerry Yu96a2e362022-07-21 15:11:34 +0800517
Gilles Peskine449bd832023-01-11 14:50:10 +0100518 while (p_identity_len < identities_end && p_binder_len < binders_end) {
Jerry Yu1c105562022-07-10 06:32:38 +0000519 const unsigned char *identity;
Jerry Yu568ec252022-07-22 21:27:34 +0800520 size_t identity_len;
Jerry Yu82534862022-08-30 10:42:33 +0800521 uint32_t obfuscated_ticket_age;
Jerry Yu96a2e362022-07-21 15:11:34 +0800522 const unsigned char *binder;
Jerry Yu568ec252022-07-22 21:27:34 +0800523 size_t binder_len;
Ronald Cron89089cc2024-02-14 18:18:08 +0100524 int psk_ciphersuite_id;
525 psa_algorithm_t psk_hash_alg;
Ronald Croncf284562024-02-16 18:54:10 +0100526 int allowed_key_exchange_modes;
Jerry Yu29d9faa2022-08-23 17:52:45 +0800527 const mbedtls_ssl_ciphersuite_t *ciphersuite_info;
Jerry Yu82534862022-08-30 10:42:33 +0800528#if defined(MBEDTLS_SSL_SESSION_TICKETS)
529 mbedtls_ssl_session session;
Gilles Peskine449bd832023-01-11 14:50:10 +0100530 mbedtls_ssl_session_init(&session);
Jerry Yu82534862022-08-30 10:42:33 +0800531#endif
Jerry Yubb852022022-07-20 21:10:44 +0800532
Gilles Peskine449bd832023-01-11 14:50:10 +0100533 MBEDTLS_SSL_CHK_BUF_READ_PTR(p_identity_len, identities_end, 2 + 1 + 4);
534 identity_len = MBEDTLS_GET_UINT16_BE(p_identity_len, 0);
Jerry Yu568ec252022-07-22 21:27:34 +0800535 identity = p_identity_len + 2;
Gilles Peskine449bd832023-01-11 14:50:10 +0100536 MBEDTLS_SSL_CHK_BUF_READ_PTR(identity, identities_end, identity_len + 4);
537 obfuscated_ticket_age = MBEDTLS_GET_UINT32_BE(identity, identity_len);
Jerry Yu568ec252022-07-22 21:27:34 +0800538 p_identity_len += identity_len + 6;
Jerry Yu1c105562022-07-10 06:32:38 +0000539
Gilles Peskine449bd832023-01-11 14:50:10 +0100540 MBEDTLS_SSL_CHK_BUF_READ_PTR(p_binder_len, binders_end, 1 + 32);
Jerry Yu568ec252022-07-22 21:27:34 +0800541 binder_len = *p_binder_len;
542 binder = p_binder_len + 1;
Gilles Peskine449bd832023-01-11 14:50:10 +0100543 MBEDTLS_SSL_CHK_BUF_READ_PTR(binder, binders_end, binder_len);
Jerry Yu568ec252022-07-22 21:27:34 +0800544 p_binder_len += binder_len + 1;
Jerry Yu96a2e362022-07-21 15:11:34 +0800545
Jerry Yu96a2e362022-07-21 15:11:34 +0800546 identity_id++;
Gilles Peskine449bd832023-01-11 14:50:10 +0100547 if (matched_identity != -1) {
Jerry Yu1c105562022-07-10 06:32:38 +0000548 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +0100549 }
Jerry Yu1c105562022-07-10 06:32:38 +0000550
Jerry Yu96a2e362022-07-21 15:11:34 +0800551 ret = ssl_tls13_offered_psks_check_identity_match(
Gilles Peskine449bd832023-01-11 14:50:10 +0100552 ssl, identity, identity_len, obfuscated_ticket_age,
Ronald Cron79cdd412024-02-20 17:19:28 +0100553 &psk->type, &session);
Ronald Cronfbae94a2023-12-05 18:15:14 +0100554 if (ret != SSL_TLS1_3_PSK_IDENTITY_MATCH) {
Jerry Yu96a2e362022-07-21 15:11:34 +0800555 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +0100556 }
Jerry Yu96a2e362022-07-21 15:11:34 +0800557
Gilles Peskine449bd832023-01-11 14:50:10 +0100558 MBEDTLS_SSL_DEBUG_MSG(4, ("found matched identity"));
Ronald Cron89089cc2024-02-14 18:18:08 +0100559
Ronald Cron79cdd412024-02-20 17:19:28 +0100560 switch (psk->type) {
Jerry Yu0baf9072022-08-25 11:21:04 +0800561 case MBEDTLS_SSL_TLS1_3_PSK_EXTERNAL:
Ronald Cron89089cc2024-02-14 18:18:08 +0100562 psk_ciphersuite_id = 0;
563 psk_hash_alg = PSA_ALG_SHA_256;
Ronald Croncf284562024-02-16 18:54:10 +0100564 allowed_key_exchange_modes =
565 MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK_ALL;
Jerry Yu0baf9072022-08-25 11:21:04 +0800566 break;
Jerry Yu82534862022-08-30 10:42:33 +0800567#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Ronald Cronf7e99162024-02-14 16:04:02 +0100568 case MBEDTLS_SSL_TLS1_3_PSK_RESUMPTION:
Ronald Cron89089cc2024-02-14 18:18:08 +0100569 psk_ciphersuite_id = session.ciphersuite;
570 psk_hash_alg = PSA_ALG_NONE;
Ronald Croncf284562024-02-16 18:54:10 +0100571 ssl->session_negotiate->ticket_flags = session.ticket_flags;
572 allowed_key_exchange_modes =
573 session.ticket_flags &
574 MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK_ALL;
Jerry Yu0baf9072022-08-25 11:21:04 +0800575 break;
Ronald Cronf7e99162024-02-14 16:04:02 +0100576#endif
Jerry Yu0baf9072022-08-25 11:21:04 +0800577 default:
Gilles Peskine449bd832023-01-11 14:50:10 +0100578 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Jerry Yu0baf9072022-08-25 11:21:04 +0800579 }
Ronald Cron89089cc2024-02-14 18:18:08 +0100580
Ronald Cron79cdd412024-02-20 17:19:28 +0100581 psk->key_exchange_mode = MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_NONE;
582
Ronald Croncf284562024-02-16 18:54:10 +0100583 if ((allowed_key_exchange_modes &
584 MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK_EPHEMERAL) &&
585 ssl_tls13_key_exchange_is_psk_ephemeral_available(ssl)) {
Ronald Cron79cdd412024-02-20 17:19:28 +0100586 psk->key_exchange_mode = MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK_EPHEMERAL;
Ronald Croncf284562024-02-16 18:54:10 +0100587 } else if ((allowed_key_exchange_modes &
588 MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK) &&
589 ssl_tls13_key_exchange_is_psk_available(ssl)) {
Ronald Cron79cdd412024-02-20 17:19:28 +0100590 psk->key_exchange_mode = MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK;
Ronald Croncf284562024-02-16 18:54:10 +0100591 }
592
Ronald Cron79cdd412024-02-20 17:19:28 +0100593 if (psk->key_exchange_mode == MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_NONE) {
Ronald Croncf284562024-02-16 18:54:10 +0100594 MBEDTLS_SSL_DEBUG_MSG(3, ("No suitable PSK key exchange mode"));
595 continue;
596 }
597
Ronald Cron89089cc2024-02-14 18:18:08 +0100598 ssl_tls13_select_ciphersuite(ssl, ciphersuites, ciphersuites_end,
599 psk_ciphersuite_id, psk_hash_alg,
600 &ciphersuite_info);
601
602 if (ciphersuite_info == NULL) {
603#if defined(MBEDTLS_SSL_SESSION_TICKETS)
604 mbedtls_ssl_session_free(&session);
605#endif
606 /*
607 * We consider finding a ciphersuite suitable for the PSK as part
608 * of the validation of its binder. Thus if we do not find one, we
609 * abort the handshake with a decrypt_error alert.
610 */
Jerry Yuf35ba382022-08-23 17:58:26 +0800611 MBEDTLS_SSL_PEND_FATAL_ALERT(
612 MBEDTLS_SSL_ALERT_MSG_DECRYPT_ERROR,
Gilles Peskine449bd832023-01-11 14:50:10 +0100613 MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE);
Ronald Cron89089cc2024-02-14 18:18:08 +0100614 return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
Jerry Yu5725f1c2022-08-21 17:27:16 +0800615 }
616
Jerry Yu96a2e362022-07-21 15:11:34 +0800617 ret = ssl_tls13_offered_psks_check_binder_match(
Ronald Cron79cdd412024-02-20 17:19:28 +0100618 ssl, binder, binder_len, psk->type,
Dave Rodgman2eab4622023-10-05 13:30:37 +0100619 mbedtls_md_psa_alg_from_type((mbedtls_md_type_t) ciphersuite_info->mac));
Ronald Cron6e311272023-12-05 17:57:01 +0100620 if (ret != SSL_TLS1_3_BINDER_MATCH) {
Jerry Yuc5a23a02022-08-25 10:51:44 +0800621 /* For security reasons, the handshake should be aborted when we
622 * fail to validate a binder value. See RFC 8446 section 4.2.11.2
623 * and appendix E.6. */
Jerry Yu82534862022-08-30 10:42:33 +0800624#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Gilles Peskine449bd832023-01-11 14:50:10 +0100625 mbedtls_ssl_session_free(&session);
Jerry Yu82534862022-08-30 10:42:33 +0800626#endif
Gilles Peskine449bd832023-01-11 14:50:10 +0100627 MBEDTLS_SSL_DEBUG_MSG(3, ("Invalid binder."));
Xiaokang Qian9f1747b2023-03-30 02:50:04 +0000628 MBEDTLS_SSL_DEBUG_RET(
629 1, "ssl_tls13_offered_psks_check_binder_match", ret);
Jerry Yu96a2e362022-07-21 15:11:34 +0800630 MBEDTLS_SSL_PEND_FATAL_ALERT(
631 MBEDTLS_SSL_ALERT_MSG_DECRYPT_ERROR,
Gilles Peskine449bd832023-01-11 14:50:10 +0100632 MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE);
633 return ret;
Jerry Yu96a2e362022-07-21 15:11:34 +0800634 }
Jerry Yu96a2e362022-07-21 15:11:34 +0800635
636 matched_identity = identity_id;
Jerry Yu5725f1c2022-08-21 17:27:16 +0800637
638 /* Update handshake parameters */
Jerry Yue5834fd2022-08-29 20:16:09 +0800639 ssl->handshake->ciphersuite_info = ciphersuite_info;
Ronald Cron89089cc2024-02-14 18:18:08 +0100640 ssl->session_negotiate->ciphersuite = ciphersuite_info->id;
Gilles Peskine449bd832023-01-11 14:50:10 +0100641 MBEDTLS_SSL_DEBUG_MSG(2, ("overwrite ciphersuite: %04x - %s",
Ronald Cron89089cc2024-02-14 18:18:08 +0100642 ((unsigned) ciphersuite_info->id),
643 ciphersuite_info->name));
Jerry Yu82534862022-08-30 10:42:33 +0800644#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Ronald Cron79cdd412024-02-20 17:19:28 +0100645 if (psk->type == MBEDTLS_SSL_TLS1_3_PSK_RESUMPTION) {
Gilles Peskine449bd832023-01-11 14:50:10 +0100646 ret = ssl_tls13_session_copy_ticket(ssl->session_negotiate,
647 &session);
648 mbedtls_ssl_session_free(&session);
649 if (ret != 0) {
650 return ret;
651 }
Jerry Yu82534862022-08-30 10:42:33 +0800652 }
653#endif /* MBEDTLS_SSL_SESSION_TICKETS */
Jerry Yu1c105562022-07-10 06:32:38 +0000654 }
655
Gilles Peskine449bd832023-01-11 14:50:10 +0100656 if (p_identity_len != identities_end || p_binder_len != binders_end) {
657 MBEDTLS_SSL_DEBUG_MSG(3, ("pre_shared_key extension decode error"));
658 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR,
659 MBEDTLS_ERR_SSL_DECODE_ERROR);
660 return MBEDTLS_ERR_SSL_DECODE_ERROR;
Jerry Yu1c105562022-07-10 06:32:38 +0000661 }
662
Jerry Yu6f1db3f2022-07-22 23:05:59 +0800663 /* Update the handshake transcript with the binder list. */
Xiaokang Qian9f1747b2023-03-30 02:50:04 +0000664 ret = ssl->handshake->update_checksum(
665 ssl, identities_end, (size_t) (binders_end - identities_end));
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +0100666 if (0 != ret) {
667 MBEDTLS_SSL_DEBUG_RET(1, ("update_checksum"), ret);
668 return ret;
669 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100670 if (matched_identity == -1) {
Ronald Croncf284562024-02-16 18:54:10 +0100671 MBEDTLS_SSL_DEBUG_MSG(3, ("No usable PSK or ticket."));
Gilles Peskine449bd832023-01-11 14:50:10 +0100672 return MBEDTLS_ERR_SSL_UNKNOWN_IDENTITY;
Jerry Yu1c105562022-07-10 06:32:38 +0000673 }
674
Gilles Peskine449bd832023-01-11 14:50:10 +0100675 ssl->handshake->selected_identity = (uint16_t) matched_identity;
676 MBEDTLS_SSL_DEBUG_MSG(3, ("Pre shared key found"));
Jerry Yu1c105562022-07-10 06:32:38 +0000677
Gilles Peskine449bd832023-01-11 14:50:10 +0100678 return 0;
Jerry Yu1c105562022-07-10 06:32:38 +0000679}
Jerry Yu032b15ce2022-07-11 06:10:03 +0000680
681/*
682 * struct {
683 * select ( Handshake.msg_type ) {
684 * ....
685 * case server_hello:
686 * uint16 selected_identity;
687 * }
688 * } PreSharedKeyExtension;
689 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100690static int ssl_tls13_write_server_pre_shared_key_ext(mbedtls_ssl_context *ssl,
691 unsigned char *buf,
692 unsigned char *end,
693 size_t *olen)
Jerry Yu032b15ce2022-07-11 06:10:03 +0000694{
Gilles Peskine449bd832023-01-11 14:50:10 +0100695 unsigned char *p = (unsigned char *) buf;
Jerry Yu032b15ce2022-07-11 06:10:03 +0000696
697 *olen = 0;
698
David Horstmann21b89762022-10-06 18:34:28 +0100699 int not_using_psk = 0;
Jerry Yu032b15ce2022-07-11 06:10:03 +0000700#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +0100701 not_using_psk = (mbedtls_svc_key_id_is_null(ssl->handshake->psk_opaque));
Jerry Yu032b15ce2022-07-11 06:10:03 +0000702#else
Gilles Peskine449bd832023-01-11 14:50:10 +0100703 not_using_psk = (ssl->handshake->psk == NULL);
Jerry Yu032b15ce2022-07-11 06:10:03 +0000704#endif
Gilles Peskine449bd832023-01-11 14:50:10 +0100705 if (not_using_psk) {
Jerry Yu032b15ce2022-07-11 06:10:03 +0000706 /* We shouldn't have called this extension writer unless we've
707 * chosen to use a PSK. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100708 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Jerry Yu032b15ce2022-07-11 06:10:03 +0000709 }
710
Gilles Peskine449bd832023-01-11 14:50:10 +0100711 MBEDTLS_SSL_DEBUG_MSG(3, ("server hello, adding pre_shared_key extension"));
712 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 6);
Jerry Yu032b15ce2022-07-11 06:10:03 +0000713
Gilles Peskine449bd832023-01-11 14:50:10 +0100714 MBEDTLS_PUT_UINT16_BE(MBEDTLS_TLS_EXT_PRE_SHARED_KEY, p, 0);
715 MBEDTLS_PUT_UINT16_BE(2, p, 2);
Jerry Yu032b15ce2022-07-11 06:10:03 +0000716
Gilles Peskine449bd832023-01-11 14:50:10 +0100717 MBEDTLS_PUT_UINT16_BE(ssl->handshake->selected_identity, p, 4);
Jerry Yu032b15ce2022-07-11 06:10:03 +0000718
719 *olen = 6;
720
Gilles Peskine449bd832023-01-11 14:50:10 +0100721 MBEDTLS_SSL_DEBUG_MSG(4, ("sent selected_identity: %u",
722 ssl->handshake->selected_identity));
Jerry Yu032b15ce2022-07-11 06:10:03 +0000723
Gilles Peskine449bd832023-01-11 14:50:10 +0100724 mbedtls_ssl_tls13_set_hs_sent_ext_mask(ssl, MBEDTLS_TLS_EXT_PRE_SHARED_KEY);
Jerry Yub95dd362022-11-08 21:19:34 +0800725
Gilles Peskine449bd832023-01-11 14:50:10 +0100726 return 0;
Jerry Yu032b15ce2022-07-11 06:10:03 +0000727}
728
Ronald Cron41a443a2022-10-04 16:38:25 +0200729#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED */
Jerry Yue19e3b92022-07-08 12:04:51 +0000730
XiaokangQian7807f9f2022-02-15 10:04:37 +0000731/* From RFC 8446:
732 * struct {
XiaokangQiancfd925f2022-04-14 07:10:37 +0000733 * ProtocolVersion versions<2..254>;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000734 * } SupportedVersions;
735 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200736MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100737static int ssl_tls13_parse_supported_versions_ext(mbedtls_ssl_context *ssl,
738 const unsigned char *buf,
739 const unsigned char *end)
XiaokangQian7807f9f2022-02-15 10:04:37 +0000740{
XiaokangQian7807f9f2022-02-15 10:04:37 +0000741 const unsigned char *p = buf;
XiaokangQian8f9dfe42022-04-15 02:52:39 +0000742 size_t versions_len;
XiaokangQian4080a7f2022-04-11 09:55:18 +0000743 const unsigned char *versions_end;
XiaokangQian0a1b54e2022-04-21 03:01:38 +0000744 uint16_t tls_version;
Ronald Cron5af4c7f2023-03-07 20:46:59 +0100745 int found_supported_version = 0;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000746
Gilles Peskine449bd832023-01-11 14:50:10 +0100747 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, 1);
XiaokangQian4080a7f2022-04-11 09:55:18 +0000748 versions_len = p[0];
XiaokangQian7807f9f2022-02-15 10:04:37 +0000749 p += 1;
750
Gilles Peskine449bd832023-01-11 14:50:10 +0100751 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, versions_len);
XiaokangQian4080a7f2022-04-11 09:55:18 +0000752 versions_end = p + versions_len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100753 while (p < versions_end) {
754 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, versions_end, 2);
755 tls_version = mbedtls_ssl_read_version(p, ssl->conf->transport);
XiaokangQianb67384d2022-04-19 00:02:38 +0000756 p += 2;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000757
Ronald Cron3bd2b022023-04-03 16:45:39 +0200758 if (MBEDTLS_SSL_VERSION_TLS1_3 == tls_version) {
Ronald Cron5af4c7f2023-03-07 20:46:59 +0100759 found_supported_version = 1;
760 break;
761 }
762
Ronald Cron3bd2b022023-04-03 16:45:39 +0200763 if ((MBEDTLS_SSL_VERSION_TLS1_2 == tls_version) &&
764 mbedtls_ssl_conf_is_tls12_enabled(ssl->conf)) {
Ronald Cron5af4c7f2023-03-07 20:46:59 +0100765 found_supported_version = 1;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000766 break;
767 }
XiaokangQian7807f9f2022-02-15 10:04:37 +0000768 }
769
Ronald Cron5af4c7f2023-03-07 20:46:59 +0100770 if (!found_supported_version) {
771 MBEDTLS_SSL_DEBUG_MSG(1, ("No supported version found."));
XiaokangQian7807f9f2022-02-15 10:04:37 +0000772
Gilles Peskine449bd832023-01-11 14:50:10 +0100773 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_PROTOCOL_VERSION,
774 MBEDTLS_ERR_SSL_BAD_PROTOCOL_VERSION);
775 return MBEDTLS_ERR_SSL_BAD_PROTOCOL_VERSION;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000776 }
777
Ronald Cron5af4c7f2023-03-07 20:46:59 +0100778 MBEDTLS_SSL_DEBUG_MSG(1, ("Negotiated version: [%04x]",
Gilles Peskine449bd832023-01-11 14:50:10 +0100779 (unsigned int) tls_version));
XiaokangQian7807f9f2022-02-15 10:04:37 +0000780
Ronald Cron5af4c7f2023-03-07 20:46:59 +0100781 return (int) tls_version;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000782}
783
Przemek Stekiel8c0a9532023-06-15 16:48:19 +0200784#if defined(PSA_WANT_ALG_ECDH) || defined(PSA_WANT_ALG_FFDH)
XiaokangQiane8ff3502022-04-22 02:34:40 +0000785/*
XiaokangQian7807f9f2022-02-15 10:04:37 +0000786 *
787 * From RFC 8446:
788 * enum {
789 * ... (0xFFFF)
790 * } NamedGroup;
791 * struct {
792 * NamedGroup named_group_list<2..2^16-1>;
793 * } NamedGroupList;
794 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200795MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100796static int ssl_tls13_parse_supported_groups_ext(mbedtls_ssl_context *ssl,
797 const unsigned char *buf,
798 const unsigned char *end)
XiaokangQian7807f9f2022-02-15 10:04:37 +0000799{
XiaokangQian7807f9f2022-02-15 10:04:37 +0000800 const unsigned char *p = buf;
XiaokangQian84823772022-04-19 07:57:30 +0000801 size_t named_group_list_len;
XiaokangQian8f9dfe42022-04-15 02:52:39 +0000802 const unsigned char *named_group_list_end;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000803
Gilles Peskine449bd832023-01-11 14:50:10 +0100804 MBEDTLS_SSL_DEBUG_BUF(3, "supported_groups extension", p, end - buf);
805 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, 2);
806 named_group_list_len = MBEDTLS_GET_UINT16_BE(p, 0);
XiaokangQian7807f9f2022-02-15 10:04:37 +0000807 p += 2;
Gilles Peskine449bd832023-01-11 14:50:10 +0100808 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, named_group_list_len);
XiaokangQian8f9dfe42022-04-15 02:52:39 +0000809 named_group_list_end = p + named_group_list_len;
XiaokangQian4e8cd7b2022-04-21 09:48:09 +0000810 ssl->handshake->hrr_selected_group = 0;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000811
Gilles Peskine449bd832023-01-11 14:50:10 +0100812 while (p < named_group_list_end) {
XiaokangQian08037552022-04-20 07:16:41 +0000813 uint16_t named_group;
Gilles Peskine449bd832023-01-11 14:50:10 +0100814 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, named_group_list_end, 2);
815 named_group = MBEDTLS_GET_UINT16_BE(p, 0);
XiaokangQian08037552022-04-20 07:16:41 +0000816 p += 2;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000817
Gilles Peskine449bd832023-01-11 14:50:10 +0100818 MBEDTLS_SSL_DEBUG_MSG(2,
819 ("got named group: %s(%04x)",
820 mbedtls_ssl_named_group_to_str(named_group),
821 named_group));
XiaokangQian08037552022-04-20 07:16:41 +0000822
Gilles Peskine449bd832023-01-11 14:50:10 +0100823 if (!mbedtls_ssl_named_group_is_offered(ssl, named_group) ||
824 !mbedtls_ssl_named_group_is_supported(named_group) ||
825 ssl->handshake->hrr_selected_group != 0) {
XiaokangQian08037552022-04-20 07:16:41 +0000826 continue;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000827 }
828
Gilles Peskine449bd832023-01-11 14:50:10 +0100829 MBEDTLS_SSL_DEBUG_MSG(2,
830 ("add named group %s(%04x) into received list.",
831 mbedtls_ssl_named_group_to_str(named_group),
832 named_group));
Jerry Yuc1be19f2022-04-23 16:11:39 +0800833
XiaokangQian4e8cd7b2022-04-21 09:48:09 +0000834 ssl->handshake->hrr_selected_group = named_group;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000835 }
836
Gilles Peskine449bd832023-01-11 14:50:10 +0100837 return 0;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000838
839}
Przemek Stekiel8c0a9532023-06-15 16:48:19 +0200840#endif /* PSA_WANT_ALG_ECDH || PSA_WANT_ALG_FFDH */
XiaokangQian7807f9f2022-02-15 10:04:37 +0000841
XiaokangQian08037552022-04-20 07:16:41 +0000842#define SSL_TLS1_3_PARSE_KEY_SHARES_EXT_NO_MATCH 1
843
Valerio Settic9ae8622023-07-25 11:23:50 +0200844#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_EPHEMERAL_ENABLED)
XiaokangQian7807f9f2022-02-15 10:04:37 +0000845/*
846 * ssl_tls13_parse_key_shares_ext() verifies whether the information in the
Xiaokang Qian9f1747b2023-03-30 02:50:04 +0000847 * extension is correct and stores the first acceptable key share and its
848 * associated group.
XiaokangQian7807f9f2022-02-15 10:04:37 +0000849 *
850 * Possible return values are:
851 * - 0: Successful processing of the client provided key share extension.
Xiaokang Qian9f1747b2023-03-30 02:50:04 +0000852 * - SSL_TLS1_3_PARSE_KEY_SHARES_EXT_NO_MATCH: The key shares provided by
853 * the client does not match a group supported by the server. A
854 * HelloRetryRequest will be needed.
XiaokangQiane8ff3502022-04-22 02:34:40 +0000855 * - A negative value for fatal errors.
Jerry Yuc1be19f2022-04-23 16:11:39 +0800856 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200857MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100858static int ssl_tls13_parse_key_shares_ext(mbedtls_ssl_context *ssl,
859 const unsigned char *buf,
860 const unsigned char *end)
XiaokangQian7807f9f2022-02-15 10:04:37 +0000861{
XiaokangQianb67384d2022-04-19 00:02:38 +0000862 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000863 unsigned char const *p = buf;
XiaokangQian8f9dfe42022-04-15 02:52:39 +0000864 unsigned char const *client_shares_end;
Jerry Yuc1be19f2022-04-23 16:11:39 +0800865 size_t client_shares_len;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000866
867 /* From RFC 8446:
868 *
869 * struct {
870 * KeyShareEntry client_shares<0..2^16-1>;
871 * } KeyShareClientHello;
872 *
873 */
874
Gilles Peskine449bd832023-01-11 14:50:10 +0100875 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, 2);
876 client_shares_len = MBEDTLS_GET_UINT16_BE(p, 0);
XiaokangQian7807f9f2022-02-15 10:04:37 +0000877 p += 2;
Gilles Peskine449bd832023-01-11 14:50:10 +0100878 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, client_shares_len);
XiaokangQian7807f9f2022-02-15 10:04:37 +0000879
880 ssl->handshake->offered_group_id = 0;
XiaokangQian8f9dfe42022-04-15 02:52:39 +0000881 client_shares_end = p + client_shares_len;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000882
883 /* We try to find a suitable key share entry and copy it to the
884 * handshake context. Later, we have to find out whether we can do
885 * something with the provided key share or whether we have to
XiaokangQianc5763b52022-04-02 03:34:37 +0000886 * dismiss it and send a HelloRetryRequest message.
887 */
XiaokangQian7807f9f2022-02-15 10:04:37 +0000888
Gilles Peskine449bd832023-01-11 14:50:10 +0100889 while (p < client_shares_end) {
XiaokangQian9b5d04b2022-04-10 10:20:43 +0000890 uint16_t group;
Jerry Yuc1be19f2022-04-23 16:11:39 +0800891 size_t key_exchange_len;
Jerry Yu086edc22022-05-05 10:50:38 +0800892 const unsigned char *key_exchange;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000893
894 /*
895 * struct {
896 * NamedGroup group;
897 * opaque key_exchange<1..2^16-1>;
898 * } KeyShareEntry;
899 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100900 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, client_shares_end, 4);
901 group = MBEDTLS_GET_UINT16_BE(p, 0);
902 key_exchange_len = MBEDTLS_GET_UINT16_BE(p, 2);
Jerry Yuc1be19f2022-04-23 16:11:39 +0800903 p += 4;
Jerry Yu086edc22022-05-05 10:50:38 +0800904 key_exchange = p;
Gilles Peskine449bd832023-01-11 14:50:10 +0100905 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, client_shares_end, key_exchange_len);
Jerry Yu086edc22022-05-05 10:50:38 +0800906 p += key_exchange_len;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000907
908 /* Continue parsing even if we have already found a match,
XiaokangQianc5763b52022-04-02 03:34:37 +0000909 * for input validation purposes.
910 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100911 if (!mbedtls_ssl_named_group_is_offered(ssl, group) ||
912 !mbedtls_ssl_named_group_is_supported(group) ||
913 ssl->handshake->offered_group_id != 0) {
XiaokangQian060d8672022-04-21 09:24:56 +0000914 continue;
915 }
XiaokangQian060d8672022-04-21 09:24:56 +0000916
XiaokangQian7807f9f2022-02-15 10:04:37 +0000917 /*
Przemek Stekielc89f3ea2023-05-18 15:45:53 +0200918 * ECDHE and FFDHE groups are supported
XiaokangQian7807f9f2022-02-15 10:04:37 +0000919 */
Przemek Stekielc89f3ea2023-05-18 15:45:53 +0200920 if (mbedtls_ssl_tls13_named_group_is_ecdhe(group) ||
Przemek Stekield5f79e72023-06-29 09:08:43 +0200921 mbedtls_ssl_tls13_named_group_is_ffdh(group)) {
Przemek Stekielc89f3ea2023-05-18 15:45:53 +0200922 MBEDTLS_SSL_DEBUG_MSG(2, ("ECDH/FFDH group: %s (%04x)",
Gilles Peskine449bd832023-01-11 14:50:10 +0100923 mbedtls_ssl_named_group_to_str(group),
924 group));
Przemek Stekiel7ac93be2023-07-04 10:02:38 +0200925 ret = mbedtls_ssl_tls13_read_public_xxdhe_share(
Gilles Peskine449bd832023-01-11 14:50:10 +0100926 ssl, key_exchange - 2, key_exchange_len + 2);
927 if (ret != 0) {
928 return ret;
929 }
XiaokangQian4e8cd7b2022-04-21 09:48:09 +0000930
Gilles Peskine449bd832023-01-11 14:50:10 +0100931 } else {
932 MBEDTLS_SSL_DEBUG_MSG(4, ("Unrecognized NamedGroup %u",
933 (unsigned) group));
XiaokangQian7807f9f2022-02-15 10:04:37 +0000934 continue;
935 }
936
XiaokangQian9b5d04b2022-04-10 10:20:43 +0000937 ssl->handshake->offered_group_id = group;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000938 }
939
Jerry Yuc1be19f2022-04-23 16:11:39 +0800940
Gilles Peskine449bd832023-01-11 14:50:10 +0100941 if (ssl->handshake->offered_group_id == 0) {
942 MBEDTLS_SSL_DEBUG_MSG(1, ("no matching key share"));
943 return SSL_TLS1_3_PARSE_KEY_SHARES_EXT_NO_MATCH;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000944 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100945 return 0;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000946}
Valerio Settic9ae8622023-07-25 11:23:50 +0200947#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_EPHEMERAL_ENABLED */
XiaokangQian7807f9f2022-02-15 10:04:37 +0000948
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200949MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100950static int ssl_tls13_client_hello_has_exts(mbedtls_ssl_context *ssl,
951 int exts_mask)
XiaokangQian7807f9f2022-02-15 10:04:37 +0000952{
Jerry Yu0c354a22022-08-29 15:25:36 +0800953 int masked = ssl->handshake->received_extensions & exts_mask;
Gilles Peskine449bd832023-01-11 14:50:10 +0100954 return masked == exts_mask;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000955}
956
Jerry Yue5991322022-11-07 14:03:44 +0800957#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200958MBEDTLS_CHECK_RETURN_CRITICAL
XiaokangQianb67384d2022-04-19 00:02:38 +0000959static int ssl_tls13_client_hello_has_exts_for_ephemeral_key_exchange(
Gilles Peskine449bd832023-01-11 14:50:10 +0100960 mbedtls_ssl_context *ssl)
XiaokangQian7807f9f2022-02-15 10:04:37 +0000961{
Gilles Peskine449bd832023-01-11 14:50:10 +0100962 return ssl_tls13_client_hello_has_exts(
963 ssl,
964 MBEDTLS_SSL_EXT_MASK(SUPPORTED_GROUPS) |
965 MBEDTLS_SSL_EXT_MASK(KEY_SHARE) |
966 MBEDTLS_SSL_EXT_MASK(SIG_ALG));
XiaokangQian7807f9f2022-02-15 10:04:37 +0000967}
Jerry Yue5991322022-11-07 14:03:44 +0800968#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED */
XiaokangQian7807f9f2022-02-15 10:04:37 +0000969
Jerry Yue5991322022-11-07 14:03:44 +0800970#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK_ENABLED)
Jerry Yu77f01482022-07-11 07:03:24 +0000971MBEDTLS_CHECK_RETURN_CRITICAL
972static int ssl_tls13_client_hello_has_exts_for_psk_key_exchange(
Gilles Peskine449bd832023-01-11 14:50:10 +0100973 mbedtls_ssl_context *ssl)
Jerry Yu77f01482022-07-11 07:03:24 +0000974{
Gilles Peskine449bd832023-01-11 14:50:10 +0100975 return ssl_tls13_client_hello_has_exts(
976 ssl,
977 MBEDTLS_SSL_EXT_MASK(PRE_SHARED_KEY) |
978 MBEDTLS_SSL_EXT_MASK(PSK_KEY_EXCHANGE_MODES));
Jerry Yu77f01482022-07-11 07:03:24 +0000979}
Jerry Yue5991322022-11-07 14:03:44 +0800980#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK_ENABLED */
Jerry Yu77f01482022-07-11 07:03:24 +0000981
Jerry Yue5991322022-11-07 14:03:44 +0800982#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK_EPHEMERAL_ENABLED)
Jerry Yu77f01482022-07-11 07:03:24 +0000983MBEDTLS_CHECK_RETURN_CRITICAL
984static int ssl_tls13_client_hello_has_exts_for_psk_ephemeral_key_exchange(
Gilles Peskine449bd832023-01-11 14:50:10 +0100985 mbedtls_ssl_context *ssl)
Jerry Yu77f01482022-07-11 07:03:24 +0000986{
Gilles Peskine449bd832023-01-11 14:50:10 +0100987 return ssl_tls13_client_hello_has_exts(
988 ssl,
989 MBEDTLS_SSL_EXT_MASK(SUPPORTED_GROUPS) |
990 MBEDTLS_SSL_EXT_MASK(KEY_SHARE) |
991 MBEDTLS_SSL_EXT_MASK(PRE_SHARED_KEY) |
992 MBEDTLS_SSL_EXT_MASK(PSK_KEY_EXCHANGE_MODES));
Jerry Yu77f01482022-07-11 07:03:24 +0000993}
Jerry Yue5991322022-11-07 14:03:44 +0800994#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK_EPHEMERAL_ENABLED */
Jerry Yu77f01482022-07-11 07:03:24 +0000995
Pengyu Lv306a01d2023-02-02 16:35:47 +0800996#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED)
997MBEDTLS_CHECK_RETURN_CRITICAL
Pengyu Lvd72e8582023-11-10 10:37:18 +0800998static int ssl_tls13_ticket_is_kex_mode_permitted(mbedtls_ssl_context *ssl,
999 unsigned int kex_mode)
Pengyu Lv306a01d2023-02-02 16:35:47 +08001000{
1001#if defined(MBEDTLS_SSL_SESSION_TICKETS)
1002 if (ssl->handshake->resume) {
Pengyu Lv94a42cc2023-12-06 10:04:17 +08001003 if (!mbedtls_ssl_tls13_session_ticket_has_flags(
Pengyu Lv306a01d2023-02-02 16:35:47 +08001004 ssl->session_negotiate, kex_mode)) {
1005 return 0;
1006 }
1007 }
1008#else
1009 ((void) ssl);
1010 ((void) kex_mode);
1011#endif
1012 return 1;
1013}
XiaokangQian7807f9f2022-02-15 10:04:37 +00001014
Jerry Yu77f01482022-07-11 07:03:24 +00001015MBEDTLS_CHECK_RETURN_CRITICAL
Pengyu Lvbc4aab72023-12-01 15:37:24 +08001016static int ssl_tls13_key_exchange_is_psk_available(mbedtls_ssl_context *ssl)
Jerry Yu77f01482022-07-11 07:03:24 +00001017{
Jerry Yue5991322022-11-07 14:03:44 +08001018#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK_ENABLED)
Pengyu Lvd72e8582023-11-10 10:37:18 +08001019 return ssl_tls13_ticket_is_kex_mode_permitted(
Pengyu Lv306a01d2023-02-02 16:35:47 +08001020 ssl, MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK) &&
Pengyu Lv0a1ff2b2023-11-14 11:03:32 +08001021 mbedtls_ssl_conf_tls13_is_psk_enabled(ssl) &&
Pengyu Lvb2cfafb2023-11-14 13:56:13 +08001022 mbedtls_ssl_tls13_is_psk_supported(ssl) &&
Gilles Peskine449bd832023-01-11 14:50:10 +01001023 ssl_tls13_client_hello_has_exts_for_psk_key_exchange(ssl);
Jerry Yu77f01482022-07-11 07:03:24 +00001024#else
1025 ((void) ssl);
Gilles Peskine449bd832023-01-11 14:50:10 +01001026 return 0;
Jerry Yu77f01482022-07-11 07:03:24 +00001027#endif
1028}
XiaokangQian7807f9f2022-02-15 10:04:37 +00001029
Jerry Yu77f01482022-07-11 07:03:24 +00001030MBEDTLS_CHECK_RETURN_CRITICAL
Pengyu Lvbc4aab72023-12-01 15:37:24 +08001031static int ssl_tls13_key_exchange_is_psk_ephemeral_available(mbedtls_ssl_context *ssl)
Jerry Yu77f01482022-07-11 07:03:24 +00001032{
Jerry Yue5991322022-11-07 14:03:44 +08001033#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK_EPHEMERAL_ENABLED)
Pengyu Lvd72e8582023-11-10 10:37:18 +08001034 return ssl_tls13_ticket_is_kex_mode_permitted(
Pengyu Lv306a01d2023-02-02 16:35:47 +08001035 ssl, MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK_EPHEMERAL) &&
Pengyu Lv0a1ff2b2023-11-14 11:03:32 +08001036 mbedtls_ssl_conf_tls13_is_psk_ephemeral_enabled(ssl) &&
Pengyu Lvb2cfafb2023-11-14 13:56:13 +08001037 mbedtls_ssl_tls13_is_psk_ephemeral_supported(ssl) &&
Gilles Peskine449bd832023-01-11 14:50:10 +01001038 ssl_tls13_client_hello_has_exts_for_psk_ephemeral_key_exchange(ssl);
Jerry Yu77f01482022-07-11 07:03:24 +00001039#else
1040 ((void) ssl);
Gilles Peskine449bd832023-01-11 14:50:10 +01001041 return 0;
Jerry Yu77f01482022-07-11 07:03:24 +00001042#endif
1043}
Ronald Cron79cdd412024-02-20 17:19:28 +01001044#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED */
Jerry Yu77f01482022-07-11 07:03:24 +00001045
Ronald Cron79cdd412024-02-20 17:19:28 +01001046MBEDTLS_CHECK_RETURN_CRITICAL
1047static int ssl_tls13_key_exchange_is_ephemeral_available(mbedtls_ssl_context *ssl)
Jerry Yu77f01482022-07-11 07:03:24 +00001048{
Ronald Cron79cdd412024-02-20 17:19:28 +01001049#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED)
1050 return mbedtls_ssl_conf_tls13_is_ephemeral_enabled(ssl) &&
1051 ssl_tls13_client_hello_has_exts_for_ephemeral_key_exchange(ssl);
1052#else
1053 ((void) ssl);
Gilles Peskine449bd832023-01-11 14:50:10 +01001054 return 0;
Ronald Cron79cdd412024-02-20 17:19:28 +01001055#endif
XiaokangQian7807f9f2022-02-15 10:04:37 +00001056}
1057
XiaokangQian81802f42022-06-10 13:25:22 +00001058#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
Ronald Cron928cbd32022-10-04 16:14:26 +02001059 defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED)
Ronald Cron67ea2542022-09-15 17:34:42 +02001060
1061#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01001062static psa_algorithm_t ssl_tls13_iana_sig_alg_to_psa_alg(uint16_t sig_alg)
Ronald Cron67ea2542022-09-15 17:34:42 +02001063{
Gilles Peskine449bd832023-01-11 14:50:10 +01001064 switch (sig_alg) {
Ronald Cron67ea2542022-09-15 17:34:42 +02001065 case MBEDTLS_TLS1_3_SIG_ECDSA_SECP256R1_SHA256:
Gilles Peskine449bd832023-01-11 14:50:10 +01001066 return PSA_ALG_ECDSA(PSA_ALG_SHA_256);
Ronald Cron67ea2542022-09-15 17:34:42 +02001067 case MBEDTLS_TLS1_3_SIG_ECDSA_SECP384R1_SHA384:
Gilles Peskine449bd832023-01-11 14:50:10 +01001068 return PSA_ALG_ECDSA(PSA_ALG_SHA_384);
Ronald Cron67ea2542022-09-15 17:34:42 +02001069 case MBEDTLS_TLS1_3_SIG_ECDSA_SECP521R1_SHA512:
Gilles Peskine449bd832023-01-11 14:50:10 +01001070 return PSA_ALG_ECDSA(PSA_ALG_SHA_512);
Ronald Cron67ea2542022-09-15 17:34:42 +02001071 case MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA256:
Gilles Peskine449bd832023-01-11 14:50:10 +01001072 return PSA_ALG_RSA_PSS(PSA_ALG_SHA_256);
Ronald Cron67ea2542022-09-15 17:34:42 +02001073 case MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA384:
Gilles Peskine449bd832023-01-11 14:50:10 +01001074 return PSA_ALG_RSA_PSS(PSA_ALG_SHA_384);
Ronald Cron67ea2542022-09-15 17:34:42 +02001075 case MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA512:
Gilles Peskine449bd832023-01-11 14:50:10 +01001076 return PSA_ALG_RSA_PSS(PSA_ALG_SHA_512);
Ronald Cron67ea2542022-09-15 17:34:42 +02001077 case MBEDTLS_TLS1_3_SIG_RSA_PKCS1_SHA256:
Gilles Peskine449bd832023-01-11 14:50:10 +01001078 return PSA_ALG_RSA_PKCS1V15_SIGN(PSA_ALG_SHA_256);
Ronald Cron67ea2542022-09-15 17:34:42 +02001079 case MBEDTLS_TLS1_3_SIG_RSA_PKCS1_SHA384:
Gilles Peskine449bd832023-01-11 14:50:10 +01001080 return PSA_ALG_RSA_PKCS1V15_SIGN(PSA_ALG_SHA_384);
Ronald Cron67ea2542022-09-15 17:34:42 +02001081 case MBEDTLS_TLS1_3_SIG_RSA_PKCS1_SHA512:
Gilles Peskine449bd832023-01-11 14:50:10 +01001082 return PSA_ALG_RSA_PKCS1V15_SIGN(PSA_ALG_SHA_512);
Ronald Cron67ea2542022-09-15 17:34:42 +02001083 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01001084 return PSA_ALG_NONE;
Ronald Cron67ea2542022-09-15 17:34:42 +02001085 }
1086}
1087#endif /* MBEDTLS_USE_PSA_CRYPTO */
1088
XiaokangQian23c5be62022-06-07 02:04:34 +00001089/*
XiaokangQianfb665a82022-06-15 03:57:21 +00001090 * Pick best ( private key, certificate chain ) pair based on the signature
1091 * algorithms supported by the client.
XiaokangQian23c5be62022-06-07 02:04:34 +00001092 */
Ronald Cronce7d76e2022-07-08 18:56:49 +02001093MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01001094static int ssl_tls13_pick_key_cert(mbedtls_ssl_context *ssl)
XiaokangQian23c5be62022-06-07 02:04:34 +00001095{
XiaokangQianfb665a82022-06-15 03:57:21 +00001096 mbedtls_ssl_key_cert *key_cert, *key_cert_list;
XiaokangQian81802f42022-06-10 13:25:22 +00001097 const uint16_t *sig_alg = ssl->handshake->received_sig_algs;
XiaokangQian23c5be62022-06-07 02:04:34 +00001098
1099#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Gilles Peskine449bd832023-01-11 14:50:10 +01001100 if (ssl->handshake->sni_key_cert != NULL) {
XiaokangQianfb665a82022-06-15 03:57:21 +00001101 key_cert_list = ssl->handshake->sni_key_cert;
Gilles Peskine449bd832023-01-11 14:50:10 +01001102 } else
XiaokangQian23c5be62022-06-07 02:04:34 +00001103#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
Gilles Peskine449bd832023-01-11 14:50:10 +01001104 key_cert_list = ssl->conf->key_cert;
XiaokangQian23c5be62022-06-07 02:04:34 +00001105
Gilles Peskine449bd832023-01-11 14:50:10 +01001106 if (key_cert_list == NULL) {
1107 MBEDTLS_SSL_DEBUG_MSG(3, ("server has no certificate"));
1108 return -1;
XiaokangQian23c5be62022-06-07 02:04:34 +00001109 }
1110
Gilles Peskine449bd832023-01-11 14:50:10 +01001111 for (; *sig_alg != MBEDTLS_TLS1_3_SIG_NONE; sig_alg++) {
1112 if (!mbedtls_ssl_sig_alg_is_offered(ssl, *sig_alg)) {
Ronald Cron67ea2542022-09-15 17:34:42 +02001113 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +01001114 }
Ronald Cron67ea2542022-09-15 17:34:42 +02001115
Gilles Peskine449bd832023-01-11 14:50:10 +01001116 if (!mbedtls_ssl_tls13_sig_alg_for_cert_verify_is_supported(*sig_alg)) {
Ronald Cron67ea2542022-09-15 17:34:42 +02001117 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +01001118 }
Ronald Cron67ea2542022-09-15 17:34:42 +02001119
Gilles Peskine449bd832023-01-11 14:50:10 +01001120 for (key_cert = key_cert_list; key_cert != NULL;
1121 key_cert = key_cert->next) {
Ronald Cron67ea2542022-09-15 17:34:42 +02001122#if defined(MBEDTLS_USE_PSA_CRYPTO)
1123 psa_algorithm_t psa_alg = PSA_ALG_NONE;
1124#endif /* MBEDTLS_USE_PSA_CRYPTO */
1125
Gilles Peskine449bd832023-01-11 14:50:10 +01001126 MBEDTLS_SSL_DEBUG_CRT(3, "certificate (chain) candidate",
1127 key_cert->cert);
XiaokangQian81802f42022-06-10 13:25:22 +00001128
1129 /*
Gilles Peskine449bd832023-01-11 14:50:10 +01001130 * This avoids sending the client a cert it'll reject based on
1131 * keyUsage or other extensions.
1132 */
1133 if (mbedtls_x509_crt_check_key_usage(
1134 key_cert->cert, MBEDTLS_X509_KU_DIGITAL_SIGNATURE) != 0 ||
XiaokangQian81802f42022-06-10 13:25:22 +00001135 mbedtls_x509_crt_check_extended_key_usage(
XiaokangQianfb665a82022-06-15 03:57:21 +00001136 key_cert->cert, MBEDTLS_OID_SERVER_AUTH,
Gilles Peskine449bd832023-01-11 14:50:10 +01001137 MBEDTLS_OID_SIZE(MBEDTLS_OID_SERVER_AUTH)) != 0) {
1138 MBEDTLS_SSL_DEBUG_MSG(3, ("certificate mismatch: "
1139 "(extended) key usage extension"));
XiaokangQian81802f42022-06-10 13:25:22 +00001140 continue;
1141 }
XiaokangQian23c5be62022-06-07 02:04:34 +00001142
Gilles Peskine449bd832023-01-11 14:50:10 +01001143 MBEDTLS_SSL_DEBUG_MSG(3,
1144 ("ssl_tls13_pick_key_cert:"
1145 "check signature algorithm %s [%04x]",
1146 mbedtls_ssl_sig_alg_to_str(*sig_alg),
1147 *sig_alg));
Ronald Cron67ea2542022-09-15 17:34:42 +02001148#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01001149 psa_alg = ssl_tls13_iana_sig_alg_to_psa_alg(*sig_alg);
Ronald Cron67ea2542022-09-15 17:34:42 +02001150#endif /* MBEDTLS_USE_PSA_CRYPTO */
1151
Gilles Peskine449bd832023-01-11 14:50:10 +01001152 if (mbedtls_ssl_tls13_check_sig_alg_cert_key_match(
1153 *sig_alg, &key_cert->cert->pk)
Ronald Cron67ea2542022-09-15 17:34:42 +02001154#if defined(MBEDTLS_USE_PSA_CRYPTO)
1155 && psa_alg != PSA_ALG_NONE &&
Gilles Peskine449bd832023-01-11 14:50:10 +01001156 mbedtls_pk_can_do_ext(&key_cert->cert->pk, psa_alg,
1157 PSA_KEY_USAGE_SIGN_HASH) == 1
Ronald Cron67ea2542022-09-15 17:34:42 +02001158#endif /* MBEDTLS_USE_PSA_CRYPTO */
Gilles Peskine449bd832023-01-11 14:50:10 +01001159 ) {
XiaokangQianfb665a82022-06-15 03:57:21 +00001160 ssl->handshake->key_cert = key_cert;
Gilles Peskine449bd832023-01-11 14:50:10 +01001161 MBEDTLS_SSL_DEBUG_MSG(3,
1162 ("ssl_tls13_pick_key_cert:"
1163 "selected signature algorithm"
1164 " %s [%04x]",
1165 mbedtls_ssl_sig_alg_to_str(*sig_alg),
1166 *sig_alg));
XiaokangQianfb665a82022-06-15 03:57:21 +00001167 MBEDTLS_SSL_DEBUG_CRT(
Gilles Peskine449bd832023-01-11 14:50:10 +01001168 3, "selected certificate (chain)",
1169 ssl->handshake->key_cert->cert);
1170 return 0;
XiaokangQian81802f42022-06-10 13:25:22 +00001171 }
XiaokangQian81802f42022-06-10 13:25:22 +00001172 }
XiaokangQian23c5be62022-06-07 02:04:34 +00001173 }
1174
Gilles Peskine449bd832023-01-11 14:50:10 +01001175 MBEDTLS_SSL_DEBUG_MSG(2, ("ssl_tls13_pick_key_cert:"
1176 "no suitable certificate found"));
1177 return -1;
XiaokangQian23c5be62022-06-07 02:04:34 +00001178}
XiaokangQian81802f42022-06-10 13:25:22 +00001179#endif /* MBEDTLS_X509_CRT_PARSE_C &&
Ronald Cron928cbd32022-10-04 16:14:26 +02001180 MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED */
XiaokangQian23c5be62022-06-07 02:04:34 +00001181
XiaokangQian4080a7f2022-04-11 09:55:18 +00001182/*
XiaokangQianed582dd2022-04-13 08:21:05 +00001183 *
1184 * STATE HANDLING: ClientHello
1185 *
XiaokangQianb67384d2022-04-19 00:02:38 +00001186 * There are three possible classes of outcomes when parsing the ClientHello:
XiaokangQianed582dd2022-04-13 08:21:05 +00001187 *
XiaokangQianb67384d2022-04-19 00:02:38 +00001188 * 1) The ClientHello was well-formed and matched the server's configuration.
XiaokangQianed582dd2022-04-13 08:21:05 +00001189 *
1190 * In this case, the server progresses to sending its ServerHello.
1191 *
XiaokangQianb67384d2022-04-19 00:02:38 +00001192 * 2) The ClientHello was well-formed but didn't match the server's
1193 * configuration.
XiaokangQianed582dd2022-04-13 08:21:05 +00001194 *
1195 * For example, the client might not have offered a key share which
1196 * the server supports, or the server might require a cookie.
1197 *
1198 * In this case, the server sends a HelloRetryRequest.
1199 *
XiaokangQianb67384d2022-04-19 00:02:38 +00001200 * 3) The ClientHello was ill-formed
XiaokangQianed582dd2022-04-13 08:21:05 +00001201 *
1202 * In this case, we abort the handshake.
1203 *
1204 */
1205
1206/*
XiaokangQian4080a7f2022-04-11 09:55:18 +00001207 * Structure of this message:
1208 *
XiaokangQiane8ff3502022-04-22 02:34:40 +00001209 * uint16 ProtocolVersion;
1210 * opaque Random[32];
1211 * uint8 CipherSuite[2]; // Cryptographic suite selector
XiaokangQian4080a7f2022-04-11 09:55:18 +00001212 *
XiaokangQiane8ff3502022-04-22 02:34:40 +00001213 * struct {
1214 * ProtocolVersion legacy_version = 0x0303; // TLS v1.2
1215 * Random random;
1216 * opaque legacy_session_id<0..32>;
1217 * CipherSuite cipher_suites<2..2^16-2>;
1218 * opaque legacy_compression_methods<1..2^8-1>;
1219 * Extension extensions<8..2^16-1>;
1220 * } ClientHello;
XiaokangQian4080a7f2022-04-11 09:55:18 +00001221 */
XiaokangQianed582dd2022-04-13 08:21:05 +00001222
1223#define SSL_CLIENT_HELLO_OK 0
1224#define SSL_CLIENT_HELLO_HRR_REQUIRED 1
Ronald Cron5af4c7f2023-03-07 20:46:59 +01001225#define SSL_CLIENT_HELLO_TLS1_2 2
XiaokangQianed582dd2022-04-13 08:21:05 +00001226
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001227MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01001228static int ssl_tls13_parse_client_hello(mbedtls_ssl_context *ssl,
1229 const unsigned char *buf,
1230 const unsigned char *end)
XiaokangQian7807f9f2022-02-15 10:04:37 +00001231{
XiaokangQianb67384d2022-04-19 00:02:38 +00001232 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1233 const unsigned char *p = buf;
Ronald Crond540d992023-03-07 09:41:48 +01001234 const unsigned char *random;
XiaokangQian4080a7f2022-04-11 09:55:18 +00001235 size_t legacy_session_id_len;
Ronald Croncada4102023-03-07 09:51:39 +01001236 const unsigned char *legacy_session_id;
XiaokangQian318dc762022-04-20 09:43:51 +00001237 size_t cipher_suites_len;
Ronald Cron2f16b4e2023-03-07 10:07:32 +01001238 const unsigned char *cipher_suites;
XiaokangQian060d8672022-04-21 09:24:56 +00001239 const unsigned char *cipher_suites_end;
XiaokangQianb67384d2022-04-19 00:02:38 +00001240 size_t extensions_len;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001241 const unsigned char *extensions_end;
Ronald Croneff56732023-04-03 17:36:31 +02001242 const unsigned char *supported_versions_data;
1243 const unsigned char *supported_versions_data_end;
Jerry Yuc4bf5d62022-10-29 09:08:47 +08001244 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
Jerry Yu49ca9282022-05-05 11:05:22 +08001245 int hrr_required = 0;
Ronald Cron8a74f072023-06-14 17:59:29 +02001246 int no_usable_share_for_key_agreement = 0;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001247
Ronald Cron41a443a2022-10-04 16:38:25 +02001248#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED)
Ronald Cron79cdd412024-02-20 17:19:28 +01001249 int got_psk = 0;
1250 struct psk_attributes psk = PSK_ATTRIBUTES_INIT;
Jerry Yu29d9faa2022-08-23 17:52:45 +08001251 const unsigned char *pre_shared_key_ext = NULL;
Jerry Yu1c105562022-07-10 06:32:38 +00001252 const unsigned char *pre_shared_key_ext_end = NULL;
Ronald Cron41a443a2022-10-04 16:38:25 +02001253#endif
XiaokangQian7807f9f2022-02-15 10:04:37 +00001254
XiaokangQian7807f9f2022-02-15 10:04:37 +00001255 /*
XiaokangQianb67384d2022-04-19 00:02:38 +00001256 * ClientHello layout:
XiaokangQian7807f9f2022-02-15 10:04:37 +00001257 * 0 . 1 protocol version
XiaokangQiane8ff3502022-04-22 02:34:40 +00001258 * 2 . 33 random bytes
XiaokangQianc5763b52022-04-02 03:34:37 +00001259 * 34 . 34 session id length ( 1 byte )
XiaokangQian7807f9f2022-02-15 10:04:37 +00001260 * 35 . 34+x session id
XiaokangQian7807f9f2022-02-15 10:04:37 +00001261 * .. . .. ciphersuite list length ( 2 bytes )
1262 * .. . .. ciphersuite list
1263 * .. . .. compression alg. list length ( 1 byte )
1264 * .. . .. compression alg. list
1265 * .. . .. extensions length ( 2 bytes, optional )
1266 * .. . .. extensions ( optional )
1267 */
1268
XiaokangQianb67384d2022-04-19 00:02:38 +00001269 /*
bootstrap-prime6dbbf442022-05-17 19:30:44 -04001270 * Minimal length ( with everything empty and extensions omitted ) is
XiaokangQian7807f9f2022-02-15 10:04:37 +00001271 * 2 + 32 + 1 + 2 + 1 = 38 bytes. Check that first, so that we can
1272 * read at least up to session id length without worrying.
1273 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001274 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, 38);
XiaokangQian7807f9f2022-02-15 10:04:37 +00001275
1276 /* ...
1277 * ProtocolVersion legacy_version = 0x0303; // TLS 1.2
1278 * ...
1279 * with ProtocolVersion defined as:
1280 * uint16 ProtocolVersion;
1281 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001282 if (mbedtls_ssl_read_version(p, ssl->conf->transport) !=
1283 MBEDTLS_SSL_VERSION_TLS1_2) {
1284 MBEDTLS_SSL_DEBUG_MSG(1, ("Unsupported version of TLS."));
1285 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_PROTOCOL_VERSION,
1286 MBEDTLS_ERR_SSL_BAD_PROTOCOL_VERSION);
1287 return MBEDTLS_ERR_SSL_BAD_PROTOCOL_VERSION;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001288 }
1289 p += 2;
1290
Jerry Yuc1be19f2022-04-23 16:11:39 +08001291 /* ...
1292 * Random random;
1293 * ...
XiaokangQianb67384d2022-04-19 00:02:38 +00001294 * with Random defined as:
1295 * opaque Random[32];
XiaokangQian7807f9f2022-02-15 10:04:37 +00001296 */
Ronald Crond540d992023-03-07 09:41:48 +01001297 random = p;
XiaokangQian08037552022-04-20 07:16:41 +00001298 p += MBEDTLS_CLIENT_HELLO_RANDOM_LEN;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001299
Jerry Yuc1be19f2022-04-23 16:11:39 +08001300 /* ...
XiaokangQianb67384d2022-04-19 00:02:38 +00001301 * opaque legacy_session_id<0..32>;
Jerry Yuc1be19f2022-04-23 16:11:39 +08001302 * ...
XiaokangQian7807f9f2022-02-15 10:04:37 +00001303 */
Ronald Croncada4102023-03-07 09:51:39 +01001304 legacy_session_id_len = *(p++);
1305 legacy_session_id = p;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001306
XiaokangQianb67384d2022-04-19 00:02:38 +00001307 /*
1308 * Check we have enough data for the legacy session identifier
Jerry Yue95c8af2022-07-26 15:48:20 +08001309 * and the ciphersuite list length.
XiaokangQianb67384d2022-04-19 00:02:38 +00001310 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001311 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, legacy_session_id_len + 2);
XiaokangQian4080a7f2022-04-11 09:55:18 +00001312 p += legacy_session_id_len;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001313
Ronald Cron2f16b4e2023-03-07 10:07:32 +01001314 /* ...
1315 * CipherSuite cipher_suites<2..2^16-2>;
1316 * ...
1317 * with CipherSuite defined as:
1318 * uint8 CipherSuite[2];
1319 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001320 cipher_suites_len = MBEDTLS_GET_UINT16_BE(p, 0);
XiaokangQian7807f9f2022-02-15 10:04:37 +00001321 p += 2;
Ronald Cron2f16b4e2023-03-07 10:07:32 +01001322 cipher_suites = p;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001323
Ronald Cronfc7ae872023-02-16 15:32:19 +01001324 /*
1325 * The length of the ciphersuite list has to be even.
1326 */
1327 if (cipher_suites_len & 1) {
1328 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR,
1329 MBEDTLS_ERR_SSL_DECODE_ERROR);
1330 return MBEDTLS_ERR_SSL_DECODE_ERROR;
1331 }
1332
XiaokangQianb67384d2022-04-19 00:02:38 +00001333 /* Check we have enough data for the ciphersuite list, the legacy
1334 * compression methods and the length of the extensions.
Jerry Yue95c8af2022-07-26 15:48:20 +08001335 *
1336 * cipher_suites cipher_suites_len bytes
1337 * legacy_compression_methods 2 bytes
1338 * extensions_len 2 bytes
XiaokangQianb67384d2022-04-19 00:02:38 +00001339 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001340 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, cipher_suites_len + 2 + 2);
Ronald Cron8c527d02023-03-07 15:47:47 +01001341 p += cipher_suites_len;
1342 cipher_suites_end = p;
Jerry Yu5725f1c2022-08-21 17:27:16 +08001343
1344 /*
Ronald Cron8c527d02023-03-07 15:47:47 +01001345 * Search for the supported versions extension and parse it to determine
1346 * if the client supports TLS 1.3.
1347 */
1348 ret = mbedtls_ssl_tls13_is_supported_versions_ext_present_in_exts(
1349 ssl, p + 2, end,
Ronald Croneff56732023-04-03 17:36:31 +02001350 &supported_versions_data, &supported_versions_data_end);
Ronald Cron8c527d02023-03-07 15:47:47 +01001351 if (ret < 0) {
1352 MBEDTLS_SSL_DEBUG_RET(1,
1353 ("mbedtls_ssl_tls13_is_supported_versions_ext_present_in_exts"), ret);
1354 return ret;
1355 }
1356
1357 if (ret == 0) {
Ronald Cron5af4c7f2023-03-07 20:46:59 +01001358 return SSL_CLIENT_HELLO_TLS1_2;
Ronald Cron8c527d02023-03-07 15:47:47 +01001359 }
1360
Ronald Cron5af4c7f2023-03-07 20:46:59 +01001361 if (ret == 1) {
1362 ret = ssl_tls13_parse_supported_versions_ext(ssl,
Ronald Croneff56732023-04-03 17:36:31 +02001363 supported_versions_data,
1364 supported_versions_data_end);
Ronald Cron5af4c7f2023-03-07 20:46:59 +01001365 if (ret < 0) {
1366 MBEDTLS_SSL_DEBUG_RET(1,
1367 ("ssl_tls13_parse_supported_versions_ext"), ret);
1368 return ret;
1369 }
1370
Ronald Cronb828c7d2023-04-03 16:37:22 +02001371 /*
1372 * The supported versions extension was parsed successfully as the
1373 * value returned by ssl_tls13_parse_supported_versions_ext() is
1374 * positive. The return value is then equal to
1375 * MBEDTLS_SSL_VERSION_TLS1_2 or MBEDTLS_SSL_VERSION_TLS1_3, defining
1376 * the TLS version to negotiate.
1377 */
Ronald Cron5af4c7f2023-03-07 20:46:59 +01001378 if (MBEDTLS_SSL_VERSION_TLS1_2 == ret) {
1379 return SSL_CLIENT_HELLO_TLS1_2;
1380 }
Ronald Cron8c527d02023-03-07 15:47:47 +01001381 }
1382
1383 /*
1384 * We negotiate TLS 1.3.
Ronald Cron64582392023-03-07 09:21:40 +01001385 */
1386 ssl->tls_version = MBEDTLS_SSL_VERSION_TLS1_3;
Ronald Cron64582392023-03-07 09:21:40 +01001387 ssl->session_negotiate->tls_version = MBEDTLS_SSL_VERSION_TLS1_3;
1388 ssl->session_negotiate->endpoint = ssl->conf->endpoint;
Ronald Cron64582392023-03-07 09:21:40 +01001389
1390 /*
Ronald Crondad02b22023-04-06 09:57:52 +02001391 * We are negotiating the version 1.3 of the protocol. Do what we have
Ronald Croncada4102023-03-07 09:51:39 +01001392 * postponed: copy of the client random bytes, copy of the legacy session
Ronald Cron2f16b4e2023-03-07 10:07:32 +01001393 * identifier and selection of the TLS 1.3 cipher suite.
Ronald Crond540d992023-03-07 09:41:48 +01001394 */
1395 MBEDTLS_SSL_DEBUG_BUF(3, "client hello, random bytes",
1396 random, MBEDTLS_CLIENT_HELLO_RANDOM_LEN);
1397 memcpy(&handshake->randbytes[0], random, MBEDTLS_CLIENT_HELLO_RANDOM_LEN);
1398
Ronald Croncada4102023-03-07 09:51:39 +01001399 if (legacy_session_id_len > sizeof(ssl->session_negotiate->id)) {
1400 MBEDTLS_SSL_DEBUG_MSG(1, ("bad client hello message"));
1401 return MBEDTLS_ERR_SSL_DECODE_ERROR;
1402 }
1403 ssl->session_negotiate->id_len = legacy_session_id_len;
1404 MBEDTLS_SSL_DEBUG_BUF(3, "client hello, session id",
1405 legacy_session_id, legacy_session_id_len);
1406 memcpy(&ssl->session_negotiate->id[0],
1407 legacy_session_id, legacy_session_id_len);
1408
Ronald Crond540d992023-03-07 09:41:48 +01001409 /*
Jerry Yu5725f1c2022-08-21 17:27:16 +08001410 * Search for a matching ciphersuite
1411 */
Ronald Cron2f16b4e2023-03-07 10:07:32 +01001412 MBEDTLS_SSL_DEBUG_BUF(3, "client hello, list of cipher suites",
1413 cipher_suites, cipher_suites_len);
Jerry Yu5725f1c2022-08-21 17:27:16 +08001414
Ronald Cron89089cc2024-02-14 18:18:08 +01001415 ssl_tls13_select_ciphersuite(ssl, cipher_suites, cipher_suites_end,
1416 0, PSA_ALG_NONE, &handshake->ciphersuite_info);
Jerry Yu5725f1c2022-08-21 17:27:16 +08001417
Gilles Peskine449bd832023-01-11 14:50:10 +01001418 if (handshake->ciphersuite_info == NULL) {
1419 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE,
1420 MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE);
1421 return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
Jerry Yu5725f1c2022-08-21 17:27:16 +08001422 }
Ronald Cron89089cc2024-02-14 18:18:08 +01001423 ssl->session_negotiate->ciphersuite = handshake->ciphersuite_info->id;
1424
1425 MBEDTLS_SSL_DEBUG_MSG(2, ("selected ciphersuite: %04x - %s",
1426 ((unsigned) handshake->ciphersuite_info->id),
1427 handshake->ciphersuite_info->name));
Jerry Yuc1be19f2022-04-23 16:11:39 +08001428
XiaokangQian4080a7f2022-04-11 09:55:18 +00001429 /* ...
XiaokangQianb67384d2022-04-19 00:02:38 +00001430 * opaque legacy_compression_methods<1..2^8-1>;
XiaokangQian4080a7f2022-04-11 09:55:18 +00001431 * ...
XiaokangQian7807f9f2022-02-15 10:04:37 +00001432 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001433 if (p[0] != 1 || p[1] != MBEDTLS_SSL_COMPRESS_NULL) {
1434 MBEDTLS_SSL_DEBUG_MSG(1, ("bad legacy compression method"));
1435 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER,
1436 MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER);
1437 return MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001438 }
XiaokangQianb67384d2022-04-19 00:02:38 +00001439 p += 2;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001440
Jerry Yuc1be19f2022-04-23 16:11:39 +08001441 /* ...
XiaokangQianb67384d2022-04-19 00:02:38 +00001442 * Extension extensions<8..2^16-1>;
Jerry Yuc1be19f2022-04-23 16:11:39 +08001443 * ...
XiaokangQianb67384d2022-04-19 00:02:38 +00001444 * with Extension defined as:
1445 * struct {
1446 * ExtensionType extension_type;
1447 * opaque extension_data<0..2^16-1>;
1448 * } Extension;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001449 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001450 extensions_len = MBEDTLS_GET_UINT16_BE(p, 0);
XiaokangQian7807f9f2022-02-15 10:04:37 +00001451 p += 2;
Gilles Peskine449bd832023-01-11 14:50:10 +01001452 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, extensions_len);
XiaokangQiane8ff3502022-04-22 02:34:40 +00001453 extensions_end = p + extensions_len;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001454
Gilles Peskine449bd832023-01-11 14:50:10 +01001455 MBEDTLS_SSL_DEBUG_BUF(3, "client hello extensions", p, extensions_len);
Jerry Yu63a459c2022-10-31 13:38:40 +08001456 handshake->received_extensions = MBEDTLS_SSL_EXT_MASK_NONE;
Jerry Yu0c354a22022-08-29 15:25:36 +08001457
Gilles Peskine449bd832023-01-11 14:50:10 +01001458 while (p < extensions_end) {
XiaokangQian7807f9f2022-02-15 10:04:37 +00001459 unsigned int extension_type;
1460 size_t extension_data_len;
1461 const unsigned char *extension_data_end;
Jerry Yu263dbf72022-10-26 10:51:27 +08001462 uint32_t allowed_exts = MBEDTLS_SSL_TLS1_3_ALLOWED_EXTS_OF_CH;
1463
Ronald Cron5fbd2702024-02-14 10:03:36 +01001464 if (ssl->handshake->hello_retry_request_flag) {
Jerry Yu263dbf72022-10-26 10:51:27 +08001465 /* Do not accept early data extension in 2nd ClientHello */
1466 allowed_exts &= ~MBEDTLS_SSL_EXT_MASK(EARLY_DATA);
1467 }
XiaokangQian7807f9f2022-02-15 10:04:37 +00001468
Jerry Yu0c354a22022-08-29 15:25:36 +08001469 /* RFC 8446, section 4.2.11
Jerry Yu1c9247c2022-07-21 12:37:39 +08001470 *
1471 * The "pre_shared_key" extension MUST be the last extension in the
1472 * ClientHello (this facilitates implementation as described below).
1473 * Servers MUST check that it is the last extension and otherwise fail
1474 * the handshake with an "illegal_parameter" alert.
1475 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001476 if (handshake->received_extensions & MBEDTLS_SSL_EXT_MASK(PRE_SHARED_KEY)) {
Jerry Yu1c9247c2022-07-21 12:37:39 +08001477 MBEDTLS_SSL_DEBUG_MSG(
Gilles Peskine449bd832023-01-11 14:50:10 +01001478 3, ("pre_shared_key is not last extension."));
Jerry Yu1c9247c2022-07-21 12:37:39 +08001479 MBEDTLS_SSL_PEND_FATAL_ALERT(
1480 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER,
Gilles Peskine449bd832023-01-11 14:50:10 +01001481 MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER);
1482 return MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER;
Jerry Yu1c9247c2022-07-21 12:37:39 +08001483 }
1484
Gilles Peskine449bd832023-01-11 14:50:10 +01001485 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, extensions_end, 4);
1486 extension_type = MBEDTLS_GET_UINT16_BE(p, 0);
1487 extension_data_len = MBEDTLS_GET_UINT16_BE(p, 2);
XiaokangQian7807f9f2022-02-15 10:04:37 +00001488 p += 4;
1489
Gilles Peskine449bd832023-01-11 14:50:10 +01001490 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, extensions_end, extension_data_len);
XiaokangQian7807f9f2022-02-15 10:04:37 +00001491 extension_data_end = p + extension_data_len;
1492
Jerry Yuc4bf5d62022-10-29 09:08:47 +08001493 ret = mbedtls_ssl_tls13_check_received_extension(
Gilles Peskine449bd832023-01-11 14:50:10 +01001494 ssl, MBEDTLS_SSL_HS_CLIENT_HELLO, extension_type,
Jerry Yu263dbf72022-10-26 10:51:27 +08001495 allowed_exts);
Gilles Peskine449bd832023-01-11 14:50:10 +01001496 if (ret != 0) {
1497 return ret;
1498 }
Jerry Yue18dc7e2022-08-04 16:29:22 +08001499
Gilles Peskine449bd832023-01-11 14:50:10 +01001500 switch (extension_type) {
XiaokangQian40a35232022-05-07 09:02:40 +00001501#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
1502 case MBEDTLS_TLS_EXT_SERVERNAME:
Gilles Peskine449bd832023-01-11 14:50:10 +01001503 MBEDTLS_SSL_DEBUG_MSG(3, ("found ServerName extension"));
1504 ret = mbedtls_ssl_parse_server_name_ext(ssl, p,
1505 extension_data_end);
1506 if (ret != 0) {
XiaokangQian40a35232022-05-07 09:02:40 +00001507 MBEDTLS_SSL_DEBUG_RET(
Gilles Peskine449bd832023-01-11 14:50:10 +01001508 1, "mbedtls_ssl_parse_servername_ext", ret);
1509 return ret;
XiaokangQian40a35232022-05-07 09:02:40 +00001510 }
XiaokangQian40a35232022-05-07 09:02:40 +00001511 break;
1512#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
1513
Przemek Stekiel8c0a9532023-06-15 16:48:19 +02001514#if defined(PSA_WANT_ALG_ECDH) || defined(PSA_WANT_ALG_FFDH)
XiaokangQian7807f9f2022-02-15 10:04:37 +00001515 case MBEDTLS_TLS_EXT_SUPPORTED_GROUPS:
Gilles Peskine449bd832023-01-11 14:50:10 +01001516 MBEDTLS_SSL_DEBUG_MSG(3, ("found supported group extension"));
XiaokangQian7807f9f2022-02-15 10:04:37 +00001517
1518 /* Supported Groups Extension
1519 *
1520 * When sent by the client, the "supported_groups" extension
1521 * indicates the named groups which the client supports,
1522 * ordered from most preferred to least preferred.
1523 */
Jerry Yuc1be19f2022-04-23 16:11:39 +08001524 ret = ssl_tls13_parse_supported_groups_ext(
Gilles Peskine449bd832023-01-11 14:50:10 +01001525 ssl, p, extension_data_end);
1526 if (ret != 0) {
Xiaokang Qian9f1747b2023-03-30 02:50:04 +00001527 MBEDTLS_SSL_DEBUG_RET(
Xiaokang Qian8bce0e62023-04-04 10:15:48 +00001528 1, "ssl_tls13_parse_supported_groups_ext", ret);
Gilles Peskine449bd832023-01-11 14:50:10 +01001529 return ret;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001530 }
1531
XiaokangQian7807f9f2022-02-15 10:04:37 +00001532 break;
Przemek Stekiel8c0a9532023-06-15 16:48:19 +02001533#endif /* PSA_WANT_ALG_ECDH || PSA_WANT_ALG_FFDH*/
XiaokangQian7807f9f2022-02-15 10:04:37 +00001534
Valerio Settic9ae8622023-07-25 11:23:50 +02001535#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_EPHEMERAL_ENABLED)
XiaokangQian7807f9f2022-02-15 10:04:37 +00001536 case MBEDTLS_TLS_EXT_KEY_SHARE:
Gilles Peskine449bd832023-01-11 14:50:10 +01001537 MBEDTLS_SSL_DEBUG_MSG(3, ("found key share extension"));
XiaokangQian7807f9f2022-02-15 10:04:37 +00001538
1539 /*
1540 * Key Share Extension
1541 *
1542 * When sent by the client, the "key_share" extension
1543 * contains the endpoint's cryptographic parameters for
1544 * ECDHE/DHE key establishment methods.
1545 */
Jerry Yuc1be19f2022-04-23 16:11:39 +08001546 ret = ssl_tls13_parse_key_shares_ext(
Gilles Peskine449bd832023-01-11 14:50:10 +01001547 ssl, p, extension_data_end);
1548 if (ret == SSL_TLS1_3_PARSE_KEY_SHARES_EXT_NO_MATCH) {
Ronald Cron8a74f072023-06-14 17:59:29 +02001549 MBEDTLS_SSL_DEBUG_MSG(2, ("No usable share for key agreement."));
1550 no_usable_share_for_key_agreement = 1;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001551 }
1552
Gilles Peskine449bd832023-01-11 14:50:10 +01001553 if (ret < 0) {
Jerry Yu582dd062022-04-22 21:59:01 +08001554 MBEDTLS_SSL_DEBUG_RET(
Gilles Peskine449bd832023-01-11 14:50:10 +01001555 1, "ssl_tls13_parse_key_shares_ext", ret);
1556 return ret;
Jerry Yu582dd062022-04-22 21:59:01 +08001557 }
XiaokangQian7807f9f2022-02-15 10:04:37 +00001558
XiaokangQian7807f9f2022-02-15 10:04:37 +00001559 break;
Valerio Settic9ae8622023-07-25 11:23:50 +02001560#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_EPHEMERAL_ENABLED */
XiaokangQian7807f9f2022-02-15 10:04:37 +00001561
1562 case MBEDTLS_TLS_EXT_SUPPORTED_VERSIONS:
Ronald Cron8c527d02023-03-07 15:47:47 +01001563 /* Already parsed */
XiaokangQian7807f9f2022-02-15 10:04:37 +00001564 break;
1565
Ronald Cron41a443a2022-10-04 16:38:25 +02001566#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED)
Jerry Yue19e3b92022-07-08 12:04:51 +00001567 case MBEDTLS_TLS_EXT_PSK_KEY_EXCHANGE_MODES:
Xiaokang Qian9f1747b2023-03-30 02:50:04 +00001568 MBEDTLS_SSL_DEBUG_MSG(
1569 3, ("found psk key exchange modes extension"));
Jerry Yue19e3b92022-07-08 12:04:51 +00001570
1571 ret = ssl_tls13_parse_key_exchange_modes_ext(
Gilles Peskine449bd832023-01-11 14:50:10 +01001572 ssl, p, extension_data_end);
1573 if (ret != 0) {
Jerry Yue19e3b92022-07-08 12:04:51 +00001574 MBEDTLS_SSL_DEBUG_RET(
Gilles Peskine449bd832023-01-11 14:50:10 +01001575 1, "ssl_tls13_parse_key_exchange_modes_ext", ret);
1576 return ret;
Jerry Yue19e3b92022-07-08 12:04:51 +00001577 }
1578
Jerry Yue19e3b92022-07-08 12:04:51 +00001579 break;
Ronald Cron41a443a2022-10-04 16:38:25 +02001580#endif
Jerry Yue19e3b92022-07-08 12:04:51 +00001581
Jerry Yu1c105562022-07-10 06:32:38 +00001582 case MBEDTLS_TLS_EXT_PRE_SHARED_KEY:
Gilles Peskine449bd832023-01-11 14:50:10 +01001583 MBEDTLS_SSL_DEBUG_MSG(3, ("found pre_shared_key extension"));
1584 if ((handshake->received_extensions &
1585 MBEDTLS_SSL_EXT_MASK(PSK_KEY_EXCHANGE_MODES)) == 0) {
Jerry Yu13ab81d2022-07-22 23:17:11 +08001586 MBEDTLS_SSL_PEND_FATAL_ALERT(
1587 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER,
Gilles Peskine449bd832023-01-11 14:50:10 +01001588 MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER);
1589 return MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER;
Jerry Yu13ab81d2022-07-22 23:17:11 +08001590 }
Ronald Cron41a443a2022-10-04 16:38:25 +02001591#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED)
Jerry Yu1c105562022-07-10 06:32:38 +00001592 /* Delay processing of the PSK identity once we have
1593 * found out which algorithms to use. We keep a pointer
1594 * to the buffer and the size for later processing.
1595 */
Jerry Yu29d9faa2022-08-23 17:52:45 +08001596 pre_shared_key_ext = p;
Jerry Yu1c105562022-07-10 06:32:38 +00001597 pre_shared_key_ext_end = extension_data_end;
Jerry Yue18dc7e2022-08-04 16:29:22 +08001598#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED */
Jerry Yu1c105562022-07-10 06:32:38 +00001599 break;
Jerry Yu1c105562022-07-10 06:32:38 +00001600
XiaokangQianacb39922022-06-17 10:18:48 +00001601#if defined(MBEDTLS_SSL_ALPN)
1602 case MBEDTLS_TLS_EXT_ALPN:
Gilles Peskine449bd832023-01-11 14:50:10 +01001603 MBEDTLS_SSL_DEBUG_MSG(3, ("found alpn extension"));
XiaokangQianacb39922022-06-17 10:18:48 +00001604
Gilles Peskine449bd832023-01-11 14:50:10 +01001605 ret = mbedtls_ssl_parse_alpn_ext(ssl, p, extension_data_end);
1606 if (ret != 0) {
XiaokangQianacb39922022-06-17 10:18:48 +00001607 MBEDTLS_SSL_DEBUG_RET(
Gilles Peskine449bd832023-01-11 14:50:10 +01001608 1, ("mbedtls_ssl_parse_alpn_ext"), ret);
1609 return ret;
XiaokangQianacb39922022-06-17 10:18:48 +00001610 }
XiaokangQianacb39922022-06-17 10:18:48 +00001611 break;
1612#endif /* MBEDTLS_SSL_ALPN */
1613
Ronald Cron928cbd32022-10-04 16:14:26 +02001614#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED)
XiaokangQian7807f9f2022-02-15 10:04:37 +00001615 case MBEDTLS_TLS_EXT_SIG_ALG:
Gilles Peskine449bd832023-01-11 14:50:10 +01001616 MBEDTLS_SSL_DEBUG_MSG(3, ("found signature_algorithms extension"));
XiaokangQian7807f9f2022-02-15 10:04:37 +00001617
Gabor Mezei078e8032022-04-27 21:17:56 +02001618 ret = mbedtls_ssl_parse_sig_alg_ext(
Gilles Peskine449bd832023-01-11 14:50:10 +01001619 ssl, p, extension_data_end);
1620 if (ret != 0) {
Xiaokang Qian49f39c12023-04-06 02:31:02 +00001621 MBEDTLS_SSL_DEBUG_RET(
1622 1, "mbedtls_ssl_parse_sig_alg_ext", ret);
Gilles Peskine449bd832023-01-11 14:50:10 +01001623 return ret;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001624 }
XiaokangQian7807f9f2022-02-15 10:04:37 +00001625 break;
Ronald Cron928cbd32022-10-04 16:14:26 +02001626#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED */
XiaokangQian7807f9f2022-02-15 10:04:37 +00001627
Jan Bruckner151f6422023-02-10 12:45:19 +01001628#if defined(MBEDTLS_SSL_RECORD_SIZE_LIMIT)
1629 case MBEDTLS_TLS_EXT_RECORD_SIZE_LIMIT:
1630 MBEDTLS_SSL_DEBUG_MSG(3, ("found record_size_limit extension"));
1631
Xiaokang Qian9f1747b2023-03-30 02:50:04 +00001632 ret = mbedtls_ssl_tls13_parse_record_size_limit_ext(
1633 ssl, p, extension_data_end);
Jan Brucknerf482dcc2023-03-15 09:09:06 +01001634 if (ret != 0) {
1635 MBEDTLS_SSL_DEBUG_RET(
1636 1, ("mbedtls_ssl_tls13_parse_record_size_limit_ext"), ret);
1637 return ret;
1638 }
Jan Bruckner151f6422023-02-10 12:45:19 +01001639 break;
1640#endif /* MBEDTLS_SSL_RECORD_SIZE_LIMIT */
1641
XiaokangQian7807f9f2022-02-15 10:04:37 +00001642 default:
Jerry Yu79aa7212022-11-08 21:30:21 +08001643 MBEDTLS_SSL_PRINT_EXT(
Jerry Yu63a459c2022-10-31 13:38:40 +08001644 3, MBEDTLS_SSL_HS_CLIENT_HELLO,
Gilles Peskine449bd832023-01-11 14:50:10 +01001645 extension_type, "( ignored )");
Jerry Yu0c354a22022-08-29 15:25:36 +08001646 break;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001647 }
1648
1649 p += extension_data_len;
1650 }
1651
Gilles Peskine449bd832023-01-11 14:50:10 +01001652 MBEDTLS_SSL_PRINT_EXTS(3, MBEDTLS_SSL_HS_CLIENT_HELLO,
1653 handshake->received_extensions);
Jerry Yue18dc7e2022-08-04 16:29:22 +08001654
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +01001655 ret = mbedtls_ssl_add_hs_hdr_to_checksum(ssl,
1656 MBEDTLS_SSL_HS_CLIENT_HELLO,
1657 p - buf);
1658 if (0 != ret) {
1659 MBEDTLS_SSL_DEBUG_RET(1, ("mbedtls_ssl_add_hs_hdr_to_checksum"), ret);
1660 return ret;
1661 }
Jerry Yu032b15ce2022-07-11 06:10:03 +00001662
Ronald Cron41a443a2022-10-04 16:38:25 +02001663#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED)
XiaokangQian7807f9f2022-02-15 10:04:37 +00001664 /* Update checksum with either
1665 * - The entire content of the CH message, if no PSK extension is present
1666 * - The content up to but excluding the PSK extension, if present.
Ronald Cron12e72f12024-02-14 15:49:38 +01001667 * Always parse the pre-shared key extension when present in the
1668 * ClientHello even if some pre-requisites for PSK key exchange modes are
1669 * not met. That way we always validate the syntax of the extension.
XiaokangQian7807f9f2022-02-15 10:04:37 +00001670 */
Ronald Cron12e72f12024-02-14 15:49:38 +01001671 if (handshake->received_extensions & MBEDTLS_SSL_EXT_MASK(PRE_SHARED_KEY)) {
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +01001672 ret = handshake->update_checksum(ssl, buf,
1673 pre_shared_key_ext - buf);
1674 if (0 != ret) {
1675 MBEDTLS_SSL_DEBUG_RET(1, ("update_checksum"), ret);
1676 return ret;
1677 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001678 ret = ssl_tls13_parse_pre_shared_key_ext(ssl,
1679 pre_shared_key_ext,
1680 pre_shared_key_ext_end,
1681 cipher_suites,
Ronald Cron79cdd412024-02-20 17:19:28 +01001682 cipher_suites_end,
1683 &psk);
1684 if (ret == 0) {
1685 got_psk = 1;
1686 } else if (ret != MBEDTLS_ERR_SSL_UNKNOWN_IDENTITY) {
Jerry Yu63a459c2022-10-31 13:38:40 +08001687 MBEDTLS_SSL_DEBUG_RET(
Gilles Peskine449bd832023-01-11 14:50:10 +01001688 1, "ssl_tls13_parse_pre_shared_key_ext", ret);
1689 return ret;
Jerry Yu1c105562022-07-10 06:32:38 +00001690 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001691 } else
Ronald Cron41a443a2022-10-04 16:38:25 +02001692#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED */
Jerry Yu1c105562022-07-10 06:32:38 +00001693 {
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +01001694 ret = handshake->update_checksum(ssl, buf, p - buf);
1695 if (0 != ret) {
1696 MBEDTLS_SSL_DEBUG_RET(1, ("update_checksum"), ret);
1697 return ret;
1698 }
Jerry Yu1c105562022-07-10 06:32:38 +00001699 }
XiaokangQian7807f9f2022-02-15 10:04:37 +00001700
Ronald Cron79cdd412024-02-20 17:19:28 +01001701 /*
1702 * Determine the key exchange algorithm to use.
1703 * There are three types of key exchanges supported in TLS 1.3:
1704 * - (EC)DH with ECDSA,
1705 * - (EC)DH with PSK,
1706 * - plain PSK.
1707 *
1708 * The PSK-based key exchanges may additionally be used with 0-RTT.
1709 *
1710 * Our built-in order of preference is
1711 * 1 ) (EC)DHE-PSK Mode ( psk_ephemeral )
1712 * 2 ) Certificate Mode ( ephemeral )
1713 * 3 ) Plain PSK Mode ( psk )
1714 */
1715#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED)
1716 if (got_psk && (psk.key_exchange_mode ==
1717 MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK_EPHEMERAL)) {
1718 handshake->key_exchange_mode =
1719 MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK_EPHEMERAL;
1720 MBEDTLS_SSL_DEBUG_MSG(2, ("key exchange mode: psk_ephemeral"));
1721
1722 } else
1723#endif
1724 if (ssl_tls13_key_exchange_is_ephemeral_available(ssl)) {
1725 handshake->key_exchange_mode =
1726 MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL;
1727 MBEDTLS_SSL_DEBUG_MSG(2, ("key exchange mode: ephemeral"));
1728
1729 }
1730#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED)
1731 else if (got_psk && (psk.key_exchange_mode ==
1732 MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK)) {
1733 handshake->key_exchange_mode = MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK;
1734 MBEDTLS_SSL_DEBUG_MSG(2, ("key exchange mode: psk"));
1735 }
1736#endif
1737 else {
1738 MBEDTLS_SSL_DEBUG_MSG(
1739 1,
1740 ("ClientHello message misses mandatory extensions."));
1741 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_MISSING_EXTENSION,
1742 MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER);
1743 return MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER;
Gilles Peskine449bd832023-01-11 14:50:10 +01001744 }
XiaokangQian7807f9f2022-02-15 10:04:37 +00001745
Ronald Cron79cdd412024-02-20 17:19:28 +01001746 if (handshake->key_exchange_mode ==
1747 MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL) {
1748 handshake->resume = 0;
1749 }
1750
1751 if (handshake->key_exchange_mode !=
Ronald Cron8a74f072023-06-14 17:59:29 +02001752 MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK) {
1753 hrr_required = (no_usable_share_for_key_agreement != 0);
1754 }
1755
Gilles Peskine449bd832023-01-11 14:50:10 +01001756 mbedtls_ssl_optimize_checksum(ssl, handshake->ciphersuite_info);
Jerry Yue5834fd2022-08-29 20:16:09 +08001757
Gilles Peskine449bd832023-01-11 14:50:10 +01001758 return hrr_required ? SSL_CLIENT_HELLO_HRR_REQUIRED : SSL_CLIENT_HELLO_OK;
XiaokangQian75fe8c72022-06-15 09:42:45 +00001759}
1760
Jerry Yuab0da372023-02-08 13:55:24 +08001761#if defined(MBEDTLS_SSL_EARLY_DATA)
Jerry Yu4caf3ca2023-11-15 16:13:47 +08001762static int ssl_tls13_check_early_data_requirements(mbedtls_ssl_context *ssl)
Jerry Yuab0da372023-02-08 13:55:24 +08001763{
1764 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
1765
Jerry Yu985c9672022-12-04 14:06:30 +08001766 if (ssl->conf->early_data_enabled == MBEDTLS_SSL_EARLY_DATA_DISABLED) {
1767 MBEDTLS_SSL_DEBUG_MSG(
Jerry Yu985c9672022-12-04 14:06:30 +08001768 1,
Jerry Yu454dda32023-10-31 15:13:54 +08001769 ("EarlyData: rejected, feature disabled in server configuration."));
Jerry Yu4caf3ca2023-11-15 16:13:47 +08001770 return -1;
Ronald Cron7b6ee942024-01-12 10:29:55 +01001771 }
1772
Jerry Yu454dda32023-10-31 15:13:54 +08001773 if (!handshake->resume) {
1774 /* We currently support early data only in the case of PSKs established
1775 via a NewSessionTicket message thus in the case of a session
1776 resumption. */
Jerry Yu985c9672022-12-04 14:06:30 +08001777 MBEDTLS_SSL_DEBUG_MSG(
Jerry Yu7ef9fd82023-11-07 14:30:38 +08001778 1, ("EarlyData: rejected, not a session resumption."));
Jerry Yu4caf3ca2023-11-15 16:13:47 +08001779 return -1;
Jerry Yu985c9672022-12-04 14:06:30 +08001780 }
1781
Jerry Yu82fd6c12023-10-31 16:32:19 +08001782 /* RFC 8446 4.2.10
1783 *
1784 * In order to accept early data, the server MUST have accepted a PSK cipher
1785 * suite and selected the first key offered in the client's "pre_shared_key"
1786 * extension. In addition, it MUST verify that the following values are the
1787 * same as those associated with the selected PSK:
1788 * - The TLS version number
1789 * - The selected cipher suite
1790 * - The selected ALPN [RFC7301] protocol, if any
1791 *
1792 * NOTE:
Jerry Yu7ef9fd82023-11-07 14:30:38 +08001793 * - The TLS version number is checked in
1794 * ssl_tls13_offered_psks_check_identity_match_ticket().
1795 * - ALPN is not checked for the time being (TODO).
Jerry Yu82fd6c12023-10-31 16:32:19 +08001796 */
1797
1798 if (handshake->selected_identity != 0) {
1799 MBEDTLS_SSL_DEBUG_MSG(
Jerry Yu7ef9fd82023-11-07 14:30:38 +08001800 1, ("EarlyData: rejected, the selected key in "
1801 "`pre_shared_key` is not the first one."));
Jerry Yu4caf3ca2023-11-15 16:13:47 +08001802 return -1;
Jerry Yu82fd6c12023-10-31 16:32:19 +08001803 }
1804
1805 if (handshake->ciphersuite_info->id !=
1806 ssl->session_negotiate->ciphersuite) {
1807 MBEDTLS_SSL_DEBUG_MSG(
Jerry Yu7ef9fd82023-11-07 14:30:38 +08001808 1, ("EarlyData: rejected, the selected ciphersuite is not the one "
1809 "of the selected pre-shared key."));
Jerry Yu4caf3ca2023-11-15 16:13:47 +08001810 return -1;
Jerry Yu82fd6c12023-10-31 16:32:19 +08001811
1812 }
Jerry Yu985c9672022-12-04 14:06:30 +08001813
Pengyu Lv94a42cc2023-12-06 10:04:17 +08001814 if (!mbedtls_ssl_tls13_session_ticket_allow_early_data(ssl->session_negotiate)) {
Jerry Yufceddb32022-12-12 15:30:34 +08001815 MBEDTLS_SSL_DEBUG_MSG(
1816 1,
Jerry Yuea96ac32023-11-21 17:06:36 +08001817 ("EarlyData: rejected, early_data not allowed in ticket "
1818 "permission bits."));
Jerry Yu4caf3ca2023-11-15 16:13:47 +08001819 return -1;
Jerry Yufceddb32022-12-12 15:30:34 +08001820 }
Jerry Yu985c9672022-12-04 14:06:30 +08001821
Jerry Yu4caf3ca2023-11-15 16:13:47 +08001822 return 0;
Jerry Yuab0da372023-02-08 13:55:24 +08001823}
1824#endif /* MBEDTLS_SSL_EARLY_DATA */
1825
XiaokangQian75fe8c72022-06-15 09:42:45 +00001826/* Update the handshake state machine */
1827
Ronald Cronce7d76e2022-07-08 18:56:49 +02001828MBEDTLS_CHECK_RETURN_CRITICAL
Ronald Cron7b6ee942024-01-12 10:29:55 +01001829static int ssl_tls13_postprocess_client_hello(mbedtls_ssl_context *ssl,
1830 int hrr_required)
XiaokangQian75fe8c72022-06-15 09:42:45 +00001831{
1832 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1833
XiaokangQian7807f9f2022-02-15 10:04:37 +00001834 /*
XiaokangQianfb665a82022-06-15 03:57:21 +00001835 * Server certificate selection
1836 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001837 if (ssl->conf->f_cert_cb && (ret = ssl->conf->f_cert_cb(ssl)) != 0) {
1838 MBEDTLS_SSL_DEBUG_RET(1, "f_cert_cb", ret);
1839 return ret;
XiaokangQianfb665a82022-06-15 03:57:21 +00001840 }
1841#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
1842 ssl->handshake->sni_name = NULL;
1843 ssl->handshake->sni_name_len = 0;
1844#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
XiaokangQian7807f9f2022-02-15 10:04:37 +00001845
Gilles Peskine449bd832023-01-11 14:50:10 +01001846 ret = mbedtls_ssl_tls13_key_schedule_stage_early(ssl);
1847 if (ret != 0) {
1848 MBEDTLS_SSL_DEBUG_RET(1,
1849 "mbedtls_ssl_tls1_3_key_schedule_stage_early", ret);
1850 return ret;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001851 }
1852
Jerry Yuab0da372023-02-08 13:55:24 +08001853#if defined(MBEDTLS_SSL_EARLY_DATA)
Jerry Yu4caf3ca2023-11-15 16:13:47 +08001854 if (ssl->handshake->received_extensions & MBEDTLS_SSL_EXT_MASK(EARLY_DATA)) {
Ronald Cron31e2d832024-02-05 16:45:57 +01001855 ssl->handshake->early_data_accepted =
1856 (!hrr_required) && (ssl_tls13_check_early_data_requirements(ssl) == 0);
Jerry Yu4e9b70e2022-12-04 14:08:02 +08001857
Jerry Yu4caf3ca2023-11-15 16:13:47 +08001858 if (ssl->handshake->early_data_accepted) {
1859 ret = mbedtls_ssl_tls13_compute_early_transform(ssl);
1860 if (ret != 0) {
1861 MBEDTLS_SSL_DEBUG_RET(
1862 1, "mbedtls_ssl_tls13_compute_early_transform", ret);
1863 return ret;
1864 }
1865 } else {
1866 ssl->discard_early_data_record =
1867 hrr_required ?
1868 MBEDTLS_SSL_EARLY_DATA_DISCARD :
1869 MBEDTLS_SSL_EARLY_DATA_TRY_TO_DEPROTECT_AND_DISCARD;
Jerry Yu4e9b70e2022-12-04 14:08:02 +08001870 }
1871 }
Ronald Cron7b6ee942024-01-12 10:29:55 +01001872#else
1873 ((void) hrr_required);
Jerry Yuab0da372023-02-08 13:55:24 +08001874#endif /* MBEDTLS_SSL_EARLY_DATA */
1875
Gilles Peskine449bd832023-01-11 14:50:10 +01001876 return 0;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001877}
1878
1879/*
XiaokangQianed582dd2022-04-13 08:21:05 +00001880 * Main entry point from the state machine; orchestrates the otherfunctions.
1881 */
1882
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001883MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01001884static int ssl_tls13_process_client_hello(mbedtls_ssl_context *ssl)
XiaokangQianed582dd2022-04-13 08:21:05 +00001885{
1886
XiaokangQian08037552022-04-20 07:16:41 +00001887 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine449bd832023-01-11 14:50:10 +01001888 unsigned char *buf = NULL;
XiaokangQianed582dd2022-04-13 08:21:05 +00001889 size_t buflen = 0;
Jerry Yu4ca91402022-05-09 15:50:57 +08001890 int parse_client_hello_ret;
1891
Gilles Peskine449bd832023-01-11 14:50:10 +01001892 MBEDTLS_SSL_DEBUG_MSG(2, ("=> parse client hello"));
XiaokangQianed582dd2022-04-13 08:21:05 +00001893
Gilles Peskine449bd832023-01-11 14:50:10 +01001894 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_tls13_fetch_handshake_msg(
1895 ssl, MBEDTLS_SSL_HS_CLIENT_HELLO,
1896 &buf, &buflen));
XiaokangQianed582dd2022-04-13 08:21:05 +00001897
Gilles Peskine449bd832023-01-11 14:50:10 +01001898 MBEDTLS_SSL_PROC_CHK_NEG(ssl_tls13_parse_client_hello(ssl, buf,
1899 buf + buflen));
Ronald Cron5af4c7f2023-03-07 20:46:59 +01001900 parse_client_hello_ret = ret; /* Store positive return value of
1901 * parse_client_hello,
1902 * as negative error codes are handled
Jerry Yuf41553b2022-05-09 22:20:30 +08001903 * by MBEDTLS_SSL_PROC_CHK_NEG. */
Jerry Yu4ca91402022-05-09 15:50:57 +08001904
Ronald Cronb828c7d2023-04-03 16:37:22 +02001905 /*
Yanray Wang90acdc62023-12-08 10:29:42 +08001906 * Version 1.2 of the protocol has to be used for the handshake.
1907 * If TLS 1.2 is not supported, abort the handshake. Otherwise, set the
Ronald Cronb828c7d2023-04-03 16:37:22 +02001908 * ssl->keep_current_message flag for the ClientHello to be kept and parsed
1909 * as a TLS 1.2 ClientHello. We also change ssl->tls_version to
1910 * MBEDTLS_SSL_VERSION_TLS1_2 thus from now on mbedtls_ssl_handshake_step()
1911 * will dispatch to the TLS 1.2 state machine.
1912 */
Ronald Cron5af4c7f2023-03-07 20:46:59 +01001913 if (SSL_CLIENT_HELLO_TLS1_2 == parse_client_hello_ret) {
Yanray Wangfb0f47b2023-12-04 15:27:28 +08001914 /* Check if server supports TLS 1.2 */
Yanray Wang408ba6f2023-12-08 10:18:03 +08001915 if (!mbedtls_ssl_conf_is_tls12_enabled(ssl->conf)) {
Yanray Wangfb0f47b2023-12-04 15:27:28 +08001916 MBEDTLS_SSL_DEBUG_MSG(
Yanray Wang177e49a2023-12-08 10:51:04 +08001917 1, ("TLS 1.2 not supported."));
Yanray Wangfb0f47b2023-12-04 15:27:28 +08001918 MBEDTLS_SSL_PEND_FATAL_ALERT(
Yanray Wang2bef9172023-12-08 10:21:53 +08001919 MBEDTLS_SSL_ALERT_MSG_PROTOCOL_VERSION,
1920 MBEDTLS_ERR_SSL_BAD_PROTOCOL_VERSION);
1921 return MBEDTLS_ERR_SSL_BAD_PROTOCOL_VERSION;
Yanray Wangfb0f47b2023-12-04 15:27:28 +08001922 }
Ronald Cron5af4c7f2023-03-07 20:46:59 +01001923 ssl->keep_current_message = 1;
1924 ssl->tls_version = MBEDTLS_SSL_VERSION_TLS1_2;
1925 return 0;
1926 }
1927
Ronald Cron7b6ee942024-01-12 10:29:55 +01001928 MBEDTLS_SSL_PROC_CHK(
1929 ssl_tls13_postprocess_client_hello(ssl, parse_client_hello_ret ==
1930 SSL_CLIENT_HELLO_HRR_REQUIRED));
Jerry Yu582dd062022-04-22 21:59:01 +08001931
Ronald Cron5af4c7f2023-03-07 20:46:59 +01001932 if (SSL_CLIENT_HELLO_OK == parse_client_hello_ret) {
Gilles Peskine449bd832023-01-11 14:50:10 +01001933 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_SERVER_HELLO);
1934 } else {
1935 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_HELLO_RETRY_REQUEST);
1936 }
XiaokangQianed582dd2022-04-13 08:21:05 +00001937
1938cleanup:
1939
Gilles Peskine449bd832023-01-11 14:50:10 +01001940 MBEDTLS_SSL_DEBUG_MSG(2, ("<= parse client hello"));
1941 return ret;
XiaokangQianed582dd2022-04-13 08:21:05 +00001942}
1943
1944/*
Jerry Yu1c3e6882022-04-20 21:23:40 +08001945 * Handler for MBEDTLS_SSL_SERVER_HELLO
Jerry Yu5b64ae92022-03-30 17:15:02 +08001946 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001947MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01001948static int ssl_tls13_prepare_server_hello(mbedtls_ssl_context *ssl)
Jerry Yuf4b27e42022-03-30 17:32:21 +08001949{
Jerry Yu637a3f12022-04-20 21:37:58 +08001950 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1951 unsigned char *server_randbytes =
Gilles Peskine449bd832023-01-11 14:50:10 +01001952 ssl->handshake->randbytes + MBEDTLS_CLIENT_HELLO_RANDOM_LEN;
1953 if (ssl->conf->f_rng == NULL) {
1954 MBEDTLS_SSL_DEBUG_MSG(1, ("no RNG provided"));
1955 return MBEDTLS_ERR_SSL_NO_RNG;
Jerry Yuf4b27e42022-03-30 17:32:21 +08001956 }
1957
Gilles Peskine449bd832023-01-11 14:50:10 +01001958 if ((ret = ssl->conf->f_rng(ssl->conf->p_rng, server_randbytes,
1959 MBEDTLS_SERVER_HELLO_RANDOM_LEN)) != 0) {
1960 MBEDTLS_SSL_DEBUG_RET(1, "f_rng", ret);
1961 return ret;
Jerry Yuf4b27e42022-03-30 17:32:21 +08001962 }
1963
Gilles Peskine449bd832023-01-11 14:50:10 +01001964 MBEDTLS_SSL_DEBUG_BUF(3, "server hello, random bytes", server_randbytes,
1965 MBEDTLS_SERVER_HELLO_RANDOM_LEN);
Jerry Yuf4b27e42022-03-30 17:32:21 +08001966
1967#if defined(MBEDTLS_HAVE_TIME)
YxCda609132023-05-22 12:08:12 -07001968 ssl->session_negotiate->start = mbedtls_time(NULL);
Jerry Yuf4b27e42022-03-30 17:32:21 +08001969#endif /* MBEDTLS_HAVE_TIME */
1970
Gilles Peskine449bd832023-01-11 14:50:10 +01001971 return ret;
Jerry Yuf4b27e42022-03-30 17:32:21 +08001972}
1973
Jerry Yu3bf2c642022-03-30 22:02:12 +08001974/*
Jerry Yue74e04a2022-04-21 09:23:16 +08001975 * ssl_tls13_write_server_hello_supported_versions_ext ():
Jerry Yu3bf2c642022-03-30 22:02:12 +08001976 *
1977 * struct {
Jerry Yufb9f54d2022-04-06 10:08:34 +08001978 * ProtocolVersion selected_version;
Jerry Yu3bf2c642022-03-30 22:02:12 +08001979 * } SupportedVersions;
1980 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001981MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yue74e04a2022-04-21 09:23:16 +08001982static int ssl_tls13_write_server_hello_supported_versions_ext(
Gilles Peskine449bd832023-01-11 14:50:10 +01001983 mbedtls_ssl_context *ssl,
1984 unsigned char *buf,
1985 unsigned char *end,
1986 size_t *out_len)
Jerry Yu3bf2c642022-03-30 22:02:12 +08001987{
Jerry Yu3bf2c642022-03-30 22:02:12 +08001988 *out_len = 0;
1989
Gilles Peskine449bd832023-01-11 14:50:10 +01001990 MBEDTLS_SSL_DEBUG_MSG(3, ("server hello, write selected version"));
Jerry Yu3bf2c642022-03-30 22:02:12 +08001991
1992 /* Check if we have space to write the extension:
1993 * - extension_type (2 bytes)
1994 * - extension_data_length (2 bytes)
Jerry Yu349a6132022-04-14 20:52:56 +08001995 * - selected_version (2 bytes)
Jerry Yu3bf2c642022-03-30 22:02:12 +08001996 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001997 MBEDTLS_SSL_CHK_BUF_PTR(buf, end, 6);
Jerry Yu3bf2c642022-03-30 22:02:12 +08001998
Gilles Peskine449bd832023-01-11 14:50:10 +01001999 MBEDTLS_PUT_UINT16_BE(MBEDTLS_TLS_EXT_SUPPORTED_VERSIONS, buf, 0);
Jerry Yu3bf2c642022-03-30 22:02:12 +08002000
Gilles Peskine449bd832023-01-11 14:50:10 +01002001 MBEDTLS_PUT_UINT16_BE(2, buf, 2);
Jerry Yu3bf2c642022-03-30 22:02:12 +08002002
Gilles Peskine449bd832023-01-11 14:50:10 +01002003 mbedtls_ssl_write_version(buf + 4,
2004 ssl->conf->transport,
2005 ssl->tls_version);
Jerry Yu3bf2c642022-03-30 22:02:12 +08002006
Gilles Peskine449bd832023-01-11 14:50:10 +01002007 MBEDTLS_SSL_DEBUG_MSG(3, ("supported version: [%04x]",
2008 ssl->tls_version));
Jerry Yu3bf2c642022-03-30 22:02:12 +08002009
Jerry Yu349a6132022-04-14 20:52:56 +08002010 *out_len = 6;
Jerry Yu3bf2c642022-03-30 22:02:12 +08002011
Jerry Yub95dd362022-11-08 21:19:34 +08002012 mbedtls_ssl_tls13_set_hs_sent_ext_mask(
Gilles Peskine449bd832023-01-11 14:50:10 +01002013 ssl, MBEDTLS_TLS_EXT_SUPPORTED_VERSIONS);
Jerry Yub95dd362022-11-08 21:19:34 +08002014
Gilles Peskine449bd832023-01-11 14:50:10 +01002015 return 0;
Jerry Yu3bf2c642022-03-30 22:02:12 +08002016}
2017
Jerry Yud9436a12022-04-20 22:28:09 +08002018
Jerry Yu3bf2c642022-03-30 22:02:12 +08002019
2020/* Generate and export a single key share. For hybrid KEMs, this can
2021 * be called multiple times with the different components of the hybrid. */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002022MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002023static int ssl_tls13_generate_and_write_key_share(mbedtls_ssl_context *ssl,
2024 uint16_t named_group,
2025 unsigned char *buf,
2026 unsigned char *end,
2027 size_t *out_len)
Jerry Yu3bf2c642022-03-30 22:02:12 +08002028{
2029 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yu955ddd72022-04-22 22:27:33 +08002030
2031 *out_len = 0;
2032
Valerio Settic9ae8622023-07-25 11:23:50 +02002033#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_EPHEMERAL_ENABLED)
Przemek Stekiel29c219c2023-05-31 15:21:04 +02002034 if (mbedtls_ssl_tls13_named_group_is_ecdhe(named_group) ||
Przemek Stekield5f79e72023-06-29 09:08:43 +02002035 mbedtls_ssl_tls13_named_group_is_ffdh(named_group)) {
Przemek Stekiel408569f2023-07-06 11:26:44 +02002036 ret = mbedtls_ssl_tls13_generate_and_write_xxdh_key_exchange(
Gilles Peskine449bd832023-01-11 14:50:10 +01002037 ssl, named_group, buf, end, out_len);
2038 if (ret != 0) {
Jerry Yu89e103c2022-03-30 22:43:29 +08002039 MBEDTLS_SSL_DEBUG_RET(
Przemek Stekiel408569f2023-07-06 11:26:44 +02002040 1, "mbedtls_ssl_tls13_generate_and_write_xxdh_key_exchange",
Gilles Peskine449bd832023-01-11 14:50:10 +01002041 ret);
2042 return ret;
Jerry Yu3bf2c642022-03-30 22:02:12 +08002043 }
Gilles Peskine449bd832023-01-11 14:50:10 +01002044 } else
Valerio Settic9ae8622023-07-25 11:23:50 +02002045#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_EPHEMERAL_ENABLED */
Gilles Peskine449bd832023-01-11 14:50:10 +01002046 if (0 /* Other kinds of KEMs */) {
2047 } else {
Jerry Yu955ddd72022-04-22 22:27:33 +08002048 ((void) ssl);
2049 ((void) named_group);
2050 ((void) buf);
2051 ((void) end);
Jerry Yu3bf2c642022-03-30 22:02:12 +08002052 ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
2053 }
2054
Gilles Peskine449bd832023-01-11 14:50:10 +01002055 return ret;
Jerry Yu3bf2c642022-03-30 22:02:12 +08002056}
2057
2058/*
2059 * ssl_tls13_write_key_share_ext
2060 *
2061 * Structure of key_share extension in ServerHello:
2062 *
Jerry Yu1c3e6882022-04-20 21:23:40 +08002063 * struct {
2064 * NamedGroup group;
2065 * opaque key_exchange<1..2^16-1>;
2066 * } KeyShareEntry;
2067 * struct {
2068 * KeyShareEntry server_share;
2069 * } KeyShareServerHello;
Jerry Yu3bf2c642022-03-30 22:02:12 +08002070 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002071MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002072static int ssl_tls13_write_key_share_ext(mbedtls_ssl_context *ssl,
2073 unsigned char *buf,
2074 unsigned char *end,
2075 size_t *out_len)
Jerry Yu3bf2c642022-03-30 22:02:12 +08002076{
Jerry Yu955ddd72022-04-22 22:27:33 +08002077 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yu3bf2c642022-03-30 22:02:12 +08002078 unsigned char *p = buf;
Jerry Yu57d48412022-04-20 21:50:42 +08002079 uint16_t group = ssl->handshake->offered_group_id;
Jerry Yu3bf2c642022-03-30 22:02:12 +08002080 unsigned char *server_share = buf + 4;
Jerry Yu3bf2c642022-03-30 22:02:12 +08002081 size_t key_exchange_length;
Jerry Yu3bf2c642022-03-30 22:02:12 +08002082
2083 *out_len = 0;
2084
Gilles Peskine449bd832023-01-11 14:50:10 +01002085 MBEDTLS_SSL_DEBUG_MSG(3, ("server hello, adding key share extension"));
Jerry Yu3bf2c642022-03-30 22:02:12 +08002086
Gilles Peskine449bd832023-01-11 14:50:10 +01002087 MBEDTLS_SSL_DEBUG_MSG(2, ("server hello, write selected_group: %s (%04x)",
2088 mbedtls_ssl_named_group_to_str(group),
2089 group));
Jerry Yu58af2332022-09-06 11:19:31 +08002090
Jerry Yu3bf2c642022-03-30 22:02:12 +08002091 /* Check if we have space for header and length fields:
2092 * - extension_type (2 bytes)
2093 * - extension_data_length (2 bytes)
2094 * - group (2 bytes)
2095 * - key_exchange_length (2 bytes)
2096 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002097 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 8);
2098 MBEDTLS_PUT_UINT16_BE(MBEDTLS_TLS_EXT_KEY_SHARE, p, 0);
2099 MBEDTLS_PUT_UINT16_BE(group, server_share, 0);
Jerry Yu3bf2c642022-03-30 22:02:12 +08002100 p += 8;
Jerry Yu57d48412022-04-20 21:50:42 +08002101
Jerry Yu3bf2c642022-03-30 22:02:12 +08002102 /* When we introduce PQC-ECDHE hybrids, we'll want to call this
2103 * function multiple times. */
Jerry Yu955ddd72022-04-22 22:27:33 +08002104 ret = ssl_tls13_generate_and_write_key_share(
Gilles Peskine449bd832023-01-11 14:50:10 +01002105 ssl, group, server_share + 4, end, &key_exchange_length);
2106 if (ret != 0) {
2107 return ret;
2108 }
Jerry Yu3bf2c642022-03-30 22:02:12 +08002109 p += key_exchange_length;
Jerry Yuc1be19f2022-04-23 16:11:39 +08002110
Gilles Peskine449bd832023-01-11 14:50:10 +01002111 MBEDTLS_PUT_UINT16_BE(key_exchange_length, server_share + 2, 0);
Jerry Yu3bf2c642022-03-30 22:02:12 +08002112
Gilles Peskine449bd832023-01-11 14:50:10 +01002113 MBEDTLS_PUT_UINT16_BE(p - server_share, buf, 2);
Jerry Yu3bf2c642022-03-30 22:02:12 +08002114
Jerry Yu57d48412022-04-20 21:50:42 +08002115 *out_len = p - buf;
Jerry Yu955ddd72022-04-22 22:27:33 +08002116
Gilles Peskine449bd832023-01-11 14:50:10 +01002117 mbedtls_ssl_tls13_set_hs_sent_ext_mask(ssl, MBEDTLS_TLS_EXT_KEY_SHARE);
Jerry Yub95dd362022-11-08 21:19:34 +08002118
Gilles Peskine449bd832023-01-11 14:50:10 +01002119 return 0;
Jerry Yu3bf2c642022-03-30 22:02:12 +08002120}
Jerry Yud9436a12022-04-20 22:28:09 +08002121
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002122MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002123static int ssl_tls13_write_hrr_key_share_ext(mbedtls_ssl_context *ssl,
2124 unsigned char *buf,
2125 unsigned char *end,
2126 size_t *out_len)
Jerry Yu23f7a6f2022-04-23 15:16:45 +08002127{
2128 uint16_t selected_group = ssl->handshake->hrr_selected_group;
2129 /* key_share Extension
2130 *
2131 * struct {
2132 * select (Handshake.msg_type) {
2133 * ...
2134 * case hello_retry_request:
2135 * NamedGroup selected_group;
2136 * ...
2137 * };
2138 * } KeyShare;
2139 */
2140
2141 *out_len = 0;
2142
Jerry Yub0ac10b2022-05-05 11:10:08 +08002143 /*
2144 * For a pure PSK key exchange, there is no group to agree upon. The purpose
2145 * of the HRR is then to transmit a cookie to force the client to demonstrate
2146 * reachability at their apparent network address (primarily useful for DTLS).
2147 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002148 if (!mbedtls_ssl_tls13_key_exchange_mode_with_ephemeral(ssl)) {
2149 return 0;
2150 }
Jerry Yu23f7a6f2022-04-23 15:16:45 +08002151
2152 /* We should only send the key_share extension if the client's initial
2153 * key share was not acceptable. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002154 if (ssl->handshake->offered_group_id != 0) {
2155 MBEDTLS_SSL_DEBUG_MSG(4, ("Skip key_share extension in HRR"));
2156 return 0;
Jerry Yu23f7a6f2022-04-23 15:16:45 +08002157 }
2158
Gilles Peskine449bd832023-01-11 14:50:10 +01002159 if (selected_group == 0) {
2160 MBEDTLS_SSL_DEBUG_MSG(1, ("no matching named group found"));
2161 return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
Jerry Yu23f7a6f2022-04-23 15:16:45 +08002162 }
2163
Jerry Yub0ac10b2022-05-05 11:10:08 +08002164 /* Check if we have enough space:
2165 * - extension_type (2 bytes)
2166 * - extension_data_length (2 bytes)
2167 * - selected_group (2 bytes)
2168 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002169 MBEDTLS_SSL_CHK_BUF_PTR(buf, end, 6);
Jerry Yu23f7a6f2022-04-23 15:16:45 +08002170
Gilles Peskine449bd832023-01-11 14:50:10 +01002171 MBEDTLS_PUT_UINT16_BE(MBEDTLS_TLS_EXT_KEY_SHARE, buf, 0);
2172 MBEDTLS_PUT_UINT16_BE(2, buf, 2);
2173 MBEDTLS_PUT_UINT16_BE(selected_group, buf, 4);
Jerry Yu23f7a6f2022-04-23 15:16:45 +08002174
Gilles Peskine449bd832023-01-11 14:50:10 +01002175 MBEDTLS_SSL_DEBUG_MSG(3,
2176 ("HRR selected_group: %s (%x)",
2177 mbedtls_ssl_named_group_to_str(selected_group),
2178 selected_group));
Jerry Yu23f7a6f2022-04-23 15:16:45 +08002179
2180 *out_len = 6;
Jerry Yu23f7a6f2022-04-23 15:16:45 +08002181
Gilles Peskine449bd832023-01-11 14:50:10 +01002182 mbedtls_ssl_tls13_set_hs_sent_ext_mask(ssl, MBEDTLS_TLS_EXT_KEY_SHARE);
Jerry Yub95dd362022-11-08 21:19:34 +08002183
Gilles Peskine449bd832023-01-11 14:50:10 +01002184 return 0;
Jerry Yu23f7a6f2022-04-23 15:16:45 +08002185}
Jerry Yu3bf2c642022-03-30 22:02:12 +08002186
2187/*
2188 * Structure of ServerHello message:
2189 *
2190 * struct {
2191 * ProtocolVersion legacy_version = 0x0303; // TLS v1.2
2192 * Random random;
2193 * opaque legacy_session_id_echo<0..32>;
2194 * CipherSuite cipher_suite;
2195 * uint8 legacy_compression_method = 0;
2196 * Extension extensions<6..2^16-1>;
2197 * } ServerHello;
2198 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002199MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002200static int ssl_tls13_write_server_hello_body(mbedtls_ssl_context *ssl,
2201 unsigned char *buf,
2202 unsigned char *end,
2203 size_t *out_len,
2204 int is_hrr)
Jerry Yu56404d72022-03-30 17:36:13 +08002205{
Jerry Yu955ddd72022-04-22 22:27:33 +08002206 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yu3bf2c642022-03-30 22:02:12 +08002207 unsigned char *p = buf;
Jerry Yud9436a12022-04-20 22:28:09 +08002208 unsigned char *p_extensions_len;
Jerry Yufbe3e642022-04-25 19:31:51 +08002209 size_t output_len;
Jerry Yu3bf2c642022-03-30 22:02:12 +08002210
2211 *out_len = 0;
Jerry Yu50e00e32022-10-31 14:45:01 +08002212 ssl->handshake->sent_extensions = MBEDTLS_SSL_EXT_MASK_NONE;
Jerry Yu3bf2c642022-03-30 22:02:12 +08002213
Jerry Yucfc04b32022-04-21 09:31:58 +08002214 /* ...
2215 * ProtocolVersion legacy_version = 0x0303; // TLS 1.2
2216 * ...
2217 * with ProtocolVersion defined as:
2218 * uint16 ProtocolVersion;
Jerry Yu3bf2c642022-03-30 22:02:12 +08002219 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002220 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 2);
2221 MBEDTLS_PUT_UINT16_BE(0x0303, p, 0);
Jerry Yu3bf2c642022-03-30 22:02:12 +08002222 p += 2;
2223
Jerry Yu1c3e6882022-04-20 21:23:40 +08002224 /* ...
2225 * Random random;
2226 * ...
Jerry Yucfc04b32022-04-21 09:31:58 +08002227 * with Random defined as:
2228 * opaque Random[MBEDTLS_SERVER_HELLO_RANDOM_LEN];
Jerry Yu1c3e6882022-04-20 21:23:40 +08002229 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002230 MBEDTLS_SSL_CHK_BUF_PTR(p, end, MBEDTLS_SERVER_HELLO_RANDOM_LEN);
2231 if (is_hrr) {
2232 memcpy(p, mbedtls_ssl_tls13_hello_retry_request_magic,
2233 MBEDTLS_SERVER_HELLO_RANDOM_LEN);
2234 } else {
2235 memcpy(p, &ssl->handshake->randbytes[MBEDTLS_CLIENT_HELLO_RANDOM_LEN],
2236 MBEDTLS_SERVER_HELLO_RANDOM_LEN);
Jerry Yu23f7a6f2022-04-23 15:16:45 +08002237 }
Gilles Peskine449bd832023-01-11 14:50:10 +01002238 MBEDTLS_SSL_DEBUG_BUF(3, "server hello, random bytes",
2239 p, MBEDTLS_SERVER_HELLO_RANDOM_LEN);
Jerry Yu3bf2c642022-03-30 22:02:12 +08002240 p += MBEDTLS_SERVER_HELLO_RANDOM_LEN;
2241
Jerry Yucfc04b32022-04-21 09:31:58 +08002242 /* ...
2243 * opaque legacy_session_id_echo<0..32>;
2244 * ...
Jerry Yu3bf2c642022-03-30 22:02:12 +08002245 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002246 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 1 + ssl->session_negotiate->id_len);
2247 *p++ = (unsigned char) ssl->session_negotiate->id_len;
2248 if (ssl->session_negotiate->id_len > 0) {
2249 memcpy(p, &ssl->session_negotiate->id[0],
2250 ssl->session_negotiate->id_len);
Jerry Yu3bf2c642022-03-30 22:02:12 +08002251 p += ssl->session_negotiate->id_len;
Jerry Yu955ddd72022-04-22 22:27:33 +08002252
Gilles Peskine449bd832023-01-11 14:50:10 +01002253 MBEDTLS_SSL_DEBUG_BUF(3, "session id", ssl->session_negotiate->id,
2254 ssl->session_negotiate->id_len);
Jerry Yu3bf2c642022-03-30 22:02:12 +08002255 }
2256
Jerry Yucfc04b32022-04-21 09:31:58 +08002257 /* ...
2258 * CipherSuite cipher_suite;
2259 * ...
2260 * with CipherSuite defined as:
2261 * uint8 CipherSuite[2];
Jerry Yu3bf2c642022-03-30 22:02:12 +08002262 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002263 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 2);
2264 MBEDTLS_PUT_UINT16_BE(ssl->session_negotiate->ciphersuite, p, 0);
Jerry Yu3bf2c642022-03-30 22:02:12 +08002265 p += 2;
Gilles Peskine449bd832023-01-11 14:50:10 +01002266 MBEDTLS_SSL_DEBUG_MSG(3,
2267 ("server hello, chosen ciphersuite: %s ( id=%d )",
2268 mbedtls_ssl_get_ciphersuite_name(
2269 ssl->session_negotiate->ciphersuite),
2270 ssl->session_negotiate->ciphersuite));
Jerry Yu3bf2c642022-03-30 22:02:12 +08002271
Jerry Yucfc04b32022-04-21 09:31:58 +08002272 /* ...
2273 * uint8 legacy_compression_method = 0;
2274 * ...
2275 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002276 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 1);
Thomas Daubney31e03a82022-07-25 15:59:25 +01002277 *p++ = MBEDTLS_SSL_COMPRESS_NULL;
Jerry Yu3bf2c642022-03-30 22:02:12 +08002278
Jerry Yucfc04b32022-04-21 09:31:58 +08002279 /* ...
2280 * Extension extensions<6..2^16-1>;
2281 * ...
2282 * struct {
2283 * ExtensionType extension_type; (2 bytes)
2284 * opaque extension_data<0..2^16-1>;
2285 * } Extension;
2286 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002287 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 2);
Jerry Yud9436a12022-04-20 22:28:09 +08002288 p_extensions_len = p;
Jerry Yu3bf2c642022-03-30 22:02:12 +08002289 p += 2;
2290
Gilles Peskine449bd832023-01-11 14:50:10 +01002291 if ((ret = ssl_tls13_write_server_hello_supported_versions_ext(
2292 ssl, p, end, &output_len)) != 0) {
Jerry Yu955ddd72022-04-22 22:27:33 +08002293 MBEDTLS_SSL_DEBUG_RET(
Gilles Peskine449bd832023-01-11 14:50:10 +01002294 1, "ssl_tls13_write_server_hello_supported_versions_ext", ret);
2295 return ret;
Jerry Yu3bf2c642022-03-30 22:02:12 +08002296 }
2297 p += output_len;
2298
Gilles Peskine449bd832023-01-11 14:50:10 +01002299 if (mbedtls_ssl_tls13_key_exchange_mode_with_ephemeral(ssl)) {
2300 if (is_hrr) {
2301 ret = ssl_tls13_write_hrr_key_share_ext(ssl, p, end, &output_len);
2302 } else {
2303 ret = ssl_tls13_write_key_share_ext(ssl, p, end, &output_len);
2304 }
2305 if (ret != 0) {
2306 return ret;
2307 }
Jerry Yu3bf2c642022-03-30 22:02:12 +08002308 p += output_len;
2309 }
Jerry Yu3bf2c642022-03-30 22:02:12 +08002310
Ronald Cron41a443a2022-10-04 16:38:25 +02002311#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01002312 if (!is_hrr && mbedtls_ssl_tls13_key_exchange_mode_with_psk(ssl)) {
2313 ret = ssl_tls13_write_server_pre_shared_key_ext(ssl, p, end, &output_len);
2314 if (ret != 0) {
2315 MBEDTLS_SSL_DEBUG_RET(1, "ssl_tls13_write_server_pre_shared_key_ext",
2316 ret);
2317 return ret;
Jerry Yu032b15ce2022-07-11 06:10:03 +00002318 }
2319 p += output_len;
2320 }
Ronald Cron41a443a2022-10-04 16:38:25 +02002321#endif
Jerry Yu032b15ce2022-07-11 06:10:03 +00002322
Gilles Peskine449bd832023-01-11 14:50:10 +01002323 MBEDTLS_PUT_UINT16_BE(p - p_extensions_len - 2, p_extensions_len, 0);
Jerry Yu3bf2c642022-03-30 22:02:12 +08002324
Gilles Peskine449bd832023-01-11 14:50:10 +01002325 MBEDTLS_SSL_DEBUG_BUF(4, "server hello extensions",
2326 p_extensions_len, p - p_extensions_len);
Jerry Yu3bf2c642022-03-30 22:02:12 +08002327
Jerry Yud9436a12022-04-20 22:28:09 +08002328 *out_len = p - buf;
Jerry Yu3bf2c642022-03-30 22:02:12 +08002329
Gilles Peskine449bd832023-01-11 14:50:10 +01002330 MBEDTLS_SSL_DEBUG_BUF(3, "server hello", buf, *out_len);
Jerry Yu3bf2c642022-03-30 22:02:12 +08002331
Jerry Yu7de2ff02022-11-08 21:43:46 +08002332 MBEDTLS_SSL_PRINT_EXTS(
Jerry Yu4b8f2f72022-10-31 13:31:22 +08002333 3, is_hrr ? MBEDTLS_SSL_TLS1_3_HS_HELLO_RETRY_REQUEST :
Gilles Peskine449bd832023-01-11 14:50:10 +01002334 MBEDTLS_SSL_HS_SERVER_HELLO,
2335 ssl->handshake->sent_extensions);
Jerry Yu4b8f2f72022-10-31 13:31:22 +08002336
Gilles Peskine449bd832023-01-11 14:50:10 +01002337 return ret;
Jerry Yu56404d72022-03-30 17:36:13 +08002338}
2339
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002340MBEDTLS_CHECK_RETURN_CRITICAL
Xiaokang Qian934ce6f2023-02-06 10:23:04 +00002341static int ssl_tls13_finalize_server_hello(mbedtls_ssl_context *ssl)
Jerry Yue110d252022-05-05 10:19:22 +08002342{
2343 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine449bd832023-01-11 14:50:10 +01002344 ret = mbedtls_ssl_tls13_compute_handshake_transform(ssl);
2345 if (ret != 0) {
2346 MBEDTLS_SSL_DEBUG_RET(1,
2347 "mbedtls_ssl_tls13_compute_handshake_transform",
2348 ret);
2349 return ret;
Jerry Yue110d252022-05-05 10:19:22 +08002350 }
2351
Gilles Peskine449bd832023-01-11 14:50:10 +01002352 return ret;
Jerry Yue110d252022-05-05 10:19:22 +08002353}
2354
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002355MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002356static int ssl_tls13_write_server_hello(mbedtls_ssl_context *ssl)
Jerry Yu5b64ae92022-03-30 17:15:02 +08002357{
Jerry Yu637a3f12022-04-20 21:37:58 +08002358 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yuf4b27e42022-03-30 17:32:21 +08002359 unsigned char *buf;
2360 size_t buf_len, msg_len;
2361
Gilles Peskine449bd832023-01-11 14:50:10 +01002362 MBEDTLS_SSL_DEBUG_MSG(2, ("=> write server hello"));
Jerry Yuf4b27e42022-03-30 17:32:21 +08002363
Gilles Peskine449bd832023-01-11 14:50:10 +01002364 MBEDTLS_SSL_PROC_CHK(ssl_tls13_prepare_server_hello(ssl));
Jerry Yuf4b27e42022-03-30 17:32:21 +08002365
Xiaokang Qian9f1747b2023-03-30 02:50:04 +00002366 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_start_handshake_msg(
2367 ssl, MBEDTLS_SSL_HS_SERVER_HELLO, &buf, &buf_len));
Jerry Yuf4b27e42022-03-30 17:32:21 +08002368
Gilles Peskine449bd832023-01-11 14:50:10 +01002369 MBEDTLS_SSL_PROC_CHK(ssl_tls13_write_server_hello_body(ssl, buf,
2370 buf + buf_len,
2371 &msg_len,
2372 0));
Jerry Yuf4b27e42022-03-30 17:32:21 +08002373
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +01002374 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_add_hs_msg_to_checksum(
Manuel Pégourié-Gonnard43cc1272023-02-06 11:48:19 +01002375 ssl, MBEDTLS_SSL_HS_SERVER_HELLO, buf, msg_len));
Jerry Yuf4b27e42022-03-30 17:32:21 +08002376
Gilles Peskine449bd832023-01-11 14:50:10 +01002377 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_finish_handshake_msg(
2378 ssl, buf_len, msg_len));
Jerry Yu637a3f12022-04-20 21:37:58 +08002379
Xiaokang Qian934ce6f2023-02-06 10:23:04 +00002380 MBEDTLS_SSL_PROC_CHK(ssl_tls13_finalize_server_hello(ssl));
Jerry Yue110d252022-05-05 10:19:22 +08002381
Gabor Mezei7b39bf12022-05-24 16:04:14 +02002382#if defined(MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE)
2383 /* The server sends a dummy change_cipher_spec record immediately
2384 * after its first handshake message. This may either be after
2385 * a ServerHello or a HelloRetryRequest.
2386 */
Gabor Mezei96ae9262022-06-28 11:45:18 +02002387 mbedtls_ssl_handshake_set_state(
Gilles Peskine449bd832023-01-11 14:50:10 +01002388 ssl, MBEDTLS_SSL_SERVER_CCS_AFTER_SERVER_HELLO);
Gabor Mezei7b39bf12022-05-24 16:04:14 +02002389#else
Gilles Peskine449bd832023-01-11 14:50:10 +01002390 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_ENCRYPTED_EXTENSIONS);
Gabor Mezei7b39bf12022-05-24 16:04:14 +02002391#endif /* MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE */
Jerry Yue110d252022-05-05 10:19:22 +08002392
Jerry Yuf4b27e42022-03-30 17:32:21 +08002393cleanup:
2394
Gilles Peskine449bd832023-01-11 14:50:10 +01002395 MBEDTLS_SSL_DEBUG_MSG(2, ("<= write server hello"));
2396 return ret;
Jerry Yu5b64ae92022-03-30 17:15:02 +08002397}
2398
Jerry Yu23d1a252022-05-12 18:08:59 +08002399
2400/*
2401 * Handler for MBEDTLS_SSL_HELLO_RETRY_REQUEST
2402 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002403MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002404static int ssl_tls13_prepare_hello_retry_request(mbedtls_ssl_context *ssl)
Jerry Yu23d1a252022-05-12 18:08:59 +08002405{
2406 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Ronald Cron5fbd2702024-02-14 10:03:36 +01002407 if (ssl->handshake->hello_retry_request_flag) {
Gilles Peskine449bd832023-01-11 14:50:10 +01002408 MBEDTLS_SSL_DEBUG_MSG(1, ("Too many HRRs"));
2409 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE,
2410 MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE);
2411 return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
Jerry Yu23d1a252022-05-12 18:08:59 +08002412 }
2413
2414 /*
2415 * Create stateless transcript hash for HRR
2416 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002417 MBEDTLS_SSL_DEBUG_MSG(4, ("Reset transcript for HRR"));
2418 ret = mbedtls_ssl_reset_transcript_for_hrr(ssl);
2419 if (ret != 0) {
2420 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_reset_transcript_for_hrr", ret);
2421 return ret;
Jerry Yu23d1a252022-05-12 18:08:59 +08002422 }
Gilles Peskine449bd832023-01-11 14:50:10 +01002423 mbedtls_ssl_session_reset_msg_layer(ssl, 0);
Jerry Yu23d1a252022-05-12 18:08:59 +08002424
Gilles Peskine449bd832023-01-11 14:50:10 +01002425 return 0;
Jerry Yu23d1a252022-05-12 18:08:59 +08002426}
2427
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002428MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002429static int ssl_tls13_write_hello_retry_request(mbedtls_ssl_context *ssl)
Jerry Yu23d1a252022-05-12 18:08:59 +08002430{
2431 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
2432 unsigned char *buf;
2433 size_t buf_len, msg_len;
2434
Gilles Peskine449bd832023-01-11 14:50:10 +01002435 MBEDTLS_SSL_DEBUG_MSG(2, ("=> write hello retry request"));
Jerry Yu23d1a252022-05-12 18:08:59 +08002436
Gilles Peskine449bd832023-01-11 14:50:10 +01002437 MBEDTLS_SSL_PROC_CHK(ssl_tls13_prepare_hello_retry_request(ssl));
Jerry Yu23d1a252022-05-12 18:08:59 +08002438
Gilles Peskine449bd832023-01-11 14:50:10 +01002439 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_start_handshake_msg(
2440 ssl, MBEDTLS_SSL_HS_SERVER_HELLO,
2441 &buf, &buf_len));
Jerry Yu23d1a252022-05-12 18:08:59 +08002442
Gilles Peskine449bd832023-01-11 14:50:10 +01002443 MBEDTLS_SSL_PROC_CHK(ssl_tls13_write_server_hello_body(ssl, buf,
2444 buf + buf_len,
2445 &msg_len,
2446 1));
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +01002447 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_add_hs_msg_to_checksum(
Manuel Pégourié-Gonnard43cc1272023-02-06 11:48:19 +01002448 ssl, MBEDTLS_SSL_HS_SERVER_HELLO, buf, msg_len));
Jerry Yu23d1a252022-05-12 18:08:59 +08002449
2450
Gilles Peskine449bd832023-01-11 14:50:10 +01002451 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_finish_handshake_msg(ssl, buf_len,
2452 msg_len));
Jerry Yu23d1a252022-05-12 18:08:59 +08002453
Ronald Cron5fbd2702024-02-14 10:03:36 +01002454 ssl->handshake->hello_retry_request_flag = 1;
Jerry Yu23d1a252022-05-12 18:08:59 +08002455
Gabor Mezei7b39bf12022-05-24 16:04:14 +02002456#if defined(MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE)
2457 /* The server sends a dummy change_cipher_spec record immediately
2458 * after its first handshake message. This may either be after
2459 * a ServerHello or a HelloRetryRequest.
2460 */
Gabor Mezei96ae9262022-06-28 11:45:18 +02002461 mbedtls_ssl_handshake_set_state(
Gilles Peskine449bd832023-01-11 14:50:10 +01002462 ssl, MBEDTLS_SSL_SERVER_CCS_AFTER_HELLO_RETRY_REQUEST);
Gabor Mezei7b39bf12022-05-24 16:04:14 +02002463#else
Gilles Peskine449bd832023-01-11 14:50:10 +01002464 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_CLIENT_HELLO);
Gabor Mezei7b39bf12022-05-24 16:04:14 +02002465#endif /* MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE */
Jerry Yu23d1a252022-05-12 18:08:59 +08002466
2467cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +01002468 MBEDTLS_SSL_DEBUG_MSG(2, ("<= write hello retry request"));
2469 return ret;
Jerry Yu23d1a252022-05-12 18:08:59 +08002470}
2471
Jerry Yu5b64ae92022-03-30 17:15:02 +08002472/*
Jerry Yu4d3841a2022-04-16 12:37:19 +08002473 * Handler for MBEDTLS_SSL_ENCRYPTED_EXTENSIONS
2474 */
Jerry Yu4d3841a2022-04-16 12:37:19 +08002475
2476/*
2477 * struct {
2478 * Extension extensions<0..2 ^ 16 - 1>;
2479 * } EncryptedExtensions;
2480 *
2481 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002482MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002483static int ssl_tls13_write_encrypted_extensions_body(mbedtls_ssl_context *ssl,
2484 unsigned char *buf,
2485 unsigned char *end,
2486 size_t *out_len)
Jerry Yu4d3841a2022-04-16 12:37:19 +08002487{
XiaokangQianacb39922022-06-17 10:18:48 +00002488 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yu4d3841a2022-04-16 12:37:19 +08002489 unsigned char *p = buf;
2490 size_t extensions_len = 0;
Jerry Yu8937eb42022-05-03 12:12:14 +08002491 unsigned char *p_extensions_len;
XiaokangQianacb39922022-06-17 10:18:48 +00002492 size_t output_len;
Jerry Yu9da5e5a2022-05-03 15:46:09 +08002493
Jerry Yu4d3841a2022-04-16 12:37:19 +08002494 *out_len = 0;
2495
Gilles Peskine449bd832023-01-11 14:50:10 +01002496 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 2);
Jerry Yu8937eb42022-05-03 12:12:14 +08002497 p_extensions_len = p;
Jerry Yu4d3841a2022-04-16 12:37:19 +08002498 p += 2;
2499
Jerry Yu8937eb42022-05-03 12:12:14 +08002500 ((void) ssl);
XiaokangQianacb39922022-06-17 10:18:48 +00002501 ((void) ret);
2502 ((void) output_len);
2503
2504#if defined(MBEDTLS_SSL_ALPN)
Gilles Peskine449bd832023-01-11 14:50:10 +01002505 ret = mbedtls_ssl_write_alpn_ext(ssl, p, end, &output_len);
2506 if (ret != 0) {
2507 return ret;
2508 }
XiaokangQian95d5f542022-06-24 02:29:26 +00002509 p += output_len;
XiaokangQianacb39922022-06-17 10:18:48 +00002510#endif /* MBEDTLS_SSL_ALPN */
Jerry Yu4d3841a2022-04-16 12:37:19 +08002511
Jerry Yu71c14f12022-12-12 11:10:35 +08002512#if defined(MBEDTLS_SSL_EARLY_DATA)
Ronald Cron78a38f62024-02-01 18:30:31 +01002513 if (ssl->handshake->early_data_accepted) {
Jerry Yu52335392023-11-23 18:06:06 +08002514 ret = mbedtls_ssl_tls13_write_early_data_ext(
Jerry Yuc59c5862023-12-05 10:40:49 +08002515 ssl, 0, p, end, &output_len);
Jerry Yu71c14f12022-12-12 11:10:35 +08002516 if (ret != 0) {
2517 return ret;
2518 }
2519 p += output_len;
2520 }
2521#endif /* MBEDTLS_SSL_EARLY_DATA */
2522
Waleed Elmelegy47d29462024-01-03 17:31:52 +00002523#if defined(MBEDTLS_SSL_RECORD_SIZE_LIMIT)
Waleed Elmelegyfbe42742024-01-05 18:11:10 +00002524 if (ssl->handshake->received_extensions & MBEDTLS_SSL_EXT_MASK(RECORD_SIZE_LIMIT)) {
Waleed Elmelegyd2fc90e2024-01-04 18:04:53 +00002525 ret = mbedtls_ssl_tls13_write_record_size_limit_ext(
2526 ssl, p, end, &output_len);
2527 if (ret != 0) {
2528 return ret;
2529 }
2530 p += output_len;
Waleed Elmelegy47d29462024-01-03 17:31:52 +00002531 }
Waleed Elmelegy47d29462024-01-03 17:31:52 +00002532#endif
2533
Gilles Peskine449bd832023-01-11 14:50:10 +01002534 extensions_len = (p - p_extensions_len) - 2;
2535 MBEDTLS_PUT_UINT16_BE(extensions_len, p_extensions_len, 0);
Jerry Yu8937eb42022-05-03 12:12:14 +08002536
2537 *out_len = p - buf;
Jerry Yu4d3841a2022-04-16 12:37:19 +08002538
Gilles Peskine449bd832023-01-11 14:50:10 +01002539 MBEDTLS_SSL_DEBUG_BUF(4, "encrypted extensions", buf, *out_len);
Jerry Yu4d3841a2022-04-16 12:37:19 +08002540
Jerry Yu7de2ff02022-11-08 21:43:46 +08002541 MBEDTLS_SSL_PRINT_EXTS(
Gilles Peskine449bd832023-01-11 14:50:10 +01002542 3, MBEDTLS_SSL_HS_ENCRYPTED_EXTENSIONS, ssl->handshake->sent_extensions);
Jerry Yu4b8f2f72022-10-31 13:31:22 +08002543
Gilles Peskine449bd832023-01-11 14:50:10 +01002544 return 0;
Jerry Yu4d3841a2022-04-16 12:37:19 +08002545}
2546
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002547MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002548static int ssl_tls13_write_encrypted_extensions(mbedtls_ssl_context *ssl)
Jerry Yu4d3841a2022-04-16 12:37:19 +08002549{
2550 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
2551 unsigned char *buf;
Jerry Yu39730a72022-05-03 12:14:04 +08002552 size_t buf_len, msg_len;
Jerry Yu4d3841a2022-04-16 12:37:19 +08002553
Gilles Peskine449bd832023-01-11 14:50:10 +01002554 mbedtls_ssl_set_outbound_transform(ssl,
2555 ssl->handshake->transform_handshake);
Gabor Mezei54719122022-06-28 11:34:56 +02002556 MBEDTLS_SSL_DEBUG_MSG(
Gilles Peskine449bd832023-01-11 14:50:10 +01002557 3, ("switching to handshake transform for outbound data"));
Gabor Mezei54719122022-06-28 11:34:56 +02002558
Gilles Peskine449bd832023-01-11 14:50:10 +01002559 MBEDTLS_SSL_DEBUG_MSG(2, ("=> write encrypted extensions"));
Jerry Yu4d3841a2022-04-16 12:37:19 +08002560
Xiaokang Qian9f1747b2023-03-30 02:50:04 +00002561 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_start_handshake_msg(
2562 ssl, MBEDTLS_SSL_HS_ENCRYPTED_EXTENSIONS,
2563 &buf, &buf_len));
Jerry Yu4d3841a2022-04-16 12:37:19 +08002564
Gilles Peskine449bd832023-01-11 14:50:10 +01002565 MBEDTLS_SSL_PROC_CHK(ssl_tls13_write_encrypted_extensions_body(
2566 ssl, buf, buf + buf_len, &msg_len));
Jerry Yu4d3841a2022-04-16 12:37:19 +08002567
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +01002568 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_add_hs_msg_to_checksum(
Xiaokang Qian9f1747b2023-03-30 02:50:04 +00002569 ssl, MBEDTLS_SSL_HS_ENCRYPTED_EXTENSIONS,
2570 buf, msg_len));
Jerry Yu4d3841a2022-04-16 12:37:19 +08002571
Gilles Peskine449bd832023-01-11 14:50:10 +01002572 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_finish_handshake_msg(
2573 ssl, buf_len, msg_len));
Jerry Yu4d3841a2022-04-16 12:37:19 +08002574
Ronald Cron928cbd32022-10-04 16:14:26 +02002575#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01002576 if (mbedtls_ssl_tls13_key_exchange_mode_with_psk(ssl)) {
2577 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_SERVER_FINISHED);
2578 } else {
2579 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_CERTIFICATE_REQUEST);
2580 }
Jerry Yu8937eb42022-05-03 12:12:14 +08002581#else
Gilles Peskine449bd832023-01-11 14:50:10 +01002582 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_SERVER_FINISHED);
Jerry Yu8937eb42022-05-03 12:12:14 +08002583#endif
2584
Jerry Yu4d3841a2022-04-16 12:37:19 +08002585cleanup:
2586
Gilles Peskine449bd832023-01-11 14:50:10 +01002587 MBEDTLS_SSL_DEBUG_MSG(2, ("<= write encrypted extensions"));
2588 return ret;
Jerry Yu4d3841a2022-04-16 12:37:19 +08002589}
2590
Ronald Cron928cbd32022-10-04 16:14:26 +02002591#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED)
XiaokangQiana987e1d2022-05-07 01:25:58 +00002592#define SSL_CERTIFICATE_REQUEST_SEND_REQUEST 0
2593#define SSL_CERTIFICATE_REQUEST_SKIP 1
2594/* Coordination:
2595 * Check whether a CertificateRequest message should be written.
2596 * Returns a negative code on failure, or
2597 * - SSL_CERTIFICATE_REQUEST_SEND_REQUEST
2598 * - SSL_CERTIFICATE_REQUEST_SKIP
2599 * indicating if the writing of the CertificateRequest
2600 * should be skipped or not.
2601 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002602MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002603static int ssl_tls13_certificate_request_coordinate(mbedtls_ssl_context *ssl)
XiaokangQiana987e1d2022-05-07 01:25:58 +00002604{
2605 int authmode;
2606
XiaokangQian40a35232022-05-07 09:02:40 +00002607#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Gilles Peskine449bd832023-01-11 14:50:10 +01002608 if (ssl->handshake->sni_authmode != MBEDTLS_SSL_VERIFY_UNSET) {
XiaokangQian40a35232022-05-07 09:02:40 +00002609 authmode = ssl->handshake->sni_authmode;
Gilles Peskine449bd832023-01-11 14:50:10 +01002610 } else
XiaokangQian40a35232022-05-07 09:02:40 +00002611#endif
XiaokangQiana987e1d2022-05-07 01:25:58 +00002612 authmode = ssl->conf->authmode;
2613
Gilles Peskine449bd832023-01-11 14:50:10 +01002614 if (authmode == MBEDTLS_SSL_VERIFY_NONE) {
Ronald Croneac00ad2022-09-13 10:16:31 +02002615 ssl->session_negotiate->verify_result = MBEDTLS_X509_BADCERT_SKIP_VERIFY;
Gilles Peskine449bd832023-01-11 14:50:10 +01002616 return SSL_CERTIFICATE_REQUEST_SKIP;
Ronald Croneac00ad2022-09-13 10:16:31 +02002617 }
XiaokangQiana987e1d2022-05-07 01:25:58 +00002618
XiaokangQianc3017f62022-05-13 05:55:41 +00002619 ssl->handshake->certificate_request_sent = 1;
XiaokangQian189ded22022-05-10 08:12:17 +00002620
Gilles Peskine449bd832023-01-11 14:50:10 +01002621 return SSL_CERTIFICATE_REQUEST_SEND_REQUEST;
XiaokangQiana987e1d2022-05-07 01:25:58 +00002622}
2623
XiaokangQiancec9ae62022-05-06 07:28:50 +00002624/*
2625 * struct {
2626 * opaque certificate_request_context<0..2^8-1>;
2627 * Extension extensions<2..2^16-1>;
2628 * } CertificateRequest;
2629 *
2630 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002631MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002632static int ssl_tls13_write_certificate_request_body(mbedtls_ssl_context *ssl,
2633 unsigned char *buf,
2634 const unsigned char *end,
2635 size_t *out_len)
XiaokangQiancec9ae62022-05-06 07:28:50 +00002636{
2637 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
2638 unsigned char *p = buf;
XiaokangQianec6efb92022-05-06 09:53:10 +00002639 size_t output_len = 0;
XiaokangQiancec9ae62022-05-06 07:28:50 +00002640 unsigned char *p_extensions_len;
2641
2642 *out_len = 0;
2643
2644 /* Check if we have enough space:
2645 * - certificate_request_context (1 byte)
2646 * - extensions length (2 bytes)
2647 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002648 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 3);
XiaokangQiancec9ae62022-05-06 07:28:50 +00002649
2650 /*
2651 * Write certificate_request_context
2652 */
2653 /*
2654 * We use a zero length context for the normal handshake
2655 * messages. For post-authentication handshake messages
2656 * this request context would be set to a non-zero value.
2657 */
2658 *p++ = 0x0;
2659
2660 /*
2661 * Write extensions
2662 */
2663 /* The extensions must contain the signature_algorithms. */
2664 p_extensions_len = p;
2665 p += 2;
Gilles Peskine449bd832023-01-11 14:50:10 +01002666 ret = mbedtls_ssl_write_sig_alg_ext(ssl, p, end, &output_len);
2667 if (ret != 0) {
2668 return ret;
2669 }
XiaokangQiancec9ae62022-05-06 07:28:50 +00002670
XiaokangQianec6efb92022-05-06 09:53:10 +00002671 p += output_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01002672 MBEDTLS_PUT_UINT16_BE(p - p_extensions_len - 2, p_extensions_len, 0);
XiaokangQiancec9ae62022-05-06 07:28:50 +00002673
2674 *out_len = p - buf;
2675
Jerry Yu7de2ff02022-11-08 21:43:46 +08002676 MBEDTLS_SSL_PRINT_EXTS(
Gilles Peskine449bd832023-01-11 14:50:10 +01002677 3, MBEDTLS_SSL_HS_CERTIFICATE_REQUEST, ssl->handshake->sent_extensions);
Jerry Yu4b8f2f72022-10-31 13:31:22 +08002678
Gilles Peskine449bd832023-01-11 14:50:10 +01002679 return 0;
XiaokangQiancec9ae62022-05-06 07:28:50 +00002680}
2681
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002682MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002683static int ssl_tls13_write_certificate_request(mbedtls_ssl_context *ssl)
XiaokangQiancec9ae62022-05-06 07:28:50 +00002684{
2685 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
2686
Gilles Peskine449bd832023-01-11 14:50:10 +01002687 MBEDTLS_SSL_DEBUG_MSG(2, ("=> write certificate request"));
XiaokangQiancec9ae62022-05-06 07:28:50 +00002688
Gilles Peskine449bd832023-01-11 14:50:10 +01002689 MBEDTLS_SSL_PROC_CHK_NEG(ssl_tls13_certificate_request_coordinate(ssl));
XiaokangQiancec9ae62022-05-06 07:28:50 +00002690
Gilles Peskine449bd832023-01-11 14:50:10 +01002691 if (ret == SSL_CERTIFICATE_REQUEST_SEND_REQUEST) {
XiaokangQiancec9ae62022-05-06 07:28:50 +00002692 unsigned char *buf;
2693 size_t buf_len, msg_len;
2694
Xiaokang Qian9f1747b2023-03-30 02:50:04 +00002695 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_start_handshake_msg(
2696 ssl, MBEDTLS_SSL_HS_CERTIFICATE_REQUEST,
2697 &buf, &buf_len));
XiaokangQiancec9ae62022-05-06 07:28:50 +00002698
Gilles Peskine449bd832023-01-11 14:50:10 +01002699 MBEDTLS_SSL_PROC_CHK(ssl_tls13_write_certificate_request_body(
2700 ssl, buf, buf + buf_len, &msg_len));
XiaokangQiancec9ae62022-05-06 07:28:50 +00002701
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +01002702 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_add_hs_msg_to_checksum(
Xiaokang Qian9f1747b2023-03-30 02:50:04 +00002703 ssl, MBEDTLS_SSL_HS_CERTIFICATE_REQUEST,
2704 buf, msg_len));
XiaokangQiancec9ae62022-05-06 07:28:50 +00002705
Gilles Peskine449bd832023-01-11 14:50:10 +01002706 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_finish_handshake_msg(
2707 ssl, buf_len, msg_len));
2708 } else if (ret == SSL_CERTIFICATE_REQUEST_SKIP) {
2709 MBEDTLS_SSL_DEBUG_MSG(2, ("<= skip write certificate request"));
XiaokangQiancec9ae62022-05-06 07:28:50 +00002710 ret = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01002711 } else {
2712 MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
XiaokangQiancec9ae62022-05-06 07:28:50 +00002713 ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
2714 goto cleanup;
2715 }
2716
Gilles Peskine449bd832023-01-11 14:50:10 +01002717 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_SERVER_CERTIFICATE);
XiaokangQiancec9ae62022-05-06 07:28:50 +00002718cleanup:
2719
Gilles Peskine449bd832023-01-11 14:50:10 +01002720 MBEDTLS_SSL_DEBUG_MSG(2, ("<= write certificate request"));
2721 return ret;
XiaokangQiancec9ae62022-05-06 07:28:50 +00002722}
XiaokangQiancec9ae62022-05-06 07:28:50 +00002723
Jerry Yu4d3841a2022-04-16 12:37:19 +08002724/*
Jerry Yuc6e6dbf2022-04-16 19:42:57 +08002725 * Handler for MBEDTLS_SSL_SERVER_CERTIFICATE
Jerry Yu83da34e2022-04-16 13:59:52 +08002726 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002727MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002728static int ssl_tls13_write_server_certificate(mbedtls_ssl_context *ssl)
Jerry Yu83da34e2022-04-16 13:59:52 +08002729{
Jerry Yu5a26f302022-05-10 20:46:40 +08002730 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
XiaokangQianfb665a82022-06-15 03:57:21 +00002731
2732#if defined(MBEDTLS_X509_CRT_PARSE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01002733 if ((ssl_tls13_pick_key_cert(ssl) != 0) ||
2734 mbedtls_ssl_own_cert(ssl) == NULL) {
2735 MBEDTLS_SSL_DEBUG_MSG(2, ("No certificate available."));
2736 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE,
2737 MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE);
2738 return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
Jerry Yu5a26f302022-05-10 20:46:40 +08002739 }
XiaokangQianfb665a82022-06-15 03:57:21 +00002740#endif /* MBEDTLS_X509_CRT_PARSE_C */
Jerry Yu5a26f302022-05-10 20:46:40 +08002741
Gilles Peskine449bd832023-01-11 14:50:10 +01002742 ret = mbedtls_ssl_tls13_write_certificate(ssl);
2743 if (ret != 0) {
2744 return ret;
2745 }
2746 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_CERTIFICATE_VERIFY);
2747 return 0;
Jerry Yu83da34e2022-04-16 13:59:52 +08002748}
2749
2750/*
Jerry Yuc6e6dbf2022-04-16 19:42:57 +08002751 * Handler for MBEDTLS_SSL_CERTIFICATE_VERIFY
Jerry Yu83da34e2022-04-16 13:59:52 +08002752 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002753MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002754static int ssl_tls13_write_certificate_verify(mbedtls_ssl_context *ssl)
Jerry Yu83da34e2022-04-16 13:59:52 +08002755{
Gilles Peskine449bd832023-01-11 14:50:10 +01002756 int ret = mbedtls_ssl_tls13_write_certificate_verify(ssl);
2757 if (ret != 0) {
2758 return ret;
2759 }
2760 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_SERVER_FINISHED);
2761 return 0;
Jerry Yu83da34e2022-04-16 13:59:52 +08002762}
Ronald Cron928cbd32022-10-04 16:14:26 +02002763#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED */
Jerry Yu83da34e2022-04-16 13:59:52 +08002764
Jerry Yu9b72e392023-12-01 16:27:08 +08002765/*
2766 * RFC 8446 section A.2
2767 *
2768 * | Send ServerHello
2769 * | K_send = handshake
2770 * | Send EncryptedExtensions
2771 * | [Send CertificateRequest]
2772 * Can send | [Send Certificate + CertificateVerify]
2773 * app data | Send Finished
2774 * after --> | K_send = application
2775 * here +--------+--------+
2776 * No 0-RTT | | 0-RTT
2777 * | |
2778 * K_recv = handshake | | K_recv = early data
2779 * [Skip decrypt errors] | +------> WAIT_EOED -+
2780 * | | Recv | | Recv EndOfEarlyData
2781 * | | early data | | K_recv = handshake
2782 * | +------------+ |
2783 * | |
2784 * +> WAIT_FLIGHT2 <--------+
2785 * |
2786 * +--------+--------+
2787 * No auth | | Client auth
2788 * | |
2789 * | v
2790 * | WAIT_CERT
2791 * | Recv | | Recv Certificate
2792 * | empty | v
2793 * | Certificate | WAIT_CV
2794 * | | | Recv
2795 * | v | CertificateVerify
2796 * +-> WAIT_FINISHED <---+
2797 * | Recv Finished
2798 *
2799 *
2800 * The following function handles the state changes after WAIT_FLIGHT2 in the
Jerry Yu3be85072023-12-04 09:58:54 +08002801 * above diagram. We are not going to receive early data related messages
2802 * anymore, prepare to receive the first handshake message of the client
2803 * second flight.
Jerry Yu9b72e392023-12-01 16:27:08 +08002804 */
Jerry Yu3be85072023-12-04 09:58:54 +08002805static void ssl_tls13_prepare_for_handshake_second_flight(
2806 mbedtls_ssl_context *ssl)
Jerry Yu9b72e392023-12-01 16:27:08 +08002807{
Jerry Yu9b72e392023-12-01 16:27:08 +08002808 if (ssl->handshake->certificate_request_sent) {
2809 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_CLIENT_CERTIFICATE);
2810 } else {
Jerry Yu42020fb2023-12-05 17:35:53 +08002811 MBEDTLS_SSL_DEBUG_MSG(2, ("skip parse certificate"));
Jerry Yuebb1b1d2023-12-05 11:02:15 +08002812 MBEDTLS_SSL_DEBUG_MSG(2, ("skip parse certificate verify"));
Jerry Yuebb1b1d2023-12-05 11:02:15 +08002813
Jerry Yu9b72e392023-12-01 16:27:08 +08002814 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_CLIENT_FINISHED);
2815 }
Jerry Yu9b72e392023-12-01 16:27:08 +08002816}
2817
Jerry Yu83da34e2022-04-16 13:59:52 +08002818/*
Jerry Yud6e253d2022-05-18 13:59:24 +08002819 * Handler for MBEDTLS_SSL_SERVER_FINISHED
Jerry Yu69dd8d42022-04-16 12:51:26 +08002820 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002821MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002822static int ssl_tls13_write_server_finished(mbedtls_ssl_context *ssl)
Jerry Yu69dd8d42022-04-16 12:51:26 +08002823{
Jerry Yud6e253d2022-05-18 13:59:24 +08002824 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yu27bdc7c2022-04-16 13:33:27 +08002825
Gilles Peskine449bd832023-01-11 14:50:10 +01002826 ret = mbedtls_ssl_tls13_write_finished_message(ssl);
2827 if (ret != 0) {
2828 return ret;
2829 }
Jerry Yu27bdc7c2022-04-16 13:33:27 +08002830
Gilles Peskine449bd832023-01-11 14:50:10 +01002831 ret = mbedtls_ssl_tls13_compute_application_transform(ssl);
2832 if (ret != 0) {
Jerry Yue3d67cb2022-05-19 15:33:10 +08002833 MBEDTLS_SSL_PEND_FATAL_ALERT(
Gilles Peskine449bd832023-01-11 14:50:10 +01002834 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE,
2835 MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE);
2836 return ret;
Jerry Yue3d67cb2022-05-19 15:33:10 +08002837 }
XiaokangQianc3017f62022-05-13 05:55:41 +00002838
Jerry Yu87b5ed42022-12-12 13:07:07 +08002839#if defined(MBEDTLS_SSL_EARLY_DATA)
Ronald Cron78a38f62024-02-01 18:30:31 +01002840 if (ssl->handshake->early_data_accepted) {
Jerry Yu0af63dc2023-12-01 17:14:51 +08002841 /* See RFC 8446 section A.2 for more information */
Jerry Yu87b5ed42022-12-12 13:07:07 +08002842 MBEDTLS_SSL_DEBUG_MSG(
2843 1, ("Switch to early keys for inbound traffic. "
2844 "( K_recv = early data )"));
2845 mbedtls_ssl_set_inbound_transform(
2846 ssl, ssl->handshake->transform_earlydata);
2847 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_END_OF_EARLY_DATA);
2848 return 0;
2849 }
2850#endif /* MBEDTLS_SSL_EARLY_DATA */
Jerry Yu0af63dc2023-12-01 17:14:51 +08002851 MBEDTLS_SSL_DEBUG_MSG(
2852 1, ("Switch to handshake keys for inbound traffic "
2853 "( K_recv = handshake )"));
Gilles Peskine449bd832023-01-11 14:50:10 +01002854 mbedtls_ssl_set_inbound_transform(ssl, ssl->handshake->transform_handshake);
XiaokangQian189ded22022-05-10 08:12:17 +00002855
Jerry Yu3be85072023-12-04 09:58:54 +08002856 ssl_tls13_prepare_for_handshake_second_flight(ssl);
Jerry Yu7d8c3fe2022-12-12 12:59:44 +08002857
2858 return 0;
2859}
2860
Jerry Yu87b5ed42022-12-12 13:07:07 +08002861#if defined(MBEDTLS_SSL_EARLY_DATA)
2862/*
Jerry Yu59d420f2023-12-01 16:30:34 +08002863 * Handler for MBEDTLS_SSL_END_OF_EARLY_DATA
Jerry Yu87b5ed42022-12-12 13:07:07 +08002864 */
Jerry Yu3be85072023-12-04 09:58:54 +08002865#define SSL_GOT_END_OF_EARLY_DATA 0
Jerry Yub55f9eb2023-12-05 10:27:17 +08002866#define SSL_GOT_EARLY_DATA 1
Jerry Yud5c34962023-12-01 16:32:31 +08002867/* Coordination:
Jerry Yu3be85072023-12-04 09:58:54 +08002868 * Deals with the ambiguity of not knowing if the next message is an
2869 * EndOfEarlyData message or an application message containing early data.
Jerry Yud5c34962023-12-01 16:32:31 +08002870 * Returns a negative code on failure, or
Jerry Yu3be85072023-12-04 09:58:54 +08002871 * - SSL_GOT_END_OF_EARLY_DATA
Jerry Yub55f9eb2023-12-05 10:27:17 +08002872 * - SSL_GOT_EARLY_DATA
Jerry Yud5c34962023-12-01 16:32:31 +08002873 * indicating which message is received.
2874 */
2875MBEDTLS_CHECK_RETURN_CRITICAL
2876static int ssl_tls13_end_of_early_data_coordinate(mbedtls_ssl_context *ssl)
2877{
2878 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yub4ed4602023-12-01 16:34:00 +08002879
2880 if ((ret = mbedtls_ssl_read_record(ssl, 0)) != 0) {
2881 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_read_record", ret);
2882 return ret;
2883 }
2884 ssl->keep_current_message = 1;
2885
2886 if (ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
2887 ssl->in_msg[0] == MBEDTLS_SSL_HS_END_OF_EARLY_DATA) {
Jerry Yub55f9eb2023-12-05 10:27:17 +08002888 MBEDTLS_SSL_DEBUG_MSG(3, ("Received an end_of_early_data message."));
Jerry Yu3be85072023-12-04 09:58:54 +08002889 return SSL_GOT_END_OF_EARLY_DATA;
Jerry Yub4ed4602023-12-01 16:34:00 +08002890 }
2891
2892 if (ssl->in_msgtype == MBEDTLS_SSL_MSG_APPLICATION_DATA) {
Jerry Yub55f9eb2023-12-05 10:27:17 +08002893 MBEDTLS_SSL_DEBUG_MSG(3, ("Received early data"));
Jerry Yu6a5904d2023-12-06 17:11:12 +08002894 /* RFC 8446 section 4.6.1
2895 *
2896 * A server receiving more than max_early_data_size bytes of 0-RTT data
2897 * SHOULD terminate the connection with an "unexpected_message" alert.
2898 *
2899 * TODO: Add received data size check here.
2900 */
Ronald Croned7d4bf2024-01-31 07:55:19 +01002901 if (ssl->in_offt == NULL) {
2902 /* Set the reading pointer */
2903 ssl->in_offt = ssl->in_msg;
2904 }
Jerry Yub55f9eb2023-12-05 10:27:17 +08002905 return SSL_GOT_EARLY_DATA;
Jerry Yub4ed4602023-12-01 16:34:00 +08002906 }
2907
Jerry Yu7bb40a32023-12-04 10:04:15 +08002908 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE,
2909 MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE);
Jerry Yub4ed4602023-12-01 16:34:00 +08002910 return MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE;
Jerry Yud5c34962023-12-01 16:32:31 +08002911}
2912
2913MBEDTLS_CHECK_RETURN_CRITICAL
2914static int ssl_tls13_parse_end_of_early_data(mbedtls_ssl_context *ssl,
2915 const unsigned char *buf,
2916 const unsigned char *end)
2917{
Jerry Yu75c9ab72023-12-01 16:41:40 +08002918 /* RFC 8446 section 4.5
2919 *
2920 * struct {} EndOfEarlyData;
2921 */
Jerry Yufbf03992023-12-04 10:00:37 +08002922 if (buf != end) {
2923 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR,
2924 MBEDTLS_ERR_SSL_DECODE_ERROR);
2925 return MBEDTLS_ERR_SSL_DECODE_ERROR;
2926 }
2927 return 0;
Jerry Yud5c34962023-12-01 16:32:31 +08002928}
2929
Jerry Yud5c34962023-12-01 16:32:31 +08002930/*
2931 * RFC 8446 section A.2
2932 *
2933 * | Send ServerHello
2934 * | K_send = handshake
2935 * | Send EncryptedExtensions
2936 * | [Send CertificateRequest]
2937 * Can send | [Send Certificate + CertificateVerify]
2938 * app data | Send Finished
2939 * after --> | K_send = application
2940 * here +--------+--------+
2941 * No 0-RTT | | 0-RTT
2942 * | |
2943 * K_recv = handshake | | K_recv = early data
2944 * [Skip decrypt errors] | +------> WAIT_EOED -+
2945 * | | Recv | | Recv EndOfEarlyData
2946 * | | early data | | K_recv = handshake
2947 * | +------------+ |
2948 * | |
2949 * +> WAIT_FLIGHT2 <--------+
2950 * |
2951 * +--------+--------+
2952 * No auth | | Client auth
2953 * | |
2954 * | v
2955 * | WAIT_CERT
2956 * | Recv | | Recv Certificate
2957 * | empty | v
2958 * | Certificate | WAIT_CV
2959 * | | | Recv
2960 * | v | CertificateVerify
2961 * +-> WAIT_FINISHED <---+
2962 * | Recv Finished
2963 *
2964 * The function handles actions and state changes from 0-RTT to WAIT_FLIGHT2 in
2965 * the above diagram.
2966 */
Jerry Yu87b5ed42022-12-12 13:07:07 +08002967MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yu59d420f2023-12-01 16:30:34 +08002968static int ssl_tls13_process_end_of_early_data(mbedtls_ssl_context *ssl)
Jerry Yu87b5ed42022-12-12 13:07:07 +08002969{
2970 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yud5c34962023-12-01 16:32:31 +08002971
2972 MBEDTLS_SSL_DEBUG_MSG(2, ("=> ssl_tls13_process_end_of_early_data"));
2973
2974 MBEDTLS_SSL_PROC_CHK_NEG(ssl_tls13_end_of_early_data_coordinate(ssl));
2975
Jerry Yu3be85072023-12-04 09:58:54 +08002976 if (ret == SSL_GOT_END_OF_EARLY_DATA) {
Jerry Yud5c34962023-12-01 16:32:31 +08002977 unsigned char *buf;
2978 size_t buf_len;
2979
2980 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_tls13_fetch_handshake_msg(
2981 ssl, MBEDTLS_SSL_HS_END_OF_EARLY_DATA,
2982 &buf, &buf_len));
2983
2984 MBEDTLS_SSL_PROC_CHK(ssl_tls13_parse_end_of_early_data(
2985 ssl, buf, buf + buf_len));
2986
Jerry Yue9655122023-12-01 16:44:40 +08002987 MBEDTLS_SSL_DEBUG_MSG(
2988 1, ("Switch to handshake keys for inbound traffic"
2989 "( K_recv = handshake )"));
2990 mbedtls_ssl_set_inbound_transform(
2991 ssl, ssl->handshake->transform_handshake);
2992
Jerry Yud5c34962023-12-01 16:32:31 +08002993 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_add_hs_msg_to_checksum(
2994 ssl, MBEDTLS_SSL_HS_END_OF_EARLY_DATA,
2995 buf, buf_len));
Jerry Yue9655122023-12-01 16:44:40 +08002996
Jerry Yu3be85072023-12-04 09:58:54 +08002997 ssl_tls13_prepare_for_handshake_second_flight(ssl);
Jerry Yud5c34962023-12-01 16:32:31 +08002998
Jerry Yub55f9eb2023-12-05 10:27:17 +08002999 } else if (ret == SSL_GOT_EARLY_DATA) {
Jerry Yud9ca3542023-12-06 17:23:52 +08003000 ret = MBEDTLS_ERR_SSL_RECEIVED_EARLY_DATA;
3001 goto cleanup;
Jerry Yud5c34962023-12-01 16:32:31 +08003002 } else {
3003 MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
3004 ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
3005 goto cleanup;
3006 }
3007
Jerry Yud5c34962023-12-01 16:32:31 +08003008cleanup:
3009 MBEDTLS_SSL_DEBUG_MSG(2, ("<= ssl_tls13_process_end_of_early_data"));
Jerry Yu87b5ed42022-12-12 13:07:07 +08003010 return ret;
3011}
3012#endif /* MBEDTLS_SSL_EARLY_DATA */
3013
Jerry Yu69dd8d42022-04-16 12:51:26 +08003014/*
Jerry Yud6e253d2022-05-18 13:59:24 +08003015 * Handler for MBEDTLS_SSL_CLIENT_FINISHED
Jerry Yu69dd8d42022-04-16 12:51:26 +08003016 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02003017MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01003018static int ssl_tls13_process_client_finished(mbedtls_ssl_context *ssl)
Jerry Yu69dd8d42022-04-16 12:51:26 +08003019{
Jerry Yud6e253d2022-05-18 13:59:24 +08003020 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
3021
Gilles Peskine449bd832023-01-11 14:50:10 +01003022 ret = mbedtls_ssl_tls13_process_finished_message(ssl);
3023 if (ret != 0) {
3024 return ret;
Jerry Yue3d67cb2022-05-19 15:33:10 +08003025 }
3026
Gilles Peskine449bd832023-01-11 14:50:10 +01003027 ret = mbedtls_ssl_tls13_compute_resumption_master_secret(ssl);
3028 if (ret != 0) {
Xiaokang Qian9f1747b2023-03-30 02:50:04 +00003029 MBEDTLS_SSL_DEBUG_RET(
3030 1, "mbedtls_ssl_tls13_compute_resumption_master_secret", ret);
Gilles Peskine449bd832023-01-11 14:50:10 +01003031 }
3032
3033 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_HANDSHAKE_WRAPUP);
3034 return 0;
Jerry Yu69dd8d42022-04-16 12:51:26 +08003035}
3036
3037/*
Jerry Yud6e253d2022-05-18 13:59:24 +08003038 * Handler for MBEDTLS_SSL_HANDSHAKE_WRAPUP
Jerry Yu69dd8d42022-04-16 12:51:26 +08003039 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02003040MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01003041static int ssl_tls13_handshake_wrapup(mbedtls_ssl_context *ssl)
Jerry Yu69dd8d42022-04-16 12:51:26 +08003042{
Gilles Peskine449bd832023-01-11 14:50:10 +01003043 MBEDTLS_SSL_DEBUG_MSG(2, ("handshake: done"));
Jerry Yu03ed50b2022-04-16 17:13:30 +08003044
Gilles Peskine449bd832023-01-11 14:50:10 +01003045 mbedtls_ssl_tls13_handshake_wrapup(ssl);
Jerry Yue67bef42022-07-07 07:29:42 +00003046
Pengyu Lv3643fdb2023-01-12 16:46:28 +08003047#if defined(MBEDTLS_SSL_SESSION_TICKETS) && \
3048 defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED)
Pengyu Lva1aa31b2022-12-13 13:49:59 +08003049/* TODO: Remove the check of SOME_PSK_ENABLED since SESSION_TICKETS requires
3050 * SOME_PSK_ENABLED to be enabled. Here is just to make CI happy. It is
3051 * expected to be resolved with issue#6395.
3052 */
Pengyu Lvc7af2c42022-12-01 16:33:00 +08003053 /* Sent NewSessionTicket message only when client supports PSK */
Pengyu Lvb2cfafb2023-11-14 13:56:13 +08003054 if (mbedtls_ssl_tls13_is_some_psk_supported(ssl)) {
Xiaokang Qian9f1747b2023-03-30 02:50:04 +00003055 mbedtls_ssl_handshake_set_state(
3056 ssl, MBEDTLS_SSL_TLS1_3_NEW_SESSION_TICKET);
Pengyu Lvc7af2c42022-12-01 16:33:00 +08003057 } else
Jerry Yufca4d572022-07-21 10:37:48 +08003058#endif
Pengyu Lv3643fdb2023-01-12 16:46:28 +08003059 {
3060 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_HANDSHAKE_OVER);
3061 }
Gilles Peskine449bd832023-01-11 14:50:10 +01003062 return 0;
Jerry Yu69dd8d42022-04-16 12:51:26 +08003063}
3064
3065/*
Jerry Yua8d3c502022-10-30 14:51:23 +08003066 * Handler for MBEDTLS_SSL_TLS1_3_NEW_SESSION_TICKET
Jerry Yue67bef42022-07-07 07:29:42 +00003067 */
3068#define SSL_NEW_SESSION_TICKET_SKIP 0
3069#define SSL_NEW_SESSION_TICKET_WRITE 1
3070MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01003071static int ssl_tls13_write_new_session_ticket_coordinate(mbedtls_ssl_context *ssl)
Jerry Yue67bef42022-07-07 07:29:42 +00003072{
3073 /* Check whether the use of session tickets is enabled */
Gilles Peskine449bd832023-01-11 14:50:10 +01003074 if (ssl->conf->f_ticket_write == NULL) {
3075 MBEDTLS_SSL_DEBUG_MSG(2, ("NewSessionTicket: disabled,"
3076 " callback is not set"));
3077 return SSL_NEW_SESSION_TICKET_SKIP;
Jerry Yud0766ec2022-09-22 10:46:57 +08003078 }
Gilles Peskine449bd832023-01-11 14:50:10 +01003079 if (ssl->conf->new_session_tickets_count == 0) {
3080 MBEDTLS_SSL_DEBUG_MSG(2, ("NewSessionTicket: disabled,"
3081 " configured count is zero"));
3082 return SSL_NEW_SESSION_TICKET_SKIP;
Jerry Yud0766ec2022-09-22 10:46:57 +08003083 }
3084
Gilles Peskine449bd832023-01-11 14:50:10 +01003085 if (ssl->handshake->new_session_tickets_count == 0) {
3086 MBEDTLS_SSL_DEBUG_MSG(2, ("NewSessionTicket: all tickets have "
3087 "been sent."));
3088 return SSL_NEW_SESSION_TICKET_SKIP;
Jerry Yue67bef42022-07-07 07:29:42 +00003089 }
3090
Gilles Peskine449bd832023-01-11 14:50:10 +01003091 return SSL_NEW_SESSION_TICKET_WRITE;
Jerry Yue67bef42022-07-07 07:29:42 +00003092}
3093
3094#if defined(MBEDTLS_SSL_SESSION_TICKETS)
3095MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01003096static int ssl_tls13_prepare_new_session_ticket(mbedtls_ssl_context *ssl,
3097 unsigned char *ticket_nonce,
3098 size_t ticket_nonce_size)
Jerry Yue67bef42022-07-07 07:29:42 +00003099{
3100 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
3101 mbedtls_ssl_session *session = ssl->session;
3102 mbedtls_ssl_ciphersuite_t *ciphersuite_info;
3103 psa_algorithm_t psa_hash_alg;
3104 int hash_length;
3105
Gilles Peskine449bd832023-01-11 14:50:10 +01003106 MBEDTLS_SSL_DEBUG_MSG(2, ("=> prepare NewSessionTicket msg"));
Jerry Yue67bef42022-07-07 07:29:42 +00003107
Pengyu Lv9f926952022-11-17 15:22:33 +08003108 /* Set ticket_flags depends on the advertised psk key exchange mode */
Pengyu Lv94a42cc2023-12-06 10:04:17 +08003109 mbedtls_ssl_tls13_session_clear_ticket_flags(
Pengyu Lv9eacb442022-12-09 14:39:19 +08003110 session, MBEDTLS_SSL_TLS1_3_TICKET_FLAGS_MASK);
Pengyu Lve6487fe2022-12-06 09:30:29 +08003111#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED)
Pengyu Lv94a42cc2023-12-06 10:04:17 +08003112 mbedtls_ssl_tls13_session_set_ticket_flags(
Pengyu Lv9eacb442022-12-09 14:39:19 +08003113 session, ssl->handshake->tls13_kex_modes);
Pengyu Lve6487fe2022-12-06 09:30:29 +08003114#endif
Jerry Yu95648b02023-12-06 15:03:34 +08003115
3116#if defined(MBEDTLS_SSL_EARLY_DATA)
3117 if (ssl->conf->early_data_enabled == MBEDTLS_SSL_EARLY_DATA_ENABLED &&
3118 ssl->conf->max_early_data_size > 0) {
Pengyu Lv94a42cc2023-12-06 10:04:17 +08003119 mbedtls_ssl_tls13_session_set_ticket_flags(
Jerry Yu95648b02023-12-06 15:03:34 +08003120 session, MBEDTLS_SSL_TLS1_3_TICKET_ALLOW_EARLY_DATA);
3121 }
3122#endif /* MBEDTLS_SSL_EARLY_DATA */
3123
Pengyu Lv4938a562023-01-16 11:28:49 +08003124 MBEDTLS_SSL_PRINT_TICKET_FLAGS(4, session->ticket_flags);
Pengyu Lv9f926952022-11-17 15:22:33 +08003125
Jerry Yue67bef42022-07-07 07:29:42 +00003126 /* Generate ticket_age_add */
Gilles Peskine449bd832023-01-11 14:50:10 +01003127 if ((ret = ssl->conf->f_rng(ssl->conf->p_rng,
3128 (unsigned char *) &session->ticket_age_add,
3129 sizeof(session->ticket_age_add)) != 0)) {
3130 MBEDTLS_SSL_DEBUG_RET(1, "generate_ticket_age_add", ret);
3131 return ret;
Jerry Yue67bef42022-07-07 07:29:42 +00003132 }
Gilles Peskine449bd832023-01-11 14:50:10 +01003133 MBEDTLS_SSL_DEBUG_MSG(3, ("ticket_age_add: %u",
3134 (unsigned int) session->ticket_age_add));
Jerry Yue67bef42022-07-07 07:29:42 +00003135
3136 /* Generate ticket_nonce */
Gilles Peskine449bd832023-01-11 14:50:10 +01003137 ret = ssl->conf->f_rng(ssl->conf->p_rng, ticket_nonce, ticket_nonce_size);
3138 if (ret != 0) {
3139 MBEDTLS_SSL_DEBUG_RET(1, "generate_ticket_nonce", ret);
3140 return ret;
Jerry Yue67bef42022-07-07 07:29:42 +00003141 }
Gilles Peskine449bd832023-01-11 14:50:10 +01003142 MBEDTLS_SSL_DEBUG_BUF(3, "ticket_nonce:",
3143 ticket_nonce, ticket_nonce_size);
Jerry Yue67bef42022-07-07 07:29:42 +00003144
3145 ciphersuite_info =
Gilles Peskine449bd832023-01-11 14:50:10 +01003146 (mbedtls_ssl_ciphersuite_t *) ssl->handshake->ciphersuite_info;
Dave Rodgman2eab4622023-10-05 13:30:37 +01003147 psa_hash_alg = mbedtls_md_psa_alg_from_type((mbedtls_md_type_t) ciphersuite_info->mac);
Gilles Peskine449bd832023-01-11 14:50:10 +01003148 hash_length = PSA_HASH_LENGTH(psa_hash_alg);
3149 if (hash_length == -1 ||
3150 (size_t) hash_length > sizeof(session->resumption_key)) {
3151 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Jerry Yufca4d572022-07-21 10:37:48 +08003152 }
3153
Jerry Yue67bef42022-07-07 07:29:42 +00003154 /* In this code the psk key length equals the length of the hash */
3155 session->resumption_key_len = hash_length;
3156 session->ciphersuite = ciphersuite_info->id;
3157
3158 /* Compute resumption key
3159 *
3160 * HKDF-Expand-Label( resumption_master_secret,
3161 * "resumption", ticket_nonce, Hash.length )
3162 */
3163 ret = mbedtls_ssl_tls13_hkdf_expand_label(
Gilles Peskine449bd832023-01-11 14:50:10 +01003164 psa_hash_alg,
3165 session->app_secrets.resumption_master_secret,
3166 hash_length,
3167 MBEDTLS_SSL_TLS1_3_LBL_WITH_LEN(resumption),
3168 ticket_nonce,
3169 ticket_nonce_size,
3170 session->resumption_key,
3171 hash_length);
Jerry Yue67bef42022-07-07 07:29:42 +00003172
Gilles Peskine449bd832023-01-11 14:50:10 +01003173 if (ret != 0) {
3174 MBEDTLS_SSL_DEBUG_RET(2,
3175 "Creating the ticket-resumed PSK failed",
3176 ret);
3177 return ret;
Jerry Yue67bef42022-07-07 07:29:42 +00003178 }
Gilles Peskine449bd832023-01-11 14:50:10 +01003179 MBEDTLS_SSL_DEBUG_BUF(3, "Ticket-resumed PSK",
3180 session->resumption_key,
3181 session->resumption_key_len);
Jerry Yue67bef42022-07-07 07:29:42 +00003182
Gilles Peskine449bd832023-01-11 14:50:10 +01003183 MBEDTLS_SSL_DEBUG_BUF(3, "resumption_master_secret",
3184 session->app_secrets.resumption_master_secret,
3185 hash_length);
Jerry Yue67bef42022-07-07 07:29:42 +00003186
Gilles Peskine449bd832023-01-11 14:50:10 +01003187 return 0;
Jerry Yue67bef42022-07-07 07:29:42 +00003188}
3189
3190/* This function creates a NewSessionTicket message in the following format:
3191 *
3192 * struct {
3193 * uint32 ticket_lifetime;
3194 * uint32 ticket_age_add;
3195 * opaque ticket_nonce<0..255>;
3196 * opaque ticket<1..2^16-1>;
3197 * Extension extensions<0..2^16-2>;
3198 * } NewSessionTicket;
3199 *
3200 * The ticket inside the NewSessionTicket message is an encrypted container
3201 * carrying the necessary information so that the server is later able to
3202 * re-start the communication.
3203 *
3204 * The following fields are placed inside the ticket by the
3205 * f_ticket_write() function:
3206 *
Jerry Yu0069abc2023-11-23 21:07:28 +08003207 * - creation time (ticket_creation_time)
3208 * - flags (ticket_flags)
Jerry Yue67bef42022-07-07 07:29:42 +00003209 * - age add (ticket_age_add)
Jerry Yu0069abc2023-11-23 21:07:28 +08003210 * - key (resumption_key)
3211 * - key length (resumption_key_len)
Jerry Yue67bef42022-07-07 07:29:42 +00003212 * - ciphersuite (ciphersuite)
Jerry Yu0069abc2023-11-23 21:07:28 +08003213 * - max_early_data_size (max_early_data_size)
Jerry Yue67bef42022-07-07 07:29:42 +00003214 */
3215MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01003216static int ssl_tls13_write_new_session_ticket_body(mbedtls_ssl_context *ssl,
3217 unsigned char *buf,
3218 unsigned char *end,
3219 size_t *out_len,
3220 unsigned char *ticket_nonce,
3221 size_t ticket_nonce_size)
Jerry Yue67bef42022-07-07 07:29:42 +00003222{
3223 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
3224 unsigned char *p = buf;
3225 mbedtls_ssl_session *session = ssl->session;
3226 size_t ticket_len;
3227 uint32_t ticket_lifetime;
Jerry Yu01da35e2022-12-12 15:09:22 +08003228 unsigned char *p_extensions_len;
Jerry Yue67bef42022-07-07 07:29:42 +00003229
3230 *out_len = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01003231 MBEDTLS_SSL_DEBUG_MSG(2, ("=> write NewSessionTicket msg"));
Jerry Yue67bef42022-07-07 07:29:42 +00003232
3233 /*
3234 * ticket_lifetime 4 bytes
3235 * ticket_age_add 4 bytes
3236 * ticket_nonce 1 + ticket_nonce_size bytes
3237 * ticket >=2 bytes
3238 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003239 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 4 + 4 + 1 + ticket_nonce_size + 2);
Jerry Yue67bef42022-07-07 07:29:42 +00003240
3241 /* Generate ticket and ticket_lifetime */
Ronald Cron3c0072b2023-11-22 10:00:14 +01003242#if defined(MBEDTLS_HAVE_TIME)
3243 session->ticket_creation_time = mbedtls_ms_time();
3244#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01003245 ret = ssl->conf->f_ticket_write(ssl->conf->p_ticket,
3246 session,
3247 p + 9 + ticket_nonce_size + 2,
3248 end,
3249 &ticket_len,
3250 &ticket_lifetime);
3251 if (ret != 0) {
3252 MBEDTLS_SSL_DEBUG_RET(1, "write_ticket", ret);
3253 return ret;
Jerry Yue67bef42022-07-07 07:29:42 +00003254 }
3255 /* RFC 8446 4.6.1
3256 * ticket_lifetime: Indicates the lifetime in seconds as a 32-bit
3257 * unsigned integer in network byte order from the time of ticket
3258 * issuance. Servers MUST NOT use any value greater than
3259 * 604800 seconds (7 days). The value of zero indicates that the
3260 * ticket should be discarded immediately. Clients MUST NOT cache
3261 * tickets for longer than 7 days, regardless of the ticket_lifetime,
3262 * and MAY delete tickets earlier based on local policy. A server
3263 * MAY treat a ticket as valid for a shorter period of time than what
3264 * is stated in the ticket_lifetime.
3265 */
Jerry Yu8e0174a2023-11-10 13:58:16 +08003266 if (ticket_lifetime > MBEDTLS_SSL_TLS1_3_MAX_ALLOWED_TICKET_LIFETIME) {
3267 ticket_lifetime = MBEDTLS_SSL_TLS1_3_MAX_ALLOWED_TICKET_LIFETIME;
Gilles Peskine449bd832023-01-11 14:50:10 +01003268 }
3269 MBEDTLS_PUT_UINT32_BE(ticket_lifetime, p, 0);
3270 MBEDTLS_SSL_DEBUG_MSG(3, ("ticket_lifetime: %u",
3271 (unsigned int) ticket_lifetime));
Jerry Yue67bef42022-07-07 07:29:42 +00003272
3273 /* Write ticket_age_add */
Gilles Peskine449bd832023-01-11 14:50:10 +01003274 MBEDTLS_PUT_UINT32_BE(session->ticket_age_add, p, 4);
3275 MBEDTLS_SSL_DEBUG_MSG(3, ("ticket_age_add: %u",
3276 (unsigned int) session->ticket_age_add));
Jerry Yue67bef42022-07-07 07:29:42 +00003277
3278 /* Write ticket_nonce */
Gilles Peskine449bd832023-01-11 14:50:10 +01003279 p[8] = (unsigned char) ticket_nonce_size;
3280 if (ticket_nonce_size > 0) {
3281 memcpy(p + 9, ticket_nonce, ticket_nonce_size);
Jerry Yue67bef42022-07-07 07:29:42 +00003282 }
3283 p += 9 + ticket_nonce_size;
3284
3285 /* Write ticket */
Gilles Peskine449bd832023-01-11 14:50:10 +01003286 MBEDTLS_PUT_UINT16_BE(ticket_len, p, 0);
Jerry Yue67bef42022-07-07 07:29:42 +00003287 p += 2;
Gilles Peskine449bd832023-01-11 14:50:10 +01003288 MBEDTLS_SSL_DEBUG_BUF(4, "ticket", p, ticket_len);
Jerry Yue67bef42022-07-07 07:29:42 +00003289 p += ticket_len;
3290
3291 /* Ticket Extensions
3292 *
Jerry Yu01da35e2022-12-12 15:09:22 +08003293 * Extension extensions<0..2^16-2>;
Jerry Yue67bef42022-07-07 07:29:42 +00003294 */
Jerry Yuedab6372022-10-31 14:37:31 +08003295 ssl->handshake->sent_extensions = MBEDTLS_SSL_EXT_MASK_NONE;
3296
Gilles Peskine449bd832023-01-11 14:50:10 +01003297 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 2);
Jerry Yu01da35e2022-12-12 15:09:22 +08003298 p_extensions_len = p;
Jerry Yue67bef42022-07-07 07:29:42 +00003299 p += 2;
3300
Jerry Yu01da35e2022-12-12 15:09:22 +08003301#if defined(MBEDTLS_SSL_EARLY_DATA)
Pengyu Lv94a42cc2023-12-06 10:04:17 +08003302 if (mbedtls_ssl_tls13_session_ticket_allow_early_data(session)) {
Jerry Yu95648b02023-12-06 15:03:34 +08003303 size_t output_len;
3304
Jerry Yuf135bac2023-11-23 18:10:51 +08003305 if ((ret = mbedtls_ssl_tls13_write_early_data_ext(
Jerry Yuc59c5862023-12-05 10:40:49 +08003306 ssl, 1, p, end, &output_len)) != 0) {
Jerry Yuf135bac2023-11-23 18:10:51 +08003307 MBEDTLS_SSL_DEBUG_RET(
3308 1, "mbedtls_ssl_tls13_write_early_data_ext", ret);
3309 return ret;
3310 }
3311 p += output_len;
Jerry Yu9e7f9bc2023-11-27 16:52:07 +08003312 } else {
3313 MBEDTLS_SSL_DEBUG_MSG(
3314 4, ("early_data not allowed, "
3315 "skip early_data extension in NewSessionTicket"));
Jerry Yu01da35e2022-12-12 15:09:22 +08003316 }
Jerry Yuf135bac2023-11-23 18:10:51 +08003317
Jerry Yu01da35e2022-12-12 15:09:22 +08003318#endif /* MBEDTLS_SSL_EARLY_DATA */
3319
3320 MBEDTLS_PUT_UINT16_BE(p - p_extensions_len - 2, p_extensions_len, 0);
3321
Jerry Yue67bef42022-07-07 07:29:42 +00003322 *out_len = p - buf;
Gilles Peskine449bd832023-01-11 14:50:10 +01003323 MBEDTLS_SSL_DEBUG_BUF(4, "ticket", buf, *out_len);
3324 MBEDTLS_SSL_DEBUG_MSG(2, ("<= write new session ticket"));
Jerry Yue67bef42022-07-07 07:29:42 +00003325
Jerry Yu7de2ff02022-11-08 21:43:46 +08003326 MBEDTLS_SSL_PRINT_EXTS(
Gilles Peskine449bd832023-01-11 14:50:10 +01003327 3, MBEDTLS_SSL_HS_NEW_SESSION_TICKET, ssl->handshake->sent_extensions);
Jerry Yu4b8f2f72022-10-31 13:31:22 +08003328
Gilles Peskine449bd832023-01-11 14:50:10 +01003329 return 0;
Jerry Yue67bef42022-07-07 07:29:42 +00003330}
3331
3332/*
Jerry Yua8d3c502022-10-30 14:51:23 +08003333 * Handler for MBEDTLS_SSL_TLS1_3_NEW_SESSION_TICKET
Jerry Yue67bef42022-07-07 07:29:42 +00003334 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003335static int ssl_tls13_write_new_session_ticket(mbedtls_ssl_context *ssl)
Jerry Yue67bef42022-07-07 07:29:42 +00003336{
3337 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
3338
Gilles Peskine449bd832023-01-11 14:50:10 +01003339 MBEDTLS_SSL_PROC_CHK_NEG(ssl_tls13_write_new_session_ticket_coordinate(ssl));
Jerry Yue67bef42022-07-07 07:29:42 +00003340
Gilles Peskine449bd832023-01-11 14:50:10 +01003341 if (ret == SSL_NEW_SESSION_TICKET_WRITE) {
Jerry Yue67bef42022-07-07 07:29:42 +00003342 unsigned char ticket_nonce[MBEDTLS_SSL_TLS1_3_TICKET_NONCE_LENGTH];
3343 unsigned char *buf;
3344 size_t buf_len, msg_len;
3345
Gilles Peskine449bd832023-01-11 14:50:10 +01003346 MBEDTLS_SSL_PROC_CHK(ssl_tls13_prepare_new_session_ticket(
3347 ssl, ticket_nonce, sizeof(ticket_nonce)));
Jerry Yue67bef42022-07-07 07:29:42 +00003348
Xiaokang Qian9f1747b2023-03-30 02:50:04 +00003349 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_start_handshake_msg(
3350 ssl, MBEDTLS_SSL_HS_NEW_SESSION_TICKET,
3351 &buf, &buf_len));
Jerry Yue67bef42022-07-07 07:29:42 +00003352
Gilles Peskine449bd832023-01-11 14:50:10 +01003353 MBEDTLS_SSL_PROC_CHK(ssl_tls13_write_new_session_ticket_body(
3354 ssl, buf, buf + buf_len, &msg_len,
3355 ticket_nonce, sizeof(ticket_nonce)));
Jerry Yue67bef42022-07-07 07:29:42 +00003356
Gilles Peskine449bd832023-01-11 14:50:10 +01003357 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_finish_handshake_msg(
3358 ssl, buf_len, msg_len));
Jerry Yufca4d572022-07-21 10:37:48 +08003359
Jerry Yu359e65f2022-09-22 23:47:43 +08003360 /* Limit session tickets count to one when resumption connection.
3361 *
3362 * See document of mbedtls_ssl_conf_new_session_tickets.
3363 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003364 if (ssl->handshake->resume == 1) {
Jerry Yu359e65f2022-09-22 23:47:43 +08003365 ssl->handshake->new_session_tickets_count = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01003366 } else {
Jerry Yu359e65f2022-09-22 23:47:43 +08003367 ssl->handshake->new_session_tickets_count--;
Gilles Peskine449bd832023-01-11 14:50:10 +01003368 }
Jerry Yub7e3fa72022-09-22 11:07:18 +08003369
Jerry Yua8d3c502022-10-30 14:51:23 +08003370 mbedtls_ssl_handshake_set_state(
Gilles Peskine449bd832023-01-11 14:50:10 +01003371 ssl, MBEDTLS_SSL_TLS1_3_NEW_SESSION_TICKET_FLUSH);
3372 } else {
3373 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_HANDSHAKE_OVER);
Jerry Yue67bef42022-07-07 07:29:42 +00003374 }
3375
Jerry Yue67bef42022-07-07 07:29:42 +00003376cleanup:
3377
Gilles Peskine449bd832023-01-11 14:50:10 +01003378 return ret;
Jerry Yue67bef42022-07-07 07:29:42 +00003379}
3380#endif /* MBEDTLS_SSL_SESSION_TICKETS */
3381
3382/*
XiaokangQiane8ff3502022-04-22 02:34:40 +00003383 * TLS 1.3 State Machine -- server side
XiaokangQian7807f9f2022-02-15 10:04:37 +00003384 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003385int mbedtls_ssl_tls13_handshake_server_step(mbedtls_ssl_context *ssl)
Jerry Yub9930e72021-08-06 17:11:51 +08003386{
XiaokangQian08037552022-04-20 07:16:41 +00003387 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
XiaokangQian7807f9f2022-02-15 10:04:37 +00003388
Gilles Peskine449bd832023-01-11 14:50:10 +01003389 if (ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER || ssl->handshake == NULL) {
3390 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
3391 }
XiaokangQian7807f9f2022-02-15 10:04:37 +00003392
Gilles Peskine449bd832023-01-11 14:50:10 +01003393 MBEDTLS_SSL_DEBUG_MSG(2, ("tls13 server state: %s(%d)",
Dave Rodgman2eab4622023-10-05 13:30:37 +01003394 mbedtls_ssl_states_str((mbedtls_ssl_states) ssl->state),
Gilles Peskine449bd832023-01-11 14:50:10 +01003395 ssl->state));
Jerry Yu6e81b272021-09-27 11:16:17 +08003396
Gilles Peskine449bd832023-01-11 14:50:10 +01003397 switch (ssl->state) {
XiaokangQian7807f9f2022-02-15 10:04:37 +00003398 /* start state */
3399 case MBEDTLS_SSL_HELLO_REQUEST:
Gilles Peskine449bd832023-01-11 14:50:10 +01003400 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_CLIENT_HELLO);
XiaokangQian08037552022-04-20 07:16:41 +00003401 ret = 0;
XiaokangQian7807f9f2022-02-15 10:04:37 +00003402 break;
3403
XiaokangQian7807f9f2022-02-15 10:04:37 +00003404 case MBEDTLS_SSL_CLIENT_HELLO:
Gilles Peskine449bd832023-01-11 14:50:10 +01003405 ret = ssl_tls13_process_client_hello(ssl);
3406 if (ret != 0) {
3407 MBEDTLS_SSL_DEBUG_RET(1, "ssl_tls13_process_client_hello", ret);
3408 }
Jerry Yuf41553b2022-05-09 22:20:30 +08003409 break;
XiaokangQian7807f9f2022-02-15 10:04:37 +00003410
Jerry Yuf41553b2022-05-09 22:20:30 +08003411 case MBEDTLS_SSL_HELLO_RETRY_REQUEST:
Gilles Peskine449bd832023-01-11 14:50:10 +01003412 ret = ssl_tls13_write_hello_retry_request(ssl);
3413 if (ret != 0) {
3414 MBEDTLS_SSL_DEBUG_RET(1, "ssl_tls13_write_hello_retry_request", ret);
3415 return ret;
Jerry Yuf41553b2022-05-09 22:20:30 +08003416 }
XiaokangQian7807f9f2022-02-15 10:04:37 +00003417 break;
3418
Jerry Yu5b64ae92022-03-30 17:15:02 +08003419 case MBEDTLS_SSL_SERVER_HELLO:
Gilles Peskine449bd832023-01-11 14:50:10 +01003420 ret = ssl_tls13_write_server_hello(ssl);
Jerry Yu5b64ae92022-03-30 17:15:02 +08003421 break;
3422
Xiaofei Baicba64af2022-02-15 10:00:56 +00003423 case MBEDTLS_SSL_ENCRYPTED_EXTENSIONS:
Gilles Peskine449bd832023-01-11 14:50:10 +01003424 ret = ssl_tls13_write_encrypted_extensions(ssl);
3425 if (ret != 0) {
3426 MBEDTLS_SSL_DEBUG_RET(1, "ssl_tls13_write_encrypted_extensions", ret);
3427 return ret;
Xiaofei Baicba64af2022-02-15 10:00:56 +00003428 }
Jerry Yu48330562022-05-06 21:35:44 +08003429 break;
Jerry Yu6a2cd9e2022-05-05 11:14:19 +08003430
Ronald Cron928cbd32022-10-04 16:14:26 +02003431#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED)
Xiaofei Bai9ca09d42022-02-14 12:57:18 +00003432 case MBEDTLS_SSL_CERTIFICATE_REQUEST:
Gilles Peskine449bd832023-01-11 14:50:10 +01003433 ret = ssl_tls13_write_certificate_request(ssl);
Xiaofei Bai9ca09d42022-02-14 12:57:18 +00003434 break;
Xiaofei Bai9ca09d42022-02-14 12:57:18 +00003435
Jerry Yu83da34e2022-04-16 13:59:52 +08003436 case MBEDTLS_SSL_SERVER_CERTIFICATE:
Gilles Peskine449bd832023-01-11 14:50:10 +01003437 ret = ssl_tls13_write_server_certificate(ssl);
Jerry Yu83da34e2022-04-16 13:59:52 +08003438 break;
3439
3440 case MBEDTLS_SSL_CERTIFICATE_VERIFY:
Gilles Peskine449bd832023-01-11 14:50:10 +01003441 ret = ssl_tls13_write_certificate_verify(ssl);
Jerry Yu83da34e2022-04-16 13:59:52 +08003442 break;
Ronald Cron928cbd32022-10-04 16:14:26 +02003443#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED */
Jerry Yu83da34e2022-04-16 13:59:52 +08003444
Gilles Peskine449bd832023-01-11 14:50:10 +01003445 /*
3446 * Injection of dummy-CCS's for middlebox compatibility
3447 */
Gabor Mezei7b39bf12022-05-24 16:04:14 +02003448#if defined(MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE)
Gabor Mezeif7044ea2022-06-28 16:01:49 +02003449 case MBEDTLS_SSL_SERVER_CCS_AFTER_HELLO_RETRY_REQUEST:
Gilles Peskine449bd832023-01-11 14:50:10 +01003450 ret = mbedtls_ssl_tls13_write_change_cipher_spec(ssl);
3451 if (ret == 0) {
3452 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_CLIENT_HELLO);
3453 }
Gabor Mezei7b39bf12022-05-24 16:04:14 +02003454 break;
3455
3456 case MBEDTLS_SSL_SERVER_CCS_AFTER_SERVER_HELLO:
Ronald Crone273f722024-02-13 18:22:26 +01003457 ret = mbedtls_ssl_tls13_write_change_cipher_spec(ssl);
3458 if (ret != 0) {
3459 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01003460 }
Ronald Cronfe59ff72024-01-24 14:31:50 +01003461 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_ENCRYPTED_EXTENSIONS);
Gabor Mezei7b39bf12022-05-24 16:04:14 +02003462 break;
3463#endif /* MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE */
3464
Jerry Yu69dd8d42022-04-16 12:51:26 +08003465 case MBEDTLS_SSL_SERVER_FINISHED:
Gilles Peskine449bd832023-01-11 14:50:10 +01003466 ret = ssl_tls13_write_server_finished(ssl);
Jerry Yu69dd8d42022-04-16 12:51:26 +08003467 break;
3468
Jerry Yu87b5ed42022-12-12 13:07:07 +08003469#if defined(MBEDTLS_SSL_EARLY_DATA)
3470 case MBEDTLS_SSL_END_OF_EARLY_DATA:
Jerry Yu59d420f2023-12-01 16:30:34 +08003471 ret = ssl_tls13_process_end_of_early_data(ssl);
Jerry Yu87b5ed42022-12-12 13:07:07 +08003472 break;
3473#endif /* MBEDTLS_SSL_EARLY_DATA */
3474
Jerry Yu69dd8d42022-04-16 12:51:26 +08003475 case MBEDTLS_SSL_CLIENT_FINISHED:
Gilles Peskine449bd832023-01-11 14:50:10 +01003476 ret = ssl_tls13_process_client_finished(ssl);
Jerry Yu69dd8d42022-04-16 12:51:26 +08003477 break;
3478
Jerry Yu69dd8d42022-04-16 12:51:26 +08003479 case MBEDTLS_SSL_HANDSHAKE_WRAPUP:
Gilles Peskine449bd832023-01-11 14:50:10 +01003480 ret = ssl_tls13_handshake_wrapup(ssl);
Jerry Yu69dd8d42022-04-16 12:51:26 +08003481 break;
3482
Ronald Cron766c0cd2022-10-18 12:17:11 +02003483#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED)
XiaokangQian6b916b12022-04-25 07:29:34 +00003484 case MBEDTLS_SSL_CLIENT_CERTIFICATE:
Gilles Peskine449bd832023-01-11 14:50:10 +01003485 ret = mbedtls_ssl_tls13_process_certificate(ssl);
3486 if (ret == 0) {
3487 if (ssl->session_negotiate->peer_cert != NULL) {
Ronald Cron209cae92022-06-07 10:30:19 +02003488 mbedtls_ssl_handshake_set_state(
Gilles Peskine449bd832023-01-11 14:50:10 +01003489 ssl, MBEDTLS_SSL_CLIENT_CERTIFICATE_VERIFY);
3490 } else {
3491 MBEDTLS_SSL_DEBUG_MSG(2, ("skip parse certificate verify"));
Ronald Cron209cae92022-06-07 10:30:19 +02003492 mbedtls_ssl_handshake_set_state(
Gilles Peskine449bd832023-01-11 14:50:10 +01003493 ssl, MBEDTLS_SSL_CLIENT_FINISHED);
Ronald Cron19385882022-06-15 16:26:13 +02003494 }
XiaokangQian6b916b12022-04-25 07:29:34 +00003495 }
3496 break;
3497
3498 case MBEDTLS_SSL_CLIENT_CERTIFICATE_VERIFY:
Gilles Peskine449bd832023-01-11 14:50:10 +01003499 ret = mbedtls_ssl_tls13_process_certificate_verify(ssl);
3500 if (ret == 0) {
XiaokangQian6b916b12022-04-25 07:29:34 +00003501 mbedtls_ssl_handshake_set_state(
Gilles Peskine449bd832023-01-11 14:50:10 +01003502 ssl, MBEDTLS_SSL_CLIENT_FINISHED);
XiaokangQian6b916b12022-04-25 07:29:34 +00003503 }
3504 break;
Ronald Cron766c0cd2022-10-18 12:17:11 +02003505#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED */
XiaokangQian6b916b12022-04-25 07:29:34 +00003506
Jerry Yue67bef42022-07-07 07:29:42 +00003507#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Jerry Yua8d3c502022-10-30 14:51:23 +08003508 case MBEDTLS_SSL_TLS1_3_NEW_SESSION_TICKET:
Gilles Peskine449bd832023-01-11 14:50:10 +01003509 ret = ssl_tls13_write_new_session_ticket(ssl);
3510 if (ret != 0) {
3511 MBEDTLS_SSL_DEBUG_RET(1,
3512 "ssl_tls13_write_new_session_ticket ",
3513 ret);
Jerry Yue67bef42022-07-07 07:29:42 +00003514 }
3515 break;
Jerry Yua8d3c502022-10-30 14:51:23 +08003516 case MBEDTLS_SSL_TLS1_3_NEW_SESSION_TICKET_FLUSH:
Jerry Yue67bef42022-07-07 07:29:42 +00003517 /* This state is necessary to do the flush of the New Session
Jerry Yua8d3c502022-10-30 14:51:23 +08003518 * Ticket message written in MBEDTLS_SSL_TLS1_3_NEW_SESSION_TICKET
Jerry Yue67bef42022-07-07 07:29:42 +00003519 * as part of ssl_prepare_handshake_step.
3520 */
3521 ret = 0;
Jerry Yud4e75002022-08-09 13:33:50 +08003522
Gilles Peskine449bd832023-01-11 14:50:10 +01003523 if (ssl->handshake->new_session_tickets_count == 0) {
3524 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_HANDSHAKE_OVER);
3525 } else {
Xiaokang Qian9f1747b2023-03-30 02:50:04 +00003526 mbedtls_ssl_handshake_set_state(
3527 ssl, MBEDTLS_SSL_TLS1_3_NEW_SESSION_TICKET);
Gilles Peskine449bd832023-01-11 14:50:10 +01003528 }
Jerry Yue67bef42022-07-07 07:29:42 +00003529 break;
3530
3531#endif /* MBEDTLS_SSL_SESSION_TICKETS */
3532
XiaokangQian7807f9f2022-02-15 10:04:37 +00003533 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01003534 MBEDTLS_SSL_DEBUG_MSG(1, ("invalid state %d", ssl->state));
3535 return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
XiaokangQian7807f9f2022-02-15 10:04:37 +00003536 }
3537
Gilles Peskine449bd832023-01-11 14:50:10 +01003538 return ret;
Jerry Yub9930e72021-08-06 17:11:51 +08003539}
Jerry Yu3cc4c2a2021-08-06 16:29:08 +08003540
Jerry Yufb4b6472022-01-27 15:03:26 +08003541#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_PROTO_TLS1_3 */