blob: e0cdf2323237e1ffe086ba0433f3901d0ce40ae6 [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
Jerry Yudb971632023-11-27 15:27:59 +0800250#if defined(MBEDTLS_SSL_EARLY_DATA)
251 MBEDTLS_SSL_DEBUG_MSG(2, ("ticket->max_early_data_size=%u",
252 (unsigned int) session->max_early_data_size));
253#endif
254
Jerry Yu8d4bbba2022-09-13 14:15:48 +0800255exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100256 if (ret != 0) {
257 mbedtls_ssl_session_free(session);
258 }
Jerry Yu95699e72022-08-21 19:22:23 +0800259
Gilles Peskine449bd832023-01-11 14:50:10 +0100260 MBEDTLS_SSL_DEBUG_MSG(2, ("<= check_identity_match_ticket"));
261 return ret;
Jerry Yu95699e72022-08-21 19:22:23 +0800262}
263#endif /* MBEDTLS_SSL_SESSION_TICKETS */
264
Jerry Yu6e74a7e2022-07-20 20:49:32 +0800265MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yu1c105562022-07-10 06:32:38 +0000266static int ssl_tls13_offered_psks_check_identity_match(
Gilles Peskine449bd832023-01-11 14:50:10 +0100267 mbedtls_ssl_context *ssl,
268 const unsigned char *identity,
269 size_t identity_len,
270 uint32_t obfuscated_ticket_age,
271 int *psk_type,
272 mbedtls_ssl_session *session)
Jerry Yu1c105562022-07-10 06:32:38 +0000273{
Ronald Cron606671e2023-02-17 11:36:33 +0100274 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
275
Jerry Yu95699e72022-08-21 19:22:23 +0800276 ((void) session);
277 ((void) obfuscated_ticket_age);
Jerry Yu5725f1c2022-08-21 17:27:16 +0800278 *psk_type = MBEDTLS_SSL_TLS1_3_PSK_EXTERNAL;
Jerry Yu95699e72022-08-21 19:22:23 +0800279
Gilles Peskine449bd832023-01-11 14:50:10 +0100280 MBEDTLS_SSL_DEBUG_BUF(4, "identity", identity, identity_len);
Jerry Yu95699e72022-08-21 19:22:23 +0800281 ssl->handshake->resume = 0;
282
Jerry Yu95699e72022-08-21 19:22:23 +0800283#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Gilles Peskine449bd832023-01-11 14:50:10 +0100284 if (ssl_tls13_offered_psks_check_identity_match_ticket(
Jerry Yu8d4bbba2022-09-13 14:15:48 +0800285 ssl, identity, identity_len, obfuscated_ticket_age,
Gilles Peskine449bd832023-01-11 14:50:10 +0100286 session) == SSL_TLS1_3_OFFERED_PSK_MATCH) {
Jerry Yu95699e72022-08-21 19:22:23 +0800287 ssl->handshake->resume = 1;
288 *psk_type = MBEDTLS_SSL_TLS1_3_PSK_RESUMPTION;
Ronald Cron606671e2023-02-17 11:36:33 +0100289 ret = mbedtls_ssl_set_hs_psk(ssl,
290 session->resumption_key,
291 session->resumption_key_len);
292 if (ret != 0) {
293 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_set_hs_psk", ret);
294 return ret;
295 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100296
297 MBEDTLS_SSL_DEBUG_BUF(4, "Ticket-resumed PSK:",
298 session->resumption_key,
299 session->resumption_key_len);
300 MBEDTLS_SSL_DEBUG_MSG(4, ("ticket: obfuscated_ticket_age: %u",
301 (unsigned) obfuscated_ticket_age));
302 return SSL_TLS1_3_OFFERED_PSK_MATCH;
Jerry Yu95699e72022-08-21 19:22:23 +0800303 }
304#endif /* MBEDTLS_SSL_SESSION_TICKETS */
305
Jerry Yu1c105562022-07-10 06:32:38 +0000306 /* Check identity with external configured function */
Gilles Peskine449bd832023-01-11 14:50:10 +0100307 if (ssl->conf->f_psk != NULL) {
308 if (ssl->conf->f_psk(
309 ssl->conf->p_psk, ssl, identity, identity_len) == 0) {
310 return SSL_TLS1_3_OFFERED_PSK_MATCH;
Jerry Yu1c105562022-07-10 06:32:38 +0000311 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100312 return SSL_TLS1_3_OFFERED_PSK_NOT_MATCH;
Jerry Yu1c105562022-07-10 06:32:38 +0000313 }
314
Gilles Peskine449bd832023-01-11 14:50:10 +0100315 MBEDTLS_SSL_DEBUG_BUF(5, "identity", identity, identity_len);
Jerry Yu1c105562022-07-10 06:32:38 +0000316 /* Check identity with pre-configured psk */
Gilles Peskine449bd832023-01-11 14:50:10 +0100317 if (ssl->conf->psk_identity != NULL &&
Jerry Yu2f0abc92022-07-22 19:34:48 +0800318 identity_len == ssl->conf->psk_identity_len &&
Gilles Peskine449bd832023-01-11 14:50:10 +0100319 mbedtls_ct_memcmp(ssl->conf->psk_identity,
320 identity, identity_len) == 0) {
Ronald Cron606671e2023-02-17 11:36:33 +0100321 ret = mbedtls_ssl_set_hs_psk(ssl, ssl->conf->psk, ssl->conf->psk_len);
322 if (ret != 0) {
323 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_set_hs_psk", ret);
324 return ret;
325 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100326 return SSL_TLS1_3_OFFERED_PSK_MATCH;
Jerry Yu1c105562022-07-10 06:32:38 +0000327 }
328
Gilles Peskine449bd832023-01-11 14:50:10 +0100329 return SSL_TLS1_3_OFFERED_PSK_NOT_MATCH;
Jerry Yu1c105562022-07-10 06:32:38 +0000330}
331
Jerry Yu6e74a7e2022-07-20 20:49:32 +0800332MBEDTLS_CHECK_RETURN_CRITICAL
Xiaokang Qian9f1747b2023-03-30 02:50:04 +0000333static int ssl_tls13_offered_psks_check_binder_match(
334 mbedtls_ssl_context *ssl,
335 const unsigned char *binder, size_t binder_len,
336 int psk_type, psa_algorithm_t psk_hash_alg)
Jerry Yu1c105562022-07-10 06:32:38 +0000337{
338 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yu96a2e362022-07-21 15:11:34 +0800339
Jerry Yudaf375a2022-07-20 21:31:43 +0800340 unsigned char transcript[PSA_HASH_MAX_SIZE];
Jerry Yu1c105562022-07-10 06:32:38 +0000341 size_t transcript_len;
Jerry Yu2f0abc92022-07-22 19:34:48 +0800342 unsigned char *psk;
Jerry Yu96a2e362022-07-21 15:11:34 +0800343 size_t psk_len;
Jerry Yudaf375a2022-07-20 21:31:43 +0800344 unsigned char server_computed_binder[PSA_HASH_MAX_SIZE];
Jerry Yu1c105562022-07-10 06:32:38 +0000345
Jerry Yu1c105562022-07-10 06:32:38 +0000346 /* Get current state of handshake transcript. */
Jerry Yu29d9faa2022-08-23 17:52:45 +0800347 ret = mbedtls_ssl_get_handshake_transcript(
Manuel Pégourié-Gonnard2d6d9932023-03-28 11:38:08 +0200348 ssl, mbedtls_md_type_from_psa_alg(psk_hash_alg),
Gilles Peskine449bd832023-01-11 14:50:10 +0100349 transcript, sizeof(transcript), &transcript_len);
350 if (ret != 0) {
351 return ret;
352 }
Jerry Yu1c105562022-07-10 06:32:38 +0000353
Gilles Peskine449bd832023-01-11 14:50:10 +0100354 ret = mbedtls_ssl_tls13_export_handshake_psk(ssl, &psk, &psk_len);
355 if (ret != 0) {
356 return ret;
357 }
Jerry Yu96a2e362022-07-21 15:11:34 +0800358
Gilles Peskine449bd832023-01-11 14:50:10 +0100359 ret = mbedtls_ssl_tls13_create_psk_binder(ssl, psk_hash_alg,
360 psk, psk_len, psk_type,
361 transcript,
362 server_computed_binder);
Jerry Yu96a2e362022-07-21 15:11:34 +0800363#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +0100364 mbedtls_free((void *) psk);
Jerry Yu96a2e362022-07-21 15:11:34 +0800365#endif
Gilles Peskine449bd832023-01-11 14:50:10 +0100366 if (ret != 0) {
367 MBEDTLS_SSL_DEBUG_MSG(1, ("PSK binder calculation failed."));
368 return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
Jerry Yu1c105562022-07-10 06:32:38 +0000369 }
370
Gilles Peskine449bd832023-01-11 14:50:10 +0100371 MBEDTLS_SSL_DEBUG_BUF(3, "psk binder ( computed ): ",
372 server_computed_binder, transcript_len);
373 MBEDTLS_SSL_DEBUG_BUF(3, "psk binder ( received ): ", binder, binder_len);
Jerry Yu1c105562022-07-10 06:32:38 +0000374
Gilles Peskine449bd832023-01-11 14:50:10 +0100375 if (mbedtls_ct_memcmp(server_computed_binder, binder, binder_len) == 0) {
376 return SSL_TLS1_3_OFFERED_PSK_MATCH;
Jerry Yu1c105562022-07-10 06:32:38 +0000377 }
378
Gilles Peskine449bd832023-01-11 14:50:10 +0100379 mbedtls_platform_zeroize(server_computed_binder,
380 sizeof(server_computed_binder));
381 return SSL_TLS1_3_OFFERED_PSK_NOT_MATCH;
Jerry Yu1c105562022-07-10 06:32:38 +0000382}
Jerry Yu96a2e362022-07-21 15:11:34 +0800383
Jerry Yu5725f1c2022-08-21 17:27:16 +0800384MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yuf35ba382022-08-23 17:58:26 +0800385static int ssl_tls13_select_ciphersuite_for_psk(
Gilles Peskine449bd832023-01-11 14:50:10 +0100386 mbedtls_ssl_context *ssl,
387 const unsigned char *cipher_suites,
388 const unsigned char *cipher_suites_end,
389 uint16_t *selected_ciphersuite,
390 const mbedtls_ssl_ciphersuite_t **selected_ciphersuite_info)
Jerry Yu5725f1c2022-08-21 17:27:16 +0800391{
Jerry Yuf35ba382022-08-23 17:58:26 +0800392 psa_algorithm_t psk_hash_alg = PSA_ALG_SHA_256;
Jerry Yu5725f1c2022-08-21 17:27:16 +0800393
Jerry Yu0baf9072022-08-25 11:21:04 +0800394 *selected_ciphersuite = 0;
395 *selected_ciphersuite_info = NULL;
396
Jerry Yuf35ba382022-08-23 17:58:26 +0800397 /* RFC 8446, page 55.
398 *
399 * For externally established PSKs, the Hash algorithm MUST be set when the
400 * PSK is established or default to SHA-256 if no such algorithm is defined.
401 *
402 */
Jerry Yu5725f1c2022-08-21 17:27:16 +0800403
Jerry Yu5725f1c2022-08-21 17:27:16 +0800404 /*
405 * Search for a matching ciphersuite
406 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100407 for (const unsigned char *p = cipher_suites;
408 p < cipher_suites_end; p += 2) {
Jerry Yu5725f1c2022-08-21 17:27:16 +0800409 uint16_t cipher_suite;
Jerry Yuf35ba382022-08-23 17:58:26 +0800410 const mbedtls_ssl_ciphersuite_t *ciphersuite_info;
Jerry Yu5725f1c2022-08-21 17:27:16 +0800411
Gilles Peskine449bd832023-01-11 14:50:10 +0100412 cipher_suite = MBEDTLS_GET_UINT16_BE(p, 0);
413 ciphersuite_info = ssl_tls13_validate_peer_ciphersuite(ssl,
414 cipher_suite);
415 if (ciphersuite_info == NULL) {
Jerry Yu5725f1c2022-08-21 17:27:16 +0800416 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +0100417 }
Jerry Yu5725f1c2022-08-21 17:27:16 +0800418
Jerry Yu5725f1c2022-08-21 17:27:16 +0800419 /* MAC of selected ciphersuite MUST be same with PSK binder if exist.
420 * Otherwise, client should reject.
421 */
Dave Rodgman2eab4622023-10-05 13:30:37 +0100422 if (psk_hash_alg ==
423 mbedtls_md_psa_alg_from_type((mbedtls_md_type_t) ciphersuite_info->mac)) {
Jerry Yuf35ba382022-08-23 17:58:26 +0800424 *selected_ciphersuite = cipher_suite;
425 *selected_ciphersuite_info = ciphersuite_info;
Gilles Peskine449bd832023-01-11 14:50:10 +0100426 return 0;
Jerry Yuf35ba382022-08-23 17:58:26 +0800427 }
428 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100429 MBEDTLS_SSL_DEBUG_MSG(2, ("No matched ciphersuite"));
430 return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
Jerry Yuf35ba382022-08-23 17:58:26 +0800431}
Jerry Yu5725f1c2022-08-21 17:27:16 +0800432
Jerry Yu82534862022-08-30 10:42:33 +0800433#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Jerry Yuf35ba382022-08-23 17:58:26 +0800434MBEDTLS_CHECK_RETURN_CRITICAL
435static int ssl_tls13_select_ciphersuite_for_resumption(
Gilles Peskine449bd832023-01-11 14:50:10 +0100436 mbedtls_ssl_context *ssl,
437 const unsigned char *cipher_suites,
438 const unsigned char *cipher_suites_end,
439 mbedtls_ssl_session *session,
440 uint16_t *selected_ciphersuite,
441 const mbedtls_ssl_ciphersuite_t **selected_ciphersuite_info)
Jerry Yuf35ba382022-08-23 17:58:26 +0800442{
Jerry Yu82534862022-08-30 10:42:33 +0800443
Jerry Yuf35ba382022-08-23 17:58:26 +0800444 *selected_ciphersuite = 0;
445 *selected_ciphersuite_info = NULL;
Gilles Peskine449bd832023-01-11 14:50:10 +0100446 for (const unsigned char *p = cipher_suites; p < cipher_suites_end; p += 2) {
447 uint16_t cipher_suite = MBEDTLS_GET_UINT16_BE(p, 0);
Jerry Yu82534862022-08-30 10:42:33 +0800448 const mbedtls_ssl_ciphersuite_t *ciphersuite_info;
449
Gilles Peskine449bd832023-01-11 14:50:10 +0100450 if (cipher_suite != session->ciphersuite) {
Jerry Yu82534862022-08-30 10:42:33 +0800451 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +0100452 }
Jerry Yu82534862022-08-30 10:42:33 +0800453
Gilles Peskine449bd832023-01-11 14:50:10 +0100454 ciphersuite_info = ssl_tls13_validate_peer_ciphersuite(ssl,
455 cipher_suite);
456 if (ciphersuite_info == NULL) {
Jerry Yu82534862022-08-30 10:42:33 +0800457 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +0100458 }
Jerry Yu82534862022-08-30 10:42:33 +0800459
Jerry Yu4746b102022-09-13 11:11:48 +0800460 *selected_ciphersuite = cipher_suite;
Jerry Yu82534862022-08-30 10:42:33 +0800461 *selected_ciphersuite_info = ciphersuite_info;
462
Gilles Peskine449bd832023-01-11 14:50:10 +0100463 return 0;
Jerry Yu82534862022-08-30 10:42:33 +0800464 }
465
Gilles Peskine449bd832023-01-11 14:50:10 +0100466 return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
Jerry Yu5725f1c2022-08-21 17:27:16 +0800467}
Jerry Yuf35ba382022-08-23 17:58:26 +0800468
Jerry Yu82534862022-08-30 10:42:33 +0800469MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100470static int ssl_tls13_session_copy_ticket(mbedtls_ssl_session *dst,
471 const mbedtls_ssl_session *src)
Jerry Yu82534862022-08-30 10:42:33 +0800472{
Jerry Yu82534862022-08-30 10:42:33 +0800473 dst->ticket_age_add = src->ticket_age_add;
474 dst->ticket_flags = src->ticket_flags;
475 dst->resumption_key_len = src->resumption_key_len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100476 if (src->resumption_key_len == 0) {
477 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
478 }
479 memcpy(dst->resumption_key, src->resumption_key, src->resumption_key_len);
Jerry Yu4746b102022-09-13 11:11:48 +0800480
Jerry Yu33bf2402022-12-12 16:01:43 +0800481#if defined(MBEDTLS_SSL_EARLY_DATA)
482 dst->max_early_data_size = src->max_early_data_size;
483#endif
484
Gilles Peskine449bd832023-01-11 14:50:10 +0100485 return 0;
Jerry Yu82534862022-08-30 10:42:33 +0800486}
487#endif /* MBEDTLS_SSL_SESSION_TICKETS */
488
Jerry Yu1c105562022-07-10 06:32:38 +0000489/* Parser for pre_shared_key extension in client hello
490 * struct {
491 * opaque identity<1..2^16-1>;
492 * uint32 obfuscated_ticket_age;
493 * } PskIdentity;
494 *
495 * opaque PskBinderEntry<32..255>;
496 *
497 * struct {
498 * PskIdentity identities<7..2^16-1>;
499 * PskBinderEntry binders<33..2^16-1>;
500 * } OfferedPsks;
501 *
502 * struct {
503 * select (Handshake.msg_type) {
504 * case client_hello: OfferedPsks;
505 * ....
506 * };
507 * } PreSharedKeyExtension;
508 */
Jerry Yu6e74a7e2022-07-20 20:49:32 +0800509MBEDTLS_CHECK_RETURN_CRITICAL
Xiaokang Qian9f1747b2023-03-30 02:50:04 +0000510static int ssl_tls13_parse_pre_shared_key_ext(
511 mbedtls_ssl_context *ssl,
512 const unsigned char *pre_shared_key_ext,
513 const unsigned char *pre_shared_key_ext_end,
514 const unsigned char *ciphersuites,
515 const unsigned char *ciphersuites_end)
Jerry Yu1c105562022-07-10 06:32:38 +0000516{
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +0100517 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yu29d9faa2022-08-23 17:52:45 +0800518 const unsigned char *identities = pre_shared_key_ext;
Jerry Yu568ec252022-07-22 21:27:34 +0800519 const unsigned char *p_identity_len;
520 size_t identities_len;
Jerry Yu1c105562022-07-10 06:32:38 +0000521 const unsigned char *identities_end;
Jerry Yu568ec252022-07-22 21:27:34 +0800522 const unsigned char *binders;
523 const unsigned char *p_binder_len;
524 size_t binders_len;
Jerry Yu1c105562022-07-10 06:32:38 +0000525 const unsigned char *binders_end;
Jerry Yu96a2e362022-07-21 15:11:34 +0800526 int matched_identity = -1;
527 int identity_id = -1;
Jerry Yu1c105562022-07-10 06:32:38 +0000528
Gilles Peskine449bd832023-01-11 14:50:10 +0100529 MBEDTLS_SSL_DEBUG_BUF(3, "pre_shared_key extension",
530 pre_shared_key_ext,
531 pre_shared_key_ext_end - pre_shared_key_ext);
Jerry Yu1c105562022-07-10 06:32:38 +0000532
Jerry Yu96a2e362022-07-21 15:11:34 +0800533 /* identities_len 2 bytes
534 * identities_data >= 7 bytes
535 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100536 MBEDTLS_SSL_CHK_BUF_READ_PTR(identities, pre_shared_key_ext_end, 7 + 2);
537 identities_len = MBEDTLS_GET_UINT16_BE(identities, 0);
Jerry Yu568ec252022-07-22 21:27:34 +0800538 p_identity_len = identities + 2;
Gilles Peskine449bd832023-01-11 14:50:10 +0100539 MBEDTLS_SSL_CHK_BUF_READ_PTR(p_identity_len, pre_shared_key_ext_end,
540 identities_len);
Jerry Yu568ec252022-07-22 21:27:34 +0800541 identities_end = p_identity_len + identities_len;
Jerry Yu1c105562022-07-10 06:32:38 +0000542
Jerry Yu96a2e362022-07-21 15:11:34 +0800543 /* binders_len 2 bytes
544 * binders >= 33 bytes
545 */
Jerry Yu568ec252022-07-22 21:27:34 +0800546 binders = identities_end;
Gilles Peskine449bd832023-01-11 14:50:10 +0100547 MBEDTLS_SSL_CHK_BUF_READ_PTR(binders, pre_shared_key_ext_end, 33 + 2);
548 binders_len = MBEDTLS_GET_UINT16_BE(binders, 0);
Jerry Yu568ec252022-07-22 21:27:34 +0800549 p_binder_len = binders + 2;
Gilles Peskine449bd832023-01-11 14:50:10 +0100550 MBEDTLS_SSL_CHK_BUF_READ_PTR(p_binder_len, pre_shared_key_ext_end, binders_len);
Jerry Yu568ec252022-07-22 21:27:34 +0800551 binders_end = p_binder_len + binders_len;
Jerry Yu1c105562022-07-10 06:32:38 +0000552
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +0100553 ret = ssl->handshake->update_checksum(ssl, pre_shared_key_ext,
554 identities_end - pre_shared_key_ext);
555 if (0 != ret) {
556 MBEDTLS_SSL_DEBUG_RET(1, ("update_checksum"), ret);
557 return ret;
558 }
Jerry Yu96a2e362022-07-21 15:11:34 +0800559
Gilles Peskine449bd832023-01-11 14:50:10 +0100560 while (p_identity_len < identities_end && p_binder_len < binders_end) {
Jerry Yu1c105562022-07-10 06:32:38 +0000561 const unsigned char *identity;
Jerry Yu568ec252022-07-22 21:27:34 +0800562 size_t identity_len;
Jerry Yu82534862022-08-30 10:42:33 +0800563 uint32_t obfuscated_ticket_age;
Jerry Yu96a2e362022-07-21 15:11:34 +0800564 const unsigned char *binder;
Jerry Yu568ec252022-07-22 21:27:34 +0800565 size_t binder_len;
Jerry Yu5725f1c2022-08-21 17:27:16 +0800566 int psk_type;
567 uint16_t cipher_suite;
Jerry Yu29d9faa2022-08-23 17:52:45 +0800568 const mbedtls_ssl_ciphersuite_t *ciphersuite_info;
Jerry Yu82534862022-08-30 10:42:33 +0800569#if defined(MBEDTLS_SSL_SESSION_TICKETS)
570 mbedtls_ssl_session session;
Gilles Peskine449bd832023-01-11 14:50:10 +0100571 mbedtls_ssl_session_init(&session);
Jerry Yu82534862022-08-30 10:42:33 +0800572#endif
Jerry Yubb852022022-07-20 21:10:44 +0800573
Gilles Peskine449bd832023-01-11 14:50:10 +0100574 MBEDTLS_SSL_CHK_BUF_READ_PTR(p_identity_len, identities_end, 2 + 1 + 4);
575 identity_len = MBEDTLS_GET_UINT16_BE(p_identity_len, 0);
Jerry Yu568ec252022-07-22 21:27:34 +0800576 identity = p_identity_len + 2;
Gilles Peskine449bd832023-01-11 14:50:10 +0100577 MBEDTLS_SSL_CHK_BUF_READ_PTR(identity, identities_end, identity_len + 4);
578 obfuscated_ticket_age = MBEDTLS_GET_UINT32_BE(identity, identity_len);
Jerry Yu568ec252022-07-22 21:27:34 +0800579 p_identity_len += identity_len + 6;
Jerry Yu1c105562022-07-10 06:32:38 +0000580
Gilles Peskine449bd832023-01-11 14:50:10 +0100581 MBEDTLS_SSL_CHK_BUF_READ_PTR(p_binder_len, binders_end, 1 + 32);
Jerry Yu568ec252022-07-22 21:27:34 +0800582 binder_len = *p_binder_len;
583 binder = p_binder_len + 1;
Gilles Peskine449bd832023-01-11 14:50:10 +0100584 MBEDTLS_SSL_CHK_BUF_READ_PTR(binder, binders_end, binder_len);
Jerry Yu568ec252022-07-22 21:27:34 +0800585 p_binder_len += binder_len + 1;
Jerry Yu96a2e362022-07-21 15:11:34 +0800586
Jerry Yu96a2e362022-07-21 15:11:34 +0800587 identity_id++;
Gilles Peskine449bd832023-01-11 14:50:10 +0100588 if (matched_identity != -1) {
Jerry Yu1c105562022-07-10 06:32:38 +0000589 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +0100590 }
Jerry Yu1c105562022-07-10 06:32:38 +0000591
Jerry Yu96a2e362022-07-21 15:11:34 +0800592 ret = ssl_tls13_offered_psks_check_identity_match(
Gilles Peskine449bd832023-01-11 14:50:10 +0100593 ssl, identity, identity_len, obfuscated_ticket_age,
594 &psk_type, &session);
595 if (ret != SSL_TLS1_3_OFFERED_PSK_MATCH) {
Jerry Yu96a2e362022-07-21 15:11:34 +0800596 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +0100597 }
Jerry Yu96a2e362022-07-21 15:11:34 +0800598
Gilles Peskine449bd832023-01-11 14:50:10 +0100599 MBEDTLS_SSL_DEBUG_MSG(4, ("found matched identity"));
600 switch (psk_type) {
Jerry Yu0baf9072022-08-25 11:21:04 +0800601 case MBEDTLS_SSL_TLS1_3_PSK_EXTERNAL:
602 ret = ssl_tls13_select_ciphersuite_for_psk(
Gilles Peskine449bd832023-01-11 14:50:10 +0100603 ssl, ciphersuites, ciphersuites_end,
604 &cipher_suite, &ciphersuite_info);
Jerry Yu0baf9072022-08-25 11:21:04 +0800605 break;
606 case MBEDTLS_SSL_TLS1_3_PSK_RESUMPTION:
Jerry Yu82534862022-08-30 10:42:33 +0800607#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Jerry Yu0baf9072022-08-25 11:21:04 +0800608 ret = ssl_tls13_select_ciphersuite_for_resumption(
Gilles Peskine449bd832023-01-11 14:50:10 +0100609 ssl, ciphersuites, ciphersuites_end, &session,
610 &cipher_suite, &ciphersuite_info);
611 if (ret != 0) {
612 mbedtls_ssl_session_free(&session);
613 }
Jerry Yu82534862022-08-30 10:42:33 +0800614#else
615 ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
616#endif
Jerry Yu0baf9072022-08-25 11:21:04 +0800617 break;
618 default:
Gilles Peskine449bd832023-01-11 14:50:10 +0100619 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Jerry Yu0baf9072022-08-25 11:21:04 +0800620 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100621 if (ret != 0) {
Jerry Yuf35ba382022-08-23 17:58:26 +0800622 /* See below, no cipher_suite available, abort handshake */
623 MBEDTLS_SSL_PEND_FATAL_ALERT(
624 MBEDTLS_SSL_ALERT_MSG_DECRYPT_ERROR,
Gilles Peskine449bd832023-01-11 14:50:10 +0100625 MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE);
Jerry Yuf35ba382022-08-23 17:58:26 +0800626 MBEDTLS_SSL_DEBUG_RET(
Gilles Peskine449bd832023-01-11 14:50:10 +0100627 2, "ssl_tls13_select_ciphersuite", ret);
628 return ret;
Jerry Yu5725f1c2022-08-21 17:27:16 +0800629 }
630
Jerry Yu96a2e362022-07-21 15:11:34 +0800631 ret = ssl_tls13_offered_psks_check_binder_match(
Gilles Peskine449bd832023-01-11 14:50:10 +0100632 ssl, binder, binder_len, psk_type,
Dave Rodgman2eab4622023-10-05 13:30:37 +0100633 mbedtls_md_psa_alg_from_type((mbedtls_md_type_t) ciphersuite_info->mac));
Gilles Peskine449bd832023-01-11 14:50:10 +0100634 if (ret != SSL_TLS1_3_OFFERED_PSK_MATCH) {
Jerry Yuc5a23a02022-08-25 10:51:44 +0800635 /* For security reasons, the handshake should be aborted when we
636 * fail to validate a binder value. See RFC 8446 section 4.2.11.2
637 * and appendix E.6. */
Jerry Yu82534862022-08-30 10:42:33 +0800638#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Gilles Peskine449bd832023-01-11 14:50:10 +0100639 mbedtls_ssl_session_free(&session);
Jerry Yu82534862022-08-30 10:42:33 +0800640#endif
Gilles Peskine449bd832023-01-11 14:50:10 +0100641 MBEDTLS_SSL_DEBUG_MSG(3, ("Invalid binder."));
Xiaokang Qian9f1747b2023-03-30 02:50:04 +0000642 MBEDTLS_SSL_DEBUG_RET(
643 1, "ssl_tls13_offered_psks_check_binder_match", ret);
Jerry Yu96a2e362022-07-21 15:11:34 +0800644 MBEDTLS_SSL_PEND_FATAL_ALERT(
645 MBEDTLS_SSL_ALERT_MSG_DECRYPT_ERROR,
Gilles Peskine449bd832023-01-11 14:50:10 +0100646 MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE);
647 return ret;
Jerry Yu96a2e362022-07-21 15:11:34 +0800648 }
Jerry Yu96a2e362022-07-21 15:11:34 +0800649
650 matched_identity = identity_id;
Jerry Yu5725f1c2022-08-21 17:27:16 +0800651
652 /* Update handshake parameters */
Jerry Yue5834fd2022-08-29 20:16:09 +0800653 ssl->handshake->ciphersuite_info = ciphersuite_info;
Jerry Yu4746b102022-09-13 11:11:48 +0800654 ssl->session_negotiate->ciphersuite = cipher_suite;
Gilles Peskine449bd832023-01-11 14:50:10 +0100655 MBEDTLS_SSL_DEBUG_MSG(2, ("overwrite ciphersuite: %04x - %s",
656 cipher_suite, ciphersuite_info->name));
Jerry Yu82534862022-08-30 10:42:33 +0800657#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Gilles Peskine449bd832023-01-11 14:50:10 +0100658 if (psk_type == MBEDTLS_SSL_TLS1_3_PSK_RESUMPTION) {
659 ret = ssl_tls13_session_copy_ticket(ssl->session_negotiate,
660 &session);
661 mbedtls_ssl_session_free(&session);
662 if (ret != 0) {
663 return ret;
664 }
Jerry Yu82534862022-08-30 10:42:33 +0800665 }
666#endif /* MBEDTLS_SSL_SESSION_TICKETS */
Jerry Yu1c105562022-07-10 06:32:38 +0000667 }
668
Gilles Peskine449bd832023-01-11 14:50:10 +0100669 if (p_identity_len != identities_end || p_binder_len != binders_end) {
670 MBEDTLS_SSL_DEBUG_MSG(3, ("pre_shared_key extension decode error"));
671 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR,
672 MBEDTLS_ERR_SSL_DECODE_ERROR);
673 return MBEDTLS_ERR_SSL_DECODE_ERROR;
Jerry Yu1c105562022-07-10 06:32:38 +0000674 }
675
Jerry Yu6f1db3f2022-07-22 23:05:59 +0800676 /* Update the handshake transcript with the binder list. */
Xiaokang Qian9f1747b2023-03-30 02:50:04 +0000677 ret = ssl->handshake->update_checksum(
678 ssl, identities_end, (size_t) (binders_end - identities_end));
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +0100679 if (0 != ret) {
680 MBEDTLS_SSL_DEBUG_RET(1, ("update_checksum"), ret);
681 return ret;
682 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100683 if (matched_identity == -1) {
684 MBEDTLS_SSL_DEBUG_MSG(3, ("No matched PSK or ticket."));
685 return MBEDTLS_ERR_SSL_UNKNOWN_IDENTITY;
Jerry Yu1c105562022-07-10 06:32:38 +0000686 }
687
Gilles Peskine449bd832023-01-11 14:50:10 +0100688 ssl->handshake->selected_identity = (uint16_t) matched_identity;
689 MBEDTLS_SSL_DEBUG_MSG(3, ("Pre shared key found"));
Jerry Yu1c105562022-07-10 06:32:38 +0000690
Gilles Peskine449bd832023-01-11 14:50:10 +0100691 return 0;
Jerry Yu1c105562022-07-10 06:32:38 +0000692}
Jerry Yu032b15ce2022-07-11 06:10:03 +0000693
694/*
695 * struct {
696 * select ( Handshake.msg_type ) {
697 * ....
698 * case server_hello:
699 * uint16 selected_identity;
700 * }
701 * } PreSharedKeyExtension;
702 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100703static int ssl_tls13_write_server_pre_shared_key_ext(mbedtls_ssl_context *ssl,
704 unsigned char *buf,
705 unsigned char *end,
706 size_t *olen)
Jerry Yu032b15ce2022-07-11 06:10:03 +0000707{
Gilles Peskine449bd832023-01-11 14:50:10 +0100708 unsigned char *p = (unsigned char *) buf;
Jerry Yu032b15ce2022-07-11 06:10:03 +0000709
710 *olen = 0;
711
David Horstmann21b89762022-10-06 18:34:28 +0100712 int not_using_psk = 0;
Jerry Yu032b15ce2022-07-11 06:10:03 +0000713#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +0100714 not_using_psk = (mbedtls_svc_key_id_is_null(ssl->handshake->psk_opaque));
Jerry Yu032b15ce2022-07-11 06:10:03 +0000715#else
Gilles Peskine449bd832023-01-11 14:50:10 +0100716 not_using_psk = (ssl->handshake->psk == NULL);
Jerry Yu032b15ce2022-07-11 06:10:03 +0000717#endif
Gilles Peskine449bd832023-01-11 14:50:10 +0100718 if (not_using_psk) {
Jerry Yu032b15ce2022-07-11 06:10:03 +0000719 /* We shouldn't have called this extension writer unless we've
720 * chosen to use a PSK. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100721 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Jerry Yu032b15ce2022-07-11 06:10:03 +0000722 }
723
Gilles Peskine449bd832023-01-11 14:50:10 +0100724 MBEDTLS_SSL_DEBUG_MSG(3, ("server hello, adding pre_shared_key extension"));
725 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 6);
Jerry Yu032b15ce2022-07-11 06:10:03 +0000726
Gilles Peskine449bd832023-01-11 14:50:10 +0100727 MBEDTLS_PUT_UINT16_BE(MBEDTLS_TLS_EXT_PRE_SHARED_KEY, p, 0);
728 MBEDTLS_PUT_UINT16_BE(2, p, 2);
Jerry Yu032b15ce2022-07-11 06:10:03 +0000729
Gilles Peskine449bd832023-01-11 14:50:10 +0100730 MBEDTLS_PUT_UINT16_BE(ssl->handshake->selected_identity, p, 4);
Jerry Yu032b15ce2022-07-11 06:10:03 +0000731
732 *olen = 6;
733
Gilles Peskine449bd832023-01-11 14:50:10 +0100734 MBEDTLS_SSL_DEBUG_MSG(4, ("sent selected_identity: %u",
735 ssl->handshake->selected_identity));
Jerry Yu032b15ce2022-07-11 06:10:03 +0000736
Gilles Peskine449bd832023-01-11 14:50:10 +0100737 mbedtls_ssl_tls13_set_hs_sent_ext_mask(ssl, MBEDTLS_TLS_EXT_PRE_SHARED_KEY);
Jerry Yub95dd362022-11-08 21:19:34 +0800738
Gilles Peskine449bd832023-01-11 14:50:10 +0100739 return 0;
Jerry Yu032b15ce2022-07-11 06:10:03 +0000740}
741
Ronald Cron41a443a2022-10-04 16:38:25 +0200742#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED */
Jerry Yue19e3b92022-07-08 12:04:51 +0000743
XiaokangQian7807f9f2022-02-15 10:04:37 +0000744/* From RFC 8446:
745 * struct {
XiaokangQiancfd925f2022-04-14 07:10:37 +0000746 * ProtocolVersion versions<2..254>;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000747 * } SupportedVersions;
748 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200749MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100750static int ssl_tls13_parse_supported_versions_ext(mbedtls_ssl_context *ssl,
751 const unsigned char *buf,
752 const unsigned char *end)
XiaokangQian7807f9f2022-02-15 10:04:37 +0000753{
XiaokangQian7807f9f2022-02-15 10:04:37 +0000754 const unsigned char *p = buf;
XiaokangQian8f9dfe42022-04-15 02:52:39 +0000755 size_t versions_len;
XiaokangQian4080a7f2022-04-11 09:55:18 +0000756 const unsigned char *versions_end;
XiaokangQian0a1b54e2022-04-21 03:01:38 +0000757 uint16_t tls_version;
Ronald Cron5af4c7f2023-03-07 20:46:59 +0100758 int found_supported_version = 0;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000759
Gilles Peskine449bd832023-01-11 14:50:10 +0100760 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, 1);
XiaokangQian4080a7f2022-04-11 09:55:18 +0000761 versions_len = p[0];
XiaokangQian7807f9f2022-02-15 10:04:37 +0000762 p += 1;
763
Gilles Peskine449bd832023-01-11 14:50:10 +0100764 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, versions_len);
XiaokangQian4080a7f2022-04-11 09:55:18 +0000765 versions_end = p + versions_len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100766 while (p < versions_end) {
767 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, versions_end, 2);
768 tls_version = mbedtls_ssl_read_version(p, ssl->conf->transport);
XiaokangQianb67384d2022-04-19 00:02:38 +0000769 p += 2;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000770
Ronald Cron3bd2b022023-04-03 16:45:39 +0200771 if (MBEDTLS_SSL_VERSION_TLS1_3 == tls_version) {
Ronald Cron5af4c7f2023-03-07 20:46:59 +0100772 found_supported_version = 1;
773 break;
774 }
775
Ronald Cron3bd2b022023-04-03 16:45:39 +0200776 if ((MBEDTLS_SSL_VERSION_TLS1_2 == tls_version) &&
777 mbedtls_ssl_conf_is_tls12_enabled(ssl->conf)) {
Ronald Cron5af4c7f2023-03-07 20:46:59 +0100778 found_supported_version = 1;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000779 break;
780 }
XiaokangQian7807f9f2022-02-15 10:04:37 +0000781 }
782
Ronald Cron5af4c7f2023-03-07 20:46:59 +0100783 if (!found_supported_version) {
784 MBEDTLS_SSL_DEBUG_MSG(1, ("No supported version found."));
XiaokangQian7807f9f2022-02-15 10:04:37 +0000785
Gilles Peskine449bd832023-01-11 14:50:10 +0100786 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_PROTOCOL_VERSION,
787 MBEDTLS_ERR_SSL_BAD_PROTOCOL_VERSION);
788 return MBEDTLS_ERR_SSL_BAD_PROTOCOL_VERSION;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000789 }
790
Ronald Cron5af4c7f2023-03-07 20:46:59 +0100791 MBEDTLS_SSL_DEBUG_MSG(1, ("Negotiated version: [%04x]",
Gilles Peskine449bd832023-01-11 14:50:10 +0100792 (unsigned int) tls_version));
XiaokangQian7807f9f2022-02-15 10:04:37 +0000793
Ronald Cron5af4c7f2023-03-07 20:46:59 +0100794 return (int) tls_version;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000795}
796
Przemek Stekiel8c0a9532023-06-15 16:48:19 +0200797#if defined(PSA_WANT_ALG_ECDH) || defined(PSA_WANT_ALG_FFDH)
XiaokangQiane8ff3502022-04-22 02:34:40 +0000798/*
XiaokangQian7807f9f2022-02-15 10:04:37 +0000799 *
800 * From RFC 8446:
801 * enum {
802 * ... (0xFFFF)
803 * } NamedGroup;
804 * struct {
805 * NamedGroup named_group_list<2..2^16-1>;
806 * } NamedGroupList;
807 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200808MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100809static int ssl_tls13_parse_supported_groups_ext(mbedtls_ssl_context *ssl,
810 const unsigned char *buf,
811 const unsigned char *end)
XiaokangQian7807f9f2022-02-15 10:04:37 +0000812{
XiaokangQian7807f9f2022-02-15 10:04:37 +0000813 const unsigned char *p = buf;
XiaokangQian84823772022-04-19 07:57:30 +0000814 size_t named_group_list_len;
XiaokangQian8f9dfe42022-04-15 02:52:39 +0000815 const unsigned char *named_group_list_end;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000816
Gilles Peskine449bd832023-01-11 14:50:10 +0100817 MBEDTLS_SSL_DEBUG_BUF(3, "supported_groups extension", p, end - buf);
818 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, 2);
819 named_group_list_len = MBEDTLS_GET_UINT16_BE(p, 0);
XiaokangQian7807f9f2022-02-15 10:04:37 +0000820 p += 2;
Gilles Peskine449bd832023-01-11 14:50:10 +0100821 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, named_group_list_len);
XiaokangQian8f9dfe42022-04-15 02:52:39 +0000822 named_group_list_end = p + named_group_list_len;
XiaokangQian4e8cd7b2022-04-21 09:48:09 +0000823 ssl->handshake->hrr_selected_group = 0;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000824
Gilles Peskine449bd832023-01-11 14:50:10 +0100825 while (p < named_group_list_end) {
XiaokangQian08037552022-04-20 07:16:41 +0000826 uint16_t named_group;
Gilles Peskine449bd832023-01-11 14:50:10 +0100827 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, named_group_list_end, 2);
828 named_group = MBEDTLS_GET_UINT16_BE(p, 0);
XiaokangQian08037552022-04-20 07:16:41 +0000829 p += 2;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000830
Gilles Peskine449bd832023-01-11 14:50:10 +0100831 MBEDTLS_SSL_DEBUG_MSG(2,
832 ("got named group: %s(%04x)",
833 mbedtls_ssl_named_group_to_str(named_group),
834 named_group));
XiaokangQian08037552022-04-20 07:16:41 +0000835
Gilles Peskine449bd832023-01-11 14:50:10 +0100836 if (!mbedtls_ssl_named_group_is_offered(ssl, named_group) ||
837 !mbedtls_ssl_named_group_is_supported(named_group) ||
838 ssl->handshake->hrr_selected_group != 0) {
XiaokangQian08037552022-04-20 07:16:41 +0000839 continue;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000840 }
841
Gilles Peskine449bd832023-01-11 14:50:10 +0100842 MBEDTLS_SSL_DEBUG_MSG(2,
843 ("add named group %s(%04x) into received list.",
844 mbedtls_ssl_named_group_to_str(named_group),
845 named_group));
Jerry Yuc1be19f2022-04-23 16:11:39 +0800846
XiaokangQian4e8cd7b2022-04-21 09:48:09 +0000847 ssl->handshake->hrr_selected_group = named_group;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000848 }
849
Gilles Peskine449bd832023-01-11 14:50:10 +0100850 return 0;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000851
852}
Przemek Stekiel8c0a9532023-06-15 16:48:19 +0200853#endif /* PSA_WANT_ALG_ECDH || PSA_WANT_ALG_FFDH */
XiaokangQian7807f9f2022-02-15 10:04:37 +0000854
XiaokangQian08037552022-04-20 07:16:41 +0000855#define SSL_TLS1_3_PARSE_KEY_SHARES_EXT_NO_MATCH 1
856
Valerio Settic9ae8622023-07-25 11:23:50 +0200857#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_EPHEMERAL_ENABLED)
XiaokangQian7807f9f2022-02-15 10:04:37 +0000858/*
859 * ssl_tls13_parse_key_shares_ext() verifies whether the information in the
Xiaokang Qian9f1747b2023-03-30 02:50:04 +0000860 * extension is correct and stores the first acceptable key share and its
861 * associated group.
XiaokangQian7807f9f2022-02-15 10:04:37 +0000862 *
863 * Possible return values are:
864 * - 0: Successful processing of the client provided key share extension.
Xiaokang Qian9f1747b2023-03-30 02:50:04 +0000865 * - SSL_TLS1_3_PARSE_KEY_SHARES_EXT_NO_MATCH: The key shares provided by
866 * the client does not match a group supported by the server. A
867 * HelloRetryRequest will be needed.
XiaokangQiane8ff3502022-04-22 02:34:40 +0000868 * - A negative value for fatal errors.
Jerry Yuc1be19f2022-04-23 16:11:39 +0800869 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200870MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100871static int ssl_tls13_parse_key_shares_ext(mbedtls_ssl_context *ssl,
872 const unsigned char *buf,
873 const unsigned char *end)
XiaokangQian7807f9f2022-02-15 10:04:37 +0000874{
XiaokangQianb67384d2022-04-19 00:02:38 +0000875 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000876 unsigned char const *p = buf;
XiaokangQian8f9dfe42022-04-15 02:52:39 +0000877 unsigned char const *client_shares_end;
Jerry Yuc1be19f2022-04-23 16:11:39 +0800878 size_t client_shares_len;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000879
880 /* From RFC 8446:
881 *
882 * struct {
883 * KeyShareEntry client_shares<0..2^16-1>;
884 * } KeyShareClientHello;
885 *
886 */
887
Gilles Peskine449bd832023-01-11 14:50:10 +0100888 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, 2);
889 client_shares_len = MBEDTLS_GET_UINT16_BE(p, 0);
XiaokangQian7807f9f2022-02-15 10:04:37 +0000890 p += 2;
Gilles Peskine449bd832023-01-11 14:50:10 +0100891 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, client_shares_len);
XiaokangQian7807f9f2022-02-15 10:04:37 +0000892
893 ssl->handshake->offered_group_id = 0;
XiaokangQian8f9dfe42022-04-15 02:52:39 +0000894 client_shares_end = p + client_shares_len;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000895
896 /* We try to find a suitable key share entry and copy it to the
897 * handshake context. Later, we have to find out whether we can do
898 * something with the provided key share or whether we have to
XiaokangQianc5763b52022-04-02 03:34:37 +0000899 * dismiss it and send a HelloRetryRequest message.
900 */
XiaokangQian7807f9f2022-02-15 10:04:37 +0000901
Gilles Peskine449bd832023-01-11 14:50:10 +0100902 while (p < client_shares_end) {
XiaokangQian9b5d04b2022-04-10 10:20:43 +0000903 uint16_t group;
Jerry Yuc1be19f2022-04-23 16:11:39 +0800904 size_t key_exchange_len;
Jerry Yu086edc22022-05-05 10:50:38 +0800905 const unsigned char *key_exchange;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000906
907 /*
908 * struct {
909 * NamedGroup group;
910 * opaque key_exchange<1..2^16-1>;
911 * } KeyShareEntry;
912 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100913 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, client_shares_end, 4);
914 group = MBEDTLS_GET_UINT16_BE(p, 0);
915 key_exchange_len = MBEDTLS_GET_UINT16_BE(p, 2);
Jerry Yuc1be19f2022-04-23 16:11:39 +0800916 p += 4;
Jerry Yu086edc22022-05-05 10:50:38 +0800917 key_exchange = p;
Gilles Peskine449bd832023-01-11 14:50:10 +0100918 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, client_shares_end, key_exchange_len);
Jerry Yu086edc22022-05-05 10:50:38 +0800919 p += key_exchange_len;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000920
921 /* Continue parsing even if we have already found a match,
XiaokangQianc5763b52022-04-02 03:34:37 +0000922 * for input validation purposes.
923 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100924 if (!mbedtls_ssl_named_group_is_offered(ssl, group) ||
925 !mbedtls_ssl_named_group_is_supported(group) ||
926 ssl->handshake->offered_group_id != 0) {
XiaokangQian060d8672022-04-21 09:24:56 +0000927 continue;
928 }
XiaokangQian060d8672022-04-21 09:24:56 +0000929
XiaokangQian7807f9f2022-02-15 10:04:37 +0000930 /*
Przemek Stekielc89f3ea2023-05-18 15:45:53 +0200931 * ECDHE and FFDHE groups are supported
XiaokangQian7807f9f2022-02-15 10:04:37 +0000932 */
Przemek Stekielc89f3ea2023-05-18 15:45:53 +0200933 if (mbedtls_ssl_tls13_named_group_is_ecdhe(group) ||
Przemek Stekield5f79e72023-06-29 09:08:43 +0200934 mbedtls_ssl_tls13_named_group_is_ffdh(group)) {
Przemek Stekielc89f3ea2023-05-18 15:45:53 +0200935 MBEDTLS_SSL_DEBUG_MSG(2, ("ECDH/FFDH group: %s (%04x)",
Gilles Peskine449bd832023-01-11 14:50:10 +0100936 mbedtls_ssl_named_group_to_str(group),
937 group));
Przemek Stekiel7ac93be2023-07-04 10:02:38 +0200938 ret = mbedtls_ssl_tls13_read_public_xxdhe_share(
Gilles Peskine449bd832023-01-11 14:50:10 +0100939 ssl, key_exchange - 2, key_exchange_len + 2);
940 if (ret != 0) {
941 return ret;
942 }
XiaokangQian4e8cd7b2022-04-21 09:48:09 +0000943
Gilles Peskine449bd832023-01-11 14:50:10 +0100944 } else {
945 MBEDTLS_SSL_DEBUG_MSG(4, ("Unrecognized NamedGroup %u",
946 (unsigned) group));
XiaokangQian7807f9f2022-02-15 10:04:37 +0000947 continue;
948 }
949
XiaokangQian9b5d04b2022-04-10 10:20:43 +0000950 ssl->handshake->offered_group_id = group;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000951 }
952
Jerry Yuc1be19f2022-04-23 16:11:39 +0800953
Gilles Peskine449bd832023-01-11 14:50:10 +0100954 if (ssl->handshake->offered_group_id == 0) {
955 MBEDTLS_SSL_DEBUG_MSG(1, ("no matching key share"));
956 return SSL_TLS1_3_PARSE_KEY_SHARES_EXT_NO_MATCH;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000957 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100958 return 0;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000959}
Valerio Settic9ae8622023-07-25 11:23:50 +0200960#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_EPHEMERAL_ENABLED */
XiaokangQian7807f9f2022-02-15 10:04:37 +0000961
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200962MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100963static int ssl_tls13_client_hello_has_exts(mbedtls_ssl_context *ssl,
964 int exts_mask)
XiaokangQian7807f9f2022-02-15 10:04:37 +0000965{
Jerry Yu0c354a22022-08-29 15:25:36 +0800966 int masked = ssl->handshake->received_extensions & exts_mask;
Gilles Peskine449bd832023-01-11 14:50:10 +0100967 return masked == exts_mask;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000968}
969
Jerry Yue5991322022-11-07 14:03:44 +0800970#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200971MBEDTLS_CHECK_RETURN_CRITICAL
XiaokangQianb67384d2022-04-19 00:02:38 +0000972static int ssl_tls13_client_hello_has_exts_for_ephemeral_key_exchange(
Gilles Peskine449bd832023-01-11 14:50:10 +0100973 mbedtls_ssl_context *ssl)
XiaokangQian7807f9f2022-02-15 10:04:37 +0000974{
Gilles Peskine449bd832023-01-11 14:50:10 +0100975 return ssl_tls13_client_hello_has_exts(
976 ssl,
977 MBEDTLS_SSL_EXT_MASK(SUPPORTED_GROUPS) |
978 MBEDTLS_SSL_EXT_MASK(KEY_SHARE) |
979 MBEDTLS_SSL_EXT_MASK(SIG_ALG));
XiaokangQian7807f9f2022-02-15 10:04:37 +0000980}
Jerry Yue5991322022-11-07 14:03:44 +0800981#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED */
XiaokangQian7807f9f2022-02-15 10:04:37 +0000982
Jerry Yue5991322022-11-07 14:03:44 +0800983#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK_ENABLED)
Jerry Yu77f01482022-07-11 07:03:24 +0000984MBEDTLS_CHECK_RETURN_CRITICAL
985static int ssl_tls13_client_hello_has_exts_for_psk_key_exchange(
Gilles Peskine449bd832023-01-11 14:50:10 +0100986 mbedtls_ssl_context *ssl)
Jerry Yu77f01482022-07-11 07:03:24 +0000987{
Gilles Peskine449bd832023-01-11 14:50:10 +0100988 return ssl_tls13_client_hello_has_exts(
989 ssl,
990 MBEDTLS_SSL_EXT_MASK(PRE_SHARED_KEY) |
991 MBEDTLS_SSL_EXT_MASK(PSK_KEY_EXCHANGE_MODES));
Jerry Yu77f01482022-07-11 07:03:24 +0000992}
Jerry Yue5991322022-11-07 14:03:44 +0800993#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK_ENABLED */
Jerry Yu77f01482022-07-11 07:03:24 +0000994
Jerry Yue5991322022-11-07 14:03:44 +0800995#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK_EPHEMERAL_ENABLED)
Jerry Yu77f01482022-07-11 07:03:24 +0000996MBEDTLS_CHECK_RETURN_CRITICAL
997static int ssl_tls13_client_hello_has_exts_for_psk_ephemeral_key_exchange(
Gilles Peskine449bd832023-01-11 14:50:10 +0100998 mbedtls_ssl_context *ssl)
Jerry Yu77f01482022-07-11 07:03:24 +0000999{
Gilles Peskine449bd832023-01-11 14:50:10 +01001000 return ssl_tls13_client_hello_has_exts(
1001 ssl,
1002 MBEDTLS_SSL_EXT_MASK(SUPPORTED_GROUPS) |
1003 MBEDTLS_SSL_EXT_MASK(KEY_SHARE) |
1004 MBEDTLS_SSL_EXT_MASK(PRE_SHARED_KEY) |
1005 MBEDTLS_SSL_EXT_MASK(PSK_KEY_EXCHANGE_MODES));
Jerry Yu77f01482022-07-11 07:03:24 +00001006}
Jerry Yue5991322022-11-07 14:03:44 +08001007#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK_EPHEMERAL_ENABLED */
Jerry Yu77f01482022-07-11 07:03:24 +00001008
Pengyu Lv306a01d2023-02-02 16:35:47 +08001009#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED)
1010MBEDTLS_CHECK_RETURN_CRITICAL
Pengyu Lv52ad3332023-02-14 14:32:37 +08001011static int ssl_tls13_ticket_permission_check(mbedtls_ssl_context *ssl,
1012 unsigned int kex_mode)
Pengyu Lv306a01d2023-02-02 16:35:47 +08001013{
1014#if defined(MBEDTLS_SSL_SESSION_TICKETS)
1015 if (ssl->handshake->resume) {
Pengyu Lv7b711712023-10-24 17:07:14 +08001016 if (mbedtls_ssl_session_check_ticket_flags(
Pengyu Lv306a01d2023-02-02 16:35:47 +08001017 ssl->session_negotiate, kex_mode)) {
1018 return 0;
1019 }
1020 }
1021#else
1022 ((void) ssl);
1023 ((void) kex_mode);
1024#endif
1025 return 1;
1026}
1027#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED */
1028
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001029MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01001030static int ssl_tls13_check_ephemeral_key_exchange(mbedtls_ssl_context *ssl)
XiaokangQian7807f9f2022-02-15 10:04:37 +00001031{
Jerry Yue5991322022-11-07 14:03:44 +08001032#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01001033 return mbedtls_ssl_conf_tls13_ephemeral_enabled(ssl) &&
1034 ssl_tls13_client_hello_has_exts_for_ephemeral_key_exchange(ssl);
Jerry Yue5991322022-11-07 14:03:44 +08001035#else
1036 ((void) ssl);
Gilles Peskine449bd832023-01-11 14:50:10 +01001037 return 0;
Jerry Yue5991322022-11-07 14:03:44 +08001038#endif
Jerry Yu77f01482022-07-11 07:03:24 +00001039}
XiaokangQian7807f9f2022-02-15 10:04:37 +00001040
Jerry Yu77f01482022-07-11 07:03:24 +00001041MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01001042static int ssl_tls13_check_psk_key_exchange(mbedtls_ssl_context *ssl)
Jerry Yu77f01482022-07-11 07:03:24 +00001043{
Jerry Yue5991322022-11-07 14:03:44 +08001044#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK_ENABLED)
Pengyu Lv52ad3332023-02-14 14:32:37 +08001045 return ssl_tls13_ticket_permission_check(
Pengyu Lv306a01d2023-02-02 16:35:47 +08001046 ssl, MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK) &&
1047 mbedtls_ssl_conf_tls13_psk_enabled(ssl) &&
Gilles Peskine449bd832023-01-11 14:50:10 +01001048 mbedtls_ssl_tls13_psk_enabled(ssl) &&
1049 ssl_tls13_client_hello_has_exts_for_psk_key_exchange(ssl);
Jerry Yu77f01482022-07-11 07:03:24 +00001050#else
1051 ((void) ssl);
Gilles Peskine449bd832023-01-11 14:50:10 +01001052 return 0;
Jerry Yu77f01482022-07-11 07:03:24 +00001053#endif
1054}
XiaokangQian7807f9f2022-02-15 10:04:37 +00001055
Jerry Yu77f01482022-07-11 07:03:24 +00001056MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01001057static int ssl_tls13_check_psk_ephemeral_key_exchange(mbedtls_ssl_context *ssl)
Jerry Yu77f01482022-07-11 07:03:24 +00001058{
Jerry Yue5991322022-11-07 14:03:44 +08001059#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK_EPHEMERAL_ENABLED)
Pengyu Lv52ad3332023-02-14 14:32:37 +08001060 return ssl_tls13_ticket_permission_check(
Pengyu Lv306a01d2023-02-02 16:35:47 +08001061 ssl, MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK_EPHEMERAL) &&
1062 mbedtls_ssl_conf_tls13_psk_ephemeral_enabled(ssl) &&
Gilles Peskine449bd832023-01-11 14:50:10 +01001063 mbedtls_ssl_tls13_psk_ephemeral_enabled(ssl) &&
1064 ssl_tls13_client_hello_has_exts_for_psk_ephemeral_key_exchange(ssl);
Jerry Yu77f01482022-07-11 07:03:24 +00001065#else
1066 ((void) ssl);
Gilles Peskine449bd832023-01-11 14:50:10 +01001067 return 0;
Jerry Yu77f01482022-07-11 07:03:24 +00001068#endif
1069}
1070
Gilles Peskine449bd832023-01-11 14:50:10 +01001071static int ssl_tls13_determine_key_exchange_mode(mbedtls_ssl_context *ssl)
Jerry Yu77f01482022-07-11 07:03:24 +00001072{
1073 /*
1074 * Determine the key exchange algorithm to use.
1075 * There are three types of key exchanges supported in TLS 1.3:
1076 * - (EC)DH with ECDSA,
1077 * - (EC)DH with PSK,
1078 * - plain PSK.
1079 *
1080 * The PSK-based key exchanges may additionally be used with 0-RTT.
1081 *
1082 * Our built-in order of preference is
Jerry Yuce6ed702022-07-22 21:49:53 +08001083 * 1 ) (EC)DHE-PSK Mode ( psk_ephemeral )
1084 * 2 ) Certificate Mode ( ephemeral )
1085 * 3 ) Plain PSK Mode ( psk )
Jerry Yu77f01482022-07-11 07:03:24 +00001086 */
1087
Xiaokang Qian9f1747b2023-03-30 02:50:04 +00001088 ssl->handshake->key_exchange_mode =
1089 MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_NONE;
Jerry Yu77f01482022-07-11 07:03:24 +00001090
Gilles Peskine449bd832023-01-11 14:50:10 +01001091 if (ssl_tls13_check_psk_ephemeral_key_exchange(ssl)) {
Jerry Yu77f01482022-07-11 07:03:24 +00001092 ssl->handshake->key_exchange_mode =
1093 MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK_EPHEMERAL;
Gilles Peskine449bd832023-01-11 14:50:10 +01001094 MBEDTLS_SSL_DEBUG_MSG(2, ("key exchange mode: psk_ephemeral"));
1095 } else
1096 if (ssl_tls13_check_ephemeral_key_exchange(ssl)) {
Jerry Yu77f01482022-07-11 07:03:24 +00001097 ssl->handshake->key_exchange_mode =
1098 MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL;
Gilles Peskine449bd832023-01-11 14:50:10 +01001099 MBEDTLS_SSL_DEBUG_MSG(2, ("key exchange mode: ephemeral"));
1100 } else
1101 if (ssl_tls13_check_psk_key_exchange(ssl)) {
Jerry Yuce6ed702022-07-22 21:49:53 +08001102 ssl->handshake->key_exchange_mode =
1103 MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK;
Gilles Peskine449bd832023-01-11 14:50:10 +01001104 MBEDTLS_SSL_DEBUG_MSG(2, ("key exchange mode: psk"));
1105 } else {
Jerry Yu77f01482022-07-11 07:03:24 +00001106 MBEDTLS_SSL_DEBUG_MSG(
Gilles Peskine449bd832023-01-11 14:50:10 +01001107 1,
1108 ("ClientHello message misses mandatory extensions."));
1109 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_MISSING_EXTENSION,
1110 MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER);
1111 return MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER;
Jerry Yu77f01482022-07-11 07:03:24 +00001112 }
1113
Gilles Peskine449bd832023-01-11 14:50:10 +01001114 return 0;
Jerry Yu77f01482022-07-11 07:03:24 +00001115
XiaokangQian7807f9f2022-02-15 10:04:37 +00001116}
1117
XiaokangQian81802f42022-06-10 13:25:22 +00001118#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
Ronald Cron928cbd32022-10-04 16:14:26 +02001119 defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED)
Ronald Cron67ea2542022-09-15 17:34:42 +02001120
1121#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01001122static psa_algorithm_t ssl_tls13_iana_sig_alg_to_psa_alg(uint16_t sig_alg)
Ronald Cron67ea2542022-09-15 17:34:42 +02001123{
Gilles Peskine449bd832023-01-11 14:50:10 +01001124 switch (sig_alg) {
Ronald Cron67ea2542022-09-15 17:34:42 +02001125 case MBEDTLS_TLS1_3_SIG_ECDSA_SECP256R1_SHA256:
Gilles Peskine449bd832023-01-11 14:50:10 +01001126 return PSA_ALG_ECDSA(PSA_ALG_SHA_256);
Ronald Cron67ea2542022-09-15 17:34:42 +02001127 case MBEDTLS_TLS1_3_SIG_ECDSA_SECP384R1_SHA384:
Gilles Peskine449bd832023-01-11 14:50:10 +01001128 return PSA_ALG_ECDSA(PSA_ALG_SHA_384);
Ronald Cron67ea2542022-09-15 17:34:42 +02001129 case MBEDTLS_TLS1_3_SIG_ECDSA_SECP521R1_SHA512:
Gilles Peskine449bd832023-01-11 14:50:10 +01001130 return PSA_ALG_ECDSA(PSA_ALG_SHA_512);
Ronald Cron67ea2542022-09-15 17:34:42 +02001131 case MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA256:
Gilles Peskine449bd832023-01-11 14:50:10 +01001132 return PSA_ALG_RSA_PSS(PSA_ALG_SHA_256);
Ronald Cron67ea2542022-09-15 17:34:42 +02001133 case MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA384:
Gilles Peskine449bd832023-01-11 14:50:10 +01001134 return PSA_ALG_RSA_PSS(PSA_ALG_SHA_384);
Ronald Cron67ea2542022-09-15 17:34:42 +02001135 case MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA512:
Gilles Peskine449bd832023-01-11 14:50:10 +01001136 return PSA_ALG_RSA_PSS(PSA_ALG_SHA_512);
Ronald Cron67ea2542022-09-15 17:34:42 +02001137 case MBEDTLS_TLS1_3_SIG_RSA_PKCS1_SHA256:
Gilles Peskine449bd832023-01-11 14:50:10 +01001138 return PSA_ALG_RSA_PKCS1V15_SIGN(PSA_ALG_SHA_256);
Ronald Cron67ea2542022-09-15 17:34:42 +02001139 case MBEDTLS_TLS1_3_SIG_RSA_PKCS1_SHA384:
Gilles Peskine449bd832023-01-11 14:50:10 +01001140 return PSA_ALG_RSA_PKCS1V15_SIGN(PSA_ALG_SHA_384);
Ronald Cron67ea2542022-09-15 17:34:42 +02001141 case MBEDTLS_TLS1_3_SIG_RSA_PKCS1_SHA512:
Gilles Peskine449bd832023-01-11 14:50:10 +01001142 return PSA_ALG_RSA_PKCS1V15_SIGN(PSA_ALG_SHA_512);
Ronald Cron67ea2542022-09-15 17:34:42 +02001143 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01001144 return PSA_ALG_NONE;
Ronald Cron67ea2542022-09-15 17:34:42 +02001145 }
1146}
1147#endif /* MBEDTLS_USE_PSA_CRYPTO */
1148
XiaokangQian23c5be62022-06-07 02:04:34 +00001149/*
XiaokangQianfb665a82022-06-15 03:57:21 +00001150 * Pick best ( private key, certificate chain ) pair based on the signature
1151 * algorithms supported by the client.
XiaokangQian23c5be62022-06-07 02:04:34 +00001152 */
Ronald Cronce7d76e2022-07-08 18:56:49 +02001153MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01001154static int ssl_tls13_pick_key_cert(mbedtls_ssl_context *ssl)
XiaokangQian23c5be62022-06-07 02:04:34 +00001155{
XiaokangQianfb665a82022-06-15 03:57:21 +00001156 mbedtls_ssl_key_cert *key_cert, *key_cert_list;
XiaokangQian81802f42022-06-10 13:25:22 +00001157 const uint16_t *sig_alg = ssl->handshake->received_sig_algs;
XiaokangQian23c5be62022-06-07 02:04:34 +00001158
1159#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Gilles Peskine449bd832023-01-11 14:50:10 +01001160 if (ssl->handshake->sni_key_cert != NULL) {
XiaokangQianfb665a82022-06-15 03:57:21 +00001161 key_cert_list = ssl->handshake->sni_key_cert;
Gilles Peskine449bd832023-01-11 14:50:10 +01001162 } else
XiaokangQian23c5be62022-06-07 02:04:34 +00001163#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
Gilles Peskine449bd832023-01-11 14:50:10 +01001164 key_cert_list = ssl->conf->key_cert;
XiaokangQian23c5be62022-06-07 02:04:34 +00001165
Gilles Peskine449bd832023-01-11 14:50:10 +01001166 if (key_cert_list == NULL) {
1167 MBEDTLS_SSL_DEBUG_MSG(3, ("server has no certificate"));
1168 return -1;
XiaokangQian23c5be62022-06-07 02:04:34 +00001169 }
1170
Gilles Peskine449bd832023-01-11 14:50:10 +01001171 for (; *sig_alg != MBEDTLS_TLS1_3_SIG_NONE; sig_alg++) {
1172 if (!mbedtls_ssl_sig_alg_is_offered(ssl, *sig_alg)) {
Ronald Cron67ea2542022-09-15 17:34:42 +02001173 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +01001174 }
Ronald Cron67ea2542022-09-15 17:34:42 +02001175
Gilles Peskine449bd832023-01-11 14:50:10 +01001176 if (!mbedtls_ssl_tls13_sig_alg_for_cert_verify_is_supported(*sig_alg)) {
Ronald Cron67ea2542022-09-15 17:34:42 +02001177 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +01001178 }
Ronald Cron67ea2542022-09-15 17:34:42 +02001179
Gilles Peskine449bd832023-01-11 14:50:10 +01001180 for (key_cert = key_cert_list; key_cert != NULL;
1181 key_cert = key_cert->next) {
Ronald Cron67ea2542022-09-15 17:34:42 +02001182#if defined(MBEDTLS_USE_PSA_CRYPTO)
1183 psa_algorithm_t psa_alg = PSA_ALG_NONE;
1184#endif /* MBEDTLS_USE_PSA_CRYPTO */
1185
Gilles Peskine449bd832023-01-11 14:50:10 +01001186 MBEDTLS_SSL_DEBUG_CRT(3, "certificate (chain) candidate",
1187 key_cert->cert);
XiaokangQian81802f42022-06-10 13:25:22 +00001188
1189 /*
Gilles Peskine449bd832023-01-11 14:50:10 +01001190 * This avoids sending the client a cert it'll reject based on
1191 * keyUsage or other extensions.
1192 */
1193 if (mbedtls_x509_crt_check_key_usage(
1194 key_cert->cert, MBEDTLS_X509_KU_DIGITAL_SIGNATURE) != 0 ||
XiaokangQian81802f42022-06-10 13:25:22 +00001195 mbedtls_x509_crt_check_extended_key_usage(
XiaokangQianfb665a82022-06-15 03:57:21 +00001196 key_cert->cert, MBEDTLS_OID_SERVER_AUTH,
Gilles Peskine449bd832023-01-11 14:50:10 +01001197 MBEDTLS_OID_SIZE(MBEDTLS_OID_SERVER_AUTH)) != 0) {
1198 MBEDTLS_SSL_DEBUG_MSG(3, ("certificate mismatch: "
1199 "(extended) key usage extension"));
XiaokangQian81802f42022-06-10 13:25:22 +00001200 continue;
1201 }
XiaokangQian23c5be62022-06-07 02:04:34 +00001202
Gilles Peskine449bd832023-01-11 14:50:10 +01001203 MBEDTLS_SSL_DEBUG_MSG(3,
1204 ("ssl_tls13_pick_key_cert:"
1205 "check signature algorithm %s [%04x]",
1206 mbedtls_ssl_sig_alg_to_str(*sig_alg),
1207 *sig_alg));
Ronald Cron67ea2542022-09-15 17:34:42 +02001208#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01001209 psa_alg = ssl_tls13_iana_sig_alg_to_psa_alg(*sig_alg);
Ronald Cron67ea2542022-09-15 17:34:42 +02001210#endif /* MBEDTLS_USE_PSA_CRYPTO */
1211
Gilles Peskine449bd832023-01-11 14:50:10 +01001212 if (mbedtls_ssl_tls13_check_sig_alg_cert_key_match(
1213 *sig_alg, &key_cert->cert->pk)
Ronald Cron67ea2542022-09-15 17:34:42 +02001214#if defined(MBEDTLS_USE_PSA_CRYPTO)
1215 && psa_alg != PSA_ALG_NONE &&
Gilles Peskine449bd832023-01-11 14:50:10 +01001216 mbedtls_pk_can_do_ext(&key_cert->cert->pk, psa_alg,
1217 PSA_KEY_USAGE_SIGN_HASH) == 1
Ronald Cron67ea2542022-09-15 17:34:42 +02001218#endif /* MBEDTLS_USE_PSA_CRYPTO */
Gilles Peskine449bd832023-01-11 14:50:10 +01001219 ) {
XiaokangQianfb665a82022-06-15 03:57:21 +00001220 ssl->handshake->key_cert = key_cert;
Gilles Peskine449bd832023-01-11 14:50:10 +01001221 MBEDTLS_SSL_DEBUG_MSG(3,
1222 ("ssl_tls13_pick_key_cert:"
1223 "selected signature algorithm"
1224 " %s [%04x]",
1225 mbedtls_ssl_sig_alg_to_str(*sig_alg),
1226 *sig_alg));
XiaokangQianfb665a82022-06-15 03:57:21 +00001227 MBEDTLS_SSL_DEBUG_CRT(
Gilles Peskine449bd832023-01-11 14:50:10 +01001228 3, "selected certificate (chain)",
1229 ssl->handshake->key_cert->cert);
1230 return 0;
XiaokangQian81802f42022-06-10 13:25:22 +00001231 }
XiaokangQian81802f42022-06-10 13:25:22 +00001232 }
XiaokangQian23c5be62022-06-07 02:04:34 +00001233 }
1234
Gilles Peskine449bd832023-01-11 14:50:10 +01001235 MBEDTLS_SSL_DEBUG_MSG(2, ("ssl_tls13_pick_key_cert:"
1236 "no suitable certificate found"));
1237 return -1;
XiaokangQian23c5be62022-06-07 02:04:34 +00001238}
XiaokangQian81802f42022-06-10 13:25:22 +00001239#endif /* MBEDTLS_X509_CRT_PARSE_C &&
Ronald Cron928cbd32022-10-04 16:14:26 +02001240 MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED */
XiaokangQian23c5be62022-06-07 02:04:34 +00001241
XiaokangQian4080a7f2022-04-11 09:55:18 +00001242/*
XiaokangQianed582dd2022-04-13 08:21:05 +00001243 *
1244 * STATE HANDLING: ClientHello
1245 *
XiaokangQianb67384d2022-04-19 00:02:38 +00001246 * There are three possible classes of outcomes when parsing the ClientHello:
XiaokangQianed582dd2022-04-13 08:21:05 +00001247 *
XiaokangQianb67384d2022-04-19 00:02:38 +00001248 * 1) The ClientHello was well-formed and matched the server's configuration.
XiaokangQianed582dd2022-04-13 08:21:05 +00001249 *
1250 * In this case, the server progresses to sending its ServerHello.
1251 *
XiaokangQianb67384d2022-04-19 00:02:38 +00001252 * 2) The ClientHello was well-formed but didn't match the server's
1253 * configuration.
XiaokangQianed582dd2022-04-13 08:21:05 +00001254 *
1255 * For example, the client might not have offered a key share which
1256 * the server supports, or the server might require a cookie.
1257 *
1258 * In this case, the server sends a HelloRetryRequest.
1259 *
XiaokangQianb67384d2022-04-19 00:02:38 +00001260 * 3) The ClientHello was ill-formed
XiaokangQianed582dd2022-04-13 08:21:05 +00001261 *
1262 * In this case, we abort the handshake.
1263 *
1264 */
1265
1266/*
XiaokangQian4080a7f2022-04-11 09:55:18 +00001267 * Structure of this message:
1268 *
XiaokangQiane8ff3502022-04-22 02:34:40 +00001269 * uint16 ProtocolVersion;
1270 * opaque Random[32];
1271 * uint8 CipherSuite[2]; // Cryptographic suite selector
XiaokangQian4080a7f2022-04-11 09:55:18 +00001272 *
XiaokangQiane8ff3502022-04-22 02:34:40 +00001273 * struct {
1274 * ProtocolVersion legacy_version = 0x0303; // TLS v1.2
1275 * Random random;
1276 * opaque legacy_session_id<0..32>;
1277 * CipherSuite cipher_suites<2..2^16-2>;
1278 * opaque legacy_compression_methods<1..2^8-1>;
1279 * Extension extensions<8..2^16-1>;
1280 * } ClientHello;
XiaokangQian4080a7f2022-04-11 09:55:18 +00001281 */
XiaokangQianed582dd2022-04-13 08:21:05 +00001282
1283#define SSL_CLIENT_HELLO_OK 0
1284#define SSL_CLIENT_HELLO_HRR_REQUIRED 1
Ronald Cron5af4c7f2023-03-07 20:46:59 +01001285#define SSL_CLIENT_HELLO_TLS1_2 2
XiaokangQianed582dd2022-04-13 08:21:05 +00001286
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001287MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01001288static int ssl_tls13_parse_client_hello(mbedtls_ssl_context *ssl,
1289 const unsigned char *buf,
1290 const unsigned char *end)
XiaokangQian7807f9f2022-02-15 10:04:37 +00001291{
XiaokangQianb67384d2022-04-19 00:02:38 +00001292 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1293 const unsigned char *p = buf;
Ronald Crond540d992023-03-07 09:41:48 +01001294 const unsigned char *random;
XiaokangQian4080a7f2022-04-11 09:55:18 +00001295 size_t legacy_session_id_len;
Ronald Croncada4102023-03-07 09:51:39 +01001296 const unsigned char *legacy_session_id;
XiaokangQian318dc762022-04-20 09:43:51 +00001297 size_t cipher_suites_len;
Ronald Cron2f16b4e2023-03-07 10:07:32 +01001298 const unsigned char *cipher_suites;
XiaokangQian060d8672022-04-21 09:24:56 +00001299 const unsigned char *cipher_suites_end;
XiaokangQianb67384d2022-04-19 00:02:38 +00001300 size_t extensions_len;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001301 const unsigned char *extensions_end;
Ronald Croneff56732023-04-03 17:36:31 +02001302 const unsigned char *supported_versions_data;
1303 const unsigned char *supported_versions_data_end;
Jerry Yuc4bf5d62022-10-29 09:08:47 +08001304 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
Jerry Yu49ca9282022-05-05 11:05:22 +08001305 int hrr_required = 0;
Ronald Cron8a74f072023-06-14 17:59:29 +02001306 int no_usable_share_for_key_agreement = 0;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001307
Ronald Cron41a443a2022-10-04 16:38:25 +02001308#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED)
Jerry Yu29d9faa2022-08-23 17:52:45 +08001309 const unsigned char *pre_shared_key_ext = NULL;
Jerry Yu1c105562022-07-10 06:32:38 +00001310 const unsigned char *pre_shared_key_ext_end = NULL;
Ronald Cron41a443a2022-10-04 16:38:25 +02001311#endif
XiaokangQian7807f9f2022-02-15 10:04:37 +00001312
XiaokangQian7807f9f2022-02-15 10:04:37 +00001313 /*
XiaokangQianb67384d2022-04-19 00:02:38 +00001314 * ClientHello layout:
XiaokangQian7807f9f2022-02-15 10:04:37 +00001315 * 0 . 1 protocol version
XiaokangQiane8ff3502022-04-22 02:34:40 +00001316 * 2 . 33 random bytes
XiaokangQianc5763b52022-04-02 03:34:37 +00001317 * 34 . 34 session id length ( 1 byte )
XiaokangQian7807f9f2022-02-15 10:04:37 +00001318 * 35 . 34+x session id
XiaokangQian7807f9f2022-02-15 10:04:37 +00001319 * .. . .. ciphersuite list length ( 2 bytes )
1320 * .. . .. ciphersuite list
1321 * .. . .. compression alg. list length ( 1 byte )
1322 * .. . .. compression alg. list
1323 * .. . .. extensions length ( 2 bytes, optional )
1324 * .. . .. extensions ( optional )
1325 */
1326
XiaokangQianb67384d2022-04-19 00:02:38 +00001327 /*
bootstrap-prime6dbbf442022-05-17 19:30:44 -04001328 * Minimal length ( with everything empty and extensions omitted ) is
XiaokangQian7807f9f2022-02-15 10:04:37 +00001329 * 2 + 32 + 1 + 2 + 1 = 38 bytes. Check that first, so that we can
1330 * read at least up to session id length without worrying.
1331 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001332 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, 38);
XiaokangQian7807f9f2022-02-15 10:04:37 +00001333
1334 /* ...
1335 * ProtocolVersion legacy_version = 0x0303; // TLS 1.2
1336 * ...
1337 * with ProtocolVersion defined as:
1338 * uint16 ProtocolVersion;
1339 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001340 if (mbedtls_ssl_read_version(p, ssl->conf->transport) !=
1341 MBEDTLS_SSL_VERSION_TLS1_2) {
1342 MBEDTLS_SSL_DEBUG_MSG(1, ("Unsupported version of TLS."));
1343 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_PROTOCOL_VERSION,
1344 MBEDTLS_ERR_SSL_BAD_PROTOCOL_VERSION);
1345 return MBEDTLS_ERR_SSL_BAD_PROTOCOL_VERSION;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001346 }
1347 p += 2;
1348
Jerry Yuc1be19f2022-04-23 16:11:39 +08001349 /* ...
1350 * Random random;
1351 * ...
XiaokangQianb67384d2022-04-19 00:02:38 +00001352 * with Random defined as:
1353 * opaque Random[32];
XiaokangQian7807f9f2022-02-15 10:04:37 +00001354 */
Ronald Crond540d992023-03-07 09:41:48 +01001355 random = p;
XiaokangQian08037552022-04-20 07:16:41 +00001356 p += MBEDTLS_CLIENT_HELLO_RANDOM_LEN;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001357
Jerry Yuc1be19f2022-04-23 16:11:39 +08001358 /* ...
XiaokangQianb67384d2022-04-19 00:02:38 +00001359 * opaque legacy_session_id<0..32>;
Jerry Yuc1be19f2022-04-23 16:11:39 +08001360 * ...
XiaokangQian7807f9f2022-02-15 10:04:37 +00001361 */
Ronald Croncada4102023-03-07 09:51:39 +01001362 legacy_session_id_len = *(p++);
1363 legacy_session_id = p;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001364
XiaokangQianb67384d2022-04-19 00:02:38 +00001365 /*
1366 * Check we have enough data for the legacy session identifier
Jerry Yue95c8af2022-07-26 15:48:20 +08001367 * and the ciphersuite list length.
XiaokangQianb67384d2022-04-19 00:02:38 +00001368 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001369 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, legacy_session_id_len + 2);
XiaokangQian4080a7f2022-04-11 09:55:18 +00001370 p += legacy_session_id_len;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001371
Ronald Cron2f16b4e2023-03-07 10:07:32 +01001372 /* ...
1373 * CipherSuite cipher_suites<2..2^16-2>;
1374 * ...
1375 * with CipherSuite defined as:
1376 * uint8 CipherSuite[2];
1377 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001378 cipher_suites_len = MBEDTLS_GET_UINT16_BE(p, 0);
XiaokangQian7807f9f2022-02-15 10:04:37 +00001379 p += 2;
Ronald Cron2f16b4e2023-03-07 10:07:32 +01001380 cipher_suites = p;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001381
Ronald Cronfc7ae872023-02-16 15:32:19 +01001382 /*
1383 * The length of the ciphersuite list has to be even.
1384 */
1385 if (cipher_suites_len & 1) {
1386 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR,
1387 MBEDTLS_ERR_SSL_DECODE_ERROR);
1388 return MBEDTLS_ERR_SSL_DECODE_ERROR;
1389 }
1390
XiaokangQianb67384d2022-04-19 00:02:38 +00001391 /* Check we have enough data for the ciphersuite list, the legacy
1392 * compression methods and the length of the extensions.
Jerry Yue95c8af2022-07-26 15:48:20 +08001393 *
1394 * cipher_suites cipher_suites_len bytes
1395 * legacy_compression_methods 2 bytes
1396 * extensions_len 2 bytes
XiaokangQianb67384d2022-04-19 00:02:38 +00001397 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001398 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, cipher_suites_len + 2 + 2);
Ronald Cron8c527d02023-03-07 15:47:47 +01001399 p += cipher_suites_len;
1400 cipher_suites_end = p;
Jerry Yu5725f1c2022-08-21 17:27:16 +08001401
1402 /*
Ronald Cron8c527d02023-03-07 15:47:47 +01001403 * Search for the supported versions extension and parse it to determine
1404 * if the client supports TLS 1.3.
1405 */
1406 ret = mbedtls_ssl_tls13_is_supported_versions_ext_present_in_exts(
1407 ssl, p + 2, end,
Ronald Croneff56732023-04-03 17:36:31 +02001408 &supported_versions_data, &supported_versions_data_end);
Ronald Cron8c527d02023-03-07 15:47:47 +01001409 if (ret < 0) {
1410 MBEDTLS_SSL_DEBUG_RET(1,
1411 ("mbedtls_ssl_tls13_is_supported_versions_ext_present_in_exts"), ret);
1412 return ret;
1413 }
1414
1415 if (ret == 0) {
Ronald Cron5af4c7f2023-03-07 20:46:59 +01001416 return SSL_CLIENT_HELLO_TLS1_2;
Ronald Cron8c527d02023-03-07 15:47:47 +01001417 }
1418
Ronald Cron5af4c7f2023-03-07 20:46:59 +01001419 if (ret == 1) {
1420 ret = ssl_tls13_parse_supported_versions_ext(ssl,
Ronald Croneff56732023-04-03 17:36:31 +02001421 supported_versions_data,
1422 supported_versions_data_end);
Ronald Cron5af4c7f2023-03-07 20:46:59 +01001423 if (ret < 0) {
1424 MBEDTLS_SSL_DEBUG_RET(1,
1425 ("ssl_tls13_parse_supported_versions_ext"), ret);
1426 return ret;
1427 }
1428
Ronald Cronb828c7d2023-04-03 16:37:22 +02001429 /*
1430 * The supported versions extension was parsed successfully as the
1431 * value returned by ssl_tls13_parse_supported_versions_ext() is
1432 * positive. The return value is then equal to
1433 * MBEDTLS_SSL_VERSION_TLS1_2 or MBEDTLS_SSL_VERSION_TLS1_3, defining
1434 * the TLS version to negotiate.
1435 */
Ronald Cron5af4c7f2023-03-07 20:46:59 +01001436 if (MBEDTLS_SSL_VERSION_TLS1_2 == ret) {
1437 return SSL_CLIENT_HELLO_TLS1_2;
1438 }
Ronald Cron8c527d02023-03-07 15:47:47 +01001439 }
1440
1441 /*
1442 * We negotiate TLS 1.3.
Ronald Cron64582392023-03-07 09:21:40 +01001443 */
1444 ssl->tls_version = MBEDTLS_SSL_VERSION_TLS1_3;
1445
1446#if defined(MBEDTLS_SSL_SESSION_TICKETS)
1447 /* Store minor version for later use with ticket serialization. */
1448 ssl->session_negotiate->tls_version = MBEDTLS_SSL_VERSION_TLS1_3;
1449 ssl->session_negotiate->endpoint = ssl->conf->endpoint;
1450#endif
1451
1452 /*
Ronald Crondad02b22023-04-06 09:57:52 +02001453 * We are negotiating the version 1.3 of the protocol. Do what we have
Ronald Croncada4102023-03-07 09:51:39 +01001454 * postponed: copy of the client random bytes, copy of the legacy session
Ronald Cron2f16b4e2023-03-07 10:07:32 +01001455 * identifier and selection of the TLS 1.3 cipher suite.
Ronald Crond540d992023-03-07 09:41:48 +01001456 */
1457 MBEDTLS_SSL_DEBUG_BUF(3, "client hello, random bytes",
1458 random, MBEDTLS_CLIENT_HELLO_RANDOM_LEN);
1459 memcpy(&handshake->randbytes[0], random, MBEDTLS_CLIENT_HELLO_RANDOM_LEN);
1460
Ronald Croncada4102023-03-07 09:51:39 +01001461 if (legacy_session_id_len > sizeof(ssl->session_negotiate->id)) {
1462 MBEDTLS_SSL_DEBUG_MSG(1, ("bad client hello message"));
1463 return MBEDTLS_ERR_SSL_DECODE_ERROR;
1464 }
1465 ssl->session_negotiate->id_len = legacy_session_id_len;
1466 MBEDTLS_SSL_DEBUG_BUF(3, "client hello, session id",
1467 legacy_session_id, legacy_session_id_len);
1468 memcpy(&ssl->session_negotiate->id[0],
1469 legacy_session_id, legacy_session_id_len);
1470
Ronald Crond540d992023-03-07 09:41:48 +01001471 /*
Jerry Yu5725f1c2022-08-21 17:27:16 +08001472 * Search for a matching ciphersuite
1473 */
Ronald Cron2f16b4e2023-03-07 10:07:32 +01001474 MBEDTLS_SSL_DEBUG_BUF(3, "client hello, list of cipher suites",
1475 cipher_suites, cipher_suites_len);
Ronald Crone45afd72023-04-04 15:10:06 +02001476 for (const unsigned char *cipher_suites_p = cipher_suites;
1477 cipher_suites_p < cipher_suites_end; cipher_suites_p += 2) {
XiaokangQiane8ff3502022-04-22 02:34:40 +00001478 uint16_t cipher_suite;
Gilles Peskine449bd832023-01-11 14:50:10 +01001479 const mbedtls_ssl_ciphersuite_t *ciphersuite_info;
Jerry Yu5725f1c2022-08-21 17:27:16 +08001480
Ronald Crond89360b2023-02-21 08:53:33 +01001481 /*
Ronald Crone45afd72023-04-04 15:10:06 +02001482 * "cipher_suites_end - cipher_suites_p is even" is an invariant of the
1483 * loop. As cipher_suites_end - cipher_suites_p > 0, we have
1484 * cipher_suites_end - cipher_suites_p >= 2 and it is thus safe to read
1485 * two bytes.
Ronald Crond89360b2023-02-21 08:53:33 +01001486 */
Ronald Crone45afd72023-04-04 15:10:06 +02001487 cipher_suite = MBEDTLS_GET_UINT16_BE(cipher_suites_p, 0);
Jerry Yuc5a23a02022-08-25 10:51:44 +08001488 ciphersuite_info = ssl_tls13_validate_peer_ciphersuite(
Gilles Peskine449bd832023-01-11 14:50:10 +01001489 ssl, cipher_suite);
1490 if (ciphersuite_info == NULL) {
Jerry Yu5725f1c2022-08-21 17:27:16 +08001491 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +01001492 }
Jerry Yu5725f1c2022-08-21 17:27:16 +08001493
Jerry Yu5725f1c2022-08-21 17:27:16 +08001494 ssl->session_negotiate->ciphersuite = cipher_suite;
Jerry Yuc4bf5d62022-10-29 09:08:47 +08001495 handshake->ciphersuite_info = ciphersuite_info;
Gilles Peskine449bd832023-01-11 14:50:10 +01001496 MBEDTLS_SSL_DEBUG_MSG(2, ("selected ciphersuite: %04x - %s",
1497 cipher_suite,
1498 ciphersuite_info->name));
Ronald Cron25e9ec62023-02-16 15:35:16 +01001499 break;
XiaokangQian17f974c2022-04-19 09:57:41 +00001500 }
Jerry Yu5725f1c2022-08-21 17:27:16 +08001501
Gilles Peskine449bd832023-01-11 14:50:10 +01001502 if (handshake->ciphersuite_info == NULL) {
1503 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE,
1504 MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE);
1505 return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
Jerry Yu5725f1c2022-08-21 17:27:16 +08001506 }
Jerry Yuc1be19f2022-04-23 16:11:39 +08001507
XiaokangQian4080a7f2022-04-11 09:55:18 +00001508 /* ...
XiaokangQianb67384d2022-04-19 00:02:38 +00001509 * opaque legacy_compression_methods<1..2^8-1>;
XiaokangQian4080a7f2022-04-11 09:55:18 +00001510 * ...
XiaokangQian7807f9f2022-02-15 10:04:37 +00001511 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001512 if (p[0] != 1 || p[1] != MBEDTLS_SSL_COMPRESS_NULL) {
1513 MBEDTLS_SSL_DEBUG_MSG(1, ("bad legacy compression method"));
1514 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER,
1515 MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER);
1516 return MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001517 }
XiaokangQianb67384d2022-04-19 00:02:38 +00001518 p += 2;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001519
Jerry Yuc1be19f2022-04-23 16:11:39 +08001520 /* ...
XiaokangQianb67384d2022-04-19 00:02:38 +00001521 * Extension extensions<8..2^16-1>;
Jerry Yuc1be19f2022-04-23 16:11:39 +08001522 * ...
XiaokangQianb67384d2022-04-19 00:02:38 +00001523 * with Extension defined as:
1524 * struct {
1525 * ExtensionType extension_type;
1526 * opaque extension_data<0..2^16-1>;
1527 * } Extension;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001528 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001529 extensions_len = MBEDTLS_GET_UINT16_BE(p, 0);
XiaokangQian7807f9f2022-02-15 10:04:37 +00001530 p += 2;
Gilles Peskine449bd832023-01-11 14:50:10 +01001531 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, extensions_len);
XiaokangQiane8ff3502022-04-22 02:34:40 +00001532 extensions_end = p + extensions_len;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001533
Gilles Peskine449bd832023-01-11 14:50:10 +01001534 MBEDTLS_SSL_DEBUG_BUF(3, "client hello extensions", p, extensions_len);
Jerry Yu63a459c2022-10-31 13:38:40 +08001535 handshake->received_extensions = MBEDTLS_SSL_EXT_MASK_NONE;
Jerry Yu0c354a22022-08-29 15:25:36 +08001536
Gilles Peskine449bd832023-01-11 14:50:10 +01001537 while (p < extensions_end) {
XiaokangQian7807f9f2022-02-15 10:04:37 +00001538 unsigned int extension_type;
1539 size_t extension_data_len;
1540 const unsigned char *extension_data_end;
1541
Jerry Yu0c354a22022-08-29 15:25:36 +08001542 /* RFC 8446, section 4.2.11
Jerry Yu1c9247c2022-07-21 12:37:39 +08001543 *
1544 * The "pre_shared_key" extension MUST be the last extension in the
1545 * ClientHello (this facilitates implementation as described below).
1546 * Servers MUST check that it is the last extension and otherwise fail
1547 * the handshake with an "illegal_parameter" alert.
1548 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001549 if (handshake->received_extensions & MBEDTLS_SSL_EXT_MASK(PRE_SHARED_KEY)) {
Jerry Yu1c9247c2022-07-21 12:37:39 +08001550 MBEDTLS_SSL_DEBUG_MSG(
Gilles Peskine449bd832023-01-11 14:50:10 +01001551 3, ("pre_shared_key is not last extension."));
Jerry Yu1c9247c2022-07-21 12:37:39 +08001552 MBEDTLS_SSL_PEND_FATAL_ALERT(
1553 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER,
Gilles Peskine449bd832023-01-11 14:50:10 +01001554 MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER);
1555 return MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER;
Jerry Yu1c9247c2022-07-21 12:37:39 +08001556 }
1557
Gilles Peskine449bd832023-01-11 14:50:10 +01001558 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, extensions_end, 4);
1559 extension_type = MBEDTLS_GET_UINT16_BE(p, 0);
1560 extension_data_len = MBEDTLS_GET_UINT16_BE(p, 2);
XiaokangQian7807f9f2022-02-15 10:04:37 +00001561 p += 4;
1562
Gilles Peskine449bd832023-01-11 14:50:10 +01001563 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, extensions_end, extension_data_len);
XiaokangQian7807f9f2022-02-15 10:04:37 +00001564 extension_data_end = p + extension_data_len;
1565
Jerry Yuc4bf5d62022-10-29 09:08:47 +08001566 ret = mbedtls_ssl_tls13_check_received_extension(
Gilles Peskine449bd832023-01-11 14:50:10 +01001567 ssl, MBEDTLS_SSL_HS_CLIENT_HELLO, extension_type,
1568 MBEDTLS_SSL_TLS1_3_ALLOWED_EXTS_OF_CH);
1569 if (ret != 0) {
1570 return ret;
1571 }
Jerry Yue18dc7e2022-08-04 16:29:22 +08001572
Gilles Peskine449bd832023-01-11 14:50:10 +01001573 switch (extension_type) {
XiaokangQian40a35232022-05-07 09:02:40 +00001574#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
1575 case MBEDTLS_TLS_EXT_SERVERNAME:
Gilles Peskine449bd832023-01-11 14:50:10 +01001576 MBEDTLS_SSL_DEBUG_MSG(3, ("found ServerName extension"));
1577 ret = mbedtls_ssl_parse_server_name_ext(ssl, p,
1578 extension_data_end);
1579 if (ret != 0) {
XiaokangQian40a35232022-05-07 09:02:40 +00001580 MBEDTLS_SSL_DEBUG_RET(
Gilles Peskine449bd832023-01-11 14:50:10 +01001581 1, "mbedtls_ssl_parse_servername_ext", ret);
1582 return ret;
XiaokangQian40a35232022-05-07 09:02:40 +00001583 }
XiaokangQian40a35232022-05-07 09:02:40 +00001584 break;
1585#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
1586
Przemek Stekiel8c0a9532023-06-15 16:48:19 +02001587#if defined(PSA_WANT_ALG_ECDH) || defined(PSA_WANT_ALG_FFDH)
XiaokangQian7807f9f2022-02-15 10:04:37 +00001588 case MBEDTLS_TLS_EXT_SUPPORTED_GROUPS:
Gilles Peskine449bd832023-01-11 14:50:10 +01001589 MBEDTLS_SSL_DEBUG_MSG(3, ("found supported group extension"));
XiaokangQian7807f9f2022-02-15 10:04:37 +00001590
1591 /* Supported Groups Extension
1592 *
1593 * When sent by the client, the "supported_groups" extension
1594 * indicates the named groups which the client supports,
1595 * ordered from most preferred to least preferred.
1596 */
Jerry Yuc1be19f2022-04-23 16:11:39 +08001597 ret = ssl_tls13_parse_supported_groups_ext(
Gilles Peskine449bd832023-01-11 14:50:10 +01001598 ssl, p, extension_data_end);
1599 if (ret != 0) {
Xiaokang Qian9f1747b2023-03-30 02:50:04 +00001600 MBEDTLS_SSL_DEBUG_RET(
Xiaokang Qian8bce0e62023-04-04 10:15:48 +00001601 1, "ssl_tls13_parse_supported_groups_ext", ret);
Gilles Peskine449bd832023-01-11 14:50:10 +01001602 return ret;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001603 }
1604
XiaokangQian7807f9f2022-02-15 10:04:37 +00001605 break;
Przemek Stekiel8c0a9532023-06-15 16:48:19 +02001606#endif /* PSA_WANT_ALG_ECDH || PSA_WANT_ALG_FFDH*/
XiaokangQian7807f9f2022-02-15 10:04:37 +00001607
Valerio Settic9ae8622023-07-25 11:23:50 +02001608#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_EPHEMERAL_ENABLED)
XiaokangQian7807f9f2022-02-15 10:04:37 +00001609 case MBEDTLS_TLS_EXT_KEY_SHARE:
Gilles Peskine449bd832023-01-11 14:50:10 +01001610 MBEDTLS_SSL_DEBUG_MSG(3, ("found key share extension"));
XiaokangQian7807f9f2022-02-15 10:04:37 +00001611
1612 /*
1613 * Key Share Extension
1614 *
1615 * When sent by the client, the "key_share" extension
1616 * contains the endpoint's cryptographic parameters for
1617 * ECDHE/DHE key establishment methods.
1618 */
Jerry Yuc1be19f2022-04-23 16:11:39 +08001619 ret = ssl_tls13_parse_key_shares_ext(
Gilles Peskine449bd832023-01-11 14:50:10 +01001620 ssl, p, extension_data_end);
1621 if (ret == SSL_TLS1_3_PARSE_KEY_SHARES_EXT_NO_MATCH) {
Ronald Cron8a74f072023-06-14 17:59:29 +02001622 MBEDTLS_SSL_DEBUG_MSG(2, ("No usable share for key agreement."));
1623 no_usable_share_for_key_agreement = 1;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001624 }
1625
Gilles Peskine449bd832023-01-11 14:50:10 +01001626 if (ret < 0) {
Jerry Yu582dd062022-04-22 21:59:01 +08001627 MBEDTLS_SSL_DEBUG_RET(
Gilles Peskine449bd832023-01-11 14:50:10 +01001628 1, "ssl_tls13_parse_key_shares_ext", ret);
1629 return ret;
Jerry Yu582dd062022-04-22 21:59:01 +08001630 }
XiaokangQian7807f9f2022-02-15 10:04:37 +00001631
XiaokangQian7807f9f2022-02-15 10:04:37 +00001632 break;
Valerio Settic9ae8622023-07-25 11:23:50 +02001633#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_EPHEMERAL_ENABLED */
XiaokangQian7807f9f2022-02-15 10:04:37 +00001634
1635 case MBEDTLS_TLS_EXT_SUPPORTED_VERSIONS:
Ronald Cron8c527d02023-03-07 15:47:47 +01001636 /* Already parsed */
XiaokangQian7807f9f2022-02-15 10:04:37 +00001637 break;
1638
Ronald Cron41a443a2022-10-04 16:38:25 +02001639#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED)
Jerry Yue19e3b92022-07-08 12:04:51 +00001640 case MBEDTLS_TLS_EXT_PSK_KEY_EXCHANGE_MODES:
Xiaokang Qian9f1747b2023-03-30 02:50:04 +00001641 MBEDTLS_SSL_DEBUG_MSG(
1642 3, ("found psk key exchange modes extension"));
Jerry Yue19e3b92022-07-08 12:04:51 +00001643
1644 ret = ssl_tls13_parse_key_exchange_modes_ext(
Gilles Peskine449bd832023-01-11 14:50:10 +01001645 ssl, p, extension_data_end);
1646 if (ret != 0) {
Jerry Yue19e3b92022-07-08 12:04:51 +00001647 MBEDTLS_SSL_DEBUG_RET(
Gilles Peskine449bd832023-01-11 14:50:10 +01001648 1, "ssl_tls13_parse_key_exchange_modes_ext", ret);
1649 return ret;
Jerry Yue19e3b92022-07-08 12:04:51 +00001650 }
1651
Jerry Yue19e3b92022-07-08 12:04:51 +00001652 break;
Ronald Cron41a443a2022-10-04 16:38:25 +02001653#endif
Jerry Yue19e3b92022-07-08 12:04:51 +00001654
Jerry Yu1c105562022-07-10 06:32:38 +00001655 case MBEDTLS_TLS_EXT_PRE_SHARED_KEY:
Gilles Peskine449bd832023-01-11 14:50:10 +01001656 MBEDTLS_SSL_DEBUG_MSG(3, ("found pre_shared_key extension"));
1657 if ((handshake->received_extensions &
1658 MBEDTLS_SSL_EXT_MASK(PSK_KEY_EXCHANGE_MODES)) == 0) {
Jerry Yu13ab81d2022-07-22 23:17:11 +08001659 MBEDTLS_SSL_PEND_FATAL_ALERT(
1660 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER,
Gilles Peskine449bd832023-01-11 14:50:10 +01001661 MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER);
1662 return MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER;
Jerry Yu13ab81d2022-07-22 23:17:11 +08001663 }
Ronald Cron41a443a2022-10-04 16:38:25 +02001664#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED)
Jerry Yu1c105562022-07-10 06:32:38 +00001665 /* Delay processing of the PSK identity once we have
1666 * found out which algorithms to use. We keep a pointer
1667 * to the buffer and the size for later processing.
1668 */
Jerry Yu29d9faa2022-08-23 17:52:45 +08001669 pre_shared_key_ext = p;
Jerry Yu1c105562022-07-10 06:32:38 +00001670 pre_shared_key_ext_end = extension_data_end;
Jerry Yue18dc7e2022-08-04 16:29:22 +08001671#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED */
Jerry Yu1c105562022-07-10 06:32:38 +00001672 break;
Jerry Yu1c105562022-07-10 06:32:38 +00001673
XiaokangQianacb39922022-06-17 10:18:48 +00001674#if defined(MBEDTLS_SSL_ALPN)
1675 case MBEDTLS_TLS_EXT_ALPN:
Gilles Peskine449bd832023-01-11 14:50:10 +01001676 MBEDTLS_SSL_DEBUG_MSG(3, ("found alpn extension"));
XiaokangQianacb39922022-06-17 10:18:48 +00001677
Gilles Peskine449bd832023-01-11 14:50:10 +01001678 ret = mbedtls_ssl_parse_alpn_ext(ssl, p, extension_data_end);
1679 if (ret != 0) {
XiaokangQianacb39922022-06-17 10:18:48 +00001680 MBEDTLS_SSL_DEBUG_RET(
Gilles Peskine449bd832023-01-11 14:50:10 +01001681 1, ("mbedtls_ssl_parse_alpn_ext"), ret);
1682 return ret;
XiaokangQianacb39922022-06-17 10:18:48 +00001683 }
XiaokangQianacb39922022-06-17 10:18:48 +00001684 break;
1685#endif /* MBEDTLS_SSL_ALPN */
1686
Ronald Cron928cbd32022-10-04 16:14:26 +02001687#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED)
XiaokangQian7807f9f2022-02-15 10:04:37 +00001688 case MBEDTLS_TLS_EXT_SIG_ALG:
Gilles Peskine449bd832023-01-11 14:50:10 +01001689 MBEDTLS_SSL_DEBUG_MSG(3, ("found signature_algorithms extension"));
XiaokangQian7807f9f2022-02-15 10:04:37 +00001690
Gabor Mezei078e8032022-04-27 21:17:56 +02001691 ret = mbedtls_ssl_parse_sig_alg_ext(
Gilles Peskine449bd832023-01-11 14:50:10 +01001692 ssl, p, extension_data_end);
1693 if (ret != 0) {
Xiaokang Qian49f39c12023-04-06 02:31:02 +00001694 MBEDTLS_SSL_DEBUG_RET(
1695 1, "mbedtls_ssl_parse_sig_alg_ext", ret);
Gilles Peskine449bd832023-01-11 14:50:10 +01001696 return ret;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001697 }
XiaokangQian7807f9f2022-02-15 10:04:37 +00001698 break;
Ronald Cron928cbd32022-10-04 16:14:26 +02001699#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED */
XiaokangQian7807f9f2022-02-15 10:04:37 +00001700
Jan Bruckner151f6422023-02-10 12:45:19 +01001701#if defined(MBEDTLS_SSL_RECORD_SIZE_LIMIT)
1702 case MBEDTLS_TLS_EXT_RECORD_SIZE_LIMIT:
1703 MBEDTLS_SSL_DEBUG_MSG(3, ("found record_size_limit extension"));
1704
Xiaokang Qian9f1747b2023-03-30 02:50:04 +00001705 ret = mbedtls_ssl_tls13_parse_record_size_limit_ext(
1706 ssl, p, extension_data_end);
Jan Bruckner151f6422023-02-10 12:45:19 +01001707
Xiaokang Qian09c3ccc2023-04-04 10:18:38 +00001708 /*
1709 * TODO: Return unconditionally here until we handle the record
1710 * size limit correctly.
1711 * Once handled correctly, only return in case of errors.
1712 */
Jan Bruckner151f6422023-02-10 12:45:19 +01001713 return ret;
1714
1715 break;
1716#endif /* MBEDTLS_SSL_RECORD_SIZE_LIMIT */
1717
XiaokangQian7807f9f2022-02-15 10:04:37 +00001718 default:
Jerry Yu79aa7212022-11-08 21:30:21 +08001719 MBEDTLS_SSL_PRINT_EXT(
Jerry Yu63a459c2022-10-31 13:38:40 +08001720 3, MBEDTLS_SSL_HS_CLIENT_HELLO,
Gilles Peskine449bd832023-01-11 14:50:10 +01001721 extension_type, "( ignored )");
Jerry Yu0c354a22022-08-29 15:25:36 +08001722 break;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001723 }
1724
1725 p += extension_data_len;
1726 }
1727
Gilles Peskine449bd832023-01-11 14:50:10 +01001728 MBEDTLS_SSL_PRINT_EXTS(3, MBEDTLS_SSL_HS_CLIENT_HELLO,
1729 handshake->received_extensions);
Jerry Yue18dc7e2022-08-04 16:29:22 +08001730
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +01001731 ret = mbedtls_ssl_add_hs_hdr_to_checksum(ssl,
1732 MBEDTLS_SSL_HS_CLIENT_HELLO,
1733 p - buf);
1734 if (0 != ret) {
1735 MBEDTLS_SSL_DEBUG_RET(1, ("mbedtls_ssl_add_hs_hdr_to_checksum"), ret);
1736 return ret;
1737 }
Jerry Yu032b15ce2022-07-11 06:10:03 +00001738
Ronald Cron41a443a2022-10-04 16:38:25 +02001739#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED)
XiaokangQian7807f9f2022-02-15 10:04:37 +00001740 /* Update checksum with either
1741 * - The entire content of the CH message, if no PSK extension is present
1742 * - The content up to but excluding the PSK extension, if present.
1743 */
Jerry Yu1c105562022-07-10 06:32:38 +00001744 /* If we've settled on a PSK-based exchange, parse PSK identity ext */
Pengyu Lvcfb23b82023-10-30 15:26:26 +08001745 if (ssl_tls13_check_psk_key_exchange(ssl) ||
1746 ssl_tls13_check_psk_ephemeral_key_exchange(ssl)) {
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +01001747 ret = handshake->update_checksum(ssl, buf,
1748 pre_shared_key_ext - buf);
1749 if (0 != ret) {
1750 MBEDTLS_SSL_DEBUG_RET(1, ("update_checksum"), ret);
1751 return ret;
1752 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001753 ret = ssl_tls13_parse_pre_shared_key_ext(ssl,
1754 pre_shared_key_ext,
1755 pre_shared_key_ext_end,
1756 cipher_suites,
1757 cipher_suites_end);
1758 if (ret == MBEDTLS_ERR_SSL_UNKNOWN_IDENTITY) {
1759 handshake->received_extensions &= ~MBEDTLS_SSL_EXT_MASK(PRE_SHARED_KEY);
1760 } else if (ret != 0) {
Jerry Yu63a459c2022-10-31 13:38:40 +08001761 MBEDTLS_SSL_DEBUG_RET(
Gilles Peskine449bd832023-01-11 14:50:10 +01001762 1, "ssl_tls13_parse_pre_shared_key_ext", ret);
1763 return ret;
Jerry Yu1c105562022-07-10 06:32:38 +00001764 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001765 } else
Ronald Cron41a443a2022-10-04 16:38:25 +02001766#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED */
Jerry Yu1c105562022-07-10 06:32:38 +00001767 {
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +01001768 ret = handshake->update_checksum(ssl, buf, p - buf);
1769 if (0 != ret) {
1770 MBEDTLS_SSL_DEBUG_RET(1, ("update_checksum"), ret);
1771 return ret;
1772 }
Jerry Yu1c105562022-07-10 06:32:38 +00001773 }
XiaokangQian7807f9f2022-02-15 10:04:37 +00001774
Gilles Peskine449bd832023-01-11 14:50:10 +01001775 ret = ssl_tls13_determine_key_exchange_mode(ssl);
1776 if (ret < 0) {
1777 return ret;
1778 }
XiaokangQian7807f9f2022-02-15 10:04:37 +00001779
Ronald Cron8a74f072023-06-14 17:59:29 +02001780 if (ssl->handshake->key_exchange_mode !=
1781 MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK) {
1782 hrr_required = (no_usable_share_for_key_agreement != 0);
1783 }
1784
Gilles Peskine449bd832023-01-11 14:50:10 +01001785 mbedtls_ssl_optimize_checksum(ssl, handshake->ciphersuite_info);
Jerry Yue5834fd2022-08-29 20:16:09 +08001786
Gilles Peskine449bd832023-01-11 14:50:10 +01001787 return hrr_required ? SSL_CLIENT_HELLO_HRR_REQUIRED : SSL_CLIENT_HELLO_OK;
XiaokangQian75fe8c72022-06-15 09:42:45 +00001788}
1789
Jerry Yuab0da372023-02-08 13:55:24 +08001790#if defined(MBEDTLS_SSL_EARLY_DATA)
1791static void ssl_tls13_update_early_data_status(mbedtls_ssl_context *ssl)
1792{
1793 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
1794
1795 if ((handshake->received_extensions &
1796 MBEDTLS_SSL_EXT_MASK(EARLY_DATA)) == 0) {
1797 MBEDTLS_SSL_DEBUG_MSG(
Jerry Yub47b2992023-10-18 15:50:35 +08001798 1, ("EarlyData: no early data extension received."));
Jerry Yuab0da372023-02-08 13:55:24 +08001799 ssl->early_data_status = MBEDTLS_SSL_EARLY_DATA_STATUS_NOT_RECEIVED;
1800 return;
1801 }
1802
1803 ssl->early_data_status = MBEDTLS_SSL_EARLY_DATA_STATUS_REJECTED;
1804
Jerry Yu985c9672022-12-04 14:06:30 +08001805 if (ssl->conf->early_data_enabled == MBEDTLS_SSL_EARLY_DATA_DISABLED) {
1806 MBEDTLS_SSL_DEBUG_MSG(
Jerry Yu985c9672022-12-04 14:06:30 +08001807 1,
Jerry Yu454dda32023-10-31 15:13:54 +08001808 ("EarlyData: rejected, feature disabled in server configuration."));
Jerry Yu985c9672022-12-04 14:06:30 +08001809 return;
1810 }
1811
Jerry Yu454dda32023-10-31 15:13:54 +08001812 if (!handshake->resume) {
1813 /* We currently support early data only in the case of PSKs established
1814 via a NewSessionTicket message thus in the case of a session
1815 resumption. */
Jerry Yu985c9672022-12-04 14:06:30 +08001816 MBEDTLS_SSL_DEBUG_MSG(
Jerry Yu7ef9fd82023-11-07 14:30:38 +08001817 1, ("EarlyData: rejected, not a session resumption."));
Jerry Yu985c9672022-12-04 14:06:30 +08001818 return;
1819 }
1820
Jerry Yu82fd6c12023-10-31 16:32:19 +08001821 /* RFC 8446 4.2.10
1822 *
1823 * In order to accept early data, the server MUST have accepted a PSK cipher
1824 * suite and selected the first key offered in the client's "pre_shared_key"
1825 * extension. In addition, it MUST verify that the following values are the
1826 * same as those associated with the selected PSK:
1827 * - The TLS version number
1828 * - The selected cipher suite
1829 * - The selected ALPN [RFC7301] protocol, if any
1830 *
1831 * NOTE:
Jerry Yu7ef9fd82023-11-07 14:30:38 +08001832 * - The TLS version number is checked in
1833 * ssl_tls13_offered_psks_check_identity_match_ticket().
1834 * - ALPN is not checked for the time being (TODO).
Jerry Yu82fd6c12023-10-31 16:32:19 +08001835 */
1836
1837 if (handshake->selected_identity != 0) {
1838 MBEDTLS_SSL_DEBUG_MSG(
Jerry Yu7ef9fd82023-11-07 14:30:38 +08001839 1, ("EarlyData: rejected, the selected key in "
1840 "`pre_shared_key` is not the first one."));
Jerry Yu82fd6c12023-10-31 16:32:19 +08001841 return;
1842 }
1843
1844 if (handshake->ciphersuite_info->id !=
1845 ssl->session_negotiate->ciphersuite) {
1846 MBEDTLS_SSL_DEBUG_MSG(
Jerry Yu7ef9fd82023-11-07 14:30:38 +08001847 1, ("EarlyData: rejected, the selected ciphersuite is not the one "
1848 "of the selected pre-shared key."));
Jerry Yu82fd6c12023-10-31 16:32:19 +08001849 return;
1850
1851 }
Jerry Yu985c9672022-12-04 14:06:30 +08001852
Jerry Yuc2b1bc42023-11-22 10:08:13 +08001853 if (!mbedtls_ssl_session_ticket_allow_early_data(ssl->session_negotiate)) {
Jerry Yufceddb32022-12-12 15:30:34 +08001854 MBEDTLS_SSL_DEBUG_MSG(
1855 1,
Jerry Yuea96ac32023-11-21 17:06:36 +08001856 ("EarlyData: rejected, early_data not allowed in ticket "
1857 "permission bits."));
Jerry Yufceddb32022-12-12 15:30:34 +08001858 return;
1859 }
Jerry Yu985c9672022-12-04 14:06:30 +08001860
1861 ssl->early_data_status = MBEDTLS_SSL_EARLY_DATA_STATUS_ACCEPTED;
1862
Jerry Yuab0da372023-02-08 13:55:24 +08001863}
1864#endif /* MBEDTLS_SSL_EARLY_DATA */
1865
XiaokangQian75fe8c72022-06-15 09:42:45 +00001866/* Update the handshake state machine */
1867
Ronald Cronce7d76e2022-07-08 18:56:49 +02001868MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01001869static int ssl_tls13_postprocess_client_hello(mbedtls_ssl_context *ssl)
XiaokangQian75fe8c72022-06-15 09:42:45 +00001870{
1871 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1872
XiaokangQian7807f9f2022-02-15 10:04:37 +00001873 /*
XiaokangQianfb665a82022-06-15 03:57:21 +00001874 * Server certificate selection
1875 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001876 if (ssl->conf->f_cert_cb && (ret = ssl->conf->f_cert_cb(ssl)) != 0) {
1877 MBEDTLS_SSL_DEBUG_RET(1, "f_cert_cb", ret);
1878 return ret;
XiaokangQianfb665a82022-06-15 03:57:21 +00001879 }
1880#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
1881 ssl->handshake->sni_name = NULL;
1882 ssl->handshake->sni_name_len = 0;
1883#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
XiaokangQian7807f9f2022-02-15 10:04:37 +00001884
Gilles Peskine449bd832023-01-11 14:50:10 +01001885 ret = mbedtls_ssl_tls13_key_schedule_stage_early(ssl);
1886 if (ret != 0) {
1887 MBEDTLS_SSL_DEBUG_RET(1,
1888 "mbedtls_ssl_tls1_3_key_schedule_stage_early", ret);
1889 return ret;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001890 }
1891
Jerry Yuab0da372023-02-08 13:55:24 +08001892#if defined(MBEDTLS_SSL_EARLY_DATA)
1893 /* There is enough information, update early data state. */
1894 ssl_tls13_update_early_data_status(ssl);
Jerry Yu4e9b70e2022-12-04 14:08:02 +08001895
1896 if (ssl->early_data_status == MBEDTLS_SSL_EARLY_DATA_STATUS_ACCEPTED) {
1897 ret = mbedtls_ssl_tls13_compute_early_transform(ssl);
1898 if (ret != 0) {
1899 MBEDTLS_SSL_DEBUG_RET(
1900 1, "mbedtls_ssl_tls13_compute_early_transform", ret);
1901 return ret;
1902 }
1903 }
Jerry Yuab0da372023-02-08 13:55:24 +08001904#endif /* MBEDTLS_SSL_EARLY_DATA */
1905
Gilles Peskine449bd832023-01-11 14:50:10 +01001906 return 0;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001907}
1908
1909/*
XiaokangQianed582dd2022-04-13 08:21:05 +00001910 * Main entry point from the state machine; orchestrates the otherfunctions.
1911 */
1912
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001913MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01001914static int ssl_tls13_process_client_hello(mbedtls_ssl_context *ssl)
XiaokangQianed582dd2022-04-13 08:21:05 +00001915{
1916
XiaokangQian08037552022-04-20 07:16:41 +00001917 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine449bd832023-01-11 14:50:10 +01001918 unsigned char *buf = NULL;
XiaokangQianed582dd2022-04-13 08:21:05 +00001919 size_t buflen = 0;
Jerry Yu4ca91402022-05-09 15:50:57 +08001920 int parse_client_hello_ret;
1921
Gilles Peskine449bd832023-01-11 14:50:10 +01001922 MBEDTLS_SSL_DEBUG_MSG(2, ("=> parse client hello"));
XiaokangQianed582dd2022-04-13 08:21:05 +00001923
Gilles Peskine449bd832023-01-11 14:50:10 +01001924 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_tls13_fetch_handshake_msg(
1925 ssl, MBEDTLS_SSL_HS_CLIENT_HELLO,
1926 &buf, &buflen));
XiaokangQianed582dd2022-04-13 08:21:05 +00001927
Gilles Peskine449bd832023-01-11 14:50:10 +01001928 MBEDTLS_SSL_PROC_CHK_NEG(ssl_tls13_parse_client_hello(ssl, buf,
1929 buf + buflen));
Ronald Cron5af4c7f2023-03-07 20:46:59 +01001930 parse_client_hello_ret = ret; /* Store positive return value of
1931 * parse_client_hello,
1932 * as negative error codes are handled
Jerry Yuf41553b2022-05-09 22:20:30 +08001933 * by MBEDTLS_SSL_PROC_CHK_NEG. */
Jerry Yu4ca91402022-05-09 15:50:57 +08001934
Ronald Cronb828c7d2023-04-03 16:37:22 +02001935 /*
1936 * Version 1.2 of the protocol has been chosen, set the
1937 * ssl->keep_current_message flag for the ClientHello to be kept and parsed
1938 * as a TLS 1.2 ClientHello. We also change ssl->tls_version to
1939 * MBEDTLS_SSL_VERSION_TLS1_2 thus from now on mbedtls_ssl_handshake_step()
1940 * will dispatch to the TLS 1.2 state machine.
1941 */
Ronald Cron5af4c7f2023-03-07 20:46:59 +01001942 if (SSL_CLIENT_HELLO_TLS1_2 == parse_client_hello_ret) {
1943 ssl->keep_current_message = 1;
1944 ssl->tls_version = MBEDTLS_SSL_VERSION_TLS1_2;
1945 return 0;
1946 }
1947
Gilles Peskine449bd832023-01-11 14:50:10 +01001948 MBEDTLS_SSL_PROC_CHK(ssl_tls13_postprocess_client_hello(ssl));
Jerry Yu582dd062022-04-22 21:59:01 +08001949
Ronald Cron5af4c7f2023-03-07 20:46:59 +01001950 if (SSL_CLIENT_HELLO_OK == parse_client_hello_ret) {
Gilles Peskine449bd832023-01-11 14:50:10 +01001951 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_SERVER_HELLO);
1952 } else {
1953 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_HELLO_RETRY_REQUEST);
1954 }
XiaokangQianed582dd2022-04-13 08:21:05 +00001955
1956cleanup:
1957
Gilles Peskine449bd832023-01-11 14:50:10 +01001958 MBEDTLS_SSL_DEBUG_MSG(2, ("<= parse client hello"));
1959 return ret;
XiaokangQianed582dd2022-04-13 08:21:05 +00001960}
1961
1962/*
Jerry Yu1c3e6882022-04-20 21:23:40 +08001963 * Handler for MBEDTLS_SSL_SERVER_HELLO
Jerry Yu5b64ae92022-03-30 17:15:02 +08001964 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001965MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01001966static int ssl_tls13_prepare_server_hello(mbedtls_ssl_context *ssl)
Jerry Yuf4b27e42022-03-30 17:32:21 +08001967{
Jerry Yu637a3f12022-04-20 21:37:58 +08001968 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1969 unsigned char *server_randbytes =
Gilles Peskine449bd832023-01-11 14:50:10 +01001970 ssl->handshake->randbytes + MBEDTLS_CLIENT_HELLO_RANDOM_LEN;
1971 if (ssl->conf->f_rng == NULL) {
1972 MBEDTLS_SSL_DEBUG_MSG(1, ("no RNG provided"));
1973 return MBEDTLS_ERR_SSL_NO_RNG;
Jerry Yuf4b27e42022-03-30 17:32:21 +08001974 }
1975
Gilles Peskine449bd832023-01-11 14:50:10 +01001976 if ((ret = ssl->conf->f_rng(ssl->conf->p_rng, server_randbytes,
1977 MBEDTLS_SERVER_HELLO_RANDOM_LEN)) != 0) {
1978 MBEDTLS_SSL_DEBUG_RET(1, "f_rng", ret);
1979 return ret;
Jerry Yuf4b27e42022-03-30 17:32:21 +08001980 }
1981
Gilles Peskine449bd832023-01-11 14:50:10 +01001982 MBEDTLS_SSL_DEBUG_BUF(3, "server hello, random bytes", server_randbytes,
1983 MBEDTLS_SERVER_HELLO_RANDOM_LEN);
Jerry Yuf4b27e42022-03-30 17:32:21 +08001984
1985#if defined(MBEDTLS_HAVE_TIME)
YxCda609132023-05-22 12:08:12 -07001986 ssl->session_negotiate->start = mbedtls_time(NULL);
Jerry Yuf4b27e42022-03-30 17:32:21 +08001987#endif /* MBEDTLS_HAVE_TIME */
1988
Gilles Peskine449bd832023-01-11 14:50:10 +01001989 return ret;
Jerry Yuf4b27e42022-03-30 17:32:21 +08001990}
1991
Jerry Yu3bf2c642022-03-30 22:02:12 +08001992/*
Jerry Yue74e04a2022-04-21 09:23:16 +08001993 * ssl_tls13_write_server_hello_supported_versions_ext ():
Jerry Yu3bf2c642022-03-30 22:02:12 +08001994 *
1995 * struct {
Jerry Yufb9f54d2022-04-06 10:08:34 +08001996 * ProtocolVersion selected_version;
Jerry Yu3bf2c642022-03-30 22:02:12 +08001997 * } SupportedVersions;
1998 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001999MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yue74e04a2022-04-21 09:23:16 +08002000static int ssl_tls13_write_server_hello_supported_versions_ext(
Gilles Peskine449bd832023-01-11 14:50:10 +01002001 mbedtls_ssl_context *ssl,
2002 unsigned char *buf,
2003 unsigned char *end,
2004 size_t *out_len)
Jerry Yu3bf2c642022-03-30 22:02:12 +08002005{
Jerry Yu3bf2c642022-03-30 22:02:12 +08002006 *out_len = 0;
2007
Gilles Peskine449bd832023-01-11 14:50:10 +01002008 MBEDTLS_SSL_DEBUG_MSG(3, ("server hello, write selected version"));
Jerry Yu3bf2c642022-03-30 22:02:12 +08002009
2010 /* Check if we have space to write the extension:
2011 * - extension_type (2 bytes)
2012 * - extension_data_length (2 bytes)
Jerry Yu349a6132022-04-14 20:52:56 +08002013 * - selected_version (2 bytes)
Jerry Yu3bf2c642022-03-30 22:02:12 +08002014 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002015 MBEDTLS_SSL_CHK_BUF_PTR(buf, end, 6);
Jerry Yu3bf2c642022-03-30 22:02:12 +08002016
Gilles Peskine449bd832023-01-11 14:50:10 +01002017 MBEDTLS_PUT_UINT16_BE(MBEDTLS_TLS_EXT_SUPPORTED_VERSIONS, buf, 0);
Jerry Yu3bf2c642022-03-30 22:02:12 +08002018
Gilles Peskine449bd832023-01-11 14:50:10 +01002019 MBEDTLS_PUT_UINT16_BE(2, buf, 2);
Jerry Yu3bf2c642022-03-30 22:02:12 +08002020
Gilles Peskine449bd832023-01-11 14:50:10 +01002021 mbedtls_ssl_write_version(buf + 4,
2022 ssl->conf->transport,
2023 ssl->tls_version);
Jerry Yu3bf2c642022-03-30 22:02:12 +08002024
Gilles Peskine449bd832023-01-11 14:50:10 +01002025 MBEDTLS_SSL_DEBUG_MSG(3, ("supported version: [%04x]",
2026 ssl->tls_version));
Jerry Yu3bf2c642022-03-30 22:02:12 +08002027
Jerry Yu349a6132022-04-14 20:52:56 +08002028 *out_len = 6;
Jerry Yu3bf2c642022-03-30 22:02:12 +08002029
Jerry Yub95dd362022-11-08 21:19:34 +08002030 mbedtls_ssl_tls13_set_hs_sent_ext_mask(
Gilles Peskine449bd832023-01-11 14:50:10 +01002031 ssl, MBEDTLS_TLS_EXT_SUPPORTED_VERSIONS);
Jerry Yub95dd362022-11-08 21:19:34 +08002032
Gilles Peskine449bd832023-01-11 14:50:10 +01002033 return 0;
Jerry Yu3bf2c642022-03-30 22:02:12 +08002034}
2035
Jerry Yud9436a12022-04-20 22:28:09 +08002036
Jerry Yu3bf2c642022-03-30 22:02:12 +08002037
2038/* Generate and export a single key share. For hybrid KEMs, this can
2039 * be called multiple times with the different components of the hybrid. */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002040MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002041static int ssl_tls13_generate_and_write_key_share(mbedtls_ssl_context *ssl,
2042 uint16_t named_group,
2043 unsigned char *buf,
2044 unsigned char *end,
2045 size_t *out_len)
Jerry Yu3bf2c642022-03-30 22:02:12 +08002046{
2047 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yu955ddd72022-04-22 22:27:33 +08002048
2049 *out_len = 0;
2050
Valerio Settic9ae8622023-07-25 11:23:50 +02002051#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_EPHEMERAL_ENABLED)
Przemek Stekiel29c219c2023-05-31 15:21:04 +02002052 if (mbedtls_ssl_tls13_named_group_is_ecdhe(named_group) ||
Przemek Stekield5f79e72023-06-29 09:08:43 +02002053 mbedtls_ssl_tls13_named_group_is_ffdh(named_group)) {
Przemek Stekiel408569f2023-07-06 11:26:44 +02002054 ret = mbedtls_ssl_tls13_generate_and_write_xxdh_key_exchange(
Gilles Peskine449bd832023-01-11 14:50:10 +01002055 ssl, named_group, buf, end, out_len);
2056 if (ret != 0) {
Jerry Yu89e103c2022-03-30 22:43:29 +08002057 MBEDTLS_SSL_DEBUG_RET(
Przemek Stekiel408569f2023-07-06 11:26:44 +02002058 1, "mbedtls_ssl_tls13_generate_and_write_xxdh_key_exchange",
Gilles Peskine449bd832023-01-11 14:50:10 +01002059 ret);
2060 return ret;
Jerry Yu3bf2c642022-03-30 22:02:12 +08002061 }
Gilles Peskine449bd832023-01-11 14:50:10 +01002062 } else
Valerio Settic9ae8622023-07-25 11:23:50 +02002063#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_EPHEMERAL_ENABLED */
Gilles Peskine449bd832023-01-11 14:50:10 +01002064 if (0 /* Other kinds of KEMs */) {
2065 } else {
Jerry Yu955ddd72022-04-22 22:27:33 +08002066 ((void) ssl);
2067 ((void) named_group);
2068 ((void) buf);
2069 ((void) end);
Jerry Yu3bf2c642022-03-30 22:02:12 +08002070 ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
2071 }
2072
Gilles Peskine449bd832023-01-11 14:50:10 +01002073 return ret;
Jerry Yu3bf2c642022-03-30 22:02:12 +08002074}
2075
2076/*
2077 * ssl_tls13_write_key_share_ext
2078 *
2079 * Structure of key_share extension in ServerHello:
2080 *
Jerry Yu1c3e6882022-04-20 21:23:40 +08002081 * struct {
2082 * NamedGroup group;
2083 * opaque key_exchange<1..2^16-1>;
2084 * } KeyShareEntry;
2085 * struct {
2086 * KeyShareEntry server_share;
2087 * } KeyShareServerHello;
Jerry Yu3bf2c642022-03-30 22:02:12 +08002088 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002089MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002090static int ssl_tls13_write_key_share_ext(mbedtls_ssl_context *ssl,
2091 unsigned char *buf,
2092 unsigned char *end,
2093 size_t *out_len)
Jerry Yu3bf2c642022-03-30 22:02:12 +08002094{
Jerry Yu955ddd72022-04-22 22:27:33 +08002095 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yu3bf2c642022-03-30 22:02:12 +08002096 unsigned char *p = buf;
Jerry Yu57d48412022-04-20 21:50:42 +08002097 uint16_t group = ssl->handshake->offered_group_id;
Jerry Yu3bf2c642022-03-30 22:02:12 +08002098 unsigned char *server_share = buf + 4;
Jerry Yu3bf2c642022-03-30 22:02:12 +08002099 size_t key_exchange_length;
Jerry Yu3bf2c642022-03-30 22:02:12 +08002100
2101 *out_len = 0;
2102
Gilles Peskine449bd832023-01-11 14:50:10 +01002103 MBEDTLS_SSL_DEBUG_MSG(3, ("server hello, adding key share extension"));
Jerry Yu3bf2c642022-03-30 22:02:12 +08002104
Gilles Peskine449bd832023-01-11 14:50:10 +01002105 MBEDTLS_SSL_DEBUG_MSG(2, ("server hello, write selected_group: %s (%04x)",
2106 mbedtls_ssl_named_group_to_str(group),
2107 group));
Jerry Yu58af2332022-09-06 11:19:31 +08002108
Jerry Yu3bf2c642022-03-30 22:02:12 +08002109 /* Check if we have space for header and length fields:
2110 * - extension_type (2 bytes)
2111 * - extension_data_length (2 bytes)
2112 * - group (2 bytes)
2113 * - key_exchange_length (2 bytes)
2114 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002115 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 8);
2116 MBEDTLS_PUT_UINT16_BE(MBEDTLS_TLS_EXT_KEY_SHARE, p, 0);
2117 MBEDTLS_PUT_UINT16_BE(group, server_share, 0);
Jerry Yu3bf2c642022-03-30 22:02:12 +08002118 p += 8;
Jerry Yu57d48412022-04-20 21:50:42 +08002119
Jerry Yu3bf2c642022-03-30 22:02:12 +08002120 /* When we introduce PQC-ECDHE hybrids, we'll want to call this
2121 * function multiple times. */
Jerry Yu955ddd72022-04-22 22:27:33 +08002122 ret = ssl_tls13_generate_and_write_key_share(
Gilles Peskine449bd832023-01-11 14:50:10 +01002123 ssl, group, server_share + 4, end, &key_exchange_length);
2124 if (ret != 0) {
2125 return ret;
2126 }
Jerry Yu3bf2c642022-03-30 22:02:12 +08002127 p += key_exchange_length;
Jerry Yuc1be19f2022-04-23 16:11:39 +08002128
Gilles Peskine449bd832023-01-11 14:50:10 +01002129 MBEDTLS_PUT_UINT16_BE(key_exchange_length, server_share + 2, 0);
Jerry Yu3bf2c642022-03-30 22:02:12 +08002130
Gilles Peskine449bd832023-01-11 14:50:10 +01002131 MBEDTLS_PUT_UINT16_BE(p - server_share, buf, 2);
Jerry Yu3bf2c642022-03-30 22:02:12 +08002132
Jerry Yu57d48412022-04-20 21:50:42 +08002133 *out_len = p - buf;
Jerry Yu955ddd72022-04-22 22:27:33 +08002134
Gilles Peskine449bd832023-01-11 14:50:10 +01002135 mbedtls_ssl_tls13_set_hs_sent_ext_mask(ssl, MBEDTLS_TLS_EXT_KEY_SHARE);
Jerry Yub95dd362022-11-08 21:19:34 +08002136
Gilles Peskine449bd832023-01-11 14:50:10 +01002137 return 0;
Jerry Yu3bf2c642022-03-30 22:02:12 +08002138}
Jerry Yud9436a12022-04-20 22:28:09 +08002139
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002140MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002141static int ssl_tls13_write_hrr_key_share_ext(mbedtls_ssl_context *ssl,
2142 unsigned char *buf,
2143 unsigned char *end,
2144 size_t *out_len)
Jerry Yu23f7a6f2022-04-23 15:16:45 +08002145{
2146 uint16_t selected_group = ssl->handshake->hrr_selected_group;
2147 /* key_share Extension
2148 *
2149 * struct {
2150 * select (Handshake.msg_type) {
2151 * ...
2152 * case hello_retry_request:
2153 * NamedGroup selected_group;
2154 * ...
2155 * };
2156 * } KeyShare;
2157 */
2158
2159 *out_len = 0;
2160
Jerry Yub0ac10b2022-05-05 11:10:08 +08002161 /*
2162 * For a pure PSK key exchange, there is no group to agree upon. The purpose
2163 * of the HRR is then to transmit a cookie to force the client to demonstrate
2164 * reachability at their apparent network address (primarily useful for DTLS).
2165 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002166 if (!mbedtls_ssl_tls13_key_exchange_mode_with_ephemeral(ssl)) {
2167 return 0;
2168 }
Jerry Yu23f7a6f2022-04-23 15:16:45 +08002169
2170 /* We should only send the key_share extension if the client's initial
2171 * key share was not acceptable. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002172 if (ssl->handshake->offered_group_id != 0) {
2173 MBEDTLS_SSL_DEBUG_MSG(4, ("Skip key_share extension in HRR"));
2174 return 0;
Jerry Yu23f7a6f2022-04-23 15:16:45 +08002175 }
2176
Gilles Peskine449bd832023-01-11 14:50:10 +01002177 if (selected_group == 0) {
2178 MBEDTLS_SSL_DEBUG_MSG(1, ("no matching named group found"));
2179 return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
Jerry Yu23f7a6f2022-04-23 15:16:45 +08002180 }
2181
Jerry Yub0ac10b2022-05-05 11:10:08 +08002182 /* Check if we have enough space:
2183 * - extension_type (2 bytes)
2184 * - extension_data_length (2 bytes)
2185 * - selected_group (2 bytes)
2186 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002187 MBEDTLS_SSL_CHK_BUF_PTR(buf, end, 6);
Jerry Yu23f7a6f2022-04-23 15:16:45 +08002188
Gilles Peskine449bd832023-01-11 14:50:10 +01002189 MBEDTLS_PUT_UINT16_BE(MBEDTLS_TLS_EXT_KEY_SHARE, buf, 0);
2190 MBEDTLS_PUT_UINT16_BE(2, buf, 2);
2191 MBEDTLS_PUT_UINT16_BE(selected_group, buf, 4);
Jerry Yu23f7a6f2022-04-23 15:16:45 +08002192
Gilles Peskine449bd832023-01-11 14:50:10 +01002193 MBEDTLS_SSL_DEBUG_MSG(3,
2194 ("HRR selected_group: %s (%x)",
2195 mbedtls_ssl_named_group_to_str(selected_group),
2196 selected_group));
Jerry Yu23f7a6f2022-04-23 15:16:45 +08002197
2198 *out_len = 6;
Jerry Yu23f7a6f2022-04-23 15:16:45 +08002199
Gilles Peskine449bd832023-01-11 14:50:10 +01002200 mbedtls_ssl_tls13_set_hs_sent_ext_mask(ssl, MBEDTLS_TLS_EXT_KEY_SHARE);
Jerry Yub95dd362022-11-08 21:19:34 +08002201
Gilles Peskine449bd832023-01-11 14:50:10 +01002202 return 0;
Jerry Yu23f7a6f2022-04-23 15:16:45 +08002203}
Jerry Yu3bf2c642022-03-30 22:02:12 +08002204
2205/*
2206 * Structure of ServerHello message:
2207 *
2208 * struct {
2209 * ProtocolVersion legacy_version = 0x0303; // TLS v1.2
2210 * Random random;
2211 * opaque legacy_session_id_echo<0..32>;
2212 * CipherSuite cipher_suite;
2213 * uint8 legacy_compression_method = 0;
2214 * Extension extensions<6..2^16-1>;
2215 * } ServerHello;
2216 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002217MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002218static int ssl_tls13_write_server_hello_body(mbedtls_ssl_context *ssl,
2219 unsigned char *buf,
2220 unsigned char *end,
2221 size_t *out_len,
2222 int is_hrr)
Jerry Yu56404d72022-03-30 17:36:13 +08002223{
Jerry Yu955ddd72022-04-22 22:27:33 +08002224 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yu3bf2c642022-03-30 22:02:12 +08002225 unsigned char *p = buf;
Jerry Yud9436a12022-04-20 22:28:09 +08002226 unsigned char *p_extensions_len;
Jerry Yufbe3e642022-04-25 19:31:51 +08002227 size_t output_len;
Jerry Yu3bf2c642022-03-30 22:02:12 +08002228
2229 *out_len = 0;
Jerry Yu50e00e32022-10-31 14:45:01 +08002230 ssl->handshake->sent_extensions = MBEDTLS_SSL_EXT_MASK_NONE;
Jerry Yu3bf2c642022-03-30 22:02:12 +08002231
Jerry Yucfc04b32022-04-21 09:31:58 +08002232 /* ...
2233 * ProtocolVersion legacy_version = 0x0303; // TLS 1.2
2234 * ...
2235 * with ProtocolVersion defined as:
2236 * uint16 ProtocolVersion;
Jerry Yu3bf2c642022-03-30 22:02:12 +08002237 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002238 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 2);
2239 MBEDTLS_PUT_UINT16_BE(0x0303, p, 0);
Jerry Yu3bf2c642022-03-30 22:02:12 +08002240 p += 2;
2241
Jerry Yu1c3e6882022-04-20 21:23:40 +08002242 /* ...
2243 * Random random;
2244 * ...
Jerry Yucfc04b32022-04-21 09:31:58 +08002245 * with Random defined as:
2246 * opaque Random[MBEDTLS_SERVER_HELLO_RANDOM_LEN];
Jerry Yu1c3e6882022-04-20 21:23:40 +08002247 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002248 MBEDTLS_SSL_CHK_BUF_PTR(p, end, MBEDTLS_SERVER_HELLO_RANDOM_LEN);
2249 if (is_hrr) {
2250 memcpy(p, mbedtls_ssl_tls13_hello_retry_request_magic,
2251 MBEDTLS_SERVER_HELLO_RANDOM_LEN);
2252 } else {
2253 memcpy(p, &ssl->handshake->randbytes[MBEDTLS_CLIENT_HELLO_RANDOM_LEN],
2254 MBEDTLS_SERVER_HELLO_RANDOM_LEN);
Jerry Yu23f7a6f2022-04-23 15:16:45 +08002255 }
Gilles Peskine449bd832023-01-11 14:50:10 +01002256 MBEDTLS_SSL_DEBUG_BUF(3, "server hello, random bytes",
2257 p, MBEDTLS_SERVER_HELLO_RANDOM_LEN);
Jerry Yu3bf2c642022-03-30 22:02:12 +08002258 p += MBEDTLS_SERVER_HELLO_RANDOM_LEN;
2259
Jerry Yucfc04b32022-04-21 09:31:58 +08002260 /* ...
2261 * opaque legacy_session_id_echo<0..32>;
2262 * ...
Jerry Yu3bf2c642022-03-30 22:02:12 +08002263 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002264 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 1 + ssl->session_negotiate->id_len);
2265 *p++ = (unsigned char) ssl->session_negotiate->id_len;
2266 if (ssl->session_negotiate->id_len > 0) {
2267 memcpy(p, &ssl->session_negotiate->id[0],
2268 ssl->session_negotiate->id_len);
Jerry Yu3bf2c642022-03-30 22:02:12 +08002269 p += ssl->session_negotiate->id_len;
Jerry Yu955ddd72022-04-22 22:27:33 +08002270
Gilles Peskine449bd832023-01-11 14:50:10 +01002271 MBEDTLS_SSL_DEBUG_BUF(3, "session id", ssl->session_negotiate->id,
2272 ssl->session_negotiate->id_len);
Jerry Yu3bf2c642022-03-30 22:02:12 +08002273 }
2274
Jerry Yucfc04b32022-04-21 09:31:58 +08002275 /* ...
2276 * CipherSuite cipher_suite;
2277 * ...
2278 * with CipherSuite defined as:
2279 * uint8 CipherSuite[2];
Jerry Yu3bf2c642022-03-30 22:02:12 +08002280 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002281 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 2);
2282 MBEDTLS_PUT_UINT16_BE(ssl->session_negotiate->ciphersuite, p, 0);
Jerry Yu3bf2c642022-03-30 22:02:12 +08002283 p += 2;
Gilles Peskine449bd832023-01-11 14:50:10 +01002284 MBEDTLS_SSL_DEBUG_MSG(3,
2285 ("server hello, chosen ciphersuite: %s ( id=%d )",
2286 mbedtls_ssl_get_ciphersuite_name(
2287 ssl->session_negotiate->ciphersuite),
2288 ssl->session_negotiate->ciphersuite));
Jerry Yu3bf2c642022-03-30 22:02:12 +08002289
Jerry Yucfc04b32022-04-21 09:31:58 +08002290 /* ...
2291 * uint8 legacy_compression_method = 0;
2292 * ...
2293 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002294 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 1);
Thomas Daubney31e03a82022-07-25 15:59:25 +01002295 *p++ = MBEDTLS_SSL_COMPRESS_NULL;
Jerry Yu3bf2c642022-03-30 22:02:12 +08002296
Jerry Yucfc04b32022-04-21 09:31:58 +08002297 /* ...
2298 * Extension extensions<6..2^16-1>;
2299 * ...
2300 * struct {
2301 * ExtensionType extension_type; (2 bytes)
2302 * opaque extension_data<0..2^16-1>;
2303 * } Extension;
2304 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002305 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 2);
Jerry Yud9436a12022-04-20 22:28:09 +08002306 p_extensions_len = p;
Jerry Yu3bf2c642022-03-30 22:02:12 +08002307 p += 2;
2308
Gilles Peskine449bd832023-01-11 14:50:10 +01002309 if ((ret = ssl_tls13_write_server_hello_supported_versions_ext(
2310 ssl, p, end, &output_len)) != 0) {
Jerry Yu955ddd72022-04-22 22:27:33 +08002311 MBEDTLS_SSL_DEBUG_RET(
Gilles Peskine449bd832023-01-11 14:50:10 +01002312 1, "ssl_tls13_write_server_hello_supported_versions_ext", ret);
2313 return ret;
Jerry Yu3bf2c642022-03-30 22:02:12 +08002314 }
2315 p += output_len;
2316
Gilles Peskine449bd832023-01-11 14:50:10 +01002317 if (mbedtls_ssl_tls13_key_exchange_mode_with_ephemeral(ssl)) {
2318 if (is_hrr) {
2319 ret = ssl_tls13_write_hrr_key_share_ext(ssl, p, end, &output_len);
2320 } else {
2321 ret = ssl_tls13_write_key_share_ext(ssl, p, end, &output_len);
2322 }
2323 if (ret != 0) {
2324 return ret;
2325 }
Jerry Yu3bf2c642022-03-30 22:02:12 +08002326 p += output_len;
2327 }
Jerry Yu3bf2c642022-03-30 22:02:12 +08002328
Ronald Cron41a443a2022-10-04 16:38:25 +02002329#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01002330 if (!is_hrr && mbedtls_ssl_tls13_key_exchange_mode_with_psk(ssl)) {
2331 ret = ssl_tls13_write_server_pre_shared_key_ext(ssl, p, end, &output_len);
2332 if (ret != 0) {
2333 MBEDTLS_SSL_DEBUG_RET(1, "ssl_tls13_write_server_pre_shared_key_ext",
2334 ret);
2335 return ret;
Jerry Yu032b15ce2022-07-11 06:10:03 +00002336 }
2337 p += output_len;
2338 }
Ronald Cron41a443a2022-10-04 16:38:25 +02002339#endif
Jerry Yu032b15ce2022-07-11 06:10:03 +00002340
Gilles Peskine449bd832023-01-11 14:50:10 +01002341 MBEDTLS_PUT_UINT16_BE(p - p_extensions_len - 2, p_extensions_len, 0);
Jerry Yu3bf2c642022-03-30 22:02:12 +08002342
Gilles Peskine449bd832023-01-11 14:50:10 +01002343 MBEDTLS_SSL_DEBUG_BUF(4, "server hello extensions",
2344 p_extensions_len, p - p_extensions_len);
Jerry Yu3bf2c642022-03-30 22:02:12 +08002345
Jerry Yud9436a12022-04-20 22:28:09 +08002346 *out_len = p - buf;
Jerry Yu3bf2c642022-03-30 22:02:12 +08002347
Gilles Peskine449bd832023-01-11 14:50:10 +01002348 MBEDTLS_SSL_DEBUG_BUF(3, "server hello", buf, *out_len);
Jerry Yu3bf2c642022-03-30 22:02:12 +08002349
Jerry Yu7de2ff02022-11-08 21:43:46 +08002350 MBEDTLS_SSL_PRINT_EXTS(
Jerry Yu4b8f2f72022-10-31 13:31:22 +08002351 3, is_hrr ? MBEDTLS_SSL_TLS1_3_HS_HELLO_RETRY_REQUEST :
Gilles Peskine449bd832023-01-11 14:50:10 +01002352 MBEDTLS_SSL_HS_SERVER_HELLO,
2353 ssl->handshake->sent_extensions);
Jerry Yu4b8f2f72022-10-31 13:31:22 +08002354
Gilles Peskine449bd832023-01-11 14:50:10 +01002355 return ret;
Jerry Yu56404d72022-03-30 17:36:13 +08002356}
2357
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002358MBEDTLS_CHECK_RETURN_CRITICAL
Xiaokang Qian934ce6f2023-02-06 10:23:04 +00002359static int ssl_tls13_finalize_server_hello(mbedtls_ssl_context *ssl)
Jerry Yue110d252022-05-05 10:19:22 +08002360{
2361 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine449bd832023-01-11 14:50:10 +01002362 ret = mbedtls_ssl_tls13_compute_handshake_transform(ssl);
2363 if (ret != 0) {
2364 MBEDTLS_SSL_DEBUG_RET(1,
2365 "mbedtls_ssl_tls13_compute_handshake_transform",
2366 ret);
2367 return ret;
Jerry Yue110d252022-05-05 10:19:22 +08002368 }
2369
Gilles Peskine449bd832023-01-11 14:50:10 +01002370 return ret;
Jerry Yue110d252022-05-05 10:19:22 +08002371}
2372
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002373MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002374static int ssl_tls13_write_server_hello(mbedtls_ssl_context *ssl)
Jerry Yu5b64ae92022-03-30 17:15:02 +08002375{
Jerry Yu637a3f12022-04-20 21:37:58 +08002376 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yuf4b27e42022-03-30 17:32:21 +08002377 unsigned char *buf;
2378 size_t buf_len, msg_len;
2379
Gilles Peskine449bd832023-01-11 14:50:10 +01002380 MBEDTLS_SSL_DEBUG_MSG(2, ("=> write server hello"));
Jerry Yuf4b27e42022-03-30 17:32:21 +08002381
Gilles Peskine449bd832023-01-11 14:50:10 +01002382 MBEDTLS_SSL_PROC_CHK(ssl_tls13_prepare_server_hello(ssl));
Jerry Yuf4b27e42022-03-30 17:32:21 +08002383
Xiaokang Qian9f1747b2023-03-30 02:50:04 +00002384 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_start_handshake_msg(
2385 ssl, MBEDTLS_SSL_HS_SERVER_HELLO, &buf, &buf_len));
Jerry Yuf4b27e42022-03-30 17:32:21 +08002386
Gilles Peskine449bd832023-01-11 14:50:10 +01002387 MBEDTLS_SSL_PROC_CHK(ssl_tls13_write_server_hello_body(ssl, buf,
2388 buf + buf_len,
2389 &msg_len,
2390 0));
Jerry Yuf4b27e42022-03-30 17:32:21 +08002391
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +01002392 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_add_hs_msg_to_checksum(
Manuel Pégourié-Gonnard43cc1272023-02-06 11:48:19 +01002393 ssl, MBEDTLS_SSL_HS_SERVER_HELLO, buf, msg_len));
Jerry Yuf4b27e42022-03-30 17:32:21 +08002394
Gilles Peskine449bd832023-01-11 14:50:10 +01002395 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_finish_handshake_msg(
2396 ssl, buf_len, msg_len));
Jerry Yu637a3f12022-04-20 21:37:58 +08002397
Xiaokang Qian934ce6f2023-02-06 10:23:04 +00002398 MBEDTLS_SSL_PROC_CHK(ssl_tls13_finalize_server_hello(ssl));
Jerry Yue110d252022-05-05 10:19:22 +08002399
Gabor Mezei7b39bf12022-05-24 16:04:14 +02002400#if defined(MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE)
2401 /* The server sends a dummy change_cipher_spec record immediately
2402 * after its first handshake message. This may either be after
2403 * a ServerHello or a HelloRetryRequest.
2404 */
Gabor Mezei96ae9262022-06-28 11:45:18 +02002405 mbedtls_ssl_handshake_set_state(
Gilles Peskine449bd832023-01-11 14:50:10 +01002406 ssl, MBEDTLS_SSL_SERVER_CCS_AFTER_SERVER_HELLO);
Gabor Mezei7b39bf12022-05-24 16:04:14 +02002407#else
Gilles Peskine449bd832023-01-11 14:50:10 +01002408 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_ENCRYPTED_EXTENSIONS);
Gabor Mezei7b39bf12022-05-24 16:04:14 +02002409#endif /* MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE */
Jerry Yue110d252022-05-05 10:19:22 +08002410
Jerry Yuf4b27e42022-03-30 17:32:21 +08002411cleanup:
2412
Gilles Peskine449bd832023-01-11 14:50:10 +01002413 MBEDTLS_SSL_DEBUG_MSG(2, ("<= write server hello"));
2414 return ret;
Jerry Yu5b64ae92022-03-30 17:15:02 +08002415}
2416
Jerry Yu23d1a252022-05-12 18:08:59 +08002417
2418/*
2419 * Handler for MBEDTLS_SSL_HELLO_RETRY_REQUEST
2420 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002421MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002422static int ssl_tls13_prepare_hello_retry_request(mbedtls_ssl_context *ssl)
Jerry Yu23d1a252022-05-12 18:08:59 +08002423{
2424 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine449bd832023-01-11 14:50:10 +01002425 if (ssl->handshake->hello_retry_request_count > 0) {
2426 MBEDTLS_SSL_DEBUG_MSG(1, ("Too many HRRs"));
2427 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE,
2428 MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE);
2429 return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
Jerry Yu23d1a252022-05-12 18:08:59 +08002430 }
2431
2432 /*
2433 * Create stateless transcript hash for HRR
2434 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002435 MBEDTLS_SSL_DEBUG_MSG(4, ("Reset transcript for HRR"));
2436 ret = mbedtls_ssl_reset_transcript_for_hrr(ssl);
2437 if (ret != 0) {
2438 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_reset_transcript_for_hrr", ret);
2439 return ret;
Jerry Yu23d1a252022-05-12 18:08:59 +08002440 }
Gilles Peskine449bd832023-01-11 14:50:10 +01002441 mbedtls_ssl_session_reset_msg_layer(ssl, 0);
Jerry Yu23d1a252022-05-12 18:08:59 +08002442
Gilles Peskine449bd832023-01-11 14:50:10 +01002443 return 0;
Jerry Yu23d1a252022-05-12 18:08:59 +08002444}
2445
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002446MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002447static int ssl_tls13_write_hello_retry_request(mbedtls_ssl_context *ssl)
Jerry Yu23d1a252022-05-12 18:08:59 +08002448{
2449 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
2450 unsigned char *buf;
2451 size_t buf_len, msg_len;
2452
Gilles Peskine449bd832023-01-11 14:50:10 +01002453 MBEDTLS_SSL_DEBUG_MSG(2, ("=> write hello retry request"));
Jerry Yu23d1a252022-05-12 18:08:59 +08002454
Gilles Peskine449bd832023-01-11 14:50:10 +01002455 MBEDTLS_SSL_PROC_CHK(ssl_tls13_prepare_hello_retry_request(ssl));
Jerry Yu23d1a252022-05-12 18:08:59 +08002456
Gilles Peskine449bd832023-01-11 14:50:10 +01002457 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_start_handshake_msg(
2458 ssl, MBEDTLS_SSL_HS_SERVER_HELLO,
2459 &buf, &buf_len));
Jerry Yu23d1a252022-05-12 18:08:59 +08002460
Gilles Peskine449bd832023-01-11 14:50:10 +01002461 MBEDTLS_SSL_PROC_CHK(ssl_tls13_write_server_hello_body(ssl, buf,
2462 buf + buf_len,
2463 &msg_len,
2464 1));
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +01002465 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_add_hs_msg_to_checksum(
Manuel Pégourié-Gonnard43cc1272023-02-06 11:48:19 +01002466 ssl, MBEDTLS_SSL_HS_SERVER_HELLO, buf, msg_len));
Jerry Yu23d1a252022-05-12 18:08:59 +08002467
2468
Gilles Peskine449bd832023-01-11 14:50:10 +01002469 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_finish_handshake_msg(ssl, buf_len,
2470 msg_len));
Jerry Yu23d1a252022-05-12 18:08:59 +08002471
2472 ssl->handshake->hello_retry_request_count++;
2473
Gabor Mezei7b39bf12022-05-24 16:04:14 +02002474#if defined(MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE)
2475 /* The server sends a dummy change_cipher_spec record immediately
2476 * after its first handshake message. This may either be after
2477 * a ServerHello or a HelloRetryRequest.
2478 */
Gabor Mezei96ae9262022-06-28 11:45:18 +02002479 mbedtls_ssl_handshake_set_state(
Gilles Peskine449bd832023-01-11 14:50:10 +01002480 ssl, MBEDTLS_SSL_SERVER_CCS_AFTER_HELLO_RETRY_REQUEST);
Gabor Mezei7b39bf12022-05-24 16:04:14 +02002481#else
Gilles Peskine449bd832023-01-11 14:50:10 +01002482 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_CLIENT_HELLO);
Gabor Mezei7b39bf12022-05-24 16:04:14 +02002483#endif /* MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE */
Jerry Yu23d1a252022-05-12 18:08:59 +08002484
2485cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +01002486 MBEDTLS_SSL_DEBUG_MSG(2, ("<= write hello retry request"));
2487 return ret;
Jerry Yu23d1a252022-05-12 18:08:59 +08002488}
2489
Jerry Yu5b64ae92022-03-30 17:15:02 +08002490/*
Jerry Yu4d3841a2022-04-16 12:37:19 +08002491 * Handler for MBEDTLS_SSL_ENCRYPTED_EXTENSIONS
2492 */
Jerry Yu4d3841a2022-04-16 12:37:19 +08002493
2494/*
2495 * struct {
2496 * Extension extensions<0..2 ^ 16 - 1>;
2497 * } EncryptedExtensions;
2498 *
2499 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002500MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002501static int ssl_tls13_write_encrypted_extensions_body(mbedtls_ssl_context *ssl,
2502 unsigned char *buf,
2503 unsigned char *end,
2504 size_t *out_len)
Jerry Yu4d3841a2022-04-16 12:37:19 +08002505{
XiaokangQianacb39922022-06-17 10:18:48 +00002506 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yu4d3841a2022-04-16 12:37:19 +08002507 unsigned char *p = buf;
2508 size_t extensions_len = 0;
Jerry Yu8937eb42022-05-03 12:12:14 +08002509 unsigned char *p_extensions_len;
XiaokangQianacb39922022-06-17 10:18:48 +00002510 size_t output_len;
Jerry Yu9da5e5a2022-05-03 15:46:09 +08002511
Jerry Yu4d3841a2022-04-16 12:37:19 +08002512 *out_len = 0;
2513
Gilles Peskine449bd832023-01-11 14:50:10 +01002514 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 2);
Jerry Yu8937eb42022-05-03 12:12:14 +08002515 p_extensions_len = p;
Jerry Yu4d3841a2022-04-16 12:37:19 +08002516 p += 2;
2517
Jerry Yu8937eb42022-05-03 12:12:14 +08002518 ((void) ssl);
XiaokangQianacb39922022-06-17 10:18:48 +00002519 ((void) ret);
2520 ((void) output_len);
2521
2522#if defined(MBEDTLS_SSL_ALPN)
Gilles Peskine449bd832023-01-11 14:50:10 +01002523 ret = mbedtls_ssl_write_alpn_ext(ssl, p, end, &output_len);
2524 if (ret != 0) {
2525 return ret;
2526 }
XiaokangQian95d5f542022-06-24 02:29:26 +00002527 p += output_len;
XiaokangQianacb39922022-06-17 10:18:48 +00002528#endif /* MBEDTLS_SSL_ALPN */
Jerry Yu4d3841a2022-04-16 12:37:19 +08002529
Jerry Yu71c14f12022-12-12 11:10:35 +08002530#if defined(MBEDTLS_SSL_EARLY_DATA)
2531 if (ssl->early_data_status == MBEDTLS_SSL_EARLY_DATA_STATUS_ACCEPTED) {
Jerry Yu52335392023-11-23 18:06:06 +08002532 ret = mbedtls_ssl_tls13_write_early_data_ext(
2533 ssl, p, end, &output_len, NULL);
Jerry Yu71c14f12022-12-12 11:10:35 +08002534 if (ret != 0) {
2535 return ret;
2536 }
2537 p += output_len;
2538 }
2539#endif /* MBEDTLS_SSL_EARLY_DATA */
2540
Gilles Peskine449bd832023-01-11 14:50:10 +01002541 extensions_len = (p - p_extensions_len) - 2;
2542 MBEDTLS_PUT_UINT16_BE(extensions_len, p_extensions_len, 0);
Jerry Yu8937eb42022-05-03 12:12:14 +08002543
2544 *out_len = p - buf;
Jerry Yu4d3841a2022-04-16 12:37:19 +08002545
Gilles Peskine449bd832023-01-11 14:50:10 +01002546 MBEDTLS_SSL_DEBUG_BUF(4, "encrypted extensions", buf, *out_len);
Jerry Yu4d3841a2022-04-16 12:37:19 +08002547
Jerry Yu7de2ff02022-11-08 21:43:46 +08002548 MBEDTLS_SSL_PRINT_EXTS(
Gilles Peskine449bd832023-01-11 14:50:10 +01002549 3, MBEDTLS_SSL_HS_ENCRYPTED_EXTENSIONS, ssl->handshake->sent_extensions);
Jerry Yu4b8f2f72022-10-31 13:31:22 +08002550
Gilles Peskine449bd832023-01-11 14:50:10 +01002551 return 0;
Jerry Yu4d3841a2022-04-16 12:37:19 +08002552}
2553
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002554MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002555static int ssl_tls13_write_encrypted_extensions(mbedtls_ssl_context *ssl)
Jerry Yu4d3841a2022-04-16 12:37:19 +08002556{
2557 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
2558 unsigned char *buf;
Jerry Yu39730a72022-05-03 12:14:04 +08002559 size_t buf_len, msg_len;
Jerry Yu4d3841a2022-04-16 12:37:19 +08002560
Gilles Peskine449bd832023-01-11 14:50:10 +01002561 mbedtls_ssl_set_outbound_transform(ssl,
2562 ssl->handshake->transform_handshake);
Gabor Mezei54719122022-06-28 11:34:56 +02002563 MBEDTLS_SSL_DEBUG_MSG(
Gilles Peskine449bd832023-01-11 14:50:10 +01002564 3, ("switching to handshake transform for outbound data"));
Gabor Mezei54719122022-06-28 11:34:56 +02002565
Gilles Peskine449bd832023-01-11 14:50:10 +01002566 MBEDTLS_SSL_DEBUG_MSG(2, ("=> write encrypted extensions"));
Jerry Yu4d3841a2022-04-16 12:37:19 +08002567
Xiaokang Qian9f1747b2023-03-30 02:50:04 +00002568 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_start_handshake_msg(
2569 ssl, MBEDTLS_SSL_HS_ENCRYPTED_EXTENSIONS,
2570 &buf, &buf_len));
Jerry Yu4d3841a2022-04-16 12:37:19 +08002571
Gilles Peskine449bd832023-01-11 14:50:10 +01002572 MBEDTLS_SSL_PROC_CHK(ssl_tls13_write_encrypted_extensions_body(
2573 ssl, buf, buf + buf_len, &msg_len));
Jerry Yu4d3841a2022-04-16 12:37:19 +08002574
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +01002575 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_add_hs_msg_to_checksum(
Xiaokang Qian9f1747b2023-03-30 02:50:04 +00002576 ssl, MBEDTLS_SSL_HS_ENCRYPTED_EXTENSIONS,
2577 buf, msg_len));
Jerry Yu4d3841a2022-04-16 12:37:19 +08002578
Gilles Peskine449bd832023-01-11 14:50:10 +01002579 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_finish_handshake_msg(
2580 ssl, buf_len, msg_len));
Jerry Yu4d3841a2022-04-16 12:37:19 +08002581
Ronald Cron928cbd32022-10-04 16:14:26 +02002582#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01002583 if (mbedtls_ssl_tls13_key_exchange_mode_with_psk(ssl)) {
2584 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_SERVER_FINISHED);
2585 } else {
2586 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_CERTIFICATE_REQUEST);
2587 }
Jerry Yu8937eb42022-05-03 12:12:14 +08002588#else
Gilles Peskine449bd832023-01-11 14:50:10 +01002589 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_SERVER_FINISHED);
Jerry Yu8937eb42022-05-03 12:12:14 +08002590#endif
2591
Jerry Yu4d3841a2022-04-16 12:37:19 +08002592cleanup:
2593
Gilles Peskine449bd832023-01-11 14:50:10 +01002594 MBEDTLS_SSL_DEBUG_MSG(2, ("<= write encrypted extensions"));
2595 return ret;
Jerry Yu4d3841a2022-04-16 12:37:19 +08002596}
2597
Ronald Cron928cbd32022-10-04 16:14:26 +02002598#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED)
XiaokangQiana987e1d2022-05-07 01:25:58 +00002599#define SSL_CERTIFICATE_REQUEST_SEND_REQUEST 0
2600#define SSL_CERTIFICATE_REQUEST_SKIP 1
2601/* Coordination:
2602 * Check whether a CertificateRequest message should be written.
2603 * Returns a negative code on failure, or
2604 * - SSL_CERTIFICATE_REQUEST_SEND_REQUEST
2605 * - SSL_CERTIFICATE_REQUEST_SKIP
2606 * indicating if the writing of the CertificateRequest
2607 * should be skipped or not.
2608 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002609MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002610static int ssl_tls13_certificate_request_coordinate(mbedtls_ssl_context *ssl)
XiaokangQiana987e1d2022-05-07 01:25:58 +00002611{
2612 int authmode;
2613
XiaokangQian40a35232022-05-07 09:02:40 +00002614#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Gilles Peskine449bd832023-01-11 14:50:10 +01002615 if (ssl->handshake->sni_authmode != MBEDTLS_SSL_VERIFY_UNSET) {
XiaokangQian40a35232022-05-07 09:02:40 +00002616 authmode = ssl->handshake->sni_authmode;
Gilles Peskine449bd832023-01-11 14:50:10 +01002617 } else
XiaokangQian40a35232022-05-07 09:02:40 +00002618#endif
XiaokangQiana987e1d2022-05-07 01:25:58 +00002619 authmode = ssl->conf->authmode;
2620
Gilles Peskine449bd832023-01-11 14:50:10 +01002621 if (authmode == MBEDTLS_SSL_VERIFY_NONE) {
Ronald Croneac00ad2022-09-13 10:16:31 +02002622 ssl->session_negotiate->verify_result = MBEDTLS_X509_BADCERT_SKIP_VERIFY;
Gilles Peskine449bd832023-01-11 14:50:10 +01002623 return SSL_CERTIFICATE_REQUEST_SKIP;
Ronald Croneac00ad2022-09-13 10:16:31 +02002624 }
XiaokangQiana987e1d2022-05-07 01:25:58 +00002625
XiaokangQianc3017f62022-05-13 05:55:41 +00002626 ssl->handshake->certificate_request_sent = 1;
XiaokangQian189ded22022-05-10 08:12:17 +00002627
Gilles Peskine449bd832023-01-11 14:50:10 +01002628 return SSL_CERTIFICATE_REQUEST_SEND_REQUEST;
XiaokangQiana987e1d2022-05-07 01:25:58 +00002629}
2630
XiaokangQiancec9ae62022-05-06 07:28:50 +00002631/*
2632 * struct {
2633 * opaque certificate_request_context<0..2^8-1>;
2634 * Extension extensions<2..2^16-1>;
2635 * } CertificateRequest;
2636 *
2637 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002638MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002639static int ssl_tls13_write_certificate_request_body(mbedtls_ssl_context *ssl,
2640 unsigned char *buf,
2641 const unsigned char *end,
2642 size_t *out_len)
XiaokangQiancec9ae62022-05-06 07:28:50 +00002643{
2644 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
2645 unsigned char *p = buf;
XiaokangQianec6efb92022-05-06 09:53:10 +00002646 size_t output_len = 0;
XiaokangQiancec9ae62022-05-06 07:28:50 +00002647 unsigned char *p_extensions_len;
2648
2649 *out_len = 0;
2650
2651 /* Check if we have enough space:
2652 * - certificate_request_context (1 byte)
2653 * - extensions length (2 bytes)
2654 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002655 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 3);
XiaokangQiancec9ae62022-05-06 07:28:50 +00002656
2657 /*
2658 * Write certificate_request_context
2659 */
2660 /*
2661 * We use a zero length context for the normal handshake
2662 * messages. For post-authentication handshake messages
2663 * this request context would be set to a non-zero value.
2664 */
2665 *p++ = 0x0;
2666
2667 /*
2668 * Write extensions
2669 */
2670 /* The extensions must contain the signature_algorithms. */
2671 p_extensions_len = p;
2672 p += 2;
Gilles Peskine449bd832023-01-11 14:50:10 +01002673 ret = mbedtls_ssl_write_sig_alg_ext(ssl, p, end, &output_len);
2674 if (ret != 0) {
2675 return ret;
2676 }
XiaokangQiancec9ae62022-05-06 07:28:50 +00002677
XiaokangQianec6efb92022-05-06 09:53:10 +00002678 p += output_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01002679 MBEDTLS_PUT_UINT16_BE(p - p_extensions_len - 2, p_extensions_len, 0);
XiaokangQiancec9ae62022-05-06 07:28:50 +00002680
2681 *out_len = p - buf;
2682
Jerry Yu7de2ff02022-11-08 21:43:46 +08002683 MBEDTLS_SSL_PRINT_EXTS(
Gilles Peskine449bd832023-01-11 14:50:10 +01002684 3, MBEDTLS_SSL_HS_CERTIFICATE_REQUEST, ssl->handshake->sent_extensions);
Jerry Yu4b8f2f72022-10-31 13:31:22 +08002685
Gilles Peskine449bd832023-01-11 14:50:10 +01002686 return 0;
XiaokangQiancec9ae62022-05-06 07:28:50 +00002687}
2688
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002689MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002690static int ssl_tls13_write_certificate_request(mbedtls_ssl_context *ssl)
XiaokangQiancec9ae62022-05-06 07:28:50 +00002691{
2692 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
2693
Gilles Peskine449bd832023-01-11 14:50:10 +01002694 MBEDTLS_SSL_DEBUG_MSG(2, ("=> write certificate request"));
XiaokangQiancec9ae62022-05-06 07:28:50 +00002695
Gilles Peskine449bd832023-01-11 14:50:10 +01002696 MBEDTLS_SSL_PROC_CHK_NEG(ssl_tls13_certificate_request_coordinate(ssl));
XiaokangQiancec9ae62022-05-06 07:28:50 +00002697
Gilles Peskine449bd832023-01-11 14:50:10 +01002698 if (ret == SSL_CERTIFICATE_REQUEST_SEND_REQUEST) {
XiaokangQiancec9ae62022-05-06 07:28:50 +00002699 unsigned char *buf;
2700 size_t buf_len, msg_len;
2701
Xiaokang Qian9f1747b2023-03-30 02:50:04 +00002702 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_start_handshake_msg(
2703 ssl, MBEDTLS_SSL_HS_CERTIFICATE_REQUEST,
2704 &buf, &buf_len));
XiaokangQiancec9ae62022-05-06 07:28:50 +00002705
Gilles Peskine449bd832023-01-11 14:50:10 +01002706 MBEDTLS_SSL_PROC_CHK(ssl_tls13_write_certificate_request_body(
2707 ssl, buf, buf + buf_len, &msg_len));
XiaokangQiancec9ae62022-05-06 07:28:50 +00002708
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +01002709 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_add_hs_msg_to_checksum(
Xiaokang Qian9f1747b2023-03-30 02:50:04 +00002710 ssl, MBEDTLS_SSL_HS_CERTIFICATE_REQUEST,
2711 buf, msg_len));
XiaokangQiancec9ae62022-05-06 07:28:50 +00002712
Gilles Peskine449bd832023-01-11 14:50:10 +01002713 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_finish_handshake_msg(
2714 ssl, buf_len, msg_len));
2715 } else if (ret == SSL_CERTIFICATE_REQUEST_SKIP) {
2716 MBEDTLS_SSL_DEBUG_MSG(2, ("<= skip write certificate request"));
XiaokangQiancec9ae62022-05-06 07:28:50 +00002717 ret = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01002718 } else {
2719 MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
XiaokangQiancec9ae62022-05-06 07:28:50 +00002720 ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
2721 goto cleanup;
2722 }
2723
Gilles Peskine449bd832023-01-11 14:50:10 +01002724 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_SERVER_CERTIFICATE);
XiaokangQiancec9ae62022-05-06 07:28:50 +00002725cleanup:
2726
Gilles Peskine449bd832023-01-11 14:50:10 +01002727 MBEDTLS_SSL_DEBUG_MSG(2, ("<= write certificate request"));
2728 return ret;
XiaokangQiancec9ae62022-05-06 07:28:50 +00002729}
XiaokangQiancec9ae62022-05-06 07:28:50 +00002730
Jerry Yu4d3841a2022-04-16 12:37:19 +08002731/*
Jerry Yuc6e6dbf2022-04-16 19:42:57 +08002732 * Handler for MBEDTLS_SSL_SERVER_CERTIFICATE
Jerry Yu83da34e2022-04-16 13:59:52 +08002733 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002734MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002735static int ssl_tls13_write_server_certificate(mbedtls_ssl_context *ssl)
Jerry Yu83da34e2022-04-16 13:59:52 +08002736{
Jerry Yu5a26f302022-05-10 20:46:40 +08002737 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
XiaokangQianfb665a82022-06-15 03:57:21 +00002738
2739#if defined(MBEDTLS_X509_CRT_PARSE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01002740 if ((ssl_tls13_pick_key_cert(ssl) != 0) ||
2741 mbedtls_ssl_own_cert(ssl) == NULL) {
2742 MBEDTLS_SSL_DEBUG_MSG(2, ("No certificate available."));
2743 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE,
2744 MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE);
2745 return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
Jerry Yu5a26f302022-05-10 20:46:40 +08002746 }
XiaokangQianfb665a82022-06-15 03:57:21 +00002747#endif /* MBEDTLS_X509_CRT_PARSE_C */
Jerry Yu5a26f302022-05-10 20:46:40 +08002748
Gilles Peskine449bd832023-01-11 14:50:10 +01002749 ret = mbedtls_ssl_tls13_write_certificate(ssl);
2750 if (ret != 0) {
2751 return ret;
2752 }
2753 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_CERTIFICATE_VERIFY);
2754 return 0;
Jerry Yu83da34e2022-04-16 13:59:52 +08002755}
2756
2757/*
Jerry Yuc6e6dbf2022-04-16 19:42:57 +08002758 * Handler for MBEDTLS_SSL_CERTIFICATE_VERIFY
Jerry Yu83da34e2022-04-16 13:59:52 +08002759 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002760MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002761static int ssl_tls13_write_certificate_verify(mbedtls_ssl_context *ssl)
Jerry Yu83da34e2022-04-16 13:59:52 +08002762{
Gilles Peskine449bd832023-01-11 14:50:10 +01002763 int ret = mbedtls_ssl_tls13_write_certificate_verify(ssl);
2764 if (ret != 0) {
2765 return ret;
2766 }
2767 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_SERVER_FINISHED);
2768 return 0;
Jerry Yu83da34e2022-04-16 13:59:52 +08002769}
Ronald Cron928cbd32022-10-04 16:14:26 +02002770#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED */
Jerry Yu83da34e2022-04-16 13:59:52 +08002771
Jerry Yu9b72e392023-12-01 16:27:08 +08002772/*
2773 * RFC 8446 section A.2
2774 *
2775 * | Send ServerHello
2776 * | K_send = handshake
2777 * | Send EncryptedExtensions
2778 * | [Send CertificateRequest]
2779 * Can send | [Send Certificate + CertificateVerify]
2780 * app data | Send Finished
2781 * after --> | K_send = application
2782 * here +--------+--------+
2783 * No 0-RTT | | 0-RTT
2784 * | |
2785 * K_recv = handshake | | K_recv = early data
2786 * [Skip decrypt errors] | +------> WAIT_EOED -+
2787 * | | Recv | | Recv EndOfEarlyData
2788 * | | early data | | K_recv = handshake
2789 * | +------------+ |
2790 * | |
2791 * +> WAIT_FLIGHT2 <--------+
2792 * |
2793 * +--------+--------+
2794 * No auth | | Client auth
2795 * | |
2796 * | v
2797 * | WAIT_CERT
2798 * | Recv | | Recv Certificate
2799 * | empty | v
2800 * | Certificate | WAIT_CV
2801 * | | | Recv
2802 * | v | CertificateVerify
2803 * +-> WAIT_FINISHED <---+
2804 * | Recv Finished
2805 *
2806 *
2807 * The following function handles the state changes after WAIT_FLIGHT2 in the
Jerry Yu3be85072023-12-04 09:58:54 +08002808 * above diagram. We are not going to receive early data related messages
2809 * anymore, prepare to receive the first handshake message of the client
2810 * second flight.
Jerry Yu9b72e392023-12-01 16:27:08 +08002811 */
Jerry Yu3be85072023-12-04 09:58:54 +08002812static void ssl_tls13_prepare_for_handshake_second_flight(
2813 mbedtls_ssl_context *ssl)
Jerry Yu9b72e392023-12-01 16:27:08 +08002814{
Jerry Yu9b72e392023-12-01 16:27:08 +08002815 if (ssl->handshake->certificate_request_sent) {
2816 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_CLIENT_CERTIFICATE);
2817 } else {
Jerry Yu42020fb2023-12-05 17:35:53 +08002818 MBEDTLS_SSL_DEBUG_MSG(2, ("skip parse certificate"));
Jerry Yuebb1b1d2023-12-05 11:02:15 +08002819 MBEDTLS_SSL_DEBUG_MSG(2, ("skip parse certificate verify"));
Jerry Yuebb1b1d2023-12-05 11:02:15 +08002820
Jerry Yu9b72e392023-12-01 16:27:08 +08002821 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_CLIENT_FINISHED);
2822 }
Jerry Yu9b72e392023-12-01 16:27:08 +08002823}
2824
Jerry Yu83da34e2022-04-16 13:59:52 +08002825/*
Jerry Yud6e253d2022-05-18 13:59:24 +08002826 * Handler for MBEDTLS_SSL_SERVER_FINISHED
Jerry Yu69dd8d42022-04-16 12:51:26 +08002827 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002828MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002829static int ssl_tls13_write_server_finished(mbedtls_ssl_context *ssl)
Jerry Yu69dd8d42022-04-16 12:51:26 +08002830{
Jerry Yud6e253d2022-05-18 13:59:24 +08002831 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yu27bdc7c2022-04-16 13:33:27 +08002832
Gilles Peskine449bd832023-01-11 14:50:10 +01002833 ret = mbedtls_ssl_tls13_write_finished_message(ssl);
2834 if (ret != 0) {
2835 return ret;
2836 }
Jerry Yu27bdc7c2022-04-16 13:33:27 +08002837
Gilles Peskine449bd832023-01-11 14:50:10 +01002838 ret = mbedtls_ssl_tls13_compute_application_transform(ssl);
2839 if (ret != 0) {
Jerry Yue3d67cb2022-05-19 15:33:10 +08002840 MBEDTLS_SSL_PEND_FATAL_ALERT(
Gilles Peskine449bd832023-01-11 14:50:10 +01002841 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE,
2842 MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE);
2843 return ret;
Jerry Yue3d67cb2022-05-19 15:33:10 +08002844 }
XiaokangQianc3017f62022-05-13 05:55:41 +00002845
Jerry Yu87b5ed42022-12-12 13:07:07 +08002846#if defined(MBEDTLS_SSL_EARLY_DATA)
2847 if (ssl->early_data_status == MBEDTLS_SSL_EARLY_DATA_STATUS_ACCEPTED) {
Jerry Yu0af63dc2023-12-01 17:14:51 +08002848 /* See RFC 8446 section A.2 for more information */
Jerry Yu87b5ed42022-12-12 13:07:07 +08002849 MBEDTLS_SSL_DEBUG_MSG(
2850 1, ("Switch to early keys for inbound traffic. "
2851 "( K_recv = early data )"));
2852 mbedtls_ssl_set_inbound_transform(
2853 ssl, ssl->handshake->transform_earlydata);
2854 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_END_OF_EARLY_DATA);
2855 return 0;
2856 }
2857#endif /* MBEDTLS_SSL_EARLY_DATA */
Jerry Yu0af63dc2023-12-01 17:14:51 +08002858 MBEDTLS_SSL_DEBUG_MSG(
2859 1, ("Switch to handshake keys for inbound traffic "
2860 "( K_recv = handshake )"));
Gilles Peskine449bd832023-01-11 14:50:10 +01002861 mbedtls_ssl_set_inbound_transform(ssl, ssl->handshake->transform_handshake);
XiaokangQian189ded22022-05-10 08:12:17 +00002862
Jerry Yu3be85072023-12-04 09:58:54 +08002863 ssl_tls13_prepare_for_handshake_second_flight(ssl);
Jerry Yu7d8c3fe2022-12-12 12:59:44 +08002864
2865 return 0;
2866}
2867
Jerry Yu87b5ed42022-12-12 13:07:07 +08002868#if defined(MBEDTLS_SSL_EARLY_DATA)
2869/*
Jerry Yu59d420f2023-12-01 16:30:34 +08002870 * Handler for MBEDTLS_SSL_END_OF_EARLY_DATA
Jerry Yu87b5ed42022-12-12 13:07:07 +08002871 */
Jerry Yu3be85072023-12-04 09:58:54 +08002872#define SSL_GOT_END_OF_EARLY_DATA 0
Jerry Yub55f9eb2023-12-05 10:27:17 +08002873#define SSL_GOT_EARLY_DATA 1
Jerry Yud5c34962023-12-01 16:32:31 +08002874/* Coordination:
Jerry Yu3be85072023-12-04 09:58:54 +08002875 * Deals with the ambiguity of not knowing if the next message is an
2876 * EndOfEarlyData message or an application message containing early data.
Jerry Yud5c34962023-12-01 16:32:31 +08002877 * Returns a negative code on failure, or
Jerry Yu3be85072023-12-04 09:58:54 +08002878 * - SSL_GOT_END_OF_EARLY_DATA
Jerry Yub55f9eb2023-12-05 10:27:17 +08002879 * - SSL_GOT_EARLY_DATA
Jerry Yud5c34962023-12-01 16:32:31 +08002880 * indicating which message is received.
2881 */
2882MBEDTLS_CHECK_RETURN_CRITICAL
2883static int ssl_tls13_end_of_early_data_coordinate(mbedtls_ssl_context *ssl)
2884{
2885 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yub4ed4602023-12-01 16:34:00 +08002886
2887 if ((ret = mbedtls_ssl_read_record(ssl, 0)) != 0) {
2888 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_read_record", ret);
2889 return ret;
2890 }
2891 ssl->keep_current_message = 1;
2892
2893 if (ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
2894 ssl->in_msg[0] == MBEDTLS_SSL_HS_END_OF_EARLY_DATA) {
Jerry Yub55f9eb2023-12-05 10:27:17 +08002895 MBEDTLS_SSL_DEBUG_MSG(3, ("Received an end_of_early_data message."));
Jerry Yu3be85072023-12-04 09:58:54 +08002896 return SSL_GOT_END_OF_EARLY_DATA;
Jerry Yub4ed4602023-12-01 16:34:00 +08002897 }
2898
2899 if (ssl->in_msgtype == MBEDTLS_SSL_MSG_APPLICATION_DATA) {
Jerry Yub55f9eb2023-12-05 10:27:17 +08002900 MBEDTLS_SSL_DEBUG_MSG(3, ("Received early data"));
2901 return SSL_GOT_EARLY_DATA;
Jerry Yub4ed4602023-12-01 16:34:00 +08002902 }
2903
Jerry Yu7bb40a32023-12-04 10:04:15 +08002904 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE,
2905 MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE);
Jerry Yub4ed4602023-12-01 16:34:00 +08002906 return MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE;
Jerry Yud5c34962023-12-01 16:32:31 +08002907}
2908
2909MBEDTLS_CHECK_RETURN_CRITICAL
2910static int ssl_tls13_parse_end_of_early_data(mbedtls_ssl_context *ssl,
2911 const unsigned char *buf,
2912 const unsigned char *end)
2913{
Jerry Yu75c9ab72023-12-01 16:41:40 +08002914 /* RFC 8446 section 4.5
2915 *
2916 * struct {} EndOfEarlyData;
2917 */
Jerry Yufbf03992023-12-04 10:00:37 +08002918 if (buf != end) {
2919 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR,
2920 MBEDTLS_ERR_SSL_DECODE_ERROR);
2921 return MBEDTLS_ERR_SSL_DECODE_ERROR;
2922 }
2923 return 0;
Jerry Yud5c34962023-12-01 16:32:31 +08002924}
2925
2926MBEDTLS_CHECK_RETURN_CRITICAL
2927static int ssl_tls13_process_early_application_data(mbedtls_ssl_context *ssl)
2928{
2929 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yuee4d7292023-12-01 16:46:14 +08002930
2931 if ((ret = mbedtls_ssl_read_record(ssl, 0)) != 0) {
2932 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_read_record", ret);
2933 return ret;
2934 }
2935
Jerry Yuee4d7292023-12-01 16:46:14 +08002936 /*
2937 * Output early data
2938 *
2939 * For the time being, we print received data via debug message.
2940 *
2941 * TODO: Remove it when `mbedtls_ssl_read_early_data` is ready.
2942 */
2943 ssl->in_msg[ssl->in_msglen] = 0;
2944 MBEDTLS_SSL_DEBUG_MSG(3, ("\n%s", ssl->in_msg));
2945
2946 /* RFC 8446 section 4.6.1
2947 *
2948 * A server receiving more than max_early_data_size bytes of 0-RTT data
2949 * SHOULD terminate the connection with an "unexpected_message" alert.
2950 *
2951 * TODO: Add received data size check here.
2952 */
2953
2954 return 0;
Jerry Yud5c34962023-12-01 16:32:31 +08002955}
2956
2957/*
2958 * RFC 8446 section A.2
2959 *
2960 * | Send ServerHello
2961 * | K_send = handshake
2962 * | Send EncryptedExtensions
2963 * | [Send CertificateRequest]
2964 * Can send | [Send Certificate + CertificateVerify]
2965 * app data | Send Finished
2966 * after --> | K_send = application
2967 * here +--------+--------+
2968 * No 0-RTT | | 0-RTT
2969 * | |
2970 * K_recv = handshake | | K_recv = early data
2971 * [Skip decrypt errors] | +------> WAIT_EOED -+
2972 * | | Recv | | Recv EndOfEarlyData
2973 * | | early data | | K_recv = handshake
2974 * | +------------+ |
2975 * | |
2976 * +> WAIT_FLIGHT2 <--------+
2977 * |
2978 * +--------+--------+
2979 * No auth | | Client auth
2980 * | |
2981 * | v
2982 * | WAIT_CERT
2983 * | Recv | | Recv Certificate
2984 * | empty | v
2985 * | Certificate | WAIT_CV
2986 * | | | Recv
2987 * | v | CertificateVerify
2988 * +-> WAIT_FINISHED <---+
2989 * | Recv Finished
2990 *
2991 * The function handles actions and state changes from 0-RTT to WAIT_FLIGHT2 in
2992 * the above diagram.
2993 */
Jerry Yu87b5ed42022-12-12 13:07:07 +08002994MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yu59d420f2023-12-01 16:30:34 +08002995static int ssl_tls13_process_end_of_early_data(mbedtls_ssl_context *ssl)
Jerry Yu87b5ed42022-12-12 13:07:07 +08002996{
2997 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yud5c34962023-12-01 16:32:31 +08002998
2999 MBEDTLS_SSL_DEBUG_MSG(2, ("=> ssl_tls13_process_end_of_early_data"));
3000
3001 MBEDTLS_SSL_PROC_CHK_NEG(ssl_tls13_end_of_early_data_coordinate(ssl));
3002
Jerry Yu3be85072023-12-04 09:58:54 +08003003 if (ret == SSL_GOT_END_OF_EARLY_DATA) {
Jerry Yud5c34962023-12-01 16:32:31 +08003004 unsigned char *buf;
3005 size_t buf_len;
3006
3007 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_tls13_fetch_handshake_msg(
3008 ssl, MBEDTLS_SSL_HS_END_OF_EARLY_DATA,
3009 &buf, &buf_len));
3010
3011 MBEDTLS_SSL_PROC_CHK(ssl_tls13_parse_end_of_early_data(
3012 ssl, buf, buf + buf_len));
3013
Jerry Yue9655122023-12-01 16:44:40 +08003014 MBEDTLS_SSL_DEBUG_MSG(
3015 1, ("Switch to handshake keys for inbound traffic"
3016 "( K_recv = handshake )"));
3017 mbedtls_ssl_set_inbound_transform(
3018 ssl, ssl->handshake->transform_handshake);
3019
Jerry Yud5c34962023-12-01 16:32:31 +08003020 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_add_hs_msg_to_checksum(
3021 ssl, MBEDTLS_SSL_HS_END_OF_EARLY_DATA,
3022 buf, buf_len));
Jerry Yue9655122023-12-01 16:44:40 +08003023
Jerry Yu3be85072023-12-04 09:58:54 +08003024 ssl_tls13_prepare_for_handshake_second_flight(ssl);
Jerry Yud5c34962023-12-01 16:32:31 +08003025
Jerry Yub55f9eb2023-12-05 10:27:17 +08003026 } else if (ret == SSL_GOT_EARLY_DATA) {
Jerry Yud5c34962023-12-01 16:32:31 +08003027 MBEDTLS_SSL_PROC_CHK(ssl_tls13_process_early_application_data(ssl));
3028 } else {
3029 MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
3030 ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
3031 goto cleanup;
3032 }
3033
Jerry Yud5c34962023-12-01 16:32:31 +08003034cleanup:
3035 MBEDTLS_SSL_DEBUG_MSG(2, ("<= ssl_tls13_process_end_of_early_data"));
Jerry Yu87b5ed42022-12-12 13:07:07 +08003036 return ret;
3037}
3038#endif /* MBEDTLS_SSL_EARLY_DATA */
3039
Jerry Yu69dd8d42022-04-16 12:51:26 +08003040/*
Jerry Yud6e253d2022-05-18 13:59:24 +08003041 * Handler for MBEDTLS_SSL_CLIENT_FINISHED
Jerry Yu69dd8d42022-04-16 12:51:26 +08003042 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02003043MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01003044static int ssl_tls13_process_client_finished(mbedtls_ssl_context *ssl)
Jerry Yu69dd8d42022-04-16 12:51:26 +08003045{
Jerry Yud6e253d2022-05-18 13:59:24 +08003046 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
3047
Gilles Peskine449bd832023-01-11 14:50:10 +01003048 ret = mbedtls_ssl_tls13_process_finished_message(ssl);
3049 if (ret != 0) {
3050 return ret;
Jerry Yue3d67cb2022-05-19 15:33:10 +08003051 }
3052
Gilles Peskine449bd832023-01-11 14:50:10 +01003053 ret = mbedtls_ssl_tls13_compute_resumption_master_secret(ssl);
3054 if (ret != 0) {
Xiaokang Qian9f1747b2023-03-30 02:50:04 +00003055 MBEDTLS_SSL_DEBUG_RET(
3056 1, "mbedtls_ssl_tls13_compute_resumption_master_secret", ret);
Gilles Peskine449bd832023-01-11 14:50:10 +01003057 }
3058
3059 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_HANDSHAKE_WRAPUP);
3060 return 0;
Jerry Yu69dd8d42022-04-16 12:51:26 +08003061}
3062
3063/*
Jerry Yud6e253d2022-05-18 13:59:24 +08003064 * Handler for MBEDTLS_SSL_HANDSHAKE_WRAPUP
Jerry Yu69dd8d42022-04-16 12:51:26 +08003065 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02003066MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01003067static int ssl_tls13_handshake_wrapup(mbedtls_ssl_context *ssl)
Jerry Yu69dd8d42022-04-16 12:51:26 +08003068{
Gilles Peskine449bd832023-01-11 14:50:10 +01003069 MBEDTLS_SSL_DEBUG_MSG(2, ("handshake: done"));
Jerry Yu03ed50b2022-04-16 17:13:30 +08003070
Gilles Peskine449bd832023-01-11 14:50:10 +01003071 mbedtls_ssl_tls13_handshake_wrapup(ssl);
Jerry Yue67bef42022-07-07 07:29:42 +00003072
Pengyu Lv3643fdb2023-01-12 16:46:28 +08003073#if defined(MBEDTLS_SSL_SESSION_TICKETS) && \
3074 defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED)
Pengyu Lva1aa31b2022-12-13 13:49:59 +08003075/* TODO: Remove the check of SOME_PSK_ENABLED since SESSION_TICKETS requires
3076 * SOME_PSK_ENABLED to be enabled. Here is just to make CI happy. It is
3077 * expected to be resolved with issue#6395.
3078 */
Pengyu Lvc7af2c42022-12-01 16:33:00 +08003079 /* Sent NewSessionTicket message only when client supports PSK */
Pengyu Lv3643fdb2023-01-12 16:46:28 +08003080 if (mbedtls_ssl_tls13_some_psk_enabled(ssl)) {
Xiaokang Qian9f1747b2023-03-30 02:50:04 +00003081 mbedtls_ssl_handshake_set_state(
3082 ssl, MBEDTLS_SSL_TLS1_3_NEW_SESSION_TICKET);
Pengyu Lvc7af2c42022-12-01 16:33:00 +08003083 } else
Jerry Yufca4d572022-07-21 10:37:48 +08003084#endif
Pengyu Lv3643fdb2023-01-12 16:46:28 +08003085 {
3086 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_HANDSHAKE_OVER);
3087 }
Gilles Peskine449bd832023-01-11 14:50:10 +01003088 return 0;
Jerry Yu69dd8d42022-04-16 12:51:26 +08003089}
3090
3091/*
Jerry Yua8d3c502022-10-30 14:51:23 +08003092 * Handler for MBEDTLS_SSL_TLS1_3_NEW_SESSION_TICKET
Jerry Yue67bef42022-07-07 07:29:42 +00003093 */
3094#define SSL_NEW_SESSION_TICKET_SKIP 0
3095#define SSL_NEW_SESSION_TICKET_WRITE 1
3096MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01003097static int ssl_tls13_write_new_session_ticket_coordinate(mbedtls_ssl_context *ssl)
Jerry Yue67bef42022-07-07 07:29:42 +00003098{
3099 /* Check whether the use of session tickets is enabled */
Gilles Peskine449bd832023-01-11 14:50:10 +01003100 if (ssl->conf->f_ticket_write == NULL) {
3101 MBEDTLS_SSL_DEBUG_MSG(2, ("NewSessionTicket: disabled,"
3102 " callback is not set"));
3103 return SSL_NEW_SESSION_TICKET_SKIP;
Jerry Yud0766ec2022-09-22 10:46:57 +08003104 }
Gilles Peskine449bd832023-01-11 14:50:10 +01003105 if (ssl->conf->new_session_tickets_count == 0) {
3106 MBEDTLS_SSL_DEBUG_MSG(2, ("NewSessionTicket: disabled,"
3107 " configured count is zero"));
3108 return SSL_NEW_SESSION_TICKET_SKIP;
Jerry Yud0766ec2022-09-22 10:46:57 +08003109 }
3110
Gilles Peskine449bd832023-01-11 14:50:10 +01003111 if (ssl->handshake->new_session_tickets_count == 0) {
3112 MBEDTLS_SSL_DEBUG_MSG(2, ("NewSessionTicket: all tickets have "
3113 "been sent."));
3114 return SSL_NEW_SESSION_TICKET_SKIP;
Jerry Yue67bef42022-07-07 07:29:42 +00003115 }
3116
Gilles Peskine449bd832023-01-11 14:50:10 +01003117 return SSL_NEW_SESSION_TICKET_WRITE;
Jerry Yue67bef42022-07-07 07:29:42 +00003118}
3119
3120#if defined(MBEDTLS_SSL_SESSION_TICKETS)
3121MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01003122static int ssl_tls13_prepare_new_session_ticket(mbedtls_ssl_context *ssl,
3123 unsigned char *ticket_nonce,
3124 size_t ticket_nonce_size)
Jerry Yue67bef42022-07-07 07:29:42 +00003125{
3126 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
3127 mbedtls_ssl_session *session = ssl->session;
3128 mbedtls_ssl_ciphersuite_t *ciphersuite_info;
3129 psa_algorithm_t psa_hash_alg;
3130 int hash_length;
3131
Gilles Peskine449bd832023-01-11 14:50:10 +01003132 MBEDTLS_SSL_DEBUG_MSG(2, ("=> prepare NewSessionTicket msg"));
Jerry Yue67bef42022-07-07 07:29:42 +00003133
3134#if defined(MBEDTLS_HAVE_TIME)
Jerry Yu25ba4d42023-11-10 14:12:20 +08003135 session->ticket_creation_time = mbedtls_ms_time();
Jerry Yue67bef42022-07-07 07:29:42 +00003136#endif
3137
Pengyu Lv9f926952022-11-17 15:22:33 +08003138 /* Set ticket_flags depends on the advertised psk key exchange mode */
Pengyu Lv80270b22023-01-12 11:54:04 +08003139 mbedtls_ssl_session_clear_ticket_flags(
Pengyu Lv9eacb442022-12-09 14:39:19 +08003140 session, MBEDTLS_SSL_TLS1_3_TICKET_FLAGS_MASK);
Pengyu Lve6487fe2022-12-06 09:30:29 +08003141#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED)
Pengyu Lv80270b22023-01-12 11:54:04 +08003142 mbedtls_ssl_session_set_ticket_flags(
Pengyu Lv9eacb442022-12-09 14:39:19 +08003143 session, ssl->handshake->tls13_kex_modes);
Pengyu Lve6487fe2022-12-06 09:30:29 +08003144#endif
Pengyu Lv4938a562023-01-16 11:28:49 +08003145 MBEDTLS_SSL_PRINT_TICKET_FLAGS(4, session->ticket_flags);
Pengyu Lv9f926952022-11-17 15:22:33 +08003146
Jerry Yue67bef42022-07-07 07:29:42 +00003147 /* Generate ticket_age_add */
Gilles Peskine449bd832023-01-11 14:50:10 +01003148 if ((ret = ssl->conf->f_rng(ssl->conf->p_rng,
3149 (unsigned char *) &session->ticket_age_add,
3150 sizeof(session->ticket_age_add)) != 0)) {
3151 MBEDTLS_SSL_DEBUG_RET(1, "generate_ticket_age_add", ret);
3152 return ret;
Jerry Yue67bef42022-07-07 07:29:42 +00003153 }
Gilles Peskine449bd832023-01-11 14:50:10 +01003154 MBEDTLS_SSL_DEBUG_MSG(3, ("ticket_age_add: %u",
3155 (unsigned int) session->ticket_age_add));
Jerry Yue67bef42022-07-07 07:29:42 +00003156
3157 /* Generate ticket_nonce */
Gilles Peskine449bd832023-01-11 14:50:10 +01003158 ret = ssl->conf->f_rng(ssl->conf->p_rng, ticket_nonce, ticket_nonce_size);
3159 if (ret != 0) {
3160 MBEDTLS_SSL_DEBUG_RET(1, "generate_ticket_nonce", ret);
3161 return ret;
Jerry Yue67bef42022-07-07 07:29:42 +00003162 }
Gilles Peskine449bd832023-01-11 14:50:10 +01003163 MBEDTLS_SSL_DEBUG_BUF(3, "ticket_nonce:",
3164 ticket_nonce, ticket_nonce_size);
Jerry Yue67bef42022-07-07 07:29:42 +00003165
3166 ciphersuite_info =
Gilles Peskine449bd832023-01-11 14:50:10 +01003167 (mbedtls_ssl_ciphersuite_t *) ssl->handshake->ciphersuite_info;
Dave Rodgman2eab4622023-10-05 13:30:37 +01003168 psa_hash_alg = mbedtls_md_psa_alg_from_type((mbedtls_md_type_t) ciphersuite_info->mac);
Gilles Peskine449bd832023-01-11 14:50:10 +01003169 hash_length = PSA_HASH_LENGTH(psa_hash_alg);
3170 if (hash_length == -1 ||
3171 (size_t) hash_length > sizeof(session->resumption_key)) {
3172 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Jerry Yufca4d572022-07-21 10:37:48 +08003173 }
3174
Jerry Yue67bef42022-07-07 07:29:42 +00003175 /* In this code the psk key length equals the length of the hash */
3176 session->resumption_key_len = hash_length;
3177 session->ciphersuite = ciphersuite_info->id;
3178
3179 /* Compute resumption key
3180 *
3181 * HKDF-Expand-Label( resumption_master_secret,
3182 * "resumption", ticket_nonce, Hash.length )
3183 */
3184 ret = mbedtls_ssl_tls13_hkdf_expand_label(
Gilles Peskine449bd832023-01-11 14:50:10 +01003185 psa_hash_alg,
3186 session->app_secrets.resumption_master_secret,
3187 hash_length,
3188 MBEDTLS_SSL_TLS1_3_LBL_WITH_LEN(resumption),
3189 ticket_nonce,
3190 ticket_nonce_size,
3191 session->resumption_key,
3192 hash_length);
Jerry Yue67bef42022-07-07 07:29:42 +00003193
Gilles Peskine449bd832023-01-11 14:50:10 +01003194 if (ret != 0) {
3195 MBEDTLS_SSL_DEBUG_RET(2,
3196 "Creating the ticket-resumed PSK failed",
3197 ret);
3198 return ret;
Jerry Yue67bef42022-07-07 07:29:42 +00003199 }
Gilles Peskine449bd832023-01-11 14:50:10 +01003200 MBEDTLS_SSL_DEBUG_BUF(3, "Ticket-resumed PSK",
3201 session->resumption_key,
3202 session->resumption_key_len);
Jerry Yue67bef42022-07-07 07:29:42 +00003203
Gilles Peskine449bd832023-01-11 14:50:10 +01003204 MBEDTLS_SSL_DEBUG_BUF(3, "resumption_master_secret",
3205 session->app_secrets.resumption_master_secret,
3206 hash_length);
Jerry Yue67bef42022-07-07 07:29:42 +00003207
Gilles Peskine449bd832023-01-11 14:50:10 +01003208 return 0;
Jerry Yue67bef42022-07-07 07:29:42 +00003209}
3210
3211/* This function creates a NewSessionTicket message in the following format:
3212 *
3213 * struct {
3214 * uint32 ticket_lifetime;
3215 * uint32 ticket_age_add;
3216 * opaque ticket_nonce<0..255>;
3217 * opaque ticket<1..2^16-1>;
3218 * Extension extensions<0..2^16-2>;
3219 * } NewSessionTicket;
3220 *
3221 * The ticket inside the NewSessionTicket message is an encrypted container
3222 * carrying the necessary information so that the server is later able to
3223 * re-start the communication.
3224 *
3225 * The following fields are placed inside the ticket by the
3226 * f_ticket_write() function:
3227 *
Jerry Yu0069abc2023-11-23 21:07:28 +08003228 * - creation time (ticket_creation_time)
3229 * - flags (ticket_flags)
Jerry Yue67bef42022-07-07 07:29:42 +00003230 * - age add (ticket_age_add)
Jerry Yu0069abc2023-11-23 21:07:28 +08003231 * - key (resumption_key)
3232 * - key length (resumption_key_len)
Jerry Yue67bef42022-07-07 07:29:42 +00003233 * - ciphersuite (ciphersuite)
Jerry Yu0069abc2023-11-23 21:07:28 +08003234 * - max_early_data_size (max_early_data_size)
Jerry Yue67bef42022-07-07 07:29:42 +00003235 */
3236MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01003237static int ssl_tls13_write_new_session_ticket_body(mbedtls_ssl_context *ssl,
3238 unsigned char *buf,
3239 unsigned char *end,
3240 size_t *out_len,
3241 unsigned char *ticket_nonce,
3242 size_t ticket_nonce_size)
Jerry Yue67bef42022-07-07 07:29:42 +00003243{
3244 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
3245 unsigned char *p = buf;
3246 mbedtls_ssl_session *session = ssl->session;
3247 size_t ticket_len;
3248 uint32_t ticket_lifetime;
Jerry Yu01da35e2022-12-12 15:09:22 +08003249 unsigned char *p_extensions_len;
3250 size_t output_len;
3251
3252 ((void) output_len);
Jerry Yue67bef42022-07-07 07:29:42 +00003253
3254 *out_len = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01003255 MBEDTLS_SSL_DEBUG_MSG(2, ("=> write NewSessionTicket msg"));
Jerry Yue67bef42022-07-07 07:29:42 +00003256
Jerry Yu01da35e2022-12-12 15:09:22 +08003257#if defined(MBEDTLS_SSL_EARLY_DATA)
Jerry Yuf135bac2023-11-23 18:10:51 +08003258 if (ssl->conf->early_data_enabled == MBEDTLS_SSL_EARLY_DATA_ENABLED &&
3259 ssl->conf->max_early_data_size > 0) {
Jerry Yu10795a02023-11-22 12:29:17 +08003260 mbedtls_ssl_session_set_ticket_flags(
3261 session, MBEDTLS_SSL_TLS1_3_TICKET_ALLOW_EARLY_DATA);
Jerry Yu1a160702023-11-23 18:17:38 +08003262 /* In resumption connection, server get `max_early_data_size` from
3263 * ticket. */
3264 session->max_early_data_size = ssl->conf->max_early_data_size;
Jerry Yu01da35e2022-12-12 15:09:22 +08003265 }
3266#endif /* MBEDTLS_SSL_EARLY_DATA */
3267
Jerry Yue67bef42022-07-07 07:29:42 +00003268 /*
3269 * ticket_lifetime 4 bytes
3270 * ticket_age_add 4 bytes
3271 * ticket_nonce 1 + ticket_nonce_size bytes
3272 * ticket >=2 bytes
3273 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003274 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 4 + 4 + 1 + ticket_nonce_size + 2);
Jerry Yue67bef42022-07-07 07:29:42 +00003275
3276 /* Generate ticket and ticket_lifetime */
Gilles Peskine449bd832023-01-11 14:50:10 +01003277 ret = ssl->conf->f_ticket_write(ssl->conf->p_ticket,
3278 session,
3279 p + 9 + ticket_nonce_size + 2,
3280 end,
3281 &ticket_len,
3282 &ticket_lifetime);
3283 if (ret != 0) {
3284 MBEDTLS_SSL_DEBUG_RET(1, "write_ticket", ret);
3285 return ret;
Jerry Yue67bef42022-07-07 07:29:42 +00003286 }
3287 /* RFC 8446 4.6.1
3288 * ticket_lifetime: Indicates the lifetime in seconds as a 32-bit
3289 * unsigned integer in network byte order from the time of ticket
3290 * issuance. Servers MUST NOT use any value greater than
3291 * 604800 seconds (7 days). The value of zero indicates that the
3292 * ticket should be discarded immediately. Clients MUST NOT cache
3293 * tickets for longer than 7 days, regardless of the ticket_lifetime,
3294 * and MAY delete tickets earlier based on local policy. A server
3295 * MAY treat a ticket as valid for a shorter period of time than what
3296 * is stated in the ticket_lifetime.
3297 */
Jerry Yu8e0174a2023-11-10 13:58:16 +08003298 if (ticket_lifetime > MBEDTLS_SSL_TLS1_3_MAX_ALLOWED_TICKET_LIFETIME) {
3299 ticket_lifetime = MBEDTLS_SSL_TLS1_3_MAX_ALLOWED_TICKET_LIFETIME;
Gilles Peskine449bd832023-01-11 14:50:10 +01003300 }
3301 MBEDTLS_PUT_UINT32_BE(ticket_lifetime, p, 0);
3302 MBEDTLS_SSL_DEBUG_MSG(3, ("ticket_lifetime: %u",
3303 (unsigned int) ticket_lifetime));
Jerry Yue67bef42022-07-07 07:29:42 +00003304
3305 /* Write ticket_age_add */
Gilles Peskine449bd832023-01-11 14:50:10 +01003306 MBEDTLS_PUT_UINT32_BE(session->ticket_age_add, p, 4);
3307 MBEDTLS_SSL_DEBUG_MSG(3, ("ticket_age_add: %u",
3308 (unsigned int) session->ticket_age_add));
Jerry Yue67bef42022-07-07 07:29:42 +00003309
3310 /* Write ticket_nonce */
Gilles Peskine449bd832023-01-11 14:50:10 +01003311 p[8] = (unsigned char) ticket_nonce_size;
3312 if (ticket_nonce_size > 0) {
3313 memcpy(p + 9, ticket_nonce, ticket_nonce_size);
Jerry Yue67bef42022-07-07 07:29:42 +00003314 }
3315 p += 9 + ticket_nonce_size;
3316
3317 /* Write ticket */
Gilles Peskine449bd832023-01-11 14:50:10 +01003318 MBEDTLS_PUT_UINT16_BE(ticket_len, p, 0);
Jerry Yue67bef42022-07-07 07:29:42 +00003319 p += 2;
Gilles Peskine449bd832023-01-11 14:50:10 +01003320 MBEDTLS_SSL_DEBUG_BUF(4, "ticket", p, ticket_len);
Jerry Yue67bef42022-07-07 07:29:42 +00003321 p += ticket_len;
3322
3323 /* Ticket Extensions
3324 *
Jerry Yu01da35e2022-12-12 15:09:22 +08003325 * Extension extensions<0..2^16-2>;
Jerry Yue67bef42022-07-07 07:29:42 +00003326 */
Jerry Yuedab6372022-10-31 14:37:31 +08003327 ssl->handshake->sent_extensions = MBEDTLS_SSL_EXT_MASK_NONE;
3328
Gilles Peskine449bd832023-01-11 14:50:10 +01003329 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 2);
Jerry Yu01da35e2022-12-12 15:09:22 +08003330 p_extensions_len = p;
Jerry Yue67bef42022-07-07 07:29:42 +00003331 p += 2;
3332
Jerry Yu01da35e2022-12-12 15:09:22 +08003333#if defined(MBEDTLS_SSL_EARLY_DATA)
Jerry Yuf135bac2023-11-23 18:10:51 +08003334 if (ssl->conf->early_data_enabled == MBEDTLS_SSL_EARLY_DATA_ENABLED &&
3335 ssl->conf->max_early_data_size > 0) {
3336 if ((ret = mbedtls_ssl_tls13_write_early_data_ext(
Jerry Yu52335392023-11-23 18:06:06 +08003337 ssl, p, end, &output_len, session)) != 0) {
Jerry Yuf135bac2023-11-23 18:10:51 +08003338 MBEDTLS_SSL_DEBUG_RET(
3339 1, "mbedtls_ssl_tls13_write_early_data_ext", ret);
3340 return ret;
3341 }
3342 p += output_len;
Jerry Yu01da35e2022-12-12 15:09:22 +08003343 }
Jerry Yuf135bac2023-11-23 18:10:51 +08003344
Jerry Yu01da35e2022-12-12 15:09:22 +08003345#endif /* MBEDTLS_SSL_EARLY_DATA */
3346
3347 MBEDTLS_PUT_UINT16_BE(p - p_extensions_len - 2, p_extensions_len, 0);
3348
Jerry Yue67bef42022-07-07 07:29:42 +00003349 *out_len = p - buf;
Gilles Peskine449bd832023-01-11 14:50:10 +01003350 MBEDTLS_SSL_DEBUG_BUF(4, "ticket", buf, *out_len);
3351 MBEDTLS_SSL_DEBUG_MSG(2, ("<= write new session ticket"));
Jerry Yue67bef42022-07-07 07:29:42 +00003352
Jerry Yu7de2ff02022-11-08 21:43:46 +08003353 MBEDTLS_SSL_PRINT_EXTS(
Gilles Peskine449bd832023-01-11 14:50:10 +01003354 3, MBEDTLS_SSL_HS_NEW_SESSION_TICKET, ssl->handshake->sent_extensions);
Jerry Yu4b8f2f72022-10-31 13:31:22 +08003355
Gilles Peskine449bd832023-01-11 14:50:10 +01003356 return 0;
Jerry Yue67bef42022-07-07 07:29:42 +00003357}
3358
3359/*
Jerry Yua8d3c502022-10-30 14:51:23 +08003360 * Handler for MBEDTLS_SSL_TLS1_3_NEW_SESSION_TICKET
Jerry Yue67bef42022-07-07 07:29:42 +00003361 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003362static int ssl_tls13_write_new_session_ticket(mbedtls_ssl_context *ssl)
Jerry Yue67bef42022-07-07 07:29:42 +00003363{
3364 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
3365
Gilles Peskine449bd832023-01-11 14:50:10 +01003366 MBEDTLS_SSL_PROC_CHK_NEG(ssl_tls13_write_new_session_ticket_coordinate(ssl));
Jerry Yue67bef42022-07-07 07:29:42 +00003367
Gilles Peskine449bd832023-01-11 14:50:10 +01003368 if (ret == SSL_NEW_SESSION_TICKET_WRITE) {
Jerry Yue67bef42022-07-07 07:29:42 +00003369 unsigned char ticket_nonce[MBEDTLS_SSL_TLS1_3_TICKET_NONCE_LENGTH];
3370 unsigned char *buf;
3371 size_t buf_len, msg_len;
3372
Gilles Peskine449bd832023-01-11 14:50:10 +01003373 MBEDTLS_SSL_PROC_CHK(ssl_tls13_prepare_new_session_ticket(
3374 ssl, ticket_nonce, sizeof(ticket_nonce)));
Jerry Yue67bef42022-07-07 07:29:42 +00003375
Xiaokang Qian9f1747b2023-03-30 02:50:04 +00003376 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_start_handshake_msg(
3377 ssl, MBEDTLS_SSL_HS_NEW_SESSION_TICKET,
3378 &buf, &buf_len));
Jerry Yue67bef42022-07-07 07:29:42 +00003379
Gilles Peskine449bd832023-01-11 14:50:10 +01003380 MBEDTLS_SSL_PROC_CHK(ssl_tls13_write_new_session_ticket_body(
3381 ssl, buf, buf + buf_len, &msg_len,
3382 ticket_nonce, sizeof(ticket_nonce)));
Jerry Yue67bef42022-07-07 07:29:42 +00003383
Gilles Peskine449bd832023-01-11 14:50:10 +01003384 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_finish_handshake_msg(
3385 ssl, buf_len, msg_len));
Jerry Yufca4d572022-07-21 10:37:48 +08003386
Jerry Yu359e65f2022-09-22 23:47:43 +08003387 /* Limit session tickets count to one when resumption connection.
3388 *
3389 * See document of mbedtls_ssl_conf_new_session_tickets.
3390 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003391 if (ssl->handshake->resume == 1) {
Jerry Yu359e65f2022-09-22 23:47:43 +08003392 ssl->handshake->new_session_tickets_count = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01003393 } else {
Jerry Yu359e65f2022-09-22 23:47:43 +08003394 ssl->handshake->new_session_tickets_count--;
Gilles Peskine449bd832023-01-11 14:50:10 +01003395 }
Jerry Yub7e3fa72022-09-22 11:07:18 +08003396
Jerry Yua8d3c502022-10-30 14:51:23 +08003397 mbedtls_ssl_handshake_set_state(
Gilles Peskine449bd832023-01-11 14:50:10 +01003398 ssl, MBEDTLS_SSL_TLS1_3_NEW_SESSION_TICKET_FLUSH);
3399 } else {
3400 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_HANDSHAKE_OVER);
Jerry Yue67bef42022-07-07 07:29:42 +00003401 }
3402
Jerry Yue67bef42022-07-07 07:29:42 +00003403cleanup:
3404
Gilles Peskine449bd832023-01-11 14:50:10 +01003405 return ret;
Jerry Yue67bef42022-07-07 07:29:42 +00003406}
3407#endif /* MBEDTLS_SSL_SESSION_TICKETS */
3408
3409/*
XiaokangQiane8ff3502022-04-22 02:34:40 +00003410 * TLS 1.3 State Machine -- server side
XiaokangQian7807f9f2022-02-15 10:04:37 +00003411 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003412int mbedtls_ssl_tls13_handshake_server_step(mbedtls_ssl_context *ssl)
Jerry Yub9930e72021-08-06 17:11:51 +08003413{
XiaokangQian08037552022-04-20 07:16:41 +00003414 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
XiaokangQian7807f9f2022-02-15 10:04:37 +00003415
Gilles Peskine449bd832023-01-11 14:50:10 +01003416 if (ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER || ssl->handshake == NULL) {
3417 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
3418 }
XiaokangQian7807f9f2022-02-15 10:04:37 +00003419
Gilles Peskine449bd832023-01-11 14:50:10 +01003420 MBEDTLS_SSL_DEBUG_MSG(2, ("tls13 server state: %s(%d)",
Dave Rodgman2eab4622023-10-05 13:30:37 +01003421 mbedtls_ssl_states_str((mbedtls_ssl_states) ssl->state),
Gilles Peskine449bd832023-01-11 14:50:10 +01003422 ssl->state));
Jerry Yu6e81b272021-09-27 11:16:17 +08003423
Gilles Peskine449bd832023-01-11 14:50:10 +01003424 switch (ssl->state) {
XiaokangQian7807f9f2022-02-15 10:04:37 +00003425 /* start state */
3426 case MBEDTLS_SSL_HELLO_REQUEST:
Gilles Peskine449bd832023-01-11 14:50:10 +01003427 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_CLIENT_HELLO);
XiaokangQian08037552022-04-20 07:16:41 +00003428 ret = 0;
XiaokangQian7807f9f2022-02-15 10:04:37 +00003429 break;
3430
XiaokangQian7807f9f2022-02-15 10:04:37 +00003431 case MBEDTLS_SSL_CLIENT_HELLO:
Gilles Peskine449bd832023-01-11 14:50:10 +01003432 ret = ssl_tls13_process_client_hello(ssl);
3433 if (ret != 0) {
3434 MBEDTLS_SSL_DEBUG_RET(1, "ssl_tls13_process_client_hello", ret);
3435 }
Jerry Yuf41553b2022-05-09 22:20:30 +08003436 break;
XiaokangQian7807f9f2022-02-15 10:04:37 +00003437
Jerry Yuf41553b2022-05-09 22:20:30 +08003438 case MBEDTLS_SSL_HELLO_RETRY_REQUEST:
Gilles Peskine449bd832023-01-11 14:50:10 +01003439 ret = ssl_tls13_write_hello_retry_request(ssl);
3440 if (ret != 0) {
3441 MBEDTLS_SSL_DEBUG_RET(1, "ssl_tls13_write_hello_retry_request", ret);
3442 return ret;
Jerry Yuf41553b2022-05-09 22:20:30 +08003443 }
XiaokangQian7807f9f2022-02-15 10:04:37 +00003444 break;
3445
Jerry Yu5b64ae92022-03-30 17:15:02 +08003446 case MBEDTLS_SSL_SERVER_HELLO:
Gilles Peskine449bd832023-01-11 14:50:10 +01003447 ret = ssl_tls13_write_server_hello(ssl);
Jerry Yu5b64ae92022-03-30 17:15:02 +08003448 break;
3449
Xiaofei Baicba64af2022-02-15 10:00:56 +00003450 case MBEDTLS_SSL_ENCRYPTED_EXTENSIONS:
Gilles Peskine449bd832023-01-11 14:50:10 +01003451 ret = ssl_tls13_write_encrypted_extensions(ssl);
3452 if (ret != 0) {
3453 MBEDTLS_SSL_DEBUG_RET(1, "ssl_tls13_write_encrypted_extensions", ret);
3454 return ret;
Xiaofei Baicba64af2022-02-15 10:00:56 +00003455 }
Jerry Yu48330562022-05-06 21:35:44 +08003456 break;
Jerry Yu6a2cd9e2022-05-05 11:14:19 +08003457
Ronald Cron928cbd32022-10-04 16:14:26 +02003458#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED)
Xiaofei Bai9ca09d42022-02-14 12:57:18 +00003459 case MBEDTLS_SSL_CERTIFICATE_REQUEST:
Gilles Peskine449bd832023-01-11 14:50:10 +01003460 ret = ssl_tls13_write_certificate_request(ssl);
Xiaofei Bai9ca09d42022-02-14 12:57:18 +00003461 break;
Xiaofei Bai9ca09d42022-02-14 12:57:18 +00003462
Jerry Yu83da34e2022-04-16 13:59:52 +08003463 case MBEDTLS_SSL_SERVER_CERTIFICATE:
Gilles Peskine449bd832023-01-11 14:50:10 +01003464 ret = ssl_tls13_write_server_certificate(ssl);
Jerry Yu83da34e2022-04-16 13:59:52 +08003465 break;
3466
3467 case MBEDTLS_SSL_CERTIFICATE_VERIFY:
Gilles Peskine449bd832023-01-11 14:50:10 +01003468 ret = ssl_tls13_write_certificate_verify(ssl);
Jerry Yu83da34e2022-04-16 13:59:52 +08003469 break;
Ronald Cron928cbd32022-10-04 16:14:26 +02003470#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED */
Jerry Yu83da34e2022-04-16 13:59:52 +08003471
Gilles Peskine449bd832023-01-11 14:50:10 +01003472 /*
3473 * Injection of dummy-CCS's for middlebox compatibility
3474 */
Gabor Mezei7b39bf12022-05-24 16:04:14 +02003475#if defined(MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE)
Gabor Mezeif7044ea2022-06-28 16:01:49 +02003476 case MBEDTLS_SSL_SERVER_CCS_AFTER_HELLO_RETRY_REQUEST:
Gilles Peskine449bd832023-01-11 14:50:10 +01003477 ret = mbedtls_ssl_tls13_write_change_cipher_spec(ssl);
3478 if (ret == 0) {
3479 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_CLIENT_HELLO);
3480 }
Gabor Mezei7b39bf12022-05-24 16:04:14 +02003481 break;
3482
3483 case MBEDTLS_SSL_SERVER_CCS_AFTER_SERVER_HELLO:
Gilles Peskine449bd832023-01-11 14:50:10 +01003484 ret = mbedtls_ssl_tls13_write_change_cipher_spec(ssl);
3485 if (ret == 0) {
3486 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_ENCRYPTED_EXTENSIONS);
3487 }
Gabor Mezei7b39bf12022-05-24 16:04:14 +02003488 break;
3489#endif /* MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE */
3490
Jerry Yu69dd8d42022-04-16 12:51:26 +08003491 case MBEDTLS_SSL_SERVER_FINISHED:
Gilles Peskine449bd832023-01-11 14:50:10 +01003492 ret = ssl_tls13_write_server_finished(ssl);
Jerry Yu69dd8d42022-04-16 12:51:26 +08003493 break;
3494
Jerry Yu87b5ed42022-12-12 13:07:07 +08003495#if defined(MBEDTLS_SSL_EARLY_DATA)
3496 case MBEDTLS_SSL_END_OF_EARLY_DATA:
Jerry Yu59d420f2023-12-01 16:30:34 +08003497 ret = ssl_tls13_process_end_of_early_data(ssl);
Jerry Yu87b5ed42022-12-12 13:07:07 +08003498 break;
3499#endif /* MBEDTLS_SSL_EARLY_DATA */
3500
Jerry Yu69dd8d42022-04-16 12:51:26 +08003501 case MBEDTLS_SSL_CLIENT_FINISHED:
Gilles Peskine449bd832023-01-11 14:50:10 +01003502 ret = ssl_tls13_process_client_finished(ssl);
Jerry Yu69dd8d42022-04-16 12:51:26 +08003503 break;
3504
Jerry Yu69dd8d42022-04-16 12:51:26 +08003505 case MBEDTLS_SSL_HANDSHAKE_WRAPUP:
Gilles Peskine449bd832023-01-11 14:50:10 +01003506 ret = ssl_tls13_handshake_wrapup(ssl);
Jerry Yu69dd8d42022-04-16 12:51:26 +08003507 break;
3508
Ronald Cron766c0cd2022-10-18 12:17:11 +02003509#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED)
XiaokangQian6b916b12022-04-25 07:29:34 +00003510 case MBEDTLS_SSL_CLIENT_CERTIFICATE:
Gilles Peskine449bd832023-01-11 14:50:10 +01003511 ret = mbedtls_ssl_tls13_process_certificate(ssl);
3512 if (ret == 0) {
3513 if (ssl->session_negotiate->peer_cert != NULL) {
Ronald Cron209cae92022-06-07 10:30:19 +02003514 mbedtls_ssl_handshake_set_state(
Gilles Peskine449bd832023-01-11 14:50:10 +01003515 ssl, MBEDTLS_SSL_CLIENT_CERTIFICATE_VERIFY);
3516 } else {
3517 MBEDTLS_SSL_DEBUG_MSG(2, ("skip parse certificate verify"));
Ronald Cron209cae92022-06-07 10:30:19 +02003518 mbedtls_ssl_handshake_set_state(
Gilles Peskine449bd832023-01-11 14:50:10 +01003519 ssl, MBEDTLS_SSL_CLIENT_FINISHED);
Ronald Cron19385882022-06-15 16:26:13 +02003520 }
XiaokangQian6b916b12022-04-25 07:29:34 +00003521 }
3522 break;
3523
3524 case MBEDTLS_SSL_CLIENT_CERTIFICATE_VERIFY:
Gilles Peskine449bd832023-01-11 14:50:10 +01003525 ret = mbedtls_ssl_tls13_process_certificate_verify(ssl);
3526 if (ret == 0) {
XiaokangQian6b916b12022-04-25 07:29:34 +00003527 mbedtls_ssl_handshake_set_state(
Gilles Peskine449bd832023-01-11 14:50:10 +01003528 ssl, MBEDTLS_SSL_CLIENT_FINISHED);
XiaokangQian6b916b12022-04-25 07:29:34 +00003529 }
3530 break;
Ronald Cron766c0cd2022-10-18 12:17:11 +02003531#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED */
XiaokangQian6b916b12022-04-25 07:29:34 +00003532
Jerry Yue67bef42022-07-07 07:29:42 +00003533#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Jerry Yua8d3c502022-10-30 14:51:23 +08003534 case MBEDTLS_SSL_TLS1_3_NEW_SESSION_TICKET:
Gilles Peskine449bd832023-01-11 14:50:10 +01003535 ret = ssl_tls13_write_new_session_ticket(ssl);
3536 if (ret != 0) {
3537 MBEDTLS_SSL_DEBUG_RET(1,
3538 "ssl_tls13_write_new_session_ticket ",
3539 ret);
Jerry Yue67bef42022-07-07 07:29:42 +00003540 }
3541 break;
Jerry Yua8d3c502022-10-30 14:51:23 +08003542 case MBEDTLS_SSL_TLS1_3_NEW_SESSION_TICKET_FLUSH:
Jerry Yue67bef42022-07-07 07:29:42 +00003543 /* This state is necessary to do the flush of the New Session
Jerry Yua8d3c502022-10-30 14:51:23 +08003544 * Ticket message written in MBEDTLS_SSL_TLS1_3_NEW_SESSION_TICKET
Jerry Yue67bef42022-07-07 07:29:42 +00003545 * as part of ssl_prepare_handshake_step.
3546 */
3547 ret = 0;
Jerry Yud4e75002022-08-09 13:33:50 +08003548
Gilles Peskine449bd832023-01-11 14:50:10 +01003549 if (ssl->handshake->new_session_tickets_count == 0) {
3550 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_HANDSHAKE_OVER);
3551 } else {
Xiaokang Qian9f1747b2023-03-30 02:50:04 +00003552 mbedtls_ssl_handshake_set_state(
3553 ssl, MBEDTLS_SSL_TLS1_3_NEW_SESSION_TICKET);
Gilles Peskine449bd832023-01-11 14:50:10 +01003554 }
Jerry Yue67bef42022-07-07 07:29:42 +00003555 break;
3556
3557#endif /* MBEDTLS_SSL_SESSION_TICKETS */
3558
XiaokangQian7807f9f2022-02-15 10:04:37 +00003559 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01003560 MBEDTLS_SSL_DEBUG_MSG(1, ("invalid state %d", ssl->state));
3561 return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
XiaokangQian7807f9f2022-02-15 10:04:37 +00003562 }
3563
Gilles Peskine449bd832023-01-11 14:50:10 +01003564 return ret;
Jerry Yub9930e72021-08-06 17:11:51 +08003565}
Jerry Yu3cc4c2a2021-08-06 16:29:08 +08003566
Jerry Yufb4b6472022-01-27 15:03:26 +08003567#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_PROTO_TLS1_3 */