blob: 4b6845d6c56aaf4670558b1581f9be663377c76a [file] [log] [blame]
Jerry Yu3cc4c2a2021-08-06 16:29:08 +08001/*
Jerry Yub9930e72021-08-06 17:11:51 +08002 * TLS 1.3 server-side functions
Jerry Yu3cc4c2a2021-08-06 16:29:08 +08003 *
4 * Copyright The Mbed TLS Contributors
Dave Rodgman16799db2023-11-02 19:47:20 +00005 * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
Gilles Peskine449bd832023-01-11 14:50:10 +01006 */
Jerry Yu3cc4c2a2021-08-06 16:29:08 +08007
8#include "common.h"
9
Jerry Yufb4b6472022-01-27 15:03:26 +080010#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_PROTO_TLS1_3)
Jerry Yu3cc4c2a2021-08-06 16:29:08 +080011
Valerio Settib4f50762024-01-17 10:24:52 +010012#include "debug_internal.h"
Xiaofei Baicba64af2022-02-15 10:00:56 +000013#include "mbedtls/error.h"
14#include "mbedtls/platform.h"
Jerry Yu1c105562022-07-10 06:32:38 +000015#include "mbedtls/constant_time.h"
Manuel Pégourié-Gonnardd55d66f2023-06-20 10:14:58 +020016#include "mbedtls/oid.h"
Valerio Setti384fbde2024-01-02 13:26:40 +010017#include "mbedtls/psa_util.h"
Jerry Yu3bf2c642022-03-30 22:02:12 +080018
Xiaofei Bai9ca09d42022-02-14 12:57:18 +000019#include "ssl_misc.h"
20#include "ssl_tls13_keys.h"
21#include "ssl_debug_helpers.h"
22
Jerry Yuf35ba382022-08-23 17:58:26 +080023
Jerry Yuc5a23a02022-08-25 10:51:44 +080024static const mbedtls_ssl_ciphersuite_t *ssl_tls13_validate_peer_ciphersuite(
Gilles Peskine449bd832023-01-11 14:50:10 +010025 mbedtls_ssl_context *ssl,
26 unsigned int cipher_suite)
Jerry Yuf35ba382022-08-23 17:58:26 +080027{
28 const mbedtls_ssl_ciphersuite_t *ciphersuite_info;
Gilles Peskine449bd832023-01-11 14:50:10 +010029 if (!mbedtls_ssl_tls13_cipher_suite_is_offered(ssl, cipher_suite)) {
30 return NULL;
Jerry Yuf35ba382022-08-23 17:58:26 +080031 }
Gilles Peskine449bd832023-01-11 14:50:10 +010032
33 ciphersuite_info = mbedtls_ssl_ciphersuite_from_id(cipher_suite);
34 if ((mbedtls_ssl_validate_ciphersuite(ssl, ciphersuite_info,
35 ssl->tls_version,
36 ssl->tls_version) != 0)) {
37 return NULL;
38 }
39 return ciphersuite_info;
Jerry Yuf35ba382022-08-23 17:58:26 +080040}
41
Ronald Cron89089cc2024-02-14 18:18:08 +010042static void ssl_tls13_select_ciphersuite(
43 mbedtls_ssl_context *ssl,
44 const unsigned char *cipher_suites,
45 const unsigned char *cipher_suites_end,
46 int psk_ciphersuite_id,
47 psa_algorithm_t psk_hash_alg,
48 const mbedtls_ssl_ciphersuite_t **selected_ciphersuite_info)
49{
50 *selected_ciphersuite_info = NULL;
51
52 /*
Ronald Cron38117652024-03-05 09:05:46 +010053 * In a compliant ClientHello the byte-length of the list of ciphersuites
54 * is even and this function relies on this fact. This should have been
55 * checked in the main ClientHello parsing function. Double check here.
Ronald Cron89089cc2024-02-14 18:18:08 +010056 */
57 if ((cipher_suites_end - cipher_suites) & 1) {
58 return;
59 }
60
61 for (const unsigned char *p = cipher_suites;
62 p < cipher_suites_end; p += 2) {
63 /*
64 * "cipher_suites_end - p is even" is an invariant of the loop. As
65 * cipher_suites_end - p > 0, we have cipher_suites_end - p >= 2 and it
66 * is thus safe to read two bytes.
67 */
68 uint16_t id = MBEDTLS_GET_UINT16_BE(p, 0);
69
70 const mbedtls_ssl_ciphersuite_t *info =
71 ssl_tls13_validate_peer_ciphersuite(ssl, id);
72 if (info == NULL) {
73 continue;
74 }
75
76 /*
77 * If a valid PSK ciphersuite identifier has been passed in, we seek
78 * for an exact match.
79 */
80 if (psk_ciphersuite_id != 0) {
81 if (id != psk_ciphersuite_id) {
82 continue;
83 }
84 } else if (psk_hash_alg != PSA_ALG_NONE) {
85 if (mbedtls_md_psa_alg_from_type((mbedtls_md_type_t) info->mac) !=
86 psk_hash_alg) {
87 continue;
88 }
89 }
90
91 *selected_ciphersuite_info = info;
92 return;
93 }
94
95 MBEDTLS_SSL_DEBUG_MSG(2, ("No matched ciphersuite"));
96}
97
Ronald Cron41a443a2022-10-04 16:38:25 +020098#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED)
Jerry Yue19e3b92022-07-08 12:04:51 +000099/* From RFC 8446:
100 *
101 * enum { psk_ke(0), psk_dhe_ke(1), (255) } PskKeyExchangeMode;
102 * struct {
103 * PskKeyExchangeMode ke_modes<1..255>;
104 * } PskKeyExchangeModes;
105 */
Jerry Yu6e74a7e2022-07-20 20:49:32 +0800106MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100107static int ssl_tls13_parse_key_exchange_modes_ext(mbedtls_ssl_context *ssl,
108 const unsigned char *buf,
109 const unsigned char *end)
Jerry Yue19e3b92022-07-08 12:04:51 +0000110{
Jerry Yu299e31f2022-07-13 23:06:36 +0800111 const unsigned char *p = buf;
Jerry Yue19e3b92022-07-08 12:04:51 +0000112 size_t ke_modes_len;
113 int ke_modes = 0;
114
Jerry Yu854dd9e2022-07-15 14:28:27 +0800115 /* Read ke_modes length (1 Byte) */
Gilles Peskine449bd832023-01-11 14:50:10 +0100116 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, 1);
Jerry Yu299e31f2022-07-13 23:06:36 +0800117 ke_modes_len = *p++;
Jerry Yue19e3b92022-07-08 12:04:51 +0000118 /* Currently, there are only two PSK modes, so even without looking
119 * at the content, something's wrong if the list has more than 2 items. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100120 if (ke_modes_len > 2) {
121 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER,
122 MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER);
123 return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
Jerry Yu299e31f2022-07-13 23:06:36 +0800124 }
Jerry Yue19e3b92022-07-08 12:04:51 +0000125
Gilles Peskine449bd832023-01-11 14:50:10 +0100126 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, ke_modes_len);
Jerry Yue19e3b92022-07-08 12:04:51 +0000127
Gilles Peskine449bd832023-01-11 14:50:10 +0100128 while (ke_modes_len-- != 0) {
129 switch (*p++) {
130 case MBEDTLS_SSL_TLS1_3_PSK_MODE_PURE:
131 ke_modes |= MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK;
132 MBEDTLS_SSL_DEBUG_MSG(3, ("Found PSK KEX MODE"));
133 break;
134 case MBEDTLS_SSL_TLS1_3_PSK_MODE_ECDHE:
135 ke_modes |= MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK_EPHEMERAL;
136 MBEDTLS_SSL_DEBUG_MSG(3, ("Found PSK_EPHEMERAL KEX MODE"));
137 break;
138 default:
139 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER,
140 MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER);
141 return MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER;
Jerry Yue19e3b92022-07-08 12:04:51 +0000142 }
143 }
144
145 ssl->handshake->tls13_kex_modes = ke_modes;
Gilles Peskine449bd832023-01-11 14:50:10 +0100146 return 0;
Jerry Yue19e3b92022-07-08 12:04:51 +0000147}
Jerry Yu1c105562022-07-10 06:32:38 +0000148
Ronald Cron38117652024-03-05 09:05:46 +0100149/*
150 * Non-error return values of
151 * ssl_tls13_offered_psks_check_identity_match_ticket() and
152 * ssl_tls13_offered_psks_check_identity_match(). They are positive to
153 * not collide with error codes that are negative. Zero
154 * (SSL_TLS1_3_PSK_IDENTITY_MATCH) in case of success as it may be propagated
155 * up by the callers of this function as a generic success condition.
156 *
157 * The return value SSL_TLS1_3_PSK_IDENTITY_MATCH_BUT_PSK_NOT_USABLE means
158 * that the pre-shared-key identity matches that of a ticket or an external
159 * provisioned pre-shared-key. We have thus been able to retrieve the
160 * attributes of the pre-shared-key but at least one of them does not meet
161 * some criteria and the pre-shared-key cannot be used. For example, a ticket
162 * is expired or its version is not TLS 1.3. Note eventually that the return
163 * value SSL_TLS1_3_PSK_IDENTITY_MATCH_BUT_PSK_NOT_USABLE does not have
164 * anything to do with binder check. A binder check is done only when a
165 * suitable pre-shared-key has been selected and only for that selected
166 * pre-shared-key: if the binder check fails, we fail the handshake and we do
167 * not try to find another pre-shared-key for which the binder check would
168 * succeed as recommended by the specification.
169 */
Ronald Cronfbae94a2023-12-05 18:15:14 +0100170#define SSL_TLS1_3_PSK_IDENTITY_DOES_NOT_MATCH 2
171#define SSL_TLS1_3_PSK_IDENTITY_MATCH_BUT_PSK_NOT_USABLE 1
172#define SSL_TLS1_3_PSK_IDENTITY_MATCH 0
Jerry Yu95699e72022-08-21 19:22:23 +0800173
174#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Pengyu Lv29daf4a2023-10-30 17:13:30 +0800175MBEDTLS_CHECK_RETURN_CRITICAL
Pengyu Lvbc4aab72023-12-01 15:37:24 +0800176static int ssl_tls13_key_exchange_is_psk_available(mbedtls_ssl_context *ssl);
Pengyu Lv29daf4a2023-10-30 17:13:30 +0800177MBEDTLS_CHECK_RETURN_CRITICAL
Pengyu Lvbc4aab72023-12-01 15:37:24 +0800178static int ssl_tls13_key_exchange_is_psk_ephemeral_available(mbedtls_ssl_context *ssl);
Jerry Yu95699e72022-08-21 19:22:23 +0800179
180MBEDTLS_CHECK_RETURN_CRITICAL
181static int ssl_tls13_offered_psks_check_identity_match_ticket(
Gilles Peskine449bd832023-01-11 14:50:10 +0100182 mbedtls_ssl_context *ssl,
183 const unsigned char *identity,
184 size_t identity_len,
185 uint32_t obfuscated_ticket_age,
186 mbedtls_ssl_session *session)
Jerry Yu95699e72022-08-21 19:22:23 +0800187{
Jerry Yufd310eb2022-09-06 09:16:35 +0800188 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yu95699e72022-08-21 19:22:23 +0800189 unsigned char *ticket_buffer;
Jerry Yu8d4bbba2022-09-13 14:15:48 +0800190#if defined(MBEDTLS_HAVE_TIME)
Jerry Yucebffc32022-12-15 18:00:05 +0800191 mbedtls_ms_time_t now;
192 mbedtls_ms_time_t server_age;
Jerry Yu713ce1f2023-11-17 15:32:12 +0800193 uint32_t client_age;
Jerry Yucebffc32022-12-15 18:00:05 +0800194 mbedtls_ms_time_t age_diff;
Jerry Yu8d4bbba2022-09-13 14:15:48 +0800195#endif
Jerry Yu95699e72022-08-21 19:22:23 +0800196
197 ((void) obfuscated_ticket_age);
198
Gilles Peskine449bd832023-01-11 14:50:10 +0100199 MBEDTLS_SSL_DEBUG_MSG(2, ("=> check_identity_match_ticket"));
Jerry Yu95699e72022-08-21 19:22:23 +0800200
Jerry Yufd310eb2022-09-06 09:16:35 +0800201 /* Ticket parser is not configured, Skip */
Gilles Peskine449bd832023-01-11 14:50:10 +0100202 if (ssl->conf->f_ticket_parse == NULL || identity_len == 0) {
Ronald Cronfbae94a2023-12-05 18:15:14 +0100203 return SSL_TLS1_3_PSK_IDENTITY_DOES_NOT_MATCH;
Gilles Peskine449bd832023-01-11 14:50:10 +0100204 }
Jerry Yu95699e72022-08-21 19:22:23 +0800205
Jerry Yu4746b102022-09-13 11:11:48 +0800206 /* We create a copy of the encrypted ticket since the ticket parsing
207 * function is allowed to use its input buffer as an output buffer
208 * (in-place decryption). We do, however, need the original buffer for
209 * computing the PSK binder value.
Jerry Yu95699e72022-08-21 19:22:23 +0800210 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100211 ticket_buffer = mbedtls_calloc(1, identity_len);
212 if (ticket_buffer == NULL) {
213 MBEDTLS_SSL_DEBUG_MSG(1, ("buffer too small"));
214 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
Jerry Yu95699e72022-08-21 19:22:23 +0800215 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100216 memcpy(ticket_buffer, identity, identity_len);
Jerry Yu95699e72022-08-21 19:22:23 +0800217
Ronald Cronfbae94a2023-12-05 18:15:14 +0100218 ret = ssl->conf->f_ticket_parse(ssl->conf->p_ticket,
219 session,
220 ticket_buffer, identity_len);
221 if (ret == 0) {
222 ret = SSL_TLS1_3_PSK_IDENTITY_MATCH;
223 } else {
224 if (ret == MBEDTLS_ERR_SSL_SESSION_TICKET_EXPIRED) {
Gilles Peskine449bd832023-01-11 14:50:10 +0100225 MBEDTLS_SSL_DEBUG_MSG(3, ("ticket is expired"));
Ronald Cronfbae94a2023-12-05 18:15:14 +0100226 ret = SSL_TLS1_3_PSK_IDENTITY_MATCH_BUT_PSK_NOT_USABLE;
Gilles Peskine449bd832023-01-11 14:50:10 +0100227 } else {
Ronald Cronfbae94a2023-12-05 18:15:14 +0100228 if (ret == MBEDTLS_ERR_SSL_INVALID_MAC) {
229 MBEDTLS_SSL_DEBUG_MSG(3, ("ticket is not authentic"));
230 } else {
231 MBEDTLS_SSL_DEBUG_RET(1, "ticket_parse", ret);
232 }
233 ret = SSL_TLS1_3_PSK_IDENTITY_DOES_NOT_MATCH;
Gilles Peskine449bd832023-01-11 14:50:10 +0100234 }
Jerry Yu95699e72022-08-21 19:22:23 +0800235 }
236
237 /* We delete the temporary buffer */
Gilles Peskine449bd832023-01-11 14:50:10 +0100238 mbedtls_free(ticket_buffer);
Jerry Yu95699e72022-08-21 19:22:23 +0800239
Ronald Cronfbae94a2023-12-05 18:15:14 +0100240 if (ret != SSL_TLS1_3_PSK_IDENTITY_MATCH) {
241 goto exit;
Jerry Yuce3b95e2023-10-31 16:02:04 +0800242 }
Jerry Yuce3b95e2023-10-31 16:02:04 +0800243
Ronald Cron38117652024-03-05 09:05:46 +0100244 /*
245 * The identity matches that of a ticket. Now check that it has suitable
246 * attributes and bet it will not be the case.
247 */
Ronald Cronfbae94a2023-12-05 18:15:14 +0100248 ret = SSL_TLS1_3_PSK_IDENTITY_MATCH_BUT_PSK_NOT_USABLE;
249
250 if (session->tls_version != MBEDTLS_SSL_VERSION_TLS1_3) {
251 MBEDTLS_SSL_DEBUG_MSG(3, ("Ticket TLS version is not 1.3."));
Jerry Yu8d4bbba2022-09-13 14:15:48 +0800252 goto exit;
253 }
Jerry Yu95699e72022-08-21 19:22:23 +0800254
Gilles Peskine449bd832023-01-11 14:50:10 +0100255#if defined(MBEDTLS_HAVE_TIME)
Jerry Yucebffc32022-12-15 18:00:05 +0800256 now = mbedtls_ms_time();
Gilles Peskine449bd832023-01-11 14:50:10 +0100257
Jerry Yu25ba4d42023-11-10 14:12:20 +0800258 if (now < session->ticket_creation_time) {
Gilles Peskine449bd832023-01-11 14:50:10 +0100259 MBEDTLS_SSL_DEBUG_MSG(
Jerry Yu713ce1f2023-11-17 15:32:12 +0800260 3, ("Invalid ticket creation time ( now = %" MBEDTLS_PRINTF_MS_TIME
261 ", creation_time = %" MBEDTLS_PRINTF_MS_TIME " )",
Jerry Yu25ba4d42023-11-10 14:12:20 +0800262 now, session->ticket_creation_time));
Gilles Peskine449bd832023-01-11 14:50:10 +0100263 goto exit;
264 }
265
Jerry Yu25ba4d42023-11-10 14:12:20 +0800266 server_age = now - session->ticket_creation_time;
Jerry Yu95699e72022-08-21 19:22:23 +0800267
Jerry Yu8d4bbba2022-09-13 14:15:48 +0800268 /* RFC 8446 section 4.6.1
269 *
270 * Servers MUST NOT use any value greater than 604800 seconds (7 days).
271 *
272 * RFC 8446 section 4.2.11.1
273 *
274 * Clients MUST NOT attempt to use tickets which have ages greater than
275 * the "ticket_lifetime" value which was provided with the ticket.
276 *
Jerry Yu8d4bbba2022-09-13 14:15:48 +0800277 */
Jerry Yu8e0174a2023-11-10 13:58:16 +0800278 if (server_age > MBEDTLS_SSL_TLS1_3_MAX_ALLOWED_TICKET_LIFETIME * 1000) {
Jerry Yu8d4bbba2022-09-13 14:15:48 +0800279 MBEDTLS_SSL_DEBUG_MSG(
Jerry Yud84c14f2023-11-16 13:33:57 +0800280 3, ("Ticket age exceeds limitation ticket_age = %" MBEDTLS_PRINTF_MS_TIME,
Jerry Yucebffc32022-12-15 18:00:05 +0800281 server_age));
Jerry Yu8d4bbba2022-09-13 14:15:48 +0800282 goto exit;
283 }
Jerry Yu95699e72022-08-21 19:22:23 +0800284
Jerry Yu8d4bbba2022-09-13 14:15:48 +0800285 /* RFC 8446 section 4.2.10
286 *
287 * For PSKs provisioned via NewSessionTicket, a server MUST validate that
288 * the ticket age for the selected PSK identity (computed by subtracting
289 * ticket_age_add from PskIdentity.obfuscated_ticket_age modulo 2^32) is
290 * within a small tolerance of the time since the ticket was issued.
Jerry Yuf7dad3c2022-09-14 22:31:39 +0800291 *
Jerry Yu31b601a2023-11-10 11:27:21 +0800292 * NOTE: The typical accuracy of an RTC crystal is ±100 to ±20 parts per
293 * million (360 to 72 milliseconds per hour). Default tolerance
Jerry Yu9cb953a2023-11-15 09:54:11 +0800294 * window is 6s, thus in the worst case clients and servers must
Jerry Yu31b601a2023-11-10 11:27:21 +0800295 * sync up their system time every 6000/360/2~=8 hours.
Jerry Yu8d4bbba2022-09-13 14:15:48 +0800296 */
Jerry Yucebffc32022-12-15 18:00:05 +0800297 client_age = obfuscated_ticket_age - session->ticket_age_add;
Jerry Yu60e99722023-11-20 09:55:24 +0800298 age_diff = server_age - (mbedtls_ms_time_t) client_age;
Jerry Yu28e7c552023-11-10 12:05:56 +0800299 if (age_diff < -MBEDTLS_SSL_TLS1_3_TICKET_AGE_TOLERANCE ||
Jerry Yucebffc32022-12-15 18:00:05 +0800300 age_diff > MBEDTLS_SSL_TLS1_3_TICKET_AGE_TOLERANCE) {
Jerry Yu8d4bbba2022-09-13 14:15:48 +0800301 MBEDTLS_SSL_DEBUG_MSG(
Jerry Yuf16efbc2023-10-30 11:06:24 +0800302 3, ("Ticket age outside tolerance window ( diff = %"
303 MBEDTLS_PRINTF_MS_TIME ")",
Jerry Yucebffc32022-12-15 18:00:05 +0800304 age_diff));
Jerry Yu8d4bbba2022-09-13 14:15:48 +0800305 goto exit;
306 }
Jerry Yu95699e72022-08-21 19:22:23 +0800307#endif /* MBEDTLS_HAVE_TIME */
Jerry Yu8d4bbba2022-09-13 14:15:48 +0800308
Ronald Cron38117652024-03-05 09:05:46 +0100309 /*
310 * All good, we have found a suitable ticket.
311 */
Ronald Cronfbae94a2023-12-05 18:15:14 +0100312 ret = SSL_TLS1_3_PSK_IDENTITY_MATCH;
313
Jerry Yu8d4bbba2022-09-13 14:15:48 +0800314exit:
Ronald Cronfbae94a2023-12-05 18:15:14 +0100315 if (ret != SSL_TLS1_3_PSK_IDENTITY_MATCH) {
Gilles Peskine449bd832023-01-11 14:50:10 +0100316 mbedtls_ssl_session_free(session);
317 }
Jerry Yu95699e72022-08-21 19:22:23 +0800318
Gilles Peskine449bd832023-01-11 14:50:10 +0100319 MBEDTLS_SSL_DEBUG_MSG(2, ("<= check_identity_match_ticket"));
320 return ret;
Jerry Yu95699e72022-08-21 19:22:23 +0800321}
322#endif /* MBEDTLS_SSL_SESSION_TICKETS */
323
Jerry Yu6e74a7e2022-07-20 20:49:32 +0800324MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yu1c105562022-07-10 06:32:38 +0000325static int ssl_tls13_offered_psks_check_identity_match(
Gilles Peskine449bd832023-01-11 14:50:10 +0100326 mbedtls_ssl_context *ssl,
327 const unsigned char *identity,
328 size_t identity_len,
329 uint32_t obfuscated_ticket_age,
330 int *psk_type,
331 mbedtls_ssl_session *session)
Jerry Yu1c105562022-07-10 06:32:38 +0000332{
Ronald Cron606671e2023-02-17 11:36:33 +0100333 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
334
Jerry Yu95699e72022-08-21 19:22:23 +0800335 ((void) session);
336 ((void) obfuscated_ticket_age);
Jerry Yu5725f1c2022-08-21 17:27:16 +0800337 *psk_type = MBEDTLS_SSL_TLS1_3_PSK_EXTERNAL;
Jerry Yu95699e72022-08-21 19:22:23 +0800338
Gilles Peskine449bd832023-01-11 14:50:10 +0100339 MBEDTLS_SSL_DEBUG_BUF(4, "identity", identity, identity_len);
Jerry Yu95699e72022-08-21 19:22:23 +0800340
Jerry Yu95699e72022-08-21 19:22:23 +0800341#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Ronald Cron7a30cf52024-02-23 14:38:59 +0100342 ret = ssl_tls13_offered_psks_check_identity_match_ticket(
343 ssl, identity, identity_len, obfuscated_ticket_age, session);
344 if (ret == SSL_TLS1_3_PSK_IDENTITY_MATCH) {
Jerry Yu95699e72022-08-21 19:22:23 +0800345 *psk_type = MBEDTLS_SSL_TLS1_3_PSK_RESUMPTION;
Ronald Cron606671e2023-02-17 11:36:33 +0100346 ret = mbedtls_ssl_set_hs_psk(ssl,
347 session->resumption_key,
348 session->resumption_key_len);
349 if (ret != 0) {
350 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_set_hs_psk", ret);
351 return ret;
352 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100353
354 MBEDTLS_SSL_DEBUG_BUF(4, "Ticket-resumed PSK:",
355 session->resumption_key,
356 session->resumption_key_len);
357 MBEDTLS_SSL_DEBUG_MSG(4, ("ticket: obfuscated_ticket_age: %u",
358 (unsigned) obfuscated_ticket_age));
Ronald Cronfbae94a2023-12-05 18:15:14 +0100359 return SSL_TLS1_3_PSK_IDENTITY_MATCH;
Ronald Cron7a30cf52024-02-23 14:38:59 +0100360 } else if (ret == SSL_TLS1_3_PSK_IDENTITY_MATCH_BUT_PSK_NOT_USABLE) {
361 return SSL_TLS1_3_PSK_IDENTITY_MATCH_BUT_PSK_NOT_USABLE;
Jerry Yu95699e72022-08-21 19:22:23 +0800362 }
363#endif /* MBEDTLS_SSL_SESSION_TICKETS */
364
Jerry Yu1c105562022-07-10 06:32:38 +0000365 /* Check identity with external configured function */
Gilles Peskine449bd832023-01-11 14:50:10 +0100366 if (ssl->conf->f_psk != NULL) {
367 if (ssl->conf->f_psk(
368 ssl->conf->p_psk, ssl, identity, identity_len) == 0) {
Ronald Cronfbae94a2023-12-05 18:15:14 +0100369 return SSL_TLS1_3_PSK_IDENTITY_MATCH;
Jerry Yu1c105562022-07-10 06:32:38 +0000370 }
Ronald Cronfbae94a2023-12-05 18:15:14 +0100371 return SSL_TLS1_3_PSK_IDENTITY_DOES_NOT_MATCH;
Jerry Yu1c105562022-07-10 06:32:38 +0000372 }
373
Gilles Peskine449bd832023-01-11 14:50:10 +0100374 MBEDTLS_SSL_DEBUG_BUF(5, "identity", identity, identity_len);
Jerry Yu1c105562022-07-10 06:32:38 +0000375 /* Check identity with pre-configured psk */
Gilles Peskine449bd832023-01-11 14:50:10 +0100376 if (ssl->conf->psk_identity != NULL &&
Jerry Yu2f0abc92022-07-22 19:34:48 +0800377 identity_len == ssl->conf->psk_identity_len &&
Gilles Peskine449bd832023-01-11 14:50:10 +0100378 mbedtls_ct_memcmp(ssl->conf->psk_identity,
379 identity, identity_len) == 0) {
Ronald Cron606671e2023-02-17 11:36:33 +0100380 ret = mbedtls_ssl_set_hs_psk(ssl, ssl->conf->psk, ssl->conf->psk_len);
381 if (ret != 0) {
382 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_set_hs_psk", ret);
383 return ret;
384 }
Ronald Cronfbae94a2023-12-05 18:15:14 +0100385 return SSL_TLS1_3_PSK_IDENTITY_MATCH;
Jerry Yu1c105562022-07-10 06:32:38 +0000386 }
387
Ronald Cronfbae94a2023-12-05 18:15:14 +0100388 return SSL_TLS1_3_PSK_IDENTITY_DOES_NOT_MATCH;
Jerry Yu1c105562022-07-10 06:32:38 +0000389}
390
Ronald Cron38117652024-03-05 09:05:46 +0100391/*
392 * Non-error return values of ssl_tls13_offered_psks_check_binder_match().
393 * They are positive to not collide with error codes that are negative. Zero
394 * (SSL_TLS1_3_BINDER_MATCH) in case of success as it may be propagated up
395 * by the callers of this function as a generic success condition.
396 */
Ronald Cron6e311272023-12-05 17:57:01 +0100397#define SSL_TLS1_3_BINDER_DOES_NOT_MATCH 1
398#define SSL_TLS1_3_BINDER_MATCH 0
Jerry Yu6e74a7e2022-07-20 20:49:32 +0800399MBEDTLS_CHECK_RETURN_CRITICAL
Xiaokang Qian9f1747b2023-03-30 02:50:04 +0000400static int ssl_tls13_offered_psks_check_binder_match(
401 mbedtls_ssl_context *ssl,
402 const unsigned char *binder, size_t binder_len,
403 int psk_type, psa_algorithm_t psk_hash_alg)
Jerry Yu1c105562022-07-10 06:32:38 +0000404{
405 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yu96a2e362022-07-21 15:11:34 +0800406
Jerry Yudaf375a2022-07-20 21:31:43 +0800407 unsigned char transcript[PSA_HASH_MAX_SIZE];
Jerry Yu1c105562022-07-10 06:32:38 +0000408 size_t transcript_len;
Jerry Yu2f0abc92022-07-22 19:34:48 +0800409 unsigned char *psk;
Jerry Yu96a2e362022-07-21 15:11:34 +0800410 size_t psk_len;
Jerry Yudaf375a2022-07-20 21:31:43 +0800411 unsigned char server_computed_binder[PSA_HASH_MAX_SIZE];
Jerry Yu1c105562022-07-10 06:32:38 +0000412
Jerry Yu1c105562022-07-10 06:32:38 +0000413 /* Get current state of handshake transcript. */
Jerry Yu29d9faa2022-08-23 17:52:45 +0800414 ret = mbedtls_ssl_get_handshake_transcript(
Manuel Pégourié-Gonnard2d6d9932023-03-28 11:38:08 +0200415 ssl, mbedtls_md_type_from_psa_alg(psk_hash_alg),
Gilles Peskine449bd832023-01-11 14:50:10 +0100416 transcript, sizeof(transcript), &transcript_len);
417 if (ret != 0) {
418 return ret;
419 }
Jerry Yu1c105562022-07-10 06:32:38 +0000420
Gilles Peskine449bd832023-01-11 14:50:10 +0100421 ret = mbedtls_ssl_tls13_export_handshake_psk(ssl, &psk, &psk_len);
422 if (ret != 0) {
423 return ret;
424 }
Jerry Yu96a2e362022-07-21 15:11:34 +0800425
Gilles Peskine449bd832023-01-11 14:50:10 +0100426 ret = mbedtls_ssl_tls13_create_psk_binder(ssl, psk_hash_alg,
427 psk, psk_len, psk_type,
428 transcript,
429 server_computed_binder);
Jerry Yu96a2e362022-07-21 15:11:34 +0800430#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +0100431 mbedtls_free((void *) psk);
Jerry Yu96a2e362022-07-21 15:11:34 +0800432#endif
Gilles Peskine449bd832023-01-11 14:50:10 +0100433 if (ret != 0) {
434 MBEDTLS_SSL_DEBUG_MSG(1, ("PSK binder calculation failed."));
435 return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
Jerry Yu1c105562022-07-10 06:32:38 +0000436 }
437
Gilles Peskine449bd832023-01-11 14:50:10 +0100438 MBEDTLS_SSL_DEBUG_BUF(3, "psk binder ( computed ): ",
439 server_computed_binder, transcript_len);
440 MBEDTLS_SSL_DEBUG_BUF(3, "psk binder ( received ): ", binder, binder_len);
Jerry Yu1c105562022-07-10 06:32:38 +0000441
Gilles Peskine449bd832023-01-11 14:50:10 +0100442 if (mbedtls_ct_memcmp(server_computed_binder, binder, binder_len) == 0) {
Ronald Cron6e311272023-12-05 17:57:01 +0100443 return SSL_TLS1_3_BINDER_MATCH;
Jerry Yu1c105562022-07-10 06:32:38 +0000444 }
445
Gilles Peskine449bd832023-01-11 14:50:10 +0100446 mbedtls_platform_zeroize(server_computed_binder,
447 sizeof(server_computed_binder));
Ronald Cron6e311272023-12-05 17:57:01 +0100448 return SSL_TLS1_3_BINDER_DOES_NOT_MATCH;
Jerry Yu1c105562022-07-10 06:32:38 +0000449}
Jerry Yu96a2e362022-07-21 15:11:34 +0800450
Jerry Yu82534862022-08-30 10:42:33 +0800451#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Jerry Yuf35ba382022-08-23 17:58:26 +0800452MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100453static int ssl_tls13_session_copy_ticket(mbedtls_ssl_session *dst,
454 const mbedtls_ssl_session *src)
Jerry Yu82534862022-08-30 10:42:33 +0800455{
Jerry Yu82534862022-08-30 10:42:33 +0800456 dst->ticket_age_add = src->ticket_age_add;
457 dst->ticket_flags = src->ticket_flags;
458 dst->resumption_key_len = src->resumption_key_len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100459 if (src->resumption_key_len == 0) {
460 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
461 }
462 memcpy(dst->resumption_key, src->resumption_key, src->resumption_key_len);
Jerry Yu4746b102022-09-13 11:11:48 +0800463
Jerry Yu33bf2402022-12-12 16:01:43 +0800464#if defined(MBEDTLS_SSL_EARLY_DATA)
465 dst->max_early_data_size = src->max_early_data_size;
466#endif
467
Gilles Peskine449bd832023-01-11 14:50:10 +0100468 return 0;
Jerry Yu82534862022-08-30 10:42:33 +0800469}
470#endif /* MBEDTLS_SSL_SESSION_TICKETS */
471
Ronald Cron79cdd412024-02-20 17:19:28 +0100472struct psk_attributes {
473 int type;
474 int key_exchange_mode;
Ronald Cron74a16292024-02-23 17:07:41 +0100475 const mbedtls_ssl_ciphersuite_t *ciphersuite_info;
Ronald Cron79cdd412024-02-20 17:19:28 +0100476};
Ronald Cron74a16292024-02-23 17:07:41 +0100477#define PSK_ATTRIBUTES_INIT { 0, 0, 0 }
Ronald Cron79cdd412024-02-20 17:19:28 +0100478
Jerry Yu1c105562022-07-10 06:32:38 +0000479/* Parser for pre_shared_key extension in client hello
480 * struct {
481 * opaque identity<1..2^16-1>;
482 * uint32 obfuscated_ticket_age;
483 * } PskIdentity;
484 *
485 * opaque PskBinderEntry<32..255>;
486 *
487 * struct {
488 * PskIdentity identities<7..2^16-1>;
489 * PskBinderEntry binders<33..2^16-1>;
490 * } OfferedPsks;
491 *
492 * struct {
493 * select (Handshake.msg_type) {
494 * case client_hello: OfferedPsks;
495 * ....
496 * };
497 * } PreSharedKeyExtension;
498 */
Jerry Yu6e74a7e2022-07-20 20:49:32 +0800499MBEDTLS_CHECK_RETURN_CRITICAL
Xiaokang Qian9f1747b2023-03-30 02:50:04 +0000500static int ssl_tls13_parse_pre_shared_key_ext(
501 mbedtls_ssl_context *ssl,
502 const unsigned char *pre_shared_key_ext,
503 const unsigned char *pre_shared_key_ext_end,
504 const unsigned char *ciphersuites,
Ronald Cron79cdd412024-02-20 17:19:28 +0100505 const unsigned char *ciphersuites_end,
506 struct psk_attributes *psk)
Jerry Yu1c105562022-07-10 06:32:38 +0000507{
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +0100508 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yu29d9faa2022-08-23 17:52:45 +0800509 const unsigned char *identities = pre_shared_key_ext;
Jerry Yu568ec252022-07-22 21:27:34 +0800510 const unsigned char *p_identity_len;
511 size_t identities_len;
Jerry Yu1c105562022-07-10 06:32:38 +0000512 const unsigned char *identities_end;
Jerry Yu568ec252022-07-22 21:27:34 +0800513 const unsigned char *binders;
514 const unsigned char *p_binder_len;
515 size_t binders_len;
Jerry Yu1c105562022-07-10 06:32:38 +0000516 const unsigned char *binders_end;
Jerry Yu96a2e362022-07-21 15:11:34 +0800517 int matched_identity = -1;
518 int identity_id = -1;
Jerry Yu1c105562022-07-10 06:32:38 +0000519
Gilles Peskine449bd832023-01-11 14:50:10 +0100520 MBEDTLS_SSL_DEBUG_BUF(3, "pre_shared_key extension",
521 pre_shared_key_ext,
522 pre_shared_key_ext_end - pre_shared_key_ext);
Jerry Yu1c105562022-07-10 06:32:38 +0000523
Jerry Yu96a2e362022-07-21 15:11:34 +0800524 /* identities_len 2 bytes
525 * identities_data >= 7 bytes
526 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100527 MBEDTLS_SSL_CHK_BUF_READ_PTR(identities, pre_shared_key_ext_end, 7 + 2);
528 identities_len = MBEDTLS_GET_UINT16_BE(identities, 0);
Jerry Yu568ec252022-07-22 21:27:34 +0800529 p_identity_len = identities + 2;
Gilles Peskine449bd832023-01-11 14:50:10 +0100530 MBEDTLS_SSL_CHK_BUF_READ_PTR(p_identity_len, pre_shared_key_ext_end,
531 identities_len);
Jerry Yu568ec252022-07-22 21:27:34 +0800532 identities_end = p_identity_len + identities_len;
Jerry Yu1c105562022-07-10 06:32:38 +0000533
Jerry Yu96a2e362022-07-21 15:11:34 +0800534 /* binders_len 2 bytes
535 * binders >= 33 bytes
536 */
Jerry Yu568ec252022-07-22 21:27:34 +0800537 binders = identities_end;
Gilles Peskine449bd832023-01-11 14:50:10 +0100538 MBEDTLS_SSL_CHK_BUF_READ_PTR(binders, pre_shared_key_ext_end, 33 + 2);
539 binders_len = MBEDTLS_GET_UINT16_BE(binders, 0);
Jerry Yu568ec252022-07-22 21:27:34 +0800540 p_binder_len = binders + 2;
Gilles Peskine449bd832023-01-11 14:50:10 +0100541 MBEDTLS_SSL_CHK_BUF_READ_PTR(p_binder_len, pre_shared_key_ext_end, binders_len);
Jerry Yu568ec252022-07-22 21:27:34 +0800542 binders_end = p_binder_len + binders_len;
Jerry Yu1c105562022-07-10 06:32:38 +0000543
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +0100544 ret = ssl->handshake->update_checksum(ssl, pre_shared_key_ext,
545 identities_end - pre_shared_key_ext);
546 if (0 != ret) {
547 MBEDTLS_SSL_DEBUG_RET(1, ("update_checksum"), ret);
548 return ret;
549 }
Jerry Yu96a2e362022-07-21 15:11:34 +0800550
Gilles Peskine449bd832023-01-11 14:50:10 +0100551 while (p_identity_len < identities_end && p_binder_len < binders_end) {
Jerry Yu1c105562022-07-10 06:32:38 +0000552 const unsigned char *identity;
Jerry Yu568ec252022-07-22 21:27:34 +0800553 size_t identity_len;
Jerry Yu82534862022-08-30 10:42:33 +0800554 uint32_t obfuscated_ticket_age;
Jerry Yu96a2e362022-07-21 15:11:34 +0800555 const unsigned char *binder;
Jerry Yu568ec252022-07-22 21:27:34 +0800556 size_t binder_len;
Ronald Cron89089cc2024-02-14 18:18:08 +0100557 int psk_ciphersuite_id;
558 psa_algorithm_t psk_hash_alg;
Ronald Croncf284562024-02-16 18:54:10 +0100559 int allowed_key_exchange_modes;
Ronald Cron74a16292024-02-23 17:07:41 +0100560
Jerry Yu82534862022-08-30 10:42:33 +0800561#if defined(MBEDTLS_SSL_SESSION_TICKETS)
562 mbedtls_ssl_session session;
Gilles Peskine449bd832023-01-11 14:50:10 +0100563 mbedtls_ssl_session_init(&session);
Jerry Yu82534862022-08-30 10:42:33 +0800564#endif
Jerry Yubb852022022-07-20 21:10:44 +0800565
Gilles Peskine449bd832023-01-11 14:50:10 +0100566 MBEDTLS_SSL_CHK_BUF_READ_PTR(p_identity_len, identities_end, 2 + 1 + 4);
567 identity_len = MBEDTLS_GET_UINT16_BE(p_identity_len, 0);
Jerry Yu568ec252022-07-22 21:27:34 +0800568 identity = p_identity_len + 2;
Gilles Peskine449bd832023-01-11 14:50:10 +0100569 MBEDTLS_SSL_CHK_BUF_READ_PTR(identity, identities_end, identity_len + 4);
570 obfuscated_ticket_age = MBEDTLS_GET_UINT32_BE(identity, identity_len);
Jerry Yu568ec252022-07-22 21:27:34 +0800571 p_identity_len += identity_len + 6;
Jerry Yu1c105562022-07-10 06:32:38 +0000572
Gilles Peskine449bd832023-01-11 14:50:10 +0100573 MBEDTLS_SSL_CHK_BUF_READ_PTR(p_binder_len, binders_end, 1 + 32);
Jerry Yu568ec252022-07-22 21:27:34 +0800574 binder_len = *p_binder_len;
575 binder = p_binder_len + 1;
Gilles Peskine449bd832023-01-11 14:50:10 +0100576 MBEDTLS_SSL_CHK_BUF_READ_PTR(binder, binders_end, binder_len);
Jerry Yu568ec252022-07-22 21:27:34 +0800577 p_binder_len += binder_len + 1;
Jerry Yu96a2e362022-07-21 15:11:34 +0800578
Jerry Yu96a2e362022-07-21 15:11:34 +0800579 identity_id++;
Gilles Peskine449bd832023-01-11 14:50:10 +0100580 if (matched_identity != -1) {
Jerry Yu1c105562022-07-10 06:32:38 +0000581 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +0100582 }
Jerry Yu1c105562022-07-10 06:32:38 +0000583
Jerry Yu96a2e362022-07-21 15:11:34 +0800584 ret = ssl_tls13_offered_psks_check_identity_match(
Gilles Peskine449bd832023-01-11 14:50:10 +0100585 ssl, identity, identity_len, obfuscated_ticket_age,
Ronald Cron79cdd412024-02-20 17:19:28 +0100586 &psk->type, &session);
Ronald Cronfbae94a2023-12-05 18:15:14 +0100587 if (ret != SSL_TLS1_3_PSK_IDENTITY_MATCH) {
Jerry Yu96a2e362022-07-21 15:11:34 +0800588 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +0100589 }
Jerry Yu96a2e362022-07-21 15:11:34 +0800590
Gilles Peskine449bd832023-01-11 14:50:10 +0100591 MBEDTLS_SSL_DEBUG_MSG(4, ("found matched identity"));
Ronald Cron89089cc2024-02-14 18:18:08 +0100592
Ronald Cron79cdd412024-02-20 17:19:28 +0100593 switch (psk->type) {
Jerry Yu0baf9072022-08-25 11:21:04 +0800594 case MBEDTLS_SSL_TLS1_3_PSK_EXTERNAL:
Ronald Cron89089cc2024-02-14 18:18:08 +0100595 psk_ciphersuite_id = 0;
596 psk_hash_alg = PSA_ALG_SHA_256;
Ronald Croncf284562024-02-16 18:54:10 +0100597 allowed_key_exchange_modes =
598 MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK_ALL;
Jerry Yu0baf9072022-08-25 11:21:04 +0800599 break;
Jerry Yu82534862022-08-30 10:42:33 +0800600#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Ronald Cronf7e99162024-02-14 16:04:02 +0100601 case MBEDTLS_SSL_TLS1_3_PSK_RESUMPTION:
Ronald Cron89089cc2024-02-14 18:18:08 +0100602 psk_ciphersuite_id = session.ciphersuite;
603 psk_hash_alg = PSA_ALG_NONE;
Ronald Croncf284562024-02-16 18:54:10 +0100604 ssl->session_negotiate->ticket_flags = session.ticket_flags;
605 allowed_key_exchange_modes =
606 session.ticket_flags &
607 MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK_ALL;
Jerry Yu0baf9072022-08-25 11:21:04 +0800608 break;
Ronald Cronf7e99162024-02-14 16:04:02 +0100609#endif
Jerry Yu0baf9072022-08-25 11:21:04 +0800610 default:
Gilles Peskine449bd832023-01-11 14:50:10 +0100611 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Jerry Yu0baf9072022-08-25 11:21:04 +0800612 }
Ronald Cron89089cc2024-02-14 18:18:08 +0100613
Ronald Cron79cdd412024-02-20 17:19:28 +0100614 psk->key_exchange_mode = MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_NONE;
615
Ronald Croncf284562024-02-16 18:54:10 +0100616 if ((allowed_key_exchange_modes &
617 MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK_EPHEMERAL) &&
618 ssl_tls13_key_exchange_is_psk_ephemeral_available(ssl)) {
Ronald Cron79cdd412024-02-20 17:19:28 +0100619 psk->key_exchange_mode = MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK_EPHEMERAL;
Ronald Croncf284562024-02-16 18:54:10 +0100620 } else if ((allowed_key_exchange_modes &
621 MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK) &&
622 ssl_tls13_key_exchange_is_psk_available(ssl)) {
Ronald Cron79cdd412024-02-20 17:19:28 +0100623 psk->key_exchange_mode = MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK;
Ronald Croncf284562024-02-16 18:54:10 +0100624 }
625
Ronald Cron79cdd412024-02-20 17:19:28 +0100626 if (psk->key_exchange_mode == MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_NONE) {
Ronald Croncf284562024-02-16 18:54:10 +0100627 MBEDTLS_SSL_DEBUG_MSG(3, ("No suitable PSK key exchange mode"));
628 continue;
629 }
630
Ronald Cron89089cc2024-02-14 18:18:08 +0100631 ssl_tls13_select_ciphersuite(ssl, ciphersuites, ciphersuites_end,
632 psk_ciphersuite_id, psk_hash_alg,
Ronald Cron74a16292024-02-23 17:07:41 +0100633 &psk->ciphersuite_info);
Ronald Cron89089cc2024-02-14 18:18:08 +0100634
Ronald Cron74a16292024-02-23 17:07:41 +0100635 if (psk->ciphersuite_info == NULL) {
Ronald Cron89089cc2024-02-14 18:18:08 +0100636#if defined(MBEDTLS_SSL_SESSION_TICKETS)
637 mbedtls_ssl_session_free(&session);
638#endif
639 /*
640 * We consider finding a ciphersuite suitable for the PSK as part
641 * of the validation of its binder. Thus if we do not find one, we
642 * abort the handshake with a decrypt_error alert.
643 */
Jerry Yuf35ba382022-08-23 17:58:26 +0800644 MBEDTLS_SSL_PEND_FATAL_ALERT(
645 MBEDTLS_SSL_ALERT_MSG_DECRYPT_ERROR,
Gilles Peskine449bd832023-01-11 14:50:10 +0100646 MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE);
Ronald Cron89089cc2024-02-14 18:18:08 +0100647 return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
Jerry Yu5725f1c2022-08-21 17:27:16 +0800648 }
649
Jerry Yu96a2e362022-07-21 15:11:34 +0800650 ret = ssl_tls13_offered_psks_check_binder_match(
Ronald Cron79cdd412024-02-20 17:19:28 +0100651 ssl, binder, binder_len, psk->type,
Ronald Cron74a16292024-02-23 17:07:41 +0100652 mbedtls_md_psa_alg_from_type((mbedtls_md_type_t) psk->ciphersuite_info->mac));
Ronald Cron6e311272023-12-05 17:57:01 +0100653 if (ret != SSL_TLS1_3_BINDER_MATCH) {
Jerry Yuc5a23a02022-08-25 10:51:44 +0800654 /* For security reasons, the handshake should be aborted when we
655 * fail to validate a binder value. See RFC 8446 section 4.2.11.2
656 * and appendix E.6. */
Jerry Yu82534862022-08-30 10:42:33 +0800657#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Gilles Peskine449bd832023-01-11 14:50:10 +0100658 mbedtls_ssl_session_free(&session);
Jerry Yu82534862022-08-30 10:42:33 +0800659#endif
Gilles Peskine449bd832023-01-11 14:50:10 +0100660 MBEDTLS_SSL_DEBUG_MSG(3, ("Invalid binder."));
Xiaokang Qian9f1747b2023-03-30 02:50:04 +0000661 MBEDTLS_SSL_DEBUG_RET(
662 1, "ssl_tls13_offered_psks_check_binder_match", ret);
Jerry Yu96a2e362022-07-21 15:11:34 +0800663 MBEDTLS_SSL_PEND_FATAL_ALERT(
664 MBEDTLS_SSL_ALERT_MSG_DECRYPT_ERROR,
Gilles Peskine449bd832023-01-11 14:50:10 +0100665 MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE);
666 return ret;
Jerry Yu96a2e362022-07-21 15:11:34 +0800667 }
Jerry Yu96a2e362022-07-21 15:11:34 +0800668
669 matched_identity = identity_id;
Jerry Yu5725f1c2022-08-21 17:27:16 +0800670
Jerry Yu82534862022-08-30 10:42:33 +0800671#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Ronald Cron79cdd412024-02-20 17:19:28 +0100672 if (psk->type == MBEDTLS_SSL_TLS1_3_PSK_RESUMPTION) {
Gilles Peskine449bd832023-01-11 14:50:10 +0100673 ret = ssl_tls13_session_copy_ticket(ssl->session_negotiate,
674 &session);
675 mbedtls_ssl_session_free(&session);
676 if (ret != 0) {
677 return ret;
678 }
Jerry Yu82534862022-08-30 10:42:33 +0800679 }
680#endif /* MBEDTLS_SSL_SESSION_TICKETS */
Jerry Yu1c105562022-07-10 06:32:38 +0000681 }
682
Gilles Peskine449bd832023-01-11 14:50:10 +0100683 if (p_identity_len != identities_end || p_binder_len != binders_end) {
684 MBEDTLS_SSL_DEBUG_MSG(3, ("pre_shared_key extension decode error"));
685 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR,
686 MBEDTLS_ERR_SSL_DECODE_ERROR);
687 return MBEDTLS_ERR_SSL_DECODE_ERROR;
Jerry Yu1c105562022-07-10 06:32:38 +0000688 }
689
Jerry Yu6f1db3f2022-07-22 23:05:59 +0800690 /* Update the handshake transcript with the binder list. */
Xiaokang Qian9f1747b2023-03-30 02:50:04 +0000691 ret = ssl->handshake->update_checksum(
692 ssl, identities_end, (size_t) (binders_end - identities_end));
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +0100693 if (0 != ret) {
694 MBEDTLS_SSL_DEBUG_RET(1, ("update_checksum"), ret);
695 return ret;
696 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100697 if (matched_identity == -1) {
Ronald Croncf284562024-02-16 18:54:10 +0100698 MBEDTLS_SSL_DEBUG_MSG(3, ("No usable PSK or ticket."));
Gilles Peskine449bd832023-01-11 14:50:10 +0100699 return MBEDTLS_ERR_SSL_UNKNOWN_IDENTITY;
Jerry Yu1c105562022-07-10 06:32:38 +0000700 }
701
Gilles Peskine449bd832023-01-11 14:50:10 +0100702 ssl->handshake->selected_identity = (uint16_t) matched_identity;
703 MBEDTLS_SSL_DEBUG_MSG(3, ("Pre shared key found"));
Jerry Yu1c105562022-07-10 06:32:38 +0000704
Gilles Peskine449bd832023-01-11 14:50:10 +0100705 return 0;
Jerry Yu1c105562022-07-10 06:32:38 +0000706}
Jerry Yu032b15ce2022-07-11 06:10:03 +0000707
708/*
709 * struct {
710 * select ( Handshake.msg_type ) {
711 * ....
712 * case server_hello:
713 * uint16 selected_identity;
714 * }
715 * } PreSharedKeyExtension;
716 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100717static int ssl_tls13_write_server_pre_shared_key_ext(mbedtls_ssl_context *ssl,
718 unsigned char *buf,
719 unsigned char *end,
720 size_t *olen)
Jerry Yu032b15ce2022-07-11 06:10:03 +0000721{
Gilles Peskine449bd832023-01-11 14:50:10 +0100722 unsigned char *p = (unsigned char *) buf;
Jerry Yu032b15ce2022-07-11 06:10:03 +0000723
724 *olen = 0;
725
David Horstmann21b89762022-10-06 18:34:28 +0100726 int not_using_psk = 0;
Jerry Yu032b15ce2022-07-11 06:10:03 +0000727#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +0100728 not_using_psk = (mbedtls_svc_key_id_is_null(ssl->handshake->psk_opaque));
Jerry Yu032b15ce2022-07-11 06:10:03 +0000729#else
Gilles Peskine449bd832023-01-11 14:50:10 +0100730 not_using_psk = (ssl->handshake->psk == NULL);
Jerry Yu032b15ce2022-07-11 06:10:03 +0000731#endif
Gilles Peskine449bd832023-01-11 14:50:10 +0100732 if (not_using_psk) {
Jerry Yu032b15ce2022-07-11 06:10:03 +0000733 /* We shouldn't have called this extension writer unless we've
734 * chosen to use a PSK. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100735 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Jerry Yu032b15ce2022-07-11 06:10:03 +0000736 }
737
Gilles Peskine449bd832023-01-11 14:50:10 +0100738 MBEDTLS_SSL_DEBUG_MSG(3, ("server hello, adding pre_shared_key extension"));
739 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 6);
Jerry Yu032b15ce2022-07-11 06:10:03 +0000740
Gilles Peskine449bd832023-01-11 14:50:10 +0100741 MBEDTLS_PUT_UINT16_BE(MBEDTLS_TLS_EXT_PRE_SHARED_KEY, p, 0);
742 MBEDTLS_PUT_UINT16_BE(2, p, 2);
Jerry Yu032b15ce2022-07-11 06:10:03 +0000743
Gilles Peskine449bd832023-01-11 14:50:10 +0100744 MBEDTLS_PUT_UINT16_BE(ssl->handshake->selected_identity, p, 4);
Jerry Yu032b15ce2022-07-11 06:10:03 +0000745
746 *olen = 6;
747
Gilles Peskine449bd832023-01-11 14:50:10 +0100748 MBEDTLS_SSL_DEBUG_MSG(4, ("sent selected_identity: %u",
749 ssl->handshake->selected_identity));
Jerry Yu032b15ce2022-07-11 06:10:03 +0000750
Gilles Peskine449bd832023-01-11 14:50:10 +0100751 mbedtls_ssl_tls13_set_hs_sent_ext_mask(ssl, MBEDTLS_TLS_EXT_PRE_SHARED_KEY);
Jerry Yub95dd362022-11-08 21:19:34 +0800752
Gilles Peskine449bd832023-01-11 14:50:10 +0100753 return 0;
Jerry Yu032b15ce2022-07-11 06:10:03 +0000754}
755
Ronald Cron41a443a2022-10-04 16:38:25 +0200756#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED */
Jerry Yue19e3b92022-07-08 12:04:51 +0000757
XiaokangQian7807f9f2022-02-15 10:04:37 +0000758/* From RFC 8446:
759 * struct {
XiaokangQiancfd925f2022-04-14 07:10:37 +0000760 * ProtocolVersion versions<2..254>;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000761 * } SupportedVersions;
762 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200763MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100764static int ssl_tls13_parse_supported_versions_ext(mbedtls_ssl_context *ssl,
765 const unsigned char *buf,
766 const unsigned char *end)
XiaokangQian7807f9f2022-02-15 10:04:37 +0000767{
XiaokangQian7807f9f2022-02-15 10:04:37 +0000768 const unsigned char *p = buf;
XiaokangQian8f9dfe42022-04-15 02:52:39 +0000769 size_t versions_len;
XiaokangQian4080a7f2022-04-11 09:55:18 +0000770 const unsigned char *versions_end;
XiaokangQian0a1b54e2022-04-21 03:01:38 +0000771 uint16_t tls_version;
Ronald Cron5af4c7f2023-03-07 20:46:59 +0100772 int found_supported_version = 0;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000773
Gilles Peskine449bd832023-01-11 14:50:10 +0100774 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, 1);
XiaokangQian4080a7f2022-04-11 09:55:18 +0000775 versions_len = p[0];
XiaokangQian7807f9f2022-02-15 10:04:37 +0000776 p += 1;
777
Gilles Peskine449bd832023-01-11 14:50:10 +0100778 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, versions_len);
XiaokangQian4080a7f2022-04-11 09:55:18 +0000779 versions_end = p + versions_len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100780 while (p < versions_end) {
781 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, versions_end, 2);
782 tls_version = mbedtls_ssl_read_version(p, ssl->conf->transport);
XiaokangQianb67384d2022-04-19 00:02:38 +0000783 p += 2;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000784
Ronald Cron3bd2b022023-04-03 16:45:39 +0200785 if (MBEDTLS_SSL_VERSION_TLS1_3 == tls_version) {
Ronald Cron5af4c7f2023-03-07 20:46:59 +0100786 found_supported_version = 1;
787 break;
788 }
789
Ronald Cron3bd2b022023-04-03 16:45:39 +0200790 if ((MBEDTLS_SSL_VERSION_TLS1_2 == tls_version) &&
791 mbedtls_ssl_conf_is_tls12_enabled(ssl->conf)) {
Ronald Cron5af4c7f2023-03-07 20:46:59 +0100792 found_supported_version = 1;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000793 break;
794 }
XiaokangQian7807f9f2022-02-15 10:04:37 +0000795 }
796
Ronald Cron5af4c7f2023-03-07 20:46:59 +0100797 if (!found_supported_version) {
798 MBEDTLS_SSL_DEBUG_MSG(1, ("No supported version found."));
XiaokangQian7807f9f2022-02-15 10:04:37 +0000799
Gilles Peskine449bd832023-01-11 14:50:10 +0100800 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_PROTOCOL_VERSION,
801 MBEDTLS_ERR_SSL_BAD_PROTOCOL_VERSION);
802 return MBEDTLS_ERR_SSL_BAD_PROTOCOL_VERSION;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000803 }
804
Ronald Cron5af4c7f2023-03-07 20:46:59 +0100805 MBEDTLS_SSL_DEBUG_MSG(1, ("Negotiated version: [%04x]",
Gilles Peskine449bd832023-01-11 14:50:10 +0100806 (unsigned int) tls_version));
XiaokangQian7807f9f2022-02-15 10:04:37 +0000807
Ronald Cron5af4c7f2023-03-07 20:46:59 +0100808 return (int) tls_version;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000809}
810
Przemek Stekiel8c0a9532023-06-15 16:48:19 +0200811#if defined(PSA_WANT_ALG_ECDH) || defined(PSA_WANT_ALG_FFDH)
XiaokangQiane8ff3502022-04-22 02:34:40 +0000812/*
XiaokangQian7807f9f2022-02-15 10:04:37 +0000813 *
814 * From RFC 8446:
815 * enum {
816 * ... (0xFFFF)
817 * } NamedGroup;
818 * struct {
819 * NamedGroup named_group_list<2..2^16-1>;
820 * } NamedGroupList;
821 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200822MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100823static int ssl_tls13_parse_supported_groups_ext(mbedtls_ssl_context *ssl,
824 const unsigned char *buf,
825 const unsigned char *end)
XiaokangQian7807f9f2022-02-15 10:04:37 +0000826{
XiaokangQian7807f9f2022-02-15 10:04:37 +0000827 const unsigned char *p = buf;
XiaokangQian84823772022-04-19 07:57:30 +0000828 size_t named_group_list_len;
XiaokangQian8f9dfe42022-04-15 02:52:39 +0000829 const unsigned char *named_group_list_end;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000830
Gilles Peskine449bd832023-01-11 14:50:10 +0100831 MBEDTLS_SSL_DEBUG_BUF(3, "supported_groups extension", p, end - buf);
832 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, 2);
833 named_group_list_len = MBEDTLS_GET_UINT16_BE(p, 0);
XiaokangQian7807f9f2022-02-15 10:04:37 +0000834 p += 2;
Gilles Peskine449bd832023-01-11 14:50:10 +0100835 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, named_group_list_len);
XiaokangQian8f9dfe42022-04-15 02:52:39 +0000836 named_group_list_end = p + named_group_list_len;
XiaokangQian4e8cd7b2022-04-21 09:48:09 +0000837 ssl->handshake->hrr_selected_group = 0;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000838
Gilles Peskine449bd832023-01-11 14:50:10 +0100839 while (p < named_group_list_end) {
XiaokangQian08037552022-04-20 07:16:41 +0000840 uint16_t named_group;
Gilles Peskine449bd832023-01-11 14:50:10 +0100841 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, named_group_list_end, 2);
842 named_group = MBEDTLS_GET_UINT16_BE(p, 0);
XiaokangQian08037552022-04-20 07:16:41 +0000843 p += 2;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000844
Gilles Peskine449bd832023-01-11 14:50:10 +0100845 MBEDTLS_SSL_DEBUG_MSG(2,
846 ("got named group: %s(%04x)",
847 mbedtls_ssl_named_group_to_str(named_group),
848 named_group));
XiaokangQian08037552022-04-20 07:16:41 +0000849
Gilles Peskine449bd832023-01-11 14:50:10 +0100850 if (!mbedtls_ssl_named_group_is_offered(ssl, named_group) ||
851 !mbedtls_ssl_named_group_is_supported(named_group) ||
852 ssl->handshake->hrr_selected_group != 0) {
XiaokangQian08037552022-04-20 07:16:41 +0000853 continue;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000854 }
855
Gilles Peskine449bd832023-01-11 14:50:10 +0100856 MBEDTLS_SSL_DEBUG_MSG(2,
857 ("add named group %s(%04x) into received list.",
858 mbedtls_ssl_named_group_to_str(named_group),
859 named_group));
Jerry Yuc1be19f2022-04-23 16:11:39 +0800860
XiaokangQian4e8cd7b2022-04-21 09:48:09 +0000861 ssl->handshake->hrr_selected_group = named_group;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000862 }
863
Gilles Peskine449bd832023-01-11 14:50:10 +0100864 return 0;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000865
866}
Przemek Stekiel8c0a9532023-06-15 16:48:19 +0200867#endif /* PSA_WANT_ALG_ECDH || PSA_WANT_ALG_FFDH */
XiaokangQian7807f9f2022-02-15 10:04:37 +0000868
XiaokangQian08037552022-04-20 07:16:41 +0000869#define SSL_TLS1_3_PARSE_KEY_SHARES_EXT_NO_MATCH 1
870
Valerio Settic9ae8622023-07-25 11:23:50 +0200871#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_EPHEMERAL_ENABLED)
XiaokangQian7807f9f2022-02-15 10:04:37 +0000872/*
873 * ssl_tls13_parse_key_shares_ext() verifies whether the information in the
Xiaokang Qian9f1747b2023-03-30 02:50:04 +0000874 * extension is correct and stores the first acceptable key share and its
875 * associated group.
XiaokangQian7807f9f2022-02-15 10:04:37 +0000876 *
877 * Possible return values are:
878 * - 0: Successful processing of the client provided key share extension.
Xiaokang Qian9f1747b2023-03-30 02:50:04 +0000879 * - SSL_TLS1_3_PARSE_KEY_SHARES_EXT_NO_MATCH: The key shares provided by
880 * the client does not match a group supported by the server. A
881 * HelloRetryRequest will be needed.
XiaokangQiane8ff3502022-04-22 02:34:40 +0000882 * - A negative value for fatal errors.
Jerry Yuc1be19f2022-04-23 16:11:39 +0800883 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200884MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100885static int ssl_tls13_parse_key_shares_ext(mbedtls_ssl_context *ssl,
886 const unsigned char *buf,
887 const unsigned char *end)
XiaokangQian7807f9f2022-02-15 10:04:37 +0000888{
XiaokangQianb67384d2022-04-19 00:02:38 +0000889 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000890 unsigned char const *p = buf;
XiaokangQian8f9dfe42022-04-15 02:52:39 +0000891 unsigned char const *client_shares_end;
Jerry Yuc1be19f2022-04-23 16:11:39 +0800892 size_t client_shares_len;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000893
894 /* From RFC 8446:
895 *
896 * struct {
897 * KeyShareEntry client_shares<0..2^16-1>;
898 * } KeyShareClientHello;
899 *
900 */
901
Gilles Peskine449bd832023-01-11 14:50:10 +0100902 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, 2);
903 client_shares_len = MBEDTLS_GET_UINT16_BE(p, 0);
XiaokangQian7807f9f2022-02-15 10:04:37 +0000904 p += 2;
Gilles Peskine449bd832023-01-11 14:50:10 +0100905 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, client_shares_len);
XiaokangQian7807f9f2022-02-15 10:04:37 +0000906
907 ssl->handshake->offered_group_id = 0;
XiaokangQian8f9dfe42022-04-15 02:52:39 +0000908 client_shares_end = p + client_shares_len;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000909
910 /* We try to find a suitable key share entry and copy it to the
911 * handshake context. Later, we have to find out whether we can do
912 * something with the provided key share or whether we have to
XiaokangQianc5763b52022-04-02 03:34:37 +0000913 * dismiss it and send a HelloRetryRequest message.
914 */
XiaokangQian7807f9f2022-02-15 10:04:37 +0000915
Gilles Peskine449bd832023-01-11 14:50:10 +0100916 while (p < client_shares_end) {
XiaokangQian9b5d04b2022-04-10 10:20:43 +0000917 uint16_t group;
Jerry Yuc1be19f2022-04-23 16:11:39 +0800918 size_t key_exchange_len;
Jerry Yu086edc22022-05-05 10:50:38 +0800919 const unsigned char *key_exchange;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000920
921 /*
922 * struct {
923 * NamedGroup group;
924 * opaque key_exchange<1..2^16-1>;
925 * } KeyShareEntry;
926 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100927 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, client_shares_end, 4);
928 group = MBEDTLS_GET_UINT16_BE(p, 0);
929 key_exchange_len = MBEDTLS_GET_UINT16_BE(p, 2);
Jerry Yuc1be19f2022-04-23 16:11:39 +0800930 p += 4;
Jerry Yu086edc22022-05-05 10:50:38 +0800931 key_exchange = p;
Gilles Peskine449bd832023-01-11 14:50:10 +0100932 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, client_shares_end, key_exchange_len);
Jerry Yu086edc22022-05-05 10:50:38 +0800933 p += key_exchange_len;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000934
935 /* Continue parsing even if we have already found a match,
XiaokangQianc5763b52022-04-02 03:34:37 +0000936 * for input validation purposes.
937 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100938 if (!mbedtls_ssl_named_group_is_offered(ssl, group) ||
939 !mbedtls_ssl_named_group_is_supported(group) ||
940 ssl->handshake->offered_group_id != 0) {
XiaokangQian060d8672022-04-21 09:24:56 +0000941 continue;
942 }
XiaokangQian060d8672022-04-21 09:24:56 +0000943
XiaokangQian7807f9f2022-02-15 10:04:37 +0000944 /*
Przemek Stekielc89f3ea2023-05-18 15:45:53 +0200945 * ECDHE and FFDHE groups are supported
XiaokangQian7807f9f2022-02-15 10:04:37 +0000946 */
Przemek Stekielc89f3ea2023-05-18 15:45:53 +0200947 if (mbedtls_ssl_tls13_named_group_is_ecdhe(group) ||
Przemek Stekield5f79e72023-06-29 09:08:43 +0200948 mbedtls_ssl_tls13_named_group_is_ffdh(group)) {
Przemek Stekielc89f3ea2023-05-18 15:45:53 +0200949 MBEDTLS_SSL_DEBUG_MSG(2, ("ECDH/FFDH group: %s (%04x)",
Gilles Peskine449bd832023-01-11 14:50:10 +0100950 mbedtls_ssl_named_group_to_str(group),
951 group));
Przemek Stekiel7ac93be2023-07-04 10:02:38 +0200952 ret = mbedtls_ssl_tls13_read_public_xxdhe_share(
Gilles Peskine449bd832023-01-11 14:50:10 +0100953 ssl, key_exchange - 2, key_exchange_len + 2);
954 if (ret != 0) {
955 return ret;
956 }
XiaokangQian4e8cd7b2022-04-21 09:48:09 +0000957
Gilles Peskine449bd832023-01-11 14:50:10 +0100958 } else {
959 MBEDTLS_SSL_DEBUG_MSG(4, ("Unrecognized NamedGroup %u",
960 (unsigned) group));
XiaokangQian7807f9f2022-02-15 10:04:37 +0000961 continue;
962 }
963
XiaokangQian9b5d04b2022-04-10 10:20:43 +0000964 ssl->handshake->offered_group_id = group;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000965 }
966
Jerry Yuc1be19f2022-04-23 16:11:39 +0800967
Gilles Peskine449bd832023-01-11 14:50:10 +0100968 if (ssl->handshake->offered_group_id == 0) {
969 MBEDTLS_SSL_DEBUG_MSG(1, ("no matching key share"));
970 return SSL_TLS1_3_PARSE_KEY_SHARES_EXT_NO_MATCH;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000971 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100972 return 0;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000973}
Valerio Settic9ae8622023-07-25 11:23:50 +0200974#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_EPHEMERAL_ENABLED */
XiaokangQian7807f9f2022-02-15 10:04:37 +0000975
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200976MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100977static int ssl_tls13_client_hello_has_exts(mbedtls_ssl_context *ssl,
978 int exts_mask)
XiaokangQian7807f9f2022-02-15 10:04:37 +0000979{
Jerry Yu0c354a22022-08-29 15:25:36 +0800980 int masked = ssl->handshake->received_extensions & exts_mask;
Gilles Peskine449bd832023-01-11 14:50:10 +0100981 return masked == exts_mask;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000982}
983
Jerry Yue5991322022-11-07 14:03:44 +0800984#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200985MBEDTLS_CHECK_RETURN_CRITICAL
XiaokangQianb67384d2022-04-19 00:02:38 +0000986static int ssl_tls13_client_hello_has_exts_for_ephemeral_key_exchange(
Gilles Peskine449bd832023-01-11 14:50:10 +0100987 mbedtls_ssl_context *ssl)
XiaokangQian7807f9f2022-02-15 10:04:37 +0000988{
Gilles Peskine449bd832023-01-11 14:50:10 +0100989 return ssl_tls13_client_hello_has_exts(
990 ssl,
991 MBEDTLS_SSL_EXT_MASK(SUPPORTED_GROUPS) |
992 MBEDTLS_SSL_EXT_MASK(KEY_SHARE) |
993 MBEDTLS_SSL_EXT_MASK(SIG_ALG));
XiaokangQian7807f9f2022-02-15 10:04:37 +0000994}
Jerry Yue5991322022-11-07 14:03:44 +0800995#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED */
XiaokangQian7807f9f2022-02-15 10:04:37 +0000996
Jerry Yue5991322022-11-07 14:03:44 +0800997#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK_ENABLED)
Jerry Yu77f01482022-07-11 07:03:24 +0000998MBEDTLS_CHECK_RETURN_CRITICAL
999static int ssl_tls13_client_hello_has_exts_for_psk_key_exchange(
Gilles Peskine449bd832023-01-11 14:50:10 +01001000 mbedtls_ssl_context *ssl)
Jerry Yu77f01482022-07-11 07:03:24 +00001001{
Gilles Peskine449bd832023-01-11 14:50:10 +01001002 return ssl_tls13_client_hello_has_exts(
1003 ssl,
1004 MBEDTLS_SSL_EXT_MASK(PRE_SHARED_KEY) |
1005 MBEDTLS_SSL_EXT_MASK(PSK_KEY_EXCHANGE_MODES));
Jerry Yu77f01482022-07-11 07:03:24 +00001006}
Jerry Yue5991322022-11-07 14:03:44 +08001007#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK_ENABLED */
Jerry Yu77f01482022-07-11 07:03:24 +00001008
Jerry Yue5991322022-11-07 14:03:44 +08001009#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK_EPHEMERAL_ENABLED)
Jerry Yu77f01482022-07-11 07:03:24 +00001010MBEDTLS_CHECK_RETURN_CRITICAL
1011static int ssl_tls13_client_hello_has_exts_for_psk_ephemeral_key_exchange(
Gilles Peskine449bd832023-01-11 14:50:10 +01001012 mbedtls_ssl_context *ssl)
Jerry Yu77f01482022-07-11 07:03:24 +00001013{
Gilles Peskine449bd832023-01-11 14:50:10 +01001014 return ssl_tls13_client_hello_has_exts(
1015 ssl,
1016 MBEDTLS_SSL_EXT_MASK(SUPPORTED_GROUPS) |
1017 MBEDTLS_SSL_EXT_MASK(KEY_SHARE) |
1018 MBEDTLS_SSL_EXT_MASK(PRE_SHARED_KEY) |
1019 MBEDTLS_SSL_EXT_MASK(PSK_KEY_EXCHANGE_MODES));
Jerry Yu77f01482022-07-11 07:03:24 +00001020}
Jerry Yue5991322022-11-07 14:03:44 +08001021#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK_EPHEMERAL_ENABLED */
Jerry Yu77f01482022-07-11 07:03:24 +00001022
Pengyu Lv306a01d2023-02-02 16:35:47 +08001023#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED)
1024MBEDTLS_CHECK_RETURN_CRITICAL
Pengyu Lvbc4aab72023-12-01 15:37:24 +08001025static int ssl_tls13_key_exchange_is_psk_available(mbedtls_ssl_context *ssl)
Jerry Yu77f01482022-07-11 07:03:24 +00001026{
Jerry Yue5991322022-11-07 14:03:44 +08001027#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK_ENABLED)
Ronald Crone8c162d2024-02-21 10:15:44 +01001028 return mbedtls_ssl_conf_tls13_is_psk_enabled(ssl) &&
Pengyu Lvb2cfafb2023-11-14 13:56:13 +08001029 mbedtls_ssl_tls13_is_psk_supported(ssl) &&
Gilles Peskine449bd832023-01-11 14:50:10 +01001030 ssl_tls13_client_hello_has_exts_for_psk_key_exchange(ssl);
Jerry Yu77f01482022-07-11 07:03:24 +00001031#else
1032 ((void) ssl);
Gilles Peskine449bd832023-01-11 14:50:10 +01001033 return 0;
Jerry Yu77f01482022-07-11 07:03:24 +00001034#endif
1035}
XiaokangQian7807f9f2022-02-15 10:04:37 +00001036
Jerry Yu77f01482022-07-11 07:03:24 +00001037MBEDTLS_CHECK_RETURN_CRITICAL
Pengyu Lvbc4aab72023-12-01 15:37:24 +08001038static int ssl_tls13_key_exchange_is_psk_ephemeral_available(mbedtls_ssl_context *ssl)
Jerry Yu77f01482022-07-11 07:03:24 +00001039{
Jerry Yue5991322022-11-07 14:03:44 +08001040#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK_EPHEMERAL_ENABLED)
Ronald Crone8c162d2024-02-21 10:15:44 +01001041 return mbedtls_ssl_conf_tls13_is_psk_ephemeral_enabled(ssl) &&
Pengyu Lvb2cfafb2023-11-14 13:56:13 +08001042 mbedtls_ssl_tls13_is_psk_ephemeral_supported(ssl) &&
Gilles Peskine449bd832023-01-11 14:50:10 +01001043 ssl_tls13_client_hello_has_exts_for_psk_ephemeral_key_exchange(ssl);
Jerry Yu77f01482022-07-11 07:03:24 +00001044#else
1045 ((void) ssl);
Gilles Peskine449bd832023-01-11 14:50:10 +01001046 return 0;
Jerry Yu77f01482022-07-11 07:03:24 +00001047#endif
1048}
Ronald Cron79cdd412024-02-20 17:19:28 +01001049#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED */
Jerry Yu77f01482022-07-11 07:03:24 +00001050
Ronald Cron79cdd412024-02-20 17:19:28 +01001051MBEDTLS_CHECK_RETURN_CRITICAL
1052static int ssl_tls13_key_exchange_is_ephemeral_available(mbedtls_ssl_context *ssl)
Jerry Yu77f01482022-07-11 07:03:24 +00001053{
Ronald Cron79cdd412024-02-20 17:19:28 +01001054#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED)
1055 return mbedtls_ssl_conf_tls13_is_ephemeral_enabled(ssl) &&
1056 ssl_tls13_client_hello_has_exts_for_ephemeral_key_exchange(ssl);
1057#else
1058 ((void) ssl);
Gilles Peskine449bd832023-01-11 14:50:10 +01001059 return 0;
Ronald Cron79cdd412024-02-20 17:19:28 +01001060#endif
XiaokangQian7807f9f2022-02-15 10:04:37 +00001061}
1062
XiaokangQian81802f42022-06-10 13:25:22 +00001063#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
Ronald Cron928cbd32022-10-04 16:14:26 +02001064 defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED)
Ronald Cron67ea2542022-09-15 17:34:42 +02001065
1066#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01001067static psa_algorithm_t ssl_tls13_iana_sig_alg_to_psa_alg(uint16_t sig_alg)
Ronald Cron67ea2542022-09-15 17:34:42 +02001068{
Gilles Peskine449bd832023-01-11 14:50:10 +01001069 switch (sig_alg) {
Ronald Cron67ea2542022-09-15 17:34:42 +02001070 case MBEDTLS_TLS1_3_SIG_ECDSA_SECP256R1_SHA256:
Gilles Peskine449bd832023-01-11 14:50:10 +01001071 return PSA_ALG_ECDSA(PSA_ALG_SHA_256);
Ronald Cron67ea2542022-09-15 17:34:42 +02001072 case MBEDTLS_TLS1_3_SIG_ECDSA_SECP384R1_SHA384:
Gilles Peskine449bd832023-01-11 14:50:10 +01001073 return PSA_ALG_ECDSA(PSA_ALG_SHA_384);
Ronald Cron67ea2542022-09-15 17:34:42 +02001074 case MBEDTLS_TLS1_3_SIG_ECDSA_SECP521R1_SHA512:
Gilles Peskine449bd832023-01-11 14:50:10 +01001075 return PSA_ALG_ECDSA(PSA_ALG_SHA_512);
Ronald Cron67ea2542022-09-15 17:34:42 +02001076 case MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA256:
Gilles Peskine449bd832023-01-11 14:50:10 +01001077 return PSA_ALG_RSA_PSS(PSA_ALG_SHA_256);
Ronald Cron67ea2542022-09-15 17:34:42 +02001078 case MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA384:
Gilles Peskine449bd832023-01-11 14:50:10 +01001079 return PSA_ALG_RSA_PSS(PSA_ALG_SHA_384);
Ronald Cron67ea2542022-09-15 17:34:42 +02001080 case MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA512:
Gilles Peskine449bd832023-01-11 14:50:10 +01001081 return PSA_ALG_RSA_PSS(PSA_ALG_SHA_512);
Ronald Cron67ea2542022-09-15 17:34:42 +02001082 case MBEDTLS_TLS1_3_SIG_RSA_PKCS1_SHA256:
Gilles Peskine449bd832023-01-11 14:50:10 +01001083 return PSA_ALG_RSA_PKCS1V15_SIGN(PSA_ALG_SHA_256);
Ronald Cron67ea2542022-09-15 17:34:42 +02001084 case MBEDTLS_TLS1_3_SIG_RSA_PKCS1_SHA384:
Gilles Peskine449bd832023-01-11 14:50:10 +01001085 return PSA_ALG_RSA_PKCS1V15_SIGN(PSA_ALG_SHA_384);
Ronald Cron67ea2542022-09-15 17:34:42 +02001086 case MBEDTLS_TLS1_3_SIG_RSA_PKCS1_SHA512:
Gilles Peskine449bd832023-01-11 14:50:10 +01001087 return PSA_ALG_RSA_PKCS1V15_SIGN(PSA_ALG_SHA_512);
Ronald Cron67ea2542022-09-15 17:34:42 +02001088 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01001089 return PSA_ALG_NONE;
Ronald Cron67ea2542022-09-15 17:34:42 +02001090 }
1091}
1092#endif /* MBEDTLS_USE_PSA_CRYPTO */
1093
XiaokangQian23c5be62022-06-07 02:04:34 +00001094/*
XiaokangQianfb665a82022-06-15 03:57:21 +00001095 * Pick best ( private key, certificate chain ) pair based on the signature
1096 * algorithms supported by the client.
XiaokangQian23c5be62022-06-07 02:04:34 +00001097 */
Ronald Cronce7d76e2022-07-08 18:56:49 +02001098MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01001099static int ssl_tls13_pick_key_cert(mbedtls_ssl_context *ssl)
XiaokangQian23c5be62022-06-07 02:04:34 +00001100{
XiaokangQianfb665a82022-06-15 03:57:21 +00001101 mbedtls_ssl_key_cert *key_cert, *key_cert_list;
XiaokangQian81802f42022-06-10 13:25:22 +00001102 const uint16_t *sig_alg = ssl->handshake->received_sig_algs;
XiaokangQian23c5be62022-06-07 02:04:34 +00001103
1104#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Gilles Peskine449bd832023-01-11 14:50:10 +01001105 if (ssl->handshake->sni_key_cert != NULL) {
XiaokangQianfb665a82022-06-15 03:57:21 +00001106 key_cert_list = ssl->handshake->sni_key_cert;
Gilles Peskine449bd832023-01-11 14:50:10 +01001107 } else
XiaokangQian23c5be62022-06-07 02:04:34 +00001108#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
Gilles Peskine449bd832023-01-11 14:50:10 +01001109 key_cert_list = ssl->conf->key_cert;
XiaokangQian23c5be62022-06-07 02:04:34 +00001110
Gilles Peskine449bd832023-01-11 14:50:10 +01001111 if (key_cert_list == NULL) {
1112 MBEDTLS_SSL_DEBUG_MSG(3, ("server has no certificate"));
1113 return -1;
XiaokangQian23c5be62022-06-07 02:04:34 +00001114 }
1115
Gilles Peskine449bd832023-01-11 14:50:10 +01001116 for (; *sig_alg != MBEDTLS_TLS1_3_SIG_NONE; sig_alg++) {
1117 if (!mbedtls_ssl_sig_alg_is_offered(ssl, *sig_alg)) {
Ronald Cron67ea2542022-09-15 17:34:42 +02001118 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +01001119 }
Ronald Cron67ea2542022-09-15 17:34:42 +02001120
Gilles Peskine449bd832023-01-11 14:50:10 +01001121 if (!mbedtls_ssl_tls13_sig_alg_for_cert_verify_is_supported(*sig_alg)) {
Ronald Cron67ea2542022-09-15 17:34:42 +02001122 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +01001123 }
Ronald Cron67ea2542022-09-15 17:34:42 +02001124
Gilles Peskine449bd832023-01-11 14:50:10 +01001125 for (key_cert = key_cert_list; key_cert != NULL;
1126 key_cert = key_cert->next) {
Ronald Cron67ea2542022-09-15 17:34:42 +02001127#if defined(MBEDTLS_USE_PSA_CRYPTO)
1128 psa_algorithm_t psa_alg = PSA_ALG_NONE;
1129#endif /* MBEDTLS_USE_PSA_CRYPTO */
1130
Gilles Peskine449bd832023-01-11 14:50:10 +01001131 MBEDTLS_SSL_DEBUG_CRT(3, "certificate (chain) candidate",
1132 key_cert->cert);
XiaokangQian81802f42022-06-10 13:25:22 +00001133
1134 /*
Gilles Peskine449bd832023-01-11 14:50:10 +01001135 * This avoids sending the client a cert it'll reject based on
1136 * keyUsage or other extensions.
1137 */
1138 if (mbedtls_x509_crt_check_key_usage(
1139 key_cert->cert, MBEDTLS_X509_KU_DIGITAL_SIGNATURE) != 0 ||
XiaokangQian81802f42022-06-10 13:25:22 +00001140 mbedtls_x509_crt_check_extended_key_usage(
XiaokangQianfb665a82022-06-15 03:57:21 +00001141 key_cert->cert, MBEDTLS_OID_SERVER_AUTH,
Gilles Peskine449bd832023-01-11 14:50:10 +01001142 MBEDTLS_OID_SIZE(MBEDTLS_OID_SERVER_AUTH)) != 0) {
1143 MBEDTLS_SSL_DEBUG_MSG(3, ("certificate mismatch: "
1144 "(extended) key usage extension"));
XiaokangQian81802f42022-06-10 13:25:22 +00001145 continue;
1146 }
XiaokangQian23c5be62022-06-07 02:04:34 +00001147
Gilles Peskine449bd832023-01-11 14:50:10 +01001148 MBEDTLS_SSL_DEBUG_MSG(3,
1149 ("ssl_tls13_pick_key_cert:"
1150 "check signature algorithm %s [%04x]",
1151 mbedtls_ssl_sig_alg_to_str(*sig_alg),
1152 *sig_alg));
Ronald Cron67ea2542022-09-15 17:34:42 +02001153#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01001154 psa_alg = ssl_tls13_iana_sig_alg_to_psa_alg(*sig_alg);
Ronald Cron67ea2542022-09-15 17:34:42 +02001155#endif /* MBEDTLS_USE_PSA_CRYPTO */
1156
Gilles Peskine449bd832023-01-11 14:50:10 +01001157 if (mbedtls_ssl_tls13_check_sig_alg_cert_key_match(
1158 *sig_alg, &key_cert->cert->pk)
Ronald Cron67ea2542022-09-15 17:34:42 +02001159#if defined(MBEDTLS_USE_PSA_CRYPTO)
1160 && psa_alg != PSA_ALG_NONE &&
Gilles Peskine449bd832023-01-11 14:50:10 +01001161 mbedtls_pk_can_do_ext(&key_cert->cert->pk, psa_alg,
1162 PSA_KEY_USAGE_SIGN_HASH) == 1
Ronald Cron67ea2542022-09-15 17:34:42 +02001163#endif /* MBEDTLS_USE_PSA_CRYPTO */
Gilles Peskine449bd832023-01-11 14:50:10 +01001164 ) {
XiaokangQianfb665a82022-06-15 03:57:21 +00001165 ssl->handshake->key_cert = key_cert;
Gilles Peskine449bd832023-01-11 14:50:10 +01001166 MBEDTLS_SSL_DEBUG_MSG(3,
1167 ("ssl_tls13_pick_key_cert:"
1168 "selected signature algorithm"
1169 " %s [%04x]",
1170 mbedtls_ssl_sig_alg_to_str(*sig_alg),
1171 *sig_alg));
XiaokangQianfb665a82022-06-15 03:57:21 +00001172 MBEDTLS_SSL_DEBUG_CRT(
Gilles Peskine449bd832023-01-11 14:50:10 +01001173 3, "selected certificate (chain)",
1174 ssl->handshake->key_cert->cert);
1175 return 0;
XiaokangQian81802f42022-06-10 13:25:22 +00001176 }
XiaokangQian81802f42022-06-10 13:25:22 +00001177 }
XiaokangQian23c5be62022-06-07 02:04:34 +00001178 }
1179
Gilles Peskine449bd832023-01-11 14:50:10 +01001180 MBEDTLS_SSL_DEBUG_MSG(2, ("ssl_tls13_pick_key_cert:"
1181 "no suitable certificate found"));
1182 return -1;
XiaokangQian23c5be62022-06-07 02:04:34 +00001183}
XiaokangQian81802f42022-06-10 13:25:22 +00001184#endif /* MBEDTLS_X509_CRT_PARSE_C &&
Ronald Cron928cbd32022-10-04 16:14:26 +02001185 MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED */
XiaokangQian23c5be62022-06-07 02:04:34 +00001186
XiaokangQian4080a7f2022-04-11 09:55:18 +00001187/*
XiaokangQianed582dd2022-04-13 08:21:05 +00001188 *
1189 * STATE HANDLING: ClientHello
1190 *
XiaokangQianb67384d2022-04-19 00:02:38 +00001191 * There are three possible classes of outcomes when parsing the ClientHello:
XiaokangQianed582dd2022-04-13 08:21:05 +00001192 *
XiaokangQianb67384d2022-04-19 00:02:38 +00001193 * 1) The ClientHello was well-formed and matched the server's configuration.
XiaokangQianed582dd2022-04-13 08:21:05 +00001194 *
1195 * In this case, the server progresses to sending its ServerHello.
1196 *
XiaokangQianb67384d2022-04-19 00:02:38 +00001197 * 2) The ClientHello was well-formed but didn't match the server's
1198 * configuration.
XiaokangQianed582dd2022-04-13 08:21:05 +00001199 *
1200 * For example, the client might not have offered a key share which
1201 * the server supports, or the server might require a cookie.
1202 *
1203 * In this case, the server sends a HelloRetryRequest.
1204 *
XiaokangQianb67384d2022-04-19 00:02:38 +00001205 * 3) The ClientHello was ill-formed
XiaokangQianed582dd2022-04-13 08:21:05 +00001206 *
1207 * In this case, we abort the handshake.
1208 *
1209 */
1210
1211/*
XiaokangQian4080a7f2022-04-11 09:55:18 +00001212 * Structure of this message:
1213 *
XiaokangQiane8ff3502022-04-22 02:34:40 +00001214 * uint16 ProtocolVersion;
1215 * opaque Random[32];
1216 * uint8 CipherSuite[2]; // Cryptographic suite selector
XiaokangQian4080a7f2022-04-11 09:55:18 +00001217 *
XiaokangQiane8ff3502022-04-22 02:34:40 +00001218 * struct {
1219 * ProtocolVersion legacy_version = 0x0303; // TLS v1.2
1220 * Random random;
1221 * opaque legacy_session_id<0..32>;
1222 * CipherSuite cipher_suites<2..2^16-2>;
1223 * opaque legacy_compression_methods<1..2^8-1>;
1224 * Extension extensions<8..2^16-1>;
1225 * } ClientHello;
XiaokangQian4080a7f2022-04-11 09:55:18 +00001226 */
XiaokangQianed582dd2022-04-13 08:21:05 +00001227
1228#define SSL_CLIENT_HELLO_OK 0
1229#define SSL_CLIENT_HELLO_HRR_REQUIRED 1
Ronald Cron5af4c7f2023-03-07 20:46:59 +01001230#define SSL_CLIENT_HELLO_TLS1_2 2
XiaokangQianed582dd2022-04-13 08:21:05 +00001231
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001232MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01001233static int ssl_tls13_parse_client_hello(mbedtls_ssl_context *ssl,
1234 const unsigned char *buf,
1235 const unsigned char *end)
XiaokangQian7807f9f2022-02-15 10:04:37 +00001236{
XiaokangQianb67384d2022-04-19 00:02:38 +00001237 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1238 const unsigned char *p = buf;
Ronald Crond540d992023-03-07 09:41:48 +01001239 const unsigned char *random;
XiaokangQian4080a7f2022-04-11 09:55:18 +00001240 size_t legacy_session_id_len;
Ronald Croncada4102023-03-07 09:51:39 +01001241 const unsigned char *legacy_session_id;
XiaokangQian318dc762022-04-20 09:43:51 +00001242 size_t cipher_suites_len;
Ronald Cron2f16b4e2023-03-07 10:07:32 +01001243 const unsigned char *cipher_suites;
XiaokangQian060d8672022-04-21 09:24:56 +00001244 const unsigned char *cipher_suites_end;
XiaokangQianb67384d2022-04-19 00:02:38 +00001245 size_t extensions_len;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001246 const unsigned char *extensions_end;
Ronald Croneff56732023-04-03 17:36:31 +02001247 const unsigned char *supported_versions_data;
1248 const unsigned char *supported_versions_data_end;
Jerry Yuc4bf5d62022-10-29 09:08:47 +08001249 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
Jerry Yu49ca9282022-05-05 11:05:22 +08001250 int hrr_required = 0;
Ronald Cron8a74f072023-06-14 17:59:29 +02001251 int no_usable_share_for_key_agreement = 0;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001252
Ronald Cron41a443a2022-10-04 16:38:25 +02001253#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED)
Ronald Cron79cdd412024-02-20 17:19:28 +01001254 int got_psk = 0;
1255 struct psk_attributes psk = PSK_ATTRIBUTES_INIT;
Jerry Yu29d9faa2022-08-23 17:52:45 +08001256 const unsigned char *pre_shared_key_ext = NULL;
Jerry Yu1c105562022-07-10 06:32:38 +00001257 const unsigned char *pre_shared_key_ext_end = NULL;
Ronald Cron41a443a2022-10-04 16:38:25 +02001258#endif
XiaokangQian7807f9f2022-02-15 10:04:37 +00001259
XiaokangQian7807f9f2022-02-15 10:04:37 +00001260 /*
XiaokangQianb67384d2022-04-19 00:02:38 +00001261 * ClientHello layout:
XiaokangQian7807f9f2022-02-15 10:04:37 +00001262 * 0 . 1 protocol version
XiaokangQiane8ff3502022-04-22 02:34:40 +00001263 * 2 . 33 random bytes
XiaokangQianc5763b52022-04-02 03:34:37 +00001264 * 34 . 34 session id length ( 1 byte )
XiaokangQian7807f9f2022-02-15 10:04:37 +00001265 * 35 . 34+x session id
XiaokangQian7807f9f2022-02-15 10:04:37 +00001266 * .. . .. ciphersuite list length ( 2 bytes )
1267 * .. . .. ciphersuite list
1268 * .. . .. compression alg. list length ( 1 byte )
1269 * .. . .. compression alg. list
1270 * .. . .. extensions length ( 2 bytes, optional )
1271 * .. . .. extensions ( optional )
1272 */
1273
XiaokangQianb67384d2022-04-19 00:02:38 +00001274 /*
bootstrap-prime6dbbf442022-05-17 19:30:44 -04001275 * Minimal length ( with everything empty and extensions omitted ) is
XiaokangQian7807f9f2022-02-15 10:04:37 +00001276 * 2 + 32 + 1 + 2 + 1 = 38 bytes. Check that first, so that we can
1277 * read at least up to session id length without worrying.
1278 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001279 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, 38);
XiaokangQian7807f9f2022-02-15 10:04:37 +00001280
1281 /* ...
1282 * ProtocolVersion legacy_version = 0x0303; // TLS 1.2
1283 * ...
1284 * with ProtocolVersion defined as:
1285 * uint16 ProtocolVersion;
1286 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001287 if (mbedtls_ssl_read_version(p, ssl->conf->transport) !=
1288 MBEDTLS_SSL_VERSION_TLS1_2) {
1289 MBEDTLS_SSL_DEBUG_MSG(1, ("Unsupported version of TLS."));
1290 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_PROTOCOL_VERSION,
1291 MBEDTLS_ERR_SSL_BAD_PROTOCOL_VERSION);
1292 return MBEDTLS_ERR_SSL_BAD_PROTOCOL_VERSION;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001293 }
1294 p += 2;
1295
Jerry Yuc1be19f2022-04-23 16:11:39 +08001296 /* ...
1297 * Random random;
1298 * ...
XiaokangQianb67384d2022-04-19 00:02:38 +00001299 * with Random defined as:
1300 * opaque Random[32];
XiaokangQian7807f9f2022-02-15 10:04:37 +00001301 */
Ronald Crond540d992023-03-07 09:41:48 +01001302 random = p;
XiaokangQian08037552022-04-20 07:16:41 +00001303 p += MBEDTLS_CLIENT_HELLO_RANDOM_LEN;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001304
Jerry Yuc1be19f2022-04-23 16:11:39 +08001305 /* ...
XiaokangQianb67384d2022-04-19 00:02:38 +00001306 * opaque legacy_session_id<0..32>;
Jerry Yuc1be19f2022-04-23 16:11:39 +08001307 * ...
XiaokangQian7807f9f2022-02-15 10:04:37 +00001308 */
Ronald Croncada4102023-03-07 09:51:39 +01001309 legacy_session_id_len = *(p++);
1310 legacy_session_id = p;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001311
XiaokangQianb67384d2022-04-19 00:02:38 +00001312 /*
1313 * Check we have enough data for the legacy session identifier
Jerry Yue95c8af2022-07-26 15:48:20 +08001314 * and the ciphersuite list length.
XiaokangQianb67384d2022-04-19 00:02:38 +00001315 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001316 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, legacy_session_id_len + 2);
XiaokangQian4080a7f2022-04-11 09:55:18 +00001317 p += legacy_session_id_len;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001318
Ronald Cron2f16b4e2023-03-07 10:07:32 +01001319 /* ...
1320 * CipherSuite cipher_suites<2..2^16-2>;
1321 * ...
1322 * with CipherSuite defined as:
1323 * uint8 CipherSuite[2];
1324 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001325 cipher_suites_len = MBEDTLS_GET_UINT16_BE(p, 0);
XiaokangQian7807f9f2022-02-15 10:04:37 +00001326 p += 2;
Ronald Cron2f16b4e2023-03-07 10:07:32 +01001327 cipher_suites = p;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001328
Ronald Cronfc7ae872023-02-16 15:32:19 +01001329 /*
1330 * The length of the ciphersuite list has to be even.
1331 */
1332 if (cipher_suites_len & 1) {
1333 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR,
1334 MBEDTLS_ERR_SSL_DECODE_ERROR);
1335 return MBEDTLS_ERR_SSL_DECODE_ERROR;
1336 }
1337
XiaokangQianb67384d2022-04-19 00:02:38 +00001338 /* Check we have enough data for the ciphersuite list, the legacy
1339 * compression methods and the length of the extensions.
Jerry Yue95c8af2022-07-26 15:48:20 +08001340 *
1341 * cipher_suites cipher_suites_len bytes
1342 * legacy_compression_methods 2 bytes
1343 * extensions_len 2 bytes
XiaokangQianb67384d2022-04-19 00:02:38 +00001344 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001345 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, cipher_suites_len + 2 + 2);
Ronald Cron8c527d02023-03-07 15:47:47 +01001346 p += cipher_suites_len;
1347 cipher_suites_end = p;
Jerry Yu5725f1c2022-08-21 17:27:16 +08001348
1349 /*
Ronald Cron8c527d02023-03-07 15:47:47 +01001350 * Search for the supported versions extension and parse it to determine
1351 * if the client supports TLS 1.3.
1352 */
1353 ret = mbedtls_ssl_tls13_is_supported_versions_ext_present_in_exts(
1354 ssl, p + 2, end,
Ronald Croneff56732023-04-03 17:36:31 +02001355 &supported_versions_data, &supported_versions_data_end);
Ronald Cron8c527d02023-03-07 15:47:47 +01001356 if (ret < 0) {
1357 MBEDTLS_SSL_DEBUG_RET(1,
1358 ("mbedtls_ssl_tls13_is_supported_versions_ext_present_in_exts"), ret);
1359 return ret;
1360 }
1361
1362 if (ret == 0) {
Ronald Cron5af4c7f2023-03-07 20:46:59 +01001363 return SSL_CLIENT_HELLO_TLS1_2;
Ronald Cron8c527d02023-03-07 15:47:47 +01001364 }
1365
Ronald Cron5af4c7f2023-03-07 20:46:59 +01001366 if (ret == 1) {
1367 ret = ssl_tls13_parse_supported_versions_ext(ssl,
Ronald Croneff56732023-04-03 17:36:31 +02001368 supported_versions_data,
1369 supported_versions_data_end);
Ronald Cron5af4c7f2023-03-07 20:46:59 +01001370 if (ret < 0) {
1371 MBEDTLS_SSL_DEBUG_RET(1,
1372 ("ssl_tls13_parse_supported_versions_ext"), ret);
1373 return ret;
1374 }
1375
Ronald Cronb828c7d2023-04-03 16:37:22 +02001376 /*
1377 * The supported versions extension was parsed successfully as the
1378 * value returned by ssl_tls13_parse_supported_versions_ext() is
1379 * positive. The return value is then equal to
1380 * MBEDTLS_SSL_VERSION_TLS1_2 or MBEDTLS_SSL_VERSION_TLS1_3, defining
1381 * the TLS version to negotiate.
1382 */
Ronald Cron5af4c7f2023-03-07 20:46:59 +01001383 if (MBEDTLS_SSL_VERSION_TLS1_2 == ret) {
1384 return SSL_CLIENT_HELLO_TLS1_2;
1385 }
Ronald Cron8c527d02023-03-07 15:47:47 +01001386 }
1387
1388 /*
1389 * We negotiate TLS 1.3.
Ronald Cron64582392023-03-07 09:21:40 +01001390 */
1391 ssl->tls_version = MBEDTLS_SSL_VERSION_TLS1_3;
Ronald Cron64582392023-03-07 09:21:40 +01001392 ssl->session_negotiate->tls_version = MBEDTLS_SSL_VERSION_TLS1_3;
1393 ssl->session_negotiate->endpoint = ssl->conf->endpoint;
Ronald Cron64582392023-03-07 09:21:40 +01001394
1395 /*
Ronald Crondad02b22023-04-06 09:57:52 +02001396 * We are negotiating the version 1.3 of the protocol. Do what we have
Ronald Croncada4102023-03-07 09:51:39 +01001397 * postponed: copy of the client random bytes, copy of the legacy session
Ronald Cron2f16b4e2023-03-07 10:07:32 +01001398 * identifier and selection of the TLS 1.3 cipher suite.
Ronald Crond540d992023-03-07 09:41:48 +01001399 */
1400 MBEDTLS_SSL_DEBUG_BUF(3, "client hello, random bytes",
1401 random, MBEDTLS_CLIENT_HELLO_RANDOM_LEN);
1402 memcpy(&handshake->randbytes[0], random, MBEDTLS_CLIENT_HELLO_RANDOM_LEN);
1403
Ronald Croncada4102023-03-07 09:51:39 +01001404 if (legacy_session_id_len > sizeof(ssl->session_negotiate->id)) {
1405 MBEDTLS_SSL_DEBUG_MSG(1, ("bad client hello message"));
1406 return MBEDTLS_ERR_SSL_DECODE_ERROR;
1407 }
1408 ssl->session_negotiate->id_len = legacy_session_id_len;
1409 MBEDTLS_SSL_DEBUG_BUF(3, "client hello, session id",
1410 legacy_session_id, legacy_session_id_len);
1411 memcpy(&ssl->session_negotiate->id[0],
1412 legacy_session_id, legacy_session_id_len);
1413
Ronald Crond540d992023-03-07 09:41:48 +01001414 /*
Jerry Yu5725f1c2022-08-21 17:27:16 +08001415 * Search for a matching ciphersuite
1416 */
Ronald Cron2f16b4e2023-03-07 10:07:32 +01001417 MBEDTLS_SSL_DEBUG_BUF(3, "client hello, list of cipher suites",
1418 cipher_suites, cipher_suites_len);
Jerry Yu5725f1c2022-08-21 17:27:16 +08001419
Ronald Cron89089cc2024-02-14 18:18:08 +01001420 ssl_tls13_select_ciphersuite(ssl, cipher_suites, cipher_suites_end,
1421 0, PSA_ALG_NONE, &handshake->ciphersuite_info);
Jerry Yu5725f1c2022-08-21 17:27:16 +08001422
Gilles Peskine449bd832023-01-11 14:50:10 +01001423 if (handshake->ciphersuite_info == NULL) {
1424 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE,
1425 MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE);
1426 return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
Jerry Yu5725f1c2022-08-21 17:27:16 +08001427 }
Ronald Cron89089cc2024-02-14 18:18:08 +01001428 ssl->session_negotiate->ciphersuite = handshake->ciphersuite_info->id;
1429
1430 MBEDTLS_SSL_DEBUG_MSG(2, ("selected ciphersuite: %04x - %s",
1431 ((unsigned) handshake->ciphersuite_info->id),
1432 handshake->ciphersuite_info->name));
Jerry Yuc1be19f2022-04-23 16:11:39 +08001433
XiaokangQian4080a7f2022-04-11 09:55:18 +00001434 /* ...
XiaokangQianb67384d2022-04-19 00:02:38 +00001435 * opaque legacy_compression_methods<1..2^8-1>;
XiaokangQian4080a7f2022-04-11 09:55:18 +00001436 * ...
XiaokangQian7807f9f2022-02-15 10:04:37 +00001437 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001438 if (p[0] != 1 || p[1] != MBEDTLS_SSL_COMPRESS_NULL) {
1439 MBEDTLS_SSL_DEBUG_MSG(1, ("bad legacy compression method"));
1440 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER,
1441 MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER);
1442 return MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001443 }
XiaokangQianb67384d2022-04-19 00:02:38 +00001444 p += 2;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001445
Jerry Yuc1be19f2022-04-23 16:11:39 +08001446 /* ...
XiaokangQianb67384d2022-04-19 00:02:38 +00001447 * Extension extensions<8..2^16-1>;
Jerry Yuc1be19f2022-04-23 16:11:39 +08001448 * ...
XiaokangQianb67384d2022-04-19 00:02:38 +00001449 * with Extension defined as:
1450 * struct {
1451 * ExtensionType extension_type;
1452 * opaque extension_data<0..2^16-1>;
1453 * } Extension;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001454 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001455 extensions_len = MBEDTLS_GET_UINT16_BE(p, 0);
XiaokangQian7807f9f2022-02-15 10:04:37 +00001456 p += 2;
Gilles Peskine449bd832023-01-11 14:50:10 +01001457 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, extensions_len);
XiaokangQiane8ff3502022-04-22 02:34:40 +00001458 extensions_end = p + extensions_len;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001459
Gilles Peskine449bd832023-01-11 14:50:10 +01001460 MBEDTLS_SSL_DEBUG_BUF(3, "client hello extensions", p, extensions_len);
Jerry Yu63a459c2022-10-31 13:38:40 +08001461 handshake->received_extensions = MBEDTLS_SSL_EXT_MASK_NONE;
Jerry Yu0c354a22022-08-29 15:25:36 +08001462
Gilles Peskine449bd832023-01-11 14:50:10 +01001463 while (p < extensions_end) {
XiaokangQian7807f9f2022-02-15 10:04:37 +00001464 unsigned int extension_type;
1465 size_t extension_data_len;
1466 const unsigned char *extension_data_end;
Jerry Yu263dbf72022-10-26 10:51:27 +08001467 uint32_t allowed_exts = MBEDTLS_SSL_TLS1_3_ALLOWED_EXTS_OF_CH;
1468
Ronald Cron5fbd2702024-02-14 10:03:36 +01001469 if (ssl->handshake->hello_retry_request_flag) {
Jerry Yu263dbf72022-10-26 10:51:27 +08001470 /* Do not accept early data extension in 2nd ClientHello */
1471 allowed_exts &= ~MBEDTLS_SSL_EXT_MASK(EARLY_DATA);
1472 }
XiaokangQian7807f9f2022-02-15 10:04:37 +00001473
Jerry Yu0c354a22022-08-29 15:25:36 +08001474 /* RFC 8446, section 4.2.11
Jerry Yu1c9247c2022-07-21 12:37:39 +08001475 *
1476 * The "pre_shared_key" extension MUST be the last extension in the
1477 * ClientHello (this facilitates implementation as described below).
1478 * Servers MUST check that it is the last extension and otherwise fail
1479 * the handshake with an "illegal_parameter" alert.
1480 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001481 if (handshake->received_extensions & MBEDTLS_SSL_EXT_MASK(PRE_SHARED_KEY)) {
Jerry Yu1c9247c2022-07-21 12:37:39 +08001482 MBEDTLS_SSL_DEBUG_MSG(
Gilles Peskine449bd832023-01-11 14:50:10 +01001483 3, ("pre_shared_key is not last extension."));
Jerry Yu1c9247c2022-07-21 12:37:39 +08001484 MBEDTLS_SSL_PEND_FATAL_ALERT(
1485 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER,
Gilles Peskine449bd832023-01-11 14:50:10 +01001486 MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER);
1487 return MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER;
Jerry Yu1c9247c2022-07-21 12:37:39 +08001488 }
1489
Gilles Peskine449bd832023-01-11 14:50:10 +01001490 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, extensions_end, 4);
1491 extension_type = MBEDTLS_GET_UINT16_BE(p, 0);
1492 extension_data_len = MBEDTLS_GET_UINT16_BE(p, 2);
XiaokangQian7807f9f2022-02-15 10:04:37 +00001493 p += 4;
1494
Gilles Peskine449bd832023-01-11 14:50:10 +01001495 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, extensions_end, extension_data_len);
XiaokangQian7807f9f2022-02-15 10:04:37 +00001496 extension_data_end = p + extension_data_len;
1497
Jerry Yuc4bf5d62022-10-29 09:08:47 +08001498 ret = mbedtls_ssl_tls13_check_received_extension(
Gilles Peskine449bd832023-01-11 14:50:10 +01001499 ssl, MBEDTLS_SSL_HS_CLIENT_HELLO, extension_type,
Jerry Yu263dbf72022-10-26 10:51:27 +08001500 allowed_exts);
Gilles Peskine449bd832023-01-11 14:50:10 +01001501 if (ret != 0) {
1502 return ret;
1503 }
Jerry Yue18dc7e2022-08-04 16:29:22 +08001504
Gilles Peskine449bd832023-01-11 14:50:10 +01001505 switch (extension_type) {
XiaokangQian40a35232022-05-07 09:02:40 +00001506#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
1507 case MBEDTLS_TLS_EXT_SERVERNAME:
Gilles Peskine449bd832023-01-11 14:50:10 +01001508 MBEDTLS_SSL_DEBUG_MSG(3, ("found ServerName extension"));
1509 ret = mbedtls_ssl_parse_server_name_ext(ssl, p,
1510 extension_data_end);
1511 if (ret != 0) {
XiaokangQian40a35232022-05-07 09:02:40 +00001512 MBEDTLS_SSL_DEBUG_RET(
Gilles Peskine449bd832023-01-11 14:50:10 +01001513 1, "mbedtls_ssl_parse_servername_ext", ret);
1514 return ret;
XiaokangQian40a35232022-05-07 09:02:40 +00001515 }
XiaokangQian40a35232022-05-07 09:02:40 +00001516 break;
1517#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
1518
Przemek Stekiel8c0a9532023-06-15 16:48:19 +02001519#if defined(PSA_WANT_ALG_ECDH) || defined(PSA_WANT_ALG_FFDH)
XiaokangQian7807f9f2022-02-15 10:04:37 +00001520 case MBEDTLS_TLS_EXT_SUPPORTED_GROUPS:
Gilles Peskine449bd832023-01-11 14:50:10 +01001521 MBEDTLS_SSL_DEBUG_MSG(3, ("found supported group extension"));
XiaokangQian7807f9f2022-02-15 10:04:37 +00001522
1523 /* Supported Groups Extension
1524 *
1525 * When sent by the client, the "supported_groups" extension
1526 * indicates the named groups which the client supports,
1527 * ordered from most preferred to least preferred.
1528 */
Jerry Yuc1be19f2022-04-23 16:11:39 +08001529 ret = ssl_tls13_parse_supported_groups_ext(
Gilles Peskine449bd832023-01-11 14:50:10 +01001530 ssl, p, extension_data_end);
1531 if (ret != 0) {
Xiaokang Qian9f1747b2023-03-30 02:50:04 +00001532 MBEDTLS_SSL_DEBUG_RET(
Xiaokang Qian8bce0e62023-04-04 10:15:48 +00001533 1, "ssl_tls13_parse_supported_groups_ext", ret);
Gilles Peskine449bd832023-01-11 14:50:10 +01001534 return ret;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001535 }
1536
XiaokangQian7807f9f2022-02-15 10:04:37 +00001537 break;
Przemek Stekiel8c0a9532023-06-15 16:48:19 +02001538#endif /* PSA_WANT_ALG_ECDH || PSA_WANT_ALG_FFDH*/
XiaokangQian7807f9f2022-02-15 10:04:37 +00001539
Valerio Settic9ae8622023-07-25 11:23:50 +02001540#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_EPHEMERAL_ENABLED)
XiaokangQian7807f9f2022-02-15 10:04:37 +00001541 case MBEDTLS_TLS_EXT_KEY_SHARE:
Gilles Peskine449bd832023-01-11 14:50:10 +01001542 MBEDTLS_SSL_DEBUG_MSG(3, ("found key share extension"));
XiaokangQian7807f9f2022-02-15 10:04:37 +00001543
1544 /*
1545 * Key Share Extension
1546 *
1547 * When sent by the client, the "key_share" extension
1548 * contains the endpoint's cryptographic parameters for
1549 * ECDHE/DHE key establishment methods.
1550 */
Jerry Yuc1be19f2022-04-23 16:11:39 +08001551 ret = ssl_tls13_parse_key_shares_ext(
Gilles Peskine449bd832023-01-11 14:50:10 +01001552 ssl, p, extension_data_end);
1553 if (ret == SSL_TLS1_3_PARSE_KEY_SHARES_EXT_NO_MATCH) {
Ronald Cron8a74f072023-06-14 17:59:29 +02001554 MBEDTLS_SSL_DEBUG_MSG(2, ("No usable share for key agreement."));
1555 no_usable_share_for_key_agreement = 1;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001556 }
1557
Gilles Peskine449bd832023-01-11 14:50:10 +01001558 if (ret < 0) {
Jerry Yu582dd062022-04-22 21:59:01 +08001559 MBEDTLS_SSL_DEBUG_RET(
Gilles Peskine449bd832023-01-11 14:50:10 +01001560 1, "ssl_tls13_parse_key_shares_ext", ret);
1561 return ret;
Jerry Yu582dd062022-04-22 21:59:01 +08001562 }
XiaokangQian7807f9f2022-02-15 10:04:37 +00001563
XiaokangQian7807f9f2022-02-15 10:04:37 +00001564 break;
Valerio Settic9ae8622023-07-25 11:23:50 +02001565#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_EPHEMERAL_ENABLED */
XiaokangQian7807f9f2022-02-15 10:04:37 +00001566
1567 case MBEDTLS_TLS_EXT_SUPPORTED_VERSIONS:
Ronald Cron8c527d02023-03-07 15:47:47 +01001568 /* Already parsed */
XiaokangQian7807f9f2022-02-15 10:04:37 +00001569 break;
1570
Ronald Cron41a443a2022-10-04 16:38:25 +02001571#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED)
Jerry Yue19e3b92022-07-08 12:04:51 +00001572 case MBEDTLS_TLS_EXT_PSK_KEY_EXCHANGE_MODES:
Xiaokang Qian9f1747b2023-03-30 02:50:04 +00001573 MBEDTLS_SSL_DEBUG_MSG(
1574 3, ("found psk key exchange modes extension"));
Jerry Yue19e3b92022-07-08 12:04:51 +00001575
1576 ret = ssl_tls13_parse_key_exchange_modes_ext(
Gilles Peskine449bd832023-01-11 14:50:10 +01001577 ssl, p, extension_data_end);
1578 if (ret != 0) {
Jerry Yue19e3b92022-07-08 12:04:51 +00001579 MBEDTLS_SSL_DEBUG_RET(
Gilles Peskine449bd832023-01-11 14:50:10 +01001580 1, "ssl_tls13_parse_key_exchange_modes_ext", ret);
1581 return ret;
Jerry Yue19e3b92022-07-08 12:04:51 +00001582 }
1583
Jerry Yue19e3b92022-07-08 12:04:51 +00001584 break;
Ronald Cron41a443a2022-10-04 16:38:25 +02001585#endif
Jerry Yue19e3b92022-07-08 12:04:51 +00001586
Jerry Yu1c105562022-07-10 06:32:38 +00001587 case MBEDTLS_TLS_EXT_PRE_SHARED_KEY:
Gilles Peskine449bd832023-01-11 14:50:10 +01001588 MBEDTLS_SSL_DEBUG_MSG(3, ("found pre_shared_key extension"));
1589 if ((handshake->received_extensions &
1590 MBEDTLS_SSL_EXT_MASK(PSK_KEY_EXCHANGE_MODES)) == 0) {
Jerry Yu13ab81d2022-07-22 23:17:11 +08001591 MBEDTLS_SSL_PEND_FATAL_ALERT(
1592 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER,
Gilles Peskine449bd832023-01-11 14:50:10 +01001593 MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER);
1594 return MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER;
Jerry Yu13ab81d2022-07-22 23:17:11 +08001595 }
Ronald Cron41a443a2022-10-04 16:38:25 +02001596#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED)
Jerry Yu1c105562022-07-10 06:32:38 +00001597 /* Delay processing of the PSK identity once we have
1598 * found out which algorithms to use. We keep a pointer
1599 * to the buffer and the size for later processing.
1600 */
Jerry Yu29d9faa2022-08-23 17:52:45 +08001601 pre_shared_key_ext = p;
Jerry Yu1c105562022-07-10 06:32:38 +00001602 pre_shared_key_ext_end = extension_data_end;
Jerry Yue18dc7e2022-08-04 16:29:22 +08001603#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED */
Jerry Yu1c105562022-07-10 06:32:38 +00001604 break;
Jerry Yu1c105562022-07-10 06:32:38 +00001605
XiaokangQianacb39922022-06-17 10:18:48 +00001606#if defined(MBEDTLS_SSL_ALPN)
1607 case MBEDTLS_TLS_EXT_ALPN:
Gilles Peskine449bd832023-01-11 14:50:10 +01001608 MBEDTLS_SSL_DEBUG_MSG(3, ("found alpn extension"));
XiaokangQianacb39922022-06-17 10:18:48 +00001609
Gilles Peskine449bd832023-01-11 14:50:10 +01001610 ret = mbedtls_ssl_parse_alpn_ext(ssl, p, extension_data_end);
1611 if (ret != 0) {
XiaokangQianacb39922022-06-17 10:18:48 +00001612 MBEDTLS_SSL_DEBUG_RET(
Gilles Peskine449bd832023-01-11 14:50:10 +01001613 1, ("mbedtls_ssl_parse_alpn_ext"), ret);
1614 return ret;
XiaokangQianacb39922022-06-17 10:18:48 +00001615 }
XiaokangQianacb39922022-06-17 10:18:48 +00001616 break;
1617#endif /* MBEDTLS_SSL_ALPN */
1618
Ronald Cron928cbd32022-10-04 16:14:26 +02001619#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED)
XiaokangQian7807f9f2022-02-15 10:04:37 +00001620 case MBEDTLS_TLS_EXT_SIG_ALG:
Gilles Peskine449bd832023-01-11 14:50:10 +01001621 MBEDTLS_SSL_DEBUG_MSG(3, ("found signature_algorithms extension"));
XiaokangQian7807f9f2022-02-15 10:04:37 +00001622
Gabor Mezei078e8032022-04-27 21:17:56 +02001623 ret = mbedtls_ssl_parse_sig_alg_ext(
Gilles Peskine449bd832023-01-11 14:50:10 +01001624 ssl, p, extension_data_end);
1625 if (ret != 0) {
Xiaokang Qian49f39c12023-04-06 02:31:02 +00001626 MBEDTLS_SSL_DEBUG_RET(
1627 1, "mbedtls_ssl_parse_sig_alg_ext", ret);
Gilles Peskine449bd832023-01-11 14:50:10 +01001628 return ret;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001629 }
XiaokangQian7807f9f2022-02-15 10:04:37 +00001630 break;
Ronald Cron928cbd32022-10-04 16:14:26 +02001631#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED */
XiaokangQian7807f9f2022-02-15 10:04:37 +00001632
Jan Bruckner151f6422023-02-10 12:45:19 +01001633#if defined(MBEDTLS_SSL_RECORD_SIZE_LIMIT)
1634 case MBEDTLS_TLS_EXT_RECORD_SIZE_LIMIT:
1635 MBEDTLS_SSL_DEBUG_MSG(3, ("found record_size_limit extension"));
1636
Xiaokang Qian9f1747b2023-03-30 02:50:04 +00001637 ret = mbedtls_ssl_tls13_parse_record_size_limit_ext(
1638 ssl, p, extension_data_end);
Jan Brucknerf482dcc2023-03-15 09:09:06 +01001639 if (ret != 0) {
1640 MBEDTLS_SSL_DEBUG_RET(
1641 1, ("mbedtls_ssl_tls13_parse_record_size_limit_ext"), ret);
1642 return ret;
1643 }
Jan Bruckner151f6422023-02-10 12:45:19 +01001644 break;
1645#endif /* MBEDTLS_SSL_RECORD_SIZE_LIMIT */
1646
XiaokangQian7807f9f2022-02-15 10:04:37 +00001647 default:
Jerry Yu79aa7212022-11-08 21:30:21 +08001648 MBEDTLS_SSL_PRINT_EXT(
Jerry Yu63a459c2022-10-31 13:38:40 +08001649 3, MBEDTLS_SSL_HS_CLIENT_HELLO,
Gilles Peskine449bd832023-01-11 14:50:10 +01001650 extension_type, "( ignored )");
Jerry Yu0c354a22022-08-29 15:25:36 +08001651 break;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001652 }
1653
1654 p += extension_data_len;
1655 }
1656
Gilles Peskine449bd832023-01-11 14:50:10 +01001657 MBEDTLS_SSL_PRINT_EXTS(3, MBEDTLS_SSL_HS_CLIENT_HELLO,
1658 handshake->received_extensions);
Jerry Yue18dc7e2022-08-04 16:29:22 +08001659
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +01001660 ret = mbedtls_ssl_add_hs_hdr_to_checksum(ssl,
1661 MBEDTLS_SSL_HS_CLIENT_HELLO,
1662 p - buf);
1663 if (0 != ret) {
1664 MBEDTLS_SSL_DEBUG_RET(1, ("mbedtls_ssl_add_hs_hdr_to_checksum"), ret);
1665 return ret;
1666 }
Jerry Yu032b15ce2022-07-11 06:10:03 +00001667
Ronald Cron41a443a2022-10-04 16:38:25 +02001668#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED)
XiaokangQian7807f9f2022-02-15 10:04:37 +00001669 /* Update checksum with either
1670 * - The entire content of the CH message, if no PSK extension is present
1671 * - The content up to but excluding the PSK extension, if present.
Ronald Cron12e72f12024-02-14 15:49:38 +01001672 * Always parse the pre-shared key extension when present in the
1673 * ClientHello even if some pre-requisites for PSK key exchange modes are
1674 * not met. That way we always validate the syntax of the extension.
XiaokangQian7807f9f2022-02-15 10:04:37 +00001675 */
Ronald Cron12e72f12024-02-14 15:49:38 +01001676 if (handshake->received_extensions & MBEDTLS_SSL_EXT_MASK(PRE_SHARED_KEY)) {
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +01001677 ret = handshake->update_checksum(ssl, buf,
1678 pre_shared_key_ext - buf);
1679 if (0 != ret) {
1680 MBEDTLS_SSL_DEBUG_RET(1, ("update_checksum"), ret);
1681 return ret;
1682 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001683 ret = ssl_tls13_parse_pre_shared_key_ext(ssl,
1684 pre_shared_key_ext,
1685 pre_shared_key_ext_end,
1686 cipher_suites,
Ronald Cron79cdd412024-02-20 17:19:28 +01001687 cipher_suites_end,
1688 &psk);
1689 if (ret == 0) {
1690 got_psk = 1;
1691 } else if (ret != MBEDTLS_ERR_SSL_UNKNOWN_IDENTITY) {
Jerry Yu63a459c2022-10-31 13:38:40 +08001692 MBEDTLS_SSL_DEBUG_RET(
Gilles Peskine449bd832023-01-11 14:50:10 +01001693 1, "ssl_tls13_parse_pre_shared_key_ext", ret);
1694 return ret;
Jerry Yu1c105562022-07-10 06:32:38 +00001695 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001696 } else
Ronald Cron41a443a2022-10-04 16:38:25 +02001697#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED */
Jerry Yu1c105562022-07-10 06:32:38 +00001698 {
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +01001699 ret = handshake->update_checksum(ssl, buf, p - buf);
1700 if (0 != ret) {
1701 MBEDTLS_SSL_DEBUG_RET(1, ("update_checksum"), ret);
1702 return ret;
1703 }
Jerry Yu1c105562022-07-10 06:32:38 +00001704 }
XiaokangQian7807f9f2022-02-15 10:04:37 +00001705
Ronald Cron79cdd412024-02-20 17:19:28 +01001706 /*
1707 * Determine the key exchange algorithm to use.
1708 * There are three types of key exchanges supported in TLS 1.3:
1709 * - (EC)DH with ECDSA,
1710 * - (EC)DH with PSK,
1711 * - plain PSK.
1712 *
1713 * The PSK-based key exchanges may additionally be used with 0-RTT.
1714 *
1715 * Our built-in order of preference is
1716 * 1 ) (EC)DHE-PSK Mode ( psk_ephemeral )
1717 * 2 ) Certificate Mode ( ephemeral )
1718 * 3 ) Plain PSK Mode ( psk )
1719 */
1720#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED)
1721 if (got_psk && (psk.key_exchange_mode ==
1722 MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK_EPHEMERAL)) {
1723 handshake->key_exchange_mode =
1724 MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK_EPHEMERAL;
1725 MBEDTLS_SSL_DEBUG_MSG(2, ("key exchange mode: psk_ephemeral"));
1726
1727 } else
1728#endif
1729 if (ssl_tls13_key_exchange_is_ephemeral_available(ssl)) {
1730 handshake->key_exchange_mode =
1731 MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL;
1732 MBEDTLS_SSL_DEBUG_MSG(2, ("key exchange mode: ephemeral"));
1733
1734 }
1735#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED)
1736 else if (got_psk && (psk.key_exchange_mode ==
1737 MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK)) {
1738 handshake->key_exchange_mode = MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK;
1739 MBEDTLS_SSL_DEBUG_MSG(2, ("key exchange mode: psk"));
1740 }
1741#endif
1742 else {
1743 MBEDTLS_SSL_DEBUG_MSG(
1744 1,
1745 ("ClientHello message misses mandatory extensions."));
1746 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_MISSING_EXTENSION,
1747 MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER);
1748 return MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER;
Gilles Peskine449bd832023-01-11 14:50:10 +01001749 }
XiaokangQian7807f9f2022-02-15 10:04:37 +00001750
Ronald Cron3e47eec2024-02-21 10:29:24 +01001751#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED)
Ronald Cron74a16292024-02-23 17:07:41 +01001752 if (handshake->key_exchange_mode &
1753 MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK_ALL) {
1754 handshake->ciphersuite_info = psk.ciphersuite_info;
1755 ssl->session_negotiate->ciphersuite = psk.ciphersuite_info->id;
1756
1757 MBEDTLS_SSL_DEBUG_MSG(2, ("Select PSK ciphersuite: %04x - %s",
1758 ((unsigned) psk.ciphersuite_info->id),
1759 psk.ciphersuite_info->name));
1760
1761 if (psk.type == MBEDTLS_SSL_TLS1_3_PSK_RESUMPTION) {
1762 handshake->resume = 1;
1763 }
Ronald Cron79cdd412024-02-20 17:19:28 +01001764 }
Ronald Cron3e47eec2024-02-21 10:29:24 +01001765#endif
Ronald Cron79cdd412024-02-20 17:19:28 +01001766
1767 if (handshake->key_exchange_mode !=
Ronald Cron8a74f072023-06-14 17:59:29 +02001768 MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK) {
1769 hrr_required = (no_usable_share_for_key_agreement != 0);
1770 }
1771
Gilles Peskine449bd832023-01-11 14:50:10 +01001772 mbedtls_ssl_optimize_checksum(ssl, handshake->ciphersuite_info);
Jerry Yue5834fd2022-08-29 20:16:09 +08001773
Gilles Peskine449bd832023-01-11 14:50:10 +01001774 return hrr_required ? SSL_CLIENT_HELLO_HRR_REQUIRED : SSL_CLIENT_HELLO_OK;
XiaokangQian75fe8c72022-06-15 09:42:45 +00001775}
1776
Jerry Yuab0da372023-02-08 13:55:24 +08001777#if defined(MBEDTLS_SSL_EARLY_DATA)
Jerry Yu4caf3ca2023-11-15 16:13:47 +08001778static int ssl_tls13_check_early_data_requirements(mbedtls_ssl_context *ssl)
Jerry Yuab0da372023-02-08 13:55:24 +08001779{
1780 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
1781
Jerry Yu985c9672022-12-04 14:06:30 +08001782 if (ssl->conf->early_data_enabled == MBEDTLS_SSL_EARLY_DATA_DISABLED) {
1783 MBEDTLS_SSL_DEBUG_MSG(
Jerry Yu985c9672022-12-04 14:06:30 +08001784 1,
Jerry Yu454dda32023-10-31 15:13:54 +08001785 ("EarlyData: rejected, feature disabled in server configuration."));
Jerry Yu4caf3ca2023-11-15 16:13:47 +08001786 return -1;
Ronald Cron7b6ee942024-01-12 10:29:55 +01001787 }
1788
Jerry Yu454dda32023-10-31 15:13:54 +08001789 if (!handshake->resume) {
1790 /* We currently support early data only in the case of PSKs established
1791 via a NewSessionTicket message thus in the case of a session
1792 resumption. */
Jerry Yu985c9672022-12-04 14:06:30 +08001793 MBEDTLS_SSL_DEBUG_MSG(
Jerry Yu7ef9fd82023-11-07 14:30:38 +08001794 1, ("EarlyData: rejected, not a session resumption."));
Jerry Yu4caf3ca2023-11-15 16:13:47 +08001795 return -1;
Jerry Yu985c9672022-12-04 14:06:30 +08001796 }
1797
Jerry Yu82fd6c12023-10-31 16:32:19 +08001798 /* RFC 8446 4.2.10
1799 *
1800 * In order to accept early data, the server MUST have accepted a PSK cipher
1801 * suite and selected the first key offered in the client's "pre_shared_key"
1802 * extension. In addition, it MUST verify that the following values are the
1803 * same as those associated with the selected PSK:
1804 * - The TLS version number
1805 * - The selected cipher suite
1806 * - The selected ALPN [RFC7301] protocol, if any
1807 *
1808 * NOTE:
Jerry Yu7ef9fd82023-11-07 14:30:38 +08001809 * - The TLS version number is checked in
1810 * ssl_tls13_offered_psks_check_identity_match_ticket().
1811 * - ALPN is not checked for the time being (TODO).
Jerry Yu82fd6c12023-10-31 16:32:19 +08001812 */
1813
1814 if (handshake->selected_identity != 0) {
1815 MBEDTLS_SSL_DEBUG_MSG(
Jerry Yu7ef9fd82023-11-07 14:30:38 +08001816 1, ("EarlyData: rejected, the selected key in "
1817 "`pre_shared_key` is not the first one."));
Jerry Yu4caf3ca2023-11-15 16:13:47 +08001818 return -1;
Jerry Yu82fd6c12023-10-31 16:32:19 +08001819 }
1820
1821 if (handshake->ciphersuite_info->id !=
1822 ssl->session_negotiate->ciphersuite) {
1823 MBEDTLS_SSL_DEBUG_MSG(
Jerry Yu7ef9fd82023-11-07 14:30:38 +08001824 1, ("EarlyData: rejected, the selected ciphersuite is not the one "
1825 "of the selected pre-shared key."));
Jerry Yu4caf3ca2023-11-15 16:13:47 +08001826 return -1;
Jerry Yu82fd6c12023-10-31 16:32:19 +08001827
1828 }
Jerry Yu985c9672022-12-04 14:06:30 +08001829
Pengyu Lv94a42cc2023-12-06 10:04:17 +08001830 if (!mbedtls_ssl_tls13_session_ticket_allow_early_data(ssl->session_negotiate)) {
Jerry Yufceddb32022-12-12 15:30:34 +08001831 MBEDTLS_SSL_DEBUG_MSG(
1832 1,
Jerry Yuea96ac32023-11-21 17:06:36 +08001833 ("EarlyData: rejected, early_data not allowed in ticket "
1834 "permission bits."));
Jerry Yu4caf3ca2023-11-15 16:13:47 +08001835 return -1;
Jerry Yufceddb32022-12-12 15:30:34 +08001836 }
Jerry Yu985c9672022-12-04 14:06:30 +08001837
Jerry Yu4caf3ca2023-11-15 16:13:47 +08001838 return 0;
Jerry Yuab0da372023-02-08 13:55:24 +08001839}
1840#endif /* MBEDTLS_SSL_EARLY_DATA */
1841
XiaokangQian75fe8c72022-06-15 09:42:45 +00001842/* Update the handshake state machine */
1843
Ronald Cronce7d76e2022-07-08 18:56:49 +02001844MBEDTLS_CHECK_RETURN_CRITICAL
Ronald Cron7b6ee942024-01-12 10:29:55 +01001845static int ssl_tls13_postprocess_client_hello(mbedtls_ssl_context *ssl,
1846 int hrr_required)
XiaokangQian75fe8c72022-06-15 09:42:45 +00001847{
1848 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1849
XiaokangQian7807f9f2022-02-15 10:04:37 +00001850 /*
XiaokangQianfb665a82022-06-15 03:57:21 +00001851 * Server certificate selection
1852 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001853 if (ssl->conf->f_cert_cb && (ret = ssl->conf->f_cert_cb(ssl)) != 0) {
1854 MBEDTLS_SSL_DEBUG_RET(1, "f_cert_cb", ret);
1855 return ret;
XiaokangQianfb665a82022-06-15 03:57:21 +00001856 }
1857#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
1858 ssl->handshake->sni_name = NULL;
1859 ssl->handshake->sni_name_len = 0;
1860#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
XiaokangQian7807f9f2022-02-15 10:04:37 +00001861
Gilles Peskine449bd832023-01-11 14:50:10 +01001862 ret = mbedtls_ssl_tls13_key_schedule_stage_early(ssl);
1863 if (ret != 0) {
1864 MBEDTLS_SSL_DEBUG_RET(1,
1865 "mbedtls_ssl_tls1_3_key_schedule_stage_early", ret);
1866 return ret;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001867 }
1868
Jerry Yuab0da372023-02-08 13:55:24 +08001869#if defined(MBEDTLS_SSL_EARLY_DATA)
Jerry Yu4caf3ca2023-11-15 16:13:47 +08001870 if (ssl->handshake->received_extensions & MBEDTLS_SSL_EXT_MASK(EARLY_DATA)) {
Ronald Cron31e2d832024-02-05 16:45:57 +01001871 ssl->handshake->early_data_accepted =
1872 (!hrr_required) && (ssl_tls13_check_early_data_requirements(ssl) == 0);
Jerry Yu4e9b70e2022-12-04 14:08:02 +08001873
Jerry Yu4caf3ca2023-11-15 16:13:47 +08001874 if (ssl->handshake->early_data_accepted) {
1875 ret = mbedtls_ssl_tls13_compute_early_transform(ssl);
1876 if (ret != 0) {
1877 MBEDTLS_SSL_DEBUG_RET(
1878 1, "mbedtls_ssl_tls13_compute_early_transform", ret);
1879 return ret;
1880 }
1881 } else {
1882 ssl->discard_early_data_record =
1883 hrr_required ?
1884 MBEDTLS_SSL_EARLY_DATA_DISCARD :
1885 MBEDTLS_SSL_EARLY_DATA_TRY_TO_DEPROTECT_AND_DISCARD;
Jerry Yu4e9b70e2022-12-04 14:08:02 +08001886 }
1887 }
Ronald Cron7b6ee942024-01-12 10:29:55 +01001888#else
1889 ((void) hrr_required);
Jerry Yuab0da372023-02-08 13:55:24 +08001890#endif /* MBEDTLS_SSL_EARLY_DATA */
1891
Gilles Peskine449bd832023-01-11 14:50:10 +01001892 return 0;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001893}
1894
1895/*
XiaokangQianed582dd2022-04-13 08:21:05 +00001896 * Main entry point from the state machine; orchestrates the otherfunctions.
1897 */
1898
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001899MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01001900static int ssl_tls13_process_client_hello(mbedtls_ssl_context *ssl)
XiaokangQianed582dd2022-04-13 08:21:05 +00001901{
1902
XiaokangQian08037552022-04-20 07:16:41 +00001903 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine449bd832023-01-11 14:50:10 +01001904 unsigned char *buf = NULL;
XiaokangQianed582dd2022-04-13 08:21:05 +00001905 size_t buflen = 0;
Jerry Yu4ca91402022-05-09 15:50:57 +08001906 int parse_client_hello_ret;
1907
Gilles Peskine449bd832023-01-11 14:50:10 +01001908 MBEDTLS_SSL_DEBUG_MSG(2, ("=> parse client hello"));
XiaokangQianed582dd2022-04-13 08:21:05 +00001909
Gilles Peskine449bd832023-01-11 14:50:10 +01001910 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_tls13_fetch_handshake_msg(
1911 ssl, MBEDTLS_SSL_HS_CLIENT_HELLO,
1912 &buf, &buflen));
XiaokangQianed582dd2022-04-13 08:21:05 +00001913
Gilles Peskine449bd832023-01-11 14:50:10 +01001914 MBEDTLS_SSL_PROC_CHK_NEG(ssl_tls13_parse_client_hello(ssl, buf,
1915 buf + buflen));
Ronald Cron5af4c7f2023-03-07 20:46:59 +01001916 parse_client_hello_ret = ret; /* Store positive return value of
1917 * parse_client_hello,
1918 * as negative error codes are handled
Jerry Yuf41553b2022-05-09 22:20:30 +08001919 * by MBEDTLS_SSL_PROC_CHK_NEG. */
Jerry Yu4ca91402022-05-09 15:50:57 +08001920
Ronald Cronb828c7d2023-04-03 16:37:22 +02001921 /*
Yanray Wang90acdc62023-12-08 10:29:42 +08001922 * Version 1.2 of the protocol has to be used for the handshake.
1923 * If TLS 1.2 is not supported, abort the handshake. Otherwise, set the
Ronald Cronb828c7d2023-04-03 16:37:22 +02001924 * ssl->keep_current_message flag for the ClientHello to be kept and parsed
1925 * as a TLS 1.2 ClientHello. We also change ssl->tls_version to
1926 * MBEDTLS_SSL_VERSION_TLS1_2 thus from now on mbedtls_ssl_handshake_step()
1927 * will dispatch to the TLS 1.2 state machine.
1928 */
Ronald Cron5af4c7f2023-03-07 20:46:59 +01001929 if (SSL_CLIENT_HELLO_TLS1_2 == parse_client_hello_ret) {
Yanray Wangfb0f47b2023-12-04 15:27:28 +08001930 /* Check if server supports TLS 1.2 */
Yanray Wang408ba6f2023-12-08 10:18:03 +08001931 if (!mbedtls_ssl_conf_is_tls12_enabled(ssl->conf)) {
Yanray Wangfb0f47b2023-12-04 15:27:28 +08001932 MBEDTLS_SSL_DEBUG_MSG(
Yanray Wang177e49a2023-12-08 10:51:04 +08001933 1, ("TLS 1.2 not supported."));
Yanray Wangfb0f47b2023-12-04 15:27:28 +08001934 MBEDTLS_SSL_PEND_FATAL_ALERT(
Yanray Wang2bef9172023-12-08 10:21:53 +08001935 MBEDTLS_SSL_ALERT_MSG_PROTOCOL_VERSION,
1936 MBEDTLS_ERR_SSL_BAD_PROTOCOL_VERSION);
1937 return MBEDTLS_ERR_SSL_BAD_PROTOCOL_VERSION;
Yanray Wangfb0f47b2023-12-04 15:27:28 +08001938 }
Ronald Cron5af4c7f2023-03-07 20:46:59 +01001939 ssl->keep_current_message = 1;
1940 ssl->tls_version = MBEDTLS_SSL_VERSION_TLS1_2;
1941 return 0;
1942 }
1943
Ronald Cron7b6ee942024-01-12 10:29:55 +01001944 MBEDTLS_SSL_PROC_CHK(
1945 ssl_tls13_postprocess_client_hello(ssl, parse_client_hello_ret ==
1946 SSL_CLIENT_HELLO_HRR_REQUIRED));
Jerry Yu582dd062022-04-22 21:59:01 +08001947
Ronald Cron5af4c7f2023-03-07 20:46:59 +01001948 if (SSL_CLIENT_HELLO_OK == parse_client_hello_ret) {
Gilles Peskine449bd832023-01-11 14:50:10 +01001949 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_SERVER_HELLO);
1950 } else {
1951 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_HELLO_RETRY_REQUEST);
1952 }
XiaokangQianed582dd2022-04-13 08:21:05 +00001953
1954cleanup:
1955
Gilles Peskine449bd832023-01-11 14:50:10 +01001956 MBEDTLS_SSL_DEBUG_MSG(2, ("<= parse client hello"));
1957 return ret;
XiaokangQianed582dd2022-04-13 08:21:05 +00001958}
1959
1960/*
Jerry Yu1c3e6882022-04-20 21:23:40 +08001961 * Handler for MBEDTLS_SSL_SERVER_HELLO
Jerry Yu5b64ae92022-03-30 17:15:02 +08001962 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001963MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01001964static int ssl_tls13_prepare_server_hello(mbedtls_ssl_context *ssl)
Jerry Yuf4b27e42022-03-30 17:32:21 +08001965{
Jerry Yu637a3f12022-04-20 21:37:58 +08001966 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1967 unsigned char *server_randbytes =
Gilles Peskine449bd832023-01-11 14:50:10 +01001968 ssl->handshake->randbytes + MBEDTLS_CLIENT_HELLO_RANDOM_LEN;
1969 if (ssl->conf->f_rng == NULL) {
1970 MBEDTLS_SSL_DEBUG_MSG(1, ("no RNG provided"));
1971 return MBEDTLS_ERR_SSL_NO_RNG;
Jerry Yuf4b27e42022-03-30 17:32:21 +08001972 }
1973
Gilles Peskine449bd832023-01-11 14:50:10 +01001974 if ((ret = ssl->conf->f_rng(ssl->conf->p_rng, server_randbytes,
1975 MBEDTLS_SERVER_HELLO_RANDOM_LEN)) != 0) {
1976 MBEDTLS_SSL_DEBUG_RET(1, "f_rng", ret);
1977 return ret;
Jerry Yuf4b27e42022-03-30 17:32:21 +08001978 }
1979
Gilles Peskine449bd832023-01-11 14:50:10 +01001980 MBEDTLS_SSL_DEBUG_BUF(3, "server hello, random bytes", server_randbytes,
1981 MBEDTLS_SERVER_HELLO_RANDOM_LEN);
Jerry Yuf4b27e42022-03-30 17:32:21 +08001982
1983#if defined(MBEDTLS_HAVE_TIME)
YxCda609132023-05-22 12:08:12 -07001984 ssl->session_negotiate->start = mbedtls_time(NULL);
Jerry Yuf4b27e42022-03-30 17:32:21 +08001985#endif /* MBEDTLS_HAVE_TIME */
1986
Gilles Peskine449bd832023-01-11 14:50:10 +01001987 return ret;
Jerry Yuf4b27e42022-03-30 17:32:21 +08001988}
1989
Jerry Yu3bf2c642022-03-30 22:02:12 +08001990/*
Jerry Yue74e04a2022-04-21 09:23:16 +08001991 * ssl_tls13_write_server_hello_supported_versions_ext ():
Jerry Yu3bf2c642022-03-30 22:02:12 +08001992 *
1993 * struct {
Jerry Yufb9f54d2022-04-06 10:08:34 +08001994 * ProtocolVersion selected_version;
Jerry Yu3bf2c642022-03-30 22:02:12 +08001995 * } SupportedVersions;
1996 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001997MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yue74e04a2022-04-21 09:23:16 +08001998static int ssl_tls13_write_server_hello_supported_versions_ext(
Gilles Peskine449bd832023-01-11 14:50:10 +01001999 mbedtls_ssl_context *ssl,
2000 unsigned char *buf,
2001 unsigned char *end,
2002 size_t *out_len)
Jerry Yu3bf2c642022-03-30 22:02:12 +08002003{
Jerry Yu3bf2c642022-03-30 22:02:12 +08002004 *out_len = 0;
2005
Gilles Peskine449bd832023-01-11 14:50:10 +01002006 MBEDTLS_SSL_DEBUG_MSG(3, ("server hello, write selected version"));
Jerry Yu3bf2c642022-03-30 22:02:12 +08002007
2008 /* Check if we have space to write the extension:
2009 * - extension_type (2 bytes)
2010 * - extension_data_length (2 bytes)
Jerry Yu349a6132022-04-14 20:52:56 +08002011 * - selected_version (2 bytes)
Jerry Yu3bf2c642022-03-30 22:02:12 +08002012 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002013 MBEDTLS_SSL_CHK_BUF_PTR(buf, end, 6);
Jerry Yu3bf2c642022-03-30 22:02:12 +08002014
Gilles Peskine449bd832023-01-11 14:50:10 +01002015 MBEDTLS_PUT_UINT16_BE(MBEDTLS_TLS_EXT_SUPPORTED_VERSIONS, buf, 0);
Jerry Yu3bf2c642022-03-30 22:02:12 +08002016
Gilles Peskine449bd832023-01-11 14:50:10 +01002017 MBEDTLS_PUT_UINT16_BE(2, buf, 2);
Jerry Yu3bf2c642022-03-30 22:02:12 +08002018
Gilles Peskine449bd832023-01-11 14:50:10 +01002019 mbedtls_ssl_write_version(buf + 4,
2020 ssl->conf->transport,
2021 ssl->tls_version);
Jerry Yu3bf2c642022-03-30 22:02:12 +08002022
Gilles Peskine449bd832023-01-11 14:50:10 +01002023 MBEDTLS_SSL_DEBUG_MSG(3, ("supported version: [%04x]",
2024 ssl->tls_version));
Jerry Yu3bf2c642022-03-30 22:02:12 +08002025
Jerry Yu349a6132022-04-14 20:52:56 +08002026 *out_len = 6;
Jerry Yu3bf2c642022-03-30 22:02:12 +08002027
Jerry Yub95dd362022-11-08 21:19:34 +08002028 mbedtls_ssl_tls13_set_hs_sent_ext_mask(
Gilles Peskine449bd832023-01-11 14:50:10 +01002029 ssl, MBEDTLS_TLS_EXT_SUPPORTED_VERSIONS);
Jerry Yub95dd362022-11-08 21:19:34 +08002030
Gilles Peskine449bd832023-01-11 14:50:10 +01002031 return 0;
Jerry Yu3bf2c642022-03-30 22:02:12 +08002032}
2033
Jerry Yud9436a12022-04-20 22:28:09 +08002034
Jerry Yu3bf2c642022-03-30 22:02:12 +08002035
2036/* Generate and export a single key share. For hybrid KEMs, this can
2037 * be called multiple times with the different components of the hybrid. */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002038MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002039static int ssl_tls13_generate_and_write_key_share(mbedtls_ssl_context *ssl,
2040 uint16_t named_group,
2041 unsigned char *buf,
2042 unsigned char *end,
2043 size_t *out_len)
Jerry Yu3bf2c642022-03-30 22:02:12 +08002044{
2045 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yu955ddd72022-04-22 22:27:33 +08002046
2047 *out_len = 0;
2048
Valerio Settic9ae8622023-07-25 11:23:50 +02002049#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_EPHEMERAL_ENABLED)
Przemek Stekiel29c219c2023-05-31 15:21:04 +02002050 if (mbedtls_ssl_tls13_named_group_is_ecdhe(named_group) ||
Przemek Stekield5f79e72023-06-29 09:08:43 +02002051 mbedtls_ssl_tls13_named_group_is_ffdh(named_group)) {
Przemek Stekiel408569f2023-07-06 11:26:44 +02002052 ret = mbedtls_ssl_tls13_generate_and_write_xxdh_key_exchange(
Gilles Peskine449bd832023-01-11 14:50:10 +01002053 ssl, named_group, buf, end, out_len);
2054 if (ret != 0) {
Jerry Yu89e103c2022-03-30 22:43:29 +08002055 MBEDTLS_SSL_DEBUG_RET(
Przemek Stekiel408569f2023-07-06 11:26:44 +02002056 1, "mbedtls_ssl_tls13_generate_and_write_xxdh_key_exchange",
Gilles Peskine449bd832023-01-11 14:50:10 +01002057 ret);
2058 return ret;
Jerry Yu3bf2c642022-03-30 22:02:12 +08002059 }
Gilles Peskine449bd832023-01-11 14:50:10 +01002060 } else
Valerio Settic9ae8622023-07-25 11:23:50 +02002061#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_EPHEMERAL_ENABLED */
Gilles Peskine449bd832023-01-11 14:50:10 +01002062 if (0 /* Other kinds of KEMs */) {
2063 } else {
Jerry Yu955ddd72022-04-22 22:27:33 +08002064 ((void) ssl);
2065 ((void) named_group);
2066 ((void) buf);
2067 ((void) end);
Jerry Yu3bf2c642022-03-30 22:02:12 +08002068 ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
2069 }
2070
Gilles Peskine449bd832023-01-11 14:50:10 +01002071 return ret;
Jerry Yu3bf2c642022-03-30 22:02:12 +08002072}
2073
2074/*
2075 * ssl_tls13_write_key_share_ext
2076 *
2077 * Structure of key_share extension in ServerHello:
2078 *
Jerry Yu1c3e6882022-04-20 21:23:40 +08002079 * struct {
2080 * NamedGroup group;
2081 * opaque key_exchange<1..2^16-1>;
2082 * } KeyShareEntry;
2083 * struct {
2084 * KeyShareEntry server_share;
2085 * } KeyShareServerHello;
Jerry Yu3bf2c642022-03-30 22:02:12 +08002086 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002087MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002088static int ssl_tls13_write_key_share_ext(mbedtls_ssl_context *ssl,
2089 unsigned char *buf,
2090 unsigned char *end,
2091 size_t *out_len)
Jerry Yu3bf2c642022-03-30 22:02:12 +08002092{
Jerry Yu955ddd72022-04-22 22:27:33 +08002093 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yu3bf2c642022-03-30 22:02:12 +08002094 unsigned char *p = buf;
Jerry Yu57d48412022-04-20 21:50:42 +08002095 uint16_t group = ssl->handshake->offered_group_id;
Jerry Yu3bf2c642022-03-30 22:02:12 +08002096 unsigned char *server_share = buf + 4;
Jerry Yu3bf2c642022-03-30 22:02:12 +08002097 size_t key_exchange_length;
Jerry Yu3bf2c642022-03-30 22:02:12 +08002098
2099 *out_len = 0;
2100
Gilles Peskine449bd832023-01-11 14:50:10 +01002101 MBEDTLS_SSL_DEBUG_MSG(3, ("server hello, adding key share extension"));
Jerry Yu3bf2c642022-03-30 22:02:12 +08002102
Gilles Peskine449bd832023-01-11 14:50:10 +01002103 MBEDTLS_SSL_DEBUG_MSG(2, ("server hello, write selected_group: %s (%04x)",
2104 mbedtls_ssl_named_group_to_str(group),
2105 group));
Jerry Yu58af2332022-09-06 11:19:31 +08002106
Jerry Yu3bf2c642022-03-30 22:02:12 +08002107 /* Check if we have space for header and length fields:
2108 * - extension_type (2 bytes)
2109 * - extension_data_length (2 bytes)
2110 * - group (2 bytes)
2111 * - key_exchange_length (2 bytes)
2112 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002113 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 8);
2114 MBEDTLS_PUT_UINT16_BE(MBEDTLS_TLS_EXT_KEY_SHARE, p, 0);
2115 MBEDTLS_PUT_UINT16_BE(group, server_share, 0);
Jerry Yu3bf2c642022-03-30 22:02:12 +08002116 p += 8;
Jerry Yu57d48412022-04-20 21:50:42 +08002117
Jerry Yu3bf2c642022-03-30 22:02:12 +08002118 /* When we introduce PQC-ECDHE hybrids, we'll want to call this
2119 * function multiple times. */
Jerry Yu955ddd72022-04-22 22:27:33 +08002120 ret = ssl_tls13_generate_and_write_key_share(
Gilles Peskine449bd832023-01-11 14:50:10 +01002121 ssl, group, server_share + 4, end, &key_exchange_length);
2122 if (ret != 0) {
2123 return ret;
2124 }
Jerry Yu3bf2c642022-03-30 22:02:12 +08002125 p += key_exchange_length;
Jerry Yuc1be19f2022-04-23 16:11:39 +08002126
Gilles Peskine449bd832023-01-11 14:50:10 +01002127 MBEDTLS_PUT_UINT16_BE(key_exchange_length, server_share + 2, 0);
Jerry Yu3bf2c642022-03-30 22:02:12 +08002128
Gilles Peskine449bd832023-01-11 14:50:10 +01002129 MBEDTLS_PUT_UINT16_BE(p - server_share, buf, 2);
Jerry Yu3bf2c642022-03-30 22:02:12 +08002130
Jerry Yu57d48412022-04-20 21:50:42 +08002131 *out_len = p - buf;
Jerry Yu955ddd72022-04-22 22:27:33 +08002132
Gilles Peskine449bd832023-01-11 14:50:10 +01002133 mbedtls_ssl_tls13_set_hs_sent_ext_mask(ssl, MBEDTLS_TLS_EXT_KEY_SHARE);
Jerry Yub95dd362022-11-08 21:19:34 +08002134
Gilles Peskine449bd832023-01-11 14:50:10 +01002135 return 0;
Jerry Yu3bf2c642022-03-30 22:02:12 +08002136}
Jerry Yud9436a12022-04-20 22:28:09 +08002137
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002138MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002139static int ssl_tls13_write_hrr_key_share_ext(mbedtls_ssl_context *ssl,
2140 unsigned char *buf,
2141 unsigned char *end,
2142 size_t *out_len)
Jerry Yu23f7a6f2022-04-23 15:16:45 +08002143{
2144 uint16_t selected_group = ssl->handshake->hrr_selected_group;
2145 /* key_share Extension
2146 *
2147 * struct {
2148 * select (Handshake.msg_type) {
2149 * ...
2150 * case hello_retry_request:
2151 * NamedGroup selected_group;
2152 * ...
2153 * };
2154 * } KeyShare;
2155 */
2156
2157 *out_len = 0;
2158
Jerry Yub0ac10b2022-05-05 11:10:08 +08002159 /*
2160 * For a pure PSK key exchange, there is no group to agree upon. The purpose
2161 * of the HRR is then to transmit a cookie to force the client to demonstrate
2162 * reachability at their apparent network address (primarily useful for DTLS).
2163 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002164 if (!mbedtls_ssl_tls13_key_exchange_mode_with_ephemeral(ssl)) {
2165 return 0;
2166 }
Jerry Yu23f7a6f2022-04-23 15:16:45 +08002167
2168 /* We should only send the key_share extension if the client's initial
2169 * key share was not acceptable. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002170 if (ssl->handshake->offered_group_id != 0) {
2171 MBEDTLS_SSL_DEBUG_MSG(4, ("Skip key_share extension in HRR"));
2172 return 0;
Jerry Yu23f7a6f2022-04-23 15:16:45 +08002173 }
2174
Gilles Peskine449bd832023-01-11 14:50:10 +01002175 if (selected_group == 0) {
2176 MBEDTLS_SSL_DEBUG_MSG(1, ("no matching named group found"));
2177 return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
Jerry Yu23f7a6f2022-04-23 15:16:45 +08002178 }
2179
Jerry Yub0ac10b2022-05-05 11:10:08 +08002180 /* Check if we have enough space:
2181 * - extension_type (2 bytes)
2182 * - extension_data_length (2 bytes)
2183 * - selected_group (2 bytes)
2184 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002185 MBEDTLS_SSL_CHK_BUF_PTR(buf, end, 6);
Jerry Yu23f7a6f2022-04-23 15:16:45 +08002186
Gilles Peskine449bd832023-01-11 14:50:10 +01002187 MBEDTLS_PUT_UINT16_BE(MBEDTLS_TLS_EXT_KEY_SHARE, buf, 0);
2188 MBEDTLS_PUT_UINT16_BE(2, buf, 2);
2189 MBEDTLS_PUT_UINT16_BE(selected_group, buf, 4);
Jerry Yu23f7a6f2022-04-23 15:16:45 +08002190
Gilles Peskine449bd832023-01-11 14:50:10 +01002191 MBEDTLS_SSL_DEBUG_MSG(3,
2192 ("HRR selected_group: %s (%x)",
2193 mbedtls_ssl_named_group_to_str(selected_group),
2194 selected_group));
Jerry Yu23f7a6f2022-04-23 15:16:45 +08002195
2196 *out_len = 6;
Jerry Yu23f7a6f2022-04-23 15:16:45 +08002197
Gilles Peskine449bd832023-01-11 14:50:10 +01002198 mbedtls_ssl_tls13_set_hs_sent_ext_mask(ssl, MBEDTLS_TLS_EXT_KEY_SHARE);
Jerry Yub95dd362022-11-08 21:19:34 +08002199
Gilles Peskine449bd832023-01-11 14:50:10 +01002200 return 0;
Jerry Yu23f7a6f2022-04-23 15:16:45 +08002201}
Jerry Yu3bf2c642022-03-30 22:02:12 +08002202
2203/*
2204 * Structure of ServerHello message:
2205 *
2206 * struct {
2207 * ProtocolVersion legacy_version = 0x0303; // TLS v1.2
2208 * Random random;
2209 * opaque legacy_session_id_echo<0..32>;
2210 * CipherSuite cipher_suite;
2211 * uint8 legacy_compression_method = 0;
2212 * Extension extensions<6..2^16-1>;
2213 * } ServerHello;
2214 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002215MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002216static int ssl_tls13_write_server_hello_body(mbedtls_ssl_context *ssl,
2217 unsigned char *buf,
2218 unsigned char *end,
2219 size_t *out_len,
2220 int is_hrr)
Jerry Yu56404d72022-03-30 17:36:13 +08002221{
Jerry Yu955ddd72022-04-22 22:27:33 +08002222 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yu3bf2c642022-03-30 22:02:12 +08002223 unsigned char *p = buf;
Jerry Yud9436a12022-04-20 22:28:09 +08002224 unsigned char *p_extensions_len;
Jerry Yufbe3e642022-04-25 19:31:51 +08002225 size_t output_len;
Jerry Yu3bf2c642022-03-30 22:02:12 +08002226
2227 *out_len = 0;
Jerry Yu50e00e32022-10-31 14:45:01 +08002228 ssl->handshake->sent_extensions = MBEDTLS_SSL_EXT_MASK_NONE;
Jerry Yu3bf2c642022-03-30 22:02:12 +08002229
Jerry Yucfc04b32022-04-21 09:31:58 +08002230 /* ...
2231 * ProtocolVersion legacy_version = 0x0303; // TLS 1.2
2232 * ...
2233 * with ProtocolVersion defined as:
2234 * uint16 ProtocolVersion;
Jerry Yu3bf2c642022-03-30 22:02:12 +08002235 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002236 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 2);
2237 MBEDTLS_PUT_UINT16_BE(0x0303, p, 0);
Jerry Yu3bf2c642022-03-30 22:02:12 +08002238 p += 2;
2239
Jerry Yu1c3e6882022-04-20 21:23:40 +08002240 /* ...
2241 * Random random;
2242 * ...
Jerry Yucfc04b32022-04-21 09:31:58 +08002243 * with Random defined as:
2244 * opaque Random[MBEDTLS_SERVER_HELLO_RANDOM_LEN];
Jerry Yu1c3e6882022-04-20 21:23:40 +08002245 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002246 MBEDTLS_SSL_CHK_BUF_PTR(p, end, MBEDTLS_SERVER_HELLO_RANDOM_LEN);
2247 if (is_hrr) {
2248 memcpy(p, mbedtls_ssl_tls13_hello_retry_request_magic,
2249 MBEDTLS_SERVER_HELLO_RANDOM_LEN);
2250 } else {
2251 memcpy(p, &ssl->handshake->randbytes[MBEDTLS_CLIENT_HELLO_RANDOM_LEN],
2252 MBEDTLS_SERVER_HELLO_RANDOM_LEN);
Jerry Yu23f7a6f2022-04-23 15:16:45 +08002253 }
Gilles Peskine449bd832023-01-11 14:50:10 +01002254 MBEDTLS_SSL_DEBUG_BUF(3, "server hello, random bytes",
2255 p, MBEDTLS_SERVER_HELLO_RANDOM_LEN);
Jerry Yu3bf2c642022-03-30 22:02:12 +08002256 p += MBEDTLS_SERVER_HELLO_RANDOM_LEN;
2257
Jerry Yucfc04b32022-04-21 09:31:58 +08002258 /* ...
2259 * opaque legacy_session_id_echo<0..32>;
2260 * ...
Jerry Yu3bf2c642022-03-30 22:02:12 +08002261 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002262 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 1 + ssl->session_negotiate->id_len);
2263 *p++ = (unsigned char) ssl->session_negotiate->id_len;
2264 if (ssl->session_negotiate->id_len > 0) {
2265 memcpy(p, &ssl->session_negotiate->id[0],
2266 ssl->session_negotiate->id_len);
Jerry Yu3bf2c642022-03-30 22:02:12 +08002267 p += ssl->session_negotiate->id_len;
Jerry Yu955ddd72022-04-22 22:27:33 +08002268
Gilles Peskine449bd832023-01-11 14:50:10 +01002269 MBEDTLS_SSL_DEBUG_BUF(3, "session id", ssl->session_negotiate->id,
2270 ssl->session_negotiate->id_len);
Jerry Yu3bf2c642022-03-30 22:02:12 +08002271 }
2272
Jerry Yucfc04b32022-04-21 09:31:58 +08002273 /* ...
2274 * CipherSuite cipher_suite;
2275 * ...
2276 * with CipherSuite defined as:
2277 * uint8 CipherSuite[2];
Jerry Yu3bf2c642022-03-30 22:02:12 +08002278 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002279 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 2);
2280 MBEDTLS_PUT_UINT16_BE(ssl->session_negotiate->ciphersuite, p, 0);
Jerry Yu3bf2c642022-03-30 22:02:12 +08002281 p += 2;
Gilles Peskine449bd832023-01-11 14:50:10 +01002282 MBEDTLS_SSL_DEBUG_MSG(3,
2283 ("server hello, chosen ciphersuite: %s ( id=%d )",
2284 mbedtls_ssl_get_ciphersuite_name(
2285 ssl->session_negotiate->ciphersuite),
2286 ssl->session_negotiate->ciphersuite));
Jerry Yu3bf2c642022-03-30 22:02:12 +08002287
Jerry Yucfc04b32022-04-21 09:31:58 +08002288 /* ...
2289 * uint8 legacy_compression_method = 0;
2290 * ...
2291 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002292 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 1);
Thomas Daubney31e03a82022-07-25 15:59:25 +01002293 *p++ = MBEDTLS_SSL_COMPRESS_NULL;
Jerry Yu3bf2c642022-03-30 22:02:12 +08002294
Jerry Yucfc04b32022-04-21 09:31:58 +08002295 /* ...
2296 * Extension extensions<6..2^16-1>;
2297 * ...
2298 * struct {
2299 * ExtensionType extension_type; (2 bytes)
2300 * opaque extension_data<0..2^16-1>;
2301 * } Extension;
2302 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002303 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 2);
Jerry Yud9436a12022-04-20 22:28:09 +08002304 p_extensions_len = p;
Jerry Yu3bf2c642022-03-30 22:02:12 +08002305 p += 2;
2306
Gilles Peskine449bd832023-01-11 14:50:10 +01002307 if ((ret = ssl_tls13_write_server_hello_supported_versions_ext(
2308 ssl, p, end, &output_len)) != 0) {
Jerry Yu955ddd72022-04-22 22:27:33 +08002309 MBEDTLS_SSL_DEBUG_RET(
Gilles Peskine449bd832023-01-11 14:50:10 +01002310 1, "ssl_tls13_write_server_hello_supported_versions_ext", ret);
2311 return ret;
Jerry Yu3bf2c642022-03-30 22:02:12 +08002312 }
2313 p += output_len;
2314
Gilles Peskine449bd832023-01-11 14:50:10 +01002315 if (mbedtls_ssl_tls13_key_exchange_mode_with_ephemeral(ssl)) {
2316 if (is_hrr) {
2317 ret = ssl_tls13_write_hrr_key_share_ext(ssl, p, end, &output_len);
2318 } else {
2319 ret = ssl_tls13_write_key_share_ext(ssl, p, end, &output_len);
2320 }
2321 if (ret != 0) {
2322 return ret;
2323 }
Jerry Yu3bf2c642022-03-30 22:02:12 +08002324 p += output_len;
2325 }
Jerry Yu3bf2c642022-03-30 22:02:12 +08002326
Ronald Cron41a443a2022-10-04 16:38:25 +02002327#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01002328 if (!is_hrr && mbedtls_ssl_tls13_key_exchange_mode_with_psk(ssl)) {
2329 ret = ssl_tls13_write_server_pre_shared_key_ext(ssl, p, end, &output_len);
2330 if (ret != 0) {
2331 MBEDTLS_SSL_DEBUG_RET(1, "ssl_tls13_write_server_pre_shared_key_ext",
2332 ret);
2333 return ret;
Jerry Yu032b15ce2022-07-11 06:10:03 +00002334 }
2335 p += output_len;
2336 }
Ronald Cron41a443a2022-10-04 16:38:25 +02002337#endif
Jerry Yu032b15ce2022-07-11 06:10:03 +00002338
Gilles Peskine449bd832023-01-11 14:50:10 +01002339 MBEDTLS_PUT_UINT16_BE(p - p_extensions_len - 2, p_extensions_len, 0);
Jerry Yu3bf2c642022-03-30 22:02:12 +08002340
Gilles Peskine449bd832023-01-11 14:50:10 +01002341 MBEDTLS_SSL_DEBUG_BUF(4, "server hello extensions",
2342 p_extensions_len, p - p_extensions_len);
Jerry Yu3bf2c642022-03-30 22:02:12 +08002343
Jerry Yud9436a12022-04-20 22:28:09 +08002344 *out_len = p - buf;
Jerry Yu3bf2c642022-03-30 22:02:12 +08002345
Gilles Peskine449bd832023-01-11 14:50:10 +01002346 MBEDTLS_SSL_DEBUG_BUF(3, "server hello", buf, *out_len);
Jerry Yu3bf2c642022-03-30 22:02:12 +08002347
Jerry Yu7de2ff02022-11-08 21:43:46 +08002348 MBEDTLS_SSL_PRINT_EXTS(
Jerry Yu4b8f2f72022-10-31 13:31:22 +08002349 3, is_hrr ? MBEDTLS_SSL_TLS1_3_HS_HELLO_RETRY_REQUEST :
Gilles Peskine449bd832023-01-11 14:50:10 +01002350 MBEDTLS_SSL_HS_SERVER_HELLO,
2351 ssl->handshake->sent_extensions);
Jerry Yu4b8f2f72022-10-31 13:31:22 +08002352
Gilles Peskine449bd832023-01-11 14:50:10 +01002353 return ret;
Jerry Yu56404d72022-03-30 17:36:13 +08002354}
2355
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002356MBEDTLS_CHECK_RETURN_CRITICAL
Xiaokang Qian934ce6f2023-02-06 10:23:04 +00002357static int ssl_tls13_finalize_server_hello(mbedtls_ssl_context *ssl)
Jerry Yue110d252022-05-05 10:19:22 +08002358{
2359 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine449bd832023-01-11 14:50:10 +01002360 ret = mbedtls_ssl_tls13_compute_handshake_transform(ssl);
2361 if (ret != 0) {
2362 MBEDTLS_SSL_DEBUG_RET(1,
2363 "mbedtls_ssl_tls13_compute_handshake_transform",
2364 ret);
2365 return ret;
Jerry Yue110d252022-05-05 10:19:22 +08002366 }
2367
Gilles Peskine449bd832023-01-11 14:50:10 +01002368 return ret;
Jerry Yue110d252022-05-05 10:19:22 +08002369}
2370
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002371MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002372static int ssl_tls13_write_server_hello(mbedtls_ssl_context *ssl)
Jerry Yu5b64ae92022-03-30 17:15:02 +08002373{
Jerry Yu637a3f12022-04-20 21:37:58 +08002374 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yuf4b27e42022-03-30 17:32:21 +08002375 unsigned char *buf;
2376 size_t buf_len, msg_len;
2377
Gilles Peskine449bd832023-01-11 14:50:10 +01002378 MBEDTLS_SSL_DEBUG_MSG(2, ("=> write server hello"));
Jerry Yuf4b27e42022-03-30 17:32:21 +08002379
Gilles Peskine449bd832023-01-11 14:50:10 +01002380 MBEDTLS_SSL_PROC_CHK(ssl_tls13_prepare_server_hello(ssl));
Jerry Yuf4b27e42022-03-30 17:32:21 +08002381
Xiaokang Qian9f1747b2023-03-30 02:50:04 +00002382 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_start_handshake_msg(
2383 ssl, MBEDTLS_SSL_HS_SERVER_HELLO, &buf, &buf_len));
Jerry Yuf4b27e42022-03-30 17:32:21 +08002384
Gilles Peskine449bd832023-01-11 14:50:10 +01002385 MBEDTLS_SSL_PROC_CHK(ssl_tls13_write_server_hello_body(ssl, buf,
2386 buf + buf_len,
2387 &msg_len,
2388 0));
Jerry Yuf4b27e42022-03-30 17:32:21 +08002389
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +01002390 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_add_hs_msg_to_checksum(
Manuel Pégourié-Gonnard43cc1272023-02-06 11:48:19 +01002391 ssl, MBEDTLS_SSL_HS_SERVER_HELLO, buf, msg_len));
Jerry Yuf4b27e42022-03-30 17:32:21 +08002392
Gilles Peskine449bd832023-01-11 14:50:10 +01002393 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_finish_handshake_msg(
2394 ssl, buf_len, msg_len));
Jerry Yu637a3f12022-04-20 21:37:58 +08002395
Xiaokang Qian934ce6f2023-02-06 10:23:04 +00002396 MBEDTLS_SSL_PROC_CHK(ssl_tls13_finalize_server_hello(ssl));
Jerry Yue110d252022-05-05 10:19:22 +08002397
Gabor Mezei7b39bf12022-05-24 16:04:14 +02002398#if defined(MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE)
2399 /* The server sends a dummy change_cipher_spec record immediately
2400 * after its first handshake message. This may either be after
2401 * a ServerHello or a HelloRetryRequest.
2402 */
Gabor Mezei96ae9262022-06-28 11:45:18 +02002403 mbedtls_ssl_handshake_set_state(
Gilles Peskine449bd832023-01-11 14:50:10 +01002404 ssl, MBEDTLS_SSL_SERVER_CCS_AFTER_SERVER_HELLO);
Gabor Mezei7b39bf12022-05-24 16:04:14 +02002405#else
Gilles Peskine449bd832023-01-11 14:50:10 +01002406 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_ENCRYPTED_EXTENSIONS);
Gabor Mezei7b39bf12022-05-24 16:04:14 +02002407#endif /* MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE */
Jerry Yue110d252022-05-05 10:19:22 +08002408
Jerry Yuf4b27e42022-03-30 17:32:21 +08002409cleanup:
2410
Gilles Peskine449bd832023-01-11 14:50:10 +01002411 MBEDTLS_SSL_DEBUG_MSG(2, ("<= write server hello"));
2412 return ret;
Jerry Yu5b64ae92022-03-30 17:15:02 +08002413}
2414
Jerry Yu23d1a252022-05-12 18:08:59 +08002415
2416/*
2417 * Handler for MBEDTLS_SSL_HELLO_RETRY_REQUEST
2418 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002419MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002420static int ssl_tls13_prepare_hello_retry_request(mbedtls_ssl_context *ssl)
Jerry Yu23d1a252022-05-12 18:08:59 +08002421{
2422 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Ronald Cron5fbd2702024-02-14 10:03:36 +01002423 if (ssl->handshake->hello_retry_request_flag) {
Gilles Peskine449bd832023-01-11 14:50:10 +01002424 MBEDTLS_SSL_DEBUG_MSG(1, ("Too many HRRs"));
2425 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE,
2426 MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE);
2427 return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
Jerry Yu23d1a252022-05-12 18:08:59 +08002428 }
2429
2430 /*
2431 * Create stateless transcript hash for HRR
2432 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002433 MBEDTLS_SSL_DEBUG_MSG(4, ("Reset transcript for HRR"));
2434 ret = mbedtls_ssl_reset_transcript_for_hrr(ssl);
2435 if (ret != 0) {
2436 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_reset_transcript_for_hrr", ret);
2437 return ret;
Jerry Yu23d1a252022-05-12 18:08:59 +08002438 }
Gilles Peskine449bd832023-01-11 14:50:10 +01002439 mbedtls_ssl_session_reset_msg_layer(ssl, 0);
Jerry Yu23d1a252022-05-12 18:08:59 +08002440
Gilles Peskine449bd832023-01-11 14:50:10 +01002441 return 0;
Jerry Yu23d1a252022-05-12 18:08:59 +08002442}
2443
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002444MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002445static int ssl_tls13_write_hello_retry_request(mbedtls_ssl_context *ssl)
Jerry Yu23d1a252022-05-12 18:08:59 +08002446{
2447 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
2448 unsigned char *buf;
2449 size_t buf_len, msg_len;
2450
Gilles Peskine449bd832023-01-11 14:50:10 +01002451 MBEDTLS_SSL_DEBUG_MSG(2, ("=> write hello retry request"));
Jerry Yu23d1a252022-05-12 18:08:59 +08002452
Gilles Peskine449bd832023-01-11 14:50:10 +01002453 MBEDTLS_SSL_PROC_CHK(ssl_tls13_prepare_hello_retry_request(ssl));
Jerry Yu23d1a252022-05-12 18:08:59 +08002454
Gilles Peskine449bd832023-01-11 14:50:10 +01002455 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_start_handshake_msg(
2456 ssl, MBEDTLS_SSL_HS_SERVER_HELLO,
2457 &buf, &buf_len));
Jerry Yu23d1a252022-05-12 18:08:59 +08002458
Gilles Peskine449bd832023-01-11 14:50:10 +01002459 MBEDTLS_SSL_PROC_CHK(ssl_tls13_write_server_hello_body(ssl, buf,
2460 buf + buf_len,
2461 &msg_len,
2462 1));
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +01002463 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_add_hs_msg_to_checksum(
Manuel Pégourié-Gonnard43cc1272023-02-06 11:48:19 +01002464 ssl, MBEDTLS_SSL_HS_SERVER_HELLO, buf, msg_len));
Jerry Yu23d1a252022-05-12 18:08:59 +08002465
2466
Gilles Peskine449bd832023-01-11 14:50:10 +01002467 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_finish_handshake_msg(ssl, buf_len,
2468 msg_len));
Jerry Yu23d1a252022-05-12 18:08:59 +08002469
Ronald Cron5fbd2702024-02-14 10:03:36 +01002470 ssl->handshake->hello_retry_request_flag = 1;
Jerry Yu23d1a252022-05-12 18:08:59 +08002471
Gabor Mezei7b39bf12022-05-24 16:04:14 +02002472#if defined(MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE)
2473 /* The server sends a dummy change_cipher_spec record immediately
2474 * after its first handshake message. This may either be after
2475 * a ServerHello or a HelloRetryRequest.
2476 */
Gabor Mezei96ae9262022-06-28 11:45:18 +02002477 mbedtls_ssl_handshake_set_state(
Gilles Peskine449bd832023-01-11 14:50:10 +01002478 ssl, MBEDTLS_SSL_SERVER_CCS_AFTER_HELLO_RETRY_REQUEST);
Gabor Mezei7b39bf12022-05-24 16:04:14 +02002479#else
Gilles Peskine449bd832023-01-11 14:50:10 +01002480 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_CLIENT_HELLO);
Gabor Mezei7b39bf12022-05-24 16:04:14 +02002481#endif /* MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE */
Jerry Yu23d1a252022-05-12 18:08:59 +08002482
2483cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +01002484 MBEDTLS_SSL_DEBUG_MSG(2, ("<= write hello retry request"));
2485 return ret;
Jerry Yu23d1a252022-05-12 18:08:59 +08002486}
2487
Jerry Yu5b64ae92022-03-30 17:15:02 +08002488/*
Jerry Yu4d3841a2022-04-16 12:37:19 +08002489 * Handler for MBEDTLS_SSL_ENCRYPTED_EXTENSIONS
2490 */
Jerry Yu4d3841a2022-04-16 12:37:19 +08002491
2492/*
2493 * struct {
2494 * Extension extensions<0..2 ^ 16 - 1>;
2495 * } EncryptedExtensions;
2496 *
2497 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002498MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002499static int ssl_tls13_write_encrypted_extensions_body(mbedtls_ssl_context *ssl,
2500 unsigned char *buf,
2501 unsigned char *end,
2502 size_t *out_len)
Jerry Yu4d3841a2022-04-16 12:37:19 +08002503{
XiaokangQianacb39922022-06-17 10:18:48 +00002504 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yu4d3841a2022-04-16 12:37:19 +08002505 unsigned char *p = buf;
2506 size_t extensions_len = 0;
Jerry Yu8937eb42022-05-03 12:12:14 +08002507 unsigned char *p_extensions_len;
XiaokangQianacb39922022-06-17 10:18:48 +00002508 size_t output_len;
Jerry Yu9da5e5a2022-05-03 15:46:09 +08002509
Jerry Yu4d3841a2022-04-16 12:37:19 +08002510 *out_len = 0;
2511
Gilles Peskine449bd832023-01-11 14:50:10 +01002512 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 2);
Jerry Yu8937eb42022-05-03 12:12:14 +08002513 p_extensions_len = p;
Jerry Yu4d3841a2022-04-16 12:37:19 +08002514 p += 2;
2515
Jerry Yu8937eb42022-05-03 12:12:14 +08002516 ((void) ssl);
XiaokangQianacb39922022-06-17 10:18:48 +00002517 ((void) ret);
2518 ((void) output_len);
2519
2520#if defined(MBEDTLS_SSL_ALPN)
Gilles Peskine449bd832023-01-11 14:50:10 +01002521 ret = mbedtls_ssl_write_alpn_ext(ssl, p, end, &output_len);
2522 if (ret != 0) {
2523 return ret;
2524 }
XiaokangQian95d5f542022-06-24 02:29:26 +00002525 p += output_len;
XiaokangQianacb39922022-06-17 10:18:48 +00002526#endif /* MBEDTLS_SSL_ALPN */
Jerry Yu4d3841a2022-04-16 12:37:19 +08002527
Jerry Yu71c14f12022-12-12 11:10:35 +08002528#if defined(MBEDTLS_SSL_EARLY_DATA)
Ronald Cron78a38f62024-02-01 18:30:31 +01002529 if (ssl->handshake->early_data_accepted) {
Jerry Yu52335392023-11-23 18:06:06 +08002530 ret = mbedtls_ssl_tls13_write_early_data_ext(
Jerry Yuc59c5862023-12-05 10:40:49 +08002531 ssl, 0, p, end, &output_len);
Jerry Yu71c14f12022-12-12 11:10:35 +08002532 if (ret != 0) {
2533 return ret;
2534 }
2535 p += output_len;
2536 }
2537#endif /* MBEDTLS_SSL_EARLY_DATA */
2538
Waleed Elmelegy47d29462024-01-03 17:31:52 +00002539#if defined(MBEDTLS_SSL_RECORD_SIZE_LIMIT)
Waleed Elmelegyfbe42742024-01-05 18:11:10 +00002540 if (ssl->handshake->received_extensions & MBEDTLS_SSL_EXT_MASK(RECORD_SIZE_LIMIT)) {
Waleed Elmelegyd2fc90e2024-01-04 18:04:53 +00002541 ret = mbedtls_ssl_tls13_write_record_size_limit_ext(
2542 ssl, p, end, &output_len);
2543 if (ret != 0) {
2544 return ret;
2545 }
2546 p += output_len;
Waleed Elmelegy47d29462024-01-03 17:31:52 +00002547 }
Waleed Elmelegy47d29462024-01-03 17:31:52 +00002548#endif
2549
Gilles Peskine449bd832023-01-11 14:50:10 +01002550 extensions_len = (p - p_extensions_len) - 2;
2551 MBEDTLS_PUT_UINT16_BE(extensions_len, p_extensions_len, 0);
Jerry Yu8937eb42022-05-03 12:12:14 +08002552
2553 *out_len = p - buf;
Jerry Yu4d3841a2022-04-16 12:37:19 +08002554
Gilles Peskine449bd832023-01-11 14:50:10 +01002555 MBEDTLS_SSL_DEBUG_BUF(4, "encrypted extensions", buf, *out_len);
Jerry Yu4d3841a2022-04-16 12:37:19 +08002556
Jerry Yu7de2ff02022-11-08 21:43:46 +08002557 MBEDTLS_SSL_PRINT_EXTS(
Gilles Peskine449bd832023-01-11 14:50:10 +01002558 3, MBEDTLS_SSL_HS_ENCRYPTED_EXTENSIONS, ssl->handshake->sent_extensions);
Jerry Yu4b8f2f72022-10-31 13:31:22 +08002559
Gilles Peskine449bd832023-01-11 14:50:10 +01002560 return 0;
Jerry Yu4d3841a2022-04-16 12:37:19 +08002561}
2562
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002563MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002564static int ssl_tls13_write_encrypted_extensions(mbedtls_ssl_context *ssl)
Jerry Yu4d3841a2022-04-16 12:37:19 +08002565{
2566 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
2567 unsigned char *buf;
Jerry Yu39730a72022-05-03 12:14:04 +08002568 size_t buf_len, msg_len;
Jerry Yu4d3841a2022-04-16 12:37:19 +08002569
Gilles Peskine449bd832023-01-11 14:50:10 +01002570 mbedtls_ssl_set_outbound_transform(ssl,
2571 ssl->handshake->transform_handshake);
Gabor Mezei54719122022-06-28 11:34:56 +02002572 MBEDTLS_SSL_DEBUG_MSG(
Gilles Peskine449bd832023-01-11 14:50:10 +01002573 3, ("switching to handshake transform for outbound data"));
Gabor Mezei54719122022-06-28 11:34:56 +02002574
Gilles Peskine449bd832023-01-11 14:50:10 +01002575 MBEDTLS_SSL_DEBUG_MSG(2, ("=> write encrypted extensions"));
Jerry Yu4d3841a2022-04-16 12:37:19 +08002576
Xiaokang Qian9f1747b2023-03-30 02:50:04 +00002577 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_start_handshake_msg(
2578 ssl, MBEDTLS_SSL_HS_ENCRYPTED_EXTENSIONS,
2579 &buf, &buf_len));
Jerry Yu4d3841a2022-04-16 12:37:19 +08002580
Gilles Peskine449bd832023-01-11 14:50:10 +01002581 MBEDTLS_SSL_PROC_CHK(ssl_tls13_write_encrypted_extensions_body(
2582 ssl, buf, buf + buf_len, &msg_len));
Jerry Yu4d3841a2022-04-16 12:37:19 +08002583
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +01002584 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_add_hs_msg_to_checksum(
Xiaokang Qian9f1747b2023-03-30 02:50:04 +00002585 ssl, MBEDTLS_SSL_HS_ENCRYPTED_EXTENSIONS,
2586 buf, msg_len));
Jerry Yu4d3841a2022-04-16 12:37:19 +08002587
Gilles Peskine449bd832023-01-11 14:50:10 +01002588 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_finish_handshake_msg(
2589 ssl, buf_len, msg_len));
Jerry Yu4d3841a2022-04-16 12:37:19 +08002590
Ronald Cron928cbd32022-10-04 16:14:26 +02002591#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01002592 if (mbedtls_ssl_tls13_key_exchange_mode_with_psk(ssl)) {
2593 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_SERVER_FINISHED);
2594 } else {
2595 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_CERTIFICATE_REQUEST);
2596 }
Jerry Yu8937eb42022-05-03 12:12:14 +08002597#else
Gilles Peskine449bd832023-01-11 14:50:10 +01002598 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_SERVER_FINISHED);
Jerry Yu8937eb42022-05-03 12:12:14 +08002599#endif
2600
Jerry Yu4d3841a2022-04-16 12:37:19 +08002601cleanup:
2602
Gilles Peskine449bd832023-01-11 14:50:10 +01002603 MBEDTLS_SSL_DEBUG_MSG(2, ("<= write encrypted extensions"));
2604 return ret;
Jerry Yu4d3841a2022-04-16 12:37:19 +08002605}
2606
Ronald Cron928cbd32022-10-04 16:14:26 +02002607#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED)
XiaokangQiana987e1d2022-05-07 01:25:58 +00002608#define SSL_CERTIFICATE_REQUEST_SEND_REQUEST 0
2609#define SSL_CERTIFICATE_REQUEST_SKIP 1
2610/* Coordination:
2611 * Check whether a CertificateRequest message should be written.
2612 * Returns a negative code on failure, or
2613 * - SSL_CERTIFICATE_REQUEST_SEND_REQUEST
2614 * - SSL_CERTIFICATE_REQUEST_SKIP
2615 * indicating if the writing of the CertificateRequest
2616 * should be skipped or not.
2617 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002618MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002619static int ssl_tls13_certificate_request_coordinate(mbedtls_ssl_context *ssl)
XiaokangQiana987e1d2022-05-07 01:25:58 +00002620{
2621 int authmode;
2622
XiaokangQian40a35232022-05-07 09:02:40 +00002623#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Gilles Peskine449bd832023-01-11 14:50:10 +01002624 if (ssl->handshake->sni_authmode != MBEDTLS_SSL_VERIFY_UNSET) {
XiaokangQian40a35232022-05-07 09:02:40 +00002625 authmode = ssl->handshake->sni_authmode;
Gilles Peskine449bd832023-01-11 14:50:10 +01002626 } else
XiaokangQian40a35232022-05-07 09:02:40 +00002627#endif
XiaokangQiana987e1d2022-05-07 01:25:58 +00002628 authmode = ssl->conf->authmode;
2629
Gilles Peskine449bd832023-01-11 14:50:10 +01002630 if (authmode == MBEDTLS_SSL_VERIFY_NONE) {
Ronald Croneac00ad2022-09-13 10:16:31 +02002631 ssl->session_negotiate->verify_result = MBEDTLS_X509_BADCERT_SKIP_VERIFY;
Gilles Peskine449bd832023-01-11 14:50:10 +01002632 return SSL_CERTIFICATE_REQUEST_SKIP;
Ronald Croneac00ad2022-09-13 10:16:31 +02002633 }
XiaokangQiana987e1d2022-05-07 01:25:58 +00002634
XiaokangQianc3017f62022-05-13 05:55:41 +00002635 ssl->handshake->certificate_request_sent = 1;
XiaokangQian189ded22022-05-10 08:12:17 +00002636
Gilles Peskine449bd832023-01-11 14:50:10 +01002637 return SSL_CERTIFICATE_REQUEST_SEND_REQUEST;
XiaokangQiana987e1d2022-05-07 01:25:58 +00002638}
2639
XiaokangQiancec9ae62022-05-06 07:28:50 +00002640/*
2641 * struct {
2642 * opaque certificate_request_context<0..2^8-1>;
2643 * Extension extensions<2..2^16-1>;
2644 * } CertificateRequest;
2645 *
2646 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002647MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002648static int ssl_tls13_write_certificate_request_body(mbedtls_ssl_context *ssl,
2649 unsigned char *buf,
2650 const unsigned char *end,
2651 size_t *out_len)
XiaokangQiancec9ae62022-05-06 07:28:50 +00002652{
2653 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
2654 unsigned char *p = buf;
XiaokangQianec6efb92022-05-06 09:53:10 +00002655 size_t output_len = 0;
XiaokangQiancec9ae62022-05-06 07:28:50 +00002656 unsigned char *p_extensions_len;
2657
2658 *out_len = 0;
2659
2660 /* Check if we have enough space:
2661 * - certificate_request_context (1 byte)
2662 * - extensions length (2 bytes)
2663 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002664 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 3);
XiaokangQiancec9ae62022-05-06 07:28:50 +00002665
2666 /*
2667 * Write certificate_request_context
2668 */
2669 /*
2670 * We use a zero length context for the normal handshake
2671 * messages. For post-authentication handshake messages
2672 * this request context would be set to a non-zero value.
2673 */
2674 *p++ = 0x0;
2675
2676 /*
2677 * Write extensions
2678 */
2679 /* The extensions must contain the signature_algorithms. */
2680 p_extensions_len = p;
2681 p += 2;
Gilles Peskine449bd832023-01-11 14:50:10 +01002682 ret = mbedtls_ssl_write_sig_alg_ext(ssl, p, end, &output_len);
2683 if (ret != 0) {
2684 return ret;
2685 }
XiaokangQiancec9ae62022-05-06 07:28:50 +00002686
XiaokangQianec6efb92022-05-06 09:53:10 +00002687 p += output_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01002688 MBEDTLS_PUT_UINT16_BE(p - p_extensions_len - 2, p_extensions_len, 0);
XiaokangQiancec9ae62022-05-06 07:28:50 +00002689
2690 *out_len = p - buf;
2691
Jerry Yu7de2ff02022-11-08 21:43:46 +08002692 MBEDTLS_SSL_PRINT_EXTS(
Gilles Peskine449bd832023-01-11 14:50:10 +01002693 3, MBEDTLS_SSL_HS_CERTIFICATE_REQUEST, ssl->handshake->sent_extensions);
Jerry Yu4b8f2f72022-10-31 13:31:22 +08002694
Gilles Peskine449bd832023-01-11 14:50:10 +01002695 return 0;
XiaokangQiancec9ae62022-05-06 07:28:50 +00002696}
2697
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002698MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002699static int ssl_tls13_write_certificate_request(mbedtls_ssl_context *ssl)
XiaokangQiancec9ae62022-05-06 07:28:50 +00002700{
2701 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
2702
Gilles Peskine449bd832023-01-11 14:50:10 +01002703 MBEDTLS_SSL_DEBUG_MSG(2, ("=> write certificate request"));
XiaokangQiancec9ae62022-05-06 07:28:50 +00002704
Gilles Peskine449bd832023-01-11 14:50:10 +01002705 MBEDTLS_SSL_PROC_CHK_NEG(ssl_tls13_certificate_request_coordinate(ssl));
XiaokangQiancec9ae62022-05-06 07:28:50 +00002706
Gilles Peskine449bd832023-01-11 14:50:10 +01002707 if (ret == SSL_CERTIFICATE_REQUEST_SEND_REQUEST) {
XiaokangQiancec9ae62022-05-06 07:28:50 +00002708 unsigned char *buf;
2709 size_t buf_len, msg_len;
2710
Xiaokang Qian9f1747b2023-03-30 02:50:04 +00002711 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_start_handshake_msg(
2712 ssl, MBEDTLS_SSL_HS_CERTIFICATE_REQUEST,
2713 &buf, &buf_len));
XiaokangQiancec9ae62022-05-06 07:28:50 +00002714
Gilles Peskine449bd832023-01-11 14:50:10 +01002715 MBEDTLS_SSL_PROC_CHK(ssl_tls13_write_certificate_request_body(
2716 ssl, buf, buf + buf_len, &msg_len));
XiaokangQiancec9ae62022-05-06 07:28:50 +00002717
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +01002718 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_add_hs_msg_to_checksum(
Xiaokang Qian9f1747b2023-03-30 02:50:04 +00002719 ssl, MBEDTLS_SSL_HS_CERTIFICATE_REQUEST,
2720 buf, msg_len));
XiaokangQiancec9ae62022-05-06 07:28:50 +00002721
Gilles Peskine449bd832023-01-11 14:50:10 +01002722 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_finish_handshake_msg(
2723 ssl, buf_len, msg_len));
2724 } else if (ret == SSL_CERTIFICATE_REQUEST_SKIP) {
2725 MBEDTLS_SSL_DEBUG_MSG(2, ("<= skip write certificate request"));
XiaokangQiancec9ae62022-05-06 07:28:50 +00002726 ret = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01002727 } else {
2728 MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
XiaokangQiancec9ae62022-05-06 07:28:50 +00002729 ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
2730 goto cleanup;
2731 }
2732
Gilles Peskine449bd832023-01-11 14:50:10 +01002733 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_SERVER_CERTIFICATE);
XiaokangQiancec9ae62022-05-06 07:28:50 +00002734cleanup:
2735
Gilles Peskine449bd832023-01-11 14:50:10 +01002736 MBEDTLS_SSL_DEBUG_MSG(2, ("<= write certificate request"));
2737 return ret;
XiaokangQiancec9ae62022-05-06 07:28:50 +00002738}
XiaokangQiancec9ae62022-05-06 07:28:50 +00002739
Jerry Yu4d3841a2022-04-16 12:37:19 +08002740/*
Jerry Yuc6e6dbf2022-04-16 19:42:57 +08002741 * Handler for MBEDTLS_SSL_SERVER_CERTIFICATE
Jerry Yu83da34e2022-04-16 13:59:52 +08002742 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002743MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002744static int ssl_tls13_write_server_certificate(mbedtls_ssl_context *ssl)
Jerry Yu83da34e2022-04-16 13:59:52 +08002745{
Jerry Yu5a26f302022-05-10 20:46:40 +08002746 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
XiaokangQianfb665a82022-06-15 03:57:21 +00002747
2748#if defined(MBEDTLS_X509_CRT_PARSE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01002749 if ((ssl_tls13_pick_key_cert(ssl) != 0) ||
2750 mbedtls_ssl_own_cert(ssl) == NULL) {
2751 MBEDTLS_SSL_DEBUG_MSG(2, ("No certificate available."));
2752 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE,
2753 MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE);
2754 return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
Jerry Yu5a26f302022-05-10 20:46:40 +08002755 }
XiaokangQianfb665a82022-06-15 03:57:21 +00002756#endif /* MBEDTLS_X509_CRT_PARSE_C */
Jerry Yu5a26f302022-05-10 20:46:40 +08002757
Gilles Peskine449bd832023-01-11 14:50:10 +01002758 ret = mbedtls_ssl_tls13_write_certificate(ssl);
2759 if (ret != 0) {
2760 return ret;
2761 }
2762 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_CERTIFICATE_VERIFY);
2763 return 0;
Jerry Yu83da34e2022-04-16 13:59:52 +08002764}
2765
2766/*
Jerry Yuc6e6dbf2022-04-16 19:42:57 +08002767 * Handler for MBEDTLS_SSL_CERTIFICATE_VERIFY
Jerry Yu83da34e2022-04-16 13:59:52 +08002768 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002769MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002770static int ssl_tls13_write_certificate_verify(mbedtls_ssl_context *ssl)
Jerry Yu83da34e2022-04-16 13:59:52 +08002771{
Gilles Peskine449bd832023-01-11 14:50:10 +01002772 int ret = mbedtls_ssl_tls13_write_certificate_verify(ssl);
2773 if (ret != 0) {
2774 return ret;
2775 }
2776 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_SERVER_FINISHED);
2777 return 0;
Jerry Yu83da34e2022-04-16 13:59:52 +08002778}
Ronald Cron928cbd32022-10-04 16:14:26 +02002779#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED */
Jerry Yu83da34e2022-04-16 13:59:52 +08002780
Jerry Yu9b72e392023-12-01 16:27:08 +08002781/*
2782 * RFC 8446 section A.2
2783 *
2784 * | Send ServerHello
2785 * | K_send = handshake
2786 * | Send EncryptedExtensions
2787 * | [Send CertificateRequest]
2788 * Can send | [Send Certificate + CertificateVerify]
2789 * app data | Send Finished
2790 * after --> | K_send = application
2791 * here +--------+--------+
2792 * No 0-RTT | | 0-RTT
2793 * | |
2794 * K_recv = handshake | | K_recv = early data
2795 * [Skip decrypt errors] | +------> WAIT_EOED -+
2796 * | | Recv | | Recv EndOfEarlyData
2797 * | | early data | | K_recv = handshake
2798 * | +------------+ |
2799 * | |
2800 * +> WAIT_FLIGHT2 <--------+
2801 * |
2802 * +--------+--------+
2803 * No auth | | Client auth
2804 * | |
2805 * | v
2806 * | WAIT_CERT
2807 * | Recv | | Recv Certificate
2808 * | empty | v
2809 * | Certificate | WAIT_CV
2810 * | | | Recv
2811 * | v | CertificateVerify
2812 * +-> WAIT_FINISHED <---+
2813 * | Recv Finished
2814 *
2815 *
2816 * The following function handles the state changes after WAIT_FLIGHT2 in the
Jerry Yu3be85072023-12-04 09:58:54 +08002817 * above diagram. We are not going to receive early data related messages
2818 * anymore, prepare to receive the first handshake message of the client
2819 * second flight.
Jerry Yu9b72e392023-12-01 16:27:08 +08002820 */
Jerry Yu3be85072023-12-04 09:58:54 +08002821static void ssl_tls13_prepare_for_handshake_second_flight(
2822 mbedtls_ssl_context *ssl)
Jerry Yu9b72e392023-12-01 16:27:08 +08002823{
Jerry Yu9b72e392023-12-01 16:27:08 +08002824 if (ssl->handshake->certificate_request_sent) {
2825 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_CLIENT_CERTIFICATE);
2826 } else {
Jerry Yu42020fb2023-12-05 17:35:53 +08002827 MBEDTLS_SSL_DEBUG_MSG(2, ("skip parse certificate"));
Jerry Yuebb1b1d2023-12-05 11:02:15 +08002828 MBEDTLS_SSL_DEBUG_MSG(2, ("skip parse certificate verify"));
Jerry Yuebb1b1d2023-12-05 11:02:15 +08002829
Jerry Yu9b72e392023-12-01 16:27:08 +08002830 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_CLIENT_FINISHED);
2831 }
Jerry Yu9b72e392023-12-01 16:27:08 +08002832}
2833
Jerry Yu83da34e2022-04-16 13:59:52 +08002834/*
Jerry Yud6e253d2022-05-18 13:59:24 +08002835 * Handler for MBEDTLS_SSL_SERVER_FINISHED
Jerry Yu69dd8d42022-04-16 12:51:26 +08002836 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002837MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002838static int ssl_tls13_write_server_finished(mbedtls_ssl_context *ssl)
Jerry Yu69dd8d42022-04-16 12:51:26 +08002839{
Jerry Yud6e253d2022-05-18 13:59:24 +08002840 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yu27bdc7c2022-04-16 13:33:27 +08002841
Gilles Peskine449bd832023-01-11 14:50:10 +01002842 ret = mbedtls_ssl_tls13_write_finished_message(ssl);
2843 if (ret != 0) {
2844 return ret;
2845 }
Jerry Yu27bdc7c2022-04-16 13:33:27 +08002846
Gilles Peskine449bd832023-01-11 14:50:10 +01002847 ret = mbedtls_ssl_tls13_compute_application_transform(ssl);
2848 if (ret != 0) {
Jerry Yue3d67cb2022-05-19 15:33:10 +08002849 MBEDTLS_SSL_PEND_FATAL_ALERT(
Gilles Peskine449bd832023-01-11 14:50:10 +01002850 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE,
2851 MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE);
2852 return ret;
Jerry Yue3d67cb2022-05-19 15:33:10 +08002853 }
XiaokangQianc3017f62022-05-13 05:55:41 +00002854
Jerry Yu87b5ed42022-12-12 13:07:07 +08002855#if defined(MBEDTLS_SSL_EARLY_DATA)
Ronald Cron78a38f62024-02-01 18:30:31 +01002856 if (ssl->handshake->early_data_accepted) {
Jerry Yu0af63dc2023-12-01 17:14:51 +08002857 /* See RFC 8446 section A.2 for more information */
Jerry Yu87b5ed42022-12-12 13:07:07 +08002858 MBEDTLS_SSL_DEBUG_MSG(
2859 1, ("Switch to early keys for inbound traffic. "
2860 "( K_recv = early data )"));
2861 mbedtls_ssl_set_inbound_transform(
2862 ssl, ssl->handshake->transform_earlydata);
2863 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_END_OF_EARLY_DATA);
2864 return 0;
2865 }
2866#endif /* MBEDTLS_SSL_EARLY_DATA */
Jerry Yu0af63dc2023-12-01 17:14:51 +08002867 MBEDTLS_SSL_DEBUG_MSG(
2868 1, ("Switch to handshake keys for inbound traffic "
2869 "( K_recv = handshake )"));
Gilles Peskine449bd832023-01-11 14:50:10 +01002870 mbedtls_ssl_set_inbound_transform(ssl, ssl->handshake->transform_handshake);
XiaokangQian189ded22022-05-10 08:12:17 +00002871
Jerry Yu3be85072023-12-04 09:58:54 +08002872 ssl_tls13_prepare_for_handshake_second_flight(ssl);
Jerry Yu7d8c3fe2022-12-12 12:59:44 +08002873
2874 return 0;
2875}
2876
Jerry Yu87b5ed42022-12-12 13:07:07 +08002877#if defined(MBEDTLS_SSL_EARLY_DATA)
2878/*
Jerry Yu59d420f2023-12-01 16:30:34 +08002879 * Handler for MBEDTLS_SSL_END_OF_EARLY_DATA
Jerry Yu87b5ed42022-12-12 13:07:07 +08002880 */
Jerry Yu3be85072023-12-04 09:58:54 +08002881#define SSL_GOT_END_OF_EARLY_DATA 0
Jerry Yub55f9eb2023-12-05 10:27:17 +08002882#define SSL_GOT_EARLY_DATA 1
Jerry Yud5c34962023-12-01 16:32:31 +08002883/* Coordination:
Jerry Yu3be85072023-12-04 09:58:54 +08002884 * Deals with the ambiguity of not knowing if the next message is an
2885 * EndOfEarlyData message or an application message containing early data.
Jerry Yud5c34962023-12-01 16:32:31 +08002886 * Returns a negative code on failure, or
Jerry Yu3be85072023-12-04 09:58:54 +08002887 * - SSL_GOT_END_OF_EARLY_DATA
Jerry Yub55f9eb2023-12-05 10:27:17 +08002888 * - SSL_GOT_EARLY_DATA
Jerry Yud5c34962023-12-01 16:32:31 +08002889 * indicating which message is received.
2890 */
2891MBEDTLS_CHECK_RETURN_CRITICAL
2892static int ssl_tls13_end_of_early_data_coordinate(mbedtls_ssl_context *ssl)
2893{
2894 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yub4ed4602023-12-01 16:34:00 +08002895
2896 if ((ret = mbedtls_ssl_read_record(ssl, 0)) != 0) {
2897 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_read_record", ret);
2898 return ret;
2899 }
2900 ssl->keep_current_message = 1;
2901
2902 if (ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
2903 ssl->in_msg[0] == MBEDTLS_SSL_HS_END_OF_EARLY_DATA) {
Jerry Yub55f9eb2023-12-05 10:27:17 +08002904 MBEDTLS_SSL_DEBUG_MSG(3, ("Received an end_of_early_data message."));
Jerry Yu3be85072023-12-04 09:58:54 +08002905 return SSL_GOT_END_OF_EARLY_DATA;
Jerry Yub4ed4602023-12-01 16:34:00 +08002906 }
2907
2908 if (ssl->in_msgtype == MBEDTLS_SSL_MSG_APPLICATION_DATA) {
Jerry Yub55f9eb2023-12-05 10:27:17 +08002909 MBEDTLS_SSL_DEBUG_MSG(3, ("Received early data"));
Jerry Yu6a5904d2023-12-06 17:11:12 +08002910 /* RFC 8446 section 4.6.1
2911 *
2912 * A server receiving more than max_early_data_size bytes of 0-RTT data
2913 * SHOULD terminate the connection with an "unexpected_message" alert.
2914 *
2915 * TODO: Add received data size check here.
2916 */
Ronald Croned7d4bf2024-01-31 07:55:19 +01002917 if (ssl->in_offt == NULL) {
2918 /* Set the reading pointer */
2919 ssl->in_offt = ssl->in_msg;
2920 }
Jerry Yub55f9eb2023-12-05 10:27:17 +08002921 return SSL_GOT_EARLY_DATA;
Jerry Yub4ed4602023-12-01 16:34:00 +08002922 }
2923
Jerry Yu7bb40a32023-12-04 10:04:15 +08002924 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE,
2925 MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE);
Jerry Yub4ed4602023-12-01 16:34:00 +08002926 return MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE;
Jerry Yud5c34962023-12-01 16:32:31 +08002927}
2928
2929MBEDTLS_CHECK_RETURN_CRITICAL
2930static int ssl_tls13_parse_end_of_early_data(mbedtls_ssl_context *ssl,
2931 const unsigned char *buf,
2932 const unsigned char *end)
2933{
Jerry Yu75c9ab72023-12-01 16:41:40 +08002934 /* RFC 8446 section 4.5
2935 *
2936 * struct {} EndOfEarlyData;
2937 */
Jerry Yufbf03992023-12-04 10:00:37 +08002938 if (buf != end) {
2939 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR,
2940 MBEDTLS_ERR_SSL_DECODE_ERROR);
2941 return MBEDTLS_ERR_SSL_DECODE_ERROR;
2942 }
2943 return 0;
Jerry Yud5c34962023-12-01 16:32:31 +08002944}
2945
Jerry Yud5c34962023-12-01 16:32:31 +08002946/*
2947 * RFC 8446 section A.2
2948 *
2949 * | Send ServerHello
2950 * | K_send = handshake
2951 * | Send EncryptedExtensions
2952 * | [Send CertificateRequest]
2953 * Can send | [Send Certificate + CertificateVerify]
2954 * app data | Send Finished
2955 * after --> | K_send = application
2956 * here +--------+--------+
2957 * No 0-RTT | | 0-RTT
2958 * | |
2959 * K_recv = handshake | | K_recv = early data
2960 * [Skip decrypt errors] | +------> WAIT_EOED -+
2961 * | | Recv | | Recv EndOfEarlyData
2962 * | | early data | | K_recv = handshake
2963 * | +------------+ |
2964 * | |
2965 * +> WAIT_FLIGHT2 <--------+
2966 * |
2967 * +--------+--------+
2968 * No auth | | Client auth
2969 * | |
2970 * | v
2971 * | WAIT_CERT
2972 * | Recv | | Recv Certificate
2973 * | empty | v
2974 * | Certificate | WAIT_CV
2975 * | | | Recv
2976 * | v | CertificateVerify
2977 * +-> WAIT_FINISHED <---+
2978 * | Recv Finished
2979 *
2980 * The function handles actions and state changes from 0-RTT to WAIT_FLIGHT2 in
2981 * the above diagram.
2982 */
Jerry Yu87b5ed42022-12-12 13:07:07 +08002983MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yu59d420f2023-12-01 16:30:34 +08002984static int ssl_tls13_process_end_of_early_data(mbedtls_ssl_context *ssl)
Jerry Yu87b5ed42022-12-12 13:07:07 +08002985{
2986 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yud5c34962023-12-01 16:32:31 +08002987
2988 MBEDTLS_SSL_DEBUG_MSG(2, ("=> ssl_tls13_process_end_of_early_data"));
2989
2990 MBEDTLS_SSL_PROC_CHK_NEG(ssl_tls13_end_of_early_data_coordinate(ssl));
2991
Jerry Yu3be85072023-12-04 09:58:54 +08002992 if (ret == SSL_GOT_END_OF_EARLY_DATA) {
Jerry Yud5c34962023-12-01 16:32:31 +08002993 unsigned char *buf;
2994 size_t buf_len;
2995
2996 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_tls13_fetch_handshake_msg(
2997 ssl, MBEDTLS_SSL_HS_END_OF_EARLY_DATA,
2998 &buf, &buf_len));
2999
3000 MBEDTLS_SSL_PROC_CHK(ssl_tls13_parse_end_of_early_data(
3001 ssl, buf, buf + buf_len));
3002
Jerry Yue9655122023-12-01 16:44:40 +08003003 MBEDTLS_SSL_DEBUG_MSG(
3004 1, ("Switch to handshake keys for inbound traffic"
3005 "( K_recv = handshake )"));
3006 mbedtls_ssl_set_inbound_transform(
3007 ssl, ssl->handshake->transform_handshake);
3008
Jerry Yud5c34962023-12-01 16:32:31 +08003009 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_add_hs_msg_to_checksum(
3010 ssl, MBEDTLS_SSL_HS_END_OF_EARLY_DATA,
3011 buf, buf_len));
Jerry Yue9655122023-12-01 16:44:40 +08003012
Jerry Yu3be85072023-12-04 09:58:54 +08003013 ssl_tls13_prepare_for_handshake_second_flight(ssl);
Jerry Yud5c34962023-12-01 16:32:31 +08003014
Jerry Yub55f9eb2023-12-05 10:27:17 +08003015 } else if (ret == SSL_GOT_EARLY_DATA) {
Jerry Yud9ca3542023-12-06 17:23:52 +08003016 ret = MBEDTLS_ERR_SSL_RECEIVED_EARLY_DATA;
3017 goto cleanup;
Jerry Yud5c34962023-12-01 16:32:31 +08003018 } else {
3019 MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
3020 ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
3021 goto cleanup;
3022 }
3023
Jerry Yud5c34962023-12-01 16:32:31 +08003024cleanup:
3025 MBEDTLS_SSL_DEBUG_MSG(2, ("<= ssl_tls13_process_end_of_early_data"));
Jerry Yu87b5ed42022-12-12 13:07:07 +08003026 return ret;
3027}
3028#endif /* MBEDTLS_SSL_EARLY_DATA */
3029
Jerry Yu69dd8d42022-04-16 12:51:26 +08003030/*
Jerry Yud6e253d2022-05-18 13:59:24 +08003031 * Handler for MBEDTLS_SSL_CLIENT_FINISHED
Jerry Yu69dd8d42022-04-16 12:51:26 +08003032 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02003033MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01003034static int ssl_tls13_process_client_finished(mbedtls_ssl_context *ssl)
Jerry Yu69dd8d42022-04-16 12:51:26 +08003035{
Jerry Yud6e253d2022-05-18 13:59:24 +08003036 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
3037
Gilles Peskine449bd832023-01-11 14:50:10 +01003038 ret = mbedtls_ssl_tls13_process_finished_message(ssl);
3039 if (ret != 0) {
3040 return ret;
Jerry Yue3d67cb2022-05-19 15:33:10 +08003041 }
3042
Gilles Peskine449bd832023-01-11 14:50:10 +01003043 ret = mbedtls_ssl_tls13_compute_resumption_master_secret(ssl);
3044 if (ret != 0) {
Xiaokang Qian9f1747b2023-03-30 02:50:04 +00003045 MBEDTLS_SSL_DEBUG_RET(
3046 1, "mbedtls_ssl_tls13_compute_resumption_master_secret", ret);
Gilles Peskine449bd832023-01-11 14:50:10 +01003047 }
3048
3049 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_HANDSHAKE_WRAPUP);
3050 return 0;
Jerry Yu69dd8d42022-04-16 12:51:26 +08003051}
3052
3053/*
Jerry Yud6e253d2022-05-18 13:59:24 +08003054 * Handler for MBEDTLS_SSL_HANDSHAKE_WRAPUP
Jerry Yu69dd8d42022-04-16 12:51:26 +08003055 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02003056MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01003057static int ssl_tls13_handshake_wrapup(mbedtls_ssl_context *ssl)
Jerry Yu69dd8d42022-04-16 12:51:26 +08003058{
Gilles Peskine449bd832023-01-11 14:50:10 +01003059 MBEDTLS_SSL_DEBUG_MSG(2, ("handshake: done"));
Jerry Yu03ed50b2022-04-16 17:13:30 +08003060
Gilles Peskine449bd832023-01-11 14:50:10 +01003061 mbedtls_ssl_tls13_handshake_wrapup(ssl);
Jerry Yue67bef42022-07-07 07:29:42 +00003062
Pengyu Lv3643fdb2023-01-12 16:46:28 +08003063#if defined(MBEDTLS_SSL_SESSION_TICKETS) && \
3064 defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED)
Pengyu Lva1aa31b2022-12-13 13:49:59 +08003065/* TODO: Remove the check of SOME_PSK_ENABLED since SESSION_TICKETS requires
3066 * SOME_PSK_ENABLED to be enabled. Here is just to make CI happy. It is
3067 * expected to be resolved with issue#6395.
3068 */
Pengyu Lvc7af2c42022-12-01 16:33:00 +08003069 /* Sent NewSessionTicket message only when client supports PSK */
Pengyu Lvb2cfafb2023-11-14 13:56:13 +08003070 if (mbedtls_ssl_tls13_is_some_psk_supported(ssl)) {
Xiaokang Qian9f1747b2023-03-30 02:50:04 +00003071 mbedtls_ssl_handshake_set_state(
3072 ssl, MBEDTLS_SSL_TLS1_3_NEW_SESSION_TICKET);
Pengyu Lvc7af2c42022-12-01 16:33:00 +08003073 } else
Jerry Yufca4d572022-07-21 10:37:48 +08003074#endif
Pengyu Lv3643fdb2023-01-12 16:46:28 +08003075 {
3076 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_HANDSHAKE_OVER);
3077 }
Gilles Peskine449bd832023-01-11 14:50:10 +01003078 return 0;
Jerry Yu69dd8d42022-04-16 12:51:26 +08003079}
3080
3081/*
Jerry Yua8d3c502022-10-30 14:51:23 +08003082 * Handler for MBEDTLS_SSL_TLS1_3_NEW_SESSION_TICKET
Jerry Yue67bef42022-07-07 07:29:42 +00003083 */
3084#define SSL_NEW_SESSION_TICKET_SKIP 0
3085#define SSL_NEW_SESSION_TICKET_WRITE 1
3086MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01003087static int ssl_tls13_write_new_session_ticket_coordinate(mbedtls_ssl_context *ssl)
Jerry Yue67bef42022-07-07 07:29:42 +00003088{
3089 /* Check whether the use of session tickets is enabled */
Gilles Peskine449bd832023-01-11 14:50:10 +01003090 if (ssl->conf->f_ticket_write == NULL) {
3091 MBEDTLS_SSL_DEBUG_MSG(2, ("NewSessionTicket: disabled,"
3092 " callback is not set"));
3093 return SSL_NEW_SESSION_TICKET_SKIP;
Jerry Yud0766ec2022-09-22 10:46:57 +08003094 }
Gilles Peskine449bd832023-01-11 14:50:10 +01003095 if (ssl->conf->new_session_tickets_count == 0) {
3096 MBEDTLS_SSL_DEBUG_MSG(2, ("NewSessionTicket: disabled,"
3097 " configured count is zero"));
3098 return SSL_NEW_SESSION_TICKET_SKIP;
Jerry Yud0766ec2022-09-22 10:46:57 +08003099 }
3100
Gilles Peskine449bd832023-01-11 14:50:10 +01003101 if (ssl->handshake->new_session_tickets_count == 0) {
3102 MBEDTLS_SSL_DEBUG_MSG(2, ("NewSessionTicket: all tickets have "
3103 "been sent."));
3104 return SSL_NEW_SESSION_TICKET_SKIP;
Jerry Yue67bef42022-07-07 07:29:42 +00003105 }
3106
Gilles Peskine449bd832023-01-11 14:50:10 +01003107 return SSL_NEW_SESSION_TICKET_WRITE;
Jerry Yue67bef42022-07-07 07:29:42 +00003108}
3109
3110#if defined(MBEDTLS_SSL_SESSION_TICKETS)
3111MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01003112static int ssl_tls13_prepare_new_session_ticket(mbedtls_ssl_context *ssl,
3113 unsigned char *ticket_nonce,
3114 size_t ticket_nonce_size)
Jerry Yue67bef42022-07-07 07:29:42 +00003115{
3116 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
3117 mbedtls_ssl_session *session = ssl->session;
3118 mbedtls_ssl_ciphersuite_t *ciphersuite_info;
3119 psa_algorithm_t psa_hash_alg;
3120 int hash_length;
3121
Gilles Peskine449bd832023-01-11 14:50:10 +01003122 MBEDTLS_SSL_DEBUG_MSG(2, ("=> prepare NewSessionTicket msg"));
Jerry Yue67bef42022-07-07 07:29:42 +00003123
Pengyu Lv9f926952022-11-17 15:22:33 +08003124 /* Set ticket_flags depends on the advertised psk key exchange mode */
Pengyu Lv94a42cc2023-12-06 10:04:17 +08003125 mbedtls_ssl_tls13_session_clear_ticket_flags(
Pengyu Lv9eacb442022-12-09 14:39:19 +08003126 session, MBEDTLS_SSL_TLS1_3_TICKET_FLAGS_MASK);
Pengyu Lve6487fe2022-12-06 09:30:29 +08003127#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED)
Pengyu Lv94a42cc2023-12-06 10:04:17 +08003128 mbedtls_ssl_tls13_session_set_ticket_flags(
Pengyu Lv9eacb442022-12-09 14:39:19 +08003129 session, ssl->handshake->tls13_kex_modes);
Pengyu Lve6487fe2022-12-06 09:30:29 +08003130#endif
Jerry Yu95648b02023-12-06 15:03:34 +08003131
3132#if defined(MBEDTLS_SSL_EARLY_DATA)
3133 if (ssl->conf->early_data_enabled == MBEDTLS_SSL_EARLY_DATA_ENABLED &&
3134 ssl->conf->max_early_data_size > 0) {
Pengyu Lv94a42cc2023-12-06 10:04:17 +08003135 mbedtls_ssl_tls13_session_set_ticket_flags(
Jerry Yu95648b02023-12-06 15:03:34 +08003136 session, MBEDTLS_SSL_TLS1_3_TICKET_ALLOW_EARLY_DATA);
3137 }
3138#endif /* MBEDTLS_SSL_EARLY_DATA */
3139
Pengyu Lv4938a562023-01-16 11:28:49 +08003140 MBEDTLS_SSL_PRINT_TICKET_FLAGS(4, session->ticket_flags);
Pengyu Lv9f926952022-11-17 15:22:33 +08003141
Jerry Yue67bef42022-07-07 07:29:42 +00003142 /* Generate ticket_age_add */
Gilles Peskine449bd832023-01-11 14:50:10 +01003143 if ((ret = ssl->conf->f_rng(ssl->conf->p_rng,
3144 (unsigned char *) &session->ticket_age_add,
3145 sizeof(session->ticket_age_add)) != 0)) {
3146 MBEDTLS_SSL_DEBUG_RET(1, "generate_ticket_age_add", ret);
3147 return ret;
Jerry Yue67bef42022-07-07 07:29:42 +00003148 }
Gilles Peskine449bd832023-01-11 14:50:10 +01003149 MBEDTLS_SSL_DEBUG_MSG(3, ("ticket_age_add: %u",
3150 (unsigned int) session->ticket_age_add));
Jerry Yue67bef42022-07-07 07:29:42 +00003151
3152 /* Generate ticket_nonce */
Gilles Peskine449bd832023-01-11 14:50:10 +01003153 ret = ssl->conf->f_rng(ssl->conf->p_rng, ticket_nonce, ticket_nonce_size);
3154 if (ret != 0) {
3155 MBEDTLS_SSL_DEBUG_RET(1, "generate_ticket_nonce", ret);
3156 return ret;
Jerry Yue67bef42022-07-07 07:29:42 +00003157 }
Gilles Peskine449bd832023-01-11 14:50:10 +01003158 MBEDTLS_SSL_DEBUG_BUF(3, "ticket_nonce:",
3159 ticket_nonce, ticket_nonce_size);
Jerry Yue67bef42022-07-07 07:29:42 +00003160
3161 ciphersuite_info =
Gilles Peskine449bd832023-01-11 14:50:10 +01003162 (mbedtls_ssl_ciphersuite_t *) ssl->handshake->ciphersuite_info;
Dave Rodgman2eab4622023-10-05 13:30:37 +01003163 psa_hash_alg = mbedtls_md_psa_alg_from_type((mbedtls_md_type_t) ciphersuite_info->mac);
Gilles Peskine449bd832023-01-11 14:50:10 +01003164 hash_length = PSA_HASH_LENGTH(psa_hash_alg);
3165 if (hash_length == -1 ||
3166 (size_t) hash_length > sizeof(session->resumption_key)) {
3167 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Jerry Yufca4d572022-07-21 10:37:48 +08003168 }
3169
Jerry Yue67bef42022-07-07 07:29:42 +00003170 /* In this code the psk key length equals the length of the hash */
3171 session->resumption_key_len = hash_length;
3172 session->ciphersuite = ciphersuite_info->id;
3173
3174 /* Compute resumption key
3175 *
3176 * HKDF-Expand-Label( resumption_master_secret,
3177 * "resumption", ticket_nonce, Hash.length )
3178 */
3179 ret = mbedtls_ssl_tls13_hkdf_expand_label(
Gilles Peskine449bd832023-01-11 14:50:10 +01003180 psa_hash_alg,
3181 session->app_secrets.resumption_master_secret,
3182 hash_length,
3183 MBEDTLS_SSL_TLS1_3_LBL_WITH_LEN(resumption),
3184 ticket_nonce,
3185 ticket_nonce_size,
3186 session->resumption_key,
3187 hash_length);
Jerry Yue67bef42022-07-07 07:29:42 +00003188
Gilles Peskine449bd832023-01-11 14:50:10 +01003189 if (ret != 0) {
3190 MBEDTLS_SSL_DEBUG_RET(2,
3191 "Creating the ticket-resumed PSK failed",
3192 ret);
3193 return ret;
Jerry Yue67bef42022-07-07 07:29:42 +00003194 }
Gilles Peskine449bd832023-01-11 14:50:10 +01003195 MBEDTLS_SSL_DEBUG_BUF(3, "Ticket-resumed PSK",
3196 session->resumption_key,
3197 session->resumption_key_len);
Jerry Yue67bef42022-07-07 07:29:42 +00003198
Gilles Peskine449bd832023-01-11 14:50:10 +01003199 MBEDTLS_SSL_DEBUG_BUF(3, "resumption_master_secret",
3200 session->app_secrets.resumption_master_secret,
3201 hash_length);
Jerry Yue67bef42022-07-07 07:29:42 +00003202
Gilles Peskine449bd832023-01-11 14:50:10 +01003203 return 0;
Jerry Yue67bef42022-07-07 07:29:42 +00003204}
3205
3206/* This function creates a NewSessionTicket message in the following format:
3207 *
3208 * struct {
3209 * uint32 ticket_lifetime;
3210 * uint32 ticket_age_add;
3211 * opaque ticket_nonce<0..255>;
3212 * opaque ticket<1..2^16-1>;
3213 * Extension extensions<0..2^16-2>;
3214 * } NewSessionTicket;
3215 *
3216 * The ticket inside the NewSessionTicket message is an encrypted container
3217 * carrying the necessary information so that the server is later able to
3218 * re-start the communication.
3219 *
3220 * The following fields are placed inside the ticket by the
3221 * f_ticket_write() function:
3222 *
Jerry Yu0069abc2023-11-23 21:07:28 +08003223 * - creation time (ticket_creation_time)
3224 * - flags (ticket_flags)
Jerry Yue67bef42022-07-07 07:29:42 +00003225 * - age add (ticket_age_add)
Jerry Yu0069abc2023-11-23 21:07:28 +08003226 * - key (resumption_key)
3227 * - key length (resumption_key_len)
Jerry Yue67bef42022-07-07 07:29:42 +00003228 * - ciphersuite (ciphersuite)
Jerry Yu0069abc2023-11-23 21:07:28 +08003229 * - max_early_data_size (max_early_data_size)
Jerry Yue67bef42022-07-07 07:29:42 +00003230 */
3231MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01003232static int ssl_tls13_write_new_session_ticket_body(mbedtls_ssl_context *ssl,
3233 unsigned char *buf,
3234 unsigned char *end,
3235 size_t *out_len,
3236 unsigned char *ticket_nonce,
3237 size_t ticket_nonce_size)
Jerry Yue67bef42022-07-07 07:29:42 +00003238{
3239 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
3240 unsigned char *p = buf;
3241 mbedtls_ssl_session *session = ssl->session;
3242 size_t ticket_len;
3243 uint32_t ticket_lifetime;
Jerry Yu01da35e2022-12-12 15:09:22 +08003244 unsigned char *p_extensions_len;
Jerry Yue67bef42022-07-07 07:29:42 +00003245
3246 *out_len = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01003247 MBEDTLS_SSL_DEBUG_MSG(2, ("=> write NewSessionTicket msg"));
Jerry Yue67bef42022-07-07 07:29:42 +00003248
3249 /*
3250 * ticket_lifetime 4 bytes
3251 * ticket_age_add 4 bytes
3252 * ticket_nonce 1 + ticket_nonce_size bytes
3253 * ticket >=2 bytes
3254 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003255 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 4 + 4 + 1 + ticket_nonce_size + 2);
Jerry Yue67bef42022-07-07 07:29:42 +00003256
3257 /* Generate ticket and ticket_lifetime */
Ronald Cron3c0072b2023-11-22 10:00:14 +01003258#if defined(MBEDTLS_HAVE_TIME)
3259 session->ticket_creation_time = mbedtls_ms_time();
3260#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01003261 ret = ssl->conf->f_ticket_write(ssl->conf->p_ticket,
3262 session,
3263 p + 9 + ticket_nonce_size + 2,
3264 end,
3265 &ticket_len,
3266 &ticket_lifetime);
3267 if (ret != 0) {
3268 MBEDTLS_SSL_DEBUG_RET(1, "write_ticket", ret);
3269 return ret;
Jerry Yue67bef42022-07-07 07:29:42 +00003270 }
3271 /* RFC 8446 4.6.1
3272 * ticket_lifetime: Indicates the lifetime in seconds as a 32-bit
3273 * unsigned integer in network byte order from the time of ticket
3274 * issuance. Servers MUST NOT use any value greater than
3275 * 604800 seconds (7 days). The value of zero indicates that the
3276 * ticket should be discarded immediately. Clients MUST NOT cache
3277 * tickets for longer than 7 days, regardless of the ticket_lifetime,
3278 * and MAY delete tickets earlier based on local policy. A server
3279 * MAY treat a ticket as valid for a shorter period of time than what
3280 * is stated in the ticket_lifetime.
3281 */
Jerry Yu8e0174a2023-11-10 13:58:16 +08003282 if (ticket_lifetime > MBEDTLS_SSL_TLS1_3_MAX_ALLOWED_TICKET_LIFETIME) {
3283 ticket_lifetime = MBEDTLS_SSL_TLS1_3_MAX_ALLOWED_TICKET_LIFETIME;
Gilles Peskine449bd832023-01-11 14:50:10 +01003284 }
3285 MBEDTLS_PUT_UINT32_BE(ticket_lifetime, p, 0);
3286 MBEDTLS_SSL_DEBUG_MSG(3, ("ticket_lifetime: %u",
3287 (unsigned int) ticket_lifetime));
Jerry Yue67bef42022-07-07 07:29:42 +00003288
3289 /* Write ticket_age_add */
Gilles Peskine449bd832023-01-11 14:50:10 +01003290 MBEDTLS_PUT_UINT32_BE(session->ticket_age_add, p, 4);
3291 MBEDTLS_SSL_DEBUG_MSG(3, ("ticket_age_add: %u",
3292 (unsigned int) session->ticket_age_add));
Jerry Yue67bef42022-07-07 07:29:42 +00003293
3294 /* Write ticket_nonce */
Gilles Peskine449bd832023-01-11 14:50:10 +01003295 p[8] = (unsigned char) ticket_nonce_size;
3296 if (ticket_nonce_size > 0) {
3297 memcpy(p + 9, ticket_nonce, ticket_nonce_size);
Jerry Yue67bef42022-07-07 07:29:42 +00003298 }
3299 p += 9 + ticket_nonce_size;
3300
3301 /* Write ticket */
Gilles Peskine449bd832023-01-11 14:50:10 +01003302 MBEDTLS_PUT_UINT16_BE(ticket_len, p, 0);
Jerry Yue67bef42022-07-07 07:29:42 +00003303 p += 2;
Gilles Peskine449bd832023-01-11 14:50:10 +01003304 MBEDTLS_SSL_DEBUG_BUF(4, "ticket", p, ticket_len);
Jerry Yue67bef42022-07-07 07:29:42 +00003305 p += ticket_len;
3306
3307 /* Ticket Extensions
3308 *
Jerry Yu01da35e2022-12-12 15:09:22 +08003309 * Extension extensions<0..2^16-2>;
Jerry Yue67bef42022-07-07 07:29:42 +00003310 */
Jerry Yuedab6372022-10-31 14:37:31 +08003311 ssl->handshake->sent_extensions = MBEDTLS_SSL_EXT_MASK_NONE;
3312
Gilles Peskine449bd832023-01-11 14:50:10 +01003313 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 2);
Jerry Yu01da35e2022-12-12 15:09:22 +08003314 p_extensions_len = p;
Jerry Yue67bef42022-07-07 07:29:42 +00003315 p += 2;
3316
Jerry Yu01da35e2022-12-12 15:09:22 +08003317#if defined(MBEDTLS_SSL_EARLY_DATA)
Pengyu Lv94a42cc2023-12-06 10:04:17 +08003318 if (mbedtls_ssl_tls13_session_ticket_allow_early_data(session)) {
Jerry Yu95648b02023-12-06 15:03:34 +08003319 size_t output_len;
3320
Jerry Yuf135bac2023-11-23 18:10:51 +08003321 if ((ret = mbedtls_ssl_tls13_write_early_data_ext(
Jerry Yuc59c5862023-12-05 10:40:49 +08003322 ssl, 1, p, end, &output_len)) != 0) {
Jerry Yuf135bac2023-11-23 18:10:51 +08003323 MBEDTLS_SSL_DEBUG_RET(
3324 1, "mbedtls_ssl_tls13_write_early_data_ext", ret);
3325 return ret;
3326 }
3327 p += output_len;
Jerry Yu9e7f9bc2023-11-27 16:52:07 +08003328 } else {
3329 MBEDTLS_SSL_DEBUG_MSG(
3330 4, ("early_data not allowed, "
3331 "skip early_data extension in NewSessionTicket"));
Jerry Yu01da35e2022-12-12 15:09:22 +08003332 }
Jerry Yuf135bac2023-11-23 18:10:51 +08003333
Jerry Yu01da35e2022-12-12 15:09:22 +08003334#endif /* MBEDTLS_SSL_EARLY_DATA */
3335
3336 MBEDTLS_PUT_UINT16_BE(p - p_extensions_len - 2, p_extensions_len, 0);
3337
Jerry Yue67bef42022-07-07 07:29:42 +00003338 *out_len = p - buf;
Gilles Peskine449bd832023-01-11 14:50:10 +01003339 MBEDTLS_SSL_DEBUG_BUF(4, "ticket", buf, *out_len);
3340 MBEDTLS_SSL_DEBUG_MSG(2, ("<= write new session ticket"));
Jerry Yue67bef42022-07-07 07:29:42 +00003341
Jerry Yu7de2ff02022-11-08 21:43:46 +08003342 MBEDTLS_SSL_PRINT_EXTS(
Gilles Peskine449bd832023-01-11 14:50:10 +01003343 3, MBEDTLS_SSL_HS_NEW_SESSION_TICKET, ssl->handshake->sent_extensions);
Jerry Yu4b8f2f72022-10-31 13:31:22 +08003344
Gilles Peskine449bd832023-01-11 14:50:10 +01003345 return 0;
Jerry Yue67bef42022-07-07 07:29:42 +00003346}
3347
3348/*
Jerry Yua8d3c502022-10-30 14:51:23 +08003349 * Handler for MBEDTLS_SSL_TLS1_3_NEW_SESSION_TICKET
Jerry Yue67bef42022-07-07 07:29:42 +00003350 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003351static int ssl_tls13_write_new_session_ticket(mbedtls_ssl_context *ssl)
Jerry Yue67bef42022-07-07 07:29:42 +00003352{
3353 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
3354
Gilles Peskine449bd832023-01-11 14:50:10 +01003355 MBEDTLS_SSL_PROC_CHK_NEG(ssl_tls13_write_new_session_ticket_coordinate(ssl));
Jerry Yue67bef42022-07-07 07:29:42 +00003356
Gilles Peskine449bd832023-01-11 14:50:10 +01003357 if (ret == SSL_NEW_SESSION_TICKET_WRITE) {
Jerry Yue67bef42022-07-07 07:29:42 +00003358 unsigned char ticket_nonce[MBEDTLS_SSL_TLS1_3_TICKET_NONCE_LENGTH];
3359 unsigned char *buf;
3360 size_t buf_len, msg_len;
3361
Gilles Peskine449bd832023-01-11 14:50:10 +01003362 MBEDTLS_SSL_PROC_CHK(ssl_tls13_prepare_new_session_ticket(
3363 ssl, ticket_nonce, sizeof(ticket_nonce)));
Jerry Yue67bef42022-07-07 07:29:42 +00003364
Xiaokang Qian9f1747b2023-03-30 02:50:04 +00003365 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_start_handshake_msg(
3366 ssl, MBEDTLS_SSL_HS_NEW_SESSION_TICKET,
3367 &buf, &buf_len));
Jerry Yue67bef42022-07-07 07:29:42 +00003368
Gilles Peskine449bd832023-01-11 14:50:10 +01003369 MBEDTLS_SSL_PROC_CHK(ssl_tls13_write_new_session_ticket_body(
3370 ssl, buf, buf + buf_len, &msg_len,
3371 ticket_nonce, sizeof(ticket_nonce)));
Jerry Yue67bef42022-07-07 07:29:42 +00003372
Gilles Peskine449bd832023-01-11 14:50:10 +01003373 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_finish_handshake_msg(
3374 ssl, buf_len, msg_len));
Jerry Yufca4d572022-07-21 10:37:48 +08003375
Jerry Yu359e65f2022-09-22 23:47:43 +08003376 /* Limit session tickets count to one when resumption connection.
3377 *
3378 * See document of mbedtls_ssl_conf_new_session_tickets.
3379 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003380 if (ssl->handshake->resume == 1) {
Jerry Yu359e65f2022-09-22 23:47:43 +08003381 ssl->handshake->new_session_tickets_count = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01003382 } else {
Jerry Yu359e65f2022-09-22 23:47:43 +08003383 ssl->handshake->new_session_tickets_count--;
Gilles Peskine449bd832023-01-11 14:50:10 +01003384 }
Jerry Yub7e3fa72022-09-22 11:07:18 +08003385
Jerry Yua8d3c502022-10-30 14:51:23 +08003386 mbedtls_ssl_handshake_set_state(
Gilles Peskine449bd832023-01-11 14:50:10 +01003387 ssl, MBEDTLS_SSL_TLS1_3_NEW_SESSION_TICKET_FLUSH);
3388 } else {
3389 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_HANDSHAKE_OVER);
Jerry Yue67bef42022-07-07 07:29:42 +00003390 }
3391
Jerry Yue67bef42022-07-07 07:29:42 +00003392cleanup:
3393
Gilles Peskine449bd832023-01-11 14:50:10 +01003394 return ret;
Jerry Yue67bef42022-07-07 07:29:42 +00003395}
3396#endif /* MBEDTLS_SSL_SESSION_TICKETS */
3397
3398/*
XiaokangQiane8ff3502022-04-22 02:34:40 +00003399 * TLS 1.3 State Machine -- server side
XiaokangQian7807f9f2022-02-15 10:04:37 +00003400 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003401int mbedtls_ssl_tls13_handshake_server_step(mbedtls_ssl_context *ssl)
Jerry Yub9930e72021-08-06 17:11:51 +08003402{
XiaokangQian08037552022-04-20 07:16:41 +00003403 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
XiaokangQian7807f9f2022-02-15 10:04:37 +00003404
Gilles Peskine449bd832023-01-11 14:50:10 +01003405 if (ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER || ssl->handshake == NULL) {
3406 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
3407 }
XiaokangQian7807f9f2022-02-15 10:04:37 +00003408
Gilles Peskine449bd832023-01-11 14:50:10 +01003409 MBEDTLS_SSL_DEBUG_MSG(2, ("tls13 server state: %s(%d)",
Dave Rodgman2eab4622023-10-05 13:30:37 +01003410 mbedtls_ssl_states_str((mbedtls_ssl_states) ssl->state),
Gilles Peskine449bd832023-01-11 14:50:10 +01003411 ssl->state));
Jerry Yu6e81b272021-09-27 11:16:17 +08003412
Gilles Peskine449bd832023-01-11 14:50:10 +01003413 switch (ssl->state) {
XiaokangQian7807f9f2022-02-15 10:04:37 +00003414 /* start state */
3415 case MBEDTLS_SSL_HELLO_REQUEST:
Gilles Peskine449bd832023-01-11 14:50:10 +01003416 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_CLIENT_HELLO);
XiaokangQian08037552022-04-20 07:16:41 +00003417 ret = 0;
XiaokangQian7807f9f2022-02-15 10:04:37 +00003418 break;
3419
XiaokangQian7807f9f2022-02-15 10:04:37 +00003420 case MBEDTLS_SSL_CLIENT_HELLO:
Gilles Peskine449bd832023-01-11 14:50:10 +01003421 ret = ssl_tls13_process_client_hello(ssl);
3422 if (ret != 0) {
3423 MBEDTLS_SSL_DEBUG_RET(1, "ssl_tls13_process_client_hello", ret);
3424 }
Jerry Yuf41553b2022-05-09 22:20:30 +08003425 break;
XiaokangQian7807f9f2022-02-15 10:04:37 +00003426
Jerry Yuf41553b2022-05-09 22:20:30 +08003427 case MBEDTLS_SSL_HELLO_RETRY_REQUEST:
Gilles Peskine449bd832023-01-11 14:50:10 +01003428 ret = ssl_tls13_write_hello_retry_request(ssl);
3429 if (ret != 0) {
3430 MBEDTLS_SSL_DEBUG_RET(1, "ssl_tls13_write_hello_retry_request", ret);
3431 return ret;
Jerry Yuf41553b2022-05-09 22:20:30 +08003432 }
XiaokangQian7807f9f2022-02-15 10:04:37 +00003433 break;
3434
Jerry Yu5b64ae92022-03-30 17:15:02 +08003435 case MBEDTLS_SSL_SERVER_HELLO:
Gilles Peskine449bd832023-01-11 14:50:10 +01003436 ret = ssl_tls13_write_server_hello(ssl);
Jerry Yu5b64ae92022-03-30 17:15:02 +08003437 break;
3438
Xiaofei Baicba64af2022-02-15 10:00:56 +00003439 case MBEDTLS_SSL_ENCRYPTED_EXTENSIONS:
Gilles Peskine449bd832023-01-11 14:50:10 +01003440 ret = ssl_tls13_write_encrypted_extensions(ssl);
3441 if (ret != 0) {
3442 MBEDTLS_SSL_DEBUG_RET(1, "ssl_tls13_write_encrypted_extensions", ret);
3443 return ret;
Xiaofei Baicba64af2022-02-15 10:00:56 +00003444 }
Jerry Yu48330562022-05-06 21:35:44 +08003445 break;
Jerry Yu6a2cd9e2022-05-05 11:14:19 +08003446
Ronald Cron928cbd32022-10-04 16:14:26 +02003447#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED)
Xiaofei Bai9ca09d42022-02-14 12:57:18 +00003448 case MBEDTLS_SSL_CERTIFICATE_REQUEST:
Gilles Peskine449bd832023-01-11 14:50:10 +01003449 ret = ssl_tls13_write_certificate_request(ssl);
Xiaofei Bai9ca09d42022-02-14 12:57:18 +00003450 break;
Xiaofei Bai9ca09d42022-02-14 12:57:18 +00003451
Jerry Yu83da34e2022-04-16 13:59:52 +08003452 case MBEDTLS_SSL_SERVER_CERTIFICATE:
Gilles Peskine449bd832023-01-11 14:50:10 +01003453 ret = ssl_tls13_write_server_certificate(ssl);
Jerry Yu83da34e2022-04-16 13:59:52 +08003454 break;
3455
3456 case MBEDTLS_SSL_CERTIFICATE_VERIFY:
Gilles Peskine449bd832023-01-11 14:50:10 +01003457 ret = ssl_tls13_write_certificate_verify(ssl);
Jerry Yu83da34e2022-04-16 13:59:52 +08003458 break;
Ronald Cron928cbd32022-10-04 16:14:26 +02003459#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED */
Jerry Yu83da34e2022-04-16 13:59:52 +08003460
Gilles Peskine449bd832023-01-11 14:50:10 +01003461 /*
3462 * Injection of dummy-CCS's for middlebox compatibility
3463 */
Gabor Mezei7b39bf12022-05-24 16:04:14 +02003464#if defined(MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE)
Gabor Mezeif7044ea2022-06-28 16:01:49 +02003465 case MBEDTLS_SSL_SERVER_CCS_AFTER_HELLO_RETRY_REQUEST:
Gilles Peskine449bd832023-01-11 14:50:10 +01003466 ret = mbedtls_ssl_tls13_write_change_cipher_spec(ssl);
3467 if (ret == 0) {
3468 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_CLIENT_HELLO);
3469 }
Gabor Mezei7b39bf12022-05-24 16:04:14 +02003470 break;
3471
3472 case MBEDTLS_SSL_SERVER_CCS_AFTER_SERVER_HELLO:
Ronald Crone273f722024-02-13 18:22:26 +01003473 ret = mbedtls_ssl_tls13_write_change_cipher_spec(ssl);
3474 if (ret != 0) {
3475 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01003476 }
Ronald Cronfe59ff72024-01-24 14:31:50 +01003477 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_ENCRYPTED_EXTENSIONS);
Gabor Mezei7b39bf12022-05-24 16:04:14 +02003478 break;
3479#endif /* MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE */
3480
Jerry Yu69dd8d42022-04-16 12:51:26 +08003481 case MBEDTLS_SSL_SERVER_FINISHED:
Gilles Peskine449bd832023-01-11 14:50:10 +01003482 ret = ssl_tls13_write_server_finished(ssl);
Jerry Yu69dd8d42022-04-16 12:51:26 +08003483 break;
3484
Jerry Yu87b5ed42022-12-12 13:07:07 +08003485#if defined(MBEDTLS_SSL_EARLY_DATA)
3486 case MBEDTLS_SSL_END_OF_EARLY_DATA:
Jerry Yu59d420f2023-12-01 16:30:34 +08003487 ret = ssl_tls13_process_end_of_early_data(ssl);
Jerry Yu87b5ed42022-12-12 13:07:07 +08003488 break;
3489#endif /* MBEDTLS_SSL_EARLY_DATA */
3490
Jerry Yu69dd8d42022-04-16 12:51:26 +08003491 case MBEDTLS_SSL_CLIENT_FINISHED:
Gilles Peskine449bd832023-01-11 14:50:10 +01003492 ret = ssl_tls13_process_client_finished(ssl);
Jerry Yu69dd8d42022-04-16 12:51:26 +08003493 break;
3494
Jerry Yu69dd8d42022-04-16 12:51:26 +08003495 case MBEDTLS_SSL_HANDSHAKE_WRAPUP:
Gilles Peskine449bd832023-01-11 14:50:10 +01003496 ret = ssl_tls13_handshake_wrapup(ssl);
Jerry Yu69dd8d42022-04-16 12:51:26 +08003497 break;
3498
Ronald Cron766c0cd2022-10-18 12:17:11 +02003499#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED)
XiaokangQian6b916b12022-04-25 07:29:34 +00003500 case MBEDTLS_SSL_CLIENT_CERTIFICATE:
Gilles Peskine449bd832023-01-11 14:50:10 +01003501 ret = mbedtls_ssl_tls13_process_certificate(ssl);
3502 if (ret == 0) {
3503 if (ssl->session_negotiate->peer_cert != NULL) {
Ronald Cron209cae92022-06-07 10:30:19 +02003504 mbedtls_ssl_handshake_set_state(
Gilles Peskine449bd832023-01-11 14:50:10 +01003505 ssl, MBEDTLS_SSL_CLIENT_CERTIFICATE_VERIFY);
3506 } else {
3507 MBEDTLS_SSL_DEBUG_MSG(2, ("skip parse certificate verify"));
Ronald Cron209cae92022-06-07 10:30:19 +02003508 mbedtls_ssl_handshake_set_state(
Gilles Peskine449bd832023-01-11 14:50:10 +01003509 ssl, MBEDTLS_SSL_CLIENT_FINISHED);
Ronald Cron19385882022-06-15 16:26:13 +02003510 }
XiaokangQian6b916b12022-04-25 07:29:34 +00003511 }
3512 break;
3513
3514 case MBEDTLS_SSL_CLIENT_CERTIFICATE_VERIFY:
Gilles Peskine449bd832023-01-11 14:50:10 +01003515 ret = mbedtls_ssl_tls13_process_certificate_verify(ssl);
3516 if (ret == 0) {
XiaokangQian6b916b12022-04-25 07:29:34 +00003517 mbedtls_ssl_handshake_set_state(
Gilles Peskine449bd832023-01-11 14:50:10 +01003518 ssl, MBEDTLS_SSL_CLIENT_FINISHED);
XiaokangQian6b916b12022-04-25 07:29:34 +00003519 }
3520 break;
Ronald Cron766c0cd2022-10-18 12:17:11 +02003521#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED */
XiaokangQian6b916b12022-04-25 07:29:34 +00003522
Jerry Yue67bef42022-07-07 07:29:42 +00003523#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Jerry Yua8d3c502022-10-30 14:51:23 +08003524 case MBEDTLS_SSL_TLS1_3_NEW_SESSION_TICKET:
Gilles Peskine449bd832023-01-11 14:50:10 +01003525 ret = ssl_tls13_write_new_session_ticket(ssl);
3526 if (ret != 0) {
3527 MBEDTLS_SSL_DEBUG_RET(1,
3528 "ssl_tls13_write_new_session_ticket ",
3529 ret);
Jerry Yue67bef42022-07-07 07:29:42 +00003530 }
3531 break;
Jerry Yua8d3c502022-10-30 14:51:23 +08003532 case MBEDTLS_SSL_TLS1_3_NEW_SESSION_TICKET_FLUSH:
Jerry Yue67bef42022-07-07 07:29:42 +00003533 /* This state is necessary to do the flush of the New Session
Jerry Yua8d3c502022-10-30 14:51:23 +08003534 * Ticket message written in MBEDTLS_SSL_TLS1_3_NEW_SESSION_TICKET
Jerry Yue67bef42022-07-07 07:29:42 +00003535 * as part of ssl_prepare_handshake_step.
3536 */
3537 ret = 0;
Jerry Yud4e75002022-08-09 13:33:50 +08003538
Gilles Peskine449bd832023-01-11 14:50:10 +01003539 if (ssl->handshake->new_session_tickets_count == 0) {
3540 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_HANDSHAKE_OVER);
3541 } else {
Xiaokang Qian9f1747b2023-03-30 02:50:04 +00003542 mbedtls_ssl_handshake_set_state(
3543 ssl, MBEDTLS_SSL_TLS1_3_NEW_SESSION_TICKET);
Gilles Peskine449bd832023-01-11 14:50:10 +01003544 }
Jerry Yue67bef42022-07-07 07:29:42 +00003545 break;
3546
3547#endif /* MBEDTLS_SSL_SESSION_TICKETS */
3548
XiaokangQian7807f9f2022-02-15 10:04:37 +00003549 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01003550 MBEDTLS_SSL_DEBUG_MSG(1, ("invalid state %d", ssl->state));
3551 return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
XiaokangQian7807f9f2022-02-15 10:04:37 +00003552 }
3553
Gilles Peskine449bd832023-01-11 14:50:10 +01003554 return ret;
Jerry Yub9930e72021-08-06 17:11:51 +08003555}
Jerry Yu3cc4c2a2021-08-06 16:29:08 +08003556
Jerry Yufb4b6472022-01-27 15:03:26 +08003557#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_PROTO_TLS1_3 */