blob: f23ba767e9d958f3ae82e26c265308d13503c3e1 [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 Cron41a443a2022-10-04 16:38:25 +020042#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED)
Jerry Yue19e3b92022-07-08 12:04:51 +000043/* From RFC 8446:
44 *
45 * enum { psk_ke(0), psk_dhe_ke(1), (255) } PskKeyExchangeMode;
46 * struct {
47 * PskKeyExchangeMode ke_modes<1..255>;
48 * } PskKeyExchangeModes;
49 */
Jerry Yu6e74a7e2022-07-20 20:49:32 +080050MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +010051static int ssl_tls13_parse_key_exchange_modes_ext(mbedtls_ssl_context *ssl,
52 const unsigned char *buf,
53 const unsigned char *end)
Jerry Yue19e3b92022-07-08 12:04:51 +000054{
Jerry Yu299e31f2022-07-13 23:06:36 +080055 const unsigned char *p = buf;
Jerry Yue19e3b92022-07-08 12:04:51 +000056 size_t ke_modes_len;
57 int ke_modes = 0;
58
Jerry Yu854dd9e2022-07-15 14:28:27 +080059 /* Read ke_modes length (1 Byte) */
Gilles Peskine449bd832023-01-11 14:50:10 +010060 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, 1);
Jerry Yu299e31f2022-07-13 23:06:36 +080061 ke_modes_len = *p++;
Jerry Yue19e3b92022-07-08 12:04:51 +000062 /* Currently, there are only two PSK modes, so even without looking
63 * at the content, something's wrong if the list has more than 2 items. */
Gilles Peskine449bd832023-01-11 14:50:10 +010064 if (ke_modes_len > 2) {
65 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER,
66 MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER);
67 return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
Jerry Yu299e31f2022-07-13 23:06:36 +080068 }
Jerry Yue19e3b92022-07-08 12:04:51 +000069
Gilles Peskine449bd832023-01-11 14:50:10 +010070 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, ke_modes_len);
Jerry Yue19e3b92022-07-08 12:04:51 +000071
Gilles Peskine449bd832023-01-11 14:50:10 +010072 while (ke_modes_len-- != 0) {
73 switch (*p++) {
74 case MBEDTLS_SSL_TLS1_3_PSK_MODE_PURE:
75 ke_modes |= MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK;
76 MBEDTLS_SSL_DEBUG_MSG(3, ("Found PSK KEX MODE"));
77 break;
78 case MBEDTLS_SSL_TLS1_3_PSK_MODE_ECDHE:
79 ke_modes |= MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK_EPHEMERAL;
80 MBEDTLS_SSL_DEBUG_MSG(3, ("Found PSK_EPHEMERAL KEX MODE"));
81 break;
82 default:
83 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER,
84 MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER);
85 return MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER;
Jerry Yue19e3b92022-07-08 12:04:51 +000086 }
87 }
88
89 ssl->handshake->tls13_kex_modes = ke_modes;
Gilles Peskine449bd832023-01-11 14:50:10 +010090 return 0;
Jerry Yue19e3b92022-07-08 12:04:51 +000091}
Jerry Yu1c105562022-07-10 06:32:38 +000092
Jerry Yue9d4fc02022-08-20 19:21:15 +080093#define SSL_TLS1_3_OFFERED_PSK_NOT_MATCH 1
94#define SSL_TLS1_3_OFFERED_PSK_MATCH 0
Jerry Yu95699e72022-08-21 19:22:23 +080095
96#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Pengyu Lv29daf4a2023-10-30 17:13:30 +080097MBEDTLS_CHECK_RETURN_CRITICAL
Pengyu Lvbc4aab72023-12-01 15:37:24 +080098static int ssl_tls13_key_exchange_is_psk_available(mbedtls_ssl_context *ssl);
Pengyu Lv29daf4a2023-10-30 17:13:30 +080099MBEDTLS_CHECK_RETURN_CRITICAL
Pengyu Lvbc4aab72023-12-01 15:37:24 +0800100static int ssl_tls13_key_exchange_is_psk_ephemeral_available(mbedtls_ssl_context *ssl);
Jerry Yu95699e72022-08-21 19:22:23 +0800101
102MBEDTLS_CHECK_RETURN_CRITICAL
103static int ssl_tls13_offered_psks_check_identity_match_ticket(
Gilles Peskine449bd832023-01-11 14:50:10 +0100104 mbedtls_ssl_context *ssl,
105 const unsigned char *identity,
106 size_t identity_len,
107 uint32_t obfuscated_ticket_age,
108 mbedtls_ssl_session *session)
Jerry Yu95699e72022-08-21 19:22:23 +0800109{
Jerry Yufd310eb2022-09-06 09:16:35 +0800110 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yu95699e72022-08-21 19:22:23 +0800111 unsigned char *ticket_buffer;
Pengyu Lv29daf4a2023-10-30 17:13:30 +0800112 unsigned int key_exchanges;
Jerry Yu8d4bbba2022-09-13 14:15:48 +0800113#if defined(MBEDTLS_HAVE_TIME)
Jerry Yucebffc32022-12-15 18:00:05 +0800114 mbedtls_ms_time_t now;
115 mbedtls_ms_time_t server_age;
Jerry Yu713ce1f2023-11-17 15:32:12 +0800116 uint32_t client_age;
Jerry Yucebffc32022-12-15 18:00:05 +0800117 mbedtls_ms_time_t age_diff;
Jerry Yu8d4bbba2022-09-13 14:15:48 +0800118#endif
Jerry Yu95699e72022-08-21 19:22:23 +0800119
120 ((void) obfuscated_ticket_age);
121
Gilles Peskine449bd832023-01-11 14:50:10 +0100122 MBEDTLS_SSL_DEBUG_MSG(2, ("=> check_identity_match_ticket"));
Jerry Yu95699e72022-08-21 19:22:23 +0800123
Jerry Yufd310eb2022-09-06 09:16:35 +0800124 /* Ticket parser is not configured, Skip */
Gilles Peskine449bd832023-01-11 14:50:10 +0100125 if (ssl->conf->f_ticket_parse == NULL || identity_len == 0) {
Ronald Cron3cdcac52023-12-05 17:43:02 +0100126 return SSL_TLS1_3_OFFERED_PSK_NOT_MATCH;
Gilles Peskine449bd832023-01-11 14:50:10 +0100127 }
Jerry Yu95699e72022-08-21 19:22:23 +0800128
Jerry Yu4746b102022-09-13 11:11:48 +0800129 /* We create a copy of the encrypted ticket since the ticket parsing
130 * function is allowed to use its input buffer as an output buffer
131 * (in-place decryption). We do, however, need the original buffer for
132 * computing the PSK binder value.
Jerry Yu95699e72022-08-21 19:22:23 +0800133 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100134 ticket_buffer = mbedtls_calloc(1, identity_len);
135 if (ticket_buffer == NULL) {
136 MBEDTLS_SSL_DEBUG_MSG(1, ("buffer too small"));
137 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
Jerry Yu95699e72022-08-21 19:22:23 +0800138 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100139 memcpy(ticket_buffer, identity, identity_len);
Jerry Yu95699e72022-08-21 19:22:23 +0800140
Gilles Peskine449bd832023-01-11 14:50:10 +0100141 if ((ret = ssl->conf->f_ticket_parse(ssl->conf->p_ticket,
142 session,
143 ticket_buffer, identity_len)) != 0) {
144 if (ret == MBEDTLS_ERR_SSL_INVALID_MAC) {
145 MBEDTLS_SSL_DEBUG_MSG(3, ("ticket is not authentic"));
146 } else if (ret == MBEDTLS_ERR_SSL_SESSION_TICKET_EXPIRED) {
147 MBEDTLS_SSL_DEBUG_MSG(3, ("ticket is expired"));
148 } else {
149 MBEDTLS_SSL_DEBUG_RET(1, "ticket_parse", ret);
150 }
Jerry Yu95699e72022-08-21 19:22:23 +0800151 }
152
153 /* We delete the temporary buffer */
Gilles Peskine449bd832023-01-11 14:50:10 +0100154 mbedtls_free(ticket_buffer);
Jerry Yu95699e72022-08-21 19:22:23 +0800155
Jerry Yuce3b95e2023-10-31 16:02:04 +0800156 if (ret == 0 && session->tls_version != MBEDTLS_SSL_VERSION_TLS1_3) {
Jerry Yu7ef9fd82023-11-07 14:30:38 +0800157 MBEDTLS_SSL_DEBUG_MSG(3, ("Ticket TLS version is not 1.3."));
158 /* TODO: Define new return value for this case. */
Jerry Yuce3b95e2023-10-31 16:02:04 +0800159 ret = MBEDTLS_ERR_SSL_BAD_PROTOCOL_VERSION;
160 }
Jerry Yuce3b95e2023-10-31 16:02:04 +0800161
Gilles Peskine449bd832023-01-11 14:50:10 +0100162 if (ret != 0) {
Jerry Yu8d4bbba2022-09-13 14:15:48 +0800163 goto exit;
164 }
Jerry Yu95699e72022-08-21 19:22:23 +0800165
Pengyu Lv3eb49be2022-12-05 16:35:12 +0800166 /* RFC 8446 section 4.2.9
167 *
168 * Servers SHOULD NOT send NewSessionTicket with tickets that are not
169 * compatible with the advertised modes; however, if a server does so,
170 * the impact will just be that the client's attempts at resumption fail.
171 *
172 * We regard the ticket with incompatible key exchange modes as not match.
173 */
Pengyu Lv29daf4a2023-10-30 17:13:30 +0800174 MBEDTLS_SSL_PRINT_TICKET_FLAGS(4, session->ticket_flags);
Pengyu Lv29daf4a2023-10-30 17:13:30 +0800175
176 key_exchanges = 0;
Pengyu Lv94a42cc2023-12-06 10:04:17 +0800177 if (mbedtls_ssl_tls13_session_ticket_allow_psk_ephemeral(session) &&
Pengyu Lvbc4aab72023-12-01 15:37:24 +0800178 ssl_tls13_key_exchange_is_psk_ephemeral_available(ssl)) {
Pengyu Lv29daf4a2023-10-30 17:13:30 +0800179 key_exchanges |= MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK_EPHEMERAL;
180 }
Pengyu Lv94a42cc2023-12-06 10:04:17 +0800181 if (mbedtls_ssl_tls13_session_ticket_allow_psk(session) &&
Pengyu Lvbc4aab72023-12-01 15:37:24 +0800182 ssl_tls13_key_exchange_is_psk_available(ssl)) {
Pengyu Lv29daf4a2023-10-30 17:13:30 +0800183 key_exchanges |= MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK;
184 }
185
186 if (key_exchanges == 0) {
Pengyu Lv3eb49be2022-12-05 16:35:12 +0800187 MBEDTLS_SSL_DEBUG_MSG(3, ("No suitable key exchange mode"));
Ronald Cron3cdcac52023-12-05 17:43:02 +0100188 ret = MBEDTLS_ERR_ERROR_GENERIC_ERROR;
Pengyu Lv3eb49be2022-12-05 16:35:12 +0800189 goto exit;
190 }
191
Gilles Peskine449bd832023-01-11 14:50:10 +0100192#if defined(MBEDTLS_HAVE_TIME)
Ronald Cron3cdcac52023-12-05 17:43:02 +0100193 ret = MBEDTLS_ERR_SSL_SESSION_TICKET_EXPIRED;
Jerry Yucebffc32022-12-15 18:00:05 +0800194 now = mbedtls_ms_time();
Gilles Peskine449bd832023-01-11 14:50:10 +0100195
Jerry Yu25ba4d42023-11-10 14:12:20 +0800196 if (now < session->ticket_creation_time) {
Gilles Peskine449bd832023-01-11 14:50:10 +0100197 MBEDTLS_SSL_DEBUG_MSG(
Jerry Yu713ce1f2023-11-17 15:32:12 +0800198 3, ("Invalid ticket creation time ( now = %" MBEDTLS_PRINTF_MS_TIME
199 ", creation_time = %" MBEDTLS_PRINTF_MS_TIME " )",
Jerry Yu25ba4d42023-11-10 14:12:20 +0800200 now, session->ticket_creation_time));
Gilles Peskine449bd832023-01-11 14:50:10 +0100201 goto exit;
202 }
203
Jerry Yu25ba4d42023-11-10 14:12:20 +0800204 server_age = now - session->ticket_creation_time;
Jerry Yu95699e72022-08-21 19:22:23 +0800205
Jerry Yu8d4bbba2022-09-13 14:15:48 +0800206 /* RFC 8446 section 4.6.1
207 *
208 * Servers MUST NOT use any value greater than 604800 seconds (7 days).
209 *
210 * RFC 8446 section 4.2.11.1
211 *
212 * Clients MUST NOT attempt to use tickets which have ages greater than
213 * the "ticket_lifetime" value which was provided with the ticket.
214 *
Jerry Yu8d4bbba2022-09-13 14:15:48 +0800215 */
Jerry Yu8e0174a2023-11-10 13:58:16 +0800216 if (server_age > MBEDTLS_SSL_TLS1_3_MAX_ALLOWED_TICKET_LIFETIME * 1000) {
Jerry Yu8d4bbba2022-09-13 14:15:48 +0800217 MBEDTLS_SSL_DEBUG_MSG(
Jerry Yud84c14f2023-11-16 13:33:57 +0800218 3, ("Ticket age exceeds limitation ticket_age = %" MBEDTLS_PRINTF_MS_TIME,
Jerry Yucebffc32022-12-15 18:00:05 +0800219 server_age));
Jerry Yu8d4bbba2022-09-13 14:15:48 +0800220 goto exit;
221 }
Jerry Yu95699e72022-08-21 19:22:23 +0800222
Jerry Yu8d4bbba2022-09-13 14:15:48 +0800223 /* RFC 8446 section 4.2.10
224 *
225 * For PSKs provisioned via NewSessionTicket, a server MUST validate that
226 * the ticket age for the selected PSK identity (computed by subtracting
227 * ticket_age_add from PskIdentity.obfuscated_ticket_age modulo 2^32) is
228 * within a small tolerance of the time since the ticket was issued.
Jerry Yuf7dad3c2022-09-14 22:31:39 +0800229 *
Jerry Yu31b601a2023-11-10 11:27:21 +0800230 * NOTE: The typical accuracy of an RTC crystal is ±100 to ±20 parts per
231 * million (360 to 72 milliseconds per hour). Default tolerance
Jerry Yu9cb953a2023-11-15 09:54:11 +0800232 * window is 6s, thus in the worst case clients and servers must
Jerry Yu31b601a2023-11-10 11:27:21 +0800233 * sync up their system time every 6000/360/2~=8 hours.
Jerry Yu8d4bbba2022-09-13 14:15:48 +0800234 */
Jerry Yucebffc32022-12-15 18:00:05 +0800235 client_age = obfuscated_ticket_age - session->ticket_age_add;
Jerry Yu60e99722023-11-20 09:55:24 +0800236 age_diff = server_age - (mbedtls_ms_time_t) client_age;
Jerry Yu28e7c552023-11-10 12:05:56 +0800237 if (age_diff < -MBEDTLS_SSL_TLS1_3_TICKET_AGE_TOLERANCE ||
Jerry Yucebffc32022-12-15 18:00:05 +0800238 age_diff > MBEDTLS_SSL_TLS1_3_TICKET_AGE_TOLERANCE) {
Jerry Yu8d4bbba2022-09-13 14:15:48 +0800239 MBEDTLS_SSL_DEBUG_MSG(
Jerry Yuf16efbc2023-10-30 11:06:24 +0800240 3, ("Ticket age outside tolerance window ( diff = %"
241 MBEDTLS_PRINTF_MS_TIME ")",
Jerry Yucebffc32022-12-15 18:00:05 +0800242 age_diff));
Jerry Yu8d4bbba2022-09-13 14:15:48 +0800243 goto exit;
244 }
245
246 ret = 0;
Jerry Yu95699e72022-08-21 19:22:23 +0800247#endif /* MBEDTLS_HAVE_TIME */
Jerry Yu8d4bbba2022-09-13 14:15:48 +0800248
249exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100250 if (ret != 0) {
251 mbedtls_ssl_session_free(session);
252 }
Jerry Yu95699e72022-08-21 19:22:23 +0800253
Gilles Peskine449bd832023-01-11 14:50:10 +0100254 MBEDTLS_SSL_DEBUG_MSG(2, ("<= check_identity_match_ticket"));
255 return ret;
Jerry Yu95699e72022-08-21 19:22:23 +0800256}
257#endif /* MBEDTLS_SSL_SESSION_TICKETS */
258
Jerry Yu6e74a7e2022-07-20 20:49:32 +0800259MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yu1c105562022-07-10 06:32:38 +0000260static int ssl_tls13_offered_psks_check_identity_match(
Gilles Peskine449bd832023-01-11 14:50:10 +0100261 mbedtls_ssl_context *ssl,
262 const unsigned char *identity,
263 size_t identity_len,
264 uint32_t obfuscated_ticket_age,
265 int *psk_type,
266 mbedtls_ssl_session *session)
Jerry Yu1c105562022-07-10 06:32:38 +0000267{
Ronald Cron606671e2023-02-17 11:36:33 +0100268 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
269
Jerry Yu95699e72022-08-21 19:22:23 +0800270 ((void) session);
271 ((void) obfuscated_ticket_age);
Jerry Yu5725f1c2022-08-21 17:27:16 +0800272 *psk_type = MBEDTLS_SSL_TLS1_3_PSK_EXTERNAL;
Jerry Yu95699e72022-08-21 19:22:23 +0800273
Gilles Peskine449bd832023-01-11 14:50:10 +0100274 MBEDTLS_SSL_DEBUG_BUF(4, "identity", identity, identity_len);
Jerry Yu95699e72022-08-21 19:22:23 +0800275 ssl->handshake->resume = 0;
276
Jerry Yu95699e72022-08-21 19:22:23 +0800277#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Gilles Peskine449bd832023-01-11 14:50:10 +0100278 if (ssl_tls13_offered_psks_check_identity_match_ticket(
Jerry Yu8d4bbba2022-09-13 14:15:48 +0800279 ssl, identity, identity_len, obfuscated_ticket_age,
Gilles Peskine449bd832023-01-11 14:50:10 +0100280 session) == SSL_TLS1_3_OFFERED_PSK_MATCH) {
Jerry Yu95699e72022-08-21 19:22:23 +0800281 ssl->handshake->resume = 1;
282 *psk_type = MBEDTLS_SSL_TLS1_3_PSK_RESUMPTION;
Ronald Cron606671e2023-02-17 11:36:33 +0100283 ret = mbedtls_ssl_set_hs_psk(ssl,
284 session->resumption_key,
285 session->resumption_key_len);
286 if (ret != 0) {
287 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_set_hs_psk", ret);
288 return ret;
289 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100290
291 MBEDTLS_SSL_DEBUG_BUF(4, "Ticket-resumed PSK:",
292 session->resumption_key,
293 session->resumption_key_len);
294 MBEDTLS_SSL_DEBUG_MSG(4, ("ticket: obfuscated_ticket_age: %u",
295 (unsigned) obfuscated_ticket_age));
296 return SSL_TLS1_3_OFFERED_PSK_MATCH;
Jerry Yu95699e72022-08-21 19:22:23 +0800297 }
298#endif /* MBEDTLS_SSL_SESSION_TICKETS */
299
Jerry Yu1c105562022-07-10 06:32:38 +0000300 /* Check identity with external configured function */
Gilles Peskine449bd832023-01-11 14:50:10 +0100301 if (ssl->conf->f_psk != NULL) {
302 if (ssl->conf->f_psk(
303 ssl->conf->p_psk, ssl, identity, identity_len) == 0) {
304 return SSL_TLS1_3_OFFERED_PSK_MATCH;
Jerry Yu1c105562022-07-10 06:32:38 +0000305 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100306 return SSL_TLS1_3_OFFERED_PSK_NOT_MATCH;
Jerry Yu1c105562022-07-10 06:32:38 +0000307 }
308
Gilles Peskine449bd832023-01-11 14:50:10 +0100309 MBEDTLS_SSL_DEBUG_BUF(5, "identity", identity, identity_len);
Jerry Yu1c105562022-07-10 06:32:38 +0000310 /* Check identity with pre-configured psk */
Gilles Peskine449bd832023-01-11 14:50:10 +0100311 if (ssl->conf->psk_identity != NULL &&
Jerry Yu2f0abc92022-07-22 19:34:48 +0800312 identity_len == ssl->conf->psk_identity_len &&
Gilles Peskine449bd832023-01-11 14:50:10 +0100313 mbedtls_ct_memcmp(ssl->conf->psk_identity,
314 identity, identity_len) == 0) {
Ronald Cron606671e2023-02-17 11:36:33 +0100315 ret = mbedtls_ssl_set_hs_psk(ssl, ssl->conf->psk, ssl->conf->psk_len);
316 if (ret != 0) {
317 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_set_hs_psk", ret);
318 return ret;
319 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100320 return SSL_TLS1_3_OFFERED_PSK_MATCH;
Jerry Yu1c105562022-07-10 06:32:38 +0000321 }
322
Gilles Peskine449bd832023-01-11 14:50:10 +0100323 return SSL_TLS1_3_OFFERED_PSK_NOT_MATCH;
Jerry Yu1c105562022-07-10 06:32:38 +0000324}
325
Ronald Cron6e311272023-12-05 17:57:01 +0100326#define SSL_TLS1_3_BINDER_DOES_NOT_MATCH 1
327#define SSL_TLS1_3_BINDER_MATCH 0
Jerry Yu6e74a7e2022-07-20 20:49:32 +0800328MBEDTLS_CHECK_RETURN_CRITICAL
Xiaokang Qian9f1747b2023-03-30 02:50:04 +0000329static int ssl_tls13_offered_psks_check_binder_match(
330 mbedtls_ssl_context *ssl,
331 const unsigned char *binder, size_t binder_len,
332 int psk_type, psa_algorithm_t psk_hash_alg)
Jerry Yu1c105562022-07-10 06:32:38 +0000333{
334 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yu96a2e362022-07-21 15:11:34 +0800335
Jerry Yudaf375a2022-07-20 21:31:43 +0800336 unsigned char transcript[PSA_HASH_MAX_SIZE];
Jerry Yu1c105562022-07-10 06:32:38 +0000337 size_t transcript_len;
Jerry Yu2f0abc92022-07-22 19:34:48 +0800338 unsigned char *psk;
Jerry Yu96a2e362022-07-21 15:11:34 +0800339 size_t psk_len;
Jerry Yudaf375a2022-07-20 21:31:43 +0800340 unsigned char server_computed_binder[PSA_HASH_MAX_SIZE];
Jerry Yu1c105562022-07-10 06:32:38 +0000341
Jerry Yu1c105562022-07-10 06:32:38 +0000342 /* Get current state of handshake transcript. */
Jerry Yu29d9faa2022-08-23 17:52:45 +0800343 ret = mbedtls_ssl_get_handshake_transcript(
Manuel Pégourié-Gonnard2d6d9932023-03-28 11:38:08 +0200344 ssl, mbedtls_md_type_from_psa_alg(psk_hash_alg),
Gilles Peskine449bd832023-01-11 14:50:10 +0100345 transcript, sizeof(transcript), &transcript_len);
346 if (ret != 0) {
347 return ret;
348 }
Jerry Yu1c105562022-07-10 06:32:38 +0000349
Gilles Peskine449bd832023-01-11 14:50:10 +0100350 ret = mbedtls_ssl_tls13_export_handshake_psk(ssl, &psk, &psk_len);
351 if (ret != 0) {
352 return ret;
353 }
Jerry Yu96a2e362022-07-21 15:11:34 +0800354
Gilles Peskine449bd832023-01-11 14:50:10 +0100355 ret = mbedtls_ssl_tls13_create_psk_binder(ssl, psk_hash_alg,
356 psk, psk_len, psk_type,
357 transcript,
358 server_computed_binder);
Jerry Yu96a2e362022-07-21 15:11:34 +0800359#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +0100360 mbedtls_free((void *) psk);
Jerry Yu96a2e362022-07-21 15:11:34 +0800361#endif
Gilles Peskine449bd832023-01-11 14:50:10 +0100362 if (ret != 0) {
363 MBEDTLS_SSL_DEBUG_MSG(1, ("PSK binder calculation failed."));
364 return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
Jerry Yu1c105562022-07-10 06:32:38 +0000365 }
366
Gilles Peskine449bd832023-01-11 14:50:10 +0100367 MBEDTLS_SSL_DEBUG_BUF(3, "psk binder ( computed ): ",
368 server_computed_binder, transcript_len);
369 MBEDTLS_SSL_DEBUG_BUF(3, "psk binder ( received ): ", binder, binder_len);
Jerry Yu1c105562022-07-10 06:32:38 +0000370
Gilles Peskine449bd832023-01-11 14:50:10 +0100371 if (mbedtls_ct_memcmp(server_computed_binder, binder, binder_len) == 0) {
Ronald Cron6e311272023-12-05 17:57:01 +0100372 return SSL_TLS1_3_BINDER_MATCH;
Jerry Yu1c105562022-07-10 06:32:38 +0000373 }
374
Gilles Peskine449bd832023-01-11 14:50:10 +0100375 mbedtls_platform_zeroize(server_computed_binder,
376 sizeof(server_computed_binder));
Ronald Cron6e311272023-12-05 17:57:01 +0100377 return SSL_TLS1_3_BINDER_DOES_NOT_MATCH;
Jerry Yu1c105562022-07-10 06:32:38 +0000378}
Jerry Yu96a2e362022-07-21 15:11:34 +0800379
Jerry Yu5725f1c2022-08-21 17:27:16 +0800380MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yuf35ba382022-08-23 17:58:26 +0800381static int ssl_tls13_select_ciphersuite_for_psk(
Gilles Peskine449bd832023-01-11 14:50:10 +0100382 mbedtls_ssl_context *ssl,
383 const unsigned char *cipher_suites,
384 const unsigned char *cipher_suites_end,
385 uint16_t *selected_ciphersuite,
386 const mbedtls_ssl_ciphersuite_t **selected_ciphersuite_info)
Jerry Yu5725f1c2022-08-21 17:27:16 +0800387{
Jerry Yuf35ba382022-08-23 17:58:26 +0800388 psa_algorithm_t psk_hash_alg = PSA_ALG_SHA_256;
Jerry Yu5725f1c2022-08-21 17:27:16 +0800389
Jerry Yu0baf9072022-08-25 11:21:04 +0800390 *selected_ciphersuite = 0;
391 *selected_ciphersuite_info = NULL;
392
Jerry Yuf35ba382022-08-23 17:58:26 +0800393 /* RFC 8446, page 55.
394 *
395 * For externally established PSKs, the Hash algorithm MUST be set when the
396 * PSK is established or default to SHA-256 if no such algorithm is defined.
397 *
398 */
Jerry Yu5725f1c2022-08-21 17:27:16 +0800399
Jerry Yu5725f1c2022-08-21 17:27:16 +0800400 /*
401 * Search for a matching ciphersuite
402 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100403 for (const unsigned char *p = cipher_suites;
404 p < cipher_suites_end; p += 2) {
Jerry Yu5725f1c2022-08-21 17:27:16 +0800405 uint16_t cipher_suite;
Jerry Yuf35ba382022-08-23 17:58:26 +0800406 const mbedtls_ssl_ciphersuite_t *ciphersuite_info;
Jerry Yu5725f1c2022-08-21 17:27:16 +0800407
Gilles Peskine449bd832023-01-11 14:50:10 +0100408 cipher_suite = MBEDTLS_GET_UINT16_BE(p, 0);
409 ciphersuite_info = ssl_tls13_validate_peer_ciphersuite(ssl,
410 cipher_suite);
411 if (ciphersuite_info == NULL) {
Jerry Yu5725f1c2022-08-21 17:27:16 +0800412 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +0100413 }
Jerry Yu5725f1c2022-08-21 17:27:16 +0800414
Jerry Yu5725f1c2022-08-21 17:27:16 +0800415 /* MAC of selected ciphersuite MUST be same with PSK binder if exist.
416 * Otherwise, client should reject.
417 */
Dave Rodgman2eab4622023-10-05 13:30:37 +0100418 if (psk_hash_alg ==
419 mbedtls_md_psa_alg_from_type((mbedtls_md_type_t) ciphersuite_info->mac)) {
Jerry Yuf35ba382022-08-23 17:58:26 +0800420 *selected_ciphersuite = cipher_suite;
421 *selected_ciphersuite_info = ciphersuite_info;
Gilles Peskine449bd832023-01-11 14:50:10 +0100422 return 0;
Jerry Yuf35ba382022-08-23 17:58:26 +0800423 }
424 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100425 MBEDTLS_SSL_DEBUG_MSG(2, ("No matched ciphersuite"));
426 return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
Jerry Yuf35ba382022-08-23 17:58:26 +0800427}
Jerry Yu5725f1c2022-08-21 17:27:16 +0800428
Jerry Yu82534862022-08-30 10:42:33 +0800429#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Jerry Yuf35ba382022-08-23 17:58:26 +0800430MBEDTLS_CHECK_RETURN_CRITICAL
431static int ssl_tls13_select_ciphersuite_for_resumption(
Gilles Peskine449bd832023-01-11 14:50:10 +0100432 mbedtls_ssl_context *ssl,
433 const unsigned char *cipher_suites,
434 const unsigned char *cipher_suites_end,
435 mbedtls_ssl_session *session,
436 uint16_t *selected_ciphersuite,
437 const mbedtls_ssl_ciphersuite_t **selected_ciphersuite_info)
Jerry Yuf35ba382022-08-23 17:58:26 +0800438{
Jerry Yu82534862022-08-30 10:42:33 +0800439
Jerry Yuf35ba382022-08-23 17:58:26 +0800440 *selected_ciphersuite = 0;
441 *selected_ciphersuite_info = NULL;
Gilles Peskine449bd832023-01-11 14:50:10 +0100442 for (const unsigned char *p = cipher_suites; p < cipher_suites_end; p += 2) {
443 uint16_t cipher_suite = MBEDTLS_GET_UINT16_BE(p, 0);
Jerry Yu82534862022-08-30 10:42:33 +0800444 const mbedtls_ssl_ciphersuite_t *ciphersuite_info;
445
Gilles Peskine449bd832023-01-11 14:50:10 +0100446 if (cipher_suite != session->ciphersuite) {
Jerry Yu82534862022-08-30 10:42:33 +0800447 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +0100448 }
Jerry Yu82534862022-08-30 10:42:33 +0800449
Gilles Peskine449bd832023-01-11 14:50:10 +0100450 ciphersuite_info = ssl_tls13_validate_peer_ciphersuite(ssl,
451 cipher_suite);
452 if (ciphersuite_info == NULL) {
Jerry Yu82534862022-08-30 10:42:33 +0800453 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +0100454 }
Jerry Yu82534862022-08-30 10:42:33 +0800455
Jerry Yu4746b102022-09-13 11:11:48 +0800456 *selected_ciphersuite = cipher_suite;
Jerry Yu82534862022-08-30 10:42:33 +0800457 *selected_ciphersuite_info = ciphersuite_info;
458
Gilles Peskine449bd832023-01-11 14:50:10 +0100459 return 0;
Jerry Yu82534862022-08-30 10:42:33 +0800460 }
461
Gilles Peskine449bd832023-01-11 14:50:10 +0100462 return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
Jerry Yu5725f1c2022-08-21 17:27:16 +0800463}
Jerry Yuf35ba382022-08-23 17:58:26 +0800464
Jerry Yu82534862022-08-30 10:42:33 +0800465MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100466static int ssl_tls13_session_copy_ticket(mbedtls_ssl_session *dst,
467 const mbedtls_ssl_session *src)
Jerry Yu82534862022-08-30 10:42:33 +0800468{
Jerry Yu82534862022-08-30 10:42:33 +0800469 dst->ticket_age_add = src->ticket_age_add;
470 dst->ticket_flags = src->ticket_flags;
471 dst->resumption_key_len = src->resumption_key_len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100472 if (src->resumption_key_len == 0) {
473 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
474 }
475 memcpy(dst->resumption_key, src->resumption_key, src->resumption_key_len);
Jerry Yu4746b102022-09-13 11:11:48 +0800476
Jerry Yu33bf2402022-12-12 16:01:43 +0800477#if defined(MBEDTLS_SSL_EARLY_DATA)
478 dst->max_early_data_size = src->max_early_data_size;
479#endif
480
Gilles Peskine449bd832023-01-11 14:50:10 +0100481 return 0;
Jerry Yu82534862022-08-30 10:42:33 +0800482}
483#endif /* MBEDTLS_SSL_SESSION_TICKETS */
484
Jerry Yu1c105562022-07-10 06:32:38 +0000485/* Parser for pre_shared_key extension in client hello
486 * struct {
487 * opaque identity<1..2^16-1>;
488 * uint32 obfuscated_ticket_age;
489 * } PskIdentity;
490 *
491 * opaque PskBinderEntry<32..255>;
492 *
493 * struct {
494 * PskIdentity identities<7..2^16-1>;
495 * PskBinderEntry binders<33..2^16-1>;
496 * } OfferedPsks;
497 *
498 * struct {
499 * select (Handshake.msg_type) {
500 * case client_hello: OfferedPsks;
501 * ....
502 * };
503 * } PreSharedKeyExtension;
504 */
Jerry Yu6e74a7e2022-07-20 20:49:32 +0800505MBEDTLS_CHECK_RETURN_CRITICAL
Xiaokang Qian9f1747b2023-03-30 02:50:04 +0000506static int ssl_tls13_parse_pre_shared_key_ext(
507 mbedtls_ssl_context *ssl,
508 const unsigned char *pre_shared_key_ext,
509 const unsigned char *pre_shared_key_ext_end,
510 const unsigned char *ciphersuites,
511 const unsigned char *ciphersuites_end)
Jerry Yu1c105562022-07-10 06:32:38 +0000512{
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +0100513 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yu29d9faa2022-08-23 17:52:45 +0800514 const unsigned char *identities = pre_shared_key_ext;
Jerry Yu568ec252022-07-22 21:27:34 +0800515 const unsigned char *p_identity_len;
516 size_t identities_len;
Jerry Yu1c105562022-07-10 06:32:38 +0000517 const unsigned char *identities_end;
Jerry Yu568ec252022-07-22 21:27:34 +0800518 const unsigned char *binders;
519 const unsigned char *p_binder_len;
520 size_t binders_len;
Jerry Yu1c105562022-07-10 06:32:38 +0000521 const unsigned char *binders_end;
Jerry Yu96a2e362022-07-21 15:11:34 +0800522 int matched_identity = -1;
523 int identity_id = -1;
Jerry Yu1c105562022-07-10 06:32:38 +0000524
Gilles Peskine449bd832023-01-11 14:50:10 +0100525 MBEDTLS_SSL_DEBUG_BUF(3, "pre_shared_key extension",
526 pre_shared_key_ext,
527 pre_shared_key_ext_end - pre_shared_key_ext);
Jerry Yu1c105562022-07-10 06:32:38 +0000528
Jerry Yu96a2e362022-07-21 15:11:34 +0800529 /* identities_len 2 bytes
530 * identities_data >= 7 bytes
531 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100532 MBEDTLS_SSL_CHK_BUF_READ_PTR(identities, pre_shared_key_ext_end, 7 + 2);
533 identities_len = MBEDTLS_GET_UINT16_BE(identities, 0);
Jerry Yu568ec252022-07-22 21:27:34 +0800534 p_identity_len = identities + 2;
Gilles Peskine449bd832023-01-11 14:50:10 +0100535 MBEDTLS_SSL_CHK_BUF_READ_PTR(p_identity_len, pre_shared_key_ext_end,
536 identities_len);
Jerry Yu568ec252022-07-22 21:27:34 +0800537 identities_end = p_identity_len + identities_len;
Jerry Yu1c105562022-07-10 06:32:38 +0000538
Jerry Yu96a2e362022-07-21 15:11:34 +0800539 /* binders_len 2 bytes
540 * binders >= 33 bytes
541 */
Jerry Yu568ec252022-07-22 21:27:34 +0800542 binders = identities_end;
Gilles Peskine449bd832023-01-11 14:50:10 +0100543 MBEDTLS_SSL_CHK_BUF_READ_PTR(binders, pre_shared_key_ext_end, 33 + 2);
544 binders_len = MBEDTLS_GET_UINT16_BE(binders, 0);
Jerry Yu568ec252022-07-22 21:27:34 +0800545 p_binder_len = binders + 2;
Gilles Peskine449bd832023-01-11 14:50:10 +0100546 MBEDTLS_SSL_CHK_BUF_READ_PTR(p_binder_len, pre_shared_key_ext_end, binders_len);
Jerry Yu568ec252022-07-22 21:27:34 +0800547 binders_end = p_binder_len + binders_len;
Jerry Yu1c105562022-07-10 06:32:38 +0000548
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +0100549 ret = ssl->handshake->update_checksum(ssl, pre_shared_key_ext,
550 identities_end - pre_shared_key_ext);
551 if (0 != ret) {
552 MBEDTLS_SSL_DEBUG_RET(1, ("update_checksum"), ret);
553 return ret;
554 }
Jerry Yu96a2e362022-07-21 15:11:34 +0800555
Gilles Peskine449bd832023-01-11 14:50:10 +0100556 while (p_identity_len < identities_end && p_binder_len < binders_end) {
Jerry Yu1c105562022-07-10 06:32:38 +0000557 const unsigned char *identity;
Jerry Yu568ec252022-07-22 21:27:34 +0800558 size_t identity_len;
Jerry Yu82534862022-08-30 10:42:33 +0800559 uint32_t obfuscated_ticket_age;
Jerry Yu96a2e362022-07-21 15:11:34 +0800560 const unsigned char *binder;
Jerry Yu568ec252022-07-22 21:27:34 +0800561 size_t binder_len;
Jerry Yu5725f1c2022-08-21 17:27:16 +0800562 int psk_type;
563 uint16_t cipher_suite;
Jerry Yu29d9faa2022-08-23 17:52:45 +0800564 const mbedtls_ssl_ciphersuite_t *ciphersuite_info;
Jerry Yu82534862022-08-30 10:42:33 +0800565#if defined(MBEDTLS_SSL_SESSION_TICKETS)
566 mbedtls_ssl_session session;
Gilles Peskine449bd832023-01-11 14:50:10 +0100567 mbedtls_ssl_session_init(&session);
Jerry Yu82534862022-08-30 10:42:33 +0800568#endif
Jerry Yubb852022022-07-20 21:10:44 +0800569
Gilles Peskine449bd832023-01-11 14:50:10 +0100570 MBEDTLS_SSL_CHK_BUF_READ_PTR(p_identity_len, identities_end, 2 + 1 + 4);
571 identity_len = MBEDTLS_GET_UINT16_BE(p_identity_len, 0);
Jerry Yu568ec252022-07-22 21:27:34 +0800572 identity = p_identity_len + 2;
Gilles Peskine449bd832023-01-11 14:50:10 +0100573 MBEDTLS_SSL_CHK_BUF_READ_PTR(identity, identities_end, identity_len + 4);
574 obfuscated_ticket_age = MBEDTLS_GET_UINT32_BE(identity, identity_len);
Jerry Yu568ec252022-07-22 21:27:34 +0800575 p_identity_len += identity_len + 6;
Jerry Yu1c105562022-07-10 06:32:38 +0000576
Gilles Peskine449bd832023-01-11 14:50:10 +0100577 MBEDTLS_SSL_CHK_BUF_READ_PTR(p_binder_len, binders_end, 1 + 32);
Jerry Yu568ec252022-07-22 21:27:34 +0800578 binder_len = *p_binder_len;
579 binder = p_binder_len + 1;
Gilles Peskine449bd832023-01-11 14:50:10 +0100580 MBEDTLS_SSL_CHK_BUF_READ_PTR(binder, binders_end, binder_len);
Jerry Yu568ec252022-07-22 21:27:34 +0800581 p_binder_len += binder_len + 1;
Jerry Yu96a2e362022-07-21 15:11:34 +0800582
Jerry Yu96a2e362022-07-21 15:11:34 +0800583 identity_id++;
Gilles Peskine449bd832023-01-11 14:50:10 +0100584 if (matched_identity != -1) {
Jerry Yu1c105562022-07-10 06:32:38 +0000585 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +0100586 }
Jerry Yu1c105562022-07-10 06:32:38 +0000587
Jerry Yu96a2e362022-07-21 15:11:34 +0800588 ret = ssl_tls13_offered_psks_check_identity_match(
Gilles Peskine449bd832023-01-11 14:50:10 +0100589 ssl, identity, identity_len, obfuscated_ticket_age,
590 &psk_type, &session);
591 if (ret != SSL_TLS1_3_OFFERED_PSK_MATCH) {
Jerry Yu96a2e362022-07-21 15:11:34 +0800592 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +0100593 }
Jerry Yu96a2e362022-07-21 15:11:34 +0800594
Gilles Peskine449bd832023-01-11 14:50:10 +0100595 MBEDTLS_SSL_DEBUG_MSG(4, ("found matched identity"));
596 switch (psk_type) {
Jerry Yu0baf9072022-08-25 11:21:04 +0800597 case MBEDTLS_SSL_TLS1_3_PSK_EXTERNAL:
598 ret = ssl_tls13_select_ciphersuite_for_psk(
Gilles Peskine449bd832023-01-11 14:50:10 +0100599 ssl, ciphersuites, ciphersuites_end,
600 &cipher_suite, &ciphersuite_info);
Jerry Yu0baf9072022-08-25 11:21:04 +0800601 break;
602 case MBEDTLS_SSL_TLS1_3_PSK_RESUMPTION:
Jerry Yu82534862022-08-30 10:42:33 +0800603#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Jerry Yu0baf9072022-08-25 11:21:04 +0800604 ret = ssl_tls13_select_ciphersuite_for_resumption(
Gilles Peskine449bd832023-01-11 14:50:10 +0100605 ssl, ciphersuites, ciphersuites_end, &session,
606 &cipher_suite, &ciphersuite_info);
607 if (ret != 0) {
608 mbedtls_ssl_session_free(&session);
609 }
Jerry Yu82534862022-08-30 10:42:33 +0800610#else
611 ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
612#endif
Jerry Yu0baf9072022-08-25 11:21:04 +0800613 break;
614 default:
Gilles Peskine449bd832023-01-11 14:50:10 +0100615 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Jerry Yu0baf9072022-08-25 11:21:04 +0800616 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100617 if (ret != 0) {
Jerry Yuf35ba382022-08-23 17:58:26 +0800618 /* See below, no cipher_suite available, abort handshake */
619 MBEDTLS_SSL_PEND_FATAL_ALERT(
620 MBEDTLS_SSL_ALERT_MSG_DECRYPT_ERROR,
Gilles Peskine449bd832023-01-11 14:50:10 +0100621 MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE);
Jerry Yuf35ba382022-08-23 17:58:26 +0800622 MBEDTLS_SSL_DEBUG_RET(
Gilles Peskine449bd832023-01-11 14:50:10 +0100623 2, "ssl_tls13_select_ciphersuite", ret);
624 return ret;
Jerry Yu5725f1c2022-08-21 17:27:16 +0800625 }
626
Jerry Yu96a2e362022-07-21 15:11:34 +0800627 ret = ssl_tls13_offered_psks_check_binder_match(
Gilles Peskine449bd832023-01-11 14:50:10 +0100628 ssl, binder, binder_len, psk_type,
Dave Rodgman2eab4622023-10-05 13:30:37 +0100629 mbedtls_md_psa_alg_from_type((mbedtls_md_type_t) ciphersuite_info->mac));
Ronald Cron6e311272023-12-05 17:57:01 +0100630 if (ret != SSL_TLS1_3_BINDER_MATCH) {
Jerry Yuc5a23a02022-08-25 10:51:44 +0800631 /* For security reasons, the handshake should be aborted when we
632 * fail to validate a binder value. See RFC 8446 section 4.2.11.2
633 * and appendix E.6. */
Jerry Yu82534862022-08-30 10:42:33 +0800634#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Gilles Peskine449bd832023-01-11 14:50:10 +0100635 mbedtls_ssl_session_free(&session);
Jerry Yu82534862022-08-30 10:42:33 +0800636#endif
Gilles Peskine449bd832023-01-11 14:50:10 +0100637 MBEDTLS_SSL_DEBUG_MSG(3, ("Invalid binder."));
Xiaokang Qian9f1747b2023-03-30 02:50:04 +0000638 MBEDTLS_SSL_DEBUG_RET(
639 1, "ssl_tls13_offered_psks_check_binder_match", ret);
Jerry Yu96a2e362022-07-21 15:11:34 +0800640 MBEDTLS_SSL_PEND_FATAL_ALERT(
641 MBEDTLS_SSL_ALERT_MSG_DECRYPT_ERROR,
Gilles Peskine449bd832023-01-11 14:50:10 +0100642 MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE);
643 return ret;
Jerry Yu96a2e362022-07-21 15:11:34 +0800644 }
Jerry Yu96a2e362022-07-21 15:11:34 +0800645
646 matched_identity = identity_id;
Jerry Yu5725f1c2022-08-21 17:27:16 +0800647
648 /* Update handshake parameters */
Jerry Yue5834fd2022-08-29 20:16:09 +0800649 ssl->handshake->ciphersuite_info = ciphersuite_info;
Jerry Yu4746b102022-09-13 11:11:48 +0800650 ssl->session_negotiate->ciphersuite = cipher_suite;
Gilles Peskine449bd832023-01-11 14:50:10 +0100651 MBEDTLS_SSL_DEBUG_MSG(2, ("overwrite ciphersuite: %04x - %s",
652 cipher_suite, ciphersuite_info->name));
Jerry Yu82534862022-08-30 10:42:33 +0800653#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Gilles Peskine449bd832023-01-11 14:50:10 +0100654 if (psk_type == MBEDTLS_SSL_TLS1_3_PSK_RESUMPTION) {
655 ret = ssl_tls13_session_copy_ticket(ssl->session_negotiate,
656 &session);
657 mbedtls_ssl_session_free(&session);
658 if (ret != 0) {
659 return ret;
660 }
Jerry Yu82534862022-08-30 10:42:33 +0800661 }
662#endif /* MBEDTLS_SSL_SESSION_TICKETS */
Jerry Yu1c105562022-07-10 06:32:38 +0000663 }
664
Gilles Peskine449bd832023-01-11 14:50:10 +0100665 if (p_identity_len != identities_end || p_binder_len != binders_end) {
666 MBEDTLS_SSL_DEBUG_MSG(3, ("pre_shared_key extension decode error"));
667 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR,
668 MBEDTLS_ERR_SSL_DECODE_ERROR);
669 return MBEDTLS_ERR_SSL_DECODE_ERROR;
Jerry Yu1c105562022-07-10 06:32:38 +0000670 }
671
Jerry Yu6f1db3f2022-07-22 23:05:59 +0800672 /* Update the handshake transcript with the binder list. */
Xiaokang Qian9f1747b2023-03-30 02:50:04 +0000673 ret = ssl->handshake->update_checksum(
674 ssl, identities_end, (size_t) (binders_end - identities_end));
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +0100675 if (0 != ret) {
676 MBEDTLS_SSL_DEBUG_RET(1, ("update_checksum"), ret);
677 return ret;
678 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100679 if (matched_identity == -1) {
680 MBEDTLS_SSL_DEBUG_MSG(3, ("No matched PSK or ticket."));
681 return MBEDTLS_ERR_SSL_UNKNOWN_IDENTITY;
Jerry Yu1c105562022-07-10 06:32:38 +0000682 }
683
Gilles Peskine449bd832023-01-11 14:50:10 +0100684 ssl->handshake->selected_identity = (uint16_t) matched_identity;
685 MBEDTLS_SSL_DEBUG_MSG(3, ("Pre shared key found"));
Jerry Yu1c105562022-07-10 06:32:38 +0000686
Gilles Peskine449bd832023-01-11 14:50:10 +0100687 return 0;
Jerry Yu1c105562022-07-10 06:32:38 +0000688}
Jerry Yu032b15ce2022-07-11 06:10:03 +0000689
690/*
691 * struct {
692 * select ( Handshake.msg_type ) {
693 * ....
694 * case server_hello:
695 * uint16 selected_identity;
696 * }
697 * } PreSharedKeyExtension;
698 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100699static int ssl_tls13_write_server_pre_shared_key_ext(mbedtls_ssl_context *ssl,
700 unsigned char *buf,
701 unsigned char *end,
702 size_t *olen)
Jerry Yu032b15ce2022-07-11 06:10:03 +0000703{
Gilles Peskine449bd832023-01-11 14:50:10 +0100704 unsigned char *p = (unsigned char *) buf;
Jerry Yu032b15ce2022-07-11 06:10:03 +0000705
706 *olen = 0;
707
David Horstmann21b89762022-10-06 18:34:28 +0100708 int not_using_psk = 0;
Jerry Yu032b15ce2022-07-11 06:10:03 +0000709#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +0100710 not_using_psk = (mbedtls_svc_key_id_is_null(ssl->handshake->psk_opaque));
Jerry Yu032b15ce2022-07-11 06:10:03 +0000711#else
Gilles Peskine449bd832023-01-11 14:50:10 +0100712 not_using_psk = (ssl->handshake->psk == NULL);
Jerry Yu032b15ce2022-07-11 06:10:03 +0000713#endif
Gilles Peskine449bd832023-01-11 14:50:10 +0100714 if (not_using_psk) {
Jerry Yu032b15ce2022-07-11 06:10:03 +0000715 /* We shouldn't have called this extension writer unless we've
716 * chosen to use a PSK. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100717 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Jerry Yu032b15ce2022-07-11 06:10:03 +0000718 }
719
Gilles Peskine449bd832023-01-11 14:50:10 +0100720 MBEDTLS_SSL_DEBUG_MSG(3, ("server hello, adding pre_shared_key extension"));
721 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 6);
Jerry Yu032b15ce2022-07-11 06:10:03 +0000722
Gilles Peskine449bd832023-01-11 14:50:10 +0100723 MBEDTLS_PUT_UINT16_BE(MBEDTLS_TLS_EXT_PRE_SHARED_KEY, p, 0);
724 MBEDTLS_PUT_UINT16_BE(2, p, 2);
Jerry Yu032b15ce2022-07-11 06:10:03 +0000725
Gilles Peskine449bd832023-01-11 14:50:10 +0100726 MBEDTLS_PUT_UINT16_BE(ssl->handshake->selected_identity, p, 4);
Jerry Yu032b15ce2022-07-11 06:10:03 +0000727
728 *olen = 6;
729
Gilles Peskine449bd832023-01-11 14:50:10 +0100730 MBEDTLS_SSL_DEBUG_MSG(4, ("sent selected_identity: %u",
731 ssl->handshake->selected_identity));
Jerry Yu032b15ce2022-07-11 06:10:03 +0000732
Gilles Peskine449bd832023-01-11 14:50:10 +0100733 mbedtls_ssl_tls13_set_hs_sent_ext_mask(ssl, MBEDTLS_TLS_EXT_PRE_SHARED_KEY);
Jerry Yub95dd362022-11-08 21:19:34 +0800734
Gilles Peskine449bd832023-01-11 14:50:10 +0100735 return 0;
Jerry Yu032b15ce2022-07-11 06:10:03 +0000736}
737
Ronald Cron41a443a2022-10-04 16:38:25 +0200738#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED */
Jerry Yue19e3b92022-07-08 12:04:51 +0000739
XiaokangQian7807f9f2022-02-15 10:04:37 +0000740/* From RFC 8446:
741 * struct {
XiaokangQiancfd925f2022-04-14 07:10:37 +0000742 * ProtocolVersion versions<2..254>;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000743 * } SupportedVersions;
744 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200745MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100746static int ssl_tls13_parse_supported_versions_ext(mbedtls_ssl_context *ssl,
747 const unsigned char *buf,
748 const unsigned char *end)
XiaokangQian7807f9f2022-02-15 10:04:37 +0000749{
XiaokangQian7807f9f2022-02-15 10:04:37 +0000750 const unsigned char *p = buf;
XiaokangQian8f9dfe42022-04-15 02:52:39 +0000751 size_t versions_len;
XiaokangQian4080a7f2022-04-11 09:55:18 +0000752 const unsigned char *versions_end;
XiaokangQian0a1b54e2022-04-21 03:01:38 +0000753 uint16_t tls_version;
Ronald Cron5af4c7f2023-03-07 20:46:59 +0100754 int found_supported_version = 0;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000755
Gilles Peskine449bd832023-01-11 14:50:10 +0100756 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, 1);
XiaokangQian4080a7f2022-04-11 09:55:18 +0000757 versions_len = p[0];
XiaokangQian7807f9f2022-02-15 10:04:37 +0000758 p += 1;
759
Gilles Peskine449bd832023-01-11 14:50:10 +0100760 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, versions_len);
XiaokangQian4080a7f2022-04-11 09:55:18 +0000761 versions_end = p + versions_len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100762 while (p < versions_end) {
763 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, versions_end, 2);
764 tls_version = mbedtls_ssl_read_version(p, ssl->conf->transport);
XiaokangQianb67384d2022-04-19 00:02:38 +0000765 p += 2;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000766
Ronald Cron3bd2b022023-04-03 16:45:39 +0200767 if (MBEDTLS_SSL_VERSION_TLS1_3 == tls_version) {
Ronald Cron5af4c7f2023-03-07 20:46:59 +0100768 found_supported_version = 1;
769 break;
770 }
771
Ronald Cron3bd2b022023-04-03 16:45:39 +0200772 if ((MBEDTLS_SSL_VERSION_TLS1_2 == tls_version) &&
773 mbedtls_ssl_conf_is_tls12_enabled(ssl->conf)) {
Ronald Cron5af4c7f2023-03-07 20:46:59 +0100774 found_supported_version = 1;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000775 break;
776 }
XiaokangQian7807f9f2022-02-15 10:04:37 +0000777 }
778
Ronald Cron5af4c7f2023-03-07 20:46:59 +0100779 if (!found_supported_version) {
780 MBEDTLS_SSL_DEBUG_MSG(1, ("No supported version found."));
XiaokangQian7807f9f2022-02-15 10:04:37 +0000781
Gilles Peskine449bd832023-01-11 14:50:10 +0100782 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_PROTOCOL_VERSION,
783 MBEDTLS_ERR_SSL_BAD_PROTOCOL_VERSION);
784 return MBEDTLS_ERR_SSL_BAD_PROTOCOL_VERSION;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000785 }
786
Ronald Cron5af4c7f2023-03-07 20:46:59 +0100787 MBEDTLS_SSL_DEBUG_MSG(1, ("Negotiated version: [%04x]",
Gilles Peskine449bd832023-01-11 14:50:10 +0100788 (unsigned int) tls_version));
XiaokangQian7807f9f2022-02-15 10:04:37 +0000789
Ronald Cron5af4c7f2023-03-07 20:46:59 +0100790 return (int) tls_version;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000791}
792
Przemek Stekiel8c0a9532023-06-15 16:48:19 +0200793#if defined(PSA_WANT_ALG_ECDH) || defined(PSA_WANT_ALG_FFDH)
XiaokangQiane8ff3502022-04-22 02:34:40 +0000794/*
XiaokangQian7807f9f2022-02-15 10:04:37 +0000795 *
796 * From RFC 8446:
797 * enum {
798 * ... (0xFFFF)
799 * } NamedGroup;
800 * struct {
801 * NamedGroup named_group_list<2..2^16-1>;
802 * } NamedGroupList;
803 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200804MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100805static int ssl_tls13_parse_supported_groups_ext(mbedtls_ssl_context *ssl,
806 const unsigned char *buf,
807 const unsigned char *end)
XiaokangQian7807f9f2022-02-15 10:04:37 +0000808{
XiaokangQian7807f9f2022-02-15 10:04:37 +0000809 const unsigned char *p = buf;
XiaokangQian84823772022-04-19 07:57:30 +0000810 size_t named_group_list_len;
XiaokangQian8f9dfe42022-04-15 02:52:39 +0000811 const unsigned char *named_group_list_end;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000812
Gilles Peskine449bd832023-01-11 14:50:10 +0100813 MBEDTLS_SSL_DEBUG_BUF(3, "supported_groups extension", p, end - buf);
814 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, 2);
815 named_group_list_len = MBEDTLS_GET_UINT16_BE(p, 0);
XiaokangQian7807f9f2022-02-15 10:04:37 +0000816 p += 2;
Gilles Peskine449bd832023-01-11 14:50:10 +0100817 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, named_group_list_len);
XiaokangQian8f9dfe42022-04-15 02:52:39 +0000818 named_group_list_end = p + named_group_list_len;
XiaokangQian4e8cd7b2022-04-21 09:48:09 +0000819 ssl->handshake->hrr_selected_group = 0;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000820
Gilles Peskine449bd832023-01-11 14:50:10 +0100821 while (p < named_group_list_end) {
XiaokangQian08037552022-04-20 07:16:41 +0000822 uint16_t named_group;
Gilles Peskine449bd832023-01-11 14:50:10 +0100823 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, named_group_list_end, 2);
824 named_group = MBEDTLS_GET_UINT16_BE(p, 0);
XiaokangQian08037552022-04-20 07:16:41 +0000825 p += 2;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000826
Gilles Peskine449bd832023-01-11 14:50:10 +0100827 MBEDTLS_SSL_DEBUG_MSG(2,
828 ("got named group: %s(%04x)",
829 mbedtls_ssl_named_group_to_str(named_group),
830 named_group));
XiaokangQian08037552022-04-20 07:16:41 +0000831
Gilles Peskine449bd832023-01-11 14:50:10 +0100832 if (!mbedtls_ssl_named_group_is_offered(ssl, named_group) ||
833 !mbedtls_ssl_named_group_is_supported(named_group) ||
834 ssl->handshake->hrr_selected_group != 0) {
XiaokangQian08037552022-04-20 07:16:41 +0000835 continue;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000836 }
837
Gilles Peskine449bd832023-01-11 14:50:10 +0100838 MBEDTLS_SSL_DEBUG_MSG(2,
839 ("add named group %s(%04x) into received list.",
840 mbedtls_ssl_named_group_to_str(named_group),
841 named_group));
Jerry Yuc1be19f2022-04-23 16:11:39 +0800842
XiaokangQian4e8cd7b2022-04-21 09:48:09 +0000843 ssl->handshake->hrr_selected_group = named_group;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000844 }
845
Gilles Peskine449bd832023-01-11 14:50:10 +0100846 return 0;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000847
848}
Przemek Stekiel8c0a9532023-06-15 16:48:19 +0200849#endif /* PSA_WANT_ALG_ECDH || PSA_WANT_ALG_FFDH */
XiaokangQian7807f9f2022-02-15 10:04:37 +0000850
XiaokangQian08037552022-04-20 07:16:41 +0000851#define SSL_TLS1_3_PARSE_KEY_SHARES_EXT_NO_MATCH 1
852
Valerio Settic9ae8622023-07-25 11:23:50 +0200853#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_EPHEMERAL_ENABLED)
XiaokangQian7807f9f2022-02-15 10:04:37 +0000854/*
855 * ssl_tls13_parse_key_shares_ext() verifies whether the information in the
Xiaokang Qian9f1747b2023-03-30 02:50:04 +0000856 * extension is correct and stores the first acceptable key share and its
857 * associated group.
XiaokangQian7807f9f2022-02-15 10:04:37 +0000858 *
859 * Possible return values are:
860 * - 0: Successful processing of the client provided key share extension.
Xiaokang Qian9f1747b2023-03-30 02:50:04 +0000861 * - SSL_TLS1_3_PARSE_KEY_SHARES_EXT_NO_MATCH: The key shares provided by
862 * the client does not match a group supported by the server. A
863 * HelloRetryRequest will be needed.
XiaokangQiane8ff3502022-04-22 02:34:40 +0000864 * - A negative value for fatal errors.
Jerry Yuc1be19f2022-04-23 16:11:39 +0800865 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200866MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100867static int ssl_tls13_parse_key_shares_ext(mbedtls_ssl_context *ssl,
868 const unsigned char *buf,
869 const unsigned char *end)
XiaokangQian7807f9f2022-02-15 10:04:37 +0000870{
XiaokangQianb67384d2022-04-19 00:02:38 +0000871 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000872 unsigned char const *p = buf;
XiaokangQian8f9dfe42022-04-15 02:52:39 +0000873 unsigned char const *client_shares_end;
Jerry Yuc1be19f2022-04-23 16:11:39 +0800874 size_t client_shares_len;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000875
876 /* From RFC 8446:
877 *
878 * struct {
879 * KeyShareEntry client_shares<0..2^16-1>;
880 * } KeyShareClientHello;
881 *
882 */
883
Gilles Peskine449bd832023-01-11 14:50:10 +0100884 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, 2);
885 client_shares_len = MBEDTLS_GET_UINT16_BE(p, 0);
XiaokangQian7807f9f2022-02-15 10:04:37 +0000886 p += 2;
Gilles Peskine449bd832023-01-11 14:50:10 +0100887 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, client_shares_len);
XiaokangQian7807f9f2022-02-15 10:04:37 +0000888
889 ssl->handshake->offered_group_id = 0;
XiaokangQian8f9dfe42022-04-15 02:52:39 +0000890 client_shares_end = p + client_shares_len;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000891
892 /* We try to find a suitable key share entry and copy it to the
893 * handshake context. Later, we have to find out whether we can do
894 * something with the provided key share or whether we have to
XiaokangQianc5763b52022-04-02 03:34:37 +0000895 * dismiss it and send a HelloRetryRequest message.
896 */
XiaokangQian7807f9f2022-02-15 10:04:37 +0000897
Gilles Peskine449bd832023-01-11 14:50:10 +0100898 while (p < client_shares_end) {
XiaokangQian9b5d04b2022-04-10 10:20:43 +0000899 uint16_t group;
Jerry Yuc1be19f2022-04-23 16:11:39 +0800900 size_t key_exchange_len;
Jerry Yu086edc22022-05-05 10:50:38 +0800901 const unsigned char *key_exchange;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000902
903 /*
904 * struct {
905 * NamedGroup group;
906 * opaque key_exchange<1..2^16-1>;
907 * } KeyShareEntry;
908 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100909 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, client_shares_end, 4);
910 group = MBEDTLS_GET_UINT16_BE(p, 0);
911 key_exchange_len = MBEDTLS_GET_UINT16_BE(p, 2);
Jerry Yuc1be19f2022-04-23 16:11:39 +0800912 p += 4;
Jerry Yu086edc22022-05-05 10:50:38 +0800913 key_exchange = p;
Gilles Peskine449bd832023-01-11 14:50:10 +0100914 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, client_shares_end, key_exchange_len);
Jerry Yu086edc22022-05-05 10:50:38 +0800915 p += key_exchange_len;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000916
917 /* Continue parsing even if we have already found a match,
XiaokangQianc5763b52022-04-02 03:34:37 +0000918 * for input validation purposes.
919 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100920 if (!mbedtls_ssl_named_group_is_offered(ssl, group) ||
921 !mbedtls_ssl_named_group_is_supported(group) ||
922 ssl->handshake->offered_group_id != 0) {
XiaokangQian060d8672022-04-21 09:24:56 +0000923 continue;
924 }
XiaokangQian060d8672022-04-21 09:24:56 +0000925
XiaokangQian7807f9f2022-02-15 10:04:37 +0000926 /*
Przemek Stekielc89f3ea2023-05-18 15:45:53 +0200927 * ECDHE and FFDHE groups are supported
XiaokangQian7807f9f2022-02-15 10:04:37 +0000928 */
Przemek Stekielc89f3ea2023-05-18 15:45:53 +0200929 if (mbedtls_ssl_tls13_named_group_is_ecdhe(group) ||
Przemek Stekield5f79e72023-06-29 09:08:43 +0200930 mbedtls_ssl_tls13_named_group_is_ffdh(group)) {
Przemek Stekielc89f3ea2023-05-18 15:45:53 +0200931 MBEDTLS_SSL_DEBUG_MSG(2, ("ECDH/FFDH group: %s (%04x)",
Gilles Peskine449bd832023-01-11 14:50:10 +0100932 mbedtls_ssl_named_group_to_str(group),
933 group));
Przemek Stekiel7ac93be2023-07-04 10:02:38 +0200934 ret = mbedtls_ssl_tls13_read_public_xxdhe_share(
Gilles Peskine449bd832023-01-11 14:50:10 +0100935 ssl, key_exchange - 2, key_exchange_len + 2);
936 if (ret != 0) {
937 return ret;
938 }
XiaokangQian4e8cd7b2022-04-21 09:48:09 +0000939
Gilles Peskine449bd832023-01-11 14:50:10 +0100940 } else {
941 MBEDTLS_SSL_DEBUG_MSG(4, ("Unrecognized NamedGroup %u",
942 (unsigned) group));
XiaokangQian7807f9f2022-02-15 10:04:37 +0000943 continue;
944 }
945
XiaokangQian9b5d04b2022-04-10 10:20:43 +0000946 ssl->handshake->offered_group_id = group;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000947 }
948
Jerry Yuc1be19f2022-04-23 16:11:39 +0800949
Gilles Peskine449bd832023-01-11 14:50:10 +0100950 if (ssl->handshake->offered_group_id == 0) {
951 MBEDTLS_SSL_DEBUG_MSG(1, ("no matching key share"));
952 return SSL_TLS1_3_PARSE_KEY_SHARES_EXT_NO_MATCH;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000953 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100954 return 0;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000955}
Valerio Settic9ae8622023-07-25 11:23:50 +0200956#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_EPHEMERAL_ENABLED */
XiaokangQian7807f9f2022-02-15 10:04:37 +0000957
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200958MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100959static int ssl_tls13_client_hello_has_exts(mbedtls_ssl_context *ssl,
960 int exts_mask)
XiaokangQian7807f9f2022-02-15 10:04:37 +0000961{
Jerry Yu0c354a22022-08-29 15:25:36 +0800962 int masked = ssl->handshake->received_extensions & exts_mask;
Gilles Peskine449bd832023-01-11 14:50:10 +0100963 return masked == exts_mask;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000964}
965
Jerry Yue5991322022-11-07 14:03:44 +0800966#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200967MBEDTLS_CHECK_RETURN_CRITICAL
XiaokangQianb67384d2022-04-19 00:02:38 +0000968static int ssl_tls13_client_hello_has_exts_for_ephemeral_key_exchange(
Gilles Peskine449bd832023-01-11 14:50:10 +0100969 mbedtls_ssl_context *ssl)
XiaokangQian7807f9f2022-02-15 10:04:37 +0000970{
Gilles Peskine449bd832023-01-11 14:50:10 +0100971 return ssl_tls13_client_hello_has_exts(
972 ssl,
973 MBEDTLS_SSL_EXT_MASK(SUPPORTED_GROUPS) |
974 MBEDTLS_SSL_EXT_MASK(KEY_SHARE) |
975 MBEDTLS_SSL_EXT_MASK(SIG_ALG));
XiaokangQian7807f9f2022-02-15 10:04:37 +0000976}
Jerry Yue5991322022-11-07 14:03:44 +0800977#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED */
XiaokangQian7807f9f2022-02-15 10:04:37 +0000978
Jerry Yue5991322022-11-07 14:03:44 +0800979#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK_ENABLED)
Jerry Yu77f01482022-07-11 07:03:24 +0000980MBEDTLS_CHECK_RETURN_CRITICAL
981static int ssl_tls13_client_hello_has_exts_for_psk_key_exchange(
Gilles Peskine449bd832023-01-11 14:50:10 +0100982 mbedtls_ssl_context *ssl)
Jerry Yu77f01482022-07-11 07:03:24 +0000983{
Gilles Peskine449bd832023-01-11 14:50:10 +0100984 return ssl_tls13_client_hello_has_exts(
985 ssl,
986 MBEDTLS_SSL_EXT_MASK(PRE_SHARED_KEY) |
987 MBEDTLS_SSL_EXT_MASK(PSK_KEY_EXCHANGE_MODES));
Jerry Yu77f01482022-07-11 07:03:24 +0000988}
Jerry Yue5991322022-11-07 14:03:44 +0800989#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK_ENABLED */
Jerry Yu77f01482022-07-11 07:03:24 +0000990
Jerry Yue5991322022-11-07 14:03:44 +0800991#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK_EPHEMERAL_ENABLED)
Jerry Yu77f01482022-07-11 07:03:24 +0000992MBEDTLS_CHECK_RETURN_CRITICAL
993static int ssl_tls13_client_hello_has_exts_for_psk_ephemeral_key_exchange(
Gilles Peskine449bd832023-01-11 14:50:10 +0100994 mbedtls_ssl_context *ssl)
Jerry Yu77f01482022-07-11 07:03:24 +0000995{
Gilles Peskine449bd832023-01-11 14:50:10 +0100996 return ssl_tls13_client_hello_has_exts(
997 ssl,
998 MBEDTLS_SSL_EXT_MASK(SUPPORTED_GROUPS) |
999 MBEDTLS_SSL_EXT_MASK(KEY_SHARE) |
1000 MBEDTLS_SSL_EXT_MASK(PRE_SHARED_KEY) |
1001 MBEDTLS_SSL_EXT_MASK(PSK_KEY_EXCHANGE_MODES));
Jerry Yu77f01482022-07-11 07:03:24 +00001002}
Jerry Yue5991322022-11-07 14:03:44 +08001003#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK_EPHEMERAL_ENABLED */
Jerry Yu77f01482022-07-11 07:03:24 +00001004
Pengyu Lv306a01d2023-02-02 16:35:47 +08001005#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED)
1006MBEDTLS_CHECK_RETURN_CRITICAL
Pengyu Lvd72e8582023-11-10 10:37:18 +08001007static int ssl_tls13_ticket_is_kex_mode_permitted(mbedtls_ssl_context *ssl,
1008 unsigned int kex_mode)
Pengyu Lv306a01d2023-02-02 16:35:47 +08001009{
1010#if defined(MBEDTLS_SSL_SESSION_TICKETS)
1011 if (ssl->handshake->resume) {
Pengyu Lv94a42cc2023-12-06 10:04:17 +08001012 if (!mbedtls_ssl_tls13_session_ticket_has_flags(
Pengyu Lv306a01d2023-02-02 16:35:47 +08001013 ssl->session_negotiate, kex_mode)) {
1014 return 0;
1015 }
1016 }
1017#else
1018 ((void) ssl);
1019 ((void) kex_mode);
1020#endif
1021 return 1;
1022}
1023#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED */
1024
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001025MBEDTLS_CHECK_RETURN_CRITICAL
Pengyu Lvbc4aab72023-12-01 15:37:24 +08001026static int ssl_tls13_key_exchange_is_ephemeral_available(mbedtls_ssl_context *ssl)
XiaokangQian7807f9f2022-02-15 10:04:37 +00001027{
Jerry Yue5991322022-11-07 14:03:44 +08001028#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED)
Pengyu Lv0a1ff2b2023-11-14 11:03:32 +08001029 return mbedtls_ssl_conf_tls13_is_ephemeral_enabled(ssl) &&
Gilles Peskine449bd832023-01-11 14:50:10 +01001030 ssl_tls13_client_hello_has_exts_for_ephemeral_key_exchange(ssl);
Jerry Yue5991322022-11-07 14:03:44 +08001031#else
1032 ((void) ssl);
Gilles Peskine449bd832023-01-11 14:50:10 +01001033 return 0;
Jerry Yue5991322022-11-07 14:03:44 +08001034#endif
Jerry Yu77f01482022-07-11 07:03:24 +00001035}
XiaokangQian7807f9f2022-02-15 10:04:37 +00001036
Jerry Yu77f01482022-07-11 07:03:24 +00001037MBEDTLS_CHECK_RETURN_CRITICAL
Pengyu Lvbc4aab72023-12-01 15:37:24 +08001038static int ssl_tls13_key_exchange_is_psk_available(mbedtls_ssl_context *ssl)
Jerry Yu77f01482022-07-11 07:03:24 +00001039{
Jerry Yue5991322022-11-07 14:03:44 +08001040#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK_ENABLED)
Pengyu Lvd72e8582023-11-10 10:37:18 +08001041 return ssl_tls13_ticket_is_kex_mode_permitted(
Pengyu Lv306a01d2023-02-02 16:35:47 +08001042 ssl, MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK) &&
Pengyu Lv0a1ff2b2023-11-14 11:03:32 +08001043 mbedtls_ssl_conf_tls13_is_psk_enabled(ssl) &&
Pengyu Lvb2cfafb2023-11-14 13:56:13 +08001044 mbedtls_ssl_tls13_is_psk_supported(ssl) &&
Gilles Peskine449bd832023-01-11 14:50:10 +01001045 ssl_tls13_client_hello_has_exts_for_psk_key_exchange(ssl);
Jerry Yu77f01482022-07-11 07:03:24 +00001046#else
1047 ((void) ssl);
Gilles Peskine449bd832023-01-11 14:50:10 +01001048 return 0;
Jerry Yu77f01482022-07-11 07:03:24 +00001049#endif
1050}
XiaokangQian7807f9f2022-02-15 10:04:37 +00001051
Jerry Yu77f01482022-07-11 07:03:24 +00001052MBEDTLS_CHECK_RETURN_CRITICAL
Pengyu Lvbc4aab72023-12-01 15:37:24 +08001053static int ssl_tls13_key_exchange_is_psk_ephemeral_available(mbedtls_ssl_context *ssl)
Jerry Yu77f01482022-07-11 07:03:24 +00001054{
Jerry Yue5991322022-11-07 14:03:44 +08001055#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK_EPHEMERAL_ENABLED)
Pengyu Lvd72e8582023-11-10 10:37:18 +08001056 return ssl_tls13_ticket_is_kex_mode_permitted(
Pengyu Lv306a01d2023-02-02 16:35:47 +08001057 ssl, MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK_EPHEMERAL) &&
Pengyu Lv0a1ff2b2023-11-14 11:03:32 +08001058 mbedtls_ssl_conf_tls13_is_psk_ephemeral_enabled(ssl) &&
Pengyu Lvb2cfafb2023-11-14 13:56:13 +08001059 mbedtls_ssl_tls13_is_psk_ephemeral_supported(ssl) &&
Gilles Peskine449bd832023-01-11 14:50:10 +01001060 ssl_tls13_client_hello_has_exts_for_psk_ephemeral_key_exchange(ssl);
Jerry Yu77f01482022-07-11 07:03:24 +00001061#else
1062 ((void) ssl);
Gilles Peskine449bd832023-01-11 14:50:10 +01001063 return 0;
Jerry Yu77f01482022-07-11 07:03:24 +00001064#endif
1065}
1066
Gilles Peskine449bd832023-01-11 14:50:10 +01001067static int ssl_tls13_determine_key_exchange_mode(mbedtls_ssl_context *ssl)
Jerry Yu77f01482022-07-11 07:03:24 +00001068{
1069 /*
1070 * Determine the key exchange algorithm to use.
1071 * There are three types of key exchanges supported in TLS 1.3:
1072 * - (EC)DH with ECDSA,
1073 * - (EC)DH with PSK,
1074 * - plain PSK.
1075 *
1076 * The PSK-based key exchanges may additionally be used with 0-RTT.
1077 *
1078 * Our built-in order of preference is
Jerry Yuce6ed702022-07-22 21:49:53 +08001079 * 1 ) (EC)DHE-PSK Mode ( psk_ephemeral )
1080 * 2 ) Certificate Mode ( ephemeral )
1081 * 3 ) Plain PSK Mode ( psk )
Jerry Yu77f01482022-07-11 07:03:24 +00001082 */
1083
Xiaokang Qian9f1747b2023-03-30 02:50:04 +00001084 ssl->handshake->key_exchange_mode =
1085 MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_NONE;
Jerry Yu77f01482022-07-11 07:03:24 +00001086
Pengyu Lvbc4aab72023-12-01 15:37:24 +08001087 if (ssl_tls13_key_exchange_is_psk_ephemeral_available(ssl)) {
Jerry Yu77f01482022-07-11 07:03:24 +00001088 ssl->handshake->key_exchange_mode =
1089 MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK_EPHEMERAL;
Gilles Peskine449bd832023-01-11 14:50:10 +01001090 MBEDTLS_SSL_DEBUG_MSG(2, ("key exchange mode: psk_ephemeral"));
1091 } else
Pengyu Lvbc4aab72023-12-01 15:37:24 +08001092 if (ssl_tls13_key_exchange_is_ephemeral_available(ssl)) {
Jerry Yu77f01482022-07-11 07:03:24 +00001093 ssl->handshake->key_exchange_mode =
1094 MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL;
Gilles Peskine449bd832023-01-11 14:50:10 +01001095 MBEDTLS_SSL_DEBUG_MSG(2, ("key exchange mode: ephemeral"));
1096 } else
Pengyu Lvbc4aab72023-12-01 15:37:24 +08001097 if (ssl_tls13_key_exchange_is_psk_available(ssl)) {
Jerry Yuce6ed702022-07-22 21:49:53 +08001098 ssl->handshake->key_exchange_mode =
1099 MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK;
Gilles Peskine449bd832023-01-11 14:50:10 +01001100 MBEDTLS_SSL_DEBUG_MSG(2, ("key exchange mode: psk"));
1101 } else {
Jerry Yu77f01482022-07-11 07:03:24 +00001102 MBEDTLS_SSL_DEBUG_MSG(
Gilles Peskine449bd832023-01-11 14:50:10 +01001103 1,
1104 ("ClientHello message misses mandatory extensions."));
1105 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_MISSING_EXTENSION,
1106 MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER);
1107 return MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER;
Jerry Yu77f01482022-07-11 07:03:24 +00001108 }
1109
Gilles Peskine449bd832023-01-11 14:50:10 +01001110 return 0;
Jerry Yu77f01482022-07-11 07:03:24 +00001111
XiaokangQian7807f9f2022-02-15 10:04:37 +00001112}
1113
XiaokangQian81802f42022-06-10 13:25:22 +00001114#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
Ronald Cron928cbd32022-10-04 16:14:26 +02001115 defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED)
Ronald Cron67ea2542022-09-15 17:34:42 +02001116
1117#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01001118static psa_algorithm_t ssl_tls13_iana_sig_alg_to_psa_alg(uint16_t sig_alg)
Ronald Cron67ea2542022-09-15 17:34:42 +02001119{
Gilles Peskine449bd832023-01-11 14:50:10 +01001120 switch (sig_alg) {
Ronald Cron67ea2542022-09-15 17:34:42 +02001121 case MBEDTLS_TLS1_3_SIG_ECDSA_SECP256R1_SHA256:
Gilles Peskine449bd832023-01-11 14:50:10 +01001122 return PSA_ALG_ECDSA(PSA_ALG_SHA_256);
Ronald Cron67ea2542022-09-15 17:34:42 +02001123 case MBEDTLS_TLS1_3_SIG_ECDSA_SECP384R1_SHA384:
Gilles Peskine449bd832023-01-11 14:50:10 +01001124 return PSA_ALG_ECDSA(PSA_ALG_SHA_384);
Ronald Cron67ea2542022-09-15 17:34:42 +02001125 case MBEDTLS_TLS1_3_SIG_ECDSA_SECP521R1_SHA512:
Gilles Peskine449bd832023-01-11 14:50:10 +01001126 return PSA_ALG_ECDSA(PSA_ALG_SHA_512);
Ronald Cron67ea2542022-09-15 17:34:42 +02001127 case MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA256:
Gilles Peskine449bd832023-01-11 14:50:10 +01001128 return PSA_ALG_RSA_PSS(PSA_ALG_SHA_256);
Ronald Cron67ea2542022-09-15 17:34:42 +02001129 case MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA384:
Gilles Peskine449bd832023-01-11 14:50:10 +01001130 return PSA_ALG_RSA_PSS(PSA_ALG_SHA_384);
Ronald Cron67ea2542022-09-15 17:34:42 +02001131 case MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA512:
Gilles Peskine449bd832023-01-11 14:50:10 +01001132 return PSA_ALG_RSA_PSS(PSA_ALG_SHA_512);
Ronald Cron67ea2542022-09-15 17:34:42 +02001133 case MBEDTLS_TLS1_3_SIG_RSA_PKCS1_SHA256:
Gilles Peskine449bd832023-01-11 14:50:10 +01001134 return PSA_ALG_RSA_PKCS1V15_SIGN(PSA_ALG_SHA_256);
Ronald Cron67ea2542022-09-15 17:34:42 +02001135 case MBEDTLS_TLS1_3_SIG_RSA_PKCS1_SHA384:
Gilles Peskine449bd832023-01-11 14:50:10 +01001136 return PSA_ALG_RSA_PKCS1V15_SIGN(PSA_ALG_SHA_384);
Ronald Cron67ea2542022-09-15 17:34:42 +02001137 case MBEDTLS_TLS1_3_SIG_RSA_PKCS1_SHA512:
Gilles Peskine449bd832023-01-11 14:50:10 +01001138 return PSA_ALG_RSA_PKCS1V15_SIGN(PSA_ALG_SHA_512);
Ronald Cron67ea2542022-09-15 17:34:42 +02001139 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01001140 return PSA_ALG_NONE;
Ronald Cron67ea2542022-09-15 17:34:42 +02001141 }
1142}
1143#endif /* MBEDTLS_USE_PSA_CRYPTO */
1144
XiaokangQian23c5be62022-06-07 02:04:34 +00001145/*
XiaokangQianfb665a82022-06-15 03:57:21 +00001146 * Pick best ( private key, certificate chain ) pair based on the signature
1147 * algorithms supported by the client.
XiaokangQian23c5be62022-06-07 02:04:34 +00001148 */
Ronald Cronce7d76e2022-07-08 18:56:49 +02001149MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01001150static int ssl_tls13_pick_key_cert(mbedtls_ssl_context *ssl)
XiaokangQian23c5be62022-06-07 02:04:34 +00001151{
XiaokangQianfb665a82022-06-15 03:57:21 +00001152 mbedtls_ssl_key_cert *key_cert, *key_cert_list;
XiaokangQian81802f42022-06-10 13:25:22 +00001153 const uint16_t *sig_alg = ssl->handshake->received_sig_algs;
XiaokangQian23c5be62022-06-07 02:04:34 +00001154
1155#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Gilles Peskine449bd832023-01-11 14:50:10 +01001156 if (ssl->handshake->sni_key_cert != NULL) {
XiaokangQianfb665a82022-06-15 03:57:21 +00001157 key_cert_list = ssl->handshake->sni_key_cert;
Gilles Peskine449bd832023-01-11 14:50:10 +01001158 } else
XiaokangQian23c5be62022-06-07 02:04:34 +00001159#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
Gilles Peskine449bd832023-01-11 14:50:10 +01001160 key_cert_list = ssl->conf->key_cert;
XiaokangQian23c5be62022-06-07 02:04:34 +00001161
Gilles Peskine449bd832023-01-11 14:50:10 +01001162 if (key_cert_list == NULL) {
1163 MBEDTLS_SSL_DEBUG_MSG(3, ("server has no certificate"));
1164 return -1;
XiaokangQian23c5be62022-06-07 02:04:34 +00001165 }
1166
Gilles Peskine449bd832023-01-11 14:50:10 +01001167 for (; *sig_alg != MBEDTLS_TLS1_3_SIG_NONE; sig_alg++) {
1168 if (!mbedtls_ssl_sig_alg_is_offered(ssl, *sig_alg)) {
Ronald Cron67ea2542022-09-15 17:34:42 +02001169 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +01001170 }
Ronald Cron67ea2542022-09-15 17:34:42 +02001171
Gilles Peskine449bd832023-01-11 14:50:10 +01001172 if (!mbedtls_ssl_tls13_sig_alg_for_cert_verify_is_supported(*sig_alg)) {
Ronald Cron67ea2542022-09-15 17:34:42 +02001173 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +01001174 }
Ronald Cron67ea2542022-09-15 17:34:42 +02001175
Gilles Peskine449bd832023-01-11 14:50:10 +01001176 for (key_cert = key_cert_list; key_cert != NULL;
1177 key_cert = key_cert->next) {
Ronald Cron67ea2542022-09-15 17:34:42 +02001178#if defined(MBEDTLS_USE_PSA_CRYPTO)
1179 psa_algorithm_t psa_alg = PSA_ALG_NONE;
1180#endif /* MBEDTLS_USE_PSA_CRYPTO */
1181
Gilles Peskine449bd832023-01-11 14:50:10 +01001182 MBEDTLS_SSL_DEBUG_CRT(3, "certificate (chain) candidate",
1183 key_cert->cert);
XiaokangQian81802f42022-06-10 13:25:22 +00001184
1185 /*
Gilles Peskine449bd832023-01-11 14:50:10 +01001186 * This avoids sending the client a cert it'll reject based on
1187 * keyUsage or other extensions.
1188 */
1189 if (mbedtls_x509_crt_check_key_usage(
1190 key_cert->cert, MBEDTLS_X509_KU_DIGITAL_SIGNATURE) != 0 ||
XiaokangQian81802f42022-06-10 13:25:22 +00001191 mbedtls_x509_crt_check_extended_key_usage(
XiaokangQianfb665a82022-06-15 03:57:21 +00001192 key_cert->cert, MBEDTLS_OID_SERVER_AUTH,
Gilles Peskine449bd832023-01-11 14:50:10 +01001193 MBEDTLS_OID_SIZE(MBEDTLS_OID_SERVER_AUTH)) != 0) {
1194 MBEDTLS_SSL_DEBUG_MSG(3, ("certificate mismatch: "
1195 "(extended) key usage extension"));
XiaokangQian81802f42022-06-10 13:25:22 +00001196 continue;
1197 }
XiaokangQian23c5be62022-06-07 02:04:34 +00001198
Gilles Peskine449bd832023-01-11 14:50:10 +01001199 MBEDTLS_SSL_DEBUG_MSG(3,
1200 ("ssl_tls13_pick_key_cert:"
1201 "check signature algorithm %s [%04x]",
1202 mbedtls_ssl_sig_alg_to_str(*sig_alg),
1203 *sig_alg));
Ronald Cron67ea2542022-09-15 17:34:42 +02001204#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01001205 psa_alg = ssl_tls13_iana_sig_alg_to_psa_alg(*sig_alg);
Ronald Cron67ea2542022-09-15 17:34:42 +02001206#endif /* MBEDTLS_USE_PSA_CRYPTO */
1207
Gilles Peskine449bd832023-01-11 14:50:10 +01001208 if (mbedtls_ssl_tls13_check_sig_alg_cert_key_match(
1209 *sig_alg, &key_cert->cert->pk)
Ronald Cron67ea2542022-09-15 17:34:42 +02001210#if defined(MBEDTLS_USE_PSA_CRYPTO)
1211 && psa_alg != PSA_ALG_NONE &&
Gilles Peskine449bd832023-01-11 14:50:10 +01001212 mbedtls_pk_can_do_ext(&key_cert->cert->pk, psa_alg,
1213 PSA_KEY_USAGE_SIGN_HASH) == 1
Ronald Cron67ea2542022-09-15 17:34:42 +02001214#endif /* MBEDTLS_USE_PSA_CRYPTO */
Gilles Peskine449bd832023-01-11 14:50:10 +01001215 ) {
XiaokangQianfb665a82022-06-15 03:57:21 +00001216 ssl->handshake->key_cert = key_cert;
Gilles Peskine449bd832023-01-11 14:50:10 +01001217 MBEDTLS_SSL_DEBUG_MSG(3,
1218 ("ssl_tls13_pick_key_cert:"
1219 "selected signature algorithm"
1220 " %s [%04x]",
1221 mbedtls_ssl_sig_alg_to_str(*sig_alg),
1222 *sig_alg));
XiaokangQianfb665a82022-06-15 03:57:21 +00001223 MBEDTLS_SSL_DEBUG_CRT(
Gilles Peskine449bd832023-01-11 14:50:10 +01001224 3, "selected certificate (chain)",
1225 ssl->handshake->key_cert->cert);
1226 return 0;
XiaokangQian81802f42022-06-10 13:25:22 +00001227 }
XiaokangQian81802f42022-06-10 13:25:22 +00001228 }
XiaokangQian23c5be62022-06-07 02:04:34 +00001229 }
1230
Gilles Peskine449bd832023-01-11 14:50:10 +01001231 MBEDTLS_SSL_DEBUG_MSG(2, ("ssl_tls13_pick_key_cert:"
1232 "no suitable certificate found"));
1233 return -1;
XiaokangQian23c5be62022-06-07 02:04:34 +00001234}
XiaokangQian81802f42022-06-10 13:25:22 +00001235#endif /* MBEDTLS_X509_CRT_PARSE_C &&
Ronald Cron928cbd32022-10-04 16:14:26 +02001236 MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED */
XiaokangQian23c5be62022-06-07 02:04:34 +00001237
XiaokangQian4080a7f2022-04-11 09:55:18 +00001238/*
XiaokangQianed582dd2022-04-13 08:21:05 +00001239 *
1240 * STATE HANDLING: ClientHello
1241 *
XiaokangQianb67384d2022-04-19 00:02:38 +00001242 * There are three possible classes of outcomes when parsing the ClientHello:
XiaokangQianed582dd2022-04-13 08:21:05 +00001243 *
XiaokangQianb67384d2022-04-19 00:02:38 +00001244 * 1) The ClientHello was well-formed and matched the server's configuration.
XiaokangQianed582dd2022-04-13 08:21:05 +00001245 *
1246 * In this case, the server progresses to sending its ServerHello.
1247 *
XiaokangQianb67384d2022-04-19 00:02:38 +00001248 * 2) The ClientHello was well-formed but didn't match the server's
1249 * configuration.
XiaokangQianed582dd2022-04-13 08:21:05 +00001250 *
1251 * For example, the client might not have offered a key share which
1252 * the server supports, or the server might require a cookie.
1253 *
1254 * In this case, the server sends a HelloRetryRequest.
1255 *
XiaokangQianb67384d2022-04-19 00:02:38 +00001256 * 3) The ClientHello was ill-formed
XiaokangQianed582dd2022-04-13 08:21:05 +00001257 *
1258 * In this case, we abort the handshake.
1259 *
1260 */
1261
1262/*
XiaokangQian4080a7f2022-04-11 09:55:18 +00001263 * Structure of this message:
1264 *
XiaokangQiane8ff3502022-04-22 02:34:40 +00001265 * uint16 ProtocolVersion;
1266 * opaque Random[32];
1267 * uint8 CipherSuite[2]; // Cryptographic suite selector
XiaokangQian4080a7f2022-04-11 09:55:18 +00001268 *
XiaokangQiane8ff3502022-04-22 02:34:40 +00001269 * struct {
1270 * ProtocolVersion legacy_version = 0x0303; // TLS v1.2
1271 * Random random;
1272 * opaque legacy_session_id<0..32>;
1273 * CipherSuite cipher_suites<2..2^16-2>;
1274 * opaque legacy_compression_methods<1..2^8-1>;
1275 * Extension extensions<8..2^16-1>;
1276 * } ClientHello;
XiaokangQian4080a7f2022-04-11 09:55:18 +00001277 */
XiaokangQianed582dd2022-04-13 08:21:05 +00001278
1279#define SSL_CLIENT_HELLO_OK 0
1280#define SSL_CLIENT_HELLO_HRR_REQUIRED 1
Ronald Cron5af4c7f2023-03-07 20:46:59 +01001281#define SSL_CLIENT_HELLO_TLS1_2 2
XiaokangQianed582dd2022-04-13 08:21:05 +00001282
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001283MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01001284static int ssl_tls13_parse_client_hello(mbedtls_ssl_context *ssl,
1285 const unsigned char *buf,
1286 const unsigned char *end)
XiaokangQian7807f9f2022-02-15 10:04:37 +00001287{
XiaokangQianb67384d2022-04-19 00:02:38 +00001288 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1289 const unsigned char *p = buf;
Ronald Crond540d992023-03-07 09:41:48 +01001290 const unsigned char *random;
XiaokangQian4080a7f2022-04-11 09:55:18 +00001291 size_t legacy_session_id_len;
Ronald Croncada4102023-03-07 09:51:39 +01001292 const unsigned char *legacy_session_id;
XiaokangQian318dc762022-04-20 09:43:51 +00001293 size_t cipher_suites_len;
Ronald Cron2f16b4e2023-03-07 10:07:32 +01001294 const unsigned char *cipher_suites;
XiaokangQian060d8672022-04-21 09:24:56 +00001295 const unsigned char *cipher_suites_end;
XiaokangQianb67384d2022-04-19 00:02:38 +00001296 size_t extensions_len;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001297 const unsigned char *extensions_end;
Ronald Croneff56732023-04-03 17:36:31 +02001298 const unsigned char *supported_versions_data;
1299 const unsigned char *supported_versions_data_end;
Jerry Yuc4bf5d62022-10-29 09:08:47 +08001300 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
Jerry Yu49ca9282022-05-05 11:05:22 +08001301 int hrr_required = 0;
Ronald Cron8a74f072023-06-14 17:59:29 +02001302 int no_usable_share_for_key_agreement = 0;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001303
Ronald Cron41a443a2022-10-04 16:38:25 +02001304#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED)
Jerry Yu29d9faa2022-08-23 17:52:45 +08001305 const unsigned char *pre_shared_key_ext = NULL;
Jerry Yu1c105562022-07-10 06:32:38 +00001306 const unsigned char *pre_shared_key_ext_end = NULL;
Ronald Cron41a443a2022-10-04 16:38:25 +02001307#endif
XiaokangQian7807f9f2022-02-15 10:04:37 +00001308
XiaokangQian7807f9f2022-02-15 10:04:37 +00001309 /*
XiaokangQianb67384d2022-04-19 00:02:38 +00001310 * ClientHello layout:
XiaokangQian7807f9f2022-02-15 10:04:37 +00001311 * 0 . 1 protocol version
XiaokangQiane8ff3502022-04-22 02:34:40 +00001312 * 2 . 33 random bytes
XiaokangQianc5763b52022-04-02 03:34:37 +00001313 * 34 . 34 session id length ( 1 byte )
XiaokangQian7807f9f2022-02-15 10:04:37 +00001314 * 35 . 34+x session id
XiaokangQian7807f9f2022-02-15 10:04:37 +00001315 * .. . .. ciphersuite list length ( 2 bytes )
1316 * .. . .. ciphersuite list
1317 * .. . .. compression alg. list length ( 1 byte )
1318 * .. . .. compression alg. list
1319 * .. . .. extensions length ( 2 bytes, optional )
1320 * .. . .. extensions ( optional )
1321 */
1322
XiaokangQianb67384d2022-04-19 00:02:38 +00001323 /*
bootstrap-prime6dbbf442022-05-17 19:30:44 -04001324 * Minimal length ( with everything empty and extensions omitted ) is
XiaokangQian7807f9f2022-02-15 10:04:37 +00001325 * 2 + 32 + 1 + 2 + 1 = 38 bytes. Check that first, so that we can
1326 * read at least up to session id length without worrying.
1327 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001328 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, 38);
XiaokangQian7807f9f2022-02-15 10:04:37 +00001329
1330 /* ...
1331 * ProtocolVersion legacy_version = 0x0303; // TLS 1.2
1332 * ...
1333 * with ProtocolVersion defined as:
1334 * uint16 ProtocolVersion;
1335 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001336 if (mbedtls_ssl_read_version(p, ssl->conf->transport) !=
1337 MBEDTLS_SSL_VERSION_TLS1_2) {
1338 MBEDTLS_SSL_DEBUG_MSG(1, ("Unsupported version of TLS."));
1339 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_PROTOCOL_VERSION,
1340 MBEDTLS_ERR_SSL_BAD_PROTOCOL_VERSION);
1341 return MBEDTLS_ERR_SSL_BAD_PROTOCOL_VERSION;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001342 }
1343 p += 2;
1344
Jerry Yuc1be19f2022-04-23 16:11:39 +08001345 /* ...
1346 * Random random;
1347 * ...
XiaokangQianb67384d2022-04-19 00:02:38 +00001348 * with Random defined as:
1349 * opaque Random[32];
XiaokangQian7807f9f2022-02-15 10:04:37 +00001350 */
Ronald Crond540d992023-03-07 09:41:48 +01001351 random = p;
XiaokangQian08037552022-04-20 07:16:41 +00001352 p += MBEDTLS_CLIENT_HELLO_RANDOM_LEN;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001353
Jerry Yuc1be19f2022-04-23 16:11:39 +08001354 /* ...
XiaokangQianb67384d2022-04-19 00:02:38 +00001355 * opaque legacy_session_id<0..32>;
Jerry Yuc1be19f2022-04-23 16:11:39 +08001356 * ...
XiaokangQian7807f9f2022-02-15 10:04:37 +00001357 */
Ronald Croncada4102023-03-07 09:51:39 +01001358 legacy_session_id_len = *(p++);
1359 legacy_session_id = p;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001360
XiaokangQianb67384d2022-04-19 00:02:38 +00001361 /*
1362 * Check we have enough data for the legacy session identifier
Jerry Yue95c8af2022-07-26 15:48:20 +08001363 * and the ciphersuite list length.
XiaokangQianb67384d2022-04-19 00:02:38 +00001364 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001365 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, legacy_session_id_len + 2);
XiaokangQian4080a7f2022-04-11 09:55:18 +00001366 p += legacy_session_id_len;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001367
Ronald Cron2f16b4e2023-03-07 10:07:32 +01001368 /* ...
1369 * CipherSuite cipher_suites<2..2^16-2>;
1370 * ...
1371 * with CipherSuite defined as:
1372 * uint8 CipherSuite[2];
1373 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001374 cipher_suites_len = MBEDTLS_GET_UINT16_BE(p, 0);
XiaokangQian7807f9f2022-02-15 10:04:37 +00001375 p += 2;
Ronald Cron2f16b4e2023-03-07 10:07:32 +01001376 cipher_suites = p;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001377
Ronald Cronfc7ae872023-02-16 15:32:19 +01001378 /*
1379 * The length of the ciphersuite list has to be even.
1380 */
1381 if (cipher_suites_len & 1) {
1382 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR,
1383 MBEDTLS_ERR_SSL_DECODE_ERROR);
1384 return MBEDTLS_ERR_SSL_DECODE_ERROR;
1385 }
1386
XiaokangQianb67384d2022-04-19 00:02:38 +00001387 /* Check we have enough data for the ciphersuite list, the legacy
1388 * compression methods and the length of the extensions.
Jerry Yue95c8af2022-07-26 15:48:20 +08001389 *
1390 * cipher_suites cipher_suites_len bytes
1391 * legacy_compression_methods 2 bytes
1392 * extensions_len 2 bytes
XiaokangQianb67384d2022-04-19 00:02:38 +00001393 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001394 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, cipher_suites_len + 2 + 2);
Ronald Cron8c527d02023-03-07 15:47:47 +01001395 p += cipher_suites_len;
1396 cipher_suites_end = p;
Jerry Yu5725f1c2022-08-21 17:27:16 +08001397
1398 /*
Ronald Cron8c527d02023-03-07 15:47:47 +01001399 * Search for the supported versions extension and parse it to determine
1400 * if the client supports TLS 1.3.
1401 */
1402 ret = mbedtls_ssl_tls13_is_supported_versions_ext_present_in_exts(
1403 ssl, p + 2, end,
Ronald Croneff56732023-04-03 17:36:31 +02001404 &supported_versions_data, &supported_versions_data_end);
Ronald Cron8c527d02023-03-07 15:47:47 +01001405 if (ret < 0) {
1406 MBEDTLS_SSL_DEBUG_RET(1,
1407 ("mbedtls_ssl_tls13_is_supported_versions_ext_present_in_exts"), ret);
1408 return ret;
1409 }
1410
1411 if (ret == 0) {
Ronald Cron5af4c7f2023-03-07 20:46:59 +01001412 return SSL_CLIENT_HELLO_TLS1_2;
Ronald Cron8c527d02023-03-07 15:47:47 +01001413 }
1414
Ronald Cron5af4c7f2023-03-07 20:46:59 +01001415 if (ret == 1) {
1416 ret = ssl_tls13_parse_supported_versions_ext(ssl,
Ronald Croneff56732023-04-03 17:36:31 +02001417 supported_versions_data,
1418 supported_versions_data_end);
Ronald Cron5af4c7f2023-03-07 20:46:59 +01001419 if (ret < 0) {
1420 MBEDTLS_SSL_DEBUG_RET(1,
1421 ("ssl_tls13_parse_supported_versions_ext"), ret);
1422 return ret;
1423 }
1424
Ronald Cronb828c7d2023-04-03 16:37:22 +02001425 /*
1426 * The supported versions extension was parsed successfully as the
1427 * value returned by ssl_tls13_parse_supported_versions_ext() is
1428 * positive. The return value is then equal to
1429 * MBEDTLS_SSL_VERSION_TLS1_2 or MBEDTLS_SSL_VERSION_TLS1_3, defining
1430 * the TLS version to negotiate.
1431 */
Ronald Cron5af4c7f2023-03-07 20:46:59 +01001432 if (MBEDTLS_SSL_VERSION_TLS1_2 == ret) {
1433 return SSL_CLIENT_HELLO_TLS1_2;
1434 }
Ronald Cron8c527d02023-03-07 15:47:47 +01001435 }
1436
1437 /*
1438 * We negotiate TLS 1.3.
Ronald Cron64582392023-03-07 09:21:40 +01001439 */
1440 ssl->tls_version = MBEDTLS_SSL_VERSION_TLS1_3;
Ronald Cron64582392023-03-07 09:21:40 +01001441 ssl->session_negotiate->tls_version = MBEDTLS_SSL_VERSION_TLS1_3;
1442 ssl->session_negotiate->endpoint = ssl->conf->endpoint;
Ronald Cron64582392023-03-07 09:21:40 +01001443
1444 /*
Ronald Crondad02b22023-04-06 09:57:52 +02001445 * We are negotiating the version 1.3 of the protocol. Do what we have
Ronald Croncada4102023-03-07 09:51:39 +01001446 * postponed: copy of the client random bytes, copy of the legacy session
Ronald Cron2f16b4e2023-03-07 10:07:32 +01001447 * identifier and selection of the TLS 1.3 cipher suite.
Ronald Crond540d992023-03-07 09:41:48 +01001448 */
1449 MBEDTLS_SSL_DEBUG_BUF(3, "client hello, random bytes",
1450 random, MBEDTLS_CLIENT_HELLO_RANDOM_LEN);
1451 memcpy(&handshake->randbytes[0], random, MBEDTLS_CLIENT_HELLO_RANDOM_LEN);
1452
Ronald Croncada4102023-03-07 09:51:39 +01001453 if (legacy_session_id_len > sizeof(ssl->session_negotiate->id)) {
1454 MBEDTLS_SSL_DEBUG_MSG(1, ("bad client hello message"));
1455 return MBEDTLS_ERR_SSL_DECODE_ERROR;
1456 }
1457 ssl->session_negotiate->id_len = legacy_session_id_len;
1458 MBEDTLS_SSL_DEBUG_BUF(3, "client hello, session id",
1459 legacy_session_id, legacy_session_id_len);
1460 memcpy(&ssl->session_negotiate->id[0],
1461 legacy_session_id, legacy_session_id_len);
1462
Ronald Crond540d992023-03-07 09:41:48 +01001463 /*
Jerry Yu5725f1c2022-08-21 17:27:16 +08001464 * Search for a matching ciphersuite
1465 */
Ronald Cron2f16b4e2023-03-07 10:07:32 +01001466 MBEDTLS_SSL_DEBUG_BUF(3, "client hello, list of cipher suites",
1467 cipher_suites, cipher_suites_len);
Ronald Crone45afd72023-04-04 15:10:06 +02001468 for (const unsigned char *cipher_suites_p = cipher_suites;
1469 cipher_suites_p < cipher_suites_end; cipher_suites_p += 2) {
XiaokangQiane8ff3502022-04-22 02:34:40 +00001470 uint16_t cipher_suite;
Gilles Peskine449bd832023-01-11 14:50:10 +01001471 const mbedtls_ssl_ciphersuite_t *ciphersuite_info;
Jerry Yu5725f1c2022-08-21 17:27:16 +08001472
Ronald Crond89360b2023-02-21 08:53:33 +01001473 /*
Ronald Crone45afd72023-04-04 15:10:06 +02001474 * "cipher_suites_end - cipher_suites_p is even" is an invariant of the
1475 * loop. As cipher_suites_end - cipher_suites_p > 0, we have
1476 * cipher_suites_end - cipher_suites_p >= 2 and it is thus safe to read
1477 * two bytes.
Ronald Crond89360b2023-02-21 08:53:33 +01001478 */
Ronald Crone45afd72023-04-04 15:10:06 +02001479 cipher_suite = MBEDTLS_GET_UINT16_BE(cipher_suites_p, 0);
Jerry Yuc5a23a02022-08-25 10:51:44 +08001480 ciphersuite_info = ssl_tls13_validate_peer_ciphersuite(
Gilles Peskine449bd832023-01-11 14:50:10 +01001481 ssl, cipher_suite);
1482 if (ciphersuite_info == NULL) {
Jerry Yu5725f1c2022-08-21 17:27:16 +08001483 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +01001484 }
Jerry Yu5725f1c2022-08-21 17:27:16 +08001485
Jerry Yu5725f1c2022-08-21 17:27:16 +08001486 ssl->session_negotiate->ciphersuite = cipher_suite;
Jerry Yuc4bf5d62022-10-29 09:08:47 +08001487 handshake->ciphersuite_info = ciphersuite_info;
Gilles Peskine449bd832023-01-11 14:50:10 +01001488 MBEDTLS_SSL_DEBUG_MSG(2, ("selected ciphersuite: %04x - %s",
1489 cipher_suite,
1490 ciphersuite_info->name));
Ronald Cron25e9ec62023-02-16 15:35:16 +01001491 break;
XiaokangQian17f974c2022-04-19 09:57:41 +00001492 }
Jerry Yu5725f1c2022-08-21 17:27:16 +08001493
Gilles Peskine449bd832023-01-11 14:50:10 +01001494 if (handshake->ciphersuite_info == NULL) {
1495 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE,
1496 MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE);
1497 return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
Jerry Yu5725f1c2022-08-21 17:27:16 +08001498 }
Jerry Yuc1be19f2022-04-23 16:11:39 +08001499
XiaokangQian4080a7f2022-04-11 09:55:18 +00001500 /* ...
XiaokangQianb67384d2022-04-19 00:02:38 +00001501 * opaque legacy_compression_methods<1..2^8-1>;
XiaokangQian4080a7f2022-04-11 09:55:18 +00001502 * ...
XiaokangQian7807f9f2022-02-15 10:04:37 +00001503 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001504 if (p[0] != 1 || p[1] != MBEDTLS_SSL_COMPRESS_NULL) {
1505 MBEDTLS_SSL_DEBUG_MSG(1, ("bad legacy compression method"));
1506 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER,
1507 MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER);
1508 return MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001509 }
XiaokangQianb67384d2022-04-19 00:02:38 +00001510 p += 2;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001511
Jerry Yuc1be19f2022-04-23 16:11:39 +08001512 /* ...
XiaokangQianb67384d2022-04-19 00:02:38 +00001513 * Extension extensions<8..2^16-1>;
Jerry Yuc1be19f2022-04-23 16:11:39 +08001514 * ...
XiaokangQianb67384d2022-04-19 00:02:38 +00001515 * with Extension defined as:
1516 * struct {
1517 * ExtensionType extension_type;
1518 * opaque extension_data<0..2^16-1>;
1519 * } Extension;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001520 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001521 extensions_len = MBEDTLS_GET_UINT16_BE(p, 0);
XiaokangQian7807f9f2022-02-15 10:04:37 +00001522 p += 2;
Gilles Peskine449bd832023-01-11 14:50:10 +01001523 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, extensions_len);
XiaokangQiane8ff3502022-04-22 02:34:40 +00001524 extensions_end = p + extensions_len;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001525
Gilles Peskine449bd832023-01-11 14:50:10 +01001526 MBEDTLS_SSL_DEBUG_BUF(3, "client hello extensions", p, extensions_len);
Jerry Yu63a459c2022-10-31 13:38:40 +08001527 handshake->received_extensions = MBEDTLS_SSL_EXT_MASK_NONE;
Jerry Yu0c354a22022-08-29 15:25:36 +08001528
Gilles Peskine449bd832023-01-11 14:50:10 +01001529 while (p < extensions_end) {
XiaokangQian7807f9f2022-02-15 10:04:37 +00001530 unsigned int extension_type;
1531 size_t extension_data_len;
1532 const unsigned char *extension_data_end;
Jerry Yu263dbf72022-10-26 10:51:27 +08001533 uint32_t allowed_exts = MBEDTLS_SSL_TLS1_3_ALLOWED_EXTS_OF_CH;
1534
Ronald Cron5fbd2702024-02-14 10:03:36 +01001535 if (ssl->handshake->hello_retry_request_flag) {
Jerry Yu263dbf72022-10-26 10:51:27 +08001536 /* Do not accept early data extension in 2nd ClientHello */
1537 allowed_exts &= ~MBEDTLS_SSL_EXT_MASK(EARLY_DATA);
1538 }
XiaokangQian7807f9f2022-02-15 10:04:37 +00001539
Jerry Yu0c354a22022-08-29 15:25:36 +08001540 /* RFC 8446, section 4.2.11
Jerry Yu1c9247c2022-07-21 12:37:39 +08001541 *
1542 * The "pre_shared_key" extension MUST be the last extension in the
1543 * ClientHello (this facilitates implementation as described below).
1544 * Servers MUST check that it is the last extension and otherwise fail
1545 * the handshake with an "illegal_parameter" alert.
1546 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001547 if (handshake->received_extensions & MBEDTLS_SSL_EXT_MASK(PRE_SHARED_KEY)) {
Jerry Yu1c9247c2022-07-21 12:37:39 +08001548 MBEDTLS_SSL_DEBUG_MSG(
Gilles Peskine449bd832023-01-11 14:50:10 +01001549 3, ("pre_shared_key is not last extension."));
Jerry Yu1c9247c2022-07-21 12:37:39 +08001550 MBEDTLS_SSL_PEND_FATAL_ALERT(
1551 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER,
Gilles Peskine449bd832023-01-11 14:50:10 +01001552 MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER);
1553 return MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER;
Jerry Yu1c9247c2022-07-21 12:37:39 +08001554 }
1555
Gilles Peskine449bd832023-01-11 14:50:10 +01001556 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, extensions_end, 4);
1557 extension_type = MBEDTLS_GET_UINT16_BE(p, 0);
1558 extension_data_len = MBEDTLS_GET_UINT16_BE(p, 2);
XiaokangQian7807f9f2022-02-15 10:04:37 +00001559 p += 4;
1560
Gilles Peskine449bd832023-01-11 14:50:10 +01001561 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, extensions_end, extension_data_len);
XiaokangQian7807f9f2022-02-15 10:04:37 +00001562 extension_data_end = p + extension_data_len;
1563
Jerry Yuc4bf5d62022-10-29 09:08:47 +08001564 ret = mbedtls_ssl_tls13_check_received_extension(
Gilles Peskine449bd832023-01-11 14:50:10 +01001565 ssl, MBEDTLS_SSL_HS_CLIENT_HELLO, extension_type,
Jerry Yu263dbf72022-10-26 10:51:27 +08001566 allowed_exts);
Gilles Peskine449bd832023-01-11 14:50:10 +01001567 if (ret != 0) {
1568 return ret;
1569 }
Jerry Yue18dc7e2022-08-04 16:29:22 +08001570
Gilles Peskine449bd832023-01-11 14:50:10 +01001571 switch (extension_type) {
XiaokangQian40a35232022-05-07 09:02:40 +00001572#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
1573 case MBEDTLS_TLS_EXT_SERVERNAME:
Gilles Peskine449bd832023-01-11 14:50:10 +01001574 MBEDTLS_SSL_DEBUG_MSG(3, ("found ServerName extension"));
1575 ret = mbedtls_ssl_parse_server_name_ext(ssl, p,
1576 extension_data_end);
1577 if (ret != 0) {
XiaokangQian40a35232022-05-07 09:02:40 +00001578 MBEDTLS_SSL_DEBUG_RET(
Gilles Peskine449bd832023-01-11 14:50:10 +01001579 1, "mbedtls_ssl_parse_servername_ext", ret);
1580 return ret;
XiaokangQian40a35232022-05-07 09:02:40 +00001581 }
XiaokangQian40a35232022-05-07 09:02:40 +00001582 break;
1583#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
1584
Przemek Stekiel8c0a9532023-06-15 16:48:19 +02001585#if defined(PSA_WANT_ALG_ECDH) || defined(PSA_WANT_ALG_FFDH)
XiaokangQian7807f9f2022-02-15 10:04:37 +00001586 case MBEDTLS_TLS_EXT_SUPPORTED_GROUPS:
Gilles Peskine449bd832023-01-11 14:50:10 +01001587 MBEDTLS_SSL_DEBUG_MSG(3, ("found supported group extension"));
XiaokangQian7807f9f2022-02-15 10:04:37 +00001588
1589 /* Supported Groups Extension
1590 *
1591 * When sent by the client, the "supported_groups" extension
1592 * indicates the named groups which the client supports,
1593 * ordered from most preferred to least preferred.
1594 */
Jerry Yuc1be19f2022-04-23 16:11:39 +08001595 ret = ssl_tls13_parse_supported_groups_ext(
Gilles Peskine449bd832023-01-11 14:50:10 +01001596 ssl, p, extension_data_end);
1597 if (ret != 0) {
Xiaokang Qian9f1747b2023-03-30 02:50:04 +00001598 MBEDTLS_SSL_DEBUG_RET(
Xiaokang Qian8bce0e62023-04-04 10:15:48 +00001599 1, "ssl_tls13_parse_supported_groups_ext", ret);
Gilles Peskine449bd832023-01-11 14:50:10 +01001600 return ret;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001601 }
1602
XiaokangQian7807f9f2022-02-15 10:04:37 +00001603 break;
Przemek Stekiel8c0a9532023-06-15 16:48:19 +02001604#endif /* PSA_WANT_ALG_ECDH || PSA_WANT_ALG_FFDH*/
XiaokangQian7807f9f2022-02-15 10:04:37 +00001605
Valerio Settic9ae8622023-07-25 11:23:50 +02001606#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_EPHEMERAL_ENABLED)
XiaokangQian7807f9f2022-02-15 10:04:37 +00001607 case MBEDTLS_TLS_EXT_KEY_SHARE:
Gilles Peskine449bd832023-01-11 14:50:10 +01001608 MBEDTLS_SSL_DEBUG_MSG(3, ("found key share extension"));
XiaokangQian7807f9f2022-02-15 10:04:37 +00001609
1610 /*
1611 * Key Share Extension
1612 *
1613 * When sent by the client, the "key_share" extension
1614 * contains the endpoint's cryptographic parameters for
1615 * ECDHE/DHE key establishment methods.
1616 */
Jerry Yuc1be19f2022-04-23 16:11:39 +08001617 ret = ssl_tls13_parse_key_shares_ext(
Gilles Peskine449bd832023-01-11 14:50:10 +01001618 ssl, p, extension_data_end);
1619 if (ret == SSL_TLS1_3_PARSE_KEY_SHARES_EXT_NO_MATCH) {
Ronald Cron8a74f072023-06-14 17:59:29 +02001620 MBEDTLS_SSL_DEBUG_MSG(2, ("No usable share for key agreement."));
1621 no_usable_share_for_key_agreement = 1;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001622 }
1623
Gilles Peskine449bd832023-01-11 14:50:10 +01001624 if (ret < 0) {
Jerry Yu582dd062022-04-22 21:59:01 +08001625 MBEDTLS_SSL_DEBUG_RET(
Gilles Peskine449bd832023-01-11 14:50:10 +01001626 1, "ssl_tls13_parse_key_shares_ext", ret);
1627 return ret;
Jerry Yu582dd062022-04-22 21:59:01 +08001628 }
XiaokangQian7807f9f2022-02-15 10:04:37 +00001629
XiaokangQian7807f9f2022-02-15 10:04:37 +00001630 break;
Valerio Settic9ae8622023-07-25 11:23:50 +02001631#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_EPHEMERAL_ENABLED */
XiaokangQian7807f9f2022-02-15 10:04:37 +00001632
1633 case MBEDTLS_TLS_EXT_SUPPORTED_VERSIONS:
Ronald Cron8c527d02023-03-07 15:47:47 +01001634 /* Already parsed */
XiaokangQian7807f9f2022-02-15 10:04:37 +00001635 break;
1636
Ronald Cron41a443a2022-10-04 16:38:25 +02001637#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED)
Jerry Yue19e3b92022-07-08 12:04:51 +00001638 case MBEDTLS_TLS_EXT_PSK_KEY_EXCHANGE_MODES:
Xiaokang Qian9f1747b2023-03-30 02:50:04 +00001639 MBEDTLS_SSL_DEBUG_MSG(
1640 3, ("found psk key exchange modes extension"));
Jerry Yue19e3b92022-07-08 12:04:51 +00001641
1642 ret = ssl_tls13_parse_key_exchange_modes_ext(
Gilles Peskine449bd832023-01-11 14:50:10 +01001643 ssl, p, extension_data_end);
1644 if (ret != 0) {
Jerry Yue19e3b92022-07-08 12:04:51 +00001645 MBEDTLS_SSL_DEBUG_RET(
Gilles Peskine449bd832023-01-11 14:50:10 +01001646 1, "ssl_tls13_parse_key_exchange_modes_ext", ret);
1647 return ret;
Jerry Yue19e3b92022-07-08 12:04:51 +00001648 }
1649
Jerry Yue19e3b92022-07-08 12:04:51 +00001650 break;
Ronald Cron41a443a2022-10-04 16:38:25 +02001651#endif
Jerry Yue19e3b92022-07-08 12:04:51 +00001652
Jerry Yu1c105562022-07-10 06:32:38 +00001653 case MBEDTLS_TLS_EXT_PRE_SHARED_KEY:
Gilles Peskine449bd832023-01-11 14:50:10 +01001654 MBEDTLS_SSL_DEBUG_MSG(3, ("found pre_shared_key extension"));
1655 if ((handshake->received_extensions &
1656 MBEDTLS_SSL_EXT_MASK(PSK_KEY_EXCHANGE_MODES)) == 0) {
Jerry Yu13ab81d2022-07-22 23:17:11 +08001657 MBEDTLS_SSL_PEND_FATAL_ALERT(
1658 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER,
Gilles Peskine449bd832023-01-11 14:50:10 +01001659 MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER);
1660 return MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER;
Jerry Yu13ab81d2022-07-22 23:17:11 +08001661 }
Ronald Cron41a443a2022-10-04 16:38:25 +02001662#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED)
Jerry Yu1c105562022-07-10 06:32:38 +00001663 /* Delay processing of the PSK identity once we have
1664 * found out which algorithms to use. We keep a pointer
1665 * to the buffer and the size for later processing.
1666 */
Jerry Yu29d9faa2022-08-23 17:52:45 +08001667 pre_shared_key_ext = p;
Jerry Yu1c105562022-07-10 06:32:38 +00001668 pre_shared_key_ext_end = extension_data_end;
Jerry Yue18dc7e2022-08-04 16:29:22 +08001669#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED */
Jerry Yu1c105562022-07-10 06:32:38 +00001670 break;
Jerry Yu1c105562022-07-10 06:32:38 +00001671
XiaokangQianacb39922022-06-17 10:18:48 +00001672#if defined(MBEDTLS_SSL_ALPN)
1673 case MBEDTLS_TLS_EXT_ALPN:
Gilles Peskine449bd832023-01-11 14:50:10 +01001674 MBEDTLS_SSL_DEBUG_MSG(3, ("found alpn extension"));
XiaokangQianacb39922022-06-17 10:18:48 +00001675
Gilles Peskine449bd832023-01-11 14:50:10 +01001676 ret = mbedtls_ssl_parse_alpn_ext(ssl, p, extension_data_end);
1677 if (ret != 0) {
XiaokangQianacb39922022-06-17 10:18:48 +00001678 MBEDTLS_SSL_DEBUG_RET(
Gilles Peskine449bd832023-01-11 14:50:10 +01001679 1, ("mbedtls_ssl_parse_alpn_ext"), ret);
1680 return ret;
XiaokangQianacb39922022-06-17 10:18:48 +00001681 }
XiaokangQianacb39922022-06-17 10:18:48 +00001682 break;
1683#endif /* MBEDTLS_SSL_ALPN */
1684
Ronald Cron928cbd32022-10-04 16:14:26 +02001685#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED)
XiaokangQian7807f9f2022-02-15 10:04:37 +00001686 case MBEDTLS_TLS_EXT_SIG_ALG:
Gilles Peskine449bd832023-01-11 14:50:10 +01001687 MBEDTLS_SSL_DEBUG_MSG(3, ("found signature_algorithms extension"));
XiaokangQian7807f9f2022-02-15 10:04:37 +00001688
Gabor Mezei078e8032022-04-27 21:17:56 +02001689 ret = mbedtls_ssl_parse_sig_alg_ext(
Gilles Peskine449bd832023-01-11 14:50:10 +01001690 ssl, p, extension_data_end);
1691 if (ret != 0) {
Xiaokang Qian49f39c12023-04-06 02:31:02 +00001692 MBEDTLS_SSL_DEBUG_RET(
1693 1, "mbedtls_ssl_parse_sig_alg_ext", ret);
Gilles Peskine449bd832023-01-11 14:50:10 +01001694 return ret;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001695 }
XiaokangQian7807f9f2022-02-15 10:04:37 +00001696 break;
Ronald Cron928cbd32022-10-04 16:14:26 +02001697#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED */
XiaokangQian7807f9f2022-02-15 10:04:37 +00001698
Jan Bruckner151f6422023-02-10 12:45:19 +01001699#if defined(MBEDTLS_SSL_RECORD_SIZE_LIMIT)
1700 case MBEDTLS_TLS_EXT_RECORD_SIZE_LIMIT:
1701 MBEDTLS_SSL_DEBUG_MSG(3, ("found record_size_limit extension"));
1702
Xiaokang Qian9f1747b2023-03-30 02:50:04 +00001703 ret = mbedtls_ssl_tls13_parse_record_size_limit_ext(
1704 ssl, p, extension_data_end);
Jan Brucknerf482dcc2023-03-15 09:09:06 +01001705 if (ret != 0) {
1706 MBEDTLS_SSL_DEBUG_RET(
1707 1, ("mbedtls_ssl_tls13_parse_record_size_limit_ext"), ret);
1708 return ret;
1709 }
Jan Bruckner151f6422023-02-10 12:45:19 +01001710 break;
1711#endif /* MBEDTLS_SSL_RECORD_SIZE_LIMIT */
1712
XiaokangQian7807f9f2022-02-15 10:04:37 +00001713 default:
Jerry Yu79aa7212022-11-08 21:30:21 +08001714 MBEDTLS_SSL_PRINT_EXT(
Jerry Yu63a459c2022-10-31 13:38:40 +08001715 3, MBEDTLS_SSL_HS_CLIENT_HELLO,
Gilles Peskine449bd832023-01-11 14:50:10 +01001716 extension_type, "( ignored )");
Jerry Yu0c354a22022-08-29 15:25:36 +08001717 break;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001718 }
1719
1720 p += extension_data_len;
1721 }
1722
Gilles Peskine449bd832023-01-11 14:50:10 +01001723 MBEDTLS_SSL_PRINT_EXTS(3, MBEDTLS_SSL_HS_CLIENT_HELLO,
1724 handshake->received_extensions);
Jerry Yue18dc7e2022-08-04 16:29:22 +08001725
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +01001726 ret = mbedtls_ssl_add_hs_hdr_to_checksum(ssl,
1727 MBEDTLS_SSL_HS_CLIENT_HELLO,
1728 p - buf);
1729 if (0 != ret) {
1730 MBEDTLS_SSL_DEBUG_RET(1, ("mbedtls_ssl_add_hs_hdr_to_checksum"), ret);
1731 return ret;
1732 }
Jerry Yu032b15ce2022-07-11 06:10:03 +00001733
Ronald Cron41a443a2022-10-04 16:38:25 +02001734#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED)
XiaokangQian7807f9f2022-02-15 10:04:37 +00001735 /* Update checksum with either
1736 * - The entire content of the CH message, if no PSK extension is present
1737 * - The content up to but excluding the PSK extension, if present.
1738 */
Jerry Yu1c105562022-07-10 06:32:38 +00001739 /* If we've settled on a PSK-based exchange, parse PSK identity ext */
Pengyu Lvbc4aab72023-12-01 15:37:24 +08001740 if (ssl_tls13_key_exchange_is_psk_available(ssl) ||
1741 ssl_tls13_key_exchange_is_psk_ephemeral_available(ssl)) {
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +01001742 ret = handshake->update_checksum(ssl, buf,
1743 pre_shared_key_ext - buf);
1744 if (0 != ret) {
1745 MBEDTLS_SSL_DEBUG_RET(1, ("update_checksum"), ret);
1746 return ret;
1747 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001748 ret = ssl_tls13_parse_pre_shared_key_ext(ssl,
1749 pre_shared_key_ext,
1750 pre_shared_key_ext_end,
1751 cipher_suites,
1752 cipher_suites_end);
1753 if (ret == MBEDTLS_ERR_SSL_UNKNOWN_IDENTITY) {
1754 handshake->received_extensions &= ~MBEDTLS_SSL_EXT_MASK(PRE_SHARED_KEY);
1755 } else if (ret != 0) {
Jerry Yu63a459c2022-10-31 13:38:40 +08001756 MBEDTLS_SSL_DEBUG_RET(
Gilles Peskine449bd832023-01-11 14:50:10 +01001757 1, "ssl_tls13_parse_pre_shared_key_ext", ret);
1758 return ret;
Jerry Yu1c105562022-07-10 06:32:38 +00001759 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001760 } else
Ronald Cron41a443a2022-10-04 16:38:25 +02001761#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED */
Jerry Yu1c105562022-07-10 06:32:38 +00001762 {
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +01001763 ret = handshake->update_checksum(ssl, buf, p - buf);
1764 if (0 != ret) {
1765 MBEDTLS_SSL_DEBUG_RET(1, ("update_checksum"), ret);
1766 return ret;
1767 }
Jerry Yu1c105562022-07-10 06:32:38 +00001768 }
XiaokangQian7807f9f2022-02-15 10:04:37 +00001769
Gilles Peskine449bd832023-01-11 14:50:10 +01001770 ret = ssl_tls13_determine_key_exchange_mode(ssl);
1771 if (ret < 0) {
1772 return ret;
1773 }
XiaokangQian7807f9f2022-02-15 10:04:37 +00001774
Ronald Cron8a74f072023-06-14 17:59:29 +02001775 if (ssl->handshake->key_exchange_mode !=
1776 MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK) {
1777 hrr_required = (no_usable_share_for_key_agreement != 0);
1778 }
1779
Gilles Peskine449bd832023-01-11 14:50:10 +01001780 mbedtls_ssl_optimize_checksum(ssl, handshake->ciphersuite_info);
Jerry Yue5834fd2022-08-29 20:16:09 +08001781
Gilles Peskine449bd832023-01-11 14:50:10 +01001782 return hrr_required ? SSL_CLIENT_HELLO_HRR_REQUIRED : SSL_CLIENT_HELLO_OK;
XiaokangQian75fe8c72022-06-15 09:42:45 +00001783}
1784
Jerry Yuab0da372023-02-08 13:55:24 +08001785#if defined(MBEDTLS_SSL_EARLY_DATA)
Jerry Yu4caf3ca2023-11-15 16:13:47 +08001786static int ssl_tls13_check_early_data_requirements(mbedtls_ssl_context *ssl)
Jerry Yuab0da372023-02-08 13:55:24 +08001787{
1788 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
1789
Jerry Yu985c9672022-12-04 14:06:30 +08001790 if (ssl->conf->early_data_enabled == MBEDTLS_SSL_EARLY_DATA_DISABLED) {
1791 MBEDTLS_SSL_DEBUG_MSG(
Jerry Yu985c9672022-12-04 14:06:30 +08001792 1,
Jerry Yu454dda32023-10-31 15:13:54 +08001793 ("EarlyData: rejected, feature disabled in server configuration."));
Jerry Yu4caf3ca2023-11-15 16:13:47 +08001794 return -1;
Ronald Cron7b6ee942024-01-12 10:29:55 +01001795 }
1796
Jerry Yu454dda32023-10-31 15:13:54 +08001797 if (!handshake->resume) {
1798 /* We currently support early data only in the case of PSKs established
1799 via a NewSessionTicket message thus in the case of a session
1800 resumption. */
Jerry Yu985c9672022-12-04 14:06:30 +08001801 MBEDTLS_SSL_DEBUG_MSG(
Jerry Yu7ef9fd82023-11-07 14:30:38 +08001802 1, ("EarlyData: rejected, not a session resumption."));
Jerry Yu4caf3ca2023-11-15 16:13:47 +08001803 return -1;
Jerry Yu985c9672022-12-04 14:06:30 +08001804 }
1805
Jerry Yu82fd6c12023-10-31 16:32:19 +08001806 /* RFC 8446 4.2.10
1807 *
1808 * In order to accept early data, the server MUST have accepted a PSK cipher
1809 * suite and selected the first key offered in the client's "pre_shared_key"
1810 * extension. In addition, it MUST verify that the following values are the
1811 * same as those associated with the selected PSK:
1812 * - The TLS version number
1813 * - The selected cipher suite
1814 * - The selected ALPN [RFC7301] protocol, if any
1815 *
1816 * NOTE:
Jerry Yu7ef9fd82023-11-07 14:30:38 +08001817 * - The TLS version number is checked in
1818 * ssl_tls13_offered_psks_check_identity_match_ticket().
1819 * - ALPN is not checked for the time being (TODO).
Jerry Yu82fd6c12023-10-31 16:32:19 +08001820 */
1821
1822 if (handshake->selected_identity != 0) {
1823 MBEDTLS_SSL_DEBUG_MSG(
Jerry Yu7ef9fd82023-11-07 14:30:38 +08001824 1, ("EarlyData: rejected, the selected key in "
1825 "`pre_shared_key` is not the first one."));
Jerry Yu4caf3ca2023-11-15 16:13:47 +08001826 return -1;
Jerry Yu82fd6c12023-10-31 16:32:19 +08001827 }
1828
1829 if (handshake->ciphersuite_info->id !=
1830 ssl->session_negotiate->ciphersuite) {
1831 MBEDTLS_SSL_DEBUG_MSG(
Jerry Yu7ef9fd82023-11-07 14:30:38 +08001832 1, ("EarlyData: rejected, the selected ciphersuite is not the one "
1833 "of the selected pre-shared key."));
Jerry Yu4caf3ca2023-11-15 16:13:47 +08001834 return -1;
Jerry Yu82fd6c12023-10-31 16:32:19 +08001835
1836 }
Jerry Yu985c9672022-12-04 14:06:30 +08001837
Pengyu Lv94a42cc2023-12-06 10:04:17 +08001838 if (!mbedtls_ssl_tls13_session_ticket_allow_early_data(ssl->session_negotiate)) {
Jerry Yufceddb32022-12-12 15:30:34 +08001839 MBEDTLS_SSL_DEBUG_MSG(
1840 1,
Jerry Yuea96ac32023-11-21 17:06:36 +08001841 ("EarlyData: rejected, early_data not allowed in ticket "
1842 "permission bits."));
Jerry Yu4caf3ca2023-11-15 16:13:47 +08001843 return -1;
Jerry Yufceddb32022-12-12 15:30:34 +08001844 }
Jerry Yu985c9672022-12-04 14:06:30 +08001845
Jerry Yu4caf3ca2023-11-15 16:13:47 +08001846 return 0;
Jerry Yuab0da372023-02-08 13:55:24 +08001847}
1848#endif /* MBEDTLS_SSL_EARLY_DATA */
1849
XiaokangQian75fe8c72022-06-15 09:42:45 +00001850/* Update the handshake state machine */
1851
Ronald Cronce7d76e2022-07-08 18:56:49 +02001852MBEDTLS_CHECK_RETURN_CRITICAL
Ronald Cron7b6ee942024-01-12 10:29:55 +01001853static int ssl_tls13_postprocess_client_hello(mbedtls_ssl_context *ssl,
1854 int hrr_required)
XiaokangQian75fe8c72022-06-15 09:42:45 +00001855{
1856 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1857
XiaokangQian7807f9f2022-02-15 10:04:37 +00001858 /*
XiaokangQianfb665a82022-06-15 03:57:21 +00001859 * Server certificate selection
1860 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001861 if (ssl->conf->f_cert_cb && (ret = ssl->conf->f_cert_cb(ssl)) != 0) {
1862 MBEDTLS_SSL_DEBUG_RET(1, "f_cert_cb", ret);
1863 return ret;
XiaokangQianfb665a82022-06-15 03:57:21 +00001864 }
1865#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
1866 ssl->handshake->sni_name = NULL;
1867 ssl->handshake->sni_name_len = 0;
1868#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
XiaokangQian7807f9f2022-02-15 10:04:37 +00001869
Gilles Peskine449bd832023-01-11 14:50:10 +01001870 ret = mbedtls_ssl_tls13_key_schedule_stage_early(ssl);
1871 if (ret != 0) {
1872 MBEDTLS_SSL_DEBUG_RET(1,
1873 "mbedtls_ssl_tls1_3_key_schedule_stage_early", ret);
1874 return ret;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001875 }
1876
Jerry Yuab0da372023-02-08 13:55:24 +08001877#if defined(MBEDTLS_SSL_EARLY_DATA)
Jerry Yu4caf3ca2023-11-15 16:13:47 +08001878 if (ssl->handshake->received_extensions & MBEDTLS_SSL_EXT_MASK(EARLY_DATA)) {
Ronald Cron31e2d832024-02-05 16:45:57 +01001879 ssl->handshake->early_data_accepted =
1880 (!hrr_required) && (ssl_tls13_check_early_data_requirements(ssl) == 0);
Jerry Yu4e9b70e2022-12-04 14:08:02 +08001881
Jerry Yu4caf3ca2023-11-15 16:13:47 +08001882 if (ssl->handshake->early_data_accepted) {
1883 ret = mbedtls_ssl_tls13_compute_early_transform(ssl);
1884 if (ret != 0) {
1885 MBEDTLS_SSL_DEBUG_RET(
1886 1, "mbedtls_ssl_tls13_compute_early_transform", ret);
1887 return ret;
1888 }
1889 } else {
1890 ssl->discard_early_data_record =
1891 hrr_required ?
1892 MBEDTLS_SSL_EARLY_DATA_DISCARD :
1893 MBEDTLS_SSL_EARLY_DATA_TRY_TO_DEPROTECT_AND_DISCARD;
Jerry Yu4e9b70e2022-12-04 14:08:02 +08001894 }
1895 }
Ronald Cron7b6ee942024-01-12 10:29:55 +01001896#else
1897 ((void) hrr_required);
Jerry Yuab0da372023-02-08 13:55:24 +08001898#endif /* MBEDTLS_SSL_EARLY_DATA */
1899
Gilles Peskine449bd832023-01-11 14:50:10 +01001900 return 0;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001901}
1902
1903/*
XiaokangQianed582dd2022-04-13 08:21:05 +00001904 * Main entry point from the state machine; orchestrates the otherfunctions.
1905 */
1906
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001907MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01001908static int ssl_tls13_process_client_hello(mbedtls_ssl_context *ssl)
XiaokangQianed582dd2022-04-13 08:21:05 +00001909{
1910
XiaokangQian08037552022-04-20 07:16:41 +00001911 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine449bd832023-01-11 14:50:10 +01001912 unsigned char *buf = NULL;
XiaokangQianed582dd2022-04-13 08:21:05 +00001913 size_t buflen = 0;
Jerry Yu4ca91402022-05-09 15:50:57 +08001914 int parse_client_hello_ret;
1915
Gilles Peskine449bd832023-01-11 14:50:10 +01001916 MBEDTLS_SSL_DEBUG_MSG(2, ("=> parse client hello"));
XiaokangQianed582dd2022-04-13 08:21:05 +00001917
Gilles Peskine449bd832023-01-11 14:50:10 +01001918 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_tls13_fetch_handshake_msg(
1919 ssl, MBEDTLS_SSL_HS_CLIENT_HELLO,
1920 &buf, &buflen));
XiaokangQianed582dd2022-04-13 08:21:05 +00001921
Gilles Peskine449bd832023-01-11 14:50:10 +01001922 MBEDTLS_SSL_PROC_CHK_NEG(ssl_tls13_parse_client_hello(ssl, buf,
1923 buf + buflen));
Ronald Cron5af4c7f2023-03-07 20:46:59 +01001924 parse_client_hello_ret = ret; /* Store positive return value of
1925 * parse_client_hello,
1926 * as negative error codes are handled
Jerry Yuf41553b2022-05-09 22:20:30 +08001927 * by MBEDTLS_SSL_PROC_CHK_NEG. */
Jerry Yu4ca91402022-05-09 15:50:57 +08001928
Ronald Cronb828c7d2023-04-03 16:37:22 +02001929 /*
Yanray Wang90acdc62023-12-08 10:29:42 +08001930 * Version 1.2 of the protocol has to be used for the handshake.
1931 * If TLS 1.2 is not supported, abort the handshake. Otherwise, set the
Ronald Cronb828c7d2023-04-03 16:37:22 +02001932 * ssl->keep_current_message flag for the ClientHello to be kept and parsed
1933 * as a TLS 1.2 ClientHello. We also change ssl->tls_version to
1934 * MBEDTLS_SSL_VERSION_TLS1_2 thus from now on mbedtls_ssl_handshake_step()
1935 * will dispatch to the TLS 1.2 state machine.
1936 */
Ronald Cron5af4c7f2023-03-07 20:46:59 +01001937 if (SSL_CLIENT_HELLO_TLS1_2 == parse_client_hello_ret) {
Yanray Wangfb0f47b2023-12-04 15:27:28 +08001938 /* Check if server supports TLS 1.2 */
Yanray Wang408ba6f2023-12-08 10:18:03 +08001939 if (!mbedtls_ssl_conf_is_tls12_enabled(ssl->conf)) {
Yanray Wangfb0f47b2023-12-04 15:27:28 +08001940 MBEDTLS_SSL_DEBUG_MSG(
Yanray Wang177e49a2023-12-08 10:51:04 +08001941 1, ("TLS 1.2 not supported."));
Yanray Wangfb0f47b2023-12-04 15:27:28 +08001942 MBEDTLS_SSL_PEND_FATAL_ALERT(
Yanray Wang2bef9172023-12-08 10:21:53 +08001943 MBEDTLS_SSL_ALERT_MSG_PROTOCOL_VERSION,
1944 MBEDTLS_ERR_SSL_BAD_PROTOCOL_VERSION);
1945 return MBEDTLS_ERR_SSL_BAD_PROTOCOL_VERSION;
Yanray Wangfb0f47b2023-12-04 15:27:28 +08001946 }
Ronald Cron5af4c7f2023-03-07 20:46:59 +01001947 ssl->keep_current_message = 1;
1948 ssl->tls_version = MBEDTLS_SSL_VERSION_TLS1_2;
1949 return 0;
1950 }
1951
Ronald Cron7b6ee942024-01-12 10:29:55 +01001952 MBEDTLS_SSL_PROC_CHK(
1953 ssl_tls13_postprocess_client_hello(ssl, parse_client_hello_ret ==
1954 SSL_CLIENT_HELLO_HRR_REQUIRED));
Jerry Yu582dd062022-04-22 21:59:01 +08001955
Ronald Cron5af4c7f2023-03-07 20:46:59 +01001956 if (SSL_CLIENT_HELLO_OK == parse_client_hello_ret) {
Gilles Peskine449bd832023-01-11 14:50:10 +01001957 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_SERVER_HELLO);
1958 } else {
1959 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_HELLO_RETRY_REQUEST);
1960 }
XiaokangQianed582dd2022-04-13 08:21:05 +00001961
1962cleanup:
1963
Gilles Peskine449bd832023-01-11 14:50:10 +01001964 MBEDTLS_SSL_DEBUG_MSG(2, ("<= parse client hello"));
1965 return ret;
XiaokangQianed582dd2022-04-13 08:21:05 +00001966}
1967
1968/*
Jerry Yu1c3e6882022-04-20 21:23:40 +08001969 * Handler for MBEDTLS_SSL_SERVER_HELLO
Jerry Yu5b64ae92022-03-30 17:15:02 +08001970 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001971MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01001972static int ssl_tls13_prepare_server_hello(mbedtls_ssl_context *ssl)
Jerry Yuf4b27e42022-03-30 17:32:21 +08001973{
Jerry Yu637a3f12022-04-20 21:37:58 +08001974 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1975 unsigned char *server_randbytes =
Gilles Peskine449bd832023-01-11 14:50:10 +01001976 ssl->handshake->randbytes + MBEDTLS_CLIENT_HELLO_RANDOM_LEN;
1977 if (ssl->conf->f_rng == NULL) {
1978 MBEDTLS_SSL_DEBUG_MSG(1, ("no RNG provided"));
1979 return MBEDTLS_ERR_SSL_NO_RNG;
Jerry Yuf4b27e42022-03-30 17:32:21 +08001980 }
1981
Gilles Peskine449bd832023-01-11 14:50:10 +01001982 if ((ret = ssl->conf->f_rng(ssl->conf->p_rng, server_randbytes,
1983 MBEDTLS_SERVER_HELLO_RANDOM_LEN)) != 0) {
1984 MBEDTLS_SSL_DEBUG_RET(1, "f_rng", ret);
1985 return ret;
Jerry Yuf4b27e42022-03-30 17:32:21 +08001986 }
1987
Gilles Peskine449bd832023-01-11 14:50:10 +01001988 MBEDTLS_SSL_DEBUG_BUF(3, "server hello, random bytes", server_randbytes,
1989 MBEDTLS_SERVER_HELLO_RANDOM_LEN);
Jerry Yuf4b27e42022-03-30 17:32:21 +08001990
1991#if defined(MBEDTLS_HAVE_TIME)
YxCda609132023-05-22 12:08:12 -07001992 ssl->session_negotiate->start = mbedtls_time(NULL);
Jerry Yuf4b27e42022-03-30 17:32:21 +08001993#endif /* MBEDTLS_HAVE_TIME */
1994
Gilles Peskine449bd832023-01-11 14:50:10 +01001995 return ret;
Jerry Yuf4b27e42022-03-30 17:32:21 +08001996}
1997
Jerry Yu3bf2c642022-03-30 22:02:12 +08001998/*
Jerry Yue74e04a2022-04-21 09:23:16 +08001999 * ssl_tls13_write_server_hello_supported_versions_ext ():
Jerry Yu3bf2c642022-03-30 22:02:12 +08002000 *
2001 * struct {
Jerry Yufb9f54d2022-04-06 10:08:34 +08002002 * ProtocolVersion selected_version;
Jerry Yu3bf2c642022-03-30 22:02:12 +08002003 * } SupportedVersions;
2004 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002005MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yue74e04a2022-04-21 09:23:16 +08002006static int ssl_tls13_write_server_hello_supported_versions_ext(
Gilles Peskine449bd832023-01-11 14:50:10 +01002007 mbedtls_ssl_context *ssl,
2008 unsigned char *buf,
2009 unsigned char *end,
2010 size_t *out_len)
Jerry Yu3bf2c642022-03-30 22:02:12 +08002011{
Jerry Yu3bf2c642022-03-30 22:02:12 +08002012 *out_len = 0;
2013
Gilles Peskine449bd832023-01-11 14:50:10 +01002014 MBEDTLS_SSL_DEBUG_MSG(3, ("server hello, write selected version"));
Jerry Yu3bf2c642022-03-30 22:02:12 +08002015
2016 /* Check if we have space to write the extension:
2017 * - extension_type (2 bytes)
2018 * - extension_data_length (2 bytes)
Jerry Yu349a6132022-04-14 20:52:56 +08002019 * - selected_version (2 bytes)
Jerry Yu3bf2c642022-03-30 22:02:12 +08002020 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002021 MBEDTLS_SSL_CHK_BUF_PTR(buf, end, 6);
Jerry Yu3bf2c642022-03-30 22:02:12 +08002022
Gilles Peskine449bd832023-01-11 14:50:10 +01002023 MBEDTLS_PUT_UINT16_BE(MBEDTLS_TLS_EXT_SUPPORTED_VERSIONS, buf, 0);
Jerry Yu3bf2c642022-03-30 22:02:12 +08002024
Gilles Peskine449bd832023-01-11 14:50:10 +01002025 MBEDTLS_PUT_UINT16_BE(2, buf, 2);
Jerry Yu3bf2c642022-03-30 22:02:12 +08002026
Gilles Peskine449bd832023-01-11 14:50:10 +01002027 mbedtls_ssl_write_version(buf + 4,
2028 ssl->conf->transport,
2029 ssl->tls_version);
Jerry Yu3bf2c642022-03-30 22:02:12 +08002030
Gilles Peskine449bd832023-01-11 14:50:10 +01002031 MBEDTLS_SSL_DEBUG_MSG(3, ("supported version: [%04x]",
2032 ssl->tls_version));
Jerry Yu3bf2c642022-03-30 22:02:12 +08002033
Jerry Yu349a6132022-04-14 20:52:56 +08002034 *out_len = 6;
Jerry Yu3bf2c642022-03-30 22:02:12 +08002035
Jerry Yub95dd362022-11-08 21:19:34 +08002036 mbedtls_ssl_tls13_set_hs_sent_ext_mask(
Gilles Peskine449bd832023-01-11 14:50:10 +01002037 ssl, MBEDTLS_TLS_EXT_SUPPORTED_VERSIONS);
Jerry Yub95dd362022-11-08 21:19:34 +08002038
Gilles Peskine449bd832023-01-11 14:50:10 +01002039 return 0;
Jerry Yu3bf2c642022-03-30 22:02:12 +08002040}
2041
Jerry Yud9436a12022-04-20 22:28:09 +08002042
Jerry Yu3bf2c642022-03-30 22:02:12 +08002043
2044/* Generate and export a single key share. For hybrid KEMs, this can
2045 * be called multiple times with the different components of the hybrid. */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002046MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002047static int ssl_tls13_generate_and_write_key_share(mbedtls_ssl_context *ssl,
2048 uint16_t named_group,
2049 unsigned char *buf,
2050 unsigned char *end,
2051 size_t *out_len)
Jerry Yu3bf2c642022-03-30 22:02:12 +08002052{
2053 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yu955ddd72022-04-22 22:27:33 +08002054
2055 *out_len = 0;
2056
Valerio Settic9ae8622023-07-25 11:23:50 +02002057#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_EPHEMERAL_ENABLED)
Przemek Stekiel29c219c2023-05-31 15:21:04 +02002058 if (mbedtls_ssl_tls13_named_group_is_ecdhe(named_group) ||
Przemek Stekield5f79e72023-06-29 09:08:43 +02002059 mbedtls_ssl_tls13_named_group_is_ffdh(named_group)) {
Przemek Stekiel408569f2023-07-06 11:26:44 +02002060 ret = mbedtls_ssl_tls13_generate_and_write_xxdh_key_exchange(
Gilles Peskine449bd832023-01-11 14:50:10 +01002061 ssl, named_group, buf, end, out_len);
2062 if (ret != 0) {
Jerry Yu89e103c2022-03-30 22:43:29 +08002063 MBEDTLS_SSL_DEBUG_RET(
Przemek Stekiel408569f2023-07-06 11:26:44 +02002064 1, "mbedtls_ssl_tls13_generate_and_write_xxdh_key_exchange",
Gilles Peskine449bd832023-01-11 14:50:10 +01002065 ret);
2066 return ret;
Jerry Yu3bf2c642022-03-30 22:02:12 +08002067 }
Gilles Peskine449bd832023-01-11 14:50:10 +01002068 } else
Valerio Settic9ae8622023-07-25 11:23:50 +02002069#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_EPHEMERAL_ENABLED */
Gilles Peskine449bd832023-01-11 14:50:10 +01002070 if (0 /* Other kinds of KEMs */) {
2071 } else {
Jerry Yu955ddd72022-04-22 22:27:33 +08002072 ((void) ssl);
2073 ((void) named_group);
2074 ((void) buf);
2075 ((void) end);
Jerry Yu3bf2c642022-03-30 22:02:12 +08002076 ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
2077 }
2078
Gilles Peskine449bd832023-01-11 14:50:10 +01002079 return ret;
Jerry Yu3bf2c642022-03-30 22:02:12 +08002080}
2081
2082/*
2083 * ssl_tls13_write_key_share_ext
2084 *
2085 * Structure of key_share extension in ServerHello:
2086 *
Jerry Yu1c3e6882022-04-20 21:23:40 +08002087 * struct {
2088 * NamedGroup group;
2089 * opaque key_exchange<1..2^16-1>;
2090 * } KeyShareEntry;
2091 * struct {
2092 * KeyShareEntry server_share;
2093 * } KeyShareServerHello;
Jerry Yu3bf2c642022-03-30 22:02:12 +08002094 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002095MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002096static int ssl_tls13_write_key_share_ext(mbedtls_ssl_context *ssl,
2097 unsigned char *buf,
2098 unsigned char *end,
2099 size_t *out_len)
Jerry Yu3bf2c642022-03-30 22:02:12 +08002100{
Jerry Yu955ddd72022-04-22 22:27:33 +08002101 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yu3bf2c642022-03-30 22:02:12 +08002102 unsigned char *p = buf;
Jerry Yu57d48412022-04-20 21:50:42 +08002103 uint16_t group = ssl->handshake->offered_group_id;
Jerry Yu3bf2c642022-03-30 22:02:12 +08002104 unsigned char *server_share = buf + 4;
Jerry Yu3bf2c642022-03-30 22:02:12 +08002105 size_t key_exchange_length;
Jerry Yu3bf2c642022-03-30 22:02:12 +08002106
2107 *out_len = 0;
2108
Gilles Peskine449bd832023-01-11 14:50:10 +01002109 MBEDTLS_SSL_DEBUG_MSG(3, ("server hello, adding key share extension"));
Jerry Yu3bf2c642022-03-30 22:02:12 +08002110
Gilles Peskine449bd832023-01-11 14:50:10 +01002111 MBEDTLS_SSL_DEBUG_MSG(2, ("server hello, write selected_group: %s (%04x)",
2112 mbedtls_ssl_named_group_to_str(group),
2113 group));
Jerry Yu58af2332022-09-06 11:19:31 +08002114
Jerry Yu3bf2c642022-03-30 22:02:12 +08002115 /* Check if we have space for header and length fields:
2116 * - extension_type (2 bytes)
2117 * - extension_data_length (2 bytes)
2118 * - group (2 bytes)
2119 * - key_exchange_length (2 bytes)
2120 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002121 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 8);
2122 MBEDTLS_PUT_UINT16_BE(MBEDTLS_TLS_EXT_KEY_SHARE, p, 0);
2123 MBEDTLS_PUT_UINT16_BE(group, server_share, 0);
Jerry Yu3bf2c642022-03-30 22:02:12 +08002124 p += 8;
Jerry Yu57d48412022-04-20 21:50:42 +08002125
Jerry Yu3bf2c642022-03-30 22:02:12 +08002126 /* When we introduce PQC-ECDHE hybrids, we'll want to call this
2127 * function multiple times. */
Jerry Yu955ddd72022-04-22 22:27:33 +08002128 ret = ssl_tls13_generate_and_write_key_share(
Gilles Peskine449bd832023-01-11 14:50:10 +01002129 ssl, group, server_share + 4, end, &key_exchange_length);
2130 if (ret != 0) {
2131 return ret;
2132 }
Jerry Yu3bf2c642022-03-30 22:02:12 +08002133 p += key_exchange_length;
Jerry Yuc1be19f2022-04-23 16:11:39 +08002134
Gilles Peskine449bd832023-01-11 14:50:10 +01002135 MBEDTLS_PUT_UINT16_BE(key_exchange_length, server_share + 2, 0);
Jerry Yu3bf2c642022-03-30 22:02:12 +08002136
Gilles Peskine449bd832023-01-11 14:50:10 +01002137 MBEDTLS_PUT_UINT16_BE(p - server_share, buf, 2);
Jerry Yu3bf2c642022-03-30 22:02:12 +08002138
Jerry Yu57d48412022-04-20 21:50:42 +08002139 *out_len = p - buf;
Jerry Yu955ddd72022-04-22 22:27:33 +08002140
Gilles Peskine449bd832023-01-11 14:50:10 +01002141 mbedtls_ssl_tls13_set_hs_sent_ext_mask(ssl, MBEDTLS_TLS_EXT_KEY_SHARE);
Jerry Yub95dd362022-11-08 21:19:34 +08002142
Gilles Peskine449bd832023-01-11 14:50:10 +01002143 return 0;
Jerry Yu3bf2c642022-03-30 22:02:12 +08002144}
Jerry Yud9436a12022-04-20 22:28:09 +08002145
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002146MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002147static int ssl_tls13_write_hrr_key_share_ext(mbedtls_ssl_context *ssl,
2148 unsigned char *buf,
2149 unsigned char *end,
2150 size_t *out_len)
Jerry Yu23f7a6f2022-04-23 15:16:45 +08002151{
2152 uint16_t selected_group = ssl->handshake->hrr_selected_group;
2153 /* key_share Extension
2154 *
2155 * struct {
2156 * select (Handshake.msg_type) {
2157 * ...
2158 * case hello_retry_request:
2159 * NamedGroup selected_group;
2160 * ...
2161 * };
2162 * } KeyShare;
2163 */
2164
2165 *out_len = 0;
2166
Jerry Yub0ac10b2022-05-05 11:10:08 +08002167 /*
2168 * For a pure PSK key exchange, there is no group to agree upon. The purpose
2169 * of the HRR is then to transmit a cookie to force the client to demonstrate
2170 * reachability at their apparent network address (primarily useful for DTLS).
2171 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002172 if (!mbedtls_ssl_tls13_key_exchange_mode_with_ephemeral(ssl)) {
2173 return 0;
2174 }
Jerry Yu23f7a6f2022-04-23 15:16:45 +08002175
2176 /* We should only send the key_share extension if the client's initial
2177 * key share was not acceptable. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002178 if (ssl->handshake->offered_group_id != 0) {
2179 MBEDTLS_SSL_DEBUG_MSG(4, ("Skip key_share extension in HRR"));
2180 return 0;
Jerry Yu23f7a6f2022-04-23 15:16:45 +08002181 }
2182
Gilles Peskine449bd832023-01-11 14:50:10 +01002183 if (selected_group == 0) {
2184 MBEDTLS_SSL_DEBUG_MSG(1, ("no matching named group found"));
2185 return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
Jerry Yu23f7a6f2022-04-23 15:16:45 +08002186 }
2187
Jerry Yub0ac10b2022-05-05 11:10:08 +08002188 /* Check if we have enough space:
2189 * - extension_type (2 bytes)
2190 * - extension_data_length (2 bytes)
2191 * - selected_group (2 bytes)
2192 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002193 MBEDTLS_SSL_CHK_BUF_PTR(buf, end, 6);
Jerry Yu23f7a6f2022-04-23 15:16:45 +08002194
Gilles Peskine449bd832023-01-11 14:50:10 +01002195 MBEDTLS_PUT_UINT16_BE(MBEDTLS_TLS_EXT_KEY_SHARE, buf, 0);
2196 MBEDTLS_PUT_UINT16_BE(2, buf, 2);
2197 MBEDTLS_PUT_UINT16_BE(selected_group, buf, 4);
Jerry Yu23f7a6f2022-04-23 15:16:45 +08002198
Gilles Peskine449bd832023-01-11 14:50:10 +01002199 MBEDTLS_SSL_DEBUG_MSG(3,
2200 ("HRR selected_group: %s (%x)",
2201 mbedtls_ssl_named_group_to_str(selected_group),
2202 selected_group));
Jerry Yu23f7a6f2022-04-23 15:16:45 +08002203
2204 *out_len = 6;
Jerry Yu23f7a6f2022-04-23 15:16:45 +08002205
Gilles Peskine449bd832023-01-11 14:50:10 +01002206 mbedtls_ssl_tls13_set_hs_sent_ext_mask(ssl, MBEDTLS_TLS_EXT_KEY_SHARE);
Jerry Yub95dd362022-11-08 21:19:34 +08002207
Gilles Peskine449bd832023-01-11 14:50:10 +01002208 return 0;
Jerry Yu23f7a6f2022-04-23 15:16:45 +08002209}
Jerry Yu3bf2c642022-03-30 22:02:12 +08002210
2211/*
2212 * Structure of ServerHello message:
2213 *
2214 * struct {
2215 * ProtocolVersion legacy_version = 0x0303; // TLS v1.2
2216 * Random random;
2217 * opaque legacy_session_id_echo<0..32>;
2218 * CipherSuite cipher_suite;
2219 * uint8 legacy_compression_method = 0;
2220 * Extension extensions<6..2^16-1>;
2221 * } ServerHello;
2222 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002223MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002224static int ssl_tls13_write_server_hello_body(mbedtls_ssl_context *ssl,
2225 unsigned char *buf,
2226 unsigned char *end,
2227 size_t *out_len,
2228 int is_hrr)
Jerry Yu56404d72022-03-30 17:36:13 +08002229{
Jerry Yu955ddd72022-04-22 22:27:33 +08002230 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yu3bf2c642022-03-30 22:02:12 +08002231 unsigned char *p = buf;
Jerry Yud9436a12022-04-20 22:28:09 +08002232 unsigned char *p_extensions_len;
Jerry Yufbe3e642022-04-25 19:31:51 +08002233 size_t output_len;
Jerry Yu3bf2c642022-03-30 22:02:12 +08002234
2235 *out_len = 0;
Jerry Yu50e00e32022-10-31 14:45:01 +08002236 ssl->handshake->sent_extensions = MBEDTLS_SSL_EXT_MASK_NONE;
Jerry Yu3bf2c642022-03-30 22:02:12 +08002237
Jerry Yucfc04b32022-04-21 09:31:58 +08002238 /* ...
2239 * ProtocolVersion legacy_version = 0x0303; // TLS 1.2
2240 * ...
2241 * with ProtocolVersion defined as:
2242 * uint16 ProtocolVersion;
Jerry Yu3bf2c642022-03-30 22:02:12 +08002243 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002244 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 2);
2245 MBEDTLS_PUT_UINT16_BE(0x0303, p, 0);
Jerry Yu3bf2c642022-03-30 22:02:12 +08002246 p += 2;
2247
Jerry Yu1c3e6882022-04-20 21:23:40 +08002248 /* ...
2249 * Random random;
2250 * ...
Jerry Yucfc04b32022-04-21 09:31:58 +08002251 * with Random defined as:
2252 * opaque Random[MBEDTLS_SERVER_HELLO_RANDOM_LEN];
Jerry Yu1c3e6882022-04-20 21:23:40 +08002253 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002254 MBEDTLS_SSL_CHK_BUF_PTR(p, end, MBEDTLS_SERVER_HELLO_RANDOM_LEN);
2255 if (is_hrr) {
2256 memcpy(p, mbedtls_ssl_tls13_hello_retry_request_magic,
2257 MBEDTLS_SERVER_HELLO_RANDOM_LEN);
2258 } else {
2259 memcpy(p, &ssl->handshake->randbytes[MBEDTLS_CLIENT_HELLO_RANDOM_LEN],
2260 MBEDTLS_SERVER_HELLO_RANDOM_LEN);
Jerry Yu23f7a6f2022-04-23 15:16:45 +08002261 }
Gilles Peskine449bd832023-01-11 14:50:10 +01002262 MBEDTLS_SSL_DEBUG_BUF(3, "server hello, random bytes",
2263 p, MBEDTLS_SERVER_HELLO_RANDOM_LEN);
Jerry Yu3bf2c642022-03-30 22:02:12 +08002264 p += MBEDTLS_SERVER_HELLO_RANDOM_LEN;
2265
Jerry Yucfc04b32022-04-21 09:31:58 +08002266 /* ...
2267 * opaque legacy_session_id_echo<0..32>;
2268 * ...
Jerry Yu3bf2c642022-03-30 22:02:12 +08002269 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002270 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 1 + ssl->session_negotiate->id_len);
2271 *p++ = (unsigned char) ssl->session_negotiate->id_len;
2272 if (ssl->session_negotiate->id_len > 0) {
2273 memcpy(p, &ssl->session_negotiate->id[0],
2274 ssl->session_negotiate->id_len);
Jerry Yu3bf2c642022-03-30 22:02:12 +08002275 p += ssl->session_negotiate->id_len;
Jerry Yu955ddd72022-04-22 22:27:33 +08002276
Gilles Peskine449bd832023-01-11 14:50:10 +01002277 MBEDTLS_SSL_DEBUG_BUF(3, "session id", ssl->session_negotiate->id,
2278 ssl->session_negotiate->id_len);
Jerry Yu3bf2c642022-03-30 22:02:12 +08002279 }
2280
Jerry Yucfc04b32022-04-21 09:31:58 +08002281 /* ...
2282 * CipherSuite cipher_suite;
2283 * ...
2284 * with CipherSuite defined as:
2285 * uint8 CipherSuite[2];
Jerry Yu3bf2c642022-03-30 22:02:12 +08002286 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002287 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 2);
2288 MBEDTLS_PUT_UINT16_BE(ssl->session_negotiate->ciphersuite, p, 0);
Jerry Yu3bf2c642022-03-30 22:02:12 +08002289 p += 2;
Gilles Peskine449bd832023-01-11 14:50:10 +01002290 MBEDTLS_SSL_DEBUG_MSG(3,
2291 ("server hello, chosen ciphersuite: %s ( id=%d )",
2292 mbedtls_ssl_get_ciphersuite_name(
2293 ssl->session_negotiate->ciphersuite),
2294 ssl->session_negotiate->ciphersuite));
Jerry Yu3bf2c642022-03-30 22:02:12 +08002295
Jerry Yucfc04b32022-04-21 09:31:58 +08002296 /* ...
2297 * uint8 legacy_compression_method = 0;
2298 * ...
2299 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002300 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 1);
Thomas Daubney31e03a82022-07-25 15:59:25 +01002301 *p++ = MBEDTLS_SSL_COMPRESS_NULL;
Jerry Yu3bf2c642022-03-30 22:02:12 +08002302
Jerry Yucfc04b32022-04-21 09:31:58 +08002303 /* ...
2304 * Extension extensions<6..2^16-1>;
2305 * ...
2306 * struct {
2307 * ExtensionType extension_type; (2 bytes)
2308 * opaque extension_data<0..2^16-1>;
2309 * } Extension;
2310 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002311 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 2);
Jerry Yud9436a12022-04-20 22:28:09 +08002312 p_extensions_len = p;
Jerry Yu3bf2c642022-03-30 22:02:12 +08002313 p += 2;
2314
Gilles Peskine449bd832023-01-11 14:50:10 +01002315 if ((ret = ssl_tls13_write_server_hello_supported_versions_ext(
2316 ssl, p, end, &output_len)) != 0) {
Jerry Yu955ddd72022-04-22 22:27:33 +08002317 MBEDTLS_SSL_DEBUG_RET(
Gilles Peskine449bd832023-01-11 14:50:10 +01002318 1, "ssl_tls13_write_server_hello_supported_versions_ext", ret);
2319 return ret;
Jerry Yu3bf2c642022-03-30 22:02:12 +08002320 }
2321 p += output_len;
2322
Gilles Peskine449bd832023-01-11 14:50:10 +01002323 if (mbedtls_ssl_tls13_key_exchange_mode_with_ephemeral(ssl)) {
2324 if (is_hrr) {
2325 ret = ssl_tls13_write_hrr_key_share_ext(ssl, p, end, &output_len);
2326 } else {
2327 ret = ssl_tls13_write_key_share_ext(ssl, p, end, &output_len);
2328 }
2329 if (ret != 0) {
2330 return ret;
2331 }
Jerry Yu3bf2c642022-03-30 22:02:12 +08002332 p += output_len;
2333 }
Jerry Yu3bf2c642022-03-30 22:02:12 +08002334
Ronald Cron41a443a2022-10-04 16:38:25 +02002335#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01002336 if (!is_hrr && mbedtls_ssl_tls13_key_exchange_mode_with_psk(ssl)) {
2337 ret = ssl_tls13_write_server_pre_shared_key_ext(ssl, p, end, &output_len);
2338 if (ret != 0) {
2339 MBEDTLS_SSL_DEBUG_RET(1, "ssl_tls13_write_server_pre_shared_key_ext",
2340 ret);
2341 return ret;
Jerry Yu032b15ce2022-07-11 06:10:03 +00002342 }
2343 p += output_len;
2344 }
Ronald Cron41a443a2022-10-04 16:38:25 +02002345#endif
Jerry Yu032b15ce2022-07-11 06:10:03 +00002346
Gilles Peskine449bd832023-01-11 14:50:10 +01002347 MBEDTLS_PUT_UINT16_BE(p - p_extensions_len - 2, p_extensions_len, 0);
Jerry Yu3bf2c642022-03-30 22:02:12 +08002348
Gilles Peskine449bd832023-01-11 14:50:10 +01002349 MBEDTLS_SSL_DEBUG_BUF(4, "server hello extensions",
2350 p_extensions_len, p - p_extensions_len);
Jerry Yu3bf2c642022-03-30 22:02:12 +08002351
Jerry Yud9436a12022-04-20 22:28:09 +08002352 *out_len = p - buf;
Jerry Yu3bf2c642022-03-30 22:02:12 +08002353
Gilles Peskine449bd832023-01-11 14:50:10 +01002354 MBEDTLS_SSL_DEBUG_BUF(3, "server hello", buf, *out_len);
Jerry Yu3bf2c642022-03-30 22:02:12 +08002355
Jerry Yu7de2ff02022-11-08 21:43:46 +08002356 MBEDTLS_SSL_PRINT_EXTS(
Jerry Yu4b8f2f72022-10-31 13:31:22 +08002357 3, is_hrr ? MBEDTLS_SSL_TLS1_3_HS_HELLO_RETRY_REQUEST :
Gilles Peskine449bd832023-01-11 14:50:10 +01002358 MBEDTLS_SSL_HS_SERVER_HELLO,
2359 ssl->handshake->sent_extensions);
Jerry Yu4b8f2f72022-10-31 13:31:22 +08002360
Gilles Peskine449bd832023-01-11 14:50:10 +01002361 return ret;
Jerry Yu56404d72022-03-30 17:36:13 +08002362}
2363
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002364MBEDTLS_CHECK_RETURN_CRITICAL
Xiaokang Qian934ce6f2023-02-06 10:23:04 +00002365static int ssl_tls13_finalize_server_hello(mbedtls_ssl_context *ssl)
Jerry Yue110d252022-05-05 10:19:22 +08002366{
2367 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine449bd832023-01-11 14:50:10 +01002368 ret = mbedtls_ssl_tls13_compute_handshake_transform(ssl);
2369 if (ret != 0) {
2370 MBEDTLS_SSL_DEBUG_RET(1,
2371 "mbedtls_ssl_tls13_compute_handshake_transform",
2372 ret);
2373 return ret;
Jerry Yue110d252022-05-05 10:19:22 +08002374 }
2375
Gilles Peskine449bd832023-01-11 14:50:10 +01002376 return ret;
Jerry Yue110d252022-05-05 10:19:22 +08002377}
2378
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002379MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002380static int ssl_tls13_write_server_hello(mbedtls_ssl_context *ssl)
Jerry Yu5b64ae92022-03-30 17:15:02 +08002381{
Jerry Yu637a3f12022-04-20 21:37:58 +08002382 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yuf4b27e42022-03-30 17:32:21 +08002383 unsigned char *buf;
2384 size_t buf_len, msg_len;
2385
Gilles Peskine449bd832023-01-11 14:50:10 +01002386 MBEDTLS_SSL_DEBUG_MSG(2, ("=> write server hello"));
Jerry Yuf4b27e42022-03-30 17:32:21 +08002387
Gilles Peskine449bd832023-01-11 14:50:10 +01002388 MBEDTLS_SSL_PROC_CHK(ssl_tls13_prepare_server_hello(ssl));
Jerry Yuf4b27e42022-03-30 17:32:21 +08002389
Xiaokang Qian9f1747b2023-03-30 02:50:04 +00002390 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_start_handshake_msg(
2391 ssl, MBEDTLS_SSL_HS_SERVER_HELLO, &buf, &buf_len));
Jerry Yuf4b27e42022-03-30 17:32:21 +08002392
Gilles Peskine449bd832023-01-11 14:50:10 +01002393 MBEDTLS_SSL_PROC_CHK(ssl_tls13_write_server_hello_body(ssl, buf,
2394 buf + buf_len,
2395 &msg_len,
2396 0));
Jerry Yuf4b27e42022-03-30 17:32:21 +08002397
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +01002398 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_add_hs_msg_to_checksum(
Manuel Pégourié-Gonnard43cc1272023-02-06 11:48:19 +01002399 ssl, MBEDTLS_SSL_HS_SERVER_HELLO, buf, msg_len));
Jerry Yuf4b27e42022-03-30 17:32:21 +08002400
Gilles Peskine449bd832023-01-11 14:50:10 +01002401 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_finish_handshake_msg(
2402 ssl, buf_len, msg_len));
Jerry Yu637a3f12022-04-20 21:37:58 +08002403
Xiaokang Qian934ce6f2023-02-06 10:23:04 +00002404 MBEDTLS_SSL_PROC_CHK(ssl_tls13_finalize_server_hello(ssl));
Jerry Yue110d252022-05-05 10:19:22 +08002405
Gabor Mezei7b39bf12022-05-24 16:04:14 +02002406#if defined(MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE)
2407 /* The server sends a dummy change_cipher_spec record immediately
2408 * after its first handshake message. This may either be after
2409 * a ServerHello or a HelloRetryRequest.
2410 */
Gabor Mezei96ae9262022-06-28 11:45:18 +02002411 mbedtls_ssl_handshake_set_state(
Gilles Peskine449bd832023-01-11 14:50:10 +01002412 ssl, MBEDTLS_SSL_SERVER_CCS_AFTER_SERVER_HELLO);
Gabor Mezei7b39bf12022-05-24 16:04:14 +02002413#else
Gilles Peskine449bd832023-01-11 14:50:10 +01002414 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_ENCRYPTED_EXTENSIONS);
Gabor Mezei7b39bf12022-05-24 16:04:14 +02002415#endif /* MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE */
Jerry Yue110d252022-05-05 10:19:22 +08002416
Jerry Yuf4b27e42022-03-30 17:32:21 +08002417cleanup:
2418
Gilles Peskine449bd832023-01-11 14:50:10 +01002419 MBEDTLS_SSL_DEBUG_MSG(2, ("<= write server hello"));
2420 return ret;
Jerry Yu5b64ae92022-03-30 17:15:02 +08002421}
2422
Jerry Yu23d1a252022-05-12 18:08:59 +08002423
2424/*
2425 * Handler for MBEDTLS_SSL_HELLO_RETRY_REQUEST
2426 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002427MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002428static int ssl_tls13_prepare_hello_retry_request(mbedtls_ssl_context *ssl)
Jerry Yu23d1a252022-05-12 18:08:59 +08002429{
2430 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Ronald Cron5fbd2702024-02-14 10:03:36 +01002431 if (ssl->handshake->hello_retry_request_flag) {
Gilles Peskine449bd832023-01-11 14:50:10 +01002432 MBEDTLS_SSL_DEBUG_MSG(1, ("Too many HRRs"));
2433 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE,
2434 MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE);
2435 return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
Jerry Yu23d1a252022-05-12 18:08:59 +08002436 }
2437
2438 /*
2439 * Create stateless transcript hash for HRR
2440 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002441 MBEDTLS_SSL_DEBUG_MSG(4, ("Reset transcript for HRR"));
2442 ret = mbedtls_ssl_reset_transcript_for_hrr(ssl);
2443 if (ret != 0) {
2444 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_reset_transcript_for_hrr", ret);
2445 return ret;
Jerry Yu23d1a252022-05-12 18:08:59 +08002446 }
Gilles Peskine449bd832023-01-11 14:50:10 +01002447 mbedtls_ssl_session_reset_msg_layer(ssl, 0);
Jerry Yu23d1a252022-05-12 18:08:59 +08002448
Gilles Peskine449bd832023-01-11 14:50:10 +01002449 return 0;
Jerry Yu23d1a252022-05-12 18:08:59 +08002450}
2451
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002452MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002453static int ssl_tls13_write_hello_retry_request(mbedtls_ssl_context *ssl)
Jerry Yu23d1a252022-05-12 18:08:59 +08002454{
2455 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
2456 unsigned char *buf;
2457 size_t buf_len, msg_len;
2458
Gilles Peskine449bd832023-01-11 14:50:10 +01002459 MBEDTLS_SSL_DEBUG_MSG(2, ("=> write hello retry request"));
Jerry Yu23d1a252022-05-12 18:08:59 +08002460
Gilles Peskine449bd832023-01-11 14:50:10 +01002461 MBEDTLS_SSL_PROC_CHK(ssl_tls13_prepare_hello_retry_request(ssl));
Jerry Yu23d1a252022-05-12 18:08:59 +08002462
Gilles Peskine449bd832023-01-11 14:50:10 +01002463 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_start_handshake_msg(
2464 ssl, MBEDTLS_SSL_HS_SERVER_HELLO,
2465 &buf, &buf_len));
Jerry Yu23d1a252022-05-12 18:08:59 +08002466
Gilles Peskine449bd832023-01-11 14:50:10 +01002467 MBEDTLS_SSL_PROC_CHK(ssl_tls13_write_server_hello_body(ssl, buf,
2468 buf + buf_len,
2469 &msg_len,
2470 1));
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +01002471 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_add_hs_msg_to_checksum(
Manuel Pégourié-Gonnard43cc1272023-02-06 11:48:19 +01002472 ssl, MBEDTLS_SSL_HS_SERVER_HELLO, buf, msg_len));
Jerry Yu23d1a252022-05-12 18:08:59 +08002473
2474
Gilles Peskine449bd832023-01-11 14:50:10 +01002475 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_finish_handshake_msg(ssl, buf_len,
2476 msg_len));
Jerry Yu23d1a252022-05-12 18:08:59 +08002477
Ronald Cron5fbd2702024-02-14 10:03:36 +01002478 ssl->handshake->hello_retry_request_flag = 1;
Jerry Yu23d1a252022-05-12 18:08:59 +08002479
Gabor Mezei7b39bf12022-05-24 16:04:14 +02002480#if defined(MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE)
2481 /* The server sends a dummy change_cipher_spec record immediately
2482 * after its first handshake message. This may either be after
2483 * a ServerHello or a HelloRetryRequest.
2484 */
Gabor Mezei96ae9262022-06-28 11:45:18 +02002485 mbedtls_ssl_handshake_set_state(
Gilles Peskine449bd832023-01-11 14:50:10 +01002486 ssl, MBEDTLS_SSL_SERVER_CCS_AFTER_HELLO_RETRY_REQUEST);
Gabor Mezei7b39bf12022-05-24 16:04:14 +02002487#else
Gilles Peskine449bd832023-01-11 14:50:10 +01002488 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_CLIENT_HELLO);
Gabor Mezei7b39bf12022-05-24 16:04:14 +02002489#endif /* MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE */
Jerry Yu23d1a252022-05-12 18:08:59 +08002490
2491cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +01002492 MBEDTLS_SSL_DEBUG_MSG(2, ("<= write hello retry request"));
2493 return ret;
Jerry Yu23d1a252022-05-12 18:08:59 +08002494}
2495
Jerry Yu5b64ae92022-03-30 17:15:02 +08002496/*
Jerry Yu4d3841a2022-04-16 12:37:19 +08002497 * Handler for MBEDTLS_SSL_ENCRYPTED_EXTENSIONS
2498 */
Jerry Yu4d3841a2022-04-16 12:37:19 +08002499
2500/*
2501 * struct {
2502 * Extension extensions<0..2 ^ 16 - 1>;
2503 * } EncryptedExtensions;
2504 *
2505 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002506MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002507static int ssl_tls13_write_encrypted_extensions_body(mbedtls_ssl_context *ssl,
2508 unsigned char *buf,
2509 unsigned char *end,
2510 size_t *out_len)
Jerry Yu4d3841a2022-04-16 12:37:19 +08002511{
XiaokangQianacb39922022-06-17 10:18:48 +00002512 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yu4d3841a2022-04-16 12:37:19 +08002513 unsigned char *p = buf;
2514 size_t extensions_len = 0;
Jerry Yu8937eb42022-05-03 12:12:14 +08002515 unsigned char *p_extensions_len;
XiaokangQianacb39922022-06-17 10:18:48 +00002516 size_t output_len;
Jerry Yu9da5e5a2022-05-03 15:46:09 +08002517
Jerry Yu4d3841a2022-04-16 12:37:19 +08002518 *out_len = 0;
2519
Gilles Peskine449bd832023-01-11 14:50:10 +01002520 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 2);
Jerry Yu8937eb42022-05-03 12:12:14 +08002521 p_extensions_len = p;
Jerry Yu4d3841a2022-04-16 12:37:19 +08002522 p += 2;
2523
Jerry Yu8937eb42022-05-03 12:12:14 +08002524 ((void) ssl);
XiaokangQianacb39922022-06-17 10:18:48 +00002525 ((void) ret);
2526 ((void) output_len);
2527
2528#if defined(MBEDTLS_SSL_ALPN)
Gilles Peskine449bd832023-01-11 14:50:10 +01002529 ret = mbedtls_ssl_write_alpn_ext(ssl, p, end, &output_len);
2530 if (ret != 0) {
2531 return ret;
2532 }
XiaokangQian95d5f542022-06-24 02:29:26 +00002533 p += output_len;
XiaokangQianacb39922022-06-17 10:18:48 +00002534#endif /* MBEDTLS_SSL_ALPN */
Jerry Yu4d3841a2022-04-16 12:37:19 +08002535
Jerry Yu71c14f12022-12-12 11:10:35 +08002536#if defined(MBEDTLS_SSL_EARLY_DATA)
Ronald Cron78a38f62024-02-01 18:30:31 +01002537 if (ssl->handshake->early_data_accepted) {
Jerry Yu52335392023-11-23 18:06:06 +08002538 ret = mbedtls_ssl_tls13_write_early_data_ext(
Jerry Yuc59c5862023-12-05 10:40:49 +08002539 ssl, 0, p, end, &output_len);
Jerry Yu71c14f12022-12-12 11:10:35 +08002540 if (ret != 0) {
2541 return ret;
2542 }
2543 p += output_len;
2544 }
2545#endif /* MBEDTLS_SSL_EARLY_DATA */
2546
Waleed Elmelegy47d29462024-01-03 17:31:52 +00002547#if defined(MBEDTLS_SSL_RECORD_SIZE_LIMIT)
Waleed Elmelegyfbe42742024-01-05 18:11:10 +00002548 if (ssl->handshake->received_extensions & MBEDTLS_SSL_EXT_MASK(RECORD_SIZE_LIMIT)) {
Waleed Elmelegyd2fc90e2024-01-04 18:04:53 +00002549 ret = mbedtls_ssl_tls13_write_record_size_limit_ext(
2550 ssl, p, end, &output_len);
2551 if (ret != 0) {
2552 return ret;
2553 }
2554 p += output_len;
Waleed Elmelegy47d29462024-01-03 17:31:52 +00002555 }
Waleed Elmelegy47d29462024-01-03 17:31:52 +00002556#endif
2557
Gilles Peskine449bd832023-01-11 14:50:10 +01002558 extensions_len = (p - p_extensions_len) - 2;
2559 MBEDTLS_PUT_UINT16_BE(extensions_len, p_extensions_len, 0);
Jerry Yu8937eb42022-05-03 12:12:14 +08002560
2561 *out_len = p - buf;
Jerry Yu4d3841a2022-04-16 12:37:19 +08002562
Gilles Peskine449bd832023-01-11 14:50:10 +01002563 MBEDTLS_SSL_DEBUG_BUF(4, "encrypted extensions", buf, *out_len);
Jerry Yu4d3841a2022-04-16 12:37:19 +08002564
Jerry Yu7de2ff02022-11-08 21:43:46 +08002565 MBEDTLS_SSL_PRINT_EXTS(
Gilles Peskine449bd832023-01-11 14:50:10 +01002566 3, MBEDTLS_SSL_HS_ENCRYPTED_EXTENSIONS, ssl->handshake->sent_extensions);
Jerry Yu4b8f2f72022-10-31 13:31:22 +08002567
Gilles Peskine449bd832023-01-11 14:50:10 +01002568 return 0;
Jerry Yu4d3841a2022-04-16 12:37:19 +08002569}
2570
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002571MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002572static int ssl_tls13_write_encrypted_extensions(mbedtls_ssl_context *ssl)
Jerry Yu4d3841a2022-04-16 12:37:19 +08002573{
2574 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
2575 unsigned char *buf;
Jerry Yu39730a72022-05-03 12:14:04 +08002576 size_t buf_len, msg_len;
Jerry Yu4d3841a2022-04-16 12:37:19 +08002577
Gilles Peskine449bd832023-01-11 14:50:10 +01002578 mbedtls_ssl_set_outbound_transform(ssl,
2579 ssl->handshake->transform_handshake);
Gabor Mezei54719122022-06-28 11:34:56 +02002580 MBEDTLS_SSL_DEBUG_MSG(
Gilles Peskine449bd832023-01-11 14:50:10 +01002581 3, ("switching to handshake transform for outbound data"));
Gabor Mezei54719122022-06-28 11:34:56 +02002582
Gilles Peskine449bd832023-01-11 14:50:10 +01002583 MBEDTLS_SSL_DEBUG_MSG(2, ("=> write encrypted extensions"));
Jerry Yu4d3841a2022-04-16 12:37:19 +08002584
Xiaokang Qian9f1747b2023-03-30 02:50:04 +00002585 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_start_handshake_msg(
2586 ssl, MBEDTLS_SSL_HS_ENCRYPTED_EXTENSIONS,
2587 &buf, &buf_len));
Jerry Yu4d3841a2022-04-16 12:37:19 +08002588
Gilles Peskine449bd832023-01-11 14:50:10 +01002589 MBEDTLS_SSL_PROC_CHK(ssl_tls13_write_encrypted_extensions_body(
2590 ssl, buf, buf + buf_len, &msg_len));
Jerry Yu4d3841a2022-04-16 12:37:19 +08002591
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +01002592 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_add_hs_msg_to_checksum(
Xiaokang Qian9f1747b2023-03-30 02:50:04 +00002593 ssl, MBEDTLS_SSL_HS_ENCRYPTED_EXTENSIONS,
2594 buf, msg_len));
Jerry Yu4d3841a2022-04-16 12:37:19 +08002595
Gilles Peskine449bd832023-01-11 14:50:10 +01002596 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_finish_handshake_msg(
2597 ssl, buf_len, msg_len));
Jerry Yu4d3841a2022-04-16 12:37:19 +08002598
Ronald Cron928cbd32022-10-04 16:14:26 +02002599#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01002600 if (mbedtls_ssl_tls13_key_exchange_mode_with_psk(ssl)) {
2601 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_SERVER_FINISHED);
2602 } else {
2603 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_CERTIFICATE_REQUEST);
2604 }
Jerry Yu8937eb42022-05-03 12:12:14 +08002605#else
Gilles Peskine449bd832023-01-11 14:50:10 +01002606 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_SERVER_FINISHED);
Jerry Yu8937eb42022-05-03 12:12:14 +08002607#endif
2608
Jerry Yu4d3841a2022-04-16 12:37:19 +08002609cleanup:
2610
Gilles Peskine449bd832023-01-11 14:50:10 +01002611 MBEDTLS_SSL_DEBUG_MSG(2, ("<= write encrypted extensions"));
2612 return ret;
Jerry Yu4d3841a2022-04-16 12:37:19 +08002613}
2614
Ronald Cron928cbd32022-10-04 16:14:26 +02002615#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED)
XiaokangQiana987e1d2022-05-07 01:25:58 +00002616#define SSL_CERTIFICATE_REQUEST_SEND_REQUEST 0
2617#define SSL_CERTIFICATE_REQUEST_SKIP 1
2618/* Coordination:
2619 * Check whether a CertificateRequest message should be written.
2620 * Returns a negative code on failure, or
2621 * - SSL_CERTIFICATE_REQUEST_SEND_REQUEST
2622 * - SSL_CERTIFICATE_REQUEST_SKIP
2623 * indicating if the writing of the CertificateRequest
2624 * should be skipped or not.
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_certificate_request_coordinate(mbedtls_ssl_context *ssl)
XiaokangQiana987e1d2022-05-07 01:25:58 +00002628{
2629 int authmode;
2630
XiaokangQian40a35232022-05-07 09:02:40 +00002631#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Gilles Peskine449bd832023-01-11 14:50:10 +01002632 if (ssl->handshake->sni_authmode != MBEDTLS_SSL_VERIFY_UNSET) {
XiaokangQian40a35232022-05-07 09:02:40 +00002633 authmode = ssl->handshake->sni_authmode;
Gilles Peskine449bd832023-01-11 14:50:10 +01002634 } else
XiaokangQian40a35232022-05-07 09:02:40 +00002635#endif
XiaokangQiana987e1d2022-05-07 01:25:58 +00002636 authmode = ssl->conf->authmode;
2637
Gilles Peskine449bd832023-01-11 14:50:10 +01002638 if (authmode == MBEDTLS_SSL_VERIFY_NONE) {
Ronald Croneac00ad2022-09-13 10:16:31 +02002639 ssl->session_negotiate->verify_result = MBEDTLS_X509_BADCERT_SKIP_VERIFY;
Gilles Peskine449bd832023-01-11 14:50:10 +01002640 return SSL_CERTIFICATE_REQUEST_SKIP;
Ronald Croneac00ad2022-09-13 10:16:31 +02002641 }
XiaokangQiana987e1d2022-05-07 01:25:58 +00002642
XiaokangQianc3017f62022-05-13 05:55:41 +00002643 ssl->handshake->certificate_request_sent = 1;
XiaokangQian189ded22022-05-10 08:12:17 +00002644
Gilles Peskine449bd832023-01-11 14:50:10 +01002645 return SSL_CERTIFICATE_REQUEST_SEND_REQUEST;
XiaokangQiana987e1d2022-05-07 01:25:58 +00002646}
2647
XiaokangQiancec9ae62022-05-06 07:28:50 +00002648/*
2649 * struct {
2650 * opaque certificate_request_context<0..2^8-1>;
2651 * Extension extensions<2..2^16-1>;
2652 * } CertificateRequest;
2653 *
2654 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002655MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002656static int ssl_tls13_write_certificate_request_body(mbedtls_ssl_context *ssl,
2657 unsigned char *buf,
2658 const unsigned char *end,
2659 size_t *out_len)
XiaokangQiancec9ae62022-05-06 07:28:50 +00002660{
2661 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
2662 unsigned char *p = buf;
XiaokangQianec6efb92022-05-06 09:53:10 +00002663 size_t output_len = 0;
XiaokangQiancec9ae62022-05-06 07:28:50 +00002664 unsigned char *p_extensions_len;
2665
2666 *out_len = 0;
2667
2668 /* Check if we have enough space:
2669 * - certificate_request_context (1 byte)
2670 * - extensions length (2 bytes)
2671 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002672 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 3);
XiaokangQiancec9ae62022-05-06 07:28:50 +00002673
2674 /*
2675 * Write certificate_request_context
2676 */
2677 /*
2678 * We use a zero length context for the normal handshake
2679 * messages. For post-authentication handshake messages
2680 * this request context would be set to a non-zero value.
2681 */
2682 *p++ = 0x0;
2683
2684 /*
2685 * Write extensions
2686 */
2687 /* The extensions must contain the signature_algorithms. */
2688 p_extensions_len = p;
2689 p += 2;
Gilles Peskine449bd832023-01-11 14:50:10 +01002690 ret = mbedtls_ssl_write_sig_alg_ext(ssl, p, end, &output_len);
2691 if (ret != 0) {
2692 return ret;
2693 }
XiaokangQiancec9ae62022-05-06 07:28:50 +00002694
XiaokangQianec6efb92022-05-06 09:53:10 +00002695 p += output_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01002696 MBEDTLS_PUT_UINT16_BE(p - p_extensions_len - 2, p_extensions_len, 0);
XiaokangQiancec9ae62022-05-06 07:28:50 +00002697
2698 *out_len = p - buf;
2699
Jerry Yu7de2ff02022-11-08 21:43:46 +08002700 MBEDTLS_SSL_PRINT_EXTS(
Gilles Peskine449bd832023-01-11 14:50:10 +01002701 3, MBEDTLS_SSL_HS_CERTIFICATE_REQUEST, ssl->handshake->sent_extensions);
Jerry Yu4b8f2f72022-10-31 13:31:22 +08002702
Gilles Peskine449bd832023-01-11 14:50:10 +01002703 return 0;
XiaokangQiancec9ae62022-05-06 07:28:50 +00002704}
2705
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002706MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002707static int ssl_tls13_write_certificate_request(mbedtls_ssl_context *ssl)
XiaokangQiancec9ae62022-05-06 07:28:50 +00002708{
2709 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
2710
Gilles Peskine449bd832023-01-11 14:50:10 +01002711 MBEDTLS_SSL_DEBUG_MSG(2, ("=> write certificate request"));
XiaokangQiancec9ae62022-05-06 07:28:50 +00002712
Gilles Peskine449bd832023-01-11 14:50:10 +01002713 MBEDTLS_SSL_PROC_CHK_NEG(ssl_tls13_certificate_request_coordinate(ssl));
XiaokangQiancec9ae62022-05-06 07:28:50 +00002714
Gilles Peskine449bd832023-01-11 14:50:10 +01002715 if (ret == SSL_CERTIFICATE_REQUEST_SEND_REQUEST) {
XiaokangQiancec9ae62022-05-06 07:28:50 +00002716 unsigned char *buf;
2717 size_t buf_len, msg_len;
2718
Xiaokang Qian9f1747b2023-03-30 02:50:04 +00002719 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_start_handshake_msg(
2720 ssl, MBEDTLS_SSL_HS_CERTIFICATE_REQUEST,
2721 &buf, &buf_len));
XiaokangQiancec9ae62022-05-06 07:28:50 +00002722
Gilles Peskine449bd832023-01-11 14:50:10 +01002723 MBEDTLS_SSL_PROC_CHK(ssl_tls13_write_certificate_request_body(
2724 ssl, buf, buf + buf_len, &msg_len));
XiaokangQiancec9ae62022-05-06 07:28:50 +00002725
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +01002726 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_add_hs_msg_to_checksum(
Xiaokang Qian9f1747b2023-03-30 02:50:04 +00002727 ssl, MBEDTLS_SSL_HS_CERTIFICATE_REQUEST,
2728 buf, msg_len));
XiaokangQiancec9ae62022-05-06 07:28:50 +00002729
Gilles Peskine449bd832023-01-11 14:50:10 +01002730 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_finish_handshake_msg(
2731 ssl, buf_len, msg_len));
2732 } else if (ret == SSL_CERTIFICATE_REQUEST_SKIP) {
2733 MBEDTLS_SSL_DEBUG_MSG(2, ("<= skip write certificate request"));
XiaokangQiancec9ae62022-05-06 07:28:50 +00002734 ret = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01002735 } else {
2736 MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
XiaokangQiancec9ae62022-05-06 07:28:50 +00002737 ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
2738 goto cleanup;
2739 }
2740
Gilles Peskine449bd832023-01-11 14:50:10 +01002741 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_SERVER_CERTIFICATE);
XiaokangQiancec9ae62022-05-06 07:28:50 +00002742cleanup:
2743
Gilles Peskine449bd832023-01-11 14:50:10 +01002744 MBEDTLS_SSL_DEBUG_MSG(2, ("<= write certificate request"));
2745 return ret;
XiaokangQiancec9ae62022-05-06 07:28:50 +00002746}
XiaokangQiancec9ae62022-05-06 07:28:50 +00002747
Jerry Yu4d3841a2022-04-16 12:37:19 +08002748/*
Jerry Yuc6e6dbf2022-04-16 19:42:57 +08002749 * Handler for MBEDTLS_SSL_SERVER_CERTIFICATE
Jerry Yu83da34e2022-04-16 13:59:52 +08002750 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002751MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002752static int ssl_tls13_write_server_certificate(mbedtls_ssl_context *ssl)
Jerry Yu83da34e2022-04-16 13:59:52 +08002753{
Jerry Yu5a26f302022-05-10 20:46:40 +08002754 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
XiaokangQianfb665a82022-06-15 03:57:21 +00002755
2756#if defined(MBEDTLS_X509_CRT_PARSE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01002757 if ((ssl_tls13_pick_key_cert(ssl) != 0) ||
2758 mbedtls_ssl_own_cert(ssl) == NULL) {
2759 MBEDTLS_SSL_DEBUG_MSG(2, ("No certificate available."));
2760 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE,
2761 MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE);
2762 return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
Jerry Yu5a26f302022-05-10 20:46:40 +08002763 }
XiaokangQianfb665a82022-06-15 03:57:21 +00002764#endif /* MBEDTLS_X509_CRT_PARSE_C */
Jerry Yu5a26f302022-05-10 20:46:40 +08002765
Gilles Peskine449bd832023-01-11 14:50:10 +01002766 ret = mbedtls_ssl_tls13_write_certificate(ssl);
2767 if (ret != 0) {
2768 return ret;
2769 }
2770 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_CERTIFICATE_VERIFY);
2771 return 0;
Jerry Yu83da34e2022-04-16 13:59:52 +08002772}
2773
2774/*
Jerry Yuc6e6dbf2022-04-16 19:42:57 +08002775 * Handler for MBEDTLS_SSL_CERTIFICATE_VERIFY
Jerry Yu83da34e2022-04-16 13:59:52 +08002776 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002777MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002778static int ssl_tls13_write_certificate_verify(mbedtls_ssl_context *ssl)
Jerry Yu83da34e2022-04-16 13:59:52 +08002779{
Gilles Peskine449bd832023-01-11 14:50:10 +01002780 int ret = mbedtls_ssl_tls13_write_certificate_verify(ssl);
2781 if (ret != 0) {
2782 return ret;
2783 }
2784 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_SERVER_FINISHED);
2785 return 0;
Jerry Yu83da34e2022-04-16 13:59:52 +08002786}
Ronald Cron928cbd32022-10-04 16:14:26 +02002787#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED */
Jerry Yu83da34e2022-04-16 13:59:52 +08002788
Jerry Yu9b72e392023-12-01 16:27:08 +08002789/*
2790 * RFC 8446 section A.2
2791 *
2792 * | Send ServerHello
2793 * | K_send = handshake
2794 * | Send EncryptedExtensions
2795 * | [Send CertificateRequest]
2796 * Can send | [Send Certificate + CertificateVerify]
2797 * app data | Send Finished
2798 * after --> | K_send = application
2799 * here +--------+--------+
2800 * No 0-RTT | | 0-RTT
2801 * | |
2802 * K_recv = handshake | | K_recv = early data
2803 * [Skip decrypt errors] | +------> WAIT_EOED -+
2804 * | | Recv | | Recv EndOfEarlyData
2805 * | | early data | | K_recv = handshake
2806 * | +------------+ |
2807 * | |
2808 * +> WAIT_FLIGHT2 <--------+
2809 * |
2810 * +--------+--------+
2811 * No auth | | Client auth
2812 * | |
2813 * | v
2814 * | WAIT_CERT
2815 * | Recv | | Recv Certificate
2816 * | empty | v
2817 * | Certificate | WAIT_CV
2818 * | | | Recv
2819 * | v | CertificateVerify
2820 * +-> WAIT_FINISHED <---+
2821 * | Recv Finished
2822 *
2823 *
2824 * The following function handles the state changes after WAIT_FLIGHT2 in the
Jerry Yu3be85072023-12-04 09:58:54 +08002825 * above diagram. We are not going to receive early data related messages
2826 * anymore, prepare to receive the first handshake message of the client
2827 * second flight.
Jerry Yu9b72e392023-12-01 16:27:08 +08002828 */
Jerry Yu3be85072023-12-04 09:58:54 +08002829static void ssl_tls13_prepare_for_handshake_second_flight(
2830 mbedtls_ssl_context *ssl)
Jerry Yu9b72e392023-12-01 16:27:08 +08002831{
Jerry Yu9b72e392023-12-01 16:27:08 +08002832 if (ssl->handshake->certificate_request_sent) {
2833 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_CLIENT_CERTIFICATE);
2834 } else {
Jerry Yu42020fb2023-12-05 17:35:53 +08002835 MBEDTLS_SSL_DEBUG_MSG(2, ("skip parse certificate"));
Jerry Yuebb1b1d2023-12-05 11:02:15 +08002836 MBEDTLS_SSL_DEBUG_MSG(2, ("skip parse certificate verify"));
Jerry Yuebb1b1d2023-12-05 11:02:15 +08002837
Jerry Yu9b72e392023-12-01 16:27:08 +08002838 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_CLIENT_FINISHED);
2839 }
Jerry Yu9b72e392023-12-01 16:27:08 +08002840}
2841
Jerry Yu83da34e2022-04-16 13:59:52 +08002842/*
Jerry Yud6e253d2022-05-18 13:59:24 +08002843 * Handler for MBEDTLS_SSL_SERVER_FINISHED
Jerry Yu69dd8d42022-04-16 12:51:26 +08002844 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002845MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002846static int ssl_tls13_write_server_finished(mbedtls_ssl_context *ssl)
Jerry Yu69dd8d42022-04-16 12:51:26 +08002847{
Jerry Yud6e253d2022-05-18 13:59:24 +08002848 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yu27bdc7c2022-04-16 13:33:27 +08002849
Gilles Peskine449bd832023-01-11 14:50:10 +01002850 ret = mbedtls_ssl_tls13_write_finished_message(ssl);
2851 if (ret != 0) {
2852 return ret;
2853 }
Jerry Yu27bdc7c2022-04-16 13:33:27 +08002854
Gilles Peskine449bd832023-01-11 14:50:10 +01002855 ret = mbedtls_ssl_tls13_compute_application_transform(ssl);
2856 if (ret != 0) {
Jerry Yue3d67cb2022-05-19 15:33:10 +08002857 MBEDTLS_SSL_PEND_FATAL_ALERT(
Gilles Peskine449bd832023-01-11 14:50:10 +01002858 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE,
2859 MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE);
2860 return ret;
Jerry Yue3d67cb2022-05-19 15:33:10 +08002861 }
XiaokangQianc3017f62022-05-13 05:55:41 +00002862
Jerry Yu87b5ed42022-12-12 13:07:07 +08002863#if defined(MBEDTLS_SSL_EARLY_DATA)
Ronald Cron78a38f62024-02-01 18:30:31 +01002864 if (ssl->handshake->early_data_accepted) {
Jerry Yu0af63dc2023-12-01 17:14:51 +08002865 /* See RFC 8446 section A.2 for more information */
Jerry Yu87b5ed42022-12-12 13:07:07 +08002866 MBEDTLS_SSL_DEBUG_MSG(
2867 1, ("Switch to early keys for inbound traffic. "
2868 "( K_recv = early data )"));
2869 mbedtls_ssl_set_inbound_transform(
2870 ssl, ssl->handshake->transform_earlydata);
2871 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_END_OF_EARLY_DATA);
2872 return 0;
2873 }
2874#endif /* MBEDTLS_SSL_EARLY_DATA */
Jerry Yu0af63dc2023-12-01 17:14:51 +08002875 MBEDTLS_SSL_DEBUG_MSG(
2876 1, ("Switch to handshake keys for inbound traffic "
2877 "( K_recv = handshake )"));
Gilles Peskine449bd832023-01-11 14:50:10 +01002878 mbedtls_ssl_set_inbound_transform(ssl, ssl->handshake->transform_handshake);
XiaokangQian189ded22022-05-10 08:12:17 +00002879
Jerry Yu3be85072023-12-04 09:58:54 +08002880 ssl_tls13_prepare_for_handshake_second_flight(ssl);
Jerry Yu7d8c3fe2022-12-12 12:59:44 +08002881
2882 return 0;
2883}
2884
Jerry Yu87b5ed42022-12-12 13:07:07 +08002885#if defined(MBEDTLS_SSL_EARLY_DATA)
2886/*
Jerry Yu59d420f2023-12-01 16:30:34 +08002887 * Handler for MBEDTLS_SSL_END_OF_EARLY_DATA
Jerry Yu87b5ed42022-12-12 13:07:07 +08002888 */
Jerry Yu3be85072023-12-04 09:58:54 +08002889#define SSL_GOT_END_OF_EARLY_DATA 0
Jerry Yub55f9eb2023-12-05 10:27:17 +08002890#define SSL_GOT_EARLY_DATA 1
Jerry Yud5c34962023-12-01 16:32:31 +08002891/* Coordination:
Jerry Yu3be85072023-12-04 09:58:54 +08002892 * Deals with the ambiguity of not knowing if the next message is an
2893 * EndOfEarlyData message or an application message containing early data.
Jerry Yud5c34962023-12-01 16:32:31 +08002894 * Returns a negative code on failure, or
Jerry Yu3be85072023-12-04 09:58:54 +08002895 * - SSL_GOT_END_OF_EARLY_DATA
Jerry Yub55f9eb2023-12-05 10:27:17 +08002896 * - SSL_GOT_EARLY_DATA
Jerry Yud5c34962023-12-01 16:32:31 +08002897 * indicating which message is received.
2898 */
2899MBEDTLS_CHECK_RETURN_CRITICAL
2900static int ssl_tls13_end_of_early_data_coordinate(mbedtls_ssl_context *ssl)
2901{
2902 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yub4ed4602023-12-01 16:34:00 +08002903
2904 if ((ret = mbedtls_ssl_read_record(ssl, 0)) != 0) {
2905 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_read_record", ret);
2906 return ret;
2907 }
2908 ssl->keep_current_message = 1;
2909
2910 if (ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
2911 ssl->in_msg[0] == MBEDTLS_SSL_HS_END_OF_EARLY_DATA) {
Jerry Yub55f9eb2023-12-05 10:27:17 +08002912 MBEDTLS_SSL_DEBUG_MSG(3, ("Received an end_of_early_data message."));
Jerry Yu3be85072023-12-04 09:58:54 +08002913 return SSL_GOT_END_OF_EARLY_DATA;
Jerry Yub4ed4602023-12-01 16:34:00 +08002914 }
2915
2916 if (ssl->in_msgtype == MBEDTLS_SSL_MSG_APPLICATION_DATA) {
Jerry Yub55f9eb2023-12-05 10:27:17 +08002917 MBEDTLS_SSL_DEBUG_MSG(3, ("Received early data"));
Jerry Yu6a5904d2023-12-06 17:11:12 +08002918 /* RFC 8446 section 4.6.1
2919 *
2920 * A server receiving more than max_early_data_size bytes of 0-RTT data
2921 * SHOULD terminate the connection with an "unexpected_message" alert.
2922 *
2923 * TODO: Add received data size check here.
2924 */
Ronald Croned7d4bf2024-01-31 07:55:19 +01002925 if (ssl->in_offt == NULL) {
2926 /* Set the reading pointer */
2927 ssl->in_offt = ssl->in_msg;
2928 }
Jerry Yub55f9eb2023-12-05 10:27:17 +08002929 return SSL_GOT_EARLY_DATA;
Jerry Yub4ed4602023-12-01 16:34:00 +08002930 }
2931
Jerry Yu7bb40a32023-12-04 10:04:15 +08002932 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE,
2933 MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE);
Jerry Yub4ed4602023-12-01 16:34:00 +08002934 return MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE;
Jerry Yud5c34962023-12-01 16:32:31 +08002935}
2936
2937MBEDTLS_CHECK_RETURN_CRITICAL
2938static int ssl_tls13_parse_end_of_early_data(mbedtls_ssl_context *ssl,
2939 const unsigned char *buf,
2940 const unsigned char *end)
2941{
Jerry Yu75c9ab72023-12-01 16:41:40 +08002942 /* RFC 8446 section 4.5
2943 *
2944 * struct {} EndOfEarlyData;
2945 */
Jerry Yufbf03992023-12-04 10:00:37 +08002946 if (buf != end) {
2947 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR,
2948 MBEDTLS_ERR_SSL_DECODE_ERROR);
2949 return MBEDTLS_ERR_SSL_DECODE_ERROR;
2950 }
2951 return 0;
Jerry Yud5c34962023-12-01 16:32:31 +08002952}
2953
Jerry Yud5c34962023-12-01 16:32:31 +08002954/*
2955 * RFC 8446 section A.2
2956 *
2957 * | Send ServerHello
2958 * | K_send = handshake
2959 * | Send EncryptedExtensions
2960 * | [Send CertificateRequest]
2961 * Can send | [Send Certificate + CertificateVerify]
2962 * app data | Send Finished
2963 * after --> | K_send = application
2964 * here +--------+--------+
2965 * No 0-RTT | | 0-RTT
2966 * | |
2967 * K_recv = handshake | | K_recv = early data
2968 * [Skip decrypt errors] | +------> WAIT_EOED -+
2969 * | | Recv | | Recv EndOfEarlyData
2970 * | | early data | | K_recv = handshake
2971 * | +------------+ |
2972 * | |
2973 * +> WAIT_FLIGHT2 <--------+
2974 * |
2975 * +--------+--------+
2976 * No auth | | Client auth
2977 * | |
2978 * | v
2979 * | WAIT_CERT
2980 * | Recv | | Recv Certificate
2981 * | empty | v
2982 * | Certificate | WAIT_CV
2983 * | | | Recv
2984 * | v | CertificateVerify
2985 * +-> WAIT_FINISHED <---+
2986 * | Recv Finished
2987 *
2988 * The function handles actions and state changes from 0-RTT to WAIT_FLIGHT2 in
2989 * the above diagram.
2990 */
Jerry Yu87b5ed42022-12-12 13:07:07 +08002991MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yu59d420f2023-12-01 16:30:34 +08002992static int ssl_tls13_process_end_of_early_data(mbedtls_ssl_context *ssl)
Jerry Yu87b5ed42022-12-12 13:07:07 +08002993{
2994 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yud5c34962023-12-01 16:32:31 +08002995
2996 MBEDTLS_SSL_DEBUG_MSG(2, ("=> ssl_tls13_process_end_of_early_data"));
2997
2998 MBEDTLS_SSL_PROC_CHK_NEG(ssl_tls13_end_of_early_data_coordinate(ssl));
2999
Jerry Yu3be85072023-12-04 09:58:54 +08003000 if (ret == SSL_GOT_END_OF_EARLY_DATA) {
Jerry Yud5c34962023-12-01 16:32:31 +08003001 unsigned char *buf;
3002 size_t buf_len;
3003
3004 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_tls13_fetch_handshake_msg(
3005 ssl, MBEDTLS_SSL_HS_END_OF_EARLY_DATA,
3006 &buf, &buf_len));
3007
3008 MBEDTLS_SSL_PROC_CHK(ssl_tls13_parse_end_of_early_data(
3009 ssl, buf, buf + buf_len));
3010
Jerry Yue9655122023-12-01 16:44:40 +08003011 MBEDTLS_SSL_DEBUG_MSG(
3012 1, ("Switch to handshake keys for inbound traffic"
3013 "( K_recv = handshake )"));
3014 mbedtls_ssl_set_inbound_transform(
3015 ssl, ssl->handshake->transform_handshake);
3016
Jerry Yud5c34962023-12-01 16:32:31 +08003017 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_add_hs_msg_to_checksum(
3018 ssl, MBEDTLS_SSL_HS_END_OF_EARLY_DATA,
3019 buf, buf_len));
Jerry Yue9655122023-12-01 16:44:40 +08003020
Jerry Yu3be85072023-12-04 09:58:54 +08003021 ssl_tls13_prepare_for_handshake_second_flight(ssl);
Jerry Yud5c34962023-12-01 16:32:31 +08003022
Jerry Yub55f9eb2023-12-05 10:27:17 +08003023 } else if (ret == SSL_GOT_EARLY_DATA) {
Jerry Yud9ca3542023-12-06 17:23:52 +08003024 ret = MBEDTLS_ERR_SSL_RECEIVED_EARLY_DATA;
3025 goto cleanup;
Jerry Yud5c34962023-12-01 16:32:31 +08003026 } else {
3027 MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
3028 ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
3029 goto cleanup;
3030 }
3031
Jerry Yud5c34962023-12-01 16:32:31 +08003032cleanup:
3033 MBEDTLS_SSL_DEBUG_MSG(2, ("<= ssl_tls13_process_end_of_early_data"));
Jerry Yu87b5ed42022-12-12 13:07:07 +08003034 return ret;
3035}
3036#endif /* MBEDTLS_SSL_EARLY_DATA */
3037
Jerry Yu69dd8d42022-04-16 12:51:26 +08003038/*
Jerry Yud6e253d2022-05-18 13:59:24 +08003039 * Handler for MBEDTLS_SSL_CLIENT_FINISHED
Jerry Yu69dd8d42022-04-16 12:51:26 +08003040 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02003041MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01003042static int ssl_tls13_process_client_finished(mbedtls_ssl_context *ssl)
Jerry Yu69dd8d42022-04-16 12:51:26 +08003043{
Jerry Yud6e253d2022-05-18 13:59:24 +08003044 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
3045
Gilles Peskine449bd832023-01-11 14:50:10 +01003046 ret = mbedtls_ssl_tls13_process_finished_message(ssl);
3047 if (ret != 0) {
3048 return ret;
Jerry Yue3d67cb2022-05-19 15:33:10 +08003049 }
3050
Gilles Peskine449bd832023-01-11 14:50:10 +01003051 ret = mbedtls_ssl_tls13_compute_resumption_master_secret(ssl);
3052 if (ret != 0) {
Xiaokang Qian9f1747b2023-03-30 02:50:04 +00003053 MBEDTLS_SSL_DEBUG_RET(
3054 1, "mbedtls_ssl_tls13_compute_resumption_master_secret", ret);
Gilles Peskine449bd832023-01-11 14:50:10 +01003055 }
3056
3057 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_HANDSHAKE_WRAPUP);
3058 return 0;
Jerry Yu69dd8d42022-04-16 12:51:26 +08003059}
3060
3061/*
Jerry Yud6e253d2022-05-18 13:59:24 +08003062 * Handler for MBEDTLS_SSL_HANDSHAKE_WRAPUP
Jerry Yu69dd8d42022-04-16 12:51:26 +08003063 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02003064MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01003065static int ssl_tls13_handshake_wrapup(mbedtls_ssl_context *ssl)
Jerry Yu69dd8d42022-04-16 12:51:26 +08003066{
Gilles Peskine449bd832023-01-11 14:50:10 +01003067 MBEDTLS_SSL_DEBUG_MSG(2, ("handshake: done"));
Jerry Yu03ed50b2022-04-16 17:13:30 +08003068
Gilles Peskine449bd832023-01-11 14:50:10 +01003069 mbedtls_ssl_tls13_handshake_wrapup(ssl);
Jerry Yue67bef42022-07-07 07:29:42 +00003070
Pengyu Lv3643fdb2023-01-12 16:46:28 +08003071#if defined(MBEDTLS_SSL_SESSION_TICKETS) && \
3072 defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED)
Pengyu Lva1aa31b2022-12-13 13:49:59 +08003073/* TODO: Remove the check of SOME_PSK_ENABLED since SESSION_TICKETS requires
3074 * SOME_PSK_ENABLED to be enabled. Here is just to make CI happy. It is
3075 * expected to be resolved with issue#6395.
3076 */
Pengyu Lvc7af2c42022-12-01 16:33:00 +08003077 /* Sent NewSessionTicket message only when client supports PSK */
Pengyu Lvb2cfafb2023-11-14 13:56:13 +08003078 if (mbedtls_ssl_tls13_is_some_psk_supported(ssl)) {
Xiaokang Qian9f1747b2023-03-30 02:50:04 +00003079 mbedtls_ssl_handshake_set_state(
3080 ssl, MBEDTLS_SSL_TLS1_3_NEW_SESSION_TICKET);
Pengyu Lvc7af2c42022-12-01 16:33:00 +08003081 } else
Jerry Yufca4d572022-07-21 10:37:48 +08003082#endif
Pengyu Lv3643fdb2023-01-12 16:46:28 +08003083 {
3084 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_HANDSHAKE_OVER);
3085 }
Gilles Peskine449bd832023-01-11 14:50:10 +01003086 return 0;
Jerry Yu69dd8d42022-04-16 12:51:26 +08003087}
3088
3089/*
Jerry Yua8d3c502022-10-30 14:51:23 +08003090 * Handler for MBEDTLS_SSL_TLS1_3_NEW_SESSION_TICKET
Jerry Yue67bef42022-07-07 07:29:42 +00003091 */
3092#define SSL_NEW_SESSION_TICKET_SKIP 0
3093#define SSL_NEW_SESSION_TICKET_WRITE 1
3094MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01003095static int ssl_tls13_write_new_session_ticket_coordinate(mbedtls_ssl_context *ssl)
Jerry Yue67bef42022-07-07 07:29:42 +00003096{
3097 /* Check whether the use of session tickets is enabled */
Gilles Peskine449bd832023-01-11 14:50:10 +01003098 if (ssl->conf->f_ticket_write == NULL) {
3099 MBEDTLS_SSL_DEBUG_MSG(2, ("NewSessionTicket: disabled,"
3100 " callback is not set"));
3101 return SSL_NEW_SESSION_TICKET_SKIP;
Jerry Yud0766ec2022-09-22 10:46:57 +08003102 }
Gilles Peskine449bd832023-01-11 14:50:10 +01003103 if (ssl->conf->new_session_tickets_count == 0) {
3104 MBEDTLS_SSL_DEBUG_MSG(2, ("NewSessionTicket: disabled,"
3105 " configured count is zero"));
3106 return SSL_NEW_SESSION_TICKET_SKIP;
Jerry Yud0766ec2022-09-22 10:46:57 +08003107 }
3108
Gilles Peskine449bd832023-01-11 14:50:10 +01003109 if (ssl->handshake->new_session_tickets_count == 0) {
3110 MBEDTLS_SSL_DEBUG_MSG(2, ("NewSessionTicket: all tickets have "
3111 "been sent."));
3112 return SSL_NEW_SESSION_TICKET_SKIP;
Jerry Yue67bef42022-07-07 07:29:42 +00003113 }
3114
Gilles Peskine449bd832023-01-11 14:50:10 +01003115 return SSL_NEW_SESSION_TICKET_WRITE;
Jerry Yue67bef42022-07-07 07:29:42 +00003116}
3117
3118#if defined(MBEDTLS_SSL_SESSION_TICKETS)
3119MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01003120static int ssl_tls13_prepare_new_session_ticket(mbedtls_ssl_context *ssl,
3121 unsigned char *ticket_nonce,
3122 size_t ticket_nonce_size)
Jerry Yue67bef42022-07-07 07:29:42 +00003123{
3124 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
3125 mbedtls_ssl_session *session = ssl->session;
3126 mbedtls_ssl_ciphersuite_t *ciphersuite_info;
3127 psa_algorithm_t psa_hash_alg;
3128 int hash_length;
3129
Gilles Peskine449bd832023-01-11 14:50:10 +01003130 MBEDTLS_SSL_DEBUG_MSG(2, ("=> prepare NewSessionTicket msg"));
Jerry Yue67bef42022-07-07 07:29:42 +00003131
Pengyu Lv9f926952022-11-17 15:22:33 +08003132 /* Set ticket_flags depends on the advertised psk key exchange mode */
Pengyu Lv94a42cc2023-12-06 10:04:17 +08003133 mbedtls_ssl_tls13_session_clear_ticket_flags(
Pengyu Lv9eacb442022-12-09 14:39:19 +08003134 session, MBEDTLS_SSL_TLS1_3_TICKET_FLAGS_MASK);
Pengyu Lve6487fe2022-12-06 09:30:29 +08003135#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED)
Pengyu Lv94a42cc2023-12-06 10:04:17 +08003136 mbedtls_ssl_tls13_session_set_ticket_flags(
Pengyu Lv9eacb442022-12-09 14:39:19 +08003137 session, ssl->handshake->tls13_kex_modes);
Pengyu Lve6487fe2022-12-06 09:30:29 +08003138#endif
Jerry Yu95648b02023-12-06 15:03:34 +08003139
3140#if defined(MBEDTLS_SSL_EARLY_DATA)
3141 if (ssl->conf->early_data_enabled == MBEDTLS_SSL_EARLY_DATA_ENABLED &&
3142 ssl->conf->max_early_data_size > 0) {
Pengyu Lv94a42cc2023-12-06 10:04:17 +08003143 mbedtls_ssl_tls13_session_set_ticket_flags(
Jerry Yu95648b02023-12-06 15:03:34 +08003144 session, MBEDTLS_SSL_TLS1_3_TICKET_ALLOW_EARLY_DATA);
3145 }
3146#endif /* MBEDTLS_SSL_EARLY_DATA */
3147
Pengyu Lv4938a562023-01-16 11:28:49 +08003148 MBEDTLS_SSL_PRINT_TICKET_FLAGS(4, session->ticket_flags);
Pengyu Lv9f926952022-11-17 15:22:33 +08003149
Jerry Yue67bef42022-07-07 07:29:42 +00003150 /* Generate ticket_age_add */
Gilles Peskine449bd832023-01-11 14:50:10 +01003151 if ((ret = ssl->conf->f_rng(ssl->conf->p_rng,
3152 (unsigned char *) &session->ticket_age_add,
3153 sizeof(session->ticket_age_add)) != 0)) {
3154 MBEDTLS_SSL_DEBUG_RET(1, "generate_ticket_age_add", ret);
3155 return ret;
Jerry Yue67bef42022-07-07 07:29:42 +00003156 }
Gilles Peskine449bd832023-01-11 14:50:10 +01003157 MBEDTLS_SSL_DEBUG_MSG(3, ("ticket_age_add: %u",
3158 (unsigned int) session->ticket_age_add));
Jerry Yue67bef42022-07-07 07:29:42 +00003159
3160 /* Generate ticket_nonce */
Gilles Peskine449bd832023-01-11 14:50:10 +01003161 ret = ssl->conf->f_rng(ssl->conf->p_rng, ticket_nonce, ticket_nonce_size);
3162 if (ret != 0) {
3163 MBEDTLS_SSL_DEBUG_RET(1, "generate_ticket_nonce", ret);
3164 return ret;
Jerry Yue67bef42022-07-07 07:29:42 +00003165 }
Gilles Peskine449bd832023-01-11 14:50:10 +01003166 MBEDTLS_SSL_DEBUG_BUF(3, "ticket_nonce:",
3167 ticket_nonce, ticket_nonce_size);
Jerry Yue67bef42022-07-07 07:29:42 +00003168
3169 ciphersuite_info =
Gilles Peskine449bd832023-01-11 14:50:10 +01003170 (mbedtls_ssl_ciphersuite_t *) ssl->handshake->ciphersuite_info;
Dave Rodgman2eab4622023-10-05 13:30:37 +01003171 psa_hash_alg = mbedtls_md_psa_alg_from_type((mbedtls_md_type_t) ciphersuite_info->mac);
Gilles Peskine449bd832023-01-11 14:50:10 +01003172 hash_length = PSA_HASH_LENGTH(psa_hash_alg);
3173 if (hash_length == -1 ||
3174 (size_t) hash_length > sizeof(session->resumption_key)) {
3175 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Jerry Yufca4d572022-07-21 10:37:48 +08003176 }
3177
Jerry Yue67bef42022-07-07 07:29:42 +00003178 /* In this code the psk key length equals the length of the hash */
3179 session->resumption_key_len = hash_length;
3180 session->ciphersuite = ciphersuite_info->id;
3181
3182 /* Compute resumption key
3183 *
3184 * HKDF-Expand-Label( resumption_master_secret,
3185 * "resumption", ticket_nonce, Hash.length )
3186 */
3187 ret = mbedtls_ssl_tls13_hkdf_expand_label(
Gilles Peskine449bd832023-01-11 14:50:10 +01003188 psa_hash_alg,
3189 session->app_secrets.resumption_master_secret,
3190 hash_length,
3191 MBEDTLS_SSL_TLS1_3_LBL_WITH_LEN(resumption),
3192 ticket_nonce,
3193 ticket_nonce_size,
3194 session->resumption_key,
3195 hash_length);
Jerry Yue67bef42022-07-07 07:29:42 +00003196
Gilles Peskine449bd832023-01-11 14:50:10 +01003197 if (ret != 0) {
3198 MBEDTLS_SSL_DEBUG_RET(2,
3199 "Creating the ticket-resumed PSK failed",
3200 ret);
3201 return ret;
Jerry Yue67bef42022-07-07 07:29:42 +00003202 }
Gilles Peskine449bd832023-01-11 14:50:10 +01003203 MBEDTLS_SSL_DEBUG_BUF(3, "Ticket-resumed PSK",
3204 session->resumption_key,
3205 session->resumption_key_len);
Jerry Yue67bef42022-07-07 07:29:42 +00003206
Gilles Peskine449bd832023-01-11 14:50:10 +01003207 MBEDTLS_SSL_DEBUG_BUF(3, "resumption_master_secret",
3208 session->app_secrets.resumption_master_secret,
3209 hash_length);
Jerry Yue67bef42022-07-07 07:29:42 +00003210
Gilles Peskine449bd832023-01-11 14:50:10 +01003211 return 0;
Jerry Yue67bef42022-07-07 07:29:42 +00003212}
3213
3214/* This function creates a NewSessionTicket message in the following format:
3215 *
3216 * struct {
3217 * uint32 ticket_lifetime;
3218 * uint32 ticket_age_add;
3219 * opaque ticket_nonce<0..255>;
3220 * opaque ticket<1..2^16-1>;
3221 * Extension extensions<0..2^16-2>;
3222 * } NewSessionTicket;
3223 *
3224 * The ticket inside the NewSessionTicket message is an encrypted container
3225 * carrying the necessary information so that the server is later able to
3226 * re-start the communication.
3227 *
3228 * The following fields are placed inside the ticket by the
3229 * f_ticket_write() function:
3230 *
Jerry Yu0069abc2023-11-23 21:07:28 +08003231 * - creation time (ticket_creation_time)
3232 * - flags (ticket_flags)
Jerry Yue67bef42022-07-07 07:29:42 +00003233 * - age add (ticket_age_add)
Jerry Yu0069abc2023-11-23 21:07:28 +08003234 * - key (resumption_key)
3235 * - key length (resumption_key_len)
Jerry Yue67bef42022-07-07 07:29:42 +00003236 * - ciphersuite (ciphersuite)
Jerry Yu0069abc2023-11-23 21:07:28 +08003237 * - max_early_data_size (max_early_data_size)
Jerry Yue67bef42022-07-07 07:29:42 +00003238 */
3239MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01003240static int ssl_tls13_write_new_session_ticket_body(mbedtls_ssl_context *ssl,
3241 unsigned char *buf,
3242 unsigned char *end,
3243 size_t *out_len,
3244 unsigned char *ticket_nonce,
3245 size_t ticket_nonce_size)
Jerry Yue67bef42022-07-07 07:29:42 +00003246{
3247 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
3248 unsigned char *p = buf;
3249 mbedtls_ssl_session *session = ssl->session;
3250 size_t ticket_len;
3251 uint32_t ticket_lifetime;
Jerry Yu01da35e2022-12-12 15:09:22 +08003252 unsigned char *p_extensions_len;
Jerry Yue67bef42022-07-07 07:29:42 +00003253
3254 *out_len = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01003255 MBEDTLS_SSL_DEBUG_MSG(2, ("=> write NewSessionTicket msg"));
Jerry Yue67bef42022-07-07 07:29:42 +00003256
3257 /*
3258 * ticket_lifetime 4 bytes
3259 * ticket_age_add 4 bytes
3260 * ticket_nonce 1 + ticket_nonce_size bytes
3261 * ticket >=2 bytes
3262 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003263 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 4 + 4 + 1 + ticket_nonce_size + 2);
Jerry Yue67bef42022-07-07 07:29:42 +00003264
3265 /* Generate ticket and ticket_lifetime */
Ronald Cron3c0072b2023-11-22 10:00:14 +01003266#if defined(MBEDTLS_HAVE_TIME)
3267 session->ticket_creation_time = mbedtls_ms_time();
3268#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01003269 ret = ssl->conf->f_ticket_write(ssl->conf->p_ticket,
3270 session,
3271 p + 9 + ticket_nonce_size + 2,
3272 end,
3273 &ticket_len,
3274 &ticket_lifetime);
3275 if (ret != 0) {
3276 MBEDTLS_SSL_DEBUG_RET(1, "write_ticket", ret);
3277 return ret;
Jerry Yue67bef42022-07-07 07:29:42 +00003278 }
3279 /* RFC 8446 4.6.1
3280 * ticket_lifetime: Indicates the lifetime in seconds as a 32-bit
3281 * unsigned integer in network byte order from the time of ticket
3282 * issuance. Servers MUST NOT use any value greater than
3283 * 604800 seconds (7 days). The value of zero indicates that the
3284 * ticket should be discarded immediately. Clients MUST NOT cache
3285 * tickets for longer than 7 days, regardless of the ticket_lifetime,
3286 * and MAY delete tickets earlier based on local policy. A server
3287 * MAY treat a ticket as valid for a shorter period of time than what
3288 * is stated in the ticket_lifetime.
3289 */
Jerry Yu8e0174a2023-11-10 13:58:16 +08003290 if (ticket_lifetime > MBEDTLS_SSL_TLS1_3_MAX_ALLOWED_TICKET_LIFETIME) {
3291 ticket_lifetime = MBEDTLS_SSL_TLS1_3_MAX_ALLOWED_TICKET_LIFETIME;
Gilles Peskine449bd832023-01-11 14:50:10 +01003292 }
3293 MBEDTLS_PUT_UINT32_BE(ticket_lifetime, p, 0);
3294 MBEDTLS_SSL_DEBUG_MSG(3, ("ticket_lifetime: %u",
3295 (unsigned int) ticket_lifetime));
Jerry Yue67bef42022-07-07 07:29:42 +00003296
3297 /* Write ticket_age_add */
Gilles Peskine449bd832023-01-11 14:50:10 +01003298 MBEDTLS_PUT_UINT32_BE(session->ticket_age_add, p, 4);
3299 MBEDTLS_SSL_DEBUG_MSG(3, ("ticket_age_add: %u",
3300 (unsigned int) session->ticket_age_add));
Jerry Yue67bef42022-07-07 07:29:42 +00003301
3302 /* Write ticket_nonce */
Gilles Peskine449bd832023-01-11 14:50:10 +01003303 p[8] = (unsigned char) ticket_nonce_size;
3304 if (ticket_nonce_size > 0) {
3305 memcpy(p + 9, ticket_nonce, ticket_nonce_size);
Jerry Yue67bef42022-07-07 07:29:42 +00003306 }
3307 p += 9 + ticket_nonce_size;
3308
3309 /* Write ticket */
Gilles Peskine449bd832023-01-11 14:50:10 +01003310 MBEDTLS_PUT_UINT16_BE(ticket_len, p, 0);
Jerry Yue67bef42022-07-07 07:29:42 +00003311 p += 2;
Gilles Peskine449bd832023-01-11 14:50:10 +01003312 MBEDTLS_SSL_DEBUG_BUF(4, "ticket", p, ticket_len);
Jerry Yue67bef42022-07-07 07:29:42 +00003313 p += ticket_len;
3314
3315 /* Ticket Extensions
3316 *
Jerry Yu01da35e2022-12-12 15:09:22 +08003317 * Extension extensions<0..2^16-2>;
Jerry Yue67bef42022-07-07 07:29:42 +00003318 */
Jerry Yuedab6372022-10-31 14:37:31 +08003319 ssl->handshake->sent_extensions = MBEDTLS_SSL_EXT_MASK_NONE;
3320
Gilles Peskine449bd832023-01-11 14:50:10 +01003321 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 2);
Jerry Yu01da35e2022-12-12 15:09:22 +08003322 p_extensions_len = p;
Jerry Yue67bef42022-07-07 07:29:42 +00003323 p += 2;
3324
Jerry Yu01da35e2022-12-12 15:09:22 +08003325#if defined(MBEDTLS_SSL_EARLY_DATA)
Pengyu Lv94a42cc2023-12-06 10:04:17 +08003326 if (mbedtls_ssl_tls13_session_ticket_allow_early_data(session)) {
Jerry Yu95648b02023-12-06 15:03:34 +08003327 size_t output_len;
3328
Jerry Yuf135bac2023-11-23 18:10:51 +08003329 if ((ret = mbedtls_ssl_tls13_write_early_data_ext(
Jerry Yuc59c5862023-12-05 10:40:49 +08003330 ssl, 1, p, end, &output_len)) != 0) {
Jerry Yuf135bac2023-11-23 18:10:51 +08003331 MBEDTLS_SSL_DEBUG_RET(
3332 1, "mbedtls_ssl_tls13_write_early_data_ext", ret);
3333 return ret;
3334 }
3335 p += output_len;
Jerry Yu9e7f9bc2023-11-27 16:52:07 +08003336 } else {
3337 MBEDTLS_SSL_DEBUG_MSG(
3338 4, ("early_data not allowed, "
3339 "skip early_data extension in NewSessionTicket"));
Jerry Yu01da35e2022-12-12 15:09:22 +08003340 }
Jerry Yuf135bac2023-11-23 18:10:51 +08003341
Jerry Yu01da35e2022-12-12 15:09:22 +08003342#endif /* MBEDTLS_SSL_EARLY_DATA */
3343
3344 MBEDTLS_PUT_UINT16_BE(p - p_extensions_len - 2, p_extensions_len, 0);
3345
Jerry Yue67bef42022-07-07 07:29:42 +00003346 *out_len = p - buf;
Gilles Peskine449bd832023-01-11 14:50:10 +01003347 MBEDTLS_SSL_DEBUG_BUF(4, "ticket", buf, *out_len);
3348 MBEDTLS_SSL_DEBUG_MSG(2, ("<= write new session ticket"));
Jerry Yue67bef42022-07-07 07:29:42 +00003349
Jerry Yu7de2ff02022-11-08 21:43:46 +08003350 MBEDTLS_SSL_PRINT_EXTS(
Gilles Peskine449bd832023-01-11 14:50:10 +01003351 3, MBEDTLS_SSL_HS_NEW_SESSION_TICKET, ssl->handshake->sent_extensions);
Jerry Yu4b8f2f72022-10-31 13:31:22 +08003352
Gilles Peskine449bd832023-01-11 14:50:10 +01003353 return 0;
Jerry Yue67bef42022-07-07 07:29:42 +00003354}
3355
3356/*
Jerry Yua8d3c502022-10-30 14:51:23 +08003357 * Handler for MBEDTLS_SSL_TLS1_3_NEW_SESSION_TICKET
Jerry Yue67bef42022-07-07 07:29:42 +00003358 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003359static int ssl_tls13_write_new_session_ticket(mbedtls_ssl_context *ssl)
Jerry Yue67bef42022-07-07 07:29:42 +00003360{
3361 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
3362
Gilles Peskine449bd832023-01-11 14:50:10 +01003363 MBEDTLS_SSL_PROC_CHK_NEG(ssl_tls13_write_new_session_ticket_coordinate(ssl));
Jerry Yue67bef42022-07-07 07:29:42 +00003364
Gilles Peskine449bd832023-01-11 14:50:10 +01003365 if (ret == SSL_NEW_SESSION_TICKET_WRITE) {
Jerry Yue67bef42022-07-07 07:29:42 +00003366 unsigned char ticket_nonce[MBEDTLS_SSL_TLS1_3_TICKET_NONCE_LENGTH];
3367 unsigned char *buf;
3368 size_t buf_len, msg_len;
3369
Gilles Peskine449bd832023-01-11 14:50:10 +01003370 MBEDTLS_SSL_PROC_CHK(ssl_tls13_prepare_new_session_ticket(
3371 ssl, ticket_nonce, sizeof(ticket_nonce)));
Jerry Yue67bef42022-07-07 07:29:42 +00003372
Xiaokang Qian9f1747b2023-03-30 02:50:04 +00003373 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_start_handshake_msg(
3374 ssl, MBEDTLS_SSL_HS_NEW_SESSION_TICKET,
3375 &buf, &buf_len));
Jerry Yue67bef42022-07-07 07:29:42 +00003376
Gilles Peskine449bd832023-01-11 14:50:10 +01003377 MBEDTLS_SSL_PROC_CHK(ssl_tls13_write_new_session_ticket_body(
3378 ssl, buf, buf + buf_len, &msg_len,
3379 ticket_nonce, sizeof(ticket_nonce)));
Jerry Yue67bef42022-07-07 07:29:42 +00003380
Gilles Peskine449bd832023-01-11 14:50:10 +01003381 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_finish_handshake_msg(
3382 ssl, buf_len, msg_len));
Jerry Yufca4d572022-07-21 10:37:48 +08003383
Jerry Yu359e65f2022-09-22 23:47:43 +08003384 /* Limit session tickets count to one when resumption connection.
3385 *
3386 * See document of mbedtls_ssl_conf_new_session_tickets.
3387 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003388 if (ssl->handshake->resume == 1) {
Jerry Yu359e65f2022-09-22 23:47:43 +08003389 ssl->handshake->new_session_tickets_count = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01003390 } else {
Jerry Yu359e65f2022-09-22 23:47:43 +08003391 ssl->handshake->new_session_tickets_count--;
Gilles Peskine449bd832023-01-11 14:50:10 +01003392 }
Jerry Yub7e3fa72022-09-22 11:07:18 +08003393
Jerry Yua8d3c502022-10-30 14:51:23 +08003394 mbedtls_ssl_handshake_set_state(
Gilles Peskine449bd832023-01-11 14:50:10 +01003395 ssl, MBEDTLS_SSL_TLS1_3_NEW_SESSION_TICKET_FLUSH);
3396 } else {
3397 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_HANDSHAKE_OVER);
Jerry Yue67bef42022-07-07 07:29:42 +00003398 }
3399
Jerry Yue67bef42022-07-07 07:29:42 +00003400cleanup:
3401
Gilles Peskine449bd832023-01-11 14:50:10 +01003402 return ret;
Jerry Yue67bef42022-07-07 07:29:42 +00003403}
3404#endif /* MBEDTLS_SSL_SESSION_TICKETS */
3405
3406/*
XiaokangQiane8ff3502022-04-22 02:34:40 +00003407 * TLS 1.3 State Machine -- server side
XiaokangQian7807f9f2022-02-15 10:04:37 +00003408 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003409int mbedtls_ssl_tls13_handshake_server_step(mbedtls_ssl_context *ssl)
Jerry Yub9930e72021-08-06 17:11:51 +08003410{
XiaokangQian08037552022-04-20 07:16:41 +00003411 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
XiaokangQian7807f9f2022-02-15 10:04:37 +00003412
Gilles Peskine449bd832023-01-11 14:50:10 +01003413 if (ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER || ssl->handshake == NULL) {
3414 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
3415 }
XiaokangQian7807f9f2022-02-15 10:04:37 +00003416
Gilles Peskine449bd832023-01-11 14:50:10 +01003417 MBEDTLS_SSL_DEBUG_MSG(2, ("tls13 server state: %s(%d)",
Dave Rodgman2eab4622023-10-05 13:30:37 +01003418 mbedtls_ssl_states_str((mbedtls_ssl_states) ssl->state),
Gilles Peskine449bd832023-01-11 14:50:10 +01003419 ssl->state));
Jerry Yu6e81b272021-09-27 11:16:17 +08003420
Gilles Peskine449bd832023-01-11 14:50:10 +01003421 switch (ssl->state) {
XiaokangQian7807f9f2022-02-15 10:04:37 +00003422 /* start state */
3423 case MBEDTLS_SSL_HELLO_REQUEST:
Gilles Peskine449bd832023-01-11 14:50:10 +01003424 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_CLIENT_HELLO);
XiaokangQian08037552022-04-20 07:16:41 +00003425 ret = 0;
XiaokangQian7807f9f2022-02-15 10:04:37 +00003426 break;
3427
XiaokangQian7807f9f2022-02-15 10:04:37 +00003428 case MBEDTLS_SSL_CLIENT_HELLO:
Gilles Peskine449bd832023-01-11 14:50:10 +01003429 ret = ssl_tls13_process_client_hello(ssl);
3430 if (ret != 0) {
3431 MBEDTLS_SSL_DEBUG_RET(1, "ssl_tls13_process_client_hello", ret);
3432 }
Jerry Yuf41553b2022-05-09 22:20:30 +08003433 break;
XiaokangQian7807f9f2022-02-15 10:04:37 +00003434
Jerry Yuf41553b2022-05-09 22:20:30 +08003435 case MBEDTLS_SSL_HELLO_RETRY_REQUEST:
Gilles Peskine449bd832023-01-11 14:50:10 +01003436 ret = ssl_tls13_write_hello_retry_request(ssl);
3437 if (ret != 0) {
3438 MBEDTLS_SSL_DEBUG_RET(1, "ssl_tls13_write_hello_retry_request", ret);
3439 return ret;
Jerry Yuf41553b2022-05-09 22:20:30 +08003440 }
XiaokangQian7807f9f2022-02-15 10:04:37 +00003441 break;
3442
Jerry Yu5b64ae92022-03-30 17:15:02 +08003443 case MBEDTLS_SSL_SERVER_HELLO:
Gilles Peskine449bd832023-01-11 14:50:10 +01003444 ret = ssl_tls13_write_server_hello(ssl);
Jerry Yu5b64ae92022-03-30 17:15:02 +08003445 break;
3446
Xiaofei Baicba64af2022-02-15 10:00:56 +00003447 case MBEDTLS_SSL_ENCRYPTED_EXTENSIONS:
Gilles Peskine449bd832023-01-11 14:50:10 +01003448 ret = ssl_tls13_write_encrypted_extensions(ssl);
3449 if (ret != 0) {
3450 MBEDTLS_SSL_DEBUG_RET(1, "ssl_tls13_write_encrypted_extensions", ret);
3451 return ret;
Xiaofei Baicba64af2022-02-15 10:00:56 +00003452 }
Jerry Yu48330562022-05-06 21:35:44 +08003453 break;
Jerry Yu6a2cd9e2022-05-05 11:14:19 +08003454
Ronald Cron928cbd32022-10-04 16:14:26 +02003455#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED)
Xiaofei Bai9ca09d42022-02-14 12:57:18 +00003456 case MBEDTLS_SSL_CERTIFICATE_REQUEST:
Gilles Peskine449bd832023-01-11 14:50:10 +01003457 ret = ssl_tls13_write_certificate_request(ssl);
Xiaofei Bai9ca09d42022-02-14 12:57:18 +00003458 break;
Xiaofei Bai9ca09d42022-02-14 12:57:18 +00003459
Jerry Yu83da34e2022-04-16 13:59:52 +08003460 case MBEDTLS_SSL_SERVER_CERTIFICATE:
Gilles Peskine449bd832023-01-11 14:50:10 +01003461 ret = ssl_tls13_write_server_certificate(ssl);
Jerry Yu83da34e2022-04-16 13:59:52 +08003462 break;
3463
3464 case MBEDTLS_SSL_CERTIFICATE_VERIFY:
Gilles Peskine449bd832023-01-11 14:50:10 +01003465 ret = ssl_tls13_write_certificate_verify(ssl);
Jerry Yu83da34e2022-04-16 13:59:52 +08003466 break;
Ronald Cron928cbd32022-10-04 16:14:26 +02003467#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED */
Jerry Yu83da34e2022-04-16 13:59:52 +08003468
Gilles Peskine449bd832023-01-11 14:50:10 +01003469 /*
3470 * Injection of dummy-CCS's for middlebox compatibility
3471 */
Gabor Mezei7b39bf12022-05-24 16:04:14 +02003472#if defined(MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE)
Gabor Mezeif7044ea2022-06-28 16:01:49 +02003473 case MBEDTLS_SSL_SERVER_CCS_AFTER_HELLO_RETRY_REQUEST:
Gilles Peskine449bd832023-01-11 14:50:10 +01003474 ret = mbedtls_ssl_tls13_write_change_cipher_spec(ssl);
3475 if (ret == 0) {
3476 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_CLIENT_HELLO);
3477 }
Gabor Mezei7b39bf12022-05-24 16:04:14 +02003478 break;
3479
3480 case MBEDTLS_SSL_SERVER_CCS_AFTER_SERVER_HELLO:
Ronald Crone273f722024-02-13 18:22:26 +01003481 ret = mbedtls_ssl_tls13_write_change_cipher_spec(ssl);
3482 if (ret != 0) {
3483 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01003484 }
Ronald Cronfe59ff72024-01-24 14:31:50 +01003485 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_ENCRYPTED_EXTENSIONS);
Gabor Mezei7b39bf12022-05-24 16:04:14 +02003486 break;
3487#endif /* MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE */
3488
Jerry Yu69dd8d42022-04-16 12:51:26 +08003489 case MBEDTLS_SSL_SERVER_FINISHED:
Gilles Peskine449bd832023-01-11 14:50:10 +01003490 ret = ssl_tls13_write_server_finished(ssl);
Jerry Yu69dd8d42022-04-16 12:51:26 +08003491 break;
3492
Jerry Yu87b5ed42022-12-12 13:07:07 +08003493#if defined(MBEDTLS_SSL_EARLY_DATA)
3494 case MBEDTLS_SSL_END_OF_EARLY_DATA:
Jerry Yu59d420f2023-12-01 16:30:34 +08003495 ret = ssl_tls13_process_end_of_early_data(ssl);
Jerry Yu87b5ed42022-12-12 13:07:07 +08003496 break;
3497#endif /* MBEDTLS_SSL_EARLY_DATA */
3498
Jerry Yu69dd8d42022-04-16 12:51:26 +08003499 case MBEDTLS_SSL_CLIENT_FINISHED:
Gilles Peskine449bd832023-01-11 14:50:10 +01003500 ret = ssl_tls13_process_client_finished(ssl);
Jerry Yu69dd8d42022-04-16 12:51:26 +08003501 break;
3502
Jerry Yu69dd8d42022-04-16 12:51:26 +08003503 case MBEDTLS_SSL_HANDSHAKE_WRAPUP:
Gilles Peskine449bd832023-01-11 14:50:10 +01003504 ret = ssl_tls13_handshake_wrapup(ssl);
Jerry Yu69dd8d42022-04-16 12:51:26 +08003505 break;
3506
Ronald Cron766c0cd2022-10-18 12:17:11 +02003507#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED)
XiaokangQian6b916b12022-04-25 07:29:34 +00003508 case MBEDTLS_SSL_CLIENT_CERTIFICATE:
Gilles Peskine449bd832023-01-11 14:50:10 +01003509 ret = mbedtls_ssl_tls13_process_certificate(ssl);
3510 if (ret == 0) {
3511 if (ssl->session_negotiate->peer_cert != NULL) {
Ronald Cron209cae92022-06-07 10:30:19 +02003512 mbedtls_ssl_handshake_set_state(
Gilles Peskine449bd832023-01-11 14:50:10 +01003513 ssl, MBEDTLS_SSL_CLIENT_CERTIFICATE_VERIFY);
3514 } else {
3515 MBEDTLS_SSL_DEBUG_MSG(2, ("skip parse certificate verify"));
Ronald Cron209cae92022-06-07 10:30:19 +02003516 mbedtls_ssl_handshake_set_state(
Gilles Peskine449bd832023-01-11 14:50:10 +01003517 ssl, MBEDTLS_SSL_CLIENT_FINISHED);
Ronald Cron19385882022-06-15 16:26:13 +02003518 }
XiaokangQian6b916b12022-04-25 07:29:34 +00003519 }
3520 break;
3521
3522 case MBEDTLS_SSL_CLIENT_CERTIFICATE_VERIFY:
Gilles Peskine449bd832023-01-11 14:50:10 +01003523 ret = mbedtls_ssl_tls13_process_certificate_verify(ssl);
3524 if (ret == 0) {
XiaokangQian6b916b12022-04-25 07:29:34 +00003525 mbedtls_ssl_handshake_set_state(
Gilles Peskine449bd832023-01-11 14:50:10 +01003526 ssl, MBEDTLS_SSL_CLIENT_FINISHED);
XiaokangQian6b916b12022-04-25 07:29:34 +00003527 }
3528 break;
Ronald Cron766c0cd2022-10-18 12:17:11 +02003529#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED */
XiaokangQian6b916b12022-04-25 07:29:34 +00003530
Jerry Yue67bef42022-07-07 07:29:42 +00003531#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Jerry Yua8d3c502022-10-30 14:51:23 +08003532 case MBEDTLS_SSL_TLS1_3_NEW_SESSION_TICKET:
Gilles Peskine449bd832023-01-11 14:50:10 +01003533 ret = ssl_tls13_write_new_session_ticket(ssl);
3534 if (ret != 0) {
3535 MBEDTLS_SSL_DEBUG_RET(1,
3536 "ssl_tls13_write_new_session_ticket ",
3537 ret);
Jerry Yue67bef42022-07-07 07:29:42 +00003538 }
3539 break;
Jerry Yua8d3c502022-10-30 14:51:23 +08003540 case MBEDTLS_SSL_TLS1_3_NEW_SESSION_TICKET_FLUSH:
Jerry Yue67bef42022-07-07 07:29:42 +00003541 /* This state is necessary to do the flush of the New Session
Jerry Yua8d3c502022-10-30 14:51:23 +08003542 * Ticket message written in MBEDTLS_SSL_TLS1_3_NEW_SESSION_TICKET
Jerry Yue67bef42022-07-07 07:29:42 +00003543 * as part of ssl_prepare_handshake_step.
3544 */
3545 ret = 0;
Jerry Yud4e75002022-08-09 13:33:50 +08003546
Gilles Peskine449bd832023-01-11 14:50:10 +01003547 if (ssl->handshake->new_session_tickets_count == 0) {
3548 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_HANDSHAKE_OVER);
3549 } else {
Xiaokang Qian9f1747b2023-03-30 02:50:04 +00003550 mbedtls_ssl_handshake_set_state(
3551 ssl, MBEDTLS_SSL_TLS1_3_NEW_SESSION_TICKET);
Gilles Peskine449bd832023-01-11 14:50:10 +01003552 }
Jerry Yue67bef42022-07-07 07:29:42 +00003553 break;
3554
3555#endif /* MBEDTLS_SSL_SESSION_TICKETS */
3556
XiaokangQian7807f9f2022-02-15 10:04:37 +00003557 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01003558 MBEDTLS_SSL_DEBUG_MSG(1, ("invalid state %d", ssl->state));
3559 return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
XiaokangQian7807f9f2022-02-15 10:04:37 +00003560 }
3561
Gilles Peskine449bd832023-01-11 14:50:10 +01003562 return ret;
Jerry Yub9930e72021-08-06 17:11:51 +08003563}
Jerry Yu3cc4c2a2021-08-06 16:29:08 +08003564
Jerry Yufb4b6472022-01-27 15:03:26 +08003565#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_PROTO_TLS1_3 */