blob: f7b3401a87279d06a185f64f1a0f3c7ed33e7309 [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 /*
Ronald Cron38117652024-03-05 09:05:46 +010053 * In a compliant ClientHello the byte-length of the list of ciphersuites
54 * is even and this function relies on this fact. This should have been
55 * checked in the main ClientHello parsing function. Double check here.
Ronald Cron89089cc2024-02-14 18:18:08 +010056 */
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 Cron38117652024-03-05 09:05:46 +0100149/*
150 * Non-error return values of
151 * ssl_tls13_offered_psks_check_identity_match_ticket() and
152 * ssl_tls13_offered_psks_check_identity_match(). They are positive to
153 * not collide with error codes that are negative. Zero
154 * (SSL_TLS1_3_PSK_IDENTITY_MATCH) in case of success as it may be propagated
155 * up by the callers of this function as a generic success condition.
156 *
157 * The return value SSL_TLS1_3_PSK_IDENTITY_MATCH_BUT_PSK_NOT_USABLE means
158 * that the pre-shared-key identity matches that of a ticket or an external
159 * provisioned pre-shared-key. We have thus been able to retrieve the
160 * attributes of the pre-shared-key but at least one of them does not meet
161 * some criteria and the pre-shared-key cannot be used. For example, a ticket
162 * is expired or its version is not TLS 1.3. Note eventually that the return
163 * value SSL_TLS1_3_PSK_IDENTITY_MATCH_BUT_PSK_NOT_USABLE does not have
164 * anything to do with binder check. A binder check is done only when a
165 * suitable pre-shared-key has been selected and only for that selected
166 * pre-shared-key: if the binder check fails, we fail the handshake and we do
167 * not try to find another pre-shared-key for which the binder check would
168 * succeed as recommended by the specification.
169 */
Ronald Cronfbae94a2023-12-05 18:15:14 +0100170#define SSL_TLS1_3_PSK_IDENTITY_DOES_NOT_MATCH 2
171#define SSL_TLS1_3_PSK_IDENTITY_MATCH_BUT_PSK_NOT_USABLE 1
172#define SSL_TLS1_3_PSK_IDENTITY_MATCH 0
Jerry Yu95699e72022-08-21 19:22:23 +0800173
174#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Pengyu Lv29daf4a2023-10-30 17:13:30 +0800175MBEDTLS_CHECK_RETURN_CRITICAL
Pengyu Lvbc4aab72023-12-01 15:37:24 +0800176static int ssl_tls13_key_exchange_is_psk_available(mbedtls_ssl_context *ssl);
Pengyu Lv29daf4a2023-10-30 17:13:30 +0800177MBEDTLS_CHECK_RETURN_CRITICAL
Pengyu Lvbc4aab72023-12-01 15:37:24 +0800178static int ssl_tls13_key_exchange_is_psk_ephemeral_available(mbedtls_ssl_context *ssl);
Jerry Yu95699e72022-08-21 19:22:23 +0800179
180MBEDTLS_CHECK_RETURN_CRITICAL
181static int ssl_tls13_offered_psks_check_identity_match_ticket(
Gilles Peskine449bd832023-01-11 14:50:10 +0100182 mbedtls_ssl_context *ssl,
183 const unsigned char *identity,
184 size_t identity_len,
185 uint32_t obfuscated_ticket_age,
186 mbedtls_ssl_session *session)
Jerry Yu95699e72022-08-21 19:22:23 +0800187{
Jerry Yufd310eb2022-09-06 09:16:35 +0800188 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yu95699e72022-08-21 19:22:23 +0800189 unsigned char *ticket_buffer;
Jerry Yu8d4bbba2022-09-13 14:15:48 +0800190#if defined(MBEDTLS_HAVE_TIME)
Jerry Yucebffc32022-12-15 18:00:05 +0800191 mbedtls_ms_time_t now;
192 mbedtls_ms_time_t server_age;
Jerry Yu713ce1f2023-11-17 15:32:12 +0800193 uint32_t client_age;
Jerry Yucebffc32022-12-15 18:00:05 +0800194 mbedtls_ms_time_t age_diff;
Jerry Yu8d4bbba2022-09-13 14:15:48 +0800195#endif
Jerry Yu95699e72022-08-21 19:22:23 +0800196
197 ((void) obfuscated_ticket_age);
198
Gilles Peskine449bd832023-01-11 14:50:10 +0100199 MBEDTLS_SSL_DEBUG_MSG(2, ("=> check_identity_match_ticket"));
Jerry Yu95699e72022-08-21 19:22:23 +0800200
Jerry Yufd310eb2022-09-06 09:16:35 +0800201 /* Ticket parser is not configured, Skip */
Gilles Peskine449bd832023-01-11 14:50:10 +0100202 if (ssl->conf->f_ticket_parse == NULL || identity_len == 0) {
Ronald Cronfbae94a2023-12-05 18:15:14 +0100203 return SSL_TLS1_3_PSK_IDENTITY_DOES_NOT_MATCH;
Gilles Peskine449bd832023-01-11 14:50:10 +0100204 }
Jerry Yu95699e72022-08-21 19:22:23 +0800205
Jerry Yu4746b102022-09-13 11:11:48 +0800206 /* We create a copy of the encrypted ticket since the ticket parsing
207 * function is allowed to use its input buffer as an output buffer
208 * (in-place decryption). We do, however, need the original buffer for
209 * computing the PSK binder value.
Jerry Yu95699e72022-08-21 19:22:23 +0800210 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100211 ticket_buffer = mbedtls_calloc(1, identity_len);
212 if (ticket_buffer == NULL) {
213 MBEDTLS_SSL_DEBUG_MSG(1, ("buffer too small"));
214 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
Jerry Yu95699e72022-08-21 19:22:23 +0800215 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100216 memcpy(ticket_buffer, identity, identity_len);
Jerry Yu95699e72022-08-21 19:22:23 +0800217
Ronald Cronfbae94a2023-12-05 18:15:14 +0100218 ret = ssl->conf->f_ticket_parse(ssl->conf->p_ticket,
219 session,
220 ticket_buffer, identity_len);
Ronald Cronf602f7b2024-03-05 09:11:55 +0100221 switch (ret) {
222 case 0:
223 ret = SSL_TLS1_3_PSK_IDENTITY_MATCH;
224 break;
225
226 case MBEDTLS_ERR_SSL_SESSION_TICKET_EXPIRED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100227 MBEDTLS_SSL_DEBUG_MSG(3, ("ticket is expired"));
Ronald Cronfbae94a2023-12-05 18:15:14 +0100228 ret = SSL_TLS1_3_PSK_IDENTITY_MATCH_BUT_PSK_NOT_USABLE;
Ronald Cronf602f7b2024-03-05 09:11:55 +0100229 break;
230
231 case MBEDTLS_ERR_SSL_INVALID_MAC:
232 MBEDTLS_SSL_DEBUG_MSG(3, ("ticket is not authentic"));
Ronald Cronfbae94a2023-12-05 18:15:14 +0100233 ret = SSL_TLS1_3_PSK_IDENTITY_DOES_NOT_MATCH;
Ronald Cronf602f7b2024-03-05 09:11:55 +0100234 break;
235
236 default:
237 MBEDTLS_SSL_DEBUG_RET(1, "ticket_parse", ret);
238 ret = SSL_TLS1_3_PSK_IDENTITY_DOES_NOT_MATCH;
Jerry Yu95699e72022-08-21 19:22:23 +0800239 }
240
241 /* We delete the temporary buffer */
Gilles Peskine449bd832023-01-11 14:50:10 +0100242 mbedtls_free(ticket_buffer);
Jerry Yu95699e72022-08-21 19:22:23 +0800243
Ronald Cronfbae94a2023-12-05 18:15:14 +0100244 if (ret != SSL_TLS1_3_PSK_IDENTITY_MATCH) {
245 goto exit;
Jerry Yuce3b95e2023-10-31 16:02:04 +0800246 }
Jerry Yuce3b95e2023-10-31 16:02:04 +0800247
Ronald Cron38117652024-03-05 09:05:46 +0100248 /*
249 * The identity matches that of a ticket. Now check that it has suitable
250 * attributes and bet it will not be the case.
251 */
Ronald Cronfbae94a2023-12-05 18:15:14 +0100252 ret = SSL_TLS1_3_PSK_IDENTITY_MATCH_BUT_PSK_NOT_USABLE;
253
254 if (session->tls_version != MBEDTLS_SSL_VERSION_TLS1_3) {
255 MBEDTLS_SSL_DEBUG_MSG(3, ("Ticket TLS version is not 1.3."));
Jerry Yu8d4bbba2022-09-13 14:15:48 +0800256 goto exit;
257 }
Jerry Yu95699e72022-08-21 19:22:23 +0800258
Gilles Peskine449bd832023-01-11 14:50:10 +0100259#if defined(MBEDTLS_HAVE_TIME)
Jerry Yucebffc32022-12-15 18:00:05 +0800260 now = mbedtls_ms_time();
Gilles Peskine449bd832023-01-11 14:50:10 +0100261
Jerry Yu25ba4d42023-11-10 14:12:20 +0800262 if (now < session->ticket_creation_time) {
Gilles Peskine449bd832023-01-11 14:50:10 +0100263 MBEDTLS_SSL_DEBUG_MSG(
Jerry Yu713ce1f2023-11-17 15:32:12 +0800264 3, ("Invalid ticket creation time ( now = %" MBEDTLS_PRINTF_MS_TIME
265 ", creation_time = %" MBEDTLS_PRINTF_MS_TIME " )",
Jerry Yu25ba4d42023-11-10 14:12:20 +0800266 now, session->ticket_creation_time));
Gilles Peskine449bd832023-01-11 14:50:10 +0100267 goto exit;
268 }
269
Jerry Yu25ba4d42023-11-10 14:12:20 +0800270 server_age = now - session->ticket_creation_time;
Jerry Yu95699e72022-08-21 19:22:23 +0800271
Jerry Yu8d4bbba2022-09-13 14:15:48 +0800272 /* RFC 8446 section 4.6.1
273 *
274 * Servers MUST NOT use any value greater than 604800 seconds (7 days).
275 *
276 * RFC 8446 section 4.2.11.1
277 *
278 * Clients MUST NOT attempt to use tickets which have ages greater than
279 * the "ticket_lifetime" value which was provided with the ticket.
280 *
Jerry Yu8d4bbba2022-09-13 14:15:48 +0800281 */
Jerry Yu8e0174a2023-11-10 13:58:16 +0800282 if (server_age > MBEDTLS_SSL_TLS1_3_MAX_ALLOWED_TICKET_LIFETIME * 1000) {
Jerry Yu8d4bbba2022-09-13 14:15:48 +0800283 MBEDTLS_SSL_DEBUG_MSG(
Jerry Yud84c14f2023-11-16 13:33:57 +0800284 3, ("Ticket age exceeds limitation ticket_age = %" MBEDTLS_PRINTF_MS_TIME,
Jerry Yucebffc32022-12-15 18:00:05 +0800285 server_age));
Jerry Yu8d4bbba2022-09-13 14:15:48 +0800286 goto exit;
287 }
Jerry Yu95699e72022-08-21 19:22:23 +0800288
Jerry Yu8d4bbba2022-09-13 14:15:48 +0800289 /* RFC 8446 section 4.2.10
290 *
291 * For PSKs provisioned via NewSessionTicket, a server MUST validate that
292 * the ticket age for the selected PSK identity (computed by subtracting
293 * ticket_age_add from PskIdentity.obfuscated_ticket_age modulo 2^32) is
294 * within a small tolerance of the time since the ticket was issued.
Jerry Yuf7dad3c2022-09-14 22:31:39 +0800295 *
Jerry Yu31b601a2023-11-10 11:27:21 +0800296 * NOTE: The typical accuracy of an RTC crystal is ±100 to ±20 parts per
297 * million (360 to 72 milliseconds per hour). Default tolerance
Jerry Yu9cb953a2023-11-15 09:54:11 +0800298 * window is 6s, thus in the worst case clients and servers must
Jerry Yu31b601a2023-11-10 11:27:21 +0800299 * sync up their system time every 6000/360/2~=8 hours.
Jerry Yu8d4bbba2022-09-13 14:15:48 +0800300 */
Jerry Yucebffc32022-12-15 18:00:05 +0800301 client_age = obfuscated_ticket_age - session->ticket_age_add;
Jerry Yu60e99722023-11-20 09:55:24 +0800302 age_diff = server_age - (mbedtls_ms_time_t) client_age;
Jerry Yu28e7c552023-11-10 12:05:56 +0800303 if (age_diff < -MBEDTLS_SSL_TLS1_3_TICKET_AGE_TOLERANCE ||
Jerry Yucebffc32022-12-15 18:00:05 +0800304 age_diff > MBEDTLS_SSL_TLS1_3_TICKET_AGE_TOLERANCE) {
Jerry Yu8d4bbba2022-09-13 14:15:48 +0800305 MBEDTLS_SSL_DEBUG_MSG(
Jerry Yuf16efbc2023-10-30 11:06:24 +0800306 3, ("Ticket age outside tolerance window ( diff = %"
307 MBEDTLS_PRINTF_MS_TIME ")",
Jerry Yucebffc32022-12-15 18:00:05 +0800308 age_diff));
Jerry Yu8d4bbba2022-09-13 14:15:48 +0800309 goto exit;
310 }
Jerry Yu95699e72022-08-21 19:22:23 +0800311#endif /* MBEDTLS_HAVE_TIME */
Jerry Yu8d4bbba2022-09-13 14:15:48 +0800312
Ronald Cron38117652024-03-05 09:05:46 +0100313 /*
314 * All good, we have found a suitable ticket.
315 */
Ronald Cronfbae94a2023-12-05 18:15:14 +0100316 ret = SSL_TLS1_3_PSK_IDENTITY_MATCH;
317
Jerry Yu8d4bbba2022-09-13 14:15:48 +0800318exit:
Ronald Cronfbae94a2023-12-05 18:15:14 +0100319 if (ret != SSL_TLS1_3_PSK_IDENTITY_MATCH) {
Gilles Peskine449bd832023-01-11 14:50:10 +0100320 mbedtls_ssl_session_free(session);
321 }
Jerry Yu95699e72022-08-21 19:22:23 +0800322
Gilles Peskine449bd832023-01-11 14:50:10 +0100323 MBEDTLS_SSL_DEBUG_MSG(2, ("<= check_identity_match_ticket"));
324 return ret;
Jerry Yu95699e72022-08-21 19:22:23 +0800325}
326#endif /* MBEDTLS_SSL_SESSION_TICKETS */
327
Jerry Yu6e74a7e2022-07-20 20:49:32 +0800328MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yu1c105562022-07-10 06:32:38 +0000329static int ssl_tls13_offered_psks_check_identity_match(
Gilles Peskine449bd832023-01-11 14:50:10 +0100330 mbedtls_ssl_context *ssl,
331 const unsigned char *identity,
332 size_t identity_len,
333 uint32_t obfuscated_ticket_age,
334 int *psk_type,
335 mbedtls_ssl_session *session)
Jerry Yu1c105562022-07-10 06:32:38 +0000336{
Ronald Cron606671e2023-02-17 11:36:33 +0100337 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
338
Jerry Yu95699e72022-08-21 19:22:23 +0800339 ((void) session);
340 ((void) obfuscated_ticket_age);
Jerry Yu5725f1c2022-08-21 17:27:16 +0800341 *psk_type = MBEDTLS_SSL_TLS1_3_PSK_EXTERNAL;
Jerry Yu95699e72022-08-21 19:22:23 +0800342
Gilles Peskine449bd832023-01-11 14:50:10 +0100343 MBEDTLS_SSL_DEBUG_BUF(4, "identity", identity, identity_len);
Jerry Yu95699e72022-08-21 19:22:23 +0800344
Jerry Yu95699e72022-08-21 19:22:23 +0800345#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Ronald Cron7a30cf52024-02-23 14:38:59 +0100346 ret = ssl_tls13_offered_psks_check_identity_match_ticket(
347 ssl, identity, identity_len, obfuscated_ticket_age, session);
348 if (ret == SSL_TLS1_3_PSK_IDENTITY_MATCH) {
Jerry Yu95699e72022-08-21 19:22:23 +0800349 *psk_type = MBEDTLS_SSL_TLS1_3_PSK_RESUMPTION;
Ronald Cron606671e2023-02-17 11:36:33 +0100350 ret = mbedtls_ssl_set_hs_psk(ssl,
351 session->resumption_key,
352 session->resumption_key_len);
353 if (ret != 0) {
354 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_set_hs_psk", ret);
355 return ret;
356 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100357
358 MBEDTLS_SSL_DEBUG_BUF(4, "Ticket-resumed PSK:",
359 session->resumption_key,
360 session->resumption_key_len);
361 MBEDTLS_SSL_DEBUG_MSG(4, ("ticket: obfuscated_ticket_age: %u",
362 (unsigned) obfuscated_ticket_age));
Ronald Cronfbae94a2023-12-05 18:15:14 +0100363 return SSL_TLS1_3_PSK_IDENTITY_MATCH;
Ronald Cron7a30cf52024-02-23 14:38:59 +0100364 } else if (ret == SSL_TLS1_3_PSK_IDENTITY_MATCH_BUT_PSK_NOT_USABLE) {
365 return SSL_TLS1_3_PSK_IDENTITY_MATCH_BUT_PSK_NOT_USABLE;
Jerry Yu95699e72022-08-21 19:22:23 +0800366 }
367#endif /* MBEDTLS_SSL_SESSION_TICKETS */
368
Jerry Yu1c105562022-07-10 06:32:38 +0000369 /* Check identity with external configured function */
Gilles Peskine449bd832023-01-11 14:50:10 +0100370 if (ssl->conf->f_psk != NULL) {
371 if (ssl->conf->f_psk(
372 ssl->conf->p_psk, ssl, identity, identity_len) == 0) {
Ronald Cronfbae94a2023-12-05 18:15:14 +0100373 return SSL_TLS1_3_PSK_IDENTITY_MATCH;
Jerry Yu1c105562022-07-10 06:32:38 +0000374 }
Ronald Cronfbae94a2023-12-05 18:15:14 +0100375 return SSL_TLS1_3_PSK_IDENTITY_DOES_NOT_MATCH;
Jerry Yu1c105562022-07-10 06:32:38 +0000376 }
377
Gilles Peskine449bd832023-01-11 14:50:10 +0100378 MBEDTLS_SSL_DEBUG_BUF(5, "identity", identity, identity_len);
Jerry Yu1c105562022-07-10 06:32:38 +0000379 /* Check identity with pre-configured psk */
Gilles Peskine449bd832023-01-11 14:50:10 +0100380 if (ssl->conf->psk_identity != NULL &&
Jerry Yu2f0abc92022-07-22 19:34:48 +0800381 identity_len == ssl->conf->psk_identity_len &&
Gilles Peskine449bd832023-01-11 14:50:10 +0100382 mbedtls_ct_memcmp(ssl->conf->psk_identity,
383 identity, identity_len) == 0) {
Ronald Cron606671e2023-02-17 11:36:33 +0100384 ret = mbedtls_ssl_set_hs_psk(ssl, ssl->conf->psk, ssl->conf->psk_len);
385 if (ret != 0) {
386 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_set_hs_psk", ret);
387 return ret;
388 }
Ronald Cronfbae94a2023-12-05 18:15:14 +0100389 return SSL_TLS1_3_PSK_IDENTITY_MATCH;
Jerry Yu1c105562022-07-10 06:32:38 +0000390 }
391
Ronald Cronfbae94a2023-12-05 18:15:14 +0100392 return SSL_TLS1_3_PSK_IDENTITY_DOES_NOT_MATCH;
Jerry Yu1c105562022-07-10 06:32:38 +0000393}
394
Ronald Cron38117652024-03-05 09:05:46 +0100395/*
396 * Non-error return values of ssl_tls13_offered_psks_check_binder_match().
397 * They are positive to not collide with error codes that are negative. Zero
398 * (SSL_TLS1_3_BINDER_MATCH) in case of success as it may be propagated up
399 * by the callers of this function as a generic success condition.
400 */
Ronald Cron6e311272023-12-05 17:57:01 +0100401#define SSL_TLS1_3_BINDER_DOES_NOT_MATCH 1
402#define SSL_TLS1_3_BINDER_MATCH 0
Jerry Yu6e74a7e2022-07-20 20:49:32 +0800403MBEDTLS_CHECK_RETURN_CRITICAL
Xiaokang Qian9f1747b2023-03-30 02:50:04 +0000404static int ssl_tls13_offered_psks_check_binder_match(
405 mbedtls_ssl_context *ssl,
406 const unsigned char *binder, size_t binder_len,
407 int psk_type, psa_algorithm_t psk_hash_alg)
Jerry Yu1c105562022-07-10 06:32:38 +0000408{
409 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yu96a2e362022-07-21 15:11:34 +0800410
Jerry Yudaf375a2022-07-20 21:31:43 +0800411 unsigned char transcript[PSA_HASH_MAX_SIZE];
Jerry Yu1c105562022-07-10 06:32:38 +0000412 size_t transcript_len;
Jerry Yu2f0abc92022-07-22 19:34:48 +0800413 unsigned char *psk;
Jerry Yu96a2e362022-07-21 15:11:34 +0800414 size_t psk_len;
Jerry Yudaf375a2022-07-20 21:31:43 +0800415 unsigned char server_computed_binder[PSA_HASH_MAX_SIZE];
Jerry Yu1c105562022-07-10 06:32:38 +0000416
Jerry Yu1c105562022-07-10 06:32:38 +0000417 /* Get current state of handshake transcript. */
Jerry Yu29d9faa2022-08-23 17:52:45 +0800418 ret = mbedtls_ssl_get_handshake_transcript(
Manuel Pégourié-Gonnard2d6d9932023-03-28 11:38:08 +0200419 ssl, mbedtls_md_type_from_psa_alg(psk_hash_alg),
Gilles Peskine449bd832023-01-11 14:50:10 +0100420 transcript, sizeof(transcript), &transcript_len);
421 if (ret != 0) {
422 return ret;
423 }
Jerry Yu1c105562022-07-10 06:32:38 +0000424
Gilles Peskine449bd832023-01-11 14:50:10 +0100425 ret = mbedtls_ssl_tls13_export_handshake_psk(ssl, &psk, &psk_len);
426 if (ret != 0) {
427 return ret;
428 }
Jerry Yu96a2e362022-07-21 15:11:34 +0800429
Gilles Peskine449bd832023-01-11 14:50:10 +0100430 ret = mbedtls_ssl_tls13_create_psk_binder(ssl, psk_hash_alg,
431 psk, psk_len, psk_type,
432 transcript,
433 server_computed_binder);
Jerry Yu96a2e362022-07-21 15:11:34 +0800434#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +0100435 mbedtls_free((void *) psk);
Jerry Yu96a2e362022-07-21 15:11:34 +0800436#endif
Gilles Peskine449bd832023-01-11 14:50:10 +0100437 if (ret != 0) {
438 MBEDTLS_SSL_DEBUG_MSG(1, ("PSK binder calculation failed."));
439 return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
Jerry Yu1c105562022-07-10 06:32:38 +0000440 }
441
Gilles Peskine449bd832023-01-11 14:50:10 +0100442 MBEDTLS_SSL_DEBUG_BUF(3, "psk binder ( computed ): ",
443 server_computed_binder, transcript_len);
444 MBEDTLS_SSL_DEBUG_BUF(3, "psk binder ( received ): ", binder, binder_len);
Jerry Yu1c105562022-07-10 06:32:38 +0000445
Gilles Peskine449bd832023-01-11 14:50:10 +0100446 if (mbedtls_ct_memcmp(server_computed_binder, binder, binder_len) == 0) {
Ronald Cron6e311272023-12-05 17:57:01 +0100447 return SSL_TLS1_3_BINDER_MATCH;
Jerry Yu1c105562022-07-10 06:32:38 +0000448 }
449
Gilles Peskine449bd832023-01-11 14:50:10 +0100450 mbedtls_platform_zeroize(server_computed_binder,
451 sizeof(server_computed_binder));
Ronald Cron6e311272023-12-05 17:57:01 +0100452 return SSL_TLS1_3_BINDER_DOES_NOT_MATCH;
Jerry Yu1c105562022-07-10 06:32:38 +0000453}
Jerry Yu96a2e362022-07-21 15:11:34 +0800454
Jerry Yu82534862022-08-30 10:42:33 +0800455#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Jerry Yuf35ba382022-08-23 17:58:26 +0800456MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100457static int ssl_tls13_session_copy_ticket(mbedtls_ssl_session *dst,
458 const mbedtls_ssl_session *src)
Jerry Yu82534862022-08-30 10:42:33 +0800459{
Jerry Yu82534862022-08-30 10:42:33 +0800460 dst->ticket_age_add = src->ticket_age_add;
461 dst->ticket_flags = src->ticket_flags;
462 dst->resumption_key_len = src->resumption_key_len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100463 if (src->resumption_key_len == 0) {
464 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
465 }
466 memcpy(dst->resumption_key, src->resumption_key, src->resumption_key_len);
Jerry Yu4746b102022-09-13 11:11:48 +0800467
Jerry Yu33bf2402022-12-12 16:01:43 +0800468#if defined(MBEDTLS_SSL_EARLY_DATA)
469 dst->max_early_data_size = src->max_early_data_size;
470#endif
471
Gilles Peskine449bd832023-01-11 14:50:10 +0100472 return 0;
Jerry Yu82534862022-08-30 10:42:33 +0800473}
474#endif /* MBEDTLS_SSL_SESSION_TICKETS */
475
Ronald Cron79cdd412024-02-20 17:19:28 +0100476struct psk_attributes {
477 int type;
478 int key_exchange_mode;
Ronald Cron74a16292024-02-23 17:07:41 +0100479 const mbedtls_ssl_ciphersuite_t *ciphersuite_info;
Ronald Cron79cdd412024-02-20 17:19:28 +0100480};
Ronald Cron16cc3702024-03-07 15:04:56 +0100481#define PSK_ATTRIBUTES_INIT { 0, 0, NULL }
Ronald Cron79cdd412024-02-20 17:19:28 +0100482
Jerry Yu1c105562022-07-10 06:32:38 +0000483/* Parser for pre_shared_key extension in client hello
484 * struct {
485 * opaque identity<1..2^16-1>;
486 * uint32 obfuscated_ticket_age;
487 * } PskIdentity;
488 *
489 * opaque PskBinderEntry<32..255>;
490 *
491 * struct {
492 * PskIdentity identities<7..2^16-1>;
493 * PskBinderEntry binders<33..2^16-1>;
494 * } OfferedPsks;
495 *
496 * struct {
497 * select (Handshake.msg_type) {
498 * case client_hello: OfferedPsks;
499 * ....
500 * };
501 * } PreSharedKeyExtension;
502 */
Jerry Yu6e74a7e2022-07-20 20:49:32 +0800503MBEDTLS_CHECK_RETURN_CRITICAL
Xiaokang Qian9f1747b2023-03-30 02:50:04 +0000504static int ssl_tls13_parse_pre_shared_key_ext(
505 mbedtls_ssl_context *ssl,
506 const unsigned char *pre_shared_key_ext,
507 const unsigned char *pre_shared_key_ext_end,
508 const unsigned char *ciphersuites,
Ronald Cron79cdd412024-02-20 17:19:28 +0100509 const unsigned char *ciphersuites_end,
510 struct psk_attributes *psk)
Jerry Yu1c105562022-07-10 06:32:38 +0000511{
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +0100512 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yu29d9faa2022-08-23 17:52:45 +0800513 const unsigned char *identities = pre_shared_key_ext;
Jerry Yu568ec252022-07-22 21:27:34 +0800514 const unsigned char *p_identity_len;
515 size_t identities_len;
Jerry Yu1c105562022-07-10 06:32:38 +0000516 const unsigned char *identities_end;
Jerry Yu568ec252022-07-22 21:27:34 +0800517 const unsigned char *binders;
518 const unsigned char *p_binder_len;
519 size_t binders_len;
Jerry Yu1c105562022-07-10 06:32:38 +0000520 const unsigned char *binders_end;
Jerry Yu96a2e362022-07-21 15:11:34 +0800521 int matched_identity = -1;
522 int identity_id = -1;
Jerry Yu1c105562022-07-10 06:32:38 +0000523
Gilles Peskine449bd832023-01-11 14:50:10 +0100524 MBEDTLS_SSL_DEBUG_BUF(3, "pre_shared_key extension",
525 pre_shared_key_ext,
526 pre_shared_key_ext_end - pre_shared_key_ext);
Jerry Yu1c105562022-07-10 06:32:38 +0000527
Jerry Yu96a2e362022-07-21 15:11:34 +0800528 /* identities_len 2 bytes
529 * identities_data >= 7 bytes
530 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100531 MBEDTLS_SSL_CHK_BUF_READ_PTR(identities, pre_shared_key_ext_end, 7 + 2);
532 identities_len = MBEDTLS_GET_UINT16_BE(identities, 0);
Jerry Yu568ec252022-07-22 21:27:34 +0800533 p_identity_len = identities + 2;
Gilles Peskine449bd832023-01-11 14:50:10 +0100534 MBEDTLS_SSL_CHK_BUF_READ_PTR(p_identity_len, pre_shared_key_ext_end,
535 identities_len);
Jerry Yu568ec252022-07-22 21:27:34 +0800536 identities_end = p_identity_len + identities_len;
Jerry Yu1c105562022-07-10 06:32:38 +0000537
Jerry Yu96a2e362022-07-21 15:11:34 +0800538 /* binders_len 2 bytes
539 * binders >= 33 bytes
540 */
Jerry Yu568ec252022-07-22 21:27:34 +0800541 binders = identities_end;
Gilles Peskine449bd832023-01-11 14:50:10 +0100542 MBEDTLS_SSL_CHK_BUF_READ_PTR(binders, pre_shared_key_ext_end, 33 + 2);
543 binders_len = MBEDTLS_GET_UINT16_BE(binders, 0);
Jerry Yu568ec252022-07-22 21:27:34 +0800544 p_binder_len = binders + 2;
Gilles Peskine449bd832023-01-11 14:50:10 +0100545 MBEDTLS_SSL_CHK_BUF_READ_PTR(p_binder_len, pre_shared_key_ext_end, binders_len);
Jerry Yu568ec252022-07-22 21:27:34 +0800546 binders_end = p_binder_len + binders_len;
Jerry Yu1c105562022-07-10 06:32:38 +0000547
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +0100548 ret = ssl->handshake->update_checksum(ssl, pre_shared_key_ext,
549 identities_end - pre_shared_key_ext);
550 if (0 != ret) {
551 MBEDTLS_SSL_DEBUG_RET(1, ("update_checksum"), ret);
552 return ret;
553 }
Jerry Yu96a2e362022-07-21 15:11:34 +0800554
Gilles Peskine449bd832023-01-11 14:50:10 +0100555 while (p_identity_len < identities_end && p_binder_len < binders_end) {
Jerry Yu1c105562022-07-10 06:32:38 +0000556 const unsigned char *identity;
Jerry Yu568ec252022-07-22 21:27:34 +0800557 size_t identity_len;
Jerry Yu82534862022-08-30 10:42:33 +0800558 uint32_t obfuscated_ticket_age;
Jerry Yu96a2e362022-07-21 15:11:34 +0800559 const unsigned char *binder;
Jerry Yu568ec252022-07-22 21:27:34 +0800560 size_t binder_len;
Ronald Cron89089cc2024-02-14 18:18:08 +0100561 int psk_ciphersuite_id;
562 psa_algorithm_t psk_hash_alg;
Ronald Croncf284562024-02-16 18:54:10 +0100563 int allowed_key_exchange_modes;
Ronald Cron74a16292024-02-23 17:07:41 +0100564
Jerry Yu82534862022-08-30 10:42:33 +0800565#if defined(MBEDTLS_SSL_SESSION_TICKETS)
566 mbedtls_ssl_session session;
Gilles Peskine449bd832023-01-11 14:50:10 +0100567 mbedtls_ssl_session_init(&session);
Jerry Yu82534862022-08-30 10:42:33 +0800568#endif
Jerry Yubb852022022-07-20 21:10:44 +0800569
Gilles Peskine449bd832023-01-11 14:50:10 +0100570 MBEDTLS_SSL_CHK_BUF_READ_PTR(p_identity_len, identities_end, 2 + 1 + 4);
571 identity_len = MBEDTLS_GET_UINT16_BE(p_identity_len, 0);
Jerry Yu568ec252022-07-22 21:27:34 +0800572 identity = p_identity_len + 2;
Gilles Peskine449bd832023-01-11 14:50:10 +0100573 MBEDTLS_SSL_CHK_BUF_READ_PTR(identity, identities_end, identity_len + 4);
574 obfuscated_ticket_age = MBEDTLS_GET_UINT32_BE(identity, identity_len);
Jerry Yu568ec252022-07-22 21:27:34 +0800575 p_identity_len += identity_len + 6;
Jerry Yu1c105562022-07-10 06:32:38 +0000576
Gilles Peskine449bd832023-01-11 14:50:10 +0100577 MBEDTLS_SSL_CHK_BUF_READ_PTR(p_binder_len, binders_end, 1 + 32);
Jerry Yu568ec252022-07-22 21:27:34 +0800578 binder_len = *p_binder_len;
579 binder = p_binder_len + 1;
Gilles Peskine449bd832023-01-11 14:50:10 +0100580 MBEDTLS_SSL_CHK_BUF_READ_PTR(binder, binders_end, binder_len);
Jerry Yu568ec252022-07-22 21:27:34 +0800581 p_binder_len += binder_len + 1;
Jerry Yu96a2e362022-07-21 15:11:34 +0800582
Jerry Yu96a2e362022-07-21 15:11:34 +0800583 identity_id++;
Gilles Peskine449bd832023-01-11 14:50:10 +0100584 if (matched_identity != -1) {
Jerry Yu1c105562022-07-10 06:32:38 +0000585 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +0100586 }
Jerry Yu1c105562022-07-10 06:32:38 +0000587
Jerry Yu96a2e362022-07-21 15:11:34 +0800588 ret = ssl_tls13_offered_psks_check_identity_match(
Gilles Peskine449bd832023-01-11 14:50:10 +0100589 ssl, identity, identity_len, obfuscated_ticket_age,
Ronald Cron79cdd412024-02-20 17:19:28 +0100590 &psk->type, &session);
Ronald Cronfbae94a2023-12-05 18:15:14 +0100591 if (ret != SSL_TLS1_3_PSK_IDENTITY_MATCH) {
Jerry Yu96a2e362022-07-21 15:11:34 +0800592 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +0100593 }
Jerry Yu96a2e362022-07-21 15:11:34 +0800594
Gilles Peskine449bd832023-01-11 14:50:10 +0100595 MBEDTLS_SSL_DEBUG_MSG(4, ("found matched identity"));
Ronald Cron89089cc2024-02-14 18:18:08 +0100596
Ronald Cron79cdd412024-02-20 17:19:28 +0100597 switch (psk->type) {
Jerry Yu0baf9072022-08-25 11:21:04 +0800598 case MBEDTLS_SSL_TLS1_3_PSK_EXTERNAL:
Ronald Cron89089cc2024-02-14 18:18:08 +0100599 psk_ciphersuite_id = 0;
600 psk_hash_alg = PSA_ALG_SHA_256;
Ronald Croncf284562024-02-16 18:54:10 +0100601 allowed_key_exchange_modes =
602 MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK_ALL;
Jerry Yu0baf9072022-08-25 11:21:04 +0800603 break;
Jerry Yu82534862022-08-30 10:42:33 +0800604#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Ronald Cronf7e99162024-02-14 16:04:02 +0100605 case MBEDTLS_SSL_TLS1_3_PSK_RESUMPTION:
Ronald Cron89089cc2024-02-14 18:18:08 +0100606 psk_ciphersuite_id = session.ciphersuite;
607 psk_hash_alg = PSA_ALG_NONE;
Ronald Croncf284562024-02-16 18:54:10 +0100608 ssl->session_negotiate->ticket_flags = session.ticket_flags;
609 allowed_key_exchange_modes =
610 session.ticket_flags &
611 MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK_ALL;
Jerry Yu0baf9072022-08-25 11:21:04 +0800612 break;
Ronald Cronf7e99162024-02-14 16:04:02 +0100613#endif
Jerry Yu0baf9072022-08-25 11:21:04 +0800614 default:
Gilles Peskine449bd832023-01-11 14:50:10 +0100615 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Jerry Yu0baf9072022-08-25 11:21:04 +0800616 }
Ronald Cron89089cc2024-02-14 18:18:08 +0100617
Ronald Cron79cdd412024-02-20 17:19:28 +0100618 psk->key_exchange_mode = MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_NONE;
619
Ronald Croncf284562024-02-16 18:54:10 +0100620 if ((allowed_key_exchange_modes &
621 MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK_EPHEMERAL) &&
622 ssl_tls13_key_exchange_is_psk_ephemeral_available(ssl)) {
Ronald Cron79cdd412024-02-20 17:19:28 +0100623 psk->key_exchange_mode = MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK_EPHEMERAL;
Ronald Croncf284562024-02-16 18:54:10 +0100624 } else if ((allowed_key_exchange_modes &
625 MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK) &&
626 ssl_tls13_key_exchange_is_psk_available(ssl)) {
Ronald Cron79cdd412024-02-20 17:19:28 +0100627 psk->key_exchange_mode = MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK;
Ronald Croncf284562024-02-16 18:54:10 +0100628 }
629
Ronald Cron79cdd412024-02-20 17:19:28 +0100630 if (psk->key_exchange_mode == MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_NONE) {
Ronald Croncf284562024-02-16 18:54:10 +0100631 MBEDTLS_SSL_DEBUG_MSG(3, ("No suitable PSK key exchange mode"));
632 continue;
633 }
634
Ronald Cron89089cc2024-02-14 18:18:08 +0100635 ssl_tls13_select_ciphersuite(ssl, ciphersuites, ciphersuites_end,
636 psk_ciphersuite_id, psk_hash_alg,
Ronald Cron74a16292024-02-23 17:07:41 +0100637 &psk->ciphersuite_info);
Ronald Cron89089cc2024-02-14 18:18:08 +0100638
Ronald Cron74a16292024-02-23 17:07:41 +0100639 if (psk->ciphersuite_info == NULL) {
Ronald Cron89089cc2024-02-14 18:18:08 +0100640#if defined(MBEDTLS_SSL_SESSION_TICKETS)
641 mbedtls_ssl_session_free(&session);
642#endif
643 /*
644 * We consider finding a ciphersuite suitable for the PSK as part
645 * of the validation of its binder. Thus if we do not find one, we
646 * abort the handshake with a decrypt_error alert.
647 */
Jerry Yuf35ba382022-08-23 17:58:26 +0800648 MBEDTLS_SSL_PEND_FATAL_ALERT(
649 MBEDTLS_SSL_ALERT_MSG_DECRYPT_ERROR,
Gilles Peskine449bd832023-01-11 14:50:10 +0100650 MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE);
Ronald Cron89089cc2024-02-14 18:18:08 +0100651 return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
Jerry Yu5725f1c2022-08-21 17:27:16 +0800652 }
653
Jerry Yu96a2e362022-07-21 15:11:34 +0800654 ret = ssl_tls13_offered_psks_check_binder_match(
Ronald Cron79cdd412024-02-20 17:19:28 +0100655 ssl, binder, binder_len, psk->type,
Ronald Cron74a16292024-02-23 17:07:41 +0100656 mbedtls_md_psa_alg_from_type((mbedtls_md_type_t) psk->ciphersuite_info->mac));
Ronald Cron6e311272023-12-05 17:57:01 +0100657 if (ret != SSL_TLS1_3_BINDER_MATCH) {
Jerry Yuc5a23a02022-08-25 10:51:44 +0800658 /* For security reasons, the handshake should be aborted when we
659 * fail to validate a binder value. See RFC 8446 section 4.2.11.2
660 * and appendix E.6. */
Jerry Yu82534862022-08-30 10:42:33 +0800661#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Gilles Peskine449bd832023-01-11 14:50:10 +0100662 mbedtls_ssl_session_free(&session);
Jerry Yu82534862022-08-30 10:42:33 +0800663#endif
Gilles Peskine449bd832023-01-11 14:50:10 +0100664 MBEDTLS_SSL_DEBUG_MSG(3, ("Invalid binder."));
Xiaokang Qian9f1747b2023-03-30 02:50:04 +0000665 MBEDTLS_SSL_DEBUG_RET(
666 1, "ssl_tls13_offered_psks_check_binder_match", ret);
Jerry Yu96a2e362022-07-21 15:11:34 +0800667 MBEDTLS_SSL_PEND_FATAL_ALERT(
668 MBEDTLS_SSL_ALERT_MSG_DECRYPT_ERROR,
Gilles Peskine449bd832023-01-11 14:50:10 +0100669 MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE);
670 return ret;
Jerry Yu96a2e362022-07-21 15:11:34 +0800671 }
Jerry Yu96a2e362022-07-21 15:11:34 +0800672
673 matched_identity = identity_id;
Jerry Yu5725f1c2022-08-21 17:27:16 +0800674
Jerry Yu82534862022-08-30 10:42:33 +0800675#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Ronald Cron79cdd412024-02-20 17:19:28 +0100676 if (psk->type == MBEDTLS_SSL_TLS1_3_PSK_RESUMPTION) {
Gilles Peskine449bd832023-01-11 14:50:10 +0100677 ret = ssl_tls13_session_copy_ticket(ssl->session_negotiate,
678 &session);
679 mbedtls_ssl_session_free(&session);
680 if (ret != 0) {
681 return ret;
682 }
Jerry Yu82534862022-08-30 10:42:33 +0800683 }
684#endif /* MBEDTLS_SSL_SESSION_TICKETS */
Jerry Yu1c105562022-07-10 06:32:38 +0000685 }
686
Gilles Peskine449bd832023-01-11 14:50:10 +0100687 if (p_identity_len != identities_end || p_binder_len != binders_end) {
688 MBEDTLS_SSL_DEBUG_MSG(3, ("pre_shared_key extension decode error"));
689 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR,
690 MBEDTLS_ERR_SSL_DECODE_ERROR);
691 return MBEDTLS_ERR_SSL_DECODE_ERROR;
Jerry Yu1c105562022-07-10 06:32:38 +0000692 }
693
Jerry Yu6f1db3f2022-07-22 23:05:59 +0800694 /* Update the handshake transcript with the binder list. */
Xiaokang Qian9f1747b2023-03-30 02:50:04 +0000695 ret = ssl->handshake->update_checksum(
696 ssl, identities_end, (size_t) (binders_end - identities_end));
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +0100697 if (0 != ret) {
698 MBEDTLS_SSL_DEBUG_RET(1, ("update_checksum"), ret);
699 return ret;
700 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100701 if (matched_identity == -1) {
Ronald Croncf284562024-02-16 18:54:10 +0100702 MBEDTLS_SSL_DEBUG_MSG(3, ("No usable PSK or ticket."));
Gilles Peskine449bd832023-01-11 14:50:10 +0100703 return MBEDTLS_ERR_SSL_UNKNOWN_IDENTITY;
Jerry Yu1c105562022-07-10 06:32:38 +0000704 }
705
Gilles Peskine449bd832023-01-11 14:50:10 +0100706 ssl->handshake->selected_identity = (uint16_t) matched_identity;
707 MBEDTLS_SSL_DEBUG_MSG(3, ("Pre shared key found"));
Jerry Yu1c105562022-07-10 06:32:38 +0000708
Gilles Peskine449bd832023-01-11 14:50:10 +0100709 return 0;
Jerry Yu1c105562022-07-10 06:32:38 +0000710}
Jerry Yu032b15ce2022-07-11 06:10:03 +0000711
712/*
713 * struct {
714 * select ( Handshake.msg_type ) {
715 * ....
716 * case server_hello:
717 * uint16 selected_identity;
718 * }
719 * } PreSharedKeyExtension;
720 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100721static int ssl_tls13_write_server_pre_shared_key_ext(mbedtls_ssl_context *ssl,
722 unsigned char *buf,
723 unsigned char *end,
724 size_t *olen)
Jerry Yu032b15ce2022-07-11 06:10:03 +0000725{
Gilles Peskine449bd832023-01-11 14:50:10 +0100726 unsigned char *p = (unsigned char *) buf;
Jerry Yu032b15ce2022-07-11 06:10:03 +0000727
728 *olen = 0;
729
David Horstmann21b89762022-10-06 18:34:28 +0100730 int not_using_psk = 0;
Jerry Yu032b15ce2022-07-11 06:10:03 +0000731#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +0100732 not_using_psk = (mbedtls_svc_key_id_is_null(ssl->handshake->psk_opaque));
Jerry Yu032b15ce2022-07-11 06:10:03 +0000733#else
Gilles Peskine449bd832023-01-11 14:50:10 +0100734 not_using_psk = (ssl->handshake->psk == NULL);
Jerry Yu032b15ce2022-07-11 06:10:03 +0000735#endif
Gilles Peskine449bd832023-01-11 14:50:10 +0100736 if (not_using_psk) {
Jerry Yu032b15ce2022-07-11 06:10:03 +0000737 /* We shouldn't have called this extension writer unless we've
738 * chosen to use a PSK. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100739 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Jerry Yu032b15ce2022-07-11 06:10:03 +0000740 }
741
Gilles Peskine449bd832023-01-11 14:50:10 +0100742 MBEDTLS_SSL_DEBUG_MSG(3, ("server hello, adding pre_shared_key extension"));
743 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 6);
Jerry Yu032b15ce2022-07-11 06:10:03 +0000744
Gilles Peskine449bd832023-01-11 14:50:10 +0100745 MBEDTLS_PUT_UINT16_BE(MBEDTLS_TLS_EXT_PRE_SHARED_KEY, p, 0);
746 MBEDTLS_PUT_UINT16_BE(2, p, 2);
Jerry Yu032b15ce2022-07-11 06:10:03 +0000747
Gilles Peskine449bd832023-01-11 14:50:10 +0100748 MBEDTLS_PUT_UINT16_BE(ssl->handshake->selected_identity, p, 4);
Jerry Yu032b15ce2022-07-11 06:10:03 +0000749
750 *olen = 6;
751
Gilles Peskine449bd832023-01-11 14:50:10 +0100752 MBEDTLS_SSL_DEBUG_MSG(4, ("sent selected_identity: %u",
753 ssl->handshake->selected_identity));
Jerry Yu032b15ce2022-07-11 06:10:03 +0000754
Gilles Peskine449bd832023-01-11 14:50:10 +0100755 mbedtls_ssl_tls13_set_hs_sent_ext_mask(ssl, MBEDTLS_TLS_EXT_PRE_SHARED_KEY);
Jerry Yub95dd362022-11-08 21:19:34 +0800756
Gilles Peskine449bd832023-01-11 14:50:10 +0100757 return 0;
Jerry Yu032b15ce2022-07-11 06:10:03 +0000758}
759
Ronald Cron41a443a2022-10-04 16:38:25 +0200760#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED */
Jerry Yue19e3b92022-07-08 12:04:51 +0000761
XiaokangQian7807f9f2022-02-15 10:04:37 +0000762/* From RFC 8446:
763 * struct {
XiaokangQiancfd925f2022-04-14 07:10:37 +0000764 * ProtocolVersion versions<2..254>;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000765 * } SupportedVersions;
766 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200767MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100768static int ssl_tls13_parse_supported_versions_ext(mbedtls_ssl_context *ssl,
769 const unsigned char *buf,
770 const unsigned char *end)
XiaokangQian7807f9f2022-02-15 10:04:37 +0000771{
XiaokangQian7807f9f2022-02-15 10:04:37 +0000772 const unsigned char *p = buf;
XiaokangQian8f9dfe42022-04-15 02:52:39 +0000773 size_t versions_len;
XiaokangQian4080a7f2022-04-11 09:55:18 +0000774 const unsigned char *versions_end;
XiaokangQian0a1b54e2022-04-21 03:01:38 +0000775 uint16_t tls_version;
Ronald Cron5af4c7f2023-03-07 20:46:59 +0100776 int found_supported_version = 0;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000777
Gilles Peskine449bd832023-01-11 14:50:10 +0100778 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, 1);
XiaokangQian4080a7f2022-04-11 09:55:18 +0000779 versions_len = p[0];
XiaokangQian7807f9f2022-02-15 10:04:37 +0000780 p += 1;
781
Gilles Peskine449bd832023-01-11 14:50:10 +0100782 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, versions_len);
XiaokangQian4080a7f2022-04-11 09:55:18 +0000783 versions_end = p + versions_len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100784 while (p < versions_end) {
785 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, versions_end, 2);
786 tls_version = mbedtls_ssl_read_version(p, ssl->conf->transport);
XiaokangQianb67384d2022-04-19 00:02:38 +0000787 p += 2;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000788
Ronald Cron3bd2b022023-04-03 16:45:39 +0200789 if (MBEDTLS_SSL_VERSION_TLS1_3 == tls_version) {
Ronald Cron5af4c7f2023-03-07 20:46:59 +0100790 found_supported_version = 1;
791 break;
792 }
793
Ronald Cron3bd2b022023-04-03 16:45:39 +0200794 if ((MBEDTLS_SSL_VERSION_TLS1_2 == tls_version) &&
795 mbedtls_ssl_conf_is_tls12_enabled(ssl->conf)) {
Ronald Cron5af4c7f2023-03-07 20:46:59 +0100796 found_supported_version = 1;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000797 break;
798 }
XiaokangQian7807f9f2022-02-15 10:04:37 +0000799 }
800
Ronald Cron5af4c7f2023-03-07 20:46:59 +0100801 if (!found_supported_version) {
802 MBEDTLS_SSL_DEBUG_MSG(1, ("No supported version found."));
XiaokangQian7807f9f2022-02-15 10:04:37 +0000803
Gilles Peskine449bd832023-01-11 14:50:10 +0100804 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_PROTOCOL_VERSION,
805 MBEDTLS_ERR_SSL_BAD_PROTOCOL_VERSION);
806 return MBEDTLS_ERR_SSL_BAD_PROTOCOL_VERSION;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000807 }
808
Ronald Cron5af4c7f2023-03-07 20:46:59 +0100809 MBEDTLS_SSL_DEBUG_MSG(1, ("Negotiated version: [%04x]",
Gilles Peskine449bd832023-01-11 14:50:10 +0100810 (unsigned int) tls_version));
XiaokangQian7807f9f2022-02-15 10:04:37 +0000811
Ronald Cron5af4c7f2023-03-07 20:46:59 +0100812 return (int) tls_version;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000813}
814
Przemek Stekiel8c0a9532023-06-15 16:48:19 +0200815#if defined(PSA_WANT_ALG_ECDH) || defined(PSA_WANT_ALG_FFDH)
XiaokangQiane8ff3502022-04-22 02:34:40 +0000816/*
XiaokangQian7807f9f2022-02-15 10:04:37 +0000817 *
818 * From RFC 8446:
819 * enum {
820 * ... (0xFFFF)
821 * } NamedGroup;
822 * struct {
823 * NamedGroup named_group_list<2..2^16-1>;
824 * } NamedGroupList;
825 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200826MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100827static int ssl_tls13_parse_supported_groups_ext(mbedtls_ssl_context *ssl,
828 const unsigned char *buf,
829 const unsigned char *end)
XiaokangQian7807f9f2022-02-15 10:04:37 +0000830{
XiaokangQian7807f9f2022-02-15 10:04:37 +0000831 const unsigned char *p = buf;
XiaokangQian84823772022-04-19 07:57:30 +0000832 size_t named_group_list_len;
XiaokangQian8f9dfe42022-04-15 02:52:39 +0000833 const unsigned char *named_group_list_end;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000834
Gilles Peskine449bd832023-01-11 14:50:10 +0100835 MBEDTLS_SSL_DEBUG_BUF(3, "supported_groups extension", p, end - buf);
836 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, 2);
837 named_group_list_len = MBEDTLS_GET_UINT16_BE(p, 0);
XiaokangQian7807f9f2022-02-15 10:04:37 +0000838 p += 2;
Gilles Peskine449bd832023-01-11 14:50:10 +0100839 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, named_group_list_len);
XiaokangQian8f9dfe42022-04-15 02:52:39 +0000840 named_group_list_end = p + named_group_list_len;
XiaokangQian4e8cd7b2022-04-21 09:48:09 +0000841 ssl->handshake->hrr_selected_group = 0;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000842
Gilles Peskine449bd832023-01-11 14:50:10 +0100843 while (p < named_group_list_end) {
XiaokangQian08037552022-04-20 07:16:41 +0000844 uint16_t named_group;
Gilles Peskine449bd832023-01-11 14:50:10 +0100845 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, named_group_list_end, 2);
846 named_group = MBEDTLS_GET_UINT16_BE(p, 0);
XiaokangQian08037552022-04-20 07:16:41 +0000847 p += 2;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000848
Gilles Peskine449bd832023-01-11 14:50:10 +0100849 MBEDTLS_SSL_DEBUG_MSG(2,
850 ("got named group: %s(%04x)",
851 mbedtls_ssl_named_group_to_str(named_group),
852 named_group));
XiaokangQian08037552022-04-20 07:16:41 +0000853
Gilles Peskine449bd832023-01-11 14:50:10 +0100854 if (!mbedtls_ssl_named_group_is_offered(ssl, named_group) ||
855 !mbedtls_ssl_named_group_is_supported(named_group) ||
856 ssl->handshake->hrr_selected_group != 0) {
XiaokangQian08037552022-04-20 07:16:41 +0000857 continue;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000858 }
859
Gilles Peskine449bd832023-01-11 14:50:10 +0100860 MBEDTLS_SSL_DEBUG_MSG(2,
861 ("add named group %s(%04x) into received list.",
862 mbedtls_ssl_named_group_to_str(named_group),
863 named_group));
Jerry Yuc1be19f2022-04-23 16:11:39 +0800864
XiaokangQian4e8cd7b2022-04-21 09:48:09 +0000865 ssl->handshake->hrr_selected_group = named_group;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000866 }
867
Gilles Peskine449bd832023-01-11 14:50:10 +0100868 return 0;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000869
870}
Przemek Stekiel8c0a9532023-06-15 16:48:19 +0200871#endif /* PSA_WANT_ALG_ECDH || PSA_WANT_ALG_FFDH */
XiaokangQian7807f9f2022-02-15 10:04:37 +0000872
XiaokangQian08037552022-04-20 07:16:41 +0000873#define SSL_TLS1_3_PARSE_KEY_SHARES_EXT_NO_MATCH 1
874
Valerio Settic9ae8622023-07-25 11:23:50 +0200875#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_EPHEMERAL_ENABLED)
XiaokangQian7807f9f2022-02-15 10:04:37 +0000876/*
877 * ssl_tls13_parse_key_shares_ext() verifies whether the information in the
Xiaokang Qian9f1747b2023-03-30 02:50:04 +0000878 * extension is correct and stores the first acceptable key share and its
879 * associated group.
XiaokangQian7807f9f2022-02-15 10:04:37 +0000880 *
881 * Possible return values are:
882 * - 0: Successful processing of the client provided key share extension.
Xiaokang Qian9f1747b2023-03-30 02:50:04 +0000883 * - SSL_TLS1_3_PARSE_KEY_SHARES_EXT_NO_MATCH: The key shares provided by
884 * the client does not match a group supported by the server. A
885 * HelloRetryRequest will be needed.
XiaokangQiane8ff3502022-04-22 02:34:40 +0000886 * - A negative value for fatal errors.
Jerry Yuc1be19f2022-04-23 16:11:39 +0800887 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200888MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100889static int ssl_tls13_parse_key_shares_ext(mbedtls_ssl_context *ssl,
890 const unsigned char *buf,
891 const unsigned char *end)
XiaokangQian7807f9f2022-02-15 10:04:37 +0000892{
XiaokangQianb67384d2022-04-19 00:02:38 +0000893 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000894 unsigned char const *p = buf;
XiaokangQian8f9dfe42022-04-15 02:52:39 +0000895 unsigned char const *client_shares_end;
Jerry Yuc1be19f2022-04-23 16:11:39 +0800896 size_t client_shares_len;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000897
898 /* From RFC 8446:
899 *
900 * struct {
901 * KeyShareEntry client_shares<0..2^16-1>;
902 * } KeyShareClientHello;
903 *
904 */
905
Gilles Peskine449bd832023-01-11 14:50:10 +0100906 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, 2);
907 client_shares_len = MBEDTLS_GET_UINT16_BE(p, 0);
XiaokangQian7807f9f2022-02-15 10:04:37 +0000908 p += 2;
Gilles Peskine449bd832023-01-11 14:50:10 +0100909 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, client_shares_len);
XiaokangQian7807f9f2022-02-15 10:04:37 +0000910
911 ssl->handshake->offered_group_id = 0;
XiaokangQian8f9dfe42022-04-15 02:52:39 +0000912 client_shares_end = p + client_shares_len;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000913
914 /* We try to find a suitable key share entry and copy it to the
915 * handshake context. Later, we have to find out whether we can do
916 * something with the provided key share or whether we have to
XiaokangQianc5763b52022-04-02 03:34:37 +0000917 * dismiss it and send a HelloRetryRequest message.
918 */
XiaokangQian7807f9f2022-02-15 10:04:37 +0000919
Gilles Peskine449bd832023-01-11 14:50:10 +0100920 while (p < client_shares_end) {
XiaokangQian9b5d04b2022-04-10 10:20:43 +0000921 uint16_t group;
Jerry Yuc1be19f2022-04-23 16:11:39 +0800922 size_t key_exchange_len;
Jerry Yu086edc22022-05-05 10:50:38 +0800923 const unsigned char *key_exchange;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000924
925 /*
926 * struct {
927 * NamedGroup group;
928 * opaque key_exchange<1..2^16-1>;
929 * } KeyShareEntry;
930 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100931 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, client_shares_end, 4);
932 group = MBEDTLS_GET_UINT16_BE(p, 0);
933 key_exchange_len = MBEDTLS_GET_UINT16_BE(p, 2);
Jerry Yuc1be19f2022-04-23 16:11:39 +0800934 p += 4;
Jerry Yu086edc22022-05-05 10:50:38 +0800935 key_exchange = p;
Gilles Peskine449bd832023-01-11 14:50:10 +0100936 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, client_shares_end, key_exchange_len);
Jerry Yu086edc22022-05-05 10:50:38 +0800937 p += key_exchange_len;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000938
939 /* Continue parsing even if we have already found a match,
XiaokangQianc5763b52022-04-02 03:34:37 +0000940 * for input validation purposes.
941 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100942 if (!mbedtls_ssl_named_group_is_offered(ssl, group) ||
943 !mbedtls_ssl_named_group_is_supported(group) ||
944 ssl->handshake->offered_group_id != 0) {
XiaokangQian060d8672022-04-21 09:24:56 +0000945 continue;
946 }
XiaokangQian060d8672022-04-21 09:24:56 +0000947
XiaokangQian7807f9f2022-02-15 10:04:37 +0000948 /*
Przemek Stekielc89f3ea2023-05-18 15:45:53 +0200949 * ECDHE and FFDHE groups are supported
XiaokangQian7807f9f2022-02-15 10:04:37 +0000950 */
Przemek Stekielc89f3ea2023-05-18 15:45:53 +0200951 if (mbedtls_ssl_tls13_named_group_is_ecdhe(group) ||
Przemek Stekield5f79e72023-06-29 09:08:43 +0200952 mbedtls_ssl_tls13_named_group_is_ffdh(group)) {
Przemek Stekielc89f3ea2023-05-18 15:45:53 +0200953 MBEDTLS_SSL_DEBUG_MSG(2, ("ECDH/FFDH group: %s (%04x)",
Gilles Peskine449bd832023-01-11 14:50:10 +0100954 mbedtls_ssl_named_group_to_str(group),
955 group));
Przemek Stekiel7ac93be2023-07-04 10:02:38 +0200956 ret = mbedtls_ssl_tls13_read_public_xxdhe_share(
Gilles Peskine449bd832023-01-11 14:50:10 +0100957 ssl, key_exchange - 2, key_exchange_len + 2);
958 if (ret != 0) {
959 return ret;
960 }
XiaokangQian4e8cd7b2022-04-21 09:48:09 +0000961
Gilles Peskine449bd832023-01-11 14:50:10 +0100962 } else {
963 MBEDTLS_SSL_DEBUG_MSG(4, ("Unrecognized NamedGroup %u",
964 (unsigned) group));
XiaokangQian7807f9f2022-02-15 10:04:37 +0000965 continue;
966 }
967
XiaokangQian9b5d04b2022-04-10 10:20:43 +0000968 ssl->handshake->offered_group_id = group;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000969 }
970
Jerry Yuc1be19f2022-04-23 16:11:39 +0800971
Gilles Peskine449bd832023-01-11 14:50:10 +0100972 if (ssl->handshake->offered_group_id == 0) {
973 MBEDTLS_SSL_DEBUG_MSG(1, ("no matching key share"));
974 return SSL_TLS1_3_PARSE_KEY_SHARES_EXT_NO_MATCH;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000975 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100976 return 0;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000977}
Valerio Settic9ae8622023-07-25 11:23:50 +0200978#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_EPHEMERAL_ENABLED */
XiaokangQian7807f9f2022-02-15 10:04:37 +0000979
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200980MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100981static int ssl_tls13_client_hello_has_exts(mbedtls_ssl_context *ssl,
982 int exts_mask)
XiaokangQian7807f9f2022-02-15 10:04:37 +0000983{
Jerry Yu0c354a22022-08-29 15:25:36 +0800984 int masked = ssl->handshake->received_extensions & exts_mask;
Gilles Peskine449bd832023-01-11 14:50:10 +0100985 return masked == exts_mask;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000986}
987
Jerry Yue5991322022-11-07 14:03:44 +0800988#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200989MBEDTLS_CHECK_RETURN_CRITICAL
XiaokangQianb67384d2022-04-19 00:02:38 +0000990static int ssl_tls13_client_hello_has_exts_for_ephemeral_key_exchange(
Gilles Peskine449bd832023-01-11 14:50:10 +0100991 mbedtls_ssl_context *ssl)
XiaokangQian7807f9f2022-02-15 10:04:37 +0000992{
Gilles Peskine449bd832023-01-11 14:50:10 +0100993 return ssl_tls13_client_hello_has_exts(
994 ssl,
995 MBEDTLS_SSL_EXT_MASK(SUPPORTED_GROUPS) |
996 MBEDTLS_SSL_EXT_MASK(KEY_SHARE) |
997 MBEDTLS_SSL_EXT_MASK(SIG_ALG));
XiaokangQian7807f9f2022-02-15 10:04:37 +0000998}
Jerry Yue5991322022-11-07 14:03:44 +0800999#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED */
XiaokangQian7807f9f2022-02-15 10:04:37 +00001000
Jerry Yue5991322022-11-07 14:03:44 +08001001#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK_ENABLED)
Jerry Yu77f01482022-07-11 07:03:24 +00001002MBEDTLS_CHECK_RETURN_CRITICAL
1003static int ssl_tls13_client_hello_has_exts_for_psk_key_exchange(
Gilles Peskine449bd832023-01-11 14:50:10 +01001004 mbedtls_ssl_context *ssl)
Jerry Yu77f01482022-07-11 07:03:24 +00001005{
Gilles Peskine449bd832023-01-11 14:50:10 +01001006 return ssl_tls13_client_hello_has_exts(
1007 ssl,
1008 MBEDTLS_SSL_EXT_MASK(PRE_SHARED_KEY) |
1009 MBEDTLS_SSL_EXT_MASK(PSK_KEY_EXCHANGE_MODES));
Jerry Yu77f01482022-07-11 07:03:24 +00001010}
Jerry Yue5991322022-11-07 14:03:44 +08001011#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK_ENABLED */
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)
Jerry Yu77f01482022-07-11 07:03:24 +00001014MBEDTLS_CHECK_RETURN_CRITICAL
1015static int ssl_tls13_client_hello_has_exts_for_psk_ephemeral_key_exchange(
Gilles Peskine449bd832023-01-11 14:50:10 +01001016 mbedtls_ssl_context *ssl)
Jerry Yu77f01482022-07-11 07:03:24 +00001017{
Gilles Peskine449bd832023-01-11 14:50:10 +01001018 return ssl_tls13_client_hello_has_exts(
1019 ssl,
1020 MBEDTLS_SSL_EXT_MASK(SUPPORTED_GROUPS) |
1021 MBEDTLS_SSL_EXT_MASK(KEY_SHARE) |
1022 MBEDTLS_SSL_EXT_MASK(PRE_SHARED_KEY) |
1023 MBEDTLS_SSL_EXT_MASK(PSK_KEY_EXCHANGE_MODES));
Jerry Yu77f01482022-07-11 07:03:24 +00001024}
Jerry Yue5991322022-11-07 14:03:44 +08001025#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK_EPHEMERAL_ENABLED */
Jerry Yu77f01482022-07-11 07:03:24 +00001026
Pengyu Lv306a01d2023-02-02 16:35:47 +08001027#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED)
1028MBEDTLS_CHECK_RETURN_CRITICAL
Pengyu Lvbc4aab72023-12-01 15:37:24 +08001029static int ssl_tls13_key_exchange_is_psk_available(mbedtls_ssl_context *ssl)
Jerry Yu77f01482022-07-11 07:03:24 +00001030{
Jerry Yue5991322022-11-07 14:03:44 +08001031#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK_ENABLED)
Ronald Crone8c162d2024-02-21 10:15:44 +01001032 return mbedtls_ssl_conf_tls13_is_psk_enabled(ssl) &&
Pengyu Lvb2cfafb2023-11-14 13:56:13 +08001033 mbedtls_ssl_tls13_is_psk_supported(ssl) &&
Gilles Peskine449bd832023-01-11 14:50:10 +01001034 ssl_tls13_client_hello_has_exts_for_psk_key_exchange(ssl);
Jerry Yu77f01482022-07-11 07:03:24 +00001035#else
1036 ((void) ssl);
Gilles Peskine449bd832023-01-11 14:50:10 +01001037 return 0;
Jerry Yu77f01482022-07-11 07:03:24 +00001038#endif
1039}
XiaokangQian7807f9f2022-02-15 10:04:37 +00001040
Jerry Yu77f01482022-07-11 07:03:24 +00001041MBEDTLS_CHECK_RETURN_CRITICAL
Pengyu Lvbc4aab72023-12-01 15:37:24 +08001042static int ssl_tls13_key_exchange_is_psk_ephemeral_available(mbedtls_ssl_context *ssl)
Jerry Yu77f01482022-07-11 07:03:24 +00001043{
Jerry Yue5991322022-11-07 14:03:44 +08001044#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK_EPHEMERAL_ENABLED)
Ronald Crone8c162d2024-02-21 10:15:44 +01001045 return mbedtls_ssl_conf_tls13_is_psk_ephemeral_enabled(ssl) &&
Pengyu Lvb2cfafb2023-11-14 13:56:13 +08001046 mbedtls_ssl_tls13_is_psk_ephemeral_supported(ssl) &&
Gilles Peskine449bd832023-01-11 14:50:10 +01001047 ssl_tls13_client_hello_has_exts_for_psk_ephemeral_key_exchange(ssl);
Jerry Yu77f01482022-07-11 07:03:24 +00001048#else
1049 ((void) ssl);
Gilles Peskine449bd832023-01-11 14:50:10 +01001050 return 0;
Jerry Yu77f01482022-07-11 07:03:24 +00001051#endif
1052}
Ronald Cron79cdd412024-02-20 17:19:28 +01001053#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED */
Jerry Yu77f01482022-07-11 07:03:24 +00001054
Ronald Cron79cdd412024-02-20 17:19:28 +01001055MBEDTLS_CHECK_RETURN_CRITICAL
1056static int ssl_tls13_key_exchange_is_ephemeral_available(mbedtls_ssl_context *ssl)
Jerry Yu77f01482022-07-11 07:03:24 +00001057{
Ronald Cron79cdd412024-02-20 17:19:28 +01001058#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED)
1059 return mbedtls_ssl_conf_tls13_is_ephemeral_enabled(ssl) &&
1060 ssl_tls13_client_hello_has_exts_for_ephemeral_key_exchange(ssl);
1061#else
1062 ((void) ssl);
Gilles Peskine449bd832023-01-11 14:50:10 +01001063 return 0;
Ronald Cron79cdd412024-02-20 17:19:28 +01001064#endif
XiaokangQian7807f9f2022-02-15 10:04:37 +00001065}
1066
XiaokangQian81802f42022-06-10 13:25:22 +00001067#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
Ronald Cron928cbd32022-10-04 16:14:26 +02001068 defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED)
Ronald Cron67ea2542022-09-15 17:34:42 +02001069
1070#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01001071static psa_algorithm_t ssl_tls13_iana_sig_alg_to_psa_alg(uint16_t sig_alg)
Ronald Cron67ea2542022-09-15 17:34:42 +02001072{
Gilles Peskine449bd832023-01-11 14:50:10 +01001073 switch (sig_alg) {
Ronald Cron67ea2542022-09-15 17:34:42 +02001074 case MBEDTLS_TLS1_3_SIG_ECDSA_SECP256R1_SHA256:
Gilles Peskine449bd832023-01-11 14:50:10 +01001075 return PSA_ALG_ECDSA(PSA_ALG_SHA_256);
Ronald Cron67ea2542022-09-15 17:34:42 +02001076 case MBEDTLS_TLS1_3_SIG_ECDSA_SECP384R1_SHA384:
Gilles Peskine449bd832023-01-11 14:50:10 +01001077 return PSA_ALG_ECDSA(PSA_ALG_SHA_384);
Ronald Cron67ea2542022-09-15 17:34:42 +02001078 case MBEDTLS_TLS1_3_SIG_ECDSA_SECP521R1_SHA512:
Gilles Peskine449bd832023-01-11 14:50:10 +01001079 return PSA_ALG_ECDSA(PSA_ALG_SHA_512);
Ronald Cron67ea2542022-09-15 17:34:42 +02001080 case MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA256:
Gilles Peskine449bd832023-01-11 14:50:10 +01001081 return PSA_ALG_RSA_PSS(PSA_ALG_SHA_256);
Ronald Cron67ea2542022-09-15 17:34:42 +02001082 case MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA384:
Gilles Peskine449bd832023-01-11 14:50:10 +01001083 return PSA_ALG_RSA_PSS(PSA_ALG_SHA_384);
Ronald Cron67ea2542022-09-15 17:34:42 +02001084 case MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA512:
Gilles Peskine449bd832023-01-11 14:50:10 +01001085 return PSA_ALG_RSA_PSS(PSA_ALG_SHA_512);
Ronald Cron67ea2542022-09-15 17:34:42 +02001086 case MBEDTLS_TLS1_3_SIG_RSA_PKCS1_SHA256:
Gilles Peskine449bd832023-01-11 14:50:10 +01001087 return PSA_ALG_RSA_PKCS1V15_SIGN(PSA_ALG_SHA_256);
Ronald Cron67ea2542022-09-15 17:34:42 +02001088 case MBEDTLS_TLS1_3_SIG_RSA_PKCS1_SHA384:
Gilles Peskine449bd832023-01-11 14:50:10 +01001089 return PSA_ALG_RSA_PKCS1V15_SIGN(PSA_ALG_SHA_384);
Ronald Cron67ea2542022-09-15 17:34:42 +02001090 case MBEDTLS_TLS1_3_SIG_RSA_PKCS1_SHA512:
Gilles Peskine449bd832023-01-11 14:50:10 +01001091 return PSA_ALG_RSA_PKCS1V15_SIGN(PSA_ALG_SHA_512);
Ronald Cron67ea2542022-09-15 17:34:42 +02001092 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01001093 return PSA_ALG_NONE;
Ronald Cron67ea2542022-09-15 17:34:42 +02001094 }
1095}
1096#endif /* MBEDTLS_USE_PSA_CRYPTO */
1097
XiaokangQian23c5be62022-06-07 02:04:34 +00001098/*
XiaokangQianfb665a82022-06-15 03:57:21 +00001099 * Pick best ( private key, certificate chain ) pair based on the signature
1100 * algorithms supported by the client.
XiaokangQian23c5be62022-06-07 02:04:34 +00001101 */
Ronald Cronce7d76e2022-07-08 18:56:49 +02001102MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01001103static int ssl_tls13_pick_key_cert(mbedtls_ssl_context *ssl)
XiaokangQian23c5be62022-06-07 02:04:34 +00001104{
XiaokangQianfb665a82022-06-15 03:57:21 +00001105 mbedtls_ssl_key_cert *key_cert, *key_cert_list;
XiaokangQian81802f42022-06-10 13:25:22 +00001106 const uint16_t *sig_alg = ssl->handshake->received_sig_algs;
XiaokangQian23c5be62022-06-07 02:04:34 +00001107
1108#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Gilles Peskine449bd832023-01-11 14:50:10 +01001109 if (ssl->handshake->sni_key_cert != NULL) {
XiaokangQianfb665a82022-06-15 03:57:21 +00001110 key_cert_list = ssl->handshake->sni_key_cert;
Gilles Peskine449bd832023-01-11 14:50:10 +01001111 } else
XiaokangQian23c5be62022-06-07 02:04:34 +00001112#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
Gilles Peskine449bd832023-01-11 14:50:10 +01001113 key_cert_list = ssl->conf->key_cert;
XiaokangQian23c5be62022-06-07 02:04:34 +00001114
Gilles Peskine449bd832023-01-11 14:50:10 +01001115 if (key_cert_list == NULL) {
1116 MBEDTLS_SSL_DEBUG_MSG(3, ("server has no certificate"));
1117 return -1;
XiaokangQian23c5be62022-06-07 02:04:34 +00001118 }
1119
Gilles Peskine449bd832023-01-11 14:50:10 +01001120 for (; *sig_alg != MBEDTLS_TLS1_3_SIG_NONE; sig_alg++) {
1121 if (!mbedtls_ssl_sig_alg_is_offered(ssl, *sig_alg)) {
Ronald Cron67ea2542022-09-15 17:34:42 +02001122 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +01001123 }
Ronald Cron67ea2542022-09-15 17:34:42 +02001124
Gilles Peskine449bd832023-01-11 14:50:10 +01001125 if (!mbedtls_ssl_tls13_sig_alg_for_cert_verify_is_supported(*sig_alg)) {
Ronald Cron67ea2542022-09-15 17:34:42 +02001126 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +01001127 }
Ronald Cron67ea2542022-09-15 17:34:42 +02001128
Gilles Peskine449bd832023-01-11 14:50:10 +01001129 for (key_cert = key_cert_list; key_cert != NULL;
1130 key_cert = key_cert->next) {
Ronald Cron67ea2542022-09-15 17:34:42 +02001131#if defined(MBEDTLS_USE_PSA_CRYPTO)
1132 psa_algorithm_t psa_alg = PSA_ALG_NONE;
1133#endif /* MBEDTLS_USE_PSA_CRYPTO */
1134
Gilles Peskine449bd832023-01-11 14:50:10 +01001135 MBEDTLS_SSL_DEBUG_CRT(3, "certificate (chain) candidate",
1136 key_cert->cert);
XiaokangQian81802f42022-06-10 13:25:22 +00001137
1138 /*
Gilles Peskine449bd832023-01-11 14:50:10 +01001139 * This avoids sending the client a cert it'll reject based on
1140 * keyUsage or other extensions.
1141 */
1142 if (mbedtls_x509_crt_check_key_usage(
1143 key_cert->cert, MBEDTLS_X509_KU_DIGITAL_SIGNATURE) != 0 ||
XiaokangQian81802f42022-06-10 13:25:22 +00001144 mbedtls_x509_crt_check_extended_key_usage(
XiaokangQianfb665a82022-06-15 03:57:21 +00001145 key_cert->cert, MBEDTLS_OID_SERVER_AUTH,
Gilles Peskine449bd832023-01-11 14:50:10 +01001146 MBEDTLS_OID_SIZE(MBEDTLS_OID_SERVER_AUTH)) != 0) {
1147 MBEDTLS_SSL_DEBUG_MSG(3, ("certificate mismatch: "
1148 "(extended) key usage extension"));
XiaokangQian81802f42022-06-10 13:25:22 +00001149 continue;
1150 }
XiaokangQian23c5be62022-06-07 02:04:34 +00001151
Gilles Peskine449bd832023-01-11 14:50:10 +01001152 MBEDTLS_SSL_DEBUG_MSG(3,
1153 ("ssl_tls13_pick_key_cert:"
1154 "check signature algorithm %s [%04x]",
1155 mbedtls_ssl_sig_alg_to_str(*sig_alg),
1156 *sig_alg));
Ronald Cron67ea2542022-09-15 17:34:42 +02001157#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01001158 psa_alg = ssl_tls13_iana_sig_alg_to_psa_alg(*sig_alg);
Ronald Cron67ea2542022-09-15 17:34:42 +02001159#endif /* MBEDTLS_USE_PSA_CRYPTO */
1160
Gilles Peskine449bd832023-01-11 14:50:10 +01001161 if (mbedtls_ssl_tls13_check_sig_alg_cert_key_match(
1162 *sig_alg, &key_cert->cert->pk)
Ronald Cron67ea2542022-09-15 17:34:42 +02001163#if defined(MBEDTLS_USE_PSA_CRYPTO)
1164 && psa_alg != PSA_ALG_NONE &&
Gilles Peskine449bd832023-01-11 14:50:10 +01001165 mbedtls_pk_can_do_ext(&key_cert->cert->pk, psa_alg,
1166 PSA_KEY_USAGE_SIGN_HASH) == 1
Ronald Cron67ea2542022-09-15 17:34:42 +02001167#endif /* MBEDTLS_USE_PSA_CRYPTO */
Gilles Peskine449bd832023-01-11 14:50:10 +01001168 ) {
XiaokangQianfb665a82022-06-15 03:57:21 +00001169 ssl->handshake->key_cert = key_cert;
Gilles Peskine449bd832023-01-11 14:50:10 +01001170 MBEDTLS_SSL_DEBUG_MSG(3,
1171 ("ssl_tls13_pick_key_cert:"
1172 "selected signature algorithm"
1173 " %s [%04x]",
1174 mbedtls_ssl_sig_alg_to_str(*sig_alg),
1175 *sig_alg));
XiaokangQianfb665a82022-06-15 03:57:21 +00001176 MBEDTLS_SSL_DEBUG_CRT(
Gilles Peskine449bd832023-01-11 14:50:10 +01001177 3, "selected certificate (chain)",
1178 ssl->handshake->key_cert->cert);
1179 return 0;
XiaokangQian81802f42022-06-10 13:25:22 +00001180 }
XiaokangQian81802f42022-06-10 13:25:22 +00001181 }
XiaokangQian23c5be62022-06-07 02:04:34 +00001182 }
1183
Gilles Peskine449bd832023-01-11 14:50:10 +01001184 MBEDTLS_SSL_DEBUG_MSG(2, ("ssl_tls13_pick_key_cert:"
1185 "no suitable certificate found"));
1186 return -1;
XiaokangQian23c5be62022-06-07 02:04:34 +00001187}
XiaokangQian81802f42022-06-10 13:25:22 +00001188#endif /* MBEDTLS_X509_CRT_PARSE_C &&
Ronald Cron928cbd32022-10-04 16:14:26 +02001189 MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED */
XiaokangQian23c5be62022-06-07 02:04:34 +00001190
XiaokangQian4080a7f2022-04-11 09:55:18 +00001191/*
XiaokangQianed582dd2022-04-13 08:21:05 +00001192 *
1193 * STATE HANDLING: ClientHello
1194 *
XiaokangQianb67384d2022-04-19 00:02:38 +00001195 * There are three possible classes of outcomes when parsing the ClientHello:
XiaokangQianed582dd2022-04-13 08:21:05 +00001196 *
XiaokangQianb67384d2022-04-19 00:02:38 +00001197 * 1) The ClientHello was well-formed and matched the server's configuration.
XiaokangQianed582dd2022-04-13 08:21:05 +00001198 *
1199 * In this case, the server progresses to sending its ServerHello.
1200 *
XiaokangQianb67384d2022-04-19 00:02:38 +00001201 * 2) The ClientHello was well-formed but didn't match the server's
1202 * configuration.
XiaokangQianed582dd2022-04-13 08:21:05 +00001203 *
1204 * For example, the client might not have offered a key share which
1205 * the server supports, or the server might require a cookie.
1206 *
1207 * In this case, the server sends a HelloRetryRequest.
1208 *
XiaokangQianb67384d2022-04-19 00:02:38 +00001209 * 3) The ClientHello was ill-formed
XiaokangQianed582dd2022-04-13 08:21:05 +00001210 *
1211 * In this case, we abort the handshake.
1212 *
1213 */
1214
1215/*
XiaokangQian4080a7f2022-04-11 09:55:18 +00001216 * Structure of this message:
1217 *
XiaokangQiane8ff3502022-04-22 02:34:40 +00001218 * uint16 ProtocolVersion;
1219 * opaque Random[32];
1220 * uint8 CipherSuite[2]; // Cryptographic suite selector
XiaokangQian4080a7f2022-04-11 09:55:18 +00001221 *
XiaokangQiane8ff3502022-04-22 02:34:40 +00001222 * struct {
1223 * ProtocolVersion legacy_version = 0x0303; // TLS v1.2
1224 * Random random;
1225 * opaque legacy_session_id<0..32>;
1226 * CipherSuite cipher_suites<2..2^16-2>;
1227 * opaque legacy_compression_methods<1..2^8-1>;
1228 * Extension extensions<8..2^16-1>;
1229 * } ClientHello;
XiaokangQian4080a7f2022-04-11 09:55:18 +00001230 */
XiaokangQianed582dd2022-04-13 08:21:05 +00001231
1232#define SSL_CLIENT_HELLO_OK 0
1233#define SSL_CLIENT_HELLO_HRR_REQUIRED 1
Ronald Cron5af4c7f2023-03-07 20:46:59 +01001234#define SSL_CLIENT_HELLO_TLS1_2 2
XiaokangQianed582dd2022-04-13 08:21:05 +00001235
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001236MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01001237static int ssl_tls13_parse_client_hello(mbedtls_ssl_context *ssl,
1238 const unsigned char *buf,
1239 const unsigned char *end)
XiaokangQian7807f9f2022-02-15 10:04:37 +00001240{
XiaokangQianb67384d2022-04-19 00:02:38 +00001241 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1242 const unsigned char *p = buf;
Ronald Crond540d992023-03-07 09:41:48 +01001243 const unsigned char *random;
XiaokangQian4080a7f2022-04-11 09:55:18 +00001244 size_t legacy_session_id_len;
Ronald Croncada4102023-03-07 09:51:39 +01001245 const unsigned char *legacy_session_id;
XiaokangQian318dc762022-04-20 09:43:51 +00001246 size_t cipher_suites_len;
Ronald Cron2f16b4e2023-03-07 10:07:32 +01001247 const unsigned char *cipher_suites;
XiaokangQian060d8672022-04-21 09:24:56 +00001248 const unsigned char *cipher_suites_end;
XiaokangQianb67384d2022-04-19 00:02:38 +00001249 size_t extensions_len;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001250 const unsigned char *extensions_end;
Ronald Croneff56732023-04-03 17:36:31 +02001251 const unsigned char *supported_versions_data;
1252 const unsigned char *supported_versions_data_end;
Jerry Yuc4bf5d62022-10-29 09:08:47 +08001253 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
Jerry Yu49ca9282022-05-05 11:05:22 +08001254 int hrr_required = 0;
Ronald Cron8a74f072023-06-14 17:59:29 +02001255 int no_usable_share_for_key_agreement = 0;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001256
Ronald Cron41a443a2022-10-04 16:38:25 +02001257#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED)
Ronald Cron79cdd412024-02-20 17:19:28 +01001258 int got_psk = 0;
1259 struct psk_attributes psk = PSK_ATTRIBUTES_INIT;
Jerry Yu29d9faa2022-08-23 17:52:45 +08001260 const unsigned char *pre_shared_key_ext = NULL;
Jerry Yu1c105562022-07-10 06:32:38 +00001261 const unsigned char *pre_shared_key_ext_end = NULL;
Ronald Cron41a443a2022-10-04 16:38:25 +02001262#endif
XiaokangQian7807f9f2022-02-15 10:04:37 +00001263
XiaokangQian7807f9f2022-02-15 10:04:37 +00001264 /*
XiaokangQianb67384d2022-04-19 00:02:38 +00001265 * ClientHello layout:
XiaokangQian7807f9f2022-02-15 10:04:37 +00001266 * 0 . 1 protocol version
XiaokangQiane8ff3502022-04-22 02:34:40 +00001267 * 2 . 33 random bytes
XiaokangQianc5763b52022-04-02 03:34:37 +00001268 * 34 . 34 session id length ( 1 byte )
XiaokangQian7807f9f2022-02-15 10:04:37 +00001269 * 35 . 34+x session id
XiaokangQian7807f9f2022-02-15 10:04:37 +00001270 * .. . .. ciphersuite list length ( 2 bytes )
1271 * .. . .. ciphersuite list
1272 * .. . .. compression alg. list length ( 1 byte )
1273 * .. . .. compression alg. list
1274 * .. . .. extensions length ( 2 bytes, optional )
1275 * .. . .. extensions ( optional )
1276 */
1277
XiaokangQianb67384d2022-04-19 00:02:38 +00001278 /*
bootstrap-prime6dbbf442022-05-17 19:30:44 -04001279 * Minimal length ( with everything empty and extensions omitted ) is
XiaokangQian7807f9f2022-02-15 10:04:37 +00001280 * 2 + 32 + 1 + 2 + 1 = 38 bytes. Check that first, so that we can
1281 * read at least up to session id length without worrying.
1282 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001283 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, 38);
XiaokangQian7807f9f2022-02-15 10:04:37 +00001284
1285 /* ...
1286 * ProtocolVersion legacy_version = 0x0303; // TLS 1.2
1287 * ...
1288 * with ProtocolVersion defined as:
1289 * uint16 ProtocolVersion;
1290 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001291 if (mbedtls_ssl_read_version(p, ssl->conf->transport) !=
1292 MBEDTLS_SSL_VERSION_TLS1_2) {
1293 MBEDTLS_SSL_DEBUG_MSG(1, ("Unsupported version of TLS."));
1294 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_PROTOCOL_VERSION,
1295 MBEDTLS_ERR_SSL_BAD_PROTOCOL_VERSION);
1296 return MBEDTLS_ERR_SSL_BAD_PROTOCOL_VERSION;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001297 }
1298 p += 2;
1299
Jerry Yuc1be19f2022-04-23 16:11:39 +08001300 /* ...
1301 * Random random;
1302 * ...
XiaokangQianb67384d2022-04-19 00:02:38 +00001303 * with Random defined as:
1304 * opaque Random[32];
XiaokangQian7807f9f2022-02-15 10:04:37 +00001305 */
Ronald Crond540d992023-03-07 09:41:48 +01001306 random = p;
XiaokangQian08037552022-04-20 07:16:41 +00001307 p += MBEDTLS_CLIENT_HELLO_RANDOM_LEN;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001308
Jerry Yuc1be19f2022-04-23 16:11:39 +08001309 /* ...
XiaokangQianb67384d2022-04-19 00:02:38 +00001310 * opaque legacy_session_id<0..32>;
Jerry Yuc1be19f2022-04-23 16:11:39 +08001311 * ...
XiaokangQian7807f9f2022-02-15 10:04:37 +00001312 */
Ronald Croncada4102023-03-07 09:51:39 +01001313 legacy_session_id_len = *(p++);
1314 legacy_session_id = p;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001315
XiaokangQianb67384d2022-04-19 00:02:38 +00001316 /*
1317 * Check we have enough data for the legacy session identifier
Jerry Yue95c8af2022-07-26 15:48:20 +08001318 * and the ciphersuite list length.
XiaokangQianb67384d2022-04-19 00:02:38 +00001319 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001320 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, legacy_session_id_len + 2);
XiaokangQian4080a7f2022-04-11 09:55:18 +00001321 p += legacy_session_id_len;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001322
Ronald Cron2f16b4e2023-03-07 10:07:32 +01001323 /* ...
1324 * CipherSuite cipher_suites<2..2^16-2>;
1325 * ...
1326 * with CipherSuite defined as:
1327 * uint8 CipherSuite[2];
1328 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001329 cipher_suites_len = MBEDTLS_GET_UINT16_BE(p, 0);
XiaokangQian7807f9f2022-02-15 10:04:37 +00001330 p += 2;
Ronald Cron2f16b4e2023-03-07 10:07:32 +01001331 cipher_suites = p;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001332
Ronald Cronfc7ae872023-02-16 15:32:19 +01001333 /*
1334 * The length of the ciphersuite list has to be even.
1335 */
1336 if (cipher_suites_len & 1) {
1337 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR,
1338 MBEDTLS_ERR_SSL_DECODE_ERROR);
1339 return MBEDTLS_ERR_SSL_DECODE_ERROR;
1340 }
1341
XiaokangQianb67384d2022-04-19 00:02:38 +00001342 /* Check we have enough data for the ciphersuite list, the legacy
1343 * compression methods and the length of the extensions.
Jerry Yue95c8af2022-07-26 15:48:20 +08001344 *
1345 * cipher_suites cipher_suites_len bytes
1346 * legacy_compression_methods 2 bytes
1347 * extensions_len 2 bytes
XiaokangQianb67384d2022-04-19 00:02:38 +00001348 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001349 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, cipher_suites_len + 2 + 2);
Ronald Cron8c527d02023-03-07 15:47:47 +01001350 p += cipher_suites_len;
1351 cipher_suites_end = p;
Jerry Yu5725f1c2022-08-21 17:27:16 +08001352
1353 /*
Ronald Cron8c527d02023-03-07 15:47:47 +01001354 * Search for the supported versions extension and parse it to determine
1355 * if the client supports TLS 1.3.
1356 */
1357 ret = mbedtls_ssl_tls13_is_supported_versions_ext_present_in_exts(
1358 ssl, p + 2, end,
Ronald Croneff56732023-04-03 17:36:31 +02001359 &supported_versions_data, &supported_versions_data_end);
Ronald Cron8c527d02023-03-07 15:47:47 +01001360 if (ret < 0) {
1361 MBEDTLS_SSL_DEBUG_RET(1,
1362 ("mbedtls_ssl_tls13_is_supported_versions_ext_present_in_exts"), ret);
1363 return ret;
1364 }
1365
1366 if (ret == 0) {
Ronald Cron5af4c7f2023-03-07 20:46:59 +01001367 return SSL_CLIENT_HELLO_TLS1_2;
Ronald Cron8c527d02023-03-07 15:47:47 +01001368 }
1369
Ronald Cron5af4c7f2023-03-07 20:46:59 +01001370 if (ret == 1) {
1371 ret = ssl_tls13_parse_supported_versions_ext(ssl,
Ronald Croneff56732023-04-03 17:36:31 +02001372 supported_versions_data,
1373 supported_versions_data_end);
Ronald Cron5af4c7f2023-03-07 20:46:59 +01001374 if (ret < 0) {
1375 MBEDTLS_SSL_DEBUG_RET(1,
1376 ("ssl_tls13_parse_supported_versions_ext"), ret);
1377 return ret;
1378 }
1379
Ronald Cronb828c7d2023-04-03 16:37:22 +02001380 /*
1381 * The supported versions extension was parsed successfully as the
1382 * value returned by ssl_tls13_parse_supported_versions_ext() is
1383 * positive. The return value is then equal to
1384 * MBEDTLS_SSL_VERSION_TLS1_2 or MBEDTLS_SSL_VERSION_TLS1_3, defining
1385 * the TLS version to negotiate.
1386 */
Ronald Cron5af4c7f2023-03-07 20:46:59 +01001387 if (MBEDTLS_SSL_VERSION_TLS1_2 == ret) {
1388 return SSL_CLIENT_HELLO_TLS1_2;
1389 }
Ronald Cron8c527d02023-03-07 15:47:47 +01001390 }
1391
1392 /*
1393 * We negotiate TLS 1.3.
Ronald Cron64582392023-03-07 09:21:40 +01001394 */
1395 ssl->tls_version = MBEDTLS_SSL_VERSION_TLS1_3;
Ronald Cron64582392023-03-07 09:21:40 +01001396 ssl->session_negotiate->tls_version = MBEDTLS_SSL_VERSION_TLS1_3;
1397 ssl->session_negotiate->endpoint = ssl->conf->endpoint;
Ronald Cron64582392023-03-07 09:21:40 +01001398
1399 /*
Ronald Crondad02b22023-04-06 09:57:52 +02001400 * We are negotiating the version 1.3 of the protocol. Do what we have
Ronald Croncada4102023-03-07 09:51:39 +01001401 * postponed: copy of the client random bytes, copy of the legacy session
Ronald Cron2f16b4e2023-03-07 10:07:32 +01001402 * identifier and selection of the TLS 1.3 cipher suite.
Ronald Crond540d992023-03-07 09:41:48 +01001403 */
1404 MBEDTLS_SSL_DEBUG_BUF(3, "client hello, random bytes",
1405 random, MBEDTLS_CLIENT_HELLO_RANDOM_LEN);
1406 memcpy(&handshake->randbytes[0], random, MBEDTLS_CLIENT_HELLO_RANDOM_LEN);
1407
Ronald Croncada4102023-03-07 09:51:39 +01001408 if (legacy_session_id_len > sizeof(ssl->session_negotiate->id)) {
1409 MBEDTLS_SSL_DEBUG_MSG(1, ("bad client hello message"));
1410 return MBEDTLS_ERR_SSL_DECODE_ERROR;
1411 }
1412 ssl->session_negotiate->id_len = legacy_session_id_len;
1413 MBEDTLS_SSL_DEBUG_BUF(3, "client hello, session id",
1414 legacy_session_id, legacy_session_id_len);
1415 memcpy(&ssl->session_negotiate->id[0],
1416 legacy_session_id, legacy_session_id_len);
1417
Ronald Crond540d992023-03-07 09:41:48 +01001418 /*
Jerry Yu5725f1c2022-08-21 17:27:16 +08001419 * Search for a matching ciphersuite
1420 */
Ronald Cron2f16b4e2023-03-07 10:07:32 +01001421 MBEDTLS_SSL_DEBUG_BUF(3, "client hello, list of cipher suites",
1422 cipher_suites, cipher_suites_len);
Jerry Yu5725f1c2022-08-21 17:27:16 +08001423
Ronald Cron89089cc2024-02-14 18:18:08 +01001424 ssl_tls13_select_ciphersuite(ssl, cipher_suites, cipher_suites_end,
1425 0, PSA_ALG_NONE, &handshake->ciphersuite_info);
Jerry Yu5725f1c2022-08-21 17:27:16 +08001426
Gilles Peskine449bd832023-01-11 14:50:10 +01001427 if (handshake->ciphersuite_info == NULL) {
1428 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE,
1429 MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE);
1430 return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
Jerry Yu5725f1c2022-08-21 17:27:16 +08001431 }
Ronald Cron89089cc2024-02-14 18:18:08 +01001432 ssl->session_negotiate->ciphersuite = handshake->ciphersuite_info->id;
1433
1434 MBEDTLS_SSL_DEBUG_MSG(2, ("selected ciphersuite: %04x - %s",
1435 ((unsigned) handshake->ciphersuite_info->id),
1436 handshake->ciphersuite_info->name));
Jerry Yuc1be19f2022-04-23 16:11:39 +08001437
XiaokangQian4080a7f2022-04-11 09:55:18 +00001438 /* ...
XiaokangQianb67384d2022-04-19 00:02:38 +00001439 * opaque legacy_compression_methods<1..2^8-1>;
XiaokangQian4080a7f2022-04-11 09:55:18 +00001440 * ...
XiaokangQian7807f9f2022-02-15 10:04:37 +00001441 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001442 if (p[0] != 1 || p[1] != MBEDTLS_SSL_COMPRESS_NULL) {
1443 MBEDTLS_SSL_DEBUG_MSG(1, ("bad legacy compression method"));
1444 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER,
1445 MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER);
1446 return MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001447 }
XiaokangQianb67384d2022-04-19 00:02:38 +00001448 p += 2;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001449
Jerry Yuc1be19f2022-04-23 16:11:39 +08001450 /* ...
XiaokangQianb67384d2022-04-19 00:02:38 +00001451 * Extension extensions<8..2^16-1>;
Jerry Yuc1be19f2022-04-23 16:11:39 +08001452 * ...
XiaokangQianb67384d2022-04-19 00:02:38 +00001453 * with Extension defined as:
1454 * struct {
1455 * ExtensionType extension_type;
1456 * opaque extension_data<0..2^16-1>;
1457 * } Extension;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001458 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001459 extensions_len = MBEDTLS_GET_UINT16_BE(p, 0);
XiaokangQian7807f9f2022-02-15 10:04:37 +00001460 p += 2;
Gilles Peskine449bd832023-01-11 14:50:10 +01001461 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, extensions_len);
XiaokangQiane8ff3502022-04-22 02:34:40 +00001462 extensions_end = p + extensions_len;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001463
Gilles Peskine449bd832023-01-11 14:50:10 +01001464 MBEDTLS_SSL_DEBUG_BUF(3, "client hello extensions", p, extensions_len);
Jerry Yu63a459c2022-10-31 13:38:40 +08001465 handshake->received_extensions = MBEDTLS_SSL_EXT_MASK_NONE;
Jerry Yu0c354a22022-08-29 15:25:36 +08001466
Gilles Peskine449bd832023-01-11 14:50:10 +01001467 while (p < extensions_end) {
XiaokangQian7807f9f2022-02-15 10:04:37 +00001468 unsigned int extension_type;
1469 size_t extension_data_len;
1470 const unsigned char *extension_data_end;
Jerry Yu263dbf72022-10-26 10:51:27 +08001471 uint32_t allowed_exts = MBEDTLS_SSL_TLS1_3_ALLOWED_EXTS_OF_CH;
1472
Ronald Cron5fbd2702024-02-14 10:03:36 +01001473 if (ssl->handshake->hello_retry_request_flag) {
Jerry Yu263dbf72022-10-26 10:51:27 +08001474 /* Do not accept early data extension in 2nd ClientHello */
1475 allowed_exts &= ~MBEDTLS_SSL_EXT_MASK(EARLY_DATA);
1476 }
XiaokangQian7807f9f2022-02-15 10:04:37 +00001477
Jerry Yu0c354a22022-08-29 15:25:36 +08001478 /* RFC 8446, section 4.2.11
Jerry Yu1c9247c2022-07-21 12:37:39 +08001479 *
1480 * The "pre_shared_key" extension MUST be the last extension in the
1481 * ClientHello (this facilitates implementation as described below).
1482 * Servers MUST check that it is the last extension and otherwise fail
1483 * the handshake with an "illegal_parameter" alert.
1484 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001485 if (handshake->received_extensions & MBEDTLS_SSL_EXT_MASK(PRE_SHARED_KEY)) {
Jerry Yu1c9247c2022-07-21 12:37:39 +08001486 MBEDTLS_SSL_DEBUG_MSG(
Gilles Peskine449bd832023-01-11 14:50:10 +01001487 3, ("pre_shared_key is not last extension."));
Jerry Yu1c9247c2022-07-21 12:37:39 +08001488 MBEDTLS_SSL_PEND_FATAL_ALERT(
1489 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER,
Gilles Peskine449bd832023-01-11 14:50:10 +01001490 MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER);
1491 return MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER;
Jerry Yu1c9247c2022-07-21 12:37:39 +08001492 }
1493
Gilles Peskine449bd832023-01-11 14:50:10 +01001494 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, extensions_end, 4);
1495 extension_type = MBEDTLS_GET_UINT16_BE(p, 0);
1496 extension_data_len = MBEDTLS_GET_UINT16_BE(p, 2);
XiaokangQian7807f9f2022-02-15 10:04:37 +00001497 p += 4;
1498
Gilles Peskine449bd832023-01-11 14:50:10 +01001499 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, extensions_end, extension_data_len);
XiaokangQian7807f9f2022-02-15 10:04:37 +00001500 extension_data_end = p + extension_data_len;
1501
Jerry Yuc4bf5d62022-10-29 09:08:47 +08001502 ret = mbedtls_ssl_tls13_check_received_extension(
Gilles Peskine449bd832023-01-11 14:50:10 +01001503 ssl, MBEDTLS_SSL_HS_CLIENT_HELLO, extension_type,
Jerry Yu263dbf72022-10-26 10:51:27 +08001504 allowed_exts);
Gilles Peskine449bd832023-01-11 14:50:10 +01001505 if (ret != 0) {
1506 return ret;
1507 }
Jerry Yue18dc7e2022-08-04 16:29:22 +08001508
Gilles Peskine449bd832023-01-11 14:50:10 +01001509 switch (extension_type) {
XiaokangQian40a35232022-05-07 09:02:40 +00001510#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
1511 case MBEDTLS_TLS_EXT_SERVERNAME:
Gilles Peskine449bd832023-01-11 14:50:10 +01001512 MBEDTLS_SSL_DEBUG_MSG(3, ("found ServerName extension"));
1513 ret = mbedtls_ssl_parse_server_name_ext(ssl, p,
1514 extension_data_end);
1515 if (ret != 0) {
XiaokangQian40a35232022-05-07 09:02:40 +00001516 MBEDTLS_SSL_DEBUG_RET(
Gilles Peskine449bd832023-01-11 14:50:10 +01001517 1, "mbedtls_ssl_parse_servername_ext", ret);
1518 return ret;
XiaokangQian40a35232022-05-07 09:02:40 +00001519 }
XiaokangQian40a35232022-05-07 09:02:40 +00001520 break;
1521#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
1522
Przemek Stekiel8c0a9532023-06-15 16:48:19 +02001523#if defined(PSA_WANT_ALG_ECDH) || defined(PSA_WANT_ALG_FFDH)
XiaokangQian7807f9f2022-02-15 10:04:37 +00001524 case MBEDTLS_TLS_EXT_SUPPORTED_GROUPS:
Gilles Peskine449bd832023-01-11 14:50:10 +01001525 MBEDTLS_SSL_DEBUG_MSG(3, ("found supported group extension"));
XiaokangQian7807f9f2022-02-15 10:04:37 +00001526
1527 /* Supported Groups Extension
1528 *
1529 * When sent by the client, the "supported_groups" extension
1530 * indicates the named groups which the client supports,
1531 * ordered from most preferred to least preferred.
1532 */
Jerry Yuc1be19f2022-04-23 16:11:39 +08001533 ret = ssl_tls13_parse_supported_groups_ext(
Gilles Peskine449bd832023-01-11 14:50:10 +01001534 ssl, p, extension_data_end);
1535 if (ret != 0) {
Xiaokang Qian9f1747b2023-03-30 02:50:04 +00001536 MBEDTLS_SSL_DEBUG_RET(
Xiaokang Qian8bce0e62023-04-04 10:15:48 +00001537 1, "ssl_tls13_parse_supported_groups_ext", ret);
Gilles Peskine449bd832023-01-11 14:50:10 +01001538 return ret;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001539 }
1540
XiaokangQian7807f9f2022-02-15 10:04:37 +00001541 break;
Przemek Stekiel8c0a9532023-06-15 16:48:19 +02001542#endif /* PSA_WANT_ALG_ECDH || PSA_WANT_ALG_FFDH*/
XiaokangQian7807f9f2022-02-15 10:04:37 +00001543
Valerio Settic9ae8622023-07-25 11:23:50 +02001544#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_EPHEMERAL_ENABLED)
XiaokangQian7807f9f2022-02-15 10:04:37 +00001545 case MBEDTLS_TLS_EXT_KEY_SHARE:
Gilles Peskine449bd832023-01-11 14:50:10 +01001546 MBEDTLS_SSL_DEBUG_MSG(3, ("found key share extension"));
XiaokangQian7807f9f2022-02-15 10:04:37 +00001547
1548 /*
1549 * Key Share Extension
1550 *
1551 * When sent by the client, the "key_share" extension
1552 * contains the endpoint's cryptographic parameters for
1553 * ECDHE/DHE key establishment methods.
1554 */
Jerry Yuc1be19f2022-04-23 16:11:39 +08001555 ret = ssl_tls13_parse_key_shares_ext(
Gilles Peskine449bd832023-01-11 14:50:10 +01001556 ssl, p, extension_data_end);
1557 if (ret == SSL_TLS1_3_PARSE_KEY_SHARES_EXT_NO_MATCH) {
Ronald Cron8a74f072023-06-14 17:59:29 +02001558 MBEDTLS_SSL_DEBUG_MSG(2, ("No usable share for key agreement."));
1559 no_usable_share_for_key_agreement = 1;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001560 }
1561
Gilles Peskine449bd832023-01-11 14:50:10 +01001562 if (ret < 0) {
Jerry Yu582dd062022-04-22 21:59:01 +08001563 MBEDTLS_SSL_DEBUG_RET(
Gilles Peskine449bd832023-01-11 14:50:10 +01001564 1, "ssl_tls13_parse_key_shares_ext", ret);
1565 return ret;
Jerry Yu582dd062022-04-22 21:59:01 +08001566 }
XiaokangQian7807f9f2022-02-15 10:04:37 +00001567
XiaokangQian7807f9f2022-02-15 10:04:37 +00001568 break;
Valerio Settic9ae8622023-07-25 11:23:50 +02001569#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_EPHEMERAL_ENABLED */
XiaokangQian7807f9f2022-02-15 10:04:37 +00001570
1571 case MBEDTLS_TLS_EXT_SUPPORTED_VERSIONS:
Ronald Cron8c527d02023-03-07 15:47:47 +01001572 /* Already parsed */
XiaokangQian7807f9f2022-02-15 10:04:37 +00001573 break;
1574
Ronald Cron41a443a2022-10-04 16:38:25 +02001575#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED)
Jerry Yue19e3b92022-07-08 12:04:51 +00001576 case MBEDTLS_TLS_EXT_PSK_KEY_EXCHANGE_MODES:
Xiaokang Qian9f1747b2023-03-30 02:50:04 +00001577 MBEDTLS_SSL_DEBUG_MSG(
1578 3, ("found psk key exchange modes extension"));
Jerry Yue19e3b92022-07-08 12:04:51 +00001579
1580 ret = ssl_tls13_parse_key_exchange_modes_ext(
Gilles Peskine449bd832023-01-11 14:50:10 +01001581 ssl, p, extension_data_end);
1582 if (ret != 0) {
Jerry Yue19e3b92022-07-08 12:04:51 +00001583 MBEDTLS_SSL_DEBUG_RET(
Gilles Peskine449bd832023-01-11 14:50:10 +01001584 1, "ssl_tls13_parse_key_exchange_modes_ext", ret);
1585 return ret;
Jerry Yue19e3b92022-07-08 12:04:51 +00001586 }
1587
Jerry Yue19e3b92022-07-08 12:04:51 +00001588 break;
Ronald Cron41a443a2022-10-04 16:38:25 +02001589#endif
Jerry Yue19e3b92022-07-08 12:04:51 +00001590
Jerry Yu1c105562022-07-10 06:32:38 +00001591 case MBEDTLS_TLS_EXT_PRE_SHARED_KEY:
Gilles Peskine449bd832023-01-11 14:50:10 +01001592 MBEDTLS_SSL_DEBUG_MSG(3, ("found pre_shared_key extension"));
1593 if ((handshake->received_extensions &
1594 MBEDTLS_SSL_EXT_MASK(PSK_KEY_EXCHANGE_MODES)) == 0) {
Jerry Yu13ab81d2022-07-22 23:17:11 +08001595 MBEDTLS_SSL_PEND_FATAL_ALERT(
1596 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER,
Gilles Peskine449bd832023-01-11 14:50:10 +01001597 MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER);
1598 return MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER;
Jerry Yu13ab81d2022-07-22 23:17:11 +08001599 }
Ronald Cron41a443a2022-10-04 16:38:25 +02001600#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED)
Jerry Yu1c105562022-07-10 06:32:38 +00001601 /* Delay processing of the PSK identity once we have
1602 * found out which algorithms to use. We keep a pointer
1603 * to the buffer and the size for later processing.
1604 */
Jerry Yu29d9faa2022-08-23 17:52:45 +08001605 pre_shared_key_ext = p;
Jerry Yu1c105562022-07-10 06:32:38 +00001606 pre_shared_key_ext_end = extension_data_end;
Jerry Yue18dc7e2022-08-04 16:29:22 +08001607#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED */
Jerry Yu1c105562022-07-10 06:32:38 +00001608 break;
Jerry Yu1c105562022-07-10 06:32:38 +00001609
XiaokangQianacb39922022-06-17 10:18:48 +00001610#if defined(MBEDTLS_SSL_ALPN)
1611 case MBEDTLS_TLS_EXT_ALPN:
Gilles Peskine449bd832023-01-11 14:50:10 +01001612 MBEDTLS_SSL_DEBUG_MSG(3, ("found alpn extension"));
XiaokangQianacb39922022-06-17 10:18:48 +00001613
Gilles Peskine449bd832023-01-11 14:50:10 +01001614 ret = mbedtls_ssl_parse_alpn_ext(ssl, p, extension_data_end);
1615 if (ret != 0) {
XiaokangQianacb39922022-06-17 10:18:48 +00001616 MBEDTLS_SSL_DEBUG_RET(
Gilles Peskine449bd832023-01-11 14:50:10 +01001617 1, ("mbedtls_ssl_parse_alpn_ext"), ret);
1618 return ret;
XiaokangQianacb39922022-06-17 10:18:48 +00001619 }
XiaokangQianacb39922022-06-17 10:18:48 +00001620 break;
1621#endif /* MBEDTLS_SSL_ALPN */
1622
Ronald Cron928cbd32022-10-04 16:14:26 +02001623#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED)
XiaokangQian7807f9f2022-02-15 10:04:37 +00001624 case MBEDTLS_TLS_EXT_SIG_ALG:
Gilles Peskine449bd832023-01-11 14:50:10 +01001625 MBEDTLS_SSL_DEBUG_MSG(3, ("found signature_algorithms extension"));
XiaokangQian7807f9f2022-02-15 10:04:37 +00001626
Gabor Mezei078e8032022-04-27 21:17:56 +02001627 ret = mbedtls_ssl_parse_sig_alg_ext(
Gilles Peskine449bd832023-01-11 14:50:10 +01001628 ssl, p, extension_data_end);
1629 if (ret != 0) {
Xiaokang Qian49f39c12023-04-06 02:31:02 +00001630 MBEDTLS_SSL_DEBUG_RET(
1631 1, "mbedtls_ssl_parse_sig_alg_ext", ret);
Gilles Peskine449bd832023-01-11 14:50:10 +01001632 return ret;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001633 }
XiaokangQian7807f9f2022-02-15 10:04:37 +00001634 break;
Ronald Cron928cbd32022-10-04 16:14:26 +02001635#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED */
XiaokangQian7807f9f2022-02-15 10:04:37 +00001636
Jan Bruckner151f6422023-02-10 12:45:19 +01001637#if defined(MBEDTLS_SSL_RECORD_SIZE_LIMIT)
1638 case MBEDTLS_TLS_EXT_RECORD_SIZE_LIMIT:
1639 MBEDTLS_SSL_DEBUG_MSG(3, ("found record_size_limit extension"));
1640
Xiaokang Qian9f1747b2023-03-30 02:50:04 +00001641 ret = mbedtls_ssl_tls13_parse_record_size_limit_ext(
1642 ssl, p, extension_data_end);
Jan Brucknerf482dcc2023-03-15 09:09:06 +01001643 if (ret != 0) {
1644 MBEDTLS_SSL_DEBUG_RET(
1645 1, ("mbedtls_ssl_tls13_parse_record_size_limit_ext"), ret);
1646 return ret;
1647 }
Jan Bruckner151f6422023-02-10 12:45:19 +01001648 break;
1649#endif /* MBEDTLS_SSL_RECORD_SIZE_LIMIT */
1650
XiaokangQian7807f9f2022-02-15 10:04:37 +00001651 default:
Jerry Yu79aa7212022-11-08 21:30:21 +08001652 MBEDTLS_SSL_PRINT_EXT(
Jerry Yu63a459c2022-10-31 13:38:40 +08001653 3, MBEDTLS_SSL_HS_CLIENT_HELLO,
Gilles Peskine449bd832023-01-11 14:50:10 +01001654 extension_type, "( ignored )");
Jerry Yu0c354a22022-08-29 15:25:36 +08001655 break;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001656 }
1657
1658 p += extension_data_len;
1659 }
1660
Gilles Peskine449bd832023-01-11 14:50:10 +01001661 MBEDTLS_SSL_PRINT_EXTS(3, MBEDTLS_SSL_HS_CLIENT_HELLO,
1662 handshake->received_extensions);
Jerry Yue18dc7e2022-08-04 16:29:22 +08001663
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +01001664 ret = mbedtls_ssl_add_hs_hdr_to_checksum(ssl,
1665 MBEDTLS_SSL_HS_CLIENT_HELLO,
1666 p - buf);
1667 if (0 != ret) {
1668 MBEDTLS_SSL_DEBUG_RET(1, ("mbedtls_ssl_add_hs_hdr_to_checksum"), ret);
1669 return ret;
1670 }
Jerry Yu032b15ce2022-07-11 06:10:03 +00001671
Ronald Cron41a443a2022-10-04 16:38:25 +02001672#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED)
XiaokangQian7807f9f2022-02-15 10:04:37 +00001673 /* Update checksum with either
1674 * - The entire content of the CH message, if no PSK extension is present
1675 * - The content up to but excluding the PSK extension, if present.
Ronald Cron12e72f12024-02-14 15:49:38 +01001676 * Always parse the pre-shared key extension when present in the
1677 * ClientHello even if some pre-requisites for PSK key exchange modes are
1678 * not met. That way we always validate the syntax of the extension.
XiaokangQian7807f9f2022-02-15 10:04:37 +00001679 */
Ronald Cron12e72f12024-02-14 15:49:38 +01001680 if (handshake->received_extensions & MBEDTLS_SSL_EXT_MASK(PRE_SHARED_KEY)) {
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +01001681 ret = handshake->update_checksum(ssl, buf,
1682 pre_shared_key_ext - buf);
1683 if (0 != ret) {
1684 MBEDTLS_SSL_DEBUG_RET(1, ("update_checksum"), ret);
1685 return ret;
1686 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001687 ret = ssl_tls13_parse_pre_shared_key_ext(ssl,
1688 pre_shared_key_ext,
1689 pre_shared_key_ext_end,
1690 cipher_suites,
Ronald Cron79cdd412024-02-20 17:19:28 +01001691 cipher_suites_end,
1692 &psk);
1693 if (ret == 0) {
1694 got_psk = 1;
1695 } else if (ret != MBEDTLS_ERR_SSL_UNKNOWN_IDENTITY) {
Jerry Yu63a459c2022-10-31 13:38:40 +08001696 MBEDTLS_SSL_DEBUG_RET(
Gilles Peskine449bd832023-01-11 14:50:10 +01001697 1, "ssl_tls13_parse_pre_shared_key_ext", ret);
1698 return ret;
Jerry Yu1c105562022-07-10 06:32:38 +00001699 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001700 } else
Ronald Cron41a443a2022-10-04 16:38:25 +02001701#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED */
Jerry Yu1c105562022-07-10 06:32:38 +00001702 {
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +01001703 ret = handshake->update_checksum(ssl, buf, p - buf);
1704 if (0 != ret) {
1705 MBEDTLS_SSL_DEBUG_RET(1, ("update_checksum"), ret);
1706 return ret;
1707 }
Jerry Yu1c105562022-07-10 06:32:38 +00001708 }
XiaokangQian7807f9f2022-02-15 10:04:37 +00001709
Ronald Cron79cdd412024-02-20 17:19:28 +01001710 /*
1711 * Determine the key exchange algorithm to use.
1712 * There are three types of key exchanges supported in TLS 1.3:
1713 * - (EC)DH with ECDSA,
1714 * - (EC)DH with PSK,
1715 * - plain PSK.
1716 *
1717 * The PSK-based key exchanges may additionally be used with 0-RTT.
1718 *
1719 * Our built-in order of preference is
1720 * 1 ) (EC)DHE-PSK Mode ( psk_ephemeral )
1721 * 2 ) Certificate Mode ( ephemeral )
1722 * 3 ) Plain PSK Mode ( psk )
1723 */
1724#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED)
1725 if (got_psk && (psk.key_exchange_mode ==
1726 MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK_EPHEMERAL)) {
1727 handshake->key_exchange_mode =
1728 MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK_EPHEMERAL;
1729 MBEDTLS_SSL_DEBUG_MSG(2, ("key exchange mode: psk_ephemeral"));
1730
1731 } else
1732#endif
1733 if (ssl_tls13_key_exchange_is_ephemeral_available(ssl)) {
1734 handshake->key_exchange_mode =
1735 MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL;
1736 MBEDTLS_SSL_DEBUG_MSG(2, ("key exchange mode: ephemeral"));
1737
1738 }
1739#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED)
1740 else if (got_psk && (psk.key_exchange_mode ==
1741 MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK)) {
1742 handshake->key_exchange_mode = MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK;
1743 MBEDTLS_SSL_DEBUG_MSG(2, ("key exchange mode: psk"));
1744 }
1745#endif
1746 else {
1747 MBEDTLS_SSL_DEBUG_MSG(
1748 1,
1749 ("ClientHello message misses mandatory extensions."));
1750 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_MISSING_EXTENSION,
1751 MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER);
1752 return MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER;
Gilles Peskine449bd832023-01-11 14:50:10 +01001753 }
XiaokangQian7807f9f2022-02-15 10:04:37 +00001754
Ronald Cron3e47eec2024-02-21 10:29:24 +01001755#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED)
Ronald Cron74a16292024-02-23 17:07:41 +01001756 if (handshake->key_exchange_mode &
1757 MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK_ALL) {
1758 handshake->ciphersuite_info = psk.ciphersuite_info;
1759 ssl->session_negotiate->ciphersuite = psk.ciphersuite_info->id;
1760
1761 MBEDTLS_SSL_DEBUG_MSG(2, ("Select PSK ciphersuite: %04x - %s",
1762 ((unsigned) psk.ciphersuite_info->id),
1763 psk.ciphersuite_info->name));
1764
1765 if (psk.type == MBEDTLS_SSL_TLS1_3_PSK_RESUMPTION) {
1766 handshake->resume = 1;
1767 }
Ronald Cron79cdd412024-02-20 17:19:28 +01001768 }
Ronald Cron3e47eec2024-02-21 10:29:24 +01001769#endif
Ronald Cron79cdd412024-02-20 17:19:28 +01001770
1771 if (handshake->key_exchange_mode !=
Ronald Cron8a74f072023-06-14 17:59:29 +02001772 MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK) {
1773 hrr_required = (no_usable_share_for_key_agreement != 0);
1774 }
1775
Gilles Peskine449bd832023-01-11 14:50:10 +01001776 mbedtls_ssl_optimize_checksum(ssl, handshake->ciphersuite_info);
Jerry Yue5834fd2022-08-29 20:16:09 +08001777
Gilles Peskine449bd832023-01-11 14:50:10 +01001778 return hrr_required ? SSL_CLIENT_HELLO_HRR_REQUIRED : SSL_CLIENT_HELLO_OK;
XiaokangQian75fe8c72022-06-15 09:42:45 +00001779}
1780
Jerry Yuab0da372023-02-08 13:55:24 +08001781#if defined(MBEDTLS_SSL_EARLY_DATA)
Jerry Yu4caf3ca2023-11-15 16:13:47 +08001782static int ssl_tls13_check_early_data_requirements(mbedtls_ssl_context *ssl)
Jerry Yuab0da372023-02-08 13:55:24 +08001783{
1784 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
1785
Jerry Yu985c9672022-12-04 14:06:30 +08001786 if (ssl->conf->early_data_enabled == MBEDTLS_SSL_EARLY_DATA_DISABLED) {
1787 MBEDTLS_SSL_DEBUG_MSG(
Jerry Yu985c9672022-12-04 14:06:30 +08001788 1,
Jerry Yu454dda32023-10-31 15:13:54 +08001789 ("EarlyData: rejected, feature disabled in server configuration."));
Jerry Yu4caf3ca2023-11-15 16:13:47 +08001790 return -1;
Ronald Cron7b6ee942024-01-12 10:29:55 +01001791 }
1792
Jerry Yu454dda32023-10-31 15:13:54 +08001793 if (!handshake->resume) {
1794 /* We currently support early data only in the case of PSKs established
1795 via a NewSessionTicket message thus in the case of a session
1796 resumption. */
Jerry Yu985c9672022-12-04 14:06:30 +08001797 MBEDTLS_SSL_DEBUG_MSG(
Jerry Yu7ef9fd82023-11-07 14:30:38 +08001798 1, ("EarlyData: rejected, not a session resumption."));
Jerry Yu4caf3ca2023-11-15 16:13:47 +08001799 return -1;
Jerry Yu985c9672022-12-04 14:06:30 +08001800 }
1801
Jerry Yu82fd6c12023-10-31 16:32:19 +08001802 /* RFC 8446 4.2.10
1803 *
1804 * In order to accept early data, the server MUST have accepted a PSK cipher
1805 * suite and selected the first key offered in the client's "pre_shared_key"
1806 * extension. In addition, it MUST verify that the following values are the
1807 * same as those associated with the selected PSK:
1808 * - The TLS version number
1809 * - The selected cipher suite
1810 * - The selected ALPN [RFC7301] protocol, if any
1811 *
1812 * NOTE:
Jerry Yu7ef9fd82023-11-07 14:30:38 +08001813 * - The TLS version number is checked in
1814 * ssl_tls13_offered_psks_check_identity_match_ticket().
1815 * - ALPN is not checked for the time being (TODO).
Jerry Yu82fd6c12023-10-31 16:32:19 +08001816 */
1817
1818 if (handshake->selected_identity != 0) {
1819 MBEDTLS_SSL_DEBUG_MSG(
Jerry Yu7ef9fd82023-11-07 14:30:38 +08001820 1, ("EarlyData: rejected, the selected key in "
1821 "`pre_shared_key` is not the first one."));
Jerry Yu4caf3ca2023-11-15 16:13:47 +08001822 return -1;
Jerry Yu82fd6c12023-10-31 16:32:19 +08001823 }
1824
1825 if (handshake->ciphersuite_info->id !=
1826 ssl->session_negotiate->ciphersuite) {
1827 MBEDTLS_SSL_DEBUG_MSG(
Jerry Yu7ef9fd82023-11-07 14:30:38 +08001828 1, ("EarlyData: rejected, the selected ciphersuite is not the one "
1829 "of the selected pre-shared key."));
Jerry Yu4caf3ca2023-11-15 16:13:47 +08001830 return -1;
Jerry Yu82fd6c12023-10-31 16:32:19 +08001831
1832 }
Jerry Yu985c9672022-12-04 14:06:30 +08001833
Pengyu Lv94a42cc2023-12-06 10:04:17 +08001834 if (!mbedtls_ssl_tls13_session_ticket_allow_early_data(ssl->session_negotiate)) {
Jerry Yufceddb32022-12-12 15:30:34 +08001835 MBEDTLS_SSL_DEBUG_MSG(
1836 1,
Jerry Yuea96ac32023-11-21 17:06:36 +08001837 ("EarlyData: rejected, early_data not allowed in ticket "
1838 "permission bits."));
Jerry Yu4caf3ca2023-11-15 16:13:47 +08001839 return -1;
Jerry Yufceddb32022-12-12 15:30:34 +08001840 }
Jerry Yu985c9672022-12-04 14:06:30 +08001841
Jerry Yu4caf3ca2023-11-15 16:13:47 +08001842 return 0;
Jerry Yuab0da372023-02-08 13:55:24 +08001843}
1844#endif /* MBEDTLS_SSL_EARLY_DATA */
1845
XiaokangQian75fe8c72022-06-15 09:42:45 +00001846/* Update the handshake state machine */
1847
Ronald Cronce7d76e2022-07-08 18:56:49 +02001848MBEDTLS_CHECK_RETURN_CRITICAL
Ronald Cron7b6ee942024-01-12 10:29:55 +01001849static int ssl_tls13_postprocess_client_hello(mbedtls_ssl_context *ssl,
1850 int hrr_required)
XiaokangQian75fe8c72022-06-15 09:42:45 +00001851{
1852 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1853
XiaokangQian7807f9f2022-02-15 10:04:37 +00001854 /*
XiaokangQianfb665a82022-06-15 03:57:21 +00001855 * Server certificate selection
1856 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001857 if (ssl->conf->f_cert_cb && (ret = ssl->conf->f_cert_cb(ssl)) != 0) {
1858 MBEDTLS_SSL_DEBUG_RET(1, "f_cert_cb", ret);
1859 return ret;
XiaokangQianfb665a82022-06-15 03:57:21 +00001860 }
1861#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
1862 ssl->handshake->sni_name = NULL;
1863 ssl->handshake->sni_name_len = 0;
1864#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
XiaokangQian7807f9f2022-02-15 10:04:37 +00001865
Gilles Peskine449bd832023-01-11 14:50:10 +01001866 ret = mbedtls_ssl_tls13_key_schedule_stage_early(ssl);
1867 if (ret != 0) {
1868 MBEDTLS_SSL_DEBUG_RET(1,
1869 "mbedtls_ssl_tls1_3_key_schedule_stage_early", ret);
1870 return ret;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001871 }
1872
Jerry Yuab0da372023-02-08 13:55:24 +08001873#if defined(MBEDTLS_SSL_EARLY_DATA)
Jerry Yu4caf3ca2023-11-15 16:13:47 +08001874 if (ssl->handshake->received_extensions & MBEDTLS_SSL_EXT_MASK(EARLY_DATA)) {
Ronald Cron31e2d832024-02-05 16:45:57 +01001875 ssl->handshake->early_data_accepted =
1876 (!hrr_required) && (ssl_tls13_check_early_data_requirements(ssl) == 0);
Jerry Yu4e9b70e2022-12-04 14:08:02 +08001877
Jerry Yu4caf3ca2023-11-15 16:13:47 +08001878 if (ssl->handshake->early_data_accepted) {
1879 ret = mbedtls_ssl_tls13_compute_early_transform(ssl);
1880 if (ret != 0) {
1881 MBEDTLS_SSL_DEBUG_RET(
1882 1, "mbedtls_ssl_tls13_compute_early_transform", ret);
1883 return ret;
1884 }
1885 } else {
1886 ssl->discard_early_data_record =
1887 hrr_required ?
1888 MBEDTLS_SSL_EARLY_DATA_DISCARD :
1889 MBEDTLS_SSL_EARLY_DATA_TRY_TO_DEPROTECT_AND_DISCARD;
Jerry Yu4e9b70e2022-12-04 14:08:02 +08001890 }
1891 }
Ronald Cron7b6ee942024-01-12 10:29:55 +01001892#else
1893 ((void) hrr_required);
Jerry Yuab0da372023-02-08 13:55:24 +08001894#endif /* MBEDTLS_SSL_EARLY_DATA */
1895
Gilles Peskine449bd832023-01-11 14:50:10 +01001896 return 0;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001897}
1898
1899/*
XiaokangQianed582dd2022-04-13 08:21:05 +00001900 * Main entry point from the state machine; orchestrates the otherfunctions.
1901 */
1902
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001903MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01001904static int ssl_tls13_process_client_hello(mbedtls_ssl_context *ssl)
XiaokangQianed582dd2022-04-13 08:21:05 +00001905{
1906
XiaokangQian08037552022-04-20 07:16:41 +00001907 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine449bd832023-01-11 14:50:10 +01001908 unsigned char *buf = NULL;
XiaokangQianed582dd2022-04-13 08:21:05 +00001909 size_t buflen = 0;
Jerry Yu4ca91402022-05-09 15:50:57 +08001910 int parse_client_hello_ret;
1911
Gilles Peskine449bd832023-01-11 14:50:10 +01001912 MBEDTLS_SSL_DEBUG_MSG(2, ("=> parse client hello"));
XiaokangQianed582dd2022-04-13 08:21:05 +00001913
Gilles Peskine449bd832023-01-11 14:50:10 +01001914 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_tls13_fetch_handshake_msg(
1915 ssl, MBEDTLS_SSL_HS_CLIENT_HELLO,
1916 &buf, &buflen));
XiaokangQianed582dd2022-04-13 08:21:05 +00001917
Gilles Peskine449bd832023-01-11 14:50:10 +01001918 MBEDTLS_SSL_PROC_CHK_NEG(ssl_tls13_parse_client_hello(ssl, buf,
1919 buf + buflen));
Ronald Cron5af4c7f2023-03-07 20:46:59 +01001920 parse_client_hello_ret = ret; /* Store positive return value of
1921 * parse_client_hello,
1922 * as negative error codes are handled
Jerry Yuf41553b2022-05-09 22:20:30 +08001923 * by MBEDTLS_SSL_PROC_CHK_NEG. */
Jerry Yu4ca91402022-05-09 15:50:57 +08001924
Ronald Cronb828c7d2023-04-03 16:37:22 +02001925 /*
Yanray Wang90acdc62023-12-08 10:29:42 +08001926 * Version 1.2 of the protocol has to be used for the handshake.
1927 * If TLS 1.2 is not supported, abort the handshake. Otherwise, set the
Ronald Cronb828c7d2023-04-03 16:37:22 +02001928 * ssl->keep_current_message flag for the ClientHello to be kept and parsed
1929 * as a TLS 1.2 ClientHello. We also change ssl->tls_version to
1930 * MBEDTLS_SSL_VERSION_TLS1_2 thus from now on mbedtls_ssl_handshake_step()
1931 * will dispatch to the TLS 1.2 state machine.
1932 */
Ronald Cron5af4c7f2023-03-07 20:46:59 +01001933 if (SSL_CLIENT_HELLO_TLS1_2 == parse_client_hello_ret) {
Yanray Wangfb0f47b2023-12-04 15:27:28 +08001934 /* Check if server supports TLS 1.2 */
Yanray Wang408ba6f2023-12-08 10:18:03 +08001935 if (!mbedtls_ssl_conf_is_tls12_enabled(ssl->conf)) {
Yanray Wangfb0f47b2023-12-04 15:27:28 +08001936 MBEDTLS_SSL_DEBUG_MSG(
Yanray Wang177e49a2023-12-08 10:51:04 +08001937 1, ("TLS 1.2 not supported."));
Yanray Wangfb0f47b2023-12-04 15:27:28 +08001938 MBEDTLS_SSL_PEND_FATAL_ALERT(
Yanray Wang2bef9172023-12-08 10:21:53 +08001939 MBEDTLS_SSL_ALERT_MSG_PROTOCOL_VERSION,
1940 MBEDTLS_ERR_SSL_BAD_PROTOCOL_VERSION);
1941 return MBEDTLS_ERR_SSL_BAD_PROTOCOL_VERSION;
Yanray Wangfb0f47b2023-12-04 15:27:28 +08001942 }
Ronald Cron5af4c7f2023-03-07 20:46:59 +01001943 ssl->keep_current_message = 1;
1944 ssl->tls_version = MBEDTLS_SSL_VERSION_TLS1_2;
1945 return 0;
1946 }
1947
Ronald Cron7b6ee942024-01-12 10:29:55 +01001948 MBEDTLS_SSL_PROC_CHK(
1949 ssl_tls13_postprocess_client_hello(ssl, parse_client_hello_ret ==
1950 SSL_CLIENT_HELLO_HRR_REQUIRED));
Jerry Yu582dd062022-04-22 21:59:01 +08001951
Ronald Cron5af4c7f2023-03-07 20:46:59 +01001952 if (SSL_CLIENT_HELLO_OK == parse_client_hello_ret) {
Gilles Peskine449bd832023-01-11 14:50:10 +01001953 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_SERVER_HELLO);
1954 } else {
1955 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_HELLO_RETRY_REQUEST);
1956 }
XiaokangQianed582dd2022-04-13 08:21:05 +00001957
1958cleanup:
1959
Gilles Peskine449bd832023-01-11 14:50:10 +01001960 MBEDTLS_SSL_DEBUG_MSG(2, ("<= parse client hello"));
1961 return ret;
XiaokangQianed582dd2022-04-13 08:21:05 +00001962}
1963
1964/*
Jerry Yu1c3e6882022-04-20 21:23:40 +08001965 * Handler for MBEDTLS_SSL_SERVER_HELLO
Jerry Yu5b64ae92022-03-30 17:15:02 +08001966 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001967MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01001968static int ssl_tls13_prepare_server_hello(mbedtls_ssl_context *ssl)
Jerry Yuf4b27e42022-03-30 17:32:21 +08001969{
Jerry Yu637a3f12022-04-20 21:37:58 +08001970 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1971 unsigned char *server_randbytes =
Gilles Peskine449bd832023-01-11 14:50:10 +01001972 ssl->handshake->randbytes + MBEDTLS_CLIENT_HELLO_RANDOM_LEN;
1973 if (ssl->conf->f_rng == NULL) {
1974 MBEDTLS_SSL_DEBUG_MSG(1, ("no RNG provided"));
1975 return MBEDTLS_ERR_SSL_NO_RNG;
Jerry Yuf4b27e42022-03-30 17:32:21 +08001976 }
1977
Gilles Peskine449bd832023-01-11 14:50:10 +01001978 if ((ret = ssl->conf->f_rng(ssl->conf->p_rng, server_randbytes,
1979 MBEDTLS_SERVER_HELLO_RANDOM_LEN)) != 0) {
1980 MBEDTLS_SSL_DEBUG_RET(1, "f_rng", ret);
1981 return ret;
Jerry Yuf4b27e42022-03-30 17:32:21 +08001982 }
1983
Gilles Peskine449bd832023-01-11 14:50:10 +01001984 MBEDTLS_SSL_DEBUG_BUF(3, "server hello, random bytes", server_randbytes,
1985 MBEDTLS_SERVER_HELLO_RANDOM_LEN);
Jerry Yuf4b27e42022-03-30 17:32:21 +08001986
1987#if defined(MBEDTLS_HAVE_TIME)
YxCda609132023-05-22 12:08:12 -07001988 ssl->session_negotiate->start = mbedtls_time(NULL);
Jerry Yuf4b27e42022-03-30 17:32:21 +08001989#endif /* MBEDTLS_HAVE_TIME */
1990
Gilles Peskine449bd832023-01-11 14:50:10 +01001991 return ret;
Jerry Yuf4b27e42022-03-30 17:32:21 +08001992}
1993
Jerry Yu3bf2c642022-03-30 22:02:12 +08001994/*
Jerry Yue74e04a2022-04-21 09:23:16 +08001995 * ssl_tls13_write_server_hello_supported_versions_ext ():
Jerry Yu3bf2c642022-03-30 22:02:12 +08001996 *
1997 * struct {
Jerry Yufb9f54d2022-04-06 10:08:34 +08001998 * ProtocolVersion selected_version;
Jerry Yu3bf2c642022-03-30 22:02:12 +08001999 * } SupportedVersions;
2000 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002001MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yue74e04a2022-04-21 09:23:16 +08002002static int ssl_tls13_write_server_hello_supported_versions_ext(
Gilles Peskine449bd832023-01-11 14:50:10 +01002003 mbedtls_ssl_context *ssl,
2004 unsigned char *buf,
2005 unsigned char *end,
2006 size_t *out_len)
Jerry Yu3bf2c642022-03-30 22:02:12 +08002007{
Jerry Yu3bf2c642022-03-30 22:02:12 +08002008 *out_len = 0;
2009
Gilles Peskine449bd832023-01-11 14:50:10 +01002010 MBEDTLS_SSL_DEBUG_MSG(3, ("server hello, write selected version"));
Jerry Yu3bf2c642022-03-30 22:02:12 +08002011
2012 /* Check if we have space to write the extension:
2013 * - extension_type (2 bytes)
2014 * - extension_data_length (2 bytes)
Jerry Yu349a6132022-04-14 20:52:56 +08002015 * - selected_version (2 bytes)
Jerry Yu3bf2c642022-03-30 22:02:12 +08002016 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002017 MBEDTLS_SSL_CHK_BUF_PTR(buf, end, 6);
Jerry Yu3bf2c642022-03-30 22:02:12 +08002018
Gilles Peskine449bd832023-01-11 14:50:10 +01002019 MBEDTLS_PUT_UINT16_BE(MBEDTLS_TLS_EXT_SUPPORTED_VERSIONS, buf, 0);
Jerry Yu3bf2c642022-03-30 22:02:12 +08002020
Gilles Peskine449bd832023-01-11 14:50:10 +01002021 MBEDTLS_PUT_UINT16_BE(2, buf, 2);
Jerry Yu3bf2c642022-03-30 22:02:12 +08002022
Gilles Peskine449bd832023-01-11 14:50:10 +01002023 mbedtls_ssl_write_version(buf + 4,
2024 ssl->conf->transport,
2025 ssl->tls_version);
Jerry Yu3bf2c642022-03-30 22:02:12 +08002026
Gilles Peskine449bd832023-01-11 14:50:10 +01002027 MBEDTLS_SSL_DEBUG_MSG(3, ("supported version: [%04x]",
2028 ssl->tls_version));
Jerry Yu3bf2c642022-03-30 22:02:12 +08002029
Jerry Yu349a6132022-04-14 20:52:56 +08002030 *out_len = 6;
Jerry Yu3bf2c642022-03-30 22:02:12 +08002031
Jerry Yub95dd362022-11-08 21:19:34 +08002032 mbedtls_ssl_tls13_set_hs_sent_ext_mask(
Gilles Peskine449bd832023-01-11 14:50:10 +01002033 ssl, MBEDTLS_TLS_EXT_SUPPORTED_VERSIONS);
Jerry Yub95dd362022-11-08 21:19:34 +08002034
Gilles Peskine449bd832023-01-11 14:50:10 +01002035 return 0;
Jerry Yu3bf2c642022-03-30 22:02:12 +08002036}
2037
Jerry Yud9436a12022-04-20 22:28:09 +08002038
Jerry Yu3bf2c642022-03-30 22:02:12 +08002039
2040/* Generate and export a single key share. For hybrid KEMs, this can
2041 * be called multiple times with the different components of the hybrid. */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002042MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002043static int ssl_tls13_generate_and_write_key_share(mbedtls_ssl_context *ssl,
2044 uint16_t named_group,
2045 unsigned char *buf,
2046 unsigned char *end,
2047 size_t *out_len)
Jerry Yu3bf2c642022-03-30 22:02:12 +08002048{
2049 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yu955ddd72022-04-22 22:27:33 +08002050
2051 *out_len = 0;
2052
Valerio Settic9ae8622023-07-25 11:23:50 +02002053#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_EPHEMERAL_ENABLED)
Przemek Stekiel29c219c2023-05-31 15:21:04 +02002054 if (mbedtls_ssl_tls13_named_group_is_ecdhe(named_group) ||
Przemek Stekield5f79e72023-06-29 09:08:43 +02002055 mbedtls_ssl_tls13_named_group_is_ffdh(named_group)) {
Przemek Stekiel408569f2023-07-06 11:26:44 +02002056 ret = mbedtls_ssl_tls13_generate_and_write_xxdh_key_exchange(
Gilles Peskine449bd832023-01-11 14:50:10 +01002057 ssl, named_group, buf, end, out_len);
2058 if (ret != 0) {
Jerry Yu89e103c2022-03-30 22:43:29 +08002059 MBEDTLS_SSL_DEBUG_RET(
Przemek Stekiel408569f2023-07-06 11:26:44 +02002060 1, "mbedtls_ssl_tls13_generate_and_write_xxdh_key_exchange",
Gilles Peskine449bd832023-01-11 14:50:10 +01002061 ret);
2062 return ret;
Jerry Yu3bf2c642022-03-30 22:02:12 +08002063 }
Gilles Peskine449bd832023-01-11 14:50:10 +01002064 } else
Valerio Settic9ae8622023-07-25 11:23:50 +02002065#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_EPHEMERAL_ENABLED */
Gilles Peskine449bd832023-01-11 14:50:10 +01002066 if (0 /* Other kinds of KEMs */) {
2067 } else {
Jerry Yu955ddd72022-04-22 22:27:33 +08002068 ((void) ssl);
2069 ((void) named_group);
2070 ((void) buf);
2071 ((void) end);
Jerry Yu3bf2c642022-03-30 22:02:12 +08002072 ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
2073 }
2074
Gilles Peskine449bd832023-01-11 14:50:10 +01002075 return ret;
Jerry Yu3bf2c642022-03-30 22:02:12 +08002076}
2077
2078/*
2079 * ssl_tls13_write_key_share_ext
2080 *
2081 * Structure of key_share extension in ServerHello:
2082 *
Jerry Yu1c3e6882022-04-20 21:23:40 +08002083 * struct {
2084 * NamedGroup group;
2085 * opaque key_exchange<1..2^16-1>;
2086 * } KeyShareEntry;
2087 * struct {
2088 * KeyShareEntry server_share;
2089 * } KeyShareServerHello;
Jerry Yu3bf2c642022-03-30 22:02:12 +08002090 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002091MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002092static int ssl_tls13_write_key_share_ext(mbedtls_ssl_context *ssl,
2093 unsigned char *buf,
2094 unsigned char *end,
2095 size_t *out_len)
Jerry Yu3bf2c642022-03-30 22:02:12 +08002096{
Jerry Yu955ddd72022-04-22 22:27:33 +08002097 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yu3bf2c642022-03-30 22:02:12 +08002098 unsigned char *p = buf;
Jerry Yu57d48412022-04-20 21:50:42 +08002099 uint16_t group = ssl->handshake->offered_group_id;
Jerry Yu3bf2c642022-03-30 22:02:12 +08002100 unsigned char *server_share = buf + 4;
Jerry Yu3bf2c642022-03-30 22:02:12 +08002101 size_t key_exchange_length;
Jerry Yu3bf2c642022-03-30 22:02:12 +08002102
2103 *out_len = 0;
2104
Gilles Peskine449bd832023-01-11 14:50:10 +01002105 MBEDTLS_SSL_DEBUG_MSG(3, ("server hello, adding key share extension"));
Jerry Yu3bf2c642022-03-30 22:02:12 +08002106
Gilles Peskine449bd832023-01-11 14:50:10 +01002107 MBEDTLS_SSL_DEBUG_MSG(2, ("server hello, write selected_group: %s (%04x)",
2108 mbedtls_ssl_named_group_to_str(group),
2109 group));
Jerry Yu58af2332022-09-06 11:19:31 +08002110
Jerry Yu3bf2c642022-03-30 22:02:12 +08002111 /* Check if we have space for header and length fields:
2112 * - extension_type (2 bytes)
2113 * - extension_data_length (2 bytes)
2114 * - group (2 bytes)
2115 * - key_exchange_length (2 bytes)
2116 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002117 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 8);
2118 MBEDTLS_PUT_UINT16_BE(MBEDTLS_TLS_EXT_KEY_SHARE, p, 0);
2119 MBEDTLS_PUT_UINT16_BE(group, server_share, 0);
Jerry Yu3bf2c642022-03-30 22:02:12 +08002120 p += 8;
Jerry Yu57d48412022-04-20 21:50:42 +08002121
Jerry Yu3bf2c642022-03-30 22:02:12 +08002122 /* When we introduce PQC-ECDHE hybrids, we'll want to call this
2123 * function multiple times. */
Jerry Yu955ddd72022-04-22 22:27:33 +08002124 ret = ssl_tls13_generate_and_write_key_share(
Gilles Peskine449bd832023-01-11 14:50:10 +01002125 ssl, group, server_share + 4, end, &key_exchange_length);
2126 if (ret != 0) {
2127 return ret;
2128 }
Jerry Yu3bf2c642022-03-30 22:02:12 +08002129 p += key_exchange_length;
Jerry Yuc1be19f2022-04-23 16:11:39 +08002130
Gilles Peskine449bd832023-01-11 14:50:10 +01002131 MBEDTLS_PUT_UINT16_BE(key_exchange_length, server_share + 2, 0);
Jerry Yu3bf2c642022-03-30 22:02:12 +08002132
Gilles Peskine449bd832023-01-11 14:50:10 +01002133 MBEDTLS_PUT_UINT16_BE(p - server_share, buf, 2);
Jerry Yu3bf2c642022-03-30 22:02:12 +08002134
Jerry Yu57d48412022-04-20 21:50:42 +08002135 *out_len = p - buf;
Jerry Yu955ddd72022-04-22 22:27:33 +08002136
Gilles Peskine449bd832023-01-11 14:50:10 +01002137 mbedtls_ssl_tls13_set_hs_sent_ext_mask(ssl, MBEDTLS_TLS_EXT_KEY_SHARE);
Jerry Yub95dd362022-11-08 21:19:34 +08002138
Gilles Peskine449bd832023-01-11 14:50:10 +01002139 return 0;
Jerry Yu3bf2c642022-03-30 22:02:12 +08002140}
Jerry Yud9436a12022-04-20 22:28:09 +08002141
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002142MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002143static int ssl_tls13_write_hrr_key_share_ext(mbedtls_ssl_context *ssl,
2144 unsigned char *buf,
2145 unsigned char *end,
2146 size_t *out_len)
Jerry Yu23f7a6f2022-04-23 15:16:45 +08002147{
2148 uint16_t selected_group = ssl->handshake->hrr_selected_group;
2149 /* key_share Extension
2150 *
2151 * struct {
2152 * select (Handshake.msg_type) {
2153 * ...
2154 * case hello_retry_request:
2155 * NamedGroup selected_group;
2156 * ...
2157 * };
2158 * } KeyShare;
2159 */
2160
2161 *out_len = 0;
2162
Jerry Yub0ac10b2022-05-05 11:10:08 +08002163 /*
2164 * For a pure PSK key exchange, there is no group to agree upon. The purpose
2165 * of the HRR is then to transmit a cookie to force the client to demonstrate
2166 * reachability at their apparent network address (primarily useful for DTLS).
2167 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002168 if (!mbedtls_ssl_tls13_key_exchange_mode_with_ephemeral(ssl)) {
2169 return 0;
2170 }
Jerry Yu23f7a6f2022-04-23 15:16:45 +08002171
2172 /* We should only send the key_share extension if the client's initial
2173 * key share was not acceptable. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002174 if (ssl->handshake->offered_group_id != 0) {
2175 MBEDTLS_SSL_DEBUG_MSG(4, ("Skip key_share extension in HRR"));
2176 return 0;
Jerry Yu23f7a6f2022-04-23 15:16:45 +08002177 }
2178
Gilles Peskine449bd832023-01-11 14:50:10 +01002179 if (selected_group == 0) {
2180 MBEDTLS_SSL_DEBUG_MSG(1, ("no matching named group found"));
2181 return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
Jerry Yu23f7a6f2022-04-23 15:16:45 +08002182 }
2183
Jerry Yub0ac10b2022-05-05 11:10:08 +08002184 /* Check if we have enough space:
2185 * - extension_type (2 bytes)
2186 * - extension_data_length (2 bytes)
2187 * - selected_group (2 bytes)
2188 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002189 MBEDTLS_SSL_CHK_BUF_PTR(buf, end, 6);
Jerry Yu23f7a6f2022-04-23 15:16:45 +08002190
Gilles Peskine449bd832023-01-11 14:50:10 +01002191 MBEDTLS_PUT_UINT16_BE(MBEDTLS_TLS_EXT_KEY_SHARE, buf, 0);
2192 MBEDTLS_PUT_UINT16_BE(2, buf, 2);
2193 MBEDTLS_PUT_UINT16_BE(selected_group, buf, 4);
Jerry Yu23f7a6f2022-04-23 15:16:45 +08002194
Gilles Peskine449bd832023-01-11 14:50:10 +01002195 MBEDTLS_SSL_DEBUG_MSG(3,
2196 ("HRR selected_group: %s (%x)",
2197 mbedtls_ssl_named_group_to_str(selected_group),
2198 selected_group));
Jerry Yu23f7a6f2022-04-23 15:16:45 +08002199
2200 *out_len = 6;
Jerry Yu23f7a6f2022-04-23 15:16:45 +08002201
Gilles Peskine449bd832023-01-11 14:50:10 +01002202 mbedtls_ssl_tls13_set_hs_sent_ext_mask(ssl, MBEDTLS_TLS_EXT_KEY_SHARE);
Jerry Yub95dd362022-11-08 21:19:34 +08002203
Gilles Peskine449bd832023-01-11 14:50:10 +01002204 return 0;
Jerry Yu23f7a6f2022-04-23 15:16:45 +08002205}
Jerry Yu3bf2c642022-03-30 22:02:12 +08002206
2207/*
2208 * Structure of ServerHello message:
2209 *
2210 * struct {
2211 * ProtocolVersion legacy_version = 0x0303; // TLS v1.2
2212 * Random random;
2213 * opaque legacy_session_id_echo<0..32>;
2214 * CipherSuite cipher_suite;
2215 * uint8 legacy_compression_method = 0;
2216 * Extension extensions<6..2^16-1>;
2217 * } ServerHello;
2218 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002219MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002220static int ssl_tls13_write_server_hello_body(mbedtls_ssl_context *ssl,
2221 unsigned char *buf,
2222 unsigned char *end,
2223 size_t *out_len,
2224 int is_hrr)
Jerry Yu56404d72022-03-30 17:36:13 +08002225{
Jerry Yu955ddd72022-04-22 22:27:33 +08002226 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yu3bf2c642022-03-30 22:02:12 +08002227 unsigned char *p = buf;
Jerry Yud9436a12022-04-20 22:28:09 +08002228 unsigned char *p_extensions_len;
Jerry Yufbe3e642022-04-25 19:31:51 +08002229 size_t output_len;
Jerry Yu3bf2c642022-03-30 22:02:12 +08002230
2231 *out_len = 0;
Jerry Yu50e00e32022-10-31 14:45:01 +08002232 ssl->handshake->sent_extensions = MBEDTLS_SSL_EXT_MASK_NONE;
Jerry Yu3bf2c642022-03-30 22:02:12 +08002233
Jerry Yucfc04b32022-04-21 09:31:58 +08002234 /* ...
2235 * ProtocolVersion legacy_version = 0x0303; // TLS 1.2
2236 * ...
2237 * with ProtocolVersion defined as:
2238 * uint16 ProtocolVersion;
Jerry Yu3bf2c642022-03-30 22:02:12 +08002239 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002240 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 2);
2241 MBEDTLS_PUT_UINT16_BE(0x0303, p, 0);
Jerry Yu3bf2c642022-03-30 22:02:12 +08002242 p += 2;
2243
Jerry Yu1c3e6882022-04-20 21:23:40 +08002244 /* ...
2245 * Random random;
2246 * ...
Jerry Yucfc04b32022-04-21 09:31:58 +08002247 * with Random defined as:
2248 * opaque Random[MBEDTLS_SERVER_HELLO_RANDOM_LEN];
Jerry Yu1c3e6882022-04-20 21:23:40 +08002249 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002250 MBEDTLS_SSL_CHK_BUF_PTR(p, end, MBEDTLS_SERVER_HELLO_RANDOM_LEN);
2251 if (is_hrr) {
2252 memcpy(p, mbedtls_ssl_tls13_hello_retry_request_magic,
2253 MBEDTLS_SERVER_HELLO_RANDOM_LEN);
2254 } else {
2255 memcpy(p, &ssl->handshake->randbytes[MBEDTLS_CLIENT_HELLO_RANDOM_LEN],
2256 MBEDTLS_SERVER_HELLO_RANDOM_LEN);
Jerry Yu23f7a6f2022-04-23 15:16:45 +08002257 }
Gilles Peskine449bd832023-01-11 14:50:10 +01002258 MBEDTLS_SSL_DEBUG_BUF(3, "server hello, random bytes",
2259 p, MBEDTLS_SERVER_HELLO_RANDOM_LEN);
Jerry Yu3bf2c642022-03-30 22:02:12 +08002260 p += MBEDTLS_SERVER_HELLO_RANDOM_LEN;
2261
Jerry Yucfc04b32022-04-21 09:31:58 +08002262 /* ...
2263 * opaque legacy_session_id_echo<0..32>;
2264 * ...
Jerry Yu3bf2c642022-03-30 22:02:12 +08002265 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002266 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 1 + ssl->session_negotiate->id_len);
2267 *p++ = (unsigned char) ssl->session_negotiate->id_len;
2268 if (ssl->session_negotiate->id_len > 0) {
2269 memcpy(p, &ssl->session_negotiate->id[0],
2270 ssl->session_negotiate->id_len);
Jerry Yu3bf2c642022-03-30 22:02:12 +08002271 p += ssl->session_negotiate->id_len;
Jerry Yu955ddd72022-04-22 22:27:33 +08002272
Gilles Peskine449bd832023-01-11 14:50:10 +01002273 MBEDTLS_SSL_DEBUG_BUF(3, "session id", ssl->session_negotiate->id,
2274 ssl->session_negotiate->id_len);
Jerry Yu3bf2c642022-03-30 22:02:12 +08002275 }
2276
Jerry Yucfc04b32022-04-21 09:31:58 +08002277 /* ...
2278 * CipherSuite cipher_suite;
2279 * ...
2280 * with CipherSuite defined as:
2281 * uint8 CipherSuite[2];
Jerry Yu3bf2c642022-03-30 22:02:12 +08002282 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002283 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 2);
2284 MBEDTLS_PUT_UINT16_BE(ssl->session_negotiate->ciphersuite, p, 0);
Jerry Yu3bf2c642022-03-30 22:02:12 +08002285 p += 2;
Gilles Peskine449bd832023-01-11 14:50:10 +01002286 MBEDTLS_SSL_DEBUG_MSG(3,
2287 ("server hello, chosen ciphersuite: %s ( id=%d )",
2288 mbedtls_ssl_get_ciphersuite_name(
2289 ssl->session_negotiate->ciphersuite),
2290 ssl->session_negotiate->ciphersuite));
Jerry Yu3bf2c642022-03-30 22:02:12 +08002291
Jerry Yucfc04b32022-04-21 09:31:58 +08002292 /* ...
2293 * uint8 legacy_compression_method = 0;
2294 * ...
2295 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002296 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 1);
Thomas Daubney31e03a82022-07-25 15:59:25 +01002297 *p++ = MBEDTLS_SSL_COMPRESS_NULL;
Jerry Yu3bf2c642022-03-30 22:02:12 +08002298
Jerry Yucfc04b32022-04-21 09:31:58 +08002299 /* ...
2300 * Extension extensions<6..2^16-1>;
2301 * ...
2302 * struct {
2303 * ExtensionType extension_type; (2 bytes)
2304 * opaque extension_data<0..2^16-1>;
2305 * } Extension;
2306 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002307 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 2);
Jerry Yud9436a12022-04-20 22:28:09 +08002308 p_extensions_len = p;
Jerry Yu3bf2c642022-03-30 22:02:12 +08002309 p += 2;
2310
Gilles Peskine449bd832023-01-11 14:50:10 +01002311 if ((ret = ssl_tls13_write_server_hello_supported_versions_ext(
2312 ssl, p, end, &output_len)) != 0) {
Jerry Yu955ddd72022-04-22 22:27:33 +08002313 MBEDTLS_SSL_DEBUG_RET(
Gilles Peskine449bd832023-01-11 14:50:10 +01002314 1, "ssl_tls13_write_server_hello_supported_versions_ext", ret);
2315 return ret;
Jerry Yu3bf2c642022-03-30 22:02:12 +08002316 }
2317 p += output_len;
2318
Gilles Peskine449bd832023-01-11 14:50:10 +01002319 if (mbedtls_ssl_tls13_key_exchange_mode_with_ephemeral(ssl)) {
2320 if (is_hrr) {
2321 ret = ssl_tls13_write_hrr_key_share_ext(ssl, p, end, &output_len);
2322 } else {
2323 ret = ssl_tls13_write_key_share_ext(ssl, p, end, &output_len);
2324 }
2325 if (ret != 0) {
2326 return ret;
2327 }
Jerry Yu3bf2c642022-03-30 22:02:12 +08002328 p += output_len;
2329 }
Jerry Yu3bf2c642022-03-30 22:02:12 +08002330
Ronald Cron41a443a2022-10-04 16:38:25 +02002331#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01002332 if (!is_hrr && mbedtls_ssl_tls13_key_exchange_mode_with_psk(ssl)) {
2333 ret = ssl_tls13_write_server_pre_shared_key_ext(ssl, p, end, &output_len);
2334 if (ret != 0) {
2335 MBEDTLS_SSL_DEBUG_RET(1, "ssl_tls13_write_server_pre_shared_key_ext",
2336 ret);
2337 return ret;
Jerry Yu032b15ce2022-07-11 06:10:03 +00002338 }
2339 p += output_len;
2340 }
Ronald Cron41a443a2022-10-04 16:38:25 +02002341#endif
Jerry Yu032b15ce2022-07-11 06:10:03 +00002342
Gilles Peskine449bd832023-01-11 14:50:10 +01002343 MBEDTLS_PUT_UINT16_BE(p - p_extensions_len - 2, p_extensions_len, 0);
Jerry Yu3bf2c642022-03-30 22:02:12 +08002344
Gilles Peskine449bd832023-01-11 14:50:10 +01002345 MBEDTLS_SSL_DEBUG_BUF(4, "server hello extensions",
2346 p_extensions_len, p - p_extensions_len);
Jerry Yu3bf2c642022-03-30 22:02:12 +08002347
Jerry Yud9436a12022-04-20 22:28:09 +08002348 *out_len = p - buf;
Jerry Yu3bf2c642022-03-30 22:02:12 +08002349
Gilles Peskine449bd832023-01-11 14:50:10 +01002350 MBEDTLS_SSL_DEBUG_BUF(3, "server hello", buf, *out_len);
Jerry Yu3bf2c642022-03-30 22:02:12 +08002351
Jerry Yu7de2ff02022-11-08 21:43:46 +08002352 MBEDTLS_SSL_PRINT_EXTS(
Jerry Yu4b8f2f72022-10-31 13:31:22 +08002353 3, is_hrr ? MBEDTLS_SSL_TLS1_3_HS_HELLO_RETRY_REQUEST :
Gilles Peskine449bd832023-01-11 14:50:10 +01002354 MBEDTLS_SSL_HS_SERVER_HELLO,
2355 ssl->handshake->sent_extensions);
Jerry Yu4b8f2f72022-10-31 13:31:22 +08002356
Gilles Peskine449bd832023-01-11 14:50:10 +01002357 return ret;
Jerry Yu56404d72022-03-30 17:36:13 +08002358}
2359
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002360MBEDTLS_CHECK_RETURN_CRITICAL
Xiaokang Qian934ce6f2023-02-06 10:23:04 +00002361static int ssl_tls13_finalize_server_hello(mbedtls_ssl_context *ssl)
Jerry Yue110d252022-05-05 10:19:22 +08002362{
2363 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine449bd832023-01-11 14:50:10 +01002364 ret = mbedtls_ssl_tls13_compute_handshake_transform(ssl);
2365 if (ret != 0) {
2366 MBEDTLS_SSL_DEBUG_RET(1,
2367 "mbedtls_ssl_tls13_compute_handshake_transform",
2368 ret);
2369 return ret;
Jerry Yue110d252022-05-05 10:19:22 +08002370 }
2371
Gilles Peskine449bd832023-01-11 14:50:10 +01002372 return ret;
Jerry Yue110d252022-05-05 10:19:22 +08002373}
2374
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002375MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002376static int ssl_tls13_write_server_hello(mbedtls_ssl_context *ssl)
Jerry Yu5b64ae92022-03-30 17:15:02 +08002377{
Jerry Yu637a3f12022-04-20 21:37:58 +08002378 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yuf4b27e42022-03-30 17:32:21 +08002379 unsigned char *buf;
2380 size_t buf_len, msg_len;
2381
Gilles Peskine449bd832023-01-11 14:50:10 +01002382 MBEDTLS_SSL_DEBUG_MSG(2, ("=> write server hello"));
Jerry Yuf4b27e42022-03-30 17:32:21 +08002383
Gilles Peskine449bd832023-01-11 14:50:10 +01002384 MBEDTLS_SSL_PROC_CHK(ssl_tls13_prepare_server_hello(ssl));
Jerry Yuf4b27e42022-03-30 17:32:21 +08002385
Xiaokang Qian9f1747b2023-03-30 02:50:04 +00002386 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_start_handshake_msg(
2387 ssl, MBEDTLS_SSL_HS_SERVER_HELLO, &buf, &buf_len));
Jerry Yuf4b27e42022-03-30 17:32:21 +08002388
Gilles Peskine449bd832023-01-11 14:50:10 +01002389 MBEDTLS_SSL_PROC_CHK(ssl_tls13_write_server_hello_body(ssl, buf,
2390 buf + buf_len,
2391 &msg_len,
2392 0));
Jerry Yuf4b27e42022-03-30 17:32:21 +08002393
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +01002394 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_add_hs_msg_to_checksum(
Manuel Pégourié-Gonnard43cc1272023-02-06 11:48:19 +01002395 ssl, MBEDTLS_SSL_HS_SERVER_HELLO, buf, msg_len));
Jerry Yuf4b27e42022-03-30 17:32:21 +08002396
Gilles Peskine449bd832023-01-11 14:50:10 +01002397 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_finish_handshake_msg(
2398 ssl, buf_len, msg_len));
Jerry Yu637a3f12022-04-20 21:37:58 +08002399
Xiaokang Qian934ce6f2023-02-06 10:23:04 +00002400 MBEDTLS_SSL_PROC_CHK(ssl_tls13_finalize_server_hello(ssl));
Jerry Yue110d252022-05-05 10:19:22 +08002401
Gabor Mezei7b39bf12022-05-24 16:04:14 +02002402#if defined(MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE)
2403 /* The server sends a dummy change_cipher_spec record immediately
2404 * after its first handshake message. This may either be after
2405 * a ServerHello or a HelloRetryRequest.
2406 */
Gabor Mezei96ae9262022-06-28 11:45:18 +02002407 mbedtls_ssl_handshake_set_state(
Gilles Peskine449bd832023-01-11 14:50:10 +01002408 ssl, MBEDTLS_SSL_SERVER_CCS_AFTER_SERVER_HELLO);
Gabor Mezei7b39bf12022-05-24 16:04:14 +02002409#else
Gilles Peskine449bd832023-01-11 14:50:10 +01002410 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_ENCRYPTED_EXTENSIONS);
Gabor Mezei7b39bf12022-05-24 16:04:14 +02002411#endif /* MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE */
Jerry Yue110d252022-05-05 10:19:22 +08002412
Jerry Yuf4b27e42022-03-30 17:32:21 +08002413cleanup:
2414
Gilles Peskine449bd832023-01-11 14:50:10 +01002415 MBEDTLS_SSL_DEBUG_MSG(2, ("<= write server hello"));
2416 return ret;
Jerry Yu5b64ae92022-03-30 17:15:02 +08002417}
2418
Jerry Yu23d1a252022-05-12 18:08:59 +08002419
2420/*
2421 * Handler for MBEDTLS_SSL_HELLO_RETRY_REQUEST
2422 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002423MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002424static int ssl_tls13_prepare_hello_retry_request(mbedtls_ssl_context *ssl)
Jerry Yu23d1a252022-05-12 18:08:59 +08002425{
2426 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Ronald Cron5fbd2702024-02-14 10:03:36 +01002427 if (ssl->handshake->hello_retry_request_flag) {
Gilles Peskine449bd832023-01-11 14:50:10 +01002428 MBEDTLS_SSL_DEBUG_MSG(1, ("Too many HRRs"));
2429 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE,
2430 MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE);
2431 return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
Jerry Yu23d1a252022-05-12 18:08:59 +08002432 }
2433
2434 /*
2435 * Create stateless transcript hash for HRR
2436 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002437 MBEDTLS_SSL_DEBUG_MSG(4, ("Reset transcript for HRR"));
2438 ret = mbedtls_ssl_reset_transcript_for_hrr(ssl);
2439 if (ret != 0) {
2440 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_reset_transcript_for_hrr", ret);
2441 return ret;
Jerry Yu23d1a252022-05-12 18:08:59 +08002442 }
Gilles Peskine449bd832023-01-11 14:50:10 +01002443 mbedtls_ssl_session_reset_msg_layer(ssl, 0);
Jerry Yu23d1a252022-05-12 18:08:59 +08002444
Gilles Peskine449bd832023-01-11 14:50:10 +01002445 return 0;
Jerry Yu23d1a252022-05-12 18:08:59 +08002446}
2447
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002448MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002449static int ssl_tls13_write_hello_retry_request(mbedtls_ssl_context *ssl)
Jerry Yu23d1a252022-05-12 18:08:59 +08002450{
2451 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
2452 unsigned char *buf;
2453 size_t buf_len, msg_len;
2454
Gilles Peskine449bd832023-01-11 14:50:10 +01002455 MBEDTLS_SSL_DEBUG_MSG(2, ("=> write hello retry request"));
Jerry Yu23d1a252022-05-12 18:08:59 +08002456
Gilles Peskine449bd832023-01-11 14:50:10 +01002457 MBEDTLS_SSL_PROC_CHK(ssl_tls13_prepare_hello_retry_request(ssl));
Jerry Yu23d1a252022-05-12 18:08:59 +08002458
Gilles Peskine449bd832023-01-11 14:50:10 +01002459 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_start_handshake_msg(
2460 ssl, MBEDTLS_SSL_HS_SERVER_HELLO,
2461 &buf, &buf_len));
Jerry Yu23d1a252022-05-12 18:08:59 +08002462
Gilles Peskine449bd832023-01-11 14:50:10 +01002463 MBEDTLS_SSL_PROC_CHK(ssl_tls13_write_server_hello_body(ssl, buf,
2464 buf + buf_len,
2465 &msg_len,
2466 1));
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +01002467 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_add_hs_msg_to_checksum(
Manuel Pégourié-Gonnard43cc1272023-02-06 11:48:19 +01002468 ssl, MBEDTLS_SSL_HS_SERVER_HELLO, buf, msg_len));
Jerry Yu23d1a252022-05-12 18:08:59 +08002469
2470
Gilles Peskine449bd832023-01-11 14:50:10 +01002471 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_finish_handshake_msg(ssl, buf_len,
2472 msg_len));
Jerry Yu23d1a252022-05-12 18:08:59 +08002473
Ronald Cron5fbd2702024-02-14 10:03:36 +01002474 ssl->handshake->hello_retry_request_flag = 1;
Jerry Yu23d1a252022-05-12 18:08:59 +08002475
Gabor Mezei7b39bf12022-05-24 16:04:14 +02002476#if defined(MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE)
2477 /* The server sends a dummy change_cipher_spec record immediately
2478 * after its first handshake message. This may either be after
2479 * a ServerHello or a HelloRetryRequest.
2480 */
Gabor Mezei96ae9262022-06-28 11:45:18 +02002481 mbedtls_ssl_handshake_set_state(
Gilles Peskine449bd832023-01-11 14:50:10 +01002482 ssl, MBEDTLS_SSL_SERVER_CCS_AFTER_HELLO_RETRY_REQUEST);
Gabor Mezei7b39bf12022-05-24 16:04:14 +02002483#else
Gilles Peskine449bd832023-01-11 14:50:10 +01002484 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_CLIENT_HELLO);
Gabor Mezei7b39bf12022-05-24 16:04:14 +02002485#endif /* MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE */
Jerry Yu23d1a252022-05-12 18:08:59 +08002486
2487cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +01002488 MBEDTLS_SSL_DEBUG_MSG(2, ("<= write hello retry request"));
2489 return ret;
Jerry Yu23d1a252022-05-12 18:08:59 +08002490}
2491
Jerry Yu5b64ae92022-03-30 17:15:02 +08002492/*
Jerry Yu4d3841a2022-04-16 12:37:19 +08002493 * Handler for MBEDTLS_SSL_ENCRYPTED_EXTENSIONS
2494 */
Jerry Yu4d3841a2022-04-16 12:37:19 +08002495
2496/*
2497 * struct {
2498 * Extension extensions<0..2 ^ 16 - 1>;
2499 * } EncryptedExtensions;
2500 *
2501 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002502MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002503static int ssl_tls13_write_encrypted_extensions_body(mbedtls_ssl_context *ssl,
2504 unsigned char *buf,
2505 unsigned char *end,
2506 size_t *out_len)
Jerry Yu4d3841a2022-04-16 12:37:19 +08002507{
XiaokangQianacb39922022-06-17 10:18:48 +00002508 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yu4d3841a2022-04-16 12:37:19 +08002509 unsigned char *p = buf;
2510 size_t extensions_len = 0;
Jerry Yu8937eb42022-05-03 12:12:14 +08002511 unsigned char *p_extensions_len;
XiaokangQianacb39922022-06-17 10:18:48 +00002512 size_t output_len;
Jerry Yu9da5e5a2022-05-03 15:46:09 +08002513
Jerry Yu4d3841a2022-04-16 12:37:19 +08002514 *out_len = 0;
2515
Gilles Peskine449bd832023-01-11 14:50:10 +01002516 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 2);
Jerry Yu8937eb42022-05-03 12:12:14 +08002517 p_extensions_len = p;
Jerry Yu4d3841a2022-04-16 12:37:19 +08002518 p += 2;
2519
Jerry Yu8937eb42022-05-03 12:12:14 +08002520 ((void) ssl);
XiaokangQianacb39922022-06-17 10:18:48 +00002521 ((void) ret);
2522 ((void) output_len);
2523
2524#if defined(MBEDTLS_SSL_ALPN)
Gilles Peskine449bd832023-01-11 14:50:10 +01002525 ret = mbedtls_ssl_write_alpn_ext(ssl, p, end, &output_len);
2526 if (ret != 0) {
2527 return ret;
2528 }
XiaokangQian95d5f542022-06-24 02:29:26 +00002529 p += output_len;
XiaokangQianacb39922022-06-17 10:18:48 +00002530#endif /* MBEDTLS_SSL_ALPN */
Jerry Yu4d3841a2022-04-16 12:37:19 +08002531
Jerry Yu71c14f12022-12-12 11:10:35 +08002532#if defined(MBEDTLS_SSL_EARLY_DATA)
Ronald Cron78a38f62024-02-01 18:30:31 +01002533 if (ssl->handshake->early_data_accepted) {
Jerry Yu52335392023-11-23 18:06:06 +08002534 ret = mbedtls_ssl_tls13_write_early_data_ext(
Jerry Yuc59c5862023-12-05 10:40:49 +08002535 ssl, 0, p, end, &output_len);
Jerry Yu71c14f12022-12-12 11:10:35 +08002536 if (ret != 0) {
2537 return ret;
2538 }
2539 p += output_len;
2540 }
2541#endif /* MBEDTLS_SSL_EARLY_DATA */
2542
Waleed Elmelegy47d29462024-01-03 17:31:52 +00002543#if defined(MBEDTLS_SSL_RECORD_SIZE_LIMIT)
Waleed Elmelegyfbe42742024-01-05 18:11:10 +00002544 if (ssl->handshake->received_extensions & MBEDTLS_SSL_EXT_MASK(RECORD_SIZE_LIMIT)) {
Waleed Elmelegyd2fc90e2024-01-04 18:04:53 +00002545 ret = mbedtls_ssl_tls13_write_record_size_limit_ext(
2546 ssl, p, end, &output_len);
2547 if (ret != 0) {
2548 return ret;
2549 }
2550 p += output_len;
Waleed Elmelegy47d29462024-01-03 17:31:52 +00002551 }
Waleed Elmelegy47d29462024-01-03 17:31:52 +00002552#endif
2553
Gilles Peskine449bd832023-01-11 14:50:10 +01002554 extensions_len = (p - p_extensions_len) - 2;
2555 MBEDTLS_PUT_UINT16_BE(extensions_len, p_extensions_len, 0);
Jerry Yu8937eb42022-05-03 12:12:14 +08002556
2557 *out_len = p - buf;
Jerry Yu4d3841a2022-04-16 12:37:19 +08002558
Gilles Peskine449bd832023-01-11 14:50:10 +01002559 MBEDTLS_SSL_DEBUG_BUF(4, "encrypted extensions", buf, *out_len);
Jerry Yu4d3841a2022-04-16 12:37:19 +08002560
Jerry Yu7de2ff02022-11-08 21:43:46 +08002561 MBEDTLS_SSL_PRINT_EXTS(
Gilles Peskine449bd832023-01-11 14:50:10 +01002562 3, MBEDTLS_SSL_HS_ENCRYPTED_EXTENSIONS, ssl->handshake->sent_extensions);
Jerry Yu4b8f2f72022-10-31 13:31:22 +08002563
Gilles Peskine449bd832023-01-11 14:50:10 +01002564 return 0;
Jerry Yu4d3841a2022-04-16 12:37:19 +08002565}
2566
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002567MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002568static int ssl_tls13_write_encrypted_extensions(mbedtls_ssl_context *ssl)
Jerry Yu4d3841a2022-04-16 12:37:19 +08002569{
2570 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
2571 unsigned char *buf;
Jerry Yu39730a72022-05-03 12:14:04 +08002572 size_t buf_len, msg_len;
Jerry Yu4d3841a2022-04-16 12:37:19 +08002573
Gilles Peskine449bd832023-01-11 14:50:10 +01002574 mbedtls_ssl_set_outbound_transform(ssl,
2575 ssl->handshake->transform_handshake);
Gabor Mezei54719122022-06-28 11:34:56 +02002576 MBEDTLS_SSL_DEBUG_MSG(
Gilles Peskine449bd832023-01-11 14:50:10 +01002577 3, ("switching to handshake transform for outbound data"));
Gabor Mezei54719122022-06-28 11:34:56 +02002578
Gilles Peskine449bd832023-01-11 14:50:10 +01002579 MBEDTLS_SSL_DEBUG_MSG(2, ("=> write encrypted extensions"));
Jerry Yu4d3841a2022-04-16 12:37:19 +08002580
Xiaokang Qian9f1747b2023-03-30 02:50:04 +00002581 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_start_handshake_msg(
2582 ssl, MBEDTLS_SSL_HS_ENCRYPTED_EXTENSIONS,
2583 &buf, &buf_len));
Jerry Yu4d3841a2022-04-16 12:37:19 +08002584
Gilles Peskine449bd832023-01-11 14:50:10 +01002585 MBEDTLS_SSL_PROC_CHK(ssl_tls13_write_encrypted_extensions_body(
2586 ssl, buf, buf + buf_len, &msg_len));
Jerry Yu4d3841a2022-04-16 12:37:19 +08002587
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +01002588 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_add_hs_msg_to_checksum(
Xiaokang Qian9f1747b2023-03-30 02:50:04 +00002589 ssl, MBEDTLS_SSL_HS_ENCRYPTED_EXTENSIONS,
2590 buf, msg_len));
Jerry Yu4d3841a2022-04-16 12:37:19 +08002591
Gilles Peskine449bd832023-01-11 14:50:10 +01002592 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_finish_handshake_msg(
2593 ssl, buf_len, msg_len));
Jerry Yu4d3841a2022-04-16 12:37:19 +08002594
Ronald Cron928cbd32022-10-04 16:14:26 +02002595#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01002596 if (mbedtls_ssl_tls13_key_exchange_mode_with_psk(ssl)) {
2597 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_SERVER_FINISHED);
2598 } else {
2599 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_CERTIFICATE_REQUEST);
2600 }
Jerry Yu8937eb42022-05-03 12:12:14 +08002601#else
Gilles Peskine449bd832023-01-11 14:50:10 +01002602 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_SERVER_FINISHED);
Jerry Yu8937eb42022-05-03 12:12:14 +08002603#endif
2604
Jerry Yu4d3841a2022-04-16 12:37:19 +08002605cleanup:
2606
Gilles Peskine449bd832023-01-11 14:50:10 +01002607 MBEDTLS_SSL_DEBUG_MSG(2, ("<= write encrypted extensions"));
2608 return ret;
Jerry Yu4d3841a2022-04-16 12:37:19 +08002609}
2610
Ronald Cron928cbd32022-10-04 16:14:26 +02002611#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED)
XiaokangQiana987e1d2022-05-07 01:25:58 +00002612#define SSL_CERTIFICATE_REQUEST_SEND_REQUEST 0
2613#define SSL_CERTIFICATE_REQUEST_SKIP 1
2614/* Coordination:
2615 * Check whether a CertificateRequest message should be written.
2616 * Returns a negative code on failure, or
2617 * - SSL_CERTIFICATE_REQUEST_SEND_REQUEST
2618 * - SSL_CERTIFICATE_REQUEST_SKIP
2619 * indicating if the writing of the CertificateRequest
2620 * should be skipped or not.
2621 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002622MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002623static int ssl_tls13_certificate_request_coordinate(mbedtls_ssl_context *ssl)
XiaokangQiana987e1d2022-05-07 01:25:58 +00002624{
2625 int authmode;
2626
XiaokangQian40a35232022-05-07 09:02:40 +00002627#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Gilles Peskine449bd832023-01-11 14:50:10 +01002628 if (ssl->handshake->sni_authmode != MBEDTLS_SSL_VERIFY_UNSET) {
XiaokangQian40a35232022-05-07 09:02:40 +00002629 authmode = ssl->handshake->sni_authmode;
Gilles Peskine449bd832023-01-11 14:50:10 +01002630 } else
XiaokangQian40a35232022-05-07 09:02:40 +00002631#endif
XiaokangQiana987e1d2022-05-07 01:25:58 +00002632 authmode = ssl->conf->authmode;
2633
Gilles Peskine449bd832023-01-11 14:50:10 +01002634 if (authmode == MBEDTLS_SSL_VERIFY_NONE) {
Ronald Croneac00ad2022-09-13 10:16:31 +02002635 ssl->session_negotiate->verify_result = MBEDTLS_X509_BADCERT_SKIP_VERIFY;
Gilles Peskine449bd832023-01-11 14:50:10 +01002636 return SSL_CERTIFICATE_REQUEST_SKIP;
Ronald Croneac00ad2022-09-13 10:16:31 +02002637 }
XiaokangQiana987e1d2022-05-07 01:25:58 +00002638
XiaokangQianc3017f62022-05-13 05:55:41 +00002639 ssl->handshake->certificate_request_sent = 1;
XiaokangQian189ded22022-05-10 08:12:17 +00002640
Gilles Peskine449bd832023-01-11 14:50:10 +01002641 return SSL_CERTIFICATE_REQUEST_SEND_REQUEST;
XiaokangQiana987e1d2022-05-07 01:25:58 +00002642}
2643
XiaokangQiancec9ae62022-05-06 07:28:50 +00002644/*
2645 * struct {
2646 * opaque certificate_request_context<0..2^8-1>;
2647 * Extension extensions<2..2^16-1>;
2648 * } CertificateRequest;
2649 *
2650 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002651MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002652static int ssl_tls13_write_certificate_request_body(mbedtls_ssl_context *ssl,
2653 unsigned char *buf,
2654 const unsigned char *end,
2655 size_t *out_len)
XiaokangQiancec9ae62022-05-06 07:28:50 +00002656{
2657 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
2658 unsigned char *p = buf;
XiaokangQianec6efb92022-05-06 09:53:10 +00002659 size_t output_len = 0;
XiaokangQiancec9ae62022-05-06 07:28:50 +00002660 unsigned char *p_extensions_len;
2661
2662 *out_len = 0;
2663
2664 /* Check if we have enough space:
2665 * - certificate_request_context (1 byte)
2666 * - extensions length (2 bytes)
2667 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002668 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 3);
XiaokangQiancec9ae62022-05-06 07:28:50 +00002669
2670 /*
2671 * Write certificate_request_context
2672 */
2673 /*
2674 * We use a zero length context for the normal handshake
2675 * messages. For post-authentication handshake messages
2676 * this request context would be set to a non-zero value.
2677 */
2678 *p++ = 0x0;
2679
2680 /*
2681 * Write extensions
2682 */
2683 /* The extensions must contain the signature_algorithms. */
2684 p_extensions_len = p;
2685 p += 2;
Gilles Peskine449bd832023-01-11 14:50:10 +01002686 ret = mbedtls_ssl_write_sig_alg_ext(ssl, p, end, &output_len);
2687 if (ret != 0) {
2688 return ret;
2689 }
XiaokangQiancec9ae62022-05-06 07:28:50 +00002690
XiaokangQianec6efb92022-05-06 09:53:10 +00002691 p += output_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01002692 MBEDTLS_PUT_UINT16_BE(p - p_extensions_len - 2, p_extensions_len, 0);
XiaokangQiancec9ae62022-05-06 07:28:50 +00002693
2694 *out_len = p - buf;
2695
Jerry Yu7de2ff02022-11-08 21:43:46 +08002696 MBEDTLS_SSL_PRINT_EXTS(
Gilles Peskine449bd832023-01-11 14:50:10 +01002697 3, MBEDTLS_SSL_HS_CERTIFICATE_REQUEST, ssl->handshake->sent_extensions);
Jerry Yu4b8f2f72022-10-31 13:31:22 +08002698
Gilles Peskine449bd832023-01-11 14:50:10 +01002699 return 0;
XiaokangQiancec9ae62022-05-06 07:28:50 +00002700}
2701
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002702MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002703static int ssl_tls13_write_certificate_request(mbedtls_ssl_context *ssl)
XiaokangQiancec9ae62022-05-06 07:28:50 +00002704{
2705 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
2706
Gilles Peskine449bd832023-01-11 14:50:10 +01002707 MBEDTLS_SSL_DEBUG_MSG(2, ("=> write certificate request"));
XiaokangQiancec9ae62022-05-06 07:28:50 +00002708
Gilles Peskine449bd832023-01-11 14:50:10 +01002709 MBEDTLS_SSL_PROC_CHK_NEG(ssl_tls13_certificate_request_coordinate(ssl));
XiaokangQiancec9ae62022-05-06 07:28:50 +00002710
Gilles Peskine449bd832023-01-11 14:50:10 +01002711 if (ret == SSL_CERTIFICATE_REQUEST_SEND_REQUEST) {
XiaokangQiancec9ae62022-05-06 07:28:50 +00002712 unsigned char *buf;
2713 size_t buf_len, msg_len;
2714
Xiaokang Qian9f1747b2023-03-30 02:50:04 +00002715 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_start_handshake_msg(
2716 ssl, MBEDTLS_SSL_HS_CERTIFICATE_REQUEST,
2717 &buf, &buf_len));
XiaokangQiancec9ae62022-05-06 07:28:50 +00002718
Gilles Peskine449bd832023-01-11 14:50:10 +01002719 MBEDTLS_SSL_PROC_CHK(ssl_tls13_write_certificate_request_body(
2720 ssl, buf, buf + buf_len, &msg_len));
XiaokangQiancec9ae62022-05-06 07:28:50 +00002721
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +01002722 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_add_hs_msg_to_checksum(
Xiaokang Qian9f1747b2023-03-30 02:50:04 +00002723 ssl, MBEDTLS_SSL_HS_CERTIFICATE_REQUEST,
2724 buf, msg_len));
XiaokangQiancec9ae62022-05-06 07:28:50 +00002725
Gilles Peskine449bd832023-01-11 14:50:10 +01002726 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_finish_handshake_msg(
2727 ssl, buf_len, msg_len));
2728 } else if (ret == SSL_CERTIFICATE_REQUEST_SKIP) {
2729 MBEDTLS_SSL_DEBUG_MSG(2, ("<= skip write certificate request"));
XiaokangQiancec9ae62022-05-06 07:28:50 +00002730 ret = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01002731 } else {
2732 MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
XiaokangQiancec9ae62022-05-06 07:28:50 +00002733 ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
2734 goto cleanup;
2735 }
2736
Gilles Peskine449bd832023-01-11 14:50:10 +01002737 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_SERVER_CERTIFICATE);
XiaokangQiancec9ae62022-05-06 07:28:50 +00002738cleanup:
2739
Gilles Peskine449bd832023-01-11 14:50:10 +01002740 MBEDTLS_SSL_DEBUG_MSG(2, ("<= write certificate request"));
2741 return ret;
XiaokangQiancec9ae62022-05-06 07:28:50 +00002742}
XiaokangQiancec9ae62022-05-06 07:28:50 +00002743
Jerry Yu4d3841a2022-04-16 12:37:19 +08002744/*
Jerry Yuc6e6dbf2022-04-16 19:42:57 +08002745 * Handler for MBEDTLS_SSL_SERVER_CERTIFICATE
Jerry Yu83da34e2022-04-16 13:59:52 +08002746 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002747MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002748static int ssl_tls13_write_server_certificate(mbedtls_ssl_context *ssl)
Jerry Yu83da34e2022-04-16 13:59:52 +08002749{
Jerry Yu5a26f302022-05-10 20:46:40 +08002750 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
XiaokangQianfb665a82022-06-15 03:57:21 +00002751
2752#if defined(MBEDTLS_X509_CRT_PARSE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01002753 if ((ssl_tls13_pick_key_cert(ssl) != 0) ||
2754 mbedtls_ssl_own_cert(ssl) == NULL) {
2755 MBEDTLS_SSL_DEBUG_MSG(2, ("No certificate available."));
2756 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE,
2757 MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE);
2758 return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
Jerry Yu5a26f302022-05-10 20:46:40 +08002759 }
XiaokangQianfb665a82022-06-15 03:57:21 +00002760#endif /* MBEDTLS_X509_CRT_PARSE_C */
Jerry Yu5a26f302022-05-10 20:46:40 +08002761
Gilles Peskine449bd832023-01-11 14:50:10 +01002762 ret = mbedtls_ssl_tls13_write_certificate(ssl);
2763 if (ret != 0) {
2764 return ret;
2765 }
2766 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_CERTIFICATE_VERIFY);
2767 return 0;
Jerry Yu83da34e2022-04-16 13:59:52 +08002768}
2769
2770/*
Jerry Yuc6e6dbf2022-04-16 19:42:57 +08002771 * Handler for MBEDTLS_SSL_CERTIFICATE_VERIFY
Jerry Yu83da34e2022-04-16 13:59:52 +08002772 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002773MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002774static int ssl_tls13_write_certificate_verify(mbedtls_ssl_context *ssl)
Jerry Yu83da34e2022-04-16 13:59:52 +08002775{
Gilles Peskine449bd832023-01-11 14:50:10 +01002776 int ret = mbedtls_ssl_tls13_write_certificate_verify(ssl);
2777 if (ret != 0) {
2778 return ret;
2779 }
2780 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_SERVER_FINISHED);
2781 return 0;
Jerry Yu83da34e2022-04-16 13:59:52 +08002782}
Ronald Cron928cbd32022-10-04 16:14:26 +02002783#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED */
Jerry Yu83da34e2022-04-16 13:59:52 +08002784
Jerry Yu9b72e392023-12-01 16:27:08 +08002785/*
2786 * RFC 8446 section A.2
2787 *
2788 * | Send ServerHello
2789 * | K_send = handshake
2790 * | Send EncryptedExtensions
2791 * | [Send CertificateRequest]
2792 * Can send | [Send Certificate + CertificateVerify]
2793 * app data | Send Finished
2794 * after --> | K_send = application
2795 * here +--------+--------+
2796 * No 0-RTT | | 0-RTT
2797 * | |
2798 * K_recv = handshake | | K_recv = early data
2799 * [Skip decrypt errors] | +------> WAIT_EOED -+
2800 * | | Recv | | Recv EndOfEarlyData
2801 * | | early data | | K_recv = handshake
2802 * | +------------+ |
2803 * | |
2804 * +> WAIT_FLIGHT2 <--------+
2805 * |
2806 * +--------+--------+
2807 * No auth | | Client auth
2808 * | |
2809 * | v
2810 * | WAIT_CERT
2811 * | Recv | | Recv Certificate
2812 * | empty | v
2813 * | Certificate | WAIT_CV
2814 * | | | Recv
2815 * | v | CertificateVerify
2816 * +-> WAIT_FINISHED <---+
2817 * | Recv Finished
2818 *
2819 *
2820 * The following function handles the state changes after WAIT_FLIGHT2 in the
Jerry Yu3be85072023-12-04 09:58:54 +08002821 * above diagram. We are not going to receive early data related messages
2822 * anymore, prepare to receive the first handshake message of the client
2823 * second flight.
Jerry Yu9b72e392023-12-01 16:27:08 +08002824 */
Jerry Yu3be85072023-12-04 09:58:54 +08002825static void ssl_tls13_prepare_for_handshake_second_flight(
2826 mbedtls_ssl_context *ssl)
Jerry Yu9b72e392023-12-01 16:27:08 +08002827{
Jerry Yu9b72e392023-12-01 16:27:08 +08002828 if (ssl->handshake->certificate_request_sent) {
2829 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_CLIENT_CERTIFICATE);
2830 } else {
Jerry Yu42020fb2023-12-05 17:35:53 +08002831 MBEDTLS_SSL_DEBUG_MSG(2, ("skip parse certificate"));
Jerry Yuebb1b1d2023-12-05 11:02:15 +08002832 MBEDTLS_SSL_DEBUG_MSG(2, ("skip parse certificate verify"));
Jerry Yuebb1b1d2023-12-05 11:02:15 +08002833
Jerry Yu9b72e392023-12-01 16:27:08 +08002834 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_CLIENT_FINISHED);
2835 }
Jerry Yu9b72e392023-12-01 16:27:08 +08002836}
2837
Jerry Yu83da34e2022-04-16 13:59:52 +08002838/*
Jerry Yud6e253d2022-05-18 13:59:24 +08002839 * Handler for MBEDTLS_SSL_SERVER_FINISHED
Jerry Yu69dd8d42022-04-16 12:51:26 +08002840 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002841MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002842static int ssl_tls13_write_server_finished(mbedtls_ssl_context *ssl)
Jerry Yu69dd8d42022-04-16 12:51:26 +08002843{
Jerry Yud6e253d2022-05-18 13:59:24 +08002844 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yu27bdc7c2022-04-16 13:33:27 +08002845
Gilles Peskine449bd832023-01-11 14:50:10 +01002846 ret = mbedtls_ssl_tls13_write_finished_message(ssl);
2847 if (ret != 0) {
2848 return ret;
2849 }
Jerry Yu27bdc7c2022-04-16 13:33:27 +08002850
Gilles Peskine449bd832023-01-11 14:50:10 +01002851 ret = mbedtls_ssl_tls13_compute_application_transform(ssl);
2852 if (ret != 0) {
Jerry Yue3d67cb2022-05-19 15:33:10 +08002853 MBEDTLS_SSL_PEND_FATAL_ALERT(
Gilles Peskine449bd832023-01-11 14:50:10 +01002854 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE,
2855 MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE);
2856 return ret;
Jerry Yue3d67cb2022-05-19 15:33:10 +08002857 }
XiaokangQianc3017f62022-05-13 05:55:41 +00002858
Jerry Yu87b5ed42022-12-12 13:07:07 +08002859#if defined(MBEDTLS_SSL_EARLY_DATA)
Ronald Cron78a38f62024-02-01 18:30:31 +01002860 if (ssl->handshake->early_data_accepted) {
Jerry Yu0af63dc2023-12-01 17:14:51 +08002861 /* See RFC 8446 section A.2 for more information */
Jerry Yu87b5ed42022-12-12 13:07:07 +08002862 MBEDTLS_SSL_DEBUG_MSG(
2863 1, ("Switch to early keys for inbound traffic. "
2864 "( K_recv = early data )"));
2865 mbedtls_ssl_set_inbound_transform(
2866 ssl, ssl->handshake->transform_earlydata);
2867 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_END_OF_EARLY_DATA);
2868 return 0;
2869 }
2870#endif /* MBEDTLS_SSL_EARLY_DATA */
Jerry Yu0af63dc2023-12-01 17:14:51 +08002871 MBEDTLS_SSL_DEBUG_MSG(
2872 1, ("Switch to handshake keys for inbound traffic "
2873 "( K_recv = handshake )"));
Gilles Peskine449bd832023-01-11 14:50:10 +01002874 mbedtls_ssl_set_inbound_transform(ssl, ssl->handshake->transform_handshake);
XiaokangQian189ded22022-05-10 08:12:17 +00002875
Jerry Yu3be85072023-12-04 09:58:54 +08002876 ssl_tls13_prepare_for_handshake_second_flight(ssl);
Jerry Yu7d8c3fe2022-12-12 12:59:44 +08002877
2878 return 0;
2879}
2880
Jerry Yu87b5ed42022-12-12 13:07:07 +08002881#if defined(MBEDTLS_SSL_EARLY_DATA)
2882/*
Jerry Yu59d420f2023-12-01 16:30:34 +08002883 * Handler for MBEDTLS_SSL_END_OF_EARLY_DATA
Jerry Yu87b5ed42022-12-12 13:07:07 +08002884 */
Jerry Yu3be85072023-12-04 09:58:54 +08002885#define SSL_GOT_END_OF_EARLY_DATA 0
Jerry Yub55f9eb2023-12-05 10:27:17 +08002886#define SSL_GOT_EARLY_DATA 1
Jerry Yud5c34962023-12-01 16:32:31 +08002887/* Coordination:
Jerry Yu3be85072023-12-04 09:58:54 +08002888 * Deals with the ambiguity of not knowing if the next message is an
2889 * EndOfEarlyData message or an application message containing early data.
Jerry Yud5c34962023-12-01 16:32:31 +08002890 * Returns a negative code on failure, or
Jerry Yu3be85072023-12-04 09:58:54 +08002891 * - SSL_GOT_END_OF_EARLY_DATA
Jerry Yub55f9eb2023-12-05 10:27:17 +08002892 * - SSL_GOT_EARLY_DATA
Jerry Yud5c34962023-12-01 16:32:31 +08002893 * indicating which message is received.
2894 */
2895MBEDTLS_CHECK_RETURN_CRITICAL
2896static int ssl_tls13_end_of_early_data_coordinate(mbedtls_ssl_context *ssl)
2897{
2898 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yub4ed4602023-12-01 16:34:00 +08002899
2900 if ((ret = mbedtls_ssl_read_record(ssl, 0)) != 0) {
2901 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_read_record", ret);
2902 return ret;
2903 }
2904 ssl->keep_current_message = 1;
2905
2906 if (ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
2907 ssl->in_msg[0] == MBEDTLS_SSL_HS_END_OF_EARLY_DATA) {
Jerry Yub55f9eb2023-12-05 10:27:17 +08002908 MBEDTLS_SSL_DEBUG_MSG(3, ("Received an end_of_early_data message."));
Jerry Yu3be85072023-12-04 09:58:54 +08002909 return SSL_GOT_END_OF_EARLY_DATA;
Jerry Yub4ed4602023-12-01 16:34:00 +08002910 }
2911
2912 if (ssl->in_msgtype == MBEDTLS_SSL_MSG_APPLICATION_DATA) {
Jerry Yub55f9eb2023-12-05 10:27:17 +08002913 MBEDTLS_SSL_DEBUG_MSG(3, ("Received early data"));
Jerry Yu6a5904d2023-12-06 17:11:12 +08002914 /* RFC 8446 section 4.6.1
2915 *
2916 * A server receiving more than max_early_data_size bytes of 0-RTT data
2917 * SHOULD terminate the connection with an "unexpected_message" alert.
2918 *
2919 * TODO: Add received data size check here.
2920 */
Ronald Croned7d4bf2024-01-31 07:55:19 +01002921 if (ssl->in_offt == NULL) {
2922 /* Set the reading pointer */
2923 ssl->in_offt = ssl->in_msg;
2924 }
Jerry Yub55f9eb2023-12-05 10:27:17 +08002925 return SSL_GOT_EARLY_DATA;
Jerry Yub4ed4602023-12-01 16:34:00 +08002926 }
2927
Jerry Yu7bb40a32023-12-04 10:04:15 +08002928 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE,
2929 MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE);
Jerry Yub4ed4602023-12-01 16:34:00 +08002930 return MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE;
Jerry Yud5c34962023-12-01 16:32:31 +08002931}
2932
2933MBEDTLS_CHECK_RETURN_CRITICAL
2934static int ssl_tls13_parse_end_of_early_data(mbedtls_ssl_context *ssl,
2935 const unsigned char *buf,
2936 const unsigned char *end)
2937{
Jerry Yu75c9ab72023-12-01 16:41:40 +08002938 /* RFC 8446 section 4.5
2939 *
2940 * struct {} EndOfEarlyData;
2941 */
Jerry Yufbf03992023-12-04 10:00:37 +08002942 if (buf != end) {
2943 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR,
2944 MBEDTLS_ERR_SSL_DECODE_ERROR);
2945 return MBEDTLS_ERR_SSL_DECODE_ERROR;
2946 }
2947 return 0;
Jerry Yud5c34962023-12-01 16:32:31 +08002948}
2949
Jerry Yud5c34962023-12-01 16:32:31 +08002950/*
2951 * RFC 8446 section A.2
2952 *
2953 * | Send ServerHello
2954 * | K_send = handshake
2955 * | Send EncryptedExtensions
2956 * | [Send CertificateRequest]
2957 * Can send | [Send Certificate + CertificateVerify]
2958 * app data | Send Finished
2959 * after --> | K_send = application
2960 * here +--------+--------+
2961 * No 0-RTT | | 0-RTT
2962 * | |
2963 * K_recv = handshake | | K_recv = early data
2964 * [Skip decrypt errors] | +------> WAIT_EOED -+
2965 * | | Recv | | Recv EndOfEarlyData
2966 * | | early data | | K_recv = handshake
2967 * | +------------+ |
2968 * | |
2969 * +> WAIT_FLIGHT2 <--------+
2970 * |
2971 * +--------+--------+
2972 * No auth | | Client auth
2973 * | |
2974 * | v
2975 * | WAIT_CERT
2976 * | Recv | | Recv Certificate
2977 * | empty | v
2978 * | Certificate | WAIT_CV
2979 * | | | Recv
2980 * | v | CertificateVerify
2981 * +-> WAIT_FINISHED <---+
2982 * | Recv Finished
2983 *
2984 * The function handles actions and state changes from 0-RTT to WAIT_FLIGHT2 in
2985 * the above diagram.
2986 */
Jerry Yu87b5ed42022-12-12 13:07:07 +08002987MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yu59d420f2023-12-01 16:30:34 +08002988static int ssl_tls13_process_end_of_early_data(mbedtls_ssl_context *ssl)
Jerry Yu87b5ed42022-12-12 13:07:07 +08002989{
2990 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yud5c34962023-12-01 16:32:31 +08002991
2992 MBEDTLS_SSL_DEBUG_MSG(2, ("=> ssl_tls13_process_end_of_early_data"));
2993
2994 MBEDTLS_SSL_PROC_CHK_NEG(ssl_tls13_end_of_early_data_coordinate(ssl));
2995
Jerry Yu3be85072023-12-04 09:58:54 +08002996 if (ret == SSL_GOT_END_OF_EARLY_DATA) {
Jerry Yud5c34962023-12-01 16:32:31 +08002997 unsigned char *buf;
2998 size_t buf_len;
2999
3000 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_tls13_fetch_handshake_msg(
3001 ssl, MBEDTLS_SSL_HS_END_OF_EARLY_DATA,
3002 &buf, &buf_len));
3003
3004 MBEDTLS_SSL_PROC_CHK(ssl_tls13_parse_end_of_early_data(
3005 ssl, buf, buf + buf_len));
3006
Jerry Yue9655122023-12-01 16:44:40 +08003007 MBEDTLS_SSL_DEBUG_MSG(
3008 1, ("Switch to handshake keys for inbound traffic"
3009 "( K_recv = handshake )"));
3010 mbedtls_ssl_set_inbound_transform(
3011 ssl, ssl->handshake->transform_handshake);
3012
Jerry Yud5c34962023-12-01 16:32:31 +08003013 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_add_hs_msg_to_checksum(
3014 ssl, MBEDTLS_SSL_HS_END_OF_EARLY_DATA,
3015 buf, buf_len));
Jerry Yue9655122023-12-01 16:44:40 +08003016
Jerry Yu3be85072023-12-04 09:58:54 +08003017 ssl_tls13_prepare_for_handshake_second_flight(ssl);
Jerry Yud5c34962023-12-01 16:32:31 +08003018
Jerry Yub55f9eb2023-12-05 10:27:17 +08003019 } else if (ret == SSL_GOT_EARLY_DATA) {
Jerry Yud9ca3542023-12-06 17:23:52 +08003020 ret = MBEDTLS_ERR_SSL_RECEIVED_EARLY_DATA;
3021 goto cleanup;
Jerry Yud5c34962023-12-01 16:32:31 +08003022 } else {
3023 MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
3024 ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
3025 goto cleanup;
3026 }
3027
Jerry Yud5c34962023-12-01 16:32:31 +08003028cleanup:
3029 MBEDTLS_SSL_DEBUG_MSG(2, ("<= ssl_tls13_process_end_of_early_data"));
Jerry Yu87b5ed42022-12-12 13:07:07 +08003030 return ret;
3031}
3032#endif /* MBEDTLS_SSL_EARLY_DATA */
3033
Jerry Yu69dd8d42022-04-16 12:51:26 +08003034/*
Jerry Yud6e253d2022-05-18 13:59:24 +08003035 * Handler for MBEDTLS_SSL_CLIENT_FINISHED
Jerry Yu69dd8d42022-04-16 12:51:26 +08003036 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02003037MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01003038static int ssl_tls13_process_client_finished(mbedtls_ssl_context *ssl)
Jerry Yu69dd8d42022-04-16 12:51:26 +08003039{
Jerry Yud6e253d2022-05-18 13:59:24 +08003040 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
3041
Gilles Peskine449bd832023-01-11 14:50:10 +01003042 ret = mbedtls_ssl_tls13_process_finished_message(ssl);
3043 if (ret != 0) {
3044 return ret;
Jerry Yue3d67cb2022-05-19 15:33:10 +08003045 }
3046
Gilles Peskine449bd832023-01-11 14:50:10 +01003047 ret = mbedtls_ssl_tls13_compute_resumption_master_secret(ssl);
3048 if (ret != 0) {
Xiaokang Qian9f1747b2023-03-30 02:50:04 +00003049 MBEDTLS_SSL_DEBUG_RET(
3050 1, "mbedtls_ssl_tls13_compute_resumption_master_secret", ret);
Gilles Peskine449bd832023-01-11 14:50:10 +01003051 }
3052
3053 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_HANDSHAKE_WRAPUP);
3054 return 0;
Jerry Yu69dd8d42022-04-16 12:51:26 +08003055}
3056
3057/*
Jerry Yud6e253d2022-05-18 13:59:24 +08003058 * Handler for MBEDTLS_SSL_HANDSHAKE_WRAPUP
Jerry Yu69dd8d42022-04-16 12:51:26 +08003059 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02003060MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01003061static int ssl_tls13_handshake_wrapup(mbedtls_ssl_context *ssl)
Jerry Yu69dd8d42022-04-16 12:51:26 +08003062{
Gilles Peskine449bd832023-01-11 14:50:10 +01003063 MBEDTLS_SSL_DEBUG_MSG(2, ("handshake: done"));
Jerry Yu03ed50b2022-04-16 17:13:30 +08003064
Gilles Peskine449bd832023-01-11 14:50:10 +01003065 mbedtls_ssl_tls13_handshake_wrapup(ssl);
Jerry Yue67bef42022-07-07 07:29:42 +00003066
Pengyu Lv3643fdb2023-01-12 16:46:28 +08003067#if defined(MBEDTLS_SSL_SESSION_TICKETS) && \
3068 defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED)
Pengyu Lva1aa31b2022-12-13 13:49:59 +08003069/* TODO: Remove the check of SOME_PSK_ENABLED since SESSION_TICKETS requires
3070 * SOME_PSK_ENABLED to be enabled. Here is just to make CI happy. It is
3071 * expected to be resolved with issue#6395.
3072 */
Pengyu Lvc7af2c42022-12-01 16:33:00 +08003073 /* Sent NewSessionTicket message only when client supports PSK */
Pengyu Lvb2cfafb2023-11-14 13:56:13 +08003074 if (mbedtls_ssl_tls13_is_some_psk_supported(ssl)) {
Xiaokang Qian9f1747b2023-03-30 02:50:04 +00003075 mbedtls_ssl_handshake_set_state(
3076 ssl, MBEDTLS_SSL_TLS1_3_NEW_SESSION_TICKET);
Pengyu Lvc7af2c42022-12-01 16:33:00 +08003077 } else
Jerry Yufca4d572022-07-21 10:37:48 +08003078#endif
Pengyu Lv3643fdb2023-01-12 16:46:28 +08003079 {
3080 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_HANDSHAKE_OVER);
3081 }
Gilles Peskine449bd832023-01-11 14:50:10 +01003082 return 0;
Jerry Yu69dd8d42022-04-16 12:51:26 +08003083}
3084
3085/*
Jerry Yua8d3c502022-10-30 14:51:23 +08003086 * Handler for MBEDTLS_SSL_TLS1_3_NEW_SESSION_TICKET
Jerry Yue67bef42022-07-07 07:29:42 +00003087 */
3088#define SSL_NEW_SESSION_TICKET_SKIP 0
3089#define SSL_NEW_SESSION_TICKET_WRITE 1
3090MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01003091static int ssl_tls13_write_new_session_ticket_coordinate(mbedtls_ssl_context *ssl)
Jerry Yue67bef42022-07-07 07:29:42 +00003092{
3093 /* Check whether the use of session tickets is enabled */
Gilles Peskine449bd832023-01-11 14:50:10 +01003094 if (ssl->conf->f_ticket_write == NULL) {
3095 MBEDTLS_SSL_DEBUG_MSG(2, ("NewSessionTicket: disabled,"
3096 " callback is not set"));
3097 return SSL_NEW_SESSION_TICKET_SKIP;
Jerry Yud0766ec2022-09-22 10:46:57 +08003098 }
Gilles Peskine449bd832023-01-11 14:50:10 +01003099 if (ssl->conf->new_session_tickets_count == 0) {
3100 MBEDTLS_SSL_DEBUG_MSG(2, ("NewSessionTicket: disabled,"
3101 " configured count is zero"));
3102 return SSL_NEW_SESSION_TICKET_SKIP;
Jerry Yud0766ec2022-09-22 10:46:57 +08003103 }
3104
Gilles Peskine449bd832023-01-11 14:50:10 +01003105 if (ssl->handshake->new_session_tickets_count == 0) {
3106 MBEDTLS_SSL_DEBUG_MSG(2, ("NewSessionTicket: all tickets have "
3107 "been sent."));
3108 return SSL_NEW_SESSION_TICKET_SKIP;
Jerry Yue67bef42022-07-07 07:29:42 +00003109 }
3110
Gilles Peskine449bd832023-01-11 14:50:10 +01003111 return SSL_NEW_SESSION_TICKET_WRITE;
Jerry Yue67bef42022-07-07 07:29:42 +00003112}
3113
3114#if defined(MBEDTLS_SSL_SESSION_TICKETS)
3115MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01003116static int ssl_tls13_prepare_new_session_ticket(mbedtls_ssl_context *ssl,
3117 unsigned char *ticket_nonce,
3118 size_t ticket_nonce_size)
Jerry Yue67bef42022-07-07 07:29:42 +00003119{
3120 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
3121 mbedtls_ssl_session *session = ssl->session;
3122 mbedtls_ssl_ciphersuite_t *ciphersuite_info;
3123 psa_algorithm_t psa_hash_alg;
3124 int hash_length;
3125
Gilles Peskine449bd832023-01-11 14:50:10 +01003126 MBEDTLS_SSL_DEBUG_MSG(2, ("=> prepare NewSessionTicket msg"));
Jerry Yue67bef42022-07-07 07:29:42 +00003127
Pengyu Lv9f926952022-11-17 15:22:33 +08003128 /* Set ticket_flags depends on the advertised psk key exchange mode */
Pengyu Lv94a42cc2023-12-06 10:04:17 +08003129 mbedtls_ssl_tls13_session_clear_ticket_flags(
Pengyu Lv9eacb442022-12-09 14:39:19 +08003130 session, MBEDTLS_SSL_TLS1_3_TICKET_FLAGS_MASK);
Pengyu Lve6487fe2022-12-06 09:30:29 +08003131#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED)
Pengyu Lv94a42cc2023-12-06 10:04:17 +08003132 mbedtls_ssl_tls13_session_set_ticket_flags(
Pengyu Lv9eacb442022-12-09 14:39:19 +08003133 session, ssl->handshake->tls13_kex_modes);
Pengyu Lve6487fe2022-12-06 09:30:29 +08003134#endif
Jerry Yu95648b02023-12-06 15:03:34 +08003135
3136#if defined(MBEDTLS_SSL_EARLY_DATA)
3137 if (ssl->conf->early_data_enabled == MBEDTLS_SSL_EARLY_DATA_ENABLED &&
3138 ssl->conf->max_early_data_size > 0) {
Pengyu Lv94a42cc2023-12-06 10:04:17 +08003139 mbedtls_ssl_tls13_session_set_ticket_flags(
Jerry Yu95648b02023-12-06 15:03:34 +08003140 session, MBEDTLS_SSL_TLS1_3_TICKET_ALLOW_EARLY_DATA);
3141 }
3142#endif /* MBEDTLS_SSL_EARLY_DATA */
3143
Pengyu Lv4938a562023-01-16 11:28:49 +08003144 MBEDTLS_SSL_PRINT_TICKET_FLAGS(4, session->ticket_flags);
Pengyu Lv9f926952022-11-17 15:22:33 +08003145
Jerry Yue67bef42022-07-07 07:29:42 +00003146 /* Generate ticket_age_add */
Gilles Peskine449bd832023-01-11 14:50:10 +01003147 if ((ret = ssl->conf->f_rng(ssl->conf->p_rng,
3148 (unsigned char *) &session->ticket_age_add,
3149 sizeof(session->ticket_age_add)) != 0)) {
3150 MBEDTLS_SSL_DEBUG_RET(1, "generate_ticket_age_add", ret);
3151 return ret;
Jerry Yue67bef42022-07-07 07:29:42 +00003152 }
Gilles Peskine449bd832023-01-11 14:50:10 +01003153 MBEDTLS_SSL_DEBUG_MSG(3, ("ticket_age_add: %u",
3154 (unsigned int) session->ticket_age_add));
Jerry Yue67bef42022-07-07 07:29:42 +00003155
3156 /* Generate ticket_nonce */
Gilles Peskine449bd832023-01-11 14:50:10 +01003157 ret = ssl->conf->f_rng(ssl->conf->p_rng, ticket_nonce, ticket_nonce_size);
3158 if (ret != 0) {
3159 MBEDTLS_SSL_DEBUG_RET(1, "generate_ticket_nonce", ret);
3160 return ret;
Jerry Yue67bef42022-07-07 07:29:42 +00003161 }
Gilles Peskine449bd832023-01-11 14:50:10 +01003162 MBEDTLS_SSL_DEBUG_BUF(3, "ticket_nonce:",
3163 ticket_nonce, ticket_nonce_size);
Jerry Yue67bef42022-07-07 07:29:42 +00003164
3165 ciphersuite_info =
Gilles Peskine449bd832023-01-11 14:50:10 +01003166 (mbedtls_ssl_ciphersuite_t *) ssl->handshake->ciphersuite_info;
Dave Rodgman2eab4622023-10-05 13:30:37 +01003167 psa_hash_alg = mbedtls_md_psa_alg_from_type((mbedtls_md_type_t) ciphersuite_info->mac);
Gilles Peskine449bd832023-01-11 14:50:10 +01003168 hash_length = PSA_HASH_LENGTH(psa_hash_alg);
3169 if (hash_length == -1 ||
3170 (size_t) hash_length > sizeof(session->resumption_key)) {
3171 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Jerry Yufca4d572022-07-21 10:37:48 +08003172 }
3173
Jerry Yue67bef42022-07-07 07:29:42 +00003174 /* In this code the psk key length equals the length of the hash */
3175 session->resumption_key_len = hash_length;
3176 session->ciphersuite = ciphersuite_info->id;
3177
3178 /* Compute resumption key
3179 *
3180 * HKDF-Expand-Label( resumption_master_secret,
3181 * "resumption", ticket_nonce, Hash.length )
3182 */
3183 ret = mbedtls_ssl_tls13_hkdf_expand_label(
Gilles Peskine449bd832023-01-11 14:50:10 +01003184 psa_hash_alg,
3185 session->app_secrets.resumption_master_secret,
3186 hash_length,
3187 MBEDTLS_SSL_TLS1_3_LBL_WITH_LEN(resumption),
3188 ticket_nonce,
3189 ticket_nonce_size,
3190 session->resumption_key,
3191 hash_length);
Jerry Yue67bef42022-07-07 07:29:42 +00003192
Gilles Peskine449bd832023-01-11 14:50:10 +01003193 if (ret != 0) {
3194 MBEDTLS_SSL_DEBUG_RET(2,
3195 "Creating the ticket-resumed PSK failed",
3196 ret);
3197 return ret;
Jerry Yue67bef42022-07-07 07:29:42 +00003198 }
Gilles Peskine449bd832023-01-11 14:50:10 +01003199 MBEDTLS_SSL_DEBUG_BUF(3, "Ticket-resumed PSK",
3200 session->resumption_key,
3201 session->resumption_key_len);
Jerry Yue67bef42022-07-07 07:29:42 +00003202
Gilles Peskine449bd832023-01-11 14:50:10 +01003203 MBEDTLS_SSL_DEBUG_BUF(3, "resumption_master_secret",
3204 session->app_secrets.resumption_master_secret,
3205 hash_length);
Jerry Yue67bef42022-07-07 07:29:42 +00003206
Gilles Peskine449bd832023-01-11 14:50:10 +01003207 return 0;
Jerry Yue67bef42022-07-07 07:29:42 +00003208}
3209
3210/* This function creates a NewSessionTicket message in the following format:
3211 *
3212 * struct {
3213 * uint32 ticket_lifetime;
3214 * uint32 ticket_age_add;
3215 * opaque ticket_nonce<0..255>;
3216 * opaque ticket<1..2^16-1>;
3217 * Extension extensions<0..2^16-2>;
3218 * } NewSessionTicket;
3219 *
3220 * The ticket inside the NewSessionTicket message is an encrypted container
3221 * carrying the necessary information so that the server is later able to
3222 * re-start the communication.
3223 *
3224 * The following fields are placed inside the ticket by the
3225 * f_ticket_write() function:
3226 *
Jerry Yu0069abc2023-11-23 21:07:28 +08003227 * - creation time (ticket_creation_time)
3228 * - flags (ticket_flags)
Jerry Yue67bef42022-07-07 07:29:42 +00003229 * - age add (ticket_age_add)
Jerry Yu0069abc2023-11-23 21:07:28 +08003230 * - key (resumption_key)
3231 * - key length (resumption_key_len)
Jerry Yue67bef42022-07-07 07:29:42 +00003232 * - ciphersuite (ciphersuite)
Jerry Yu0069abc2023-11-23 21:07:28 +08003233 * - max_early_data_size (max_early_data_size)
Jerry Yue67bef42022-07-07 07:29:42 +00003234 */
3235MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01003236static int ssl_tls13_write_new_session_ticket_body(mbedtls_ssl_context *ssl,
3237 unsigned char *buf,
3238 unsigned char *end,
3239 size_t *out_len,
3240 unsigned char *ticket_nonce,
3241 size_t ticket_nonce_size)
Jerry Yue67bef42022-07-07 07:29:42 +00003242{
3243 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
3244 unsigned char *p = buf;
3245 mbedtls_ssl_session *session = ssl->session;
3246 size_t ticket_len;
3247 uint32_t ticket_lifetime;
Jerry Yu01da35e2022-12-12 15:09:22 +08003248 unsigned char *p_extensions_len;
Jerry Yue67bef42022-07-07 07:29:42 +00003249
3250 *out_len = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01003251 MBEDTLS_SSL_DEBUG_MSG(2, ("=> write NewSessionTicket msg"));
Jerry Yue67bef42022-07-07 07:29:42 +00003252
3253 /*
3254 * ticket_lifetime 4 bytes
3255 * ticket_age_add 4 bytes
3256 * ticket_nonce 1 + ticket_nonce_size bytes
3257 * ticket >=2 bytes
3258 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003259 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 4 + 4 + 1 + ticket_nonce_size + 2);
Jerry Yue67bef42022-07-07 07:29:42 +00003260
3261 /* Generate ticket and ticket_lifetime */
Ronald Cron3c0072b2023-11-22 10:00:14 +01003262#if defined(MBEDTLS_HAVE_TIME)
3263 session->ticket_creation_time = mbedtls_ms_time();
3264#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01003265 ret = ssl->conf->f_ticket_write(ssl->conf->p_ticket,
3266 session,
3267 p + 9 + ticket_nonce_size + 2,
3268 end,
3269 &ticket_len,
3270 &ticket_lifetime);
3271 if (ret != 0) {
3272 MBEDTLS_SSL_DEBUG_RET(1, "write_ticket", ret);
3273 return ret;
Jerry Yue67bef42022-07-07 07:29:42 +00003274 }
3275 /* RFC 8446 4.6.1
3276 * ticket_lifetime: Indicates the lifetime in seconds as a 32-bit
3277 * unsigned integer in network byte order from the time of ticket
3278 * issuance. Servers MUST NOT use any value greater than
3279 * 604800 seconds (7 days). The value of zero indicates that the
3280 * ticket should be discarded immediately. Clients MUST NOT cache
3281 * tickets for longer than 7 days, regardless of the ticket_lifetime,
3282 * and MAY delete tickets earlier based on local policy. A server
3283 * MAY treat a ticket as valid for a shorter period of time than what
3284 * is stated in the ticket_lifetime.
3285 */
Jerry Yu8e0174a2023-11-10 13:58:16 +08003286 if (ticket_lifetime > MBEDTLS_SSL_TLS1_3_MAX_ALLOWED_TICKET_LIFETIME) {
3287 ticket_lifetime = MBEDTLS_SSL_TLS1_3_MAX_ALLOWED_TICKET_LIFETIME;
Gilles Peskine449bd832023-01-11 14:50:10 +01003288 }
3289 MBEDTLS_PUT_UINT32_BE(ticket_lifetime, p, 0);
3290 MBEDTLS_SSL_DEBUG_MSG(3, ("ticket_lifetime: %u",
3291 (unsigned int) ticket_lifetime));
Jerry Yue67bef42022-07-07 07:29:42 +00003292
3293 /* Write ticket_age_add */
Gilles Peskine449bd832023-01-11 14:50:10 +01003294 MBEDTLS_PUT_UINT32_BE(session->ticket_age_add, p, 4);
3295 MBEDTLS_SSL_DEBUG_MSG(3, ("ticket_age_add: %u",
3296 (unsigned int) session->ticket_age_add));
Jerry Yue67bef42022-07-07 07:29:42 +00003297
3298 /* Write ticket_nonce */
Gilles Peskine449bd832023-01-11 14:50:10 +01003299 p[8] = (unsigned char) ticket_nonce_size;
3300 if (ticket_nonce_size > 0) {
3301 memcpy(p + 9, ticket_nonce, ticket_nonce_size);
Jerry Yue67bef42022-07-07 07:29:42 +00003302 }
3303 p += 9 + ticket_nonce_size;
3304
3305 /* Write ticket */
Gilles Peskine449bd832023-01-11 14:50:10 +01003306 MBEDTLS_PUT_UINT16_BE(ticket_len, p, 0);
Jerry Yue67bef42022-07-07 07:29:42 +00003307 p += 2;
Gilles Peskine449bd832023-01-11 14:50:10 +01003308 MBEDTLS_SSL_DEBUG_BUF(4, "ticket", p, ticket_len);
Jerry Yue67bef42022-07-07 07:29:42 +00003309 p += ticket_len;
3310
3311 /* Ticket Extensions
3312 *
Jerry Yu01da35e2022-12-12 15:09:22 +08003313 * Extension extensions<0..2^16-2>;
Jerry Yue67bef42022-07-07 07:29:42 +00003314 */
Jerry Yuedab6372022-10-31 14:37:31 +08003315 ssl->handshake->sent_extensions = MBEDTLS_SSL_EXT_MASK_NONE;
3316
Gilles Peskine449bd832023-01-11 14:50:10 +01003317 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 2);
Jerry Yu01da35e2022-12-12 15:09:22 +08003318 p_extensions_len = p;
Jerry Yue67bef42022-07-07 07:29:42 +00003319 p += 2;
3320
Jerry Yu01da35e2022-12-12 15:09:22 +08003321#if defined(MBEDTLS_SSL_EARLY_DATA)
Pengyu Lv94a42cc2023-12-06 10:04:17 +08003322 if (mbedtls_ssl_tls13_session_ticket_allow_early_data(session)) {
Jerry Yu95648b02023-12-06 15:03:34 +08003323 size_t output_len;
3324
Jerry Yuf135bac2023-11-23 18:10:51 +08003325 if ((ret = mbedtls_ssl_tls13_write_early_data_ext(
Jerry Yuc59c5862023-12-05 10:40:49 +08003326 ssl, 1, p, end, &output_len)) != 0) {
Jerry Yuf135bac2023-11-23 18:10:51 +08003327 MBEDTLS_SSL_DEBUG_RET(
3328 1, "mbedtls_ssl_tls13_write_early_data_ext", ret);
3329 return ret;
3330 }
3331 p += output_len;
Jerry Yu9e7f9bc2023-11-27 16:52:07 +08003332 } else {
3333 MBEDTLS_SSL_DEBUG_MSG(
3334 4, ("early_data not allowed, "
3335 "skip early_data extension in NewSessionTicket"));
Jerry Yu01da35e2022-12-12 15:09:22 +08003336 }
Jerry Yuf135bac2023-11-23 18:10:51 +08003337
Jerry Yu01da35e2022-12-12 15:09:22 +08003338#endif /* MBEDTLS_SSL_EARLY_DATA */
3339
3340 MBEDTLS_PUT_UINT16_BE(p - p_extensions_len - 2, p_extensions_len, 0);
3341
Jerry Yue67bef42022-07-07 07:29:42 +00003342 *out_len = p - buf;
Gilles Peskine449bd832023-01-11 14:50:10 +01003343 MBEDTLS_SSL_DEBUG_BUF(4, "ticket", buf, *out_len);
3344 MBEDTLS_SSL_DEBUG_MSG(2, ("<= write new session ticket"));
Jerry Yue67bef42022-07-07 07:29:42 +00003345
Jerry Yu7de2ff02022-11-08 21:43:46 +08003346 MBEDTLS_SSL_PRINT_EXTS(
Gilles Peskine449bd832023-01-11 14:50:10 +01003347 3, MBEDTLS_SSL_HS_NEW_SESSION_TICKET, ssl->handshake->sent_extensions);
Jerry Yu4b8f2f72022-10-31 13:31:22 +08003348
Gilles Peskine449bd832023-01-11 14:50:10 +01003349 return 0;
Jerry Yue67bef42022-07-07 07:29:42 +00003350}
3351
3352/*
Jerry Yua8d3c502022-10-30 14:51:23 +08003353 * Handler for MBEDTLS_SSL_TLS1_3_NEW_SESSION_TICKET
Jerry Yue67bef42022-07-07 07:29:42 +00003354 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003355static int ssl_tls13_write_new_session_ticket(mbedtls_ssl_context *ssl)
Jerry Yue67bef42022-07-07 07:29:42 +00003356{
3357 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
3358
Gilles Peskine449bd832023-01-11 14:50:10 +01003359 MBEDTLS_SSL_PROC_CHK_NEG(ssl_tls13_write_new_session_ticket_coordinate(ssl));
Jerry Yue67bef42022-07-07 07:29:42 +00003360
Gilles Peskine449bd832023-01-11 14:50:10 +01003361 if (ret == SSL_NEW_SESSION_TICKET_WRITE) {
Jerry Yue67bef42022-07-07 07:29:42 +00003362 unsigned char ticket_nonce[MBEDTLS_SSL_TLS1_3_TICKET_NONCE_LENGTH];
3363 unsigned char *buf;
3364 size_t buf_len, msg_len;
3365
Gilles Peskine449bd832023-01-11 14:50:10 +01003366 MBEDTLS_SSL_PROC_CHK(ssl_tls13_prepare_new_session_ticket(
3367 ssl, ticket_nonce, sizeof(ticket_nonce)));
Jerry Yue67bef42022-07-07 07:29:42 +00003368
Xiaokang Qian9f1747b2023-03-30 02:50:04 +00003369 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_start_handshake_msg(
3370 ssl, MBEDTLS_SSL_HS_NEW_SESSION_TICKET,
3371 &buf, &buf_len));
Jerry Yue67bef42022-07-07 07:29:42 +00003372
Gilles Peskine449bd832023-01-11 14:50:10 +01003373 MBEDTLS_SSL_PROC_CHK(ssl_tls13_write_new_session_ticket_body(
3374 ssl, buf, buf + buf_len, &msg_len,
3375 ticket_nonce, sizeof(ticket_nonce)));
Jerry Yue67bef42022-07-07 07:29:42 +00003376
Gilles Peskine449bd832023-01-11 14:50:10 +01003377 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_finish_handshake_msg(
3378 ssl, buf_len, msg_len));
Jerry Yufca4d572022-07-21 10:37:48 +08003379
Jerry Yu359e65f2022-09-22 23:47:43 +08003380 /* Limit session tickets count to one when resumption connection.
3381 *
3382 * See document of mbedtls_ssl_conf_new_session_tickets.
3383 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003384 if (ssl->handshake->resume == 1) {
Jerry Yu359e65f2022-09-22 23:47:43 +08003385 ssl->handshake->new_session_tickets_count = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01003386 } else {
Jerry Yu359e65f2022-09-22 23:47:43 +08003387 ssl->handshake->new_session_tickets_count--;
Gilles Peskine449bd832023-01-11 14:50:10 +01003388 }
Jerry Yub7e3fa72022-09-22 11:07:18 +08003389
Jerry Yua8d3c502022-10-30 14:51:23 +08003390 mbedtls_ssl_handshake_set_state(
Gilles Peskine449bd832023-01-11 14:50:10 +01003391 ssl, MBEDTLS_SSL_TLS1_3_NEW_SESSION_TICKET_FLUSH);
3392 } else {
3393 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_HANDSHAKE_OVER);
Jerry Yue67bef42022-07-07 07:29:42 +00003394 }
3395
Jerry Yue67bef42022-07-07 07:29:42 +00003396cleanup:
3397
Gilles Peskine449bd832023-01-11 14:50:10 +01003398 return ret;
Jerry Yue67bef42022-07-07 07:29:42 +00003399}
3400#endif /* MBEDTLS_SSL_SESSION_TICKETS */
3401
3402/*
XiaokangQiane8ff3502022-04-22 02:34:40 +00003403 * TLS 1.3 State Machine -- server side
XiaokangQian7807f9f2022-02-15 10:04:37 +00003404 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003405int mbedtls_ssl_tls13_handshake_server_step(mbedtls_ssl_context *ssl)
Jerry Yub9930e72021-08-06 17:11:51 +08003406{
XiaokangQian08037552022-04-20 07:16:41 +00003407 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
XiaokangQian7807f9f2022-02-15 10:04:37 +00003408
Gilles Peskine449bd832023-01-11 14:50:10 +01003409 if (ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER || ssl->handshake == NULL) {
3410 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
3411 }
XiaokangQian7807f9f2022-02-15 10:04:37 +00003412
Gilles Peskine449bd832023-01-11 14:50:10 +01003413 MBEDTLS_SSL_DEBUG_MSG(2, ("tls13 server state: %s(%d)",
Dave Rodgman2eab4622023-10-05 13:30:37 +01003414 mbedtls_ssl_states_str((mbedtls_ssl_states) ssl->state),
Gilles Peskine449bd832023-01-11 14:50:10 +01003415 ssl->state));
Jerry Yu6e81b272021-09-27 11:16:17 +08003416
Gilles Peskine449bd832023-01-11 14:50:10 +01003417 switch (ssl->state) {
XiaokangQian7807f9f2022-02-15 10:04:37 +00003418 /* start state */
3419 case MBEDTLS_SSL_HELLO_REQUEST:
Gilles Peskine449bd832023-01-11 14:50:10 +01003420 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_CLIENT_HELLO);
XiaokangQian08037552022-04-20 07:16:41 +00003421 ret = 0;
XiaokangQian7807f9f2022-02-15 10:04:37 +00003422 break;
3423
XiaokangQian7807f9f2022-02-15 10:04:37 +00003424 case MBEDTLS_SSL_CLIENT_HELLO:
Gilles Peskine449bd832023-01-11 14:50:10 +01003425 ret = ssl_tls13_process_client_hello(ssl);
3426 if (ret != 0) {
3427 MBEDTLS_SSL_DEBUG_RET(1, "ssl_tls13_process_client_hello", ret);
3428 }
Jerry Yuf41553b2022-05-09 22:20:30 +08003429 break;
XiaokangQian7807f9f2022-02-15 10:04:37 +00003430
Jerry Yuf41553b2022-05-09 22:20:30 +08003431 case MBEDTLS_SSL_HELLO_RETRY_REQUEST:
Gilles Peskine449bd832023-01-11 14:50:10 +01003432 ret = ssl_tls13_write_hello_retry_request(ssl);
3433 if (ret != 0) {
3434 MBEDTLS_SSL_DEBUG_RET(1, "ssl_tls13_write_hello_retry_request", ret);
3435 return ret;
Jerry Yuf41553b2022-05-09 22:20:30 +08003436 }
XiaokangQian7807f9f2022-02-15 10:04:37 +00003437 break;
3438
Jerry Yu5b64ae92022-03-30 17:15:02 +08003439 case MBEDTLS_SSL_SERVER_HELLO:
Gilles Peskine449bd832023-01-11 14:50:10 +01003440 ret = ssl_tls13_write_server_hello(ssl);
Jerry Yu5b64ae92022-03-30 17:15:02 +08003441 break;
3442
Xiaofei Baicba64af2022-02-15 10:00:56 +00003443 case MBEDTLS_SSL_ENCRYPTED_EXTENSIONS:
Gilles Peskine449bd832023-01-11 14:50:10 +01003444 ret = ssl_tls13_write_encrypted_extensions(ssl);
3445 if (ret != 0) {
3446 MBEDTLS_SSL_DEBUG_RET(1, "ssl_tls13_write_encrypted_extensions", ret);
3447 return ret;
Xiaofei Baicba64af2022-02-15 10:00:56 +00003448 }
Jerry Yu48330562022-05-06 21:35:44 +08003449 break;
Jerry Yu6a2cd9e2022-05-05 11:14:19 +08003450
Ronald Cron928cbd32022-10-04 16:14:26 +02003451#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED)
Xiaofei Bai9ca09d42022-02-14 12:57:18 +00003452 case MBEDTLS_SSL_CERTIFICATE_REQUEST:
Gilles Peskine449bd832023-01-11 14:50:10 +01003453 ret = ssl_tls13_write_certificate_request(ssl);
Xiaofei Bai9ca09d42022-02-14 12:57:18 +00003454 break;
Xiaofei Bai9ca09d42022-02-14 12:57:18 +00003455
Jerry Yu83da34e2022-04-16 13:59:52 +08003456 case MBEDTLS_SSL_SERVER_CERTIFICATE:
Gilles Peskine449bd832023-01-11 14:50:10 +01003457 ret = ssl_tls13_write_server_certificate(ssl);
Jerry Yu83da34e2022-04-16 13:59:52 +08003458 break;
3459
3460 case MBEDTLS_SSL_CERTIFICATE_VERIFY:
Gilles Peskine449bd832023-01-11 14:50:10 +01003461 ret = ssl_tls13_write_certificate_verify(ssl);
Jerry Yu83da34e2022-04-16 13:59:52 +08003462 break;
Ronald Cron928cbd32022-10-04 16:14:26 +02003463#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED */
Jerry Yu83da34e2022-04-16 13:59:52 +08003464
Gilles Peskine449bd832023-01-11 14:50:10 +01003465 /*
3466 * Injection of dummy-CCS's for middlebox compatibility
3467 */
Gabor Mezei7b39bf12022-05-24 16:04:14 +02003468#if defined(MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE)
Gabor Mezeif7044ea2022-06-28 16:01:49 +02003469 case MBEDTLS_SSL_SERVER_CCS_AFTER_HELLO_RETRY_REQUEST:
Gilles Peskine449bd832023-01-11 14:50:10 +01003470 ret = mbedtls_ssl_tls13_write_change_cipher_spec(ssl);
3471 if (ret == 0) {
3472 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_CLIENT_HELLO);
3473 }
Gabor Mezei7b39bf12022-05-24 16:04:14 +02003474 break;
3475
3476 case MBEDTLS_SSL_SERVER_CCS_AFTER_SERVER_HELLO:
Ronald Crone273f722024-02-13 18:22:26 +01003477 ret = mbedtls_ssl_tls13_write_change_cipher_spec(ssl);
3478 if (ret != 0) {
3479 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01003480 }
Ronald Cronfe59ff72024-01-24 14:31:50 +01003481 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_ENCRYPTED_EXTENSIONS);
Gabor Mezei7b39bf12022-05-24 16:04:14 +02003482 break;
3483#endif /* MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE */
3484
Jerry Yu69dd8d42022-04-16 12:51:26 +08003485 case MBEDTLS_SSL_SERVER_FINISHED:
Gilles Peskine449bd832023-01-11 14:50:10 +01003486 ret = ssl_tls13_write_server_finished(ssl);
Jerry Yu69dd8d42022-04-16 12:51:26 +08003487 break;
3488
Jerry Yu87b5ed42022-12-12 13:07:07 +08003489#if defined(MBEDTLS_SSL_EARLY_DATA)
3490 case MBEDTLS_SSL_END_OF_EARLY_DATA:
Jerry Yu59d420f2023-12-01 16:30:34 +08003491 ret = ssl_tls13_process_end_of_early_data(ssl);
Jerry Yu87b5ed42022-12-12 13:07:07 +08003492 break;
3493#endif /* MBEDTLS_SSL_EARLY_DATA */
3494
Jerry Yu69dd8d42022-04-16 12:51:26 +08003495 case MBEDTLS_SSL_CLIENT_FINISHED:
Gilles Peskine449bd832023-01-11 14:50:10 +01003496 ret = ssl_tls13_process_client_finished(ssl);
Jerry Yu69dd8d42022-04-16 12:51:26 +08003497 break;
3498
Jerry Yu69dd8d42022-04-16 12:51:26 +08003499 case MBEDTLS_SSL_HANDSHAKE_WRAPUP:
Gilles Peskine449bd832023-01-11 14:50:10 +01003500 ret = ssl_tls13_handshake_wrapup(ssl);
Jerry Yu69dd8d42022-04-16 12:51:26 +08003501 break;
3502
Ronald Cron766c0cd2022-10-18 12:17:11 +02003503#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED)
XiaokangQian6b916b12022-04-25 07:29:34 +00003504 case MBEDTLS_SSL_CLIENT_CERTIFICATE:
Gilles Peskine449bd832023-01-11 14:50:10 +01003505 ret = mbedtls_ssl_tls13_process_certificate(ssl);
3506 if (ret == 0) {
3507 if (ssl->session_negotiate->peer_cert != NULL) {
Ronald Cron209cae92022-06-07 10:30:19 +02003508 mbedtls_ssl_handshake_set_state(
Gilles Peskine449bd832023-01-11 14:50:10 +01003509 ssl, MBEDTLS_SSL_CLIENT_CERTIFICATE_VERIFY);
3510 } else {
3511 MBEDTLS_SSL_DEBUG_MSG(2, ("skip parse certificate verify"));
Ronald Cron209cae92022-06-07 10:30:19 +02003512 mbedtls_ssl_handshake_set_state(
Gilles Peskine449bd832023-01-11 14:50:10 +01003513 ssl, MBEDTLS_SSL_CLIENT_FINISHED);
Ronald Cron19385882022-06-15 16:26:13 +02003514 }
XiaokangQian6b916b12022-04-25 07:29:34 +00003515 }
3516 break;
3517
3518 case MBEDTLS_SSL_CLIENT_CERTIFICATE_VERIFY:
Gilles Peskine449bd832023-01-11 14:50:10 +01003519 ret = mbedtls_ssl_tls13_process_certificate_verify(ssl);
3520 if (ret == 0) {
XiaokangQian6b916b12022-04-25 07:29:34 +00003521 mbedtls_ssl_handshake_set_state(
Gilles Peskine449bd832023-01-11 14:50:10 +01003522 ssl, MBEDTLS_SSL_CLIENT_FINISHED);
XiaokangQian6b916b12022-04-25 07:29:34 +00003523 }
3524 break;
Ronald Cron766c0cd2022-10-18 12:17:11 +02003525#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED */
XiaokangQian6b916b12022-04-25 07:29:34 +00003526
Jerry Yue67bef42022-07-07 07:29:42 +00003527#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Jerry Yua8d3c502022-10-30 14:51:23 +08003528 case MBEDTLS_SSL_TLS1_3_NEW_SESSION_TICKET:
Gilles Peskine449bd832023-01-11 14:50:10 +01003529 ret = ssl_tls13_write_new_session_ticket(ssl);
3530 if (ret != 0) {
3531 MBEDTLS_SSL_DEBUG_RET(1,
3532 "ssl_tls13_write_new_session_ticket ",
3533 ret);
Jerry Yue67bef42022-07-07 07:29:42 +00003534 }
3535 break;
Jerry Yua8d3c502022-10-30 14:51:23 +08003536 case MBEDTLS_SSL_TLS1_3_NEW_SESSION_TICKET_FLUSH:
Jerry Yue67bef42022-07-07 07:29:42 +00003537 /* This state is necessary to do the flush of the New Session
Jerry Yua8d3c502022-10-30 14:51:23 +08003538 * Ticket message written in MBEDTLS_SSL_TLS1_3_NEW_SESSION_TICKET
Jerry Yue67bef42022-07-07 07:29:42 +00003539 * as part of ssl_prepare_handshake_step.
3540 */
3541 ret = 0;
Jerry Yud4e75002022-08-09 13:33:50 +08003542
Gilles Peskine449bd832023-01-11 14:50:10 +01003543 if (ssl->handshake->new_session_tickets_count == 0) {
3544 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_HANDSHAKE_OVER);
3545 } else {
Xiaokang Qian9f1747b2023-03-30 02:50:04 +00003546 mbedtls_ssl_handshake_set_state(
3547 ssl, MBEDTLS_SSL_TLS1_3_NEW_SESSION_TICKET);
Gilles Peskine449bd832023-01-11 14:50:10 +01003548 }
Jerry Yue67bef42022-07-07 07:29:42 +00003549 break;
3550
3551#endif /* MBEDTLS_SSL_SESSION_TICKETS */
3552
XiaokangQian7807f9f2022-02-15 10:04:37 +00003553 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01003554 MBEDTLS_SSL_DEBUG_MSG(1, ("invalid state %d", ssl->state));
3555 return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
XiaokangQian7807f9f2022-02-15 10:04:37 +00003556 }
3557
Gilles Peskine449bd832023-01-11 14:50:10 +01003558 return ret;
Jerry Yub9930e72021-08-06 17:11:51 +08003559}
Jerry Yu3cc4c2a2021-08-06 16:29:08 +08003560
Jerry Yufb4b6472022-01-27 15:03:26 +08003561#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_PROTO_TLS1_3 */