blob: 6fcf8a23cde75c72bd20b2c57e9a44d1fc6f6695 [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) {
126 return 0;
127 }
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 Lv18946532023-01-12 12:28:09 +0800174 ret = MBEDTLS_ERR_ERROR_GENERIC_ERROR;
Pengyu Lv29daf4a2023-10-30 17:13:30 +0800175 MBEDTLS_SSL_PRINT_TICKET_FLAGS(4, session->ticket_flags);
Pengyu Lv29daf4a2023-10-30 17:13:30 +0800176
177 key_exchanges = 0;
Pengyu Lv94a42cc2023-12-06 10:04:17 +0800178 if (mbedtls_ssl_tls13_session_ticket_allow_psk_ephemeral(session) &&
Pengyu Lvbc4aab72023-12-01 15:37:24 +0800179 ssl_tls13_key_exchange_is_psk_ephemeral_available(ssl)) {
Pengyu Lv29daf4a2023-10-30 17:13:30 +0800180 key_exchanges |= MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK_EPHEMERAL;
181 }
Pengyu Lv94a42cc2023-12-06 10:04:17 +0800182 if (mbedtls_ssl_tls13_session_ticket_allow_psk(session) &&
Pengyu Lvbc4aab72023-12-01 15:37:24 +0800183 ssl_tls13_key_exchange_is_psk_available(ssl)) {
Pengyu Lv29daf4a2023-10-30 17:13:30 +0800184 key_exchanges |= MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK;
185 }
186
187 if (key_exchanges == 0) {
Pengyu Lv3eb49be2022-12-05 16:35:12 +0800188 MBEDTLS_SSL_DEBUG_MSG(3, ("No suitable key exchange mode"));
189 goto exit;
190 }
191
Gilles Peskine449bd832023-01-11 14:50:10 +0100192 ret = MBEDTLS_ERR_SSL_SESSION_TICKET_EXPIRED;
193#if defined(MBEDTLS_HAVE_TIME)
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
248#endif /* MBEDTLS_HAVE_TIME */
Jerry Yu8d4bbba2022-09-13 14:15:48 +0800249
250exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100251 if (ret != 0) {
252 mbedtls_ssl_session_free(session);
253 }
Jerry Yu95699e72022-08-21 19:22:23 +0800254
Gilles Peskine449bd832023-01-11 14:50:10 +0100255 MBEDTLS_SSL_DEBUG_MSG(2, ("<= check_identity_match_ticket"));
256 return ret;
Jerry Yu95699e72022-08-21 19:22:23 +0800257}
258#endif /* MBEDTLS_SSL_SESSION_TICKETS */
259
Jerry Yu6e74a7e2022-07-20 20:49:32 +0800260MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yu1c105562022-07-10 06:32:38 +0000261static int ssl_tls13_offered_psks_check_identity_match(
Gilles Peskine449bd832023-01-11 14:50:10 +0100262 mbedtls_ssl_context *ssl,
263 const unsigned char *identity,
264 size_t identity_len,
265 uint32_t obfuscated_ticket_age,
266 int *psk_type,
267 mbedtls_ssl_session *session)
Jerry Yu1c105562022-07-10 06:32:38 +0000268{
Ronald Cron606671e2023-02-17 11:36:33 +0100269 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
270
Jerry Yu95699e72022-08-21 19:22:23 +0800271 ((void) session);
272 ((void) obfuscated_ticket_age);
Jerry Yu5725f1c2022-08-21 17:27:16 +0800273 *psk_type = MBEDTLS_SSL_TLS1_3_PSK_EXTERNAL;
Jerry Yu95699e72022-08-21 19:22:23 +0800274
Gilles Peskine449bd832023-01-11 14:50:10 +0100275 MBEDTLS_SSL_DEBUG_BUF(4, "identity", identity, identity_len);
Jerry Yu95699e72022-08-21 19:22:23 +0800276 ssl->handshake->resume = 0;
277
Jerry Yu95699e72022-08-21 19:22:23 +0800278#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Gilles Peskine449bd832023-01-11 14:50:10 +0100279 if (ssl_tls13_offered_psks_check_identity_match_ticket(
Jerry Yu8d4bbba2022-09-13 14:15:48 +0800280 ssl, identity, identity_len, obfuscated_ticket_age,
Gilles Peskine449bd832023-01-11 14:50:10 +0100281 session) == SSL_TLS1_3_OFFERED_PSK_MATCH) {
Jerry Yu95699e72022-08-21 19:22:23 +0800282 ssl->handshake->resume = 1;
283 *psk_type = MBEDTLS_SSL_TLS1_3_PSK_RESUMPTION;
Ronald Cron606671e2023-02-17 11:36:33 +0100284 ret = mbedtls_ssl_set_hs_psk(ssl,
285 session->resumption_key,
286 session->resumption_key_len);
287 if (ret != 0) {
288 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_set_hs_psk", ret);
289 return ret;
290 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100291
292 MBEDTLS_SSL_DEBUG_BUF(4, "Ticket-resumed PSK:",
293 session->resumption_key,
294 session->resumption_key_len);
295 MBEDTLS_SSL_DEBUG_MSG(4, ("ticket: obfuscated_ticket_age: %u",
296 (unsigned) obfuscated_ticket_age));
297 return SSL_TLS1_3_OFFERED_PSK_MATCH;
Jerry Yu95699e72022-08-21 19:22:23 +0800298 }
299#endif /* MBEDTLS_SSL_SESSION_TICKETS */
300
Jerry Yu1c105562022-07-10 06:32:38 +0000301 /* Check identity with external configured function */
Gilles Peskine449bd832023-01-11 14:50:10 +0100302 if (ssl->conf->f_psk != NULL) {
303 if (ssl->conf->f_psk(
304 ssl->conf->p_psk, ssl, identity, identity_len) == 0) {
305 return SSL_TLS1_3_OFFERED_PSK_MATCH;
Jerry Yu1c105562022-07-10 06:32:38 +0000306 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100307 return SSL_TLS1_3_OFFERED_PSK_NOT_MATCH;
Jerry Yu1c105562022-07-10 06:32:38 +0000308 }
309
Gilles Peskine449bd832023-01-11 14:50:10 +0100310 MBEDTLS_SSL_DEBUG_BUF(5, "identity", identity, identity_len);
Jerry Yu1c105562022-07-10 06:32:38 +0000311 /* Check identity with pre-configured psk */
Gilles Peskine449bd832023-01-11 14:50:10 +0100312 if (ssl->conf->psk_identity != NULL &&
Jerry Yu2f0abc92022-07-22 19:34:48 +0800313 identity_len == ssl->conf->psk_identity_len &&
Gilles Peskine449bd832023-01-11 14:50:10 +0100314 mbedtls_ct_memcmp(ssl->conf->psk_identity,
315 identity, identity_len) == 0) {
Ronald Cron606671e2023-02-17 11:36:33 +0100316 ret = mbedtls_ssl_set_hs_psk(ssl, ssl->conf->psk, ssl->conf->psk_len);
317 if (ret != 0) {
318 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_set_hs_psk", ret);
319 return ret;
320 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100321 return SSL_TLS1_3_OFFERED_PSK_MATCH;
Jerry Yu1c105562022-07-10 06:32:38 +0000322 }
323
Gilles Peskine449bd832023-01-11 14:50:10 +0100324 return SSL_TLS1_3_OFFERED_PSK_NOT_MATCH;
Jerry Yu1c105562022-07-10 06:32:38 +0000325}
326
Ronald Cron6e311272023-12-05 17:57:01 +0100327#define SSL_TLS1_3_BINDER_DOES_NOT_MATCH 1
328#define SSL_TLS1_3_BINDER_MATCH 0
Jerry Yu6e74a7e2022-07-20 20:49:32 +0800329MBEDTLS_CHECK_RETURN_CRITICAL
Xiaokang Qian9f1747b2023-03-30 02:50:04 +0000330static int ssl_tls13_offered_psks_check_binder_match(
331 mbedtls_ssl_context *ssl,
332 const unsigned char *binder, size_t binder_len,
333 int psk_type, psa_algorithm_t psk_hash_alg)
Jerry Yu1c105562022-07-10 06:32:38 +0000334{
335 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yu96a2e362022-07-21 15:11:34 +0800336
Jerry Yudaf375a2022-07-20 21:31:43 +0800337 unsigned char transcript[PSA_HASH_MAX_SIZE];
Jerry Yu1c105562022-07-10 06:32:38 +0000338 size_t transcript_len;
Jerry Yu2f0abc92022-07-22 19:34:48 +0800339 unsigned char *psk;
Jerry Yu96a2e362022-07-21 15:11:34 +0800340 size_t psk_len;
Jerry Yudaf375a2022-07-20 21:31:43 +0800341 unsigned char server_computed_binder[PSA_HASH_MAX_SIZE];
Jerry Yu1c105562022-07-10 06:32:38 +0000342
Jerry Yu1c105562022-07-10 06:32:38 +0000343 /* Get current state of handshake transcript. */
Jerry Yu29d9faa2022-08-23 17:52:45 +0800344 ret = mbedtls_ssl_get_handshake_transcript(
Manuel Pégourié-Gonnard2d6d9932023-03-28 11:38:08 +0200345 ssl, mbedtls_md_type_from_psa_alg(psk_hash_alg),
Gilles Peskine449bd832023-01-11 14:50:10 +0100346 transcript, sizeof(transcript), &transcript_len);
347 if (ret != 0) {
348 return ret;
349 }
Jerry Yu1c105562022-07-10 06:32:38 +0000350
Gilles Peskine449bd832023-01-11 14:50:10 +0100351 ret = mbedtls_ssl_tls13_export_handshake_psk(ssl, &psk, &psk_len);
352 if (ret != 0) {
353 return ret;
354 }
Jerry Yu96a2e362022-07-21 15:11:34 +0800355
Gilles Peskine449bd832023-01-11 14:50:10 +0100356 ret = mbedtls_ssl_tls13_create_psk_binder(ssl, psk_hash_alg,
357 psk, psk_len, psk_type,
358 transcript,
359 server_computed_binder);
Jerry Yu96a2e362022-07-21 15:11:34 +0800360#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +0100361 mbedtls_free((void *) psk);
Jerry Yu96a2e362022-07-21 15:11:34 +0800362#endif
Gilles Peskine449bd832023-01-11 14:50:10 +0100363 if (ret != 0) {
364 MBEDTLS_SSL_DEBUG_MSG(1, ("PSK binder calculation failed."));
365 return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
Jerry Yu1c105562022-07-10 06:32:38 +0000366 }
367
Gilles Peskine449bd832023-01-11 14:50:10 +0100368 MBEDTLS_SSL_DEBUG_BUF(3, "psk binder ( computed ): ",
369 server_computed_binder, transcript_len);
370 MBEDTLS_SSL_DEBUG_BUF(3, "psk binder ( received ): ", binder, binder_len);
Jerry Yu1c105562022-07-10 06:32:38 +0000371
Gilles Peskine449bd832023-01-11 14:50:10 +0100372 if (mbedtls_ct_memcmp(server_computed_binder, binder, binder_len) == 0) {
Ronald Cron6e311272023-12-05 17:57:01 +0100373 return SSL_TLS1_3_BINDER_MATCH;
Jerry Yu1c105562022-07-10 06:32:38 +0000374 }
375
Gilles Peskine449bd832023-01-11 14:50:10 +0100376 mbedtls_platform_zeroize(server_computed_binder,
377 sizeof(server_computed_binder));
Ronald Cron6e311272023-12-05 17:57:01 +0100378 return SSL_TLS1_3_BINDER_DOES_NOT_MATCH;
Jerry Yu1c105562022-07-10 06:32:38 +0000379}
Jerry Yu96a2e362022-07-21 15:11:34 +0800380
Jerry Yu5725f1c2022-08-21 17:27:16 +0800381MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yuf35ba382022-08-23 17:58:26 +0800382static int ssl_tls13_select_ciphersuite_for_psk(
Gilles Peskine449bd832023-01-11 14:50:10 +0100383 mbedtls_ssl_context *ssl,
384 const unsigned char *cipher_suites,
385 const unsigned char *cipher_suites_end,
386 uint16_t *selected_ciphersuite,
387 const mbedtls_ssl_ciphersuite_t **selected_ciphersuite_info)
Jerry Yu5725f1c2022-08-21 17:27:16 +0800388{
Jerry Yuf35ba382022-08-23 17:58:26 +0800389 psa_algorithm_t psk_hash_alg = PSA_ALG_SHA_256;
Jerry Yu5725f1c2022-08-21 17:27:16 +0800390
Jerry Yu0baf9072022-08-25 11:21:04 +0800391 *selected_ciphersuite = 0;
392 *selected_ciphersuite_info = NULL;
393
Jerry Yuf35ba382022-08-23 17:58:26 +0800394 /* RFC 8446, page 55.
395 *
396 * For externally established PSKs, the Hash algorithm MUST be set when the
397 * PSK is established or default to SHA-256 if no such algorithm is defined.
398 *
399 */
Jerry Yu5725f1c2022-08-21 17:27:16 +0800400
Jerry Yu5725f1c2022-08-21 17:27:16 +0800401 /*
402 * Search for a matching ciphersuite
403 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100404 for (const unsigned char *p = cipher_suites;
405 p < cipher_suites_end; p += 2) {
Jerry Yu5725f1c2022-08-21 17:27:16 +0800406 uint16_t cipher_suite;
Jerry Yuf35ba382022-08-23 17:58:26 +0800407 const mbedtls_ssl_ciphersuite_t *ciphersuite_info;
Jerry Yu5725f1c2022-08-21 17:27:16 +0800408
Gilles Peskine449bd832023-01-11 14:50:10 +0100409 cipher_suite = MBEDTLS_GET_UINT16_BE(p, 0);
410 ciphersuite_info = ssl_tls13_validate_peer_ciphersuite(ssl,
411 cipher_suite);
412 if (ciphersuite_info == NULL) {
Jerry Yu5725f1c2022-08-21 17:27:16 +0800413 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +0100414 }
Jerry Yu5725f1c2022-08-21 17:27:16 +0800415
Jerry Yu5725f1c2022-08-21 17:27:16 +0800416 /* MAC of selected ciphersuite MUST be same with PSK binder if exist.
417 * Otherwise, client should reject.
418 */
Dave Rodgman2eab4622023-10-05 13:30:37 +0100419 if (psk_hash_alg ==
420 mbedtls_md_psa_alg_from_type((mbedtls_md_type_t) ciphersuite_info->mac)) {
Jerry Yuf35ba382022-08-23 17:58:26 +0800421 *selected_ciphersuite = cipher_suite;
422 *selected_ciphersuite_info = ciphersuite_info;
Gilles Peskine449bd832023-01-11 14:50:10 +0100423 return 0;
Jerry Yuf35ba382022-08-23 17:58:26 +0800424 }
425 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100426 MBEDTLS_SSL_DEBUG_MSG(2, ("No matched ciphersuite"));
427 return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
Jerry Yuf35ba382022-08-23 17:58:26 +0800428}
Jerry Yu5725f1c2022-08-21 17:27:16 +0800429
Jerry Yu82534862022-08-30 10:42:33 +0800430#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Jerry Yuf35ba382022-08-23 17:58:26 +0800431MBEDTLS_CHECK_RETURN_CRITICAL
432static int ssl_tls13_select_ciphersuite_for_resumption(
Gilles Peskine449bd832023-01-11 14:50:10 +0100433 mbedtls_ssl_context *ssl,
434 const unsigned char *cipher_suites,
435 const unsigned char *cipher_suites_end,
436 mbedtls_ssl_session *session,
437 uint16_t *selected_ciphersuite,
438 const mbedtls_ssl_ciphersuite_t **selected_ciphersuite_info)
Jerry Yuf35ba382022-08-23 17:58:26 +0800439{
Jerry Yu82534862022-08-30 10:42:33 +0800440
Jerry Yuf35ba382022-08-23 17:58:26 +0800441 *selected_ciphersuite = 0;
442 *selected_ciphersuite_info = NULL;
Gilles Peskine449bd832023-01-11 14:50:10 +0100443 for (const unsigned char *p = cipher_suites; p < cipher_suites_end; p += 2) {
444 uint16_t cipher_suite = MBEDTLS_GET_UINT16_BE(p, 0);
Jerry Yu82534862022-08-30 10:42:33 +0800445 const mbedtls_ssl_ciphersuite_t *ciphersuite_info;
446
Gilles Peskine449bd832023-01-11 14:50:10 +0100447 if (cipher_suite != session->ciphersuite) {
Jerry Yu82534862022-08-30 10:42:33 +0800448 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +0100449 }
Jerry Yu82534862022-08-30 10:42:33 +0800450
Gilles Peskine449bd832023-01-11 14:50:10 +0100451 ciphersuite_info = ssl_tls13_validate_peer_ciphersuite(ssl,
452 cipher_suite);
453 if (ciphersuite_info == NULL) {
Jerry Yu82534862022-08-30 10:42:33 +0800454 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +0100455 }
Jerry Yu82534862022-08-30 10:42:33 +0800456
Jerry Yu4746b102022-09-13 11:11:48 +0800457 *selected_ciphersuite = cipher_suite;
Jerry Yu82534862022-08-30 10:42:33 +0800458 *selected_ciphersuite_info = ciphersuite_info;
459
Gilles Peskine449bd832023-01-11 14:50:10 +0100460 return 0;
Jerry Yu82534862022-08-30 10:42:33 +0800461 }
462
Gilles Peskine449bd832023-01-11 14:50:10 +0100463 return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
Jerry Yu5725f1c2022-08-21 17:27:16 +0800464}
Jerry Yuf35ba382022-08-23 17:58:26 +0800465
Jerry Yu82534862022-08-30 10:42:33 +0800466MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100467static int ssl_tls13_session_copy_ticket(mbedtls_ssl_session *dst,
468 const mbedtls_ssl_session *src)
Jerry Yu82534862022-08-30 10:42:33 +0800469{
Jerry Yu82534862022-08-30 10:42:33 +0800470 dst->ticket_age_add = src->ticket_age_add;
471 dst->ticket_flags = src->ticket_flags;
472 dst->resumption_key_len = src->resumption_key_len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100473 if (src->resumption_key_len == 0) {
474 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
475 }
476 memcpy(dst->resumption_key, src->resumption_key, src->resumption_key_len);
Jerry Yu4746b102022-09-13 11:11:48 +0800477
Jerry Yu33bf2402022-12-12 16:01:43 +0800478#if defined(MBEDTLS_SSL_EARLY_DATA)
479 dst->max_early_data_size = src->max_early_data_size;
480#endif
481
Gilles Peskine449bd832023-01-11 14:50:10 +0100482 return 0;
Jerry Yu82534862022-08-30 10:42:33 +0800483}
484#endif /* MBEDTLS_SSL_SESSION_TICKETS */
485
Jerry Yu1c105562022-07-10 06:32:38 +0000486/* Parser for pre_shared_key extension in client hello
487 * struct {
488 * opaque identity<1..2^16-1>;
489 * uint32 obfuscated_ticket_age;
490 * } PskIdentity;
491 *
492 * opaque PskBinderEntry<32..255>;
493 *
494 * struct {
495 * PskIdentity identities<7..2^16-1>;
496 * PskBinderEntry binders<33..2^16-1>;
497 * } OfferedPsks;
498 *
499 * struct {
500 * select (Handshake.msg_type) {
501 * case client_hello: OfferedPsks;
502 * ....
503 * };
504 * } PreSharedKeyExtension;
505 */
Jerry Yu6e74a7e2022-07-20 20:49:32 +0800506MBEDTLS_CHECK_RETURN_CRITICAL
Xiaokang Qian9f1747b2023-03-30 02:50:04 +0000507static int ssl_tls13_parse_pre_shared_key_ext(
508 mbedtls_ssl_context *ssl,
509 const unsigned char *pre_shared_key_ext,
510 const unsigned char *pre_shared_key_ext_end,
511 const unsigned char *ciphersuites,
512 const unsigned char *ciphersuites_end)
Jerry Yu1c105562022-07-10 06:32:38 +0000513{
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +0100514 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yu29d9faa2022-08-23 17:52:45 +0800515 const unsigned char *identities = pre_shared_key_ext;
Jerry Yu568ec252022-07-22 21:27:34 +0800516 const unsigned char *p_identity_len;
517 size_t identities_len;
Jerry Yu1c105562022-07-10 06:32:38 +0000518 const unsigned char *identities_end;
Jerry Yu568ec252022-07-22 21:27:34 +0800519 const unsigned char *binders;
520 const unsigned char *p_binder_len;
521 size_t binders_len;
Jerry Yu1c105562022-07-10 06:32:38 +0000522 const unsigned char *binders_end;
Jerry Yu96a2e362022-07-21 15:11:34 +0800523 int matched_identity = -1;
524 int identity_id = -1;
Jerry Yu1c105562022-07-10 06:32:38 +0000525
Gilles Peskine449bd832023-01-11 14:50:10 +0100526 MBEDTLS_SSL_DEBUG_BUF(3, "pre_shared_key extension",
527 pre_shared_key_ext,
528 pre_shared_key_ext_end - pre_shared_key_ext);
Jerry Yu1c105562022-07-10 06:32:38 +0000529
Jerry Yu96a2e362022-07-21 15:11:34 +0800530 /* identities_len 2 bytes
531 * identities_data >= 7 bytes
532 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100533 MBEDTLS_SSL_CHK_BUF_READ_PTR(identities, pre_shared_key_ext_end, 7 + 2);
534 identities_len = MBEDTLS_GET_UINT16_BE(identities, 0);
Jerry Yu568ec252022-07-22 21:27:34 +0800535 p_identity_len = identities + 2;
Gilles Peskine449bd832023-01-11 14:50:10 +0100536 MBEDTLS_SSL_CHK_BUF_READ_PTR(p_identity_len, pre_shared_key_ext_end,
537 identities_len);
Jerry Yu568ec252022-07-22 21:27:34 +0800538 identities_end = p_identity_len + identities_len;
Jerry Yu1c105562022-07-10 06:32:38 +0000539
Jerry Yu96a2e362022-07-21 15:11:34 +0800540 /* binders_len 2 bytes
541 * binders >= 33 bytes
542 */
Jerry Yu568ec252022-07-22 21:27:34 +0800543 binders = identities_end;
Gilles Peskine449bd832023-01-11 14:50:10 +0100544 MBEDTLS_SSL_CHK_BUF_READ_PTR(binders, pre_shared_key_ext_end, 33 + 2);
545 binders_len = MBEDTLS_GET_UINT16_BE(binders, 0);
Jerry Yu568ec252022-07-22 21:27:34 +0800546 p_binder_len = binders + 2;
Gilles Peskine449bd832023-01-11 14:50:10 +0100547 MBEDTLS_SSL_CHK_BUF_READ_PTR(p_binder_len, pre_shared_key_ext_end, binders_len);
Jerry Yu568ec252022-07-22 21:27:34 +0800548 binders_end = p_binder_len + binders_len;
Jerry Yu1c105562022-07-10 06:32:38 +0000549
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +0100550 ret = ssl->handshake->update_checksum(ssl, pre_shared_key_ext,
551 identities_end - pre_shared_key_ext);
552 if (0 != ret) {
553 MBEDTLS_SSL_DEBUG_RET(1, ("update_checksum"), ret);
554 return ret;
555 }
Jerry Yu96a2e362022-07-21 15:11:34 +0800556
Gilles Peskine449bd832023-01-11 14:50:10 +0100557 while (p_identity_len < identities_end && p_binder_len < binders_end) {
Jerry Yu1c105562022-07-10 06:32:38 +0000558 const unsigned char *identity;
Jerry Yu568ec252022-07-22 21:27:34 +0800559 size_t identity_len;
Jerry Yu82534862022-08-30 10:42:33 +0800560 uint32_t obfuscated_ticket_age;
Jerry Yu96a2e362022-07-21 15:11:34 +0800561 const unsigned char *binder;
Jerry Yu568ec252022-07-22 21:27:34 +0800562 size_t binder_len;
Jerry Yu5725f1c2022-08-21 17:27:16 +0800563 int psk_type;
564 uint16_t cipher_suite;
Jerry Yu29d9faa2022-08-23 17:52:45 +0800565 const mbedtls_ssl_ciphersuite_t *ciphersuite_info;
Jerry Yu82534862022-08-30 10:42:33 +0800566#if defined(MBEDTLS_SSL_SESSION_TICKETS)
567 mbedtls_ssl_session session;
Gilles Peskine449bd832023-01-11 14:50:10 +0100568 mbedtls_ssl_session_init(&session);
Jerry Yu82534862022-08-30 10:42:33 +0800569#endif
Jerry Yubb852022022-07-20 21:10:44 +0800570
Gilles Peskine449bd832023-01-11 14:50:10 +0100571 MBEDTLS_SSL_CHK_BUF_READ_PTR(p_identity_len, identities_end, 2 + 1 + 4);
572 identity_len = MBEDTLS_GET_UINT16_BE(p_identity_len, 0);
Jerry Yu568ec252022-07-22 21:27:34 +0800573 identity = p_identity_len + 2;
Gilles Peskine449bd832023-01-11 14:50:10 +0100574 MBEDTLS_SSL_CHK_BUF_READ_PTR(identity, identities_end, identity_len + 4);
575 obfuscated_ticket_age = MBEDTLS_GET_UINT32_BE(identity, identity_len);
Jerry Yu568ec252022-07-22 21:27:34 +0800576 p_identity_len += identity_len + 6;
Jerry Yu1c105562022-07-10 06:32:38 +0000577
Gilles Peskine449bd832023-01-11 14:50:10 +0100578 MBEDTLS_SSL_CHK_BUF_READ_PTR(p_binder_len, binders_end, 1 + 32);
Jerry Yu568ec252022-07-22 21:27:34 +0800579 binder_len = *p_binder_len;
580 binder = p_binder_len + 1;
Gilles Peskine449bd832023-01-11 14:50:10 +0100581 MBEDTLS_SSL_CHK_BUF_READ_PTR(binder, binders_end, binder_len);
Jerry Yu568ec252022-07-22 21:27:34 +0800582 p_binder_len += binder_len + 1;
Jerry Yu96a2e362022-07-21 15:11:34 +0800583
Jerry Yu96a2e362022-07-21 15:11:34 +0800584 identity_id++;
Gilles Peskine449bd832023-01-11 14:50:10 +0100585 if (matched_identity != -1) {
Jerry Yu1c105562022-07-10 06:32:38 +0000586 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +0100587 }
Jerry Yu1c105562022-07-10 06:32:38 +0000588
Jerry Yu96a2e362022-07-21 15:11:34 +0800589 ret = ssl_tls13_offered_psks_check_identity_match(
Gilles Peskine449bd832023-01-11 14:50:10 +0100590 ssl, identity, identity_len, obfuscated_ticket_age,
591 &psk_type, &session);
592 if (ret != SSL_TLS1_3_OFFERED_PSK_MATCH) {
Jerry Yu96a2e362022-07-21 15:11:34 +0800593 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +0100594 }
Jerry Yu96a2e362022-07-21 15:11:34 +0800595
Gilles Peskine449bd832023-01-11 14:50:10 +0100596 MBEDTLS_SSL_DEBUG_MSG(4, ("found matched identity"));
597 switch (psk_type) {
Jerry Yu0baf9072022-08-25 11:21:04 +0800598 case MBEDTLS_SSL_TLS1_3_PSK_EXTERNAL:
599 ret = ssl_tls13_select_ciphersuite_for_psk(
Gilles Peskine449bd832023-01-11 14:50:10 +0100600 ssl, ciphersuites, ciphersuites_end,
601 &cipher_suite, &ciphersuite_info);
Jerry Yu0baf9072022-08-25 11:21:04 +0800602 break;
603 case MBEDTLS_SSL_TLS1_3_PSK_RESUMPTION:
Jerry Yu82534862022-08-30 10:42:33 +0800604#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Jerry Yu0baf9072022-08-25 11:21:04 +0800605 ret = ssl_tls13_select_ciphersuite_for_resumption(
Gilles Peskine449bd832023-01-11 14:50:10 +0100606 ssl, ciphersuites, ciphersuites_end, &session,
607 &cipher_suite, &ciphersuite_info);
608 if (ret != 0) {
609 mbedtls_ssl_session_free(&session);
610 }
Jerry Yu82534862022-08-30 10:42:33 +0800611#else
612 ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
613#endif
Jerry Yu0baf9072022-08-25 11:21:04 +0800614 break;
615 default:
Gilles Peskine449bd832023-01-11 14:50:10 +0100616 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Jerry Yu0baf9072022-08-25 11:21:04 +0800617 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100618 if (ret != 0) {
Jerry Yuf35ba382022-08-23 17:58:26 +0800619 /* See below, no cipher_suite available, abort handshake */
620 MBEDTLS_SSL_PEND_FATAL_ALERT(
621 MBEDTLS_SSL_ALERT_MSG_DECRYPT_ERROR,
Gilles Peskine449bd832023-01-11 14:50:10 +0100622 MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE);
Jerry Yuf35ba382022-08-23 17:58:26 +0800623 MBEDTLS_SSL_DEBUG_RET(
Gilles Peskine449bd832023-01-11 14:50:10 +0100624 2, "ssl_tls13_select_ciphersuite", ret);
625 return ret;
Jerry Yu5725f1c2022-08-21 17:27:16 +0800626 }
627
Jerry Yu96a2e362022-07-21 15:11:34 +0800628 ret = ssl_tls13_offered_psks_check_binder_match(
Gilles Peskine449bd832023-01-11 14:50:10 +0100629 ssl, binder, binder_len, psk_type,
Dave Rodgman2eab4622023-10-05 13:30:37 +0100630 mbedtls_md_psa_alg_from_type((mbedtls_md_type_t) ciphersuite_info->mac));
Ronald Cron6e311272023-12-05 17:57:01 +0100631 if (ret != SSL_TLS1_3_BINDER_MATCH) {
Jerry Yuc5a23a02022-08-25 10:51:44 +0800632 /* For security reasons, the handshake should be aborted when we
633 * fail to validate a binder value. See RFC 8446 section 4.2.11.2
634 * and appendix E.6. */
Jerry Yu82534862022-08-30 10:42:33 +0800635#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Gilles Peskine449bd832023-01-11 14:50:10 +0100636 mbedtls_ssl_session_free(&session);
Jerry Yu82534862022-08-30 10:42:33 +0800637#endif
Gilles Peskine449bd832023-01-11 14:50:10 +0100638 MBEDTLS_SSL_DEBUG_MSG(3, ("Invalid binder."));
Xiaokang Qian9f1747b2023-03-30 02:50:04 +0000639 MBEDTLS_SSL_DEBUG_RET(
640 1, "ssl_tls13_offered_psks_check_binder_match", ret);
Jerry Yu96a2e362022-07-21 15:11:34 +0800641 MBEDTLS_SSL_PEND_FATAL_ALERT(
642 MBEDTLS_SSL_ALERT_MSG_DECRYPT_ERROR,
Gilles Peskine449bd832023-01-11 14:50:10 +0100643 MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE);
644 return ret;
Jerry Yu96a2e362022-07-21 15:11:34 +0800645 }
Jerry Yu96a2e362022-07-21 15:11:34 +0800646
647 matched_identity = identity_id;
Jerry Yu5725f1c2022-08-21 17:27:16 +0800648
649 /* Update handshake parameters */
Jerry Yue5834fd2022-08-29 20:16:09 +0800650 ssl->handshake->ciphersuite_info = ciphersuite_info;
Jerry Yu4746b102022-09-13 11:11:48 +0800651 ssl->session_negotiate->ciphersuite = cipher_suite;
Gilles Peskine449bd832023-01-11 14:50:10 +0100652 MBEDTLS_SSL_DEBUG_MSG(2, ("overwrite ciphersuite: %04x - %s",
653 cipher_suite, ciphersuite_info->name));
Jerry Yu82534862022-08-30 10:42:33 +0800654#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Gilles Peskine449bd832023-01-11 14:50:10 +0100655 if (psk_type == MBEDTLS_SSL_TLS1_3_PSK_RESUMPTION) {
656 ret = ssl_tls13_session_copy_ticket(ssl->session_negotiate,
657 &session);
658 mbedtls_ssl_session_free(&session);
659 if (ret != 0) {
660 return ret;
661 }
Jerry Yu82534862022-08-30 10:42:33 +0800662 }
663#endif /* MBEDTLS_SSL_SESSION_TICKETS */
Jerry Yu1c105562022-07-10 06:32:38 +0000664 }
665
Gilles Peskine449bd832023-01-11 14:50:10 +0100666 if (p_identity_len != identities_end || p_binder_len != binders_end) {
667 MBEDTLS_SSL_DEBUG_MSG(3, ("pre_shared_key extension decode error"));
668 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR,
669 MBEDTLS_ERR_SSL_DECODE_ERROR);
670 return MBEDTLS_ERR_SSL_DECODE_ERROR;
Jerry Yu1c105562022-07-10 06:32:38 +0000671 }
672
Jerry Yu6f1db3f2022-07-22 23:05:59 +0800673 /* Update the handshake transcript with the binder list. */
Xiaokang Qian9f1747b2023-03-30 02:50:04 +0000674 ret = ssl->handshake->update_checksum(
675 ssl, identities_end, (size_t) (binders_end - identities_end));
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +0100676 if (0 != ret) {
677 MBEDTLS_SSL_DEBUG_RET(1, ("update_checksum"), ret);
678 return ret;
679 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100680 if (matched_identity == -1) {
681 MBEDTLS_SSL_DEBUG_MSG(3, ("No matched PSK or ticket."));
682 return MBEDTLS_ERR_SSL_UNKNOWN_IDENTITY;
Jerry Yu1c105562022-07-10 06:32:38 +0000683 }
684
Gilles Peskine449bd832023-01-11 14:50:10 +0100685 ssl->handshake->selected_identity = (uint16_t) matched_identity;
686 MBEDTLS_SSL_DEBUG_MSG(3, ("Pre shared key found"));
Jerry Yu1c105562022-07-10 06:32:38 +0000687
Gilles Peskine449bd832023-01-11 14:50:10 +0100688 return 0;
Jerry Yu1c105562022-07-10 06:32:38 +0000689}
Jerry Yu032b15ce2022-07-11 06:10:03 +0000690
691/*
692 * struct {
693 * select ( Handshake.msg_type ) {
694 * ....
695 * case server_hello:
696 * uint16 selected_identity;
697 * }
698 * } PreSharedKeyExtension;
699 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100700static int ssl_tls13_write_server_pre_shared_key_ext(mbedtls_ssl_context *ssl,
701 unsigned char *buf,
702 unsigned char *end,
703 size_t *olen)
Jerry Yu032b15ce2022-07-11 06:10:03 +0000704{
Gilles Peskine449bd832023-01-11 14:50:10 +0100705 unsigned char *p = (unsigned char *) buf;
Jerry Yu032b15ce2022-07-11 06:10:03 +0000706
707 *olen = 0;
708
David Horstmann21b89762022-10-06 18:34:28 +0100709 int not_using_psk = 0;
Jerry Yu032b15ce2022-07-11 06:10:03 +0000710#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +0100711 not_using_psk = (mbedtls_svc_key_id_is_null(ssl->handshake->psk_opaque));
Jerry Yu032b15ce2022-07-11 06:10:03 +0000712#else
Gilles Peskine449bd832023-01-11 14:50:10 +0100713 not_using_psk = (ssl->handshake->psk == NULL);
Jerry Yu032b15ce2022-07-11 06:10:03 +0000714#endif
Gilles Peskine449bd832023-01-11 14:50:10 +0100715 if (not_using_psk) {
Jerry Yu032b15ce2022-07-11 06:10:03 +0000716 /* We shouldn't have called this extension writer unless we've
717 * chosen to use a PSK. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100718 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Jerry Yu032b15ce2022-07-11 06:10:03 +0000719 }
720
Gilles Peskine449bd832023-01-11 14:50:10 +0100721 MBEDTLS_SSL_DEBUG_MSG(3, ("server hello, adding pre_shared_key extension"));
722 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 6);
Jerry Yu032b15ce2022-07-11 06:10:03 +0000723
Gilles Peskine449bd832023-01-11 14:50:10 +0100724 MBEDTLS_PUT_UINT16_BE(MBEDTLS_TLS_EXT_PRE_SHARED_KEY, p, 0);
725 MBEDTLS_PUT_UINT16_BE(2, p, 2);
Jerry Yu032b15ce2022-07-11 06:10:03 +0000726
Gilles Peskine449bd832023-01-11 14:50:10 +0100727 MBEDTLS_PUT_UINT16_BE(ssl->handshake->selected_identity, p, 4);
Jerry Yu032b15ce2022-07-11 06:10:03 +0000728
729 *olen = 6;
730
Gilles Peskine449bd832023-01-11 14:50:10 +0100731 MBEDTLS_SSL_DEBUG_MSG(4, ("sent selected_identity: %u",
732 ssl->handshake->selected_identity));
Jerry Yu032b15ce2022-07-11 06:10:03 +0000733
Gilles Peskine449bd832023-01-11 14:50:10 +0100734 mbedtls_ssl_tls13_set_hs_sent_ext_mask(ssl, MBEDTLS_TLS_EXT_PRE_SHARED_KEY);
Jerry Yub95dd362022-11-08 21:19:34 +0800735
Gilles Peskine449bd832023-01-11 14:50:10 +0100736 return 0;
Jerry Yu032b15ce2022-07-11 06:10:03 +0000737}
738
Ronald Cron41a443a2022-10-04 16:38:25 +0200739#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED */
Jerry Yue19e3b92022-07-08 12:04:51 +0000740
XiaokangQian7807f9f2022-02-15 10:04:37 +0000741/* From RFC 8446:
742 * struct {
XiaokangQiancfd925f2022-04-14 07:10:37 +0000743 * ProtocolVersion versions<2..254>;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000744 * } SupportedVersions;
745 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200746MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100747static int ssl_tls13_parse_supported_versions_ext(mbedtls_ssl_context *ssl,
748 const unsigned char *buf,
749 const unsigned char *end)
XiaokangQian7807f9f2022-02-15 10:04:37 +0000750{
XiaokangQian7807f9f2022-02-15 10:04:37 +0000751 const unsigned char *p = buf;
XiaokangQian8f9dfe42022-04-15 02:52:39 +0000752 size_t versions_len;
XiaokangQian4080a7f2022-04-11 09:55:18 +0000753 const unsigned char *versions_end;
XiaokangQian0a1b54e2022-04-21 03:01:38 +0000754 uint16_t tls_version;
Ronald Cron5af4c7f2023-03-07 20:46:59 +0100755 int found_supported_version = 0;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000756
Gilles Peskine449bd832023-01-11 14:50:10 +0100757 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, 1);
XiaokangQian4080a7f2022-04-11 09:55:18 +0000758 versions_len = p[0];
XiaokangQian7807f9f2022-02-15 10:04:37 +0000759 p += 1;
760
Gilles Peskine449bd832023-01-11 14:50:10 +0100761 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, versions_len);
XiaokangQian4080a7f2022-04-11 09:55:18 +0000762 versions_end = p + versions_len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100763 while (p < versions_end) {
764 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, versions_end, 2);
765 tls_version = mbedtls_ssl_read_version(p, ssl->conf->transport);
XiaokangQianb67384d2022-04-19 00:02:38 +0000766 p += 2;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000767
Ronald Cron3bd2b022023-04-03 16:45:39 +0200768 if (MBEDTLS_SSL_VERSION_TLS1_3 == tls_version) {
Ronald Cron5af4c7f2023-03-07 20:46:59 +0100769 found_supported_version = 1;
770 break;
771 }
772
Ronald Cron3bd2b022023-04-03 16:45:39 +0200773 if ((MBEDTLS_SSL_VERSION_TLS1_2 == tls_version) &&
774 mbedtls_ssl_conf_is_tls12_enabled(ssl->conf)) {
Ronald Cron5af4c7f2023-03-07 20:46:59 +0100775 found_supported_version = 1;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000776 break;
777 }
XiaokangQian7807f9f2022-02-15 10:04:37 +0000778 }
779
Ronald Cron5af4c7f2023-03-07 20:46:59 +0100780 if (!found_supported_version) {
781 MBEDTLS_SSL_DEBUG_MSG(1, ("No supported version found."));
XiaokangQian7807f9f2022-02-15 10:04:37 +0000782
Gilles Peskine449bd832023-01-11 14:50:10 +0100783 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_PROTOCOL_VERSION,
784 MBEDTLS_ERR_SSL_BAD_PROTOCOL_VERSION);
785 return MBEDTLS_ERR_SSL_BAD_PROTOCOL_VERSION;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000786 }
787
Ronald Cron5af4c7f2023-03-07 20:46:59 +0100788 MBEDTLS_SSL_DEBUG_MSG(1, ("Negotiated version: [%04x]",
Gilles Peskine449bd832023-01-11 14:50:10 +0100789 (unsigned int) tls_version));
XiaokangQian7807f9f2022-02-15 10:04:37 +0000790
Ronald Cron5af4c7f2023-03-07 20:46:59 +0100791 return (int) tls_version;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000792}
793
Przemek Stekiel8c0a9532023-06-15 16:48:19 +0200794#if defined(PSA_WANT_ALG_ECDH) || defined(PSA_WANT_ALG_FFDH)
XiaokangQiane8ff3502022-04-22 02:34:40 +0000795/*
XiaokangQian7807f9f2022-02-15 10:04:37 +0000796 *
797 * From RFC 8446:
798 * enum {
799 * ... (0xFFFF)
800 * } NamedGroup;
801 * struct {
802 * NamedGroup named_group_list<2..2^16-1>;
803 * } NamedGroupList;
804 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200805MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100806static int ssl_tls13_parse_supported_groups_ext(mbedtls_ssl_context *ssl,
807 const unsigned char *buf,
808 const unsigned char *end)
XiaokangQian7807f9f2022-02-15 10:04:37 +0000809{
XiaokangQian7807f9f2022-02-15 10:04:37 +0000810 const unsigned char *p = buf;
XiaokangQian84823772022-04-19 07:57:30 +0000811 size_t named_group_list_len;
XiaokangQian8f9dfe42022-04-15 02:52:39 +0000812 const unsigned char *named_group_list_end;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000813
Gilles Peskine449bd832023-01-11 14:50:10 +0100814 MBEDTLS_SSL_DEBUG_BUF(3, "supported_groups extension", p, end - buf);
815 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, 2);
816 named_group_list_len = MBEDTLS_GET_UINT16_BE(p, 0);
XiaokangQian7807f9f2022-02-15 10:04:37 +0000817 p += 2;
Gilles Peskine449bd832023-01-11 14:50:10 +0100818 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, named_group_list_len);
XiaokangQian8f9dfe42022-04-15 02:52:39 +0000819 named_group_list_end = p + named_group_list_len;
XiaokangQian4e8cd7b2022-04-21 09:48:09 +0000820 ssl->handshake->hrr_selected_group = 0;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000821
Gilles Peskine449bd832023-01-11 14:50:10 +0100822 while (p < named_group_list_end) {
XiaokangQian08037552022-04-20 07:16:41 +0000823 uint16_t named_group;
Gilles Peskine449bd832023-01-11 14:50:10 +0100824 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, named_group_list_end, 2);
825 named_group = MBEDTLS_GET_UINT16_BE(p, 0);
XiaokangQian08037552022-04-20 07:16:41 +0000826 p += 2;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000827
Gilles Peskine449bd832023-01-11 14:50:10 +0100828 MBEDTLS_SSL_DEBUG_MSG(2,
829 ("got named group: %s(%04x)",
830 mbedtls_ssl_named_group_to_str(named_group),
831 named_group));
XiaokangQian08037552022-04-20 07:16:41 +0000832
Gilles Peskine449bd832023-01-11 14:50:10 +0100833 if (!mbedtls_ssl_named_group_is_offered(ssl, named_group) ||
834 !mbedtls_ssl_named_group_is_supported(named_group) ||
835 ssl->handshake->hrr_selected_group != 0) {
XiaokangQian08037552022-04-20 07:16:41 +0000836 continue;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000837 }
838
Gilles Peskine449bd832023-01-11 14:50:10 +0100839 MBEDTLS_SSL_DEBUG_MSG(2,
840 ("add named group %s(%04x) into received list.",
841 mbedtls_ssl_named_group_to_str(named_group),
842 named_group));
Jerry Yuc1be19f2022-04-23 16:11:39 +0800843
XiaokangQian4e8cd7b2022-04-21 09:48:09 +0000844 ssl->handshake->hrr_selected_group = named_group;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000845 }
846
Gilles Peskine449bd832023-01-11 14:50:10 +0100847 return 0;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000848
849}
Przemek Stekiel8c0a9532023-06-15 16:48:19 +0200850#endif /* PSA_WANT_ALG_ECDH || PSA_WANT_ALG_FFDH */
XiaokangQian7807f9f2022-02-15 10:04:37 +0000851
XiaokangQian08037552022-04-20 07:16:41 +0000852#define SSL_TLS1_3_PARSE_KEY_SHARES_EXT_NO_MATCH 1
853
Valerio Settic9ae8622023-07-25 11:23:50 +0200854#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_EPHEMERAL_ENABLED)
XiaokangQian7807f9f2022-02-15 10:04:37 +0000855/*
856 * ssl_tls13_parse_key_shares_ext() verifies whether the information in the
Xiaokang Qian9f1747b2023-03-30 02:50:04 +0000857 * extension is correct and stores the first acceptable key share and its
858 * associated group.
XiaokangQian7807f9f2022-02-15 10:04:37 +0000859 *
860 * Possible return values are:
861 * - 0: Successful processing of the client provided key share extension.
Xiaokang Qian9f1747b2023-03-30 02:50:04 +0000862 * - SSL_TLS1_3_PARSE_KEY_SHARES_EXT_NO_MATCH: The key shares provided by
863 * the client does not match a group supported by the server. A
864 * HelloRetryRequest will be needed.
XiaokangQiane8ff3502022-04-22 02:34:40 +0000865 * - A negative value for fatal errors.
Jerry Yuc1be19f2022-04-23 16:11:39 +0800866 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200867MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100868static int ssl_tls13_parse_key_shares_ext(mbedtls_ssl_context *ssl,
869 const unsigned char *buf,
870 const unsigned char *end)
XiaokangQian7807f9f2022-02-15 10:04:37 +0000871{
XiaokangQianb67384d2022-04-19 00:02:38 +0000872 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000873 unsigned char const *p = buf;
XiaokangQian8f9dfe42022-04-15 02:52:39 +0000874 unsigned char const *client_shares_end;
Jerry Yuc1be19f2022-04-23 16:11:39 +0800875 size_t client_shares_len;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000876
877 /* From RFC 8446:
878 *
879 * struct {
880 * KeyShareEntry client_shares<0..2^16-1>;
881 * } KeyShareClientHello;
882 *
883 */
884
Gilles Peskine449bd832023-01-11 14:50:10 +0100885 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, 2);
886 client_shares_len = MBEDTLS_GET_UINT16_BE(p, 0);
XiaokangQian7807f9f2022-02-15 10:04:37 +0000887 p += 2;
Gilles Peskine449bd832023-01-11 14:50:10 +0100888 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, client_shares_len);
XiaokangQian7807f9f2022-02-15 10:04:37 +0000889
890 ssl->handshake->offered_group_id = 0;
XiaokangQian8f9dfe42022-04-15 02:52:39 +0000891 client_shares_end = p + client_shares_len;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000892
893 /* We try to find a suitable key share entry and copy it to the
894 * handshake context. Later, we have to find out whether we can do
895 * something with the provided key share or whether we have to
XiaokangQianc5763b52022-04-02 03:34:37 +0000896 * dismiss it and send a HelloRetryRequest message.
897 */
XiaokangQian7807f9f2022-02-15 10:04:37 +0000898
Gilles Peskine449bd832023-01-11 14:50:10 +0100899 while (p < client_shares_end) {
XiaokangQian9b5d04b2022-04-10 10:20:43 +0000900 uint16_t group;
Jerry Yuc1be19f2022-04-23 16:11:39 +0800901 size_t key_exchange_len;
Jerry Yu086edc22022-05-05 10:50:38 +0800902 const unsigned char *key_exchange;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000903
904 /*
905 * struct {
906 * NamedGroup group;
907 * opaque key_exchange<1..2^16-1>;
908 * } KeyShareEntry;
909 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100910 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, client_shares_end, 4);
911 group = MBEDTLS_GET_UINT16_BE(p, 0);
912 key_exchange_len = MBEDTLS_GET_UINT16_BE(p, 2);
Jerry Yuc1be19f2022-04-23 16:11:39 +0800913 p += 4;
Jerry Yu086edc22022-05-05 10:50:38 +0800914 key_exchange = p;
Gilles Peskine449bd832023-01-11 14:50:10 +0100915 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, client_shares_end, key_exchange_len);
Jerry Yu086edc22022-05-05 10:50:38 +0800916 p += key_exchange_len;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000917
918 /* Continue parsing even if we have already found a match,
XiaokangQianc5763b52022-04-02 03:34:37 +0000919 * for input validation purposes.
920 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100921 if (!mbedtls_ssl_named_group_is_offered(ssl, group) ||
922 !mbedtls_ssl_named_group_is_supported(group) ||
923 ssl->handshake->offered_group_id != 0) {
XiaokangQian060d8672022-04-21 09:24:56 +0000924 continue;
925 }
XiaokangQian060d8672022-04-21 09:24:56 +0000926
XiaokangQian7807f9f2022-02-15 10:04:37 +0000927 /*
Przemek Stekielc89f3ea2023-05-18 15:45:53 +0200928 * ECDHE and FFDHE groups are supported
XiaokangQian7807f9f2022-02-15 10:04:37 +0000929 */
Przemek Stekielc89f3ea2023-05-18 15:45:53 +0200930 if (mbedtls_ssl_tls13_named_group_is_ecdhe(group) ||
Przemek Stekield5f79e72023-06-29 09:08:43 +0200931 mbedtls_ssl_tls13_named_group_is_ffdh(group)) {
Przemek Stekielc89f3ea2023-05-18 15:45:53 +0200932 MBEDTLS_SSL_DEBUG_MSG(2, ("ECDH/FFDH group: %s (%04x)",
Gilles Peskine449bd832023-01-11 14:50:10 +0100933 mbedtls_ssl_named_group_to_str(group),
934 group));
Przemek Stekiel7ac93be2023-07-04 10:02:38 +0200935 ret = mbedtls_ssl_tls13_read_public_xxdhe_share(
Gilles Peskine449bd832023-01-11 14:50:10 +0100936 ssl, key_exchange - 2, key_exchange_len + 2);
937 if (ret != 0) {
938 return ret;
939 }
XiaokangQian4e8cd7b2022-04-21 09:48:09 +0000940
Gilles Peskine449bd832023-01-11 14:50:10 +0100941 } else {
942 MBEDTLS_SSL_DEBUG_MSG(4, ("Unrecognized NamedGroup %u",
943 (unsigned) group));
XiaokangQian7807f9f2022-02-15 10:04:37 +0000944 continue;
945 }
946
XiaokangQian9b5d04b2022-04-10 10:20:43 +0000947 ssl->handshake->offered_group_id = group;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000948 }
949
Jerry Yuc1be19f2022-04-23 16:11:39 +0800950
Gilles Peskine449bd832023-01-11 14:50:10 +0100951 if (ssl->handshake->offered_group_id == 0) {
952 MBEDTLS_SSL_DEBUG_MSG(1, ("no matching key share"));
953 return SSL_TLS1_3_PARSE_KEY_SHARES_EXT_NO_MATCH;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000954 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100955 return 0;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000956}
Valerio Settic9ae8622023-07-25 11:23:50 +0200957#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_EPHEMERAL_ENABLED */
XiaokangQian7807f9f2022-02-15 10:04:37 +0000958
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200959MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100960static int ssl_tls13_client_hello_has_exts(mbedtls_ssl_context *ssl,
961 int exts_mask)
XiaokangQian7807f9f2022-02-15 10:04:37 +0000962{
Jerry Yu0c354a22022-08-29 15:25:36 +0800963 int masked = ssl->handshake->received_extensions & exts_mask;
Gilles Peskine449bd832023-01-11 14:50:10 +0100964 return masked == exts_mask;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000965}
966
Jerry Yue5991322022-11-07 14:03:44 +0800967#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200968MBEDTLS_CHECK_RETURN_CRITICAL
XiaokangQianb67384d2022-04-19 00:02:38 +0000969static int ssl_tls13_client_hello_has_exts_for_ephemeral_key_exchange(
Gilles Peskine449bd832023-01-11 14:50:10 +0100970 mbedtls_ssl_context *ssl)
XiaokangQian7807f9f2022-02-15 10:04:37 +0000971{
Gilles Peskine449bd832023-01-11 14:50:10 +0100972 return ssl_tls13_client_hello_has_exts(
973 ssl,
974 MBEDTLS_SSL_EXT_MASK(SUPPORTED_GROUPS) |
975 MBEDTLS_SSL_EXT_MASK(KEY_SHARE) |
976 MBEDTLS_SSL_EXT_MASK(SIG_ALG));
XiaokangQian7807f9f2022-02-15 10:04:37 +0000977}
Jerry Yue5991322022-11-07 14:03:44 +0800978#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED */
XiaokangQian7807f9f2022-02-15 10:04:37 +0000979
Jerry Yue5991322022-11-07 14:03:44 +0800980#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK_ENABLED)
Jerry Yu77f01482022-07-11 07:03:24 +0000981MBEDTLS_CHECK_RETURN_CRITICAL
982static int ssl_tls13_client_hello_has_exts_for_psk_key_exchange(
Gilles Peskine449bd832023-01-11 14:50:10 +0100983 mbedtls_ssl_context *ssl)
Jerry Yu77f01482022-07-11 07:03:24 +0000984{
Gilles Peskine449bd832023-01-11 14:50:10 +0100985 return ssl_tls13_client_hello_has_exts(
986 ssl,
987 MBEDTLS_SSL_EXT_MASK(PRE_SHARED_KEY) |
988 MBEDTLS_SSL_EXT_MASK(PSK_KEY_EXCHANGE_MODES));
Jerry Yu77f01482022-07-11 07:03:24 +0000989}
Jerry Yue5991322022-11-07 14:03:44 +0800990#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK_ENABLED */
Jerry Yu77f01482022-07-11 07:03:24 +0000991
Jerry Yue5991322022-11-07 14:03:44 +0800992#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK_EPHEMERAL_ENABLED)
Jerry Yu77f01482022-07-11 07:03:24 +0000993MBEDTLS_CHECK_RETURN_CRITICAL
994static int ssl_tls13_client_hello_has_exts_for_psk_ephemeral_key_exchange(
Gilles Peskine449bd832023-01-11 14:50:10 +0100995 mbedtls_ssl_context *ssl)
Jerry Yu77f01482022-07-11 07:03:24 +0000996{
Gilles Peskine449bd832023-01-11 14:50:10 +0100997 return ssl_tls13_client_hello_has_exts(
998 ssl,
999 MBEDTLS_SSL_EXT_MASK(SUPPORTED_GROUPS) |
1000 MBEDTLS_SSL_EXT_MASK(KEY_SHARE) |
1001 MBEDTLS_SSL_EXT_MASK(PRE_SHARED_KEY) |
1002 MBEDTLS_SSL_EXT_MASK(PSK_KEY_EXCHANGE_MODES));
Jerry Yu77f01482022-07-11 07:03:24 +00001003}
Jerry Yue5991322022-11-07 14:03:44 +08001004#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK_EPHEMERAL_ENABLED */
Jerry Yu77f01482022-07-11 07:03:24 +00001005
Pengyu Lv306a01d2023-02-02 16:35:47 +08001006#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED)
1007MBEDTLS_CHECK_RETURN_CRITICAL
Pengyu Lvd72e8582023-11-10 10:37:18 +08001008static int ssl_tls13_ticket_is_kex_mode_permitted(mbedtls_ssl_context *ssl,
1009 unsigned int kex_mode)
Pengyu Lv306a01d2023-02-02 16:35:47 +08001010{
1011#if defined(MBEDTLS_SSL_SESSION_TICKETS)
1012 if (ssl->handshake->resume) {
Pengyu Lv94a42cc2023-12-06 10:04:17 +08001013 if (!mbedtls_ssl_tls13_session_ticket_has_flags(
Pengyu Lv306a01d2023-02-02 16:35:47 +08001014 ssl->session_negotiate, kex_mode)) {
1015 return 0;
1016 }
1017 }
1018#else
1019 ((void) ssl);
1020 ((void) kex_mode);
1021#endif
1022 return 1;
1023}
1024#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED */
1025
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001026MBEDTLS_CHECK_RETURN_CRITICAL
Pengyu Lvbc4aab72023-12-01 15:37:24 +08001027static int ssl_tls13_key_exchange_is_ephemeral_available(mbedtls_ssl_context *ssl)
XiaokangQian7807f9f2022-02-15 10:04:37 +00001028{
Jerry Yue5991322022-11-07 14:03:44 +08001029#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED)
Pengyu Lv0a1ff2b2023-11-14 11:03:32 +08001030 return mbedtls_ssl_conf_tls13_is_ephemeral_enabled(ssl) &&
Gilles Peskine449bd832023-01-11 14:50:10 +01001031 ssl_tls13_client_hello_has_exts_for_ephemeral_key_exchange(ssl);
Jerry Yue5991322022-11-07 14:03:44 +08001032#else
1033 ((void) ssl);
Gilles Peskine449bd832023-01-11 14:50:10 +01001034 return 0;
Jerry Yue5991322022-11-07 14:03:44 +08001035#endif
Jerry Yu77f01482022-07-11 07:03:24 +00001036}
XiaokangQian7807f9f2022-02-15 10:04:37 +00001037
Jerry Yu77f01482022-07-11 07:03:24 +00001038MBEDTLS_CHECK_RETURN_CRITICAL
Pengyu Lvbc4aab72023-12-01 15:37:24 +08001039static int ssl_tls13_key_exchange_is_psk_available(mbedtls_ssl_context *ssl)
Jerry Yu77f01482022-07-11 07:03:24 +00001040{
Jerry Yue5991322022-11-07 14:03:44 +08001041#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK_ENABLED)
Pengyu Lvd72e8582023-11-10 10:37:18 +08001042 return ssl_tls13_ticket_is_kex_mode_permitted(
Pengyu Lv306a01d2023-02-02 16:35:47 +08001043 ssl, MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK) &&
Pengyu Lv0a1ff2b2023-11-14 11:03:32 +08001044 mbedtls_ssl_conf_tls13_is_psk_enabled(ssl) &&
Pengyu Lvb2cfafb2023-11-14 13:56:13 +08001045 mbedtls_ssl_tls13_is_psk_supported(ssl) &&
Gilles Peskine449bd832023-01-11 14:50:10 +01001046 ssl_tls13_client_hello_has_exts_for_psk_key_exchange(ssl);
Jerry Yu77f01482022-07-11 07:03:24 +00001047#else
1048 ((void) ssl);
Gilles Peskine449bd832023-01-11 14:50:10 +01001049 return 0;
Jerry Yu77f01482022-07-11 07:03:24 +00001050#endif
1051}
XiaokangQian7807f9f2022-02-15 10:04:37 +00001052
Jerry Yu77f01482022-07-11 07:03:24 +00001053MBEDTLS_CHECK_RETURN_CRITICAL
Pengyu Lvbc4aab72023-12-01 15:37:24 +08001054static int ssl_tls13_key_exchange_is_psk_ephemeral_available(mbedtls_ssl_context *ssl)
Jerry Yu77f01482022-07-11 07:03:24 +00001055{
Jerry Yue5991322022-11-07 14:03:44 +08001056#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK_EPHEMERAL_ENABLED)
Pengyu Lvd72e8582023-11-10 10:37:18 +08001057 return ssl_tls13_ticket_is_kex_mode_permitted(
Pengyu Lv306a01d2023-02-02 16:35:47 +08001058 ssl, MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK_EPHEMERAL) &&
Pengyu Lv0a1ff2b2023-11-14 11:03:32 +08001059 mbedtls_ssl_conf_tls13_is_psk_ephemeral_enabled(ssl) &&
Pengyu Lvb2cfafb2023-11-14 13:56:13 +08001060 mbedtls_ssl_tls13_is_psk_ephemeral_supported(ssl) &&
Gilles Peskine449bd832023-01-11 14:50:10 +01001061 ssl_tls13_client_hello_has_exts_for_psk_ephemeral_key_exchange(ssl);
Jerry Yu77f01482022-07-11 07:03:24 +00001062#else
1063 ((void) ssl);
Gilles Peskine449bd832023-01-11 14:50:10 +01001064 return 0;
Jerry Yu77f01482022-07-11 07:03:24 +00001065#endif
1066}
1067
Gilles Peskine449bd832023-01-11 14:50:10 +01001068static int ssl_tls13_determine_key_exchange_mode(mbedtls_ssl_context *ssl)
Jerry Yu77f01482022-07-11 07:03:24 +00001069{
1070 /*
1071 * Determine the key exchange algorithm to use.
1072 * There are three types of key exchanges supported in TLS 1.3:
1073 * - (EC)DH with ECDSA,
1074 * - (EC)DH with PSK,
1075 * - plain PSK.
1076 *
1077 * The PSK-based key exchanges may additionally be used with 0-RTT.
1078 *
1079 * Our built-in order of preference is
Jerry Yuce6ed702022-07-22 21:49:53 +08001080 * 1 ) (EC)DHE-PSK Mode ( psk_ephemeral )
1081 * 2 ) Certificate Mode ( ephemeral )
1082 * 3 ) Plain PSK Mode ( psk )
Jerry Yu77f01482022-07-11 07:03:24 +00001083 */
1084
Xiaokang Qian9f1747b2023-03-30 02:50:04 +00001085 ssl->handshake->key_exchange_mode =
1086 MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_NONE;
Jerry Yu77f01482022-07-11 07:03:24 +00001087
Pengyu Lvbc4aab72023-12-01 15:37:24 +08001088 if (ssl_tls13_key_exchange_is_psk_ephemeral_available(ssl)) {
Jerry Yu77f01482022-07-11 07:03:24 +00001089 ssl->handshake->key_exchange_mode =
1090 MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK_EPHEMERAL;
Gilles Peskine449bd832023-01-11 14:50:10 +01001091 MBEDTLS_SSL_DEBUG_MSG(2, ("key exchange mode: psk_ephemeral"));
1092 } else
Pengyu Lvbc4aab72023-12-01 15:37:24 +08001093 if (ssl_tls13_key_exchange_is_ephemeral_available(ssl)) {
Jerry Yu77f01482022-07-11 07:03:24 +00001094 ssl->handshake->key_exchange_mode =
1095 MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL;
Gilles Peskine449bd832023-01-11 14:50:10 +01001096 MBEDTLS_SSL_DEBUG_MSG(2, ("key exchange mode: ephemeral"));
1097 } else
Pengyu Lvbc4aab72023-12-01 15:37:24 +08001098 if (ssl_tls13_key_exchange_is_psk_available(ssl)) {
Jerry Yuce6ed702022-07-22 21:49:53 +08001099 ssl->handshake->key_exchange_mode =
1100 MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK;
Gilles Peskine449bd832023-01-11 14:50:10 +01001101 MBEDTLS_SSL_DEBUG_MSG(2, ("key exchange mode: psk"));
1102 } else {
Jerry Yu77f01482022-07-11 07:03:24 +00001103 MBEDTLS_SSL_DEBUG_MSG(
Gilles Peskine449bd832023-01-11 14:50:10 +01001104 1,
1105 ("ClientHello message misses mandatory extensions."));
1106 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_MISSING_EXTENSION,
1107 MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER);
1108 return MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER;
Jerry Yu77f01482022-07-11 07:03:24 +00001109 }
1110
Gilles Peskine449bd832023-01-11 14:50:10 +01001111 return 0;
Jerry Yu77f01482022-07-11 07:03:24 +00001112
XiaokangQian7807f9f2022-02-15 10:04:37 +00001113}
1114
XiaokangQian81802f42022-06-10 13:25:22 +00001115#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
Ronald Cron928cbd32022-10-04 16:14:26 +02001116 defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED)
Ronald Cron67ea2542022-09-15 17:34:42 +02001117
1118#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01001119static psa_algorithm_t ssl_tls13_iana_sig_alg_to_psa_alg(uint16_t sig_alg)
Ronald Cron67ea2542022-09-15 17:34:42 +02001120{
Gilles Peskine449bd832023-01-11 14:50:10 +01001121 switch (sig_alg) {
Ronald Cron67ea2542022-09-15 17:34:42 +02001122 case MBEDTLS_TLS1_3_SIG_ECDSA_SECP256R1_SHA256:
Gilles Peskine449bd832023-01-11 14:50:10 +01001123 return PSA_ALG_ECDSA(PSA_ALG_SHA_256);
Ronald Cron67ea2542022-09-15 17:34:42 +02001124 case MBEDTLS_TLS1_3_SIG_ECDSA_SECP384R1_SHA384:
Gilles Peskine449bd832023-01-11 14:50:10 +01001125 return PSA_ALG_ECDSA(PSA_ALG_SHA_384);
Ronald Cron67ea2542022-09-15 17:34:42 +02001126 case MBEDTLS_TLS1_3_SIG_ECDSA_SECP521R1_SHA512:
Gilles Peskine449bd832023-01-11 14:50:10 +01001127 return PSA_ALG_ECDSA(PSA_ALG_SHA_512);
Ronald Cron67ea2542022-09-15 17:34:42 +02001128 case MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA256:
Gilles Peskine449bd832023-01-11 14:50:10 +01001129 return PSA_ALG_RSA_PSS(PSA_ALG_SHA_256);
Ronald Cron67ea2542022-09-15 17:34:42 +02001130 case MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA384:
Gilles Peskine449bd832023-01-11 14:50:10 +01001131 return PSA_ALG_RSA_PSS(PSA_ALG_SHA_384);
Ronald Cron67ea2542022-09-15 17:34:42 +02001132 case MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA512:
Gilles Peskine449bd832023-01-11 14:50:10 +01001133 return PSA_ALG_RSA_PSS(PSA_ALG_SHA_512);
Ronald Cron67ea2542022-09-15 17:34:42 +02001134 case MBEDTLS_TLS1_3_SIG_RSA_PKCS1_SHA256:
Gilles Peskine449bd832023-01-11 14:50:10 +01001135 return PSA_ALG_RSA_PKCS1V15_SIGN(PSA_ALG_SHA_256);
Ronald Cron67ea2542022-09-15 17:34:42 +02001136 case MBEDTLS_TLS1_3_SIG_RSA_PKCS1_SHA384:
Gilles Peskine449bd832023-01-11 14:50:10 +01001137 return PSA_ALG_RSA_PKCS1V15_SIGN(PSA_ALG_SHA_384);
Ronald Cron67ea2542022-09-15 17:34:42 +02001138 case MBEDTLS_TLS1_3_SIG_RSA_PKCS1_SHA512:
Gilles Peskine449bd832023-01-11 14:50:10 +01001139 return PSA_ALG_RSA_PKCS1V15_SIGN(PSA_ALG_SHA_512);
Ronald Cron67ea2542022-09-15 17:34:42 +02001140 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01001141 return PSA_ALG_NONE;
Ronald Cron67ea2542022-09-15 17:34:42 +02001142 }
1143}
1144#endif /* MBEDTLS_USE_PSA_CRYPTO */
1145
XiaokangQian23c5be62022-06-07 02:04:34 +00001146/*
XiaokangQianfb665a82022-06-15 03:57:21 +00001147 * Pick best ( private key, certificate chain ) pair based on the signature
1148 * algorithms supported by the client.
XiaokangQian23c5be62022-06-07 02:04:34 +00001149 */
Ronald Cronce7d76e2022-07-08 18:56:49 +02001150MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01001151static int ssl_tls13_pick_key_cert(mbedtls_ssl_context *ssl)
XiaokangQian23c5be62022-06-07 02:04:34 +00001152{
XiaokangQianfb665a82022-06-15 03:57:21 +00001153 mbedtls_ssl_key_cert *key_cert, *key_cert_list;
XiaokangQian81802f42022-06-10 13:25:22 +00001154 const uint16_t *sig_alg = ssl->handshake->received_sig_algs;
XiaokangQian23c5be62022-06-07 02:04:34 +00001155
1156#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Gilles Peskine449bd832023-01-11 14:50:10 +01001157 if (ssl->handshake->sni_key_cert != NULL) {
XiaokangQianfb665a82022-06-15 03:57:21 +00001158 key_cert_list = ssl->handshake->sni_key_cert;
Gilles Peskine449bd832023-01-11 14:50:10 +01001159 } else
XiaokangQian23c5be62022-06-07 02:04:34 +00001160#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
Gilles Peskine449bd832023-01-11 14:50:10 +01001161 key_cert_list = ssl->conf->key_cert;
XiaokangQian23c5be62022-06-07 02:04:34 +00001162
Gilles Peskine449bd832023-01-11 14:50:10 +01001163 if (key_cert_list == NULL) {
1164 MBEDTLS_SSL_DEBUG_MSG(3, ("server has no certificate"));
1165 return -1;
XiaokangQian23c5be62022-06-07 02:04:34 +00001166 }
1167
Gilles Peskine449bd832023-01-11 14:50:10 +01001168 for (; *sig_alg != MBEDTLS_TLS1_3_SIG_NONE; sig_alg++) {
1169 if (!mbedtls_ssl_sig_alg_is_offered(ssl, *sig_alg)) {
Ronald Cron67ea2542022-09-15 17:34:42 +02001170 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +01001171 }
Ronald Cron67ea2542022-09-15 17:34:42 +02001172
Gilles Peskine449bd832023-01-11 14:50:10 +01001173 if (!mbedtls_ssl_tls13_sig_alg_for_cert_verify_is_supported(*sig_alg)) {
Ronald Cron67ea2542022-09-15 17:34:42 +02001174 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +01001175 }
Ronald Cron67ea2542022-09-15 17:34:42 +02001176
Gilles Peskine449bd832023-01-11 14:50:10 +01001177 for (key_cert = key_cert_list; key_cert != NULL;
1178 key_cert = key_cert->next) {
Ronald Cron67ea2542022-09-15 17:34:42 +02001179#if defined(MBEDTLS_USE_PSA_CRYPTO)
1180 psa_algorithm_t psa_alg = PSA_ALG_NONE;
1181#endif /* MBEDTLS_USE_PSA_CRYPTO */
1182
Gilles Peskine449bd832023-01-11 14:50:10 +01001183 MBEDTLS_SSL_DEBUG_CRT(3, "certificate (chain) candidate",
1184 key_cert->cert);
XiaokangQian81802f42022-06-10 13:25:22 +00001185
1186 /*
Gilles Peskine449bd832023-01-11 14:50:10 +01001187 * This avoids sending the client a cert it'll reject based on
1188 * keyUsage or other extensions.
1189 */
1190 if (mbedtls_x509_crt_check_key_usage(
1191 key_cert->cert, MBEDTLS_X509_KU_DIGITAL_SIGNATURE) != 0 ||
XiaokangQian81802f42022-06-10 13:25:22 +00001192 mbedtls_x509_crt_check_extended_key_usage(
XiaokangQianfb665a82022-06-15 03:57:21 +00001193 key_cert->cert, MBEDTLS_OID_SERVER_AUTH,
Gilles Peskine449bd832023-01-11 14:50:10 +01001194 MBEDTLS_OID_SIZE(MBEDTLS_OID_SERVER_AUTH)) != 0) {
1195 MBEDTLS_SSL_DEBUG_MSG(3, ("certificate mismatch: "
1196 "(extended) key usage extension"));
XiaokangQian81802f42022-06-10 13:25:22 +00001197 continue;
1198 }
XiaokangQian23c5be62022-06-07 02:04:34 +00001199
Gilles Peskine449bd832023-01-11 14:50:10 +01001200 MBEDTLS_SSL_DEBUG_MSG(3,
1201 ("ssl_tls13_pick_key_cert:"
1202 "check signature algorithm %s [%04x]",
1203 mbedtls_ssl_sig_alg_to_str(*sig_alg),
1204 *sig_alg));
Ronald Cron67ea2542022-09-15 17:34:42 +02001205#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01001206 psa_alg = ssl_tls13_iana_sig_alg_to_psa_alg(*sig_alg);
Ronald Cron67ea2542022-09-15 17:34:42 +02001207#endif /* MBEDTLS_USE_PSA_CRYPTO */
1208
Gilles Peskine449bd832023-01-11 14:50:10 +01001209 if (mbedtls_ssl_tls13_check_sig_alg_cert_key_match(
1210 *sig_alg, &key_cert->cert->pk)
Ronald Cron67ea2542022-09-15 17:34:42 +02001211#if defined(MBEDTLS_USE_PSA_CRYPTO)
1212 && psa_alg != PSA_ALG_NONE &&
Gilles Peskine449bd832023-01-11 14:50:10 +01001213 mbedtls_pk_can_do_ext(&key_cert->cert->pk, psa_alg,
1214 PSA_KEY_USAGE_SIGN_HASH) == 1
Ronald Cron67ea2542022-09-15 17:34:42 +02001215#endif /* MBEDTLS_USE_PSA_CRYPTO */
Gilles Peskine449bd832023-01-11 14:50:10 +01001216 ) {
XiaokangQianfb665a82022-06-15 03:57:21 +00001217 ssl->handshake->key_cert = key_cert;
Gilles Peskine449bd832023-01-11 14:50:10 +01001218 MBEDTLS_SSL_DEBUG_MSG(3,
1219 ("ssl_tls13_pick_key_cert:"
1220 "selected signature algorithm"
1221 " %s [%04x]",
1222 mbedtls_ssl_sig_alg_to_str(*sig_alg),
1223 *sig_alg));
XiaokangQianfb665a82022-06-15 03:57:21 +00001224 MBEDTLS_SSL_DEBUG_CRT(
Gilles Peskine449bd832023-01-11 14:50:10 +01001225 3, "selected certificate (chain)",
1226 ssl->handshake->key_cert->cert);
1227 return 0;
XiaokangQian81802f42022-06-10 13:25:22 +00001228 }
XiaokangQian81802f42022-06-10 13:25:22 +00001229 }
XiaokangQian23c5be62022-06-07 02:04:34 +00001230 }
1231
Gilles Peskine449bd832023-01-11 14:50:10 +01001232 MBEDTLS_SSL_DEBUG_MSG(2, ("ssl_tls13_pick_key_cert:"
1233 "no suitable certificate found"));
1234 return -1;
XiaokangQian23c5be62022-06-07 02:04:34 +00001235}
XiaokangQian81802f42022-06-10 13:25:22 +00001236#endif /* MBEDTLS_X509_CRT_PARSE_C &&
Ronald Cron928cbd32022-10-04 16:14:26 +02001237 MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED */
XiaokangQian23c5be62022-06-07 02:04:34 +00001238
XiaokangQian4080a7f2022-04-11 09:55:18 +00001239/*
XiaokangQianed582dd2022-04-13 08:21:05 +00001240 *
1241 * STATE HANDLING: ClientHello
1242 *
XiaokangQianb67384d2022-04-19 00:02:38 +00001243 * There are three possible classes of outcomes when parsing the ClientHello:
XiaokangQianed582dd2022-04-13 08:21:05 +00001244 *
XiaokangQianb67384d2022-04-19 00:02:38 +00001245 * 1) The ClientHello was well-formed and matched the server's configuration.
XiaokangQianed582dd2022-04-13 08:21:05 +00001246 *
1247 * In this case, the server progresses to sending its ServerHello.
1248 *
XiaokangQianb67384d2022-04-19 00:02:38 +00001249 * 2) The ClientHello was well-formed but didn't match the server's
1250 * configuration.
XiaokangQianed582dd2022-04-13 08:21:05 +00001251 *
1252 * For example, the client might not have offered a key share which
1253 * the server supports, or the server might require a cookie.
1254 *
1255 * In this case, the server sends a HelloRetryRequest.
1256 *
XiaokangQianb67384d2022-04-19 00:02:38 +00001257 * 3) The ClientHello was ill-formed
XiaokangQianed582dd2022-04-13 08:21:05 +00001258 *
1259 * In this case, we abort the handshake.
1260 *
1261 */
1262
1263/*
XiaokangQian4080a7f2022-04-11 09:55:18 +00001264 * Structure of this message:
1265 *
XiaokangQiane8ff3502022-04-22 02:34:40 +00001266 * uint16 ProtocolVersion;
1267 * opaque Random[32];
1268 * uint8 CipherSuite[2]; // Cryptographic suite selector
XiaokangQian4080a7f2022-04-11 09:55:18 +00001269 *
XiaokangQiane8ff3502022-04-22 02:34:40 +00001270 * struct {
1271 * ProtocolVersion legacy_version = 0x0303; // TLS v1.2
1272 * Random random;
1273 * opaque legacy_session_id<0..32>;
1274 * CipherSuite cipher_suites<2..2^16-2>;
1275 * opaque legacy_compression_methods<1..2^8-1>;
1276 * Extension extensions<8..2^16-1>;
1277 * } ClientHello;
XiaokangQian4080a7f2022-04-11 09:55:18 +00001278 */
XiaokangQianed582dd2022-04-13 08:21:05 +00001279
1280#define SSL_CLIENT_HELLO_OK 0
1281#define SSL_CLIENT_HELLO_HRR_REQUIRED 1
Ronald Cron5af4c7f2023-03-07 20:46:59 +01001282#define SSL_CLIENT_HELLO_TLS1_2 2
XiaokangQianed582dd2022-04-13 08:21:05 +00001283
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001284MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01001285static int ssl_tls13_parse_client_hello(mbedtls_ssl_context *ssl,
1286 const unsigned char *buf,
1287 const unsigned char *end)
XiaokangQian7807f9f2022-02-15 10:04:37 +00001288{
XiaokangQianb67384d2022-04-19 00:02:38 +00001289 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1290 const unsigned char *p = buf;
Ronald Crond540d992023-03-07 09:41:48 +01001291 const unsigned char *random;
XiaokangQian4080a7f2022-04-11 09:55:18 +00001292 size_t legacy_session_id_len;
Ronald Croncada4102023-03-07 09:51:39 +01001293 const unsigned char *legacy_session_id;
XiaokangQian318dc762022-04-20 09:43:51 +00001294 size_t cipher_suites_len;
Ronald Cron2f16b4e2023-03-07 10:07:32 +01001295 const unsigned char *cipher_suites;
XiaokangQian060d8672022-04-21 09:24:56 +00001296 const unsigned char *cipher_suites_end;
XiaokangQianb67384d2022-04-19 00:02:38 +00001297 size_t extensions_len;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001298 const unsigned char *extensions_end;
Ronald Croneff56732023-04-03 17:36:31 +02001299 const unsigned char *supported_versions_data;
1300 const unsigned char *supported_versions_data_end;
Jerry Yuc4bf5d62022-10-29 09:08:47 +08001301 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
Jerry Yu49ca9282022-05-05 11:05:22 +08001302 int hrr_required = 0;
Ronald Cron8a74f072023-06-14 17:59:29 +02001303 int no_usable_share_for_key_agreement = 0;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001304
Ronald Cron41a443a2022-10-04 16:38:25 +02001305#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED)
Jerry Yu29d9faa2022-08-23 17:52:45 +08001306 const unsigned char *pre_shared_key_ext = NULL;
Jerry Yu1c105562022-07-10 06:32:38 +00001307 const unsigned char *pre_shared_key_ext_end = NULL;
Ronald Cron41a443a2022-10-04 16:38:25 +02001308#endif
XiaokangQian7807f9f2022-02-15 10:04:37 +00001309
XiaokangQian7807f9f2022-02-15 10:04:37 +00001310 /*
XiaokangQianb67384d2022-04-19 00:02:38 +00001311 * ClientHello layout:
XiaokangQian7807f9f2022-02-15 10:04:37 +00001312 * 0 . 1 protocol version
XiaokangQiane8ff3502022-04-22 02:34:40 +00001313 * 2 . 33 random bytes
XiaokangQianc5763b52022-04-02 03:34:37 +00001314 * 34 . 34 session id length ( 1 byte )
XiaokangQian7807f9f2022-02-15 10:04:37 +00001315 * 35 . 34+x session id
XiaokangQian7807f9f2022-02-15 10:04:37 +00001316 * .. . .. ciphersuite list length ( 2 bytes )
1317 * .. . .. ciphersuite list
1318 * .. . .. compression alg. list length ( 1 byte )
1319 * .. . .. compression alg. list
1320 * .. . .. extensions length ( 2 bytes, optional )
1321 * .. . .. extensions ( optional )
1322 */
1323
XiaokangQianb67384d2022-04-19 00:02:38 +00001324 /*
bootstrap-prime6dbbf442022-05-17 19:30:44 -04001325 * Minimal length ( with everything empty and extensions omitted ) is
XiaokangQian7807f9f2022-02-15 10:04:37 +00001326 * 2 + 32 + 1 + 2 + 1 = 38 bytes. Check that first, so that we can
1327 * read at least up to session id length without worrying.
1328 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001329 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, 38);
XiaokangQian7807f9f2022-02-15 10:04:37 +00001330
1331 /* ...
1332 * ProtocolVersion legacy_version = 0x0303; // TLS 1.2
1333 * ...
1334 * with ProtocolVersion defined as:
1335 * uint16 ProtocolVersion;
1336 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001337 if (mbedtls_ssl_read_version(p, ssl->conf->transport) !=
1338 MBEDTLS_SSL_VERSION_TLS1_2) {
1339 MBEDTLS_SSL_DEBUG_MSG(1, ("Unsupported version of TLS."));
1340 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_PROTOCOL_VERSION,
1341 MBEDTLS_ERR_SSL_BAD_PROTOCOL_VERSION);
1342 return MBEDTLS_ERR_SSL_BAD_PROTOCOL_VERSION;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001343 }
1344 p += 2;
1345
Jerry Yuc1be19f2022-04-23 16:11:39 +08001346 /* ...
1347 * Random random;
1348 * ...
XiaokangQianb67384d2022-04-19 00:02:38 +00001349 * with Random defined as:
1350 * opaque Random[32];
XiaokangQian7807f9f2022-02-15 10:04:37 +00001351 */
Ronald Crond540d992023-03-07 09:41:48 +01001352 random = p;
XiaokangQian08037552022-04-20 07:16:41 +00001353 p += MBEDTLS_CLIENT_HELLO_RANDOM_LEN;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001354
Jerry Yuc1be19f2022-04-23 16:11:39 +08001355 /* ...
XiaokangQianb67384d2022-04-19 00:02:38 +00001356 * opaque legacy_session_id<0..32>;
Jerry Yuc1be19f2022-04-23 16:11:39 +08001357 * ...
XiaokangQian7807f9f2022-02-15 10:04:37 +00001358 */
Ronald Croncada4102023-03-07 09:51:39 +01001359 legacy_session_id_len = *(p++);
1360 legacy_session_id = p;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001361
XiaokangQianb67384d2022-04-19 00:02:38 +00001362 /*
1363 * Check we have enough data for the legacy session identifier
Jerry Yue95c8af2022-07-26 15:48:20 +08001364 * and the ciphersuite list length.
XiaokangQianb67384d2022-04-19 00:02:38 +00001365 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001366 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, legacy_session_id_len + 2);
XiaokangQian4080a7f2022-04-11 09:55:18 +00001367 p += legacy_session_id_len;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001368
Ronald Cron2f16b4e2023-03-07 10:07:32 +01001369 /* ...
1370 * CipherSuite cipher_suites<2..2^16-2>;
1371 * ...
1372 * with CipherSuite defined as:
1373 * uint8 CipherSuite[2];
1374 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001375 cipher_suites_len = MBEDTLS_GET_UINT16_BE(p, 0);
XiaokangQian7807f9f2022-02-15 10:04:37 +00001376 p += 2;
Ronald Cron2f16b4e2023-03-07 10:07:32 +01001377 cipher_suites = p;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001378
Ronald Cronfc7ae872023-02-16 15:32:19 +01001379 /*
1380 * The length of the ciphersuite list has to be even.
1381 */
1382 if (cipher_suites_len & 1) {
1383 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR,
1384 MBEDTLS_ERR_SSL_DECODE_ERROR);
1385 return MBEDTLS_ERR_SSL_DECODE_ERROR;
1386 }
1387
XiaokangQianb67384d2022-04-19 00:02:38 +00001388 /* Check we have enough data for the ciphersuite list, the legacy
1389 * compression methods and the length of the extensions.
Jerry Yue95c8af2022-07-26 15:48:20 +08001390 *
1391 * cipher_suites cipher_suites_len bytes
1392 * legacy_compression_methods 2 bytes
1393 * extensions_len 2 bytes
XiaokangQianb67384d2022-04-19 00:02:38 +00001394 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001395 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, cipher_suites_len + 2 + 2);
Ronald Cron8c527d02023-03-07 15:47:47 +01001396 p += cipher_suites_len;
1397 cipher_suites_end = p;
Jerry Yu5725f1c2022-08-21 17:27:16 +08001398
1399 /*
Ronald Cron8c527d02023-03-07 15:47:47 +01001400 * Search for the supported versions extension and parse it to determine
1401 * if the client supports TLS 1.3.
1402 */
1403 ret = mbedtls_ssl_tls13_is_supported_versions_ext_present_in_exts(
1404 ssl, p + 2, end,
Ronald Croneff56732023-04-03 17:36:31 +02001405 &supported_versions_data, &supported_versions_data_end);
Ronald Cron8c527d02023-03-07 15:47:47 +01001406 if (ret < 0) {
1407 MBEDTLS_SSL_DEBUG_RET(1,
1408 ("mbedtls_ssl_tls13_is_supported_versions_ext_present_in_exts"), ret);
1409 return ret;
1410 }
1411
1412 if (ret == 0) {
Ronald Cron5af4c7f2023-03-07 20:46:59 +01001413 return SSL_CLIENT_HELLO_TLS1_2;
Ronald Cron8c527d02023-03-07 15:47:47 +01001414 }
1415
Ronald Cron5af4c7f2023-03-07 20:46:59 +01001416 if (ret == 1) {
1417 ret = ssl_tls13_parse_supported_versions_ext(ssl,
Ronald Croneff56732023-04-03 17:36:31 +02001418 supported_versions_data,
1419 supported_versions_data_end);
Ronald Cron5af4c7f2023-03-07 20:46:59 +01001420 if (ret < 0) {
1421 MBEDTLS_SSL_DEBUG_RET(1,
1422 ("ssl_tls13_parse_supported_versions_ext"), ret);
1423 return ret;
1424 }
1425
Ronald Cronb828c7d2023-04-03 16:37:22 +02001426 /*
1427 * The supported versions extension was parsed successfully as the
1428 * value returned by ssl_tls13_parse_supported_versions_ext() is
1429 * positive. The return value is then equal to
1430 * MBEDTLS_SSL_VERSION_TLS1_2 or MBEDTLS_SSL_VERSION_TLS1_3, defining
1431 * the TLS version to negotiate.
1432 */
Ronald Cron5af4c7f2023-03-07 20:46:59 +01001433 if (MBEDTLS_SSL_VERSION_TLS1_2 == ret) {
1434 return SSL_CLIENT_HELLO_TLS1_2;
1435 }
Ronald Cron8c527d02023-03-07 15:47:47 +01001436 }
1437
1438 /*
1439 * We negotiate TLS 1.3.
Ronald Cron64582392023-03-07 09:21:40 +01001440 */
1441 ssl->tls_version = MBEDTLS_SSL_VERSION_TLS1_3;
Ronald Cron64582392023-03-07 09:21:40 +01001442 ssl->session_negotiate->tls_version = MBEDTLS_SSL_VERSION_TLS1_3;
1443 ssl->session_negotiate->endpoint = ssl->conf->endpoint;
Ronald Cron64582392023-03-07 09:21:40 +01001444
1445 /*
Ronald Crondad02b22023-04-06 09:57:52 +02001446 * We are negotiating the version 1.3 of the protocol. Do what we have
Ronald Croncada4102023-03-07 09:51:39 +01001447 * postponed: copy of the client random bytes, copy of the legacy session
Ronald Cron2f16b4e2023-03-07 10:07:32 +01001448 * identifier and selection of the TLS 1.3 cipher suite.
Ronald Crond540d992023-03-07 09:41:48 +01001449 */
1450 MBEDTLS_SSL_DEBUG_BUF(3, "client hello, random bytes",
1451 random, MBEDTLS_CLIENT_HELLO_RANDOM_LEN);
1452 memcpy(&handshake->randbytes[0], random, MBEDTLS_CLIENT_HELLO_RANDOM_LEN);
1453
Ronald Croncada4102023-03-07 09:51:39 +01001454 if (legacy_session_id_len > sizeof(ssl->session_negotiate->id)) {
1455 MBEDTLS_SSL_DEBUG_MSG(1, ("bad client hello message"));
1456 return MBEDTLS_ERR_SSL_DECODE_ERROR;
1457 }
1458 ssl->session_negotiate->id_len = legacy_session_id_len;
1459 MBEDTLS_SSL_DEBUG_BUF(3, "client hello, session id",
1460 legacy_session_id, legacy_session_id_len);
1461 memcpy(&ssl->session_negotiate->id[0],
1462 legacy_session_id, legacy_session_id_len);
1463
Ronald Crond540d992023-03-07 09:41:48 +01001464 /*
Jerry Yu5725f1c2022-08-21 17:27:16 +08001465 * Search for a matching ciphersuite
1466 */
Ronald Cron2f16b4e2023-03-07 10:07:32 +01001467 MBEDTLS_SSL_DEBUG_BUF(3, "client hello, list of cipher suites",
1468 cipher_suites, cipher_suites_len);
Ronald Crone45afd72023-04-04 15:10:06 +02001469 for (const unsigned char *cipher_suites_p = cipher_suites;
1470 cipher_suites_p < cipher_suites_end; cipher_suites_p += 2) {
XiaokangQiane8ff3502022-04-22 02:34:40 +00001471 uint16_t cipher_suite;
Gilles Peskine449bd832023-01-11 14:50:10 +01001472 const mbedtls_ssl_ciphersuite_t *ciphersuite_info;
Jerry Yu5725f1c2022-08-21 17:27:16 +08001473
Ronald Crond89360b2023-02-21 08:53:33 +01001474 /*
Ronald Crone45afd72023-04-04 15:10:06 +02001475 * "cipher_suites_end - cipher_suites_p is even" is an invariant of the
1476 * loop. As cipher_suites_end - cipher_suites_p > 0, we have
1477 * cipher_suites_end - cipher_suites_p >= 2 and it is thus safe to read
1478 * two bytes.
Ronald Crond89360b2023-02-21 08:53:33 +01001479 */
Ronald Crone45afd72023-04-04 15:10:06 +02001480 cipher_suite = MBEDTLS_GET_UINT16_BE(cipher_suites_p, 0);
Jerry Yuc5a23a02022-08-25 10:51:44 +08001481 ciphersuite_info = ssl_tls13_validate_peer_ciphersuite(
Gilles Peskine449bd832023-01-11 14:50:10 +01001482 ssl, cipher_suite);
1483 if (ciphersuite_info == NULL) {
Jerry Yu5725f1c2022-08-21 17:27:16 +08001484 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +01001485 }
Jerry Yu5725f1c2022-08-21 17:27:16 +08001486
Jerry Yu5725f1c2022-08-21 17:27:16 +08001487 ssl->session_negotiate->ciphersuite = cipher_suite;
Jerry Yuc4bf5d62022-10-29 09:08:47 +08001488 handshake->ciphersuite_info = ciphersuite_info;
Gilles Peskine449bd832023-01-11 14:50:10 +01001489 MBEDTLS_SSL_DEBUG_MSG(2, ("selected ciphersuite: %04x - %s",
1490 cipher_suite,
1491 ciphersuite_info->name));
Ronald Cron25e9ec62023-02-16 15:35:16 +01001492 break;
XiaokangQian17f974c2022-04-19 09:57:41 +00001493 }
Jerry Yu5725f1c2022-08-21 17:27:16 +08001494
Gilles Peskine449bd832023-01-11 14:50:10 +01001495 if (handshake->ciphersuite_info == NULL) {
1496 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE,
1497 MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE);
1498 return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
Jerry Yu5725f1c2022-08-21 17:27:16 +08001499 }
Jerry Yuc1be19f2022-04-23 16:11:39 +08001500
XiaokangQian4080a7f2022-04-11 09:55:18 +00001501 /* ...
XiaokangQianb67384d2022-04-19 00:02:38 +00001502 * opaque legacy_compression_methods<1..2^8-1>;
XiaokangQian4080a7f2022-04-11 09:55:18 +00001503 * ...
XiaokangQian7807f9f2022-02-15 10:04:37 +00001504 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001505 if (p[0] != 1 || p[1] != MBEDTLS_SSL_COMPRESS_NULL) {
1506 MBEDTLS_SSL_DEBUG_MSG(1, ("bad legacy compression method"));
1507 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER,
1508 MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER);
1509 return MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001510 }
XiaokangQianb67384d2022-04-19 00:02:38 +00001511 p += 2;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001512
Jerry Yuc1be19f2022-04-23 16:11:39 +08001513 /* ...
XiaokangQianb67384d2022-04-19 00:02:38 +00001514 * Extension extensions<8..2^16-1>;
Jerry Yuc1be19f2022-04-23 16:11:39 +08001515 * ...
XiaokangQianb67384d2022-04-19 00:02:38 +00001516 * with Extension defined as:
1517 * struct {
1518 * ExtensionType extension_type;
1519 * opaque extension_data<0..2^16-1>;
1520 * } Extension;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001521 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001522 extensions_len = MBEDTLS_GET_UINT16_BE(p, 0);
XiaokangQian7807f9f2022-02-15 10:04:37 +00001523 p += 2;
Gilles Peskine449bd832023-01-11 14:50:10 +01001524 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, extensions_len);
XiaokangQiane8ff3502022-04-22 02:34:40 +00001525 extensions_end = p + extensions_len;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001526
Gilles Peskine449bd832023-01-11 14:50:10 +01001527 MBEDTLS_SSL_DEBUG_BUF(3, "client hello extensions", p, extensions_len);
Jerry Yu63a459c2022-10-31 13:38:40 +08001528 handshake->received_extensions = MBEDTLS_SSL_EXT_MASK_NONE;
Jerry Yu0c354a22022-08-29 15:25:36 +08001529
Gilles Peskine449bd832023-01-11 14:50:10 +01001530 while (p < extensions_end) {
XiaokangQian7807f9f2022-02-15 10:04:37 +00001531 unsigned int extension_type;
1532 size_t extension_data_len;
1533 const unsigned char *extension_data_end;
Jerry Yu263dbf72022-10-26 10:51:27 +08001534 uint32_t allowed_exts = MBEDTLS_SSL_TLS1_3_ALLOWED_EXTS_OF_CH;
1535
Ronald Cron5fbd2702024-02-14 10:03:36 +01001536 if (ssl->handshake->hello_retry_request_flag) {
Jerry Yu263dbf72022-10-26 10:51:27 +08001537 /* Do not accept early data extension in 2nd ClientHello */
1538 allowed_exts &= ~MBEDTLS_SSL_EXT_MASK(EARLY_DATA);
1539 }
XiaokangQian7807f9f2022-02-15 10:04:37 +00001540
Jerry Yu0c354a22022-08-29 15:25:36 +08001541 /* RFC 8446, section 4.2.11
Jerry Yu1c9247c2022-07-21 12:37:39 +08001542 *
1543 * The "pre_shared_key" extension MUST be the last extension in the
1544 * ClientHello (this facilitates implementation as described below).
1545 * Servers MUST check that it is the last extension and otherwise fail
1546 * the handshake with an "illegal_parameter" alert.
1547 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001548 if (handshake->received_extensions & MBEDTLS_SSL_EXT_MASK(PRE_SHARED_KEY)) {
Jerry Yu1c9247c2022-07-21 12:37:39 +08001549 MBEDTLS_SSL_DEBUG_MSG(
Gilles Peskine449bd832023-01-11 14:50:10 +01001550 3, ("pre_shared_key is not last extension."));
Jerry Yu1c9247c2022-07-21 12:37:39 +08001551 MBEDTLS_SSL_PEND_FATAL_ALERT(
1552 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER,
Gilles Peskine449bd832023-01-11 14:50:10 +01001553 MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER);
1554 return MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER;
Jerry Yu1c9247c2022-07-21 12:37:39 +08001555 }
1556
Gilles Peskine449bd832023-01-11 14:50:10 +01001557 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, extensions_end, 4);
1558 extension_type = MBEDTLS_GET_UINT16_BE(p, 0);
1559 extension_data_len = MBEDTLS_GET_UINT16_BE(p, 2);
XiaokangQian7807f9f2022-02-15 10:04:37 +00001560 p += 4;
1561
Gilles Peskine449bd832023-01-11 14:50:10 +01001562 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, extensions_end, extension_data_len);
XiaokangQian7807f9f2022-02-15 10:04:37 +00001563 extension_data_end = p + extension_data_len;
1564
Jerry Yuc4bf5d62022-10-29 09:08:47 +08001565 ret = mbedtls_ssl_tls13_check_received_extension(
Gilles Peskine449bd832023-01-11 14:50:10 +01001566 ssl, MBEDTLS_SSL_HS_CLIENT_HELLO, extension_type,
Jerry Yu263dbf72022-10-26 10:51:27 +08001567 allowed_exts);
Gilles Peskine449bd832023-01-11 14:50:10 +01001568 if (ret != 0) {
1569 return ret;
1570 }
Jerry Yue18dc7e2022-08-04 16:29:22 +08001571
Gilles Peskine449bd832023-01-11 14:50:10 +01001572 switch (extension_type) {
XiaokangQian40a35232022-05-07 09:02:40 +00001573#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
1574 case MBEDTLS_TLS_EXT_SERVERNAME:
Gilles Peskine449bd832023-01-11 14:50:10 +01001575 MBEDTLS_SSL_DEBUG_MSG(3, ("found ServerName extension"));
1576 ret = mbedtls_ssl_parse_server_name_ext(ssl, p,
1577 extension_data_end);
1578 if (ret != 0) {
XiaokangQian40a35232022-05-07 09:02:40 +00001579 MBEDTLS_SSL_DEBUG_RET(
Gilles Peskine449bd832023-01-11 14:50:10 +01001580 1, "mbedtls_ssl_parse_servername_ext", ret);
1581 return ret;
XiaokangQian40a35232022-05-07 09:02:40 +00001582 }
XiaokangQian40a35232022-05-07 09:02:40 +00001583 break;
1584#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
1585
Przemek Stekiel8c0a9532023-06-15 16:48:19 +02001586#if defined(PSA_WANT_ALG_ECDH) || defined(PSA_WANT_ALG_FFDH)
XiaokangQian7807f9f2022-02-15 10:04:37 +00001587 case MBEDTLS_TLS_EXT_SUPPORTED_GROUPS:
Gilles Peskine449bd832023-01-11 14:50:10 +01001588 MBEDTLS_SSL_DEBUG_MSG(3, ("found supported group extension"));
XiaokangQian7807f9f2022-02-15 10:04:37 +00001589
1590 /* Supported Groups Extension
1591 *
1592 * When sent by the client, the "supported_groups" extension
1593 * indicates the named groups which the client supports,
1594 * ordered from most preferred to least preferred.
1595 */
Jerry Yuc1be19f2022-04-23 16:11:39 +08001596 ret = ssl_tls13_parse_supported_groups_ext(
Gilles Peskine449bd832023-01-11 14:50:10 +01001597 ssl, p, extension_data_end);
1598 if (ret != 0) {
Xiaokang Qian9f1747b2023-03-30 02:50:04 +00001599 MBEDTLS_SSL_DEBUG_RET(
Xiaokang Qian8bce0e62023-04-04 10:15:48 +00001600 1, "ssl_tls13_parse_supported_groups_ext", ret);
Gilles Peskine449bd832023-01-11 14:50:10 +01001601 return ret;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001602 }
1603
XiaokangQian7807f9f2022-02-15 10:04:37 +00001604 break;
Przemek Stekiel8c0a9532023-06-15 16:48:19 +02001605#endif /* PSA_WANT_ALG_ECDH || PSA_WANT_ALG_FFDH*/
XiaokangQian7807f9f2022-02-15 10:04:37 +00001606
Valerio Settic9ae8622023-07-25 11:23:50 +02001607#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_EPHEMERAL_ENABLED)
XiaokangQian7807f9f2022-02-15 10:04:37 +00001608 case MBEDTLS_TLS_EXT_KEY_SHARE:
Gilles Peskine449bd832023-01-11 14:50:10 +01001609 MBEDTLS_SSL_DEBUG_MSG(3, ("found key share extension"));
XiaokangQian7807f9f2022-02-15 10:04:37 +00001610
1611 /*
1612 * Key Share Extension
1613 *
1614 * When sent by the client, the "key_share" extension
1615 * contains the endpoint's cryptographic parameters for
1616 * ECDHE/DHE key establishment methods.
1617 */
Jerry Yuc1be19f2022-04-23 16:11:39 +08001618 ret = ssl_tls13_parse_key_shares_ext(
Gilles Peskine449bd832023-01-11 14:50:10 +01001619 ssl, p, extension_data_end);
1620 if (ret == SSL_TLS1_3_PARSE_KEY_SHARES_EXT_NO_MATCH) {
Ronald Cron8a74f072023-06-14 17:59:29 +02001621 MBEDTLS_SSL_DEBUG_MSG(2, ("No usable share for key agreement."));
1622 no_usable_share_for_key_agreement = 1;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001623 }
1624
Gilles Peskine449bd832023-01-11 14:50:10 +01001625 if (ret < 0) {
Jerry Yu582dd062022-04-22 21:59:01 +08001626 MBEDTLS_SSL_DEBUG_RET(
Gilles Peskine449bd832023-01-11 14:50:10 +01001627 1, "ssl_tls13_parse_key_shares_ext", ret);
1628 return ret;
Jerry Yu582dd062022-04-22 21:59:01 +08001629 }
XiaokangQian7807f9f2022-02-15 10:04:37 +00001630
XiaokangQian7807f9f2022-02-15 10:04:37 +00001631 break;
Valerio Settic9ae8622023-07-25 11:23:50 +02001632#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_EPHEMERAL_ENABLED */
XiaokangQian7807f9f2022-02-15 10:04:37 +00001633
1634 case MBEDTLS_TLS_EXT_SUPPORTED_VERSIONS:
Ronald Cron8c527d02023-03-07 15:47:47 +01001635 /* Already parsed */
XiaokangQian7807f9f2022-02-15 10:04:37 +00001636 break;
1637
Ronald Cron41a443a2022-10-04 16:38:25 +02001638#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED)
Jerry Yue19e3b92022-07-08 12:04:51 +00001639 case MBEDTLS_TLS_EXT_PSK_KEY_EXCHANGE_MODES:
Xiaokang Qian9f1747b2023-03-30 02:50:04 +00001640 MBEDTLS_SSL_DEBUG_MSG(
1641 3, ("found psk key exchange modes extension"));
Jerry Yue19e3b92022-07-08 12:04:51 +00001642
1643 ret = ssl_tls13_parse_key_exchange_modes_ext(
Gilles Peskine449bd832023-01-11 14:50:10 +01001644 ssl, p, extension_data_end);
1645 if (ret != 0) {
Jerry Yue19e3b92022-07-08 12:04:51 +00001646 MBEDTLS_SSL_DEBUG_RET(
Gilles Peskine449bd832023-01-11 14:50:10 +01001647 1, "ssl_tls13_parse_key_exchange_modes_ext", ret);
1648 return ret;
Jerry Yue19e3b92022-07-08 12:04:51 +00001649 }
1650
Jerry Yue19e3b92022-07-08 12:04:51 +00001651 break;
Ronald Cron41a443a2022-10-04 16:38:25 +02001652#endif
Jerry Yue19e3b92022-07-08 12:04:51 +00001653
Jerry Yu1c105562022-07-10 06:32:38 +00001654 case MBEDTLS_TLS_EXT_PRE_SHARED_KEY:
Gilles Peskine449bd832023-01-11 14:50:10 +01001655 MBEDTLS_SSL_DEBUG_MSG(3, ("found pre_shared_key extension"));
1656 if ((handshake->received_extensions &
1657 MBEDTLS_SSL_EXT_MASK(PSK_KEY_EXCHANGE_MODES)) == 0) {
Jerry Yu13ab81d2022-07-22 23:17:11 +08001658 MBEDTLS_SSL_PEND_FATAL_ALERT(
1659 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER,
Gilles Peskine449bd832023-01-11 14:50:10 +01001660 MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER);
1661 return MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER;
Jerry Yu13ab81d2022-07-22 23:17:11 +08001662 }
Ronald Cron41a443a2022-10-04 16:38:25 +02001663#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED)
Jerry Yu1c105562022-07-10 06:32:38 +00001664 /* Delay processing of the PSK identity once we have
1665 * found out which algorithms to use. We keep a pointer
1666 * to the buffer and the size for later processing.
1667 */
Jerry Yu29d9faa2022-08-23 17:52:45 +08001668 pre_shared_key_ext = p;
Jerry Yu1c105562022-07-10 06:32:38 +00001669 pre_shared_key_ext_end = extension_data_end;
Jerry Yue18dc7e2022-08-04 16:29:22 +08001670#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED */
Jerry Yu1c105562022-07-10 06:32:38 +00001671 break;
Jerry Yu1c105562022-07-10 06:32:38 +00001672
XiaokangQianacb39922022-06-17 10:18:48 +00001673#if defined(MBEDTLS_SSL_ALPN)
1674 case MBEDTLS_TLS_EXT_ALPN:
Gilles Peskine449bd832023-01-11 14:50:10 +01001675 MBEDTLS_SSL_DEBUG_MSG(3, ("found alpn extension"));
XiaokangQianacb39922022-06-17 10:18:48 +00001676
Gilles Peskine449bd832023-01-11 14:50:10 +01001677 ret = mbedtls_ssl_parse_alpn_ext(ssl, p, extension_data_end);
1678 if (ret != 0) {
XiaokangQianacb39922022-06-17 10:18:48 +00001679 MBEDTLS_SSL_DEBUG_RET(
Gilles Peskine449bd832023-01-11 14:50:10 +01001680 1, ("mbedtls_ssl_parse_alpn_ext"), ret);
1681 return ret;
XiaokangQianacb39922022-06-17 10:18:48 +00001682 }
XiaokangQianacb39922022-06-17 10:18:48 +00001683 break;
1684#endif /* MBEDTLS_SSL_ALPN */
1685
Ronald Cron928cbd32022-10-04 16:14:26 +02001686#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED)
XiaokangQian7807f9f2022-02-15 10:04:37 +00001687 case MBEDTLS_TLS_EXT_SIG_ALG:
Gilles Peskine449bd832023-01-11 14:50:10 +01001688 MBEDTLS_SSL_DEBUG_MSG(3, ("found signature_algorithms extension"));
XiaokangQian7807f9f2022-02-15 10:04:37 +00001689
Gabor Mezei078e8032022-04-27 21:17:56 +02001690 ret = mbedtls_ssl_parse_sig_alg_ext(
Gilles Peskine449bd832023-01-11 14:50:10 +01001691 ssl, p, extension_data_end);
1692 if (ret != 0) {
Xiaokang Qian49f39c12023-04-06 02:31:02 +00001693 MBEDTLS_SSL_DEBUG_RET(
1694 1, "mbedtls_ssl_parse_sig_alg_ext", ret);
Gilles Peskine449bd832023-01-11 14:50:10 +01001695 return ret;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001696 }
XiaokangQian7807f9f2022-02-15 10:04:37 +00001697 break;
Ronald Cron928cbd32022-10-04 16:14:26 +02001698#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED */
XiaokangQian7807f9f2022-02-15 10:04:37 +00001699
Jan Bruckner151f6422023-02-10 12:45:19 +01001700#if defined(MBEDTLS_SSL_RECORD_SIZE_LIMIT)
1701 case MBEDTLS_TLS_EXT_RECORD_SIZE_LIMIT:
1702 MBEDTLS_SSL_DEBUG_MSG(3, ("found record_size_limit extension"));
1703
Xiaokang Qian9f1747b2023-03-30 02:50:04 +00001704 ret = mbedtls_ssl_tls13_parse_record_size_limit_ext(
1705 ssl, p, extension_data_end);
Jan Brucknerf482dcc2023-03-15 09:09:06 +01001706 if (ret != 0) {
1707 MBEDTLS_SSL_DEBUG_RET(
1708 1, ("mbedtls_ssl_tls13_parse_record_size_limit_ext"), ret);
1709 return ret;
1710 }
Jan Bruckner151f6422023-02-10 12:45:19 +01001711 break;
1712#endif /* MBEDTLS_SSL_RECORD_SIZE_LIMIT */
1713
XiaokangQian7807f9f2022-02-15 10:04:37 +00001714 default:
Jerry Yu79aa7212022-11-08 21:30:21 +08001715 MBEDTLS_SSL_PRINT_EXT(
Jerry Yu63a459c2022-10-31 13:38:40 +08001716 3, MBEDTLS_SSL_HS_CLIENT_HELLO,
Gilles Peskine449bd832023-01-11 14:50:10 +01001717 extension_type, "( ignored )");
Jerry Yu0c354a22022-08-29 15:25:36 +08001718 break;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001719 }
1720
1721 p += extension_data_len;
1722 }
1723
Gilles Peskine449bd832023-01-11 14:50:10 +01001724 MBEDTLS_SSL_PRINT_EXTS(3, MBEDTLS_SSL_HS_CLIENT_HELLO,
1725 handshake->received_extensions);
Jerry Yue18dc7e2022-08-04 16:29:22 +08001726
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +01001727 ret = mbedtls_ssl_add_hs_hdr_to_checksum(ssl,
1728 MBEDTLS_SSL_HS_CLIENT_HELLO,
1729 p - buf);
1730 if (0 != ret) {
1731 MBEDTLS_SSL_DEBUG_RET(1, ("mbedtls_ssl_add_hs_hdr_to_checksum"), ret);
1732 return ret;
1733 }
Jerry Yu032b15ce2022-07-11 06:10:03 +00001734
Ronald Cron41a443a2022-10-04 16:38:25 +02001735#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED)
XiaokangQian7807f9f2022-02-15 10:04:37 +00001736 /* Update checksum with either
1737 * - The entire content of the CH message, if no PSK extension is present
1738 * - The content up to but excluding the PSK extension, if present.
1739 */
Jerry Yu1c105562022-07-10 06:32:38 +00001740 /* If we've settled on a PSK-based exchange, parse PSK identity ext */
Pengyu Lvbc4aab72023-12-01 15:37:24 +08001741 if (ssl_tls13_key_exchange_is_psk_available(ssl) ||
1742 ssl_tls13_key_exchange_is_psk_ephemeral_available(ssl)) {
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +01001743 ret = handshake->update_checksum(ssl, buf,
1744 pre_shared_key_ext - buf);
1745 if (0 != ret) {
1746 MBEDTLS_SSL_DEBUG_RET(1, ("update_checksum"), ret);
1747 return ret;
1748 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001749 ret = ssl_tls13_parse_pre_shared_key_ext(ssl,
1750 pre_shared_key_ext,
1751 pre_shared_key_ext_end,
1752 cipher_suites,
1753 cipher_suites_end);
1754 if (ret == MBEDTLS_ERR_SSL_UNKNOWN_IDENTITY) {
1755 handshake->received_extensions &= ~MBEDTLS_SSL_EXT_MASK(PRE_SHARED_KEY);
1756 } else if (ret != 0) {
Jerry Yu63a459c2022-10-31 13:38:40 +08001757 MBEDTLS_SSL_DEBUG_RET(
Gilles Peskine449bd832023-01-11 14:50:10 +01001758 1, "ssl_tls13_parse_pre_shared_key_ext", ret);
1759 return ret;
Jerry Yu1c105562022-07-10 06:32:38 +00001760 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001761 } else
Ronald Cron41a443a2022-10-04 16:38:25 +02001762#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED */
Jerry Yu1c105562022-07-10 06:32:38 +00001763 {
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +01001764 ret = handshake->update_checksum(ssl, buf, p - buf);
1765 if (0 != ret) {
1766 MBEDTLS_SSL_DEBUG_RET(1, ("update_checksum"), ret);
1767 return ret;
1768 }
Jerry Yu1c105562022-07-10 06:32:38 +00001769 }
XiaokangQian7807f9f2022-02-15 10:04:37 +00001770
Gilles Peskine449bd832023-01-11 14:50:10 +01001771 ret = ssl_tls13_determine_key_exchange_mode(ssl);
1772 if (ret < 0) {
1773 return ret;
1774 }
XiaokangQian7807f9f2022-02-15 10:04:37 +00001775
Ronald Cron8a74f072023-06-14 17:59:29 +02001776 if (ssl->handshake->key_exchange_mode !=
1777 MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK) {
1778 hrr_required = (no_usable_share_for_key_agreement != 0);
1779 }
1780
Gilles Peskine449bd832023-01-11 14:50:10 +01001781 mbedtls_ssl_optimize_checksum(ssl, handshake->ciphersuite_info);
Jerry Yue5834fd2022-08-29 20:16:09 +08001782
Gilles Peskine449bd832023-01-11 14:50:10 +01001783 return hrr_required ? SSL_CLIENT_HELLO_HRR_REQUIRED : SSL_CLIENT_HELLO_OK;
XiaokangQian75fe8c72022-06-15 09:42:45 +00001784}
1785
Jerry Yuab0da372023-02-08 13:55:24 +08001786#if defined(MBEDTLS_SSL_EARLY_DATA)
Jerry Yu4caf3ca2023-11-15 16:13:47 +08001787static int ssl_tls13_check_early_data_requirements(mbedtls_ssl_context *ssl)
Jerry Yuab0da372023-02-08 13:55:24 +08001788{
1789 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
1790
Jerry Yu985c9672022-12-04 14:06:30 +08001791 if (ssl->conf->early_data_enabled == MBEDTLS_SSL_EARLY_DATA_DISABLED) {
1792 MBEDTLS_SSL_DEBUG_MSG(
Jerry Yu985c9672022-12-04 14:06:30 +08001793 1,
Jerry Yu454dda32023-10-31 15:13:54 +08001794 ("EarlyData: rejected, feature disabled in server configuration."));
Jerry Yu4caf3ca2023-11-15 16:13:47 +08001795 return -1;
Ronald Cron7b6ee942024-01-12 10:29:55 +01001796 }
1797
Jerry Yu454dda32023-10-31 15:13:54 +08001798 if (!handshake->resume) {
1799 /* We currently support early data only in the case of PSKs established
1800 via a NewSessionTicket message thus in the case of a session
1801 resumption. */
Jerry Yu985c9672022-12-04 14:06:30 +08001802 MBEDTLS_SSL_DEBUG_MSG(
Jerry Yu7ef9fd82023-11-07 14:30:38 +08001803 1, ("EarlyData: rejected, not a session resumption."));
Jerry Yu4caf3ca2023-11-15 16:13:47 +08001804 return -1;
Jerry Yu985c9672022-12-04 14:06:30 +08001805 }
1806
Jerry Yu82fd6c12023-10-31 16:32:19 +08001807 /* RFC 8446 4.2.10
1808 *
1809 * In order to accept early data, the server MUST have accepted a PSK cipher
1810 * suite and selected the first key offered in the client's "pre_shared_key"
1811 * extension. In addition, it MUST verify that the following values are the
1812 * same as those associated with the selected PSK:
1813 * - The TLS version number
1814 * - The selected cipher suite
1815 * - The selected ALPN [RFC7301] protocol, if any
1816 *
1817 * NOTE:
Jerry Yu7ef9fd82023-11-07 14:30:38 +08001818 * - The TLS version number is checked in
1819 * ssl_tls13_offered_psks_check_identity_match_ticket().
1820 * - ALPN is not checked for the time being (TODO).
Jerry Yu82fd6c12023-10-31 16:32:19 +08001821 */
1822
1823 if (handshake->selected_identity != 0) {
1824 MBEDTLS_SSL_DEBUG_MSG(
Jerry Yu7ef9fd82023-11-07 14:30:38 +08001825 1, ("EarlyData: rejected, the selected key in "
1826 "`pre_shared_key` is not the first one."));
Jerry Yu4caf3ca2023-11-15 16:13:47 +08001827 return -1;
Jerry Yu82fd6c12023-10-31 16:32:19 +08001828 }
1829
1830 if (handshake->ciphersuite_info->id !=
1831 ssl->session_negotiate->ciphersuite) {
1832 MBEDTLS_SSL_DEBUG_MSG(
Jerry Yu7ef9fd82023-11-07 14:30:38 +08001833 1, ("EarlyData: rejected, the selected ciphersuite is not the one "
1834 "of the selected pre-shared key."));
Jerry Yu4caf3ca2023-11-15 16:13:47 +08001835 return -1;
Jerry Yu82fd6c12023-10-31 16:32:19 +08001836
1837 }
Jerry Yu985c9672022-12-04 14:06:30 +08001838
Pengyu Lv94a42cc2023-12-06 10:04:17 +08001839 if (!mbedtls_ssl_tls13_session_ticket_allow_early_data(ssl->session_negotiate)) {
Jerry Yufceddb32022-12-12 15:30:34 +08001840 MBEDTLS_SSL_DEBUG_MSG(
1841 1,
Jerry Yuea96ac32023-11-21 17:06:36 +08001842 ("EarlyData: rejected, early_data not allowed in ticket "
1843 "permission bits."));
Jerry Yu4caf3ca2023-11-15 16:13:47 +08001844 return -1;
Jerry Yufceddb32022-12-12 15:30:34 +08001845 }
Jerry Yu985c9672022-12-04 14:06:30 +08001846
Jerry Yu4caf3ca2023-11-15 16:13:47 +08001847 return 0;
Jerry Yuab0da372023-02-08 13:55:24 +08001848}
1849#endif /* MBEDTLS_SSL_EARLY_DATA */
1850
XiaokangQian75fe8c72022-06-15 09:42:45 +00001851/* Update the handshake state machine */
1852
Ronald Cronce7d76e2022-07-08 18:56:49 +02001853MBEDTLS_CHECK_RETURN_CRITICAL
Ronald Cron7b6ee942024-01-12 10:29:55 +01001854static int ssl_tls13_postprocess_client_hello(mbedtls_ssl_context *ssl,
1855 int hrr_required)
XiaokangQian75fe8c72022-06-15 09:42:45 +00001856{
1857 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1858
XiaokangQian7807f9f2022-02-15 10:04:37 +00001859 /*
XiaokangQianfb665a82022-06-15 03:57:21 +00001860 * Server certificate selection
1861 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001862 if (ssl->conf->f_cert_cb && (ret = ssl->conf->f_cert_cb(ssl)) != 0) {
1863 MBEDTLS_SSL_DEBUG_RET(1, "f_cert_cb", ret);
1864 return ret;
XiaokangQianfb665a82022-06-15 03:57:21 +00001865 }
1866#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
1867 ssl->handshake->sni_name = NULL;
1868 ssl->handshake->sni_name_len = 0;
1869#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
XiaokangQian7807f9f2022-02-15 10:04:37 +00001870
Gilles Peskine449bd832023-01-11 14:50:10 +01001871 ret = mbedtls_ssl_tls13_key_schedule_stage_early(ssl);
1872 if (ret != 0) {
1873 MBEDTLS_SSL_DEBUG_RET(1,
1874 "mbedtls_ssl_tls1_3_key_schedule_stage_early", ret);
1875 return ret;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001876 }
1877
Jerry Yuab0da372023-02-08 13:55:24 +08001878#if defined(MBEDTLS_SSL_EARLY_DATA)
Jerry Yu4caf3ca2023-11-15 16:13:47 +08001879 if (ssl->handshake->received_extensions & MBEDTLS_SSL_EXT_MASK(EARLY_DATA)) {
Ronald Cron31e2d832024-02-05 16:45:57 +01001880 ssl->handshake->early_data_accepted =
1881 (!hrr_required) && (ssl_tls13_check_early_data_requirements(ssl) == 0);
Jerry Yu4e9b70e2022-12-04 14:08:02 +08001882
Jerry Yu4caf3ca2023-11-15 16:13:47 +08001883 if (ssl->handshake->early_data_accepted) {
1884 ret = mbedtls_ssl_tls13_compute_early_transform(ssl);
1885 if (ret != 0) {
1886 MBEDTLS_SSL_DEBUG_RET(
1887 1, "mbedtls_ssl_tls13_compute_early_transform", ret);
1888 return ret;
1889 }
1890 } else {
1891 ssl->discard_early_data_record =
1892 hrr_required ?
1893 MBEDTLS_SSL_EARLY_DATA_DISCARD :
1894 MBEDTLS_SSL_EARLY_DATA_TRY_TO_DEPROTECT_AND_DISCARD;
Jerry Yu4e9b70e2022-12-04 14:08:02 +08001895 }
1896 }
Ronald Cron7b6ee942024-01-12 10:29:55 +01001897#else
1898 ((void) hrr_required);
Jerry Yuab0da372023-02-08 13:55:24 +08001899#endif /* MBEDTLS_SSL_EARLY_DATA */
1900
Gilles Peskine449bd832023-01-11 14:50:10 +01001901 return 0;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001902}
1903
1904/*
XiaokangQianed582dd2022-04-13 08:21:05 +00001905 * Main entry point from the state machine; orchestrates the otherfunctions.
1906 */
1907
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001908MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01001909static int ssl_tls13_process_client_hello(mbedtls_ssl_context *ssl)
XiaokangQianed582dd2022-04-13 08:21:05 +00001910{
1911
XiaokangQian08037552022-04-20 07:16:41 +00001912 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine449bd832023-01-11 14:50:10 +01001913 unsigned char *buf = NULL;
XiaokangQianed582dd2022-04-13 08:21:05 +00001914 size_t buflen = 0;
Jerry Yu4ca91402022-05-09 15:50:57 +08001915 int parse_client_hello_ret;
1916
Gilles Peskine449bd832023-01-11 14:50:10 +01001917 MBEDTLS_SSL_DEBUG_MSG(2, ("=> parse client hello"));
XiaokangQianed582dd2022-04-13 08:21:05 +00001918
Gilles Peskine449bd832023-01-11 14:50:10 +01001919 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_tls13_fetch_handshake_msg(
1920 ssl, MBEDTLS_SSL_HS_CLIENT_HELLO,
1921 &buf, &buflen));
XiaokangQianed582dd2022-04-13 08:21:05 +00001922
Gilles Peskine449bd832023-01-11 14:50:10 +01001923 MBEDTLS_SSL_PROC_CHK_NEG(ssl_tls13_parse_client_hello(ssl, buf,
1924 buf + buflen));
Ronald Cron5af4c7f2023-03-07 20:46:59 +01001925 parse_client_hello_ret = ret; /* Store positive return value of
1926 * parse_client_hello,
1927 * as negative error codes are handled
Jerry Yuf41553b2022-05-09 22:20:30 +08001928 * by MBEDTLS_SSL_PROC_CHK_NEG. */
Jerry Yu4ca91402022-05-09 15:50:57 +08001929
Ronald Cronb828c7d2023-04-03 16:37:22 +02001930 /*
Yanray Wang90acdc62023-12-08 10:29:42 +08001931 * Version 1.2 of the protocol has to be used for the handshake.
1932 * If TLS 1.2 is not supported, abort the handshake. Otherwise, set the
Ronald Cronb828c7d2023-04-03 16:37:22 +02001933 * ssl->keep_current_message flag for the ClientHello to be kept and parsed
1934 * as a TLS 1.2 ClientHello. We also change ssl->tls_version to
1935 * MBEDTLS_SSL_VERSION_TLS1_2 thus from now on mbedtls_ssl_handshake_step()
1936 * will dispatch to the TLS 1.2 state machine.
1937 */
Ronald Cron5af4c7f2023-03-07 20:46:59 +01001938 if (SSL_CLIENT_HELLO_TLS1_2 == parse_client_hello_ret) {
Yanray Wangfb0f47b2023-12-04 15:27:28 +08001939 /* Check if server supports TLS 1.2 */
Yanray Wang408ba6f2023-12-08 10:18:03 +08001940 if (!mbedtls_ssl_conf_is_tls12_enabled(ssl->conf)) {
Yanray Wangfb0f47b2023-12-04 15:27:28 +08001941 MBEDTLS_SSL_DEBUG_MSG(
Yanray Wang177e49a2023-12-08 10:51:04 +08001942 1, ("TLS 1.2 not supported."));
Yanray Wangfb0f47b2023-12-04 15:27:28 +08001943 MBEDTLS_SSL_PEND_FATAL_ALERT(
Yanray Wang2bef9172023-12-08 10:21:53 +08001944 MBEDTLS_SSL_ALERT_MSG_PROTOCOL_VERSION,
1945 MBEDTLS_ERR_SSL_BAD_PROTOCOL_VERSION);
1946 return MBEDTLS_ERR_SSL_BAD_PROTOCOL_VERSION;
Yanray Wangfb0f47b2023-12-04 15:27:28 +08001947 }
Ronald Cron5af4c7f2023-03-07 20:46:59 +01001948 ssl->keep_current_message = 1;
1949 ssl->tls_version = MBEDTLS_SSL_VERSION_TLS1_2;
1950 return 0;
1951 }
1952
Ronald Cron7b6ee942024-01-12 10:29:55 +01001953 MBEDTLS_SSL_PROC_CHK(
1954 ssl_tls13_postprocess_client_hello(ssl, parse_client_hello_ret ==
1955 SSL_CLIENT_HELLO_HRR_REQUIRED));
Jerry Yu582dd062022-04-22 21:59:01 +08001956
Ronald Cron5af4c7f2023-03-07 20:46:59 +01001957 if (SSL_CLIENT_HELLO_OK == parse_client_hello_ret) {
Gilles Peskine449bd832023-01-11 14:50:10 +01001958 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_SERVER_HELLO);
1959 } else {
1960 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_HELLO_RETRY_REQUEST);
1961 }
XiaokangQianed582dd2022-04-13 08:21:05 +00001962
1963cleanup:
1964
Gilles Peskine449bd832023-01-11 14:50:10 +01001965 MBEDTLS_SSL_DEBUG_MSG(2, ("<= parse client hello"));
1966 return ret;
XiaokangQianed582dd2022-04-13 08:21:05 +00001967}
1968
1969/*
Jerry Yu1c3e6882022-04-20 21:23:40 +08001970 * Handler for MBEDTLS_SSL_SERVER_HELLO
Jerry Yu5b64ae92022-03-30 17:15:02 +08001971 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001972MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01001973static int ssl_tls13_prepare_server_hello(mbedtls_ssl_context *ssl)
Jerry Yuf4b27e42022-03-30 17:32:21 +08001974{
Jerry Yu637a3f12022-04-20 21:37:58 +08001975 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1976 unsigned char *server_randbytes =
Gilles Peskine449bd832023-01-11 14:50:10 +01001977 ssl->handshake->randbytes + MBEDTLS_CLIENT_HELLO_RANDOM_LEN;
1978 if (ssl->conf->f_rng == NULL) {
1979 MBEDTLS_SSL_DEBUG_MSG(1, ("no RNG provided"));
1980 return MBEDTLS_ERR_SSL_NO_RNG;
Jerry Yuf4b27e42022-03-30 17:32:21 +08001981 }
1982
Gilles Peskine449bd832023-01-11 14:50:10 +01001983 if ((ret = ssl->conf->f_rng(ssl->conf->p_rng, server_randbytes,
1984 MBEDTLS_SERVER_HELLO_RANDOM_LEN)) != 0) {
1985 MBEDTLS_SSL_DEBUG_RET(1, "f_rng", ret);
1986 return ret;
Jerry Yuf4b27e42022-03-30 17:32:21 +08001987 }
1988
Gilles Peskine449bd832023-01-11 14:50:10 +01001989 MBEDTLS_SSL_DEBUG_BUF(3, "server hello, random bytes", server_randbytes,
1990 MBEDTLS_SERVER_HELLO_RANDOM_LEN);
Jerry Yuf4b27e42022-03-30 17:32:21 +08001991
1992#if defined(MBEDTLS_HAVE_TIME)
YxCda609132023-05-22 12:08:12 -07001993 ssl->session_negotiate->start = mbedtls_time(NULL);
Jerry Yuf4b27e42022-03-30 17:32:21 +08001994#endif /* MBEDTLS_HAVE_TIME */
1995
Gilles Peskine449bd832023-01-11 14:50:10 +01001996 return ret;
Jerry Yuf4b27e42022-03-30 17:32:21 +08001997}
1998
Jerry Yu3bf2c642022-03-30 22:02:12 +08001999/*
Jerry Yue74e04a2022-04-21 09:23:16 +08002000 * ssl_tls13_write_server_hello_supported_versions_ext ():
Jerry Yu3bf2c642022-03-30 22:02:12 +08002001 *
2002 * struct {
Jerry Yufb9f54d2022-04-06 10:08:34 +08002003 * ProtocolVersion selected_version;
Jerry Yu3bf2c642022-03-30 22:02:12 +08002004 * } SupportedVersions;
2005 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002006MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yue74e04a2022-04-21 09:23:16 +08002007static int ssl_tls13_write_server_hello_supported_versions_ext(
Gilles Peskine449bd832023-01-11 14:50:10 +01002008 mbedtls_ssl_context *ssl,
2009 unsigned char *buf,
2010 unsigned char *end,
2011 size_t *out_len)
Jerry Yu3bf2c642022-03-30 22:02:12 +08002012{
Jerry Yu3bf2c642022-03-30 22:02:12 +08002013 *out_len = 0;
2014
Gilles Peskine449bd832023-01-11 14:50:10 +01002015 MBEDTLS_SSL_DEBUG_MSG(3, ("server hello, write selected version"));
Jerry Yu3bf2c642022-03-30 22:02:12 +08002016
2017 /* Check if we have space to write the extension:
2018 * - extension_type (2 bytes)
2019 * - extension_data_length (2 bytes)
Jerry Yu349a6132022-04-14 20:52:56 +08002020 * - selected_version (2 bytes)
Jerry Yu3bf2c642022-03-30 22:02:12 +08002021 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002022 MBEDTLS_SSL_CHK_BUF_PTR(buf, end, 6);
Jerry Yu3bf2c642022-03-30 22:02:12 +08002023
Gilles Peskine449bd832023-01-11 14:50:10 +01002024 MBEDTLS_PUT_UINT16_BE(MBEDTLS_TLS_EXT_SUPPORTED_VERSIONS, buf, 0);
Jerry Yu3bf2c642022-03-30 22:02:12 +08002025
Gilles Peskine449bd832023-01-11 14:50:10 +01002026 MBEDTLS_PUT_UINT16_BE(2, buf, 2);
Jerry Yu3bf2c642022-03-30 22:02:12 +08002027
Gilles Peskine449bd832023-01-11 14:50:10 +01002028 mbedtls_ssl_write_version(buf + 4,
2029 ssl->conf->transport,
2030 ssl->tls_version);
Jerry Yu3bf2c642022-03-30 22:02:12 +08002031
Gilles Peskine449bd832023-01-11 14:50:10 +01002032 MBEDTLS_SSL_DEBUG_MSG(3, ("supported version: [%04x]",
2033 ssl->tls_version));
Jerry Yu3bf2c642022-03-30 22:02:12 +08002034
Jerry Yu349a6132022-04-14 20:52:56 +08002035 *out_len = 6;
Jerry Yu3bf2c642022-03-30 22:02:12 +08002036
Jerry Yub95dd362022-11-08 21:19:34 +08002037 mbedtls_ssl_tls13_set_hs_sent_ext_mask(
Gilles Peskine449bd832023-01-11 14:50:10 +01002038 ssl, MBEDTLS_TLS_EXT_SUPPORTED_VERSIONS);
Jerry Yub95dd362022-11-08 21:19:34 +08002039
Gilles Peskine449bd832023-01-11 14:50:10 +01002040 return 0;
Jerry Yu3bf2c642022-03-30 22:02:12 +08002041}
2042
Jerry Yud9436a12022-04-20 22:28:09 +08002043
Jerry Yu3bf2c642022-03-30 22:02:12 +08002044
2045/* Generate and export a single key share. For hybrid KEMs, this can
2046 * be called multiple times with the different components of the hybrid. */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002047MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002048static int ssl_tls13_generate_and_write_key_share(mbedtls_ssl_context *ssl,
2049 uint16_t named_group,
2050 unsigned char *buf,
2051 unsigned char *end,
2052 size_t *out_len)
Jerry Yu3bf2c642022-03-30 22:02:12 +08002053{
2054 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yu955ddd72022-04-22 22:27:33 +08002055
2056 *out_len = 0;
2057
Valerio Settic9ae8622023-07-25 11:23:50 +02002058#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_EPHEMERAL_ENABLED)
Przemek Stekiel29c219c2023-05-31 15:21:04 +02002059 if (mbedtls_ssl_tls13_named_group_is_ecdhe(named_group) ||
Przemek Stekield5f79e72023-06-29 09:08:43 +02002060 mbedtls_ssl_tls13_named_group_is_ffdh(named_group)) {
Przemek Stekiel408569f2023-07-06 11:26:44 +02002061 ret = mbedtls_ssl_tls13_generate_and_write_xxdh_key_exchange(
Gilles Peskine449bd832023-01-11 14:50:10 +01002062 ssl, named_group, buf, end, out_len);
2063 if (ret != 0) {
Jerry Yu89e103c2022-03-30 22:43:29 +08002064 MBEDTLS_SSL_DEBUG_RET(
Przemek Stekiel408569f2023-07-06 11:26:44 +02002065 1, "mbedtls_ssl_tls13_generate_and_write_xxdh_key_exchange",
Gilles Peskine449bd832023-01-11 14:50:10 +01002066 ret);
2067 return ret;
Jerry Yu3bf2c642022-03-30 22:02:12 +08002068 }
Gilles Peskine449bd832023-01-11 14:50:10 +01002069 } else
Valerio Settic9ae8622023-07-25 11:23:50 +02002070#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_EPHEMERAL_ENABLED */
Gilles Peskine449bd832023-01-11 14:50:10 +01002071 if (0 /* Other kinds of KEMs */) {
2072 } else {
Jerry Yu955ddd72022-04-22 22:27:33 +08002073 ((void) ssl);
2074 ((void) named_group);
2075 ((void) buf);
2076 ((void) end);
Jerry Yu3bf2c642022-03-30 22:02:12 +08002077 ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
2078 }
2079
Gilles Peskine449bd832023-01-11 14:50:10 +01002080 return ret;
Jerry Yu3bf2c642022-03-30 22:02:12 +08002081}
2082
2083/*
2084 * ssl_tls13_write_key_share_ext
2085 *
2086 * Structure of key_share extension in ServerHello:
2087 *
Jerry Yu1c3e6882022-04-20 21:23:40 +08002088 * struct {
2089 * NamedGroup group;
2090 * opaque key_exchange<1..2^16-1>;
2091 * } KeyShareEntry;
2092 * struct {
2093 * KeyShareEntry server_share;
2094 * } KeyShareServerHello;
Jerry Yu3bf2c642022-03-30 22:02:12 +08002095 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002096MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002097static int ssl_tls13_write_key_share_ext(mbedtls_ssl_context *ssl,
2098 unsigned char *buf,
2099 unsigned char *end,
2100 size_t *out_len)
Jerry Yu3bf2c642022-03-30 22:02:12 +08002101{
Jerry Yu955ddd72022-04-22 22:27:33 +08002102 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yu3bf2c642022-03-30 22:02:12 +08002103 unsigned char *p = buf;
Jerry Yu57d48412022-04-20 21:50:42 +08002104 uint16_t group = ssl->handshake->offered_group_id;
Jerry Yu3bf2c642022-03-30 22:02:12 +08002105 unsigned char *server_share = buf + 4;
Jerry Yu3bf2c642022-03-30 22:02:12 +08002106 size_t key_exchange_length;
Jerry Yu3bf2c642022-03-30 22:02:12 +08002107
2108 *out_len = 0;
2109
Gilles Peskine449bd832023-01-11 14:50:10 +01002110 MBEDTLS_SSL_DEBUG_MSG(3, ("server hello, adding key share extension"));
Jerry Yu3bf2c642022-03-30 22:02:12 +08002111
Gilles Peskine449bd832023-01-11 14:50:10 +01002112 MBEDTLS_SSL_DEBUG_MSG(2, ("server hello, write selected_group: %s (%04x)",
2113 mbedtls_ssl_named_group_to_str(group),
2114 group));
Jerry Yu58af2332022-09-06 11:19:31 +08002115
Jerry Yu3bf2c642022-03-30 22:02:12 +08002116 /* Check if we have space for header and length fields:
2117 * - extension_type (2 bytes)
2118 * - extension_data_length (2 bytes)
2119 * - group (2 bytes)
2120 * - key_exchange_length (2 bytes)
2121 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002122 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 8);
2123 MBEDTLS_PUT_UINT16_BE(MBEDTLS_TLS_EXT_KEY_SHARE, p, 0);
2124 MBEDTLS_PUT_UINT16_BE(group, server_share, 0);
Jerry Yu3bf2c642022-03-30 22:02:12 +08002125 p += 8;
Jerry Yu57d48412022-04-20 21:50:42 +08002126
Jerry Yu3bf2c642022-03-30 22:02:12 +08002127 /* When we introduce PQC-ECDHE hybrids, we'll want to call this
2128 * function multiple times. */
Jerry Yu955ddd72022-04-22 22:27:33 +08002129 ret = ssl_tls13_generate_and_write_key_share(
Gilles Peskine449bd832023-01-11 14:50:10 +01002130 ssl, group, server_share + 4, end, &key_exchange_length);
2131 if (ret != 0) {
2132 return ret;
2133 }
Jerry Yu3bf2c642022-03-30 22:02:12 +08002134 p += key_exchange_length;
Jerry Yuc1be19f2022-04-23 16:11:39 +08002135
Gilles Peskine449bd832023-01-11 14:50:10 +01002136 MBEDTLS_PUT_UINT16_BE(key_exchange_length, server_share + 2, 0);
Jerry Yu3bf2c642022-03-30 22:02:12 +08002137
Gilles Peskine449bd832023-01-11 14:50:10 +01002138 MBEDTLS_PUT_UINT16_BE(p - server_share, buf, 2);
Jerry Yu3bf2c642022-03-30 22:02:12 +08002139
Jerry Yu57d48412022-04-20 21:50:42 +08002140 *out_len = p - buf;
Jerry Yu955ddd72022-04-22 22:27:33 +08002141
Gilles Peskine449bd832023-01-11 14:50:10 +01002142 mbedtls_ssl_tls13_set_hs_sent_ext_mask(ssl, MBEDTLS_TLS_EXT_KEY_SHARE);
Jerry Yub95dd362022-11-08 21:19:34 +08002143
Gilles Peskine449bd832023-01-11 14:50:10 +01002144 return 0;
Jerry Yu3bf2c642022-03-30 22:02:12 +08002145}
Jerry Yud9436a12022-04-20 22:28:09 +08002146
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002147MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002148static int ssl_tls13_write_hrr_key_share_ext(mbedtls_ssl_context *ssl,
2149 unsigned char *buf,
2150 unsigned char *end,
2151 size_t *out_len)
Jerry Yu23f7a6f2022-04-23 15:16:45 +08002152{
2153 uint16_t selected_group = ssl->handshake->hrr_selected_group;
2154 /* key_share Extension
2155 *
2156 * struct {
2157 * select (Handshake.msg_type) {
2158 * ...
2159 * case hello_retry_request:
2160 * NamedGroup selected_group;
2161 * ...
2162 * };
2163 * } KeyShare;
2164 */
2165
2166 *out_len = 0;
2167
Jerry Yub0ac10b2022-05-05 11:10:08 +08002168 /*
2169 * For a pure PSK key exchange, there is no group to agree upon. The purpose
2170 * of the HRR is then to transmit a cookie to force the client to demonstrate
2171 * reachability at their apparent network address (primarily useful for DTLS).
2172 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002173 if (!mbedtls_ssl_tls13_key_exchange_mode_with_ephemeral(ssl)) {
2174 return 0;
2175 }
Jerry Yu23f7a6f2022-04-23 15:16:45 +08002176
2177 /* We should only send the key_share extension if the client's initial
2178 * key share was not acceptable. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002179 if (ssl->handshake->offered_group_id != 0) {
2180 MBEDTLS_SSL_DEBUG_MSG(4, ("Skip key_share extension in HRR"));
2181 return 0;
Jerry Yu23f7a6f2022-04-23 15:16:45 +08002182 }
2183
Gilles Peskine449bd832023-01-11 14:50:10 +01002184 if (selected_group == 0) {
2185 MBEDTLS_SSL_DEBUG_MSG(1, ("no matching named group found"));
2186 return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
Jerry Yu23f7a6f2022-04-23 15:16:45 +08002187 }
2188
Jerry Yub0ac10b2022-05-05 11:10:08 +08002189 /* Check if we have enough space:
2190 * - extension_type (2 bytes)
2191 * - extension_data_length (2 bytes)
2192 * - selected_group (2 bytes)
2193 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002194 MBEDTLS_SSL_CHK_BUF_PTR(buf, end, 6);
Jerry Yu23f7a6f2022-04-23 15:16:45 +08002195
Gilles Peskine449bd832023-01-11 14:50:10 +01002196 MBEDTLS_PUT_UINT16_BE(MBEDTLS_TLS_EXT_KEY_SHARE, buf, 0);
2197 MBEDTLS_PUT_UINT16_BE(2, buf, 2);
2198 MBEDTLS_PUT_UINT16_BE(selected_group, buf, 4);
Jerry Yu23f7a6f2022-04-23 15:16:45 +08002199
Gilles Peskine449bd832023-01-11 14:50:10 +01002200 MBEDTLS_SSL_DEBUG_MSG(3,
2201 ("HRR selected_group: %s (%x)",
2202 mbedtls_ssl_named_group_to_str(selected_group),
2203 selected_group));
Jerry Yu23f7a6f2022-04-23 15:16:45 +08002204
2205 *out_len = 6;
Jerry Yu23f7a6f2022-04-23 15:16:45 +08002206
Gilles Peskine449bd832023-01-11 14:50:10 +01002207 mbedtls_ssl_tls13_set_hs_sent_ext_mask(ssl, MBEDTLS_TLS_EXT_KEY_SHARE);
Jerry Yub95dd362022-11-08 21:19:34 +08002208
Gilles Peskine449bd832023-01-11 14:50:10 +01002209 return 0;
Jerry Yu23f7a6f2022-04-23 15:16:45 +08002210}
Jerry Yu3bf2c642022-03-30 22:02:12 +08002211
2212/*
2213 * Structure of ServerHello message:
2214 *
2215 * struct {
2216 * ProtocolVersion legacy_version = 0x0303; // TLS v1.2
2217 * Random random;
2218 * opaque legacy_session_id_echo<0..32>;
2219 * CipherSuite cipher_suite;
2220 * uint8 legacy_compression_method = 0;
2221 * Extension extensions<6..2^16-1>;
2222 * } ServerHello;
2223 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002224MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002225static int ssl_tls13_write_server_hello_body(mbedtls_ssl_context *ssl,
2226 unsigned char *buf,
2227 unsigned char *end,
2228 size_t *out_len,
2229 int is_hrr)
Jerry Yu56404d72022-03-30 17:36:13 +08002230{
Jerry Yu955ddd72022-04-22 22:27:33 +08002231 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yu3bf2c642022-03-30 22:02:12 +08002232 unsigned char *p = buf;
Jerry Yud9436a12022-04-20 22:28:09 +08002233 unsigned char *p_extensions_len;
Jerry Yufbe3e642022-04-25 19:31:51 +08002234 size_t output_len;
Jerry Yu3bf2c642022-03-30 22:02:12 +08002235
2236 *out_len = 0;
Jerry Yu50e00e32022-10-31 14:45:01 +08002237 ssl->handshake->sent_extensions = MBEDTLS_SSL_EXT_MASK_NONE;
Jerry Yu3bf2c642022-03-30 22:02:12 +08002238
Jerry Yucfc04b32022-04-21 09:31:58 +08002239 /* ...
2240 * ProtocolVersion legacy_version = 0x0303; // TLS 1.2
2241 * ...
2242 * with ProtocolVersion defined as:
2243 * uint16 ProtocolVersion;
Jerry Yu3bf2c642022-03-30 22:02:12 +08002244 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002245 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 2);
2246 MBEDTLS_PUT_UINT16_BE(0x0303, p, 0);
Jerry Yu3bf2c642022-03-30 22:02:12 +08002247 p += 2;
2248
Jerry Yu1c3e6882022-04-20 21:23:40 +08002249 /* ...
2250 * Random random;
2251 * ...
Jerry Yucfc04b32022-04-21 09:31:58 +08002252 * with Random defined as:
2253 * opaque Random[MBEDTLS_SERVER_HELLO_RANDOM_LEN];
Jerry Yu1c3e6882022-04-20 21:23:40 +08002254 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002255 MBEDTLS_SSL_CHK_BUF_PTR(p, end, MBEDTLS_SERVER_HELLO_RANDOM_LEN);
2256 if (is_hrr) {
2257 memcpy(p, mbedtls_ssl_tls13_hello_retry_request_magic,
2258 MBEDTLS_SERVER_HELLO_RANDOM_LEN);
2259 } else {
2260 memcpy(p, &ssl->handshake->randbytes[MBEDTLS_CLIENT_HELLO_RANDOM_LEN],
2261 MBEDTLS_SERVER_HELLO_RANDOM_LEN);
Jerry Yu23f7a6f2022-04-23 15:16:45 +08002262 }
Gilles Peskine449bd832023-01-11 14:50:10 +01002263 MBEDTLS_SSL_DEBUG_BUF(3, "server hello, random bytes",
2264 p, MBEDTLS_SERVER_HELLO_RANDOM_LEN);
Jerry Yu3bf2c642022-03-30 22:02:12 +08002265 p += MBEDTLS_SERVER_HELLO_RANDOM_LEN;
2266
Jerry Yucfc04b32022-04-21 09:31:58 +08002267 /* ...
2268 * opaque legacy_session_id_echo<0..32>;
2269 * ...
Jerry Yu3bf2c642022-03-30 22:02:12 +08002270 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002271 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 1 + ssl->session_negotiate->id_len);
2272 *p++ = (unsigned char) ssl->session_negotiate->id_len;
2273 if (ssl->session_negotiate->id_len > 0) {
2274 memcpy(p, &ssl->session_negotiate->id[0],
2275 ssl->session_negotiate->id_len);
Jerry Yu3bf2c642022-03-30 22:02:12 +08002276 p += ssl->session_negotiate->id_len;
Jerry Yu955ddd72022-04-22 22:27:33 +08002277
Gilles Peskine449bd832023-01-11 14:50:10 +01002278 MBEDTLS_SSL_DEBUG_BUF(3, "session id", ssl->session_negotiate->id,
2279 ssl->session_negotiate->id_len);
Jerry Yu3bf2c642022-03-30 22:02:12 +08002280 }
2281
Jerry Yucfc04b32022-04-21 09:31:58 +08002282 /* ...
2283 * CipherSuite cipher_suite;
2284 * ...
2285 * with CipherSuite defined as:
2286 * uint8 CipherSuite[2];
Jerry Yu3bf2c642022-03-30 22:02:12 +08002287 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002288 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 2);
2289 MBEDTLS_PUT_UINT16_BE(ssl->session_negotiate->ciphersuite, p, 0);
Jerry Yu3bf2c642022-03-30 22:02:12 +08002290 p += 2;
Gilles Peskine449bd832023-01-11 14:50:10 +01002291 MBEDTLS_SSL_DEBUG_MSG(3,
2292 ("server hello, chosen ciphersuite: %s ( id=%d )",
2293 mbedtls_ssl_get_ciphersuite_name(
2294 ssl->session_negotiate->ciphersuite),
2295 ssl->session_negotiate->ciphersuite));
Jerry Yu3bf2c642022-03-30 22:02:12 +08002296
Jerry Yucfc04b32022-04-21 09:31:58 +08002297 /* ...
2298 * uint8 legacy_compression_method = 0;
2299 * ...
2300 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002301 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 1);
Thomas Daubney31e03a82022-07-25 15:59:25 +01002302 *p++ = MBEDTLS_SSL_COMPRESS_NULL;
Jerry Yu3bf2c642022-03-30 22:02:12 +08002303
Jerry Yucfc04b32022-04-21 09:31:58 +08002304 /* ...
2305 * Extension extensions<6..2^16-1>;
2306 * ...
2307 * struct {
2308 * ExtensionType extension_type; (2 bytes)
2309 * opaque extension_data<0..2^16-1>;
2310 * } Extension;
2311 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002312 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 2);
Jerry Yud9436a12022-04-20 22:28:09 +08002313 p_extensions_len = p;
Jerry Yu3bf2c642022-03-30 22:02:12 +08002314 p += 2;
2315
Gilles Peskine449bd832023-01-11 14:50:10 +01002316 if ((ret = ssl_tls13_write_server_hello_supported_versions_ext(
2317 ssl, p, end, &output_len)) != 0) {
Jerry Yu955ddd72022-04-22 22:27:33 +08002318 MBEDTLS_SSL_DEBUG_RET(
Gilles Peskine449bd832023-01-11 14:50:10 +01002319 1, "ssl_tls13_write_server_hello_supported_versions_ext", ret);
2320 return ret;
Jerry Yu3bf2c642022-03-30 22:02:12 +08002321 }
2322 p += output_len;
2323
Gilles Peskine449bd832023-01-11 14:50:10 +01002324 if (mbedtls_ssl_tls13_key_exchange_mode_with_ephemeral(ssl)) {
2325 if (is_hrr) {
2326 ret = ssl_tls13_write_hrr_key_share_ext(ssl, p, end, &output_len);
2327 } else {
2328 ret = ssl_tls13_write_key_share_ext(ssl, p, end, &output_len);
2329 }
2330 if (ret != 0) {
2331 return ret;
2332 }
Jerry Yu3bf2c642022-03-30 22:02:12 +08002333 p += output_len;
2334 }
Jerry Yu3bf2c642022-03-30 22:02:12 +08002335
Ronald Cron41a443a2022-10-04 16:38:25 +02002336#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01002337 if (!is_hrr && mbedtls_ssl_tls13_key_exchange_mode_with_psk(ssl)) {
2338 ret = ssl_tls13_write_server_pre_shared_key_ext(ssl, p, end, &output_len);
2339 if (ret != 0) {
2340 MBEDTLS_SSL_DEBUG_RET(1, "ssl_tls13_write_server_pre_shared_key_ext",
2341 ret);
2342 return ret;
Jerry Yu032b15ce2022-07-11 06:10:03 +00002343 }
2344 p += output_len;
2345 }
Ronald Cron41a443a2022-10-04 16:38:25 +02002346#endif
Jerry Yu032b15ce2022-07-11 06:10:03 +00002347
Gilles Peskine449bd832023-01-11 14:50:10 +01002348 MBEDTLS_PUT_UINT16_BE(p - p_extensions_len - 2, p_extensions_len, 0);
Jerry Yu3bf2c642022-03-30 22:02:12 +08002349
Gilles Peskine449bd832023-01-11 14:50:10 +01002350 MBEDTLS_SSL_DEBUG_BUF(4, "server hello extensions",
2351 p_extensions_len, p - p_extensions_len);
Jerry Yu3bf2c642022-03-30 22:02:12 +08002352
Jerry Yud9436a12022-04-20 22:28:09 +08002353 *out_len = p - buf;
Jerry Yu3bf2c642022-03-30 22:02:12 +08002354
Gilles Peskine449bd832023-01-11 14:50:10 +01002355 MBEDTLS_SSL_DEBUG_BUF(3, "server hello", buf, *out_len);
Jerry Yu3bf2c642022-03-30 22:02:12 +08002356
Jerry Yu7de2ff02022-11-08 21:43:46 +08002357 MBEDTLS_SSL_PRINT_EXTS(
Jerry Yu4b8f2f72022-10-31 13:31:22 +08002358 3, is_hrr ? MBEDTLS_SSL_TLS1_3_HS_HELLO_RETRY_REQUEST :
Gilles Peskine449bd832023-01-11 14:50:10 +01002359 MBEDTLS_SSL_HS_SERVER_HELLO,
2360 ssl->handshake->sent_extensions);
Jerry Yu4b8f2f72022-10-31 13:31:22 +08002361
Gilles Peskine449bd832023-01-11 14:50:10 +01002362 return ret;
Jerry Yu56404d72022-03-30 17:36:13 +08002363}
2364
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002365MBEDTLS_CHECK_RETURN_CRITICAL
Xiaokang Qian934ce6f2023-02-06 10:23:04 +00002366static int ssl_tls13_finalize_server_hello(mbedtls_ssl_context *ssl)
Jerry Yue110d252022-05-05 10:19:22 +08002367{
2368 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine449bd832023-01-11 14:50:10 +01002369 ret = mbedtls_ssl_tls13_compute_handshake_transform(ssl);
2370 if (ret != 0) {
2371 MBEDTLS_SSL_DEBUG_RET(1,
2372 "mbedtls_ssl_tls13_compute_handshake_transform",
2373 ret);
2374 return ret;
Jerry Yue110d252022-05-05 10:19:22 +08002375 }
2376
Gilles Peskine449bd832023-01-11 14:50:10 +01002377 return ret;
Jerry Yue110d252022-05-05 10:19:22 +08002378}
2379
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002380MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002381static int ssl_tls13_write_server_hello(mbedtls_ssl_context *ssl)
Jerry Yu5b64ae92022-03-30 17:15:02 +08002382{
Jerry Yu637a3f12022-04-20 21:37:58 +08002383 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yuf4b27e42022-03-30 17:32:21 +08002384 unsigned char *buf;
2385 size_t buf_len, msg_len;
2386
Gilles Peskine449bd832023-01-11 14:50:10 +01002387 MBEDTLS_SSL_DEBUG_MSG(2, ("=> write server hello"));
Jerry Yuf4b27e42022-03-30 17:32:21 +08002388
Gilles Peskine449bd832023-01-11 14:50:10 +01002389 MBEDTLS_SSL_PROC_CHK(ssl_tls13_prepare_server_hello(ssl));
Jerry Yuf4b27e42022-03-30 17:32:21 +08002390
Xiaokang Qian9f1747b2023-03-30 02:50:04 +00002391 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_start_handshake_msg(
2392 ssl, MBEDTLS_SSL_HS_SERVER_HELLO, &buf, &buf_len));
Jerry Yuf4b27e42022-03-30 17:32:21 +08002393
Gilles Peskine449bd832023-01-11 14:50:10 +01002394 MBEDTLS_SSL_PROC_CHK(ssl_tls13_write_server_hello_body(ssl, buf,
2395 buf + buf_len,
2396 &msg_len,
2397 0));
Jerry Yuf4b27e42022-03-30 17:32:21 +08002398
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +01002399 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_add_hs_msg_to_checksum(
Manuel Pégourié-Gonnard43cc1272023-02-06 11:48:19 +01002400 ssl, MBEDTLS_SSL_HS_SERVER_HELLO, buf, msg_len));
Jerry Yuf4b27e42022-03-30 17:32:21 +08002401
Gilles Peskine449bd832023-01-11 14:50:10 +01002402 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_finish_handshake_msg(
2403 ssl, buf_len, msg_len));
Jerry Yu637a3f12022-04-20 21:37:58 +08002404
Xiaokang Qian934ce6f2023-02-06 10:23:04 +00002405 MBEDTLS_SSL_PROC_CHK(ssl_tls13_finalize_server_hello(ssl));
Jerry Yue110d252022-05-05 10:19:22 +08002406
Gabor Mezei7b39bf12022-05-24 16:04:14 +02002407#if defined(MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE)
2408 /* The server sends a dummy change_cipher_spec record immediately
2409 * after its first handshake message. This may either be after
2410 * a ServerHello or a HelloRetryRequest.
2411 */
Gabor Mezei96ae9262022-06-28 11:45:18 +02002412 mbedtls_ssl_handshake_set_state(
Gilles Peskine449bd832023-01-11 14:50:10 +01002413 ssl, MBEDTLS_SSL_SERVER_CCS_AFTER_SERVER_HELLO);
Gabor Mezei7b39bf12022-05-24 16:04:14 +02002414#else
Gilles Peskine449bd832023-01-11 14:50:10 +01002415 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_ENCRYPTED_EXTENSIONS);
Gabor Mezei7b39bf12022-05-24 16:04:14 +02002416#endif /* MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE */
Jerry Yue110d252022-05-05 10:19:22 +08002417
Jerry Yuf4b27e42022-03-30 17:32:21 +08002418cleanup:
2419
Gilles Peskine449bd832023-01-11 14:50:10 +01002420 MBEDTLS_SSL_DEBUG_MSG(2, ("<= write server hello"));
2421 return ret;
Jerry Yu5b64ae92022-03-30 17:15:02 +08002422}
2423
Jerry Yu23d1a252022-05-12 18:08:59 +08002424
2425/*
2426 * Handler for MBEDTLS_SSL_HELLO_RETRY_REQUEST
2427 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002428MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002429static int ssl_tls13_prepare_hello_retry_request(mbedtls_ssl_context *ssl)
Jerry Yu23d1a252022-05-12 18:08:59 +08002430{
2431 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Ronald Cron5fbd2702024-02-14 10:03:36 +01002432 if (ssl->handshake->hello_retry_request_flag) {
Gilles Peskine449bd832023-01-11 14:50:10 +01002433 MBEDTLS_SSL_DEBUG_MSG(1, ("Too many HRRs"));
2434 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE,
2435 MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE);
2436 return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
Jerry Yu23d1a252022-05-12 18:08:59 +08002437 }
2438
2439 /*
2440 * Create stateless transcript hash for HRR
2441 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002442 MBEDTLS_SSL_DEBUG_MSG(4, ("Reset transcript for HRR"));
2443 ret = mbedtls_ssl_reset_transcript_for_hrr(ssl);
2444 if (ret != 0) {
2445 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_reset_transcript_for_hrr", ret);
2446 return ret;
Jerry Yu23d1a252022-05-12 18:08:59 +08002447 }
Gilles Peskine449bd832023-01-11 14:50:10 +01002448 mbedtls_ssl_session_reset_msg_layer(ssl, 0);
Jerry Yu23d1a252022-05-12 18:08:59 +08002449
Gilles Peskine449bd832023-01-11 14:50:10 +01002450 return 0;
Jerry Yu23d1a252022-05-12 18:08:59 +08002451}
2452
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002453MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002454static int ssl_tls13_write_hello_retry_request(mbedtls_ssl_context *ssl)
Jerry Yu23d1a252022-05-12 18:08:59 +08002455{
2456 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
2457 unsigned char *buf;
2458 size_t buf_len, msg_len;
2459
Gilles Peskine449bd832023-01-11 14:50:10 +01002460 MBEDTLS_SSL_DEBUG_MSG(2, ("=> write hello retry request"));
Jerry Yu23d1a252022-05-12 18:08:59 +08002461
Gilles Peskine449bd832023-01-11 14:50:10 +01002462 MBEDTLS_SSL_PROC_CHK(ssl_tls13_prepare_hello_retry_request(ssl));
Jerry Yu23d1a252022-05-12 18:08:59 +08002463
Gilles Peskine449bd832023-01-11 14:50:10 +01002464 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_start_handshake_msg(
2465 ssl, MBEDTLS_SSL_HS_SERVER_HELLO,
2466 &buf, &buf_len));
Jerry Yu23d1a252022-05-12 18:08:59 +08002467
Gilles Peskine449bd832023-01-11 14:50:10 +01002468 MBEDTLS_SSL_PROC_CHK(ssl_tls13_write_server_hello_body(ssl, buf,
2469 buf + buf_len,
2470 &msg_len,
2471 1));
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +01002472 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_add_hs_msg_to_checksum(
Manuel Pégourié-Gonnard43cc1272023-02-06 11:48:19 +01002473 ssl, MBEDTLS_SSL_HS_SERVER_HELLO, buf, msg_len));
Jerry Yu23d1a252022-05-12 18:08:59 +08002474
2475
Gilles Peskine449bd832023-01-11 14:50:10 +01002476 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_finish_handshake_msg(ssl, buf_len,
2477 msg_len));
Jerry Yu23d1a252022-05-12 18:08:59 +08002478
Ronald Cron5fbd2702024-02-14 10:03:36 +01002479 ssl->handshake->hello_retry_request_flag = 1;
Jerry Yu23d1a252022-05-12 18:08:59 +08002480
Gabor Mezei7b39bf12022-05-24 16:04:14 +02002481#if defined(MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE)
2482 /* The server sends a dummy change_cipher_spec record immediately
2483 * after its first handshake message. This may either be after
2484 * a ServerHello or a HelloRetryRequest.
2485 */
Gabor Mezei96ae9262022-06-28 11:45:18 +02002486 mbedtls_ssl_handshake_set_state(
Gilles Peskine449bd832023-01-11 14:50:10 +01002487 ssl, MBEDTLS_SSL_SERVER_CCS_AFTER_HELLO_RETRY_REQUEST);
Gabor Mezei7b39bf12022-05-24 16:04:14 +02002488#else
Gilles Peskine449bd832023-01-11 14:50:10 +01002489 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_CLIENT_HELLO);
Gabor Mezei7b39bf12022-05-24 16:04:14 +02002490#endif /* MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE */
Jerry Yu23d1a252022-05-12 18:08:59 +08002491
2492cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +01002493 MBEDTLS_SSL_DEBUG_MSG(2, ("<= write hello retry request"));
2494 return ret;
Jerry Yu23d1a252022-05-12 18:08:59 +08002495}
2496
Jerry Yu5b64ae92022-03-30 17:15:02 +08002497/*
Jerry Yu4d3841a2022-04-16 12:37:19 +08002498 * Handler for MBEDTLS_SSL_ENCRYPTED_EXTENSIONS
2499 */
Jerry Yu4d3841a2022-04-16 12:37:19 +08002500
2501/*
2502 * struct {
2503 * Extension extensions<0..2 ^ 16 - 1>;
2504 * } EncryptedExtensions;
2505 *
2506 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002507MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002508static int ssl_tls13_write_encrypted_extensions_body(mbedtls_ssl_context *ssl,
2509 unsigned char *buf,
2510 unsigned char *end,
2511 size_t *out_len)
Jerry Yu4d3841a2022-04-16 12:37:19 +08002512{
XiaokangQianacb39922022-06-17 10:18:48 +00002513 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yu4d3841a2022-04-16 12:37:19 +08002514 unsigned char *p = buf;
2515 size_t extensions_len = 0;
Jerry Yu8937eb42022-05-03 12:12:14 +08002516 unsigned char *p_extensions_len;
XiaokangQianacb39922022-06-17 10:18:48 +00002517 size_t output_len;
Jerry Yu9da5e5a2022-05-03 15:46:09 +08002518
Jerry Yu4d3841a2022-04-16 12:37:19 +08002519 *out_len = 0;
2520
Gilles Peskine449bd832023-01-11 14:50:10 +01002521 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 2);
Jerry Yu8937eb42022-05-03 12:12:14 +08002522 p_extensions_len = p;
Jerry Yu4d3841a2022-04-16 12:37:19 +08002523 p += 2;
2524
Jerry Yu8937eb42022-05-03 12:12:14 +08002525 ((void) ssl);
XiaokangQianacb39922022-06-17 10:18:48 +00002526 ((void) ret);
2527 ((void) output_len);
2528
2529#if defined(MBEDTLS_SSL_ALPN)
Gilles Peskine449bd832023-01-11 14:50:10 +01002530 ret = mbedtls_ssl_write_alpn_ext(ssl, p, end, &output_len);
2531 if (ret != 0) {
2532 return ret;
2533 }
XiaokangQian95d5f542022-06-24 02:29:26 +00002534 p += output_len;
XiaokangQianacb39922022-06-17 10:18:48 +00002535#endif /* MBEDTLS_SSL_ALPN */
Jerry Yu4d3841a2022-04-16 12:37:19 +08002536
Jerry Yu71c14f12022-12-12 11:10:35 +08002537#if defined(MBEDTLS_SSL_EARLY_DATA)
Ronald Cron78a38f62024-02-01 18:30:31 +01002538 if (ssl->handshake->early_data_accepted) {
Jerry Yu52335392023-11-23 18:06:06 +08002539 ret = mbedtls_ssl_tls13_write_early_data_ext(
Jerry Yuc59c5862023-12-05 10:40:49 +08002540 ssl, 0, p, end, &output_len);
Jerry Yu71c14f12022-12-12 11:10:35 +08002541 if (ret != 0) {
2542 return ret;
2543 }
2544 p += output_len;
2545 }
2546#endif /* MBEDTLS_SSL_EARLY_DATA */
2547
Waleed Elmelegy47d29462024-01-03 17:31:52 +00002548#if defined(MBEDTLS_SSL_RECORD_SIZE_LIMIT)
Waleed Elmelegyfbe42742024-01-05 18:11:10 +00002549 if (ssl->handshake->received_extensions & MBEDTLS_SSL_EXT_MASK(RECORD_SIZE_LIMIT)) {
Waleed Elmelegyd2fc90e2024-01-04 18:04:53 +00002550 ret = mbedtls_ssl_tls13_write_record_size_limit_ext(
2551 ssl, p, end, &output_len);
2552 if (ret != 0) {
2553 return ret;
2554 }
2555 p += output_len;
Waleed Elmelegy47d29462024-01-03 17:31:52 +00002556 }
Waleed Elmelegy47d29462024-01-03 17:31:52 +00002557#endif
2558
Gilles Peskine449bd832023-01-11 14:50:10 +01002559 extensions_len = (p - p_extensions_len) - 2;
2560 MBEDTLS_PUT_UINT16_BE(extensions_len, p_extensions_len, 0);
Jerry Yu8937eb42022-05-03 12:12:14 +08002561
2562 *out_len = p - buf;
Jerry Yu4d3841a2022-04-16 12:37:19 +08002563
Gilles Peskine449bd832023-01-11 14:50:10 +01002564 MBEDTLS_SSL_DEBUG_BUF(4, "encrypted extensions", buf, *out_len);
Jerry Yu4d3841a2022-04-16 12:37:19 +08002565
Jerry Yu7de2ff02022-11-08 21:43:46 +08002566 MBEDTLS_SSL_PRINT_EXTS(
Gilles Peskine449bd832023-01-11 14:50:10 +01002567 3, MBEDTLS_SSL_HS_ENCRYPTED_EXTENSIONS, ssl->handshake->sent_extensions);
Jerry Yu4b8f2f72022-10-31 13:31:22 +08002568
Gilles Peskine449bd832023-01-11 14:50:10 +01002569 return 0;
Jerry Yu4d3841a2022-04-16 12:37:19 +08002570}
2571
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002572MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002573static int ssl_tls13_write_encrypted_extensions(mbedtls_ssl_context *ssl)
Jerry Yu4d3841a2022-04-16 12:37:19 +08002574{
2575 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
2576 unsigned char *buf;
Jerry Yu39730a72022-05-03 12:14:04 +08002577 size_t buf_len, msg_len;
Jerry Yu4d3841a2022-04-16 12:37:19 +08002578
Gilles Peskine449bd832023-01-11 14:50:10 +01002579 mbedtls_ssl_set_outbound_transform(ssl,
2580 ssl->handshake->transform_handshake);
Gabor Mezei54719122022-06-28 11:34:56 +02002581 MBEDTLS_SSL_DEBUG_MSG(
Gilles Peskine449bd832023-01-11 14:50:10 +01002582 3, ("switching to handshake transform for outbound data"));
Gabor Mezei54719122022-06-28 11:34:56 +02002583
Gilles Peskine449bd832023-01-11 14:50:10 +01002584 MBEDTLS_SSL_DEBUG_MSG(2, ("=> write encrypted extensions"));
Jerry Yu4d3841a2022-04-16 12:37:19 +08002585
Xiaokang Qian9f1747b2023-03-30 02:50:04 +00002586 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_start_handshake_msg(
2587 ssl, MBEDTLS_SSL_HS_ENCRYPTED_EXTENSIONS,
2588 &buf, &buf_len));
Jerry Yu4d3841a2022-04-16 12:37:19 +08002589
Gilles Peskine449bd832023-01-11 14:50:10 +01002590 MBEDTLS_SSL_PROC_CHK(ssl_tls13_write_encrypted_extensions_body(
2591 ssl, buf, buf + buf_len, &msg_len));
Jerry Yu4d3841a2022-04-16 12:37:19 +08002592
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +01002593 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_add_hs_msg_to_checksum(
Xiaokang Qian9f1747b2023-03-30 02:50:04 +00002594 ssl, MBEDTLS_SSL_HS_ENCRYPTED_EXTENSIONS,
2595 buf, msg_len));
Jerry Yu4d3841a2022-04-16 12:37:19 +08002596
Gilles Peskine449bd832023-01-11 14:50:10 +01002597 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_finish_handshake_msg(
2598 ssl, buf_len, msg_len));
Jerry Yu4d3841a2022-04-16 12:37:19 +08002599
Ronald Cron928cbd32022-10-04 16:14:26 +02002600#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01002601 if (mbedtls_ssl_tls13_key_exchange_mode_with_psk(ssl)) {
2602 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_SERVER_FINISHED);
2603 } else {
2604 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_CERTIFICATE_REQUEST);
2605 }
Jerry Yu8937eb42022-05-03 12:12:14 +08002606#else
Gilles Peskine449bd832023-01-11 14:50:10 +01002607 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_SERVER_FINISHED);
Jerry Yu8937eb42022-05-03 12:12:14 +08002608#endif
2609
Jerry Yu4d3841a2022-04-16 12:37:19 +08002610cleanup:
2611
Gilles Peskine449bd832023-01-11 14:50:10 +01002612 MBEDTLS_SSL_DEBUG_MSG(2, ("<= write encrypted extensions"));
2613 return ret;
Jerry Yu4d3841a2022-04-16 12:37:19 +08002614}
2615
Ronald Cron928cbd32022-10-04 16:14:26 +02002616#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED)
XiaokangQiana987e1d2022-05-07 01:25:58 +00002617#define SSL_CERTIFICATE_REQUEST_SEND_REQUEST 0
2618#define SSL_CERTIFICATE_REQUEST_SKIP 1
2619/* Coordination:
2620 * Check whether a CertificateRequest message should be written.
2621 * Returns a negative code on failure, or
2622 * - SSL_CERTIFICATE_REQUEST_SEND_REQUEST
2623 * - SSL_CERTIFICATE_REQUEST_SKIP
2624 * indicating if the writing of the CertificateRequest
2625 * should be skipped or not.
2626 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002627MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002628static int ssl_tls13_certificate_request_coordinate(mbedtls_ssl_context *ssl)
XiaokangQiana987e1d2022-05-07 01:25:58 +00002629{
2630 int authmode;
2631
XiaokangQian40a35232022-05-07 09:02:40 +00002632#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Gilles Peskine449bd832023-01-11 14:50:10 +01002633 if (ssl->handshake->sni_authmode != MBEDTLS_SSL_VERIFY_UNSET) {
XiaokangQian40a35232022-05-07 09:02:40 +00002634 authmode = ssl->handshake->sni_authmode;
Gilles Peskine449bd832023-01-11 14:50:10 +01002635 } else
XiaokangQian40a35232022-05-07 09:02:40 +00002636#endif
XiaokangQiana987e1d2022-05-07 01:25:58 +00002637 authmode = ssl->conf->authmode;
2638
Gilles Peskine449bd832023-01-11 14:50:10 +01002639 if (authmode == MBEDTLS_SSL_VERIFY_NONE) {
Ronald Croneac00ad2022-09-13 10:16:31 +02002640 ssl->session_negotiate->verify_result = MBEDTLS_X509_BADCERT_SKIP_VERIFY;
Gilles Peskine449bd832023-01-11 14:50:10 +01002641 return SSL_CERTIFICATE_REQUEST_SKIP;
Ronald Croneac00ad2022-09-13 10:16:31 +02002642 }
XiaokangQiana987e1d2022-05-07 01:25:58 +00002643
XiaokangQianc3017f62022-05-13 05:55:41 +00002644 ssl->handshake->certificate_request_sent = 1;
XiaokangQian189ded22022-05-10 08:12:17 +00002645
Gilles Peskine449bd832023-01-11 14:50:10 +01002646 return SSL_CERTIFICATE_REQUEST_SEND_REQUEST;
XiaokangQiana987e1d2022-05-07 01:25:58 +00002647}
2648
XiaokangQiancec9ae62022-05-06 07:28:50 +00002649/*
2650 * struct {
2651 * opaque certificate_request_context<0..2^8-1>;
2652 * Extension extensions<2..2^16-1>;
2653 * } CertificateRequest;
2654 *
2655 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002656MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002657static int ssl_tls13_write_certificate_request_body(mbedtls_ssl_context *ssl,
2658 unsigned char *buf,
2659 const unsigned char *end,
2660 size_t *out_len)
XiaokangQiancec9ae62022-05-06 07:28:50 +00002661{
2662 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
2663 unsigned char *p = buf;
XiaokangQianec6efb92022-05-06 09:53:10 +00002664 size_t output_len = 0;
XiaokangQiancec9ae62022-05-06 07:28:50 +00002665 unsigned char *p_extensions_len;
2666
2667 *out_len = 0;
2668
2669 /* Check if we have enough space:
2670 * - certificate_request_context (1 byte)
2671 * - extensions length (2 bytes)
2672 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002673 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 3);
XiaokangQiancec9ae62022-05-06 07:28:50 +00002674
2675 /*
2676 * Write certificate_request_context
2677 */
2678 /*
2679 * We use a zero length context for the normal handshake
2680 * messages. For post-authentication handshake messages
2681 * this request context would be set to a non-zero value.
2682 */
2683 *p++ = 0x0;
2684
2685 /*
2686 * Write extensions
2687 */
2688 /* The extensions must contain the signature_algorithms. */
2689 p_extensions_len = p;
2690 p += 2;
Gilles Peskine449bd832023-01-11 14:50:10 +01002691 ret = mbedtls_ssl_write_sig_alg_ext(ssl, p, end, &output_len);
2692 if (ret != 0) {
2693 return ret;
2694 }
XiaokangQiancec9ae62022-05-06 07:28:50 +00002695
XiaokangQianec6efb92022-05-06 09:53:10 +00002696 p += output_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01002697 MBEDTLS_PUT_UINT16_BE(p - p_extensions_len - 2, p_extensions_len, 0);
XiaokangQiancec9ae62022-05-06 07:28:50 +00002698
2699 *out_len = p - buf;
2700
Jerry Yu7de2ff02022-11-08 21:43:46 +08002701 MBEDTLS_SSL_PRINT_EXTS(
Gilles Peskine449bd832023-01-11 14:50:10 +01002702 3, MBEDTLS_SSL_HS_CERTIFICATE_REQUEST, ssl->handshake->sent_extensions);
Jerry Yu4b8f2f72022-10-31 13:31:22 +08002703
Gilles Peskine449bd832023-01-11 14:50:10 +01002704 return 0;
XiaokangQiancec9ae62022-05-06 07:28:50 +00002705}
2706
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002707MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002708static int ssl_tls13_write_certificate_request(mbedtls_ssl_context *ssl)
XiaokangQiancec9ae62022-05-06 07:28:50 +00002709{
2710 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
2711
Gilles Peskine449bd832023-01-11 14:50:10 +01002712 MBEDTLS_SSL_DEBUG_MSG(2, ("=> write certificate request"));
XiaokangQiancec9ae62022-05-06 07:28:50 +00002713
Gilles Peskine449bd832023-01-11 14:50:10 +01002714 MBEDTLS_SSL_PROC_CHK_NEG(ssl_tls13_certificate_request_coordinate(ssl));
XiaokangQiancec9ae62022-05-06 07:28:50 +00002715
Gilles Peskine449bd832023-01-11 14:50:10 +01002716 if (ret == SSL_CERTIFICATE_REQUEST_SEND_REQUEST) {
XiaokangQiancec9ae62022-05-06 07:28:50 +00002717 unsigned char *buf;
2718 size_t buf_len, msg_len;
2719
Xiaokang Qian9f1747b2023-03-30 02:50:04 +00002720 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_start_handshake_msg(
2721 ssl, MBEDTLS_SSL_HS_CERTIFICATE_REQUEST,
2722 &buf, &buf_len));
XiaokangQiancec9ae62022-05-06 07:28:50 +00002723
Gilles Peskine449bd832023-01-11 14:50:10 +01002724 MBEDTLS_SSL_PROC_CHK(ssl_tls13_write_certificate_request_body(
2725 ssl, buf, buf + buf_len, &msg_len));
XiaokangQiancec9ae62022-05-06 07:28:50 +00002726
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +01002727 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_add_hs_msg_to_checksum(
Xiaokang Qian9f1747b2023-03-30 02:50:04 +00002728 ssl, MBEDTLS_SSL_HS_CERTIFICATE_REQUEST,
2729 buf, msg_len));
XiaokangQiancec9ae62022-05-06 07:28:50 +00002730
Gilles Peskine449bd832023-01-11 14:50:10 +01002731 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_finish_handshake_msg(
2732 ssl, buf_len, msg_len));
2733 } else if (ret == SSL_CERTIFICATE_REQUEST_SKIP) {
2734 MBEDTLS_SSL_DEBUG_MSG(2, ("<= skip write certificate request"));
XiaokangQiancec9ae62022-05-06 07:28:50 +00002735 ret = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01002736 } else {
2737 MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
XiaokangQiancec9ae62022-05-06 07:28:50 +00002738 ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
2739 goto cleanup;
2740 }
2741
Gilles Peskine449bd832023-01-11 14:50:10 +01002742 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_SERVER_CERTIFICATE);
XiaokangQiancec9ae62022-05-06 07:28:50 +00002743cleanup:
2744
Gilles Peskine449bd832023-01-11 14:50:10 +01002745 MBEDTLS_SSL_DEBUG_MSG(2, ("<= write certificate request"));
2746 return ret;
XiaokangQiancec9ae62022-05-06 07:28:50 +00002747}
XiaokangQiancec9ae62022-05-06 07:28:50 +00002748
Jerry Yu4d3841a2022-04-16 12:37:19 +08002749/*
Jerry Yuc6e6dbf2022-04-16 19:42:57 +08002750 * Handler for MBEDTLS_SSL_SERVER_CERTIFICATE
Jerry Yu83da34e2022-04-16 13:59:52 +08002751 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002752MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002753static int ssl_tls13_write_server_certificate(mbedtls_ssl_context *ssl)
Jerry Yu83da34e2022-04-16 13:59:52 +08002754{
Jerry Yu5a26f302022-05-10 20:46:40 +08002755 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
XiaokangQianfb665a82022-06-15 03:57:21 +00002756
2757#if defined(MBEDTLS_X509_CRT_PARSE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01002758 if ((ssl_tls13_pick_key_cert(ssl) != 0) ||
2759 mbedtls_ssl_own_cert(ssl) == NULL) {
2760 MBEDTLS_SSL_DEBUG_MSG(2, ("No certificate available."));
2761 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE,
2762 MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE);
2763 return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
Jerry Yu5a26f302022-05-10 20:46:40 +08002764 }
XiaokangQianfb665a82022-06-15 03:57:21 +00002765#endif /* MBEDTLS_X509_CRT_PARSE_C */
Jerry Yu5a26f302022-05-10 20:46:40 +08002766
Gilles Peskine449bd832023-01-11 14:50:10 +01002767 ret = mbedtls_ssl_tls13_write_certificate(ssl);
2768 if (ret != 0) {
2769 return ret;
2770 }
2771 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_CERTIFICATE_VERIFY);
2772 return 0;
Jerry Yu83da34e2022-04-16 13:59:52 +08002773}
2774
2775/*
Jerry Yuc6e6dbf2022-04-16 19:42:57 +08002776 * Handler for MBEDTLS_SSL_CERTIFICATE_VERIFY
Jerry Yu83da34e2022-04-16 13:59:52 +08002777 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002778MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002779static int ssl_tls13_write_certificate_verify(mbedtls_ssl_context *ssl)
Jerry Yu83da34e2022-04-16 13:59:52 +08002780{
Gilles Peskine449bd832023-01-11 14:50:10 +01002781 int ret = mbedtls_ssl_tls13_write_certificate_verify(ssl);
2782 if (ret != 0) {
2783 return ret;
2784 }
2785 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_SERVER_FINISHED);
2786 return 0;
Jerry Yu83da34e2022-04-16 13:59:52 +08002787}
Ronald Cron928cbd32022-10-04 16:14:26 +02002788#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED */
Jerry Yu83da34e2022-04-16 13:59:52 +08002789
Jerry Yu9b72e392023-12-01 16:27:08 +08002790/*
2791 * RFC 8446 section A.2
2792 *
2793 * | Send ServerHello
2794 * | K_send = handshake
2795 * | Send EncryptedExtensions
2796 * | [Send CertificateRequest]
2797 * Can send | [Send Certificate + CertificateVerify]
2798 * app data | Send Finished
2799 * after --> | K_send = application
2800 * here +--------+--------+
2801 * No 0-RTT | | 0-RTT
2802 * | |
2803 * K_recv = handshake | | K_recv = early data
2804 * [Skip decrypt errors] | +------> WAIT_EOED -+
2805 * | | Recv | | Recv EndOfEarlyData
2806 * | | early data | | K_recv = handshake
2807 * | +------------+ |
2808 * | |
2809 * +> WAIT_FLIGHT2 <--------+
2810 * |
2811 * +--------+--------+
2812 * No auth | | Client auth
2813 * | |
2814 * | v
2815 * | WAIT_CERT
2816 * | Recv | | Recv Certificate
2817 * | empty | v
2818 * | Certificate | WAIT_CV
2819 * | | | Recv
2820 * | v | CertificateVerify
2821 * +-> WAIT_FINISHED <---+
2822 * | Recv Finished
2823 *
2824 *
2825 * The following function handles the state changes after WAIT_FLIGHT2 in the
Jerry Yu3be85072023-12-04 09:58:54 +08002826 * above diagram. We are not going to receive early data related messages
2827 * anymore, prepare to receive the first handshake message of the client
2828 * second flight.
Jerry Yu9b72e392023-12-01 16:27:08 +08002829 */
Jerry Yu3be85072023-12-04 09:58:54 +08002830static void ssl_tls13_prepare_for_handshake_second_flight(
2831 mbedtls_ssl_context *ssl)
Jerry Yu9b72e392023-12-01 16:27:08 +08002832{
Jerry Yu9b72e392023-12-01 16:27:08 +08002833 if (ssl->handshake->certificate_request_sent) {
2834 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_CLIENT_CERTIFICATE);
2835 } else {
Jerry Yu42020fb2023-12-05 17:35:53 +08002836 MBEDTLS_SSL_DEBUG_MSG(2, ("skip parse certificate"));
Jerry Yuebb1b1d2023-12-05 11:02:15 +08002837 MBEDTLS_SSL_DEBUG_MSG(2, ("skip parse certificate verify"));
Jerry Yuebb1b1d2023-12-05 11:02:15 +08002838
Jerry Yu9b72e392023-12-01 16:27:08 +08002839 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_CLIENT_FINISHED);
2840 }
Jerry Yu9b72e392023-12-01 16:27:08 +08002841}
2842
Jerry Yu83da34e2022-04-16 13:59:52 +08002843/*
Jerry Yud6e253d2022-05-18 13:59:24 +08002844 * Handler for MBEDTLS_SSL_SERVER_FINISHED
Jerry Yu69dd8d42022-04-16 12:51:26 +08002845 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002846MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002847static int ssl_tls13_write_server_finished(mbedtls_ssl_context *ssl)
Jerry Yu69dd8d42022-04-16 12:51:26 +08002848{
Jerry Yud6e253d2022-05-18 13:59:24 +08002849 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yu27bdc7c2022-04-16 13:33:27 +08002850
Gilles Peskine449bd832023-01-11 14:50:10 +01002851 ret = mbedtls_ssl_tls13_write_finished_message(ssl);
2852 if (ret != 0) {
2853 return ret;
2854 }
Jerry Yu27bdc7c2022-04-16 13:33:27 +08002855
Gilles Peskine449bd832023-01-11 14:50:10 +01002856 ret = mbedtls_ssl_tls13_compute_application_transform(ssl);
2857 if (ret != 0) {
Jerry Yue3d67cb2022-05-19 15:33:10 +08002858 MBEDTLS_SSL_PEND_FATAL_ALERT(
Gilles Peskine449bd832023-01-11 14:50:10 +01002859 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE,
2860 MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE);
2861 return ret;
Jerry Yue3d67cb2022-05-19 15:33:10 +08002862 }
XiaokangQianc3017f62022-05-13 05:55:41 +00002863
Jerry Yu87b5ed42022-12-12 13:07:07 +08002864#if defined(MBEDTLS_SSL_EARLY_DATA)
Ronald Cron78a38f62024-02-01 18:30:31 +01002865 if (ssl->handshake->early_data_accepted) {
Jerry Yu0af63dc2023-12-01 17:14:51 +08002866 /* See RFC 8446 section A.2 for more information */
Jerry Yu87b5ed42022-12-12 13:07:07 +08002867 MBEDTLS_SSL_DEBUG_MSG(
2868 1, ("Switch to early keys for inbound traffic. "
2869 "( K_recv = early data )"));
2870 mbedtls_ssl_set_inbound_transform(
2871 ssl, ssl->handshake->transform_earlydata);
2872 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_END_OF_EARLY_DATA);
2873 return 0;
2874 }
2875#endif /* MBEDTLS_SSL_EARLY_DATA */
Jerry Yu0af63dc2023-12-01 17:14:51 +08002876 MBEDTLS_SSL_DEBUG_MSG(
2877 1, ("Switch to handshake keys for inbound traffic "
2878 "( K_recv = handshake )"));
Gilles Peskine449bd832023-01-11 14:50:10 +01002879 mbedtls_ssl_set_inbound_transform(ssl, ssl->handshake->transform_handshake);
XiaokangQian189ded22022-05-10 08:12:17 +00002880
Jerry Yu3be85072023-12-04 09:58:54 +08002881 ssl_tls13_prepare_for_handshake_second_flight(ssl);
Jerry Yu7d8c3fe2022-12-12 12:59:44 +08002882
2883 return 0;
2884}
2885
Jerry Yu87b5ed42022-12-12 13:07:07 +08002886#if defined(MBEDTLS_SSL_EARLY_DATA)
2887/*
Jerry Yu59d420f2023-12-01 16:30:34 +08002888 * Handler for MBEDTLS_SSL_END_OF_EARLY_DATA
Jerry Yu87b5ed42022-12-12 13:07:07 +08002889 */
Jerry Yu3be85072023-12-04 09:58:54 +08002890#define SSL_GOT_END_OF_EARLY_DATA 0
Jerry Yub55f9eb2023-12-05 10:27:17 +08002891#define SSL_GOT_EARLY_DATA 1
Jerry Yud5c34962023-12-01 16:32:31 +08002892/* Coordination:
Jerry Yu3be85072023-12-04 09:58:54 +08002893 * Deals with the ambiguity of not knowing if the next message is an
2894 * EndOfEarlyData message or an application message containing early data.
Jerry Yud5c34962023-12-01 16:32:31 +08002895 * Returns a negative code on failure, or
Jerry Yu3be85072023-12-04 09:58:54 +08002896 * - SSL_GOT_END_OF_EARLY_DATA
Jerry Yub55f9eb2023-12-05 10:27:17 +08002897 * - SSL_GOT_EARLY_DATA
Jerry Yud5c34962023-12-01 16:32:31 +08002898 * indicating which message is received.
2899 */
2900MBEDTLS_CHECK_RETURN_CRITICAL
2901static int ssl_tls13_end_of_early_data_coordinate(mbedtls_ssl_context *ssl)
2902{
2903 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yub4ed4602023-12-01 16:34:00 +08002904
2905 if ((ret = mbedtls_ssl_read_record(ssl, 0)) != 0) {
2906 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_read_record", ret);
2907 return ret;
2908 }
2909 ssl->keep_current_message = 1;
2910
2911 if (ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
2912 ssl->in_msg[0] == MBEDTLS_SSL_HS_END_OF_EARLY_DATA) {
Jerry Yub55f9eb2023-12-05 10:27:17 +08002913 MBEDTLS_SSL_DEBUG_MSG(3, ("Received an end_of_early_data message."));
Jerry Yu3be85072023-12-04 09:58:54 +08002914 return SSL_GOT_END_OF_EARLY_DATA;
Jerry Yub4ed4602023-12-01 16:34:00 +08002915 }
2916
2917 if (ssl->in_msgtype == MBEDTLS_SSL_MSG_APPLICATION_DATA) {
Jerry Yub55f9eb2023-12-05 10:27:17 +08002918 MBEDTLS_SSL_DEBUG_MSG(3, ("Received early data"));
Jerry Yu6a5904d2023-12-06 17:11:12 +08002919 /* RFC 8446 section 4.6.1
2920 *
2921 * A server receiving more than max_early_data_size bytes of 0-RTT data
2922 * SHOULD terminate the connection with an "unexpected_message" alert.
2923 *
2924 * TODO: Add received data size check here.
2925 */
Ronald Croned7d4bf2024-01-31 07:55:19 +01002926 if (ssl->in_offt == NULL) {
2927 /* Set the reading pointer */
2928 ssl->in_offt = ssl->in_msg;
2929 }
Jerry Yub55f9eb2023-12-05 10:27:17 +08002930 return SSL_GOT_EARLY_DATA;
Jerry Yub4ed4602023-12-01 16:34:00 +08002931 }
2932
Jerry Yu7bb40a32023-12-04 10:04:15 +08002933 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE,
2934 MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE);
Jerry Yub4ed4602023-12-01 16:34:00 +08002935 return MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE;
Jerry Yud5c34962023-12-01 16:32:31 +08002936}
2937
2938MBEDTLS_CHECK_RETURN_CRITICAL
2939static int ssl_tls13_parse_end_of_early_data(mbedtls_ssl_context *ssl,
2940 const unsigned char *buf,
2941 const unsigned char *end)
2942{
Jerry Yu75c9ab72023-12-01 16:41:40 +08002943 /* RFC 8446 section 4.5
2944 *
2945 * struct {} EndOfEarlyData;
2946 */
Jerry Yufbf03992023-12-04 10:00:37 +08002947 if (buf != end) {
2948 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR,
2949 MBEDTLS_ERR_SSL_DECODE_ERROR);
2950 return MBEDTLS_ERR_SSL_DECODE_ERROR;
2951 }
2952 return 0;
Jerry Yud5c34962023-12-01 16:32:31 +08002953}
2954
Jerry Yud5c34962023-12-01 16:32:31 +08002955/*
2956 * RFC 8446 section A.2
2957 *
2958 * | Send ServerHello
2959 * | K_send = handshake
2960 * | Send EncryptedExtensions
2961 * | [Send CertificateRequest]
2962 * Can send | [Send Certificate + CertificateVerify]
2963 * app data | Send Finished
2964 * after --> | K_send = application
2965 * here +--------+--------+
2966 * No 0-RTT | | 0-RTT
2967 * | |
2968 * K_recv = handshake | | K_recv = early data
2969 * [Skip decrypt errors] | +------> WAIT_EOED -+
2970 * | | Recv | | Recv EndOfEarlyData
2971 * | | early data | | K_recv = handshake
2972 * | +------------+ |
2973 * | |
2974 * +> WAIT_FLIGHT2 <--------+
2975 * |
2976 * +--------+--------+
2977 * No auth | | Client auth
2978 * | |
2979 * | v
2980 * | WAIT_CERT
2981 * | Recv | | Recv Certificate
2982 * | empty | v
2983 * | Certificate | WAIT_CV
2984 * | | | Recv
2985 * | v | CertificateVerify
2986 * +-> WAIT_FINISHED <---+
2987 * | Recv Finished
2988 *
2989 * The function handles actions and state changes from 0-RTT to WAIT_FLIGHT2 in
2990 * the above diagram.
2991 */
Jerry Yu87b5ed42022-12-12 13:07:07 +08002992MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yu59d420f2023-12-01 16:30:34 +08002993static int ssl_tls13_process_end_of_early_data(mbedtls_ssl_context *ssl)
Jerry Yu87b5ed42022-12-12 13:07:07 +08002994{
2995 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yud5c34962023-12-01 16:32:31 +08002996
2997 MBEDTLS_SSL_DEBUG_MSG(2, ("=> ssl_tls13_process_end_of_early_data"));
2998
2999 MBEDTLS_SSL_PROC_CHK_NEG(ssl_tls13_end_of_early_data_coordinate(ssl));
3000
Jerry Yu3be85072023-12-04 09:58:54 +08003001 if (ret == SSL_GOT_END_OF_EARLY_DATA) {
Jerry Yud5c34962023-12-01 16:32:31 +08003002 unsigned char *buf;
3003 size_t buf_len;
3004
3005 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_tls13_fetch_handshake_msg(
3006 ssl, MBEDTLS_SSL_HS_END_OF_EARLY_DATA,
3007 &buf, &buf_len));
3008
3009 MBEDTLS_SSL_PROC_CHK(ssl_tls13_parse_end_of_early_data(
3010 ssl, buf, buf + buf_len));
3011
Jerry Yue9655122023-12-01 16:44:40 +08003012 MBEDTLS_SSL_DEBUG_MSG(
3013 1, ("Switch to handshake keys for inbound traffic"
3014 "( K_recv = handshake )"));
3015 mbedtls_ssl_set_inbound_transform(
3016 ssl, ssl->handshake->transform_handshake);
3017
Jerry Yud5c34962023-12-01 16:32:31 +08003018 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_add_hs_msg_to_checksum(
3019 ssl, MBEDTLS_SSL_HS_END_OF_EARLY_DATA,
3020 buf, buf_len));
Jerry Yue9655122023-12-01 16:44:40 +08003021
Jerry Yu3be85072023-12-04 09:58:54 +08003022 ssl_tls13_prepare_for_handshake_second_flight(ssl);
Jerry Yud5c34962023-12-01 16:32:31 +08003023
Jerry Yub55f9eb2023-12-05 10:27:17 +08003024 } else if (ret == SSL_GOT_EARLY_DATA) {
Jerry Yud9ca3542023-12-06 17:23:52 +08003025 ret = MBEDTLS_ERR_SSL_RECEIVED_EARLY_DATA;
3026 goto cleanup;
Jerry Yud5c34962023-12-01 16:32:31 +08003027 } else {
3028 MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
3029 ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
3030 goto cleanup;
3031 }
3032
Jerry Yud5c34962023-12-01 16:32:31 +08003033cleanup:
3034 MBEDTLS_SSL_DEBUG_MSG(2, ("<= ssl_tls13_process_end_of_early_data"));
Jerry Yu87b5ed42022-12-12 13:07:07 +08003035 return ret;
3036}
3037#endif /* MBEDTLS_SSL_EARLY_DATA */
3038
Jerry Yu69dd8d42022-04-16 12:51:26 +08003039/*
Jerry Yud6e253d2022-05-18 13:59:24 +08003040 * Handler for MBEDTLS_SSL_CLIENT_FINISHED
Jerry Yu69dd8d42022-04-16 12:51:26 +08003041 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02003042MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01003043static int ssl_tls13_process_client_finished(mbedtls_ssl_context *ssl)
Jerry Yu69dd8d42022-04-16 12:51:26 +08003044{
Jerry Yud6e253d2022-05-18 13:59:24 +08003045 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
3046
Gilles Peskine449bd832023-01-11 14:50:10 +01003047 ret = mbedtls_ssl_tls13_process_finished_message(ssl);
3048 if (ret != 0) {
3049 return ret;
Jerry Yue3d67cb2022-05-19 15:33:10 +08003050 }
3051
Gilles Peskine449bd832023-01-11 14:50:10 +01003052 ret = mbedtls_ssl_tls13_compute_resumption_master_secret(ssl);
3053 if (ret != 0) {
Xiaokang Qian9f1747b2023-03-30 02:50:04 +00003054 MBEDTLS_SSL_DEBUG_RET(
3055 1, "mbedtls_ssl_tls13_compute_resumption_master_secret", ret);
Gilles Peskine449bd832023-01-11 14:50:10 +01003056 }
3057
3058 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_HANDSHAKE_WRAPUP);
3059 return 0;
Jerry Yu69dd8d42022-04-16 12:51:26 +08003060}
3061
3062/*
Jerry Yud6e253d2022-05-18 13:59:24 +08003063 * Handler for MBEDTLS_SSL_HANDSHAKE_WRAPUP
Jerry Yu69dd8d42022-04-16 12:51:26 +08003064 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02003065MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01003066static int ssl_tls13_handshake_wrapup(mbedtls_ssl_context *ssl)
Jerry Yu69dd8d42022-04-16 12:51:26 +08003067{
Gilles Peskine449bd832023-01-11 14:50:10 +01003068 MBEDTLS_SSL_DEBUG_MSG(2, ("handshake: done"));
Jerry Yu03ed50b2022-04-16 17:13:30 +08003069
Gilles Peskine449bd832023-01-11 14:50:10 +01003070 mbedtls_ssl_tls13_handshake_wrapup(ssl);
Jerry Yue67bef42022-07-07 07:29:42 +00003071
Pengyu Lv3643fdb2023-01-12 16:46:28 +08003072#if defined(MBEDTLS_SSL_SESSION_TICKETS) && \
3073 defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED)
Pengyu Lva1aa31b2022-12-13 13:49:59 +08003074/* TODO: Remove the check of SOME_PSK_ENABLED since SESSION_TICKETS requires
3075 * SOME_PSK_ENABLED to be enabled. Here is just to make CI happy. It is
3076 * expected to be resolved with issue#6395.
3077 */
Pengyu Lvc7af2c42022-12-01 16:33:00 +08003078 /* Sent NewSessionTicket message only when client supports PSK */
Pengyu Lvb2cfafb2023-11-14 13:56:13 +08003079 if (mbedtls_ssl_tls13_is_some_psk_supported(ssl)) {
Xiaokang Qian9f1747b2023-03-30 02:50:04 +00003080 mbedtls_ssl_handshake_set_state(
3081 ssl, MBEDTLS_SSL_TLS1_3_NEW_SESSION_TICKET);
Pengyu Lvc7af2c42022-12-01 16:33:00 +08003082 } else
Jerry Yufca4d572022-07-21 10:37:48 +08003083#endif
Pengyu Lv3643fdb2023-01-12 16:46:28 +08003084 {
3085 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_HANDSHAKE_OVER);
3086 }
Gilles Peskine449bd832023-01-11 14:50:10 +01003087 return 0;
Jerry Yu69dd8d42022-04-16 12:51:26 +08003088}
3089
3090/*
Jerry Yua8d3c502022-10-30 14:51:23 +08003091 * Handler for MBEDTLS_SSL_TLS1_3_NEW_SESSION_TICKET
Jerry Yue67bef42022-07-07 07:29:42 +00003092 */
3093#define SSL_NEW_SESSION_TICKET_SKIP 0
3094#define SSL_NEW_SESSION_TICKET_WRITE 1
3095MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01003096static int ssl_tls13_write_new_session_ticket_coordinate(mbedtls_ssl_context *ssl)
Jerry Yue67bef42022-07-07 07:29:42 +00003097{
3098 /* Check whether the use of session tickets is enabled */
Gilles Peskine449bd832023-01-11 14:50:10 +01003099 if (ssl->conf->f_ticket_write == NULL) {
3100 MBEDTLS_SSL_DEBUG_MSG(2, ("NewSessionTicket: disabled,"
3101 " callback is not set"));
3102 return SSL_NEW_SESSION_TICKET_SKIP;
Jerry Yud0766ec2022-09-22 10:46:57 +08003103 }
Gilles Peskine449bd832023-01-11 14:50:10 +01003104 if (ssl->conf->new_session_tickets_count == 0) {
3105 MBEDTLS_SSL_DEBUG_MSG(2, ("NewSessionTicket: disabled,"
3106 " configured count is zero"));
3107 return SSL_NEW_SESSION_TICKET_SKIP;
Jerry Yud0766ec2022-09-22 10:46:57 +08003108 }
3109
Gilles Peskine449bd832023-01-11 14:50:10 +01003110 if (ssl->handshake->new_session_tickets_count == 0) {
3111 MBEDTLS_SSL_DEBUG_MSG(2, ("NewSessionTicket: all tickets have "
3112 "been sent."));
3113 return SSL_NEW_SESSION_TICKET_SKIP;
Jerry Yue67bef42022-07-07 07:29:42 +00003114 }
3115
Gilles Peskine449bd832023-01-11 14:50:10 +01003116 return SSL_NEW_SESSION_TICKET_WRITE;
Jerry Yue67bef42022-07-07 07:29:42 +00003117}
3118
3119#if defined(MBEDTLS_SSL_SESSION_TICKETS)
3120MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01003121static int ssl_tls13_prepare_new_session_ticket(mbedtls_ssl_context *ssl,
3122 unsigned char *ticket_nonce,
3123 size_t ticket_nonce_size)
Jerry Yue67bef42022-07-07 07:29:42 +00003124{
3125 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
3126 mbedtls_ssl_session *session = ssl->session;
3127 mbedtls_ssl_ciphersuite_t *ciphersuite_info;
3128 psa_algorithm_t psa_hash_alg;
3129 int hash_length;
3130
Gilles Peskine449bd832023-01-11 14:50:10 +01003131 MBEDTLS_SSL_DEBUG_MSG(2, ("=> prepare NewSessionTicket msg"));
Jerry Yue67bef42022-07-07 07:29:42 +00003132
Pengyu Lv9f926952022-11-17 15:22:33 +08003133 /* Set ticket_flags depends on the advertised psk key exchange mode */
Pengyu Lv94a42cc2023-12-06 10:04:17 +08003134 mbedtls_ssl_tls13_session_clear_ticket_flags(
Pengyu Lv9eacb442022-12-09 14:39:19 +08003135 session, MBEDTLS_SSL_TLS1_3_TICKET_FLAGS_MASK);
Pengyu Lve6487fe2022-12-06 09:30:29 +08003136#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED)
Pengyu Lv94a42cc2023-12-06 10:04:17 +08003137 mbedtls_ssl_tls13_session_set_ticket_flags(
Pengyu Lv9eacb442022-12-09 14:39:19 +08003138 session, ssl->handshake->tls13_kex_modes);
Pengyu Lve6487fe2022-12-06 09:30:29 +08003139#endif
Jerry Yu95648b02023-12-06 15:03:34 +08003140
3141#if defined(MBEDTLS_SSL_EARLY_DATA)
3142 if (ssl->conf->early_data_enabled == MBEDTLS_SSL_EARLY_DATA_ENABLED &&
3143 ssl->conf->max_early_data_size > 0) {
Pengyu Lv94a42cc2023-12-06 10:04:17 +08003144 mbedtls_ssl_tls13_session_set_ticket_flags(
Jerry Yu95648b02023-12-06 15:03:34 +08003145 session, MBEDTLS_SSL_TLS1_3_TICKET_ALLOW_EARLY_DATA);
3146 }
3147#endif /* MBEDTLS_SSL_EARLY_DATA */
3148
Pengyu Lv4938a562023-01-16 11:28:49 +08003149 MBEDTLS_SSL_PRINT_TICKET_FLAGS(4, session->ticket_flags);
Pengyu Lv9f926952022-11-17 15:22:33 +08003150
Jerry Yue67bef42022-07-07 07:29:42 +00003151 /* Generate ticket_age_add */
Gilles Peskine449bd832023-01-11 14:50:10 +01003152 if ((ret = ssl->conf->f_rng(ssl->conf->p_rng,
3153 (unsigned char *) &session->ticket_age_add,
3154 sizeof(session->ticket_age_add)) != 0)) {
3155 MBEDTLS_SSL_DEBUG_RET(1, "generate_ticket_age_add", ret);
3156 return ret;
Jerry Yue67bef42022-07-07 07:29:42 +00003157 }
Gilles Peskine449bd832023-01-11 14:50:10 +01003158 MBEDTLS_SSL_DEBUG_MSG(3, ("ticket_age_add: %u",
3159 (unsigned int) session->ticket_age_add));
Jerry Yue67bef42022-07-07 07:29:42 +00003160
3161 /* Generate ticket_nonce */
Gilles Peskine449bd832023-01-11 14:50:10 +01003162 ret = ssl->conf->f_rng(ssl->conf->p_rng, ticket_nonce, ticket_nonce_size);
3163 if (ret != 0) {
3164 MBEDTLS_SSL_DEBUG_RET(1, "generate_ticket_nonce", ret);
3165 return ret;
Jerry Yue67bef42022-07-07 07:29:42 +00003166 }
Gilles Peskine449bd832023-01-11 14:50:10 +01003167 MBEDTLS_SSL_DEBUG_BUF(3, "ticket_nonce:",
3168 ticket_nonce, ticket_nonce_size);
Jerry Yue67bef42022-07-07 07:29:42 +00003169
3170 ciphersuite_info =
Gilles Peskine449bd832023-01-11 14:50:10 +01003171 (mbedtls_ssl_ciphersuite_t *) ssl->handshake->ciphersuite_info;
Dave Rodgman2eab4622023-10-05 13:30:37 +01003172 psa_hash_alg = mbedtls_md_psa_alg_from_type((mbedtls_md_type_t) ciphersuite_info->mac);
Gilles Peskine449bd832023-01-11 14:50:10 +01003173 hash_length = PSA_HASH_LENGTH(psa_hash_alg);
3174 if (hash_length == -1 ||
3175 (size_t) hash_length > sizeof(session->resumption_key)) {
3176 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Jerry Yufca4d572022-07-21 10:37:48 +08003177 }
3178
Jerry Yue67bef42022-07-07 07:29:42 +00003179 /* In this code the psk key length equals the length of the hash */
3180 session->resumption_key_len = hash_length;
3181 session->ciphersuite = ciphersuite_info->id;
3182
3183 /* Compute resumption key
3184 *
3185 * HKDF-Expand-Label( resumption_master_secret,
3186 * "resumption", ticket_nonce, Hash.length )
3187 */
3188 ret = mbedtls_ssl_tls13_hkdf_expand_label(
Gilles Peskine449bd832023-01-11 14:50:10 +01003189 psa_hash_alg,
3190 session->app_secrets.resumption_master_secret,
3191 hash_length,
3192 MBEDTLS_SSL_TLS1_3_LBL_WITH_LEN(resumption),
3193 ticket_nonce,
3194 ticket_nonce_size,
3195 session->resumption_key,
3196 hash_length);
Jerry Yue67bef42022-07-07 07:29:42 +00003197
Gilles Peskine449bd832023-01-11 14:50:10 +01003198 if (ret != 0) {
3199 MBEDTLS_SSL_DEBUG_RET(2,
3200 "Creating the ticket-resumed PSK failed",
3201 ret);
3202 return ret;
Jerry Yue67bef42022-07-07 07:29:42 +00003203 }
Gilles Peskine449bd832023-01-11 14:50:10 +01003204 MBEDTLS_SSL_DEBUG_BUF(3, "Ticket-resumed PSK",
3205 session->resumption_key,
3206 session->resumption_key_len);
Jerry Yue67bef42022-07-07 07:29:42 +00003207
Gilles Peskine449bd832023-01-11 14:50:10 +01003208 MBEDTLS_SSL_DEBUG_BUF(3, "resumption_master_secret",
3209 session->app_secrets.resumption_master_secret,
3210 hash_length);
Jerry Yue67bef42022-07-07 07:29:42 +00003211
Gilles Peskine449bd832023-01-11 14:50:10 +01003212 return 0;
Jerry Yue67bef42022-07-07 07:29:42 +00003213}
3214
3215/* This function creates a NewSessionTicket message in the following format:
3216 *
3217 * struct {
3218 * uint32 ticket_lifetime;
3219 * uint32 ticket_age_add;
3220 * opaque ticket_nonce<0..255>;
3221 * opaque ticket<1..2^16-1>;
3222 * Extension extensions<0..2^16-2>;
3223 * } NewSessionTicket;
3224 *
3225 * The ticket inside the NewSessionTicket message is an encrypted container
3226 * carrying the necessary information so that the server is later able to
3227 * re-start the communication.
3228 *
3229 * The following fields are placed inside the ticket by the
3230 * f_ticket_write() function:
3231 *
Jerry Yu0069abc2023-11-23 21:07:28 +08003232 * - creation time (ticket_creation_time)
3233 * - flags (ticket_flags)
Jerry Yue67bef42022-07-07 07:29:42 +00003234 * - age add (ticket_age_add)
Jerry Yu0069abc2023-11-23 21:07:28 +08003235 * - key (resumption_key)
3236 * - key length (resumption_key_len)
Jerry Yue67bef42022-07-07 07:29:42 +00003237 * - ciphersuite (ciphersuite)
Jerry Yu0069abc2023-11-23 21:07:28 +08003238 * - max_early_data_size (max_early_data_size)
Jerry Yue67bef42022-07-07 07:29:42 +00003239 */
3240MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01003241static int ssl_tls13_write_new_session_ticket_body(mbedtls_ssl_context *ssl,
3242 unsigned char *buf,
3243 unsigned char *end,
3244 size_t *out_len,
3245 unsigned char *ticket_nonce,
3246 size_t ticket_nonce_size)
Jerry Yue67bef42022-07-07 07:29:42 +00003247{
3248 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
3249 unsigned char *p = buf;
3250 mbedtls_ssl_session *session = ssl->session;
3251 size_t ticket_len;
3252 uint32_t ticket_lifetime;
Jerry Yu01da35e2022-12-12 15:09:22 +08003253 unsigned char *p_extensions_len;
Jerry Yue67bef42022-07-07 07:29:42 +00003254
3255 *out_len = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01003256 MBEDTLS_SSL_DEBUG_MSG(2, ("=> write NewSessionTicket msg"));
Jerry Yue67bef42022-07-07 07:29:42 +00003257
3258 /*
3259 * ticket_lifetime 4 bytes
3260 * ticket_age_add 4 bytes
3261 * ticket_nonce 1 + ticket_nonce_size bytes
3262 * ticket >=2 bytes
3263 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003264 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 4 + 4 + 1 + ticket_nonce_size + 2);
Jerry Yue67bef42022-07-07 07:29:42 +00003265
3266 /* Generate ticket and ticket_lifetime */
Ronald Cron3c0072b2023-11-22 10:00:14 +01003267#if defined(MBEDTLS_HAVE_TIME)
3268 session->ticket_creation_time = mbedtls_ms_time();
3269#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01003270 ret = ssl->conf->f_ticket_write(ssl->conf->p_ticket,
3271 session,
3272 p + 9 + ticket_nonce_size + 2,
3273 end,
3274 &ticket_len,
3275 &ticket_lifetime);
3276 if (ret != 0) {
3277 MBEDTLS_SSL_DEBUG_RET(1, "write_ticket", ret);
3278 return ret;
Jerry Yue67bef42022-07-07 07:29:42 +00003279 }
3280 /* RFC 8446 4.6.1
3281 * ticket_lifetime: Indicates the lifetime in seconds as a 32-bit
3282 * unsigned integer in network byte order from the time of ticket
3283 * issuance. Servers MUST NOT use any value greater than
3284 * 604800 seconds (7 days). The value of zero indicates that the
3285 * ticket should be discarded immediately. Clients MUST NOT cache
3286 * tickets for longer than 7 days, regardless of the ticket_lifetime,
3287 * and MAY delete tickets earlier based on local policy. A server
3288 * MAY treat a ticket as valid for a shorter period of time than what
3289 * is stated in the ticket_lifetime.
3290 */
Jerry Yu8e0174a2023-11-10 13:58:16 +08003291 if (ticket_lifetime > MBEDTLS_SSL_TLS1_3_MAX_ALLOWED_TICKET_LIFETIME) {
3292 ticket_lifetime = MBEDTLS_SSL_TLS1_3_MAX_ALLOWED_TICKET_LIFETIME;
Gilles Peskine449bd832023-01-11 14:50:10 +01003293 }
3294 MBEDTLS_PUT_UINT32_BE(ticket_lifetime, p, 0);
3295 MBEDTLS_SSL_DEBUG_MSG(3, ("ticket_lifetime: %u",
3296 (unsigned int) ticket_lifetime));
Jerry Yue67bef42022-07-07 07:29:42 +00003297
3298 /* Write ticket_age_add */
Gilles Peskine449bd832023-01-11 14:50:10 +01003299 MBEDTLS_PUT_UINT32_BE(session->ticket_age_add, p, 4);
3300 MBEDTLS_SSL_DEBUG_MSG(3, ("ticket_age_add: %u",
3301 (unsigned int) session->ticket_age_add));
Jerry Yue67bef42022-07-07 07:29:42 +00003302
3303 /* Write ticket_nonce */
Gilles Peskine449bd832023-01-11 14:50:10 +01003304 p[8] = (unsigned char) ticket_nonce_size;
3305 if (ticket_nonce_size > 0) {
3306 memcpy(p + 9, ticket_nonce, ticket_nonce_size);
Jerry Yue67bef42022-07-07 07:29:42 +00003307 }
3308 p += 9 + ticket_nonce_size;
3309
3310 /* Write ticket */
Gilles Peskine449bd832023-01-11 14:50:10 +01003311 MBEDTLS_PUT_UINT16_BE(ticket_len, p, 0);
Jerry Yue67bef42022-07-07 07:29:42 +00003312 p += 2;
Gilles Peskine449bd832023-01-11 14:50:10 +01003313 MBEDTLS_SSL_DEBUG_BUF(4, "ticket", p, ticket_len);
Jerry Yue67bef42022-07-07 07:29:42 +00003314 p += ticket_len;
3315
3316 /* Ticket Extensions
3317 *
Jerry Yu01da35e2022-12-12 15:09:22 +08003318 * Extension extensions<0..2^16-2>;
Jerry Yue67bef42022-07-07 07:29:42 +00003319 */
Jerry Yuedab6372022-10-31 14:37:31 +08003320 ssl->handshake->sent_extensions = MBEDTLS_SSL_EXT_MASK_NONE;
3321
Gilles Peskine449bd832023-01-11 14:50:10 +01003322 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 2);
Jerry Yu01da35e2022-12-12 15:09:22 +08003323 p_extensions_len = p;
Jerry Yue67bef42022-07-07 07:29:42 +00003324 p += 2;
3325
Jerry Yu01da35e2022-12-12 15:09:22 +08003326#if defined(MBEDTLS_SSL_EARLY_DATA)
Pengyu Lv94a42cc2023-12-06 10:04:17 +08003327 if (mbedtls_ssl_tls13_session_ticket_allow_early_data(session)) {
Jerry Yu95648b02023-12-06 15:03:34 +08003328 size_t output_len;
3329
Jerry Yuf135bac2023-11-23 18:10:51 +08003330 if ((ret = mbedtls_ssl_tls13_write_early_data_ext(
Jerry Yuc59c5862023-12-05 10:40:49 +08003331 ssl, 1, p, end, &output_len)) != 0) {
Jerry Yuf135bac2023-11-23 18:10:51 +08003332 MBEDTLS_SSL_DEBUG_RET(
3333 1, "mbedtls_ssl_tls13_write_early_data_ext", ret);
3334 return ret;
3335 }
3336 p += output_len;
Jerry Yu9e7f9bc2023-11-27 16:52:07 +08003337 } else {
3338 MBEDTLS_SSL_DEBUG_MSG(
3339 4, ("early_data not allowed, "
3340 "skip early_data extension in NewSessionTicket"));
Jerry Yu01da35e2022-12-12 15:09:22 +08003341 }
Jerry Yuf135bac2023-11-23 18:10:51 +08003342
Jerry Yu01da35e2022-12-12 15:09:22 +08003343#endif /* MBEDTLS_SSL_EARLY_DATA */
3344
3345 MBEDTLS_PUT_UINT16_BE(p - p_extensions_len - 2, p_extensions_len, 0);
3346
Jerry Yue67bef42022-07-07 07:29:42 +00003347 *out_len = p - buf;
Gilles Peskine449bd832023-01-11 14:50:10 +01003348 MBEDTLS_SSL_DEBUG_BUF(4, "ticket", buf, *out_len);
3349 MBEDTLS_SSL_DEBUG_MSG(2, ("<= write new session ticket"));
Jerry Yue67bef42022-07-07 07:29:42 +00003350
Jerry Yu7de2ff02022-11-08 21:43:46 +08003351 MBEDTLS_SSL_PRINT_EXTS(
Gilles Peskine449bd832023-01-11 14:50:10 +01003352 3, MBEDTLS_SSL_HS_NEW_SESSION_TICKET, ssl->handshake->sent_extensions);
Jerry Yu4b8f2f72022-10-31 13:31:22 +08003353
Gilles Peskine449bd832023-01-11 14:50:10 +01003354 return 0;
Jerry Yue67bef42022-07-07 07:29:42 +00003355}
3356
3357/*
Jerry Yua8d3c502022-10-30 14:51:23 +08003358 * Handler for MBEDTLS_SSL_TLS1_3_NEW_SESSION_TICKET
Jerry Yue67bef42022-07-07 07:29:42 +00003359 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003360static int ssl_tls13_write_new_session_ticket(mbedtls_ssl_context *ssl)
Jerry Yue67bef42022-07-07 07:29:42 +00003361{
3362 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
3363
Gilles Peskine449bd832023-01-11 14:50:10 +01003364 MBEDTLS_SSL_PROC_CHK_NEG(ssl_tls13_write_new_session_ticket_coordinate(ssl));
Jerry Yue67bef42022-07-07 07:29:42 +00003365
Gilles Peskine449bd832023-01-11 14:50:10 +01003366 if (ret == SSL_NEW_SESSION_TICKET_WRITE) {
Jerry Yue67bef42022-07-07 07:29:42 +00003367 unsigned char ticket_nonce[MBEDTLS_SSL_TLS1_3_TICKET_NONCE_LENGTH];
3368 unsigned char *buf;
3369 size_t buf_len, msg_len;
3370
Gilles Peskine449bd832023-01-11 14:50:10 +01003371 MBEDTLS_SSL_PROC_CHK(ssl_tls13_prepare_new_session_ticket(
3372 ssl, ticket_nonce, sizeof(ticket_nonce)));
Jerry Yue67bef42022-07-07 07:29:42 +00003373
Xiaokang Qian9f1747b2023-03-30 02:50:04 +00003374 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_start_handshake_msg(
3375 ssl, MBEDTLS_SSL_HS_NEW_SESSION_TICKET,
3376 &buf, &buf_len));
Jerry Yue67bef42022-07-07 07:29:42 +00003377
Gilles Peskine449bd832023-01-11 14:50:10 +01003378 MBEDTLS_SSL_PROC_CHK(ssl_tls13_write_new_session_ticket_body(
3379 ssl, buf, buf + buf_len, &msg_len,
3380 ticket_nonce, sizeof(ticket_nonce)));
Jerry Yue67bef42022-07-07 07:29:42 +00003381
Gilles Peskine449bd832023-01-11 14:50:10 +01003382 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_finish_handshake_msg(
3383 ssl, buf_len, msg_len));
Jerry Yufca4d572022-07-21 10:37:48 +08003384
Jerry Yu359e65f2022-09-22 23:47:43 +08003385 /* Limit session tickets count to one when resumption connection.
3386 *
3387 * See document of mbedtls_ssl_conf_new_session_tickets.
3388 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003389 if (ssl->handshake->resume == 1) {
Jerry Yu359e65f2022-09-22 23:47:43 +08003390 ssl->handshake->new_session_tickets_count = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01003391 } else {
Jerry Yu359e65f2022-09-22 23:47:43 +08003392 ssl->handshake->new_session_tickets_count--;
Gilles Peskine449bd832023-01-11 14:50:10 +01003393 }
Jerry Yub7e3fa72022-09-22 11:07:18 +08003394
Jerry Yua8d3c502022-10-30 14:51:23 +08003395 mbedtls_ssl_handshake_set_state(
Gilles Peskine449bd832023-01-11 14:50:10 +01003396 ssl, MBEDTLS_SSL_TLS1_3_NEW_SESSION_TICKET_FLUSH);
3397 } else {
3398 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_HANDSHAKE_OVER);
Jerry Yue67bef42022-07-07 07:29:42 +00003399 }
3400
Jerry Yue67bef42022-07-07 07:29:42 +00003401cleanup:
3402
Gilles Peskine449bd832023-01-11 14:50:10 +01003403 return ret;
Jerry Yue67bef42022-07-07 07:29:42 +00003404}
3405#endif /* MBEDTLS_SSL_SESSION_TICKETS */
3406
3407/*
XiaokangQiane8ff3502022-04-22 02:34:40 +00003408 * TLS 1.3 State Machine -- server side
XiaokangQian7807f9f2022-02-15 10:04:37 +00003409 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003410int mbedtls_ssl_tls13_handshake_server_step(mbedtls_ssl_context *ssl)
Jerry Yub9930e72021-08-06 17:11:51 +08003411{
XiaokangQian08037552022-04-20 07:16:41 +00003412 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
XiaokangQian7807f9f2022-02-15 10:04:37 +00003413
Gilles Peskine449bd832023-01-11 14:50:10 +01003414 if (ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER || ssl->handshake == NULL) {
3415 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
3416 }
XiaokangQian7807f9f2022-02-15 10:04:37 +00003417
Gilles Peskine449bd832023-01-11 14:50:10 +01003418 MBEDTLS_SSL_DEBUG_MSG(2, ("tls13 server state: %s(%d)",
Dave Rodgman2eab4622023-10-05 13:30:37 +01003419 mbedtls_ssl_states_str((mbedtls_ssl_states) ssl->state),
Gilles Peskine449bd832023-01-11 14:50:10 +01003420 ssl->state));
Jerry Yu6e81b272021-09-27 11:16:17 +08003421
Gilles Peskine449bd832023-01-11 14:50:10 +01003422 switch (ssl->state) {
XiaokangQian7807f9f2022-02-15 10:04:37 +00003423 /* start state */
3424 case MBEDTLS_SSL_HELLO_REQUEST:
Gilles Peskine449bd832023-01-11 14:50:10 +01003425 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_CLIENT_HELLO);
XiaokangQian08037552022-04-20 07:16:41 +00003426 ret = 0;
XiaokangQian7807f9f2022-02-15 10:04:37 +00003427 break;
3428
XiaokangQian7807f9f2022-02-15 10:04:37 +00003429 case MBEDTLS_SSL_CLIENT_HELLO:
Gilles Peskine449bd832023-01-11 14:50:10 +01003430 ret = ssl_tls13_process_client_hello(ssl);
3431 if (ret != 0) {
3432 MBEDTLS_SSL_DEBUG_RET(1, "ssl_tls13_process_client_hello", ret);
3433 }
Jerry Yuf41553b2022-05-09 22:20:30 +08003434 break;
XiaokangQian7807f9f2022-02-15 10:04:37 +00003435
Jerry Yuf41553b2022-05-09 22:20:30 +08003436 case MBEDTLS_SSL_HELLO_RETRY_REQUEST:
Gilles Peskine449bd832023-01-11 14:50:10 +01003437 ret = ssl_tls13_write_hello_retry_request(ssl);
3438 if (ret != 0) {
3439 MBEDTLS_SSL_DEBUG_RET(1, "ssl_tls13_write_hello_retry_request", ret);
3440 return ret;
Jerry Yuf41553b2022-05-09 22:20:30 +08003441 }
XiaokangQian7807f9f2022-02-15 10:04:37 +00003442 break;
3443
Jerry Yu5b64ae92022-03-30 17:15:02 +08003444 case MBEDTLS_SSL_SERVER_HELLO:
Gilles Peskine449bd832023-01-11 14:50:10 +01003445 ret = ssl_tls13_write_server_hello(ssl);
Jerry Yu5b64ae92022-03-30 17:15:02 +08003446 break;
3447
Xiaofei Baicba64af2022-02-15 10:00:56 +00003448 case MBEDTLS_SSL_ENCRYPTED_EXTENSIONS:
Gilles Peskine449bd832023-01-11 14:50:10 +01003449 ret = ssl_tls13_write_encrypted_extensions(ssl);
3450 if (ret != 0) {
3451 MBEDTLS_SSL_DEBUG_RET(1, "ssl_tls13_write_encrypted_extensions", ret);
3452 return ret;
Xiaofei Baicba64af2022-02-15 10:00:56 +00003453 }
Jerry Yu48330562022-05-06 21:35:44 +08003454 break;
Jerry Yu6a2cd9e2022-05-05 11:14:19 +08003455
Ronald Cron928cbd32022-10-04 16:14:26 +02003456#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED)
Xiaofei Bai9ca09d42022-02-14 12:57:18 +00003457 case MBEDTLS_SSL_CERTIFICATE_REQUEST:
Gilles Peskine449bd832023-01-11 14:50:10 +01003458 ret = ssl_tls13_write_certificate_request(ssl);
Xiaofei Bai9ca09d42022-02-14 12:57:18 +00003459 break;
Xiaofei Bai9ca09d42022-02-14 12:57:18 +00003460
Jerry Yu83da34e2022-04-16 13:59:52 +08003461 case MBEDTLS_SSL_SERVER_CERTIFICATE:
Gilles Peskine449bd832023-01-11 14:50:10 +01003462 ret = ssl_tls13_write_server_certificate(ssl);
Jerry Yu83da34e2022-04-16 13:59:52 +08003463 break;
3464
3465 case MBEDTLS_SSL_CERTIFICATE_VERIFY:
Gilles Peskine449bd832023-01-11 14:50:10 +01003466 ret = ssl_tls13_write_certificate_verify(ssl);
Jerry Yu83da34e2022-04-16 13:59:52 +08003467 break;
Ronald Cron928cbd32022-10-04 16:14:26 +02003468#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED */
Jerry Yu83da34e2022-04-16 13:59:52 +08003469
Gilles Peskine449bd832023-01-11 14:50:10 +01003470 /*
3471 * Injection of dummy-CCS's for middlebox compatibility
3472 */
Gabor Mezei7b39bf12022-05-24 16:04:14 +02003473#if defined(MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE)
Gabor Mezeif7044ea2022-06-28 16:01:49 +02003474 case MBEDTLS_SSL_SERVER_CCS_AFTER_HELLO_RETRY_REQUEST:
Gilles Peskine449bd832023-01-11 14:50:10 +01003475 ret = mbedtls_ssl_tls13_write_change_cipher_spec(ssl);
3476 if (ret == 0) {
3477 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_CLIENT_HELLO);
3478 }
Gabor Mezei7b39bf12022-05-24 16:04:14 +02003479 break;
3480
3481 case MBEDTLS_SSL_SERVER_CCS_AFTER_SERVER_HELLO:
Ronald Crone273f722024-02-13 18:22:26 +01003482 ret = mbedtls_ssl_tls13_write_change_cipher_spec(ssl);
3483 if (ret != 0) {
3484 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01003485 }
Ronald Cronfe59ff72024-01-24 14:31:50 +01003486 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_ENCRYPTED_EXTENSIONS);
Gabor Mezei7b39bf12022-05-24 16:04:14 +02003487 break;
3488#endif /* MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE */
3489
Jerry Yu69dd8d42022-04-16 12:51:26 +08003490 case MBEDTLS_SSL_SERVER_FINISHED:
Gilles Peskine449bd832023-01-11 14:50:10 +01003491 ret = ssl_tls13_write_server_finished(ssl);
Jerry Yu69dd8d42022-04-16 12:51:26 +08003492 break;
3493
Jerry Yu87b5ed42022-12-12 13:07:07 +08003494#if defined(MBEDTLS_SSL_EARLY_DATA)
3495 case MBEDTLS_SSL_END_OF_EARLY_DATA:
Jerry Yu59d420f2023-12-01 16:30:34 +08003496 ret = ssl_tls13_process_end_of_early_data(ssl);
Jerry Yu87b5ed42022-12-12 13:07:07 +08003497 break;
3498#endif /* MBEDTLS_SSL_EARLY_DATA */
3499
Jerry Yu69dd8d42022-04-16 12:51:26 +08003500 case MBEDTLS_SSL_CLIENT_FINISHED:
Gilles Peskine449bd832023-01-11 14:50:10 +01003501 ret = ssl_tls13_process_client_finished(ssl);
Jerry Yu69dd8d42022-04-16 12:51:26 +08003502 break;
3503
Jerry Yu69dd8d42022-04-16 12:51:26 +08003504 case MBEDTLS_SSL_HANDSHAKE_WRAPUP:
Gilles Peskine449bd832023-01-11 14:50:10 +01003505 ret = ssl_tls13_handshake_wrapup(ssl);
Jerry Yu69dd8d42022-04-16 12:51:26 +08003506 break;
3507
Ronald Cron766c0cd2022-10-18 12:17:11 +02003508#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED)
XiaokangQian6b916b12022-04-25 07:29:34 +00003509 case MBEDTLS_SSL_CLIENT_CERTIFICATE:
Gilles Peskine449bd832023-01-11 14:50:10 +01003510 ret = mbedtls_ssl_tls13_process_certificate(ssl);
3511 if (ret == 0) {
3512 if (ssl->session_negotiate->peer_cert != NULL) {
Ronald Cron209cae92022-06-07 10:30:19 +02003513 mbedtls_ssl_handshake_set_state(
Gilles Peskine449bd832023-01-11 14:50:10 +01003514 ssl, MBEDTLS_SSL_CLIENT_CERTIFICATE_VERIFY);
3515 } else {
3516 MBEDTLS_SSL_DEBUG_MSG(2, ("skip parse certificate verify"));
Ronald Cron209cae92022-06-07 10:30:19 +02003517 mbedtls_ssl_handshake_set_state(
Gilles Peskine449bd832023-01-11 14:50:10 +01003518 ssl, MBEDTLS_SSL_CLIENT_FINISHED);
Ronald Cron19385882022-06-15 16:26:13 +02003519 }
XiaokangQian6b916b12022-04-25 07:29:34 +00003520 }
3521 break;
3522
3523 case MBEDTLS_SSL_CLIENT_CERTIFICATE_VERIFY:
Gilles Peskine449bd832023-01-11 14:50:10 +01003524 ret = mbedtls_ssl_tls13_process_certificate_verify(ssl);
3525 if (ret == 0) {
XiaokangQian6b916b12022-04-25 07:29:34 +00003526 mbedtls_ssl_handshake_set_state(
Gilles Peskine449bd832023-01-11 14:50:10 +01003527 ssl, MBEDTLS_SSL_CLIENT_FINISHED);
XiaokangQian6b916b12022-04-25 07:29:34 +00003528 }
3529 break;
Ronald Cron766c0cd2022-10-18 12:17:11 +02003530#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED */
XiaokangQian6b916b12022-04-25 07:29:34 +00003531
Jerry Yue67bef42022-07-07 07:29:42 +00003532#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Jerry Yua8d3c502022-10-30 14:51:23 +08003533 case MBEDTLS_SSL_TLS1_3_NEW_SESSION_TICKET:
Gilles Peskine449bd832023-01-11 14:50:10 +01003534 ret = ssl_tls13_write_new_session_ticket(ssl);
3535 if (ret != 0) {
3536 MBEDTLS_SSL_DEBUG_RET(1,
3537 "ssl_tls13_write_new_session_ticket ",
3538 ret);
Jerry Yue67bef42022-07-07 07:29:42 +00003539 }
3540 break;
Jerry Yua8d3c502022-10-30 14:51:23 +08003541 case MBEDTLS_SSL_TLS1_3_NEW_SESSION_TICKET_FLUSH:
Jerry Yue67bef42022-07-07 07:29:42 +00003542 /* This state is necessary to do the flush of the New Session
Jerry Yua8d3c502022-10-30 14:51:23 +08003543 * Ticket message written in MBEDTLS_SSL_TLS1_3_NEW_SESSION_TICKET
Jerry Yue67bef42022-07-07 07:29:42 +00003544 * as part of ssl_prepare_handshake_step.
3545 */
3546 ret = 0;
Jerry Yud4e75002022-08-09 13:33:50 +08003547
Gilles Peskine449bd832023-01-11 14:50:10 +01003548 if (ssl->handshake->new_session_tickets_count == 0) {
3549 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_HANDSHAKE_OVER);
3550 } else {
Xiaokang Qian9f1747b2023-03-30 02:50:04 +00003551 mbedtls_ssl_handshake_set_state(
3552 ssl, MBEDTLS_SSL_TLS1_3_NEW_SESSION_TICKET);
Gilles Peskine449bd832023-01-11 14:50:10 +01003553 }
Jerry Yue67bef42022-07-07 07:29:42 +00003554 break;
3555
3556#endif /* MBEDTLS_SSL_SESSION_TICKETS */
3557
XiaokangQian7807f9f2022-02-15 10:04:37 +00003558 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01003559 MBEDTLS_SSL_DEBUG_MSG(1, ("invalid state %d", ssl->state));
3560 return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
XiaokangQian7807f9f2022-02-15 10:04:37 +00003561 }
3562
Gilles Peskine449bd832023-01-11 14:50:10 +01003563 return ret;
Jerry Yub9930e72021-08-06 17:11:51 +08003564}
Jerry Yu3cc4c2a2021-08-06 16:29:08 +08003565
Jerry Yufb4b6472022-01-27 15:03:26 +08003566#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_PROTO_TLS1_3 */