blob: 13ae61df722c1a8415cb6ac038fb54853aff8721 [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
Jerry Yu6e74a7e2022-07-20 20:49:32 +0800327MBEDTLS_CHECK_RETURN_CRITICAL
Xiaokang Qian9f1747b2023-03-30 02:50:04 +0000328static int ssl_tls13_offered_psks_check_binder_match(
329 mbedtls_ssl_context *ssl,
330 const unsigned char *binder, size_t binder_len,
331 int psk_type, psa_algorithm_t psk_hash_alg)
Jerry Yu1c105562022-07-10 06:32:38 +0000332{
333 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yu96a2e362022-07-21 15:11:34 +0800334
Jerry Yudaf375a2022-07-20 21:31:43 +0800335 unsigned char transcript[PSA_HASH_MAX_SIZE];
Jerry Yu1c105562022-07-10 06:32:38 +0000336 size_t transcript_len;
Jerry Yu2f0abc92022-07-22 19:34:48 +0800337 unsigned char *psk;
Jerry Yu96a2e362022-07-21 15:11:34 +0800338 size_t psk_len;
Jerry Yudaf375a2022-07-20 21:31:43 +0800339 unsigned char server_computed_binder[PSA_HASH_MAX_SIZE];
Jerry Yu1c105562022-07-10 06:32:38 +0000340
Jerry Yu1c105562022-07-10 06:32:38 +0000341 /* Get current state of handshake transcript. */
Jerry Yu29d9faa2022-08-23 17:52:45 +0800342 ret = mbedtls_ssl_get_handshake_transcript(
Manuel Pégourié-Gonnard2d6d9932023-03-28 11:38:08 +0200343 ssl, mbedtls_md_type_from_psa_alg(psk_hash_alg),
Gilles Peskine449bd832023-01-11 14:50:10 +0100344 transcript, sizeof(transcript), &transcript_len);
345 if (ret != 0) {
346 return ret;
347 }
Jerry Yu1c105562022-07-10 06:32:38 +0000348
Gilles Peskine449bd832023-01-11 14:50:10 +0100349 ret = mbedtls_ssl_tls13_export_handshake_psk(ssl, &psk, &psk_len);
350 if (ret != 0) {
351 return ret;
352 }
Jerry Yu96a2e362022-07-21 15:11:34 +0800353
Gilles Peskine449bd832023-01-11 14:50:10 +0100354 ret = mbedtls_ssl_tls13_create_psk_binder(ssl, psk_hash_alg,
355 psk, psk_len, psk_type,
356 transcript,
357 server_computed_binder);
Jerry Yu96a2e362022-07-21 15:11:34 +0800358#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +0100359 mbedtls_free((void *) psk);
Jerry Yu96a2e362022-07-21 15:11:34 +0800360#endif
Gilles Peskine449bd832023-01-11 14:50:10 +0100361 if (ret != 0) {
362 MBEDTLS_SSL_DEBUG_MSG(1, ("PSK binder calculation failed."));
363 return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
Jerry Yu1c105562022-07-10 06:32:38 +0000364 }
365
Gilles Peskine449bd832023-01-11 14:50:10 +0100366 MBEDTLS_SSL_DEBUG_BUF(3, "psk binder ( computed ): ",
367 server_computed_binder, transcript_len);
368 MBEDTLS_SSL_DEBUG_BUF(3, "psk binder ( received ): ", binder, binder_len);
Jerry Yu1c105562022-07-10 06:32:38 +0000369
Gilles Peskine449bd832023-01-11 14:50:10 +0100370 if (mbedtls_ct_memcmp(server_computed_binder, binder, binder_len) == 0) {
371 return SSL_TLS1_3_OFFERED_PSK_MATCH;
Jerry Yu1c105562022-07-10 06:32:38 +0000372 }
373
Gilles Peskine449bd832023-01-11 14:50:10 +0100374 mbedtls_platform_zeroize(server_computed_binder,
375 sizeof(server_computed_binder));
376 return SSL_TLS1_3_OFFERED_PSK_NOT_MATCH;
Jerry Yu1c105562022-07-10 06:32:38 +0000377}
Jerry Yu96a2e362022-07-21 15:11:34 +0800378
Jerry Yu5725f1c2022-08-21 17:27:16 +0800379MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yuf35ba382022-08-23 17:58:26 +0800380static int ssl_tls13_select_ciphersuite_for_psk(
Gilles Peskine449bd832023-01-11 14:50:10 +0100381 mbedtls_ssl_context *ssl,
382 const unsigned char *cipher_suites,
383 const unsigned char *cipher_suites_end,
384 uint16_t *selected_ciphersuite,
385 const mbedtls_ssl_ciphersuite_t **selected_ciphersuite_info)
Jerry Yu5725f1c2022-08-21 17:27:16 +0800386{
Jerry Yuf35ba382022-08-23 17:58:26 +0800387 psa_algorithm_t psk_hash_alg = PSA_ALG_SHA_256;
Jerry Yu5725f1c2022-08-21 17:27:16 +0800388
Jerry Yu0baf9072022-08-25 11:21:04 +0800389 *selected_ciphersuite = 0;
390 *selected_ciphersuite_info = NULL;
391
Jerry Yuf35ba382022-08-23 17:58:26 +0800392 /* RFC 8446, page 55.
393 *
394 * For externally established PSKs, the Hash algorithm MUST be set when the
395 * PSK is established or default to SHA-256 if no such algorithm is defined.
396 *
397 */
Jerry Yu5725f1c2022-08-21 17:27:16 +0800398
Jerry Yu5725f1c2022-08-21 17:27:16 +0800399 /*
400 * Search for a matching ciphersuite
401 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100402 for (const unsigned char *p = cipher_suites;
403 p < cipher_suites_end; p += 2) {
Jerry Yu5725f1c2022-08-21 17:27:16 +0800404 uint16_t cipher_suite;
Jerry Yuf35ba382022-08-23 17:58:26 +0800405 const mbedtls_ssl_ciphersuite_t *ciphersuite_info;
Jerry Yu5725f1c2022-08-21 17:27:16 +0800406
Gilles Peskine449bd832023-01-11 14:50:10 +0100407 cipher_suite = MBEDTLS_GET_UINT16_BE(p, 0);
408 ciphersuite_info = ssl_tls13_validate_peer_ciphersuite(ssl,
409 cipher_suite);
410 if (ciphersuite_info == NULL) {
Jerry Yu5725f1c2022-08-21 17:27:16 +0800411 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +0100412 }
Jerry Yu5725f1c2022-08-21 17:27:16 +0800413
Jerry Yu5725f1c2022-08-21 17:27:16 +0800414 /* MAC of selected ciphersuite MUST be same with PSK binder if exist.
415 * Otherwise, client should reject.
416 */
Dave Rodgman2eab4622023-10-05 13:30:37 +0100417 if (psk_hash_alg ==
418 mbedtls_md_psa_alg_from_type((mbedtls_md_type_t) ciphersuite_info->mac)) {
Jerry Yuf35ba382022-08-23 17:58:26 +0800419 *selected_ciphersuite = cipher_suite;
420 *selected_ciphersuite_info = ciphersuite_info;
Gilles Peskine449bd832023-01-11 14:50:10 +0100421 return 0;
Jerry Yuf35ba382022-08-23 17:58:26 +0800422 }
423 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100424 MBEDTLS_SSL_DEBUG_MSG(2, ("No matched ciphersuite"));
425 return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
Jerry Yuf35ba382022-08-23 17:58:26 +0800426}
Jerry Yu5725f1c2022-08-21 17:27:16 +0800427
Jerry Yu82534862022-08-30 10:42:33 +0800428#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Jerry Yuf35ba382022-08-23 17:58:26 +0800429MBEDTLS_CHECK_RETURN_CRITICAL
430static int ssl_tls13_select_ciphersuite_for_resumption(
Gilles Peskine449bd832023-01-11 14:50:10 +0100431 mbedtls_ssl_context *ssl,
432 const unsigned char *cipher_suites,
433 const unsigned char *cipher_suites_end,
434 mbedtls_ssl_session *session,
435 uint16_t *selected_ciphersuite,
436 const mbedtls_ssl_ciphersuite_t **selected_ciphersuite_info)
Jerry Yuf35ba382022-08-23 17:58:26 +0800437{
Jerry Yu82534862022-08-30 10:42:33 +0800438
Jerry Yuf35ba382022-08-23 17:58:26 +0800439 *selected_ciphersuite = 0;
440 *selected_ciphersuite_info = NULL;
Gilles Peskine449bd832023-01-11 14:50:10 +0100441 for (const unsigned char *p = cipher_suites; p < cipher_suites_end; p += 2) {
442 uint16_t cipher_suite = MBEDTLS_GET_UINT16_BE(p, 0);
Jerry Yu82534862022-08-30 10:42:33 +0800443 const mbedtls_ssl_ciphersuite_t *ciphersuite_info;
444
Gilles Peskine449bd832023-01-11 14:50:10 +0100445 if (cipher_suite != session->ciphersuite) {
Jerry Yu82534862022-08-30 10:42:33 +0800446 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +0100447 }
Jerry Yu82534862022-08-30 10:42:33 +0800448
Gilles Peskine449bd832023-01-11 14:50:10 +0100449 ciphersuite_info = ssl_tls13_validate_peer_ciphersuite(ssl,
450 cipher_suite);
451 if (ciphersuite_info == NULL) {
Jerry Yu82534862022-08-30 10:42:33 +0800452 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +0100453 }
Jerry Yu82534862022-08-30 10:42:33 +0800454
Jerry Yu4746b102022-09-13 11:11:48 +0800455 *selected_ciphersuite = cipher_suite;
Jerry Yu82534862022-08-30 10:42:33 +0800456 *selected_ciphersuite_info = ciphersuite_info;
457
Gilles Peskine449bd832023-01-11 14:50:10 +0100458 return 0;
Jerry Yu82534862022-08-30 10:42:33 +0800459 }
460
Gilles Peskine449bd832023-01-11 14:50:10 +0100461 return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
Jerry Yu5725f1c2022-08-21 17:27:16 +0800462}
Jerry Yuf35ba382022-08-23 17:58:26 +0800463
Jerry Yu82534862022-08-30 10:42:33 +0800464MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100465static int ssl_tls13_session_copy_ticket(mbedtls_ssl_session *dst,
466 const mbedtls_ssl_session *src)
Jerry Yu82534862022-08-30 10:42:33 +0800467{
Jerry Yu82534862022-08-30 10:42:33 +0800468 dst->ticket_age_add = src->ticket_age_add;
469 dst->ticket_flags = src->ticket_flags;
470 dst->resumption_key_len = src->resumption_key_len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100471 if (src->resumption_key_len == 0) {
472 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
473 }
474 memcpy(dst->resumption_key, src->resumption_key, src->resumption_key_len);
Jerry Yu4746b102022-09-13 11:11:48 +0800475
Jerry Yu33bf2402022-12-12 16:01:43 +0800476#if defined(MBEDTLS_SSL_EARLY_DATA)
477 dst->max_early_data_size = src->max_early_data_size;
478#endif
479
Gilles Peskine449bd832023-01-11 14:50:10 +0100480 return 0;
Jerry Yu82534862022-08-30 10:42:33 +0800481}
482#endif /* MBEDTLS_SSL_SESSION_TICKETS */
483
Jerry Yu1c105562022-07-10 06:32:38 +0000484/* Parser for pre_shared_key extension in client hello
485 * struct {
486 * opaque identity<1..2^16-1>;
487 * uint32 obfuscated_ticket_age;
488 * } PskIdentity;
489 *
490 * opaque PskBinderEntry<32..255>;
491 *
492 * struct {
493 * PskIdentity identities<7..2^16-1>;
494 * PskBinderEntry binders<33..2^16-1>;
495 * } OfferedPsks;
496 *
497 * struct {
498 * select (Handshake.msg_type) {
499 * case client_hello: OfferedPsks;
500 * ....
501 * };
502 * } PreSharedKeyExtension;
503 */
Jerry Yu6e74a7e2022-07-20 20:49:32 +0800504MBEDTLS_CHECK_RETURN_CRITICAL
Xiaokang Qian9f1747b2023-03-30 02:50:04 +0000505static int ssl_tls13_parse_pre_shared_key_ext(
506 mbedtls_ssl_context *ssl,
507 const unsigned char *pre_shared_key_ext,
508 const unsigned char *pre_shared_key_ext_end,
509 const unsigned char *ciphersuites,
510 const unsigned char *ciphersuites_end)
Jerry Yu1c105562022-07-10 06:32:38 +0000511{
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +0100512 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yu29d9faa2022-08-23 17:52:45 +0800513 const unsigned char *identities = pre_shared_key_ext;
Jerry Yu568ec252022-07-22 21:27:34 +0800514 const unsigned char *p_identity_len;
515 size_t identities_len;
Jerry Yu1c105562022-07-10 06:32:38 +0000516 const unsigned char *identities_end;
Jerry Yu568ec252022-07-22 21:27:34 +0800517 const unsigned char *binders;
518 const unsigned char *p_binder_len;
519 size_t binders_len;
Jerry Yu1c105562022-07-10 06:32:38 +0000520 const unsigned char *binders_end;
Jerry Yu96a2e362022-07-21 15:11:34 +0800521 int matched_identity = -1;
522 int identity_id = -1;
Jerry Yu1c105562022-07-10 06:32:38 +0000523
Gilles Peskine449bd832023-01-11 14:50:10 +0100524 MBEDTLS_SSL_DEBUG_BUF(3, "pre_shared_key extension",
525 pre_shared_key_ext,
526 pre_shared_key_ext_end - pre_shared_key_ext);
Jerry Yu1c105562022-07-10 06:32:38 +0000527
Jerry Yu96a2e362022-07-21 15:11:34 +0800528 /* identities_len 2 bytes
529 * identities_data >= 7 bytes
530 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100531 MBEDTLS_SSL_CHK_BUF_READ_PTR(identities, pre_shared_key_ext_end, 7 + 2);
532 identities_len = MBEDTLS_GET_UINT16_BE(identities, 0);
Jerry Yu568ec252022-07-22 21:27:34 +0800533 p_identity_len = identities + 2;
Gilles Peskine449bd832023-01-11 14:50:10 +0100534 MBEDTLS_SSL_CHK_BUF_READ_PTR(p_identity_len, pre_shared_key_ext_end,
535 identities_len);
Jerry Yu568ec252022-07-22 21:27:34 +0800536 identities_end = p_identity_len + identities_len;
Jerry Yu1c105562022-07-10 06:32:38 +0000537
Jerry Yu96a2e362022-07-21 15:11:34 +0800538 /* binders_len 2 bytes
539 * binders >= 33 bytes
540 */
Jerry Yu568ec252022-07-22 21:27:34 +0800541 binders = identities_end;
Gilles Peskine449bd832023-01-11 14:50:10 +0100542 MBEDTLS_SSL_CHK_BUF_READ_PTR(binders, pre_shared_key_ext_end, 33 + 2);
543 binders_len = MBEDTLS_GET_UINT16_BE(binders, 0);
Jerry Yu568ec252022-07-22 21:27:34 +0800544 p_binder_len = binders + 2;
Gilles Peskine449bd832023-01-11 14:50:10 +0100545 MBEDTLS_SSL_CHK_BUF_READ_PTR(p_binder_len, pre_shared_key_ext_end, binders_len);
Jerry Yu568ec252022-07-22 21:27:34 +0800546 binders_end = p_binder_len + binders_len;
Jerry Yu1c105562022-07-10 06:32:38 +0000547
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +0100548 ret = ssl->handshake->update_checksum(ssl, pre_shared_key_ext,
549 identities_end - pre_shared_key_ext);
550 if (0 != ret) {
551 MBEDTLS_SSL_DEBUG_RET(1, ("update_checksum"), ret);
552 return ret;
553 }
Jerry Yu96a2e362022-07-21 15:11:34 +0800554
Gilles Peskine449bd832023-01-11 14:50:10 +0100555 while (p_identity_len < identities_end && p_binder_len < binders_end) {
Jerry Yu1c105562022-07-10 06:32:38 +0000556 const unsigned char *identity;
Jerry Yu568ec252022-07-22 21:27:34 +0800557 size_t identity_len;
Jerry Yu82534862022-08-30 10:42:33 +0800558 uint32_t obfuscated_ticket_age;
Jerry Yu96a2e362022-07-21 15:11:34 +0800559 const unsigned char *binder;
Jerry Yu568ec252022-07-22 21:27:34 +0800560 size_t binder_len;
Jerry Yu5725f1c2022-08-21 17:27:16 +0800561 int psk_type;
562 uint16_t cipher_suite;
Jerry Yu29d9faa2022-08-23 17:52:45 +0800563 const mbedtls_ssl_ciphersuite_t *ciphersuite_info;
Jerry Yu82534862022-08-30 10:42:33 +0800564#if defined(MBEDTLS_SSL_SESSION_TICKETS)
565 mbedtls_ssl_session session;
Gilles Peskine449bd832023-01-11 14:50:10 +0100566 mbedtls_ssl_session_init(&session);
Jerry Yu82534862022-08-30 10:42:33 +0800567#endif
Jerry Yubb852022022-07-20 21:10:44 +0800568
Gilles Peskine449bd832023-01-11 14:50:10 +0100569 MBEDTLS_SSL_CHK_BUF_READ_PTR(p_identity_len, identities_end, 2 + 1 + 4);
570 identity_len = MBEDTLS_GET_UINT16_BE(p_identity_len, 0);
Jerry Yu568ec252022-07-22 21:27:34 +0800571 identity = p_identity_len + 2;
Gilles Peskine449bd832023-01-11 14:50:10 +0100572 MBEDTLS_SSL_CHK_BUF_READ_PTR(identity, identities_end, identity_len + 4);
573 obfuscated_ticket_age = MBEDTLS_GET_UINT32_BE(identity, identity_len);
Jerry Yu568ec252022-07-22 21:27:34 +0800574 p_identity_len += identity_len + 6;
Jerry Yu1c105562022-07-10 06:32:38 +0000575
Gilles Peskine449bd832023-01-11 14:50:10 +0100576 MBEDTLS_SSL_CHK_BUF_READ_PTR(p_binder_len, binders_end, 1 + 32);
Jerry Yu568ec252022-07-22 21:27:34 +0800577 binder_len = *p_binder_len;
578 binder = p_binder_len + 1;
Gilles Peskine449bd832023-01-11 14:50:10 +0100579 MBEDTLS_SSL_CHK_BUF_READ_PTR(binder, binders_end, binder_len);
Jerry Yu568ec252022-07-22 21:27:34 +0800580 p_binder_len += binder_len + 1;
Jerry Yu96a2e362022-07-21 15:11:34 +0800581
Jerry Yu96a2e362022-07-21 15:11:34 +0800582 identity_id++;
Gilles Peskine449bd832023-01-11 14:50:10 +0100583 if (matched_identity != -1) {
Jerry Yu1c105562022-07-10 06:32:38 +0000584 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +0100585 }
Jerry Yu1c105562022-07-10 06:32:38 +0000586
Jerry Yu96a2e362022-07-21 15:11:34 +0800587 ret = ssl_tls13_offered_psks_check_identity_match(
Gilles Peskine449bd832023-01-11 14:50:10 +0100588 ssl, identity, identity_len, obfuscated_ticket_age,
589 &psk_type, &session);
590 if (ret != SSL_TLS1_3_OFFERED_PSK_MATCH) {
Jerry Yu96a2e362022-07-21 15:11:34 +0800591 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +0100592 }
Jerry Yu96a2e362022-07-21 15:11:34 +0800593
Gilles Peskine449bd832023-01-11 14:50:10 +0100594 MBEDTLS_SSL_DEBUG_MSG(4, ("found matched identity"));
595 switch (psk_type) {
Jerry Yu0baf9072022-08-25 11:21:04 +0800596 case MBEDTLS_SSL_TLS1_3_PSK_EXTERNAL:
597 ret = ssl_tls13_select_ciphersuite_for_psk(
Gilles Peskine449bd832023-01-11 14:50:10 +0100598 ssl, ciphersuites, ciphersuites_end,
599 &cipher_suite, &ciphersuite_info);
Jerry Yu0baf9072022-08-25 11:21:04 +0800600 break;
601 case MBEDTLS_SSL_TLS1_3_PSK_RESUMPTION:
Jerry Yu82534862022-08-30 10:42:33 +0800602#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Jerry Yu0baf9072022-08-25 11:21:04 +0800603 ret = ssl_tls13_select_ciphersuite_for_resumption(
Gilles Peskine449bd832023-01-11 14:50:10 +0100604 ssl, ciphersuites, ciphersuites_end, &session,
605 &cipher_suite, &ciphersuite_info);
606 if (ret != 0) {
607 mbedtls_ssl_session_free(&session);
608 }
Jerry Yu82534862022-08-30 10:42:33 +0800609#else
610 ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
611#endif
Jerry Yu0baf9072022-08-25 11:21:04 +0800612 break;
613 default:
Gilles Peskine449bd832023-01-11 14:50:10 +0100614 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Jerry Yu0baf9072022-08-25 11:21:04 +0800615 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100616 if (ret != 0) {
Jerry Yuf35ba382022-08-23 17:58:26 +0800617 /* See below, no cipher_suite available, abort handshake */
618 MBEDTLS_SSL_PEND_FATAL_ALERT(
619 MBEDTLS_SSL_ALERT_MSG_DECRYPT_ERROR,
Gilles Peskine449bd832023-01-11 14:50:10 +0100620 MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE);
Jerry Yuf35ba382022-08-23 17:58:26 +0800621 MBEDTLS_SSL_DEBUG_RET(
Gilles Peskine449bd832023-01-11 14:50:10 +0100622 2, "ssl_tls13_select_ciphersuite", ret);
623 return ret;
Jerry Yu5725f1c2022-08-21 17:27:16 +0800624 }
625
Jerry Yu96a2e362022-07-21 15:11:34 +0800626 ret = ssl_tls13_offered_psks_check_binder_match(
Gilles Peskine449bd832023-01-11 14:50:10 +0100627 ssl, binder, binder_len, psk_type,
Dave Rodgman2eab4622023-10-05 13:30:37 +0100628 mbedtls_md_psa_alg_from_type((mbedtls_md_type_t) ciphersuite_info->mac));
Gilles Peskine449bd832023-01-11 14:50:10 +0100629 if (ret != SSL_TLS1_3_OFFERED_PSK_MATCH) {
Jerry Yuc5a23a02022-08-25 10:51:44 +0800630 /* For security reasons, the handshake should be aborted when we
631 * fail to validate a binder value. See RFC 8446 section 4.2.11.2
632 * and appendix E.6. */
Jerry Yu82534862022-08-30 10:42:33 +0800633#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Gilles Peskine449bd832023-01-11 14:50:10 +0100634 mbedtls_ssl_session_free(&session);
Jerry Yu82534862022-08-30 10:42:33 +0800635#endif
Gilles Peskine449bd832023-01-11 14:50:10 +0100636 MBEDTLS_SSL_DEBUG_MSG(3, ("Invalid binder."));
Xiaokang Qian9f1747b2023-03-30 02:50:04 +0000637 MBEDTLS_SSL_DEBUG_RET(
638 1, "ssl_tls13_offered_psks_check_binder_match", ret);
Jerry Yu96a2e362022-07-21 15:11:34 +0800639 MBEDTLS_SSL_PEND_FATAL_ALERT(
640 MBEDTLS_SSL_ALERT_MSG_DECRYPT_ERROR,
Gilles Peskine449bd832023-01-11 14:50:10 +0100641 MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE);
642 return ret;
Jerry Yu96a2e362022-07-21 15:11:34 +0800643 }
Jerry Yu96a2e362022-07-21 15:11:34 +0800644
645 matched_identity = identity_id;
Jerry Yu5725f1c2022-08-21 17:27:16 +0800646
647 /* Update handshake parameters */
Jerry Yue5834fd2022-08-29 20:16:09 +0800648 ssl->handshake->ciphersuite_info = ciphersuite_info;
Jerry Yu4746b102022-09-13 11:11:48 +0800649 ssl->session_negotiate->ciphersuite = cipher_suite;
Gilles Peskine449bd832023-01-11 14:50:10 +0100650 MBEDTLS_SSL_DEBUG_MSG(2, ("overwrite ciphersuite: %04x - %s",
651 cipher_suite, ciphersuite_info->name));
Jerry Yu82534862022-08-30 10:42:33 +0800652#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Gilles Peskine449bd832023-01-11 14:50:10 +0100653 if (psk_type == MBEDTLS_SSL_TLS1_3_PSK_RESUMPTION) {
654 ret = ssl_tls13_session_copy_ticket(ssl->session_negotiate,
655 &session);
656 mbedtls_ssl_session_free(&session);
657 if (ret != 0) {
658 return ret;
659 }
Jerry Yu82534862022-08-30 10:42:33 +0800660 }
661#endif /* MBEDTLS_SSL_SESSION_TICKETS */
Jerry Yu1c105562022-07-10 06:32:38 +0000662 }
663
Gilles Peskine449bd832023-01-11 14:50:10 +0100664 if (p_identity_len != identities_end || p_binder_len != binders_end) {
665 MBEDTLS_SSL_DEBUG_MSG(3, ("pre_shared_key extension decode error"));
666 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR,
667 MBEDTLS_ERR_SSL_DECODE_ERROR);
668 return MBEDTLS_ERR_SSL_DECODE_ERROR;
Jerry Yu1c105562022-07-10 06:32:38 +0000669 }
670
Jerry Yu6f1db3f2022-07-22 23:05:59 +0800671 /* Update the handshake transcript with the binder list. */
Xiaokang Qian9f1747b2023-03-30 02:50:04 +0000672 ret = ssl->handshake->update_checksum(
673 ssl, identities_end, (size_t) (binders_end - identities_end));
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +0100674 if (0 != ret) {
675 MBEDTLS_SSL_DEBUG_RET(1, ("update_checksum"), ret);
676 return ret;
677 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100678 if (matched_identity == -1) {
679 MBEDTLS_SSL_DEBUG_MSG(3, ("No matched PSK or ticket."));
680 return MBEDTLS_ERR_SSL_UNKNOWN_IDENTITY;
Jerry Yu1c105562022-07-10 06:32:38 +0000681 }
682
Gilles Peskine449bd832023-01-11 14:50:10 +0100683 ssl->handshake->selected_identity = (uint16_t) matched_identity;
684 MBEDTLS_SSL_DEBUG_MSG(3, ("Pre shared key found"));
Jerry Yu1c105562022-07-10 06:32:38 +0000685
Gilles Peskine449bd832023-01-11 14:50:10 +0100686 return 0;
Jerry Yu1c105562022-07-10 06:32:38 +0000687}
Jerry Yu032b15ce2022-07-11 06:10:03 +0000688
689/*
690 * struct {
691 * select ( Handshake.msg_type ) {
692 * ....
693 * case server_hello:
694 * uint16 selected_identity;
695 * }
696 * } PreSharedKeyExtension;
697 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100698static int ssl_tls13_write_server_pre_shared_key_ext(mbedtls_ssl_context *ssl,
699 unsigned char *buf,
700 unsigned char *end,
701 size_t *olen)
Jerry Yu032b15ce2022-07-11 06:10:03 +0000702{
Gilles Peskine449bd832023-01-11 14:50:10 +0100703 unsigned char *p = (unsigned char *) buf;
Jerry Yu032b15ce2022-07-11 06:10:03 +0000704
705 *olen = 0;
706
David Horstmann21b89762022-10-06 18:34:28 +0100707 int not_using_psk = 0;
Jerry Yu032b15ce2022-07-11 06:10:03 +0000708#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +0100709 not_using_psk = (mbedtls_svc_key_id_is_null(ssl->handshake->psk_opaque));
Jerry Yu032b15ce2022-07-11 06:10:03 +0000710#else
Gilles Peskine449bd832023-01-11 14:50:10 +0100711 not_using_psk = (ssl->handshake->psk == NULL);
Jerry Yu032b15ce2022-07-11 06:10:03 +0000712#endif
Gilles Peskine449bd832023-01-11 14:50:10 +0100713 if (not_using_psk) {
Jerry Yu032b15ce2022-07-11 06:10:03 +0000714 /* We shouldn't have called this extension writer unless we've
715 * chosen to use a PSK. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100716 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Jerry Yu032b15ce2022-07-11 06:10:03 +0000717 }
718
Gilles Peskine449bd832023-01-11 14:50:10 +0100719 MBEDTLS_SSL_DEBUG_MSG(3, ("server hello, adding pre_shared_key extension"));
720 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 6);
Jerry Yu032b15ce2022-07-11 06:10:03 +0000721
Gilles Peskine449bd832023-01-11 14:50:10 +0100722 MBEDTLS_PUT_UINT16_BE(MBEDTLS_TLS_EXT_PRE_SHARED_KEY, p, 0);
723 MBEDTLS_PUT_UINT16_BE(2, p, 2);
Jerry Yu032b15ce2022-07-11 06:10:03 +0000724
Gilles Peskine449bd832023-01-11 14:50:10 +0100725 MBEDTLS_PUT_UINT16_BE(ssl->handshake->selected_identity, p, 4);
Jerry Yu032b15ce2022-07-11 06:10:03 +0000726
727 *olen = 6;
728
Gilles Peskine449bd832023-01-11 14:50:10 +0100729 MBEDTLS_SSL_DEBUG_MSG(4, ("sent selected_identity: %u",
730 ssl->handshake->selected_identity));
Jerry Yu032b15ce2022-07-11 06:10:03 +0000731
Gilles Peskine449bd832023-01-11 14:50:10 +0100732 mbedtls_ssl_tls13_set_hs_sent_ext_mask(ssl, MBEDTLS_TLS_EXT_PRE_SHARED_KEY);
Jerry Yub95dd362022-11-08 21:19:34 +0800733
Gilles Peskine449bd832023-01-11 14:50:10 +0100734 return 0;
Jerry Yu032b15ce2022-07-11 06:10:03 +0000735}
736
Ronald Cron41a443a2022-10-04 16:38:25 +0200737#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED */
Jerry Yue19e3b92022-07-08 12:04:51 +0000738
XiaokangQian7807f9f2022-02-15 10:04:37 +0000739/* From RFC 8446:
740 * struct {
XiaokangQiancfd925f2022-04-14 07:10:37 +0000741 * ProtocolVersion versions<2..254>;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000742 * } SupportedVersions;
743 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200744MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100745static int ssl_tls13_parse_supported_versions_ext(mbedtls_ssl_context *ssl,
746 const unsigned char *buf,
747 const unsigned char *end)
XiaokangQian7807f9f2022-02-15 10:04:37 +0000748{
XiaokangQian7807f9f2022-02-15 10:04:37 +0000749 const unsigned char *p = buf;
XiaokangQian8f9dfe42022-04-15 02:52:39 +0000750 size_t versions_len;
XiaokangQian4080a7f2022-04-11 09:55:18 +0000751 const unsigned char *versions_end;
XiaokangQian0a1b54e2022-04-21 03:01:38 +0000752 uint16_t tls_version;
Ronald Cron5af4c7f2023-03-07 20:46:59 +0100753 int found_supported_version = 0;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000754
Gilles Peskine449bd832023-01-11 14:50:10 +0100755 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, 1);
XiaokangQian4080a7f2022-04-11 09:55:18 +0000756 versions_len = p[0];
XiaokangQian7807f9f2022-02-15 10:04:37 +0000757 p += 1;
758
Gilles Peskine449bd832023-01-11 14:50:10 +0100759 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, versions_len);
XiaokangQian4080a7f2022-04-11 09:55:18 +0000760 versions_end = p + versions_len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100761 while (p < versions_end) {
762 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, versions_end, 2);
763 tls_version = mbedtls_ssl_read_version(p, ssl->conf->transport);
XiaokangQianb67384d2022-04-19 00:02:38 +0000764 p += 2;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000765
Ronald Cron3bd2b022023-04-03 16:45:39 +0200766 if (MBEDTLS_SSL_VERSION_TLS1_3 == tls_version) {
Ronald Cron5af4c7f2023-03-07 20:46:59 +0100767 found_supported_version = 1;
768 break;
769 }
770
Ronald Cron3bd2b022023-04-03 16:45:39 +0200771 if ((MBEDTLS_SSL_VERSION_TLS1_2 == tls_version) &&
772 mbedtls_ssl_conf_is_tls12_enabled(ssl->conf)) {
Ronald Cron5af4c7f2023-03-07 20:46:59 +0100773 found_supported_version = 1;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000774 break;
775 }
XiaokangQian7807f9f2022-02-15 10:04:37 +0000776 }
777
Ronald Cron5af4c7f2023-03-07 20:46:59 +0100778 if (!found_supported_version) {
779 MBEDTLS_SSL_DEBUG_MSG(1, ("No supported version found."));
XiaokangQian7807f9f2022-02-15 10:04:37 +0000780
Gilles Peskine449bd832023-01-11 14:50:10 +0100781 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_PROTOCOL_VERSION,
782 MBEDTLS_ERR_SSL_BAD_PROTOCOL_VERSION);
783 return MBEDTLS_ERR_SSL_BAD_PROTOCOL_VERSION;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000784 }
785
Ronald Cron5af4c7f2023-03-07 20:46:59 +0100786 MBEDTLS_SSL_DEBUG_MSG(1, ("Negotiated version: [%04x]",
Gilles Peskine449bd832023-01-11 14:50:10 +0100787 (unsigned int) tls_version));
XiaokangQian7807f9f2022-02-15 10:04:37 +0000788
Ronald Cron5af4c7f2023-03-07 20:46:59 +0100789 return (int) tls_version;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000790}
791
Przemek Stekiel8c0a9532023-06-15 16:48:19 +0200792#if defined(PSA_WANT_ALG_ECDH) || defined(PSA_WANT_ALG_FFDH)
XiaokangQiane8ff3502022-04-22 02:34:40 +0000793/*
XiaokangQian7807f9f2022-02-15 10:04:37 +0000794 *
795 * From RFC 8446:
796 * enum {
797 * ... (0xFFFF)
798 * } NamedGroup;
799 * struct {
800 * NamedGroup named_group_list<2..2^16-1>;
801 * } NamedGroupList;
802 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200803MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100804static int ssl_tls13_parse_supported_groups_ext(mbedtls_ssl_context *ssl,
805 const unsigned char *buf,
806 const unsigned char *end)
XiaokangQian7807f9f2022-02-15 10:04:37 +0000807{
XiaokangQian7807f9f2022-02-15 10:04:37 +0000808 const unsigned char *p = buf;
XiaokangQian84823772022-04-19 07:57:30 +0000809 size_t named_group_list_len;
XiaokangQian8f9dfe42022-04-15 02:52:39 +0000810 const unsigned char *named_group_list_end;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000811
Gilles Peskine449bd832023-01-11 14:50:10 +0100812 MBEDTLS_SSL_DEBUG_BUF(3, "supported_groups extension", p, end - buf);
813 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, 2);
814 named_group_list_len = MBEDTLS_GET_UINT16_BE(p, 0);
XiaokangQian7807f9f2022-02-15 10:04:37 +0000815 p += 2;
Gilles Peskine449bd832023-01-11 14:50:10 +0100816 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, named_group_list_len);
XiaokangQian8f9dfe42022-04-15 02:52:39 +0000817 named_group_list_end = p + named_group_list_len;
XiaokangQian4e8cd7b2022-04-21 09:48:09 +0000818 ssl->handshake->hrr_selected_group = 0;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000819
Gilles Peskine449bd832023-01-11 14:50:10 +0100820 while (p < named_group_list_end) {
XiaokangQian08037552022-04-20 07:16:41 +0000821 uint16_t named_group;
Gilles Peskine449bd832023-01-11 14:50:10 +0100822 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, named_group_list_end, 2);
823 named_group = MBEDTLS_GET_UINT16_BE(p, 0);
XiaokangQian08037552022-04-20 07:16:41 +0000824 p += 2;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000825
Gilles Peskine449bd832023-01-11 14:50:10 +0100826 MBEDTLS_SSL_DEBUG_MSG(2,
827 ("got named group: %s(%04x)",
828 mbedtls_ssl_named_group_to_str(named_group),
829 named_group));
XiaokangQian08037552022-04-20 07:16:41 +0000830
Gilles Peskine449bd832023-01-11 14:50:10 +0100831 if (!mbedtls_ssl_named_group_is_offered(ssl, named_group) ||
832 !mbedtls_ssl_named_group_is_supported(named_group) ||
833 ssl->handshake->hrr_selected_group != 0) {
XiaokangQian08037552022-04-20 07:16:41 +0000834 continue;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000835 }
836
Gilles Peskine449bd832023-01-11 14:50:10 +0100837 MBEDTLS_SSL_DEBUG_MSG(2,
838 ("add named group %s(%04x) into received list.",
839 mbedtls_ssl_named_group_to_str(named_group),
840 named_group));
Jerry Yuc1be19f2022-04-23 16:11:39 +0800841
XiaokangQian4e8cd7b2022-04-21 09:48:09 +0000842 ssl->handshake->hrr_selected_group = named_group;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000843 }
844
Gilles Peskine449bd832023-01-11 14:50:10 +0100845 return 0;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000846
847}
Przemek Stekiel8c0a9532023-06-15 16:48:19 +0200848#endif /* PSA_WANT_ALG_ECDH || PSA_WANT_ALG_FFDH */
XiaokangQian7807f9f2022-02-15 10:04:37 +0000849
XiaokangQian08037552022-04-20 07:16:41 +0000850#define SSL_TLS1_3_PARSE_KEY_SHARES_EXT_NO_MATCH 1
851
Valerio Settic9ae8622023-07-25 11:23:50 +0200852#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_EPHEMERAL_ENABLED)
XiaokangQian7807f9f2022-02-15 10:04:37 +0000853/*
854 * ssl_tls13_parse_key_shares_ext() verifies whether the information in the
Xiaokang Qian9f1747b2023-03-30 02:50:04 +0000855 * extension is correct and stores the first acceptable key share and its
856 * associated group.
XiaokangQian7807f9f2022-02-15 10:04:37 +0000857 *
858 * Possible return values are:
859 * - 0: Successful processing of the client provided key share extension.
Xiaokang Qian9f1747b2023-03-30 02:50:04 +0000860 * - SSL_TLS1_3_PARSE_KEY_SHARES_EXT_NO_MATCH: The key shares provided by
861 * the client does not match a group supported by the server. A
862 * HelloRetryRequest will be needed.
XiaokangQiane8ff3502022-04-22 02:34:40 +0000863 * - A negative value for fatal errors.
Jerry Yuc1be19f2022-04-23 16:11:39 +0800864 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200865MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100866static int ssl_tls13_parse_key_shares_ext(mbedtls_ssl_context *ssl,
867 const unsigned char *buf,
868 const unsigned char *end)
XiaokangQian7807f9f2022-02-15 10:04:37 +0000869{
XiaokangQianb67384d2022-04-19 00:02:38 +0000870 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000871 unsigned char const *p = buf;
XiaokangQian8f9dfe42022-04-15 02:52:39 +0000872 unsigned char const *client_shares_end;
Jerry Yuc1be19f2022-04-23 16:11:39 +0800873 size_t client_shares_len;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000874
875 /* From RFC 8446:
876 *
877 * struct {
878 * KeyShareEntry client_shares<0..2^16-1>;
879 * } KeyShareClientHello;
880 *
881 */
882
Gilles Peskine449bd832023-01-11 14:50:10 +0100883 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, 2);
884 client_shares_len = MBEDTLS_GET_UINT16_BE(p, 0);
XiaokangQian7807f9f2022-02-15 10:04:37 +0000885 p += 2;
Gilles Peskine449bd832023-01-11 14:50:10 +0100886 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, client_shares_len);
XiaokangQian7807f9f2022-02-15 10:04:37 +0000887
888 ssl->handshake->offered_group_id = 0;
XiaokangQian8f9dfe42022-04-15 02:52:39 +0000889 client_shares_end = p + client_shares_len;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000890
891 /* We try to find a suitable key share entry and copy it to the
892 * handshake context. Later, we have to find out whether we can do
893 * something with the provided key share or whether we have to
XiaokangQianc5763b52022-04-02 03:34:37 +0000894 * dismiss it and send a HelloRetryRequest message.
895 */
XiaokangQian7807f9f2022-02-15 10:04:37 +0000896
Gilles Peskine449bd832023-01-11 14:50:10 +0100897 while (p < client_shares_end) {
XiaokangQian9b5d04b2022-04-10 10:20:43 +0000898 uint16_t group;
Jerry Yuc1be19f2022-04-23 16:11:39 +0800899 size_t key_exchange_len;
Jerry Yu086edc22022-05-05 10:50:38 +0800900 const unsigned char *key_exchange;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000901
902 /*
903 * struct {
904 * NamedGroup group;
905 * opaque key_exchange<1..2^16-1>;
906 * } KeyShareEntry;
907 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100908 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, client_shares_end, 4);
909 group = MBEDTLS_GET_UINT16_BE(p, 0);
910 key_exchange_len = MBEDTLS_GET_UINT16_BE(p, 2);
Jerry Yuc1be19f2022-04-23 16:11:39 +0800911 p += 4;
Jerry Yu086edc22022-05-05 10:50:38 +0800912 key_exchange = p;
Gilles Peskine449bd832023-01-11 14:50:10 +0100913 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, client_shares_end, key_exchange_len);
Jerry Yu086edc22022-05-05 10:50:38 +0800914 p += key_exchange_len;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000915
916 /* Continue parsing even if we have already found a match,
XiaokangQianc5763b52022-04-02 03:34:37 +0000917 * for input validation purposes.
918 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100919 if (!mbedtls_ssl_named_group_is_offered(ssl, group) ||
920 !mbedtls_ssl_named_group_is_supported(group) ||
921 ssl->handshake->offered_group_id != 0) {
XiaokangQian060d8672022-04-21 09:24:56 +0000922 continue;
923 }
XiaokangQian060d8672022-04-21 09:24:56 +0000924
XiaokangQian7807f9f2022-02-15 10:04:37 +0000925 /*
Przemek Stekielc89f3ea2023-05-18 15:45:53 +0200926 * ECDHE and FFDHE groups are supported
XiaokangQian7807f9f2022-02-15 10:04:37 +0000927 */
Przemek Stekielc89f3ea2023-05-18 15:45:53 +0200928 if (mbedtls_ssl_tls13_named_group_is_ecdhe(group) ||
Przemek Stekield5f79e72023-06-29 09:08:43 +0200929 mbedtls_ssl_tls13_named_group_is_ffdh(group)) {
Przemek Stekielc89f3ea2023-05-18 15:45:53 +0200930 MBEDTLS_SSL_DEBUG_MSG(2, ("ECDH/FFDH group: %s (%04x)",
Gilles Peskine449bd832023-01-11 14:50:10 +0100931 mbedtls_ssl_named_group_to_str(group),
932 group));
Przemek Stekiel7ac93be2023-07-04 10:02:38 +0200933 ret = mbedtls_ssl_tls13_read_public_xxdhe_share(
Gilles Peskine449bd832023-01-11 14:50:10 +0100934 ssl, key_exchange - 2, key_exchange_len + 2);
935 if (ret != 0) {
936 return ret;
937 }
XiaokangQian4e8cd7b2022-04-21 09:48:09 +0000938
Gilles Peskine449bd832023-01-11 14:50:10 +0100939 } else {
940 MBEDTLS_SSL_DEBUG_MSG(4, ("Unrecognized NamedGroup %u",
941 (unsigned) group));
XiaokangQian7807f9f2022-02-15 10:04:37 +0000942 continue;
943 }
944
XiaokangQian9b5d04b2022-04-10 10:20:43 +0000945 ssl->handshake->offered_group_id = group;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000946 }
947
Jerry Yuc1be19f2022-04-23 16:11:39 +0800948
Gilles Peskine449bd832023-01-11 14:50:10 +0100949 if (ssl->handshake->offered_group_id == 0) {
950 MBEDTLS_SSL_DEBUG_MSG(1, ("no matching key share"));
951 return SSL_TLS1_3_PARSE_KEY_SHARES_EXT_NO_MATCH;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000952 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100953 return 0;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000954}
Valerio Settic9ae8622023-07-25 11:23:50 +0200955#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_EPHEMERAL_ENABLED */
XiaokangQian7807f9f2022-02-15 10:04:37 +0000956
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200957MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100958static int ssl_tls13_client_hello_has_exts(mbedtls_ssl_context *ssl,
959 int exts_mask)
XiaokangQian7807f9f2022-02-15 10:04:37 +0000960{
Jerry Yu0c354a22022-08-29 15:25:36 +0800961 int masked = ssl->handshake->received_extensions & exts_mask;
Gilles Peskine449bd832023-01-11 14:50:10 +0100962 return masked == exts_mask;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000963}
964
Jerry Yue5991322022-11-07 14:03:44 +0800965#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200966MBEDTLS_CHECK_RETURN_CRITICAL
XiaokangQianb67384d2022-04-19 00:02:38 +0000967static int ssl_tls13_client_hello_has_exts_for_ephemeral_key_exchange(
Gilles Peskine449bd832023-01-11 14:50:10 +0100968 mbedtls_ssl_context *ssl)
XiaokangQian7807f9f2022-02-15 10:04:37 +0000969{
Gilles Peskine449bd832023-01-11 14:50:10 +0100970 return ssl_tls13_client_hello_has_exts(
971 ssl,
972 MBEDTLS_SSL_EXT_MASK(SUPPORTED_GROUPS) |
973 MBEDTLS_SSL_EXT_MASK(KEY_SHARE) |
974 MBEDTLS_SSL_EXT_MASK(SIG_ALG));
XiaokangQian7807f9f2022-02-15 10:04:37 +0000975}
Jerry Yue5991322022-11-07 14:03:44 +0800976#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED */
XiaokangQian7807f9f2022-02-15 10:04:37 +0000977
Jerry Yue5991322022-11-07 14:03:44 +0800978#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK_ENABLED)
Jerry Yu77f01482022-07-11 07:03:24 +0000979MBEDTLS_CHECK_RETURN_CRITICAL
980static int ssl_tls13_client_hello_has_exts_for_psk_key_exchange(
Gilles Peskine449bd832023-01-11 14:50:10 +0100981 mbedtls_ssl_context *ssl)
Jerry Yu77f01482022-07-11 07:03:24 +0000982{
Gilles Peskine449bd832023-01-11 14:50:10 +0100983 return ssl_tls13_client_hello_has_exts(
984 ssl,
985 MBEDTLS_SSL_EXT_MASK(PRE_SHARED_KEY) |
986 MBEDTLS_SSL_EXT_MASK(PSK_KEY_EXCHANGE_MODES));
Jerry Yu77f01482022-07-11 07:03:24 +0000987}
Jerry Yue5991322022-11-07 14:03:44 +0800988#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK_ENABLED */
Jerry Yu77f01482022-07-11 07:03:24 +0000989
Jerry Yue5991322022-11-07 14:03:44 +0800990#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK_EPHEMERAL_ENABLED)
Jerry Yu77f01482022-07-11 07:03:24 +0000991MBEDTLS_CHECK_RETURN_CRITICAL
992static int ssl_tls13_client_hello_has_exts_for_psk_ephemeral_key_exchange(
Gilles Peskine449bd832023-01-11 14:50:10 +0100993 mbedtls_ssl_context *ssl)
Jerry Yu77f01482022-07-11 07:03:24 +0000994{
Gilles Peskine449bd832023-01-11 14:50:10 +0100995 return ssl_tls13_client_hello_has_exts(
996 ssl,
997 MBEDTLS_SSL_EXT_MASK(SUPPORTED_GROUPS) |
998 MBEDTLS_SSL_EXT_MASK(KEY_SHARE) |
999 MBEDTLS_SSL_EXT_MASK(PRE_SHARED_KEY) |
1000 MBEDTLS_SSL_EXT_MASK(PSK_KEY_EXCHANGE_MODES));
Jerry Yu77f01482022-07-11 07:03:24 +00001001}
Jerry Yue5991322022-11-07 14:03:44 +08001002#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK_EPHEMERAL_ENABLED */
Jerry Yu77f01482022-07-11 07:03:24 +00001003
Pengyu Lv306a01d2023-02-02 16:35:47 +08001004#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED)
1005MBEDTLS_CHECK_RETURN_CRITICAL
Pengyu Lvd72e8582023-11-10 10:37:18 +08001006static int ssl_tls13_ticket_is_kex_mode_permitted(mbedtls_ssl_context *ssl,
1007 unsigned int kex_mode)
Pengyu Lv306a01d2023-02-02 16:35:47 +08001008{
1009#if defined(MBEDTLS_SSL_SESSION_TICKETS)
1010 if (ssl->handshake->resume) {
Pengyu Lv94a42cc2023-12-06 10:04:17 +08001011 if (!mbedtls_ssl_tls13_session_ticket_has_flags(
Pengyu Lv306a01d2023-02-02 16:35:47 +08001012 ssl->session_negotiate, kex_mode)) {
1013 return 0;
1014 }
1015 }
1016#else
1017 ((void) ssl);
1018 ((void) kex_mode);
1019#endif
1020 return 1;
1021}
1022#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED */
1023
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001024MBEDTLS_CHECK_RETURN_CRITICAL
Pengyu Lvbc4aab72023-12-01 15:37:24 +08001025static int ssl_tls13_key_exchange_is_ephemeral_available(mbedtls_ssl_context *ssl)
XiaokangQian7807f9f2022-02-15 10:04:37 +00001026{
Jerry Yue5991322022-11-07 14:03:44 +08001027#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED)
Pengyu Lv0a1ff2b2023-11-14 11:03:32 +08001028 return mbedtls_ssl_conf_tls13_is_ephemeral_enabled(ssl) &&
Gilles Peskine449bd832023-01-11 14:50:10 +01001029 ssl_tls13_client_hello_has_exts_for_ephemeral_key_exchange(ssl);
Jerry Yue5991322022-11-07 14:03:44 +08001030#else
1031 ((void) ssl);
Gilles Peskine449bd832023-01-11 14:50:10 +01001032 return 0;
Jerry Yue5991322022-11-07 14:03:44 +08001033#endif
Jerry Yu77f01482022-07-11 07:03:24 +00001034}
XiaokangQian7807f9f2022-02-15 10:04:37 +00001035
Jerry Yu77f01482022-07-11 07:03:24 +00001036MBEDTLS_CHECK_RETURN_CRITICAL
Pengyu Lvbc4aab72023-12-01 15:37:24 +08001037static int ssl_tls13_key_exchange_is_psk_available(mbedtls_ssl_context *ssl)
Jerry Yu77f01482022-07-11 07:03:24 +00001038{
Jerry Yue5991322022-11-07 14:03:44 +08001039#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK_ENABLED)
Pengyu Lvd72e8582023-11-10 10:37:18 +08001040 return ssl_tls13_ticket_is_kex_mode_permitted(
Pengyu Lv306a01d2023-02-02 16:35:47 +08001041 ssl, MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK) &&
Pengyu Lv0a1ff2b2023-11-14 11:03:32 +08001042 mbedtls_ssl_conf_tls13_is_psk_enabled(ssl) &&
Pengyu Lvb2cfafb2023-11-14 13:56:13 +08001043 mbedtls_ssl_tls13_is_psk_supported(ssl) &&
Gilles Peskine449bd832023-01-11 14:50:10 +01001044 ssl_tls13_client_hello_has_exts_for_psk_key_exchange(ssl);
Jerry Yu77f01482022-07-11 07:03:24 +00001045#else
1046 ((void) ssl);
Gilles Peskine449bd832023-01-11 14:50:10 +01001047 return 0;
Jerry Yu77f01482022-07-11 07:03:24 +00001048#endif
1049}
XiaokangQian7807f9f2022-02-15 10:04:37 +00001050
Jerry Yu77f01482022-07-11 07:03:24 +00001051MBEDTLS_CHECK_RETURN_CRITICAL
Pengyu Lvbc4aab72023-12-01 15:37:24 +08001052static int ssl_tls13_key_exchange_is_psk_ephemeral_available(mbedtls_ssl_context *ssl)
Jerry Yu77f01482022-07-11 07:03:24 +00001053{
Jerry Yue5991322022-11-07 14:03:44 +08001054#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK_EPHEMERAL_ENABLED)
Pengyu Lvd72e8582023-11-10 10:37:18 +08001055 return ssl_tls13_ticket_is_kex_mode_permitted(
Pengyu Lv306a01d2023-02-02 16:35:47 +08001056 ssl, MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK_EPHEMERAL) &&
Pengyu Lv0a1ff2b2023-11-14 11:03:32 +08001057 mbedtls_ssl_conf_tls13_is_psk_ephemeral_enabled(ssl) &&
Pengyu Lvb2cfafb2023-11-14 13:56:13 +08001058 mbedtls_ssl_tls13_is_psk_ephemeral_supported(ssl) &&
Gilles Peskine449bd832023-01-11 14:50:10 +01001059 ssl_tls13_client_hello_has_exts_for_psk_ephemeral_key_exchange(ssl);
Jerry Yu77f01482022-07-11 07:03:24 +00001060#else
1061 ((void) ssl);
Gilles Peskine449bd832023-01-11 14:50:10 +01001062 return 0;
Jerry Yu77f01482022-07-11 07:03:24 +00001063#endif
1064}
1065
Gilles Peskine449bd832023-01-11 14:50:10 +01001066static int ssl_tls13_determine_key_exchange_mode(mbedtls_ssl_context *ssl)
Jerry Yu77f01482022-07-11 07:03:24 +00001067{
1068 /*
1069 * Determine the key exchange algorithm to use.
1070 * There are three types of key exchanges supported in TLS 1.3:
1071 * - (EC)DH with ECDSA,
1072 * - (EC)DH with PSK,
1073 * - plain PSK.
1074 *
1075 * The PSK-based key exchanges may additionally be used with 0-RTT.
1076 *
1077 * Our built-in order of preference is
Jerry Yuce6ed702022-07-22 21:49:53 +08001078 * 1 ) (EC)DHE-PSK Mode ( psk_ephemeral )
1079 * 2 ) Certificate Mode ( ephemeral )
1080 * 3 ) Plain PSK Mode ( psk )
Jerry Yu77f01482022-07-11 07:03:24 +00001081 */
1082
Xiaokang Qian9f1747b2023-03-30 02:50:04 +00001083 ssl->handshake->key_exchange_mode =
1084 MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_NONE;
Jerry Yu77f01482022-07-11 07:03:24 +00001085
Pengyu Lvbc4aab72023-12-01 15:37:24 +08001086 if (ssl_tls13_key_exchange_is_psk_ephemeral_available(ssl)) {
Jerry Yu77f01482022-07-11 07:03:24 +00001087 ssl->handshake->key_exchange_mode =
1088 MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK_EPHEMERAL;
Gilles Peskine449bd832023-01-11 14:50:10 +01001089 MBEDTLS_SSL_DEBUG_MSG(2, ("key exchange mode: psk_ephemeral"));
1090 } else
Pengyu Lvbc4aab72023-12-01 15:37:24 +08001091 if (ssl_tls13_key_exchange_is_ephemeral_available(ssl)) {
Jerry Yu77f01482022-07-11 07:03:24 +00001092 ssl->handshake->key_exchange_mode =
1093 MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL;
Gilles Peskine449bd832023-01-11 14:50:10 +01001094 MBEDTLS_SSL_DEBUG_MSG(2, ("key exchange mode: ephemeral"));
1095 } else
Pengyu Lvbc4aab72023-12-01 15:37:24 +08001096 if (ssl_tls13_key_exchange_is_psk_available(ssl)) {
Jerry Yuce6ed702022-07-22 21:49:53 +08001097 ssl->handshake->key_exchange_mode =
1098 MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK;
Gilles Peskine449bd832023-01-11 14:50:10 +01001099 MBEDTLS_SSL_DEBUG_MSG(2, ("key exchange mode: psk"));
1100 } else {
Jerry Yu77f01482022-07-11 07:03:24 +00001101 MBEDTLS_SSL_DEBUG_MSG(
Gilles Peskine449bd832023-01-11 14:50:10 +01001102 1,
1103 ("ClientHello message misses mandatory extensions."));
1104 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_MISSING_EXTENSION,
1105 MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER);
1106 return MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER;
Jerry Yu77f01482022-07-11 07:03:24 +00001107 }
1108
Gilles Peskine449bd832023-01-11 14:50:10 +01001109 return 0;
Jerry Yu77f01482022-07-11 07:03:24 +00001110
XiaokangQian7807f9f2022-02-15 10:04:37 +00001111}
1112
XiaokangQian81802f42022-06-10 13:25:22 +00001113#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
Ronald Cron928cbd32022-10-04 16:14:26 +02001114 defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED)
Ronald Cron67ea2542022-09-15 17:34:42 +02001115
1116#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01001117static psa_algorithm_t ssl_tls13_iana_sig_alg_to_psa_alg(uint16_t sig_alg)
Ronald Cron67ea2542022-09-15 17:34:42 +02001118{
Gilles Peskine449bd832023-01-11 14:50:10 +01001119 switch (sig_alg) {
Ronald Cron67ea2542022-09-15 17:34:42 +02001120 case MBEDTLS_TLS1_3_SIG_ECDSA_SECP256R1_SHA256:
Gilles Peskine449bd832023-01-11 14:50:10 +01001121 return PSA_ALG_ECDSA(PSA_ALG_SHA_256);
Ronald Cron67ea2542022-09-15 17:34:42 +02001122 case MBEDTLS_TLS1_3_SIG_ECDSA_SECP384R1_SHA384:
Gilles Peskine449bd832023-01-11 14:50:10 +01001123 return PSA_ALG_ECDSA(PSA_ALG_SHA_384);
Ronald Cron67ea2542022-09-15 17:34:42 +02001124 case MBEDTLS_TLS1_3_SIG_ECDSA_SECP521R1_SHA512:
Gilles Peskine449bd832023-01-11 14:50:10 +01001125 return PSA_ALG_ECDSA(PSA_ALG_SHA_512);
Ronald Cron67ea2542022-09-15 17:34:42 +02001126 case MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA256:
Gilles Peskine449bd832023-01-11 14:50:10 +01001127 return PSA_ALG_RSA_PSS(PSA_ALG_SHA_256);
Ronald Cron67ea2542022-09-15 17:34:42 +02001128 case MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA384:
Gilles Peskine449bd832023-01-11 14:50:10 +01001129 return PSA_ALG_RSA_PSS(PSA_ALG_SHA_384);
Ronald Cron67ea2542022-09-15 17:34:42 +02001130 case MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA512:
Gilles Peskine449bd832023-01-11 14:50:10 +01001131 return PSA_ALG_RSA_PSS(PSA_ALG_SHA_512);
Ronald Cron67ea2542022-09-15 17:34:42 +02001132 case MBEDTLS_TLS1_3_SIG_RSA_PKCS1_SHA256:
Gilles Peskine449bd832023-01-11 14:50:10 +01001133 return PSA_ALG_RSA_PKCS1V15_SIGN(PSA_ALG_SHA_256);
Ronald Cron67ea2542022-09-15 17:34:42 +02001134 case MBEDTLS_TLS1_3_SIG_RSA_PKCS1_SHA384:
Gilles Peskine449bd832023-01-11 14:50:10 +01001135 return PSA_ALG_RSA_PKCS1V15_SIGN(PSA_ALG_SHA_384);
Ronald Cron67ea2542022-09-15 17:34:42 +02001136 case MBEDTLS_TLS1_3_SIG_RSA_PKCS1_SHA512:
Gilles Peskine449bd832023-01-11 14:50:10 +01001137 return PSA_ALG_RSA_PKCS1V15_SIGN(PSA_ALG_SHA_512);
Ronald Cron67ea2542022-09-15 17:34:42 +02001138 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01001139 return PSA_ALG_NONE;
Ronald Cron67ea2542022-09-15 17:34:42 +02001140 }
1141}
1142#endif /* MBEDTLS_USE_PSA_CRYPTO */
1143
XiaokangQian23c5be62022-06-07 02:04:34 +00001144/*
XiaokangQianfb665a82022-06-15 03:57:21 +00001145 * Pick best ( private key, certificate chain ) pair based on the signature
1146 * algorithms supported by the client.
XiaokangQian23c5be62022-06-07 02:04:34 +00001147 */
Ronald Cronce7d76e2022-07-08 18:56:49 +02001148MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01001149static int ssl_tls13_pick_key_cert(mbedtls_ssl_context *ssl)
XiaokangQian23c5be62022-06-07 02:04:34 +00001150{
XiaokangQianfb665a82022-06-15 03:57:21 +00001151 mbedtls_ssl_key_cert *key_cert, *key_cert_list;
XiaokangQian81802f42022-06-10 13:25:22 +00001152 const uint16_t *sig_alg = ssl->handshake->received_sig_algs;
XiaokangQian23c5be62022-06-07 02:04:34 +00001153
1154#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Gilles Peskine449bd832023-01-11 14:50:10 +01001155 if (ssl->handshake->sni_key_cert != NULL) {
XiaokangQianfb665a82022-06-15 03:57:21 +00001156 key_cert_list = ssl->handshake->sni_key_cert;
Gilles Peskine449bd832023-01-11 14:50:10 +01001157 } else
XiaokangQian23c5be62022-06-07 02:04:34 +00001158#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
Gilles Peskine449bd832023-01-11 14:50:10 +01001159 key_cert_list = ssl->conf->key_cert;
XiaokangQian23c5be62022-06-07 02:04:34 +00001160
Gilles Peskine449bd832023-01-11 14:50:10 +01001161 if (key_cert_list == NULL) {
1162 MBEDTLS_SSL_DEBUG_MSG(3, ("server has no certificate"));
1163 return -1;
XiaokangQian23c5be62022-06-07 02:04:34 +00001164 }
1165
Gilles Peskine449bd832023-01-11 14:50:10 +01001166 for (; *sig_alg != MBEDTLS_TLS1_3_SIG_NONE; sig_alg++) {
1167 if (!mbedtls_ssl_sig_alg_is_offered(ssl, *sig_alg)) {
Ronald Cron67ea2542022-09-15 17:34:42 +02001168 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +01001169 }
Ronald Cron67ea2542022-09-15 17:34:42 +02001170
Gilles Peskine449bd832023-01-11 14:50:10 +01001171 if (!mbedtls_ssl_tls13_sig_alg_for_cert_verify_is_supported(*sig_alg)) {
Ronald Cron67ea2542022-09-15 17:34:42 +02001172 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +01001173 }
Ronald Cron67ea2542022-09-15 17:34:42 +02001174
Gilles Peskine449bd832023-01-11 14:50:10 +01001175 for (key_cert = key_cert_list; key_cert != NULL;
1176 key_cert = key_cert->next) {
Ronald Cron67ea2542022-09-15 17:34:42 +02001177#if defined(MBEDTLS_USE_PSA_CRYPTO)
1178 psa_algorithm_t psa_alg = PSA_ALG_NONE;
1179#endif /* MBEDTLS_USE_PSA_CRYPTO */
1180
Gilles Peskine449bd832023-01-11 14:50:10 +01001181 MBEDTLS_SSL_DEBUG_CRT(3, "certificate (chain) candidate",
1182 key_cert->cert);
XiaokangQian81802f42022-06-10 13:25:22 +00001183
1184 /*
Gilles Peskine449bd832023-01-11 14:50:10 +01001185 * This avoids sending the client a cert it'll reject based on
1186 * keyUsage or other extensions.
1187 */
1188 if (mbedtls_x509_crt_check_key_usage(
1189 key_cert->cert, MBEDTLS_X509_KU_DIGITAL_SIGNATURE) != 0 ||
XiaokangQian81802f42022-06-10 13:25:22 +00001190 mbedtls_x509_crt_check_extended_key_usage(
XiaokangQianfb665a82022-06-15 03:57:21 +00001191 key_cert->cert, MBEDTLS_OID_SERVER_AUTH,
Gilles Peskine449bd832023-01-11 14:50:10 +01001192 MBEDTLS_OID_SIZE(MBEDTLS_OID_SERVER_AUTH)) != 0) {
1193 MBEDTLS_SSL_DEBUG_MSG(3, ("certificate mismatch: "
1194 "(extended) key usage extension"));
XiaokangQian81802f42022-06-10 13:25:22 +00001195 continue;
1196 }
XiaokangQian23c5be62022-06-07 02:04:34 +00001197
Gilles Peskine449bd832023-01-11 14:50:10 +01001198 MBEDTLS_SSL_DEBUG_MSG(3,
1199 ("ssl_tls13_pick_key_cert:"
1200 "check signature algorithm %s [%04x]",
1201 mbedtls_ssl_sig_alg_to_str(*sig_alg),
1202 *sig_alg));
Ronald Cron67ea2542022-09-15 17:34:42 +02001203#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01001204 psa_alg = ssl_tls13_iana_sig_alg_to_psa_alg(*sig_alg);
Ronald Cron67ea2542022-09-15 17:34:42 +02001205#endif /* MBEDTLS_USE_PSA_CRYPTO */
1206
Gilles Peskine449bd832023-01-11 14:50:10 +01001207 if (mbedtls_ssl_tls13_check_sig_alg_cert_key_match(
1208 *sig_alg, &key_cert->cert->pk)
Ronald Cron67ea2542022-09-15 17:34:42 +02001209#if defined(MBEDTLS_USE_PSA_CRYPTO)
1210 && psa_alg != PSA_ALG_NONE &&
Gilles Peskine449bd832023-01-11 14:50:10 +01001211 mbedtls_pk_can_do_ext(&key_cert->cert->pk, psa_alg,
1212 PSA_KEY_USAGE_SIGN_HASH) == 1
Ronald Cron67ea2542022-09-15 17:34:42 +02001213#endif /* MBEDTLS_USE_PSA_CRYPTO */
Gilles Peskine449bd832023-01-11 14:50:10 +01001214 ) {
XiaokangQianfb665a82022-06-15 03:57:21 +00001215 ssl->handshake->key_cert = key_cert;
Gilles Peskine449bd832023-01-11 14:50:10 +01001216 MBEDTLS_SSL_DEBUG_MSG(3,
1217 ("ssl_tls13_pick_key_cert:"
1218 "selected signature algorithm"
1219 " %s [%04x]",
1220 mbedtls_ssl_sig_alg_to_str(*sig_alg),
1221 *sig_alg));
XiaokangQianfb665a82022-06-15 03:57:21 +00001222 MBEDTLS_SSL_DEBUG_CRT(
Gilles Peskine449bd832023-01-11 14:50:10 +01001223 3, "selected certificate (chain)",
1224 ssl->handshake->key_cert->cert);
1225 return 0;
XiaokangQian81802f42022-06-10 13:25:22 +00001226 }
XiaokangQian81802f42022-06-10 13:25:22 +00001227 }
XiaokangQian23c5be62022-06-07 02:04:34 +00001228 }
1229
Gilles Peskine449bd832023-01-11 14:50:10 +01001230 MBEDTLS_SSL_DEBUG_MSG(2, ("ssl_tls13_pick_key_cert:"
1231 "no suitable certificate found"));
1232 return -1;
XiaokangQian23c5be62022-06-07 02:04:34 +00001233}
XiaokangQian81802f42022-06-10 13:25:22 +00001234#endif /* MBEDTLS_X509_CRT_PARSE_C &&
Ronald Cron928cbd32022-10-04 16:14:26 +02001235 MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED */
XiaokangQian23c5be62022-06-07 02:04:34 +00001236
XiaokangQian4080a7f2022-04-11 09:55:18 +00001237/*
XiaokangQianed582dd2022-04-13 08:21:05 +00001238 *
1239 * STATE HANDLING: ClientHello
1240 *
XiaokangQianb67384d2022-04-19 00:02:38 +00001241 * There are three possible classes of outcomes when parsing the ClientHello:
XiaokangQianed582dd2022-04-13 08:21:05 +00001242 *
XiaokangQianb67384d2022-04-19 00:02:38 +00001243 * 1) The ClientHello was well-formed and matched the server's configuration.
XiaokangQianed582dd2022-04-13 08:21:05 +00001244 *
1245 * In this case, the server progresses to sending its ServerHello.
1246 *
XiaokangQianb67384d2022-04-19 00:02:38 +00001247 * 2) The ClientHello was well-formed but didn't match the server's
1248 * configuration.
XiaokangQianed582dd2022-04-13 08:21:05 +00001249 *
1250 * For example, the client might not have offered a key share which
1251 * the server supports, or the server might require a cookie.
1252 *
1253 * In this case, the server sends a HelloRetryRequest.
1254 *
XiaokangQianb67384d2022-04-19 00:02:38 +00001255 * 3) The ClientHello was ill-formed
XiaokangQianed582dd2022-04-13 08:21:05 +00001256 *
1257 * In this case, we abort the handshake.
1258 *
1259 */
1260
1261/*
XiaokangQian4080a7f2022-04-11 09:55:18 +00001262 * Structure of this message:
1263 *
XiaokangQiane8ff3502022-04-22 02:34:40 +00001264 * uint16 ProtocolVersion;
1265 * opaque Random[32];
1266 * uint8 CipherSuite[2]; // Cryptographic suite selector
XiaokangQian4080a7f2022-04-11 09:55:18 +00001267 *
XiaokangQiane8ff3502022-04-22 02:34:40 +00001268 * struct {
1269 * ProtocolVersion legacy_version = 0x0303; // TLS v1.2
1270 * Random random;
1271 * opaque legacy_session_id<0..32>;
1272 * CipherSuite cipher_suites<2..2^16-2>;
1273 * opaque legacy_compression_methods<1..2^8-1>;
1274 * Extension extensions<8..2^16-1>;
1275 * } ClientHello;
XiaokangQian4080a7f2022-04-11 09:55:18 +00001276 */
XiaokangQianed582dd2022-04-13 08:21:05 +00001277
1278#define SSL_CLIENT_HELLO_OK 0
1279#define SSL_CLIENT_HELLO_HRR_REQUIRED 1
Ronald Cron5af4c7f2023-03-07 20:46:59 +01001280#define SSL_CLIENT_HELLO_TLS1_2 2
XiaokangQianed582dd2022-04-13 08:21:05 +00001281
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001282MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01001283static int ssl_tls13_parse_client_hello(mbedtls_ssl_context *ssl,
1284 const unsigned char *buf,
1285 const unsigned char *end)
XiaokangQian7807f9f2022-02-15 10:04:37 +00001286{
XiaokangQianb67384d2022-04-19 00:02:38 +00001287 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1288 const unsigned char *p = buf;
Ronald Crond540d992023-03-07 09:41:48 +01001289 const unsigned char *random;
XiaokangQian4080a7f2022-04-11 09:55:18 +00001290 size_t legacy_session_id_len;
Ronald Croncada4102023-03-07 09:51:39 +01001291 const unsigned char *legacy_session_id;
XiaokangQian318dc762022-04-20 09:43:51 +00001292 size_t cipher_suites_len;
Ronald Cron2f16b4e2023-03-07 10:07:32 +01001293 const unsigned char *cipher_suites;
XiaokangQian060d8672022-04-21 09:24:56 +00001294 const unsigned char *cipher_suites_end;
XiaokangQianb67384d2022-04-19 00:02:38 +00001295 size_t extensions_len;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001296 const unsigned char *extensions_end;
Ronald Croneff56732023-04-03 17:36:31 +02001297 const unsigned char *supported_versions_data;
1298 const unsigned char *supported_versions_data_end;
Jerry Yuc4bf5d62022-10-29 09:08:47 +08001299 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
Jerry Yu49ca9282022-05-05 11:05:22 +08001300 int hrr_required = 0;
Ronald Cron8a74f072023-06-14 17:59:29 +02001301 int no_usable_share_for_key_agreement = 0;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001302
Ronald Cron41a443a2022-10-04 16:38:25 +02001303#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED)
Jerry Yu29d9faa2022-08-23 17:52:45 +08001304 const unsigned char *pre_shared_key_ext = NULL;
Jerry Yu1c105562022-07-10 06:32:38 +00001305 const unsigned char *pre_shared_key_ext_end = NULL;
Ronald Cron41a443a2022-10-04 16:38:25 +02001306#endif
XiaokangQian7807f9f2022-02-15 10:04:37 +00001307
XiaokangQian7807f9f2022-02-15 10:04:37 +00001308 /*
XiaokangQianb67384d2022-04-19 00:02:38 +00001309 * ClientHello layout:
XiaokangQian7807f9f2022-02-15 10:04:37 +00001310 * 0 . 1 protocol version
XiaokangQiane8ff3502022-04-22 02:34:40 +00001311 * 2 . 33 random bytes
XiaokangQianc5763b52022-04-02 03:34:37 +00001312 * 34 . 34 session id length ( 1 byte )
XiaokangQian7807f9f2022-02-15 10:04:37 +00001313 * 35 . 34+x session id
XiaokangQian7807f9f2022-02-15 10:04:37 +00001314 * .. . .. ciphersuite list length ( 2 bytes )
1315 * .. . .. ciphersuite list
1316 * .. . .. compression alg. list length ( 1 byte )
1317 * .. . .. compression alg. list
1318 * .. . .. extensions length ( 2 bytes, optional )
1319 * .. . .. extensions ( optional )
1320 */
1321
XiaokangQianb67384d2022-04-19 00:02:38 +00001322 /*
bootstrap-prime6dbbf442022-05-17 19:30:44 -04001323 * Minimal length ( with everything empty and extensions omitted ) is
XiaokangQian7807f9f2022-02-15 10:04:37 +00001324 * 2 + 32 + 1 + 2 + 1 = 38 bytes. Check that first, so that we can
1325 * read at least up to session id length without worrying.
1326 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001327 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, 38);
XiaokangQian7807f9f2022-02-15 10:04:37 +00001328
1329 /* ...
1330 * ProtocolVersion legacy_version = 0x0303; // TLS 1.2
1331 * ...
1332 * with ProtocolVersion defined as:
1333 * uint16 ProtocolVersion;
1334 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001335 if (mbedtls_ssl_read_version(p, ssl->conf->transport) !=
1336 MBEDTLS_SSL_VERSION_TLS1_2) {
1337 MBEDTLS_SSL_DEBUG_MSG(1, ("Unsupported version of TLS."));
1338 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_PROTOCOL_VERSION,
1339 MBEDTLS_ERR_SSL_BAD_PROTOCOL_VERSION);
1340 return MBEDTLS_ERR_SSL_BAD_PROTOCOL_VERSION;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001341 }
1342 p += 2;
1343
Jerry Yuc1be19f2022-04-23 16:11:39 +08001344 /* ...
1345 * Random random;
1346 * ...
XiaokangQianb67384d2022-04-19 00:02:38 +00001347 * with Random defined as:
1348 * opaque Random[32];
XiaokangQian7807f9f2022-02-15 10:04:37 +00001349 */
Ronald Crond540d992023-03-07 09:41:48 +01001350 random = p;
XiaokangQian08037552022-04-20 07:16:41 +00001351 p += MBEDTLS_CLIENT_HELLO_RANDOM_LEN;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001352
Jerry Yuc1be19f2022-04-23 16:11:39 +08001353 /* ...
XiaokangQianb67384d2022-04-19 00:02:38 +00001354 * opaque legacy_session_id<0..32>;
Jerry Yuc1be19f2022-04-23 16:11:39 +08001355 * ...
XiaokangQian7807f9f2022-02-15 10:04:37 +00001356 */
Ronald Croncada4102023-03-07 09:51:39 +01001357 legacy_session_id_len = *(p++);
1358 legacy_session_id = p;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001359
XiaokangQianb67384d2022-04-19 00:02:38 +00001360 /*
1361 * Check we have enough data for the legacy session identifier
Jerry Yue95c8af2022-07-26 15:48:20 +08001362 * and the ciphersuite list length.
XiaokangQianb67384d2022-04-19 00:02:38 +00001363 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001364 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, legacy_session_id_len + 2);
XiaokangQian4080a7f2022-04-11 09:55:18 +00001365 p += legacy_session_id_len;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001366
Ronald Cron2f16b4e2023-03-07 10:07:32 +01001367 /* ...
1368 * CipherSuite cipher_suites<2..2^16-2>;
1369 * ...
1370 * with CipherSuite defined as:
1371 * uint8 CipherSuite[2];
1372 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001373 cipher_suites_len = MBEDTLS_GET_UINT16_BE(p, 0);
XiaokangQian7807f9f2022-02-15 10:04:37 +00001374 p += 2;
Ronald Cron2f16b4e2023-03-07 10:07:32 +01001375 cipher_suites = p;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001376
Ronald Cronfc7ae872023-02-16 15:32:19 +01001377 /*
1378 * The length of the ciphersuite list has to be even.
1379 */
1380 if (cipher_suites_len & 1) {
1381 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR,
1382 MBEDTLS_ERR_SSL_DECODE_ERROR);
1383 return MBEDTLS_ERR_SSL_DECODE_ERROR;
1384 }
1385
XiaokangQianb67384d2022-04-19 00:02:38 +00001386 /* Check we have enough data for the ciphersuite list, the legacy
1387 * compression methods and the length of the extensions.
Jerry Yue95c8af2022-07-26 15:48:20 +08001388 *
1389 * cipher_suites cipher_suites_len bytes
1390 * legacy_compression_methods 2 bytes
1391 * extensions_len 2 bytes
XiaokangQianb67384d2022-04-19 00:02:38 +00001392 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001393 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, cipher_suites_len + 2 + 2);
Ronald Cron8c527d02023-03-07 15:47:47 +01001394 p += cipher_suites_len;
1395 cipher_suites_end = p;
Jerry Yu5725f1c2022-08-21 17:27:16 +08001396
1397 /*
Ronald Cron8c527d02023-03-07 15:47:47 +01001398 * Search for the supported versions extension and parse it to determine
1399 * if the client supports TLS 1.3.
1400 */
1401 ret = mbedtls_ssl_tls13_is_supported_versions_ext_present_in_exts(
1402 ssl, p + 2, end,
Ronald Croneff56732023-04-03 17:36:31 +02001403 &supported_versions_data, &supported_versions_data_end);
Ronald Cron8c527d02023-03-07 15:47:47 +01001404 if (ret < 0) {
1405 MBEDTLS_SSL_DEBUG_RET(1,
1406 ("mbedtls_ssl_tls13_is_supported_versions_ext_present_in_exts"), ret);
1407 return ret;
1408 }
1409
1410 if (ret == 0) {
Ronald Cron5af4c7f2023-03-07 20:46:59 +01001411 return SSL_CLIENT_HELLO_TLS1_2;
Ronald Cron8c527d02023-03-07 15:47:47 +01001412 }
1413
Ronald Cron5af4c7f2023-03-07 20:46:59 +01001414 if (ret == 1) {
1415 ret = ssl_tls13_parse_supported_versions_ext(ssl,
Ronald Croneff56732023-04-03 17:36:31 +02001416 supported_versions_data,
1417 supported_versions_data_end);
Ronald Cron5af4c7f2023-03-07 20:46:59 +01001418 if (ret < 0) {
1419 MBEDTLS_SSL_DEBUG_RET(1,
1420 ("ssl_tls13_parse_supported_versions_ext"), ret);
1421 return ret;
1422 }
1423
Ronald Cronb828c7d2023-04-03 16:37:22 +02001424 /*
1425 * The supported versions extension was parsed successfully as the
1426 * value returned by ssl_tls13_parse_supported_versions_ext() is
1427 * positive. The return value is then equal to
1428 * MBEDTLS_SSL_VERSION_TLS1_2 or MBEDTLS_SSL_VERSION_TLS1_3, defining
1429 * the TLS version to negotiate.
1430 */
Ronald Cron5af4c7f2023-03-07 20:46:59 +01001431 if (MBEDTLS_SSL_VERSION_TLS1_2 == ret) {
1432 return SSL_CLIENT_HELLO_TLS1_2;
1433 }
Ronald Cron8c527d02023-03-07 15:47:47 +01001434 }
1435
1436 /*
1437 * We negotiate TLS 1.3.
Ronald Cron64582392023-03-07 09:21:40 +01001438 */
1439 ssl->tls_version = MBEDTLS_SSL_VERSION_TLS1_3;
Ronald Cron64582392023-03-07 09:21:40 +01001440 ssl->session_negotiate->tls_version = MBEDTLS_SSL_VERSION_TLS1_3;
1441 ssl->session_negotiate->endpoint = ssl->conf->endpoint;
Ronald Cron64582392023-03-07 09:21:40 +01001442
1443 /*
Ronald Crondad02b22023-04-06 09:57:52 +02001444 * We are negotiating the version 1.3 of the protocol. Do what we have
Ronald Croncada4102023-03-07 09:51:39 +01001445 * postponed: copy of the client random bytes, copy of the legacy session
Ronald Cron2f16b4e2023-03-07 10:07:32 +01001446 * identifier and selection of the TLS 1.3 cipher suite.
Ronald Crond540d992023-03-07 09:41:48 +01001447 */
1448 MBEDTLS_SSL_DEBUG_BUF(3, "client hello, random bytes",
1449 random, MBEDTLS_CLIENT_HELLO_RANDOM_LEN);
1450 memcpy(&handshake->randbytes[0], random, MBEDTLS_CLIENT_HELLO_RANDOM_LEN);
1451
Ronald Croncada4102023-03-07 09:51:39 +01001452 if (legacy_session_id_len > sizeof(ssl->session_negotiate->id)) {
1453 MBEDTLS_SSL_DEBUG_MSG(1, ("bad client hello message"));
1454 return MBEDTLS_ERR_SSL_DECODE_ERROR;
1455 }
1456 ssl->session_negotiate->id_len = legacy_session_id_len;
1457 MBEDTLS_SSL_DEBUG_BUF(3, "client hello, session id",
1458 legacy_session_id, legacy_session_id_len);
1459 memcpy(&ssl->session_negotiate->id[0],
1460 legacy_session_id, legacy_session_id_len);
1461
Ronald Crond540d992023-03-07 09:41:48 +01001462 /*
Jerry Yu5725f1c2022-08-21 17:27:16 +08001463 * Search for a matching ciphersuite
1464 */
Ronald Cron2f16b4e2023-03-07 10:07:32 +01001465 MBEDTLS_SSL_DEBUG_BUF(3, "client hello, list of cipher suites",
1466 cipher_suites, cipher_suites_len);
Ronald Crone45afd72023-04-04 15:10:06 +02001467 for (const unsigned char *cipher_suites_p = cipher_suites;
1468 cipher_suites_p < cipher_suites_end; cipher_suites_p += 2) {
XiaokangQiane8ff3502022-04-22 02:34:40 +00001469 uint16_t cipher_suite;
Gilles Peskine449bd832023-01-11 14:50:10 +01001470 const mbedtls_ssl_ciphersuite_t *ciphersuite_info;
Jerry Yu5725f1c2022-08-21 17:27:16 +08001471
Ronald Crond89360b2023-02-21 08:53:33 +01001472 /*
Ronald Crone45afd72023-04-04 15:10:06 +02001473 * "cipher_suites_end - cipher_suites_p is even" is an invariant of the
1474 * loop. As cipher_suites_end - cipher_suites_p > 0, we have
1475 * cipher_suites_end - cipher_suites_p >= 2 and it is thus safe to read
1476 * two bytes.
Ronald Crond89360b2023-02-21 08:53:33 +01001477 */
Ronald Crone45afd72023-04-04 15:10:06 +02001478 cipher_suite = MBEDTLS_GET_UINT16_BE(cipher_suites_p, 0);
Jerry Yuc5a23a02022-08-25 10:51:44 +08001479 ciphersuite_info = ssl_tls13_validate_peer_ciphersuite(
Gilles Peskine449bd832023-01-11 14:50:10 +01001480 ssl, cipher_suite);
1481 if (ciphersuite_info == NULL) {
Jerry Yu5725f1c2022-08-21 17:27:16 +08001482 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +01001483 }
Jerry Yu5725f1c2022-08-21 17:27:16 +08001484
Jerry Yu5725f1c2022-08-21 17:27:16 +08001485 ssl->session_negotiate->ciphersuite = cipher_suite;
Jerry Yuc4bf5d62022-10-29 09:08:47 +08001486 handshake->ciphersuite_info = ciphersuite_info;
Gilles Peskine449bd832023-01-11 14:50:10 +01001487 MBEDTLS_SSL_DEBUG_MSG(2, ("selected ciphersuite: %04x - %s",
1488 cipher_suite,
1489 ciphersuite_info->name));
Ronald Cron25e9ec62023-02-16 15:35:16 +01001490 break;
XiaokangQian17f974c2022-04-19 09:57:41 +00001491 }
Jerry Yu5725f1c2022-08-21 17:27:16 +08001492
Gilles Peskine449bd832023-01-11 14:50:10 +01001493 if (handshake->ciphersuite_info == NULL) {
1494 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE,
1495 MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE);
1496 return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
Jerry Yu5725f1c2022-08-21 17:27:16 +08001497 }
Jerry Yuc1be19f2022-04-23 16:11:39 +08001498
XiaokangQian4080a7f2022-04-11 09:55:18 +00001499 /* ...
XiaokangQianb67384d2022-04-19 00:02:38 +00001500 * opaque legacy_compression_methods<1..2^8-1>;
XiaokangQian4080a7f2022-04-11 09:55:18 +00001501 * ...
XiaokangQian7807f9f2022-02-15 10:04:37 +00001502 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001503 if (p[0] != 1 || p[1] != MBEDTLS_SSL_COMPRESS_NULL) {
1504 MBEDTLS_SSL_DEBUG_MSG(1, ("bad legacy compression method"));
1505 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER,
1506 MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER);
1507 return MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001508 }
XiaokangQianb67384d2022-04-19 00:02:38 +00001509 p += 2;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001510
Jerry Yuc1be19f2022-04-23 16:11:39 +08001511 /* ...
XiaokangQianb67384d2022-04-19 00:02:38 +00001512 * Extension extensions<8..2^16-1>;
Jerry Yuc1be19f2022-04-23 16:11:39 +08001513 * ...
XiaokangQianb67384d2022-04-19 00:02:38 +00001514 * with Extension defined as:
1515 * struct {
1516 * ExtensionType extension_type;
1517 * opaque extension_data<0..2^16-1>;
1518 * } Extension;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001519 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001520 extensions_len = MBEDTLS_GET_UINT16_BE(p, 0);
XiaokangQian7807f9f2022-02-15 10:04:37 +00001521 p += 2;
Gilles Peskine449bd832023-01-11 14:50:10 +01001522 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, extensions_len);
XiaokangQiane8ff3502022-04-22 02:34:40 +00001523 extensions_end = p + extensions_len;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001524
Gilles Peskine449bd832023-01-11 14:50:10 +01001525 MBEDTLS_SSL_DEBUG_BUF(3, "client hello extensions", p, extensions_len);
Jerry Yu63a459c2022-10-31 13:38:40 +08001526 handshake->received_extensions = MBEDTLS_SSL_EXT_MASK_NONE;
Jerry Yu0c354a22022-08-29 15:25:36 +08001527
Gilles Peskine449bd832023-01-11 14:50:10 +01001528 while (p < extensions_end) {
XiaokangQian7807f9f2022-02-15 10:04:37 +00001529 unsigned int extension_type;
1530 size_t extension_data_len;
1531 const unsigned char *extension_data_end;
Jerry Yu263dbf72022-10-26 10:51:27 +08001532 uint32_t allowed_exts = MBEDTLS_SSL_TLS1_3_ALLOWED_EXTS_OF_CH;
1533
Ronald Cron5fbd2702024-02-14 10:03:36 +01001534 if (ssl->handshake->hello_retry_request_flag) {
Jerry Yu263dbf72022-10-26 10:51:27 +08001535 /* Do not accept early data extension in 2nd ClientHello */
1536 allowed_exts &= ~MBEDTLS_SSL_EXT_MASK(EARLY_DATA);
1537 }
XiaokangQian7807f9f2022-02-15 10:04:37 +00001538
Jerry Yu0c354a22022-08-29 15:25:36 +08001539 /* RFC 8446, section 4.2.11
Jerry Yu1c9247c2022-07-21 12:37:39 +08001540 *
1541 * The "pre_shared_key" extension MUST be the last extension in the
1542 * ClientHello (this facilitates implementation as described below).
1543 * Servers MUST check that it is the last extension and otherwise fail
1544 * the handshake with an "illegal_parameter" alert.
1545 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001546 if (handshake->received_extensions & MBEDTLS_SSL_EXT_MASK(PRE_SHARED_KEY)) {
Jerry Yu1c9247c2022-07-21 12:37:39 +08001547 MBEDTLS_SSL_DEBUG_MSG(
Gilles Peskine449bd832023-01-11 14:50:10 +01001548 3, ("pre_shared_key is not last extension."));
Jerry Yu1c9247c2022-07-21 12:37:39 +08001549 MBEDTLS_SSL_PEND_FATAL_ALERT(
1550 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER,
Gilles Peskine449bd832023-01-11 14:50:10 +01001551 MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER);
1552 return MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER;
Jerry Yu1c9247c2022-07-21 12:37:39 +08001553 }
1554
Gilles Peskine449bd832023-01-11 14:50:10 +01001555 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, extensions_end, 4);
1556 extension_type = MBEDTLS_GET_UINT16_BE(p, 0);
1557 extension_data_len = MBEDTLS_GET_UINT16_BE(p, 2);
XiaokangQian7807f9f2022-02-15 10:04:37 +00001558 p += 4;
1559
Gilles Peskine449bd832023-01-11 14:50:10 +01001560 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, extensions_end, extension_data_len);
XiaokangQian7807f9f2022-02-15 10:04:37 +00001561 extension_data_end = p + extension_data_len;
1562
Jerry Yuc4bf5d62022-10-29 09:08:47 +08001563 ret = mbedtls_ssl_tls13_check_received_extension(
Gilles Peskine449bd832023-01-11 14:50:10 +01001564 ssl, MBEDTLS_SSL_HS_CLIENT_HELLO, extension_type,
Jerry Yu263dbf72022-10-26 10:51:27 +08001565 allowed_exts);
Gilles Peskine449bd832023-01-11 14:50:10 +01001566 if (ret != 0) {
1567 return ret;
1568 }
Jerry Yue18dc7e2022-08-04 16:29:22 +08001569
Gilles Peskine449bd832023-01-11 14:50:10 +01001570 switch (extension_type) {
XiaokangQian40a35232022-05-07 09:02:40 +00001571#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
1572 case MBEDTLS_TLS_EXT_SERVERNAME:
Gilles Peskine449bd832023-01-11 14:50:10 +01001573 MBEDTLS_SSL_DEBUG_MSG(3, ("found ServerName extension"));
1574 ret = mbedtls_ssl_parse_server_name_ext(ssl, p,
1575 extension_data_end);
1576 if (ret != 0) {
XiaokangQian40a35232022-05-07 09:02:40 +00001577 MBEDTLS_SSL_DEBUG_RET(
Gilles Peskine449bd832023-01-11 14:50:10 +01001578 1, "mbedtls_ssl_parse_servername_ext", ret);
1579 return ret;
XiaokangQian40a35232022-05-07 09:02:40 +00001580 }
XiaokangQian40a35232022-05-07 09:02:40 +00001581 break;
1582#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
1583
Przemek Stekiel8c0a9532023-06-15 16:48:19 +02001584#if defined(PSA_WANT_ALG_ECDH) || defined(PSA_WANT_ALG_FFDH)
XiaokangQian7807f9f2022-02-15 10:04:37 +00001585 case MBEDTLS_TLS_EXT_SUPPORTED_GROUPS:
Gilles Peskine449bd832023-01-11 14:50:10 +01001586 MBEDTLS_SSL_DEBUG_MSG(3, ("found supported group extension"));
XiaokangQian7807f9f2022-02-15 10:04:37 +00001587
1588 /* Supported Groups Extension
1589 *
1590 * When sent by the client, the "supported_groups" extension
1591 * indicates the named groups which the client supports,
1592 * ordered from most preferred to least preferred.
1593 */
Jerry Yuc1be19f2022-04-23 16:11:39 +08001594 ret = ssl_tls13_parse_supported_groups_ext(
Gilles Peskine449bd832023-01-11 14:50:10 +01001595 ssl, p, extension_data_end);
1596 if (ret != 0) {
Xiaokang Qian9f1747b2023-03-30 02:50:04 +00001597 MBEDTLS_SSL_DEBUG_RET(
Xiaokang Qian8bce0e62023-04-04 10:15:48 +00001598 1, "ssl_tls13_parse_supported_groups_ext", ret);
Gilles Peskine449bd832023-01-11 14:50:10 +01001599 return ret;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001600 }
1601
XiaokangQian7807f9f2022-02-15 10:04:37 +00001602 break;
Przemek Stekiel8c0a9532023-06-15 16:48:19 +02001603#endif /* PSA_WANT_ALG_ECDH || PSA_WANT_ALG_FFDH*/
XiaokangQian7807f9f2022-02-15 10:04:37 +00001604
Valerio Settic9ae8622023-07-25 11:23:50 +02001605#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_EPHEMERAL_ENABLED)
XiaokangQian7807f9f2022-02-15 10:04:37 +00001606 case MBEDTLS_TLS_EXT_KEY_SHARE:
Gilles Peskine449bd832023-01-11 14:50:10 +01001607 MBEDTLS_SSL_DEBUG_MSG(3, ("found key share extension"));
XiaokangQian7807f9f2022-02-15 10:04:37 +00001608
1609 /*
1610 * Key Share Extension
1611 *
1612 * When sent by the client, the "key_share" extension
1613 * contains the endpoint's cryptographic parameters for
1614 * ECDHE/DHE key establishment methods.
1615 */
Jerry Yuc1be19f2022-04-23 16:11:39 +08001616 ret = ssl_tls13_parse_key_shares_ext(
Gilles Peskine449bd832023-01-11 14:50:10 +01001617 ssl, p, extension_data_end);
1618 if (ret == SSL_TLS1_3_PARSE_KEY_SHARES_EXT_NO_MATCH) {
Ronald Cron8a74f072023-06-14 17:59:29 +02001619 MBEDTLS_SSL_DEBUG_MSG(2, ("No usable share for key agreement."));
1620 no_usable_share_for_key_agreement = 1;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001621 }
1622
Gilles Peskine449bd832023-01-11 14:50:10 +01001623 if (ret < 0) {
Jerry Yu582dd062022-04-22 21:59:01 +08001624 MBEDTLS_SSL_DEBUG_RET(
Gilles Peskine449bd832023-01-11 14:50:10 +01001625 1, "ssl_tls13_parse_key_shares_ext", ret);
1626 return ret;
Jerry Yu582dd062022-04-22 21:59:01 +08001627 }
XiaokangQian7807f9f2022-02-15 10:04:37 +00001628
XiaokangQian7807f9f2022-02-15 10:04:37 +00001629 break;
Valerio Settic9ae8622023-07-25 11:23:50 +02001630#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_EPHEMERAL_ENABLED */
XiaokangQian7807f9f2022-02-15 10:04:37 +00001631
1632 case MBEDTLS_TLS_EXT_SUPPORTED_VERSIONS:
Ronald Cron8c527d02023-03-07 15:47:47 +01001633 /* Already parsed */
XiaokangQian7807f9f2022-02-15 10:04:37 +00001634 break;
1635
Ronald Cron41a443a2022-10-04 16:38:25 +02001636#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED)
Jerry Yue19e3b92022-07-08 12:04:51 +00001637 case MBEDTLS_TLS_EXT_PSK_KEY_EXCHANGE_MODES:
Xiaokang Qian9f1747b2023-03-30 02:50:04 +00001638 MBEDTLS_SSL_DEBUG_MSG(
1639 3, ("found psk key exchange modes extension"));
Jerry Yue19e3b92022-07-08 12:04:51 +00001640
1641 ret = ssl_tls13_parse_key_exchange_modes_ext(
Gilles Peskine449bd832023-01-11 14:50:10 +01001642 ssl, p, extension_data_end);
1643 if (ret != 0) {
Jerry Yue19e3b92022-07-08 12:04:51 +00001644 MBEDTLS_SSL_DEBUG_RET(
Gilles Peskine449bd832023-01-11 14:50:10 +01001645 1, "ssl_tls13_parse_key_exchange_modes_ext", ret);
1646 return ret;
Jerry Yue19e3b92022-07-08 12:04:51 +00001647 }
1648
Jerry Yue19e3b92022-07-08 12:04:51 +00001649 break;
Ronald Cron41a443a2022-10-04 16:38:25 +02001650#endif
Jerry Yue19e3b92022-07-08 12:04:51 +00001651
Jerry Yu1c105562022-07-10 06:32:38 +00001652 case MBEDTLS_TLS_EXT_PRE_SHARED_KEY:
Gilles Peskine449bd832023-01-11 14:50:10 +01001653 MBEDTLS_SSL_DEBUG_MSG(3, ("found pre_shared_key extension"));
1654 if ((handshake->received_extensions &
1655 MBEDTLS_SSL_EXT_MASK(PSK_KEY_EXCHANGE_MODES)) == 0) {
Jerry Yu13ab81d2022-07-22 23:17:11 +08001656 MBEDTLS_SSL_PEND_FATAL_ALERT(
1657 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER,
Gilles Peskine449bd832023-01-11 14:50:10 +01001658 MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER);
1659 return MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER;
Jerry Yu13ab81d2022-07-22 23:17:11 +08001660 }
Ronald Cron41a443a2022-10-04 16:38:25 +02001661#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED)
Jerry Yu1c105562022-07-10 06:32:38 +00001662 /* Delay processing of the PSK identity once we have
1663 * found out which algorithms to use. We keep a pointer
1664 * to the buffer and the size for later processing.
1665 */
Jerry Yu29d9faa2022-08-23 17:52:45 +08001666 pre_shared_key_ext = p;
Jerry Yu1c105562022-07-10 06:32:38 +00001667 pre_shared_key_ext_end = extension_data_end;
Jerry Yue18dc7e2022-08-04 16:29:22 +08001668#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED */
Jerry Yu1c105562022-07-10 06:32:38 +00001669 break;
Jerry Yu1c105562022-07-10 06:32:38 +00001670
XiaokangQianacb39922022-06-17 10:18:48 +00001671#if defined(MBEDTLS_SSL_ALPN)
1672 case MBEDTLS_TLS_EXT_ALPN:
Gilles Peskine449bd832023-01-11 14:50:10 +01001673 MBEDTLS_SSL_DEBUG_MSG(3, ("found alpn extension"));
XiaokangQianacb39922022-06-17 10:18:48 +00001674
Gilles Peskine449bd832023-01-11 14:50:10 +01001675 ret = mbedtls_ssl_parse_alpn_ext(ssl, p, extension_data_end);
1676 if (ret != 0) {
XiaokangQianacb39922022-06-17 10:18:48 +00001677 MBEDTLS_SSL_DEBUG_RET(
Gilles Peskine449bd832023-01-11 14:50:10 +01001678 1, ("mbedtls_ssl_parse_alpn_ext"), ret);
1679 return ret;
XiaokangQianacb39922022-06-17 10:18:48 +00001680 }
XiaokangQianacb39922022-06-17 10:18:48 +00001681 break;
1682#endif /* MBEDTLS_SSL_ALPN */
1683
Ronald Cron928cbd32022-10-04 16:14:26 +02001684#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED)
XiaokangQian7807f9f2022-02-15 10:04:37 +00001685 case MBEDTLS_TLS_EXT_SIG_ALG:
Gilles Peskine449bd832023-01-11 14:50:10 +01001686 MBEDTLS_SSL_DEBUG_MSG(3, ("found signature_algorithms extension"));
XiaokangQian7807f9f2022-02-15 10:04:37 +00001687
Gabor Mezei078e8032022-04-27 21:17:56 +02001688 ret = mbedtls_ssl_parse_sig_alg_ext(
Gilles Peskine449bd832023-01-11 14:50:10 +01001689 ssl, p, extension_data_end);
1690 if (ret != 0) {
Xiaokang Qian49f39c12023-04-06 02:31:02 +00001691 MBEDTLS_SSL_DEBUG_RET(
1692 1, "mbedtls_ssl_parse_sig_alg_ext", ret);
Gilles Peskine449bd832023-01-11 14:50:10 +01001693 return ret;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001694 }
XiaokangQian7807f9f2022-02-15 10:04:37 +00001695 break;
Ronald Cron928cbd32022-10-04 16:14:26 +02001696#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED */
XiaokangQian7807f9f2022-02-15 10:04:37 +00001697
Jan Bruckner151f6422023-02-10 12:45:19 +01001698#if defined(MBEDTLS_SSL_RECORD_SIZE_LIMIT)
1699 case MBEDTLS_TLS_EXT_RECORD_SIZE_LIMIT:
1700 MBEDTLS_SSL_DEBUG_MSG(3, ("found record_size_limit extension"));
1701
Xiaokang Qian9f1747b2023-03-30 02:50:04 +00001702 ret = mbedtls_ssl_tls13_parse_record_size_limit_ext(
1703 ssl, p, extension_data_end);
Jan Brucknerf482dcc2023-03-15 09:09:06 +01001704 if (ret != 0) {
1705 MBEDTLS_SSL_DEBUG_RET(
1706 1, ("mbedtls_ssl_tls13_parse_record_size_limit_ext"), ret);
1707 return ret;
1708 }
Jan Bruckner151f6422023-02-10 12:45:19 +01001709 break;
1710#endif /* MBEDTLS_SSL_RECORD_SIZE_LIMIT */
1711
XiaokangQian7807f9f2022-02-15 10:04:37 +00001712 default:
Jerry Yu79aa7212022-11-08 21:30:21 +08001713 MBEDTLS_SSL_PRINT_EXT(
Jerry Yu63a459c2022-10-31 13:38:40 +08001714 3, MBEDTLS_SSL_HS_CLIENT_HELLO,
Gilles Peskine449bd832023-01-11 14:50:10 +01001715 extension_type, "( ignored )");
Jerry Yu0c354a22022-08-29 15:25:36 +08001716 break;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001717 }
1718
1719 p += extension_data_len;
1720 }
1721
Gilles Peskine449bd832023-01-11 14:50:10 +01001722 MBEDTLS_SSL_PRINT_EXTS(3, MBEDTLS_SSL_HS_CLIENT_HELLO,
1723 handshake->received_extensions);
Jerry Yue18dc7e2022-08-04 16:29:22 +08001724
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +01001725 ret = mbedtls_ssl_add_hs_hdr_to_checksum(ssl,
1726 MBEDTLS_SSL_HS_CLIENT_HELLO,
1727 p - buf);
1728 if (0 != ret) {
1729 MBEDTLS_SSL_DEBUG_RET(1, ("mbedtls_ssl_add_hs_hdr_to_checksum"), ret);
1730 return ret;
1731 }
Jerry Yu032b15ce2022-07-11 06:10:03 +00001732
Ronald Cron41a443a2022-10-04 16:38:25 +02001733#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED)
XiaokangQian7807f9f2022-02-15 10:04:37 +00001734 /* Update checksum with either
1735 * - The entire content of the CH message, if no PSK extension is present
1736 * - The content up to but excluding the PSK extension, if present.
1737 */
Jerry Yu1c105562022-07-10 06:32:38 +00001738 /* If we've settled on a PSK-based exchange, parse PSK identity ext */
Pengyu Lvbc4aab72023-12-01 15:37:24 +08001739 if (ssl_tls13_key_exchange_is_psk_available(ssl) ||
1740 ssl_tls13_key_exchange_is_psk_ephemeral_available(ssl)) {
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +01001741 ret = handshake->update_checksum(ssl, buf,
1742 pre_shared_key_ext - buf);
1743 if (0 != ret) {
1744 MBEDTLS_SSL_DEBUG_RET(1, ("update_checksum"), ret);
1745 return ret;
1746 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001747 ret = ssl_tls13_parse_pre_shared_key_ext(ssl,
1748 pre_shared_key_ext,
1749 pre_shared_key_ext_end,
1750 cipher_suites,
1751 cipher_suites_end);
1752 if (ret == MBEDTLS_ERR_SSL_UNKNOWN_IDENTITY) {
1753 handshake->received_extensions &= ~MBEDTLS_SSL_EXT_MASK(PRE_SHARED_KEY);
1754 } else if (ret != 0) {
Jerry Yu63a459c2022-10-31 13:38:40 +08001755 MBEDTLS_SSL_DEBUG_RET(
Gilles Peskine449bd832023-01-11 14:50:10 +01001756 1, "ssl_tls13_parse_pre_shared_key_ext", ret);
1757 return ret;
Jerry Yu1c105562022-07-10 06:32:38 +00001758 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001759 } else
Ronald Cron41a443a2022-10-04 16:38:25 +02001760#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED */
Jerry Yu1c105562022-07-10 06:32:38 +00001761 {
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +01001762 ret = handshake->update_checksum(ssl, buf, p - buf);
1763 if (0 != ret) {
1764 MBEDTLS_SSL_DEBUG_RET(1, ("update_checksum"), ret);
1765 return ret;
1766 }
Jerry Yu1c105562022-07-10 06:32:38 +00001767 }
XiaokangQian7807f9f2022-02-15 10:04:37 +00001768
Gilles Peskine449bd832023-01-11 14:50:10 +01001769 ret = ssl_tls13_determine_key_exchange_mode(ssl);
1770 if (ret < 0) {
1771 return ret;
1772 }
XiaokangQian7807f9f2022-02-15 10:04:37 +00001773
Ronald Cron8a74f072023-06-14 17:59:29 +02001774 if (ssl->handshake->key_exchange_mode !=
1775 MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK) {
1776 hrr_required = (no_usable_share_for_key_agreement != 0);
1777 }
1778
Gilles Peskine449bd832023-01-11 14:50:10 +01001779 mbedtls_ssl_optimize_checksum(ssl, handshake->ciphersuite_info);
Jerry Yue5834fd2022-08-29 20:16:09 +08001780
Gilles Peskine449bd832023-01-11 14:50:10 +01001781 return hrr_required ? SSL_CLIENT_HELLO_HRR_REQUIRED : SSL_CLIENT_HELLO_OK;
XiaokangQian75fe8c72022-06-15 09:42:45 +00001782}
1783
Jerry Yuab0da372023-02-08 13:55:24 +08001784#if defined(MBEDTLS_SSL_EARLY_DATA)
Jerry Yu4caf3ca2023-11-15 16:13:47 +08001785static int ssl_tls13_check_early_data_requirements(mbedtls_ssl_context *ssl)
Jerry Yuab0da372023-02-08 13:55:24 +08001786{
1787 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
1788
Jerry Yu985c9672022-12-04 14:06:30 +08001789 if (ssl->conf->early_data_enabled == MBEDTLS_SSL_EARLY_DATA_DISABLED) {
1790 MBEDTLS_SSL_DEBUG_MSG(
Jerry Yu985c9672022-12-04 14:06:30 +08001791 1,
Jerry Yu454dda32023-10-31 15:13:54 +08001792 ("EarlyData: rejected, feature disabled in server configuration."));
Jerry Yu4caf3ca2023-11-15 16:13:47 +08001793 return -1;
Ronald Cron7b6ee942024-01-12 10:29:55 +01001794 }
1795
Jerry Yu454dda32023-10-31 15:13:54 +08001796 if (!handshake->resume) {
1797 /* We currently support early data only in the case of PSKs established
1798 via a NewSessionTicket message thus in the case of a session
1799 resumption. */
Jerry Yu985c9672022-12-04 14:06:30 +08001800 MBEDTLS_SSL_DEBUG_MSG(
Jerry Yu7ef9fd82023-11-07 14:30:38 +08001801 1, ("EarlyData: rejected, not a session resumption."));
Jerry Yu4caf3ca2023-11-15 16:13:47 +08001802 return -1;
Jerry Yu985c9672022-12-04 14:06:30 +08001803 }
1804
Jerry Yu82fd6c12023-10-31 16:32:19 +08001805 /* RFC 8446 4.2.10
1806 *
1807 * In order to accept early data, the server MUST have accepted a PSK cipher
1808 * suite and selected the first key offered in the client's "pre_shared_key"
1809 * extension. In addition, it MUST verify that the following values are the
1810 * same as those associated with the selected PSK:
1811 * - The TLS version number
1812 * - The selected cipher suite
1813 * - The selected ALPN [RFC7301] protocol, if any
1814 *
1815 * NOTE:
Jerry Yu7ef9fd82023-11-07 14:30:38 +08001816 * - The TLS version number is checked in
1817 * ssl_tls13_offered_psks_check_identity_match_ticket().
1818 * - ALPN is not checked for the time being (TODO).
Jerry Yu82fd6c12023-10-31 16:32:19 +08001819 */
1820
1821 if (handshake->selected_identity != 0) {
1822 MBEDTLS_SSL_DEBUG_MSG(
Jerry Yu7ef9fd82023-11-07 14:30:38 +08001823 1, ("EarlyData: rejected, the selected key in "
1824 "`pre_shared_key` is not the first one."));
Jerry Yu4caf3ca2023-11-15 16:13:47 +08001825 return -1;
Jerry Yu82fd6c12023-10-31 16:32:19 +08001826 }
1827
1828 if (handshake->ciphersuite_info->id !=
1829 ssl->session_negotiate->ciphersuite) {
1830 MBEDTLS_SSL_DEBUG_MSG(
Jerry Yu7ef9fd82023-11-07 14:30:38 +08001831 1, ("EarlyData: rejected, the selected ciphersuite is not the one "
1832 "of the selected pre-shared key."));
Jerry Yu4caf3ca2023-11-15 16:13:47 +08001833 return -1;
Jerry Yu82fd6c12023-10-31 16:32:19 +08001834
1835 }
Jerry Yu985c9672022-12-04 14:06:30 +08001836
Pengyu Lv94a42cc2023-12-06 10:04:17 +08001837 if (!mbedtls_ssl_tls13_session_ticket_allow_early_data(ssl->session_negotiate)) {
Jerry Yufceddb32022-12-12 15:30:34 +08001838 MBEDTLS_SSL_DEBUG_MSG(
1839 1,
Jerry Yuea96ac32023-11-21 17:06:36 +08001840 ("EarlyData: rejected, early_data not allowed in ticket "
1841 "permission bits."));
Jerry Yu4caf3ca2023-11-15 16:13:47 +08001842 return -1;
Jerry Yufceddb32022-12-12 15:30:34 +08001843 }
Jerry Yu985c9672022-12-04 14:06:30 +08001844
Jerry Yu4caf3ca2023-11-15 16:13:47 +08001845 return 0;
Jerry Yuab0da372023-02-08 13:55:24 +08001846}
1847#endif /* MBEDTLS_SSL_EARLY_DATA */
1848
XiaokangQian75fe8c72022-06-15 09:42:45 +00001849/* Update the handshake state machine */
1850
Ronald Cronce7d76e2022-07-08 18:56:49 +02001851MBEDTLS_CHECK_RETURN_CRITICAL
Ronald Cron7b6ee942024-01-12 10:29:55 +01001852static int ssl_tls13_postprocess_client_hello(mbedtls_ssl_context *ssl,
1853 int hrr_required)
XiaokangQian75fe8c72022-06-15 09:42:45 +00001854{
1855 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1856
XiaokangQian7807f9f2022-02-15 10:04:37 +00001857 /*
XiaokangQianfb665a82022-06-15 03:57:21 +00001858 * Server certificate selection
1859 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001860 if (ssl->conf->f_cert_cb && (ret = ssl->conf->f_cert_cb(ssl)) != 0) {
1861 MBEDTLS_SSL_DEBUG_RET(1, "f_cert_cb", ret);
1862 return ret;
XiaokangQianfb665a82022-06-15 03:57:21 +00001863 }
1864#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
1865 ssl->handshake->sni_name = NULL;
1866 ssl->handshake->sni_name_len = 0;
1867#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
XiaokangQian7807f9f2022-02-15 10:04:37 +00001868
Gilles Peskine449bd832023-01-11 14:50:10 +01001869 ret = mbedtls_ssl_tls13_key_schedule_stage_early(ssl);
1870 if (ret != 0) {
1871 MBEDTLS_SSL_DEBUG_RET(1,
1872 "mbedtls_ssl_tls1_3_key_schedule_stage_early", ret);
1873 return ret;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001874 }
1875
Jerry Yuab0da372023-02-08 13:55:24 +08001876#if defined(MBEDTLS_SSL_EARLY_DATA)
Jerry Yu4caf3ca2023-11-15 16:13:47 +08001877 if (ssl->handshake->received_extensions & MBEDTLS_SSL_EXT_MASK(EARLY_DATA)) {
Ronald Cron31e2d832024-02-05 16:45:57 +01001878 ssl->handshake->early_data_accepted =
1879 (!hrr_required) && (ssl_tls13_check_early_data_requirements(ssl) == 0);
Jerry Yu4e9b70e2022-12-04 14:08:02 +08001880
Jerry Yu4caf3ca2023-11-15 16:13:47 +08001881 if (ssl->handshake->early_data_accepted) {
1882 ret = mbedtls_ssl_tls13_compute_early_transform(ssl);
1883 if (ret != 0) {
1884 MBEDTLS_SSL_DEBUG_RET(
1885 1, "mbedtls_ssl_tls13_compute_early_transform", ret);
1886 return ret;
1887 }
1888 } else {
1889 ssl->discard_early_data_record =
1890 hrr_required ?
1891 MBEDTLS_SSL_EARLY_DATA_DISCARD :
1892 MBEDTLS_SSL_EARLY_DATA_TRY_TO_DEPROTECT_AND_DISCARD;
Jerry Yu4e9b70e2022-12-04 14:08:02 +08001893 }
1894 }
Ronald Cron7b6ee942024-01-12 10:29:55 +01001895#else
1896 ((void) hrr_required);
Jerry Yuab0da372023-02-08 13:55:24 +08001897#endif /* MBEDTLS_SSL_EARLY_DATA */
1898
Gilles Peskine449bd832023-01-11 14:50:10 +01001899 return 0;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001900}
1901
1902/*
XiaokangQianed582dd2022-04-13 08:21:05 +00001903 * Main entry point from the state machine; orchestrates the otherfunctions.
1904 */
1905
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001906MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01001907static int ssl_tls13_process_client_hello(mbedtls_ssl_context *ssl)
XiaokangQianed582dd2022-04-13 08:21:05 +00001908{
1909
XiaokangQian08037552022-04-20 07:16:41 +00001910 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine449bd832023-01-11 14:50:10 +01001911 unsigned char *buf = NULL;
XiaokangQianed582dd2022-04-13 08:21:05 +00001912 size_t buflen = 0;
Jerry Yu4ca91402022-05-09 15:50:57 +08001913 int parse_client_hello_ret;
1914
Gilles Peskine449bd832023-01-11 14:50:10 +01001915 MBEDTLS_SSL_DEBUG_MSG(2, ("=> parse client hello"));
XiaokangQianed582dd2022-04-13 08:21:05 +00001916
Gilles Peskine449bd832023-01-11 14:50:10 +01001917 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_tls13_fetch_handshake_msg(
1918 ssl, MBEDTLS_SSL_HS_CLIENT_HELLO,
1919 &buf, &buflen));
XiaokangQianed582dd2022-04-13 08:21:05 +00001920
Gilles Peskine449bd832023-01-11 14:50:10 +01001921 MBEDTLS_SSL_PROC_CHK_NEG(ssl_tls13_parse_client_hello(ssl, buf,
1922 buf + buflen));
Ronald Cron5af4c7f2023-03-07 20:46:59 +01001923 parse_client_hello_ret = ret; /* Store positive return value of
1924 * parse_client_hello,
1925 * as negative error codes are handled
Jerry Yuf41553b2022-05-09 22:20:30 +08001926 * by MBEDTLS_SSL_PROC_CHK_NEG. */
Jerry Yu4ca91402022-05-09 15:50:57 +08001927
Ronald Cronb828c7d2023-04-03 16:37:22 +02001928 /*
Yanray Wang90acdc62023-12-08 10:29:42 +08001929 * Version 1.2 of the protocol has to be used for the handshake.
1930 * If TLS 1.2 is not supported, abort the handshake. Otherwise, set the
Ronald Cronb828c7d2023-04-03 16:37:22 +02001931 * ssl->keep_current_message flag for the ClientHello to be kept and parsed
1932 * as a TLS 1.2 ClientHello. We also change ssl->tls_version to
1933 * MBEDTLS_SSL_VERSION_TLS1_2 thus from now on mbedtls_ssl_handshake_step()
1934 * will dispatch to the TLS 1.2 state machine.
1935 */
Ronald Cron5af4c7f2023-03-07 20:46:59 +01001936 if (SSL_CLIENT_HELLO_TLS1_2 == parse_client_hello_ret) {
Yanray Wangfb0f47b2023-12-04 15:27:28 +08001937 /* Check if server supports TLS 1.2 */
Yanray Wang408ba6f2023-12-08 10:18:03 +08001938 if (!mbedtls_ssl_conf_is_tls12_enabled(ssl->conf)) {
Yanray Wangfb0f47b2023-12-04 15:27:28 +08001939 MBEDTLS_SSL_DEBUG_MSG(
Yanray Wang177e49a2023-12-08 10:51:04 +08001940 1, ("TLS 1.2 not supported."));
Yanray Wangfb0f47b2023-12-04 15:27:28 +08001941 MBEDTLS_SSL_PEND_FATAL_ALERT(
Yanray Wang2bef9172023-12-08 10:21:53 +08001942 MBEDTLS_SSL_ALERT_MSG_PROTOCOL_VERSION,
1943 MBEDTLS_ERR_SSL_BAD_PROTOCOL_VERSION);
1944 return MBEDTLS_ERR_SSL_BAD_PROTOCOL_VERSION;
Yanray Wangfb0f47b2023-12-04 15:27:28 +08001945 }
Ronald Cron5af4c7f2023-03-07 20:46:59 +01001946 ssl->keep_current_message = 1;
1947 ssl->tls_version = MBEDTLS_SSL_VERSION_TLS1_2;
1948 return 0;
1949 }
1950
Ronald Cron7b6ee942024-01-12 10:29:55 +01001951 MBEDTLS_SSL_PROC_CHK(
1952 ssl_tls13_postprocess_client_hello(ssl, parse_client_hello_ret ==
1953 SSL_CLIENT_HELLO_HRR_REQUIRED));
Jerry Yu582dd062022-04-22 21:59:01 +08001954
Ronald Cron5af4c7f2023-03-07 20:46:59 +01001955 if (SSL_CLIENT_HELLO_OK == parse_client_hello_ret) {
Gilles Peskine449bd832023-01-11 14:50:10 +01001956 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_SERVER_HELLO);
1957 } else {
1958 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_HELLO_RETRY_REQUEST);
1959 }
XiaokangQianed582dd2022-04-13 08:21:05 +00001960
1961cleanup:
1962
Gilles Peskine449bd832023-01-11 14:50:10 +01001963 MBEDTLS_SSL_DEBUG_MSG(2, ("<= parse client hello"));
1964 return ret;
XiaokangQianed582dd2022-04-13 08:21:05 +00001965}
1966
1967/*
Jerry Yu1c3e6882022-04-20 21:23:40 +08001968 * Handler for MBEDTLS_SSL_SERVER_HELLO
Jerry Yu5b64ae92022-03-30 17:15:02 +08001969 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001970MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01001971static int ssl_tls13_prepare_server_hello(mbedtls_ssl_context *ssl)
Jerry Yuf4b27e42022-03-30 17:32:21 +08001972{
Jerry Yu637a3f12022-04-20 21:37:58 +08001973 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1974 unsigned char *server_randbytes =
Gilles Peskine449bd832023-01-11 14:50:10 +01001975 ssl->handshake->randbytes + MBEDTLS_CLIENT_HELLO_RANDOM_LEN;
Jerry Yuf4b27e42022-03-30 17:32:21 +08001976
Gilles Peskine449bd832023-01-11 14:50:10 +01001977 if ((ret = ssl->conf->f_rng(ssl->conf->p_rng, server_randbytes,
1978 MBEDTLS_SERVER_HELLO_RANDOM_LEN)) != 0) {
1979 MBEDTLS_SSL_DEBUG_RET(1, "f_rng", ret);
1980 return ret;
Jerry Yuf4b27e42022-03-30 17:32:21 +08001981 }
1982
Gilles Peskine449bd832023-01-11 14:50:10 +01001983 MBEDTLS_SSL_DEBUG_BUF(3, "server hello, random bytes", server_randbytes,
1984 MBEDTLS_SERVER_HELLO_RANDOM_LEN);
Jerry Yuf4b27e42022-03-30 17:32:21 +08001985
1986#if defined(MBEDTLS_HAVE_TIME)
YxCda609132023-05-22 12:08:12 -07001987 ssl->session_negotiate->start = mbedtls_time(NULL);
Jerry Yuf4b27e42022-03-30 17:32:21 +08001988#endif /* MBEDTLS_HAVE_TIME */
1989
Gilles Peskine449bd832023-01-11 14:50:10 +01001990 return ret;
Jerry Yuf4b27e42022-03-30 17:32:21 +08001991}
1992
Jerry Yu3bf2c642022-03-30 22:02:12 +08001993/*
Jerry Yue74e04a2022-04-21 09:23:16 +08001994 * ssl_tls13_write_server_hello_supported_versions_ext ():
Jerry Yu3bf2c642022-03-30 22:02:12 +08001995 *
1996 * struct {
Jerry Yufb9f54d2022-04-06 10:08:34 +08001997 * ProtocolVersion selected_version;
Jerry Yu3bf2c642022-03-30 22:02:12 +08001998 * } SupportedVersions;
1999 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002000MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yue74e04a2022-04-21 09:23:16 +08002001static int ssl_tls13_write_server_hello_supported_versions_ext(
Gilles Peskine449bd832023-01-11 14:50:10 +01002002 mbedtls_ssl_context *ssl,
2003 unsigned char *buf,
2004 unsigned char *end,
2005 size_t *out_len)
Jerry Yu3bf2c642022-03-30 22:02:12 +08002006{
Jerry Yu3bf2c642022-03-30 22:02:12 +08002007 *out_len = 0;
2008
Gilles Peskine449bd832023-01-11 14:50:10 +01002009 MBEDTLS_SSL_DEBUG_MSG(3, ("server hello, write selected version"));
Jerry Yu3bf2c642022-03-30 22:02:12 +08002010
2011 /* Check if we have space to write the extension:
2012 * - extension_type (2 bytes)
2013 * - extension_data_length (2 bytes)
Jerry Yu349a6132022-04-14 20:52:56 +08002014 * - selected_version (2 bytes)
Jerry Yu3bf2c642022-03-30 22:02:12 +08002015 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002016 MBEDTLS_SSL_CHK_BUF_PTR(buf, end, 6);
Jerry Yu3bf2c642022-03-30 22:02:12 +08002017
Gilles Peskine449bd832023-01-11 14:50:10 +01002018 MBEDTLS_PUT_UINT16_BE(MBEDTLS_TLS_EXT_SUPPORTED_VERSIONS, buf, 0);
Jerry Yu3bf2c642022-03-30 22:02:12 +08002019
Gilles Peskine449bd832023-01-11 14:50:10 +01002020 MBEDTLS_PUT_UINT16_BE(2, buf, 2);
Jerry Yu3bf2c642022-03-30 22:02:12 +08002021
Gilles Peskine449bd832023-01-11 14:50:10 +01002022 mbedtls_ssl_write_version(buf + 4,
2023 ssl->conf->transport,
2024 ssl->tls_version);
Jerry Yu3bf2c642022-03-30 22:02:12 +08002025
Gilles Peskine449bd832023-01-11 14:50:10 +01002026 MBEDTLS_SSL_DEBUG_MSG(3, ("supported version: [%04x]",
2027 ssl->tls_version));
Jerry Yu3bf2c642022-03-30 22:02:12 +08002028
Jerry Yu349a6132022-04-14 20:52:56 +08002029 *out_len = 6;
Jerry Yu3bf2c642022-03-30 22:02:12 +08002030
Jerry Yub95dd362022-11-08 21:19:34 +08002031 mbedtls_ssl_tls13_set_hs_sent_ext_mask(
Gilles Peskine449bd832023-01-11 14:50:10 +01002032 ssl, MBEDTLS_TLS_EXT_SUPPORTED_VERSIONS);
Jerry Yub95dd362022-11-08 21:19:34 +08002033
Gilles Peskine449bd832023-01-11 14:50:10 +01002034 return 0;
Jerry Yu3bf2c642022-03-30 22:02:12 +08002035}
2036
Jerry Yud9436a12022-04-20 22:28:09 +08002037
Jerry Yu3bf2c642022-03-30 22:02:12 +08002038
2039/* Generate and export a single key share. For hybrid KEMs, this can
2040 * be called multiple times with the different components of the hybrid. */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002041MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002042static int ssl_tls13_generate_and_write_key_share(mbedtls_ssl_context *ssl,
2043 uint16_t named_group,
2044 unsigned char *buf,
2045 unsigned char *end,
2046 size_t *out_len)
Jerry Yu3bf2c642022-03-30 22:02:12 +08002047{
2048 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yu955ddd72022-04-22 22:27:33 +08002049
2050 *out_len = 0;
2051
Valerio Settic9ae8622023-07-25 11:23:50 +02002052#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_EPHEMERAL_ENABLED)
Przemek Stekiel29c219c2023-05-31 15:21:04 +02002053 if (mbedtls_ssl_tls13_named_group_is_ecdhe(named_group) ||
Przemek Stekield5f79e72023-06-29 09:08:43 +02002054 mbedtls_ssl_tls13_named_group_is_ffdh(named_group)) {
Przemek Stekiel408569f2023-07-06 11:26:44 +02002055 ret = mbedtls_ssl_tls13_generate_and_write_xxdh_key_exchange(
Gilles Peskine449bd832023-01-11 14:50:10 +01002056 ssl, named_group, buf, end, out_len);
2057 if (ret != 0) {
Jerry Yu89e103c2022-03-30 22:43:29 +08002058 MBEDTLS_SSL_DEBUG_RET(
Przemek Stekiel408569f2023-07-06 11:26:44 +02002059 1, "mbedtls_ssl_tls13_generate_and_write_xxdh_key_exchange",
Gilles Peskine449bd832023-01-11 14:50:10 +01002060 ret);
2061 return ret;
Jerry Yu3bf2c642022-03-30 22:02:12 +08002062 }
Gilles Peskine449bd832023-01-11 14:50:10 +01002063 } else
Valerio Settic9ae8622023-07-25 11:23:50 +02002064#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_EPHEMERAL_ENABLED */
Gilles Peskine449bd832023-01-11 14:50:10 +01002065 if (0 /* Other kinds of KEMs */) {
2066 } else {
Jerry Yu955ddd72022-04-22 22:27:33 +08002067 ((void) ssl);
2068 ((void) named_group);
2069 ((void) buf);
2070 ((void) end);
Jerry Yu3bf2c642022-03-30 22:02:12 +08002071 ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
2072 }
2073
Gilles Peskine449bd832023-01-11 14:50:10 +01002074 return ret;
Jerry Yu3bf2c642022-03-30 22:02:12 +08002075}
2076
2077/*
2078 * ssl_tls13_write_key_share_ext
2079 *
2080 * Structure of key_share extension in ServerHello:
2081 *
Jerry Yu1c3e6882022-04-20 21:23:40 +08002082 * struct {
2083 * NamedGroup group;
2084 * opaque key_exchange<1..2^16-1>;
2085 * } KeyShareEntry;
2086 * struct {
2087 * KeyShareEntry server_share;
2088 * } KeyShareServerHello;
Jerry Yu3bf2c642022-03-30 22:02:12 +08002089 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002090MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002091static int ssl_tls13_write_key_share_ext(mbedtls_ssl_context *ssl,
2092 unsigned char *buf,
2093 unsigned char *end,
2094 size_t *out_len)
Jerry Yu3bf2c642022-03-30 22:02:12 +08002095{
Jerry Yu955ddd72022-04-22 22:27:33 +08002096 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yu3bf2c642022-03-30 22:02:12 +08002097 unsigned char *p = buf;
Jerry Yu57d48412022-04-20 21:50:42 +08002098 uint16_t group = ssl->handshake->offered_group_id;
Jerry Yu3bf2c642022-03-30 22:02:12 +08002099 unsigned char *server_share = buf + 4;
Jerry Yu3bf2c642022-03-30 22:02:12 +08002100 size_t key_exchange_length;
Jerry Yu3bf2c642022-03-30 22:02:12 +08002101
2102 *out_len = 0;
2103
Gilles Peskine449bd832023-01-11 14:50:10 +01002104 MBEDTLS_SSL_DEBUG_MSG(3, ("server hello, adding key share extension"));
Jerry Yu3bf2c642022-03-30 22:02:12 +08002105
Gilles Peskine449bd832023-01-11 14:50:10 +01002106 MBEDTLS_SSL_DEBUG_MSG(2, ("server hello, write selected_group: %s (%04x)",
2107 mbedtls_ssl_named_group_to_str(group),
2108 group));
Jerry Yu58af2332022-09-06 11:19:31 +08002109
Jerry Yu3bf2c642022-03-30 22:02:12 +08002110 /* Check if we have space for header and length fields:
2111 * - extension_type (2 bytes)
2112 * - extension_data_length (2 bytes)
2113 * - group (2 bytes)
2114 * - key_exchange_length (2 bytes)
2115 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002116 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 8);
2117 MBEDTLS_PUT_UINT16_BE(MBEDTLS_TLS_EXT_KEY_SHARE, p, 0);
2118 MBEDTLS_PUT_UINT16_BE(group, server_share, 0);
Jerry Yu3bf2c642022-03-30 22:02:12 +08002119 p += 8;
Jerry Yu57d48412022-04-20 21:50:42 +08002120
Jerry Yu3bf2c642022-03-30 22:02:12 +08002121 /* When we introduce PQC-ECDHE hybrids, we'll want to call this
2122 * function multiple times. */
Jerry Yu955ddd72022-04-22 22:27:33 +08002123 ret = ssl_tls13_generate_and_write_key_share(
Gilles Peskine449bd832023-01-11 14:50:10 +01002124 ssl, group, server_share + 4, end, &key_exchange_length);
2125 if (ret != 0) {
2126 return ret;
2127 }
Jerry Yu3bf2c642022-03-30 22:02:12 +08002128 p += key_exchange_length;
Jerry Yuc1be19f2022-04-23 16:11:39 +08002129
Gilles Peskine449bd832023-01-11 14:50:10 +01002130 MBEDTLS_PUT_UINT16_BE(key_exchange_length, server_share + 2, 0);
Jerry Yu3bf2c642022-03-30 22:02:12 +08002131
Gilles Peskine449bd832023-01-11 14:50:10 +01002132 MBEDTLS_PUT_UINT16_BE(p - server_share, buf, 2);
Jerry Yu3bf2c642022-03-30 22:02:12 +08002133
Jerry Yu57d48412022-04-20 21:50:42 +08002134 *out_len = p - buf;
Jerry Yu955ddd72022-04-22 22:27:33 +08002135
Gilles Peskine449bd832023-01-11 14:50:10 +01002136 mbedtls_ssl_tls13_set_hs_sent_ext_mask(ssl, MBEDTLS_TLS_EXT_KEY_SHARE);
Jerry Yub95dd362022-11-08 21:19:34 +08002137
Gilles Peskine449bd832023-01-11 14:50:10 +01002138 return 0;
Jerry Yu3bf2c642022-03-30 22:02:12 +08002139}
Jerry Yud9436a12022-04-20 22:28:09 +08002140
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002141MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002142static int ssl_tls13_write_hrr_key_share_ext(mbedtls_ssl_context *ssl,
2143 unsigned char *buf,
2144 unsigned char *end,
2145 size_t *out_len)
Jerry Yu23f7a6f2022-04-23 15:16:45 +08002146{
2147 uint16_t selected_group = ssl->handshake->hrr_selected_group;
2148 /* key_share Extension
2149 *
2150 * struct {
2151 * select (Handshake.msg_type) {
2152 * ...
2153 * case hello_retry_request:
2154 * NamedGroup selected_group;
2155 * ...
2156 * };
2157 * } KeyShare;
2158 */
2159
2160 *out_len = 0;
2161
Jerry Yub0ac10b2022-05-05 11:10:08 +08002162 /*
2163 * For a pure PSK key exchange, there is no group to agree upon. The purpose
2164 * of the HRR is then to transmit a cookie to force the client to demonstrate
2165 * reachability at their apparent network address (primarily useful for DTLS).
2166 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002167 if (!mbedtls_ssl_tls13_key_exchange_mode_with_ephemeral(ssl)) {
2168 return 0;
2169 }
Jerry Yu23f7a6f2022-04-23 15:16:45 +08002170
2171 /* We should only send the key_share extension if the client's initial
2172 * key share was not acceptable. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002173 if (ssl->handshake->offered_group_id != 0) {
2174 MBEDTLS_SSL_DEBUG_MSG(4, ("Skip key_share extension in HRR"));
2175 return 0;
Jerry Yu23f7a6f2022-04-23 15:16:45 +08002176 }
2177
Gilles Peskine449bd832023-01-11 14:50:10 +01002178 if (selected_group == 0) {
2179 MBEDTLS_SSL_DEBUG_MSG(1, ("no matching named group found"));
2180 return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
Jerry Yu23f7a6f2022-04-23 15:16:45 +08002181 }
2182
Jerry Yub0ac10b2022-05-05 11:10:08 +08002183 /* Check if we have enough space:
2184 * - extension_type (2 bytes)
2185 * - extension_data_length (2 bytes)
2186 * - selected_group (2 bytes)
2187 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002188 MBEDTLS_SSL_CHK_BUF_PTR(buf, end, 6);
Jerry Yu23f7a6f2022-04-23 15:16:45 +08002189
Gilles Peskine449bd832023-01-11 14:50:10 +01002190 MBEDTLS_PUT_UINT16_BE(MBEDTLS_TLS_EXT_KEY_SHARE, buf, 0);
2191 MBEDTLS_PUT_UINT16_BE(2, buf, 2);
2192 MBEDTLS_PUT_UINT16_BE(selected_group, buf, 4);
Jerry Yu23f7a6f2022-04-23 15:16:45 +08002193
Gilles Peskine449bd832023-01-11 14:50:10 +01002194 MBEDTLS_SSL_DEBUG_MSG(3,
2195 ("HRR selected_group: %s (%x)",
2196 mbedtls_ssl_named_group_to_str(selected_group),
2197 selected_group));
Jerry Yu23f7a6f2022-04-23 15:16:45 +08002198
2199 *out_len = 6;
Jerry Yu23f7a6f2022-04-23 15:16:45 +08002200
Gilles Peskine449bd832023-01-11 14:50:10 +01002201 mbedtls_ssl_tls13_set_hs_sent_ext_mask(ssl, MBEDTLS_TLS_EXT_KEY_SHARE);
Jerry Yub95dd362022-11-08 21:19:34 +08002202
Gilles Peskine449bd832023-01-11 14:50:10 +01002203 return 0;
Jerry Yu23f7a6f2022-04-23 15:16:45 +08002204}
Jerry Yu3bf2c642022-03-30 22:02:12 +08002205
2206/*
2207 * Structure of ServerHello message:
2208 *
2209 * struct {
2210 * ProtocolVersion legacy_version = 0x0303; // TLS v1.2
2211 * Random random;
2212 * opaque legacy_session_id_echo<0..32>;
2213 * CipherSuite cipher_suite;
2214 * uint8 legacy_compression_method = 0;
2215 * Extension extensions<6..2^16-1>;
2216 * } ServerHello;
2217 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002218MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002219static int ssl_tls13_write_server_hello_body(mbedtls_ssl_context *ssl,
2220 unsigned char *buf,
2221 unsigned char *end,
2222 size_t *out_len,
2223 int is_hrr)
Jerry Yu56404d72022-03-30 17:36:13 +08002224{
Jerry Yu955ddd72022-04-22 22:27:33 +08002225 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yu3bf2c642022-03-30 22:02:12 +08002226 unsigned char *p = buf;
Jerry Yud9436a12022-04-20 22:28:09 +08002227 unsigned char *p_extensions_len;
Jerry Yufbe3e642022-04-25 19:31:51 +08002228 size_t output_len;
Jerry Yu3bf2c642022-03-30 22:02:12 +08002229
2230 *out_len = 0;
Jerry Yu50e00e32022-10-31 14:45:01 +08002231 ssl->handshake->sent_extensions = MBEDTLS_SSL_EXT_MASK_NONE;
Jerry Yu3bf2c642022-03-30 22:02:12 +08002232
Jerry Yucfc04b32022-04-21 09:31:58 +08002233 /* ...
2234 * ProtocolVersion legacy_version = 0x0303; // TLS 1.2
2235 * ...
2236 * with ProtocolVersion defined as:
2237 * uint16 ProtocolVersion;
Jerry Yu3bf2c642022-03-30 22:02:12 +08002238 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002239 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 2);
2240 MBEDTLS_PUT_UINT16_BE(0x0303, p, 0);
Jerry Yu3bf2c642022-03-30 22:02:12 +08002241 p += 2;
2242
Jerry Yu1c3e6882022-04-20 21:23:40 +08002243 /* ...
2244 * Random random;
2245 * ...
Jerry Yucfc04b32022-04-21 09:31:58 +08002246 * with Random defined as:
2247 * opaque Random[MBEDTLS_SERVER_HELLO_RANDOM_LEN];
Jerry Yu1c3e6882022-04-20 21:23:40 +08002248 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002249 MBEDTLS_SSL_CHK_BUF_PTR(p, end, MBEDTLS_SERVER_HELLO_RANDOM_LEN);
2250 if (is_hrr) {
2251 memcpy(p, mbedtls_ssl_tls13_hello_retry_request_magic,
2252 MBEDTLS_SERVER_HELLO_RANDOM_LEN);
2253 } else {
2254 memcpy(p, &ssl->handshake->randbytes[MBEDTLS_CLIENT_HELLO_RANDOM_LEN],
2255 MBEDTLS_SERVER_HELLO_RANDOM_LEN);
Jerry Yu23f7a6f2022-04-23 15:16:45 +08002256 }
Gilles Peskine449bd832023-01-11 14:50:10 +01002257 MBEDTLS_SSL_DEBUG_BUF(3, "server hello, random bytes",
2258 p, MBEDTLS_SERVER_HELLO_RANDOM_LEN);
Jerry Yu3bf2c642022-03-30 22:02:12 +08002259 p += MBEDTLS_SERVER_HELLO_RANDOM_LEN;
2260
Jerry Yucfc04b32022-04-21 09:31:58 +08002261 /* ...
2262 * opaque legacy_session_id_echo<0..32>;
2263 * ...
Jerry Yu3bf2c642022-03-30 22:02:12 +08002264 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002265 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 1 + ssl->session_negotiate->id_len);
2266 *p++ = (unsigned char) ssl->session_negotiate->id_len;
2267 if (ssl->session_negotiate->id_len > 0) {
2268 memcpy(p, &ssl->session_negotiate->id[0],
2269 ssl->session_negotiate->id_len);
Jerry Yu3bf2c642022-03-30 22:02:12 +08002270 p += ssl->session_negotiate->id_len;
Jerry Yu955ddd72022-04-22 22:27:33 +08002271
Gilles Peskine449bd832023-01-11 14:50:10 +01002272 MBEDTLS_SSL_DEBUG_BUF(3, "session id", ssl->session_negotiate->id,
2273 ssl->session_negotiate->id_len);
Jerry Yu3bf2c642022-03-30 22:02:12 +08002274 }
2275
Jerry Yucfc04b32022-04-21 09:31:58 +08002276 /* ...
2277 * CipherSuite cipher_suite;
2278 * ...
2279 * with CipherSuite defined as:
2280 * uint8 CipherSuite[2];
Jerry Yu3bf2c642022-03-30 22:02:12 +08002281 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002282 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 2);
2283 MBEDTLS_PUT_UINT16_BE(ssl->session_negotiate->ciphersuite, p, 0);
Jerry Yu3bf2c642022-03-30 22:02:12 +08002284 p += 2;
Gilles Peskine449bd832023-01-11 14:50:10 +01002285 MBEDTLS_SSL_DEBUG_MSG(3,
2286 ("server hello, chosen ciphersuite: %s ( id=%d )",
2287 mbedtls_ssl_get_ciphersuite_name(
2288 ssl->session_negotiate->ciphersuite),
2289 ssl->session_negotiate->ciphersuite));
Jerry Yu3bf2c642022-03-30 22:02:12 +08002290
Jerry Yucfc04b32022-04-21 09:31:58 +08002291 /* ...
2292 * uint8 legacy_compression_method = 0;
2293 * ...
2294 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002295 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 1);
Thomas Daubney31e03a82022-07-25 15:59:25 +01002296 *p++ = MBEDTLS_SSL_COMPRESS_NULL;
Jerry Yu3bf2c642022-03-30 22:02:12 +08002297
Jerry Yucfc04b32022-04-21 09:31:58 +08002298 /* ...
2299 * Extension extensions<6..2^16-1>;
2300 * ...
2301 * struct {
2302 * ExtensionType extension_type; (2 bytes)
2303 * opaque extension_data<0..2^16-1>;
2304 * } Extension;
2305 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002306 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 2);
Jerry Yud9436a12022-04-20 22:28:09 +08002307 p_extensions_len = p;
Jerry Yu3bf2c642022-03-30 22:02:12 +08002308 p += 2;
2309
Gilles Peskine449bd832023-01-11 14:50:10 +01002310 if ((ret = ssl_tls13_write_server_hello_supported_versions_ext(
2311 ssl, p, end, &output_len)) != 0) {
Jerry Yu955ddd72022-04-22 22:27:33 +08002312 MBEDTLS_SSL_DEBUG_RET(
Gilles Peskine449bd832023-01-11 14:50:10 +01002313 1, "ssl_tls13_write_server_hello_supported_versions_ext", ret);
2314 return ret;
Jerry Yu3bf2c642022-03-30 22:02:12 +08002315 }
2316 p += output_len;
2317
Gilles Peskine449bd832023-01-11 14:50:10 +01002318 if (mbedtls_ssl_tls13_key_exchange_mode_with_ephemeral(ssl)) {
2319 if (is_hrr) {
2320 ret = ssl_tls13_write_hrr_key_share_ext(ssl, p, end, &output_len);
2321 } else {
2322 ret = ssl_tls13_write_key_share_ext(ssl, p, end, &output_len);
2323 }
2324 if (ret != 0) {
2325 return ret;
2326 }
Jerry Yu3bf2c642022-03-30 22:02:12 +08002327 p += output_len;
2328 }
Jerry Yu3bf2c642022-03-30 22:02:12 +08002329
Ronald Cron41a443a2022-10-04 16:38:25 +02002330#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01002331 if (!is_hrr && mbedtls_ssl_tls13_key_exchange_mode_with_psk(ssl)) {
2332 ret = ssl_tls13_write_server_pre_shared_key_ext(ssl, p, end, &output_len);
2333 if (ret != 0) {
2334 MBEDTLS_SSL_DEBUG_RET(1, "ssl_tls13_write_server_pre_shared_key_ext",
2335 ret);
2336 return ret;
Jerry Yu032b15ce2022-07-11 06:10:03 +00002337 }
2338 p += output_len;
2339 }
Ronald Cron41a443a2022-10-04 16:38:25 +02002340#endif
Jerry Yu032b15ce2022-07-11 06:10:03 +00002341
Gilles Peskine449bd832023-01-11 14:50:10 +01002342 MBEDTLS_PUT_UINT16_BE(p - p_extensions_len - 2, p_extensions_len, 0);
Jerry Yu3bf2c642022-03-30 22:02:12 +08002343
Gilles Peskine449bd832023-01-11 14:50:10 +01002344 MBEDTLS_SSL_DEBUG_BUF(4, "server hello extensions",
2345 p_extensions_len, p - p_extensions_len);
Jerry Yu3bf2c642022-03-30 22:02:12 +08002346
Jerry Yud9436a12022-04-20 22:28:09 +08002347 *out_len = p - buf;
Jerry Yu3bf2c642022-03-30 22:02:12 +08002348
Gilles Peskine449bd832023-01-11 14:50:10 +01002349 MBEDTLS_SSL_DEBUG_BUF(3, "server hello", buf, *out_len);
Jerry Yu3bf2c642022-03-30 22:02:12 +08002350
Jerry Yu7de2ff02022-11-08 21:43:46 +08002351 MBEDTLS_SSL_PRINT_EXTS(
Jerry Yu4b8f2f72022-10-31 13:31:22 +08002352 3, is_hrr ? MBEDTLS_SSL_TLS1_3_HS_HELLO_RETRY_REQUEST :
Gilles Peskine449bd832023-01-11 14:50:10 +01002353 MBEDTLS_SSL_HS_SERVER_HELLO,
2354 ssl->handshake->sent_extensions);
Jerry Yu4b8f2f72022-10-31 13:31:22 +08002355
Gilles Peskine449bd832023-01-11 14:50:10 +01002356 return ret;
Jerry Yu56404d72022-03-30 17:36:13 +08002357}
2358
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002359MBEDTLS_CHECK_RETURN_CRITICAL
Xiaokang Qian934ce6f2023-02-06 10:23:04 +00002360static int ssl_tls13_finalize_server_hello(mbedtls_ssl_context *ssl)
Jerry Yue110d252022-05-05 10:19:22 +08002361{
2362 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine449bd832023-01-11 14:50:10 +01002363 ret = mbedtls_ssl_tls13_compute_handshake_transform(ssl);
2364 if (ret != 0) {
2365 MBEDTLS_SSL_DEBUG_RET(1,
2366 "mbedtls_ssl_tls13_compute_handshake_transform",
2367 ret);
2368 return ret;
Jerry Yue110d252022-05-05 10:19:22 +08002369 }
2370
Gilles Peskine449bd832023-01-11 14:50:10 +01002371 return ret;
Jerry Yue110d252022-05-05 10:19:22 +08002372}
2373
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002374MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002375static int ssl_tls13_write_server_hello(mbedtls_ssl_context *ssl)
Jerry Yu5b64ae92022-03-30 17:15:02 +08002376{
Jerry Yu637a3f12022-04-20 21:37:58 +08002377 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yuf4b27e42022-03-30 17:32:21 +08002378 unsigned char *buf;
2379 size_t buf_len, msg_len;
2380
Gilles Peskine449bd832023-01-11 14:50:10 +01002381 MBEDTLS_SSL_DEBUG_MSG(2, ("=> write server hello"));
Jerry Yuf4b27e42022-03-30 17:32:21 +08002382
Gilles Peskine449bd832023-01-11 14:50:10 +01002383 MBEDTLS_SSL_PROC_CHK(ssl_tls13_prepare_server_hello(ssl));
Jerry Yuf4b27e42022-03-30 17:32:21 +08002384
Xiaokang Qian9f1747b2023-03-30 02:50:04 +00002385 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_start_handshake_msg(
2386 ssl, MBEDTLS_SSL_HS_SERVER_HELLO, &buf, &buf_len));
Jerry Yuf4b27e42022-03-30 17:32:21 +08002387
Gilles Peskine449bd832023-01-11 14:50:10 +01002388 MBEDTLS_SSL_PROC_CHK(ssl_tls13_write_server_hello_body(ssl, buf,
2389 buf + buf_len,
2390 &msg_len,
2391 0));
Jerry Yuf4b27e42022-03-30 17:32:21 +08002392
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +01002393 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_add_hs_msg_to_checksum(
Manuel Pégourié-Gonnard43cc1272023-02-06 11:48:19 +01002394 ssl, MBEDTLS_SSL_HS_SERVER_HELLO, buf, msg_len));
Jerry Yuf4b27e42022-03-30 17:32:21 +08002395
Gilles Peskine449bd832023-01-11 14:50:10 +01002396 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_finish_handshake_msg(
2397 ssl, buf_len, msg_len));
Jerry Yu637a3f12022-04-20 21:37:58 +08002398
Xiaokang Qian934ce6f2023-02-06 10:23:04 +00002399 MBEDTLS_SSL_PROC_CHK(ssl_tls13_finalize_server_hello(ssl));
Jerry Yue110d252022-05-05 10:19:22 +08002400
Gabor Mezei7b39bf12022-05-24 16:04:14 +02002401#if defined(MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE)
2402 /* The server sends a dummy change_cipher_spec record immediately
2403 * after its first handshake message. This may either be after
2404 * a ServerHello or a HelloRetryRequest.
2405 */
Gabor Mezei96ae9262022-06-28 11:45:18 +02002406 mbedtls_ssl_handshake_set_state(
Gilles Peskine449bd832023-01-11 14:50:10 +01002407 ssl, MBEDTLS_SSL_SERVER_CCS_AFTER_SERVER_HELLO);
Gabor Mezei7b39bf12022-05-24 16:04:14 +02002408#else
Gilles Peskine449bd832023-01-11 14:50:10 +01002409 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_ENCRYPTED_EXTENSIONS);
Gabor Mezei7b39bf12022-05-24 16:04:14 +02002410#endif /* MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE */
Jerry Yue110d252022-05-05 10:19:22 +08002411
Jerry Yuf4b27e42022-03-30 17:32:21 +08002412cleanup:
2413
Gilles Peskine449bd832023-01-11 14:50:10 +01002414 MBEDTLS_SSL_DEBUG_MSG(2, ("<= write server hello"));
2415 return ret;
Jerry Yu5b64ae92022-03-30 17:15:02 +08002416}
2417
Jerry Yu23d1a252022-05-12 18:08:59 +08002418
2419/*
2420 * Handler for MBEDTLS_SSL_HELLO_RETRY_REQUEST
2421 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002422MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002423static int ssl_tls13_prepare_hello_retry_request(mbedtls_ssl_context *ssl)
Jerry Yu23d1a252022-05-12 18:08:59 +08002424{
2425 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Ronald Cron5fbd2702024-02-14 10:03:36 +01002426 if (ssl->handshake->hello_retry_request_flag) {
Gilles Peskine449bd832023-01-11 14:50:10 +01002427 MBEDTLS_SSL_DEBUG_MSG(1, ("Too many HRRs"));
2428 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE,
2429 MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE);
2430 return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
Jerry Yu23d1a252022-05-12 18:08:59 +08002431 }
2432
2433 /*
2434 * Create stateless transcript hash for HRR
2435 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002436 MBEDTLS_SSL_DEBUG_MSG(4, ("Reset transcript for HRR"));
2437 ret = mbedtls_ssl_reset_transcript_for_hrr(ssl);
2438 if (ret != 0) {
2439 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_reset_transcript_for_hrr", ret);
2440 return ret;
Jerry Yu23d1a252022-05-12 18:08:59 +08002441 }
Gilles Peskine449bd832023-01-11 14:50:10 +01002442 mbedtls_ssl_session_reset_msg_layer(ssl, 0);
Jerry Yu23d1a252022-05-12 18:08:59 +08002443
Gilles Peskine449bd832023-01-11 14:50:10 +01002444 return 0;
Jerry Yu23d1a252022-05-12 18:08:59 +08002445}
2446
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002447MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002448static int ssl_tls13_write_hello_retry_request(mbedtls_ssl_context *ssl)
Jerry Yu23d1a252022-05-12 18:08:59 +08002449{
2450 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
2451 unsigned char *buf;
2452 size_t buf_len, msg_len;
2453
Gilles Peskine449bd832023-01-11 14:50:10 +01002454 MBEDTLS_SSL_DEBUG_MSG(2, ("=> write hello retry request"));
Jerry Yu23d1a252022-05-12 18:08:59 +08002455
Gilles Peskine449bd832023-01-11 14:50:10 +01002456 MBEDTLS_SSL_PROC_CHK(ssl_tls13_prepare_hello_retry_request(ssl));
Jerry Yu23d1a252022-05-12 18:08:59 +08002457
Gilles Peskine449bd832023-01-11 14:50:10 +01002458 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_start_handshake_msg(
2459 ssl, MBEDTLS_SSL_HS_SERVER_HELLO,
2460 &buf, &buf_len));
Jerry Yu23d1a252022-05-12 18:08:59 +08002461
Gilles Peskine449bd832023-01-11 14:50:10 +01002462 MBEDTLS_SSL_PROC_CHK(ssl_tls13_write_server_hello_body(ssl, buf,
2463 buf + buf_len,
2464 &msg_len,
2465 1));
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +01002466 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_add_hs_msg_to_checksum(
Manuel Pégourié-Gonnard43cc1272023-02-06 11:48:19 +01002467 ssl, MBEDTLS_SSL_HS_SERVER_HELLO, buf, msg_len));
Jerry Yu23d1a252022-05-12 18:08:59 +08002468
2469
Gilles Peskine449bd832023-01-11 14:50:10 +01002470 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_finish_handshake_msg(ssl, buf_len,
2471 msg_len));
Jerry Yu23d1a252022-05-12 18:08:59 +08002472
Ronald Cron5fbd2702024-02-14 10:03:36 +01002473 ssl->handshake->hello_retry_request_flag = 1;
Jerry Yu23d1a252022-05-12 18:08:59 +08002474
Gabor Mezei7b39bf12022-05-24 16:04:14 +02002475#if defined(MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE)
2476 /* The server sends a dummy change_cipher_spec record immediately
2477 * after its first handshake message. This may either be after
2478 * a ServerHello or a HelloRetryRequest.
2479 */
Gabor Mezei96ae9262022-06-28 11:45:18 +02002480 mbedtls_ssl_handshake_set_state(
Gilles Peskine449bd832023-01-11 14:50:10 +01002481 ssl, MBEDTLS_SSL_SERVER_CCS_AFTER_HELLO_RETRY_REQUEST);
Gabor Mezei7b39bf12022-05-24 16:04:14 +02002482#else
Gilles Peskine449bd832023-01-11 14:50:10 +01002483 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_CLIENT_HELLO);
Gabor Mezei7b39bf12022-05-24 16:04:14 +02002484#endif /* MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE */
Jerry Yu23d1a252022-05-12 18:08:59 +08002485
2486cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +01002487 MBEDTLS_SSL_DEBUG_MSG(2, ("<= write hello retry request"));
2488 return ret;
Jerry Yu23d1a252022-05-12 18:08:59 +08002489}
2490
Jerry Yu5b64ae92022-03-30 17:15:02 +08002491/*
Jerry Yu4d3841a2022-04-16 12:37:19 +08002492 * Handler for MBEDTLS_SSL_ENCRYPTED_EXTENSIONS
2493 */
Jerry Yu4d3841a2022-04-16 12:37:19 +08002494
2495/*
2496 * struct {
2497 * Extension extensions<0..2 ^ 16 - 1>;
2498 * } EncryptedExtensions;
2499 *
2500 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002501MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002502static int ssl_tls13_write_encrypted_extensions_body(mbedtls_ssl_context *ssl,
2503 unsigned char *buf,
2504 unsigned char *end,
2505 size_t *out_len)
Jerry Yu4d3841a2022-04-16 12:37:19 +08002506{
XiaokangQianacb39922022-06-17 10:18:48 +00002507 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yu4d3841a2022-04-16 12:37:19 +08002508 unsigned char *p = buf;
2509 size_t extensions_len = 0;
Jerry Yu8937eb42022-05-03 12:12:14 +08002510 unsigned char *p_extensions_len;
XiaokangQianacb39922022-06-17 10:18:48 +00002511 size_t output_len;
Jerry Yu9da5e5a2022-05-03 15:46:09 +08002512
Jerry Yu4d3841a2022-04-16 12:37:19 +08002513 *out_len = 0;
2514
Gilles Peskine449bd832023-01-11 14:50:10 +01002515 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 2);
Jerry Yu8937eb42022-05-03 12:12:14 +08002516 p_extensions_len = p;
Jerry Yu4d3841a2022-04-16 12:37:19 +08002517 p += 2;
2518
Jerry Yu8937eb42022-05-03 12:12:14 +08002519 ((void) ssl);
XiaokangQianacb39922022-06-17 10:18:48 +00002520 ((void) ret);
2521 ((void) output_len);
2522
2523#if defined(MBEDTLS_SSL_ALPN)
Gilles Peskine449bd832023-01-11 14:50:10 +01002524 ret = mbedtls_ssl_write_alpn_ext(ssl, p, end, &output_len);
2525 if (ret != 0) {
2526 return ret;
2527 }
XiaokangQian95d5f542022-06-24 02:29:26 +00002528 p += output_len;
XiaokangQianacb39922022-06-17 10:18:48 +00002529#endif /* MBEDTLS_SSL_ALPN */
Jerry Yu4d3841a2022-04-16 12:37:19 +08002530
Jerry Yu71c14f12022-12-12 11:10:35 +08002531#if defined(MBEDTLS_SSL_EARLY_DATA)
Ronald Cron78a38f62024-02-01 18:30:31 +01002532 if (ssl->handshake->early_data_accepted) {
Jerry Yu52335392023-11-23 18:06:06 +08002533 ret = mbedtls_ssl_tls13_write_early_data_ext(
Jerry Yuc59c5862023-12-05 10:40:49 +08002534 ssl, 0, p, end, &output_len);
Jerry Yu71c14f12022-12-12 11:10:35 +08002535 if (ret != 0) {
2536 return ret;
2537 }
2538 p += output_len;
2539 }
2540#endif /* MBEDTLS_SSL_EARLY_DATA */
2541
Waleed Elmelegy47d29462024-01-03 17:31:52 +00002542#if defined(MBEDTLS_SSL_RECORD_SIZE_LIMIT)
Waleed Elmelegyfbe42742024-01-05 18:11:10 +00002543 if (ssl->handshake->received_extensions & MBEDTLS_SSL_EXT_MASK(RECORD_SIZE_LIMIT)) {
Waleed Elmelegyd2fc90e2024-01-04 18:04:53 +00002544 ret = mbedtls_ssl_tls13_write_record_size_limit_ext(
2545 ssl, p, end, &output_len);
2546 if (ret != 0) {
2547 return ret;
2548 }
2549 p += output_len;
Waleed Elmelegy47d29462024-01-03 17:31:52 +00002550 }
Waleed Elmelegy47d29462024-01-03 17:31:52 +00002551#endif
2552
Gilles Peskine449bd832023-01-11 14:50:10 +01002553 extensions_len = (p - p_extensions_len) - 2;
2554 MBEDTLS_PUT_UINT16_BE(extensions_len, p_extensions_len, 0);
Jerry Yu8937eb42022-05-03 12:12:14 +08002555
2556 *out_len = p - buf;
Jerry Yu4d3841a2022-04-16 12:37:19 +08002557
Gilles Peskine449bd832023-01-11 14:50:10 +01002558 MBEDTLS_SSL_DEBUG_BUF(4, "encrypted extensions", buf, *out_len);
Jerry Yu4d3841a2022-04-16 12:37:19 +08002559
Jerry Yu7de2ff02022-11-08 21:43:46 +08002560 MBEDTLS_SSL_PRINT_EXTS(
Gilles Peskine449bd832023-01-11 14:50:10 +01002561 3, MBEDTLS_SSL_HS_ENCRYPTED_EXTENSIONS, ssl->handshake->sent_extensions);
Jerry Yu4b8f2f72022-10-31 13:31:22 +08002562
Gilles Peskine449bd832023-01-11 14:50:10 +01002563 return 0;
Jerry Yu4d3841a2022-04-16 12:37:19 +08002564}
2565
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002566MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002567static int ssl_tls13_write_encrypted_extensions(mbedtls_ssl_context *ssl)
Jerry Yu4d3841a2022-04-16 12:37:19 +08002568{
2569 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
2570 unsigned char *buf;
Jerry Yu39730a72022-05-03 12:14:04 +08002571 size_t buf_len, msg_len;
Jerry Yu4d3841a2022-04-16 12:37:19 +08002572
Gilles Peskine449bd832023-01-11 14:50:10 +01002573 mbedtls_ssl_set_outbound_transform(ssl,
2574 ssl->handshake->transform_handshake);
Gabor Mezei54719122022-06-28 11:34:56 +02002575 MBEDTLS_SSL_DEBUG_MSG(
Gilles Peskine449bd832023-01-11 14:50:10 +01002576 3, ("switching to handshake transform for outbound data"));
Gabor Mezei54719122022-06-28 11:34:56 +02002577
Gilles Peskine449bd832023-01-11 14:50:10 +01002578 MBEDTLS_SSL_DEBUG_MSG(2, ("=> write encrypted extensions"));
Jerry Yu4d3841a2022-04-16 12:37:19 +08002579
Xiaokang Qian9f1747b2023-03-30 02:50:04 +00002580 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_start_handshake_msg(
2581 ssl, MBEDTLS_SSL_HS_ENCRYPTED_EXTENSIONS,
2582 &buf, &buf_len));
Jerry Yu4d3841a2022-04-16 12:37:19 +08002583
Gilles Peskine449bd832023-01-11 14:50:10 +01002584 MBEDTLS_SSL_PROC_CHK(ssl_tls13_write_encrypted_extensions_body(
2585 ssl, buf, buf + buf_len, &msg_len));
Jerry Yu4d3841a2022-04-16 12:37:19 +08002586
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +01002587 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_add_hs_msg_to_checksum(
Xiaokang Qian9f1747b2023-03-30 02:50:04 +00002588 ssl, MBEDTLS_SSL_HS_ENCRYPTED_EXTENSIONS,
2589 buf, msg_len));
Jerry Yu4d3841a2022-04-16 12:37:19 +08002590
Gilles Peskine449bd832023-01-11 14:50:10 +01002591 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_finish_handshake_msg(
2592 ssl, buf_len, msg_len));
Jerry Yu4d3841a2022-04-16 12:37:19 +08002593
Ronald Cron928cbd32022-10-04 16:14:26 +02002594#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01002595 if (mbedtls_ssl_tls13_key_exchange_mode_with_psk(ssl)) {
2596 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_SERVER_FINISHED);
2597 } else {
2598 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_CERTIFICATE_REQUEST);
2599 }
Jerry Yu8937eb42022-05-03 12:12:14 +08002600#else
Gilles Peskine449bd832023-01-11 14:50:10 +01002601 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_SERVER_FINISHED);
Jerry Yu8937eb42022-05-03 12:12:14 +08002602#endif
2603
Jerry Yu4d3841a2022-04-16 12:37:19 +08002604cleanup:
2605
Gilles Peskine449bd832023-01-11 14:50:10 +01002606 MBEDTLS_SSL_DEBUG_MSG(2, ("<= write encrypted extensions"));
2607 return ret;
Jerry Yu4d3841a2022-04-16 12:37:19 +08002608}
2609
Ronald Cron928cbd32022-10-04 16:14:26 +02002610#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED)
XiaokangQiana987e1d2022-05-07 01:25:58 +00002611#define SSL_CERTIFICATE_REQUEST_SEND_REQUEST 0
2612#define SSL_CERTIFICATE_REQUEST_SKIP 1
2613/* Coordination:
2614 * Check whether a CertificateRequest message should be written.
2615 * Returns a negative code on failure, or
2616 * - SSL_CERTIFICATE_REQUEST_SEND_REQUEST
2617 * - SSL_CERTIFICATE_REQUEST_SKIP
2618 * indicating if the writing of the CertificateRequest
2619 * should be skipped or not.
2620 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002621MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002622static int ssl_tls13_certificate_request_coordinate(mbedtls_ssl_context *ssl)
XiaokangQiana987e1d2022-05-07 01:25:58 +00002623{
2624 int authmode;
2625
XiaokangQian40a35232022-05-07 09:02:40 +00002626#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Gilles Peskine449bd832023-01-11 14:50:10 +01002627 if (ssl->handshake->sni_authmode != MBEDTLS_SSL_VERIFY_UNSET) {
XiaokangQian40a35232022-05-07 09:02:40 +00002628 authmode = ssl->handshake->sni_authmode;
Gilles Peskine449bd832023-01-11 14:50:10 +01002629 } else
XiaokangQian40a35232022-05-07 09:02:40 +00002630#endif
XiaokangQiana987e1d2022-05-07 01:25:58 +00002631 authmode = ssl->conf->authmode;
2632
Gilles Peskine449bd832023-01-11 14:50:10 +01002633 if (authmode == MBEDTLS_SSL_VERIFY_NONE) {
Ronald Croneac00ad2022-09-13 10:16:31 +02002634 ssl->session_negotiate->verify_result = MBEDTLS_X509_BADCERT_SKIP_VERIFY;
Gilles Peskine449bd832023-01-11 14:50:10 +01002635 return SSL_CERTIFICATE_REQUEST_SKIP;
Ronald Croneac00ad2022-09-13 10:16:31 +02002636 }
XiaokangQiana987e1d2022-05-07 01:25:58 +00002637
XiaokangQianc3017f62022-05-13 05:55:41 +00002638 ssl->handshake->certificate_request_sent = 1;
XiaokangQian189ded22022-05-10 08:12:17 +00002639
Gilles Peskine449bd832023-01-11 14:50:10 +01002640 return SSL_CERTIFICATE_REQUEST_SEND_REQUEST;
XiaokangQiana987e1d2022-05-07 01:25:58 +00002641}
2642
XiaokangQiancec9ae62022-05-06 07:28:50 +00002643/*
2644 * struct {
2645 * opaque certificate_request_context<0..2^8-1>;
2646 * Extension extensions<2..2^16-1>;
2647 * } CertificateRequest;
2648 *
2649 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002650MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002651static int ssl_tls13_write_certificate_request_body(mbedtls_ssl_context *ssl,
2652 unsigned char *buf,
2653 const unsigned char *end,
2654 size_t *out_len)
XiaokangQiancec9ae62022-05-06 07:28:50 +00002655{
2656 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
2657 unsigned char *p = buf;
XiaokangQianec6efb92022-05-06 09:53:10 +00002658 size_t output_len = 0;
XiaokangQiancec9ae62022-05-06 07:28:50 +00002659 unsigned char *p_extensions_len;
2660
2661 *out_len = 0;
2662
2663 /* Check if we have enough space:
2664 * - certificate_request_context (1 byte)
2665 * - extensions length (2 bytes)
2666 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002667 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 3);
XiaokangQiancec9ae62022-05-06 07:28:50 +00002668
2669 /*
2670 * Write certificate_request_context
2671 */
2672 /*
2673 * We use a zero length context for the normal handshake
2674 * messages. For post-authentication handshake messages
2675 * this request context would be set to a non-zero value.
2676 */
2677 *p++ = 0x0;
2678
2679 /*
2680 * Write extensions
2681 */
2682 /* The extensions must contain the signature_algorithms. */
2683 p_extensions_len = p;
2684 p += 2;
Gilles Peskine449bd832023-01-11 14:50:10 +01002685 ret = mbedtls_ssl_write_sig_alg_ext(ssl, p, end, &output_len);
2686 if (ret != 0) {
2687 return ret;
2688 }
XiaokangQiancec9ae62022-05-06 07:28:50 +00002689
XiaokangQianec6efb92022-05-06 09:53:10 +00002690 p += output_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01002691 MBEDTLS_PUT_UINT16_BE(p - p_extensions_len - 2, p_extensions_len, 0);
XiaokangQiancec9ae62022-05-06 07:28:50 +00002692
2693 *out_len = p - buf;
2694
Jerry Yu7de2ff02022-11-08 21:43:46 +08002695 MBEDTLS_SSL_PRINT_EXTS(
Gilles Peskine449bd832023-01-11 14:50:10 +01002696 3, MBEDTLS_SSL_HS_CERTIFICATE_REQUEST, ssl->handshake->sent_extensions);
Jerry Yu4b8f2f72022-10-31 13:31:22 +08002697
Gilles Peskine449bd832023-01-11 14:50:10 +01002698 return 0;
XiaokangQiancec9ae62022-05-06 07:28:50 +00002699}
2700
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002701MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002702static int ssl_tls13_write_certificate_request(mbedtls_ssl_context *ssl)
XiaokangQiancec9ae62022-05-06 07:28:50 +00002703{
2704 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
2705
Gilles Peskine449bd832023-01-11 14:50:10 +01002706 MBEDTLS_SSL_DEBUG_MSG(2, ("=> write certificate request"));
XiaokangQiancec9ae62022-05-06 07:28:50 +00002707
Gilles Peskine449bd832023-01-11 14:50:10 +01002708 MBEDTLS_SSL_PROC_CHK_NEG(ssl_tls13_certificate_request_coordinate(ssl));
XiaokangQiancec9ae62022-05-06 07:28:50 +00002709
Gilles Peskine449bd832023-01-11 14:50:10 +01002710 if (ret == SSL_CERTIFICATE_REQUEST_SEND_REQUEST) {
XiaokangQiancec9ae62022-05-06 07:28:50 +00002711 unsigned char *buf;
2712 size_t buf_len, msg_len;
2713
Xiaokang Qian9f1747b2023-03-30 02:50:04 +00002714 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_start_handshake_msg(
2715 ssl, MBEDTLS_SSL_HS_CERTIFICATE_REQUEST,
2716 &buf, &buf_len));
XiaokangQiancec9ae62022-05-06 07:28:50 +00002717
Gilles Peskine449bd832023-01-11 14:50:10 +01002718 MBEDTLS_SSL_PROC_CHK(ssl_tls13_write_certificate_request_body(
2719 ssl, buf, buf + buf_len, &msg_len));
XiaokangQiancec9ae62022-05-06 07:28:50 +00002720
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +01002721 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_add_hs_msg_to_checksum(
Xiaokang Qian9f1747b2023-03-30 02:50:04 +00002722 ssl, MBEDTLS_SSL_HS_CERTIFICATE_REQUEST,
2723 buf, msg_len));
XiaokangQiancec9ae62022-05-06 07:28:50 +00002724
Gilles Peskine449bd832023-01-11 14:50:10 +01002725 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_finish_handshake_msg(
2726 ssl, buf_len, msg_len));
2727 } else if (ret == SSL_CERTIFICATE_REQUEST_SKIP) {
2728 MBEDTLS_SSL_DEBUG_MSG(2, ("<= skip write certificate request"));
XiaokangQiancec9ae62022-05-06 07:28:50 +00002729 ret = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01002730 } else {
2731 MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
XiaokangQiancec9ae62022-05-06 07:28:50 +00002732 ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
2733 goto cleanup;
2734 }
2735
Gilles Peskine449bd832023-01-11 14:50:10 +01002736 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_SERVER_CERTIFICATE);
XiaokangQiancec9ae62022-05-06 07:28:50 +00002737cleanup:
2738
Gilles Peskine449bd832023-01-11 14:50:10 +01002739 MBEDTLS_SSL_DEBUG_MSG(2, ("<= write certificate request"));
2740 return ret;
XiaokangQiancec9ae62022-05-06 07:28:50 +00002741}
XiaokangQiancec9ae62022-05-06 07:28:50 +00002742
Jerry Yu4d3841a2022-04-16 12:37:19 +08002743/*
Jerry Yuc6e6dbf2022-04-16 19:42:57 +08002744 * Handler for MBEDTLS_SSL_SERVER_CERTIFICATE
Jerry Yu83da34e2022-04-16 13:59:52 +08002745 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002746MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002747static int ssl_tls13_write_server_certificate(mbedtls_ssl_context *ssl)
Jerry Yu83da34e2022-04-16 13:59:52 +08002748{
Jerry Yu5a26f302022-05-10 20:46:40 +08002749 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
XiaokangQianfb665a82022-06-15 03:57:21 +00002750
2751#if defined(MBEDTLS_X509_CRT_PARSE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01002752 if ((ssl_tls13_pick_key_cert(ssl) != 0) ||
2753 mbedtls_ssl_own_cert(ssl) == NULL) {
2754 MBEDTLS_SSL_DEBUG_MSG(2, ("No certificate available."));
2755 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE,
2756 MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE);
2757 return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
Jerry Yu5a26f302022-05-10 20:46:40 +08002758 }
XiaokangQianfb665a82022-06-15 03:57:21 +00002759#endif /* MBEDTLS_X509_CRT_PARSE_C */
Jerry Yu5a26f302022-05-10 20:46:40 +08002760
Gilles Peskine449bd832023-01-11 14:50:10 +01002761 ret = mbedtls_ssl_tls13_write_certificate(ssl);
2762 if (ret != 0) {
2763 return ret;
2764 }
2765 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_CERTIFICATE_VERIFY);
2766 return 0;
Jerry Yu83da34e2022-04-16 13:59:52 +08002767}
2768
2769/*
Jerry Yuc6e6dbf2022-04-16 19:42:57 +08002770 * Handler for MBEDTLS_SSL_CERTIFICATE_VERIFY
Jerry Yu83da34e2022-04-16 13:59:52 +08002771 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002772MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002773static int ssl_tls13_write_certificate_verify(mbedtls_ssl_context *ssl)
Jerry Yu83da34e2022-04-16 13:59:52 +08002774{
Gilles Peskine449bd832023-01-11 14:50:10 +01002775 int ret = mbedtls_ssl_tls13_write_certificate_verify(ssl);
2776 if (ret != 0) {
2777 return ret;
2778 }
2779 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_SERVER_FINISHED);
2780 return 0;
Jerry Yu83da34e2022-04-16 13:59:52 +08002781}
Ronald Cron928cbd32022-10-04 16:14:26 +02002782#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED */
Jerry Yu83da34e2022-04-16 13:59:52 +08002783
Jerry Yu9b72e392023-12-01 16:27:08 +08002784/*
2785 * RFC 8446 section A.2
2786 *
2787 * | Send ServerHello
2788 * | K_send = handshake
2789 * | Send EncryptedExtensions
2790 * | [Send CertificateRequest]
2791 * Can send | [Send Certificate + CertificateVerify]
2792 * app data | Send Finished
2793 * after --> | K_send = application
2794 * here +--------+--------+
2795 * No 0-RTT | | 0-RTT
2796 * | |
2797 * K_recv = handshake | | K_recv = early data
2798 * [Skip decrypt errors] | +------> WAIT_EOED -+
2799 * | | Recv | | Recv EndOfEarlyData
2800 * | | early data | | K_recv = handshake
2801 * | +------------+ |
2802 * | |
2803 * +> WAIT_FLIGHT2 <--------+
2804 * |
2805 * +--------+--------+
2806 * No auth | | Client auth
2807 * | |
2808 * | v
2809 * | WAIT_CERT
2810 * | Recv | | Recv Certificate
2811 * | empty | v
2812 * | Certificate | WAIT_CV
2813 * | | | Recv
2814 * | v | CertificateVerify
2815 * +-> WAIT_FINISHED <---+
2816 * | Recv Finished
2817 *
2818 *
2819 * The following function handles the state changes after WAIT_FLIGHT2 in the
Jerry Yu3be85072023-12-04 09:58:54 +08002820 * above diagram. We are not going to receive early data related messages
2821 * anymore, prepare to receive the first handshake message of the client
2822 * second flight.
Jerry Yu9b72e392023-12-01 16:27:08 +08002823 */
Jerry Yu3be85072023-12-04 09:58:54 +08002824static void ssl_tls13_prepare_for_handshake_second_flight(
2825 mbedtls_ssl_context *ssl)
Jerry Yu9b72e392023-12-01 16:27:08 +08002826{
Jerry Yu9b72e392023-12-01 16:27:08 +08002827 if (ssl->handshake->certificate_request_sent) {
2828 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_CLIENT_CERTIFICATE);
2829 } else {
Jerry Yu42020fb2023-12-05 17:35:53 +08002830 MBEDTLS_SSL_DEBUG_MSG(2, ("skip parse certificate"));
Jerry Yuebb1b1d2023-12-05 11:02:15 +08002831 MBEDTLS_SSL_DEBUG_MSG(2, ("skip parse certificate verify"));
Jerry Yuebb1b1d2023-12-05 11:02:15 +08002832
Jerry Yu9b72e392023-12-01 16:27:08 +08002833 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_CLIENT_FINISHED);
2834 }
Jerry Yu9b72e392023-12-01 16:27:08 +08002835}
2836
Jerry Yu83da34e2022-04-16 13:59:52 +08002837/*
Jerry Yud6e253d2022-05-18 13:59:24 +08002838 * Handler for MBEDTLS_SSL_SERVER_FINISHED
Jerry Yu69dd8d42022-04-16 12:51:26 +08002839 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002840MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002841static int ssl_tls13_write_server_finished(mbedtls_ssl_context *ssl)
Jerry Yu69dd8d42022-04-16 12:51:26 +08002842{
Jerry Yud6e253d2022-05-18 13:59:24 +08002843 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yu27bdc7c2022-04-16 13:33:27 +08002844
Gilles Peskine449bd832023-01-11 14:50:10 +01002845 ret = mbedtls_ssl_tls13_write_finished_message(ssl);
2846 if (ret != 0) {
2847 return ret;
2848 }
Jerry Yu27bdc7c2022-04-16 13:33:27 +08002849
Gilles Peskine449bd832023-01-11 14:50:10 +01002850 ret = mbedtls_ssl_tls13_compute_application_transform(ssl);
2851 if (ret != 0) {
Jerry Yue3d67cb2022-05-19 15:33:10 +08002852 MBEDTLS_SSL_PEND_FATAL_ALERT(
Gilles Peskine449bd832023-01-11 14:50:10 +01002853 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE,
2854 MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE);
2855 return ret;
Jerry Yue3d67cb2022-05-19 15:33:10 +08002856 }
XiaokangQianc3017f62022-05-13 05:55:41 +00002857
Jerry Yu87b5ed42022-12-12 13:07:07 +08002858#if defined(MBEDTLS_SSL_EARLY_DATA)
Ronald Cron78a38f62024-02-01 18:30:31 +01002859 if (ssl->handshake->early_data_accepted) {
Jerry Yu0af63dc2023-12-01 17:14:51 +08002860 /* See RFC 8446 section A.2 for more information */
Jerry Yu87b5ed42022-12-12 13:07:07 +08002861 MBEDTLS_SSL_DEBUG_MSG(
2862 1, ("Switch to early keys for inbound traffic. "
2863 "( K_recv = early data )"));
2864 mbedtls_ssl_set_inbound_transform(
2865 ssl, ssl->handshake->transform_earlydata);
2866 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_END_OF_EARLY_DATA);
2867 return 0;
2868 }
2869#endif /* MBEDTLS_SSL_EARLY_DATA */
Jerry Yu0af63dc2023-12-01 17:14:51 +08002870 MBEDTLS_SSL_DEBUG_MSG(
2871 1, ("Switch to handshake keys for inbound traffic "
2872 "( K_recv = handshake )"));
Gilles Peskine449bd832023-01-11 14:50:10 +01002873 mbedtls_ssl_set_inbound_transform(ssl, ssl->handshake->transform_handshake);
XiaokangQian189ded22022-05-10 08:12:17 +00002874
Jerry Yu3be85072023-12-04 09:58:54 +08002875 ssl_tls13_prepare_for_handshake_second_flight(ssl);
Jerry Yu7d8c3fe2022-12-12 12:59:44 +08002876
2877 return 0;
2878}
2879
Jerry Yu87b5ed42022-12-12 13:07:07 +08002880#if defined(MBEDTLS_SSL_EARLY_DATA)
2881/*
Jerry Yu59d420f2023-12-01 16:30:34 +08002882 * Handler for MBEDTLS_SSL_END_OF_EARLY_DATA
Jerry Yu87b5ed42022-12-12 13:07:07 +08002883 */
Jerry Yu3be85072023-12-04 09:58:54 +08002884#define SSL_GOT_END_OF_EARLY_DATA 0
Jerry Yub55f9eb2023-12-05 10:27:17 +08002885#define SSL_GOT_EARLY_DATA 1
Jerry Yud5c34962023-12-01 16:32:31 +08002886/* Coordination:
Jerry Yu3be85072023-12-04 09:58:54 +08002887 * Deals with the ambiguity of not knowing if the next message is an
2888 * EndOfEarlyData message or an application message containing early data.
Jerry Yud5c34962023-12-01 16:32:31 +08002889 * Returns a negative code on failure, or
Jerry Yu3be85072023-12-04 09:58:54 +08002890 * - SSL_GOT_END_OF_EARLY_DATA
Jerry Yub55f9eb2023-12-05 10:27:17 +08002891 * - SSL_GOT_EARLY_DATA
Jerry Yud5c34962023-12-01 16:32:31 +08002892 * indicating which message is received.
2893 */
2894MBEDTLS_CHECK_RETURN_CRITICAL
2895static int ssl_tls13_end_of_early_data_coordinate(mbedtls_ssl_context *ssl)
2896{
2897 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yub4ed4602023-12-01 16:34:00 +08002898
2899 if ((ret = mbedtls_ssl_read_record(ssl, 0)) != 0) {
2900 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_read_record", ret);
2901 return ret;
2902 }
2903 ssl->keep_current_message = 1;
2904
2905 if (ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
2906 ssl->in_msg[0] == MBEDTLS_SSL_HS_END_OF_EARLY_DATA) {
Jerry Yub55f9eb2023-12-05 10:27:17 +08002907 MBEDTLS_SSL_DEBUG_MSG(3, ("Received an end_of_early_data message."));
Jerry Yu3be85072023-12-04 09:58:54 +08002908 return SSL_GOT_END_OF_EARLY_DATA;
Jerry Yub4ed4602023-12-01 16:34:00 +08002909 }
2910
2911 if (ssl->in_msgtype == MBEDTLS_SSL_MSG_APPLICATION_DATA) {
Jerry Yub55f9eb2023-12-05 10:27:17 +08002912 MBEDTLS_SSL_DEBUG_MSG(3, ("Received early data"));
Jerry Yu6a5904d2023-12-06 17:11:12 +08002913 /* RFC 8446 section 4.6.1
2914 *
2915 * A server receiving more than max_early_data_size bytes of 0-RTT data
2916 * SHOULD terminate the connection with an "unexpected_message" alert.
2917 *
2918 * TODO: Add received data size check here.
2919 */
Ronald Croned7d4bf2024-01-31 07:55:19 +01002920 if (ssl->in_offt == NULL) {
2921 /* Set the reading pointer */
2922 ssl->in_offt = ssl->in_msg;
2923 }
Jerry Yub55f9eb2023-12-05 10:27:17 +08002924 return SSL_GOT_EARLY_DATA;
Jerry Yub4ed4602023-12-01 16:34:00 +08002925 }
2926
Jerry Yu7bb40a32023-12-04 10:04:15 +08002927 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE,
2928 MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE);
Jerry Yub4ed4602023-12-01 16:34:00 +08002929 return MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE;
Jerry Yud5c34962023-12-01 16:32:31 +08002930}
2931
2932MBEDTLS_CHECK_RETURN_CRITICAL
2933static int ssl_tls13_parse_end_of_early_data(mbedtls_ssl_context *ssl,
2934 const unsigned char *buf,
2935 const unsigned char *end)
2936{
Jerry Yu75c9ab72023-12-01 16:41:40 +08002937 /* RFC 8446 section 4.5
2938 *
2939 * struct {} EndOfEarlyData;
2940 */
Jerry Yufbf03992023-12-04 10:00:37 +08002941 if (buf != end) {
2942 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR,
2943 MBEDTLS_ERR_SSL_DECODE_ERROR);
2944 return MBEDTLS_ERR_SSL_DECODE_ERROR;
2945 }
2946 return 0;
Jerry Yud5c34962023-12-01 16:32:31 +08002947}
2948
Jerry Yud5c34962023-12-01 16:32:31 +08002949/*
2950 * RFC 8446 section A.2
2951 *
2952 * | Send ServerHello
2953 * | K_send = handshake
2954 * | Send EncryptedExtensions
2955 * | [Send CertificateRequest]
2956 * Can send | [Send Certificate + CertificateVerify]
2957 * app data | Send Finished
2958 * after --> | K_send = application
2959 * here +--------+--------+
2960 * No 0-RTT | | 0-RTT
2961 * | |
2962 * K_recv = handshake | | K_recv = early data
2963 * [Skip decrypt errors] | +------> WAIT_EOED -+
2964 * | | Recv | | Recv EndOfEarlyData
2965 * | | early data | | K_recv = handshake
2966 * | +------------+ |
2967 * | |
2968 * +> WAIT_FLIGHT2 <--------+
2969 * |
2970 * +--------+--------+
2971 * No auth | | Client auth
2972 * | |
2973 * | v
2974 * | WAIT_CERT
2975 * | Recv | | Recv Certificate
2976 * | empty | v
2977 * | Certificate | WAIT_CV
2978 * | | | Recv
2979 * | v | CertificateVerify
2980 * +-> WAIT_FINISHED <---+
2981 * | Recv Finished
2982 *
2983 * The function handles actions and state changes from 0-RTT to WAIT_FLIGHT2 in
2984 * the above diagram.
2985 */
Jerry Yu87b5ed42022-12-12 13:07:07 +08002986MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yu59d420f2023-12-01 16:30:34 +08002987static int ssl_tls13_process_end_of_early_data(mbedtls_ssl_context *ssl)
Jerry Yu87b5ed42022-12-12 13:07:07 +08002988{
2989 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yud5c34962023-12-01 16:32:31 +08002990
2991 MBEDTLS_SSL_DEBUG_MSG(2, ("=> ssl_tls13_process_end_of_early_data"));
2992
2993 MBEDTLS_SSL_PROC_CHK_NEG(ssl_tls13_end_of_early_data_coordinate(ssl));
2994
Jerry Yu3be85072023-12-04 09:58:54 +08002995 if (ret == SSL_GOT_END_OF_EARLY_DATA) {
Jerry Yud5c34962023-12-01 16:32:31 +08002996 unsigned char *buf;
2997 size_t buf_len;
2998
2999 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_tls13_fetch_handshake_msg(
3000 ssl, MBEDTLS_SSL_HS_END_OF_EARLY_DATA,
3001 &buf, &buf_len));
3002
3003 MBEDTLS_SSL_PROC_CHK(ssl_tls13_parse_end_of_early_data(
3004 ssl, buf, buf + buf_len));
3005
Jerry Yue9655122023-12-01 16:44:40 +08003006 MBEDTLS_SSL_DEBUG_MSG(
3007 1, ("Switch to handshake keys for inbound traffic"
3008 "( K_recv = handshake )"));
3009 mbedtls_ssl_set_inbound_transform(
3010 ssl, ssl->handshake->transform_handshake);
3011
Jerry Yud5c34962023-12-01 16:32:31 +08003012 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_add_hs_msg_to_checksum(
3013 ssl, MBEDTLS_SSL_HS_END_OF_EARLY_DATA,
3014 buf, buf_len));
Jerry Yue9655122023-12-01 16:44:40 +08003015
Jerry Yu3be85072023-12-04 09:58:54 +08003016 ssl_tls13_prepare_for_handshake_second_flight(ssl);
Jerry Yud5c34962023-12-01 16:32:31 +08003017
Jerry Yub55f9eb2023-12-05 10:27:17 +08003018 } else if (ret == SSL_GOT_EARLY_DATA) {
Jerry Yud9ca3542023-12-06 17:23:52 +08003019 ret = MBEDTLS_ERR_SSL_RECEIVED_EARLY_DATA;
3020 goto cleanup;
Jerry Yud5c34962023-12-01 16:32:31 +08003021 } else {
3022 MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
3023 ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
3024 goto cleanup;
3025 }
3026
Jerry Yud5c34962023-12-01 16:32:31 +08003027cleanup:
3028 MBEDTLS_SSL_DEBUG_MSG(2, ("<= ssl_tls13_process_end_of_early_data"));
Jerry Yu87b5ed42022-12-12 13:07:07 +08003029 return ret;
3030}
3031#endif /* MBEDTLS_SSL_EARLY_DATA */
3032
Jerry Yu69dd8d42022-04-16 12:51:26 +08003033/*
Jerry Yud6e253d2022-05-18 13:59:24 +08003034 * Handler for MBEDTLS_SSL_CLIENT_FINISHED
Jerry Yu69dd8d42022-04-16 12:51:26 +08003035 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02003036MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01003037static int ssl_tls13_process_client_finished(mbedtls_ssl_context *ssl)
Jerry Yu69dd8d42022-04-16 12:51:26 +08003038{
Jerry Yud6e253d2022-05-18 13:59:24 +08003039 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
3040
Gilles Peskine449bd832023-01-11 14:50:10 +01003041 ret = mbedtls_ssl_tls13_process_finished_message(ssl);
3042 if (ret != 0) {
3043 return ret;
Jerry Yue3d67cb2022-05-19 15:33:10 +08003044 }
3045
Gilles Peskine449bd832023-01-11 14:50:10 +01003046 ret = mbedtls_ssl_tls13_compute_resumption_master_secret(ssl);
3047 if (ret != 0) {
Xiaokang Qian9f1747b2023-03-30 02:50:04 +00003048 MBEDTLS_SSL_DEBUG_RET(
3049 1, "mbedtls_ssl_tls13_compute_resumption_master_secret", ret);
Gilles Peskine449bd832023-01-11 14:50:10 +01003050 }
3051
3052 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_HANDSHAKE_WRAPUP);
3053 return 0;
Jerry Yu69dd8d42022-04-16 12:51:26 +08003054}
3055
3056/*
Jerry Yud6e253d2022-05-18 13:59:24 +08003057 * Handler for MBEDTLS_SSL_HANDSHAKE_WRAPUP
Jerry Yu69dd8d42022-04-16 12:51:26 +08003058 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02003059MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01003060static int ssl_tls13_handshake_wrapup(mbedtls_ssl_context *ssl)
Jerry Yu69dd8d42022-04-16 12:51:26 +08003061{
Gilles Peskine449bd832023-01-11 14:50:10 +01003062 MBEDTLS_SSL_DEBUG_MSG(2, ("handshake: done"));
Jerry Yu03ed50b2022-04-16 17:13:30 +08003063
Gilles Peskine449bd832023-01-11 14:50:10 +01003064 mbedtls_ssl_tls13_handshake_wrapup(ssl);
Jerry Yue67bef42022-07-07 07:29:42 +00003065
Pengyu Lv3643fdb2023-01-12 16:46:28 +08003066#if defined(MBEDTLS_SSL_SESSION_TICKETS) && \
3067 defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED)
Pengyu Lva1aa31b2022-12-13 13:49:59 +08003068/* TODO: Remove the check of SOME_PSK_ENABLED since SESSION_TICKETS requires
3069 * SOME_PSK_ENABLED to be enabled. Here is just to make CI happy. It is
3070 * expected to be resolved with issue#6395.
3071 */
Pengyu Lvc7af2c42022-12-01 16:33:00 +08003072 /* Sent NewSessionTicket message only when client supports PSK */
Pengyu Lvb2cfafb2023-11-14 13:56:13 +08003073 if (mbedtls_ssl_tls13_is_some_psk_supported(ssl)) {
Xiaokang Qian9f1747b2023-03-30 02:50:04 +00003074 mbedtls_ssl_handshake_set_state(
3075 ssl, MBEDTLS_SSL_TLS1_3_NEW_SESSION_TICKET);
Pengyu Lvc7af2c42022-12-01 16:33:00 +08003076 } else
Jerry Yufca4d572022-07-21 10:37:48 +08003077#endif
Pengyu Lv3643fdb2023-01-12 16:46:28 +08003078 {
3079 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_HANDSHAKE_OVER);
3080 }
Gilles Peskine449bd832023-01-11 14:50:10 +01003081 return 0;
Jerry Yu69dd8d42022-04-16 12:51:26 +08003082}
3083
3084/*
Jerry Yua8d3c502022-10-30 14:51:23 +08003085 * Handler for MBEDTLS_SSL_TLS1_3_NEW_SESSION_TICKET
Jerry Yue67bef42022-07-07 07:29:42 +00003086 */
3087#define SSL_NEW_SESSION_TICKET_SKIP 0
3088#define SSL_NEW_SESSION_TICKET_WRITE 1
3089MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01003090static int ssl_tls13_write_new_session_ticket_coordinate(mbedtls_ssl_context *ssl)
Jerry Yue67bef42022-07-07 07:29:42 +00003091{
3092 /* Check whether the use of session tickets is enabled */
Gilles Peskine449bd832023-01-11 14:50:10 +01003093 if (ssl->conf->f_ticket_write == NULL) {
3094 MBEDTLS_SSL_DEBUG_MSG(2, ("NewSessionTicket: disabled,"
3095 " callback is not set"));
3096 return SSL_NEW_SESSION_TICKET_SKIP;
Jerry Yud0766ec2022-09-22 10:46:57 +08003097 }
Gilles Peskine449bd832023-01-11 14:50:10 +01003098 if (ssl->conf->new_session_tickets_count == 0) {
3099 MBEDTLS_SSL_DEBUG_MSG(2, ("NewSessionTicket: disabled,"
3100 " configured count is zero"));
3101 return SSL_NEW_SESSION_TICKET_SKIP;
Jerry Yud0766ec2022-09-22 10:46:57 +08003102 }
3103
Gilles Peskine449bd832023-01-11 14:50:10 +01003104 if (ssl->handshake->new_session_tickets_count == 0) {
3105 MBEDTLS_SSL_DEBUG_MSG(2, ("NewSessionTicket: all tickets have "
3106 "been sent."));
3107 return SSL_NEW_SESSION_TICKET_SKIP;
Jerry Yue67bef42022-07-07 07:29:42 +00003108 }
3109
Gilles Peskine449bd832023-01-11 14:50:10 +01003110 return SSL_NEW_SESSION_TICKET_WRITE;
Jerry Yue67bef42022-07-07 07:29:42 +00003111}
3112
3113#if defined(MBEDTLS_SSL_SESSION_TICKETS)
3114MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01003115static int ssl_tls13_prepare_new_session_ticket(mbedtls_ssl_context *ssl,
3116 unsigned char *ticket_nonce,
3117 size_t ticket_nonce_size)
Jerry Yue67bef42022-07-07 07:29:42 +00003118{
3119 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
3120 mbedtls_ssl_session *session = ssl->session;
3121 mbedtls_ssl_ciphersuite_t *ciphersuite_info;
3122 psa_algorithm_t psa_hash_alg;
3123 int hash_length;
3124
Gilles Peskine449bd832023-01-11 14:50:10 +01003125 MBEDTLS_SSL_DEBUG_MSG(2, ("=> prepare NewSessionTicket msg"));
Jerry Yue67bef42022-07-07 07:29:42 +00003126
Pengyu Lv9f926952022-11-17 15:22:33 +08003127 /* Set ticket_flags depends on the advertised psk key exchange mode */
Pengyu Lv94a42cc2023-12-06 10:04:17 +08003128 mbedtls_ssl_tls13_session_clear_ticket_flags(
Pengyu Lv9eacb442022-12-09 14:39:19 +08003129 session, MBEDTLS_SSL_TLS1_3_TICKET_FLAGS_MASK);
Pengyu Lve6487fe2022-12-06 09:30:29 +08003130#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED)
Pengyu Lv94a42cc2023-12-06 10:04:17 +08003131 mbedtls_ssl_tls13_session_set_ticket_flags(
Pengyu Lv9eacb442022-12-09 14:39:19 +08003132 session, ssl->handshake->tls13_kex_modes);
Pengyu Lve6487fe2022-12-06 09:30:29 +08003133#endif
Jerry Yu95648b02023-12-06 15:03:34 +08003134
3135#if defined(MBEDTLS_SSL_EARLY_DATA)
3136 if (ssl->conf->early_data_enabled == MBEDTLS_SSL_EARLY_DATA_ENABLED &&
3137 ssl->conf->max_early_data_size > 0) {
Pengyu Lv94a42cc2023-12-06 10:04:17 +08003138 mbedtls_ssl_tls13_session_set_ticket_flags(
Jerry Yu95648b02023-12-06 15:03:34 +08003139 session, MBEDTLS_SSL_TLS1_3_TICKET_ALLOW_EARLY_DATA);
3140 }
3141#endif /* MBEDTLS_SSL_EARLY_DATA */
3142
Pengyu Lv4938a562023-01-16 11:28:49 +08003143 MBEDTLS_SSL_PRINT_TICKET_FLAGS(4, session->ticket_flags);
Pengyu Lv9f926952022-11-17 15:22:33 +08003144
Jerry Yue67bef42022-07-07 07:29:42 +00003145 /* Generate ticket_age_add */
Gilles Peskine449bd832023-01-11 14:50:10 +01003146 if ((ret = ssl->conf->f_rng(ssl->conf->p_rng,
3147 (unsigned char *) &session->ticket_age_add,
3148 sizeof(session->ticket_age_add)) != 0)) {
3149 MBEDTLS_SSL_DEBUG_RET(1, "generate_ticket_age_add", ret);
3150 return ret;
Jerry Yue67bef42022-07-07 07:29:42 +00003151 }
Gilles Peskine449bd832023-01-11 14:50:10 +01003152 MBEDTLS_SSL_DEBUG_MSG(3, ("ticket_age_add: %u",
3153 (unsigned int) session->ticket_age_add));
Jerry Yue67bef42022-07-07 07:29:42 +00003154
3155 /* Generate ticket_nonce */
Gilles Peskine449bd832023-01-11 14:50:10 +01003156 ret = ssl->conf->f_rng(ssl->conf->p_rng, ticket_nonce, ticket_nonce_size);
3157 if (ret != 0) {
3158 MBEDTLS_SSL_DEBUG_RET(1, "generate_ticket_nonce", ret);
3159 return ret;
Jerry Yue67bef42022-07-07 07:29:42 +00003160 }
Gilles Peskine449bd832023-01-11 14:50:10 +01003161 MBEDTLS_SSL_DEBUG_BUF(3, "ticket_nonce:",
3162 ticket_nonce, ticket_nonce_size);
Jerry Yue67bef42022-07-07 07:29:42 +00003163
3164 ciphersuite_info =
Gilles Peskine449bd832023-01-11 14:50:10 +01003165 (mbedtls_ssl_ciphersuite_t *) ssl->handshake->ciphersuite_info;
Dave Rodgman2eab4622023-10-05 13:30:37 +01003166 psa_hash_alg = mbedtls_md_psa_alg_from_type((mbedtls_md_type_t) ciphersuite_info->mac);
Gilles Peskine449bd832023-01-11 14:50:10 +01003167 hash_length = PSA_HASH_LENGTH(psa_hash_alg);
3168 if (hash_length == -1 ||
3169 (size_t) hash_length > sizeof(session->resumption_key)) {
3170 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Jerry Yufca4d572022-07-21 10:37:48 +08003171 }
3172
Jerry Yue67bef42022-07-07 07:29:42 +00003173 /* In this code the psk key length equals the length of the hash */
3174 session->resumption_key_len = hash_length;
3175 session->ciphersuite = ciphersuite_info->id;
3176
3177 /* Compute resumption key
3178 *
3179 * HKDF-Expand-Label( resumption_master_secret,
3180 * "resumption", ticket_nonce, Hash.length )
3181 */
3182 ret = mbedtls_ssl_tls13_hkdf_expand_label(
Gilles Peskine449bd832023-01-11 14:50:10 +01003183 psa_hash_alg,
3184 session->app_secrets.resumption_master_secret,
3185 hash_length,
3186 MBEDTLS_SSL_TLS1_3_LBL_WITH_LEN(resumption),
3187 ticket_nonce,
3188 ticket_nonce_size,
3189 session->resumption_key,
3190 hash_length);
Jerry Yue67bef42022-07-07 07:29:42 +00003191
Gilles Peskine449bd832023-01-11 14:50:10 +01003192 if (ret != 0) {
3193 MBEDTLS_SSL_DEBUG_RET(2,
3194 "Creating the ticket-resumed PSK failed",
3195 ret);
3196 return ret;
Jerry Yue67bef42022-07-07 07:29:42 +00003197 }
Gilles Peskine449bd832023-01-11 14:50:10 +01003198 MBEDTLS_SSL_DEBUG_BUF(3, "Ticket-resumed PSK",
3199 session->resumption_key,
3200 session->resumption_key_len);
Jerry Yue67bef42022-07-07 07:29:42 +00003201
Gilles Peskine449bd832023-01-11 14:50:10 +01003202 MBEDTLS_SSL_DEBUG_BUF(3, "resumption_master_secret",
3203 session->app_secrets.resumption_master_secret,
3204 hash_length);
Jerry Yue67bef42022-07-07 07:29:42 +00003205
Gilles Peskine449bd832023-01-11 14:50:10 +01003206 return 0;
Jerry Yue67bef42022-07-07 07:29:42 +00003207}
3208
3209/* This function creates a NewSessionTicket message in the following format:
3210 *
3211 * struct {
3212 * uint32 ticket_lifetime;
3213 * uint32 ticket_age_add;
3214 * opaque ticket_nonce<0..255>;
3215 * opaque ticket<1..2^16-1>;
3216 * Extension extensions<0..2^16-2>;
3217 * } NewSessionTicket;
3218 *
3219 * The ticket inside the NewSessionTicket message is an encrypted container
3220 * carrying the necessary information so that the server is later able to
3221 * re-start the communication.
3222 *
3223 * The following fields are placed inside the ticket by the
3224 * f_ticket_write() function:
3225 *
Jerry Yu0069abc2023-11-23 21:07:28 +08003226 * - creation time (ticket_creation_time)
3227 * - flags (ticket_flags)
Jerry Yue67bef42022-07-07 07:29:42 +00003228 * - age add (ticket_age_add)
Jerry Yu0069abc2023-11-23 21:07:28 +08003229 * - key (resumption_key)
3230 * - key length (resumption_key_len)
Jerry Yue67bef42022-07-07 07:29:42 +00003231 * - ciphersuite (ciphersuite)
Jerry Yu0069abc2023-11-23 21:07:28 +08003232 * - max_early_data_size (max_early_data_size)
Jerry Yue67bef42022-07-07 07:29:42 +00003233 */
3234MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01003235static int ssl_tls13_write_new_session_ticket_body(mbedtls_ssl_context *ssl,
3236 unsigned char *buf,
3237 unsigned char *end,
3238 size_t *out_len,
3239 unsigned char *ticket_nonce,
3240 size_t ticket_nonce_size)
Jerry Yue67bef42022-07-07 07:29:42 +00003241{
3242 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
3243 unsigned char *p = buf;
3244 mbedtls_ssl_session *session = ssl->session;
3245 size_t ticket_len;
3246 uint32_t ticket_lifetime;
Jerry Yu01da35e2022-12-12 15:09:22 +08003247 unsigned char *p_extensions_len;
Jerry Yue67bef42022-07-07 07:29:42 +00003248
3249 *out_len = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01003250 MBEDTLS_SSL_DEBUG_MSG(2, ("=> write NewSessionTicket msg"));
Jerry Yue67bef42022-07-07 07:29:42 +00003251
3252 /*
3253 * ticket_lifetime 4 bytes
3254 * ticket_age_add 4 bytes
3255 * ticket_nonce 1 + ticket_nonce_size bytes
3256 * ticket >=2 bytes
3257 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003258 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 4 + 4 + 1 + ticket_nonce_size + 2);
Jerry Yue67bef42022-07-07 07:29:42 +00003259
3260 /* Generate ticket and ticket_lifetime */
Ronald Cron3c0072b2023-11-22 10:00:14 +01003261#if defined(MBEDTLS_HAVE_TIME)
3262 session->ticket_creation_time = mbedtls_ms_time();
3263#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01003264 ret = ssl->conf->f_ticket_write(ssl->conf->p_ticket,
3265 session,
3266 p + 9 + ticket_nonce_size + 2,
3267 end,
3268 &ticket_len,
3269 &ticket_lifetime);
3270 if (ret != 0) {
3271 MBEDTLS_SSL_DEBUG_RET(1, "write_ticket", ret);
3272 return ret;
Jerry Yue67bef42022-07-07 07:29:42 +00003273 }
Jerry Yuce794882023-11-22 15:01:18 +08003274
3275 /* RFC 8446 section 4.6.1
3276 *
Jerry Yue67bef42022-07-07 07:29:42 +00003277 * ticket_lifetime: Indicates the lifetime in seconds as a 32-bit
Jerry Yuce794882023-11-22 15:01:18 +08003278 * unsigned integer in network byte order from the time of ticket
3279 * issuance. Servers MUST NOT use any value greater than
3280 * 604800 seconds (7 days) ...
Jerry Yue67bef42022-07-07 07:29:42 +00003281 */
Jerry Yu8e0174a2023-11-10 13:58:16 +08003282 if (ticket_lifetime > MBEDTLS_SSL_TLS1_3_MAX_ALLOWED_TICKET_LIFETIME) {
Jerry Yuce794882023-11-22 15:01:18 +08003283 MBEDTLS_SSL_DEBUG_MSG(
3284 1, ("Ticket lifetime (%u) is greater than 7 days.",
3285 (unsigned int) ticket_lifetime));
3286 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Gilles Peskine449bd832023-01-11 14:50:10 +01003287 }
Jerry Yuce794882023-11-22 15:01:18 +08003288
Gilles Peskine449bd832023-01-11 14:50:10 +01003289 MBEDTLS_PUT_UINT32_BE(ticket_lifetime, p, 0);
3290 MBEDTLS_SSL_DEBUG_MSG(3, ("ticket_lifetime: %u",
3291 (unsigned int) ticket_lifetime));
Jerry Yue67bef42022-07-07 07:29:42 +00003292
3293 /* Write ticket_age_add */
Gilles Peskine449bd832023-01-11 14:50:10 +01003294 MBEDTLS_PUT_UINT32_BE(session->ticket_age_add, p, 4);
3295 MBEDTLS_SSL_DEBUG_MSG(3, ("ticket_age_add: %u",
3296 (unsigned int) session->ticket_age_add));
Jerry Yue67bef42022-07-07 07:29:42 +00003297
3298 /* Write ticket_nonce */
Gilles Peskine449bd832023-01-11 14:50:10 +01003299 p[8] = (unsigned char) ticket_nonce_size;
3300 if (ticket_nonce_size > 0) {
3301 memcpy(p + 9, ticket_nonce, ticket_nonce_size);
Jerry Yue67bef42022-07-07 07:29:42 +00003302 }
3303 p += 9 + ticket_nonce_size;
3304
3305 /* Write ticket */
Gilles Peskine449bd832023-01-11 14:50:10 +01003306 MBEDTLS_PUT_UINT16_BE(ticket_len, p, 0);
Jerry Yue67bef42022-07-07 07:29:42 +00003307 p += 2;
Gilles Peskine449bd832023-01-11 14:50:10 +01003308 MBEDTLS_SSL_DEBUG_BUF(4, "ticket", p, ticket_len);
Jerry Yue67bef42022-07-07 07:29:42 +00003309 p += ticket_len;
3310
3311 /* Ticket Extensions
3312 *
Jerry Yu01da35e2022-12-12 15:09:22 +08003313 * Extension extensions<0..2^16-2>;
Jerry Yue67bef42022-07-07 07:29:42 +00003314 */
Jerry Yuedab6372022-10-31 14:37:31 +08003315 ssl->handshake->sent_extensions = MBEDTLS_SSL_EXT_MASK_NONE;
3316
Gilles Peskine449bd832023-01-11 14:50:10 +01003317 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 2);
Jerry Yu01da35e2022-12-12 15:09:22 +08003318 p_extensions_len = p;
Jerry Yue67bef42022-07-07 07:29:42 +00003319 p += 2;
3320
Jerry Yu01da35e2022-12-12 15:09:22 +08003321#if defined(MBEDTLS_SSL_EARLY_DATA)
Pengyu Lv94a42cc2023-12-06 10:04:17 +08003322 if (mbedtls_ssl_tls13_session_ticket_allow_early_data(session)) {
Jerry Yu95648b02023-12-06 15:03:34 +08003323 size_t output_len;
3324
Jerry Yuf135bac2023-11-23 18:10:51 +08003325 if ((ret = mbedtls_ssl_tls13_write_early_data_ext(
Jerry Yuc59c5862023-12-05 10:40:49 +08003326 ssl, 1, p, end, &output_len)) != 0) {
Jerry Yuf135bac2023-11-23 18:10:51 +08003327 MBEDTLS_SSL_DEBUG_RET(
3328 1, "mbedtls_ssl_tls13_write_early_data_ext", ret);
3329 return ret;
3330 }
3331 p += output_len;
Jerry Yu9e7f9bc2023-11-27 16:52:07 +08003332 } else {
3333 MBEDTLS_SSL_DEBUG_MSG(
3334 4, ("early_data not allowed, "
3335 "skip early_data extension in NewSessionTicket"));
Jerry Yu01da35e2022-12-12 15:09:22 +08003336 }
Jerry Yuf135bac2023-11-23 18:10:51 +08003337
Jerry Yu01da35e2022-12-12 15:09:22 +08003338#endif /* MBEDTLS_SSL_EARLY_DATA */
3339
3340 MBEDTLS_PUT_UINT16_BE(p - p_extensions_len - 2, p_extensions_len, 0);
3341
Jerry Yue67bef42022-07-07 07:29:42 +00003342 *out_len = p - buf;
Gilles Peskine449bd832023-01-11 14:50:10 +01003343 MBEDTLS_SSL_DEBUG_BUF(4, "ticket", buf, *out_len);
3344 MBEDTLS_SSL_DEBUG_MSG(2, ("<= write new session ticket"));
Jerry Yue67bef42022-07-07 07:29:42 +00003345
Jerry Yu7de2ff02022-11-08 21:43:46 +08003346 MBEDTLS_SSL_PRINT_EXTS(
Gilles Peskine449bd832023-01-11 14:50:10 +01003347 3, MBEDTLS_SSL_HS_NEW_SESSION_TICKET, ssl->handshake->sent_extensions);
Jerry Yu4b8f2f72022-10-31 13:31:22 +08003348
Gilles Peskine449bd832023-01-11 14:50:10 +01003349 return 0;
Jerry Yue67bef42022-07-07 07:29:42 +00003350}
3351
3352/*
Jerry Yua8d3c502022-10-30 14:51:23 +08003353 * Handler for MBEDTLS_SSL_TLS1_3_NEW_SESSION_TICKET
Jerry Yue67bef42022-07-07 07:29:42 +00003354 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003355static int ssl_tls13_write_new_session_ticket(mbedtls_ssl_context *ssl)
Jerry Yue67bef42022-07-07 07:29:42 +00003356{
3357 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
3358
Gilles Peskine449bd832023-01-11 14:50:10 +01003359 MBEDTLS_SSL_PROC_CHK_NEG(ssl_tls13_write_new_session_ticket_coordinate(ssl));
Jerry Yue67bef42022-07-07 07:29:42 +00003360
Gilles Peskine449bd832023-01-11 14:50:10 +01003361 if (ret == SSL_NEW_SESSION_TICKET_WRITE) {
Jerry Yue67bef42022-07-07 07:29:42 +00003362 unsigned char ticket_nonce[MBEDTLS_SSL_TLS1_3_TICKET_NONCE_LENGTH];
3363 unsigned char *buf;
3364 size_t buf_len, msg_len;
3365
Gilles Peskine449bd832023-01-11 14:50:10 +01003366 MBEDTLS_SSL_PROC_CHK(ssl_tls13_prepare_new_session_ticket(
3367 ssl, ticket_nonce, sizeof(ticket_nonce)));
Jerry Yue67bef42022-07-07 07:29:42 +00003368
Xiaokang Qian9f1747b2023-03-30 02:50:04 +00003369 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_start_handshake_msg(
3370 ssl, MBEDTLS_SSL_HS_NEW_SESSION_TICKET,
3371 &buf, &buf_len));
Jerry Yue67bef42022-07-07 07:29:42 +00003372
Gilles Peskine449bd832023-01-11 14:50:10 +01003373 MBEDTLS_SSL_PROC_CHK(ssl_tls13_write_new_session_ticket_body(
3374 ssl, buf, buf + buf_len, &msg_len,
3375 ticket_nonce, sizeof(ticket_nonce)));
Jerry Yue67bef42022-07-07 07:29:42 +00003376
Gilles Peskine449bd832023-01-11 14:50:10 +01003377 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_finish_handshake_msg(
3378 ssl, buf_len, msg_len));
Jerry Yufca4d572022-07-21 10:37:48 +08003379
Jerry Yu359e65f2022-09-22 23:47:43 +08003380 /* Limit session tickets count to one when resumption connection.
3381 *
3382 * See document of mbedtls_ssl_conf_new_session_tickets.
3383 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003384 if (ssl->handshake->resume == 1) {
Jerry Yu359e65f2022-09-22 23:47:43 +08003385 ssl->handshake->new_session_tickets_count = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01003386 } else {
Jerry Yu359e65f2022-09-22 23:47:43 +08003387 ssl->handshake->new_session_tickets_count--;
Gilles Peskine449bd832023-01-11 14:50:10 +01003388 }
Jerry Yub7e3fa72022-09-22 11:07:18 +08003389
Jerry Yua8d3c502022-10-30 14:51:23 +08003390 mbedtls_ssl_handshake_set_state(
Gilles Peskine449bd832023-01-11 14:50:10 +01003391 ssl, MBEDTLS_SSL_TLS1_3_NEW_SESSION_TICKET_FLUSH);
3392 } else {
3393 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_HANDSHAKE_OVER);
Jerry Yue67bef42022-07-07 07:29:42 +00003394 }
3395
Jerry Yue67bef42022-07-07 07:29:42 +00003396cleanup:
3397
Gilles Peskine449bd832023-01-11 14:50:10 +01003398 return ret;
Jerry Yue67bef42022-07-07 07:29:42 +00003399}
3400#endif /* MBEDTLS_SSL_SESSION_TICKETS */
3401
3402/*
XiaokangQiane8ff3502022-04-22 02:34:40 +00003403 * TLS 1.3 State Machine -- server side
XiaokangQian7807f9f2022-02-15 10:04:37 +00003404 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003405int mbedtls_ssl_tls13_handshake_server_step(mbedtls_ssl_context *ssl)
Jerry Yub9930e72021-08-06 17:11:51 +08003406{
XiaokangQian08037552022-04-20 07:16:41 +00003407 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
XiaokangQian7807f9f2022-02-15 10:04:37 +00003408
Gilles Peskine449bd832023-01-11 14:50:10 +01003409 if (ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER || ssl->handshake == NULL) {
3410 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
3411 }
XiaokangQian7807f9f2022-02-15 10:04:37 +00003412
Gilles Peskine449bd832023-01-11 14:50:10 +01003413 MBEDTLS_SSL_DEBUG_MSG(2, ("tls13 server state: %s(%d)",
Dave Rodgman2eab4622023-10-05 13:30:37 +01003414 mbedtls_ssl_states_str((mbedtls_ssl_states) ssl->state),
Gilles Peskine449bd832023-01-11 14:50:10 +01003415 ssl->state));
Jerry Yu6e81b272021-09-27 11:16:17 +08003416
Gilles Peskine449bd832023-01-11 14:50:10 +01003417 switch (ssl->state) {
XiaokangQian7807f9f2022-02-15 10:04:37 +00003418 /* start state */
3419 case MBEDTLS_SSL_HELLO_REQUEST:
Gilles Peskine449bd832023-01-11 14:50:10 +01003420 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_CLIENT_HELLO);
XiaokangQian08037552022-04-20 07:16:41 +00003421 ret = 0;
XiaokangQian7807f9f2022-02-15 10:04:37 +00003422 break;
3423
XiaokangQian7807f9f2022-02-15 10:04:37 +00003424 case MBEDTLS_SSL_CLIENT_HELLO:
Gilles Peskine449bd832023-01-11 14:50:10 +01003425 ret = ssl_tls13_process_client_hello(ssl);
3426 if (ret != 0) {
3427 MBEDTLS_SSL_DEBUG_RET(1, "ssl_tls13_process_client_hello", ret);
3428 }
Jerry Yuf41553b2022-05-09 22:20:30 +08003429 break;
XiaokangQian7807f9f2022-02-15 10:04:37 +00003430
Jerry Yuf41553b2022-05-09 22:20:30 +08003431 case MBEDTLS_SSL_HELLO_RETRY_REQUEST:
Gilles Peskine449bd832023-01-11 14:50:10 +01003432 ret = ssl_tls13_write_hello_retry_request(ssl);
3433 if (ret != 0) {
3434 MBEDTLS_SSL_DEBUG_RET(1, "ssl_tls13_write_hello_retry_request", ret);
3435 return ret;
Jerry Yuf41553b2022-05-09 22:20:30 +08003436 }
XiaokangQian7807f9f2022-02-15 10:04:37 +00003437 break;
3438
Jerry Yu5b64ae92022-03-30 17:15:02 +08003439 case MBEDTLS_SSL_SERVER_HELLO:
Gilles Peskine449bd832023-01-11 14:50:10 +01003440 ret = ssl_tls13_write_server_hello(ssl);
Jerry Yu5b64ae92022-03-30 17:15:02 +08003441 break;
3442
Xiaofei Baicba64af2022-02-15 10:00:56 +00003443 case MBEDTLS_SSL_ENCRYPTED_EXTENSIONS:
Gilles Peskine449bd832023-01-11 14:50:10 +01003444 ret = ssl_tls13_write_encrypted_extensions(ssl);
3445 if (ret != 0) {
3446 MBEDTLS_SSL_DEBUG_RET(1, "ssl_tls13_write_encrypted_extensions", ret);
3447 return ret;
Xiaofei Baicba64af2022-02-15 10:00:56 +00003448 }
Jerry Yu48330562022-05-06 21:35:44 +08003449 break;
Jerry Yu6a2cd9e2022-05-05 11:14:19 +08003450
Ronald Cron928cbd32022-10-04 16:14:26 +02003451#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED)
Xiaofei Bai9ca09d42022-02-14 12:57:18 +00003452 case MBEDTLS_SSL_CERTIFICATE_REQUEST:
Gilles Peskine449bd832023-01-11 14:50:10 +01003453 ret = ssl_tls13_write_certificate_request(ssl);
Xiaofei Bai9ca09d42022-02-14 12:57:18 +00003454 break;
Xiaofei Bai9ca09d42022-02-14 12:57:18 +00003455
Jerry Yu83da34e2022-04-16 13:59:52 +08003456 case MBEDTLS_SSL_SERVER_CERTIFICATE:
Gilles Peskine449bd832023-01-11 14:50:10 +01003457 ret = ssl_tls13_write_server_certificate(ssl);
Jerry Yu83da34e2022-04-16 13:59:52 +08003458 break;
3459
3460 case MBEDTLS_SSL_CERTIFICATE_VERIFY:
Gilles Peskine449bd832023-01-11 14:50:10 +01003461 ret = ssl_tls13_write_certificate_verify(ssl);
Jerry Yu83da34e2022-04-16 13:59:52 +08003462 break;
Ronald Cron928cbd32022-10-04 16:14:26 +02003463#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED */
Jerry Yu83da34e2022-04-16 13:59:52 +08003464
Gilles Peskine449bd832023-01-11 14:50:10 +01003465 /*
3466 * Injection of dummy-CCS's for middlebox compatibility
3467 */
Gabor Mezei7b39bf12022-05-24 16:04:14 +02003468#if defined(MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE)
Gabor Mezeif7044ea2022-06-28 16:01:49 +02003469 case MBEDTLS_SSL_SERVER_CCS_AFTER_HELLO_RETRY_REQUEST:
Gilles Peskine449bd832023-01-11 14:50:10 +01003470 ret = mbedtls_ssl_tls13_write_change_cipher_spec(ssl);
3471 if (ret == 0) {
3472 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_CLIENT_HELLO);
3473 }
Gabor Mezei7b39bf12022-05-24 16:04:14 +02003474 break;
3475
3476 case MBEDTLS_SSL_SERVER_CCS_AFTER_SERVER_HELLO:
Ronald Crone273f722024-02-13 18:22:26 +01003477 ret = mbedtls_ssl_tls13_write_change_cipher_spec(ssl);
3478 if (ret != 0) {
3479 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01003480 }
Ronald Cronfe59ff72024-01-24 14:31:50 +01003481 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_ENCRYPTED_EXTENSIONS);
Gabor Mezei7b39bf12022-05-24 16:04:14 +02003482 break;
3483#endif /* MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE */
3484
Jerry Yu69dd8d42022-04-16 12:51:26 +08003485 case MBEDTLS_SSL_SERVER_FINISHED:
Gilles Peskine449bd832023-01-11 14:50:10 +01003486 ret = ssl_tls13_write_server_finished(ssl);
Jerry Yu69dd8d42022-04-16 12:51:26 +08003487 break;
3488
Jerry Yu87b5ed42022-12-12 13:07:07 +08003489#if defined(MBEDTLS_SSL_EARLY_DATA)
3490 case MBEDTLS_SSL_END_OF_EARLY_DATA:
Jerry Yu59d420f2023-12-01 16:30:34 +08003491 ret = ssl_tls13_process_end_of_early_data(ssl);
Jerry Yu87b5ed42022-12-12 13:07:07 +08003492 break;
3493#endif /* MBEDTLS_SSL_EARLY_DATA */
3494
Jerry Yu69dd8d42022-04-16 12:51:26 +08003495 case MBEDTLS_SSL_CLIENT_FINISHED:
Gilles Peskine449bd832023-01-11 14:50:10 +01003496 ret = ssl_tls13_process_client_finished(ssl);
Jerry Yu69dd8d42022-04-16 12:51:26 +08003497 break;
3498
Jerry Yu69dd8d42022-04-16 12:51:26 +08003499 case MBEDTLS_SSL_HANDSHAKE_WRAPUP:
Gilles Peskine449bd832023-01-11 14:50:10 +01003500 ret = ssl_tls13_handshake_wrapup(ssl);
Jerry Yu69dd8d42022-04-16 12:51:26 +08003501 break;
3502
Ronald Cron766c0cd2022-10-18 12:17:11 +02003503#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED)
XiaokangQian6b916b12022-04-25 07:29:34 +00003504 case MBEDTLS_SSL_CLIENT_CERTIFICATE:
Gilles Peskine449bd832023-01-11 14:50:10 +01003505 ret = mbedtls_ssl_tls13_process_certificate(ssl);
3506 if (ret == 0) {
3507 if (ssl->session_negotiate->peer_cert != NULL) {
Ronald Cron209cae92022-06-07 10:30:19 +02003508 mbedtls_ssl_handshake_set_state(
Gilles Peskine449bd832023-01-11 14:50:10 +01003509 ssl, MBEDTLS_SSL_CLIENT_CERTIFICATE_VERIFY);
3510 } else {
3511 MBEDTLS_SSL_DEBUG_MSG(2, ("skip parse certificate verify"));
Ronald Cron209cae92022-06-07 10:30:19 +02003512 mbedtls_ssl_handshake_set_state(
Gilles Peskine449bd832023-01-11 14:50:10 +01003513 ssl, MBEDTLS_SSL_CLIENT_FINISHED);
Ronald Cron19385882022-06-15 16:26:13 +02003514 }
XiaokangQian6b916b12022-04-25 07:29:34 +00003515 }
3516 break;
3517
3518 case MBEDTLS_SSL_CLIENT_CERTIFICATE_VERIFY:
Gilles Peskine449bd832023-01-11 14:50:10 +01003519 ret = mbedtls_ssl_tls13_process_certificate_verify(ssl);
3520 if (ret == 0) {
XiaokangQian6b916b12022-04-25 07:29:34 +00003521 mbedtls_ssl_handshake_set_state(
Gilles Peskine449bd832023-01-11 14:50:10 +01003522 ssl, MBEDTLS_SSL_CLIENT_FINISHED);
XiaokangQian6b916b12022-04-25 07:29:34 +00003523 }
3524 break;
Ronald Cron766c0cd2022-10-18 12:17:11 +02003525#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED */
XiaokangQian6b916b12022-04-25 07:29:34 +00003526
Jerry Yue67bef42022-07-07 07:29:42 +00003527#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Jerry Yua8d3c502022-10-30 14:51:23 +08003528 case MBEDTLS_SSL_TLS1_3_NEW_SESSION_TICKET:
Gilles Peskine449bd832023-01-11 14:50:10 +01003529 ret = ssl_tls13_write_new_session_ticket(ssl);
3530 if (ret != 0) {
3531 MBEDTLS_SSL_DEBUG_RET(1,
3532 "ssl_tls13_write_new_session_ticket ",
3533 ret);
Jerry Yue67bef42022-07-07 07:29:42 +00003534 }
3535 break;
Jerry Yua8d3c502022-10-30 14:51:23 +08003536 case MBEDTLS_SSL_TLS1_3_NEW_SESSION_TICKET_FLUSH:
Jerry Yue67bef42022-07-07 07:29:42 +00003537 /* This state is necessary to do the flush of the New Session
Jerry Yua8d3c502022-10-30 14:51:23 +08003538 * Ticket message written in MBEDTLS_SSL_TLS1_3_NEW_SESSION_TICKET
Jerry Yue67bef42022-07-07 07:29:42 +00003539 * as part of ssl_prepare_handshake_step.
3540 */
3541 ret = 0;
Jerry Yud4e75002022-08-09 13:33:50 +08003542
Gilles Peskine449bd832023-01-11 14:50:10 +01003543 if (ssl->handshake->new_session_tickets_count == 0) {
3544 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_HANDSHAKE_OVER);
3545 } else {
Xiaokang Qian9f1747b2023-03-30 02:50:04 +00003546 mbedtls_ssl_handshake_set_state(
3547 ssl, MBEDTLS_SSL_TLS1_3_NEW_SESSION_TICKET);
Gilles Peskine449bd832023-01-11 14:50:10 +01003548 }
Jerry Yue67bef42022-07-07 07:29:42 +00003549 break;
3550
3551#endif /* MBEDTLS_SSL_SESSION_TICKETS */
3552
XiaokangQian7807f9f2022-02-15 10:04:37 +00003553 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01003554 MBEDTLS_SSL_DEBUG_MSG(1, ("invalid state %d", ssl->state));
3555 return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
XiaokangQian7807f9f2022-02-15 10:04:37 +00003556 }
3557
Gilles Peskine449bd832023-01-11 14:50:10 +01003558 return ret;
Jerry Yub9930e72021-08-06 17:11:51 +08003559}
Jerry Yu3cc4c2a2021-08-06 16:29:08 +08003560
Jerry Yufb4b6472022-01-27 15:03:26 +08003561#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_PROTO_TLS1_3 */