blob: 693edc7b0bccc1ec95f10bc46359f9c1f9119987 [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 /*
Ronald Cron7cab4f82024-03-07 15:09:09 +010077 * If a valid PSK ciphersuite identifier has been passed in, we want
78 * an exact match.
Ronald Cron89089cc2024-02-14 18:18:08 +010079 */
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
Gilles Peskineeeb4ff52024-06-03 22:16:23 +020095 MBEDTLS_SSL_DEBUG_MSG(2, ("No matched ciphersuite, psk_ciphersuite_id=%x, psk_hash_alg=%lx",
96 (unsigned) psk_ciphersuite_id,
97 (unsigned long) psk_hash_alg));
Ronald Cron89089cc2024-02-14 18:18:08 +010098}
99
Ronald Cron41a443a2022-10-04 16:38:25 +0200100#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED)
Jerry Yue19e3b92022-07-08 12:04:51 +0000101/* From RFC 8446:
102 *
103 * enum { psk_ke(0), psk_dhe_ke(1), (255) } PskKeyExchangeMode;
104 * struct {
105 * PskKeyExchangeMode ke_modes<1..255>;
106 * } PskKeyExchangeModes;
107 */
Jerry Yu6e74a7e2022-07-20 20:49:32 +0800108MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100109static int ssl_tls13_parse_key_exchange_modes_ext(mbedtls_ssl_context *ssl,
110 const unsigned char *buf,
111 const unsigned char *end)
Jerry Yue19e3b92022-07-08 12:04:51 +0000112{
Jerry Yu299e31f2022-07-13 23:06:36 +0800113 const unsigned char *p = buf;
Jerry Yue19e3b92022-07-08 12:04:51 +0000114 size_t ke_modes_len;
115 int ke_modes = 0;
116
Jerry Yu854dd9e2022-07-15 14:28:27 +0800117 /* Read ke_modes length (1 Byte) */
Gilles Peskine449bd832023-01-11 14:50:10 +0100118 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, 1);
Jerry Yu299e31f2022-07-13 23:06:36 +0800119 ke_modes_len = *p++;
Jerry Yue19e3b92022-07-08 12:04:51 +0000120 /* Currently, there are only two PSK modes, so even without looking
121 * at the content, something's wrong if the list has more than 2 items. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100122 if (ke_modes_len > 2) {
123 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER,
124 MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER);
125 return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
Jerry Yu299e31f2022-07-13 23:06:36 +0800126 }
Jerry Yue19e3b92022-07-08 12:04:51 +0000127
Gilles Peskine449bd832023-01-11 14:50:10 +0100128 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, ke_modes_len);
Jerry Yue19e3b92022-07-08 12:04:51 +0000129
Gilles Peskine449bd832023-01-11 14:50:10 +0100130 while (ke_modes_len-- != 0) {
131 switch (*p++) {
132 case MBEDTLS_SSL_TLS1_3_PSK_MODE_PURE:
133 ke_modes |= MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK;
134 MBEDTLS_SSL_DEBUG_MSG(3, ("Found PSK KEX MODE"));
135 break;
136 case MBEDTLS_SSL_TLS1_3_PSK_MODE_ECDHE:
137 ke_modes |= MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK_EPHEMERAL;
138 MBEDTLS_SSL_DEBUG_MSG(3, ("Found PSK_EPHEMERAL KEX MODE"));
139 break;
140 default:
141 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER,
142 MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER);
143 return MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER;
Jerry Yue19e3b92022-07-08 12:04:51 +0000144 }
145 }
146
147 ssl->handshake->tls13_kex_modes = ke_modes;
Gilles Peskine449bd832023-01-11 14:50:10 +0100148 return 0;
Jerry Yue19e3b92022-07-08 12:04:51 +0000149}
Jerry Yu1c105562022-07-10 06:32:38 +0000150
Ronald Cron38117652024-03-05 09:05:46 +0100151/*
152 * Non-error return values of
153 * ssl_tls13_offered_psks_check_identity_match_ticket() and
154 * ssl_tls13_offered_psks_check_identity_match(). They are positive to
155 * not collide with error codes that are negative. Zero
156 * (SSL_TLS1_3_PSK_IDENTITY_MATCH) in case of success as it may be propagated
157 * up by the callers of this function as a generic success condition.
158 *
159 * The return value SSL_TLS1_3_PSK_IDENTITY_MATCH_BUT_PSK_NOT_USABLE means
Ronald Cron7cab4f82024-03-07 15:09:09 +0100160 * that the pre-shared-key identity matches that of a ticket or an externally-
Ronald Cron38117652024-03-05 09:05:46 +0100161 * provisioned pre-shared-key. We have thus been able to retrieve the
162 * attributes of the pre-shared-key but at least one of them does not meet
163 * some criteria and the pre-shared-key cannot be used. For example, a ticket
164 * is expired or its version is not TLS 1.3. Note eventually that the return
165 * value SSL_TLS1_3_PSK_IDENTITY_MATCH_BUT_PSK_NOT_USABLE does not have
166 * anything to do with binder check. A binder check is done only when a
167 * suitable pre-shared-key has been selected and only for that selected
168 * pre-shared-key: if the binder check fails, we fail the handshake and we do
169 * not try to find another pre-shared-key for which the binder check would
170 * succeed as recommended by the specification.
171 */
Ronald Cronfbae94a2023-12-05 18:15:14 +0100172#define SSL_TLS1_3_PSK_IDENTITY_DOES_NOT_MATCH 2
173#define SSL_TLS1_3_PSK_IDENTITY_MATCH_BUT_PSK_NOT_USABLE 1
174#define SSL_TLS1_3_PSK_IDENTITY_MATCH 0
Jerry Yu95699e72022-08-21 19:22:23 +0800175
Pengyu Lv29daf4a2023-10-30 17:13:30 +0800176MBEDTLS_CHECK_RETURN_CRITICAL
Pengyu Lvbc4aab72023-12-01 15:37:24 +0800177static int ssl_tls13_key_exchange_is_psk_available(mbedtls_ssl_context *ssl);
Pengyu Lv29daf4a2023-10-30 17:13:30 +0800178MBEDTLS_CHECK_RETURN_CRITICAL
Pengyu Lvbc4aab72023-12-01 15:37:24 +0800179static int ssl_tls13_key_exchange_is_psk_ephemeral_available(mbedtls_ssl_context *ssl);
Jerry Yu95699e72022-08-21 19:22:23 +0800180
Ronald Cron5e297b92024-03-25 13:37:07 +0100181#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Jerry Yu95699e72022-08-21 19:22:23 +0800182MBEDTLS_CHECK_RETURN_CRITICAL
183static int ssl_tls13_offered_psks_check_identity_match_ticket(
Gilles Peskine449bd832023-01-11 14:50:10 +0100184 mbedtls_ssl_context *ssl,
185 const unsigned char *identity,
186 size_t identity_len,
187 uint32_t obfuscated_ticket_age,
188 mbedtls_ssl_session *session)
Jerry Yu95699e72022-08-21 19:22:23 +0800189{
Jerry Yufd310eb2022-09-06 09:16:35 +0800190 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yu95699e72022-08-21 19:22:23 +0800191 unsigned char *ticket_buffer;
Jerry Yu8d4bbba2022-09-13 14:15:48 +0800192#if defined(MBEDTLS_HAVE_TIME)
Jerry Yucebffc32022-12-15 18:00:05 +0800193 mbedtls_ms_time_t now;
194 mbedtls_ms_time_t server_age;
Jerry Yu713ce1f2023-11-17 15:32:12 +0800195 uint32_t client_age;
Jerry Yucebffc32022-12-15 18:00:05 +0800196 mbedtls_ms_time_t age_diff;
Jerry Yu8d4bbba2022-09-13 14:15:48 +0800197#endif
Jerry Yu95699e72022-08-21 19:22:23 +0800198
199 ((void) obfuscated_ticket_age);
200
Gilles Peskine449bd832023-01-11 14:50:10 +0100201 MBEDTLS_SSL_DEBUG_MSG(2, ("=> check_identity_match_ticket"));
Jerry Yu95699e72022-08-21 19:22:23 +0800202
Jerry Yufd310eb2022-09-06 09:16:35 +0800203 /* Ticket parser is not configured, Skip */
Gilles Peskine449bd832023-01-11 14:50:10 +0100204 if (ssl->conf->f_ticket_parse == NULL || identity_len == 0) {
Ronald Cronfbae94a2023-12-05 18:15:14 +0100205 return SSL_TLS1_3_PSK_IDENTITY_DOES_NOT_MATCH;
Gilles Peskine449bd832023-01-11 14:50:10 +0100206 }
Jerry Yu95699e72022-08-21 19:22:23 +0800207
Jerry Yu4746b102022-09-13 11:11:48 +0800208 /* We create a copy of the encrypted ticket since the ticket parsing
209 * function is allowed to use its input buffer as an output buffer
210 * (in-place decryption). We do, however, need the original buffer for
211 * computing the PSK binder value.
Jerry Yu95699e72022-08-21 19:22:23 +0800212 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100213 ticket_buffer = mbedtls_calloc(1, identity_len);
214 if (ticket_buffer == NULL) {
Gilles Peskine449bd832023-01-11 14:50:10 +0100215 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
Jerry Yu95699e72022-08-21 19:22:23 +0800216 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100217 memcpy(ticket_buffer, identity, identity_len);
Jerry Yu95699e72022-08-21 19:22:23 +0800218
Ronald Cronfbae94a2023-12-05 18:15:14 +0100219 ret = ssl->conf->f_ticket_parse(ssl->conf->p_ticket,
220 session,
221 ticket_buffer, identity_len);
Ronald Cronf602f7b2024-03-05 09:11:55 +0100222 switch (ret) {
223 case 0:
224 ret = SSL_TLS1_3_PSK_IDENTITY_MATCH;
225 break;
226
227 case MBEDTLS_ERR_SSL_SESSION_TICKET_EXPIRED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100228 MBEDTLS_SSL_DEBUG_MSG(3, ("ticket is expired"));
Ronald Cronfbae94a2023-12-05 18:15:14 +0100229 ret = SSL_TLS1_3_PSK_IDENTITY_MATCH_BUT_PSK_NOT_USABLE;
Ronald Cronf602f7b2024-03-05 09:11:55 +0100230 break;
231
232 case MBEDTLS_ERR_SSL_INVALID_MAC:
233 MBEDTLS_SSL_DEBUG_MSG(3, ("ticket is not authentic"));
Ronald Cronfbae94a2023-12-05 18:15:14 +0100234 ret = SSL_TLS1_3_PSK_IDENTITY_DOES_NOT_MATCH;
Ronald Cronf602f7b2024-03-05 09:11:55 +0100235 break;
236
237 default:
Gilles Peskine449bd832023-01-11 14:50:10 +0100238 MBEDTLS_SSL_DEBUG_RET(1, "ticket_parse", ret);
Ronald Cronf602f7b2024-03-05 09:11:55 +0100239 ret = SSL_TLS1_3_PSK_IDENTITY_DOES_NOT_MATCH;
Jerry Yu95699e72022-08-21 19:22:23 +0800240 }
241
242 /* We delete the temporary buffer */
Gilles Peskine449bd832023-01-11 14:50:10 +0100243 mbedtls_free(ticket_buffer);
Jerry Yu95699e72022-08-21 19:22:23 +0800244
Ronald Cronfbae94a2023-12-05 18:15:14 +0100245 if (ret != SSL_TLS1_3_PSK_IDENTITY_MATCH) {
Jerry Yu8d4bbba2022-09-13 14:15:48 +0800246 goto exit;
247 }
Jerry Yu95699e72022-08-21 19:22:23 +0800248
Ronald Cron38117652024-03-05 09:05:46 +0100249 /*
250 * The identity matches that of a ticket. Now check that it has suitable
251 * attributes and bet it will not be the case.
Pengyu Lv3eb49be2022-12-05 16:35:12 +0800252 */
Ronald Cronfbae94a2023-12-05 18:15:14 +0100253 ret = SSL_TLS1_3_PSK_IDENTITY_MATCH_BUT_PSK_NOT_USABLE;
Pengyu Lv29daf4a2023-10-30 17:13:30 +0800254
Ronald Cronfbae94a2023-12-05 18:15:14 +0100255 if (session->tls_version != MBEDTLS_SSL_VERSION_TLS1_3) {
256 MBEDTLS_SSL_DEBUG_MSG(3, ("Ticket TLS version is not 1.3."));
Pengyu Lv3eb49be2022-12-05 16:35:12 +0800257 goto exit;
258 }
259
Gilles Peskine449bd832023-01-11 14:50:10 +0100260#if defined(MBEDTLS_HAVE_TIME)
Jerry Yucebffc32022-12-15 18:00:05 +0800261 now = mbedtls_ms_time();
Gilles Peskine449bd832023-01-11 14:50:10 +0100262
Jerry Yu25ba4d42023-11-10 14:12:20 +0800263 if (now < session->ticket_creation_time) {
Gilles Peskine449bd832023-01-11 14:50:10 +0100264 MBEDTLS_SSL_DEBUG_MSG(
Jerry Yu713ce1f2023-11-17 15:32:12 +0800265 3, ("Invalid ticket creation time ( now = %" MBEDTLS_PRINTF_MS_TIME
266 ", creation_time = %" MBEDTLS_PRINTF_MS_TIME " )",
Jerry Yu25ba4d42023-11-10 14:12:20 +0800267 now, session->ticket_creation_time));
Gilles Peskine449bd832023-01-11 14:50:10 +0100268 goto exit;
269 }
270
Jerry Yu25ba4d42023-11-10 14:12:20 +0800271 server_age = now - session->ticket_creation_time;
Jerry Yu95699e72022-08-21 19:22:23 +0800272
Jerry Yu8d4bbba2022-09-13 14:15:48 +0800273 /* RFC 8446 section 4.6.1
274 *
275 * Servers MUST NOT use any value greater than 604800 seconds (7 days).
276 *
277 * RFC 8446 section 4.2.11.1
278 *
279 * Clients MUST NOT attempt to use tickets which have ages greater than
280 * the "ticket_lifetime" value which was provided with the ticket.
281 *
Jerry Yu8d4bbba2022-09-13 14:15:48 +0800282 */
Jerry Yu8e0174a2023-11-10 13:58:16 +0800283 if (server_age > MBEDTLS_SSL_TLS1_3_MAX_ALLOWED_TICKET_LIFETIME * 1000) {
Jerry Yu8d4bbba2022-09-13 14:15:48 +0800284 MBEDTLS_SSL_DEBUG_MSG(
Jerry Yud84c14f2023-11-16 13:33:57 +0800285 3, ("Ticket age exceeds limitation ticket_age = %" MBEDTLS_PRINTF_MS_TIME,
Jerry Yucebffc32022-12-15 18:00:05 +0800286 server_age));
Jerry Yu8d4bbba2022-09-13 14:15:48 +0800287 goto exit;
288 }
Jerry Yu95699e72022-08-21 19:22:23 +0800289
Jerry Yu8d4bbba2022-09-13 14:15:48 +0800290 /* RFC 8446 section 4.2.10
291 *
292 * For PSKs provisioned via NewSessionTicket, a server MUST validate that
293 * the ticket age for the selected PSK identity (computed by subtracting
294 * ticket_age_add from PskIdentity.obfuscated_ticket_age modulo 2^32) is
295 * within a small tolerance of the time since the ticket was issued.
Jerry Yuf7dad3c2022-09-14 22:31:39 +0800296 *
Jerry Yu31b601a2023-11-10 11:27:21 +0800297 * NOTE: The typical accuracy of an RTC crystal is ±100 to ±20 parts per
298 * million (360 to 72 milliseconds per hour). Default tolerance
Jerry Yu9cb953a2023-11-15 09:54:11 +0800299 * window is 6s, thus in the worst case clients and servers must
Jerry Yu31b601a2023-11-10 11:27:21 +0800300 * sync up their system time every 6000/360/2~=8 hours.
Jerry Yu8d4bbba2022-09-13 14:15:48 +0800301 */
Jerry Yucebffc32022-12-15 18:00:05 +0800302 client_age = obfuscated_ticket_age - session->ticket_age_add;
Jerry Yu60e99722023-11-20 09:55:24 +0800303 age_diff = server_age - (mbedtls_ms_time_t) client_age;
Jerry Yu28e7c552023-11-10 12:05:56 +0800304 if (age_diff < -MBEDTLS_SSL_TLS1_3_TICKET_AGE_TOLERANCE ||
Jerry Yucebffc32022-12-15 18:00:05 +0800305 age_diff > MBEDTLS_SSL_TLS1_3_TICKET_AGE_TOLERANCE) {
Jerry Yu8d4bbba2022-09-13 14:15:48 +0800306 MBEDTLS_SSL_DEBUG_MSG(
Jerry Yuf16efbc2023-10-30 11:06:24 +0800307 3, ("Ticket age outside tolerance window ( diff = %"
308 MBEDTLS_PRINTF_MS_TIME ")",
Jerry Yucebffc32022-12-15 18:00:05 +0800309 age_diff));
Jerry Yu8d4bbba2022-09-13 14:15:48 +0800310 goto exit;
311 }
Jerry Yu95699e72022-08-21 19:22:23 +0800312#endif /* MBEDTLS_HAVE_TIME */
Jerry Yu8d4bbba2022-09-13 14:15:48 +0800313
Ronald Cron38117652024-03-05 09:05:46 +0100314 /*
315 * All good, we have found a suitable ticket.
316 */
Ronald Cronfbae94a2023-12-05 18:15:14 +0100317 ret = SSL_TLS1_3_PSK_IDENTITY_MATCH;
318
Jerry Yu8d4bbba2022-09-13 14:15:48 +0800319exit:
Ronald Cronfbae94a2023-12-05 18:15:14 +0100320 if (ret != SSL_TLS1_3_PSK_IDENTITY_MATCH) {
Gilles Peskine449bd832023-01-11 14:50:10 +0100321 mbedtls_ssl_session_free(session);
322 }
Jerry Yu95699e72022-08-21 19:22:23 +0800323
Gilles Peskine449bd832023-01-11 14:50:10 +0100324 MBEDTLS_SSL_DEBUG_MSG(2, ("<= check_identity_match_ticket"));
325 return ret;
Jerry Yu95699e72022-08-21 19:22:23 +0800326}
327#endif /* MBEDTLS_SSL_SESSION_TICKETS */
328
Jerry Yu6e74a7e2022-07-20 20:49:32 +0800329MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yu1c105562022-07-10 06:32:38 +0000330static int ssl_tls13_offered_psks_check_identity_match(
Gilles Peskine449bd832023-01-11 14:50:10 +0100331 mbedtls_ssl_context *ssl,
332 const unsigned char *identity,
333 size_t identity_len,
334 uint32_t obfuscated_ticket_age,
335 int *psk_type,
336 mbedtls_ssl_session *session)
Jerry Yu1c105562022-07-10 06:32:38 +0000337{
Ronald Cron606671e2023-02-17 11:36:33 +0100338 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
339
Jerry Yu95699e72022-08-21 19:22:23 +0800340 ((void) session);
341 ((void) obfuscated_ticket_age);
Jerry Yu5725f1c2022-08-21 17:27:16 +0800342 *psk_type = MBEDTLS_SSL_TLS1_3_PSK_EXTERNAL;
Jerry Yu95699e72022-08-21 19:22:23 +0800343
Gilles Peskine449bd832023-01-11 14:50:10 +0100344 MBEDTLS_SSL_DEBUG_BUF(4, "identity", identity, identity_len);
Jerry Yu95699e72022-08-21 19:22:23 +0800345
Jerry Yu95699e72022-08-21 19:22:23 +0800346#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Ronald Cron7a30cf52024-02-23 14:38:59 +0100347 ret = ssl_tls13_offered_psks_check_identity_match_ticket(
348 ssl, identity, identity_len, obfuscated_ticket_age, session);
349 if (ret == SSL_TLS1_3_PSK_IDENTITY_MATCH) {
Jerry Yu95699e72022-08-21 19:22:23 +0800350 *psk_type = MBEDTLS_SSL_TLS1_3_PSK_RESUMPTION;
Ronald Cron606671e2023-02-17 11:36:33 +0100351 ret = mbedtls_ssl_set_hs_psk(ssl,
352 session->resumption_key,
353 session->resumption_key_len);
354 if (ret != 0) {
355 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_set_hs_psk", ret);
356 return ret;
357 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100358
359 MBEDTLS_SSL_DEBUG_BUF(4, "Ticket-resumed PSK:",
360 session->resumption_key,
361 session->resumption_key_len);
362 MBEDTLS_SSL_DEBUG_MSG(4, ("ticket: obfuscated_ticket_age: %u",
363 (unsigned) obfuscated_ticket_age));
Ronald Cronfbae94a2023-12-05 18:15:14 +0100364 return SSL_TLS1_3_PSK_IDENTITY_MATCH;
Ronald Cron7a30cf52024-02-23 14:38:59 +0100365 } else if (ret == SSL_TLS1_3_PSK_IDENTITY_MATCH_BUT_PSK_NOT_USABLE) {
366 return SSL_TLS1_3_PSK_IDENTITY_MATCH_BUT_PSK_NOT_USABLE;
Jerry Yu95699e72022-08-21 19:22:23 +0800367 }
368#endif /* MBEDTLS_SSL_SESSION_TICKETS */
369
Jerry Yu1c105562022-07-10 06:32:38 +0000370 /* Check identity with external configured function */
Gilles Peskine449bd832023-01-11 14:50:10 +0100371 if (ssl->conf->f_psk != NULL) {
372 if (ssl->conf->f_psk(
373 ssl->conf->p_psk, ssl, identity, identity_len) == 0) {
Ronald Cronfbae94a2023-12-05 18:15:14 +0100374 return SSL_TLS1_3_PSK_IDENTITY_MATCH;
Jerry Yu1c105562022-07-10 06:32:38 +0000375 }
Ronald Cronfbae94a2023-12-05 18:15:14 +0100376 return SSL_TLS1_3_PSK_IDENTITY_DOES_NOT_MATCH;
Jerry Yu1c105562022-07-10 06:32:38 +0000377 }
378
Gilles Peskine449bd832023-01-11 14:50:10 +0100379 MBEDTLS_SSL_DEBUG_BUF(5, "identity", identity, identity_len);
Jerry Yu1c105562022-07-10 06:32:38 +0000380 /* Check identity with pre-configured psk */
Gilles Peskine449bd832023-01-11 14:50:10 +0100381 if (ssl->conf->psk_identity != NULL &&
Jerry Yu2f0abc92022-07-22 19:34:48 +0800382 identity_len == ssl->conf->psk_identity_len &&
Gilles Peskine449bd832023-01-11 14:50:10 +0100383 mbedtls_ct_memcmp(ssl->conf->psk_identity,
384 identity, identity_len) == 0) {
Ronald Cron606671e2023-02-17 11:36:33 +0100385 ret = mbedtls_ssl_set_hs_psk(ssl, ssl->conf->psk, ssl->conf->psk_len);
386 if (ret != 0) {
387 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_set_hs_psk", ret);
388 return ret;
389 }
Ronald Cronfbae94a2023-12-05 18:15:14 +0100390 return SSL_TLS1_3_PSK_IDENTITY_MATCH;
Jerry Yu1c105562022-07-10 06:32:38 +0000391 }
392
Ronald Cronfbae94a2023-12-05 18:15:14 +0100393 return SSL_TLS1_3_PSK_IDENTITY_DOES_NOT_MATCH;
Jerry Yu1c105562022-07-10 06:32:38 +0000394}
395
Ronald Cron38117652024-03-05 09:05:46 +0100396/*
397 * Non-error return values of ssl_tls13_offered_psks_check_binder_match().
398 * They are positive to not collide with error codes that are negative. Zero
399 * (SSL_TLS1_3_BINDER_MATCH) in case of success as it may be propagated up
400 * by the callers of this function as a generic success condition.
401 */
Ronald Cron6e311272023-12-05 17:57:01 +0100402#define SSL_TLS1_3_BINDER_DOES_NOT_MATCH 1
403#define SSL_TLS1_3_BINDER_MATCH 0
Jerry Yu6e74a7e2022-07-20 20:49:32 +0800404MBEDTLS_CHECK_RETURN_CRITICAL
Xiaokang Qian9f1747b2023-03-30 02:50:04 +0000405static int ssl_tls13_offered_psks_check_binder_match(
406 mbedtls_ssl_context *ssl,
407 const unsigned char *binder, size_t binder_len,
408 int psk_type, psa_algorithm_t psk_hash_alg)
Jerry Yu1c105562022-07-10 06:32:38 +0000409{
410 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yu96a2e362022-07-21 15:11:34 +0800411
Jerry Yudaf375a2022-07-20 21:31:43 +0800412 unsigned char transcript[PSA_HASH_MAX_SIZE];
Jerry Yu1c105562022-07-10 06:32:38 +0000413 size_t transcript_len;
Jerry Yu2f0abc92022-07-22 19:34:48 +0800414 unsigned char *psk;
Jerry Yu96a2e362022-07-21 15:11:34 +0800415 size_t psk_len;
Jerry Yudaf375a2022-07-20 21:31:43 +0800416 unsigned char server_computed_binder[PSA_HASH_MAX_SIZE];
Jerry Yu1c105562022-07-10 06:32:38 +0000417
Ronald Crona5c5c582024-03-19 13:54:15 +0100418 if (binder_len != PSA_HASH_LENGTH(psk_hash_alg)) {
419 return SSL_TLS1_3_BINDER_DOES_NOT_MATCH;
420 }
421
Jerry Yu1c105562022-07-10 06:32:38 +0000422 /* Get current state of handshake transcript. */
Jerry Yu29d9faa2022-08-23 17:52:45 +0800423 ret = mbedtls_ssl_get_handshake_transcript(
Manuel Pégourié-Gonnard2d6d9932023-03-28 11:38:08 +0200424 ssl, mbedtls_md_type_from_psa_alg(psk_hash_alg),
Gilles Peskine449bd832023-01-11 14:50:10 +0100425 transcript, sizeof(transcript), &transcript_len);
426 if (ret != 0) {
427 return ret;
428 }
Jerry Yu1c105562022-07-10 06:32:38 +0000429
Gilles Peskine449bd832023-01-11 14:50:10 +0100430 ret = mbedtls_ssl_tls13_export_handshake_psk(ssl, &psk, &psk_len);
431 if (ret != 0) {
432 return ret;
433 }
Jerry Yu96a2e362022-07-21 15:11:34 +0800434
Gilles Peskine449bd832023-01-11 14:50:10 +0100435 ret = mbedtls_ssl_tls13_create_psk_binder(ssl, psk_hash_alg,
436 psk, psk_len, psk_type,
437 transcript,
438 server_computed_binder);
Jerry Yu96a2e362022-07-21 15:11:34 +0800439#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +0100440 mbedtls_free((void *) psk);
Jerry Yu96a2e362022-07-21 15:11:34 +0800441#endif
Gilles Peskine449bd832023-01-11 14:50:10 +0100442 if (ret != 0) {
443 MBEDTLS_SSL_DEBUG_MSG(1, ("PSK binder calculation failed."));
444 return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
Jerry Yu1c105562022-07-10 06:32:38 +0000445 }
446
Gilles Peskine449bd832023-01-11 14:50:10 +0100447 MBEDTLS_SSL_DEBUG_BUF(3, "psk binder ( computed ): ",
448 server_computed_binder, transcript_len);
449 MBEDTLS_SSL_DEBUG_BUF(3, "psk binder ( received ): ", binder, binder_len);
Jerry Yu1c105562022-07-10 06:32:38 +0000450
Ronald Crona5c5c582024-03-19 13:54:15 +0100451 if (mbedtls_ct_memcmp(server_computed_binder,
452 binder,
453 PSA_HASH_LENGTH(psk_hash_alg)) == 0) {
Ronald Cron6e311272023-12-05 17:57:01 +0100454 return SSL_TLS1_3_BINDER_MATCH;
Jerry Yu1c105562022-07-10 06:32:38 +0000455 }
456
Gilles Peskine449bd832023-01-11 14:50:10 +0100457 mbedtls_platform_zeroize(server_computed_binder,
458 sizeof(server_computed_binder));
Ronald Cron6e311272023-12-05 17:57:01 +0100459 return SSL_TLS1_3_BINDER_DOES_NOT_MATCH;
Jerry Yuf35ba382022-08-23 17:58:26 +0800460}
Jerry Yu5725f1c2022-08-21 17:27:16 +0800461
Jerry Yu82534862022-08-30 10:42:33 +0800462#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Jerry Yuf35ba382022-08-23 17:58:26 +0800463MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100464static int ssl_tls13_session_copy_ticket(mbedtls_ssl_session *dst,
465 const mbedtls_ssl_session *src)
Jerry Yu82534862022-08-30 10:42:33 +0800466{
Jerry Yu82534862022-08-30 10:42:33 +0800467 dst->ticket_age_add = src->ticket_age_add;
468 dst->ticket_flags = src->ticket_flags;
469 dst->resumption_key_len = src->resumption_key_len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100470 if (src->resumption_key_len == 0) {
471 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
472 }
473 memcpy(dst->resumption_key, src->resumption_key, src->resumption_key_len);
Jerry Yu4746b102022-09-13 11:11:48 +0800474
Jerry Yu33bf2402022-12-12 16:01:43 +0800475#if defined(MBEDTLS_SSL_EARLY_DATA)
476 dst->max_early_data_size = src->max_early_data_size;
Waleed Elmelegy2824a202024-02-23 17:51:47 +0000477
478#if defined(MBEDTLS_SSL_ALPN)
Waleed Elmelegy5bc52632024-03-12 16:25:08 +0000479 int ret = mbedtls_ssl_session_set_ticket_alpn(dst, src->ticket_alpn);
Waleed Elmelegy883f77c2024-03-06 19:09:41 +0000480 if (ret != 0) {
481 return ret;
Waleed Elmelegy2824a202024-02-23 17:51:47 +0000482 }
483#endif /* MBEDTLS_SSL_ALPN */
484#endif /* MBEDTLS_SSL_EARLY_DATA*/
Jerry Yu33bf2402022-12-12 16:01:43 +0800485
Gilles Peskine449bd832023-01-11 14:50:10 +0100486 return 0;
Jerry Yu82534862022-08-30 10:42:33 +0800487}
488#endif /* MBEDTLS_SSL_SESSION_TICKETS */
489
Ronald Cron79cdd412024-02-20 17:19:28 +0100490struct psk_attributes {
491 int type;
492 int key_exchange_mode;
Ronald Cron74a16292024-02-23 17:07:41 +0100493 const mbedtls_ssl_ciphersuite_t *ciphersuite_info;
Ronald Cron79cdd412024-02-20 17:19:28 +0100494};
Ronald Cron16cc3702024-03-07 15:04:56 +0100495#define PSK_ATTRIBUTES_INIT { 0, 0, NULL }
Ronald Cron79cdd412024-02-20 17:19:28 +0100496
Jerry Yu1c105562022-07-10 06:32:38 +0000497/* Parser for pre_shared_key extension in client hello
498 * struct {
499 * opaque identity<1..2^16-1>;
500 * uint32 obfuscated_ticket_age;
501 * } PskIdentity;
502 *
503 * opaque PskBinderEntry<32..255>;
504 *
505 * struct {
506 * PskIdentity identities<7..2^16-1>;
507 * PskBinderEntry binders<33..2^16-1>;
508 * } OfferedPsks;
509 *
510 * struct {
511 * select (Handshake.msg_type) {
512 * case client_hello: OfferedPsks;
513 * ....
514 * };
515 * } PreSharedKeyExtension;
516 */
Jerry Yu6e74a7e2022-07-20 20:49:32 +0800517MBEDTLS_CHECK_RETURN_CRITICAL
Xiaokang Qian9f1747b2023-03-30 02:50:04 +0000518static int ssl_tls13_parse_pre_shared_key_ext(
519 mbedtls_ssl_context *ssl,
520 const unsigned char *pre_shared_key_ext,
521 const unsigned char *pre_shared_key_ext_end,
522 const unsigned char *ciphersuites,
Ronald Cron79cdd412024-02-20 17:19:28 +0100523 const unsigned char *ciphersuites_end,
524 struct psk_attributes *psk)
Jerry Yu1c105562022-07-10 06:32:38 +0000525{
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +0100526 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yu29d9faa2022-08-23 17:52:45 +0800527 const unsigned char *identities = pre_shared_key_ext;
Jerry Yu568ec252022-07-22 21:27:34 +0800528 const unsigned char *p_identity_len;
529 size_t identities_len;
Jerry Yu1c105562022-07-10 06:32:38 +0000530 const unsigned char *identities_end;
Jerry Yu568ec252022-07-22 21:27:34 +0800531 const unsigned char *binders;
532 const unsigned char *p_binder_len;
533 size_t binders_len;
Jerry Yu1c105562022-07-10 06:32:38 +0000534 const unsigned char *binders_end;
Jerry Yu96a2e362022-07-21 15:11:34 +0800535 int matched_identity = -1;
536 int identity_id = -1;
Jerry Yu1c105562022-07-10 06:32:38 +0000537
Gilles Peskine449bd832023-01-11 14:50:10 +0100538 MBEDTLS_SSL_DEBUG_BUF(3, "pre_shared_key extension",
539 pre_shared_key_ext,
540 pre_shared_key_ext_end - pre_shared_key_ext);
Jerry Yu1c105562022-07-10 06:32:38 +0000541
Jerry Yu96a2e362022-07-21 15:11:34 +0800542 /* identities_len 2 bytes
543 * identities_data >= 7 bytes
544 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100545 MBEDTLS_SSL_CHK_BUF_READ_PTR(identities, pre_shared_key_ext_end, 7 + 2);
546 identities_len = MBEDTLS_GET_UINT16_BE(identities, 0);
Jerry Yu568ec252022-07-22 21:27:34 +0800547 p_identity_len = identities + 2;
Gilles Peskine449bd832023-01-11 14:50:10 +0100548 MBEDTLS_SSL_CHK_BUF_READ_PTR(p_identity_len, pre_shared_key_ext_end,
549 identities_len);
Jerry Yu568ec252022-07-22 21:27:34 +0800550 identities_end = p_identity_len + identities_len;
Jerry Yu1c105562022-07-10 06:32:38 +0000551
Jerry Yu96a2e362022-07-21 15:11:34 +0800552 /* binders_len 2 bytes
553 * binders >= 33 bytes
554 */
Jerry Yu568ec252022-07-22 21:27:34 +0800555 binders = identities_end;
Gilles Peskine449bd832023-01-11 14:50:10 +0100556 MBEDTLS_SSL_CHK_BUF_READ_PTR(binders, pre_shared_key_ext_end, 33 + 2);
557 binders_len = MBEDTLS_GET_UINT16_BE(binders, 0);
Jerry Yu568ec252022-07-22 21:27:34 +0800558 p_binder_len = binders + 2;
Gilles Peskine449bd832023-01-11 14:50:10 +0100559 MBEDTLS_SSL_CHK_BUF_READ_PTR(p_binder_len, pre_shared_key_ext_end, binders_len);
Jerry Yu568ec252022-07-22 21:27:34 +0800560 binders_end = p_binder_len + binders_len;
Jerry Yu1c105562022-07-10 06:32:38 +0000561
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +0100562 ret = ssl->handshake->update_checksum(ssl, pre_shared_key_ext,
563 identities_end - pre_shared_key_ext);
564 if (0 != ret) {
565 MBEDTLS_SSL_DEBUG_RET(1, ("update_checksum"), ret);
566 return ret;
567 }
Jerry Yu96a2e362022-07-21 15:11:34 +0800568
Gilles Peskine449bd832023-01-11 14:50:10 +0100569 while (p_identity_len < identities_end && p_binder_len < binders_end) {
Jerry Yu1c105562022-07-10 06:32:38 +0000570 const unsigned char *identity;
Jerry Yu568ec252022-07-22 21:27:34 +0800571 size_t identity_len;
Jerry Yu82534862022-08-30 10:42:33 +0800572 uint32_t obfuscated_ticket_age;
Jerry Yu96a2e362022-07-21 15:11:34 +0800573 const unsigned char *binder;
Jerry Yu568ec252022-07-22 21:27:34 +0800574 size_t binder_len;
Ronald Cron89089cc2024-02-14 18:18:08 +0100575 int psk_ciphersuite_id;
576 psa_algorithm_t psk_hash_alg;
Ronald Croncf284562024-02-16 18:54:10 +0100577 int allowed_key_exchange_modes;
Ronald Cron74a16292024-02-23 17:07:41 +0100578
Jerry Yu82534862022-08-30 10:42:33 +0800579 mbedtls_ssl_session session;
Gilles Peskine449bd832023-01-11 14:50:10 +0100580 mbedtls_ssl_session_init(&session);
Jerry Yubb852022022-07-20 21:10:44 +0800581
Gilles Peskine449bd832023-01-11 14:50:10 +0100582 MBEDTLS_SSL_CHK_BUF_READ_PTR(p_identity_len, identities_end, 2 + 1 + 4);
583 identity_len = MBEDTLS_GET_UINT16_BE(p_identity_len, 0);
Jerry Yu568ec252022-07-22 21:27:34 +0800584 identity = p_identity_len + 2;
Gilles Peskine449bd832023-01-11 14:50:10 +0100585 MBEDTLS_SSL_CHK_BUF_READ_PTR(identity, identities_end, identity_len + 4);
586 obfuscated_ticket_age = MBEDTLS_GET_UINT32_BE(identity, identity_len);
Jerry Yu568ec252022-07-22 21:27:34 +0800587 p_identity_len += identity_len + 6;
Jerry Yu1c105562022-07-10 06:32:38 +0000588
Gilles Peskine449bd832023-01-11 14:50:10 +0100589 MBEDTLS_SSL_CHK_BUF_READ_PTR(p_binder_len, binders_end, 1 + 32);
Jerry Yu568ec252022-07-22 21:27:34 +0800590 binder_len = *p_binder_len;
591 binder = p_binder_len + 1;
Gilles Peskine449bd832023-01-11 14:50:10 +0100592 MBEDTLS_SSL_CHK_BUF_READ_PTR(binder, binders_end, binder_len);
Jerry Yu568ec252022-07-22 21:27:34 +0800593 p_binder_len += binder_len + 1;
Jerry Yu96a2e362022-07-21 15:11:34 +0800594
Jerry Yu96a2e362022-07-21 15:11:34 +0800595 identity_id++;
Gilles Peskine449bd832023-01-11 14:50:10 +0100596 if (matched_identity != -1) {
Jerry Yu1c105562022-07-10 06:32:38 +0000597 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +0100598 }
Jerry Yu1c105562022-07-10 06:32:38 +0000599
Jerry Yu96a2e362022-07-21 15:11:34 +0800600 ret = ssl_tls13_offered_psks_check_identity_match(
Gilles Peskine449bd832023-01-11 14:50:10 +0100601 ssl, identity, identity_len, obfuscated_ticket_age,
Ronald Cron79cdd412024-02-20 17:19:28 +0100602 &psk->type, &session);
Ronald Cronfbae94a2023-12-05 18:15:14 +0100603 if (ret != SSL_TLS1_3_PSK_IDENTITY_MATCH) {
Jerry Yu96a2e362022-07-21 15:11:34 +0800604 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +0100605 }
Jerry Yu96a2e362022-07-21 15:11:34 +0800606
Gilles Peskine449bd832023-01-11 14:50:10 +0100607 MBEDTLS_SSL_DEBUG_MSG(4, ("found matched identity"));
Ronald Cron89089cc2024-02-14 18:18:08 +0100608
Ronald Cron79cdd412024-02-20 17:19:28 +0100609 switch (psk->type) {
Jerry Yu0baf9072022-08-25 11:21:04 +0800610 case MBEDTLS_SSL_TLS1_3_PSK_EXTERNAL:
Ronald Cron89089cc2024-02-14 18:18:08 +0100611 psk_ciphersuite_id = 0;
612 psk_hash_alg = PSA_ALG_SHA_256;
Ronald Croncf284562024-02-16 18:54:10 +0100613 allowed_key_exchange_modes =
614 MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK_ALL;
Jerry Yu0baf9072022-08-25 11:21:04 +0800615 break;
Jerry Yu82534862022-08-30 10:42:33 +0800616#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Ronald Cronf7e99162024-02-14 16:04:02 +0100617 case MBEDTLS_SSL_TLS1_3_PSK_RESUMPTION:
Ronald Cron89089cc2024-02-14 18:18:08 +0100618 psk_ciphersuite_id = session.ciphersuite;
619 psk_hash_alg = PSA_ALG_NONE;
Ronald Croncf284562024-02-16 18:54:10 +0100620 ssl->session_negotiate->ticket_flags = session.ticket_flags;
621 allowed_key_exchange_modes =
622 session.ticket_flags &
623 MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK_ALL;
Jerry Yu0baf9072022-08-25 11:21:04 +0800624 break;
Ronald Cronf7e99162024-02-14 16:04:02 +0100625#endif
Jerry Yu0baf9072022-08-25 11:21:04 +0800626 default:
Gilles Peskine449bd832023-01-11 14:50:10 +0100627 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Jerry Yu0baf9072022-08-25 11:21:04 +0800628 }
Ronald Cron89089cc2024-02-14 18:18:08 +0100629
Ronald Cron79cdd412024-02-20 17:19:28 +0100630 psk->key_exchange_mode = MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_NONE;
631
Ronald Croncf284562024-02-16 18:54:10 +0100632 if ((allowed_key_exchange_modes &
633 MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK_EPHEMERAL) &&
634 ssl_tls13_key_exchange_is_psk_ephemeral_available(ssl)) {
Ronald Cron79cdd412024-02-20 17:19:28 +0100635 psk->key_exchange_mode = MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK_EPHEMERAL;
Ronald Croncf284562024-02-16 18:54:10 +0100636 } else if ((allowed_key_exchange_modes &
637 MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK) &&
638 ssl_tls13_key_exchange_is_psk_available(ssl)) {
Ronald Cron79cdd412024-02-20 17:19:28 +0100639 psk->key_exchange_mode = MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK;
Ronald Croncf284562024-02-16 18:54:10 +0100640 }
641
Ronald Cron79cdd412024-02-20 17:19:28 +0100642 if (psk->key_exchange_mode == MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_NONE) {
Ronald Croncf284562024-02-16 18:54:10 +0100643 MBEDTLS_SSL_DEBUG_MSG(3, ("No suitable PSK key exchange mode"));
644 continue;
645 }
646
Ronald Cron89089cc2024-02-14 18:18:08 +0100647 ssl_tls13_select_ciphersuite(ssl, ciphersuites, ciphersuites_end,
648 psk_ciphersuite_id, psk_hash_alg,
Ronald Cron74a16292024-02-23 17:07:41 +0100649 &psk->ciphersuite_info);
Ronald Cron89089cc2024-02-14 18:18:08 +0100650
Ronald Cron74a16292024-02-23 17:07:41 +0100651 if (psk->ciphersuite_info == NULL) {
Ronald Cron89089cc2024-02-14 18:18:08 +0100652#if defined(MBEDTLS_SSL_SESSION_TICKETS)
653 mbedtls_ssl_session_free(&session);
654#endif
655 /*
656 * We consider finding a ciphersuite suitable for the PSK as part
657 * of the validation of its binder. Thus if we do not find one, we
658 * abort the handshake with a decrypt_error alert.
659 */
Jerry Yuf35ba382022-08-23 17:58:26 +0800660 MBEDTLS_SSL_PEND_FATAL_ALERT(
661 MBEDTLS_SSL_ALERT_MSG_DECRYPT_ERROR,
Gilles Peskine449bd832023-01-11 14:50:10 +0100662 MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE);
Ronald Cron89089cc2024-02-14 18:18:08 +0100663 return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
Jerry Yu5725f1c2022-08-21 17:27:16 +0800664 }
665
Jerry Yu96a2e362022-07-21 15:11:34 +0800666 ret = ssl_tls13_offered_psks_check_binder_match(
Ronald Cron79cdd412024-02-20 17:19:28 +0100667 ssl, binder, binder_len, psk->type,
Ronald Cron74a16292024-02-23 17:07:41 +0100668 mbedtls_md_psa_alg_from_type((mbedtls_md_type_t) psk->ciphersuite_info->mac));
Ronald Cron6e311272023-12-05 17:57:01 +0100669 if (ret != SSL_TLS1_3_BINDER_MATCH) {
Jerry Yuc5a23a02022-08-25 10:51:44 +0800670 /* For security reasons, the handshake should be aborted when we
671 * fail to validate a binder value. See RFC 8446 section 4.2.11.2
672 * and appendix E.6. */
Jerry Yu82534862022-08-30 10:42:33 +0800673#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Gilles Peskine449bd832023-01-11 14:50:10 +0100674 mbedtls_ssl_session_free(&session);
Jerry Yu82534862022-08-30 10:42:33 +0800675#endif
Gilles Peskine449bd832023-01-11 14:50:10 +0100676 MBEDTLS_SSL_DEBUG_MSG(3, ("Invalid binder."));
Xiaokang Qian9f1747b2023-03-30 02:50:04 +0000677 MBEDTLS_SSL_DEBUG_RET(
678 1, "ssl_tls13_offered_psks_check_binder_match", ret);
Jerry Yu96a2e362022-07-21 15:11:34 +0800679 MBEDTLS_SSL_PEND_FATAL_ALERT(
680 MBEDTLS_SSL_ALERT_MSG_DECRYPT_ERROR,
Gilles Peskine449bd832023-01-11 14:50:10 +0100681 MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE);
682 return ret;
Jerry Yu96a2e362022-07-21 15:11:34 +0800683 }
Jerry Yu96a2e362022-07-21 15:11:34 +0800684
685 matched_identity = identity_id;
Jerry Yu5725f1c2022-08-21 17:27:16 +0800686
Jerry Yu82534862022-08-30 10:42:33 +0800687#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Ronald Cron79cdd412024-02-20 17:19:28 +0100688 if (psk->type == MBEDTLS_SSL_TLS1_3_PSK_RESUMPTION) {
Gilles Peskine449bd832023-01-11 14:50:10 +0100689 ret = ssl_tls13_session_copy_ticket(ssl->session_negotiate,
690 &session);
691 mbedtls_ssl_session_free(&session);
692 if (ret != 0) {
693 return ret;
694 }
Jerry Yu82534862022-08-30 10:42:33 +0800695 }
696#endif /* MBEDTLS_SSL_SESSION_TICKETS */
Jerry Yu1c105562022-07-10 06:32:38 +0000697 }
698
Gilles Peskine449bd832023-01-11 14:50:10 +0100699 if (p_identity_len != identities_end || p_binder_len != binders_end) {
700 MBEDTLS_SSL_DEBUG_MSG(3, ("pre_shared_key extension decode error"));
701 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR,
702 MBEDTLS_ERR_SSL_DECODE_ERROR);
703 return MBEDTLS_ERR_SSL_DECODE_ERROR;
Jerry Yu1c105562022-07-10 06:32:38 +0000704 }
705
Jerry Yu6f1db3f2022-07-22 23:05:59 +0800706 /* Update the handshake transcript with the binder list. */
Xiaokang Qian9f1747b2023-03-30 02:50:04 +0000707 ret = ssl->handshake->update_checksum(
708 ssl, identities_end, (size_t) (binders_end - identities_end));
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +0100709 if (0 != ret) {
710 MBEDTLS_SSL_DEBUG_RET(1, ("update_checksum"), ret);
711 return ret;
712 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100713 if (matched_identity == -1) {
Ronald Croncf284562024-02-16 18:54:10 +0100714 MBEDTLS_SSL_DEBUG_MSG(3, ("No usable PSK or ticket."));
Gilles Peskine449bd832023-01-11 14:50:10 +0100715 return MBEDTLS_ERR_SSL_UNKNOWN_IDENTITY;
Jerry Yu1c105562022-07-10 06:32:38 +0000716 }
717
Gilles Peskine449bd832023-01-11 14:50:10 +0100718 ssl->handshake->selected_identity = (uint16_t) matched_identity;
719 MBEDTLS_SSL_DEBUG_MSG(3, ("Pre shared key found"));
Jerry Yu1c105562022-07-10 06:32:38 +0000720
Gilles Peskine449bd832023-01-11 14:50:10 +0100721 return 0;
Jerry Yu1c105562022-07-10 06:32:38 +0000722}
Jerry Yu032b15ce2022-07-11 06:10:03 +0000723
724/*
725 * struct {
726 * select ( Handshake.msg_type ) {
727 * ....
728 * case server_hello:
729 * uint16 selected_identity;
730 * }
731 * } PreSharedKeyExtension;
732 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100733static int ssl_tls13_write_server_pre_shared_key_ext(mbedtls_ssl_context *ssl,
734 unsigned char *buf,
735 unsigned char *end,
736 size_t *olen)
Jerry Yu032b15ce2022-07-11 06:10:03 +0000737{
Gilles Peskine449bd832023-01-11 14:50:10 +0100738 unsigned char *p = (unsigned char *) buf;
Jerry Yu032b15ce2022-07-11 06:10:03 +0000739
740 *olen = 0;
741
David Horstmann21b89762022-10-06 18:34:28 +0100742 int not_using_psk = 0;
Jerry Yu032b15ce2022-07-11 06:10:03 +0000743#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +0100744 not_using_psk = (mbedtls_svc_key_id_is_null(ssl->handshake->psk_opaque));
Jerry Yu032b15ce2022-07-11 06:10:03 +0000745#else
Gilles Peskine449bd832023-01-11 14:50:10 +0100746 not_using_psk = (ssl->handshake->psk == NULL);
Jerry Yu032b15ce2022-07-11 06:10:03 +0000747#endif
Gilles Peskine449bd832023-01-11 14:50:10 +0100748 if (not_using_psk) {
Jerry Yu032b15ce2022-07-11 06:10:03 +0000749 /* We shouldn't have called this extension writer unless we've
750 * chosen to use a PSK. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100751 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Jerry Yu032b15ce2022-07-11 06:10:03 +0000752 }
753
Gilles Peskine449bd832023-01-11 14:50:10 +0100754 MBEDTLS_SSL_DEBUG_MSG(3, ("server hello, adding pre_shared_key extension"));
755 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 6);
Jerry Yu032b15ce2022-07-11 06:10:03 +0000756
Gilles Peskine449bd832023-01-11 14:50:10 +0100757 MBEDTLS_PUT_UINT16_BE(MBEDTLS_TLS_EXT_PRE_SHARED_KEY, p, 0);
758 MBEDTLS_PUT_UINT16_BE(2, p, 2);
Jerry Yu032b15ce2022-07-11 06:10:03 +0000759
Gilles Peskine449bd832023-01-11 14:50:10 +0100760 MBEDTLS_PUT_UINT16_BE(ssl->handshake->selected_identity, p, 4);
Jerry Yu032b15ce2022-07-11 06:10:03 +0000761
762 *olen = 6;
763
Gilles Peskine449bd832023-01-11 14:50:10 +0100764 MBEDTLS_SSL_DEBUG_MSG(4, ("sent selected_identity: %u",
765 ssl->handshake->selected_identity));
Jerry Yu032b15ce2022-07-11 06:10:03 +0000766
Gilles Peskine449bd832023-01-11 14:50:10 +0100767 mbedtls_ssl_tls13_set_hs_sent_ext_mask(ssl, MBEDTLS_TLS_EXT_PRE_SHARED_KEY);
Jerry Yub95dd362022-11-08 21:19:34 +0800768
Gilles Peskine449bd832023-01-11 14:50:10 +0100769 return 0;
Jerry Yu032b15ce2022-07-11 06:10:03 +0000770}
771
Ronald Cron41a443a2022-10-04 16:38:25 +0200772#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED */
Jerry Yue19e3b92022-07-08 12:04:51 +0000773
XiaokangQian7807f9f2022-02-15 10:04:37 +0000774/* From RFC 8446:
775 * struct {
XiaokangQiancfd925f2022-04-14 07:10:37 +0000776 * ProtocolVersion versions<2..254>;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000777 * } SupportedVersions;
778 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200779MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100780static int ssl_tls13_parse_supported_versions_ext(mbedtls_ssl_context *ssl,
781 const unsigned char *buf,
782 const unsigned char *end)
XiaokangQian7807f9f2022-02-15 10:04:37 +0000783{
XiaokangQian7807f9f2022-02-15 10:04:37 +0000784 const unsigned char *p = buf;
XiaokangQian8f9dfe42022-04-15 02:52:39 +0000785 size_t versions_len;
XiaokangQian4080a7f2022-04-11 09:55:18 +0000786 const unsigned char *versions_end;
XiaokangQian0a1b54e2022-04-21 03:01:38 +0000787 uint16_t tls_version;
Ronald Cron5af4c7f2023-03-07 20:46:59 +0100788 int found_supported_version = 0;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000789
Gilles Peskine449bd832023-01-11 14:50:10 +0100790 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, 1);
XiaokangQian4080a7f2022-04-11 09:55:18 +0000791 versions_len = p[0];
XiaokangQian7807f9f2022-02-15 10:04:37 +0000792 p += 1;
793
Gilles Peskine449bd832023-01-11 14:50:10 +0100794 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, versions_len);
XiaokangQian4080a7f2022-04-11 09:55:18 +0000795 versions_end = p + versions_len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100796 while (p < versions_end) {
797 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, versions_end, 2);
798 tls_version = mbedtls_ssl_read_version(p, ssl->conf->transport);
XiaokangQianb67384d2022-04-19 00:02:38 +0000799 p += 2;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000800
Ronald Cron3bd2b022023-04-03 16:45:39 +0200801 if (MBEDTLS_SSL_VERSION_TLS1_3 == tls_version) {
Ronald Cron5af4c7f2023-03-07 20:46:59 +0100802 found_supported_version = 1;
803 break;
804 }
805
Ronald Cron3bd2b022023-04-03 16:45:39 +0200806 if ((MBEDTLS_SSL_VERSION_TLS1_2 == tls_version) &&
807 mbedtls_ssl_conf_is_tls12_enabled(ssl->conf)) {
Ronald Cron5af4c7f2023-03-07 20:46:59 +0100808 found_supported_version = 1;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000809 break;
810 }
XiaokangQian7807f9f2022-02-15 10:04:37 +0000811 }
812
Ronald Cron5af4c7f2023-03-07 20:46:59 +0100813 if (!found_supported_version) {
814 MBEDTLS_SSL_DEBUG_MSG(1, ("No supported version found."));
XiaokangQian7807f9f2022-02-15 10:04:37 +0000815
Gilles Peskine449bd832023-01-11 14:50:10 +0100816 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_PROTOCOL_VERSION,
817 MBEDTLS_ERR_SSL_BAD_PROTOCOL_VERSION);
818 return MBEDTLS_ERR_SSL_BAD_PROTOCOL_VERSION;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000819 }
820
Ronald Cron5af4c7f2023-03-07 20:46:59 +0100821 MBEDTLS_SSL_DEBUG_MSG(1, ("Negotiated version: [%04x]",
Gilles Peskine449bd832023-01-11 14:50:10 +0100822 (unsigned int) tls_version));
XiaokangQian7807f9f2022-02-15 10:04:37 +0000823
Ronald Cron5af4c7f2023-03-07 20:46:59 +0100824 return (int) tls_version;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000825}
826
Przemek Stekiel8c0a9532023-06-15 16:48:19 +0200827#if defined(PSA_WANT_ALG_ECDH) || defined(PSA_WANT_ALG_FFDH)
XiaokangQiane8ff3502022-04-22 02:34:40 +0000828/*
XiaokangQian7807f9f2022-02-15 10:04:37 +0000829 *
830 * From RFC 8446:
831 * enum {
832 * ... (0xFFFF)
833 * } NamedGroup;
834 * struct {
835 * NamedGroup named_group_list<2..2^16-1>;
836 * } NamedGroupList;
837 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200838MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100839static int ssl_tls13_parse_supported_groups_ext(mbedtls_ssl_context *ssl,
840 const unsigned char *buf,
841 const unsigned char *end)
XiaokangQian7807f9f2022-02-15 10:04:37 +0000842{
XiaokangQian7807f9f2022-02-15 10:04:37 +0000843 const unsigned char *p = buf;
XiaokangQian84823772022-04-19 07:57:30 +0000844 size_t named_group_list_len;
XiaokangQian8f9dfe42022-04-15 02:52:39 +0000845 const unsigned char *named_group_list_end;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000846
Gilles Peskine449bd832023-01-11 14:50:10 +0100847 MBEDTLS_SSL_DEBUG_BUF(3, "supported_groups extension", p, end - buf);
848 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, 2);
849 named_group_list_len = MBEDTLS_GET_UINT16_BE(p, 0);
XiaokangQian7807f9f2022-02-15 10:04:37 +0000850 p += 2;
Gilles Peskine449bd832023-01-11 14:50:10 +0100851 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, named_group_list_len);
XiaokangQian8f9dfe42022-04-15 02:52:39 +0000852 named_group_list_end = p + named_group_list_len;
XiaokangQian4e8cd7b2022-04-21 09:48:09 +0000853 ssl->handshake->hrr_selected_group = 0;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000854
Gilles Peskine449bd832023-01-11 14:50:10 +0100855 while (p < named_group_list_end) {
XiaokangQian08037552022-04-20 07:16:41 +0000856 uint16_t named_group;
Gilles Peskine449bd832023-01-11 14:50:10 +0100857 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, named_group_list_end, 2);
858 named_group = MBEDTLS_GET_UINT16_BE(p, 0);
XiaokangQian08037552022-04-20 07:16:41 +0000859 p += 2;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000860
Gilles Peskine449bd832023-01-11 14:50:10 +0100861 MBEDTLS_SSL_DEBUG_MSG(2,
862 ("got named group: %s(%04x)",
863 mbedtls_ssl_named_group_to_str(named_group),
864 named_group));
XiaokangQian08037552022-04-20 07:16:41 +0000865
Gilles Peskine449bd832023-01-11 14:50:10 +0100866 if (!mbedtls_ssl_named_group_is_offered(ssl, named_group) ||
867 !mbedtls_ssl_named_group_is_supported(named_group) ||
868 ssl->handshake->hrr_selected_group != 0) {
XiaokangQian08037552022-04-20 07:16:41 +0000869 continue;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000870 }
871
Gilles Peskine449bd832023-01-11 14:50:10 +0100872 MBEDTLS_SSL_DEBUG_MSG(2,
873 ("add named group %s(%04x) into received list.",
874 mbedtls_ssl_named_group_to_str(named_group),
875 named_group));
Jerry Yuc1be19f2022-04-23 16:11:39 +0800876
XiaokangQian4e8cd7b2022-04-21 09:48:09 +0000877 ssl->handshake->hrr_selected_group = named_group;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000878 }
879
Gilles Peskine449bd832023-01-11 14:50:10 +0100880 return 0;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000881
882}
Przemek Stekiel8c0a9532023-06-15 16:48:19 +0200883#endif /* PSA_WANT_ALG_ECDH || PSA_WANT_ALG_FFDH */
XiaokangQian7807f9f2022-02-15 10:04:37 +0000884
XiaokangQian08037552022-04-20 07:16:41 +0000885#define SSL_TLS1_3_PARSE_KEY_SHARES_EXT_NO_MATCH 1
886
Valerio Settic9ae8622023-07-25 11:23:50 +0200887#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_EPHEMERAL_ENABLED)
XiaokangQian7807f9f2022-02-15 10:04:37 +0000888/*
889 * ssl_tls13_parse_key_shares_ext() verifies whether the information in the
Xiaokang Qian9f1747b2023-03-30 02:50:04 +0000890 * extension is correct and stores the first acceptable key share and its
891 * associated group.
XiaokangQian7807f9f2022-02-15 10:04:37 +0000892 *
893 * Possible return values are:
894 * - 0: Successful processing of the client provided key share extension.
Xiaokang Qian9f1747b2023-03-30 02:50:04 +0000895 * - SSL_TLS1_3_PARSE_KEY_SHARES_EXT_NO_MATCH: The key shares provided by
896 * the client does not match a group supported by the server. A
897 * HelloRetryRequest will be needed.
XiaokangQiane8ff3502022-04-22 02:34:40 +0000898 * - A negative value for fatal errors.
Jerry Yuc1be19f2022-04-23 16:11:39 +0800899 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200900MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100901static int ssl_tls13_parse_key_shares_ext(mbedtls_ssl_context *ssl,
902 const unsigned char *buf,
903 const unsigned char *end)
XiaokangQian7807f9f2022-02-15 10:04:37 +0000904{
XiaokangQianb67384d2022-04-19 00:02:38 +0000905 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000906 unsigned char const *p = buf;
XiaokangQian8f9dfe42022-04-15 02:52:39 +0000907 unsigned char const *client_shares_end;
Jerry Yuc1be19f2022-04-23 16:11:39 +0800908 size_t client_shares_len;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000909
910 /* From RFC 8446:
911 *
912 * struct {
913 * KeyShareEntry client_shares<0..2^16-1>;
914 * } KeyShareClientHello;
915 *
916 */
917
Gilles Peskine449bd832023-01-11 14:50:10 +0100918 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, 2);
919 client_shares_len = MBEDTLS_GET_UINT16_BE(p, 0);
XiaokangQian7807f9f2022-02-15 10:04:37 +0000920 p += 2;
Gilles Peskine449bd832023-01-11 14:50:10 +0100921 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, client_shares_len);
XiaokangQian7807f9f2022-02-15 10:04:37 +0000922
923 ssl->handshake->offered_group_id = 0;
XiaokangQian8f9dfe42022-04-15 02:52:39 +0000924 client_shares_end = p + client_shares_len;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000925
926 /* We try to find a suitable key share entry and copy it to the
927 * handshake context. Later, we have to find out whether we can do
928 * something with the provided key share or whether we have to
XiaokangQianc5763b52022-04-02 03:34:37 +0000929 * dismiss it and send a HelloRetryRequest message.
930 */
XiaokangQian7807f9f2022-02-15 10:04:37 +0000931
Gilles Peskine449bd832023-01-11 14:50:10 +0100932 while (p < client_shares_end) {
XiaokangQian9b5d04b2022-04-10 10:20:43 +0000933 uint16_t group;
Jerry Yuc1be19f2022-04-23 16:11:39 +0800934 size_t key_exchange_len;
Jerry Yu086edc22022-05-05 10:50:38 +0800935 const unsigned char *key_exchange;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000936
937 /*
938 * struct {
939 * NamedGroup group;
940 * opaque key_exchange<1..2^16-1>;
941 * } KeyShareEntry;
942 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100943 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, client_shares_end, 4);
944 group = MBEDTLS_GET_UINT16_BE(p, 0);
945 key_exchange_len = MBEDTLS_GET_UINT16_BE(p, 2);
Jerry Yuc1be19f2022-04-23 16:11:39 +0800946 p += 4;
Jerry Yu086edc22022-05-05 10:50:38 +0800947 key_exchange = p;
Gilles Peskine449bd832023-01-11 14:50:10 +0100948 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, client_shares_end, key_exchange_len);
Jerry Yu086edc22022-05-05 10:50:38 +0800949 p += key_exchange_len;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000950
951 /* Continue parsing even if we have already found a match,
XiaokangQianc5763b52022-04-02 03:34:37 +0000952 * for input validation purposes.
953 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100954 if (!mbedtls_ssl_named_group_is_offered(ssl, group) ||
955 !mbedtls_ssl_named_group_is_supported(group) ||
956 ssl->handshake->offered_group_id != 0) {
XiaokangQian060d8672022-04-21 09:24:56 +0000957 continue;
958 }
XiaokangQian060d8672022-04-21 09:24:56 +0000959
XiaokangQian7807f9f2022-02-15 10:04:37 +0000960 /*
Przemek Stekielc89f3ea2023-05-18 15:45:53 +0200961 * ECDHE and FFDHE groups are supported
XiaokangQian7807f9f2022-02-15 10:04:37 +0000962 */
Przemek Stekielc89f3ea2023-05-18 15:45:53 +0200963 if (mbedtls_ssl_tls13_named_group_is_ecdhe(group) ||
Przemek Stekield5f79e72023-06-29 09:08:43 +0200964 mbedtls_ssl_tls13_named_group_is_ffdh(group)) {
Przemek Stekielc89f3ea2023-05-18 15:45:53 +0200965 MBEDTLS_SSL_DEBUG_MSG(2, ("ECDH/FFDH group: %s (%04x)",
Gilles Peskine449bd832023-01-11 14:50:10 +0100966 mbedtls_ssl_named_group_to_str(group),
967 group));
Przemek Stekiel7ac93be2023-07-04 10:02:38 +0200968 ret = mbedtls_ssl_tls13_read_public_xxdhe_share(
Gilles Peskine449bd832023-01-11 14:50:10 +0100969 ssl, key_exchange - 2, key_exchange_len + 2);
970 if (ret != 0) {
971 return ret;
972 }
XiaokangQian4e8cd7b2022-04-21 09:48:09 +0000973
Gilles Peskine449bd832023-01-11 14:50:10 +0100974 } else {
975 MBEDTLS_SSL_DEBUG_MSG(4, ("Unrecognized NamedGroup %u",
976 (unsigned) group));
XiaokangQian7807f9f2022-02-15 10:04:37 +0000977 continue;
978 }
979
XiaokangQian9b5d04b2022-04-10 10:20:43 +0000980 ssl->handshake->offered_group_id = group;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000981 }
982
Jerry Yuc1be19f2022-04-23 16:11:39 +0800983
Gilles Peskine449bd832023-01-11 14:50:10 +0100984 if (ssl->handshake->offered_group_id == 0) {
985 MBEDTLS_SSL_DEBUG_MSG(1, ("no matching key share"));
986 return SSL_TLS1_3_PARSE_KEY_SHARES_EXT_NO_MATCH;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000987 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100988 return 0;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000989}
Valerio Settic9ae8622023-07-25 11:23:50 +0200990#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_EPHEMERAL_ENABLED */
XiaokangQian7807f9f2022-02-15 10:04:37 +0000991
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200992MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100993static int ssl_tls13_client_hello_has_exts(mbedtls_ssl_context *ssl,
994 int exts_mask)
XiaokangQian7807f9f2022-02-15 10:04:37 +0000995{
Jerry Yu0c354a22022-08-29 15:25:36 +0800996 int masked = ssl->handshake->received_extensions & exts_mask;
Gilles Peskine449bd832023-01-11 14:50:10 +0100997 return masked == exts_mask;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000998}
999
Jerry Yue5991322022-11-07 14:03:44 +08001000#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001001MBEDTLS_CHECK_RETURN_CRITICAL
XiaokangQianb67384d2022-04-19 00:02:38 +00001002static int ssl_tls13_client_hello_has_exts_for_ephemeral_key_exchange(
Gilles Peskine449bd832023-01-11 14:50:10 +01001003 mbedtls_ssl_context *ssl)
XiaokangQian7807f9f2022-02-15 10:04:37 +00001004{
Gilles Peskine449bd832023-01-11 14:50:10 +01001005 return ssl_tls13_client_hello_has_exts(
1006 ssl,
1007 MBEDTLS_SSL_EXT_MASK(SUPPORTED_GROUPS) |
1008 MBEDTLS_SSL_EXT_MASK(KEY_SHARE) |
1009 MBEDTLS_SSL_EXT_MASK(SIG_ALG));
XiaokangQian7807f9f2022-02-15 10:04:37 +00001010}
Jerry Yue5991322022-11-07 14:03:44 +08001011#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED */
XiaokangQian7807f9f2022-02-15 10:04:37 +00001012
Jerry Yue5991322022-11-07 14:03:44 +08001013#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK_ENABLED)
Jerry Yu77f01482022-07-11 07:03:24 +00001014MBEDTLS_CHECK_RETURN_CRITICAL
1015static int ssl_tls13_client_hello_has_exts_for_psk_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(PRE_SHARED_KEY) |
1021 MBEDTLS_SSL_EXT_MASK(PSK_KEY_EXCHANGE_MODES));
Jerry Yu77f01482022-07-11 07:03:24 +00001022}
Jerry Yue5991322022-11-07 14:03:44 +08001023#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK_ENABLED */
Jerry Yu77f01482022-07-11 07:03:24 +00001024
Jerry Yue5991322022-11-07 14:03:44 +08001025#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK_EPHEMERAL_ENABLED)
Jerry Yu77f01482022-07-11 07:03:24 +00001026MBEDTLS_CHECK_RETURN_CRITICAL
1027static int ssl_tls13_client_hello_has_exts_for_psk_ephemeral_key_exchange(
Gilles Peskine449bd832023-01-11 14:50:10 +01001028 mbedtls_ssl_context *ssl)
Jerry Yu77f01482022-07-11 07:03:24 +00001029{
Gilles Peskine449bd832023-01-11 14:50:10 +01001030 return ssl_tls13_client_hello_has_exts(
1031 ssl,
1032 MBEDTLS_SSL_EXT_MASK(SUPPORTED_GROUPS) |
1033 MBEDTLS_SSL_EXT_MASK(KEY_SHARE) |
1034 MBEDTLS_SSL_EXT_MASK(PRE_SHARED_KEY) |
1035 MBEDTLS_SSL_EXT_MASK(PSK_KEY_EXCHANGE_MODES));
Jerry Yu77f01482022-07-11 07:03:24 +00001036}
Jerry Yue5991322022-11-07 14:03:44 +08001037#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK_EPHEMERAL_ENABLED */
Jerry Yu77f01482022-07-11 07:03:24 +00001038
Pengyu Lv306a01d2023-02-02 16:35:47 +08001039#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED)
1040MBEDTLS_CHECK_RETURN_CRITICAL
Pengyu Lvbc4aab72023-12-01 15:37:24 +08001041static int ssl_tls13_key_exchange_is_psk_available(mbedtls_ssl_context *ssl)
Pengyu Lv306a01d2023-02-02 16:35:47 +08001042{
Jerry Yue5991322022-11-07 14:03:44 +08001043#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK_ENABLED)
Ronald Crone8c162d2024-02-21 10:15:44 +01001044 return mbedtls_ssl_conf_tls13_is_psk_enabled(ssl) &&
Pengyu Lvb2cfafb2023-11-14 13:56:13 +08001045 mbedtls_ssl_tls13_is_psk_supported(ssl) &&
Gilles Peskine449bd832023-01-11 14:50:10 +01001046 ssl_tls13_client_hello_has_exts_for_psk_key_exchange(ssl);
XiaokangQian7807f9f2022-02-15 10:04:37 +00001047#else
1048 ((void) ssl);
Gilles Peskine449bd832023-01-11 14:50:10 +01001049 return 0;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001050#endif
Jerry Yu77f01482022-07-11 07:03:24 +00001051}
XiaokangQian7807f9f2022-02-15 10:04:37 +00001052
Jerry Yu77f01482022-07-11 07:03:24 +00001053MBEDTLS_CHECK_RETURN_CRITICAL
Pengyu Lvbc4aab72023-12-01 15:37:24 +08001054static int ssl_tls13_key_exchange_is_psk_ephemeral_available(mbedtls_ssl_context *ssl)
Jerry Yu77f01482022-07-11 07:03:24 +00001055{
Jerry Yue5991322022-11-07 14:03:44 +08001056#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK_EPHEMERAL_ENABLED)
Ronald Crone8c162d2024-02-21 10:15:44 +01001057 return mbedtls_ssl_conf_tls13_is_psk_ephemeral_enabled(ssl) &&
Pengyu Lvb2cfafb2023-11-14 13:56:13 +08001058 mbedtls_ssl_tls13_is_psk_ephemeral_supported(ssl) &&
Gilles Peskine449bd832023-01-11 14:50:10 +01001059 ssl_tls13_client_hello_has_exts_for_psk_ephemeral_key_exchange(ssl);
Jerry Yu77f01482022-07-11 07:03:24 +00001060#else
1061 ((void) ssl);
Gilles Peskine449bd832023-01-11 14:50:10 +01001062 return 0;
Jerry Yu77f01482022-07-11 07:03:24 +00001063#endif
XiaokangQian7807f9f2022-02-15 10:04:37 +00001064}
1065#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED */
1066
1067MBEDTLS_CHECK_RETURN_CRITICAL
1068static int ssl_tls13_key_exchange_is_ephemeral_available(mbedtls_ssl_context *ssl)
1069{
1070#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED)
1071 return mbedtls_ssl_conf_tls13_is_ephemeral_enabled(ssl) &&
1072 ssl_tls13_client_hello_has_exts_for_ephemeral_key_exchange(ssl);
1073#else
1074 ((void) ssl);
1075 return 0;
1076#endif
1077}
1078
XiaokangQian81802f42022-06-10 13:25:22 +00001079#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
Ronald Cron928cbd32022-10-04 16:14:26 +02001080 defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED)
Ronald Cron67ea2542022-09-15 17:34:42 +02001081
1082#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01001083static psa_algorithm_t ssl_tls13_iana_sig_alg_to_psa_alg(uint16_t sig_alg)
Ronald Cron67ea2542022-09-15 17:34:42 +02001084{
Gilles Peskine449bd832023-01-11 14:50:10 +01001085 switch (sig_alg) {
Ronald Cron67ea2542022-09-15 17:34:42 +02001086 case MBEDTLS_TLS1_3_SIG_ECDSA_SECP256R1_SHA256:
Gilles Peskine449bd832023-01-11 14:50:10 +01001087 return PSA_ALG_ECDSA(PSA_ALG_SHA_256);
Ronald Cron67ea2542022-09-15 17:34:42 +02001088 case MBEDTLS_TLS1_3_SIG_ECDSA_SECP384R1_SHA384:
Gilles Peskine449bd832023-01-11 14:50:10 +01001089 return PSA_ALG_ECDSA(PSA_ALG_SHA_384);
Ronald Cron67ea2542022-09-15 17:34:42 +02001090 case MBEDTLS_TLS1_3_SIG_ECDSA_SECP521R1_SHA512:
Gilles Peskine449bd832023-01-11 14:50:10 +01001091 return PSA_ALG_ECDSA(PSA_ALG_SHA_512);
Ronald Cron67ea2542022-09-15 17:34:42 +02001092 case MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA256:
Gilles Peskine449bd832023-01-11 14:50:10 +01001093 return PSA_ALG_RSA_PSS(PSA_ALG_SHA_256);
Ronald Cron67ea2542022-09-15 17:34:42 +02001094 case MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA384:
Gilles Peskine449bd832023-01-11 14:50:10 +01001095 return PSA_ALG_RSA_PSS(PSA_ALG_SHA_384);
Ronald Cron67ea2542022-09-15 17:34:42 +02001096 case MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA512:
Gilles Peskine449bd832023-01-11 14:50:10 +01001097 return PSA_ALG_RSA_PSS(PSA_ALG_SHA_512);
Ronald Cron67ea2542022-09-15 17:34:42 +02001098 case MBEDTLS_TLS1_3_SIG_RSA_PKCS1_SHA256:
Gilles Peskine449bd832023-01-11 14:50:10 +01001099 return PSA_ALG_RSA_PKCS1V15_SIGN(PSA_ALG_SHA_256);
Ronald Cron67ea2542022-09-15 17:34:42 +02001100 case MBEDTLS_TLS1_3_SIG_RSA_PKCS1_SHA384:
Gilles Peskine449bd832023-01-11 14:50:10 +01001101 return PSA_ALG_RSA_PKCS1V15_SIGN(PSA_ALG_SHA_384);
Ronald Cron67ea2542022-09-15 17:34:42 +02001102 case MBEDTLS_TLS1_3_SIG_RSA_PKCS1_SHA512:
Gilles Peskine449bd832023-01-11 14:50:10 +01001103 return PSA_ALG_RSA_PKCS1V15_SIGN(PSA_ALG_SHA_512);
Ronald Cron67ea2542022-09-15 17:34:42 +02001104 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01001105 return PSA_ALG_NONE;
Ronald Cron67ea2542022-09-15 17:34:42 +02001106 }
1107}
1108#endif /* MBEDTLS_USE_PSA_CRYPTO */
1109
XiaokangQian23c5be62022-06-07 02:04:34 +00001110/*
XiaokangQianfb665a82022-06-15 03:57:21 +00001111 * Pick best ( private key, certificate chain ) pair based on the signature
1112 * algorithms supported by the client.
XiaokangQian23c5be62022-06-07 02:04:34 +00001113 */
Ronald Cronce7d76e2022-07-08 18:56:49 +02001114MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01001115static int ssl_tls13_pick_key_cert(mbedtls_ssl_context *ssl)
XiaokangQian23c5be62022-06-07 02:04:34 +00001116{
XiaokangQianfb665a82022-06-15 03:57:21 +00001117 mbedtls_ssl_key_cert *key_cert, *key_cert_list;
XiaokangQian81802f42022-06-10 13:25:22 +00001118 const uint16_t *sig_alg = ssl->handshake->received_sig_algs;
XiaokangQian23c5be62022-06-07 02:04:34 +00001119
1120#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Gilles Peskine449bd832023-01-11 14:50:10 +01001121 if (ssl->handshake->sni_key_cert != NULL) {
XiaokangQianfb665a82022-06-15 03:57:21 +00001122 key_cert_list = ssl->handshake->sni_key_cert;
Gilles Peskine449bd832023-01-11 14:50:10 +01001123 } else
XiaokangQian23c5be62022-06-07 02:04:34 +00001124#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
Gilles Peskine449bd832023-01-11 14:50:10 +01001125 key_cert_list = ssl->conf->key_cert;
XiaokangQian23c5be62022-06-07 02:04:34 +00001126
Gilles Peskine449bd832023-01-11 14:50:10 +01001127 if (key_cert_list == NULL) {
1128 MBEDTLS_SSL_DEBUG_MSG(3, ("server has no certificate"));
1129 return -1;
XiaokangQian23c5be62022-06-07 02:04:34 +00001130 }
1131
Gilles Peskine449bd832023-01-11 14:50:10 +01001132 for (; *sig_alg != MBEDTLS_TLS1_3_SIG_NONE; sig_alg++) {
1133 if (!mbedtls_ssl_sig_alg_is_offered(ssl, *sig_alg)) {
Ronald Cron67ea2542022-09-15 17:34:42 +02001134 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +01001135 }
Ronald Cron67ea2542022-09-15 17:34:42 +02001136
Gilles Peskine449bd832023-01-11 14:50:10 +01001137 if (!mbedtls_ssl_tls13_sig_alg_for_cert_verify_is_supported(*sig_alg)) {
Ronald Cron67ea2542022-09-15 17:34:42 +02001138 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +01001139 }
Ronald Cron67ea2542022-09-15 17:34:42 +02001140
Gilles Peskine449bd832023-01-11 14:50:10 +01001141 for (key_cert = key_cert_list; key_cert != NULL;
1142 key_cert = key_cert->next) {
Ronald Cron67ea2542022-09-15 17:34:42 +02001143#if defined(MBEDTLS_USE_PSA_CRYPTO)
1144 psa_algorithm_t psa_alg = PSA_ALG_NONE;
1145#endif /* MBEDTLS_USE_PSA_CRYPTO */
1146
Gilles Peskine449bd832023-01-11 14:50:10 +01001147 MBEDTLS_SSL_DEBUG_CRT(3, "certificate (chain) candidate",
1148 key_cert->cert);
XiaokangQian81802f42022-06-10 13:25:22 +00001149
1150 /*
Gilles Peskine449bd832023-01-11 14:50:10 +01001151 * This avoids sending the client a cert it'll reject based on
1152 * keyUsage or other extensions.
1153 */
1154 if (mbedtls_x509_crt_check_key_usage(
1155 key_cert->cert, MBEDTLS_X509_KU_DIGITAL_SIGNATURE) != 0 ||
XiaokangQian81802f42022-06-10 13:25:22 +00001156 mbedtls_x509_crt_check_extended_key_usage(
XiaokangQianfb665a82022-06-15 03:57:21 +00001157 key_cert->cert, MBEDTLS_OID_SERVER_AUTH,
Gilles Peskine449bd832023-01-11 14:50:10 +01001158 MBEDTLS_OID_SIZE(MBEDTLS_OID_SERVER_AUTH)) != 0) {
1159 MBEDTLS_SSL_DEBUG_MSG(3, ("certificate mismatch: "
1160 "(extended) key usage extension"));
XiaokangQian81802f42022-06-10 13:25:22 +00001161 continue;
1162 }
XiaokangQian23c5be62022-06-07 02:04:34 +00001163
Gilles Peskine449bd832023-01-11 14:50:10 +01001164 MBEDTLS_SSL_DEBUG_MSG(3,
1165 ("ssl_tls13_pick_key_cert:"
1166 "check signature algorithm %s [%04x]",
1167 mbedtls_ssl_sig_alg_to_str(*sig_alg),
1168 *sig_alg));
Ronald Cron67ea2542022-09-15 17:34:42 +02001169#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01001170 psa_alg = ssl_tls13_iana_sig_alg_to_psa_alg(*sig_alg);
Ronald Cron67ea2542022-09-15 17:34:42 +02001171#endif /* MBEDTLS_USE_PSA_CRYPTO */
1172
Gilles Peskine449bd832023-01-11 14:50:10 +01001173 if (mbedtls_ssl_tls13_check_sig_alg_cert_key_match(
1174 *sig_alg, &key_cert->cert->pk)
Ronald Cron67ea2542022-09-15 17:34:42 +02001175#if defined(MBEDTLS_USE_PSA_CRYPTO)
1176 && psa_alg != PSA_ALG_NONE &&
Gilles Peskine449bd832023-01-11 14:50:10 +01001177 mbedtls_pk_can_do_ext(&key_cert->cert->pk, psa_alg,
1178 PSA_KEY_USAGE_SIGN_HASH) == 1
Ronald Cron67ea2542022-09-15 17:34:42 +02001179#endif /* MBEDTLS_USE_PSA_CRYPTO */
Gilles Peskine449bd832023-01-11 14:50:10 +01001180 ) {
XiaokangQianfb665a82022-06-15 03:57:21 +00001181 ssl->handshake->key_cert = key_cert;
Gilles Peskine449bd832023-01-11 14:50:10 +01001182 MBEDTLS_SSL_DEBUG_MSG(3,
1183 ("ssl_tls13_pick_key_cert:"
1184 "selected signature algorithm"
1185 " %s [%04x]",
1186 mbedtls_ssl_sig_alg_to_str(*sig_alg),
1187 *sig_alg));
XiaokangQianfb665a82022-06-15 03:57:21 +00001188 MBEDTLS_SSL_DEBUG_CRT(
Gilles Peskine449bd832023-01-11 14:50:10 +01001189 3, "selected certificate (chain)",
1190 ssl->handshake->key_cert->cert);
1191 return 0;
XiaokangQian81802f42022-06-10 13:25:22 +00001192 }
XiaokangQian81802f42022-06-10 13:25:22 +00001193 }
XiaokangQian23c5be62022-06-07 02:04:34 +00001194 }
1195
Gilles Peskine449bd832023-01-11 14:50:10 +01001196 MBEDTLS_SSL_DEBUG_MSG(2, ("ssl_tls13_pick_key_cert:"
1197 "no suitable certificate found"));
1198 return -1;
XiaokangQian23c5be62022-06-07 02:04:34 +00001199}
XiaokangQian81802f42022-06-10 13:25:22 +00001200#endif /* MBEDTLS_X509_CRT_PARSE_C &&
Ronald Cron928cbd32022-10-04 16:14:26 +02001201 MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED */
XiaokangQian23c5be62022-06-07 02:04:34 +00001202
XiaokangQian4080a7f2022-04-11 09:55:18 +00001203/*
XiaokangQianed582dd2022-04-13 08:21:05 +00001204 *
1205 * STATE HANDLING: ClientHello
1206 *
XiaokangQianb67384d2022-04-19 00:02:38 +00001207 * There are three possible classes of outcomes when parsing the ClientHello:
XiaokangQianed582dd2022-04-13 08:21:05 +00001208 *
XiaokangQianb67384d2022-04-19 00:02:38 +00001209 * 1) The ClientHello was well-formed and matched the server's configuration.
XiaokangQianed582dd2022-04-13 08:21:05 +00001210 *
1211 * In this case, the server progresses to sending its ServerHello.
1212 *
XiaokangQianb67384d2022-04-19 00:02:38 +00001213 * 2) The ClientHello was well-formed but didn't match the server's
1214 * configuration.
XiaokangQianed582dd2022-04-13 08:21:05 +00001215 *
1216 * For example, the client might not have offered a key share which
1217 * the server supports, or the server might require a cookie.
1218 *
1219 * In this case, the server sends a HelloRetryRequest.
1220 *
XiaokangQianb67384d2022-04-19 00:02:38 +00001221 * 3) The ClientHello was ill-formed
XiaokangQianed582dd2022-04-13 08:21:05 +00001222 *
1223 * In this case, we abort the handshake.
1224 *
1225 */
1226
1227/*
XiaokangQian4080a7f2022-04-11 09:55:18 +00001228 * Structure of this message:
1229 *
XiaokangQiane8ff3502022-04-22 02:34:40 +00001230 * uint16 ProtocolVersion;
1231 * opaque Random[32];
1232 * uint8 CipherSuite[2]; // Cryptographic suite selector
XiaokangQian4080a7f2022-04-11 09:55:18 +00001233 *
XiaokangQiane8ff3502022-04-22 02:34:40 +00001234 * struct {
1235 * ProtocolVersion legacy_version = 0x0303; // TLS v1.2
1236 * Random random;
1237 * opaque legacy_session_id<0..32>;
1238 * CipherSuite cipher_suites<2..2^16-2>;
1239 * opaque legacy_compression_methods<1..2^8-1>;
1240 * Extension extensions<8..2^16-1>;
1241 * } ClientHello;
XiaokangQian4080a7f2022-04-11 09:55:18 +00001242 */
XiaokangQianed582dd2022-04-13 08:21:05 +00001243
1244#define SSL_CLIENT_HELLO_OK 0
1245#define SSL_CLIENT_HELLO_HRR_REQUIRED 1
Ronald Cron5af4c7f2023-03-07 20:46:59 +01001246#define SSL_CLIENT_HELLO_TLS1_2 2
XiaokangQianed582dd2022-04-13 08:21:05 +00001247
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001248MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01001249static int ssl_tls13_parse_client_hello(mbedtls_ssl_context *ssl,
1250 const unsigned char *buf,
1251 const unsigned char *end)
XiaokangQian7807f9f2022-02-15 10:04:37 +00001252{
XiaokangQianb67384d2022-04-19 00:02:38 +00001253 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1254 const unsigned char *p = buf;
Ronald Crond540d992023-03-07 09:41:48 +01001255 const unsigned char *random;
XiaokangQian4080a7f2022-04-11 09:55:18 +00001256 size_t legacy_session_id_len;
Ronald Croncada4102023-03-07 09:51:39 +01001257 const unsigned char *legacy_session_id;
XiaokangQian318dc762022-04-20 09:43:51 +00001258 size_t cipher_suites_len;
Ronald Cron2f16b4e2023-03-07 10:07:32 +01001259 const unsigned char *cipher_suites;
XiaokangQian060d8672022-04-21 09:24:56 +00001260 const unsigned char *cipher_suites_end;
XiaokangQianb67384d2022-04-19 00:02:38 +00001261 size_t extensions_len;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001262 const unsigned char *extensions_end;
Ronald Croneff56732023-04-03 17:36:31 +02001263 const unsigned char *supported_versions_data;
1264 const unsigned char *supported_versions_data_end;
Jerry Yuc4bf5d62022-10-29 09:08:47 +08001265 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
Jerry Yu49ca9282022-05-05 11:05:22 +08001266 int hrr_required = 0;
Ronald Cron8a74f072023-06-14 17:59:29 +02001267 int no_usable_share_for_key_agreement = 0;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001268
Ronald Cron41a443a2022-10-04 16:38:25 +02001269#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED)
Ronald Cron79cdd412024-02-20 17:19:28 +01001270 int got_psk = 0;
1271 struct psk_attributes psk = PSK_ATTRIBUTES_INIT;
Jerry Yu29d9faa2022-08-23 17:52:45 +08001272 const unsigned char *pre_shared_key_ext = NULL;
Jerry Yu1c105562022-07-10 06:32:38 +00001273 const unsigned char *pre_shared_key_ext_end = NULL;
Ronald Cron41a443a2022-10-04 16:38:25 +02001274#endif
XiaokangQian7807f9f2022-02-15 10:04:37 +00001275
XiaokangQian7807f9f2022-02-15 10:04:37 +00001276 /*
XiaokangQianb67384d2022-04-19 00:02:38 +00001277 * ClientHello layout:
XiaokangQian7807f9f2022-02-15 10:04:37 +00001278 * 0 . 1 protocol version
XiaokangQiane8ff3502022-04-22 02:34:40 +00001279 * 2 . 33 random bytes
XiaokangQianc5763b52022-04-02 03:34:37 +00001280 * 34 . 34 session id length ( 1 byte )
XiaokangQian7807f9f2022-02-15 10:04:37 +00001281 * 35 . 34+x session id
XiaokangQian7807f9f2022-02-15 10:04:37 +00001282 * .. . .. ciphersuite list length ( 2 bytes )
1283 * .. . .. ciphersuite list
1284 * .. . .. compression alg. list length ( 1 byte )
1285 * .. . .. compression alg. list
1286 * .. . .. extensions length ( 2 bytes, optional )
1287 * .. . .. extensions ( optional )
1288 */
1289
XiaokangQianb67384d2022-04-19 00:02:38 +00001290 /*
bootstrap-prime6dbbf442022-05-17 19:30:44 -04001291 * Minimal length ( with everything empty and extensions omitted ) is
XiaokangQian7807f9f2022-02-15 10:04:37 +00001292 * 2 + 32 + 1 + 2 + 1 = 38 bytes. Check that first, so that we can
1293 * read at least up to session id length without worrying.
1294 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001295 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, 38);
XiaokangQian7807f9f2022-02-15 10:04:37 +00001296
1297 /* ...
1298 * ProtocolVersion legacy_version = 0x0303; // TLS 1.2
1299 * ...
1300 * with ProtocolVersion defined as:
1301 * uint16 ProtocolVersion;
1302 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001303 if (mbedtls_ssl_read_version(p, ssl->conf->transport) !=
1304 MBEDTLS_SSL_VERSION_TLS1_2) {
1305 MBEDTLS_SSL_DEBUG_MSG(1, ("Unsupported version of TLS."));
1306 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_PROTOCOL_VERSION,
1307 MBEDTLS_ERR_SSL_BAD_PROTOCOL_VERSION);
1308 return MBEDTLS_ERR_SSL_BAD_PROTOCOL_VERSION;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001309 }
1310 p += 2;
1311
Jerry Yuc1be19f2022-04-23 16:11:39 +08001312 /* ...
1313 * Random random;
1314 * ...
XiaokangQianb67384d2022-04-19 00:02:38 +00001315 * with Random defined as:
1316 * opaque Random[32];
XiaokangQian7807f9f2022-02-15 10:04:37 +00001317 */
Ronald Crond540d992023-03-07 09:41:48 +01001318 random = p;
XiaokangQian08037552022-04-20 07:16:41 +00001319 p += MBEDTLS_CLIENT_HELLO_RANDOM_LEN;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001320
Jerry Yuc1be19f2022-04-23 16:11:39 +08001321 /* ...
XiaokangQianb67384d2022-04-19 00:02:38 +00001322 * opaque legacy_session_id<0..32>;
Jerry Yuc1be19f2022-04-23 16:11:39 +08001323 * ...
XiaokangQian7807f9f2022-02-15 10:04:37 +00001324 */
Ronald Croncada4102023-03-07 09:51:39 +01001325 legacy_session_id_len = *(p++);
1326 legacy_session_id = p;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001327
XiaokangQianb67384d2022-04-19 00:02:38 +00001328 /*
1329 * Check we have enough data for the legacy session identifier
Jerry Yue95c8af2022-07-26 15:48:20 +08001330 * and the ciphersuite list length.
XiaokangQianb67384d2022-04-19 00:02:38 +00001331 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001332 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, legacy_session_id_len + 2);
XiaokangQian4080a7f2022-04-11 09:55:18 +00001333 p += legacy_session_id_len;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001334
Ronald Cron2f16b4e2023-03-07 10:07:32 +01001335 /* ...
1336 * CipherSuite cipher_suites<2..2^16-2>;
1337 * ...
1338 * with CipherSuite defined as:
1339 * uint8 CipherSuite[2];
1340 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001341 cipher_suites_len = MBEDTLS_GET_UINT16_BE(p, 0);
XiaokangQian7807f9f2022-02-15 10:04:37 +00001342 p += 2;
Ronald Cron2f16b4e2023-03-07 10:07:32 +01001343 cipher_suites = p;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001344
Ronald Cronfc7ae872023-02-16 15:32:19 +01001345 /*
1346 * The length of the ciphersuite list has to be even.
1347 */
1348 if (cipher_suites_len & 1) {
1349 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR,
1350 MBEDTLS_ERR_SSL_DECODE_ERROR);
1351 return MBEDTLS_ERR_SSL_DECODE_ERROR;
1352 }
1353
XiaokangQianb67384d2022-04-19 00:02:38 +00001354 /* Check we have enough data for the ciphersuite list, the legacy
1355 * compression methods and the length of the extensions.
Jerry Yue95c8af2022-07-26 15:48:20 +08001356 *
1357 * cipher_suites cipher_suites_len bytes
Waleed Elmelegya1c4f4c2024-06-25 18:16:16 +01001358 * legacy_compression_methods length 1 byte
XiaokangQianb67384d2022-04-19 00:02:38 +00001359 */
Waleed Elmelegya1c4f4c2024-06-25 18:16:16 +01001360 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, cipher_suites_len + 1);
Ronald Cron8c527d02023-03-07 15:47:47 +01001361 p += cipher_suites_len;
1362 cipher_suites_end = p;
Jerry Yu5725f1c2022-08-21 17:27:16 +08001363
Waleed Elmelegy39185982024-06-25 10:22:49 +01001364 /* Check if we have enough data for legacy_compression_methods
Waleed Elmelegya1c4f4c2024-06-25 18:16:16 +01001365 * and the length of the extensions (2 bytes).
Waleed Elmelegy566ed542024-06-19 15:09:48 +01001366 */
Waleed Elmelegya1c4f4c2024-06-25 18:16:16 +01001367 MBEDTLS_SSL_CHK_BUF_READ_PTR(p + 1, end, p[0] + 2);
Waleed Elmelegy41e0cdf2024-06-11 18:44:48 +01001368
Jerry Yu5725f1c2022-08-21 17:27:16 +08001369 /*
Ronald Cron8c527d02023-03-07 15:47:47 +01001370 * Search for the supported versions extension and parse it to determine
1371 * if the client supports TLS 1.3.
1372 */
1373 ret = mbedtls_ssl_tls13_is_supported_versions_ext_present_in_exts(
Waleed Elmelegy566ed542024-06-19 15:09:48 +01001374 ssl, p + 1 + p[0], end,
Ronald Croneff56732023-04-03 17:36:31 +02001375 &supported_versions_data, &supported_versions_data_end);
Ronald Cron8c527d02023-03-07 15:47:47 +01001376 if (ret < 0) {
1377 MBEDTLS_SSL_DEBUG_RET(1,
1378 ("mbedtls_ssl_tls13_is_supported_versions_ext_present_in_exts"), ret);
1379 return ret;
1380 }
1381
1382 if (ret == 0) {
Ronald Cron5af4c7f2023-03-07 20:46:59 +01001383 return SSL_CLIENT_HELLO_TLS1_2;
Ronald Cron8c527d02023-03-07 15:47:47 +01001384 }
1385
Ronald Cron5af4c7f2023-03-07 20:46:59 +01001386 if (ret == 1) {
1387 ret = ssl_tls13_parse_supported_versions_ext(ssl,
Ronald Croneff56732023-04-03 17:36:31 +02001388 supported_versions_data,
1389 supported_versions_data_end);
Ronald Cron5af4c7f2023-03-07 20:46:59 +01001390 if (ret < 0) {
1391 MBEDTLS_SSL_DEBUG_RET(1,
1392 ("ssl_tls13_parse_supported_versions_ext"), ret);
1393 return ret;
1394 }
1395
Ronald Cronb828c7d2023-04-03 16:37:22 +02001396 /*
1397 * The supported versions extension was parsed successfully as the
1398 * value returned by ssl_tls13_parse_supported_versions_ext() is
1399 * positive. The return value is then equal to
1400 * MBEDTLS_SSL_VERSION_TLS1_2 or MBEDTLS_SSL_VERSION_TLS1_3, defining
1401 * the TLS version to negotiate.
1402 */
Ronald Cron5af4c7f2023-03-07 20:46:59 +01001403 if (MBEDTLS_SSL_VERSION_TLS1_2 == ret) {
1404 return SSL_CLIENT_HELLO_TLS1_2;
1405 }
Ronald Cron8c527d02023-03-07 15:47:47 +01001406 }
1407
1408 /*
1409 * We negotiate TLS 1.3.
Ronald Cron64582392023-03-07 09:21:40 +01001410 */
1411 ssl->tls_version = MBEDTLS_SSL_VERSION_TLS1_3;
Ronald Cron64582392023-03-07 09:21:40 +01001412 ssl->session_negotiate->tls_version = MBEDTLS_SSL_VERSION_TLS1_3;
1413 ssl->session_negotiate->endpoint = ssl->conf->endpoint;
Ronald Cron64582392023-03-07 09:21:40 +01001414
Gilles Peskine57dbd692024-08-26 12:04:39 +02001415 /* Before doing any crypto, make sure we can. */
1416 ret = mbedtls_ssl_tls13_crypto_init(ssl);
1417 if (ret != 0) {
1418 return ret;
1419 }
1420
Ronald Cron64582392023-03-07 09:21:40 +01001421 /*
Ronald Crondad02b22023-04-06 09:57:52 +02001422 * We are negotiating the version 1.3 of the protocol. Do what we have
Ronald Croncada4102023-03-07 09:51:39 +01001423 * postponed: copy of the client random bytes, copy of the legacy session
Ronald Cron2f16b4e2023-03-07 10:07:32 +01001424 * identifier and selection of the TLS 1.3 cipher suite.
Ronald Crond540d992023-03-07 09:41:48 +01001425 */
1426 MBEDTLS_SSL_DEBUG_BUF(3, "client hello, random bytes",
1427 random, MBEDTLS_CLIENT_HELLO_RANDOM_LEN);
1428 memcpy(&handshake->randbytes[0], random, MBEDTLS_CLIENT_HELLO_RANDOM_LEN);
1429
Ronald Croncada4102023-03-07 09:51:39 +01001430 if (legacy_session_id_len > sizeof(ssl->session_negotiate->id)) {
1431 MBEDTLS_SSL_DEBUG_MSG(1, ("bad client hello message"));
1432 return MBEDTLS_ERR_SSL_DECODE_ERROR;
1433 }
1434 ssl->session_negotiate->id_len = legacy_session_id_len;
1435 MBEDTLS_SSL_DEBUG_BUF(3, "client hello, session id",
1436 legacy_session_id, legacy_session_id_len);
1437 memcpy(&ssl->session_negotiate->id[0],
1438 legacy_session_id, legacy_session_id_len);
1439
Ronald Crond540d992023-03-07 09:41:48 +01001440 /*
Jerry Yu5725f1c2022-08-21 17:27:16 +08001441 * Search for a matching ciphersuite
1442 */
Ronald Cron2f16b4e2023-03-07 10:07:32 +01001443 MBEDTLS_SSL_DEBUG_BUF(3, "client hello, list of cipher suites",
1444 cipher_suites, cipher_suites_len);
Jerry Yu5725f1c2022-08-21 17:27:16 +08001445
Ronald Cron89089cc2024-02-14 18:18:08 +01001446 ssl_tls13_select_ciphersuite(ssl, cipher_suites, cipher_suites_end,
1447 0, PSA_ALG_NONE, &handshake->ciphersuite_info);
Jerry Yu5725f1c2022-08-21 17:27:16 +08001448
Gilles Peskine449bd832023-01-11 14:50:10 +01001449 if (handshake->ciphersuite_info == NULL) {
1450 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE,
1451 MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE);
1452 return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
Jerry Yu5725f1c2022-08-21 17:27:16 +08001453 }
Ronald Cron89089cc2024-02-14 18:18:08 +01001454 ssl->session_negotiate->ciphersuite = handshake->ciphersuite_info->id;
1455
1456 MBEDTLS_SSL_DEBUG_MSG(2, ("selected ciphersuite: %04x - %s",
1457 ((unsigned) handshake->ciphersuite_info->id),
1458 handshake->ciphersuite_info->name));
Jerry Yuc1be19f2022-04-23 16:11:39 +08001459
XiaokangQian4080a7f2022-04-11 09:55:18 +00001460 /* ...
XiaokangQianb67384d2022-04-19 00:02:38 +00001461 * opaque legacy_compression_methods<1..2^8-1>;
XiaokangQian4080a7f2022-04-11 09:55:18 +00001462 * ...
XiaokangQian7807f9f2022-02-15 10:04:37 +00001463 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001464 if (p[0] != 1 || p[1] != MBEDTLS_SSL_COMPRESS_NULL) {
1465 MBEDTLS_SSL_DEBUG_MSG(1, ("bad legacy compression method"));
1466 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER,
1467 MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER);
1468 return MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001469 }
XiaokangQianb67384d2022-04-19 00:02:38 +00001470 p += 2;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001471
Jerry Yuc1be19f2022-04-23 16:11:39 +08001472 /* ...
XiaokangQianb67384d2022-04-19 00:02:38 +00001473 * Extension extensions<8..2^16-1>;
Jerry Yuc1be19f2022-04-23 16:11:39 +08001474 * ...
XiaokangQianb67384d2022-04-19 00:02:38 +00001475 * with Extension defined as:
1476 * struct {
1477 * ExtensionType extension_type;
1478 * opaque extension_data<0..2^16-1>;
1479 * } Extension;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001480 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001481 extensions_len = MBEDTLS_GET_UINT16_BE(p, 0);
XiaokangQian7807f9f2022-02-15 10:04:37 +00001482 p += 2;
Gilles Peskine449bd832023-01-11 14:50:10 +01001483 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, extensions_len);
XiaokangQiane8ff3502022-04-22 02:34:40 +00001484 extensions_end = p + extensions_len;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001485
Gilles Peskine449bd832023-01-11 14:50:10 +01001486 MBEDTLS_SSL_DEBUG_BUF(3, "client hello extensions", p, extensions_len);
Jerry Yu63a459c2022-10-31 13:38:40 +08001487 handshake->received_extensions = MBEDTLS_SSL_EXT_MASK_NONE;
Jerry Yu0c354a22022-08-29 15:25:36 +08001488
Gilles Peskine449bd832023-01-11 14:50:10 +01001489 while (p < extensions_end) {
XiaokangQian7807f9f2022-02-15 10:04:37 +00001490 unsigned int extension_type;
1491 size_t extension_data_len;
1492 const unsigned char *extension_data_end;
Jerry Yu263dbf72022-10-26 10:51:27 +08001493 uint32_t allowed_exts = MBEDTLS_SSL_TLS1_3_ALLOWED_EXTS_OF_CH;
1494
Ronald Cron5fbd2702024-02-14 10:03:36 +01001495 if (ssl->handshake->hello_retry_request_flag) {
Jerry Yu263dbf72022-10-26 10:51:27 +08001496 /* Do not accept early data extension in 2nd ClientHello */
1497 allowed_exts &= ~MBEDTLS_SSL_EXT_MASK(EARLY_DATA);
1498 }
XiaokangQian7807f9f2022-02-15 10:04:37 +00001499
Jerry Yu0c354a22022-08-29 15:25:36 +08001500 /* RFC 8446, section 4.2.11
Jerry Yu1c9247c2022-07-21 12:37:39 +08001501 *
1502 * The "pre_shared_key" extension MUST be the last extension in the
1503 * ClientHello (this facilitates implementation as described below).
1504 * Servers MUST check that it is the last extension and otherwise fail
1505 * the handshake with an "illegal_parameter" alert.
1506 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001507 if (handshake->received_extensions & MBEDTLS_SSL_EXT_MASK(PRE_SHARED_KEY)) {
Jerry Yu1c9247c2022-07-21 12:37:39 +08001508 MBEDTLS_SSL_DEBUG_MSG(
Gilles Peskine449bd832023-01-11 14:50:10 +01001509 3, ("pre_shared_key is not last extension."));
Jerry Yu1c9247c2022-07-21 12:37:39 +08001510 MBEDTLS_SSL_PEND_FATAL_ALERT(
1511 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER,
Gilles Peskine449bd832023-01-11 14:50:10 +01001512 MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER);
1513 return MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER;
Jerry Yu1c9247c2022-07-21 12:37:39 +08001514 }
1515
Gilles Peskine449bd832023-01-11 14:50:10 +01001516 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, extensions_end, 4);
1517 extension_type = MBEDTLS_GET_UINT16_BE(p, 0);
1518 extension_data_len = MBEDTLS_GET_UINT16_BE(p, 2);
XiaokangQian7807f9f2022-02-15 10:04:37 +00001519 p += 4;
1520
Gilles Peskine449bd832023-01-11 14:50:10 +01001521 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, extensions_end, extension_data_len);
XiaokangQian7807f9f2022-02-15 10:04:37 +00001522 extension_data_end = p + extension_data_len;
1523
Jerry Yuc4bf5d62022-10-29 09:08:47 +08001524 ret = mbedtls_ssl_tls13_check_received_extension(
Gilles Peskine449bd832023-01-11 14:50:10 +01001525 ssl, MBEDTLS_SSL_HS_CLIENT_HELLO, extension_type,
Jerry Yu263dbf72022-10-26 10:51:27 +08001526 allowed_exts);
Gilles Peskine449bd832023-01-11 14:50:10 +01001527 if (ret != 0) {
1528 return ret;
1529 }
Jerry Yue18dc7e2022-08-04 16:29:22 +08001530
Gilles Peskine449bd832023-01-11 14:50:10 +01001531 switch (extension_type) {
XiaokangQian40a35232022-05-07 09:02:40 +00001532#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
1533 case MBEDTLS_TLS_EXT_SERVERNAME:
Gilles Peskine449bd832023-01-11 14:50:10 +01001534 MBEDTLS_SSL_DEBUG_MSG(3, ("found ServerName extension"));
1535 ret = mbedtls_ssl_parse_server_name_ext(ssl, p,
1536 extension_data_end);
1537 if (ret != 0) {
XiaokangQian40a35232022-05-07 09:02:40 +00001538 MBEDTLS_SSL_DEBUG_RET(
Gilles Peskine449bd832023-01-11 14:50:10 +01001539 1, "mbedtls_ssl_parse_servername_ext", ret);
1540 return ret;
XiaokangQian40a35232022-05-07 09:02:40 +00001541 }
XiaokangQian40a35232022-05-07 09:02:40 +00001542 break;
1543#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
1544
Przemek Stekiel8c0a9532023-06-15 16:48:19 +02001545#if defined(PSA_WANT_ALG_ECDH) || defined(PSA_WANT_ALG_FFDH)
XiaokangQian7807f9f2022-02-15 10:04:37 +00001546 case MBEDTLS_TLS_EXT_SUPPORTED_GROUPS:
Gilles Peskine449bd832023-01-11 14:50:10 +01001547 MBEDTLS_SSL_DEBUG_MSG(3, ("found supported group extension"));
XiaokangQian7807f9f2022-02-15 10:04:37 +00001548
1549 /* Supported Groups Extension
1550 *
1551 * When sent by the client, the "supported_groups" extension
1552 * indicates the named groups which the client supports,
1553 * ordered from most preferred to least preferred.
1554 */
Jerry Yuc1be19f2022-04-23 16:11:39 +08001555 ret = ssl_tls13_parse_supported_groups_ext(
Gilles Peskine449bd832023-01-11 14:50:10 +01001556 ssl, p, extension_data_end);
1557 if (ret != 0) {
Xiaokang Qian9f1747b2023-03-30 02:50:04 +00001558 MBEDTLS_SSL_DEBUG_RET(
Xiaokang Qian8bce0e62023-04-04 10:15:48 +00001559 1, "ssl_tls13_parse_supported_groups_ext", ret);
Gilles Peskine449bd832023-01-11 14:50:10 +01001560 return ret;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001561 }
1562
XiaokangQian7807f9f2022-02-15 10:04:37 +00001563 break;
Przemek Stekiel8c0a9532023-06-15 16:48:19 +02001564#endif /* PSA_WANT_ALG_ECDH || PSA_WANT_ALG_FFDH*/
XiaokangQian7807f9f2022-02-15 10:04:37 +00001565
Valerio Settic9ae8622023-07-25 11:23:50 +02001566#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_EPHEMERAL_ENABLED)
XiaokangQian7807f9f2022-02-15 10:04:37 +00001567 case MBEDTLS_TLS_EXT_KEY_SHARE:
Gilles Peskine449bd832023-01-11 14:50:10 +01001568 MBEDTLS_SSL_DEBUG_MSG(3, ("found key share extension"));
XiaokangQian7807f9f2022-02-15 10:04:37 +00001569
1570 /*
1571 * Key Share Extension
1572 *
1573 * When sent by the client, the "key_share" extension
1574 * contains the endpoint's cryptographic parameters for
1575 * ECDHE/DHE key establishment methods.
1576 */
Jerry Yuc1be19f2022-04-23 16:11:39 +08001577 ret = ssl_tls13_parse_key_shares_ext(
Gilles Peskine449bd832023-01-11 14:50:10 +01001578 ssl, p, extension_data_end);
1579 if (ret == SSL_TLS1_3_PARSE_KEY_SHARES_EXT_NO_MATCH) {
Ronald Cron8a74f072023-06-14 17:59:29 +02001580 MBEDTLS_SSL_DEBUG_MSG(2, ("No usable share for key agreement."));
1581 no_usable_share_for_key_agreement = 1;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001582 }
1583
Gilles Peskine449bd832023-01-11 14:50:10 +01001584 if (ret < 0) {
Jerry Yu582dd062022-04-22 21:59:01 +08001585 MBEDTLS_SSL_DEBUG_RET(
Gilles Peskine449bd832023-01-11 14:50:10 +01001586 1, "ssl_tls13_parse_key_shares_ext", ret);
1587 return ret;
Jerry Yu582dd062022-04-22 21:59:01 +08001588 }
XiaokangQian7807f9f2022-02-15 10:04:37 +00001589
XiaokangQian7807f9f2022-02-15 10:04:37 +00001590 break;
Valerio Settic9ae8622023-07-25 11:23:50 +02001591#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_EPHEMERAL_ENABLED */
XiaokangQian7807f9f2022-02-15 10:04:37 +00001592
1593 case MBEDTLS_TLS_EXT_SUPPORTED_VERSIONS:
Ronald Cron8c527d02023-03-07 15:47:47 +01001594 /* Already parsed */
XiaokangQian7807f9f2022-02-15 10:04:37 +00001595 break;
1596
Ronald Cron41a443a2022-10-04 16:38:25 +02001597#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED)
Jerry Yue19e3b92022-07-08 12:04:51 +00001598 case MBEDTLS_TLS_EXT_PSK_KEY_EXCHANGE_MODES:
Xiaokang Qian9f1747b2023-03-30 02:50:04 +00001599 MBEDTLS_SSL_DEBUG_MSG(
1600 3, ("found psk key exchange modes extension"));
Jerry Yue19e3b92022-07-08 12:04:51 +00001601
1602 ret = ssl_tls13_parse_key_exchange_modes_ext(
Gilles Peskine449bd832023-01-11 14:50:10 +01001603 ssl, p, extension_data_end);
1604 if (ret != 0) {
Jerry Yue19e3b92022-07-08 12:04:51 +00001605 MBEDTLS_SSL_DEBUG_RET(
Gilles Peskine449bd832023-01-11 14:50:10 +01001606 1, "ssl_tls13_parse_key_exchange_modes_ext", ret);
1607 return ret;
Jerry Yue19e3b92022-07-08 12:04:51 +00001608 }
1609
Jerry Yue19e3b92022-07-08 12:04:51 +00001610 break;
Ronald Cron41a443a2022-10-04 16:38:25 +02001611#endif
Jerry Yue19e3b92022-07-08 12:04:51 +00001612
Jerry Yu1c105562022-07-10 06:32:38 +00001613 case MBEDTLS_TLS_EXT_PRE_SHARED_KEY:
Gilles Peskine449bd832023-01-11 14:50:10 +01001614 MBEDTLS_SSL_DEBUG_MSG(3, ("found pre_shared_key extension"));
1615 if ((handshake->received_extensions &
1616 MBEDTLS_SSL_EXT_MASK(PSK_KEY_EXCHANGE_MODES)) == 0) {
Jerry Yu13ab81d2022-07-22 23:17:11 +08001617 MBEDTLS_SSL_PEND_FATAL_ALERT(
1618 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER,
Gilles Peskine449bd832023-01-11 14:50:10 +01001619 MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER);
1620 return MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER;
Jerry Yu13ab81d2022-07-22 23:17:11 +08001621 }
Ronald Cron41a443a2022-10-04 16:38:25 +02001622#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED)
Jerry Yu1c105562022-07-10 06:32:38 +00001623 /* Delay processing of the PSK identity once we have
1624 * found out which algorithms to use. We keep a pointer
1625 * to the buffer and the size for later processing.
1626 */
Jerry Yu29d9faa2022-08-23 17:52:45 +08001627 pre_shared_key_ext = p;
Jerry Yu1c105562022-07-10 06:32:38 +00001628 pre_shared_key_ext_end = extension_data_end;
Jerry Yue18dc7e2022-08-04 16:29:22 +08001629#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED */
Jerry Yu1c105562022-07-10 06:32:38 +00001630 break;
Jerry Yu1c105562022-07-10 06:32:38 +00001631
XiaokangQianacb39922022-06-17 10:18:48 +00001632#if defined(MBEDTLS_SSL_ALPN)
1633 case MBEDTLS_TLS_EXT_ALPN:
Gilles Peskine449bd832023-01-11 14:50:10 +01001634 MBEDTLS_SSL_DEBUG_MSG(3, ("found alpn extension"));
XiaokangQianacb39922022-06-17 10:18:48 +00001635
Gilles Peskine449bd832023-01-11 14:50:10 +01001636 ret = mbedtls_ssl_parse_alpn_ext(ssl, p, extension_data_end);
1637 if (ret != 0) {
XiaokangQianacb39922022-06-17 10:18:48 +00001638 MBEDTLS_SSL_DEBUG_RET(
Gilles Peskine449bd832023-01-11 14:50:10 +01001639 1, ("mbedtls_ssl_parse_alpn_ext"), ret);
1640 return ret;
XiaokangQianacb39922022-06-17 10:18:48 +00001641 }
XiaokangQianacb39922022-06-17 10:18:48 +00001642 break;
1643#endif /* MBEDTLS_SSL_ALPN */
1644
Ronald Cron928cbd32022-10-04 16:14:26 +02001645#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED)
XiaokangQian7807f9f2022-02-15 10:04:37 +00001646 case MBEDTLS_TLS_EXT_SIG_ALG:
Gilles Peskine449bd832023-01-11 14:50:10 +01001647 MBEDTLS_SSL_DEBUG_MSG(3, ("found signature_algorithms extension"));
XiaokangQian7807f9f2022-02-15 10:04:37 +00001648
Gabor Mezei078e8032022-04-27 21:17:56 +02001649 ret = mbedtls_ssl_parse_sig_alg_ext(
Gilles Peskine449bd832023-01-11 14:50:10 +01001650 ssl, p, extension_data_end);
1651 if (ret != 0) {
Xiaokang Qian49f39c12023-04-06 02:31:02 +00001652 MBEDTLS_SSL_DEBUG_RET(
1653 1, "mbedtls_ssl_parse_sig_alg_ext", ret);
Gilles Peskine449bd832023-01-11 14:50:10 +01001654 return ret;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001655 }
XiaokangQian7807f9f2022-02-15 10:04:37 +00001656 break;
Ronald Cron928cbd32022-10-04 16:14:26 +02001657#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED */
XiaokangQian7807f9f2022-02-15 10:04:37 +00001658
Jan Bruckner151f6422023-02-10 12:45:19 +01001659#if defined(MBEDTLS_SSL_RECORD_SIZE_LIMIT)
1660 case MBEDTLS_TLS_EXT_RECORD_SIZE_LIMIT:
1661 MBEDTLS_SSL_DEBUG_MSG(3, ("found record_size_limit extension"));
1662
Xiaokang Qian9f1747b2023-03-30 02:50:04 +00001663 ret = mbedtls_ssl_tls13_parse_record_size_limit_ext(
1664 ssl, p, extension_data_end);
Jan Brucknerf482dcc2023-03-15 09:09:06 +01001665 if (ret != 0) {
1666 MBEDTLS_SSL_DEBUG_RET(
1667 1, ("mbedtls_ssl_tls13_parse_record_size_limit_ext"), ret);
1668 return ret;
1669 }
Jan Bruckner151f6422023-02-10 12:45:19 +01001670 break;
1671#endif /* MBEDTLS_SSL_RECORD_SIZE_LIMIT */
1672
XiaokangQian7807f9f2022-02-15 10:04:37 +00001673 default:
Jerry Yu79aa7212022-11-08 21:30:21 +08001674 MBEDTLS_SSL_PRINT_EXT(
Jerry Yu63a459c2022-10-31 13:38:40 +08001675 3, MBEDTLS_SSL_HS_CLIENT_HELLO,
Gilles Peskine449bd832023-01-11 14:50:10 +01001676 extension_type, "( ignored )");
Jerry Yu0c354a22022-08-29 15:25:36 +08001677 break;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001678 }
1679
1680 p += extension_data_len;
1681 }
1682
Gilles Peskine449bd832023-01-11 14:50:10 +01001683 MBEDTLS_SSL_PRINT_EXTS(3, MBEDTLS_SSL_HS_CLIENT_HELLO,
1684 handshake->received_extensions);
Jerry Yue18dc7e2022-08-04 16:29:22 +08001685
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +01001686 ret = mbedtls_ssl_add_hs_hdr_to_checksum(ssl,
1687 MBEDTLS_SSL_HS_CLIENT_HELLO,
1688 p - buf);
1689 if (0 != ret) {
1690 MBEDTLS_SSL_DEBUG_RET(1, ("mbedtls_ssl_add_hs_hdr_to_checksum"), ret);
1691 return ret;
1692 }
Jerry Yu032b15ce2022-07-11 06:10:03 +00001693
Ronald Cron41a443a2022-10-04 16:38:25 +02001694#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED)
XiaokangQian7807f9f2022-02-15 10:04:37 +00001695 /* Update checksum with either
1696 * - The entire content of the CH message, if no PSK extension is present
1697 * - The content up to but excluding the PSK extension, if present.
Ronald Cron7cab4f82024-03-07 15:09:09 +01001698 * Always parse the pre-shared-key extension when present in the
Ronald Cron12e72f12024-02-14 15:49:38 +01001699 * ClientHello even if some pre-requisites for PSK key exchange modes are
1700 * not met. That way we always validate the syntax of the extension.
XiaokangQian7807f9f2022-02-15 10:04:37 +00001701 */
Ronald Cron12e72f12024-02-14 15:49:38 +01001702 if (handshake->received_extensions & MBEDTLS_SSL_EXT_MASK(PRE_SHARED_KEY)) {
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +01001703 ret = handshake->update_checksum(ssl, buf,
1704 pre_shared_key_ext - buf);
1705 if (0 != ret) {
1706 MBEDTLS_SSL_DEBUG_RET(1, ("update_checksum"), ret);
1707 return ret;
1708 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001709 ret = ssl_tls13_parse_pre_shared_key_ext(ssl,
1710 pre_shared_key_ext,
1711 pre_shared_key_ext_end,
1712 cipher_suites,
Ronald Cron79cdd412024-02-20 17:19:28 +01001713 cipher_suites_end,
1714 &psk);
1715 if (ret == 0) {
1716 got_psk = 1;
1717 } else if (ret != MBEDTLS_ERR_SSL_UNKNOWN_IDENTITY) {
Jerry Yu63a459c2022-10-31 13:38:40 +08001718 MBEDTLS_SSL_DEBUG_RET(
Gilles Peskine449bd832023-01-11 14:50:10 +01001719 1, "ssl_tls13_parse_pre_shared_key_ext", ret);
1720 return ret;
Jerry Yu1c105562022-07-10 06:32:38 +00001721 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001722 } else
Ronald Cron41a443a2022-10-04 16:38:25 +02001723#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED */
Jerry Yu1c105562022-07-10 06:32:38 +00001724 {
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +01001725 ret = handshake->update_checksum(ssl, buf, p - buf);
1726 if (0 != ret) {
1727 MBEDTLS_SSL_DEBUG_RET(1, ("update_checksum"), ret);
1728 return ret;
1729 }
Jerry Yu1c105562022-07-10 06:32:38 +00001730 }
XiaokangQian7807f9f2022-02-15 10:04:37 +00001731
Ronald Cron79cdd412024-02-20 17:19:28 +01001732 /*
1733 * Determine the key exchange algorithm to use.
1734 * There are three types of key exchanges supported in TLS 1.3:
1735 * - (EC)DH with ECDSA,
1736 * - (EC)DH with PSK,
1737 * - plain PSK.
1738 *
1739 * The PSK-based key exchanges may additionally be used with 0-RTT.
1740 *
1741 * Our built-in order of preference is
1742 * 1 ) (EC)DHE-PSK Mode ( psk_ephemeral )
1743 * 2 ) Certificate Mode ( ephemeral )
1744 * 3 ) Plain PSK Mode ( psk )
1745 */
1746#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED)
1747 if (got_psk && (psk.key_exchange_mode ==
1748 MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK_EPHEMERAL)) {
1749 handshake->key_exchange_mode =
1750 MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK_EPHEMERAL;
1751 MBEDTLS_SSL_DEBUG_MSG(2, ("key exchange mode: psk_ephemeral"));
1752
1753 } else
1754#endif
1755 if (ssl_tls13_key_exchange_is_ephemeral_available(ssl)) {
1756 handshake->key_exchange_mode =
1757 MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL;
1758 MBEDTLS_SSL_DEBUG_MSG(2, ("key exchange mode: ephemeral"));
1759
1760 }
1761#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED)
1762 else if (got_psk && (psk.key_exchange_mode ==
1763 MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK)) {
1764 handshake->key_exchange_mode = MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK;
1765 MBEDTLS_SSL_DEBUG_MSG(2, ("key exchange mode: psk"));
1766 }
1767#endif
1768 else {
1769 MBEDTLS_SSL_DEBUG_MSG(
1770 1,
1771 ("ClientHello message misses mandatory extensions."));
1772 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_MISSING_EXTENSION,
1773 MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER);
1774 return MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER;
Gilles Peskine449bd832023-01-11 14:50:10 +01001775 }
XiaokangQian7807f9f2022-02-15 10:04:37 +00001776
Ronald Cron3e47eec2024-02-21 10:29:24 +01001777#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED)
Ronald Cron74a16292024-02-23 17:07:41 +01001778 if (handshake->key_exchange_mode &
1779 MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK_ALL) {
1780 handshake->ciphersuite_info = psk.ciphersuite_info;
1781 ssl->session_negotiate->ciphersuite = psk.ciphersuite_info->id;
1782
1783 MBEDTLS_SSL_DEBUG_MSG(2, ("Select PSK ciphersuite: %04x - %s",
1784 ((unsigned) psk.ciphersuite_info->id),
1785 psk.ciphersuite_info->name));
1786
1787 if (psk.type == MBEDTLS_SSL_TLS1_3_PSK_RESUMPTION) {
1788 handshake->resume = 1;
1789 }
Ronald Cron79cdd412024-02-20 17:19:28 +01001790 }
Ronald Cron3e47eec2024-02-21 10:29:24 +01001791#endif
Ronald Cron79cdd412024-02-20 17:19:28 +01001792
1793 if (handshake->key_exchange_mode !=
Ronald Cron8a74f072023-06-14 17:59:29 +02001794 MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK) {
1795 hrr_required = (no_usable_share_for_key_agreement != 0);
1796 }
1797
Gilles Peskine449bd832023-01-11 14:50:10 +01001798 mbedtls_ssl_optimize_checksum(ssl, handshake->ciphersuite_info);
Jerry Yue5834fd2022-08-29 20:16:09 +08001799
Gilles Peskine449bd832023-01-11 14:50:10 +01001800 return hrr_required ? SSL_CLIENT_HELLO_HRR_REQUIRED : SSL_CLIENT_HELLO_OK;
XiaokangQian75fe8c72022-06-15 09:42:45 +00001801}
1802
Jerry Yuab0da372023-02-08 13:55:24 +08001803#if defined(MBEDTLS_SSL_EARLY_DATA)
Jerry Yu4caf3ca2023-11-15 16:13:47 +08001804static int ssl_tls13_check_early_data_requirements(mbedtls_ssl_context *ssl)
Jerry Yuab0da372023-02-08 13:55:24 +08001805{
1806 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
1807
Jerry Yu985c9672022-12-04 14:06:30 +08001808 if (ssl->conf->early_data_enabled == MBEDTLS_SSL_EARLY_DATA_DISABLED) {
1809 MBEDTLS_SSL_DEBUG_MSG(
Jerry Yu985c9672022-12-04 14:06:30 +08001810 1,
Jerry Yu454dda32023-10-31 15:13:54 +08001811 ("EarlyData: rejected, feature disabled in server configuration."));
Jerry Yu4caf3ca2023-11-15 16:13:47 +08001812 return -1;
Ronald Cron7b6ee942024-01-12 10:29:55 +01001813 }
1814
Jerry Yu454dda32023-10-31 15:13:54 +08001815 if (!handshake->resume) {
1816 /* We currently support early data only in the case of PSKs established
1817 via a NewSessionTicket message thus in the case of a session
1818 resumption. */
Jerry Yu985c9672022-12-04 14:06:30 +08001819 MBEDTLS_SSL_DEBUG_MSG(
Jerry Yu7ef9fd82023-11-07 14:30:38 +08001820 1, ("EarlyData: rejected, not a session resumption."));
Jerry Yu4caf3ca2023-11-15 16:13:47 +08001821 return -1;
Jerry Yu985c9672022-12-04 14:06:30 +08001822 }
1823
Jerry Yu82fd6c12023-10-31 16:32:19 +08001824 /* RFC 8446 4.2.10
1825 *
1826 * In order to accept early data, the server MUST have accepted a PSK cipher
1827 * suite and selected the first key offered in the client's "pre_shared_key"
1828 * extension. In addition, it MUST verify that the following values are the
1829 * same as those associated with the selected PSK:
1830 * - The TLS version number
1831 * - The selected cipher suite
1832 * - The selected ALPN [RFC7301] protocol, if any
1833 *
1834 * NOTE:
Jerry Yu7ef9fd82023-11-07 14:30:38 +08001835 * - The TLS version number is checked in
1836 * ssl_tls13_offered_psks_check_identity_match_ticket().
Jerry Yu82fd6c12023-10-31 16:32:19 +08001837 */
1838
1839 if (handshake->selected_identity != 0) {
1840 MBEDTLS_SSL_DEBUG_MSG(
Jerry Yu7ef9fd82023-11-07 14:30:38 +08001841 1, ("EarlyData: rejected, the selected key in "
1842 "`pre_shared_key` is not the first one."));
Jerry Yu4caf3ca2023-11-15 16:13:47 +08001843 return -1;
Jerry Yu82fd6c12023-10-31 16:32:19 +08001844 }
1845
1846 if (handshake->ciphersuite_info->id !=
1847 ssl->session_negotiate->ciphersuite) {
1848 MBEDTLS_SSL_DEBUG_MSG(
Jerry Yu7ef9fd82023-11-07 14:30:38 +08001849 1, ("EarlyData: rejected, the selected ciphersuite is not the one "
1850 "of the selected pre-shared key."));
Jerry Yu4caf3ca2023-11-15 16:13:47 +08001851 return -1;
Jerry Yu82fd6c12023-10-31 16:32:19 +08001852
1853 }
Jerry Yu985c9672022-12-04 14:06:30 +08001854
Pengyu Lv94a42cc2023-12-06 10:04:17 +08001855 if (!mbedtls_ssl_tls13_session_ticket_allow_early_data(ssl->session_negotiate)) {
Jerry Yufceddb32022-12-12 15:30:34 +08001856 MBEDTLS_SSL_DEBUG_MSG(
1857 1,
Jerry Yuea96ac32023-11-21 17:06:36 +08001858 ("EarlyData: rejected, early_data not allowed in ticket "
1859 "permission bits."));
Jerry Yu4caf3ca2023-11-15 16:13:47 +08001860 return -1;
Jerry Yufceddb32022-12-12 15:30:34 +08001861 }
Jerry Yu985c9672022-12-04 14:06:30 +08001862
Waleed Elmelegy4dfb0e72024-03-14 01:48:40 +00001863#if defined(MBEDTLS_SSL_ALPN)
1864 const char *alpn = mbedtls_ssl_get_alpn_protocol(ssl);
1865 size_t alpn_len;
1866
1867 if (alpn == NULL && ssl->session_negotiate->ticket_alpn == NULL) {
1868 return 0;
1869 }
1870
1871 if (alpn != NULL) {
1872 alpn_len = strlen(alpn);
1873 }
1874
1875 if (alpn == NULL ||
1876 ssl->session_negotiate->ticket_alpn == NULL ||
1877 alpn_len != strlen(ssl->session_negotiate->ticket_alpn) ||
1878 (memcmp(alpn, ssl->session_negotiate->ticket_alpn, alpn_len) != 0)) {
1879 MBEDTLS_SSL_DEBUG_MSG(1, ("EarlyData: rejected, the selected ALPN is different "
1880 "from the one associated with the pre-shared key."));
1881 return -1;
1882 }
1883#endif
1884
Jerry Yu4caf3ca2023-11-15 16:13:47 +08001885 return 0;
Jerry Yuab0da372023-02-08 13:55:24 +08001886}
1887#endif /* MBEDTLS_SSL_EARLY_DATA */
1888
XiaokangQian75fe8c72022-06-15 09:42:45 +00001889/* Update the handshake state machine */
1890
Ronald Cronce7d76e2022-07-08 18:56:49 +02001891MBEDTLS_CHECK_RETURN_CRITICAL
Ronald Cron7b6ee942024-01-12 10:29:55 +01001892static int ssl_tls13_postprocess_client_hello(mbedtls_ssl_context *ssl,
1893 int hrr_required)
XiaokangQian75fe8c72022-06-15 09:42:45 +00001894{
1895 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1896
XiaokangQian7807f9f2022-02-15 10:04:37 +00001897 /*
XiaokangQianfb665a82022-06-15 03:57:21 +00001898 * Server certificate selection
1899 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001900 if (ssl->conf->f_cert_cb && (ret = ssl->conf->f_cert_cb(ssl)) != 0) {
1901 MBEDTLS_SSL_DEBUG_RET(1, "f_cert_cb", ret);
1902 return ret;
XiaokangQianfb665a82022-06-15 03:57:21 +00001903 }
1904#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
1905 ssl->handshake->sni_name = NULL;
1906 ssl->handshake->sni_name_len = 0;
1907#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
XiaokangQian7807f9f2022-02-15 10:04:37 +00001908
Gilles Peskine449bd832023-01-11 14:50:10 +01001909 ret = mbedtls_ssl_tls13_key_schedule_stage_early(ssl);
1910 if (ret != 0) {
1911 MBEDTLS_SSL_DEBUG_RET(1,
1912 "mbedtls_ssl_tls1_3_key_schedule_stage_early", ret);
1913 return ret;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001914 }
1915
Jerry Yuab0da372023-02-08 13:55:24 +08001916#if defined(MBEDTLS_SSL_EARLY_DATA)
Jerry Yu4caf3ca2023-11-15 16:13:47 +08001917 if (ssl->handshake->received_extensions & MBEDTLS_SSL_EXT_MASK(EARLY_DATA)) {
Ronald Cron31e2d832024-02-05 16:45:57 +01001918 ssl->handshake->early_data_accepted =
1919 (!hrr_required) && (ssl_tls13_check_early_data_requirements(ssl) == 0);
Jerry Yu4e9b70e2022-12-04 14:08:02 +08001920
Jerry Yu4caf3ca2023-11-15 16:13:47 +08001921 if (ssl->handshake->early_data_accepted) {
1922 ret = mbedtls_ssl_tls13_compute_early_transform(ssl);
1923 if (ret != 0) {
1924 MBEDTLS_SSL_DEBUG_RET(
1925 1, "mbedtls_ssl_tls13_compute_early_transform", ret);
1926 return ret;
1927 }
1928 } else {
1929 ssl->discard_early_data_record =
1930 hrr_required ?
1931 MBEDTLS_SSL_EARLY_DATA_DISCARD :
1932 MBEDTLS_SSL_EARLY_DATA_TRY_TO_DEPROTECT_AND_DISCARD;
Jerry Yu4e9b70e2022-12-04 14:08:02 +08001933 }
1934 }
Ronald Cron7b6ee942024-01-12 10:29:55 +01001935#else
1936 ((void) hrr_required);
Jerry Yuab0da372023-02-08 13:55:24 +08001937#endif /* MBEDTLS_SSL_EARLY_DATA */
1938
Gilles Peskine449bd832023-01-11 14:50:10 +01001939 return 0;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001940}
1941
1942/*
XiaokangQianed582dd2022-04-13 08:21:05 +00001943 * Main entry point from the state machine; orchestrates the otherfunctions.
1944 */
1945
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001946MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01001947static int ssl_tls13_process_client_hello(mbedtls_ssl_context *ssl)
XiaokangQianed582dd2022-04-13 08:21:05 +00001948{
1949
XiaokangQian08037552022-04-20 07:16:41 +00001950 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine449bd832023-01-11 14:50:10 +01001951 unsigned char *buf = NULL;
XiaokangQianed582dd2022-04-13 08:21:05 +00001952 size_t buflen = 0;
Jerry Yu4ca91402022-05-09 15:50:57 +08001953 int parse_client_hello_ret;
1954
Gilles Peskine449bd832023-01-11 14:50:10 +01001955 MBEDTLS_SSL_DEBUG_MSG(2, ("=> parse client hello"));
XiaokangQianed582dd2022-04-13 08:21:05 +00001956
Gilles Peskine449bd832023-01-11 14:50:10 +01001957 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_tls13_fetch_handshake_msg(
1958 ssl, MBEDTLS_SSL_HS_CLIENT_HELLO,
1959 &buf, &buflen));
XiaokangQianed582dd2022-04-13 08:21:05 +00001960
Gilles Peskine449bd832023-01-11 14:50:10 +01001961 MBEDTLS_SSL_PROC_CHK_NEG(ssl_tls13_parse_client_hello(ssl, buf,
1962 buf + buflen));
Ronald Cron5af4c7f2023-03-07 20:46:59 +01001963 parse_client_hello_ret = ret; /* Store positive return value of
1964 * parse_client_hello,
1965 * as negative error codes are handled
Jerry Yuf41553b2022-05-09 22:20:30 +08001966 * by MBEDTLS_SSL_PROC_CHK_NEG. */
Jerry Yu4ca91402022-05-09 15:50:57 +08001967
Ronald Cronb828c7d2023-04-03 16:37:22 +02001968 /*
Yanray Wang90acdc62023-12-08 10:29:42 +08001969 * Version 1.2 of the protocol has to be used for the handshake.
1970 * If TLS 1.2 is not supported, abort the handshake. Otherwise, set the
Ronald Cronb828c7d2023-04-03 16:37:22 +02001971 * ssl->keep_current_message flag for the ClientHello to be kept and parsed
1972 * as a TLS 1.2 ClientHello. We also change ssl->tls_version to
1973 * MBEDTLS_SSL_VERSION_TLS1_2 thus from now on mbedtls_ssl_handshake_step()
1974 * will dispatch to the TLS 1.2 state machine.
1975 */
Ronald Cron5af4c7f2023-03-07 20:46:59 +01001976 if (SSL_CLIENT_HELLO_TLS1_2 == parse_client_hello_ret) {
Yanray Wangfb0f47b2023-12-04 15:27:28 +08001977 /* Check if server supports TLS 1.2 */
Yanray Wang408ba6f2023-12-08 10:18:03 +08001978 if (!mbedtls_ssl_conf_is_tls12_enabled(ssl->conf)) {
Yanray Wangfb0f47b2023-12-04 15:27:28 +08001979 MBEDTLS_SSL_DEBUG_MSG(
Yanray Wang177e49a2023-12-08 10:51:04 +08001980 1, ("TLS 1.2 not supported."));
Yanray Wangfb0f47b2023-12-04 15:27:28 +08001981 MBEDTLS_SSL_PEND_FATAL_ALERT(
Yanray Wang2bef9172023-12-08 10:21:53 +08001982 MBEDTLS_SSL_ALERT_MSG_PROTOCOL_VERSION,
1983 MBEDTLS_ERR_SSL_BAD_PROTOCOL_VERSION);
1984 return MBEDTLS_ERR_SSL_BAD_PROTOCOL_VERSION;
Yanray Wangfb0f47b2023-12-04 15:27:28 +08001985 }
Ronald Cron5af4c7f2023-03-07 20:46:59 +01001986 ssl->keep_current_message = 1;
1987 ssl->tls_version = MBEDTLS_SSL_VERSION_TLS1_2;
1988 return 0;
1989 }
1990
Ronald Cron7b6ee942024-01-12 10:29:55 +01001991 MBEDTLS_SSL_PROC_CHK(
1992 ssl_tls13_postprocess_client_hello(ssl, parse_client_hello_ret ==
1993 SSL_CLIENT_HELLO_HRR_REQUIRED));
Jerry Yu582dd062022-04-22 21:59:01 +08001994
Ronald Cron5af4c7f2023-03-07 20:46:59 +01001995 if (SSL_CLIENT_HELLO_OK == parse_client_hello_ret) {
Gilles Peskine449bd832023-01-11 14:50:10 +01001996 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_SERVER_HELLO);
1997 } else {
1998 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_HELLO_RETRY_REQUEST);
1999 }
XiaokangQianed582dd2022-04-13 08:21:05 +00002000
2001cleanup:
2002
Gilles Peskine449bd832023-01-11 14:50:10 +01002003 MBEDTLS_SSL_DEBUG_MSG(2, ("<= parse client hello"));
2004 return ret;
XiaokangQianed582dd2022-04-13 08:21:05 +00002005}
2006
2007/*
Jerry Yu1c3e6882022-04-20 21:23:40 +08002008 * Handler for MBEDTLS_SSL_SERVER_HELLO
Jerry Yu5b64ae92022-03-30 17:15:02 +08002009 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002010MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002011static int ssl_tls13_prepare_server_hello(mbedtls_ssl_context *ssl)
Jerry Yuf4b27e42022-03-30 17:32:21 +08002012{
Jerry Yu637a3f12022-04-20 21:37:58 +08002013 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
2014 unsigned char *server_randbytes =
Gilles Peskine449bd832023-01-11 14:50:10 +01002015 ssl->handshake->randbytes + MBEDTLS_CLIENT_HELLO_RANDOM_LEN;
Jerry Yuf4b27e42022-03-30 17:32:21 +08002016
Gilles Peskine449bd832023-01-11 14:50:10 +01002017 if ((ret = ssl->conf->f_rng(ssl->conf->p_rng, server_randbytes,
2018 MBEDTLS_SERVER_HELLO_RANDOM_LEN)) != 0) {
2019 MBEDTLS_SSL_DEBUG_RET(1, "f_rng", ret);
2020 return ret;
Jerry Yuf4b27e42022-03-30 17:32:21 +08002021 }
2022
Gilles Peskine449bd832023-01-11 14:50:10 +01002023 MBEDTLS_SSL_DEBUG_BUF(3, "server hello, random bytes", server_randbytes,
2024 MBEDTLS_SERVER_HELLO_RANDOM_LEN);
Jerry Yuf4b27e42022-03-30 17:32:21 +08002025
2026#if defined(MBEDTLS_HAVE_TIME)
YxCda609132023-05-22 12:08:12 -07002027 ssl->session_negotiate->start = mbedtls_time(NULL);
Jerry Yuf4b27e42022-03-30 17:32:21 +08002028#endif /* MBEDTLS_HAVE_TIME */
2029
Gilles Peskine449bd832023-01-11 14:50:10 +01002030 return ret;
Jerry Yuf4b27e42022-03-30 17:32:21 +08002031}
2032
Jerry Yu3bf2c642022-03-30 22:02:12 +08002033/*
Jerry Yue74e04a2022-04-21 09:23:16 +08002034 * ssl_tls13_write_server_hello_supported_versions_ext ():
Jerry Yu3bf2c642022-03-30 22:02:12 +08002035 *
2036 * struct {
Jerry Yufb9f54d2022-04-06 10:08:34 +08002037 * ProtocolVersion selected_version;
Jerry Yu3bf2c642022-03-30 22:02:12 +08002038 * } SupportedVersions;
2039 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002040MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yue74e04a2022-04-21 09:23:16 +08002041static int ssl_tls13_write_server_hello_supported_versions_ext(
Gilles Peskine449bd832023-01-11 14:50:10 +01002042 mbedtls_ssl_context *ssl,
2043 unsigned char *buf,
2044 unsigned char *end,
2045 size_t *out_len)
Jerry Yu3bf2c642022-03-30 22:02:12 +08002046{
Jerry Yu3bf2c642022-03-30 22:02:12 +08002047 *out_len = 0;
2048
Gilles Peskine449bd832023-01-11 14:50:10 +01002049 MBEDTLS_SSL_DEBUG_MSG(3, ("server hello, write selected version"));
Jerry Yu3bf2c642022-03-30 22:02:12 +08002050
2051 /* Check if we have space to write the extension:
2052 * - extension_type (2 bytes)
2053 * - extension_data_length (2 bytes)
Jerry Yu349a6132022-04-14 20:52:56 +08002054 * - selected_version (2 bytes)
Jerry Yu3bf2c642022-03-30 22:02:12 +08002055 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002056 MBEDTLS_SSL_CHK_BUF_PTR(buf, end, 6);
Jerry Yu3bf2c642022-03-30 22:02:12 +08002057
Gilles Peskine449bd832023-01-11 14:50:10 +01002058 MBEDTLS_PUT_UINT16_BE(MBEDTLS_TLS_EXT_SUPPORTED_VERSIONS, buf, 0);
Jerry Yu3bf2c642022-03-30 22:02:12 +08002059
Gilles Peskine449bd832023-01-11 14:50:10 +01002060 MBEDTLS_PUT_UINT16_BE(2, buf, 2);
Jerry Yu3bf2c642022-03-30 22:02:12 +08002061
Gilles Peskine449bd832023-01-11 14:50:10 +01002062 mbedtls_ssl_write_version(buf + 4,
2063 ssl->conf->transport,
2064 ssl->tls_version);
Jerry Yu3bf2c642022-03-30 22:02:12 +08002065
Gilles Peskine449bd832023-01-11 14:50:10 +01002066 MBEDTLS_SSL_DEBUG_MSG(3, ("supported version: [%04x]",
2067 ssl->tls_version));
Jerry Yu3bf2c642022-03-30 22:02:12 +08002068
Jerry Yu349a6132022-04-14 20:52:56 +08002069 *out_len = 6;
Jerry Yu3bf2c642022-03-30 22:02:12 +08002070
Jerry Yub95dd362022-11-08 21:19:34 +08002071 mbedtls_ssl_tls13_set_hs_sent_ext_mask(
Gilles Peskine449bd832023-01-11 14:50:10 +01002072 ssl, MBEDTLS_TLS_EXT_SUPPORTED_VERSIONS);
Jerry Yub95dd362022-11-08 21:19:34 +08002073
Gilles Peskine449bd832023-01-11 14:50:10 +01002074 return 0;
Jerry Yu3bf2c642022-03-30 22:02:12 +08002075}
2076
Jerry Yud9436a12022-04-20 22:28:09 +08002077
Jerry Yu3bf2c642022-03-30 22:02:12 +08002078
2079/* Generate and export a single key share. For hybrid KEMs, this can
2080 * be called multiple times with the different components of the hybrid. */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002081MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002082static int ssl_tls13_generate_and_write_key_share(mbedtls_ssl_context *ssl,
2083 uint16_t named_group,
2084 unsigned char *buf,
2085 unsigned char *end,
2086 size_t *out_len)
Jerry Yu3bf2c642022-03-30 22:02:12 +08002087{
2088 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yu955ddd72022-04-22 22:27:33 +08002089
2090 *out_len = 0;
2091
Valerio Settic9ae8622023-07-25 11:23:50 +02002092#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_EPHEMERAL_ENABLED)
Przemek Stekiel29c219c2023-05-31 15:21:04 +02002093 if (mbedtls_ssl_tls13_named_group_is_ecdhe(named_group) ||
Przemek Stekield5f79e72023-06-29 09:08:43 +02002094 mbedtls_ssl_tls13_named_group_is_ffdh(named_group)) {
Przemek Stekiel408569f2023-07-06 11:26:44 +02002095 ret = mbedtls_ssl_tls13_generate_and_write_xxdh_key_exchange(
Gilles Peskine449bd832023-01-11 14:50:10 +01002096 ssl, named_group, buf, end, out_len);
2097 if (ret != 0) {
Jerry Yu89e103c2022-03-30 22:43:29 +08002098 MBEDTLS_SSL_DEBUG_RET(
Przemek Stekiel408569f2023-07-06 11:26:44 +02002099 1, "mbedtls_ssl_tls13_generate_and_write_xxdh_key_exchange",
Gilles Peskine449bd832023-01-11 14:50:10 +01002100 ret);
2101 return ret;
Jerry Yu3bf2c642022-03-30 22:02:12 +08002102 }
Gilles Peskine449bd832023-01-11 14:50:10 +01002103 } else
Valerio Settic9ae8622023-07-25 11:23:50 +02002104#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_EPHEMERAL_ENABLED */
Gilles Peskine449bd832023-01-11 14:50:10 +01002105 if (0 /* Other kinds of KEMs */) {
2106 } else {
Jerry Yu955ddd72022-04-22 22:27:33 +08002107 ((void) ssl);
2108 ((void) named_group);
2109 ((void) buf);
2110 ((void) end);
Jerry Yu3bf2c642022-03-30 22:02:12 +08002111 ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
2112 }
2113
Gilles Peskine449bd832023-01-11 14:50:10 +01002114 return ret;
Jerry Yu3bf2c642022-03-30 22:02:12 +08002115}
2116
2117/*
2118 * ssl_tls13_write_key_share_ext
2119 *
2120 * Structure of key_share extension in ServerHello:
2121 *
Jerry Yu1c3e6882022-04-20 21:23:40 +08002122 * struct {
2123 * NamedGroup group;
2124 * opaque key_exchange<1..2^16-1>;
2125 * } KeyShareEntry;
2126 * struct {
2127 * KeyShareEntry server_share;
2128 * } KeyShareServerHello;
Jerry Yu3bf2c642022-03-30 22:02:12 +08002129 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002130MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002131static int ssl_tls13_write_key_share_ext(mbedtls_ssl_context *ssl,
2132 unsigned char *buf,
2133 unsigned char *end,
2134 size_t *out_len)
Jerry Yu3bf2c642022-03-30 22:02:12 +08002135{
Jerry Yu955ddd72022-04-22 22:27:33 +08002136 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yu3bf2c642022-03-30 22:02:12 +08002137 unsigned char *p = buf;
Jerry Yu57d48412022-04-20 21:50:42 +08002138 uint16_t group = ssl->handshake->offered_group_id;
Jerry Yu3bf2c642022-03-30 22:02:12 +08002139 unsigned char *server_share = buf + 4;
Jerry Yu3bf2c642022-03-30 22:02:12 +08002140 size_t key_exchange_length;
Jerry Yu3bf2c642022-03-30 22:02:12 +08002141
2142 *out_len = 0;
2143
Gilles Peskine449bd832023-01-11 14:50:10 +01002144 MBEDTLS_SSL_DEBUG_MSG(3, ("server hello, adding key share extension"));
Jerry Yu3bf2c642022-03-30 22:02:12 +08002145
Gilles Peskine449bd832023-01-11 14:50:10 +01002146 MBEDTLS_SSL_DEBUG_MSG(2, ("server hello, write selected_group: %s (%04x)",
2147 mbedtls_ssl_named_group_to_str(group),
2148 group));
Jerry Yu58af2332022-09-06 11:19:31 +08002149
Jerry Yu3bf2c642022-03-30 22:02:12 +08002150 /* Check if we have space for header and length fields:
2151 * - extension_type (2 bytes)
2152 * - extension_data_length (2 bytes)
2153 * - group (2 bytes)
2154 * - key_exchange_length (2 bytes)
2155 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002156 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 8);
2157 MBEDTLS_PUT_UINT16_BE(MBEDTLS_TLS_EXT_KEY_SHARE, p, 0);
2158 MBEDTLS_PUT_UINT16_BE(group, server_share, 0);
Jerry Yu3bf2c642022-03-30 22:02:12 +08002159 p += 8;
Jerry Yu57d48412022-04-20 21:50:42 +08002160
Jerry Yu3bf2c642022-03-30 22:02:12 +08002161 /* When we introduce PQC-ECDHE hybrids, we'll want to call this
2162 * function multiple times. */
Jerry Yu955ddd72022-04-22 22:27:33 +08002163 ret = ssl_tls13_generate_and_write_key_share(
Gilles Peskine449bd832023-01-11 14:50:10 +01002164 ssl, group, server_share + 4, end, &key_exchange_length);
2165 if (ret != 0) {
2166 return ret;
2167 }
Jerry Yu3bf2c642022-03-30 22:02:12 +08002168 p += key_exchange_length;
Jerry Yuc1be19f2022-04-23 16:11:39 +08002169
Gilles Peskine449bd832023-01-11 14:50:10 +01002170 MBEDTLS_PUT_UINT16_BE(key_exchange_length, server_share + 2, 0);
Jerry Yu3bf2c642022-03-30 22:02:12 +08002171
Gilles Peskine449bd832023-01-11 14:50:10 +01002172 MBEDTLS_PUT_UINT16_BE(p - server_share, buf, 2);
Jerry Yu3bf2c642022-03-30 22:02:12 +08002173
Jerry Yu57d48412022-04-20 21:50:42 +08002174 *out_len = p - buf;
Jerry Yu955ddd72022-04-22 22:27:33 +08002175
Gilles Peskine449bd832023-01-11 14:50:10 +01002176 mbedtls_ssl_tls13_set_hs_sent_ext_mask(ssl, MBEDTLS_TLS_EXT_KEY_SHARE);
Jerry Yub95dd362022-11-08 21:19:34 +08002177
Gilles Peskine449bd832023-01-11 14:50:10 +01002178 return 0;
Jerry Yu3bf2c642022-03-30 22:02:12 +08002179}
Jerry Yud9436a12022-04-20 22:28:09 +08002180
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002181MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002182static int ssl_tls13_write_hrr_key_share_ext(mbedtls_ssl_context *ssl,
2183 unsigned char *buf,
2184 unsigned char *end,
2185 size_t *out_len)
Jerry Yu23f7a6f2022-04-23 15:16:45 +08002186{
2187 uint16_t selected_group = ssl->handshake->hrr_selected_group;
2188 /* key_share Extension
2189 *
2190 * struct {
2191 * select (Handshake.msg_type) {
2192 * ...
2193 * case hello_retry_request:
2194 * NamedGroup selected_group;
2195 * ...
2196 * };
2197 * } KeyShare;
2198 */
2199
2200 *out_len = 0;
2201
Jerry Yub0ac10b2022-05-05 11:10:08 +08002202 /*
2203 * For a pure PSK key exchange, there is no group to agree upon. The purpose
2204 * of the HRR is then to transmit a cookie to force the client to demonstrate
2205 * reachability at their apparent network address (primarily useful for DTLS).
2206 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002207 if (!mbedtls_ssl_tls13_key_exchange_mode_with_ephemeral(ssl)) {
2208 return 0;
2209 }
Jerry Yu23f7a6f2022-04-23 15:16:45 +08002210
2211 /* We should only send the key_share extension if the client's initial
2212 * key share was not acceptable. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002213 if (ssl->handshake->offered_group_id != 0) {
2214 MBEDTLS_SSL_DEBUG_MSG(4, ("Skip key_share extension in HRR"));
2215 return 0;
Jerry Yu23f7a6f2022-04-23 15:16:45 +08002216 }
2217
Gilles Peskine449bd832023-01-11 14:50:10 +01002218 if (selected_group == 0) {
2219 MBEDTLS_SSL_DEBUG_MSG(1, ("no matching named group found"));
2220 return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
Jerry Yu23f7a6f2022-04-23 15:16:45 +08002221 }
2222
Jerry Yub0ac10b2022-05-05 11:10:08 +08002223 /* Check if we have enough space:
2224 * - extension_type (2 bytes)
2225 * - extension_data_length (2 bytes)
2226 * - selected_group (2 bytes)
2227 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002228 MBEDTLS_SSL_CHK_BUF_PTR(buf, end, 6);
Jerry Yu23f7a6f2022-04-23 15:16:45 +08002229
Gilles Peskine449bd832023-01-11 14:50:10 +01002230 MBEDTLS_PUT_UINT16_BE(MBEDTLS_TLS_EXT_KEY_SHARE, buf, 0);
2231 MBEDTLS_PUT_UINT16_BE(2, buf, 2);
2232 MBEDTLS_PUT_UINT16_BE(selected_group, buf, 4);
Jerry Yu23f7a6f2022-04-23 15:16:45 +08002233
Gilles Peskine449bd832023-01-11 14:50:10 +01002234 MBEDTLS_SSL_DEBUG_MSG(3,
2235 ("HRR selected_group: %s (%x)",
2236 mbedtls_ssl_named_group_to_str(selected_group),
2237 selected_group));
Jerry Yu23f7a6f2022-04-23 15:16:45 +08002238
2239 *out_len = 6;
Jerry Yu23f7a6f2022-04-23 15:16:45 +08002240
Gilles Peskine449bd832023-01-11 14:50:10 +01002241 mbedtls_ssl_tls13_set_hs_sent_ext_mask(ssl, MBEDTLS_TLS_EXT_KEY_SHARE);
Jerry Yub95dd362022-11-08 21:19:34 +08002242
Gilles Peskine449bd832023-01-11 14:50:10 +01002243 return 0;
Jerry Yu23f7a6f2022-04-23 15:16:45 +08002244}
Jerry Yu3bf2c642022-03-30 22:02:12 +08002245
2246/*
2247 * Structure of ServerHello message:
2248 *
2249 * struct {
2250 * ProtocolVersion legacy_version = 0x0303; // TLS v1.2
2251 * Random random;
2252 * opaque legacy_session_id_echo<0..32>;
2253 * CipherSuite cipher_suite;
2254 * uint8 legacy_compression_method = 0;
2255 * Extension extensions<6..2^16-1>;
2256 * } ServerHello;
2257 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002258MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002259static int ssl_tls13_write_server_hello_body(mbedtls_ssl_context *ssl,
2260 unsigned char *buf,
2261 unsigned char *end,
2262 size_t *out_len,
2263 int is_hrr)
Jerry Yu56404d72022-03-30 17:36:13 +08002264{
Jerry Yu955ddd72022-04-22 22:27:33 +08002265 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yu3bf2c642022-03-30 22:02:12 +08002266 unsigned char *p = buf;
Jerry Yud9436a12022-04-20 22:28:09 +08002267 unsigned char *p_extensions_len;
Jerry Yufbe3e642022-04-25 19:31:51 +08002268 size_t output_len;
Jerry Yu3bf2c642022-03-30 22:02:12 +08002269
2270 *out_len = 0;
Jerry Yu50e00e32022-10-31 14:45:01 +08002271 ssl->handshake->sent_extensions = MBEDTLS_SSL_EXT_MASK_NONE;
Jerry Yu3bf2c642022-03-30 22:02:12 +08002272
Jerry Yucfc04b32022-04-21 09:31:58 +08002273 /* ...
2274 * ProtocolVersion legacy_version = 0x0303; // TLS 1.2
2275 * ...
2276 * with ProtocolVersion defined as:
2277 * uint16 ProtocolVersion;
Jerry Yu3bf2c642022-03-30 22:02:12 +08002278 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002279 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 2);
2280 MBEDTLS_PUT_UINT16_BE(0x0303, p, 0);
Jerry Yu3bf2c642022-03-30 22:02:12 +08002281 p += 2;
2282
Jerry Yu1c3e6882022-04-20 21:23:40 +08002283 /* ...
2284 * Random random;
2285 * ...
Jerry Yucfc04b32022-04-21 09:31:58 +08002286 * with Random defined as:
2287 * opaque Random[MBEDTLS_SERVER_HELLO_RANDOM_LEN];
Jerry Yu1c3e6882022-04-20 21:23:40 +08002288 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002289 MBEDTLS_SSL_CHK_BUF_PTR(p, end, MBEDTLS_SERVER_HELLO_RANDOM_LEN);
2290 if (is_hrr) {
2291 memcpy(p, mbedtls_ssl_tls13_hello_retry_request_magic,
2292 MBEDTLS_SERVER_HELLO_RANDOM_LEN);
2293 } else {
2294 memcpy(p, &ssl->handshake->randbytes[MBEDTLS_CLIENT_HELLO_RANDOM_LEN],
2295 MBEDTLS_SERVER_HELLO_RANDOM_LEN);
Jerry Yu23f7a6f2022-04-23 15:16:45 +08002296 }
Gilles Peskine449bd832023-01-11 14:50:10 +01002297 MBEDTLS_SSL_DEBUG_BUF(3, "server hello, random bytes",
2298 p, MBEDTLS_SERVER_HELLO_RANDOM_LEN);
Jerry Yu3bf2c642022-03-30 22:02:12 +08002299 p += MBEDTLS_SERVER_HELLO_RANDOM_LEN;
2300
Jerry Yucfc04b32022-04-21 09:31:58 +08002301 /* ...
2302 * opaque legacy_session_id_echo<0..32>;
2303 * ...
Jerry Yu3bf2c642022-03-30 22:02:12 +08002304 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002305 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 1 + ssl->session_negotiate->id_len);
2306 *p++ = (unsigned char) ssl->session_negotiate->id_len;
2307 if (ssl->session_negotiate->id_len > 0) {
2308 memcpy(p, &ssl->session_negotiate->id[0],
2309 ssl->session_negotiate->id_len);
Jerry Yu3bf2c642022-03-30 22:02:12 +08002310 p += ssl->session_negotiate->id_len;
Jerry Yu955ddd72022-04-22 22:27:33 +08002311
Gilles Peskine449bd832023-01-11 14:50:10 +01002312 MBEDTLS_SSL_DEBUG_BUF(3, "session id", ssl->session_negotiate->id,
2313 ssl->session_negotiate->id_len);
Jerry Yu3bf2c642022-03-30 22:02:12 +08002314 }
2315
Jerry Yucfc04b32022-04-21 09:31:58 +08002316 /* ...
2317 * CipherSuite cipher_suite;
2318 * ...
2319 * with CipherSuite defined as:
2320 * uint8 CipherSuite[2];
Jerry Yu3bf2c642022-03-30 22:02:12 +08002321 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002322 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 2);
2323 MBEDTLS_PUT_UINT16_BE(ssl->session_negotiate->ciphersuite, p, 0);
Jerry Yu3bf2c642022-03-30 22:02:12 +08002324 p += 2;
Gilles Peskine449bd832023-01-11 14:50:10 +01002325 MBEDTLS_SSL_DEBUG_MSG(3,
2326 ("server hello, chosen ciphersuite: %s ( id=%d )",
2327 mbedtls_ssl_get_ciphersuite_name(
2328 ssl->session_negotiate->ciphersuite),
2329 ssl->session_negotiate->ciphersuite));
Jerry Yu3bf2c642022-03-30 22:02:12 +08002330
Jerry Yucfc04b32022-04-21 09:31:58 +08002331 /* ...
2332 * uint8 legacy_compression_method = 0;
2333 * ...
2334 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002335 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 1);
Thomas Daubney31e03a82022-07-25 15:59:25 +01002336 *p++ = MBEDTLS_SSL_COMPRESS_NULL;
Jerry Yu3bf2c642022-03-30 22:02:12 +08002337
Jerry Yucfc04b32022-04-21 09:31:58 +08002338 /* ...
2339 * Extension extensions<6..2^16-1>;
2340 * ...
2341 * struct {
2342 * ExtensionType extension_type; (2 bytes)
2343 * opaque extension_data<0..2^16-1>;
2344 * } Extension;
2345 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002346 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 2);
Jerry Yud9436a12022-04-20 22:28:09 +08002347 p_extensions_len = p;
Jerry Yu3bf2c642022-03-30 22:02:12 +08002348 p += 2;
2349
Gilles Peskine449bd832023-01-11 14:50:10 +01002350 if ((ret = ssl_tls13_write_server_hello_supported_versions_ext(
2351 ssl, p, end, &output_len)) != 0) {
Jerry Yu955ddd72022-04-22 22:27:33 +08002352 MBEDTLS_SSL_DEBUG_RET(
Gilles Peskine449bd832023-01-11 14:50:10 +01002353 1, "ssl_tls13_write_server_hello_supported_versions_ext", ret);
2354 return ret;
Jerry Yu3bf2c642022-03-30 22:02:12 +08002355 }
2356 p += output_len;
2357
Gilles Peskine449bd832023-01-11 14:50:10 +01002358 if (mbedtls_ssl_tls13_key_exchange_mode_with_ephemeral(ssl)) {
2359 if (is_hrr) {
2360 ret = ssl_tls13_write_hrr_key_share_ext(ssl, p, end, &output_len);
2361 } else {
2362 ret = ssl_tls13_write_key_share_ext(ssl, p, end, &output_len);
2363 }
2364 if (ret != 0) {
2365 return ret;
2366 }
Jerry Yu3bf2c642022-03-30 22:02:12 +08002367 p += output_len;
2368 }
Jerry Yu3bf2c642022-03-30 22:02:12 +08002369
Ronald Cron41a443a2022-10-04 16:38:25 +02002370#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01002371 if (!is_hrr && mbedtls_ssl_tls13_key_exchange_mode_with_psk(ssl)) {
2372 ret = ssl_tls13_write_server_pre_shared_key_ext(ssl, p, end, &output_len);
2373 if (ret != 0) {
2374 MBEDTLS_SSL_DEBUG_RET(1, "ssl_tls13_write_server_pre_shared_key_ext",
2375 ret);
2376 return ret;
Jerry Yu032b15ce2022-07-11 06:10:03 +00002377 }
2378 p += output_len;
2379 }
Ronald Cron41a443a2022-10-04 16:38:25 +02002380#endif
Jerry Yu032b15ce2022-07-11 06:10:03 +00002381
Gilles Peskine449bd832023-01-11 14:50:10 +01002382 MBEDTLS_PUT_UINT16_BE(p - p_extensions_len - 2, p_extensions_len, 0);
Jerry Yu3bf2c642022-03-30 22:02:12 +08002383
Gilles Peskine449bd832023-01-11 14:50:10 +01002384 MBEDTLS_SSL_DEBUG_BUF(4, "server hello extensions",
2385 p_extensions_len, p - p_extensions_len);
Jerry Yu3bf2c642022-03-30 22:02:12 +08002386
Jerry Yud9436a12022-04-20 22:28:09 +08002387 *out_len = p - buf;
Jerry Yu3bf2c642022-03-30 22:02:12 +08002388
Gilles Peskine449bd832023-01-11 14:50:10 +01002389 MBEDTLS_SSL_DEBUG_BUF(3, "server hello", buf, *out_len);
Jerry Yu3bf2c642022-03-30 22:02:12 +08002390
Jerry Yu7de2ff02022-11-08 21:43:46 +08002391 MBEDTLS_SSL_PRINT_EXTS(
Jerry Yu4b8f2f72022-10-31 13:31:22 +08002392 3, is_hrr ? MBEDTLS_SSL_TLS1_3_HS_HELLO_RETRY_REQUEST :
Gilles Peskine449bd832023-01-11 14:50:10 +01002393 MBEDTLS_SSL_HS_SERVER_HELLO,
2394 ssl->handshake->sent_extensions);
Jerry Yu4b8f2f72022-10-31 13:31:22 +08002395
Gilles Peskine449bd832023-01-11 14:50:10 +01002396 return ret;
Jerry Yu56404d72022-03-30 17:36:13 +08002397}
2398
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002399MBEDTLS_CHECK_RETURN_CRITICAL
Xiaokang Qian934ce6f2023-02-06 10:23:04 +00002400static int ssl_tls13_finalize_server_hello(mbedtls_ssl_context *ssl)
Jerry Yue110d252022-05-05 10:19:22 +08002401{
2402 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine449bd832023-01-11 14:50:10 +01002403 ret = mbedtls_ssl_tls13_compute_handshake_transform(ssl);
2404 if (ret != 0) {
2405 MBEDTLS_SSL_DEBUG_RET(1,
2406 "mbedtls_ssl_tls13_compute_handshake_transform",
2407 ret);
2408 return ret;
Jerry Yue110d252022-05-05 10:19:22 +08002409 }
2410
Gilles Peskine449bd832023-01-11 14:50:10 +01002411 return ret;
Jerry Yue110d252022-05-05 10:19:22 +08002412}
2413
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002414MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002415static int ssl_tls13_write_server_hello(mbedtls_ssl_context *ssl)
Jerry Yu5b64ae92022-03-30 17:15:02 +08002416{
Jerry Yu637a3f12022-04-20 21:37:58 +08002417 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yuf4b27e42022-03-30 17:32:21 +08002418 unsigned char *buf;
2419 size_t buf_len, msg_len;
2420
Gilles Peskine449bd832023-01-11 14:50:10 +01002421 MBEDTLS_SSL_DEBUG_MSG(2, ("=> write server hello"));
Jerry Yuf4b27e42022-03-30 17:32:21 +08002422
Gilles Peskine449bd832023-01-11 14:50:10 +01002423 MBEDTLS_SSL_PROC_CHK(ssl_tls13_prepare_server_hello(ssl));
Jerry Yuf4b27e42022-03-30 17:32:21 +08002424
Xiaokang Qian9f1747b2023-03-30 02:50:04 +00002425 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_start_handshake_msg(
2426 ssl, MBEDTLS_SSL_HS_SERVER_HELLO, &buf, &buf_len));
Jerry Yuf4b27e42022-03-30 17:32:21 +08002427
Gilles Peskine449bd832023-01-11 14:50:10 +01002428 MBEDTLS_SSL_PROC_CHK(ssl_tls13_write_server_hello_body(ssl, buf,
2429 buf + buf_len,
2430 &msg_len,
2431 0));
Jerry Yuf4b27e42022-03-30 17:32:21 +08002432
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +01002433 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_add_hs_msg_to_checksum(
Manuel Pégourié-Gonnard43cc1272023-02-06 11:48:19 +01002434 ssl, MBEDTLS_SSL_HS_SERVER_HELLO, buf, msg_len));
Jerry Yuf4b27e42022-03-30 17:32:21 +08002435
Gilles Peskine449bd832023-01-11 14:50:10 +01002436 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_finish_handshake_msg(
2437 ssl, buf_len, msg_len));
Jerry Yu637a3f12022-04-20 21:37:58 +08002438
Xiaokang Qian934ce6f2023-02-06 10:23:04 +00002439 MBEDTLS_SSL_PROC_CHK(ssl_tls13_finalize_server_hello(ssl));
Jerry Yue110d252022-05-05 10:19:22 +08002440
Gabor Mezei7b39bf12022-05-24 16:04:14 +02002441#if defined(MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE)
2442 /* The server sends a dummy change_cipher_spec record immediately
2443 * after its first handshake message. This may either be after
2444 * a ServerHello or a HelloRetryRequest.
2445 */
Gabor Mezei96ae9262022-06-28 11:45:18 +02002446 mbedtls_ssl_handshake_set_state(
Gilles Peskine449bd832023-01-11 14:50:10 +01002447 ssl, MBEDTLS_SSL_SERVER_CCS_AFTER_SERVER_HELLO);
Gabor Mezei7b39bf12022-05-24 16:04:14 +02002448#else
Gilles Peskine449bd832023-01-11 14:50:10 +01002449 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_ENCRYPTED_EXTENSIONS);
Gabor Mezei7b39bf12022-05-24 16:04:14 +02002450#endif /* MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE */
Jerry Yue110d252022-05-05 10:19:22 +08002451
Jerry Yuf4b27e42022-03-30 17:32:21 +08002452cleanup:
2453
Gilles Peskine449bd832023-01-11 14:50:10 +01002454 MBEDTLS_SSL_DEBUG_MSG(2, ("<= write server hello"));
2455 return ret;
Jerry Yu5b64ae92022-03-30 17:15:02 +08002456}
2457
Jerry Yu23d1a252022-05-12 18:08:59 +08002458
2459/*
2460 * Handler for MBEDTLS_SSL_HELLO_RETRY_REQUEST
2461 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002462MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002463static int ssl_tls13_prepare_hello_retry_request(mbedtls_ssl_context *ssl)
Jerry Yu23d1a252022-05-12 18:08:59 +08002464{
2465 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Ronald Cron5fbd2702024-02-14 10:03:36 +01002466 if (ssl->handshake->hello_retry_request_flag) {
Gilles Peskine449bd832023-01-11 14:50:10 +01002467 MBEDTLS_SSL_DEBUG_MSG(1, ("Too many HRRs"));
2468 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE,
2469 MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE);
2470 return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
Jerry Yu23d1a252022-05-12 18:08:59 +08002471 }
2472
2473 /*
2474 * Create stateless transcript hash for HRR
2475 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002476 MBEDTLS_SSL_DEBUG_MSG(4, ("Reset transcript for HRR"));
2477 ret = mbedtls_ssl_reset_transcript_for_hrr(ssl);
2478 if (ret != 0) {
2479 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_reset_transcript_for_hrr", ret);
2480 return ret;
Jerry Yu23d1a252022-05-12 18:08:59 +08002481 }
Gilles Peskine449bd832023-01-11 14:50:10 +01002482 mbedtls_ssl_session_reset_msg_layer(ssl, 0);
Jerry Yu23d1a252022-05-12 18:08:59 +08002483
Gilles Peskine449bd832023-01-11 14:50:10 +01002484 return 0;
Jerry Yu23d1a252022-05-12 18:08:59 +08002485}
2486
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002487MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002488static int ssl_tls13_write_hello_retry_request(mbedtls_ssl_context *ssl)
Jerry Yu23d1a252022-05-12 18:08:59 +08002489{
2490 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
2491 unsigned char *buf;
2492 size_t buf_len, msg_len;
2493
Gilles Peskine449bd832023-01-11 14:50:10 +01002494 MBEDTLS_SSL_DEBUG_MSG(2, ("=> write hello retry request"));
Jerry Yu23d1a252022-05-12 18:08:59 +08002495
Gilles Peskine449bd832023-01-11 14:50:10 +01002496 MBEDTLS_SSL_PROC_CHK(ssl_tls13_prepare_hello_retry_request(ssl));
Jerry Yu23d1a252022-05-12 18:08:59 +08002497
Gilles Peskine449bd832023-01-11 14:50:10 +01002498 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_start_handshake_msg(
2499 ssl, MBEDTLS_SSL_HS_SERVER_HELLO,
2500 &buf, &buf_len));
Jerry Yu23d1a252022-05-12 18:08:59 +08002501
Gilles Peskine449bd832023-01-11 14:50:10 +01002502 MBEDTLS_SSL_PROC_CHK(ssl_tls13_write_server_hello_body(ssl, buf,
2503 buf + buf_len,
2504 &msg_len,
2505 1));
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +01002506 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_add_hs_msg_to_checksum(
Manuel Pégourié-Gonnard43cc1272023-02-06 11:48:19 +01002507 ssl, MBEDTLS_SSL_HS_SERVER_HELLO, buf, msg_len));
Jerry Yu23d1a252022-05-12 18:08:59 +08002508
2509
Gilles Peskine449bd832023-01-11 14:50:10 +01002510 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_finish_handshake_msg(ssl, buf_len,
2511 msg_len));
Jerry Yu23d1a252022-05-12 18:08:59 +08002512
Ronald Cron5fbd2702024-02-14 10:03:36 +01002513 ssl->handshake->hello_retry_request_flag = 1;
Jerry Yu23d1a252022-05-12 18:08:59 +08002514
Gabor Mezei7b39bf12022-05-24 16:04:14 +02002515#if defined(MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE)
2516 /* The server sends a dummy change_cipher_spec record immediately
2517 * after its first handshake message. This may either be after
2518 * a ServerHello or a HelloRetryRequest.
2519 */
Gabor Mezei96ae9262022-06-28 11:45:18 +02002520 mbedtls_ssl_handshake_set_state(
Gilles Peskine449bd832023-01-11 14:50:10 +01002521 ssl, MBEDTLS_SSL_SERVER_CCS_AFTER_HELLO_RETRY_REQUEST);
Gabor Mezei7b39bf12022-05-24 16:04:14 +02002522#else
Gilles Peskine449bd832023-01-11 14:50:10 +01002523 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_CLIENT_HELLO);
Gabor Mezei7b39bf12022-05-24 16:04:14 +02002524#endif /* MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE */
Jerry Yu23d1a252022-05-12 18:08:59 +08002525
2526cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +01002527 MBEDTLS_SSL_DEBUG_MSG(2, ("<= write hello retry request"));
2528 return ret;
Jerry Yu23d1a252022-05-12 18:08:59 +08002529}
2530
Jerry Yu5b64ae92022-03-30 17:15:02 +08002531/*
Jerry Yu4d3841a2022-04-16 12:37:19 +08002532 * Handler for MBEDTLS_SSL_ENCRYPTED_EXTENSIONS
2533 */
Jerry Yu4d3841a2022-04-16 12:37:19 +08002534
2535/*
2536 * struct {
2537 * Extension extensions<0..2 ^ 16 - 1>;
2538 * } EncryptedExtensions;
2539 *
2540 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002541MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002542static int ssl_tls13_write_encrypted_extensions_body(mbedtls_ssl_context *ssl,
2543 unsigned char *buf,
2544 unsigned char *end,
2545 size_t *out_len)
Jerry Yu4d3841a2022-04-16 12:37:19 +08002546{
XiaokangQianacb39922022-06-17 10:18:48 +00002547 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yu4d3841a2022-04-16 12:37:19 +08002548 unsigned char *p = buf;
2549 size_t extensions_len = 0;
Jerry Yu8937eb42022-05-03 12:12:14 +08002550 unsigned char *p_extensions_len;
XiaokangQianacb39922022-06-17 10:18:48 +00002551 size_t output_len;
Jerry Yu9da5e5a2022-05-03 15:46:09 +08002552
Jerry Yu4d3841a2022-04-16 12:37:19 +08002553 *out_len = 0;
2554
Gilles Peskine449bd832023-01-11 14:50:10 +01002555 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 2);
Jerry Yu8937eb42022-05-03 12:12:14 +08002556 p_extensions_len = p;
Jerry Yu4d3841a2022-04-16 12:37:19 +08002557 p += 2;
2558
Jerry Yu8937eb42022-05-03 12:12:14 +08002559 ((void) ssl);
XiaokangQianacb39922022-06-17 10:18:48 +00002560 ((void) ret);
2561 ((void) output_len);
2562
2563#if defined(MBEDTLS_SSL_ALPN)
Gilles Peskine449bd832023-01-11 14:50:10 +01002564 ret = mbedtls_ssl_write_alpn_ext(ssl, p, end, &output_len);
2565 if (ret != 0) {
2566 return ret;
2567 }
XiaokangQian95d5f542022-06-24 02:29:26 +00002568 p += output_len;
XiaokangQianacb39922022-06-17 10:18:48 +00002569#endif /* MBEDTLS_SSL_ALPN */
Jerry Yu4d3841a2022-04-16 12:37:19 +08002570
Jerry Yu71c14f12022-12-12 11:10:35 +08002571#if defined(MBEDTLS_SSL_EARLY_DATA)
Ronald Cron78a38f62024-02-01 18:30:31 +01002572 if (ssl->handshake->early_data_accepted) {
Jerry Yu52335392023-11-23 18:06:06 +08002573 ret = mbedtls_ssl_tls13_write_early_data_ext(
Jerry Yuc59c5862023-12-05 10:40:49 +08002574 ssl, 0, p, end, &output_len);
Jerry Yu71c14f12022-12-12 11:10:35 +08002575 if (ret != 0) {
2576 return ret;
2577 }
2578 p += output_len;
2579 }
2580#endif /* MBEDTLS_SSL_EARLY_DATA */
2581
Waleed Elmelegy47d29462024-01-03 17:31:52 +00002582#if defined(MBEDTLS_SSL_RECORD_SIZE_LIMIT)
Waleed Elmelegyfbe42742024-01-05 18:11:10 +00002583 if (ssl->handshake->received_extensions & MBEDTLS_SSL_EXT_MASK(RECORD_SIZE_LIMIT)) {
Waleed Elmelegyd2fc90e2024-01-04 18:04:53 +00002584 ret = mbedtls_ssl_tls13_write_record_size_limit_ext(
2585 ssl, p, end, &output_len);
2586 if (ret != 0) {
2587 return ret;
2588 }
2589 p += output_len;
Waleed Elmelegy47d29462024-01-03 17:31:52 +00002590 }
Waleed Elmelegy47d29462024-01-03 17:31:52 +00002591#endif
2592
Gilles Peskine449bd832023-01-11 14:50:10 +01002593 extensions_len = (p - p_extensions_len) - 2;
2594 MBEDTLS_PUT_UINT16_BE(extensions_len, p_extensions_len, 0);
Jerry Yu8937eb42022-05-03 12:12:14 +08002595
2596 *out_len = p - buf;
Jerry Yu4d3841a2022-04-16 12:37:19 +08002597
Gilles Peskine449bd832023-01-11 14:50:10 +01002598 MBEDTLS_SSL_DEBUG_BUF(4, "encrypted extensions", buf, *out_len);
Jerry Yu4d3841a2022-04-16 12:37:19 +08002599
Jerry Yu7de2ff02022-11-08 21:43:46 +08002600 MBEDTLS_SSL_PRINT_EXTS(
Gilles Peskine449bd832023-01-11 14:50:10 +01002601 3, MBEDTLS_SSL_HS_ENCRYPTED_EXTENSIONS, ssl->handshake->sent_extensions);
Jerry Yu4b8f2f72022-10-31 13:31:22 +08002602
Gilles Peskine449bd832023-01-11 14:50:10 +01002603 return 0;
Jerry Yu4d3841a2022-04-16 12:37:19 +08002604}
2605
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002606MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002607static int ssl_tls13_write_encrypted_extensions(mbedtls_ssl_context *ssl)
Jerry Yu4d3841a2022-04-16 12:37:19 +08002608{
2609 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
2610 unsigned char *buf;
Jerry Yu39730a72022-05-03 12:14:04 +08002611 size_t buf_len, msg_len;
Jerry Yu4d3841a2022-04-16 12:37:19 +08002612
Gilles Peskine449bd832023-01-11 14:50:10 +01002613 mbedtls_ssl_set_outbound_transform(ssl,
2614 ssl->handshake->transform_handshake);
Gabor Mezei54719122022-06-28 11:34:56 +02002615 MBEDTLS_SSL_DEBUG_MSG(
Gilles Peskine449bd832023-01-11 14:50:10 +01002616 3, ("switching to handshake transform for outbound data"));
Gabor Mezei54719122022-06-28 11:34:56 +02002617
Gilles Peskine449bd832023-01-11 14:50:10 +01002618 MBEDTLS_SSL_DEBUG_MSG(2, ("=> write encrypted extensions"));
Jerry Yu4d3841a2022-04-16 12:37:19 +08002619
Xiaokang Qian9f1747b2023-03-30 02:50:04 +00002620 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_start_handshake_msg(
2621 ssl, MBEDTLS_SSL_HS_ENCRYPTED_EXTENSIONS,
2622 &buf, &buf_len));
Jerry Yu4d3841a2022-04-16 12:37:19 +08002623
Gilles Peskine449bd832023-01-11 14:50:10 +01002624 MBEDTLS_SSL_PROC_CHK(ssl_tls13_write_encrypted_extensions_body(
2625 ssl, buf, buf + buf_len, &msg_len));
Jerry Yu4d3841a2022-04-16 12:37:19 +08002626
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +01002627 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_add_hs_msg_to_checksum(
Xiaokang Qian9f1747b2023-03-30 02:50:04 +00002628 ssl, MBEDTLS_SSL_HS_ENCRYPTED_EXTENSIONS,
2629 buf, msg_len));
Jerry Yu4d3841a2022-04-16 12:37:19 +08002630
Gilles Peskine449bd832023-01-11 14:50:10 +01002631 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_finish_handshake_msg(
2632 ssl, buf_len, msg_len));
Jerry Yu4d3841a2022-04-16 12:37:19 +08002633
Ronald Cron928cbd32022-10-04 16:14:26 +02002634#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01002635 if (mbedtls_ssl_tls13_key_exchange_mode_with_psk(ssl)) {
2636 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_SERVER_FINISHED);
2637 } else {
2638 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_CERTIFICATE_REQUEST);
2639 }
Jerry Yu8937eb42022-05-03 12:12:14 +08002640#else
Gilles Peskine449bd832023-01-11 14:50:10 +01002641 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_SERVER_FINISHED);
Jerry Yu8937eb42022-05-03 12:12:14 +08002642#endif
2643
Jerry Yu4d3841a2022-04-16 12:37:19 +08002644cleanup:
2645
Gilles Peskine449bd832023-01-11 14:50:10 +01002646 MBEDTLS_SSL_DEBUG_MSG(2, ("<= write encrypted extensions"));
2647 return ret;
Jerry Yu4d3841a2022-04-16 12:37:19 +08002648}
2649
Ronald Cron928cbd32022-10-04 16:14:26 +02002650#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED)
XiaokangQiana987e1d2022-05-07 01:25:58 +00002651#define SSL_CERTIFICATE_REQUEST_SEND_REQUEST 0
2652#define SSL_CERTIFICATE_REQUEST_SKIP 1
2653/* Coordination:
2654 * Check whether a CertificateRequest message should be written.
2655 * Returns a negative code on failure, or
2656 * - SSL_CERTIFICATE_REQUEST_SEND_REQUEST
2657 * - SSL_CERTIFICATE_REQUEST_SKIP
2658 * indicating if the writing of the CertificateRequest
2659 * should be skipped or not.
2660 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002661MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002662static int ssl_tls13_certificate_request_coordinate(mbedtls_ssl_context *ssl)
XiaokangQiana987e1d2022-05-07 01:25:58 +00002663{
2664 int authmode;
2665
XiaokangQian40a35232022-05-07 09:02:40 +00002666#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Gilles Peskine449bd832023-01-11 14:50:10 +01002667 if (ssl->handshake->sni_authmode != MBEDTLS_SSL_VERIFY_UNSET) {
XiaokangQian40a35232022-05-07 09:02:40 +00002668 authmode = ssl->handshake->sni_authmode;
Gilles Peskine449bd832023-01-11 14:50:10 +01002669 } else
XiaokangQian40a35232022-05-07 09:02:40 +00002670#endif
XiaokangQiana987e1d2022-05-07 01:25:58 +00002671 authmode = ssl->conf->authmode;
2672
Gilles Peskine449bd832023-01-11 14:50:10 +01002673 if (authmode == MBEDTLS_SSL_VERIFY_NONE) {
Ronald Croneac00ad2022-09-13 10:16:31 +02002674 ssl->session_negotiate->verify_result = MBEDTLS_X509_BADCERT_SKIP_VERIFY;
Gilles Peskine449bd832023-01-11 14:50:10 +01002675 return SSL_CERTIFICATE_REQUEST_SKIP;
Ronald Croneac00ad2022-09-13 10:16:31 +02002676 }
XiaokangQiana987e1d2022-05-07 01:25:58 +00002677
XiaokangQianc3017f62022-05-13 05:55:41 +00002678 ssl->handshake->certificate_request_sent = 1;
XiaokangQian189ded22022-05-10 08:12:17 +00002679
Gilles Peskine449bd832023-01-11 14:50:10 +01002680 return SSL_CERTIFICATE_REQUEST_SEND_REQUEST;
XiaokangQiana987e1d2022-05-07 01:25:58 +00002681}
2682
XiaokangQiancec9ae62022-05-06 07:28:50 +00002683/*
2684 * struct {
2685 * opaque certificate_request_context<0..2^8-1>;
2686 * Extension extensions<2..2^16-1>;
2687 * } CertificateRequest;
2688 *
2689 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002690MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002691static int ssl_tls13_write_certificate_request_body(mbedtls_ssl_context *ssl,
2692 unsigned char *buf,
2693 const unsigned char *end,
2694 size_t *out_len)
XiaokangQiancec9ae62022-05-06 07:28:50 +00002695{
2696 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
2697 unsigned char *p = buf;
XiaokangQianec6efb92022-05-06 09:53:10 +00002698 size_t output_len = 0;
XiaokangQiancec9ae62022-05-06 07:28:50 +00002699 unsigned char *p_extensions_len;
2700
2701 *out_len = 0;
2702
2703 /* Check if we have enough space:
2704 * - certificate_request_context (1 byte)
2705 * - extensions length (2 bytes)
2706 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002707 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 3);
XiaokangQiancec9ae62022-05-06 07:28:50 +00002708
2709 /*
2710 * Write certificate_request_context
2711 */
2712 /*
2713 * We use a zero length context for the normal handshake
2714 * messages. For post-authentication handshake messages
2715 * this request context would be set to a non-zero value.
2716 */
2717 *p++ = 0x0;
2718
2719 /*
2720 * Write extensions
2721 */
2722 /* The extensions must contain the signature_algorithms. */
2723 p_extensions_len = p;
2724 p += 2;
Gilles Peskine449bd832023-01-11 14:50:10 +01002725 ret = mbedtls_ssl_write_sig_alg_ext(ssl, p, end, &output_len);
2726 if (ret != 0) {
2727 return ret;
2728 }
XiaokangQiancec9ae62022-05-06 07:28:50 +00002729
XiaokangQianec6efb92022-05-06 09:53:10 +00002730 p += output_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01002731 MBEDTLS_PUT_UINT16_BE(p - p_extensions_len - 2, p_extensions_len, 0);
XiaokangQiancec9ae62022-05-06 07:28:50 +00002732
2733 *out_len = p - buf;
2734
Jerry Yu7de2ff02022-11-08 21:43:46 +08002735 MBEDTLS_SSL_PRINT_EXTS(
Gilles Peskine449bd832023-01-11 14:50:10 +01002736 3, MBEDTLS_SSL_HS_CERTIFICATE_REQUEST, ssl->handshake->sent_extensions);
Jerry Yu4b8f2f72022-10-31 13:31:22 +08002737
Gilles Peskine449bd832023-01-11 14:50:10 +01002738 return 0;
XiaokangQiancec9ae62022-05-06 07:28:50 +00002739}
2740
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002741MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002742static int ssl_tls13_write_certificate_request(mbedtls_ssl_context *ssl)
XiaokangQiancec9ae62022-05-06 07:28:50 +00002743{
2744 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
2745
Gilles Peskine449bd832023-01-11 14:50:10 +01002746 MBEDTLS_SSL_DEBUG_MSG(2, ("=> write certificate request"));
XiaokangQiancec9ae62022-05-06 07:28:50 +00002747
Gilles Peskine449bd832023-01-11 14:50:10 +01002748 MBEDTLS_SSL_PROC_CHK_NEG(ssl_tls13_certificate_request_coordinate(ssl));
XiaokangQiancec9ae62022-05-06 07:28:50 +00002749
Gilles Peskine449bd832023-01-11 14:50:10 +01002750 if (ret == SSL_CERTIFICATE_REQUEST_SEND_REQUEST) {
XiaokangQiancec9ae62022-05-06 07:28:50 +00002751 unsigned char *buf;
2752 size_t buf_len, msg_len;
2753
Xiaokang Qian9f1747b2023-03-30 02:50:04 +00002754 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_start_handshake_msg(
2755 ssl, MBEDTLS_SSL_HS_CERTIFICATE_REQUEST,
2756 &buf, &buf_len));
XiaokangQiancec9ae62022-05-06 07:28:50 +00002757
Gilles Peskine449bd832023-01-11 14:50:10 +01002758 MBEDTLS_SSL_PROC_CHK(ssl_tls13_write_certificate_request_body(
2759 ssl, buf, buf + buf_len, &msg_len));
XiaokangQiancec9ae62022-05-06 07:28:50 +00002760
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +01002761 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_add_hs_msg_to_checksum(
Xiaokang Qian9f1747b2023-03-30 02:50:04 +00002762 ssl, MBEDTLS_SSL_HS_CERTIFICATE_REQUEST,
2763 buf, msg_len));
XiaokangQiancec9ae62022-05-06 07:28:50 +00002764
Gilles Peskine449bd832023-01-11 14:50:10 +01002765 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_finish_handshake_msg(
2766 ssl, buf_len, msg_len));
2767 } else if (ret == SSL_CERTIFICATE_REQUEST_SKIP) {
2768 MBEDTLS_SSL_DEBUG_MSG(2, ("<= skip write certificate request"));
XiaokangQiancec9ae62022-05-06 07:28:50 +00002769 ret = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01002770 } else {
2771 MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
XiaokangQiancec9ae62022-05-06 07:28:50 +00002772 ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
2773 goto cleanup;
2774 }
2775
Gilles Peskine449bd832023-01-11 14:50:10 +01002776 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_SERVER_CERTIFICATE);
XiaokangQiancec9ae62022-05-06 07:28:50 +00002777cleanup:
2778
Gilles Peskine449bd832023-01-11 14:50:10 +01002779 MBEDTLS_SSL_DEBUG_MSG(2, ("<= write certificate request"));
2780 return ret;
XiaokangQiancec9ae62022-05-06 07:28:50 +00002781}
XiaokangQiancec9ae62022-05-06 07:28:50 +00002782
Jerry Yu4d3841a2022-04-16 12:37:19 +08002783/*
Jerry Yuc6e6dbf2022-04-16 19:42:57 +08002784 * Handler for MBEDTLS_SSL_SERVER_CERTIFICATE
Jerry Yu83da34e2022-04-16 13:59:52 +08002785 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002786MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002787static int ssl_tls13_write_server_certificate(mbedtls_ssl_context *ssl)
Jerry Yu83da34e2022-04-16 13:59:52 +08002788{
Jerry Yu5a26f302022-05-10 20:46:40 +08002789 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
XiaokangQianfb665a82022-06-15 03:57:21 +00002790
2791#if defined(MBEDTLS_X509_CRT_PARSE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01002792 if ((ssl_tls13_pick_key_cert(ssl) != 0) ||
2793 mbedtls_ssl_own_cert(ssl) == NULL) {
2794 MBEDTLS_SSL_DEBUG_MSG(2, ("No certificate available."));
2795 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE,
2796 MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE);
2797 return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
Jerry Yu5a26f302022-05-10 20:46:40 +08002798 }
XiaokangQianfb665a82022-06-15 03:57:21 +00002799#endif /* MBEDTLS_X509_CRT_PARSE_C */
Jerry Yu5a26f302022-05-10 20:46:40 +08002800
Gilles Peskine449bd832023-01-11 14:50:10 +01002801 ret = mbedtls_ssl_tls13_write_certificate(ssl);
2802 if (ret != 0) {
2803 return ret;
2804 }
2805 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_CERTIFICATE_VERIFY);
2806 return 0;
Jerry Yu83da34e2022-04-16 13:59:52 +08002807}
2808
2809/*
Jerry Yuc6e6dbf2022-04-16 19:42:57 +08002810 * Handler for MBEDTLS_SSL_CERTIFICATE_VERIFY
Jerry Yu83da34e2022-04-16 13:59:52 +08002811 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002812MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002813static int ssl_tls13_write_certificate_verify(mbedtls_ssl_context *ssl)
Jerry Yu83da34e2022-04-16 13:59:52 +08002814{
Gilles Peskine449bd832023-01-11 14:50:10 +01002815 int ret = mbedtls_ssl_tls13_write_certificate_verify(ssl);
2816 if (ret != 0) {
2817 return ret;
2818 }
2819 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_SERVER_FINISHED);
2820 return 0;
Jerry Yu83da34e2022-04-16 13:59:52 +08002821}
Ronald Cron928cbd32022-10-04 16:14:26 +02002822#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED */
Jerry Yu83da34e2022-04-16 13:59:52 +08002823
Jerry Yu9b72e392023-12-01 16:27:08 +08002824/*
2825 * RFC 8446 section A.2
2826 *
2827 * | Send ServerHello
2828 * | K_send = handshake
2829 * | Send EncryptedExtensions
2830 * | [Send CertificateRequest]
2831 * Can send | [Send Certificate + CertificateVerify]
2832 * app data | Send Finished
2833 * after --> | K_send = application
2834 * here +--------+--------+
2835 * No 0-RTT | | 0-RTT
2836 * | |
2837 * K_recv = handshake | | K_recv = early data
2838 * [Skip decrypt errors] | +------> WAIT_EOED -+
2839 * | | Recv | | Recv EndOfEarlyData
2840 * | | early data | | K_recv = handshake
2841 * | +------------+ |
2842 * | |
2843 * +> WAIT_FLIGHT2 <--------+
2844 * |
2845 * +--------+--------+
2846 * No auth | | Client auth
2847 * | |
2848 * | v
2849 * | WAIT_CERT
2850 * | Recv | | Recv Certificate
2851 * | empty | v
2852 * | Certificate | WAIT_CV
2853 * | | | Recv
2854 * | v | CertificateVerify
2855 * +-> WAIT_FINISHED <---+
2856 * | Recv Finished
2857 *
2858 *
2859 * The following function handles the state changes after WAIT_FLIGHT2 in the
Jerry Yu3be85072023-12-04 09:58:54 +08002860 * above diagram. We are not going to receive early data related messages
2861 * anymore, prepare to receive the first handshake message of the client
2862 * second flight.
Jerry Yu9b72e392023-12-01 16:27:08 +08002863 */
Jerry Yu3be85072023-12-04 09:58:54 +08002864static void ssl_tls13_prepare_for_handshake_second_flight(
2865 mbedtls_ssl_context *ssl)
Jerry Yu9b72e392023-12-01 16:27:08 +08002866{
Jerry Yu9b72e392023-12-01 16:27:08 +08002867 if (ssl->handshake->certificate_request_sent) {
2868 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_CLIENT_CERTIFICATE);
2869 } else {
Jerry Yu42020fb2023-12-05 17:35:53 +08002870 MBEDTLS_SSL_DEBUG_MSG(2, ("skip parse certificate"));
Jerry Yuebb1b1d2023-12-05 11:02:15 +08002871 MBEDTLS_SSL_DEBUG_MSG(2, ("skip parse certificate verify"));
Jerry Yuebb1b1d2023-12-05 11:02:15 +08002872
Jerry Yu9b72e392023-12-01 16:27:08 +08002873 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_CLIENT_FINISHED);
2874 }
Jerry Yu9b72e392023-12-01 16:27:08 +08002875}
2876
Jerry Yu83da34e2022-04-16 13:59:52 +08002877/*
Jerry Yud6e253d2022-05-18 13:59:24 +08002878 * Handler for MBEDTLS_SSL_SERVER_FINISHED
Jerry Yu69dd8d42022-04-16 12:51:26 +08002879 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002880MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002881static int ssl_tls13_write_server_finished(mbedtls_ssl_context *ssl)
Jerry Yu69dd8d42022-04-16 12:51:26 +08002882{
Jerry Yud6e253d2022-05-18 13:59:24 +08002883 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yu27bdc7c2022-04-16 13:33:27 +08002884
Gilles Peskine449bd832023-01-11 14:50:10 +01002885 ret = mbedtls_ssl_tls13_write_finished_message(ssl);
2886 if (ret != 0) {
2887 return ret;
2888 }
Jerry Yu27bdc7c2022-04-16 13:33:27 +08002889
Gilles Peskine449bd832023-01-11 14:50:10 +01002890 ret = mbedtls_ssl_tls13_compute_application_transform(ssl);
2891 if (ret != 0) {
Jerry Yue3d67cb2022-05-19 15:33:10 +08002892 MBEDTLS_SSL_PEND_FATAL_ALERT(
Gilles Peskine449bd832023-01-11 14:50:10 +01002893 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE,
2894 MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE);
2895 return ret;
Jerry Yue3d67cb2022-05-19 15:33:10 +08002896 }
XiaokangQianc3017f62022-05-13 05:55:41 +00002897
Jerry Yu87b5ed42022-12-12 13:07:07 +08002898#if defined(MBEDTLS_SSL_EARLY_DATA)
Ronald Cron78a38f62024-02-01 18:30:31 +01002899 if (ssl->handshake->early_data_accepted) {
Jerry Yu0af63dc2023-12-01 17:14:51 +08002900 /* See RFC 8446 section A.2 for more information */
Jerry Yu87b5ed42022-12-12 13:07:07 +08002901 MBEDTLS_SSL_DEBUG_MSG(
2902 1, ("Switch to early keys for inbound traffic. "
2903 "( K_recv = early data )"));
2904 mbedtls_ssl_set_inbound_transform(
2905 ssl, ssl->handshake->transform_earlydata);
2906 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_END_OF_EARLY_DATA);
2907 return 0;
2908 }
2909#endif /* MBEDTLS_SSL_EARLY_DATA */
Jerry Yu0af63dc2023-12-01 17:14:51 +08002910 MBEDTLS_SSL_DEBUG_MSG(
2911 1, ("Switch to handshake keys for inbound traffic "
2912 "( K_recv = handshake )"));
Gilles Peskine449bd832023-01-11 14:50:10 +01002913 mbedtls_ssl_set_inbound_transform(ssl, ssl->handshake->transform_handshake);
XiaokangQian189ded22022-05-10 08:12:17 +00002914
Jerry Yu3be85072023-12-04 09:58:54 +08002915 ssl_tls13_prepare_for_handshake_second_flight(ssl);
Jerry Yu7d8c3fe2022-12-12 12:59:44 +08002916
2917 return 0;
2918}
2919
Jerry Yu87b5ed42022-12-12 13:07:07 +08002920#if defined(MBEDTLS_SSL_EARLY_DATA)
2921/*
Jerry Yu59d420f2023-12-01 16:30:34 +08002922 * Handler for MBEDTLS_SSL_END_OF_EARLY_DATA
Jerry Yu87b5ed42022-12-12 13:07:07 +08002923 */
Jerry Yu3be85072023-12-04 09:58:54 +08002924#define SSL_GOT_END_OF_EARLY_DATA 0
Jerry Yub55f9eb2023-12-05 10:27:17 +08002925#define SSL_GOT_EARLY_DATA 1
Jerry Yud5c34962023-12-01 16:32:31 +08002926/* Coordination:
Jerry Yu3be85072023-12-04 09:58:54 +08002927 * Deals with the ambiguity of not knowing if the next message is an
2928 * EndOfEarlyData message or an application message containing early data.
Jerry Yud5c34962023-12-01 16:32:31 +08002929 * Returns a negative code on failure, or
Jerry Yu3be85072023-12-04 09:58:54 +08002930 * - SSL_GOT_END_OF_EARLY_DATA
Jerry Yub55f9eb2023-12-05 10:27:17 +08002931 * - SSL_GOT_EARLY_DATA
Jerry Yud5c34962023-12-01 16:32:31 +08002932 * indicating which message is received.
2933 */
2934MBEDTLS_CHECK_RETURN_CRITICAL
2935static int ssl_tls13_end_of_early_data_coordinate(mbedtls_ssl_context *ssl)
2936{
2937 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yub4ed4602023-12-01 16:34:00 +08002938
2939 if ((ret = mbedtls_ssl_read_record(ssl, 0)) != 0) {
2940 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_read_record", ret);
2941 return ret;
2942 }
2943 ssl->keep_current_message = 1;
2944
2945 if (ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
2946 ssl->in_msg[0] == MBEDTLS_SSL_HS_END_OF_EARLY_DATA) {
Jerry Yub55f9eb2023-12-05 10:27:17 +08002947 MBEDTLS_SSL_DEBUG_MSG(3, ("Received an end_of_early_data message."));
Jerry Yu3be85072023-12-04 09:58:54 +08002948 return SSL_GOT_END_OF_EARLY_DATA;
Jerry Yub4ed4602023-12-01 16:34:00 +08002949 }
2950
2951 if (ssl->in_msgtype == MBEDTLS_SSL_MSG_APPLICATION_DATA) {
Ronald Croned7d4bf2024-01-31 07:55:19 +01002952 if (ssl->in_offt == NULL) {
Ronald Cron85718042024-02-22 10:22:09 +01002953 MBEDTLS_SSL_DEBUG_MSG(3, ("Received early data"));
Ronald Croned7d4bf2024-01-31 07:55:19 +01002954 /* Set the reading pointer */
2955 ssl->in_offt = ssl->in_msg;
Ronald Cron85718042024-02-22 10:22:09 +01002956 ret = mbedtls_ssl_tls13_check_early_data_len(ssl, ssl->in_msglen);
2957 if (ret != 0) {
2958 return ret;
2959 }
Ronald Croned7d4bf2024-01-31 07:55:19 +01002960 }
Jerry Yub55f9eb2023-12-05 10:27:17 +08002961 return SSL_GOT_EARLY_DATA;
Jerry Yub4ed4602023-12-01 16:34:00 +08002962 }
2963
Jerry Yu7bb40a32023-12-04 10:04:15 +08002964 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE,
2965 MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE);
Jerry Yub4ed4602023-12-01 16:34:00 +08002966 return MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE;
Jerry Yud5c34962023-12-01 16:32:31 +08002967}
2968
2969MBEDTLS_CHECK_RETURN_CRITICAL
2970static int ssl_tls13_parse_end_of_early_data(mbedtls_ssl_context *ssl,
2971 const unsigned char *buf,
2972 const unsigned char *end)
2973{
Jerry Yu75c9ab72023-12-01 16:41:40 +08002974 /* RFC 8446 section 4.5
2975 *
2976 * struct {} EndOfEarlyData;
2977 */
Jerry Yufbf03992023-12-04 10:00:37 +08002978 if (buf != end) {
2979 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR,
2980 MBEDTLS_ERR_SSL_DECODE_ERROR);
2981 return MBEDTLS_ERR_SSL_DECODE_ERROR;
2982 }
2983 return 0;
Jerry Yud5c34962023-12-01 16:32:31 +08002984}
2985
Jerry Yud5c34962023-12-01 16:32:31 +08002986/*
2987 * RFC 8446 section A.2
2988 *
2989 * | Send ServerHello
2990 * | K_send = handshake
2991 * | Send EncryptedExtensions
2992 * | [Send CertificateRequest]
2993 * Can send | [Send Certificate + CertificateVerify]
2994 * app data | Send Finished
2995 * after --> | K_send = application
2996 * here +--------+--------+
2997 * No 0-RTT | | 0-RTT
2998 * | |
2999 * K_recv = handshake | | K_recv = early data
3000 * [Skip decrypt errors] | +------> WAIT_EOED -+
3001 * | | Recv | | Recv EndOfEarlyData
3002 * | | early data | | K_recv = handshake
3003 * | +------------+ |
3004 * | |
3005 * +> WAIT_FLIGHT2 <--------+
3006 * |
3007 * +--------+--------+
3008 * No auth | | Client auth
3009 * | |
3010 * | v
3011 * | WAIT_CERT
3012 * | Recv | | Recv Certificate
3013 * | empty | v
3014 * | Certificate | WAIT_CV
3015 * | | | Recv
3016 * | v | CertificateVerify
3017 * +-> WAIT_FINISHED <---+
3018 * | Recv Finished
3019 *
3020 * The function handles actions and state changes from 0-RTT to WAIT_FLIGHT2 in
3021 * the above diagram.
3022 */
Jerry Yu87b5ed42022-12-12 13:07:07 +08003023MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yu59d420f2023-12-01 16:30:34 +08003024static int ssl_tls13_process_end_of_early_data(mbedtls_ssl_context *ssl)
Jerry Yu87b5ed42022-12-12 13:07:07 +08003025{
3026 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yud5c34962023-12-01 16:32:31 +08003027
3028 MBEDTLS_SSL_DEBUG_MSG(2, ("=> ssl_tls13_process_end_of_early_data"));
3029
3030 MBEDTLS_SSL_PROC_CHK_NEG(ssl_tls13_end_of_early_data_coordinate(ssl));
3031
Jerry Yu3be85072023-12-04 09:58:54 +08003032 if (ret == SSL_GOT_END_OF_EARLY_DATA) {
Jerry Yud5c34962023-12-01 16:32:31 +08003033 unsigned char *buf;
3034 size_t buf_len;
3035
3036 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_tls13_fetch_handshake_msg(
3037 ssl, MBEDTLS_SSL_HS_END_OF_EARLY_DATA,
3038 &buf, &buf_len));
3039
3040 MBEDTLS_SSL_PROC_CHK(ssl_tls13_parse_end_of_early_data(
3041 ssl, buf, buf + buf_len));
3042
Jerry Yue9655122023-12-01 16:44:40 +08003043 MBEDTLS_SSL_DEBUG_MSG(
3044 1, ("Switch to handshake keys for inbound traffic"
3045 "( K_recv = handshake )"));
3046 mbedtls_ssl_set_inbound_transform(
3047 ssl, ssl->handshake->transform_handshake);
3048
Jerry Yud5c34962023-12-01 16:32:31 +08003049 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_add_hs_msg_to_checksum(
3050 ssl, MBEDTLS_SSL_HS_END_OF_EARLY_DATA,
3051 buf, buf_len));
Jerry Yue9655122023-12-01 16:44:40 +08003052
Jerry Yu3be85072023-12-04 09:58:54 +08003053 ssl_tls13_prepare_for_handshake_second_flight(ssl);
Jerry Yud5c34962023-12-01 16:32:31 +08003054
Jerry Yub55f9eb2023-12-05 10:27:17 +08003055 } else if (ret == SSL_GOT_EARLY_DATA) {
Jerry Yud9ca3542023-12-06 17:23:52 +08003056 ret = MBEDTLS_ERR_SSL_RECEIVED_EARLY_DATA;
3057 goto cleanup;
Jerry Yud5c34962023-12-01 16:32:31 +08003058 } else {
3059 MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
3060 ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
3061 goto cleanup;
3062 }
3063
Jerry Yud5c34962023-12-01 16:32:31 +08003064cleanup:
3065 MBEDTLS_SSL_DEBUG_MSG(2, ("<= ssl_tls13_process_end_of_early_data"));
Jerry Yu87b5ed42022-12-12 13:07:07 +08003066 return ret;
3067}
3068#endif /* MBEDTLS_SSL_EARLY_DATA */
3069
Jerry Yu69dd8d42022-04-16 12:51:26 +08003070/*
Jerry Yud6e253d2022-05-18 13:59:24 +08003071 * Handler for MBEDTLS_SSL_CLIENT_FINISHED
Jerry Yu69dd8d42022-04-16 12:51:26 +08003072 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02003073MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01003074static int ssl_tls13_process_client_finished(mbedtls_ssl_context *ssl)
Jerry Yu69dd8d42022-04-16 12:51:26 +08003075{
Jerry Yud6e253d2022-05-18 13:59:24 +08003076 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
3077
Gilles Peskine449bd832023-01-11 14:50:10 +01003078 ret = mbedtls_ssl_tls13_process_finished_message(ssl);
3079 if (ret != 0) {
3080 return ret;
Jerry Yue3d67cb2022-05-19 15:33:10 +08003081 }
3082
Gilles Peskine449bd832023-01-11 14:50:10 +01003083 ret = mbedtls_ssl_tls13_compute_resumption_master_secret(ssl);
3084 if (ret != 0) {
Xiaokang Qian9f1747b2023-03-30 02:50:04 +00003085 MBEDTLS_SSL_DEBUG_RET(
3086 1, "mbedtls_ssl_tls13_compute_resumption_master_secret", ret);
Gilles Peskine449bd832023-01-11 14:50:10 +01003087 }
3088
3089 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_HANDSHAKE_WRAPUP);
3090 return 0;
Jerry Yu69dd8d42022-04-16 12:51:26 +08003091}
3092
3093/*
Jerry Yud6e253d2022-05-18 13:59:24 +08003094 * Handler for MBEDTLS_SSL_HANDSHAKE_WRAPUP
Jerry Yu69dd8d42022-04-16 12:51:26 +08003095 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02003096MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01003097static int ssl_tls13_handshake_wrapup(mbedtls_ssl_context *ssl)
Jerry Yu69dd8d42022-04-16 12:51:26 +08003098{
Gilles Peskine449bd832023-01-11 14:50:10 +01003099 MBEDTLS_SSL_DEBUG_MSG(2, ("handshake: done"));
Jerry Yu03ed50b2022-04-16 17:13:30 +08003100
Gilles Peskine449bd832023-01-11 14:50:10 +01003101 mbedtls_ssl_tls13_handshake_wrapup(ssl);
Jerry Yue67bef42022-07-07 07:29:42 +00003102
Pengyu Lv3643fdb2023-01-12 16:46:28 +08003103#if defined(MBEDTLS_SSL_SESSION_TICKETS) && \
3104 defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED)
Pengyu Lva1aa31b2022-12-13 13:49:59 +08003105/* TODO: Remove the check of SOME_PSK_ENABLED since SESSION_TICKETS requires
3106 * SOME_PSK_ENABLED to be enabled. Here is just to make CI happy. It is
3107 * expected to be resolved with issue#6395.
3108 */
Pengyu Lvc7af2c42022-12-01 16:33:00 +08003109 /* Sent NewSessionTicket message only when client supports PSK */
Pengyu Lvb2cfafb2023-11-14 13:56:13 +08003110 if (mbedtls_ssl_tls13_is_some_psk_supported(ssl)) {
Xiaokang Qian9f1747b2023-03-30 02:50:04 +00003111 mbedtls_ssl_handshake_set_state(
3112 ssl, MBEDTLS_SSL_TLS1_3_NEW_SESSION_TICKET);
Pengyu Lvc7af2c42022-12-01 16:33:00 +08003113 } else
Jerry Yufca4d572022-07-21 10:37:48 +08003114#endif
Pengyu Lv3643fdb2023-01-12 16:46:28 +08003115 {
3116 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_HANDSHAKE_OVER);
3117 }
Gilles Peskine449bd832023-01-11 14:50:10 +01003118 return 0;
Jerry Yu69dd8d42022-04-16 12:51:26 +08003119}
3120
Norbert Fabritiusda0d1692023-01-23 15:24:59 +01003121#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Jerry Yu69dd8d42022-04-16 12:51:26 +08003122/*
Jerry Yua8d3c502022-10-30 14:51:23 +08003123 * Handler for MBEDTLS_SSL_TLS1_3_NEW_SESSION_TICKET
Jerry Yue67bef42022-07-07 07:29:42 +00003124 */
3125#define SSL_NEW_SESSION_TICKET_SKIP 0
3126#define SSL_NEW_SESSION_TICKET_WRITE 1
3127MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01003128static int ssl_tls13_write_new_session_ticket_coordinate(mbedtls_ssl_context *ssl)
Jerry Yue67bef42022-07-07 07:29:42 +00003129{
3130 /* Check whether the use of session tickets is enabled */
Gilles Peskine449bd832023-01-11 14:50:10 +01003131 if (ssl->conf->f_ticket_write == NULL) {
3132 MBEDTLS_SSL_DEBUG_MSG(2, ("NewSessionTicket: disabled,"
3133 " callback is not set"));
3134 return SSL_NEW_SESSION_TICKET_SKIP;
Jerry Yud0766ec2022-09-22 10:46:57 +08003135 }
Gilles Peskine449bd832023-01-11 14:50:10 +01003136 if (ssl->conf->new_session_tickets_count == 0) {
3137 MBEDTLS_SSL_DEBUG_MSG(2, ("NewSessionTicket: disabled,"
3138 " configured count is zero"));
3139 return SSL_NEW_SESSION_TICKET_SKIP;
Jerry Yud0766ec2022-09-22 10:46:57 +08003140 }
3141
Gilles Peskine449bd832023-01-11 14:50:10 +01003142 if (ssl->handshake->new_session_tickets_count == 0) {
3143 MBEDTLS_SSL_DEBUG_MSG(2, ("NewSessionTicket: all tickets have "
3144 "been sent."));
3145 return SSL_NEW_SESSION_TICKET_SKIP;
Jerry Yue67bef42022-07-07 07:29:42 +00003146 }
3147
Gilles Peskine449bd832023-01-11 14:50:10 +01003148 return SSL_NEW_SESSION_TICKET_WRITE;
Jerry Yue67bef42022-07-07 07:29:42 +00003149}
3150
Jerry Yue67bef42022-07-07 07:29:42 +00003151MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01003152static int ssl_tls13_prepare_new_session_ticket(mbedtls_ssl_context *ssl,
3153 unsigned char *ticket_nonce,
3154 size_t ticket_nonce_size)
Jerry Yue67bef42022-07-07 07:29:42 +00003155{
3156 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
3157 mbedtls_ssl_session *session = ssl->session;
3158 mbedtls_ssl_ciphersuite_t *ciphersuite_info;
3159 psa_algorithm_t psa_hash_alg;
3160 int hash_length;
3161
Gilles Peskine449bd832023-01-11 14:50:10 +01003162 MBEDTLS_SSL_DEBUG_MSG(2, ("=> prepare NewSessionTicket msg"));
Jerry Yue67bef42022-07-07 07:29:42 +00003163
Pengyu Lv9f926952022-11-17 15:22:33 +08003164 /* Set ticket_flags depends on the advertised psk key exchange mode */
Pengyu Lv94a42cc2023-12-06 10:04:17 +08003165 mbedtls_ssl_tls13_session_clear_ticket_flags(
Pengyu Lv9eacb442022-12-09 14:39:19 +08003166 session, MBEDTLS_SSL_TLS1_3_TICKET_FLAGS_MASK);
Pengyu Lve6487fe2022-12-06 09:30:29 +08003167#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED)
Pengyu Lv94a42cc2023-12-06 10:04:17 +08003168 mbedtls_ssl_tls13_session_set_ticket_flags(
Pengyu Lv9eacb442022-12-09 14:39:19 +08003169 session, ssl->handshake->tls13_kex_modes);
Pengyu Lve6487fe2022-12-06 09:30:29 +08003170#endif
Jerry Yu95648b02023-12-06 15:03:34 +08003171
3172#if defined(MBEDTLS_SSL_EARLY_DATA)
3173 if (ssl->conf->early_data_enabled == MBEDTLS_SSL_EARLY_DATA_ENABLED &&
3174 ssl->conf->max_early_data_size > 0) {
Pengyu Lv94a42cc2023-12-06 10:04:17 +08003175 mbedtls_ssl_tls13_session_set_ticket_flags(
Jerry Yu95648b02023-12-06 15:03:34 +08003176 session, MBEDTLS_SSL_TLS1_3_TICKET_ALLOW_EARLY_DATA);
Ronald Cronc2865192024-02-07 14:01:03 +01003177 session->max_early_data_size = ssl->conf->max_early_data_size;
Jerry Yu95648b02023-12-06 15:03:34 +08003178 }
3179#endif /* MBEDTLS_SSL_EARLY_DATA */
3180
Pengyu Lv4938a562023-01-16 11:28:49 +08003181 MBEDTLS_SSL_PRINT_TICKET_FLAGS(4, session->ticket_flags);
Pengyu Lv9f926952022-11-17 15:22:33 +08003182
Waleed Elmelegy2824a202024-02-23 17:51:47 +00003183#if defined(MBEDTLS_SSL_EARLY_DATA) && defined(MBEDTLS_SSL_ALPN)
Waleed Elmelegy5bc52632024-03-12 16:25:08 +00003184 if (session->ticket_alpn == NULL) {
3185 ret = mbedtls_ssl_session_set_ticket_alpn(session, ssl->alpn_chosen);
3186 if (ret != 0) {
3187 return ret;
3188 }
Waleed Elmelegy2824a202024-02-23 17:51:47 +00003189 }
3190#endif
3191
Jerry Yue67bef42022-07-07 07:29:42 +00003192 /* Generate ticket_age_add */
Gilles Peskine449bd832023-01-11 14:50:10 +01003193 if ((ret = ssl->conf->f_rng(ssl->conf->p_rng,
3194 (unsigned char *) &session->ticket_age_add,
3195 sizeof(session->ticket_age_add)) != 0)) {
3196 MBEDTLS_SSL_DEBUG_RET(1, "generate_ticket_age_add", ret);
3197 return ret;
Jerry Yue67bef42022-07-07 07:29:42 +00003198 }
Gilles Peskine449bd832023-01-11 14:50:10 +01003199 MBEDTLS_SSL_DEBUG_MSG(3, ("ticket_age_add: %u",
3200 (unsigned int) session->ticket_age_add));
Jerry Yue67bef42022-07-07 07:29:42 +00003201
3202 /* Generate ticket_nonce */
Gilles Peskine449bd832023-01-11 14:50:10 +01003203 ret = ssl->conf->f_rng(ssl->conf->p_rng, ticket_nonce, ticket_nonce_size);
3204 if (ret != 0) {
3205 MBEDTLS_SSL_DEBUG_RET(1, "generate_ticket_nonce", ret);
3206 return ret;
Jerry Yue67bef42022-07-07 07:29:42 +00003207 }
Gilles Peskine449bd832023-01-11 14:50:10 +01003208 MBEDTLS_SSL_DEBUG_BUF(3, "ticket_nonce:",
3209 ticket_nonce, ticket_nonce_size);
Jerry Yue67bef42022-07-07 07:29:42 +00003210
3211 ciphersuite_info =
Gilles Peskine449bd832023-01-11 14:50:10 +01003212 (mbedtls_ssl_ciphersuite_t *) ssl->handshake->ciphersuite_info;
Dave Rodgman2eab4622023-10-05 13:30:37 +01003213 psa_hash_alg = mbedtls_md_psa_alg_from_type((mbedtls_md_type_t) ciphersuite_info->mac);
Gilles Peskine449bd832023-01-11 14:50:10 +01003214 hash_length = PSA_HASH_LENGTH(psa_hash_alg);
3215 if (hash_length == -1 ||
3216 (size_t) hash_length > sizeof(session->resumption_key)) {
3217 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Jerry Yufca4d572022-07-21 10:37:48 +08003218 }
3219
Jerry Yue67bef42022-07-07 07:29:42 +00003220 /* In this code the psk key length equals the length of the hash */
3221 session->resumption_key_len = hash_length;
3222 session->ciphersuite = ciphersuite_info->id;
3223
3224 /* Compute resumption key
3225 *
3226 * HKDF-Expand-Label( resumption_master_secret,
3227 * "resumption", ticket_nonce, Hash.length )
3228 */
3229 ret = mbedtls_ssl_tls13_hkdf_expand_label(
Gilles Peskine449bd832023-01-11 14:50:10 +01003230 psa_hash_alg,
3231 session->app_secrets.resumption_master_secret,
3232 hash_length,
3233 MBEDTLS_SSL_TLS1_3_LBL_WITH_LEN(resumption),
3234 ticket_nonce,
3235 ticket_nonce_size,
3236 session->resumption_key,
3237 hash_length);
Jerry Yue67bef42022-07-07 07:29:42 +00003238
Gilles Peskine449bd832023-01-11 14:50:10 +01003239 if (ret != 0) {
3240 MBEDTLS_SSL_DEBUG_RET(2,
3241 "Creating the ticket-resumed PSK failed",
3242 ret);
3243 return ret;
Jerry Yue67bef42022-07-07 07:29:42 +00003244 }
Gilles Peskine449bd832023-01-11 14:50:10 +01003245 MBEDTLS_SSL_DEBUG_BUF(3, "Ticket-resumed PSK",
3246 session->resumption_key,
3247 session->resumption_key_len);
Jerry Yue67bef42022-07-07 07:29:42 +00003248
Gilles Peskine449bd832023-01-11 14:50:10 +01003249 MBEDTLS_SSL_DEBUG_BUF(3, "resumption_master_secret",
3250 session->app_secrets.resumption_master_secret,
3251 hash_length);
Jerry Yue67bef42022-07-07 07:29:42 +00003252
Gilles Peskine449bd832023-01-11 14:50:10 +01003253 return 0;
Jerry Yue67bef42022-07-07 07:29:42 +00003254}
3255
3256/* This function creates a NewSessionTicket message in the following format:
3257 *
3258 * struct {
3259 * uint32 ticket_lifetime;
3260 * uint32 ticket_age_add;
3261 * opaque ticket_nonce<0..255>;
3262 * opaque ticket<1..2^16-1>;
3263 * Extension extensions<0..2^16-2>;
3264 * } NewSessionTicket;
3265 *
3266 * The ticket inside the NewSessionTicket message is an encrypted container
3267 * carrying the necessary information so that the server is later able to
3268 * re-start the communication.
3269 *
3270 * The following fields are placed inside the ticket by the
3271 * f_ticket_write() function:
3272 *
Jerry Yu0069abc2023-11-23 21:07:28 +08003273 * - creation time (ticket_creation_time)
3274 * - flags (ticket_flags)
Jerry Yue67bef42022-07-07 07:29:42 +00003275 * - age add (ticket_age_add)
Jerry Yu0069abc2023-11-23 21:07:28 +08003276 * - key (resumption_key)
3277 * - key length (resumption_key_len)
Jerry Yue67bef42022-07-07 07:29:42 +00003278 * - ciphersuite (ciphersuite)
Jerry Yu0069abc2023-11-23 21:07:28 +08003279 * - max_early_data_size (max_early_data_size)
Jerry Yue67bef42022-07-07 07:29:42 +00003280 */
3281MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01003282static int ssl_tls13_write_new_session_ticket_body(mbedtls_ssl_context *ssl,
3283 unsigned char *buf,
3284 unsigned char *end,
3285 size_t *out_len,
3286 unsigned char *ticket_nonce,
3287 size_t ticket_nonce_size)
Jerry Yue67bef42022-07-07 07:29:42 +00003288{
3289 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
3290 unsigned char *p = buf;
3291 mbedtls_ssl_session *session = ssl->session;
3292 size_t ticket_len;
3293 uint32_t ticket_lifetime;
Jerry Yu01da35e2022-12-12 15:09:22 +08003294 unsigned char *p_extensions_len;
Jerry Yue67bef42022-07-07 07:29:42 +00003295
3296 *out_len = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01003297 MBEDTLS_SSL_DEBUG_MSG(2, ("=> write NewSessionTicket msg"));
Jerry Yue67bef42022-07-07 07:29:42 +00003298
3299 /*
3300 * ticket_lifetime 4 bytes
3301 * ticket_age_add 4 bytes
3302 * ticket_nonce 1 + ticket_nonce_size bytes
3303 * ticket >=2 bytes
3304 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003305 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 4 + 4 + 1 + ticket_nonce_size + 2);
Jerry Yue67bef42022-07-07 07:29:42 +00003306
3307 /* Generate ticket and ticket_lifetime */
Ronald Cron3c0072b2023-11-22 10:00:14 +01003308#if defined(MBEDTLS_HAVE_TIME)
3309 session->ticket_creation_time = mbedtls_ms_time();
3310#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01003311 ret = ssl->conf->f_ticket_write(ssl->conf->p_ticket,
3312 session,
3313 p + 9 + ticket_nonce_size + 2,
3314 end,
3315 &ticket_len,
3316 &ticket_lifetime);
3317 if (ret != 0) {
3318 MBEDTLS_SSL_DEBUG_RET(1, "write_ticket", ret);
3319 return ret;
Jerry Yue67bef42022-07-07 07:29:42 +00003320 }
Jerry Yuce794882023-11-22 15:01:18 +08003321
3322 /* RFC 8446 section 4.6.1
3323 *
Jerry Yue67bef42022-07-07 07:29:42 +00003324 * ticket_lifetime: Indicates the lifetime in seconds as a 32-bit
Jerry Yuce794882023-11-22 15:01:18 +08003325 * unsigned integer in network byte order from the time of ticket
3326 * issuance. Servers MUST NOT use any value greater than
3327 * 604800 seconds (7 days) ...
Jerry Yue67bef42022-07-07 07:29:42 +00003328 */
Jerry Yu8e0174a2023-11-10 13:58:16 +08003329 if (ticket_lifetime > MBEDTLS_SSL_TLS1_3_MAX_ALLOWED_TICKET_LIFETIME) {
Jerry Yuce794882023-11-22 15:01:18 +08003330 MBEDTLS_SSL_DEBUG_MSG(
3331 1, ("Ticket lifetime (%u) is greater than 7 days.",
3332 (unsigned int) ticket_lifetime));
3333 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Gilles Peskine449bd832023-01-11 14:50:10 +01003334 }
Jerry Yuce794882023-11-22 15:01:18 +08003335
Gilles Peskine449bd832023-01-11 14:50:10 +01003336 MBEDTLS_PUT_UINT32_BE(ticket_lifetime, p, 0);
3337 MBEDTLS_SSL_DEBUG_MSG(3, ("ticket_lifetime: %u",
3338 (unsigned int) ticket_lifetime));
Jerry Yue67bef42022-07-07 07:29:42 +00003339
3340 /* Write ticket_age_add */
Gilles Peskine449bd832023-01-11 14:50:10 +01003341 MBEDTLS_PUT_UINT32_BE(session->ticket_age_add, p, 4);
3342 MBEDTLS_SSL_DEBUG_MSG(3, ("ticket_age_add: %u",
3343 (unsigned int) session->ticket_age_add));
Jerry Yue67bef42022-07-07 07:29:42 +00003344
3345 /* Write ticket_nonce */
Gilles Peskine449bd832023-01-11 14:50:10 +01003346 p[8] = (unsigned char) ticket_nonce_size;
3347 if (ticket_nonce_size > 0) {
3348 memcpy(p + 9, ticket_nonce, ticket_nonce_size);
Jerry Yue67bef42022-07-07 07:29:42 +00003349 }
3350 p += 9 + ticket_nonce_size;
3351
3352 /* Write ticket */
Gilles Peskine449bd832023-01-11 14:50:10 +01003353 MBEDTLS_PUT_UINT16_BE(ticket_len, p, 0);
Jerry Yue67bef42022-07-07 07:29:42 +00003354 p += 2;
Gilles Peskine449bd832023-01-11 14:50:10 +01003355 MBEDTLS_SSL_DEBUG_BUF(4, "ticket", p, ticket_len);
Jerry Yue67bef42022-07-07 07:29:42 +00003356 p += ticket_len;
3357
3358 /* Ticket Extensions
3359 *
Jerry Yu01da35e2022-12-12 15:09:22 +08003360 * Extension extensions<0..2^16-2>;
Jerry Yue67bef42022-07-07 07:29:42 +00003361 */
Jerry Yuedab6372022-10-31 14:37:31 +08003362 ssl->handshake->sent_extensions = MBEDTLS_SSL_EXT_MASK_NONE;
3363
Gilles Peskine449bd832023-01-11 14:50:10 +01003364 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 2);
Jerry Yu01da35e2022-12-12 15:09:22 +08003365 p_extensions_len = p;
Jerry Yue67bef42022-07-07 07:29:42 +00003366 p += 2;
3367
Jerry Yu01da35e2022-12-12 15:09:22 +08003368#if defined(MBEDTLS_SSL_EARLY_DATA)
Pengyu Lv94a42cc2023-12-06 10:04:17 +08003369 if (mbedtls_ssl_tls13_session_ticket_allow_early_data(session)) {
Jerry Yu95648b02023-12-06 15:03:34 +08003370 size_t output_len;
3371
Jerry Yuf135bac2023-11-23 18:10:51 +08003372 if ((ret = mbedtls_ssl_tls13_write_early_data_ext(
Jerry Yuc59c5862023-12-05 10:40:49 +08003373 ssl, 1, p, end, &output_len)) != 0) {
Jerry Yuf135bac2023-11-23 18:10:51 +08003374 MBEDTLS_SSL_DEBUG_RET(
3375 1, "mbedtls_ssl_tls13_write_early_data_ext", ret);
3376 return ret;
3377 }
3378 p += output_len;
Jerry Yu9e7f9bc2023-11-27 16:52:07 +08003379 } else {
3380 MBEDTLS_SSL_DEBUG_MSG(
3381 4, ("early_data not allowed, "
3382 "skip early_data extension in NewSessionTicket"));
Jerry Yu01da35e2022-12-12 15:09:22 +08003383 }
Jerry Yuf135bac2023-11-23 18:10:51 +08003384
Jerry Yu01da35e2022-12-12 15:09:22 +08003385#endif /* MBEDTLS_SSL_EARLY_DATA */
3386
3387 MBEDTLS_PUT_UINT16_BE(p - p_extensions_len - 2, p_extensions_len, 0);
3388
Jerry Yue67bef42022-07-07 07:29:42 +00003389 *out_len = p - buf;
Gilles Peskine449bd832023-01-11 14:50:10 +01003390 MBEDTLS_SSL_DEBUG_BUF(4, "ticket", buf, *out_len);
3391 MBEDTLS_SSL_DEBUG_MSG(2, ("<= write new session ticket"));
Jerry Yue67bef42022-07-07 07:29:42 +00003392
Jerry Yu7de2ff02022-11-08 21:43:46 +08003393 MBEDTLS_SSL_PRINT_EXTS(
Gilles Peskine449bd832023-01-11 14:50:10 +01003394 3, MBEDTLS_SSL_HS_NEW_SESSION_TICKET, ssl->handshake->sent_extensions);
Jerry Yu4b8f2f72022-10-31 13:31:22 +08003395
Gilles Peskine449bd832023-01-11 14:50:10 +01003396 return 0;
Jerry Yue67bef42022-07-07 07:29:42 +00003397}
3398
3399/*
Jerry Yua8d3c502022-10-30 14:51:23 +08003400 * Handler for MBEDTLS_SSL_TLS1_3_NEW_SESSION_TICKET
Jerry Yue67bef42022-07-07 07:29:42 +00003401 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003402static int ssl_tls13_write_new_session_ticket(mbedtls_ssl_context *ssl)
Jerry Yue67bef42022-07-07 07:29:42 +00003403{
3404 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
3405
Gilles Peskine449bd832023-01-11 14:50:10 +01003406 MBEDTLS_SSL_PROC_CHK_NEG(ssl_tls13_write_new_session_ticket_coordinate(ssl));
Jerry Yue67bef42022-07-07 07:29:42 +00003407
Gilles Peskine449bd832023-01-11 14:50:10 +01003408 if (ret == SSL_NEW_SESSION_TICKET_WRITE) {
Jerry Yue67bef42022-07-07 07:29:42 +00003409 unsigned char ticket_nonce[MBEDTLS_SSL_TLS1_3_TICKET_NONCE_LENGTH];
3410 unsigned char *buf;
3411 size_t buf_len, msg_len;
3412
Gilles Peskine449bd832023-01-11 14:50:10 +01003413 MBEDTLS_SSL_PROC_CHK(ssl_tls13_prepare_new_session_ticket(
3414 ssl, ticket_nonce, sizeof(ticket_nonce)));
Jerry Yue67bef42022-07-07 07:29:42 +00003415
Xiaokang Qian9f1747b2023-03-30 02:50:04 +00003416 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_start_handshake_msg(
3417 ssl, MBEDTLS_SSL_HS_NEW_SESSION_TICKET,
3418 &buf, &buf_len));
Jerry Yue67bef42022-07-07 07:29:42 +00003419
Gilles Peskine449bd832023-01-11 14:50:10 +01003420 MBEDTLS_SSL_PROC_CHK(ssl_tls13_write_new_session_ticket_body(
3421 ssl, buf, buf + buf_len, &msg_len,
3422 ticket_nonce, sizeof(ticket_nonce)));
Jerry Yue67bef42022-07-07 07:29:42 +00003423
Gilles Peskine449bd832023-01-11 14:50:10 +01003424 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_finish_handshake_msg(
3425 ssl, buf_len, msg_len));
Jerry Yufca4d572022-07-21 10:37:48 +08003426
Jerry Yu359e65f2022-09-22 23:47:43 +08003427 /* Limit session tickets count to one when resumption connection.
3428 *
3429 * See document of mbedtls_ssl_conf_new_session_tickets.
3430 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003431 if (ssl->handshake->resume == 1) {
Jerry Yu359e65f2022-09-22 23:47:43 +08003432 ssl->handshake->new_session_tickets_count = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01003433 } else {
Jerry Yu359e65f2022-09-22 23:47:43 +08003434 ssl->handshake->new_session_tickets_count--;
Gilles Peskine449bd832023-01-11 14:50:10 +01003435 }
Jerry Yub7e3fa72022-09-22 11:07:18 +08003436
Jerry Yua8d3c502022-10-30 14:51:23 +08003437 mbedtls_ssl_handshake_set_state(
Gilles Peskine449bd832023-01-11 14:50:10 +01003438 ssl, MBEDTLS_SSL_TLS1_3_NEW_SESSION_TICKET_FLUSH);
3439 } else {
3440 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_HANDSHAKE_OVER);
Jerry Yue67bef42022-07-07 07:29:42 +00003441 }
3442
Jerry Yue67bef42022-07-07 07:29:42 +00003443cleanup:
3444
Gilles Peskine449bd832023-01-11 14:50:10 +01003445 return ret;
Jerry Yue67bef42022-07-07 07:29:42 +00003446}
3447#endif /* MBEDTLS_SSL_SESSION_TICKETS */
3448
3449/*
XiaokangQiane8ff3502022-04-22 02:34:40 +00003450 * TLS 1.3 State Machine -- server side
XiaokangQian7807f9f2022-02-15 10:04:37 +00003451 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003452int mbedtls_ssl_tls13_handshake_server_step(mbedtls_ssl_context *ssl)
Jerry Yub9930e72021-08-06 17:11:51 +08003453{
XiaokangQian08037552022-04-20 07:16:41 +00003454 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
XiaokangQian7807f9f2022-02-15 10:04:37 +00003455
Gilles Peskine449bd832023-01-11 14:50:10 +01003456 if (ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER || ssl->handshake == NULL) {
3457 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
3458 }
XiaokangQian7807f9f2022-02-15 10:04:37 +00003459
Gilles Peskine449bd832023-01-11 14:50:10 +01003460 MBEDTLS_SSL_DEBUG_MSG(2, ("tls13 server state: %s(%d)",
Dave Rodgman2eab4622023-10-05 13:30:37 +01003461 mbedtls_ssl_states_str((mbedtls_ssl_states) ssl->state),
Gilles Peskine449bd832023-01-11 14:50:10 +01003462 ssl->state));
Jerry Yu6e81b272021-09-27 11:16:17 +08003463
Gilles Peskine449bd832023-01-11 14:50:10 +01003464 switch (ssl->state) {
XiaokangQian7807f9f2022-02-15 10:04:37 +00003465 /* start state */
3466 case MBEDTLS_SSL_HELLO_REQUEST:
Gilles Peskine449bd832023-01-11 14:50:10 +01003467 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_CLIENT_HELLO);
XiaokangQian08037552022-04-20 07:16:41 +00003468 ret = 0;
XiaokangQian7807f9f2022-02-15 10:04:37 +00003469 break;
3470
XiaokangQian7807f9f2022-02-15 10:04:37 +00003471 case MBEDTLS_SSL_CLIENT_HELLO:
Gilles Peskine449bd832023-01-11 14:50:10 +01003472 ret = ssl_tls13_process_client_hello(ssl);
3473 if (ret != 0) {
3474 MBEDTLS_SSL_DEBUG_RET(1, "ssl_tls13_process_client_hello", ret);
3475 }
Jerry Yuf41553b2022-05-09 22:20:30 +08003476 break;
XiaokangQian7807f9f2022-02-15 10:04:37 +00003477
Jerry Yuf41553b2022-05-09 22:20:30 +08003478 case MBEDTLS_SSL_HELLO_RETRY_REQUEST:
Gilles Peskine449bd832023-01-11 14:50:10 +01003479 ret = ssl_tls13_write_hello_retry_request(ssl);
3480 if (ret != 0) {
3481 MBEDTLS_SSL_DEBUG_RET(1, "ssl_tls13_write_hello_retry_request", ret);
3482 return ret;
Jerry Yuf41553b2022-05-09 22:20:30 +08003483 }
XiaokangQian7807f9f2022-02-15 10:04:37 +00003484 break;
3485
Jerry Yu5b64ae92022-03-30 17:15:02 +08003486 case MBEDTLS_SSL_SERVER_HELLO:
Gilles Peskine449bd832023-01-11 14:50:10 +01003487 ret = ssl_tls13_write_server_hello(ssl);
Jerry Yu5b64ae92022-03-30 17:15:02 +08003488 break;
3489
Xiaofei Baicba64af2022-02-15 10:00:56 +00003490 case MBEDTLS_SSL_ENCRYPTED_EXTENSIONS:
Gilles Peskine449bd832023-01-11 14:50:10 +01003491 ret = ssl_tls13_write_encrypted_extensions(ssl);
3492 if (ret != 0) {
3493 MBEDTLS_SSL_DEBUG_RET(1, "ssl_tls13_write_encrypted_extensions", ret);
3494 return ret;
Xiaofei Baicba64af2022-02-15 10:00:56 +00003495 }
Jerry Yu48330562022-05-06 21:35:44 +08003496 break;
Jerry Yu6a2cd9e2022-05-05 11:14:19 +08003497
Ronald Cron928cbd32022-10-04 16:14:26 +02003498#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED)
Xiaofei Bai9ca09d42022-02-14 12:57:18 +00003499 case MBEDTLS_SSL_CERTIFICATE_REQUEST:
Gilles Peskine449bd832023-01-11 14:50:10 +01003500 ret = ssl_tls13_write_certificate_request(ssl);
Xiaofei Bai9ca09d42022-02-14 12:57:18 +00003501 break;
Xiaofei Bai9ca09d42022-02-14 12:57:18 +00003502
Jerry Yu83da34e2022-04-16 13:59:52 +08003503 case MBEDTLS_SSL_SERVER_CERTIFICATE:
Gilles Peskine449bd832023-01-11 14:50:10 +01003504 ret = ssl_tls13_write_server_certificate(ssl);
Jerry Yu83da34e2022-04-16 13:59:52 +08003505 break;
3506
3507 case MBEDTLS_SSL_CERTIFICATE_VERIFY:
Gilles Peskine449bd832023-01-11 14:50:10 +01003508 ret = ssl_tls13_write_certificate_verify(ssl);
Jerry Yu83da34e2022-04-16 13:59:52 +08003509 break;
Ronald Cron928cbd32022-10-04 16:14:26 +02003510#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED */
Jerry Yu83da34e2022-04-16 13:59:52 +08003511
Gilles Peskine449bd832023-01-11 14:50:10 +01003512 /*
3513 * Injection of dummy-CCS's for middlebox compatibility
3514 */
Gabor Mezei7b39bf12022-05-24 16:04:14 +02003515#if defined(MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE)
Gabor Mezeif7044ea2022-06-28 16:01:49 +02003516 case MBEDTLS_SSL_SERVER_CCS_AFTER_HELLO_RETRY_REQUEST:
Gilles Peskine449bd832023-01-11 14:50:10 +01003517 ret = mbedtls_ssl_tls13_write_change_cipher_spec(ssl);
3518 if (ret == 0) {
3519 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_CLIENT_HELLO);
3520 }
Gabor Mezei7b39bf12022-05-24 16:04:14 +02003521 break;
3522
3523 case MBEDTLS_SSL_SERVER_CCS_AFTER_SERVER_HELLO:
Ronald Crone273f722024-02-13 18:22:26 +01003524 ret = mbedtls_ssl_tls13_write_change_cipher_spec(ssl);
3525 if (ret != 0) {
3526 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01003527 }
Ronald Cronfe59ff72024-01-24 14:31:50 +01003528 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_ENCRYPTED_EXTENSIONS);
Gabor Mezei7b39bf12022-05-24 16:04:14 +02003529 break;
3530#endif /* MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE */
3531
Jerry Yu69dd8d42022-04-16 12:51:26 +08003532 case MBEDTLS_SSL_SERVER_FINISHED:
Gilles Peskine449bd832023-01-11 14:50:10 +01003533 ret = ssl_tls13_write_server_finished(ssl);
Jerry Yu69dd8d42022-04-16 12:51:26 +08003534 break;
3535
Jerry Yu87b5ed42022-12-12 13:07:07 +08003536#if defined(MBEDTLS_SSL_EARLY_DATA)
3537 case MBEDTLS_SSL_END_OF_EARLY_DATA:
Jerry Yu59d420f2023-12-01 16:30:34 +08003538 ret = ssl_tls13_process_end_of_early_data(ssl);
Jerry Yu87b5ed42022-12-12 13:07:07 +08003539 break;
3540#endif /* MBEDTLS_SSL_EARLY_DATA */
3541
Jerry Yu69dd8d42022-04-16 12:51:26 +08003542 case MBEDTLS_SSL_CLIENT_FINISHED:
Gilles Peskine449bd832023-01-11 14:50:10 +01003543 ret = ssl_tls13_process_client_finished(ssl);
Jerry Yu69dd8d42022-04-16 12:51:26 +08003544 break;
3545
Jerry Yu69dd8d42022-04-16 12:51:26 +08003546 case MBEDTLS_SSL_HANDSHAKE_WRAPUP:
Gilles Peskine449bd832023-01-11 14:50:10 +01003547 ret = ssl_tls13_handshake_wrapup(ssl);
Jerry Yu69dd8d42022-04-16 12:51:26 +08003548 break;
3549
Ronald Cron766c0cd2022-10-18 12:17:11 +02003550#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED)
XiaokangQian6b916b12022-04-25 07:29:34 +00003551 case MBEDTLS_SSL_CLIENT_CERTIFICATE:
Gilles Peskine449bd832023-01-11 14:50:10 +01003552 ret = mbedtls_ssl_tls13_process_certificate(ssl);
3553 if (ret == 0) {
3554 if (ssl->session_negotiate->peer_cert != NULL) {
Ronald Cron209cae92022-06-07 10:30:19 +02003555 mbedtls_ssl_handshake_set_state(
Gilles Peskine449bd832023-01-11 14:50:10 +01003556 ssl, MBEDTLS_SSL_CLIENT_CERTIFICATE_VERIFY);
3557 } else {
3558 MBEDTLS_SSL_DEBUG_MSG(2, ("skip parse certificate verify"));
Ronald Cron209cae92022-06-07 10:30:19 +02003559 mbedtls_ssl_handshake_set_state(
Gilles Peskine449bd832023-01-11 14:50:10 +01003560 ssl, MBEDTLS_SSL_CLIENT_FINISHED);
Ronald Cron19385882022-06-15 16:26:13 +02003561 }
XiaokangQian6b916b12022-04-25 07:29:34 +00003562 }
3563 break;
3564
3565 case MBEDTLS_SSL_CLIENT_CERTIFICATE_VERIFY:
Gilles Peskine449bd832023-01-11 14:50:10 +01003566 ret = mbedtls_ssl_tls13_process_certificate_verify(ssl);
3567 if (ret == 0) {
XiaokangQian6b916b12022-04-25 07:29:34 +00003568 mbedtls_ssl_handshake_set_state(
Gilles Peskine449bd832023-01-11 14:50:10 +01003569 ssl, MBEDTLS_SSL_CLIENT_FINISHED);
XiaokangQian6b916b12022-04-25 07:29:34 +00003570 }
3571 break;
Ronald Cron766c0cd2022-10-18 12:17:11 +02003572#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED */
XiaokangQian6b916b12022-04-25 07:29:34 +00003573
Jerry Yue67bef42022-07-07 07:29:42 +00003574#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Jerry Yua8d3c502022-10-30 14:51:23 +08003575 case MBEDTLS_SSL_TLS1_3_NEW_SESSION_TICKET:
Gilles Peskine449bd832023-01-11 14:50:10 +01003576 ret = ssl_tls13_write_new_session_ticket(ssl);
3577 if (ret != 0) {
3578 MBEDTLS_SSL_DEBUG_RET(1,
3579 "ssl_tls13_write_new_session_ticket ",
3580 ret);
Jerry Yue67bef42022-07-07 07:29:42 +00003581 }
3582 break;
Jerry Yua8d3c502022-10-30 14:51:23 +08003583 case MBEDTLS_SSL_TLS1_3_NEW_SESSION_TICKET_FLUSH:
Jerry Yue67bef42022-07-07 07:29:42 +00003584 /* This state is necessary to do the flush of the New Session
Jerry Yua8d3c502022-10-30 14:51:23 +08003585 * Ticket message written in MBEDTLS_SSL_TLS1_3_NEW_SESSION_TICKET
Jerry Yue67bef42022-07-07 07:29:42 +00003586 * as part of ssl_prepare_handshake_step.
3587 */
3588 ret = 0;
Jerry Yud4e75002022-08-09 13:33:50 +08003589
Gilles Peskine449bd832023-01-11 14:50:10 +01003590 if (ssl->handshake->new_session_tickets_count == 0) {
3591 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_HANDSHAKE_OVER);
3592 } else {
Xiaokang Qian9f1747b2023-03-30 02:50:04 +00003593 mbedtls_ssl_handshake_set_state(
3594 ssl, MBEDTLS_SSL_TLS1_3_NEW_SESSION_TICKET);
Gilles Peskine449bd832023-01-11 14:50:10 +01003595 }
Jerry Yue67bef42022-07-07 07:29:42 +00003596 break;
3597
3598#endif /* MBEDTLS_SSL_SESSION_TICKETS */
3599
XiaokangQian7807f9f2022-02-15 10:04:37 +00003600 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01003601 MBEDTLS_SSL_DEBUG_MSG(1, ("invalid state %d", ssl->state));
3602 return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
XiaokangQian7807f9f2022-02-15 10:04:37 +00003603 }
3604
Gilles Peskine449bd832023-01-11 14:50:10 +01003605 return ret;
Jerry Yub9930e72021-08-06 17:11:51 +08003606}
Jerry Yu3cc4c2a2021-08-06 16:29:08 +08003607
Jerry Yufb4b6472022-01-27 15:03:26 +08003608#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_PROTO_TLS1_3 */