blob: 25279b3e34378dcd493f38ddfe1b06d9c707d417 [file] [log] [blame]
Jerry Yu3cc4c2a2021-08-06 16:29:08 +08001/*
Jerry Yub9930e72021-08-06 17:11:51 +08002 * TLS 1.3 server-side functions
Jerry Yu3cc4c2a2021-08-06 16:29:08 +08003 *
4 * Copyright The Mbed TLS Contributors
Dave Rodgman16799db2023-11-02 19:47:20 +00005 * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
Gilles Peskine449bd832023-01-11 14:50:10 +01006 */
Jerry Yu3cc4c2a2021-08-06 16:29:08 +08007
8#include "common.h"
9
Jerry Yufb4b6472022-01-27 15:03:26 +080010#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_PROTO_TLS1_3)
Jerry Yu3cc4c2a2021-08-06 16:29:08 +080011
Valerio Settib4f50762024-01-17 10:24:52 +010012#include "debug_internal.h"
Xiaofei Baicba64af2022-02-15 10:00:56 +000013#include "mbedtls/error.h"
14#include "mbedtls/platform.h"
Jerry Yu1c105562022-07-10 06:32:38 +000015#include "mbedtls/constant_time.h"
Manuel Pégourié-Gonnardd55d66f2023-06-20 10:14:58 +020016#include "mbedtls/oid.h"
Valerio Setti384fbde2024-01-02 13:26:40 +010017#include "mbedtls/psa_util.h"
Jerry Yu3bf2c642022-03-30 22:02:12 +080018
Xiaofei Bai9ca09d42022-02-14 12:57:18 +000019#include "ssl_misc.h"
20#include "ssl_tls13_keys.h"
21#include "ssl_debug_helpers.h"
22
Jerry Yuf35ba382022-08-23 17:58:26 +080023
Jerry Yuc5a23a02022-08-25 10:51:44 +080024static const mbedtls_ssl_ciphersuite_t *ssl_tls13_validate_peer_ciphersuite(
Gilles Peskine449bd832023-01-11 14:50:10 +010025 mbedtls_ssl_context *ssl,
26 unsigned int cipher_suite)
Jerry Yuf35ba382022-08-23 17:58:26 +080027{
28 const mbedtls_ssl_ciphersuite_t *ciphersuite_info;
Gilles Peskine449bd832023-01-11 14:50:10 +010029 if (!mbedtls_ssl_tls13_cipher_suite_is_offered(ssl, cipher_suite)) {
30 return NULL;
Jerry Yuf35ba382022-08-23 17:58:26 +080031 }
Gilles Peskine449bd832023-01-11 14:50:10 +010032
33 ciphersuite_info = mbedtls_ssl_ciphersuite_from_id(cipher_suite);
34 if ((mbedtls_ssl_validate_ciphersuite(ssl, ciphersuite_info,
35 ssl->tls_version,
36 ssl->tls_version) != 0)) {
37 return NULL;
38 }
39 return ciphersuite_info;
Jerry Yuf35ba382022-08-23 17:58:26 +080040}
41
Ronald Cron89089cc2024-02-14 18:18:08 +010042static void ssl_tls13_select_ciphersuite(
43 mbedtls_ssl_context *ssl,
44 const unsigned char *cipher_suites,
45 const unsigned char *cipher_suites_end,
46 int psk_ciphersuite_id,
47 psa_algorithm_t psk_hash_alg,
48 const mbedtls_ssl_ciphersuite_t **selected_ciphersuite_info)
49{
50 *selected_ciphersuite_info = NULL;
51
52 /*
53 * This function assumes that the length of the list of ciphersuites is
54 * even and it should have been checked it is so in the main ClientHello
55 * parsing function. Double check here.
56 */
57 if ((cipher_suites_end - cipher_suites) & 1) {
58 return;
59 }
60
61 for (const unsigned char *p = cipher_suites;
62 p < cipher_suites_end; p += 2) {
63 /*
64 * "cipher_suites_end - p is even" is an invariant of the loop. As
65 * cipher_suites_end - p > 0, we have cipher_suites_end - p >= 2 and it
66 * is thus safe to read two bytes.
67 */
68 uint16_t id = MBEDTLS_GET_UINT16_BE(p, 0);
69
70 const mbedtls_ssl_ciphersuite_t *info =
71 ssl_tls13_validate_peer_ciphersuite(ssl, id);
72 if (info == NULL) {
73 continue;
74 }
75
76 /*
77 * If a valid PSK ciphersuite identifier has been passed in, we seek
78 * for an exact match.
79 */
80 if (psk_ciphersuite_id != 0) {
81 if (id != psk_ciphersuite_id) {
82 continue;
83 }
84 } else if (psk_hash_alg != PSA_ALG_NONE) {
85 if (mbedtls_md_psa_alg_from_type((mbedtls_md_type_t) info->mac) !=
86 psk_hash_alg) {
87 continue;
88 }
89 }
90
91 *selected_ciphersuite_info = info;
92 return;
93 }
94
95 MBEDTLS_SSL_DEBUG_MSG(2, ("No matched ciphersuite"));
96}
97
Ronald Cron41a443a2022-10-04 16:38:25 +020098#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED)
Jerry Yue19e3b92022-07-08 12:04:51 +000099/* From RFC 8446:
100 *
101 * enum { psk_ke(0), psk_dhe_ke(1), (255) } PskKeyExchangeMode;
102 * struct {
103 * PskKeyExchangeMode ke_modes<1..255>;
104 * } PskKeyExchangeModes;
105 */
Jerry Yu6e74a7e2022-07-20 20:49:32 +0800106MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100107static int ssl_tls13_parse_key_exchange_modes_ext(mbedtls_ssl_context *ssl,
108 const unsigned char *buf,
109 const unsigned char *end)
Jerry Yue19e3b92022-07-08 12:04:51 +0000110{
Jerry Yu299e31f2022-07-13 23:06:36 +0800111 const unsigned char *p = buf;
Jerry Yue19e3b92022-07-08 12:04:51 +0000112 size_t ke_modes_len;
113 int ke_modes = 0;
114
Jerry Yu854dd9e2022-07-15 14:28:27 +0800115 /* Read ke_modes length (1 Byte) */
Gilles Peskine449bd832023-01-11 14:50:10 +0100116 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, 1);
Jerry Yu299e31f2022-07-13 23:06:36 +0800117 ke_modes_len = *p++;
Jerry Yue19e3b92022-07-08 12:04:51 +0000118 /* Currently, there are only two PSK modes, so even without looking
119 * at the content, something's wrong if the list has more than 2 items. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100120 if (ke_modes_len > 2) {
121 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER,
122 MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER);
123 return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
Jerry Yu299e31f2022-07-13 23:06:36 +0800124 }
Jerry Yue19e3b92022-07-08 12:04:51 +0000125
Gilles Peskine449bd832023-01-11 14:50:10 +0100126 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, ke_modes_len);
Jerry Yue19e3b92022-07-08 12:04:51 +0000127
Gilles Peskine449bd832023-01-11 14:50:10 +0100128 while (ke_modes_len-- != 0) {
129 switch (*p++) {
130 case MBEDTLS_SSL_TLS1_3_PSK_MODE_PURE:
131 ke_modes |= MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK;
132 MBEDTLS_SSL_DEBUG_MSG(3, ("Found PSK KEX MODE"));
133 break;
134 case MBEDTLS_SSL_TLS1_3_PSK_MODE_ECDHE:
135 ke_modes |= MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK_EPHEMERAL;
136 MBEDTLS_SSL_DEBUG_MSG(3, ("Found PSK_EPHEMERAL KEX MODE"));
137 break;
138 default:
139 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER,
140 MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER);
141 return MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER;
Jerry Yue19e3b92022-07-08 12:04:51 +0000142 }
143 }
144
145 ssl->handshake->tls13_kex_modes = ke_modes;
Gilles Peskine449bd832023-01-11 14:50:10 +0100146 return 0;
Jerry Yue19e3b92022-07-08 12:04:51 +0000147}
Jerry Yu1c105562022-07-10 06:32:38 +0000148
Ronald Cronfbae94a2023-12-05 18:15:14 +0100149#define SSL_TLS1_3_PSK_IDENTITY_DOES_NOT_MATCH 2
150#define SSL_TLS1_3_PSK_IDENTITY_MATCH_BUT_PSK_NOT_USABLE 1
151#define SSL_TLS1_3_PSK_IDENTITY_MATCH 0
Jerry Yu95699e72022-08-21 19:22:23 +0800152
153#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Pengyu Lv29daf4a2023-10-30 17:13:30 +0800154MBEDTLS_CHECK_RETURN_CRITICAL
Pengyu Lvbc4aab72023-12-01 15:37:24 +0800155static int ssl_tls13_key_exchange_is_psk_available(mbedtls_ssl_context *ssl);
Pengyu Lv29daf4a2023-10-30 17:13:30 +0800156MBEDTLS_CHECK_RETURN_CRITICAL
Pengyu Lvbc4aab72023-12-01 15:37:24 +0800157static int ssl_tls13_key_exchange_is_psk_ephemeral_available(mbedtls_ssl_context *ssl);
Jerry Yu95699e72022-08-21 19:22:23 +0800158
159MBEDTLS_CHECK_RETURN_CRITICAL
160static int ssl_tls13_offered_psks_check_identity_match_ticket(
Gilles Peskine449bd832023-01-11 14:50:10 +0100161 mbedtls_ssl_context *ssl,
162 const unsigned char *identity,
163 size_t identity_len,
164 uint32_t obfuscated_ticket_age,
165 mbedtls_ssl_session *session)
Jerry Yu95699e72022-08-21 19:22:23 +0800166{
Jerry Yufd310eb2022-09-06 09:16:35 +0800167 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yu95699e72022-08-21 19:22:23 +0800168 unsigned char *ticket_buffer;
Pengyu Lv29daf4a2023-10-30 17:13:30 +0800169 unsigned int key_exchanges;
Jerry Yu8d4bbba2022-09-13 14:15:48 +0800170#if defined(MBEDTLS_HAVE_TIME)
Jerry Yucebffc32022-12-15 18:00:05 +0800171 mbedtls_ms_time_t now;
172 mbedtls_ms_time_t server_age;
Jerry Yu713ce1f2023-11-17 15:32:12 +0800173 uint32_t client_age;
Jerry Yucebffc32022-12-15 18:00:05 +0800174 mbedtls_ms_time_t age_diff;
Jerry Yu8d4bbba2022-09-13 14:15:48 +0800175#endif
Jerry Yu95699e72022-08-21 19:22:23 +0800176
177 ((void) obfuscated_ticket_age);
178
Gilles Peskine449bd832023-01-11 14:50:10 +0100179 MBEDTLS_SSL_DEBUG_MSG(2, ("=> check_identity_match_ticket"));
Jerry Yu95699e72022-08-21 19:22:23 +0800180
Jerry Yufd310eb2022-09-06 09:16:35 +0800181 /* Ticket parser is not configured, Skip */
Gilles Peskine449bd832023-01-11 14:50:10 +0100182 if (ssl->conf->f_ticket_parse == NULL || identity_len == 0) {
Ronald Cronfbae94a2023-12-05 18:15:14 +0100183 return SSL_TLS1_3_PSK_IDENTITY_DOES_NOT_MATCH;
Gilles Peskine449bd832023-01-11 14:50:10 +0100184 }
Jerry Yu95699e72022-08-21 19:22:23 +0800185
Jerry Yu4746b102022-09-13 11:11:48 +0800186 /* We create a copy of the encrypted ticket since the ticket parsing
187 * function is allowed to use its input buffer as an output buffer
188 * (in-place decryption). We do, however, need the original buffer for
189 * computing the PSK binder value.
Jerry Yu95699e72022-08-21 19:22:23 +0800190 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100191 ticket_buffer = mbedtls_calloc(1, identity_len);
192 if (ticket_buffer == NULL) {
193 MBEDTLS_SSL_DEBUG_MSG(1, ("buffer too small"));
194 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
Jerry Yu95699e72022-08-21 19:22:23 +0800195 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100196 memcpy(ticket_buffer, identity, identity_len);
Jerry Yu95699e72022-08-21 19:22:23 +0800197
Ronald Cronfbae94a2023-12-05 18:15:14 +0100198 ret = ssl->conf->f_ticket_parse(ssl->conf->p_ticket,
199 session,
200 ticket_buffer, identity_len);
201 if (ret == 0) {
202 ret = SSL_TLS1_3_PSK_IDENTITY_MATCH;
203 } else {
204 if (ret == MBEDTLS_ERR_SSL_SESSION_TICKET_EXPIRED) {
Gilles Peskine449bd832023-01-11 14:50:10 +0100205 MBEDTLS_SSL_DEBUG_MSG(3, ("ticket is expired"));
Ronald Cronfbae94a2023-12-05 18:15:14 +0100206 ret = SSL_TLS1_3_PSK_IDENTITY_MATCH_BUT_PSK_NOT_USABLE;
Gilles Peskine449bd832023-01-11 14:50:10 +0100207 } else {
Ronald Cronfbae94a2023-12-05 18:15:14 +0100208 if (ret == MBEDTLS_ERR_SSL_INVALID_MAC) {
209 MBEDTLS_SSL_DEBUG_MSG(3, ("ticket is not authentic"));
210 } else {
211 MBEDTLS_SSL_DEBUG_RET(1, "ticket_parse", ret);
212 }
213 ret = SSL_TLS1_3_PSK_IDENTITY_DOES_NOT_MATCH;
Gilles Peskine449bd832023-01-11 14:50:10 +0100214 }
Jerry Yu95699e72022-08-21 19:22:23 +0800215 }
216
217 /* We delete the temporary buffer */
Gilles Peskine449bd832023-01-11 14:50:10 +0100218 mbedtls_free(ticket_buffer);
Jerry Yu95699e72022-08-21 19:22:23 +0800219
Ronald Cronfbae94a2023-12-05 18:15:14 +0100220 if (ret != SSL_TLS1_3_PSK_IDENTITY_MATCH) {
221 goto exit;
Jerry Yuce3b95e2023-10-31 16:02:04 +0800222 }
Jerry Yuce3b95e2023-10-31 16:02:04 +0800223
Ronald Cronfbae94a2023-12-05 18:15:14 +0100224 ret = SSL_TLS1_3_PSK_IDENTITY_MATCH_BUT_PSK_NOT_USABLE;
225
226 if (session->tls_version != MBEDTLS_SSL_VERSION_TLS1_3) {
227 MBEDTLS_SSL_DEBUG_MSG(3, ("Ticket TLS version is not 1.3."));
Jerry Yu8d4bbba2022-09-13 14:15:48 +0800228 goto exit;
229 }
Jerry Yu95699e72022-08-21 19:22:23 +0800230
Pengyu Lv3eb49be2022-12-05 16:35:12 +0800231 /* RFC 8446 section 4.2.9
232 *
233 * Servers SHOULD NOT send NewSessionTicket with tickets that are not
234 * compatible with the advertised modes; however, if a server does so,
235 * the impact will just be that the client's attempts at resumption fail.
236 *
237 * We regard the ticket with incompatible key exchange modes as not match.
238 */
Pengyu Lv29daf4a2023-10-30 17:13:30 +0800239 MBEDTLS_SSL_PRINT_TICKET_FLAGS(4, session->ticket_flags);
Pengyu Lv29daf4a2023-10-30 17:13:30 +0800240
241 key_exchanges = 0;
Pengyu Lv94a42cc2023-12-06 10:04:17 +0800242 if (mbedtls_ssl_tls13_session_ticket_allow_psk_ephemeral(session) &&
Pengyu Lvbc4aab72023-12-01 15:37:24 +0800243 ssl_tls13_key_exchange_is_psk_ephemeral_available(ssl)) {
Pengyu Lv29daf4a2023-10-30 17:13:30 +0800244 key_exchanges |= MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK_EPHEMERAL;
245 }
Pengyu Lv94a42cc2023-12-06 10:04:17 +0800246 if (mbedtls_ssl_tls13_session_ticket_allow_psk(session) &&
Pengyu Lvbc4aab72023-12-01 15:37:24 +0800247 ssl_tls13_key_exchange_is_psk_available(ssl)) {
Pengyu Lv29daf4a2023-10-30 17:13:30 +0800248 key_exchanges |= MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK;
249 }
250
251 if (key_exchanges == 0) {
Pengyu Lv3eb49be2022-12-05 16:35:12 +0800252 MBEDTLS_SSL_DEBUG_MSG(3, ("No suitable key exchange mode"));
253 goto exit;
254 }
255
Gilles Peskine449bd832023-01-11 14:50:10 +0100256#if defined(MBEDTLS_HAVE_TIME)
Jerry Yucebffc32022-12-15 18:00:05 +0800257 now = mbedtls_ms_time();
Gilles Peskine449bd832023-01-11 14:50:10 +0100258
Jerry Yu25ba4d42023-11-10 14:12:20 +0800259 if (now < session->ticket_creation_time) {
Gilles Peskine449bd832023-01-11 14:50:10 +0100260 MBEDTLS_SSL_DEBUG_MSG(
Jerry Yu713ce1f2023-11-17 15:32:12 +0800261 3, ("Invalid ticket creation time ( now = %" MBEDTLS_PRINTF_MS_TIME
262 ", creation_time = %" MBEDTLS_PRINTF_MS_TIME " )",
Jerry Yu25ba4d42023-11-10 14:12:20 +0800263 now, session->ticket_creation_time));
Gilles Peskine449bd832023-01-11 14:50:10 +0100264 goto exit;
265 }
266
Jerry Yu25ba4d42023-11-10 14:12:20 +0800267 server_age = now - session->ticket_creation_time;
Jerry Yu95699e72022-08-21 19:22:23 +0800268
Jerry Yu8d4bbba2022-09-13 14:15:48 +0800269 /* RFC 8446 section 4.6.1
270 *
271 * Servers MUST NOT use any value greater than 604800 seconds (7 days).
272 *
273 * RFC 8446 section 4.2.11.1
274 *
275 * Clients MUST NOT attempt to use tickets which have ages greater than
276 * the "ticket_lifetime" value which was provided with the ticket.
277 *
Jerry Yu8d4bbba2022-09-13 14:15:48 +0800278 */
Jerry Yu8e0174a2023-11-10 13:58:16 +0800279 if (server_age > MBEDTLS_SSL_TLS1_3_MAX_ALLOWED_TICKET_LIFETIME * 1000) {
Jerry Yu8d4bbba2022-09-13 14:15:48 +0800280 MBEDTLS_SSL_DEBUG_MSG(
Jerry Yud84c14f2023-11-16 13:33:57 +0800281 3, ("Ticket age exceeds limitation ticket_age = %" MBEDTLS_PRINTF_MS_TIME,
Jerry Yucebffc32022-12-15 18:00:05 +0800282 server_age));
Jerry Yu8d4bbba2022-09-13 14:15:48 +0800283 goto exit;
284 }
Jerry Yu95699e72022-08-21 19:22:23 +0800285
Jerry Yu8d4bbba2022-09-13 14:15:48 +0800286 /* RFC 8446 section 4.2.10
287 *
288 * For PSKs provisioned via NewSessionTicket, a server MUST validate that
289 * the ticket age for the selected PSK identity (computed by subtracting
290 * ticket_age_add from PskIdentity.obfuscated_ticket_age modulo 2^32) is
291 * within a small tolerance of the time since the ticket was issued.
Jerry Yuf7dad3c2022-09-14 22:31:39 +0800292 *
Jerry Yu31b601a2023-11-10 11:27:21 +0800293 * NOTE: The typical accuracy of an RTC crystal is ±100 to ±20 parts per
294 * million (360 to 72 milliseconds per hour). Default tolerance
Jerry Yu9cb953a2023-11-15 09:54:11 +0800295 * window is 6s, thus in the worst case clients and servers must
Jerry Yu31b601a2023-11-10 11:27:21 +0800296 * sync up their system time every 6000/360/2~=8 hours.
Jerry Yu8d4bbba2022-09-13 14:15:48 +0800297 */
Jerry Yucebffc32022-12-15 18:00:05 +0800298 client_age = obfuscated_ticket_age - session->ticket_age_add;
Jerry Yu60e99722023-11-20 09:55:24 +0800299 age_diff = server_age - (mbedtls_ms_time_t) client_age;
Jerry Yu28e7c552023-11-10 12:05:56 +0800300 if (age_diff < -MBEDTLS_SSL_TLS1_3_TICKET_AGE_TOLERANCE ||
Jerry Yucebffc32022-12-15 18:00:05 +0800301 age_diff > MBEDTLS_SSL_TLS1_3_TICKET_AGE_TOLERANCE) {
Jerry Yu8d4bbba2022-09-13 14:15:48 +0800302 MBEDTLS_SSL_DEBUG_MSG(
Jerry Yuf16efbc2023-10-30 11:06:24 +0800303 3, ("Ticket age outside tolerance window ( diff = %"
304 MBEDTLS_PRINTF_MS_TIME ")",
Jerry Yucebffc32022-12-15 18:00:05 +0800305 age_diff));
Jerry Yu8d4bbba2022-09-13 14:15:48 +0800306 goto exit;
307 }
Jerry Yu95699e72022-08-21 19:22:23 +0800308#endif /* MBEDTLS_HAVE_TIME */
Jerry Yu8d4bbba2022-09-13 14:15:48 +0800309
Ronald Cronfbae94a2023-12-05 18:15:14 +0100310 ret = SSL_TLS1_3_PSK_IDENTITY_MATCH;
311
Jerry Yu8d4bbba2022-09-13 14:15:48 +0800312exit:
Ronald Cronfbae94a2023-12-05 18:15:14 +0100313 if (ret != SSL_TLS1_3_PSK_IDENTITY_MATCH) {
Gilles Peskine449bd832023-01-11 14:50:10 +0100314 mbedtls_ssl_session_free(session);
315 }
Jerry Yu95699e72022-08-21 19:22:23 +0800316
Gilles Peskine449bd832023-01-11 14:50:10 +0100317 MBEDTLS_SSL_DEBUG_MSG(2, ("<= check_identity_match_ticket"));
318 return ret;
Jerry Yu95699e72022-08-21 19:22:23 +0800319}
320#endif /* MBEDTLS_SSL_SESSION_TICKETS */
321
Jerry Yu6e74a7e2022-07-20 20:49:32 +0800322MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yu1c105562022-07-10 06:32:38 +0000323static int ssl_tls13_offered_psks_check_identity_match(
Gilles Peskine449bd832023-01-11 14:50:10 +0100324 mbedtls_ssl_context *ssl,
325 const unsigned char *identity,
326 size_t identity_len,
327 uint32_t obfuscated_ticket_age,
328 int *psk_type,
329 mbedtls_ssl_session *session)
Jerry Yu1c105562022-07-10 06:32:38 +0000330{
Ronald Cron606671e2023-02-17 11:36:33 +0100331 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
332
Jerry Yu95699e72022-08-21 19:22:23 +0800333 ((void) session);
334 ((void) obfuscated_ticket_age);
Jerry Yu5725f1c2022-08-21 17:27:16 +0800335 *psk_type = MBEDTLS_SSL_TLS1_3_PSK_EXTERNAL;
Jerry Yu95699e72022-08-21 19:22:23 +0800336
Gilles Peskine449bd832023-01-11 14:50:10 +0100337 MBEDTLS_SSL_DEBUG_BUF(4, "identity", identity, identity_len);
Jerry Yu95699e72022-08-21 19:22:23 +0800338 ssl->handshake->resume = 0;
339
Jerry Yu95699e72022-08-21 19:22:23 +0800340#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Ronald Cron7a30cf52024-02-23 14:38:59 +0100341 ret = ssl_tls13_offered_psks_check_identity_match_ticket(
342 ssl, identity, identity_len, obfuscated_ticket_age, session);
343 if (ret == SSL_TLS1_3_PSK_IDENTITY_MATCH) {
Jerry Yu95699e72022-08-21 19:22:23 +0800344 ssl->handshake->resume = 1;
345 *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 Cron6e311272023-12-05 17:57:01 +0100391#define SSL_TLS1_3_BINDER_DOES_NOT_MATCH 1
392#define SSL_TLS1_3_BINDER_MATCH 0
Jerry Yu6e74a7e2022-07-20 20:49:32 +0800393MBEDTLS_CHECK_RETURN_CRITICAL
Xiaokang Qian9f1747b2023-03-30 02:50:04 +0000394static int ssl_tls13_offered_psks_check_binder_match(
395 mbedtls_ssl_context *ssl,
396 const unsigned char *binder, size_t binder_len,
397 int psk_type, psa_algorithm_t psk_hash_alg)
Jerry Yu1c105562022-07-10 06:32:38 +0000398{
399 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yu96a2e362022-07-21 15:11:34 +0800400
Jerry Yudaf375a2022-07-20 21:31:43 +0800401 unsigned char transcript[PSA_HASH_MAX_SIZE];
Jerry Yu1c105562022-07-10 06:32:38 +0000402 size_t transcript_len;
Jerry Yu2f0abc92022-07-22 19:34:48 +0800403 unsigned char *psk;
Jerry Yu96a2e362022-07-21 15:11:34 +0800404 size_t psk_len;
Jerry Yudaf375a2022-07-20 21:31:43 +0800405 unsigned char server_computed_binder[PSA_HASH_MAX_SIZE];
Jerry Yu1c105562022-07-10 06:32:38 +0000406
Jerry Yu1c105562022-07-10 06:32:38 +0000407 /* Get current state of handshake transcript. */
Jerry Yu29d9faa2022-08-23 17:52:45 +0800408 ret = mbedtls_ssl_get_handshake_transcript(
Manuel Pégourié-Gonnard2d6d9932023-03-28 11:38:08 +0200409 ssl, mbedtls_md_type_from_psa_alg(psk_hash_alg),
Gilles Peskine449bd832023-01-11 14:50:10 +0100410 transcript, sizeof(transcript), &transcript_len);
411 if (ret != 0) {
412 return ret;
413 }
Jerry Yu1c105562022-07-10 06:32:38 +0000414
Gilles Peskine449bd832023-01-11 14:50:10 +0100415 ret = mbedtls_ssl_tls13_export_handshake_psk(ssl, &psk, &psk_len);
416 if (ret != 0) {
417 return ret;
418 }
Jerry Yu96a2e362022-07-21 15:11:34 +0800419
Gilles Peskine449bd832023-01-11 14:50:10 +0100420 ret = mbedtls_ssl_tls13_create_psk_binder(ssl, psk_hash_alg,
421 psk, psk_len, psk_type,
422 transcript,
423 server_computed_binder);
Jerry Yu96a2e362022-07-21 15:11:34 +0800424#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +0100425 mbedtls_free((void *) psk);
Jerry Yu96a2e362022-07-21 15:11:34 +0800426#endif
Gilles Peskine449bd832023-01-11 14:50:10 +0100427 if (ret != 0) {
428 MBEDTLS_SSL_DEBUG_MSG(1, ("PSK binder calculation failed."));
429 return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
Jerry Yu1c105562022-07-10 06:32:38 +0000430 }
431
Gilles Peskine449bd832023-01-11 14:50:10 +0100432 MBEDTLS_SSL_DEBUG_BUF(3, "psk binder ( computed ): ",
433 server_computed_binder, transcript_len);
434 MBEDTLS_SSL_DEBUG_BUF(3, "psk binder ( received ): ", binder, binder_len);
Jerry Yu1c105562022-07-10 06:32:38 +0000435
Gilles Peskine449bd832023-01-11 14:50:10 +0100436 if (mbedtls_ct_memcmp(server_computed_binder, binder, binder_len) == 0) {
Ronald Cron6e311272023-12-05 17:57:01 +0100437 return SSL_TLS1_3_BINDER_MATCH;
Jerry Yu1c105562022-07-10 06:32:38 +0000438 }
439
Gilles Peskine449bd832023-01-11 14:50:10 +0100440 mbedtls_platform_zeroize(server_computed_binder,
441 sizeof(server_computed_binder));
Ronald Cron6e311272023-12-05 17:57:01 +0100442 return SSL_TLS1_3_BINDER_DOES_NOT_MATCH;
Jerry Yu1c105562022-07-10 06:32:38 +0000443}
Jerry Yu96a2e362022-07-21 15:11:34 +0800444
Jerry Yu82534862022-08-30 10:42:33 +0800445#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Jerry Yuf35ba382022-08-23 17:58:26 +0800446MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100447static int ssl_tls13_session_copy_ticket(mbedtls_ssl_session *dst,
448 const mbedtls_ssl_session *src)
Jerry Yu82534862022-08-30 10:42:33 +0800449{
Jerry Yu82534862022-08-30 10:42:33 +0800450 dst->ticket_age_add = src->ticket_age_add;
451 dst->ticket_flags = src->ticket_flags;
452 dst->resumption_key_len = src->resumption_key_len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100453 if (src->resumption_key_len == 0) {
454 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
455 }
456 memcpy(dst->resumption_key, src->resumption_key, src->resumption_key_len);
Jerry Yu4746b102022-09-13 11:11:48 +0800457
Jerry Yu33bf2402022-12-12 16:01:43 +0800458#if defined(MBEDTLS_SSL_EARLY_DATA)
459 dst->max_early_data_size = src->max_early_data_size;
460#endif
461
Gilles Peskine449bd832023-01-11 14:50:10 +0100462 return 0;
Jerry Yu82534862022-08-30 10:42:33 +0800463}
464#endif /* MBEDTLS_SSL_SESSION_TICKETS */
465
Jerry Yu1c105562022-07-10 06:32:38 +0000466/* Parser for pre_shared_key extension in client hello
467 * struct {
468 * opaque identity<1..2^16-1>;
469 * uint32 obfuscated_ticket_age;
470 * } PskIdentity;
471 *
472 * opaque PskBinderEntry<32..255>;
473 *
474 * struct {
475 * PskIdentity identities<7..2^16-1>;
476 * PskBinderEntry binders<33..2^16-1>;
477 * } OfferedPsks;
478 *
479 * struct {
480 * select (Handshake.msg_type) {
481 * case client_hello: OfferedPsks;
482 * ....
483 * };
484 * } PreSharedKeyExtension;
485 */
Jerry Yu6e74a7e2022-07-20 20:49:32 +0800486MBEDTLS_CHECK_RETURN_CRITICAL
Xiaokang Qian9f1747b2023-03-30 02:50:04 +0000487static int ssl_tls13_parse_pre_shared_key_ext(
488 mbedtls_ssl_context *ssl,
489 const unsigned char *pre_shared_key_ext,
490 const unsigned char *pre_shared_key_ext_end,
491 const unsigned char *ciphersuites,
492 const unsigned char *ciphersuites_end)
Jerry Yu1c105562022-07-10 06:32:38 +0000493{
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +0100494 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yu29d9faa2022-08-23 17:52:45 +0800495 const unsigned char *identities = pre_shared_key_ext;
Jerry Yu568ec252022-07-22 21:27:34 +0800496 const unsigned char *p_identity_len;
497 size_t identities_len;
Jerry Yu1c105562022-07-10 06:32:38 +0000498 const unsigned char *identities_end;
Jerry Yu568ec252022-07-22 21:27:34 +0800499 const unsigned char *binders;
500 const unsigned char *p_binder_len;
501 size_t binders_len;
Jerry Yu1c105562022-07-10 06:32:38 +0000502 const unsigned char *binders_end;
Jerry Yu96a2e362022-07-21 15:11:34 +0800503 int matched_identity = -1;
504 int identity_id = -1;
Jerry Yu1c105562022-07-10 06:32:38 +0000505
Gilles Peskine449bd832023-01-11 14:50:10 +0100506 MBEDTLS_SSL_DEBUG_BUF(3, "pre_shared_key extension",
507 pre_shared_key_ext,
508 pre_shared_key_ext_end - pre_shared_key_ext);
Jerry Yu1c105562022-07-10 06:32:38 +0000509
Jerry Yu96a2e362022-07-21 15:11:34 +0800510 /* identities_len 2 bytes
511 * identities_data >= 7 bytes
512 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100513 MBEDTLS_SSL_CHK_BUF_READ_PTR(identities, pre_shared_key_ext_end, 7 + 2);
514 identities_len = MBEDTLS_GET_UINT16_BE(identities, 0);
Jerry Yu568ec252022-07-22 21:27:34 +0800515 p_identity_len = identities + 2;
Gilles Peskine449bd832023-01-11 14:50:10 +0100516 MBEDTLS_SSL_CHK_BUF_READ_PTR(p_identity_len, pre_shared_key_ext_end,
517 identities_len);
Jerry Yu568ec252022-07-22 21:27:34 +0800518 identities_end = p_identity_len + identities_len;
Jerry Yu1c105562022-07-10 06:32:38 +0000519
Jerry Yu96a2e362022-07-21 15:11:34 +0800520 /* binders_len 2 bytes
521 * binders >= 33 bytes
522 */
Jerry Yu568ec252022-07-22 21:27:34 +0800523 binders = identities_end;
Gilles Peskine449bd832023-01-11 14:50:10 +0100524 MBEDTLS_SSL_CHK_BUF_READ_PTR(binders, pre_shared_key_ext_end, 33 + 2);
525 binders_len = MBEDTLS_GET_UINT16_BE(binders, 0);
Jerry Yu568ec252022-07-22 21:27:34 +0800526 p_binder_len = binders + 2;
Gilles Peskine449bd832023-01-11 14:50:10 +0100527 MBEDTLS_SSL_CHK_BUF_READ_PTR(p_binder_len, pre_shared_key_ext_end, binders_len);
Jerry Yu568ec252022-07-22 21:27:34 +0800528 binders_end = p_binder_len + binders_len;
Jerry Yu1c105562022-07-10 06:32:38 +0000529
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +0100530 ret = ssl->handshake->update_checksum(ssl, pre_shared_key_ext,
531 identities_end - pre_shared_key_ext);
532 if (0 != ret) {
533 MBEDTLS_SSL_DEBUG_RET(1, ("update_checksum"), ret);
534 return ret;
535 }
Jerry Yu96a2e362022-07-21 15:11:34 +0800536
Gilles Peskine449bd832023-01-11 14:50:10 +0100537 while (p_identity_len < identities_end && p_binder_len < binders_end) {
Jerry Yu1c105562022-07-10 06:32:38 +0000538 const unsigned char *identity;
Jerry Yu568ec252022-07-22 21:27:34 +0800539 size_t identity_len;
Jerry Yu82534862022-08-30 10:42:33 +0800540 uint32_t obfuscated_ticket_age;
Jerry Yu96a2e362022-07-21 15:11:34 +0800541 const unsigned char *binder;
Jerry Yu568ec252022-07-22 21:27:34 +0800542 size_t binder_len;
Jerry Yu5725f1c2022-08-21 17:27:16 +0800543 int psk_type;
Ronald Cron89089cc2024-02-14 18:18:08 +0100544 int psk_ciphersuite_id;
545 psa_algorithm_t psk_hash_alg;
Jerry Yu29d9faa2022-08-23 17:52:45 +0800546 const mbedtls_ssl_ciphersuite_t *ciphersuite_info;
Jerry Yu82534862022-08-30 10:42:33 +0800547#if defined(MBEDTLS_SSL_SESSION_TICKETS)
548 mbedtls_ssl_session session;
Gilles Peskine449bd832023-01-11 14:50:10 +0100549 mbedtls_ssl_session_init(&session);
Jerry Yu82534862022-08-30 10:42:33 +0800550#endif
Jerry Yubb852022022-07-20 21:10:44 +0800551
Gilles Peskine449bd832023-01-11 14:50:10 +0100552 MBEDTLS_SSL_CHK_BUF_READ_PTR(p_identity_len, identities_end, 2 + 1 + 4);
553 identity_len = MBEDTLS_GET_UINT16_BE(p_identity_len, 0);
Jerry Yu568ec252022-07-22 21:27:34 +0800554 identity = p_identity_len + 2;
Gilles Peskine449bd832023-01-11 14:50:10 +0100555 MBEDTLS_SSL_CHK_BUF_READ_PTR(identity, identities_end, identity_len + 4);
556 obfuscated_ticket_age = MBEDTLS_GET_UINT32_BE(identity, identity_len);
Jerry Yu568ec252022-07-22 21:27:34 +0800557 p_identity_len += identity_len + 6;
Jerry Yu1c105562022-07-10 06:32:38 +0000558
Gilles Peskine449bd832023-01-11 14:50:10 +0100559 MBEDTLS_SSL_CHK_BUF_READ_PTR(p_binder_len, binders_end, 1 + 32);
Jerry Yu568ec252022-07-22 21:27:34 +0800560 binder_len = *p_binder_len;
561 binder = p_binder_len + 1;
Gilles Peskine449bd832023-01-11 14:50:10 +0100562 MBEDTLS_SSL_CHK_BUF_READ_PTR(binder, binders_end, binder_len);
Jerry Yu568ec252022-07-22 21:27:34 +0800563 p_binder_len += binder_len + 1;
Jerry Yu96a2e362022-07-21 15:11:34 +0800564
Jerry Yu96a2e362022-07-21 15:11:34 +0800565 identity_id++;
Gilles Peskine449bd832023-01-11 14:50:10 +0100566 if (matched_identity != -1) {
Jerry Yu1c105562022-07-10 06:32:38 +0000567 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +0100568 }
Jerry Yu1c105562022-07-10 06:32:38 +0000569
Jerry Yu96a2e362022-07-21 15:11:34 +0800570 ret = ssl_tls13_offered_psks_check_identity_match(
Gilles Peskine449bd832023-01-11 14:50:10 +0100571 ssl, identity, identity_len, obfuscated_ticket_age,
572 &psk_type, &session);
Ronald Cronfbae94a2023-12-05 18:15:14 +0100573 if (ret != SSL_TLS1_3_PSK_IDENTITY_MATCH) {
Jerry Yu96a2e362022-07-21 15:11:34 +0800574 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +0100575 }
Jerry Yu96a2e362022-07-21 15:11:34 +0800576
Gilles Peskine449bd832023-01-11 14:50:10 +0100577 MBEDTLS_SSL_DEBUG_MSG(4, ("found matched identity"));
Ronald Cron89089cc2024-02-14 18:18:08 +0100578
Gilles Peskine449bd832023-01-11 14:50:10 +0100579 switch (psk_type) {
Jerry Yu0baf9072022-08-25 11:21:04 +0800580 case MBEDTLS_SSL_TLS1_3_PSK_EXTERNAL:
Ronald Cron89089cc2024-02-14 18:18:08 +0100581 psk_ciphersuite_id = 0;
582 psk_hash_alg = PSA_ALG_SHA_256;
Jerry Yu0baf9072022-08-25 11:21:04 +0800583 break;
Jerry Yu82534862022-08-30 10:42:33 +0800584#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Ronald Cronf7e99162024-02-14 16:04:02 +0100585 case MBEDTLS_SSL_TLS1_3_PSK_RESUMPTION:
Ronald Cron89089cc2024-02-14 18:18:08 +0100586 psk_ciphersuite_id = session.ciphersuite;
587 psk_hash_alg = PSA_ALG_NONE;
Jerry Yu0baf9072022-08-25 11:21:04 +0800588 break;
Ronald Cronf7e99162024-02-14 16:04:02 +0100589#endif
Jerry Yu0baf9072022-08-25 11:21:04 +0800590 default:
Gilles Peskine449bd832023-01-11 14:50:10 +0100591 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Jerry Yu0baf9072022-08-25 11:21:04 +0800592 }
Ronald Cron89089cc2024-02-14 18:18:08 +0100593
594 ssl_tls13_select_ciphersuite(ssl, ciphersuites, ciphersuites_end,
595 psk_ciphersuite_id, psk_hash_alg,
596 &ciphersuite_info);
597
598 if (ciphersuite_info == NULL) {
599#if defined(MBEDTLS_SSL_SESSION_TICKETS)
600 mbedtls_ssl_session_free(&session);
601#endif
602 /*
603 * We consider finding a ciphersuite suitable for the PSK as part
604 * of the validation of its binder. Thus if we do not find one, we
605 * abort the handshake with a decrypt_error alert.
606 */
Jerry Yuf35ba382022-08-23 17:58:26 +0800607 MBEDTLS_SSL_PEND_FATAL_ALERT(
608 MBEDTLS_SSL_ALERT_MSG_DECRYPT_ERROR,
Gilles Peskine449bd832023-01-11 14:50:10 +0100609 MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE);
Ronald Cron89089cc2024-02-14 18:18:08 +0100610 return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
Jerry Yu5725f1c2022-08-21 17:27:16 +0800611 }
612
Jerry Yu96a2e362022-07-21 15:11:34 +0800613 ret = ssl_tls13_offered_psks_check_binder_match(
Gilles Peskine449bd832023-01-11 14:50:10 +0100614 ssl, binder, binder_len, psk_type,
Dave Rodgman2eab4622023-10-05 13:30:37 +0100615 mbedtls_md_psa_alg_from_type((mbedtls_md_type_t) ciphersuite_info->mac));
Ronald Cron6e311272023-12-05 17:57:01 +0100616 if (ret != SSL_TLS1_3_BINDER_MATCH) {
Jerry Yuc5a23a02022-08-25 10:51:44 +0800617 /* For security reasons, the handshake should be aborted when we
618 * fail to validate a binder value. See RFC 8446 section 4.2.11.2
619 * and appendix E.6. */
Jerry Yu82534862022-08-30 10:42:33 +0800620#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Gilles Peskine449bd832023-01-11 14:50:10 +0100621 mbedtls_ssl_session_free(&session);
Jerry Yu82534862022-08-30 10:42:33 +0800622#endif
Gilles Peskine449bd832023-01-11 14:50:10 +0100623 MBEDTLS_SSL_DEBUG_MSG(3, ("Invalid binder."));
Xiaokang Qian9f1747b2023-03-30 02:50:04 +0000624 MBEDTLS_SSL_DEBUG_RET(
625 1, "ssl_tls13_offered_psks_check_binder_match", ret);
Jerry Yu96a2e362022-07-21 15:11:34 +0800626 MBEDTLS_SSL_PEND_FATAL_ALERT(
627 MBEDTLS_SSL_ALERT_MSG_DECRYPT_ERROR,
Gilles Peskine449bd832023-01-11 14:50:10 +0100628 MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE);
629 return ret;
Jerry Yu96a2e362022-07-21 15:11:34 +0800630 }
Jerry Yu96a2e362022-07-21 15:11:34 +0800631
632 matched_identity = identity_id;
Jerry Yu5725f1c2022-08-21 17:27:16 +0800633
634 /* Update handshake parameters */
Jerry Yue5834fd2022-08-29 20:16:09 +0800635 ssl->handshake->ciphersuite_info = ciphersuite_info;
Ronald Cron89089cc2024-02-14 18:18:08 +0100636 ssl->session_negotiate->ciphersuite = ciphersuite_info->id;
Gilles Peskine449bd832023-01-11 14:50:10 +0100637 MBEDTLS_SSL_DEBUG_MSG(2, ("overwrite ciphersuite: %04x - %s",
Ronald Cron89089cc2024-02-14 18:18:08 +0100638 ((unsigned) ciphersuite_info->id),
639 ciphersuite_info->name));
Jerry Yu82534862022-08-30 10:42:33 +0800640#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Gilles Peskine449bd832023-01-11 14:50:10 +0100641 if (psk_type == MBEDTLS_SSL_TLS1_3_PSK_RESUMPTION) {
642 ret = ssl_tls13_session_copy_ticket(ssl->session_negotiate,
643 &session);
644 mbedtls_ssl_session_free(&session);
645 if (ret != 0) {
646 return ret;
647 }
Jerry Yu82534862022-08-30 10:42:33 +0800648 }
649#endif /* MBEDTLS_SSL_SESSION_TICKETS */
Jerry Yu1c105562022-07-10 06:32:38 +0000650 }
651
Gilles Peskine449bd832023-01-11 14:50:10 +0100652 if (p_identity_len != identities_end || p_binder_len != binders_end) {
653 MBEDTLS_SSL_DEBUG_MSG(3, ("pre_shared_key extension decode error"));
654 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR,
655 MBEDTLS_ERR_SSL_DECODE_ERROR);
656 return MBEDTLS_ERR_SSL_DECODE_ERROR;
Jerry Yu1c105562022-07-10 06:32:38 +0000657 }
658
Jerry Yu6f1db3f2022-07-22 23:05:59 +0800659 /* Update the handshake transcript with the binder list. */
Xiaokang Qian9f1747b2023-03-30 02:50:04 +0000660 ret = ssl->handshake->update_checksum(
661 ssl, identities_end, (size_t) (binders_end - identities_end));
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +0100662 if (0 != ret) {
663 MBEDTLS_SSL_DEBUG_RET(1, ("update_checksum"), ret);
664 return ret;
665 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100666 if (matched_identity == -1) {
667 MBEDTLS_SSL_DEBUG_MSG(3, ("No matched PSK or ticket."));
668 return MBEDTLS_ERR_SSL_UNKNOWN_IDENTITY;
Jerry Yu1c105562022-07-10 06:32:38 +0000669 }
670
Gilles Peskine449bd832023-01-11 14:50:10 +0100671 ssl->handshake->selected_identity = (uint16_t) matched_identity;
672 MBEDTLS_SSL_DEBUG_MSG(3, ("Pre shared key found"));
Jerry Yu1c105562022-07-10 06:32:38 +0000673
Gilles Peskine449bd832023-01-11 14:50:10 +0100674 return 0;
Jerry Yu1c105562022-07-10 06:32:38 +0000675}
Jerry Yu032b15ce2022-07-11 06:10:03 +0000676
677/*
678 * struct {
679 * select ( Handshake.msg_type ) {
680 * ....
681 * case server_hello:
682 * uint16 selected_identity;
683 * }
684 * } PreSharedKeyExtension;
685 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100686static int ssl_tls13_write_server_pre_shared_key_ext(mbedtls_ssl_context *ssl,
687 unsigned char *buf,
688 unsigned char *end,
689 size_t *olen)
Jerry Yu032b15ce2022-07-11 06:10:03 +0000690{
Gilles Peskine449bd832023-01-11 14:50:10 +0100691 unsigned char *p = (unsigned char *) buf;
Jerry Yu032b15ce2022-07-11 06:10:03 +0000692
693 *olen = 0;
694
David Horstmann21b89762022-10-06 18:34:28 +0100695 int not_using_psk = 0;
Jerry Yu032b15ce2022-07-11 06:10:03 +0000696#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +0100697 not_using_psk = (mbedtls_svc_key_id_is_null(ssl->handshake->psk_opaque));
Jerry Yu032b15ce2022-07-11 06:10:03 +0000698#else
Gilles Peskine449bd832023-01-11 14:50:10 +0100699 not_using_psk = (ssl->handshake->psk == NULL);
Jerry Yu032b15ce2022-07-11 06:10:03 +0000700#endif
Gilles Peskine449bd832023-01-11 14:50:10 +0100701 if (not_using_psk) {
Jerry Yu032b15ce2022-07-11 06:10:03 +0000702 /* We shouldn't have called this extension writer unless we've
703 * chosen to use a PSK. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100704 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Jerry Yu032b15ce2022-07-11 06:10:03 +0000705 }
706
Gilles Peskine449bd832023-01-11 14:50:10 +0100707 MBEDTLS_SSL_DEBUG_MSG(3, ("server hello, adding pre_shared_key extension"));
708 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 6);
Jerry Yu032b15ce2022-07-11 06:10:03 +0000709
Gilles Peskine449bd832023-01-11 14:50:10 +0100710 MBEDTLS_PUT_UINT16_BE(MBEDTLS_TLS_EXT_PRE_SHARED_KEY, p, 0);
711 MBEDTLS_PUT_UINT16_BE(2, p, 2);
Jerry Yu032b15ce2022-07-11 06:10:03 +0000712
Gilles Peskine449bd832023-01-11 14:50:10 +0100713 MBEDTLS_PUT_UINT16_BE(ssl->handshake->selected_identity, p, 4);
Jerry Yu032b15ce2022-07-11 06:10:03 +0000714
715 *olen = 6;
716
Gilles Peskine449bd832023-01-11 14:50:10 +0100717 MBEDTLS_SSL_DEBUG_MSG(4, ("sent selected_identity: %u",
718 ssl->handshake->selected_identity));
Jerry Yu032b15ce2022-07-11 06:10:03 +0000719
Gilles Peskine449bd832023-01-11 14:50:10 +0100720 mbedtls_ssl_tls13_set_hs_sent_ext_mask(ssl, MBEDTLS_TLS_EXT_PRE_SHARED_KEY);
Jerry Yub95dd362022-11-08 21:19:34 +0800721
Gilles Peskine449bd832023-01-11 14:50:10 +0100722 return 0;
Jerry Yu032b15ce2022-07-11 06:10:03 +0000723}
724
Ronald Cron41a443a2022-10-04 16:38:25 +0200725#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED */
Jerry Yue19e3b92022-07-08 12:04:51 +0000726
XiaokangQian7807f9f2022-02-15 10:04:37 +0000727/* From RFC 8446:
728 * struct {
XiaokangQiancfd925f2022-04-14 07:10:37 +0000729 * ProtocolVersion versions<2..254>;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000730 * } SupportedVersions;
731 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200732MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100733static int ssl_tls13_parse_supported_versions_ext(mbedtls_ssl_context *ssl,
734 const unsigned char *buf,
735 const unsigned char *end)
XiaokangQian7807f9f2022-02-15 10:04:37 +0000736{
XiaokangQian7807f9f2022-02-15 10:04:37 +0000737 const unsigned char *p = buf;
XiaokangQian8f9dfe42022-04-15 02:52:39 +0000738 size_t versions_len;
XiaokangQian4080a7f2022-04-11 09:55:18 +0000739 const unsigned char *versions_end;
XiaokangQian0a1b54e2022-04-21 03:01:38 +0000740 uint16_t tls_version;
Ronald Cron5af4c7f2023-03-07 20:46:59 +0100741 int found_supported_version = 0;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000742
Gilles Peskine449bd832023-01-11 14:50:10 +0100743 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, 1);
XiaokangQian4080a7f2022-04-11 09:55:18 +0000744 versions_len = p[0];
XiaokangQian7807f9f2022-02-15 10:04:37 +0000745 p += 1;
746
Gilles Peskine449bd832023-01-11 14:50:10 +0100747 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, versions_len);
XiaokangQian4080a7f2022-04-11 09:55:18 +0000748 versions_end = p + versions_len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100749 while (p < versions_end) {
750 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, versions_end, 2);
751 tls_version = mbedtls_ssl_read_version(p, ssl->conf->transport);
XiaokangQianb67384d2022-04-19 00:02:38 +0000752 p += 2;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000753
Ronald Cron3bd2b022023-04-03 16:45:39 +0200754 if (MBEDTLS_SSL_VERSION_TLS1_3 == tls_version) {
Ronald Cron5af4c7f2023-03-07 20:46:59 +0100755 found_supported_version = 1;
756 break;
757 }
758
Ronald Cron3bd2b022023-04-03 16:45:39 +0200759 if ((MBEDTLS_SSL_VERSION_TLS1_2 == tls_version) &&
760 mbedtls_ssl_conf_is_tls12_enabled(ssl->conf)) {
Ronald Cron5af4c7f2023-03-07 20:46:59 +0100761 found_supported_version = 1;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000762 break;
763 }
XiaokangQian7807f9f2022-02-15 10:04:37 +0000764 }
765
Ronald Cron5af4c7f2023-03-07 20:46:59 +0100766 if (!found_supported_version) {
767 MBEDTLS_SSL_DEBUG_MSG(1, ("No supported version found."));
XiaokangQian7807f9f2022-02-15 10:04:37 +0000768
Gilles Peskine449bd832023-01-11 14:50:10 +0100769 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_PROTOCOL_VERSION,
770 MBEDTLS_ERR_SSL_BAD_PROTOCOL_VERSION);
771 return MBEDTLS_ERR_SSL_BAD_PROTOCOL_VERSION;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000772 }
773
Ronald Cron5af4c7f2023-03-07 20:46:59 +0100774 MBEDTLS_SSL_DEBUG_MSG(1, ("Negotiated version: [%04x]",
Gilles Peskine449bd832023-01-11 14:50:10 +0100775 (unsigned int) tls_version));
XiaokangQian7807f9f2022-02-15 10:04:37 +0000776
Ronald Cron5af4c7f2023-03-07 20:46:59 +0100777 return (int) tls_version;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000778}
779
Przemek Stekiel8c0a9532023-06-15 16:48:19 +0200780#if defined(PSA_WANT_ALG_ECDH) || defined(PSA_WANT_ALG_FFDH)
XiaokangQiane8ff3502022-04-22 02:34:40 +0000781/*
XiaokangQian7807f9f2022-02-15 10:04:37 +0000782 *
783 * From RFC 8446:
784 * enum {
785 * ... (0xFFFF)
786 * } NamedGroup;
787 * struct {
788 * NamedGroup named_group_list<2..2^16-1>;
789 * } NamedGroupList;
790 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200791MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100792static int ssl_tls13_parse_supported_groups_ext(mbedtls_ssl_context *ssl,
793 const unsigned char *buf,
794 const unsigned char *end)
XiaokangQian7807f9f2022-02-15 10:04:37 +0000795{
XiaokangQian7807f9f2022-02-15 10:04:37 +0000796 const unsigned char *p = buf;
XiaokangQian84823772022-04-19 07:57:30 +0000797 size_t named_group_list_len;
XiaokangQian8f9dfe42022-04-15 02:52:39 +0000798 const unsigned char *named_group_list_end;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000799
Gilles Peskine449bd832023-01-11 14:50:10 +0100800 MBEDTLS_SSL_DEBUG_BUF(3, "supported_groups extension", p, end - buf);
801 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, 2);
802 named_group_list_len = MBEDTLS_GET_UINT16_BE(p, 0);
XiaokangQian7807f9f2022-02-15 10:04:37 +0000803 p += 2;
Gilles Peskine449bd832023-01-11 14:50:10 +0100804 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, named_group_list_len);
XiaokangQian8f9dfe42022-04-15 02:52:39 +0000805 named_group_list_end = p + named_group_list_len;
XiaokangQian4e8cd7b2022-04-21 09:48:09 +0000806 ssl->handshake->hrr_selected_group = 0;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000807
Gilles Peskine449bd832023-01-11 14:50:10 +0100808 while (p < named_group_list_end) {
XiaokangQian08037552022-04-20 07:16:41 +0000809 uint16_t named_group;
Gilles Peskine449bd832023-01-11 14:50:10 +0100810 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, named_group_list_end, 2);
811 named_group = MBEDTLS_GET_UINT16_BE(p, 0);
XiaokangQian08037552022-04-20 07:16:41 +0000812 p += 2;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000813
Gilles Peskine449bd832023-01-11 14:50:10 +0100814 MBEDTLS_SSL_DEBUG_MSG(2,
815 ("got named group: %s(%04x)",
816 mbedtls_ssl_named_group_to_str(named_group),
817 named_group));
XiaokangQian08037552022-04-20 07:16:41 +0000818
Gilles Peskine449bd832023-01-11 14:50:10 +0100819 if (!mbedtls_ssl_named_group_is_offered(ssl, named_group) ||
820 !mbedtls_ssl_named_group_is_supported(named_group) ||
821 ssl->handshake->hrr_selected_group != 0) {
XiaokangQian08037552022-04-20 07:16:41 +0000822 continue;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000823 }
824
Gilles Peskine449bd832023-01-11 14:50:10 +0100825 MBEDTLS_SSL_DEBUG_MSG(2,
826 ("add named group %s(%04x) into received list.",
827 mbedtls_ssl_named_group_to_str(named_group),
828 named_group));
Jerry Yuc1be19f2022-04-23 16:11:39 +0800829
XiaokangQian4e8cd7b2022-04-21 09:48:09 +0000830 ssl->handshake->hrr_selected_group = named_group;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000831 }
832
Gilles Peskine449bd832023-01-11 14:50:10 +0100833 return 0;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000834
835}
Przemek Stekiel8c0a9532023-06-15 16:48:19 +0200836#endif /* PSA_WANT_ALG_ECDH || PSA_WANT_ALG_FFDH */
XiaokangQian7807f9f2022-02-15 10:04:37 +0000837
XiaokangQian08037552022-04-20 07:16:41 +0000838#define SSL_TLS1_3_PARSE_KEY_SHARES_EXT_NO_MATCH 1
839
Valerio Settic9ae8622023-07-25 11:23:50 +0200840#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_EPHEMERAL_ENABLED)
XiaokangQian7807f9f2022-02-15 10:04:37 +0000841/*
842 * ssl_tls13_parse_key_shares_ext() verifies whether the information in the
Xiaokang Qian9f1747b2023-03-30 02:50:04 +0000843 * extension is correct and stores the first acceptable key share and its
844 * associated group.
XiaokangQian7807f9f2022-02-15 10:04:37 +0000845 *
846 * Possible return values are:
847 * - 0: Successful processing of the client provided key share extension.
Xiaokang Qian9f1747b2023-03-30 02:50:04 +0000848 * - SSL_TLS1_3_PARSE_KEY_SHARES_EXT_NO_MATCH: The key shares provided by
849 * the client does not match a group supported by the server. A
850 * HelloRetryRequest will be needed.
XiaokangQiane8ff3502022-04-22 02:34:40 +0000851 * - A negative value for fatal errors.
Jerry Yuc1be19f2022-04-23 16:11:39 +0800852 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200853MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100854static int ssl_tls13_parse_key_shares_ext(mbedtls_ssl_context *ssl,
855 const unsigned char *buf,
856 const unsigned char *end)
XiaokangQian7807f9f2022-02-15 10:04:37 +0000857{
XiaokangQianb67384d2022-04-19 00:02:38 +0000858 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000859 unsigned char const *p = buf;
XiaokangQian8f9dfe42022-04-15 02:52:39 +0000860 unsigned char const *client_shares_end;
Jerry Yuc1be19f2022-04-23 16:11:39 +0800861 size_t client_shares_len;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000862
863 /* From RFC 8446:
864 *
865 * struct {
866 * KeyShareEntry client_shares<0..2^16-1>;
867 * } KeyShareClientHello;
868 *
869 */
870
Gilles Peskine449bd832023-01-11 14:50:10 +0100871 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, 2);
872 client_shares_len = MBEDTLS_GET_UINT16_BE(p, 0);
XiaokangQian7807f9f2022-02-15 10:04:37 +0000873 p += 2;
Gilles Peskine449bd832023-01-11 14:50:10 +0100874 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, client_shares_len);
XiaokangQian7807f9f2022-02-15 10:04:37 +0000875
876 ssl->handshake->offered_group_id = 0;
XiaokangQian8f9dfe42022-04-15 02:52:39 +0000877 client_shares_end = p + client_shares_len;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000878
879 /* We try to find a suitable key share entry and copy it to the
880 * handshake context. Later, we have to find out whether we can do
881 * something with the provided key share or whether we have to
XiaokangQianc5763b52022-04-02 03:34:37 +0000882 * dismiss it and send a HelloRetryRequest message.
883 */
XiaokangQian7807f9f2022-02-15 10:04:37 +0000884
Gilles Peskine449bd832023-01-11 14:50:10 +0100885 while (p < client_shares_end) {
XiaokangQian9b5d04b2022-04-10 10:20:43 +0000886 uint16_t group;
Jerry Yuc1be19f2022-04-23 16:11:39 +0800887 size_t key_exchange_len;
Jerry Yu086edc22022-05-05 10:50:38 +0800888 const unsigned char *key_exchange;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000889
890 /*
891 * struct {
892 * NamedGroup group;
893 * opaque key_exchange<1..2^16-1>;
894 * } KeyShareEntry;
895 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100896 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, client_shares_end, 4);
897 group = MBEDTLS_GET_UINT16_BE(p, 0);
898 key_exchange_len = MBEDTLS_GET_UINT16_BE(p, 2);
Jerry Yuc1be19f2022-04-23 16:11:39 +0800899 p += 4;
Jerry Yu086edc22022-05-05 10:50:38 +0800900 key_exchange = p;
Gilles Peskine449bd832023-01-11 14:50:10 +0100901 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, client_shares_end, key_exchange_len);
Jerry Yu086edc22022-05-05 10:50:38 +0800902 p += key_exchange_len;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000903
904 /* Continue parsing even if we have already found a match,
XiaokangQianc5763b52022-04-02 03:34:37 +0000905 * for input validation purposes.
906 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100907 if (!mbedtls_ssl_named_group_is_offered(ssl, group) ||
908 !mbedtls_ssl_named_group_is_supported(group) ||
909 ssl->handshake->offered_group_id != 0) {
XiaokangQian060d8672022-04-21 09:24:56 +0000910 continue;
911 }
XiaokangQian060d8672022-04-21 09:24:56 +0000912
XiaokangQian7807f9f2022-02-15 10:04:37 +0000913 /*
Przemek Stekielc89f3ea2023-05-18 15:45:53 +0200914 * ECDHE and FFDHE groups are supported
XiaokangQian7807f9f2022-02-15 10:04:37 +0000915 */
Przemek Stekielc89f3ea2023-05-18 15:45:53 +0200916 if (mbedtls_ssl_tls13_named_group_is_ecdhe(group) ||
Przemek Stekield5f79e72023-06-29 09:08:43 +0200917 mbedtls_ssl_tls13_named_group_is_ffdh(group)) {
Przemek Stekielc89f3ea2023-05-18 15:45:53 +0200918 MBEDTLS_SSL_DEBUG_MSG(2, ("ECDH/FFDH group: %s (%04x)",
Gilles Peskine449bd832023-01-11 14:50:10 +0100919 mbedtls_ssl_named_group_to_str(group),
920 group));
Przemek Stekiel7ac93be2023-07-04 10:02:38 +0200921 ret = mbedtls_ssl_tls13_read_public_xxdhe_share(
Gilles Peskine449bd832023-01-11 14:50:10 +0100922 ssl, key_exchange - 2, key_exchange_len + 2);
923 if (ret != 0) {
924 return ret;
925 }
XiaokangQian4e8cd7b2022-04-21 09:48:09 +0000926
Gilles Peskine449bd832023-01-11 14:50:10 +0100927 } else {
928 MBEDTLS_SSL_DEBUG_MSG(4, ("Unrecognized NamedGroup %u",
929 (unsigned) group));
XiaokangQian7807f9f2022-02-15 10:04:37 +0000930 continue;
931 }
932
XiaokangQian9b5d04b2022-04-10 10:20:43 +0000933 ssl->handshake->offered_group_id = group;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000934 }
935
Jerry Yuc1be19f2022-04-23 16:11:39 +0800936
Gilles Peskine449bd832023-01-11 14:50:10 +0100937 if (ssl->handshake->offered_group_id == 0) {
938 MBEDTLS_SSL_DEBUG_MSG(1, ("no matching key share"));
939 return SSL_TLS1_3_PARSE_KEY_SHARES_EXT_NO_MATCH;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000940 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100941 return 0;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000942}
Valerio Settic9ae8622023-07-25 11:23:50 +0200943#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_EPHEMERAL_ENABLED */
XiaokangQian7807f9f2022-02-15 10:04:37 +0000944
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200945MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100946static int ssl_tls13_client_hello_has_exts(mbedtls_ssl_context *ssl,
947 int exts_mask)
XiaokangQian7807f9f2022-02-15 10:04:37 +0000948{
Jerry Yu0c354a22022-08-29 15:25:36 +0800949 int masked = ssl->handshake->received_extensions & exts_mask;
Gilles Peskine449bd832023-01-11 14:50:10 +0100950 return masked == exts_mask;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000951}
952
Jerry Yue5991322022-11-07 14:03:44 +0800953#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200954MBEDTLS_CHECK_RETURN_CRITICAL
XiaokangQianb67384d2022-04-19 00:02:38 +0000955static int ssl_tls13_client_hello_has_exts_for_ephemeral_key_exchange(
Gilles Peskine449bd832023-01-11 14:50:10 +0100956 mbedtls_ssl_context *ssl)
XiaokangQian7807f9f2022-02-15 10:04:37 +0000957{
Gilles Peskine449bd832023-01-11 14:50:10 +0100958 return ssl_tls13_client_hello_has_exts(
959 ssl,
960 MBEDTLS_SSL_EXT_MASK(SUPPORTED_GROUPS) |
961 MBEDTLS_SSL_EXT_MASK(KEY_SHARE) |
962 MBEDTLS_SSL_EXT_MASK(SIG_ALG));
XiaokangQian7807f9f2022-02-15 10:04:37 +0000963}
Jerry Yue5991322022-11-07 14:03:44 +0800964#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED */
XiaokangQian7807f9f2022-02-15 10:04:37 +0000965
Jerry Yue5991322022-11-07 14:03:44 +0800966#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK_ENABLED)
Jerry Yu77f01482022-07-11 07:03:24 +0000967MBEDTLS_CHECK_RETURN_CRITICAL
968static int ssl_tls13_client_hello_has_exts_for_psk_key_exchange(
Gilles Peskine449bd832023-01-11 14:50:10 +0100969 mbedtls_ssl_context *ssl)
Jerry Yu77f01482022-07-11 07:03:24 +0000970{
Gilles Peskine449bd832023-01-11 14:50:10 +0100971 return ssl_tls13_client_hello_has_exts(
972 ssl,
973 MBEDTLS_SSL_EXT_MASK(PRE_SHARED_KEY) |
974 MBEDTLS_SSL_EXT_MASK(PSK_KEY_EXCHANGE_MODES));
Jerry Yu77f01482022-07-11 07:03:24 +0000975}
Jerry Yue5991322022-11-07 14:03:44 +0800976#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK_ENABLED */
Jerry Yu77f01482022-07-11 07:03:24 +0000977
Jerry Yue5991322022-11-07 14:03:44 +0800978#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK_EPHEMERAL_ENABLED)
Jerry Yu77f01482022-07-11 07:03:24 +0000979MBEDTLS_CHECK_RETURN_CRITICAL
980static int ssl_tls13_client_hello_has_exts_for_psk_ephemeral_key_exchange(
Gilles Peskine449bd832023-01-11 14:50:10 +0100981 mbedtls_ssl_context *ssl)
Jerry Yu77f01482022-07-11 07:03:24 +0000982{
Gilles Peskine449bd832023-01-11 14:50:10 +0100983 return ssl_tls13_client_hello_has_exts(
984 ssl,
985 MBEDTLS_SSL_EXT_MASK(SUPPORTED_GROUPS) |
986 MBEDTLS_SSL_EXT_MASK(KEY_SHARE) |
987 MBEDTLS_SSL_EXT_MASK(PRE_SHARED_KEY) |
988 MBEDTLS_SSL_EXT_MASK(PSK_KEY_EXCHANGE_MODES));
Jerry Yu77f01482022-07-11 07:03:24 +0000989}
Jerry Yue5991322022-11-07 14:03:44 +0800990#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK_EPHEMERAL_ENABLED */
Jerry Yu77f01482022-07-11 07:03:24 +0000991
Pengyu Lv306a01d2023-02-02 16:35:47 +0800992#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED)
993MBEDTLS_CHECK_RETURN_CRITICAL
Pengyu Lvd72e8582023-11-10 10:37:18 +0800994static int ssl_tls13_ticket_is_kex_mode_permitted(mbedtls_ssl_context *ssl,
995 unsigned int kex_mode)
Pengyu Lv306a01d2023-02-02 16:35:47 +0800996{
997#if defined(MBEDTLS_SSL_SESSION_TICKETS)
998 if (ssl->handshake->resume) {
Pengyu Lv94a42cc2023-12-06 10:04:17 +0800999 if (!mbedtls_ssl_tls13_session_ticket_has_flags(
Pengyu Lv306a01d2023-02-02 16:35:47 +08001000 ssl->session_negotiate, kex_mode)) {
1001 return 0;
1002 }
1003 }
1004#else
1005 ((void) ssl);
1006 ((void) kex_mode);
1007#endif
1008 return 1;
1009}
1010#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED */
1011
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001012MBEDTLS_CHECK_RETURN_CRITICAL
Pengyu Lvbc4aab72023-12-01 15:37:24 +08001013static int ssl_tls13_key_exchange_is_ephemeral_available(mbedtls_ssl_context *ssl)
XiaokangQian7807f9f2022-02-15 10:04:37 +00001014{
Jerry Yue5991322022-11-07 14:03:44 +08001015#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED)
Pengyu Lv0a1ff2b2023-11-14 11:03:32 +08001016 return mbedtls_ssl_conf_tls13_is_ephemeral_enabled(ssl) &&
Gilles Peskine449bd832023-01-11 14:50:10 +01001017 ssl_tls13_client_hello_has_exts_for_ephemeral_key_exchange(ssl);
Jerry Yue5991322022-11-07 14:03:44 +08001018#else
1019 ((void) ssl);
Gilles Peskine449bd832023-01-11 14:50:10 +01001020 return 0;
Jerry Yue5991322022-11-07 14:03:44 +08001021#endif
Jerry Yu77f01482022-07-11 07:03:24 +00001022}
XiaokangQian7807f9f2022-02-15 10:04:37 +00001023
Jerry Yu77f01482022-07-11 07:03:24 +00001024MBEDTLS_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)
Pengyu Lvd72e8582023-11-10 10:37:18 +08001028 return ssl_tls13_ticket_is_kex_mode_permitted(
Pengyu Lv306a01d2023-02-02 16:35:47 +08001029 ssl, MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK) &&
Pengyu Lv0a1ff2b2023-11-14 11:03:32 +08001030 mbedtls_ssl_conf_tls13_is_psk_enabled(ssl) &&
Pengyu Lvb2cfafb2023-11-14 13:56:13 +08001031 mbedtls_ssl_tls13_is_psk_supported(ssl) &&
Gilles Peskine449bd832023-01-11 14:50:10 +01001032 ssl_tls13_client_hello_has_exts_for_psk_key_exchange(ssl);
Jerry Yu77f01482022-07-11 07:03:24 +00001033#else
1034 ((void) ssl);
Gilles Peskine449bd832023-01-11 14:50:10 +01001035 return 0;
Jerry Yu77f01482022-07-11 07:03:24 +00001036#endif
1037}
XiaokangQian7807f9f2022-02-15 10:04:37 +00001038
Jerry Yu77f01482022-07-11 07:03:24 +00001039MBEDTLS_CHECK_RETURN_CRITICAL
Pengyu Lvbc4aab72023-12-01 15:37:24 +08001040static int ssl_tls13_key_exchange_is_psk_ephemeral_available(mbedtls_ssl_context *ssl)
Jerry Yu77f01482022-07-11 07:03:24 +00001041{
Jerry Yue5991322022-11-07 14:03:44 +08001042#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK_EPHEMERAL_ENABLED)
Pengyu Lvd72e8582023-11-10 10:37:18 +08001043 return ssl_tls13_ticket_is_kex_mode_permitted(
Pengyu Lv306a01d2023-02-02 16:35:47 +08001044 ssl, MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK_EPHEMERAL) &&
Pengyu Lv0a1ff2b2023-11-14 11:03:32 +08001045 mbedtls_ssl_conf_tls13_is_psk_ephemeral_enabled(ssl) &&
Pengyu Lvb2cfafb2023-11-14 13:56:13 +08001046 mbedtls_ssl_tls13_is_psk_ephemeral_supported(ssl) &&
Gilles Peskine449bd832023-01-11 14:50:10 +01001047 ssl_tls13_client_hello_has_exts_for_psk_ephemeral_key_exchange(ssl);
Jerry Yu77f01482022-07-11 07:03:24 +00001048#else
1049 ((void) ssl);
Gilles Peskine449bd832023-01-11 14:50:10 +01001050 return 0;
Jerry Yu77f01482022-07-11 07:03:24 +00001051#endif
1052}
1053
Gilles Peskine449bd832023-01-11 14:50:10 +01001054static int ssl_tls13_determine_key_exchange_mode(mbedtls_ssl_context *ssl)
Jerry Yu77f01482022-07-11 07:03:24 +00001055{
1056 /*
1057 * Determine the key exchange algorithm to use.
1058 * There are three types of key exchanges supported in TLS 1.3:
1059 * - (EC)DH with ECDSA,
1060 * - (EC)DH with PSK,
1061 * - plain PSK.
1062 *
1063 * The PSK-based key exchanges may additionally be used with 0-RTT.
1064 *
1065 * Our built-in order of preference is
Jerry Yuce6ed702022-07-22 21:49:53 +08001066 * 1 ) (EC)DHE-PSK Mode ( psk_ephemeral )
1067 * 2 ) Certificate Mode ( ephemeral )
1068 * 3 ) Plain PSK Mode ( psk )
Jerry Yu77f01482022-07-11 07:03:24 +00001069 */
1070
Xiaokang Qian9f1747b2023-03-30 02:50:04 +00001071 ssl->handshake->key_exchange_mode =
1072 MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_NONE;
Jerry Yu77f01482022-07-11 07:03:24 +00001073
Pengyu Lvbc4aab72023-12-01 15:37:24 +08001074 if (ssl_tls13_key_exchange_is_psk_ephemeral_available(ssl)) {
Jerry Yu77f01482022-07-11 07:03:24 +00001075 ssl->handshake->key_exchange_mode =
1076 MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK_EPHEMERAL;
Gilles Peskine449bd832023-01-11 14:50:10 +01001077 MBEDTLS_SSL_DEBUG_MSG(2, ("key exchange mode: psk_ephemeral"));
1078 } else
Pengyu Lvbc4aab72023-12-01 15:37:24 +08001079 if (ssl_tls13_key_exchange_is_ephemeral_available(ssl)) {
Jerry Yu77f01482022-07-11 07:03:24 +00001080 ssl->handshake->key_exchange_mode =
1081 MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL;
Gilles Peskine449bd832023-01-11 14:50:10 +01001082 MBEDTLS_SSL_DEBUG_MSG(2, ("key exchange mode: ephemeral"));
1083 } else
Pengyu Lvbc4aab72023-12-01 15:37:24 +08001084 if (ssl_tls13_key_exchange_is_psk_available(ssl)) {
Jerry Yuce6ed702022-07-22 21:49:53 +08001085 ssl->handshake->key_exchange_mode =
1086 MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK;
Gilles Peskine449bd832023-01-11 14:50:10 +01001087 MBEDTLS_SSL_DEBUG_MSG(2, ("key exchange mode: psk"));
1088 } else {
Jerry Yu77f01482022-07-11 07:03:24 +00001089 MBEDTLS_SSL_DEBUG_MSG(
Gilles Peskine449bd832023-01-11 14:50:10 +01001090 1,
1091 ("ClientHello message misses mandatory extensions."));
1092 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_MISSING_EXTENSION,
1093 MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER);
1094 return MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER;
Jerry Yu77f01482022-07-11 07:03:24 +00001095 }
1096
Gilles Peskine449bd832023-01-11 14:50:10 +01001097 return 0;
Jerry Yu77f01482022-07-11 07:03:24 +00001098
XiaokangQian7807f9f2022-02-15 10:04:37 +00001099}
1100
XiaokangQian81802f42022-06-10 13:25:22 +00001101#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
Ronald Cron928cbd32022-10-04 16:14:26 +02001102 defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED)
Ronald Cron67ea2542022-09-15 17:34:42 +02001103
1104#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01001105static psa_algorithm_t ssl_tls13_iana_sig_alg_to_psa_alg(uint16_t sig_alg)
Ronald Cron67ea2542022-09-15 17:34:42 +02001106{
Gilles Peskine449bd832023-01-11 14:50:10 +01001107 switch (sig_alg) {
Ronald Cron67ea2542022-09-15 17:34:42 +02001108 case MBEDTLS_TLS1_3_SIG_ECDSA_SECP256R1_SHA256:
Gilles Peskine449bd832023-01-11 14:50:10 +01001109 return PSA_ALG_ECDSA(PSA_ALG_SHA_256);
Ronald Cron67ea2542022-09-15 17:34:42 +02001110 case MBEDTLS_TLS1_3_SIG_ECDSA_SECP384R1_SHA384:
Gilles Peskine449bd832023-01-11 14:50:10 +01001111 return PSA_ALG_ECDSA(PSA_ALG_SHA_384);
Ronald Cron67ea2542022-09-15 17:34:42 +02001112 case MBEDTLS_TLS1_3_SIG_ECDSA_SECP521R1_SHA512:
Gilles Peskine449bd832023-01-11 14:50:10 +01001113 return PSA_ALG_ECDSA(PSA_ALG_SHA_512);
Ronald Cron67ea2542022-09-15 17:34:42 +02001114 case MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA256:
Gilles Peskine449bd832023-01-11 14:50:10 +01001115 return PSA_ALG_RSA_PSS(PSA_ALG_SHA_256);
Ronald Cron67ea2542022-09-15 17:34:42 +02001116 case MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA384:
Gilles Peskine449bd832023-01-11 14:50:10 +01001117 return PSA_ALG_RSA_PSS(PSA_ALG_SHA_384);
Ronald Cron67ea2542022-09-15 17:34:42 +02001118 case MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA512:
Gilles Peskine449bd832023-01-11 14:50:10 +01001119 return PSA_ALG_RSA_PSS(PSA_ALG_SHA_512);
Ronald Cron67ea2542022-09-15 17:34:42 +02001120 case MBEDTLS_TLS1_3_SIG_RSA_PKCS1_SHA256:
Gilles Peskine449bd832023-01-11 14:50:10 +01001121 return PSA_ALG_RSA_PKCS1V15_SIGN(PSA_ALG_SHA_256);
Ronald Cron67ea2542022-09-15 17:34:42 +02001122 case MBEDTLS_TLS1_3_SIG_RSA_PKCS1_SHA384:
Gilles Peskine449bd832023-01-11 14:50:10 +01001123 return PSA_ALG_RSA_PKCS1V15_SIGN(PSA_ALG_SHA_384);
Ronald Cron67ea2542022-09-15 17:34:42 +02001124 case MBEDTLS_TLS1_3_SIG_RSA_PKCS1_SHA512:
Gilles Peskine449bd832023-01-11 14:50:10 +01001125 return PSA_ALG_RSA_PKCS1V15_SIGN(PSA_ALG_SHA_512);
Ronald Cron67ea2542022-09-15 17:34:42 +02001126 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01001127 return PSA_ALG_NONE;
Ronald Cron67ea2542022-09-15 17:34:42 +02001128 }
1129}
1130#endif /* MBEDTLS_USE_PSA_CRYPTO */
1131
XiaokangQian23c5be62022-06-07 02:04:34 +00001132/*
XiaokangQianfb665a82022-06-15 03:57:21 +00001133 * Pick best ( private key, certificate chain ) pair based on the signature
1134 * algorithms supported by the client.
XiaokangQian23c5be62022-06-07 02:04:34 +00001135 */
Ronald Cronce7d76e2022-07-08 18:56:49 +02001136MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01001137static int ssl_tls13_pick_key_cert(mbedtls_ssl_context *ssl)
XiaokangQian23c5be62022-06-07 02:04:34 +00001138{
XiaokangQianfb665a82022-06-15 03:57:21 +00001139 mbedtls_ssl_key_cert *key_cert, *key_cert_list;
XiaokangQian81802f42022-06-10 13:25:22 +00001140 const uint16_t *sig_alg = ssl->handshake->received_sig_algs;
XiaokangQian23c5be62022-06-07 02:04:34 +00001141
1142#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Gilles Peskine449bd832023-01-11 14:50:10 +01001143 if (ssl->handshake->sni_key_cert != NULL) {
XiaokangQianfb665a82022-06-15 03:57:21 +00001144 key_cert_list = ssl->handshake->sni_key_cert;
Gilles Peskine449bd832023-01-11 14:50:10 +01001145 } else
XiaokangQian23c5be62022-06-07 02:04:34 +00001146#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
Gilles Peskine449bd832023-01-11 14:50:10 +01001147 key_cert_list = ssl->conf->key_cert;
XiaokangQian23c5be62022-06-07 02:04:34 +00001148
Gilles Peskine449bd832023-01-11 14:50:10 +01001149 if (key_cert_list == NULL) {
1150 MBEDTLS_SSL_DEBUG_MSG(3, ("server has no certificate"));
1151 return -1;
XiaokangQian23c5be62022-06-07 02:04:34 +00001152 }
1153
Gilles Peskine449bd832023-01-11 14:50:10 +01001154 for (; *sig_alg != MBEDTLS_TLS1_3_SIG_NONE; sig_alg++) {
1155 if (!mbedtls_ssl_sig_alg_is_offered(ssl, *sig_alg)) {
Ronald Cron67ea2542022-09-15 17:34:42 +02001156 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +01001157 }
Ronald Cron67ea2542022-09-15 17:34:42 +02001158
Gilles Peskine449bd832023-01-11 14:50:10 +01001159 if (!mbedtls_ssl_tls13_sig_alg_for_cert_verify_is_supported(*sig_alg)) {
Ronald Cron67ea2542022-09-15 17:34:42 +02001160 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +01001161 }
Ronald Cron67ea2542022-09-15 17:34:42 +02001162
Gilles Peskine449bd832023-01-11 14:50:10 +01001163 for (key_cert = key_cert_list; key_cert != NULL;
1164 key_cert = key_cert->next) {
Ronald Cron67ea2542022-09-15 17:34:42 +02001165#if defined(MBEDTLS_USE_PSA_CRYPTO)
1166 psa_algorithm_t psa_alg = PSA_ALG_NONE;
1167#endif /* MBEDTLS_USE_PSA_CRYPTO */
1168
Gilles Peskine449bd832023-01-11 14:50:10 +01001169 MBEDTLS_SSL_DEBUG_CRT(3, "certificate (chain) candidate",
1170 key_cert->cert);
XiaokangQian81802f42022-06-10 13:25:22 +00001171
1172 /*
Gilles Peskine449bd832023-01-11 14:50:10 +01001173 * This avoids sending the client a cert it'll reject based on
1174 * keyUsage or other extensions.
1175 */
1176 if (mbedtls_x509_crt_check_key_usage(
1177 key_cert->cert, MBEDTLS_X509_KU_DIGITAL_SIGNATURE) != 0 ||
XiaokangQian81802f42022-06-10 13:25:22 +00001178 mbedtls_x509_crt_check_extended_key_usage(
XiaokangQianfb665a82022-06-15 03:57:21 +00001179 key_cert->cert, MBEDTLS_OID_SERVER_AUTH,
Gilles Peskine449bd832023-01-11 14:50:10 +01001180 MBEDTLS_OID_SIZE(MBEDTLS_OID_SERVER_AUTH)) != 0) {
1181 MBEDTLS_SSL_DEBUG_MSG(3, ("certificate mismatch: "
1182 "(extended) key usage extension"));
XiaokangQian81802f42022-06-10 13:25:22 +00001183 continue;
1184 }
XiaokangQian23c5be62022-06-07 02:04:34 +00001185
Gilles Peskine449bd832023-01-11 14:50:10 +01001186 MBEDTLS_SSL_DEBUG_MSG(3,
1187 ("ssl_tls13_pick_key_cert:"
1188 "check signature algorithm %s [%04x]",
1189 mbedtls_ssl_sig_alg_to_str(*sig_alg),
1190 *sig_alg));
Ronald Cron67ea2542022-09-15 17:34:42 +02001191#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01001192 psa_alg = ssl_tls13_iana_sig_alg_to_psa_alg(*sig_alg);
Ronald Cron67ea2542022-09-15 17:34:42 +02001193#endif /* MBEDTLS_USE_PSA_CRYPTO */
1194
Gilles Peskine449bd832023-01-11 14:50:10 +01001195 if (mbedtls_ssl_tls13_check_sig_alg_cert_key_match(
1196 *sig_alg, &key_cert->cert->pk)
Ronald Cron67ea2542022-09-15 17:34:42 +02001197#if defined(MBEDTLS_USE_PSA_CRYPTO)
1198 && psa_alg != PSA_ALG_NONE &&
Gilles Peskine449bd832023-01-11 14:50:10 +01001199 mbedtls_pk_can_do_ext(&key_cert->cert->pk, psa_alg,
1200 PSA_KEY_USAGE_SIGN_HASH) == 1
Ronald Cron67ea2542022-09-15 17:34:42 +02001201#endif /* MBEDTLS_USE_PSA_CRYPTO */
Gilles Peskine449bd832023-01-11 14:50:10 +01001202 ) {
XiaokangQianfb665a82022-06-15 03:57:21 +00001203 ssl->handshake->key_cert = key_cert;
Gilles Peskine449bd832023-01-11 14:50:10 +01001204 MBEDTLS_SSL_DEBUG_MSG(3,
1205 ("ssl_tls13_pick_key_cert:"
1206 "selected signature algorithm"
1207 " %s [%04x]",
1208 mbedtls_ssl_sig_alg_to_str(*sig_alg),
1209 *sig_alg));
XiaokangQianfb665a82022-06-15 03:57:21 +00001210 MBEDTLS_SSL_DEBUG_CRT(
Gilles Peskine449bd832023-01-11 14:50:10 +01001211 3, "selected certificate (chain)",
1212 ssl->handshake->key_cert->cert);
1213 return 0;
XiaokangQian81802f42022-06-10 13:25:22 +00001214 }
XiaokangQian81802f42022-06-10 13:25:22 +00001215 }
XiaokangQian23c5be62022-06-07 02:04:34 +00001216 }
1217
Gilles Peskine449bd832023-01-11 14:50:10 +01001218 MBEDTLS_SSL_DEBUG_MSG(2, ("ssl_tls13_pick_key_cert:"
1219 "no suitable certificate found"));
1220 return -1;
XiaokangQian23c5be62022-06-07 02:04:34 +00001221}
XiaokangQian81802f42022-06-10 13:25:22 +00001222#endif /* MBEDTLS_X509_CRT_PARSE_C &&
Ronald Cron928cbd32022-10-04 16:14:26 +02001223 MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED */
XiaokangQian23c5be62022-06-07 02:04:34 +00001224
XiaokangQian4080a7f2022-04-11 09:55:18 +00001225/*
XiaokangQianed582dd2022-04-13 08:21:05 +00001226 *
1227 * STATE HANDLING: ClientHello
1228 *
XiaokangQianb67384d2022-04-19 00:02:38 +00001229 * There are three possible classes of outcomes when parsing the ClientHello:
XiaokangQianed582dd2022-04-13 08:21:05 +00001230 *
XiaokangQianb67384d2022-04-19 00:02:38 +00001231 * 1) The ClientHello was well-formed and matched the server's configuration.
XiaokangQianed582dd2022-04-13 08:21:05 +00001232 *
1233 * In this case, the server progresses to sending its ServerHello.
1234 *
XiaokangQianb67384d2022-04-19 00:02:38 +00001235 * 2) The ClientHello was well-formed but didn't match the server's
1236 * configuration.
XiaokangQianed582dd2022-04-13 08:21:05 +00001237 *
1238 * For example, the client might not have offered a key share which
1239 * the server supports, or the server might require a cookie.
1240 *
1241 * In this case, the server sends a HelloRetryRequest.
1242 *
XiaokangQianb67384d2022-04-19 00:02:38 +00001243 * 3) The ClientHello was ill-formed
XiaokangQianed582dd2022-04-13 08:21:05 +00001244 *
1245 * In this case, we abort the handshake.
1246 *
1247 */
1248
1249/*
XiaokangQian4080a7f2022-04-11 09:55:18 +00001250 * Structure of this message:
1251 *
XiaokangQiane8ff3502022-04-22 02:34:40 +00001252 * uint16 ProtocolVersion;
1253 * opaque Random[32];
1254 * uint8 CipherSuite[2]; // Cryptographic suite selector
XiaokangQian4080a7f2022-04-11 09:55:18 +00001255 *
XiaokangQiane8ff3502022-04-22 02:34:40 +00001256 * struct {
1257 * ProtocolVersion legacy_version = 0x0303; // TLS v1.2
1258 * Random random;
1259 * opaque legacy_session_id<0..32>;
1260 * CipherSuite cipher_suites<2..2^16-2>;
1261 * opaque legacy_compression_methods<1..2^8-1>;
1262 * Extension extensions<8..2^16-1>;
1263 * } ClientHello;
XiaokangQian4080a7f2022-04-11 09:55:18 +00001264 */
XiaokangQianed582dd2022-04-13 08:21:05 +00001265
1266#define SSL_CLIENT_HELLO_OK 0
1267#define SSL_CLIENT_HELLO_HRR_REQUIRED 1
Ronald Cron5af4c7f2023-03-07 20:46:59 +01001268#define SSL_CLIENT_HELLO_TLS1_2 2
XiaokangQianed582dd2022-04-13 08:21:05 +00001269
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001270MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01001271static int ssl_tls13_parse_client_hello(mbedtls_ssl_context *ssl,
1272 const unsigned char *buf,
1273 const unsigned char *end)
XiaokangQian7807f9f2022-02-15 10:04:37 +00001274{
XiaokangQianb67384d2022-04-19 00:02:38 +00001275 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1276 const unsigned char *p = buf;
Ronald Crond540d992023-03-07 09:41:48 +01001277 const unsigned char *random;
XiaokangQian4080a7f2022-04-11 09:55:18 +00001278 size_t legacy_session_id_len;
Ronald Croncada4102023-03-07 09:51:39 +01001279 const unsigned char *legacy_session_id;
XiaokangQian318dc762022-04-20 09:43:51 +00001280 size_t cipher_suites_len;
Ronald Cron2f16b4e2023-03-07 10:07:32 +01001281 const unsigned char *cipher_suites;
XiaokangQian060d8672022-04-21 09:24:56 +00001282 const unsigned char *cipher_suites_end;
XiaokangQianb67384d2022-04-19 00:02:38 +00001283 size_t extensions_len;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001284 const unsigned char *extensions_end;
Ronald Croneff56732023-04-03 17:36:31 +02001285 const unsigned char *supported_versions_data;
1286 const unsigned char *supported_versions_data_end;
Jerry Yuc4bf5d62022-10-29 09:08:47 +08001287 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
Jerry Yu49ca9282022-05-05 11:05:22 +08001288 int hrr_required = 0;
Ronald Cron8a74f072023-06-14 17:59:29 +02001289 int no_usable_share_for_key_agreement = 0;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001290
Ronald Cron41a443a2022-10-04 16:38:25 +02001291#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED)
Jerry Yu29d9faa2022-08-23 17:52:45 +08001292 const unsigned char *pre_shared_key_ext = NULL;
Jerry Yu1c105562022-07-10 06:32:38 +00001293 const unsigned char *pre_shared_key_ext_end = NULL;
Ronald Cron41a443a2022-10-04 16:38:25 +02001294#endif
XiaokangQian7807f9f2022-02-15 10:04:37 +00001295
XiaokangQian7807f9f2022-02-15 10:04:37 +00001296 /*
XiaokangQianb67384d2022-04-19 00:02:38 +00001297 * ClientHello layout:
XiaokangQian7807f9f2022-02-15 10:04:37 +00001298 * 0 . 1 protocol version
XiaokangQiane8ff3502022-04-22 02:34:40 +00001299 * 2 . 33 random bytes
XiaokangQianc5763b52022-04-02 03:34:37 +00001300 * 34 . 34 session id length ( 1 byte )
XiaokangQian7807f9f2022-02-15 10:04:37 +00001301 * 35 . 34+x session id
XiaokangQian7807f9f2022-02-15 10:04:37 +00001302 * .. . .. ciphersuite list length ( 2 bytes )
1303 * .. . .. ciphersuite list
1304 * .. . .. compression alg. list length ( 1 byte )
1305 * .. . .. compression alg. list
1306 * .. . .. extensions length ( 2 bytes, optional )
1307 * .. . .. extensions ( optional )
1308 */
1309
XiaokangQianb67384d2022-04-19 00:02:38 +00001310 /*
bootstrap-prime6dbbf442022-05-17 19:30:44 -04001311 * Minimal length ( with everything empty and extensions omitted ) is
XiaokangQian7807f9f2022-02-15 10:04:37 +00001312 * 2 + 32 + 1 + 2 + 1 = 38 bytes. Check that first, so that we can
1313 * read at least up to session id length without worrying.
1314 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001315 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, 38);
XiaokangQian7807f9f2022-02-15 10:04:37 +00001316
1317 /* ...
1318 * ProtocolVersion legacy_version = 0x0303; // TLS 1.2
1319 * ...
1320 * with ProtocolVersion defined as:
1321 * uint16 ProtocolVersion;
1322 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001323 if (mbedtls_ssl_read_version(p, ssl->conf->transport) !=
1324 MBEDTLS_SSL_VERSION_TLS1_2) {
1325 MBEDTLS_SSL_DEBUG_MSG(1, ("Unsupported version of TLS."));
1326 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_PROTOCOL_VERSION,
1327 MBEDTLS_ERR_SSL_BAD_PROTOCOL_VERSION);
1328 return MBEDTLS_ERR_SSL_BAD_PROTOCOL_VERSION;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001329 }
1330 p += 2;
1331
Jerry Yuc1be19f2022-04-23 16:11:39 +08001332 /* ...
1333 * Random random;
1334 * ...
XiaokangQianb67384d2022-04-19 00:02:38 +00001335 * with Random defined as:
1336 * opaque Random[32];
XiaokangQian7807f9f2022-02-15 10:04:37 +00001337 */
Ronald Crond540d992023-03-07 09:41:48 +01001338 random = p;
XiaokangQian08037552022-04-20 07:16:41 +00001339 p += MBEDTLS_CLIENT_HELLO_RANDOM_LEN;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001340
Jerry Yuc1be19f2022-04-23 16:11:39 +08001341 /* ...
XiaokangQianb67384d2022-04-19 00:02:38 +00001342 * opaque legacy_session_id<0..32>;
Jerry Yuc1be19f2022-04-23 16:11:39 +08001343 * ...
XiaokangQian7807f9f2022-02-15 10:04:37 +00001344 */
Ronald Croncada4102023-03-07 09:51:39 +01001345 legacy_session_id_len = *(p++);
1346 legacy_session_id = p;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001347
XiaokangQianb67384d2022-04-19 00:02:38 +00001348 /*
1349 * Check we have enough data for the legacy session identifier
Jerry Yue95c8af2022-07-26 15:48:20 +08001350 * and the ciphersuite list length.
XiaokangQianb67384d2022-04-19 00:02:38 +00001351 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001352 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, legacy_session_id_len + 2);
XiaokangQian4080a7f2022-04-11 09:55:18 +00001353 p += legacy_session_id_len;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001354
Ronald Cron2f16b4e2023-03-07 10:07:32 +01001355 /* ...
1356 * CipherSuite cipher_suites<2..2^16-2>;
1357 * ...
1358 * with CipherSuite defined as:
1359 * uint8 CipherSuite[2];
1360 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001361 cipher_suites_len = MBEDTLS_GET_UINT16_BE(p, 0);
XiaokangQian7807f9f2022-02-15 10:04:37 +00001362 p += 2;
Ronald Cron2f16b4e2023-03-07 10:07:32 +01001363 cipher_suites = p;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001364
Ronald Cronfc7ae872023-02-16 15:32:19 +01001365 /*
1366 * The length of the ciphersuite list has to be even.
1367 */
1368 if (cipher_suites_len & 1) {
1369 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR,
1370 MBEDTLS_ERR_SSL_DECODE_ERROR);
1371 return MBEDTLS_ERR_SSL_DECODE_ERROR;
1372 }
1373
XiaokangQianb67384d2022-04-19 00:02:38 +00001374 /* Check we have enough data for the ciphersuite list, the legacy
1375 * compression methods and the length of the extensions.
Jerry Yue95c8af2022-07-26 15:48:20 +08001376 *
1377 * cipher_suites cipher_suites_len bytes
1378 * legacy_compression_methods 2 bytes
1379 * extensions_len 2 bytes
XiaokangQianb67384d2022-04-19 00:02:38 +00001380 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001381 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, cipher_suites_len + 2 + 2);
Ronald Cron8c527d02023-03-07 15:47:47 +01001382 p += cipher_suites_len;
1383 cipher_suites_end = p;
Jerry Yu5725f1c2022-08-21 17:27:16 +08001384
1385 /*
Ronald Cron8c527d02023-03-07 15:47:47 +01001386 * Search for the supported versions extension and parse it to determine
1387 * if the client supports TLS 1.3.
1388 */
1389 ret = mbedtls_ssl_tls13_is_supported_versions_ext_present_in_exts(
1390 ssl, p + 2, end,
Ronald Croneff56732023-04-03 17:36:31 +02001391 &supported_versions_data, &supported_versions_data_end);
Ronald Cron8c527d02023-03-07 15:47:47 +01001392 if (ret < 0) {
1393 MBEDTLS_SSL_DEBUG_RET(1,
1394 ("mbedtls_ssl_tls13_is_supported_versions_ext_present_in_exts"), ret);
1395 return ret;
1396 }
1397
1398 if (ret == 0) {
Ronald Cron5af4c7f2023-03-07 20:46:59 +01001399 return SSL_CLIENT_HELLO_TLS1_2;
Ronald Cron8c527d02023-03-07 15:47:47 +01001400 }
1401
Ronald Cron5af4c7f2023-03-07 20:46:59 +01001402 if (ret == 1) {
1403 ret = ssl_tls13_parse_supported_versions_ext(ssl,
Ronald Croneff56732023-04-03 17:36:31 +02001404 supported_versions_data,
1405 supported_versions_data_end);
Ronald Cron5af4c7f2023-03-07 20:46:59 +01001406 if (ret < 0) {
1407 MBEDTLS_SSL_DEBUG_RET(1,
1408 ("ssl_tls13_parse_supported_versions_ext"), ret);
1409 return ret;
1410 }
1411
Ronald Cronb828c7d2023-04-03 16:37:22 +02001412 /*
1413 * The supported versions extension was parsed successfully as the
1414 * value returned by ssl_tls13_parse_supported_versions_ext() is
1415 * positive. The return value is then equal to
1416 * MBEDTLS_SSL_VERSION_TLS1_2 or MBEDTLS_SSL_VERSION_TLS1_3, defining
1417 * the TLS version to negotiate.
1418 */
Ronald Cron5af4c7f2023-03-07 20:46:59 +01001419 if (MBEDTLS_SSL_VERSION_TLS1_2 == ret) {
1420 return SSL_CLIENT_HELLO_TLS1_2;
1421 }
Ronald Cron8c527d02023-03-07 15:47:47 +01001422 }
1423
1424 /*
1425 * We negotiate TLS 1.3.
Ronald Cron64582392023-03-07 09:21:40 +01001426 */
1427 ssl->tls_version = MBEDTLS_SSL_VERSION_TLS1_3;
Ronald Cron64582392023-03-07 09:21:40 +01001428 ssl->session_negotiate->tls_version = MBEDTLS_SSL_VERSION_TLS1_3;
1429 ssl->session_negotiate->endpoint = ssl->conf->endpoint;
Ronald Cron64582392023-03-07 09:21:40 +01001430
1431 /*
Ronald Crondad02b22023-04-06 09:57:52 +02001432 * We are negotiating the version 1.3 of the protocol. Do what we have
Ronald Croncada4102023-03-07 09:51:39 +01001433 * postponed: copy of the client random bytes, copy of the legacy session
Ronald Cron2f16b4e2023-03-07 10:07:32 +01001434 * identifier and selection of the TLS 1.3 cipher suite.
Ronald Crond540d992023-03-07 09:41:48 +01001435 */
1436 MBEDTLS_SSL_DEBUG_BUF(3, "client hello, random bytes",
1437 random, MBEDTLS_CLIENT_HELLO_RANDOM_LEN);
1438 memcpy(&handshake->randbytes[0], random, MBEDTLS_CLIENT_HELLO_RANDOM_LEN);
1439
Ronald Croncada4102023-03-07 09:51:39 +01001440 if (legacy_session_id_len > sizeof(ssl->session_negotiate->id)) {
1441 MBEDTLS_SSL_DEBUG_MSG(1, ("bad client hello message"));
1442 return MBEDTLS_ERR_SSL_DECODE_ERROR;
1443 }
1444 ssl->session_negotiate->id_len = legacy_session_id_len;
1445 MBEDTLS_SSL_DEBUG_BUF(3, "client hello, session id",
1446 legacy_session_id, legacy_session_id_len);
1447 memcpy(&ssl->session_negotiate->id[0],
1448 legacy_session_id, legacy_session_id_len);
1449
Ronald Crond540d992023-03-07 09:41:48 +01001450 /*
Jerry Yu5725f1c2022-08-21 17:27:16 +08001451 * Search for a matching ciphersuite
1452 */
Ronald Cron2f16b4e2023-03-07 10:07:32 +01001453 MBEDTLS_SSL_DEBUG_BUF(3, "client hello, list of cipher suites",
1454 cipher_suites, cipher_suites_len);
Jerry Yu5725f1c2022-08-21 17:27:16 +08001455
Ronald Cron89089cc2024-02-14 18:18:08 +01001456 ssl_tls13_select_ciphersuite(ssl, cipher_suites, cipher_suites_end,
1457 0, PSA_ALG_NONE, &handshake->ciphersuite_info);
Jerry Yu5725f1c2022-08-21 17:27:16 +08001458
Gilles Peskine449bd832023-01-11 14:50:10 +01001459 if (handshake->ciphersuite_info == NULL) {
1460 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE,
1461 MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE);
1462 return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
Jerry Yu5725f1c2022-08-21 17:27:16 +08001463 }
Ronald Cron89089cc2024-02-14 18:18:08 +01001464 ssl->session_negotiate->ciphersuite = handshake->ciphersuite_info->id;
1465
1466 MBEDTLS_SSL_DEBUG_MSG(2, ("selected ciphersuite: %04x - %s",
1467 ((unsigned) handshake->ciphersuite_info->id),
1468 handshake->ciphersuite_info->name));
Jerry Yuc1be19f2022-04-23 16:11:39 +08001469
XiaokangQian4080a7f2022-04-11 09:55:18 +00001470 /* ...
XiaokangQianb67384d2022-04-19 00:02:38 +00001471 * opaque legacy_compression_methods<1..2^8-1>;
XiaokangQian4080a7f2022-04-11 09:55:18 +00001472 * ...
XiaokangQian7807f9f2022-02-15 10:04:37 +00001473 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001474 if (p[0] != 1 || p[1] != MBEDTLS_SSL_COMPRESS_NULL) {
1475 MBEDTLS_SSL_DEBUG_MSG(1, ("bad legacy compression method"));
1476 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER,
1477 MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER);
1478 return MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001479 }
XiaokangQianb67384d2022-04-19 00:02:38 +00001480 p += 2;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001481
Jerry Yuc1be19f2022-04-23 16:11:39 +08001482 /* ...
XiaokangQianb67384d2022-04-19 00:02:38 +00001483 * Extension extensions<8..2^16-1>;
Jerry Yuc1be19f2022-04-23 16:11:39 +08001484 * ...
XiaokangQianb67384d2022-04-19 00:02:38 +00001485 * with Extension defined as:
1486 * struct {
1487 * ExtensionType extension_type;
1488 * opaque extension_data<0..2^16-1>;
1489 * } Extension;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001490 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001491 extensions_len = MBEDTLS_GET_UINT16_BE(p, 0);
XiaokangQian7807f9f2022-02-15 10:04:37 +00001492 p += 2;
Gilles Peskine449bd832023-01-11 14:50:10 +01001493 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, extensions_len);
XiaokangQiane8ff3502022-04-22 02:34:40 +00001494 extensions_end = p + extensions_len;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001495
Gilles Peskine449bd832023-01-11 14:50:10 +01001496 MBEDTLS_SSL_DEBUG_BUF(3, "client hello extensions", p, extensions_len);
Jerry Yu63a459c2022-10-31 13:38:40 +08001497 handshake->received_extensions = MBEDTLS_SSL_EXT_MASK_NONE;
Jerry Yu0c354a22022-08-29 15:25:36 +08001498
Gilles Peskine449bd832023-01-11 14:50:10 +01001499 while (p < extensions_end) {
XiaokangQian7807f9f2022-02-15 10:04:37 +00001500 unsigned int extension_type;
1501 size_t extension_data_len;
1502 const unsigned char *extension_data_end;
Jerry Yu263dbf72022-10-26 10:51:27 +08001503 uint32_t allowed_exts = MBEDTLS_SSL_TLS1_3_ALLOWED_EXTS_OF_CH;
1504
Ronald Cron5fbd2702024-02-14 10:03:36 +01001505 if (ssl->handshake->hello_retry_request_flag) {
Jerry Yu263dbf72022-10-26 10:51:27 +08001506 /* Do not accept early data extension in 2nd ClientHello */
1507 allowed_exts &= ~MBEDTLS_SSL_EXT_MASK(EARLY_DATA);
1508 }
XiaokangQian7807f9f2022-02-15 10:04:37 +00001509
Jerry Yu0c354a22022-08-29 15:25:36 +08001510 /* RFC 8446, section 4.2.11
Jerry Yu1c9247c2022-07-21 12:37:39 +08001511 *
1512 * The "pre_shared_key" extension MUST be the last extension in the
1513 * ClientHello (this facilitates implementation as described below).
1514 * Servers MUST check that it is the last extension and otherwise fail
1515 * the handshake with an "illegal_parameter" alert.
1516 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001517 if (handshake->received_extensions & MBEDTLS_SSL_EXT_MASK(PRE_SHARED_KEY)) {
Jerry Yu1c9247c2022-07-21 12:37:39 +08001518 MBEDTLS_SSL_DEBUG_MSG(
Gilles Peskine449bd832023-01-11 14:50:10 +01001519 3, ("pre_shared_key is not last extension."));
Jerry Yu1c9247c2022-07-21 12:37:39 +08001520 MBEDTLS_SSL_PEND_FATAL_ALERT(
1521 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER,
Gilles Peskine449bd832023-01-11 14:50:10 +01001522 MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER);
1523 return MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER;
Jerry Yu1c9247c2022-07-21 12:37:39 +08001524 }
1525
Gilles Peskine449bd832023-01-11 14:50:10 +01001526 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, extensions_end, 4);
1527 extension_type = MBEDTLS_GET_UINT16_BE(p, 0);
1528 extension_data_len = MBEDTLS_GET_UINT16_BE(p, 2);
XiaokangQian7807f9f2022-02-15 10:04:37 +00001529 p += 4;
1530
Gilles Peskine449bd832023-01-11 14:50:10 +01001531 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, extensions_end, extension_data_len);
XiaokangQian7807f9f2022-02-15 10:04:37 +00001532 extension_data_end = p + extension_data_len;
1533
Jerry Yuc4bf5d62022-10-29 09:08:47 +08001534 ret = mbedtls_ssl_tls13_check_received_extension(
Gilles Peskine449bd832023-01-11 14:50:10 +01001535 ssl, MBEDTLS_SSL_HS_CLIENT_HELLO, extension_type,
Jerry Yu263dbf72022-10-26 10:51:27 +08001536 allowed_exts);
Gilles Peskine449bd832023-01-11 14:50:10 +01001537 if (ret != 0) {
1538 return ret;
1539 }
Jerry Yue18dc7e2022-08-04 16:29:22 +08001540
Gilles Peskine449bd832023-01-11 14:50:10 +01001541 switch (extension_type) {
XiaokangQian40a35232022-05-07 09:02:40 +00001542#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
1543 case MBEDTLS_TLS_EXT_SERVERNAME:
Gilles Peskine449bd832023-01-11 14:50:10 +01001544 MBEDTLS_SSL_DEBUG_MSG(3, ("found ServerName extension"));
1545 ret = mbedtls_ssl_parse_server_name_ext(ssl, p,
1546 extension_data_end);
1547 if (ret != 0) {
XiaokangQian40a35232022-05-07 09:02:40 +00001548 MBEDTLS_SSL_DEBUG_RET(
Gilles Peskine449bd832023-01-11 14:50:10 +01001549 1, "mbedtls_ssl_parse_servername_ext", ret);
1550 return ret;
XiaokangQian40a35232022-05-07 09:02:40 +00001551 }
XiaokangQian40a35232022-05-07 09:02:40 +00001552 break;
1553#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
1554
Przemek Stekiel8c0a9532023-06-15 16:48:19 +02001555#if defined(PSA_WANT_ALG_ECDH) || defined(PSA_WANT_ALG_FFDH)
XiaokangQian7807f9f2022-02-15 10:04:37 +00001556 case MBEDTLS_TLS_EXT_SUPPORTED_GROUPS:
Gilles Peskine449bd832023-01-11 14:50:10 +01001557 MBEDTLS_SSL_DEBUG_MSG(3, ("found supported group extension"));
XiaokangQian7807f9f2022-02-15 10:04:37 +00001558
1559 /* Supported Groups Extension
1560 *
1561 * When sent by the client, the "supported_groups" extension
1562 * indicates the named groups which the client supports,
1563 * ordered from most preferred to least preferred.
1564 */
Jerry Yuc1be19f2022-04-23 16:11:39 +08001565 ret = ssl_tls13_parse_supported_groups_ext(
Gilles Peskine449bd832023-01-11 14:50:10 +01001566 ssl, p, extension_data_end);
1567 if (ret != 0) {
Xiaokang Qian9f1747b2023-03-30 02:50:04 +00001568 MBEDTLS_SSL_DEBUG_RET(
Xiaokang Qian8bce0e62023-04-04 10:15:48 +00001569 1, "ssl_tls13_parse_supported_groups_ext", ret);
Gilles Peskine449bd832023-01-11 14:50:10 +01001570 return ret;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001571 }
1572
XiaokangQian7807f9f2022-02-15 10:04:37 +00001573 break;
Przemek Stekiel8c0a9532023-06-15 16:48:19 +02001574#endif /* PSA_WANT_ALG_ECDH || PSA_WANT_ALG_FFDH*/
XiaokangQian7807f9f2022-02-15 10:04:37 +00001575
Valerio Settic9ae8622023-07-25 11:23:50 +02001576#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_EPHEMERAL_ENABLED)
XiaokangQian7807f9f2022-02-15 10:04:37 +00001577 case MBEDTLS_TLS_EXT_KEY_SHARE:
Gilles Peskine449bd832023-01-11 14:50:10 +01001578 MBEDTLS_SSL_DEBUG_MSG(3, ("found key share extension"));
XiaokangQian7807f9f2022-02-15 10:04:37 +00001579
1580 /*
1581 * Key Share Extension
1582 *
1583 * When sent by the client, the "key_share" extension
1584 * contains the endpoint's cryptographic parameters for
1585 * ECDHE/DHE key establishment methods.
1586 */
Jerry Yuc1be19f2022-04-23 16:11:39 +08001587 ret = ssl_tls13_parse_key_shares_ext(
Gilles Peskine449bd832023-01-11 14:50:10 +01001588 ssl, p, extension_data_end);
1589 if (ret == SSL_TLS1_3_PARSE_KEY_SHARES_EXT_NO_MATCH) {
Ronald Cron8a74f072023-06-14 17:59:29 +02001590 MBEDTLS_SSL_DEBUG_MSG(2, ("No usable share for key agreement."));
1591 no_usable_share_for_key_agreement = 1;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001592 }
1593
Gilles Peskine449bd832023-01-11 14:50:10 +01001594 if (ret < 0) {
Jerry Yu582dd062022-04-22 21:59:01 +08001595 MBEDTLS_SSL_DEBUG_RET(
Gilles Peskine449bd832023-01-11 14:50:10 +01001596 1, "ssl_tls13_parse_key_shares_ext", ret);
1597 return ret;
Jerry Yu582dd062022-04-22 21:59:01 +08001598 }
XiaokangQian7807f9f2022-02-15 10:04:37 +00001599
XiaokangQian7807f9f2022-02-15 10:04:37 +00001600 break;
Valerio Settic9ae8622023-07-25 11:23:50 +02001601#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_EPHEMERAL_ENABLED */
XiaokangQian7807f9f2022-02-15 10:04:37 +00001602
1603 case MBEDTLS_TLS_EXT_SUPPORTED_VERSIONS:
Ronald Cron8c527d02023-03-07 15:47:47 +01001604 /* Already parsed */
XiaokangQian7807f9f2022-02-15 10:04:37 +00001605 break;
1606
Ronald Cron41a443a2022-10-04 16:38:25 +02001607#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED)
Jerry Yue19e3b92022-07-08 12:04:51 +00001608 case MBEDTLS_TLS_EXT_PSK_KEY_EXCHANGE_MODES:
Xiaokang Qian9f1747b2023-03-30 02:50:04 +00001609 MBEDTLS_SSL_DEBUG_MSG(
1610 3, ("found psk key exchange modes extension"));
Jerry Yue19e3b92022-07-08 12:04:51 +00001611
1612 ret = ssl_tls13_parse_key_exchange_modes_ext(
Gilles Peskine449bd832023-01-11 14:50:10 +01001613 ssl, p, extension_data_end);
1614 if (ret != 0) {
Jerry Yue19e3b92022-07-08 12:04:51 +00001615 MBEDTLS_SSL_DEBUG_RET(
Gilles Peskine449bd832023-01-11 14:50:10 +01001616 1, "ssl_tls13_parse_key_exchange_modes_ext", ret);
1617 return ret;
Jerry Yue19e3b92022-07-08 12:04:51 +00001618 }
1619
Jerry Yue19e3b92022-07-08 12:04:51 +00001620 break;
Ronald Cron41a443a2022-10-04 16:38:25 +02001621#endif
Jerry Yue19e3b92022-07-08 12:04:51 +00001622
Jerry Yu1c105562022-07-10 06:32:38 +00001623 case MBEDTLS_TLS_EXT_PRE_SHARED_KEY:
Gilles Peskine449bd832023-01-11 14:50:10 +01001624 MBEDTLS_SSL_DEBUG_MSG(3, ("found pre_shared_key extension"));
1625 if ((handshake->received_extensions &
1626 MBEDTLS_SSL_EXT_MASK(PSK_KEY_EXCHANGE_MODES)) == 0) {
Jerry Yu13ab81d2022-07-22 23:17:11 +08001627 MBEDTLS_SSL_PEND_FATAL_ALERT(
1628 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER,
Gilles Peskine449bd832023-01-11 14:50:10 +01001629 MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER);
1630 return MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER;
Jerry Yu13ab81d2022-07-22 23:17:11 +08001631 }
Ronald Cron41a443a2022-10-04 16:38:25 +02001632#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED)
Jerry Yu1c105562022-07-10 06:32:38 +00001633 /* Delay processing of the PSK identity once we have
1634 * found out which algorithms to use. We keep a pointer
1635 * to the buffer and the size for later processing.
1636 */
Jerry Yu29d9faa2022-08-23 17:52:45 +08001637 pre_shared_key_ext = p;
Jerry Yu1c105562022-07-10 06:32:38 +00001638 pre_shared_key_ext_end = extension_data_end;
Jerry Yue18dc7e2022-08-04 16:29:22 +08001639#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED */
Jerry Yu1c105562022-07-10 06:32:38 +00001640 break;
Jerry Yu1c105562022-07-10 06:32:38 +00001641
XiaokangQianacb39922022-06-17 10:18:48 +00001642#if defined(MBEDTLS_SSL_ALPN)
1643 case MBEDTLS_TLS_EXT_ALPN:
Gilles Peskine449bd832023-01-11 14:50:10 +01001644 MBEDTLS_SSL_DEBUG_MSG(3, ("found alpn extension"));
XiaokangQianacb39922022-06-17 10:18:48 +00001645
Gilles Peskine449bd832023-01-11 14:50:10 +01001646 ret = mbedtls_ssl_parse_alpn_ext(ssl, p, extension_data_end);
1647 if (ret != 0) {
XiaokangQianacb39922022-06-17 10:18:48 +00001648 MBEDTLS_SSL_DEBUG_RET(
Gilles Peskine449bd832023-01-11 14:50:10 +01001649 1, ("mbedtls_ssl_parse_alpn_ext"), ret);
1650 return ret;
XiaokangQianacb39922022-06-17 10:18:48 +00001651 }
XiaokangQianacb39922022-06-17 10:18:48 +00001652 break;
1653#endif /* MBEDTLS_SSL_ALPN */
1654
Ronald Cron928cbd32022-10-04 16:14:26 +02001655#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED)
XiaokangQian7807f9f2022-02-15 10:04:37 +00001656 case MBEDTLS_TLS_EXT_SIG_ALG:
Gilles Peskine449bd832023-01-11 14:50:10 +01001657 MBEDTLS_SSL_DEBUG_MSG(3, ("found signature_algorithms extension"));
XiaokangQian7807f9f2022-02-15 10:04:37 +00001658
Gabor Mezei078e8032022-04-27 21:17:56 +02001659 ret = mbedtls_ssl_parse_sig_alg_ext(
Gilles Peskine449bd832023-01-11 14:50:10 +01001660 ssl, p, extension_data_end);
1661 if (ret != 0) {
Xiaokang Qian49f39c12023-04-06 02:31:02 +00001662 MBEDTLS_SSL_DEBUG_RET(
1663 1, "mbedtls_ssl_parse_sig_alg_ext", ret);
Gilles Peskine449bd832023-01-11 14:50:10 +01001664 return ret;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001665 }
XiaokangQian7807f9f2022-02-15 10:04:37 +00001666 break;
Ronald Cron928cbd32022-10-04 16:14:26 +02001667#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED */
XiaokangQian7807f9f2022-02-15 10:04:37 +00001668
Jan Bruckner151f6422023-02-10 12:45:19 +01001669#if defined(MBEDTLS_SSL_RECORD_SIZE_LIMIT)
1670 case MBEDTLS_TLS_EXT_RECORD_SIZE_LIMIT:
1671 MBEDTLS_SSL_DEBUG_MSG(3, ("found record_size_limit extension"));
1672
Xiaokang Qian9f1747b2023-03-30 02:50:04 +00001673 ret = mbedtls_ssl_tls13_parse_record_size_limit_ext(
1674 ssl, p, extension_data_end);
Jan Brucknerf482dcc2023-03-15 09:09:06 +01001675 if (ret != 0) {
1676 MBEDTLS_SSL_DEBUG_RET(
1677 1, ("mbedtls_ssl_tls13_parse_record_size_limit_ext"), ret);
1678 return ret;
1679 }
Jan Bruckner151f6422023-02-10 12:45:19 +01001680 break;
1681#endif /* MBEDTLS_SSL_RECORD_SIZE_LIMIT */
1682
XiaokangQian7807f9f2022-02-15 10:04:37 +00001683 default:
Jerry Yu79aa7212022-11-08 21:30:21 +08001684 MBEDTLS_SSL_PRINT_EXT(
Jerry Yu63a459c2022-10-31 13:38:40 +08001685 3, MBEDTLS_SSL_HS_CLIENT_HELLO,
Gilles Peskine449bd832023-01-11 14:50:10 +01001686 extension_type, "( ignored )");
Jerry Yu0c354a22022-08-29 15:25:36 +08001687 break;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001688 }
1689
1690 p += extension_data_len;
1691 }
1692
Gilles Peskine449bd832023-01-11 14:50:10 +01001693 MBEDTLS_SSL_PRINT_EXTS(3, MBEDTLS_SSL_HS_CLIENT_HELLO,
1694 handshake->received_extensions);
Jerry Yue18dc7e2022-08-04 16:29:22 +08001695
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +01001696 ret = mbedtls_ssl_add_hs_hdr_to_checksum(ssl,
1697 MBEDTLS_SSL_HS_CLIENT_HELLO,
1698 p - buf);
1699 if (0 != ret) {
1700 MBEDTLS_SSL_DEBUG_RET(1, ("mbedtls_ssl_add_hs_hdr_to_checksum"), ret);
1701 return ret;
1702 }
Jerry Yu032b15ce2022-07-11 06:10:03 +00001703
Ronald Cron41a443a2022-10-04 16:38:25 +02001704#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED)
XiaokangQian7807f9f2022-02-15 10:04:37 +00001705 /* Update checksum with either
1706 * - The entire content of the CH message, if no PSK extension is present
1707 * - The content up to but excluding the PSK extension, if present.
Ronald Cron12e72f12024-02-14 15:49:38 +01001708 * Always parse the pre-shared key extension when present in the
1709 * ClientHello even if some pre-requisites for PSK key exchange modes are
1710 * not met. That way we always validate the syntax of the extension.
XiaokangQian7807f9f2022-02-15 10:04:37 +00001711 */
Ronald Cron12e72f12024-02-14 15:49:38 +01001712 if (handshake->received_extensions & MBEDTLS_SSL_EXT_MASK(PRE_SHARED_KEY)) {
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +01001713 ret = handshake->update_checksum(ssl, buf,
1714 pre_shared_key_ext - buf);
1715 if (0 != ret) {
1716 MBEDTLS_SSL_DEBUG_RET(1, ("update_checksum"), ret);
1717 return ret;
1718 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001719 ret = ssl_tls13_parse_pre_shared_key_ext(ssl,
1720 pre_shared_key_ext,
1721 pre_shared_key_ext_end,
1722 cipher_suites,
1723 cipher_suites_end);
1724 if (ret == MBEDTLS_ERR_SSL_UNKNOWN_IDENTITY) {
1725 handshake->received_extensions &= ~MBEDTLS_SSL_EXT_MASK(PRE_SHARED_KEY);
1726 } else if (ret != 0) {
Jerry Yu63a459c2022-10-31 13:38:40 +08001727 MBEDTLS_SSL_DEBUG_RET(
Gilles Peskine449bd832023-01-11 14:50:10 +01001728 1, "ssl_tls13_parse_pre_shared_key_ext", ret);
1729 return ret;
Jerry Yu1c105562022-07-10 06:32:38 +00001730 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001731 } else
Ronald Cron41a443a2022-10-04 16:38:25 +02001732#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED */
Jerry Yu1c105562022-07-10 06:32:38 +00001733 {
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +01001734 ret = handshake->update_checksum(ssl, buf, p - buf);
1735 if (0 != ret) {
1736 MBEDTLS_SSL_DEBUG_RET(1, ("update_checksum"), ret);
1737 return ret;
1738 }
Jerry Yu1c105562022-07-10 06:32:38 +00001739 }
XiaokangQian7807f9f2022-02-15 10:04:37 +00001740
Gilles Peskine449bd832023-01-11 14:50:10 +01001741 ret = ssl_tls13_determine_key_exchange_mode(ssl);
1742 if (ret < 0) {
1743 return ret;
1744 }
XiaokangQian7807f9f2022-02-15 10:04:37 +00001745
Ronald Cron8a74f072023-06-14 17:59:29 +02001746 if (ssl->handshake->key_exchange_mode !=
1747 MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK) {
1748 hrr_required = (no_usable_share_for_key_agreement != 0);
1749 }
1750
Gilles Peskine449bd832023-01-11 14:50:10 +01001751 mbedtls_ssl_optimize_checksum(ssl, handshake->ciphersuite_info);
Jerry Yue5834fd2022-08-29 20:16:09 +08001752
Gilles Peskine449bd832023-01-11 14:50:10 +01001753 return hrr_required ? SSL_CLIENT_HELLO_HRR_REQUIRED : SSL_CLIENT_HELLO_OK;
XiaokangQian75fe8c72022-06-15 09:42:45 +00001754}
1755
Jerry Yuab0da372023-02-08 13:55:24 +08001756#if defined(MBEDTLS_SSL_EARLY_DATA)
Jerry Yu4caf3ca2023-11-15 16:13:47 +08001757static int ssl_tls13_check_early_data_requirements(mbedtls_ssl_context *ssl)
Jerry Yuab0da372023-02-08 13:55:24 +08001758{
1759 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
1760
Jerry Yu985c9672022-12-04 14:06:30 +08001761 if (ssl->conf->early_data_enabled == MBEDTLS_SSL_EARLY_DATA_DISABLED) {
1762 MBEDTLS_SSL_DEBUG_MSG(
Jerry Yu985c9672022-12-04 14:06:30 +08001763 1,
Jerry Yu454dda32023-10-31 15:13:54 +08001764 ("EarlyData: rejected, feature disabled in server configuration."));
Jerry Yu4caf3ca2023-11-15 16:13:47 +08001765 return -1;
Ronald Cron7b6ee942024-01-12 10:29:55 +01001766 }
1767
Jerry Yu454dda32023-10-31 15:13:54 +08001768 if (!handshake->resume) {
1769 /* We currently support early data only in the case of PSKs established
1770 via a NewSessionTicket message thus in the case of a session
1771 resumption. */
Jerry Yu985c9672022-12-04 14:06:30 +08001772 MBEDTLS_SSL_DEBUG_MSG(
Jerry Yu7ef9fd82023-11-07 14:30:38 +08001773 1, ("EarlyData: rejected, not a session resumption."));
Jerry Yu4caf3ca2023-11-15 16:13:47 +08001774 return -1;
Jerry Yu985c9672022-12-04 14:06:30 +08001775 }
1776
Jerry Yu82fd6c12023-10-31 16:32:19 +08001777 /* RFC 8446 4.2.10
1778 *
1779 * In order to accept early data, the server MUST have accepted a PSK cipher
1780 * suite and selected the first key offered in the client's "pre_shared_key"
1781 * extension. In addition, it MUST verify that the following values are the
1782 * same as those associated with the selected PSK:
1783 * - The TLS version number
1784 * - The selected cipher suite
1785 * - The selected ALPN [RFC7301] protocol, if any
1786 *
1787 * NOTE:
Jerry Yu7ef9fd82023-11-07 14:30:38 +08001788 * - The TLS version number is checked in
1789 * ssl_tls13_offered_psks_check_identity_match_ticket().
1790 * - ALPN is not checked for the time being (TODO).
Jerry Yu82fd6c12023-10-31 16:32:19 +08001791 */
1792
1793 if (handshake->selected_identity != 0) {
1794 MBEDTLS_SSL_DEBUG_MSG(
Jerry Yu7ef9fd82023-11-07 14:30:38 +08001795 1, ("EarlyData: rejected, the selected key in "
1796 "`pre_shared_key` is not the first one."));
Jerry Yu4caf3ca2023-11-15 16:13:47 +08001797 return -1;
Jerry Yu82fd6c12023-10-31 16:32:19 +08001798 }
1799
1800 if (handshake->ciphersuite_info->id !=
1801 ssl->session_negotiate->ciphersuite) {
1802 MBEDTLS_SSL_DEBUG_MSG(
Jerry Yu7ef9fd82023-11-07 14:30:38 +08001803 1, ("EarlyData: rejected, the selected ciphersuite is not the one "
1804 "of the selected pre-shared key."));
Jerry Yu4caf3ca2023-11-15 16:13:47 +08001805 return -1;
Jerry Yu82fd6c12023-10-31 16:32:19 +08001806
1807 }
Jerry Yu985c9672022-12-04 14:06:30 +08001808
Pengyu Lv94a42cc2023-12-06 10:04:17 +08001809 if (!mbedtls_ssl_tls13_session_ticket_allow_early_data(ssl->session_negotiate)) {
Jerry Yufceddb32022-12-12 15:30:34 +08001810 MBEDTLS_SSL_DEBUG_MSG(
1811 1,
Jerry Yuea96ac32023-11-21 17:06:36 +08001812 ("EarlyData: rejected, early_data not allowed in ticket "
1813 "permission bits."));
Jerry Yu4caf3ca2023-11-15 16:13:47 +08001814 return -1;
Jerry Yufceddb32022-12-12 15:30:34 +08001815 }
Jerry Yu985c9672022-12-04 14:06:30 +08001816
Jerry Yu4caf3ca2023-11-15 16:13:47 +08001817 return 0;
Jerry Yuab0da372023-02-08 13:55:24 +08001818}
1819#endif /* MBEDTLS_SSL_EARLY_DATA */
1820
XiaokangQian75fe8c72022-06-15 09:42:45 +00001821/* Update the handshake state machine */
1822
Ronald Cronce7d76e2022-07-08 18:56:49 +02001823MBEDTLS_CHECK_RETURN_CRITICAL
Ronald Cron7b6ee942024-01-12 10:29:55 +01001824static int ssl_tls13_postprocess_client_hello(mbedtls_ssl_context *ssl,
1825 int hrr_required)
XiaokangQian75fe8c72022-06-15 09:42:45 +00001826{
1827 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1828
XiaokangQian7807f9f2022-02-15 10:04:37 +00001829 /*
XiaokangQianfb665a82022-06-15 03:57:21 +00001830 * Server certificate selection
1831 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001832 if (ssl->conf->f_cert_cb && (ret = ssl->conf->f_cert_cb(ssl)) != 0) {
1833 MBEDTLS_SSL_DEBUG_RET(1, "f_cert_cb", ret);
1834 return ret;
XiaokangQianfb665a82022-06-15 03:57:21 +00001835 }
1836#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
1837 ssl->handshake->sni_name = NULL;
1838 ssl->handshake->sni_name_len = 0;
1839#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
XiaokangQian7807f9f2022-02-15 10:04:37 +00001840
Gilles Peskine449bd832023-01-11 14:50:10 +01001841 ret = mbedtls_ssl_tls13_key_schedule_stage_early(ssl);
1842 if (ret != 0) {
1843 MBEDTLS_SSL_DEBUG_RET(1,
1844 "mbedtls_ssl_tls1_3_key_schedule_stage_early", ret);
1845 return ret;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001846 }
1847
Jerry Yuab0da372023-02-08 13:55:24 +08001848#if defined(MBEDTLS_SSL_EARLY_DATA)
Jerry Yu4caf3ca2023-11-15 16:13:47 +08001849 if (ssl->handshake->received_extensions & MBEDTLS_SSL_EXT_MASK(EARLY_DATA)) {
Ronald Cron31e2d832024-02-05 16:45:57 +01001850 ssl->handshake->early_data_accepted =
1851 (!hrr_required) && (ssl_tls13_check_early_data_requirements(ssl) == 0);
Jerry Yu4e9b70e2022-12-04 14:08:02 +08001852
Jerry Yu4caf3ca2023-11-15 16:13:47 +08001853 if (ssl->handshake->early_data_accepted) {
1854 ret = mbedtls_ssl_tls13_compute_early_transform(ssl);
1855 if (ret != 0) {
1856 MBEDTLS_SSL_DEBUG_RET(
1857 1, "mbedtls_ssl_tls13_compute_early_transform", ret);
1858 return ret;
1859 }
1860 } else {
1861 ssl->discard_early_data_record =
1862 hrr_required ?
1863 MBEDTLS_SSL_EARLY_DATA_DISCARD :
1864 MBEDTLS_SSL_EARLY_DATA_TRY_TO_DEPROTECT_AND_DISCARD;
Jerry Yu4e9b70e2022-12-04 14:08:02 +08001865 }
1866 }
Ronald Cron7b6ee942024-01-12 10:29:55 +01001867#else
1868 ((void) hrr_required);
Jerry Yuab0da372023-02-08 13:55:24 +08001869#endif /* MBEDTLS_SSL_EARLY_DATA */
1870
Gilles Peskine449bd832023-01-11 14:50:10 +01001871 return 0;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001872}
1873
1874/*
XiaokangQianed582dd2022-04-13 08:21:05 +00001875 * Main entry point from the state machine; orchestrates the otherfunctions.
1876 */
1877
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001878MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01001879static int ssl_tls13_process_client_hello(mbedtls_ssl_context *ssl)
XiaokangQianed582dd2022-04-13 08:21:05 +00001880{
1881
XiaokangQian08037552022-04-20 07:16:41 +00001882 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine449bd832023-01-11 14:50:10 +01001883 unsigned char *buf = NULL;
XiaokangQianed582dd2022-04-13 08:21:05 +00001884 size_t buflen = 0;
Jerry Yu4ca91402022-05-09 15:50:57 +08001885 int parse_client_hello_ret;
1886
Gilles Peskine449bd832023-01-11 14:50:10 +01001887 MBEDTLS_SSL_DEBUG_MSG(2, ("=> parse client hello"));
XiaokangQianed582dd2022-04-13 08:21:05 +00001888
Gilles Peskine449bd832023-01-11 14:50:10 +01001889 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_tls13_fetch_handshake_msg(
1890 ssl, MBEDTLS_SSL_HS_CLIENT_HELLO,
1891 &buf, &buflen));
XiaokangQianed582dd2022-04-13 08:21:05 +00001892
Gilles Peskine449bd832023-01-11 14:50:10 +01001893 MBEDTLS_SSL_PROC_CHK_NEG(ssl_tls13_parse_client_hello(ssl, buf,
1894 buf + buflen));
Ronald Cron5af4c7f2023-03-07 20:46:59 +01001895 parse_client_hello_ret = ret; /* Store positive return value of
1896 * parse_client_hello,
1897 * as negative error codes are handled
Jerry Yuf41553b2022-05-09 22:20:30 +08001898 * by MBEDTLS_SSL_PROC_CHK_NEG. */
Jerry Yu4ca91402022-05-09 15:50:57 +08001899
Ronald Cronb828c7d2023-04-03 16:37:22 +02001900 /*
Yanray Wang90acdc62023-12-08 10:29:42 +08001901 * Version 1.2 of the protocol has to be used for the handshake.
1902 * If TLS 1.2 is not supported, abort the handshake. Otherwise, set the
Ronald Cronb828c7d2023-04-03 16:37:22 +02001903 * ssl->keep_current_message flag for the ClientHello to be kept and parsed
1904 * as a TLS 1.2 ClientHello. We also change ssl->tls_version to
1905 * MBEDTLS_SSL_VERSION_TLS1_2 thus from now on mbedtls_ssl_handshake_step()
1906 * will dispatch to the TLS 1.2 state machine.
1907 */
Ronald Cron5af4c7f2023-03-07 20:46:59 +01001908 if (SSL_CLIENT_HELLO_TLS1_2 == parse_client_hello_ret) {
Yanray Wangfb0f47b2023-12-04 15:27:28 +08001909 /* Check if server supports TLS 1.2 */
Yanray Wang408ba6f2023-12-08 10:18:03 +08001910 if (!mbedtls_ssl_conf_is_tls12_enabled(ssl->conf)) {
Yanray Wangfb0f47b2023-12-04 15:27:28 +08001911 MBEDTLS_SSL_DEBUG_MSG(
Yanray Wang177e49a2023-12-08 10:51:04 +08001912 1, ("TLS 1.2 not supported."));
Yanray Wangfb0f47b2023-12-04 15:27:28 +08001913 MBEDTLS_SSL_PEND_FATAL_ALERT(
Yanray Wang2bef9172023-12-08 10:21:53 +08001914 MBEDTLS_SSL_ALERT_MSG_PROTOCOL_VERSION,
1915 MBEDTLS_ERR_SSL_BAD_PROTOCOL_VERSION);
1916 return MBEDTLS_ERR_SSL_BAD_PROTOCOL_VERSION;
Yanray Wangfb0f47b2023-12-04 15:27:28 +08001917 }
Ronald Cron5af4c7f2023-03-07 20:46:59 +01001918 ssl->keep_current_message = 1;
1919 ssl->tls_version = MBEDTLS_SSL_VERSION_TLS1_2;
1920 return 0;
1921 }
1922
Ronald Cron7b6ee942024-01-12 10:29:55 +01001923 MBEDTLS_SSL_PROC_CHK(
1924 ssl_tls13_postprocess_client_hello(ssl, parse_client_hello_ret ==
1925 SSL_CLIENT_HELLO_HRR_REQUIRED));
Jerry Yu582dd062022-04-22 21:59:01 +08001926
Ronald Cron5af4c7f2023-03-07 20:46:59 +01001927 if (SSL_CLIENT_HELLO_OK == parse_client_hello_ret) {
Gilles Peskine449bd832023-01-11 14:50:10 +01001928 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_SERVER_HELLO);
1929 } else {
1930 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_HELLO_RETRY_REQUEST);
1931 }
XiaokangQianed582dd2022-04-13 08:21:05 +00001932
1933cleanup:
1934
Gilles Peskine449bd832023-01-11 14:50:10 +01001935 MBEDTLS_SSL_DEBUG_MSG(2, ("<= parse client hello"));
1936 return ret;
XiaokangQianed582dd2022-04-13 08:21:05 +00001937}
1938
1939/*
Jerry Yu1c3e6882022-04-20 21:23:40 +08001940 * Handler for MBEDTLS_SSL_SERVER_HELLO
Jerry Yu5b64ae92022-03-30 17:15:02 +08001941 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001942MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01001943static int ssl_tls13_prepare_server_hello(mbedtls_ssl_context *ssl)
Jerry Yuf4b27e42022-03-30 17:32:21 +08001944{
Jerry Yu637a3f12022-04-20 21:37:58 +08001945 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1946 unsigned char *server_randbytes =
Gilles Peskine449bd832023-01-11 14:50:10 +01001947 ssl->handshake->randbytes + MBEDTLS_CLIENT_HELLO_RANDOM_LEN;
1948 if (ssl->conf->f_rng == NULL) {
1949 MBEDTLS_SSL_DEBUG_MSG(1, ("no RNG provided"));
1950 return MBEDTLS_ERR_SSL_NO_RNG;
Jerry Yuf4b27e42022-03-30 17:32:21 +08001951 }
1952
Gilles Peskine449bd832023-01-11 14:50:10 +01001953 if ((ret = ssl->conf->f_rng(ssl->conf->p_rng, server_randbytes,
1954 MBEDTLS_SERVER_HELLO_RANDOM_LEN)) != 0) {
1955 MBEDTLS_SSL_DEBUG_RET(1, "f_rng", ret);
1956 return ret;
Jerry Yuf4b27e42022-03-30 17:32:21 +08001957 }
1958
Gilles Peskine449bd832023-01-11 14:50:10 +01001959 MBEDTLS_SSL_DEBUG_BUF(3, "server hello, random bytes", server_randbytes,
1960 MBEDTLS_SERVER_HELLO_RANDOM_LEN);
Jerry Yuf4b27e42022-03-30 17:32:21 +08001961
1962#if defined(MBEDTLS_HAVE_TIME)
YxCda609132023-05-22 12:08:12 -07001963 ssl->session_negotiate->start = mbedtls_time(NULL);
Jerry Yuf4b27e42022-03-30 17:32:21 +08001964#endif /* MBEDTLS_HAVE_TIME */
1965
Gilles Peskine449bd832023-01-11 14:50:10 +01001966 return ret;
Jerry Yuf4b27e42022-03-30 17:32:21 +08001967}
1968
Jerry Yu3bf2c642022-03-30 22:02:12 +08001969/*
Jerry Yue74e04a2022-04-21 09:23:16 +08001970 * ssl_tls13_write_server_hello_supported_versions_ext ():
Jerry Yu3bf2c642022-03-30 22:02:12 +08001971 *
1972 * struct {
Jerry Yufb9f54d2022-04-06 10:08:34 +08001973 * ProtocolVersion selected_version;
Jerry Yu3bf2c642022-03-30 22:02:12 +08001974 * } SupportedVersions;
1975 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001976MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yue74e04a2022-04-21 09:23:16 +08001977static int ssl_tls13_write_server_hello_supported_versions_ext(
Gilles Peskine449bd832023-01-11 14:50:10 +01001978 mbedtls_ssl_context *ssl,
1979 unsigned char *buf,
1980 unsigned char *end,
1981 size_t *out_len)
Jerry Yu3bf2c642022-03-30 22:02:12 +08001982{
Jerry Yu3bf2c642022-03-30 22:02:12 +08001983 *out_len = 0;
1984
Gilles Peskine449bd832023-01-11 14:50:10 +01001985 MBEDTLS_SSL_DEBUG_MSG(3, ("server hello, write selected version"));
Jerry Yu3bf2c642022-03-30 22:02:12 +08001986
1987 /* Check if we have space to write the extension:
1988 * - extension_type (2 bytes)
1989 * - extension_data_length (2 bytes)
Jerry Yu349a6132022-04-14 20:52:56 +08001990 * - selected_version (2 bytes)
Jerry Yu3bf2c642022-03-30 22:02:12 +08001991 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001992 MBEDTLS_SSL_CHK_BUF_PTR(buf, end, 6);
Jerry Yu3bf2c642022-03-30 22:02:12 +08001993
Gilles Peskine449bd832023-01-11 14:50:10 +01001994 MBEDTLS_PUT_UINT16_BE(MBEDTLS_TLS_EXT_SUPPORTED_VERSIONS, buf, 0);
Jerry Yu3bf2c642022-03-30 22:02:12 +08001995
Gilles Peskine449bd832023-01-11 14:50:10 +01001996 MBEDTLS_PUT_UINT16_BE(2, buf, 2);
Jerry Yu3bf2c642022-03-30 22:02:12 +08001997
Gilles Peskine449bd832023-01-11 14:50:10 +01001998 mbedtls_ssl_write_version(buf + 4,
1999 ssl->conf->transport,
2000 ssl->tls_version);
Jerry Yu3bf2c642022-03-30 22:02:12 +08002001
Gilles Peskine449bd832023-01-11 14:50:10 +01002002 MBEDTLS_SSL_DEBUG_MSG(3, ("supported version: [%04x]",
2003 ssl->tls_version));
Jerry Yu3bf2c642022-03-30 22:02:12 +08002004
Jerry Yu349a6132022-04-14 20:52:56 +08002005 *out_len = 6;
Jerry Yu3bf2c642022-03-30 22:02:12 +08002006
Jerry Yub95dd362022-11-08 21:19:34 +08002007 mbedtls_ssl_tls13_set_hs_sent_ext_mask(
Gilles Peskine449bd832023-01-11 14:50:10 +01002008 ssl, MBEDTLS_TLS_EXT_SUPPORTED_VERSIONS);
Jerry Yub95dd362022-11-08 21:19:34 +08002009
Gilles Peskine449bd832023-01-11 14:50:10 +01002010 return 0;
Jerry Yu3bf2c642022-03-30 22:02:12 +08002011}
2012
Jerry Yud9436a12022-04-20 22:28:09 +08002013
Jerry Yu3bf2c642022-03-30 22:02:12 +08002014
2015/* Generate and export a single key share. For hybrid KEMs, this can
2016 * be called multiple times with the different components of the hybrid. */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002017MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002018static int ssl_tls13_generate_and_write_key_share(mbedtls_ssl_context *ssl,
2019 uint16_t named_group,
2020 unsigned char *buf,
2021 unsigned char *end,
2022 size_t *out_len)
Jerry Yu3bf2c642022-03-30 22:02:12 +08002023{
2024 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yu955ddd72022-04-22 22:27:33 +08002025
2026 *out_len = 0;
2027
Valerio Settic9ae8622023-07-25 11:23:50 +02002028#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_EPHEMERAL_ENABLED)
Przemek Stekiel29c219c2023-05-31 15:21:04 +02002029 if (mbedtls_ssl_tls13_named_group_is_ecdhe(named_group) ||
Przemek Stekield5f79e72023-06-29 09:08:43 +02002030 mbedtls_ssl_tls13_named_group_is_ffdh(named_group)) {
Przemek Stekiel408569f2023-07-06 11:26:44 +02002031 ret = mbedtls_ssl_tls13_generate_and_write_xxdh_key_exchange(
Gilles Peskine449bd832023-01-11 14:50:10 +01002032 ssl, named_group, buf, end, out_len);
2033 if (ret != 0) {
Jerry Yu89e103c2022-03-30 22:43:29 +08002034 MBEDTLS_SSL_DEBUG_RET(
Przemek Stekiel408569f2023-07-06 11:26:44 +02002035 1, "mbedtls_ssl_tls13_generate_and_write_xxdh_key_exchange",
Gilles Peskine449bd832023-01-11 14:50:10 +01002036 ret);
2037 return ret;
Jerry Yu3bf2c642022-03-30 22:02:12 +08002038 }
Gilles Peskine449bd832023-01-11 14:50:10 +01002039 } else
Valerio Settic9ae8622023-07-25 11:23:50 +02002040#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_EPHEMERAL_ENABLED */
Gilles Peskine449bd832023-01-11 14:50:10 +01002041 if (0 /* Other kinds of KEMs */) {
2042 } else {
Jerry Yu955ddd72022-04-22 22:27:33 +08002043 ((void) ssl);
2044 ((void) named_group);
2045 ((void) buf);
2046 ((void) end);
Jerry Yu3bf2c642022-03-30 22:02:12 +08002047 ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
2048 }
2049
Gilles Peskine449bd832023-01-11 14:50:10 +01002050 return ret;
Jerry Yu3bf2c642022-03-30 22:02:12 +08002051}
2052
2053/*
2054 * ssl_tls13_write_key_share_ext
2055 *
2056 * Structure of key_share extension in ServerHello:
2057 *
Jerry Yu1c3e6882022-04-20 21:23:40 +08002058 * struct {
2059 * NamedGroup group;
2060 * opaque key_exchange<1..2^16-1>;
2061 * } KeyShareEntry;
2062 * struct {
2063 * KeyShareEntry server_share;
2064 * } KeyShareServerHello;
Jerry Yu3bf2c642022-03-30 22:02:12 +08002065 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002066MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002067static int ssl_tls13_write_key_share_ext(mbedtls_ssl_context *ssl,
2068 unsigned char *buf,
2069 unsigned char *end,
2070 size_t *out_len)
Jerry Yu3bf2c642022-03-30 22:02:12 +08002071{
Jerry Yu955ddd72022-04-22 22:27:33 +08002072 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yu3bf2c642022-03-30 22:02:12 +08002073 unsigned char *p = buf;
Jerry Yu57d48412022-04-20 21:50:42 +08002074 uint16_t group = ssl->handshake->offered_group_id;
Jerry Yu3bf2c642022-03-30 22:02:12 +08002075 unsigned char *server_share = buf + 4;
Jerry Yu3bf2c642022-03-30 22:02:12 +08002076 size_t key_exchange_length;
Jerry Yu3bf2c642022-03-30 22:02:12 +08002077
2078 *out_len = 0;
2079
Gilles Peskine449bd832023-01-11 14:50:10 +01002080 MBEDTLS_SSL_DEBUG_MSG(3, ("server hello, adding key share extension"));
Jerry Yu3bf2c642022-03-30 22:02:12 +08002081
Gilles Peskine449bd832023-01-11 14:50:10 +01002082 MBEDTLS_SSL_DEBUG_MSG(2, ("server hello, write selected_group: %s (%04x)",
2083 mbedtls_ssl_named_group_to_str(group),
2084 group));
Jerry Yu58af2332022-09-06 11:19:31 +08002085
Jerry Yu3bf2c642022-03-30 22:02:12 +08002086 /* Check if we have space for header and length fields:
2087 * - extension_type (2 bytes)
2088 * - extension_data_length (2 bytes)
2089 * - group (2 bytes)
2090 * - key_exchange_length (2 bytes)
2091 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002092 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 8);
2093 MBEDTLS_PUT_UINT16_BE(MBEDTLS_TLS_EXT_KEY_SHARE, p, 0);
2094 MBEDTLS_PUT_UINT16_BE(group, server_share, 0);
Jerry Yu3bf2c642022-03-30 22:02:12 +08002095 p += 8;
Jerry Yu57d48412022-04-20 21:50:42 +08002096
Jerry Yu3bf2c642022-03-30 22:02:12 +08002097 /* When we introduce PQC-ECDHE hybrids, we'll want to call this
2098 * function multiple times. */
Jerry Yu955ddd72022-04-22 22:27:33 +08002099 ret = ssl_tls13_generate_and_write_key_share(
Gilles Peskine449bd832023-01-11 14:50:10 +01002100 ssl, group, server_share + 4, end, &key_exchange_length);
2101 if (ret != 0) {
2102 return ret;
2103 }
Jerry Yu3bf2c642022-03-30 22:02:12 +08002104 p += key_exchange_length;
Jerry Yuc1be19f2022-04-23 16:11:39 +08002105
Gilles Peskine449bd832023-01-11 14:50:10 +01002106 MBEDTLS_PUT_UINT16_BE(key_exchange_length, server_share + 2, 0);
Jerry Yu3bf2c642022-03-30 22:02:12 +08002107
Gilles Peskine449bd832023-01-11 14:50:10 +01002108 MBEDTLS_PUT_UINT16_BE(p - server_share, buf, 2);
Jerry Yu3bf2c642022-03-30 22:02:12 +08002109
Jerry Yu57d48412022-04-20 21:50:42 +08002110 *out_len = p - buf;
Jerry Yu955ddd72022-04-22 22:27:33 +08002111
Gilles Peskine449bd832023-01-11 14:50:10 +01002112 mbedtls_ssl_tls13_set_hs_sent_ext_mask(ssl, MBEDTLS_TLS_EXT_KEY_SHARE);
Jerry Yub95dd362022-11-08 21:19:34 +08002113
Gilles Peskine449bd832023-01-11 14:50:10 +01002114 return 0;
Jerry Yu3bf2c642022-03-30 22:02:12 +08002115}
Jerry Yud9436a12022-04-20 22:28:09 +08002116
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002117MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002118static int ssl_tls13_write_hrr_key_share_ext(mbedtls_ssl_context *ssl,
2119 unsigned char *buf,
2120 unsigned char *end,
2121 size_t *out_len)
Jerry Yu23f7a6f2022-04-23 15:16:45 +08002122{
2123 uint16_t selected_group = ssl->handshake->hrr_selected_group;
2124 /* key_share Extension
2125 *
2126 * struct {
2127 * select (Handshake.msg_type) {
2128 * ...
2129 * case hello_retry_request:
2130 * NamedGroup selected_group;
2131 * ...
2132 * };
2133 * } KeyShare;
2134 */
2135
2136 *out_len = 0;
2137
Jerry Yub0ac10b2022-05-05 11:10:08 +08002138 /*
2139 * For a pure PSK key exchange, there is no group to agree upon. The purpose
2140 * of the HRR is then to transmit a cookie to force the client to demonstrate
2141 * reachability at their apparent network address (primarily useful for DTLS).
2142 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002143 if (!mbedtls_ssl_tls13_key_exchange_mode_with_ephemeral(ssl)) {
2144 return 0;
2145 }
Jerry Yu23f7a6f2022-04-23 15:16:45 +08002146
2147 /* We should only send the key_share extension if the client's initial
2148 * key share was not acceptable. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002149 if (ssl->handshake->offered_group_id != 0) {
2150 MBEDTLS_SSL_DEBUG_MSG(4, ("Skip key_share extension in HRR"));
2151 return 0;
Jerry Yu23f7a6f2022-04-23 15:16:45 +08002152 }
2153
Gilles Peskine449bd832023-01-11 14:50:10 +01002154 if (selected_group == 0) {
2155 MBEDTLS_SSL_DEBUG_MSG(1, ("no matching named group found"));
2156 return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
Jerry Yu23f7a6f2022-04-23 15:16:45 +08002157 }
2158
Jerry Yub0ac10b2022-05-05 11:10:08 +08002159 /* Check if we have enough space:
2160 * - extension_type (2 bytes)
2161 * - extension_data_length (2 bytes)
2162 * - selected_group (2 bytes)
2163 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002164 MBEDTLS_SSL_CHK_BUF_PTR(buf, end, 6);
Jerry Yu23f7a6f2022-04-23 15:16:45 +08002165
Gilles Peskine449bd832023-01-11 14:50:10 +01002166 MBEDTLS_PUT_UINT16_BE(MBEDTLS_TLS_EXT_KEY_SHARE, buf, 0);
2167 MBEDTLS_PUT_UINT16_BE(2, buf, 2);
2168 MBEDTLS_PUT_UINT16_BE(selected_group, buf, 4);
Jerry Yu23f7a6f2022-04-23 15:16:45 +08002169
Gilles Peskine449bd832023-01-11 14:50:10 +01002170 MBEDTLS_SSL_DEBUG_MSG(3,
2171 ("HRR selected_group: %s (%x)",
2172 mbedtls_ssl_named_group_to_str(selected_group),
2173 selected_group));
Jerry Yu23f7a6f2022-04-23 15:16:45 +08002174
2175 *out_len = 6;
Jerry Yu23f7a6f2022-04-23 15:16:45 +08002176
Gilles Peskine449bd832023-01-11 14:50:10 +01002177 mbedtls_ssl_tls13_set_hs_sent_ext_mask(ssl, MBEDTLS_TLS_EXT_KEY_SHARE);
Jerry Yub95dd362022-11-08 21:19:34 +08002178
Gilles Peskine449bd832023-01-11 14:50:10 +01002179 return 0;
Jerry Yu23f7a6f2022-04-23 15:16:45 +08002180}
Jerry Yu3bf2c642022-03-30 22:02:12 +08002181
2182/*
2183 * Structure of ServerHello message:
2184 *
2185 * struct {
2186 * ProtocolVersion legacy_version = 0x0303; // TLS v1.2
2187 * Random random;
2188 * opaque legacy_session_id_echo<0..32>;
2189 * CipherSuite cipher_suite;
2190 * uint8 legacy_compression_method = 0;
2191 * Extension extensions<6..2^16-1>;
2192 * } ServerHello;
2193 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002194MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002195static int ssl_tls13_write_server_hello_body(mbedtls_ssl_context *ssl,
2196 unsigned char *buf,
2197 unsigned char *end,
2198 size_t *out_len,
2199 int is_hrr)
Jerry Yu56404d72022-03-30 17:36:13 +08002200{
Jerry Yu955ddd72022-04-22 22:27:33 +08002201 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yu3bf2c642022-03-30 22:02:12 +08002202 unsigned char *p = buf;
Jerry Yud9436a12022-04-20 22:28:09 +08002203 unsigned char *p_extensions_len;
Jerry Yufbe3e642022-04-25 19:31:51 +08002204 size_t output_len;
Jerry Yu3bf2c642022-03-30 22:02:12 +08002205
2206 *out_len = 0;
Jerry Yu50e00e32022-10-31 14:45:01 +08002207 ssl->handshake->sent_extensions = MBEDTLS_SSL_EXT_MASK_NONE;
Jerry Yu3bf2c642022-03-30 22:02:12 +08002208
Jerry Yucfc04b32022-04-21 09:31:58 +08002209 /* ...
2210 * ProtocolVersion legacy_version = 0x0303; // TLS 1.2
2211 * ...
2212 * with ProtocolVersion defined as:
2213 * uint16 ProtocolVersion;
Jerry Yu3bf2c642022-03-30 22:02:12 +08002214 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002215 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 2);
2216 MBEDTLS_PUT_UINT16_BE(0x0303, p, 0);
Jerry Yu3bf2c642022-03-30 22:02:12 +08002217 p += 2;
2218
Jerry Yu1c3e6882022-04-20 21:23:40 +08002219 /* ...
2220 * Random random;
2221 * ...
Jerry Yucfc04b32022-04-21 09:31:58 +08002222 * with Random defined as:
2223 * opaque Random[MBEDTLS_SERVER_HELLO_RANDOM_LEN];
Jerry Yu1c3e6882022-04-20 21:23:40 +08002224 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002225 MBEDTLS_SSL_CHK_BUF_PTR(p, end, MBEDTLS_SERVER_HELLO_RANDOM_LEN);
2226 if (is_hrr) {
2227 memcpy(p, mbedtls_ssl_tls13_hello_retry_request_magic,
2228 MBEDTLS_SERVER_HELLO_RANDOM_LEN);
2229 } else {
2230 memcpy(p, &ssl->handshake->randbytes[MBEDTLS_CLIENT_HELLO_RANDOM_LEN],
2231 MBEDTLS_SERVER_HELLO_RANDOM_LEN);
Jerry Yu23f7a6f2022-04-23 15:16:45 +08002232 }
Gilles Peskine449bd832023-01-11 14:50:10 +01002233 MBEDTLS_SSL_DEBUG_BUF(3, "server hello, random bytes",
2234 p, MBEDTLS_SERVER_HELLO_RANDOM_LEN);
Jerry Yu3bf2c642022-03-30 22:02:12 +08002235 p += MBEDTLS_SERVER_HELLO_RANDOM_LEN;
2236
Jerry Yucfc04b32022-04-21 09:31:58 +08002237 /* ...
2238 * opaque legacy_session_id_echo<0..32>;
2239 * ...
Jerry Yu3bf2c642022-03-30 22:02:12 +08002240 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002241 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 1 + ssl->session_negotiate->id_len);
2242 *p++ = (unsigned char) ssl->session_negotiate->id_len;
2243 if (ssl->session_negotiate->id_len > 0) {
2244 memcpy(p, &ssl->session_negotiate->id[0],
2245 ssl->session_negotiate->id_len);
Jerry Yu3bf2c642022-03-30 22:02:12 +08002246 p += ssl->session_negotiate->id_len;
Jerry Yu955ddd72022-04-22 22:27:33 +08002247
Gilles Peskine449bd832023-01-11 14:50:10 +01002248 MBEDTLS_SSL_DEBUG_BUF(3, "session id", ssl->session_negotiate->id,
2249 ssl->session_negotiate->id_len);
Jerry Yu3bf2c642022-03-30 22:02:12 +08002250 }
2251
Jerry Yucfc04b32022-04-21 09:31:58 +08002252 /* ...
2253 * CipherSuite cipher_suite;
2254 * ...
2255 * with CipherSuite defined as:
2256 * uint8 CipherSuite[2];
Jerry Yu3bf2c642022-03-30 22:02:12 +08002257 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002258 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 2);
2259 MBEDTLS_PUT_UINT16_BE(ssl->session_negotiate->ciphersuite, p, 0);
Jerry Yu3bf2c642022-03-30 22:02:12 +08002260 p += 2;
Gilles Peskine449bd832023-01-11 14:50:10 +01002261 MBEDTLS_SSL_DEBUG_MSG(3,
2262 ("server hello, chosen ciphersuite: %s ( id=%d )",
2263 mbedtls_ssl_get_ciphersuite_name(
2264 ssl->session_negotiate->ciphersuite),
2265 ssl->session_negotiate->ciphersuite));
Jerry Yu3bf2c642022-03-30 22:02:12 +08002266
Jerry Yucfc04b32022-04-21 09:31:58 +08002267 /* ...
2268 * uint8 legacy_compression_method = 0;
2269 * ...
2270 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002271 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 1);
Thomas Daubney31e03a82022-07-25 15:59:25 +01002272 *p++ = MBEDTLS_SSL_COMPRESS_NULL;
Jerry Yu3bf2c642022-03-30 22:02:12 +08002273
Jerry Yucfc04b32022-04-21 09:31:58 +08002274 /* ...
2275 * Extension extensions<6..2^16-1>;
2276 * ...
2277 * struct {
2278 * ExtensionType extension_type; (2 bytes)
2279 * opaque extension_data<0..2^16-1>;
2280 * } Extension;
2281 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002282 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 2);
Jerry Yud9436a12022-04-20 22:28:09 +08002283 p_extensions_len = p;
Jerry Yu3bf2c642022-03-30 22:02:12 +08002284 p += 2;
2285
Gilles Peskine449bd832023-01-11 14:50:10 +01002286 if ((ret = ssl_tls13_write_server_hello_supported_versions_ext(
2287 ssl, p, end, &output_len)) != 0) {
Jerry Yu955ddd72022-04-22 22:27:33 +08002288 MBEDTLS_SSL_DEBUG_RET(
Gilles Peskine449bd832023-01-11 14:50:10 +01002289 1, "ssl_tls13_write_server_hello_supported_versions_ext", ret);
2290 return ret;
Jerry Yu3bf2c642022-03-30 22:02:12 +08002291 }
2292 p += output_len;
2293
Gilles Peskine449bd832023-01-11 14:50:10 +01002294 if (mbedtls_ssl_tls13_key_exchange_mode_with_ephemeral(ssl)) {
2295 if (is_hrr) {
2296 ret = ssl_tls13_write_hrr_key_share_ext(ssl, p, end, &output_len);
2297 } else {
2298 ret = ssl_tls13_write_key_share_ext(ssl, p, end, &output_len);
2299 }
2300 if (ret != 0) {
2301 return ret;
2302 }
Jerry Yu3bf2c642022-03-30 22:02:12 +08002303 p += output_len;
2304 }
Jerry Yu3bf2c642022-03-30 22:02:12 +08002305
Ronald Cron41a443a2022-10-04 16:38:25 +02002306#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01002307 if (!is_hrr && mbedtls_ssl_tls13_key_exchange_mode_with_psk(ssl)) {
2308 ret = ssl_tls13_write_server_pre_shared_key_ext(ssl, p, end, &output_len);
2309 if (ret != 0) {
2310 MBEDTLS_SSL_DEBUG_RET(1, "ssl_tls13_write_server_pre_shared_key_ext",
2311 ret);
2312 return ret;
Jerry Yu032b15ce2022-07-11 06:10:03 +00002313 }
2314 p += output_len;
2315 }
Ronald Cron41a443a2022-10-04 16:38:25 +02002316#endif
Jerry Yu032b15ce2022-07-11 06:10:03 +00002317
Gilles Peskine449bd832023-01-11 14:50:10 +01002318 MBEDTLS_PUT_UINT16_BE(p - p_extensions_len - 2, p_extensions_len, 0);
Jerry Yu3bf2c642022-03-30 22:02:12 +08002319
Gilles Peskine449bd832023-01-11 14:50:10 +01002320 MBEDTLS_SSL_DEBUG_BUF(4, "server hello extensions",
2321 p_extensions_len, p - p_extensions_len);
Jerry Yu3bf2c642022-03-30 22:02:12 +08002322
Jerry Yud9436a12022-04-20 22:28:09 +08002323 *out_len = p - buf;
Jerry Yu3bf2c642022-03-30 22:02:12 +08002324
Gilles Peskine449bd832023-01-11 14:50:10 +01002325 MBEDTLS_SSL_DEBUG_BUF(3, "server hello", buf, *out_len);
Jerry Yu3bf2c642022-03-30 22:02:12 +08002326
Jerry Yu7de2ff02022-11-08 21:43:46 +08002327 MBEDTLS_SSL_PRINT_EXTS(
Jerry Yu4b8f2f72022-10-31 13:31:22 +08002328 3, is_hrr ? MBEDTLS_SSL_TLS1_3_HS_HELLO_RETRY_REQUEST :
Gilles Peskine449bd832023-01-11 14:50:10 +01002329 MBEDTLS_SSL_HS_SERVER_HELLO,
2330 ssl->handshake->sent_extensions);
Jerry Yu4b8f2f72022-10-31 13:31:22 +08002331
Gilles Peskine449bd832023-01-11 14:50:10 +01002332 return ret;
Jerry Yu56404d72022-03-30 17:36:13 +08002333}
2334
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002335MBEDTLS_CHECK_RETURN_CRITICAL
Xiaokang Qian934ce6f2023-02-06 10:23:04 +00002336static int ssl_tls13_finalize_server_hello(mbedtls_ssl_context *ssl)
Jerry Yue110d252022-05-05 10:19:22 +08002337{
2338 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine449bd832023-01-11 14:50:10 +01002339 ret = mbedtls_ssl_tls13_compute_handshake_transform(ssl);
2340 if (ret != 0) {
2341 MBEDTLS_SSL_DEBUG_RET(1,
2342 "mbedtls_ssl_tls13_compute_handshake_transform",
2343 ret);
2344 return ret;
Jerry Yue110d252022-05-05 10:19:22 +08002345 }
2346
Gilles Peskine449bd832023-01-11 14:50:10 +01002347 return ret;
Jerry Yue110d252022-05-05 10:19:22 +08002348}
2349
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002350MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002351static int ssl_tls13_write_server_hello(mbedtls_ssl_context *ssl)
Jerry Yu5b64ae92022-03-30 17:15:02 +08002352{
Jerry Yu637a3f12022-04-20 21:37:58 +08002353 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yuf4b27e42022-03-30 17:32:21 +08002354 unsigned char *buf;
2355 size_t buf_len, msg_len;
2356
Gilles Peskine449bd832023-01-11 14:50:10 +01002357 MBEDTLS_SSL_DEBUG_MSG(2, ("=> write server hello"));
Jerry Yuf4b27e42022-03-30 17:32:21 +08002358
Gilles Peskine449bd832023-01-11 14:50:10 +01002359 MBEDTLS_SSL_PROC_CHK(ssl_tls13_prepare_server_hello(ssl));
Jerry Yuf4b27e42022-03-30 17:32:21 +08002360
Xiaokang Qian9f1747b2023-03-30 02:50:04 +00002361 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_start_handshake_msg(
2362 ssl, MBEDTLS_SSL_HS_SERVER_HELLO, &buf, &buf_len));
Jerry Yuf4b27e42022-03-30 17:32:21 +08002363
Gilles Peskine449bd832023-01-11 14:50:10 +01002364 MBEDTLS_SSL_PROC_CHK(ssl_tls13_write_server_hello_body(ssl, buf,
2365 buf + buf_len,
2366 &msg_len,
2367 0));
Jerry Yuf4b27e42022-03-30 17:32:21 +08002368
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +01002369 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_add_hs_msg_to_checksum(
Manuel Pégourié-Gonnard43cc1272023-02-06 11:48:19 +01002370 ssl, MBEDTLS_SSL_HS_SERVER_HELLO, buf, msg_len));
Jerry Yuf4b27e42022-03-30 17:32:21 +08002371
Gilles Peskine449bd832023-01-11 14:50:10 +01002372 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_finish_handshake_msg(
2373 ssl, buf_len, msg_len));
Jerry Yu637a3f12022-04-20 21:37:58 +08002374
Xiaokang Qian934ce6f2023-02-06 10:23:04 +00002375 MBEDTLS_SSL_PROC_CHK(ssl_tls13_finalize_server_hello(ssl));
Jerry Yue110d252022-05-05 10:19:22 +08002376
Gabor Mezei7b39bf12022-05-24 16:04:14 +02002377#if defined(MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE)
2378 /* The server sends a dummy change_cipher_spec record immediately
2379 * after its first handshake message. This may either be after
2380 * a ServerHello or a HelloRetryRequest.
2381 */
Gabor Mezei96ae9262022-06-28 11:45:18 +02002382 mbedtls_ssl_handshake_set_state(
Gilles Peskine449bd832023-01-11 14:50:10 +01002383 ssl, MBEDTLS_SSL_SERVER_CCS_AFTER_SERVER_HELLO);
Gabor Mezei7b39bf12022-05-24 16:04:14 +02002384#else
Gilles Peskine449bd832023-01-11 14:50:10 +01002385 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_ENCRYPTED_EXTENSIONS);
Gabor Mezei7b39bf12022-05-24 16:04:14 +02002386#endif /* MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE */
Jerry Yue110d252022-05-05 10:19:22 +08002387
Jerry Yuf4b27e42022-03-30 17:32:21 +08002388cleanup:
2389
Gilles Peskine449bd832023-01-11 14:50:10 +01002390 MBEDTLS_SSL_DEBUG_MSG(2, ("<= write server hello"));
2391 return ret;
Jerry Yu5b64ae92022-03-30 17:15:02 +08002392}
2393
Jerry Yu23d1a252022-05-12 18:08:59 +08002394
2395/*
2396 * Handler for MBEDTLS_SSL_HELLO_RETRY_REQUEST
2397 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002398MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002399static int ssl_tls13_prepare_hello_retry_request(mbedtls_ssl_context *ssl)
Jerry Yu23d1a252022-05-12 18:08:59 +08002400{
2401 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Ronald Cron5fbd2702024-02-14 10:03:36 +01002402 if (ssl->handshake->hello_retry_request_flag) {
Gilles Peskine449bd832023-01-11 14:50:10 +01002403 MBEDTLS_SSL_DEBUG_MSG(1, ("Too many HRRs"));
2404 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE,
2405 MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE);
2406 return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
Jerry Yu23d1a252022-05-12 18:08:59 +08002407 }
2408
2409 /*
2410 * Create stateless transcript hash for HRR
2411 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002412 MBEDTLS_SSL_DEBUG_MSG(4, ("Reset transcript for HRR"));
2413 ret = mbedtls_ssl_reset_transcript_for_hrr(ssl);
2414 if (ret != 0) {
2415 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_reset_transcript_for_hrr", ret);
2416 return ret;
Jerry Yu23d1a252022-05-12 18:08:59 +08002417 }
Gilles Peskine449bd832023-01-11 14:50:10 +01002418 mbedtls_ssl_session_reset_msg_layer(ssl, 0);
Jerry Yu23d1a252022-05-12 18:08:59 +08002419
Gilles Peskine449bd832023-01-11 14:50:10 +01002420 return 0;
Jerry Yu23d1a252022-05-12 18:08:59 +08002421}
2422
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002423MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002424static int ssl_tls13_write_hello_retry_request(mbedtls_ssl_context *ssl)
Jerry Yu23d1a252022-05-12 18:08:59 +08002425{
2426 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
2427 unsigned char *buf;
2428 size_t buf_len, msg_len;
2429
Gilles Peskine449bd832023-01-11 14:50:10 +01002430 MBEDTLS_SSL_DEBUG_MSG(2, ("=> write hello retry request"));
Jerry Yu23d1a252022-05-12 18:08:59 +08002431
Gilles Peskine449bd832023-01-11 14:50:10 +01002432 MBEDTLS_SSL_PROC_CHK(ssl_tls13_prepare_hello_retry_request(ssl));
Jerry Yu23d1a252022-05-12 18:08:59 +08002433
Gilles Peskine449bd832023-01-11 14:50:10 +01002434 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_start_handshake_msg(
2435 ssl, MBEDTLS_SSL_HS_SERVER_HELLO,
2436 &buf, &buf_len));
Jerry Yu23d1a252022-05-12 18:08:59 +08002437
Gilles Peskine449bd832023-01-11 14:50:10 +01002438 MBEDTLS_SSL_PROC_CHK(ssl_tls13_write_server_hello_body(ssl, buf,
2439 buf + buf_len,
2440 &msg_len,
2441 1));
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +01002442 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_add_hs_msg_to_checksum(
Manuel Pégourié-Gonnard43cc1272023-02-06 11:48:19 +01002443 ssl, MBEDTLS_SSL_HS_SERVER_HELLO, buf, msg_len));
Jerry Yu23d1a252022-05-12 18:08:59 +08002444
2445
Gilles Peskine449bd832023-01-11 14:50:10 +01002446 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_finish_handshake_msg(ssl, buf_len,
2447 msg_len));
Jerry Yu23d1a252022-05-12 18:08:59 +08002448
Ronald Cron5fbd2702024-02-14 10:03:36 +01002449 ssl->handshake->hello_retry_request_flag = 1;
Jerry Yu23d1a252022-05-12 18:08:59 +08002450
Gabor Mezei7b39bf12022-05-24 16:04:14 +02002451#if defined(MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE)
2452 /* The server sends a dummy change_cipher_spec record immediately
2453 * after its first handshake message. This may either be after
2454 * a ServerHello or a HelloRetryRequest.
2455 */
Gabor Mezei96ae9262022-06-28 11:45:18 +02002456 mbedtls_ssl_handshake_set_state(
Gilles Peskine449bd832023-01-11 14:50:10 +01002457 ssl, MBEDTLS_SSL_SERVER_CCS_AFTER_HELLO_RETRY_REQUEST);
Gabor Mezei7b39bf12022-05-24 16:04:14 +02002458#else
Gilles Peskine449bd832023-01-11 14:50:10 +01002459 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_CLIENT_HELLO);
Gabor Mezei7b39bf12022-05-24 16:04:14 +02002460#endif /* MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE */
Jerry Yu23d1a252022-05-12 18:08:59 +08002461
2462cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +01002463 MBEDTLS_SSL_DEBUG_MSG(2, ("<= write hello retry request"));
2464 return ret;
Jerry Yu23d1a252022-05-12 18:08:59 +08002465}
2466
Jerry Yu5b64ae92022-03-30 17:15:02 +08002467/*
Jerry Yu4d3841a2022-04-16 12:37:19 +08002468 * Handler for MBEDTLS_SSL_ENCRYPTED_EXTENSIONS
2469 */
Jerry Yu4d3841a2022-04-16 12:37:19 +08002470
2471/*
2472 * struct {
2473 * Extension extensions<0..2 ^ 16 - 1>;
2474 * } EncryptedExtensions;
2475 *
2476 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002477MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002478static int ssl_tls13_write_encrypted_extensions_body(mbedtls_ssl_context *ssl,
2479 unsigned char *buf,
2480 unsigned char *end,
2481 size_t *out_len)
Jerry Yu4d3841a2022-04-16 12:37:19 +08002482{
XiaokangQianacb39922022-06-17 10:18:48 +00002483 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yu4d3841a2022-04-16 12:37:19 +08002484 unsigned char *p = buf;
2485 size_t extensions_len = 0;
Jerry Yu8937eb42022-05-03 12:12:14 +08002486 unsigned char *p_extensions_len;
XiaokangQianacb39922022-06-17 10:18:48 +00002487 size_t output_len;
Jerry Yu9da5e5a2022-05-03 15:46:09 +08002488
Jerry Yu4d3841a2022-04-16 12:37:19 +08002489 *out_len = 0;
2490
Gilles Peskine449bd832023-01-11 14:50:10 +01002491 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 2);
Jerry Yu8937eb42022-05-03 12:12:14 +08002492 p_extensions_len = p;
Jerry Yu4d3841a2022-04-16 12:37:19 +08002493 p += 2;
2494
Jerry Yu8937eb42022-05-03 12:12:14 +08002495 ((void) ssl);
XiaokangQianacb39922022-06-17 10:18:48 +00002496 ((void) ret);
2497 ((void) output_len);
2498
2499#if defined(MBEDTLS_SSL_ALPN)
Gilles Peskine449bd832023-01-11 14:50:10 +01002500 ret = mbedtls_ssl_write_alpn_ext(ssl, p, end, &output_len);
2501 if (ret != 0) {
2502 return ret;
2503 }
XiaokangQian95d5f542022-06-24 02:29:26 +00002504 p += output_len;
XiaokangQianacb39922022-06-17 10:18:48 +00002505#endif /* MBEDTLS_SSL_ALPN */
Jerry Yu4d3841a2022-04-16 12:37:19 +08002506
Jerry Yu71c14f12022-12-12 11:10:35 +08002507#if defined(MBEDTLS_SSL_EARLY_DATA)
Ronald Cron78a38f62024-02-01 18:30:31 +01002508 if (ssl->handshake->early_data_accepted) {
Jerry Yu52335392023-11-23 18:06:06 +08002509 ret = mbedtls_ssl_tls13_write_early_data_ext(
Jerry Yuc59c5862023-12-05 10:40:49 +08002510 ssl, 0, p, end, &output_len);
Jerry Yu71c14f12022-12-12 11:10:35 +08002511 if (ret != 0) {
2512 return ret;
2513 }
2514 p += output_len;
2515 }
2516#endif /* MBEDTLS_SSL_EARLY_DATA */
2517
Waleed Elmelegy47d29462024-01-03 17:31:52 +00002518#if defined(MBEDTLS_SSL_RECORD_SIZE_LIMIT)
Waleed Elmelegyfbe42742024-01-05 18:11:10 +00002519 if (ssl->handshake->received_extensions & MBEDTLS_SSL_EXT_MASK(RECORD_SIZE_LIMIT)) {
Waleed Elmelegyd2fc90e2024-01-04 18:04:53 +00002520 ret = mbedtls_ssl_tls13_write_record_size_limit_ext(
2521 ssl, p, end, &output_len);
2522 if (ret != 0) {
2523 return ret;
2524 }
2525 p += output_len;
Waleed Elmelegy47d29462024-01-03 17:31:52 +00002526 }
Waleed Elmelegy47d29462024-01-03 17:31:52 +00002527#endif
2528
Gilles Peskine449bd832023-01-11 14:50:10 +01002529 extensions_len = (p - p_extensions_len) - 2;
2530 MBEDTLS_PUT_UINT16_BE(extensions_len, p_extensions_len, 0);
Jerry Yu8937eb42022-05-03 12:12:14 +08002531
2532 *out_len = p - buf;
Jerry Yu4d3841a2022-04-16 12:37:19 +08002533
Gilles Peskine449bd832023-01-11 14:50:10 +01002534 MBEDTLS_SSL_DEBUG_BUF(4, "encrypted extensions", buf, *out_len);
Jerry Yu4d3841a2022-04-16 12:37:19 +08002535
Jerry Yu7de2ff02022-11-08 21:43:46 +08002536 MBEDTLS_SSL_PRINT_EXTS(
Gilles Peskine449bd832023-01-11 14:50:10 +01002537 3, MBEDTLS_SSL_HS_ENCRYPTED_EXTENSIONS, ssl->handshake->sent_extensions);
Jerry Yu4b8f2f72022-10-31 13:31:22 +08002538
Gilles Peskine449bd832023-01-11 14:50:10 +01002539 return 0;
Jerry Yu4d3841a2022-04-16 12:37:19 +08002540}
2541
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002542MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002543static int ssl_tls13_write_encrypted_extensions(mbedtls_ssl_context *ssl)
Jerry Yu4d3841a2022-04-16 12:37:19 +08002544{
2545 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
2546 unsigned char *buf;
Jerry Yu39730a72022-05-03 12:14:04 +08002547 size_t buf_len, msg_len;
Jerry Yu4d3841a2022-04-16 12:37:19 +08002548
Gilles Peskine449bd832023-01-11 14:50:10 +01002549 mbedtls_ssl_set_outbound_transform(ssl,
2550 ssl->handshake->transform_handshake);
Gabor Mezei54719122022-06-28 11:34:56 +02002551 MBEDTLS_SSL_DEBUG_MSG(
Gilles Peskine449bd832023-01-11 14:50:10 +01002552 3, ("switching to handshake transform for outbound data"));
Gabor Mezei54719122022-06-28 11:34:56 +02002553
Gilles Peskine449bd832023-01-11 14:50:10 +01002554 MBEDTLS_SSL_DEBUG_MSG(2, ("=> write encrypted extensions"));
Jerry Yu4d3841a2022-04-16 12:37:19 +08002555
Xiaokang Qian9f1747b2023-03-30 02:50:04 +00002556 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_start_handshake_msg(
2557 ssl, MBEDTLS_SSL_HS_ENCRYPTED_EXTENSIONS,
2558 &buf, &buf_len));
Jerry Yu4d3841a2022-04-16 12:37:19 +08002559
Gilles Peskine449bd832023-01-11 14:50:10 +01002560 MBEDTLS_SSL_PROC_CHK(ssl_tls13_write_encrypted_extensions_body(
2561 ssl, buf, buf + buf_len, &msg_len));
Jerry Yu4d3841a2022-04-16 12:37:19 +08002562
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +01002563 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_add_hs_msg_to_checksum(
Xiaokang Qian9f1747b2023-03-30 02:50:04 +00002564 ssl, MBEDTLS_SSL_HS_ENCRYPTED_EXTENSIONS,
2565 buf, msg_len));
Jerry Yu4d3841a2022-04-16 12:37:19 +08002566
Gilles Peskine449bd832023-01-11 14:50:10 +01002567 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_finish_handshake_msg(
2568 ssl, buf_len, msg_len));
Jerry Yu4d3841a2022-04-16 12:37:19 +08002569
Ronald Cron928cbd32022-10-04 16:14:26 +02002570#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01002571 if (mbedtls_ssl_tls13_key_exchange_mode_with_psk(ssl)) {
2572 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_SERVER_FINISHED);
2573 } else {
2574 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_CERTIFICATE_REQUEST);
2575 }
Jerry Yu8937eb42022-05-03 12:12:14 +08002576#else
Gilles Peskine449bd832023-01-11 14:50:10 +01002577 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_SERVER_FINISHED);
Jerry Yu8937eb42022-05-03 12:12:14 +08002578#endif
2579
Jerry Yu4d3841a2022-04-16 12:37:19 +08002580cleanup:
2581
Gilles Peskine449bd832023-01-11 14:50:10 +01002582 MBEDTLS_SSL_DEBUG_MSG(2, ("<= write encrypted extensions"));
2583 return ret;
Jerry Yu4d3841a2022-04-16 12:37:19 +08002584}
2585
Ronald Cron928cbd32022-10-04 16:14:26 +02002586#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED)
XiaokangQiana987e1d2022-05-07 01:25:58 +00002587#define SSL_CERTIFICATE_REQUEST_SEND_REQUEST 0
2588#define SSL_CERTIFICATE_REQUEST_SKIP 1
2589/* Coordination:
2590 * Check whether a CertificateRequest message should be written.
2591 * Returns a negative code on failure, or
2592 * - SSL_CERTIFICATE_REQUEST_SEND_REQUEST
2593 * - SSL_CERTIFICATE_REQUEST_SKIP
2594 * indicating if the writing of the CertificateRequest
2595 * should be skipped or not.
2596 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002597MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002598static int ssl_tls13_certificate_request_coordinate(mbedtls_ssl_context *ssl)
XiaokangQiana987e1d2022-05-07 01:25:58 +00002599{
2600 int authmode;
2601
XiaokangQian40a35232022-05-07 09:02:40 +00002602#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Gilles Peskine449bd832023-01-11 14:50:10 +01002603 if (ssl->handshake->sni_authmode != MBEDTLS_SSL_VERIFY_UNSET) {
XiaokangQian40a35232022-05-07 09:02:40 +00002604 authmode = ssl->handshake->sni_authmode;
Gilles Peskine449bd832023-01-11 14:50:10 +01002605 } else
XiaokangQian40a35232022-05-07 09:02:40 +00002606#endif
XiaokangQiana987e1d2022-05-07 01:25:58 +00002607 authmode = ssl->conf->authmode;
2608
Gilles Peskine449bd832023-01-11 14:50:10 +01002609 if (authmode == MBEDTLS_SSL_VERIFY_NONE) {
Ronald Croneac00ad2022-09-13 10:16:31 +02002610 ssl->session_negotiate->verify_result = MBEDTLS_X509_BADCERT_SKIP_VERIFY;
Gilles Peskine449bd832023-01-11 14:50:10 +01002611 return SSL_CERTIFICATE_REQUEST_SKIP;
Ronald Croneac00ad2022-09-13 10:16:31 +02002612 }
XiaokangQiana987e1d2022-05-07 01:25:58 +00002613
XiaokangQianc3017f62022-05-13 05:55:41 +00002614 ssl->handshake->certificate_request_sent = 1;
XiaokangQian189ded22022-05-10 08:12:17 +00002615
Gilles Peskine449bd832023-01-11 14:50:10 +01002616 return SSL_CERTIFICATE_REQUEST_SEND_REQUEST;
XiaokangQiana987e1d2022-05-07 01:25:58 +00002617}
2618
XiaokangQiancec9ae62022-05-06 07:28:50 +00002619/*
2620 * struct {
2621 * opaque certificate_request_context<0..2^8-1>;
2622 * Extension extensions<2..2^16-1>;
2623 * } CertificateRequest;
2624 *
2625 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002626MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002627static int ssl_tls13_write_certificate_request_body(mbedtls_ssl_context *ssl,
2628 unsigned char *buf,
2629 const unsigned char *end,
2630 size_t *out_len)
XiaokangQiancec9ae62022-05-06 07:28:50 +00002631{
2632 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
2633 unsigned char *p = buf;
XiaokangQianec6efb92022-05-06 09:53:10 +00002634 size_t output_len = 0;
XiaokangQiancec9ae62022-05-06 07:28:50 +00002635 unsigned char *p_extensions_len;
2636
2637 *out_len = 0;
2638
2639 /* Check if we have enough space:
2640 * - certificate_request_context (1 byte)
2641 * - extensions length (2 bytes)
2642 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002643 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 3);
XiaokangQiancec9ae62022-05-06 07:28:50 +00002644
2645 /*
2646 * Write certificate_request_context
2647 */
2648 /*
2649 * We use a zero length context for the normal handshake
2650 * messages. For post-authentication handshake messages
2651 * this request context would be set to a non-zero value.
2652 */
2653 *p++ = 0x0;
2654
2655 /*
2656 * Write extensions
2657 */
2658 /* The extensions must contain the signature_algorithms. */
2659 p_extensions_len = p;
2660 p += 2;
Gilles Peskine449bd832023-01-11 14:50:10 +01002661 ret = mbedtls_ssl_write_sig_alg_ext(ssl, p, end, &output_len);
2662 if (ret != 0) {
2663 return ret;
2664 }
XiaokangQiancec9ae62022-05-06 07:28:50 +00002665
XiaokangQianec6efb92022-05-06 09:53:10 +00002666 p += output_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01002667 MBEDTLS_PUT_UINT16_BE(p - p_extensions_len - 2, p_extensions_len, 0);
XiaokangQiancec9ae62022-05-06 07:28:50 +00002668
2669 *out_len = p - buf;
2670
Jerry Yu7de2ff02022-11-08 21:43:46 +08002671 MBEDTLS_SSL_PRINT_EXTS(
Gilles Peskine449bd832023-01-11 14:50:10 +01002672 3, MBEDTLS_SSL_HS_CERTIFICATE_REQUEST, ssl->handshake->sent_extensions);
Jerry Yu4b8f2f72022-10-31 13:31:22 +08002673
Gilles Peskine449bd832023-01-11 14:50:10 +01002674 return 0;
XiaokangQiancec9ae62022-05-06 07:28:50 +00002675}
2676
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002677MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002678static int ssl_tls13_write_certificate_request(mbedtls_ssl_context *ssl)
XiaokangQiancec9ae62022-05-06 07:28:50 +00002679{
2680 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
2681
Gilles Peskine449bd832023-01-11 14:50:10 +01002682 MBEDTLS_SSL_DEBUG_MSG(2, ("=> write certificate request"));
XiaokangQiancec9ae62022-05-06 07:28:50 +00002683
Gilles Peskine449bd832023-01-11 14:50:10 +01002684 MBEDTLS_SSL_PROC_CHK_NEG(ssl_tls13_certificate_request_coordinate(ssl));
XiaokangQiancec9ae62022-05-06 07:28:50 +00002685
Gilles Peskine449bd832023-01-11 14:50:10 +01002686 if (ret == SSL_CERTIFICATE_REQUEST_SEND_REQUEST) {
XiaokangQiancec9ae62022-05-06 07:28:50 +00002687 unsigned char *buf;
2688 size_t buf_len, msg_len;
2689
Xiaokang Qian9f1747b2023-03-30 02:50:04 +00002690 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_start_handshake_msg(
2691 ssl, MBEDTLS_SSL_HS_CERTIFICATE_REQUEST,
2692 &buf, &buf_len));
XiaokangQiancec9ae62022-05-06 07:28:50 +00002693
Gilles Peskine449bd832023-01-11 14:50:10 +01002694 MBEDTLS_SSL_PROC_CHK(ssl_tls13_write_certificate_request_body(
2695 ssl, buf, buf + buf_len, &msg_len));
XiaokangQiancec9ae62022-05-06 07:28:50 +00002696
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +01002697 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_add_hs_msg_to_checksum(
Xiaokang Qian9f1747b2023-03-30 02:50:04 +00002698 ssl, MBEDTLS_SSL_HS_CERTIFICATE_REQUEST,
2699 buf, msg_len));
XiaokangQiancec9ae62022-05-06 07:28:50 +00002700
Gilles Peskine449bd832023-01-11 14:50:10 +01002701 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_finish_handshake_msg(
2702 ssl, buf_len, msg_len));
2703 } else if (ret == SSL_CERTIFICATE_REQUEST_SKIP) {
2704 MBEDTLS_SSL_DEBUG_MSG(2, ("<= skip write certificate request"));
XiaokangQiancec9ae62022-05-06 07:28:50 +00002705 ret = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01002706 } else {
2707 MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
XiaokangQiancec9ae62022-05-06 07:28:50 +00002708 ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
2709 goto cleanup;
2710 }
2711
Gilles Peskine449bd832023-01-11 14:50:10 +01002712 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_SERVER_CERTIFICATE);
XiaokangQiancec9ae62022-05-06 07:28:50 +00002713cleanup:
2714
Gilles Peskine449bd832023-01-11 14:50:10 +01002715 MBEDTLS_SSL_DEBUG_MSG(2, ("<= write certificate request"));
2716 return ret;
XiaokangQiancec9ae62022-05-06 07:28:50 +00002717}
XiaokangQiancec9ae62022-05-06 07:28:50 +00002718
Jerry Yu4d3841a2022-04-16 12:37:19 +08002719/*
Jerry Yuc6e6dbf2022-04-16 19:42:57 +08002720 * Handler for MBEDTLS_SSL_SERVER_CERTIFICATE
Jerry Yu83da34e2022-04-16 13:59:52 +08002721 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002722MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002723static int ssl_tls13_write_server_certificate(mbedtls_ssl_context *ssl)
Jerry Yu83da34e2022-04-16 13:59:52 +08002724{
Jerry Yu5a26f302022-05-10 20:46:40 +08002725 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
XiaokangQianfb665a82022-06-15 03:57:21 +00002726
2727#if defined(MBEDTLS_X509_CRT_PARSE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01002728 if ((ssl_tls13_pick_key_cert(ssl) != 0) ||
2729 mbedtls_ssl_own_cert(ssl) == NULL) {
2730 MBEDTLS_SSL_DEBUG_MSG(2, ("No certificate available."));
2731 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE,
2732 MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE);
2733 return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
Jerry Yu5a26f302022-05-10 20:46:40 +08002734 }
XiaokangQianfb665a82022-06-15 03:57:21 +00002735#endif /* MBEDTLS_X509_CRT_PARSE_C */
Jerry Yu5a26f302022-05-10 20:46:40 +08002736
Gilles Peskine449bd832023-01-11 14:50:10 +01002737 ret = mbedtls_ssl_tls13_write_certificate(ssl);
2738 if (ret != 0) {
2739 return ret;
2740 }
2741 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_CERTIFICATE_VERIFY);
2742 return 0;
Jerry Yu83da34e2022-04-16 13:59:52 +08002743}
2744
2745/*
Jerry Yuc6e6dbf2022-04-16 19:42:57 +08002746 * Handler for MBEDTLS_SSL_CERTIFICATE_VERIFY
Jerry Yu83da34e2022-04-16 13:59:52 +08002747 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002748MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002749static int ssl_tls13_write_certificate_verify(mbedtls_ssl_context *ssl)
Jerry Yu83da34e2022-04-16 13:59:52 +08002750{
Gilles Peskine449bd832023-01-11 14:50:10 +01002751 int ret = mbedtls_ssl_tls13_write_certificate_verify(ssl);
2752 if (ret != 0) {
2753 return ret;
2754 }
2755 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_SERVER_FINISHED);
2756 return 0;
Jerry Yu83da34e2022-04-16 13:59:52 +08002757}
Ronald Cron928cbd32022-10-04 16:14:26 +02002758#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED */
Jerry Yu83da34e2022-04-16 13:59:52 +08002759
Jerry Yu9b72e392023-12-01 16:27:08 +08002760/*
2761 * RFC 8446 section A.2
2762 *
2763 * | Send ServerHello
2764 * | K_send = handshake
2765 * | Send EncryptedExtensions
2766 * | [Send CertificateRequest]
2767 * Can send | [Send Certificate + CertificateVerify]
2768 * app data | Send Finished
2769 * after --> | K_send = application
2770 * here +--------+--------+
2771 * No 0-RTT | | 0-RTT
2772 * | |
2773 * K_recv = handshake | | K_recv = early data
2774 * [Skip decrypt errors] | +------> WAIT_EOED -+
2775 * | | Recv | | Recv EndOfEarlyData
2776 * | | early data | | K_recv = handshake
2777 * | +------------+ |
2778 * | |
2779 * +> WAIT_FLIGHT2 <--------+
2780 * |
2781 * +--------+--------+
2782 * No auth | | Client auth
2783 * | |
2784 * | v
2785 * | WAIT_CERT
2786 * | Recv | | Recv Certificate
2787 * | empty | v
2788 * | Certificate | WAIT_CV
2789 * | | | Recv
2790 * | v | CertificateVerify
2791 * +-> WAIT_FINISHED <---+
2792 * | Recv Finished
2793 *
2794 *
2795 * The following function handles the state changes after WAIT_FLIGHT2 in the
Jerry Yu3be85072023-12-04 09:58:54 +08002796 * above diagram. We are not going to receive early data related messages
2797 * anymore, prepare to receive the first handshake message of the client
2798 * second flight.
Jerry Yu9b72e392023-12-01 16:27:08 +08002799 */
Jerry Yu3be85072023-12-04 09:58:54 +08002800static void ssl_tls13_prepare_for_handshake_second_flight(
2801 mbedtls_ssl_context *ssl)
Jerry Yu9b72e392023-12-01 16:27:08 +08002802{
Jerry Yu9b72e392023-12-01 16:27:08 +08002803 if (ssl->handshake->certificate_request_sent) {
2804 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_CLIENT_CERTIFICATE);
2805 } else {
Jerry Yu42020fb2023-12-05 17:35:53 +08002806 MBEDTLS_SSL_DEBUG_MSG(2, ("skip parse certificate"));
Jerry Yuebb1b1d2023-12-05 11:02:15 +08002807 MBEDTLS_SSL_DEBUG_MSG(2, ("skip parse certificate verify"));
Jerry Yuebb1b1d2023-12-05 11:02:15 +08002808
Jerry Yu9b72e392023-12-01 16:27:08 +08002809 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_CLIENT_FINISHED);
2810 }
Jerry Yu9b72e392023-12-01 16:27:08 +08002811}
2812
Jerry Yu83da34e2022-04-16 13:59:52 +08002813/*
Jerry Yud6e253d2022-05-18 13:59:24 +08002814 * Handler for MBEDTLS_SSL_SERVER_FINISHED
Jerry Yu69dd8d42022-04-16 12:51:26 +08002815 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002816MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002817static int ssl_tls13_write_server_finished(mbedtls_ssl_context *ssl)
Jerry Yu69dd8d42022-04-16 12:51:26 +08002818{
Jerry Yud6e253d2022-05-18 13:59:24 +08002819 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yu27bdc7c2022-04-16 13:33:27 +08002820
Gilles Peskine449bd832023-01-11 14:50:10 +01002821 ret = mbedtls_ssl_tls13_write_finished_message(ssl);
2822 if (ret != 0) {
2823 return ret;
2824 }
Jerry Yu27bdc7c2022-04-16 13:33:27 +08002825
Gilles Peskine449bd832023-01-11 14:50:10 +01002826 ret = mbedtls_ssl_tls13_compute_application_transform(ssl);
2827 if (ret != 0) {
Jerry Yue3d67cb2022-05-19 15:33:10 +08002828 MBEDTLS_SSL_PEND_FATAL_ALERT(
Gilles Peskine449bd832023-01-11 14:50:10 +01002829 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE,
2830 MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE);
2831 return ret;
Jerry Yue3d67cb2022-05-19 15:33:10 +08002832 }
XiaokangQianc3017f62022-05-13 05:55:41 +00002833
Jerry Yu87b5ed42022-12-12 13:07:07 +08002834#if defined(MBEDTLS_SSL_EARLY_DATA)
Ronald Cron78a38f62024-02-01 18:30:31 +01002835 if (ssl->handshake->early_data_accepted) {
Jerry Yu0af63dc2023-12-01 17:14:51 +08002836 /* See RFC 8446 section A.2 for more information */
Jerry Yu87b5ed42022-12-12 13:07:07 +08002837 MBEDTLS_SSL_DEBUG_MSG(
2838 1, ("Switch to early keys for inbound traffic. "
2839 "( K_recv = early data )"));
2840 mbedtls_ssl_set_inbound_transform(
2841 ssl, ssl->handshake->transform_earlydata);
2842 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_END_OF_EARLY_DATA);
2843 return 0;
2844 }
2845#endif /* MBEDTLS_SSL_EARLY_DATA */
Jerry Yu0af63dc2023-12-01 17:14:51 +08002846 MBEDTLS_SSL_DEBUG_MSG(
2847 1, ("Switch to handshake keys for inbound traffic "
2848 "( K_recv = handshake )"));
Gilles Peskine449bd832023-01-11 14:50:10 +01002849 mbedtls_ssl_set_inbound_transform(ssl, ssl->handshake->transform_handshake);
XiaokangQian189ded22022-05-10 08:12:17 +00002850
Jerry Yu3be85072023-12-04 09:58:54 +08002851 ssl_tls13_prepare_for_handshake_second_flight(ssl);
Jerry Yu7d8c3fe2022-12-12 12:59:44 +08002852
2853 return 0;
2854}
2855
Jerry Yu87b5ed42022-12-12 13:07:07 +08002856#if defined(MBEDTLS_SSL_EARLY_DATA)
2857/*
Jerry Yu59d420f2023-12-01 16:30:34 +08002858 * Handler for MBEDTLS_SSL_END_OF_EARLY_DATA
Jerry Yu87b5ed42022-12-12 13:07:07 +08002859 */
Jerry Yu3be85072023-12-04 09:58:54 +08002860#define SSL_GOT_END_OF_EARLY_DATA 0
Jerry Yub55f9eb2023-12-05 10:27:17 +08002861#define SSL_GOT_EARLY_DATA 1
Jerry Yud5c34962023-12-01 16:32:31 +08002862/* Coordination:
Jerry Yu3be85072023-12-04 09:58:54 +08002863 * Deals with the ambiguity of not knowing if the next message is an
2864 * EndOfEarlyData message or an application message containing early data.
Jerry Yud5c34962023-12-01 16:32:31 +08002865 * Returns a negative code on failure, or
Jerry Yu3be85072023-12-04 09:58:54 +08002866 * - SSL_GOT_END_OF_EARLY_DATA
Jerry Yub55f9eb2023-12-05 10:27:17 +08002867 * - SSL_GOT_EARLY_DATA
Jerry Yud5c34962023-12-01 16:32:31 +08002868 * indicating which message is received.
2869 */
2870MBEDTLS_CHECK_RETURN_CRITICAL
2871static int ssl_tls13_end_of_early_data_coordinate(mbedtls_ssl_context *ssl)
2872{
2873 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yub4ed4602023-12-01 16:34:00 +08002874
2875 if ((ret = mbedtls_ssl_read_record(ssl, 0)) != 0) {
2876 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_read_record", ret);
2877 return ret;
2878 }
2879 ssl->keep_current_message = 1;
2880
2881 if (ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
2882 ssl->in_msg[0] == MBEDTLS_SSL_HS_END_OF_EARLY_DATA) {
Jerry Yub55f9eb2023-12-05 10:27:17 +08002883 MBEDTLS_SSL_DEBUG_MSG(3, ("Received an end_of_early_data message."));
Jerry Yu3be85072023-12-04 09:58:54 +08002884 return SSL_GOT_END_OF_EARLY_DATA;
Jerry Yub4ed4602023-12-01 16:34:00 +08002885 }
2886
2887 if (ssl->in_msgtype == MBEDTLS_SSL_MSG_APPLICATION_DATA) {
Jerry Yub55f9eb2023-12-05 10:27:17 +08002888 MBEDTLS_SSL_DEBUG_MSG(3, ("Received early data"));
Jerry Yu6a5904d2023-12-06 17:11:12 +08002889 /* RFC 8446 section 4.6.1
2890 *
2891 * A server receiving more than max_early_data_size bytes of 0-RTT data
2892 * SHOULD terminate the connection with an "unexpected_message" alert.
2893 *
2894 * TODO: Add received data size check here.
2895 */
Ronald Croned7d4bf2024-01-31 07:55:19 +01002896 if (ssl->in_offt == NULL) {
2897 /* Set the reading pointer */
2898 ssl->in_offt = ssl->in_msg;
2899 }
Jerry Yub55f9eb2023-12-05 10:27:17 +08002900 return SSL_GOT_EARLY_DATA;
Jerry Yub4ed4602023-12-01 16:34:00 +08002901 }
2902
Jerry Yu7bb40a32023-12-04 10:04:15 +08002903 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE,
2904 MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE);
Jerry Yub4ed4602023-12-01 16:34:00 +08002905 return MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE;
Jerry Yud5c34962023-12-01 16:32:31 +08002906}
2907
2908MBEDTLS_CHECK_RETURN_CRITICAL
2909static int ssl_tls13_parse_end_of_early_data(mbedtls_ssl_context *ssl,
2910 const unsigned char *buf,
2911 const unsigned char *end)
2912{
Jerry Yu75c9ab72023-12-01 16:41:40 +08002913 /* RFC 8446 section 4.5
2914 *
2915 * struct {} EndOfEarlyData;
2916 */
Jerry Yufbf03992023-12-04 10:00:37 +08002917 if (buf != end) {
2918 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR,
2919 MBEDTLS_ERR_SSL_DECODE_ERROR);
2920 return MBEDTLS_ERR_SSL_DECODE_ERROR;
2921 }
2922 return 0;
Jerry Yud5c34962023-12-01 16:32:31 +08002923}
2924
Jerry Yud5c34962023-12-01 16:32:31 +08002925/*
2926 * RFC 8446 section A.2
2927 *
2928 * | Send ServerHello
2929 * | K_send = handshake
2930 * | Send EncryptedExtensions
2931 * | [Send CertificateRequest]
2932 * Can send | [Send Certificate + CertificateVerify]
2933 * app data | Send Finished
2934 * after --> | K_send = application
2935 * here +--------+--------+
2936 * No 0-RTT | | 0-RTT
2937 * | |
2938 * K_recv = handshake | | K_recv = early data
2939 * [Skip decrypt errors] | +------> WAIT_EOED -+
2940 * | | Recv | | Recv EndOfEarlyData
2941 * | | early data | | K_recv = handshake
2942 * | +------------+ |
2943 * | |
2944 * +> WAIT_FLIGHT2 <--------+
2945 * |
2946 * +--------+--------+
2947 * No auth | | Client auth
2948 * | |
2949 * | v
2950 * | WAIT_CERT
2951 * | Recv | | Recv Certificate
2952 * | empty | v
2953 * | Certificate | WAIT_CV
2954 * | | | Recv
2955 * | v | CertificateVerify
2956 * +-> WAIT_FINISHED <---+
2957 * | Recv Finished
2958 *
2959 * The function handles actions and state changes from 0-RTT to WAIT_FLIGHT2 in
2960 * the above diagram.
2961 */
Jerry Yu87b5ed42022-12-12 13:07:07 +08002962MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yu59d420f2023-12-01 16:30:34 +08002963static int ssl_tls13_process_end_of_early_data(mbedtls_ssl_context *ssl)
Jerry Yu87b5ed42022-12-12 13:07:07 +08002964{
2965 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yud5c34962023-12-01 16:32:31 +08002966
2967 MBEDTLS_SSL_DEBUG_MSG(2, ("=> ssl_tls13_process_end_of_early_data"));
2968
2969 MBEDTLS_SSL_PROC_CHK_NEG(ssl_tls13_end_of_early_data_coordinate(ssl));
2970
Jerry Yu3be85072023-12-04 09:58:54 +08002971 if (ret == SSL_GOT_END_OF_EARLY_DATA) {
Jerry Yud5c34962023-12-01 16:32:31 +08002972 unsigned char *buf;
2973 size_t buf_len;
2974
2975 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_tls13_fetch_handshake_msg(
2976 ssl, MBEDTLS_SSL_HS_END_OF_EARLY_DATA,
2977 &buf, &buf_len));
2978
2979 MBEDTLS_SSL_PROC_CHK(ssl_tls13_parse_end_of_early_data(
2980 ssl, buf, buf + buf_len));
2981
Jerry Yue9655122023-12-01 16:44:40 +08002982 MBEDTLS_SSL_DEBUG_MSG(
2983 1, ("Switch to handshake keys for inbound traffic"
2984 "( K_recv = handshake )"));
2985 mbedtls_ssl_set_inbound_transform(
2986 ssl, ssl->handshake->transform_handshake);
2987
Jerry Yud5c34962023-12-01 16:32:31 +08002988 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_add_hs_msg_to_checksum(
2989 ssl, MBEDTLS_SSL_HS_END_OF_EARLY_DATA,
2990 buf, buf_len));
Jerry Yue9655122023-12-01 16:44:40 +08002991
Jerry Yu3be85072023-12-04 09:58:54 +08002992 ssl_tls13_prepare_for_handshake_second_flight(ssl);
Jerry Yud5c34962023-12-01 16:32:31 +08002993
Jerry Yub55f9eb2023-12-05 10:27:17 +08002994 } else if (ret == SSL_GOT_EARLY_DATA) {
Jerry Yud9ca3542023-12-06 17:23:52 +08002995 ret = MBEDTLS_ERR_SSL_RECEIVED_EARLY_DATA;
2996 goto cleanup;
Jerry Yud5c34962023-12-01 16:32:31 +08002997 } else {
2998 MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
2999 ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
3000 goto cleanup;
3001 }
3002
Jerry Yud5c34962023-12-01 16:32:31 +08003003cleanup:
3004 MBEDTLS_SSL_DEBUG_MSG(2, ("<= ssl_tls13_process_end_of_early_data"));
Jerry Yu87b5ed42022-12-12 13:07:07 +08003005 return ret;
3006}
3007#endif /* MBEDTLS_SSL_EARLY_DATA */
3008
Jerry Yu69dd8d42022-04-16 12:51:26 +08003009/*
Jerry Yud6e253d2022-05-18 13:59:24 +08003010 * Handler for MBEDTLS_SSL_CLIENT_FINISHED
Jerry Yu69dd8d42022-04-16 12:51:26 +08003011 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02003012MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01003013static int ssl_tls13_process_client_finished(mbedtls_ssl_context *ssl)
Jerry Yu69dd8d42022-04-16 12:51:26 +08003014{
Jerry Yud6e253d2022-05-18 13:59:24 +08003015 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
3016
Gilles Peskine449bd832023-01-11 14:50:10 +01003017 ret = mbedtls_ssl_tls13_process_finished_message(ssl);
3018 if (ret != 0) {
3019 return ret;
Jerry Yue3d67cb2022-05-19 15:33:10 +08003020 }
3021
Gilles Peskine449bd832023-01-11 14:50:10 +01003022 ret = mbedtls_ssl_tls13_compute_resumption_master_secret(ssl);
3023 if (ret != 0) {
Xiaokang Qian9f1747b2023-03-30 02:50:04 +00003024 MBEDTLS_SSL_DEBUG_RET(
3025 1, "mbedtls_ssl_tls13_compute_resumption_master_secret", ret);
Gilles Peskine449bd832023-01-11 14:50:10 +01003026 }
3027
3028 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_HANDSHAKE_WRAPUP);
3029 return 0;
Jerry Yu69dd8d42022-04-16 12:51:26 +08003030}
3031
3032/*
Jerry Yud6e253d2022-05-18 13:59:24 +08003033 * Handler for MBEDTLS_SSL_HANDSHAKE_WRAPUP
Jerry Yu69dd8d42022-04-16 12:51:26 +08003034 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02003035MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01003036static int ssl_tls13_handshake_wrapup(mbedtls_ssl_context *ssl)
Jerry Yu69dd8d42022-04-16 12:51:26 +08003037{
Gilles Peskine449bd832023-01-11 14:50:10 +01003038 MBEDTLS_SSL_DEBUG_MSG(2, ("handshake: done"));
Jerry Yu03ed50b2022-04-16 17:13:30 +08003039
Gilles Peskine449bd832023-01-11 14:50:10 +01003040 mbedtls_ssl_tls13_handshake_wrapup(ssl);
Jerry Yue67bef42022-07-07 07:29:42 +00003041
Pengyu Lv3643fdb2023-01-12 16:46:28 +08003042#if defined(MBEDTLS_SSL_SESSION_TICKETS) && \
3043 defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED)
Pengyu Lva1aa31b2022-12-13 13:49:59 +08003044/* TODO: Remove the check of SOME_PSK_ENABLED since SESSION_TICKETS requires
3045 * SOME_PSK_ENABLED to be enabled. Here is just to make CI happy. It is
3046 * expected to be resolved with issue#6395.
3047 */
Pengyu Lvc7af2c42022-12-01 16:33:00 +08003048 /* Sent NewSessionTicket message only when client supports PSK */
Pengyu Lvb2cfafb2023-11-14 13:56:13 +08003049 if (mbedtls_ssl_tls13_is_some_psk_supported(ssl)) {
Xiaokang Qian9f1747b2023-03-30 02:50:04 +00003050 mbedtls_ssl_handshake_set_state(
3051 ssl, MBEDTLS_SSL_TLS1_3_NEW_SESSION_TICKET);
Pengyu Lvc7af2c42022-12-01 16:33:00 +08003052 } else
Jerry Yufca4d572022-07-21 10:37:48 +08003053#endif
Pengyu Lv3643fdb2023-01-12 16:46:28 +08003054 {
3055 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_HANDSHAKE_OVER);
3056 }
Gilles Peskine449bd832023-01-11 14:50:10 +01003057 return 0;
Jerry Yu69dd8d42022-04-16 12:51:26 +08003058}
3059
3060/*
Jerry Yua8d3c502022-10-30 14:51:23 +08003061 * Handler for MBEDTLS_SSL_TLS1_3_NEW_SESSION_TICKET
Jerry Yue67bef42022-07-07 07:29:42 +00003062 */
3063#define SSL_NEW_SESSION_TICKET_SKIP 0
3064#define SSL_NEW_SESSION_TICKET_WRITE 1
3065MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01003066static int ssl_tls13_write_new_session_ticket_coordinate(mbedtls_ssl_context *ssl)
Jerry Yue67bef42022-07-07 07:29:42 +00003067{
3068 /* Check whether the use of session tickets is enabled */
Gilles Peskine449bd832023-01-11 14:50:10 +01003069 if (ssl->conf->f_ticket_write == NULL) {
3070 MBEDTLS_SSL_DEBUG_MSG(2, ("NewSessionTicket: disabled,"
3071 " callback is not set"));
3072 return SSL_NEW_SESSION_TICKET_SKIP;
Jerry Yud0766ec2022-09-22 10:46:57 +08003073 }
Gilles Peskine449bd832023-01-11 14:50:10 +01003074 if (ssl->conf->new_session_tickets_count == 0) {
3075 MBEDTLS_SSL_DEBUG_MSG(2, ("NewSessionTicket: disabled,"
3076 " configured count is zero"));
3077 return SSL_NEW_SESSION_TICKET_SKIP;
Jerry Yud0766ec2022-09-22 10:46:57 +08003078 }
3079
Gilles Peskine449bd832023-01-11 14:50:10 +01003080 if (ssl->handshake->new_session_tickets_count == 0) {
3081 MBEDTLS_SSL_DEBUG_MSG(2, ("NewSessionTicket: all tickets have "
3082 "been sent."));
3083 return SSL_NEW_SESSION_TICKET_SKIP;
Jerry Yue67bef42022-07-07 07:29:42 +00003084 }
3085
Gilles Peskine449bd832023-01-11 14:50:10 +01003086 return SSL_NEW_SESSION_TICKET_WRITE;
Jerry Yue67bef42022-07-07 07:29:42 +00003087}
3088
3089#if defined(MBEDTLS_SSL_SESSION_TICKETS)
3090MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01003091static int ssl_tls13_prepare_new_session_ticket(mbedtls_ssl_context *ssl,
3092 unsigned char *ticket_nonce,
3093 size_t ticket_nonce_size)
Jerry Yue67bef42022-07-07 07:29:42 +00003094{
3095 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
3096 mbedtls_ssl_session *session = ssl->session;
3097 mbedtls_ssl_ciphersuite_t *ciphersuite_info;
3098 psa_algorithm_t psa_hash_alg;
3099 int hash_length;
3100
Gilles Peskine449bd832023-01-11 14:50:10 +01003101 MBEDTLS_SSL_DEBUG_MSG(2, ("=> prepare NewSessionTicket msg"));
Jerry Yue67bef42022-07-07 07:29:42 +00003102
Pengyu Lv9f926952022-11-17 15:22:33 +08003103 /* Set ticket_flags depends on the advertised psk key exchange mode */
Pengyu Lv94a42cc2023-12-06 10:04:17 +08003104 mbedtls_ssl_tls13_session_clear_ticket_flags(
Pengyu Lv9eacb442022-12-09 14:39:19 +08003105 session, MBEDTLS_SSL_TLS1_3_TICKET_FLAGS_MASK);
Pengyu Lve6487fe2022-12-06 09:30:29 +08003106#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED)
Pengyu Lv94a42cc2023-12-06 10:04:17 +08003107 mbedtls_ssl_tls13_session_set_ticket_flags(
Pengyu Lv9eacb442022-12-09 14:39:19 +08003108 session, ssl->handshake->tls13_kex_modes);
Pengyu Lve6487fe2022-12-06 09:30:29 +08003109#endif
Jerry Yu95648b02023-12-06 15:03:34 +08003110
3111#if defined(MBEDTLS_SSL_EARLY_DATA)
3112 if (ssl->conf->early_data_enabled == MBEDTLS_SSL_EARLY_DATA_ENABLED &&
3113 ssl->conf->max_early_data_size > 0) {
Pengyu Lv94a42cc2023-12-06 10:04:17 +08003114 mbedtls_ssl_tls13_session_set_ticket_flags(
Jerry Yu95648b02023-12-06 15:03:34 +08003115 session, MBEDTLS_SSL_TLS1_3_TICKET_ALLOW_EARLY_DATA);
3116 }
3117#endif /* MBEDTLS_SSL_EARLY_DATA */
3118
Pengyu Lv4938a562023-01-16 11:28:49 +08003119 MBEDTLS_SSL_PRINT_TICKET_FLAGS(4, session->ticket_flags);
Pengyu Lv9f926952022-11-17 15:22:33 +08003120
Jerry Yue67bef42022-07-07 07:29:42 +00003121 /* Generate ticket_age_add */
Gilles Peskine449bd832023-01-11 14:50:10 +01003122 if ((ret = ssl->conf->f_rng(ssl->conf->p_rng,
3123 (unsigned char *) &session->ticket_age_add,
3124 sizeof(session->ticket_age_add)) != 0)) {
3125 MBEDTLS_SSL_DEBUG_RET(1, "generate_ticket_age_add", ret);
3126 return ret;
Jerry Yue67bef42022-07-07 07:29:42 +00003127 }
Gilles Peskine449bd832023-01-11 14:50:10 +01003128 MBEDTLS_SSL_DEBUG_MSG(3, ("ticket_age_add: %u",
3129 (unsigned int) session->ticket_age_add));
Jerry Yue67bef42022-07-07 07:29:42 +00003130
3131 /* Generate ticket_nonce */
Gilles Peskine449bd832023-01-11 14:50:10 +01003132 ret = ssl->conf->f_rng(ssl->conf->p_rng, ticket_nonce, ticket_nonce_size);
3133 if (ret != 0) {
3134 MBEDTLS_SSL_DEBUG_RET(1, "generate_ticket_nonce", ret);
3135 return ret;
Jerry Yue67bef42022-07-07 07:29:42 +00003136 }
Gilles Peskine449bd832023-01-11 14:50:10 +01003137 MBEDTLS_SSL_DEBUG_BUF(3, "ticket_nonce:",
3138 ticket_nonce, ticket_nonce_size);
Jerry Yue67bef42022-07-07 07:29:42 +00003139
3140 ciphersuite_info =
Gilles Peskine449bd832023-01-11 14:50:10 +01003141 (mbedtls_ssl_ciphersuite_t *) ssl->handshake->ciphersuite_info;
Dave Rodgman2eab4622023-10-05 13:30:37 +01003142 psa_hash_alg = mbedtls_md_psa_alg_from_type((mbedtls_md_type_t) ciphersuite_info->mac);
Gilles Peskine449bd832023-01-11 14:50:10 +01003143 hash_length = PSA_HASH_LENGTH(psa_hash_alg);
3144 if (hash_length == -1 ||
3145 (size_t) hash_length > sizeof(session->resumption_key)) {
3146 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Jerry Yufca4d572022-07-21 10:37:48 +08003147 }
3148
Jerry Yue67bef42022-07-07 07:29:42 +00003149 /* In this code the psk key length equals the length of the hash */
3150 session->resumption_key_len = hash_length;
3151 session->ciphersuite = ciphersuite_info->id;
3152
3153 /* Compute resumption key
3154 *
3155 * HKDF-Expand-Label( resumption_master_secret,
3156 * "resumption", ticket_nonce, Hash.length )
3157 */
3158 ret = mbedtls_ssl_tls13_hkdf_expand_label(
Gilles Peskine449bd832023-01-11 14:50:10 +01003159 psa_hash_alg,
3160 session->app_secrets.resumption_master_secret,
3161 hash_length,
3162 MBEDTLS_SSL_TLS1_3_LBL_WITH_LEN(resumption),
3163 ticket_nonce,
3164 ticket_nonce_size,
3165 session->resumption_key,
3166 hash_length);
Jerry Yue67bef42022-07-07 07:29:42 +00003167
Gilles Peskine449bd832023-01-11 14:50:10 +01003168 if (ret != 0) {
3169 MBEDTLS_SSL_DEBUG_RET(2,
3170 "Creating the ticket-resumed PSK failed",
3171 ret);
3172 return ret;
Jerry Yue67bef42022-07-07 07:29:42 +00003173 }
Gilles Peskine449bd832023-01-11 14:50:10 +01003174 MBEDTLS_SSL_DEBUG_BUF(3, "Ticket-resumed PSK",
3175 session->resumption_key,
3176 session->resumption_key_len);
Jerry Yue67bef42022-07-07 07:29:42 +00003177
Gilles Peskine449bd832023-01-11 14:50:10 +01003178 MBEDTLS_SSL_DEBUG_BUF(3, "resumption_master_secret",
3179 session->app_secrets.resumption_master_secret,
3180 hash_length);
Jerry Yue67bef42022-07-07 07:29:42 +00003181
Gilles Peskine449bd832023-01-11 14:50:10 +01003182 return 0;
Jerry Yue67bef42022-07-07 07:29:42 +00003183}
3184
3185/* This function creates a NewSessionTicket message in the following format:
3186 *
3187 * struct {
3188 * uint32 ticket_lifetime;
3189 * uint32 ticket_age_add;
3190 * opaque ticket_nonce<0..255>;
3191 * opaque ticket<1..2^16-1>;
3192 * Extension extensions<0..2^16-2>;
3193 * } NewSessionTicket;
3194 *
3195 * The ticket inside the NewSessionTicket message is an encrypted container
3196 * carrying the necessary information so that the server is later able to
3197 * re-start the communication.
3198 *
3199 * The following fields are placed inside the ticket by the
3200 * f_ticket_write() function:
3201 *
Jerry Yu0069abc2023-11-23 21:07:28 +08003202 * - creation time (ticket_creation_time)
3203 * - flags (ticket_flags)
Jerry Yue67bef42022-07-07 07:29:42 +00003204 * - age add (ticket_age_add)
Jerry Yu0069abc2023-11-23 21:07:28 +08003205 * - key (resumption_key)
3206 * - key length (resumption_key_len)
Jerry Yue67bef42022-07-07 07:29:42 +00003207 * - ciphersuite (ciphersuite)
Jerry Yu0069abc2023-11-23 21:07:28 +08003208 * - max_early_data_size (max_early_data_size)
Jerry Yue67bef42022-07-07 07:29:42 +00003209 */
3210MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01003211static int ssl_tls13_write_new_session_ticket_body(mbedtls_ssl_context *ssl,
3212 unsigned char *buf,
3213 unsigned char *end,
3214 size_t *out_len,
3215 unsigned char *ticket_nonce,
3216 size_t ticket_nonce_size)
Jerry Yue67bef42022-07-07 07:29:42 +00003217{
3218 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
3219 unsigned char *p = buf;
3220 mbedtls_ssl_session *session = ssl->session;
3221 size_t ticket_len;
3222 uint32_t ticket_lifetime;
Jerry Yu01da35e2022-12-12 15:09:22 +08003223 unsigned char *p_extensions_len;
Jerry Yue67bef42022-07-07 07:29:42 +00003224
3225 *out_len = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01003226 MBEDTLS_SSL_DEBUG_MSG(2, ("=> write NewSessionTicket msg"));
Jerry Yue67bef42022-07-07 07:29:42 +00003227
3228 /*
3229 * ticket_lifetime 4 bytes
3230 * ticket_age_add 4 bytes
3231 * ticket_nonce 1 + ticket_nonce_size bytes
3232 * ticket >=2 bytes
3233 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003234 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 4 + 4 + 1 + ticket_nonce_size + 2);
Jerry Yue67bef42022-07-07 07:29:42 +00003235
3236 /* Generate ticket and ticket_lifetime */
Ronald Cron3c0072b2023-11-22 10:00:14 +01003237#if defined(MBEDTLS_HAVE_TIME)
3238 session->ticket_creation_time = mbedtls_ms_time();
3239#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01003240 ret = ssl->conf->f_ticket_write(ssl->conf->p_ticket,
3241 session,
3242 p + 9 + ticket_nonce_size + 2,
3243 end,
3244 &ticket_len,
3245 &ticket_lifetime);
3246 if (ret != 0) {
3247 MBEDTLS_SSL_DEBUG_RET(1, "write_ticket", ret);
3248 return ret;
Jerry Yue67bef42022-07-07 07:29:42 +00003249 }
3250 /* RFC 8446 4.6.1
3251 * ticket_lifetime: Indicates the lifetime in seconds as a 32-bit
3252 * unsigned integer in network byte order from the time of ticket
3253 * issuance. Servers MUST NOT use any value greater than
3254 * 604800 seconds (7 days). The value of zero indicates that the
3255 * ticket should be discarded immediately. Clients MUST NOT cache
3256 * tickets for longer than 7 days, regardless of the ticket_lifetime,
3257 * and MAY delete tickets earlier based on local policy. A server
3258 * MAY treat a ticket as valid for a shorter period of time than what
3259 * is stated in the ticket_lifetime.
3260 */
Jerry Yu8e0174a2023-11-10 13:58:16 +08003261 if (ticket_lifetime > MBEDTLS_SSL_TLS1_3_MAX_ALLOWED_TICKET_LIFETIME) {
3262 ticket_lifetime = MBEDTLS_SSL_TLS1_3_MAX_ALLOWED_TICKET_LIFETIME;
Gilles Peskine449bd832023-01-11 14:50:10 +01003263 }
3264 MBEDTLS_PUT_UINT32_BE(ticket_lifetime, p, 0);
3265 MBEDTLS_SSL_DEBUG_MSG(3, ("ticket_lifetime: %u",
3266 (unsigned int) ticket_lifetime));
Jerry Yue67bef42022-07-07 07:29:42 +00003267
3268 /* Write ticket_age_add */
Gilles Peskine449bd832023-01-11 14:50:10 +01003269 MBEDTLS_PUT_UINT32_BE(session->ticket_age_add, p, 4);
3270 MBEDTLS_SSL_DEBUG_MSG(3, ("ticket_age_add: %u",
3271 (unsigned int) session->ticket_age_add));
Jerry Yue67bef42022-07-07 07:29:42 +00003272
3273 /* Write ticket_nonce */
Gilles Peskine449bd832023-01-11 14:50:10 +01003274 p[8] = (unsigned char) ticket_nonce_size;
3275 if (ticket_nonce_size > 0) {
3276 memcpy(p + 9, ticket_nonce, ticket_nonce_size);
Jerry Yue67bef42022-07-07 07:29:42 +00003277 }
3278 p += 9 + ticket_nonce_size;
3279
3280 /* Write ticket */
Gilles Peskine449bd832023-01-11 14:50:10 +01003281 MBEDTLS_PUT_UINT16_BE(ticket_len, p, 0);
Jerry Yue67bef42022-07-07 07:29:42 +00003282 p += 2;
Gilles Peskine449bd832023-01-11 14:50:10 +01003283 MBEDTLS_SSL_DEBUG_BUF(4, "ticket", p, ticket_len);
Jerry Yue67bef42022-07-07 07:29:42 +00003284 p += ticket_len;
3285
3286 /* Ticket Extensions
3287 *
Jerry Yu01da35e2022-12-12 15:09:22 +08003288 * Extension extensions<0..2^16-2>;
Jerry Yue67bef42022-07-07 07:29:42 +00003289 */
Jerry Yuedab6372022-10-31 14:37:31 +08003290 ssl->handshake->sent_extensions = MBEDTLS_SSL_EXT_MASK_NONE;
3291
Gilles Peskine449bd832023-01-11 14:50:10 +01003292 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 2);
Jerry Yu01da35e2022-12-12 15:09:22 +08003293 p_extensions_len = p;
Jerry Yue67bef42022-07-07 07:29:42 +00003294 p += 2;
3295
Jerry Yu01da35e2022-12-12 15:09:22 +08003296#if defined(MBEDTLS_SSL_EARLY_DATA)
Pengyu Lv94a42cc2023-12-06 10:04:17 +08003297 if (mbedtls_ssl_tls13_session_ticket_allow_early_data(session)) {
Jerry Yu95648b02023-12-06 15:03:34 +08003298 size_t output_len;
3299
Jerry Yuf135bac2023-11-23 18:10:51 +08003300 if ((ret = mbedtls_ssl_tls13_write_early_data_ext(
Jerry Yuc59c5862023-12-05 10:40:49 +08003301 ssl, 1, p, end, &output_len)) != 0) {
Jerry Yuf135bac2023-11-23 18:10:51 +08003302 MBEDTLS_SSL_DEBUG_RET(
3303 1, "mbedtls_ssl_tls13_write_early_data_ext", ret);
3304 return ret;
3305 }
3306 p += output_len;
Jerry Yu9e7f9bc2023-11-27 16:52:07 +08003307 } else {
3308 MBEDTLS_SSL_DEBUG_MSG(
3309 4, ("early_data not allowed, "
3310 "skip early_data extension in NewSessionTicket"));
Jerry Yu01da35e2022-12-12 15:09:22 +08003311 }
Jerry Yuf135bac2023-11-23 18:10:51 +08003312
Jerry Yu01da35e2022-12-12 15:09:22 +08003313#endif /* MBEDTLS_SSL_EARLY_DATA */
3314
3315 MBEDTLS_PUT_UINT16_BE(p - p_extensions_len - 2, p_extensions_len, 0);
3316
Jerry Yue67bef42022-07-07 07:29:42 +00003317 *out_len = p - buf;
Gilles Peskine449bd832023-01-11 14:50:10 +01003318 MBEDTLS_SSL_DEBUG_BUF(4, "ticket", buf, *out_len);
3319 MBEDTLS_SSL_DEBUG_MSG(2, ("<= write new session ticket"));
Jerry Yue67bef42022-07-07 07:29:42 +00003320
Jerry Yu7de2ff02022-11-08 21:43:46 +08003321 MBEDTLS_SSL_PRINT_EXTS(
Gilles Peskine449bd832023-01-11 14:50:10 +01003322 3, MBEDTLS_SSL_HS_NEW_SESSION_TICKET, ssl->handshake->sent_extensions);
Jerry Yu4b8f2f72022-10-31 13:31:22 +08003323
Gilles Peskine449bd832023-01-11 14:50:10 +01003324 return 0;
Jerry Yue67bef42022-07-07 07:29:42 +00003325}
3326
3327/*
Jerry Yua8d3c502022-10-30 14:51:23 +08003328 * Handler for MBEDTLS_SSL_TLS1_3_NEW_SESSION_TICKET
Jerry Yue67bef42022-07-07 07:29:42 +00003329 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003330static int ssl_tls13_write_new_session_ticket(mbedtls_ssl_context *ssl)
Jerry Yue67bef42022-07-07 07:29:42 +00003331{
3332 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
3333
Gilles Peskine449bd832023-01-11 14:50:10 +01003334 MBEDTLS_SSL_PROC_CHK_NEG(ssl_tls13_write_new_session_ticket_coordinate(ssl));
Jerry Yue67bef42022-07-07 07:29:42 +00003335
Gilles Peskine449bd832023-01-11 14:50:10 +01003336 if (ret == SSL_NEW_SESSION_TICKET_WRITE) {
Jerry Yue67bef42022-07-07 07:29:42 +00003337 unsigned char ticket_nonce[MBEDTLS_SSL_TLS1_3_TICKET_NONCE_LENGTH];
3338 unsigned char *buf;
3339 size_t buf_len, msg_len;
3340
Gilles Peskine449bd832023-01-11 14:50:10 +01003341 MBEDTLS_SSL_PROC_CHK(ssl_tls13_prepare_new_session_ticket(
3342 ssl, ticket_nonce, sizeof(ticket_nonce)));
Jerry Yue67bef42022-07-07 07:29:42 +00003343
Xiaokang Qian9f1747b2023-03-30 02:50:04 +00003344 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_start_handshake_msg(
3345 ssl, MBEDTLS_SSL_HS_NEW_SESSION_TICKET,
3346 &buf, &buf_len));
Jerry Yue67bef42022-07-07 07:29:42 +00003347
Gilles Peskine449bd832023-01-11 14:50:10 +01003348 MBEDTLS_SSL_PROC_CHK(ssl_tls13_write_new_session_ticket_body(
3349 ssl, buf, buf + buf_len, &msg_len,
3350 ticket_nonce, sizeof(ticket_nonce)));
Jerry Yue67bef42022-07-07 07:29:42 +00003351
Gilles Peskine449bd832023-01-11 14:50:10 +01003352 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_finish_handshake_msg(
3353 ssl, buf_len, msg_len));
Jerry Yufca4d572022-07-21 10:37:48 +08003354
Jerry Yu359e65f2022-09-22 23:47:43 +08003355 /* Limit session tickets count to one when resumption connection.
3356 *
3357 * See document of mbedtls_ssl_conf_new_session_tickets.
3358 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003359 if (ssl->handshake->resume == 1) {
Jerry Yu359e65f2022-09-22 23:47:43 +08003360 ssl->handshake->new_session_tickets_count = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01003361 } else {
Jerry Yu359e65f2022-09-22 23:47:43 +08003362 ssl->handshake->new_session_tickets_count--;
Gilles Peskine449bd832023-01-11 14:50:10 +01003363 }
Jerry Yub7e3fa72022-09-22 11:07:18 +08003364
Jerry Yua8d3c502022-10-30 14:51:23 +08003365 mbedtls_ssl_handshake_set_state(
Gilles Peskine449bd832023-01-11 14:50:10 +01003366 ssl, MBEDTLS_SSL_TLS1_3_NEW_SESSION_TICKET_FLUSH);
3367 } else {
3368 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_HANDSHAKE_OVER);
Jerry Yue67bef42022-07-07 07:29:42 +00003369 }
3370
Jerry Yue67bef42022-07-07 07:29:42 +00003371cleanup:
3372
Gilles Peskine449bd832023-01-11 14:50:10 +01003373 return ret;
Jerry Yue67bef42022-07-07 07:29:42 +00003374}
3375#endif /* MBEDTLS_SSL_SESSION_TICKETS */
3376
3377/*
XiaokangQiane8ff3502022-04-22 02:34:40 +00003378 * TLS 1.3 State Machine -- server side
XiaokangQian7807f9f2022-02-15 10:04:37 +00003379 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003380int mbedtls_ssl_tls13_handshake_server_step(mbedtls_ssl_context *ssl)
Jerry Yub9930e72021-08-06 17:11:51 +08003381{
XiaokangQian08037552022-04-20 07:16:41 +00003382 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
XiaokangQian7807f9f2022-02-15 10:04:37 +00003383
Gilles Peskine449bd832023-01-11 14:50:10 +01003384 if (ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER || ssl->handshake == NULL) {
3385 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
3386 }
XiaokangQian7807f9f2022-02-15 10:04:37 +00003387
Gilles Peskine449bd832023-01-11 14:50:10 +01003388 MBEDTLS_SSL_DEBUG_MSG(2, ("tls13 server state: %s(%d)",
Dave Rodgman2eab4622023-10-05 13:30:37 +01003389 mbedtls_ssl_states_str((mbedtls_ssl_states) ssl->state),
Gilles Peskine449bd832023-01-11 14:50:10 +01003390 ssl->state));
Jerry Yu6e81b272021-09-27 11:16:17 +08003391
Gilles Peskine449bd832023-01-11 14:50:10 +01003392 switch (ssl->state) {
XiaokangQian7807f9f2022-02-15 10:04:37 +00003393 /* start state */
3394 case MBEDTLS_SSL_HELLO_REQUEST:
Gilles Peskine449bd832023-01-11 14:50:10 +01003395 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_CLIENT_HELLO);
XiaokangQian08037552022-04-20 07:16:41 +00003396 ret = 0;
XiaokangQian7807f9f2022-02-15 10:04:37 +00003397 break;
3398
XiaokangQian7807f9f2022-02-15 10:04:37 +00003399 case MBEDTLS_SSL_CLIENT_HELLO:
Gilles Peskine449bd832023-01-11 14:50:10 +01003400 ret = ssl_tls13_process_client_hello(ssl);
3401 if (ret != 0) {
3402 MBEDTLS_SSL_DEBUG_RET(1, "ssl_tls13_process_client_hello", ret);
3403 }
Jerry Yuf41553b2022-05-09 22:20:30 +08003404 break;
XiaokangQian7807f9f2022-02-15 10:04:37 +00003405
Jerry Yuf41553b2022-05-09 22:20:30 +08003406 case MBEDTLS_SSL_HELLO_RETRY_REQUEST:
Gilles Peskine449bd832023-01-11 14:50:10 +01003407 ret = ssl_tls13_write_hello_retry_request(ssl);
3408 if (ret != 0) {
3409 MBEDTLS_SSL_DEBUG_RET(1, "ssl_tls13_write_hello_retry_request", ret);
3410 return ret;
Jerry Yuf41553b2022-05-09 22:20:30 +08003411 }
XiaokangQian7807f9f2022-02-15 10:04:37 +00003412 break;
3413
Jerry Yu5b64ae92022-03-30 17:15:02 +08003414 case MBEDTLS_SSL_SERVER_HELLO:
Gilles Peskine449bd832023-01-11 14:50:10 +01003415 ret = ssl_tls13_write_server_hello(ssl);
Jerry Yu5b64ae92022-03-30 17:15:02 +08003416 break;
3417
Xiaofei Baicba64af2022-02-15 10:00:56 +00003418 case MBEDTLS_SSL_ENCRYPTED_EXTENSIONS:
Gilles Peskine449bd832023-01-11 14:50:10 +01003419 ret = ssl_tls13_write_encrypted_extensions(ssl);
3420 if (ret != 0) {
3421 MBEDTLS_SSL_DEBUG_RET(1, "ssl_tls13_write_encrypted_extensions", ret);
3422 return ret;
Xiaofei Baicba64af2022-02-15 10:00:56 +00003423 }
Jerry Yu48330562022-05-06 21:35:44 +08003424 break;
Jerry Yu6a2cd9e2022-05-05 11:14:19 +08003425
Ronald Cron928cbd32022-10-04 16:14:26 +02003426#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED)
Xiaofei Bai9ca09d42022-02-14 12:57:18 +00003427 case MBEDTLS_SSL_CERTIFICATE_REQUEST:
Gilles Peskine449bd832023-01-11 14:50:10 +01003428 ret = ssl_tls13_write_certificate_request(ssl);
Xiaofei Bai9ca09d42022-02-14 12:57:18 +00003429 break;
Xiaofei Bai9ca09d42022-02-14 12:57:18 +00003430
Jerry Yu83da34e2022-04-16 13:59:52 +08003431 case MBEDTLS_SSL_SERVER_CERTIFICATE:
Gilles Peskine449bd832023-01-11 14:50:10 +01003432 ret = ssl_tls13_write_server_certificate(ssl);
Jerry Yu83da34e2022-04-16 13:59:52 +08003433 break;
3434
3435 case MBEDTLS_SSL_CERTIFICATE_VERIFY:
Gilles Peskine449bd832023-01-11 14:50:10 +01003436 ret = ssl_tls13_write_certificate_verify(ssl);
Jerry Yu83da34e2022-04-16 13:59:52 +08003437 break;
Ronald Cron928cbd32022-10-04 16:14:26 +02003438#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED */
Jerry Yu83da34e2022-04-16 13:59:52 +08003439
Gilles Peskine449bd832023-01-11 14:50:10 +01003440 /*
3441 * Injection of dummy-CCS's for middlebox compatibility
3442 */
Gabor Mezei7b39bf12022-05-24 16:04:14 +02003443#if defined(MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE)
Gabor Mezeif7044ea2022-06-28 16:01:49 +02003444 case MBEDTLS_SSL_SERVER_CCS_AFTER_HELLO_RETRY_REQUEST:
Gilles Peskine449bd832023-01-11 14:50:10 +01003445 ret = mbedtls_ssl_tls13_write_change_cipher_spec(ssl);
3446 if (ret == 0) {
3447 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_CLIENT_HELLO);
3448 }
Gabor Mezei7b39bf12022-05-24 16:04:14 +02003449 break;
3450
3451 case MBEDTLS_SSL_SERVER_CCS_AFTER_SERVER_HELLO:
Ronald Crone273f722024-02-13 18:22:26 +01003452 ret = mbedtls_ssl_tls13_write_change_cipher_spec(ssl);
3453 if (ret != 0) {
3454 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01003455 }
Ronald Cronfe59ff72024-01-24 14:31:50 +01003456 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_ENCRYPTED_EXTENSIONS);
Gabor Mezei7b39bf12022-05-24 16:04:14 +02003457 break;
3458#endif /* MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE */
3459
Jerry Yu69dd8d42022-04-16 12:51:26 +08003460 case MBEDTLS_SSL_SERVER_FINISHED:
Gilles Peskine449bd832023-01-11 14:50:10 +01003461 ret = ssl_tls13_write_server_finished(ssl);
Jerry Yu69dd8d42022-04-16 12:51:26 +08003462 break;
3463
Jerry Yu87b5ed42022-12-12 13:07:07 +08003464#if defined(MBEDTLS_SSL_EARLY_DATA)
3465 case MBEDTLS_SSL_END_OF_EARLY_DATA:
Jerry Yu59d420f2023-12-01 16:30:34 +08003466 ret = ssl_tls13_process_end_of_early_data(ssl);
Jerry Yu87b5ed42022-12-12 13:07:07 +08003467 break;
3468#endif /* MBEDTLS_SSL_EARLY_DATA */
3469
Jerry Yu69dd8d42022-04-16 12:51:26 +08003470 case MBEDTLS_SSL_CLIENT_FINISHED:
Gilles Peskine449bd832023-01-11 14:50:10 +01003471 ret = ssl_tls13_process_client_finished(ssl);
Jerry Yu69dd8d42022-04-16 12:51:26 +08003472 break;
3473
Jerry Yu69dd8d42022-04-16 12:51:26 +08003474 case MBEDTLS_SSL_HANDSHAKE_WRAPUP:
Gilles Peskine449bd832023-01-11 14:50:10 +01003475 ret = ssl_tls13_handshake_wrapup(ssl);
Jerry Yu69dd8d42022-04-16 12:51:26 +08003476 break;
3477
Ronald Cron766c0cd2022-10-18 12:17:11 +02003478#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED)
XiaokangQian6b916b12022-04-25 07:29:34 +00003479 case MBEDTLS_SSL_CLIENT_CERTIFICATE:
Gilles Peskine449bd832023-01-11 14:50:10 +01003480 ret = mbedtls_ssl_tls13_process_certificate(ssl);
3481 if (ret == 0) {
3482 if (ssl->session_negotiate->peer_cert != NULL) {
Ronald Cron209cae92022-06-07 10:30:19 +02003483 mbedtls_ssl_handshake_set_state(
Gilles Peskine449bd832023-01-11 14:50:10 +01003484 ssl, MBEDTLS_SSL_CLIENT_CERTIFICATE_VERIFY);
3485 } else {
3486 MBEDTLS_SSL_DEBUG_MSG(2, ("skip parse certificate verify"));
Ronald Cron209cae92022-06-07 10:30:19 +02003487 mbedtls_ssl_handshake_set_state(
Gilles Peskine449bd832023-01-11 14:50:10 +01003488 ssl, MBEDTLS_SSL_CLIENT_FINISHED);
Ronald Cron19385882022-06-15 16:26:13 +02003489 }
XiaokangQian6b916b12022-04-25 07:29:34 +00003490 }
3491 break;
3492
3493 case MBEDTLS_SSL_CLIENT_CERTIFICATE_VERIFY:
Gilles Peskine449bd832023-01-11 14:50:10 +01003494 ret = mbedtls_ssl_tls13_process_certificate_verify(ssl);
3495 if (ret == 0) {
XiaokangQian6b916b12022-04-25 07:29:34 +00003496 mbedtls_ssl_handshake_set_state(
Gilles Peskine449bd832023-01-11 14:50:10 +01003497 ssl, MBEDTLS_SSL_CLIENT_FINISHED);
XiaokangQian6b916b12022-04-25 07:29:34 +00003498 }
3499 break;
Ronald Cron766c0cd2022-10-18 12:17:11 +02003500#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED */
XiaokangQian6b916b12022-04-25 07:29:34 +00003501
Jerry Yue67bef42022-07-07 07:29:42 +00003502#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Jerry Yua8d3c502022-10-30 14:51:23 +08003503 case MBEDTLS_SSL_TLS1_3_NEW_SESSION_TICKET:
Gilles Peskine449bd832023-01-11 14:50:10 +01003504 ret = ssl_tls13_write_new_session_ticket(ssl);
3505 if (ret != 0) {
3506 MBEDTLS_SSL_DEBUG_RET(1,
3507 "ssl_tls13_write_new_session_ticket ",
3508 ret);
Jerry Yue67bef42022-07-07 07:29:42 +00003509 }
3510 break;
Jerry Yua8d3c502022-10-30 14:51:23 +08003511 case MBEDTLS_SSL_TLS1_3_NEW_SESSION_TICKET_FLUSH:
Jerry Yue67bef42022-07-07 07:29:42 +00003512 /* This state is necessary to do the flush of the New Session
Jerry Yua8d3c502022-10-30 14:51:23 +08003513 * Ticket message written in MBEDTLS_SSL_TLS1_3_NEW_SESSION_TICKET
Jerry Yue67bef42022-07-07 07:29:42 +00003514 * as part of ssl_prepare_handshake_step.
3515 */
3516 ret = 0;
Jerry Yud4e75002022-08-09 13:33:50 +08003517
Gilles Peskine449bd832023-01-11 14:50:10 +01003518 if (ssl->handshake->new_session_tickets_count == 0) {
3519 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_HANDSHAKE_OVER);
3520 } else {
Xiaokang Qian9f1747b2023-03-30 02:50:04 +00003521 mbedtls_ssl_handshake_set_state(
3522 ssl, MBEDTLS_SSL_TLS1_3_NEW_SESSION_TICKET);
Gilles Peskine449bd832023-01-11 14:50:10 +01003523 }
Jerry Yue67bef42022-07-07 07:29:42 +00003524 break;
3525
3526#endif /* MBEDTLS_SSL_SESSION_TICKETS */
3527
XiaokangQian7807f9f2022-02-15 10:04:37 +00003528 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01003529 MBEDTLS_SSL_DEBUG_MSG(1, ("invalid state %d", ssl->state));
3530 return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
XiaokangQian7807f9f2022-02-15 10:04:37 +00003531 }
3532
Gilles Peskine449bd832023-01-11 14:50:10 +01003533 return ret;
Jerry Yub9930e72021-08-06 17:11:51 +08003534}
Jerry Yu3cc4c2a2021-08-06 16:29:08 +08003535
Jerry Yufb4b6472022-01-27 15:03:26 +08003536#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_PROTO_TLS1_3 */