blob: a496981828426f3463c9ad2cf5ac2ffe003faca2 [file] [log] [blame]
Jerry Yu3cc4c2a2021-08-06 16:29:08 +08001/*
Jerry Yub9930e72021-08-06 17:11:51 +08002 * TLS 1.3 server-side functions
Jerry Yu3cc4c2a2021-08-06 16:29:08 +08003 *
4 * Copyright The Mbed TLS Contributors
Dave Rodgman16799db2023-11-02 19:47:20 +00005 * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
Gilles Peskine449bd832023-01-11 14:50:10 +01006 */
Jerry Yu3cc4c2a2021-08-06 16:29:08 +08007
8#include "common.h"
9
Jerry Yufb4b6472022-01-27 15:03:26 +080010#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_PROTO_TLS1_3)
Jerry Yu3cc4c2a2021-08-06 16:29:08 +080011
Valerio Settib4f50762024-01-17 10:24:52 +010012#include "debug_internal.h"
Xiaofei Baicba64af2022-02-15 10:00:56 +000013#include "mbedtls/error.h"
14#include "mbedtls/platform.h"
Jerry Yu1c105562022-07-10 06:32:38 +000015#include "mbedtls/constant_time.h"
Manuel Pégourié-Gonnardd55d66f2023-06-20 10:14:58 +020016#include "mbedtls/oid.h"
Valerio Setti384fbde2024-01-02 13:26:40 +010017#include "mbedtls/psa_util.h"
Jerry Yu3bf2c642022-03-30 22:02:12 +080018
Xiaofei Bai9ca09d42022-02-14 12:57:18 +000019#include "ssl_misc.h"
20#include "ssl_tls13_keys.h"
21#include "ssl_debug_helpers.h"
22
Jerry Yuf35ba382022-08-23 17:58:26 +080023
Jerry Yuc5a23a02022-08-25 10:51:44 +080024static const mbedtls_ssl_ciphersuite_t *ssl_tls13_validate_peer_ciphersuite(
Gilles Peskine449bd832023-01-11 14:50:10 +010025 mbedtls_ssl_context *ssl,
26 unsigned int cipher_suite)
Jerry Yuf35ba382022-08-23 17:58:26 +080027{
28 const mbedtls_ssl_ciphersuite_t *ciphersuite_info;
Gilles Peskine449bd832023-01-11 14:50:10 +010029 if (!mbedtls_ssl_tls13_cipher_suite_is_offered(ssl, cipher_suite)) {
30 return NULL;
Jerry Yuf35ba382022-08-23 17:58:26 +080031 }
Gilles Peskine449bd832023-01-11 14:50:10 +010032
33 ciphersuite_info = mbedtls_ssl_ciphersuite_from_id(cipher_suite);
34 if ((mbedtls_ssl_validate_ciphersuite(ssl, ciphersuite_info,
35 ssl->tls_version,
36 ssl->tls_version) != 0)) {
37 return NULL;
38 }
39 return ciphersuite_info;
Jerry Yuf35ba382022-08-23 17:58:26 +080040}
41
Ronald Cron89089cc2024-02-14 18:18:08 +010042static void ssl_tls13_select_ciphersuite(
43 mbedtls_ssl_context *ssl,
44 const unsigned char *cipher_suites,
45 const unsigned char *cipher_suites_end,
46 int psk_ciphersuite_id,
47 psa_algorithm_t psk_hash_alg,
48 const mbedtls_ssl_ciphersuite_t **selected_ciphersuite_info)
49{
50 *selected_ciphersuite_info = NULL;
51
52 /*
53 * This function assumes that the length of the list of ciphersuites is
54 * even and it should have been checked it is so in the main ClientHello
55 * parsing function. Double check here.
56 */
57 if ((cipher_suites_end - cipher_suites) & 1) {
58 return;
59 }
60
61 for (const unsigned char *p = cipher_suites;
62 p < cipher_suites_end; p += 2) {
63 /*
64 * "cipher_suites_end - p is even" is an invariant of the loop. As
65 * cipher_suites_end - p > 0, we have cipher_suites_end - p >= 2 and it
66 * is thus safe to read two bytes.
67 */
68 uint16_t id = MBEDTLS_GET_UINT16_BE(p, 0);
69
70 const mbedtls_ssl_ciphersuite_t *info =
71 ssl_tls13_validate_peer_ciphersuite(ssl, id);
72 if (info == NULL) {
73 continue;
74 }
75
76 /*
77 * If a valid PSK ciphersuite identifier has been passed in, we seek
78 * for an exact match.
79 */
80 if (psk_ciphersuite_id != 0) {
81 if (id != psk_ciphersuite_id) {
82 continue;
83 }
84 } else if (psk_hash_alg != PSA_ALG_NONE) {
85 if (mbedtls_md_psa_alg_from_type((mbedtls_md_type_t) info->mac) !=
86 psk_hash_alg) {
87 continue;
88 }
89 }
90
91 *selected_ciphersuite_info = info;
92 return;
93 }
94
95 MBEDTLS_SSL_DEBUG_MSG(2, ("No matched ciphersuite"));
96}
97
Ronald Cron41a443a2022-10-04 16:38:25 +020098#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED)
Jerry Yue19e3b92022-07-08 12:04:51 +000099/* From RFC 8446:
100 *
101 * enum { psk_ke(0), psk_dhe_ke(1), (255) } PskKeyExchangeMode;
102 * struct {
103 * PskKeyExchangeMode ke_modes<1..255>;
104 * } PskKeyExchangeModes;
105 */
Jerry Yu6e74a7e2022-07-20 20:49:32 +0800106MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100107static int ssl_tls13_parse_key_exchange_modes_ext(mbedtls_ssl_context *ssl,
108 const unsigned char *buf,
109 const unsigned char *end)
Jerry Yue19e3b92022-07-08 12:04:51 +0000110{
Jerry Yu299e31f2022-07-13 23:06:36 +0800111 const unsigned char *p = buf;
Jerry Yue19e3b92022-07-08 12:04:51 +0000112 size_t ke_modes_len;
113 int ke_modes = 0;
114
Jerry Yu854dd9e2022-07-15 14:28:27 +0800115 /* Read ke_modes length (1 Byte) */
Gilles Peskine449bd832023-01-11 14:50:10 +0100116 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, 1);
Jerry Yu299e31f2022-07-13 23:06:36 +0800117 ke_modes_len = *p++;
Jerry Yue19e3b92022-07-08 12:04:51 +0000118 /* Currently, there are only two PSK modes, so even without looking
119 * at the content, something's wrong if the list has more than 2 items. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100120 if (ke_modes_len > 2) {
121 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER,
122 MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER);
123 return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
Jerry Yu299e31f2022-07-13 23:06:36 +0800124 }
Jerry Yue19e3b92022-07-08 12:04:51 +0000125
Gilles Peskine449bd832023-01-11 14:50:10 +0100126 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, ke_modes_len);
Jerry Yue19e3b92022-07-08 12:04:51 +0000127
Gilles Peskine449bd832023-01-11 14:50:10 +0100128 while (ke_modes_len-- != 0) {
129 switch (*p++) {
130 case MBEDTLS_SSL_TLS1_3_PSK_MODE_PURE:
131 ke_modes |= MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK;
132 MBEDTLS_SSL_DEBUG_MSG(3, ("Found PSK KEX MODE"));
133 break;
134 case MBEDTLS_SSL_TLS1_3_PSK_MODE_ECDHE:
135 ke_modes |= MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK_EPHEMERAL;
136 MBEDTLS_SSL_DEBUG_MSG(3, ("Found PSK_EPHEMERAL KEX MODE"));
137 break;
138 default:
139 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER,
140 MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER);
141 return MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER;
Jerry Yue19e3b92022-07-08 12:04:51 +0000142 }
143 }
144
145 ssl->handshake->tls13_kex_modes = ke_modes;
Gilles Peskine449bd832023-01-11 14:50:10 +0100146 return 0;
Jerry Yue19e3b92022-07-08 12:04:51 +0000147}
Jerry Yu1c105562022-07-10 06:32:38 +0000148
Ronald Cronfbae94a2023-12-05 18:15:14 +0100149#define SSL_TLS1_3_PSK_IDENTITY_DOES_NOT_MATCH 2
150#define SSL_TLS1_3_PSK_IDENTITY_MATCH_BUT_PSK_NOT_USABLE 1
151#define SSL_TLS1_3_PSK_IDENTITY_MATCH 0
Jerry Yu95699e72022-08-21 19:22:23 +0800152
153#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Pengyu Lv29daf4a2023-10-30 17:13:30 +0800154MBEDTLS_CHECK_RETURN_CRITICAL
Pengyu Lvbc4aab72023-12-01 15:37:24 +0800155static int ssl_tls13_key_exchange_is_psk_available(mbedtls_ssl_context *ssl);
Pengyu Lv29daf4a2023-10-30 17:13:30 +0800156MBEDTLS_CHECK_RETURN_CRITICAL
Pengyu Lvbc4aab72023-12-01 15:37:24 +0800157static int ssl_tls13_key_exchange_is_psk_ephemeral_available(mbedtls_ssl_context *ssl);
Jerry Yu95699e72022-08-21 19:22:23 +0800158
159MBEDTLS_CHECK_RETURN_CRITICAL
160static int ssl_tls13_offered_psks_check_identity_match_ticket(
Gilles Peskine449bd832023-01-11 14:50:10 +0100161 mbedtls_ssl_context *ssl,
162 const unsigned char *identity,
163 size_t identity_len,
164 uint32_t obfuscated_ticket_age,
165 mbedtls_ssl_session *session)
Jerry Yu95699e72022-08-21 19:22:23 +0800166{
Jerry Yufd310eb2022-09-06 09:16:35 +0800167 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yu95699e72022-08-21 19:22:23 +0800168 unsigned char *ticket_buffer;
Jerry Yu8d4bbba2022-09-13 14:15:48 +0800169#if defined(MBEDTLS_HAVE_TIME)
Jerry Yucebffc32022-12-15 18:00:05 +0800170 mbedtls_ms_time_t now;
171 mbedtls_ms_time_t server_age;
Jerry Yu713ce1f2023-11-17 15:32:12 +0800172 uint32_t client_age;
Jerry Yucebffc32022-12-15 18:00:05 +0800173 mbedtls_ms_time_t age_diff;
Jerry Yu8d4bbba2022-09-13 14:15:48 +0800174#endif
Jerry Yu95699e72022-08-21 19:22:23 +0800175
176 ((void) obfuscated_ticket_age);
177
Gilles Peskine449bd832023-01-11 14:50:10 +0100178 MBEDTLS_SSL_DEBUG_MSG(2, ("=> check_identity_match_ticket"));
Jerry Yu95699e72022-08-21 19:22:23 +0800179
Jerry Yufd310eb2022-09-06 09:16:35 +0800180 /* Ticket parser is not configured, Skip */
Gilles Peskine449bd832023-01-11 14:50:10 +0100181 if (ssl->conf->f_ticket_parse == NULL || identity_len == 0) {
Ronald Cronfbae94a2023-12-05 18:15:14 +0100182 return SSL_TLS1_3_PSK_IDENTITY_DOES_NOT_MATCH;
Gilles Peskine449bd832023-01-11 14:50:10 +0100183 }
Jerry Yu95699e72022-08-21 19:22:23 +0800184
Jerry Yu4746b102022-09-13 11:11:48 +0800185 /* We create a copy of the encrypted ticket since the ticket parsing
186 * function is allowed to use its input buffer as an output buffer
187 * (in-place decryption). We do, however, need the original buffer for
188 * computing the PSK binder value.
Jerry Yu95699e72022-08-21 19:22:23 +0800189 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100190 ticket_buffer = mbedtls_calloc(1, identity_len);
191 if (ticket_buffer == NULL) {
192 MBEDTLS_SSL_DEBUG_MSG(1, ("buffer too small"));
193 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
Jerry Yu95699e72022-08-21 19:22:23 +0800194 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100195 memcpy(ticket_buffer, identity, identity_len);
Jerry Yu95699e72022-08-21 19:22:23 +0800196
Ronald Cronfbae94a2023-12-05 18:15:14 +0100197 ret = ssl->conf->f_ticket_parse(ssl->conf->p_ticket,
198 session,
199 ticket_buffer, identity_len);
200 if (ret == 0) {
201 ret = SSL_TLS1_3_PSK_IDENTITY_MATCH;
202 } else {
203 if (ret == MBEDTLS_ERR_SSL_SESSION_TICKET_EXPIRED) {
Gilles Peskine449bd832023-01-11 14:50:10 +0100204 MBEDTLS_SSL_DEBUG_MSG(3, ("ticket is expired"));
Ronald Cronfbae94a2023-12-05 18:15:14 +0100205 ret = SSL_TLS1_3_PSK_IDENTITY_MATCH_BUT_PSK_NOT_USABLE;
Gilles Peskine449bd832023-01-11 14:50:10 +0100206 } else {
Ronald Cronfbae94a2023-12-05 18:15:14 +0100207 if (ret == MBEDTLS_ERR_SSL_INVALID_MAC) {
208 MBEDTLS_SSL_DEBUG_MSG(3, ("ticket is not authentic"));
209 } else {
210 MBEDTLS_SSL_DEBUG_RET(1, "ticket_parse", ret);
211 }
212 ret = SSL_TLS1_3_PSK_IDENTITY_DOES_NOT_MATCH;
Gilles Peskine449bd832023-01-11 14:50:10 +0100213 }
Jerry Yu95699e72022-08-21 19:22:23 +0800214 }
215
216 /* We delete the temporary buffer */
Gilles Peskine449bd832023-01-11 14:50:10 +0100217 mbedtls_free(ticket_buffer);
Jerry Yu95699e72022-08-21 19:22:23 +0800218
Ronald Cronfbae94a2023-12-05 18:15:14 +0100219 if (ret != SSL_TLS1_3_PSK_IDENTITY_MATCH) {
220 goto exit;
Jerry Yuce3b95e2023-10-31 16:02:04 +0800221 }
Jerry Yuce3b95e2023-10-31 16:02:04 +0800222
Ronald Cronfbae94a2023-12-05 18:15:14 +0100223 ret = SSL_TLS1_3_PSK_IDENTITY_MATCH_BUT_PSK_NOT_USABLE;
224
225 if (session->tls_version != MBEDTLS_SSL_VERSION_TLS1_3) {
226 MBEDTLS_SSL_DEBUG_MSG(3, ("Ticket TLS version is not 1.3."));
Jerry Yu8d4bbba2022-09-13 14:15:48 +0800227 goto exit;
228 }
Jerry Yu95699e72022-08-21 19:22:23 +0800229
Gilles Peskine449bd832023-01-11 14:50:10 +0100230#if defined(MBEDTLS_HAVE_TIME)
Jerry Yucebffc32022-12-15 18:00:05 +0800231 now = mbedtls_ms_time();
Gilles Peskine449bd832023-01-11 14:50:10 +0100232
Jerry Yu25ba4d42023-11-10 14:12:20 +0800233 if (now < session->ticket_creation_time) {
Gilles Peskine449bd832023-01-11 14:50:10 +0100234 MBEDTLS_SSL_DEBUG_MSG(
Jerry Yu713ce1f2023-11-17 15:32:12 +0800235 3, ("Invalid ticket creation time ( now = %" MBEDTLS_PRINTF_MS_TIME
236 ", creation_time = %" MBEDTLS_PRINTF_MS_TIME " )",
Jerry Yu25ba4d42023-11-10 14:12:20 +0800237 now, session->ticket_creation_time));
Gilles Peskine449bd832023-01-11 14:50:10 +0100238 goto exit;
239 }
240
Jerry Yu25ba4d42023-11-10 14:12:20 +0800241 server_age = now - session->ticket_creation_time;
Jerry Yu95699e72022-08-21 19:22:23 +0800242
Jerry Yu8d4bbba2022-09-13 14:15:48 +0800243 /* RFC 8446 section 4.6.1
244 *
245 * Servers MUST NOT use any value greater than 604800 seconds (7 days).
246 *
247 * RFC 8446 section 4.2.11.1
248 *
249 * Clients MUST NOT attempt to use tickets which have ages greater than
250 * the "ticket_lifetime" value which was provided with the ticket.
251 *
Jerry Yu8d4bbba2022-09-13 14:15:48 +0800252 */
Jerry Yu8e0174a2023-11-10 13:58:16 +0800253 if (server_age > MBEDTLS_SSL_TLS1_3_MAX_ALLOWED_TICKET_LIFETIME * 1000) {
Jerry Yu8d4bbba2022-09-13 14:15:48 +0800254 MBEDTLS_SSL_DEBUG_MSG(
Jerry Yud84c14f2023-11-16 13:33:57 +0800255 3, ("Ticket age exceeds limitation ticket_age = %" MBEDTLS_PRINTF_MS_TIME,
Jerry Yucebffc32022-12-15 18:00:05 +0800256 server_age));
Jerry Yu8d4bbba2022-09-13 14:15:48 +0800257 goto exit;
258 }
Jerry Yu95699e72022-08-21 19:22:23 +0800259
Jerry Yu8d4bbba2022-09-13 14:15:48 +0800260 /* RFC 8446 section 4.2.10
261 *
262 * For PSKs provisioned via NewSessionTicket, a server MUST validate that
263 * the ticket age for the selected PSK identity (computed by subtracting
264 * ticket_age_add from PskIdentity.obfuscated_ticket_age modulo 2^32) is
265 * within a small tolerance of the time since the ticket was issued.
Jerry Yuf7dad3c2022-09-14 22:31:39 +0800266 *
Jerry Yu31b601a2023-11-10 11:27:21 +0800267 * NOTE: The typical accuracy of an RTC crystal is ±100 to ±20 parts per
268 * million (360 to 72 milliseconds per hour). Default tolerance
Jerry Yu9cb953a2023-11-15 09:54:11 +0800269 * window is 6s, thus in the worst case clients and servers must
Jerry Yu31b601a2023-11-10 11:27:21 +0800270 * sync up their system time every 6000/360/2~=8 hours.
Jerry Yu8d4bbba2022-09-13 14:15:48 +0800271 */
Jerry Yucebffc32022-12-15 18:00:05 +0800272 client_age = obfuscated_ticket_age - session->ticket_age_add;
Jerry Yu60e99722023-11-20 09:55:24 +0800273 age_diff = server_age - (mbedtls_ms_time_t) client_age;
Jerry Yu28e7c552023-11-10 12:05:56 +0800274 if (age_diff < -MBEDTLS_SSL_TLS1_3_TICKET_AGE_TOLERANCE ||
Jerry Yucebffc32022-12-15 18:00:05 +0800275 age_diff > MBEDTLS_SSL_TLS1_3_TICKET_AGE_TOLERANCE) {
Jerry Yu8d4bbba2022-09-13 14:15:48 +0800276 MBEDTLS_SSL_DEBUG_MSG(
Jerry Yuf16efbc2023-10-30 11:06:24 +0800277 3, ("Ticket age outside tolerance window ( diff = %"
278 MBEDTLS_PRINTF_MS_TIME ")",
Jerry Yucebffc32022-12-15 18:00:05 +0800279 age_diff));
Jerry Yu8d4bbba2022-09-13 14:15:48 +0800280 goto exit;
281 }
Jerry Yu95699e72022-08-21 19:22:23 +0800282#endif /* MBEDTLS_HAVE_TIME */
Jerry Yu8d4bbba2022-09-13 14:15:48 +0800283
Ronald Cronfbae94a2023-12-05 18:15:14 +0100284 ret = SSL_TLS1_3_PSK_IDENTITY_MATCH;
285
Jerry Yu8d4bbba2022-09-13 14:15:48 +0800286exit:
Ronald Cronfbae94a2023-12-05 18:15:14 +0100287 if (ret != SSL_TLS1_3_PSK_IDENTITY_MATCH) {
Gilles Peskine449bd832023-01-11 14:50:10 +0100288 mbedtls_ssl_session_free(session);
289 }
Jerry Yu95699e72022-08-21 19:22:23 +0800290
Gilles Peskine449bd832023-01-11 14:50:10 +0100291 MBEDTLS_SSL_DEBUG_MSG(2, ("<= check_identity_match_ticket"));
292 return ret;
Jerry Yu95699e72022-08-21 19:22:23 +0800293}
294#endif /* MBEDTLS_SSL_SESSION_TICKETS */
295
Jerry Yu6e74a7e2022-07-20 20:49:32 +0800296MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yu1c105562022-07-10 06:32:38 +0000297static int ssl_tls13_offered_psks_check_identity_match(
Gilles Peskine449bd832023-01-11 14:50:10 +0100298 mbedtls_ssl_context *ssl,
299 const unsigned char *identity,
300 size_t identity_len,
301 uint32_t obfuscated_ticket_age,
302 int *psk_type,
303 mbedtls_ssl_session *session)
Jerry Yu1c105562022-07-10 06:32:38 +0000304{
Ronald Cron606671e2023-02-17 11:36:33 +0100305 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
306
Jerry Yu95699e72022-08-21 19:22:23 +0800307 ((void) session);
308 ((void) obfuscated_ticket_age);
Jerry Yu5725f1c2022-08-21 17:27:16 +0800309 *psk_type = MBEDTLS_SSL_TLS1_3_PSK_EXTERNAL;
Jerry Yu95699e72022-08-21 19:22:23 +0800310
Gilles Peskine449bd832023-01-11 14:50:10 +0100311 MBEDTLS_SSL_DEBUG_BUF(4, "identity", identity, identity_len);
Jerry Yu95699e72022-08-21 19:22:23 +0800312 ssl->handshake->resume = 0;
313
Jerry Yu95699e72022-08-21 19:22:23 +0800314#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Ronald Cron7a30cf52024-02-23 14:38:59 +0100315 ret = ssl_tls13_offered_psks_check_identity_match_ticket(
316 ssl, identity, identity_len, obfuscated_ticket_age, session);
317 if (ret == SSL_TLS1_3_PSK_IDENTITY_MATCH) {
Jerry Yu95699e72022-08-21 19:22:23 +0800318 ssl->handshake->resume = 1;
319 *psk_type = MBEDTLS_SSL_TLS1_3_PSK_RESUMPTION;
Ronald Cron606671e2023-02-17 11:36:33 +0100320 ret = mbedtls_ssl_set_hs_psk(ssl,
321 session->resumption_key,
322 session->resumption_key_len);
323 if (ret != 0) {
324 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_set_hs_psk", ret);
325 return ret;
326 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100327
328 MBEDTLS_SSL_DEBUG_BUF(4, "Ticket-resumed PSK:",
329 session->resumption_key,
330 session->resumption_key_len);
331 MBEDTLS_SSL_DEBUG_MSG(4, ("ticket: obfuscated_ticket_age: %u",
332 (unsigned) obfuscated_ticket_age));
Ronald Cronfbae94a2023-12-05 18:15:14 +0100333 return SSL_TLS1_3_PSK_IDENTITY_MATCH;
Ronald Cron7a30cf52024-02-23 14:38:59 +0100334 } else if (ret == SSL_TLS1_3_PSK_IDENTITY_MATCH_BUT_PSK_NOT_USABLE) {
335 return SSL_TLS1_3_PSK_IDENTITY_MATCH_BUT_PSK_NOT_USABLE;
Jerry Yu95699e72022-08-21 19:22:23 +0800336 }
337#endif /* MBEDTLS_SSL_SESSION_TICKETS */
338
Jerry Yu1c105562022-07-10 06:32:38 +0000339 /* Check identity with external configured function */
Gilles Peskine449bd832023-01-11 14:50:10 +0100340 if (ssl->conf->f_psk != NULL) {
341 if (ssl->conf->f_psk(
342 ssl->conf->p_psk, ssl, identity, identity_len) == 0) {
Ronald Cronfbae94a2023-12-05 18:15:14 +0100343 return SSL_TLS1_3_PSK_IDENTITY_MATCH;
Jerry Yu1c105562022-07-10 06:32:38 +0000344 }
Ronald Cronfbae94a2023-12-05 18:15:14 +0100345 return SSL_TLS1_3_PSK_IDENTITY_DOES_NOT_MATCH;
Jerry Yu1c105562022-07-10 06:32:38 +0000346 }
347
Gilles Peskine449bd832023-01-11 14:50:10 +0100348 MBEDTLS_SSL_DEBUG_BUF(5, "identity", identity, identity_len);
Jerry Yu1c105562022-07-10 06:32:38 +0000349 /* Check identity with pre-configured psk */
Gilles Peskine449bd832023-01-11 14:50:10 +0100350 if (ssl->conf->psk_identity != NULL &&
Jerry Yu2f0abc92022-07-22 19:34:48 +0800351 identity_len == ssl->conf->psk_identity_len &&
Gilles Peskine449bd832023-01-11 14:50:10 +0100352 mbedtls_ct_memcmp(ssl->conf->psk_identity,
353 identity, identity_len) == 0) {
Ronald Cron606671e2023-02-17 11:36:33 +0100354 ret = mbedtls_ssl_set_hs_psk(ssl, ssl->conf->psk, ssl->conf->psk_len);
355 if (ret != 0) {
356 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_set_hs_psk", ret);
357 return ret;
358 }
Ronald Cronfbae94a2023-12-05 18:15:14 +0100359 return SSL_TLS1_3_PSK_IDENTITY_MATCH;
Jerry Yu1c105562022-07-10 06:32:38 +0000360 }
361
Ronald Cronfbae94a2023-12-05 18:15:14 +0100362 return SSL_TLS1_3_PSK_IDENTITY_DOES_NOT_MATCH;
Jerry Yu1c105562022-07-10 06:32:38 +0000363}
364
Ronald Cron6e311272023-12-05 17:57:01 +0100365#define SSL_TLS1_3_BINDER_DOES_NOT_MATCH 1
366#define SSL_TLS1_3_BINDER_MATCH 0
Jerry Yu6e74a7e2022-07-20 20:49:32 +0800367MBEDTLS_CHECK_RETURN_CRITICAL
Xiaokang Qian9f1747b2023-03-30 02:50:04 +0000368static int ssl_tls13_offered_psks_check_binder_match(
369 mbedtls_ssl_context *ssl,
370 const unsigned char *binder, size_t binder_len,
371 int psk_type, psa_algorithm_t psk_hash_alg)
Jerry Yu1c105562022-07-10 06:32:38 +0000372{
373 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yu96a2e362022-07-21 15:11:34 +0800374
Jerry Yudaf375a2022-07-20 21:31:43 +0800375 unsigned char transcript[PSA_HASH_MAX_SIZE];
Jerry Yu1c105562022-07-10 06:32:38 +0000376 size_t transcript_len;
Jerry Yu2f0abc92022-07-22 19:34:48 +0800377 unsigned char *psk;
Jerry Yu96a2e362022-07-21 15:11:34 +0800378 size_t psk_len;
Jerry Yudaf375a2022-07-20 21:31:43 +0800379 unsigned char server_computed_binder[PSA_HASH_MAX_SIZE];
Jerry Yu1c105562022-07-10 06:32:38 +0000380
Jerry Yu1c105562022-07-10 06:32:38 +0000381 /* Get current state of handshake transcript. */
Jerry Yu29d9faa2022-08-23 17:52:45 +0800382 ret = mbedtls_ssl_get_handshake_transcript(
Manuel Pégourié-Gonnard2d6d9932023-03-28 11:38:08 +0200383 ssl, mbedtls_md_type_from_psa_alg(psk_hash_alg),
Gilles Peskine449bd832023-01-11 14:50:10 +0100384 transcript, sizeof(transcript), &transcript_len);
385 if (ret != 0) {
386 return ret;
387 }
Jerry Yu1c105562022-07-10 06:32:38 +0000388
Gilles Peskine449bd832023-01-11 14:50:10 +0100389 ret = mbedtls_ssl_tls13_export_handshake_psk(ssl, &psk, &psk_len);
390 if (ret != 0) {
391 return ret;
392 }
Jerry Yu96a2e362022-07-21 15:11:34 +0800393
Gilles Peskine449bd832023-01-11 14:50:10 +0100394 ret = mbedtls_ssl_tls13_create_psk_binder(ssl, psk_hash_alg,
395 psk, psk_len, psk_type,
396 transcript,
397 server_computed_binder);
Jerry Yu96a2e362022-07-21 15:11:34 +0800398#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +0100399 mbedtls_free((void *) psk);
Jerry Yu96a2e362022-07-21 15:11:34 +0800400#endif
Gilles Peskine449bd832023-01-11 14:50:10 +0100401 if (ret != 0) {
402 MBEDTLS_SSL_DEBUG_MSG(1, ("PSK binder calculation failed."));
403 return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
Jerry Yu1c105562022-07-10 06:32:38 +0000404 }
405
Gilles Peskine449bd832023-01-11 14:50:10 +0100406 MBEDTLS_SSL_DEBUG_BUF(3, "psk binder ( computed ): ",
407 server_computed_binder, transcript_len);
408 MBEDTLS_SSL_DEBUG_BUF(3, "psk binder ( received ): ", binder, binder_len);
Jerry Yu1c105562022-07-10 06:32:38 +0000409
Gilles Peskine449bd832023-01-11 14:50:10 +0100410 if (mbedtls_ct_memcmp(server_computed_binder, binder, binder_len) == 0) {
Ronald Cron6e311272023-12-05 17:57:01 +0100411 return SSL_TLS1_3_BINDER_MATCH;
Jerry Yu1c105562022-07-10 06:32:38 +0000412 }
413
Gilles Peskine449bd832023-01-11 14:50:10 +0100414 mbedtls_platform_zeroize(server_computed_binder,
415 sizeof(server_computed_binder));
Ronald Cron6e311272023-12-05 17:57:01 +0100416 return SSL_TLS1_3_BINDER_DOES_NOT_MATCH;
Jerry Yu1c105562022-07-10 06:32:38 +0000417}
Jerry Yu96a2e362022-07-21 15:11:34 +0800418
Jerry Yu82534862022-08-30 10:42:33 +0800419#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Jerry Yuf35ba382022-08-23 17:58:26 +0800420MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100421static int ssl_tls13_session_copy_ticket(mbedtls_ssl_session *dst,
422 const mbedtls_ssl_session *src)
Jerry Yu82534862022-08-30 10:42:33 +0800423{
Jerry Yu82534862022-08-30 10:42:33 +0800424 dst->ticket_age_add = src->ticket_age_add;
425 dst->ticket_flags = src->ticket_flags;
426 dst->resumption_key_len = src->resumption_key_len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100427 if (src->resumption_key_len == 0) {
428 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
429 }
430 memcpy(dst->resumption_key, src->resumption_key, src->resumption_key_len);
Jerry Yu4746b102022-09-13 11:11:48 +0800431
Jerry Yu33bf2402022-12-12 16:01:43 +0800432#if defined(MBEDTLS_SSL_EARLY_DATA)
433 dst->max_early_data_size = src->max_early_data_size;
434#endif
435
Gilles Peskine449bd832023-01-11 14:50:10 +0100436 return 0;
Jerry Yu82534862022-08-30 10:42:33 +0800437}
438#endif /* MBEDTLS_SSL_SESSION_TICKETS */
439
Ronald Cron79cdd412024-02-20 17:19:28 +0100440struct psk_attributes {
441 int type;
442 int key_exchange_mode;
443};
444#define PSK_ATTRIBUTES_INIT { 0, 0 }
445
Jerry Yu1c105562022-07-10 06:32:38 +0000446/* Parser for pre_shared_key extension in client hello
447 * struct {
448 * opaque identity<1..2^16-1>;
449 * uint32 obfuscated_ticket_age;
450 * } PskIdentity;
451 *
452 * opaque PskBinderEntry<32..255>;
453 *
454 * struct {
455 * PskIdentity identities<7..2^16-1>;
456 * PskBinderEntry binders<33..2^16-1>;
457 * } OfferedPsks;
458 *
459 * struct {
460 * select (Handshake.msg_type) {
461 * case client_hello: OfferedPsks;
462 * ....
463 * };
464 * } PreSharedKeyExtension;
465 */
Jerry Yu6e74a7e2022-07-20 20:49:32 +0800466MBEDTLS_CHECK_RETURN_CRITICAL
Xiaokang Qian9f1747b2023-03-30 02:50:04 +0000467static int ssl_tls13_parse_pre_shared_key_ext(
468 mbedtls_ssl_context *ssl,
469 const unsigned char *pre_shared_key_ext,
470 const unsigned char *pre_shared_key_ext_end,
471 const unsigned char *ciphersuites,
Ronald Cron79cdd412024-02-20 17:19:28 +0100472 const unsigned char *ciphersuites_end,
473 struct psk_attributes *psk)
Jerry Yu1c105562022-07-10 06:32:38 +0000474{
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +0100475 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yu29d9faa2022-08-23 17:52:45 +0800476 const unsigned char *identities = pre_shared_key_ext;
Jerry Yu568ec252022-07-22 21:27:34 +0800477 const unsigned char *p_identity_len;
478 size_t identities_len;
Jerry Yu1c105562022-07-10 06:32:38 +0000479 const unsigned char *identities_end;
Jerry Yu568ec252022-07-22 21:27:34 +0800480 const unsigned char *binders;
481 const unsigned char *p_binder_len;
482 size_t binders_len;
Jerry Yu1c105562022-07-10 06:32:38 +0000483 const unsigned char *binders_end;
Jerry Yu96a2e362022-07-21 15:11:34 +0800484 int matched_identity = -1;
485 int identity_id = -1;
Jerry Yu1c105562022-07-10 06:32:38 +0000486
Gilles Peskine449bd832023-01-11 14:50:10 +0100487 MBEDTLS_SSL_DEBUG_BUF(3, "pre_shared_key extension",
488 pre_shared_key_ext,
489 pre_shared_key_ext_end - pre_shared_key_ext);
Jerry Yu1c105562022-07-10 06:32:38 +0000490
Jerry Yu96a2e362022-07-21 15:11:34 +0800491 /* identities_len 2 bytes
492 * identities_data >= 7 bytes
493 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100494 MBEDTLS_SSL_CHK_BUF_READ_PTR(identities, pre_shared_key_ext_end, 7 + 2);
495 identities_len = MBEDTLS_GET_UINT16_BE(identities, 0);
Jerry Yu568ec252022-07-22 21:27:34 +0800496 p_identity_len = identities + 2;
Gilles Peskine449bd832023-01-11 14:50:10 +0100497 MBEDTLS_SSL_CHK_BUF_READ_PTR(p_identity_len, pre_shared_key_ext_end,
498 identities_len);
Jerry Yu568ec252022-07-22 21:27:34 +0800499 identities_end = p_identity_len + identities_len;
Jerry Yu1c105562022-07-10 06:32:38 +0000500
Jerry Yu96a2e362022-07-21 15:11:34 +0800501 /* binders_len 2 bytes
502 * binders >= 33 bytes
503 */
Jerry Yu568ec252022-07-22 21:27:34 +0800504 binders = identities_end;
Gilles Peskine449bd832023-01-11 14:50:10 +0100505 MBEDTLS_SSL_CHK_BUF_READ_PTR(binders, pre_shared_key_ext_end, 33 + 2);
506 binders_len = MBEDTLS_GET_UINT16_BE(binders, 0);
Jerry Yu568ec252022-07-22 21:27:34 +0800507 p_binder_len = binders + 2;
Gilles Peskine449bd832023-01-11 14:50:10 +0100508 MBEDTLS_SSL_CHK_BUF_READ_PTR(p_binder_len, pre_shared_key_ext_end, binders_len);
Jerry Yu568ec252022-07-22 21:27:34 +0800509 binders_end = p_binder_len + binders_len;
Jerry Yu1c105562022-07-10 06:32:38 +0000510
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +0100511 ret = ssl->handshake->update_checksum(ssl, pre_shared_key_ext,
512 identities_end - pre_shared_key_ext);
513 if (0 != ret) {
514 MBEDTLS_SSL_DEBUG_RET(1, ("update_checksum"), ret);
515 return ret;
516 }
Jerry Yu96a2e362022-07-21 15:11:34 +0800517
Gilles Peskine449bd832023-01-11 14:50:10 +0100518 while (p_identity_len < identities_end && p_binder_len < binders_end) {
Jerry Yu1c105562022-07-10 06:32:38 +0000519 const unsigned char *identity;
Jerry Yu568ec252022-07-22 21:27:34 +0800520 size_t identity_len;
Jerry Yu82534862022-08-30 10:42:33 +0800521 uint32_t obfuscated_ticket_age;
Jerry Yu96a2e362022-07-21 15:11:34 +0800522 const unsigned char *binder;
Jerry Yu568ec252022-07-22 21:27:34 +0800523 size_t binder_len;
Ronald Cron89089cc2024-02-14 18:18:08 +0100524 int psk_ciphersuite_id;
525 psa_algorithm_t psk_hash_alg;
Ronald Croncf284562024-02-16 18:54:10 +0100526 int allowed_key_exchange_modes;
Jerry Yu29d9faa2022-08-23 17:52:45 +0800527 const mbedtls_ssl_ciphersuite_t *ciphersuite_info;
Jerry Yu82534862022-08-30 10:42:33 +0800528#if defined(MBEDTLS_SSL_SESSION_TICKETS)
529 mbedtls_ssl_session session;
Gilles Peskine449bd832023-01-11 14:50:10 +0100530 mbedtls_ssl_session_init(&session);
Jerry Yu82534862022-08-30 10:42:33 +0800531#endif
Jerry Yubb852022022-07-20 21:10:44 +0800532
Gilles Peskine449bd832023-01-11 14:50:10 +0100533 MBEDTLS_SSL_CHK_BUF_READ_PTR(p_identity_len, identities_end, 2 + 1 + 4);
534 identity_len = MBEDTLS_GET_UINT16_BE(p_identity_len, 0);
Jerry Yu568ec252022-07-22 21:27:34 +0800535 identity = p_identity_len + 2;
Gilles Peskine449bd832023-01-11 14:50:10 +0100536 MBEDTLS_SSL_CHK_BUF_READ_PTR(identity, identities_end, identity_len + 4);
537 obfuscated_ticket_age = MBEDTLS_GET_UINT32_BE(identity, identity_len);
Jerry Yu568ec252022-07-22 21:27:34 +0800538 p_identity_len += identity_len + 6;
Jerry Yu1c105562022-07-10 06:32:38 +0000539
Gilles Peskine449bd832023-01-11 14:50:10 +0100540 MBEDTLS_SSL_CHK_BUF_READ_PTR(p_binder_len, binders_end, 1 + 32);
Jerry Yu568ec252022-07-22 21:27:34 +0800541 binder_len = *p_binder_len;
542 binder = p_binder_len + 1;
Gilles Peskine449bd832023-01-11 14:50:10 +0100543 MBEDTLS_SSL_CHK_BUF_READ_PTR(binder, binders_end, binder_len);
Jerry Yu568ec252022-07-22 21:27:34 +0800544 p_binder_len += binder_len + 1;
Jerry Yu96a2e362022-07-21 15:11:34 +0800545
Jerry Yu96a2e362022-07-21 15:11:34 +0800546 identity_id++;
Gilles Peskine449bd832023-01-11 14:50:10 +0100547 if (matched_identity != -1) {
Jerry Yu1c105562022-07-10 06:32:38 +0000548 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +0100549 }
Jerry Yu1c105562022-07-10 06:32:38 +0000550
Jerry Yu96a2e362022-07-21 15:11:34 +0800551 ret = ssl_tls13_offered_psks_check_identity_match(
Gilles Peskine449bd832023-01-11 14:50:10 +0100552 ssl, identity, identity_len, obfuscated_ticket_age,
Ronald Cron79cdd412024-02-20 17:19:28 +0100553 &psk->type, &session);
Ronald Cronfbae94a2023-12-05 18:15:14 +0100554 if (ret != SSL_TLS1_3_PSK_IDENTITY_MATCH) {
Jerry Yu96a2e362022-07-21 15:11:34 +0800555 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +0100556 }
Jerry Yu96a2e362022-07-21 15:11:34 +0800557
Gilles Peskine449bd832023-01-11 14:50:10 +0100558 MBEDTLS_SSL_DEBUG_MSG(4, ("found matched identity"));
Ronald Cron89089cc2024-02-14 18:18:08 +0100559
Ronald Cron79cdd412024-02-20 17:19:28 +0100560 switch (psk->type) {
Jerry Yu0baf9072022-08-25 11:21:04 +0800561 case MBEDTLS_SSL_TLS1_3_PSK_EXTERNAL:
Ronald Cron89089cc2024-02-14 18:18:08 +0100562 psk_ciphersuite_id = 0;
563 psk_hash_alg = PSA_ALG_SHA_256;
Ronald Croncf284562024-02-16 18:54:10 +0100564 allowed_key_exchange_modes =
565 MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK_ALL;
Jerry Yu0baf9072022-08-25 11:21:04 +0800566 break;
Jerry Yu82534862022-08-30 10:42:33 +0800567#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Ronald Cronf7e99162024-02-14 16:04:02 +0100568 case MBEDTLS_SSL_TLS1_3_PSK_RESUMPTION:
Ronald Cron89089cc2024-02-14 18:18:08 +0100569 psk_ciphersuite_id = session.ciphersuite;
570 psk_hash_alg = PSA_ALG_NONE;
Ronald Croncf284562024-02-16 18:54:10 +0100571 ssl->session_negotiate->ticket_flags = session.ticket_flags;
572 allowed_key_exchange_modes =
573 session.ticket_flags &
574 MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK_ALL;
Jerry Yu0baf9072022-08-25 11:21:04 +0800575 break;
Ronald Cronf7e99162024-02-14 16:04:02 +0100576#endif
Jerry Yu0baf9072022-08-25 11:21:04 +0800577 default:
Gilles Peskine449bd832023-01-11 14:50:10 +0100578 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Jerry Yu0baf9072022-08-25 11:21:04 +0800579 }
Ronald Cron89089cc2024-02-14 18:18:08 +0100580
Ronald Cron79cdd412024-02-20 17:19:28 +0100581 psk->key_exchange_mode = MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_NONE;
582
Ronald Croncf284562024-02-16 18:54:10 +0100583 if ((allowed_key_exchange_modes &
584 MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK_EPHEMERAL) &&
585 ssl_tls13_key_exchange_is_psk_ephemeral_available(ssl)) {
Ronald Cron79cdd412024-02-20 17:19:28 +0100586 psk->key_exchange_mode = MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK_EPHEMERAL;
Ronald Croncf284562024-02-16 18:54:10 +0100587 } else if ((allowed_key_exchange_modes &
588 MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK) &&
589 ssl_tls13_key_exchange_is_psk_available(ssl)) {
Ronald Cron79cdd412024-02-20 17:19:28 +0100590 psk->key_exchange_mode = MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK;
Ronald Croncf284562024-02-16 18:54:10 +0100591 }
592
Ronald Cron79cdd412024-02-20 17:19:28 +0100593 if (psk->key_exchange_mode == MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_NONE) {
Ronald Croncf284562024-02-16 18:54:10 +0100594 MBEDTLS_SSL_DEBUG_MSG(3, ("No suitable PSK key exchange mode"));
595 continue;
596 }
597
Ronald Cron89089cc2024-02-14 18:18:08 +0100598 ssl_tls13_select_ciphersuite(ssl, ciphersuites, ciphersuites_end,
599 psk_ciphersuite_id, psk_hash_alg,
600 &ciphersuite_info);
601
602 if (ciphersuite_info == NULL) {
603#if defined(MBEDTLS_SSL_SESSION_TICKETS)
604 mbedtls_ssl_session_free(&session);
605#endif
606 /*
607 * We consider finding a ciphersuite suitable for the PSK as part
608 * of the validation of its binder. Thus if we do not find one, we
609 * abort the handshake with a decrypt_error alert.
610 */
Jerry Yuf35ba382022-08-23 17:58:26 +0800611 MBEDTLS_SSL_PEND_FATAL_ALERT(
612 MBEDTLS_SSL_ALERT_MSG_DECRYPT_ERROR,
Gilles Peskine449bd832023-01-11 14:50:10 +0100613 MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE);
Ronald Cron89089cc2024-02-14 18:18:08 +0100614 return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
Jerry Yu5725f1c2022-08-21 17:27:16 +0800615 }
616
Jerry Yu96a2e362022-07-21 15:11:34 +0800617 ret = ssl_tls13_offered_psks_check_binder_match(
Ronald Cron79cdd412024-02-20 17:19:28 +0100618 ssl, binder, binder_len, psk->type,
Dave Rodgman2eab4622023-10-05 13:30:37 +0100619 mbedtls_md_psa_alg_from_type((mbedtls_md_type_t) ciphersuite_info->mac));
Ronald Cron6e311272023-12-05 17:57:01 +0100620 if (ret != SSL_TLS1_3_BINDER_MATCH) {
Jerry Yuc5a23a02022-08-25 10:51:44 +0800621 /* For security reasons, the handshake should be aborted when we
622 * fail to validate a binder value. See RFC 8446 section 4.2.11.2
623 * and appendix E.6. */
Jerry Yu82534862022-08-30 10:42:33 +0800624#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Gilles Peskine449bd832023-01-11 14:50:10 +0100625 mbedtls_ssl_session_free(&session);
Jerry Yu82534862022-08-30 10:42:33 +0800626#endif
Gilles Peskine449bd832023-01-11 14:50:10 +0100627 MBEDTLS_SSL_DEBUG_MSG(3, ("Invalid binder."));
Xiaokang Qian9f1747b2023-03-30 02:50:04 +0000628 MBEDTLS_SSL_DEBUG_RET(
629 1, "ssl_tls13_offered_psks_check_binder_match", ret);
Jerry Yu96a2e362022-07-21 15:11:34 +0800630 MBEDTLS_SSL_PEND_FATAL_ALERT(
631 MBEDTLS_SSL_ALERT_MSG_DECRYPT_ERROR,
Gilles Peskine449bd832023-01-11 14:50:10 +0100632 MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE);
633 return ret;
Jerry Yu96a2e362022-07-21 15:11:34 +0800634 }
Jerry Yu96a2e362022-07-21 15:11:34 +0800635
636 matched_identity = identity_id;
Jerry Yu5725f1c2022-08-21 17:27:16 +0800637
638 /* Update handshake parameters */
Jerry Yue5834fd2022-08-29 20:16:09 +0800639 ssl->handshake->ciphersuite_info = ciphersuite_info;
Ronald Cron89089cc2024-02-14 18:18:08 +0100640 ssl->session_negotiate->ciphersuite = ciphersuite_info->id;
Gilles Peskine449bd832023-01-11 14:50:10 +0100641 MBEDTLS_SSL_DEBUG_MSG(2, ("overwrite ciphersuite: %04x - %s",
Ronald Cron89089cc2024-02-14 18:18:08 +0100642 ((unsigned) ciphersuite_info->id),
643 ciphersuite_info->name));
Jerry Yu82534862022-08-30 10:42:33 +0800644#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Ronald Cron79cdd412024-02-20 17:19:28 +0100645 if (psk->type == MBEDTLS_SSL_TLS1_3_PSK_RESUMPTION) {
Gilles Peskine449bd832023-01-11 14:50:10 +0100646 ret = ssl_tls13_session_copy_ticket(ssl->session_negotiate,
647 &session);
648 mbedtls_ssl_session_free(&session);
649 if (ret != 0) {
650 return ret;
651 }
Jerry Yu82534862022-08-30 10:42:33 +0800652 }
653#endif /* MBEDTLS_SSL_SESSION_TICKETS */
Jerry Yu1c105562022-07-10 06:32:38 +0000654 }
655
Gilles Peskine449bd832023-01-11 14:50:10 +0100656 if (p_identity_len != identities_end || p_binder_len != binders_end) {
657 MBEDTLS_SSL_DEBUG_MSG(3, ("pre_shared_key extension decode error"));
658 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR,
659 MBEDTLS_ERR_SSL_DECODE_ERROR);
660 return MBEDTLS_ERR_SSL_DECODE_ERROR;
Jerry Yu1c105562022-07-10 06:32:38 +0000661 }
662
Jerry Yu6f1db3f2022-07-22 23:05:59 +0800663 /* Update the handshake transcript with the binder list. */
Xiaokang Qian9f1747b2023-03-30 02:50:04 +0000664 ret = ssl->handshake->update_checksum(
665 ssl, identities_end, (size_t) (binders_end - identities_end));
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +0100666 if (0 != ret) {
667 MBEDTLS_SSL_DEBUG_RET(1, ("update_checksum"), ret);
668 return ret;
669 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100670 if (matched_identity == -1) {
Ronald Croncf284562024-02-16 18:54:10 +0100671 MBEDTLS_SSL_DEBUG_MSG(3, ("No usable PSK or ticket."));
Gilles Peskine449bd832023-01-11 14:50:10 +0100672 return MBEDTLS_ERR_SSL_UNKNOWN_IDENTITY;
Jerry Yu1c105562022-07-10 06:32:38 +0000673 }
674
Gilles Peskine449bd832023-01-11 14:50:10 +0100675 ssl->handshake->selected_identity = (uint16_t) matched_identity;
676 MBEDTLS_SSL_DEBUG_MSG(3, ("Pre shared key found"));
Jerry Yu1c105562022-07-10 06:32:38 +0000677
Gilles Peskine449bd832023-01-11 14:50:10 +0100678 return 0;
Jerry Yu1c105562022-07-10 06:32:38 +0000679}
Jerry Yu032b15ce2022-07-11 06:10:03 +0000680
681/*
682 * struct {
683 * select ( Handshake.msg_type ) {
684 * ....
685 * case server_hello:
686 * uint16 selected_identity;
687 * }
688 * } PreSharedKeyExtension;
689 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100690static int ssl_tls13_write_server_pre_shared_key_ext(mbedtls_ssl_context *ssl,
691 unsigned char *buf,
692 unsigned char *end,
693 size_t *olen)
Jerry Yu032b15ce2022-07-11 06:10:03 +0000694{
Gilles Peskine449bd832023-01-11 14:50:10 +0100695 unsigned char *p = (unsigned char *) buf;
Jerry Yu032b15ce2022-07-11 06:10:03 +0000696
697 *olen = 0;
698
David Horstmann21b89762022-10-06 18:34:28 +0100699 int not_using_psk = 0;
Jerry Yu032b15ce2022-07-11 06:10:03 +0000700#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +0100701 not_using_psk = (mbedtls_svc_key_id_is_null(ssl->handshake->psk_opaque));
Jerry Yu032b15ce2022-07-11 06:10:03 +0000702#else
Gilles Peskine449bd832023-01-11 14:50:10 +0100703 not_using_psk = (ssl->handshake->psk == NULL);
Jerry Yu032b15ce2022-07-11 06:10:03 +0000704#endif
Gilles Peskine449bd832023-01-11 14:50:10 +0100705 if (not_using_psk) {
Jerry Yu032b15ce2022-07-11 06:10:03 +0000706 /* We shouldn't have called this extension writer unless we've
707 * chosen to use a PSK. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100708 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Jerry Yu032b15ce2022-07-11 06:10:03 +0000709 }
710
Gilles Peskine449bd832023-01-11 14:50:10 +0100711 MBEDTLS_SSL_DEBUG_MSG(3, ("server hello, adding pre_shared_key extension"));
712 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 6);
Jerry Yu032b15ce2022-07-11 06:10:03 +0000713
Gilles Peskine449bd832023-01-11 14:50:10 +0100714 MBEDTLS_PUT_UINT16_BE(MBEDTLS_TLS_EXT_PRE_SHARED_KEY, p, 0);
715 MBEDTLS_PUT_UINT16_BE(2, p, 2);
Jerry Yu032b15ce2022-07-11 06:10:03 +0000716
Gilles Peskine449bd832023-01-11 14:50:10 +0100717 MBEDTLS_PUT_UINT16_BE(ssl->handshake->selected_identity, p, 4);
Jerry Yu032b15ce2022-07-11 06:10:03 +0000718
719 *olen = 6;
720
Gilles Peskine449bd832023-01-11 14:50:10 +0100721 MBEDTLS_SSL_DEBUG_MSG(4, ("sent selected_identity: %u",
722 ssl->handshake->selected_identity));
Jerry Yu032b15ce2022-07-11 06:10:03 +0000723
Gilles Peskine449bd832023-01-11 14:50:10 +0100724 mbedtls_ssl_tls13_set_hs_sent_ext_mask(ssl, MBEDTLS_TLS_EXT_PRE_SHARED_KEY);
Jerry Yub95dd362022-11-08 21:19:34 +0800725
Gilles Peskine449bd832023-01-11 14:50:10 +0100726 return 0;
Jerry Yu032b15ce2022-07-11 06:10:03 +0000727}
728
Ronald Cron41a443a2022-10-04 16:38:25 +0200729#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED */
Jerry Yue19e3b92022-07-08 12:04:51 +0000730
XiaokangQian7807f9f2022-02-15 10:04:37 +0000731/* From RFC 8446:
732 * struct {
XiaokangQiancfd925f2022-04-14 07:10:37 +0000733 * ProtocolVersion versions<2..254>;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000734 * } SupportedVersions;
735 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200736MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100737static int ssl_tls13_parse_supported_versions_ext(mbedtls_ssl_context *ssl,
738 const unsigned char *buf,
739 const unsigned char *end)
XiaokangQian7807f9f2022-02-15 10:04:37 +0000740{
XiaokangQian7807f9f2022-02-15 10:04:37 +0000741 const unsigned char *p = buf;
XiaokangQian8f9dfe42022-04-15 02:52:39 +0000742 size_t versions_len;
XiaokangQian4080a7f2022-04-11 09:55:18 +0000743 const unsigned char *versions_end;
XiaokangQian0a1b54e2022-04-21 03:01:38 +0000744 uint16_t tls_version;
Ronald Cron5af4c7f2023-03-07 20:46:59 +0100745 int found_supported_version = 0;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000746
Gilles Peskine449bd832023-01-11 14:50:10 +0100747 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, 1);
XiaokangQian4080a7f2022-04-11 09:55:18 +0000748 versions_len = p[0];
XiaokangQian7807f9f2022-02-15 10:04:37 +0000749 p += 1;
750
Gilles Peskine449bd832023-01-11 14:50:10 +0100751 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, versions_len);
XiaokangQian4080a7f2022-04-11 09:55:18 +0000752 versions_end = p + versions_len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100753 while (p < versions_end) {
754 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, versions_end, 2);
755 tls_version = mbedtls_ssl_read_version(p, ssl->conf->transport);
XiaokangQianb67384d2022-04-19 00:02:38 +0000756 p += 2;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000757
Ronald Cron3bd2b022023-04-03 16:45:39 +0200758 if (MBEDTLS_SSL_VERSION_TLS1_3 == tls_version) {
Ronald Cron5af4c7f2023-03-07 20:46:59 +0100759 found_supported_version = 1;
760 break;
761 }
762
Ronald Cron3bd2b022023-04-03 16:45:39 +0200763 if ((MBEDTLS_SSL_VERSION_TLS1_2 == tls_version) &&
764 mbedtls_ssl_conf_is_tls12_enabled(ssl->conf)) {
Ronald Cron5af4c7f2023-03-07 20:46:59 +0100765 found_supported_version = 1;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000766 break;
767 }
XiaokangQian7807f9f2022-02-15 10:04:37 +0000768 }
769
Ronald Cron5af4c7f2023-03-07 20:46:59 +0100770 if (!found_supported_version) {
771 MBEDTLS_SSL_DEBUG_MSG(1, ("No supported version found."));
XiaokangQian7807f9f2022-02-15 10:04:37 +0000772
Gilles Peskine449bd832023-01-11 14:50:10 +0100773 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_PROTOCOL_VERSION,
774 MBEDTLS_ERR_SSL_BAD_PROTOCOL_VERSION);
775 return MBEDTLS_ERR_SSL_BAD_PROTOCOL_VERSION;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000776 }
777
Ronald Cron5af4c7f2023-03-07 20:46:59 +0100778 MBEDTLS_SSL_DEBUG_MSG(1, ("Negotiated version: [%04x]",
Gilles Peskine449bd832023-01-11 14:50:10 +0100779 (unsigned int) tls_version));
XiaokangQian7807f9f2022-02-15 10:04:37 +0000780
Ronald Cron5af4c7f2023-03-07 20:46:59 +0100781 return (int) tls_version;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000782}
783
Przemek Stekiel8c0a9532023-06-15 16:48:19 +0200784#if defined(PSA_WANT_ALG_ECDH) || defined(PSA_WANT_ALG_FFDH)
XiaokangQiane8ff3502022-04-22 02:34:40 +0000785/*
XiaokangQian7807f9f2022-02-15 10:04:37 +0000786 *
787 * From RFC 8446:
788 * enum {
789 * ... (0xFFFF)
790 * } NamedGroup;
791 * struct {
792 * NamedGroup named_group_list<2..2^16-1>;
793 * } NamedGroupList;
794 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200795MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100796static int ssl_tls13_parse_supported_groups_ext(mbedtls_ssl_context *ssl,
797 const unsigned char *buf,
798 const unsigned char *end)
XiaokangQian7807f9f2022-02-15 10:04:37 +0000799{
XiaokangQian7807f9f2022-02-15 10:04:37 +0000800 const unsigned char *p = buf;
XiaokangQian84823772022-04-19 07:57:30 +0000801 size_t named_group_list_len;
XiaokangQian8f9dfe42022-04-15 02:52:39 +0000802 const unsigned char *named_group_list_end;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000803
Gilles Peskine449bd832023-01-11 14:50:10 +0100804 MBEDTLS_SSL_DEBUG_BUF(3, "supported_groups extension", p, end - buf);
805 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, 2);
806 named_group_list_len = MBEDTLS_GET_UINT16_BE(p, 0);
XiaokangQian7807f9f2022-02-15 10:04:37 +0000807 p += 2;
Gilles Peskine449bd832023-01-11 14:50:10 +0100808 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, named_group_list_len);
XiaokangQian8f9dfe42022-04-15 02:52:39 +0000809 named_group_list_end = p + named_group_list_len;
XiaokangQian4e8cd7b2022-04-21 09:48:09 +0000810 ssl->handshake->hrr_selected_group = 0;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000811
Gilles Peskine449bd832023-01-11 14:50:10 +0100812 while (p < named_group_list_end) {
XiaokangQian08037552022-04-20 07:16:41 +0000813 uint16_t named_group;
Gilles Peskine449bd832023-01-11 14:50:10 +0100814 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, named_group_list_end, 2);
815 named_group = MBEDTLS_GET_UINT16_BE(p, 0);
XiaokangQian08037552022-04-20 07:16:41 +0000816 p += 2;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000817
Gilles Peskine449bd832023-01-11 14:50:10 +0100818 MBEDTLS_SSL_DEBUG_MSG(2,
819 ("got named group: %s(%04x)",
820 mbedtls_ssl_named_group_to_str(named_group),
821 named_group));
XiaokangQian08037552022-04-20 07:16:41 +0000822
Gilles Peskine449bd832023-01-11 14:50:10 +0100823 if (!mbedtls_ssl_named_group_is_offered(ssl, named_group) ||
824 !mbedtls_ssl_named_group_is_supported(named_group) ||
825 ssl->handshake->hrr_selected_group != 0) {
XiaokangQian08037552022-04-20 07:16:41 +0000826 continue;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000827 }
828
Gilles Peskine449bd832023-01-11 14:50:10 +0100829 MBEDTLS_SSL_DEBUG_MSG(2,
830 ("add named group %s(%04x) into received list.",
831 mbedtls_ssl_named_group_to_str(named_group),
832 named_group));
Jerry Yuc1be19f2022-04-23 16:11:39 +0800833
XiaokangQian4e8cd7b2022-04-21 09:48:09 +0000834 ssl->handshake->hrr_selected_group = named_group;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000835 }
836
Gilles Peskine449bd832023-01-11 14:50:10 +0100837 return 0;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000838
839}
Przemek Stekiel8c0a9532023-06-15 16:48:19 +0200840#endif /* PSA_WANT_ALG_ECDH || PSA_WANT_ALG_FFDH */
XiaokangQian7807f9f2022-02-15 10:04:37 +0000841
XiaokangQian08037552022-04-20 07:16:41 +0000842#define SSL_TLS1_3_PARSE_KEY_SHARES_EXT_NO_MATCH 1
843
Valerio Settic9ae8622023-07-25 11:23:50 +0200844#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_EPHEMERAL_ENABLED)
XiaokangQian7807f9f2022-02-15 10:04:37 +0000845/*
846 * ssl_tls13_parse_key_shares_ext() verifies whether the information in the
Xiaokang Qian9f1747b2023-03-30 02:50:04 +0000847 * extension is correct and stores the first acceptable key share and its
848 * associated group.
XiaokangQian7807f9f2022-02-15 10:04:37 +0000849 *
850 * Possible return values are:
851 * - 0: Successful processing of the client provided key share extension.
Xiaokang Qian9f1747b2023-03-30 02:50:04 +0000852 * - SSL_TLS1_3_PARSE_KEY_SHARES_EXT_NO_MATCH: The key shares provided by
853 * the client does not match a group supported by the server. A
854 * HelloRetryRequest will be needed.
XiaokangQiane8ff3502022-04-22 02:34:40 +0000855 * - A negative value for fatal errors.
Jerry Yuc1be19f2022-04-23 16:11:39 +0800856 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200857MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100858static int ssl_tls13_parse_key_shares_ext(mbedtls_ssl_context *ssl,
859 const unsigned char *buf,
860 const unsigned char *end)
XiaokangQian7807f9f2022-02-15 10:04:37 +0000861{
XiaokangQianb67384d2022-04-19 00:02:38 +0000862 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000863 unsigned char const *p = buf;
XiaokangQian8f9dfe42022-04-15 02:52:39 +0000864 unsigned char const *client_shares_end;
Jerry Yuc1be19f2022-04-23 16:11:39 +0800865 size_t client_shares_len;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000866
867 /* From RFC 8446:
868 *
869 * struct {
870 * KeyShareEntry client_shares<0..2^16-1>;
871 * } KeyShareClientHello;
872 *
873 */
874
Gilles Peskine449bd832023-01-11 14:50:10 +0100875 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, 2);
876 client_shares_len = MBEDTLS_GET_UINT16_BE(p, 0);
XiaokangQian7807f9f2022-02-15 10:04:37 +0000877 p += 2;
Gilles Peskine449bd832023-01-11 14:50:10 +0100878 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, client_shares_len);
XiaokangQian7807f9f2022-02-15 10:04:37 +0000879
880 ssl->handshake->offered_group_id = 0;
XiaokangQian8f9dfe42022-04-15 02:52:39 +0000881 client_shares_end = p + client_shares_len;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000882
883 /* We try to find a suitable key share entry and copy it to the
884 * handshake context. Later, we have to find out whether we can do
885 * something with the provided key share or whether we have to
XiaokangQianc5763b52022-04-02 03:34:37 +0000886 * dismiss it and send a HelloRetryRequest message.
887 */
XiaokangQian7807f9f2022-02-15 10:04:37 +0000888
Gilles Peskine449bd832023-01-11 14:50:10 +0100889 while (p < client_shares_end) {
XiaokangQian9b5d04b2022-04-10 10:20:43 +0000890 uint16_t group;
Jerry Yuc1be19f2022-04-23 16:11:39 +0800891 size_t key_exchange_len;
Jerry Yu086edc22022-05-05 10:50:38 +0800892 const unsigned char *key_exchange;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000893
894 /*
895 * struct {
896 * NamedGroup group;
897 * opaque key_exchange<1..2^16-1>;
898 * } KeyShareEntry;
899 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100900 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, client_shares_end, 4);
901 group = MBEDTLS_GET_UINT16_BE(p, 0);
902 key_exchange_len = MBEDTLS_GET_UINT16_BE(p, 2);
Jerry Yuc1be19f2022-04-23 16:11:39 +0800903 p += 4;
Jerry Yu086edc22022-05-05 10:50:38 +0800904 key_exchange = p;
Gilles Peskine449bd832023-01-11 14:50:10 +0100905 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, client_shares_end, key_exchange_len);
Jerry Yu086edc22022-05-05 10:50:38 +0800906 p += key_exchange_len;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000907
908 /* Continue parsing even if we have already found a match,
XiaokangQianc5763b52022-04-02 03:34:37 +0000909 * for input validation purposes.
910 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100911 if (!mbedtls_ssl_named_group_is_offered(ssl, group) ||
912 !mbedtls_ssl_named_group_is_supported(group) ||
913 ssl->handshake->offered_group_id != 0) {
XiaokangQian060d8672022-04-21 09:24:56 +0000914 continue;
915 }
XiaokangQian060d8672022-04-21 09:24:56 +0000916
XiaokangQian7807f9f2022-02-15 10:04:37 +0000917 /*
Przemek Stekielc89f3ea2023-05-18 15:45:53 +0200918 * ECDHE and FFDHE groups are supported
XiaokangQian7807f9f2022-02-15 10:04:37 +0000919 */
Przemek Stekielc89f3ea2023-05-18 15:45:53 +0200920 if (mbedtls_ssl_tls13_named_group_is_ecdhe(group) ||
Przemek Stekield5f79e72023-06-29 09:08:43 +0200921 mbedtls_ssl_tls13_named_group_is_ffdh(group)) {
Przemek Stekielc89f3ea2023-05-18 15:45:53 +0200922 MBEDTLS_SSL_DEBUG_MSG(2, ("ECDH/FFDH group: %s (%04x)",
Gilles Peskine449bd832023-01-11 14:50:10 +0100923 mbedtls_ssl_named_group_to_str(group),
924 group));
Przemek Stekiel7ac93be2023-07-04 10:02:38 +0200925 ret = mbedtls_ssl_tls13_read_public_xxdhe_share(
Gilles Peskine449bd832023-01-11 14:50:10 +0100926 ssl, key_exchange - 2, key_exchange_len + 2);
927 if (ret != 0) {
928 return ret;
929 }
XiaokangQian4e8cd7b2022-04-21 09:48:09 +0000930
Gilles Peskine449bd832023-01-11 14:50:10 +0100931 } else {
932 MBEDTLS_SSL_DEBUG_MSG(4, ("Unrecognized NamedGroup %u",
933 (unsigned) group));
XiaokangQian7807f9f2022-02-15 10:04:37 +0000934 continue;
935 }
936
XiaokangQian9b5d04b2022-04-10 10:20:43 +0000937 ssl->handshake->offered_group_id = group;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000938 }
939
Jerry Yuc1be19f2022-04-23 16:11:39 +0800940
Gilles Peskine449bd832023-01-11 14:50:10 +0100941 if (ssl->handshake->offered_group_id == 0) {
942 MBEDTLS_SSL_DEBUG_MSG(1, ("no matching key share"));
943 return SSL_TLS1_3_PARSE_KEY_SHARES_EXT_NO_MATCH;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000944 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100945 return 0;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000946}
Valerio Settic9ae8622023-07-25 11:23:50 +0200947#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_EPHEMERAL_ENABLED */
XiaokangQian7807f9f2022-02-15 10:04:37 +0000948
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200949MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100950static int ssl_tls13_client_hello_has_exts(mbedtls_ssl_context *ssl,
951 int exts_mask)
XiaokangQian7807f9f2022-02-15 10:04:37 +0000952{
Jerry Yu0c354a22022-08-29 15:25:36 +0800953 int masked = ssl->handshake->received_extensions & exts_mask;
Gilles Peskine449bd832023-01-11 14:50:10 +0100954 return masked == exts_mask;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000955}
956
Jerry Yue5991322022-11-07 14:03:44 +0800957#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200958MBEDTLS_CHECK_RETURN_CRITICAL
XiaokangQianb67384d2022-04-19 00:02:38 +0000959static int ssl_tls13_client_hello_has_exts_for_ephemeral_key_exchange(
Gilles Peskine449bd832023-01-11 14:50:10 +0100960 mbedtls_ssl_context *ssl)
XiaokangQian7807f9f2022-02-15 10:04:37 +0000961{
Gilles Peskine449bd832023-01-11 14:50:10 +0100962 return ssl_tls13_client_hello_has_exts(
963 ssl,
964 MBEDTLS_SSL_EXT_MASK(SUPPORTED_GROUPS) |
965 MBEDTLS_SSL_EXT_MASK(KEY_SHARE) |
966 MBEDTLS_SSL_EXT_MASK(SIG_ALG));
XiaokangQian7807f9f2022-02-15 10:04:37 +0000967}
Jerry Yue5991322022-11-07 14:03:44 +0800968#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED */
XiaokangQian7807f9f2022-02-15 10:04:37 +0000969
Jerry Yue5991322022-11-07 14:03:44 +0800970#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK_ENABLED)
Jerry Yu77f01482022-07-11 07:03:24 +0000971MBEDTLS_CHECK_RETURN_CRITICAL
972static int ssl_tls13_client_hello_has_exts_for_psk_key_exchange(
Gilles Peskine449bd832023-01-11 14:50:10 +0100973 mbedtls_ssl_context *ssl)
Jerry Yu77f01482022-07-11 07:03:24 +0000974{
Gilles Peskine449bd832023-01-11 14:50:10 +0100975 return ssl_tls13_client_hello_has_exts(
976 ssl,
977 MBEDTLS_SSL_EXT_MASK(PRE_SHARED_KEY) |
978 MBEDTLS_SSL_EXT_MASK(PSK_KEY_EXCHANGE_MODES));
Jerry Yu77f01482022-07-11 07:03:24 +0000979}
Jerry Yue5991322022-11-07 14:03:44 +0800980#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK_ENABLED */
Jerry Yu77f01482022-07-11 07:03:24 +0000981
Jerry Yue5991322022-11-07 14:03:44 +0800982#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK_EPHEMERAL_ENABLED)
Jerry Yu77f01482022-07-11 07:03:24 +0000983MBEDTLS_CHECK_RETURN_CRITICAL
984static int ssl_tls13_client_hello_has_exts_for_psk_ephemeral_key_exchange(
Gilles Peskine449bd832023-01-11 14:50:10 +0100985 mbedtls_ssl_context *ssl)
Jerry Yu77f01482022-07-11 07:03:24 +0000986{
Gilles Peskine449bd832023-01-11 14:50:10 +0100987 return ssl_tls13_client_hello_has_exts(
988 ssl,
989 MBEDTLS_SSL_EXT_MASK(SUPPORTED_GROUPS) |
990 MBEDTLS_SSL_EXT_MASK(KEY_SHARE) |
991 MBEDTLS_SSL_EXT_MASK(PRE_SHARED_KEY) |
992 MBEDTLS_SSL_EXT_MASK(PSK_KEY_EXCHANGE_MODES));
Jerry Yu77f01482022-07-11 07:03:24 +0000993}
Jerry Yue5991322022-11-07 14:03:44 +0800994#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK_EPHEMERAL_ENABLED */
Jerry Yu77f01482022-07-11 07:03:24 +0000995
Pengyu Lv306a01d2023-02-02 16:35:47 +0800996#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED)
997MBEDTLS_CHECK_RETURN_CRITICAL
Pengyu Lvbc4aab72023-12-01 15:37:24 +0800998static int ssl_tls13_key_exchange_is_psk_available(mbedtls_ssl_context *ssl)
Jerry Yu77f01482022-07-11 07:03:24 +0000999{
Jerry Yue5991322022-11-07 14:03:44 +08001000#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK_ENABLED)
Ronald Crone8c162d2024-02-21 10:15:44 +01001001 return mbedtls_ssl_conf_tls13_is_psk_enabled(ssl) &&
Pengyu Lvb2cfafb2023-11-14 13:56:13 +08001002 mbedtls_ssl_tls13_is_psk_supported(ssl) &&
Gilles Peskine449bd832023-01-11 14:50:10 +01001003 ssl_tls13_client_hello_has_exts_for_psk_key_exchange(ssl);
Jerry Yu77f01482022-07-11 07:03:24 +00001004#else
1005 ((void) ssl);
Gilles Peskine449bd832023-01-11 14:50:10 +01001006 return 0;
Jerry Yu77f01482022-07-11 07:03:24 +00001007#endif
1008}
XiaokangQian7807f9f2022-02-15 10:04:37 +00001009
Jerry Yu77f01482022-07-11 07:03:24 +00001010MBEDTLS_CHECK_RETURN_CRITICAL
Pengyu Lvbc4aab72023-12-01 15:37:24 +08001011static int ssl_tls13_key_exchange_is_psk_ephemeral_available(mbedtls_ssl_context *ssl)
Jerry Yu77f01482022-07-11 07:03:24 +00001012{
Jerry Yue5991322022-11-07 14:03:44 +08001013#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK_EPHEMERAL_ENABLED)
Ronald Crone8c162d2024-02-21 10:15:44 +01001014 return mbedtls_ssl_conf_tls13_is_psk_ephemeral_enabled(ssl) &&
Pengyu Lvb2cfafb2023-11-14 13:56:13 +08001015 mbedtls_ssl_tls13_is_psk_ephemeral_supported(ssl) &&
Gilles Peskine449bd832023-01-11 14:50:10 +01001016 ssl_tls13_client_hello_has_exts_for_psk_ephemeral_key_exchange(ssl);
Jerry Yu77f01482022-07-11 07:03:24 +00001017#else
1018 ((void) ssl);
Gilles Peskine449bd832023-01-11 14:50:10 +01001019 return 0;
Jerry Yu77f01482022-07-11 07:03:24 +00001020#endif
1021}
Ronald Cron79cdd412024-02-20 17:19:28 +01001022#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED */
Jerry Yu77f01482022-07-11 07:03:24 +00001023
Ronald Cron79cdd412024-02-20 17:19:28 +01001024MBEDTLS_CHECK_RETURN_CRITICAL
1025static int ssl_tls13_key_exchange_is_ephemeral_available(mbedtls_ssl_context *ssl)
Jerry Yu77f01482022-07-11 07:03:24 +00001026{
Ronald Cron79cdd412024-02-20 17:19:28 +01001027#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED)
1028 return mbedtls_ssl_conf_tls13_is_ephemeral_enabled(ssl) &&
1029 ssl_tls13_client_hello_has_exts_for_ephemeral_key_exchange(ssl);
1030#else
1031 ((void) ssl);
Gilles Peskine449bd832023-01-11 14:50:10 +01001032 return 0;
Ronald Cron79cdd412024-02-20 17:19:28 +01001033#endif
XiaokangQian7807f9f2022-02-15 10:04:37 +00001034}
1035
XiaokangQian81802f42022-06-10 13:25:22 +00001036#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
Ronald Cron928cbd32022-10-04 16:14:26 +02001037 defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED)
Ronald Cron67ea2542022-09-15 17:34:42 +02001038
1039#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01001040static psa_algorithm_t ssl_tls13_iana_sig_alg_to_psa_alg(uint16_t sig_alg)
Ronald Cron67ea2542022-09-15 17:34:42 +02001041{
Gilles Peskine449bd832023-01-11 14:50:10 +01001042 switch (sig_alg) {
Ronald Cron67ea2542022-09-15 17:34:42 +02001043 case MBEDTLS_TLS1_3_SIG_ECDSA_SECP256R1_SHA256:
Gilles Peskine449bd832023-01-11 14:50:10 +01001044 return PSA_ALG_ECDSA(PSA_ALG_SHA_256);
Ronald Cron67ea2542022-09-15 17:34:42 +02001045 case MBEDTLS_TLS1_3_SIG_ECDSA_SECP384R1_SHA384:
Gilles Peskine449bd832023-01-11 14:50:10 +01001046 return PSA_ALG_ECDSA(PSA_ALG_SHA_384);
Ronald Cron67ea2542022-09-15 17:34:42 +02001047 case MBEDTLS_TLS1_3_SIG_ECDSA_SECP521R1_SHA512:
Gilles Peskine449bd832023-01-11 14:50:10 +01001048 return PSA_ALG_ECDSA(PSA_ALG_SHA_512);
Ronald Cron67ea2542022-09-15 17:34:42 +02001049 case MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA256:
Gilles Peskine449bd832023-01-11 14:50:10 +01001050 return PSA_ALG_RSA_PSS(PSA_ALG_SHA_256);
Ronald Cron67ea2542022-09-15 17:34:42 +02001051 case MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA384:
Gilles Peskine449bd832023-01-11 14:50:10 +01001052 return PSA_ALG_RSA_PSS(PSA_ALG_SHA_384);
Ronald Cron67ea2542022-09-15 17:34:42 +02001053 case MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA512:
Gilles Peskine449bd832023-01-11 14:50:10 +01001054 return PSA_ALG_RSA_PSS(PSA_ALG_SHA_512);
Ronald Cron67ea2542022-09-15 17:34:42 +02001055 case MBEDTLS_TLS1_3_SIG_RSA_PKCS1_SHA256:
Gilles Peskine449bd832023-01-11 14:50:10 +01001056 return PSA_ALG_RSA_PKCS1V15_SIGN(PSA_ALG_SHA_256);
Ronald Cron67ea2542022-09-15 17:34:42 +02001057 case MBEDTLS_TLS1_3_SIG_RSA_PKCS1_SHA384:
Gilles Peskine449bd832023-01-11 14:50:10 +01001058 return PSA_ALG_RSA_PKCS1V15_SIGN(PSA_ALG_SHA_384);
Ronald Cron67ea2542022-09-15 17:34:42 +02001059 case MBEDTLS_TLS1_3_SIG_RSA_PKCS1_SHA512:
Gilles Peskine449bd832023-01-11 14:50:10 +01001060 return PSA_ALG_RSA_PKCS1V15_SIGN(PSA_ALG_SHA_512);
Ronald Cron67ea2542022-09-15 17:34:42 +02001061 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01001062 return PSA_ALG_NONE;
Ronald Cron67ea2542022-09-15 17:34:42 +02001063 }
1064}
1065#endif /* MBEDTLS_USE_PSA_CRYPTO */
1066
XiaokangQian23c5be62022-06-07 02:04:34 +00001067/*
XiaokangQianfb665a82022-06-15 03:57:21 +00001068 * Pick best ( private key, certificate chain ) pair based on the signature
1069 * algorithms supported by the client.
XiaokangQian23c5be62022-06-07 02:04:34 +00001070 */
Ronald Cronce7d76e2022-07-08 18:56:49 +02001071MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01001072static int ssl_tls13_pick_key_cert(mbedtls_ssl_context *ssl)
XiaokangQian23c5be62022-06-07 02:04:34 +00001073{
XiaokangQianfb665a82022-06-15 03:57:21 +00001074 mbedtls_ssl_key_cert *key_cert, *key_cert_list;
XiaokangQian81802f42022-06-10 13:25:22 +00001075 const uint16_t *sig_alg = ssl->handshake->received_sig_algs;
XiaokangQian23c5be62022-06-07 02:04:34 +00001076
1077#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Gilles Peskine449bd832023-01-11 14:50:10 +01001078 if (ssl->handshake->sni_key_cert != NULL) {
XiaokangQianfb665a82022-06-15 03:57:21 +00001079 key_cert_list = ssl->handshake->sni_key_cert;
Gilles Peskine449bd832023-01-11 14:50:10 +01001080 } else
XiaokangQian23c5be62022-06-07 02:04:34 +00001081#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
Gilles Peskine449bd832023-01-11 14:50:10 +01001082 key_cert_list = ssl->conf->key_cert;
XiaokangQian23c5be62022-06-07 02:04:34 +00001083
Gilles Peskine449bd832023-01-11 14:50:10 +01001084 if (key_cert_list == NULL) {
1085 MBEDTLS_SSL_DEBUG_MSG(3, ("server has no certificate"));
1086 return -1;
XiaokangQian23c5be62022-06-07 02:04:34 +00001087 }
1088
Gilles Peskine449bd832023-01-11 14:50:10 +01001089 for (; *sig_alg != MBEDTLS_TLS1_3_SIG_NONE; sig_alg++) {
1090 if (!mbedtls_ssl_sig_alg_is_offered(ssl, *sig_alg)) {
Ronald Cron67ea2542022-09-15 17:34:42 +02001091 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +01001092 }
Ronald Cron67ea2542022-09-15 17:34:42 +02001093
Gilles Peskine449bd832023-01-11 14:50:10 +01001094 if (!mbedtls_ssl_tls13_sig_alg_for_cert_verify_is_supported(*sig_alg)) {
Ronald Cron67ea2542022-09-15 17:34:42 +02001095 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +01001096 }
Ronald Cron67ea2542022-09-15 17:34:42 +02001097
Gilles Peskine449bd832023-01-11 14:50:10 +01001098 for (key_cert = key_cert_list; key_cert != NULL;
1099 key_cert = key_cert->next) {
Ronald Cron67ea2542022-09-15 17:34:42 +02001100#if defined(MBEDTLS_USE_PSA_CRYPTO)
1101 psa_algorithm_t psa_alg = PSA_ALG_NONE;
1102#endif /* MBEDTLS_USE_PSA_CRYPTO */
1103
Gilles Peskine449bd832023-01-11 14:50:10 +01001104 MBEDTLS_SSL_DEBUG_CRT(3, "certificate (chain) candidate",
1105 key_cert->cert);
XiaokangQian81802f42022-06-10 13:25:22 +00001106
1107 /*
Gilles Peskine449bd832023-01-11 14:50:10 +01001108 * This avoids sending the client a cert it'll reject based on
1109 * keyUsage or other extensions.
1110 */
1111 if (mbedtls_x509_crt_check_key_usage(
1112 key_cert->cert, MBEDTLS_X509_KU_DIGITAL_SIGNATURE) != 0 ||
XiaokangQian81802f42022-06-10 13:25:22 +00001113 mbedtls_x509_crt_check_extended_key_usage(
XiaokangQianfb665a82022-06-15 03:57:21 +00001114 key_cert->cert, MBEDTLS_OID_SERVER_AUTH,
Gilles Peskine449bd832023-01-11 14:50:10 +01001115 MBEDTLS_OID_SIZE(MBEDTLS_OID_SERVER_AUTH)) != 0) {
1116 MBEDTLS_SSL_DEBUG_MSG(3, ("certificate mismatch: "
1117 "(extended) key usage extension"));
XiaokangQian81802f42022-06-10 13:25:22 +00001118 continue;
1119 }
XiaokangQian23c5be62022-06-07 02:04:34 +00001120
Gilles Peskine449bd832023-01-11 14:50:10 +01001121 MBEDTLS_SSL_DEBUG_MSG(3,
1122 ("ssl_tls13_pick_key_cert:"
1123 "check signature algorithm %s [%04x]",
1124 mbedtls_ssl_sig_alg_to_str(*sig_alg),
1125 *sig_alg));
Ronald Cron67ea2542022-09-15 17:34:42 +02001126#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01001127 psa_alg = ssl_tls13_iana_sig_alg_to_psa_alg(*sig_alg);
Ronald Cron67ea2542022-09-15 17:34:42 +02001128#endif /* MBEDTLS_USE_PSA_CRYPTO */
1129
Gilles Peskine449bd832023-01-11 14:50:10 +01001130 if (mbedtls_ssl_tls13_check_sig_alg_cert_key_match(
1131 *sig_alg, &key_cert->cert->pk)
Ronald Cron67ea2542022-09-15 17:34:42 +02001132#if defined(MBEDTLS_USE_PSA_CRYPTO)
1133 && psa_alg != PSA_ALG_NONE &&
Gilles Peskine449bd832023-01-11 14:50:10 +01001134 mbedtls_pk_can_do_ext(&key_cert->cert->pk, psa_alg,
1135 PSA_KEY_USAGE_SIGN_HASH) == 1
Ronald Cron67ea2542022-09-15 17:34:42 +02001136#endif /* MBEDTLS_USE_PSA_CRYPTO */
Gilles Peskine449bd832023-01-11 14:50:10 +01001137 ) {
XiaokangQianfb665a82022-06-15 03:57:21 +00001138 ssl->handshake->key_cert = key_cert;
Gilles Peskine449bd832023-01-11 14:50:10 +01001139 MBEDTLS_SSL_DEBUG_MSG(3,
1140 ("ssl_tls13_pick_key_cert:"
1141 "selected signature algorithm"
1142 " %s [%04x]",
1143 mbedtls_ssl_sig_alg_to_str(*sig_alg),
1144 *sig_alg));
XiaokangQianfb665a82022-06-15 03:57:21 +00001145 MBEDTLS_SSL_DEBUG_CRT(
Gilles Peskine449bd832023-01-11 14:50:10 +01001146 3, "selected certificate (chain)",
1147 ssl->handshake->key_cert->cert);
1148 return 0;
XiaokangQian81802f42022-06-10 13:25:22 +00001149 }
XiaokangQian81802f42022-06-10 13:25:22 +00001150 }
XiaokangQian23c5be62022-06-07 02:04:34 +00001151 }
1152
Gilles Peskine449bd832023-01-11 14:50:10 +01001153 MBEDTLS_SSL_DEBUG_MSG(2, ("ssl_tls13_pick_key_cert:"
1154 "no suitable certificate found"));
1155 return -1;
XiaokangQian23c5be62022-06-07 02:04:34 +00001156}
XiaokangQian81802f42022-06-10 13:25:22 +00001157#endif /* MBEDTLS_X509_CRT_PARSE_C &&
Ronald Cron928cbd32022-10-04 16:14:26 +02001158 MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED */
XiaokangQian23c5be62022-06-07 02:04:34 +00001159
XiaokangQian4080a7f2022-04-11 09:55:18 +00001160/*
XiaokangQianed582dd2022-04-13 08:21:05 +00001161 *
1162 * STATE HANDLING: ClientHello
1163 *
XiaokangQianb67384d2022-04-19 00:02:38 +00001164 * There are three possible classes of outcomes when parsing the ClientHello:
XiaokangQianed582dd2022-04-13 08:21:05 +00001165 *
XiaokangQianb67384d2022-04-19 00:02:38 +00001166 * 1) The ClientHello was well-formed and matched the server's configuration.
XiaokangQianed582dd2022-04-13 08:21:05 +00001167 *
1168 * In this case, the server progresses to sending its ServerHello.
1169 *
XiaokangQianb67384d2022-04-19 00:02:38 +00001170 * 2) The ClientHello was well-formed but didn't match the server's
1171 * configuration.
XiaokangQianed582dd2022-04-13 08:21:05 +00001172 *
1173 * For example, the client might not have offered a key share which
1174 * the server supports, or the server might require a cookie.
1175 *
1176 * In this case, the server sends a HelloRetryRequest.
1177 *
XiaokangQianb67384d2022-04-19 00:02:38 +00001178 * 3) The ClientHello was ill-formed
XiaokangQianed582dd2022-04-13 08:21:05 +00001179 *
1180 * In this case, we abort the handshake.
1181 *
1182 */
1183
1184/*
XiaokangQian4080a7f2022-04-11 09:55:18 +00001185 * Structure of this message:
1186 *
XiaokangQiane8ff3502022-04-22 02:34:40 +00001187 * uint16 ProtocolVersion;
1188 * opaque Random[32];
1189 * uint8 CipherSuite[2]; // Cryptographic suite selector
XiaokangQian4080a7f2022-04-11 09:55:18 +00001190 *
XiaokangQiane8ff3502022-04-22 02:34:40 +00001191 * struct {
1192 * ProtocolVersion legacy_version = 0x0303; // TLS v1.2
1193 * Random random;
1194 * opaque legacy_session_id<0..32>;
1195 * CipherSuite cipher_suites<2..2^16-2>;
1196 * opaque legacy_compression_methods<1..2^8-1>;
1197 * Extension extensions<8..2^16-1>;
1198 * } ClientHello;
XiaokangQian4080a7f2022-04-11 09:55:18 +00001199 */
XiaokangQianed582dd2022-04-13 08:21:05 +00001200
1201#define SSL_CLIENT_HELLO_OK 0
1202#define SSL_CLIENT_HELLO_HRR_REQUIRED 1
Ronald Cron5af4c7f2023-03-07 20:46:59 +01001203#define SSL_CLIENT_HELLO_TLS1_2 2
XiaokangQianed582dd2022-04-13 08:21:05 +00001204
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001205MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01001206static int ssl_tls13_parse_client_hello(mbedtls_ssl_context *ssl,
1207 const unsigned char *buf,
1208 const unsigned char *end)
XiaokangQian7807f9f2022-02-15 10:04:37 +00001209{
XiaokangQianb67384d2022-04-19 00:02:38 +00001210 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1211 const unsigned char *p = buf;
Ronald Crond540d992023-03-07 09:41:48 +01001212 const unsigned char *random;
XiaokangQian4080a7f2022-04-11 09:55:18 +00001213 size_t legacy_session_id_len;
Ronald Croncada4102023-03-07 09:51:39 +01001214 const unsigned char *legacy_session_id;
XiaokangQian318dc762022-04-20 09:43:51 +00001215 size_t cipher_suites_len;
Ronald Cron2f16b4e2023-03-07 10:07:32 +01001216 const unsigned char *cipher_suites;
XiaokangQian060d8672022-04-21 09:24:56 +00001217 const unsigned char *cipher_suites_end;
XiaokangQianb67384d2022-04-19 00:02:38 +00001218 size_t extensions_len;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001219 const unsigned char *extensions_end;
Ronald Croneff56732023-04-03 17:36:31 +02001220 const unsigned char *supported_versions_data;
1221 const unsigned char *supported_versions_data_end;
Jerry Yuc4bf5d62022-10-29 09:08:47 +08001222 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
Jerry Yu49ca9282022-05-05 11:05:22 +08001223 int hrr_required = 0;
Ronald Cron8a74f072023-06-14 17:59:29 +02001224 int no_usable_share_for_key_agreement = 0;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001225
Ronald Cron41a443a2022-10-04 16:38:25 +02001226#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED)
Ronald Cron79cdd412024-02-20 17:19:28 +01001227 int got_psk = 0;
1228 struct psk_attributes psk = PSK_ATTRIBUTES_INIT;
Jerry Yu29d9faa2022-08-23 17:52:45 +08001229 const unsigned char *pre_shared_key_ext = NULL;
Jerry Yu1c105562022-07-10 06:32:38 +00001230 const unsigned char *pre_shared_key_ext_end = NULL;
Ronald Cron41a443a2022-10-04 16:38:25 +02001231#endif
XiaokangQian7807f9f2022-02-15 10:04:37 +00001232
XiaokangQian7807f9f2022-02-15 10:04:37 +00001233 /*
XiaokangQianb67384d2022-04-19 00:02:38 +00001234 * ClientHello layout:
XiaokangQian7807f9f2022-02-15 10:04:37 +00001235 * 0 . 1 protocol version
XiaokangQiane8ff3502022-04-22 02:34:40 +00001236 * 2 . 33 random bytes
XiaokangQianc5763b52022-04-02 03:34:37 +00001237 * 34 . 34 session id length ( 1 byte )
XiaokangQian7807f9f2022-02-15 10:04:37 +00001238 * 35 . 34+x session id
XiaokangQian7807f9f2022-02-15 10:04:37 +00001239 * .. . .. ciphersuite list length ( 2 bytes )
1240 * .. . .. ciphersuite list
1241 * .. . .. compression alg. list length ( 1 byte )
1242 * .. . .. compression alg. list
1243 * .. . .. extensions length ( 2 bytes, optional )
1244 * .. . .. extensions ( optional )
1245 */
1246
XiaokangQianb67384d2022-04-19 00:02:38 +00001247 /*
bootstrap-prime6dbbf442022-05-17 19:30:44 -04001248 * Minimal length ( with everything empty and extensions omitted ) is
XiaokangQian7807f9f2022-02-15 10:04:37 +00001249 * 2 + 32 + 1 + 2 + 1 = 38 bytes. Check that first, so that we can
1250 * read at least up to session id length without worrying.
1251 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001252 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, 38);
XiaokangQian7807f9f2022-02-15 10:04:37 +00001253
1254 /* ...
1255 * ProtocolVersion legacy_version = 0x0303; // TLS 1.2
1256 * ...
1257 * with ProtocolVersion defined as:
1258 * uint16 ProtocolVersion;
1259 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001260 if (mbedtls_ssl_read_version(p, ssl->conf->transport) !=
1261 MBEDTLS_SSL_VERSION_TLS1_2) {
1262 MBEDTLS_SSL_DEBUG_MSG(1, ("Unsupported version of TLS."));
1263 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_PROTOCOL_VERSION,
1264 MBEDTLS_ERR_SSL_BAD_PROTOCOL_VERSION);
1265 return MBEDTLS_ERR_SSL_BAD_PROTOCOL_VERSION;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001266 }
1267 p += 2;
1268
Jerry Yuc1be19f2022-04-23 16:11:39 +08001269 /* ...
1270 * Random random;
1271 * ...
XiaokangQianb67384d2022-04-19 00:02:38 +00001272 * with Random defined as:
1273 * opaque Random[32];
XiaokangQian7807f9f2022-02-15 10:04:37 +00001274 */
Ronald Crond540d992023-03-07 09:41:48 +01001275 random = p;
XiaokangQian08037552022-04-20 07:16:41 +00001276 p += MBEDTLS_CLIENT_HELLO_RANDOM_LEN;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001277
Jerry Yuc1be19f2022-04-23 16:11:39 +08001278 /* ...
XiaokangQianb67384d2022-04-19 00:02:38 +00001279 * opaque legacy_session_id<0..32>;
Jerry Yuc1be19f2022-04-23 16:11:39 +08001280 * ...
XiaokangQian7807f9f2022-02-15 10:04:37 +00001281 */
Ronald Croncada4102023-03-07 09:51:39 +01001282 legacy_session_id_len = *(p++);
1283 legacy_session_id = p;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001284
XiaokangQianb67384d2022-04-19 00:02:38 +00001285 /*
1286 * Check we have enough data for the legacy session identifier
Jerry Yue95c8af2022-07-26 15:48:20 +08001287 * and the ciphersuite list length.
XiaokangQianb67384d2022-04-19 00:02:38 +00001288 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001289 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, legacy_session_id_len + 2);
XiaokangQian4080a7f2022-04-11 09:55:18 +00001290 p += legacy_session_id_len;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001291
Ronald Cron2f16b4e2023-03-07 10:07:32 +01001292 /* ...
1293 * CipherSuite cipher_suites<2..2^16-2>;
1294 * ...
1295 * with CipherSuite defined as:
1296 * uint8 CipherSuite[2];
1297 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001298 cipher_suites_len = MBEDTLS_GET_UINT16_BE(p, 0);
XiaokangQian7807f9f2022-02-15 10:04:37 +00001299 p += 2;
Ronald Cron2f16b4e2023-03-07 10:07:32 +01001300 cipher_suites = p;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001301
Ronald Cronfc7ae872023-02-16 15:32:19 +01001302 /*
1303 * The length of the ciphersuite list has to be even.
1304 */
1305 if (cipher_suites_len & 1) {
1306 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR,
1307 MBEDTLS_ERR_SSL_DECODE_ERROR);
1308 return MBEDTLS_ERR_SSL_DECODE_ERROR;
1309 }
1310
XiaokangQianb67384d2022-04-19 00:02:38 +00001311 /* Check we have enough data for the ciphersuite list, the legacy
1312 * compression methods and the length of the extensions.
Jerry Yue95c8af2022-07-26 15:48:20 +08001313 *
1314 * cipher_suites cipher_suites_len bytes
1315 * legacy_compression_methods 2 bytes
1316 * extensions_len 2 bytes
XiaokangQianb67384d2022-04-19 00:02:38 +00001317 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001318 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, cipher_suites_len + 2 + 2);
Ronald Cron8c527d02023-03-07 15:47:47 +01001319 p += cipher_suites_len;
1320 cipher_suites_end = p;
Jerry Yu5725f1c2022-08-21 17:27:16 +08001321
1322 /*
Ronald Cron8c527d02023-03-07 15:47:47 +01001323 * Search for the supported versions extension and parse it to determine
1324 * if the client supports TLS 1.3.
1325 */
1326 ret = mbedtls_ssl_tls13_is_supported_versions_ext_present_in_exts(
1327 ssl, p + 2, end,
Ronald Croneff56732023-04-03 17:36:31 +02001328 &supported_versions_data, &supported_versions_data_end);
Ronald Cron8c527d02023-03-07 15:47:47 +01001329 if (ret < 0) {
1330 MBEDTLS_SSL_DEBUG_RET(1,
1331 ("mbedtls_ssl_tls13_is_supported_versions_ext_present_in_exts"), ret);
1332 return ret;
1333 }
1334
1335 if (ret == 0) {
Ronald Cron5af4c7f2023-03-07 20:46:59 +01001336 return SSL_CLIENT_HELLO_TLS1_2;
Ronald Cron8c527d02023-03-07 15:47:47 +01001337 }
1338
Ronald Cron5af4c7f2023-03-07 20:46:59 +01001339 if (ret == 1) {
1340 ret = ssl_tls13_parse_supported_versions_ext(ssl,
Ronald Croneff56732023-04-03 17:36:31 +02001341 supported_versions_data,
1342 supported_versions_data_end);
Ronald Cron5af4c7f2023-03-07 20:46:59 +01001343 if (ret < 0) {
1344 MBEDTLS_SSL_DEBUG_RET(1,
1345 ("ssl_tls13_parse_supported_versions_ext"), ret);
1346 return ret;
1347 }
1348
Ronald Cronb828c7d2023-04-03 16:37:22 +02001349 /*
1350 * The supported versions extension was parsed successfully as the
1351 * value returned by ssl_tls13_parse_supported_versions_ext() is
1352 * positive. The return value is then equal to
1353 * MBEDTLS_SSL_VERSION_TLS1_2 or MBEDTLS_SSL_VERSION_TLS1_3, defining
1354 * the TLS version to negotiate.
1355 */
Ronald Cron5af4c7f2023-03-07 20:46:59 +01001356 if (MBEDTLS_SSL_VERSION_TLS1_2 == ret) {
1357 return SSL_CLIENT_HELLO_TLS1_2;
1358 }
Ronald Cron8c527d02023-03-07 15:47:47 +01001359 }
1360
1361 /*
1362 * We negotiate TLS 1.3.
Ronald Cron64582392023-03-07 09:21:40 +01001363 */
1364 ssl->tls_version = MBEDTLS_SSL_VERSION_TLS1_3;
Ronald Cron64582392023-03-07 09:21:40 +01001365 ssl->session_negotiate->tls_version = MBEDTLS_SSL_VERSION_TLS1_3;
1366 ssl->session_negotiate->endpoint = ssl->conf->endpoint;
Ronald Cron64582392023-03-07 09:21:40 +01001367
1368 /*
Ronald Crondad02b22023-04-06 09:57:52 +02001369 * We are negotiating the version 1.3 of the protocol. Do what we have
Ronald Croncada4102023-03-07 09:51:39 +01001370 * postponed: copy of the client random bytes, copy of the legacy session
Ronald Cron2f16b4e2023-03-07 10:07:32 +01001371 * identifier and selection of the TLS 1.3 cipher suite.
Ronald Crond540d992023-03-07 09:41:48 +01001372 */
1373 MBEDTLS_SSL_DEBUG_BUF(3, "client hello, random bytes",
1374 random, MBEDTLS_CLIENT_HELLO_RANDOM_LEN);
1375 memcpy(&handshake->randbytes[0], random, MBEDTLS_CLIENT_HELLO_RANDOM_LEN);
1376
Ronald Croncada4102023-03-07 09:51:39 +01001377 if (legacy_session_id_len > sizeof(ssl->session_negotiate->id)) {
1378 MBEDTLS_SSL_DEBUG_MSG(1, ("bad client hello message"));
1379 return MBEDTLS_ERR_SSL_DECODE_ERROR;
1380 }
1381 ssl->session_negotiate->id_len = legacy_session_id_len;
1382 MBEDTLS_SSL_DEBUG_BUF(3, "client hello, session id",
1383 legacy_session_id, legacy_session_id_len);
1384 memcpy(&ssl->session_negotiate->id[0],
1385 legacy_session_id, legacy_session_id_len);
1386
Ronald Crond540d992023-03-07 09:41:48 +01001387 /*
Jerry Yu5725f1c2022-08-21 17:27:16 +08001388 * Search for a matching ciphersuite
1389 */
Ronald Cron2f16b4e2023-03-07 10:07:32 +01001390 MBEDTLS_SSL_DEBUG_BUF(3, "client hello, list of cipher suites",
1391 cipher_suites, cipher_suites_len);
Jerry Yu5725f1c2022-08-21 17:27:16 +08001392
Ronald Cron89089cc2024-02-14 18:18:08 +01001393 ssl_tls13_select_ciphersuite(ssl, cipher_suites, cipher_suites_end,
1394 0, PSA_ALG_NONE, &handshake->ciphersuite_info);
Jerry Yu5725f1c2022-08-21 17:27:16 +08001395
Gilles Peskine449bd832023-01-11 14:50:10 +01001396 if (handshake->ciphersuite_info == NULL) {
1397 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE,
1398 MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE);
1399 return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
Jerry Yu5725f1c2022-08-21 17:27:16 +08001400 }
Ronald Cron89089cc2024-02-14 18:18:08 +01001401 ssl->session_negotiate->ciphersuite = handshake->ciphersuite_info->id;
1402
1403 MBEDTLS_SSL_DEBUG_MSG(2, ("selected ciphersuite: %04x - %s",
1404 ((unsigned) handshake->ciphersuite_info->id),
1405 handshake->ciphersuite_info->name));
Jerry Yuc1be19f2022-04-23 16:11:39 +08001406
XiaokangQian4080a7f2022-04-11 09:55:18 +00001407 /* ...
XiaokangQianb67384d2022-04-19 00:02:38 +00001408 * opaque legacy_compression_methods<1..2^8-1>;
XiaokangQian4080a7f2022-04-11 09:55:18 +00001409 * ...
XiaokangQian7807f9f2022-02-15 10:04:37 +00001410 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001411 if (p[0] != 1 || p[1] != MBEDTLS_SSL_COMPRESS_NULL) {
1412 MBEDTLS_SSL_DEBUG_MSG(1, ("bad legacy compression method"));
1413 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER,
1414 MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER);
1415 return MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001416 }
XiaokangQianb67384d2022-04-19 00:02:38 +00001417 p += 2;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001418
Jerry Yuc1be19f2022-04-23 16:11:39 +08001419 /* ...
XiaokangQianb67384d2022-04-19 00:02:38 +00001420 * Extension extensions<8..2^16-1>;
Jerry Yuc1be19f2022-04-23 16:11:39 +08001421 * ...
XiaokangQianb67384d2022-04-19 00:02:38 +00001422 * with Extension defined as:
1423 * struct {
1424 * ExtensionType extension_type;
1425 * opaque extension_data<0..2^16-1>;
1426 * } Extension;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001427 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001428 extensions_len = MBEDTLS_GET_UINT16_BE(p, 0);
XiaokangQian7807f9f2022-02-15 10:04:37 +00001429 p += 2;
Gilles Peskine449bd832023-01-11 14:50:10 +01001430 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, extensions_len);
XiaokangQiane8ff3502022-04-22 02:34:40 +00001431 extensions_end = p + extensions_len;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001432
Gilles Peskine449bd832023-01-11 14:50:10 +01001433 MBEDTLS_SSL_DEBUG_BUF(3, "client hello extensions", p, extensions_len);
Jerry Yu63a459c2022-10-31 13:38:40 +08001434 handshake->received_extensions = MBEDTLS_SSL_EXT_MASK_NONE;
Jerry Yu0c354a22022-08-29 15:25:36 +08001435
Gilles Peskine449bd832023-01-11 14:50:10 +01001436 while (p < extensions_end) {
XiaokangQian7807f9f2022-02-15 10:04:37 +00001437 unsigned int extension_type;
1438 size_t extension_data_len;
1439 const unsigned char *extension_data_end;
Jerry Yu263dbf72022-10-26 10:51:27 +08001440 uint32_t allowed_exts = MBEDTLS_SSL_TLS1_3_ALLOWED_EXTS_OF_CH;
1441
Ronald Cron5fbd2702024-02-14 10:03:36 +01001442 if (ssl->handshake->hello_retry_request_flag) {
Jerry Yu263dbf72022-10-26 10:51:27 +08001443 /* Do not accept early data extension in 2nd ClientHello */
1444 allowed_exts &= ~MBEDTLS_SSL_EXT_MASK(EARLY_DATA);
1445 }
XiaokangQian7807f9f2022-02-15 10:04:37 +00001446
Jerry Yu0c354a22022-08-29 15:25:36 +08001447 /* RFC 8446, section 4.2.11
Jerry Yu1c9247c2022-07-21 12:37:39 +08001448 *
1449 * The "pre_shared_key" extension MUST be the last extension in the
1450 * ClientHello (this facilitates implementation as described below).
1451 * Servers MUST check that it is the last extension and otherwise fail
1452 * the handshake with an "illegal_parameter" alert.
1453 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001454 if (handshake->received_extensions & MBEDTLS_SSL_EXT_MASK(PRE_SHARED_KEY)) {
Jerry Yu1c9247c2022-07-21 12:37:39 +08001455 MBEDTLS_SSL_DEBUG_MSG(
Gilles Peskine449bd832023-01-11 14:50:10 +01001456 3, ("pre_shared_key is not last extension."));
Jerry Yu1c9247c2022-07-21 12:37:39 +08001457 MBEDTLS_SSL_PEND_FATAL_ALERT(
1458 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER,
Gilles Peskine449bd832023-01-11 14:50:10 +01001459 MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER);
1460 return MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER;
Jerry Yu1c9247c2022-07-21 12:37:39 +08001461 }
1462
Gilles Peskine449bd832023-01-11 14:50:10 +01001463 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, extensions_end, 4);
1464 extension_type = MBEDTLS_GET_UINT16_BE(p, 0);
1465 extension_data_len = MBEDTLS_GET_UINT16_BE(p, 2);
XiaokangQian7807f9f2022-02-15 10:04:37 +00001466 p += 4;
1467
Gilles Peskine449bd832023-01-11 14:50:10 +01001468 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, extensions_end, extension_data_len);
XiaokangQian7807f9f2022-02-15 10:04:37 +00001469 extension_data_end = p + extension_data_len;
1470
Jerry Yuc4bf5d62022-10-29 09:08:47 +08001471 ret = mbedtls_ssl_tls13_check_received_extension(
Gilles Peskine449bd832023-01-11 14:50:10 +01001472 ssl, MBEDTLS_SSL_HS_CLIENT_HELLO, extension_type,
Jerry Yu263dbf72022-10-26 10:51:27 +08001473 allowed_exts);
Gilles Peskine449bd832023-01-11 14:50:10 +01001474 if (ret != 0) {
1475 return ret;
1476 }
Jerry Yue18dc7e2022-08-04 16:29:22 +08001477
Gilles Peskine449bd832023-01-11 14:50:10 +01001478 switch (extension_type) {
XiaokangQian40a35232022-05-07 09:02:40 +00001479#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
1480 case MBEDTLS_TLS_EXT_SERVERNAME:
Gilles Peskine449bd832023-01-11 14:50:10 +01001481 MBEDTLS_SSL_DEBUG_MSG(3, ("found ServerName extension"));
1482 ret = mbedtls_ssl_parse_server_name_ext(ssl, p,
1483 extension_data_end);
1484 if (ret != 0) {
XiaokangQian40a35232022-05-07 09:02:40 +00001485 MBEDTLS_SSL_DEBUG_RET(
Gilles Peskine449bd832023-01-11 14:50:10 +01001486 1, "mbedtls_ssl_parse_servername_ext", ret);
1487 return ret;
XiaokangQian40a35232022-05-07 09:02:40 +00001488 }
XiaokangQian40a35232022-05-07 09:02:40 +00001489 break;
1490#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
1491
Przemek Stekiel8c0a9532023-06-15 16:48:19 +02001492#if defined(PSA_WANT_ALG_ECDH) || defined(PSA_WANT_ALG_FFDH)
XiaokangQian7807f9f2022-02-15 10:04:37 +00001493 case MBEDTLS_TLS_EXT_SUPPORTED_GROUPS:
Gilles Peskine449bd832023-01-11 14:50:10 +01001494 MBEDTLS_SSL_DEBUG_MSG(3, ("found supported group extension"));
XiaokangQian7807f9f2022-02-15 10:04:37 +00001495
1496 /* Supported Groups Extension
1497 *
1498 * When sent by the client, the "supported_groups" extension
1499 * indicates the named groups which the client supports,
1500 * ordered from most preferred to least preferred.
1501 */
Jerry Yuc1be19f2022-04-23 16:11:39 +08001502 ret = ssl_tls13_parse_supported_groups_ext(
Gilles Peskine449bd832023-01-11 14:50:10 +01001503 ssl, p, extension_data_end);
1504 if (ret != 0) {
Xiaokang Qian9f1747b2023-03-30 02:50:04 +00001505 MBEDTLS_SSL_DEBUG_RET(
Xiaokang Qian8bce0e62023-04-04 10:15:48 +00001506 1, "ssl_tls13_parse_supported_groups_ext", ret);
Gilles Peskine449bd832023-01-11 14:50:10 +01001507 return ret;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001508 }
1509
XiaokangQian7807f9f2022-02-15 10:04:37 +00001510 break;
Przemek Stekiel8c0a9532023-06-15 16:48:19 +02001511#endif /* PSA_WANT_ALG_ECDH || PSA_WANT_ALG_FFDH*/
XiaokangQian7807f9f2022-02-15 10:04:37 +00001512
Valerio Settic9ae8622023-07-25 11:23:50 +02001513#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_EPHEMERAL_ENABLED)
XiaokangQian7807f9f2022-02-15 10:04:37 +00001514 case MBEDTLS_TLS_EXT_KEY_SHARE:
Gilles Peskine449bd832023-01-11 14:50:10 +01001515 MBEDTLS_SSL_DEBUG_MSG(3, ("found key share extension"));
XiaokangQian7807f9f2022-02-15 10:04:37 +00001516
1517 /*
1518 * Key Share Extension
1519 *
1520 * When sent by the client, the "key_share" extension
1521 * contains the endpoint's cryptographic parameters for
1522 * ECDHE/DHE key establishment methods.
1523 */
Jerry Yuc1be19f2022-04-23 16:11:39 +08001524 ret = ssl_tls13_parse_key_shares_ext(
Gilles Peskine449bd832023-01-11 14:50:10 +01001525 ssl, p, extension_data_end);
1526 if (ret == SSL_TLS1_3_PARSE_KEY_SHARES_EXT_NO_MATCH) {
Ronald Cron8a74f072023-06-14 17:59:29 +02001527 MBEDTLS_SSL_DEBUG_MSG(2, ("No usable share for key agreement."));
1528 no_usable_share_for_key_agreement = 1;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001529 }
1530
Gilles Peskine449bd832023-01-11 14:50:10 +01001531 if (ret < 0) {
Jerry Yu582dd062022-04-22 21:59:01 +08001532 MBEDTLS_SSL_DEBUG_RET(
Gilles Peskine449bd832023-01-11 14:50:10 +01001533 1, "ssl_tls13_parse_key_shares_ext", ret);
1534 return ret;
Jerry Yu582dd062022-04-22 21:59:01 +08001535 }
XiaokangQian7807f9f2022-02-15 10:04:37 +00001536
XiaokangQian7807f9f2022-02-15 10:04:37 +00001537 break;
Valerio Settic9ae8622023-07-25 11:23:50 +02001538#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_EPHEMERAL_ENABLED */
XiaokangQian7807f9f2022-02-15 10:04:37 +00001539
1540 case MBEDTLS_TLS_EXT_SUPPORTED_VERSIONS:
Ronald Cron8c527d02023-03-07 15:47:47 +01001541 /* Already parsed */
XiaokangQian7807f9f2022-02-15 10:04:37 +00001542 break;
1543
Ronald Cron41a443a2022-10-04 16:38:25 +02001544#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED)
Jerry Yue19e3b92022-07-08 12:04:51 +00001545 case MBEDTLS_TLS_EXT_PSK_KEY_EXCHANGE_MODES:
Xiaokang Qian9f1747b2023-03-30 02:50:04 +00001546 MBEDTLS_SSL_DEBUG_MSG(
1547 3, ("found psk key exchange modes extension"));
Jerry Yue19e3b92022-07-08 12:04:51 +00001548
1549 ret = ssl_tls13_parse_key_exchange_modes_ext(
Gilles Peskine449bd832023-01-11 14:50:10 +01001550 ssl, p, extension_data_end);
1551 if (ret != 0) {
Jerry Yue19e3b92022-07-08 12:04:51 +00001552 MBEDTLS_SSL_DEBUG_RET(
Gilles Peskine449bd832023-01-11 14:50:10 +01001553 1, "ssl_tls13_parse_key_exchange_modes_ext", ret);
1554 return ret;
Jerry Yue19e3b92022-07-08 12:04:51 +00001555 }
1556
Jerry Yue19e3b92022-07-08 12:04:51 +00001557 break;
Ronald Cron41a443a2022-10-04 16:38:25 +02001558#endif
Jerry Yue19e3b92022-07-08 12:04:51 +00001559
Jerry Yu1c105562022-07-10 06:32:38 +00001560 case MBEDTLS_TLS_EXT_PRE_SHARED_KEY:
Gilles Peskine449bd832023-01-11 14:50:10 +01001561 MBEDTLS_SSL_DEBUG_MSG(3, ("found pre_shared_key extension"));
1562 if ((handshake->received_extensions &
1563 MBEDTLS_SSL_EXT_MASK(PSK_KEY_EXCHANGE_MODES)) == 0) {
Jerry Yu13ab81d2022-07-22 23:17:11 +08001564 MBEDTLS_SSL_PEND_FATAL_ALERT(
1565 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER,
Gilles Peskine449bd832023-01-11 14:50:10 +01001566 MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER);
1567 return MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER;
Jerry Yu13ab81d2022-07-22 23:17:11 +08001568 }
Ronald Cron41a443a2022-10-04 16:38:25 +02001569#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED)
Jerry Yu1c105562022-07-10 06:32:38 +00001570 /* Delay processing of the PSK identity once we have
1571 * found out which algorithms to use. We keep a pointer
1572 * to the buffer and the size for later processing.
1573 */
Jerry Yu29d9faa2022-08-23 17:52:45 +08001574 pre_shared_key_ext = p;
Jerry Yu1c105562022-07-10 06:32:38 +00001575 pre_shared_key_ext_end = extension_data_end;
Jerry Yue18dc7e2022-08-04 16:29:22 +08001576#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED */
Jerry Yu1c105562022-07-10 06:32:38 +00001577 break;
Jerry Yu1c105562022-07-10 06:32:38 +00001578
XiaokangQianacb39922022-06-17 10:18:48 +00001579#if defined(MBEDTLS_SSL_ALPN)
1580 case MBEDTLS_TLS_EXT_ALPN:
Gilles Peskine449bd832023-01-11 14:50:10 +01001581 MBEDTLS_SSL_DEBUG_MSG(3, ("found alpn extension"));
XiaokangQianacb39922022-06-17 10:18:48 +00001582
Gilles Peskine449bd832023-01-11 14:50:10 +01001583 ret = mbedtls_ssl_parse_alpn_ext(ssl, p, extension_data_end);
1584 if (ret != 0) {
XiaokangQianacb39922022-06-17 10:18:48 +00001585 MBEDTLS_SSL_DEBUG_RET(
Gilles Peskine449bd832023-01-11 14:50:10 +01001586 1, ("mbedtls_ssl_parse_alpn_ext"), ret);
1587 return ret;
XiaokangQianacb39922022-06-17 10:18:48 +00001588 }
XiaokangQianacb39922022-06-17 10:18:48 +00001589 break;
1590#endif /* MBEDTLS_SSL_ALPN */
1591
Ronald Cron928cbd32022-10-04 16:14:26 +02001592#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED)
XiaokangQian7807f9f2022-02-15 10:04:37 +00001593 case MBEDTLS_TLS_EXT_SIG_ALG:
Gilles Peskine449bd832023-01-11 14:50:10 +01001594 MBEDTLS_SSL_DEBUG_MSG(3, ("found signature_algorithms extension"));
XiaokangQian7807f9f2022-02-15 10:04:37 +00001595
Gabor Mezei078e8032022-04-27 21:17:56 +02001596 ret = mbedtls_ssl_parse_sig_alg_ext(
Gilles Peskine449bd832023-01-11 14:50:10 +01001597 ssl, p, extension_data_end);
1598 if (ret != 0) {
Xiaokang Qian49f39c12023-04-06 02:31:02 +00001599 MBEDTLS_SSL_DEBUG_RET(
1600 1, "mbedtls_ssl_parse_sig_alg_ext", ret);
Gilles Peskine449bd832023-01-11 14:50:10 +01001601 return ret;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001602 }
XiaokangQian7807f9f2022-02-15 10:04:37 +00001603 break;
Ronald Cron928cbd32022-10-04 16:14:26 +02001604#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED */
XiaokangQian7807f9f2022-02-15 10:04:37 +00001605
Jan Bruckner151f6422023-02-10 12:45:19 +01001606#if defined(MBEDTLS_SSL_RECORD_SIZE_LIMIT)
1607 case MBEDTLS_TLS_EXT_RECORD_SIZE_LIMIT:
1608 MBEDTLS_SSL_DEBUG_MSG(3, ("found record_size_limit extension"));
1609
Xiaokang Qian9f1747b2023-03-30 02:50:04 +00001610 ret = mbedtls_ssl_tls13_parse_record_size_limit_ext(
1611 ssl, p, extension_data_end);
Jan Brucknerf482dcc2023-03-15 09:09:06 +01001612 if (ret != 0) {
1613 MBEDTLS_SSL_DEBUG_RET(
1614 1, ("mbedtls_ssl_tls13_parse_record_size_limit_ext"), ret);
1615 return ret;
1616 }
Jan Bruckner151f6422023-02-10 12:45:19 +01001617 break;
1618#endif /* MBEDTLS_SSL_RECORD_SIZE_LIMIT */
1619
XiaokangQian7807f9f2022-02-15 10:04:37 +00001620 default:
Jerry Yu79aa7212022-11-08 21:30:21 +08001621 MBEDTLS_SSL_PRINT_EXT(
Jerry Yu63a459c2022-10-31 13:38:40 +08001622 3, MBEDTLS_SSL_HS_CLIENT_HELLO,
Gilles Peskine449bd832023-01-11 14:50:10 +01001623 extension_type, "( ignored )");
Jerry Yu0c354a22022-08-29 15:25:36 +08001624 break;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001625 }
1626
1627 p += extension_data_len;
1628 }
1629
Gilles Peskine449bd832023-01-11 14:50:10 +01001630 MBEDTLS_SSL_PRINT_EXTS(3, MBEDTLS_SSL_HS_CLIENT_HELLO,
1631 handshake->received_extensions);
Jerry Yue18dc7e2022-08-04 16:29:22 +08001632
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +01001633 ret = mbedtls_ssl_add_hs_hdr_to_checksum(ssl,
1634 MBEDTLS_SSL_HS_CLIENT_HELLO,
1635 p - buf);
1636 if (0 != ret) {
1637 MBEDTLS_SSL_DEBUG_RET(1, ("mbedtls_ssl_add_hs_hdr_to_checksum"), ret);
1638 return ret;
1639 }
Jerry Yu032b15ce2022-07-11 06:10:03 +00001640
Ronald Cron41a443a2022-10-04 16:38:25 +02001641#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED)
XiaokangQian7807f9f2022-02-15 10:04:37 +00001642 /* Update checksum with either
1643 * - The entire content of the CH message, if no PSK extension is present
1644 * - The content up to but excluding the PSK extension, if present.
Ronald Cron12e72f12024-02-14 15:49:38 +01001645 * Always parse the pre-shared key extension when present in the
1646 * ClientHello even if some pre-requisites for PSK key exchange modes are
1647 * not met. That way we always validate the syntax of the extension.
XiaokangQian7807f9f2022-02-15 10:04:37 +00001648 */
Ronald Cron12e72f12024-02-14 15:49:38 +01001649 if (handshake->received_extensions & MBEDTLS_SSL_EXT_MASK(PRE_SHARED_KEY)) {
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +01001650 ret = handshake->update_checksum(ssl, buf,
1651 pre_shared_key_ext - buf);
1652 if (0 != ret) {
1653 MBEDTLS_SSL_DEBUG_RET(1, ("update_checksum"), ret);
1654 return ret;
1655 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001656 ret = ssl_tls13_parse_pre_shared_key_ext(ssl,
1657 pre_shared_key_ext,
1658 pre_shared_key_ext_end,
1659 cipher_suites,
Ronald Cron79cdd412024-02-20 17:19:28 +01001660 cipher_suites_end,
1661 &psk);
1662 if (ret == 0) {
1663 got_psk = 1;
1664 } else if (ret != MBEDTLS_ERR_SSL_UNKNOWN_IDENTITY) {
Jerry Yu63a459c2022-10-31 13:38:40 +08001665 MBEDTLS_SSL_DEBUG_RET(
Gilles Peskine449bd832023-01-11 14:50:10 +01001666 1, "ssl_tls13_parse_pre_shared_key_ext", ret);
1667 return ret;
Jerry Yu1c105562022-07-10 06:32:38 +00001668 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001669 } else
Ronald Cron41a443a2022-10-04 16:38:25 +02001670#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED */
Jerry Yu1c105562022-07-10 06:32:38 +00001671 {
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +01001672 ret = handshake->update_checksum(ssl, buf, p - buf);
1673 if (0 != ret) {
1674 MBEDTLS_SSL_DEBUG_RET(1, ("update_checksum"), ret);
1675 return ret;
1676 }
Jerry Yu1c105562022-07-10 06:32:38 +00001677 }
XiaokangQian7807f9f2022-02-15 10:04:37 +00001678
Ronald Cron79cdd412024-02-20 17:19:28 +01001679 /*
1680 * Determine the key exchange algorithm to use.
1681 * There are three types of key exchanges supported in TLS 1.3:
1682 * - (EC)DH with ECDSA,
1683 * - (EC)DH with PSK,
1684 * - plain PSK.
1685 *
1686 * The PSK-based key exchanges may additionally be used with 0-RTT.
1687 *
1688 * Our built-in order of preference is
1689 * 1 ) (EC)DHE-PSK Mode ( psk_ephemeral )
1690 * 2 ) Certificate Mode ( ephemeral )
1691 * 3 ) Plain PSK Mode ( psk )
1692 */
1693#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED)
1694 if (got_psk && (psk.key_exchange_mode ==
1695 MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK_EPHEMERAL)) {
1696 handshake->key_exchange_mode =
1697 MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK_EPHEMERAL;
1698 MBEDTLS_SSL_DEBUG_MSG(2, ("key exchange mode: psk_ephemeral"));
1699
1700 } else
1701#endif
1702 if (ssl_tls13_key_exchange_is_ephemeral_available(ssl)) {
1703 handshake->key_exchange_mode =
1704 MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL;
1705 MBEDTLS_SSL_DEBUG_MSG(2, ("key exchange mode: ephemeral"));
1706
1707 }
1708#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED)
1709 else if (got_psk && (psk.key_exchange_mode ==
1710 MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK)) {
1711 handshake->key_exchange_mode = MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK;
1712 MBEDTLS_SSL_DEBUG_MSG(2, ("key exchange mode: psk"));
1713 }
1714#endif
1715 else {
1716 MBEDTLS_SSL_DEBUG_MSG(
1717 1,
1718 ("ClientHello message misses mandatory extensions."));
1719 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_MISSING_EXTENSION,
1720 MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER);
1721 return MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER;
Gilles Peskine449bd832023-01-11 14:50:10 +01001722 }
XiaokangQian7807f9f2022-02-15 10:04:37 +00001723
Ronald Cron79cdd412024-02-20 17:19:28 +01001724 if (handshake->key_exchange_mode ==
1725 MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL) {
1726 handshake->resume = 0;
1727 }
1728
1729 if (handshake->key_exchange_mode !=
Ronald Cron8a74f072023-06-14 17:59:29 +02001730 MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK) {
1731 hrr_required = (no_usable_share_for_key_agreement != 0);
1732 }
1733
Gilles Peskine449bd832023-01-11 14:50:10 +01001734 mbedtls_ssl_optimize_checksum(ssl, handshake->ciphersuite_info);
Jerry Yue5834fd2022-08-29 20:16:09 +08001735
Gilles Peskine449bd832023-01-11 14:50:10 +01001736 return hrr_required ? SSL_CLIENT_HELLO_HRR_REQUIRED : SSL_CLIENT_HELLO_OK;
XiaokangQian75fe8c72022-06-15 09:42:45 +00001737}
1738
Jerry Yuab0da372023-02-08 13:55:24 +08001739#if defined(MBEDTLS_SSL_EARLY_DATA)
Jerry Yu4caf3ca2023-11-15 16:13:47 +08001740static int ssl_tls13_check_early_data_requirements(mbedtls_ssl_context *ssl)
Jerry Yuab0da372023-02-08 13:55:24 +08001741{
1742 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
1743
Jerry Yu985c9672022-12-04 14:06:30 +08001744 if (ssl->conf->early_data_enabled == MBEDTLS_SSL_EARLY_DATA_DISABLED) {
1745 MBEDTLS_SSL_DEBUG_MSG(
Jerry Yu985c9672022-12-04 14:06:30 +08001746 1,
Jerry Yu454dda32023-10-31 15:13:54 +08001747 ("EarlyData: rejected, feature disabled in server configuration."));
Jerry Yu4caf3ca2023-11-15 16:13:47 +08001748 return -1;
Ronald Cron7b6ee942024-01-12 10:29:55 +01001749 }
1750
Jerry Yu454dda32023-10-31 15:13:54 +08001751 if (!handshake->resume) {
1752 /* We currently support early data only in the case of PSKs established
1753 via a NewSessionTicket message thus in the case of a session
1754 resumption. */
Jerry Yu985c9672022-12-04 14:06:30 +08001755 MBEDTLS_SSL_DEBUG_MSG(
Jerry Yu7ef9fd82023-11-07 14:30:38 +08001756 1, ("EarlyData: rejected, not a session resumption."));
Jerry Yu4caf3ca2023-11-15 16:13:47 +08001757 return -1;
Jerry Yu985c9672022-12-04 14:06:30 +08001758 }
1759
Jerry Yu82fd6c12023-10-31 16:32:19 +08001760 /* RFC 8446 4.2.10
1761 *
1762 * In order to accept early data, the server MUST have accepted a PSK cipher
1763 * suite and selected the first key offered in the client's "pre_shared_key"
1764 * extension. In addition, it MUST verify that the following values are the
1765 * same as those associated with the selected PSK:
1766 * - The TLS version number
1767 * - The selected cipher suite
1768 * - The selected ALPN [RFC7301] protocol, if any
1769 *
1770 * NOTE:
Jerry Yu7ef9fd82023-11-07 14:30:38 +08001771 * - The TLS version number is checked in
1772 * ssl_tls13_offered_psks_check_identity_match_ticket().
1773 * - ALPN is not checked for the time being (TODO).
Jerry Yu82fd6c12023-10-31 16:32:19 +08001774 */
1775
1776 if (handshake->selected_identity != 0) {
1777 MBEDTLS_SSL_DEBUG_MSG(
Jerry Yu7ef9fd82023-11-07 14:30:38 +08001778 1, ("EarlyData: rejected, the selected key in "
1779 "`pre_shared_key` is not the first one."));
Jerry Yu4caf3ca2023-11-15 16:13:47 +08001780 return -1;
Jerry Yu82fd6c12023-10-31 16:32:19 +08001781 }
1782
1783 if (handshake->ciphersuite_info->id !=
1784 ssl->session_negotiate->ciphersuite) {
1785 MBEDTLS_SSL_DEBUG_MSG(
Jerry Yu7ef9fd82023-11-07 14:30:38 +08001786 1, ("EarlyData: rejected, the selected ciphersuite is not the one "
1787 "of the selected pre-shared key."));
Jerry Yu4caf3ca2023-11-15 16:13:47 +08001788 return -1;
Jerry Yu82fd6c12023-10-31 16:32:19 +08001789
1790 }
Jerry Yu985c9672022-12-04 14:06:30 +08001791
Pengyu Lv94a42cc2023-12-06 10:04:17 +08001792 if (!mbedtls_ssl_tls13_session_ticket_allow_early_data(ssl->session_negotiate)) {
Jerry Yufceddb32022-12-12 15:30:34 +08001793 MBEDTLS_SSL_DEBUG_MSG(
1794 1,
Jerry Yuea96ac32023-11-21 17:06:36 +08001795 ("EarlyData: rejected, early_data not allowed in ticket "
1796 "permission bits."));
Jerry Yu4caf3ca2023-11-15 16:13:47 +08001797 return -1;
Jerry Yufceddb32022-12-12 15:30:34 +08001798 }
Jerry Yu985c9672022-12-04 14:06:30 +08001799
Jerry Yu4caf3ca2023-11-15 16:13:47 +08001800 return 0;
Jerry Yuab0da372023-02-08 13:55:24 +08001801}
1802#endif /* MBEDTLS_SSL_EARLY_DATA */
1803
XiaokangQian75fe8c72022-06-15 09:42:45 +00001804/* Update the handshake state machine */
1805
Ronald Cronce7d76e2022-07-08 18:56:49 +02001806MBEDTLS_CHECK_RETURN_CRITICAL
Ronald Cron7b6ee942024-01-12 10:29:55 +01001807static int ssl_tls13_postprocess_client_hello(mbedtls_ssl_context *ssl,
1808 int hrr_required)
XiaokangQian75fe8c72022-06-15 09:42:45 +00001809{
1810 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1811
XiaokangQian7807f9f2022-02-15 10:04:37 +00001812 /*
XiaokangQianfb665a82022-06-15 03:57:21 +00001813 * Server certificate selection
1814 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001815 if (ssl->conf->f_cert_cb && (ret = ssl->conf->f_cert_cb(ssl)) != 0) {
1816 MBEDTLS_SSL_DEBUG_RET(1, "f_cert_cb", ret);
1817 return ret;
XiaokangQianfb665a82022-06-15 03:57:21 +00001818 }
1819#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
1820 ssl->handshake->sni_name = NULL;
1821 ssl->handshake->sni_name_len = 0;
1822#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
XiaokangQian7807f9f2022-02-15 10:04:37 +00001823
Gilles Peskine449bd832023-01-11 14:50:10 +01001824 ret = mbedtls_ssl_tls13_key_schedule_stage_early(ssl);
1825 if (ret != 0) {
1826 MBEDTLS_SSL_DEBUG_RET(1,
1827 "mbedtls_ssl_tls1_3_key_schedule_stage_early", ret);
1828 return ret;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001829 }
1830
Jerry Yuab0da372023-02-08 13:55:24 +08001831#if defined(MBEDTLS_SSL_EARLY_DATA)
Jerry Yu4caf3ca2023-11-15 16:13:47 +08001832 if (ssl->handshake->received_extensions & MBEDTLS_SSL_EXT_MASK(EARLY_DATA)) {
Ronald Cron31e2d832024-02-05 16:45:57 +01001833 ssl->handshake->early_data_accepted =
1834 (!hrr_required) && (ssl_tls13_check_early_data_requirements(ssl) == 0);
Jerry Yu4e9b70e2022-12-04 14:08:02 +08001835
Jerry Yu4caf3ca2023-11-15 16:13:47 +08001836 if (ssl->handshake->early_data_accepted) {
1837 ret = mbedtls_ssl_tls13_compute_early_transform(ssl);
1838 if (ret != 0) {
1839 MBEDTLS_SSL_DEBUG_RET(
1840 1, "mbedtls_ssl_tls13_compute_early_transform", ret);
1841 return ret;
1842 }
1843 } else {
1844 ssl->discard_early_data_record =
1845 hrr_required ?
1846 MBEDTLS_SSL_EARLY_DATA_DISCARD :
1847 MBEDTLS_SSL_EARLY_DATA_TRY_TO_DEPROTECT_AND_DISCARD;
Jerry Yu4e9b70e2022-12-04 14:08:02 +08001848 }
1849 }
Ronald Cron7b6ee942024-01-12 10:29:55 +01001850#else
1851 ((void) hrr_required);
Jerry Yuab0da372023-02-08 13:55:24 +08001852#endif /* MBEDTLS_SSL_EARLY_DATA */
1853
Gilles Peskine449bd832023-01-11 14:50:10 +01001854 return 0;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001855}
1856
1857/*
XiaokangQianed582dd2022-04-13 08:21:05 +00001858 * Main entry point from the state machine; orchestrates the otherfunctions.
1859 */
1860
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001861MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01001862static int ssl_tls13_process_client_hello(mbedtls_ssl_context *ssl)
XiaokangQianed582dd2022-04-13 08:21:05 +00001863{
1864
XiaokangQian08037552022-04-20 07:16:41 +00001865 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine449bd832023-01-11 14:50:10 +01001866 unsigned char *buf = NULL;
XiaokangQianed582dd2022-04-13 08:21:05 +00001867 size_t buflen = 0;
Jerry Yu4ca91402022-05-09 15:50:57 +08001868 int parse_client_hello_ret;
1869
Gilles Peskine449bd832023-01-11 14:50:10 +01001870 MBEDTLS_SSL_DEBUG_MSG(2, ("=> parse client hello"));
XiaokangQianed582dd2022-04-13 08:21:05 +00001871
Gilles Peskine449bd832023-01-11 14:50:10 +01001872 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_tls13_fetch_handshake_msg(
1873 ssl, MBEDTLS_SSL_HS_CLIENT_HELLO,
1874 &buf, &buflen));
XiaokangQianed582dd2022-04-13 08:21:05 +00001875
Gilles Peskine449bd832023-01-11 14:50:10 +01001876 MBEDTLS_SSL_PROC_CHK_NEG(ssl_tls13_parse_client_hello(ssl, buf,
1877 buf + buflen));
Ronald Cron5af4c7f2023-03-07 20:46:59 +01001878 parse_client_hello_ret = ret; /* Store positive return value of
1879 * parse_client_hello,
1880 * as negative error codes are handled
Jerry Yuf41553b2022-05-09 22:20:30 +08001881 * by MBEDTLS_SSL_PROC_CHK_NEG. */
Jerry Yu4ca91402022-05-09 15:50:57 +08001882
Ronald Cronb828c7d2023-04-03 16:37:22 +02001883 /*
Yanray Wang90acdc62023-12-08 10:29:42 +08001884 * Version 1.2 of the protocol has to be used for the handshake.
1885 * If TLS 1.2 is not supported, abort the handshake. Otherwise, set the
Ronald Cronb828c7d2023-04-03 16:37:22 +02001886 * ssl->keep_current_message flag for the ClientHello to be kept and parsed
1887 * as a TLS 1.2 ClientHello. We also change ssl->tls_version to
1888 * MBEDTLS_SSL_VERSION_TLS1_2 thus from now on mbedtls_ssl_handshake_step()
1889 * will dispatch to the TLS 1.2 state machine.
1890 */
Ronald Cron5af4c7f2023-03-07 20:46:59 +01001891 if (SSL_CLIENT_HELLO_TLS1_2 == parse_client_hello_ret) {
Yanray Wangfb0f47b2023-12-04 15:27:28 +08001892 /* Check if server supports TLS 1.2 */
Yanray Wang408ba6f2023-12-08 10:18:03 +08001893 if (!mbedtls_ssl_conf_is_tls12_enabled(ssl->conf)) {
Yanray Wangfb0f47b2023-12-04 15:27:28 +08001894 MBEDTLS_SSL_DEBUG_MSG(
Yanray Wang177e49a2023-12-08 10:51:04 +08001895 1, ("TLS 1.2 not supported."));
Yanray Wangfb0f47b2023-12-04 15:27:28 +08001896 MBEDTLS_SSL_PEND_FATAL_ALERT(
Yanray Wang2bef9172023-12-08 10:21:53 +08001897 MBEDTLS_SSL_ALERT_MSG_PROTOCOL_VERSION,
1898 MBEDTLS_ERR_SSL_BAD_PROTOCOL_VERSION);
1899 return MBEDTLS_ERR_SSL_BAD_PROTOCOL_VERSION;
Yanray Wangfb0f47b2023-12-04 15:27:28 +08001900 }
Ronald Cron5af4c7f2023-03-07 20:46:59 +01001901 ssl->keep_current_message = 1;
1902 ssl->tls_version = MBEDTLS_SSL_VERSION_TLS1_2;
1903 return 0;
1904 }
1905
Ronald Cron7b6ee942024-01-12 10:29:55 +01001906 MBEDTLS_SSL_PROC_CHK(
1907 ssl_tls13_postprocess_client_hello(ssl, parse_client_hello_ret ==
1908 SSL_CLIENT_HELLO_HRR_REQUIRED));
Jerry Yu582dd062022-04-22 21:59:01 +08001909
Ronald Cron5af4c7f2023-03-07 20:46:59 +01001910 if (SSL_CLIENT_HELLO_OK == parse_client_hello_ret) {
Gilles Peskine449bd832023-01-11 14:50:10 +01001911 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_SERVER_HELLO);
1912 } else {
1913 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_HELLO_RETRY_REQUEST);
1914 }
XiaokangQianed582dd2022-04-13 08:21:05 +00001915
1916cleanup:
1917
Gilles Peskine449bd832023-01-11 14:50:10 +01001918 MBEDTLS_SSL_DEBUG_MSG(2, ("<= parse client hello"));
1919 return ret;
XiaokangQianed582dd2022-04-13 08:21:05 +00001920}
1921
1922/*
Jerry Yu1c3e6882022-04-20 21:23:40 +08001923 * Handler for MBEDTLS_SSL_SERVER_HELLO
Jerry Yu5b64ae92022-03-30 17:15:02 +08001924 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001925MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01001926static int ssl_tls13_prepare_server_hello(mbedtls_ssl_context *ssl)
Jerry Yuf4b27e42022-03-30 17:32:21 +08001927{
Jerry Yu637a3f12022-04-20 21:37:58 +08001928 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1929 unsigned char *server_randbytes =
Gilles Peskine449bd832023-01-11 14:50:10 +01001930 ssl->handshake->randbytes + MBEDTLS_CLIENT_HELLO_RANDOM_LEN;
1931 if (ssl->conf->f_rng == NULL) {
1932 MBEDTLS_SSL_DEBUG_MSG(1, ("no RNG provided"));
1933 return MBEDTLS_ERR_SSL_NO_RNG;
Jerry Yuf4b27e42022-03-30 17:32:21 +08001934 }
1935
Gilles Peskine449bd832023-01-11 14:50:10 +01001936 if ((ret = ssl->conf->f_rng(ssl->conf->p_rng, server_randbytes,
1937 MBEDTLS_SERVER_HELLO_RANDOM_LEN)) != 0) {
1938 MBEDTLS_SSL_DEBUG_RET(1, "f_rng", ret);
1939 return ret;
Jerry Yuf4b27e42022-03-30 17:32:21 +08001940 }
1941
Gilles Peskine449bd832023-01-11 14:50:10 +01001942 MBEDTLS_SSL_DEBUG_BUF(3, "server hello, random bytes", server_randbytes,
1943 MBEDTLS_SERVER_HELLO_RANDOM_LEN);
Jerry Yuf4b27e42022-03-30 17:32:21 +08001944
1945#if defined(MBEDTLS_HAVE_TIME)
YxCda609132023-05-22 12:08:12 -07001946 ssl->session_negotiate->start = mbedtls_time(NULL);
Jerry Yuf4b27e42022-03-30 17:32:21 +08001947#endif /* MBEDTLS_HAVE_TIME */
1948
Gilles Peskine449bd832023-01-11 14:50:10 +01001949 return ret;
Jerry Yuf4b27e42022-03-30 17:32:21 +08001950}
1951
Jerry Yu3bf2c642022-03-30 22:02:12 +08001952/*
Jerry Yue74e04a2022-04-21 09:23:16 +08001953 * ssl_tls13_write_server_hello_supported_versions_ext ():
Jerry Yu3bf2c642022-03-30 22:02:12 +08001954 *
1955 * struct {
Jerry Yufb9f54d2022-04-06 10:08:34 +08001956 * ProtocolVersion selected_version;
Jerry Yu3bf2c642022-03-30 22:02:12 +08001957 * } SupportedVersions;
1958 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001959MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yue74e04a2022-04-21 09:23:16 +08001960static int ssl_tls13_write_server_hello_supported_versions_ext(
Gilles Peskine449bd832023-01-11 14:50:10 +01001961 mbedtls_ssl_context *ssl,
1962 unsigned char *buf,
1963 unsigned char *end,
1964 size_t *out_len)
Jerry Yu3bf2c642022-03-30 22:02:12 +08001965{
Jerry Yu3bf2c642022-03-30 22:02:12 +08001966 *out_len = 0;
1967
Gilles Peskine449bd832023-01-11 14:50:10 +01001968 MBEDTLS_SSL_DEBUG_MSG(3, ("server hello, write selected version"));
Jerry Yu3bf2c642022-03-30 22:02:12 +08001969
1970 /* Check if we have space to write the extension:
1971 * - extension_type (2 bytes)
1972 * - extension_data_length (2 bytes)
Jerry Yu349a6132022-04-14 20:52:56 +08001973 * - selected_version (2 bytes)
Jerry Yu3bf2c642022-03-30 22:02:12 +08001974 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001975 MBEDTLS_SSL_CHK_BUF_PTR(buf, end, 6);
Jerry Yu3bf2c642022-03-30 22:02:12 +08001976
Gilles Peskine449bd832023-01-11 14:50:10 +01001977 MBEDTLS_PUT_UINT16_BE(MBEDTLS_TLS_EXT_SUPPORTED_VERSIONS, buf, 0);
Jerry Yu3bf2c642022-03-30 22:02:12 +08001978
Gilles Peskine449bd832023-01-11 14:50:10 +01001979 MBEDTLS_PUT_UINT16_BE(2, buf, 2);
Jerry Yu3bf2c642022-03-30 22:02:12 +08001980
Gilles Peskine449bd832023-01-11 14:50:10 +01001981 mbedtls_ssl_write_version(buf + 4,
1982 ssl->conf->transport,
1983 ssl->tls_version);
Jerry Yu3bf2c642022-03-30 22:02:12 +08001984
Gilles Peskine449bd832023-01-11 14:50:10 +01001985 MBEDTLS_SSL_DEBUG_MSG(3, ("supported version: [%04x]",
1986 ssl->tls_version));
Jerry Yu3bf2c642022-03-30 22:02:12 +08001987
Jerry Yu349a6132022-04-14 20:52:56 +08001988 *out_len = 6;
Jerry Yu3bf2c642022-03-30 22:02:12 +08001989
Jerry Yub95dd362022-11-08 21:19:34 +08001990 mbedtls_ssl_tls13_set_hs_sent_ext_mask(
Gilles Peskine449bd832023-01-11 14:50:10 +01001991 ssl, MBEDTLS_TLS_EXT_SUPPORTED_VERSIONS);
Jerry Yub95dd362022-11-08 21:19:34 +08001992
Gilles Peskine449bd832023-01-11 14:50:10 +01001993 return 0;
Jerry Yu3bf2c642022-03-30 22:02:12 +08001994}
1995
Jerry Yud9436a12022-04-20 22:28:09 +08001996
Jerry Yu3bf2c642022-03-30 22:02:12 +08001997
1998/* Generate and export a single key share. For hybrid KEMs, this can
1999 * be called multiple times with the different components of the hybrid. */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002000MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002001static int ssl_tls13_generate_and_write_key_share(mbedtls_ssl_context *ssl,
2002 uint16_t named_group,
2003 unsigned char *buf,
2004 unsigned char *end,
2005 size_t *out_len)
Jerry Yu3bf2c642022-03-30 22:02:12 +08002006{
2007 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yu955ddd72022-04-22 22:27:33 +08002008
2009 *out_len = 0;
2010
Valerio Settic9ae8622023-07-25 11:23:50 +02002011#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_EPHEMERAL_ENABLED)
Przemek Stekiel29c219c2023-05-31 15:21:04 +02002012 if (mbedtls_ssl_tls13_named_group_is_ecdhe(named_group) ||
Przemek Stekield5f79e72023-06-29 09:08:43 +02002013 mbedtls_ssl_tls13_named_group_is_ffdh(named_group)) {
Przemek Stekiel408569f2023-07-06 11:26:44 +02002014 ret = mbedtls_ssl_tls13_generate_and_write_xxdh_key_exchange(
Gilles Peskine449bd832023-01-11 14:50:10 +01002015 ssl, named_group, buf, end, out_len);
2016 if (ret != 0) {
Jerry Yu89e103c2022-03-30 22:43:29 +08002017 MBEDTLS_SSL_DEBUG_RET(
Przemek Stekiel408569f2023-07-06 11:26:44 +02002018 1, "mbedtls_ssl_tls13_generate_and_write_xxdh_key_exchange",
Gilles Peskine449bd832023-01-11 14:50:10 +01002019 ret);
2020 return ret;
Jerry Yu3bf2c642022-03-30 22:02:12 +08002021 }
Gilles Peskine449bd832023-01-11 14:50:10 +01002022 } else
Valerio Settic9ae8622023-07-25 11:23:50 +02002023#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_EPHEMERAL_ENABLED */
Gilles Peskine449bd832023-01-11 14:50:10 +01002024 if (0 /* Other kinds of KEMs */) {
2025 } else {
Jerry Yu955ddd72022-04-22 22:27:33 +08002026 ((void) ssl);
2027 ((void) named_group);
2028 ((void) buf);
2029 ((void) end);
Jerry Yu3bf2c642022-03-30 22:02:12 +08002030 ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
2031 }
2032
Gilles Peskine449bd832023-01-11 14:50:10 +01002033 return ret;
Jerry Yu3bf2c642022-03-30 22:02:12 +08002034}
2035
2036/*
2037 * ssl_tls13_write_key_share_ext
2038 *
2039 * Structure of key_share extension in ServerHello:
2040 *
Jerry Yu1c3e6882022-04-20 21:23:40 +08002041 * struct {
2042 * NamedGroup group;
2043 * opaque key_exchange<1..2^16-1>;
2044 * } KeyShareEntry;
2045 * struct {
2046 * KeyShareEntry server_share;
2047 * } KeyShareServerHello;
Jerry Yu3bf2c642022-03-30 22:02:12 +08002048 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002049MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002050static int ssl_tls13_write_key_share_ext(mbedtls_ssl_context *ssl,
2051 unsigned char *buf,
2052 unsigned char *end,
2053 size_t *out_len)
Jerry Yu3bf2c642022-03-30 22:02:12 +08002054{
Jerry Yu955ddd72022-04-22 22:27:33 +08002055 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yu3bf2c642022-03-30 22:02:12 +08002056 unsigned char *p = buf;
Jerry Yu57d48412022-04-20 21:50:42 +08002057 uint16_t group = ssl->handshake->offered_group_id;
Jerry Yu3bf2c642022-03-30 22:02:12 +08002058 unsigned char *server_share = buf + 4;
Jerry Yu3bf2c642022-03-30 22:02:12 +08002059 size_t key_exchange_length;
Jerry Yu3bf2c642022-03-30 22:02:12 +08002060
2061 *out_len = 0;
2062
Gilles Peskine449bd832023-01-11 14:50:10 +01002063 MBEDTLS_SSL_DEBUG_MSG(3, ("server hello, adding key share extension"));
Jerry Yu3bf2c642022-03-30 22:02:12 +08002064
Gilles Peskine449bd832023-01-11 14:50:10 +01002065 MBEDTLS_SSL_DEBUG_MSG(2, ("server hello, write selected_group: %s (%04x)",
2066 mbedtls_ssl_named_group_to_str(group),
2067 group));
Jerry Yu58af2332022-09-06 11:19:31 +08002068
Jerry Yu3bf2c642022-03-30 22:02:12 +08002069 /* Check if we have space for header and length fields:
2070 * - extension_type (2 bytes)
2071 * - extension_data_length (2 bytes)
2072 * - group (2 bytes)
2073 * - key_exchange_length (2 bytes)
2074 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002075 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 8);
2076 MBEDTLS_PUT_UINT16_BE(MBEDTLS_TLS_EXT_KEY_SHARE, p, 0);
2077 MBEDTLS_PUT_UINT16_BE(group, server_share, 0);
Jerry Yu3bf2c642022-03-30 22:02:12 +08002078 p += 8;
Jerry Yu57d48412022-04-20 21:50:42 +08002079
Jerry Yu3bf2c642022-03-30 22:02:12 +08002080 /* When we introduce PQC-ECDHE hybrids, we'll want to call this
2081 * function multiple times. */
Jerry Yu955ddd72022-04-22 22:27:33 +08002082 ret = ssl_tls13_generate_and_write_key_share(
Gilles Peskine449bd832023-01-11 14:50:10 +01002083 ssl, group, server_share + 4, end, &key_exchange_length);
2084 if (ret != 0) {
2085 return ret;
2086 }
Jerry Yu3bf2c642022-03-30 22:02:12 +08002087 p += key_exchange_length;
Jerry Yuc1be19f2022-04-23 16:11:39 +08002088
Gilles Peskine449bd832023-01-11 14:50:10 +01002089 MBEDTLS_PUT_UINT16_BE(key_exchange_length, server_share + 2, 0);
Jerry Yu3bf2c642022-03-30 22:02:12 +08002090
Gilles Peskine449bd832023-01-11 14:50:10 +01002091 MBEDTLS_PUT_UINT16_BE(p - server_share, buf, 2);
Jerry Yu3bf2c642022-03-30 22:02:12 +08002092
Jerry Yu57d48412022-04-20 21:50:42 +08002093 *out_len = p - buf;
Jerry Yu955ddd72022-04-22 22:27:33 +08002094
Gilles Peskine449bd832023-01-11 14:50:10 +01002095 mbedtls_ssl_tls13_set_hs_sent_ext_mask(ssl, MBEDTLS_TLS_EXT_KEY_SHARE);
Jerry Yub95dd362022-11-08 21:19:34 +08002096
Gilles Peskine449bd832023-01-11 14:50:10 +01002097 return 0;
Jerry Yu3bf2c642022-03-30 22:02:12 +08002098}
Jerry Yud9436a12022-04-20 22:28:09 +08002099
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002100MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002101static int ssl_tls13_write_hrr_key_share_ext(mbedtls_ssl_context *ssl,
2102 unsigned char *buf,
2103 unsigned char *end,
2104 size_t *out_len)
Jerry Yu23f7a6f2022-04-23 15:16:45 +08002105{
2106 uint16_t selected_group = ssl->handshake->hrr_selected_group;
2107 /* key_share Extension
2108 *
2109 * struct {
2110 * select (Handshake.msg_type) {
2111 * ...
2112 * case hello_retry_request:
2113 * NamedGroup selected_group;
2114 * ...
2115 * };
2116 * } KeyShare;
2117 */
2118
2119 *out_len = 0;
2120
Jerry Yub0ac10b2022-05-05 11:10:08 +08002121 /*
2122 * For a pure PSK key exchange, there is no group to agree upon. The purpose
2123 * of the HRR is then to transmit a cookie to force the client to demonstrate
2124 * reachability at their apparent network address (primarily useful for DTLS).
2125 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002126 if (!mbedtls_ssl_tls13_key_exchange_mode_with_ephemeral(ssl)) {
2127 return 0;
2128 }
Jerry Yu23f7a6f2022-04-23 15:16:45 +08002129
2130 /* We should only send the key_share extension if the client's initial
2131 * key share was not acceptable. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002132 if (ssl->handshake->offered_group_id != 0) {
2133 MBEDTLS_SSL_DEBUG_MSG(4, ("Skip key_share extension in HRR"));
2134 return 0;
Jerry Yu23f7a6f2022-04-23 15:16:45 +08002135 }
2136
Gilles Peskine449bd832023-01-11 14:50:10 +01002137 if (selected_group == 0) {
2138 MBEDTLS_SSL_DEBUG_MSG(1, ("no matching named group found"));
2139 return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
Jerry Yu23f7a6f2022-04-23 15:16:45 +08002140 }
2141
Jerry Yub0ac10b2022-05-05 11:10:08 +08002142 /* Check if we have enough space:
2143 * - extension_type (2 bytes)
2144 * - extension_data_length (2 bytes)
2145 * - selected_group (2 bytes)
2146 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002147 MBEDTLS_SSL_CHK_BUF_PTR(buf, end, 6);
Jerry Yu23f7a6f2022-04-23 15:16:45 +08002148
Gilles Peskine449bd832023-01-11 14:50:10 +01002149 MBEDTLS_PUT_UINT16_BE(MBEDTLS_TLS_EXT_KEY_SHARE, buf, 0);
2150 MBEDTLS_PUT_UINT16_BE(2, buf, 2);
2151 MBEDTLS_PUT_UINT16_BE(selected_group, buf, 4);
Jerry Yu23f7a6f2022-04-23 15:16:45 +08002152
Gilles Peskine449bd832023-01-11 14:50:10 +01002153 MBEDTLS_SSL_DEBUG_MSG(3,
2154 ("HRR selected_group: %s (%x)",
2155 mbedtls_ssl_named_group_to_str(selected_group),
2156 selected_group));
Jerry Yu23f7a6f2022-04-23 15:16:45 +08002157
2158 *out_len = 6;
Jerry Yu23f7a6f2022-04-23 15:16:45 +08002159
Gilles Peskine449bd832023-01-11 14:50:10 +01002160 mbedtls_ssl_tls13_set_hs_sent_ext_mask(ssl, MBEDTLS_TLS_EXT_KEY_SHARE);
Jerry Yub95dd362022-11-08 21:19:34 +08002161
Gilles Peskine449bd832023-01-11 14:50:10 +01002162 return 0;
Jerry Yu23f7a6f2022-04-23 15:16:45 +08002163}
Jerry Yu3bf2c642022-03-30 22:02:12 +08002164
2165/*
2166 * Structure of ServerHello message:
2167 *
2168 * struct {
2169 * ProtocolVersion legacy_version = 0x0303; // TLS v1.2
2170 * Random random;
2171 * opaque legacy_session_id_echo<0..32>;
2172 * CipherSuite cipher_suite;
2173 * uint8 legacy_compression_method = 0;
2174 * Extension extensions<6..2^16-1>;
2175 * } ServerHello;
2176 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002177MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002178static int ssl_tls13_write_server_hello_body(mbedtls_ssl_context *ssl,
2179 unsigned char *buf,
2180 unsigned char *end,
2181 size_t *out_len,
2182 int is_hrr)
Jerry Yu56404d72022-03-30 17:36:13 +08002183{
Jerry Yu955ddd72022-04-22 22:27:33 +08002184 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yu3bf2c642022-03-30 22:02:12 +08002185 unsigned char *p = buf;
Jerry Yud9436a12022-04-20 22:28:09 +08002186 unsigned char *p_extensions_len;
Jerry Yufbe3e642022-04-25 19:31:51 +08002187 size_t output_len;
Jerry Yu3bf2c642022-03-30 22:02:12 +08002188
2189 *out_len = 0;
Jerry Yu50e00e32022-10-31 14:45:01 +08002190 ssl->handshake->sent_extensions = MBEDTLS_SSL_EXT_MASK_NONE;
Jerry Yu3bf2c642022-03-30 22:02:12 +08002191
Jerry Yucfc04b32022-04-21 09:31:58 +08002192 /* ...
2193 * ProtocolVersion legacy_version = 0x0303; // TLS 1.2
2194 * ...
2195 * with ProtocolVersion defined as:
2196 * uint16 ProtocolVersion;
Jerry Yu3bf2c642022-03-30 22:02:12 +08002197 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002198 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 2);
2199 MBEDTLS_PUT_UINT16_BE(0x0303, p, 0);
Jerry Yu3bf2c642022-03-30 22:02:12 +08002200 p += 2;
2201
Jerry Yu1c3e6882022-04-20 21:23:40 +08002202 /* ...
2203 * Random random;
2204 * ...
Jerry Yucfc04b32022-04-21 09:31:58 +08002205 * with Random defined as:
2206 * opaque Random[MBEDTLS_SERVER_HELLO_RANDOM_LEN];
Jerry Yu1c3e6882022-04-20 21:23:40 +08002207 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002208 MBEDTLS_SSL_CHK_BUF_PTR(p, end, MBEDTLS_SERVER_HELLO_RANDOM_LEN);
2209 if (is_hrr) {
2210 memcpy(p, mbedtls_ssl_tls13_hello_retry_request_magic,
2211 MBEDTLS_SERVER_HELLO_RANDOM_LEN);
2212 } else {
2213 memcpy(p, &ssl->handshake->randbytes[MBEDTLS_CLIENT_HELLO_RANDOM_LEN],
2214 MBEDTLS_SERVER_HELLO_RANDOM_LEN);
Jerry Yu23f7a6f2022-04-23 15:16:45 +08002215 }
Gilles Peskine449bd832023-01-11 14:50:10 +01002216 MBEDTLS_SSL_DEBUG_BUF(3, "server hello, random bytes",
2217 p, MBEDTLS_SERVER_HELLO_RANDOM_LEN);
Jerry Yu3bf2c642022-03-30 22:02:12 +08002218 p += MBEDTLS_SERVER_HELLO_RANDOM_LEN;
2219
Jerry Yucfc04b32022-04-21 09:31:58 +08002220 /* ...
2221 * opaque legacy_session_id_echo<0..32>;
2222 * ...
Jerry Yu3bf2c642022-03-30 22:02:12 +08002223 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002224 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 1 + ssl->session_negotiate->id_len);
2225 *p++ = (unsigned char) ssl->session_negotiate->id_len;
2226 if (ssl->session_negotiate->id_len > 0) {
2227 memcpy(p, &ssl->session_negotiate->id[0],
2228 ssl->session_negotiate->id_len);
Jerry Yu3bf2c642022-03-30 22:02:12 +08002229 p += ssl->session_negotiate->id_len;
Jerry Yu955ddd72022-04-22 22:27:33 +08002230
Gilles Peskine449bd832023-01-11 14:50:10 +01002231 MBEDTLS_SSL_DEBUG_BUF(3, "session id", ssl->session_negotiate->id,
2232 ssl->session_negotiate->id_len);
Jerry Yu3bf2c642022-03-30 22:02:12 +08002233 }
2234
Jerry Yucfc04b32022-04-21 09:31:58 +08002235 /* ...
2236 * CipherSuite cipher_suite;
2237 * ...
2238 * with CipherSuite defined as:
2239 * uint8 CipherSuite[2];
Jerry Yu3bf2c642022-03-30 22:02:12 +08002240 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002241 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 2);
2242 MBEDTLS_PUT_UINT16_BE(ssl->session_negotiate->ciphersuite, p, 0);
Jerry Yu3bf2c642022-03-30 22:02:12 +08002243 p += 2;
Gilles Peskine449bd832023-01-11 14:50:10 +01002244 MBEDTLS_SSL_DEBUG_MSG(3,
2245 ("server hello, chosen ciphersuite: %s ( id=%d )",
2246 mbedtls_ssl_get_ciphersuite_name(
2247 ssl->session_negotiate->ciphersuite),
2248 ssl->session_negotiate->ciphersuite));
Jerry Yu3bf2c642022-03-30 22:02:12 +08002249
Jerry Yucfc04b32022-04-21 09:31:58 +08002250 /* ...
2251 * uint8 legacy_compression_method = 0;
2252 * ...
2253 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002254 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 1);
Thomas Daubney31e03a82022-07-25 15:59:25 +01002255 *p++ = MBEDTLS_SSL_COMPRESS_NULL;
Jerry Yu3bf2c642022-03-30 22:02:12 +08002256
Jerry Yucfc04b32022-04-21 09:31:58 +08002257 /* ...
2258 * Extension extensions<6..2^16-1>;
2259 * ...
2260 * struct {
2261 * ExtensionType extension_type; (2 bytes)
2262 * opaque extension_data<0..2^16-1>;
2263 * } Extension;
2264 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002265 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 2);
Jerry Yud9436a12022-04-20 22:28:09 +08002266 p_extensions_len = p;
Jerry Yu3bf2c642022-03-30 22:02:12 +08002267 p += 2;
2268
Gilles Peskine449bd832023-01-11 14:50:10 +01002269 if ((ret = ssl_tls13_write_server_hello_supported_versions_ext(
2270 ssl, p, end, &output_len)) != 0) {
Jerry Yu955ddd72022-04-22 22:27:33 +08002271 MBEDTLS_SSL_DEBUG_RET(
Gilles Peskine449bd832023-01-11 14:50:10 +01002272 1, "ssl_tls13_write_server_hello_supported_versions_ext", ret);
2273 return ret;
Jerry Yu3bf2c642022-03-30 22:02:12 +08002274 }
2275 p += output_len;
2276
Gilles Peskine449bd832023-01-11 14:50:10 +01002277 if (mbedtls_ssl_tls13_key_exchange_mode_with_ephemeral(ssl)) {
2278 if (is_hrr) {
2279 ret = ssl_tls13_write_hrr_key_share_ext(ssl, p, end, &output_len);
2280 } else {
2281 ret = ssl_tls13_write_key_share_ext(ssl, p, end, &output_len);
2282 }
2283 if (ret != 0) {
2284 return ret;
2285 }
Jerry Yu3bf2c642022-03-30 22:02:12 +08002286 p += output_len;
2287 }
Jerry Yu3bf2c642022-03-30 22:02:12 +08002288
Ronald Cron41a443a2022-10-04 16:38:25 +02002289#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01002290 if (!is_hrr && mbedtls_ssl_tls13_key_exchange_mode_with_psk(ssl)) {
2291 ret = ssl_tls13_write_server_pre_shared_key_ext(ssl, p, end, &output_len);
2292 if (ret != 0) {
2293 MBEDTLS_SSL_DEBUG_RET(1, "ssl_tls13_write_server_pre_shared_key_ext",
2294 ret);
2295 return ret;
Jerry Yu032b15ce2022-07-11 06:10:03 +00002296 }
2297 p += output_len;
2298 }
Ronald Cron41a443a2022-10-04 16:38:25 +02002299#endif
Jerry Yu032b15ce2022-07-11 06:10:03 +00002300
Gilles Peskine449bd832023-01-11 14:50:10 +01002301 MBEDTLS_PUT_UINT16_BE(p - p_extensions_len - 2, p_extensions_len, 0);
Jerry Yu3bf2c642022-03-30 22:02:12 +08002302
Gilles Peskine449bd832023-01-11 14:50:10 +01002303 MBEDTLS_SSL_DEBUG_BUF(4, "server hello extensions",
2304 p_extensions_len, p - p_extensions_len);
Jerry Yu3bf2c642022-03-30 22:02:12 +08002305
Jerry Yud9436a12022-04-20 22:28:09 +08002306 *out_len = p - buf;
Jerry Yu3bf2c642022-03-30 22:02:12 +08002307
Gilles Peskine449bd832023-01-11 14:50:10 +01002308 MBEDTLS_SSL_DEBUG_BUF(3, "server hello", buf, *out_len);
Jerry Yu3bf2c642022-03-30 22:02:12 +08002309
Jerry Yu7de2ff02022-11-08 21:43:46 +08002310 MBEDTLS_SSL_PRINT_EXTS(
Jerry Yu4b8f2f72022-10-31 13:31:22 +08002311 3, is_hrr ? MBEDTLS_SSL_TLS1_3_HS_HELLO_RETRY_REQUEST :
Gilles Peskine449bd832023-01-11 14:50:10 +01002312 MBEDTLS_SSL_HS_SERVER_HELLO,
2313 ssl->handshake->sent_extensions);
Jerry Yu4b8f2f72022-10-31 13:31:22 +08002314
Gilles Peskine449bd832023-01-11 14:50:10 +01002315 return ret;
Jerry Yu56404d72022-03-30 17:36:13 +08002316}
2317
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002318MBEDTLS_CHECK_RETURN_CRITICAL
Xiaokang Qian934ce6f2023-02-06 10:23:04 +00002319static int ssl_tls13_finalize_server_hello(mbedtls_ssl_context *ssl)
Jerry Yue110d252022-05-05 10:19:22 +08002320{
2321 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine449bd832023-01-11 14:50:10 +01002322 ret = mbedtls_ssl_tls13_compute_handshake_transform(ssl);
2323 if (ret != 0) {
2324 MBEDTLS_SSL_DEBUG_RET(1,
2325 "mbedtls_ssl_tls13_compute_handshake_transform",
2326 ret);
2327 return ret;
Jerry Yue110d252022-05-05 10:19:22 +08002328 }
2329
Gilles Peskine449bd832023-01-11 14:50:10 +01002330 return ret;
Jerry Yue110d252022-05-05 10:19:22 +08002331}
2332
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002333MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002334static int ssl_tls13_write_server_hello(mbedtls_ssl_context *ssl)
Jerry Yu5b64ae92022-03-30 17:15:02 +08002335{
Jerry Yu637a3f12022-04-20 21:37:58 +08002336 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yuf4b27e42022-03-30 17:32:21 +08002337 unsigned char *buf;
2338 size_t buf_len, msg_len;
2339
Gilles Peskine449bd832023-01-11 14:50:10 +01002340 MBEDTLS_SSL_DEBUG_MSG(2, ("=> write server hello"));
Jerry Yuf4b27e42022-03-30 17:32:21 +08002341
Gilles Peskine449bd832023-01-11 14:50:10 +01002342 MBEDTLS_SSL_PROC_CHK(ssl_tls13_prepare_server_hello(ssl));
Jerry Yuf4b27e42022-03-30 17:32:21 +08002343
Xiaokang Qian9f1747b2023-03-30 02:50:04 +00002344 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_start_handshake_msg(
2345 ssl, MBEDTLS_SSL_HS_SERVER_HELLO, &buf, &buf_len));
Jerry Yuf4b27e42022-03-30 17:32:21 +08002346
Gilles Peskine449bd832023-01-11 14:50:10 +01002347 MBEDTLS_SSL_PROC_CHK(ssl_tls13_write_server_hello_body(ssl, buf,
2348 buf + buf_len,
2349 &msg_len,
2350 0));
Jerry Yuf4b27e42022-03-30 17:32:21 +08002351
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +01002352 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_add_hs_msg_to_checksum(
Manuel Pégourié-Gonnard43cc1272023-02-06 11:48:19 +01002353 ssl, MBEDTLS_SSL_HS_SERVER_HELLO, buf, msg_len));
Jerry Yuf4b27e42022-03-30 17:32:21 +08002354
Gilles Peskine449bd832023-01-11 14:50:10 +01002355 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_finish_handshake_msg(
2356 ssl, buf_len, msg_len));
Jerry Yu637a3f12022-04-20 21:37:58 +08002357
Xiaokang Qian934ce6f2023-02-06 10:23:04 +00002358 MBEDTLS_SSL_PROC_CHK(ssl_tls13_finalize_server_hello(ssl));
Jerry Yue110d252022-05-05 10:19:22 +08002359
Gabor Mezei7b39bf12022-05-24 16:04:14 +02002360#if defined(MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE)
2361 /* The server sends a dummy change_cipher_spec record immediately
2362 * after its first handshake message. This may either be after
2363 * a ServerHello or a HelloRetryRequest.
2364 */
Gabor Mezei96ae9262022-06-28 11:45:18 +02002365 mbedtls_ssl_handshake_set_state(
Gilles Peskine449bd832023-01-11 14:50:10 +01002366 ssl, MBEDTLS_SSL_SERVER_CCS_AFTER_SERVER_HELLO);
Gabor Mezei7b39bf12022-05-24 16:04:14 +02002367#else
Gilles Peskine449bd832023-01-11 14:50:10 +01002368 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_ENCRYPTED_EXTENSIONS);
Gabor Mezei7b39bf12022-05-24 16:04:14 +02002369#endif /* MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE */
Jerry Yue110d252022-05-05 10:19:22 +08002370
Jerry Yuf4b27e42022-03-30 17:32:21 +08002371cleanup:
2372
Gilles Peskine449bd832023-01-11 14:50:10 +01002373 MBEDTLS_SSL_DEBUG_MSG(2, ("<= write server hello"));
2374 return ret;
Jerry Yu5b64ae92022-03-30 17:15:02 +08002375}
2376
Jerry Yu23d1a252022-05-12 18:08:59 +08002377
2378/*
2379 * Handler for MBEDTLS_SSL_HELLO_RETRY_REQUEST
2380 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002381MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002382static int ssl_tls13_prepare_hello_retry_request(mbedtls_ssl_context *ssl)
Jerry Yu23d1a252022-05-12 18:08:59 +08002383{
2384 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Ronald Cron5fbd2702024-02-14 10:03:36 +01002385 if (ssl->handshake->hello_retry_request_flag) {
Gilles Peskine449bd832023-01-11 14:50:10 +01002386 MBEDTLS_SSL_DEBUG_MSG(1, ("Too many HRRs"));
2387 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE,
2388 MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE);
2389 return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
Jerry Yu23d1a252022-05-12 18:08:59 +08002390 }
2391
2392 /*
2393 * Create stateless transcript hash for HRR
2394 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002395 MBEDTLS_SSL_DEBUG_MSG(4, ("Reset transcript for HRR"));
2396 ret = mbedtls_ssl_reset_transcript_for_hrr(ssl);
2397 if (ret != 0) {
2398 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_reset_transcript_for_hrr", ret);
2399 return ret;
Jerry Yu23d1a252022-05-12 18:08:59 +08002400 }
Gilles Peskine449bd832023-01-11 14:50:10 +01002401 mbedtls_ssl_session_reset_msg_layer(ssl, 0);
Jerry Yu23d1a252022-05-12 18:08:59 +08002402
Gilles Peskine449bd832023-01-11 14:50:10 +01002403 return 0;
Jerry Yu23d1a252022-05-12 18:08:59 +08002404}
2405
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002406MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002407static int ssl_tls13_write_hello_retry_request(mbedtls_ssl_context *ssl)
Jerry Yu23d1a252022-05-12 18:08:59 +08002408{
2409 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
2410 unsigned char *buf;
2411 size_t buf_len, msg_len;
2412
Gilles Peskine449bd832023-01-11 14:50:10 +01002413 MBEDTLS_SSL_DEBUG_MSG(2, ("=> write hello retry request"));
Jerry Yu23d1a252022-05-12 18:08:59 +08002414
Gilles Peskine449bd832023-01-11 14:50:10 +01002415 MBEDTLS_SSL_PROC_CHK(ssl_tls13_prepare_hello_retry_request(ssl));
Jerry Yu23d1a252022-05-12 18:08:59 +08002416
Gilles Peskine449bd832023-01-11 14:50:10 +01002417 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_start_handshake_msg(
2418 ssl, MBEDTLS_SSL_HS_SERVER_HELLO,
2419 &buf, &buf_len));
Jerry Yu23d1a252022-05-12 18:08:59 +08002420
Gilles Peskine449bd832023-01-11 14:50:10 +01002421 MBEDTLS_SSL_PROC_CHK(ssl_tls13_write_server_hello_body(ssl, buf,
2422 buf + buf_len,
2423 &msg_len,
2424 1));
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +01002425 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_add_hs_msg_to_checksum(
Manuel Pégourié-Gonnard43cc1272023-02-06 11:48:19 +01002426 ssl, MBEDTLS_SSL_HS_SERVER_HELLO, buf, msg_len));
Jerry Yu23d1a252022-05-12 18:08:59 +08002427
2428
Gilles Peskine449bd832023-01-11 14:50:10 +01002429 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_finish_handshake_msg(ssl, buf_len,
2430 msg_len));
Jerry Yu23d1a252022-05-12 18:08:59 +08002431
Ronald Cron5fbd2702024-02-14 10:03:36 +01002432 ssl->handshake->hello_retry_request_flag = 1;
Jerry Yu23d1a252022-05-12 18:08:59 +08002433
Gabor Mezei7b39bf12022-05-24 16:04:14 +02002434#if defined(MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE)
2435 /* The server sends a dummy change_cipher_spec record immediately
2436 * after its first handshake message. This may either be after
2437 * a ServerHello or a HelloRetryRequest.
2438 */
Gabor Mezei96ae9262022-06-28 11:45:18 +02002439 mbedtls_ssl_handshake_set_state(
Gilles Peskine449bd832023-01-11 14:50:10 +01002440 ssl, MBEDTLS_SSL_SERVER_CCS_AFTER_HELLO_RETRY_REQUEST);
Gabor Mezei7b39bf12022-05-24 16:04:14 +02002441#else
Gilles Peskine449bd832023-01-11 14:50:10 +01002442 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_CLIENT_HELLO);
Gabor Mezei7b39bf12022-05-24 16:04:14 +02002443#endif /* MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE */
Jerry Yu23d1a252022-05-12 18:08:59 +08002444
2445cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +01002446 MBEDTLS_SSL_DEBUG_MSG(2, ("<= write hello retry request"));
2447 return ret;
Jerry Yu23d1a252022-05-12 18:08:59 +08002448}
2449
Jerry Yu5b64ae92022-03-30 17:15:02 +08002450/*
Jerry Yu4d3841a2022-04-16 12:37:19 +08002451 * Handler for MBEDTLS_SSL_ENCRYPTED_EXTENSIONS
2452 */
Jerry Yu4d3841a2022-04-16 12:37:19 +08002453
2454/*
2455 * struct {
2456 * Extension extensions<0..2 ^ 16 - 1>;
2457 * } EncryptedExtensions;
2458 *
2459 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002460MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002461static int ssl_tls13_write_encrypted_extensions_body(mbedtls_ssl_context *ssl,
2462 unsigned char *buf,
2463 unsigned char *end,
2464 size_t *out_len)
Jerry Yu4d3841a2022-04-16 12:37:19 +08002465{
XiaokangQianacb39922022-06-17 10:18:48 +00002466 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yu4d3841a2022-04-16 12:37:19 +08002467 unsigned char *p = buf;
2468 size_t extensions_len = 0;
Jerry Yu8937eb42022-05-03 12:12:14 +08002469 unsigned char *p_extensions_len;
XiaokangQianacb39922022-06-17 10:18:48 +00002470 size_t output_len;
Jerry Yu9da5e5a2022-05-03 15:46:09 +08002471
Jerry Yu4d3841a2022-04-16 12:37:19 +08002472 *out_len = 0;
2473
Gilles Peskine449bd832023-01-11 14:50:10 +01002474 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 2);
Jerry Yu8937eb42022-05-03 12:12:14 +08002475 p_extensions_len = p;
Jerry Yu4d3841a2022-04-16 12:37:19 +08002476 p += 2;
2477
Jerry Yu8937eb42022-05-03 12:12:14 +08002478 ((void) ssl);
XiaokangQianacb39922022-06-17 10:18:48 +00002479 ((void) ret);
2480 ((void) output_len);
2481
2482#if defined(MBEDTLS_SSL_ALPN)
Gilles Peskine449bd832023-01-11 14:50:10 +01002483 ret = mbedtls_ssl_write_alpn_ext(ssl, p, end, &output_len);
2484 if (ret != 0) {
2485 return ret;
2486 }
XiaokangQian95d5f542022-06-24 02:29:26 +00002487 p += output_len;
XiaokangQianacb39922022-06-17 10:18:48 +00002488#endif /* MBEDTLS_SSL_ALPN */
Jerry Yu4d3841a2022-04-16 12:37:19 +08002489
Jerry Yu71c14f12022-12-12 11:10:35 +08002490#if defined(MBEDTLS_SSL_EARLY_DATA)
Ronald Cron78a38f62024-02-01 18:30:31 +01002491 if (ssl->handshake->early_data_accepted) {
Jerry Yu52335392023-11-23 18:06:06 +08002492 ret = mbedtls_ssl_tls13_write_early_data_ext(
Jerry Yuc59c5862023-12-05 10:40:49 +08002493 ssl, 0, p, end, &output_len);
Jerry Yu71c14f12022-12-12 11:10:35 +08002494 if (ret != 0) {
2495 return ret;
2496 }
2497 p += output_len;
2498 }
2499#endif /* MBEDTLS_SSL_EARLY_DATA */
2500
Waleed Elmelegy47d29462024-01-03 17:31:52 +00002501#if defined(MBEDTLS_SSL_RECORD_SIZE_LIMIT)
Waleed Elmelegyfbe42742024-01-05 18:11:10 +00002502 if (ssl->handshake->received_extensions & MBEDTLS_SSL_EXT_MASK(RECORD_SIZE_LIMIT)) {
Waleed Elmelegyd2fc90e2024-01-04 18:04:53 +00002503 ret = mbedtls_ssl_tls13_write_record_size_limit_ext(
2504 ssl, p, end, &output_len);
2505 if (ret != 0) {
2506 return ret;
2507 }
2508 p += output_len;
Waleed Elmelegy47d29462024-01-03 17:31:52 +00002509 }
Waleed Elmelegy47d29462024-01-03 17:31:52 +00002510#endif
2511
Gilles Peskine449bd832023-01-11 14:50:10 +01002512 extensions_len = (p - p_extensions_len) - 2;
2513 MBEDTLS_PUT_UINT16_BE(extensions_len, p_extensions_len, 0);
Jerry Yu8937eb42022-05-03 12:12:14 +08002514
2515 *out_len = p - buf;
Jerry Yu4d3841a2022-04-16 12:37:19 +08002516
Gilles Peskine449bd832023-01-11 14:50:10 +01002517 MBEDTLS_SSL_DEBUG_BUF(4, "encrypted extensions", buf, *out_len);
Jerry Yu4d3841a2022-04-16 12:37:19 +08002518
Jerry Yu7de2ff02022-11-08 21:43:46 +08002519 MBEDTLS_SSL_PRINT_EXTS(
Gilles Peskine449bd832023-01-11 14:50:10 +01002520 3, MBEDTLS_SSL_HS_ENCRYPTED_EXTENSIONS, ssl->handshake->sent_extensions);
Jerry Yu4b8f2f72022-10-31 13:31:22 +08002521
Gilles Peskine449bd832023-01-11 14:50:10 +01002522 return 0;
Jerry Yu4d3841a2022-04-16 12:37:19 +08002523}
2524
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002525MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002526static int ssl_tls13_write_encrypted_extensions(mbedtls_ssl_context *ssl)
Jerry Yu4d3841a2022-04-16 12:37:19 +08002527{
2528 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
2529 unsigned char *buf;
Jerry Yu39730a72022-05-03 12:14:04 +08002530 size_t buf_len, msg_len;
Jerry Yu4d3841a2022-04-16 12:37:19 +08002531
Gilles Peskine449bd832023-01-11 14:50:10 +01002532 mbedtls_ssl_set_outbound_transform(ssl,
2533 ssl->handshake->transform_handshake);
Gabor Mezei54719122022-06-28 11:34:56 +02002534 MBEDTLS_SSL_DEBUG_MSG(
Gilles Peskine449bd832023-01-11 14:50:10 +01002535 3, ("switching to handshake transform for outbound data"));
Gabor Mezei54719122022-06-28 11:34:56 +02002536
Gilles Peskine449bd832023-01-11 14:50:10 +01002537 MBEDTLS_SSL_DEBUG_MSG(2, ("=> write encrypted extensions"));
Jerry Yu4d3841a2022-04-16 12:37:19 +08002538
Xiaokang Qian9f1747b2023-03-30 02:50:04 +00002539 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_start_handshake_msg(
2540 ssl, MBEDTLS_SSL_HS_ENCRYPTED_EXTENSIONS,
2541 &buf, &buf_len));
Jerry Yu4d3841a2022-04-16 12:37:19 +08002542
Gilles Peskine449bd832023-01-11 14:50:10 +01002543 MBEDTLS_SSL_PROC_CHK(ssl_tls13_write_encrypted_extensions_body(
2544 ssl, buf, buf + buf_len, &msg_len));
Jerry Yu4d3841a2022-04-16 12:37:19 +08002545
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +01002546 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_add_hs_msg_to_checksum(
Xiaokang Qian9f1747b2023-03-30 02:50:04 +00002547 ssl, MBEDTLS_SSL_HS_ENCRYPTED_EXTENSIONS,
2548 buf, msg_len));
Jerry Yu4d3841a2022-04-16 12:37:19 +08002549
Gilles Peskine449bd832023-01-11 14:50:10 +01002550 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_finish_handshake_msg(
2551 ssl, buf_len, msg_len));
Jerry Yu4d3841a2022-04-16 12:37:19 +08002552
Ronald Cron928cbd32022-10-04 16:14:26 +02002553#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01002554 if (mbedtls_ssl_tls13_key_exchange_mode_with_psk(ssl)) {
2555 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_SERVER_FINISHED);
2556 } else {
2557 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_CERTIFICATE_REQUEST);
2558 }
Jerry Yu8937eb42022-05-03 12:12:14 +08002559#else
Gilles Peskine449bd832023-01-11 14:50:10 +01002560 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_SERVER_FINISHED);
Jerry Yu8937eb42022-05-03 12:12:14 +08002561#endif
2562
Jerry Yu4d3841a2022-04-16 12:37:19 +08002563cleanup:
2564
Gilles Peskine449bd832023-01-11 14:50:10 +01002565 MBEDTLS_SSL_DEBUG_MSG(2, ("<= write encrypted extensions"));
2566 return ret;
Jerry Yu4d3841a2022-04-16 12:37:19 +08002567}
2568
Ronald Cron928cbd32022-10-04 16:14:26 +02002569#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED)
XiaokangQiana987e1d2022-05-07 01:25:58 +00002570#define SSL_CERTIFICATE_REQUEST_SEND_REQUEST 0
2571#define SSL_CERTIFICATE_REQUEST_SKIP 1
2572/* Coordination:
2573 * Check whether a CertificateRequest message should be written.
2574 * Returns a negative code on failure, or
2575 * - SSL_CERTIFICATE_REQUEST_SEND_REQUEST
2576 * - SSL_CERTIFICATE_REQUEST_SKIP
2577 * indicating if the writing of the CertificateRequest
2578 * should be skipped or not.
2579 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002580MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002581static int ssl_tls13_certificate_request_coordinate(mbedtls_ssl_context *ssl)
XiaokangQiana987e1d2022-05-07 01:25:58 +00002582{
2583 int authmode;
2584
XiaokangQian40a35232022-05-07 09:02:40 +00002585#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Gilles Peskine449bd832023-01-11 14:50:10 +01002586 if (ssl->handshake->sni_authmode != MBEDTLS_SSL_VERIFY_UNSET) {
XiaokangQian40a35232022-05-07 09:02:40 +00002587 authmode = ssl->handshake->sni_authmode;
Gilles Peskine449bd832023-01-11 14:50:10 +01002588 } else
XiaokangQian40a35232022-05-07 09:02:40 +00002589#endif
XiaokangQiana987e1d2022-05-07 01:25:58 +00002590 authmode = ssl->conf->authmode;
2591
Gilles Peskine449bd832023-01-11 14:50:10 +01002592 if (authmode == MBEDTLS_SSL_VERIFY_NONE) {
Ronald Croneac00ad2022-09-13 10:16:31 +02002593 ssl->session_negotiate->verify_result = MBEDTLS_X509_BADCERT_SKIP_VERIFY;
Gilles Peskine449bd832023-01-11 14:50:10 +01002594 return SSL_CERTIFICATE_REQUEST_SKIP;
Ronald Croneac00ad2022-09-13 10:16:31 +02002595 }
XiaokangQiana987e1d2022-05-07 01:25:58 +00002596
XiaokangQianc3017f62022-05-13 05:55:41 +00002597 ssl->handshake->certificate_request_sent = 1;
XiaokangQian189ded22022-05-10 08:12:17 +00002598
Gilles Peskine449bd832023-01-11 14:50:10 +01002599 return SSL_CERTIFICATE_REQUEST_SEND_REQUEST;
XiaokangQiana987e1d2022-05-07 01:25:58 +00002600}
2601
XiaokangQiancec9ae62022-05-06 07:28:50 +00002602/*
2603 * struct {
2604 * opaque certificate_request_context<0..2^8-1>;
2605 * Extension extensions<2..2^16-1>;
2606 * } CertificateRequest;
2607 *
2608 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002609MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002610static int ssl_tls13_write_certificate_request_body(mbedtls_ssl_context *ssl,
2611 unsigned char *buf,
2612 const unsigned char *end,
2613 size_t *out_len)
XiaokangQiancec9ae62022-05-06 07:28:50 +00002614{
2615 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
2616 unsigned char *p = buf;
XiaokangQianec6efb92022-05-06 09:53:10 +00002617 size_t output_len = 0;
XiaokangQiancec9ae62022-05-06 07:28:50 +00002618 unsigned char *p_extensions_len;
2619
2620 *out_len = 0;
2621
2622 /* Check if we have enough space:
2623 * - certificate_request_context (1 byte)
2624 * - extensions length (2 bytes)
2625 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002626 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 3);
XiaokangQiancec9ae62022-05-06 07:28:50 +00002627
2628 /*
2629 * Write certificate_request_context
2630 */
2631 /*
2632 * We use a zero length context for the normal handshake
2633 * messages. For post-authentication handshake messages
2634 * this request context would be set to a non-zero value.
2635 */
2636 *p++ = 0x0;
2637
2638 /*
2639 * Write extensions
2640 */
2641 /* The extensions must contain the signature_algorithms. */
2642 p_extensions_len = p;
2643 p += 2;
Gilles Peskine449bd832023-01-11 14:50:10 +01002644 ret = mbedtls_ssl_write_sig_alg_ext(ssl, p, end, &output_len);
2645 if (ret != 0) {
2646 return ret;
2647 }
XiaokangQiancec9ae62022-05-06 07:28:50 +00002648
XiaokangQianec6efb92022-05-06 09:53:10 +00002649 p += output_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01002650 MBEDTLS_PUT_UINT16_BE(p - p_extensions_len - 2, p_extensions_len, 0);
XiaokangQiancec9ae62022-05-06 07:28:50 +00002651
2652 *out_len = p - buf;
2653
Jerry Yu7de2ff02022-11-08 21:43:46 +08002654 MBEDTLS_SSL_PRINT_EXTS(
Gilles Peskine449bd832023-01-11 14:50:10 +01002655 3, MBEDTLS_SSL_HS_CERTIFICATE_REQUEST, ssl->handshake->sent_extensions);
Jerry Yu4b8f2f72022-10-31 13:31:22 +08002656
Gilles Peskine449bd832023-01-11 14:50:10 +01002657 return 0;
XiaokangQiancec9ae62022-05-06 07:28:50 +00002658}
2659
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002660MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002661static int ssl_tls13_write_certificate_request(mbedtls_ssl_context *ssl)
XiaokangQiancec9ae62022-05-06 07:28:50 +00002662{
2663 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
2664
Gilles Peskine449bd832023-01-11 14:50:10 +01002665 MBEDTLS_SSL_DEBUG_MSG(2, ("=> write certificate request"));
XiaokangQiancec9ae62022-05-06 07:28:50 +00002666
Gilles Peskine449bd832023-01-11 14:50:10 +01002667 MBEDTLS_SSL_PROC_CHK_NEG(ssl_tls13_certificate_request_coordinate(ssl));
XiaokangQiancec9ae62022-05-06 07:28:50 +00002668
Gilles Peskine449bd832023-01-11 14:50:10 +01002669 if (ret == SSL_CERTIFICATE_REQUEST_SEND_REQUEST) {
XiaokangQiancec9ae62022-05-06 07:28:50 +00002670 unsigned char *buf;
2671 size_t buf_len, msg_len;
2672
Xiaokang Qian9f1747b2023-03-30 02:50:04 +00002673 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_start_handshake_msg(
2674 ssl, MBEDTLS_SSL_HS_CERTIFICATE_REQUEST,
2675 &buf, &buf_len));
XiaokangQiancec9ae62022-05-06 07:28:50 +00002676
Gilles Peskine449bd832023-01-11 14:50:10 +01002677 MBEDTLS_SSL_PROC_CHK(ssl_tls13_write_certificate_request_body(
2678 ssl, buf, buf + buf_len, &msg_len));
XiaokangQiancec9ae62022-05-06 07:28:50 +00002679
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +01002680 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_add_hs_msg_to_checksum(
Xiaokang Qian9f1747b2023-03-30 02:50:04 +00002681 ssl, MBEDTLS_SSL_HS_CERTIFICATE_REQUEST,
2682 buf, msg_len));
XiaokangQiancec9ae62022-05-06 07:28:50 +00002683
Gilles Peskine449bd832023-01-11 14:50:10 +01002684 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_finish_handshake_msg(
2685 ssl, buf_len, msg_len));
2686 } else if (ret == SSL_CERTIFICATE_REQUEST_SKIP) {
2687 MBEDTLS_SSL_DEBUG_MSG(2, ("<= skip write certificate request"));
XiaokangQiancec9ae62022-05-06 07:28:50 +00002688 ret = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01002689 } else {
2690 MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
XiaokangQiancec9ae62022-05-06 07:28:50 +00002691 ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
2692 goto cleanup;
2693 }
2694
Gilles Peskine449bd832023-01-11 14:50:10 +01002695 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_SERVER_CERTIFICATE);
XiaokangQiancec9ae62022-05-06 07:28:50 +00002696cleanup:
2697
Gilles Peskine449bd832023-01-11 14:50:10 +01002698 MBEDTLS_SSL_DEBUG_MSG(2, ("<= write certificate request"));
2699 return ret;
XiaokangQiancec9ae62022-05-06 07:28:50 +00002700}
XiaokangQiancec9ae62022-05-06 07:28:50 +00002701
Jerry Yu4d3841a2022-04-16 12:37:19 +08002702/*
Jerry Yuc6e6dbf2022-04-16 19:42:57 +08002703 * Handler for MBEDTLS_SSL_SERVER_CERTIFICATE
Jerry Yu83da34e2022-04-16 13:59:52 +08002704 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002705MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002706static int ssl_tls13_write_server_certificate(mbedtls_ssl_context *ssl)
Jerry Yu83da34e2022-04-16 13:59:52 +08002707{
Jerry Yu5a26f302022-05-10 20:46:40 +08002708 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
XiaokangQianfb665a82022-06-15 03:57:21 +00002709
2710#if defined(MBEDTLS_X509_CRT_PARSE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01002711 if ((ssl_tls13_pick_key_cert(ssl) != 0) ||
2712 mbedtls_ssl_own_cert(ssl) == NULL) {
2713 MBEDTLS_SSL_DEBUG_MSG(2, ("No certificate available."));
2714 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE,
2715 MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE);
2716 return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
Jerry Yu5a26f302022-05-10 20:46:40 +08002717 }
XiaokangQianfb665a82022-06-15 03:57:21 +00002718#endif /* MBEDTLS_X509_CRT_PARSE_C */
Jerry Yu5a26f302022-05-10 20:46:40 +08002719
Gilles Peskine449bd832023-01-11 14:50:10 +01002720 ret = mbedtls_ssl_tls13_write_certificate(ssl);
2721 if (ret != 0) {
2722 return ret;
2723 }
2724 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_CERTIFICATE_VERIFY);
2725 return 0;
Jerry Yu83da34e2022-04-16 13:59:52 +08002726}
2727
2728/*
Jerry Yuc6e6dbf2022-04-16 19:42:57 +08002729 * Handler for MBEDTLS_SSL_CERTIFICATE_VERIFY
Jerry Yu83da34e2022-04-16 13:59:52 +08002730 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002731MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002732static int ssl_tls13_write_certificate_verify(mbedtls_ssl_context *ssl)
Jerry Yu83da34e2022-04-16 13:59:52 +08002733{
Gilles Peskine449bd832023-01-11 14:50:10 +01002734 int ret = mbedtls_ssl_tls13_write_certificate_verify(ssl);
2735 if (ret != 0) {
2736 return ret;
2737 }
2738 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_SERVER_FINISHED);
2739 return 0;
Jerry Yu83da34e2022-04-16 13:59:52 +08002740}
Ronald Cron928cbd32022-10-04 16:14:26 +02002741#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED */
Jerry Yu83da34e2022-04-16 13:59:52 +08002742
Jerry Yu9b72e392023-12-01 16:27:08 +08002743/*
2744 * RFC 8446 section A.2
2745 *
2746 * | Send ServerHello
2747 * | K_send = handshake
2748 * | Send EncryptedExtensions
2749 * | [Send CertificateRequest]
2750 * Can send | [Send Certificate + CertificateVerify]
2751 * app data | Send Finished
2752 * after --> | K_send = application
2753 * here +--------+--------+
2754 * No 0-RTT | | 0-RTT
2755 * | |
2756 * K_recv = handshake | | K_recv = early data
2757 * [Skip decrypt errors] | +------> WAIT_EOED -+
2758 * | | Recv | | Recv EndOfEarlyData
2759 * | | early data | | K_recv = handshake
2760 * | +------------+ |
2761 * | |
2762 * +> WAIT_FLIGHT2 <--------+
2763 * |
2764 * +--------+--------+
2765 * No auth | | Client auth
2766 * | |
2767 * | v
2768 * | WAIT_CERT
2769 * | Recv | | Recv Certificate
2770 * | empty | v
2771 * | Certificate | WAIT_CV
2772 * | | | Recv
2773 * | v | CertificateVerify
2774 * +-> WAIT_FINISHED <---+
2775 * | Recv Finished
2776 *
2777 *
2778 * The following function handles the state changes after WAIT_FLIGHT2 in the
Jerry Yu3be85072023-12-04 09:58:54 +08002779 * above diagram. We are not going to receive early data related messages
2780 * anymore, prepare to receive the first handshake message of the client
2781 * second flight.
Jerry Yu9b72e392023-12-01 16:27:08 +08002782 */
Jerry Yu3be85072023-12-04 09:58:54 +08002783static void ssl_tls13_prepare_for_handshake_second_flight(
2784 mbedtls_ssl_context *ssl)
Jerry Yu9b72e392023-12-01 16:27:08 +08002785{
Jerry Yu9b72e392023-12-01 16:27:08 +08002786 if (ssl->handshake->certificate_request_sent) {
2787 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_CLIENT_CERTIFICATE);
2788 } else {
Jerry Yu42020fb2023-12-05 17:35:53 +08002789 MBEDTLS_SSL_DEBUG_MSG(2, ("skip parse certificate"));
Jerry Yuebb1b1d2023-12-05 11:02:15 +08002790 MBEDTLS_SSL_DEBUG_MSG(2, ("skip parse certificate verify"));
Jerry Yuebb1b1d2023-12-05 11:02:15 +08002791
Jerry Yu9b72e392023-12-01 16:27:08 +08002792 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_CLIENT_FINISHED);
2793 }
Jerry Yu9b72e392023-12-01 16:27:08 +08002794}
2795
Jerry Yu83da34e2022-04-16 13:59:52 +08002796/*
Jerry Yud6e253d2022-05-18 13:59:24 +08002797 * Handler for MBEDTLS_SSL_SERVER_FINISHED
Jerry Yu69dd8d42022-04-16 12:51:26 +08002798 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002799MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002800static int ssl_tls13_write_server_finished(mbedtls_ssl_context *ssl)
Jerry Yu69dd8d42022-04-16 12:51:26 +08002801{
Jerry Yud6e253d2022-05-18 13:59:24 +08002802 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yu27bdc7c2022-04-16 13:33:27 +08002803
Gilles Peskine449bd832023-01-11 14:50:10 +01002804 ret = mbedtls_ssl_tls13_write_finished_message(ssl);
2805 if (ret != 0) {
2806 return ret;
2807 }
Jerry Yu27bdc7c2022-04-16 13:33:27 +08002808
Gilles Peskine449bd832023-01-11 14:50:10 +01002809 ret = mbedtls_ssl_tls13_compute_application_transform(ssl);
2810 if (ret != 0) {
Jerry Yue3d67cb2022-05-19 15:33:10 +08002811 MBEDTLS_SSL_PEND_FATAL_ALERT(
Gilles Peskine449bd832023-01-11 14:50:10 +01002812 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE,
2813 MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE);
2814 return ret;
Jerry Yue3d67cb2022-05-19 15:33:10 +08002815 }
XiaokangQianc3017f62022-05-13 05:55:41 +00002816
Jerry Yu87b5ed42022-12-12 13:07:07 +08002817#if defined(MBEDTLS_SSL_EARLY_DATA)
Ronald Cron78a38f62024-02-01 18:30:31 +01002818 if (ssl->handshake->early_data_accepted) {
Jerry Yu0af63dc2023-12-01 17:14:51 +08002819 /* See RFC 8446 section A.2 for more information */
Jerry Yu87b5ed42022-12-12 13:07:07 +08002820 MBEDTLS_SSL_DEBUG_MSG(
2821 1, ("Switch to early keys for inbound traffic. "
2822 "( K_recv = early data )"));
2823 mbedtls_ssl_set_inbound_transform(
2824 ssl, ssl->handshake->transform_earlydata);
2825 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_END_OF_EARLY_DATA);
2826 return 0;
2827 }
2828#endif /* MBEDTLS_SSL_EARLY_DATA */
Jerry Yu0af63dc2023-12-01 17:14:51 +08002829 MBEDTLS_SSL_DEBUG_MSG(
2830 1, ("Switch to handshake keys for inbound traffic "
2831 "( K_recv = handshake )"));
Gilles Peskine449bd832023-01-11 14:50:10 +01002832 mbedtls_ssl_set_inbound_transform(ssl, ssl->handshake->transform_handshake);
XiaokangQian189ded22022-05-10 08:12:17 +00002833
Jerry Yu3be85072023-12-04 09:58:54 +08002834 ssl_tls13_prepare_for_handshake_second_flight(ssl);
Jerry Yu7d8c3fe2022-12-12 12:59:44 +08002835
2836 return 0;
2837}
2838
Jerry Yu87b5ed42022-12-12 13:07:07 +08002839#if defined(MBEDTLS_SSL_EARLY_DATA)
2840/*
Jerry Yu59d420f2023-12-01 16:30:34 +08002841 * Handler for MBEDTLS_SSL_END_OF_EARLY_DATA
Jerry Yu87b5ed42022-12-12 13:07:07 +08002842 */
Jerry Yu3be85072023-12-04 09:58:54 +08002843#define SSL_GOT_END_OF_EARLY_DATA 0
Jerry Yub55f9eb2023-12-05 10:27:17 +08002844#define SSL_GOT_EARLY_DATA 1
Jerry Yud5c34962023-12-01 16:32:31 +08002845/* Coordination:
Jerry Yu3be85072023-12-04 09:58:54 +08002846 * Deals with the ambiguity of not knowing if the next message is an
2847 * EndOfEarlyData message or an application message containing early data.
Jerry Yud5c34962023-12-01 16:32:31 +08002848 * Returns a negative code on failure, or
Jerry Yu3be85072023-12-04 09:58:54 +08002849 * - SSL_GOT_END_OF_EARLY_DATA
Jerry Yub55f9eb2023-12-05 10:27:17 +08002850 * - SSL_GOT_EARLY_DATA
Jerry Yud5c34962023-12-01 16:32:31 +08002851 * indicating which message is received.
2852 */
2853MBEDTLS_CHECK_RETURN_CRITICAL
2854static int ssl_tls13_end_of_early_data_coordinate(mbedtls_ssl_context *ssl)
2855{
2856 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yub4ed4602023-12-01 16:34:00 +08002857
2858 if ((ret = mbedtls_ssl_read_record(ssl, 0)) != 0) {
2859 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_read_record", ret);
2860 return ret;
2861 }
2862 ssl->keep_current_message = 1;
2863
2864 if (ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
2865 ssl->in_msg[0] == MBEDTLS_SSL_HS_END_OF_EARLY_DATA) {
Jerry Yub55f9eb2023-12-05 10:27:17 +08002866 MBEDTLS_SSL_DEBUG_MSG(3, ("Received an end_of_early_data message."));
Jerry Yu3be85072023-12-04 09:58:54 +08002867 return SSL_GOT_END_OF_EARLY_DATA;
Jerry Yub4ed4602023-12-01 16:34:00 +08002868 }
2869
2870 if (ssl->in_msgtype == MBEDTLS_SSL_MSG_APPLICATION_DATA) {
Jerry Yub55f9eb2023-12-05 10:27:17 +08002871 MBEDTLS_SSL_DEBUG_MSG(3, ("Received early data"));
Jerry Yu6a5904d2023-12-06 17:11:12 +08002872 /* RFC 8446 section 4.6.1
2873 *
2874 * A server receiving more than max_early_data_size bytes of 0-RTT data
2875 * SHOULD terminate the connection with an "unexpected_message" alert.
2876 *
2877 * TODO: Add received data size check here.
2878 */
Ronald Croned7d4bf2024-01-31 07:55:19 +01002879 if (ssl->in_offt == NULL) {
2880 /* Set the reading pointer */
2881 ssl->in_offt = ssl->in_msg;
2882 }
Jerry Yub55f9eb2023-12-05 10:27:17 +08002883 return SSL_GOT_EARLY_DATA;
Jerry Yub4ed4602023-12-01 16:34:00 +08002884 }
2885
Jerry Yu7bb40a32023-12-04 10:04:15 +08002886 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE,
2887 MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE);
Jerry Yub4ed4602023-12-01 16:34:00 +08002888 return MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE;
Jerry Yud5c34962023-12-01 16:32:31 +08002889}
2890
2891MBEDTLS_CHECK_RETURN_CRITICAL
2892static int ssl_tls13_parse_end_of_early_data(mbedtls_ssl_context *ssl,
2893 const unsigned char *buf,
2894 const unsigned char *end)
2895{
Jerry Yu75c9ab72023-12-01 16:41:40 +08002896 /* RFC 8446 section 4.5
2897 *
2898 * struct {} EndOfEarlyData;
2899 */
Jerry Yufbf03992023-12-04 10:00:37 +08002900 if (buf != end) {
2901 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR,
2902 MBEDTLS_ERR_SSL_DECODE_ERROR);
2903 return MBEDTLS_ERR_SSL_DECODE_ERROR;
2904 }
2905 return 0;
Jerry Yud5c34962023-12-01 16:32:31 +08002906}
2907
Jerry Yud5c34962023-12-01 16:32:31 +08002908/*
2909 * RFC 8446 section A.2
2910 *
2911 * | Send ServerHello
2912 * | K_send = handshake
2913 * | Send EncryptedExtensions
2914 * | [Send CertificateRequest]
2915 * Can send | [Send Certificate + CertificateVerify]
2916 * app data | Send Finished
2917 * after --> | K_send = application
2918 * here +--------+--------+
2919 * No 0-RTT | | 0-RTT
2920 * | |
2921 * K_recv = handshake | | K_recv = early data
2922 * [Skip decrypt errors] | +------> WAIT_EOED -+
2923 * | | Recv | | Recv EndOfEarlyData
2924 * | | early data | | K_recv = handshake
2925 * | +------------+ |
2926 * | |
2927 * +> WAIT_FLIGHT2 <--------+
2928 * |
2929 * +--------+--------+
2930 * No auth | | Client auth
2931 * | |
2932 * | v
2933 * | WAIT_CERT
2934 * | Recv | | Recv Certificate
2935 * | empty | v
2936 * | Certificate | WAIT_CV
2937 * | | | Recv
2938 * | v | CertificateVerify
2939 * +-> WAIT_FINISHED <---+
2940 * | Recv Finished
2941 *
2942 * The function handles actions and state changes from 0-RTT to WAIT_FLIGHT2 in
2943 * the above diagram.
2944 */
Jerry Yu87b5ed42022-12-12 13:07:07 +08002945MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yu59d420f2023-12-01 16:30:34 +08002946static int ssl_tls13_process_end_of_early_data(mbedtls_ssl_context *ssl)
Jerry Yu87b5ed42022-12-12 13:07:07 +08002947{
2948 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yud5c34962023-12-01 16:32:31 +08002949
2950 MBEDTLS_SSL_DEBUG_MSG(2, ("=> ssl_tls13_process_end_of_early_data"));
2951
2952 MBEDTLS_SSL_PROC_CHK_NEG(ssl_tls13_end_of_early_data_coordinate(ssl));
2953
Jerry Yu3be85072023-12-04 09:58:54 +08002954 if (ret == SSL_GOT_END_OF_EARLY_DATA) {
Jerry Yud5c34962023-12-01 16:32:31 +08002955 unsigned char *buf;
2956 size_t buf_len;
2957
2958 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_tls13_fetch_handshake_msg(
2959 ssl, MBEDTLS_SSL_HS_END_OF_EARLY_DATA,
2960 &buf, &buf_len));
2961
2962 MBEDTLS_SSL_PROC_CHK(ssl_tls13_parse_end_of_early_data(
2963 ssl, buf, buf + buf_len));
2964
Jerry Yue9655122023-12-01 16:44:40 +08002965 MBEDTLS_SSL_DEBUG_MSG(
2966 1, ("Switch to handshake keys for inbound traffic"
2967 "( K_recv = handshake )"));
2968 mbedtls_ssl_set_inbound_transform(
2969 ssl, ssl->handshake->transform_handshake);
2970
Jerry Yud5c34962023-12-01 16:32:31 +08002971 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_add_hs_msg_to_checksum(
2972 ssl, MBEDTLS_SSL_HS_END_OF_EARLY_DATA,
2973 buf, buf_len));
Jerry Yue9655122023-12-01 16:44:40 +08002974
Jerry Yu3be85072023-12-04 09:58:54 +08002975 ssl_tls13_prepare_for_handshake_second_flight(ssl);
Jerry Yud5c34962023-12-01 16:32:31 +08002976
Jerry Yub55f9eb2023-12-05 10:27:17 +08002977 } else if (ret == SSL_GOT_EARLY_DATA) {
Jerry Yud9ca3542023-12-06 17:23:52 +08002978 ret = MBEDTLS_ERR_SSL_RECEIVED_EARLY_DATA;
2979 goto cleanup;
Jerry Yud5c34962023-12-01 16:32:31 +08002980 } else {
2981 MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
2982 ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
2983 goto cleanup;
2984 }
2985
Jerry Yud5c34962023-12-01 16:32:31 +08002986cleanup:
2987 MBEDTLS_SSL_DEBUG_MSG(2, ("<= ssl_tls13_process_end_of_early_data"));
Jerry Yu87b5ed42022-12-12 13:07:07 +08002988 return ret;
2989}
2990#endif /* MBEDTLS_SSL_EARLY_DATA */
2991
Jerry Yu69dd8d42022-04-16 12:51:26 +08002992/*
Jerry Yud6e253d2022-05-18 13:59:24 +08002993 * Handler for MBEDTLS_SSL_CLIENT_FINISHED
Jerry Yu69dd8d42022-04-16 12:51:26 +08002994 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002995MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002996static int ssl_tls13_process_client_finished(mbedtls_ssl_context *ssl)
Jerry Yu69dd8d42022-04-16 12:51:26 +08002997{
Jerry Yud6e253d2022-05-18 13:59:24 +08002998 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
2999
Gilles Peskine449bd832023-01-11 14:50:10 +01003000 ret = mbedtls_ssl_tls13_process_finished_message(ssl);
3001 if (ret != 0) {
3002 return ret;
Jerry Yue3d67cb2022-05-19 15:33:10 +08003003 }
3004
Gilles Peskine449bd832023-01-11 14:50:10 +01003005 ret = mbedtls_ssl_tls13_compute_resumption_master_secret(ssl);
3006 if (ret != 0) {
Xiaokang Qian9f1747b2023-03-30 02:50:04 +00003007 MBEDTLS_SSL_DEBUG_RET(
3008 1, "mbedtls_ssl_tls13_compute_resumption_master_secret", ret);
Gilles Peskine449bd832023-01-11 14:50:10 +01003009 }
3010
3011 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_HANDSHAKE_WRAPUP);
3012 return 0;
Jerry Yu69dd8d42022-04-16 12:51:26 +08003013}
3014
3015/*
Jerry Yud6e253d2022-05-18 13:59:24 +08003016 * Handler for MBEDTLS_SSL_HANDSHAKE_WRAPUP
Jerry Yu69dd8d42022-04-16 12:51:26 +08003017 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02003018MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01003019static int ssl_tls13_handshake_wrapup(mbedtls_ssl_context *ssl)
Jerry Yu69dd8d42022-04-16 12:51:26 +08003020{
Gilles Peskine449bd832023-01-11 14:50:10 +01003021 MBEDTLS_SSL_DEBUG_MSG(2, ("handshake: done"));
Jerry Yu03ed50b2022-04-16 17:13:30 +08003022
Gilles Peskine449bd832023-01-11 14:50:10 +01003023 mbedtls_ssl_tls13_handshake_wrapup(ssl);
Jerry Yue67bef42022-07-07 07:29:42 +00003024
Pengyu Lv3643fdb2023-01-12 16:46:28 +08003025#if defined(MBEDTLS_SSL_SESSION_TICKETS) && \
3026 defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED)
Pengyu Lva1aa31b2022-12-13 13:49:59 +08003027/* TODO: Remove the check of SOME_PSK_ENABLED since SESSION_TICKETS requires
3028 * SOME_PSK_ENABLED to be enabled. Here is just to make CI happy. It is
3029 * expected to be resolved with issue#6395.
3030 */
Pengyu Lvc7af2c42022-12-01 16:33:00 +08003031 /* Sent NewSessionTicket message only when client supports PSK */
Pengyu Lvb2cfafb2023-11-14 13:56:13 +08003032 if (mbedtls_ssl_tls13_is_some_psk_supported(ssl)) {
Xiaokang Qian9f1747b2023-03-30 02:50:04 +00003033 mbedtls_ssl_handshake_set_state(
3034 ssl, MBEDTLS_SSL_TLS1_3_NEW_SESSION_TICKET);
Pengyu Lvc7af2c42022-12-01 16:33:00 +08003035 } else
Jerry Yufca4d572022-07-21 10:37:48 +08003036#endif
Pengyu Lv3643fdb2023-01-12 16:46:28 +08003037 {
3038 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_HANDSHAKE_OVER);
3039 }
Gilles Peskine449bd832023-01-11 14:50:10 +01003040 return 0;
Jerry Yu69dd8d42022-04-16 12:51:26 +08003041}
3042
3043/*
Jerry Yua8d3c502022-10-30 14:51:23 +08003044 * Handler for MBEDTLS_SSL_TLS1_3_NEW_SESSION_TICKET
Jerry Yue67bef42022-07-07 07:29:42 +00003045 */
3046#define SSL_NEW_SESSION_TICKET_SKIP 0
3047#define SSL_NEW_SESSION_TICKET_WRITE 1
3048MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01003049static int ssl_tls13_write_new_session_ticket_coordinate(mbedtls_ssl_context *ssl)
Jerry Yue67bef42022-07-07 07:29:42 +00003050{
3051 /* Check whether the use of session tickets is enabled */
Gilles Peskine449bd832023-01-11 14:50:10 +01003052 if (ssl->conf->f_ticket_write == NULL) {
3053 MBEDTLS_SSL_DEBUG_MSG(2, ("NewSessionTicket: disabled,"
3054 " callback is not set"));
3055 return SSL_NEW_SESSION_TICKET_SKIP;
Jerry Yud0766ec2022-09-22 10:46:57 +08003056 }
Gilles Peskine449bd832023-01-11 14:50:10 +01003057 if (ssl->conf->new_session_tickets_count == 0) {
3058 MBEDTLS_SSL_DEBUG_MSG(2, ("NewSessionTicket: disabled,"
3059 " configured count is zero"));
3060 return SSL_NEW_SESSION_TICKET_SKIP;
Jerry Yud0766ec2022-09-22 10:46:57 +08003061 }
3062
Gilles Peskine449bd832023-01-11 14:50:10 +01003063 if (ssl->handshake->new_session_tickets_count == 0) {
3064 MBEDTLS_SSL_DEBUG_MSG(2, ("NewSessionTicket: all tickets have "
3065 "been sent."));
3066 return SSL_NEW_SESSION_TICKET_SKIP;
Jerry Yue67bef42022-07-07 07:29:42 +00003067 }
3068
Gilles Peskine449bd832023-01-11 14:50:10 +01003069 return SSL_NEW_SESSION_TICKET_WRITE;
Jerry Yue67bef42022-07-07 07:29:42 +00003070}
3071
3072#if defined(MBEDTLS_SSL_SESSION_TICKETS)
3073MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01003074static int ssl_tls13_prepare_new_session_ticket(mbedtls_ssl_context *ssl,
3075 unsigned char *ticket_nonce,
3076 size_t ticket_nonce_size)
Jerry Yue67bef42022-07-07 07:29:42 +00003077{
3078 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
3079 mbedtls_ssl_session *session = ssl->session;
3080 mbedtls_ssl_ciphersuite_t *ciphersuite_info;
3081 psa_algorithm_t psa_hash_alg;
3082 int hash_length;
3083
Gilles Peskine449bd832023-01-11 14:50:10 +01003084 MBEDTLS_SSL_DEBUG_MSG(2, ("=> prepare NewSessionTicket msg"));
Jerry Yue67bef42022-07-07 07:29:42 +00003085
Pengyu Lv9f926952022-11-17 15:22:33 +08003086 /* Set ticket_flags depends on the advertised psk key exchange mode */
Pengyu Lv94a42cc2023-12-06 10:04:17 +08003087 mbedtls_ssl_tls13_session_clear_ticket_flags(
Pengyu Lv9eacb442022-12-09 14:39:19 +08003088 session, MBEDTLS_SSL_TLS1_3_TICKET_FLAGS_MASK);
Pengyu Lve6487fe2022-12-06 09:30:29 +08003089#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED)
Pengyu Lv94a42cc2023-12-06 10:04:17 +08003090 mbedtls_ssl_tls13_session_set_ticket_flags(
Pengyu Lv9eacb442022-12-09 14:39:19 +08003091 session, ssl->handshake->tls13_kex_modes);
Pengyu Lve6487fe2022-12-06 09:30:29 +08003092#endif
Jerry Yu95648b02023-12-06 15:03:34 +08003093
3094#if defined(MBEDTLS_SSL_EARLY_DATA)
3095 if (ssl->conf->early_data_enabled == MBEDTLS_SSL_EARLY_DATA_ENABLED &&
3096 ssl->conf->max_early_data_size > 0) {
Pengyu Lv94a42cc2023-12-06 10:04:17 +08003097 mbedtls_ssl_tls13_session_set_ticket_flags(
Jerry Yu95648b02023-12-06 15:03:34 +08003098 session, MBEDTLS_SSL_TLS1_3_TICKET_ALLOW_EARLY_DATA);
3099 }
3100#endif /* MBEDTLS_SSL_EARLY_DATA */
3101
Pengyu Lv4938a562023-01-16 11:28:49 +08003102 MBEDTLS_SSL_PRINT_TICKET_FLAGS(4, session->ticket_flags);
Pengyu Lv9f926952022-11-17 15:22:33 +08003103
Jerry Yue67bef42022-07-07 07:29:42 +00003104 /* Generate ticket_age_add */
Gilles Peskine449bd832023-01-11 14:50:10 +01003105 if ((ret = ssl->conf->f_rng(ssl->conf->p_rng,
3106 (unsigned char *) &session->ticket_age_add,
3107 sizeof(session->ticket_age_add)) != 0)) {
3108 MBEDTLS_SSL_DEBUG_RET(1, "generate_ticket_age_add", ret);
3109 return ret;
Jerry Yue67bef42022-07-07 07:29:42 +00003110 }
Gilles Peskine449bd832023-01-11 14:50:10 +01003111 MBEDTLS_SSL_DEBUG_MSG(3, ("ticket_age_add: %u",
3112 (unsigned int) session->ticket_age_add));
Jerry Yue67bef42022-07-07 07:29:42 +00003113
3114 /* Generate ticket_nonce */
Gilles Peskine449bd832023-01-11 14:50:10 +01003115 ret = ssl->conf->f_rng(ssl->conf->p_rng, ticket_nonce, ticket_nonce_size);
3116 if (ret != 0) {
3117 MBEDTLS_SSL_DEBUG_RET(1, "generate_ticket_nonce", ret);
3118 return ret;
Jerry Yue67bef42022-07-07 07:29:42 +00003119 }
Gilles Peskine449bd832023-01-11 14:50:10 +01003120 MBEDTLS_SSL_DEBUG_BUF(3, "ticket_nonce:",
3121 ticket_nonce, ticket_nonce_size);
Jerry Yue67bef42022-07-07 07:29:42 +00003122
3123 ciphersuite_info =
Gilles Peskine449bd832023-01-11 14:50:10 +01003124 (mbedtls_ssl_ciphersuite_t *) ssl->handshake->ciphersuite_info;
Dave Rodgman2eab4622023-10-05 13:30:37 +01003125 psa_hash_alg = mbedtls_md_psa_alg_from_type((mbedtls_md_type_t) ciphersuite_info->mac);
Gilles Peskine449bd832023-01-11 14:50:10 +01003126 hash_length = PSA_HASH_LENGTH(psa_hash_alg);
3127 if (hash_length == -1 ||
3128 (size_t) hash_length > sizeof(session->resumption_key)) {
3129 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Jerry Yufca4d572022-07-21 10:37:48 +08003130 }
3131
Jerry Yue67bef42022-07-07 07:29:42 +00003132 /* In this code the psk key length equals the length of the hash */
3133 session->resumption_key_len = hash_length;
3134 session->ciphersuite = ciphersuite_info->id;
3135
3136 /* Compute resumption key
3137 *
3138 * HKDF-Expand-Label( resumption_master_secret,
3139 * "resumption", ticket_nonce, Hash.length )
3140 */
3141 ret = mbedtls_ssl_tls13_hkdf_expand_label(
Gilles Peskine449bd832023-01-11 14:50:10 +01003142 psa_hash_alg,
3143 session->app_secrets.resumption_master_secret,
3144 hash_length,
3145 MBEDTLS_SSL_TLS1_3_LBL_WITH_LEN(resumption),
3146 ticket_nonce,
3147 ticket_nonce_size,
3148 session->resumption_key,
3149 hash_length);
Jerry Yue67bef42022-07-07 07:29:42 +00003150
Gilles Peskine449bd832023-01-11 14:50:10 +01003151 if (ret != 0) {
3152 MBEDTLS_SSL_DEBUG_RET(2,
3153 "Creating the ticket-resumed PSK failed",
3154 ret);
3155 return ret;
Jerry Yue67bef42022-07-07 07:29:42 +00003156 }
Gilles Peskine449bd832023-01-11 14:50:10 +01003157 MBEDTLS_SSL_DEBUG_BUF(3, "Ticket-resumed PSK",
3158 session->resumption_key,
3159 session->resumption_key_len);
Jerry Yue67bef42022-07-07 07:29:42 +00003160
Gilles Peskine449bd832023-01-11 14:50:10 +01003161 MBEDTLS_SSL_DEBUG_BUF(3, "resumption_master_secret",
3162 session->app_secrets.resumption_master_secret,
3163 hash_length);
Jerry Yue67bef42022-07-07 07:29:42 +00003164
Gilles Peskine449bd832023-01-11 14:50:10 +01003165 return 0;
Jerry Yue67bef42022-07-07 07:29:42 +00003166}
3167
3168/* This function creates a NewSessionTicket message in the following format:
3169 *
3170 * struct {
3171 * uint32 ticket_lifetime;
3172 * uint32 ticket_age_add;
3173 * opaque ticket_nonce<0..255>;
3174 * opaque ticket<1..2^16-1>;
3175 * Extension extensions<0..2^16-2>;
3176 * } NewSessionTicket;
3177 *
3178 * The ticket inside the NewSessionTicket message is an encrypted container
3179 * carrying the necessary information so that the server is later able to
3180 * re-start the communication.
3181 *
3182 * The following fields are placed inside the ticket by the
3183 * f_ticket_write() function:
3184 *
Jerry Yu0069abc2023-11-23 21:07:28 +08003185 * - creation time (ticket_creation_time)
3186 * - flags (ticket_flags)
Jerry Yue67bef42022-07-07 07:29:42 +00003187 * - age add (ticket_age_add)
Jerry Yu0069abc2023-11-23 21:07:28 +08003188 * - key (resumption_key)
3189 * - key length (resumption_key_len)
Jerry Yue67bef42022-07-07 07:29:42 +00003190 * - ciphersuite (ciphersuite)
Jerry Yu0069abc2023-11-23 21:07:28 +08003191 * - max_early_data_size (max_early_data_size)
Jerry Yue67bef42022-07-07 07:29:42 +00003192 */
3193MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01003194static int ssl_tls13_write_new_session_ticket_body(mbedtls_ssl_context *ssl,
3195 unsigned char *buf,
3196 unsigned char *end,
3197 size_t *out_len,
3198 unsigned char *ticket_nonce,
3199 size_t ticket_nonce_size)
Jerry Yue67bef42022-07-07 07:29:42 +00003200{
3201 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
3202 unsigned char *p = buf;
3203 mbedtls_ssl_session *session = ssl->session;
3204 size_t ticket_len;
3205 uint32_t ticket_lifetime;
Jerry Yu01da35e2022-12-12 15:09:22 +08003206 unsigned char *p_extensions_len;
Jerry Yue67bef42022-07-07 07:29:42 +00003207
3208 *out_len = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01003209 MBEDTLS_SSL_DEBUG_MSG(2, ("=> write NewSessionTicket msg"));
Jerry Yue67bef42022-07-07 07:29:42 +00003210
3211 /*
3212 * ticket_lifetime 4 bytes
3213 * ticket_age_add 4 bytes
3214 * ticket_nonce 1 + ticket_nonce_size bytes
3215 * ticket >=2 bytes
3216 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003217 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 4 + 4 + 1 + ticket_nonce_size + 2);
Jerry Yue67bef42022-07-07 07:29:42 +00003218
3219 /* Generate ticket and ticket_lifetime */
Ronald Cron3c0072b2023-11-22 10:00:14 +01003220#if defined(MBEDTLS_HAVE_TIME)
3221 session->ticket_creation_time = mbedtls_ms_time();
3222#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01003223 ret = ssl->conf->f_ticket_write(ssl->conf->p_ticket,
3224 session,
3225 p + 9 + ticket_nonce_size + 2,
3226 end,
3227 &ticket_len,
3228 &ticket_lifetime);
3229 if (ret != 0) {
3230 MBEDTLS_SSL_DEBUG_RET(1, "write_ticket", ret);
3231 return ret;
Jerry Yue67bef42022-07-07 07:29:42 +00003232 }
3233 /* RFC 8446 4.6.1
3234 * ticket_lifetime: Indicates the lifetime in seconds as a 32-bit
3235 * unsigned integer in network byte order from the time of ticket
3236 * issuance. Servers MUST NOT use any value greater than
3237 * 604800 seconds (7 days). The value of zero indicates that the
3238 * ticket should be discarded immediately. Clients MUST NOT cache
3239 * tickets for longer than 7 days, regardless of the ticket_lifetime,
3240 * and MAY delete tickets earlier based on local policy. A server
3241 * MAY treat a ticket as valid for a shorter period of time than what
3242 * is stated in the ticket_lifetime.
3243 */
Jerry Yu8e0174a2023-11-10 13:58:16 +08003244 if (ticket_lifetime > MBEDTLS_SSL_TLS1_3_MAX_ALLOWED_TICKET_LIFETIME) {
3245 ticket_lifetime = MBEDTLS_SSL_TLS1_3_MAX_ALLOWED_TICKET_LIFETIME;
Gilles Peskine449bd832023-01-11 14:50:10 +01003246 }
3247 MBEDTLS_PUT_UINT32_BE(ticket_lifetime, p, 0);
3248 MBEDTLS_SSL_DEBUG_MSG(3, ("ticket_lifetime: %u",
3249 (unsigned int) ticket_lifetime));
Jerry Yue67bef42022-07-07 07:29:42 +00003250
3251 /* Write ticket_age_add */
Gilles Peskine449bd832023-01-11 14:50:10 +01003252 MBEDTLS_PUT_UINT32_BE(session->ticket_age_add, p, 4);
3253 MBEDTLS_SSL_DEBUG_MSG(3, ("ticket_age_add: %u",
3254 (unsigned int) session->ticket_age_add));
Jerry Yue67bef42022-07-07 07:29:42 +00003255
3256 /* Write ticket_nonce */
Gilles Peskine449bd832023-01-11 14:50:10 +01003257 p[8] = (unsigned char) ticket_nonce_size;
3258 if (ticket_nonce_size > 0) {
3259 memcpy(p + 9, ticket_nonce, ticket_nonce_size);
Jerry Yue67bef42022-07-07 07:29:42 +00003260 }
3261 p += 9 + ticket_nonce_size;
3262
3263 /* Write ticket */
Gilles Peskine449bd832023-01-11 14:50:10 +01003264 MBEDTLS_PUT_UINT16_BE(ticket_len, p, 0);
Jerry Yue67bef42022-07-07 07:29:42 +00003265 p += 2;
Gilles Peskine449bd832023-01-11 14:50:10 +01003266 MBEDTLS_SSL_DEBUG_BUF(4, "ticket", p, ticket_len);
Jerry Yue67bef42022-07-07 07:29:42 +00003267 p += ticket_len;
3268
3269 /* Ticket Extensions
3270 *
Jerry Yu01da35e2022-12-12 15:09:22 +08003271 * Extension extensions<0..2^16-2>;
Jerry Yue67bef42022-07-07 07:29:42 +00003272 */
Jerry Yuedab6372022-10-31 14:37:31 +08003273 ssl->handshake->sent_extensions = MBEDTLS_SSL_EXT_MASK_NONE;
3274
Gilles Peskine449bd832023-01-11 14:50:10 +01003275 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 2);
Jerry Yu01da35e2022-12-12 15:09:22 +08003276 p_extensions_len = p;
Jerry Yue67bef42022-07-07 07:29:42 +00003277 p += 2;
3278
Jerry Yu01da35e2022-12-12 15:09:22 +08003279#if defined(MBEDTLS_SSL_EARLY_DATA)
Pengyu Lv94a42cc2023-12-06 10:04:17 +08003280 if (mbedtls_ssl_tls13_session_ticket_allow_early_data(session)) {
Jerry Yu95648b02023-12-06 15:03:34 +08003281 size_t output_len;
3282
Jerry Yuf135bac2023-11-23 18:10:51 +08003283 if ((ret = mbedtls_ssl_tls13_write_early_data_ext(
Jerry Yuc59c5862023-12-05 10:40:49 +08003284 ssl, 1, p, end, &output_len)) != 0) {
Jerry Yuf135bac2023-11-23 18:10:51 +08003285 MBEDTLS_SSL_DEBUG_RET(
3286 1, "mbedtls_ssl_tls13_write_early_data_ext", ret);
3287 return ret;
3288 }
3289 p += output_len;
Jerry Yu9e7f9bc2023-11-27 16:52:07 +08003290 } else {
3291 MBEDTLS_SSL_DEBUG_MSG(
3292 4, ("early_data not allowed, "
3293 "skip early_data extension in NewSessionTicket"));
Jerry Yu01da35e2022-12-12 15:09:22 +08003294 }
Jerry Yuf135bac2023-11-23 18:10:51 +08003295
Jerry Yu01da35e2022-12-12 15:09:22 +08003296#endif /* MBEDTLS_SSL_EARLY_DATA */
3297
3298 MBEDTLS_PUT_UINT16_BE(p - p_extensions_len - 2, p_extensions_len, 0);
3299
Jerry Yue67bef42022-07-07 07:29:42 +00003300 *out_len = p - buf;
Gilles Peskine449bd832023-01-11 14:50:10 +01003301 MBEDTLS_SSL_DEBUG_BUF(4, "ticket", buf, *out_len);
3302 MBEDTLS_SSL_DEBUG_MSG(2, ("<= write new session ticket"));
Jerry Yue67bef42022-07-07 07:29:42 +00003303
Jerry Yu7de2ff02022-11-08 21:43:46 +08003304 MBEDTLS_SSL_PRINT_EXTS(
Gilles Peskine449bd832023-01-11 14:50:10 +01003305 3, MBEDTLS_SSL_HS_NEW_SESSION_TICKET, ssl->handshake->sent_extensions);
Jerry Yu4b8f2f72022-10-31 13:31:22 +08003306
Gilles Peskine449bd832023-01-11 14:50:10 +01003307 return 0;
Jerry Yue67bef42022-07-07 07:29:42 +00003308}
3309
3310/*
Jerry Yua8d3c502022-10-30 14:51:23 +08003311 * Handler for MBEDTLS_SSL_TLS1_3_NEW_SESSION_TICKET
Jerry Yue67bef42022-07-07 07:29:42 +00003312 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003313static int ssl_tls13_write_new_session_ticket(mbedtls_ssl_context *ssl)
Jerry Yue67bef42022-07-07 07:29:42 +00003314{
3315 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
3316
Gilles Peskine449bd832023-01-11 14:50:10 +01003317 MBEDTLS_SSL_PROC_CHK_NEG(ssl_tls13_write_new_session_ticket_coordinate(ssl));
Jerry Yue67bef42022-07-07 07:29:42 +00003318
Gilles Peskine449bd832023-01-11 14:50:10 +01003319 if (ret == SSL_NEW_SESSION_TICKET_WRITE) {
Jerry Yue67bef42022-07-07 07:29:42 +00003320 unsigned char ticket_nonce[MBEDTLS_SSL_TLS1_3_TICKET_NONCE_LENGTH];
3321 unsigned char *buf;
3322 size_t buf_len, msg_len;
3323
Gilles Peskine449bd832023-01-11 14:50:10 +01003324 MBEDTLS_SSL_PROC_CHK(ssl_tls13_prepare_new_session_ticket(
3325 ssl, ticket_nonce, sizeof(ticket_nonce)));
Jerry Yue67bef42022-07-07 07:29:42 +00003326
Xiaokang Qian9f1747b2023-03-30 02:50:04 +00003327 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_start_handshake_msg(
3328 ssl, MBEDTLS_SSL_HS_NEW_SESSION_TICKET,
3329 &buf, &buf_len));
Jerry Yue67bef42022-07-07 07:29:42 +00003330
Gilles Peskine449bd832023-01-11 14:50:10 +01003331 MBEDTLS_SSL_PROC_CHK(ssl_tls13_write_new_session_ticket_body(
3332 ssl, buf, buf + buf_len, &msg_len,
3333 ticket_nonce, sizeof(ticket_nonce)));
Jerry Yue67bef42022-07-07 07:29:42 +00003334
Gilles Peskine449bd832023-01-11 14:50:10 +01003335 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_finish_handshake_msg(
3336 ssl, buf_len, msg_len));
Jerry Yufca4d572022-07-21 10:37:48 +08003337
Jerry Yu359e65f2022-09-22 23:47:43 +08003338 /* Limit session tickets count to one when resumption connection.
3339 *
3340 * See document of mbedtls_ssl_conf_new_session_tickets.
3341 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003342 if (ssl->handshake->resume == 1) {
Jerry Yu359e65f2022-09-22 23:47:43 +08003343 ssl->handshake->new_session_tickets_count = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01003344 } else {
Jerry Yu359e65f2022-09-22 23:47:43 +08003345 ssl->handshake->new_session_tickets_count--;
Gilles Peskine449bd832023-01-11 14:50:10 +01003346 }
Jerry Yub7e3fa72022-09-22 11:07:18 +08003347
Jerry Yua8d3c502022-10-30 14:51:23 +08003348 mbedtls_ssl_handshake_set_state(
Gilles Peskine449bd832023-01-11 14:50:10 +01003349 ssl, MBEDTLS_SSL_TLS1_3_NEW_SESSION_TICKET_FLUSH);
3350 } else {
3351 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_HANDSHAKE_OVER);
Jerry Yue67bef42022-07-07 07:29:42 +00003352 }
3353
Jerry Yue67bef42022-07-07 07:29:42 +00003354cleanup:
3355
Gilles Peskine449bd832023-01-11 14:50:10 +01003356 return ret;
Jerry Yue67bef42022-07-07 07:29:42 +00003357}
3358#endif /* MBEDTLS_SSL_SESSION_TICKETS */
3359
3360/*
XiaokangQiane8ff3502022-04-22 02:34:40 +00003361 * TLS 1.3 State Machine -- server side
XiaokangQian7807f9f2022-02-15 10:04:37 +00003362 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003363int mbedtls_ssl_tls13_handshake_server_step(mbedtls_ssl_context *ssl)
Jerry Yub9930e72021-08-06 17:11:51 +08003364{
XiaokangQian08037552022-04-20 07:16:41 +00003365 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
XiaokangQian7807f9f2022-02-15 10:04:37 +00003366
Gilles Peskine449bd832023-01-11 14:50:10 +01003367 if (ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER || ssl->handshake == NULL) {
3368 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
3369 }
XiaokangQian7807f9f2022-02-15 10:04:37 +00003370
Gilles Peskine449bd832023-01-11 14:50:10 +01003371 MBEDTLS_SSL_DEBUG_MSG(2, ("tls13 server state: %s(%d)",
Dave Rodgman2eab4622023-10-05 13:30:37 +01003372 mbedtls_ssl_states_str((mbedtls_ssl_states) ssl->state),
Gilles Peskine449bd832023-01-11 14:50:10 +01003373 ssl->state));
Jerry Yu6e81b272021-09-27 11:16:17 +08003374
Gilles Peskine449bd832023-01-11 14:50:10 +01003375 switch (ssl->state) {
XiaokangQian7807f9f2022-02-15 10:04:37 +00003376 /* start state */
3377 case MBEDTLS_SSL_HELLO_REQUEST:
Gilles Peskine449bd832023-01-11 14:50:10 +01003378 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_CLIENT_HELLO);
XiaokangQian08037552022-04-20 07:16:41 +00003379 ret = 0;
XiaokangQian7807f9f2022-02-15 10:04:37 +00003380 break;
3381
XiaokangQian7807f9f2022-02-15 10:04:37 +00003382 case MBEDTLS_SSL_CLIENT_HELLO:
Gilles Peskine449bd832023-01-11 14:50:10 +01003383 ret = ssl_tls13_process_client_hello(ssl);
3384 if (ret != 0) {
3385 MBEDTLS_SSL_DEBUG_RET(1, "ssl_tls13_process_client_hello", ret);
3386 }
Jerry Yuf41553b2022-05-09 22:20:30 +08003387 break;
XiaokangQian7807f9f2022-02-15 10:04:37 +00003388
Jerry Yuf41553b2022-05-09 22:20:30 +08003389 case MBEDTLS_SSL_HELLO_RETRY_REQUEST:
Gilles Peskine449bd832023-01-11 14:50:10 +01003390 ret = ssl_tls13_write_hello_retry_request(ssl);
3391 if (ret != 0) {
3392 MBEDTLS_SSL_DEBUG_RET(1, "ssl_tls13_write_hello_retry_request", ret);
3393 return ret;
Jerry Yuf41553b2022-05-09 22:20:30 +08003394 }
XiaokangQian7807f9f2022-02-15 10:04:37 +00003395 break;
3396
Jerry Yu5b64ae92022-03-30 17:15:02 +08003397 case MBEDTLS_SSL_SERVER_HELLO:
Gilles Peskine449bd832023-01-11 14:50:10 +01003398 ret = ssl_tls13_write_server_hello(ssl);
Jerry Yu5b64ae92022-03-30 17:15:02 +08003399 break;
3400
Xiaofei Baicba64af2022-02-15 10:00:56 +00003401 case MBEDTLS_SSL_ENCRYPTED_EXTENSIONS:
Gilles Peskine449bd832023-01-11 14:50:10 +01003402 ret = ssl_tls13_write_encrypted_extensions(ssl);
3403 if (ret != 0) {
3404 MBEDTLS_SSL_DEBUG_RET(1, "ssl_tls13_write_encrypted_extensions", ret);
3405 return ret;
Xiaofei Baicba64af2022-02-15 10:00:56 +00003406 }
Jerry Yu48330562022-05-06 21:35:44 +08003407 break;
Jerry Yu6a2cd9e2022-05-05 11:14:19 +08003408
Ronald Cron928cbd32022-10-04 16:14:26 +02003409#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED)
Xiaofei Bai9ca09d42022-02-14 12:57:18 +00003410 case MBEDTLS_SSL_CERTIFICATE_REQUEST:
Gilles Peskine449bd832023-01-11 14:50:10 +01003411 ret = ssl_tls13_write_certificate_request(ssl);
Xiaofei Bai9ca09d42022-02-14 12:57:18 +00003412 break;
Xiaofei Bai9ca09d42022-02-14 12:57:18 +00003413
Jerry Yu83da34e2022-04-16 13:59:52 +08003414 case MBEDTLS_SSL_SERVER_CERTIFICATE:
Gilles Peskine449bd832023-01-11 14:50:10 +01003415 ret = ssl_tls13_write_server_certificate(ssl);
Jerry Yu83da34e2022-04-16 13:59:52 +08003416 break;
3417
3418 case MBEDTLS_SSL_CERTIFICATE_VERIFY:
Gilles Peskine449bd832023-01-11 14:50:10 +01003419 ret = ssl_tls13_write_certificate_verify(ssl);
Jerry Yu83da34e2022-04-16 13:59:52 +08003420 break;
Ronald Cron928cbd32022-10-04 16:14:26 +02003421#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED */
Jerry Yu83da34e2022-04-16 13:59:52 +08003422
Gilles Peskine449bd832023-01-11 14:50:10 +01003423 /*
3424 * Injection of dummy-CCS's for middlebox compatibility
3425 */
Gabor Mezei7b39bf12022-05-24 16:04:14 +02003426#if defined(MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE)
Gabor Mezeif7044ea2022-06-28 16:01:49 +02003427 case MBEDTLS_SSL_SERVER_CCS_AFTER_HELLO_RETRY_REQUEST:
Gilles Peskine449bd832023-01-11 14:50:10 +01003428 ret = mbedtls_ssl_tls13_write_change_cipher_spec(ssl);
3429 if (ret == 0) {
3430 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_CLIENT_HELLO);
3431 }
Gabor Mezei7b39bf12022-05-24 16:04:14 +02003432 break;
3433
3434 case MBEDTLS_SSL_SERVER_CCS_AFTER_SERVER_HELLO:
Ronald Crone273f722024-02-13 18:22:26 +01003435 ret = mbedtls_ssl_tls13_write_change_cipher_spec(ssl);
3436 if (ret != 0) {
3437 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01003438 }
Ronald Cronfe59ff72024-01-24 14:31:50 +01003439 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_ENCRYPTED_EXTENSIONS);
Gabor Mezei7b39bf12022-05-24 16:04:14 +02003440 break;
3441#endif /* MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE */
3442
Jerry Yu69dd8d42022-04-16 12:51:26 +08003443 case MBEDTLS_SSL_SERVER_FINISHED:
Gilles Peskine449bd832023-01-11 14:50:10 +01003444 ret = ssl_tls13_write_server_finished(ssl);
Jerry Yu69dd8d42022-04-16 12:51:26 +08003445 break;
3446
Jerry Yu87b5ed42022-12-12 13:07:07 +08003447#if defined(MBEDTLS_SSL_EARLY_DATA)
3448 case MBEDTLS_SSL_END_OF_EARLY_DATA:
Jerry Yu59d420f2023-12-01 16:30:34 +08003449 ret = ssl_tls13_process_end_of_early_data(ssl);
Jerry Yu87b5ed42022-12-12 13:07:07 +08003450 break;
3451#endif /* MBEDTLS_SSL_EARLY_DATA */
3452
Jerry Yu69dd8d42022-04-16 12:51:26 +08003453 case MBEDTLS_SSL_CLIENT_FINISHED:
Gilles Peskine449bd832023-01-11 14:50:10 +01003454 ret = ssl_tls13_process_client_finished(ssl);
Jerry Yu69dd8d42022-04-16 12:51:26 +08003455 break;
3456
Jerry Yu69dd8d42022-04-16 12:51:26 +08003457 case MBEDTLS_SSL_HANDSHAKE_WRAPUP:
Gilles Peskine449bd832023-01-11 14:50:10 +01003458 ret = ssl_tls13_handshake_wrapup(ssl);
Jerry Yu69dd8d42022-04-16 12:51:26 +08003459 break;
3460
Ronald Cron766c0cd2022-10-18 12:17:11 +02003461#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED)
XiaokangQian6b916b12022-04-25 07:29:34 +00003462 case MBEDTLS_SSL_CLIENT_CERTIFICATE:
Gilles Peskine449bd832023-01-11 14:50:10 +01003463 ret = mbedtls_ssl_tls13_process_certificate(ssl);
3464 if (ret == 0) {
3465 if (ssl->session_negotiate->peer_cert != NULL) {
Ronald Cron209cae92022-06-07 10:30:19 +02003466 mbedtls_ssl_handshake_set_state(
Gilles Peskine449bd832023-01-11 14:50:10 +01003467 ssl, MBEDTLS_SSL_CLIENT_CERTIFICATE_VERIFY);
3468 } else {
3469 MBEDTLS_SSL_DEBUG_MSG(2, ("skip parse certificate verify"));
Ronald Cron209cae92022-06-07 10:30:19 +02003470 mbedtls_ssl_handshake_set_state(
Gilles Peskine449bd832023-01-11 14:50:10 +01003471 ssl, MBEDTLS_SSL_CLIENT_FINISHED);
Ronald Cron19385882022-06-15 16:26:13 +02003472 }
XiaokangQian6b916b12022-04-25 07:29:34 +00003473 }
3474 break;
3475
3476 case MBEDTLS_SSL_CLIENT_CERTIFICATE_VERIFY:
Gilles Peskine449bd832023-01-11 14:50:10 +01003477 ret = mbedtls_ssl_tls13_process_certificate_verify(ssl);
3478 if (ret == 0) {
XiaokangQian6b916b12022-04-25 07:29:34 +00003479 mbedtls_ssl_handshake_set_state(
Gilles Peskine449bd832023-01-11 14:50:10 +01003480 ssl, MBEDTLS_SSL_CLIENT_FINISHED);
XiaokangQian6b916b12022-04-25 07:29:34 +00003481 }
3482 break;
Ronald Cron766c0cd2022-10-18 12:17:11 +02003483#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED */
XiaokangQian6b916b12022-04-25 07:29:34 +00003484
Jerry Yue67bef42022-07-07 07:29:42 +00003485#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Jerry Yua8d3c502022-10-30 14:51:23 +08003486 case MBEDTLS_SSL_TLS1_3_NEW_SESSION_TICKET:
Gilles Peskine449bd832023-01-11 14:50:10 +01003487 ret = ssl_tls13_write_new_session_ticket(ssl);
3488 if (ret != 0) {
3489 MBEDTLS_SSL_DEBUG_RET(1,
3490 "ssl_tls13_write_new_session_ticket ",
3491 ret);
Jerry Yue67bef42022-07-07 07:29:42 +00003492 }
3493 break;
Jerry Yua8d3c502022-10-30 14:51:23 +08003494 case MBEDTLS_SSL_TLS1_3_NEW_SESSION_TICKET_FLUSH:
Jerry Yue67bef42022-07-07 07:29:42 +00003495 /* This state is necessary to do the flush of the New Session
Jerry Yua8d3c502022-10-30 14:51:23 +08003496 * Ticket message written in MBEDTLS_SSL_TLS1_3_NEW_SESSION_TICKET
Jerry Yue67bef42022-07-07 07:29:42 +00003497 * as part of ssl_prepare_handshake_step.
3498 */
3499 ret = 0;
Jerry Yud4e75002022-08-09 13:33:50 +08003500
Gilles Peskine449bd832023-01-11 14:50:10 +01003501 if (ssl->handshake->new_session_tickets_count == 0) {
3502 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_HANDSHAKE_OVER);
3503 } else {
Xiaokang Qian9f1747b2023-03-30 02:50:04 +00003504 mbedtls_ssl_handshake_set_state(
3505 ssl, MBEDTLS_SSL_TLS1_3_NEW_SESSION_TICKET);
Gilles Peskine449bd832023-01-11 14:50:10 +01003506 }
Jerry Yue67bef42022-07-07 07:29:42 +00003507 break;
3508
3509#endif /* MBEDTLS_SSL_SESSION_TICKETS */
3510
XiaokangQian7807f9f2022-02-15 10:04:37 +00003511 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01003512 MBEDTLS_SSL_DEBUG_MSG(1, ("invalid state %d", ssl->state));
3513 return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
XiaokangQian7807f9f2022-02-15 10:04:37 +00003514 }
3515
Gilles Peskine449bd832023-01-11 14:50:10 +01003516 return ret;
Jerry Yub9930e72021-08-06 17:11:51 +08003517}
Jerry Yu3cc4c2a2021-08-06 16:29:08 +08003518
Jerry Yufb4b6472022-01-27 15:03:26 +08003519#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_PROTO_TLS1_3 */