blob: 4ce9670f8d9294b307b6ab248006b0233976c42a [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
Jerry Yu687101b2021-09-14 16:03:56 +080012#include "mbedtls/debug.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"
Manuel Pégourié-Gonnard02b10d82023-03-28 12:33:20 +020017#include "md_psa.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
98static int ssl_tls13_check_psk_key_exchange(mbedtls_ssl_context *ssl);
99MBEDTLS_CHECK_RETURN_CRITICAL
100static int ssl_tls13_check_psk_ephemeral_key_exchange(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 Lvdbd1e0d2023-10-31 10:08:10 +0800178 if (mbedtls_ssl_session_ticket_allow_psk_ephemeral(session) &&
Pengyu Lv29daf4a2023-10-30 17:13:30 +0800179 ssl_tls13_check_psk_ephemeral_key_exchange(ssl)) {
180 key_exchanges |= MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK_EPHEMERAL;
181 }
Pengyu Lvdbd1e0d2023-10-31 10:08:10 +0800182 if (mbedtls_ssl_session_ticket_allow_psk(session) &&
Pengyu Lv29daf4a2023-10-30 17:13:30 +0800183 ssl_tls13_check_psk_key_exchange(ssl)) {
184 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 Yu525990f2023-11-15 14:51:18 +0800567#if defined(MBEDTLS_SSL_EARLY_DATA)
568 session.max_early_data_size = ssl->conf->max_early_data_size;
569#endif
Jerry Yu82534862022-08-30 10:42:33 +0800570#endif
Jerry Yubb852022022-07-20 21:10:44 +0800571
Gilles Peskine449bd832023-01-11 14:50:10 +0100572 MBEDTLS_SSL_CHK_BUF_READ_PTR(p_identity_len, identities_end, 2 + 1 + 4);
573 identity_len = MBEDTLS_GET_UINT16_BE(p_identity_len, 0);
Jerry Yu568ec252022-07-22 21:27:34 +0800574 identity = p_identity_len + 2;
Gilles Peskine449bd832023-01-11 14:50:10 +0100575 MBEDTLS_SSL_CHK_BUF_READ_PTR(identity, identities_end, identity_len + 4);
576 obfuscated_ticket_age = MBEDTLS_GET_UINT32_BE(identity, identity_len);
Jerry Yu568ec252022-07-22 21:27:34 +0800577 p_identity_len += identity_len + 6;
Jerry Yu1c105562022-07-10 06:32:38 +0000578
Gilles Peskine449bd832023-01-11 14:50:10 +0100579 MBEDTLS_SSL_CHK_BUF_READ_PTR(p_binder_len, binders_end, 1 + 32);
Jerry Yu568ec252022-07-22 21:27:34 +0800580 binder_len = *p_binder_len;
581 binder = p_binder_len + 1;
Gilles Peskine449bd832023-01-11 14:50:10 +0100582 MBEDTLS_SSL_CHK_BUF_READ_PTR(binder, binders_end, binder_len);
Jerry Yu568ec252022-07-22 21:27:34 +0800583 p_binder_len += binder_len + 1;
Jerry Yu96a2e362022-07-21 15:11:34 +0800584
Jerry Yu96a2e362022-07-21 15:11:34 +0800585 identity_id++;
Gilles Peskine449bd832023-01-11 14:50:10 +0100586 if (matched_identity != -1) {
Jerry Yu1c105562022-07-10 06:32:38 +0000587 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +0100588 }
Jerry Yu1c105562022-07-10 06:32:38 +0000589
Jerry Yu96a2e362022-07-21 15:11:34 +0800590 ret = ssl_tls13_offered_psks_check_identity_match(
Gilles Peskine449bd832023-01-11 14:50:10 +0100591 ssl, identity, identity_len, obfuscated_ticket_age,
592 &psk_type, &session);
593 if (ret != SSL_TLS1_3_OFFERED_PSK_MATCH) {
Jerry Yu96a2e362022-07-21 15:11:34 +0800594 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +0100595 }
Jerry Yu96a2e362022-07-21 15:11:34 +0800596
Gilles Peskine449bd832023-01-11 14:50:10 +0100597 MBEDTLS_SSL_DEBUG_MSG(4, ("found matched identity"));
598 switch (psk_type) {
Jerry Yu0baf9072022-08-25 11:21:04 +0800599 case MBEDTLS_SSL_TLS1_3_PSK_EXTERNAL:
600 ret = ssl_tls13_select_ciphersuite_for_psk(
Gilles Peskine449bd832023-01-11 14:50:10 +0100601 ssl, ciphersuites, ciphersuites_end,
602 &cipher_suite, &ciphersuite_info);
Jerry Yu0baf9072022-08-25 11:21:04 +0800603 break;
604 case MBEDTLS_SSL_TLS1_3_PSK_RESUMPTION:
Jerry Yu82534862022-08-30 10:42:33 +0800605#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Jerry Yu0baf9072022-08-25 11:21:04 +0800606 ret = ssl_tls13_select_ciphersuite_for_resumption(
Gilles Peskine449bd832023-01-11 14:50:10 +0100607 ssl, ciphersuites, ciphersuites_end, &session,
608 &cipher_suite, &ciphersuite_info);
609 if (ret != 0) {
610 mbedtls_ssl_session_free(&session);
611 }
Jerry Yu82534862022-08-30 10:42:33 +0800612#else
613 ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
614#endif
Jerry Yu0baf9072022-08-25 11:21:04 +0800615 break;
616 default:
Gilles Peskine449bd832023-01-11 14:50:10 +0100617 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Jerry Yu0baf9072022-08-25 11:21:04 +0800618 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100619 if (ret != 0) {
Jerry Yuf35ba382022-08-23 17:58:26 +0800620 /* See below, no cipher_suite available, abort handshake */
621 MBEDTLS_SSL_PEND_FATAL_ALERT(
622 MBEDTLS_SSL_ALERT_MSG_DECRYPT_ERROR,
Gilles Peskine449bd832023-01-11 14:50:10 +0100623 MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE);
Jerry Yuf35ba382022-08-23 17:58:26 +0800624 MBEDTLS_SSL_DEBUG_RET(
Gilles Peskine449bd832023-01-11 14:50:10 +0100625 2, "ssl_tls13_select_ciphersuite", ret);
626 return ret;
Jerry Yu5725f1c2022-08-21 17:27:16 +0800627 }
628
Jerry Yu96a2e362022-07-21 15:11:34 +0800629 ret = ssl_tls13_offered_psks_check_binder_match(
Gilles Peskine449bd832023-01-11 14:50:10 +0100630 ssl, binder, binder_len, psk_type,
Dave Rodgman2eab4622023-10-05 13:30:37 +0100631 mbedtls_md_psa_alg_from_type((mbedtls_md_type_t) ciphersuite_info->mac));
Gilles Peskine449bd832023-01-11 14:50:10 +0100632 if (ret != SSL_TLS1_3_OFFERED_PSK_MATCH) {
Jerry Yuc5a23a02022-08-25 10:51:44 +0800633 /* For security reasons, the handshake should be aborted when we
634 * fail to validate a binder value. See RFC 8446 section 4.2.11.2
635 * and appendix E.6. */
Jerry Yu82534862022-08-30 10:42:33 +0800636#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Gilles Peskine449bd832023-01-11 14:50:10 +0100637 mbedtls_ssl_session_free(&session);
Jerry Yu82534862022-08-30 10:42:33 +0800638#endif
Gilles Peskine449bd832023-01-11 14:50:10 +0100639 MBEDTLS_SSL_DEBUG_MSG(3, ("Invalid binder."));
Xiaokang Qian9f1747b2023-03-30 02:50:04 +0000640 MBEDTLS_SSL_DEBUG_RET(
641 1, "ssl_tls13_offered_psks_check_binder_match", ret);
Jerry Yu96a2e362022-07-21 15:11:34 +0800642 MBEDTLS_SSL_PEND_FATAL_ALERT(
643 MBEDTLS_SSL_ALERT_MSG_DECRYPT_ERROR,
Gilles Peskine449bd832023-01-11 14:50:10 +0100644 MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE);
645 return ret;
Jerry Yu96a2e362022-07-21 15:11:34 +0800646 }
Jerry Yu96a2e362022-07-21 15:11:34 +0800647
648 matched_identity = identity_id;
Jerry Yu5725f1c2022-08-21 17:27:16 +0800649
650 /* Update handshake parameters */
Jerry Yue5834fd2022-08-29 20:16:09 +0800651 ssl->handshake->ciphersuite_info = ciphersuite_info;
Jerry Yu4746b102022-09-13 11:11:48 +0800652 ssl->session_negotiate->ciphersuite = cipher_suite;
Gilles Peskine449bd832023-01-11 14:50:10 +0100653 MBEDTLS_SSL_DEBUG_MSG(2, ("overwrite ciphersuite: %04x - %s",
654 cipher_suite, ciphersuite_info->name));
Jerry Yu82534862022-08-30 10:42:33 +0800655#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Gilles Peskine449bd832023-01-11 14:50:10 +0100656 if (psk_type == MBEDTLS_SSL_TLS1_3_PSK_RESUMPTION) {
657 ret = ssl_tls13_session_copy_ticket(ssl->session_negotiate,
658 &session);
659 mbedtls_ssl_session_free(&session);
660 if (ret != 0) {
661 return ret;
662 }
Jerry Yu82534862022-08-30 10:42:33 +0800663 }
664#endif /* MBEDTLS_SSL_SESSION_TICKETS */
Jerry Yu1c105562022-07-10 06:32:38 +0000665 }
666
Gilles Peskine449bd832023-01-11 14:50:10 +0100667 if (p_identity_len != identities_end || p_binder_len != binders_end) {
668 MBEDTLS_SSL_DEBUG_MSG(3, ("pre_shared_key extension decode error"));
669 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR,
670 MBEDTLS_ERR_SSL_DECODE_ERROR);
671 return MBEDTLS_ERR_SSL_DECODE_ERROR;
Jerry Yu1c105562022-07-10 06:32:38 +0000672 }
673
Jerry Yu6f1db3f2022-07-22 23:05:59 +0800674 /* Update the handshake transcript with the binder list. */
Xiaokang Qian9f1747b2023-03-30 02:50:04 +0000675 ret = ssl->handshake->update_checksum(
676 ssl, identities_end, (size_t) (binders_end - identities_end));
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +0100677 if (0 != ret) {
678 MBEDTLS_SSL_DEBUG_RET(1, ("update_checksum"), ret);
679 return ret;
680 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100681 if (matched_identity == -1) {
682 MBEDTLS_SSL_DEBUG_MSG(3, ("No matched PSK or ticket."));
683 return MBEDTLS_ERR_SSL_UNKNOWN_IDENTITY;
Jerry Yu1c105562022-07-10 06:32:38 +0000684 }
685
Gilles Peskine449bd832023-01-11 14:50:10 +0100686 ssl->handshake->selected_identity = (uint16_t) matched_identity;
687 MBEDTLS_SSL_DEBUG_MSG(3, ("Pre shared key found"));
Jerry Yu1c105562022-07-10 06:32:38 +0000688
Gilles Peskine449bd832023-01-11 14:50:10 +0100689 return 0;
Jerry Yu1c105562022-07-10 06:32:38 +0000690}
Jerry Yu032b15ce2022-07-11 06:10:03 +0000691
692/*
693 * struct {
694 * select ( Handshake.msg_type ) {
695 * ....
696 * case server_hello:
697 * uint16 selected_identity;
698 * }
699 * } PreSharedKeyExtension;
700 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100701static int ssl_tls13_write_server_pre_shared_key_ext(mbedtls_ssl_context *ssl,
702 unsigned char *buf,
703 unsigned char *end,
704 size_t *olen)
Jerry Yu032b15ce2022-07-11 06:10:03 +0000705{
Gilles Peskine449bd832023-01-11 14:50:10 +0100706 unsigned char *p = (unsigned char *) buf;
Jerry Yu032b15ce2022-07-11 06:10:03 +0000707
708 *olen = 0;
709
David Horstmann21b89762022-10-06 18:34:28 +0100710 int not_using_psk = 0;
Jerry Yu032b15ce2022-07-11 06:10:03 +0000711#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +0100712 not_using_psk = (mbedtls_svc_key_id_is_null(ssl->handshake->psk_opaque));
Jerry Yu032b15ce2022-07-11 06:10:03 +0000713#else
Gilles Peskine449bd832023-01-11 14:50:10 +0100714 not_using_psk = (ssl->handshake->psk == NULL);
Jerry Yu032b15ce2022-07-11 06:10:03 +0000715#endif
Gilles Peskine449bd832023-01-11 14:50:10 +0100716 if (not_using_psk) {
Jerry Yu032b15ce2022-07-11 06:10:03 +0000717 /* We shouldn't have called this extension writer unless we've
718 * chosen to use a PSK. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100719 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Jerry Yu032b15ce2022-07-11 06:10:03 +0000720 }
721
Gilles Peskine449bd832023-01-11 14:50:10 +0100722 MBEDTLS_SSL_DEBUG_MSG(3, ("server hello, adding pre_shared_key extension"));
723 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 6);
Jerry Yu032b15ce2022-07-11 06:10:03 +0000724
Gilles Peskine449bd832023-01-11 14:50:10 +0100725 MBEDTLS_PUT_UINT16_BE(MBEDTLS_TLS_EXT_PRE_SHARED_KEY, p, 0);
726 MBEDTLS_PUT_UINT16_BE(2, p, 2);
Jerry Yu032b15ce2022-07-11 06:10:03 +0000727
Gilles Peskine449bd832023-01-11 14:50:10 +0100728 MBEDTLS_PUT_UINT16_BE(ssl->handshake->selected_identity, p, 4);
Jerry Yu032b15ce2022-07-11 06:10:03 +0000729
730 *olen = 6;
731
Gilles Peskine449bd832023-01-11 14:50:10 +0100732 MBEDTLS_SSL_DEBUG_MSG(4, ("sent selected_identity: %u",
733 ssl->handshake->selected_identity));
Jerry Yu032b15ce2022-07-11 06:10:03 +0000734
Gilles Peskine449bd832023-01-11 14:50:10 +0100735 mbedtls_ssl_tls13_set_hs_sent_ext_mask(ssl, MBEDTLS_TLS_EXT_PRE_SHARED_KEY);
Jerry Yub95dd362022-11-08 21:19:34 +0800736
Gilles Peskine449bd832023-01-11 14:50:10 +0100737 return 0;
Jerry Yu032b15ce2022-07-11 06:10:03 +0000738}
739
Ronald Cron41a443a2022-10-04 16:38:25 +0200740#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED */
Jerry Yue19e3b92022-07-08 12:04:51 +0000741
XiaokangQian7807f9f2022-02-15 10:04:37 +0000742/* From RFC 8446:
743 * struct {
XiaokangQiancfd925f2022-04-14 07:10:37 +0000744 * ProtocolVersion versions<2..254>;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000745 * } SupportedVersions;
746 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200747MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100748static int ssl_tls13_parse_supported_versions_ext(mbedtls_ssl_context *ssl,
749 const unsigned char *buf,
750 const unsigned char *end)
XiaokangQian7807f9f2022-02-15 10:04:37 +0000751{
XiaokangQian7807f9f2022-02-15 10:04:37 +0000752 const unsigned char *p = buf;
XiaokangQian8f9dfe42022-04-15 02:52:39 +0000753 size_t versions_len;
XiaokangQian4080a7f2022-04-11 09:55:18 +0000754 const unsigned char *versions_end;
XiaokangQian0a1b54e2022-04-21 03:01:38 +0000755 uint16_t tls_version;
Ronald Cron5af4c7f2023-03-07 20:46:59 +0100756 int found_supported_version = 0;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000757
Gilles Peskine449bd832023-01-11 14:50:10 +0100758 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, 1);
XiaokangQian4080a7f2022-04-11 09:55:18 +0000759 versions_len = p[0];
XiaokangQian7807f9f2022-02-15 10:04:37 +0000760 p += 1;
761
Gilles Peskine449bd832023-01-11 14:50:10 +0100762 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, versions_len);
XiaokangQian4080a7f2022-04-11 09:55:18 +0000763 versions_end = p + versions_len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100764 while (p < versions_end) {
765 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, versions_end, 2);
766 tls_version = mbedtls_ssl_read_version(p, ssl->conf->transport);
XiaokangQianb67384d2022-04-19 00:02:38 +0000767 p += 2;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000768
Ronald Cron3bd2b022023-04-03 16:45:39 +0200769 if (MBEDTLS_SSL_VERSION_TLS1_3 == tls_version) {
Ronald Cron5af4c7f2023-03-07 20:46:59 +0100770 found_supported_version = 1;
771 break;
772 }
773
Ronald Cron3bd2b022023-04-03 16:45:39 +0200774 if ((MBEDTLS_SSL_VERSION_TLS1_2 == tls_version) &&
775 mbedtls_ssl_conf_is_tls12_enabled(ssl->conf)) {
Ronald Cron5af4c7f2023-03-07 20:46:59 +0100776 found_supported_version = 1;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000777 break;
778 }
XiaokangQian7807f9f2022-02-15 10:04:37 +0000779 }
780
Ronald Cron5af4c7f2023-03-07 20:46:59 +0100781 if (!found_supported_version) {
782 MBEDTLS_SSL_DEBUG_MSG(1, ("No supported version found."));
XiaokangQian7807f9f2022-02-15 10:04:37 +0000783
Gilles Peskine449bd832023-01-11 14:50:10 +0100784 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_PROTOCOL_VERSION,
785 MBEDTLS_ERR_SSL_BAD_PROTOCOL_VERSION);
786 return MBEDTLS_ERR_SSL_BAD_PROTOCOL_VERSION;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000787 }
788
Ronald Cron5af4c7f2023-03-07 20:46:59 +0100789 MBEDTLS_SSL_DEBUG_MSG(1, ("Negotiated version: [%04x]",
Gilles Peskine449bd832023-01-11 14:50:10 +0100790 (unsigned int) tls_version));
XiaokangQian7807f9f2022-02-15 10:04:37 +0000791
Ronald Cron5af4c7f2023-03-07 20:46:59 +0100792 return (int) tls_version;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000793}
794
Przemek Stekiel8c0a9532023-06-15 16:48:19 +0200795#if defined(PSA_WANT_ALG_ECDH) || defined(PSA_WANT_ALG_FFDH)
XiaokangQiane8ff3502022-04-22 02:34:40 +0000796/*
XiaokangQian7807f9f2022-02-15 10:04:37 +0000797 *
798 * From RFC 8446:
799 * enum {
800 * ... (0xFFFF)
801 * } NamedGroup;
802 * struct {
803 * NamedGroup named_group_list<2..2^16-1>;
804 * } NamedGroupList;
805 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200806MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100807static int ssl_tls13_parse_supported_groups_ext(mbedtls_ssl_context *ssl,
808 const unsigned char *buf,
809 const unsigned char *end)
XiaokangQian7807f9f2022-02-15 10:04:37 +0000810{
XiaokangQian7807f9f2022-02-15 10:04:37 +0000811 const unsigned char *p = buf;
XiaokangQian84823772022-04-19 07:57:30 +0000812 size_t named_group_list_len;
XiaokangQian8f9dfe42022-04-15 02:52:39 +0000813 const unsigned char *named_group_list_end;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000814
Gilles Peskine449bd832023-01-11 14:50:10 +0100815 MBEDTLS_SSL_DEBUG_BUF(3, "supported_groups extension", p, end - buf);
816 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, 2);
817 named_group_list_len = MBEDTLS_GET_UINT16_BE(p, 0);
XiaokangQian7807f9f2022-02-15 10:04:37 +0000818 p += 2;
Gilles Peskine449bd832023-01-11 14:50:10 +0100819 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, named_group_list_len);
XiaokangQian8f9dfe42022-04-15 02:52:39 +0000820 named_group_list_end = p + named_group_list_len;
XiaokangQian4e8cd7b2022-04-21 09:48:09 +0000821 ssl->handshake->hrr_selected_group = 0;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000822
Gilles Peskine449bd832023-01-11 14:50:10 +0100823 while (p < named_group_list_end) {
XiaokangQian08037552022-04-20 07:16:41 +0000824 uint16_t named_group;
Gilles Peskine449bd832023-01-11 14:50:10 +0100825 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, named_group_list_end, 2);
826 named_group = MBEDTLS_GET_UINT16_BE(p, 0);
XiaokangQian08037552022-04-20 07:16:41 +0000827 p += 2;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000828
Gilles Peskine449bd832023-01-11 14:50:10 +0100829 MBEDTLS_SSL_DEBUG_MSG(2,
830 ("got named group: %s(%04x)",
831 mbedtls_ssl_named_group_to_str(named_group),
832 named_group));
XiaokangQian08037552022-04-20 07:16:41 +0000833
Gilles Peskine449bd832023-01-11 14:50:10 +0100834 if (!mbedtls_ssl_named_group_is_offered(ssl, named_group) ||
835 !mbedtls_ssl_named_group_is_supported(named_group) ||
836 ssl->handshake->hrr_selected_group != 0) {
XiaokangQian08037552022-04-20 07:16:41 +0000837 continue;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000838 }
839
Gilles Peskine449bd832023-01-11 14:50:10 +0100840 MBEDTLS_SSL_DEBUG_MSG(2,
841 ("add named group %s(%04x) into received list.",
842 mbedtls_ssl_named_group_to_str(named_group),
843 named_group));
Jerry Yuc1be19f2022-04-23 16:11:39 +0800844
XiaokangQian4e8cd7b2022-04-21 09:48:09 +0000845 ssl->handshake->hrr_selected_group = named_group;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000846 }
847
Gilles Peskine449bd832023-01-11 14:50:10 +0100848 return 0;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000849
850}
Przemek Stekiel8c0a9532023-06-15 16:48:19 +0200851#endif /* PSA_WANT_ALG_ECDH || PSA_WANT_ALG_FFDH */
XiaokangQian7807f9f2022-02-15 10:04:37 +0000852
XiaokangQian08037552022-04-20 07:16:41 +0000853#define SSL_TLS1_3_PARSE_KEY_SHARES_EXT_NO_MATCH 1
854
Valerio Settic9ae8622023-07-25 11:23:50 +0200855#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_EPHEMERAL_ENABLED)
XiaokangQian7807f9f2022-02-15 10:04:37 +0000856/*
857 * ssl_tls13_parse_key_shares_ext() verifies whether the information in the
Xiaokang Qian9f1747b2023-03-30 02:50:04 +0000858 * extension is correct and stores the first acceptable key share and its
859 * associated group.
XiaokangQian7807f9f2022-02-15 10:04:37 +0000860 *
861 * Possible return values are:
862 * - 0: Successful processing of the client provided key share extension.
Xiaokang Qian9f1747b2023-03-30 02:50:04 +0000863 * - SSL_TLS1_3_PARSE_KEY_SHARES_EXT_NO_MATCH: The key shares provided by
864 * the client does not match a group supported by the server. A
865 * HelloRetryRequest will be needed.
XiaokangQiane8ff3502022-04-22 02:34:40 +0000866 * - A negative value for fatal errors.
Jerry Yuc1be19f2022-04-23 16:11:39 +0800867 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200868MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100869static int ssl_tls13_parse_key_shares_ext(mbedtls_ssl_context *ssl,
870 const unsigned char *buf,
871 const unsigned char *end)
XiaokangQian7807f9f2022-02-15 10:04:37 +0000872{
XiaokangQianb67384d2022-04-19 00:02:38 +0000873 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000874 unsigned char const *p = buf;
XiaokangQian8f9dfe42022-04-15 02:52:39 +0000875 unsigned char const *client_shares_end;
Jerry Yuc1be19f2022-04-23 16:11:39 +0800876 size_t client_shares_len;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000877
878 /* From RFC 8446:
879 *
880 * struct {
881 * KeyShareEntry client_shares<0..2^16-1>;
882 * } KeyShareClientHello;
883 *
884 */
885
Gilles Peskine449bd832023-01-11 14:50:10 +0100886 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, 2);
887 client_shares_len = MBEDTLS_GET_UINT16_BE(p, 0);
XiaokangQian7807f9f2022-02-15 10:04:37 +0000888 p += 2;
Gilles Peskine449bd832023-01-11 14:50:10 +0100889 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, client_shares_len);
XiaokangQian7807f9f2022-02-15 10:04:37 +0000890
891 ssl->handshake->offered_group_id = 0;
XiaokangQian8f9dfe42022-04-15 02:52:39 +0000892 client_shares_end = p + client_shares_len;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000893
894 /* We try to find a suitable key share entry and copy it to the
895 * handshake context. Later, we have to find out whether we can do
896 * something with the provided key share or whether we have to
XiaokangQianc5763b52022-04-02 03:34:37 +0000897 * dismiss it and send a HelloRetryRequest message.
898 */
XiaokangQian7807f9f2022-02-15 10:04:37 +0000899
Gilles Peskine449bd832023-01-11 14:50:10 +0100900 while (p < client_shares_end) {
XiaokangQian9b5d04b2022-04-10 10:20:43 +0000901 uint16_t group;
Jerry Yuc1be19f2022-04-23 16:11:39 +0800902 size_t key_exchange_len;
Jerry Yu086edc22022-05-05 10:50:38 +0800903 const unsigned char *key_exchange;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000904
905 /*
906 * struct {
907 * NamedGroup group;
908 * opaque key_exchange<1..2^16-1>;
909 * } KeyShareEntry;
910 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100911 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, client_shares_end, 4);
912 group = MBEDTLS_GET_UINT16_BE(p, 0);
913 key_exchange_len = MBEDTLS_GET_UINT16_BE(p, 2);
Jerry Yuc1be19f2022-04-23 16:11:39 +0800914 p += 4;
Jerry Yu086edc22022-05-05 10:50:38 +0800915 key_exchange = p;
Gilles Peskine449bd832023-01-11 14:50:10 +0100916 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, client_shares_end, key_exchange_len);
Jerry Yu086edc22022-05-05 10:50:38 +0800917 p += key_exchange_len;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000918
919 /* Continue parsing even if we have already found a match,
XiaokangQianc5763b52022-04-02 03:34:37 +0000920 * for input validation purposes.
921 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100922 if (!mbedtls_ssl_named_group_is_offered(ssl, group) ||
923 !mbedtls_ssl_named_group_is_supported(group) ||
924 ssl->handshake->offered_group_id != 0) {
XiaokangQian060d8672022-04-21 09:24:56 +0000925 continue;
926 }
XiaokangQian060d8672022-04-21 09:24:56 +0000927
XiaokangQian7807f9f2022-02-15 10:04:37 +0000928 /*
Przemek Stekielc89f3ea2023-05-18 15:45:53 +0200929 * ECDHE and FFDHE groups are supported
XiaokangQian7807f9f2022-02-15 10:04:37 +0000930 */
Przemek Stekielc89f3ea2023-05-18 15:45:53 +0200931 if (mbedtls_ssl_tls13_named_group_is_ecdhe(group) ||
Przemek Stekield5f79e72023-06-29 09:08:43 +0200932 mbedtls_ssl_tls13_named_group_is_ffdh(group)) {
Przemek Stekielc89f3ea2023-05-18 15:45:53 +0200933 MBEDTLS_SSL_DEBUG_MSG(2, ("ECDH/FFDH group: %s (%04x)",
Gilles Peskine449bd832023-01-11 14:50:10 +0100934 mbedtls_ssl_named_group_to_str(group),
935 group));
Przemek Stekiel7ac93be2023-07-04 10:02:38 +0200936 ret = mbedtls_ssl_tls13_read_public_xxdhe_share(
Gilles Peskine449bd832023-01-11 14:50:10 +0100937 ssl, key_exchange - 2, key_exchange_len + 2);
938 if (ret != 0) {
939 return ret;
940 }
XiaokangQian4e8cd7b2022-04-21 09:48:09 +0000941
Gilles Peskine449bd832023-01-11 14:50:10 +0100942 } else {
943 MBEDTLS_SSL_DEBUG_MSG(4, ("Unrecognized NamedGroup %u",
944 (unsigned) group));
XiaokangQian7807f9f2022-02-15 10:04:37 +0000945 continue;
946 }
947
XiaokangQian9b5d04b2022-04-10 10:20:43 +0000948 ssl->handshake->offered_group_id = group;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000949 }
950
Jerry Yuc1be19f2022-04-23 16:11:39 +0800951
Gilles Peskine449bd832023-01-11 14:50:10 +0100952 if (ssl->handshake->offered_group_id == 0) {
953 MBEDTLS_SSL_DEBUG_MSG(1, ("no matching key share"));
954 return SSL_TLS1_3_PARSE_KEY_SHARES_EXT_NO_MATCH;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000955 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100956 return 0;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000957}
Valerio Settic9ae8622023-07-25 11:23:50 +0200958#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_EPHEMERAL_ENABLED */
XiaokangQian7807f9f2022-02-15 10:04:37 +0000959
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200960MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100961static int ssl_tls13_client_hello_has_exts(mbedtls_ssl_context *ssl,
962 int exts_mask)
XiaokangQian7807f9f2022-02-15 10:04:37 +0000963{
Jerry Yu0c354a22022-08-29 15:25:36 +0800964 int masked = ssl->handshake->received_extensions & exts_mask;
Gilles Peskine449bd832023-01-11 14:50:10 +0100965 return masked == exts_mask;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000966}
967
Jerry Yue5991322022-11-07 14:03:44 +0800968#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200969MBEDTLS_CHECK_RETURN_CRITICAL
XiaokangQianb67384d2022-04-19 00:02:38 +0000970static int ssl_tls13_client_hello_has_exts_for_ephemeral_key_exchange(
Gilles Peskine449bd832023-01-11 14:50:10 +0100971 mbedtls_ssl_context *ssl)
XiaokangQian7807f9f2022-02-15 10:04:37 +0000972{
Gilles Peskine449bd832023-01-11 14:50:10 +0100973 return ssl_tls13_client_hello_has_exts(
974 ssl,
975 MBEDTLS_SSL_EXT_MASK(SUPPORTED_GROUPS) |
976 MBEDTLS_SSL_EXT_MASK(KEY_SHARE) |
977 MBEDTLS_SSL_EXT_MASK(SIG_ALG));
XiaokangQian7807f9f2022-02-15 10:04:37 +0000978}
Jerry Yue5991322022-11-07 14:03:44 +0800979#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED */
XiaokangQian7807f9f2022-02-15 10:04:37 +0000980
Jerry Yue5991322022-11-07 14:03:44 +0800981#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK_ENABLED)
Jerry Yu77f01482022-07-11 07:03:24 +0000982MBEDTLS_CHECK_RETURN_CRITICAL
983static int ssl_tls13_client_hello_has_exts_for_psk_key_exchange(
Gilles Peskine449bd832023-01-11 14:50:10 +0100984 mbedtls_ssl_context *ssl)
Jerry Yu77f01482022-07-11 07:03:24 +0000985{
Gilles Peskine449bd832023-01-11 14:50:10 +0100986 return ssl_tls13_client_hello_has_exts(
987 ssl,
988 MBEDTLS_SSL_EXT_MASK(PRE_SHARED_KEY) |
989 MBEDTLS_SSL_EXT_MASK(PSK_KEY_EXCHANGE_MODES));
Jerry Yu77f01482022-07-11 07:03:24 +0000990}
Jerry Yue5991322022-11-07 14:03:44 +0800991#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK_ENABLED */
Jerry Yu77f01482022-07-11 07:03:24 +0000992
Jerry Yue5991322022-11-07 14:03:44 +0800993#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK_EPHEMERAL_ENABLED)
Jerry Yu77f01482022-07-11 07:03:24 +0000994MBEDTLS_CHECK_RETURN_CRITICAL
995static int ssl_tls13_client_hello_has_exts_for_psk_ephemeral_key_exchange(
Gilles Peskine449bd832023-01-11 14:50:10 +0100996 mbedtls_ssl_context *ssl)
Jerry Yu77f01482022-07-11 07:03:24 +0000997{
Gilles Peskine449bd832023-01-11 14:50:10 +0100998 return ssl_tls13_client_hello_has_exts(
999 ssl,
1000 MBEDTLS_SSL_EXT_MASK(SUPPORTED_GROUPS) |
1001 MBEDTLS_SSL_EXT_MASK(KEY_SHARE) |
1002 MBEDTLS_SSL_EXT_MASK(PRE_SHARED_KEY) |
1003 MBEDTLS_SSL_EXT_MASK(PSK_KEY_EXCHANGE_MODES));
Jerry Yu77f01482022-07-11 07:03:24 +00001004}
Jerry Yue5991322022-11-07 14:03:44 +08001005#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK_EPHEMERAL_ENABLED */
Jerry Yu77f01482022-07-11 07:03:24 +00001006
Pengyu Lv306a01d2023-02-02 16:35:47 +08001007#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED)
1008MBEDTLS_CHECK_RETURN_CRITICAL
Pengyu Lv52ad3332023-02-14 14:32:37 +08001009static int ssl_tls13_ticket_permission_check(mbedtls_ssl_context *ssl,
1010 unsigned int kex_mode)
Pengyu Lv306a01d2023-02-02 16:35:47 +08001011{
1012#if defined(MBEDTLS_SSL_SESSION_TICKETS)
1013 if (ssl->handshake->resume) {
Pengyu Lv7b711712023-10-24 17:07:14 +08001014 if (mbedtls_ssl_session_check_ticket_flags(
Pengyu Lv306a01d2023-02-02 16:35:47 +08001015 ssl->session_negotiate, kex_mode)) {
1016 return 0;
1017 }
1018 }
1019#else
1020 ((void) ssl);
1021 ((void) kex_mode);
1022#endif
1023 return 1;
1024}
1025#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED */
1026
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001027MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01001028static int ssl_tls13_check_ephemeral_key_exchange(mbedtls_ssl_context *ssl)
XiaokangQian7807f9f2022-02-15 10:04:37 +00001029{
Jerry Yue5991322022-11-07 14:03:44 +08001030#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01001031 return mbedtls_ssl_conf_tls13_ephemeral_enabled(ssl) &&
1032 ssl_tls13_client_hello_has_exts_for_ephemeral_key_exchange(ssl);
Jerry Yue5991322022-11-07 14:03:44 +08001033#else
1034 ((void) ssl);
Gilles Peskine449bd832023-01-11 14:50:10 +01001035 return 0;
Jerry Yue5991322022-11-07 14:03:44 +08001036#endif
Jerry Yu77f01482022-07-11 07:03:24 +00001037}
XiaokangQian7807f9f2022-02-15 10:04:37 +00001038
Jerry Yu77f01482022-07-11 07:03:24 +00001039MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01001040static int ssl_tls13_check_psk_key_exchange(mbedtls_ssl_context *ssl)
Jerry Yu77f01482022-07-11 07:03:24 +00001041{
Jerry Yue5991322022-11-07 14:03:44 +08001042#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK_ENABLED)
Pengyu Lv52ad3332023-02-14 14:32:37 +08001043 return ssl_tls13_ticket_permission_check(
Pengyu Lv306a01d2023-02-02 16:35:47 +08001044 ssl, MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK) &&
1045 mbedtls_ssl_conf_tls13_psk_enabled(ssl) &&
Gilles Peskine449bd832023-01-11 14:50:10 +01001046 mbedtls_ssl_tls13_psk_enabled(ssl) &&
1047 ssl_tls13_client_hello_has_exts_for_psk_key_exchange(ssl);
Jerry Yu77f01482022-07-11 07:03:24 +00001048#else
1049 ((void) ssl);
Gilles Peskine449bd832023-01-11 14:50:10 +01001050 return 0;
Jerry Yu77f01482022-07-11 07:03:24 +00001051#endif
1052}
XiaokangQian7807f9f2022-02-15 10:04:37 +00001053
Jerry Yu77f01482022-07-11 07:03:24 +00001054MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01001055static int ssl_tls13_check_psk_ephemeral_key_exchange(mbedtls_ssl_context *ssl)
Jerry Yu77f01482022-07-11 07:03:24 +00001056{
Jerry Yue5991322022-11-07 14:03:44 +08001057#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK_EPHEMERAL_ENABLED)
Pengyu Lv52ad3332023-02-14 14:32:37 +08001058 return ssl_tls13_ticket_permission_check(
Pengyu Lv306a01d2023-02-02 16:35:47 +08001059 ssl, MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK_EPHEMERAL) &&
1060 mbedtls_ssl_conf_tls13_psk_ephemeral_enabled(ssl) &&
Gilles Peskine449bd832023-01-11 14:50:10 +01001061 mbedtls_ssl_tls13_psk_ephemeral_enabled(ssl) &&
1062 ssl_tls13_client_hello_has_exts_for_psk_ephemeral_key_exchange(ssl);
Jerry Yu77f01482022-07-11 07:03:24 +00001063#else
1064 ((void) ssl);
Gilles Peskine449bd832023-01-11 14:50:10 +01001065 return 0;
Jerry Yu77f01482022-07-11 07:03:24 +00001066#endif
1067}
1068
Gilles Peskine449bd832023-01-11 14:50:10 +01001069static int ssl_tls13_determine_key_exchange_mode(mbedtls_ssl_context *ssl)
Jerry Yu77f01482022-07-11 07:03:24 +00001070{
1071 /*
1072 * Determine the key exchange algorithm to use.
1073 * There are three types of key exchanges supported in TLS 1.3:
1074 * - (EC)DH with ECDSA,
1075 * - (EC)DH with PSK,
1076 * - plain PSK.
1077 *
1078 * The PSK-based key exchanges may additionally be used with 0-RTT.
1079 *
1080 * Our built-in order of preference is
Jerry Yuce6ed702022-07-22 21:49:53 +08001081 * 1 ) (EC)DHE-PSK Mode ( psk_ephemeral )
1082 * 2 ) Certificate Mode ( ephemeral )
1083 * 3 ) Plain PSK Mode ( psk )
Jerry Yu77f01482022-07-11 07:03:24 +00001084 */
1085
Xiaokang Qian9f1747b2023-03-30 02:50:04 +00001086 ssl->handshake->key_exchange_mode =
1087 MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_NONE;
Jerry Yu77f01482022-07-11 07:03:24 +00001088
Gilles Peskine449bd832023-01-11 14:50:10 +01001089 if (ssl_tls13_check_psk_ephemeral_key_exchange(ssl)) {
Jerry Yu77f01482022-07-11 07:03:24 +00001090 ssl->handshake->key_exchange_mode =
1091 MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK_EPHEMERAL;
Gilles Peskine449bd832023-01-11 14:50:10 +01001092 MBEDTLS_SSL_DEBUG_MSG(2, ("key exchange mode: psk_ephemeral"));
1093 } else
1094 if (ssl_tls13_check_ephemeral_key_exchange(ssl)) {
Jerry Yu77f01482022-07-11 07:03:24 +00001095 ssl->handshake->key_exchange_mode =
1096 MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL;
Gilles Peskine449bd832023-01-11 14:50:10 +01001097 MBEDTLS_SSL_DEBUG_MSG(2, ("key exchange mode: ephemeral"));
1098 } else
1099 if (ssl_tls13_check_psk_key_exchange(ssl)) {
Jerry Yuce6ed702022-07-22 21:49:53 +08001100 ssl->handshake->key_exchange_mode =
1101 MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK;
Gilles Peskine449bd832023-01-11 14:50:10 +01001102 MBEDTLS_SSL_DEBUG_MSG(2, ("key exchange mode: psk"));
1103 } else {
Jerry Yu77f01482022-07-11 07:03:24 +00001104 MBEDTLS_SSL_DEBUG_MSG(
Gilles Peskine449bd832023-01-11 14:50:10 +01001105 1,
1106 ("ClientHello message misses mandatory extensions."));
1107 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_MISSING_EXTENSION,
1108 MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER);
1109 return MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER;
Jerry Yu77f01482022-07-11 07:03:24 +00001110 }
1111
Gilles Peskine449bd832023-01-11 14:50:10 +01001112 return 0;
Jerry Yu77f01482022-07-11 07:03:24 +00001113
XiaokangQian7807f9f2022-02-15 10:04:37 +00001114}
1115
XiaokangQian81802f42022-06-10 13:25:22 +00001116#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
Ronald Cron928cbd32022-10-04 16:14:26 +02001117 defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED)
Ronald Cron67ea2542022-09-15 17:34:42 +02001118
1119#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01001120static psa_algorithm_t ssl_tls13_iana_sig_alg_to_psa_alg(uint16_t sig_alg)
Ronald Cron67ea2542022-09-15 17:34:42 +02001121{
Gilles Peskine449bd832023-01-11 14:50:10 +01001122 switch (sig_alg) {
Ronald Cron67ea2542022-09-15 17:34:42 +02001123 case MBEDTLS_TLS1_3_SIG_ECDSA_SECP256R1_SHA256:
Gilles Peskine449bd832023-01-11 14:50:10 +01001124 return PSA_ALG_ECDSA(PSA_ALG_SHA_256);
Ronald Cron67ea2542022-09-15 17:34:42 +02001125 case MBEDTLS_TLS1_3_SIG_ECDSA_SECP384R1_SHA384:
Gilles Peskine449bd832023-01-11 14:50:10 +01001126 return PSA_ALG_ECDSA(PSA_ALG_SHA_384);
Ronald Cron67ea2542022-09-15 17:34:42 +02001127 case MBEDTLS_TLS1_3_SIG_ECDSA_SECP521R1_SHA512:
Gilles Peskine449bd832023-01-11 14:50:10 +01001128 return PSA_ALG_ECDSA(PSA_ALG_SHA_512);
Ronald Cron67ea2542022-09-15 17:34:42 +02001129 case MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA256:
Gilles Peskine449bd832023-01-11 14:50:10 +01001130 return PSA_ALG_RSA_PSS(PSA_ALG_SHA_256);
Ronald Cron67ea2542022-09-15 17:34:42 +02001131 case MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA384:
Gilles Peskine449bd832023-01-11 14:50:10 +01001132 return PSA_ALG_RSA_PSS(PSA_ALG_SHA_384);
Ronald Cron67ea2542022-09-15 17:34:42 +02001133 case MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA512:
Gilles Peskine449bd832023-01-11 14:50:10 +01001134 return PSA_ALG_RSA_PSS(PSA_ALG_SHA_512);
Ronald Cron67ea2542022-09-15 17:34:42 +02001135 case MBEDTLS_TLS1_3_SIG_RSA_PKCS1_SHA256:
Gilles Peskine449bd832023-01-11 14:50:10 +01001136 return PSA_ALG_RSA_PKCS1V15_SIGN(PSA_ALG_SHA_256);
Ronald Cron67ea2542022-09-15 17:34:42 +02001137 case MBEDTLS_TLS1_3_SIG_RSA_PKCS1_SHA384:
Gilles Peskine449bd832023-01-11 14:50:10 +01001138 return PSA_ALG_RSA_PKCS1V15_SIGN(PSA_ALG_SHA_384);
Ronald Cron67ea2542022-09-15 17:34:42 +02001139 case MBEDTLS_TLS1_3_SIG_RSA_PKCS1_SHA512:
Gilles Peskine449bd832023-01-11 14:50:10 +01001140 return PSA_ALG_RSA_PKCS1V15_SIGN(PSA_ALG_SHA_512);
Ronald Cron67ea2542022-09-15 17:34:42 +02001141 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01001142 return PSA_ALG_NONE;
Ronald Cron67ea2542022-09-15 17:34:42 +02001143 }
1144}
1145#endif /* MBEDTLS_USE_PSA_CRYPTO */
1146
XiaokangQian23c5be62022-06-07 02:04:34 +00001147/*
XiaokangQianfb665a82022-06-15 03:57:21 +00001148 * Pick best ( private key, certificate chain ) pair based on the signature
1149 * algorithms supported by the client.
XiaokangQian23c5be62022-06-07 02:04:34 +00001150 */
Ronald Cronce7d76e2022-07-08 18:56:49 +02001151MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01001152static int ssl_tls13_pick_key_cert(mbedtls_ssl_context *ssl)
XiaokangQian23c5be62022-06-07 02:04:34 +00001153{
XiaokangQianfb665a82022-06-15 03:57:21 +00001154 mbedtls_ssl_key_cert *key_cert, *key_cert_list;
XiaokangQian81802f42022-06-10 13:25:22 +00001155 const uint16_t *sig_alg = ssl->handshake->received_sig_algs;
XiaokangQian23c5be62022-06-07 02:04:34 +00001156
1157#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Gilles Peskine449bd832023-01-11 14:50:10 +01001158 if (ssl->handshake->sni_key_cert != NULL) {
XiaokangQianfb665a82022-06-15 03:57:21 +00001159 key_cert_list = ssl->handshake->sni_key_cert;
Gilles Peskine449bd832023-01-11 14:50:10 +01001160 } else
XiaokangQian23c5be62022-06-07 02:04:34 +00001161#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
Gilles Peskine449bd832023-01-11 14:50:10 +01001162 key_cert_list = ssl->conf->key_cert;
XiaokangQian23c5be62022-06-07 02:04:34 +00001163
Gilles Peskine449bd832023-01-11 14:50:10 +01001164 if (key_cert_list == NULL) {
1165 MBEDTLS_SSL_DEBUG_MSG(3, ("server has no certificate"));
1166 return -1;
XiaokangQian23c5be62022-06-07 02:04:34 +00001167 }
1168
Gilles Peskine449bd832023-01-11 14:50:10 +01001169 for (; *sig_alg != MBEDTLS_TLS1_3_SIG_NONE; sig_alg++) {
1170 if (!mbedtls_ssl_sig_alg_is_offered(ssl, *sig_alg)) {
Ronald Cron67ea2542022-09-15 17:34:42 +02001171 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +01001172 }
Ronald Cron67ea2542022-09-15 17:34:42 +02001173
Gilles Peskine449bd832023-01-11 14:50:10 +01001174 if (!mbedtls_ssl_tls13_sig_alg_for_cert_verify_is_supported(*sig_alg)) {
Ronald Cron67ea2542022-09-15 17:34:42 +02001175 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +01001176 }
Ronald Cron67ea2542022-09-15 17:34:42 +02001177
Gilles Peskine449bd832023-01-11 14:50:10 +01001178 for (key_cert = key_cert_list; key_cert != NULL;
1179 key_cert = key_cert->next) {
Ronald Cron67ea2542022-09-15 17:34:42 +02001180#if defined(MBEDTLS_USE_PSA_CRYPTO)
1181 psa_algorithm_t psa_alg = PSA_ALG_NONE;
1182#endif /* MBEDTLS_USE_PSA_CRYPTO */
1183
Gilles Peskine449bd832023-01-11 14:50:10 +01001184 MBEDTLS_SSL_DEBUG_CRT(3, "certificate (chain) candidate",
1185 key_cert->cert);
XiaokangQian81802f42022-06-10 13:25:22 +00001186
1187 /*
Gilles Peskine449bd832023-01-11 14:50:10 +01001188 * This avoids sending the client a cert it'll reject based on
1189 * keyUsage or other extensions.
1190 */
1191 if (mbedtls_x509_crt_check_key_usage(
1192 key_cert->cert, MBEDTLS_X509_KU_DIGITAL_SIGNATURE) != 0 ||
XiaokangQian81802f42022-06-10 13:25:22 +00001193 mbedtls_x509_crt_check_extended_key_usage(
XiaokangQianfb665a82022-06-15 03:57:21 +00001194 key_cert->cert, MBEDTLS_OID_SERVER_AUTH,
Gilles Peskine449bd832023-01-11 14:50:10 +01001195 MBEDTLS_OID_SIZE(MBEDTLS_OID_SERVER_AUTH)) != 0) {
1196 MBEDTLS_SSL_DEBUG_MSG(3, ("certificate mismatch: "
1197 "(extended) key usage extension"));
XiaokangQian81802f42022-06-10 13:25:22 +00001198 continue;
1199 }
XiaokangQian23c5be62022-06-07 02:04:34 +00001200
Gilles Peskine449bd832023-01-11 14:50:10 +01001201 MBEDTLS_SSL_DEBUG_MSG(3,
1202 ("ssl_tls13_pick_key_cert:"
1203 "check signature algorithm %s [%04x]",
1204 mbedtls_ssl_sig_alg_to_str(*sig_alg),
1205 *sig_alg));
Ronald Cron67ea2542022-09-15 17:34:42 +02001206#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01001207 psa_alg = ssl_tls13_iana_sig_alg_to_psa_alg(*sig_alg);
Ronald Cron67ea2542022-09-15 17:34:42 +02001208#endif /* MBEDTLS_USE_PSA_CRYPTO */
1209
Gilles Peskine449bd832023-01-11 14:50:10 +01001210 if (mbedtls_ssl_tls13_check_sig_alg_cert_key_match(
1211 *sig_alg, &key_cert->cert->pk)
Ronald Cron67ea2542022-09-15 17:34:42 +02001212#if defined(MBEDTLS_USE_PSA_CRYPTO)
1213 && psa_alg != PSA_ALG_NONE &&
Gilles Peskine449bd832023-01-11 14:50:10 +01001214 mbedtls_pk_can_do_ext(&key_cert->cert->pk, psa_alg,
1215 PSA_KEY_USAGE_SIGN_HASH) == 1
Ronald Cron67ea2542022-09-15 17:34:42 +02001216#endif /* MBEDTLS_USE_PSA_CRYPTO */
Gilles Peskine449bd832023-01-11 14:50:10 +01001217 ) {
XiaokangQianfb665a82022-06-15 03:57:21 +00001218 ssl->handshake->key_cert = key_cert;
Gilles Peskine449bd832023-01-11 14:50:10 +01001219 MBEDTLS_SSL_DEBUG_MSG(3,
1220 ("ssl_tls13_pick_key_cert:"
1221 "selected signature algorithm"
1222 " %s [%04x]",
1223 mbedtls_ssl_sig_alg_to_str(*sig_alg),
1224 *sig_alg));
XiaokangQianfb665a82022-06-15 03:57:21 +00001225 MBEDTLS_SSL_DEBUG_CRT(
Gilles Peskine449bd832023-01-11 14:50:10 +01001226 3, "selected certificate (chain)",
1227 ssl->handshake->key_cert->cert);
1228 return 0;
XiaokangQian81802f42022-06-10 13:25:22 +00001229 }
XiaokangQian81802f42022-06-10 13:25:22 +00001230 }
XiaokangQian23c5be62022-06-07 02:04:34 +00001231 }
1232
Gilles Peskine449bd832023-01-11 14:50:10 +01001233 MBEDTLS_SSL_DEBUG_MSG(2, ("ssl_tls13_pick_key_cert:"
1234 "no suitable certificate found"));
1235 return -1;
XiaokangQian23c5be62022-06-07 02:04:34 +00001236}
XiaokangQian81802f42022-06-10 13:25:22 +00001237#endif /* MBEDTLS_X509_CRT_PARSE_C &&
Ronald Cron928cbd32022-10-04 16:14:26 +02001238 MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED */
XiaokangQian23c5be62022-06-07 02:04:34 +00001239
XiaokangQian4080a7f2022-04-11 09:55:18 +00001240/*
XiaokangQianed582dd2022-04-13 08:21:05 +00001241 *
1242 * STATE HANDLING: ClientHello
1243 *
XiaokangQianb67384d2022-04-19 00:02:38 +00001244 * There are three possible classes of outcomes when parsing the ClientHello:
XiaokangQianed582dd2022-04-13 08:21:05 +00001245 *
XiaokangQianb67384d2022-04-19 00:02:38 +00001246 * 1) The ClientHello was well-formed and matched the server's configuration.
XiaokangQianed582dd2022-04-13 08:21:05 +00001247 *
1248 * In this case, the server progresses to sending its ServerHello.
1249 *
XiaokangQianb67384d2022-04-19 00:02:38 +00001250 * 2) The ClientHello was well-formed but didn't match the server's
1251 * configuration.
XiaokangQianed582dd2022-04-13 08:21:05 +00001252 *
1253 * For example, the client might not have offered a key share which
1254 * the server supports, or the server might require a cookie.
1255 *
1256 * In this case, the server sends a HelloRetryRequest.
1257 *
XiaokangQianb67384d2022-04-19 00:02:38 +00001258 * 3) The ClientHello was ill-formed
XiaokangQianed582dd2022-04-13 08:21:05 +00001259 *
1260 * In this case, we abort the handshake.
1261 *
1262 */
1263
1264/*
XiaokangQian4080a7f2022-04-11 09:55:18 +00001265 * Structure of this message:
1266 *
XiaokangQiane8ff3502022-04-22 02:34:40 +00001267 * uint16 ProtocolVersion;
1268 * opaque Random[32];
1269 * uint8 CipherSuite[2]; // Cryptographic suite selector
XiaokangQian4080a7f2022-04-11 09:55:18 +00001270 *
XiaokangQiane8ff3502022-04-22 02:34:40 +00001271 * struct {
1272 * ProtocolVersion legacy_version = 0x0303; // TLS v1.2
1273 * Random random;
1274 * opaque legacy_session_id<0..32>;
1275 * CipherSuite cipher_suites<2..2^16-2>;
1276 * opaque legacy_compression_methods<1..2^8-1>;
1277 * Extension extensions<8..2^16-1>;
1278 * } ClientHello;
XiaokangQian4080a7f2022-04-11 09:55:18 +00001279 */
XiaokangQianed582dd2022-04-13 08:21:05 +00001280
1281#define SSL_CLIENT_HELLO_OK 0
1282#define SSL_CLIENT_HELLO_HRR_REQUIRED 1
Ronald Cron5af4c7f2023-03-07 20:46:59 +01001283#define SSL_CLIENT_HELLO_TLS1_2 2
XiaokangQianed582dd2022-04-13 08:21:05 +00001284
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001285MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01001286static int ssl_tls13_parse_client_hello(mbedtls_ssl_context *ssl,
1287 const unsigned char *buf,
1288 const unsigned char *end)
XiaokangQian7807f9f2022-02-15 10:04:37 +00001289{
XiaokangQianb67384d2022-04-19 00:02:38 +00001290 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1291 const unsigned char *p = buf;
Ronald Crond540d992023-03-07 09:41:48 +01001292 const unsigned char *random;
XiaokangQian4080a7f2022-04-11 09:55:18 +00001293 size_t legacy_session_id_len;
Ronald Croncada4102023-03-07 09:51:39 +01001294 const unsigned char *legacy_session_id;
XiaokangQian318dc762022-04-20 09:43:51 +00001295 size_t cipher_suites_len;
Ronald Cron2f16b4e2023-03-07 10:07:32 +01001296 const unsigned char *cipher_suites;
XiaokangQian060d8672022-04-21 09:24:56 +00001297 const unsigned char *cipher_suites_end;
XiaokangQianb67384d2022-04-19 00:02:38 +00001298 size_t extensions_len;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001299 const unsigned char *extensions_end;
Ronald Croneff56732023-04-03 17:36:31 +02001300 const unsigned char *supported_versions_data;
1301 const unsigned char *supported_versions_data_end;
Jerry Yuc4bf5d62022-10-29 09:08:47 +08001302 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
Jerry Yu49ca9282022-05-05 11:05:22 +08001303 int hrr_required = 0;
Ronald Cron8a74f072023-06-14 17:59:29 +02001304 int no_usable_share_for_key_agreement = 0;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001305
Ronald Cron41a443a2022-10-04 16:38:25 +02001306#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED)
Jerry Yu29d9faa2022-08-23 17:52:45 +08001307 const unsigned char *pre_shared_key_ext = NULL;
Jerry Yu1c105562022-07-10 06:32:38 +00001308 const unsigned char *pre_shared_key_ext_end = NULL;
Ronald Cron41a443a2022-10-04 16:38:25 +02001309#endif
XiaokangQian7807f9f2022-02-15 10:04:37 +00001310
XiaokangQian7807f9f2022-02-15 10:04:37 +00001311 /*
XiaokangQianb67384d2022-04-19 00:02:38 +00001312 * ClientHello layout:
XiaokangQian7807f9f2022-02-15 10:04:37 +00001313 * 0 . 1 protocol version
XiaokangQiane8ff3502022-04-22 02:34:40 +00001314 * 2 . 33 random bytes
XiaokangQianc5763b52022-04-02 03:34:37 +00001315 * 34 . 34 session id length ( 1 byte )
XiaokangQian7807f9f2022-02-15 10:04:37 +00001316 * 35 . 34+x session id
XiaokangQian7807f9f2022-02-15 10:04:37 +00001317 * .. . .. ciphersuite list length ( 2 bytes )
1318 * .. . .. ciphersuite list
1319 * .. . .. compression alg. list length ( 1 byte )
1320 * .. . .. compression alg. list
1321 * .. . .. extensions length ( 2 bytes, optional )
1322 * .. . .. extensions ( optional )
1323 */
1324
XiaokangQianb67384d2022-04-19 00:02:38 +00001325 /*
bootstrap-prime6dbbf442022-05-17 19:30:44 -04001326 * Minimal length ( with everything empty and extensions omitted ) is
XiaokangQian7807f9f2022-02-15 10:04:37 +00001327 * 2 + 32 + 1 + 2 + 1 = 38 bytes. Check that first, so that we can
1328 * read at least up to session id length without worrying.
1329 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001330 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, 38);
XiaokangQian7807f9f2022-02-15 10:04:37 +00001331
1332 /* ...
1333 * ProtocolVersion legacy_version = 0x0303; // TLS 1.2
1334 * ...
1335 * with ProtocolVersion defined as:
1336 * uint16 ProtocolVersion;
1337 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001338 if (mbedtls_ssl_read_version(p, ssl->conf->transport) !=
1339 MBEDTLS_SSL_VERSION_TLS1_2) {
1340 MBEDTLS_SSL_DEBUG_MSG(1, ("Unsupported version of TLS."));
1341 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_PROTOCOL_VERSION,
1342 MBEDTLS_ERR_SSL_BAD_PROTOCOL_VERSION);
1343 return MBEDTLS_ERR_SSL_BAD_PROTOCOL_VERSION;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001344 }
1345 p += 2;
1346
Jerry Yuc1be19f2022-04-23 16:11:39 +08001347 /* ...
1348 * Random random;
1349 * ...
XiaokangQianb67384d2022-04-19 00:02:38 +00001350 * with Random defined as:
1351 * opaque Random[32];
XiaokangQian7807f9f2022-02-15 10:04:37 +00001352 */
Ronald Crond540d992023-03-07 09:41:48 +01001353 random = p;
XiaokangQian08037552022-04-20 07:16:41 +00001354 p += MBEDTLS_CLIENT_HELLO_RANDOM_LEN;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001355
Jerry Yuc1be19f2022-04-23 16:11:39 +08001356 /* ...
XiaokangQianb67384d2022-04-19 00:02:38 +00001357 * opaque legacy_session_id<0..32>;
Jerry Yuc1be19f2022-04-23 16:11:39 +08001358 * ...
XiaokangQian7807f9f2022-02-15 10:04:37 +00001359 */
Ronald Croncada4102023-03-07 09:51:39 +01001360 legacy_session_id_len = *(p++);
1361 legacy_session_id = p;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001362
XiaokangQianb67384d2022-04-19 00:02:38 +00001363 /*
1364 * Check we have enough data for the legacy session identifier
Jerry Yue95c8af2022-07-26 15:48:20 +08001365 * and the ciphersuite list length.
XiaokangQianb67384d2022-04-19 00:02:38 +00001366 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001367 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, legacy_session_id_len + 2);
XiaokangQian4080a7f2022-04-11 09:55:18 +00001368 p += legacy_session_id_len;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001369
Ronald Cron2f16b4e2023-03-07 10:07:32 +01001370 /* ...
1371 * CipherSuite cipher_suites<2..2^16-2>;
1372 * ...
1373 * with CipherSuite defined as:
1374 * uint8 CipherSuite[2];
1375 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001376 cipher_suites_len = MBEDTLS_GET_UINT16_BE(p, 0);
XiaokangQian7807f9f2022-02-15 10:04:37 +00001377 p += 2;
Ronald Cron2f16b4e2023-03-07 10:07:32 +01001378 cipher_suites = p;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001379
Ronald Cronfc7ae872023-02-16 15:32:19 +01001380 /*
1381 * The length of the ciphersuite list has to be even.
1382 */
1383 if (cipher_suites_len & 1) {
1384 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR,
1385 MBEDTLS_ERR_SSL_DECODE_ERROR);
1386 return MBEDTLS_ERR_SSL_DECODE_ERROR;
1387 }
1388
XiaokangQianb67384d2022-04-19 00:02:38 +00001389 /* Check we have enough data for the ciphersuite list, the legacy
1390 * compression methods and the length of the extensions.
Jerry Yue95c8af2022-07-26 15:48:20 +08001391 *
1392 * cipher_suites cipher_suites_len bytes
1393 * legacy_compression_methods 2 bytes
1394 * extensions_len 2 bytes
XiaokangQianb67384d2022-04-19 00:02:38 +00001395 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001396 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, cipher_suites_len + 2 + 2);
Ronald Cron8c527d02023-03-07 15:47:47 +01001397 p += cipher_suites_len;
1398 cipher_suites_end = p;
Jerry Yu5725f1c2022-08-21 17:27:16 +08001399
1400 /*
Ronald Cron8c527d02023-03-07 15:47:47 +01001401 * Search for the supported versions extension and parse it to determine
1402 * if the client supports TLS 1.3.
1403 */
1404 ret = mbedtls_ssl_tls13_is_supported_versions_ext_present_in_exts(
1405 ssl, p + 2, end,
Ronald Croneff56732023-04-03 17:36:31 +02001406 &supported_versions_data, &supported_versions_data_end);
Ronald Cron8c527d02023-03-07 15:47:47 +01001407 if (ret < 0) {
1408 MBEDTLS_SSL_DEBUG_RET(1,
1409 ("mbedtls_ssl_tls13_is_supported_versions_ext_present_in_exts"), ret);
1410 return ret;
1411 }
1412
1413 if (ret == 0) {
Ronald Cron5af4c7f2023-03-07 20:46:59 +01001414 return SSL_CLIENT_HELLO_TLS1_2;
Ronald Cron8c527d02023-03-07 15:47:47 +01001415 }
1416
Ronald Cron5af4c7f2023-03-07 20:46:59 +01001417 if (ret == 1) {
1418 ret = ssl_tls13_parse_supported_versions_ext(ssl,
Ronald Croneff56732023-04-03 17:36:31 +02001419 supported_versions_data,
1420 supported_versions_data_end);
Ronald Cron5af4c7f2023-03-07 20:46:59 +01001421 if (ret < 0) {
1422 MBEDTLS_SSL_DEBUG_RET(1,
1423 ("ssl_tls13_parse_supported_versions_ext"), ret);
1424 return ret;
1425 }
1426
Ronald Cronb828c7d2023-04-03 16:37:22 +02001427 /*
1428 * The supported versions extension was parsed successfully as the
1429 * value returned by ssl_tls13_parse_supported_versions_ext() is
1430 * positive. The return value is then equal to
1431 * MBEDTLS_SSL_VERSION_TLS1_2 or MBEDTLS_SSL_VERSION_TLS1_3, defining
1432 * the TLS version to negotiate.
1433 */
Ronald Cron5af4c7f2023-03-07 20:46:59 +01001434 if (MBEDTLS_SSL_VERSION_TLS1_2 == ret) {
1435 return SSL_CLIENT_HELLO_TLS1_2;
1436 }
Ronald Cron8c527d02023-03-07 15:47:47 +01001437 }
1438
1439 /*
1440 * We negotiate TLS 1.3.
Ronald Cron64582392023-03-07 09:21:40 +01001441 */
1442 ssl->tls_version = MBEDTLS_SSL_VERSION_TLS1_3;
1443
1444#if defined(MBEDTLS_SSL_SESSION_TICKETS)
1445 /* Store minor version for later use with ticket serialization. */
1446 ssl->session_negotiate->tls_version = MBEDTLS_SSL_VERSION_TLS1_3;
1447 ssl->session_negotiate->endpoint = ssl->conf->endpoint;
1448#endif
1449
1450 /*
Ronald Crondad02b22023-04-06 09:57:52 +02001451 * We are negotiating the version 1.3 of the protocol. Do what we have
Ronald Croncada4102023-03-07 09:51:39 +01001452 * postponed: copy of the client random bytes, copy of the legacy session
Ronald Cron2f16b4e2023-03-07 10:07:32 +01001453 * identifier and selection of the TLS 1.3 cipher suite.
Ronald Crond540d992023-03-07 09:41:48 +01001454 */
1455 MBEDTLS_SSL_DEBUG_BUF(3, "client hello, random bytes",
1456 random, MBEDTLS_CLIENT_HELLO_RANDOM_LEN);
1457 memcpy(&handshake->randbytes[0], random, MBEDTLS_CLIENT_HELLO_RANDOM_LEN);
1458
Ronald Croncada4102023-03-07 09:51:39 +01001459 if (legacy_session_id_len > sizeof(ssl->session_negotiate->id)) {
1460 MBEDTLS_SSL_DEBUG_MSG(1, ("bad client hello message"));
1461 return MBEDTLS_ERR_SSL_DECODE_ERROR;
1462 }
1463 ssl->session_negotiate->id_len = legacy_session_id_len;
1464 MBEDTLS_SSL_DEBUG_BUF(3, "client hello, session id",
1465 legacy_session_id, legacy_session_id_len);
1466 memcpy(&ssl->session_negotiate->id[0],
1467 legacy_session_id, legacy_session_id_len);
1468
Ronald Crond540d992023-03-07 09:41:48 +01001469 /*
Jerry Yu5725f1c2022-08-21 17:27:16 +08001470 * Search for a matching ciphersuite
1471 */
Ronald Cron2f16b4e2023-03-07 10:07:32 +01001472 MBEDTLS_SSL_DEBUG_BUF(3, "client hello, list of cipher suites",
1473 cipher_suites, cipher_suites_len);
Ronald Crone45afd72023-04-04 15:10:06 +02001474 for (const unsigned char *cipher_suites_p = cipher_suites;
1475 cipher_suites_p < cipher_suites_end; cipher_suites_p += 2) {
XiaokangQiane8ff3502022-04-22 02:34:40 +00001476 uint16_t cipher_suite;
Gilles Peskine449bd832023-01-11 14:50:10 +01001477 const mbedtls_ssl_ciphersuite_t *ciphersuite_info;
Jerry Yu5725f1c2022-08-21 17:27:16 +08001478
Ronald Crond89360b2023-02-21 08:53:33 +01001479 /*
Ronald Crone45afd72023-04-04 15:10:06 +02001480 * "cipher_suites_end - cipher_suites_p is even" is an invariant of the
1481 * loop. As cipher_suites_end - cipher_suites_p > 0, we have
1482 * cipher_suites_end - cipher_suites_p >= 2 and it is thus safe to read
1483 * two bytes.
Ronald Crond89360b2023-02-21 08:53:33 +01001484 */
Ronald Crone45afd72023-04-04 15:10:06 +02001485 cipher_suite = MBEDTLS_GET_UINT16_BE(cipher_suites_p, 0);
Jerry Yuc5a23a02022-08-25 10:51:44 +08001486 ciphersuite_info = ssl_tls13_validate_peer_ciphersuite(
Gilles Peskine449bd832023-01-11 14:50:10 +01001487 ssl, cipher_suite);
1488 if (ciphersuite_info == NULL) {
Jerry Yu5725f1c2022-08-21 17:27:16 +08001489 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +01001490 }
Jerry Yu5725f1c2022-08-21 17:27:16 +08001491
Jerry Yu5725f1c2022-08-21 17:27:16 +08001492 ssl->session_negotiate->ciphersuite = cipher_suite;
Jerry Yuc4bf5d62022-10-29 09:08:47 +08001493 handshake->ciphersuite_info = ciphersuite_info;
Gilles Peskine449bd832023-01-11 14:50:10 +01001494 MBEDTLS_SSL_DEBUG_MSG(2, ("selected ciphersuite: %04x - %s",
1495 cipher_suite,
1496 ciphersuite_info->name));
Ronald Cron25e9ec62023-02-16 15:35:16 +01001497 break;
XiaokangQian17f974c2022-04-19 09:57:41 +00001498 }
Jerry Yu5725f1c2022-08-21 17:27:16 +08001499
Gilles Peskine449bd832023-01-11 14:50:10 +01001500 if (handshake->ciphersuite_info == NULL) {
1501 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE,
1502 MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE);
1503 return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
Jerry Yu5725f1c2022-08-21 17:27:16 +08001504 }
Jerry Yuc1be19f2022-04-23 16:11:39 +08001505
XiaokangQian4080a7f2022-04-11 09:55:18 +00001506 /* ...
XiaokangQianb67384d2022-04-19 00:02:38 +00001507 * opaque legacy_compression_methods<1..2^8-1>;
XiaokangQian4080a7f2022-04-11 09:55:18 +00001508 * ...
XiaokangQian7807f9f2022-02-15 10:04:37 +00001509 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001510 if (p[0] != 1 || p[1] != MBEDTLS_SSL_COMPRESS_NULL) {
1511 MBEDTLS_SSL_DEBUG_MSG(1, ("bad legacy compression method"));
1512 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER,
1513 MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER);
1514 return MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001515 }
XiaokangQianb67384d2022-04-19 00:02:38 +00001516 p += 2;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001517
Jerry Yuc1be19f2022-04-23 16:11:39 +08001518 /* ...
XiaokangQianb67384d2022-04-19 00:02:38 +00001519 * Extension extensions<8..2^16-1>;
Jerry Yuc1be19f2022-04-23 16:11:39 +08001520 * ...
XiaokangQianb67384d2022-04-19 00:02:38 +00001521 * with Extension defined as:
1522 * struct {
1523 * ExtensionType extension_type;
1524 * opaque extension_data<0..2^16-1>;
1525 * } Extension;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001526 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001527 extensions_len = MBEDTLS_GET_UINT16_BE(p, 0);
XiaokangQian7807f9f2022-02-15 10:04:37 +00001528 p += 2;
Gilles Peskine449bd832023-01-11 14:50:10 +01001529 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, extensions_len);
XiaokangQiane8ff3502022-04-22 02:34:40 +00001530 extensions_end = p + extensions_len;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001531
Gilles Peskine449bd832023-01-11 14:50:10 +01001532 MBEDTLS_SSL_DEBUG_BUF(3, "client hello extensions", p, extensions_len);
Jerry Yu63a459c2022-10-31 13:38:40 +08001533 handshake->received_extensions = MBEDTLS_SSL_EXT_MASK_NONE;
Jerry Yu0c354a22022-08-29 15:25:36 +08001534
Gilles Peskine449bd832023-01-11 14:50:10 +01001535 while (p < extensions_end) {
XiaokangQian7807f9f2022-02-15 10:04:37 +00001536 unsigned int extension_type;
1537 size_t extension_data_len;
1538 const unsigned char *extension_data_end;
1539
Jerry Yu0c354a22022-08-29 15:25:36 +08001540 /* RFC 8446, section 4.2.11
Jerry Yu1c9247c2022-07-21 12:37:39 +08001541 *
1542 * The "pre_shared_key" extension MUST be the last extension in the
1543 * ClientHello (this facilitates implementation as described below).
1544 * Servers MUST check that it is the last extension and otherwise fail
1545 * the handshake with an "illegal_parameter" alert.
1546 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001547 if (handshake->received_extensions & MBEDTLS_SSL_EXT_MASK(PRE_SHARED_KEY)) {
Jerry Yu1c9247c2022-07-21 12:37:39 +08001548 MBEDTLS_SSL_DEBUG_MSG(
Gilles Peskine449bd832023-01-11 14:50:10 +01001549 3, ("pre_shared_key is not last extension."));
Jerry Yu1c9247c2022-07-21 12:37:39 +08001550 MBEDTLS_SSL_PEND_FATAL_ALERT(
1551 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER,
Gilles Peskine449bd832023-01-11 14:50:10 +01001552 MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER);
1553 return MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER;
Jerry Yu1c9247c2022-07-21 12:37:39 +08001554 }
1555
Gilles Peskine449bd832023-01-11 14:50:10 +01001556 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, extensions_end, 4);
1557 extension_type = MBEDTLS_GET_UINT16_BE(p, 0);
1558 extension_data_len = MBEDTLS_GET_UINT16_BE(p, 2);
XiaokangQian7807f9f2022-02-15 10:04:37 +00001559 p += 4;
1560
Gilles Peskine449bd832023-01-11 14:50:10 +01001561 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, extensions_end, extension_data_len);
XiaokangQian7807f9f2022-02-15 10:04:37 +00001562 extension_data_end = p + extension_data_len;
1563
Jerry Yuc4bf5d62022-10-29 09:08:47 +08001564 ret = mbedtls_ssl_tls13_check_received_extension(
Gilles Peskine449bd832023-01-11 14:50:10 +01001565 ssl, MBEDTLS_SSL_HS_CLIENT_HELLO, extension_type,
1566 MBEDTLS_SSL_TLS1_3_ALLOWED_EXTS_OF_CH);
1567 if (ret != 0) {
1568 return ret;
1569 }
Jerry Yue18dc7e2022-08-04 16:29:22 +08001570
Gilles Peskine449bd832023-01-11 14:50:10 +01001571 switch (extension_type) {
XiaokangQian40a35232022-05-07 09:02:40 +00001572#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
1573 case MBEDTLS_TLS_EXT_SERVERNAME:
Gilles Peskine449bd832023-01-11 14:50:10 +01001574 MBEDTLS_SSL_DEBUG_MSG(3, ("found ServerName extension"));
1575 ret = mbedtls_ssl_parse_server_name_ext(ssl, p,
1576 extension_data_end);
1577 if (ret != 0) {
XiaokangQian40a35232022-05-07 09:02:40 +00001578 MBEDTLS_SSL_DEBUG_RET(
Gilles Peskine449bd832023-01-11 14:50:10 +01001579 1, "mbedtls_ssl_parse_servername_ext", ret);
1580 return ret;
XiaokangQian40a35232022-05-07 09:02:40 +00001581 }
XiaokangQian40a35232022-05-07 09:02:40 +00001582 break;
1583#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
1584
Przemek Stekiel8c0a9532023-06-15 16:48:19 +02001585#if defined(PSA_WANT_ALG_ECDH) || defined(PSA_WANT_ALG_FFDH)
XiaokangQian7807f9f2022-02-15 10:04:37 +00001586 case MBEDTLS_TLS_EXT_SUPPORTED_GROUPS:
Gilles Peskine449bd832023-01-11 14:50:10 +01001587 MBEDTLS_SSL_DEBUG_MSG(3, ("found supported group extension"));
XiaokangQian7807f9f2022-02-15 10:04:37 +00001588
1589 /* Supported Groups Extension
1590 *
1591 * When sent by the client, the "supported_groups" extension
1592 * indicates the named groups which the client supports,
1593 * ordered from most preferred to least preferred.
1594 */
Jerry Yuc1be19f2022-04-23 16:11:39 +08001595 ret = ssl_tls13_parse_supported_groups_ext(
Gilles Peskine449bd832023-01-11 14:50:10 +01001596 ssl, p, extension_data_end);
1597 if (ret != 0) {
Xiaokang Qian9f1747b2023-03-30 02:50:04 +00001598 MBEDTLS_SSL_DEBUG_RET(
Xiaokang Qian8bce0e62023-04-04 10:15:48 +00001599 1, "ssl_tls13_parse_supported_groups_ext", ret);
Gilles Peskine449bd832023-01-11 14:50:10 +01001600 return ret;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001601 }
1602
XiaokangQian7807f9f2022-02-15 10:04:37 +00001603 break;
Przemek Stekiel8c0a9532023-06-15 16:48:19 +02001604#endif /* PSA_WANT_ALG_ECDH || PSA_WANT_ALG_FFDH*/
XiaokangQian7807f9f2022-02-15 10:04:37 +00001605
Valerio Settic9ae8622023-07-25 11:23:50 +02001606#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_EPHEMERAL_ENABLED)
XiaokangQian7807f9f2022-02-15 10:04:37 +00001607 case MBEDTLS_TLS_EXT_KEY_SHARE:
Gilles Peskine449bd832023-01-11 14:50:10 +01001608 MBEDTLS_SSL_DEBUG_MSG(3, ("found key share extension"));
XiaokangQian7807f9f2022-02-15 10:04:37 +00001609
1610 /*
1611 * Key Share Extension
1612 *
1613 * When sent by the client, the "key_share" extension
1614 * contains the endpoint's cryptographic parameters for
1615 * ECDHE/DHE key establishment methods.
1616 */
Jerry Yuc1be19f2022-04-23 16:11:39 +08001617 ret = ssl_tls13_parse_key_shares_ext(
Gilles Peskine449bd832023-01-11 14:50:10 +01001618 ssl, p, extension_data_end);
1619 if (ret == SSL_TLS1_3_PARSE_KEY_SHARES_EXT_NO_MATCH) {
Ronald Cron8a74f072023-06-14 17:59:29 +02001620 MBEDTLS_SSL_DEBUG_MSG(2, ("No usable share for key agreement."));
1621 no_usable_share_for_key_agreement = 1;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001622 }
1623
Gilles Peskine449bd832023-01-11 14:50:10 +01001624 if (ret < 0) {
Jerry Yu582dd062022-04-22 21:59:01 +08001625 MBEDTLS_SSL_DEBUG_RET(
Gilles Peskine449bd832023-01-11 14:50:10 +01001626 1, "ssl_tls13_parse_key_shares_ext", ret);
1627 return ret;
Jerry Yu582dd062022-04-22 21:59:01 +08001628 }
XiaokangQian7807f9f2022-02-15 10:04:37 +00001629
XiaokangQian7807f9f2022-02-15 10:04:37 +00001630 break;
Valerio Settic9ae8622023-07-25 11:23:50 +02001631#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_EPHEMERAL_ENABLED */
XiaokangQian7807f9f2022-02-15 10:04:37 +00001632
1633 case MBEDTLS_TLS_EXT_SUPPORTED_VERSIONS:
Ronald Cron8c527d02023-03-07 15:47:47 +01001634 /* Already parsed */
XiaokangQian7807f9f2022-02-15 10:04:37 +00001635 break;
1636
Ronald Cron41a443a2022-10-04 16:38:25 +02001637#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED)
Jerry Yue19e3b92022-07-08 12:04:51 +00001638 case MBEDTLS_TLS_EXT_PSK_KEY_EXCHANGE_MODES:
Xiaokang Qian9f1747b2023-03-30 02:50:04 +00001639 MBEDTLS_SSL_DEBUG_MSG(
1640 3, ("found psk key exchange modes extension"));
Jerry Yue19e3b92022-07-08 12:04:51 +00001641
1642 ret = ssl_tls13_parse_key_exchange_modes_ext(
Gilles Peskine449bd832023-01-11 14:50:10 +01001643 ssl, p, extension_data_end);
1644 if (ret != 0) {
Jerry Yue19e3b92022-07-08 12:04:51 +00001645 MBEDTLS_SSL_DEBUG_RET(
Gilles Peskine449bd832023-01-11 14:50:10 +01001646 1, "ssl_tls13_parse_key_exchange_modes_ext", ret);
1647 return ret;
Jerry Yue19e3b92022-07-08 12:04:51 +00001648 }
1649
Jerry Yue19e3b92022-07-08 12:04:51 +00001650 break;
Ronald Cron41a443a2022-10-04 16:38:25 +02001651#endif
Jerry Yue19e3b92022-07-08 12:04:51 +00001652
Jerry Yu1c105562022-07-10 06:32:38 +00001653 case MBEDTLS_TLS_EXT_PRE_SHARED_KEY:
Gilles Peskine449bd832023-01-11 14:50:10 +01001654 MBEDTLS_SSL_DEBUG_MSG(3, ("found pre_shared_key extension"));
1655 if ((handshake->received_extensions &
1656 MBEDTLS_SSL_EXT_MASK(PSK_KEY_EXCHANGE_MODES)) == 0) {
Jerry Yu13ab81d2022-07-22 23:17:11 +08001657 MBEDTLS_SSL_PEND_FATAL_ALERT(
1658 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER,
Gilles Peskine449bd832023-01-11 14:50:10 +01001659 MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER);
1660 return MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER;
Jerry Yu13ab81d2022-07-22 23:17:11 +08001661 }
Ronald Cron41a443a2022-10-04 16:38:25 +02001662#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED)
Jerry Yu1c105562022-07-10 06:32:38 +00001663 /* Delay processing of the PSK identity once we have
1664 * found out which algorithms to use. We keep a pointer
1665 * to the buffer and the size for later processing.
1666 */
Jerry Yu29d9faa2022-08-23 17:52:45 +08001667 pre_shared_key_ext = p;
Jerry Yu1c105562022-07-10 06:32:38 +00001668 pre_shared_key_ext_end = extension_data_end;
Jerry Yue18dc7e2022-08-04 16:29:22 +08001669#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED */
Jerry Yu1c105562022-07-10 06:32:38 +00001670 break;
Jerry Yu1c105562022-07-10 06:32:38 +00001671
XiaokangQianacb39922022-06-17 10:18:48 +00001672#if defined(MBEDTLS_SSL_ALPN)
1673 case MBEDTLS_TLS_EXT_ALPN:
Gilles Peskine449bd832023-01-11 14:50:10 +01001674 MBEDTLS_SSL_DEBUG_MSG(3, ("found alpn extension"));
XiaokangQianacb39922022-06-17 10:18:48 +00001675
Gilles Peskine449bd832023-01-11 14:50:10 +01001676 ret = mbedtls_ssl_parse_alpn_ext(ssl, p, extension_data_end);
1677 if (ret != 0) {
XiaokangQianacb39922022-06-17 10:18:48 +00001678 MBEDTLS_SSL_DEBUG_RET(
Gilles Peskine449bd832023-01-11 14:50:10 +01001679 1, ("mbedtls_ssl_parse_alpn_ext"), ret);
1680 return ret;
XiaokangQianacb39922022-06-17 10:18:48 +00001681 }
XiaokangQianacb39922022-06-17 10:18:48 +00001682 break;
1683#endif /* MBEDTLS_SSL_ALPN */
1684
Ronald Cron928cbd32022-10-04 16:14:26 +02001685#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED)
XiaokangQian7807f9f2022-02-15 10:04:37 +00001686 case MBEDTLS_TLS_EXT_SIG_ALG:
Gilles Peskine449bd832023-01-11 14:50:10 +01001687 MBEDTLS_SSL_DEBUG_MSG(3, ("found signature_algorithms extension"));
XiaokangQian7807f9f2022-02-15 10:04:37 +00001688
Gabor Mezei078e8032022-04-27 21:17:56 +02001689 ret = mbedtls_ssl_parse_sig_alg_ext(
Gilles Peskine449bd832023-01-11 14:50:10 +01001690 ssl, p, extension_data_end);
1691 if (ret != 0) {
Xiaokang Qian49f39c12023-04-06 02:31:02 +00001692 MBEDTLS_SSL_DEBUG_RET(
1693 1, "mbedtls_ssl_parse_sig_alg_ext", ret);
Gilles Peskine449bd832023-01-11 14:50:10 +01001694 return ret;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001695 }
XiaokangQian7807f9f2022-02-15 10:04:37 +00001696 break;
Ronald Cron928cbd32022-10-04 16:14:26 +02001697#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED */
XiaokangQian7807f9f2022-02-15 10:04:37 +00001698
Jan Bruckner151f6422023-02-10 12:45:19 +01001699#if defined(MBEDTLS_SSL_RECORD_SIZE_LIMIT)
1700 case MBEDTLS_TLS_EXT_RECORD_SIZE_LIMIT:
1701 MBEDTLS_SSL_DEBUG_MSG(3, ("found record_size_limit extension"));
1702
Xiaokang Qian9f1747b2023-03-30 02:50:04 +00001703 ret = mbedtls_ssl_tls13_parse_record_size_limit_ext(
1704 ssl, p, extension_data_end);
Jan Bruckner151f6422023-02-10 12:45:19 +01001705
Xiaokang Qian09c3ccc2023-04-04 10:18:38 +00001706 /*
1707 * TODO: Return unconditionally here until we handle the record
1708 * size limit correctly.
1709 * Once handled correctly, only return in case of errors.
1710 */
Jan Bruckner151f6422023-02-10 12:45:19 +01001711 return ret;
1712
1713 break;
1714#endif /* MBEDTLS_SSL_RECORD_SIZE_LIMIT */
1715
XiaokangQian7807f9f2022-02-15 10:04:37 +00001716 default:
Jerry Yu79aa7212022-11-08 21:30:21 +08001717 MBEDTLS_SSL_PRINT_EXT(
Jerry Yu63a459c2022-10-31 13:38:40 +08001718 3, MBEDTLS_SSL_HS_CLIENT_HELLO,
Gilles Peskine449bd832023-01-11 14:50:10 +01001719 extension_type, "( ignored )");
Jerry Yu0c354a22022-08-29 15:25:36 +08001720 break;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001721 }
1722
1723 p += extension_data_len;
1724 }
1725
Gilles Peskine449bd832023-01-11 14:50:10 +01001726 MBEDTLS_SSL_PRINT_EXTS(3, MBEDTLS_SSL_HS_CLIENT_HELLO,
1727 handshake->received_extensions);
Jerry Yue18dc7e2022-08-04 16:29:22 +08001728
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +01001729 ret = mbedtls_ssl_add_hs_hdr_to_checksum(ssl,
1730 MBEDTLS_SSL_HS_CLIENT_HELLO,
1731 p - buf);
1732 if (0 != ret) {
1733 MBEDTLS_SSL_DEBUG_RET(1, ("mbedtls_ssl_add_hs_hdr_to_checksum"), ret);
1734 return ret;
1735 }
Jerry Yu032b15ce2022-07-11 06:10:03 +00001736
Ronald Cron41a443a2022-10-04 16:38:25 +02001737#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED)
XiaokangQian7807f9f2022-02-15 10:04:37 +00001738 /* Update checksum with either
1739 * - The entire content of the CH message, if no PSK extension is present
1740 * - The content up to but excluding the PSK extension, if present.
1741 */
Jerry Yu1c105562022-07-10 06:32:38 +00001742 /* If we've settled on a PSK-based exchange, parse PSK identity ext */
Pengyu Lvcfb23b82023-10-30 15:26:26 +08001743 if (ssl_tls13_check_psk_key_exchange(ssl) ||
1744 ssl_tls13_check_psk_ephemeral_key_exchange(ssl)) {
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +01001745 ret = handshake->update_checksum(ssl, buf,
1746 pre_shared_key_ext - buf);
1747 if (0 != ret) {
1748 MBEDTLS_SSL_DEBUG_RET(1, ("update_checksum"), ret);
1749 return ret;
1750 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001751 ret = ssl_tls13_parse_pre_shared_key_ext(ssl,
1752 pre_shared_key_ext,
1753 pre_shared_key_ext_end,
1754 cipher_suites,
1755 cipher_suites_end);
1756 if (ret == MBEDTLS_ERR_SSL_UNKNOWN_IDENTITY) {
1757 handshake->received_extensions &= ~MBEDTLS_SSL_EXT_MASK(PRE_SHARED_KEY);
1758 } else if (ret != 0) {
Jerry Yu63a459c2022-10-31 13:38:40 +08001759 MBEDTLS_SSL_DEBUG_RET(
Gilles Peskine449bd832023-01-11 14:50:10 +01001760 1, "ssl_tls13_parse_pre_shared_key_ext", ret);
1761 return ret;
Jerry Yu1c105562022-07-10 06:32:38 +00001762 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001763 } else
Ronald Cron41a443a2022-10-04 16:38:25 +02001764#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED */
Jerry Yu1c105562022-07-10 06:32:38 +00001765 {
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +01001766 ret = handshake->update_checksum(ssl, buf, p - buf);
1767 if (0 != ret) {
1768 MBEDTLS_SSL_DEBUG_RET(1, ("update_checksum"), ret);
1769 return ret;
1770 }
Jerry Yu1c105562022-07-10 06:32:38 +00001771 }
XiaokangQian7807f9f2022-02-15 10:04:37 +00001772
Gilles Peskine449bd832023-01-11 14:50:10 +01001773 ret = ssl_tls13_determine_key_exchange_mode(ssl);
1774 if (ret < 0) {
1775 return ret;
1776 }
XiaokangQian7807f9f2022-02-15 10:04:37 +00001777
Ronald Cron8a74f072023-06-14 17:59:29 +02001778 if (ssl->handshake->key_exchange_mode !=
1779 MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK) {
1780 hrr_required = (no_usable_share_for_key_agreement != 0);
1781 }
1782
Gilles Peskine449bd832023-01-11 14:50:10 +01001783 mbedtls_ssl_optimize_checksum(ssl, handshake->ciphersuite_info);
Jerry Yue5834fd2022-08-29 20:16:09 +08001784
Gilles Peskine449bd832023-01-11 14:50:10 +01001785 return hrr_required ? SSL_CLIENT_HELLO_HRR_REQUIRED : SSL_CLIENT_HELLO_OK;
XiaokangQian75fe8c72022-06-15 09:42:45 +00001786}
1787
Jerry Yuab0da372023-02-08 13:55:24 +08001788#if defined(MBEDTLS_SSL_EARLY_DATA)
1789static void ssl_tls13_update_early_data_status(mbedtls_ssl_context *ssl)
1790{
1791 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
1792
1793 if ((handshake->received_extensions &
1794 MBEDTLS_SSL_EXT_MASK(EARLY_DATA)) == 0) {
1795 MBEDTLS_SSL_DEBUG_MSG(
Jerry Yub47b2992023-10-18 15:50:35 +08001796 1, ("EarlyData: no early data extension received."));
Jerry Yuab0da372023-02-08 13:55:24 +08001797 ssl->early_data_status = MBEDTLS_SSL_EARLY_DATA_STATUS_NOT_RECEIVED;
1798 return;
1799 }
1800
1801 ssl->early_data_status = MBEDTLS_SSL_EARLY_DATA_STATUS_REJECTED;
1802
Jerry Yu985c9672022-12-04 14:06:30 +08001803 if (ssl->conf->early_data_enabled == MBEDTLS_SSL_EARLY_DATA_DISABLED) {
1804 MBEDTLS_SSL_DEBUG_MSG(
Jerry Yu985c9672022-12-04 14:06:30 +08001805 1,
Jerry Yu454dda32023-10-31 15:13:54 +08001806 ("EarlyData: rejected, feature disabled in server configuration."));
Jerry Yu985c9672022-12-04 14:06:30 +08001807 return;
1808 }
1809
Jerry Yu454dda32023-10-31 15:13:54 +08001810 if (!handshake->resume) {
1811 /* We currently support early data only in the case of PSKs established
1812 via a NewSessionTicket message thus in the case of a session
1813 resumption. */
Jerry Yu985c9672022-12-04 14:06:30 +08001814 MBEDTLS_SSL_DEBUG_MSG(
Jerry Yu7ef9fd82023-11-07 14:30:38 +08001815 1, ("EarlyData: rejected, not a session resumption."));
Jerry Yu985c9672022-12-04 14:06:30 +08001816 return;
1817 }
1818
Jerry Yu82fd6c12023-10-31 16:32:19 +08001819 /* RFC 8446 4.2.10
1820 *
1821 * In order to accept early data, the server MUST have accepted a PSK cipher
1822 * suite and selected the first key offered in the client's "pre_shared_key"
1823 * extension. In addition, it MUST verify that the following values are the
1824 * same as those associated with the selected PSK:
1825 * - The TLS version number
1826 * - The selected cipher suite
1827 * - The selected ALPN [RFC7301] protocol, if any
1828 *
1829 * NOTE:
Jerry Yu7ef9fd82023-11-07 14:30:38 +08001830 * - The TLS version number is checked in
1831 * ssl_tls13_offered_psks_check_identity_match_ticket().
1832 * - ALPN is not checked for the time being (TODO).
Jerry Yu82fd6c12023-10-31 16:32:19 +08001833 */
1834
1835 if (handshake->selected_identity != 0) {
1836 MBEDTLS_SSL_DEBUG_MSG(
Jerry Yu7ef9fd82023-11-07 14:30:38 +08001837 1, ("EarlyData: rejected, the selected key in "
1838 "`pre_shared_key` is not the first one."));
Jerry Yu82fd6c12023-10-31 16:32:19 +08001839 return;
1840 }
1841
1842 if (handshake->ciphersuite_info->id !=
1843 ssl->session_negotiate->ciphersuite) {
1844 MBEDTLS_SSL_DEBUG_MSG(
Jerry Yu7ef9fd82023-11-07 14:30:38 +08001845 1, ("EarlyData: rejected, the selected ciphersuite is not the one "
1846 "of the selected pre-shared key."));
Jerry Yu82fd6c12023-10-31 16:32:19 +08001847 return;
1848
1849 }
Jerry Yu985c9672022-12-04 14:06:30 +08001850
Jerry Yuc2b1bc42023-11-22 10:08:13 +08001851 if (!mbedtls_ssl_session_ticket_allow_early_data(ssl->session_negotiate)) {
Jerry Yufceddb32022-12-12 15:30:34 +08001852 MBEDTLS_SSL_DEBUG_MSG(
1853 1,
Jerry Yuea96ac32023-11-21 17:06:36 +08001854 ("EarlyData: rejected, early_data not allowed in ticket "
1855 "permission bits."));
Jerry Yufceddb32022-12-12 15:30:34 +08001856 return;
1857 }
Jerry Yu985c9672022-12-04 14:06:30 +08001858
1859 ssl->early_data_status = MBEDTLS_SSL_EARLY_DATA_STATUS_ACCEPTED;
1860
Jerry Yuab0da372023-02-08 13:55:24 +08001861}
1862#endif /* MBEDTLS_SSL_EARLY_DATA */
1863
XiaokangQian75fe8c72022-06-15 09:42:45 +00001864/* Update the handshake state machine */
1865
Ronald Cronce7d76e2022-07-08 18:56:49 +02001866MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01001867static int ssl_tls13_postprocess_client_hello(mbedtls_ssl_context *ssl)
XiaokangQian75fe8c72022-06-15 09:42:45 +00001868{
1869 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1870
XiaokangQian7807f9f2022-02-15 10:04:37 +00001871 /*
XiaokangQianfb665a82022-06-15 03:57:21 +00001872 * Server certificate selection
1873 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001874 if (ssl->conf->f_cert_cb && (ret = ssl->conf->f_cert_cb(ssl)) != 0) {
1875 MBEDTLS_SSL_DEBUG_RET(1, "f_cert_cb", ret);
1876 return ret;
XiaokangQianfb665a82022-06-15 03:57:21 +00001877 }
1878#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
1879 ssl->handshake->sni_name = NULL;
1880 ssl->handshake->sni_name_len = 0;
1881#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
XiaokangQian7807f9f2022-02-15 10:04:37 +00001882
Gilles Peskine449bd832023-01-11 14:50:10 +01001883 ret = mbedtls_ssl_tls13_key_schedule_stage_early(ssl);
1884 if (ret != 0) {
1885 MBEDTLS_SSL_DEBUG_RET(1,
1886 "mbedtls_ssl_tls1_3_key_schedule_stage_early", ret);
1887 return ret;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001888 }
1889
Jerry Yuab0da372023-02-08 13:55:24 +08001890#if defined(MBEDTLS_SSL_EARLY_DATA)
1891 /* There is enough information, update early data state. */
1892 ssl_tls13_update_early_data_status(ssl);
Jerry Yu4e9b70e2022-12-04 14:08:02 +08001893
1894 if (ssl->early_data_status == MBEDTLS_SSL_EARLY_DATA_STATUS_ACCEPTED) {
1895 ret = mbedtls_ssl_tls13_compute_early_transform(ssl);
1896 if (ret != 0) {
1897 MBEDTLS_SSL_DEBUG_RET(
1898 1, "mbedtls_ssl_tls13_compute_early_transform", ret);
1899 return ret;
1900 }
1901 }
Jerry Yuab0da372023-02-08 13:55:24 +08001902#endif /* MBEDTLS_SSL_EARLY_DATA */
1903
Gilles Peskine449bd832023-01-11 14:50:10 +01001904 return 0;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001905}
1906
1907/*
XiaokangQianed582dd2022-04-13 08:21:05 +00001908 * Main entry point from the state machine; orchestrates the otherfunctions.
1909 */
1910
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001911MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01001912static int ssl_tls13_process_client_hello(mbedtls_ssl_context *ssl)
XiaokangQianed582dd2022-04-13 08:21:05 +00001913{
1914
XiaokangQian08037552022-04-20 07:16:41 +00001915 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine449bd832023-01-11 14:50:10 +01001916 unsigned char *buf = NULL;
XiaokangQianed582dd2022-04-13 08:21:05 +00001917 size_t buflen = 0;
Jerry Yu4ca91402022-05-09 15:50:57 +08001918 int parse_client_hello_ret;
1919
Gilles Peskine449bd832023-01-11 14:50:10 +01001920 MBEDTLS_SSL_DEBUG_MSG(2, ("=> parse client hello"));
XiaokangQianed582dd2022-04-13 08:21:05 +00001921
Gilles Peskine449bd832023-01-11 14:50:10 +01001922 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_tls13_fetch_handshake_msg(
1923 ssl, MBEDTLS_SSL_HS_CLIENT_HELLO,
1924 &buf, &buflen));
XiaokangQianed582dd2022-04-13 08:21:05 +00001925
Gilles Peskine449bd832023-01-11 14:50:10 +01001926 MBEDTLS_SSL_PROC_CHK_NEG(ssl_tls13_parse_client_hello(ssl, buf,
1927 buf + buflen));
Ronald Cron5af4c7f2023-03-07 20:46:59 +01001928 parse_client_hello_ret = ret; /* Store positive return value of
1929 * parse_client_hello,
1930 * as negative error codes are handled
Jerry Yuf41553b2022-05-09 22:20:30 +08001931 * by MBEDTLS_SSL_PROC_CHK_NEG. */
Jerry Yu4ca91402022-05-09 15:50:57 +08001932
Ronald Cronb828c7d2023-04-03 16:37:22 +02001933 /*
1934 * Version 1.2 of the protocol has been chosen, set the
1935 * ssl->keep_current_message flag for the ClientHello to be kept and parsed
1936 * as a TLS 1.2 ClientHello. We also change ssl->tls_version to
1937 * MBEDTLS_SSL_VERSION_TLS1_2 thus from now on mbedtls_ssl_handshake_step()
1938 * will dispatch to the TLS 1.2 state machine.
1939 */
Ronald Cron5af4c7f2023-03-07 20:46:59 +01001940 if (SSL_CLIENT_HELLO_TLS1_2 == parse_client_hello_ret) {
1941 ssl->keep_current_message = 1;
1942 ssl->tls_version = MBEDTLS_SSL_VERSION_TLS1_2;
1943 return 0;
1944 }
1945
Gilles Peskine449bd832023-01-11 14:50:10 +01001946 MBEDTLS_SSL_PROC_CHK(ssl_tls13_postprocess_client_hello(ssl));
Jerry Yu582dd062022-04-22 21:59:01 +08001947
Ronald Cron5af4c7f2023-03-07 20:46:59 +01001948 if (SSL_CLIENT_HELLO_OK == parse_client_hello_ret) {
Gilles Peskine449bd832023-01-11 14:50:10 +01001949 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_SERVER_HELLO);
1950 } else {
1951 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_HELLO_RETRY_REQUEST);
1952 }
XiaokangQianed582dd2022-04-13 08:21:05 +00001953
1954cleanup:
1955
Gilles Peskine449bd832023-01-11 14:50:10 +01001956 MBEDTLS_SSL_DEBUG_MSG(2, ("<= parse client hello"));
1957 return ret;
XiaokangQianed582dd2022-04-13 08:21:05 +00001958}
1959
1960/*
Jerry Yu1c3e6882022-04-20 21:23:40 +08001961 * Handler for MBEDTLS_SSL_SERVER_HELLO
Jerry Yu5b64ae92022-03-30 17:15:02 +08001962 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001963MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01001964static int ssl_tls13_prepare_server_hello(mbedtls_ssl_context *ssl)
Jerry Yuf4b27e42022-03-30 17:32:21 +08001965{
Jerry Yu637a3f12022-04-20 21:37:58 +08001966 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1967 unsigned char *server_randbytes =
Gilles Peskine449bd832023-01-11 14:50:10 +01001968 ssl->handshake->randbytes + MBEDTLS_CLIENT_HELLO_RANDOM_LEN;
1969 if (ssl->conf->f_rng == NULL) {
1970 MBEDTLS_SSL_DEBUG_MSG(1, ("no RNG provided"));
1971 return MBEDTLS_ERR_SSL_NO_RNG;
Jerry Yuf4b27e42022-03-30 17:32:21 +08001972 }
1973
Gilles Peskine449bd832023-01-11 14:50:10 +01001974 if ((ret = ssl->conf->f_rng(ssl->conf->p_rng, server_randbytes,
1975 MBEDTLS_SERVER_HELLO_RANDOM_LEN)) != 0) {
1976 MBEDTLS_SSL_DEBUG_RET(1, "f_rng", ret);
1977 return ret;
Jerry Yuf4b27e42022-03-30 17:32:21 +08001978 }
1979
Gilles Peskine449bd832023-01-11 14:50:10 +01001980 MBEDTLS_SSL_DEBUG_BUF(3, "server hello, random bytes", server_randbytes,
1981 MBEDTLS_SERVER_HELLO_RANDOM_LEN);
Jerry Yuf4b27e42022-03-30 17:32:21 +08001982
1983#if defined(MBEDTLS_HAVE_TIME)
YxCda609132023-05-22 12:08:12 -07001984 ssl->session_negotiate->start = mbedtls_time(NULL);
Jerry Yuf4b27e42022-03-30 17:32:21 +08001985#endif /* MBEDTLS_HAVE_TIME */
1986
Gilles Peskine449bd832023-01-11 14:50:10 +01001987 return ret;
Jerry Yuf4b27e42022-03-30 17:32:21 +08001988}
1989
Jerry Yu3bf2c642022-03-30 22:02:12 +08001990/*
Jerry Yue74e04a2022-04-21 09:23:16 +08001991 * ssl_tls13_write_server_hello_supported_versions_ext ():
Jerry Yu3bf2c642022-03-30 22:02:12 +08001992 *
1993 * struct {
Jerry Yufb9f54d2022-04-06 10:08:34 +08001994 * ProtocolVersion selected_version;
Jerry Yu3bf2c642022-03-30 22:02:12 +08001995 * } SupportedVersions;
1996 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001997MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yue74e04a2022-04-21 09:23:16 +08001998static int ssl_tls13_write_server_hello_supported_versions_ext(
Gilles Peskine449bd832023-01-11 14:50:10 +01001999 mbedtls_ssl_context *ssl,
2000 unsigned char *buf,
2001 unsigned char *end,
2002 size_t *out_len)
Jerry Yu3bf2c642022-03-30 22:02:12 +08002003{
Jerry Yu3bf2c642022-03-30 22:02:12 +08002004 *out_len = 0;
2005
Gilles Peskine449bd832023-01-11 14:50:10 +01002006 MBEDTLS_SSL_DEBUG_MSG(3, ("server hello, write selected version"));
Jerry Yu3bf2c642022-03-30 22:02:12 +08002007
2008 /* Check if we have space to write the extension:
2009 * - extension_type (2 bytes)
2010 * - extension_data_length (2 bytes)
Jerry Yu349a6132022-04-14 20:52:56 +08002011 * - selected_version (2 bytes)
Jerry Yu3bf2c642022-03-30 22:02:12 +08002012 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002013 MBEDTLS_SSL_CHK_BUF_PTR(buf, end, 6);
Jerry Yu3bf2c642022-03-30 22:02:12 +08002014
Gilles Peskine449bd832023-01-11 14:50:10 +01002015 MBEDTLS_PUT_UINT16_BE(MBEDTLS_TLS_EXT_SUPPORTED_VERSIONS, buf, 0);
Jerry Yu3bf2c642022-03-30 22:02:12 +08002016
Gilles Peskine449bd832023-01-11 14:50:10 +01002017 MBEDTLS_PUT_UINT16_BE(2, buf, 2);
Jerry Yu3bf2c642022-03-30 22:02:12 +08002018
Gilles Peskine449bd832023-01-11 14:50:10 +01002019 mbedtls_ssl_write_version(buf + 4,
2020 ssl->conf->transport,
2021 ssl->tls_version);
Jerry Yu3bf2c642022-03-30 22:02:12 +08002022
Gilles Peskine449bd832023-01-11 14:50:10 +01002023 MBEDTLS_SSL_DEBUG_MSG(3, ("supported version: [%04x]",
2024 ssl->tls_version));
Jerry Yu3bf2c642022-03-30 22:02:12 +08002025
Jerry Yu349a6132022-04-14 20:52:56 +08002026 *out_len = 6;
Jerry Yu3bf2c642022-03-30 22:02:12 +08002027
Jerry Yub95dd362022-11-08 21:19:34 +08002028 mbedtls_ssl_tls13_set_hs_sent_ext_mask(
Gilles Peskine449bd832023-01-11 14:50:10 +01002029 ssl, MBEDTLS_TLS_EXT_SUPPORTED_VERSIONS);
Jerry Yub95dd362022-11-08 21:19:34 +08002030
Gilles Peskine449bd832023-01-11 14:50:10 +01002031 return 0;
Jerry Yu3bf2c642022-03-30 22:02:12 +08002032}
2033
Jerry Yud9436a12022-04-20 22:28:09 +08002034
Jerry Yu3bf2c642022-03-30 22:02:12 +08002035
2036/* Generate and export a single key share. For hybrid KEMs, this can
2037 * be called multiple times with the different components of the hybrid. */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002038MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002039static int ssl_tls13_generate_and_write_key_share(mbedtls_ssl_context *ssl,
2040 uint16_t named_group,
2041 unsigned char *buf,
2042 unsigned char *end,
2043 size_t *out_len)
Jerry Yu3bf2c642022-03-30 22:02:12 +08002044{
2045 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yu955ddd72022-04-22 22:27:33 +08002046
2047 *out_len = 0;
2048
Valerio Settic9ae8622023-07-25 11:23:50 +02002049#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_EPHEMERAL_ENABLED)
Przemek Stekiel29c219c2023-05-31 15:21:04 +02002050 if (mbedtls_ssl_tls13_named_group_is_ecdhe(named_group) ||
Przemek Stekield5f79e72023-06-29 09:08:43 +02002051 mbedtls_ssl_tls13_named_group_is_ffdh(named_group)) {
Przemek Stekiel408569f2023-07-06 11:26:44 +02002052 ret = mbedtls_ssl_tls13_generate_and_write_xxdh_key_exchange(
Gilles Peskine449bd832023-01-11 14:50:10 +01002053 ssl, named_group, buf, end, out_len);
2054 if (ret != 0) {
Jerry Yu89e103c2022-03-30 22:43:29 +08002055 MBEDTLS_SSL_DEBUG_RET(
Przemek Stekiel408569f2023-07-06 11:26:44 +02002056 1, "mbedtls_ssl_tls13_generate_and_write_xxdh_key_exchange",
Gilles Peskine449bd832023-01-11 14:50:10 +01002057 ret);
2058 return ret;
Jerry Yu3bf2c642022-03-30 22:02:12 +08002059 }
Gilles Peskine449bd832023-01-11 14:50:10 +01002060 } else
Valerio Settic9ae8622023-07-25 11:23:50 +02002061#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_EPHEMERAL_ENABLED */
Gilles Peskine449bd832023-01-11 14:50:10 +01002062 if (0 /* Other kinds of KEMs */) {
2063 } else {
Jerry Yu955ddd72022-04-22 22:27:33 +08002064 ((void) ssl);
2065 ((void) named_group);
2066 ((void) buf);
2067 ((void) end);
Jerry Yu3bf2c642022-03-30 22:02:12 +08002068 ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
2069 }
2070
Gilles Peskine449bd832023-01-11 14:50:10 +01002071 return ret;
Jerry Yu3bf2c642022-03-30 22:02:12 +08002072}
2073
2074/*
2075 * ssl_tls13_write_key_share_ext
2076 *
2077 * Structure of key_share extension in ServerHello:
2078 *
Jerry Yu1c3e6882022-04-20 21:23:40 +08002079 * struct {
2080 * NamedGroup group;
2081 * opaque key_exchange<1..2^16-1>;
2082 * } KeyShareEntry;
2083 * struct {
2084 * KeyShareEntry server_share;
2085 * } KeyShareServerHello;
Jerry Yu3bf2c642022-03-30 22:02:12 +08002086 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002087MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002088static int ssl_tls13_write_key_share_ext(mbedtls_ssl_context *ssl,
2089 unsigned char *buf,
2090 unsigned char *end,
2091 size_t *out_len)
Jerry Yu3bf2c642022-03-30 22:02:12 +08002092{
Jerry Yu955ddd72022-04-22 22:27:33 +08002093 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yu3bf2c642022-03-30 22:02:12 +08002094 unsigned char *p = buf;
Jerry Yu57d48412022-04-20 21:50:42 +08002095 uint16_t group = ssl->handshake->offered_group_id;
Jerry Yu3bf2c642022-03-30 22:02:12 +08002096 unsigned char *server_share = buf + 4;
Jerry Yu3bf2c642022-03-30 22:02:12 +08002097 size_t key_exchange_length;
Jerry Yu3bf2c642022-03-30 22:02:12 +08002098
2099 *out_len = 0;
2100
Gilles Peskine449bd832023-01-11 14:50:10 +01002101 MBEDTLS_SSL_DEBUG_MSG(3, ("server hello, adding key share extension"));
Jerry Yu3bf2c642022-03-30 22:02:12 +08002102
Gilles Peskine449bd832023-01-11 14:50:10 +01002103 MBEDTLS_SSL_DEBUG_MSG(2, ("server hello, write selected_group: %s (%04x)",
2104 mbedtls_ssl_named_group_to_str(group),
2105 group));
Jerry Yu58af2332022-09-06 11:19:31 +08002106
Jerry Yu3bf2c642022-03-30 22:02:12 +08002107 /* Check if we have space for header and length fields:
2108 * - extension_type (2 bytes)
2109 * - extension_data_length (2 bytes)
2110 * - group (2 bytes)
2111 * - key_exchange_length (2 bytes)
2112 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002113 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 8);
2114 MBEDTLS_PUT_UINT16_BE(MBEDTLS_TLS_EXT_KEY_SHARE, p, 0);
2115 MBEDTLS_PUT_UINT16_BE(group, server_share, 0);
Jerry Yu3bf2c642022-03-30 22:02:12 +08002116 p += 8;
Jerry Yu57d48412022-04-20 21:50:42 +08002117
Jerry Yu3bf2c642022-03-30 22:02:12 +08002118 /* When we introduce PQC-ECDHE hybrids, we'll want to call this
2119 * function multiple times. */
Jerry Yu955ddd72022-04-22 22:27:33 +08002120 ret = ssl_tls13_generate_and_write_key_share(
Gilles Peskine449bd832023-01-11 14:50:10 +01002121 ssl, group, server_share + 4, end, &key_exchange_length);
2122 if (ret != 0) {
2123 return ret;
2124 }
Jerry Yu3bf2c642022-03-30 22:02:12 +08002125 p += key_exchange_length;
Jerry Yuc1be19f2022-04-23 16:11:39 +08002126
Gilles Peskine449bd832023-01-11 14:50:10 +01002127 MBEDTLS_PUT_UINT16_BE(key_exchange_length, server_share + 2, 0);
Jerry Yu3bf2c642022-03-30 22:02:12 +08002128
Gilles Peskine449bd832023-01-11 14:50:10 +01002129 MBEDTLS_PUT_UINT16_BE(p - server_share, buf, 2);
Jerry Yu3bf2c642022-03-30 22:02:12 +08002130
Jerry Yu57d48412022-04-20 21:50:42 +08002131 *out_len = p - buf;
Jerry Yu955ddd72022-04-22 22:27:33 +08002132
Gilles Peskine449bd832023-01-11 14:50:10 +01002133 mbedtls_ssl_tls13_set_hs_sent_ext_mask(ssl, MBEDTLS_TLS_EXT_KEY_SHARE);
Jerry Yub95dd362022-11-08 21:19:34 +08002134
Gilles Peskine449bd832023-01-11 14:50:10 +01002135 return 0;
Jerry Yu3bf2c642022-03-30 22:02:12 +08002136}
Jerry Yud9436a12022-04-20 22:28:09 +08002137
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002138MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002139static int ssl_tls13_write_hrr_key_share_ext(mbedtls_ssl_context *ssl,
2140 unsigned char *buf,
2141 unsigned char *end,
2142 size_t *out_len)
Jerry Yu23f7a6f2022-04-23 15:16:45 +08002143{
2144 uint16_t selected_group = ssl->handshake->hrr_selected_group;
2145 /* key_share Extension
2146 *
2147 * struct {
2148 * select (Handshake.msg_type) {
2149 * ...
2150 * case hello_retry_request:
2151 * NamedGroup selected_group;
2152 * ...
2153 * };
2154 * } KeyShare;
2155 */
2156
2157 *out_len = 0;
2158
Jerry Yub0ac10b2022-05-05 11:10:08 +08002159 /*
2160 * For a pure PSK key exchange, there is no group to agree upon. The purpose
2161 * of the HRR is then to transmit a cookie to force the client to demonstrate
2162 * reachability at their apparent network address (primarily useful for DTLS).
2163 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002164 if (!mbedtls_ssl_tls13_key_exchange_mode_with_ephemeral(ssl)) {
2165 return 0;
2166 }
Jerry Yu23f7a6f2022-04-23 15:16:45 +08002167
2168 /* We should only send the key_share extension if the client's initial
2169 * key share was not acceptable. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002170 if (ssl->handshake->offered_group_id != 0) {
2171 MBEDTLS_SSL_DEBUG_MSG(4, ("Skip key_share extension in HRR"));
2172 return 0;
Jerry Yu23f7a6f2022-04-23 15:16:45 +08002173 }
2174
Gilles Peskine449bd832023-01-11 14:50:10 +01002175 if (selected_group == 0) {
2176 MBEDTLS_SSL_DEBUG_MSG(1, ("no matching named group found"));
2177 return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
Jerry Yu23f7a6f2022-04-23 15:16:45 +08002178 }
2179
Jerry Yub0ac10b2022-05-05 11:10:08 +08002180 /* Check if we have enough space:
2181 * - extension_type (2 bytes)
2182 * - extension_data_length (2 bytes)
2183 * - selected_group (2 bytes)
2184 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002185 MBEDTLS_SSL_CHK_BUF_PTR(buf, end, 6);
Jerry Yu23f7a6f2022-04-23 15:16:45 +08002186
Gilles Peskine449bd832023-01-11 14:50:10 +01002187 MBEDTLS_PUT_UINT16_BE(MBEDTLS_TLS_EXT_KEY_SHARE, buf, 0);
2188 MBEDTLS_PUT_UINT16_BE(2, buf, 2);
2189 MBEDTLS_PUT_UINT16_BE(selected_group, buf, 4);
Jerry Yu23f7a6f2022-04-23 15:16:45 +08002190
Gilles Peskine449bd832023-01-11 14:50:10 +01002191 MBEDTLS_SSL_DEBUG_MSG(3,
2192 ("HRR selected_group: %s (%x)",
2193 mbedtls_ssl_named_group_to_str(selected_group),
2194 selected_group));
Jerry Yu23f7a6f2022-04-23 15:16:45 +08002195
2196 *out_len = 6;
Jerry Yu23f7a6f2022-04-23 15:16:45 +08002197
Gilles Peskine449bd832023-01-11 14:50:10 +01002198 mbedtls_ssl_tls13_set_hs_sent_ext_mask(ssl, MBEDTLS_TLS_EXT_KEY_SHARE);
Jerry Yub95dd362022-11-08 21:19:34 +08002199
Gilles Peskine449bd832023-01-11 14:50:10 +01002200 return 0;
Jerry Yu23f7a6f2022-04-23 15:16:45 +08002201}
Jerry Yu3bf2c642022-03-30 22:02:12 +08002202
2203/*
2204 * Structure of ServerHello message:
2205 *
2206 * struct {
2207 * ProtocolVersion legacy_version = 0x0303; // TLS v1.2
2208 * Random random;
2209 * opaque legacy_session_id_echo<0..32>;
2210 * CipherSuite cipher_suite;
2211 * uint8 legacy_compression_method = 0;
2212 * Extension extensions<6..2^16-1>;
2213 * } ServerHello;
2214 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002215MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002216static int ssl_tls13_write_server_hello_body(mbedtls_ssl_context *ssl,
2217 unsigned char *buf,
2218 unsigned char *end,
2219 size_t *out_len,
2220 int is_hrr)
Jerry Yu56404d72022-03-30 17:36:13 +08002221{
Jerry Yu955ddd72022-04-22 22:27:33 +08002222 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yu3bf2c642022-03-30 22:02:12 +08002223 unsigned char *p = buf;
Jerry Yud9436a12022-04-20 22:28:09 +08002224 unsigned char *p_extensions_len;
Jerry Yufbe3e642022-04-25 19:31:51 +08002225 size_t output_len;
Jerry Yu3bf2c642022-03-30 22:02:12 +08002226
2227 *out_len = 0;
Jerry Yu50e00e32022-10-31 14:45:01 +08002228 ssl->handshake->sent_extensions = MBEDTLS_SSL_EXT_MASK_NONE;
Jerry Yu3bf2c642022-03-30 22:02:12 +08002229
Jerry Yucfc04b32022-04-21 09:31:58 +08002230 /* ...
2231 * ProtocolVersion legacy_version = 0x0303; // TLS 1.2
2232 * ...
2233 * with ProtocolVersion defined as:
2234 * uint16 ProtocolVersion;
Jerry Yu3bf2c642022-03-30 22:02:12 +08002235 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002236 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 2);
2237 MBEDTLS_PUT_UINT16_BE(0x0303, p, 0);
Jerry Yu3bf2c642022-03-30 22:02:12 +08002238 p += 2;
2239
Jerry Yu1c3e6882022-04-20 21:23:40 +08002240 /* ...
2241 * Random random;
2242 * ...
Jerry Yucfc04b32022-04-21 09:31:58 +08002243 * with Random defined as:
2244 * opaque Random[MBEDTLS_SERVER_HELLO_RANDOM_LEN];
Jerry Yu1c3e6882022-04-20 21:23:40 +08002245 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002246 MBEDTLS_SSL_CHK_BUF_PTR(p, end, MBEDTLS_SERVER_HELLO_RANDOM_LEN);
2247 if (is_hrr) {
2248 memcpy(p, mbedtls_ssl_tls13_hello_retry_request_magic,
2249 MBEDTLS_SERVER_HELLO_RANDOM_LEN);
2250 } else {
2251 memcpy(p, &ssl->handshake->randbytes[MBEDTLS_CLIENT_HELLO_RANDOM_LEN],
2252 MBEDTLS_SERVER_HELLO_RANDOM_LEN);
Jerry Yu23f7a6f2022-04-23 15:16:45 +08002253 }
Gilles Peskine449bd832023-01-11 14:50:10 +01002254 MBEDTLS_SSL_DEBUG_BUF(3, "server hello, random bytes",
2255 p, MBEDTLS_SERVER_HELLO_RANDOM_LEN);
Jerry Yu3bf2c642022-03-30 22:02:12 +08002256 p += MBEDTLS_SERVER_HELLO_RANDOM_LEN;
2257
Jerry Yucfc04b32022-04-21 09:31:58 +08002258 /* ...
2259 * opaque legacy_session_id_echo<0..32>;
2260 * ...
Jerry Yu3bf2c642022-03-30 22:02:12 +08002261 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002262 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 1 + ssl->session_negotiate->id_len);
2263 *p++ = (unsigned char) ssl->session_negotiate->id_len;
2264 if (ssl->session_negotiate->id_len > 0) {
2265 memcpy(p, &ssl->session_negotiate->id[0],
2266 ssl->session_negotiate->id_len);
Jerry Yu3bf2c642022-03-30 22:02:12 +08002267 p += ssl->session_negotiate->id_len;
Jerry Yu955ddd72022-04-22 22:27:33 +08002268
Gilles Peskine449bd832023-01-11 14:50:10 +01002269 MBEDTLS_SSL_DEBUG_BUF(3, "session id", ssl->session_negotiate->id,
2270 ssl->session_negotiate->id_len);
Jerry Yu3bf2c642022-03-30 22:02:12 +08002271 }
2272
Jerry Yucfc04b32022-04-21 09:31:58 +08002273 /* ...
2274 * CipherSuite cipher_suite;
2275 * ...
2276 * with CipherSuite defined as:
2277 * uint8 CipherSuite[2];
Jerry Yu3bf2c642022-03-30 22:02:12 +08002278 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002279 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 2);
2280 MBEDTLS_PUT_UINT16_BE(ssl->session_negotiate->ciphersuite, p, 0);
Jerry Yu3bf2c642022-03-30 22:02:12 +08002281 p += 2;
Gilles Peskine449bd832023-01-11 14:50:10 +01002282 MBEDTLS_SSL_DEBUG_MSG(3,
2283 ("server hello, chosen ciphersuite: %s ( id=%d )",
2284 mbedtls_ssl_get_ciphersuite_name(
2285 ssl->session_negotiate->ciphersuite),
2286 ssl->session_negotiate->ciphersuite));
Jerry Yu3bf2c642022-03-30 22:02:12 +08002287
Jerry Yucfc04b32022-04-21 09:31:58 +08002288 /* ...
2289 * uint8 legacy_compression_method = 0;
2290 * ...
2291 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002292 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 1);
Thomas Daubney31e03a82022-07-25 15:59:25 +01002293 *p++ = MBEDTLS_SSL_COMPRESS_NULL;
Jerry Yu3bf2c642022-03-30 22:02:12 +08002294
Jerry Yucfc04b32022-04-21 09:31:58 +08002295 /* ...
2296 * Extension extensions<6..2^16-1>;
2297 * ...
2298 * struct {
2299 * ExtensionType extension_type; (2 bytes)
2300 * opaque extension_data<0..2^16-1>;
2301 * } Extension;
2302 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002303 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 2);
Jerry Yud9436a12022-04-20 22:28:09 +08002304 p_extensions_len = p;
Jerry Yu3bf2c642022-03-30 22:02:12 +08002305 p += 2;
2306
Gilles Peskine449bd832023-01-11 14:50:10 +01002307 if ((ret = ssl_tls13_write_server_hello_supported_versions_ext(
2308 ssl, p, end, &output_len)) != 0) {
Jerry Yu955ddd72022-04-22 22:27:33 +08002309 MBEDTLS_SSL_DEBUG_RET(
Gilles Peskine449bd832023-01-11 14:50:10 +01002310 1, "ssl_tls13_write_server_hello_supported_versions_ext", ret);
2311 return ret;
Jerry Yu3bf2c642022-03-30 22:02:12 +08002312 }
2313 p += output_len;
2314
Gilles Peskine449bd832023-01-11 14:50:10 +01002315 if (mbedtls_ssl_tls13_key_exchange_mode_with_ephemeral(ssl)) {
2316 if (is_hrr) {
2317 ret = ssl_tls13_write_hrr_key_share_ext(ssl, p, end, &output_len);
2318 } else {
2319 ret = ssl_tls13_write_key_share_ext(ssl, p, end, &output_len);
2320 }
2321 if (ret != 0) {
2322 return ret;
2323 }
Jerry Yu3bf2c642022-03-30 22:02:12 +08002324 p += output_len;
2325 }
Jerry Yu3bf2c642022-03-30 22:02:12 +08002326
Ronald Cron41a443a2022-10-04 16:38:25 +02002327#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01002328 if (!is_hrr && mbedtls_ssl_tls13_key_exchange_mode_with_psk(ssl)) {
2329 ret = ssl_tls13_write_server_pre_shared_key_ext(ssl, p, end, &output_len);
2330 if (ret != 0) {
2331 MBEDTLS_SSL_DEBUG_RET(1, "ssl_tls13_write_server_pre_shared_key_ext",
2332 ret);
2333 return ret;
Jerry Yu032b15ce2022-07-11 06:10:03 +00002334 }
2335 p += output_len;
2336 }
Ronald Cron41a443a2022-10-04 16:38:25 +02002337#endif
Jerry Yu032b15ce2022-07-11 06:10:03 +00002338
Gilles Peskine449bd832023-01-11 14:50:10 +01002339 MBEDTLS_PUT_UINT16_BE(p - p_extensions_len - 2, p_extensions_len, 0);
Jerry Yu3bf2c642022-03-30 22:02:12 +08002340
Gilles Peskine449bd832023-01-11 14:50:10 +01002341 MBEDTLS_SSL_DEBUG_BUF(4, "server hello extensions",
2342 p_extensions_len, p - p_extensions_len);
Jerry Yu3bf2c642022-03-30 22:02:12 +08002343
Jerry Yud9436a12022-04-20 22:28:09 +08002344 *out_len = p - buf;
Jerry Yu3bf2c642022-03-30 22:02:12 +08002345
Gilles Peskine449bd832023-01-11 14:50:10 +01002346 MBEDTLS_SSL_DEBUG_BUF(3, "server hello", buf, *out_len);
Jerry Yu3bf2c642022-03-30 22:02:12 +08002347
Jerry Yu7de2ff02022-11-08 21:43:46 +08002348 MBEDTLS_SSL_PRINT_EXTS(
Jerry Yu4b8f2f72022-10-31 13:31:22 +08002349 3, is_hrr ? MBEDTLS_SSL_TLS1_3_HS_HELLO_RETRY_REQUEST :
Gilles Peskine449bd832023-01-11 14:50:10 +01002350 MBEDTLS_SSL_HS_SERVER_HELLO,
2351 ssl->handshake->sent_extensions);
Jerry Yu4b8f2f72022-10-31 13:31:22 +08002352
Gilles Peskine449bd832023-01-11 14:50:10 +01002353 return ret;
Jerry Yu56404d72022-03-30 17:36:13 +08002354}
2355
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002356MBEDTLS_CHECK_RETURN_CRITICAL
Xiaokang Qian934ce6f2023-02-06 10:23:04 +00002357static int ssl_tls13_finalize_server_hello(mbedtls_ssl_context *ssl)
Jerry Yue110d252022-05-05 10:19:22 +08002358{
2359 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine449bd832023-01-11 14:50:10 +01002360 ret = mbedtls_ssl_tls13_compute_handshake_transform(ssl);
2361 if (ret != 0) {
2362 MBEDTLS_SSL_DEBUG_RET(1,
2363 "mbedtls_ssl_tls13_compute_handshake_transform",
2364 ret);
2365 return ret;
Jerry Yue110d252022-05-05 10:19:22 +08002366 }
2367
Gilles Peskine449bd832023-01-11 14:50:10 +01002368 return ret;
Jerry Yue110d252022-05-05 10:19:22 +08002369}
2370
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002371MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002372static int ssl_tls13_write_server_hello(mbedtls_ssl_context *ssl)
Jerry Yu5b64ae92022-03-30 17:15:02 +08002373{
Jerry Yu637a3f12022-04-20 21:37:58 +08002374 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yuf4b27e42022-03-30 17:32:21 +08002375 unsigned char *buf;
2376 size_t buf_len, msg_len;
2377
Gilles Peskine449bd832023-01-11 14:50:10 +01002378 MBEDTLS_SSL_DEBUG_MSG(2, ("=> write server hello"));
Jerry Yuf4b27e42022-03-30 17:32:21 +08002379
Gilles Peskine449bd832023-01-11 14:50:10 +01002380 MBEDTLS_SSL_PROC_CHK(ssl_tls13_prepare_server_hello(ssl));
Jerry Yuf4b27e42022-03-30 17:32:21 +08002381
Xiaokang Qian9f1747b2023-03-30 02:50:04 +00002382 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_start_handshake_msg(
2383 ssl, MBEDTLS_SSL_HS_SERVER_HELLO, &buf, &buf_len));
Jerry Yuf4b27e42022-03-30 17:32:21 +08002384
Gilles Peskine449bd832023-01-11 14:50:10 +01002385 MBEDTLS_SSL_PROC_CHK(ssl_tls13_write_server_hello_body(ssl, buf,
2386 buf + buf_len,
2387 &msg_len,
2388 0));
Jerry Yuf4b27e42022-03-30 17:32:21 +08002389
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +01002390 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_add_hs_msg_to_checksum(
Manuel Pégourié-Gonnard43cc1272023-02-06 11:48:19 +01002391 ssl, MBEDTLS_SSL_HS_SERVER_HELLO, buf, msg_len));
Jerry Yuf4b27e42022-03-30 17:32:21 +08002392
Gilles Peskine449bd832023-01-11 14:50:10 +01002393 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_finish_handshake_msg(
2394 ssl, buf_len, msg_len));
Jerry Yu637a3f12022-04-20 21:37:58 +08002395
Xiaokang Qian934ce6f2023-02-06 10:23:04 +00002396 MBEDTLS_SSL_PROC_CHK(ssl_tls13_finalize_server_hello(ssl));
Jerry Yue110d252022-05-05 10:19:22 +08002397
Gabor Mezei7b39bf12022-05-24 16:04:14 +02002398#if defined(MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE)
2399 /* The server sends a dummy change_cipher_spec record immediately
2400 * after its first handshake message. This may either be after
2401 * a ServerHello or a HelloRetryRequest.
2402 */
Gabor Mezei96ae9262022-06-28 11:45:18 +02002403 mbedtls_ssl_handshake_set_state(
Gilles Peskine449bd832023-01-11 14:50:10 +01002404 ssl, MBEDTLS_SSL_SERVER_CCS_AFTER_SERVER_HELLO);
Gabor Mezei7b39bf12022-05-24 16:04:14 +02002405#else
Gilles Peskine449bd832023-01-11 14:50:10 +01002406 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_ENCRYPTED_EXTENSIONS);
Gabor Mezei7b39bf12022-05-24 16:04:14 +02002407#endif /* MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE */
Jerry Yue110d252022-05-05 10:19:22 +08002408
Jerry Yuf4b27e42022-03-30 17:32:21 +08002409cleanup:
2410
Gilles Peskine449bd832023-01-11 14:50:10 +01002411 MBEDTLS_SSL_DEBUG_MSG(2, ("<= write server hello"));
2412 return ret;
Jerry Yu5b64ae92022-03-30 17:15:02 +08002413}
2414
Jerry Yu23d1a252022-05-12 18:08:59 +08002415
2416/*
2417 * Handler for MBEDTLS_SSL_HELLO_RETRY_REQUEST
2418 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002419MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002420static int ssl_tls13_prepare_hello_retry_request(mbedtls_ssl_context *ssl)
Jerry Yu23d1a252022-05-12 18:08:59 +08002421{
2422 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine449bd832023-01-11 14:50:10 +01002423 if (ssl->handshake->hello_retry_request_count > 0) {
2424 MBEDTLS_SSL_DEBUG_MSG(1, ("Too many HRRs"));
2425 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE,
2426 MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE);
2427 return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
Jerry Yu23d1a252022-05-12 18:08:59 +08002428 }
2429
2430 /*
2431 * Create stateless transcript hash for HRR
2432 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002433 MBEDTLS_SSL_DEBUG_MSG(4, ("Reset transcript for HRR"));
2434 ret = mbedtls_ssl_reset_transcript_for_hrr(ssl);
2435 if (ret != 0) {
2436 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_reset_transcript_for_hrr", ret);
2437 return ret;
Jerry Yu23d1a252022-05-12 18:08:59 +08002438 }
Gilles Peskine449bd832023-01-11 14:50:10 +01002439 mbedtls_ssl_session_reset_msg_layer(ssl, 0);
Jerry Yu23d1a252022-05-12 18:08:59 +08002440
Gilles Peskine449bd832023-01-11 14:50:10 +01002441 return 0;
Jerry Yu23d1a252022-05-12 18:08:59 +08002442}
2443
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002444MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002445static int ssl_tls13_write_hello_retry_request(mbedtls_ssl_context *ssl)
Jerry Yu23d1a252022-05-12 18:08:59 +08002446{
2447 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
2448 unsigned char *buf;
2449 size_t buf_len, msg_len;
2450
Gilles Peskine449bd832023-01-11 14:50:10 +01002451 MBEDTLS_SSL_DEBUG_MSG(2, ("=> write hello retry request"));
Jerry Yu23d1a252022-05-12 18:08:59 +08002452
Gilles Peskine449bd832023-01-11 14:50:10 +01002453 MBEDTLS_SSL_PROC_CHK(ssl_tls13_prepare_hello_retry_request(ssl));
Jerry Yu23d1a252022-05-12 18:08:59 +08002454
Gilles Peskine449bd832023-01-11 14:50:10 +01002455 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_start_handshake_msg(
2456 ssl, MBEDTLS_SSL_HS_SERVER_HELLO,
2457 &buf, &buf_len));
Jerry Yu23d1a252022-05-12 18:08:59 +08002458
Gilles Peskine449bd832023-01-11 14:50:10 +01002459 MBEDTLS_SSL_PROC_CHK(ssl_tls13_write_server_hello_body(ssl, buf,
2460 buf + buf_len,
2461 &msg_len,
2462 1));
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +01002463 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_add_hs_msg_to_checksum(
Manuel Pégourié-Gonnard43cc1272023-02-06 11:48:19 +01002464 ssl, MBEDTLS_SSL_HS_SERVER_HELLO, buf, msg_len));
Jerry Yu23d1a252022-05-12 18:08:59 +08002465
2466
Gilles Peskine449bd832023-01-11 14:50:10 +01002467 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_finish_handshake_msg(ssl, buf_len,
2468 msg_len));
Jerry Yu23d1a252022-05-12 18:08:59 +08002469
2470 ssl->handshake->hello_retry_request_count++;
2471
Gabor Mezei7b39bf12022-05-24 16:04:14 +02002472#if defined(MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE)
2473 /* The server sends a dummy change_cipher_spec record immediately
2474 * after its first handshake message. This may either be after
2475 * a ServerHello or a HelloRetryRequest.
2476 */
Gabor Mezei96ae9262022-06-28 11:45:18 +02002477 mbedtls_ssl_handshake_set_state(
Gilles Peskine449bd832023-01-11 14:50:10 +01002478 ssl, MBEDTLS_SSL_SERVER_CCS_AFTER_HELLO_RETRY_REQUEST);
Gabor Mezei7b39bf12022-05-24 16:04:14 +02002479#else
Gilles Peskine449bd832023-01-11 14:50:10 +01002480 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_CLIENT_HELLO);
Gabor Mezei7b39bf12022-05-24 16:04:14 +02002481#endif /* MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE */
Jerry Yu23d1a252022-05-12 18:08:59 +08002482
2483cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +01002484 MBEDTLS_SSL_DEBUG_MSG(2, ("<= write hello retry request"));
2485 return ret;
Jerry Yu23d1a252022-05-12 18:08:59 +08002486}
2487
Jerry Yu5b64ae92022-03-30 17:15:02 +08002488/*
Jerry Yu4d3841a2022-04-16 12:37:19 +08002489 * Handler for MBEDTLS_SSL_ENCRYPTED_EXTENSIONS
2490 */
Jerry Yu4d3841a2022-04-16 12:37:19 +08002491
2492/*
2493 * struct {
2494 * Extension extensions<0..2 ^ 16 - 1>;
2495 * } EncryptedExtensions;
2496 *
2497 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002498MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002499static int ssl_tls13_write_encrypted_extensions_body(mbedtls_ssl_context *ssl,
2500 unsigned char *buf,
2501 unsigned char *end,
2502 size_t *out_len)
Jerry Yu4d3841a2022-04-16 12:37:19 +08002503{
XiaokangQianacb39922022-06-17 10:18:48 +00002504 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yu4d3841a2022-04-16 12:37:19 +08002505 unsigned char *p = buf;
2506 size_t extensions_len = 0;
Jerry Yu8937eb42022-05-03 12:12:14 +08002507 unsigned char *p_extensions_len;
XiaokangQianacb39922022-06-17 10:18:48 +00002508 size_t output_len;
Jerry Yu9da5e5a2022-05-03 15:46:09 +08002509
Jerry Yu4d3841a2022-04-16 12:37:19 +08002510 *out_len = 0;
2511
Gilles Peskine449bd832023-01-11 14:50:10 +01002512 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 2);
Jerry Yu8937eb42022-05-03 12:12:14 +08002513 p_extensions_len = p;
Jerry Yu4d3841a2022-04-16 12:37:19 +08002514 p += 2;
2515
Jerry Yu8937eb42022-05-03 12:12:14 +08002516 ((void) ssl);
XiaokangQianacb39922022-06-17 10:18:48 +00002517 ((void) ret);
2518 ((void) output_len);
2519
2520#if defined(MBEDTLS_SSL_ALPN)
Gilles Peskine449bd832023-01-11 14:50:10 +01002521 ret = mbedtls_ssl_write_alpn_ext(ssl, p, end, &output_len);
2522 if (ret != 0) {
2523 return ret;
2524 }
XiaokangQian95d5f542022-06-24 02:29:26 +00002525 p += output_len;
XiaokangQianacb39922022-06-17 10:18:48 +00002526#endif /* MBEDTLS_SSL_ALPN */
Jerry Yu4d3841a2022-04-16 12:37:19 +08002527
Jerry Yu71c14f12022-12-12 11:10:35 +08002528#if defined(MBEDTLS_SSL_EARLY_DATA)
2529 if (ssl->early_data_status == MBEDTLS_SSL_EARLY_DATA_STATUS_ACCEPTED) {
2530 ret = mbedtls_ssl_tls13_write_early_data_ext(ssl, p, end, &output_len);
2531 if (ret != 0) {
2532 return ret;
2533 }
2534 p += output_len;
2535 }
2536#endif /* MBEDTLS_SSL_EARLY_DATA */
2537
Gilles Peskine449bd832023-01-11 14:50:10 +01002538 extensions_len = (p - p_extensions_len) - 2;
2539 MBEDTLS_PUT_UINT16_BE(extensions_len, p_extensions_len, 0);
Jerry Yu8937eb42022-05-03 12:12:14 +08002540
2541 *out_len = p - buf;
Jerry Yu4d3841a2022-04-16 12:37:19 +08002542
Gilles Peskine449bd832023-01-11 14:50:10 +01002543 MBEDTLS_SSL_DEBUG_BUF(4, "encrypted extensions", buf, *out_len);
Jerry Yu4d3841a2022-04-16 12:37:19 +08002544
Jerry Yu7de2ff02022-11-08 21:43:46 +08002545 MBEDTLS_SSL_PRINT_EXTS(
Gilles Peskine449bd832023-01-11 14:50:10 +01002546 3, MBEDTLS_SSL_HS_ENCRYPTED_EXTENSIONS, ssl->handshake->sent_extensions);
Jerry Yu4b8f2f72022-10-31 13:31:22 +08002547
Gilles Peskine449bd832023-01-11 14:50:10 +01002548 return 0;
Jerry Yu4d3841a2022-04-16 12:37:19 +08002549}
2550
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002551MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002552static int ssl_tls13_write_encrypted_extensions(mbedtls_ssl_context *ssl)
Jerry Yu4d3841a2022-04-16 12:37:19 +08002553{
2554 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
2555 unsigned char *buf;
Jerry Yu39730a72022-05-03 12:14:04 +08002556 size_t buf_len, msg_len;
Jerry Yu4d3841a2022-04-16 12:37:19 +08002557
Gilles Peskine449bd832023-01-11 14:50:10 +01002558 mbedtls_ssl_set_outbound_transform(ssl,
2559 ssl->handshake->transform_handshake);
Gabor Mezei54719122022-06-28 11:34:56 +02002560 MBEDTLS_SSL_DEBUG_MSG(
Gilles Peskine449bd832023-01-11 14:50:10 +01002561 3, ("switching to handshake transform for outbound data"));
Gabor Mezei54719122022-06-28 11:34:56 +02002562
Gilles Peskine449bd832023-01-11 14:50:10 +01002563 MBEDTLS_SSL_DEBUG_MSG(2, ("=> write encrypted extensions"));
Jerry Yu4d3841a2022-04-16 12:37:19 +08002564
Xiaokang Qian9f1747b2023-03-30 02:50:04 +00002565 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_start_handshake_msg(
2566 ssl, MBEDTLS_SSL_HS_ENCRYPTED_EXTENSIONS,
2567 &buf, &buf_len));
Jerry Yu4d3841a2022-04-16 12:37:19 +08002568
Gilles Peskine449bd832023-01-11 14:50:10 +01002569 MBEDTLS_SSL_PROC_CHK(ssl_tls13_write_encrypted_extensions_body(
2570 ssl, buf, buf + buf_len, &msg_len));
Jerry Yu4d3841a2022-04-16 12:37:19 +08002571
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +01002572 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_add_hs_msg_to_checksum(
Xiaokang Qian9f1747b2023-03-30 02:50:04 +00002573 ssl, MBEDTLS_SSL_HS_ENCRYPTED_EXTENSIONS,
2574 buf, msg_len));
Jerry Yu4d3841a2022-04-16 12:37:19 +08002575
Gilles Peskine449bd832023-01-11 14:50:10 +01002576 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_finish_handshake_msg(
2577 ssl, buf_len, msg_len));
Jerry Yu4d3841a2022-04-16 12:37:19 +08002578
Ronald Cron928cbd32022-10-04 16:14:26 +02002579#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01002580 if (mbedtls_ssl_tls13_key_exchange_mode_with_psk(ssl)) {
2581 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_SERVER_FINISHED);
2582 } else {
2583 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_CERTIFICATE_REQUEST);
2584 }
Jerry Yu8937eb42022-05-03 12:12:14 +08002585#else
Gilles Peskine449bd832023-01-11 14:50:10 +01002586 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_SERVER_FINISHED);
Jerry Yu8937eb42022-05-03 12:12:14 +08002587#endif
2588
Jerry Yu4d3841a2022-04-16 12:37:19 +08002589cleanup:
2590
Gilles Peskine449bd832023-01-11 14:50:10 +01002591 MBEDTLS_SSL_DEBUG_MSG(2, ("<= write encrypted extensions"));
2592 return ret;
Jerry Yu4d3841a2022-04-16 12:37:19 +08002593}
2594
Ronald Cron928cbd32022-10-04 16:14:26 +02002595#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED)
XiaokangQiana987e1d2022-05-07 01:25:58 +00002596#define SSL_CERTIFICATE_REQUEST_SEND_REQUEST 0
2597#define SSL_CERTIFICATE_REQUEST_SKIP 1
2598/* Coordination:
2599 * Check whether a CertificateRequest message should be written.
2600 * Returns a negative code on failure, or
2601 * - SSL_CERTIFICATE_REQUEST_SEND_REQUEST
2602 * - SSL_CERTIFICATE_REQUEST_SKIP
2603 * indicating if the writing of the CertificateRequest
2604 * should be skipped or not.
2605 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002606MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002607static int ssl_tls13_certificate_request_coordinate(mbedtls_ssl_context *ssl)
XiaokangQiana987e1d2022-05-07 01:25:58 +00002608{
2609 int authmode;
2610
XiaokangQian40a35232022-05-07 09:02:40 +00002611#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Gilles Peskine449bd832023-01-11 14:50:10 +01002612 if (ssl->handshake->sni_authmode != MBEDTLS_SSL_VERIFY_UNSET) {
XiaokangQian40a35232022-05-07 09:02:40 +00002613 authmode = ssl->handshake->sni_authmode;
Gilles Peskine449bd832023-01-11 14:50:10 +01002614 } else
XiaokangQian40a35232022-05-07 09:02:40 +00002615#endif
XiaokangQiana987e1d2022-05-07 01:25:58 +00002616 authmode = ssl->conf->authmode;
2617
Gilles Peskine449bd832023-01-11 14:50:10 +01002618 if (authmode == MBEDTLS_SSL_VERIFY_NONE) {
Ronald Croneac00ad2022-09-13 10:16:31 +02002619 ssl->session_negotiate->verify_result = MBEDTLS_X509_BADCERT_SKIP_VERIFY;
Gilles Peskine449bd832023-01-11 14:50:10 +01002620 return SSL_CERTIFICATE_REQUEST_SKIP;
Ronald Croneac00ad2022-09-13 10:16:31 +02002621 }
XiaokangQiana987e1d2022-05-07 01:25:58 +00002622
XiaokangQianc3017f62022-05-13 05:55:41 +00002623 ssl->handshake->certificate_request_sent = 1;
XiaokangQian189ded22022-05-10 08:12:17 +00002624
Gilles Peskine449bd832023-01-11 14:50:10 +01002625 return SSL_CERTIFICATE_REQUEST_SEND_REQUEST;
XiaokangQiana987e1d2022-05-07 01:25:58 +00002626}
2627
XiaokangQiancec9ae62022-05-06 07:28:50 +00002628/*
2629 * struct {
2630 * opaque certificate_request_context<0..2^8-1>;
2631 * Extension extensions<2..2^16-1>;
2632 * } CertificateRequest;
2633 *
2634 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002635MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002636static int ssl_tls13_write_certificate_request_body(mbedtls_ssl_context *ssl,
2637 unsigned char *buf,
2638 const unsigned char *end,
2639 size_t *out_len)
XiaokangQiancec9ae62022-05-06 07:28:50 +00002640{
2641 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
2642 unsigned char *p = buf;
XiaokangQianec6efb92022-05-06 09:53:10 +00002643 size_t output_len = 0;
XiaokangQiancec9ae62022-05-06 07:28:50 +00002644 unsigned char *p_extensions_len;
2645
2646 *out_len = 0;
2647
2648 /* Check if we have enough space:
2649 * - certificate_request_context (1 byte)
2650 * - extensions length (2 bytes)
2651 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002652 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 3);
XiaokangQiancec9ae62022-05-06 07:28:50 +00002653
2654 /*
2655 * Write certificate_request_context
2656 */
2657 /*
2658 * We use a zero length context for the normal handshake
2659 * messages. For post-authentication handshake messages
2660 * this request context would be set to a non-zero value.
2661 */
2662 *p++ = 0x0;
2663
2664 /*
2665 * Write extensions
2666 */
2667 /* The extensions must contain the signature_algorithms. */
2668 p_extensions_len = p;
2669 p += 2;
Gilles Peskine449bd832023-01-11 14:50:10 +01002670 ret = mbedtls_ssl_write_sig_alg_ext(ssl, p, end, &output_len);
2671 if (ret != 0) {
2672 return ret;
2673 }
XiaokangQiancec9ae62022-05-06 07:28:50 +00002674
XiaokangQianec6efb92022-05-06 09:53:10 +00002675 p += output_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01002676 MBEDTLS_PUT_UINT16_BE(p - p_extensions_len - 2, p_extensions_len, 0);
XiaokangQiancec9ae62022-05-06 07:28:50 +00002677
2678 *out_len = p - buf;
2679
Jerry Yu7de2ff02022-11-08 21:43:46 +08002680 MBEDTLS_SSL_PRINT_EXTS(
Gilles Peskine449bd832023-01-11 14:50:10 +01002681 3, MBEDTLS_SSL_HS_CERTIFICATE_REQUEST, ssl->handshake->sent_extensions);
Jerry Yu4b8f2f72022-10-31 13:31:22 +08002682
Gilles Peskine449bd832023-01-11 14:50:10 +01002683 return 0;
XiaokangQiancec9ae62022-05-06 07:28:50 +00002684}
2685
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002686MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002687static int ssl_tls13_write_certificate_request(mbedtls_ssl_context *ssl)
XiaokangQiancec9ae62022-05-06 07:28:50 +00002688{
2689 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
2690
Gilles Peskine449bd832023-01-11 14:50:10 +01002691 MBEDTLS_SSL_DEBUG_MSG(2, ("=> write certificate request"));
XiaokangQiancec9ae62022-05-06 07:28:50 +00002692
Gilles Peskine449bd832023-01-11 14:50:10 +01002693 MBEDTLS_SSL_PROC_CHK_NEG(ssl_tls13_certificate_request_coordinate(ssl));
XiaokangQiancec9ae62022-05-06 07:28:50 +00002694
Gilles Peskine449bd832023-01-11 14:50:10 +01002695 if (ret == SSL_CERTIFICATE_REQUEST_SEND_REQUEST) {
XiaokangQiancec9ae62022-05-06 07:28:50 +00002696 unsigned char *buf;
2697 size_t buf_len, msg_len;
2698
Xiaokang Qian9f1747b2023-03-30 02:50:04 +00002699 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_start_handshake_msg(
2700 ssl, MBEDTLS_SSL_HS_CERTIFICATE_REQUEST,
2701 &buf, &buf_len));
XiaokangQiancec9ae62022-05-06 07:28:50 +00002702
Gilles Peskine449bd832023-01-11 14:50:10 +01002703 MBEDTLS_SSL_PROC_CHK(ssl_tls13_write_certificate_request_body(
2704 ssl, buf, buf + buf_len, &msg_len));
XiaokangQiancec9ae62022-05-06 07:28:50 +00002705
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +01002706 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_add_hs_msg_to_checksum(
Xiaokang Qian9f1747b2023-03-30 02:50:04 +00002707 ssl, MBEDTLS_SSL_HS_CERTIFICATE_REQUEST,
2708 buf, msg_len));
XiaokangQiancec9ae62022-05-06 07:28:50 +00002709
Gilles Peskine449bd832023-01-11 14:50:10 +01002710 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_finish_handshake_msg(
2711 ssl, buf_len, msg_len));
2712 } else if (ret == SSL_CERTIFICATE_REQUEST_SKIP) {
2713 MBEDTLS_SSL_DEBUG_MSG(2, ("<= skip write certificate request"));
XiaokangQiancec9ae62022-05-06 07:28:50 +00002714 ret = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01002715 } else {
2716 MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
XiaokangQiancec9ae62022-05-06 07:28:50 +00002717 ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
2718 goto cleanup;
2719 }
2720
Gilles Peskine449bd832023-01-11 14:50:10 +01002721 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_SERVER_CERTIFICATE);
XiaokangQiancec9ae62022-05-06 07:28:50 +00002722cleanup:
2723
Gilles Peskine449bd832023-01-11 14:50:10 +01002724 MBEDTLS_SSL_DEBUG_MSG(2, ("<= write certificate request"));
2725 return ret;
XiaokangQiancec9ae62022-05-06 07:28:50 +00002726}
XiaokangQiancec9ae62022-05-06 07:28:50 +00002727
Jerry Yu4d3841a2022-04-16 12:37:19 +08002728/*
Jerry Yuc6e6dbf2022-04-16 19:42:57 +08002729 * Handler for MBEDTLS_SSL_SERVER_CERTIFICATE
Jerry Yu83da34e2022-04-16 13:59:52 +08002730 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002731MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002732static int ssl_tls13_write_server_certificate(mbedtls_ssl_context *ssl)
Jerry Yu83da34e2022-04-16 13:59:52 +08002733{
Jerry Yu5a26f302022-05-10 20:46:40 +08002734 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
XiaokangQianfb665a82022-06-15 03:57:21 +00002735
2736#if defined(MBEDTLS_X509_CRT_PARSE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01002737 if ((ssl_tls13_pick_key_cert(ssl) != 0) ||
2738 mbedtls_ssl_own_cert(ssl) == NULL) {
2739 MBEDTLS_SSL_DEBUG_MSG(2, ("No certificate available."));
2740 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE,
2741 MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE);
2742 return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
Jerry Yu5a26f302022-05-10 20:46:40 +08002743 }
XiaokangQianfb665a82022-06-15 03:57:21 +00002744#endif /* MBEDTLS_X509_CRT_PARSE_C */
Jerry Yu5a26f302022-05-10 20:46:40 +08002745
Gilles Peskine449bd832023-01-11 14:50:10 +01002746 ret = mbedtls_ssl_tls13_write_certificate(ssl);
2747 if (ret != 0) {
2748 return ret;
2749 }
2750 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_CERTIFICATE_VERIFY);
2751 return 0;
Jerry Yu83da34e2022-04-16 13:59:52 +08002752}
2753
2754/*
Jerry Yuc6e6dbf2022-04-16 19:42:57 +08002755 * Handler for MBEDTLS_SSL_CERTIFICATE_VERIFY
Jerry Yu83da34e2022-04-16 13:59:52 +08002756 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002757MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002758static int ssl_tls13_write_certificate_verify(mbedtls_ssl_context *ssl)
Jerry Yu83da34e2022-04-16 13:59:52 +08002759{
Gilles Peskine449bd832023-01-11 14:50:10 +01002760 int ret = mbedtls_ssl_tls13_write_certificate_verify(ssl);
2761 if (ret != 0) {
2762 return ret;
2763 }
2764 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_SERVER_FINISHED);
2765 return 0;
Jerry Yu83da34e2022-04-16 13:59:52 +08002766}
Ronald Cron928cbd32022-10-04 16:14:26 +02002767#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED */
Jerry Yu83da34e2022-04-16 13:59:52 +08002768
Jerry Yu9b72e392023-12-01 16:27:08 +08002769/*
2770 * RFC 8446 section A.2
2771 *
2772 * | Send ServerHello
2773 * | K_send = handshake
2774 * | Send EncryptedExtensions
2775 * | [Send CertificateRequest]
2776 * Can send | [Send Certificate + CertificateVerify]
2777 * app data | Send Finished
2778 * after --> | K_send = application
2779 * here +--------+--------+
2780 * No 0-RTT | | 0-RTT
2781 * | |
2782 * K_recv = handshake | | K_recv = early data
2783 * [Skip decrypt errors] | +------> WAIT_EOED -+
2784 * | | Recv | | Recv EndOfEarlyData
2785 * | | early data | | K_recv = handshake
2786 * | +------------+ |
2787 * | |
2788 * +> WAIT_FLIGHT2 <--------+
2789 * |
2790 * +--------+--------+
2791 * No auth | | Client auth
2792 * | |
2793 * | v
2794 * | WAIT_CERT
2795 * | Recv | | Recv Certificate
2796 * | empty | v
2797 * | Certificate | WAIT_CV
2798 * | | | Recv
2799 * | v | CertificateVerify
2800 * +-> WAIT_FINISHED <---+
2801 * | Recv Finished
2802 *
2803 *
2804 * The following function handles the state changes after WAIT_FLIGHT2 in the
Jerry Yu3be85072023-12-04 09:58:54 +08002805 * above diagram. We are not going to receive early data related messages
2806 * anymore, prepare to receive the first handshake message of the client
2807 * second flight.
Jerry Yu9b72e392023-12-01 16:27:08 +08002808 */
Jerry Yu3be85072023-12-04 09:58:54 +08002809static void ssl_tls13_prepare_for_handshake_second_flight(
2810 mbedtls_ssl_context *ssl)
Jerry Yu9b72e392023-12-01 16:27:08 +08002811{
Jerry Yu9b72e392023-12-01 16:27:08 +08002812 if (ssl->handshake->certificate_request_sent) {
2813 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_CLIENT_CERTIFICATE);
2814 } else {
Jerry Yu42020fb2023-12-05 17:35:53 +08002815 MBEDTLS_SSL_DEBUG_MSG(2, ("skip parse certificate"));
Jerry Yuebb1b1d2023-12-05 11:02:15 +08002816 MBEDTLS_SSL_DEBUG_MSG(2, ("skip parse certificate verify"));
Jerry Yuebb1b1d2023-12-05 11:02:15 +08002817
Jerry Yu9b72e392023-12-01 16:27:08 +08002818 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_CLIENT_FINISHED);
2819 }
Jerry Yu9b72e392023-12-01 16:27:08 +08002820}
2821
Jerry Yu83da34e2022-04-16 13:59:52 +08002822/*
Jerry Yud6e253d2022-05-18 13:59:24 +08002823 * Handler for MBEDTLS_SSL_SERVER_FINISHED
Jerry Yu69dd8d42022-04-16 12:51:26 +08002824 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002825MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002826static int ssl_tls13_write_server_finished(mbedtls_ssl_context *ssl)
Jerry Yu69dd8d42022-04-16 12:51:26 +08002827{
Jerry Yud6e253d2022-05-18 13:59:24 +08002828 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yu27bdc7c2022-04-16 13:33:27 +08002829
Gilles Peskine449bd832023-01-11 14:50:10 +01002830 ret = mbedtls_ssl_tls13_write_finished_message(ssl);
2831 if (ret != 0) {
2832 return ret;
2833 }
Jerry Yu27bdc7c2022-04-16 13:33:27 +08002834
Gilles Peskine449bd832023-01-11 14:50:10 +01002835 ret = mbedtls_ssl_tls13_compute_application_transform(ssl);
2836 if (ret != 0) {
Jerry Yue3d67cb2022-05-19 15:33:10 +08002837 MBEDTLS_SSL_PEND_FATAL_ALERT(
Gilles Peskine449bd832023-01-11 14:50:10 +01002838 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE,
2839 MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE);
2840 return ret;
Jerry Yue3d67cb2022-05-19 15:33:10 +08002841 }
XiaokangQianc3017f62022-05-13 05:55:41 +00002842
Jerry Yu87b5ed42022-12-12 13:07:07 +08002843#if defined(MBEDTLS_SSL_EARLY_DATA)
2844 if (ssl->early_data_status == MBEDTLS_SSL_EARLY_DATA_STATUS_ACCEPTED) {
Jerry Yu0af63dc2023-12-01 17:14:51 +08002845 /* See RFC 8446 section A.2 for more information */
Jerry Yu87b5ed42022-12-12 13:07:07 +08002846 MBEDTLS_SSL_DEBUG_MSG(
2847 1, ("Switch to early keys for inbound traffic. "
2848 "( K_recv = early data )"));
2849 mbedtls_ssl_set_inbound_transform(
2850 ssl, ssl->handshake->transform_earlydata);
2851 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_END_OF_EARLY_DATA);
2852 return 0;
2853 }
2854#endif /* MBEDTLS_SSL_EARLY_DATA */
Jerry Yu0af63dc2023-12-01 17:14:51 +08002855 MBEDTLS_SSL_DEBUG_MSG(
2856 1, ("Switch to handshake keys for inbound traffic "
2857 "( K_recv = handshake )"));
Gilles Peskine449bd832023-01-11 14:50:10 +01002858 mbedtls_ssl_set_inbound_transform(ssl, ssl->handshake->transform_handshake);
XiaokangQian189ded22022-05-10 08:12:17 +00002859
Jerry Yu3be85072023-12-04 09:58:54 +08002860 ssl_tls13_prepare_for_handshake_second_flight(ssl);
Jerry Yu7d8c3fe2022-12-12 12:59:44 +08002861
2862 return 0;
2863}
2864
Jerry Yu87b5ed42022-12-12 13:07:07 +08002865#if defined(MBEDTLS_SSL_EARLY_DATA)
2866/*
Jerry Yu59d420f2023-12-01 16:30:34 +08002867 * Handler for MBEDTLS_SSL_END_OF_EARLY_DATA
Jerry Yu87b5ed42022-12-12 13:07:07 +08002868 */
Jerry Yu3be85072023-12-04 09:58:54 +08002869#define SSL_GOT_END_OF_EARLY_DATA 0
Jerry Yub55f9eb2023-12-05 10:27:17 +08002870#define SSL_GOT_EARLY_DATA 1
Jerry Yud5c34962023-12-01 16:32:31 +08002871/* Coordination:
Jerry Yu3be85072023-12-04 09:58:54 +08002872 * Deals with the ambiguity of not knowing if the next message is an
2873 * EndOfEarlyData message or an application message containing early data.
Jerry Yud5c34962023-12-01 16:32:31 +08002874 * Returns a negative code on failure, or
Jerry Yu3be85072023-12-04 09:58:54 +08002875 * - SSL_GOT_END_OF_EARLY_DATA
Jerry Yub55f9eb2023-12-05 10:27:17 +08002876 * - SSL_GOT_EARLY_DATA
Jerry Yud5c34962023-12-01 16:32:31 +08002877 * indicating which message is received.
2878 */
2879MBEDTLS_CHECK_RETURN_CRITICAL
2880static int ssl_tls13_end_of_early_data_coordinate(mbedtls_ssl_context *ssl)
2881{
2882 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yub4ed4602023-12-01 16:34:00 +08002883
2884 if ((ret = mbedtls_ssl_read_record(ssl, 0)) != 0) {
2885 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_read_record", ret);
2886 return ret;
2887 }
2888 ssl->keep_current_message = 1;
2889
2890 if (ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
2891 ssl->in_msg[0] == MBEDTLS_SSL_HS_END_OF_EARLY_DATA) {
Jerry Yub55f9eb2023-12-05 10:27:17 +08002892 MBEDTLS_SSL_DEBUG_MSG(3, ("Received an end_of_early_data message."));
Jerry Yu3be85072023-12-04 09:58:54 +08002893 return SSL_GOT_END_OF_EARLY_DATA;
Jerry Yub4ed4602023-12-01 16:34:00 +08002894 }
2895
2896 if (ssl->in_msgtype == MBEDTLS_SSL_MSG_APPLICATION_DATA) {
Jerry Yub55f9eb2023-12-05 10:27:17 +08002897 MBEDTLS_SSL_DEBUG_MSG(3, ("Received early data"));
2898 return SSL_GOT_EARLY_DATA;
Jerry Yub4ed4602023-12-01 16:34:00 +08002899 }
2900
Jerry Yu7bb40a32023-12-04 10:04:15 +08002901 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE,
2902 MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE);
Jerry Yub4ed4602023-12-01 16:34:00 +08002903 return MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE;
Jerry Yud5c34962023-12-01 16:32:31 +08002904}
2905
2906MBEDTLS_CHECK_RETURN_CRITICAL
2907static int ssl_tls13_parse_end_of_early_data(mbedtls_ssl_context *ssl,
2908 const unsigned char *buf,
2909 const unsigned char *end)
2910{
Jerry Yu75c9ab72023-12-01 16:41:40 +08002911 /* RFC 8446 section 4.5
2912 *
2913 * struct {} EndOfEarlyData;
2914 */
Jerry Yufbf03992023-12-04 10:00:37 +08002915 if (buf != end) {
2916 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR,
2917 MBEDTLS_ERR_SSL_DECODE_ERROR);
2918 return MBEDTLS_ERR_SSL_DECODE_ERROR;
2919 }
2920 return 0;
Jerry Yud5c34962023-12-01 16:32:31 +08002921}
2922
2923MBEDTLS_CHECK_RETURN_CRITICAL
2924static int ssl_tls13_process_early_application_data(mbedtls_ssl_context *ssl)
2925{
2926 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yuee4d7292023-12-01 16:46:14 +08002927
2928 if ((ret = mbedtls_ssl_read_record(ssl, 0)) != 0) {
2929 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_read_record", ret);
2930 return ret;
2931 }
2932
Jerry Yuee4d7292023-12-01 16:46:14 +08002933 /*
2934 * Output early data
2935 *
2936 * For the time being, we print received data via debug message.
2937 *
2938 * TODO: Remove it when `mbedtls_ssl_read_early_data` is ready.
2939 */
2940 ssl->in_msg[ssl->in_msglen] = 0;
2941 MBEDTLS_SSL_DEBUG_MSG(3, ("\n%s", ssl->in_msg));
2942
2943 /* RFC 8446 section 4.6.1
2944 *
2945 * A server receiving more than max_early_data_size bytes of 0-RTT data
2946 * SHOULD terminate the connection with an "unexpected_message" alert.
2947 *
2948 * TODO: Add received data size check here.
2949 */
2950
2951 return 0;
Jerry Yud5c34962023-12-01 16:32:31 +08002952}
2953
2954/*
2955 * RFC 8446 section A.2
2956 *
2957 * | Send ServerHello
2958 * | K_send = handshake
2959 * | Send EncryptedExtensions
2960 * | [Send CertificateRequest]
2961 * Can send | [Send Certificate + CertificateVerify]
2962 * app data | Send Finished
2963 * after --> | K_send = application
2964 * here +--------+--------+
2965 * No 0-RTT | | 0-RTT
2966 * | |
2967 * K_recv = handshake | | K_recv = early data
2968 * [Skip decrypt errors] | +------> WAIT_EOED -+
2969 * | | Recv | | Recv EndOfEarlyData
2970 * | | early data | | K_recv = handshake
2971 * | +------------+ |
2972 * | |
2973 * +> WAIT_FLIGHT2 <--------+
2974 * |
2975 * +--------+--------+
2976 * No auth | | Client auth
2977 * | |
2978 * | v
2979 * | WAIT_CERT
2980 * | Recv | | Recv Certificate
2981 * | empty | v
2982 * | Certificate | WAIT_CV
2983 * | | | Recv
2984 * | v | CertificateVerify
2985 * +-> WAIT_FINISHED <---+
2986 * | Recv Finished
2987 *
2988 * The function handles actions and state changes from 0-RTT to WAIT_FLIGHT2 in
2989 * the above diagram.
2990 */
Jerry Yu87b5ed42022-12-12 13:07:07 +08002991MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yu59d420f2023-12-01 16:30:34 +08002992static int ssl_tls13_process_end_of_early_data(mbedtls_ssl_context *ssl)
Jerry Yu87b5ed42022-12-12 13:07:07 +08002993{
2994 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yud5c34962023-12-01 16:32:31 +08002995
2996 MBEDTLS_SSL_DEBUG_MSG(2, ("=> ssl_tls13_process_end_of_early_data"));
2997
2998 MBEDTLS_SSL_PROC_CHK_NEG(ssl_tls13_end_of_early_data_coordinate(ssl));
2999
Jerry Yu3be85072023-12-04 09:58:54 +08003000 if (ret == SSL_GOT_END_OF_EARLY_DATA) {
Jerry Yud5c34962023-12-01 16:32:31 +08003001 unsigned char *buf;
3002 size_t buf_len;
3003
3004 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_tls13_fetch_handshake_msg(
3005 ssl, MBEDTLS_SSL_HS_END_OF_EARLY_DATA,
3006 &buf, &buf_len));
3007
3008 MBEDTLS_SSL_PROC_CHK(ssl_tls13_parse_end_of_early_data(
3009 ssl, buf, buf + buf_len));
3010
Jerry Yue9655122023-12-01 16:44:40 +08003011 MBEDTLS_SSL_DEBUG_MSG(
3012 1, ("Switch to handshake keys for inbound traffic"
3013 "( K_recv = handshake )"));
3014 mbedtls_ssl_set_inbound_transform(
3015 ssl, ssl->handshake->transform_handshake);
3016
Jerry Yud5c34962023-12-01 16:32:31 +08003017 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_add_hs_msg_to_checksum(
3018 ssl, MBEDTLS_SSL_HS_END_OF_EARLY_DATA,
3019 buf, buf_len));
Jerry Yue9655122023-12-01 16:44:40 +08003020
Jerry Yu3be85072023-12-04 09:58:54 +08003021 ssl_tls13_prepare_for_handshake_second_flight(ssl);
Jerry Yud5c34962023-12-01 16:32:31 +08003022
Jerry Yub55f9eb2023-12-05 10:27:17 +08003023 } else if (ret == SSL_GOT_EARLY_DATA) {
Jerry Yud5c34962023-12-01 16:32:31 +08003024 MBEDTLS_SSL_PROC_CHK(ssl_tls13_process_early_application_data(ssl));
3025 } else {
3026 MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
3027 ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
3028 goto cleanup;
3029 }
3030
Jerry Yud5c34962023-12-01 16:32:31 +08003031cleanup:
3032 MBEDTLS_SSL_DEBUG_MSG(2, ("<= ssl_tls13_process_end_of_early_data"));
Jerry Yu87b5ed42022-12-12 13:07:07 +08003033 return ret;
3034}
3035#endif /* MBEDTLS_SSL_EARLY_DATA */
3036
Jerry Yu69dd8d42022-04-16 12:51:26 +08003037/*
Jerry Yud6e253d2022-05-18 13:59:24 +08003038 * Handler for MBEDTLS_SSL_CLIENT_FINISHED
Jerry Yu69dd8d42022-04-16 12:51:26 +08003039 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02003040MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01003041static int ssl_tls13_process_client_finished(mbedtls_ssl_context *ssl)
Jerry Yu69dd8d42022-04-16 12:51:26 +08003042{
Jerry Yud6e253d2022-05-18 13:59:24 +08003043 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
3044
Gilles Peskine449bd832023-01-11 14:50:10 +01003045 ret = mbedtls_ssl_tls13_process_finished_message(ssl);
3046 if (ret != 0) {
3047 return ret;
Jerry Yue3d67cb2022-05-19 15:33:10 +08003048 }
3049
Gilles Peskine449bd832023-01-11 14:50:10 +01003050 ret = mbedtls_ssl_tls13_compute_resumption_master_secret(ssl);
3051 if (ret != 0) {
Xiaokang Qian9f1747b2023-03-30 02:50:04 +00003052 MBEDTLS_SSL_DEBUG_RET(
3053 1, "mbedtls_ssl_tls13_compute_resumption_master_secret", ret);
Gilles Peskine449bd832023-01-11 14:50:10 +01003054 }
3055
3056 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_HANDSHAKE_WRAPUP);
3057 return 0;
Jerry Yu69dd8d42022-04-16 12:51:26 +08003058}
3059
3060/*
Jerry Yud6e253d2022-05-18 13:59:24 +08003061 * Handler for MBEDTLS_SSL_HANDSHAKE_WRAPUP
Jerry Yu69dd8d42022-04-16 12:51:26 +08003062 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02003063MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01003064static int ssl_tls13_handshake_wrapup(mbedtls_ssl_context *ssl)
Jerry Yu69dd8d42022-04-16 12:51:26 +08003065{
Gilles Peskine449bd832023-01-11 14:50:10 +01003066 MBEDTLS_SSL_DEBUG_MSG(2, ("handshake: done"));
Jerry Yu03ed50b2022-04-16 17:13:30 +08003067
Gilles Peskine449bd832023-01-11 14:50:10 +01003068 mbedtls_ssl_tls13_handshake_wrapup(ssl);
Jerry Yue67bef42022-07-07 07:29:42 +00003069
Pengyu Lv3643fdb2023-01-12 16:46:28 +08003070#if defined(MBEDTLS_SSL_SESSION_TICKETS) && \
3071 defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED)
Pengyu Lva1aa31b2022-12-13 13:49:59 +08003072/* TODO: Remove the check of SOME_PSK_ENABLED since SESSION_TICKETS requires
3073 * SOME_PSK_ENABLED to be enabled. Here is just to make CI happy. It is
3074 * expected to be resolved with issue#6395.
3075 */
Pengyu Lvc7af2c42022-12-01 16:33:00 +08003076 /* Sent NewSessionTicket message only when client supports PSK */
Pengyu Lv3643fdb2023-01-12 16:46:28 +08003077 if (mbedtls_ssl_tls13_some_psk_enabled(ssl)) {
Xiaokang Qian9f1747b2023-03-30 02:50:04 +00003078 mbedtls_ssl_handshake_set_state(
3079 ssl, MBEDTLS_SSL_TLS1_3_NEW_SESSION_TICKET);
Pengyu Lvc7af2c42022-12-01 16:33:00 +08003080 } else
Jerry Yufca4d572022-07-21 10:37:48 +08003081#endif
Pengyu Lv3643fdb2023-01-12 16:46:28 +08003082 {
3083 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_HANDSHAKE_OVER);
3084 }
Gilles Peskine449bd832023-01-11 14:50:10 +01003085 return 0;
Jerry Yu69dd8d42022-04-16 12:51:26 +08003086}
3087
3088/*
Jerry Yua8d3c502022-10-30 14:51:23 +08003089 * Handler for MBEDTLS_SSL_TLS1_3_NEW_SESSION_TICKET
Jerry Yue67bef42022-07-07 07:29:42 +00003090 */
3091#define SSL_NEW_SESSION_TICKET_SKIP 0
3092#define SSL_NEW_SESSION_TICKET_WRITE 1
3093MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01003094static int ssl_tls13_write_new_session_ticket_coordinate(mbedtls_ssl_context *ssl)
Jerry Yue67bef42022-07-07 07:29:42 +00003095{
3096 /* Check whether the use of session tickets is enabled */
Gilles Peskine449bd832023-01-11 14:50:10 +01003097 if (ssl->conf->f_ticket_write == NULL) {
3098 MBEDTLS_SSL_DEBUG_MSG(2, ("NewSessionTicket: disabled,"
3099 " callback is not set"));
3100 return SSL_NEW_SESSION_TICKET_SKIP;
Jerry Yud0766ec2022-09-22 10:46:57 +08003101 }
Gilles Peskine449bd832023-01-11 14:50:10 +01003102 if (ssl->conf->new_session_tickets_count == 0) {
3103 MBEDTLS_SSL_DEBUG_MSG(2, ("NewSessionTicket: disabled,"
3104 " configured count is zero"));
3105 return SSL_NEW_SESSION_TICKET_SKIP;
Jerry Yud0766ec2022-09-22 10:46:57 +08003106 }
3107
Gilles Peskine449bd832023-01-11 14:50:10 +01003108 if (ssl->handshake->new_session_tickets_count == 0) {
3109 MBEDTLS_SSL_DEBUG_MSG(2, ("NewSessionTicket: all tickets have "
3110 "been sent."));
3111 return SSL_NEW_SESSION_TICKET_SKIP;
Jerry Yue67bef42022-07-07 07:29:42 +00003112 }
3113
Gilles Peskine449bd832023-01-11 14:50:10 +01003114 return SSL_NEW_SESSION_TICKET_WRITE;
Jerry Yue67bef42022-07-07 07:29:42 +00003115}
3116
3117#if defined(MBEDTLS_SSL_SESSION_TICKETS)
3118MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01003119static int ssl_tls13_prepare_new_session_ticket(mbedtls_ssl_context *ssl,
3120 unsigned char *ticket_nonce,
3121 size_t ticket_nonce_size)
Jerry Yue67bef42022-07-07 07:29:42 +00003122{
3123 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
3124 mbedtls_ssl_session *session = ssl->session;
3125 mbedtls_ssl_ciphersuite_t *ciphersuite_info;
3126 psa_algorithm_t psa_hash_alg;
3127 int hash_length;
3128
Gilles Peskine449bd832023-01-11 14:50:10 +01003129 MBEDTLS_SSL_DEBUG_MSG(2, ("=> prepare NewSessionTicket msg"));
Jerry Yue67bef42022-07-07 07:29:42 +00003130
3131#if defined(MBEDTLS_HAVE_TIME)
Jerry Yu25ba4d42023-11-10 14:12:20 +08003132 session->ticket_creation_time = mbedtls_ms_time();
Jerry Yue67bef42022-07-07 07:29:42 +00003133#endif
3134
Pengyu Lv9f926952022-11-17 15:22:33 +08003135 /* Set ticket_flags depends on the advertised psk key exchange mode */
Pengyu Lv80270b22023-01-12 11:54:04 +08003136 mbedtls_ssl_session_clear_ticket_flags(
Pengyu Lv9eacb442022-12-09 14:39:19 +08003137 session, MBEDTLS_SSL_TLS1_3_TICKET_FLAGS_MASK);
Pengyu Lve6487fe2022-12-06 09:30:29 +08003138#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED)
Pengyu Lv80270b22023-01-12 11:54:04 +08003139 mbedtls_ssl_session_set_ticket_flags(
Pengyu Lv9eacb442022-12-09 14:39:19 +08003140 session, ssl->handshake->tls13_kex_modes);
Pengyu Lve6487fe2022-12-06 09:30:29 +08003141#endif
Pengyu Lv4938a562023-01-16 11:28:49 +08003142 MBEDTLS_SSL_PRINT_TICKET_FLAGS(4, session->ticket_flags);
Pengyu Lv9f926952022-11-17 15:22:33 +08003143
Jerry Yue67bef42022-07-07 07:29:42 +00003144 /* Generate ticket_age_add */
Gilles Peskine449bd832023-01-11 14:50:10 +01003145 if ((ret = ssl->conf->f_rng(ssl->conf->p_rng,
3146 (unsigned char *) &session->ticket_age_add,
3147 sizeof(session->ticket_age_add)) != 0)) {
3148 MBEDTLS_SSL_DEBUG_RET(1, "generate_ticket_age_add", ret);
3149 return ret;
Jerry Yue67bef42022-07-07 07:29:42 +00003150 }
Gilles Peskine449bd832023-01-11 14:50:10 +01003151 MBEDTLS_SSL_DEBUG_MSG(3, ("ticket_age_add: %u",
3152 (unsigned int) session->ticket_age_add));
Jerry Yue67bef42022-07-07 07:29:42 +00003153
3154 /* Generate ticket_nonce */
Gilles Peskine449bd832023-01-11 14:50:10 +01003155 ret = ssl->conf->f_rng(ssl->conf->p_rng, ticket_nonce, ticket_nonce_size);
3156 if (ret != 0) {
3157 MBEDTLS_SSL_DEBUG_RET(1, "generate_ticket_nonce", ret);
3158 return ret;
Jerry Yue67bef42022-07-07 07:29:42 +00003159 }
Gilles Peskine449bd832023-01-11 14:50:10 +01003160 MBEDTLS_SSL_DEBUG_BUF(3, "ticket_nonce:",
3161 ticket_nonce, ticket_nonce_size);
Jerry Yue67bef42022-07-07 07:29:42 +00003162
3163 ciphersuite_info =
Gilles Peskine449bd832023-01-11 14:50:10 +01003164 (mbedtls_ssl_ciphersuite_t *) ssl->handshake->ciphersuite_info;
Dave Rodgman2eab4622023-10-05 13:30:37 +01003165 psa_hash_alg = mbedtls_md_psa_alg_from_type((mbedtls_md_type_t) ciphersuite_info->mac);
Gilles Peskine449bd832023-01-11 14:50:10 +01003166 hash_length = PSA_HASH_LENGTH(psa_hash_alg);
3167 if (hash_length == -1 ||
3168 (size_t) hash_length > sizeof(session->resumption_key)) {
3169 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Jerry Yufca4d572022-07-21 10:37:48 +08003170 }
3171
Jerry Yue67bef42022-07-07 07:29:42 +00003172 /* In this code the psk key length equals the length of the hash */
3173 session->resumption_key_len = hash_length;
3174 session->ciphersuite = ciphersuite_info->id;
3175
3176 /* Compute resumption key
3177 *
3178 * HKDF-Expand-Label( resumption_master_secret,
3179 * "resumption", ticket_nonce, Hash.length )
3180 */
3181 ret = mbedtls_ssl_tls13_hkdf_expand_label(
Gilles Peskine449bd832023-01-11 14:50:10 +01003182 psa_hash_alg,
3183 session->app_secrets.resumption_master_secret,
3184 hash_length,
3185 MBEDTLS_SSL_TLS1_3_LBL_WITH_LEN(resumption),
3186 ticket_nonce,
3187 ticket_nonce_size,
3188 session->resumption_key,
3189 hash_length);
Jerry Yue67bef42022-07-07 07:29:42 +00003190
Gilles Peskine449bd832023-01-11 14:50:10 +01003191 if (ret != 0) {
3192 MBEDTLS_SSL_DEBUG_RET(2,
3193 "Creating the ticket-resumed PSK failed",
3194 ret);
3195 return ret;
Jerry Yue67bef42022-07-07 07:29:42 +00003196 }
Gilles Peskine449bd832023-01-11 14:50:10 +01003197 MBEDTLS_SSL_DEBUG_BUF(3, "Ticket-resumed PSK",
3198 session->resumption_key,
3199 session->resumption_key_len);
Jerry Yue67bef42022-07-07 07:29:42 +00003200
Gilles Peskine449bd832023-01-11 14:50:10 +01003201 MBEDTLS_SSL_DEBUG_BUF(3, "resumption_master_secret",
3202 session->app_secrets.resumption_master_secret,
3203 hash_length);
Jerry Yue67bef42022-07-07 07:29:42 +00003204
Gilles Peskine449bd832023-01-11 14:50:10 +01003205 return 0;
Jerry Yue67bef42022-07-07 07:29:42 +00003206}
3207
Jerry Yu01da35e2022-12-12 15:09:22 +08003208#if defined(MBEDTLS_SSL_EARLY_DATA)
3209/* RFC 8446 section 4.2.10
3210 *
3211 * struct {
Jerry Yudb6fda72023-11-22 12:40:20 +08003212 * select (Handshake.msg_type) {
3213 * case new_session_ticket: uint32 max_early_data_size;
3214 * ...
3215 * };
Jerry Yu01da35e2022-12-12 15:09:22 +08003216 * } EarlyDataIndication;
3217 */
3218MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yu3db60df2023-11-21 16:39:10 +08003219static int ssl_tls13_write_nst_early_data_ext(mbedtls_ssl_context *ssl,
3220 unsigned char *buf,
3221 const unsigned char *end,
3222 size_t *out_len)
Jerry Yu01da35e2022-12-12 15:09:22 +08003223{
3224 unsigned char *p = buf;
3225 *out_len = 0;
3226
Jerry Yuc2b1bc42023-11-22 10:08:13 +08003227 if (!mbedtls_ssl_session_ticket_allow_early_data(ssl->session)) {
Jerry Yu01da35e2022-12-12 15:09:22 +08003228 MBEDTLS_SSL_DEBUG_MSG(
Jerry Yuea96ac32023-11-21 17:06:36 +08003229 4, ("early_data not allowed, skip early_data extension in "
3230 "NewSessionTicket"));
Jerry Yu01da35e2022-12-12 15:09:22 +08003231 return 0;
3232 }
3233
3234 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 8);
3235
3236 MBEDTLS_PUT_UINT16_BE(MBEDTLS_TLS_EXT_EARLY_DATA, p, 0);
3237 MBEDTLS_PUT_UINT16_BE(4, p, 2);
Jerry Yud450fd22023-11-22 16:38:00 +08003238 MBEDTLS_PUT_UINT32_BE(ssl->session->max_early_data_size, p, 4);
Jerry Yu01da35e2022-12-12 15:09:22 +08003239 MBEDTLS_SSL_DEBUG_MSG(
3240 4, ("Sent max_early_data_size=%u",
Jerry Yud450fd22023-11-22 16:38:00 +08003241 (unsigned int) ssl->session->max_early_data_size));
Jerry Yu01da35e2022-12-12 15:09:22 +08003242
3243 *out_len = 8;
3244
3245 mbedtls_ssl_tls13_set_hs_sent_ext_mask(ssl, MBEDTLS_TLS_EXT_EARLY_DATA);
3246
3247 return 0;
3248}
3249#endif /* MBEDTLS_SSL_EARLY_DATA */
3250
Jerry Yue67bef42022-07-07 07:29:42 +00003251/* This function creates a NewSessionTicket message in the following format:
3252 *
3253 * struct {
3254 * uint32 ticket_lifetime;
3255 * uint32 ticket_age_add;
3256 * opaque ticket_nonce<0..255>;
3257 * opaque ticket<1..2^16-1>;
3258 * Extension extensions<0..2^16-2>;
3259 * } NewSessionTicket;
3260 *
3261 * The ticket inside the NewSessionTicket message is an encrypted container
3262 * carrying the necessary information so that the server is later able to
3263 * re-start the communication.
3264 *
3265 * The following fields are placed inside the ticket by the
3266 * f_ticket_write() function:
3267 *
3268 * - creation time (start)
3269 * - flags (flags)
3270 * - age add (ticket_age_add)
3271 * - key (key)
3272 * - key length (key_len)
3273 * - ciphersuite (ciphersuite)
3274 */
3275MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01003276static int ssl_tls13_write_new_session_ticket_body(mbedtls_ssl_context *ssl,
3277 unsigned char *buf,
3278 unsigned char *end,
3279 size_t *out_len,
3280 unsigned char *ticket_nonce,
3281 size_t ticket_nonce_size)
Jerry Yue67bef42022-07-07 07:29:42 +00003282{
3283 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
3284 unsigned char *p = buf;
3285 mbedtls_ssl_session *session = ssl->session;
3286 size_t ticket_len;
3287 uint32_t ticket_lifetime;
Jerry Yu01da35e2022-12-12 15:09:22 +08003288 unsigned char *p_extensions_len;
3289 size_t output_len;
3290
3291 ((void) output_len);
Jerry Yue67bef42022-07-07 07:29:42 +00003292
3293 *out_len = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01003294 MBEDTLS_SSL_DEBUG_MSG(2, ("=> write NewSessionTicket msg"));
Jerry Yue67bef42022-07-07 07:29:42 +00003295
Jerry Yu01da35e2022-12-12 15:09:22 +08003296#if defined(MBEDTLS_SSL_EARLY_DATA)
3297 if (ssl->conf->early_data_enabled == MBEDTLS_SSL_EARLY_DATA_ENABLED) {
Jerry Yu10795a02023-11-22 12:29:17 +08003298 mbedtls_ssl_session_set_ticket_flags(
3299 session, MBEDTLS_SSL_TLS1_3_TICKET_ALLOW_EARLY_DATA);
Jerry Yu01da35e2022-12-12 15:09:22 +08003300 }
3301#endif /* MBEDTLS_SSL_EARLY_DATA */
3302
Jerry Yue67bef42022-07-07 07:29:42 +00003303 /*
3304 * ticket_lifetime 4 bytes
3305 * ticket_age_add 4 bytes
3306 * ticket_nonce 1 + ticket_nonce_size bytes
3307 * ticket >=2 bytes
3308 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003309 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 4 + 4 + 1 + ticket_nonce_size + 2);
Jerry Yue67bef42022-07-07 07:29:42 +00003310
3311 /* Generate ticket and ticket_lifetime */
Gilles Peskine449bd832023-01-11 14:50:10 +01003312 ret = ssl->conf->f_ticket_write(ssl->conf->p_ticket,
3313 session,
3314 p + 9 + ticket_nonce_size + 2,
3315 end,
3316 &ticket_len,
3317 &ticket_lifetime);
3318 if (ret != 0) {
3319 MBEDTLS_SSL_DEBUG_RET(1, "write_ticket", ret);
3320 return ret;
Jerry Yue67bef42022-07-07 07:29:42 +00003321 }
3322 /* RFC 8446 4.6.1
3323 * ticket_lifetime: Indicates the lifetime in seconds as a 32-bit
3324 * unsigned integer in network byte order from the time of ticket
3325 * issuance. Servers MUST NOT use any value greater than
3326 * 604800 seconds (7 days). The value of zero indicates that the
3327 * ticket should be discarded immediately. Clients MUST NOT cache
3328 * tickets for longer than 7 days, regardless of the ticket_lifetime,
3329 * and MAY delete tickets earlier based on local policy. A server
3330 * MAY treat a ticket as valid for a shorter period of time than what
3331 * is stated in the ticket_lifetime.
3332 */
Jerry Yu8e0174a2023-11-10 13:58:16 +08003333 if (ticket_lifetime > MBEDTLS_SSL_TLS1_3_MAX_ALLOWED_TICKET_LIFETIME) {
3334 ticket_lifetime = MBEDTLS_SSL_TLS1_3_MAX_ALLOWED_TICKET_LIFETIME;
Gilles Peskine449bd832023-01-11 14:50:10 +01003335 }
3336 MBEDTLS_PUT_UINT32_BE(ticket_lifetime, p, 0);
3337 MBEDTLS_SSL_DEBUG_MSG(3, ("ticket_lifetime: %u",
3338 (unsigned int) ticket_lifetime));
Jerry Yue67bef42022-07-07 07:29:42 +00003339
3340 /* Write ticket_age_add */
Gilles Peskine449bd832023-01-11 14:50:10 +01003341 MBEDTLS_PUT_UINT32_BE(session->ticket_age_add, p, 4);
3342 MBEDTLS_SSL_DEBUG_MSG(3, ("ticket_age_add: %u",
3343 (unsigned int) session->ticket_age_add));
Jerry Yue67bef42022-07-07 07:29:42 +00003344
3345 /* Write ticket_nonce */
Gilles Peskine449bd832023-01-11 14:50:10 +01003346 p[8] = (unsigned char) ticket_nonce_size;
3347 if (ticket_nonce_size > 0) {
3348 memcpy(p + 9, ticket_nonce, ticket_nonce_size);
Jerry Yue67bef42022-07-07 07:29:42 +00003349 }
3350 p += 9 + ticket_nonce_size;
3351
3352 /* Write ticket */
Gilles Peskine449bd832023-01-11 14:50:10 +01003353 MBEDTLS_PUT_UINT16_BE(ticket_len, p, 0);
Jerry Yue67bef42022-07-07 07:29:42 +00003354 p += 2;
Gilles Peskine449bd832023-01-11 14:50:10 +01003355 MBEDTLS_SSL_DEBUG_BUF(4, "ticket", p, ticket_len);
Jerry Yue67bef42022-07-07 07:29:42 +00003356 p += ticket_len;
3357
3358 /* Ticket Extensions
3359 *
Jerry Yu01da35e2022-12-12 15:09:22 +08003360 * Extension extensions<0..2^16-2>;
Jerry Yue67bef42022-07-07 07:29:42 +00003361 */
Jerry Yuedab6372022-10-31 14:37:31 +08003362 ssl->handshake->sent_extensions = MBEDTLS_SSL_EXT_MASK_NONE;
3363
Gilles Peskine449bd832023-01-11 14:50:10 +01003364 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 2);
Jerry Yu01da35e2022-12-12 15:09:22 +08003365 p_extensions_len = p;
Jerry Yue67bef42022-07-07 07:29:42 +00003366 p += 2;
3367
Jerry Yu01da35e2022-12-12 15:09:22 +08003368#if defined(MBEDTLS_SSL_EARLY_DATA)
Jerry Yu3db60df2023-11-21 16:39:10 +08003369 if ((ret = ssl_tls13_write_nst_early_data_ext(
Jerry Yu01da35e2022-12-12 15:09:22 +08003370 ssl, p, end, &output_len)) != 0) {
Jerry Yu3db60df2023-11-21 16:39:10 +08003371 MBEDTLS_SSL_DEBUG_RET(1, "ssl_tls13_write_nst_early_data_ext", ret);
Jerry Yu01da35e2022-12-12 15:09:22 +08003372 return ret;
3373 }
3374 p += output_len;
3375#endif /* MBEDTLS_SSL_EARLY_DATA */
3376
3377 MBEDTLS_PUT_UINT16_BE(p - p_extensions_len - 2, p_extensions_len, 0);
3378
Jerry Yue67bef42022-07-07 07:29:42 +00003379 *out_len = p - buf;
Gilles Peskine449bd832023-01-11 14:50:10 +01003380 MBEDTLS_SSL_DEBUG_BUF(4, "ticket", buf, *out_len);
3381 MBEDTLS_SSL_DEBUG_MSG(2, ("<= write new session ticket"));
Jerry Yue67bef42022-07-07 07:29:42 +00003382
Jerry Yu7de2ff02022-11-08 21:43:46 +08003383 MBEDTLS_SSL_PRINT_EXTS(
Gilles Peskine449bd832023-01-11 14:50:10 +01003384 3, MBEDTLS_SSL_HS_NEW_SESSION_TICKET, ssl->handshake->sent_extensions);
Jerry Yu4b8f2f72022-10-31 13:31:22 +08003385
Gilles Peskine449bd832023-01-11 14:50:10 +01003386 return 0;
Jerry Yue67bef42022-07-07 07:29:42 +00003387}
3388
3389/*
Jerry Yua8d3c502022-10-30 14:51:23 +08003390 * Handler for MBEDTLS_SSL_TLS1_3_NEW_SESSION_TICKET
Jerry Yue67bef42022-07-07 07:29:42 +00003391 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003392static int ssl_tls13_write_new_session_ticket(mbedtls_ssl_context *ssl)
Jerry Yue67bef42022-07-07 07:29:42 +00003393{
3394 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
3395
Gilles Peskine449bd832023-01-11 14:50:10 +01003396 MBEDTLS_SSL_PROC_CHK_NEG(ssl_tls13_write_new_session_ticket_coordinate(ssl));
Jerry Yue67bef42022-07-07 07:29:42 +00003397
Gilles Peskine449bd832023-01-11 14:50:10 +01003398 if (ret == SSL_NEW_SESSION_TICKET_WRITE) {
Jerry Yue67bef42022-07-07 07:29:42 +00003399 unsigned char ticket_nonce[MBEDTLS_SSL_TLS1_3_TICKET_NONCE_LENGTH];
3400 unsigned char *buf;
3401 size_t buf_len, msg_len;
3402
Gilles Peskine449bd832023-01-11 14:50:10 +01003403 MBEDTLS_SSL_PROC_CHK(ssl_tls13_prepare_new_session_ticket(
3404 ssl, ticket_nonce, sizeof(ticket_nonce)));
Jerry Yue67bef42022-07-07 07:29:42 +00003405
Xiaokang Qian9f1747b2023-03-30 02:50:04 +00003406 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_start_handshake_msg(
3407 ssl, MBEDTLS_SSL_HS_NEW_SESSION_TICKET,
3408 &buf, &buf_len));
Jerry Yue67bef42022-07-07 07:29:42 +00003409
Gilles Peskine449bd832023-01-11 14:50:10 +01003410 MBEDTLS_SSL_PROC_CHK(ssl_tls13_write_new_session_ticket_body(
3411 ssl, buf, buf + buf_len, &msg_len,
3412 ticket_nonce, sizeof(ticket_nonce)));
Jerry Yue67bef42022-07-07 07:29:42 +00003413
Gilles Peskine449bd832023-01-11 14:50:10 +01003414 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_finish_handshake_msg(
3415 ssl, buf_len, msg_len));
Jerry Yufca4d572022-07-21 10:37:48 +08003416
Jerry Yu359e65f2022-09-22 23:47:43 +08003417 /* Limit session tickets count to one when resumption connection.
3418 *
3419 * See document of mbedtls_ssl_conf_new_session_tickets.
3420 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003421 if (ssl->handshake->resume == 1) {
Jerry Yu359e65f2022-09-22 23:47:43 +08003422 ssl->handshake->new_session_tickets_count = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01003423 } else {
Jerry Yu359e65f2022-09-22 23:47:43 +08003424 ssl->handshake->new_session_tickets_count--;
Gilles Peskine449bd832023-01-11 14:50:10 +01003425 }
Jerry Yub7e3fa72022-09-22 11:07:18 +08003426
Jerry Yua8d3c502022-10-30 14:51:23 +08003427 mbedtls_ssl_handshake_set_state(
Gilles Peskine449bd832023-01-11 14:50:10 +01003428 ssl, MBEDTLS_SSL_TLS1_3_NEW_SESSION_TICKET_FLUSH);
3429 } else {
3430 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_HANDSHAKE_OVER);
Jerry Yue67bef42022-07-07 07:29:42 +00003431 }
3432
Jerry Yue67bef42022-07-07 07:29:42 +00003433cleanup:
3434
Gilles Peskine449bd832023-01-11 14:50:10 +01003435 return ret;
Jerry Yue67bef42022-07-07 07:29:42 +00003436}
3437#endif /* MBEDTLS_SSL_SESSION_TICKETS */
3438
3439/*
XiaokangQiane8ff3502022-04-22 02:34:40 +00003440 * TLS 1.3 State Machine -- server side
XiaokangQian7807f9f2022-02-15 10:04:37 +00003441 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003442int mbedtls_ssl_tls13_handshake_server_step(mbedtls_ssl_context *ssl)
Jerry Yub9930e72021-08-06 17:11:51 +08003443{
XiaokangQian08037552022-04-20 07:16:41 +00003444 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
XiaokangQian7807f9f2022-02-15 10:04:37 +00003445
Gilles Peskine449bd832023-01-11 14:50:10 +01003446 if (ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER || ssl->handshake == NULL) {
3447 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
3448 }
XiaokangQian7807f9f2022-02-15 10:04:37 +00003449
Gilles Peskine449bd832023-01-11 14:50:10 +01003450 MBEDTLS_SSL_DEBUG_MSG(2, ("tls13 server state: %s(%d)",
Dave Rodgman2eab4622023-10-05 13:30:37 +01003451 mbedtls_ssl_states_str((mbedtls_ssl_states) ssl->state),
Gilles Peskine449bd832023-01-11 14:50:10 +01003452 ssl->state));
Jerry Yu6e81b272021-09-27 11:16:17 +08003453
Gilles Peskine449bd832023-01-11 14:50:10 +01003454 switch (ssl->state) {
XiaokangQian7807f9f2022-02-15 10:04:37 +00003455 /* start state */
3456 case MBEDTLS_SSL_HELLO_REQUEST:
Gilles Peskine449bd832023-01-11 14:50:10 +01003457 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_CLIENT_HELLO);
XiaokangQian08037552022-04-20 07:16:41 +00003458 ret = 0;
XiaokangQian7807f9f2022-02-15 10:04:37 +00003459 break;
3460
XiaokangQian7807f9f2022-02-15 10:04:37 +00003461 case MBEDTLS_SSL_CLIENT_HELLO:
Gilles Peskine449bd832023-01-11 14:50:10 +01003462 ret = ssl_tls13_process_client_hello(ssl);
3463 if (ret != 0) {
3464 MBEDTLS_SSL_DEBUG_RET(1, "ssl_tls13_process_client_hello", ret);
3465 }
Jerry Yuf41553b2022-05-09 22:20:30 +08003466 break;
XiaokangQian7807f9f2022-02-15 10:04:37 +00003467
Jerry Yuf41553b2022-05-09 22:20:30 +08003468 case MBEDTLS_SSL_HELLO_RETRY_REQUEST:
Gilles Peskine449bd832023-01-11 14:50:10 +01003469 ret = ssl_tls13_write_hello_retry_request(ssl);
3470 if (ret != 0) {
3471 MBEDTLS_SSL_DEBUG_RET(1, "ssl_tls13_write_hello_retry_request", ret);
3472 return ret;
Jerry Yuf41553b2022-05-09 22:20:30 +08003473 }
XiaokangQian7807f9f2022-02-15 10:04:37 +00003474 break;
3475
Jerry Yu5b64ae92022-03-30 17:15:02 +08003476 case MBEDTLS_SSL_SERVER_HELLO:
Gilles Peskine449bd832023-01-11 14:50:10 +01003477 ret = ssl_tls13_write_server_hello(ssl);
Jerry Yu5b64ae92022-03-30 17:15:02 +08003478 break;
3479
Xiaofei Baicba64af2022-02-15 10:00:56 +00003480 case MBEDTLS_SSL_ENCRYPTED_EXTENSIONS:
Gilles Peskine449bd832023-01-11 14:50:10 +01003481 ret = ssl_tls13_write_encrypted_extensions(ssl);
3482 if (ret != 0) {
3483 MBEDTLS_SSL_DEBUG_RET(1, "ssl_tls13_write_encrypted_extensions", ret);
3484 return ret;
Xiaofei Baicba64af2022-02-15 10:00:56 +00003485 }
Jerry Yu48330562022-05-06 21:35:44 +08003486 break;
Jerry Yu6a2cd9e2022-05-05 11:14:19 +08003487
Ronald Cron928cbd32022-10-04 16:14:26 +02003488#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED)
Xiaofei Bai9ca09d42022-02-14 12:57:18 +00003489 case MBEDTLS_SSL_CERTIFICATE_REQUEST:
Gilles Peskine449bd832023-01-11 14:50:10 +01003490 ret = ssl_tls13_write_certificate_request(ssl);
Xiaofei Bai9ca09d42022-02-14 12:57:18 +00003491 break;
Xiaofei Bai9ca09d42022-02-14 12:57:18 +00003492
Jerry Yu83da34e2022-04-16 13:59:52 +08003493 case MBEDTLS_SSL_SERVER_CERTIFICATE:
Gilles Peskine449bd832023-01-11 14:50:10 +01003494 ret = ssl_tls13_write_server_certificate(ssl);
Jerry Yu83da34e2022-04-16 13:59:52 +08003495 break;
3496
3497 case MBEDTLS_SSL_CERTIFICATE_VERIFY:
Gilles Peskine449bd832023-01-11 14:50:10 +01003498 ret = ssl_tls13_write_certificate_verify(ssl);
Jerry Yu83da34e2022-04-16 13:59:52 +08003499 break;
Ronald Cron928cbd32022-10-04 16:14:26 +02003500#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED */
Jerry Yu83da34e2022-04-16 13:59:52 +08003501
Gilles Peskine449bd832023-01-11 14:50:10 +01003502 /*
3503 * Injection of dummy-CCS's for middlebox compatibility
3504 */
Gabor Mezei7b39bf12022-05-24 16:04:14 +02003505#if defined(MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE)
Gabor Mezeif7044ea2022-06-28 16:01:49 +02003506 case MBEDTLS_SSL_SERVER_CCS_AFTER_HELLO_RETRY_REQUEST:
Gilles Peskine449bd832023-01-11 14:50:10 +01003507 ret = mbedtls_ssl_tls13_write_change_cipher_spec(ssl);
3508 if (ret == 0) {
3509 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_CLIENT_HELLO);
3510 }
Gabor Mezei7b39bf12022-05-24 16:04:14 +02003511 break;
3512
3513 case MBEDTLS_SSL_SERVER_CCS_AFTER_SERVER_HELLO:
Gilles Peskine449bd832023-01-11 14:50:10 +01003514 ret = mbedtls_ssl_tls13_write_change_cipher_spec(ssl);
3515 if (ret == 0) {
3516 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_ENCRYPTED_EXTENSIONS);
3517 }
Gabor Mezei7b39bf12022-05-24 16:04:14 +02003518 break;
3519#endif /* MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE */
3520
Jerry Yu69dd8d42022-04-16 12:51:26 +08003521 case MBEDTLS_SSL_SERVER_FINISHED:
Gilles Peskine449bd832023-01-11 14:50:10 +01003522 ret = ssl_tls13_write_server_finished(ssl);
Jerry Yu69dd8d42022-04-16 12:51:26 +08003523 break;
3524
Jerry Yu87b5ed42022-12-12 13:07:07 +08003525#if defined(MBEDTLS_SSL_EARLY_DATA)
3526 case MBEDTLS_SSL_END_OF_EARLY_DATA:
Jerry Yu59d420f2023-12-01 16:30:34 +08003527 ret = ssl_tls13_process_end_of_early_data(ssl);
Jerry Yu87b5ed42022-12-12 13:07:07 +08003528 break;
3529#endif /* MBEDTLS_SSL_EARLY_DATA */
3530
Jerry Yu69dd8d42022-04-16 12:51:26 +08003531 case MBEDTLS_SSL_CLIENT_FINISHED:
Gilles Peskine449bd832023-01-11 14:50:10 +01003532 ret = ssl_tls13_process_client_finished(ssl);
Jerry Yu69dd8d42022-04-16 12:51:26 +08003533 break;
3534
Jerry Yu69dd8d42022-04-16 12:51:26 +08003535 case MBEDTLS_SSL_HANDSHAKE_WRAPUP:
Gilles Peskine449bd832023-01-11 14:50:10 +01003536 ret = ssl_tls13_handshake_wrapup(ssl);
Jerry Yu69dd8d42022-04-16 12:51:26 +08003537 break;
3538
Ronald Cron766c0cd2022-10-18 12:17:11 +02003539#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED)
XiaokangQian6b916b12022-04-25 07:29:34 +00003540 case MBEDTLS_SSL_CLIENT_CERTIFICATE:
Gilles Peskine449bd832023-01-11 14:50:10 +01003541 ret = mbedtls_ssl_tls13_process_certificate(ssl);
3542 if (ret == 0) {
3543 if (ssl->session_negotiate->peer_cert != NULL) {
Ronald Cron209cae92022-06-07 10:30:19 +02003544 mbedtls_ssl_handshake_set_state(
Gilles Peskine449bd832023-01-11 14:50:10 +01003545 ssl, MBEDTLS_SSL_CLIENT_CERTIFICATE_VERIFY);
3546 } else {
3547 MBEDTLS_SSL_DEBUG_MSG(2, ("skip parse certificate verify"));
Ronald Cron209cae92022-06-07 10:30:19 +02003548 mbedtls_ssl_handshake_set_state(
Gilles Peskine449bd832023-01-11 14:50:10 +01003549 ssl, MBEDTLS_SSL_CLIENT_FINISHED);
Ronald Cron19385882022-06-15 16:26:13 +02003550 }
XiaokangQian6b916b12022-04-25 07:29:34 +00003551 }
3552 break;
3553
3554 case MBEDTLS_SSL_CLIENT_CERTIFICATE_VERIFY:
Gilles Peskine449bd832023-01-11 14:50:10 +01003555 ret = mbedtls_ssl_tls13_process_certificate_verify(ssl);
3556 if (ret == 0) {
XiaokangQian6b916b12022-04-25 07:29:34 +00003557 mbedtls_ssl_handshake_set_state(
Gilles Peskine449bd832023-01-11 14:50:10 +01003558 ssl, MBEDTLS_SSL_CLIENT_FINISHED);
XiaokangQian6b916b12022-04-25 07:29:34 +00003559 }
3560 break;
Ronald Cron766c0cd2022-10-18 12:17:11 +02003561#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED */
XiaokangQian6b916b12022-04-25 07:29:34 +00003562
Jerry Yue67bef42022-07-07 07:29:42 +00003563#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Jerry Yua8d3c502022-10-30 14:51:23 +08003564 case MBEDTLS_SSL_TLS1_3_NEW_SESSION_TICKET:
Gilles Peskine449bd832023-01-11 14:50:10 +01003565 ret = ssl_tls13_write_new_session_ticket(ssl);
3566 if (ret != 0) {
3567 MBEDTLS_SSL_DEBUG_RET(1,
3568 "ssl_tls13_write_new_session_ticket ",
3569 ret);
Jerry Yue67bef42022-07-07 07:29:42 +00003570 }
3571 break;
Jerry Yua8d3c502022-10-30 14:51:23 +08003572 case MBEDTLS_SSL_TLS1_3_NEW_SESSION_TICKET_FLUSH:
Jerry Yue67bef42022-07-07 07:29:42 +00003573 /* This state is necessary to do the flush of the New Session
Jerry Yua8d3c502022-10-30 14:51:23 +08003574 * Ticket message written in MBEDTLS_SSL_TLS1_3_NEW_SESSION_TICKET
Jerry Yue67bef42022-07-07 07:29:42 +00003575 * as part of ssl_prepare_handshake_step.
3576 */
3577 ret = 0;
Jerry Yud4e75002022-08-09 13:33:50 +08003578
Gilles Peskine449bd832023-01-11 14:50:10 +01003579 if (ssl->handshake->new_session_tickets_count == 0) {
3580 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_HANDSHAKE_OVER);
3581 } else {
Xiaokang Qian9f1747b2023-03-30 02:50:04 +00003582 mbedtls_ssl_handshake_set_state(
3583 ssl, MBEDTLS_SSL_TLS1_3_NEW_SESSION_TICKET);
Gilles Peskine449bd832023-01-11 14:50:10 +01003584 }
Jerry Yue67bef42022-07-07 07:29:42 +00003585 break;
3586
3587#endif /* MBEDTLS_SSL_SESSION_TICKETS */
3588
XiaokangQian7807f9f2022-02-15 10:04:37 +00003589 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01003590 MBEDTLS_SSL_DEBUG_MSG(1, ("invalid state %d", ssl->state));
3591 return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
XiaokangQian7807f9f2022-02-15 10:04:37 +00003592 }
3593
Gilles Peskine449bd832023-01-11 14:50:10 +01003594 return ret;
Jerry Yub9930e72021-08-06 17:11:51 +08003595}
Jerry Yu3cc4c2a2021-08-06 16:29:08 +08003596
Jerry Yufb4b6472022-01-27 15:03:26 +08003597#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_PROTO_TLS1_3 */