blob: 5757d20180584a95ca235d2b93813e8400d23fe4 [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
Manuel Pégourié-Gonnard051b1e22025-03-05 11:53:09 +010095 MBEDTLS_SSL_DEBUG_MSG(1, ("No matched ciphersuite, psk_ciphersuite_id=%x, psk_hash_alg=%lx",
Gilles Peskineeeb4ff52024-06-03 22:16:23 +020096 (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) {
Manuel Pégourié-Gonnard00ad6f62025-02-11 13:19:45 +01001383 MBEDTLS_SSL_DEBUG_MSG(2, ("no supported_versions extension"));
Ronald Cron5af4c7f2023-03-07 20:46:59 +01001384 return SSL_CLIENT_HELLO_TLS1_2;
Ronald Cron8c527d02023-03-07 15:47:47 +01001385 }
1386
Ronald Cron5af4c7f2023-03-07 20:46:59 +01001387 if (ret == 1) {
1388 ret = ssl_tls13_parse_supported_versions_ext(ssl,
Ronald Croneff56732023-04-03 17:36:31 +02001389 supported_versions_data,
1390 supported_versions_data_end);
Ronald Cron5af4c7f2023-03-07 20:46:59 +01001391 if (ret < 0) {
1392 MBEDTLS_SSL_DEBUG_RET(1,
1393 ("ssl_tls13_parse_supported_versions_ext"), ret);
1394 return ret;
1395 }
1396
Ronald Cronb828c7d2023-04-03 16:37:22 +02001397 /*
1398 * The supported versions extension was parsed successfully as the
1399 * value returned by ssl_tls13_parse_supported_versions_ext() is
1400 * positive. The return value is then equal to
1401 * MBEDTLS_SSL_VERSION_TLS1_2 or MBEDTLS_SSL_VERSION_TLS1_3, defining
1402 * the TLS version to negotiate.
1403 */
Ronald Cron5af4c7f2023-03-07 20:46:59 +01001404 if (MBEDTLS_SSL_VERSION_TLS1_2 == ret) {
Manuel Pégourié-Gonnard00ad6f62025-02-11 13:19:45 +01001405 MBEDTLS_SSL_DEBUG_MSG(2, ("supported_versions without 1.3"));
Ronald Cron5af4c7f2023-03-07 20:46:59 +01001406 return SSL_CLIENT_HELLO_TLS1_2;
1407 }
Ronald Cron8c527d02023-03-07 15:47:47 +01001408 }
1409
1410 /*
1411 * We negotiate TLS 1.3.
Ronald Cron64582392023-03-07 09:21:40 +01001412 */
1413 ssl->tls_version = MBEDTLS_SSL_VERSION_TLS1_3;
Ronald Cron64582392023-03-07 09:21:40 +01001414 ssl->session_negotiate->tls_version = MBEDTLS_SSL_VERSION_TLS1_3;
1415 ssl->session_negotiate->endpoint = ssl->conf->endpoint;
Ronald Cron64582392023-03-07 09:21:40 +01001416
Gilles Peskine57dbd692024-08-26 12:04:39 +02001417 /* Before doing any crypto, make sure we can. */
1418 ret = mbedtls_ssl_tls13_crypto_init(ssl);
1419 if (ret != 0) {
1420 return ret;
1421 }
1422
Ronald Cron64582392023-03-07 09:21:40 +01001423 /*
Ronald Crondad02b22023-04-06 09:57:52 +02001424 * We are negotiating the version 1.3 of the protocol. Do what we have
Ronald Croncada4102023-03-07 09:51:39 +01001425 * postponed: copy of the client random bytes, copy of the legacy session
Ronald Cron2f16b4e2023-03-07 10:07:32 +01001426 * identifier and selection of the TLS 1.3 cipher suite.
Ronald Crond540d992023-03-07 09:41:48 +01001427 */
1428 MBEDTLS_SSL_DEBUG_BUF(3, "client hello, random bytes",
1429 random, MBEDTLS_CLIENT_HELLO_RANDOM_LEN);
1430 memcpy(&handshake->randbytes[0], random, MBEDTLS_CLIENT_HELLO_RANDOM_LEN);
1431
Ronald Croncada4102023-03-07 09:51:39 +01001432 if (legacy_session_id_len > sizeof(ssl->session_negotiate->id)) {
1433 MBEDTLS_SSL_DEBUG_MSG(1, ("bad client hello message"));
1434 return MBEDTLS_ERR_SSL_DECODE_ERROR;
1435 }
1436 ssl->session_negotiate->id_len = legacy_session_id_len;
1437 MBEDTLS_SSL_DEBUG_BUF(3, "client hello, session id",
1438 legacy_session_id, legacy_session_id_len);
1439 memcpy(&ssl->session_negotiate->id[0],
1440 legacy_session_id, legacy_session_id_len);
1441
Ronald Crond540d992023-03-07 09:41:48 +01001442 /*
Jerry Yu5725f1c2022-08-21 17:27:16 +08001443 * Search for a matching ciphersuite
1444 */
Ronald Cron2f16b4e2023-03-07 10:07:32 +01001445 MBEDTLS_SSL_DEBUG_BUF(3, "client hello, list of cipher suites",
1446 cipher_suites, cipher_suites_len);
Jerry Yu5725f1c2022-08-21 17:27:16 +08001447
Ronald Cron89089cc2024-02-14 18:18:08 +01001448 ssl_tls13_select_ciphersuite(ssl, cipher_suites, cipher_suites_end,
1449 0, PSA_ALG_NONE, &handshake->ciphersuite_info);
Jerry Yu5725f1c2022-08-21 17:27:16 +08001450
Gilles Peskine449bd832023-01-11 14:50:10 +01001451 if (handshake->ciphersuite_info == NULL) {
1452 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE,
1453 MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE);
1454 return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
Jerry Yu5725f1c2022-08-21 17:27:16 +08001455 }
Ronald Cron89089cc2024-02-14 18:18:08 +01001456 ssl->session_negotiate->ciphersuite = handshake->ciphersuite_info->id;
1457
1458 MBEDTLS_SSL_DEBUG_MSG(2, ("selected ciphersuite: %04x - %s",
1459 ((unsigned) handshake->ciphersuite_info->id),
1460 handshake->ciphersuite_info->name));
Jerry Yuc1be19f2022-04-23 16:11:39 +08001461
XiaokangQian4080a7f2022-04-11 09:55:18 +00001462 /* ...
XiaokangQianb67384d2022-04-19 00:02:38 +00001463 * opaque legacy_compression_methods<1..2^8-1>;
XiaokangQian4080a7f2022-04-11 09:55:18 +00001464 * ...
XiaokangQian7807f9f2022-02-15 10:04:37 +00001465 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001466 if (p[0] != 1 || p[1] != MBEDTLS_SSL_COMPRESS_NULL) {
1467 MBEDTLS_SSL_DEBUG_MSG(1, ("bad legacy compression method"));
1468 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER,
1469 MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER);
1470 return MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001471 }
XiaokangQianb67384d2022-04-19 00:02:38 +00001472 p += 2;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001473
Jerry Yuc1be19f2022-04-23 16:11:39 +08001474 /* ...
XiaokangQianb67384d2022-04-19 00:02:38 +00001475 * Extension extensions<8..2^16-1>;
Jerry Yuc1be19f2022-04-23 16:11:39 +08001476 * ...
XiaokangQianb67384d2022-04-19 00:02:38 +00001477 * with Extension defined as:
1478 * struct {
1479 * ExtensionType extension_type;
1480 * opaque extension_data<0..2^16-1>;
1481 * } Extension;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001482 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001483 extensions_len = MBEDTLS_GET_UINT16_BE(p, 0);
XiaokangQian7807f9f2022-02-15 10:04:37 +00001484 p += 2;
Gilles Peskine449bd832023-01-11 14:50:10 +01001485 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, extensions_len);
XiaokangQiane8ff3502022-04-22 02:34:40 +00001486 extensions_end = p + extensions_len;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001487
Gilles Peskine449bd832023-01-11 14:50:10 +01001488 MBEDTLS_SSL_DEBUG_BUF(3, "client hello extensions", p, extensions_len);
Jerry Yu63a459c2022-10-31 13:38:40 +08001489 handshake->received_extensions = MBEDTLS_SSL_EXT_MASK_NONE;
Jerry Yu0c354a22022-08-29 15:25:36 +08001490
Gilles Peskine449bd832023-01-11 14:50:10 +01001491 while (p < extensions_end) {
XiaokangQian7807f9f2022-02-15 10:04:37 +00001492 unsigned int extension_type;
1493 size_t extension_data_len;
1494 const unsigned char *extension_data_end;
Jerry Yu263dbf72022-10-26 10:51:27 +08001495 uint32_t allowed_exts = MBEDTLS_SSL_TLS1_3_ALLOWED_EXTS_OF_CH;
1496
Ronald Cron5fbd2702024-02-14 10:03:36 +01001497 if (ssl->handshake->hello_retry_request_flag) {
Jerry Yu263dbf72022-10-26 10:51:27 +08001498 /* Do not accept early data extension in 2nd ClientHello */
1499 allowed_exts &= ~MBEDTLS_SSL_EXT_MASK(EARLY_DATA);
1500 }
XiaokangQian7807f9f2022-02-15 10:04:37 +00001501
Jerry Yu0c354a22022-08-29 15:25:36 +08001502 /* RFC 8446, section 4.2.11
Jerry Yu1c9247c2022-07-21 12:37:39 +08001503 *
1504 * The "pre_shared_key" extension MUST be the last extension in the
1505 * ClientHello (this facilitates implementation as described below).
1506 * Servers MUST check that it is the last extension and otherwise fail
1507 * the handshake with an "illegal_parameter" alert.
1508 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001509 if (handshake->received_extensions & MBEDTLS_SSL_EXT_MASK(PRE_SHARED_KEY)) {
Jerry Yu1c9247c2022-07-21 12:37:39 +08001510 MBEDTLS_SSL_DEBUG_MSG(
Gilles Peskine449bd832023-01-11 14:50:10 +01001511 3, ("pre_shared_key is not last extension."));
Jerry Yu1c9247c2022-07-21 12:37:39 +08001512 MBEDTLS_SSL_PEND_FATAL_ALERT(
1513 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER,
Gilles Peskine449bd832023-01-11 14:50:10 +01001514 MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER);
1515 return MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER;
Jerry Yu1c9247c2022-07-21 12:37:39 +08001516 }
1517
Gilles Peskine449bd832023-01-11 14:50:10 +01001518 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, extensions_end, 4);
1519 extension_type = MBEDTLS_GET_UINT16_BE(p, 0);
1520 extension_data_len = MBEDTLS_GET_UINT16_BE(p, 2);
XiaokangQian7807f9f2022-02-15 10:04:37 +00001521 p += 4;
1522
Gilles Peskine449bd832023-01-11 14:50:10 +01001523 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, extensions_end, extension_data_len);
XiaokangQian7807f9f2022-02-15 10:04:37 +00001524 extension_data_end = p + extension_data_len;
1525
Jerry Yuc4bf5d62022-10-29 09:08:47 +08001526 ret = mbedtls_ssl_tls13_check_received_extension(
Gilles Peskine449bd832023-01-11 14:50:10 +01001527 ssl, MBEDTLS_SSL_HS_CLIENT_HELLO, extension_type,
Jerry Yu263dbf72022-10-26 10:51:27 +08001528 allowed_exts);
Gilles Peskine449bd832023-01-11 14:50:10 +01001529 if (ret != 0) {
1530 return ret;
1531 }
Jerry Yue18dc7e2022-08-04 16:29:22 +08001532
Gilles Peskine449bd832023-01-11 14:50:10 +01001533 switch (extension_type) {
XiaokangQian40a35232022-05-07 09:02:40 +00001534#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
1535 case MBEDTLS_TLS_EXT_SERVERNAME:
Gilles Peskine449bd832023-01-11 14:50:10 +01001536 MBEDTLS_SSL_DEBUG_MSG(3, ("found ServerName extension"));
1537 ret = mbedtls_ssl_parse_server_name_ext(ssl, p,
1538 extension_data_end);
1539 if (ret != 0) {
XiaokangQian40a35232022-05-07 09:02:40 +00001540 MBEDTLS_SSL_DEBUG_RET(
Gilles Peskine449bd832023-01-11 14:50:10 +01001541 1, "mbedtls_ssl_parse_servername_ext", ret);
1542 return ret;
XiaokangQian40a35232022-05-07 09:02:40 +00001543 }
XiaokangQian40a35232022-05-07 09:02:40 +00001544 break;
1545#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
1546
Przemek Stekiel8c0a9532023-06-15 16:48:19 +02001547#if defined(PSA_WANT_ALG_ECDH) || defined(PSA_WANT_ALG_FFDH)
XiaokangQian7807f9f2022-02-15 10:04:37 +00001548 case MBEDTLS_TLS_EXT_SUPPORTED_GROUPS:
Gilles Peskine449bd832023-01-11 14:50:10 +01001549 MBEDTLS_SSL_DEBUG_MSG(3, ("found supported group extension"));
XiaokangQian7807f9f2022-02-15 10:04:37 +00001550
1551 /* Supported Groups Extension
1552 *
1553 * When sent by the client, the "supported_groups" extension
1554 * indicates the named groups which the client supports,
1555 * ordered from most preferred to least preferred.
1556 */
Jerry Yuc1be19f2022-04-23 16:11:39 +08001557 ret = ssl_tls13_parse_supported_groups_ext(
Gilles Peskine449bd832023-01-11 14:50:10 +01001558 ssl, p, extension_data_end);
1559 if (ret != 0) {
Xiaokang Qian9f1747b2023-03-30 02:50:04 +00001560 MBEDTLS_SSL_DEBUG_RET(
Xiaokang Qian8bce0e62023-04-04 10:15:48 +00001561 1, "ssl_tls13_parse_supported_groups_ext", ret);
Gilles Peskine449bd832023-01-11 14:50:10 +01001562 return ret;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001563 }
1564
XiaokangQian7807f9f2022-02-15 10:04:37 +00001565 break;
Przemek Stekiel8c0a9532023-06-15 16:48:19 +02001566#endif /* PSA_WANT_ALG_ECDH || PSA_WANT_ALG_FFDH*/
XiaokangQian7807f9f2022-02-15 10:04:37 +00001567
Valerio Settic9ae8622023-07-25 11:23:50 +02001568#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_EPHEMERAL_ENABLED)
XiaokangQian7807f9f2022-02-15 10:04:37 +00001569 case MBEDTLS_TLS_EXT_KEY_SHARE:
Gilles Peskine449bd832023-01-11 14:50:10 +01001570 MBEDTLS_SSL_DEBUG_MSG(3, ("found key share extension"));
XiaokangQian7807f9f2022-02-15 10:04:37 +00001571
1572 /*
1573 * Key Share Extension
1574 *
1575 * When sent by the client, the "key_share" extension
1576 * contains the endpoint's cryptographic parameters for
1577 * ECDHE/DHE key establishment methods.
1578 */
Jerry Yuc1be19f2022-04-23 16:11:39 +08001579 ret = ssl_tls13_parse_key_shares_ext(
Gilles Peskine449bd832023-01-11 14:50:10 +01001580 ssl, p, extension_data_end);
1581 if (ret == SSL_TLS1_3_PARSE_KEY_SHARES_EXT_NO_MATCH) {
Ronald Cron8a74f072023-06-14 17:59:29 +02001582 MBEDTLS_SSL_DEBUG_MSG(2, ("No usable share for key agreement."));
1583 no_usable_share_for_key_agreement = 1;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001584 }
1585
Gilles Peskine449bd832023-01-11 14:50:10 +01001586 if (ret < 0) {
Jerry Yu582dd062022-04-22 21:59:01 +08001587 MBEDTLS_SSL_DEBUG_RET(
Gilles Peskine449bd832023-01-11 14:50:10 +01001588 1, "ssl_tls13_parse_key_shares_ext", ret);
1589 return ret;
Jerry Yu582dd062022-04-22 21:59:01 +08001590 }
XiaokangQian7807f9f2022-02-15 10:04:37 +00001591
XiaokangQian7807f9f2022-02-15 10:04:37 +00001592 break;
Valerio Settic9ae8622023-07-25 11:23:50 +02001593#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_EPHEMERAL_ENABLED */
XiaokangQian7807f9f2022-02-15 10:04:37 +00001594
1595 case MBEDTLS_TLS_EXT_SUPPORTED_VERSIONS:
Ronald Cron8c527d02023-03-07 15:47:47 +01001596 /* Already parsed */
XiaokangQian7807f9f2022-02-15 10:04:37 +00001597 break;
1598
Ronald Cron41a443a2022-10-04 16:38:25 +02001599#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED)
Jerry Yue19e3b92022-07-08 12:04:51 +00001600 case MBEDTLS_TLS_EXT_PSK_KEY_EXCHANGE_MODES:
Xiaokang Qian9f1747b2023-03-30 02:50:04 +00001601 MBEDTLS_SSL_DEBUG_MSG(
1602 3, ("found psk key exchange modes extension"));
Jerry Yue19e3b92022-07-08 12:04:51 +00001603
1604 ret = ssl_tls13_parse_key_exchange_modes_ext(
Gilles Peskine449bd832023-01-11 14:50:10 +01001605 ssl, p, extension_data_end);
1606 if (ret != 0) {
Jerry Yue19e3b92022-07-08 12:04:51 +00001607 MBEDTLS_SSL_DEBUG_RET(
Gilles Peskine449bd832023-01-11 14:50:10 +01001608 1, "ssl_tls13_parse_key_exchange_modes_ext", ret);
1609 return ret;
Jerry Yue19e3b92022-07-08 12:04:51 +00001610 }
1611
Jerry Yue19e3b92022-07-08 12:04:51 +00001612 break;
Ronald Cron41a443a2022-10-04 16:38:25 +02001613#endif
Jerry Yue19e3b92022-07-08 12:04:51 +00001614
Jerry Yu1c105562022-07-10 06:32:38 +00001615 case MBEDTLS_TLS_EXT_PRE_SHARED_KEY:
Gilles Peskine449bd832023-01-11 14:50:10 +01001616 MBEDTLS_SSL_DEBUG_MSG(3, ("found pre_shared_key extension"));
1617 if ((handshake->received_extensions &
1618 MBEDTLS_SSL_EXT_MASK(PSK_KEY_EXCHANGE_MODES)) == 0) {
Jerry Yu13ab81d2022-07-22 23:17:11 +08001619 MBEDTLS_SSL_PEND_FATAL_ALERT(
1620 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER,
Gilles Peskine449bd832023-01-11 14:50:10 +01001621 MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER);
1622 return MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER;
Jerry Yu13ab81d2022-07-22 23:17:11 +08001623 }
Ronald Cron41a443a2022-10-04 16:38:25 +02001624#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED)
Jerry Yu1c105562022-07-10 06:32:38 +00001625 /* Delay processing of the PSK identity once we have
1626 * found out which algorithms to use. We keep a pointer
1627 * to the buffer and the size for later processing.
1628 */
Jerry Yu29d9faa2022-08-23 17:52:45 +08001629 pre_shared_key_ext = p;
Jerry Yu1c105562022-07-10 06:32:38 +00001630 pre_shared_key_ext_end = extension_data_end;
Jerry Yue18dc7e2022-08-04 16:29:22 +08001631#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED */
Jerry Yu1c105562022-07-10 06:32:38 +00001632 break;
Jerry Yu1c105562022-07-10 06:32:38 +00001633
XiaokangQianacb39922022-06-17 10:18:48 +00001634#if defined(MBEDTLS_SSL_ALPN)
1635 case MBEDTLS_TLS_EXT_ALPN:
Gilles Peskine449bd832023-01-11 14:50:10 +01001636 MBEDTLS_SSL_DEBUG_MSG(3, ("found alpn extension"));
XiaokangQianacb39922022-06-17 10:18:48 +00001637
Gilles Peskine449bd832023-01-11 14:50:10 +01001638 ret = mbedtls_ssl_parse_alpn_ext(ssl, p, extension_data_end);
1639 if (ret != 0) {
XiaokangQianacb39922022-06-17 10:18:48 +00001640 MBEDTLS_SSL_DEBUG_RET(
Gilles Peskine449bd832023-01-11 14:50:10 +01001641 1, ("mbedtls_ssl_parse_alpn_ext"), ret);
1642 return ret;
XiaokangQianacb39922022-06-17 10:18:48 +00001643 }
XiaokangQianacb39922022-06-17 10:18:48 +00001644 break;
1645#endif /* MBEDTLS_SSL_ALPN */
1646
Ronald Cron928cbd32022-10-04 16:14:26 +02001647#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED)
XiaokangQian7807f9f2022-02-15 10:04:37 +00001648 case MBEDTLS_TLS_EXT_SIG_ALG:
Gilles Peskine449bd832023-01-11 14:50:10 +01001649 MBEDTLS_SSL_DEBUG_MSG(3, ("found signature_algorithms extension"));
XiaokangQian7807f9f2022-02-15 10:04:37 +00001650
Gabor Mezei078e8032022-04-27 21:17:56 +02001651 ret = mbedtls_ssl_parse_sig_alg_ext(
Gilles Peskine449bd832023-01-11 14:50:10 +01001652 ssl, p, extension_data_end);
1653 if (ret != 0) {
Xiaokang Qian49f39c12023-04-06 02:31:02 +00001654 MBEDTLS_SSL_DEBUG_RET(
1655 1, "mbedtls_ssl_parse_sig_alg_ext", ret);
Gilles Peskine449bd832023-01-11 14:50:10 +01001656 return ret;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001657 }
XiaokangQian7807f9f2022-02-15 10:04:37 +00001658 break;
Ronald Cron928cbd32022-10-04 16:14:26 +02001659#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED */
XiaokangQian7807f9f2022-02-15 10:04:37 +00001660
Jan Bruckner151f6422023-02-10 12:45:19 +01001661#if defined(MBEDTLS_SSL_RECORD_SIZE_LIMIT)
1662 case MBEDTLS_TLS_EXT_RECORD_SIZE_LIMIT:
1663 MBEDTLS_SSL_DEBUG_MSG(3, ("found record_size_limit extension"));
1664
Xiaokang Qian9f1747b2023-03-30 02:50:04 +00001665 ret = mbedtls_ssl_tls13_parse_record_size_limit_ext(
1666 ssl, p, extension_data_end);
Jan Brucknerf482dcc2023-03-15 09:09:06 +01001667 if (ret != 0) {
1668 MBEDTLS_SSL_DEBUG_RET(
1669 1, ("mbedtls_ssl_tls13_parse_record_size_limit_ext"), ret);
1670 return ret;
1671 }
Jan Bruckner151f6422023-02-10 12:45:19 +01001672 break;
1673#endif /* MBEDTLS_SSL_RECORD_SIZE_LIMIT */
1674
XiaokangQian7807f9f2022-02-15 10:04:37 +00001675 default:
Jerry Yu79aa7212022-11-08 21:30:21 +08001676 MBEDTLS_SSL_PRINT_EXT(
Jerry Yu63a459c2022-10-31 13:38:40 +08001677 3, MBEDTLS_SSL_HS_CLIENT_HELLO,
Gilles Peskine449bd832023-01-11 14:50:10 +01001678 extension_type, "( ignored )");
Jerry Yu0c354a22022-08-29 15:25:36 +08001679 break;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001680 }
1681
1682 p += extension_data_len;
1683 }
1684
Gilles Peskine449bd832023-01-11 14:50:10 +01001685 MBEDTLS_SSL_PRINT_EXTS(3, MBEDTLS_SSL_HS_CLIENT_HELLO,
1686 handshake->received_extensions);
Jerry Yue18dc7e2022-08-04 16:29:22 +08001687
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +01001688 ret = mbedtls_ssl_add_hs_hdr_to_checksum(ssl,
1689 MBEDTLS_SSL_HS_CLIENT_HELLO,
1690 p - buf);
1691 if (0 != ret) {
1692 MBEDTLS_SSL_DEBUG_RET(1, ("mbedtls_ssl_add_hs_hdr_to_checksum"), ret);
1693 return ret;
1694 }
Jerry Yu032b15ce2022-07-11 06:10:03 +00001695
Ronald Cron41a443a2022-10-04 16:38:25 +02001696#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED)
XiaokangQian7807f9f2022-02-15 10:04:37 +00001697 /* Update checksum with either
1698 * - The entire content of the CH message, if no PSK extension is present
1699 * - The content up to but excluding the PSK extension, if present.
Ronald Cron7cab4f82024-03-07 15:09:09 +01001700 * Always parse the pre-shared-key extension when present in the
Ronald Cron12e72f12024-02-14 15:49:38 +01001701 * ClientHello even if some pre-requisites for PSK key exchange modes are
1702 * not met. That way we always validate the syntax of the extension.
XiaokangQian7807f9f2022-02-15 10:04:37 +00001703 */
Ronald Cron12e72f12024-02-14 15:49:38 +01001704 if (handshake->received_extensions & MBEDTLS_SSL_EXT_MASK(PRE_SHARED_KEY)) {
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +01001705 ret = handshake->update_checksum(ssl, buf,
1706 pre_shared_key_ext - buf);
1707 if (0 != ret) {
1708 MBEDTLS_SSL_DEBUG_RET(1, ("update_checksum"), ret);
1709 return ret;
1710 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001711 ret = ssl_tls13_parse_pre_shared_key_ext(ssl,
1712 pre_shared_key_ext,
1713 pre_shared_key_ext_end,
1714 cipher_suites,
Ronald Cron79cdd412024-02-20 17:19:28 +01001715 cipher_suites_end,
1716 &psk);
1717 if (ret == 0) {
1718 got_psk = 1;
1719 } else if (ret != MBEDTLS_ERR_SSL_UNKNOWN_IDENTITY) {
Jerry Yu63a459c2022-10-31 13:38:40 +08001720 MBEDTLS_SSL_DEBUG_RET(
Gilles Peskine449bd832023-01-11 14:50:10 +01001721 1, "ssl_tls13_parse_pre_shared_key_ext", ret);
1722 return ret;
Jerry Yu1c105562022-07-10 06:32:38 +00001723 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001724 } else
Ronald Cron41a443a2022-10-04 16:38:25 +02001725#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED */
Jerry Yu1c105562022-07-10 06:32:38 +00001726 {
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +01001727 ret = handshake->update_checksum(ssl, buf, p - buf);
1728 if (0 != ret) {
1729 MBEDTLS_SSL_DEBUG_RET(1, ("update_checksum"), ret);
1730 return ret;
1731 }
Jerry Yu1c105562022-07-10 06:32:38 +00001732 }
XiaokangQian7807f9f2022-02-15 10:04:37 +00001733
Ronald Cron79cdd412024-02-20 17:19:28 +01001734 /*
1735 * Determine the key exchange algorithm to use.
1736 * There are three types of key exchanges supported in TLS 1.3:
1737 * - (EC)DH with ECDSA,
1738 * - (EC)DH with PSK,
1739 * - plain PSK.
1740 *
1741 * The PSK-based key exchanges may additionally be used with 0-RTT.
1742 *
1743 * Our built-in order of preference is
1744 * 1 ) (EC)DHE-PSK Mode ( psk_ephemeral )
1745 * 2 ) Certificate Mode ( ephemeral )
1746 * 3 ) Plain PSK Mode ( psk )
1747 */
1748#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED)
1749 if (got_psk && (psk.key_exchange_mode ==
1750 MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK_EPHEMERAL)) {
1751 handshake->key_exchange_mode =
1752 MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK_EPHEMERAL;
1753 MBEDTLS_SSL_DEBUG_MSG(2, ("key exchange mode: psk_ephemeral"));
1754
1755 } else
1756#endif
1757 if (ssl_tls13_key_exchange_is_ephemeral_available(ssl)) {
1758 handshake->key_exchange_mode =
1759 MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL;
1760 MBEDTLS_SSL_DEBUG_MSG(2, ("key exchange mode: ephemeral"));
1761
1762 }
1763#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED)
1764 else if (got_psk && (psk.key_exchange_mode ==
1765 MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK)) {
1766 handshake->key_exchange_mode = MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK;
1767 MBEDTLS_SSL_DEBUG_MSG(2, ("key exchange mode: psk"));
1768 }
1769#endif
1770 else {
1771 MBEDTLS_SSL_DEBUG_MSG(
1772 1,
1773 ("ClientHello message misses mandatory extensions."));
1774 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_MISSING_EXTENSION,
1775 MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER);
1776 return MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER;
Gilles Peskine449bd832023-01-11 14:50:10 +01001777 }
XiaokangQian7807f9f2022-02-15 10:04:37 +00001778
Ronald Cron3e47eec2024-02-21 10:29:24 +01001779#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED)
Ronald Cron74a16292024-02-23 17:07:41 +01001780 if (handshake->key_exchange_mode &
1781 MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK_ALL) {
1782 handshake->ciphersuite_info = psk.ciphersuite_info;
1783 ssl->session_negotiate->ciphersuite = psk.ciphersuite_info->id;
1784
1785 MBEDTLS_SSL_DEBUG_MSG(2, ("Select PSK ciphersuite: %04x - %s",
1786 ((unsigned) psk.ciphersuite_info->id),
1787 psk.ciphersuite_info->name));
1788
1789 if (psk.type == MBEDTLS_SSL_TLS1_3_PSK_RESUMPTION) {
1790 handshake->resume = 1;
1791 }
Ronald Cron79cdd412024-02-20 17:19:28 +01001792 }
Ronald Cron3e47eec2024-02-21 10:29:24 +01001793#endif
Ronald Cron79cdd412024-02-20 17:19:28 +01001794
1795 if (handshake->key_exchange_mode !=
Ronald Cron8a74f072023-06-14 17:59:29 +02001796 MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK) {
1797 hrr_required = (no_usable_share_for_key_agreement != 0);
1798 }
1799
Gilles Peskine449bd832023-01-11 14:50:10 +01001800 mbedtls_ssl_optimize_checksum(ssl, handshake->ciphersuite_info);
Jerry Yue5834fd2022-08-29 20:16:09 +08001801
Gilles Peskine449bd832023-01-11 14:50:10 +01001802 return hrr_required ? SSL_CLIENT_HELLO_HRR_REQUIRED : SSL_CLIENT_HELLO_OK;
XiaokangQian75fe8c72022-06-15 09:42:45 +00001803}
1804
Jerry Yuab0da372023-02-08 13:55:24 +08001805#if defined(MBEDTLS_SSL_EARLY_DATA)
Jerry Yu4caf3ca2023-11-15 16:13:47 +08001806static int ssl_tls13_check_early_data_requirements(mbedtls_ssl_context *ssl)
Jerry Yuab0da372023-02-08 13:55:24 +08001807{
1808 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
1809
Jerry Yu985c9672022-12-04 14:06:30 +08001810 if (ssl->conf->early_data_enabled == MBEDTLS_SSL_EARLY_DATA_DISABLED) {
1811 MBEDTLS_SSL_DEBUG_MSG(
Jerry Yu985c9672022-12-04 14:06:30 +08001812 1,
Jerry Yu454dda32023-10-31 15:13:54 +08001813 ("EarlyData: rejected, feature disabled in server configuration."));
Jerry Yu4caf3ca2023-11-15 16:13:47 +08001814 return -1;
Ronald Cron7b6ee942024-01-12 10:29:55 +01001815 }
1816
Jerry Yu454dda32023-10-31 15:13:54 +08001817 if (!handshake->resume) {
1818 /* We currently support early data only in the case of PSKs established
1819 via a NewSessionTicket message thus in the case of a session
1820 resumption. */
Jerry Yu985c9672022-12-04 14:06:30 +08001821 MBEDTLS_SSL_DEBUG_MSG(
Jerry Yu7ef9fd82023-11-07 14:30:38 +08001822 1, ("EarlyData: rejected, not a session resumption."));
Jerry Yu4caf3ca2023-11-15 16:13:47 +08001823 return -1;
Jerry Yu985c9672022-12-04 14:06:30 +08001824 }
1825
Jerry Yu82fd6c12023-10-31 16:32:19 +08001826 /* RFC 8446 4.2.10
1827 *
1828 * In order to accept early data, the server MUST have accepted a PSK cipher
1829 * suite and selected the first key offered in the client's "pre_shared_key"
1830 * extension. In addition, it MUST verify that the following values are the
1831 * same as those associated with the selected PSK:
1832 * - The TLS version number
1833 * - The selected cipher suite
1834 * - The selected ALPN [RFC7301] protocol, if any
1835 *
1836 * NOTE:
Jerry Yu7ef9fd82023-11-07 14:30:38 +08001837 * - The TLS version number is checked in
1838 * ssl_tls13_offered_psks_check_identity_match_ticket().
Jerry Yu82fd6c12023-10-31 16:32:19 +08001839 */
1840
1841 if (handshake->selected_identity != 0) {
1842 MBEDTLS_SSL_DEBUG_MSG(
Jerry Yu7ef9fd82023-11-07 14:30:38 +08001843 1, ("EarlyData: rejected, the selected key in "
1844 "`pre_shared_key` is not the first one."));
Jerry Yu4caf3ca2023-11-15 16:13:47 +08001845 return -1;
Jerry Yu82fd6c12023-10-31 16:32:19 +08001846 }
1847
1848 if (handshake->ciphersuite_info->id !=
1849 ssl->session_negotiate->ciphersuite) {
1850 MBEDTLS_SSL_DEBUG_MSG(
Jerry Yu7ef9fd82023-11-07 14:30:38 +08001851 1, ("EarlyData: rejected, the selected ciphersuite is not the one "
1852 "of the selected pre-shared key."));
Jerry Yu4caf3ca2023-11-15 16:13:47 +08001853 return -1;
Jerry Yu82fd6c12023-10-31 16:32:19 +08001854
1855 }
Jerry Yu985c9672022-12-04 14:06:30 +08001856
Pengyu Lv94a42cc2023-12-06 10:04:17 +08001857 if (!mbedtls_ssl_tls13_session_ticket_allow_early_data(ssl->session_negotiate)) {
Jerry Yufceddb32022-12-12 15:30:34 +08001858 MBEDTLS_SSL_DEBUG_MSG(
1859 1,
Jerry Yuea96ac32023-11-21 17:06:36 +08001860 ("EarlyData: rejected, early_data not allowed in ticket "
1861 "permission bits."));
Jerry Yu4caf3ca2023-11-15 16:13:47 +08001862 return -1;
Jerry Yufceddb32022-12-12 15:30:34 +08001863 }
Jerry Yu985c9672022-12-04 14:06:30 +08001864
Waleed Elmelegy4dfb0e72024-03-14 01:48:40 +00001865#if defined(MBEDTLS_SSL_ALPN)
1866 const char *alpn = mbedtls_ssl_get_alpn_protocol(ssl);
1867 size_t alpn_len;
1868
1869 if (alpn == NULL && ssl->session_negotiate->ticket_alpn == NULL) {
1870 return 0;
1871 }
1872
1873 if (alpn != NULL) {
1874 alpn_len = strlen(alpn);
1875 }
1876
1877 if (alpn == NULL ||
1878 ssl->session_negotiate->ticket_alpn == NULL ||
1879 alpn_len != strlen(ssl->session_negotiate->ticket_alpn) ||
1880 (memcmp(alpn, ssl->session_negotiate->ticket_alpn, alpn_len) != 0)) {
1881 MBEDTLS_SSL_DEBUG_MSG(1, ("EarlyData: rejected, the selected ALPN is different "
1882 "from the one associated with the pre-shared key."));
1883 return -1;
1884 }
1885#endif
1886
Jerry Yu4caf3ca2023-11-15 16:13:47 +08001887 return 0;
Jerry Yuab0da372023-02-08 13:55:24 +08001888}
1889#endif /* MBEDTLS_SSL_EARLY_DATA */
1890
XiaokangQian75fe8c72022-06-15 09:42:45 +00001891/* Update the handshake state machine */
1892
Ronald Cronce7d76e2022-07-08 18:56:49 +02001893MBEDTLS_CHECK_RETURN_CRITICAL
Ronald Cron7b6ee942024-01-12 10:29:55 +01001894static int ssl_tls13_postprocess_client_hello(mbedtls_ssl_context *ssl,
1895 int hrr_required)
XiaokangQian75fe8c72022-06-15 09:42:45 +00001896{
1897 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1898
XiaokangQian7807f9f2022-02-15 10:04:37 +00001899 /*
XiaokangQianfb665a82022-06-15 03:57:21 +00001900 * Server certificate selection
1901 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001902 if (ssl->conf->f_cert_cb && (ret = ssl->conf->f_cert_cb(ssl)) != 0) {
1903 MBEDTLS_SSL_DEBUG_RET(1, "f_cert_cb", ret);
1904 return ret;
XiaokangQianfb665a82022-06-15 03:57:21 +00001905 }
1906#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
1907 ssl->handshake->sni_name = NULL;
1908 ssl->handshake->sni_name_len = 0;
1909#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
XiaokangQian7807f9f2022-02-15 10:04:37 +00001910
Gilles Peskine449bd832023-01-11 14:50:10 +01001911 ret = mbedtls_ssl_tls13_key_schedule_stage_early(ssl);
1912 if (ret != 0) {
1913 MBEDTLS_SSL_DEBUG_RET(1,
1914 "mbedtls_ssl_tls1_3_key_schedule_stage_early", ret);
1915 return ret;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001916 }
1917
Jerry Yuab0da372023-02-08 13:55:24 +08001918#if defined(MBEDTLS_SSL_EARLY_DATA)
Jerry Yu4caf3ca2023-11-15 16:13:47 +08001919 if (ssl->handshake->received_extensions & MBEDTLS_SSL_EXT_MASK(EARLY_DATA)) {
Ronald Cron31e2d832024-02-05 16:45:57 +01001920 ssl->handshake->early_data_accepted =
1921 (!hrr_required) && (ssl_tls13_check_early_data_requirements(ssl) == 0);
Jerry Yu4e9b70e2022-12-04 14:08:02 +08001922
Jerry Yu4caf3ca2023-11-15 16:13:47 +08001923 if (ssl->handshake->early_data_accepted) {
1924 ret = mbedtls_ssl_tls13_compute_early_transform(ssl);
1925 if (ret != 0) {
1926 MBEDTLS_SSL_DEBUG_RET(
1927 1, "mbedtls_ssl_tls13_compute_early_transform", ret);
1928 return ret;
1929 }
1930 } else {
1931 ssl->discard_early_data_record =
1932 hrr_required ?
1933 MBEDTLS_SSL_EARLY_DATA_DISCARD :
1934 MBEDTLS_SSL_EARLY_DATA_TRY_TO_DEPROTECT_AND_DISCARD;
Jerry Yu4e9b70e2022-12-04 14:08:02 +08001935 }
1936 }
Ronald Cron7b6ee942024-01-12 10:29:55 +01001937#else
1938 ((void) hrr_required);
Jerry Yuab0da372023-02-08 13:55:24 +08001939#endif /* MBEDTLS_SSL_EARLY_DATA */
1940
Gilles Peskine449bd832023-01-11 14:50:10 +01001941 return 0;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001942}
1943
1944/*
XiaokangQianed582dd2022-04-13 08:21:05 +00001945 * Main entry point from the state machine; orchestrates the otherfunctions.
1946 */
1947
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001948MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01001949static int ssl_tls13_process_client_hello(mbedtls_ssl_context *ssl)
XiaokangQianed582dd2022-04-13 08:21:05 +00001950{
1951
XiaokangQian08037552022-04-20 07:16:41 +00001952 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine449bd832023-01-11 14:50:10 +01001953 unsigned char *buf = NULL;
XiaokangQianed582dd2022-04-13 08:21:05 +00001954 size_t buflen = 0;
Jerry Yu4ca91402022-05-09 15:50:57 +08001955 int parse_client_hello_ret;
1956
Gilles Peskine449bd832023-01-11 14:50:10 +01001957 MBEDTLS_SSL_DEBUG_MSG(2, ("=> parse client hello"));
XiaokangQianed582dd2022-04-13 08:21:05 +00001958
Gilles Peskine449bd832023-01-11 14:50:10 +01001959 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_tls13_fetch_handshake_msg(
1960 ssl, MBEDTLS_SSL_HS_CLIENT_HELLO,
1961 &buf, &buflen));
XiaokangQianed582dd2022-04-13 08:21:05 +00001962
Gilles Peskine449bd832023-01-11 14:50:10 +01001963 MBEDTLS_SSL_PROC_CHK_NEG(ssl_tls13_parse_client_hello(ssl, buf,
1964 buf + buflen));
Ronald Cron5af4c7f2023-03-07 20:46:59 +01001965 parse_client_hello_ret = ret; /* Store positive return value of
1966 * parse_client_hello,
1967 * as negative error codes are handled
Jerry Yuf41553b2022-05-09 22:20:30 +08001968 * by MBEDTLS_SSL_PROC_CHK_NEG. */
Jerry Yu4ca91402022-05-09 15:50:57 +08001969
Ronald Cronb828c7d2023-04-03 16:37:22 +02001970 /*
Yanray Wang90acdc62023-12-08 10:29:42 +08001971 * Version 1.2 of the protocol has to be used for the handshake.
1972 * If TLS 1.2 is not supported, abort the handshake. Otherwise, set the
Ronald Cronb828c7d2023-04-03 16:37:22 +02001973 * ssl->keep_current_message flag for the ClientHello to be kept and parsed
1974 * as a TLS 1.2 ClientHello. We also change ssl->tls_version to
1975 * MBEDTLS_SSL_VERSION_TLS1_2 thus from now on mbedtls_ssl_handshake_step()
1976 * will dispatch to the TLS 1.2 state machine.
1977 */
Ronald Cron5af4c7f2023-03-07 20:46:59 +01001978 if (SSL_CLIENT_HELLO_TLS1_2 == parse_client_hello_ret) {
Yanray Wangfb0f47b2023-12-04 15:27:28 +08001979 /* Check if server supports TLS 1.2 */
Yanray Wang408ba6f2023-12-08 10:18:03 +08001980 if (!mbedtls_ssl_conf_is_tls12_enabled(ssl->conf)) {
Yanray Wangfb0f47b2023-12-04 15:27:28 +08001981 MBEDTLS_SSL_DEBUG_MSG(
Yanray Wang177e49a2023-12-08 10:51:04 +08001982 1, ("TLS 1.2 not supported."));
Yanray Wangfb0f47b2023-12-04 15:27:28 +08001983 MBEDTLS_SSL_PEND_FATAL_ALERT(
Yanray Wang2bef9172023-12-08 10:21:53 +08001984 MBEDTLS_SSL_ALERT_MSG_PROTOCOL_VERSION,
1985 MBEDTLS_ERR_SSL_BAD_PROTOCOL_VERSION);
1986 return MBEDTLS_ERR_SSL_BAD_PROTOCOL_VERSION;
Yanray Wangfb0f47b2023-12-04 15:27:28 +08001987 }
Ronald Cron5af4c7f2023-03-07 20:46:59 +01001988 ssl->keep_current_message = 1;
1989 ssl->tls_version = MBEDTLS_SSL_VERSION_TLS1_2;
Manuel Pégourié-Gonnard00ad6f62025-02-11 13:19:45 +01001990 MBEDTLS_SSL_DEBUG_MSG(1, ("non-1.3 ClientHello left for later processing"));
Ronald Cron5af4c7f2023-03-07 20:46:59 +01001991 return 0;
1992 }
1993
Ronald Cron7b6ee942024-01-12 10:29:55 +01001994 MBEDTLS_SSL_PROC_CHK(
1995 ssl_tls13_postprocess_client_hello(ssl, parse_client_hello_ret ==
1996 SSL_CLIENT_HELLO_HRR_REQUIRED));
Jerry Yu582dd062022-04-22 21:59:01 +08001997
Ronald Cron5af4c7f2023-03-07 20:46:59 +01001998 if (SSL_CLIENT_HELLO_OK == parse_client_hello_ret) {
Gilles Peskine449bd832023-01-11 14:50:10 +01001999 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_SERVER_HELLO);
2000 } else {
2001 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_HELLO_RETRY_REQUEST);
2002 }
XiaokangQianed582dd2022-04-13 08:21:05 +00002003
2004cleanup:
2005
Gilles Peskine449bd832023-01-11 14:50:10 +01002006 MBEDTLS_SSL_DEBUG_MSG(2, ("<= parse client hello"));
2007 return ret;
XiaokangQianed582dd2022-04-13 08:21:05 +00002008}
2009
2010/*
Jerry Yu1c3e6882022-04-20 21:23:40 +08002011 * Handler for MBEDTLS_SSL_SERVER_HELLO
Jerry Yu5b64ae92022-03-30 17:15:02 +08002012 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002013MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002014static int ssl_tls13_prepare_server_hello(mbedtls_ssl_context *ssl)
Jerry Yuf4b27e42022-03-30 17:32:21 +08002015{
Jerry Yu637a3f12022-04-20 21:37:58 +08002016 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
2017 unsigned char *server_randbytes =
Gilles Peskine449bd832023-01-11 14:50:10 +01002018 ssl->handshake->randbytes + MBEDTLS_CLIENT_HELLO_RANDOM_LEN;
Jerry Yuf4b27e42022-03-30 17:32:21 +08002019
Gilles Peskine449bd832023-01-11 14:50:10 +01002020 if ((ret = ssl->conf->f_rng(ssl->conf->p_rng, server_randbytes,
2021 MBEDTLS_SERVER_HELLO_RANDOM_LEN)) != 0) {
2022 MBEDTLS_SSL_DEBUG_RET(1, "f_rng", ret);
2023 return ret;
Jerry Yuf4b27e42022-03-30 17:32:21 +08002024 }
2025
Gilles Peskine449bd832023-01-11 14:50:10 +01002026 MBEDTLS_SSL_DEBUG_BUF(3, "server hello, random bytes", server_randbytes,
2027 MBEDTLS_SERVER_HELLO_RANDOM_LEN);
Jerry Yuf4b27e42022-03-30 17:32:21 +08002028
2029#if defined(MBEDTLS_HAVE_TIME)
YxCda609132023-05-22 12:08:12 -07002030 ssl->session_negotiate->start = mbedtls_time(NULL);
Jerry Yuf4b27e42022-03-30 17:32:21 +08002031#endif /* MBEDTLS_HAVE_TIME */
2032
Gilles Peskine449bd832023-01-11 14:50:10 +01002033 return ret;
Jerry Yuf4b27e42022-03-30 17:32:21 +08002034}
2035
Jerry Yu3bf2c642022-03-30 22:02:12 +08002036/*
Jerry Yue74e04a2022-04-21 09:23:16 +08002037 * ssl_tls13_write_server_hello_supported_versions_ext ():
Jerry Yu3bf2c642022-03-30 22:02:12 +08002038 *
2039 * struct {
Jerry Yufb9f54d2022-04-06 10:08:34 +08002040 * ProtocolVersion selected_version;
Jerry Yu3bf2c642022-03-30 22:02:12 +08002041 * } SupportedVersions;
2042 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002043MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yue74e04a2022-04-21 09:23:16 +08002044static int ssl_tls13_write_server_hello_supported_versions_ext(
Gilles Peskine449bd832023-01-11 14:50:10 +01002045 mbedtls_ssl_context *ssl,
2046 unsigned char *buf,
2047 unsigned char *end,
2048 size_t *out_len)
Jerry Yu3bf2c642022-03-30 22:02:12 +08002049{
Jerry Yu3bf2c642022-03-30 22:02:12 +08002050 *out_len = 0;
2051
Gilles Peskine449bd832023-01-11 14:50:10 +01002052 MBEDTLS_SSL_DEBUG_MSG(3, ("server hello, write selected version"));
Jerry Yu3bf2c642022-03-30 22:02:12 +08002053
2054 /* Check if we have space to write the extension:
2055 * - extension_type (2 bytes)
2056 * - extension_data_length (2 bytes)
Jerry Yu349a6132022-04-14 20:52:56 +08002057 * - selected_version (2 bytes)
Jerry Yu3bf2c642022-03-30 22:02:12 +08002058 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002059 MBEDTLS_SSL_CHK_BUF_PTR(buf, end, 6);
Jerry Yu3bf2c642022-03-30 22:02:12 +08002060
Gilles Peskine449bd832023-01-11 14:50:10 +01002061 MBEDTLS_PUT_UINT16_BE(MBEDTLS_TLS_EXT_SUPPORTED_VERSIONS, buf, 0);
Jerry Yu3bf2c642022-03-30 22:02:12 +08002062
Gilles Peskine449bd832023-01-11 14:50:10 +01002063 MBEDTLS_PUT_UINT16_BE(2, buf, 2);
Jerry Yu3bf2c642022-03-30 22:02:12 +08002064
Gilles Peskine449bd832023-01-11 14:50:10 +01002065 mbedtls_ssl_write_version(buf + 4,
2066 ssl->conf->transport,
2067 ssl->tls_version);
Jerry Yu3bf2c642022-03-30 22:02:12 +08002068
Gilles Peskine449bd832023-01-11 14:50:10 +01002069 MBEDTLS_SSL_DEBUG_MSG(3, ("supported version: [%04x]",
2070 ssl->tls_version));
Jerry Yu3bf2c642022-03-30 22:02:12 +08002071
Jerry Yu349a6132022-04-14 20:52:56 +08002072 *out_len = 6;
Jerry Yu3bf2c642022-03-30 22:02:12 +08002073
Jerry Yub95dd362022-11-08 21:19:34 +08002074 mbedtls_ssl_tls13_set_hs_sent_ext_mask(
Gilles Peskine449bd832023-01-11 14:50:10 +01002075 ssl, MBEDTLS_TLS_EXT_SUPPORTED_VERSIONS);
Jerry Yub95dd362022-11-08 21:19:34 +08002076
Gilles Peskine449bd832023-01-11 14:50:10 +01002077 return 0;
Jerry Yu3bf2c642022-03-30 22:02:12 +08002078}
2079
Jerry Yud9436a12022-04-20 22:28:09 +08002080
Jerry Yu3bf2c642022-03-30 22:02:12 +08002081
2082/* Generate and export a single key share. For hybrid KEMs, this can
2083 * be called multiple times with the different components of the hybrid. */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002084MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002085static int ssl_tls13_generate_and_write_key_share(mbedtls_ssl_context *ssl,
2086 uint16_t named_group,
2087 unsigned char *buf,
2088 unsigned char *end,
2089 size_t *out_len)
Jerry Yu3bf2c642022-03-30 22:02:12 +08002090{
2091 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yu955ddd72022-04-22 22:27:33 +08002092
2093 *out_len = 0;
2094
Valerio Settic9ae8622023-07-25 11:23:50 +02002095#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_EPHEMERAL_ENABLED)
Przemek Stekiel29c219c2023-05-31 15:21:04 +02002096 if (mbedtls_ssl_tls13_named_group_is_ecdhe(named_group) ||
Przemek Stekield5f79e72023-06-29 09:08:43 +02002097 mbedtls_ssl_tls13_named_group_is_ffdh(named_group)) {
Przemek Stekiel408569f2023-07-06 11:26:44 +02002098 ret = mbedtls_ssl_tls13_generate_and_write_xxdh_key_exchange(
Gilles Peskine449bd832023-01-11 14:50:10 +01002099 ssl, named_group, buf, end, out_len);
2100 if (ret != 0) {
Jerry Yu89e103c2022-03-30 22:43:29 +08002101 MBEDTLS_SSL_DEBUG_RET(
Przemek Stekiel408569f2023-07-06 11:26:44 +02002102 1, "mbedtls_ssl_tls13_generate_and_write_xxdh_key_exchange",
Gilles Peskine449bd832023-01-11 14:50:10 +01002103 ret);
2104 return ret;
Jerry Yu3bf2c642022-03-30 22:02:12 +08002105 }
Gilles Peskine449bd832023-01-11 14:50:10 +01002106 } else
Valerio Settic9ae8622023-07-25 11:23:50 +02002107#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_EPHEMERAL_ENABLED */
Gilles Peskine449bd832023-01-11 14:50:10 +01002108 if (0 /* Other kinds of KEMs */) {
2109 } else {
Jerry Yu955ddd72022-04-22 22:27:33 +08002110 ((void) ssl);
2111 ((void) named_group);
2112 ((void) buf);
2113 ((void) end);
Jerry Yu3bf2c642022-03-30 22:02:12 +08002114 ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
2115 }
2116
Gilles Peskine449bd832023-01-11 14:50:10 +01002117 return ret;
Jerry Yu3bf2c642022-03-30 22:02:12 +08002118}
2119
2120/*
2121 * ssl_tls13_write_key_share_ext
2122 *
2123 * Structure of key_share extension in ServerHello:
2124 *
Jerry Yu1c3e6882022-04-20 21:23:40 +08002125 * struct {
2126 * NamedGroup group;
2127 * opaque key_exchange<1..2^16-1>;
2128 * } KeyShareEntry;
2129 * struct {
2130 * KeyShareEntry server_share;
2131 * } KeyShareServerHello;
Jerry Yu3bf2c642022-03-30 22:02:12 +08002132 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002133MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002134static int ssl_tls13_write_key_share_ext(mbedtls_ssl_context *ssl,
2135 unsigned char *buf,
2136 unsigned char *end,
2137 size_t *out_len)
Jerry Yu3bf2c642022-03-30 22:02:12 +08002138{
Jerry Yu955ddd72022-04-22 22:27:33 +08002139 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yu3bf2c642022-03-30 22:02:12 +08002140 unsigned char *p = buf;
Jerry Yu57d48412022-04-20 21:50:42 +08002141 uint16_t group = ssl->handshake->offered_group_id;
Jerry Yu3bf2c642022-03-30 22:02:12 +08002142 unsigned char *server_share = buf + 4;
Jerry Yu3bf2c642022-03-30 22:02:12 +08002143 size_t key_exchange_length;
Jerry Yu3bf2c642022-03-30 22:02:12 +08002144
2145 *out_len = 0;
2146
Gilles Peskine449bd832023-01-11 14:50:10 +01002147 MBEDTLS_SSL_DEBUG_MSG(3, ("server hello, adding key share extension"));
Jerry Yu3bf2c642022-03-30 22:02:12 +08002148
Gilles Peskine449bd832023-01-11 14:50:10 +01002149 MBEDTLS_SSL_DEBUG_MSG(2, ("server hello, write selected_group: %s (%04x)",
2150 mbedtls_ssl_named_group_to_str(group),
2151 group));
Jerry Yu58af2332022-09-06 11:19:31 +08002152
Jerry Yu3bf2c642022-03-30 22:02:12 +08002153 /* Check if we have space for header and length fields:
2154 * - extension_type (2 bytes)
2155 * - extension_data_length (2 bytes)
2156 * - group (2 bytes)
2157 * - key_exchange_length (2 bytes)
2158 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002159 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 8);
2160 MBEDTLS_PUT_UINT16_BE(MBEDTLS_TLS_EXT_KEY_SHARE, p, 0);
2161 MBEDTLS_PUT_UINT16_BE(group, server_share, 0);
Jerry Yu3bf2c642022-03-30 22:02:12 +08002162 p += 8;
Jerry Yu57d48412022-04-20 21:50:42 +08002163
Jerry Yu3bf2c642022-03-30 22:02:12 +08002164 /* When we introduce PQC-ECDHE hybrids, we'll want to call this
2165 * function multiple times. */
Jerry Yu955ddd72022-04-22 22:27:33 +08002166 ret = ssl_tls13_generate_and_write_key_share(
Gilles Peskine449bd832023-01-11 14:50:10 +01002167 ssl, group, server_share + 4, end, &key_exchange_length);
2168 if (ret != 0) {
2169 return ret;
2170 }
Jerry Yu3bf2c642022-03-30 22:02:12 +08002171 p += key_exchange_length;
Jerry Yuc1be19f2022-04-23 16:11:39 +08002172
Gilles Peskine449bd832023-01-11 14:50:10 +01002173 MBEDTLS_PUT_UINT16_BE(key_exchange_length, server_share + 2, 0);
Jerry Yu3bf2c642022-03-30 22:02:12 +08002174
Gilles Peskine449bd832023-01-11 14:50:10 +01002175 MBEDTLS_PUT_UINT16_BE(p - server_share, buf, 2);
Jerry Yu3bf2c642022-03-30 22:02:12 +08002176
Jerry Yu57d48412022-04-20 21:50:42 +08002177 *out_len = p - buf;
Jerry Yu955ddd72022-04-22 22:27:33 +08002178
Gilles Peskine449bd832023-01-11 14:50:10 +01002179 mbedtls_ssl_tls13_set_hs_sent_ext_mask(ssl, MBEDTLS_TLS_EXT_KEY_SHARE);
Jerry Yub95dd362022-11-08 21:19:34 +08002180
Gilles Peskine449bd832023-01-11 14:50:10 +01002181 return 0;
Jerry Yu3bf2c642022-03-30 22:02:12 +08002182}
Jerry Yud9436a12022-04-20 22:28:09 +08002183
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002184MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002185static int ssl_tls13_write_hrr_key_share_ext(mbedtls_ssl_context *ssl,
2186 unsigned char *buf,
2187 unsigned char *end,
2188 size_t *out_len)
Jerry Yu23f7a6f2022-04-23 15:16:45 +08002189{
2190 uint16_t selected_group = ssl->handshake->hrr_selected_group;
2191 /* key_share Extension
2192 *
2193 * struct {
2194 * select (Handshake.msg_type) {
2195 * ...
2196 * case hello_retry_request:
2197 * NamedGroup selected_group;
2198 * ...
2199 * };
2200 * } KeyShare;
2201 */
2202
2203 *out_len = 0;
2204
Jerry Yub0ac10b2022-05-05 11:10:08 +08002205 /*
2206 * For a pure PSK key exchange, there is no group to agree upon. The purpose
2207 * of the HRR is then to transmit a cookie to force the client to demonstrate
2208 * reachability at their apparent network address (primarily useful for DTLS).
2209 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002210 if (!mbedtls_ssl_tls13_key_exchange_mode_with_ephemeral(ssl)) {
2211 return 0;
2212 }
Jerry Yu23f7a6f2022-04-23 15:16:45 +08002213
2214 /* We should only send the key_share extension if the client's initial
2215 * key share was not acceptable. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002216 if (ssl->handshake->offered_group_id != 0) {
2217 MBEDTLS_SSL_DEBUG_MSG(4, ("Skip key_share extension in HRR"));
2218 return 0;
Jerry Yu23f7a6f2022-04-23 15:16:45 +08002219 }
2220
Gilles Peskine449bd832023-01-11 14:50:10 +01002221 if (selected_group == 0) {
2222 MBEDTLS_SSL_DEBUG_MSG(1, ("no matching named group found"));
2223 return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
Jerry Yu23f7a6f2022-04-23 15:16:45 +08002224 }
2225
Jerry Yub0ac10b2022-05-05 11:10:08 +08002226 /* Check if we have enough space:
2227 * - extension_type (2 bytes)
2228 * - extension_data_length (2 bytes)
2229 * - selected_group (2 bytes)
2230 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002231 MBEDTLS_SSL_CHK_BUF_PTR(buf, end, 6);
Jerry Yu23f7a6f2022-04-23 15:16:45 +08002232
Gilles Peskine449bd832023-01-11 14:50:10 +01002233 MBEDTLS_PUT_UINT16_BE(MBEDTLS_TLS_EXT_KEY_SHARE, buf, 0);
2234 MBEDTLS_PUT_UINT16_BE(2, buf, 2);
2235 MBEDTLS_PUT_UINT16_BE(selected_group, buf, 4);
Jerry Yu23f7a6f2022-04-23 15:16:45 +08002236
Gilles Peskine449bd832023-01-11 14:50:10 +01002237 MBEDTLS_SSL_DEBUG_MSG(3,
2238 ("HRR selected_group: %s (%x)",
2239 mbedtls_ssl_named_group_to_str(selected_group),
2240 selected_group));
Jerry Yu23f7a6f2022-04-23 15:16:45 +08002241
2242 *out_len = 6;
Jerry Yu23f7a6f2022-04-23 15:16:45 +08002243
Gilles Peskine449bd832023-01-11 14:50:10 +01002244 mbedtls_ssl_tls13_set_hs_sent_ext_mask(ssl, MBEDTLS_TLS_EXT_KEY_SHARE);
Jerry Yub95dd362022-11-08 21:19:34 +08002245
Gilles Peskine449bd832023-01-11 14:50:10 +01002246 return 0;
Jerry Yu23f7a6f2022-04-23 15:16:45 +08002247}
Jerry Yu3bf2c642022-03-30 22:02:12 +08002248
2249/*
2250 * Structure of ServerHello message:
2251 *
2252 * struct {
2253 * ProtocolVersion legacy_version = 0x0303; // TLS v1.2
2254 * Random random;
2255 * opaque legacy_session_id_echo<0..32>;
2256 * CipherSuite cipher_suite;
2257 * uint8 legacy_compression_method = 0;
2258 * Extension extensions<6..2^16-1>;
2259 * } ServerHello;
2260 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002261MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002262static int ssl_tls13_write_server_hello_body(mbedtls_ssl_context *ssl,
2263 unsigned char *buf,
2264 unsigned char *end,
2265 size_t *out_len,
2266 int is_hrr)
Jerry Yu56404d72022-03-30 17:36:13 +08002267{
Jerry Yu955ddd72022-04-22 22:27:33 +08002268 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yu3bf2c642022-03-30 22:02:12 +08002269 unsigned char *p = buf;
Jerry Yud9436a12022-04-20 22:28:09 +08002270 unsigned char *p_extensions_len;
Jerry Yufbe3e642022-04-25 19:31:51 +08002271 size_t output_len;
Jerry Yu3bf2c642022-03-30 22:02:12 +08002272
2273 *out_len = 0;
Jerry Yu50e00e32022-10-31 14:45:01 +08002274 ssl->handshake->sent_extensions = MBEDTLS_SSL_EXT_MASK_NONE;
Jerry Yu3bf2c642022-03-30 22:02:12 +08002275
Jerry Yucfc04b32022-04-21 09:31:58 +08002276 /* ...
2277 * ProtocolVersion legacy_version = 0x0303; // TLS 1.2
2278 * ...
2279 * with ProtocolVersion defined as:
2280 * uint16 ProtocolVersion;
Jerry Yu3bf2c642022-03-30 22:02:12 +08002281 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002282 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 2);
2283 MBEDTLS_PUT_UINT16_BE(0x0303, p, 0);
Jerry Yu3bf2c642022-03-30 22:02:12 +08002284 p += 2;
2285
Jerry Yu1c3e6882022-04-20 21:23:40 +08002286 /* ...
2287 * Random random;
2288 * ...
Jerry Yucfc04b32022-04-21 09:31:58 +08002289 * with Random defined as:
2290 * opaque Random[MBEDTLS_SERVER_HELLO_RANDOM_LEN];
Jerry Yu1c3e6882022-04-20 21:23:40 +08002291 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002292 MBEDTLS_SSL_CHK_BUF_PTR(p, end, MBEDTLS_SERVER_HELLO_RANDOM_LEN);
2293 if (is_hrr) {
2294 memcpy(p, mbedtls_ssl_tls13_hello_retry_request_magic,
2295 MBEDTLS_SERVER_HELLO_RANDOM_LEN);
2296 } else {
2297 memcpy(p, &ssl->handshake->randbytes[MBEDTLS_CLIENT_HELLO_RANDOM_LEN],
2298 MBEDTLS_SERVER_HELLO_RANDOM_LEN);
Jerry Yu23f7a6f2022-04-23 15:16:45 +08002299 }
Gilles Peskine449bd832023-01-11 14:50:10 +01002300 MBEDTLS_SSL_DEBUG_BUF(3, "server hello, random bytes",
2301 p, MBEDTLS_SERVER_HELLO_RANDOM_LEN);
Jerry Yu3bf2c642022-03-30 22:02:12 +08002302 p += MBEDTLS_SERVER_HELLO_RANDOM_LEN;
2303
Jerry Yucfc04b32022-04-21 09:31:58 +08002304 /* ...
2305 * opaque legacy_session_id_echo<0..32>;
2306 * ...
Jerry Yu3bf2c642022-03-30 22:02:12 +08002307 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002308 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 1 + ssl->session_negotiate->id_len);
2309 *p++ = (unsigned char) ssl->session_negotiate->id_len;
2310 if (ssl->session_negotiate->id_len > 0) {
2311 memcpy(p, &ssl->session_negotiate->id[0],
2312 ssl->session_negotiate->id_len);
Jerry Yu3bf2c642022-03-30 22:02:12 +08002313 p += ssl->session_negotiate->id_len;
Jerry Yu955ddd72022-04-22 22:27:33 +08002314
Gilles Peskine449bd832023-01-11 14:50:10 +01002315 MBEDTLS_SSL_DEBUG_BUF(3, "session id", ssl->session_negotiate->id,
2316 ssl->session_negotiate->id_len);
Jerry Yu3bf2c642022-03-30 22:02:12 +08002317 }
2318
Jerry Yucfc04b32022-04-21 09:31:58 +08002319 /* ...
2320 * CipherSuite cipher_suite;
2321 * ...
2322 * with CipherSuite defined as:
2323 * uint8 CipherSuite[2];
Jerry Yu3bf2c642022-03-30 22:02:12 +08002324 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002325 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 2);
2326 MBEDTLS_PUT_UINT16_BE(ssl->session_negotiate->ciphersuite, p, 0);
Jerry Yu3bf2c642022-03-30 22:02:12 +08002327 p += 2;
Gilles Peskine449bd832023-01-11 14:50:10 +01002328 MBEDTLS_SSL_DEBUG_MSG(3,
2329 ("server hello, chosen ciphersuite: %s ( id=%d )",
2330 mbedtls_ssl_get_ciphersuite_name(
2331 ssl->session_negotiate->ciphersuite),
2332 ssl->session_negotiate->ciphersuite));
Jerry Yu3bf2c642022-03-30 22:02:12 +08002333
Jerry Yucfc04b32022-04-21 09:31:58 +08002334 /* ...
2335 * uint8 legacy_compression_method = 0;
2336 * ...
2337 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002338 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 1);
Thomas Daubney31e03a82022-07-25 15:59:25 +01002339 *p++ = MBEDTLS_SSL_COMPRESS_NULL;
Jerry Yu3bf2c642022-03-30 22:02:12 +08002340
Jerry Yucfc04b32022-04-21 09:31:58 +08002341 /* ...
2342 * Extension extensions<6..2^16-1>;
2343 * ...
2344 * struct {
2345 * ExtensionType extension_type; (2 bytes)
2346 * opaque extension_data<0..2^16-1>;
2347 * } Extension;
2348 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002349 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 2);
Jerry Yud9436a12022-04-20 22:28:09 +08002350 p_extensions_len = p;
Jerry Yu3bf2c642022-03-30 22:02:12 +08002351 p += 2;
2352
Gilles Peskine449bd832023-01-11 14:50:10 +01002353 if ((ret = ssl_tls13_write_server_hello_supported_versions_ext(
2354 ssl, p, end, &output_len)) != 0) {
Jerry Yu955ddd72022-04-22 22:27:33 +08002355 MBEDTLS_SSL_DEBUG_RET(
Gilles Peskine449bd832023-01-11 14:50:10 +01002356 1, "ssl_tls13_write_server_hello_supported_versions_ext", ret);
2357 return ret;
Jerry Yu3bf2c642022-03-30 22:02:12 +08002358 }
2359 p += output_len;
2360
Gilles Peskine449bd832023-01-11 14:50:10 +01002361 if (mbedtls_ssl_tls13_key_exchange_mode_with_ephemeral(ssl)) {
2362 if (is_hrr) {
2363 ret = ssl_tls13_write_hrr_key_share_ext(ssl, p, end, &output_len);
2364 } else {
2365 ret = ssl_tls13_write_key_share_ext(ssl, p, end, &output_len);
2366 }
2367 if (ret != 0) {
2368 return ret;
2369 }
Jerry Yu3bf2c642022-03-30 22:02:12 +08002370 p += output_len;
2371 }
Jerry Yu3bf2c642022-03-30 22:02:12 +08002372
Ronald Cron41a443a2022-10-04 16:38:25 +02002373#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01002374 if (!is_hrr && mbedtls_ssl_tls13_key_exchange_mode_with_psk(ssl)) {
2375 ret = ssl_tls13_write_server_pre_shared_key_ext(ssl, p, end, &output_len);
2376 if (ret != 0) {
2377 MBEDTLS_SSL_DEBUG_RET(1, "ssl_tls13_write_server_pre_shared_key_ext",
2378 ret);
2379 return ret;
Jerry Yu032b15ce2022-07-11 06:10:03 +00002380 }
2381 p += output_len;
2382 }
Ronald Cron41a443a2022-10-04 16:38:25 +02002383#endif
Jerry Yu032b15ce2022-07-11 06:10:03 +00002384
Gilles Peskine449bd832023-01-11 14:50:10 +01002385 MBEDTLS_PUT_UINT16_BE(p - p_extensions_len - 2, p_extensions_len, 0);
Jerry Yu3bf2c642022-03-30 22:02:12 +08002386
Gilles Peskine449bd832023-01-11 14:50:10 +01002387 MBEDTLS_SSL_DEBUG_BUF(4, "server hello extensions",
2388 p_extensions_len, p - p_extensions_len);
Jerry Yu3bf2c642022-03-30 22:02:12 +08002389
Jerry Yud9436a12022-04-20 22:28:09 +08002390 *out_len = p - buf;
Jerry Yu3bf2c642022-03-30 22:02:12 +08002391
Gilles Peskine449bd832023-01-11 14:50:10 +01002392 MBEDTLS_SSL_DEBUG_BUF(3, "server hello", buf, *out_len);
Jerry Yu3bf2c642022-03-30 22:02:12 +08002393
Jerry Yu7de2ff02022-11-08 21:43:46 +08002394 MBEDTLS_SSL_PRINT_EXTS(
Jerry Yu4b8f2f72022-10-31 13:31:22 +08002395 3, is_hrr ? MBEDTLS_SSL_TLS1_3_HS_HELLO_RETRY_REQUEST :
Gilles Peskine449bd832023-01-11 14:50:10 +01002396 MBEDTLS_SSL_HS_SERVER_HELLO,
2397 ssl->handshake->sent_extensions);
Jerry Yu4b8f2f72022-10-31 13:31:22 +08002398
Gilles Peskine449bd832023-01-11 14:50:10 +01002399 return ret;
Jerry Yu56404d72022-03-30 17:36:13 +08002400}
2401
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002402MBEDTLS_CHECK_RETURN_CRITICAL
Xiaokang Qian934ce6f2023-02-06 10:23:04 +00002403static int ssl_tls13_finalize_server_hello(mbedtls_ssl_context *ssl)
Jerry Yue110d252022-05-05 10:19:22 +08002404{
2405 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine449bd832023-01-11 14:50:10 +01002406 ret = mbedtls_ssl_tls13_compute_handshake_transform(ssl);
2407 if (ret != 0) {
2408 MBEDTLS_SSL_DEBUG_RET(1,
2409 "mbedtls_ssl_tls13_compute_handshake_transform",
2410 ret);
2411 return ret;
Jerry Yue110d252022-05-05 10:19:22 +08002412 }
2413
Gilles Peskine449bd832023-01-11 14:50:10 +01002414 return ret;
Jerry Yue110d252022-05-05 10:19:22 +08002415}
2416
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002417MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002418static int ssl_tls13_write_server_hello(mbedtls_ssl_context *ssl)
Jerry Yu5b64ae92022-03-30 17:15:02 +08002419{
Jerry Yu637a3f12022-04-20 21:37:58 +08002420 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yuf4b27e42022-03-30 17:32:21 +08002421 unsigned char *buf;
2422 size_t buf_len, msg_len;
2423
Gilles Peskine449bd832023-01-11 14:50:10 +01002424 MBEDTLS_SSL_DEBUG_MSG(2, ("=> write server hello"));
Jerry Yuf4b27e42022-03-30 17:32:21 +08002425
Gilles Peskine449bd832023-01-11 14:50:10 +01002426 MBEDTLS_SSL_PROC_CHK(ssl_tls13_prepare_server_hello(ssl));
Jerry Yuf4b27e42022-03-30 17:32:21 +08002427
Xiaokang Qian9f1747b2023-03-30 02:50:04 +00002428 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_start_handshake_msg(
2429 ssl, MBEDTLS_SSL_HS_SERVER_HELLO, &buf, &buf_len));
Jerry Yuf4b27e42022-03-30 17:32:21 +08002430
Gilles Peskine449bd832023-01-11 14:50:10 +01002431 MBEDTLS_SSL_PROC_CHK(ssl_tls13_write_server_hello_body(ssl, buf,
2432 buf + buf_len,
2433 &msg_len,
2434 0));
Jerry Yuf4b27e42022-03-30 17:32:21 +08002435
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +01002436 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_add_hs_msg_to_checksum(
Manuel Pégourié-Gonnard43cc1272023-02-06 11:48:19 +01002437 ssl, MBEDTLS_SSL_HS_SERVER_HELLO, buf, msg_len));
Jerry Yuf4b27e42022-03-30 17:32:21 +08002438
Gilles Peskine449bd832023-01-11 14:50:10 +01002439 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_finish_handshake_msg(
2440 ssl, buf_len, msg_len));
Jerry Yu637a3f12022-04-20 21:37:58 +08002441
Xiaokang Qian934ce6f2023-02-06 10:23:04 +00002442 MBEDTLS_SSL_PROC_CHK(ssl_tls13_finalize_server_hello(ssl));
Jerry Yue110d252022-05-05 10:19:22 +08002443
Gabor Mezei7b39bf12022-05-24 16:04:14 +02002444#if defined(MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE)
2445 /* The server sends a dummy change_cipher_spec record immediately
2446 * after its first handshake message. This may either be after
2447 * a ServerHello or a HelloRetryRequest.
2448 */
Gabor Mezei96ae9262022-06-28 11:45:18 +02002449 mbedtls_ssl_handshake_set_state(
Gilles Peskine449bd832023-01-11 14:50:10 +01002450 ssl, MBEDTLS_SSL_SERVER_CCS_AFTER_SERVER_HELLO);
Gabor Mezei7b39bf12022-05-24 16:04:14 +02002451#else
Gilles Peskine449bd832023-01-11 14:50:10 +01002452 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_ENCRYPTED_EXTENSIONS);
Gabor Mezei7b39bf12022-05-24 16:04:14 +02002453#endif /* MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE */
Jerry Yue110d252022-05-05 10:19:22 +08002454
Jerry Yuf4b27e42022-03-30 17:32:21 +08002455cleanup:
2456
Gilles Peskine449bd832023-01-11 14:50:10 +01002457 MBEDTLS_SSL_DEBUG_MSG(2, ("<= write server hello"));
2458 return ret;
Jerry Yu5b64ae92022-03-30 17:15:02 +08002459}
2460
Jerry Yu23d1a252022-05-12 18:08:59 +08002461
2462/*
2463 * Handler for MBEDTLS_SSL_HELLO_RETRY_REQUEST
2464 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002465MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002466static int ssl_tls13_prepare_hello_retry_request(mbedtls_ssl_context *ssl)
Jerry Yu23d1a252022-05-12 18:08:59 +08002467{
2468 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Ronald Cron5fbd2702024-02-14 10:03:36 +01002469 if (ssl->handshake->hello_retry_request_flag) {
Gilles Peskine449bd832023-01-11 14:50:10 +01002470 MBEDTLS_SSL_DEBUG_MSG(1, ("Too many HRRs"));
2471 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE,
2472 MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE);
2473 return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
Jerry Yu23d1a252022-05-12 18:08:59 +08002474 }
2475
2476 /*
2477 * Create stateless transcript hash for HRR
2478 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002479 MBEDTLS_SSL_DEBUG_MSG(4, ("Reset transcript for HRR"));
2480 ret = mbedtls_ssl_reset_transcript_for_hrr(ssl);
2481 if (ret != 0) {
2482 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_reset_transcript_for_hrr", ret);
2483 return ret;
Jerry Yu23d1a252022-05-12 18:08:59 +08002484 }
Gilles Peskine449bd832023-01-11 14:50:10 +01002485 mbedtls_ssl_session_reset_msg_layer(ssl, 0);
Jerry Yu23d1a252022-05-12 18:08:59 +08002486
Gilles Peskine449bd832023-01-11 14:50:10 +01002487 return 0;
Jerry Yu23d1a252022-05-12 18:08:59 +08002488}
2489
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002490MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002491static int ssl_tls13_write_hello_retry_request(mbedtls_ssl_context *ssl)
Jerry Yu23d1a252022-05-12 18:08:59 +08002492{
2493 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
2494 unsigned char *buf;
2495 size_t buf_len, msg_len;
2496
Gilles Peskine449bd832023-01-11 14:50:10 +01002497 MBEDTLS_SSL_DEBUG_MSG(2, ("=> write hello retry request"));
Jerry Yu23d1a252022-05-12 18:08:59 +08002498
Gilles Peskine449bd832023-01-11 14:50:10 +01002499 MBEDTLS_SSL_PROC_CHK(ssl_tls13_prepare_hello_retry_request(ssl));
Jerry Yu23d1a252022-05-12 18:08:59 +08002500
Gilles Peskine449bd832023-01-11 14:50:10 +01002501 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_start_handshake_msg(
2502 ssl, MBEDTLS_SSL_HS_SERVER_HELLO,
2503 &buf, &buf_len));
Jerry Yu23d1a252022-05-12 18:08:59 +08002504
Gilles Peskine449bd832023-01-11 14:50:10 +01002505 MBEDTLS_SSL_PROC_CHK(ssl_tls13_write_server_hello_body(ssl, buf,
2506 buf + buf_len,
2507 &msg_len,
2508 1));
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +01002509 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_add_hs_msg_to_checksum(
Manuel Pégourié-Gonnard43cc1272023-02-06 11:48:19 +01002510 ssl, MBEDTLS_SSL_HS_SERVER_HELLO, buf, msg_len));
Jerry Yu23d1a252022-05-12 18:08:59 +08002511
2512
Gilles Peskine449bd832023-01-11 14:50:10 +01002513 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_finish_handshake_msg(ssl, buf_len,
2514 msg_len));
Jerry Yu23d1a252022-05-12 18:08:59 +08002515
Ronald Cron5fbd2702024-02-14 10:03:36 +01002516 ssl->handshake->hello_retry_request_flag = 1;
Jerry Yu23d1a252022-05-12 18:08:59 +08002517
Gabor Mezei7b39bf12022-05-24 16:04:14 +02002518#if defined(MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE)
2519 /* The server sends a dummy change_cipher_spec record immediately
2520 * after its first handshake message. This may either be after
2521 * a ServerHello or a HelloRetryRequest.
2522 */
Gabor Mezei96ae9262022-06-28 11:45:18 +02002523 mbedtls_ssl_handshake_set_state(
Gilles Peskine449bd832023-01-11 14:50:10 +01002524 ssl, MBEDTLS_SSL_SERVER_CCS_AFTER_HELLO_RETRY_REQUEST);
Gabor Mezei7b39bf12022-05-24 16:04:14 +02002525#else
Gilles Peskine449bd832023-01-11 14:50:10 +01002526 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_CLIENT_HELLO);
Gabor Mezei7b39bf12022-05-24 16:04:14 +02002527#endif /* MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE */
Jerry Yu23d1a252022-05-12 18:08:59 +08002528
2529cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +01002530 MBEDTLS_SSL_DEBUG_MSG(2, ("<= write hello retry request"));
2531 return ret;
Jerry Yu23d1a252022-05-12 18:08:59 +08002532}
2533
Jerry Yu5b64ae92022-03-30 17:15:02 +08002534/*
Jerry Yu4d3841a2022-04-16 12:37:19 +08002535 * Handler for MBEDTLS_SSL_ENCRYPTED_EXTENSIONS
2536 */
Jerry Yu4d3841a2022-04-16 12:37:19 +08002537
2538/*
2539 * struct {
2540 * Extension extensions<0..2 ^ 16 - 1>;
2541 * } EncryptedExtensions;
2542 *
2543 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002544MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002545static int ssl_tls13_write_encrypted_extensions_body(mbedtls_ssl_context *ssl,
2546 unsigned char *buf,
2547 unsigned char *end,
2548 size_t *out_len)
Jerry Yu4d3841a2022-04-16 12:37:19 +08002549{
XiaokangQianacb39922022-06-17 10:18:48 +00002550 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yu4d3841a2022-04-16 12:37:19 +08002551 unsigned char *p = buf;
2552 size_t extensions_len = 0;
Jerry Yu8937eb42022-05-03 12:12:14 +08002553 unsigned char *p_extensions_len;
XiaokangQianacb39922022-06-17 10:18:48 +00002554 size_t output_len;
Jerry Yu9da5e5a2022-05-03 15:46:09 +08002555
Jerry Yu4d3841a2022-04-16 12:37:19 +08002556 *out_len = 0;
2557
Gilles Peskine449bd832023-01-11 14:50:10 +01002558 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 2);
Jerry Yu8937eb42022-05-03 12:12:14 +08002559 p_extensions_len = p;
Jerry Yu4d3841a2022-04-16 12:37:19 +08002560 p += 2;
2561
Jerry Yu8937eb42022-05-03 12:12:14 +08002562 ((void) ssl);
XiaokangQianacb39922022-06-17 10:18:48 +00002563 ((void) ret);
2564 ((void) output_len);
2565
2566#if defined(MBEDTLS_SSL_ALPN)
Gilles Peskine449bd832023-01-11 14:50:10 +01002567 ret = mbedtls_ssl_write_alpn_ext(ssl, p, end, &output_len);
2568 if (ret != 0) {
2569 return ret;
2570 }
XiaokangQian95d5f542022-06-24 02:29:26 +00002571 p += output_len;
XiaokangQianacb39922022-06-17 10:18:48 +00002572#endif /* MBEDTLS_SSL_ALPN */
Jerry Yu4d3841a2022-04-16 12:37:19 +08002573
Jerry Yu71c14f12022-12-12 11:10:35 +08002574#if defined(MBEDTLS_SSL_EARLY_DATA)
Ronald Cron78a38f62024-02-01 18:30:31 +01002575 if (ssl->handshake->early_data_accepted) {
Jerry Yu52335392023-11-23 18:06:06 +08002576 ret = mbedtls_ssl_tls13_write_early_data_ext(
Jerry Yuc59c5862023-12-05 10:40:49 +08002577 ssl, 0, p, end, &output_len);
Jerry Yu71c14f12022-12-12 11:10:35 +08002578 if (ret != 0) {
2579 return ret;
2580 }
2581 p += output_len;
2582 }
2583#endif /* MBEDTLS_SSL_EARLY_DATA */
2584
Waleed Elmelegy47d29462024-01-03 17:31:52 +00002585#if defined(MBEDTLS_SSL_RECORD_SIZE_LIMIT)
Waleed Elmelegyfbe42742024-01-05 18:11:10 +00002586 if (ssl->handshake->received_extensions & MBEDTLS_SSL_EXT_MASK(RECORD_SIZE_LIMIT)) {
Waleed Elmelegyd2fc90e2024-01-04 18:04:53 +00002587 ret = mbedtls_ssl_tls13_write_record_size_limit_ext(
2588 ssl, p, end, &output_len);
2589 if (ret != 0) {
2590 return ret;
2591 }
2592 p += output_len;
Waleed Elmelegy47d29462024-01-03 17:31:52 +00002593 }
Waleed Elmelegy47d29462024-01-03 17:31:52 +00002594#endif
2595
Gilles Peskine449bd832023-01-11 14:50:10 +01002596 extensions_len = (p - p_extensions_len) - 2;
2597 MBEDTLS_PUT_UINT16_BE(extensions_len, p_extensions_len, 0);
Jerry Yu8937eb42022-05-03 12:12:14 +08002598
2599 *out_len = p - buf;
Jerry Yu4d3841a2022-04-16 12:37:19 +08002600
Gilles Peskine449bd832023-01-11 14:50:10 +01002601 MBEDTLS_SSL_DEBUG_BUF(4, "encrypted extensions", buf, *out_len);
Jerry Yu4d3841a2022-04-16 12:37:19 +08002602
Jerry Yu7de2ff02022-11-08 21:43:46 +08002603 MBEDTLS_SSL_PRINT_EXTS(
Gilles Peskine449bd832023-01-11 14:50:10 +01002604 3, MBEDTLS_SSL_HS_ENCRYPTED_EXTENSIONS, ssl->handshake->sent_extensions);
Jerry Yu4b8f2f72022-10-31 13:31:22 +08002605
Gilles Peskine449bd832023-01-11 14:50:10 +01002606 return 0;
Jerry Yu4d3841a2022-04-16 12:37:19 +08002607}
2608
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002609MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002610static int ssl_tls13_write_encrypted_extensions(mbedtls_ssl_context *ssl)
Jerry Yu4d3841a2022-04-16 12:37:19 +08002611{
2612 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
2613 unsigned char *buf;
Jerry Yu39730a72022-05-03 12:14:04 +08002614 size_t buf_len, msg_len;
Jerry Yu4d3841a2022-04-16 12:37:19 +08002615
Gilles Peskine449bd832023-01-11 14:50:10 +01002616 mbedtls_ssl_set_outbound_transform(ssl,
2617 ssl->handshake->transform_handshake);
Gabor Mezei54719122022-06-28 11:34:56 +02002618 MBEDTLS_SSL_DEBUG_MSG(
Gilles Peskine449bd832023-01-11 14:50:10 +01002619 3, ("switching to handshake transform for outbound data"));
Gabor Mezei54719122022-06-28 11:34:56 +02002620
Gilles Peskine449bd832023-01-11 14:50:10 +01002621 MBEDTLS_SSL_DEBUG_MSG(2, ("=> write encrypted extensions"));
Jerry Yu4d3841a2022-04-16 12:37:19 +08002622
Xiaokang Qian9f1747b2023-03-30 02:50:04 +00002623 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_start_handshake_msg(
2624 ssl, MBEDTLS_SSL_HS_ENCRYPTED_EXTENSIONS,
2625 &buf, &buf_len));
Jerry Yu4d3841a2022-04-16 12:37:19 +08002626
Gilles Peskine449bd832023-01-11 14:50:10 +01002627 MBEDTLS_SSL_PROC_CHK(ssl_tls13_write_encrypted_extensions_body(
2628 ssl, buf, buf + buf_len, &msg_len));
Jerry Yu4d3841a2022-04-16 12:37:19 +08002629
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +01002630 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_add_hs_msg_to_checksum(
Xiaokang Qian9f1747b2023-03-30 02:50:04 +00002631 ssl, MBEDTLS_SSL_HS_ENCRYPTED_EXTENSIONS,
2632 buf, msg_len));
Jerry Yu4d3841a2022-04-16 12:37:19 +08002633
Gilles Peskine449bd832023-01-11 14:50:10 +01002634 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_finish_handshake_msg(
2635 ssl, buf_len, msg_len));
Jerry Yu4d3841a2022-04-16 12:37:19 +08002636
Ronald Cron928cbd32022-10-04 16:14:26 +02002637#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01002638 if (mbedtls_ssl_tls13_key_exchange_mode_with_psk(ssl)) {
2639 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_SERVER_FINISHED);
2640 } else {
2641 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_CERTIFICATE_REQUEST);
2642 }
Jerry Yu8937eb42022-05-03 12:12:14 +08002643#else
Gilles Peskine449bd832023-01-11 14:50:10 +01002644 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_SERVER_FINISHED);
Jerry Yu8937eb42022-05-03 12:12:14 +08002645#endif
2646
Jerry Yu4d3841a2022-04-16 12:37:19 +08002647cleanup:
2648
Gilles Peskine449bd832023-01-11 14:50:10 +01002649 MBEDTLS_SSL_DEBUG_MSG(2, ("<= write encrypted extensions"));
2650 return ret;
Jerry Yu4d3841a2022-04-16 12:37:19 +08002651}
2652
Ronald Cron928cbd32022-10-04 16:14:26 +02002653#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED)
XiaokangQiana987e1d2022-05-07 01:25:58 +00002654#define SSL_CERTIFICATE_REQUEST_SEND_REQUEST 0
2655#define SSL_CERTIFICATE_REQUEST_SKIP 1
2656/* Coordination:
2657 * Check whether a CertificateRequest message should be written.
2658 * Returns a negative code on failure, or
2659 * - SSL_CERTIFICATE_REQUEST_SEND_REQUEST
2660 * - SSL_CERTIFICATE_REQUEST_SKIP
2661 * indicating if the writing of the CertificateRequest
2662 * should be skipped or not.
2663 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002664MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002665static int ssl_tls13_certificate_request_coordinate(mbedtls_ssl_context *ssl)
XiaokangQiana987e1d2022-05-07 01:25:58 +00002666{
2667 int authmode;
2668
XiaokangQian40a35232022-05-07 09:02:40 +00002669#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Gilles Peskine449bd832023-01-11 14:50:10 +01002670 if (ssl->handshake->sni_authmode != MBEDTLS_SSL_VERIFY_UNSET) {
XiaokangQian40a35232022-05-07 09:02:40 +00002671 authmode = ssl->handshake->sni_authmode;
Gilles Peskine449bd832023-01-11 14:50:10 +01002672 } else
XiaokangQian40a35232022-05-07 09:02:40 +00002673#endif
XiaokangQiana987e1d2022-05-07 01:25:58 +00002674 authmode = ssl->conf->authmode;
2675
Gilles Peskine449bd832023-01-11 14:50:10 +01002676 if (authmode == MBEDTLS_SSL_VERIFY_NONE) {
Ronald Croneac00ad2022-09-13 10:16:31 +02002677 ssl->session_negotiate->verify_result = MBEDTLS_X509_BADCERT_SKIP_VERIFY;
Gilles Peskine449bd832023-01-11 14:50:10 +01002678 return SSL_CERTIFICATE_REQUEST_SKIP;
Ronald Croneac00ad2022-09-13 10:16:31 +02002679 }
XiaokangQiana987e1d2022-05-07 01:25:58 +00002680
XiaokangQianc3017f62022-05-13 05:55:41 +00002681 ssl->handshake->certificate_request_sent = 1;
XiaokangQian189ded22022-05-10 08:12:17 +00002682
Gilles Peskine449bd832023-01-11 14:50:10 +01002683 return SSL_CERTIFICATE_REQUEST_SEND_REQUEST;
XiaokangQiana987e1d2022-05-07 01:25:58 +00002684}
2685
XiaokangQiancec9ae62022-05-06 07:28:50 +00002686/*
2687 * struct {
2688 * opaque certificate_request_context<0..2^8-1>;
2689 * Extension extensions<2..2^16-1>;
2690 * } CertificateRequest;
2691 *
2692 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002693MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002694static int ssl_tls13_write_certificate_request_body(mbedtls_ssl_context *ssl,
2695 unsigned char *buf,
2696 const unsigned char *end,
2697 size_t *out_len)
XiaokangQiancec9ae62022-05-06 07:28:50 +00002698{
2699 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
2700 unsigned char *p = buf;
XiaokangQianec6efb92022-05-06 09:53:10 +00002701 size_t output_len = 0;
XiaokangQiancec9ae62022-05-06 07:28:50 +00002702 unsigned char *p_extensions_len;
2703
2704 *out_len = 0;
2705
2706 /* Check if we have enough space:
2707 * - certificate_request_context (1 byte)
2708 * - extensions length (2 bytes)
2709 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002710 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 3);
XiaokangQiancec9ae62022-05-06 07:28:50 +00002711
2712 /*
2713 * Write certificate_request_context
2714 */
2715 /*
2716 * We use a zero length context for the normal handshake
2717 * messages. For post-authentication handshake messages
2718 * this request context would be set to a non-zero value.
2719 */
2720 *p++ = 0x0;
2721
2722 /*
2723 * Write extensions
2724 */
2725 /* The extensions must contain the signature_algorithms. */
2726 p_extensions_len = p;
2727 p += 2;
Gilles Peskine449bd832023-01-11 14:50:10 +01002728 ret = mbedtls_ssl_write_sig_alg_ext(ssl, p, end, &output_len);
2729 if (ret != 0) {
2730 return ret;
2731 }
XiaokangQiancec9ae62022-05-06 07:28:50 +00002732
XiaokangQianec6efb92022-05-06 09:53:10 +00002733 p += output_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01002734 MBEDTLS_PUT_UINT16_BE(p - p_extensions_len - 2, p_extensions_len, 0);
XiaokangQiancec9ae62022-05-06 07:28:50 +00002735
2736 *out_len = p - buf;
2737
Jerry Yu7de2ff02022-11-08 21:43:46 +08002738 MBEDTLS_SSL_PRINT_EXTS(
Gilles Peskine449bd832023-01-11 14:50:10 +01002739 3, MBEDTLS_SSL_HS_CERTIFICATE_REQUEST, ssl->handshake->sent_extensions);
Jerry Yu4b8f2f72022-10-31 13:31:22 +08002740
Gilles Peskine449bd832023-01-11 14:50:10 +01002741 return 0;
XiaokangQiancec9ae62022-05-06 07:28:50 +00002742}
2743
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002744MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002745static int ssl_tls13_write_certificate_request(mbedtls_ssl_context *ssl)
XiaokangQiancec9ae62022-05-06 07:28:50 +00002746{
2747 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
2748
Gilles Peskine449bd832023-01-11 14:50:10 +01002749 MBEDTLS_SSL_DEBUG_MSG(2, ("=> write certificate request"));
XiaokangQiancec9ae62022-05-06 07:28:50 +00002750
Gilles Peskine449bd832023-01-11 14:50:10 +01002751 MBEDTLS_SSL_PROC_CHK_NEG(ssl_tls13_certificate_request_coordinate(ssl));
XiaokangQiancec9ae62022-05-06 07:28:50 +00002752
Gilles Peskine449bd832023-01-11 14:50:10 +01002753 if (ret == SSL_CERTIFICATE_REQUEST_SEND_REQUEST) {
XiaokangQiancec9ae62022-05-06 07:28:50 +00002754 unsigned char *buf;
2755 size_t buf_len, msg_len;
2756
Xiaokang Qian9f1747b2023-03-30 02:50:04 +00002757 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_start_handshake_msg(
2758 ssl, MBEDTLS_SSL_HS_CERTIFICATE_REQUEST,
2759 &buf, &buf_len));
XiaokangQiancec9ae62022-05-06 07:28:50 +00002760
Gilles Peskine449bd832023-01-11 14:50:10 +01002761 MBEDTLS_SSL_PROC_CHK(ssl_tls13_write_certificate_request_body(
2762 ssl, buf, buf + buf_len, &msg_len));
XiaokangQiancec9ae62022-05-06 07:28:50 +00002763
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +01002764 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_add_hs_msg_to_checksum(
Xiaokang Qian9f1747b2023-03-30 02:50:04 +00002765 ssl, MBEDTLS_SSL_HS_CERTIFICATE_REQUEST,
2766 buf, msg_len));
XiaokangQiancec9ae62022-05-06 07:28:50 +00002767
Gilles Peskine449bd832023-01-11 14:50:10 +01002768 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_finish_handshake_msg(
2769 ssl, buf_len, msg_len));
2770 } else if (ret == SSL_CERTIFICATE_REQUEST_SKIP) {
2771 MBEDTLS_SSL_DEBUG_MSG(2, ("<= skip write certificate request"));
XiaokangQiancec9ae62022-05-06 07:28:50 +00002772 ret = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01002773 } else {
2774 MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
XiaokangQiancec9ae62022-05-06 07:28:50 +00002775 ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
2776 goto cleanup;
2777 }
2778
Gilles Peskine449bd832023-01-11 14:50:10 +01002779 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_SERVER_CERTIFICATE);
XiaokangQiancec9ae62022-05-06 07:28:50 +00002780cleanup:
2781
Gilles Peskine449bd832023-01-11 14:50:10 +01002782 MBEDTLS_SSL_DEBUG_MSG(2, ("<= write certificate request"));
2783 return ret;
XiaokangQiancec9ae62022-05-06 07:28:50 +00002784}
XiaokangQiancec9ae62022-05-06 07:28:50 +00002785
Jerry Yu4d3841a2022-04-16 12:37:19 +08002786/*
Jerry Yuc6e6dbf2022-04-16 19:42:57 +08002787 * Handler for MBEDTLS_SSL_SERVER_CERTIFICATE
Jerry Yu83da34e2022-04-16 13:59:52 +08002788 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002789MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002790static int ssl_tls13_write_server_certificate(mbedtls_ssl_context *ssl)
Jerry Yu83da34e2022-04-16 13:59:52 +08002791{
Jerry Yu5a26f302022-05-10 20:46:40 +08002792 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
XiaokangQianfb665a82022-06-15 03:57:21 +00002793
2794#if defined(MBEDTLS_X509_CRT_PARSE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01002795 if ((ssl_tls13_pick_key_cert(ssl) != 0) ||
2796 mbedtls_ssl_own_cert(ssl) == NULL) {
2797 MBEDTLS_SSL_DEBUG_MSG(2, ("No certificate available."));
2798 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE,
2799 MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE);
2800 return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
Jerry Yu5a26f302022-05-10 20:46:40 +08002801 }
XiaokangQianfb665a82022-06-15 03:57:21 +00002802#endif /* MBEDTLS_X509_CRT_PARSE_C */
Jerry Yu5a26f302022-05-10 20:46:40 +08002803
Gilles Peskine449bd832023-01-11 14:50:10 +01002804 ret = mbedtls_ssl_tls13_write_certificate(ssl);
2805 if (ret != 0) {
2806 return ret;
2807 }
2808 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_CERTIFICATE_VERIFY);
2809 return 0;
Jerry Yu83da34e2022-04-16 13:59:52 +08002810}
2811
2812/*
Jerry Yuc6e6dbf2022-04-16 19:42:57 +08002813 * Handler for MBEDTLS_SSL_CERTIFICATE_VERIFY
Jerry Yu83da34e2022-04-16 13:59:52 +08002814 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002815MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002816static int ssl_tls13_write_certificate_verify(mbedtls_ssl_context *ssl)
Jerry Yu83da34e2022-04-16 13:59:52 +08002817{
Gilles Peskine449bd832023-01-11 14:50:10 +01002818 int ret = mbedtls_ssl_tls13_write_certificate_verify(ssl);
2819 if (ret != 0) {
2820 return ret;
2821 }
2822 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_SERVER_FINISHED);
2823 return 0;
Jerry Yu83da34e2022-04-16 13:59:52 +08002824}
Ronald Cron928cbd32022-10-04 16:14:26 +02002825#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED */
Jerry Yu83da34e2022-04-16 13:59:52 +08002826
Jerry Yu9b72e392023-12-01 16:27:08 +08002827/*
2828 * RFC 8446 section A.2
2829 *
2830 * | Send ServerHello
2831 * | K_send = handshake
2832 * | Send EncryptedExtensions
2833 * | [Send CertificateRequest]
2834 * Can send | [Send Certificate + CertificateVerify]
2835 * app data | Send Finished
2836 * after --> | K_send = application
2837 * here +--------+--------+
2838 * No 0-RTT | | 0-RTT
2839 * | |
2840 * K_recv = handshake | | K_recv = early data
2841 * [Skip decrypt errors] | +------> WAIT_EOED -+
2842 * | | Recv | | Recv EndOfEarlyData
2843 * | | early data | | K_recv = handshake
2844 * | +------------+ |
2845 * | |
2846 * +> WAIT_FLIGHT2 <--------+
2847 * |
2848 * +--------+--------+
2849 * No auth | | Client auth
2850 * | |
2851 * | v
2852 * | WAIT_CERT
2853 * | Recv | | Recv Certificate
2854 * | empty | v
2855 * | Certificate | WAIT_CV
2856 * | | | Recv
2857 * | v | CertificateVerify
2858 * +-> WAIT_FINISHED <---+
2859 * | Recv Finished
2860 *
2861 *
2862 * The following function handles the state changes after WAIT_FLIGHT2 in the
Jerry Yu3be85072023-12-04 09:58:54 +08002863 * above diagram. We are not going to receive early data related messages
2864 * anymore, prepare to receive the first handshake message of the client
2865 * second flight.
Jerry Yu9b72e392023-12-01 16:27:08 +08002866 */
Jerry Yu3be85072023-12-04 09:58:54 +08002867static void ssl_tls13_prepare_for_handshake_second_flight(
2868 mbedtls_ssl_context *ssl)
Jerry Yu9b72e392023-12-01 16:27:08 +08002869{
Jerry Yu9b72e392023-12-01 16:27:08 +08002870 if (ssl->handshake->certificate_request_sent) {
2871 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_CLIENT_CERTIFICATE);
2872 } else {
Jerry Yu42020fb2023-12-05 17:35:53 +08002873 MBEDTLS_SSL_DEBUG_MSG(2, ("skip parse certificate"));
Jerry Yuebb1b1d2023-12-05 11:02:15 +08002874 MBEDTLS_SSL_DEBUG_MSG(2, ("skip parse certificate verify"));
Jerry Yuebb1b1d2023-12-05 11:02:15 +08002875
Jerry Yu9b72e392023-12-01 16:27:08 +08002876 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_CLIENT_FINISHED);
2877 }
Jerry Yu9b72e392023-12-01 16:27:08 +08002878}
2879
Jerry Yu83da34e2022-04-16 13:59:52 +08002880/*
Jerry Yud6e253d2022-05-18 13:59:24 +08002881 * Handler for MBEDTLS_SSL_SERVER_FINISHED
Jerry Yu69dd8d42022-04-16 12:51:26 +08002882 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002883MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002884static int ssl_tls13_write_server_finished(mbedtls_ssl_context *ssl)
Jerry Yu69dd8d42022-04-16 12:51:26 +08002885{
Jerry Yud6e253d2022-05-18 13:59:24 +08002886 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yu27bdc7c2022-04-16 13:33:27 +08002887
Gilles Peskine449bd832023-01-11 14:50:10 +01002888 ret = mbedtls_ssl_tls13_write_finished_message(ssl);
2889 if (ret != 0) {
2890 return ret;
2891 }
Jerry Yu27bdc7c2022-04-16 13:33:27 +08002892
Gilles Peskine449bd832023-01-11 14:50:10 +01002893 ret = mbedtls_ssl_tls13_compute_application_transform(ssl);
2894 if (ret != 0) {
Jerry Yue3d67cb2022-05-19 15:33:10 +08002895 MBEDTLS_SSL_PEND_FATAL_ALERT(
Gilles Peskine449bd832023-01-11 14:50:10 +01002896 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE,
2897 MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE);
2898 return ret;
Jerry Yue3d67cb2022-05-19 15:33:10 +08002899 }
XiaokangQianc3017f62022-05-13 05:55:41 +00002900
Jerry Yu87b5ed42022-12-12 13:07:07 +08002901#if defined(MBEDTLS_SSL_EARLY_DATA)
Ronald Cron78a38f62024-02-01 18:30:31 +01002902 if (ssl->handshake->early_data_accepted) {
Jerry Yu0af63dc2023-12-01 17:14:51 +08002903 /* See RFC 8446 section A.2 for more information */
Jerry Yu87b5ed42022-12-12 13:07:07 +08002904 MBEDTLS_SSL_DEBUG_MSG(
2905 1, ("Switch to early keys for inbound traffic. "
2906 "( K_recv = early data )"));
2907 mbedtls_ssl_set_inbound_transform(
2908 ssl, ssl->handshake->transform_earlydata);
2909 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_END_OF_EARLY_DATA);
2910 return 0;
2911 }
2912#endif /* MBEDTLS_SSL_EARLY_DATA */
Jerry Yu0af63dc2023-12-01 17:14:51 +08002913 MBEDTLS_SSL_DEBUG_MSG(
2914 1, ("Switch to handshake keys for inbound traffic "
2915 "( K_recv = handshake )"));
Gilles Peskine449bd832023-01-11 14:50:10 +01002916 mbedtls_ssl_set_inbound_transform(ssl, ssl->handshake->transform_handshake);
XiaokangQian189ded22022-05-10 08:12:17 +00002917
Jerry Yu3be85072023-12-04 09:58:54 +08002918 ssl_tls13_prepare_for_handshake_second_flight(ssl);
Jerry Yu7d8c3fe2022-12-12 12:59:44 +08002919
2920 return 0;
2921}
2922
Jerry Yu87b5ed42022-12-12 13:07:07 +08002923#if defined(MBEDTLS_SSL_EARLY_DATA)
2924/*
Jerry Yu59d420f2023-12-01 16:30:34 +08002925 * Handler for MBEDTLS_SSL_END_OF_EARLY_DATA
Jerry Yu87b5ed42022-12-12 13:07:07 +08002926 */
Jerry Yu3be85072023-12-04 09:58:54 +08002927#define SSL_GOT_END_OF_EARLY_DATA 0
Jerry Yub55f9eb2023-12-05 10:27:17 +08002928#define SSL_GOT_EARLY_DATA 1
Jerry Yud5c34962023-12-01 16:32:31 +08002929/* Coordination:
Jerry Yu3be85072023-12-04 09:58:54 +08002930 * Deals with the ambiguity of not knowing if the next message is an
2931 * EndOfEarlyData message or an application message containing early data.
Jerry Yud5c34962023-12-01 16:32:31 +08002932 * Returns a negative code on failure, or
Jerry Yu3be85072023-12-04 09:58:54 +08002933 * - SSL_GOT_END_OF_EARLY_DATA
Jerry Yub55f9eb2023-12-05 10:27:17 +08002934 * - SSL_GOT_EARLY_DATA
Jerry Yud5c34962023-12-01 16:32:31 +08002935 * indicating which message is received.
2936 */
2937MBEDTLS_CHECK_RETURN_CRITICAL
2938static int ssl_tls13_end_of_early_data_coordinate(mbedtls_ssl_context *ssl)
2939{
2940 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yub4ed4602023-12-01 16:34:00 +08002941
2942 if ((ret = mbedtls_ssl_read_record(ssl, 0)) != 0) {
2943 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_read_record", ret);
2944 return ret;
2945 }
2946 ssl->keep_current_message = 1;
2947
2948 if (ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
2949 ssl->in_msg[0] == MBEDTLS_SSL_HS_END_OF_EARLY_DATA) {
Jerry Yub55f9eb2023-12-05 10:27:17 +08002950 MBEDTLS_SSL_DEBUG_MSG(3, ("Received an end_of_early_data message."));
Jerry Yu3be85072023-12-04 09:58:54 +08002951 return SSL_GOT_END_OF_EARLY_DATA;
Jerry Yub4ed4602023-12-01 16:34:00 +08002952 }
2953
2954 if (ssl->in_msgtype == MBEDTLS_SSL_MSG_APPLICATION_DATA) {
Ronald Croned7d4bf2024-01-31 07:55:19 +01002955 if (ssl->in_offt == NULL) {
Ronald Cron85718042024-02-22 10:22:09 +01002956 MBEDTLS_SSL_DEBUG_MSG(3, ("Received early data"));
Ronald Croned7d4bf2024-01-31 07:55:19 +01002957 /* Set the reading pointer */
2958 ssl->in_offt = ssl->in_msg;
Ronald Cron85718042024-02-22 10:22:09 +01002959 ret = mbedtls_ssl_tls13_check_early_data_len(ssl, ssl->in_msglen);
2960 if (ret != 0) {
2961 return ret;
2962 }
Ronald Croned7d4bf2024-01-31 07:55:19 +01002963 }
Jerry Yub55f9eb2023-12-05 10:27:17 +08002964 return SSL_GOT_EARLY_DATA;
Jerry Yub4ed4602023-12-01 16:34:00 +08002965 }
2966
Jerry Yu7bb40a32023-12-04 10:04:15 +08002967 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE,
2968 MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE);
Jerry Yub4ed4602023-12-01 16:34:00 +08002969 return MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE;
Jerry Yud5c34962023-12-01 16:32:31 +08002970}
2971
2972MBEDTLS_CHECK_RETURN_CRITICAL
2973static int ssl_tls13_parse_end_of_early_data(mbedtls_ssl_context *ssl,
2974 const unsigned char *buf,
2975 const unsigned char *end)
2976{
Jerry Yu75c9ab72023-12-01 16:41:40 +08002977 /* RFC 8446 section 4.5
2978 *
2979 * struct {} EndOfEarlyData;
2980 */
Jerry Yufbf03992023-12-04 10:00:37 +08002981 if (buf != end) {
2982 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR,
2983 MBEDTLS_ERR_SSL_DECODE_ERROR);
2984 return MBEDTLS_ERR_SSL_DECODE_ERROR;
2985 }
2986 return 0;
Jerry Yud5c34962023-12-01 16:32:31 +08002987}
2988
Jerry Yud5c34962023-12-01 16:32:31 +08002989/*
2990 * RFC 8446 section A.2
2991 *
2992 * | Send ServerHello
2993 * | K_send = handshake
2994 * | Send EncryptedExtensions
2995 * | [Send CertificateRequest]
2996 * Can send | [Send Certificate + CertificateVerify]
2997 * app data | Send Finished
2998 * after --> | K_send = application
2999 * here +--------+--------+
3000 * No 0-RTT | | 0-RTT
3001 * | |
3002 * K_recv = handshake | | K_recv = early data
3003 * [Skip decrypt errors] | +------> WAIT_EOED -+
3004 * | | Recv | | Recv EndOfEarlyData
3005 * | | early data | | K_recv = handshake
3006 * | +------------+ |
3007 * | |
3008 * +> WAIT_FLIGHT2 <--------+
3009 * |
3010 * +--------+--------+
3011 * No auth | | Client auth
3012 * | |
3013 * | v
3014 * | WAIT_CERT
3015 * | Recv | | Recv Certificate
3016 * | empty | v
3017 * | Certificate | WAIT_CV
3018 * | | | Recv
3019 * | v | CertificateVerify
3020 * +-> WAIT_FINISHED <---+
3021 * | Recv Finished
3022 *
3023 * The function handles actions and state changes from 0-RTT to WAIT_FLIGHT2 in
3024 * the above diagram.
3025 */
Jerry Yu87b5ed42022-12-12 13:07:07 +08003026MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yu59d420f2023-12-01 16:30:34 +08003027static int ssl_tls13_process_end_of_early_data(mbedtls_ssl_context *ssl)
Jerry Yu87b5ed42022-12-12 13:07:07 +08003028{
3029 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yud5c34962023-12-01 16:32:31 +08003030
3031 MBEDTLS_SSL_DEBUG_MSG(2, ("=> ssl_tls13_process_end_of_early_data"));
3032
3033 MBEDTLS_SSL_PROC_CHK_NEG(ssl_tls13_end_of_early_data_coordinate(ssl));
3034
Jerry Yu3be85072023-12-04 09:58:54 +08003035 if (ret == SSL_GOT_END_OF_EARLY_DATA) {
Jerry Yud5c34962023-12-01 16:32:31 +08003036 unsigned char *buf;
3037 size_t buf_len;
3038
3039 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_tls13_fetch_handshake_msg(
3040 ssl, MBEDTLS_SSL_HS_END_OF_EARLY_DATA,
3041 &buf, &buf_len));
3042
3043 MBEDTLS_SSL_PROC_CHK(ssl_tls13_parse_end_of_early_data(
3044 ssl, buf, buf + buf_len));
3045
Jerry Yue9655122023-12-01 16:44:40 +08003046 MBEDTLS_SSL_DEBUG_MSG(
3047 1, ("Switch to handshake keys for inbound traffic"
3048 "( K_recv = handshake )"));
3049 mbedtls_ssl_set_inbound_transform(
3050 ssl, ssl->handshake->transform_handshake);
3051
Jerry Yud5c34962023-12-01 16:32:31 +08003052 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_add_hs_msg_to_checksum(
3053 ssl, MBEDTLS_SSL_HS_END_OF_EARLY_DATA,
3054 buf, buf_len));
Jerry Yue9655122023-12-01 16:44:40 +08003055
Jerry Yu3be85072023-12-04 09:58:54 +08003056 ssl_tls13_prepare_for_handshake_second_flight(ssl);
Jerry Yud5c34962023-12-01 16:32:31 +08003057
Jerry Yub55f9eb2023-12-05 10:27:17 +08003058 } else if (ret == SSL_GOT_EARLY_DATA) {
Jerry Yud9ca3542023-12-06 17:23:52 +08003059 ret = MBEDTLS_ERR_SSL_RECEIVED_EARLY_DATA;
3060 goto cleanup;
Jerry Yud5c34962023-12-01 16:32:31 +08003061 } else {
3062 MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
3063 ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
3064 goto cleanup;
3065 }
3066
Jerry Yud5c34962023-12-01 16:32:31 +08003067cleanup:
3068 MBEDTLS_SSL_DEBUG_MSG(2, ("<= ssl_tls13_process_end_of_early_data"));
Jerry Yu87b5ed42022-12-12 13:07:07 +08003069 return ret;
3070}
3071#endif /* MBEDTLS_SSL_EARLY_DATA */
3072
Jerry Yu69dd8d42022-04-16 12:51:26 +08003073/*
Jerry Yud6e253d2022-05-18 13:59:24 +08003074 * Handler for MBEDTLS_SSL_CLIENT_FINISHED
Jerry Yu69dd8d42022-04-16 12:51:26 +08003075 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02003076MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01003077static int ssl_tls13_process_client_finished(mbedtls_ssl_context *ssl)
Jerry Yu69dd8d42022-04-16 12:51:26 +08003078{
Jerry Yud6e253d2022-05-18 13:59:24 +08003079 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
3080
Gilles Peskine449bd832023-01-11 14:50:10 +01003081 ret = mbedtls_ssl_tls13_process_finished_message(ssl);
3082 if (ret != 0) {
3083 return ret;
Jerry Yue3d67cb2022-05-19 15:33:10 +08003084 }
3085
Gilles Peskine449bd832023-01-11 14:50:10 +01003086 ret = mbedtls_ssl_tls13_compute_resumption_master_secret(ssl);
3087 if (ret != 0) {
Xiaokang Qian9f1747b2023-03-30 02:50:04 +00003088 MBEDTLS_SSL_DEBUG_RET(
3089 1, "mbedtls_ssl_tls13_compute_resumption_master_secret", ret);
Gilles Peskine449bd832023-01-11 14:50:10 +01003090 }
3091
3092 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_HANDSHAKE_WRAPUP);
3093 return 0;
Jerry Yu69dd8d42022-04-16 12:51:26 +08003094}
3095
3096/*
Jerry Yud6e253d2022-05-18 13:59:24 +08003097 * Handler for MBEDTLS_SSL_HANDSHAKE_WRAPUP
Jerry Yu69dd8d42022-04-16 12:51:26 +08003098 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02003099MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01003100static int ssl_tls13_handshake_wrapup(mbedtls_ssl_context *ssl)
Jerry Yu69dd8d42022-04-16 12:51:26 +08003101{
Gilles Peskine449bd832023-01-11 14:50:10 +01003102 MBEDTLS_SSL_DEBUG_MSG(2, ("handshake: done"));
Jerry Yu03ed50b2022-04-16 17:13:30 +08003103
Gilles Peskine449bd832023-01-11 14:50:10 +01003104 mbedtls_ssl_tls13_handshake_wrapup(ssl);
Jerry Yue67bef42022-07-07 07:29:42 +00003105
Pengyu Lv3643fdb2023-01-12 16:46:28 +08003106#if defined(MBEDTLS_SSL_SESSION_TICKETS) && \
3107 defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED)
Pengyu Lva1aa31b2022-12-13 13:49:59 +08003108/* TODO: Remove the check of SOME_PSK_ENABLED since SESSION_TICKETS requires
3109 * SOME_PSK_ENABLED to be enabled. Here is just to make CI happy. It is
3110 * expected to be resolved with issue#6395.
3111 */
Pengyu Lvc7af2c42022-12-01 16:33:00 +08003112 /* Sent NewSessionTicket message only when client supports PSK */
Pengyu Lvb2cfafb2023-11-14 13:56:13 +08003113 if (mbedtls_ssl_tls13_is_some_psk_supported(ssl)) {
Xiaokang Qian9f1747b2023-03-30 02:50:04 +00003114 mbedtls_ssl_handshake_set_state(
3115 ssl, MBEDTLS_SSL_TLS1_3_NEW_SESSION_TICKET);
Pengyu Lvc7af2c42022-12-01 16:33:00 +08003116 } else
Jerry Yufca4d572022-07-21 10:37:48 +08003117#endif
Pengyu Lv3643fdb2023-01-12 16:46:28 +08003118 {
3119 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_HANDSHAKE_OVER);
3120 }
Gilles Peskine449bd832023-01-11 14:50:10 +01003121 return 0;
Jerry Yu69dd8d42022-04-16 12:51:26 +08003122}
3123
Norbert Fabritiusda0d1692023-01-23 15:24:59 +01003124#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Jerry Yu69dd8d42022-04-16 12:51:26 +08003125/*
Jerry Yua8d3c502022-10-30 14:51:23 +08003126 * Handler for MBEDTLS_SSL_TLS1_3_NEW_SESSION_TICKET
Jerry Yue67bef42022-07-07 07:29:42 +00003127 */
3128#define SSL_NEW_SESSION_TICKET_SKIP 0
3129#define SSL_NEW_SESSION_TICKET_WRITE 1
3130MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01003131static int ssl_tls13_write_new_session_ticket_coordinate(mbedtls_ssl_context *ssl)
Jerry Yue67bef42022-07-07 07:29:42 +00003132{
3133 /* Check whether the use of session tickets is enabled */
Gilles Peskine449bd832023-01-11 14:50:10 +01003134 if (ssl->conf->f_ticket_write == NULL) {
3135 MBEDTLS_SSL_DEBUG_MSG(2, ("NewSessionTicket: disabled,"
3136 " callback is not set"));
3137 return SSL_NEW_SESSION_TICKET_SKIP;
Jerry Yud0766ec2022-09-22 10:46:57 +08003138 }
Gilles Peskine449bd832023-01-11 14:50:10 +01003139 if (ssl->conf->new_session_tickets_count == 0) {
3140 MBEDTLS_SSL_DEBUG_MSG(2, ("NewSessionTicket: disabled,"
3141 " configured count is zero"));
3142 return SSL_NEW_SESSION_TICKET_SKIP;
Jerry Yud0766ec2022-09-22 10:46:57 +08003143 }
3144
Gilles Peskine449bd832023-01-11 14:50:10 +01003145 if (ssl->handshake->new_session_tickets_count == 0) {
3146 MBEDTLS_SSL_DEBUG_MSG(2, ("NewSessionTicket: all tickets have "
3147 "been sent."));
3148 return SSL_NEW_SESSION_TICKET_SKIP;
Jerry Yue67bef42022-07-07 07:29:42 +00003149 }
3150
Gilles Peskine449bd832023-01-11 14:50:10 +01003151 return SSL_NEW_SESSION_TICKET_WRITE;
Jerry Yue67bef42022-07-07 07:29:42 +00003152}
3153
Jerry Yue67bef42022-07-07 07:29:42 +00003154MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01003155static int ssl_tls13_prepare_new_session_ticket(mbedtls_ssl_context *ssl,
3156 unsigned char *ticket_nonce,
3157 size_t ticket_nonce_size)
Jerry Yue67bef42022-07-07 07:29:42 +00003158{
3159 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
3160 mbedtls_ssl_session *session = ssl->session;
3161 mbedtls_ssl_ciphersuite_t *ciphersuite_info;
3162 psa_algorithm_t psa_hash_alg;
3163 int hash_length;
3164
Gilles Peskine449bd832023-01-11 14:50:10 +01003165 MBEDTLS_SSL_DEBUG_MSG(2, ("=> prepare NewSessionTicket msg"));
Jerry Yue67bef42022-07-07 07:29:42 +00003166
Pengyu Lv9f926952022-11-17 15:22:33 +08003167 /* Set ticket_flags depends on the advertised psk key exchange mode */
Pengyu Lv94a42cc2023-12-06 10:04:17 +08003168 mbedtls_ssl_tls13_session_clear_ticket_flags(
Pengyu Lv9eacb442022-12-09 14:39:19 +08003169 session, MBEDTLS_SSL_TLS1_3_TICKET_FLAGS_MASK);
Pengyu Lve6487fe2022-12-06 09:30:29 +08003170#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED)
Pengyu Lv94a42cc2023-12-06 10:04:17 +08003171 mbedtls_ssl_tls13_session_set_ticket_flags(
Pengyu Lv9eacb442022-12-09 14:39:19 +08003172 session, ssl->handshake->tls13_kex_modes);
Pengyu Lve6487fe2022-12-06 09:30:29 +08003173#endif
Jerry Yu95648b02023-12-06 15:03:34 +08003174
3175#if defined(MBEDTLS_SSL_EARLY_DATA)
3176 if (ssl->conf->early_data_enabled == MBEDTLS_SSL_EARLY_DATA_ENABLED &&
3177 ssl->conf->max_early_data_size > 0) {
Pengyu Lv94a42cc2023-12-06 10:04:17 +08003178 mbedtls_ssl_tls13_session_set_ticket_flags(
Jerry Yu95648b02023-12-06 15:03:34 +08003179 session, MBEDTLS_SSL_TLS1_3_TICKET_ALLOW_EARLY_DATA);
Ronald Cronc2865192024-02-07 14:01:03 +01003180 session->max_early_data_size = ssl->conf->max_early_data_size;
Jerry Yu95648b02023-12-06 15:03:34 +08003181 }
3182#endif /* MBEDTLS_SSL_EARLY_DATA */
3183
Pengyu Lv4938a562023-01-16 11:28:49 +08003184 MBEDTLS_SSL_PRINT_TICKET_FLAGS(4, session->ticket_flags);
Pengyu Lv9f926952022-11-17 15:22:33 +08003185
Waleed Elmelegy2824a202024-02-23 17:51:47 +00003186#if defined(MBEDTLS_SSL_EARLY_DATA) && defined(MBEDTLS_SSL_ALPN)
Waleed Elmelegy5bc52632024-03-12 16:25:08 +00003187 if (session->ticket_alpn == NULL) {
3188 ret = mbedtls_ssl_session_set_ticket_alpn(session, ssl->alpn_chosen);
3189 if (ret != 0) {
3190 return ret;
3191 }
Waleed Elmelegy2824a202024-02-23 17:51:47 +00003192 }
3193#endif
3194
Jerry Yue67bef42022-07-07 07:29:42 +00003195 /* Generate ticket_age_add */
Gilles Peskine449bd832023-01-11 14:50:10 +01003196 if ((ret = ssl->conf->f_rng(ssl->conf->p_rng,
3197 (unsigned char *) &session->ticket_age_add,
3198 sizeof(session->ticket_age_add)) != 0)) {
3199 MBEDTLS_SSL_DEBUG_RET(1, "generate_ticket_age_add", ret);
3200 return ret;
Jerry Yue67bef42022-07-07 07:29:42 +00003201 }
Gilles Peskine449bd832023-01-11 14:50:10 +01003202 MBEDTLS_SSL_DEBUG_MSG(3, ("ticket_age_add: %u",
3203 (unsigned int) session->ticket_age_add));
Jerry Yue67bef42022-07-07 07:29:42 +00003204
3205 /* Generate ticket_nonce */
Gilles Peskine449bd832023-01-11 14:50:10 +01003206 ret = ssl->conf->f_rng(ssl->conf->p_rng, ticket_nonce, ticket_nonce_size);
3207 if (ret != 0) {
3208 MBEDTLS_SSL_DEBUG_RET(1, "generate_ticket_nonce", ret);
3209 return ret;
Jerry Yue67bef42022-07-07 07:29:42 +00003210 }
Gilles Peskine449bd832023-01-11 14:50:10 +01003211 MBEDTLS_SSL_DEBUG_BUF(3, "ticket_nonce:",
3212 ticket_nonce, ticket_nonce_size);
Jerry Yue67bef42022-07-07 07:29:42 +00003213
3214 ciphersuite_info =
Gilles Peskine449bd832023-01-11 14:50:10 +01003215 (mbedtls_ssl_ciphersuite_t *) ssl->handshake->ciphersuite_info;
Dave Rodgman2eab4622023-10-05 13:30:37 +01003216 psa_hash_alg = mbedtls_md_psa_alg_from_type((mbedtls_md_type_t) ciphersuite_info->mac);
Gilles Peskine449bd832023-01-11 14:50:10 +01003217 hash_length = PSA_HASH_LENGTH(psa_hash_alg);
3218 if (hash_length == -1 ||
3219 (size_t) hash_length > sizeof(session->resumption_key)) {
3220 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Jerry Yufca4d572022-07-21 10:37:48 +08003221 }
3222
Jerry Yue67bef42022-07-07 07:29:42 +00003223 /* In this code the psk key length equals the length of the hash */
3224 session->resumption_key_len = hash_length;
3225 session->ciphersuite = ciphersuite_info->id;
3226
3227 /* Compute resumption key
3228 *
3229 * HKDF-Expand-Label( resumption_master_secret,
3230 * "resumption", ticket_nonce, Hash.length )
3231 */
3232 ret = mbedtls_ssl_tls13_hkdf_expand_label(
Gilles Peskine449bd832023-01-11 14:50:10 +01003233 psa_hash_alg,
3234 session->app_secrets.resumption_master_secret,
3235 hash_length,
3236 MBEDTLS_SSL_TLS1_3_LBL_WITH_LEN(resumption),
3237 ticket_nonce,
3238 ticket_nonce_size,
3239 session->resumption_key,
3240 hash_length);
Jerry Yue67bef42022-07-07 07:29:42 +00003241
Gilles Peskine449bd832023-01-11 14:50:10 +01003242 if (ret != 0) {
3243 MBEDTLS_SSL_DEBUG_RET(2,
3244 "Creating the ticket-resumed PSK failed",
3245 ret);
3246 return ret;
Jerry Yue67bef42022-07-07 07:29:42 +00003247 }
Gilles Peskine449bd832023-01-11 14:50:10 +01003248 MBEDTLS_SSL_DEBUG_BUF(3, "Ticket-resumed PSK",
3249 session->resumption_key,
3250 session->resumption_key_len);
Jerry Yue67bef42022-07-07 07:29:42 +00003251
Gilles Peskine449bd832023-01-11 14:50:10 +01003252 MBEDTLS_SSL_DEBUG_BUF(3, "resumption_master_secret",
3253 session->app_secrets.resumption_master_secret,
3254 hash_length);
Jerry Yue67bef42022-07-07 07:29:42 +00003255
Gilles Peskine449bd832023-01-11 14:50:10 +01003256 return 0;
Jerry Yue67bef42022-07-07 07:29:42 +00003257}
3258
3259/* This function creates a NewSessionTicket message in the following format:
3260 *
3261 * struct {
3262 * uint32 ticket_lifetime;
3263 * uint32 ticket_age_add;
3264 * opaque ticket_nonce<0..255>;
3265 * opaque ticket<1..2^16-1>;
3266 * Extension extensions<0..2^16-2>;
3267 * } NewSessionTicket;
3268 *
3269 * The ticket inside the NewSessionTicket message is an encrypted container
3270 * carrying the necessary information so that the server is later able to
3271 * re-start the communication.
3272 *
3273 * The following fields are placed inside the ticket by the
3274 * f_ticket_write() function:
3275 *
Jerry Yu0069abc2023-11-23 21:07:28 +08003276 * - creation time (ticket_creation_time)
3277 * - flags (ticket_flags)
Jerry Yue67bef42022-07-07 07:29:42 +00003278 * - age add (ticket_age_add)
Jerry Yu0069abc2023-11-23 21:07:28 +08003279 * - key (resumption_key)
3280 * - key length (resumption_key_len)
Jerry Yue67bef42022-07-07 07:29:42 +00003281 * - ciphersuite (ciphersuite)
Jerry Yu0069abc2023-11-23 21:07:28 +08003282 * - max_early_data_size (max_early_data_size)
Jerry Yue67bef42022-07-07 07:29:42 +00003283 */
3284MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01003285static int ssl_tls13_write_new_session_ticket_body(mbedtls_ssl_context *ssl,
3286 unsigned char *buf,
3287 unsigned char *end,
3288 size_t *out_len,
3289 unsigned char *ticket_nonce,
3290 size_t ticket_nonce_size)
Jerry Yue67bef42022-07-07 07:29:42 +00003291{
3292 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
3293 unsigned char *p = buf;
3294 mbedtls_ssl_session *session = ssl->session;
3295 size_t ticket_len;
3296 uint32_t ticket_lifetime;
Jerry Yu01da35e2022-12-12 15:09:22 +08003297 unsigned char *p_extensions_len;
Jerry Yue67bef42022-07-07 07:29:42 +00003298
3299 *out_len = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01003300 MBEDTLS_SSL_DEBUG_MSG(2, ("=> write NewSessionTicket msg"));
Jerry Yue67bef42022-07-07 07:29:42 +00003301
3302 /*
3303 * ticket_lifetime 4 bytes
3304 * ticket_age_add 4 bytes
3305 * ticket_nonce 1 + ticket_nonce_size bytes
3306 * ticket >=2 bytes
3307 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003308 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 4 + 4 + 1 + ticket_nonce_size + 2);
Jerry Yue67bef42022-07-07 07:29:42 +00003309
3310 /* Generate ticket and ticket_lifetime */
Ronald Cron3c0072b2023-11-22 10:00:14 +01003311#if defined(MBEDTLS_HAVE_TIME)
3312 session->ticket_creation_time = mbedtls_ms_time();
3313#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01003314 ret = ssl->conf->f_ticket_write(ssl->conf->p_ticket,
3315 session,
3316 p + 9 + ticket_nonce_size + 2,
3317 end,
3318 &ticket_len,
3319 &ticket_lifetime);
3320 if (ret != 0) {
3321 MBEDTLS_SSL_DEBUG_RET(1, "write_ticket", ret);
3322 return ret;
Jerry Yue67bef42022-07-07 07:29:42 +00003323 }
Jerry Yuce794882023-11-22 15:01:18 +08003324
3325 /* RFC 8446 section 4.6.1
3326 *
Jerry Yue67bef42022-07-07 07:29:42 +00003327 * ticket_lifetime: Indicates the lifetime in seconds as a 32-bit
Jerry Yuce794882023-11-22 15:01:18 +08003328 * unsigned integer in network byte order from the time of ticket
3329 * issuance. Servers MUST NOT use any value greater than
3330 * 604800 seconds (7 days) ...
Jerry Yue67bef42022-07-07 07:29:42 +00003331 */
Jerry Yu8e0174a2023-11-10 13:58:16 +08003332 if (ticket_lifetime > MBEDTLS_SSL_TLS1_3_MAX_ALLOWED_TICKET_LIFETIME) {
Jerry Yuce794882023-11-22 15:01:18 +08003333 MBEDTLS_SSL_DEBUG_MSG(
3334 1, ("Ticket lifetime (%u) is greater than 7 days.",
3335 (unsigned int) ticket_lifetime));
3336 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Gilles Peskine449bd832023-01-11 14:50:10 +01003337 }
Jerry Yuce794882023-11-22 15:01:18 +08003338
Gilles Peskine449bd832023-01-11 14:50:10 +01003339 MBEDTLS_PUT_UINT32_BE(ticket_lifetime, p, 0);
3340 MBEDTLS_SSL_DEBUG_MSG(3, ("ticket_lifetime: %u",
3341 (unsigned int) ticket_lifetime));
Jerry Yue67bef42022-07-07 07:29:42 +00003342
3343 /* Write ticket_age_add */
Gilles Peskine449bd832023-01-11 14:50:10 +01003344 MBEDTLS_PUT_UINT32_BE(session->ticket_age_add, p, 4);
3345 MBEDTLS_SSL_DEBUG_MSG(3, ("ticket_age_add: %u",
3346 (unsigned int) session->ticket_age_add));
Jerry Yue67bef42022-07-07 07:29:42 +00003347
3348 /* Write ticket_nonce */
Gilles Peskine449bd832023-01-11 14:50:10 +01003349 p[8] = (unsigned char) ticket_nonce_size;
3350 if (ticket_nonce_size > 0) {
3351 memcpy(p + 9, ticket_nonce, ticket_nonce_size);
Jerry Yue67bef42022-07-07 07:29:42 +00003352 }
3353 p += 9 + ticket_nonce_size;
3354
3355 /* Write ticket */
Gilles Peskine449bd832023-01-11 14:50:10 +01003356 MBEDTLS_PUT_UINT16_BE(ticket_len, p, 0);
Jerry Yue67bef42022-07-07 07:29:42 +00003357 p += 2;
Gilles Peskine449bd832023-01-11 14:50:10 +01003358 MBEDTLS_SSL_DEBUG_BUF(4, "ticket", p, ticket_len);
Jerry Yue67bef42022-07-07 07:29:42 +00003359 p += ticket_len;
3360
3361 /* Ticket Extensions
3362 *
Jerry Yu01da35e2022-12-12 15:09:22 +08003363 * Extension extensions<0..2^16-2>;
Jerry Yue67bef42022-07-07 07:29:42 +00003364 */
Jerry Yuedab6372022-10-31 14:37:31 +08003365 ssl->handshake->sent_extensions = MBEDTLS_SSL_EXT_MASK_NONE;
3366
Gilles Peskine449bd832023-01-11 14:50:10 +01003367 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 2);
Jerry Yu01da35e2022-12-12 15:09:22 +08003368 p_extensions_len = p;
Jerry Yue67bef42022-07-07 07:29:42 +00003369 p += 2;
3370
Jerry Yu01da35e2022-12-12 15:09:22 +08003371#if defined(MBEDTLS_SSL_EARLY_DATA)
Pengyu Lv94a42cc2023-12-06 10:04:17 +08003372 if (mbedtls_ssl_tls13_session_ticket_allow_early_data(session)) {
Jerry Yu95648b02023-12-06 15:03:34 +08003373 size_t output_len;
3374
Jerry Yuf135bac2023-11-23 18:10:51 +08003375 if ((ret = mbedtls_ssl_tls13_write_early_data_ext(
Jerry Yuc59c5862023-12-05 10:40:49 +08003376 ssl, 1, p, end, &output_len)) != 0) {
Jerry Yuf135bac2023-11-23 18:10:51 +08003377 MBEDTLS_SSL_DEBUG_RET(
3378 1, "mbedtls_ssl_tls13_write_early_data_ext", ret);
3379 return ret;
3380 }
3381 p += output_len;
Jerry Yu9e7f9bc2023-11-27 16:52:07 +08003382 } else {
3383 MBEDTLS_SSL_DEBUG_MSG(
3384 4, ("early_data not allowed, "
3385 "skip early_data extension in NewSessionTicket"));
Jerry Yu01da35e2022-12-12 15:09:22 +08003386 }
Jerry Yuf135bac2023-11-23 18:10:51 +08003387
Jerry Yu01da35e2022-12-12 15:09:22 +08003388#endif /* MBEDTLS_SSL_EARLY_DATA */
3389
3390 MBEDTLS_PUT_UINT16_BE(p - p_extensions_len - 2, p_extensions_len, 0);
3391
Jerry Yue67bef42022-07-07 07:29:42 +00003392 *out_len = p - buf;
Gilles Peskine449bd832023-01-11 14:50:10 +01003393 MBEDTLS_SSL_DEBUG_BUF(4, "ticket", buf, *out_len);
3394 MBEDTLS_SSL_DEBUG_MSG(2, ("<= write new session ticket"));
Jerry Yue67bef42022-07-07 07:29:42 +00003395
Jerry Yu7de2ff02022-11-08 21:43:46 +08003396 MBEDTLS_SSL_PRINT_EXTS(
Gilles Peskine449bd832023-01-11 14:50:10 +01003397 3, MBEDTLS_SSL_HS_NEW_SESSION_TICKET, ssl->handshake->sent_extensions);
Jerry Yu4b8f2f72022-10-31 13:31:22 +08003398
Gilles Peskine449bd832023-01-11 14:50:10 +01003399 return 0;
Jerry Yue67bef42022-07-07 07:29:42 +00003400}
3401
3402/*
Jerry Yua8d3c502022-10-30 14:51:23 +08003403 * Handler for MBEDTLS_SSL_TLS1_3_NEW_SESSION_TICKET
Jerry Yue67bef42022-07-07 07:29:42 +00003404 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003405static int ssl_tls13_write_new_session_ticket(mbedtls_ssl_context *ssl)
Jerry Yue67bef42022-07-07 07:29:42 +00003406{
3407 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
3408
Gilles Peskine449bd832023-01-11 14:50:10 +01003409 MBEDTLS_SSL_PROC_CHK_NEG(ssl_tls13_write_new_session_ticket_coordinate(ssl));
Jerry Yue67bef42022-07-07 07:29:42 +00003410
Gilles Peskine449bd832023-01-11 14:50:10 +01003411 if (ret == SSL_NEW_SESSION_TICKET_WRITE) {
Jerry Yue67bef42022-07-07 07:29:42 +00003412 unsigned char ticket_nonce[MBEDTLS_SSL_TLS1_3_TICKET_NONCE_LENGTH];
3413 unsigned char *buf;
3414 size_t buf_len, msg_len;
3415
Gilles Peskine449bd832023-01-11 14:50:10 +01003416 MBEDTLS_SSL_PROC_CHK(ssl_tls13_prepare_new_session_ticket(
3417 ssl, ticket_nonce, sizeof(ticket_nonce)));
Jerry Yue67bef42022-07-07 07:29:42 +00003418
Xiaokang Qian9f1747b2023-03-30 02:50:04 +00003419 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_start_handshake_msg(
3420 ssl, MBEDTLS_SSL_HS_NEW_SESSION_TICKET,
3421 &buf, &buf_len));
Jerry Yue67bef42022-07-07 07:29:42 +00003422
Gilles Peskine449bd832023-01-11 14:50:10 +01003423 MBEDTLS_SSL_PROC_CHK(ssl_tls13_write_new_session_ticket_body(
3424 ssl, buf, buf + buf_len, &msg_len,
3425 ticket_nonce, sizeof(ticket_nonce)));
Jerry Yue67bef42022-07-07 07:29:42 +00003426
Gilles Peskine449bd832023-01-11 14:50:10 +01003427 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_finish_handshake_msg(
3428 ssl, buf_len, msg_len));
Jerry Yufca4d572022-07-21 10:37:48 +08003429
Jerry Yu359e65f2022-09-22 23:47:43 +08003430 /* Limit session tickets count to one when resumption connection.
3431 *
3432 * See document of mbedtls_ssl_conf_new_session_tickets.
3433 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003434 if (ssl->handshake->resume == 1) {
Jerry Yu359e65f2022-09-22 23:47:43 +08003435 ssl->handshake->new_session_tickets_count = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01003436 } else {
Jerry Yu359e65f2022-09-22 23:47:43 +08003437 ssl->handshake->new_session_tickets_count--;
Gilles Peskine449bd832023-01-11 14:50:10 +01003438 }
Jerry Yub7e3fa72022-09-22 11:07:18 +08003439
Jerry Yua8d3c502022-10-30 14:51:23 +08003440 mbedtls_ssl_handshake_set_state(
Gilles Peskine449bd832023-01-11 14:50:10 +01003441 ssl, MBEDTLS_SSL_TLS1_3_NEW_SESSION_TICKET_FLUSH);
3442 } else {
3443 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_HANDSHAKE_OVER);
Jerry Yue67bef42022-07-07 07:29:42 +00003444 }
3445
Jerry Yue67bef42022-07-07 07:29:42 +00003446cleanup:
3447
Gilles Peskine449bd832023-01-11 14:50:10 +01003448 return ret;
Jerry Yue67bef42022-07-07 07:29:42 +00003449}
3450#endif /* MBEDTLS_SSL_SESSION_TICKETS */
3451
3452/*
XiaokangQiane8ff3502022-04-22 02:34:40 +00003453 * TLS 1.3 State Machine -- server side
XiaokangQian7807f9f2022-02-15 10:04:37 +00003454 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003455int mbedtls_ssl_tls13_handshake_server_step(mbedtls_ssl_context *ssl)
Jerry Yub9930e72021-08-06 17:11:51 +08003456{
XiaokangQian08037552022-04-20 07:16:41 +00003457 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
XiaokangQian7807f9f2022-02-15 10:04:37 +00003458
Gilles Peskine449bd832023-01-11 14:50:10 +01003459 if (ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER || ssl->handshake == NULL) {
3460 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
3461 }
XiaokangQian7807f9f2022-02-15 10:04:37 +00003462
Gilles Peskine449bd832023-01-11 14:50:10 +01003463 MBEDTLS_SSL_DEBUG_MSG(2, ("tls13 server state: %s(%d)",
Dave Rodgman2eab4622023-10-05 13:30:37 +01003464 mbedtls_ssl_states_str((mbedtls_ssl_states) ssl->state),
Gilles Peskine449bd832023-01-11 14:50:10 +01003465 ssl->state));
Jerry Yu6e81b272021-09-27 11:16:17 +08003466
Gilles Peskine449bd832023-01-11 14:50:10 +01003467 switch (ssl->state) {
XiaokangQian7807f9f2022-02-15 10:04:37 +00003468 /* start state */
3469 case MBEDTLS_SSL_HELLO_REQUEST:
Gilles Peskine449bd832023-01-11 14:50:10 +01003470 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_CLIENT_HELLO);
XiaokangQian08037552022-04-20 07:16:41 +00003471 ret = 0;
XiaokangQian7807f9f2022-02-15 10:04:37 +00003472 break;
3473
XiaokangQian7807f9f2022-02-15 10:04:37 +00003474 case MBEDTLS_SSL_CLIENT_HELLO:
Gilles Peskine449bd832023-01-11 14:50:10 +01003475 ret = ssl_tls13_process_client_hello(ssl);
3476 if (ret != 0) {
3477 MBEDTLS_SSL_DEBUG_RET(1, "ssl_tls13_process_client_hello", ret);
3478 }
Jerry Yuf41553b2022-05-09 22:20:30 +08003479 break;
XiaokangQian7807f9f2022-02-15 10:04:37 +00003480
Jerry Yuf41553b2022-05-09 22:20:30 +08003481 case MBEDTLS_SSL_HELLO_RETRY_REQUEST:
Gilles Peskine449bd832023-01-11 14:50:10 +01003482 ret = ssl_tls13_write_hello_retry_request(ssl);
3483 if (ret != 0) {
3484 MBEDTLS_SSL_DEBUG_RET(1, "ssl_tls13_write_hello_retry_request", ret);
3485 return ret;
Jerry Yuf41553b2022-05-09 22:20:30 +08003486 }
XiaokangQian7807f9f2022-02-15 10:04:37 +00003487 break;
3488
Jerry Yu5b64ae92022-03-30 17:15:02 +08003489 case MBEDTLS_SSL_SERVER_HELLO:
Gilles Peskine449bd832023-01-11 14:50:10 +01003490 ret = ssl_tls13_write_server_hello(ssl);
Jerry Yu5b64ae92022-03-30 17:15:02 +08003491 break;
3492
Xiaofei Baicba64af2022-02-15 10:00:56 +00003493 case MBEDTLS_SSL_ENCRYPTED_EXTENSIONS:
Gilles Peskine449bd832023-01-11 14:50:10 +01003494 ret = ssl_tls13_write_encrypted_extensions(ssl);
3495 if (ret != 0) {
3496 MBEDTLS_SSL_DEBUG_RET(1, "ssl_tls13_write_encrypted_extensions", ret);
3497 return ret;
Xiaofei Baicba64af2022-02-15 10:00:56 +00003498 }
Jerry Yu48330562022-05-06 21:35:44 +08003499 break;
Jerry Yu6a2cd9e2022-05-05 11:14:19 +08003500
Ronald Cron928cbd32022-10-04 16:14:26 +02003501#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED)
Xiaofei Bai9ca09d42022-02-14 12:57:18 +00003502 case MBEDTLS_SSL_CERTIFICATE_REQUEST:
Gilles Peskine449bd832023-01-11 14:50:10 +01003503 ret = ssl_tls13_write_certificate_request(ssl);
Xiaofei Bai9ca09d42022-02-14 12:57:18 +00003504 break;
Xiaofei Bai9ca09d42022-02-14 12:57:18 +00003505
Jerry Yu83da34e2022-04-16 13:59:52 +08003506 case MBEDTLS_SSL_SERVER_CERTIFICATE:
Gilles Peskine449bd832023-01-11 14:50:10 +01003507 ret = ssl_tls13_write_server_certificate(ssl);
Jerry Yu83da34e2022-04-16 13:59:52 +08003508 break;
3509
3510 case MBEDTLS_SSL_CERTIFICATE_VERIFY:
Gilles Peskine449bd832023-01-11 14:50:10 +01003511 ret = ssl_tls13_write_certificate_verify(ssl);
Jerry Yu83da34e2022-04-16 13:59:52 +08003512 break;
Ronald Cron928cbd32022-10-04 16:14:26 +02003513#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED */
Jerry Yu83da34e2022-04-16 13:59:52 +08003514
Gilles Peskine449bd832023-01-11 14:50:10 +01003515 /*
3516 * Injection of dummy-CCS's for middlebox compatibility
3517 */
Gabor Mezei7b39bf12022-05-24 16:04:14 +02003518#if defined(MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE)
Gabor Mezeif7044ea2022-06-28 16:01:49 +02003519 case MBEDTLS_SSL_SERVER_CCS_AFTER_HELLO_RETRY_REQUEST:
Gilles Peskine449bd832023-01-11 14:50:10 +01003520 ret = mbedtls_ssl_tls13_write_change_cipher_spec(ssl);
3521 if (ret == 0) {
3522 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_CLIENT_HELLO);
3523 }
Gabor Mezei7b39bf12022-05-24 16:04:14 +02003524 break;
3525
3526 case MBEDTLS_SSL_SERVER_CCS_AFTER_SERVER_HELLO:
Ronald Crone273f722024-02-13 18:22:26 +01003527 ret = mbedtls_ssl_tls13_write_change_cipher_spec(ssl);
3528 if (ret != 0) {
3529 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01003530 }
Ronald Cronfe59ff72024-01-24 14:31:50 +01003531 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_ENCRYPTED_EXTENSIONS);
Gabor Mezei7b39bf12022-05-24 16:04:14 +02003532 break;
3533#endif /* MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE */
3534
Jerry Yu69dd8d42022-04-16 12:51:26 +08003535 case MBEDTLS_SSL_SERVER_FINISHED:
Gilles Peskine449bd832023-01-11 14:50:10 +01003536 ret = ssl_tls13_write_server_finished(ssl);
Jerry Yu69dd8d42022-04-16 12:51:26 +08003537 break;
3538
Jerry Yu87b5ed42022-12-12 13:07:07 +08003539#if defined(MBEDTLS_SSL_EARLY_DATA)
3540 case MBEDTLS_SSL_END_OF_EARLY_DATA:
Jerry Yu59d420f2023-12-01 16:30:34 +08003541 ret = ssl_tls13_process_end_of_early_data(ssl);
Jerry Yu87b5ed42022-12-12 13:07:07 +08003542 break;
3543#endif /* MBEDTLS_SSL_EARLY_DATA */
3544
Jerry Yu69dd8d42022-04-16 12:51:26 +08003545 case MBEDTLS_SSL_CLIENT_FINISHED:
Gilles Peskine449bd832023-01-11 14:50:10 +01003546 ret = ssl_tls13_process_client_finished(ssl);
Jerry Yu69dd8d42022-04-16 12:51:26 +08003547 break;
3548
Jerry Yu69dd8d42022-04-16 12:51:26 +08003549 case MBEDTLS_SSL_HANDSHAKE_WRAPUP:
Gilles Peskine449bd832023-01-11 14:50:10 +01003550 ret = ssl_tls13_handshake_wrapup(ssl);
Jerry Yu69dd8d42022-04-16 12:51:26 +08003551 break;
3552
Ronald Cron766c0cd2022-10-18 12:17:11 +02003553#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED)
XiaokangQian6b916b12022-04-25 07:29:34 +00003554 case MBEDTLS_SSL_CLIENT_CERTIFICATE:
Gilles Peskine449bd832023-01-11 14:50:10 +01003555 ret = mbedtls_ssl_tls13_process_certificate(ssl);
3556 if (ret == 0) {
3557 if (ssl->session_negotiate->peer_cert != NULL) {
Ronald Cron209cae92022-06-07 10:30:19 +02003558 mbedtls_ssl_handshake_set_state(
Gilles Peskine449bd832023-01-11 14:50:10 +01003559 ssl, MBEDTLS_SSL_CLIENT_CERTIFICATE_VERIFY);
3560 } else {
3561 MBEDTLS_SSL_DEBUG_MSG(2, ("skip parse certificate verify"));
Ronald Cron209cae92022-06-07 10:30:19 +02003562 mbedtls_ssl_handshake_set_state(
Gilles Peskine449bd832023-01-11 14:50:10 +01003563 ssl, MBEDTLS_SSL_CLIENT_FINISHED);
Ronald Cron19385882022-06-15 16:26:13 +02003564 }
XiaokangQian6b916b12022-04-25 07:29:34 +00003565 }
3566 break;
3567
3568 case MBEDTLS_SSL_CLIENT_CERTIFICATE_VERIFY:
Gilles Peskine449bd832023-01-11 14:50:10 +01003569 ret = mbedtls_ssl_tls13_process_certificate_verify(ssl);
3570 if (ret == 0) {
XiaokangQian6b916b12022-04-25 07:29:34 +00003571 mbedtls_ssl_handshake_set_state(
Gilles Peskine449bd832023-01-11 14:50:10 +01003572 ssl, MBEDTLS_SSL_CLIENT_FINISHED);
XiaokangQian6b916b12022-04-25 07:29:34 +00003573 }
3574 break;
Ronald Cron766c0cd2022-10-18 12:17:11 +02003575#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED */
XiaokangQian6b916b12022-04-25 07:29:34 +00003576
Jerry Yue67bef42022-07-07 07:29:42 +00003577#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Jerry Yua8d3c502022-10-30 14:51:23 +08003578 case MBEDTLS_SSL_TLS1_3_NEW_SESSION_TICKET:
Gilles Peskine449bd832023-01-11 14:50:10 +01003579 ret = ssl_tls13_write_new_session_ticket(ssl);
3580 if (ret != 0) {
3581 MBEDTLS_SSL_DEBUG_RET(1,
3582 "ssl_tls13_write_new_session_ticket ",
3583 ret);
Jerry Yue67bef42022-07-07 07:29:42 +00003584 }
3585 break;
Jerry Yua8d3c502022-10-30 14:51:23 +08003586 case MBEDTLS_SSL_TLS1_3_NEW_SESSION_TICKET_FLUSH:
Jerry Yue67bef42022-07-07 07:29:42 +00003587 /* This state is necessary to do the flush of the New Session
Jerry Yua8d3c502022-10-30 14:51:23 +08003588 * Ticket message written in MBEDTLS_SSL_TLS1_3_NEW_SESSION_TICKET
Jerry Yue67bef42022-07-07 07:29:42 +00003589 * as part of ssl_prepare_handshake_step.
3590 */
3591 ret = 0;
Jerry Yud4e75002022-08-09 13:33:50 +08003592
Gilles Peskine449bd832023-01-11 14:50:10 +01003593 if (ssl->handshake->new_session_tickets_count == 0) {
3594 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_HANDSHAKE_OVER);
3595 } else {
Xiaokang Qian9f1747b2023-03-30 02:50:04 +00003596 mbedtls_ssl_handshake_set_state(
3597 ssl, MBEDTLS_SSL_TLS1_3_NEW_SESSION_TICKET);
Gilles Peskine449bd832023-01-11 14:50:10 +01003598 }
Jerry Yue67bef42022-07-07 07:29:42 +00003599 break;
3600
3601#endif /* MBEDTLS_SSL_SESSION_TICKETS */
3602
XiaokangQian7807f9f2022-02-15 10:04:37 +00003603 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01003604 MBEDTLS_SSL_DEBUG_MSG(1, ("invalid state %d", ssl->state));
3605 return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
XiaokangQian7807f9f2022-02-15 10:04:37 +00003606 }
3607
Gilles Peskine449bd832023-01-11 14:50:10 +01003608 return ret;
Jerry Yub9930e72021-08-06 17:11:51 +08003609}
Jerry Yu3cc4c2a2021-08-06 16:29:08 +08003610
Jerry Yufb4b6472022-01-27 15:03:26 +08003611#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_PROTO_TLS1_3 */