blob: 744e984acf4c11d0f85e8074416b6cb6ab862bd5 [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;
116 mbedtls_ms_time_t client_age;
117 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 Yuec6d0782023-10-31 14:42:20 +0800196 if (now < session->ticket_creation) {
Gilles Peskine449bd832023-01-11 14:50:10 +0100197 MBEDTLS_SSL_DEBUG_MSG(
Jerry Yuf16efbc2023-10-30 11:06:24 +0800198 3, ("Invalid ticket start time ( now = %" MBEDTLS_PRINTF_MS_TIME
199 ", start = %" MBEDTLS_PRINTF_MS_TIME " )",
Jerry Yuec6d0782023-10-31 14:42:20 +0800200 now, session->ticket_creation));
Gilles Peskine449bd832023-01-11 14:50:10 +0100201 goto exit;
202 }
203
Jerry Yuec6d0782023-10-31 14:42:20 +0800204 server_age = now - session->ticket_creation;
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 *
215 * For time being, the age MUST be less than 604800 seconds (7 days).
216 */
Jerry Yuf16efbc2023-10-30 11:06:24 +0800217 if (server_age > 604800 * 1000) {
Jerry Yu8d4bbba2022-09-13 14:15:48 +0800218 MBEDTLS_SSL_DEBUG_MSG(
Jerry Yucebffc32022-12-15 18:00:05 +0800219 3, ("Ticket age exceeds limitation ticket_age=%" MBEDTLS_PRINTF_MS_TIME,
220 server_age));
Jerry Yu8d4bbba2022-09-13 14:15:48 +0800221 goto exit;
222 }
Jerry Yu95699e72022-08-21 19:22:23 +0800223
Jerry Yu8d4bbba2022-09-13 14:15:48 +0800224 /* RFC 8446 section 4.2.10
225 *
226 * For PSKs provisioned via NewSessionTicket, a server MUST validate that
227 * the ticket age for the selected PSK identity (computed by subtracting
228 * ticket_age_add from PskIdentity.obfuscated_ticket_age modulo 2^32) is
229 * within a small tolerance of the time since the ticket was issued.
Jerry Yuf7dad3c2022-09-14 22:31:39 +0800230 *
Jerry Yucebffc32022-12-15 18:00:05 +0800231 * NOTE: Typical crystal RTC accuracy specifications are from ±100 to ±20
232 * parts per million (360 to 72 million seconds per hour). Defualt
233 * tolerance windows is 6000 millionsections, that means client host
234 * MUST sync up system time every 16 hours. Otherwise, the ticket will
235 * be invalid.
Jerry Yu8d4bbba2022-09-13 14:15:48 +0800236 */
Jerry Yucebffc32022-12-15 18:00:05 +0800237 client_age = obfuscated_ticket_age - session->ticket_age_add;
238 age_diff = server_age - client_age;
239 if (age_diff < -1000 ||
240 age_diff > MBEDTLS_SSL_TLS1_3_TICKET_AGE_TOLERANCE) {
Jerry Yu8d4bbba2022-09-13 14:15:48 +0800241 MBEDTLS_SSL_DEBUG_MSG(
Jerry Yuf16efbc2023-10-30 11:06:24 +0800242 3, ("Ticket age outside tolerance window ( diff = %"
243 MBEDTLS_PRINTF_MS_TIME ")",
Jerry Yucebffc32022-12-15 18:00:05 +0800244 age_diff));
Jerry Yu8d4bbba2022-09-13 14:15:48 +0800245 goto exit;
246 }
247
248 ret = 0;
Jerry Yu95699e72022-08-21 19:22:23 +0800249
250#endif /* MBEDTLS_HAVE_TIME */
Jerry Yu8d4bbba2022-09-13 14:15:48 +0800251
252exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100253 if (ret != 0) {
254 mbedtls_ssl_session_free(session);
255 }
Jerry Yu95699e72022-08-21 19:22:23 +0800256
Gilles Peskine449bd832023-01-11 14:50:10 +0100257 MBEDTLS_SSL_DEBUG_MSG(2, ("<= check_identity_match_ticket"));
258 return ret;
Jerry Yu95699e72022-08-21 19:22:23 +0800259}
260#endif /* MBEDTLS_SSL_SESSION_TICKETS */
261
Jerry Yu6e74a7e2022-07-20 20:49:32 +0800262MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yu1c105562022-07-10 06:32:38 +0000263static int ssl_tls13_offered_psks_check_identity_match(
Gilles Peskine449bd832023-01-11 14:50:10 +0100264 mbedtls_ssl_context *ssl,
265 const unsigned char *identity,
266 size_t identity_len,
267 uint32_t obfuscated_ticket_age,
268 int *psk_type,
269 mbedtls_ssl_session *session)
Jerry Yu1c105562022-07-10 06:32:38 +0000270{
Ronald Cron606671e2023-02-17 11:36:33 +0100271 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
272
Jerry Yu95699e72022-08-21 19:22:23 +0800273 ((void) session);
274 ((void) obfuscated_ticket_age);
Jerry Yu5725f1c2022-08-21 17:27:16 +0800275 *psk_type = MBEDTLS_SSL_TLS1_3_PSK_EXTERNAL;
Jerry Yu95699e72022-08-21 19:22:23 +0800276
Gilles Peskine449bd832023-01-11 14:50:10 +0100277 MBEDTLS_SSL_DEBUG_BUF(4, "identity", identity, identity_len);
Jerry Yu95699e72022-08-21 19:22:23 +0800278 ssl->handshake->resume = 0;
279
Jerry Yu95699e72022-08-21 19:22:23 +0800280#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Gilles Peskine449bd832023-01-11 14:50:10 +0100281 if (ssl_tls13_offered_psks_check_identity_match_ticket(
Jerry Yu8d4bbba2022-09-13 14:15:48 +0800282 ssl, identity, identity_len, obfuscated_ticket_age,
Gilles Peskine449bd832023-01-11 14:50:10 +0100283 session) == SSL_TLS1_3_OFFERED_PSK_MATCH) {
Jerry Yu95699e72022-08-21 19:22:23 +0800284 ssl->handshake->resume = 1;
285 *psk_type = MBEDTLS_SSL_TLS1_3_PSK_RESUMPTION;
Ronald Cron606671e2023-02-17 11:36:33 +0100286 ret = mbedtls_ssl_set_hs_psk(ssl,
287 session->resumption_key,
288 session->resumption_key_len);
289 if (ret != 0) {
290 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_set_hs_psk", ret);
291 return ret;
292 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100293
294 MBEDTLS_SSL_DEBUG_BUF(4, "Ticket-resumed PSK:",
295 session->resumption_key,
296 session->resumption_key_len);
297 MBEDTLS_SSL_DEBUG_MSG(4, ("ticket: obfuscated_ticket_age: %u",
298 (unsigned) obfuscated_ticket_age));
299 return SSL_TLS1_3_OFFERED_PSK_MATCH;
Jerry Yu95699e72022-08-21 19:22:23 +0800300 }
301#endif /* MBEDTLS_SSL_SESSION_TICKETS */
302
Jerry Yu1c105562022-07-10 06:32:38 +0000303 /* Check identity with external configured function */
Gilles Peskine449bd832023-01-11 14:50:10 +0100304 if (ssl->conf->f_psk != NULL) {
305 if (ssl->conf->f_psk(
306 ssl->conf->p_psk, ssl, identity, identity_len) == 0) {
307 return SSL_TLS1_3_OFFERED_PSK_MATCH;
Jerry Yu1c105562022-07-10 06:32:38 +0000308 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100309 return SSL_TLS1_3_OFFERED_PSK_NOT_MATCH;
Jerry Yu1c105562022-07-10 06:32:38 +0000310 }
311
Gilles Peskine449bd832023-01-11 14:50:10 +0100312 MBEDTLS_SSL_DEBUG_BUF(5, "identity", identity, identity_len);
Jerry Yu1c105562022-07-10 06:32:38 +0000313 /* Check identity with pre-configured psk */
Gilles Peskine449bd832023-01-11 14:50:10 +0100314 if (ssl->conf->psk_identity != NULL &&
Jerry Yu2f0abc92022-07-22 19:34:48 +0800315 identity_len == ssl->conf->psk_identity_len &&
Gilles Peskine449bd832023-01-11 14:50:10 +0100316 mbedtls_ct_memcmp(ssl->conf->psk_identity,
317 identity, identity_len) == 0) {
Ronald Cron606671e2023-02-17 11:36:33 +0100318 ret = mbedtls_ssl_set_hs_psk(ssl, ssl->conf->psk, ssl->conf->psk_len);
319 if (ret != 0) {
320 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_set_hs_psk", ret);
321 return ret;
322 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100323 return SSL_TLS1_3_OFFERED_PSK_MATCH;
Jerry Yu1c105562022-07-10 06:32:38 +0000324 }
325
Gilles Peskine449bd832023-01-11 14:50:10 +0100326 return SSL_TLS1_3_OFFERED_PSK_NOT_MATCH;
Jerry Yu1c105562022-07-10 06:32:38 +0000327}
328
Jerry Yu6e74a7e2022-07-20 20:49:32 +0800329MBEDTLS_CHECK_RETURN_CRITICAL
Xiaokang Qian9f1747b2023-03-30 02:50:04 +0000330static int ssl_tls13_offered_psks_check_binder_match(
331 mbedtls_ssl_context *ssl,
332 const unsigned char *binder, size_t binder_len,
333 int psk_type, psa_algorithm_t psk_hash_alg)
Jerry Yu1c105562022-07-10 06:32:38 +0000334{
335 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yu96a2e362022-07-21 15:11:34 +0800336
Jerry Yudaf375a2022-07-20 21:31:43 +0800337 unsigned char transcript[PSA_HASH_MAX_SIZE];
Jerry Yu1c105562022-07-10 06:32:38 +0000338 size_t transcript_len;
Jerry Yu2f0abc92022-07-22 19:34:48 +0800339 unsigned char *psk;
Jerry Yu96a2e362022-07-21 15:11:34 +0800340 size_t psk_len;
Jerry Yudaf375a2022-07-20 21:31:43 +0800341 unsigned char server_computed_binder[PSA_HASH_MAX_SIZE];
Jerry Yu1c105562022-07-10 06:32:38 +0000342
Jerry Yu1c105562022-07-10 06:32:38 +0000343 /* Get current state of handshake transcript. */
Jerry Yu29d9faa2022-08-23 17:52:45 +0800344 ret = mbedtls_ssl_get_handshake_transcript(
Manuel Pégourié-Gonnard2d6d9932023-03-28 11:38:08 +0200345 ssl, mbedtls_md_type_from_psa_alg(psk_hash_alg),
Gilles Peskine449bd832023-01-11 14:50:10 +0100346 transcript, sizeof(transcript), &transcript_len);
347 if (ret != 0) {
348 return ret;
349 }
Jerry Yu1c105562022-07-10 06:32:38 +0000350
Gilles Peskine449bd832023-01-11 14:50:10 +0100351 ret = mbedtls_ssl_tls13_export_handshake_psk(ssl, &psk, &psk_len);
352 if (ret != 0) {
353 return ret;
354 }
Jerry Yu96a2e362022-07-21 15:11:34 +0800355
Gilles Peskine449bd832023-01-11 14:50:10 +0100356 ret = mbedtls_ssl_tls13_create_psk_binder(ssl, psk_hash_alg,
357 psk, psk_len, psk_type,
358 transcript,
359 server_computed_binder);
Jerry Yu96a2e362022-07-21 15:11:34 +0800360#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +0100361 mbedtls_free((void *) psk);
Jerry Yu96a2e362022-07-21 15:11:34 +0800362#endif
Gilles Peskine449bd832023-01-11 14:50:10 +0100363 if (ret != 0) {
364 MBEDTLS_SSL_DEBUG_MSG(1, ("PSK binder calculation failed."));
365 return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
Jerry Yu1c105562022-07-10 06:32:38 +0000366 }
367
Gilles Peskine449bd832023-01-11 14:50:10 +0100368 MBEDTLS_SSL_DEBUG_BUF(3, "psk binder ( computed ): ",
369 server_computed_binder, transcript_len);
370 MBEDTLS_SSL_DEBUG_BUF(3, "psk binder ( received ): ", binder, binder_len);
Jerry Yu1c105562022-07-10 06:32:38 +0000371
Gilles Peskine449bd832023-01-11 14:50:10 +0100372 if (mbedtls_ct_memcmp(server_computed_binder, binder, binder_len) == 0) {
373 return SSL_TLS1_3_OFFERED_PSK_MATCH;
Jerry Yu1c105562022-07-10 06:32:38 +0000374 }
375
Gilles Peskine449bd832023-01-11 14:50:10 +0100376 mbedtls_platform_zeroize(server_computed_binder,
377 sizeof(server_computed_binder));
378 return SSL_TLS1_3_OFFERED_PSK_NOT_MATCH;
Jerry Yu1c105562022-07-10 06:32:38 +0000379}
Jerry Yu96a2e362022-07-21 15:11:34 +0800380
Jerry Yu5725f1c2022-08-21 17:27:16 +0800381MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yuf35ba382022-08-23 17:58:26 +0800382static int ssl_tls13_select_ciphersuite_for_psk(
Gilles Peskine449bd832023-01-11 14:50:10 +0100383 mbedtls_ssl_context *ssl,
384 const unsigned char *cipher_suites,
385 const unsigned char *cipher_suites_end,
386 uint16_t *selected_ciphersuite,
387 const mbedtls_ssl_ciphersuite_t **selected_ciphersuite_info)
Jerry Yu5725f1c2022-08-21 17:27:16 +0800388{
Jerry Yuf35ba382022-08-23 17:58:26 +0800389 psa_algorithm_t psk_hash_alg = PSA_ALG_SHA_256;
Jerry Yu5725f1c2022-08-21 17:27:16 +0800390
Jerry Yu0baf9072022-08-25 11:21:04 +0800391 *selected_ciphersuite = 0;
392 *selected_ciphersuite_info = NULL;
393
Jerry Yuf35ba382022-08-23 17:58:26 +0800394 /* RFC 8446, page 55.
395 *
396 * For externally established PSKs, the Hash algorithm MUST be set when the
397 * PSK is established or default to SHA-256 if no such algorithm is defined.
398 *
399 */
Jerry Yu5725f1c2022-08-21 17:27:16 +0800400
Jerry Yu5725f1c2022-08-21 17:27:16 +0800401 /*
402 * Search for a matching ciphersuite
403 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100404 for (const unsigned char *p = cipher_suites;
405 p < cipher_suites_end; p += 2) {
Jerry Yu5725f1c2022-08-21 17:27:16 +0800406 uint16_t cipher_suite;
Jerry Yuf35ba382022-08-23 17:58:26 +0800407 const mbedtls_ssl_ciphersuite_t *ciphersuite_info;
Jerry Yu5725f1c2022-08-21 17:27:16 +0800408
Gilles Peskine449bd832023-01-11 14:50:10 +0100409 cipher_suite = MBEDTLS_GET_UINT16_BE(p, 0);
410 ciphersuite_info = ssl_tls13_validate_peer_ciphersuite(ssl,
411 cipher_suite);
412 if (ciphersuite_info == NULL) {
Jerry Yu5725f1c2022-08-21 17:27:16 +0800413 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +0100414 }
Jerry Yu5725f1c2022-08-21 17:27:16 +0800415
Jerry Yu5725f1c2022-08-21 17:27:16 +0800416 /* MAC of selected ciphersuite MUST be same with PSK binder if exist.
417 * Otherwise, client should reject.
418 */
Dave Rodgman2eab4622023-10-05 13:30:37 +0100419 if (psk_hash_alg ==
420 mbedtls_md_psa_alg_from_type((mbedtls_md_type_t) ciphersuite_info->mac)) {
Jerry Yuf35ba382022-08-23 17:58:26 +0800421 *selected_ciphersuite = cipher_suite;
422 *selected_ciphersuite_info = ciphersuite_info;
Gilles Peskine449bd832023-01-11 14:50:10 +0100423 return 0;
Jerry Yuf35ba382022-08-23 17:58:26 +0800424 }
425 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100426 MBEDTLS_SSL_DEBUG_MSG(2, ("No matched ciphersuite"));
427 return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
Jerry Yuf35ba382022-08-23 17:58:26 +0800428}
Jerry Yu5725f1c2022-08-21 17:27:16 +0800429
Jerry Yu82534862022-08-30 10:42:33 +0800430#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Jerry Yuf35ba382022-08-23 17:58:26 +0800431MBEDTLS_CHECK_RETURN_CRITICAL
432static int ssl_tls13_select_ciphersuite_for_resumption(
Gilles Peskine449bd832023-01-11 14:50:10 +0100433 mbedtls_ssl_context *ssl,
434 const unsigned char *cipher_suites,
435 const unsigned char *cipher_suites_end,
436 mbedtls_ssl_session *session,
437 uint16_t *selected_ciphersuite,
438 const mbedtls_ssl_ciphersuite_t **selected_ciphersuite_info)
Jerry Yuf35ba382022-08-23 17:58:26 +0800439{
Jerry Yu82534862022-08-30 10:42:33 +0800440
Jerry Yuf35ba382022-08-23 17:58:26 +0800441 *selected_ciphersuite = 0;
442 *selected_ciphersuite_info = NULL;
Gilles Peskine449bd832023-01-11 14:50:10 +0100443 for (const unsigned char *p = cipher_suites; p < cipher_suites_end; p += 2) {
444 uint16_t cipher_suite = MBEDTLS_GET_UINT16_BE(p, 0);
Jerry Yu82534862022-08-30 10:42:33 +0800445 const mbedtls_ssl_ciphersuite_t *ciphersuite_info;
446
Gilles Peskine449bd832023-01-11 14:50:10 +0100447 if (cipher_suite != session->ciphersuite) {
Jerry Yu82534862022-08-30 10:42:33 +0800448 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +0100449 }
Jerry Yu82534862022-08-30 10:42:33 +0800450
Gilles Peskine449bd832023-01-11 14:50:10 +0100451 ciphersuite_info = ssl_tls13_validate_peer_ciphersuite(ssl,
452 cipher_suite);
453 if (ciphersuite_info == NULL) {
Jerry Yu82534862022-08-30 10:42:33 +0800454 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +0100455 }
Jerry Yu82534862022-08-30 10:42:33 +0800456
Jerry Yu4746b102022-09-13 11:11:48 +0800457 *selected_ciphersuite = cipher_suite;
Jerry Yu82534862022-08-30 10:42:33 +0800458 *selected_ciphersuite_info = ciphersuite_info;
459
Gilles Peskine449bd832023-01-11 14:50:10 +0100460 return 0;
Jerry Yu82534862022-08-30 10:42:33 +0800461 }
462
Gilles Peskine449bd832023-01-11 14:50:10 +0100463 return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
Jerry Yu5725f1c2022-08-21 17:27:16 +0800464}
Jerry Yuf35ba382022-08-23 17:58:26 +0800465
Jerry Yu82534862022-08-30 10:42:33 +0800466MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100467static int ssl_tls13_session_copy_ticket(mbedtls_ssl_session *dst,
468 const mbedtls_ssl_session *src)
Jerry Yu82534862022-08-30 10:42:33 +0800469{
Jerry Yu82534862022-08-30 10:42:33 +0800470 dst->ticket_age_add = src->ticket_age_add;
471 dst->ticket_flags = src->ticket_flags;
472 dst->resumption_key_len = src->resumption_key_len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100473 if (src->resumption_key_len == 0) {
474 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
475 }
476 memcpy(dst->resumption_key, src->resumption_key, src->resumption_key_len);
Jerry Yu4746b102022-09-13 11:11:48 +0800477
Jerry Yu33bf2402022-12-12 16:01:43 +0800478#if defined(MBEDTLS_SSL_EARLY_DATA)
479 dst->max_early_data_size = src->max_early_data_size;
480#endif
481
Gilles Peskine449bd832023-01-11 14:50:10 +0100482 return 0;
Jerry Yu82534862022-08-30 10:42:33 +0800483}
484#endif /* MBEDTLS_SSL_SESSION_TICKETS */
485
Jerry Yu1c105562022-07-10 06:32:38 +0000486/* Parser for pre_shared_key extension in client hello
487 * struct {
488 * opaque identity<1..2^16-1>;
489 * uint32 obfuscated_ticket_age;
490 * } PskIdentity;
491 *
492 * opaque PskBinderEntry<32..255>;
493 *
494 * struct {
495 * PskIdentity identities<7..2^16-1>;
496 * PskBinderEntry binders<33..2^16-1>;
497 * } OfferedPsks;
498 *
499 * struct {
500 * select (Handshake.msg_type) {
501 * case client_hello: OfferedPsks;
502 * ....
503 * };
504 * } PreSharedKeyExtension;
505 */
Jerry Yu6e74a7e2022-07-20 20:49:32 +0800506MBEDTLS_CHECK_RETURN_CRITICAL
Xiaokang Qian9f1747b2023-03-30 02:50:04 +0000507static int ssl_tls13_parse_pre_shared_key_ext(
508 mbedtls_ssl_context *ssl,
509 const unsigned char *pre_shared_key_ext,
510 const unsigned char *pre_shared_key_ext_end,
511 const unsigned char *ciphersuites,
512 const unsigned char *ciphersuites_end)
Jerry Yu1c105562022-07-10 06:32:38 +0000513{
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +0100514 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yu29d9faa2022-08-23 17:52:45 +0800515 const unsigned char *identities = pre_shared_key_ext;
Jerry Yu568ec252022-07-22 21:27:34 +0800516 const unsigned char *p_identity_len;
517 size_t identities_len;
Jerry Yu1c105562022-07-10 06:32:38 +0000518 const unsigned char *identities_end;
Jerry Yu568ec252022-07-22 21:27:34 +0800519 const unsigned char *binders;
520 const unsigned char *p_binder_len;
521 size_t binders_len;
Jerry Yu1c105562022-07-10 06:32:38 +0000522 const unsigned char *binders_end;
Jerry Yu96a2e362022-07-21 15:11:34 +0800523 int matched_identity = -1;
524 int identity_id = -1;
Jerry Yu1c105562022-07-10 06:32:38 +0000525
Gilles Peskine449bd832023-01-11 14:50:10 +0100526 MBEDTLS_SSL_DEBUG_BUF(3, "pre_shared_key extension",
527 pre_shared_key_ext,
528 pre_shared_key_ext_end - pre_shared_key_ext);
Jerry Yu1c105562022-07-10 06:32:38 +0000529
Jerry Yu96a2e362022-07-21 15:11:34 +0800530 /* identities_len 2 bytes
531 * identities_data >= 7 bytes
532 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100533 MBEDTLS_SSL_CHK_BUF_READ_PTR(identities, pre_shared_key_ext_end, 7 + 2);
534 identities_len = MBEDTLS_GET_UINT16_BE(identities, 0);
Jerry Yu568ec252022-07-22 21:27:34 +0800535 p_identity_len = identities + 2;
Gilles Peskine449bd832023-01-11 14:50:10 +0100536 MBEDTLS_SSL_CHK_BUF_READ_PTR(p_identity_len, pre_shared_key_ext_end,
537 identities_len);
Jerry Yu568ec252022-07-22 21:27:34 +0800538 identities_end = p_identity_len + identities_len;
Jerry Yu1c105562022-07-10 06:32:38 +0000539
Jerry Yu96a2e362022-07-21 15:11:34 +0800540 /* binders_len 2 bytes
541 * binders >= 33 bytes
542 */
Jerry Yu568ec252022-07-22 21:27:34 +0800543 binders = identities_end;
Gilles Peskine449bd832023-01-11 14:50:10 +0100544 MBEDTLS_SSL_CHK_BUF_READ_PTR(binders, pre_shared_key_ext_end, 33 + 2);
545 binders_len = MBEDTLS_GET_UINT16_BE(binders, 0);
Jerry Yu568ec252022-07-22 21:27:34 +0800546 p_binder_len = binders + 2;
Gilles Peskine449bd832023-01-11 14:50:10 +0100547 MBEDTLS_SSL_CHK_BUF_READ_PTR(p_binder_len, pre_shared_key_ext_end, binders_len);
Jerry Yu568ec252022-07-22 21:27:34 +0800548 binders_end = p_binder_len + binders_len;
Jerry Yu1c105562022-07-10 06:32:38 +0000549
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +0100550 ret = ssl->handshake->update_checksum(ssl, pre_shared_key_ext,
551 identities_end - pre_shared_key_ext);
552 if (0 != ret) {
553 MBEDTLS_SSL_DEBUG_RET(1, ("update_checksum"), ret);
554 return ret;
555 }
Jerry Yu96a2e362022-07-21 15:11:34 +0800556
Gilles Peskine449bd832023-01-11 14:50:10 +0100557 while (p_identity_len < identities_end && p_binder_len < binders_end) {
Jerry Yu1c105562022-07-10 06:32:38 +0000558 const unsigned char *identity;
Jerry Yu568ec252022-07-22 21:27:34 +0800559 size_t identity_len;
Jerry Yu82534862022-08-30 10:42:33 +0800560 uint32_t obfuscated_ticket_age;
Jerry Yu96a2e362022-07-21 15:11:34 +0800561 const unsigned char *binder;
Jerry Yu568ec252022-07-22 21:27:34 +0800562 size_t binder_len;
Jerry Yu5725f1c2022-08-21 17:27:16 +0800563 int psk_type;
564 uint16_t cipher_suite;
Jerry Yu29d9faa2022-08-23 17:52:45 +0800565 const mbedtls_ssl_ciphersuite_t *ciphersuite_info;
Jerry Yu82534862022-08-30 10:42:33 +0800566#if defined(MBEDTLS_SSL_SESSION_TICKETS)
567 mbedtls_ssl_session session;
Gilles Peskine449bd832023-01-11 14:50:10 +0100568 mbedtls_ssl_session_init(&session);
Jerry Yu82534862022-08-30 10:42:33 +0800569#endif
Jerry Yubb852022022-07-20 21:10:44 +0800570
Gilles Peskine449bd832023-01-11 14:50:10 +0100571 MBEDTLS_SSL_CHK_BUF_READ_PTR(p_identity_len, identities_end, 2 + 1 + 4);
572 identity_len = MBEDTLS_GET_UINT16_BE(p_identity_len, 0);
Jerry Yu568ec252022-07-22 21:27:34 +0800573 identity = p_identity_len + 2;
Gilles Peskine449bd832023-01-11 14:50:10 +0100574 MBEDTLS_SSL_CHK_BUF_READ_PTR(identity, identities_end, identity_len + 4);
575 obfuscated_ticket_age = MBEDTLS_GET_UINT32_BE(identity, identity_len);
Jerry Yu568ec252022-07-22 21:27:34 +0800576 p_identity_len += identity_len + 6;
Jerry Yu1c105562022-07-10 06:32:38 +0000577
Gilles Peskine449bd832023-01-11 14:50:10 +0100578 MBEDTLS_SSL_CHK_BUF_READ_PTR(p_binder_len, binders_end, 1 + 32);
Jerry Yu568ec252022-07-22 21:27:34 +0800579 binder_len = *p_binder_len;
580 binder = p_binder_len + 1;
Gilles Peskine449bd832023-01-11 14:50:10 +0100581 MBEDTLS_SSL_CHK_BUF_READ_PTR(binder, binders_end, binder_len);
Jerry Yu568ec252022-07-22 21:27:34 +0800582 p_binder_len += binder_len + 1;
Jerry Yu96a2e362022-07-21 15:11:34 +0800583
Jerry Yu96a2e362022-07-21 15:11:34 +0800584 identity_id++;
Gilles Peskine449bd832023-01-11 14:50:10 +0100585 if (matched_identity != -1) {
Jerry Yu1c105562022-07-10 06:32:38 +0000586 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +0100587 }
Jerry Yu1c105562022-07-10 06:32:38 +0000588
Jerry Yu96a2e362022-07-21 15:11:34 +0800589 ret = ssl_tls13_offered_psks_check_identity_match(
Gilles Peskine449bd832023-01-11 14:50:10 +0100590 ssl, identity, identity_len, obfuscated_ticket_age,
591 &psk_type, &session);
592 if (ret != SSL_TLS1_3_OFFERED_PSK_MATCH) {
Jerry Yu96a2e362022-07-21 15:11:34 +0800593 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +0100594 }
Jerry Yu96a2e362022-07-21 15:11:34 +0800595
Gilles Peskine449bd832023-01-11 14:50:10 +0100596 MBEDTLS_SSL_DEBUG_MSG(4, ("found matched identity"));
597 switch (psk_type) {
Jerry Yu0baf9072022-08-25 11:21:04 +0800598 case MBEDTLS_SSL_TLS1_3_PSK_EXTERNAL:
599 ret = ssl_tls13_select_ciphersuite_for_psk(
Gilles Peskine449bd832023-01-11 14:50:10 +0100600 ssl, ciphersuites, ciphersuites_end,
601 &cipher_suite, &ciphersuite_info);
Jerry Yu0baf9072022-08-25 11:21:04 +0800602 break;
603 case MBEDTLS_SSL_TLS1_3_PSK_RESUMPTION:
Jerry Yu82534862022-08-30 10:42:33 +0800604#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Jerry Yu0baf9072022-08-25 11:21:04 +0800605 ret = ssl_tls13_select_ciphersuite_for_resumption(
Gilles Peskine449bd832023-01-11 14:50:10 +0100606 ssl, ciphersuites, ciphersuites_end, &session,
607 &cipher_suite, &ciphersuite_info);
608 if (ret != 0) {
609 mbedtls_ssl_session_free(&session);
610 }
Jerry Yu82534862022-08-30 10:42:33 +0800611#else
612 ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
613#endif
Jerry Yu0baf9072022-08-25 11:21:04 +0800614 break;
615 default:
Gilles Peskine449bd832023-01-11 14:50:10 +0100616 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Jerry Yu0baf9072022-08-25 11:21:04 +0800617 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100618 if (ret != 0) {
Jerry Yuf35ba382022-08-23 17:58:26 +0800619 /* See below, no cipher_suite available, abort handshake */
620 MBEDTLS_SSL_PEND_FATAL_ALERT(
621 MBEDTLS_SSL_ALERT_MSG_DECRYPT_ERROR,
Gilles Peskine449bd832023-01-11 14:50:10 +0100622 MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE);
Jerry Yuf35ba382022-08-23 17:58:26 +0800623 MBEDTLS_SSL_DEBUG_RET(
Gilles Peskine449bd832023-01-11 14:50:10 +0100624 2, "ssl_tls13_select_ciphersuite", ret);
625 return ret;
Jerry Yu5725f1c2022-08-21 17:27:16 +0800626 }
627
Jerry Yu96a2e362022-07-21 15:11:34 +0800628 ret = ssl_tls13_offered_psks_check_binder_match(
Gilles Peskine449bd832023-01-11 14:50:10 +0100629 ssl, binder, binder_len, psk_type,
Dave Rodgman2eab4622023-10-05 13:30:37 +0100630 mbedtls_md_psa_alg_from_type((mbedtls_md_type_t) ciphersuite_info->mac));
Gilles Peskine449bd832023-01-11 14:50:10 +0100631 if (ret != SSL_TLS1_3_OFFERED_PSK_MATCH) {
Jerry Yuc5a23a02022-08-25 10:51:44 +0800632 /* For security reasons, the handshake should be aborted when we
633 * fail to validate a binder value. See RFC 8446 section 4.2.11.2
634 * and appendix E.6. */
Jerry Yu82534862022-08-30 10:42:33 +0800635#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Gilles Peskine449bd832023-01-11 14:50:10 +0100636 mbedtls_ssl_session_free(&session);
Jerry Yu82534862022-08-30 10:42:33 +0800637#endif
Gilles Peskine449bd832023-01-11 14:50:10 +0100638 MBEDTLS_SSL_DEBUG_MSG(3, ("Invalid binder."));
Xiaokang Qian9f1747b2023-03-30 02:50:04 +0000639 MBEDTLS_SSL_DEBUG_RET(
640 1, "ssl_tls13_offered_psks_check_binder_match", ret);
Jerry Yu96a2e362022-07-21 15:11:34 +0800641 MBEDTLS_SSL_PEND_FATAL_ALERT(
642 MBEDTLS_SSL_ALERT_MSG_DECRYPT_ERROR,
Gilles Peskine449bd832023-01-11 14:50:10 +0100643 MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE);
644 return ret;
Jerry Yu96a2e362022-07-21 15:11:34 +0800645 }
Jerry Yu96a2e362022-07-21 15:11:34 +0800646
647 matched_identity = identity_id;
Jerry Yu5725f1c2022-08-21 17:27:16 +0800648
649 /* Update handshake parameters */
Jerry Yue5834fd2022-08-29 20:16:09 +0800650 ssl->handshake->ciphersuite_info = ciphersuite_info;
Jerry Yu4746b102022-09-13 11:11:48 +0800651 ssl->session_negotiate->ciphersuite = cipher_suite;
Gilles Peskine449bd832023-01-11 14:50:10 +0100652 MBEDTLS_SSL_DEBUG_MSG(2, ("overwrite ciphersuite: %04x - %s",
653 cipher_suite, ciphersuite_info->name));
Jerry Yu82534862022-08-30 10:42:33 +0800654#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Gilles Peskine449bd832023-01-11 14:50:10 +0100655 if (psk_type == MBEDTLS_SSL_TLS1_3_PSK_RESUMPTION) {
656 ret = ssl_tls13_session_copy_ticket(ssl->session_negotiate,
657 &session);
658 mbedtls_ssl_session_free(&session);
659 if (ret != 0) {
660 return ret;
661 }
Jerry Yu82534862022-08-30 10:42:33 +0800662 }
663#endif /* MBEDTLS_SSL_SESSION_TICKETS */
Jerry Yu1c105562022-07-10 06:32:38 +0000664 }
665
Gilles Peskine449bd832023-01-11 14:50:10 +0100666 if (p_identity_len != identities_end || p_binder_len != binders_end) {
667 MBEDTLS_SSL_DEBUG_MSG(3, ("pre_shared_key extension decode error"));
668 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR,
669 MBEDTLS_ERR_SSL_DECODE_ERROR);
670 return MBEDTLS_ERR_SSL_DECODE_ERROR;
Jerry Yu1c105562022-07-10 06:32:38 +0000671 }
672
Jerry Yu6f1db3f2022-07-22 23:05:59 +0800673 /* Update the handshake transcript with the binder list. */
Xiaokang Qian9f1747b2023-03-30 02:50:04 +0000674 ret = ssl->handshake->update_checksum(
675 ssl, identities_end, (size_t) (binders_end - identities_end));
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +0100676 if (0 != ret) {
677 MBEDTLS_SSL_DEBUG_RET(1, ("update_checksum"), ret);
678 return ret;
679 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100680 if (matched_identity == -1) {
681 MBEDTLS_SSL_DEBUG_MSG(3, ("No matched PSK or ticket."));
682 return MBEDTLS_ERR_SSL_UNKNOWN_IDENTITY;
Jerry Yu1c105562022-07-10 06:32:38 +0000683 }
684
Gilles Peskine449bd832023-01-11 14:50:10 +0100685 ssl->handshake->selected_identity = (uint16_t) matched_identity;
686 MBEDTLS_SSL_DEBUG_MSG(3, ("Pre shared key found"));
Jerry Yu1c105562022-07-10 06:32:38 +0000687
Gilles Peskine449bd832023-01-11 14:50:10 +0100688 return 0;
Jerry Yu1c105562022-07-10 06:32:38 +0000689}
Jerry Yu032b15ce2022-07-11 06:10:03 +0000690
691/*
692 * struct {
693 * select ( Handshake.msg_type ) {
694 * ....
695 * case server_hello:
696 * uint16 selected_identity;
697 * }
698 * } PreSharedKeyExtension;
699 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100700static int ssl_tls13_write_server_pre_shared_key_ext(mbedtls_ssl_context *ssl,
701 unsigned char *buf,
702 unsigned char *end,
703 size_t *olen)
Jerry Yu032b15ce2022-07-11 06:10:03 +0000704{
Gilles Peskine449bd832023-01-11 14:50:10 +0100705 unsigned char *p = (unsigned char *) buf;
Jerry Yu032b15ce2022-07-11 06:10:03 +0000706
707 *olen = 0;
708
David Horstmann21b89762022-10-06 18:34:28 +0100709 int not_using_psk = 0;
Jerry Yu032b15ce2022-07-11 06:10:03 +0000710#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +0100711 not_using_psk = (mbedtls_svc_key_id_is_null(ssl->handshake->psk_opaque));
Jerry Yu032b15ce2022-07-11 06:10:03 +0000712#else
Gilles Peskine449bd832023-01-11 14:50:10 +0100713 not_using_psk = (ssl->handshake->psk == NULL);
Jerry Yu032b15ce2022-07-11 06:10:03 +0000714#endif
Gilles Peskine449bd832023-01-11 14:50:10 +0100715 if (not_using_psk) {
Jerry Yu032b15ce2022-07-11 06:10:03 +0000716 /* We shouldn't have called this extension writer unless we've
717 * chosen to use a PSK. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100718 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Jerry Yu032b15ce2022-07-11 06:10:03 +0000719 }
720
Gilles Peskine449bd832023-01-11 14:50:10 +0100721 MBEDTLS_SSL_DEBUG_MSG(3, ("server hello, adding pre_shared_key extension"));
722 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 6);
Jerry Yu032b15ce2022-07-11 06:10:03 +0000723
Gilles Peskine449bd832023-01-11 14:50:10 +0100724 MBEDTLS_PUT_UINT16_BE(MBEDTLS_TLS_EXT_PRE_SHARED_KEY, p, 0);
725 MBEDTLS_PUT_UINT16_BE(2, p, 2);
Jerry Yu032b15ce2022-07-11 06:10:03 +0000726
Gilles Peskine449bd832023-01-11 14:50:10 +0100727 MBEDTLS_PUT_UINT16_BE(ssl->handshake->selected_identity, p, 4);
Jerry Yu032b15ce2022-07-11 06:10:03 +0000728
729 *olen = 6;
730
Gilles Peskine449bd832023-01-11 14:50:10 +0100731 MBEDTLS_SSL_DEBUG_MSG(4, ("sent selected_identity: %u",
732 ssl->handshake->selected_identity));
Jerry Yu032b15ce2022-07-11 06:10:03 +0000733
Gilles Peskine449bd832023-01-11 14:50:10 +0100734 mbedtls_ssl_tls13_set_hs_sent_ext_mask(ssl, MBEDTLS_TLS_EXT_PRE_SHARED_KEY);
Jerry Yub95dd362022-11-08 21:19:34 +0800735
Gilles Peskine449bd832023-01-11 14:50:10 +0100736 return 0;
Jerry Yu032b15ce2022-07-11 06:10:03 +0000737}
738
Ronald Cron41a443a2022-10-04 16:38:25 +0200739#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED */
Jerry Yue19e3b92022-07-08 12:04:51 +0000740
XiaokangQian7807f9f2022-02-15 10:04:37 +0000741/* From RFC 8446:
742 * struct {
XiaokangQiancfd925f2022-04-14 07:10:37 +0000743 * ProtocolVersion versions<2..254>;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000744 * } SupportedVersions;
745 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200746MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100747static int ssl_tls13_parse_supported_versions_ext(mbedtls_ssl_context *ssl,
748 const unsigned char *buf,
749 const unsigned char *end)
XiaokangQian7807f9f2022-02-15 10:04:37 +0000750{
XiaokangQian7807f9f2022-02-15 10:04:37 +0000751 const unsigned char *p = buf;
XiaokangQian8f9dfe42022-04-15 02:52:39 +0000752 size_t versions_len;
XiaokangQian4080a7f2022-04-11 09:55:18 +0000753 const unsigned char *versions_end;
XiaokangQian0a1b54e2022-04-21 03:01:38 +0000754 uint16_t tls_version;
Ronald Cron5af4c7f2023-03-07 20:46:59 +0100755 int found_supported_version = 0;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000756
Gilles Peskine449bd832023-01-11 14:50:10 +0100757 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, 1);
XiaokangQian4080a7f2022-04-11 09:55:18 +0000758 versions_len = p[0];
XiaokangQian7807f9f2022-02-15 10:04:37 +0000759 p += 1;
760
Gilles Peskine449bd832023-01-11 14:50:10 +0100761 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, versions_len);
XiaokangQian4080a7f2022-04-11 09:55:18 +0000762 versions_end = p + versions_len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100763 while (p < versions_end) {
764 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, versions_end, 2);
765 tls_version = mbedtls_ssl_read_version(p, ssl->conf->transport);
XiaokangQianb67384d2022-04-19 00:02:38 +0000766 p += 2;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000767
Ronald Cron3bd2b022023-04-03 16:45:39 +0200768 if (MBEDTLS_SSL_VERSION_TLS1_3 == tls_version) {
Ronald Cron5af4c7f2023-03-07 20:46:59 +0100769 found_supported_version = 1;
770 break;
771 }
772
Ronald Cron3bd2b022023-04-03 16:45:39 +0200773 if ((MBEDTLS_SSL_VERSION_TLS1_2 == tls_version) &&
774 mbedtls_ssl_conf_is_tls12_enabled(ssl->conf)) {
Ronald Cron5af4c7f2023-03-07 20:46:59 +0100775 found_supported_version = 1;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000776 break;
777 }
XiaokangQian7807f9f2022-02-15 10:04:37 +0000778 }
779
Ronald Cron5af4c7f2023-03-07 20:46:59 +0100780 if (!found_supported_version) {
781 MBEDTLS_SSL_DEBUG_MSG(1, ("No supported version found."));
XiaokangQian7807f9f2022-02-15 10:04:37 +0000782
Gilles Peskine449bd832023-01-11 14:50:10 +0100783 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_PROTOCOL_VERSION,
784 MBEDTLS_ERR_SSL_BAD_PROTOCOL_VERSION);
785 return MBEDTLS_ERR_SSL_BAD_PROTOCOL_VERSION;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000786 }
787
Ronald Cron5af4c7f2023-03-07 20:46:59 +0100788 MBEDTLS_SSL_DEBUG_MSG(1, ("Negotiated version: [%04x]",
Gilles Peskine449bd832023-01-11 14:50:10 +0100789 (unsigned int) tls_version));
XiaokangQian7807f9f2022-02-15 10:04:37 +0000790
Ronald Cron5af4c7f2023-03-07 20:46:59 +0100791 return (int) tls_version;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000792}
793
Przemek Stekiel8c0a9532023-06-15 16:48:19 +0200794#if defined(PSA_WANT_ALG_ECDH) || defined(PSA_WANT_ALG_FFDH)
XiaokangQiane8ff3502022-04-22 02:34:40 +0000795/*
XiaokangQian7807f9f2022-02-15 10:04:37 +0000796 *
797 * From RFC 8446:
798 * enum {
799 * ... (0xFFFF)
800 * } NamedGroup;
801 * struct {
802 * NamedGroup named_group_list<2..2^16-1>;
803 * } NamedGroupList;
804 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200805MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100806static int ssl_tls13_parse_supported_groups_ext(mbedtls_ssl_context *ssl,
807 const unsigned char *buf,
808 const unsigned char *end)
XiaokangQian7807f9f2022-02-15 10:04:37 +0000809{
XiaokangQian7807f9f2022-02-15 10:04:37 +0000810 const unsigned char *p = buf;
XiaokangQian84823772022-04-19 07:57:30 +0000811 size_t named_group_list_len;
XiaokangQian8f9dfe42022-04-15 02:52:39 +0000812 const unsigned char *named_group_list_end;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000813
Gilles Peskine449bd832023-01-11 14:50:10 +0100814 MBEDTLS_SSL_DEBUG_BUF(3, "supported_groups extension", p, end - buf);
815 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, 2);
816 named_group_list_len = MBEDTLS_GET_UINT16_BE(p, 0);
XiaokangQian7807f9f2022-02-15 10:04:37 +0000817 p += 2;
Gilles Peskine449bd832023-01-11 14:50:10 +0100818 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, named_group_list_len);
XiaokangQian8f9dfe42022-04-15 02:52:39 +0000819 named_group_list_end = p + named_group_list_len;
XiaokangQian4e8cd7b2022-04-21 09:48:09 +0000820 ssl->handshake->hrr_selected_group = 0;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000821
Gilles Peskine449bd832023-01-11 14:50:10 +0100822 while (p < named_group_list_end) {
XiaokangQian08037552022-04-20 07:16:41 +0000823 uint16_t named_group;
Gilles Peskine449bd832023-01-11 14:50:10 +0100824 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, named_group_list_end, 2);
825 named_group = MBEDTLS_GET_UINT16_BE(p, 0);
XiaokangQian08037552022-04-20 07:16:41 +0000826 p += 2;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000827
Gilles Peskine449bd832023-01-11 14:50:10 +0100828 MBEDTLS_SSL_DEBUG_MSG(2,
829 ("got named group: %s(%04x)",
830 mbedtls_ssl_named_group_to_str(named_group),
831 named_group));
XiaokangQian08037552022-04-20 07:16:41 +0000832
Gilles Peskine449bd832023-01-11 14:50:10 +0100833 if (!mbedtls_ssl_named_group_is_offered(ssl, named_group) ||
834 !mbedtls_ssl_named_group_is_supported(named_group) ||
835 ssl->handshake->hrr_selected_group != 0) {
XiaokangQian08037552022-04-20 07:16:41 +0000836 continue;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000837 }
838
Gilles Peskine449bd832023-01-11 14:50:10 +0100839 MBEDTLS_SSL_DEBUG_MSG(2,
840 ("add named group %s(%04x) into received list.",
841 mbedtls_ssl_named_group_to_str(named_group),
842 named_group));
Jerry Yuc1be19f2022-04-23 16:11:39 +0800843
XiaokangQian4e8cd7b2022-04-21 09:48:09 +0000844 ssl->handshake->hrr_selected_group = named_group;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000845 }
846
Gilles Peskine449bd832023-01-11 14:50:10 +0100847 return 0;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000848
849}
Przemek Stekiel8c0a9532023-06-15 16:48:19 +0200850#endif /* PSA_WANT_ALG_ECDH || PSA_WANT_ALG_FFDH */
XiaokangQian7807f9f2022-02-15 10:04:37 +0000851
XiaokangQian08037552022-04-20 07:16:41 +0000852#define SSL_TLS1_3_PARSE_KEY_SHARES_EXT_NO_MATCH 1
853
Valerio Settic9ae8622023-07-25 11:23:50 +0200854#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_EPHEMERAL_ENABLED)
XiaokangQian7807f9f2022-02-15 10:04:37 +0000855/*
856 * ssl_tls13_parse_key_shares_ext() verifies whether the information in the
Xiaokang Qian9f1747b2023-03-30 02:50:04 +0000857 * extension is correct and stores the first acceptable key share and its
858 * associated group.
XiaokangQian7807f9f2022-02-15 10:04:37 +0000859 *
860 * Possible return values are:
861 * - 0: Successful processing of the client provided key share extension.
Xiaokang Qian9f1747b2023-03-30 02:50:04 +0000862 * - SSL_TLS1_3_PARSE_KEY_SHARES_EXT_NO_MATCH: The key shares provided by
863 * the client does not match a group supported by the server. A
864 * HelloRetryRequest will be needed.
XiaokangQiane8ff3502022-04-22 02:34:40 +0000865 * - A negative value for fatal errors.
Jerry Yuc1be19f2022-04-23 16:11:39 +0800866 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200867MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100868static int ssl_tls13_parse_key_shares_ext(mbedtls_ssl_context *ssl,
869 const unsigned char *buf,
870 const unsigned char *end)
XiaokangQian7807f9f2022-02-15 10:04:37 +0000871{
XiaokangQianb67384d2022-04-19 00:02:38 +0000872 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000873 unsigned char const *p = buf;
XiaokangQian8f9dfe42022-04-15 02:52:39 +0000874 unsigned char const *client_shares_end;
Jerry Yuc1be19f2022-04-23 16:11:39 +0800875 size_t client_shares_len;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000876
877 /* From RFC 8446:
878 *
879 * struct {
880 * KeyShareEntry client_shares<0..2^16-1>;
881 * } KeyShareClientHello;
882 *
883 */
884
Gilles Peskine449bd832023-01-11 14:50:10 +0100885 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, 2);
886 client_shares_len = MBEDTLS_GET_UINT16_BE(p, 0);
XiaokangQian7807f9f2022-02-15 10:04:37 +0000887 p += 2;
Gilles Peskine449bd832023-01-11 14:50:10 +0100888 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, client_shares_len);
XiaokangQian7807f9f2022-02-15 10:04:37 +0000889
890 ssl->handshake->offered_group_id = 0;
XiaokangQian8f9dfe42022-04-15 02:52:39 +0000891 client_shares_end = p + client_shares_len;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000892
893 /* We try to find a suitable key share entry and copy it to the
894 * handshake context. Later, we have to find out whether we can do
895 * something with the provided key share or whether we have to
XiaokangQianc5763b52022-04-02 03:34:37 +0000896 * dismiss it and send a HelloRetryRequest message.
897 */
XiaokangQian7807f9f2022-02-15 10:04:37 +0000898
Gilles Peskine449bd832023-01-11 14:50:10 +0100899 while (p < client_shares_end) {
XiaokangQian9b5d04b2022-04-10 10:20:43 +0000900 uint16_t group;
Jerry Yuc1be19f2022-04-23 16:11:39 +0800901 size_t key_exchange_len;
Jerry Yu086edc22022-05-05 10:50:38 +0800902 const unsigned char *key_exchange;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000903
904 /*
905 * struct {
906 * NamedGroup group;
907 * opaque key_exchange<1..2^16-1>;
908 * } KeyShareEntry;
909 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100910 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, client_shares_end, 4);
911 group = MBEDTLS_GET_UINT16_BE(p, 0);
912 key_exchange_len = MBEDTLS_GET_UINT16_BE(p, 2);
Jerry Yuc1be19f2022-04-23 16:11:39 +0800913 p += 4;
Jerry Yu086edc22022-05-05 10:50:38 +0800914 key_exchange = p;
Gilles Peskine449bd832023-01-11 14:50:10 +0100915 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, client_shares_end, key_exchange_len);
Jerry Yu086edc22022-05-05 10:50:38 +0800916 p += key_exchange_len;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000917
918 /* Continue parsing even if we have already found a match,
XiaokangQianc5763b52022-04-02 03:34:37 +0000919 * for input validation purposes.
920 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100921 if (!mbedtls_ssl_named_group_is_offered(ssl, group) ||
922 !mbedtls_ssl_named_group_is_supported(group) ||
923 ssl->handshake->offered_group_id != 0) {
XiaokangQian060d8672022-04-21 09:24:56 +0000924 continue;
925 }
XiaokangQian060d8672022-04-21 09:24:56 +0000926
XiaokangQian7807f9f2022-02-15 10:04:37 +0000927 /*
Przemek Stekielc89f3ea2023-05-18 15:45:53 +0200928 * ECDHE and FFDHE groups are supported
XiaokangQian7807f9f2022-02-15 10:04:37 +0000929 */
Przemek Stekielc89f3ea2023-05-18 15:45:53 +0200930 if (mbedtls_ssl_tls13_named_group_is_ecdhe(group) ||
Przemek Stekield5f79e72023-06-29 09:08:43 +0200931 mbedtls_ssl_tls13_named_group_is_ffdh(group)) {
Przemek Stekielc89f3ea2023-05-18 15:45:53 +0200932 MBEDTLS_SSL_DEBUG_MSG(2, ("ECDH/FFDH group: %s (%04x)",
Gilles Peskine449bd832023-01-11 14:50:10 +0100933 mbedtls_ssl_named_group_to_str(group),
934 group));
Przemek Stekiel7ac93be2023-07-04 10:02:38 +0200935 ret = mbedtls_ssl_tls13_read_public_xxdhe_share(
Gilles Peskine449bd832023-01-11 14:50:10 +0100936 ssl, key_exchange - 2, key_exchange_len + 2);
937 if (ret != 0) {
938 return ret;
939 }
XiaokangQian4e8cd7b2022-04-21 09:48:09 +0000940
Gilles Peskine449bd832023-01-11 14:50:10 +0100941 } else {
942 MBEDTLS_SSL_DEBUG_MSG(4, ("Unrecognized NamedGroup %u",
943 (unsigned) group));
XiaokangQian7807f9f2022-02-15 10:04:37 +0000944 continue;
945 }
946
XiaokangQian9b5d04b2022-04-10 10:20:43 +0000947 ssl->handshake->offered_group_id = group;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000948 }
949
Jerry Yuc1be19f2022-04-23 16:11:39 +0800950
Gilles Peskine449bd832023-01-11 14:50:10 +0100951 if (ssl->handshake->offered_group_id == 0) {
952 MBEDTLS_SSL_DEBUG_MSG(1, ("no matching key share"));
953 return SSL_TLS1_3_PARSE_KEY_SHARES_EXT_NO_MATCH;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000954 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100955 return 0;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000956}
Valerio Settic9ae8622023-07-25 11:23:50 +0200957#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_EPHEMERAL_ENABLED */
XiaokangQian7807f9f2022-02-15 10:04:37 +0000958
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200959MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100960static int ssl_tls13_client_hello_has_exts(mbedtls_ssl_context *ssl,
961 int exts_mask)
XiaokangQian7807f9f2022-02-15 10:04:37 +0000962{
Jerry Yu0c354a22022-08-29 15:25:36 +0800963 int masked = ssl->handshake->received_extensions & exts_mask;
Gilles Peskine449bd832023-01-11 14:50:10 +0100964 return masked == exts_mask;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000965}
966
Jerry Yue5991322022-11-07 14:03:44 +0800967#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200968MBEDTLS_CHECK_RETURN_CRITICAL
XiaokangQianb67384d2022-04-19 00:02:38 +0000969static int ssl_tls13_client_hello_has_exts_for_ephemeral_key_exchange(
Gilles Peskine449bd832023-01-11 14:50:10 +0100970 mbedtls_ssl_context *ssl)
XiaokangQian7807f9f2022-02-15 10:04:37 +0000971{
Gilles Peskine449bd832023-01-11 14:50:10 +0100972 return ssl_tls13_client_hello_has_exts(
973 ssl,
974 MBEDTLS_SSL_EXT_MASK(SUPPORTED_GROUPS) |
975 MBEDTLS_SSL_EXT_MASK(KEY_SHARE) |
976 MBEDTLS_SSL_EXT_MASK(SIG_ALG));
XiaokangQian7807f9f2022-02-15 10:04:37 +0000977}
Jerry Yue5991322022-11-07 14:03:44 +0800978#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED */
XiaokangQian7807f9f2022-02-15 10:04:37 +0000979
Jerry Yue5991322022-11-07 14:03:44 +0800980#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK_ENABLED)
Jerry Yu77f01482022-07-11 07:03:24 +0000981MBEDTLS_CHECK_RETURN_CRITICAL
982static int ssl_tls13_client_hello_has_exts_for_psk_key_exchange(
Gilles Peskine449bd832023-01-11 14:50:10 +0100983 mbedtls_ssl_context *ssl)
Jerry Yu77f01482022-07-11 07:03:24 +0000984{
Gilles Peskine449bd832023-01-11 14:50:10 +0100985 return ssl_tls13_client_hello_has_exts(
986 ssl,
987 MBEDTLS_SSL_EXT_MASK(PRE_SHARED_KEY) |
988 MBEDTLS_SSL_EXT_MASK(PSK_KEY_EXCHANGE_MODES));
Jerry Yu77f01482022-07-11 07:03:24 +0000989}
Jerry Yue5991322022-11-07 14:03:44 +0800990#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK_ENABLED */
Jerry Yu77f01482022-07-11 07:03:24 +0000991
Jerry Yue5991322022-11-07 14:03:44 +0800992#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK_EPHEMERAL_ENABLED)
Jerry Yu77f01482022-07-11 07:03:24 +0000993MBEDTLS_CHECK_RETURN_CRITICAL
994static int ssl_tls13_client_hello_has_exts_for_psk_ephemeral_key_exchange(
Gilles Peskine449bd832023-01-11 14:50:10 +0100995 mbedtls_ssl_context *ssl)
Jerry Yu77f01482022-07-11 07:03:24 +0000996{
Gilles Peskine449bd832023-01-11 14:50:10 +0100997 return ssl_tls13_client_hello_has_exts(
998 ssl,
999 MBEDTLS_SSL_EXT_MASK(SUPPORTED_GROUPS) |
1000 MBEDTLS_SSL_EXT_MASK(KEY_SHARE) |
1001 MBEDTLS_SSL_EXT_MASK(PRE_SHARED_KEY) |
1002 MBEDTLS_SSL_EXT_MASK(PSK_KEY_EXCHANGE_MODES));
Jerry Yu77f01482022-07-11 07:03:24 +00001003}
Jerry Yue5991322022-11-07 14:03:44 +08001004#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK_EPHEMERAL_ENABLED */
Jerry Yu77f01482022-07-11 07:03:24 +00001005
Pengyu Lv306a01d2023-02-02 16:35:47 +08001006#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED)
1007MBEDTLS_CHECK_RETURN_CRITICAL
Pengyu Lv52ad3332023-02-14 14:32:37 +08001008static int ssl_tls13_ticket_permission_check(mbedtls_ssl_context *ssl,
1009 unsigned int kex_mode)
Pengyu Lv306a01d2023-02-02 16:35:47 +08001010{
1011#if defined(MBEDTLS_SSL_SESSION_TICKETS)
1012 if (ssl->handshake->resume) {
Pengyu Lv7b711712023-10-24 17:07:14 +08001013 if (mbedtls_ssl_session_check_ticket_flags(
Pengyu Lv306a01d2023-02-02 16:35:47 +08001014 ssl->session_negotiate, kex_mode)) {
1015 return 0;
1016 }
1017 }
1018#else
1019 ((void) ssl);
1020 ((void) kex_mode);
1021#endif
1022 return 1;
1023}
1024#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED */
1025
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001026MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01001027static int ssl_tls13_check_ephemeral_key_exchange(mbedtls_ssl_context *ssl)
XiaokangQian7807f9f2022-02-15 10:04:37 +00001028{
Jerry Yue5991322022-11-07 14:03:44 +08001029#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01001030 return mbedtls_ssl_conf_tls13_ephemeral_enabled(ssl) &&
1031 ssl_tls13_client_hello_has_exts_for_ephemeral_key_exchange(ssl);
Jerry Yue5991322022-11-07 14:03:44 +08001032#else
1033 ((void) ssl);
Gilles Peskine449bd832023-01-11 14:50:10 +01001034 return 0;
Jerry Yue5991322022-11-07 14:03:44 +08001035#endif
Jerry Yu77f01482022-07-11 07:03:24 +00001036}
XiaokangQian7807f9f2022-02-15 10:04:37 +00001037
Jerry Yu77f01482022-07-11 07:03:24 +00001038MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01001039static int ssl_tls13_check_psk_key_exchange(mbedtls_ssl_context *ssl)
Jerry Yu77f01482022-07-11 07:03:24 +00001040{
Jerry Yue5991322022-11-07 14:03:44 +08001041#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK_ENABLED)
Pengyu Lv52ad3332023-02-14 14:32:37 +08001042 return ssl_tls13_ticket_permission_check(
Pengyu Lv306a01d2023-02-02 16:35:47 +08001043 ssl, MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK) &&
1044 mbedtls_ssl_conf_tls13_psk_enabled(ssl) &&
Gilles Peskine449bd832023-01-11 14:50:10 +01001045 mbedtls_ssl_tls13_psk_enabled(ssl) &&
1046 ssl_tls13_client_hello_has_exts_for_psk_key_exchange(ssl);
Jerry Yu77f01482022-07-11 07:03:24 +00001047#else
1048 ((void) ssl);
Gilles Peskine449bd832023-01-11 14:50:10 +01001049 return 0;
Jerry Yu77f01482022-07-11 07:03:24 +00001050#endif
1051}
XiaokangQian7807f9f2022-02-15 10:04:37 +00001052
Jerry Yu77f01482022-07-11 07:03:24 +00001053MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01001054static int ssl_tls13_check_psk_ephemeral_key_exchange(mbedtls_ssl_context *ssl)
Jerry Yu77f01482022-07-11 07:03:24 +00001055{
Jerry Yue5991322022-11-07 14:03:44 +08001056#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK_EPHEMERAL_ENABLED)
Pengyu Lv52ad3332023-02-14 14:32:37 +08001057 return ssl_tls13_ticket_permission_check(
Pengyu Lv306a01d2023-02-02 16:35:47 +08001058 ssl, MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK_EPHEMERAL) &&
1059 mbedtls_ssl_conf_tls13_psk_ephemeral_enabled(ssl) &&
Gilles Peskine449bd832023-01-11 14:50:10 +01001060 mbedtls_ssl_tls13_psk_ephemeral_enabled(ssl) &&
1061 ssl_tls13_client_hello_has_exts_for_psk_ephemeral_key_exchange(ssl);
Jerry Yu77f01482022-07-11 07:03:24 +00001062#else
1063 ((void) ssl);
Gilles Peskine449bd832023-01-11 14:50:10 +01001064 return 0;
Jerry Yu77f01482022-07-11 07:03:24 +00001065#endif
1066}
1067
Gilles Peskine449bd832023-01-11 14:50:10 +01001068static int ssl_tls13_determine_key_exchange_mode(mbedtls_ssl_context *ssl)
Jerry Yu77f01482022-07-11 07:03:24 +00001069{
1070 /*
1071 * Determine the key exchange algorithm to use.
1072 * There are three types of key exchanges supported in TLS 1.3:
1073 * - (EC)DH with ECDSA,
1074 * - (EC)DH with PSK,
1075 * - plain PSK.
1076 *
1077 * The PSK-based key exchanges may additionally be used with 0-RTT.
1078 *
1079 * Our built-in order of preference is
Jerry Yuce6ed702022-07-22 21:49:53 +08001080 * 1 ) (EC)DHE-PSK Mode ( psk_ephemeral )
1081 * 2 ) Certificate Mode ( ephemeral )
1082 * 3 ) Plain PSK Mode ( psk )
Jerry Yu77f01482022-07-11 07:03:24 +00001083 */
1084
Xiaokang Qian9f1747b2023-03-30 02:50:04 +00001085 ssl->handshake->key_exchange_mode =
1086 MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_NONE;
Jerry Yu77f01482022-07-11 07:03:24 +00001087
Gilles Peskine449bd832023-01-11 14:50:10 +01001088 if (ssl_tls13_check_psk_ephemeral_key_exchange(ssl)) {
Jerry Yu77f01482022-07-11 07:03:24 +00001089 ssl->handshake->key_exchange_mode =
1090 MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK_EPHEMERAL;
Gilles Peskine449bd832023-01-11 14:50:10 +01001091 MBEDTLS_SSL_DEBUG_MSG(2, ("key exchange mode: psk_ephemeral"));
1092 } else
1093 if (ssl_tls13_check_ephemeral_key_exchange(ssl)) {
Jerry Yu77f01482022-07-11 07:03:24 +00001094 ssl->handshake->key_exchange_mode =
1095 MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL;
Gilles Peskine449bd832023-01-11 14:50:10 +01001096 MBEDTLS_SSL_DEBUG_MSG(2, ("key exchange mode: ephemeral"));
1097 } else
1098 if (ssl_tls13_check_psk_key_exchange(ssl)) {
Jerry Yuce6ed702022-07-22 21:49:53 +08001099 ssl->handshake->key_exchange_mode =
1100 MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK;
Gilles Peskine449bd832023-01-11 14:50:10 +01001101 MBEDTLS_SSL_DEBUG_MSG(2, ("key exchange mode: psk"));
1102 } else {
Jerry Yu77f01482022-07-11 07:03:24 +00001103 MBEDTLS_SSL_DEBUG_MSG(
Gilles Peskine449bd832023-01-11 14:50:10 +01001104 1,
1105 ("ClientHello message misses mandatory extensions."));
1106 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_MISSING_EXTENSION,
1107 MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER);
1108 return MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER;
Jerry Yu77f01482022-07-11 07:03:24 +00001109 }
1110
Gilles Peskine449bd832023-01-11 14:50:10 +01001111 return 0;
Jerry Yu77f01482022-07-11 07:03:24 +00001112
XiaokangQian7807f9f2022-02-15 10:04:37 +00001113}
1114
XiaokangQian81802f42022-06-10 13:25:22 +00001115#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
Ronald Cron928cbd32022-10-04 16:14:26 +02001116 defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED)
Ronald Cron67ea2542022-09-15 17:34:42 +02001117
1118#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01001119static psa_algorithm_t ssl_tls13_iana_sig_alg_to_psa_alg(uint16_t sig_alg)
Ronald Cron67ea2542022-09-15 17:34:42 +02001120{
Gilles Peskine449bd832023-01-11 14:50:10 +01001121 switch (sig_alg) {
Ronald Cron67ea2542022-09-15 17:34:42 +02001122 case MBEDTLS_TLS1_3_SIG_ECDSA_SECP256R1_SHA256:
Gilles Peskine449bd832023-01-11 14:50:10 +01001123 return PSA_ALG_ECDSA(PSA_ALG_SHA_256);
Ronald Cron67ea2542022-09-15 17:34:42 +02001124 case MBEDTLS_TLS1_3_SIG_ECDSA_SECP384R1_SHA384:
Gilles Peskine449bd832023-01-11 14:50:10 +01001125 return PSA_ALG_ECDSA(PSA_ALG_SHA_384);
Ronald Cron67ea2542022-09-15 17:34:42 +02001126 case MBEDTLS_TLS1_3_SIG_ECDSA_SECP521R1_SHA512:
Gilles Peskine449bd832023-01-11 14:50:10 +01001127 return PSA_ALG_ECDSA(PSA_ALG_SHA_512);
Ronald Cron67ea2542022-09-15 17:34:42 +02001128 case MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA256:
Gilles Peskine449bd832023-01-11 14:50:10 +01001129 return PSA_ALG_RSA_PSS(PSA_ALG_SHA_256);
Ronald Cron67ea2542022-09-15 17:34:42 +02001130 case MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA384:
Gilles Peskine449bd832023-01-11 14:50:10 +01001131 return PSA_ALG_RSA_PSS(PSA_ALG_SHA_384);
Ronald Cron67ea2542022-09-15 17:34:42 +02001132 case MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA512:
Gilles Peskine449bd832023-01-11 14:50:10 +01001133 return PSA_ALG_RSA_PSS(PSA_ALG_SHA_512);
Ronald Cron67ea2542022-09-15 17:34:42 +02001134 case MBEDTLS_TLS1_3_SIG_RSA_PKCS1_SHA256:
Gilles Peskine449bd832023-01-11 14:50:10 +01001135 return PSA_ALG_RSA_PKCS1V15_SIGN(PSA_ALG_SHA_256);
Ronald Cron67ea2542022-09-15 17:34:42 +02001136 case MBEDTLS_TLS1_3_SIG_RSA_PKCS1_SHA384:
Gilles Peskine449bd832023-01-11 14:50:10 +01001137 return PSA_ALG_RSA_PKCS1V15_SIGN(PSA_ALG_SHA_384);
Ronald Cron67ea2542022-09-15 17:34:42 +02001138 case MBEDTLS_TLS1_3_SIG_RSA_PKCS1_SHA512:
Gilles Peskine449bd832023-01-11 14:50:10 +01001139 return PSA_ALG_RSA_PKCS1V15_SIGN(PSA_ALG_SHA_512);
Ronald Cron67ea2542022-09-15 17:34:42 +02001140 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01001141 return PSA_ALG_NONE;
Ronald Cron67ea2542022-09-15 17:34:42 +02001142 }
1143}
1144#endif /* MBEDTLS_USE_PSA_CRYPTO */
1145
XiaokangQian23c5be62022-06-07 02:04:34 +00001146/*
XiaokangQianfb665a82022-06-15 03:57:21 +00001147 * Pick best ( private key, certificate chain ) pair based on the signature
1148 * algorithms supported by the client.
XiaokangQian23c5be62022-06-07 02:04:34 +00001149 */
Ronald Cronce7d76e2022-07-08 18:56:49 +02001150MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01001151static int ssl_tls13_pick_key_cert(mbedtls_ssl_context *ssl)
XiaokangQian23c5be62022-06-07 02:04:34 +00001152{
XiaokangQianfb665a82022-06-15 03:57:21 +00001153 mbedtls_ssl_key_cert *key_cert, *key_cert_list;
XiaokangQian81802f42022-06-10 13:25:22 +00001154 const uint16_t *sig_alg = ssl->handshake->received_sig_algs;
XiaokangQian23c5be62022-06-07 02:04:34 +00001155
1156#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Gilles Peskine449bd832023-01-11 14:50:10 +01001157 if (ssl->handshake->sni_key_cert != NULL) {
XiaokangQianfb665a82022-06-15 03:57:21 +00001158 key_cert_list = ssl->handshake->sni_key_cert;
Gilles Peskine449bd832023-01-11 14:50:10 +01001159 } else
XiaokangQian23c5be62022-06-07 02:04:34 +00001160#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
Gilles Peskine449bd832023-01-11 14:50:10 +01001161 key_cert_list = ssl->conf->key_cert;
XiaokangQian23c5be62022-06-07 02:04:34 +00001162
Gilles Peskine449bd832023-01-11 14:50:10 +01001163 if (key_cert_list == NULL) {
1164 MBEDTLS_SSL_DEBUG_MSG(3, ("server has no certificate"));
1165 return -1;
XiaokangQian23c5be62022-06-07 02:04:34 +00001166 }
1167
Gilles Peskine449bd832023-01-11 14:50:10 +01001168 for (; *sig_alg != MBEDTLS_TLS1_3_SIG_NONE; sig_alg++) {
1169 if (!mbedtls_ssl_sig_alg_is_offered(ssl, *sig_alg)) {
Ronald Cron67ea2542022-09-15 17:34:42 +02001170 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +01001171 }
Ronald Cron67ea2542022-09-15 17:34:42 +02001172
Gilles Peskine449bd832023-01-11 14:50:10 +01001173 if (!mbedtls_ssl_tls13_sig_alg_for_cert_verify_is_supported(*sig_alg)) {
Ronald Cron67ea2542022-09-15 17:34:42 +02001174 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +01001175 }
Ronald Cron67ea2542022-09-15 17:34:42 +02001176
Gilles Peskine449bd832023-01-11 14:50:10 +01001177 for (key_cert = key_cert_list; key_cert != NULL;
1178 key_cert = key_cert->next) {
Ronald Cron67ea2542022-09-15 17:34:42 +02001179#if defined(MBEDTLS_USE_PSA_CRYPTO)
1180 psa_algorithm_t psa_alg = PSA_ALG_NONE;
1181#endif /* MBEDTLS_USE_PSA_CRYPTO */
1182
Gilles Peskine449bd832023-01-11 14:50:10 +01001183 MBEDTLS_SSL_DEBUG_CRT(3, "certificate (chain) candidate",
1184 key_cert->cert);
XiaokangQian81802f42022-06-10 13:25:22 +00001185
1186 /*
Gilles Peskine449bd832023-01-11 14:50:10 +01001187 * This avoids sending the client a cert it'll reject based on
1188 * keyUsage or other extensions.
1189 */
1190 if (mbedtls_x509_crt_check_key_usage(
1191 key_cert->cert, MBEDTLS_X509_KU_DIGITAL_SIGNATURE) != 0 ||
XiaokangQian81802f42022-06-10 13:25:22 +00001192 mbedtls_x509_crt_check_extended_key_usage(
XiaokangQianfb665a82022-06-15 03:57:21 +00001193 key_cert->cert, MBEDTLS_OID_SERVER_AUTH,
Gilles Peskine449bd832023-01-11 14:50:10 +01001194 MBEDTLS_OID_SIZE(MBEDTLS_OID_SERVER_AUTH)) != 0) {
1195 MBEDTLS_SSL_DEBUG_MSG(3, ("certificate mismatch: "
1196 "(extended) key usage extension"));
XiaokangQian81802f42022-06-10 13:25:22 +00001197 continue;
1198 }
XiaokangQian23c5be62022-06-07 02:04:34 +00001199
Gilles Peskine449bd832023-01-11 14:50:10 +01001200 MBEDTLS_SSL_DEBUG_MSG(3,
1201 ("ssl_tls13_pick_key_cert:"
1202 "check signature algorithm %s [%04x]",
1203 mbedtls_ssl_sig_alg_to_str(*sig_alg),
1204 *sig_alg));
Ronald Cron67ea2542022-09-15 17:34:42 +02001205#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01001206 psa_alg = ssl_tls13_iana_sig_alg_to_psa_alg(*sig_alg);
Ronald Cron67ea2542022-09-15 17:34:42 +02001207#endif /* MBEDTLS_USE_PSA_CRYPTO */
1208
Gilles Peskine449bd832023-01-11 14:50:10 +01001209 if (mbedtls_ssl_tls13_check_sig_alg_cert_key_match(
1210 *sig_alg, &key_cert->cert->pk)
Ronald Cron67ea2542022-09-15 17:34:42 +02001211#if defined(MBEDTLS_USE_PSA_CRYPTO)
1212 && psa_alg != PSA_ALG_NONE &&
Gilles Peskine449bd832023-01-11 14:50:10 +01001213 mbedtls_pk_can_do_ext(&key_cert->cert->pk, psa_alg,
1214 PSA_KEY_USAGE_SIGN_HASH) == 1
Ronald Cron67ea2542022-09-15 17:34:42 +02001215#endif /* MBEDTLS_USE_PSA_CRYPTO */
Gilles Peskine449bd832023-01-11 14:50:10 +01001216 ) {
XiaokangQianfb665a82022-06-15 03:57:21 +00001217 ssl->handshake->key_cert = key_cert;
Gilles Peskine449bd832023-01-11 14:50:10 +01001218 MBEDTLS_SSL_DEBUG_MSG(3,
1219 ("ssl_tls13_pick_key_cert:"
1220 "selected signature algorithm"
1221 " %s [%04x]",
1222 mbedtls_ssl_sig_alg_to_str(*sig_alg),
1223 *sig_alg));
XiaokangQianfb665a82022-06-15 03:57:21 +00001224 MBEDTLS_SSL_DEBUG_CRT(
Gilles Peskine449bd832023-01-11 14:50:10 +01001225 3, "selected certificate (chain)",
1226 ssl->handshake->key_cert->cert);
1227 return 0;
XiaokangQian81802f42022-06-10 13:25:22 +00001228 }
XiaokangQian81802f42022-06-10 13:25:22 +00001229 }
XiaokangQian23c5be62022-06-07 02:04:34 +00001230 }
1231
Gilles Peskine449bd832023-01-11 14:50:10 +01001232 MBEDTLS_SSL_DEBUG_MSG(2, ("ssl_tls13_pick_key_cert:"
1233 "no suitable certificate found"));
1234 return -1;
XiaokangQian23c5be62022-06-07 02:04:34 +00001235}
XiaokangQian81802f42022-06-10 13:25:22 +00001236#endif /* MBEDTLS_X509_CRT_PARSE_C &&
Ronald Cron928cbd32022-10-04 16:14:26 +02001237 MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED */
XiaokangQian23c5be62022-06-07 02:04:34 +00001238
XiaokangQian4080a7f2022-04-11 09:55:18 +00001239/*
XiaokangQianed582dd2022-04-13 08:21:05 +00001240 *
1241 * STATE HANDLING: ClientHello
1242 *
XiaokangQianb67384d2022-04-19 00:02:38 +00001243 * There are three possible classes of outcomes when parsing the ClientHello:
XiaokangQianed582dd2022-04-13 08:21:05 +00001244 *
XiaokangQianb67384d2022-04-19 00:02:38 +00001245 * 1) The ClientHello was well-formed and matched the server's configuration.
XiaokangQianed582dd2022-04-13 08:21:05 +00001246 *
1247 * In this case, the server progresses to sending its ServerHello.
1248 *
XiaokangQianb67384d2022-04-19 00:02:38 +00001249 * 2) The ClientHello was well-formed but didn't match the server's
1250 * configuration.
XiaokangQianed582dd2022-04-13 08:21:05 +00001251 *
1252 * For example, the client might not have offered a key share which
1253 * the server supports, or the server might require a cookie.
1254 *
1255 * In this case, the server sends a HelloRetryRequest.
1256 *
XiaokangQianb67384d2022-04-19 00:02:38 +00001257 * 3) The ClientHello was ill-formed
XiaokangQianed582dd2022-04-13 08:21:05 +00001258 *
1259 * In this case, we abort the handshake.
1260 *
1261 */
1262
1263/*
XiaokangQian4080a7f2022-04-11 09:55:18 +00001264 * Structure of this message:
1265 *
XiaokangQiane8ff3502022-04-22 02:34:40 +00001266 * uint16 ProtocolVersion;
1267 * opaque Random[32];
1268 * uint8 CipherSuite[2]; // Cryptographic suite selector
XiaokangQian4080a7f2022-04-11 09:55:18 +00001269 *
XiaokangQiane8ff3502022-04-22 02:34:40 +00001270 * struct {
1271 * ProtocolVersion legacy_version = 0x0303; // TLS v1.2
1272 * Random random;
1273 * opaque legacy_session_id<0..32>;
1274 * CipherSuite cipher_suites<2..2^16-2>;
1275 * opaque legacy_compression_methods<1..2^8-1>;
1276 * Extension extensions<8..2^16-1>;
1277 * } ClientHello;
XiaokangQian4080a7f2022-04-11 09:55:18 +00001278 */
XiaokangQianed582dd2022-04-13 08:21:05 +00001279
1280#define SSL_CLIENT_HELLO_OK 0
1281#define SSL_CLIENT_HELLO_HRR_REQUIRED 1
Ronald Cron5af4c7f2023-03-07 20:46:59 +01001282#define SSL_CLIENT_HELLO_TLS1_2 2
XiaokangQianed582dd2022-04-13 08:21:05 +00001283
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001284MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01001285static int ssl_tls13_parse_client_hello(mbedtls_ssl_context *ssl,
1286 const unsigned char *buf,
1287 const unsigned char *end)
XiaokangQian7807f9f2022-02-15 10:04:37 +00001288{
XiaokangQianb67384d2022-04-19 00:02:38 +00001289 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1290 const unsigned char *p = buf;
Ronald Crond540d992023-03-07 09:41:48 +01001291 const unsigned char *random;
XiaokangQian4080a7f2022-04-11 09:55:18 +00001292 size_t legacy_session_id_len;
Ronald Croncada4102023-03-07 09:51:39 +01001293 const unsigned char *legacy_session_id;
XiaokangQian318dc762022-04-20 09:43:51 +00001294 size_t cipher_suites_len;
Ronald Cron2f16b4e2023-03-07 10:07:32 +01001295 const unsigned char *cipher_suites;
XiaokangQian060d8672022-04-21 09:24:56 +00001296 const unsigned char *cipher_suites_end;
XiaokangQianb67384d2022-04-19 00:02:38 +00001297 size_t extensions_len;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001298 const unsigned char *extensions_end;
Ronald Croneff56732023-04-03 17:36:31 +02001299 const unsigned char *supported_versions_data;
1300 const unsigned char *supported_versions_data_end;
Jerry Yuc4bf5d62022-10-29 09:08:47 +08001301 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
Jerry Yu49ca9282022-05-05 11:05:22 +08001302 int hrr_required = 0;
Ronald Cron8a74f072023-06-14 17:59:29 +02001303 int no_usable_share_for_key_agreement = 0;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001304
Ronald Cron41a443a2022-10-04 16:38:25 +02001305#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED)
Jerry Yu29d9faa2022-08-23 17:52:45 +08001306 const unsigned char *pre_shared_key_ext = NULL;
Jerry Yu1c105562022-07-10 06:32:38 +00001307 const unsigned char *pre_shared_key_ext_end = NULL;
Ronald Cron41a443a2022-10-04 16:38:25 +02001308#endif
XiaokangQian7807f9f2022-02-15 10:04:37 +00001309
XiaokangQian7807f9f2022-02-15 10:04:37 +00001310 /*
XiaokangQianb67384d2022-04-19 00:02:38 +00001311 * ClientHello layout:
XiaokangQian7807f9f2022-02-15 10:04:37 +00001312 * 0 . 1 protocol version
XiaokangQiane8ff3502022-04-22 02:34:40 +00001313 * 2 . 33 random bytes
XiaokangQianc5763b52022-04-02 03:34:37 +00001314 * 34 . 34 session id length ( 1 byte )
XiaokangQian7807f9f2022-02-15 10:04:37 +00001315 * 35 . 34+x session id
XiaokangQian7807f9f2022-02-15 10:04:37 +00001316 * .. . .. ciphersuite list length ( 2 bytes )
1317 * .. . .. ciphersuite list
1318 * .. . .. compression alg. list length ( 1 byte )
1319 * .. . .. compression alg. list
1320 * .. . .. extensions length ( 2 bytes, optional )
1321 * .. . .. extensions ( optional )
1322 */
1323
XiaokangQianb67384d2022-04-19 00:02:38 +00001324 /*
bootstrap-prime6dbbf442022-05-17 19:30:44 -04001325 * Minimal length ( with everything empty and extensions omitted ) is
XiaokangQian7807f9f2022-02-15 10:04:37 +00001326 * 2 + 32 + 1 + 2 + 1 = 38 bytes. Check that first, so that we can
1327 * read at least up to session id length without worrying.
1328 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001329 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, 38);
XiaokangQian7807f9f2022-02-15 10:04:37 +00001330
1331 /* ...
1332 * ProtocolVersion legacy_version = 0x0303; // TLS 1.2
1333 * ...
1334 * with ProtocolVersion defined as:
1335 * uint16 ProtocolVersion;
1336 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001337 if (mbedtls_ssl_read_version(p, ssl->conf->transport) !=
1338 MBEDTLS_SSL_VERSION_TLS1_2) {
1339 MBEDTLS_SSL_DEBUG_MSG(1, ("Unsupported version of TLS."));
1340 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_PROTOCOL_VERSION,
1341 MBEDTLS_ERR_SSL_BAD_PROTOCOL_VERSION);
1342 return MBEDTLS_ERR_SSL_BAD_PROTOCOL_VERSION;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001343 }
1344 p += 2;
1345
Jerry Yuc1be19f2022-04-23 16:11:39 +08001346 /* ...
1347 * Random random;
1348 * ...
XiaokangQianb67384d2022-04-19 00:02:38 +00001349 * with Random defined as:
1350 * opaque Random[32];
XiaokangQian7807f9f2022-02-15 10:04:37 +00001351 */
Ronald Crond540d992023-03-07 09:41:48 +01001352 random = p;
XiaokangQian08037552022-04-20 07:16:41 +00001353 p += MBEDTLS_CLIENT_HELLO_RANDOM_LEN;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001354
Jerry Yuc1be19f2022-04-23 16:11:39 +08001355 /* ...
XiaokangQianb67384d2022-04-19 00:02:38 +00001356 * opaque legacy_session_id<0..32>;
Jerry Yuc1be19f2022-04-23 16:11:39 +08001357 * ...
XiaokangQian7807f9f2022-02-15 10:04:37 +00001358 */
Ronald Croncada4102023-03-07 09:51:39 +01001359 legacy_session_id_len = *(p++);
1360 legacy_session_id = p;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001361
XiaokangQianb67384d2022-04-19 00:02:38 +00001362 /*
1363 * Check we have enough data for the legacy session identifier
Jerry Yue95c8af2022-07-26 15:48:20 +08001364 * and the ciphersuite list length.
XiaokangQianb67384d2022-04-19 00:02:38 +00001365 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001366 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, legacy_session_id_len + 2);
XiaokangQian4080a7f2022-04-11 09:55:18 +00001367 p += legacy_session_id_len;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001368
Ronald Cron2f16b4e2023-03-07 10:07:32 +01001369 /* ...
1370 * CipherSuite cipher_suites<2..2^16-2>;
1371 * ...
1372 * with CipherSuite defined as:
1373 * uint8 CipherSuite[2];
1374 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001375 cipher_suites_len = MBEDTLS_GET_UINT16_BE(p, 0);
XiaokangQian7807f9f2022-02-15 10:04:37 +00001376 p += 2;
Ronald Cron2f16b4e2023-03-07 10:07:32 +01001377 cipher_suites = p;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001378
Ronald Cronfc7ae872023-02-16 15:32:19 +01001379 /*
1380 * The length of the ciphersuite list has to be even.
1381 */
1382 if (cipher_suites_len & 1) {
1383 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR,
1384 MBEDTLS_ERR_SSL_DECODE_ERROR);
1385 return MBEDTLS_ERR_SSL_DECODE_ERROR;
1386 }
1387
XiaokangQianb67384d2022-04-19 00:02:38 +00001388 /* Check we have enough data for the ciphersuite list, the legacy
1389 * compression methods and the length of the extensions.
Jerry Yue95c8af2022-07-26 15:48:20 +08001390 *
1391 * cipher_suites cipher_suites_len bytes
1392 * legacy_compression_methods 2 bytes
1393 * extensions_len 2 bytes
XiaokangQianb67384d2022-04-19 00:02:38 +00001394 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001395 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, cipher_suites_len + 2 + 2);
Ronald Cron8c527d02023-03-07 15:47:47 +01001396 p += cipher_suites_len;
1397 cipher_suites_end = p;
Jerry Yu5725f1c2022-08-21 17:27:16 +08001398
1399 /*
Ronald Cron8c527d02023-03-07 15:47:47 +01001400 * Search for the supported versions extension and parse it to determine
1401 * if the client supports TLS 1.3.
1402 */
1403 ret = mbedtls_ssl_tls13_is_supported_versions_ext_present_in_exts(
1404 ssl, p + 2, end,
Ronald Croneff56732023-04-03 17:36:31 +02001405 &supported_versions_data, &supported_versions_data_end);
Ronald Cron8c527d02023-03-07 15:47:47 +01001406 if (ret < 0) {
1407 MBEDTLS_SSL_DEBUG_RET(1,
1408 ("mbedtls_ssl_tls13_is_supported_versions_ext_present_in_exts"), ret);
1409 return ret;
1410 }
1411
1412 if (ret == 0) {
Ronald Cron5af4c7f2023-03-07 20:46:59 +01001413 return SSL_CLIENT_HELLO_TLS1_2;
Ronald Cron8c527d02023-03-07 15:47:47 +01001414 }
1415
Ronald Cron5af4c7f2023-03-07 20:46:59 +01001416 if (ret == 1) {
1417 ret = ssl_tls13_parse_supported_versions_ext(ssl,
Ronald Croneff56732023-04-03 17:36:31 +02001418 supported_versions_data,
1419 supported_versions_data_end);
Ronald Cron5af4c7f2023-03-07 20:46:59 +01001420 if (ret < 0) {
1421 MBEDTLS_SSL_DEBUG_RET(1,
1422 ("ssl_tls13_parse_supported_versions_ext"), ret);
1423 return ret;
1424 }
1425
Ronald Cronb828c7d2023-04-03 16:37:22 +02001426 /*
1427 * The supported versions extension was parsed successfully as the
1428 * value returned by ssl_tls13_parse_supported_versions_ext() is
1429 * positive. The return value is then equal to
1430 * MBEDTLS_SSL_VERSION_TLS1_2 or MBEDTLS_SSL_VERSION_TLS1_3, defining
1431 * the TLS version to negotiate.
1432 */
Ronald Cron5af4c7f2023-03-07 20:46:59 +01001433 if (MBEDTLS_SSL_VERSION_TLS1_2 == ret) {
1434 return SSL_CLIENT_HELLO_TLS1_2;
1435 }
Ronald Cron8c527d02023-03-07 15:47:47 +01001436 }
1437
1438 /*
1439 * We negotiate TLS 1.3.
Ronald Cron64582392023-03-07 09:21:40 +01001440 */
1441 ssl->tls_version = MBEDTLS_SSL_VERSION_TLS1_3;
1442
1443#if defined(MBEDTLS_SSL_SESSION_TICKETS)
1444 /* Store minor version for later use with ticket serialization. */
1445 ssl->session_negotiate->tls_version = MBEDTLS_SSL_VERSION_TLS1_3;
1446 ssl->session_negotiate->endpoint = ssl->conf->endpoint;
1447#endif
1448
1449 /*
Ronald Crondad02b22023-04-06 09:57:52 +02001450 * We are negotiating the version 1.3 of the protocol. Do what we have
Ronald Croncada4102023-03-07 09:51:39 +01001451 * postponed: copy of the client random bytes, copy of the legacy session
Ronald Cron2f16b4e2023-03-07 10:07:32 +01001452 * identifier and selection of the TLS 1.3 cipher suite.
Ronald Crond540d992023-03-07 09:41:48 +01001453 */
1454 MBEDTLS_SSL_DEBUG_BUF(3, "client hello, random bytes",
1455 random, MBEDTLS_CLIENT_HELLO_RANDOM_LEN);
1456 memcpy(&handshake->randbytes[0], random, MBEDTLS_CLIENT_HELLO_RANDOM_LEN);
1457
Ronald Croncada4102023-03-07 09:51:39 +01001458 if (legacy_session_id_len > sizeof(ssl->session_negotiate->id)) {
1459 MBEDTLS_SSL_DEBUG_MSG(1, ("bad client hello message"));
1460 return MBEDTLS_ERR_SSL_DECODE_ERROR;
1461 }
1462 ssl->session_negotiate->id_len = legacy_session_id_len;
1463 MBEDTLS_SSL_DEBUG_BUF(3, "client hello, session id",
1464 legacy_session_id, legacy_session_id_len);
1465 memcpy(&ssl->session_negotiate->id[0],
1466 legacy_session_id, legacy_session_id_len);
1467
Ronald Crond540d992023-03-07 09:41:48 +01001468 /*
Jerry Yu5725f1c2022-08-21 17:27:16 +08001469 * Search for a matching ciphersuite
1470 */
Ronald Cron2f16b4e2023-03-07 10:07:32 +01001471 MBEDTLS_SSL_DEBUG_BUF(3, "client hello, list of cipher suites",
1472 cipher_suites, cipher_suites_len);
Ronald Crone45afd72023-04-04 15:10:06 +02001473 for (const unsigned char *cipher_suites_p = cipher_suites;
1474 cipher_suites_p < cipher_suites_end; cipher_suites_p += 2) {
XiaokangQiane8ff3502022-04-22 02:34:40 +00001475 uint16_t cipher_suite;
Gilles Peskine449bd832023-01-11 14:50:10 +01001476 const mbedtls_ssl_ciphersuite_t *ciphersuite_info;
Jerry Yu5725f1c2022-08-21 17:27:16 +08001477
Ronald Crond89360b2023-02-21 08:53:33 +01001478 /*
Ronald Crone45afd72023-04-04 15:10:06 +02001479 * "cipher_suites_end - cipher_suites_p is even" is an invariant of the
1480 * loop. As cipher_suites_end - cipher_suites_p > 0, we have
1481 * cipher_suites_end - cipher_suites_p >= 2 and it is thus safe to read
1482 * two bytes.
Ronald Crond89360b2023-02-21 08:53:33 +01001483 */
Ronald Crone45afd72023-04-04 15:10:06 +02001484 cipher_suite = MBEDTLS_GET_UINT16_BE(cipher_suites_p, 0);
Jerry Yuc5a23a02022-08-25 10:51:44 +08001485 ciphersuite_info = ssl_tls13_validate_peer_ciphersuite(
Gilles Peskine449bd832023-01-11 14:50:10 +01001486 ssl, cipher_suite);
1487 if (ciphersuite_info == NULL) {
Jerry Yu5725f1c2022-08-21 17:27:16 +08001488 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +01001489 }
Jerry Yu5725f1c2022-08-21 17:27:16 +08001490
Jerry Yu5725f1c2022-08-21 17:27:16 +08001491 ssl->session_negotiate->ciphersuite = cipher_suite;
Jerry Yuc4bf5d62022-10-29 09:08:47 +08001492 handshake->ciphersuite_info = ciphersuite_info;
Gilles Peskine449bd832023-01-11 14:50:10 +01001493 MBEDTLS_SSL_DEBUG_MSG(2, ("selected ciphersuite: %04x - %s",
1494 cipher_suite,
1495 ciphersuite_info->name));
Ronald Cron25e9ec62023-02-16 15:35:16 +01001496 break;
XiaokangQian17f974c2022-04-19 09:57:41 +00001497 }
Jerry Yu5725f1c2022-08-21 17:27:16 +08001498
Gilles Peskine449bd832023-01-11 14:50:10 +01001499 if (handshake->ciphersuite_info == NULL) {
1500 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE,
1501 MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE);
1502 return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
Jerry Yu5725f1c2022-08-21 17:27:16 +08001503 }
Jerry Yuc1be19f2022-04-23 16:11:39 +08001504
XiaokangQian4080a7f2022-04-11 09:55:18 +00001505 /* ...
XiaokangQianb67384d2022-04-19 00:02:38 +00001506 * opaque legacy_compression_methods<1..2^8-1>;
XiaokangQian4080a7f2022-04-11 09:55:18 +00001507 * ...
XiaokangQian7807f9f2022-02-15 10:04:37 +00001508 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001509 if (p[0] != 1 || p[1] != MBEDTLS_SSL_COMPRESS_NULL) {
1510 MBEDTLS_SSL_DEBUG_MSG(1, ("bad legacy compression method"));
1511 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER,
1512 MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER);
1513 return MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001514 }
XiaokangQianb67384d2022-04-19 00:02:38 +00001515 p += 2;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001516
Jerry Yuc1be19f2022-04-23 16:11:39 +08001517 /* ...
XiaokangQianb67384d2022-04-19 00:02:38 +00001518 * Extension extensions<8..2^16-1>;
Jerry Yuc1be19f2022-04-23 16:11:39 +08001519 * ...
XiaokangQianb67384d2022-04-19 00:02:38 +00001520 * with Extension defined as:
1521 * struct {
1522 * ExtensionType extension_type;
1523 * opaque extension_data<0..2^16-1>;
1524 * } Extension;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001525 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001526 extensions_len = MBEDTLS_GET_UINT16_BE(p, 0);
XiaokangQian7807f9f2022-02-15 10:04:37 +00001527 p += 2;
Gilles Peskine449bd832023-01-11 14:50:10 +01001528 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, extensions_len);
XiaokangQiane8ff3502022-04-22 02:34:40 +00001529 extensions_end = p + extensions_len;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001530
Gilles Peskine449bd832023-01-11 14:50:10 +01001531 MBEDTLS_SSL_DEBUG_BUF(3, "client hello extensions", p, extensions_len);
Jerry Yu63a459c2022-10-31 13:38:40 +08001532 handshake->received_extensions = MBEDTLS_SSL_EXT_MASK_NONE;
Jerry Yu0c354a22022-08-29 15:25:36 +08001533
Gilles Peskine449bd832023-01-11 14:50:10 +01001534 while (p < extensions_end) {
XiaokangQian7807f9f2022-02-15 10:04:37 +00001535 unsigned int extension_type;
1536 size_t extension_data_len;
1537 const unsigned char *extension_data_end;
1538
Jerry Yu0c354a22022-08-29 15:25:36 +08001539 /* RFC 8446, section 4.2.11
Jerry Yu1c9247c2022-07-21 12:37:39 +08001540 *
1541 * The "pre_shared_key" extension MUST be the last extension in the
1542 * ClientHello (this facilitates implementation as described below).
1543 * Servers MUST check that it is the last extension and otherwise fail
1544 * the handshake with an "illegal_parameter" alert.
1545 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001546 if (handshake->received_extensions & MBEDTLS_SSL_EXT_MASK(PRE_SHARED_KEY)) {
Jerry Yu1c9247c2022-07-21 12:37:39 +08001547 MBEDTLS_SSL_DEBUG_MSG(
Gilles Peskine449bd832023-01-11 14:50:10 +01001548 3, ("pre_shared_key is not last extension."));
Jerry Yu1c9247c2022-07-21 12:37:39 +08001549 MBEDTLS_SSL_PEND_FATAL_ALERT(
1550 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER,
Gilles Peskine449bd832023-01-11 14:50:10 +01001551 MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER);
1552 return MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER;
Jerry Yu1c9247c2022-07-21 12:37:39 +08001553 }
1554
Gilles Peskine449bd832023-01-11 14:50:10 +01001555 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, extensions_end, 4);
1556 extension_type = MBEDTLS_GET_UINT16_BE(p, 0);
1557 extension_data_len = MBEDTLS_GET_UINT16_BE(p, 2);
XiaokangQian7807f9f2022-02-15 10:04:37 +00001558 p += 4;
1559
Gilles Peskine449bd832023-01-11 14:50:10 +01001560 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, extensions_end, extension_data_len);
XiaokangQian7807f9f2022-02-15 10:04:37 +00001561 extension_data_end = p + extension_data_len;
1562
Jerry Yuc4bf5d62022-10-29 09:08:47 +08001563 ret = mbedtls_ssl_tls13_check_received_extension(
Gilles Peskine449bd832023-01-11 14:50:10 +01001564 ssl, MBEDTLS_SSL_HS_CLIENT_HELLO, extension_type,
1565 MBEDTLS_SSL_TLS1_3_ALLOWED_EXTS_OF_CH);
1566 if (ret != 0) {
1567 return ret;
1568 }
Jerry Yue18dc7e2022-08-04 16:29:22 +08001569
Gilles Peskine449bd832023-01-11 14:50:10 +01001570 switch (extension_type) {
XiaokangQian40a35232022-05-07 09:02:40 +00001571#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
1572 case MBEDTLS_TLS_EXT_SERVERNAME:
Gilles Peskine449bd832023-01-11 14:50:10 +01001573 MBEDTLS_SSL_DEBUG_MSG(3, ("found ServerName extension"));
1574 ret = mbedtls_ssl_parse_server_name_ext(ssl, p,
1575 extension_data_end);
1576 if (ret != 0) {
XiaokangQian40a35232022-05-07 09:02:40 +00001577 MBEDTLS_SSL_DEBUG_RET(
Gilles Peskine449bd832023-01-11 14:50:10 +01001578 1, "mbedtls_ssl_parse_servername_ext", ret);
1579 return ret;
XiaokangQian40a35232022-05-07 09:02:40 +00001580 }
XiaokangQian40a35232022-05-07 09:02:40 +00001581 break;
1582#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
1583
Przemek Stekiel8c0a9532023-06-15 16:48:19 +02001584#if defined(PSA_WANT_ALG_ECDH) || defined(PSA_WANT_ALG_FFDH)
XiaokangQian7807f9f2022-02-15 10:04:37 +00001585 case MBEDTLS_TLS_EXT_SUPPORTED_GROUPS:
Gilles Peskine449bd832023-01-11 14:50:10 +01001586 MBEDTLS_SSL_DEBUG_MSG(3, ("found supported group extension"));
XiaokangQian7807f9f2022-02-15 10:04:37 +00001587
1588 /* Supported Groups Extension
1589 *
1590 * When sent by the client, the "supported_groups" extension
1591 * indicates the named groups which the client supports,
1592 * ordered from most preferred to least preferred.
1593 */
Jerry Yuc1be19f2022-04-23 16:11:39 +08001594 ret = ssl_tls13_parse_supported_groups_ext(
Gilles Peskine449bd832023-01-11 14:50:10 +01001595 ssl, p, extension_data_end);
1596 if (ret != 0) {
Xiaokang Qian9f1747b2023-03-30 02:50:04 +00001597 MBEDTLS_SSL_DEBUG_RET(
Xiaokang Qian8bce0e62023-04-04 10:15:48 +00001598 1, "ssl_tls13_parse_supported_groups_ext", ret);
Gilles Peskine449bd832023-01-11 14:50:10 +01001599 return ret;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001600 }
1601
XiaokangQian7807f9f2022-02-15 10:04:37 +00001602 break;
Przemek Stekiel8c0a9532023-06-15 16:48:19 +02001603#endif /* PSA_WANT_ALG_ECDH || PSA_WANT_ALG_FFDH*/
XiaokangQian7807f9f2022-02-15 10:04:37 +00001604
Valerio Settic9ae8622023-07-25 11:23:50 +02001605#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_EPHEMERAL_ENABLED)
XiaokangQian7807f9f2022-02-15 10:04:37 +00001606 case MBEDTLS_TLS_EXT_KEY_SHARE:
Gilles Peskine449bd832023-01-11 14:50:10 +01001607 MBEDTLS_SSL_DEBUG_MSG(3, ("found key share extension"));
XiaokangQian7807f9f2022-02-15 10:04:37 +00001608
1609 /*
1610 * Key Share Extension
1611 *
1612 * When sent by the client, the "key_share" extension
1613 * contains the endpoint's cryptographic parameters for
1614 * ECDHE/DHE key establishment methods.
1615 */
Jerry Yuc1be19f2022-04-23 16:11:39 +08001616 ret = ssl_tls13_parse_key_shares_ext(
Gilles Peskine449bd832023-01-11 14:50:10 +01001617 ssl, p, extension_data_end);
1618 if (ret == SSL_TLS1_3_PARSE_KEY_SHARES_EXT_NO_MATCH) {
Ronald Cron8a74f072023-06-14 17:59:29 +02001619 MBEDTLS_SSL_DEBUG_MSG(2, ("No usable share for key agreement."));
1620 no_usable_share_for_key_agreement = 1;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001621 }
1622
Gilles Peskine449bd832023-01-11 14:50:10 +01001623 if (ret < 0) {
Jerry Yu582dd062022-04-22 21:59:01 +08001624 MBEDTLS_SSL_DEBUG_RET(
Gilles Peskine449bd832023-01-11 14:50:10 +01001625 1, "ssl_tls13_parse_key_shares_ext", ret);
1626 return ret;
Jerry Yu582dd062022-04-22 21:59:01 +08001627 }
XiaokangQian7807f9f2022-02-15 10:04:37 +00001628
XiaokangQian7807f9f2022-02-15 10:04:37 +00001629 break;
Valerio Settic9ae8622023-07-25 11:23:50 +02001630#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_EPHEMERAL_ENABLED */
XiaokangQian7807f9f2022-02-15 10:04:37 +00001631
1632 case MBEDTLS_TLS_EXT_SUPPORTED_VERSIONS:
Ronald Cron8c527d02023-03-07 15:47:47 +01001633 /* Already parsed */
XiaokangQian7807f9f2022-02-15 10:04:37 +00001634 break;
1635
Ronald Cron41a443a2022-10-04 16:38:25 +02001636#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED)
Jerry Yue19e3b92022-07-08 12:04:51 +00001637 case MBEDTLS_TLS_EXT_PSK_KEY_EXCHANGE_MODES:
Xiaokang Qian9f1747b2023-03-30 02:50:04 +00001638 MBEDTLS_SSL_DEBUG_MSG(
1639 3, ("found psk key exchange modes extension"));
Jerry Yue19e3b92022-07-08 12:04:51 +00001640
1641 ret = ssl_tls13_parse_key_exchange_modes_ext(
Gilles Peskine449bd832023-01-11 14:50:10 +01001642 ssl, p, extension_data_end);
1643 if (ret != 0) {
Jerry Yue19e3b92022-07-08 12:04:51 +00001644 MBEDTLS_SSL_DEBUG_RET(
Gilles Peskine449bd832023-01-11 14:50:10 +01001645 1, "ssl_tls13_parse_key_exchange_modes_ext", ret);
1646 return ret;
Jerry Yue19e3b92022-07-08 12:04:51 +00001647 }
1648
Jerry Yue19e3b92022-07-08 12:04:51 +00001649 break;
Ronald Cron41a443a2022-10-04 16:38:25 +02001650#endif
Jerry Yue19e3b92022-07-08 12:04:51 +00001651
Jerry Yu1c105562022-07-10 06:32:38 +00001652 case MBEDTLS_TLS_EXT_PRE_SHARED_KEY:
Gilles Peskine449bd832023-01-11 14:50:10 +01001653 MBEDTLS_SSL_DEBUG_MSG(3, ("found pre_shared_key extension"));
1654 if ((handshake->received_extensions &
1655 MBEDTLS_SSL_EXT_MASK(PSK_KEY_EXCHANGE_MODES)) == 0) {
Jerry Yu13ab81d2022-07-22 23:17:11 +08001656 MBEDTLS_SSL_PEND_FATAL_ALERT(
1657 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER,
Gilles Peskine449bd832023-01-11 14:50:10 +01001658 MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER);
1659 return MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER;
Jerry Yu13ab81d2022-07-22 23:17:11 +08001660 }
Ronald Cron41a443a2022-10-04 16:38:25 +02001661#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED)
Jerry Yu1c105562022-07-10 06:32:38 +00001662 /* Delay processing of the PSK identity once we have
1663 * found out which algorithms to use. We keep a pointer
1664 * to the buffer and the size for later processing.
1665 */
Jerry Yu29d9faa2022-08-23 17:52:45 +08001666 pre_shared_key_ext = p;
Jerry Yu1c105562022-07-10 06:32:38 +00001667 pre_shared_key_ext_end = extension_data_end;
Jerry Yue18dc7e2022-08-04 16:29:22 +08001668#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED */
Jerry Yu1c105562022-07-10 06:32:38 +00001669 break;
Jerry Yu1c105562022-07-10 06:32:38 +00001670
XiaokangQianacb39922022-06-17 10:18:48 +00001671#if defined(MBEDTLS_SSL_ALPN)
1672 case MBEDTLS_TLS_EXT_ALPN:
Gilles Peskine449bd832023-01-11 14:50:10 +01001673 MBEDTLS_SSL_DEBUG_MSG(3, ("found alpn extension"));
XiaokangQianacb39922022-06-17 10:18:48 +00001674
Gilles Peskine449bd832023-01-11 14:50:10 +01001675 ret = mbedtls_ssl_parse_alpn_ext(ssl, p, extension_data_end);
1676 if (ret != 0) {
XiaokangQianacb39922022-06-17 10:18:48 +00001677 MBEDTLS_SSL_DEBUG_RET(
Gilles Peskine449bd832023-01-11 14:50:10 +01001678 1, ("mbedtls_ssl_parse_alpn_ext"), ret);
1679 return ret;
XiaokangQianacb39922022-06-17 10:18:48 +00001680 }
XiaokangQianacb39922022-06-17 10:18:48 +00001681 break;
1682#endif /* MBEDTLS_SSL_ALPN */
1683
Ronald Cron928cbd32022-10-04 16:14:26 +02001684#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED)
XiaokangQian7807f9f2022-02-15 10:04:37 +00001685 case MBEDTLS_TLS_EXT_SIG_ALG:
Gilles Peskine449bd832023-01-11 14:50:10 +01001686 MBEDTLS_SSL_DEBUG_MSG(3, ("found signature_algorithms extension"));
XiaokangQian7807f9f2022-02-15 10:04:37 +00001687
Gabor Mezei078e8032022-04-27 21:17:56 +02001688 ret = mbedtls_ssl_parse_sig_alg_ext(
Gilles Peskine449bd832023-01-11 14:50:10 +01001689 ssl, p, extension_data_end);
1690 if (ret != 0) {
Xiaokang Qian49f39c12023-04-06 02:31:02 +00001691 MBEDTLS_SSL_DEBUG_RET(
1692 1, "mbedtls_ssl_parse_sig_alg_ext", ret);
Gilles Peskine449bd832023-01-11 14:50:10 +01001693 return ret;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001694 }
XiaokangQian7807f9f2022-02-15 10:04:37 +00001695 break;
Ronald Cron928cbd32022-10-04 16:14:26 +02001696#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED */
XiaokangQian7807f9f2022-02-15 10:04:37 +00001697
Jan Bruckner151f6422023-02-10 12:45:19 +01001698#if defined(MBEDTLS_SSL_RECORD_SIZE_LIMIT)
1699 case MBEDTLS_TLS_EXT_RECORD_SIZE_LIMIT:
1700 MBEDTLS_SSL_DEBUG_MSG(3, ("found record_size_limit extension"));
1701
Xiaokang Qian9f1747b2023-03-30 02:50:04 +00001702 ret = mbedtls_ssl_tls13_parse_record_size_limit_ext(
1703 ssl, p, extension_data_end);
Jan Bruckner151f6422023-02-10 12:45:19 +01001704
Xiaokang Qian09c3ccc2023-04-04 10:18:38 +00001705 /*
1706 * TODO: Return unconditionally here until we handle the record
1707 * size limit correctly.
1708 * Once handled correctly, only return in case of errors.
1709 */
Jan Bruckner151f6422023-02-10 12:45:19 +01001710 return ret;
1711
1712 break;
1713#endif /* MBEDTLS_SSL_RECORD_SIZE_LIMIT */
1714
XiaokangQian7807f9f2022-02-15 10:04:37 +00001715 default:
Jerry Yu79aa7212022-11-08 21:30:21 +08001716 MBEDTLS_SSL_PRINT_EXT(
Jerry Yu63a459c2022-10-31 13:38:40 +08001717 3, MBEDTLS_SSL_HS_CLIENT_HELLO,
Gilles Peskine449bd832023-01-11 14:50:10 +01001718 extension_type, "( ignored )");
Jerry Yu0c354a22022-08-29 15:25:36 +08001719 break;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001720 }
1721
1722 p += extension_data_len;
1723 }
1724
Gilles Peskine449bd832023-01-11 14:50:10 +01001725 MBEDTLS_SSL_PRINT_EXTS(3, MBEDTLS_SSL_HS_CLIENT_HELLO,
1726 handshake->received_extensions);
Jerry Yue18dc7e2022-08-04 16:29:22 +08001727
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +01001728 ret = mbedtls_ssl_add_hs_hdr_to_checksum(ssl,
1729 MBEDTLS_SSL_HS_CLIENT_HELLO,
1730 p - buf);
1731 if (0 != ret) {
1732 MBEDTLS_SSL_DEBUG_RET(1, ("mbedtls_ssl_add_hs_hdr_to_checksum"), ret);
1733 return ret;
1734 }
Jerry Yu032b15ce2022-07-11 06:10:03 +00001735
Ronald Cron41a443a2022-10-04 16:38:25 +02001736#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED)
XiaokangQian7807f9f2022-02-15 10:04:37 +00001737 /* Update checksum with either
1738 * - The entire content of the CH message, if no PSK extension is present
1739 * - The content up to but excluding the PSK extension, if present.
1740 */
Jerry Yu1c105562022-07-10 06:32:38 +00001741 /* If we've settled on a PSK-based exchange, parse PSK identity ext */
Pengyu Lvcfb23b82023-10-30 15:26:26 +08001742 if (ssl_tls13_check_psk_key_exchange(ssl) ||
1743 ssl_tls13_check_psk_ephemeral_key_exchange(ssl)) {
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +01001744 ret = handshake->update_checksum(ssl, buf,
1745 pre_shared_key_ext - buf);
1746 if (0 != ret) {
1747 MBEDTLS_SSL_DEBUG_RET(1, ("update_checksum"), ret);
1748 return ret;
1749 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001750 ret = ssl_tls13_parse_pre_shared_key_ext(ssl,
1751 pre_shared_key_ext,
1752 pre_shared_key_ext_end,
1753 cipher_suites,
1754 cipher_suites_end);
1755 if (ret == MBEDTLS_ERR_SSL_UNKNOWN_IDENTITY) {
1756 handshake->received_extensions &= ~MBEDTLS_SSL_EXT_MASK(PRE_SHARED_KEY);
1757 } else if (ret != 0) {
Jerry Yu63a459c2022-10-31 13:38:40 +08001758 MBEDTLS_SSL_DEBUG_RET(
Gilles Peskine449bd832023-01-11 14:50:10 +01001759 1, "ssl_tls13_parse_pre_shared_key_ext", ret);
1760 return ret;
Jerry Yu1c105562022-07-10 06:32:38 +00001761 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001762 } else
Ronald Cron41a443a2022-10-04 16:38:25 +02001763#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED */
Jerry Yu1c105562022-07-10 06:32:38 +00001764 {
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +01001765 ret = handshake->update_checksum(ssl, buf, p - buf);
1766 if (0 != ret) {
1767 MBEDTLS_SSL_DEBUG_RET(1, ("update_checksum"), ret);
1768 return ret;
1769 }
Jerry Yu1c105562022-07-10 06:32:38 +00001770 }
XiaokangQian7807f9f2022-02-15 10:04:37 +00001771
Gilles Peskine449bd832023-01-11 14:50:10 +01001772 ret = ssl_tls13_determine_key_exchange_mode(ssl);
1773 if (ret < 0) {
1774 return ret;
1775 }
XiaokangQian7807f9f2022-02-15 10:04:37 +00001776
Ronald Cron8a74f072023-06-14 17:59:29 +02001777 if (ssl->handshake->key_exchange_mode !=
1778 MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK) {
1779 hrr_required = (no_usable_share_for_key_agreement != 0);
1780 }
1781
Gilles Peskine449bd832023-01-11 14:50:10 +01001782 mbedtls_ssl_optimize_checksum(ssl, handshake->ciphersuite_info);
Jerry Yue5834fd2022-08-29 20:16:09 +08001783
Gilles Peskine449bd832023-01-11 14:50:10 +01001784 return hrr_required ? SSL_CLIENT_HELLO_HRR_REQUIRED : SSL_CLIENT_HELLO_OK;
XiaokangQian75fe8c72022-06-15 09:42:45 +00001785}
1786
Jerry Yuab0da372023-02-08 13:55:24 +08001787#if defined(MBEDTLS_SSL_EARLY_DATA)
1788static void ssl_tls13_update_early_data_status(mbedtls_ssl_context *ssl)
1789{
1790 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
1791
1792 if ((handshake->received_extensions &
1793 MBEDTLS_SSL_EXT_MASK(EARLY_DATA)) == 0) {
1794 MBEDTLS_SSL_DEBUG_MSG(
Jerry Yub47b2992023-10-18 15:50:35 +08001795 1, ("EarlyData: no early data extension received."));
Jerry Yuab0da372023-02-08 13:55:24 +08001796 ssl->early_data_status = MBEDTLS_SSL_EARLY_DATA_STATUS_NOT_RECEIVED;
1797 return;
1798 }
1799
1800 ssl->early_data_status = MBEDTLS_SSL_EARLY_DATA_STATUS_REJECTED;
1801
Jerry Yu985c9672022-12-04 14:06:30 +08001802 if (ssl->conf->early_data_enabled == MBEDTLS_SSL_EARLY_DATA_DISABLED) {
1803 MBEDTLS_SSL_DEBUG_MSG(
Jerry Yu985c9672022-12-04 14:06:30 +08001804 1,
Jerry Yu454dda32023-10-31 15:13:54 +08001805 ("EarlyData: rejected, feature disabled in server configuration."));
Jerry Yu985c9672022-12-04 14:06:30 +08001806 return;
1807 }
1808
Jerry Yu454dda32023-10-31 15:13:54 +08001809 if (!handshake->resume) {
1810 /* We currently support early data only in the case of PSKs established
1811 via a NewSessionTicket message thus in the case of a session
1812 resumption. */
Jerry Yu985c9672022-12-04 14:06:30 +08001813 MBEDTLS_SSL_DEBUG_MSG(
Jerry Yu7ef9fd82023-11-07 14:30:38 +08001814 1, ("EarlyData: rejected, not a session resumption."));
Jerry Yu985c9672022-12-04 14:06:30 +08001815 return;
1816 }
1817
Jerry Yu82fd6c12023-10-31 16:32:19 +08001818 /* RFC 8446 4.2.10
1819 *
1820 * In order to accept early data, the server MUST have accepted a PSK cipher
1821 * suite and selected the first key offered in the client's "pre_shared_key"
1822 * extension. In addition, it MUST verify that the following values are the
1823 * same as those associated with the selected PSK:
1824 * - The TLS version number
1825 * - The selected cipher suite
1826 * - The selected ALPN [RFC7301] protocol, if any
1827 *
1828 * NOTE:
Jerry Yu7ef9fd82023-11-07 14:30:38 +08001829 * - The TLS version number is checked in
1830 * ssl_tls13_offered_psks_check_identity_match_ticket().
1831 * - ALPN is not checked for the time being (TODO).
Jerry Yu82fd6c12023-10-31 16:32:19 +08001832 */
1833
1834 if (handshake->selected_identity != 0) {
1835 MBEDTLS_SSL_DEBUG_MSG(
Jerry Yu7ef9fd82023-11-07 14:30:38 +08001836 1, ("EarlyData: rejected, the selected key in "
1837 "`pre_shared_key` is not the first one."));
Jerry Yu82fd6c12023-10-31 16:32:19 +08001838 return;
1839 }
1840
1841 if (handshake->ciphersuite_info->id !=
1842 ssl->session_negotiate->ciphersuite) {
1843 MBEDTLS_SSL_DEBUG_MSG(
Jerry Yu7ef9fd82023-11-07 14:30:38 +08001844 1, ("EarlyData: rejected, the selected ciphersuite is not the one "
1845 "of the selected pre-shared key."));
Jerry Yu82fd6c12023-10-31 16:32:19 +08001846 return;
1847
1848 }
Jerry Yu985c9672022-12-04 14:06:30 +08001849
Jerry Yu985c9672022-12-04 14:06:30 +08001850
1851 ssl->early_data_status = MBEDTLS_SSL_EARLY_DATA_STATUS_ACCEPTED;
1852
Jerry Yuab0da372023-02-08 13:55:24 +08001853}
1854#endif /* MBEDTLS_SSL_EARLY_DATA */
1855
XiaokangQian75fe8c72022-06-15 09:42:45 +00001856/* Update the handshake state machine */
1857
Ronald Cronce7d76e2022-07-08 18:56:49 +02001858MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01001859static int ssl_tls13_postprocess_client_hello(mbedtls_ssl_context *ssl)
XiaokangQian75fe8c72022-06-15 09:42:45 +00001860{
1861 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1862
XiaokangQian7807f9f2022-02-15 10:04:37 +00001863 /*
XiaokangQianfb665a82022-06-15 03:57:21 +00001864 * Server certificate selection
1865 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001866 if (ssl->conf->f_cert_cb && (ret = ssl->conf->f_cert_cb(ssl)) != 0) {
1867 MBEDTLS_SSL_DEBUG_RET(1, "f_cert_cb", ret);
1868 return ret;
XiaokangQianfb665a82022-06-15 03:57:21 +00001869 }
1870#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
1871 ssl->handshake->sni_name = NULL;
1872 ssl->handshake->sni_name_len = 0;
1873#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
XiaokangQian7807f9f2022-02-15 10:04:37 +00001874
Gilles Peskine449bd832023-01-11 14:50:10 +01001875 ret = mbedtls_ssl_tls13_key_schedule_stage_early(ssl);
1876 if (ret != 0) {
1877 MBEDTLS_SSL_DEBUG_RET(1,
1878 "mbedtls_ssl_tls1_3_key_schedule_stage_early", ret);
1879 return ret;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001880 }
1881
Jerry Yuab0da372023-02-08 13:55:24 +08001882#if defined(MBEDTLS_SSL_EARLY_DATA)
1883 /* There is enough information, update early data state. */
1884 ssl_tls13_update_early_data_status(ssl);
Jerry Yuab0da372023-02-08 13:55:24 +08001885#endif /* MBEDTLS_SSL_EARLY_DATA */
1886
Gilles Peskine449bd832023-01-11 14:50:10 +01001887 return 0;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001888
1889}
1890
1891/*
XiaokangQianed582dd2022-04-13 08:21:05 +00001892 * Main entry point from the state machine; orchestrates the otherfunctions.
1893 */
1894
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001895MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01001896static int ssl_tls13_process_client_hello(mbedtls_ssl_context *ssl)
XiaokangQianed582dd2022-04-13 08:21:05 +00001897{
1898
XiaokangQian08037552022-04-20 07:16:41 +00001899 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine449bd832023-01-11 14:50:10 +01001900 unsigned char *buf = NULL;
XiaokangQianed582dd2022-04-13 08:21:05 +00001901 size_t buflen = 0;
Jerry Yu4ca91402022-05-09 15:50:57 +08001902 int parse_client_hello_ret;
1903
Gilles Peskine449bd832023-01-11 14:50:10 +01001904 MBEDTLS_SSL_DEBUG_MSG(2, ("=> parse client hello"));
XiaokangQianed582dd2022-04-13 08:21:05 +00001905
Gilles Peskine449bd832023-01-11 14:50:10 +01001906 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_tls13_fetch_handshake_msg(
1907 ssl, MBEDTLS_SSL_HS_CLIENT_HELLO,
1908 &buf, &buflen));
XiaokangQianed582dd2022-04-13 08:21:05 +00001909
Gilles Peskine449bd832023-01-11 14:50:10 +01001910 MBEDTLS_SSL_PROC_CHK_NEG(ssl_tls13_parse_client_hello(ssl, buf,
1911 buf + buflen));
Ronald Cron5af4c7f2023-03-07 20:46:59 +01001912 parse_client_hello_ret = ret; /* Store positive return value of
1913 * parse_client_hello,
1914 * as negative error codes are handled
Jerry Yuf41553b2022-05-09 22:20:30 +08001915 * by MBEDTLS_SSL_PROC_CHK_NEG. */
Jerry Yu4ca91402022-05-09 15:50:57 +08001916
Ronald Cronb828c7d2023-04-03 16:37:22 +02001917 /*
1918 * Version 1.2 of the protocol has been chosen, set the
1919 * ssl->keep_current_message flag for the ClientHello to be kept and parsed
1920 * as a TLS 1.2 ClientHello. We also change ssl->tls_version to
1921 * MBEDTLS_SSL_VERSION_TLS1_2 thus from now on mbedtls_ssl_handshake_step()
1922 * will dispatch to the TLS 1.2 state machine.
1923 */
Ronald Cron5af4c7f2023-03-07 20:46:59 +01001924 if (SSL_CLIENT_HELLO_TLS1_2 == parse_client_hello_ret) {
1925 ssl->keep_current_message = 1;
1926 ssl->tls_version = MBEDTLS_SSL_VERSION_TLS1_2;
1927 return 0;
1928 }
1929
Gilles Peskine449bd832023-01-11 14:50:10 +01001930 MBEDTLS_SSL_PROC_CHK(ssl_tls13_postprocess_client_hello(ssl));
Jerry Yu582dd062022-04-22 21:59:01 +08001931
Ronald Cron5af4c7f2023-03-07 20:46:59 +01001932 if (SSL_CLIENT_HELLO_OK == parse_client_hello_ret) {
Gilles Peskine449bd832023-01-11 14:50:10 +01001933 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_SERVER_HELLO);
1934 } else {
1935 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_HELLO_RETRY_REQUEST);
1936 }
XiaokangQianed582dd2022-04-13 08:21:05 +00001937
1938cleanup:
1939
Gilles Peskine449bd832023-01-11 14:50:10 +01001940 MBEDTLS_SSL_DEBUG_MSG(2, ("<= parse client hello"));
1941 return ret;
XiaokangQianed582dd2022-04-13 08:21:05 +00001942}
1943
1944/*
Jerry Yu1c3e6882022-04-20 21:23:40 +08001945 * Handler for MBEDTLS_SSL_SERVER_HELLO
Jerry Yu5b64ae92022-03-30 17:15:02 +08001946 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001947MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01001948static int ssl_tls13_prepare_server_hello(mbedtls_ssl_context *ssl)
Jerry Yuf4b27e42022-03-30 17:32:21 +08001949{
Jerry Yu637a3f12022-04-20 21:37:58 +08001950 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1951 unsigned char *server_randbytes =
Gilles Peskine449bd832023-01-11 14:50:10 +01001952 ssl->handshake->randbytes + MBEDTLS_CLIENT_HELLO_RANDOM_LEN;
1953 if (ssl->conf->f_rng == NULL) {
1954 MBEDTLS_SSL_DEBUG_MSG(1, ("no RNG provided"));
1955 return MBEDTLS_ERR_SSL_NO_RNG;
Jerry Yuf4b27e42022-03-30 17:32:21 +08001956 }
1957
Gilles Peskine449bd832023-01-11 14:50:10 +01001958 if ((ret = ssl->conf->f_rng(ssl->conf->p_rng, server_randbytes,
1959 MBEDTLS_SERVER_HELLO_RANDOM_LEN)) != 0) {
1960 MBEDTLS_SSL_DEBUG_RET(1, "f_rng", ret);
1961 return ret;
Jerry Yuf4b27e42022-03-30 17:32:21 +08001962 }
1963
Gilles Peskine449bd832023-01-11 14:50:10 +01001964 MBEDTLS_SSL_DEBUG_BUF(3, "server hello, random bytes", server_randbytes,
1965 MBEDTLS_SERVER_HELLO_RANDOM_LEN);
Jerry Yuf4b27e42022-03-30 17:32:21 +08001966
1967#if defined(MBEDTLS_HAVE_TIME)
YxCda609132023-05-22 12:08:12 -07001968 ssl->session_negotiate->start = mbedtls_time(NULL);
Jerry Yuf4b27e42022-03-30 17:32:21 +08001969#endif /* MBEDTLS_HAVE_TIME */
1970
Gilles Peskine449bd832023-01-11 14:50:10 +01001971 return ret;
Jerry Yuf4b27e42022-03-30 17:32:21 +08001972}
1973
Jerry Yu3bf2c642022-03-30 22:02:12 +08001974/*
Jerry Yue74e04a2022-04-21 09:23:16 +08001975 * ssl_tls13_write_server_hello_supported_versions_ext ():
Jerry Yu3bf2c642022-03-30 22:02:12 +08001976 *
1977 * struct {
Jerry Yufb9f54d2022-04-06 10:08:34 +08001978 * ProtocolVersion selected_version;
Jerry Yu3bf2c642022-03-30 22:02:12 +08001979 * } SupportedVersions;
1980 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001981MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yue74e04a2022-04-21 09:23:16 +08001982static int ssl_tls13_write_server_hello_supported_versions_ext(
Gilles Peskine449bd832023-01-11 14:50:10 +01001983 mbedtls_ssl_context *ssl,
1984 unsigned char *buf,
1985 unsigned char *end,
1986 size_t *out_len)
Jerry Yu3bf2c642022-03-30 22:02:12 +08001987{
Jerry Yu3bf2c642022-03-30 22:02:12 +08001988 *out_len = 0;
1989
Gilles Peskine449bd832023-01-11 14:50:10 +01001990 MBEDTLS_SSL_DEBUG_MSG(3, ("server hello, write selected version"));
Jerry Yu3bf2c642022-03-30 22:02:12 +08001991
1992 /* Check if we have space to write the extension:
1993 * - extension_type (2 bytes)
1994 * - extension_data_length (2 bytes)
Jerry Yu349a6132022-04-14 20:52:56 +08001995 * - selected_version (2 bytes)
Jerry Yu3bf2c642022-03-30 22:02:12 +08001996 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001997 MBEDTLS_SSL_CHK_BUF_PTR(buf, end, 6);
Jerry Yu3bf2c642022-03-30 22:02:12 +08001998
Gilles Peskine449bd832023-01-11 14:50:10 +01001999 MBEDTLS_PUT_UINT16_BE(MBEDTLS_TLS_EXT_SUPPORTED_VERSIONS, buf, 0);
Jerry Yu3bf2c642022-03-30 22:02:12 +08002000
Gilles Peskine449bd832023-01-11 14:50:10 +01002001 MBEDTLS_PUT_UINT16_BE(2, buf, 2);
Jerry Yu3bf2c642022-03-30 22:02:12 +08002002
Gilles Peskine449bd832023-01-11 14:50:10 +01002003 mbedtls_ssl_write_version(buf + 4,
2004 ssl->conf->transport,
2005 ssl->tls_version);
Jerry Yu3bf2c642022-03-30 22:02:12 +08002006
Gilles Peskine449bd832023-01-11 14:50:10 +01002007 MBEDTLS_SSL_DEBUG_MSG(3, ("supported version: [%04x]",
2008 ssl->tls_version));
Jerry Yu3bf2c642022-03-30 22:02:12 +08002009
Jerry Yu349a6132022-04-14 20:52:56 +08002010 *out_len = 6;
Jerry Yu3bf2c642022-03-30 22:02:12 +08002011
Jerry Yub95dd362022-11-08 21:19:34 +08002012 mbedtls_ssl_tls13_set_hs_sent_ext_mask(
Gilles Peskine449bd832023-01-11 14:50:10 +01002013 ssl, MBEDTLS_TLS_EXT_SUPPORTED_VERSIONS);
Jerry Yub95dd362022-11-08 21:19:34 +08002014
Gilles Peskine449bd832023-01-11 14:50:10 +01002015 return 0;
Jerry Yu3bf2c642022-03-30 22:02:12 +08002016}
2017
Jerry Yud9436a12022-04-20 22:28:09 +08002018
Jerry Yu3bf2c642022-03-30 22:02:12 +08002019
2020/* Generate and export a single key share. For hybrid KEMs, this can
2021 * be called multiple times with the different components of the hybrid. */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002022MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002023static int ssl_tls13_generate_and_write_key_share(mbedtls_ssl_context *ssl,
2024 uint16_t named_group,
2025 unsigned char *buf,
2026 unsigned char *end,
2027 size_t *out_len)
Jerry Yu3bf2c642022-03-30 22:02:12 +08002028{
2029 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yu955ddd72022-04-22 22:27:33 +08002030
2031 *out_len = 0;
2032
Valerio Settic9ae8622023-07-25 11:23:50 +02002033#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_EPHEMERAL_ENABLED)
Przemek Stekiel29c219c2023-05-31 15:21:04 +02002034 if (mbedtls_ssl_tls13_named_group_is_ecdhe(named_group) ||
Przemek Stekield5f79e72023-06-29 09:08:43 +02002035 mbedtls_ssl_tls13_named_group_is_ffdh(named_group)) {
Przemek Stekiel408569f2023-07-06 11:26:44 +02002036 ret = mbedtls_ssl_tls13_generate_and_write_xxdh_key_exchange(
Gilles Peskine449bd832023-01-11 14:50:10 +01002037 ssl, named_group, buf, end, out_len);
2038 if (ret != 0) {
Jerry Yu89e103c2022-03-30 22:43:29 +08002039 MBEDTLS_SSL_DEBUG_RET(
Przemek Stekiel408569f2023-07-06 11:26:44 +02002040 1, "mbedtls_ssl_tls13_generate_and_write_xxdh_key_exchange",
Gilles Peskine449bd832023-01-11 14:50:10 +01002041 ret);
2042 return ret;
Jerry Yu3bf2c642022-03-30 22:02:12 +08002043 }
Gilles Peskine449bd832023-01-11 14:50:10 +01002044 } else
Valerio Settic9ae8622023-07-25 11:23:50 +02002045#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_EPHEMERAL_ENABLED */
Gilles Peskine449bd832023-01-11 14:50:10 +01002046 if (0 /* Other kinds of KEMs */) {
2047 } else {
Jerry Yu955ddd72022-04-22 22:27:33 +08002048 ((void) ssl);
2049 ((void) named_group);
2050 ((void) buf);
2051 ((void) end);
Jerry Yu3bf2c642022-03-30 22:02:12 +08002052 ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
2053 }
2054
Gilles Peskine449bd832023-01-11 14:50:10 +01002055 return ret;
Jerry Yu3bf2c642022-03-30 22:02:12 +08002056}
2057
2058/*
2059 * ssl_tls13_write_key_share_ext
2060 *
2061 * Structure of key_share extension in ServerHello:
2062 *
Jerry Yu1c3e6882022-04-20 21:23:40 +08002063 * struct {
2064 * NamedGroup group;
2065 * opaque key_exchange<1..2^16-1>;
2066 * } KeyShareEntry;
2067 * struct {
2068 * KeyShareEntry server_share;
2069 * } KeyShareServerHello;
Jerry Yu3bf2c642022-03-30 22:02:12 +08002070 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002071MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002072static int ssl_tls13_write_key_share_ext(mbedtls_ssl_context *ssl,
2073 unsigned char *buf,
2074 unsigned char *end,
2075 size_t *out_len)
Jerry Yu3bf2c642022-03-30 22:02:12 +08002076{
Jerry Yu955ddd72022-04-22 22:27:33 +08002077 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yu3bf2c642022-03-30 22:02:12 +08002078 unsigned char *p = buf;
Jerry Yu57d48412022-04-20 21:50:42 +08002079 uint16_t group = ssl->handshake->offered_group_id;
Jerry Yu3bf2c642022-03-30 22:02:12 +08002080 unsigned char *server_share = buf + 4;
Jerry Yu3bf2c642022-03-30 22:02:12 +08002081 size_t key_exchange_length;
Jerry Yu3bf2c642022-03-30 22:02:12 +08002082
2083 *out_len = 0;
2084
Gilles Peskine449bd832023-01-11 14:50:10 +01002085 MBEDTLS_SSL_DEBUG_MSG(3, ("server hello, adding key share extension"));
Jerry Yu3bf2c642022-03-30 22:02:12 +08002086
Gilles Peskine449bd832023-01-11 14:50:10 +01002087 MBEDTLS_SSL_DEBUG_MSG(2, ("server hello, write selected_group: %s (%04x)",
2088 mbedtls_ssl_named_group_to_str(group),
2089 group));
Jerry Yu58af2332022-09-06 11:19:31 +08002090
Jerry Yu3bf2c642022-03-30 22:02:12 +08002091 /* Check if we have space for header and length fields:
2092 * - extension_type (2 bytes)
2093 * - extension_data_length (2 bytes)
2094 * - group (2 bytes)
2095 * - key_exchange_length (2 bytes)
2096 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002097 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 8);
2098 MBEDTLS_PUT_UINT16_BE(MBEDTLS_TLS_EXT_KEY_SHARE, p, 0);
2099 MBEDTLS_PUT_UINT16_BE(group, server_share, 0);
Jerry Yu3bf2c642022-03-30 22:02:12 +08002100 p += 8;
Jerry Yu57d48412022-04-20 21:50:42 +08002101
Jerry Yu3bf2c642022-03-30 22:02:12 +08002102 /* When we introduce PQC-ECDHE hybrids, we'll want to call this
2103 * function multiple times. */
Jerry Yu955ddd72022-04-22 22:27:33 +08002104 ret = ssl_tls13_generate_and_write_key_share(
Gilles Peskine449bd832023-01-11 14:50:10 +01002105 ssl, group, server_share + 4, end, &key_exchange_length);
2106 if (ret != 0) {
2107 return ret;
2108 }
Jerry Yu3bf2c642022-03-30 22:02:12 +08002109 p += key_exchange_length;
Jerry Yuc1be19f2022-04-23 16:11:39 +08002110
Gilles Peskine449bd832023-01-11 14:50:10 +01002111 MBEDTLS_PUT_UINT16_BE(key_exchange_length, server_share + 2, 0);
Jerry Yu3bf2c642022-03-30 22:02:12 +08002112
Gilles Peskine449bd832023-01-11 14:50:10 +01002113 MBEDTLS_PUT_UINT16_BE(p - server_share, buf, 2);
Jerry Yu3bf2c642022-03-30 22:02:12 +08002114
Jerry Yu57d48412022-04-20 21:50:42 +08002115 *out_len = p - buf;
Jerry Yu955ddd72022-04-22 22:27:33 +08002116
Gilles Peskine449bd832023-01-11 14:50:10 +01002117 mbedtls_ssl_tls13_set_hs_sent_ext_mask(ssl, MBEDTLS_TLS_EXT_KEY_SHARE);
Jerry Yub95dd362022-11-08 21:19:34 +08002118
Gilles Peskine449bd832023-01-11 14:50:10 +01002119 return 0;
Jerry Yu3bf2c642022-03-30 22:02:12 +08002120}
Jerry Yud9436a12022-04-20 22:28:09 +08002121
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002122MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002123static int ssl_tls13_write_hrr_key_share_ext(mbedtls_ssl_context *ssl,
2124 unsigned char *buf,
2125 unsigned char *end,
2126 size_t *out_len)
Jerry Yu23f7a6f2022-04-23 15:16:45 +08002127{
2128 uint16_t selected_group = ssl->handshake->hrr_selected_group;
2129 /* key_share Extension
2130 *
2131 * struct {
2132 * select (Handshake.msg_type) {
2133 * ...
2134 * case hello_retry_request:
2135 * NamedGroup selected_group;
2136 * ...
2137 * };
2138 * } KeyShare;
2139 */
2140
2141 *out_len = 0;
2142
Jerry Yub0ac10b2022-05-05 11:10:08 +08002143 /*
2144 * For a pure PSK key exchange, there is no group to agree upon. The purpose
2145 * of the HRR is then to transmit a cookie to force the client to demonstrate
2146 * reachability at their apparent network address (primarily useful for DTLS).
2147 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002148 if (!mbedtls_ssl_tls13_key_exchange_mode_with_ephemeral(ssl)) {
2149 return 0;
2150 }
Jerry Yu23f7a6f2022-04-23 15:16:45 +08002151
2152 /* We should only send the key_share extension if the client's initial
2153 * key share was not acceptable. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002154 if (ssl->handshake->offered_group_id != 0) {
2155 MBEDTLS_SSL_DEBUG_MSG(4, ("Skip key_share extension in HRR"));
2156 return 0;
Jerry Yu23f7a6f2022-04-23 15:16:45 +08002157 }
2158
Gilles Peskine449bd832023-01-11 14:50:10 +01002159 if (selected_group == 0) {
2160 MBEDTLS_SSL_DEBUG_MSG(1, ("no matching named group found"));
2161 return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
Jerry Yu23f7a6f2022-04-23 15:16:45 +08002162 }
2163
Jerry Yub0ac10b2022-05-05 11:10:08 +08002164 /* Check if we have enough space:
2165 * - extension_type (2 bytes)
2166 * - extension_data_length (2 bytes)
2167 * - selected_group (2 bytes)
2168 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002169 MBEDTLS_SSL_CHK_BUF_PTR(buf, end, 6);
Jerry Yu23f7a6f2022-04-23 15:16:45 +08002170
Gilles Peskine449bd832023-01-11 14:50:10 +01002171 MBEDTLS_PUT_UINT16_BE(MBEDTLS_TLS_EXT_KEY_SHARE, buf, 0);
2172 MBEDTLS_PUT_UINT16_BE(2, buf, 2);
2173 MBEDTLS_PUT_UINT16_BE(selected_group, buf, 4);
Jerry Yu23f7a6f2022-04-23 15:16:45 +08002174
Gilles Peskine449bd832023-01-11 14:50:10 +01002175 MBEDTLS_SSL_DEBUG_MSG(3,
2176 ("HRR selected_group: %s (%x)",
2177 mbedtls_ssl_named_group_to_str(selected_group),
2178 selected_group));
Jerry Yu23f7a6f2022-04-23 15:16:45 +08002179
2180 *out_len = 6;
Jerry Yu23f7a6f2022-04-23 15:16:45 +08002181
Gilles Peskine449bd832023-01-11 14:50:10 +01002182 mbedtls_ssl_tls13_set_hs_sent_ext_mask(ssl, MBEDTLS_TLS_EXT_KEY_SHARE);
Jerry Yub95dd362022-11-08 21:19:34 +08002183
Gilles Peskine449bd832023-01-11 14:50:10 +01002184 return 0;
Jerry Yu23f7a6f2022-04-23 15:16:45 +08002185}
Jerry Yu3bf2c642022-03-30 22:02:12 +08002186
2187/*
2188 * Structure of ServerHello message:
2189 *
2190 * struct {
2191 * ProtocolVersion legacy_version = 0x0303; // TLS v1.2
2192 * Random random;
2193 * opaque legacy_session_id_echo<0..32>;
2194 * CipherSuite cipher_suite;
2195 * uint8 legacy_compression_method = 0;
2196 * Extension extensions<6..2^16-1>;
2197 * } ServerHello;
2198 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002199MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002200static int ssl_tls13_write_server_hello_body(mbedtls_ssl_context *ssl,
2201 unsigned char *buf,
2202 unsigned char *end,
2203 size_t *out_len,
2204 int is_hrr)
Jerry Yu56404d72022-03-30 17:36:13 +08002205{
Jerry Yu955ddd72022-04-22 22:27:33 +08002206 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yu3bf2c642022-03-30 22:02:12 +08002207 unsigned char *p = buf;
Jerry Yud9436a12022-04-20 22:28:09 +08002208 unsigned char *p_extensions_len;
Jerry Yufbe3e642022-04-25 19:31:51 +08002209 size_t output_len;
Jerry Yu3bf2c642022-03-30 22:02:12 +08002210
2211 *out_len = 0;
Jerry Yu50e00e32022-10-31 14:45:01 +08002212 ssl->handshake->sent_extensions = MBEDTLS_SSL_EXT_MASK_NONE;
Jerry Yu3bf2c642022-03-30 22:02:12 +08002213
Jerry Yucfc04b32022-04-21 09:31:58 +08002214 /* ...
2215 * ProtocolVersion legacy_version = 0x0303; // TLS 1.2
2216 * ...
2217 * with ProtocolVersion defined as:
2218 * uint16 ProtocolVersion;
Jerry Yu3bf2c642022-03-30 22:02:12 +08002219 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002220 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 2);
2221 MBEDTLS_PUT_UINT16_BE(0x0303, p, 0);
Jerry Yu3bf2c642022-03-30 22:02:12 +08002222 p += 2;
2223
Jerry Yu1c3e6882022-04-20 21:23:40 +08002224 /* ...
2225 * Random random;
2226 * ...
Jerry Yucfc04b32022-04-21 09:31:58 +08002227 * with Random defined as:
2228 * opaque Random[MBEDTLS_SERVER_HELLO_RANDOM_LEN];
Jerry Yu1c3e6882022-04-20 21:23:40 +08002229 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002230 MBEDTLS_SSL_CHK_BUF_PTR(p, end, MBEDTLS_SERVER_HELLO_RANDOM_LEN);
2231 if (is_hrr) {
2232 memcpy(p, mbedtls_ssl_tls13_hello_retry_request_magic,
2233 MBEDTLS_SERVER_HELLO_RANDOM_LEN);
2234 } else {
2235 memcpy(p, &ssl->handshake->randbytes[MBEDTLS_CLIENT_HELLO_RANDOM_LEN],
2236 MBEDTLS_SERVER_HELLO_RANDOM_LEN);
Jerry Yu23f7a6f2022-04-23 15:16:45 +08002237 }
Gilles Peskine449bd832023-01-11 14:50:10 +01002238 MBEDTLS_SSL_DEBUG_BUF(3, "server hello, random bytes",
2239 p, MBEDTLS_SERVER_HELLO_RANDOM_LEN);
Jerry Yu3bf2c642022-03-30 22:02:12 +08002240 p += MBEDTLS_SERVER_HELLO_RANDOM_LEN;
2241
Jerry Yucfc04b32022-04-21 09:31:58 +08002242 /* ...
2243 * opaque legacy_session_id_echo<0..32>;
2244 * ...
Jerry Yu3bf2c642022-03-30 22:02:12 +08002245 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002246 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 1 + ssl->session_negotiate->id_len);
2247 *p++ = (unsigned char) ssl->session_negotiate->id_len;
2248 if (ssl->session_negotiate->id_len > 0) {
2249 memcpy(p, &ssl->session_negotiate->id[0],
2250 ssl->session_negotiate->id_len);
Jerry Yu3bf2c642022-03-30 22:02:12 +08002251 p += ssl->session_negotiate->id_len;
Jerry Yu955ddd72022-04-22 22:27:33 +08002252
Gilles Peskine449bd832023-01-11 14:50:10 +01002253 MBEDTLS_SSL_DEBUG_BUF(3, "session id", ssl->session_negotiate->id,
2254 ssl->session_negotiate->id_len);
Jerry Yu3bf2c642022-03-30 22:02:12 +08002255 }
2256
Jerry Yucfc04b32022-04-21 09:31:58 +08002257 /* ...
2258 * CipherSuite cipher_suite;
2259 * ...
2260 * with CipherSuite defined as:
2261 * uint8 CipherSuite[2];
Jerry Yu3bf2c642022-03-30 22:02:12 +08002262 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002263 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 2);
2264 MBEDTLS_PUT_UINT16_BE(ssl->session_negotiate->ciphersuite, p, 0);
Jerry Yu3bf2c642022-03-30 22:02:12 +08002265 p += 2;
Gilles Peskine449bd832023-01-11 14:50:10 +01002266 MBEDTLS_SSL_DEBUG_MSG(3,
2267 ("server hello, chosen ciphersuite: %s ( id=%d )",
2268 mbedtls_ssl_get_ciphersuite_name(
2269 ssl->session_negotiate->ciphersuite),
2270 ssl->session_negotiate->ciphersuite));
Jerry Yu3bf2c642022-03-30 22:02:12 +08002271
Jerry Yucfc04b32022-04-21 09:31:58 +08002272 /* ...
2273 * uint8 legacy_compression_method = 0;
2274 * ...
2275 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002276 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 1);
Thomas Daubney31e03a82022-07-25 15:59:25 +01002277 *p++ = MBEDTLS_SSL_COMPRESS_NULL;
Jerry Yu3bf2c642022-03-30 22:02:12 +08002278
Jerry Yucfc04b32022-04-21 09:31:58 +08002279 /* ...
2280 * Extension extensions<6..2^16-1>;
2281 * ...
2282 * struct {
2283 * ExtensionType extension_type; (2 bytes)
2284 * opaque extension_data<0..2^16-1>;
2285 * } Extension;
2286 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002287 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 2);
Jerry Yud9436a12022-04-20 22:28:09 +08002288 p_extensions_len = p;
Jerry Yu3bf2c642022-03-30 22:02:12 +08002289 p += 2;
2290
Gilles Peskine449bd832023-01-11 14:50:10 +01002291 if ((ret = ssl_tls13_write_server_hello_supported_versions_ext(
2292 ssl, p, end, &output_len)) != 0) {
Jerry Yu955ddd72022-04-22 22:27:33 +08002293 MBEDTLS_SSL_DEBUG_RET(
Gilles Peskine449bd832023-01-11 14:50:10 +01002294 1, "ssl_tls13_write_server_hello_supported_versions_ext", ret);
2295 return ret;
Jerry Yu3bf2c642022-03-30 22:02:12 +08002296 }
2297 p += output_len;
2298
Gilles Peskine449bd832023-01-11 14:50:10 +01002299 if (mbedtls_ssl_tls13_key_exchange_mode_with_ephemeral(ssl)) {
2300 if (is_hrr) {
2301 ret = ssl_tls13_write_hrr_key_share_ext(ssl, p, end, &output_len);
2302 } else {
2303 ret = ssl_tls13_write_key_share_ext(ssl, p, end, &output_len);
2304 }
2305 if (ret != 0) {
2306 return ret;
2307 }
Jerry Yu3bf2c642022-03-30 22:02:12 +08002308 p += output_len;
2309 }
Jerry Yu3bf2c642022-03-30 22:02:12 +08002310
Ronald Cron41a443a2022-10-04 16:38:25 +02002311#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01002312 if (!is_hrr && mbedtls_ssl_tls13_key_exchange_mode_with_psk(ssl)) {
2313 ret = ssl_tls13_write_server_pre_shared_key_ext(ssl, p, end, &output_len);
2314 if (ret != 0) {
2315 MBEDTLS_SSL_DEBUG_RET(1, "ssl_tls13_write_server_pre_shared_key_ext",
2316 ret);
2317 return ret;
Jerry Yu032b15ce2022-07-11 06:10:03 +00002318 }
2319 p += output_len;
2320 }
Ronald Cron41a443a2022-10-04 16:38:25 +02002321#endif
Jerry Yu032b15ce2022-07-11 06:10:03 +00002322
Gilles Peskine449bd832023-01-11 14:50:10 +01002323 MBEDTLS_PUT_UINT16_BE(p - p_extensions_len - 2, p_extensions_len, 0);
Jerry Yu3bf2c642022-03-30 22:02:12 +08002324
Gilles Peskine449bd832023-01-11 14:50:10 +01002325 MBEDTLS_SSL_DEBUG_BUF(4, "server hello extensions",
2326 p_extensions_len, p - p_extensions_len);
Jerry Yu3bf2c642022-03-30 22:02:12 +08002327
Jerry Yud9436a12022-04-20 22:28:09 +08002328 *out_len = p - buf;
Jerry Yu3bf2c642022-03-30 22:02:12 +08002329
Gilles Peskine449bd832023-01-11 14:50:10 +01002330 MBEDTLS_SSL_DEBUG_BUF(3, "server hello", buf, *out_len);
Jerry Yu3bf2c642022-03-30 22:02:12 +08002331
Jerry Yu7de2ff02022-11-08 21:43:46 +08002332 MBEDTLS_SSL_PRINT_EXTS(
Jerry Yu4b8f2f72022-10-31 13:31:22 +08002333 3, is_hrr ? MBEDTLS_SSL_TLS1_3_HS_HELLO_RETRY_REQUEST :
Gilles Peskine449bd832023-01-11 14:50:10 +01002334 MBEDTLS_SSL_HS_SERVER_HELLO,
2335 ssl->handshake->sent_extensions);
Jerry Yu4b8f2f72022-10-31 13:31:22 +08002336
Gilles Peskine449bd832023-01-11 14:50:10 +01002337 return ret;
Jerry Yu56404d72022-03-30 17:36:13 +08002338}
2339
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002340MBEDTLS_CHECK_RETURN_CRITICAL
Xiaokang Qian934ce6f2023-02-06 10:23:04 +00002341static int ssl_tls13_finalize_server_hello(mbedtls_ssl_context *ssl)
Jerry Yue110d252022-05-05 10:19:22 +08002342{
2343 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine449bd832023-01-11 14:50:10 +01002344 ret = mbedtls_ssl_tls13_compute_handshake_transform(ssl);
2345 if (ret != 0) {
2346 MBEDTLS_SSL_DEBUG_RET(1,
2347 "mbedtls_ssl_tls13_compute_handshake_transform",
2348 ret);
2349 return ret;
Jerry Yue110d252022-05-05 10:19:22 +08002350 }
2351
Gilles Peskine449bd832023-01-11 14:50:10 +01002352 return ret;
Jerry Yue110d252022-05-05 10:19:22 +08002353}
2354
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002355MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002356static int ssl_tls13_write_server_hello(mbedtls_ssl_context *ssl)
Jerry Yu5b64ae92022-03-30 17:15:02 +08002357{
Jerry Yu637a3f12022-04-20 21:37:58 +08002358 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yuf4b27e42022-03-30 17:32:21 +08002359 unsigned char *buf;
2360 size_t buf_len, msg_len;
2361
Gilles Peskine449bd832023-01-11 14:50:10 +01002362 MBEDTLS_SSL_DEBUG_MSG(2, ("=> write server hello"));
Jerry Yuf4b27e42022-03-30 17:32:21 +08002363
Gilles Peskine449bd832023-01-11 14:50:10 +01002364 MBEDTLS_SSL_PROC_CHK(ssl_tls13_prepare_server_hello(ssl));
Jerry Yuf4b27e42022-03-30 17:32:21 +08002365
Xiaokang Qian9f1747b2023-03-30 02:50:04 +00002366 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_start_handshake_msg(
2367 ssl, MBEDTLS_SSL_HS_SERVER_HELLO, &buf, &buf_len));
Jerry Yuf4b27e42022-03-30 17:32:21 +08002368
Gilles Peskine449bd832023-01-11 14:50:10 +01002369 MBEDTLS_SSL_PROC_CHK(ssl_tls13_write_server_hello_body(ssl, buf,
2370 buf + buf_len,
2371 &msg_len,
2372 0));
Jerry Yuf4b27e42022-03-30 17:32:21 +08002373
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +01002374 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_add_hs_msg_to_checksum(
Manuel Pégourié-Gonnard43cc1272023-02-06 11:48:19 +01002375 ssl, MBEDTLS_SSL_HS_SERVER_HELLO, buf, msg_len));
Jerry Yuf4b27e42022-03-30 17:32:21 +08002376
Gilles Peskine449bd832023-01-11 14:50:10 +01002377 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_finish_handshake_msg(
2378 ssl, buf_len, msg_len));
Jerry Yu637a3f12022-04-20 21:37:58 +08002379
Xiaokang Qian934ce6f2023-02-06 10:23:04 +00002380 MBEDTLS_SSL_PROC_CHK(ssl_tls13_finalize_server_hello(ssl));
Jerry Yue110d252022-05-05 10:19:22 +08002381
Gabor Mezei7b39bf12022-05-24 16:04:14 +02002382#if defined(MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE)
2383 /* The server sends a dummy change_cipher_spec record immediately
2384 * after its first handshake message. This may either be after
2385 * a ServerHello or a HelloRetryRequest.
2386 */
Gabor Mezei96ae9262022-06-28 11:45:18 +02002387 mbedtls_ssl_handshake_set_state(
Gilles Peskine449bd832023-01-11 14:50:10 +01002388 ssl, MBEDTLS_SSL_SERVER_CCS_AFTER_SERVER_HELLO);
Gabor Mezei7b39bf12022-05-24 16:04:14 +02002389#else
Gilles Peskine449bd832023-01-11 14:50:10 +01002390 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_ENCRYPTED_EXTENSIONS);
Gabor Mezei7b39bf12022-05-24 16:04:14 +02002391#endif /* MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE */
Jerry Yue110d252022-05-05 10:19:22 +08002392
Jerry Yuf4b27e42022-03-30 17:32:21 +08002393cleanup:
2394
Gilles Peskine449bd832023-01-11 14:50:10 +01002395 MBEDTLS_SSL_DEBUG_MSG(2, ("<= write server hello"));
2396 return ret;
Jerry Yu5b64ae92022-03-30 17:15:02 +08002397}
2398
Jerry Yu23d1a252022-05-12 18:08:59 +08002399
2400/*
2401 * Handler for MBEDTLS_SSL_HELLO_RETRY_REQUEST
2402 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002403MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002404static int ssl_tls13_prepare_hello_retry_request(mbedtls_ssl_context *ssl)
Jerry Yu23d1a252022-05-12 18:08:59 +08002405{
2406 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine449bd832023-01-11 14:50:10 +01002407 if (ssl->handshake->hello_retry_request_count > 0) {
2408 MBEDTLS_SSL_DEBUG_MSG(1, ("Too many HRRs"));
2409 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE,
2410 MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE);
2411 return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
Jerry Yu23d1a252022-05-12 18:08:59 +08002412 }
2413
2414 /*
2415 * Create stateless transcript hash for HRR
2416 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002417 MBEDTLS_SSL_DEBUG_MSG(4, ("Reset transcript for HRR"));
2418 ret = mbedtls_ssl_reset_transcript_for_hrr(ssl);
2419 if (ret != 0) {
2420 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_reset_transcript_for_hrr", ret);
2421 return ret;
Jerry Yu23d1a252022-05-12 18:08:59 +08002422 }
Gilles Peskine449bd832023-01-11 14:50:10 +01002423 mbedtls_ssl_session_reset_msg_layer(ssl, 0);
Jerry Yu23d1a252022-05-12 18:08:59 +08002424
Gilles Peskine449bd832023-01-11 14:50:10 +01002425 return 0;
Jerry Yu23d1a252022-05-12 18:08:59 +08002426}
2427
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002428MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002429static int ssl_tls13_write_hello_retry_request(mbedtls_ssl_context *ssl)
Jerry Yu23d1a252022-05-12 18:08:59 +08002430{
2431 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
2432 unsigned char *buf;
2433 size_t buf_len, msg_len;
2434
Gilles Peskine449bd832023-01-11 14:50:10 +01002435 MBEDTLS_SSL_DEBUG_MSG(2, ("=> write hello retry request"));
Jerry Yu23d1a252022-05-12 18:08:59 +08002436
Gilles Peskine449bd832023-01-11 14:50:10 +01002437 MBEDTLS_SSL_PROC_CHK(ssl_tls13_prepare_hello_retry_request(ssl));
Jerry Yu23d1a252022-05-12 18:08:59 +08002438
Gilles Peskine449bd832023-01-11 14:50:10 +01002439 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_start_handshake_msg(
2440 ssl, MBEDTLS_SSL_HS_SERVER_HELLO,
2441 &buf, &buf_len));
Jerry Yu23d1a252022-05-12 18:08:59 +08002442
Gilles Peskine449bd832023-01-11 14:50:10 +01002443 MBEDTLS_SSL_PROC_CHK(ssl_tls13_write_server_hello_body(ssl, buf,
2444 buf + buf_len,
2445 &msg_len,
2446 1));
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +01002447 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_add_hs_msg_to_checksum(
Manuel Pégourié-Gonnard43cc1272023-02-06 11:48:19 +01002448 ssl, MBEDTLS_SSL_HS_SERVER_HELLO, buf, msg_len));
Jerry Yu23d1a252022-05-12 18:08:59 +08002449
2450
Gilles Peskine449bd832023-01-11 14:50:10 +01002451 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_finish_handshake_msg(ssl, buf_len,
2452 msg_len));
Jerry Yu23d1a252022-05-12 18:08:59 +08002453
2454 ssl->handshake->hello_retry_request_count++;
2455
Gabor Mezei7b39bf12022-05-24 16:04:14 +02002456#if defined(MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE)
2457 /* The server sends a dummy change_cipher_spec record immediately
2458 * after its first handshake message. This may either be after
2459 * a ServerHello or a HelloRetryRequest.
2460 */
Gabor Mezei96ae9262022-06-28 11:45:18 +02002461 mbedtls_ssl_handshake_set_state(
Gilles Peskine449bd832023-01-11 14:50:10 +01002462 ssl, MBEDTLS_SSL_SERVER_CCS_AFTER_HELLO_RETRY_REQUEST);
Gabor Mezei7b39bf12022-05-24 16:04:14 +02002463#else
Gilles Peskine449bd832023-01-11 14:50:10 +01002464 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_CLIENT_HELLO);
Gabor Mezei7b39bf12022-05-24 16:04:14 +02002465#endif /* MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE */
Jerry Yu23d1a252022-05-12 18:08:59 +08002466
2467cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +01002468 MBEDTLS_SSL_DEBUG_MSG(2, ("<= write hello retry request"));
2469 return ret;
Jerry Yu23d1a252022-05-12 18:08:59 +08002470}
2471
Jerry Yu5b64ae92022-03-30 17:15:02 +08002472/*
Jerry Yu4d3841a2022-04-16 12:37:19 +08002473 * Handler for MBEDTLS_SSL_ENCRYPTED_EXTENSIONS
2474 */
Jerry Yu4d3841a2022-04-16 12:37:19 +08002475
2476/*
2477 * struct {
2478 * Extension extensions<0..2 ^ 16 - 1>;
2479 * } EncryptedExtensions;
2480 *
2481 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002482MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002483static int ssl_tls13_write_encrypted_extensions_body(mbedtls_ssl_context *ssl,
2484 unsigned char *buf,
2485 unsigned char *end,
2486 size_t *out_len)
Jerry Yu4d3841a2022-04-16 12:37:19 +08002487{
XiaokangQianacb39922022-06-17 10:18:48 +00002488 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yu4d3841a2022-04-16 12:37:19 +08002489 unsigned char *p = buf;
2490 size_t extensions_len = 0;
Jerry Yu8937eb42022-05-03 12:12:14 +08002491 unsigned char *p_extensions_len;
XiaokangQianacb39922022-06-17 10:18:48 +00002492 size_t output_len;
Jerry Yu9da5e5a2022-05-03 15:46:09 +08002493
Jerry Yu4d3841a2022-04-16 12:37:19 +08002494 *out_len = 0;
2495
Gilles Peskine449bd832023-01-11 14:50:10 +01002496 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 2);
Jerry Yu8937eb42022-05-03 12:12:14 +08002497 p_extensions_len = p;
Jerry Yu4d3841a2022-04-16 12:37:19 +08002498 p += 2;
2499
Jerry Yu8937eb42022-05-03 12:12:14 +08002500 ((void) ssl);
XiaokangQianacb39922022-06-17 10:18:48 +00002501 ((void) ret);
2502 ((void) output_len);
2503
2504#if defined(MBEDTLS_SSL_ALPN)
Gilles Peskine449bd832023-01-11 14:50:10 +01002505 ret = mbedtls_ssl_write_alpn_ext(ssl, p, end, &output_len);
2506 if (ret != 0) {
2507 return ret;
2508 }
XiaokangQian95d5f542022-06-24 02:29:26 +00002509 p += output_len;
XiaokangQianacb39922022-06-17 10:18:48 +00002510#endif /* MBEDTLS_SSL_ALPN */
Jerry Yu4d3841a2022-04-16 12:37:19 +08002511
Jerry Yu71c14f12022-12-12 11:10:35 +08002512#if defined(MBEDTLS_SSL_EARLY_DATA)
2513 if (ssl->early_data_status == MBEDTLS_SSL_EARLY_DATA_STATUS_ACCEPTED) {
2514 ret = mbedtls_ssl_tls13_write_early_data_ext(ssl, p, end, &output_len);
2515 if (ret != 0) {
2516 return ret;
2517 }
2518 p += output_len;
2519 }
2520#endif /* MBEDTLS_SSL_EARLY_DATA */
2521
Gilles Peskine449bd832023-01-11 14:50:10 +01002522 extensions_len = (p - p_extensions_len) - 2;
2523 MBEDTLS_PUT_UINT16_BE(extensions_len, p_extensions_len, 0);
Jerry Yu8937eb42022-05-03 12:12:14 +08002524
2525 *out_len = p - buf;
Jerry Yu4d3841a2022-04-16 12:37:19 +08002526
Gilles Peskine449bd832023-01-11 14:50:10 +01002527 MBEDTLS_SSL_DEBUG_BUF(4, "encrypted extensions", buf, *out_len);
Jerry Yu4d3841a2022-04-16 12:37:19 +08002528
Jerry Yu7de2ff02022-11-08 21:43:46 +08002529 MBEDTLS_SSL_PRINT_EXTS(
Gilles Peskine449bd832023-01-11 14:50:10 +01002530 3, MBEDTLS_SSL_HS_ENCRYPTED_EXTENSIONS, ssl->handshake->sent_extensions);
Jerry Yu4b8f2f72022-10-31 13:31:22 +08002531
Gilles Peskine449bd832023-01-11 14:50:10 +01002532 return 0;
Jerry Yu4d3841a2022-04-16 12:37:19 +08002533}
2534
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002535MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002536static int ssl_tls13_write_encrypted_extensions(mbedtls_ssl_context *ssl)
Jerry Yu4d3841a2022-04-16 12:37:19 +08002537{
2538 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
2539 unsigned char *buf;
Jerry Yu39730a72022-05-03 12:14:04 +08002540 size_t buf_len, msg_len;
Jerry Yu4d3841a2022-04-16 12:37:19 +08002541
Gilles Peskine449bd832023-01-11 14:50:10 +01002542 mbedtls_ssl_set_outbound_transform(ssl,
2543 ssl->handshake->transform_handshake);
Gabor Mezei54719122022-06-28 11:34:56 +02002544 MBEDTLS_SSL_DEBUG_MSG(
Gilles Peskine449bd832023-01-11 14:50:10 +01002545 3, ("switching to handshake transform for outbound data"));
Gabor Mezei54719122022-06-28 11:34:56 +02002546
Gilles Peskine449bd832023-01-11 14:50:10 +01002547 MBEDTLS_SSL_DEBUG_MSG(2, ("=> write encrypted extensions"));
Jerry Yu4d3841a2022-04-16 12:37:19 +08002548
Xiaokang Qian9f1747b2023-03-30 02:50:04 +00002549 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_start_handshake_msg(
2550 ssl, MBEDTLS_SSL_HS_ENCRYPTED_EXTENSIONS,
2551 &buf, &buf_len));
Jerry Yu4d3841a2022-04-16 12:37:19 +08002552
Gilles Peskine449bd832023-01-11 14:50:10 +01002553 MBEDTLS_SSL_PROC_CHK(ssl_tls13_write_encrypted_extensions_body(
2554 ssl, buf, buf + buf_len, &msg_len));
Jerry Yu4d3841a2022-04-16 12:37:19 +08002555
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +01002556 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_add_hs_msg_to_checksum(
Xiaokang Qian9f1747b2023-03-30 02:50:04 +00002557 ssl, MBEDTLS_SSL_HS_ENCRYPTED_EXTENSIONS,
2558 buf, msg_len));
Jerry Yu4d3841a2022-04-16 12:37:19 +08002559
Gilles Peskine449bd832023-01-11 14:50:10 +01002560 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_finish_handshake_msg(
2561 ssl, buf_len, msg_len));
Jerry Yu4d3841a2022-04-16 12:37:19 +08002562
Ronald Cron928cbd32022-10-04 16:14:26 +02002563#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01002564 if (mbedtls_ssl_tls13_key_exchange_mode_with_psk(ssl)) {
2565 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_SERVER_FINISHED);
2566 } else {
2567 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_CERTIFICATE_REQUEST);
2568 }
Jerry Yu8937eb42022-05-03 12:12:14 +08002569#else
Gilles Peskine449bd832023-01-11 14:50:10 +01002570 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_SERVER_FINISHED);
Jerry Yu8937eb42022-05-03 12:12:14 +08002571#endif
2572
Jerry Yu4d3841a2022-04-16 12:37:19 +08002573cleanup:
2574
Gilles Peskine449bd832023-01-11 14:50:10 +01002575 MBEDTLS_SSL_DEBUG_MSG(2, ("<= write encrypted extensions"));
2576 return ret;
Jerry Yu4d3841a2022-04-16 12:37:19 +08002577}
2578
Ronald Cron928cbd32022-10-04 16:14:26 +02002579#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED)
XiaokangQiana987e1d2022-05-07 01:25:58 +00002580#define SSL_CERTIFICATE_REQUEST_SEND_REQUEST 0
2581#define SSL_CERTIFICATE_REQUEST_SKIP 1
2582/* Coordination:
2583 * Check whether a CertificateRequest message should be written.
2584 * Returns a negative code on failure, or
2585 * - SSL_CERTIFICATE_REQUEST_SEND_REQUEST
2586 * - SSL_CERTIFICATE_REQUEST_SKIP
2587 * indicating if the writing of the CertificateRequest
2588 * should be skipped or not.
2589 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002590MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002591static int ssl_tls13_certificate_request_coordinate(mbedtls_ssl_context *ssl)
XiaokangQiana987e1d2022-05-07 01:25:58 +00002592{
2593 int authmode;
2594
XiaokangQian40a35232022-05-07 09:02:40 +00002595#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Gilles Peskine449bd832023-01-11 14:50:10 +01002596 if (ssl->handshake->sni_authmode != MBEDTLS_SSL_VERIFY_UNSET) {
XiaokangQian40a35232022-05-07 09:02:40 +00002597 authmode = ssl->handshake->sni_authmode;
Gilles Peskine449bd832023-01-11 14:50:10 +01002598 } else
XiaokangQian40a35232022-05-07 09:02:40 +00002599#endif
XiaokangQiana987e1d2022-05-07 01:25:58 +00002600 authmode = ssl->conf->authmode;
2601
Gilles Peskine449bd832023-01-11 14:50:10 +01002602 if (authmode == MBEDTLS_SSL_VERIFY_NONE) {
Ronald Croneac00ad2022-09-13 10:16:31 +02002603 ssl->session_negotiate->verify_result = MBEDTLS_X509_BADCERT_SKIP_VERIFY;
Gilles Peskine449bd832023-01-11 14:50:10 +01002604 return SSL_CERTIFICATE_REQUEST_SKIP;
Ronald Croneac00ad2022-09-13 10:16:31 +02002605 }
XiaokangQiana987e1d2022-05-07 01:25:58 +00002606
XiaokangQianc3017f62022-05-13 05:55:41 +00002607 ssl->handshake->certificate_request_sent = 1;
XiaokangQian189ded22022-05-10 08:12:17 +00002608
Gilles Peskine449bd832023-01-11 14:50:10 +01002609 return SSL_CERTIFICATE_REQUEST_SEND_REQUEST;
XiaokangQiana987e1d2022-05-07 01:25:58 +00002610}
2611
XiaokangQiancec9ae62022-05-06 07:28:50 +00002612/*
2613 * struct {
2614 * opaque certificate_request_context<0..2^8-1>;
2615 * Extension extensions<2..2^16-1>;
2616 * } CertificateRequest;
2617 *
2618 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002619MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002620static int ssl_tls13_write_certificate_request_body(mbedtls_ssl_context *ssl,
2621 unsigned char *buf,
2622 const unsigned char *end,
2623 size_t *out_len)
XiaokangQiancec9ae62022-05-06 07:28:50 +00002624{
2625 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
2626 unsigned char *p = buf;
XiaokangQianec6efb92022-05-06 09:53:10 +00002627 size_t output_len = 0;
XiaokangQiancec9ae62022-05-06 07:28:50 +00002628 unsigned char *p_extensions_len;
2629
2630 *out_len = 0;
2631
2632 /* Check if we have enough space:
2633 * - certificate_request_context (1 byte)
2634 * - extensions length (2 bytes)
2635 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002636 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 3);
XiaokangQiancec9ae62022-05-06 07:28:50 +00002637
2638 /*
2639 * Write certificate_request_context
2640 */
2641 /*
2642 * We use a zero length context for the normal handshake
2643 * messages. For post-authentication handshake messages
2644 * this request context would be set to a non-zero value.
2645 */
2646 *p++ = 0x0;
2647
2648 /*
2649 * Write extensions
2650 */
2651 /* The extensions must contain the signature_algorithms. */
2652 p_extensions_len = p;
2653 p += 2;
Gilles Peskine449bd832023-01-11 14:50:10 +01002654 ret = mbedtls_ssl_write_sig_alg_ext(ssl, p, end, &output_len);
2655 if (ret != 0) {
2656 return ret;
2657 }
XiaokangQiancec9ae62022-05-06 07:28:50 +00002658
XiaokangQianec6efb92022-05-06 09:53:10 +00002659 p += output_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01002660 MBEDTLS_PUT_UINT16_BE(p - p_extensions_len - 2, p_extensions_len, 0);
XiaokangQiancec9ae62022-05-06 07:28:50 +00002661
2662 *out_len = p - buf;
2663
Jerry Yu7de2ff02022-11-08 21:43:46 +08002664 MBEDTLS_SSL_PRINT_EXTS(
Gilles Peskine449bd832023-01-11 14:50:10 +01002665 3, MBEDTLS_SSL_HS_CERTIFICATE_REQUEST, ssl->handshake->sent_extensions);
Jerry Yu4b8f2f72022-10-31 13:31:22 +08002666
Gilles Peskine449bd832023-01-11 14:50:10 +01002667 return 0;
XiaokangQiancec9ae62022-05-06 07:28:50 +00002668}
2669
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002670MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002671static int ssl_tls13_write_certificate_request(mbedtls_ssl_context *ssl)
XiaokangQiancec9ae62022-05-06 07:28:50 +00002672{
2673 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
2674
Gilles Peskine449bd832023-01-11 14:50:10 +01002675 MBEDTLS_SSL_DEBUG_MSG(2, ("=> write certificate request"));
XiaokangQiancec9ae62022-05-06 07:28:50 +00002676
Gilles Peskine449bd832023-01-11 14:50:10 +01002677 MBEDTLS_SSL_PROC_CHK_NEG(ssl_tls13_certificate_request_coordinate(ssl));
XiaokangQiancec9ae62022-05-06 07:28:50 +00002678
Gilles Peskine449bd832023-01-11 14:50:10 +01002679 if (ret == SSL_CERTIFICATE_REQUEST_SEND_REQUEST) {
XiaokangQiancec9ae62022-05-06 07:28:50 +00002680 unsigned char *buf;
2681 size_t buf_len, msg_len;
2682
Xiaokang Qian9f1747b2023-03-30 02:50:04 +00002683 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_start_handshake_msg(
2684 ssl, MBEDTLS_SSL_HS_CERTIFICATE_REQUEST,
2685 &buf, &buf_len));
XiaokangQiancec9ae62022-05-06 07:28:50 +00002686
Gilles Peskine449bd832023-01-11 14:50:10 +01002687 MBEDTLS_SSL_PROC_CHK(ssl_tls13_write_certificate_request_body(
2688 ssl, buf, buf + buf_len, &msg_len));
XiaokangQiancec9ae62022-05-06 07:28:50 +00002689
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +01002690 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_add_hs_msg_to_checksum(
Xiaokang Qian9f1747b2023-03-30 02:50:04 +00002691 ssl, MBEDTLS_SSL_HS_CERTIFICATE_REQUEST,
2692 buf, msg_len));
XiaokangQiancec9ae62022-05-06 07:28:50 +00002693
Gilles Peskine449bd832023-01-11 14:50:10 +01002694 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_finish_handshake_msg(
2695 ssl, buf_len, msg_len));
2696 } else if (ret == SSL_CERTIFICATE_REQUEST_SKIP) {
2697 MBEDTLS_SSL_DEBUG_MSG(2, ("<= skip write certificate request"));
XiaokangQiancec9ae62022-05-06 07:28:50 +00002698 ret = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01002699 } else {
2700 MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
XiaokangQiancec9ae62022-05-06 07:28:50 +00002701 ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
2702 goto cleanup;
2703 }
2704
Gilles Peskine449bd832023-01-11 14:50:10 +01002705 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_SERVER_CERTIFICATE);
XiaokangQiancec9ae62022-05-06 07:28:50 +00002706cleanup:
2707
Gilles Peskine449bd832023-01-11 14:50:10 +01002708 MBEDTLS_SSL_DEBUG_MSG(2, ("<= write certificate request"));
2709 return ret;
XiaokangQiancec9ae62022-05-06 07:28:50 +00002710}
XiaokangQiancec9ae62022-05-06 07:28:50 +00002711
Jerry Yu4d3841a2022-04-16 12:37:19 +08002712/*
Jerry Yuc6e6dbf2022-04-16 19:42:57 +08002713 * Handler for MBEDTLS_SSL_SERVER_CERTIFICATE
Jerry Yu83da34e2022-04-16 13:59:52 +08002714 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002715MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002716static int ssl_tls13_write_server_certificate(mbedtls_ssl_context *ssl)
Jerry Yu83da34e2022-04-16 13:59:52 +08002717{
Jerry Yu5a26f302022-05-10 20:46:40 +08002718 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
XiaokangQianfb665a82022-06-15 03:57:21 +00002719
2720#if defined(MBEDTLS_X509_CRT_PARSE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01002721 if ((ssl_tls13_pick_key_cert(ssl) != 0) ||
2722 mbedtls_ssl_own_cert(ssl) == NULL) {
2723 MBEDTLS_SSL_DEBUG_MSG(2, ("No certificate available."));
2724 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE,
2725 MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE);
2726 return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
Jerry Yu5a26f302022-05-10 20:46:40 +08002727 }
XiaokangQianfb665a82022-06-15 03:57:21 +00002728#endif /* MBEDTLS_X509_CRT_PARSE_C */
Jerry Yu5a26f302022-05-10 20:46:40 +08002729
Gilles Peskine449bd832023-01-11 14:50:10 +01002730 ret = mbedtls_ssl_tls13_write_certificate(ssl);
2731 if (ret != 0) {
2732 return ret;
2733 }
2734 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_CERTIFICATE_VERIFY);
2735 return 0;
Jerry Yu83da34e2022-04-16 13:59:52 +08002736}
2737
2738/*
Jerry Yuc6e6dbf2022-04-16 19:42:57 +08002739 * Handler for MBEDTLS_SSL_CERTIFICATE_VERIFY
Jerry Yu83da34e2022-04-16 13:59:52 +08002740 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002741MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002742static int ssl_tls13_write_certificate_verify(mbedtls_ssl_context *ssl)
Jerry Yu83da34e2022-04-16 13:59:52 +08002743{
Gilles Peskine449bd832023-01-11 14:50:10 +01002744 int ret = mbedtls_ssl_tls13_write_certificate_verify(ssl);
2745 if (ret != 0) {
2746 return ret;
2747 }
2748 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_SERVER_FINISHED);
2749 return 0;
Jerry Yu83da34e2022-04-16 13:59:52 +08002750}
Ronald Cron928cbd32022-10-04 16:14:26 +02002751#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED */
Jerry Yu83da34e2022-04-16 13:59:52 +08002752
2753/*
Jerry Yud6e253d2022-05-18 13:59:24 +08002754 * Handler for MBEDTLS_SSL_SERVER_FINISHED
Jerry Yu69dd8d42022-04-16 12:51:26 +08002755 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002756MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002757static int ssl_tls13_write_server_finished(mbedtls_ssl_context *ssl)
Jerry Yu69dd8d42022-04-16 12:51:26 +08002758{
Jerry Yud6e253d2022-05-18 13:59:24 +08002759 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yu27bdc7c2022-04-16 13:33:27 +08002760
Gilles Peskine449bd832023-01-11 14:50:10 +01002761 ret = mbedtls_ssl_tls13_write_finished_message(ssl);
2762 if (ret != 0) {
2763 return ret;
2764 }
Jerry Yu27bdc7c2022-04-16 13:33:27 +08002765
Gilles Peskine449bd832023-01-11 14:50:10 +01002766 ret = mbedtls_ssl_tls13_compute_application_transform(ssl);
2767 if (ret != 0) {
Jerry Yue3d67cb2022-05-19 15:33:10 +08002768 MBEDTLS_SSL_PEND_FATAL_ALERT(
Gilles Peskine449bd832023-01-11 14:50:10 +01002769 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE,
2770 MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE);
2771 return ret;
Jerry Yue3d67cb2022-05-19 15:33:10 +08002772 }
XiaokangQianc3017f62022-05-13 05:55:41 +00002773
Gilles Peskine449bd832023-01-11 14:50:10 +01002774 MBEDTLS_SSL_DEBUG_MSG(1, ("Switch to handshake keys for inbound traffic"));
2775 mbedtls_ssl_set_inbound_transform(ssl, ssl->handshake->transform_handshake);
XiaokangQian189ded22022-05-10 08:12:17 +00002776
Gilles Peskine449bd832023-01-11 14:50:10 +01002777 if (ssl->handshake->certificate_request_sent) {
2778 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_CLIENT_CERTIFICATE);
2779 } else {
2780 MBEDTLS_SSL_DEBUG_MSG(2, ("skip parse certificate"));
2781 MBEDTLS_SSL_DEBUG_MSG(2, ("skip parse certificate verify"));
2782 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_CLIENT_FINISHED);
Ronald Cron19385882022-06-15 16:26:13 +02002783 }
Ronald Cron63dc4632022-05-31 14:41:53 +02002784
Gilles Peskine449bd832023-01-11 14:50:10 +01002785 return 0;
Jerry Yu69dd8d42022-04-16 12:51:26 +08002786}
2787
2788/*
Jerry Yud6e253d2022-05-18 13:59:24 +08002789 * Handler for MBEDTLS_SSL_CLIENT_FINISHED
Jerry Yu69dd8d42022-04-16 12:51:26 +08002790 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002791MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002792static int ssl_tls13_process_client_finished(mbedtls_ssl_context *ssl)
Jerry Yu69dd8d42022-04-16 12:51:26 +08002793{
Jerry Yud6e253d2022-05-18 13:59:24 +08002794 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
2795
Gilles Peskine449bd832023-01-11 14:50:10 +01002796 ret = mbedtls_ssl_tls13_process_finished_message(ssl);
2797 if (ret != 0) {
2798 return ret;
Jerry Yue3d67cb2022-05-19 15:33:10 +08002799 }
2800
Gilles Peskine449bd832023-01-11 14:50:10 +01002801 ret = mbedtls_ssl_tls13_compute_resumption_master_secret(ssl);
2802 if (ret != 0) {
Xiaokang Qian9f1747b2023-03-30 02:50:04 +00002803 MBEDTLS_SSL_DEBUG_RET(
2804 1, "mbedtls_ssl_tls13_compute_resumption_master_secret", ret);
Gilles Peskine449bd832023-01-11 14:50:10 +01002805 }
2806
2807 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_HANDSHAKE_WRAPUP);
2808 return 0;
Jerry Yu69dd8d42022-04-16 12:51:26 +08002809}
2810
2811/*
Jerry Yud6e253d2022-05-18 13:59:24 +08002812 * Handler for MBEDTLS_SSL_HANDSHAKE_WRAPUP
Jerry Yu69dd8d42022-04-16 12:51:26 +08002813 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002814MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002815static int ssl_tls13_handshake_wrapup(mbedtls_ssl_context *ssl)
Jerry Yu69dd8d42022-04-16 12:51:26 +08002816{
Gilles Peskine449bd832023-01-11 14:50:10 +01002817 MBEDTLS_SSL_DEBUG_MSG(2, ("handshake: done"));
Jerry Yu03ed50b2022-04-16 17:13:30 +08002818
Gilles Peskine449bd832023-01-11 14:50:10 +01002819 mbedtls_ssl_tls13_handshake_wrapup(ssl);
Jerry Yue67bef42022-07-07 07:29:42 +00002820
Pengyu Lv3643fdb2023-01-12 16:46:28 +08002821#if defined(MBEDTLS_SSL_SESSION_TICKETS) && \
2822 defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED)
Pengyu Lva1aa31b2022-12-13 13:49:59 +08002823/* TODO: Remove the check of SOME_PSK_ENABLED since SESSION_TICKETS requires
2824 * SOME_PSK_ENABLED to be enabled. Here is just to make CI happy. It is
2825 * expected to be resolved with issue#6395.
2826 */
Pengyu Lvc7af2c42022-12-01 16:33:00 +08002827 /* Sent NewSessionTicket message only when client supports PSK */
Pengyu Lv3643fdb2023-01-12 16:46:28 +08002828 if (mbedtls_ssl_tls13_some_psk_enabled(ssl)) {
Xiaokang Qian9f1747b2023-03-30 02:50:04 +00002829 mbedtls_ssl_handshake_set_state(
2830 ssl, MBEDTLS_SSL_TLS1_3_NEW_SESSION_TICKET);
Pengyu Lvc7af2c42022-12-01 16:33:00 +08002831 } else
Jerry Yufca4d572022-07-21 10:37:48 +08002832#endif
Pengyu Lv3643fdb2023-01-12 16:46:28 +08002833 {
2834 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_HANDSHAKE_OVER);
2835 }
Gilles Peskine449bd832023-01-11 14:50:10 +01002836 return 0;
Jerry Yu69dd8d42022-04-16 12:51:26 +08002837}
2838
2839/*
Jerry Yua8d3c502022-10-30 14:51:23 +08002840 * Handler for MBEDTLS_SSL_TLS1_3_NEW_SESSION_TICKET
Jerry Yue67bef42022-07-07 07:29:42 +00002841 */
2842#define SSL_NEW_SESSION_TICKET_SKIP 0
2843#define SSL_NEW_SESSION_TICKET_WRITE 1
2844MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002845static int ssl_tls13_write_new_session_ticket_coordinate(mbedtls_ssl_context *ssl)
Jerry Yue67bef42022-07-07 07:29:42 +00002846{
2847 /* Check whether the use of session tickets is enabled */
Gilles Peskine449bd832023-01-11 14:50:10 +01002848 if (ssl->conf->f_ticket_write == NULL) {
2849 MBEDTLS_SSL_DEBUG_MSG(2, ("NewSessionTicket: disabled,"
2850 " callback is not set"));
2851 return SSL_NEW_SESSION_TICKET_SKIP;
Jerry Yud0766ec2022-09-22 10:46:57 +08002852 }
Gilles Peskine449bd832023-01-11 14:50:10 +01002853 if (ssl->conf->new_session_tickets_count == 0) {
2854 MBEDTLS_SSL_DEBUG_MSG(2, ("NewSessionTicket: disabled,"
2855 " configured count is zero"));
2856 return SSL_NEW_SESSION_TICKET_SKIP;
Jerry Yud0766ec2022-09-22 10:46:57 +08002857 }
2858
Gilles Peskine449bd832023-01-11 14:50:10 +01002859 if (ssl->handshake->new_session_tickets_count == 0) {
2860 MBEDTLS_SSL_DEBUG_MSG(2, ("NewSessionTicket: all tickets have "
2861 "been sent."));
2862 return SSL_NEW_SESSION_TICKET_SKIP;
Jerry Yue67bef42022-07-07 07:29:42 +00002863 }
2864
Gilles Peskine449bd832023-01-11 14:50:10 +01002865 return SSL_NEW_SESSION_TICKET_WRITE;
Jerry Yue67bef42022-07-07 07:29:42 +00002866}
2867
2868#if defined(MBEDTLS_SSL_SESSION_TICKETS)
2869MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002870static int ssl_tls13_prepare_new_session_ticket(mbedtls_ssl_context *ssl,
2871 unsigned char *ticket_nonce,
2872 size_t ticket_nonce_size)
Jerry Yue67bef42022-07-07 07:29:42 +00002873{
2874 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
2875 mbedtls_ssl_session *session = ssl->session;
2876 mbedtls_ssl_ciphersuite_t *ciphersuite_info;
2877 psa_algorithm_t psa_hash_alg;
2878 int hash_length;
2879
Gilles Peskine449bd832023-01-11 14:50:10 +01002880 MBEDTLS_SSL_DEBUG_MSG(2, ("=> prepare NewSessionTicket msg"));
Jerry Yue67bef42022-07-07 07:29:42 +00002881
2882#if defined(MBEDTLS_HAVE_TIME)
Jerry Yuec6d0782023-10-31 14:42:20 +08002883 session->ticket_creation = mbedtls_ms_time();
Jerry Yue67bef42022-07-07 07:29:42 +00002884#endif
2885
Pengyu Lv9f926952022-11-17 15:22:33 +08002886 /* Set ticket_flags depends on the advertised psk key exchange mode */
Pengyu Lv80270b22023-01-12 11:54:04 +08002887 mbedtls_ssl_session_clear_ticket_flags(
Pengyu Lv9eacb442022-12-09 14:39:19 +08002888 session, MBEDTLS_SSL_TLS1_3_TICKET_FLAGS_MASK);
Pengyu Lve6487fe2022-12-06 09:30:29 +08002889#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED)
Pengyu Lv80270b22023-01-12 11:54:04 +08002890 mbedtls_ssl_session_set_ticket_flags(
Pengyu Lv9eacb442022-12-09 14:39:19 +08002891 session, ssl->handshake->tls13_kex_modes);
Pengyu Lve6487fe2022-12-06 09:30:29 +08002892#endif
Pengyu Lv4938a562023-01-16 11:28:49 +08002893 MBEDTLS_SSL_PRINT_TICKET_FLAGS(4, session->ticket_flags);
Pengyu Lv9f926952022-11-17 15:22:33 +08002894
Jerry Yue67bef42022-07-07 07:29:42 +00002895 /* Generate ticket_age_add */
Gilles Peskine449bd832023-01-11 14:50:10 +01002896 if ((ret = ssl->conf->f_rng(ssl->conf->p_rng,
2897 (unsigned char *) &session->ticket_age_add,
2898 sizeof(session->ticket_age_add)) != 0)) {
2899 MBEDTLS_SSL_DEBUG_RET(1, "generate_ticket_age_add", ret);
2900 return ret;
Jerry Yue67bef42022-07-07 07:29:42 +00002901 }
Gilles Peskine449bd832023-01-11 14:50:10 +01002902 MBEDTLS_SSL_DEBUG_MSG(3, ("ticket_age_add: %u",
2903 (unsigned int) session->ticket_age_add));
Jerry Yue67bef42022-07-07 07:29:42 +00002904
2905 /* Generate ticket_nonce */
Gilles Peskine449bd832023-01-11 14:50:10 +01002906 ret = ssl->conf->f_rng(ssl->conf->p_rng, ticket_nonce, ticket_nonce_size);
2907 if (ret != 0) {
2908 MBEDTLS_SSL_DEBUG_RET(1, "generate_ticket_nonce", ret);
2909 return ret;
Jerry Yue67bef42022-07-07 07:29:42 +00002910 }
Gilles Peskine449bd832023-01-11 14:50:10 +01002911 MBEDTLS_SSL_DEBUG_BUF(3, "ticket_nonce:",
2912 ticket_nonce, ticket_nonce_size);
Jerry Yue67bef42022-07-07 07:29:42 +00002913
2914 ciphersuite_info =
Gilles Peskine449bd832023-01-11 14:50:10 +01002915 (mbedtls_ssl_ciphersuite_t *) ssl->handshake->ciphersuite_info;
Dave Rodgman2eab4622023-10-05 13:30:37 +01002916 psa_hash_alg = mbedtls_md_psa_alg_from_type((mbedtls_md_type_t) ciphersuite_info->mac);
Gilles Peskine449bd832023-01-11 14:50:10 +01002917 hash_length = PSA_HASH_LENGTH(psa_hash_alg);
2918 if (hash_length == -1 ||
2919 (size_t) hash_length > sizeof(session->resumption_key)) {
2920 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Jerry Yufca4d572022-07-21 10:37:48 +08002921 }
2922
Jerry Yue67bef42022-07-07 07:29:42 +00002923 /* In this code the psk key length equals the length of the hash */
2924 session->resumption_key_len = hash_length;
2925 session->ciphersuite = ciphersuite_info->id;
2926
2927 /* Compute resumption key
2928 *
2929 * HKDF-Expand-Label( resumption_master_secret,
2930 * "resumption", ticket_nonce, Hash.length )
2931 */
2932 ret = mbedtls_ssl_tls13_hkdf_expand_label(
Gilles Peskine449bd832023-01-11 14:50:10 +01002933 psa_hash_alg,
2934 session->app_secrets.resumption_master_secret,
2935 hash_length,
2936 MBEDTLS_SSL_TLS1_3_LBL_WITH_LEN(resumption),
2937 ticket_nonce,
2938 ticket_nonce_size,
2939 session->resumption_key,
2940 hash_length);
Jerry Yue67bef42022-07-07 07:29:42 +00002941
Gilles Peskine449bd832023-01-11 14:50:10 +01002942 if (ret != 0) {
2943 MBEDTLS_SSL_DEBUG_RET(2,
2944 "Creating the ticket-resumed PSK failed",
2945 ret);
2946 return ret;
Jerry Yue67bef42022-07-07 07:29:42 +00002947 }
Gilles Peskine449bd832023-01-11 14:50:10 +01002948 MBEDTLS_SSL_DEBUG_BUF(3, "Ticket-resumed PSK",
2949 session->resumption_key,
2950 session->resumption_key_len);
Jerry Yue67bef42022-07-07 07:29:42 +00002951
Gilles Peskine449bd832023-01-11 14:50:10 +01002952 MBEDTLS_SSL_DEBUG_BUF(3, "resumption_master_secret",
2953 session->app_secrets.resumption_master_secret,
2954 hash_length);
Jerry Yue67bef42022-07-07 07:29:42 +00002955
Gilles Peskine449bd832023-01-11 14:50:10 +01002956 return 0;
Jerry Yue67bef42022-07-07 07:29:42 +00002957}
2958
2959/* This function creates a NewSessionTicket message in the following format:
2960 *
2961 * struct {
2962 * uint32 ticket_lifetime;
2963 * uint32 ticket_age_add;
2964 * opaque ticket_nonce<0..255>;
2965 * opaque ticket<1..2^16-1>;
2966 * Extension extensions<0..2^16-2>;
2967 * } NewSessionTicket;
2968 *
2969 * The ticket inside the NewSessionTicket message is an encrypted container
2970 * carrying the necessary information so that the server is later able to
2971 * re-start the communication.
2972 *
2973 * The following fields are placed inside the ticket by the
2974 * f_ticket_write() function:
2975 *
2976 * - creation time (start)
2977 * - flags (flags)
2978 * - age add (ticket_age_add)
2979 * - key (key)
2980 * - key length (key_len)
2981 * - ciphersuite (ciphersuite)
2982 */
2983MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002984static int ssl_tls13_write_new_session_ticket_body(mbedtls_ssl_context *ssl,
2985 unsigned char *buf,
2986 unsigned char *end,
2987 size_t *out_len,
2988 unsigned char *ticket_nonce,
2989 size_t ticket_nonce_size)
Jerry Yue67bef42022-07-07 07:29:42 +00002990{
2991 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
2992 unsigned char *p = buf;
2993 mbedtls_ssl_session *session = ssl->session;
2994 size_t ticket_len;
2995 uint32_t ticket_lifetime;
2996
2997 *out_len = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01002998 MBEDTLS_SSL_DEBUG_MSG(2, ("=> write NewSessionTicket msg"));
Jerry Yue67bef42022-07-07 07:29:42 +00002999
3000 /*
3001 * ticket_lifetime 4 bytes
3002 * ticket_age_add 4 bytes
3003 * ticket_nonce 1 + ticket_nonce_size bytes
3004 * ticket >=2 bytes
3005 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003006 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 4 + 4 + 1 + ticket_nonce_size + 2);
Jerry Yue67bef42022-07-07 07:29:42 +00003007
3008 /* Generate ticket and ticket_lifetime */
Gilles Peskine449bd832023-01-11 14:50:10 +01003009 ret = ssl->conf->f_ticket_write(ssl->conf->p_ticket,
3010 session,
3011 p + 9 + ticket_nonce_size + 2,
3012 end,
3013 &ticket_len,
3014 &ticket_lifetime);
3015 if (ret != 0) {
3016 MBEDTLS_SSL_DEBUG_RET(1, "write_ticket", ret);
3017 return ret;
Jerry Yue67bef42022-07-07 07:29:42 +00003018 }
3019 /* RFC 8446 4.6.1
3020 * ticket_lifetime: Indicates the lifetime in seconds as a 32-bit
3021 * unsigned integer in network byte order from the time of ticket
3022 * issuance. Servers MUST NOT use any value greater than
3023 * 604800 seconds (7 days). The value of zero indicates that the
3024 * ticket should be discarded immediately. Clients MUST NOT cache
3025 * tickets for longer than 7 days, regardless of the ticket_lifetime,
3026 * and MAY delete tickets earlier based on local policy. A server
3027 * MAY treat a ticket as valid for a shorter period of time than what
3028 * is stated in the ticket_lifetime.
3029 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003030 if (ticket_lifetime > 604800) {
Xiaokang Qian28af5012022-10-13 08:18:19 +00003031 ticket_lifetime = 604800;
Gilles Peskine449bd832023-01-11 14:50:10 +01003032 }
3033 MBEDTLS_PUT_UINT32_BE(ticket_lifetime, p, 0);
3034 MBEDTLS_SSL_DEBUG_MSG(3, ("ticket_lifetime: %u",
3035 (unsigned int) ticket_lifetime));
Jerry Yue67bef42022-07-07 07:29:42 +00003036
3037 /* Write ticket_age_add */
Gilles Peskine449bd832023-01-11 14:50:10 +01003038 MBEDTLS_PUT_UINT32_BE(session->ticket_age_add, p, 4);
3039 MBEDTLS_SSL_DEBUG_MSG(3, ("ticket_age_add: %u",
3040 (unsigned int) session->ticket_age_add));
Jerry Yue67bef42022-07-07 07:29:42 +00003041
3042 /* Write ticket_nonce */
Gilles Peskine449bd832023-01-11 14:50:10 +01003043 p[8] = (unsigned char) ticket_nonce_size;
3044 if (ticket_nonce_size > 0) {
3045 memcpy(p + 9, ticket_nonce, ticket_nonce_size);
Jerry Yue67bef42022-07-07 07:29:42 +00003046 }
3047 p += 9 + ticket_nonce_size;
3048
3049 /* Write ticket */
Gilles Peskine449bd832023-01-11 14:50:10 +01003050 MBEDTLS_PUT_UINT16_BE(ticket_len, p, 0);
Jerry Yue67bef42022-07-07 07:29:42 +00003051 p += 2;
Gilles Peskine449bd832023-01-11 14:50:10 +01003052 MBEDTLS_SSL_DEBUG_BUF(4, "ticket", p, ticket_len);
Jerry Yue67bef42022-07-07 07:29:42 +00003053 p += ticket_len;
3054
3055 /* Ticket Extensions
3056 *
3057 * Note: We currently don't have any extensions.
3058 * Set length to zero.
3059 */
Jerry Yuedab6372022-10-31 14:37:31 +08003060 ssl->handshake->sent_extensions = MBEDTLS_SSL_EXT_MASK_NONE;
3061
Gilles Peskine449bd832023-01-11 14:50:10 +01003062 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 2);
3063 MBEDTLS_PUT_UINT16_BE(0, p, 0);
Jerry Yue67bef42022-07-07 07:29:42 +00003064 p += 2;
3065
3066 *out_len = p - buf;
Gilles Peskine449bd832023-01-11 14:50:10 +01003067 MBEDTLS_SSL_DEBUG_BUF(4, "ticket", buf, *out_len);
3068 MBEDTLS_SSL_DEBUG_MSG(2, ("<= write new session ticket"));
Jerry Yue67bef42022-07-07 07:29:42 +00003069
Jerry Yu7de2ff02022-11-08 21:43:46 +08003070 MBEDTLS_SSL_PRINT_EXTS(
Gilles Peskine449bd832023-01-11 14:50:10 +01003071 3, MBEDTLS_SSL_HS_NEW_SESSION_TICKET, ssl->handshake->sent_extensions);
Jerry Yu4b8f2f72022-10-31 13:31:22 +08003072
Gilles Peskine449bd832023-01-11 14:50:10 +01003073 return 0;
Jerry Yue67bef42022-07-07 07:29:42 +00003074}
3075
3076/*
Jerry Yua8d3c502022-10-30 14:51:23 +08003077 * Handler for MBEDTLS_SSL_TLS1_3_NEW_SESSION_TICKET
Jerry Yue67bef42022-07-07 07:29:42 +00003078 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003079static int ssl_tls13_write_new_session_ticket(mbedtls_ssl_context *ssl)
Jerry Yue67bef42022-07-07 07:29:42 +00003080{
3081 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
3082
Gilles Peskine449bd832023-01-11 14:50:10 +01003083 MBEDTLS_SSL_PROC_CHK_NEG(ssl_tls13_write_new_session_ticket_coordinate(ssl));
Jerry Yue67bef42022-07-07 07:29:42 +00003084
Gilles Peskine449bd832023-01-11 14:50:10 +01003085 if (ret == SSL_NEW_SESSION_TICKET_WRITE) {
Jerry Yue67bef42022-07-07 07:29:42 +00003086 unsigned char ticket_nonce[MBEDTLS_SSL_TLS1_3_TICKET_NONCE_LENGTH];
3087 unsigned char *buf;
3088 size_t buf_len, msg_len;
3089
Gilles Peskine449bd832023-01-11 14:50:10 +01003090 MBEDTLS_SSL_PROC_CHK(ssl_tls13_prepare_new_session_ticket(
3091 ssl, ticket_nonce, sizeof(ticket_nonce)));
Jerry Yue67bef42022-07-07 07:29:42 +00003092
Xiaokang Qian9f1747b2023-03-30 02:50:04 +00003093 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_start_handshake_msg(
3094 ssl, MBEDTLS_SSL_HS_NEW_SESSION_TICKET,
3095 &buf, &buf_len));
Jerry Yue67bef42022-07-07 07:29:42 +00003096
Gilles Peskine449bd832023-01-11 14:50:10 +01003097 MBEDTLS_SSL_PROC_CHK(ssl_tls13_write_new_session_ticket_body(
3098 ssl, buf, buf + buf_len, &msg_len,
3099 ticket_nonce, sizeof(ticket_nonce)));
Jerry Yue67bef42022-07-07 07:29:42 +00003100
Gilles Peskine449bd832023-01-11 14:50:10 +01003101 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_finish_handshake_msg(
3102 ssl, buf_len, msg_len));
Jerry Yufca4d572022-07-21 10:37:48 +08003103
Jerry Yu359e65f2022-09-22 23:47:43 +08003104 /* Limit session tickets count to one when resumption connection.
3105 *
3106 * See document of mbedtls_ssl_conf_new_session_tickets.
3107 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003108 if (ssl->handshake->resume == 1) {
Jerry Yu359e65f2022-09-22 23:47:43 +08003109 ssl->handshake->new_session_tickets_count = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01003110 } else {
Jerry Yu359e65f2022-09-22 23:47:43 +08003111 ssl->handshake->new_session_tickets_count--;
Gilles Peskine449bd832023-01-11 14:50:10 +01003112 }
Jerry Yub7e3fa72022-09-22 11:07:18 +08003113
Jerry Yua8d3c502022-10-30 14:51:23 +08003114 mbedtls_ssl_handshake_set_state(
Gilles Peskine449bd832023-01-11 14:50:10 +01003115 ssl, MBEDTLS_SSL_TLS1_3_NEW_SESSION_TICKET_FLUSH);
3116 } else {
3117 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_HANDSHAKE_OVER);
Jerry Yue67bef42022-07-07 07:29:42 +00003118 }
3119
Jerry Yue67bef42022-07-07 07:29:42 +00003120cleanup:
3121
Gilles Peskine449bd832023-01-11 14:50:10 +01003122 return ret;
Jerry Yue67bef42022-07-07 07:29:42 +00003123}
3124#endif /* MBEDTLS_SSL_SESSION_TICKETS */
3125
3126/*
XiaokangQiane8ff3502022-04-22 02:34:40 +00003127 * TLS 1.3 State Machine -- server side
XiaokangQian7807f9f2022-02-15 10:04:37 +00003128 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003129int mbedtls_ssl_tls13_handshake_server_step(mbedtls_ssl_context *ssl)
Jerry Yub9930e72021-08-06 17:11:51 +08003130{
XiaokangQian08037552022-04-20 07:16:41 +00003131 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
XiaokangQian7807f9f2022-02-15 10:04:37 +00003132
Gilles Peskine449bd832023-01-11 14:50:10 +01003133 if (ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER || ssl->handshake == NULL) {
3134 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
3135 }
XiaokangQian7807f9f2022-02-15 10:04:37 +00003136
Gilles Peskine449bd832023-01-11 14:50:10 +01003137 MBEDTLS_SSL_DEBUG_MSG(2, ("tls13 server state: %s(%d)",
Dave Rodgman2eab4622023-10-05 13:30:37 +01003138 mbedtls_ssl_states_str((mbedtls_ssl_states) ssl->state),
Gilles Peskine449bd832023-01-11 14:50:10 +01003139 ssl->state));
Jerry Yu6e81b272021-09-27 11:16:17 +08003140
Gilles Peskine449bd832023-01-11 14:50:10 +01003141 switch (ssl->state) {
XiaokangQian7807f9f2022-02-15 10:04:37 +00003142 /* start state */
3143 case MBEDTLS_SSL_HELLO_REQUEST:
Gilles Peskine449bd832023-01-11 14:50:10 +01003144 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_CLIENT_HELLO);
XiaokangQian08037552022-04-20 07:16:41 +00003145 ret = 0;
XiaokangQian7807f9f2022-02-15 10:04:37 +00003146 break;
3147
XiaokangQian7807f9f2022-02-15 10:04:37 +00003148 case MBEDTLS_SSL_CLIENT_HELLO:
Gilles Peskine449bd832023-01-11 14:50:10 +01003149 ret = ssl_tls13_process_client_hello(ssl);
3150 if (ret != 0) {
3151 MBEDTLS_SSL_DEBUG_RET(1, "ssl_tls13_process_client_hello", ret);
3152 }
Jerry Yuf41553b2022-05-09 22:20:30 +08003153 break;
XiaokangQian7807f9f2022-02-15 10:04:37 +00003154
Jerry Yuf41553b2022-05-09 22:20:30 +08003155 case MBEDTLS_SSL_HELLO_RETRY_REQUEST:
Gilles Peskine449bd832023-01-11 14:50:10 +01003156 ret = ssl_tls13_write_hello_retry_request(ssl);
3157 if (ret != 0) {
3158 MBEDTLS_SSL_DEBUG_RET(1, "ssl_tls13_write_hello_retry_request", ret);
3159 return ret;
Jerry Yuf41553b2022-05-09 22:20:30 +08003160 }
XiaokangQian7807f9f2022-02-15 10:04:37 +00003161 break;
3162
Jerry Yu5b64ae92022-03-30 17:15:02 +08003163 case MBEDTLS_SSL_SERVER_HELLO:
Gilles Peskine449bd832023-01-11 14:50:10 +01003164 ret = ssl_tls13_write_server_hello(ssl);
Jerry Yu5b64ae92022-03-30 17:15:02 +08003165 break;
3166
Xiaofei Baicba64af2022-02-15 10:00:56 +00003167 case MBEDTLS_SSL_ENCRYPTED_EXTENSIONS:
Gilles Peskine449bd832023-01-11 14:50:10 +01003168 ret = ssl_tls13_write_encrypted_extensions(ssl);
3169 if (ret != 0) {
3170 MBEDTLS_SSL_DEBUG_RET(1, "ssl_tls13_write_encrypted_extensions", ret);
3171 return ret;
Xiaofei Baicba64af2022-02-15 10:00:56 +00003172 }
Jerry Yu48330562022-05-06 21:35:44 +08003173 break;
Jerry Yu6a2cd9e2022-05-05 11:14:19 +08003174
Ronald Cron928cbd32022-10-04 16:14:26 +02003175#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED)
Xiaofei Bai9ca09d42022-02-14 12:57:18 +00003176 case MBEDTLS_SSL_CERTIFICATE_REQUEST:
Gilles Peskine449bd832023-01-11 14:50:10 +01003177 ret = ssl_tls13_write_certificate_request(ssl);
Xiaofei Bai9ca09d42022-02-14 12:57:18 +00003178 break;
Xiaofei Bai9ca09d42022-02-14 12:57:18 +00003179
Jerry Yu83da34e2022-04-16 13:59:52 +08003180 case MBEDTLS_SSL_SERVER_CERTIFICATE:
Gilles Peskine449bd832023-01-11 14:50:10 +01003181 ret = ssl_tls13_write_server_certificate(ssl);
Jerry Yu83da34e2022-04-16 13:59:52 +08003182 break;
3183
3184 case MBEDTLS_SSL_CERTIFICATE_VERIFY:
Gilles Peskine449bd832023-01-11 14:50:10 +01003185 ret = ssl_tls13_write_certificate_verify(ssl);
Jerry Yu83da34e2022-04-16 13:59:52 +08003186 break;
Ronald Cron928cbd32022-10-04 16:14:26 +02003187#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED */
Jerry Yu83da34e2022-04-16 13:59:52 +08003188
Gilles Peskine449bd832023-01-11 14:50:10 +01003189 /*
3190 * Injection of dummy-CCS's for middlebox compatibility
3191 */
Gabor Mezei7b39bf12022-05-24 16:04:14 +02003192#if defined(MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE)
Gabor Mezeif7044ea2022-06-28 16:01:49 +02003193 case MBEDTLS_SSL_SERVER_CCS_AFTER_HELLO_RETRY_REQUEST:
Gilles Peskine449bd832023-01-11 14:50:10 +01003194 ret = mbedtls_ssl_tls13_write_change_cipher_spec(ssl);
3195 if (ret == 0) {
3196 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_CLIENT_HELLO);
3197 }
Gabor Mezei7b39bf12022-05-24 16:04:14 +02003198 break;
3199
3200 case MBEDTLS_SSL_SERVER_CCS_AFTER_SERVER_HELLO:
Gilles Peskine449bd832023-01-11 14:50:10 +01003201 ret = mbedtls_ssl_tls13_write_change_cipher_spec(ssl);
3202 if (ret == 0) {
3203 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_ENCRYPTED_EXTENSIONS);
3204 }
Gabor Mezei7b39bf12022-05-24 16:04:14 +02003205 break;
3206#endif /* MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE */
3207
Jerry Yu69dd8d42022-04-16 12:51:26 +08003208 case MBEDTLS_SSL_SERVER_FINISHED:
Gilles Peskine449bd832023-01-11 14:50:10 +01003209 ret = ssl_tls13_write_server_finished(ssl);
Jerry Yu69dd8d42022-04-16 12:51:26 +08003210 break;
3211
3212 case MBEDTLS_SSL_CLIENT_FINISHED:
Gilles Peskine449bd832023-01-11 14:50:10 +01003213 ret = ssl_tls13_process_client_finished(ssl);
Jerry Yu69dd8d42022-04-16 12:51:26 +08003214 break;
3215
Jerry Yu69dd8d42022-04-16 12:51:26 +08003216 case MBEDTLS_SSL_HANDSHAKE_WRAPUP:
Gilles Peskine449bd832023-01-11 14:50:10 +01003217 ret = ssl_tls13_handshake_wrapup(ssl);
Jerry Yu69dd8d42022-04-16 12:51:26 +08003218 break;
3219
Ronald Cron766c0cd2022-10-18 12:17:11 +02003220#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED)
XiaokangQian6b916b12022-04-25 07:29:34 +00003221 case MBEDTLS_SSL_CLIENT_CERTIFICATE:
Gilles Peskine449bd832023-01-11 14:50:10 +01003222 ret = mbedtls_ssl_tls13_process_certificate(ssl);
3223 if (ret == 0) {
3224 if (ssl->session_negotiate->peer_cert != NULL) {
Ronald Cron209cae92022-06-07 10:30:19 +02003225 mbedtls_ssl_handshake_set_state(
Gilles Peskine449bd832023-01-11 14:50:10 +01003226 ssl, MBEDTLS_SSL_CLIENT_CERTIFICATE_VERIFY);
3227 } else {
3228 MBEDTLS_SSL_DEBUG_MSG(2, ("skip parse certificate verify"));
Ronald Cron209cae92022-06-07 10:30:19 +02003229 mbedtls_ssl_handshake_set_state(
Gilles Peskine449bd832023-01-11 14:50:10 +01003230 ssl, MBEDTLS_SSL_CLIENT_FINISHED);
Ronald Cron19385882022-06-15 16:26:13 +02003231 }
XiaokangQian6b916b12022-04-25 07:29:34 +00003232 }
3233 break;
3234
3235 case MBEDTLS_SSL_CLIENT_CERTIFICATE_VERIFY:
Gilles Peskine449bd832023-01-11 14:50:10 +01003236 ret = mbedtls_ssl_tls13_process_certificate_verify(ssl);
3237 if (ret == 0) {
XiaokangQian6b916b12022-04-25 07:29:34 +00003238 mbedtls_ssl_handshake_set_state(
Gilles Peskine449bd832023-01-11 14:50:10 +01003239 ssl, MBEDTLS_SSL_CLIENT_FINISHED);
XiaokangQian6b916b12022-04-25 07:29:34 +00003240 }
3241 break;
Ronald Cron766c0cd2022-10-18 12:17:11 +02003242#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED */
XiaokangQian6b916b12022-04-25 07:29:34 +00003243
Jerry Yue67bef42022-07-07 07:29:42 +00003244#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Jerry Yua8d3c502022-10-30 14:51:23 +08003245 case MBEDTLS_SSL_TLS1_3_NEW_SESSION_TICKET:
Gilles Peskine449bd832023-01-11 14:50:10 +01003246 ret = ssl_tls13_write_new_session_ticket(ssl);
3247 if (ret != 0) {
3248 MBEDTLS_SSL_DEBUG_RET(1,
3249 "ssl_tls13_write_new_session_ticket ",
3250 ret);
Jerry Yue67bef42022-07-07 07:29:42 +00003251 }
3252 break;
Jerry Yua8d3c502022-10-30 14:51:23 +08003253 case MBEDTLS_SSL_TLS1_3_NEW_SESSION_TICKET_FLUSH:
Jerry Yue67bef42022-07-07 07:29:42 +00003254 /* This state is necessary to do the flush of the New Session
Jerry Yua8d3c502022-10-30 14:51:23 +08003255 * Ticket message written in MBEDTLS_SSL_TLS1_3_NEW_SESSION_TICKET
Jerry Yue67bef42022-07-07 07:29:42 +00003256 * as part of ssl_prepare_handshake_step.
3257 */
3258 ret = 0;
Jerry Yud4e75002022-08-09 13:33:50 +08003259
Gilles Peskine449bd832023-01-11 14:50:10 +01003260 if (ssl->handshake->new_session_tickets_count == 0) {
3261 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_HANDSHAKE_OVER);
3262 } else {
Xiaokang Qian9f1747b2023-03-30 02:50:04 +00003263 mbedtls_ssl_handshake_set_state(
3264 ssl, MBEDTLS_SSL_TLS1_3_NEW_SESSION_TICKET);
Gilles Peskine449bd832023-01-11 14:50:10 +01003265 }
Jerry Yue67bef42022-07-07 07:29:42 +00003266 break;
3267
3268#endif /* MBEDTLS_SSL_SESSION_TICKETS */
3269
XiaokangQian7807f9f2022-02-15 10:04:37 +00003270 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01003271 MBEDTLS_SSL_DEBUG_MSG(1, ("invalid state %d", ssl->state));
3272 return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
XiaokangQian7807f9f2022-02-15 10:04:37 +00003273 }
3274
Gilles Peskine449bd832023-01-11 14:50:10 +01003275 return ret;
Jerry Yub9930e72021-08-06 17:11:51 +08003276}
Jerry Yu3cc4c2a2021-08-06 16:29:08 +08003277
Jerry Yufb4b6472022-01-27 15:03:26 +08003278#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_PROTO_TLS1_3 */