blob: 9b273b834c03dd8112ced7a1ac52e406927b2e08 [file] [log] [blame]
Jerry Yu3cc4c2a2021-08-06 16:29:08 +08001/*
Jerry Yub9930e72021-08-06 17:11:51 +08002 * TLS 1.3 server-side functions
Jerry Yu3cc4c2a2021-08-06 16:29:08 +08003 *
4 * Copyright The Mbed TLS Contributors
Dave Rodgman16799db2023-11-02 19:47:20 +00005 * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
Gilles Peskine449bd832023-01-11 14:50:10 +01006 */
Jerry Yu3cc4c2a2021-08-06 16:29:08 +08007
8#include "common.h"
9
Jerry Yufb4b6472022-01-27 15:03:26 +080010#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_PROTO_TLS1_3)
Jerry Yu3cc4c2a2021-08-06 16:29:08 +080011
Valerio Settib4f50762024-01-17 10:24:52 +010012#include "debug_internal.h"
Xiaofei Baicba64af2022-02-15 10:00:56 +000013#include "mbedtls/error.h"
14#include "mbedtls/platform.h"
Jerry Yu1c105562022-07-10 06:32:38 +000015#include "mbedtls/constant_time.h"
Manuel Pégourié-Gonnardd55d66f2023-06-20 10:14:58 +020016#include "mbedtls/oid.h"
Valerio Setti384fbde2024-01-02 13:26:40 +010017#include "mbedtls/psa_util.h"
Jerry Yu3bf2c642022-03-30 22:02:12 +080018
Xiaofei Bai9ca09d42022-02-14 12:57:18 +000019#include "ssl_misc.h"
20#include "ssl_tls13_keys.h"
21#include "ssl_debug_helpers.h"
22
Jerry Yuf35ba382022-08-23 17:58:26 +080023
Jerry Yuc5a23a02022-08-25 10:51:44 +080024static const mbedtls_ssl_ciphersuite_t *ssl_tls13_validate_peer_ciphersuite(
Gilles Peskine449bd832023-01-11 14:50:10 +010025 mbedtls_ssl_context *ssl,
26 unsigned int cipher_suite)
Jerry Yuf35ba382022-08-23 17:58:26 +080027{
28 const mbedtls_ssl_ciphersuite_t *ciphersuite_info;
Gilles Peskine449bd832023-01-11 14:50:10 +010029 if (!mbedtls_ssl_tls13_cipher_suite_is_offered(ssl, cipher_suite)) {
30 return NULL;
Jerry Yuf35ba382022-08-23 17:58:26 +080031 }
Gilles Peskine449bd832023-01-11 14:50:10 +010032
33 ciphersuite_info = mbedtls_ssl_ciphersuite_from_id(cipher_suite);
34 if ((mbedtls_ssl_validate_ciphersuite(ssl, ciphersuite_info,
35 ssl->tls_version,
36 ssl->tls_version) != 0)) {
37 return NULL;
38 }
39 return ciphersuite_info;
Jerry Yuf35ba382022-08-23 17:58:26 +080040}
41
Ronald Cron89089cc2024-02-14 18:18:08 +010042static void ssl_tls13_select_ciphersuite(
43 mbedtls_ssl_context *ssl,
44 const unsigned char *cipher_suites,
45 const unsigned char *cipher_suites_end,
46 int psk_ciphersuite_id,
47 psa_algorithm_t psk_hash_alg,
48 const mbedtls_ssl_ciphersuite_t **selected_ciphersuite_info)
49{
50 *selected_ciphersuite_info = NULL;
51
52 /*
53 * This function assumes that the length of the list of ciphersuites is
54 * even and it should have been checked it is so in the main ClientHello
55 * parsing function. Double check here.
56 */
57 if ((cipher_suites_end - cipher_suites) & 1) {
58 return;
59 }
60
61 for (const unsigned char *p = cipher_suites;
62 p < cipher_suites_end; p += 2) {
63 /*
64 * "cipher_suites_end - p is even" is an invariant of the loop. As
65 * cipher_suites_end - p > 0, we have cipher_suites_end - p >= 2 and it
66 * is thus safe to read two bytes.
67 */
68 uint16_t id = MBEDTLS_GET_UINT16_BE(p, 0);
69
70 const mbedtls_ssl_ciphersuite_t *info =
71 ssl_tls13_validate_peer_ciphersuite(ssl, id);
72 if (info == NULL) {
73 continue;
74 }
75
76 /*
77 * If a valid PSK ciphersuite identifier has been passed in, we seek
78 * for an exact match.
79 */
80 if (psk_ciphersuite_id != 0) {
81 if (id != psk_ciphersuite_id) {
82 continue;
83 }
84 } else if (psk_hash_alg != PSA_ALG_NONE) {
85 if (mbedtls_md_psa_alg_from_type((mbedtls_md_type_t) info->mac) !=
86 psk_hash_alg) {
87 continue;
88 }
89 }
90
91 *selected_ciphersuite_info = info;
92 return;
93 }
94
95 MBEDTLS_SSL_DEBUG_MSG(2, ("No matched ciphersuite"));
96}
97
Ronald Cron41a443a2022-10-04 16:38:25 +020098#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED)
Jerry Yue19e3b92022-07-08 12:04:51 +000099/* From RFC 8446:
100 *
101 * enum { psk_ke(0), psk_dhe_ke(1), (255) } PskKeyExchangeMode;
102 * struct {
103 * PskKeyExchangeMode ke_modes<1..255>;
104 * } PskKeyExchangeModes;
105 */
Jerry Yu6e74a7e2022-07-20 20:49:32 +0800106MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100107static int ssl_tls13_parse_key_exchange_modes_ext(mbedtls_ssl_context *ssl,
108 const unsigned char *buf,
109 const unsigned char *end)
Jerry Yue19e3b92022-07-08 12:04:51 +0000110{
Jerry Yu299e31f2022-07-13 23:06:36 +0800111 const unsigned char *p = buf;
Jerry Yue19e3b92022-07-08 12:04:51 +0000112 size_t ke_modes_len;
113 int ke_modes = 0;
114
Jerry Yu854dd9e2022-07-15 14:28:27 +0800115 /* Read ke_modes length (1 Byte) */
Gilles Peskine449bd832023-01-11 14:50:10 +0100116 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, 1);
Jerry Yu299e31f2022-07-13 23:06:36 +0800117 ke_modes_len = *p++;
Jerry Yue19e3b92022-07-08 12:04:51 +0000118 /* Currently, there are only two PSK modes, so even without looking
119 * at the content, something's wrong if the list has more than 2 items. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100120 if (ke_modes_len > 2) {
121 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER,
122 MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER);
123 return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
Jerry Yu299e31f2022-07-13 23:06:36 +0800124 }
Jerry Yue19e3b92022-07-08 12:04:51 +0000125
Gilles Peskine449bd832023-01-11 14:50:10 +0100126 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, ke_modes_len);
Jerry Yue19e3b92022-07-08 12:04:51 +0000127
Gilles Peskine449bd832023-01-11 14:50:10 +0100128 while (ke_modes_len-- != 0) {
129 switch (*p++) {
130 case MBEDTLS_SSL_TLS1_3_PSK_MODE_PURE:
131 ke_modes |= MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK;
132 MBEDTLS_SSL_DEBUG_MSG(3, ("Found PSK KEX MODE"));
133 break;
134 case MBEDTLS_SSL_TLS1_3_PSK_MODE_ECDHE:
135 ke_modes |= MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK_EPHEMERAL;
136 MBEDTLS_SSL_DEBUG_MSG(3, ("Found PSK_EPHEMERAL KEX MODE"));
137 break;
138 default:
139 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER,
140 MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER);
141 return MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER;
Jerry Yue19e3b92022-07-08 12:04:51 +0000142 }
143 }
144
145 ssl->handshake->tls13_kex_modes = ke_modes;
Gilles Peskine449bd832023-01-11 14:50:10 +0100146 return 0;
Jerry Yue19e3b92022-07-08 12:04:51 +0000147}
Jerry Yu1c105562022-07-10 06:32:38 +0000148
Ronald Cronfbae94a2023-12-05 18:15:14 +0100149#define SSL_TLS1_3_PSK_IDENTITY_DOES_NOT_MATCH 2
150#define SSL_TLS1_3_PSK_IDENTITY_MATCH_BUT_PSK_NOT_USABLE 1
151#define SSL_TLS1_3_PSK_IDENTITY_MATCH 0
Jerry Yu95699e72022-08-21 19:22:23 +0800152
153#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Pengyu Lv29daf4a2023-10-30 17:13:30 +0800154MBEDTLS_CHECK_RETURN_CRITICAL
Pengyu Lvbc4aab72023-12-01 15:37:24 +0800155static int ssl_tls13_key_exchange_is_psk_available(mbedtls_ssl_context *ssl);
Pengyu Lv29daf4a2023-10-30 17:13:30 +0800156MBEDTLS_CHECK_RETURN_CRITICAL
Pengyu Lvbc4aab72023-12-01 15:37:24 +0800157static int ssl_tls13_key_exchange_is_psk_ephemeral_available(mbedtls_ssl_context *ssl);
Jerry Yu95699e72022-08-21 19:22:23 +0800158
159MBEDTLS_CHECK_RETURN_CRITICAL
160static int ssl_tls13_offered_psks_check_identity_match_ticket(
Gilles Peskine449bd832023-01-11 14:50:10 +0100161 mbedtls_ssl_context *ssl,
162 const unsigned char *identity,
163 size_t identity_len,
164 uint32_t obfuscated_ticket_age,
165 mbedtls_ssl_session *session)
Jerry Yu95699e72022-08-21 19:22:23 +0800166{
Jerry Yufd310eb2022-09-06 09:16:35 +0800167 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yu95699e72022-08-21 19:22:23 +0800168 unsigned char *ticket_buffer;
Jerry Yu8d4bbba2022-09-13 14:15:48 +0800169#if defined(MBEDTLS_HAVE_TIME)
Jerry Yucebffc32022-12-15 18:00:05 +0800170 mbedtls_ms_time_t now;
171 mbedtls_ms_time_t server_age;
Jerry Yu713ce1f2023-11-17 15:32:12 +0800172 uint32_t client_age;
Jerry Yucebffc32022-12-15 18:00:05 +0800173 mbedtls_ms_time_t age_diff;
Jerry Yu8d4bbba2022-09-13 14:15:48 +0800174#endif
Jerry Yu95699e72022-08-21 19:22:23 +0800175
176 ((void) obfuscated_ticket_age);
177
Gilles Peskine449bd832023-01-11 14:50:10 +0100178 MBEDTLS_SSL_DEBUG_MSG(2, ("=> check_identity_match_ticket"));
Jerry Yu95699e72022-08-21 19:22:23 +0800179
Jerry Yufd310eb2022-09-06 09:16:35 +0800180 /* Ticket parser is not configured, Skip */
Gilles Peskine449bd832023-01-11 14:50:10 +0100181 if (ssl->conf->f_ticket_parse == NULL || identity_len == 0) {
Ronald Cronfbae94a2023-12-05 18:15:14 +0100182 return SSL_TLS1_3_PSK_IDENTITY_DOES_NOT_MATCH;
Gilles Peskine449bd832023-01-11 14:50:10 +0100183 }
Jerry Yu95699e72022-08-21 19:22:23 +0800184
Jerry Yu4746b102022-09-13 11:11:48 +0800185 /* We create a copy of the encrypted ticket since the ticket parsing
186 * function is allowed to use its input buffer as an output buffer
187 * (in-place decryption). We do, however, need the original buffer for
188 * computing the PSK binder value.
Jerry Yu95699e72022-08-21 19:22:23 +0800189 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100190 ticket_buffer = mbedtls_calloc(1, identity_len);
191 if (ticket_buffer == NULL) {
192 MBEDTLS_SSL_DEBUG_MSG(1, ("buffer too small"));
193 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
Jerry Yu95699e72022-08-21 19:22:23 +0800194 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100195 memcpy(ticket_buffer, identity, identity_len);
Jerry Yu95699e72022-08-21 19:22:23 +0800196
Ronald Cronfbae94a2023-12-05 18:15:14 +0100197 ret = ssl->conf->f_ticket_parse(ssl->conf->p_ticket,
198 session,
199 ticket_buffer, identity_len);
200 if (ret == 0) {
201 ret = SSL_TLS1_3_PSK_IDENTITY_MATCH;
202 } else {
203 if (ret == MBEDTLS_ERR_SSL_SESSION_TICKET_EXPIRED) {
Gilles Peskine449bd832023-01-11 14:50:10 +0100204 MBEDTLS_SSL_DEBUG_MSG(3, ("ticket is expired"));
Ronald Cronfbae94a2023-12-05 18:15:14 +0100205 ret = SSL_TLS1_3_PSK_IDENTITY_MATCH_BUT_PSK_NOT_USABLE;
Gilles Peskine449bd832023-01-11 14:50:10 +0100206 } else {
Ronald Cronfbae94a2023-12-05 18:15:14 +0100207 if (ret == MBEDTLS_ERR_SSL_INVALID_MAC) {
208 MBEDTLS_SSL_DEBUG_MSG(3, ("ticket is not authentic"));
209 } else {
210 MBEDTLS_SSL_DEBUG_RET(1, "ticket_parse", ret);
211 }
212 ret = SSL_TLS1_3_PSK_IDENTITY_DOES_NOT_MATCH;
Gilles Peskine449bd832023-01-11 14:50:10 +0100213 }
Jerry Yu95699e72022-08-21 19:22:23 +0800214 }
215
216 /* We delete the temporary buffer */
Gilles Peskine449bd832023-01-11 14:50:10 +0100217 mbedtls_free(ticket_buffer);
Jerry Yu95699e72022-08-21 19:22:23 +0800218
Ronald Cronfbae94a2023-12-05 18:15:14 +0100219 if (ret != SSL_TLS1_3_PSK_IDENTITY_MATCH) {
220 goto exit;
Jerry Yuce3b95e2023-10-31 16:02:04 +0800221 }
Jerry Yuce3b95e2023-10-31 16:02:04 +0800222
Ronald Cronfbae94a2023-12-05 18:15:14 +0100223 ret = SSL_TLS1_3_PSK_IDENTITY_MATCH_BUT_PSK_NOT_USABLE;
224
225 if (session->tls_version != MBEDTLS_SSL_VERSION_TLS1_3) {
226 MBEDTLS_SSL_DEBUG_MSG(3, ("Ticket TLS version is not 1.3."));
Jerry Yu8d4bbba2022-09-13 14:15:48 +0800227 goto exit;
228 }
Jerry Yu95699e72022-08-21 19:22:23 +0800229
Gilles Peskine449bd832023-01-11 14:50:10 +0100230#if defined(MBEDTLS_HAVE_TIME)
Jerry Yucebffc32022-12-15 18:00:05 +0800231 now = mbedtls_ms_time();
Gilles Peskine449bd832023-01-11 14:50:10 +0100232
Jerry Yu25ba4d42023-11-10 14:12:20 +0800233 if (now < session->ticket_creation_time) {
Gilles Peskine449bd832023-01-11 14:50:10 +0100234 MBEDTLS_SSL_DEBUG_MSG(
Jerry Yu713ce1f2023-11-17 15:32:12 +0800235 3, ("Invalid ticket creation time ( now = %" MBEDTLS_PRINTF_MS_TIME
236 ", creation_time = %" MBEDTLS_PRINTF_MS_TIME " )",
Jerry Yu25ba4d42023-11-10 14:12:20 +0800237 now, session->ticket_creation_time));
Gilles Peskine449bd832023-01-11 14:50:10 +0100238 goto exit;
239 }
240
Jerry Yu25ba4d42023-11-10 14:12:20 +0800241 server_age = now - session->ticket_creation_time;
Jerry Yu95699e72022-08-21 19:22:23 +0800242
Jerry Yu8d4bbba2022-09-13 14:15:48 +0800243 /* RFC 8446 section 4.6.1
244 *
245 * Servers MUST NOT use any value greater than 604800 seconds (7 days).
246 *
247 * RFC 8446 section 4.2.11.1
248 *
249 * Clients MUST NOT attempt to use tickets which have ages greater than
250 * the "ticket_lifetime" value which was provided with the ticket.
251 *
Jerry Yu8d4bbba2022-09-13 14:15:48 +0800252 */
Jerry Yu8e0174a2023-11-10 13:58:16 +0800253 if (server_age > MBEDTLS_SSL_TLS1_3_MAX_ALLOWED_TICKET_LIFETIME * 1000) {
Jerry Yu8d4bbba2022-09-13 14:15:48 +0800254 MBEDTLS_SSL_DEBUG_MSG(
Jerry Yud84c14f2023-11-16 13:33:57 +0800255 3, ("Ticket age exceeds limitation ticket_age = %" MBEDTLS_PRINTF_MS_TIME,
Jerry Yucebffc32022-12-15 18:00:05 +0800256 server_age));
Jerry Yu8d4bbba2022-09-13 14:15:48 +0800257 goto exit;
258 }
Jerry Yu95699e72022-08-21 19:22:23 +0800259
Jerry Yu8d4bbba2022-09-13 14:15:48 +0800260 /* RFC 8446 section 4.2.10
261 *
262 * For PSKs provisioned via NewSessionTicket, a server MUST validate that
263 * the ticket age for the selected PSK identity (computed by subtracting
264 * ticket_age_add from PskIdentity.obfuscated_ticket_age modulo 2^32) is
265 * within a small tolerance of the time since the ticket was issued.
Jerry Yuf7dad3c2022-09-14 22:31:39 +0800266 *
Jerry Yu31b601a2023-11-10 11:27:21 +0800267 * NOTE: The typical accuracy of an RTC crystal is ±100 to ±20 parts per
268 * million (360 to 72 milliseconds per hour). Default tolerance
Jerry Yu9cb953a2023-11-15 09:54:11 +0800269 * window is 6s, thus in the worst case clients and servers must
Jerry Yu31b601a2023-11-10 11:27:21 +0800270 * sync up their system time every 6000/360/2~=8 hours.
Jerry Yu8d4bbba2022-09-13 14:15:48 +0800271 */
Jerry Yucebffc32022-12-15 18:00:05 +0800272 client_age = obfuscated_ticket_age - session->ticket_age_add;
Jerry Yu60e99722023-11-20 09:55:24 +0800273 age_diff = server_age - (mbedtls_ms_time_t) client_age;
Jerry Yu28e7c552023-11-10 12:05:56 +0800274 if (age_diff < -MBEDTLS_SSL_TLS1_3_TICKET_AGE_TOLERANCE ||
Jerry Yucebffc32022-12-15 18:00:05 +0800275 age_diff > MBEDTLS_SSL_TLS1_3_TICKET_AGE_TOLERANCE) {
Jerry Yu8d4bbba2022-09-13 14:15:48 +0800276 MBEDTLS_SSL_DEBUG_MSG(
Jerry Yuf16efbc2023-10-30 11:06:24 +0800277 3, ("Ticket age outside tolerance window ( diff = %"
278 MBEDTLS_PRINTF_MS_TIME ")",
Jerry Yucebffc32022-12-15 18:00:05 +0800279 age_diff));
Jerry Yu8d4bbba2022-09-13 14:15:48 +0800280 goto exit;
281 }
Jerry Yu95699e72022-08-21 19:22:23 +0800282#endif /* MBEDTLS_HAVE_TIME */
Jerry Yu8d4bbba2022-09-13 14:15:48 +0800283
Ronald Cronfbae94a2023-12-05 18:15:14 +0100284 ret = SSL_TLS1_3_PSK_IDENTITY_MATCH;
285
Jerry Yu8d4bbba2022-09-13 14:15:48 +0800286exit:
Ronald Cronfbae94a2023-12-05 18:15:14 +0100287 if (ret != SSL_TLS1_3_PSK_IDENTITY_MATCH) {
Gilles Peskine449bd832023-01-11 14:50:10 +0100288 mbedtls_ssl_session_free(session);
289 }
Jerry Yu95699e72022-08-21 19:22:23 +0800290
Gilles Peskine449bd832023-01-11 14:50:10 +0100291 MBEDTLS_SSL_DEBUG_MSG(2, ("<= check_identity_match_ticket"));
292 return ret;
Jerry Yu95699e72022-08-21 19:22:23 +0800293}
294#endif /* MBEDTLS_SSL_SESSION_TICKETS */
295
Jerry Yu6e74a7e2022-07-20 20:49:32 +0800296MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yu1c105562022-07-10 06:32:38 +0000297static int ssl_tls13_offered_psks_check_identity_match(
Gilles Peskine449bd832023-01-11 14:50:10 +0100298 mbedtls_ssl_context *ssl,
299 const unsigned char *identity,
300 size_t identity_len,
301 uint32_t obfuscated_ticket_age,
302 int *psk_type,
303 mbedtls_ssl_session *session)
Jerry Yu1c105562022-07-10 06:32:38 +0000304{
Ronald Cron606671e2023-02-17 11:36:33 +0100305 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
306
Jerry Yu95699e72022-08-21 19:22:23 +0800307 ((void) session);
308 ((void) obfuscated_ticket_age);
Jerry Yu5725f1c2022-08-21 17:27:16 +0800309 *psk_type = MBEDTLS_SSL_TLS1_3_PSK_EXTERNAL;
Jerry Yu95699e72022-08-21 19:22:23 +0800310
Gilles Peskine449bd832023-01-11 14:50:10 +0100311 MBEDTLS_SSL_DEBUG_BUF(4, "identity", identity, identity_len);
Jerry Yu95699e72022-08-21 19:22:23 +0800312 ssl->handshake->resume = 0;
313
Jerry Yu95699e72022-08-21 19:22:23 +0800314#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Ronald Cron7a30cf52024-02-23 14:38:59 +0100315 ret = ssl_tls13_offered_psks_check_identity_match_ticket(
316 ssl, identity, identity_len, obfuscated_ticket_age, session);
317 if (ret == SSL_TLS1_3_PSK_IDENTITY_MATCH) {
Jerry Yu95699e72022-08-21 19:22:23 +0800318 ssl->handshake->resume = 1;
319 *psk_type = MBEDTLS_SSL_TLS1_3_PSK_RESUMPTION;
Ronald Cron606671e2023-02-17 11:36:33 +0100320 ret = mbedtls_ssl_set_hs_psk(ssl,
321 session->resumption_key,
322 session->resumption_key_len);
323 if (ret != 0) {
324 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_set_hs_psk", ret);
325 return ret;
326 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100327
328 MBEDTLS_SSL_DEBUG_BUF(4, "Ticket-resumed PSK:",
329 session->resumption_key,
330 session->resumption_key_len);
331 MBEDTLS_SSL_DEBUG_MSG(4, ("ticket: obfuscated_ticket_age: %u",
332 (unsigned) obfuscated_ticket_age));
Ronald Cronfbae94a2023-12-05 18:15:14 +0100333 return SSL_TLS1_3_PSK_IDENTITY_MATCH;
Ronald Cron7a30cf52024-02-23 14:38:59 +0100334 } else if (ret == SSL_TLS1_3_PSK_IDENTITY_MATCH_BUT_PSK_NOT_USABLE) {
335 return SSL_TLS1_3_PSK_IDENTITY_MATCH_BUT_PSK_NOT_USABLE;
Jerry Yu95699e72022-08-21 19:22:23 +0800336 }
337#endif /* MBEDTLS_SSL_SESSION_TICKETS */
338
Jerry Yu1c105562022-07-10 06:32:38 +0000339 /* Check identity with external configured function */
Gilles Peskine449bd832023-01-11 14:50:10 +0100340 if (ssl->conf->f_psk != NULL) {
341 if (ssl->conf->f_psk(
342 ssl->conf->p_psk, ssl, identity, identity_len) == 0) {
Ronald Cronfbae94a2023-12-05 18:15:14 +0100343 return SSL_TLS1_3_PSK_IDENTITY_MATCH;
Jerry Yu1c105562022-07-10 06:32:38 +0000344 }
Ronald Cronfbae94a2023-12-05 18:15:14 +0100345 return SSL_TLS1_3_PSK_IDENTITY_DOES_NOT_MATCH;
Jerry Yu1c105562022-07-10 06:32:38 +0000346 }
347
Gilles Peskine449bd832023-01-11 14:50:10 +0100348 MBEDTLS_SSL_DEBUG_BUF(5, "identity", identity, identity_len);
Jerry Yu1c105562022-07-10 06:32:38 +0000349 /* Check identity with pre-configured psk */
Gilles Peskine449bd832023-01-11 14:50:10 +0100350 if (ssl->conf->psk_identity != NULL &&
Jerry Yu2f0abc92022-07-22 19:34:48 +0800351 identity_len == ssl->conf->psk_identity_len &&
Gilles Peskine449bd832023-01-11 14:50:10 +0100352 mbedtls_ct_memcmp(ssl->conf->psk_identity,
353 identity, identity_len) == 0) {
Ronald Cron606671e2023-02-17 11:36:33 +0100354 ret = mbedtls_ssl_set_hs_psk(ssl, ssl->conf->psk, ssl->conf->psk_len);
355 if (ret != 0) {
356 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_set_hs_psk", ret);
357 return ret;
358 }
Ronald Cronfbae94a2023-12-05 18:15:14 +0100359 return SSL_TLS1_3_PSK_IDENTITY_MATCH;
Jerry Yu1c105562022-07-10 06:32:38 +0000360 }
361
Ronald Cronfbae94a2023-12-05 18:15:14 +0100362 return SSL_TLS1_3_PSK_IDENTITY_DOES_NOT_MATCH;
Jerry Yu1c105562022-07-10 06:32:38 +0000363}
364
Ronald Cron6e311272023-12-05 17:57:01 +0100365#define SSL_TLS1_3_BINDER_DOES_NOT_MATCH 1
366#define SSL_TLS1_3_BINDER_MATCH 0
Jerry Yu6e74a7e2022-07-20 20:49:32 +0800367MBEDTLS_CHECK_RETURN_CRITICAL
Xiaokang Qian9f1747b2023-03-30 02:50:04 +0000368static int ssl_tls13_offered_psks_check_binder_match(
369 mbedtls_ssl_context *ssl,
370 const unsigned char *binder, size_t binder_len,
371 int psk_type, psa_algorithm_t psk_hash_alg)
Jerry Yu1c105562022-07-10 06:32:38 +0000372{
373 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yu96a2e362022-07-21 15:11:34 +0800374
Jerry Yudaf375a2022-07-20 21:31:43 +0800375 unsigned char transcript[PSA_HASH_MAX_SIZE];
Jerry Yu1c105562022-07-10 06:32:38 +0000376 size_t transcript_len;
Jerry Yu2f0abc92022-07-22 19:34:48 +0800377 unsigned char *psk;
Jerry Yu96a2e362022-07-21 15:11:34 +0800378 size_t psk_len;
Jerry Yudaf375a2022-07-20 21:31:43 +0800379 unsigned char server_computed_binder[PSA_HASH_MAX_SIZE];
Jerry Yu1c105562022-07-10 06:32:38 +0000380
Jerry Yu1c105562022-07-10 06:32:38 +0000381 /* Get current state of handshake transcript. */
Jerry Yu29d9faa2022-08-23 17:52:45 +0800382 ret = mbedtls_ssl_get_handshake_transcript(
Manuel Pégourié-Gonnard2d6d9932023-03-28 11:38:08 +0200383 ssl, mbedtls_md_type_from_psa_alg(psk_hash_alg),
Gilles Peskine449bd832023-01-11 14:50:10 +0100384 transcript, sizeof(transcript), &transcript_len);
385 if (ret != 0) {
386 return ret;
387 }
Jerry Yu1c105562022-07-10 06:32:38 +0000388
Gilles Peskine449bd832023-01-11 14:50:10 +0100389 ret = mbedtls_ssl_tls13_export_handshake_psk(ssl, &psk, &psk_len);
390 if (ret != 0) {
391 return ret;
392 }
Jerry Yu96a2e362022-07-21 15:11:34 +0800393
Gilles Peskine449bd832023-01-11 14:50:10 +0100394 ret = mbedtls_ssl_tls13_create_psk_binder(ssl, psk_hash_alg,
395 psk, psk_len, psk_type,
396 transcript,
397 server_computed_binder);
Jerry Yu96a2e362022-07-21 15:11:34 +0800398#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +0100399 mbedtls_free((void *) psk);
Jerry Yu96a2e362022-07-21 15:11:34 +0800400#endif
Gilles Peskine449bd832023-01-11 14:50:10 +0100401 if (ret != 0) {
402 MBEDTLS_SSL_DEBUG_MSG(1, ("PSK binder calculation failed."));
403 return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
Jerry Yu1c105562022-07-10 06:32:38 +0000404 }
405
Gilles Peskine449bd832023-01-11 14:50:10 +0100406 MBEDTLS_SSL_DEBUG_BUF(3, "psk binder ( computed ): ",
407 server_computed_binder, transcript_len);
408 MBEDTLS_SSL_DEBUG_BUF(3, "psk binder ( received ): ", binder, binder_len);
Jerry Yu1c105562022-07-10 06:32:38 +0000409
Gilles Peskine449bd832023-01-11 14:50:10 +0100410 if (mbedtls_ct_memcmp(server_computed_binder, binder, binder_len) == 0) {
Ronald Cron6e311272023-12-05 17:57:01 +0100411 return SSL_TLS1_3_BINDER_MATCH;
Jerry Yu1c105562022-07-10 06:32:38 +0000412 }
413
Gilles Peskine449bd832023-01-11 14:50:10 +0100414 mbedtls_platform_zeroize(server_computed_binder,
415 sizeof(server_computed_binder));
Ronald Cron6e311272023-12-05 17:57:01 +0100416 return SSL_TLS1_3_BINDER_DOES_NOT_MATCH;
Jerry Yu1c105562022-07-10 06:32:38 +0000417}
Jerry Yu96a2e362022-07-21 15:11:34 +0800418
Jerry Yu82534862022-08-30 10:42:33 +0800419#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Jerry Yuf35ba382022-08-23 17:58:26 +0800420MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100421static int ssl_tls13_session_copy_ticket(mbedtls_ssl_session *dst,
422 const mbedtls_ssl_session *src)
Jerry Yu82534862022-08-30 10:42:33 +0800423{
Jerry Yu82534862022-08-30 10:42:33 +0800424 dst->ticket_age_add = src->ticket_age_add;
425 dst->ticket_flags = src->ticket_flags;
426 dst->resumption_key_len = src->resumption_key_len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100427 if (src->resumption_key_len == 0) {
428 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
429 }
430 memcpy(dst->resumption_key, src->resumption_key, src->resumption_key_len);
Jerry Yu4746b102022-09-13 11:11:48 +0800431
Jerry Yu33bf2402022-12-12 16:01:43 +0800432#if defined(MBEDTLS_SSL_EARLY_DATA)
433 dst->max_early_data_size = src->max_early_data_size;
434#endif
435
Gilles Peskine449bd832023-01-11 14:50:10 +0100436 return 0;
Jerry Yu82534862022-08-30 10:42:33 +0800437}
438#endif /* MBEDTLS_SSL_SESSION_TICKETS */
439
Jerry Yu1c105562022-07-10 06:32:38 +0000440/* Parser for pre_shared_key extension in client hello
441 * struct {
442 * opaque identity<1..2^16-1>;
443 * uint32 obfuscated_ticket_age;
444 * } PskIdentity;
445 *
446 * opaque PskBinderEntry<32..255>;
447 *
448 * struct {
449 * PskIdentity identities<7..2^16-1>;
450 * PskBinderEntry binders<33..2^16-1>;
451 * } OfferedPsks;
452 *
453 * struct {
454 * select (Handshake.msg_type) {
455 * case client_hello: OfferedPsks;
456 * ....
457 * };
458 * } PreSharedKeyExtension;
459 */
Jerry Yu6e74a7e2022-07-20 20:49:32 +0800460MBEDTLS_CHECK_RETURN_CRITICAL
Xiaokang Qian9f1747b2023-03-30 02:50:04 +0000461static int ssl_tls13_parse_pre_shared_key_ext(
462 mbedtls_ssl_context *ssl,
463 const unsigned char *pre_shared_key_ext,
464 const unsigned char *pre_shared_key_ext_end,
465 const unsigned char *ciphersuites,
466 const unsigned char *ciphersuites_end)
Jerry Yu1c105562022-07-10 06:32:38 +0000467{
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +0100468 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yu29d9faa2022-08-23 17:52:45 +0800469 const unsigned char *identities = pre_shared_key_ext;
Jerry Yu568ec252022-07-22 21:27:34 +0800470 const unsigned char *p_identity_len;
471 size_t identities_len;
Jerry Yu1c105562022-07-10 06:32:38 +0000472 const unsigned char *identities_end;
Jerry Yu568ec252022-07-22 21:27:34 +0800473 const unsigned char *binders;
474 const unsigned char *p_binder_len;
475 size_t binders_len;
Jerry Yu1c105562022-07-10 06:32:38 +0000476 const unsigned char *binders_end;
Jerry Yu96a2e362022-07-21 15:11:34 +0800477 int matched_identity = -1;
478 int identity_id = -1;
Jerry Yu1c105562022-07-10 06:32:38 +0000479
Gilles Peskine449bd832023-01-11 14:50:10 +0100480 MBEDTLS_SSL_DEBUG_BUF(3, "pre_shared_key extension",
481 pre_shared_key_ext,
482 pre_shared_key_ext_end - pre_shared_key_ext);
Jerry Yu1c105562022-07-10 06:32:38 +0000483
Jerry Yu96a2e362022-07-21 15:11:34 +0800484 /* identities_len 2 bytes
485 * identities_data >= 7 bytes
486 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100487 MBEDTLS_SSL_CHK_BUF_READ_PTR(identities, pre_shared_key_ext_end, 7 + 2);
488 identities_len = MBEDTLS_GET_UINT16_BE(identities, 0);
Jerry Yu568ec252022-07-22 21:27:34 +0800489 p_identity_len = identities + 2;
Gilles Peskine449bd832023-01-11 14:50:10 +0100490 MBEDTLS_SSL_CHK_BUF_READ_PTR(p_identity_len, pre_shared_key_ext_end,
491 identities_len);
Jerry Yu568ec252022-07-22 21:27:34 +0800492 identities_end = p_identity_len + identities_len;
Jerry Yu1c105562022-07-10 06:32:38 +0000493
Jerry Yu96a2e362022-07-21 15:11:34 +0800494 /* binders_len 2 bytes
495 * binders >= 33 bytes
496 */
Jerry Yu568ec252022-07-22 21:27:34 +0800497 binders = identities_end;
Gilles Peskine449bd832023-01-11 14:50:10 +0100498 MBEDTLS_SSL_CHK_BUF_READ_PTR(binders, pre_shared_key_ext_end, 33 + 2);
499 binders_len = MBEDTLS_GET_UINT16_BE(binders, 0);
Jerry Yu568ec252022-07-22 21:27:34 +0800500 p_binder_len = binders + 2;
Gilles Peskine449bd832023-01-11 14:50:10 +0100501 MBEDTLS_SSL_CHK_BUF_READ_PTR(p_binder_len, pre_shared_key_ext_end, binders_len);
Jerry Yu568ec252022-07-22 21:27:34 +0800502 binders_end = p_binder_len + binders_len;
Jerry Yu1c105562022-07-10 06:32:38 +0000503
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +0100504 ret = ssl->handshake->update_checksum(ssl, pre_shared_key_ext,
505 identities_end - pre_shared_key_ext);
506 if (0 != ret) {
507 MBEDTLS_SSL_DEBUG_RET(1, ("update_checksum"), ret);
508 return ret;
509 }
Jerry Yu96a2e362022-07-21 15:11:34 +0800510
Gilles Peskine449bd832023-01-11 14:50:10 +0100511 while (p_identity_len < identities_end && p_binder_len < binders_end) {
Jerry Yu1c105562022-07-10 06:32:38 +0000512 const unsigned char *identity;
Jerry Yu568ec252022-07-22 21:27:34 +0800513 size_t identity_len;
Jerry Yu82534862022-08-30 10:42:33 +0800514 uint32_t obfuscated_ticket_age;
Jerry Yu96a2e362022-07-21 15:11:34 +0800515 const unsigned char *binder;
Jerry Yu568ec252022-07-22 21:27:34 +0800516 size_t binder_len;
Jerry Yu5725f1c2022-08-21 17:27:16 +0800517 int psk_type;
Ronald Cron89089cc2024-02-14 18:18:08 +0100518 int psk_ciphersuite_id;
519 psa_algorithm_t psk_hash_alg;
Ronald Croncf284562024-02-16 18:54:10 +0100520 int allowed_key_exchange_modes;
521 int key_exchange_mode = MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_NONE;
Jerry Yu29d9faa2022-08-23 17:52:45 +0800522 const mbedtls_ssl_ciphersuite_t *ciphersuite_info;
Jerry Yu82534862022-08-30 10:42:33 +0800523#if defined(MBEDTLS_SSL_SESSION_TICKETS)
524 mbedtls_ssl_session session;
Gilles Peskine449bd832023-01-11 14:50:10 +0100525 mbedtls_ssl_session_init(&session);
Jerry Yu82534862022-08-30 10:42:33 +0800526#endif
Jerry Yubb852022022-07-20 21:10:44 +0800527
Gilles Peskine449bd832023-01-11 14:50:10 +0100528 MBEDTLS_SSL_CHK_BUF_READ_PTR(p_identity_len, identities_end, 2 + 1 + 4);
529 identity_len = MBEDTLS_GET_UINT16_BE(p_identity_len, 0);
Jerry Yu568ec252022-07-22 21:27:34 +0800530 identity = p_identity_len + 2;
Gilles Peskine449bd832023-01-11 14:50:10 +0100531 MBEDTLS_SSL_CHK_BUF_READ_PTR(identity, identities_end, identity_len + 4);
532 obfuscated_ticket_age = MBEDTLS_GET_UINT32_BE(identity, identity_len);
Jerry Yu568ec252022-07-22 21:27:34 +0800533 p_identity_len += identity_len + 6;
Jerry Yu1c105562022-07-10 06:32:38 +0000534
Gilles Peskine449bd832023-01-11 14:50:10 +0100535 MBEDTLS_SSL_CHK_BUF_READ_PTR(p_binder_len, binders_end, 1 + 32);
Jerry Yu568ec252022-07-22 21:27:34 +0800536 binder_len = *p_binder_len;
537 binder = p_binder_len + 1;
Gilles Peskine449bd832023-01-11 14:50:10 +0100538 MBEDTLS_SSL_CHK_BUF_READ_PTR(binder, binders_end, binder_len);
Jerry Yu568ec252022-07-22 21:27:34 +0800539 p_binder_len += binder_len + 1;
Jerry Yu96a2e362022-07-21 15:11:34 +0800540
Jerry Yu96a2e362022-07-21 15:11:34 +0800541 identity_id++;
Gilles Peskine449bd832023-01-11 14:50:10 +0100542 if (matched_identity != -1) {
Jerry Yu1c105562022-07-10 06:32:38 +0000543 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +0100544 }
Jerry Yu1c105562022-07-10 06:32:38 +0000545
Jerry Yu96a2e362022-07-21 15:11:34 +0800546 ret = ssl_tls13_offered_psks_check_identity_match(
Gilles Peskine449bd832023-01-11 14:50:10 +0100547 ssl, identity, identity_len, obfuscated_ticket_age,
548 &psk_type, &session);
Ronald Cronfbae94a2023-12-05 18:15:14 +0100549 if (ret != SSL_TLS1_3_PSK_IDENTITY_MATCH) {
Jerry Yu96a2e362022-07-21 15:11:34 +0800550 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +0100551 }
Jerry Yu96a2e362022-07-21 15:11:34 +0800552
Gilles Peskine449bd832023-01-11 14:50:10 +0100553 MBEDTLS_SSL_DEBUG_MSG(4, ("found matched identity"));
Ronald Cron89089cc2024-02-14 18:18:08 +0100554
Gilles Peskine449bd832023-01-11 14:50:10 +0100555 switch (psk_type) {
Jerry Yu0baf9072022-08-25 11:21:04 +0800556 case MBEDTLS_SSL_TLS1_3_PSK_EXTERNAL:
Ronald Cron89089cc2024-02-14 18:18:08 +0100557 psk_ciphersuite_id = 0;
558 psk_hash_alg = PSA_ALG_SHA_256;
Ronald Croncf284562024-02-16 18:54:10 +0100559 allowed_key_exchange_modes =
560 MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK_ALL;
Jerry Yu0baf9072022-08-25 11:21:04 +0800561 break;
Jerry Yu82534862022-08-30 10:42:33 +0800562#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Ronald Cronf7e99162024-02-14 16:04:02 +0100563 case MBEDTLS_SSL_TLS1_3_PSK_RESUMPTION:
Ronald Cron89089cc2024-02-14 18:18:08 +0100564 psk_ciphersuite_id = session.ciphersuite;
565 psk_hash_alg = PSA_ALG_NONE;
Ronald Croncf284562024-02-16 18:54:10 +0100566 ssl->session_negotiate->ticket_flags = session.ticket_flags;
567 allowed_key_exchange_modes =
568 session.ticket_flags &
569 MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK_ALL;
Jerry Yu0baf9072022-08-25 11:21:04 +0800570 break;
Ronald Cronf7e99162024-02-14 16:04:02 +0100571#endif
Jerry Yu0baf9072022-08-25 11:21:04 +0800572 default:
Gilles Peskine449bd832023-01-11 14:50:10 +0100573 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Jerry Yu0baf9072022-08-25 11:21:04 +0800574 }
Ronald Cron89089cc2024-02-14 18:18:08 +0100575
Ronald Croncf284562024-02-16 18:54:10 +0100576 if ((allowed_key_exchange_modes &
577 MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK_EPHEMERAL) &&
578 ssl_tls13_key_exchange_is_psk_ephemeral_available(ssl)) {
579 key_exchange_mode = MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK_EPHEMERAL;
580 } else if ((allowed_key_exchange_modes &
581 MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK) &&
582 ssl_tls13_key_exchange_is_psk_available(ssl)) {
583 key_exchange_mode = MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK;
584 }
585
586 if (key_exchange_mode == MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_NONE) {
587 MBEDTLS_SSL_DEBUG_MSG(3, ("No suitable PSK key exchange mode"));
588 continue;
589 }
590
Ronald Cron89089cc2024-02-14 18:18:08 +0100591 ssl_tls13_select_ciphersuite(ssl, ciphersuites, ciphersuites_end,
592 psk_ciphersuite_id, psk_hash_alg,
593 &ciphersuite_info);
594
595 if (ciphersuite_info == NULL) {
596#if defined(MBEDTLS_SSL_SESSION_TICKETS)
597 mbedtls_ssl_session_free(&session);
598#endif
599 /*
600 * We consider finding a ciphersuite suitable for the PSK as part
601 * of the validation of its binder. Thus if we do not find one, we
602 * abort the handshake with a decrypt_error alert.
603 */
Jerry Yuf35ba382022-08-23 17:58:26 +0800604 MBEDTLS_SSL_PEND_FATAL_ALERT(
605 MBEDTLS_SSL_ALERT_MSG_DECRYPT_ERROR,
Gilles Peskine449bd832023-01-11 14:50:10 +0100606 MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE);
Ronald Cron89089cc2024-02-14 18:18:08 +0100607 return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
Jerry Yu5725f1c2022-08-21 17:27:16 +0800608 }
609
Jerry Yu96a2e362022-07-21 15:11:34 +0800610 ret = ssl_tls13_offered_psks_check_binder_match(
Gilles Peskine449bd832023-01-11 14:50:10 +0100611 ssl, binder, binder_len, psk_type,
Dave Rodgman2eab4622023-10-05 13:30:37 +0100612 mbedtls_md_psa_alg_from_type((mbedtls_md_type_t) ciphersuite_info->mac));
Ronald Cron6e311272023-12-05 17:57:01 +0100613 if (ret != SSL_TLS1_3_BINDER_MATCH) {
Jerry Yuc5a23a02022-08-25 10:51:44 +0800614 /* For security reasons, the handshake should be aborted when we
615 * fail to validate a binder value. See RFC 8446 section 4.2.11.2
616 * and appendix E.6. */
Jerry Yu82534862022-08-30 10:42:33 +0800617#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Gilles Peskine449bd832023-01-11 14:50:10 +0100618 mbedtls_ssl_session_free(&session);
Jerry Yu82534862022-08-30 10:42:33 +0800619#endif
Gilles Peskine449bd832023-01-11 14:50:10 +0100620 MBEDTLS_SSL_DEBUG_MSG(3, ("Invalid binder."));
Xiaokang Qian9f1747b2023-03-30 02:50:04 +0000621 MBEDTLS_SSL_DEBUG_RET(
622 1, "ssl_tls13_offered_psks_check_binder_match", ret);
Jerry Yu96a2e362022-07-21 15:11:34 +0800623 MBEDTLS_SSL_PEND_FATAL_ALERT(
624 MBEDTLS_SSL_ALERT_MSG_DECRYPT_ERROR,
Gilles Peskine449bd832023-01-11 14:50:10 +0100625 MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE);
626 return ret;
Jerry Yu96a2e362022-07-21 15:11:34 +0800627 }
Jerry Yu96a2e362022-07-21 15:11:34 +0800628
629 matched_identity = identity_id;
Jerry Yu5725f1c2022-08-21 17:27:16 +0800630
631 /* Update handshake parameters */
Jerry Yue5834fd2022-08-29 20:16:09 +0800632 ssl->handshake->ciphersuite_info = ciphersuite_info;
Ronald Cron89089cc2024-02-14 18:18:08 +0100633 ssl->session_negotiate->ciphersuite = ciphersuite_info->id;
Gilles Peskine449bd832023-01-11 14:50:10 +0100634 MBEDTLS_SSL_DEBUG_MSG(2, ("overwrite ciphersuite: %04x - %s",
Ronald Cron89089cc2024-02-14 18:18:08 +0100635 ((unsigned) ciphersuite_info->id),
636 ciphersuite_info->name));
Jerry Yu82534862022-08-30 10:42:33 +0800637#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Gilles Peskine449bd832023-01-11 14:50:10 +0100638 if (psk_type == MBEDTLS_SSL_TLS1_3_PSK_RESUMPTION) {
639 ret = ssl_tls13_session_copy_ticket(ssl->session_negotiate,
640 &session);
641 mbedtls_ssl_session_free(&session);
642 if (ret != 0) {
643 return ret;
644 }
Jerry Yu82534862022-08-30 10:42:33 +0800645 }
646#endif /* MBEDTLS_SSL_SESSION_TICKETS */
Jerry Yu1c105562022-07-10 06:32:38 +0000647 }
648
Gilles Peskine449bd832023-01-11 14:50:10 +0100649 if (p_identity_len != identities_end || p_binder_len != binders_end) {
650 MBEDTLS_SSL_DEBUG_MSG(3, ("pre_shared_key extension decode error"));
651 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR,
652 MBEDTLS_ERR_SSL_DECODE_ERROR);
653 return MBEDTLS_ERR_SSL_DECODE_ERROR;
Jerry Yu1c105562022-07-10 06:32:38 +0000654 }
655
Jerry Yu6f1db3f2022-07-22 23:05:59 +0800656 /* Update the handshake transcript with the binder list. */
Xiaokang Qian9f1747b2023-03-30 02:50:04 +0000657 ret = ssl->handshake->update_checksum(
658 ssl, identities_end, (size_t) (binders_end - identities_end));
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +0100659 if (0 != ret) {
660 MBEDTLS_SSL_DEBUG_RET(1, ("update_checksum"), ret);
661 return ret;
662 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100663 if (matched_identity == -1) {
Ronald Croncf284562024-02-16 18:54:10 +0100664 MBEDTLS_SSL_DEBUG_MSG(3, ("No usable PSK or ticket."));
Gilles Peskine449bd832023-01-11 14:50:10 +0100665 return MBEDTLS_ERR_SSL_UNKNOWN_IDENTITY;
Jerry Yu1c105562022-07-10 06:32:38 +0000666 }
667
Gilles Peskine449bd832023-01-11 14:50:10 +0100668 ssl->handshake->selected_identity = (uint16_t) matched_identity;
669 MBEDTLS_SSL_DEBUG_MSG(3, ("Pre shared key found"));
Jerry Yu1c105562022-07-10 06:32:38 +0000670
Gilles Peskine449bd832023-01-11 14:50:10 +0100671 return 0;
Jerry Yu1c105562022-07-10 06:32:38 +0000672}
Jerry Yu032b15ce2022-07-11 06:10:03 +0000673
674/*
675 * struct {
676 * select ( Handshake.msg_type ) {
677 * ....
678 * case server_hello:
679 * uint16 selected_identity;
680 * }
681 * } PreSharedKeyExtension;
682 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100683static int ssl_tls13_write_server_pre_shared_key_ext(mbedtls_ssl_context *ssl,
684 unsigned char *buf,
685 unsigned char *end,
686 size_t *olen)
Jerry Yu032b15ce2022-07-11 06:10:03 +0000687{
Gilles Peskine449bd832023-01-11 14:50:10 +0100688 unsigned char *p = (unsigned char *) buf;
Jerry Yu032b15ce2022-07-11 06:10:03 +0000689
690 *olen = 0;
691
David Horstmann21b89762022-10-06 18:34:28 +0100692 int not_using_psk = 0;
Jerry Yu032b15ce2022-07-11 06:10:03 +0000693#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +0100694 not_using_psk = (mbedtls_svc_key_id_is_null(ssl->handshake->psk_opaque));
Jerry Yu032b15ce2022-07-11 06:10:03 +0000695#else
Gilles Peskine449bd832023-01-11 14:50:10 +0100696 not_using_psk = (ssl->handshake->psk == NULL);
Jerry Yu032b15ce2022-07-11 06:10:03 +0000697#endif
Gilles Peskine449bd832023-01-11 14:50:10 +0100698 if (not_using_psk) {
Jerry Yu032b15ce2022-07-11 06:10:03 +0000699 /* We shouldn't have called this extension writer unless we've
700 * chosen to use a PSK. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100701 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Jerry Yu032b15ce2022-07-11 06:10:03 +0000702 }
703
Gilles Peskine449bd832023-01-11 14:50:10 +0100704 MBEDTLS_SSL_DEBUG_MSG(3, ("server hello, adding pre_shared_key extension"));
705 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 6);
Jerry Yu032b15ce2022-07-11 06:10:03 +0000706
Gilles Peskine449bd832023-01-11 14:50:10 +0100707 MBEDTLS_PUT_UINT16_BE(MBEDTLS_TLS_EXT_PRE_SHARED_KEY, p, 0);
708 MBEDTLS_PUT_UINT16_BE(2, p, 2);
Jerry Yu032b15ce2022-07-11 06:10:03 +0000709
Gilles Peskine449bd832023-01-11 14:50:10 +0100710 MBEDTLS_PUT_UINT16_BE(ssl->handshake->selected_identity, p, 4);
Jerry Yu032b15ce2022-07-11 06:10:03 +0000711
712 *olen = 6;
713
Gilles Peskine449bd832023-01-11 14:50:10 +0100714 MBEDTLS_SSL_DEBUG_MSG(4, ("sent selected_identity: %u",
715 ssl->handshake->selected_identity));
Jerry Yu032b15ce2022-07-11 06:10:03 +0000716
Gilles Peskine449bd832023-01-11 14:50:10 +0100717 mbedtls_ssl_tls13_set_hs_sent_ext_mask(ssl, MBEDTLS_TLS_EXT_PRE_SHARED_KEY);
Jerry Yub95dd362022-11-08 21:19:34 +0800718
Gilles Peskine449bd832023-01-11 14:50:10 +0100719 return 0;
Jerry Yu032b15ce2022-07-11 06:10:03 +0000720}
721
Ronald Cron41a443a2022-10-04 16:38:25 +0200722#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED */
Jerry Yue19e3b92022-07-08 12:04:51 +0000723
XiaokangQian7807f9f2022-02-15 10:04:37 +0000724/* From RFC 8446:
725 * struct {
XiaokangQiancfd925f2022-04-14 07:10:37 +0000726 * ProtocolVersion versions<2..254>;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000727 * } SupportedVersions;
728 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200729MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100730static int ssl_tls13_parse_supported_versions_ext(mbedtls_ssl_context *ssl,
731 const unsigned char *buf,
732 const unsigned char *end)
XiaokangQian7807f9f2022-02-15 10:04:37 +0000733{
XiaokangQian7807f9f2022-02-15 10:04:37 +0000734 const unsigned char *p = buf;
XiaokangQian8f9dfe42022-04-15 02:52:39 +0000735 size_t versions_len;
XiaokangQian4080a7f2022-04-11 09:55:18 +0000736 const unsigned char *versions_end;
XiaokangQian0a1b54e2022-04-21 03:01:38 +0000737 uint16_t tls_version;
Ronald Cron5af4c7f2023-03-07 20:46:59 +0100738 int found_supported_version = 0;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000739
Gilles Peskine449bd832023-01-11 14:50:10 +0100740 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, 1);
XiaokangQian4080a7f2022-04-11 09:55:18 +0000741 versions_len = p[0];
XiaokangQian7807f9f2022-02-15 10:04:37 +0000742 p += 1;
743
Gilles Peskine449bd832023-01-11 14:50:10 +0100744 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, versions_len);
XiaokangQian4080a7f2022-04-11 09:55:18 +0000745 versions_end = p + versions_len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100746 while (p < versions_end) {
747 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, versions_end, 2);
748 tls_version = mbedtls_ssl_read_version(p, ssl->conf->transport);
XiaokangQianb67384d2022-04-19 00:02:38 +0000749 p += 2;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000750
Ronald Cron3bd2b022023-04-03 16:45:39 +0200751 if (MBEDTLS_SSL_VERSION_TLS1_3 == tls_version) {
Ronald Cron5af4c7f2023-03-07 20:46:59 +0100752 found_supported_version = 1;
753 break;
754 }
755
Ronald Cron3bd2b022023-04-03 16:45:39 +0200756 if ((MBEDTLS_SSL_VERSION_TLS1_2 == tls_version) &&
757 mbedtls_ssl_conf_is_tls12_enabled(ssl->conf)) {
Ronald Cron5af4c7f2023-03-07 20:46:59 +0100758 found_supported_version = 1;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000759 break;
760 }
XiaokangQian7807f9f2022-02-15 10:04:37 +0000761 }
762
Ronald Cron5af4c7f2023-03-07 20:46:59 +0100763 if (!found_supported_version) {
764 MBEDTLS_SSL_DEBUG_MSG(1, ("No supported version found."));
XiaokangQian7807f9f2022-02-15 10:04:37 +0000765
Gilles Peskine449bd832023-01-11 14:50:10 +0100766 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_PROTOCOL_VERSION,
767 MBEDTLS_ERR_SSL_BAD_PROTOCOL_VERSION);
768 return MBEDTLS_ERR_SSL_BAD_PROTOCOL_VERSION;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000769 }
770
Ronald Cron5af4c7f2023-03-07 20:46:59 +0100771 MBEDTLS_SSL_DEBUG_MSG(1, ("Negotiated version: [%04x]",
Gilles Peskine449bd832023-01-11 14:50:10 +0100772 (unsigned int) tls_version));
XiaokangQian7807f9f2022-02-15 10:04:37 +0000773
Ronald Cron5af4c7f2023-03-07 20:46:59 +0100774 return (int) tls_version;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000775}
776
Przemek Stekiel8c0a9532023-06-15 16:48:19 +0200777#if defined(PSA_WANT_ALG_ECDH) || defined(PSA_WANT_ALG_FFDH)
XiaokangQiane8ff3502022-04-22 02:34:40 +0000778/*
XiaokangQian7807f9f2022-02-15 10:04:37 +0000779 *
780 * From RFC 8446:
781 * enum {
782 * ... (0xFFFF)
783 * } NamedGroup;
784 * struct {
785 * NamedGroup named_group_list<2..2^16-1>;
786 * } NamedGroupList;
787 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200788MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100789static int ssl_tls13_parse_supported_groups_ext(mbedtls_ssl_context *ssl,
790 const unsigned char *buf,
791 const unsigned char *end)
XiaokangQian7807f9f2022-02-15 10:04:37 +0000792{
XiaokangQian7807f9f2022-02-15 10:04:37 +0000793 const unsigned char *p = buf;
XiaokangQian84823772022-04-19 07:57:30 +0000794 size_t named_group_list_len;
XiaokangQian8f9dfe42022-04-15 02:52:39 +0000795 const unsigned char *named_group_list_end;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000796
Gilles Peskine449bd832023-01-11 14:50:10 +0100797 MBEDTLS_SSL_DEBUG_BUF(3, "supported_groups extension", p, end - buf);
798 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, 2);
799 named_group_list_len = MBEDTLS_GET_UINT16_BE(p, 0);
XiaokangQian7807f9f2022-02-15 10:04:37 +0000800 p += 2;
Gilles Peskine449bd832023-01-11 14:50:10 +0100801 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, named_group_list_len);
XiaokangQian8f9dfe42022-04-15 02:52:39 +0000802 named_group_list_end = p + named_group_list_len;
XiaokangQian4e8cd7b2022-04-21 09:48:09 +0000803 ssl->handshake->hrr_selected_group = 0;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000804
Gilles Peskine449bd832023-01-11 14:50:10 +0100805 while (p < named_group_list_end) {
XiaokangQian08037552022-04-20 07:16:41 +0000806 uint16_t named_group;
Gilles Peskine449bd832023-01-11 14:50:10 +0100807 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, named_group_list_end, 2);
808 named_group = MBEDTLS_GET_UINT16_BE(p, 0);
XiaokangQian08037552022-04-20 07:16:41 +0000809 p += 2;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000810
Gilles Peskine449bd832023-01-11 14:50:10 +0100811 MBEDTLS_SSL_DEBUG_MSG(2,
812 ("got named group: %s(%04x)",
813 mbedtls_ssl_named_group_to_str(named_group),
814 named_group));
XiaokangQian08037552022-04-20 07:16:41 +0000815
Gilles Peskine449bd832023-01-11 14:50:10 +0100816 if (!mbedtls_ssl_named_group_is_offered(ssl, named_group) ||
817 !mbedtls_ssl_named_group_is_supported(named_group) ||
818 ssl->handshake->hrr_selected_group != 0) {
XiaokangQian08037552022-04-20 07:16:41 +0000819 continue;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000820 }
821
Gilles Peskine449bd832023-01-11 14:50:10 +0100822 MBEDTLS_SSL_DEBUG_MSG(2,
823 ("add named group %s(%04x) into received list.",
824 mbedtls_ssl_named_group_to_str(named_group),
825 named_group));
Jerry Yuc1be19f2022-04-23 16:11:39 +0800826
XiaokangQian4e8cd7b2022-04-21 09:48:09 +0000827 ssl->handshake->hrr_selected_group = named_group;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000828 }
829
Gilles Peskine449bd832023-01-11 14:50:10 +0100830 return 0;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000831
832}
Przemek Stekiel8c0a9532023-06-15 16:48:19 +0200833#endif /* PSA_WANT_ALG_ECDH || PSA_WANT_ALG_FFDH */
XiaokangQian7807f9f2022-02-15 10:04:37 +0000834
XiaokangQian08037552022-04-20 07:16:41 +0000835#define SSL_TLS1_3_PARSE_KEY_SHARES_EXT_NO_MATCH 1
836
Valerio Settic9ae8622023-07-25 11:23:50 +0200837#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_EPHEMERAL_ENABLED)
XiaokangQian7807f9f2022-02-15 10:04:37 +0000838/*
839 * ssl_tls13_parse_key_shares_ext() verifies whether the information in the
Xiaokang Qian9f1747b2023-03-30 02:50:04 +0000840 * extension is correct and stores the first acceptable key share and its
841 * associated group.
XiaokangQian7807f9f2022-02-15 10:04:37 +0000842 *
843 * Possible return values are:
844 * - 0: Successful processing of the client provided key share extension.
Xiaokang Qian9f1747b2023-03-30 02:50:04 +0000845 * - SSL_TLS1_3_PARSE_KEY_SHARES_EXT_NO_MATCH: The key shares provided by
846 * the client does not match a group supported by the server. A
847 * HelloRetryRequest will be needed.
XiaokangQiane8ff3502022-04-22 02:34:40 +0000848 * - A negative value for fatal errors.
Jerry Yuc1be19f2022-04-23 16:11:39 +0800849 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200850MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100851static int ssl_tls13_parse_key_shares_ext(mbedtls_ssl_context *ssl,
852 const unsigned char *buf,
853 const unsigned char *end)
XiaokangQian7807f9f2022-02-15 10:04:37 +0000854{
XiaokangQianb67384d2022-04-19 00:02:38 +0000855 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000856 unsigned char const *p = buf;
XiaokangQian8f9dfe42022-04-15 02:52:39 +0000857 unsigned char const *client_shares_end;
Jerry Yuc1be19f2022-04-23 16:11:39 +0800858 size_t client_shares_len;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000859
860 /* From RFC 8446:
861 *
862 * struct {
863 * KeyShareEntry client_shares<0..2^16-1>;
864 * } KeyShareClientHello;
865 *
866 */
867
Gilles Peskine449bd832023-01-11 14:50:10 +0100868 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, 2);
869 client_shares_len = MBEDTLS_GET_UINT16_BE(p, 0);
XiaokangQian7807f9f2022-02-15 10:04:37 +0000870 p += 2;
Gilles Peskine449bd832023-01-11 14:50:10 +0100871 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, client_shares_len);
XiaokangQian7807f9f2022-02-15 10:04:37 +0000872
873 ssl->handshake->offered_group_id = 0;
XiaokangQian8f9dfe42022-04-15 02:52:39 +0000874 client_shares_end = p + client_shares_len;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000875
876 /* We try to find a suitable key share entry and copy it to the
877 * handshake context. Later, we have to find out whether we can do
878 * something with the provided key share or whether we have to
XiaokangQianc5763b52022-04-02 03:34:37 +0000879 * dismiss it and send a HelloRetryRequest message.
880 */
XiaokangQian7807f9f2022-02-15 10:04:37 +0000881
Gilles Peskine449bd832023-01-11 14:50:10 +0100882 while (p < client_shares_end) {
XiaokangQian9b5d04b2022-04-10 10:20:43 +0000883 uint16_t group;
Jerry Yuc1be19f2022-04-23 16:11:39 +0800884 size_t key_exchange_len;
Jerry Yu086edc22022-05-05 10:50:38 +0800885 const unsigned char *key_exchange;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000886
887 /*
888 * struct {
889 * NamedGroup group;
890 * opaque key_exchange<1..2^16-1>;
891 * } KeyShareEntry;
892 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100893 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, client_shares_end, 4);
894 group = MBEDTLS_GET_UINT16_BE(p, 0);
895 key_exchange_len = MBEDTLS_GET_UINT16_BE(p, 2);
Jerry Yuc1be19f2022-04-23 16:11:39 +0800896 p += 4;
Jerry Yu086edc22022-05-05 10:50:38 +0800897 key_exchange = p;
Gilles Peskine449bd832023-01-11 14:50:10 +0100898 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, client_shares_end, key_exchange_len);
Jerry Yu086edc22022-05-05 10:50:38 +0800899 p += key_exchange_len;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000900
901 /* Continue parsing even if we have already found a match,
XiaokangQianc5763b52022-04-02 03:34:37 +0000902 * for input validation purposes.
903 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100904 if (!mbedtls_ssl_named_group_is_offered(ssl, group) ||
905 !mbedtls_ssl_named_group_is_supported(group) ||
906 ssl->handshake->offered_group_id != 0) {
XiaokangQian060d8672022-04-21 09:24:56 +0000907 continue;
908 }
XiaokangQian060d8672022-04-21 09:24:56 +0000909
XiaokangQian7807f9f2022-02-15 10:04:37 +0000910 /*
Przemek Stekielc89f3ea2023-05-18 15:45:53 +0200911 * ECDHE and FFDHE groups are supported
XiaokangQian7807f9f2022-02-15 10:04:37 +0000912 */
Przemek Stekielc89f3ea2023-05-18 15:45:53 +0200913 if (mbedtls_ssl_tls13_named_group_is_ecdhe(group) ||
Przemek Stekield5f79e72023-06-29 09:08:43 +0200914 mbedtls_ssl_tls13_named_group_is_ffdh(group)) {
Przemek Stekielc89f3ea2023-05-18 15:45:53 +0200915 MBEDTLS_SSL_DEBUG_MSG(2, ("ECDH/FFDH group: %s (%04x)",
Gilles Peskine449bd832023-01-11 14:50:10 +0100916 mbedtls_ssl_named_group_to_str(group),
917 group));
Przemek Stekiel7ac93be2023-07-04 10:02:38 +0200918 ret = mbedtls_ssl_tls13_read_public_xxdhe_share(
Gilles Peskine449bd832023-01-11 14:50:10 +0100919 ssl, key_exchange - 2, key_exchange_len + 2);
920 if (ret != 0) {
921 return ret;
922 }
XiaokangQian4e8cd7b2022-04-21 09:48:09 +0000923
Gilles Peskine449bd832023-01-11 14:50:10 +0100924 } else {
925 MBEDTLS_SSL_DEBUG_MSG(4, ("Unrecognized NamedGroup %u",
926 (unsigned) group));
XiaokangQian7807f9f2022-02-15 10:04:37 +0000927 continue;
928 }
929
XiaokangQian9b5d04b2022-04-10 10:20:43 +0000930 ssl->handshake->offered_group_id = group;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000931 }
932
Jerry Yuc1be19f2022-04-23 16:11:39 +0800933
Gilles Peskine449bd832023-01-11 14:50:10 +0100934 if (ssl->handshake->offered_group_id == 0) {
935 MBEDTLS_SSL_DEBUG_MSG(1, ("no matching key share"));
936 return SSL_TLS1_3_PARSE_KEY_SHARES_EXT_NO_MATCH;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000937 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100938 return 0;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000939}
Valerio Settic9ae8622023-07-25 11:23:50 +0200940#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_EPHEMERAL_ENABLED */
XiaokangQian7807f9f2022-02-15 10:04:37 +0000941
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200942MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100943static int ssl_tls13_client_hello_has_exts(mbedtls_ssl_context *ssl,
944 int exts_mask)
XiaokangQian7807f9f2022-02-15 10:04:37 +0000945{
Jerry Yu0c354a22022-08-29 15:25:36 +0800946 int masked = ssl->handshake->received_extensions & exts_mask;
Gilles Peskine449bd832023-01-11 14:50:10 +0100947 return masked == exts_mask;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000948}
949
Jerry Yue5991322022-11-07 14:03:44 +0800950#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200951MBEDTLS_CHECK_RETURN_CRITICAL
XiaokangQianb67384d2022-04-19 00:02:38 +0000952static int ssl_tls13_client_hello_has_exts_for_ephemeral_key_exchange(
Gilles Peskine449bd832023-01-11 14:50:10 +0100953 mbedtls_ssl_context *ssl)
XiaokangQian7807f9f2022-02-15 10:04:37 +0000954{
Gilles Peskine449bd832023-01-11 14:50:10 +0100955 return ssl_tls13_client_hello_has_exts(
956 ssl,
957 MBEDTLS_SSL_EXT_MASK(SUPPORTED_GROUPS) |
958 MBEDTLS_SSL_EXT_MASK(KEY_SHARE) |
959 MBEDTLS_SSL_EXT_MASK(SIG_ALG));
XiaokangQian7807f9f2022-02-15 10:04:37 +0000960}
Jerry Yue5991322022-11-07 14:03:44 +0800961#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED */
XiaokangQian7807f9f2022-02-15 10:04:37 +0000962
Jerry Yue5991322022-11-07 14:03:44 +0800963#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK_ENABLED)
Jerry Yu77f01482022-07-11 07:03:24 +0000964MBEDTLS_CHECK_RETURN_CRITICAL
965static int ssl_tls13_client_hello_has_exts_for_psk_key_exchange(
Gilles Peskine449bd832023-01-11 14:50:10 +0100966 mbedtls_ssl_context *ssl)
Jerry Yu77f01482022-07-11 07:03:24 +0000967{
Gilles Peskine449bd832023-01-11 14:50:10 +0100968 return ssl_tls13_client_hello_has_exts(
969 ssl,
970 MBEDTLS_SSL_EXT_MASK(PRE_SHARED_KEY) |
971 MBEDTLS_SSL_EXT_MASK(PSK_KEY_EXCHANGE_MODES));
Jerry Yu77f01482022-07-11 07:03:24 +0000972}
Jerry Yue5991322022-11-07 14:03:44 +0800973#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK_ENABLED */
Jerry Yu77f01482022-07-11 07:03:24 +0000974
Jerry Yue5991322022-11-07 14:03:44 +0800975#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK_EPHEMERAL_ENABLED)
Jerry Yu77f01482022-07-11 07:03:24 +0000976MBEDTLS_CHECK_RETURN_CRITICAL
977static int ssl_tls13_client_hello_has_exts_for_psk_ephemeral_key_exchange(
Gilles Peskine449bd832023-01-11 14:50:10 +0100978 mbedtls_ssl_context *ssl)
Jerry Yu77f01482022-07-11 07:03:24 +0000979{
Gilles Peskine449bd832023-01-11 14:50:10 +0100980 return ssl_tls13_client_hello_has_exts(
981 ssl,
982 MBEDTLS_SSL_EXT_MASK(SUPPORTED_GROUPS) |
983 MBEDTLS_SSL_EXT_MASK(KEY_SHARE) |
984 MBEDTLS_SSL_EXT_MASK(PRE_SHARED_KEY) |
985 MBEDTLS_SSL_EXT_MASK(PSK_KEY_EXCHANGE_MODES));
Jerry Yu77f01482022-07-11 07:03:24 +0000986}
Jerry Yue5991322022-11-07 14:03:44 +0800987#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK_EPHEMERAL_ENABLED */
Jerry Yu77f01482022-07-11 07:03:24 +0000988
Pengyu Lv306a01d2023-02-02 16:35:47 +0800989#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED)
990MBEDTLS_CHECK_RETURN_CRITICAL
Pengyu Lvd72e8582023-11-10 10:37:18 +0800991static int ssl_tls13_ticket_is_kex_mode_permitted(mbedtls_ssl_context *ssl,
992 unsigned int kex_mode)
Pengyu Lv306a01d2023-02-02 16:35:47 +0800993{
994#if defined(MBEDTLS_SSL_SESSION_TICKETS)
995 if (ssl->handshake->resume) {
Pengyu Lv94a42cc2023-12-06 10:04:17 +0800996 if (!mbedtls_ssl_tls13_session_ticket_has_flags(
Pengyu Lv306a01d2023-02-02 16:35:47 +0800997 ssl->session_negotiate, kex_mode)) {
998 return 0;
999 }
1000 }
1001#else
1002 ((void) ssl);
1003 ((void) kex_mode);
1004#endif
1005 return 1;
1006}
1007#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED */
1008
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001009MBEDTLS_CHECK_RETURN_CRITICAL
Pengyu Lvbc4aab72023-12-01 15:37:24 +08001010static int ssl_tls13_key_exchange_is_ephemeral_available(mbedtls_ssl_context *ssl)
XiaokangQian7807f9f2022-02-15 10:04:37 +00001011{
Jerry Yue5991322022-11-07 14:03:44 +08001012#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED)
Pengyu Lv0a1ff2b2023-11-14 11:03:32 +08001013 return mbedtls_ssl_conf_tls13_is_ephemeral_enabled(ssl) &&
Gilles Peskine449bd832023-01-11 14:50:10 +01001014 ssl_tls13_client_hello_has_exts_for_ephemeral_key_exchange(ssl);
Jerry Yue5991322022-11-07 14:03:44 +08001015#else
1016 ((void) ssl);
Gilles Peskine449bd832023-01-11 14:50:10 +01001017 return 0;
Jerry Yue5991322022-11-07 14:03:44 +08001018#endif
Jerry Yu77f01482022-07-11 07:03:24 +00001019}
XiaokangQian7807f9f2022-02-15 10:04:37 +00001020
Jerry Yu77f01482022-07-11 07:03:24 +00001021MBEDTLS_CHECK_RETURN_CRITICAL
Pengyu Lvbc4aab72023-12-01 15:37:24 +08001022static int ssl_tls13_key_exchange_is_psk_available(mbedtls_ssl_context *ssl)
Jerry Yu77f01482022-07-11 07:03:24 +00001023{
Jerry Yue5991322022-11-07 14:03:44 +08001024#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK_ENABLED)
Pengyu Lvd72e8582023-11-10 10:37:18 +08001025 return ssl_tls13_ticket_is_kex_mode_permitted(
Pengyu Lv306a01d2023-02-02 16:35:47 +08001026 ssl, MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK) &&
Pengyu Lv0a1ff2b2023-11-14 11:03:32 +08001027 mbedtls_ssl_conf_tls13_is_psk_enabled(ssl) &&
Pengyu Lvb2cfafb2023-11-14 13:56:13 +08001028 mbedtls_ssl_tls13_is_psk_supported(ssl) &&
Gilles Peskine449bd832023-01-11 14:50:10 +01001029 ssl_tls13_client_hello_has_exts_for_psk_key_exchange(ssl);
Jerry Yu77f01482022-07-11 07:03:24 +00001030#else
1031 ((void) ssl);
Gilles Peskine449bd832023-01-11 14:50:10 +01001032 return 0;
Jerry Yu77f01482022-07-11 07:03:24 +00001033#endif
1034}
XiaokangQian7807f9f2022-02-15 10:04:37 +00001035
Jerry Yu77f01482022-07-11 07:03:24 +00001036MBEDTLS_CHECK_RETURN_CRITICAL
Pengyu Lvbc4aab72023-12-01 15:37:24 +08001037static int ssl_tls13_key_exchange_is_psk_ephemeral_available(mbedtls_ssl_context *ssl)
Jerry Yu77f01482022-07-11 07:03:24 +00001038{
Jerry Yue5991322022-11-07 14:03:44 +08001039#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK_EPHEMERAL_ENABLED)
Pengyu Lvd72e8582023-11-10 10:37:18 +08001040 return ssl_tls13_ticket_is_kex_mode_permitted(
Pengyu Lv306a01d2023-02-02 16:35:47 +08001041 ssl, MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK_EPHEMERAL) &&
Pengyu Lv0a1ff2b2023-11-14 11:03:32 +08001042 mbedtls_ssl_conf_tls13_is_psk_ephemeral_enabled(ssl) &&
Pengyu Lvb2cfafb2023-11-14 13:56:13 +08001043 mbedtls_ssl_tls13_is_psk_ephemeral_supported(ssl) &&
Gilles Peskine449bd832023-01-11 14:50:10 +01001044 ssl_tls13_client_hello_has_exts_for_psk_ephemeral_key_exchange(ssl);
Jerry Yu77f01482022-07-11 07:03:24 +00001045#else
1046 ((void) ssl);
Gilles Peskine449bd832023-01-11 14:50:10 +01001047 return 0;
Jerry Yu77f01482022-07-11 07:03:24 +00001048#endif
1049}
1050
Gilles Peskine449bd832023-01-11 14:50:10 +01001051static int ssl_tls13_determine_key_exchange_mode(mbedtls_ssl_context *ssl)
Jerry Yu77f01482022-07-11 07:03:24 +00001052{
1053 /*
1054 * Determine the key exchange algorithm to use.
1055 * There are three types of key exchanges supported in TLS 1.3:
1056 * - (EC)DH with ECDSA,
1057 * - (EC)DH with PSK,
1058 * - plain PSK.
1059 *
1060 * The PSK-based key exchanges may additionally be used with 0-RTT.
1061 *
1062 * Our built-in order of preference is
Jerry Yuce6ed702022-07-22 21:49:53 +08001063 * 1 ) (EC)DHE-PSK Mode ( psk_ephemeral )
1064 * 2 ) Certificate Mode ( ephemeral )
1065 * 3 ) Plain PSK Mode ( psk )
Jerry Yu77f01482022-07-11 07:03:24 +00001066 */
1067
Xiaokang Qian9f1747b2023-03-30 02:50:04 +00001068 ssl->handshake->key_exchange_mode =
1069 MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_NONE;
Jerry Yu77f01482022-07-11 07:03:24 +00001070
Pengyu Lvbc4aab72023-12-01 15:37:24 +08001071 if (ssl_tls13_key_exchange_is_psk_ephemeral_available(ssl)) {
Jerry Yu77f01482022-07-11 07:03:24 +00001072 ssl->handshake->key_exchange_mode =
1073 MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK_EPHEMERAL;
Gilles Peskine449bd832023-01-11 14:50:10 +01001074 MBEDTLS_SSL_DEBUG_MSG(2, ("key exchange mode: psk_ephemeral"));
1075 } else
Pengyu Lvbc4aab72023-12-01 15:37:24 +08001076 if (ssl_tls13_key_exchange_is_ephemeral_available(ssl)) {
Jerry Yu77f01482022-07-11 07:03:24 +00001077 ssl->handshake->key_exchange_mode =
1078 MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL;
Gilles Peskine449bd832023-01-11 14:50:10 +01001079 MBEDTLS_SSL_DEBUG_MSG(2, ("key exchange mode: ephemeral"));
1080 } else
Pengyu Lvbc4aab72023-12-01 15:37:24 +08001081 if (ssl_tls13_key_exchange_is_psk_available(ssl)) {
Jerry Yuce6ed702022-07-22 21:49:53 +08001082 ssl->handshake->key_exchange_mode =
1083 MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK;
Gilles Peskine449bd832023-01-11 14:50:10 +01001084 MBEDTLS_SSL_DEBUG_MSG(2, ("key exchange mode: psk"));
1085 } else {
Jerry Yu77f01482022-07-11 07:03:24 +00001086 MBEDTLS_SSL_DEBUG_MSG(
Gilles Peskine449bd832023-01-11 14:50:10 +01001087 1,
1088 ("ClientHello message misses mandatory extensions."));
1089 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_MISSING_EXTENSION,
1090 MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER);
1091 return MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER;
Jerry Yu77f01482022-07-11 07:03:24 +00001092 }
1093
Gilles Peskine449bd832023-01-11 14:50:10 +01001094 return 0;
Jerry Yu77f01482022-07-11 07:03:24 +00001095
XiaokangQian7807f9f2022-02-15 10:04:37 +00001096}
1097
XiaokangQian81802f42022-06-10 13:25:22 +00001098#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
Ronald Cron928cbd32022-10-04 16:14:26 +02001099 defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED)
Ronald Cron67ea2542022-09-15 17:34:42 +02001100
1101#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01001102static psa_algorithm_t ssl_tls13_iana_sig_alg_to_psa_alg(uint16_t sig_alg)
Ronald Cron67ea2542022-09-15 17:34:42 +02001103{
Gilles Peskine449bd832023-01-11 14:50:10 +01001104 switch (sig_alg) {
Ronald Cron67ea2542022-09-15 17:34:42 +02001105 case MBEDTLS_TLS1_3_SIG_ECDSA_SECP256R1_SHA256:
Gilles Peskine449bd832023-01-11 14:50:10 +01001106 return PSA_ALG_ECDSA(PSA_ALG_SHA_256);
Ronald Cron67ea2542022-09-15 17:34:42 +02001107 case MBEDTLS_TLS1_3_SIG_ECDSA_SECP384R1_SHA384:
Gilles Peskine449bd832023-01-11 14:50:10 +01001108 return PSA_ALG_ECDSA(PSA_ALG_SHA_384);
Ronald Cron67ea2542022-09-15 17:34:42 +02001109 case MBEDTLS_TLS1_3_SIG_ECDSA_SECP521R1_SHA512:
Gilles Peskine449bd832023-01-11 14:50:10 +01001110 return PSA_ALG_ECDSA(PSA_ALG_SHA_512);
Ronald Cron67ea2542022-09-15 17:34:42 +02001111 case MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA256:
Gilles Peskine449bd832023-01-11 14:50:10 +01001112 return PSA_ALG_RSA_PSS(PSA_ALG_SHA_256);
Ronald Cron67ea2542022-09-15 17:34:42 +02001113 case MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA384:
Gilles Peskine449bd832023-01-11 14:50:10 +01001114 return PSA_ALG_RSA_PSS(PSA_ALG_SHA_384);
Ronald Cron67ea2542022-09-15 17:34:42 +02001115 case MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA512:
Gilles Peskine449bd832023-01-11 14:50:10 +01001116 return PSA_ALG_RSA_PSS(PSA_ALG_SHA_512);
Ronald Cron67ea2542022-09-15 17:34:42 +02001117 case MBEDTLS_TLS1_3_SIG_RSA_PKCS1_SHA256:
Gilles Peskine449bd832023-01-11 14:50:10 +01001118 return PSA_ALG_RSA_PKCS1V15_SIGN(PSA_ALG_SHA_256);
Ronald Cron67ea2542022-09-15 17:34:42 +02001119 case MBEDTLS_TLS1_3_SIG_RSA_PKCS1_SHA384:
Gilles Peskine449bd832023-01-11 14:50:10 +01001120 return PSA_ALG_RSA_PKCS1V15_SIGN(PSA_ALG_SHA_384);
Ronald Cron67ea2542022-09-15 17:34:42 +02001121 case MBEDTLS_TLS1_3_SIG_RSA_PKCS1_SHA512:
Gilles Peskine449bd832023-01-11 14:50:10 +01001122 return PSA_ALG_RSA_PKCS1V15_SIGN(PSA_ALG_SHA_512);
Ronald Cron67ea2542022-09-15 17:34:42 +02001123 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01001124 return PSA_ALG_NONE;
Ronald Cron67ea2542022-09-15 17:34:42 +02001125 }
1126}
1127#endif /* MBEDTLS_USE_PSA_CRYPTO */
1128
XiaokangQian23c5be62022-06-07 02:04:34 +00001129/*
XiaokangQianfb665a82022-06-15 03:57:21 +00001130 * Pick best ( private key, certificate chain ) pair based on the signature
1131 * algorithms supported by the client.
XiaokangQian23c5be62022-06-07 02:04:34 +00001132 */
Ronald Cronce7d76e2022-07-08 18:56:49 +02001133MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01001134static int ssl_tls13_pick_key_cert(mbedtls_ssl_context *ssl)
XiaokangQian23c5be62022-06-07 02:04:34 +00001135{
XiaokangQianfb665a82022-06-15 03:57:21 +00001136 mbedtls_ssl_key_cert *key_cert, *key_cert_list;
XiaokangQian81802f42022-06-10 13:25:22 +00001137 const uint16_t *sig_alg = ssl->handshake->received_sig_algs;
XiaokangQian23c5be62022-06-07 02:04:34 +00001138
1139#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Gilles Peskine449bd832023-01-11 14:50:10 +01001140 if (ssl->handshake->sni_key_cert != NULL) {
XiaokangQianfb665a82022-06-15 03:57:21 +00001141 key_cert_list = ssl->handshake->sni_key_cert;
Gilles Peskine449bd832023-01-11 14:50:10 +01001142 } else
XiaokangQian23c5be62022-06-07 02:04:34 +00001143#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
Gilles Peskine449bd832023-01-11 14:50:10 +01001144 key_cert_list = ssl->conf->key_cert;
XiaokangQian23c5be62022-06-07 02:04:34 +00001145
Gilles Peskine449bd832023-01-11 14:50:10 +01001146 if (key_cert_list == NULL) {
1147 MBEDTLS_SSL_DEBUG_MSG(3, ("server has no certificate"));
1148 return -1;
XiaokangQian23c5be62022-06-07 02:04:34 +00001149 }
1150
Gilles Peskine449bd832023-01-11 14:50:10 +01001151 for (; *sig_alg != MBEDTLS_TLS1_3_SIG_NONE; sig_alg++) {
1152 if (!mbedtls_ssl_sig_alg_is_offered(ssl, *sig_alg)) {
Ronald Cron67ea2542022-09-15 17:34:42 +02001153 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +01001154 }
Ronald Cron67ea2542022-09-15 17:34:42 +02001155
Gilles Peskine449bd832023-01-11 14:50:10 +01001156 if (!mbedtls_ssl_tls13_sig_alg_for_cert_verify_is_supported(*sig_alg)) {
Ronald Cron67ea2542022-09-15 17:34:42 +02001157 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +01001158 }
Ronald Cron67ea2542022-09-15 17:34:42 +02001159
Gilles Peskine449bd832023-01-11 14:50:10 +01001160 for (key_cert = key_cert_list; key_cert != NULL;
1161 key_cert = key_cert->next) {
Ronald Cron67ea2542022-09-15 17:34:42 +02001162#if defined(MBEDTLS_USE_PSA_CRYPTO)
1163 psa_algorithm_t psa_alg = PSA_ALG_NONE;
1164#endif /* MBEDTLS_USE_PSA_CRYPTO */
1165
Gilles Peskine449bd832023-01-11 14:50:10 +01001166 MBEDTLS_SSL_DEBUG_CRT(3, "certificate (chain) candidate",
1167 key_cert->cert);
XiaokangQian81802f42022-06-10 13:25:22 +00001168
1169 /*
Gilles Peskine449bd832023-01-11 14:50:10 +01001170 * This avoids sending the client a cert it'll reject based on
1171 * keyUsage or other extensions.
1172 */
1173 if (mbedtls_x509_crt_check_key_usage(
1174 key_cert->cert, MBEDTLS_X509_KU_DIGITAL_SIGNATURE) != 0 ||
XiaokangQian81802f42022-06-10 13:25:22 +00001175 mbedtls_x509_crt_check_extended_key_usage(
XiaokangQianfb665a82022-06-15 03:57:21 +00001176 key_cert->cert, MBEDTLS_OID_SERVER_AUTH,
Gilles Peskine449bd832023-01-11 14:50:10 +01001177 MBEDTLS_OID_SIZE(MBEDTLS_OID_SERVER_AUTH)) != 0) {
1178 MBEDTLS_SSL_DEBUG_MSG(3, ("certificate mismatch: "
1179 "(extended) key usage extension"));
XiaokangQian81802f42022-06-10 13:25:22 +00001180 continue;
1181 }
XiaokangQian23c5be62022-06-07 02:04:34 +00001182
Gilles Peskine449bd832023-01-11 14:50:10 +01001183 MBEDTLS_SSL_DEBUG_MSG(3,
1184 ("ssl_tls13_pick_key_cert:"
1185 "check signature algorithm %s [%04x]",
1186 mbedtls_ssl_sig_alg_to_str(*sig_alg),
1187 *sig_alg));
Ronald Cron67ea2542022-09-15 17:34:42 +02001188#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01001189 psa_alg = ssl_tls13_iana_sig_alg_to_psa_alg(*sig_alg);
Ronald Cron67ea2542022-09-15 17:34:42 +02001190#endif /* MBEDTLS_USE_PSA_CRYPTO */
1191
Gilles Peskine449bd832023-01-11 14:50:10 +01001192 if (mbedtls_ssl_tls13_check_sig_alg_cert_key_match(
1193 *sig_alg, &key_cert->cert->pk)
Ronald Cron67ea2542022-09-15 17:34:42 +02001194#if defined(MBEDTLS_USE_PSA_CRYPTO)
1195 && psa_alg != PSA_ALG_NONE &&
Gilles Peskine449bd832023-01-11 14:50:10 +01001196 mbedtls_pk_can_do_ext(&key_cert->cert->pk, psa_alg,
1197 PSA_KEY_USAGE_SIGN_HASH) == 1
Ronald Cron67ea2542022-09-15 17:34:42 +02001198#endif /* MBEDTLS_USE_PSA_CRYPTO */
Gilles Peskine449bd832023-01-11 14:50:10 +01001199 ) {
XiaokangQianfb665a82022-06-15 03:57:21 +00001200 ssl->handshake->key_cert = key_cert;
Gilles Peskine449bd832023-01-11 14:50:10 +01001201 MBEDTLS_SSL_DEBUG_MSG(3,
1202 ("ssl_tls13_pick_key_cert:"
1203 "selected signature algorithm"
1204 " %s [%04x]",
1205 mbedtls_ssl_sig_alg_to_str(*sig_alg),
1206 *sig_alg));
XiaokangQianfb665a82022-06-15 03:57:21 +00001207 MBEDTLS_SSL_DEBUG_CRT(
Gilles Peskine449bd832023-01-11 14:50:10 +01001208 3, "selected certificate (chain)",
1209 ssl->handshake->key_cert->cert);
1210 return 0;
XiaokangQian81802f42022-06-10 13:25:22 +00001211 }
XiaokangQian81802f42022-06-10 13:25:22 +00001212 }
XiaokangQian23c5be62022-06-07 02:04:34 +00001213 }
1214
Gilles Peskine449bd832023-01-11 14:50:10 +01001215 MBEDTLS_SSL_DEBUG_MSG(2, ("ssl_tls13_pick_key_cert:"
1216 "no suitable certificate found"));
1217 return -1;
XiaokangQian23c5be62022-06-07 02:04:34 +00001218}
XiaokangQian81802f42022-06-10 13:25:22 +00001219#endif /* MBEDTLS_X509_CRT_PARSE_C &&
Ronald Cron928cbd32022-10-04 16:14:26 +02001220 MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED */
XiaokangQian23c5be62022-06-07 02:04:34 +00001221
XiaokangQian4080a7f2022-04-11 09:55:18 +00001222/*
XiaokangQianed582dd2022-04-13 08:21:05 +00001223 *
1224 * STATE HANDLING: ClientHello
1225 *
XiaokangQianb67384d2022-04-19 00:02:38 +00001226 * There are three possible classes of outcomes when parsing the ClientHello:
XiaokangQianed582dd2022-04-13 08:21:05 +00001227 *
XiaokangQianb67384d2022-04-19 00:02:38 +00001228 * 1) The ClientHello was well-formed and matched the server's configuration.
XiaokangQianed582dd2022-04-13 08:21:05 +00001229 *
1230 * In this case, the server progresses to sending its ServerHello.
1231 *
XiaokangQianb67384d2022-04-19 00:02:38 +00001232 * 2) The ClientHello was well-formed but didn't match the server's
1233 * configuration.
XiaokangQianed582dd2022-04-13 08:21:05 +00001234 *
1235 * For example, the client might not have offered a key share which
1236 * the server supports, or the server might require a cookie.
1237 *
1238 * In this case, the server sends a HelloRetryRequest.
1239 *
XiaokangQianb67384d2022-04-19 00:02:38 +00001240 * 3) The ClientHello was ill-formed
XiaokangQianed582dd2022-04-13 08:21:05 +00001241 *
1242 * In this case, we abort the handshake.
1243 *
1244 */
1245
1246/*
XiaokangQian4080a7f2022-04-11 09:55:18 +00001247 * Structure of this message:
1248 *
XiaokangQiane8ff3502022-04-22 02:34:40 +00001249 * uint16 ProtocolVersion;
1250 * opaque Random[32];
1251 * uint8 CipherSuite[2]; // Cryptographic suite selector
XiaokangQian4080a7f2022-04-11 09:55:18 +00001252 *
XiaokangQiane8ff3502022-04-22 02:34:40 +00001253 * struct {
1254 * ProtocolVersion legacy_version = 0x0303; // TLS v1.2
1255 * Random random;
1256 * opaque legacy_session_id<0..32>;
1257 * CipherSuite cipher_suites<2..2^16-2>;
1258 * opaque legacy_compression_methods<1..2^8-1>;
1259 * Extension extensions<8..2^16-1>;
1260 * } ClientHello;
XiaokangQian4080a7f2022-04-11 09:55:18 +00001261 */
XiaokangQianed582dd2022-04-13 08:21:05 +00001262
1263#define SSL_CLIENT_HELLO_OK 0
1264#define SSL_CLIENT_HELLO_HRR_REQUIRED 1
Ronald Cron5af4c7f2023-03-07 20:46:59 +01001265#define SSL_CLIENT_HELLO_TLS1_2 2
XiaokangQianed582dd2022-04-13 08:21:05 +00001266
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001267MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01001268static int ssl_tls13_parse_client_hello(mbedtls_ssl_context *ssl,
1269 const unsigned char *buf,
1270 const unsigned char *end)
XiaokangQian7807f9f2022-02-15 10:04:37 +00001271{
XiaokangQianb67384d2022-04-19 00:02:38 +00001272 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1273 const unsigned char *p = buf;
Ronald Crond540d992023-03-07 09:41:48 +01001274 const unsigned char *random;
XiaokangQian4080a7f2022-04-11 09:55:18 +00001275 size_t legacy_session_id_len;
Ronald Croncada4102023-03-07 09:51:39 +01001276 const unsigned char *legacy_session_id;
XiaokangQian318dc762022-04-20 09:43:51 +00001277 size_t cipher_suites_len;
Ronald Cron2f16b4e2023-03-07 10:07:32 +01001278 const unsigned char *cipher_suites;
XiaokangQian060d8672022-04-21 09:24:56 +00001279 const unsigned char *cipher_suites_end;
XiaokangQianb67384d2022-04-19 00:02:38 +00001280 size_t extensions_len;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001281 const unsigned char *extensions_end;
Ronald Croneff56732023-04-03 17:36:31 +02001282 const unsigned char *supported_versions_data;
1283 const unsigned char *supported_versions_data_end;
Jerry Yuc4bf5d62022-10-29 09:08:47 +08001284 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
Jerry Yu49ca9282022-05-05 11:05:22 +08001285 int hrr_required = 0;
Ronald Cron8a74f072023-06-14 17:59:29 +02001286 int no_usable_share_for_key_agreement = 0;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001287
Ronald Cron41a443a2022-10-04 16:38:25 +02001288#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED)
Jerry Yu29d9faa2022-08-23 17:52:45 +08001289 const unsigned char *pre_shared_key_ext = NULL;
Jerry Yu1c105562022-07-10 06:32:38 +00001290 const unsigned char *pre_shared_key_ext_end = NULL;
Ronald Cron41a443a2022-10-04 16:38:25 +02001291#endif
XiaokangQian7807f9f2022-02-15 10:04:37 +00001292
XiaokangQian7807f9f2022-02-15 10:04:37 +00001293 /*
XiaokangQianb67384d2022-04-19 00:02:38 +00001294 * ClientHello layout:
XiaokangQian7807f9f2022-02-15 10:04:37 +00001295 * 0 . 1 protocol version
XiaokangQiane8ff3502022-04-22 02:34:40 +00001296 * 2 . 33 random bytes
XiaokangQianc5763b52022-04-02 03:34:37 +00001297 * 34 . 34 session id length ( 1 byte )
XiaokangQian7807f9f2022-02-15 10:04:37 +00001298 * 35 . 34+x session id
XiaokangQian7807f9f2022-02-15 10:04:37 +00001299 * .. . .. ciphersuite list length ( 2 bytes )
1300 * .. . .. ciphersuite list
1301 * .. . .. compression alg. list length ( 1 byte )
1302 * .. . .. compression alg. list
1303 * .. . .. extensions length ( 2 bytes, optional )
1304 * .. . .. extensions ( optional )
1305 */
1306
XiaokangQianb67384d2022-04-19 00:02:38 +00001307 /*
bootstrap-prime6dbbf442022-05-17 19:30:44 -04001308 * Minimal length ( with everything empty and extensions omitted ) is
XiaokangQian7807f9f2022-02-15 10:04:37 +00001309 * 2 + 32 + 1 + 2 + 1 = 38 bytes. Check that first, so that we can
1310 * read at least up to session id length without worrying.
1311 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001312 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, 38);
XiaokangQian7807f9f2022-02-15 10:04:37 +00001313
1314 /* ...
1315 * ProtocolVersion legacy_version = 0x0303; // TLS 1.2
1316 * ...
1317 * with ProtocolVersion defined as:
1318 * uint16 ProtocolVersion;
1319 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001320 if (mbedtls_ssl_read_version(p, ssl->conf->transport) !=
1321 MBEDTLS_SSL_VERSION_TLS1_2) {
1322 MBEDTLS_SSL_DEBUG_MSG(1, ("Unsupported version of TLS."));
1323 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_PROTOCOL_VERSION,
1324 MBEDTLS_ERR_SSL_BAD_PROTOCOL_VERSION);
1325 return MBEDTLS_ERR_SSL_BAD_PROTOCOL_VERSION;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001326 }
1327 p += 2;
1328
Jerry Yuc1be19f2022-04-23 16:11:39 +08001329 /* ...
1330 * Random random;
1331 * ...
XiaokangQianb67384d2022-04-19 00:02:38 +00001332 * with Random defined as:
1333 * opaque Random[32];
XiaokangQian7807f9f2022-02-15 10:04:37 +00001334 */
Ronald Crond540d992023-03-07 09:41:48 +01001335 random = p;
XiaokangQian08037552022-04-20 07:16:41 +00001336 p += MBEDTLS_CLIENT_HELLO_RANDOM_LEN;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001337
Jerry Yuc1be19f2022-04-23 16:11:39 +08001338 /* ...
XiaokangQianb67384d2022-04-19 00:02:38 +00001339 * opaque legacy_session_id<0..32>;
Jerry Yuc1be19f2022-04-23 16:11:39 +08001340 * ...
XiaokangQian7807f9f2022-02-15 10:04:37 +00001341 */
Ronald Croncada4102023-03-07 09:51:39 +01001342 legacy_session_id_len = *(p++);
1343 legacy_session_id = p;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001344
XiaokangQianb67384d2022-04-19 00:02:38 +00001345 /*
1346 * Check we have enough data for the legacy session identifier
Jerry Yue95c8af2022-07-26 15:48:20 +08001347 * and the ciphersuite list length.
XiaokangQianb67384d2022-04-19 00:02:38 +00001348 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001349 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, legacy_session_id_len + 2);
XiaokangQian4080a7f2022-04-11 09:55:18 +00001350 p += legacy_session_id_len;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001351
Ronald Cron2f16b4e2023-03-07 10:07:32 +01001352 /* ...
1353 * CipherSuite cipher_suites<2..2^16-2>;
1354 * ...
1355 * with CipherSuite defined as:
1356 * uint8 CipherSuite[2];
1357 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001358 cipher_suites_len = MBEDTLS_GET_UINT16_BE(p, 0);
XiaokangQian7807f9f2022-02-15 10:04:37 +00001359 p += 2;
Ronald Cron2f16b4e2023-03-07 10:07:32 +01001360 cipher_suites = p;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001361
Ronald Cronfc7ae872023-02-16 15:32:19 +01001362 /*
1363 * The length of the ciphersuite list has to be even.
1364 */
1365 if (cipher_suites_len & 1) {
1366 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR,
1367 MBEDTLS_ERR_SSL_DECODE_ERROR);
1368 return MBEDTLS_ERR_SSL_DECODE_ERROR;
1369 }
1370
XiaokangQianb67384d2022-04-19 00:02:38 +00001371 /* Check we have enough data for the ciphersuite list, the legacy
1372 * compression methods and the length of the extensions.
Jerry Yue95c8af2022-07-26 15:48:20 +08001373 *
1374 * cipher_suites cipher_suites_len bytes
1375 * legacy_compression_methods 2 bytes
1376 * extensions_len 2 bytes
XiaokangQianb67384d2022-04-19 00:02:38 +00001377 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001378 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, cipher_suites_len + 2 + 2);
Ronald Cron8c527d02023-03-07 15:47:47 +01001379 p += cipher_suites_len;
1380 cipher_suites_end = p;
Jerry Yu5725f1c2022-08-21 17:27:16 +08001381
1382 /*
Ronald Cron8c527d02023-03-07 15:47:47 +01001383 * Search for the supported versions extension and parse it to determine
1384 * if the client supports TLS 1.3.
1385 */
1386 ret = mbedtls_ssl_tls13_is_supported_versions_ext_present_in_exts(
1387 ssl, p + 2, end,
Ronald Croneff56732023-04-03 17:36:31 +02001388 &supported_versions_data, &supported_versions_data_end);
Ronald Cron8c527d02023-03-07 15:47:47 +01001389 if (ret < 0) {
1390 MBEDTLS_SSL_DEBUG_RET(1,
1391 ("mbedtls_ssl_tls13_is_supported_versions_ext_present_in_exts"), ret);
1392 return ret;
1393 }
1394
1395 if (ret == 0) {
Ronald Cron5af4c7f2023-03-07 20:46:59 +01001396 return SSL_CLIENT_HELLO_TLS1_2;
Ronald Cron8c527d02023-03-07 15:47:47 +01001397 }
1398
Ronald Cron5af4c7f2023-03-07 20:46:59 +01001399 if (ret == 1) {
1400 ret = ssl_tls13_parse_supported_versions_ext(ssl,
Ronald Croneff56732023-04-03 17:36:31 +02001401 supported_versions_data,
1402 supported_versions_data_end);
Ronald Cron5af4c7f2023-03-07 20:46:59 +01001403 if (ret < 0) {
1404 MBEDTLS_SSL_DEBUG_RET(1,
1405 ("ssl_tls13_parse_supported_versions_ext"), ret);
1406 return ret;
1407 }
1408
Ronald Cronb828c7d2023-04-03 16:37:22 +02001409 /*
1410 * The supported versions extension was parsed successfully as the
1411 * value returned by ssl_tls13_parse_supported_versions_ext() is
1412 * positive. The return value is then equal to
1413 * MBEDTLS_SSL_VERSION_TLS1_2 or MBEDTLS_SSL_VERSION_TLS1_3, defining
1414 * the TLS version to negotiate.
1415 */
Ronald Cron5af4c7f2023-03-07 20:46:59 +01001416 if (MBEDTLS_SSL_VERSION_TLS1_2 == ret) {
1417 return SSL_CLIENT_HELLO_TLS1_2;
1418 }
Ronald Cron8c527d02023-03-07 15:47:47 +01001419 }
1420
1421 /*
1422 * We negotiate TLS 1.3.
Ronald Cron64582392023-03-07 09:21:40 +01001423 */
1424 ssl->tls_version = MBEDTLS_SSL_VERSION_TLS1_3;
Ronald Cron64582392023-03-07 09:21:40 +01001425 ssl->session_negotiate->tls_version = MBEDTLS_SSL_VERSION_TLS1_3;
1426 ssl->session_negotiate->endpoint = ssl->conf->endpoint;
Ronald Cron64582392023-03-07 09:21:40 +01001427
1428 /*
Ronald Crondad02b22023-04-06 09:57:52 +02001429 * We are negotiating the version 1.3 of the protocol. Do what we have
Ronald Croncada4102023-03-07 09:51:39 +01001430 * postponed: copy of the client random bytes, copy of the legacy session
Ronald Cron2f16b4e2023-03-07 10:07:32 +01001431 * identifier and selection of the TLS 1.3 cipher suite.
Ronald Crond540d992023-03-07 09:41:48 +01001432 */
1433 MBEDTLS_SSL_DEBUG_BUF(3, "client hello, random bytes",
1434 random, MBEDTLS_CLIENT_HELLO_RANDOM_LEN);
1435 memcpy(&handshake->randbytes[0], random, MBEDTLS_CLIENT_HELLO_RANDOM_LEN);
1436
Ronald Croncada4102023-03-07 09:51:39 +01001437 if (legacy_session_id_len > sizeof(ssl->session_negotiate->id)) {
1438 MBEDTLS_SSL_DEBUG_MSG(1, ("bad client hello message"));
1439 return MBEDTLS_ERR_SSL_DECODE_ERROR;
1440 }
1441 ssl->session_negotiate->id_len = legacy_session_id_len;
1442 MBEDTLS_SSL_DEBUG_BUF(3, "client hello, session id",
1443 legacy_session_id, legacy_session_id_len);
1444 memcpy(&ssl->session_negotiate->id[0],
1445 legacy_session_id, legacy_session_id_len);
1446
Ronald Crond540d992023-03-07 09:41:48 +01001447 /*
Jerry Yu5725f1c2022-08-21 17:27:16 +08001448 * Search for a matching ciphersuite
1449 */
Ronald Cron2f16b4e2023-03-07 10:07:32 +01001450 MBEDTLS_SSL_DEBUG_BUF(3, "client hello, list of cipher suites",
1451 cipher_suites, cipher_suites_len);
Jerry Yu5725f1c2022-08-21 17:27:16 +08001452
Ronald Cron89089cc2024-02-14 18:18:08 +01001453 ssl_tls13_select_ciphersuite(ssl, cipher_suites, cipher_suites_end,
1454 0, PSA_ALG_NONE, &handshake->ciphersuite_info);
Jerry Yu5725f1c2022-08-21 17:27:16 +08001455
Gilles Peskine449bd832023-01-11 14:50:10 +01001456 if (handshake->ciphersuite_info == NULL) {
1457 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE,
1458 MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE);
1459 return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
Jerry Yu5725f1c2022-08-21 17:27:16 +08001460 }
Ronald Cron89089cc2024-02-14 18:18:08 +01001461 ssl->session_negotiate->ciphersuite = handshake->ciphersuite_info->id;
1462
1463 MBEDTLS_SSL_DEBUG_MSG(2, ("selected ciphersuite: %04x - %s",
1464 ((unsigned) handshake->ciphersuite_info->id),
1465 handshake->ciphersuite_info->name));
Jerry Yuc1be19f2022-04-23 16:11:39 +08001466
XiaokangQian4080a7f2022-04-11 09:55:18 +00001467 /* ...
XiaokangQianb67384d2022-04-19 00:02:38 +00001468 * opaque legacy_compression_methods<1..2^8-1>;
XiaokangQian4080a7f2022-04-11 09:55:18 +00001469 * ...
XiaokangQian7807f9f2022-02-15 10:04:37 +00001470 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001471 if (p[0] != 1 || p[1] != MBEDTLS_SSL_COMPRESS_NULL) {
1472 MBEDTLS_SSL_DEBUG_MSG(1, ("bad legacy compression method"));
1473 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER,
1474 MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER);
1475 return MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001476 }
XiaokangQianb67384d2022-04-19 00:02:38 +00001477 p += 2;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001478
Jerry Yuc1be19f2022-04-23 16:11:39 +08001479 /* ...
XiaokangQianb67384d2022-04-19 00:02:38 +00001480 * Extension extensions<8..2^16-1>;
Jerry Yuc1be19f2022-04-23 16:11:39 +08001481 * ...
XiaokangQianb67384d2022-04-19 00:02:38 +00001482 * with Extension defined as:
1483 * struct {
1484 * ExtensionType extension_type;
1485 * opaque extension_data<0..2^16-1>;
1486 * } Extension;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001487 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001488 extensions_len = MBEDTLS_GET_UINT16_BE(p, 0);
XiaokangQian7807f9f2022-02-15 10:04:37 +00001489 p += 2;
Gilles Peskine449bd832023-01-11 14:50:10 +01001490 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, extensions_len);
XiaokangQiane8ff3502022-04-22 02:34:40 +00001491 extensions_end = p + extensions_len;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001492
Gilles Peskine449bd832023-01-11 14:50:10 +01001493 MBEDTLS_SSL_DEBUG_BUF(3, "client hello extensions", p, extensions_len);
Jerry Yu63a459c2022-10-31 13:38:40 +08001494 handshake->received_extensions = MBEDTLS_SSL_EXT_MASK_NONE;
Jerry Yu0c354a22022-08-29 15:25:36 +08001495
Gilles Peskine449bd832023-01-11 14:50:10 +01001496 while (p < extensions_end) {
XiaokangQian7807f9f2022-02-15 10:04:37 +00001497 unsigned int extension_type;
1498 size_t extension_data_len;
1499 const unsigned char *extension_data_end;
Jerry Yu263dbf72022-10-26 10:51:27 +08001500 uint32_t allowed_exts = MBEDTLS_SSL_TLS1_3_ALLOWED_EXTS_OF_CH;
1501
Ronald Cron5fbd2702024-02-14 10:03:36 +01001502 if (ssl->handshake->hello_retry_request_flag) {
Jerry Yu263dbf72022-10-26 10:51:27 +08001503 /* Do not accept early data extension in 2nd ClientHello */
1504 allowed_exts &= ~MBEDTLS_SSL_EXT_MASK(EARLY_DATA);
1505 }
XiaokangQian7807f9f2022-02-15 10:04:37 +00001506
Jerry Yu0c354a22022-08-29 15:25:36 +08001507 /* RFC 8446, section 4.2.11
Jerry Yu1c9247c2022-07-21 12:37:39 +08001508 *
1509 * The "pre_shared_key" extension MUST be the last extension in the
1510 * ClientHello (this facilitates implementation as described below).
1511 * Servers MUST check that it is the last extension and otherwise fail
1512 * the handshake with an "illegal_parameter" alert.
1513 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001514 if (handshake->received_extensions & MBEDTLS_SSL_EXT_MASK(PRE_SHARED_KEY)) {
Jerry Yu1c9247c2022-07-21 12:37:39 +08001515 MBEDTLS_SSL_DEBUG_MSG(
Gilles Peskine449bd832023-01-11 14:50:10 +01001516 3, ("pre_shared_key is not last extension."));
Jerry Yu1c9247c2022-07-21 12:37:39 +08001517 MBEDTLS_SSL_PEND_FATAL_ALERT(
1518 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER,
Gilles Peskine449bd832023-01-11 14:50:10 +01001519 MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER);
1520 return MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER;
Jerry Yu1c9247c2022-07-21 12:37:39 +08001521 }
1522
Gilles Peskine449bd832023-01-11 14:50:10 +01001523 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, extensions_end, 4);
1524 extension_type = MBEDTLS_GET_UINT16_BE(p, 0);
1525 extension_data_len = MBEDTLS_GET_UINT16_BE(p, 2);
XiaokangQian7807f9f2022-02-15 10:04:37 +00001526 p += 4;
1527
Gilles Peskine449bd832023-01-11 14:50:10 +01001528 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, extensions_end, extension_data_len);
XiaokangQian7807f9f2022-02-15 10:04:37 +00001529 extension_data_end = p + extension_data_len;
1530
Jerry Yuc4bf5d62022-10-29 09:08:47 +08001531 ret = mbedtls_ssl_tls13_check_received_extension(
Gilles Peskine449bd832023-01-11 14:50:10 +01001532 ssl, MBEDTLS_SSL_HS_CLIENT_HELLO, extension_type,
Jerry Yu263dbf72022-10-26 10:51:27 +08001533 allowed_exts);
Gilles Peskine449bd832023-01-11 14:50:10 +01001534 if (ret != 0) {
1535 return ret;
1536 }
Jerry Yue18dc7e2022-08-04 16:29:22 +08001537
Gilles Peskine449bd832023-01-11 14:50:10 +01001538 switch (extension_type) {
XiaokangQian40a35232022-05-07 09:02:40 +00001539#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
1540 case MBEDTLS_TLS_EXT_SERVERNAME:
Gilles Peskine449bd832023-01-11 14:50:10 +01001541 MBEDTLS_SSL_DEBUG_MSG(3, ("found ServerName extension"));
1542 ret = mbedtls_ssl_parse_server_name_ext(ssl, p,
1543 extension_data_end);
1544 if (ret != 0) {
XiaokangQian40a35232022-05-07 09:02:40 +00001545 MBEDTLS_SSL_DEBUG_RET(
Gilles Peskine449bd832023-01-11 14:50:10 +01001546 1, "mbedtls_ssl_parse_servername_ext", ret);
1547 return ret;
XiaokangQian40a35232022-05-07 09:02:40 +00001548 }
XiaokangQian40a35232022-05-07 09:02:40 +00001549 break;
1550#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
1551
Przemek Stekiel8c0a9532023-06-15 16:48:19 +02001552#if defined(PSA_WANT_ALG_ECDH) || defined(PSA_WANT_ALG_FFDH)
XiaokangQian7807f9f2022-02-15 10:04:37 +00001553 case MBEDTLS_TLS_EXT_SUPPORTED_GROUPS:
Gilles Peskine449bd832023-01-11 14:50:10 +01001554 MBEDTLS_SSL_DEBUG_MSG(3, ("found supported group extension"));
XiaokangQian7807f9f2022-02-15 10:04:37 +00001555
1556 /* Supported Groups Extension
1557 *
1558 * When sent by the client, the "supported_groups" extension
1559 * indicates the named groups which the client supports,
1560 * ordered from most preferred to least preferred.
1561 */
Jerry Yuc1be19f2022-04-23 16:11:39 +08001562 ret = ssl_tls13_parse_supported_groups_ext(
Gilles Peskine449bd832023-01-11 14:50:10 +01001563 ssl, p, extension_data_end);
1564 if (ret != 0) {
Xiaokang Qian9f1747b2023-03-30 02:50:04 +00001565 MBEDTLS_SSL_DEBUG_RET(
Xiaokang Qian8bce0e62023-04-04 10:15:48 +00001566 1, "ssl_tls13_parse_supported_groups_ext", ret);
Gilles Peskine449bd832023-01-11 14:50:10 +01001567 return ret;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001568 }
1569
XiaokangQian7807f9f2022-02-15 10:04:37 +00001570 break;
Przemek Stekiel8c0a9532023-06-15 16:48:19 +02001571#endif /* PSA_WANT_ALG_ECDH || PSA_WANT_ALG_FFDH*/
XiaokangQian7807f9f2022-02-15 10:04:37 +00001572
Valerio Settic9ae8622023-07-25 11:23:50 +02001573#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_EPHEMERAL_ENABLED)
XiaokangQian7807f9f2022-02-15 10:04:37 +00001574 case MBEDTLS_TLS_EXT_KEY_SHARE:
Gilles Peskine449bd832023-01-11 14:50:10 +01001575 MBEDTLS_SSL_DEBUG_MSG(3, ("found key share extension"));
XiaokangQian7807f9f2022-02-15 10:04:37 +00001576
1577 /*
1578 * Key Share Extension
1579 *
1580 * When sent by the client, the "key_share" extension
1581 * contains the endpoint's cryptographic parameters for
1582 * ECDHE/DHE key establishment methods.
1583 */
Jerry Yuc1be19f2022-04-23 16:11:39 +08001584 ret = ssl_tls13_parse_key_shares_ext(
Gilles Peskine449bd832023-01-11 14:50:10 +01001585 ssl, p, extension_data_end);
1586 if (ret == SSL_TLS1_3_PARSE_KEY_SHARES_EXT_NO_MATCH) {
Ronald Cron8a74f072023-06-14 17:59:29 +02001587 MBEDTLS_SSL_DEBUG_MSG(2, ("No usable share for key agreement."));
1588 no_usable_share_for_key_agreement = 1;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001589 }
1590
Gilles Peskine449bd832023-01-11 14:50:10 +01001591 if (ret < 0) {
Jerry Yu582dd062022-04-22 21:59:01 +08001592 MBEDTLS_SSL_DEBUG_RET(
Gilles Peskine449bd832023-01-11 14:50:10 +01001593 1, "ssl_tls13_parse_key_shares_ext", ret);
1594 return ret;
Jerry Yu582dd062022-04-22 21:59:01 +08001595 }
XiaokangQian7807f9f2022-02-15 10:04:37 +00001596
XiaokangQian7807f9f2022-02-15 10:04:37 +00001597 break;
Valerio Settic9ae8622023-07-25 11:23:50 +02001598#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_EPHEMERAL_ENABLED */
XiaokangQian7807f9f2022-02-15 10:04:37 +00001599
1600 case MBEDTLS_TLS_EXT_SUPPORTED_VERSIONS:
Ronald Cron8c527d02023-03-07 15:47:47 +01001601 /* Already parsed */
XiaokangQian7807f9f2022-02-15 10:04:37 +00001602 break;
1603
Ronald Cron41a443a2022-10-04 16:38:25 +02001604#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED)
Jerry Yue19e3b92022-07-08 12:04:51 +00001605 case MBEDTLS_TLS_EXT_PSK_KEY_EXCHANGE_MODES:
Xiaokang Qian9f1747b2023-03-30 02:50:04 +00001606 MBEDTLS_SSL_DEBUG_MSG(
1607 3, ("found psk key exchange modes extension"));
Jerry Yue19e3b92022-07-08 12:04:51 +00001608
1609 ret = ssl_tls13_parse_key_exchange_modes_ext(
Gilles Peskine449bd832023-01-11 14:50:10 +01001610 ssl, p, extension_data_end);
1611 if (ret != 0) {
Jerry Yue19e3b92022-07-08 12:04:51 +00001612 MBEDTLS_SSL_DEBUG_RET(
Gilles Peskine449bd832023-01-11 14:50:10 +01001613 1, "ssl_tls13_parse_key_exchange_modes_ext", ret);
1614 return ret;
Jerry Yue19e3b92022-07-08 12:04:51 +00001615 }
1616
Jerry Yue19e3b92022-07-08 12:04:51 +00001617 break;
Ronald Cron41a443a2022-10-04 16:38:25 +02001618#endif
Jerry Yue19e3b92022-07-08 12:04:51 +00001619
Jerry Yu1c105562022-07-10 06:32:38 +00001620 case MBEDTLS_TLS_EXT_PRE_SHARED_KEY:
Gilles Peskine449bd832023-01-11 14:50:10 +01001621 MBEDTLS_SSL_DEBUG_MSG(3, ("found pre_shared_key extension"));
1622 if ((handshake->received_extensions &
1623 MBEDTLS_SSL_EXT_MASK(PSK_KEY_EXCHANGE_MODES)) == 0) {
Jerry Yu13ab81d2022-07-22 23:17:11 +08001624 MBEDTLS_SSL_PEND_FATAL_ALERT(
1625 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER,
Gilles Peskine449bd832023-01-11 14:50:10 +01001626 MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER);
1627 return MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER;
Jerry Yu13ab81d2022-07-22 23:17:11 +08001628 }
Ronald Cron41a443a2022-10-04 16:38:25 +02001629#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED)
Jerry Yu1c105562022-07-10 06:32:38 +00001630 /* Delay processing of the PSK identity once we have
1631 * found out which algorithms to use. We keep a pointer
1632 * to the buffer and the size for later processing.
1633 */
Jerry Yu29d9faa2022-08-23 17:52:45 +08001634 pre_shared_key_ext = p;
Jerry Yu1c105562022-07-10 06:32:38 +00001635 pre_shared_key_ext_end = extension_data_end;
Jerry Yue18dc7e2022-08-04 16:29:22 +08001636#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED */
Jerry Yu1c105562022-07-10 06:32:38 +00001637 break;
Jerry Yu1c105562022-07-10 06:32:38 +00001638
XiaokangQianacb39922022-06-17 10:18:48 +00001639#if defined(MBEDTLS_SSL_ALPN)
1640 case MBEDTLS_TLS_EXT_ALPN:
Gilles Peskine449bd832023-01-11 14:50:10 +01001641 MBEDTLS_SSL_DEBUG_MSG(3, ("found alpn extension"));
XiaokangQianacb39922022-06-17 10:18:48 +00001642
Gilles Peskine449bd832023-01-11 14:50:10 +01001643 ret = mbedtls_ssl_parse_alpn_ext(ssl, p, extension_data_end);
1644 if (ret != 0) {
XiaokangQianacb39922022-06-17 10:18:48 +00001645 MBEDTLS_SSL_DEBUG_RET(
Gilles Peskine449bd832023-01-11 14:50:10 +01001646 1, ("mbedtls_ssl_parse_alpn_ext"), ret);
1647 return ret;
XiaokangQianacb39922022-06-17 10:18:48 +00001648 }
XiaokangQianacb39922022-06-17 10:18:48 +00001649 break;
1650#endif /* MBEDTLS_SSL_ALPN */
1651
Ronald Cron928cbd32022-10-04 16:14:26 +02001652#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED)
XiaokangQian7807f9f2022-02-15 10:04:37 +00001653 case MBEDTLS_TLS_EXT_SIG_ALG:
Gilles Peskine449bd832023-01-11 14:50:10 +01001654 MBEDTLS_SSL_DEBUG_MSG(3, ("found signature_algorithms extension"));
XiaokangQian7807f9f2022-02-15 10:04:37 +00001655
Gabor Mezei078e8032022-04-27 21:17:56 +02001656 ret = mbedtls_ssl_parse_sig_alg_ext(
Gilles Peskine449bd832023-01-11 14:50:10 +01001657 ssl, p, extension_data_end);
1658 if (ret != 0) {
Xiaokang Qian49f39c12023-04-06 02:31:02 +00001659 MBEDTLS_SSL_DEBUG_RET(
1660 1, "mbedtls_ssl_parse_sig_alg_ext", ret);
Gilles Peskine449bd832023-01-11 14:50:10 +01001661 return ret;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001662 }
XiaokangQian7807f9f2022-02-15 10:04:37 +00001663 break;
Ronald Cron928cbd32022-10-04 16:14:26 +02001664#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED */
XiaokangQian7807f9f2022-02-15 10:04:37 +00001665
Jan Bruckner151f6422023-02-10 12:45:19 +01001666#if defined(MBEDTLS_SSL_RECORD_SIZE_LIMIT)
1667 case MBEDTLS_TLS_EXT_RECORD_SIZE_LIMIT:
1668 MBEDTLS_SSL_DEBUG_MSG(3, ("found record_size_limit extension"));
1669
Xiaokang Qian9f1747b2023-03-30 02:50:04 +00001670 ret = mbedtls_ssl_tls13_parse_record_size_limit_ext(
1671 ssl, p, extension_data_end);
Jan Brucknerf482dcc2023-03-15 09:09:06 +01001672 if (ret != 0) {
1673 MBEDTLS_SSL_DEBUG_RET(
1674 1, ("mbedtls_ssl_tls13_parse_record_size_limit_ext"), ret);
1675 return ret;
1676 }
Jan Bruckner151f6422023-02-10 12:45:19 +01001677 break;
1678#endif /* MBEDTLS_SSL_RECORD_SIZE_LIMIT */
1679
XiaokangQian7807f9f2022-02-15 10:04:37 +00001680 default:
Jerry Yu79aa7212022-11-08 21:30:21 +08001681 MBEDTLS_SSL_PRINT_EXT(
Jerry Yu63a459c2022-10-31 13:38:40 +08001682 3, MBEDTLS_SSL_HS_CLIENT_HELLO,
Gilles Peskine449bd832023-01-11 14:50:10 +01001683 extension_type, "( ignored )");
Jerry Yu0c354a22022-08-29 15:25:36 +08001684 break;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001685 }
1686
1687 p += extension_data_len;
1688 }
1689
Gilles Peskine449bd832023-01-11 14:50:10 +01001690 MBEDTLS_SSL_PRINT_EXTS(3, MBEDTLS_SSL_HS_CLIENT_HELLO,
1691 handshake->received_extensions);
Jerry Yue18dc7e2022-08-04 16:29:22 +08001692
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +01001693 ret = mbedtls_ssl_add_hs_hdr_to_checksum(ssl,
1694 MBEDTLS_SSL_HS_CLIENT_HELLO,
1695 p - buf);
1696 if (0 != ret) {
1697 MBEDTLS_SSL_DEBUG_RET(1, ("mbedtls_ssl_add_hs_hdr_to_checksum"), ret);
1698 return ret;
1699 }
Jerry Yu032b15ce2022-07-11 06:10:03 +00001700
Ronald Cron41a443a2022-10-04 16:38:25 +02001701#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED)
XiaokangQian7807f9f2022-02-15 10:04:37 +00001702 /* Update checksum with either
1703 * - The entire content of the CH message, if no PSK extension is present
1704 * - The content up to but excluding the PSK extension, if present.
Ronald Cron12e72f12024-02-14 15:49:38 +01001705 * Always parse the pre-shared key extension when present in the
1706 * ClientHello even if some pre-requisites for PSK key exchange modes are
1707 * not met. That way we always validate the syntax of the extension.
XiaokangQian7807f9f2022-02-15 10:04:37 +00001708 */
Ronald Cron12e72f12024-02-14 15:49:38 +01001709 if (handshake->received_extensions & MBEDTLS_SSL_EXT_MASK(PRE_SHARED_KEY)) {
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +01001710 ret = handshake->update_checksum(ssl, buf,
1711 pre_shared_key_ext - buf);
1712 if (0 != ret) {
1713 MBEDTLS_SSL_DEBUG_RET(1, ("update_checksum"), ret);
1714 return ret;
1715 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001716 ret = ssl_tls13_parse_pre_shared_key_ext(ssl,
1717 pre_shared_key_ext,
1718 pre_shared_key_ext_end,
1719 cipher_suites,
1720 cipher_suites_end);
1721 if (ret == MBEDTLS_ERR_SSL_UNKNOWN_IDENTITY) {
1722 handshake->received_extensions &= ~MBEDTLS_SSL_EXT_MASK(PRE_SHARED_KEY);
1723 } else if (ret != 0) {
Jerry Yu63a459c2022-10-31 13:38:40 +08001724 MBEDTLS_SSL_DEBUG_RET(
Gilles Peskine449bd832023-01-11 14:50:10 +01001725 1, "ssl_tls13_parse_pre_shared_key_ext", ret);
1726 return ret;
Jerry Yu1c105562022-07-10 06:32:38 +00001727 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001728 } else
Ronald Cron41a443a2022-10-04 16:38:25 +02001729#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED */
Jerry Yu1c105562022-07-10 06:32:38 +00001730 {
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +01001731 ret = handshake->update_checksum(ssl, buf, p - buf);
1732 if (0 != ret) {
1733 MBEDTLS_SSL_DEBUG_RET(1, ("update_checksum"), ret);
1734 return ret;
1735 }
Jerry Yu1c105562022-07-10 06:32:38 +00001736 }
XiaokangQian7807f9f2022-02-15 10:04:37 +00001737
Gilles Peskine449bd832023-01-11 14:50:10 +01001738 ret = ssl_tls13_determine_key_exchange_mode(ssl);
1739 if (ret < 0) {
1740 return ret;
1741 }
XiaokangQian7807f9f2022-02-15 10:04:37 +00001742
Ronald Cron8a74f072023-06-14 17:59:29 +02001743 if (ssl->handshake->key_exchange_mode !=
1744 MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK) {
1745 hrr_required = (no_usable_share_for_key_agreement != 0);
1746 }
1747
Gilles Peskine449bd832023-01-11 14:50:10 +01001748 mbedtls_ssl_optimize_checksum(ssl, handshake->ciphersuite_info);
Jerry Yue5834fd2022-08-29 20:16:09 +08001749
Gilles Peskine449bd832023-01-11 14:50:10 +01001750 return hrr_required ? SSL_CLIENT_HELLO_HRR_REQUIRED : SSL_CLIENT_HELLO_OK;
XiaokangQian75fe8c72022-06-15 09:42:45 +00001751}
1752
Jerry Yuab0da372023-02-08 13:55:24 +08001753#if defined(MBEDTLS_SSL_EARLY_DATA)
Jerry Yu4caf3ca2023-11-15 16:13:47 +08001754static int ssl_tls13_check_early_data_requirements(mbedtls_ssl_context *ssl)
Jerry Yuab0da372023-02-08 13:55:24 +08001755{
1756 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
1757
Jerry Yu985c9672022-12-04 14:06:30 +08001758 if (ssl->conf->early_data_enabled == MBEDTLS_SSL_EARLY_DATA_DISABLED) {
1759 MBEDTLS_SSL_DEBUG_MSG(
Jerry Yu985c9672022-12-04 14:06:30 +08001760 1,
Jerry Yu454dda32023-10-31 15:13:54 +08001761 ("EarlyData: rejected, feature disabled in server configuration."));
Jerry Yu4caf3ca2023-11-15 16:13:47 +08001762 return -1;
Ronald Cron7b6ee942024-01-12 10:29:55 +01001763 }
1764
Jerry Yu454dda32023-10-31 15:13:54 +08001765 if (!handshake->resume) {
1766 /* We currently support early data only in the case of PSKs established
1767 via a NewSessionTicket message thus in the case of a session
1768 resumption. */
Jerry Yu985c9672022-12-04 14:06:30 +08001769 MBEDTLS_SSL_DEBUG_MSG(
Jerry Yu7ef9fd82023-11-07 14:30:38 +08001770 1, ("EarlyData: rejected, not a session resumption."));
Jerry Yu4caf3ca2023-11-15 16:13:47 +08001771 return -1;
Jerry Yu985c9672022-12-04 14:06:30 +08001772 }
1773
Jerry Yu82fd6c12023-10-31 16:32:19 +08001774 /* RFC 8446 4.2.10
1775 *
1776 * In order to accept early data, the server MUST have accepted a PSK cipher
1777 * suite and selected the first key offered in the client's "pre_shared_key"
1778 * extension. In addition, it MUST verify that the following values are the
1779 * same as those associated with the selected PSK:
1780 * - The TLS version number
1781 * - The selected cipher suite
1782 * - The selected ALPN [RFC7301] protocol, if any
1783 *
1784 * NOTE:
Jerry Yu7ef9fd82023-11-07 14:30:38 +08001785 * - The TLS version number is checked in
1786 * ssl_tls13_offered_psks_check_identity_match_ticket().
1787 * - ALPN is not checked for the time being (TODO).
Jerry Yu82fd6c12023-10-31 16:32:19 +08001788 */
1789
1790 if (handshake->selected_identity != 0) {
1791 MBEDTLS_SSL_DEBUG_MSG(
Jerry Yu7ef9fd82023-11-07 14:30:38 +08001792 1, ("EarlyData: rejected, the selected key in "
1793 "`pre_shared_key` is not the first one."));
Jerry Yu4caf3ca2023-11-15 16:13:47 +08001794 return -1;
Jerry Yu82fd6c12023-10-31 16:32:19 +08001795 }
1796
1797 if (handshake->ciphersuite_info->id !=
1798 ssl->session_negotiate->ciphersuite) {
1799 MBEDTLS_SSL_DEBUG_MSG(
Jerry Yu7ef9fd82023-11-07 14:30:38 +08001800 1, ("EarlyData: rejected, the selected ciphersuite is not the one "
1801 "of the selected pre-shared key."));
Jerry Yu4caf3ca2023-11-15 16:13:47 +08001802 return -1;
Jerry Yu82fd6c12023-10-31 16:32:19 +08001803
1804 }
Jerry Yu985c9672022-12-04 14:06:30 +08001805
Pengyu Lv94a42cc2023-12-06 10:04:17 +08001806 if (!mbedtls_ssl_tls13_session_ticket_allow_early_data(ssl->session_negotiate)) {
Jerry Yufceddb32022-12-12 15:30:34 +08001807 MBEDTLS_SSL_DEBUG_MSG(
1808 1,
Jerry Yuea96ac32023-11-21 17:06:36 +08001809 ("EarlyData: rejected, early_data not allowed in ticket "
1810 "permission bits."));
Jerry Yu4caf3ca2023-11-15 16:13:47 +08001811 return -1;
Jerry Yufceddb32022-12-12 15:30:34 +08001812 }
Jerry Yu985c9672022-12-04 14:06:30 +08001813
Jerry Yu4caf3ca2023-11-15 16:13:47 +08001814 return 0;
Jerry Yuab0da372023-02-08 13:55:24 +08001815}
1816#endif /* MBEDTLS_SSL_EARLY_DATA */
1817
XiaokangQian75fe8c72022-06-15 09:42:45 +00001818/* Update the handshake state machine */
1819
Ronald Cronce7d76e2022-07-08 18:56:49 +02001820MBEDTLS_CHECK_RETURN_CRITICAL
Ronald Cron7b6ee942024-01-12 10:29:55 +01001821static int ssl_tls13_postprocess_client_hello(mbedtls_ssl_context *ssl,
1822 int hrr_required)
XiaokangQian75fe8c72022-06-15 09:42:45 +00001823{
1824 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1825
XiaokangQian7807f9f2022-02-15 10:04:37 +00001826 /*
XiaokangQianfb665a82022-06-15 03:57:21 +00001827 * Server certificate selection
1828 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001829 if (ssl->conf->f_cert_cb && (ret = ssl->conf->f_cert_cb(ssl)) != 0) {
1830 MBEDTLS_SSL_DEBUG_RET(1, "f_cert_cb", ret);
1831 return ret;
XiaokangQianfb665a82022-06-15 03:57:21 +00001832 }
1833#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
1834 ssl->handshake->sni_name = NULL;
1835 ssl->handshake->sni_name_len = 0;
1836#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
XiaokangQian7807f9f2022-02-15 10:04:37 +00001837
Gilles Peskine449bd832023-01-11 14:50:10 +01001838 ret = mbedtls_ssl_tls13_key_schedule_stage_early(ssl);
1839 if (ret != 0) {
1840 MBEDTLS_SSL_DEBUG_RET(1,
1841 "mbedtls_ssl_tls1_3_key_schedule_stage_early", ret);
1842 return ret;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001843 }
1844
Jerry Yuab0da372023-02-08 13:55:24 +08001845#if defined(MBEDTLS_SSL_EARLY_DATA)
Jerry Yu4caf3ca2023-11-15 16:13:47 +08001846 if (ssl->handshake->received_extensions & MBEDTLS_SSL_EXT_MASK(EARLY_DATA)) {
Ronald Cron31e2d832024-02-05 16:45:57 +01001847 ssl->handshake->early_data_accepted =
1848 (!hrr_required) && (ssl_tls13_check_early_data_requirements(ssl) == 0);
Jerry Yu4e9b70e2022-12-04 14:08:02 +08001849
Jerry Yu4caf3ca2023-11-15 16:13:47 +08001850 if (ssl->handshake->early_data_accepted) {
1851 ret = mbedtls_ssl_tls13_compute_early_transform(ssl);
1852 if (ret != 0) {
1853 MBEDTLS_SSL_DEBUG_RET(
1854 1, "mbedtls_ssl_tls13_compute_early_transform", ret);
1855 return ret;
1856 }
1857 } else {
1858 ssl->discard_early_data_record =
1859 hrr_required ?
1860 MBEDTLS_SSL_EARLY_DATA_DISCARD :
1861 MBEDTLS_SSL_EARLY_DATA_TRY_TO_DEPROTECT_AND_DISCARD;
Jerry Yu4e9b70e2022-12-04 14:08:02 +08001862 }
1863 }
Ronald Cron7b6ee942024-01-12 10:29:55 +01001864#else
1865 ((void) hrr_required);
Jerry Yuab0da372023-02-08 13:55:24 +08001866#endif /* MBEDTLS_SSL_EARLY_DATA */
1867
Gilles Peskine449bd832023-01-11 14:50:10 +01001868 return 0;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001869}
1870
1871/*
XiaokangQianed582dd2022-04-13 08:21:05 +00001872 * Main entry point from the state machine; orchestrates the otherfunctions.
1873 */
1874
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001875MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01001876static int ssl_tls13_process_client_hello(mbedtls_ssl_context *ssl)
XiaokangQianed582dd2022-04-13 08:21:05 +00001877{
1878
XiaokangQian08037552022-04-20 07:16:41 +00001879 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine449bd832023-01-11 14:50:10 +01001880 unsigned char *buf = NULL;
XiaokangQianed582dd2022-04-13 08:21:05 +00001881 size_t buflen = 0;
Jerry Yu4ca91402022-05-09 15:50:57 +08001882 int parse_client_hello_ret;
1883
Gilles Peskine449bd832023-01-11 14:50:10 +01001884 MBEDTLS_SSL_DEBUG_MSG(2, ("=> parse client hello"));
XiaokangQianed582dd2022-04-13 08:21:05 +00001885
Gilles Peskine449bd832023-01-11 14:50:10 +01001886 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_tls13_fetch_handshake_msg(
1887 ssl, MBEDTLS_SSL_HS_CLIENT_HELLO,
1888 &buf, &buflen));
XiaokangQianed582dd2022-04-13 08:21:05 +00001889
Gilles Peskine449bd832023-01-11 14:50:10 +01001890 MBEDTLS_SSL_PROC_CHK_NEG(ssl_tls13_parse_client_hello(ssl, buf,
1891 buf + buflen));
Ronald Cron5af4c7f2023-03-07 20:46:59 +01001892 parse_client_hello_ret = ret; /* Store positive return value of
1893 * parse_client_hello,
1894 * as negative error codes are handled
Jerry Yuf41553b2022-05-09 22:20:30 +08001895 * by MBEDTLS_SSL_PROC_CHK_NEG. */
Jerry Yu4ca91402022-05-09 15:50:57 +08001896
Ronald Cronb828c7d2023-04-03 16:37:22 +02001897 /*
Yanray Wang90acdc62023-12-08 10:29:42 +08001898 * Version 1.2 of the protocol has to be used for the handshake.
1899 * If TLS 1.2 is not supported, abort the handshake. Otherwise, set the
Ronald Cronb828c7d2023-04-03 16:37:22 +02001900 * ssl->keep_current_message flag for the ClientHello to be kept and parsed
1901 * as a TLS 1.2 ClientHello. We also change ssl->tls_version to
1902 * MBEDTLS_SSL_VERSION_TLS1_2 thus from now on mbedtls_ssl_handshake_step()
1903 * will dispatch to the TLS 1.2 state machine.
1904 */
Ronald Cron5af4c7f2023-03-07 20:46:59 +01001905 if (SSL_CLIENT_HELLO_TLS1_2 == parse_client_hello_ret) {
Yanray Wangfb0f47b2023-12-04 15:27:28 +08001906 /* Check if server supports TLS 1.2 */
Yanray Wang408ba6f2023-12-08 10:18:03 +08001907 if (!mbedtls_ssl_conf_is_tls12_enabled(ssl->conf)) {
Yanray Wangfb0f47b2023-12-04 15:27:28 +08001908 MBEDTLS_SSL_DEBUG_MSG(
Yanray Wang177e49a2023-12-08 10:51:04 +08001909 1, ("TLS 1.2 not supported."));
Yanray Wangfb0f47b2023-12-04 15:27:28 +08001910 MBEDTLS_SSL_PEND_FATAL_ALERT(
Yanray Wang2bef9172023-12-08 10:21:53 +08001911 MBEDTLS_SSL_ALERT_MSG_PROTOCOL_VERSION,
1912 MBEDTLS_ERR_SSL_BAD_PROTOCOL_VERSION);
1913 return MBEDTLS_ERR_SSL_BAD_PROTOCOL_VERSION;
Yanray Wangfb0f47b2023-12-04 15:27:28 +08001914 }
Ronald Cron5af4c7f2023-03-07 20:46:59 +01001915 ssl->keep_current_message = 1;
1916 ssl->tls_version = MBEDTLS_SSL_VERSION_TLS1_2;
1917 return 0;
1918 }
1919
Ronald Cron7b6ee942024-01-12 10:29:55 +01001920 MBEDTLS_SSL_PROC_CHK(
1921 ssl_tls13_postprocess_client_hello(ssl, parse_client_hello_ret ==
1922 SSL_CLIENT_HELLO_HRR_REQUIRED));
Jerry Yu582dd062022-04-22 21:59:01 +08001923
Ronald Cron5af4c7f2023-03-07 20:46:59 +01001924 if (SSL_CLIENT_HELLO_OK == parse_client_hello_ret) {
Gilles Peskine449bd832023-01-11 14:50:10 +01001925 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_SERVER_HELLO);
1926 } else {
1927 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_HELLO_RETRY_REQUEST);
1928 }
XiaokangQianed582dd2022-04-13 08:21:05 +00001929
1930cleanup:
1931
Gilles Peskine449bd832023-01-11 14:50:10 +01001932 MBEDTLS_SSL_DEBUG_MSG(2, ("<= parse client hello"));
1933 return ret;
XiaokangQianed582dd2022-04-13 08:21:05 +00001934}
1935
1936/*
Jerry Yu1c3e6882022-04-20 21:23:40 +08001937 * Handler for MBEDTLS_SSL_SERVER_HELLO
Jerry Yu5b64ae92022-03-30 17:15:02 +08001938 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001939MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01001940static int ssl_tls13_prepare_server_hello(mbedtls_ssl_context *ssl)
Jerry Yuf4b27e42022-03-30 17:32:21 +08001941{
Jerry Yu637a3f12022-04-20 21:37:58 +08001942 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1943 unsigned char *server_randbytes =
Gilles Peskine449bd832023-01-11 14:50:10 +01001944 ssl->handshake->randbytes + MBEDTLS_CLIENT_HELLO_RANDOM_LEN;
1945 if (ssl->conf->f_rng == NULL) {
1946 MBEDTLS_SSL_DEBUG_MSG(1, ("no RNG provided"));
1947 return MBEDTLS_ERR_SSL_NO_RNG;
Jerry Yuf4b27e42022-03-30 17:32:21 +08001948 }
1949
Gilles Peskine449bd832023-01-11 14:50:10 +01001950 if ((ret = ssl->conf->f_rng(ssl->conf->p_rng, server_randbytes,
1951 MBEDTLS_SERVER_HELLO_RANDOM_LEN)) != 0) {
1952 MBEDTLS_SSL_DEBUG_RET(1, "f_rng", ret);
1953 return ret;
Jerry Yuf4b27e42022-03-30 17:32:21 +08001954 }
1955
Gilles Peskine449bd832023-01-11 14:50:10 +01001956 MBEDTLS_SSL_DEBUG_BUF(3, "server hello, random bytes", server_randbytes,
1957 MBEDTLS_SERVER_HELLO_RANDOM_LEN);
Jerry Yuf4b27e42022-03-30 17:32:21 +08001958
1959#if defined(MBEDTLS_HAVE_TIME)
YxCda609132023-05-22 12:08:12 -07001960 ssl->session_negotiate->start = mbedtls_time(NULL);
Jerry Yuf4b27e42022-03-30 17:32:21 +08001961#endif /* MBEDTLS_HAVE_TIME */
1962
Gilles Peskine449bd832023-01-11 14:50:10 +01001963 return ret;
Jerry Yuf4b27e42022-03-30 17:32:21 +08001964}
1965
Jerry Yu3bf2c642022-03-30 22:02:12 +08001966/*
Jerry Yue74e04a2022-04-21 09:23:16 +08001967 * ssl_tls13_write_server_hello_supported_versions_ext ():
Jerry Yu3bf2c642022-03-30 22:02:12 +08001968 *
1969 * struct {
Jerry Yufb9f54d2022-04-06 10:08:34 +08001970 * ProtocolVersion selected_version;
Jerry Yu3bf2c642022-03-30 22:02:12 +08001971 * } SupportedVersions;
1972 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001973MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yue74e04a2022-04-21 09:23:16 +08001974static int ssl_tls13_write_server_hello_supported_versions_ext(
Gilles Peskine449bd832023-01-11 14:50:10 +01001975 mbedtls_ssl_context *ssl,
1976 unsigned char *buf,
1977 unsigned char *end,
1978 size_t *out_len)
Jerry Yu3bf2c642022-03-30 22:02:12 +08001979{
Jerry Yu3bf2c642022-03-30 22:02:12 +08001980 *out_len = 0;
1981
Gilles Peskine449bd832023-01-11 14:50:10 +01001982 MBEDTLS_SSL_DEBUG_MSG(3, ("server hello, write selected version"));
Jerry Yu3bf2c642022-03-30 22:02:12 +08001983
1984 /* Check if we have space to write the extension:
1985 * - extension_type (2 bytes)
1986 * - extension_data_length (2 bytes)
Jerry Yu349a6132022-04-14 20:52:56 +08001987 * - selected_version (2 bytes)
Jerry Yu3bf2c642022-03-30 22:02:12 +08001988 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001989 MBEDTLS_SSL_CHK_BUF_PTR(buf, end, 6);
Jerry Yu3bf2c642022-03-30 22:02:12 +08001990
Gilles Peskine449bd832023-01-11 14:50:10 +01001991 MBEDTLS_PUT_UINT16_BE(MBEDTLS_TLS_EXT_SUPPORTED_VERSIONS, buf, 0);
Jerry Yu3bf2c642022-03-30 22:02:12 +08001992
Gilles Peskine449bd832023-01-11 14:50:10 +01001993 MBEDTLS_PUT_UINT16_BE(2, buf, 2);
Jerry Yu3bf2c642022-03-30 22:02:12 +08001994
Gilles Peskine449bd832023-01-11 14:50:10 +01001995 mbedtls_ssl_write_version(buf + 4,
1996 ssl->conf->transport,
1997 ssl->tls_version);
Jerry Yu3bf2c642022-03-30 22:02:12 +08001998
Gilles Peskine449bd832023-01-11 14:50:10 +01001999 MBEDTLS_SSL_DEBUG_MSG(3, ("supported version: [%04x]",
2000 ssl->tls_version));
Jerry Yu3bf2c642022-03-30 22:02:12 +08002001
Jerry Yu349a6132022-04-14 20:52:56 +08002002 *out_len = 6;
Jerry Yu3bf2c642022-03-30 22:02:12 +08002003
Jerry Yub95dd362022-11-08 21:19:34 +08002004 mbedtls_ssl_tls13_set_hs_sent_ext_mask(
Gilles Peskine449bd832023-01-11 14:50:10 +01002005 ssl, MBEDTLS_TLS_EXT_SUPPORTED_VERSIONS);
Jerry Yub95dd362022-11-08 21:19:34 +08002006
Gilles Peskine449bd832023-01-11 14:50:10 +01002007 return 0;
Jerry Yu3bf2c642022-03-30 22:02:12 +08002008}
2009
Jerry Yud9436a12022-04-20 22:28:09 +08002010
Jerry Yu3bf2c642022-03-30 22:02:12 +08002011
2012/* Generate and export a single key share. For hybrid KEMs, this can
2013 * be called multiple times with the different components of the hybrid. */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002014MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002015static int ssl_tls13_generate_and_write_key_share(mbedtls_ssl_context *ssl,
2016 uint16_t named_group,
2017 unsigned char *buf,
2018 unsigned char *end,
2019 size_t *out_len)
Jerry Yu3bf2c642022-03-30 22:02:12 +08002020{
2021 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yu955ddd72022-04-22 22:27:33 +08002022
2023 *out_len = 0;
2024
Valerio Settic9ae8622023-07-25 11:23:50 +02002025#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_EPHEMERAL_ENABLED)
Przemek Stekiel29c219c2023-05-31 15:21:04 +02002026 if (mbedtls_ssl_tls13_named_group_is_ecdhe(named_group) ||
Przemek Stekield5f79e72023-06-29 09:08:43 +02002027 mbedtls_ssl_tls13_named_group_is_ffdh(named_group)) {
Przemek Stekiel408569f2023-07-06 11:26:44 +02002028 ret = mbedtls_ssl_tls13_generate_and_write_xxdh_key_exchange(
Gilles Peskine449bd832023-01-11 14:50:10 +01002029 ssl, named_group, buf, end, out_len);
2030 if (ret != 0) {
Jerry Yu89e103c2022-03-30 22:43:29 +08002031 MBEDTLS_SSL_DEBUG_RET(
Przemek Stekiel408569f2023-07-06 11:26:44 +02002032 1, "mbedtls_ssl_tls13_generate_and_write_xxdh_key_exchange",
Gilles Peskine449bd832023-01-11 14:50:10 +01002033 ret);
2034 return ret;
Jerry Yu3bf2c642022-03-30 22:02:12 +08002035 }
Gilles Peskine449bd832023-01-11 14:50:10 +01002036 } else
Valerio Settic9ae8622023-07-25 11:23:50 +02002037#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_EPHEMERAL_ENABLED */
Gilles Peskine449bd832023-01-11 14:50:10 +01002038 if (0 /* Other kinds of KEMs */) {
2039 } else {
Jerry Yu955ddd72022-04-22 22:27:33 +08002040 ((void) ssl);
2041 ((void) named_group);
2042 ((void) buf);
2043 ((void) end);
Jerry Yu3bf2c642022-03-30 22:02:12 +08002044 ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
2045 }
2046
Gilles Peskine449bd832023-01-11 14:50:10 +01002047 return ret;
Jerry Yu3bf2c642022-03-30 22:02:12 +08002048}
2049
2050/*
2051 * ssl_tls13_write_key_share_ext
2052 *
2053 * Structure of key_share extension in ServerHello:
2054 *
Jerry Yu1c3e6882022-04-20 21:23:40 +08002055 * struct {
2056 * NamedGroup group;
2057 * opaque key_exchange<1..2^16-1>;
2058 * } KeyShareEntry;
2059 * struct {
2060 * KeyShareEntry server_share;
2061 * } KeyShareServerHello;
Jerry Yu3bf2c642022-03-30 22:02:12 +08002062 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002063MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002064static int ssl_tls13_write_key_share_ext(mbedtls_ssl_context *ssl,
2065 unsigned char *buf,
2066 unsigned char *end,
2067 size_t *out_len)
Jerry Yu3bf2c642022-03-30 22:02:12 +08002068{
Jerry Yu955ddd72022-04-22 22:27:33 +08002069 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yu3bf2c642022-03-30 22:02:12 +08002070 unsigned char *p = buf;
Jerry Yu57d48412022-04-20 21:50:42 +08002071 uint16_t group = ssl->handshake->offered_group_id;
Jerry Yu3bf2c642022-03-30 22:02:12 +08002072 unsigned char *server_share = buf + 4;
Jerry Yu3bf2c642022-03-30 22:02:12 +08002073 size_t key_exchange_length;
Jerry Yu3bf2c642022-03-30 22:02:12 +08002074
2075 *out_len = 0;
2076
Gilles Peskine449bd832023-01-11 14:50:10 +01002077 MBEDTLS_SSL_DEBUG_MSG(3, ("server hello, adding key share extension"));
Jerry Yu3bf2c642022-03-30 22:02:12 +08002078
Gilles Peskine449bd832023-01-11 14:50:10 +01002079 MBEDTLS_SSL_DEBUG_MSG(2, ("server hello, write selected_group: %s (%04x)",
2080 mbedtls_ssl_named_group_to_str(group),
2081 group));
Jerry Yu58af2332022-09-06 11:19:31 +08002082
Jerry Yu3bf2c642022-03-30 22:02:12 +08002083 /* Check if we have space for header and length fields:
2084 * - extension_type (2 bytes)
2085 * - extension_data_length (2 bytes)
2086 * - group (2 bytes)
2087 * - key_exchange_length (2 bytes)
2088 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002089 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 8);
2090 MBEDTLS_PUT_UINT16_BE(MBEDTLS_TLS_EXT_KEY_SHARE, p, 0);
2091 MBEDTLS_PUT_UINT16_BE(group, server_share, 0);
Jerry Yu3bf2c642022-03-30 22:02:12 +08002092 p += 8;
Jerry Yu57d48412022-04-20 21:50:42 +08002093
Jerry Yu3bf2c642022-03-30 22:02:12 +08002094 /* When we introduce PQC-ECDHE hybrids, we'll want to call this
2095 * function multiple times. */
Jerry Yu955ddd72022-04-22 22:27:33 +08002096 ret = ssl_tls13_generate_and_write_key_share(
Gilles Peskine449bd832023-01-11 14:50:10 +01002097 ssl, group, server_share + 4, end, &key_exchange_length);
2098 if (ret != 0) {
2099 return ret;
2100 }
Jerry Yu3bf2c642022-03-30 22:02:12 +08002101 p += key_exchange_length;
Jerry Yuc1be19f2022-04-23 16:11:39 +08002102
Gilles Peskine449bd832023-01-11 14:50:10 +01002103 MBEDTLS_PUT_UINT16_BE(key_exchange_length, server_share + 2, 0);
Jerry Yu3bf2c642022-03-30 22:02:12 +08002104
Gilles Peskine449bd832023-01-11 14:50:10 +01002105 MBEDTLS_PUT_UINT16_BE(p - server_share, buf, 2);
Jerry Yu3bf2c642022-03-30 22:02:12 +08002106
Jerry Yu57d48412022-04-20 21:50:42 +08002107 *out_len = p - buf;
Jerry Yu955ddd72022-04-22 22:27:33 +08002108
Gilles Peskine449bd832023-01-11 14:50:10 +01002109 mbedtls_ssl_tls13_set_hs_sent_ext_mask(ssl, MBEDTLS_TLS_EXT_KEY_SHARE);
Jerry Yub95dd362022-11-08 21:19:34 +08002110
Gilles Peskine449bd832023-01-11 14:50:10 +01002111 return 0;
Jerry Yu3bf2c642022-03-30 22:02:12 +08002112}
Jerry Yud9436a12022-04-20 22:28:09 +08002113
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002114MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002115static int ssl_tls13_write_hrr_key_share_ext(mbedtls_ssl_context *ssl,
2116 unsigned char *buf,
2117 unsigned char *end,
2118 size_t *out_len)
Jerry Yu23f7a6f2022-04-23 15:16:45 +08002119{
2120 uint16_t selected_group = ssl->handshake->hrr_selected_group;
2121 /* key_share Extension
2122 *
2123 * struct {
2124 * select (Handshake.msg_type) {
2125 * ...
2126 * case hello_retry_request:
2127 * NamedGroup selected_group;
2128 * ...
2129 * };
2130 * } KeyShare;
2131 */
2132
2133 *out_len = 0;
2134
Jerry Yub0ac10b2022-05-05 11:10:08 +08002135 /*
2136 * For a pure PSK key exchange, there is no group to agree upon. The purpose
2137 * of the HRR is then to transmit a cookie to force the client to demonstrate
2138 * reachability at their apparent network address (primarily useful for DTLS).
2139 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002140 if (!mbedtls_ssl_tls13_key_exchange_mode_with_ephemeral(ssl)) {
2141 return 0;
2142 }
Jerry Yu23f7a6f2022-04-23 15:16:45 +08002143
2144 /* We should only send the key_share extension if the client's initial
2145 * key share was not acceptable. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002146 if (ssl->handshake->offered_group_id != 0) {
2147 MBEDTLS_SSL_DEBUG_MSG(4, ("Skip key_share extension in HRR"));
2148 return 0;
Jerry Yu23f7a6f2022-04-23 15:16:45 +08002149 }
2150
Gilles Peskine449bd832023-01-11 14:50:10 +01002151 if (selected_group == 0) {
2152 MBEDTLS_SSL_DEBUG_MSG(1, ("no matching named group found"));
2153 return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
Jerry Yu23f7a6f2022-04-23 15:16:45 +08002154 }
2155
Jerry Yub0ac10b2022-05-05 11:10:08 +08002156 /* Check if we have enough space:
2157 * - extension_type (2 bytes)
2158 * - extension_data_length (2 bytes)
2159 * - selected_group (2 bytes)
2160 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002161 MBEDTLS_SSL_CHK_BUF_PTR(buf, end, 6);
Jerry Yu23f7a6f2022-04-23 15:16:45 +08002162
Gilles Peskine449bd832023-01-11 14:50:10 +01002163 MBEDTLS_PUT_UINT16_BE(MBEDTLS_TLS_EXT_KEY_SHARE, buf, 0);
2164 MBEDTLS_PUT_UINT16_BE(2, buf, 2);
2165 MBEDTLS_PUT_UINT16_BE(selected_group, buf, 4);
Jerry Yu23f7a6f2022-04-23 15:16:45 +08002166
Gilles Peskine449bd832023-01-11 14:50:10 +01002167 MBEDTLS_SSL_DEBUG_MSG(3,
2168 ("HRR selected_group: %s (%x)",
2169 mbedtls_ssl_named_group_to_str(selected_group),
2170 selected_group));
Jerry Yu23f7a6f2022-04-23 15:16:45 +08002171
2172 *out_len = 6;
Jerry Yu23f7a6f2022-04-23 15:16:45 +08002173
Gilles Peskine449bd832023-01-11 14:50:10 +01002174 mbedtls_ssl_tls13_set_hs_sent_ext_mask(ssl, MBEDTLS_TLS_EXT_KEY_SHARE);
Jerry Yub95dd362022-11-08 21:19:34 +08002175
Gilles Peskine449bd832023-01-11 14:50:10 +01002176 return 0;
Jerry Yu23f7a6f2022-04-23 15:16:45 +08002177}
Jerry Yu3bf2c642022-03-30 22:02:12 +08002178
2179/*
2180 * Structure of ServerHello message:
2181 *
2182 * struct {
2183 * ProtocolVersion legacy_version = 0x0303; // TLS v1.2
2184 * Random random;
2185 * opaque legacy_session_id_echo<0..32>;
2186 * CipherSuite cipher_suite;
2187 * uint8 legacy_compression_method = 0;
2188 * Extension extensions<6..2^16-1>;
2189 * } ServerHello;
2190 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002191MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002192static int ssl_tls13_write_server_hello_body(mbedtls_ssl_context *ssl,
2193 unsigned char *buf,
2194 unsigned char *end,
2195 size_t *out_len,
2196 int is_hrr)
Jerry Yu56404d72022-03-30 17:36:13 +08002197{
Jerry Yu955ddd72022-04-22 22:27:33 +08002198 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yu3bf2c642022-03-30 22:02:12 +08002199 unsigned char *p = buf;
Jerry Yud9436a12022-04-20 22:28:09 +08002200 unsigned char *p_extensions_len;
Jerry Yufbe3e642022-04-25 19:31:51 +08002201 size_t output_len;
Jerry Yu3bf2c642022-03-30 22:02:12 +08002202
2203 *out_len = 0;
Jerry Yu50e00e32022-10-31 14:45:01 +08002204 ssl->handshake->sent_extensions = MBEDTLS_SSL_EXT_MASK_NONE;
Jerry Yu3bf2c642022-03-30 22:02:12 +08002205
Jerry Yucfc04b32022-04-21 09:31:58 +08002206 /* ...
2207 * ProtocolVersion legacy_version = 0x0303; // TLS 1.2
2208 * ...
2209 * with ProtocolVersion defined as:
2210 * uint16 ProtocolVersion;
Jerry Yu3bf2c642022-03-30 22:02:12 +08002211 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002212 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 2);
2213 MBEDTLS_PUT_UINT16_BE(0x0303, p, 0);
Jerry Yu3bf2c642022-03-30 22:02:12 +08002214 p += 2;
2215
Jerry Yu1c3e6882022-04-20 21:23:40 +08002216 /* ...
2217 * Random random;
2218 * ...
Jerry Yucfc04b32022-04-21 09:31:58 +08002219 * with Random defined as:
2220 * opaque Random[MBEDTLS_SERVER_HELLO_RANDOM_LEN];
Jerry Yu1c3e6882022-04-20 21:23:40 +08002221 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002222 MBEDTLS_SSL_CHK_BUF_PTR(p, end, MBEDTLS_SERVER_HELLO_RANDOM_LEN);
2223 if (is_hrr) {
2224 memcpy(p, mbedtls_ssl_tls13_hello_retry_request_magic,
2225 MBEDTLS_SERVER_HELLO_RANDOM_LEN);
2226 } else {
2227 memcpy(p, &ssl->handshake->randbytes[MBEDTLS_CLIENT_HELLO_RANDOM_LEN],
2228 MBEDTLS_SERVER_HELLO_RANDOM_LEN);
Jerry Yu23f7a6f2022-04-23 15:16:45 +08002229 }
Gilles Peskine449bd832023-01-11 14:50:10 +01002230 MBEDTLS_SSL_DEBUG_BUF(3, "server hello, random bytes",
2231 p, MBEDTLS_SERVER_HELLO_RANDOM_LEN);
Jerry Yu3bf2c642022-03-30 22:02:12 +08002232 p += MBEDTLS_SERVER_HELLO_RANDOM_LEN;
2233
Jerry Yucfc04b32022-04-21 09:31:58 +08002234 /* ...
2235 * opaque legacy_session_id_echo<0..32>;
2236 * ...
Jerry Yu3bf2c642022-03-30 22:02:12 +08002237 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002238 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 1 + ssl->session_negotiate->id_len);
2239 *p++ = (unsigned char) ssl->session_negotiate->id_len;
2240 if (ssl->session_negotiate->id_len > 0) {
2241 memcpy(p, &ssl->session_negotiate->id[0],
2242 ssl->session_negotiate->id_len);
Jerry Yu3bf2c642022-03-30 22:02:12 +08002243 p += ssl->session_negotiate->id_len;
Jerry Yu955ddd72022-04-22 22:27:33 +08002244
Gilles Peskine449bd832023-01-11 14:50:10 +01002245 MBEDTLS_SSL_DEBUG_BUF(3, "session id", ssl->session_negotiate->id,
2246 ssl->session_negotiate->id_len);
Jerry Yu3bf2c642022-03-30 22:02:12 +08002247 }
2248
Jerry Yucfc04b32022-04-21 09:31:58 +08002249 /* ...
2250 * CipherSuite cipher_suite;
2251 * ...
2252 * with CipherSuite defined as:
2253 * uint8 CipherSuite[2];
Jerry Yu3bf2c642022-03-30 22:02:12 +08002254 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002255 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 2);
2256 MBEDTLS_PUT_UINT16_BE(ssl->session_negotiate->ciphersuite, p, 0);
Jerry Yu3bf2c642022-03-30 22:02:12 +08002257 p += 2;
Gilles Peskine449bd832023-01-11 14:50:10 +01002258 MBEDTLS_SSL_DEBUG_MSG(3,
2259 ("server hello, chosen ciphersuite: %s ( id=%d )",
2260 mbedtls_ssl_get_ciphersuite_name(
2261 ssl->session_negotiate->ciphersuite),
2262 ssl->session_negotiate->ciphersuite));
Jerry Yu3bf2c642022-03-30 22:02:12 +08002263
Jerry Yucfc04b32022-04-21 09:31:58 +08002264 /* ...
2265 * uint8 legacy_compression_method = 0;
2266 * ...
2267 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002268 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 1);
Thomas Daubney31e03a82022-07-25 15:59:25 +01002269 *p++ = MBEDTLS_SSL_COMPRESS_NULL;
Jerry Yu3bf2c642022-03-30 22:02:12 +08002270
Jerry Yucfc04b32022-04-21 09:31:58 +08002271 /* ...
2272 * Extension extensions<6..2^16-1>;
2273 * ...
2274 * struct {
2275 * ExtensionType extension_type; (2 bytes)
2276 * opaque extension_data<0..2^16-1>;
2277 * } Extension;
2278 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002279 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 2);
Jerry Yud9436a12022-04-20 22:28:09 +08002280 p_extensions_len = p;
Jerry Yu3bf2c642022-03-30 22:02:12 +08002281 p += 2;
2282
Gilles Peskine449bd832023-01-11 14:50:10 +01002283 if ((ret = ssl_tls13_write_server_hello_supported_versions_ext(
2284 ssl, p, end, &output_len)) != 0) {
Jerry Yu955ddd72022-04-22 22:27:33 +08002285 MBEDTLS_SSL_DEBUG_RET(
Gilles Peskine449bd832023-01-11 14:50:10 +01002286 1, "ssl_tls13_write_server_hello_supported_versions_ext", ret);
2287 return ret;
Jerry Yu3bf2c642022-03-30 22:02:12 +08002288 }
2289 p += output_len;
2290
Gilles Peskine449bd832023-01-11 14:50:10 +01002291 if (mbedtls_ssl_tls13_key_exchange_mode_with_ephemeral(ssl)) {
2292 if (is_hrr) {
2293 ret = ssl_tls13_write_hrr_key_share_ext(ssl, p, end, &output_len);
2294 } else {
2295 ret = ssl_tls13_write_key_share_ext(ssl, p, end, &output_len);
2296 }
2297 if (ret != 0) {
2298 return ret;
2299 }
Jerry Yu3bf2c642022-03-30 22:02:12 +08002300 p += output_len;
2301 }
Jerry Yu3bf2c642022-03-30 22:02:12 +08002302
Ronald Cron41a443a2022-10-04 16:38:25 +02002303#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01002304 if (!is_hrr && mbedtls_ssl_tls13_key_exchange_mode_with_psk(ssl)) {
2305 ret = ssl_tls13_write_server_pre_shared_key_ext(ssl, p, end, &output_len);
2306 if (ret != 0) {
2307 MBEDTLS_SSL_DEBUG_RET(1, "ssl_tls13_write_server_pre_shared_key_ext",
2308 ret);
2309 return ret;
Jerry Yu032b15ce2022-07-11 06:10:03 +00002310 }
2311 p += output_len;
2312 }
Ronald Cron41a443a2022-10-04 16:38:25 +02002313#endif
Jerry Yu032b15ce2022-07-11 06:10:03 +00002314
Gilles Peskine449bd832023-01-11 14:50:10 +01002315 MBEDTLS_PUT_UINT16_BE(p - p_extensions_len - 2, p_extensions_len, 0);
Jerry Yu3bf2c642022-03-30 22:02:12 +08002316
Gilles Peskine449bd832023-01-11 14:50:10 +01002317 MBEDTLS_SSL_DEBUG_BUF(4, "server hello extensions",
2318 p_extensions_len, p - p_extensions_len);
Jerry Yu3bf2c642022-03-30 22:02:12 +08002319
Jerry Yud9436a12022-04-20 22:28:09 +08002320 *out_len = p - buf;
Jerry Yu3bf2c642022-03-30 22:02:12 +08002321
Gilles Peskine449bd832023-01-11 14:50:10 +01002322 MBEDTLS_SSL_DEBUG_BUF(3, "server hello", buf, *out_len);
Jerry Yu3bf2c642022-03-30 22:02:12 +08002323
Jerry Yu7de2ff02022-11-08 21:43:46 +08002324 MBEDTLS_SSL_PRINT_EXTS(
Jerry Yu4b8f2f72022-10-31 13:31:22 +08002325 3, is_hrr ? MBEDTLS_SSL_TLS1_3_HS_HELLO_RETRY_REQUEST :
Gilles Peskine449bd832023-01-11 14:50:10 +01002326 MBEDTLS_SSL_HS_SERVER_HELLO,
2327 ssl->handshake->sent_extensions);
Jerry Yu4b8f2f72022-10-31 13:31:22 +08002328
Gilles Peskine449bd832023-01-11 14:50:10 +01002329 return ret;
Jerry Yu56404d72022-03-30 17:36:13 +08002330}
2331
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002332MBEDTLS_CHECK_RETURN_CRITICAL
Xiaokang Qian934ce6f2023-02-06 10:23:04 +00002333static int ssl_tls13_finalize_server_hello(mbedtls_ssl_context *ssl)
Jerry Yue110d252022-05-05 10:19:22 +08002334{
2335 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine449bd832023-01-11 14:50:10 +01002336 ret = mbedtls_ssl_tls13_compute_handshake_transform(ssl);
2337 if (ret != 0) {
2338 MBEDTLS_SSL_DEBUG_RET(1,
2339 "mbedtls_ssl_tls13_compute_handshake_transform",
2340 ret);
2341 return ret;
Jerry Yue110d252022-05-05 10:19:22 +08002342 }
2343
Gilles Peskine449bd832023-01-11 14:50:10 +01002344 return ret;
Jerry Yue110d252022-05-05 10:19:22 +08002345}
2346
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002347MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002348static int ssl_tls13_write_server_hello(mbedtls_ssl_context *ssl)
Jerry Yu5b64ae92022-03-30 17:15:02 +08002349{
Jerry Yu637a3f12022-04-20 21:37:58 +08002350 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yuf4b27e42022-03-30 17:32:21 +08002351 unsigned char *buf;
2352 size_t buf_len, msg_len;
2353
Gilles Peskine449bd832023-01-11 14:50:10 +01002354 MBEDTLS_SSL_DEBUG_MSG(2, ("=> write server hello"));
Jerry Yuf4b27e42022-03-30 17:32:21 +08002355
Gilles Peskine449bd832023-01-11 14:50:10 +01002356 MBEDTLS_SSL_PROC_CHK(ssl_tls13_prepare_server_hello(ssl));
Jerry Yuf4b27e42022-03-30 17:32:21 +08002357
Xiaokang Qian9f1747b2023-03-30 02:50:04 +00002358 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_start_handshake_msg(
2359 ssl, MBEDTLS_SSL_HS_SERVER_HELLO, &buf, &buf_len));
Jerry Yuf4b27e42022-03-30 17:32:21 +08002360
Gilles Peskine449bd832023-01-11 14:50:10 +01002361 MBEDTLS_SSL_PROC_CHK(ssl_tls13_write_server_hello_body(ssl, buf,
2362 buf + buf_len,
2363 &msg_len,
2364 0));
Jerry Yuf4b27e42022-03-30 17:32:21 +08002365
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +01002366 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_add_hs_msg_to_checksum(
Manuel Pégourié-Gonnard43cc1272023-02-06 11:48:19 +01002367 ssl, MBEDTLS_SSL_HS_SERVER_HELLO, buf, msg_len));
Jerry Yuf4b27e42022-03-30 17:32:21 +08002368
Gilles Peskine449bd832023-01-11 14:50:10 +01002369 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_finish_handshake_msg(
2370 ssl, buf_len, msg_len));
Jerry Yu637a3f12022-04-20 21:37:58 +08002371
Xiaokang Qian934ce6f2023-02-06 10:23:04 +00002372 MBEDTLS_SSL_PROC_CHK(ssl_tls13_finalize_server_hello(ssl));
Jerry Yue110d252022-05-05 10:19:22 +08002373
Gabor Mezei7b39bf12022-05-24 16:04:14 +02002374#if defined(MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE)
2375 /* The server sends a dummy change_cipher_spec record immediately
2376 * after its first handshake message. This may either be after
2377 * a ServerHello or a HelloRetryRequest.
2378 */
Gabor Mezei96ae9262022-06-28 11:45:18 +02002379 mbedtls_ssl_handshake_set_state(
Gilles Peskine449bd832023-01-11 14:50:10 +01002380 ssl, MBEDTLS_SSL_SERVER_CCS_AFTER_SERVER_HELLO);
Gabor Mezei7b39bf12022-05-24 16:04:14 +02002381#else
Gilles Peskine449bd832023-01-11 14:50:10 +01002382 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_ENCRYPTED_EXTENSIONS);
Gabor Mezei7b39bf12022-05-24 16:04:14 +02002383#endif /* MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE */
Jerry Yue110d252022-05-05 10:19:22 +08002384
Jerry Yuf4b27e42022-03-30 17:32:21 +08002385cleanup:
2386
Gilles Peskine449bd832023-01-11 14:50:10 +01002387 MBEDTLS_SSL_DEBUG_MSG(2, ("<= write server hello"));
2388 return ret;
Jerry Yu5b64ae92022-03-30 17:15:02 +08002389}
2390
Jerry Yu23d1a252022-05-12 18:08:59 +08002391
2392/*
2393 * Handler for MBEDTLS_SSL_HELLO_RETRY_REQUEST
2394 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002395MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002396static int ssl_tls13_prepare_hello_retry_request(mbedtls_ssl_context *ssl)
Jerry Yu23d1a252022-05-12 18:08:59 +08002397{
2398 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Ronald Cron5fbd2702024-02-14 10:03:36 +01002399 if (ssl->handshake->hello_retry_request_flag) {
Gilles Peskine449bd832023-01-11 14:50:10 +01002400 MBEDTLS_SSL_DEBUG_MSG(1, ("Too many HRRs"));
2401 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE,
2402 MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE);
2403 return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
Jerry Yu23d1a252022-05-12 18:08:59 +08002404 }
2405
2406 /*
2407 * Create stateless transcript hash for HRR
2408 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002409 MBEDTLS_SSL_DEBUG_MSG(4, ("Reset transcript for HRR"));
2410 ret = mbedtls_ssl_reset_transcript_for_hrr(ssl);
2411 if (ret != 0) {
2412 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_reset_transcript_for_hrr", ret);
2413 return ret;
Jerry Yu23d1a252022-05-12 18:08:59 +08002414 }
Gilles Peskine449bd832023-01-11 14:50:10 +01002415 mbedtls_ssl_session_reset_msg_layer(ssl, 0);
Jerry Yu23d1a252022-05-12 18:08:59 +08002416
Gilles Peskine449bd832023-01-11 14:50:10 +01002417 return 0;
Jerry Yu23d1a252022-05-12 18:08:59 +08002418}
2419
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002420MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002421static int ssl_tls13_write_hello_retry_request(mbedtls_ssl_context *ssl)
Jerry Yu23d1a252022-05-12 18:08:59 +08002422{
2423 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
2424 unsigned char *buf;
2425 size_t buf_len, msg_len;
2426
Gilles Peskine449bd832023-01-11 14:50:10 +01002427 MBEDTLS_SSL_DEBUG_MSG(2, ("=> write hello retry request"));
Jerry Yu23d1a252022-05-12 18:08:59 +08002428
Gilles Peskine449bd832023-01-11 14:50:10 +01002429 MBEDTLS_SSL_PROC_CHK(ssl_tls13_prepare_hello_retry_request(ssl));
Jerry Yu23d1a252022-05-12 18:08:59 +08002430
Gilles Peskine449bd832023-01-11 14:50:10 +01002431 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_start_handshake_msg(
2432 ssl, MBEDTLS_SSL_HS_SERVER_HELLO,
2433 &buf, &buf_len));
Jerry Yu23d1a252022-05-12 18:08:59 +08002434
Gilles Peskine449bd832023-01-11 14:50:10 +01002435 MBEDTLS_SSL_PROC_CHK(ssl_tls13_write_server_hello_body(ssl, buf,
2436 buf + buf_len,
2437 &msg_len,
2438 1));
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +01002439 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_add_hs_msg_to_checksum(
Manuel Pégourié-Gonnard43cc1272023-02-06 11:48:19 +01002440 ssl, MBEDTLS_SSL_HS_SERVER_HELLO, buf, msg_len));
Jerry Yu23d1a252022-05-12 18:08:59 +08002441
2442
Gilles Peskine449bd832023-01-11 14:50:10 +01002443 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_finish_handshake_msg(ssl, buf_len,
2444 msg_len));
Jerry Yu23d1a252022-05-12 18:08:59 +08002445
Ronald Cron5fbd2702024-02-14 10:03:36 +01002446 ssl->handshake->hello_retry_request_flag = 1;
Jerry Yu23d1a252022-05-12 18:08:59 +08002447
Gabor Mezei7b39bf12022-05-24 16:04:14 +02002448#if defined(MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE)
2449 /* The server sends a dummy change_cipher_spec record immediately
2450 * after its first handshake message. This may either be after
2451 * a ServerHello or a HelloRetryRequest.
2452 */
Gabor Mezei96ae9262022-06-28 11:45:18 +02002453 mbedtls_ssl_handshake_set_state(
Gilles Peskine449bd832023-01-11 14:50:10 +01002454 ssl, MBEDTLS_SSL_SERVER_CCS_AFTER_HELLO_RETRY_REQUEST);
Gabor Mezei7b39bf12022-05-24 16:04:14 +02002455#else
Gilles Peskine449bd832023-01-11 14:50:10 +01002456 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_CLIENT_HELLO);
Gabor Mezei7b39bf12022-05-24 16:04:14 +02002457#endif /* MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE */
Jerry Yu23d1a252022-05-12 18:08:59 +08002458
2459cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +01002460 MBEDTLS_SSL_DEBUG_MSG(2, ("<= write hello retry request"));
2461 return ret;
Jerry Yu23d1a252022-05-12 18:08:59 +08002462}
2463
Jerry Yu5b64ae92022-03-30 17:15:02 +08002464/*
Jerry Yu4d3841a2022-04-16 12:37:19 +08002465 * Handler for MBEDTLS_SSL_ENCRYPTED_EXTENSIONS
2466 */
Jerry Yu4d3841a2022-04-16 12:37:19 +08002467
2468/*
2469 * struct {
2470 * Extension extensions<0..2 ^ 16 - 1>;
2471 * } EncryptedExtensions;
2472 *
2473 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002474MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002475static int ssl_tls13_write_encrypted_extensions_body(mbedtls_ssl_context *ssl,
2476 unsigned char *buf,
2477 unsigned char *end,
2478 size_t *out_len)
Jerry Yu4d3841a2022-04-16 12:37:19 +08002479{
XiaokangQianacb39922022-06-17 10:18:48 +00002480 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yu4d3841a2022-04-16 12:37:19 +08002481 unsigned char *p = buf;
2482 size_t extensions_len = 0;
Jerry Yu8937eb42022-05-03 12:12:14 +08002483 unsigned char *p_extensions_len;
XiaokangQianacb39922022-06-17 10:18:48 +00002484 size_t output_len;
Jerry Yu9da5e5a2022-05-03 15:46:09 +08002485
Jerry Yu4d3841a2022-04-16 12:37:19 +08002486 *out_len = 0;
2487
Gilles Peskine449bd832023-01-11 14:50:10 +01002488 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 2);
Jerry Yu8937eb42022-05-03 12:12:14 +08002489 p_extensions_len = p;
Jerry Yu4d3841a2022-04-16 12:37:19 +08002490 p += 2;
2491
Jerry Yu8937eb42022-05-03 12:12:14 +08002492 ((void) ssl);
XiaokangQianacb39922022-06-17 10:18:48 +00002493 ((void) ret);
2494 ((void) output_len);
2495
2496#if defined(MBEDTLS_SSL_ALPN)
Gilles Peskine449bd832023-01-11 14:50:10 +01002497 ret = mbedtls_ssl_write_alpn_ext(ssl, p, end, &output_len);
2498 if (ret != 0) {
2499 return ret;
2500 }
XiaokangQian95d5f542022-06-24 02:29:26 +00002501 p += output_len;
XiaokangQianacb39922022-06-17 10:18:48 +00002502#endif /* MBEDTLS_SSL_ALPN */
Jerry Yu4d3841a2022-04-16 12:37:19 +08002503
Jerry Yu71c14f12022-12-12 11:10:35 +08002504#if defined(MBEDTLS_SSL_EARLY_DATA)
Ronald Cron78a38f62024-02-01 18:30:31 +01002505 if (ssl->handshake->early_data_accepted) {
Jerry Yu52335392023-11-23 18:06:06 +08002506 ret = mbedtls_ssl_tls13_write_early_data_ext(
Jerry Yuc59c5862023-12-05 10:40:49 +08002507 ssl, 0, p, end, &output_len);
Jerry Yu71c14f12022-12-12 11:10:35 +08002508 if (ret != 0) {
2509 return ret;
2510 }
2511 p += output_len;
2512 }
2513#endif /* MBEDTLS_SSL_EARLY_DATA */
2514
Waleed Elmelegy47d29462024-01-03 17:31:52 +00002515#if defined(MBEDTLS_SSL_RECORD_SIZE_LIMIT)
Waleed Elmelegyfbe42742024-01-05 18:11:10 +00002516 if (ssl->handshake->received_extensions & MBEDTLS_SSL_EXT_MASK(RECORD_SIZE_LIMIT)) {
Waleed Elmelegyd2fc90e2024-01-04 18:04:53 +00002517 ret = mbedtls_ssl_tls13_write_record_size_limit_ext(
2518 ssl, p, end, &output_len);
2519 if (ret != 0) {
2520 return ret;
2521 }
2522 p += output_len;
Waleed Elmelegy47d29462024-01-03 17:31:52 +00002523 }
Waleed Elmelegy47d29462024-01-03 17:31:52 +00002524#endif
2525
Gilles Peskine449bd832023-01-11 14:50:10 +01002526 extensions_len = (p - p_extensions_len) - 2;
2527 MBEDTLS_PUT_UINT16_BE(extensions_len, p_extensions_len, 0);
Jerry Yu8937eb42022-05-03 12:12:14 +08002528
2529 *out_len = p - buf;
Jerry Yu4d3841a2022-04-16 12:37:19 +08002530
Gilles Peskine449bd832023-01-11 14:50:10 +01002531 MBEDTLS_SSL_DEBUG_BUF(4, "encrypted extensions", buf, *out_len);
Jerry Yu4d3841a2022-04-16 12:37:19 +08002532
Jerry Yu7de2ff02022-11-08 21:43:46 +08002533 MBEDTLS_SSL_PRINT_EXTS(
Gilles Peskine449bd832023-01-11 14:50:10 +01002534 3, MBEDTLS_SSL_HS_ENCRYPTED_EXTENSIONS, ssl->handshake->sent_extensions);
Jerry Yu4b8f2f72022-10-31 13:31:22 +08002535
Gilles Peskine449bd832023-01-11 14:50:10 +01002536 return 0;
Jerry Yu4d3841a2022-04-16 12:37:19 +08002537}
2538
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002539MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002540static int ssl_tls13_write_encrypted_extensions(mbedtls_ssl_context *ssl)
Jerry Yu4d3841a2022-04-16 12:37:19 +08002541{
2542 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
2543 unsigned char *buf;
Jerry Yu39730a72022-05-03 12:14:04 +08002544 size_t buf_len, msg_len;
Jerry Yu4d3841a2022-04-16 12:37:19 +08002545
Gilles Peskine449bd832023-01-11 14:50:10 +01002546 mbedtls_ssl_set_outbound_transform(ssl,
2547 ssl->handshake->transform_handshake);
Gabor Mezei54719122022-06-28 11:34:56 +02002548 MBEDTLS_SSL_DEBUG_MSG(
Gilles Peskine449bd832023-01-11 14:50:10 +01002549 3, ("switching to handshake transform for outbound data"));
Gabor Mezei54719122022-06-28 11:34:56 +02002550
Gilles Peskine449bd832023-01-11 14:50:10 +01002551 MBEDTLS_SSL_DEBUG_MSG(2, ("=> write encrypted extensions"));
Jerry Yu4d3841a2022-04-16 12:37:19 +08002552
Xiaokang Qian9f1747b2023-03-30 02:50:04 +00002553 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_start_handshake_msg(
2554 ssl, MBEDTLS_SSL_HS_ENCRYPTED_EXTENSIONS,
2555 &buf, &buf_len));
Jerry Yu4d3841a2022-04-16 12:37:19 +08002556
Gilles Peskine449bd832023-01-11 14:50:10 +01002557 MBEDTLS_SSL_PROC_CHK(ssl_tls13_write_encrypted_extensions_body(
2558 ssl, buf, buf + buf_len, &msg_len));
Jerry Yu4d3841a2022-04-16 12:37:19 +08002559
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +01002560 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_add_hs_msg_to_checksum(
Xiaokang Qian9f1747b2023-03-30 02:50:04 +00002561 ssl, MBEDTLS_SSL_HS_ENCRYPTED_EXTENSIONS,
2562 buf, msg_len));
Jerry Yu4d3841a2022-04-16 12:37:19 +08002563
Gilles Peskine449bd832023-01-11 14:50:10 +01002564 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_finish_handshake_msg(
2565 ssl, buf_len, msg_len));
Jerry Yu4d3841a2022-04-16 12:37:19 +08002566
Ronald Cron928cbd32022-10-04 16:14:26 +02002567#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01002568 if (mbedtls_ssl_tls13_key_exchange_mode_with_psk(ssl)) {
2569 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_SERVER_FINISHED);
2570 } else {
2571 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_CERTIFICATE_REQUEST);
2572 }
Jerry Yu8937eb42022-05-03 12:12:14 +08002573#else
Gilles Peskine449bd832023-01-11 14:50:10 +01002574 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_SERVER_FINISHED);
Jerry Yu8937eb42022-05-03 12:12:14 +08002575#endif
2576
Jerry Yu4d3841a2022-04-16 12:37:19 +08002577cleanup:
2578
Gilles Peskine449bd832023-01-11 14:50:10 +01002579 MBEDTLS_SSL_DEBUG_MSG(2, ("<= write encrypted extensions"));
2580 return ret;
Jerry Yu4d3841a2022-04-16 12:37:19 +08002581}
2582
Ronald Cron928cbd32022-10-04 16:14:26 +02002583#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED)
XiaokangQiana987e1d2022-05-07 01:25:58 +00002584#define SSL_CERTIFICATE_REQUEST_SEND_REQUEST 0
2585#define SSL_CERTIFICATE_REQUEST_SKIP 1
2586/* Coordination:
2587 * Check whether a CertificateRequest message should be written.
2588 * Returns a negative code on failure, or
2589 * - SSL_CERTIFICATE_REQUEST_SEND_REQUEST
2590 * - SSL_CERTIFICATE_REQUEST_SKIP
2591 * indicating if the writing of the CertificateRequest
2592 * should be skipped or not.
2593 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002594MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002595static int ssl_tls13_certificate_request_coordinate(mbedtls_ssl_context *ssl)
XiaokangQiana987e1d2022-05-07 01:25:58 +00002596{
2597 int authmode;
2598
XiaokangQian40a35232022-05-07 09:02:40 +00002599#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Gilles Peskine449bd832023-01-11 14:50:10 +01002600 if (ssl->handshake->sni_authmode != MBEDTLS_SSL_VERIFY_UNSET) {
XiaokangQian40a35232022-05-07 09:02:40 +00002601 authmode = ssl->handshake->sni_authmode;
Gilles Peskine449bd832023-01-11 14:50:10 +01002602 } else
XiaokangQian40a35232022-05-07 09:02:40 +00002603#endif
XiaokangQiana987e1d2022-05-07 01:25:58 +00002604 authmode = ssl->conf->authmode;
2605
Gilles Peskine449bd832023-01-11 14:50:10 +01002606 if (authmode == MBEDTLS_SSL_VERIFY_NONE) {
Ronald Croneac00ad2022-09-13 10:16:31 +02002607 ssl->session_negotiate->verify_result = MBEDTLS_X509_BADCERT_SKIP_VERIFY;
Gilles Peskine449bd832023-01-11 14:50:10 +01002608 return SSL_CERTIFICATE_REQUEST_SKIP;
Ronald Croneac00ad2022-09-13 10:16:31 +02002609 }
XiaokangQiana987e1d2022-05-07 01:25:58 +00002610
XiaokangQianc3017f62022-05-13 05:55:41 +00002611 ssl->handshake->certificate_request_sent = 1;
XiaokangQian189ded22022-05-10 08:12:17 +00002612
Gilles Peskine449bd832023-01-11 14:50:10 +01002613 return SSL_CERTIFICATE_REQUEST_SEND_REQUEST;
XiaokangQiana987e1d2022-05-07 01:25:58 +00002614}
2615
XiaokangQiancec9ae62022-05-06 07:28:50 +00002616/*
2617 * struct {
2618 * opaque certificate_request_context<0..2^8-1>;
2619 * Extension extensions<2..2^16-1>;
2620 * } CertificateRequest;
2621 *
2622 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002623MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002624static int ssl_tls13_write_certificate_request_body(mbedtls_ssl_context *ssl,
2625 unsigned char *buf,
2626 const unsigned char *end,
2627 size_t *out_len)
XiaokangQiancec9ae62022-05-06 07:28:50 +00002628{
2629 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
2630 unsigned char *p = buf;
XiaokangQianec6efb92022-05-06 09:53:10 +00002631 size_t output_len = 0;
XiaokangQiancec9ae62022-05-06 07:28:50 +00002632 unsigned char *p_extensions_len;
2633
2634 *out_len = 0;
2635
2636 /* Check if we have enough space:
2637 * - certificate_request_context (1 byte)
2638 * - extensions length (2 bytes)
2639 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002640 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 3);
XiaokangQiancec9ae62022-05-06 07:28:50 +00002641
2642 /*
2643 * Write certificate_request_context
2644 */
2645 /*
2646 * We use a zero length context for the normal handshake
2647 * messages. For post-authentication handshake messages
2648 * this request context would be set to a non-zero value.
2649 */
2650 *p++ = 0x0;
2651
2652 /*
2653 * Write extensions
2654 */
2655 /* The extensions must contain the signature_algorithms. */
2656 p_extensions_len = p;
2657 p += 2;
Gilles Peskine449bd832023-01-11 14:50:10 +01002658 ret = mbedtls_ssl_write_sig_alg_ext(ssl, p, end, &output_len);
2659 if (ret != 0) {
2660 return ret;
2661 }
XiaokangQiancec9ae62022-05-06 07:28:50 +00002662
XiaokangQianec6efb92022-05-06 09:53:10 +00002663 p += output_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01002664 MBEDTLS_PUT_UINT16_BE(p - p_extensions_len - 2, p_extensions_len, 0);
XiaokangQiancec9ae62022-05-06 07:28:50 +00002665
2666 *out_len = p - buf;
2667
Jerry Yu7de2ff02022-11-08 21:43:46 +08002668 MBEDTLS_SSL_PRINT_EXTS(
Gilles Peskine449bd832023-01-11 14:50:10 +01002669 3, MBEDTLS_SSL_HS_CERTIFICATE_REQUEST, ssl->handshake->sent_extensions);
Jerry Yu4b8f2f72022-10-31 13:31:22 +08002670
Gilles Peskine449bd832023-01-11 14:50:10 +01002671 return 0;
XiaokangQiancec9ae62022-05-06 07:28:50 +00002672}
2673
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002674MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002675static int ssl_tls13_write_certificate_request(mbedtls_ssl_context *ssl)
XiaokangQiancec9ae62022-05-06 07:28:50 +00002676{
2677 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
2678
Gilles Peskine449bd832023-01-11 14:50:10 +01002679 MBEDTLS_SSL_DEBUG_MSG(2, ("=> write certificate request"));
XiaokangQiancec9ae62022-05-06 07:28:50 +00002680
Gilles Peskine449bd832023-01-11 14:50:10 +01002681 MBEDTLS_SSL_PROC_CHK_NEG(ssl_tls13_certificate_request_coordinate(ssl));
XiaokangQiancec9ae62022-05-06 07:28:50 +00002682
Gilles Peskine449bd832023-01-11 14:50:10 +01002683 if (ret == SSL_CERTIFICATE_REQUEST_SEND_REQUEST) {
XiaokangQiancec9ae62022-05-06 07:28:50 +00002684 unsigned char *buf;
2685 size_t buf_len, msg_len;
2686
Xiaokang Qian9f1747b2023-03-30 02:50:04 +00002687 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_start_handshake_msg(
2688 ssl, MBEDTLS_SSL_HS_CERTIFICATE_REQUEST,
2689 &buf, &buf_len));
XiaokangQiancec9ae62022-05-06 07:28:50 +00002690
Gilles Peskine449bd832023-01-11 14:50:10 +01002691 MBEDTLS_SSL_PROC_CHK(ssl_tls13_write_certificate_request_body(
2692 ssl, buf, buf + buf_len, &msg_len));
XiaokangQiancec9ae62022-05-06 07:28:50 +00002693
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +01002694 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_add_hs_msg_to_checksum(
Xiaokang Qian9f1747b2023-03-30 02:50:04 +00002695 ssl, MBEDTLS_SSL_HS_CERTIFICATE_REQUEST,
2696 buf, msg_len));
XiaokangQiancec9ae62022-05-06 07:28:50 +00002697
Gilles Peskine449bd832023-01-11 14:50:10 +01002698 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_finish_handshake_msg(
2699 ssl, buf_len, msg_len));
2700 } else if (ret == SSL_CERTIFICATE_REQUEST_SKIP) {
2701 MBEDTLS_SSL_DEBUG_MSG(2, ("<= skip write certificate request"));
XiaokangQiancec9ae62022-05-06 07:28:50 +00002702 ret = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01002703 } else {
2704 MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
XiaokangQiancec9ae62022-05-06 07:28:50 +00002705 ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
2706 goto cleanup;
2707 }
2708
Gilles Peskine449bd832023-01-11 14:50:10 +01002709 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_SERVER_CERTIFICATE);
XiaokangQiancec9ae62022-05-06 07:28:50 +00002710cleanup:
2711
Gilles Peskine449bd832023-01-11 14:50:10 +01002712 MBEDTLS_SSL_DEBUG_MSG(2, ("<= write certificate request"));
2713 return ret;
XiaokangQiancec9ae62022-05-06 07:28:50 +00002714}
XiaokangQiancec9ae62022-05-06 07:28:50 +00002715
Jerry Yu4d3841a2022-04-16 12:37:19 +08002716/*
Jerry Yuc6e6dbf2022-04-16 19:42:57 +08002717 * Handler for MBEDTLS_SSL_SERVER_CERTIFICATE
Jerry Yu83da34e2022-04-16 13:59:52 +08002718 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002719MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002720static int ssl_tls13_write_server_certificate(mbedtls_ssl_context *ssl)
Jerry Yu83da34e2022-04-16 13:59:52 +08002721{
Jerry Yu5a26f302022-05-10 20:46:40 +08002722 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
XiaokangQianfb665a82022-06-15 03:57:21 +00002723
2724#if defined(MBEDTLS_X509_CRT_PARSE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01002725 if ((ssl_tls13_pick_key_cert(ssl) != 0) ||
2726 mbedtls_ssl_own_cert(ssl) == NULL) {
2727 MBEDTLS_SSL_DEBUG_MSG(2, ("No certificate available."));
2728 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE,
2729 MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE);
2730 return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
Jerry Yu5a26f302022-05-10 20:46:40 +08002731 }
XiaokangQianfb665a82022-06-15 03:57:21 +00002732#endif /* MBEDTLS_X509_CRT_PARSE_C */
Jerry Yu5a26f302022-05-10 20:46:40 +08002733
Gilles Peskine449bd832023-01-11 14:50:10 +01002734 ret = mbedtls_ssl_tls13_write_certificate(ssl);
2735 if (ret != 0) {
2736 return ret;
2737 }
2738 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_CERTIFICATE_VERIFY);
2739 return 0;
Jerry Yu83da34e2022-04-16 13:59:52 +08002740}
2741
2742/*
Jerry Yuc6e6dbf2022-04-16 19:42:57 +08002743 * Handler for MBEDTLS_SSL_CERTIFICATE_VERIFY
Jerry Yu83da34e2022-04-16 13:59:52 +08002744 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002745MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002746static int ssl_tls13_write_certificate_verify(mbedtls_ssl_context *ssl)
Jerry Yu83da34e2022-04-16 13:59:52 +08002747{
Gilles Peskine449bd832023-01-11 14:50:10 +01002748 int ret = mbedtls_ssl_tls13_write_certificate_verify(ssl);
2749 if (ret != 0) {
2750 return ret;
2751 }
2752 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_SERVER_FINISHED);
2753 return 0;
Jerry Yu83da34e2022-04-16 13:59:52 +08002754}
Ronald Cron928cbd32022-10-04 16:14:26 +02002755#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED */
Jerry Yu83da34e2022-04-16 13:59:52 +08002756
Jerry Yu9b72e392023-12-01 16:27:08 +08002757/*
2758 * RFC 8446 section A.2
2759 *
2760 * | Send ServerHello
2761 * | K_send = handshake
2762 * | Send EncryptedExtensions
2763 * | [Send CertificateRequest]
2764 * Can send | [Send Certificate + CertificateVerify]
2765 * app data | Send Finished
2766 * after --> | K_send = application
2767 * here +--------+--------+
2768 * No 0-RTT | | 0-RTT
2769 * | |
2770 * K_recv = handshake | | K_recv = early data
2771 * [Skip decrypt errors] | +------> WAIT_EOED -+
2772 * | | Recv | | Recv EndOfEarlyData
2773 * | | early data | | K_recv = handshake
2774 * | +------------+ |
2775 * | |
2776 * +> WAIT_FLIGHT2 <--------+
2777 * |
2778 * +--------+--------+
2779 * No auth | | Client auth
2780 * | |
2781 * | v
2782 * | WAIT_CERT
2783 * | Recv | | Recv Certificate
2784 * | empty | v
2785 * | Certificate | WAIT_CV
2786 * | | | Recv
2787 * | v | CertificateVerify
2788 * +-> WAIT_FINISHED <---+
2789 * | Recv Finished
2790 *
2791 *
2792 * The following function handles the state changes after WAIT_FLIGHT2 in the
Jerry Yu3be85072023-12-04 09:58:54 +08002793 * above diagram. We are not going to receive early data related messages
2794 * anymore, prepare to receive the first handshake message of the client
2795 * second flight.
Jerry Yu9b72e392023-12-01 16:27:08 +08002796 */
Jerry Yu3be85072023-12-04 09:58:54 +08002797static void ssl_tls13_prepare_for_handshake_second_flight(
2798 mbedtls_ssl_context *ssl)
Jerry Yu9b72e392023-12-01 16:27:08 +08002799{
Jerry Yu9b72e392023-12-01 16:27:08 +08002800 if (ssl->handshake->certificate_request_sent) {
2801 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_CLIENT_CERTIFICATE);
2802 } else {
Jerry Yu42020fb2023-12-05 17:35:53 +08002803 MBEDTLS_SSL_DEBUG_MSG(2, ("skip parse certificate"));
Jerry Yuebb1b1d2023-12-05 11:02:15 +08002804 MBEDTLS_SSL_DEBUG_MSG(2, ("skip parse certificate verify"));
Jerry Yuebb1b1d2023-12-05 11:02:15 +08002805
Jerry Yu9b72e392023-12-01 16:27:08 +08002806 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_CLIENT_FINISHED);
2807 }
Jerry Yu9b72e392023-12-01 16:27:08 +08002808}
2809
Jerry Yu83da34e2022-04-16 13:59:52 +08002810/*
Jerry Yud6e253d2022-05-18 13:59:24 +08002811 * Handler for MBEDTLS_SSL_SERVER_FINISHED
Jerry Yu69dd8d42022-04-16 12:51:26 +08002812 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002813MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002814static int ssl_tls13_write_server_finished(mbedtls_ssl_context *ssl)
Jerry Yu69dd8d42022-04-16 12:51:26 +08002815{
Jerry Yud6e253d2022-05-18 13:59:24 +08002816 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yu27bdc7c2022-04-16 13:33:27 +08002817
Gilles Peskine449bd832023-01-11 14:50:10 +01002818 ret = mbedtls_ssl_tls13_write_finished_message(ssl);
2819 if (ret != 0) {
2820 return ret;
2821 }
Jerry Yu27bdc7c2022-04-16 13:33:27 +08002822
Gilles Peskine449bd832023-01-11 14:50:10 +01002823 ret = mbedtls_ssl_tls13_compute_application_transform(ssl);
2824 if (ret != 0) {
Jerry Yue3d67cb2022-05-19 15:33:10 +08002825 MBEDTLS_SSL_PEND_FATAL_ALERT(
Gilles Peskine449bd832023-01-11 14:50:10 +01002826 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE,
2827 MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE);
2828 return ret;
Jerry Yue3d67cb2022-05-19 15:33:10 +08002829 }
XiaokangQianc3017f62022-05-13 05:55:41 +00002830
Jerry Yu87b5ed42022-12-12 13:07:07 +08002831#if defined(MBEDTLS_SSL_EARLY_DATA)
Ronald Cron78a38f62024-02-01 18:30:31 +01002832 if (ssl->handshake->early_data_accepted) {
Jerry Yu0af63dc2023-12-01 17:14:51 +08002833 /* See RFC 8446 section A.2 for more information */
Jerry Yu87b5ed42022-12-12 13:07:07 +08002834 MBEDTLS_SSL_DEBUG_MSG(
2835 1, ("Switch to early keys for inbound traffic. "
2836 "( K_recv = early data )"));
2837 mbedtls_ssl_set_inbound_transform(
2838 ssl, ssl->handshake->transform_earlydata);
2839 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_END_OF_EARLY_DATA);
2840 return 0;
2841 }
2842#endif /* MBEDTLS_SSL_EARLY_DATA */
Jerry Yu0af63dc2023-12-01 17:14:51 +08002843 MBEDTLS_SSL_DEBUG_MSG(
2844 1, ("Switch to handshake keys for inbound traffic "
2845 "( K_recv = handshake )"));
Gilles Peskine449bd832023-01-11 14:50:10 +01002846 mbedtls_ssl_set_inbound_transform(ssl, ssl->handshake->transform_handshake);
XiaokangQian189ded22022-05-10 08:12:17 +00002847
Jerry Yu3be85072023-12-04 09:58:54 +08002848 ssl_tls13_prepare_for_handshake_second_flight(ssl);
Jerry Yu7d8c3fe2022-12-12 12:59:44 +08002849
2850 return 0;
2851}
2852
Jerry Yu87b5ed42022-12-12 13:07:07 +08002853#if defined(MBEDTLS_SSL_EARLY_DATA)
2854/*
Jerry Yu59d420f2023-12-01 16:30:34 +08002855 * Handler for MBEDTLS_SSL_END_OF_EARLY_DATA
Jerry Yu87b5ed42022-12-12 13:07:07 +08002856 */
Jerry Yu3be85072023-12-04 09:58:54 +08002857#define SSL_GOT_END_OF_EARLY_DATA 0
Jerry Yub55f9eb2023-12-05 10:27:17 +08002858#define SSL_GOT_EARLY_DATA 1
Jerry Yud5c34962023-12-01 16:32:31 +08002859/* Coordination:
Jerry Yu3be85072023-12-04 09:58:54 +08002860 * Deals with the ambiguity of not knowing if the next message is an
2861 * EndOfEarlyData message or an application message containing early data.
Jerry Yud5c34962023-12-01 16:32:31 +08002862 * Returns a negative code on failure, or
Jerry Yu3be85072023-12-04 09:58:54 +08002863 * - SSL_GOT_END_OF_EARLY_DATA
Jerry Yub55f9eb2023-12-05 10:27:17 +08002864 * - SSL_GOT_EARLY_DATA
Jerry Yud5c34962023-12-01 16:32:31 +08002865 * indicating which message is received.
2866 */
2867MBEDTLS_CHECK_RETURN_CRITICAL
2868static int ssl_tls13_end_of_early_data_coordinate(mbedtls_ssl_context *ssl)
2869{
2870 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yub4ed4602023-12-01 16:34:00 +08002871
2872 if ((ret = mbedtls_ssl_read_record(ssl, 0)) != 0) {
2873 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_read_record", ret);
2874 return ret;
2875 }
2876 ssl->keep_current_message = 1;
2877
2878 if (ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
2879 ssl->in_msg[0] == MBEDTLS_SSL_HS_END_OF_EARLY_DATA) {
Jerry Yub55f9eb2023-12-05 10:27:17 +08002880 MBEDTLS_SSL_DEBUG_MSG(3, ("Received an end_of_early_data message."));
Jerry Yu3be85072023-12-04 09:58:54 +08002881 return SSL_GOT_END_OF_EARLY_DATA;
Jerry Yub4ed4602023-12-01 16:34:00 +08002882 }
2883
2884 if (ssl->in_msgtype == MBEDTLS_SSL_MSG_APPLICATION_DATA) {
Jerry Yub55f9eb2023-12-05 10:27:17 +08002885 MBEDTLS_SSL_DEBUG_MSG(3, ("Received early data"));
Jerry Yu6a5904d2023-12-06 17:11:12 +08002886 /* RFC 8446 section 4.6.1
2887 *
2888 * A server receiving more than max_early_data_size bytes of 0-RTT data
2889 * SHOULD terminate the connection with an "unexpected_message" alert.
2890 *
2891 * TODO: Add received data size check here.
2892 */
Ronald Croned7d4bf2024-01-31 07:55:19 +01002893 if (ssl->in_offt == NULL) {
2894 /* Set the reading pointer */
2895 ssl->in_offt = ssl->in_msg;
2896 }
Jerry Yub55f9eb2023-12-05 10:27:17 +08002897 return SSL_GOT_EARLY_DATA;
Jerry Yub4ed4602023-12-01 16:34:00 +08002898 }
2899
Jerry Yu7bb40a32023-12-04 10:04:15 +08002900 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE,
2901 MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE);
Jerry Yub4ed4602023-12-01 16:34:00 +08002902 return MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE;
Jerry Yud5c34962023-12-01 16:32:31 +08002903}
2904
2905MBEDTLS_CHECK_RETURN_CRITICAL
2906static int ssl_tls13_parse_end_of_early_data(mbedtls_ssl_context *ssl,
2907 const unsigned char *buf,
2908 const unsigned char *end)
2909{
Jerry Yu75c9ab72023-12-01 16:41:40 +08002910 /* RFC 8446 section 4.5
2911 *
2912 * struct {} EndOfEarlyData;
2913 */
Jerry Yufbf03992023-12-04 10:00:37 +08002914 if (buf != end) {
2915 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR,
2916 MBEDTLS_ERR_SSL_DECODE_ERROR);
2917 return MBEDTLS_ERR_SSL_DECODE_ERROR;
2918 }
2919 return 0;
Jerry Yud5c34962023-12-01 16:32:31 +08002920}
2921
Jerry Yud5c34962023-12-01 16:32:31 +08002922/*
2923 * RFC 8446 section A.2
2924 *
2925 * | Send ServerHello
2926 * | K_send = handshake
2927 * | Send EncryptedExtensions
2928 * | [Send CertificateRequest]
2929 * Can send | [Send Certificate + CertificateVerify]
2930 * app data | Send Finished
2931 * after --> | K_send = application
2932 * here +--------+--------+
2933 * No 0-RTT | | 0-RTT
2934 * | |
2935 * K_recv = handshake | | K_recv = early data
2936 * [Skip decrypt errors] | +------> WAIT_EOED -+
2937 * | | Recv | | Recv EndOfEarlyData
2938 * | | early data | | K_recv = handshake
2939 * | +------------+ |
2940 * | |
2941 * +> WAIT_FLIGHT2 <--------+
2942 * |
2943 * +--------+--------+
2944 * No auth | | Client auth
2945 * | |
2946 * | v
2947 * | WAIT_CERT
2948 * | Recv | | Recv Certificate
2949 * | empty | v
2950 * | Certificate | WAIT_CV
2951 * | | | Recv
2952 * | v | CertificateVerify
2953 * +-> WAIT_FINISHED <---+
2954 * | Recv Finished
2955 *
2956 * The function handles actions and state changes from 0-RTT to WAIT_FLIGHT2 in
2957 * the above diagram.
2958 */
Jerry Yu87b5ed42022-12-12 13:07:07 +08002959MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yu59d420f2023-12-01 16:30:34 +08002960static int ssl_tls13_process_end_of_early_data(mbedtls_ssl_context *ssl)
Jerry Yu87b5ed42022-12-12 13:07:07 +08002961{
2962 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yud5c34962023-12-01 16:32:31 +08002963
2964 MBEDTLS_SSL_DEBUG_MSG(2, ("=> ssl_tls13_process_end_of_early_data"));
2965
2966 MBEDTLS_SSL_PROC_CHK_NEG(ssl_tls13_end_of_early_data_coordinate(ssl));
2967
Jerry Yu3be85072023-12-04 09:58:54 +08002968 if (ret == SSL_GOT_END_OF_EARLY_DATA) {
Jerry Yud5c34962023-12-01 16:32:31 +08002969 unsigned char *buf;
2970 size_t buf_len;
2971
2972 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_tls13_fetch_handshake_msg(
2973 ssl, MBEDTLS_SSL_HS_END_OF_EARLY_DATA,
2974 &buf, &buf_len));
2975
2976 MBEDTLS_SSL_PROC_CHK(ssl_tls13_parse_end_of_early_data(
2977 ssl, buf, buf + buf_len));
2978
Jerry Yue9655122023-12-01 16:44:40 +08002979 MBEDTLS_SSL_DEBUG_MSG(
2980 1, ("Switch to handshake keys for inbound traffic"
2981 "( K_recv = handshake )"));
2982 mbedtls_ssl_set_inbound_transform(
2983 ssl, ssl->handshake->transform_handshake);
2984
Jerry Yud5c34962023-12-01 16:32:31 +08002985 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_add_hs_msg_to_checksum(
2986 ssl, MBEDTLS_SSL_HS_END_OF_EARLY_DATA,
2987 buf, buf_len));
Jerry Yue9655122023-12-01 16:44:40 +08002988
Jerry Yu3be85072023-12-04 09:58:54 +08002989 ssl_tls13_prepare_for_handshake_second_flight(ssl);
Jerry Yud5c34962023-12-01 16:32:31 +08002990
Jerry Yub55f9eb2023-12-05 10:27:17 +08002991 } else if (ret == SSL_GOT_EARLY_DATA) {
Jerry Yud9ca3542023-12-06 17:23:52 +08002992 ret = MBEDTLS_ERR_SSL_RECEIVED_EARLY_DATA;
2993 goto cleanup;
Jerry Yud5c34962023-12-01 16:32:31 +08002994 } else {
2995 MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
2996 ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
2997 goto cleanup;
2998 }
2999
Jerry Yud5c34962023-12-01 16:32:31 +08003000cleanup:
3001 MBEDTLS_SSL_DEBUG_MSG(2, ("<= ssl_tls13_process_end_of_early_data"));
Jerry Yu87b5ed42022-12-12 13:07:07 +08003002 return ret;
3003}
3004#endif /* MBEDTLS_SSL_EARLY_DATA */
3005
Jerry Yu69dd8d42022-04-16 12:51:26 +08003006/*
Jerry Yud6e253d2022-05-18 13:59:24 +08003007 * Handler for MBEDTLS_SSL_CLIENT_FINISHED
Jerry Yu69dd8d42022-04-16 12:51:26 +08003008 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02003009MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01003010static int ssl_tls13_process_client_finished(mbedtls_ssl_context *ssl)
Jerry Yu69dd8d42022-04-16 12:51:26 +08003011{
Jerry Yud6e253d2022-05-18 13:59:24 +08003012 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
3013
Gilles Peskine449bd832023-01-11 14:50:10 +01003014 ret = mbedtls_ssl_tls13_process_finished_message(ssl);
3015 if (ret != 0) {
3016 return ret;
Jerry Yue3d67cb2022-05-19 15:33:10 +08003017 }
3018
Gilles Peskine449bd832023-01-11 14:50:10 +01003019 ret = mbedtls_ssl_tls13_compute_resumption_master_secret(ssl);
3020 if (ret != 0) {
Xiaokang Qian9f1747b2023-03-30 02:50:04 +00003021 MBEDTLS_SSL_DEBUG_RET(
3022 1, "mbedtls_ssl_tls13_compute_resumption_master_secret", ret);
Gilles Peskine449bd832023-01-11 14:50:10 +01003023 }
3024
3025 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_HANDSHAKE_WRAPUP);
3026 return 0;
Jerry Yu69dd8d42022-04-16 12:51:26 +08003027}
3028
3029/*
Jerry Yud6e253d2022-05-18 13:59:24 +08003030 * Handler for MBEDTLS_SSL_HANDSHAKE_WRAPUP
Jerry Yu69dd8d42022-04-16 12:51:26 +08003031 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02003032MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01003033static int ssl_tls13_handshake_wrapup(mbedtls_ssl_context *ssl)
Jerry Yu69dd8d42022-04-16 12:51:26 +08003034{
Gilles Peskine449bd832023-01-11 14:50:10 +01003035 MBEDTLS_SSL_DEBUG_MSG(2, ("handshake: done"));
Jerry Yu03ed50b2022-04-16 17:13:30 +08003036
Gilles Peskine449bd832023-01-11 14:50:10 +01003037 mbedtls_ssl_tls13_handshake_wrapup(ssl);
Jerry Yue67bef42022-07-07 07:29:42 +00003038
Pengyu Lv3643fdb2023-01-12 16:46:28 +08003039#if defined(MBEDTLS_SSL_SESSION_TICKETS) && \
3040 defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED)
Pengyu Lva1aa31b2022-12-13 13:49:59 +08003041/* TODO: Remove the check of SOME_PSK_ENABLED since SESSION_TICKETS requires
3042 * SOME_PSK_ENABLED to be enabled. Here is just to make CI happy. It is
3043 * expected to be resolved with issue#6395.
3044 */
Pengyu Lvc7af2c42022-12-01 16:33:00 +08003045 /* Sent NewSessionTicket message only when client supports PSK */
Pengyu Lvb2cfafb2023-11-14 13:56:13 +08003046 if (mbedtls_ssl_tls13_is_some_psk_supported(ssl)) {
Xiaokang Qian9f1747b2023-03-30 02:50:04 +00003047 mbedtls_ssl_handshake_set_state(
3048 ssl, MBEDTLS_SSL_TLS1_3_NEW_SESSION_TICKET);
Pengyu Lvc7af2c42022-12-01 16:33:00 +08003049 } else
Jerry Yufca4d572022-07-21 10:37:48 +08003050#endif
Pengyu Lv3643fdb2023-01-12 16:46:28 +08003051 {
3052 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_HANDSHAKE_OVER);
3053 }
Gilles Peskine449bd832023-01-11 14:50:10 +01003054 return 0;
Jerry Yu69dd8d42022-04-16 12:51:26 +08003055}
3056
3057/*
Jerry Yua8d3c502022-10-30 14:51:23 +08003058 * Handler for MBEDTLS_SSL_TLS1_3_NEW_SESSION_TICKET
Jerry Yue67bef42022-07-07 07:29:42 +00003059 */
3060#define SSL_NEW_SESSION_TICKET_SKIP 0
3061#define SSL_NEW_SESSION_TICKET_WRITE 1
3062MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01003063static int ssl_tls13_write_new_session_ticket_coordinate(mbedtls_ssl_context *ssl)
Jerry Yue67bef42022-07-07 07:29:42 +00003064{
3065 /* Check whether the use of session tickets is enabled */
Gilles Peskine449bd832023-01-11 14:50:10 +01003066 if (ssl->conf->f_ticket_write == NULL) {
3067 MBEDTLS_SSL_DEBUG_MSG(2, ("NewSessionTicket: disabled,"
3068 " callback is not set"));
3069 return SSL_NEW_SESSION_TICKET_SKIP;
Jerry Yud0766ec2022-09-22 10:46:57 +08003070 }
Gilles Peskine449bd832023-01-11 14:50:10 +01003071 if (ssl->conf->new_session_tickets_count == 0) {
3072 MBEDTLS_SSL_DEBUG_MSG(2, ("NewSessionTicket: disabled,"
3073 " configured count is zero"));
3074 return SSL_NEW_SESSION_TICKET_SKIP;
Jerry Yud0766ec2022-09-22 10:46:57 +08003075 }
3076
Gilles Peskine449bd832023-01-11 14:50:10 +01003077 if (ssl->handshake->new_session_tickets_count == 0) {
3078 MBEDTLS_SSL_DEBUG_MSG(2, ("NewSessionTicket: all tickets have "
3079 "been sent."));
3080 return SSL_NEW_SESSION_TICKET_SKIP;
Jerry Yue67bef42022-07-07 07:29:42 +00003081 }
3082
Gilles Peskine449bd832023-01-11 14:50:10 +01003083 return SSL_NEW_SESSION_TICKET_WRITE;
Jerry Yue67bef42022-07-07 07:29:42 +00003084}
3085
3086#if defined(MBEDTLS_SSL_SESSION_TICKETS)
3087MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01003088static int ssl_tls13_prepare_new_session_ticket(mbedtls_ssl_context *ssl,
3089 unsigned char *ticket_nonce,
3090 size_t ticket_nonce_size)
Jerry Yue67bef42022-07-07 07:29:42 +00003091{
3092 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
3093 mbedtls_ssl_session *session = ssl->session;
3094 mbedtls_ssl_ciphersuite_t *ciphersuite_info;
3095 psa_algorithm_t psa_hash_alg;
3096 int hash_length;
3097
Gilles Peskine449bd832023-01-11 14:50:10 +01003098 MBEDTLS_SSL_DEBUG_MSG(2, ("=> prepare NewSessionTicket msg"));
Jerry Yue67bef42022-07-07 07:29:42 +00003099
Pengyu Lv9f926952022-11-17 15:22:33 +08003100 /* Set ticket_flags depends on the advertised psk key exchange mode */
Pengyu Lv94a42cc2023-12-06 10:04:17 +08003101 mbedtls_ssl_tls13_session_clear_ticket_flags(
Pengyu Lv9eacb442022-12-09 14:39:19 +08003102 session, MBEDTLS_SSL_TLS1_3_TICKET_FLAGS_MASK);
Pengyu Lve6487fe2022-12-06 09:30:29 +08003103#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED)
Pengyu Lv94a42cc2023-12-06 10:04:17 +08003104 mbedtls_ssl_tls13_session_set_ticket_flags(
Pengyu Lv9eacb442022-12-09 14:39:19 +08003105 session, ssl->handshake->tls13_kex_modes);
Pengyu Lve6487fe2022-12-06 09:30:29 +08003106#endif
Jerry Yu95648b02023-12-06 15:03:34 +08003107
3108#if defined(MBEDTLS_SSL_EARLY_DATA)
3109 if (ssl->conf->early_data_enabled == MBEDTLS_SSL_EARLY_DATA_ENABLED &&
3110 ssl->conf->max_early_data_size > 0) {
Pengyu Lv94a42cc2023-12-06 10:04:17 +08003111 mbedtls_ssl_tls13_session_set_ticket_flags(
Jerry Yu95648b02023-12-06 15:03:34 +08003112 session, MBEDTLS_SSL_TLS1_3_TICKET_ALLOW_EARLY_DATA);
3113 }
3114#endif /* MBEDTLS_SSL_EARLY_DATA */
3115
Pengyu Lv4938a562023-01-16 11:28:49 +08003116 MBEDTLS_SSL_PRINT_TICKET_FLAGS(4, session->ticket_flags);
Pengyu Lv9f926952022-11-17 15:22:33 +08003117
Jerry Yue67bef42022-07-07 07:29:42 +00003118 /* Generate ticket_age_add */
Gilles Peskine449bd832023-01-11 14:50:10 +01003119 if ((ret = ssl->conf->f_rng(ssl->conf->p_rng,
3120 (unsigned char *) &session->ticket_age_add,
3121 sizeof(session->ticket_age_add)) != 0)) {
3122 MBEDTLS_SSL_DEBUG_RET(1, "generate_ticket_age_add", ret);
3123 return ret;
Jerry Yue67bef42022-07-07 07:29:42 +00003124 }
Gilles Peskine449bd832023-01-11 14:50:10 +01003125 MBEDTLS_SSL_DEBUG_MSG(3, ("ticket_age_add: %u",
3126 (unsigned int) session->ticket_age_add));
Jerry Yue67bef42022-07-07 07:29:42 +00003127
3128 /* Generate ticket_nonce */
Gilles Peskine449bd832023-01-11 14:50:10 +01003129 ret = ssl->conf->f_rng(ssl->conf->p_rng, ticket_nonce, ticket_nonce_size);
3130 if (ret != 0) {
3131 MBEDTLS_SSL_DEBUG_RET(1, "generate_ticket_nonce", ret);
3132 return ret;
Jerry Yue67bef42022-07-07 07:29:42 +00003133 }
Gilles Peskine449bd832023-01-11 14:50:10 +01003134 MBEDTLS_SSL_DEBUG_BUF(3, "ticket_nonce:",
3135 ticket_nonce, ticket_nonce_size);
Jerry Yue67bef42022-07-07 07:29:42 +00003136
3137 ciphersuite_info =
Gilles Peskine449bd832023-01-11 14:50:10 +01003138 (mbedtls_ssl_ciphersuite_t *) ssl->handshake->ciphersuite_info;
Dave Rodgman2eab4622023-10-05 13:30:37 +01003139 psa_hash_alg = mbedtls_md_psa_alg_from_type((mbedtls_md_type_t) ciphersuite_info->mac);
Gilles Peskine449bd832023-01-11 14:50:10 +01003140 hash_length = PSA_HASH_LENGTH(psa_hash_alg);
3141 if (hash_length == -1 ||
3142 (size_t) hash_length > sizeof(session->resumption_key)) {
3143 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Jerry Yufca4d572022-07-21 10:37:48 +08003144 }
3145
Jerry Yue67bef42022-07-07 07:29:42 +00003146 /* In this code the psk key length equals the length of the hash */
3147 session->resumption_key_len = hash_length;
3148 session->ciphersuite = ciphersuite_info->id;
3149
3150 /* Compute resumption key
3151 *
3152 * HKDF-Expand-Label( resumption_master_secret,
3153 * "resumption", ticket_nonce, Hash.length )
3154 */
3155 ret = mbedtls_ssl_tls13_hkdf_expand_label(
Gilles Peskine449bd832023-01-11 14:50:10 +01003156 psa_hash_alg,
3157 session->app_secrets.resumption_master_secret,
3158 hash_length,
3159 MBEDTLS_SSL_TLS1_3_LBL_WITH_LEN(resumption),
3160 ticket_nonce,
3161 ticket_nonce_size,
3162 session->resumption_key,
3163 hash_length);
Jerry Yue67bef42022-07-07 07:29:42 +00003164
Gilles Peskine449bd832023-01-11 14:50:10 +01003165 if (ret != 0) {
3166 MBEDTLS_SSL_DEBUG_RET(2,
3167 "Creating the ticket-resumed PSK failed",
3168 ret);
3169 return ret;
Jerry Yue67bef42022-07-07 07:29:42 +00003170 }
Gilles Peskine449bd832023-01-11 14:50:10 +01003171 MBEDTLS_SSL_DEBUG_BUF(3, "Ticket-resumed PSK",
3172 session->resumption_key,
3173 session->resumption_key_len);
Jerry Yue67bef42022-07-07 07:29:42 +00003174
Gilles Peskine449bd832023-01-11 14:50:10 +01003175 MBEDTLS_SSL_DEBUG_BUF(3, "resumption_master_secret",
3176 session->app_secrets.resumption_master_secret,
3177 hash_length);
Jerry Yue67bef42022-07-07 07:29:42 +00003178
Gilles Peskine449bd832023-01-11 14:50:10 +01003179 return 0;
Jerry Yue67bef42022-07-07 07:29:42 +00003180}
3181
3182/* This function creates a NewSessionTicket message in the following format:
3183 *
3184 * struct {
3185 * uint32 ticket_lifetime;
3186 * uint32 ticket_age_add;
3187 * opaque ticket_nonce<0..255>;
3188 * opaque ticket<1..2^16-1>;
3189 * Extension extensions<0..2^16-2>;
3190 * } NewSessionTicket;
3191 *
3192 * The ticket inside the NewSessionTicket message is an encrypted container
3193 * carrying the necessary information so that the server is later able to
3194 * re-start the communication.
3195 *
3196 * The following fields are placed inside the ticket by the
3197 * f_ticket_write() function:
3198 *
Jerry Yu0069abc2023-11-23 21:07:28 +08003199 * - creation time (ticket_creation_time)
3200 * - flags (ticket_flags)
Jerry Yue67bef42022-07-07 07:29:42 +00003201 * - age add (ticket_age_add)
Jerry Yu0069abc2023-11-23 21:07:28 +08003202 * - key (resumption_key)
3203 * - key length (resumption_key_len)
Jerry Yue67bef42022-07-07 07:29:42 +00003204 * - ciphersuite (ciphersuite)
Jerry Yu0069abc2023-11-23 21:07:28 +08003205 * - max_early_data_size (max_early_data_size)
Jerry Yue67bef42022-07-07 07:29:42 +00003206 */
3207MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01003208static int ssl_tls13_write_new_session_ticket_body(mbedtls_ssl_context *ssl,
3209 unsigned char *buf,
3210 unsigned char *end,
3211 size_t *out_len,
3212 unsigned char *ticket_nonce,
3213 size_t ticket_nonce_size)
Jerry Yue67bef42022-07-07 07:29:42 +00003214{
3215 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
3216 unsigned char *p = buf;
3217 mbedtls_ssl_session *session = ssl->session;
3218 size_t ticket_len;
3219 uint32_t ticket_lifetime;
Jerry Yu01da35e2022-12-12 15:09:22 +08003220 unsigned char *p_extensions_len;
Jerry Yue67bef42022-07-07 07:29:42 +00003221
3222 *out_len = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01003223 MBEDTLS_SSL_DEBUG_MSG(2, ("=> write NewSessionTicket msg"));
Jerry Yue67bef42022-07-07 07:29:42 +00003224
3225 /*
3226 * ticket_lifetime 4 bytes
3227 * ticket_age_add 4 bytes
3228 * ticket_nonce 1 + ticket_nonce_size bytes
3229 * ticket >=2 bytes
3230 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003231 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 4 + 4 + 1 + ticket_nonce_size + 2);
Jerry Yue67bef42022-07-07 07:29:42 +00003232
3233 /* Generate ticket and ticket_lifetime */
Ronald Cron3c0072b2023-11-22 10:00:14 +01003234#if defined(MBEDTLS_HAVE_TIME)
3235 session->ticket_creation_time = mbedtls_ms_time();
3236#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01003237 ret = ssl->conf->f_ticket_write(ssl->conf->p_ticket,
3238 session,
3239 p + 9 + ticket_nonce_size + 2,
3240 end,
3241 &ticket_len,
3242 &ticket_lifetime);
3243 if (ret != 0) {
3244 MBEDTLS_SSL_DEBUG_RET(1, "write_ticket", ret);
3245 return ret;
Jerry Yue67bef42022-07-07 07:29:42 +00003246 }
3247 /* RFC 8446 4.6.1
3248 * ticket_lifetime: Indicates the lifetime in seconds as a 32-bit
3249 * unsigned integer in network byte order from the time of ticket
3250 * issuance. Servers MUST NOT use any value greater than
3251 * 604800 seconds (7 days). The value of zero indicates that the
3252 * ticket should be discarded immediately. Clients MUST NOT cache
3253 * tickets for longer than 7 days, regardless of the ticket_lifetime,
3254 * and MAY delete tickets earlier based on local policy. A server
3255 * MAY treat a ticket as valid for a shorter period of time than what
3256 * is stated in the ticket_lifetime.
3257 */
Jerry Yu8e0174a2023-11-10 13:58:16 +08003258 if (ticket_lifetime > MBEDTLS_SSL_TLS1_3_MAX_ALLOWED_TICKET_LIFETIME) {
3259 ticket_lifetime = MBEDTLS_SSL_TLS1_3_MAX_ALLOWED_TICKET_LIFETIME;
Gilles Peskine449bd832023-01-11 14:50:10 +01003260 }
3261 MBEDTLS_PUT_UINT32_BE(ticket_lifetime, p, 0);
3262 MBEDTLS_SSL_DEBUG_MSG(3, ("ticket_lifetime: %u",
3263 (unsigned int) ticket_lifetime));
Jerry Yue67bef42022-07-07 07:29:42 +00003264
3265 /* Write ticket_age_add */
Gilles Peskine449bd832023-01-11 14:50:10 +01003266 MBEDTLS_PUT_UINT32_BE(session->ticket_age_add, p, 4);
3267 MBEDTLS_SSL_DEBUG_MSG(3, ("ticket_age_add: %u",
3268 (unsigned int) session->ticket_age_add));
Jerry Yue67bef42022-07-07 07:29:42 +00003269
3270 /* Write ticket_nonce */
Gilles Peskine449bd832023-01-11 14:50:10 +01003271 p[8] = (unsigned char) ticket_nonce_size;
3272 if (ticket_nonce_size > 0) {
3273 memcpy(p + 9, ticket_nonce, ticket_nonce_size);
Jerry Yue67bef42022-07-07 07:29:42 +00003274 }
3275 p += 9 + ticket_nonce_size;
3276
3277 /* Write ticket */
Gilles Peskine449bd832023-01-11 14:50:10 +01003278 MBEDTLS_PUT_UINT16_BE(ticket_len, p, 0);
Jerry Yue67bef42022-07-07 07:29:42 +00003279 p += 2;
Gilles Peskine449bd832023-01-11 14:50:10 +01003280 MBEDTLS_SSL_DEBUG_BUF(4, "ticket", p, ticket_len);
Jerry Yue67bef42022-07-07 07:29:42 +00003281 p += ticket_len;
3282
3283 /* Ticket Extensions
3284 *
Jerry Yu01da35e2022-12-12 15:09:22 +08003285 * Extension extensions<0..2^16-2>;
Jerry Yue67bef42022-07-07 07:29:42 +00003286 */
Jerry Yuedab6372022-10-31 14:37:31 +08003287 ssl->handshake->sent_extensions = MBEDTLS_SSL_EXT_MASK_NONE;
3288
Gilles Peskine449bd832023-01-11 14:50:10 +01003289 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 2);
Jerry Yu01da35e2022-12-12 15:09:22 +08003290 p_extensions_len = p;
Jerry Yue67bef42022-07-07 07:29:42 +00003291 p += 2;
3292
Jerry Yu01da35e2022-12-12 15:09:22 +08003293#if defined(MBEDTLS_SSL_EARLY_DATA)
Pengyu Lv94a42cc2023-12-06 10:04:17 +08003294 if (mbedtls_ssl_tls13_session_ticket_allow_early_data(session)) {
Jerry Yu95648b02023-12-06 15:03:34 +08003295 size_t output_len;
3296
Jerry Yuf135bac2023-11-23 18:10:51 +08003297 if ((ret = mbedtls_ssl_tls13_write_early_data_ext(
Jerry Yuc59c5862023-12-05 10:40:49 +08003298 ssl, 1, p, end, &output_len)) != 0) {
Jerry Yuf135bac2023-11-23 18:10:51 +08003299 MBEDTLS_SSL_DEBUG_RET(
3300 1, "mbedtls_ssl_tls13_write_early_data_ext", ret);
3301 return ret;
3302 }
3303 p += output_len;
Jerry Yu9e7f9bc2023-11-27 16:52:07 +08003304 } else {
3305 MBEDTLS_SSL_DEBUG_MSG(
3306 4, ("early_data not allowed, "
3307 "skip early_data extension in NewSessionTicket"));
Jerry Yu01da35e2022-12-12 15:09:22 +08003308 }
Jerry Yuf135bac2023-11-23 18:10:51 +08003309
Jerry Yu01da35e2022-12-12 15:09:22 +08003310#endif /* MBEDTLS_SSL_EARLY_DATA */
3311
3312 MBEDTLS_PUT_UINT16_BE(p - p_extensions_len - 2, p_extensions_len, 0);
3313
Jerry Yue67bef42022-07-07 07:29:42 +00003314 *out_len = p - buf;
Gilles Peskine449bd832023-01-11 14:50:10 +01003315 MBEDTLS_SSL_DEBUG_BUF(4, "ticket", buf, *out_len);
3316 MBEDTLS_SSL_DEBUG_MSG(2, ("<= write new session ticket"));
Jerry Yue67bef42022-07-07 07:29:42 +00003317
Jerry Yu7de2ff02022-11-08 21:43:46 +08003318 MBEDTLS_SSL_PRINT_EXTS(
Gilles Peskine449bd832023-01-11 14:50:10 +01003319 3, MBEDTLS_SSL_HS_NEW_SESSION_TICKET, ssl->handshake->sent_extensions);
Jerry Yu4b8f2f72022-10-31 13:31:22 +08003320
Gilles Peskine449bd832023-01-11 14:50:10 +01003321 return 0;
Jerry Yue67bef42022-07-07 07:29:42 +00003322}
3323
3324/*
Jerry Yua8d3c502022-10-30 14:51:23 +08003325 * Handler for MBEDTLS_SSL_TLS1_3_NEW_SESSION_TICKET
Jerry Yue67bef42022-07-07 07:29:42 +00003326 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003327static int ssl_tls13_write_new_session_ticket(mbedtls_ssl_context *ssl)
Jerry Yue67bef42022-07-07 07:29:42 +00003328{
3329 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
3330
Gilles Peskine449bd832023-01-11 14:50:10 +01003331 MBEDTLS_SSL_PROC_CHK_NEG(ssl_tls13_write_new_session_ticket_coordinate(ssl));
Jerry Yue67bef42022-07-07 07:29:42 +00003332
Gilles Peskine449bd832023-01-11 14:50:10 +01003333 if (ret == SSL_NEW_SESSION_TICKET_WRITE) {
Jerry Yue67bef42022-07-07 07:29:42 +00003334 unsigned char ticket_nonce[MBEDTLS_SSL_TLS1_3_TICKET_NONCE_LENGTH];
3335 unsigned char *buf;
3336 size_t buf_len, msg_len;
3337
Gilles Peskine449bd832023-01-11 14:50:10 +01003338 MBEDTLS_SSL_PROC_CHK(ssl_tls13_prepare_new_session_ticket(
3339 ssl, ticket_nonce, sizeof(ticket_nonce)));
Jerry Yue67bef42022-07-07 07:29:42 +00003340
Xiaokang Qian9f1747b2023-03-30 02:50:04 +00003341 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_start_handshake_msg(
3342 ssl, MBEDTLS_SSL_HS_NEW_SESSION_TICKET,
3343 &buf, &buf_len));
Jerry Yue67bef42022-07-07 07:29:42 +00003344
Gilles Peskine449bd832023-01-11 14:50:10 +01003345 MBEDTLS_SSL_PROC_CHK(ssl_tls13_write_new_session_ticket_body(
3346 ssl, buf, buf + buf_len, &msg_len,
3347 ticket_nonce, sizeof(ticket_nonce)));
Jerry Yue67bef42022-07-07 07:29:42 +00003348
Gilles Peskine449bd832023-01-11 14:50:10 +01003349 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_finish_handshake_msg(
3350 ssl, buf_len, msg_len));
Jerry Yufca4d572022-07-21 10:37:48 +08003351
Jerry Yu359e65f2022-09-22 23:47:43 +08003352 /* Limit session tickets count to one when resumption connection.
3353 *
3354 * See document of mbedtls_ssl_conf_new_session_tickets.
3355 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003356 if (ssl->handshake->resume == 1) {
Jerry Yu359e65f2022-09-22 23:47:43 +08003357 ssl->handshake->new_session_tickets_count = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01003358 } else {
Jerry Yu359e65f2022-09-22 23:47:43 +08003359 ssl->handshake->new_session_tickets_count--;
Gilles Peskine449bd832023-01-11 14:50:10 +01003360 }
Jerry Yub7e3fa72022-09-22 11:07:18 +08003361
Jerry Yua8d3c502022-10-30 14:51:23 +08003362 mbedtls_ssl_handshake_set_state(
Gilles Peskine449bd832023-01-11 14:50:10 +01003363 ssl, MBEDTLS_SSL_TLS1_3_NEW_SESSION_TICKET_FLUSH);
3364 } else {
3365 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_HANDSHAKE_OVER);
Jerry Yue67bef42022-07-07 07:29:42 +00003366 }
3367
Jerry Yue67bef42022-07-07 07:29:42 +00003368cleanup:
3369
Gilles Peskine449bd832023-01-11 14:50:10 +01003370 return ret;
Jerry Yue67bef42022-07-07 07:29:42 +00003371}
3372#endif /* MBEDTLS_SSL_SESSION_TICKETS */
3373
3374/*
XiaokangQiane8ff3502022-04-22 02:34:40 +00003375 * TLS 1.3 State Machine -- server side
XiaokangQian7807f9f2022-02-15 10:04:37 +00003376 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003377int mbedtls_ssl_tls13_handshake_server_step(mbedtls_ssl_context *ssl)
Jerry Yub9930e72021-08-06 17:11:51 +08003378{
XiaokangQian08037552022-04-20 07:16:41 +00003379 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
XiaokangQian7807f9f2022-02-15 10:04:37 +00003380
Gilles Peskine449bd832023-01-11 14:50:10 +01003381 if (ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER || ssl->handshake == NULL) {
3382 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
3383 }
XiaokangQian7807f9f2022-02-15 10:04:37 +00003384
Gilles Peskine449bd832023-01-11 14:50:10 +01003385 MBEDTLS_SSL_DEBUG_MSG(2, ("tls13 server state: %s(%d)",
Dave Rodgman2eab4622023-10-05 13:30:37 +01003386 mbedtls_ssl_states_str((mbedtls_ssl_states) ssl->state),
Gilles Peskine449bd832023-01-11 14:50:10 +01003387 ssl->state));
Jerry Yu6e81b272021-09-27 11:16:17 +08003388
Gilles Peskine449bd832023-01-11 14:50:10 +01003389 switch (ssl->state) {
XiaokangQian7807f9f2022-02-15 10:04:37 +00003390 /* start state */
3391 case MBEDTLS_SSL_HELLO_REQUEST:
Gilles Peskine449bd832023-01-11 14:50:10 +01003392 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_CLIENT_HELLO);
XiaokangQian08037552022-04-20 07:16:41 +00003393 ret = 0;
XiaokangQian7807f9f2022-02-15 10:04:37 +00003394 break;
3395
XiaokangQian7807f9f2022-02-15 10:04:37 +00003396 case MBEDTLS_SSL_CLIENT_HELLO:
Gilles Peskine449bd832023-01-11 14:50:10 +01003397 ret = ssl_tls13_process_client_hello(ssl);
3398 if (ret != 0) {
3399 MBEDTLS_SSL_DEBUG_RET(1, "ssl_tls13_process_client_hello", ret);
3400 }
Jerry Yuf41553b2022-05-09 22:20:30 +08003401 break;
XiaokangQian7807f9f2022-02-15 10:04:37 +00003402
Jerry Yuf41553b2022-05-09 22:20:30 +08003403 case MBEDTLS_SSL_HELLO_RETRY_REQUEST:
Gilles Peskine449bd832023-01-11 14:50:10 +01003404 ret = ssl_tls13_write_hello_retry_request(ssl);
3405 if (ret != 0) {
3406 MBEDTLS_SSL_DEBUG_RET(1, "ssl_tls13_write_hello_retry_request", ret);
3407 return ret;
Jerry Yuf41553b2022-05-09 22:20:30 +08003408 }
XiaokangQian7807f9f2022-02-15 10:04:37 +00003409 break;
3410
Jerry Yu5b64ae92022-03-30 17:15:02 +08003411 case MBEDTLS_SSL_SERVER_HELLO:
Gilles Peskine449bd832023-01-11 14:50:10 +01003412 ret = ssl_tls13_write_server_hello(ssl);
Jerry Yu5b64ae92022-03-30 17:15:02 +08003413 break;
3414
Xiaofei Baicba64af2022-02-15 10:00:56 +00003415 case MBEDTLS_SSL_ENCRYPTED_EXTENSIONS:
Gilles Peskine449bd832023-01-11 14:50:10 +01003416 ret = ssl_tls13_write_encrypted_extensions(ssl);
3417 if (ret != 0) {
3418 MBEDTLS_SSL_DEBUG_RET(1, "ssl_tls13_write_encrypted_extensions", ret);
3419 return ret;
Xiaofei Baicba64af2022-02-15 10:00:56 +00003420 }
Jerry Yu48330562022-05-06 21:35:44 +08003421 break;
Jerry Yu6a2cd9e2022-05-05 11:14:19 +08003422
Ronald Cron928cbd32022-10-04 16:14:26 +02003423#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED)
Xiaofei Bai9ca09d42022-02-14 12:57:18 +00003424 case MBEDTLS_SSL_CERTIFICATE_REQUEST:
Gilles Peskine449bd832023-01-11 14:50:10 +01003425 ret = ssl_tls13_write_certificate_request(ssl);
Xiaofei Bai9ca09d42022-02-14 12:57:18 +00003426 break;
Xiaofei Bai9ca09d42022-02-14 12:57:18 +00003427
Jerry Yu83da34e2022-04-16 13:59:52 +08003428 case MBEDTLS_SSL_SERVER_CERTIFICATE:
Gilles Peskine449bd832023-01-11 14:50:10 +01003429 ret = ssl_tls13_write_server_certificate(ssl);
Jerry Yu83da34e2022-04-16 13:59:52 +08003430 break;
3431
3432 case MBEDTLS_SSL_CERTIFICATE_VERIFY:
Gilles Peskine449bd832023-01-11 14:50:10 +01003433 ret = ssl_tls13_write_certificate_verify(ssl);
Jerry Yu83da34e2022-04-16 13:59:52 +08003434 break;
Ronald Cron928cbd32022-10-04 16:14:26 +02003435#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED */
Jerry Yu83da34e2022-04-16 13:59:52 +08003436
Gilles Peskine449bd832023-01-11 14:50:10 +01003437 /*
3438 * Injection of dummy-CCS's for middlebox compatibility
3439 */
Gabor Mezei7b39bf12022-05-24 16:04:14 +02003440#if defined(MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE)
Gabor Mezeif7044ea2022-06-28 16:01:49 +02003441 case MBEDTLS_SSL_SERVER_CCS_AFTER_HELLO_RETRY_REQUEST:
Gilles Peskine449bd832023-01-11 14:50:10 +01003442 ret = mbedtls_ssl_tls13_write_change_cipher_spec(ssl);
3443 if (ret == 0) {
3444 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_CLIENT_HELLO);
3445 }
Gabor Mezei7b39bf12022-05-24 16:04:14 +02003446 break;
3447
3448 case MBEDTLS_SSL_SERVER_CCS_AFTER_SERVER_HELLO:
Ronald Crone273f722024-02-13 18:22:26 +01003449 ret = mbedtls_ssl_tls13_write_change_cipher_spec(ssl);
3450 if (ret != 0) {
3451 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01003452 }
Ronald Cronfe59ff72024-01-24 14:31:50 +01003453 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_ENCRYPTED_EXTENSIONS);
Gabor Mezei7b39bf12022-05-24 16:04:14 +02003454 break;
3455#endif /* MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE */
3456
Jerry Yu69dd8d42022-04-16 12:51:26 +08003457 case MBEDTLS_SSL_SERVER_FINISHED:
Gilles Peskine449bd832023-01-11 14:50:10 +01003458 ret = ssl_tls13_write_server_finished(ssl);
Jerry Yu69dd8d42022-04-16 12:51:26 +08003459 break;
3460
Jerry Yu87b5ed42022-12-12 13:07:07 +08003461#if defined(MBEDTLS_SSL_EARLY_DATA)
3462 case MBEDTLS_SSL_END_OF_EARLY_DATA:
Jerry Yu59d420f2023-12-01 16:30:34 +08003463 ret = ssl_tls13_process_end_of_early_data(ssl);
Jerry Yu87b5ed42022-12-12 13:07:07 +08003464 break;
3465#endif /* MBEDTLS_SSL_EARLY_DATA */
3466
Jerry Yu69dd8d42022-04-16 12:51:26 +08003467 case MBEDTLS_SSL_CLIENT_FINISHED:
Gilles Peskine449bd832023-01-11 14:50:10 +01003468 ret = ssl_tls13_process_client_finished(ssl);
Jerry Yu69dd8d42022-04-16 12:51:26 +08003469 break;
3470
Jerry Yu69dd8d42022-04-16 12:51:26 +08003471 case MBEDTLS_SSL_HANDSHAKE_WRAPUP:
Gilles Peskine449bd832023-01-11 14:50:10 +01003472 ret = ssl_tls13_handshake_wrapup(ssl);
Jerry Yu69dd8d42022-04-16 12:51:26 +08003473 break;
3474
Ronald Cron766c0cd2022-10-18 12:17:11 +02003475#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED)
XiaokangQian6b916b12022-04-25 07:29:34 +00003476 case MBEDTLS_SSL_CLIENT_CERTIFICATE:
Gilles Peskine449bd832023-01-11 14:50:10 +01003477 ret = mbedtls_ssl_tls13_process_certificate(ssl);
3478 if (ret == 0) {
3479 if (ssl->session_negotiate->peer_cert != NULL) {
Ronald Cron209cae92022-06-07 10:30:19 +02003480 mbedtls_ssl_handshake_set_state(
Gilles Peskine449bd832023-01-11 14:50:10 +01003481 ssl, MBEDTLS_SSL_CLIENT_CERTIFICATE_VERIFY);
3482 } else {
3483 MBEDTLS_SSL_DEBUG_MSG(2, ("skip parse certificate verify"));
Ronald Cron209cae92022-06-07 10:30:19 +02003484 mbedtls_ssl_handshake_set_state(
Gilles Peskine449bd832023-01-11 14:50:10 +01003485 ssl, MBEDTLS_SSL_CLIENT_FINISHED);
Ronald Cron19385882022-06-15 16:26:13 +02003486 }
XiaokangQian6b916b12022-04-25 07:29:34 +00003487 }
3488 break;
3489
3490 case MBEDTLS_SSL_CLIENT_CERTIFICATE_VERIFY:
Gilles Peskine449bd832023-01-11 14:50:10 +01003491 ret = mbedtls_ssl_tls13_process_certificate_verify(ssl);
3492 if (ret == 0) {
XiaokangQian6b916b12022-04-25 07:29:34 +00003493 mbedtls_ssl_handshake_set_state(
Gilles Peskine449bd832023-01-11 14:50:10 +01003494 ssl, MBEDTLS_SSL_CLIENT_FINISHED);
XiaokangQian6b916b12022-04-25 07:29:34 +00003495 }
3496 break;
Ronald Cron766c0cd2022-10-18 12:17:11 +02003497#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED */
XiaokangQian6b916b12022-04-25 07:29:34 +00003498
Jerry Yue67bef42022-07-07 07:29:42 +00003499#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Jerry Yua8d3c502022-10-30 14:51:23 +08003500 case MBEDTLS_SSL_TLS1_3_NEW_SESSION_TICKET:
Gilles Peskine449bd832023-01-11 14:50:10 +01003501 ret = ssl_tls13_write_new_session_ticket(ssl);
3502 if (ret != 0) {
3503 MBEDTLS_SSL_DEBUG_RET(1,
3504 "ssl_tls13_write_new_session_ticket ",
3505 ret);
Jerry Yue67bef42022-07-07 07:29:42 +00003506 }
3507 break;
Jerry Yua8d3c502022-10-30 14:51:23 +08003508 case MBEDTLS_SSL_TLS1_3_NEW_SESSION_TICKET_FLUSH:
Jerry Yue67bef42022-07-07 07:29:42 +00003509 /* This state is necessary to do the flush of the New Session
Jerry Yua8d3c502022-10-30 14:51:23 +08003510 * Ticket message written in MBEDTLS_SSL_TLS1_3_NEW_SESSION_TICKET
Jerry Yue67bef42022-07-07 07:29:42 +00003511 * as part of ssl_prepare_handshake_step.
3512 */
3513 ret = 0;
Jerry Yud4e75002022-08-09 13:33:50 +08003514
Gilles Peskine449bd832023-01-11 14:50:10 +01003515 if (ssl->handshake->new_session_tickets_count == 0) {
3516 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_HANDSHAKE_OVER);
3517 } else {
Xiaokang Qian9f1747b2023-03-30 02:50:04 +00003518 mbedtls_ssl_handshake_set_state(
3519 ssl, MBEDTLS_SSL_TLS1_3_NEW_SESSION_TICKET);
Gilles Peskine449bd832023-01-11 14:50:10 +01003520 }
Jerry Yue67bef42022-07-07 07:29:42 +00003521 break;
3522
3523#endif /* MBEDTLS_SSL_SESSION_TICKETS */
3524
XiaokangQian7807f9f2022-02-15 10:04:37 +00003525 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01003526 MBEDTLS_SSL_DEBUG_MSG(1, ("invalid state %d", ssl->state));
3527 return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
XiaokangQian7807f9f2022-02-15 10:04:37 +00003528 }
3529
Gilles Peskine449bd832023-01-11 14:50:10 +01003530 return ret;
Jerry Yub9930e72021-08-06 17:11:51 +08003531}
Jerry Yu3cc4c2a2021-08-06 16:29:08 +08003532
Jerry Yufb4b6472022-01-27 15:03:26 +08003533#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_PROTO_TLS1_3 */