blob: d0e819537e0e8334f11055fdd5518533c4256723 [file] [log] [blame]
Jerry Yu3cc4c2a2021-08-06 16:29:08 +08001/*
Jerry Yub9930e72021-08-06 17:11:51 +08002 * TLS 1.3 server-side functions
Jerry Yu3cc4c2a2021-08-06 16:29:08 +08003 *
4 * Copyright The Mbed TLS Contributors
Dave Rodgman16799db2023-11-02 19:47:20 +00005 * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
Gilles Peskine449bd832023-01-11 14:50:10 +01006 */
Jerry Yu3cc4c2a2021-08-06 16:29:08 +08007
8#include "common.h"
9
Jerry Yufb4b6472022-01-27 15:03:26 +080010#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_PROTO_TLS1_3)
Jerry Yu3cc4c2a2021-08-06 16:29:08 +080011
Valerio Settib4f50762024-01-17 10:24:52 +010012#include "debug_internal.h"
Xiaofei Baicba64af2022-02-15 10:00:56 +000013#include "mbedtls/error.h"
14#include "mbedtls/platform.h"
Jerry Yu1c105562022-07-10 06:32:38 +000015#include "mbedtls/constant_time.h"
Manuel Pégourié-Gonnardd55d66f2023-06-20 10:14:58 +020016#include "mbedtls/oid.h"
Valerio Setti384fbde2024-01-02 13:26:40 +010017#include "mbedtls/psa_util.h"
Jerry Yu3bf2c642022-03-30 22:02:12 +080018
Xiaofei Bai9ca09d42022-02-14 12:57:18 +000019#include "ssl_misc.h"
20#include "ssl_tls13_keys.h"
21#include "ssl_debug_helpers.h"
22
Jerry Yuf35ba382022-08-23 17:58:26 +080023
Jerry Yuc5a23a02022-08-25 10:51:44 +080024static const mbedtls_ssl_ciphersuite_t *ssl_tls13_validate_peer_ciphersuite(
Gilles Peskine449bd832023-01-11 14:50:10 +010025 mbedtls_ssl_context *ssl,
26 unsigned int cipher_suite)
Jerry Yuf35ba382022-08-23 17:58:26 +080027{
28 const mbedtls_ssl_ciphersuite_t *ciphersuite_info;
Gilles Peskine449bd832023-01-11 14:50:10 +010029 if (!mbedtls_ssl_tls13_cipher_suite_is_offered(ssl, cipher_suite)) {
30 return NULL;
Jerry Yuf35ba382022-08-23 17:58:26 +080031 }
Gilles Peskine449bd832023-01-11 14:50:10 +010032
33 ciphersuite_info = mbedtls_ssl_ciphersuite_from_id(cipher_suite);
34 if ((mbedtls_ssl_validate_ciphersuite(ssl, ciphersuite_info,
35 ssl->tls_version,
36 ssl->tls_version) != 0)) {
37 return NULL;
38 }
39 return ciphersuite_info;
Jerry Yuf35ba382022-08-23 17:58:26 +080040}
41
Ronald Cron41a443a2022-10-04 16:38:25 +020042#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED)
Jerry Yue19e3b92022-07-08 12:04:51 +000043/* From RFC 8446:
44 *
45 * enum { psk_ke(0), psk_dhe_ke(1), (255) } PskKeyExchangeMode;
46 * struct {
47 * PskKeyExchangeMode ke_modes<1..255>;
48 * } PskKeyExchangeModes;
49 */
Jerry Yu6e74a7e2022-07-20 20:49:32 +080050MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +010051static int ssl_tls13_parse_key_exchange_modes_ext(mbedtls_ssl_context *ssl,
52 const unsigned char *buf,
53 const unsigned char *end)
Jerry Yue19e3b92022-07-08 12:04:51 +000054{
Jerry Yu299e31f2022-07-13 23:06:36 +080055 const unsigned char *p = buf;
Jerry Yue19e3b92022-07-08 12:04:51 +000056 size_t ke_modes_len;
57 int ke_modes = 0;
58
Jerry Yu854dd9e2022-07-15 14:28:27 +080059 /* Read ke_modes length (1 Byte) */
Gilles Peskine449bd832023-01-11 14:50:10 +010060 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, 1);
Jerry Yu299e31f2022-07-13 23:06:36 +080061 ke_modes_len = *p++;
Jerry Yue19e3b92022-07-08 12:04:51 +000062 /* Currently, there are only two PSK modes, so even without looking
63 * at the content, something's wrong if the list has more than 2 items. */
Gilles Peskine449bd832023-01-11 14:50:10 +010064 if (ke_modes_len > 2) {
65 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER,
66 MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER);
67 return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
Jerry Yu299e31f2022-07-13 23:06:36 +080068 }
Jerry Yue19e3b92022-07-08 12:04:51 +000069
Gilles Peskine449bd832023-01-11 14:50:10 +010070 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, ke_modes_len);
Jerry Yue19e3b92022-07-08 12:04:51 +000071
Gilles Peskine449bd832023-01-11 14:50:10 +010072 while (ke_modes_len-- != 0) {
73 switch (*p++) {
74 case MBEDTLS_SSL_TLS1_3_PSK_MODE_PURE:
75 ke_modes |= MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK;
76 MBEDTLS_SSL_DEBUG_MSG(3, ("Found PSK KEX MODE"));
77 break;
78 case MBEDTLS_SSL_TLS1_3_PSK_MODE_ECDHE:
79 ke_modes |= MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK_EPHEMERAL;
80 MBEDTLS_SSL_DEBUG_MSG(3, ("Found PSK_EPHEMERAL KEX MODE"));
81 break;
82 default:
83 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER,
84 MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER);
85 return MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER;
Jerry Yue19e3b92022-07-08 12:04:51 +000086 }
87 }
88
89 ssl->handshake->tls13_kex_modes = ke_modes;
Gilles Peskine449bd832023-01-11 14:50:10 +010090 return 0;
Jerry Yue19e3b92022-07-08 12:04:51 +000091}
Jerry Yu1c105562022-07-10 06:32:38 +000092
Ronald Cronfbae94a2023-12-05 18:15:14 +010093#define SSL_TLS1_3_PSK_IDENTITY_DOES_NOT_MATCH 2
94#define SSL_TLS1_3_PSK_IDENTITY_MATCH_BUT_PSK_NOT_USABLE 1
95#define SSL_TLS1_3_PSK_IDENTITY_MATCH 0
Jerry Yu95699e72022-08-21 19:22:23 +080096
97#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Pengyu Lv29daf4a2023-10-30 17:13:30 +080098MBEDTLS_CHECK_RETURN_CRITICAL
Pengyu Lvbc4aab72023-12-01 15:37:24 +080099static int ssl_tls13_key_exchange_is_psk_available(mbedtls_ssl_context *ssl);
Pengyu Lv29daf4a2023-10-30 17:13:30 +0800100MBEDTLS_CHECK_RETURN_CRITICAL
Pengyu Lvbc4aab72023-12-01 15:37:24 +0800101static int ssl_tls13_key_exchange_is_psk_ephemeral_available(mbedtls_ssl_context *ssl);
Jerry Yu95699e72022-08-21 19:22:23 +0800102
103MBEDTLS_CHECK_RETURN_CRITICAL
104static int ssl_tls13_offered_psks_check_identity_match_ticket(
Gilles Peskine449bd832023-01-11 14:50:10 +0100105 mbedtls_ssl_context *ssl,
106 const unsigned char *identity,
107 size_t identity_len,
108 uint32_t obfuscated_ticket_age,
109 mbedtls_ssl_session *session)
Jerry Yu95699e72022-08-21 19:22:23 +0800110{
Jerry Yufd310eb2022-09-06 09:16:35 +0800111 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yu95699e72022-08-21 19:22:23 +0800112 unsigned char *ticket_buffer;
Pengyu Lv29daf4a2023-10-30 17:13:30 +0800113 unsigned int key_exchanges;
Jerry Yu8d4bbba2022-09-13 14:15:48 +0800114#if defined(MBEDTLS_HAVE_TIME)
Jerry Yucebffc32022-12-15 18:00:05 +0800115 mbedtls_ms_time_t now;
116 mbedtls_ms_time_t server_age;
Jerry Yu713ce1f2023-11-17 15:32:12 +0800117 uint32_t client_age;
Jerry Yucebffc32022-12-15 18:00:05 +0800118 mbedtls_ms_time_t age_diff;
Jerry Yu8d4bbba2022-09-13 14:15:48 +0800119#endif
Jerry Yu95699e72022-08-21 19:22:23 +0800120
121 ((void) obfuscated_ticket_age);
122
Gilles Peskine449bd832023-01-11 14:50:10 +0100123 MBEDTLS_SSL_DEBUG_MSG(2, ("=> check_identity_match_ticket"));
Jerry Yu95699e72022-08-21 19:22:23 +0800124
Jerry Yufd310eb2022-09-06 09:16:35 +0800125 /* Ticket parser is not configured, Skip */
Gilles Peskine449bd832023-01-11 14:50:10 +0100126 if (ssl->conf->f_ticket_parse == NULL || identity_len == 0) {
Ronald Cronfbae94a2023-12-05 18:15:14 +0100127 return SSL_TLS1_3_PSK_IDENTITY_DOES_NOT_MATCH;
Gilles Peskine449bd832023-01-11 14:50:10 +0100128 }
Jerry Yu95699e72022-08-21 19:22:23 +0800129
Jerry Yu4746b102022-09-13 11:11:48 +0800130 /* We create a copy of the encrypted ticket since the ticket parsing
131 * function is allowed to use its input buffer as an output buffer
132 * (in-place decryption). We do, however, need the original buffer for
133 * computing the PSK binder value.
Jerry Yu95699e72022-08-21 19:22:23 +0800134 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100135 ticket_buffer = mbedtls_calloc(1, identity_len);
136 if (ticket_buffer == NULL) {
137 MBEDTLS_SSL_DEBUG_MSG(1, ("buffer too small"));
138 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
Jerry Yu95699e72022-08-21 19:22:23 +0800139 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100140 memcpy(ticket_buffer, identity, identity_len);
Jerry Yu95699e72022-08-21 19:22:23 +0800141
Ronald Cronfbae94a2023-12-05 18:15:14 +0100142 ret = ssl->conf->f_ticket_parse(ssl->conf->p_ticket,
143 session,
144 ticket_buffer, identity_len);
145 if (ret == 0) {
146 ret = SSL_TLS1_3_PSK_IDENTITY_MATCH;
147 } else {
148 if (ret == MBEDTLS_ERR_SSL_SESSION_TICKET_EXPIRED) {
Gilles Peskine449bd832023-01-11 14:50:10 +0100149 MBEDTLS_SSL_DEBUG_MSG(3, ("ticket is expired"));
Ronald Cronfbae94a2023-12-05 18:15:14 +0100150 ret = SSL_TLS1_3_PSK_IDENTITY_MATCH_BUT_PSK_NOT_USABLE;
Gilles Peskine449bd832023-01-11 14:50:10 +0100151 } else {
Ronald Cronfbae94a2023-12-05 18:15:14 +0100152 if (ret == MBEDTLS_ERR_SSL_INVALID_MAC) {
153 MBEDTLS_SSL_DEBUG_MSG(3, ("ticket is not authentic"));
154 } else {
155 MBEDTLS_SSL_DEBUG_RET(1, "ticket_parse", ret);
156 }
157 ret = SSL_TLS1_3_PSK_IDENTITY_DOES_NOT_MATCH;
Gilles Peskine449bd832023-01-11 14:50:10 +0100158 }
Jerry Yu95699e72022-08-21 19:22:23 +0800159 }
160
161 /* We delete the temporary buffer */
Gilles Peskine449bd832023-01-11 14:50:10 +0100162 mbedtls_free(ticket_buffer);
Jerry Yu95699e72022-08-21 19:22:23 +0800163
Ronald Cronfbae94a2023-12-05 18:15:14 +0100164 if (ret != SSL_TLS1_3_PSK_IDENTITY_MATCH) {
165 goto exit;
Jerry Yuce3b95e2023-10-31 16:02:04 +0800166 }
Jerry Yuce3b95e2023-10-31 16:02:04 +0800167
Ronald Cronfbae94a2023-12-05 18:15:14 +0100168 ret = SSL_TLS1_3_PSK_IDENTITY_MATCH_BUT_PSK_NOT_USABLE;
169
170 if (session->tls_version != MBEDTLS_SSL_VERSION_TLS1_3) {
171 MBEDTLS_SSL_DEBUG_MSG(3, ("Ticket TLS version is not 1.3."));
Jerry Yu8d4bbba2022-09-13 14:15:48 +0800172 goto exit;
173 }
Jerry Yu95699e72022-08-21 19:22:23 +0800174
Pengyu Lv3eb49be2022-12-05 16:35:12 +0800175 /* RFC 8446 section 4.2.9
176 *
177 * Servers SHOULD NOT send NewSessionTicket with tickets that are not
178 * compatible with the advertised modes; however, if a server does so,
179 * the impact will just be that the client's attempts at resumption fail.
180 *
181 * We regard the ticket with incompatible key exchange modes as not match.
182 */
Pengyu Lv29daf4a2023-10-30 17:13:30 +0800183 MBEDTLS_SSL_PRINT_TICKET_FLAGS(4, session->ticket_flags);
Pengyu Lv29daf4a2023-10-30 17:13:30 +0800184
185 key_exchanges = 0;
Pengyu Lv94a42cc2023-12-06 10:04:17 +0800186 if (mbedtls_ssl_tls13_session_ticket_allow_psk_ephemeral(session) &&
Pengyu Lvbc4aab72023-12-01 15:37:24 +0800187 ssl_tls13_key_exchange_is_psk_ephemeral_available(ssl)) {
Pengyu Lv29daf4a2023-10-30 17:13:30 +0800188 key_exchanges |= MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK_EPHEMERAL;
189 }
Pengyu Lv94a42cc2023-12-06 10:04:17 +0800190 if (mbedtls_ssl_tls13_session_ticket_allow_psk(session) &&
Pengyu Lvbc4aab72023-12-01 15:37:24 +0800191 ssl_tls13_key_exchange_is_psk_available(ssl)) {
Pengyu Lv29daf4a2023-10-30 17:13:30 +0800192 key_exchanges |= MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK;
193 }
194
195 if (key_exchanges == 0) {
Pengyu Lv3eb49be2022-12-05 16:35:12 +0800196 MBEDTLS_SSL_DEBUG_MSG(3, ("No suitable key exchange mode"));
197 goto exit;
198 }
199
Gilles Peskine449bd832023-01-11 14:50:10 +0100200#if defined(MBEDTLS_HAVE_TIME)
Jerry Yucebffc32022-12-15 18:00:05 +0800201 now = mbedtls_ms_time();
Gilles Peskine449bd832023-01-11 14:50:10 +0100202
Jerry Yu25ba4d42023-11-10 14:12:20 +0800203 if (now < session->ticket_creation_time) {
Gilles Peskine449bd832023-01-11 14:50:10 +0100204 MBEDTLS_SSL_DEBUG_MSG(
Jerry Yu713ce1f2023-11-17 15:32:12 +0800205 3, ("Invalid ticket creation time ( now = %" MBEDTLS_PRINTF_MS_TIME
206 ", creation_time = %" MBEDTLS_PRINTF_MS_TIME " )",
Jerry Yu25ba4d42023-11-10 14:12:20 +0800207 now, session->ticket_creation_time));
Gilles Peskine449bd832023-01-11 14:50:10 +0100208 goto exit;
209 }
210
Jerry Yu25ba4d42023-11-10 14:12:20 +0800211 server_age = now - session->ticket_creation_time;
Jerry Yu95699e72022-08-21 19:22:23 +0800212
Jerry Yu8d4bbba2022-09-13 14:15:48 +0800213 /* RFC 8446 section 4.6.1
214 *
215 * Servers MUST NOT use any value greater than 604800 seconds (7 days).
216 *
217 * RFC 8446 section 4.2.11.1
218 *
219 * Clients MUST NOT attempt to use tickets which have ages greater than
220 * the "ticket_lifetime" value which was provided with the ticket.
221 *
Jerry Yu8d4bbba2022-09-13 14:15:48 +0800222 */
Jerry Yu8e0174a2023-11-10 13:58:16 +0800223 if (server_age > MBEDTLS_SSL_TLS1_3_MAX_ALLOWED_TICKET_LIFETIME * 1000) {
Jerry Yu8d4bbba2022-09-13 14:15:48 +0800224 MBEDTLS_SSL_DEBUG_MSG(
Jerry Yud84c14f2023-11-16 13:33:57 +0800225 3, ("Ticket age exceeds limitation ticket_age = %" MBEDTLS_PRINTF_MS_TIME,
Jerry Yucebffc32022-12-15 18:00:05 +0800226 server_age));
Jerry Yu8d4bbba2022-09-13 14:15:48 +0800227 goto exit;
228 }
Jerry Yu95699e72022-08-21 19:22:23 +0800229
Jerry Yu8d4bbba2022-09-13 14:15:48 +0800230 /* RFC 8446 section 4.2.10
231 *
232 * For PSKs provisioned via NewSessionTicket, a server MUST validate that
233 * the ticket age for the selected PSK identity (computed by subtracting
234 * ticket_age_add from PskIdentity.obfuscated_ticket_age modulo 2^32) is
235 * within a small tolerance of the time since the ticket was issued.
Jerry Yuf7dad3c2022-09-14 22:31:39 +0800236 *
Jerry Yu31b601a2023-11-10 11:27:21 +0800237 * NOTE: The typical accuracy of an RTC crystal is ±100 to ±20 parts per
238 * million (360 to 72 milliseconds per hour). Default tolerance
Jerry Yu9cb953a2023-11-15 09:54:11 +0800239 * window is 6s, thus in the worst case clients and servers must
Jerry Yu31b601a2023-11-10 11:27:21 +0800240 * sync up their system time every 6000/360/2~=8 hours.
Jerry Yu8d4bbba2022-09-13 14:15:48 +0800241 */
Jerry Yucebffc32022-12-15 18:00:05 +0800242 client_age = obfuscated_ticket_age - session->ticket_age_add;
Jerry Yu60e99722023-11-20 09:55:24 +0800243 age_diff = server_age - (mbedtls_ms_time_t) client_age;
Jerry Yu28e7c552023-11-10 12:05:56 +0800244 if (age_diff < -MBEDTLS_SSL_TLS1_3_TICKET_AGE_TOLERANCE ||
Jerry Yucebffc32022-12-15 18:00:05 +0800245 age_diff > MBEDTLS_SSL_TLS1_3_TICKET_AGE_TOLERANCE) {
Jerry Yu8d4bbba2022-09-13 14:15:48 +0800246 MBEDTLS_SSL_DEBUG_MSG(
Jerry Yuf16efbc2023-10-30 11:06:24 +0800247 3, ("Ticket age outside tolerance window ( diff = %"
248 MBEDTLS_PRINTF_MS_TIME ")",
Jerry Yucebffc32022-12-15 18:00:05 +0800249 age_diff));
Jerry Yu8d4bbba2022-09-13 14:15:48 +0800250 goto exit;
251 }
Jerry Yu95699e72022-08-21 19:22:23 +0800252#endif /* MBEDTLS_HAVE_TIME */
Jerry Yu8d4bbba2022-09-13 14:15:48 +0800253
Ronald Cronfbae94a2023-12-05 18:15:14 +0100254 ret = SSL_TLS1_3_PSK_IDENTITY_MATCH;
255
Jerry Yu8d4bbba2022-09-13 14:15:48 +0800256exit:
Ronald Cronfbae94a2023-12-05 18:15:14 +0100257 if (ret != SSL_TLS1_3_PSK_IDENTITY_MATCH) {
Gilles Peskine449bd832023-01-11 14:50:10 +0100258 mbedtls_ssl_session_free(session);
259 }
Jerry Yu95699e72022-08-21 19:22:23 +0800260
Gilles Peskine449bd832023-01-11 14:50:10 +0100261 MBEDTLS_SSL_DEBUG_MSG(2, ("<= check_identity_match_ticket"));
262 return ret;
Jerry Yu95699e72022-08-21 19:22:23 +0800263}
264#endif /* MBEDTLS_SSL_SESSION_TICKETS */
265
Jerry Yu6e74a7e2022-07-20 20:49:32 +0800266MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yu1c105562022-07-10 06:32:38 +0000267static int ssl_tls13_offered_psks_check_identity_match(
Gilles Peskine449bd832023-01-11 14:50:10 +0100268 mbedtls_ssl_context *ssl,
269 const unsigned char *identity,
270 size_t identity_len,
271 uint32_t obfuscated_ticket_age,
272 int *psk_type,
273 mbedtls_ssl_session *session)
Jerry Yu1c105562022-07-10 06:32:38 +0000274{
Ronald Cron606671e2023-02-17 11:36:33 +0100275 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
276
Jerry Yu95699e72022-08-21 19:22:23 +0800277 ((void) session);
278 ((void) obfuscated_ticket_age);
Jerry Yu5725f1c2022-08-21 17:27:16 +0800279 *psk_type = MBEDTLS_SSL_TLS1_3_PSK_EXTERNAL;
Jerry Yu95699e72022-08-21 19:22:23 +0800280
Gilles Peskine449bd832023-01-11 14:50:10 +0100281 MBEDTLS_SSL_DEBUG_BUF(4, "identity", identity, identity_len);
Jerry Yu95699e72022-08-21 19:22:23 +0800282 ssl->handshake->resume = 0;
283
Jerry Yu95699e72022-08-21 19:22:23 +0800284#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Ronald Cron7a30cf52024-02-23 14:38:59 +0100285 ret = ssl_tls13_offered_psks_check_identity_match_ticket(
286 ssl, identity, identity_len, obfuscated_ticket_age, session);
287 if (ret == SSL_TLS1_3_PSK_IDENTITY_MATCH) {
Jerry Yu95699e72022-08-21 19:22:23 +0800288 ssl->handshake->resume = 1;
289 *psk_type = MBEDTLS_SSL_TLS1_3_PSK_RESUMPTION;
Ronald Cron606671e2023-02-17 11:36:33 +0100290 ret = mbedtls_ssl_set_hs_psk(ssl,
291 session->resumption_key,
292 session->resumption_key_len);
293 if (ret != 0) {
294 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_set_hs_psk", ret);
295 return ret;
296 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100297
298 MBEDTLS_SSL_DEBUG_BUF(4, "Ticket-resumed PSK:",
299 session->resumption_key,
300 session->resumption_key_len);
301 MBEDTLS_SSL_DEBUG_MSG(4, ("ticket: obfuscated_ticket_age: %u",
302 (unsigned) obfuscated_ticket_age));
Ronald Cronfbae94a2023-12-05 18:15:14 +0100303 return SSL_TLS1_3_PSK_IDENTITY_MATCH;
Ronald Cron7a30cf52024-02-23 14:38:59 +0100304 } else if (ret == SSL_TLS1_3_PSK_IDENTITY_MATCH_BUT_PSK_NOT_USABLE) {
305 return SSL_TLS1_3_PSK_IDENTITY_MATCH_BUT_PSK_NOT_USABLE;
Jerry Yu95699e72022-08-21 19:22:23 +0800306 }
307#endif /* MBEDTLS_SSL_SESSION_TICKETS */
308
Jerry Yu1c105562022-07-10 06:32:38 +0000309 /* Check identity with external configured function */
Gilles Peskine449bd832023-01-11 14:50:10 +0100310 if (ssl->conf->f_psk != NULL) {
311 if (ssl->conf->f_psk(
312 ssl->conf->p_psk, ssl, identity, identity_len) == 0) {
Ronald Cronfbae94a2023-12-05 18:15:14 +0100313 return SSL_TLS1_3_PSK_IDENTITY_MATCH;
Jerry Yu1c105562022-07-10 06:32:38 +0000314 }
Ronald Cronfbae94a2023-12-05 18:15:14 +0100315 return SSL_TLS1_3_PSK_IDENTITY_DOES_NOT_MATCH;
Jerry Yu1c105562022-07-10 06:32:38 +0000316 }
317
Gilles Peskine449bd832023-01-11 14:50:10 +0100318 MBEDTLS_SSL_DEBUG_BUF(5, "identity", identity, identity_len);
Jerry Yu1c105562022-07-10 06:32:38 +0000319 /* Check identity with pre-configured psk */
Gilles Peskine449bd832023-01-11 14:50:10 +0100320 if (ssl->conf->psk_identity != NULL &&
Jerry Yu2f0abc92022-07-22 19:34:48 +0800321 identity_len == ssl->conf->psk_identity_len &&
Gilles Peskine449bd832023-01-11 14:50:10 +0100322 mbedtls_ct_memcmp(ssl->conf->psk_identity,
323 identity, identity_len) == 0) {
Ronald Cron606671e2023-02-17 11:36:33 +0100324 ret = mbedtls_ssl_set_hs_psk(ssl, ssl->conf->psk, ssl->conf->psk_len);
325 if (ret != 0) {
326 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_set_hs_psk", ret);
327 return ret;
328 }
Ronald Cronfbae94a2023-12-05 18:15:14 +0100329 return SSL_TLS1_3_PSK_IDENTITY_MATCH;
Jerry Yu1c105562022-07-10 06:32:38 +0000330 }
331
Ronald Cronfbae94a2023-12-05 18:15:14 +0100332 return SSL_TLS1_3_PSK_IDENTITY_DOES_NOT_MATCH;
Jerry Yu1c105562022-07-10 06:32:38 +0000333}
334
Ronald Cron6e311272023-12-05 17:57:01 +0100335#define SSL_TLS1_3_BINDER_DOES_NOT_MATCH 1
336#define SSL_TLS1_3_BINDER_MATCH 0
Jerry Yu6e74a7e2022-07-20 20:49:32 +0800337MBEDTLS_CHECK_RETURN_CRITICAL
Xiaokang Qian9f1747b2023-03-30 02:50:04 +0000338static int ssl_tls13_offered_psks_check_binder_match(
339 mbedtls_ssl_context *ssl,
340 const unsigned char *binder, size_t binder_len,
341 int psk_type, psa_algorithm_t psk_hash_alg)
Jerry Yu1c105562022-07-10 06:32:38 +0000342{
343 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yu96a2e362022-07-21 15:11:34 +0800344
Jerry Yudaf375a2022-07-20 21:31:43 +0800345 unsigned char transcript[PSA_HASH_MAX_SIZE];
Jerry Yu1c105562022-07-10 06:32:38 +0000346 size_t transcript_len;
Jerry Yu2f0abc92022-07-22 19:34:48 +0800347 unsigned char *psk;
Jerry Yu96a2e362022-07-21 15:11:34 +0800348 size_t psk_len;
Jerry Yudaf375a2022-07-20 21:31:43 +0800349 unsigned char server_computed_binder[PSA_HASH_MAX_SIZE];
Jerry Yu1c105562022-07-10 06:32:38 +0000350
Jerry Yu1c105562022-07-10 06:32:38 +0000351 /* Get current state of handshake transcript. */
Jerry Yu29d9faa2022-08-23 17:52:45 +0800352 ret = mbedtls_ssl_get_handshake_transcript(
Manuel Pégourié-Gonnard2d6d9932023-03-28 11:38:08 +0200353 ssl, mbedtls_md_type_from_psa_alg(psk_hash_alg),
Gilles Peskine449bd832023-01-11 14:50:10 +0100354 transcript, sizeof(transcript), &transcript_len);
355 if (ret != 0) {
356 return ret;
357 }
Jerry Yu1c105562022-07-10 06:32:38 +0000358
Gilles Peskine449bd832023-01-11 14:50:10 +0100359 ret = mbedtls_ssl_tls13_export_handshake_psk(ssl, &psk, &psk_len);
360 if (ret != 0) {
361 return ret;
362 }
Jerry Yu96a2e362022-07-21 15:11:34 +0800363
Gilles Peskine449bd832023-01-11 14:50:10 +0100364 ret = mbedtls_ssl_tls13_create_psk_binder(ssl, psk_hash_alg,
365 psk, psk_len, psk_type,
366 transcript,
367 server_computed_binder);
Jerry Yu96a2e362022-07-21 15:11:34 +0800368#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +0100369 mbedtls_free((void *) psk);
Jerry Yu96a2e362022-07-21 15:11:34 +0800370#endif
Gilles Peskine449bd832023-01-11 14:50:10 +0100371 if (ret != 0) {
372 MBEDTLS_SSL_DEBUG_MSG(1, ("PSK binder calculation failed."));
373 return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
Jerry Yu1c105562022-07-10 06:32:38 +0000374 }
375
Gilles Peskine449bd832023-01-11 14:50:10 +0100376 MBEDTLS_SSL_DEBUG_BUF(3, "psk binder ( computed ): ",
377 server_computed_binder, transcript_len);
378 MBEDTLS_SSL_DEBUG_BUF(3, "psk binder ( received ): ", binder, binder_len);
Jerry Yu1c105562022-07-10 06:32:38 +0000379
Gilles Peskine449bd832023-01-11 14:50:10 +0100380 if (mbedtls_ct_memcmp(server_computed_binder, binder, binder_len) == 0) {
Ronald Cron6e311272023-12-05 17:57:01 +0100381 return SSL_TLS1_3_BINDER_MATCH;
Jerry Yu1c105562022-07-10 06:32:38 +0000382 }
383
Gilles Peskine449bd832023-01-11 14:50:10 +0100384 mbedtls_platform_zeroize(server_computed_binder,
385 sizeof(server_computed_binder));
Ronald Cron6e311272023-12-05 17:57:01 +0100386 return SSL_TLS1_3_BINDER_DOES_NOT_MATCH;
Jerry Yu1c105562022-07-10 06:32:38 +0000387}
Jerry Yu96a2e362022-07-21 15:11:34 +0800388
Jerry Yu5725f1c2022-08-21 17:27:16 +0800389MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yuf35ba382022-08-23 17:58:26 +0800390static int ssl_tls13_select_ciphersuite_for_psk(
Gilles Peskine449bd832023-01-11 14:50:10 +0100391 mbedtls_ssl_context *ssl,
392 const unsigned char *cipher_suites,
393 const unsigned char *cipher_suites_end,
394 uint16_t *selected_ciphersuite,
395 const mbedtls_ssl_ciphersuite_t **selected_ciphersuite_info)
Jerry Yu5725f1c2022-08-21 17:27:16 +0800396{
Jerry Yuf35ba382022-08-23 17:58:26 +0800397 psa_algorithm_t psk_hash_alg = PSA_ALG_SHA_256;
Jerry Yu5725f1c2022-08-21 17:27:16 +0800398
Jerry Yu0baf9072022-08-25 11:21:04 +0800399 *selected_ciphersuite = 0;
400 *selected_ciphersuite_info = NULL;
401
Jerry Yuf35ba382022-08-23 17:58:26 +0800402 /* RFC 8446, page 55.
403 *
404 * For externally established PSKs, the Hash algorithm MUST be set when the
405 * PSK is established or default to SHA-256 if no such algorithm is defined.
406 *
407 */
Jerry Yu5725f1c2022-08-21 17:27:16 +0800408
Jerry Yu5725f1c2022-08-21 17:27:16 +0800409 /*
410 * Search for a matching ciphersuite
411 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100412 for (const unsigned char *p = cipher_suites;
413 p < cipher_suites_end; p += 2) {
Jerry Yu5725f1c2022-08-21 17:27:16 +0800414 uint16_t cipher_suite;
Jerry Yuf35ba382022-08-23 17:58:26 +0800415 const mbedtls_ssl_ciphersuite_t *ciphersuite_info;
Jerry Yu5725f1c2022-08-21 17:27:16 +0800416
Gilles Peskine449bd832023-01-11 14:50:10 +0100417 cipher_suite = MBEDTLS_GET_UINT16_BE(p, 0);
418 ciphersuite_info = ssl_tls13_validate_peer_ciphersuite(ssl,
419 cipher_suite);
420 if (ciphersuite_info == NULL) {
Jerry Yu5725f1c2022-08-21 17:27:16 +0800421 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +0100422 }
Jerry Yu5725f1c2022-08-21 17:27:16 +0800423
Jerry Yu5725f1c2022-08-21 17:27:16 +0800424 /* MAC of selected ciphersuite MUST be same with PSK binder if exist.
425 * Otherwise, client should reject.
426 */
Dave Rodgman2eab4622023-10-05 13:30:37 +0100427 if (psk_hash_alg ==
428 mbedtls_md_psa_alg_from_type((mbedtls_md_type_t) ciphersuite_info->mac)) {
Jerry Yuf35ba382022-08-23 17:58:26 +0800429 *selected_ciphersuite = cipher_suite;
430 *selected_ciphersuite_info = ciphersuite_info;
Gilles Peskine449bd832023-01-11 14:50:10 +0100431 return 0;
Jerry Yuf35ba382022-08-23 17:58:26 +0800432 }
433 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100434 MBEDTLS_SSL_DEBUG_MSG(2, ("No matched ciphersuite"));
435 return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
Jerry Yuf35ba382022-08-23 17:58:26 +0800436}
Jerry Yu5725f1c2022-08-21 17:27:16 +0800437
Jerry Yu82534862022-08-30 10:42:33 +0800438#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Jerry Yuf35ba382022-08-23 17:58:26 +0800439MBEDTLS_CHECK_RETURN_CRITICAL
440static int ssl_tls13_select_ciphersuite_for_resumption(
Gilles Peskine449bd832023-01-11 14:50:10 +0100441 mbedtls_ssl_context *ssl,
442 const unsigned char *cipher_suites,
443 const unsigned char *cipher_suites_end,
444 mbedtls_ssl_session *session,
445 uint16_t *selected_ciphersuite,
446 const mbedtls_ssl_ciphersuite_t **selected_ciphersuite_info)
Jerry Yuf35ba382022-08-23 17:58:26 +0800447{
Jerry Yu82534862022-08-30 10:42:33 +0800448
Jerry Yuf35ba382022-08-23 17:58:26 +0800449 *selected_ciphersuite = 0;
450 *selected_ciphersuite_info = NULL;
Gilles Peskine449bd832023-01-11 14:50:10 +0100451 for (const unsigned char *p = cipher_suites; p < cipher_suites_end; p += 2) {
452 uint16_t cipher_suite = MBEDTLS_GET_UINT16_BE(p, 0);
Jerry Yu82534862022-08-30 10:42:33 +0800453 const mbedtls_ssl_ciphersuite_t *ciphersuite_info;
454
Gilles Peskine449bd832023-01-11 14:50:10 +0100455 if (cipher_suite != session->ciphersuite) {
Jerry Yu82534862022-08-30 10:42:33 +0800456 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +0100457 }
Jerry Yu82534862022-08-30 10:42:33 +0800458
Gilles Peskine449bd832023-01-11 14:50:10 +0100459 ciphersuite_info = ssl_tls13_validate_peer_ciphersuite(ssl,
460 cipher_suite);
461 if (ciphersuite_info == NULL) {
Jerry Yu82534862022-08-30 10:42:33 +0800462 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +0100463 }
Jerry Yu82534862022-08-30 10:42:33 +0800464
Jerry Yu4746b102022-09-13 11:11:48 +0800465 *selected_ciphersuite = cipher_suite;
Jerry Yu82534862022-08-30 10:42:33 +0800466 *selected_ciphersuite_info = ciphersuite_info;
467
Gilles Peskine449bd832023-01-11 14:50:10 +0100468 return 0;
Jerry Yu82534862022-08-30 10:42:33 +0800469 }
470
Gilles Peskine449bd832023-01-11 14:50:10 +0100471 return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
Jerry Yu5725f1c2022-08-21 17:27:16 +0800472}
Jerry Yuf35ba382022-08-23 17:58:26 +0800473
Jerry Yu82534862022-08-30 10:42:33 +0800474MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100475static int ssl_tls13_session_copy_ticket(mbedtls_ssl_session *dst,
476 const mbedtls_ssl_session *src)
Jerry Yu82534862022-08-30 10:42:33 +0800477{
Jerry Yu82534862022-08-30 10:42:33 +0800478 dst->ticket_age_add = src->ticket_age_add;
479 dst->ticket_flags = src->ticket_flags;
480 dst->resumption_key_len = src->resumption_key_len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100481 if (src->resumption_key_len == 0) {
482 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
483 }
484 memcpy(dst->resumption_key, src->resumption_key, src->resumption_key_len);
Jerry Yu4746b102022-09-13 11:11:48 +0800485
Jerry Yu33bf2402022-12-12 16:01:43 +0800486#if defined(MBEDTLS_SSL_EARLY_DATA)
487 dst->max_early_data_size = src->max_early_data_size;
488#endif
489
Gilles Peskine449bd832023-01-11 14:50:10 +0100490 return 0;
Jerry Yu82534862022-08-30 10:42:33 +0800491}
492#endif /* MBEDTLS_SSL_SESSION_TICKETS */
493
Jerry Yu1c105562022-07-10 06:32:38 +0000494/* Parser for pre_shared_key extension in client hello
495 * struct {
496 * opaque identity<1..2^16-1>;
497 * uint32 obfuscated_ticket_age;
498 * } PskIdentity;
499 *
500 * opaque PskBinderEntry<32..255>;
501 *
502 * struct {
503 * PskIdentity identities<7..2^16-1>;
504 * PskBinderEntry binders<33..2^16-1>;
505 * } OfferedPsks;
506 *
507 * struct {
508 * select (Handshake.msg_type) {
509 * case client_hello: OfferedPsks;
510 * ....
511 * };
512 * } PreSharedKeyExtension;
513 */
Jerry Yu6e74a7e2022-07-20 20:49:32 +0800514MBEDTLS_CHECK_RETURN_CRITICAL
Xiaokang Qian9f1747b2023-03-30 02:50:04 +0000515static int ssl_tls13_parse_pre_shared_key_ext(
516 mbedtls_ssl_context *ssl,
517 const unsigned char *pre_shared_key_ext,
518 const unsigned char *pre_shared_key_ext_end,
519 const unsigned char *ciphersuites,
520 const unsigned char *ciphersuites_end)
Jerry Yu1c105562022-07-10 06:32:38 +0000521{
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +0100522 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yu29d9faa2022-08-23 17:52:45 +0800523 const unsigned char *identities = pre_shared_key_ext;
Jerry Yu568ec252022-07-22 21:27:34 +0800524 const unsigned char *p_identity_len;
525 size_t identities_len;
Jerry Yu1c105562022-07-10 06:32:38 +0000526 const unsigned char *identities_end;
Jerry Yu568ec252022-07-22 21:27:34 +0800527 const unsigned char *binders;
528 const unsigned char *p_binder_len;
529 size_t binders_len;
Jerry Yu1c105562022-07-10 06:32:38 +0000530 const unsigned char *binders_end;
Jerry Yu96a2e362022-07-21 15:11:34 +0800531 int matched_identity = -1;
532 int identity_id = -1;
Jerry Yu1c105562022-07-10 06:32:38 +0000533
Gilles Peskine449bd832023-01-11 14:50:10 +0100534 MBEDTLS_SSL_DEBUG_BUF(3, "pre_shared_key extension",
535 pre_shared_key_ext,
536 pre_shared_key_ext_end - pre_shared_key_ext);
Jerry Yu1c105562022-07-10 06:32:38 +0000537
Jerry Yu96a2e362022-07-21 15:11:34 +0800538 /* identities_len 2 bytes
539 * identities_data >= 7 bytes
540 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100541 MBEDTLS_SSL_CHK_BUF_READ_PTR(identities, pre_shared_key_ext_end, 7 + 2);
542 identities_len = MBEDTLS_GET_UINT16_BE(identities, 0);
Jerry Yu568ec252022-07-22 21:27:34 +0800543 p_identity_len = identities + 2;
Gilles Peskine449bd832023-01-11 14:50:10 +0100544 MBEDTLS_SSL_CHK_BUF_READ_PTR(p_identity_len, pre_shared_key_ext_end,
545 identities_len);
Jerry Yu568ec252022-07-22 21:27:34 +0800546 identities_end = p_identity_len + identities_len;
Jerry Yu1c105562022-07-10 06:32:38 +0000547
Jerry Yu96a2e362022-07-21 15:11:34 +0800548 /* binders_len 2 bytes
549 * binders >= 33 bytes
550 */
Jerry Yu568ec252022-07-22 21:27:34 +0800551 binders = identities_end;
Gilles Peskine449bd832023-01-11 14:50:10 +0100552 MBEDTLS_SSL_CHK_BUF_READ_PTR(binders, pre_shared_key_ext_end, 33 + 2);
553 binders_len = MBEDTLS_GET_UINT16_BE(binders, 0);
Jerry Yu568ec252022-07-22 21:27:34 +0800554 p_binder_len = binders + 2;
Gilles Peskine449bd832023-01-11 14:50:10 +0100555 MBEDTLS_SSL_CHK_BUF_READ_PTR(p_binder_len, pre_shared_key_ext_end, binders_len);
Jerry Yu568ec252022-07-22 21:27:34 +0800556 binders_end = p_binder_len + binders_len;
Jerry Yu1c105562022-07-10 06:32:38 +0000557
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +0100558 ret = ssl->handshake->update_checksum(ssl, pre_shared_key_ext,
559 identities_end - pre_shared_key_ext);
560 if (0 != ret) {
561 MBEDTLS_SSL_DEBUG_RET(1, ("update_checksum"), ret);
562 return ret;
563 }
Jerry Yu96a2e362022-07-21 15:11:34 +0800564
Gilles Peskine449bd832023-01-11 14:50:10 +0100565 while (p_identity_len < identities_end && p_binder_len < binders_end) {
Jerry Yu1c105562022-07-10 06:32:38 +0000566 const unsigned char *identity;
Jerry Yu568ec252022-07-22 21:27:34 +0800567 size_t identity_len;
Jerry Yu82534862022-08-30 10:42:33 +0800568 uint32_t obfuscated_ticket_age;
Jerry Yu96a2e362022-07-21 15:11:34 +0800569 const unsigned char *binder;
Jerry Yu568ec252022-07-22 21:27:34 +0800570 size_t binder_len;
Jerry Yu5725f1c2022-08-21 17:27:16 +0800571 int psk_type;
572 uint16_t cipher_suite;
Jerry Yu29d9faa2022-08-23 17:52:45 +0800573 const mbedtls_ssl_ciphersuite_t *ciphersuite_info;
Jerry Yu82534862022-08-30 10:42:33 +0800574#if defined(MBEDTLS_SSL_SESSION_TICKETS)
575 mbedtls_ssl_session session;
Gilles Peskine449bd832023-01-11 14:50:10 +0100576 mbedtls_ssl_session_init(&session);
Jerry Yu82534862022-08-30 10:42:33 +0800577#endif
Jerry Yubb852022022-07-20 21:10:44 +0800578
Gilles Peskine449bd832023-01-11 14:50:10 +0100579 MBEDTLS_SSL_CHK_BUF_READ_PTR(p_identity_len, identities_end, 2 + 1 + 4);
580 identity_len = MBEDTLS_GET_UINT16_BE(p_identity_len, 0);
Jerry Yu568ec252022-07-22 21:27:34 +0800581 identity = p_identity_len + 2;
Gilles Peskine449bd832023-01-11 14:50:10 +0100582 MBEDTLS_SSL_CHK_BUF_READ_PTR(identity, identities_end, identity_len + 4);
583 obfuscated_ticket_age = MBEDTLS_GET_UINT32_BE(identity, identity_len);
Jerry Yu568ec252022-07-22 21:27:34 +0800584 p_identity_len += identity_len + 6;
Jerry Yu1c105562022-07-10 06:32:38 +0000585
Gilles Peskine449bd832023-01-11 14:50:10 +0100586 MBEDTLS_SSL_CHK_BUF_READ_PTR(p_binder_len, binders_end, 1 + 32);
Jerry Yu568ec252022-07-22 21:27:34 +0800587 binder_len = *p_binder_len;
588 binder = p_binder_len + 1;
Gilles Peskine449bd832023-01-11 14:50:10 +0100589 MBEDTLS_SSL_CHK_BUF_READ_PTR(binder, binders_end, binder_len);
Jerry Yu568ec252022-07-22 21:27:34 +0800590 p_binder_len += binder_len + 1;
Jerry Yu96a2e362022-07-21 15:11:34 +0800591
Jerry Yu96a2e362022-07-21 15:11:34 +0800592 identity_id++;
Gilles Peskine449bd832023-01-11 14:50:10 +0100593 if (matched_identity != -1) {
Jerry Yu1c105562022-07-10 06:32:38 +0000594 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +0100595 }
Jerry Yu1c105562022-07-10 06:32:38 +0000596
Jerry Yu96a2e362022-07-21 15:11:34 +0800597 ret = ssl_tls13_offered_psks_check_identity_match(
Gilles Peskine449bd832023-01-11 14:50:10 +0100598 ssl, identity, identity_len, obfuscated_ticket_age,
599 &psk_type, &session);
Ronald Cronfbae94a2023-12-05 18:15:14 +0100600 if (ret != SSL_TLS1_3_PSK_IDENTITY_MATCH) {
Jerry Yu96a2e362022-07-21 15:11:34 +0800601 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +0100602 }
Jerry Yu96a2e362022-07-21 15:11:34 +0800603
Gilles Peskine449bd832023-01-11 14:50:10 +0100604 MBEDTLS_SSL_DEBUG_MSG(4, ("found matched identity"));
605 switch (psk_type) {
Jerry Yu0baf9072022-08-25 11:21:04 +0800606 case MBEDTLS_SSL_TLS1_3_PSK_EXTERNAL:
607 ret = ssl_tls13_select_ciphersuite_for_psk(
Gilles Peskine449bd832023-01-11 14:50:10 +0100608 ssl, ciphersuites, ciphersuites_end,
609 &cipher_suite, &ciphersuite_info);
Jerry Yu0baf9072022-08-25 11:21:04 +0800610 break;
Jerry Yu82534862022-08-30 10:42:33 +0800611#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Ronald Cronf7e99162024-02-14 16:04:02 +0100612 case MBEDTLS_SSL_TLS1_3_PSK_RESUMPTION:
Jerry Yu0baf9072022-08-25 11:21:04 +0800613 ret = ssl_tls13_select_ciphersuite_for_resumption(
Gilles Peskine449bd832023-01-11 14:50:10 +0100614 ssl, ciphersuites, ciphersuites_end, &session,
615 &cipher_suite, &ciphersuite_info);
616 if (ret != 0) {
617 mbedtls_ssl_session_free(&session);
618 }
Jerry Yu0baf9072022-08-25 11:21:04 +0800619 break;
Ronald Cronf7e99162024-02-14 16:04:02 +0100620#endif
Jerry Yu0baf9072022-08-25 11:21:04 +0800621 default:
Gilles Peskine449bd832023-01-11 14:50:10 +0100622 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Jerry Yu0baf9072022-08-25 11:21:04 +0800623 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100624 if (ret != 0) {
Jerry Yuf35ba382022-08-23 17:58:26 +0800625 /* See below, no cipher_suite available, abort handshake */
626 MBEDTLS_SSL_PEND_FATAL_ALERT(
627 MBEDTLS_SSL_ALERT_MSG_DECRYPT_ERROR,
Gilles Peskine449bd832023-01-11 14:50:10 +0100628 MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE);
Jerry Yuf35ba382022-08-23 17:58:26 +0800629 MBEDTLS_SSL_DEBUG_RET(
Gilles Peskine449bd832023-01-11 14:50:10 +0100630 2, "ssl_tls13_select_ciphersuite", ret);
631 return ret;
Jerry Yu5725f1c2022-08-21 17:27:16 +0800632 }
633
Jerry Yu96a2e362022-07-21 15:11:34 +0800634 ret = ssl_tls13_offered_psks_check_binder_match(
Gilles Peskine449bd832023-01-11 14:50:10 +0100635 ssl, binder, binder_len, psk_type,
Dave Rodgman2eab4622023-10-05 13:30:37 +0100636 mbedtls_md_psa_alg_from_type((mbedtls_md_type_t) ciphersuite_info->mac));
Ronald Cron6e311272023-12-05 17:57:01 +0100637 if (ret != SSL_TLS1_3_BINDER_MATCH) {
Jerry Yuc5a23a02022-08-25 10:51:44 +0800638 /* For security reasons, the handshake should be aborted when we
639 * fail to validate a binder value. See RFC 8446 section 4.2.11.2
640 * and appendix E.6. */
Jerry Yu82534862022-08-30 10:42:33 +0800641#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Gilles Peskine449bd832023-01-11 14:50:10 +0100642 mbedtls_ssl_session_free(&session);
Jerry Yu82534862022-08-30 10:42:33 +0800643#endif
Gilles Peskine449bd832023-01-11 14:50:10 +0100644 MBEDTLS_SSL_DEBUG_MSG(3, ("Invalid binder."));
Xiaokang Qian9f1747b2023-03-30 02:50:04 +0000645 MBEDTLS_SSL_DEBUG_RET(
646 1, "ssl_tls13_offered_psks_check_binder_match", ret);
Jerry Yu96a2e362022-07-21 15:11:34 +0800647 MBEDTLS_SSL_PEND_FATAL_ALERT(
648 MBEDTLS_SSL_ALERT_MSG_DECRYPT_ERROR,
Gilles Peskine449bd832023-01-11 14:50:10 +0100649 MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE);
650 return ret;
Jerry Yu96a2e362022-07-21 15:11:34 +0800651 }
Jerry Yu96a2e362022-07-21 15:11:34 +0800652
653 matched_identity = identity_id;
Jerry Yu5725f1c2022-08-21 17:27:16 +0800654
655 /* Update handshake parameters */
Jerry Yue5834fd2022-08-29 20:16:09 +0800656 ssl->handshake->ciphersuite_info = ciphersuite_info;
Jerry Yu4746b102022-09-13 11:11:48 +0800657 ssl->session_negotiate->ciphersuite = cipher_suite;
Gilles Peskine449bd832023-01-11 14:50:10 +0100658 MBEDTLS_SSL_DEBUG_MSG(2, ("overwrite ciphersuite: %04x - %s",
659 cipher_suite, ciphersuite_info->name));
Jerry Yu82534862022-08-30 10:42:33 +0800660#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Gilles Peskine449bd832023-01-11 14:50:10 +0100661 if (psk_type == MBEDTLS_SSL_TLS1_3_PSK_RESUMPTION) {
662 ret = ssl_tls13_session_copy_ticket(ssl->session_negotiate,
663 &session);
664 mbedtls_ssl_session_free(&session);
665 if (ret != 0) {
666 return ret;
667 }
Jerry Yu82534862022-08-30 10:42:33 +0800668 }
669#endif /* MBEDTLS_SSL_SESSION_TICKETS */
Jerry Yu1c105562022-07-10 06:32:38 +0000670 }
671
Gilles Peskine449bd832023-01-11 14:50:10 +0100672 if (p_identity_len != identities_end || p_binder_len != binders_end) {
673 MBEDTLS_SSL_DEBUG_MSG(3, ("pre_shared_key extension decode error"));
674 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR,
675 MBEDTLS_ERR_SSL_DECODE_ERROR);
676 return MBEDTLS_ERR_SSL_DECODE_ERROR;
Jerry Yu1c105562022-07-10 06:32:38 +0000677 }
678
Jerry Yu6f1db3f2022-07-22 23:05:59 +0800679 /* Update the handshake transcript with the binder list. */
Xiaokang Qian9f1747b2023-03-30 02:50:04 +0000680 ret = ssl->handshake->update_checksum(
681 ssl, identities_end, (size_t) (binders_end - identities_end));
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +0100682 if (0 != ret) {
683 MBEDTLS_SSL_DEBUG_RET(1, ("update_checksum"), ret);
684 return ret;
685 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100686 if (matched_identity == -1) {
687 MBEDTLS_SSL_DEBUG_MSG(3, ("No matched PSK or ticket."));
688 return MBEDTLS_ERR_SSL_UNKNOWN_IDENTITY;
Jerry Yu1c105562022-07-10 06:32:38 +0000689 }
690
Gilles Peskine449bd832023-01-11 14:50:10 +0100691 ssl->handshake->selected_identity = (uint16_t) matched_identity;
692 MBEDTLS_SSL_DEBUG_MSG(3, ("Pre shared key found"));
Jerry Yu1c105562022-07-10 06:32:38 +0000693
Gilles Peskine449bd832023-01-11 14:50:10 +0100694 return 0;
Jerry Yu1c105562022-07-10 06:32:38 +0000695}
Jerry Yu032b15ce2022-07-11 06:10:03 +0000696
697/*
698 * struct {
699 * select ( Handshake.msg_type ) {
700 * ....
701 * case server_hello:
702 * uint16 selected_identity;
703 * }
704 * } PreSharedKeyExtension;
705 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100706static int ssl_tls13_write_server_pre_shared_key_ext(mbedtls_ssl_context *ssl,
707 unsigned char *buf,
708 unsigned char *end,
709 size_t *olen)
Jerry Yu032b15ce2022-07-11 06:10:03 +0000710{
Gilles Peskine449bd832023-01-11 14:50:10 +0100711 unsigned char *p = (unsigned char *) buf;
Jerry Yu032b15ce2022-07-11 06:10:03 +0000712
713 *olen = 0;
714
David Horstmann21b89762022-10-06 18:34:28 +0100715 int not_using_psk = 0;
Jerry Yu032b15ce2022-07-11 06:10:03 +0000716#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +0100717 not_using_psk = (mbedtls_svc_key_id_is_null(ssl->handshake->psk_opaque));
Jerry Yu032b15ce2022-07-11 06:10:03 +0000718#else
Gilles Peskine449bd832023-01-11 14:50:10 +0100719 not_using_psk = (ssl->handshake->psk == NULL);
Jerry Yu032b15ce2022-07-11 06:10:03 +0000720#endif
Gilles Peskine449bd832023-01-11 14:50:10 +0100721 if (not_using_psk) {
Jerry Yu032b15ce2022-07-11 06:10:03 +0000722 /* We shouldn't have called this extension writer unless we've
723 * chosen to use a PSK. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100724 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Jerry Yu032b15ce2022-07-11 06:10:03 +0000725 }
726
Gilles Peskine449bd832023-01-11 14:50:10 +0100727 MBEDTLS_SSL_DEBUG_MSG(3, ("server hello, adding pre_shared_key extension"));
728 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 6);
Jerry Yu032b15ce2022-07-11 06:10:03 +0000729
Gilles Peskine449bd832023-01-11 14:50:10 +0100730 MBEDTLS_PUT_UINT16_BE(MBEDTLS_TLS_EXT_PRE_SHARED_KEY, p, 0);
731 MBEDTLS_PUT_UINT16_BE(2, p, 2);
Jerry Yu032b15ce2022-07-11 06:10:03 +0000732
Gilles Peskine449bd832023-01-11 14:50:10 +0100733 MBEDTLS_PUT_UINT16_BE(ssl->handshake->selected_identity, p, 4);
Jerry Yu032b15ce2022-07-11 06:10:03 +0000734
735 *olen = 6;
736
Gilles Peskine449bd832023-01-11 14:50:10 +0100737 MBEDTLS_SSL_DEBUG_MSG(4, ("sent selected_identity: %u",
738 ssl->handshake->selected_identity));
Jerry Yu032b15ce2022-07-11 06:10:03 +0000739
Gilles Peskine449bd832023-01-11 14:50:10 +0100740 mbedtls_ssl_tls13_set_hs_sent_ext_mask(ssl, MBEDTLS_TLS_EXT_PRE_SHARED_KEY);
Jerry Yub95dd362022-11-08 21:19:34 +0800741
Gilles Peskine449bd832023-01-11 14:50:10 +0100742 return 0;
Jerry Yu032b15ce2022-07-11 06:10:03 +0000743}
744
Ronald Cron41a443a2022-10-04 16:38:25 +0200745#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED */
Jerry Yue19e3b92022-07-08 12:04:51 +0000746
XiaokangQian7807f9f2022-02-15 10:04:37 +0000747/* From RFC 8446:
748 * struct {
XiaokangQiancfd925f2022-04-14 07:10:37 +0000749 * ProtocolVersion versions<2..254>;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000750 * } SupportedVersions;
751 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200752MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100753static int ssl_tls13_parse_supported_versions_ext(mbedtls_ssl_context *ssl,
754 const unsigned char *buf,
755 const unsigned char *end)
XiaokangQian7807f9f2022-02-15 10:04:37 +0000756{
XiaokangQian7807f9f2022-02-15 10:04:37 +0000757 const unsigned char *p = buf;
XiaokangQian8f9dfe42022-04-15 02:52:39 +0000758 size_t versions_len;
XiaokangQian4080a7f2022-04-11 09:55:18 +0000759 const unsigned char *versions_end;
XiaokangQian0a1b54e2022-04-21 03:01:38 +0000760 uint16_t tls_version;
Ronald Cron5af4c7f2023-03-07 20:46:59 +0100761 int found_supported_version = 0;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000762
Gilles Peskine449bd832023-01-11 14:50:10 +0100763 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, 1);
XiaokangQian4080a7f2022-04-11 09:55:18 +0000764 versions_len = p[0];
XiaokangQian7807f9f2022-02-15 10:04:37 +0000765 p += 1;
766
Gilles Peskine449bd832023-01-11 14:50:10 +0100767 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, versions_len);
XiaokangQian4080a7f2022-04-11 09:55:18 +0000768 versions_end = p + versions_len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100769 while (p < versions_end) {
770 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, versions_end, 2);
771 tls_version = mbedtls_ssl_read_version(p, ssl->conf->transport);
XiaokangQianb67384d2022-04-19 00:02:38 +0000772 p += 2;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000773
Ronald Cron3bd2b022023-04-03 16:45:39 +0200774 if (MBEDTLS_SSL_VERSION_TLS1_3 == tls_version) {
Ronald Cron5af4c7f2023-03-07 20:46:59 +0100775 found_supported_version = 1;
776 break;
777 }
778
Ronald Cron3bd2b022023-04-03 16:45:39 +0200779 if ((MBEDTLS_SSL_VERSION_TLS1_2 == tls_version) &&
780 mbedtls_ssl_conf_is_tls12_enabled(ssl->conf)) {
Ronald Cron5af4c7f2023-03-07 20:46:59 +0100781 found_supported_version = 1;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000782 break;
783 }
XiaokangQian7807f9f2022-02-15 10:04:37 +0000784 }
785
Ronald Cron5af4c7f2023-03-07 20:46:59 +0100786 if (!found_supported_version) {
787 MBEDTLS_SSL_DEBUG_MSG(1, ("No supported version found."));
XiaokangQian7807f9f2022-02-15 10:04:37 +0000788
Gilles Peskine449bd832023-01-11 14:50:10 +0100789 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_PROTOCOL_VERSION,
790 MBEDTLS_ERR_SSL_BAD_PROTOCOL_VERSION);
791 return MBEDTLS_ERR_SSL_BAD_PROTOCOL_VERSION;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000792 }
793
Ronald Cron5af4c7f2023-03-07 20:46:59 +0100794 MBEDTLS_SSL_DEBUG_MSG(1, ("Negotiated version: [%04x]",
Gilles Peskine449bd832023-01-11 14:50:10 +0100795 (unsigned int) tls_version));
XiaokangQian7807f9f2022-02-15 10:04:37 +0000796
Ronald Cron5af4c7f2023-03-07 20:46:59 +0100797 return (int) tls_version;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000798}
799
Przemek Stekiel8c0a9532023-06-15 16:48:19 +0200800#if defined(PSA_WANT_ALG_ECDH) || defined(PSA_WANT_ALG_FFDH)
XiaokangQiane8ff3502022-04-22 02:34:40 +0000801/*
XiaokangQian7807f9f2022-02-15 10:04:37 +0000802 *
803 * From RFC 8446:
804 * enum {
805 * ... (0xFFFF)
806 * } NamedGroup;
807 * struct {
808 * NamedGroup named_group_list<2..2^16-1>;
809 * } NamedGroupList;
810 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200811MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100812static int ssl_tls13_parse_supported_groups_ext(mbedtls_ssl_context *ssl,
813 const unsigned char *buf,
814 const unsigned char *end)
XiaokangQian7807f9f2022-02-15 10:04:37 +0000815{
XiaokangQian7807f9f2022-02-15 10:04:37 +0000816 const unsigned char *p = buf;
XiaokangQian84823772022-04-19 07:57:30 +0000817 size_t named_group_list_len;
XiaokangQian8f9dfe42022-04-15 02:52:39 +0000818 const unsigned char *named_group_list_end;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000819
Gilles Peskine449bd832023-01-11 14:50:10 +0100820 MBEDTLS_SSL_DEBUG_BUF(3, "supported_groups extension", p, end - buf);
821 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, 2);
822 named_group_list_len = MBEDTLS_GET_UINT16_BE(p, 0);
XiaokangQian7807f9f2022-02-15 10:04:37 +0000823 p += 2;
Gilles Peskine449bd832023-01-11 14:50:10 +0100824 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, named_group_list_len);
XiaokangQian8f9dfe42022-04-15 02:52:39 +0000825 named_group_list_end = p + named_group_list_len;
XiaokangQian4e8cd7b2022-04-21 09:48:09 +0000826 ssl->handshake->hrr_selected_group = 0;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000827
Gilles Peskine449bd832023-01-11 14:50:10 +0100828 while (p < named_group_list_end) {
XiaokangQian08037552022-04-20 07:16:41 +0000829 uint16_t named_group;
Gilles Peskine449bd832023-01-11 14:50:10 +0100830 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, named_group_list_end, 2);
831 named_group = MBEDTLS_GET_UINT16_BE(p, 0);
XiaokangQian08037552022-04-20 07:16:41 +0000832 p += 2;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000833
Gilles Peskine449bd832023-01-11 14:50:10 +0100834 MBEDTLS_SSL_DEBUG_MSG(2,
835 ("got named group: %s(%04x)",
836 mbedtls_ssl_named_group_to_str(named_group),
837 named_group));
XiaokangQian08037552022-04-20 07:16:41 +0000838
Gilles Peskine449bd832023-01-11 14:50:10 +0100839 if (!mbedtls_ssl_named_group_is_offered(ssl, named_group) ||
840 !mbedtls_ssl_named_group_is_supported(named_group) ||
841 ssl->handshake->hrr_selected_group != 0) {
XiaokangQian08037552022-04-20 07:16:41 +0000842 continue;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000843 }
844
Gilles Peskine449bd832023-01-11 14:50:10 +0100845 MBEDTLS_SSL_DEBUG_MSG(2,
846 ("add named group %s(%04x) into received list.",
847 mbedtls_ssl_named_group_to_str(named_group),
848 named_group));
Jerry Yuc1be19f2022-04-23 16:11:39 +0800849
XiaokangQian4e8cd7b2022-04-21 09:48:09 +0000850 ssl->handshake->hrr_selected_group = named_group;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000851 }
852
Gilles Peskine449bd832023-01-11 14:50:10 +0100853 return 0;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000854
855}
Przemek Stekiel8c0a9532023-06-15 16:48:19 +0200856#endif /* PSA_WANT_ALG_ECDH || PSA_WANT_ALG_FFDH */
XiaokangQian7807f9f2022-02-15 10:04:37 +0000857
XiaokangQian08037552022-04-20 07:16:41 +0000858#define SSL_TLS1_3_PARSE_KEY_SHARES_EXT_NO_MATCH 1
859
Valerio Settic9ae8622023-07-25 11:23:50 +0200860#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_EPHEMERAL_ENABLED)
XiaokangQian7807f9f2022-02-15 10:04:37 +0000861/*
862 * ssl_tls13_parse_key_shares_ext() verifies whether the information in the
Xiaokang Qian9f1747b2023-03-30 02:50:04 +0000863 * extension is correct and stores the first acceptable key share and its
864 * associated group.
XiaokangQian7807f9f2022-02-15 10:04:37 +0000865 *
866 * Possible return values are:
867 * - 0: Successful processing of the client provided key share extension.
Xiaokang Qian9f1747b2023-03-30 02:50:04 +0000868 * - SSL_TLS1_3_PARSE_KEY_SHARES_EXT_NO_MATCH: The key shares provided by
869 * the client does not match a group supported by the server. A
870 * HelloRetryRequest will be needed.
XiaokangQiane8ff3502022-04-22 02:34:40 +0000871 * - A negative value for fatal errors.
Jerry Yuc1be19f2022-04-23 16:11:39 +0800872 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200873MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100874static int ssl_tls13_parse_key_shares_ext(mbedtls_ssl_context *ssl,
875 const unsigned char *buf,
876 const unsigned char *end)
XiaokangQian7807f9f2022-02-15 10:04:37 +0000877{
XiaokangQianb67384d2022-04-19 00:02:38 +0000878 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000879 unsigned char const *p = buf;
XiaokangQian8f9dfe42022-04-15 02:52:39 +0000880 unsigned char const *client_shares_end;
Jerry Yuc1be19f2022-04-23 16:11:39 +0800881 size_t client_shares_len;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000882
883 /* From RFC 8446:
884 *
885 * struct {
886 * KeyShareEntry client_shares<0..2^16-1>;
887 * } KeyShareClientHello;
888 *
889 */
890
Gilles Peskine449bd832023-01-11 14:50:10 +0100891 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, 2);
892 client_shares_len = MBEDTLS_GET_UINT16_BE(p, 0);
XiaokangQian7807f9f2022-02-15 10:04:37 +0000893 p += 2;
Gilles Peskine449bd832023-01-11 14:50:10 +0100894 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, client_shares_len);
XiaokangQian7807f9f2022-02-15 10:04:37 +0000895
896 ssl->handshake->offered_group_id = 0;
XiaokangQian8f9dfe42022-04-15 02:52:39 +0000897 client_shares_end = p + client_shares_len;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000898
899 /* We try to find a suitable key share entry and copy it to the
900 * handshake context. Later, we have to find out whether we can do
901 * something with the provided key share or whether we have to
XiaokangQianc5763b52022-04-02 03:34:37 +0000902 * dismiss it and send a HelloRetryRequest message.
903 */
XiaokangQian7807f9f2022-02-15 10:04:37 +0000904
Gilles Peskine449bd832023-01-11 14:50:10 +0100905 while (p < client_shares_end) {
XiaokangQian9b5d04b2022-04-10 10:20:43 +0000906 uint16_t group;
Jerry Yuc1be19f2022-04-23 16:11:39 +0800907 size_t key_exchange_len;
Jerry Yu086edc22022-05-05 10:50:38 +0800908 const unsigned char *key_exchange;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000909
910 /*
911 * struct {
912 * NamedGroup group;
913 * opaque key_exchange<1..2^16-1>;
914 * } KeyShareEntry;
915 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100916 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, client_shares_end, 4);
917 group = MBEDTLS_GET_UINT16_BE(p, 0);
918 key_exchange_len = MBEDTLS_GET_UINT16_BE(p, 2);
Jerry Yuc1be19f2022-04-23 16:11:39 +0800919 p += 4;
Jerry Yu086edc22022-05-05 10:50:38 +0800920 key_exchange = p;
Gilles Peskine449bd832023-01-11 14:50:10 +0100921 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, client_shares_end, key_exchange_len);
Jerry Yu086edc22022-05-05 10:50:38 +0800922 p += key_exchange_len;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000923
924 /* Continue parsing even if we have already found a match,
XiaokangQianc5763b52022-04-02 03:34:37 +0000925 * for input validation purposes.
926 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100927 if (!mbedtls_ssl_named_group_is_offered(ssl, group) ||
928 !mbedtls_ssl_named_group_is_supported(group) ||
929 ssl->handshake->offered_group_id != 0) {
XiaokangQian060d8672022-04-21 09:24:56 +0000930 continue;
931 }
XiaokangQian060d8672022-04-21 09:24:56 +0000932
XiaokangQian7807f9f2022-02-15 10:04:37 +0000933 /*
Przemek Stekielc89f3ea2023-05-18 15:45:53 +0200934 * ECDHE and FFDHE groups are supported
XiaokangQian7807f9f2022-02-15 10:04:37 +0000935 */
Przemek Stekielc89f3ea2023-05-18 15:45:53 +0200936 if (mbedtls_ssl_tls13_named_group_is_ecdhe(group) ||
Przemek Stekield5f79e72023-06-29 09:08:43 +0200937 mbedtls_ssl_tls13_named_group_is_ffdh(group)) {
Przemek Stekielc89f3ea2023-05-18 15:45:53 +0200938 MBEDTLS_SSL_DEBUG_MSG(2, ("ECDH/FFDH group: %s (%04x)",
Gilles Peskine449bd832023-01-11 14:50:10 +0100939 mbedtls_ssl_named_group_to_str(group),
940 group));
Przemek Stekiel7ac93be2023-07-04 10:02:38 +0200941 ret = mbedtls_ssl_tls13_read_public_xxdhe_share(
Gilles Peskine449bd832023-01-11 14:50:10 +0100942 ssl, key_exchange - 2, key_exchange_len + 2);
943 if (ret != 0) {
944 return ret;
945 }
XiaokangQian4e8cd7b2022-04-21 09:48:09 +0000946
Gilles Peskine449bd832023-01-11 14:50:10 +0100947 } else {
948 MBEDTLS_SSL_DEBUG_MSG(4, ("Unrecognized NamedGroup %u",
949 (unsigned) group));
XiaokangQian7807f9f2022-02-15 10:04:37 +0000950 continue;
951 }
952
XiaokangQian9b5d04b2022-04-10 10:20:43 +0000953 ssl->handshake->offered_group_id = group;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000954 }
955
Jerry Yuc1be19f2022-04-23 16:11:39 +0800956
Gilles Peskine449bd832023-01-11 14:50:10 +0100957 if (ssl->handshake->offered_group_id == 0) {
958 MBEDTLS_SSL_DEBUG_MSG(1, ("no matching key share"));
959 return SSL_TLS1_3_PARSE_KEY_SHARES_EXT_NO_MATCH;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000960 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100961 return 0;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000962}
Valerio Settic9ae8622023-07-25 11:23:50 +0200963#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_EPHEMERAL_ENABLED */
XiaokangQian7807f9f2022-02-15 10:04:37 +0000964
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200965MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100966static int ssl_tls13_client_hello_has_exts(mbedtls_ssl_context *ssl,
967 int exts_mask)
XiaokangQian7807f9f2022-02-15 10:04:37 +0000968{
Jerry Yu0c354a22022-08-29 15:25:36 +0800969 int masked = ssl->handshake->received_extensions & exts_mask;
Gilles Peskine449bd832023-01-11 14:50:10 +0100970 return masked == exts_mask;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000971}
972
Jerry Yue5991322022-11-07 14:03:44 +0800973#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200974MBEDTLS_CHECK_RETURN_CRITICAL
XiaokangQianb67384d2022-04-19 00:02:38 +0000975static int ssl_tls13_client_hello_has_exts_for_ephemeral_key_exchange(
Gilles Peskine449bd832023-01-11 14:50:10 +0100976 mbedtls_ssl_context *ssl)
XiaokangQian7807f9f2022-02-15 10:04:37 +0000977{
Gilles Peskine449bd832023-01-11 14:50:10 +0100978 return ssl_tls13_client_hello_has_exts(
979 ssl,
980 MBEDTLS_SSL_EXT_MASK(SUPPORTED_GROUPS) |
981 MBEDTLS_SSL_EXT_MASK(KEY_SHARE) |
982 MBEDTLS_SSL_EXT_MASK(SIG_ALG));
XiaokangQian7807f9f2022-02-15 10:04:37 +0000983}
Jerry Yue5991322022-11-07 14:03:44 +0800984#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED */
XiaokangQian7807f9f2022-02-15 10:04:37 +0000985
Jerry Yue5991322022-11-07 14:03:44 +0800986#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK_ENABLED)
Jerry Yu77f01482022-07-11 07:03:24 +0000987MBEDTLS_CHECK_RETURN_CRITICAL
988static int ssl_tls13_client_hello_has_exts_for_psk_key_exchange(
Gilles Peskine449bd832023-01-11 14:50:10 +0100989 mbedtls_ssl_context *ssl)
Jerry Yu77f01482022-07-11 07:03:24 +0000990{
Gilles Peskine449bd832023-01-11 14:50:10 +0100991 return ssl_tls13_client_hello_has_exts(
992 ssl,
993 MBEDTLS_SSL_EXT_MASK(PRE_SHARED_KEY) |
994 MBEDTLS_SSL_EXT_MASK(PSK_KEY_EXCHANGE_MODES));
Jerry Yu77f01482022-07-11 07:03:24 +0000995}
Jerry Yue5991322022-11-07 14:03:44 +0800996#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK_ENABLED */
Jerry Yu77f01482022-07-11 07:03:24 +0000997
Jerry Yue5991322022-11-07 14:03:44 +0800998#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK_EPHEMERAL_ENABLED)
Jerry Yu77f01482022-07-11 07:03:24 +0000999MBEDTLS_CHECK_RETURN_CRITICAL
1000static int ssl_tls13_client_hello_has_exts_for_psk_ephemeral_key_exchange(
Gilles Peskine449bd832023-01-11 14:50:10 +01001001 mbedtls_ssl_context *ssl)
Jerry Yu77f01482022-07-11 07:03:24 +00001002{
Gilles Peskine449bd832023-01-11 14:50:10 +01001003 return ssl_tls13_client_hello_has_exts(
1004 ssl,
1005 MBEDTLS_SSL_EXT_MASK(SUPPORTED_GROUPS) |
1006 MBEDTLS_SSL_EXT_MASK(KEY_SHARE) |
1007 MBEDTLS_SSL_EXT_MASK(PRE_SHARED_KEY) |
1008 MBEDTLS_SSL_EXT_MASK(PSK_KEY_EXCHANGE_MODES));
Jerry Yu77f01482022-07-11 07:03:24 +00001009}
Jerry Yue5991322022-11-07 14:03:44 +08001010#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK_EPHEMERAL_ENABLED */
Jerry Yu77f01482022-07-11 07:03:24 +00001011
Pengyu Lv306a01d2023-02-02 16:35:47 +08001012#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED)
1013MBEDTLS_CHECK_RETURN_CRITICAL
Pengyu Lvd72e8582023-11-10 10:37:18 +08001014static int ssl_tls13_ticket_is_kex_mode_permitted(mbedtls_ssl_context *ssl,
1015 unsigned int kex_mode)
Pengyu Lv306a01d2023-02-02 16:35:47 +08001016{
1017#if defined(MBEDTLS_SSL_SESSION_TICKETS)
1018 if (ssl->handshake->resume) {
Pengyu Lv94a42cc2023-12-06 10:04:17 +08001019 if (!mbedtls_ssl_tls13_session_ticket_has_flags(
Pengyu Lv306a01d2023-02-02 16:35:47 +08001020 ssl->session_negotiate, kex_mode)) {
1021 return 0;
1022 }
1023 }
1024#else
1025 ((void) ssl);
1026 ((void) kex_mode);
1027#endif
1028 return 1;
1029}
1030#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED */
1031
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001032MBEDTLS_CHECK_RETURN_CRITICAL
Pengyu Lvbc4aab72023-12-01 15:37:24 +08001033static int ssl_tls13_key_exchange_is_ephemeral_available(mbedtls_ssl_context *ssl)
XiaokangQian7807f9f2022-02-15 10:04:37 +00001034{
Jerry Yue5991322022-11-07 14:03:44 +08001035#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED)
Pengyu Lv0a1ff2b2023-11-14 11:03:32 +08001036 return mbedtls_ssl_conf_tls13_is_ephemeral_enabled(ssl) &&
Gilles Peskine449bd832023-01-11 14:50:10 +01001037 ssl_tls13_client_hello_has_exts_for_ephemeral_key_exchange(ssl);
Jerry Yue5991322022-11-07 14:03:44 +08001038#else
1039 ((void) ssl);
Gilles Peskine449bd832023-01-11 14:50:10 +01001040 return 0;
Jerry Yue5991322022-11-07 14:03:44 +08001041#endif
Jerry Yu77f01482022-07-11 07:03:24 +00001042}
XiaokangQian7807f9f2022-02-15 10:04:37 +00001043
Jerry Yu77f01482022-07-11 07:03:24 +00001044MBEDTLS_CHECK_RETURN_CRITICAL
Pengyu Lvbc4aab72023-12-01 15:37:24 +08001045static int ssl_tls13_key_exchange_is_psk_available(mbedtls_ssl_context *ssl)
Jerry Yu77f01482022-07-11 07:03:24 +00001046{
Jerry Yue5991322022-11-07 14:03:44 +08001047#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK_ENABLED)
Pengyu Lvd72e8582023-11-10 10:37:18 +08001048 return ssl_tls13_ticket_is_kex_mode_permitted(
Pengyu Lv306a01d2023-02-02 16:35:47 +08001049 ssl, MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK) &&
Pengyu Lv0a1ff2b2023-11-14 11:03:32 +08001050 mbedtls_ssl_conf_tls13_is_psk_enabled(ssl) &&
Pengyu Lvb2cfafb2023-11-14 13:56:13 +08001051 mbedtls_ssl_tls13_is_psk_supported(ssl) &&
Gilles Peskine449bd832023-01-11 14:50:10 +01001052 ssl_tls13_client_hello_has_exts_for_psk_key_exchange(ssl);
Jerry Yu77f01482022-07-11 07:03:24 +00001053#else
1054 ((void) ssl);
Gilles Peskine449bd832023-01-11 14:50:10 +01001055 return 0;
Jerry Yu77f01482022-07-11 07:03:24 +00001056#endif
1057}
XiaokangQian7807f9f2022-02-15 10:04:37 +00001058
Jerry Yu77f01482022-07-11 07:03:24 +00001059MBEDTLS_CHECK_RETURN_CRITICAL
Pengyu Lvbc4aab72023-12-01 15:37:24 +08001060static int ssl_tls13_key_exchange_is_psk_ephemeral_available(mbedtls_ssl_context *ssl)
Jerry Yu77f01482022-07-11 07:03:24 +00001061{
Jerry Yue5991322022-11-07 14:03:44 +08001062#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK_EPHEMERAL_ENABLED)
Pengyu Lvd72e8582023-11-10 10:37:18 +08001063 return ssl_tls13_ticket_is_kex_mode_permitted(
Pengyu Lv306a01d2023-02-02 16:35:47 +08001064 ssl, MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK_EPHEMERAL) &&
Pengyu Lv0a1ff2b2023-11-14 11:03:32 +08001065 mbedtls_ssl_conf_tls13_is_psk_ephemeral_enabled(ssl) &&
Pengyu Lvb2cfafb2023-11-14 13:56:13 +08001066 mbedtls_ssl_tls13_is_psk_ephemeral_supported(ssl) &&
Gilles Peskine449bd832023-01-11 14:50:10 +01001067 ssl_tls13_client_hello_has_exts_for_psk_ephemeral_key_exchange(ssl);
Jerry Yu77f01482022-07-11 07:03:24 +00001068#else
1069 ((void) ssl);
Gilles Peskine449bd832023-01-11 14:50:10 +01001070 return 0;
Jerry Yu77f01482022-07-11 07:03:24 +00001071#endif
1072}
1073
Gilles Peskine449bd832023-01-11 14:50:10 +01001074static int ssl_tls13_determine_key_exchange_mode(mbedtls_ssl_context *ssl)
Jerry Yu77f01482022-07-11 07:03:24 +00001075{
1076 /*
1077 * Determine the key exchange algorithm to use.
1078 * There are three types of key exchanges supported in TLS 1.3:
1079 * - (EC)DH with ECDSA,
1080 * - (EC)DH with PSK,
1081 * - plain PSK.
1082 *
1083 * The PSK-based key exchanges may additionally be used with 0-RTT.
1084 *
1085 * Our built-in order of preference is
Jerry Yuce6ed702022-07-22 21:49:53 +08001086 * 1 ) (EC)DHE-PSK Mode ( psk_ephemeral )
1087 * 2 ) Certificate Mode ( ephemeral )
1088 * 3 ) Plain PSK Mode ( psk )
Jerry Yu77f01482022-07-11 07:03:24 +00001089 */
1090
Xiaokang Qian9f1747b2023-03-30 02:50:04 +00001091 ssl->handshake->key_exchange_mode =
1092 MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_NONE;
Jerry Yu77f01482022-07-11 07:03:24 +00001093
Pengyu Lvbc4aab72023-12-01 15:37:24 +08001094 if (ssl_tls13_key_exchange_is_psk_ephemeral_available(ssl)) {
Jerry Yu77f01482022-07-11 07:03:24 +00001095 ssl->handshake->key_exchange_mode =
1096 MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK_EPHEMERAL;
Gilles Peskine449bd832023-01-11 14:50:10 +01001097 MBEDTLS_SSL_DEBUG_MSG(2, ("key exchange mode: psk_ephemeral"));
1098 } else
Pengyu Lvbc4aab72023-12-01 15:37:24 +08001099 if (ssl_tls13_key_exchange_is_ephemeral_available(ssl)) {
Jerry Yu77f01482022-07-11 07:03:24 +00001100 ssl->handshake->key_exchange_mode =
1101 MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL;
Gilles Peskine449bd832023-01-11 14:50:10 +01001102 MBEDTLS_SSL_DEBUG_MSG(2, ("key exchange mode: ephemeral"));
1103 } else
Pengyu Lvbc4aab72023-12-01 15:37:24 +08001104 if (ssl_tls13_key_exchange_is_psk_available(ssl)) {
Jerry Yuce6ed702022-07-22 21:49:53 +08001105 ssl->handshake->key_exchange_mode =
1106 MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK;
Gilles Peskine449bd832023-01-11 14:50:10 +01001107 MBEDTLS_SSL_DEBUG_MSG(2, ("key exchange mode: psk"));
1108 } else {
Jerry Yu77f01482022-07-11 07:03:24 +00001109 MBEDTLS_SSL_DEBUG_MSG(
Gilles Peskine449bd832023-01-11 14:50:10 +01001110 1,
1111 ("ClientHello message misses mandatory extensions."));
1112 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_MISSING_EXTENSION,
1113 MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER);
1114 return MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER;
Jerry Yu77f01482022-07-11 07:03:24 +00001115 }
1116
Gilles Peskine449bd832023-01-11 14:50:10 +01001117 return 0;
Jerry Yu77f01482022-07-11 07:03:24 +00001118
XiaokangQian7807f9f2022-02-15 10:04:37 +00001119}
1120
XiaokangQian81802f42022-06-10 13:25:22 +00001121#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
Ronald Cron928cbd32022-10-04 16:14:26 +02001122 defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED)
Ronald Cron67ea2542022-09-15 17:34:42 +02001123
1124#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01001125static psa_algorithm_t ssl_tls13_iana_sig_alg_to_psa_alg(uint16_t sig_alg)
Ronald Cron67ea2542022-09-15 17:34:42 +02001126{
Gilles Peskine449bd832023-01-11 14:50:10 +01001127 switch (sig_alg) {
Ronald Cron67ea2542022-09-15 17:34:42 +02001128 case MBEDTLS_TLS1_3_SIG_ECDSA_SECP256R1_SHA256:
Gilles Peskine449bd832023-01-11 14:50:10 +01001129 return PSA_ALG_ECDSA(PSA_ALG_SHA_256);
Ronald Cron67ea2542022-09-15 17:34:42 +02001130 case MBEDTLS_TLS1_3_SIG_ECDSA_SECP384R1_SHA384:
Gilles Peskine449bd832023-01-11 14:50:10 +01001131 return PSA_ALG_ECDSA(PSA_ALG_SHA_384);
Ronald Cron67ea2542022-09-15 17:34:42 +02001132 case MBEDTLS_TLS1_3_SIG_ECDSA_SECP521R1_SHA512:
Gilles Peskine449bd832023-01-11 14:50:10 +01001133 return PSA_ALG_ECDSA(PSA_ALG_SHA_512);
Ronald Cron67ea2542022-09-15 17:34:42 +02001134 case MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA256:
Gilles Peskine449bd832023-01-11 14:50:10 +01001135 return PSA_ALG_RSA_PSS(PSA_ALG_SHA_256);
Ronald Cron67ea2542022-09-15 17:34:42 +02001136 case MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA384:
Gilles Peskine449bd832023-01-11 14:50:10 +01001137 return PSA_ALG_RSA_PSS(PSA_ALG_SHA_384);
Ronald Cron67ea2542022-09-15 17:34:42 +02001138 case MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA512:
Gilles Peskine449bd832023-01-11 14:50:10 +01001139 return PSA_ALG_RSA_PSS(PSA_ALG_SHA_512);
Ronald Cron67ea2542022-09-15 17:34:42 +02001140 case MBEDTLS_TLS1_3_SIG_RSA_PKCS1_SHA256:
Gilles Peskine449bd832023-01-11 14:50:10 +01001141 return PSA_ALG_RSA_PKCS1V15_SIGN(PSA_ALG_SHA_256);
Ronald Cron67ea2542022-09-15 17:34:42 +02001142 case MBEDTLS_TLS1_3_SIG_RSA_PKCS1_SHA384:
Gilles Peskine449bd832023-01-11 14:50:10 +01001143 return PSA_ALG_RSA_PKCS1V15_SIGN(PSA_ALG_SHA_384);
Ronald Cron67ea2542022-09-15 17:34:42 +02001144 case MBEDTLS_TLS1_3_SIG_RSA_PKCS1_SHA512:
Gilles Peskine449bd832023-01-11 14:50:10 +01001145 return PSA_ALG_RSA_PKCS1V15_SIGN(PSA_ALG_SHA_512);
Ronald Cron67ea2542022-09-15 17:34:42 +02001146 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01001147 return PSA_ALG_NONE;
Ronald Cron67ea2542022-09-15 17:34:42 +02001148 }
1149}
1150#endif /* MBEDTLS_USE_PSA_CRYPTO */
1151
XiaokangQian23c5be62022-06-07 02:04:34 +00001152/*
XiaokangQianfb665a82022-06-15 03:57:21 +00001153 * Pick best ( private key, certificate chain ) pair based on the signature
1154 * algorithms supported by the client.
XiaokangQian23c5be62022-06-07 02:04:34 +00001155 */
Ronald Cronce7d76e2022-07-08 18:56:49 +02001156MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01001157static int ssl_tls13_pick_key_cert(mbedtls_ssl_context *ssl)
XiaokangQian23c5be62022-06-07 02:04:34 +00001158{
XiaokangQianfb665a82022-06-15 03:57:21 +00001159 mbedtls_ssl_key_cert *key_cert, *key_cert_list;
XiaokangQian81802f42022-06-10 13:25:22 +00001160 const uint16_t *sig_alg = ssl->handshake->received_sig_algs;
XiaokangQian23c5be62022-06-07 02:04:34 +00001161
1162#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Gilles Peskine449bd832023-01-11 14:50:10 +01001163 if (ssl->handshake->sni_key_cert != NULL) {
XiaokangQianfb665a82022-06-15 03:57:21 +00001164 key_cert_list = ssl->handshake->sni_key_cert;
Gilles Peskine449bd832023-01-11 14:50:10 +01001165 } else
XiaokangQian23c5be62022-06-07 02:04:34 +00001166#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
Gilles Peskine449bd832023-01-11 14:50:10 +01001167 key_cert_list = ssl->conf->key_cert;
XiaokangQian23c5be62022-06-07 02:04:34 +00001168
Gilles Peskine449bd832023-01-11 14:50:10 +01001169 if (key_cert_list == NULL) {
1170 MBEDTLS_SSL_DEBUG_MSG(3, ("server has no certificate"));
1171 return -1;
XiaokangQian23c5be62022-06-07 02:04:34 +00001172 }
1173
Gilles Peskine449bd832023-01-11 14:50:10 +01001174 for (; *sig_alg != MBEDTLS_TLS1_3_SIG_NONE; sig_alg++) {
1175 if (!mbedtls_ssl_sig_alg_is_offered(ssl, *sig_alg)) {
Ronald Cron67ea2542022-09-15 17:34:42 +02001176 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +01001177 }
Ronald Cron67ea2542022-09-15 17:34:42 +02001178
Gilles Peskine449bd832023-01-11 14:50:10 +01001179 if (!mbedtls_ssl_tls13_sig_alg_for_cert_verify_is_supported(*sig_alg)) {
Ronald Cron67ea2542022-09-15 17:34:42 +02001180 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +01001181 }
Ronald Cron67ea2542022-09-15 17:34:42 +02001182
Gilles Peskine449bd832023-01-11 14:50:10 +01001183 for (key_cert = key_cert_list; key_cert != NULL;
1184 key_cert = key_cert->next) {
Ronald Cron67ea2542022-09-15 17:34:42 +02001185#if defined(MBEDTLS_USE_PSA_CRYPTO)
1186 psa_algorithm_t psa_alg = PSA_ALG_NONE;
1187#endif /* MBEDTLS_USE_PSA_CRYPTO */
1188
Gilles Peskine449bd832023-01-11 14:50:10 +01001189 MBEDTLS_SSL_DEBUG_CRT(3, "certificate (chain) candidate",
1190 key_cert->cert);
XiaokangQian81802f42022-06-10 13:25:22 +00001191
1192 /*
Gilles Peskine449bd832023-01-11 14:50:10 +01001193 * This avoids sending the client a cert it'll reject based on
1194 * keyUsage or other extensions.
1195 */
1196 if (mbedtls_x509_crt_check_key_usage(
1197 key_cert->cert, MBEDTLS_X509_KU_DIGITAL_SIGNATURE) != 0 ||
XiaokangQian81802f42022-06-10 13:25:22 +00001198 mbedtls_x509_crt_check_extended_key_usage(
XiaokangQianfb665a82022-06-15 03:57:21 +00001199 key_cert->cert, MBEDTLS_OID_SERVER_AUTH,
Gilles Peskine449bd832023-01-11 14:50:10 +01001200 MBEDTLS_OID_SIZE(MBEDTLS_OID_SERVER_AUTH)) != 0) {
1201 MBEDTLS_SSL_DEBUG_MSG(3, ("certificate mismatch: "
1202 "(extended) key usage extension"));
XiaokangQian81802f42022-06-10 13:25:22 +00001203 continue;
1204 }
XiaokangQian23c5be62022-06-07 02:04:34 +00001205
Gilles Peskine449bd832023-01-11 14:50:10 +01001206 MBEDTLS_SSL_DEBUG_MSG(3,
1207 ("ssl_tls13_pick_key_cert:"
1208 "check signature algorithm %s [%04x]",
1209 mbedtls_ssl_sig_alg_to_str(*sig_alg),
1210 *sig_alg));
Ronald Cron67ea2542022-09-15 17:34:42 +02001211#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01001212 psa_alg = ssl_tls13_iana_sig_alg_to_psa_alg(*sig_alg);
Ronald Cron67ea2542022-09-15 17:34:42 +02001213#endif /* MBEDTLS_USE_PSA_CRYPTO */
1214
Gilles Peskine449bd832023-01-11 14:50:10 +01001215 if (mbedtls_ssl_tls13_check_sig_alg_cert_key_match(
1216 *sig_alg, &key_cert->cert->pk)
Ronald Cron67ea2542022-09-15 17:34:42 +02001217#if defined(MBEDTLS_USE_PSA_CRYPTO)
1218 && psa_alg != PSA_ALG_NONE &&
Gilles Peskine449bd832023-01-11 14:50:10 +01001219 mbedtls_pk_can_do_ext(&key_cert->cert->pk, psa_alg,
1220 PSA_KEY_USAGE_SIGN_HASH) == 1
Ronald Cron67ea2542022-09-15 17:34:42 +02001221#endif /* MBEDTLS_USE_PSA_CRYPTO */
Gilles Peskine449bd832023-01-11 14:50:10 +01001222 ) {
XiaokangQianfb665a82022-06-15 03:57:21 +00001223 ssl->handshake->key_cert = key_cert;
Gilles Peskine449bd832023-01-11 14:50:10 +01001224 MBEDTLS_SSL_DEBUG_MSG(3,
1225 ("ssl_tls13_pick_key_cert:"
1226 "selected signature algorithm"
1227 " %s [%04x]",
1228 mbedtls_ssl_sig_alg_to_str(*sig_alg),
1229 *sig_alg));
XiaokangQianfb665a82022-06-15 03:57:21 +00001230 MBEDTLS_SSL_DEBUG_CRT(
Gilles Peskine449bd832023-01-11 14:50:10 +01001231 3, "selected certificate (chain)",
1232 ssl->handshake->key_cert->cert);
1233 return 0;
XiaokangQian81802f42022-06-10 13:25:22 +00001234 }
XiaokangQian81802f42022-06-10 13:25:22 +00001235 }
XiaokangQian23c5be62022-06-07 02:04:34 +00001236 }
1237
Gilles Peskine449bd832023-01-11 14:50:10 +01001238 MBEDTLS_SSL_DEBUG_MSG(2, ("ssl_tls13_pick_key_cert:"
1239 "no suitable certificate found"));
1240 return -1;
XiaokangQian23c5be62022-06-07 02:04:34 +00001241}
XiaokangQian81802f42022-06-10 13:25:22 +00001242#endif /* MBEDTLS_X509_CRT_PARSE_C &&
Ronald Cron928cbd32022-10-04 16:14:26 +02001243 MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED */
XiaokangQian23c5be62022-06-07 02:04:34 +00001244
XiaokangQian4080a7f2022-04-11 09:55:18 +00001245/*
XiaokangQianed582dd2022-04-13 08:21:05 +00001246 *
1247 * STATE HANDLING: ClientHello
1248 *
XiaokangQianb67384d2022-04-19 00:02:38 +00001249 * There are three possible classes of outcomes when parsing the ClientHello:
XiaokangQianed582dd2022-04-13 08:21:05 +00001250 *
XiaokangQianb67384d2022-04-19 00:02:38 +00001251 * 1) The ClientHello was well-formed and matched the server's configuration.
XiaokangQianed582dd2022-04-13 08:21:05 +00001252 *
1253 * In this case, the server progresses to sending its ServerHello.
1254 *
XiaokangQianb67384d2022-04-19 00:02:38 +00001255 * 2) The ClientHello was well-formed but didn't match the server's
1256 * configuration.
XiaokangQianed582dd2022-04-13 08:21:05 +00001257 *
1258 * For example, the client might not have offered a key share which
1259 * the server supports, or the server might require a cookie.
1260 *
1261 * In this case, the server sends a HelloRetryRequest.
1262 *
XiaokangQianb67384d2022-04-19 00:02:38 +00001263 * 3) The ClientHello was ill-formed
XiaokangQianed582dd2022-04-13 08:21:05 +00001264 *
1265 * In this case, we abort the handshake.
1266 *
1267 */
1268
1269/*
XiaokangQian4080a7f2022-04-11 09:55:18 +00001270 * Structure of this message:
1271 *
XiaokangQiane8ff3502022-04-22 02:34:40 +00001272 * uint16 ProtocolVersion;
1273 * opaque Random[32];
1274 * uint8 CipherSuite[2]; // Cryptographic suite selector
XiaokangQian4080a7f2022-04-11 09:55:18 +00001275 *
XiaokangQiane8ff3502022-04-22 02:34:40 +00001276 * struct {
1277 * ProtocolVersion legacy_version = 0x0303; // TLS v1.2
1278 * Random random;
1279 * opaque legacy_session_id<0..32>;
1280 * CipherSuite cipher_suites<2..2^16-2>;
1281 * opaque legacy_compression_methods<1..2^8-1>;
1282 * Extension extensions<8..2^16-1>;
1283 * } ClientHello;
XiaokangQian4080a7f2022-04-11 09:55:18 +00001284 */
XiaokangQianed582dd2022-04-13 08:21:05 +00001285
1286#define SSL_CLIENT_HELLO_OK 0
1287#define SSL_CLIENT_HELLO_HRR_REQUIRED 1
Ronald Cron5af4c7f2023-03-07 20:46:59 +01001288#define SSL_CLIENT_HELLO_TLS1_2 2
XiaokangQianed582dd2022-04-13 08:21:05 +00001289
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001290MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01001291static int ssl_tls13_parse_client_hello(mbedtls_ssl_context *ssl,
1292 const unsigned char *buf,
1293 const unsigned char *end)
XiaokangQian7807f9f2022-02-15 10:04:37 +00001294{
XiaokangQianb67384d2022-04-19 00:02:38 +00001295 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1296 const unsigned char *p = buf;
Ronald Crond540d992023-03-07 09:41:48 +01001297 const unsigned char *random;
XiaokangQian4080a7f2022-04-11 09:55:18 +00001298 size_t legacy_session_id_len;
Ronald Croncada4102023-03-07 09:51:39 +01001299 const unsigned char *legacy_session_id;
XiaokangQian318dc762022-04-20 09:43:51 +00001300 size_t cipher_suites_len;
Ronald Cron2f16b4e2023-03-07 10:07:32 +01001301 const unsigned char *cipher_suites;
XiaokangQian060d8672022-04-21 09:24:56 +00001302 const unsigned char *cipher_suites_end;
XiaokangQianb67384d2022-04-19 00:02:38 +00001303 size_t extensions_len;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001304 const unsigned char *extensions_end;
Ronald Croneff56732023-04-03 17:36:31 +02001305 const unsigned char *supported_versions_data;
1306 const unsigned char *supported_versions_data_end;
Jerry Yuc4bf5d62022-10-29 09:08:47 +08001307 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
Jerry Yu49ca9282022-05-05 11:05:22 +08001308 int hrr_required = 0;
Ronald Cron8a74f072023-06-14 17:59:29 +02001309 int no_usable_share_for_key_agreement = 0;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001310
Ronald Cron41a443a2022-10-04 16:38:25 +02001311#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED)
Jerry Yu29d9faa2022-08-23 17:52:45 +08001312 const unsigned char *pre_shared_key_ext = NULL;
Jerry Yu1c105562022-07-10 06:32:38 +00001313 const unsigned char *pre_shared_key_ext_end = NULL;
Ronald Cron41a443a2022-10-04 16:38:25 +02001314#endif
XiaokangQian7807f9f2022-02-15 10:04:37 +00001315
XiaokangQian7807f9f2022-02-15 10:04:37 +00001316 /*
XiaokangQianb67384d2022-04-19 00:02:38 +00001317 * ClientHello layout:
XiaokangQian7807f9f2022-02-15 10:04:37 +00001318 * 0 . 1 protocol version
XiaokangQiane8ff3502022-04-22 02:34:40 +00001319 * 2 . 33 random bytes
XiaokangQianc5763b52022-04-02 03:34:37 +00001320 * 34 . 34 session id length ( 1 byte )
XiaokangQian7807f9f2022-02-15 10:04:37 +00001321 * 35 . 34+x session id
XiaokangQian7807f9f2022-02-15 10:04:37 +00001322 * .. . .. ciphersuite list length ( 2 bytes )
1323 * .. . .. ciphersuite list
1324 * .. . .. compression alg. list length ( 1 byte )
1325 * .. . .. compression alg. list
1326 * .. . .. extensions length ( 2 bytes, optional )
1327 * .. . .. extensions ( optional )
1328 */
1329
XiaokangQianb67384d2022-04-19 00:02:38 +00001330 /*
bootstrap-prime6dbbf442022-05-17 19:30:44 -04001331 * Minimal length ( with everything empty and extensions omitted ) is
XiaokangQian7807f9f2022-02-15 10:04:37 +00001332 * 2 + 32 + 1 + 2 + 1 = 38 bytes. Check that first, so that we can
1333 * read at least up to session id length without worrying.
1334 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001335 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, 38);
XiaokangQian7807f9f2022-02-15 10:04:37 +00001336
1337 /* ...
1338 * ProtocolVersion legacy_version = 0x0303; // TLS 1.2
1339 * ...
1340 * with ProtocolVersion defined as:
1341 * uint16 ProtocolVersion;
1342 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001343 if (mbedtls_ssl_read_version(p, ssl->conf->transport) !=
1344 MBEDTLS_SSL_VERSION_TLS1_2) {
1345 MBEDTLS_SSL_DEBUG_MSG(1, ("Unsupported version of TLS."));
1346 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_PROTOCOL_VERSION,
1347 MBEDTLS_ERR_SSL_BAD_PROTOCOL_VERSION);
1348 return MBEDTLS_ERR_SSL_BAD_PROTOCOL_VERSION;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001349 }
1350 p += 2;
1351
Jerry Yuc1be19f2022-04-23 16:11:39 +08001352 /* ...
1353 * Random random;
1354 * ...
XiaokangQianb67384d2022-04-19 00:02:38 +00001355 * with Random defined as:
1356 * opaque Random[32];
XiaokangQian7807f9f2022-02-15 10:04:37 +00001357 */
Ronald Crond540d992023-03-07 09:41:48 +01001358 random = p;
XiaokangQian08037552022-04-20 07:16:41 +00001359 p += MBEDTLS_CLIENT_HELLO_RANDOM_LEN;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001360
Jerry Yuc1be19f2022-04-23 16:11:39 +08001361 /* ...
XiaokangQianb67384d2022-04-19 00:02:38 +00001362 * opaque legacy_session_id<0..32>;
Jerry Yuc1be19f2022-04-23 16:11:39 +08001363 * ...
XiaokangQian7807f9f2022-02-15 10:04:37 +00001364 */
Ronald Croncada4102023-03-07 09:51:39 +01001365 legacy_session_id_len = *(p++);
1366 legacy_session_id = p;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001367
XiaokangQianb67384d2022-04-19 00:02:38 +00001368 /*
1369 * Check we have enough data for the legacy session identifier
Jerry Yue95c8af2022-07-26 15:48:20 +08001370 * and the ciphersuite list length.
XiaokangQianb67384d2022-04-19 00:02:38 +00001371 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001372 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, legacy_session_id_len + 2);
XiaokangQian4080a7f2022-04-11 09:55:18 +00001373 p += legacy_session_id_len;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001374
Ronald Cron2f16b4e2023-03-07 10:07:32 +01001375 /* ...
1376 * CipherSuite cipher_suites<2..2^16-2>;
1377 * ...
1378 * with CipherSuite defined as:
1379 * uint8 CipherSuite[2];
1380 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001381 cipher_suites_len = MBEDTLS_GET_UINT16_BE(p, 0);
XiaokangQian7807f9f2022-02-15 10:04:37 +00001382 p += 2;
Ronald Cron2f16b4e2023-03-07 10:07:32 +01001383 cipher_suites = p;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001384
Ronald Cronfc7ae872023-02-16 15:32:19 +01001385 /*
1386 * The length of the ciphersuite list has to be even.
1387 */
1388 if (cipher_suites_len & 1) {
1389 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR,
1390 MBEDTLS_ERR_SSL_DECODE_ERROR);
1391 return MBEDTLS_ERR_SSL_DECODE_ERROR;
1392 }
1393
XiaokangQianb67384d2022-04-19 00:02:38 +00001394 /* Check we have enough data for the ciphersuite list, the legacy
1395 * compression methods and the length of the extensions.
Jerry Yue95c8af2022-07-26 15:48:20 +08001396 *
1397 * cipher_suites cipher_suites_len bytes
1398 * legacy_compression_methods 2 bytes
1399 * extensions_len 2 bytes
XiaokangQianb67384d2022-04-19 00:02:38 +00001400 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001401 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, cipher_suites_len + 2 + 2);
Ronald Cron8c527d02023-03-07 15:47:47 +01001402 p += cipher_suites_len;
1403 cipher_suites_end = p;
Jerry Yu5725f1c2022-08-21 17:27:16 +08001404
1405 /*
Ronald Cron8c527d02023-03-07 15:47:47 +01001406 * Search for the supported versions extension and parse it to determine
1407 * if the client supports TLS 1.3.
1408 */
1409 ret = mbedtls_ssl_tls13_is_supported_versions_ext_present_in_exts(
1410 ssl, p + 2, end,
Ronald Croneff56732023-04-03 17:36:31 +02001411 &supported_versions_data, &supported_versions_data_end);
Ronald Cron8c527d02023-03-07 15:47:47 +01001412 if (ret < 0) {
1413 MBEDTLS_SSL_DEBUG_RET(1,
1414 ("mbedtls_ssl_tls13_is_supported_versions_ext_present_in_exts"), ret);
1415 return ret;
1416 }
1417
1418 if (ret == 0) {
Ronald Cron5af4c7f2023-03-07 20:46:59 +01001419 return SSL_CLIENT_HELLO_TLS1_2;
Ronald Cron8c527d02023-03-07 15:47:47 +01001420 }
1421
Ronald Cron5af4c7f2023-03-07 20:46:59 +01001422 if (ret == 1) {
1423 ret = ssl_tls13_parse_supported_versions_ext(ssl,
Ronald Croneff56732023-04-03 17:36:31 +02001424 supported_versions_data,
1425 supported_versions_data_end);
Ronald Cron5af4c7f2023-03-07 20:46:59 +01001426 if (ret < 0) {
1427 MBEDTLS_SSL_DEBUG_RET(1,
1428 ("ssl_tls13_parse_supported_versions_ext"), ret);
1429 return ret;
1430 }
1431
Ronald Cronb828c7d2023-04-03 16:37:22 +02001432 /*
1433 * The supported versions extension was parsed successfully as the
1434 * value returned by ssl_tls13_parse_supported_versions_ext() is
1435 * positive. The return value is then equal to
1436 * MBEDTLS_SSL_VERSION_TLS1_2 or MBEDTLS_SSL_VERSION_TLS1_3, defining
1437 * the TLS version to negotiate.
1438 */
Ronald Cron5af4c7f2023-03-07 20:46:59 +01001439 if (MBEDTLS_SSL_VERSION_TLS1_2 == ret) {
1440 return SSL_CLIENT_HELLO_TLS1_2;
1441 }
Ronald Cron8c527d02023-03-07 15:47:47 +01001442 }
1443
1444 /*
1445 * We negotiate TLS 1.3.
Ronald Cron64582392023-03-07 09:21:40 +01001446 */
1447 ssl->tls_version = MBEDTLS_SSL_VERSION_TLS1_3;
Ronald Cron64582392023-03-07 09:21:40 +01001448 ssl->session_negotiate->tls_version = MBEDTLS_SSL_VERSION_TLS1_3;
1449 ssl->session_negotiate->endpoint = ssl->conf->endpoint;
Ronald Cron64582392023-03-07 09:21:40 +01001450
1451 /*
Ronald Crondad02b22023-04-06 09:57:52 +02001452 * We are negotiating the version 1.3 of the protocol. Do what we have
Ronald Croncada4102023-03-07 09:51:39 +01001453 * postponed: copy of the client random bytes, copy of the legacy session
Ronald Cron2f16b4e2023-03-07 10:07:32 +01001454 * identifier and selection of the TLS 1.3 cipher suite.
Ronald Crond540d992023-03-07 09:41:48 +01001455 */
1456 MBEDTLS_SSL_DEBUG_BUF(3, "client hello, random bytes",
1457 random, MBEDTLS_CLIENT_HELLO_RANDOM_LEN);
1458 memcpy(&handshake->randbytes[0], random, MBEDTLS_CLIENT_HELLO_RANDOM_LEN);
1459
Ronald Croncada4102023-03-07 09:51:39 +01001460 if (legacy_session_id_len > sizeof(ssl->session_negotiate->id)) {
1461 MBEDTLS_SSL_DEBUG_MSG(1, ("bad client hello message"));
1462 return MBEDTLS_ERR_SSL_DECODE_ERROR;
1463 }
1464 ssl->session_negotiate->id_len = legacy_session_id_len;
1465 MBEDTLS_SSL_DEBUG_BUF(3, "client hello, session id",
1466 legacy_session_id, legacy_session_id_len);
1467 memcpy(&ssl->session_negotiate->id[0],
1468 legacy_session_id, legacy_session_id_len);
1469
Ronald Crond540d992023-03-07 09:41:48 +01001470 /*
Jerry Yu5725f1c2022-08-21 17:27:16 +08001471 * Search for a matching ciphersuite
1472 */
Ronald Cron2f16b4e2023-03-07 10:07:32 +01001473 MBEDTLS_SSL_DEBUG_BUF(3, "client hello, list of cipher suites",
1474 cipher_suites, cipher_suites_len);
Ronald Crone45afd72023-04-04 15:10:06 +02001475 for (const unsigned char *cipher_suites_p = cipher_suites;
1476 cipher_suites_p < cipher_suites_end; cipher_suites_p += 2) {
XiaokangQiane8ff3502022-04-22 02:34:40 +00001477 uint16_t cipher_suite;
Gilles Peskine449bd832023-01-11 14:50:10 +01001478 const mbedtls_ssl_ciphersuite_t *ciphersuite_info;
Jerry Yu5725f1c2022-08-21 17:27:16 +08001479
Ronald Crond89360b2023-02-21 08:53:33 +01001480 /*
Ronald Crone45afd72023-04-04 15:10:06 +02001481 * "cipher_suites_end - cipher_suites_p is even" is an invariant of the
1482 * loop. As cipher_suites_end - cipher_suites_p > 0, we have
1483 * cipher_suites_end - cipher_suites_p >= 2 and it is thus safe to read
1484 * two bytes.
Ronald Crond89360b2023-02-21 08:53:33 +01001485 */
Ronald Crone45afd72023-04-04 15:10:06 +02001486 cipher_suite = MBEDTLS_GET_UINT16_BE(cipher_suites_p, 0);
Jerry Yuc5a23a02022-08-25 10:51:44 +08001487 ciphersuite_info = ssl_tls13_validate_peer_ciphersuite(
Gilles Peskine449bd832023-01-11 14:50:10 +01001488 ssl, cipher_suite);
1489 if (ciphersuite_info == NULL) {
Jerry Yu5725f1c2022-08-21 17:27:16 +08001490 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +01001491 }
Jerry Yu5725f1c2022-08-21 17:27:16 +08001492
Jerry Yu5725f1c2022-08-21 17:27:16 +08001493 ssl->session_negotiate->ciphersuite = cipher_suite;
Jerry Yuc4bf5d62022-10-29 09:08:47 +08001494 handshake->ciphersuite_info = ciphersuite_info;
Gilles Peskine449bd832023-01-11 14:50:10 +01001495 MBEDTLS_SSL_DEBUG_MSG(2, ("selected ciphersuite: %04x - %s",
1496 cipher_suite,
1497 ciphersuite_info->name));
Ronald Cron25e9ec62023-02-16 15:35:16 +01001498 break;
XiaokangQian17f974c2022-04-19 09:57:41 +00001499 }
Jerry Yu5725f1c2022-08-21 17:27:16 +08001500
Gilles Peskine449bd832023-01-11 14:50:10 +01001501 if (handshake->ciphersuite_info == NULL) {
1502 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE,
1503 MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE);
1504 return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
Jerry Yu5725f1c2022-08-21 17:27:16 +08001505 }
Jerry Yuc1be19f2022-04-23 16:11:39 +08001506
XiaokangQian4080a7f2022-04-11 09:55:18 +00001507 /* ...
XiaokangQianb67384d2022-04-19 00:02:38 +00001508 * opaque legacy_compression_methods<1..2^8-1>;
XiaokangQian4080a7f2022-04-11 09:55:18 +00001509 * ...
XiaokangQian7807f9f2022-02-15 10:04:37 +00001510 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001511 if (p[0] != 1 || p[1] != MBEDTLS_SSL_COMPRESS_NULL) {
1512 MBEDTLS_SSL_DEBUG_MSG(1, ("bad legacy compression method"));
1513 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER,
1514 MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER);
1515 return MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001516 }
XiaokangQianb67384d2022-04-19 00:02:38 +00001517 p += 2;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001518
Jerry Yuc1be19f2022-04-23 16:11:39 +08001519 /* ...
XiaokangQianb67384d2022-04-19 00:02:38 +00001520 * Extension extensions<8..2^16-1>;
Jerry Yuc1be19f2022-04-23 16:11:39 +08001521 * ...
XiaokangQianb67384d2022-04-19 00:02:38 +00001522 * with Extension defined as:
1523 * struct {
1524 * ExtensionType extension_type;
1525 * opaque extension_data<0..2^16-1>;
1526 * } Extension;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001527 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001528 extensions_len = MBEDTLS_GET_UINT16_BE(p, 0);
XiaokangQian7807f9f2022-02-15 10:04:37 +00001529 p += 2;
Gilles Peskine449bd832023-01-11 14:50:10 +01001530 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, extensions_len);
XiaokangQiane8ff3502022-04-22 02:34:40 +00001531 extensions_end = p + extensions_len;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001532
Gilles Peskine449bd832023-01-11 14:50:10 +01001533 MBEDTLS_SSL_DEBUG_BUF(3, "client hello extensions", p, extensions_len);
Jerry Yu63a459c2022-10-31 13:38:40 +08001534 handshake->received_extensions = MBEDTLS_SSL_EXT_MASK_NONE;
Jerry Yu0c354a22022-08-29 15:25:36 +08001535
Gilles Peskine449bd832023-01-11 14:50:10 +01001536 while (p < extensions_end) {
XiaokangQian7807f9f2022-02-15 10:04:37 +00001537 unsigned int extension_type;
1538 size_t extension_data_len;
1539 const unsigned char *extension_data_end;
Jerry Yu263dbf72022-10-26 10:51:27 +08001540 uint32_t allowed_exts = MBEDTLS_SSL_TLS1_3_ALLOWED_EXTS_OF_CH;
1541
Ronald Cron5fbd2702024-02-14 10:03:36 +01001542 if (ssl->handshake->hello_retry_request_flag) {
Jerry Yu263dbf72022-10-26 10:51:27 +08001543 /* Do not accept early data extension in 2nd ClientHello */
1544 allowed_exts &= ~MBEDTLS_SSL_EXT_MASK(EARLY_DATA);
1545 }
XiaokangQian7807f9f2022-02-15 10:04:37 +00001546
Jerry Yu0c354a22022-08-29 15:25:36 +08001547 /* RFC 8446, section 4.2.11
Jerry Yu1c9247c2022-07-21 12:37:39 +08001548 *
1549 * The "pre_shared_key" extension MUST be the last extension in the
1550 * ClientHello (this facilitates implementation as described below).
1551 * Servers MUST check that it is the last extension and otherwise fail
1552 * the handshake with an "illegal_parameter" alert.
1553 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001554 if (handshake->received_extensions & MBEDTLS_SSL_EXT_MASK(PRE_SHARED_KEY)) {
Jerry Yu1c9247c2022-07-21 12:37:39 +08001555 MBEDTLS_SSL_DEBUG_MSG(
Gilles Peskine449bd832023-01-11 14:50:10 +01001556 3, ("pre_shared_key is not last extension."));
Jerry Yu1c9247c2022-07-21 12:37:39 +08001557 MBEDTLS_SSL_PEND_FATAL_ALERT(
1558 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER,
Gilles Peskine449bd832023-01-11 14:50:10 +01001559 MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER);
1560 return MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER;
Jerry Yu1c9247c2022-07-21 12:37:39 +08001561 }
1562
Gilles Peskine449bd832023-01-11 14:50:10 +01001563 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, extensions_end, 4);
1564 extension_type = MBEDTLS_GET_UINT16_BE(p, 0);
1565 extension_data_len = MBEDTLS_GET_UINT16_BE(p, 2);
XiaokangQian7807f9f2022-02-15 10:04:37 +00001566 p += 4;
1567
Gilles Peskine449bd832023-01-11 14:50:10 +01001568 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, extensions_end, extension_data_len);
XiaokangQian7807f9f2022-02-15 10:04:37 +00001569 extension_data_end = p + extension_data_len;
1570
Jerry Yuc4bf5d62022-10-29 09:08:47 +08001571 ret = mbedtls_ssl_tls13_check_received_extension(
Gilles Peskine449bd832023-01-11 14:50:10 +01001572 ssl, MBEDTLS_SSL_HS_CLIENT_HELLO, extension_type,
Jerry Yu263dbf72022-10-26 10:51:27 +08001573 allowed_exts);
Gilles Peskine449bd832023-01-11 14:50:10 +01001574 if (ret != 0) {
1575 return ret;
1576 }
Jerry Yue18dc7e2022-08-04 16:29:22 +08001577
Gilles Peskine449bd832023-01-11 14:50:10 +01001578 switch (extension_type) {
XiaokangQian40a35232022-05-07 09:02:40 +00001579#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
1580 case MBEDTLS_TLS_EXT_SERVERNAME:
Gilles Peskine449bd832023-01-11 14:50:10 +01001581 MBEDTLS_SSL_DEBUG_MSG(3, ("found ServerName extension"));
1582 ret = mbedtls_ssl_parse_server_name_ext(ssl, p,
1583 extension_data_end);
1584 if (ret != 0) {
XiaokangQian40a35232022-05-07 09:02:40 +00001585 MBEDTLS_SSL_DEBUG_RET(
Gilles Peskine449bd832023-01-11 14:50:10 +01001586 1, "mbedtls_ssl_parse_servername_ext", ret);
1587 return ret;
XiaokangQian40a35232022-05-07 09:02:40 +00001588 }
XiaokangQian40a35232022-05-07 09:02:40 +00001589 break;
1590#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
1591
Przemek Stekiel8c0a9532023-06-15 16:48:19 +02001592#if defined(PSA_WANT_ALG_ECDH) || defined(PSA_WANT_ALG_FFDH)
XiaokangQian7807f9f2022-02-15 10:04:37 +00001593 case MBEDTLS_TLS_EXT_SUPPORTED_GROUPS:
Gilles Peskine449bd832023-01-11 14:50:10 +01001594 MBEDTLS_SSL_DEBUG_MSG(3, ("found supported group extension"));
XiaokangQian7807f9f2022-02-15 10:04:37 +00001595
1596 /* Supported Groups Extension
1597 *
1598 * When sent by the client, the "supported_groups" extension
1599 * indicates the named groups which the client supports,
1600 * ordered from most preferred to least preferred.
1601 */
Jerry Yuc1be19f2022-04-23 16:11:39 +08001602 ret = ssl_tls13_parse_supported_groups_ext(
Gilles Peskine449bd832023-01-11 14:50:10 +01001603 ssl, p, extension_data_end);
1604 if (ret != 0) {
Xiaokang Qian9f1747b2023-03-30 02:50:04 +00001605 MBEDTLS_SSL_DEBUG_RET(
Xiaokang Qian8bce0e62023-04-04 10:15:48 +00001606 1, "ssl_tls13_parse_supported_groups_ext", ret);
Gilles Peskine449bd832023-01-11 14:50:10 +01001607 return ret;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001608 }
1609
XiaokangQian7807f9f2022-02-15 10:04:37 +00001610 break;
Przemek Stekiel8c0a9532023-06-15 16:48:19 +02001611#endif /* PSA_WANT_ALG_ECDH || PSA_WANT_ALG_FFDH*/
XiaokangQian7807f9f2022-02-15 10:04:37 +00001612
Valerio Settic9ae8622023-07-25 11:23:50 +02001613#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_EPHEMERAL_ENABLED)
XiaokangQian7807f9f2022-02-15 10:04:37 +00001614 case MBEDTLS_TLS_EXT_KEY_SHARE:
Gilles Peskine449bd832023-01-11 14:50:10 +01001615 MBEDTLS_SSL_DEBUG_MSG(3, ("found key share extension"));
XiaokangQian7807f9f2022-02-15 10:04:37 +00001616
1617 /*
1618 * Key Share Extension
1619 *
1620 * When sent by the client, the "key_share" extension
1621 * contains the endpoint's cryptographic parameters for
1622 * ECDHE/DHE key establishment methods.
1623 */
Jerry Yuc1be19f2022-04-23 16:11:39 +08001624 ret = ssl_tls13_parse_key_shares_ext(
Gilles Peskine449bd832023-01-11 14:50:10 +01001625 ssl, p, extension_data_end);
1626 if (ret == SSL_TLS1_3_PARSE_KEY_SHARES_EXT_NO_MATCH) {
Ronald Cron8a74f072023-06-14 17:59:29 +02001627 MBEDTLS_SSL_DEBUG_MSG(2, ("No usable share for key agreement."));
1628 no_usable_share_for_key_agreement = 1;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001629 }
1630
Gilles Peskine449bd832023-01-11 14:50:10 +01001631 if (ret < 0) {
Jerry Yu582dd062022-04-22 21:59:01 +08001632 MBEDTLS_SSL_DEBUG_RET(
Gilles Peskine449bd832023-01-11 14:50:10 +01001633 1, "ssl_tls13_parse_key_shares_ext", ret);
1634 return ret;
Jerry Yu582dd062022-04-22 21:59:01 +08001635 }
XiaokangQian7807f9f2022-02-15 10:04:37 +00001636
XiaokangQian7807f9f2022-02-15 10:04:37 +00001637 break;
Valerio Settic9ae8622023-07-25 11:23:50 +02001638#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_EPHEMERAL_ENABLED */
XiaokangQian7807f9f2022-02-15 10:04:37 +00001639
1640 case MBEDTLS_TLS_EXT_SUPPORTED_VERSIONS:
Ronald Cron8c527d02023-03-07 15:47:47 +01001641 /* Already parsed */
XiaokangQian7807f9f2022-02-15 10:04:37 +00001642 break;
1643
Ronald Cron41a443a2022-10-04 16:38:25 +02001644#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED)
Jerry Yue19e3b92022-07-08 12:04:51 +00001645 case MBEDTLS_TLS_EXT_PSK_KEY_EXCHANGE_MODES:
Xiaokang Qian9f1747b2023-03-30 02:50:04 +00001646 MBEDTLS_SSL_DEBUG_MSG(
1647 3, ("found psk key exchange modes extension"));
Jerry Yue19e3b92022-07-08 12:04:51 +00001648
1649 ret = ssl_tls13_parse_key_exchange_modes_ext(
Gilles Peskine449bd832023-01-11 14:50:10 +01001650 ssl, p, extension_data_end);
1651 if (ret != 0) {
Jerry Yue19e3b92022-07-08 12:04:51 +00001652 MBEDTLS_SSL_DEBUG_RET(
Gilles Peskine449bd832023-01-11 14:50:10 +01001653 1, "ssl_tls13_parse_key_exchange_modes_ext", ret);
1654 return ret;
Jerry Yue19e3b92022-07-08 12:04:51 +00001655 }
1656
Jerry Yue19e3b92022-07-08 12:04:51 +00001657 break;
Ronald Cron41a443a2022-10-04 16:38:25 +02001658#endif
Jerry Yue19e3b92022-07-08 12:04:51 +00001659
Jerry Yu1c105562022-07-10 06:32:38 +00001660 case MBEDTLS_TLS_EXT_PRE_SHARED_KEY:
Gilles Peskine449bd832023-01-11 14:50:10 +01001661 MBEDTLS_SSL_DEBUG_MSG(3, ("found pre_shared_key extension"));
1662 if ((handshake->received_extensions &
1663 MBEDTLS_SSL_EXT_MASK(PSK_KEY_EXCHANGE_MODES)) == 0) {
Jerry Yu13ab81d2022-07-22 23:17:11 +08001664 MBEDTLS_SSL_PEND_FATAL_ALERT(
1665 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER,
Gilles Peskine449bd832023-01-11 14:50:10 +01001666 MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER);
1667 return MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER;
Jerry Yu13ab81d2022-07-22 23:17:11 +08001668 }
Ronald Cron41a443a2022-10-04 16:38:25 +02001669#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED)
Jerry Yu1c105562022-07-10 06:32:38 +00001670 /* Delay processing of the PSK identity once we have
1671 * found out which algorithms to use. We keep a pointer
1672 * to the buffer and the size for later processing.
1673 */
Jerry Yu29d9faa2022-08-23 17:52:45 +08001674 pre_shared_key_ext = p;
Jerry Yu1c105562022-07-10 06:32:38 +00001675 pre_shared_key_ext_end = extension_data_end;
Jerry Yue18dc7e2022-08-04 16:29:22 +08001676#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED */
Jerry Yu1c105562022-07-10 06:32:38 +00001677 break;
Jerry Yu1c105562022-07-10 06:32:38 +00001678
XiaokangQianacb39922022-06-17 10:18:48 +00001679#if defined(MBEDTLS_SSL_ALPN)
1680 case MBEDTLS_TLS_EXT_ALPN:
Gilles Peskine449bd832023-01-11 14:50:10 +01001681 MBEDTLS_SSL_DEBUG_MSG(3, ("found alpn extension"));
XiaokangQianacb39922022-06-17 10:18:48 +00001682
Gilles Peskine449bd832023-01-11 14:50:10 +01001683 ret = mbedtls_ssl_parse_alpn_ext(ssl, p, extension_data_end);
1684 if (ret != 0) {
XiaokangQianacb39922022-06-17 10:18:48 +00001685 MBEDTLS_SSL_DEBUG_RET(
Gilles Peskine449bd832023-01-11 14:50:10 +01001686 1, ("mbedtls_ssl_parse_alpn_ext"), ret);
1687 return ret;
XiaokangQianacb39922022-06-17 10:18:48 +00001688 }
XiaokangQianacb39922022-06-17 10:18:48 +00001689 break;
1690#endif /* MBEDTLS_SSL_ALPN */
1691
Ronald Cron928cbd32022-10-04 16:14:26 +02001692#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED)
XiaokangQian7807f9f2022-02-15 10:04:37 +00001693 case MBEDTLS_TLS_EXT_SIG_ALG:
Gilles Peskine449bd832023-01-11 14:50:10 +01001694 MBEDTLS_SSL_DEBUG_MSG(3, ("found signature_algorithms extension"));
XiaokangQian7807f9f2022-02-15 10:04:37 +00001695
Gabor Mezei078e8032022-04-27 21:17:56 +02001696 ret = mbedtls_ssl_parse_sig_alg_ext(
Gilles Peskine449bd832023-01-11 14:50:10 +01001697 ssl, p, extension_data_end);
1698 if (ret != 0) {
Xiaokang Qian49f39c12023-04-06 02:31:02 +00001699 MBEDTLS_SSL_DEBUG_RET(
1700 1, "mbedtls_ssl_parse_sig_alg_ext", ret);
Gilles Peskine449bd832023-01-11 14:50:10 +01001701 return ret;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001702 }
XiaokangQian7807f9f2022-02-15 10:04:37 +00001703 break;
Ronald Cron928cbd32022-10-04 16:14:26 +02001704#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED */
XiaokangQian7807f9f2022-02-15 10:04:37 +00001705
Jan Bruckner151f6422023-02-10 12:45:19 +01001706#if defined(MBEDTLS_SSL_RECORD_SIZE_LIMIT)
1707 case MBEDTLS_TLS_EXT_RECORD_SIZE_LIMIT:
1708 MBEDTLS_SSL_DEBUG_MSG(3, ("found record_size_limit extension"));
1709
Xiaokang Qian9f1747b2023-03-30 02:50:04 +00001710 ret = mbedtls_ssl_tls13_parse_record_size_limit_ext(
1711 ssl, p, extension_data_end);
Jan Brucknerf482dcc2023-03-15 09:09:06 +01001712 if (ret != 0) {
1713 MBEDTLS_SSL_DEBUG_RET(
1714 1, ("mbedtls_ssl_tls13_parse_record_size_limit_ext"), ret);
1715 return ret;
1716 }
Jan Bruckner151f6422023-02-10 12:45:19 +01001717 break;
1718#endif /* MBEDTLS_SSL_RECORD_SIZE_LIMIT */
1719
XiaokangQian7807f9f2022-02-15 10:04:37 +00001720 default:
Jerry Yu79aa7212022-11-08 21:30:21 +08001721 MBEDTLS_SSL_PRINT_EXT(
Jerry Yu63a459c2022-10-31 13:38:40 +08001722 3, MBEDTLS_SSL_HS_CLIENT_HELLO,
Gilles Peskine449bd832023-01-11 14:50:10 +01001723 extension_type, "( ignored )");
Jerry Yu0c354a22022-08-29 15:25:36 +08001724 break;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001725 }
1726
1727 p += extension_data_len;
1728 }
1729
Gilles Peskine449bd832023-01-11 14:50:10 +01001730 MBEDTLS_SSL_PRINT_EXTS(3, MBEDTLS_SSL_HS_CLIENT_HELLO,
1731 handshake->received_extensions);
Jerry Yue18dc7e2022-08-04 16:29:22 +08001732
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +01001733 ret = mbedtls_ssl_add_hs_hdr_to_checksum(ssl,
1734 MBEDTLS_SSL_HS_CLIENT_HELLO,
1735 p - buf);
1736 if (0 != ret) {
1737 MBEDTLS_SSL_DEBUG_RET(1, ("mbedtls_ssl_add_hs_hdr_to_checksum"), ret);
1738 return ret;
1739 }
Jerry Yu032b15ce2022-07-11 06:10:03 +00001740
Ronald Cron41a443a2022-10-04 16:38:25 +02001741#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED)
XiaokangQian7807f9f2022-02-15 10:04:37 +00001742 /* Update checksum with either
1743 * - The entire content of the CH message, if no PSK extension is present
1744 * - The content up to but excluding the PSK extension, if present.
Ronald Cron12e72f12024-02-14 15:49:38 +01001745 * Always parse the pre-shared key extension when present in the
1746 * ClientHello even if some pre-requisites for PSK key exchange modes are
1747 * not met. That way we always validate the syntax of the extension.
XiaokangQian7807f9f2022-02-15 10:04:37 +00001748 */
Ronald Cron12e72f12024-02-14 15:49:38 +01001749 if (handshake->received_extensions & MBEDTLS_SSL_EXT_MASK(PRE_SHARED_KEY)) {
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +01001750 ret = handshake->update_checksum(ssl, buf,
1751 pre_shared_key_ext - buf);
1752 if (0 != ret) {
1753 MBEDTLS_SSL_DEBUG_RET(1, ("update_checksum"), ret);
1754 return ret;
1755 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001756 ret = ssl_tls13_parse_pre_shared_key_ext(ssl,
1757 pre_shared_key_ext,
1758 pre_shared_key_ext_end,
1759 cipher_suites,
1760 cipher_suites_end);
1761 if (ret == MBEDTLS_ERR_SSL_UNKNOWN_IDENTITY) {
1762 handshake->received_extensions &= ~MBEDTLS_SSL_EXT_MASK(PRE_SHARED_KEY);
1763 } else if (ret != 0) {
Jerry Yu63a459c2022-10-31 13:38:40 +08001764 MBEDTLS_SSL_DEBUG_RET(
Gilles Peskine449bd832023-01-11 14:50:10 +01001765 1, "ssl_tls13_parse_pre_shared_key_ext", ret);
1766 return ret;
Jerry Yu1c105562022-07-10 06:32:38 +00001767 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001768 } else
Ronald Cron41a443a2022-10-04 16:38:25 +02001769#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED */
Jerry Yu1c105562022-07-10 06:32:38 +00001770 {
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +01001771 ret = handshake->update_checksum(ssl, buf, p - buf);
1772 if (0 != ret) {
1773 MBEDTLS_SSL_DEBUG_RET(1, ("update_checksum"), ret);
1774 return ret;
1775 }
Jerry Yu1c105562022-07-10 06:32:38 +00001776 }
XiaokangQian7807f9f2022-02-15 10:04:37 +00001777
Gilles Peskine449bd832023-01-11 14:50:10 +01001778 ret = ssl_tls13_determine_key_exchange_mode(ssl);
1779 if (ret < 0) {
1780 return ret;
1781 }
XiaokangQian7807f9f2022-02-15 10:04:37 +00001782
Ronald Cron8a74f072023-06-14 17:59:29 +02001783 if (ssl->handshake->key_exchange_mode !=
1784 MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK) {
1785 hrr_required = (no_usable_share_for_key_agreement != 0);
1786 }
1787
Gilles Peskine449bd832023-01-11 14:50:10 +01001788 mbedtls_ssl_optimize_checksum(ssl, handshake->ciphersuite_info);
Jerry Yue5834fd2022-08-29 20:16:09 +08001789
Gilles Peskine449bd832023-01-11 14:50:10 +01001790 return hrr_required ? SSL_CLIENT_HELLO_HRR_REQUIRED : SSL_CLIENT_HELLO_OK;
XiaokangQian75fe8c72022-06-15 09:42:45 +00001791}
1792
Jerry Yuab0da372023-02-08 13:55:24 +08001793#if defined(MBEDTLS_SSL_EARLY_DATA)
Jerry Yu4caf3ca2023-11-15 16:13:47 +08001794static int ssl_tls13_check_early_data_requirements(mbedtls_ssl_context *ssl)
Jerry Yuab0da372023-02-08 13:55:24 +08001795{
1796 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
1797
Jerry Yu985c9672022-12-04 14:06:30 +08001798 if (ssl->conf->early_data_enabled == MBEDTLS_SSL_EARLY_DATA_DISABLED) {
1799 MBEDTLS_SSL_DEBUG_MSG(
Jerry Yu985c9672022-12-04 14:06:30 +08001800 1,
Jerry Yu454dda32023-10-31 15:13:54 +08001801 ("EarlyData: rejected, feature disabled in server configuration."));
Jerry Yu4caf3ca2023-11-15 16:13:47 +08001802 return -1;
Ronald Cron7b6ee942024-01-12 10:29:55 +01001803 }
1804
Jerry Yu454dda32023-10-31 15:13:54 +08001805 if (!handshake->resume) {
1806 /* We currently support early data only in the case of PSKs established
1807 via a NewSessionTicket message thus in the case of a session
1808 resumption. */
Jerry Yu985c9672022-12-04 14:06:30 +08001809 MBEDTLS_SSL_DEBUG_MSG(
Jerry Yu7ef9fd82023-11-07 14:30:38 +08001810 1, ("EarlyData: rejected, not a session resumption."));
Jerry Yu4caf3ca2023-11-15 16:13:47 +08001811 return -1;
Jerry Yu985c9672022-12-04 14:06:30 +08001812 }
1813
Jerry Yu82fd6c12023-10-31 16:32:19 +08001814 /* RFC 8446 4.2.10
1815 *
1816 * In order to accept early data, the server MUST have accepted a PSK cipher
1817 * suite and selected the first key offered in the client's "pre_shared_key"
1818 * extension. In addition, it MUST verify that the following values are the
1819 * same as those associated with the selected PSK:
1820 * - The TLS version number
1821 * - The selected cipher suite
1822 * - The selected ALPN [RFC7301] protocol, if any
1823 *
1824 * NOTE:
Jerry Yu7ef9fd82023-11-07 14:30:38 +08001825 * - The TLS version number is checked in
1826 * ssl_tls13_offered_psks_check_identity_match_ticket().
1827 * - ALPN is not checked for the time being (TODO).
Jerry Yu82fd6c12023-10-31 16:32:19 +08001828 */
1829
1830 if (handshake->selected_identity != 0) {
1831 MBEDTLS_SSL_DEBUG_MSG(
Jerry Yu7ef9fd82023-11-07 14:30:38 +08001832 1, ("EarlyData: rejected, the selected key in "
1833 "`pre_shared_key` is not the first one."));
Jerry Yu4caf3ca2023-11-15 16:13:47 +08001834 return -1;
Jerry Yu82fd6c12023-10-31 16:32:19 +08001835 }
1836
1837 if (handshake->ciphersuite_info->id !=
1838 ssl->session_negotiate->ciphersuite) {
1839 MBEDTLS_SSL_DEBUG_MSG(
Jerry Yu7ef9fd82023-11-07 14:30:38 +08001840 1, ("EarlyData: rejected, the selected ciphersuite is not the one "
1841 "of the selected pre-shared key."));
Jerry Yu4caf3ca2023-11-15 16:13:47 +08001842 return -1;
Jerry Yu82fd6c12023-10-31 16:32:19 +08001843
1844 }
Jerry Yu985c9672022-12-04 14:06:30 +08001845
Pengyu Lv94a42cc2023-12-06 10:04:17 +08001846 if (!mbedtls_ssl_tls13_session_ticket_allow_early_data(ssl->session_negotiate)) {
Jerry Yufceddb32022-12-12 15:30:34 +08001847 MBEDTLS_SSL_DEBUG_MSG(
1848 1,
Jerry Yuea96ac32023-11-21 17:06:36 +08001849 ("EarlyData: rejected, early_data not allowed in ticket "
1850 "permission bits."));
Jerry Yu4caf3ca2023-11-15 16:13:47 +08001851 return -1;
Jerry Yufceddb32022-12-12 15:30:34 +08001852 }
Jerry Yu985c9672022-12-04 14:06:30 +08001853
Jerry Yu4caf3ca2023-11-15 16:13:47 +08001854 return 0;
Jerry Yuab0da372023-02-08 13:55:24 +08001855}
1856#endif /* MBEDTLS_SSL_EARLY_DATA */
1857
XiaokangQian75fe8c72022-06-15 09:42:45 +00001858/* Update the handshake state machine */
1859
Ronald Cronce7d76e2022-07-08 18:56:49 +02001860MBEDTLS_CHECK_RETURN_CRITICAL
Ronald Cron7b6ee942024-01-12 10:29:55 +01001861static int ssl_tls13_postprocess_client_hello(mbedtls_ssl_context *ssl,
1862 int hrr_required)
XiaokangQian75fe8c72022-06-15 09:42:45 +00001863{
1864 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1865
XiaokangQian7807f9f2022-02-15 10:04:37 +00001866 /*
XiaokangQianfb665a82022-06-15 03:57:21 +00001867 * Server certificate selection
1868 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001869 if (ssl->conf->f_cert_cb && (ret = ssl->conf->f_cert_cb(ssl)) != 0) {
1870 MBEDTLS_SSL_DEBUG_RET(1, "f_cert_cb", ret);
1871 return ret;
XiaokangQianfb665a82022-06-15 03:57:21 +00001872 }
1873#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
1874 ssl->handshake->sni_name = NULL;
1875 ssl->handshake->sni_name_len = 0;
1876#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
XiaokangQian7807f9f2022-02-15 10:04:37 +00001877
Gilles Peskine449bd832023-01-11 14:50:10 +01001878 ret = mbedtls_ssl_tls13_key_schedule_stage_early(ssl);
1879 if (ret != 0) {
1880 MBEDTLS_SSL_DEBUG_RET(1,
1881 "mbedtls_ssl_tls1_3_key_schedule_stage_early", ret);
1882 return ret;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001883 }
1884
Jerry Yuab0da372023-02-08 13:55:24 +08001885#if defined(MBEDTLS_SSL_EARLY_DATA)
Jerry Yu4caf3ca2023-11-15 16:13:47 +08001886 if (ssl->handshake->received_extensions & MBEDTLS_SSL_EXT_MASK(EARLY_DATA)) {
Ronald Cron31e2d832024-02-05 16:45:57 +01001887 ssl->handshake->early_data_accepted =
1888 (!hrr_required) && (ssl_tls13_check_early_data_requirements(ssl) == 0);
Jerry Yu4e9b70e2022-12-04 14:08:02 +08001889
Jerry Yu4caf3ca2023-11-15 16:13:47 +08001890 if (ssl->handshake->early_data_accepted) {
1891 ret = mbedtls_ssl_tls13_compute_early_transform(ssl);
1892 if (ret != 0) {
1893 MBEDTLS_SSL_DEBUG_RET(
1894 1, "mbedtls_ssl_tls13_compute_early_transform", ret);
1895 return ret;
1896 }
1897 } else {
1898 ssl->discard_early_data_record =
1899 hrr_required ?
1900 MBEDTLS_SSL_EARLY_DATA_DISCARD :
1901 MBEDTLS_SSL_EARLY_DATA_TRY_TO_DEPROTECT_AND_DISCARD;
Jerry Yu4e9b70e2022-12-04 14:08:02 +08001902 }
1903 }
Ronald Cron7b6ee942024-01-12 10:29:55 +01001904#else
1905 ((void) hrr_required);
Jerry Yuab0da372023-02-08 13:55:24 +08001906#endif /* MBEDTLS_SSL_EARLY_DATA */
1907
Gilles Peskine449bd832023-01-11 14:50:10 +01001908 return 0;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001909}
1910
1911/*
XiaokangQianed582dd2022-04-13 08:21:05 +00001912 * Main entry point from the state machine; orchestrates the otherfunctions.
1913 */
1914
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001915MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01001916static int ssl_tls13_process_client_hello(mbedtls_ssl_context *ssl)
XiaokangQianed582dd2022-04-13 08:21:05 +00001917{
1918
XiaokangQian08037552022-04-20 07:16:41 +00001919 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine449bd832023-01-11 14:50:10 +01001920 unsigned char *buf = NULL;
XiaokangQianed582dd2022-04-13 08:21:05 +00001921 size_t buflen = 0;
Jerry Yu4ca91402022-05-09 15:50:57 +08001922 int parse_client_hello_ret;
1923
Gilles Peskine449bd832023-01-11 14:50:10 +01001924 MBEDTLS_SSL_DEBUG_MSG(2, ("=> parse client hello"));
XiaokangQianed582dd2022-04-13 08:21:05 +00001925
Gilles Peskine449bd832023-01-11 14:50:10 +01001926 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_tls13_fetch_handshake_msg(
1927 ssl, MBEDTLS_SSL_HS_CLIENT_HELLO,
1928 &buf, &buflen));
XiaokangQianed582dd2022-04-13 08:21:05 +00001929
Gilles Peskine449bd832023-01-11 14:50:10 +01001930 MBEDTLS_SSL_PROC_CHK_NEG(ssl_tls13_parse_client_hello(ssl, buf,
1931 buf + buflen));
Ronald Cron5af4c7f2023-03-07 20:46:59 +01001932 parse_client_hello_ret = ret; /* Store positive return value of
1933 * parse_client_hello,
1934 * as negative error codes are handled
Jerry Yuf41553b2022-05-09 22:20:30 +08001935 * by MBEDTLS_SSL_PROC_CHK_NEG. */
Jerry Yu4ca91402022-05-09 15:50:57 +08001936
Ronald Cronb828c7d2023-04-03 16:37:22 +02001937 /*
Yanray Wang90acdc62023-12-08 10:29:42 +08001938 * Version 1.2 of the protocol has to be used for the handshake.
1939 * If TLS 1.2 is not supported, abort the handshake. Otherwise, set the
Ronald Cronb828c7d2023-04-03 16:37:22 +02001940 * ssl->keep_current_message flag for the ClientHello to be kept and parsed
1941 * as a TLS 1.2 ClientHello. We also change ssl->tls_version to
1942 * MBEDTLS_SSL_VERSION_TLS1_2 thus from now on mbedtls_ssl_handshake_step()
1943 * will dispatch to the TLS 1.2 state machine.
1944 */
Ronald Cron5af4c7f2023-03-07 20:46:59 +01001945 if (SSL_CLIENT_HELLO_TLS1_2 == parse_client_hello_ret) {
Yanray Wangfb0f47b2023-12-04 15:27:28 +08001946 /* Check if server supports TLS 1.2 */
Yanray Wang408ba6f2023-12-08 10:18:03 +08001947 if (!mbedtls_ssl_conf_is_tls12_enabled(ssl->conf)) {
Yanray Wangfb0f47b2023-12-04 15:27:28 +08001948 MBEDTLS_SSL_DEBUG_MSG(
Yanray Wang177e49a2023-12-08 10:51:04 +08001949 1, ("TLS 1.2 not supported."));
Yanray Wangfb0f47b2023-12-04 15:27:28 +08001950 MBEDTLS_SSL_PEND_FATAL_ALERT(
Yanray Wang2bef9172023-12-08 10:21:53 +08001951 MBEDTLS_SSL_ALERT_MSG_PROTOCOL_VERSION,
1952 MBEDTLS_ERR_SSL_BAD_PROTOCOL_VERSION);
1953 return MBEDTLS_ERR_SSL_BAD_PROTOCOL_VERSION;
Yanray Wangfb0f47b2023-12-04 15:27:28 +08001954 }
Ronald Cron5af4c7f2023-03-07 20:46:59 +01001955 ssl->keep_current_message = 1;
1956 ssl->tls_version = MBEDTLS_SSL_VERSION_TLS1_2;
1957 return 0;
1958 }
1959
Ronald Cron7b6ee942024-01-12 10:29:55 +01001960 MBEDTLS_SSL_PROC_CHK(
1961 ssl_tls13_postprocess_client_hello(ssl, parse_client_hello_ret ==
1962 SSL_CLIENT_HELLO_HRR_REQUIRED));
Jerry Yu582dd062022-04-22 21:59:01 +08001963
Ronald Cron5af4c7f2023-03-07 20:46:59 +01001964 if (SSL_CLIENT_HELLO_OK == parse_client_hello_ret) {
Gilles Peskine449bd832023-01-11 14:50:10 +01001965 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_SERVER_HELLO);
1966 } else {
1967 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_HELLO_RETRY_REQUEST);
1968 }
XiaokangQianed582dd2022-04-13 08:21:05 +00001969
1970cleanup:
1971
Gilles Peskine449bd832023-01-11 14:50:10 +01001972 MBEDTLS_SSL_DEBUG_MSG(2, ("<= parse client hello"));
1973 return ret;
XiaokangQianed582dd2022-04-13 08:21:05 +00001974}
1975
1976/*
Jerry Yu1c3e6882022-04-20 21:23:40 +08001977 * Handler for MBEDTLS_SSL_SERVER_HELLO
Jerry Yu5b64ae92022-03-30 17:15:02 +08001978 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001979MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01001980static int ssl_tls13_prepare_server_hello(mbedtls_ssl_context *ssl)
Jerry Yuf4b27e42022-03-30 17:32:21 +08001981{
Jerry Yu637a3f12022-04-20 21:37:58 +08001982 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1983 unsigned char *server_randbytes =
Gilles Peskine449bd832023-01-11 14:50:10 +01001984 ssl->handshake->randbytes + MBEDTLS_CLIENT_HELLO_RANDOM_LEN;
1985 if (ssl->conf->f_rng == NULL) {
1986 MBEDTLS_SSL_DEBUG_MSG(1, ("no RNG provided"));
1987 return MBEDTLS_ERR_SSL_NO_RNG;
Jerry Yuf4b27e42022-03-30 17:32:21 +08001988 }
1989
Gilles Peskine449bd832023-01-11 14:50:10 +01001990 if ((ret = ssl->conf->f_rng(ssl->conf->p_rng, server_randbytes,
1991 MBEDTLS_SERVER_HELLO_RANDOM_LEN)) != 0) {
1992 MBEDTLS_SSL_DEBUG_RET(1, "f_rng", ret);
1993 return ret;
Jerry Yuf4b27e42022-03-30 17:32:21 +08001994 }
1995
Gilles Peskine449bd832023-01-11 14:50:10 +01001996 MBEDTLS_SSL_DEBUG_BUF(3, "server hello, random bytes", server_randbytes,
1997 MBEDTLS_SERVER_HELLO_RANDOM_LEN);
Jerry Yuf4b27e42022-03-30 17:32:21 +08001998
1999#if defined(MBEDTLS_HAVE_TIME)
YxCda609132023-05-22 12:08:12 -07002000 ssl->session_negotiate->start = mbedtls_time(NULL);
Jerry Yuf4b27e42022-03-30 17:32:21 +08002001#endif /* MBEDTLS_HAVE_TIME */
2002
Gilles Peskine449bd832023-01-11 14:50:10 +01002003 return ret;
Jerry Yuf4b27e42022-03-30 17:32:21 +08002004}
2005
Jerry Yu3bf2c642022-03-30 22:02:12 +08002006/*
Jerry Yue74e04a2022-04-21 09:23:16 +08002007 * ssl_tls13_write_server_hello_supported_versions_ext ():
Jerry Yu3bf2c642022-03-30 22:02:12 +08002008 *
2009 * struct {
Jerry Yufb9f54d2022-04-06 10:08:34 +08002010 * ProtocolVersion selected_version;
Jerry Yu3bf2c642022-03-30 22:02:12 +08002011 * } SupportedVersions;
2012 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002013MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yue74e04a2022-04-21 09:23:16 +08002014static int ssl_tls13_write_server_hello_supported_versions_ext(
Gilles Peskine449bd832023-01-11 14:50:10 +01002015 mbedtls_ssl_context *ssl,
2016 unsigned char *buf,
2017 unsigned char *end,
2018 size_t *out_len)
Jerry Yu3bf2c642022-03-30 22:02:12 +08002019{
Jerry Yu3bf2c642022-03-30 22:02:12 +08002020 *out_len = 0;
2021
Gilles Peskine449bd832023-01-11 14:50:10 +01002022 MBEDTLS_SSL_DEBUG_MSG(3, ("server hello, write selected version"));
Jerry Yu3bf2c642022-03-30 22:02:12 +08002023
2024 /* Check if we have space to write the extension:
2025 * - extension_type (2 bytes)
2026 * - extension_data_length (2 bytes)
Jerry Yu349a6132022-04-14 20:52:56 +08002027 * - selected_version (2 bytes)
Jerry Yu3bf2c642022-03-30 22:02:12 +08002028 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002029 MBEDTLS_SSL_CHK_BUF_PTR(buf, end, 6);
Jerry Yu3bf2c642022-03-30 22:02:12 +08002030
Gilles Peskine449bd832023-01-11 14:50:10 +01002031 MBEDTLS_PUT_UINT16_BE(MBEDTLS_TLS_EXT_SUPPORTED_VERSIONS, buf, 0);
Jerry Yu3bf2c642022-03-30 22:02:12 +08002032
Gilles Peskine449bd832023-01-11 14:50:10 +01002033 MBEDTLS_PUT_UINT16_BE(2, buf, 2);
Jerry Yu3bf2c642022-03-30 22:02:12 +08002034
Gilles Peskine449bd832023-01-11 14:50:10 +01002035 mbedtls_ssl_write_version(buf + 4,
2036 ssl->conf->transport,
2037 ssl->tls_version);
Jerry Yu3bf2c642022-03-30 22:02:12 +08002038
Gilles Peskine449bd832023-01-11 14:50:10 +01002039 MBEDTLS_SSL_DEBUG_MSG(3, ("supported version: [%04x]",
2040 ssl->tls_version));
Jerry Yu3bf2c642022-03-30 22:02:12 +08002041
Jerry Yu349a6132022-04-14 20:52:56 +08002042 *out_len = 6;
Jerry Yu3bf2c642022-03-30 22:02:12 +08002043
Jerry Yub95dd362022-11-08 21:19:34 +08002044 mbedtls_ssl_tls13_set_hs_sent_ext_mask(
Gilles Peskine449bd832023-01-11 14:50:10 +01002045 ssl, MBEDTLS_TLS_EXT_SUPPORTED_VERSIONS);
Jerry Yub95dd362022-11-08 21:19:34 +08002046
Gilles Peskine449bd832023-01-11 14:50:10 +01002047 return 0;
Jerry Yu3bf2c642022-03-30 22:02:12 +08002048}
2049
Jerry Yud9436a12022-04-20 22:28:09 +08002050
Jerry Yu3bf2c642022-03-30 22:02:12 +08002051
2052/* Generate and export a single key share. For hybrid KEMs, this can
2053 * be called multiple times with the different components of the hybrid. */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002054MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002055static int ssl_tls13_generate_and_write_key_share(mbedtls_ssl_context *ssl,
2056 uint16_t named_group,
2057 unsigned char *buf,
2058 unsigned char *end,
2059 size_t *out_len)
Jerry Yu3bf2c642022-03-30 22:02:12 +08002060{
2061 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yu955ddd72022-04-22 22:27:33 +08002062
2063 *out_len = 0;
2064
Valerio Settic9ae8622023-07-25 11:23:50 +02002065#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_EPHEMERAL_ENABLED)
Przemek Stekiel29c219c2023-05-31 15:21:04 +02002066 if (mbedtls_ssl_tls13_named_group_is_ecdhe(named_group) ||
Przemek Stekield5f79e72023-06-29 09:08:43 +02002067 mbedtls_ssl_tls13_named_group_is_ffdh(named_group)) {
Przemek Stekiel408569f2023-07-06 11:26:44 +02002068 ret = mbedtls_ssl_tls13_generate_and_write_xxdh_key_exchange(
Gilles Peskine449bd832023-01-11 14:50:10 +01002069 ssl, named_group, buf, end, out_len);
2070 if (ret != 0) {
Jerry Yu89e103c2022-03-30 22:43:29 +08002071 MBEDTLS_SSL_DEBUG_RET(
Przemek Stekiel408569f2023-07-06 11:26:44 +02002072 1, "mbedtls_ssl_tls13_generate_and_write_xxdh_key_exchange",
Gilles Peskine449bd832023-01-11 14:50:10 +01002073 ret);
2074 return ret;
Jerry Yu3bf2c642022-03-30 22:02:12 +08002075 }
Gilles Peskine449bd832023-01-11 14:50:10 +01002076 } else
Valerio Settic9ae8622023-07-25 11:23:50 +02002077#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_EPHEMERAL_ENABLED */
Gilles Peskine449bd832023-01-11 14:50:10 +01002078 if (0 /* Other kinds of KEMs */) {
2079 } else {
Jerry Yu955ddd72022-04-22 22:27:33 +08002080 ((void) ssl);
2081 ((void) named_group);
2082 ((void) buf);
2083 ((void) end);
Jerry Yu3bf2c642022-03-30 22:02:12 +08002084 ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
2085 }
2086
Gilles Peskine449bd832023-01-11 14:50:10 +01002087 return ret;
Jerry Yu3bf2c642022-03-30 22:02:12 +08002088}
2089
2090/*
2091 * ssl_tls13_write_key_share_ext
2092 *
2093 * Structure of key_share extension in ServerHello:
2094 *
Jerry Yu1c3e6882022-04-20 21:23:40 +08002095 * struct {
2096 * NamedGroup group;
2097 * opaque key_exchange<1..2^16-1>;
2098 * } KeyShareEntry;
2099 * struct {
2100 * KeyShareEntry server_share;
2101 * } KeyShareServerHello;
Jerry Yu3bf2c642022-03-30 22:02:12 +08002102 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002103MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002104static int ssl_tls13_write_key_share_ext(mbedtls_ssl_context *ssl,
2105 unsigned char *buf,
2106 unsigned char *end,
2107 size_t *out_len)
Jerry Yu3bf2c642022-03-30 22:02:12 +08002108{
Jerry Yu955ddd72022-04-22 22:27:33 +08002109 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yu3bf2c642022-03-30 22:02:12 +08002110 unsigned char *p = buf;
Jerry Yu57d48412022-04-20 21:50:42 +08002111 uint16_t group = ssl->handshake->offered_group_id;
Jerry Yu3bf2c642022-03-30 22:02:12 +08002112 unsigned char *server_share = buf + 4;
Jerry Yu3bf2c642022-03-30 22:02:12 +08002113 size_t key_exchange_length;
Jerry Yu3bf2c642022-03-30 22:02:12 +08002114
2115 *out_len = 0;
2116
Gilles Peskine449bd832023-01-11 14:50:10 +01002117 MBEDTLS_SSL_DEBUG_MSG(3, ("server hello, adding key share extension"));
Jerry Yu3bf2c642022-03-30 22:02:12 +08002118
Gilles Peskine449bd832023-01-11 14:50:10 +01002119 MBEDTLS_SSL_DEBUG_MSG(2, ("server hello, write selected_group: %s (%04x)",
2120 mbedtls_ssl_named_group_to_str(group),
2121 group));
Jerry Yu58af2332022-09-06 11:19:31 +08002122
Jerry Yu3bf2c642022-03-30 22:02:12 +08002123 /* Check if we have space for header and length fields:
2124 * - extension_type (2 bytes)
2125 * - extension_data_length (2 bytes)
2126 * - group (2 bytes)
2127 * - key_exchange_length (2 bytes)
2128 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002129 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 8);
2130 MBEDTLS_PUT_UINT16_BE(MBEDTLS_TLS_EXT_KEY_SHARE, p, 0);
2131 MBEDTLS_PUT_UINT16_BE(group, server_share, 0);
Jerry Yu3bf2c642022-03-30 22:02:12 +08002132 p += 8;
Jerry Yu57d48412022-04-20 21:50:42 +08002133
Jerry Yu3bf2c642022-03-30 22:02:12 +08002134 /* When we introduce PQC-ECDHE hybrids, we'll want to call this
2135 * function multiple times. */
Jerry Yu955ddd72022-04-22 22:27:33 +08002136 ret = ssl_tls13_generate_and_write_key_share(
Gilles Peskine449bd832023-01-11 14:50:10 +01002137 ssl, group, server_share + 4, end, &key_exchange_length);
2138 if (ret != 0) {
2139 return ret;
2140 }
Jerry Yu3bf2c642022-03-30 22:02:12 +08002141 p += key_exchange_length;
Jerry Yuc1be19f2022-04-23 16:11:39 +08002142
Gilles Peskine449bd832023-01-11 14:50:10 +01002143 MBEDTLS_PUT_UINT16_BE(key_exchange_length, server_share + 2, 0);
Jerry Yu3bf2c642022-03-30 22:02:12 +08002144
Gilles Peskine449bd832023-01-11 14:50:10 +01002145 MBEDTLS_PUT_UINT16_BE(p - server_share, buf, 2);
Jerry Yu3bf2c642022-03-30 22:02:12 +08002146
Jerry Yu57d48412022-04-20 21:50:42 +08002147 *out_len = p - buf;
Jerry Yu955ddd72022-04-22 22:27:33 +08002148
Gilles Peskine449bd832023-01-11 14:50:10 +01002149 mbedtls_ssl_tls13_set_hs_sent_ext_mask(ssl, MBEDTLS_TLS_EXT_KEY_SHARE);
Jerry Yub95dd362022-11-08 21:19:34 +08002150
Gilles Peskine449bd832023-01-11 14:50:10 +01002151 return 0;
Jerry Yu3bf2c642022-03-30 22:02:12 +08002152}
Jerry Yud9436a12022-04-20 22:28:09 +08002153
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002154MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002155static int ssl_tls13_write_hrr_key_share_ext(mbedtls_ssl_context *ssl,
2156 unsigned char *buf,
2157 unsigned char *end,
2158 size_t *out_len)
Jerry Yu23f7a6f2022-04-23 15:16:45 +08002159{
2160 uint16_t selected_group = ssl->handshake->hrr_selected_group;
2161 /* key_share Extension
2162 *
2163 * struct {
2164 * select (Handshake.msg_type) {
2165 * ...
2166 * case hello_retry_request:
2167 * NamedGroup selected_group;
2168 * ...
2169 * };
2170 * } KeyShare;
2171 */
2172
2173 *out_len = 0;
2174
Jerry Yub0ac10b2022-05-05 11:10:08 +08002175 /*
2176 * For a pure PSK key exchange, there is no group to agree upon. The purpose
2177 * of the HRR is then to transmit a cookie to force the client to demonstrate
2178 * reachability at their apparent network address (primarily useful for DTLS).
2179 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002180 if (!mbedtls_ssl_tls13_key_exchange_mode_with_ephemeral(ssl)) {
2181 return 0;
2182 }
Jerry Yu23f7a6f2022-04-23 15:16:45 +08002183
2184 /* We should only send the key_share extension if the client's initial
2185 * key share was not acceptable. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002186 if (ssl->handshake->offered_group_id != 0) {
2187 MBEDTLS_SSL_DEBUG_MSG(4, ("Skip key_share extension in HRR"));
2188 return 0;
Jerry Yu23f7a6f2022-04-23 15:16:45 +08002189 }
2190
Gilles Peskine449bd832023-01-11 14:50:10 +01002191 if (selected_group == 0) {
2192 MBEDTLS_SSL_DEBUG_MSG(1, ("no matching named group found"));
2193 return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
Jerry Yu23f7a6f2022-04-23 15:16:45 +08002194 }
2195
Jerry Yub0ac10b2022-05-05 11:10:08 +08002196 /* Check if we have enough space:
2197 * - extension_type (2 bytes)
2198 * - extension_data_length (2 bytes)
2199 * - selected_group (2 bytes)
2200 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002201 MBEDTLS_SSL_CHK_BUF_PTR(buf, end, 6);
Jerry Yu23f7a6f2022-04-23 15:16:45 +08002202
Gilles Peskine449bd832023-01-11 14:50:10 +01002203 MBEDTLS_PUT_UINT16_BE(MBEDTLS_TLS_EXT_KEY_SHARE, buf, 0);
2204 MBEDTLS_PUT_UINT16_BE(2, buf, 2);
2205 MBEDTLS_PUT_UINT16_BE(selected_group, buf, 4);
Jerry Yu23f7a6f2022-04-23 15:16:45 +08002206
Gilles Peskine449bd832023-01-11 14:50:10 +01002207 MBEDTLS_SSL_DEBUG_MSG(3,
2208 ("HRR selected_group: %s (%x)",
2209 mbedtls_ssl_named_group_to_str(selected_group),
2210 selected_group));
Jerry Yu23f7a6f2022-04-23 15:16:45 +08002211
2212 *out_len = 6;
Jerry Yu23f7a6f2022-04-23 15:16:45 +08002213
Gilles Peskine449bd832023-01-11 14:50:10 +01002214 mbedtls_ssl_tls13_set_hs_sent_ext_mask(ssl, MBEDTLS_TLS_EXT_KEY_SHARE);
Jerry Yub95dd362022-11-08 21:19:34 +08002215
Gilles Peskine449bd832023-01-11 14:50:10 +01002216 return 0;
Jerry Yu23f7a6f2022-04-23 15:16:45 +08002217}
Jerry Yu3bf2c642022-03-30 22:02:12 +08002218
2219/*
2220 * Structure of ServerHello message:
2221 *
2222 * struct {
2223 * ProtocolVersion legacy_version = 0x0303; // TLS v1.2
2224 * Random random;
2225 * opaque legacy_session_id_echo<0..32>;
2226 * CipherSuite cipher_suite;
2227 * uint8 legacy_compression_method = 0;
2228 * Extension extensions<6..2^16-1>;
2229 * } ServerHello;
2230 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002231MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002232static int ssl_tls13_write_server_hello_body(mbedtls_ssl_context *ssl,
2233 unsigned char *buf,
2234 unsigned char *end,
2235 size_t *out_len,
2236 int is_hrr)
Jerry Yu56404d72022-03-30 17:36:13 +08002237{
Jerry Yu955ddd72022-04-22 22:27:33 +08002238 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yu3bf2c642022-03-30 22:02:12 +08002239 unsigned char *p = buf;
Jerry Yud9436a12022-04-20 22:28:09 +08002240 unsigned char *p_extensions_len;
Jerry Yufbe3e642022-04-25 19:31:51 +08002241 size_t output_len;
Jerry Yu3bf2c642022-03-30 22:02:12 +08002242
2243 *out_len = 0;
Jerry Yu50e00e32022-10-31 14:45:01 +08002244 ssl->handshake->sent_extensions = MBEDTLS_SSL_EXT_MASK_NONE;
Jerry Yu3bf2c642022-03-30 22:02:12 +08002245
Jerry Yucfc04b32022-04-21 09:31:58 +08002246 /* ...
2247 * ProtocolVersion legacy_version = 0x0303; // TLS 1.2
2248 * ...
2249 * with ProtocolVersion defined as:
2250 * uint16 ProtocolVersion;
Jerry Yu3bf2c642022-03-30 22:02:12 +08002251 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002252 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 2);
2253 MBEDTLS_PUT_UINT16_BE(0x0303, p, 0);
Jerry Yu3bf2c642022-03-30 22:02:12 +08002254 p += 2;
2255
Jerry Yu1c3e6882022-04-20 21:23:40 +08002256 /* ...
2257 * Random random;
2258 * ...
Jerry Yucfc04b32022-04-21 09:31:58 +08002259 * with Random defined as:
2260 * opaque Random[MBEDTLS_SERVER_HELLO_RANDOM_LEN];
Jerry Yu1c3e6882022-04-20 21:23:40 +08002261 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002262 MBEDTLS_SSL_CHK_BUF_PTR(p, end, MBEDTLS_SERVER_HELLO_RANDOM_LEN);
2263 if (is_hrr) {
2264 memcpy(p, mbedtls_ssl_tls13_hello_retry_request_magic,
2265 MBEDTLS_SERVER_HELLO_RANDOM_LEN);
2266 } else {
2267 memcpy(p, &ssl->handshake->randbytes[MBEDTLS_CLIENT_HELLO_RANDOM_LEN],
2268 MBEDTLS_SERVER_HELLO_RANDOM_LEN);
Jerry Yu23f7a6f2022-04-23 15:16:45 +08002269 }
Gilles Peskine449bd832023-01-11 14:50:10 +01002270 MBEDTLS_SSL_DEBUG_BUF(3, "server hello, random bytes",
2271 p, MBEDTLS_SERVER_HELLO_RANDOM_LEN);
Jerry Yu3bf2c642022-03-30 22:02:12 +08002272 p += MBEDTLS_SERVER_HELLO_RANDOM_LEN;
2273
Jerry Yucfc04b32022-04-21 09:31:58 +08002274 /* ...
2275 * opaque legacy_session_id_echo<0..32>;
2276 * ...
Jerry Yu3bf2c642022-03-30 22:02:12 +08002277 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002278 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 1 + ssl->session_negotiate->id_len);
2279 *p++ = (unsigned char) ssl->session_negotiate->id_len;
2280 if (ssl->session_negotiate->id_len > 0) {
2281 memcpy(p, &ssl->session_negotiate->id[0],
2282 ssl->session_negotiate->id_len);
Jerry Yu3bf2c642022-03-30 22:02:12 +08002283 p += ssl->session_negotiate->id_len;
Jerry Yu955ddd72022-04-22 22:27:33 +08002284
Gilles Peskine449bd832023-01-11 14:50:10 +01002285 MBEDTLS_SSL_DEBUG_BUF(3, "session id", ssl->session_negotiate->id,
2286 ssl->session_negotiate->id_len);
Jerry Yu3bf2c642022-03-30 22:02:12 +08002287 }
2288
Jerry Yucfc04b32022-04-21 09:31:58 +08002289 /* ...
2290 * CipherSuite cipher_suite;
2291 * ...
2292 * with CipherSuite defined as:
2293 * uint8 CipherSuite[2];
Jerry Yu3bf2c642022-03-30 22:02:12 +08002294 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002295 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 2);
2296 MBEDTLS_PUT_UINT16_BE(ssl->session_negotiate->ciphersuite, p, 0);
Jerry Yu3bf2c642022-03-30 22:02:12 +08002297 p += 2;
Gilles Peskine449bd832023-01-11 14:50:10 +01002298 MBEDTLS_SSL_DEBUG_MSG(3,
2299 ("server hello, chosen ciphersuite: %s ( id=%d )",
2300 mbedtls_ssl_get_ciphersuite_name(
2301 ssl->session_negotiate->ciphersuite),
2302 ssl->session_negotiate->ciphersuite));
Jerry Yu3bf2c642022-03-30 22:02:12 +08002303
Jerry Yucfc04b32022-04-21 09:31:58 +08002304 /* ...
2305 * uint8 legacy_compression_method = 0;
2306 * ...
2307 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002308 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 1);
Thomas Daubney31e03a82022-07-25 15:59:25 +01002309 *p++ = MBEDTLS_SSL_COMPRESS_NULL;
Jerry Yu3bf2c642022-03-30 22:02:12 +08002310
Jerry Yucfc04b32022-04-21 09:31:58 +08002311 /* ...
2312 * Extension extensions<6..2^16-1>;
2313 * ...
2314 * struct {
2315 * ExtensionType extension_type; (2 bytes)
2316 * opaque extension_data<0..2^16-1>;
2317 * } Extension;
2318 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002319 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 2);
Jerry Yud9436a12022-04-20 22:28:09 +08002320 p_extensions_len = p;
Jerry Yu3bf2c642022-03-30 22:02:12 +08002321 p += 2;
2322
Gilles Peskine449bd832023-01-11 14:50:10 +01002323 if ((ret = ssl_tls13_write_server_hello_supported_versions_ext(
2324 ssl, p, end, &output_len)) != 0) {
Jerry Yu955ddd72022-04-22 22:27:33 +08002325 MBEDTLS_SSL_DEBUG_RET(
Gilles Peskine449bd832023-01-11 14:50:10 +01002326 1, "ssl_tls13_write_server_hello_supported_versions_ext", ret);
2327 return ret;
Jerry Yu3bf2c642022-03-30 22:02:12 +08002328 }
2329 p += output_len;
2330
Gilles Peskine449bd832023-01-11 14:50:10 +01002331 if (mbedtls_ssl_tls13_key_exchange_mode_with_ephemeral(ssl)) {
2332 if (is_hrr) {
2333 ret = ssl_tls13_write_hrr_key_share_ext(ssl, p, end, &output_len);
2334 } else {
2335 ret = ssl_tls13_write_key_share_ext(ssl, p, end, &output_len);
2336 }
2337 if (ret != 0) {
2338 return ret;
2339 }
Jerry Yu3bf2c642022-03-30 22:02:12 +08002340 p += output_len;
2341 }
Jerry Yu3bf2c642022-03-30 22:02:12 +08002342
Ronald Cron41a443a2022-10-04 16:38:25 +02002343#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01002344 if (!is_hrr && mbedtls_ssl_tls13_key_exchange_mode_with_psk(ssl)) {
2345 ret = ssl_tls13_write_server_pre_shared_key_ext(ssl, p, end, &output_len);
2346 if (ret != 0) {
2347 MBEDTLS_SSL_DEBUG_RET(1, "ssl_tls13_write_server_pre_shared_key_ext",
2348 ret);
2349 return ret;
Jerry Yu032b15ce2022-07-11 06:10:03 +00002350 }
2351 p += output_len;
2352 }
Ronald Cron41a443a2022-10-04 16:38:25 +02002353#endif
Jerry Yu032b15ce2022-07-11 06:10:03 +00002354
Gilles Peskine449bd832023-01-11 14:50:10 +01002355 MBEDTLS_PUT_UINT16_BE(p - p_extensions_len - 2, p_extensions_len, 0);
Jerry Yu3bf2c642022-03-30 22:02:12 +08002356
Gilles Peskine449bd832023-01-11 14:50:10 +01002357 MBEDTLS_SSL_DEBUG_BUF(4, "server hello extensions",
2358 p_extensions_len, p - p_extensions_len);
Jerry Yu3bf2c642022-03-30 22:02:12 +08002359
Jerry Yud9436a12022-04-20 22:28:09 +08002360 *out_len = p - buf;
Jerry Yu3bf2c642022-03-30 22:02:12 +08002361
Gilles Peskine449bd832023-01-11 14:50:10 +01002362 MBEDTLS_SSL_DEBUG_BUF(3, "server hello", buf, *out_len);
Jerry Yu3bf2c642022-03-30 22:02:12 +08002363
Jerry Yu7de2ff02022-11-08 21:43:46 +08002364 MBEDTLS_SSL_PRINT_EXTS(
Jerry Yu4b8f2f72022-10-31 13:31:22 +08002365 3, is_hrr ? MBEDTLS_SSL_TLS1_3_HS_HELLO_RETRY_REQUEST :
Gilles Peskine449bd832023-01-11 14:50:10 +01002366 MBEDTLS_SSL_HS_SERVER_HELLO,
2367 ssl->handshake->sent_extensions);
Jerry Yu4b8f2f72022-10-31 13:31:22 +08002368
Gilles Peskine449bd832023-01-11 14:50:10 +01002369 return ret;
Jerry Yu56404d72022-03-30 17:36:13 +08002370}
2371
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002372MBEDTLS_CHECK_RETURN_CRITICAL
Xiaokang Qian934ce6f2023-02-06 10:23:04 +00002373static int ssl_tls13_finalize_server_hello(mbedtls_ssl_context *ssl)
Jerry Yue110d252022-05-05 10:19:22 +08002374{
2375 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine449bd832023-01-11 14:50:10 +01002376 ret = mbedtls_ssl_tls13_compute_handshake_transform(ssl);
2377 if (ret != 0) {
2378 MBEDTLS_SSL_DEBUG_RET(1,
2379 "mbedtls_ssl_tls13_compute_handshake_transform",
2380 ret);
2381 return ret;
Jerry Yue110d252022-05-05 10:19:22 +08002382 }
2383
Gilles Peskine449bd832023-01-11 14:50:10 +01002384 return ret;
Jerry Yue110d252022-05-05 10:19:22 +08002385}
2386
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002387MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002388static int ssl_tls13_write_server_hello(mbedtls_ssl_context *ssl)
Jerry Yu5b64ae92022-03-30 17:15:02 +08002389{
Jerry Yu637a3f12022-04-20 21:37:58 +08002390 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yuf4b27e42022-03-30 17:32:21 +08002391 unsigned char *buf;
2392 size_t buf_len, msg_len;
2393
Gilles Peskine449bd832023-01-11 14:50:10 +01002394 MBEDTLS_SSL_DEBUG_MSG(2, ("=> write server hello"));
Jerry Yuf4b27e42022-03-30 17:32:21 +08002395
Gilles Peskine449bd832023-01-11 14:50:10 +01002396 MBEDTLS_SSL_PROC_CHK(ssl_tls13_prepare_server_hello(ssl));
Jerry Yuf4b27e42022-03-30 17:32:21 +08002397
Xiaokang Qian9f1747b2023-03-30 02:50:04 +00002398 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_start_handshake_msg(
2399 ssl, MBEDTLS_SSL_HS_SERVER_HELLO, &buf, &buf_len));
Jerry Yuf4b27e42022-03-30 17:32:21 +08002400
Gilles Peskine449bd832023-01-11 14:50:10 +01002401 MBEDTLS_SSL_PROC_CHK(ssl_tls13_write_server_hello_body(ssl, buf,
2402 buf + buf_len,
2403 &msg_len,
2404 0));
Jerry Yuf4b27e42022-03-30 17:32:21 +08002405
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +01002406 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_add_hs_msg_to_checksum(
Manuel Pégourié-Gonnard43cc1272023-02-06 11:48:19 +01002407 ssl, MBEDTLS_SSL_HS_SERVER_HELLO, buf, msg_len));
Jerry Yuf4b27e42022-03-30 17:32:21 +08002408
Gilles Peskine449bd832023-01-11 14:50:10 +01002409 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_finish_handshake_msg(
2410 ssl, buf_len, msg_len));
Jerry Yu637a3f12022-04-20 21:37:58 +08002411
Xiaokang Qian934ce6f2023-02-06 10:23:04 +00002412 MBEDTLS_SSL_PROC_CHK(ssl_tls13_finalize_server_hello(ssl));
Jerry Yue110d252022-05-05 10:19:22 +08002413
Gabor Mezei7b39bf12022-05-24 16:04:14 +02002414#if defined(MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE)
2415 /* The server sends a dummy change_cipher_spec record immediately
2416 * after its first handshake message. This may either be after
2417 * a ServerHello or a HelloRetryRequest.
2418 */
Gabor Mezei96ae9262022-06-28 11:45:18 +02002419 mbedtls_ssl_handshake_set_state(
Gilles Peskine449bd832023-01-11 14:50:10 +01002420 ssl, MBEDTLS_SSL_SERVER_CCS_AFTER_SERVER_HELLO);
Gabor Mezei7b39bf12022-05-24 16:04:14 +02002421#else
Gilles Peskine449bd832023-01-11 14:50:10 +01002422 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_ENCRYPTED_EXTENSIONS);
Gabor Mezei7b39bf12022-05-24 16:04:14 +02002423#endif /* MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE */
Jerry Yue110d252022-05-05 10:19:22 +08002424
Jerry Yuf4b27e42022-03-30 17:32:21 +08002425cleanup:
2426
Gilles Peskine449bd832023-01-11 14:50:10 +01002427 MBEDTLS_SSL_DEBUG_MSG(2, ("<= write server hello"));
2428 return ret;
Jerry Yu5b64ae92022-03-30 17:15:02 +08002429}
2430
Jerry Yu23d1a252022-05-12 18:08:59 +08002431
2432/*
2433 * Handler for MBEDTLS_SSL_HELLO_RETRY_REQUEST
2434 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002435MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002436static int ssl_tls13_prepare_hello_retry_request(mbedtls_ssl_context *ssl)
Jerry Yu23d1a252022-05-12 18:08:59 +08002437{
2438 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Ronald Cron5fbd2702024-02-14 10:03:36 +01002439 if (ssl->handshake->hello_retry_request_flag) {
Gilles Peskine449bd832023-01-11 14:50:10 +01002440 MBEDTLS_SSL_DEBUG_MSG(1, ("Too many HRRs"));
2441 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE,
2442 MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE);
2443 return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
Jerry Yu23d1a252022-05-12 18:08:59 +08002444 }
2445
2446 /*
2447 * Create stateless transcript hash for HRR
2448 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002449 MBEDTLS_SSL_DEBUG_MSG(4, ("Reset transcript for HRR"));
2450 ret = mbedtls_ssl_reset_transcript_for_hrr(ssl);
2451 if (ret != 0) {
2452 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_reset_transcript_for_hrr", ret);
2453 return ret;
Jerry Yu23d1a252022-05-12 18:08:59 +08002454 }
Gilles Peskine449bd832023-01-11 14:50:10 +01002455 mbedtls_ssl_session_reset_msg_layer(ssl, 0);
Jerry Yu23d1a252022-05-12 18:08:59 +08002456
Gilles Peskine449bd832023-01-11 14:50:10 +01002457 return 0;
Jerry Yu23d1a252022-05-12 18:08:59 +08002458}
2459
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002460MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002461static int ssl_tls13_write_hello_retry_request(mbedtls_ssl_context *ssl)
Jerry Yu23d1a252022-05-12 18:08:59 +08002462{
2463 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
2464 unsigned char *buf;
2465 size_t buf_len, msg_len;
2466
Gilles Peskine449bd832023-01-11 14:50:10 +01002467 MBEDTLS_SSL_DEBUG_MSG(2, ("=> write hello retry request"));
Jerry Yu23d1a252022-05-12 18:08:59 +08002468
Gilles Peskine449bd832023-01-11 14:50:10 +01002469 MBEDTLS_SSL_PROC_CHK(ssl_tls13_prepare_hello_retry_request(ssl));
Jerry Yu23d1a252022-05-12 18:08:59 +08002470
Gilles Peskine449bd832023-01-11 14:50:10 +01002471 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_start_handshake_msg(
2472 ssl, MBEDTLS_SSL_HS_SERVER_HELLO,
2473 &buf, &buf_len));
Jerry Yu23d1a252022-05-12 18:08:59 +08002474
Gilles Peskine449bd832023-01-11 14:50:10 +01002475 MBEDTLS_SSL_PROC_CHK(ssl_tls13_write_server_hello_body(ssl, buf,
2476 buf + buf_len,
2477 &msg_len,
2478 1));
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +01002479 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_add_hs_msg_to_checksum(
Manuel Pégourié-Gonnard43cc1272023-02-06 11:48:19 +01002480 ssl, MBEDTLS_SSL_HS_SERVER_HELLO, buf, msg_len));
Jerry Yu23d1a252022-05-12 18:08:59 +08002481
2482
Gilles Peskine449bd832023-01-11 14:50:10 +01002483 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_finish_handshake_msg(ssl, buf_len,
2484 msg_len));
Jerry Yu23d1a252022-05-12 18:08:59 +08002485
Ronald Cron5fbd2702024-02-14 10:03:36 +01002486 ssl->handshake->hello_retry_request_flag = 1;
Jerry Yu23d1a252022-05-12 18:08:59 +08002487
Gabor Mezei7b39bf12022-05-24 16:04:14 +02002488#if defined(MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE)
2489 /* The server sends a dummy change_cipher_spec record immediately
2490 * after its first handshake message. This may either be after
2491 * a ServerHello or a HelloRetryRequest.
2492 */
Gabor Mezei96ae9262022-06-28 11:45:18 +02002493 mbedtls_ssl_handshake_set_state(
Gilles Peskine449bd832023-01-11 14:50:10 +01002494 ssl, MBEDTLS_SSL_SERVER_CCS_AFTER_HELLO_RETRY_REQUEST);
Gabor Mezei7b39bf12022-05-24 16:04:14 +02002495#else
Gilles Peskine449bd832023-01-11 14:50:10 +01002496 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_CLIENT_HELLO);
Gabor Mezei7b39bf12022-05-24 16:04:14 +02002497#endif /* MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE */
Jerry Yu23d1a252022-05-12 18:08:59 +08002498
2499cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +01002500 MBEDTLS_SSL_DEBUG_MSG(2, ("<= write hello retry request"));
2501 return ret;
Jerry Yu23d1a252022-05-12 18:08:59 +08002502}
2503
Jerry Yu5b64ae92022-03-30 17:15:02 +08002504/*
Jerry Yu4d3841a2022-04-16 12:37:19 +08002505 * Handler for MBEDTLS_SSL_ENCRYPTED_EXTENSIONS
2506 */
Jerry Yu4d3841a2022-04-16 12:37:19 +08002507
2508/*
2509 * struct {
2510 * Extension extensions<0..2 ^ 16 - 1>;
2511 * } EncryptedExtensions;
2512 *
2513 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002514MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002515static int ssl_tls13_write_encrypted_extensions_body(mbedtls_ssl_context *ssl,
2516 unsigned char *buf,
2517 unsigned char *end,
2518 size_t *out_len)
Jerry Yu4d3841a2022-04-16 12:37:19 +08002519{
XiaokangQianacb39922022-06-17 10:18:48 +00002520 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yu4d3841a2022-04-16 12:37:19 +08002521 unsigned char *p = buf;
2522 size_t extensions_len = 0;
Jerry Yu8937eb42022-05-03 12:12:14 +08002523 unsigned char *p_extensions_len;
XiaokangQianacb39922022-06-17 10:18:48 +00002524 size_t output_len;
Jerry Yu9da5e5a2022-05-03 15:46:09 +08002525
Jerry Yu4d3841a2022-04-16 12:37:19 +08002526 *out_len = 0;
2527
Gilles Peskine449bd832023-01-11 14:50:10 +01002528 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 2);
Jerry Yu8937eb42022-05-03 12:12:14 +08002529 p_extensions_len = p;
Jerry Yu4d3841a2022-04-16 12:37:19 +08002530 p += 2;
2531
Jerry Yu8937eb42022-05-03 12:12:14 +08002532 ((void) ssl);
XiaokangQianacb39922022-06-17 10:18:48 +00002533 ((void) ret);
2534 ((void) output_len);
2535
2536#if defined(MBEDTLS_SSL_ALPN)
Gilles Peskine449bd832023-01-11 14:50:10 +01002537 ret = mbedtls_ssl_write_alpn_ext(ssl, p, end, &output_len);
2538 if (ret != 0) {
2539 return ret;
2540 }
XiaokangQian95d5f542022-06-24 02:29:26 +00002541 p += output_len;
XiaokangQianacb39922022-06-17 10:18:48 +00002542#endif /* MBEDTLS_SSL_ALPN */
Jerry Yu4d3841a2022-04-16 12:37:19 +08002543
Jerry Yu71c14f12022-12-12 11:10:35 +08002544#if defined(MBEDTLS_SSL_EARLY_DATA)
Ronald Cron78a38f62024-02-01 18:30:31 +01002545 if (ssl->handshake->early_data_accepted) {
Jerry Yu52335392023-11-23 18:06:06 +08002546 ret = mbedtls_ssl_tls13_write_early_data_ext(
Jerry Yuc59c5862023-12-05 10:40:49 +08002547 ssl, 0, p, end, &output_len);
Jerry Yu71c14f12022-12-12 11:10:35 +08002548 if (ret != 0) {
2549 return ret;
2550 }
2551 p += output_len;
2552 }
2553#endif /* MBEDTLS_SSL_EARLY_DATA */
2554
Waleed Elmelegy47d29462024-01-03 17:31:52 +00002555#if defined(MBEDTLS_SSL_RECORD_SIZE_LIMIT)
Waleed Elmelegyfbe42742024-01-05 18:11:10 +00002556 if (ssl->handshake->received_extensions & MBEDTLS_SSL_EXT_MASK(RECORD_SIZE_LIMIT)) {
Waleed Elmelegyd2fc90e2024-01-04 18:04:53 +00002557 ret = mbedtls_ssl_tls13_write_record_size_limit_ext(
2558 ssl, p, end, &output_len);
2559 if (ret != 0) {
2560 return ret;
2561 }
2562 p += output_len;
Waleed Elmelegy47d29462024-01-03 17:31:52 +00002563 }
Waleed Elmelegy47d29462024-01-03 17:31:52 +00002564#endif
2565
Gilles Peskine449bd832023-01-11 14:50:10 +01002566 extensions_len = (p - p_extensions_len) - 2;
2567 MBEDTLS_PUT_UINT16_BE(extensions_len, p_extensions_len, 0);
Jerry Yu8937eb42022-05-03 12:12:14 +08002568
2569 *out_len = p - buf;
Jerry Yu4d3841a2022-04-16 12:37:19 +08002570
Gilles Peskine449bd832023-01-11 14:50:10 +01002571 MBEDTLS_SSL_DEBUG_BUF(4, "encrypted extensions", buf, *out_len);
Jerry Yu4d3841a2022-04-16 12:37:19 +08002572
Jerry Yu7de2ff02022-11-08 21:43:46 +08002573 MBEDTLS_SSL_PRINT_EXTS(
Gilles Peskine449bd832023-01-11 14:50:10 +01002574 3, MBEDTLS_SSL_HS_ENCRYPTED_EXTENSIONS, ssl->handshake->sent_extensions);
Jerry Yu4b8f2f72022-10-31 13:31:22 +08002575
Gilles Peskine449bd832023-01-11 14:50:10 +01002576 return 0;
Jerry Yu4d3841a2022-04-16 12:37:19 +08002577}
2578
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002579MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002580static int ssl_tls13_write_encrypted_extensions(mbedtls_ssl_context *ssl)
Jerry Yu4d3841a2022-04-16 12:37:19 +08002581{
2582 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
2583 unsigned char *buf;
Jerry Yu39730a72022-05-03 12:14:04 +08002584 size_t buf_len, msg_len;
Jerry Yu4d3841a2022-04-16 12:37:19 +08002585
Gilles Peskine449bd832023-01-11 14:50:10 +01002586 mbedtls_ssl_set_outbound_transform(ssl,
2587 ssl->handshake->transform_handshake);
Gabor Mezei54719122022-06-28 11:34:56 +02002588 MBEDTLS_SSL_DEBUG_MSG(
Gilles Peskine449bd832023-01-11 14:50:10 +01002589 3, ("switching to handshake transform for outbound data"));
Gabor Mezei54719122022-06-28 11:34:56 +02002590
Gilles Peskine449bd832023-01-11 14:50:10 +01002591 MBEDTLS_SSL_DEBUG_MSG(2, ("=> write encrypted extensions"));
Jerry Yu4d3841a2022-04-16 12:37:19 +08002592
Xiaokang Qian9f1747b2023-03-30 02:50:04 +00002593 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_start_handshake_msg(
2594 ssl, MBEDTLS_SSL_HS_ENCRYPTED_EXTENSIONS,
2595 &buf, &buf_len));
Jerry Yu4d3841a2022-04-16 12:37:19 +08002596
Gilles Peskine449bd832023-01-11 14:50:10 +01002597 MBEDTLS_SSL_PROC_CHK(ssl_tls13_write_encrypted_extensions_body(
2598 ssl, buf, buf + buf_len, &msg_len));
Jerry Yu4d3841a2022-04-16 12:37:19 +08002599
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +01002600 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_add_hs_msg_to_checksum(
Xiaokang Qian9f1747b2023-03-30 02:50:04 +00002601 ssl, MBEDTLS_SSL_HS_ENCRYPTED_EXTENSIONS,
2602 buf, msg_len));
Jerry Yu4d3841a2022-04-16 12:37:19 +08002603
Gilles Peskine449bd832023-01-11 14:50:10 +01002604 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_finish_handshake_msg(
2605 ssl, buf_len, msg_len));
Jerry Yu4d3841a2022-04-16 12:37:19 +08002606
Ronald Cron928cbd32022-10-04 16:14:26 +02002607#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01002608 if (mbedtls_ssl_tls13_key_exchange_mode_with_psk(ssl)) {
2609 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_SERVER_FINISHED);
2610 } else {
2611 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_CERTIFICATE_REQUEST);
2612 }
Jerry Yu8937eb42022-05-03 12:12:14 +08002613#else
Gilles Peskine449bd832023-01-11 14:50:10 +01002614 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_SERVER_FINISHED);
Jerry Yu8937eb42022-05-03 12:12:14 +08002615#endif
2616
Jerry Yu4d3841a2022-04-16 12:37:19 +08002617cleanup:
2618
Gilles Peskine449bd832023-01-11 14:50:10 +01002619 MBEDTLS_SSL_DEBUG_MSG(2, ("<= write encrypted extensions"));
2620 return ret;
Jerry Yu4d3841a2022-04-16 12:37:19 +08002621}
2622
Ronald Cron928cbd32022-10-04 16:14:26 +02002623#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED)
XiaokangQiana987e1d2022-05-07 01:25:58 +00002624#define SSL_CERTIFICATE_REQUEST_SEND_REQUEST 0
2625#define SSL_CERTIFICATE_REQUEST_SKIP 1
2626/* Coordination:
2627 * Check whether a CertificateRequest message should be written.
2628 * Returns a negative code on failure, or
2629 * - SSL_CERTIFICATE_REQUEST_SEND_REQUEST
2630 * - SSL_CERTIFICATE_REQUEST_SKIP
2631 * indicating if the writing of the CertificateRequest
2632 * should be skipped or not.
2633 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002634MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002635static int ssl_tls13_certificate_request_coordinate(mbedtls_ssl_context *ssl)
XiaokangQiana987e1d2022-05-07 01:25:58 +00002636{
2637 int authmode;
2638
XiaokangQian40a35232022-05-07 09:02:40 +00002639#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Gilles Peskine449bd832023-01-11 14:50:10 +01002640 if (ssl->handshake->sni_authmode != MBEDTLS_SSL_VERIFY_UNSET) {
XiaokangQian40a35232022-05-07 09:02:40 +00002641 authmode = ssl->handshake->sni_authmode;
Gilles Peskine449bd832023-01-11 14:50:10 +01002642 } else
XiaokangQian40a35232022-05-07 09:02:40 +00002643#endif
XiaokangQiana987e1d2022-05-07 01:25:58 +00002644 authmode = ssl->conf->authmode;
2645
Gilles Peskine449bd832023-01-11 14:50:10 +01002646 if (authmode == MBEDTLS_SSL_VERIFY_NONE) {
Ronald Croneac00ad2022-09-13 10:16:31 +02002647 ssl->session_negotiate->verify_result = MBEDTLS_X509_BADCERT_SKIP_VERIFY;
Gilles Peskine449bd832023-01-11 14:50:10 +01002648 return SSL_CERTIFICATE_REQUEST_SKIP;
Ronald Croneac00ad2022-09-13 10:16:31 +02002649 }
XiaokangQiana987e1d2022-05-07 01:25:58 +00002650
XiaokangQianc3017f62022-05-13 05:55:41 +00002651 ssl->handshake->certificate_request_sent = 1;
XiaokangQian189ded22022-05-10 08:12:17 +00002652
Gilles Peskine449bd832023-01-11 14:50:10 +01002653 return SSL_CERTIFICATE_REQUEST_SEND_REQUEST;
XiaokangQiana987e1d2022-05-07 01:25:58 +00002654}
2655
XiaokangQiancec9ae62022-05-06 07:28:50 +00002656/*
2657 * struct {
2658 * opaque certificate_request_context<0..2^8-1>;
2659 * Extension extensions<2..2^16-1>;
2660 * } CertificateRequest;
2661 *
2662 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002663MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002664static int ssl_tls13_write_certificate_request_body(mbedtls_ssl_context *ssl,
2665 unsigned char *buf,
2666 const unsigned char *end,
2667 size_t *out_len)
XiaokangQiancec9ae62022-05-06 07:28:50 +00002668{
2669 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
2670 unsigned char *p = buf;
XiaokangQianec6efb92022-05-06 09:53:10 +00002671 size_t output_len = 0;
XiaokangQiancec9ae62022-05-06 07:28:50 +00002672 unsigned char *p_extensions_len;
2673
2674 *out_len = 0;
2675
2676 /* Check if we have enough space:
2677 * - certificate_request_context (1 byte)
2678 * - extensions length (2 bytes)
2679 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002680 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 3);
XiaokangQiancec9ae62022-05-06 07:28:50 +00002681
2682 /*
2683 * Write certificate_request_context
2684 */
2685 /*
2686 * We use a zero length context for the normal handshake
2687 * messages. For post-authentication handshake messages
2688 * this request context would be set to a non-zero value.
2689 */
2690 *p++ = 0x0;
2691
2692 /*
2693 * Write extensions
2694 */
2695 /* The extensions must contain the signature_algorithms. */
2696 p_extensions_len = p;
2697 p += 2;
Gilles Peskine449bd832023-01-11 14:50:10 +01002698 ret = mbedtls_ssl_write_sig_alg_ext(ssl, p, end, &output_len);
2699 if (ret != 0) {
2700 return ret;
2701 }
XiaokangQiancec9ae62022-05-06 07:28:50 +00002702
XiaokangQianec6efb92022-05-06 09:53:10 +00002703 p += output_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01002704 MBEDTLS_PUT_UINT16_BE(p - p_extensions_len - 2, p_extensions_len, 0);
XiaokangQiancec9ae62022-05-06 07:28:50 +00002705
2706 *out_len = p - buf;
2707
Jerry Yu7de2ff02022-11-08 21:43:46 +08002708 MBEDTLS_SSL_PRINT_EXTS(
Gilles Peskine449bd832023-01-11 14:50:10 +01002709 3, MBEDTLS_SSL_HS_CERTIFICATE_REQUEST, ssl->handshake->sent_extensions);
Jerry Yu4b8f2f72022-10-31 13:31:22 +08002710
Gilles Peskine449bd832023-01-11 14:50:10 +01002711 return 0;
XiaokangQiancec9ae62022-05-06 07:28:50 +00002712}
2713
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002714MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002715static int ssl_tls13_write_certificate_request(mbedtls_ssl_context *ssl)
XiaokangQiancec9ae62022-05-06 07:28:50 +00002716{
2717 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
2718
Gilles Peskine449bd832023-01-11 14:50:10 +01002719 MBEDTLS_SSL_DEBUG_MSG(2, ("=> write certificate request"));
XiaokangQiancec9ae62022-05-06 07:28:50 +00002720
Gilles Peskine449bd832023-01-11 14:50:10 +01002721 MBEDTLS_SSL_PROC_CHK_NEG(ssl_tls13_certificate_request_coordinate(ssl));
XiaokangQiancec9ae62022-05-06 07:28:50 +00002722
Gilles Peskine449bd832023-01-11 14:50:10 +01002723 if (ret == SSL_CERTIFICATE_REQUEST_SEND_REQUEST) {
XiaokangQiancec9ae62022-05-06 07:28:50 +00002724 unsigned char *buf;
2725 size_t buf_len, msg_len;
2726
Xiaokang Qian9f1747b2023-03-30 02:50:04 +00002727 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_start_handshake_msg(
2728 ssl, MBEDTLS_SSL_HS_CERTIFICATE_REQUEST,
2729 &buf, &buf_len));
XiaokangQiancec9ae62022-05-06 07:28:50 +00002730
Gilles Peskine449bd832023-01-11 14:50:10 +01002731 MBEDTLS_SSL_PROC_CHK(ssl_tls13_write_certificate_request_body(
2732 ssl, buf, buf + buf_len, &msg_len));
XiaokangQiancec9ae62022-05-06 07:28:50 +00002733
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +01002734 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_add_hs_msg_to_checksum(
Xiaokang Qian9f1747b2023-03-30 02:50:04 +00002735 ssl, MBEDTLS_SSL_HS_CERTIFICATE_REQUEST,
2736 buf, msg_len));
XiaokangQiancec9ae62022-05-06 07:28:50 +00002737
Gilles Peskine449bd832023-01-11 14:50:10 +01002738 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_finish_handshake_msg(
2739 ssl, buf_len, msg_len));
2740 } else if (ret == SSL_CERTIFICATE_REQUEST_SKIP) {
2741 MBEDTLS_SSL_DEBUG_MSG(2, ("<= skip write certificate request"));
XiaokangQiancec9ae62022-05-06 07:28:50 +00002742 ret = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01002743 } else {
2744 MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
XiaokangQiancec9ae62022-05-06 07:28:50 +00002745 ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
2746 goto cleanup;
2747 }
2748
Gilles Peskine449bd832023-01-11 14:50:10 +01002749 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_SERVER_CERTIFICATE);
XiaokangQiancec9ae62022-05-06 07:28:50 +00002750cleanup:
2751
Gilles Peskine449bd832023-01-11 14:50:10 +01002752 MBEDTLS_SSL_DEBUG_MSG(2, ("<= write certificate request"));
2753 return ret;
XiaokangQiancec9ae62022-05-06 07:28:50 +00002754}
XiaokangQiancec9ae62022-05-06 07:28:50 +00002755
Jerry Yu4d3841a2022-04-16 12:37:19 +08002756/*
Jerry Yuc6e6dbf2022-04-16 19:42:57 +08002757 * Handler for MBEDTLS_SSL_SERVER_CERTIFICATE
Jerry Yu83da34e2022-04-16 13:59:52 +08002758 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002759MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002760static int ssl_tls13_write_server_certificate(mbedtls_ssl_context *ssl)
Jerry Yu83da34e2022-04-16 13:59:52 +08002761{
Jerry Yu5a26f302022-05-10 20:46:40 +08002762 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
XiaokangQianfb665a82022-06-15 03:57:21 +00002763
2764#if defined(MBEDTLS_X509_CRT_PARSE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01002765 if ((ssl_tls13_pick_key_cert(ssl) != 0) ||
2766 mbedtls_ssl_own_cert(ssl) == NULL) {
2767 MBEDTLS_SSL_DEBUG_MSG(2, ("No certificate available."));
2768 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE,
2769 MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE);
2770 return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
Jerry Yu5a26f302022-05-10 20:46:40 +08002771 }
XiaokangQianfb665a82022-06-15 03:57:21 +00002772#endif /* MBEDTLS_X509_CRT_PARSE_C */
Jerry Yu5a26f302022-05-10 20:46:40 +08002773
Gilles Peskine449bd832023-01-11 14:50:10 +01002774 ret = mbedtls_ssl_tls13_write_certificate(ssl);
2775 if (ret != 0) {
2776 return ret;
2777 }
2778 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_CERTIFICATE_VERIFY);
2779 return 0;
Jerry Yu83da34e2022-04-16 13:59:52 +08002780}
2781
2782/*
Jerry Yuc6e6dbf2022-04-16 19:42:57 +08002783 * Handler for MBEDTLS_SSL_CERTIFICATE_VERIFY
Jerry Yu83da34e2022-04-16 13:59:52 +08002784 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002785MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002786static int ssl_tls13_write_certificate_verify(mbedtls_ssl_context *ssl)
Jerry Yu83da34e2022-04-16 13:59:52 +08002787{
Gilles Peskine449bd832023-01-11 14:50:10 +01002788 int ret = mbedtls_ssl_tls13_write_certificate_verify(ssl);
2789 if (ret != 0) {
2790 return ret;
2791 }
2792 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_SERVER_FINISHED);
2793 return 0;
Jerry Yu83da34e2022-04-16 13:59:52 +08002794}
Ronald Cron928cbd32022-10-04 16:14:26 +02002795#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED */
Jerry Yu83da34e2022-04-16 13:59:52 +08002796
Jerry Yu9b72e392023-12-01 16:27:08 +08002797/*
2798 * RFC 8446 section A.2
2799 *
2800 * | Send ServerHello
2801 * | K_send = handshake
2802 * | Send EncryptedExtensions
2803 * | [Send CertificateRequest]
2804 * Can send | [Send Certificate + CertificateVerify]
2805 * app data | Send Finished
2806 * after --> | K_send = application
2807 * here +--------+--------+
2808 * No 0-RTT | | 0-RTT
2809 * | |
2810 * K_recv = handshake | | K_recv = early data
2811 * [Skip decrypt errors] | +------> WAIT_EOED -+
2812 * | | Recv | | Recv EndOfEarlyData
2813 * | | early data | | K_recv = handshake
2814 * | +------------+ |
2815 * | |
2816 * +> WAIT_FLIGHT2 <--------+
2817 * |
2818 * +--------+--------+
2819 * No auth | | Client auth
2820 * | |
2821 * | v
2822 * | WAIT_CERT
2823 * | Recv | | Recv Certificate
2824 * | empty | v
2825 * | Certificate | WAIT_CV
2826 * | | | Recv
2827 * | v | CertificateVerify
2828 * +-> WAIT_FINISHED <---+
2829 * | Recv Finished
2830 *
2831 *
2832 * The following function handles the state changes after WAIT_FLIGHT2 in the
Jerry Yu3be85072023-12-04 09:58:54 +08002833 * above diagram. We are not going to receive early data related messages
2834 * anymore, prepare to receive the first handshake message of the client
2835 * second flight.
Jerry Yu9b72e392023-12-01 16:27:08 +08002836 */
Jerry Yu3be85072023-12-04 09:58:54 +08002837static void ssl_tls13_prepare_for_handshake_second_flight(
2838 mbedtls_ssl_context *ssl)
Jerry Yu9b72e392023-12-01 16:27:08 +08002839{
Jerry Yu9b72e392023-12-01 16:27:08 +08002840 if (ssl->handshake->certificate_request_sent) {
2841 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_CLIENT_CERTIFICATE);
2842 } else {
Jerry Yu42020fb2023-12-05 17:35:53 +08002843 MBEDTLS_SSL_DEBUG_MSG(2, ("skip parse certificate"));
Jerry Yuebb1b1d2023-12-05 11:02:15 +08002844 MBEDTLS_SSL_DEBUG_MSG(2, ("skip parse certificate verify"));
Jerry Yuebb1b1d2023-12-05 11:02:15 +08002845
Jerry Yu9b72e392023-12-01 16:27:08 +08002846 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_CLIENT_FINISHED);
2847 }
Jerry Yu9b72e392023-12-01 16:27:08 +08002848}
2849
Jerry Yu83da34e2022-04-16 13:59:52 +08002850/*
Jerry Yud6e253d2022-05-18 13:59:24 +08002851 * Handler for MBEDTLS_SSL_SERVER_FINISHED
Jerry Yu69dd8d42022-04-16 12:51:26 +08002852 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002853MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002854static int ssl_tls13_write_server_finished(mbedtls_ssl_context *ssl)
Jerry Yu69dd8d42022-04-16 12:51:26 +08002855{
Jerry Yud6e253d2022-05-18 13:59:24 +08002856 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yu27bdc7c2022-04-16 13:33:27 +08002857
Gilles Peskine449bd832023-01-11 14:50:10 +01002858 ret = mbedtls_ssl_tls13_write_finished_message(ssl);
2859 if (ret != 0) {
2860 return ret;
2861 }
Jerry Yu27bdc7c2022-04-16 13:33:27 +08002862
Gilles Peskine449bd832023-01-11 14:50:10 +01002863 ret = mbedtls_ssl_tls13_compute_application_transform(ssl);
2864 if (ret != 0) {
Jerry Yue3d67cb2022-05-19 15:33:10 +08002865 MBEDTLS_SSL_PEND_FATAL_ALERT(
Gilles Peskine449bd832023-01-11 14:50:10 +01002866 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE,
2867 MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE);
2868 return ret;
Jerry Yue3d67cb2022-05-19 15:33:10 +08002869 }
XiaokangQianc3017f62022-05-13 05:55:41 +00002870
Jerry Yu87b5ed42022-12-12 13:07:07 +08002871#if defined(MBEDTLS_SSL_EARLY_DATA)
Ronald Cron78a38f62024-02-01 18:30:31 +01002872 if (ssl->handshake->early_data_accepted) {
Jerry Yu0af63dc2023-12-01 17:14:51 +08002873 /* See RFC 8446 section A.2 for more information */
Jerry Yu87b5ed42022-12-12 13:07:07 +08002874 MBEDTLS_SSL_DEBUG_MSG(
2875 1, ("Switch to early keys for inbound traffic. "
2876 "( K_recv = early data )"));
2877 mbedtls_ssl_set_inbound_transform(
2878 ssl, ssl->handshake->transform_earlydata);
2879 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_END_OF_EARLY_DATA);
2880 return 0;
2881 }
2882#endif /* MBEDTLS_SSL_EARLY_DATA */
Jerry Yu0af63dc2023-12-01 17:14:51 +08002883 MBEDTLS_SSL_DEBUG_MSG(
2884 1, ("Switch to handshake keys for inbound traffic "
2885 "( K_recv = handshake )"));
Gilles Peskine449bd832023-01-11 14:50:10 +01002886 mbedtls_ssl_set_inbound_transform(ssl, ssl->handshake->transform_handshake);
XiaokangQian189ded22022-05-10 08:12:17 +00002887
Jerry Yu3be85072023-12-04 09:58:54 +08002888 ssl_tls13_prepare_for_handshake_second_flight(ssl);
Jerry Yu7d8c3fe2022-12-12 12:59:44 +08002889
2890 return 0;
2891}
2892
Jerry Yu87b5ed42022-12-12 13:07:07 +08002893#if defined(MBEDTLS_SSL_EARLY_DATA)
2894/*
Jerry Yu59d420f2023-12-01 16:30:34 +08002895 * Handler for MBEDTLS_SSL_END_OF_EARLY_DATA
Jerry Yu87b5ed42022-12-12 13:07:07 +08002896 */
Jerry Yu3be85072023-12-04 09:58:54 +08002897#define SSL_GOT_END_OF_EARLY_DATA 0
Jerry Yub55f9eb2023-12-05 10:27:17 +08002898#define SSL_GOT_EARLY_DATA 1
Jerry Yud5c34962023-12-01 16:32:31 +08002899/* Coordination:
Jerry Yu3be85072023-12-04 09:58:54 +08002900 * Deals with the ambiguity of not knowing if the next message is an
2901 * EndOfEarlyData message or an application message containing early data.
Jerry Yud5c34962023-12-01 16:32:31 +08002902 * Returns a negative code on failure, or
Jerry Yu3be85072023-12-04 09:58:54 +08002903 * - SSL_GOT_END_OF_EARLY_DATA
Jerry Yub55f9eb2023-12-05 10:27:17 +08002904 * - SSL_GOT_EARLY_DATA
Jerry Yud5c34962023-12-01 16:32:31 +08002905 * indicating which message is received.
2906 */
2907MBEDTLS_CHECK_RETURN_CRITICAL
2908static int ssl_tls13_end_of_early_data_coordinate(mbedtls_ssl_context *ssl)
2909{
2910 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yub4ed4602023-12-01 16:34:00 +08002911
2912 if ((ret = mbedtls_ssl_read_record(ssl, 0)) != 0) {
2913 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_read_record", ret);
2914 return ret;
2915 }
2916 ssl->keep_current_message = 1;
2917
2918 if (ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
2919 ssl->in_msg[0] == MBEDTLS_SSL_HS_END_OF_EARLY_DATA) {
Jerry Yub55f9eb2023-12-05 10:27:17 +08002920 MBEDTLS_SSL_DEBUG_MSG(3, ("Received an end_of_early_data message."));
Jerry Yu3be85072023-12-04 09:58:54 +08002921 return SSL_GOT_END_OF_EARLY_DATA;
Jerry Yub4ed4602023-12-01 16:34:00 +08002922 }
2923
2924 if (ssl->in_msgtype == MBEDTLS_SSL_MSG_APPLICATION_DATA) {
Jerry Yub55f9eb2023-12-05 10:27:17 +08002925 MBEDTLS_SSL_DEBUG_MSG(3, ("Received early data"));
Jerry Yu6a5904d2023-12-06 17:11:12 +08002926 /* RFC 8446 section 4.6.1
2927 *
2928 * A server receiving more than max_early_data_size bytes of 0-RTT data
2929 * SHOULD terminate the connection with an "unexpected_message" alert.
2930 *
2931 * TODO: Add received data size check here.
2932 */
Ronald Croned7d4bf2024-01-31 07:55:19 +01002933 if (ssl->in_offt == NULL) {
2934 /* Set the reading pointer */
2935 ssl->in_offt = ssl->in_msg;
2936 }
Jerry Yub55f9eb2023-12-05 10:27:17 +08002937 return SSL_GOT_EARLY_DATA;
Jerry Yub4ed4602023-12-01 16:34:00 +08002938 }
2939
Jerry Yu7bb40a32023-12-04 10:04:15 +08002940 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE,
2941 MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE);
Jerry Yub4ed4602023-12-01 16:34:00 +08002942 return MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE;
Jerry Yud5c34962023-12-01 16:32:31 +08002943}
2944
2945MBEDTLS_CHECK_RETURN_CRITICAL
2946static int ssl_tls13_parse_end_of_early_data(mbedtls_ssl_context *ssl,
2947 const unsigned char *buf,
2948 const unsigned char *end)
2949{
Jerry Yu75c9ab72023-12-01 16:41:40 +08002950 /* RFC 8446 section 4.5
2951 *
2952 * struct {} EndOfEarlyData;
2953 */
Jerry Yufbf03992023-12-04 10:00:37 +08002954 if (buf != end) {
2955 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR,
2956 MBEDTLS_ERR_SSL_DECODE_ERROR);
2957 return MBEDTLS_ERR_SSL_DECODE_ERROR;
2958 }
2959 return 0;
Jerry Yud5c34962023-12-01 16:32:31 +08002960}
2961
Jerry Yud5c34962023-12-01 16:32:31 +08002962/*
2963 * RFC 8446 section A.2
2964 *
2965 * | Send ServerHello
2966 * | K_send = handshake
2967 * | Send EncryptedExtensions
2968 * | [Send CertificateRequest]
2969 * Can send | [Send Certificate + CertificateVerify]
2970 * app data | Send Finished
2971 * after --> | K_send = application
2972 * here +--------+--------+
2973 * No 0-RTT | | 0-RTT
2974 * | |
2975 * K_recv = handshake | | K_recv = early data
2976 * [Skip decrypt errors] | +------> WAIT_EOED -+
2977 * | | Recv | | Recv EndOfEarlyData
2978 * | | early data | | K_recv = handshake
2979 * | +------------+ |
2980 * | |
2981 * +> WAIT_FLIGHT2 <--------+
2982 * |
2983 * +--------+--------+
2984 * No auth | | Client auth
2985 * | |
2986 * | v
2987 * | WAIT_CERT
2988 * | Recv | | Recv Certificate
2989 * | empty | v
2990 * | Certificate | WAIT_CV
2991 * | | | Recv
2992 * | v | CertificateVerify
2993 * +-> WAIT_FINISHED <---+
2994 * | Recv Finished
2995 *
2996 * The function handles actions and state changes from 0-RTT to WAIT_FLIGHT2 in
2997 * the above diagram.
2998 */
Jerry Yu87b5ed42022-12-12 13:07:07 +08002999MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yu59d420f2023-12-01 16:30:34 +08003000static int ssl_tls13_process_end_of_early_data(mbedtls_ssl_context *ssl)
Jerry Yu87b5ed42022-12-12 13:07:07 +08003001{
3002 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yud5c34962023-12-01 16:32:31 +08003003
3004 MBEDTLS_SSL_DEBUG_MSG(2, ("=> ssl_tls13_process_end_of_early_data"));
3005
3006 MBEDTLS_SSL_PROC_CHK_NEG(ssl_tls13_end_of_early_data_coordinate(ssl));
3007
Jerry Yu3be85072023-12-04 09:58:54 +08003008 if (ret == SSL_GOT_END_OF_EARLY_DATA) {
Jerry Yud5c34962023-12-01 16:32:31 +08003009 unsigned char *buf;
3010 size_t buf_len;
3011
3012 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_tls13_fetch_handshake_msg(
3013 ssl, MBEDTLS_SSL_HS_END_OF_EARLY_DATA,
3014 &buf, &buf_len));
3015
3016 MBEDTLS_SSL_PROC_CHK(ssl_tls13_parse_end_of_early_data(
3017 ssl, buf, buf + buf_len));
3018
Jerry Yue9655122023-12-01 16:44:40 +08003019 MBEDTLS_SSL_DEBUG_MSG(
3020 1, ("Switch to handshake keys for inbound traffic"
3021 "( K_recv = handshake )"));
3022 mbedtls_ssl_set_inbound_transform(
3023 ssl, ssl->handshake->transform_handshake);
3024
Jerry Yud5c34962023-12-01 16:32:31 +08003025 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_add_hs_msg_to_checksum(
3026 ssl, MBEDTLS_SSL_HS_END_OF_EARLY_DATA,
3027 buf, buf_len));
Jerry Yue9655122023-12-01 16:44:40 +08003028
Jerry Yu3be85072023-12-04 09:58:54 +08003029 ssl_tls13_prepare_for_handshake_second_flight(ssl);
Jerry Yud5c34962023-12-01 16:32:31 +08003030
Jerry Yub55f9eb2023-12-05 10:27:17 +08003031 } else if (ret == SSL_GOT_EARLY_DATA) {
Jerry Yud9ca3542023-12-06 17:23:52 +08003032 ret = MBEDTLS_ERR_SSL_RECEIVED_EARLY_DATA;
3033 goto cleanup;
Jerry Yud5c34962023-12-01 16:32:31 +08003034 } else {
3035 MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
3036 ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
3037 goto cleanup;
3038 }
3039
Jerry Yud5c34962023-12-01 16:32:31 +08003040cleanup:
3041 MBEDTLS_SSL_DEBUG_MSG(2, ("<= ssl_tls13_process_end_of_early_data"));
Jerry Yu87b5ed42022-12-12 13:07:07 +08003042 return ret;
3043}
3044#endif /* MBEDTLS_SSL_EARLY_DATA */
3045
Jerry Yu69dd8d42022-04-16 12:51:26 +08003046/*
Jerry Yud6e253d2022-05-18 13:59:24 +08003047 * Handler for MBEDTLS_SSL_CLIENT_FINISHED
Jerry Yu69dd8d42022-04-16 12:51:26 +08003048 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02003049MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01003050static int ssl_tls13_process_client_finished(mbedtls_ssl_context *ssl)
Jerry Yu69dd8d42022-04-16 12:51:26 +08003051{
Jerry Yud6e253d2022-05-18 13:59:24 +08003052 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
3053
Gilles Peskine449bd832023-01-11 14:50:10 +01003054 ret = mbedtls_ssl_tls13_process_finished_message(ssl);
3055 if (ret != 0) {
3056 return ret;
Jerry Yue3d67cb2022-05-19 15:33:10 +08003057 }
3058
Gilles Peskine449bd832023-01-11 14:50:10 +01003059 ret = mbedtls_ssl_tls13_compute_resumption_master_secret(ssl);
3060 if (ret != 0) {
Xiaokang Qian9f1747b2023-03-30 02:50:04 +00003061 MBEDTLS_SSL_DEBUG_RET(
3062 1, "mbedtls_ssl_tls13_compute_resumption_master_secret", ret);
Gilles Peskine449bd832023-01-11 14:50:10 +01003063 }
3064
3065 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_HANDSHAKE_WRAPUP);
3066 return 0;
Jerry Yu69dd8d42022-04-16 12:51:26 +08003067}
3068
3069/*
Jerry Yud6e253d2022-05-18 13:59:24 +08003070 * Handler for MBEDTLS_SSL_HANDSHAKE_WRAPUP
Jerry Yu69dd8d42022-04-16 12:51:26 +08003071 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02003072MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01003073static int ssl_tls13_handshake_wrapup(mbedtls_ssl_context *ssl)
Jerry Yu69dd8d42022-04-16 12:51:26 +08003074{
Gilles Peskine449bd832023-01-11 14:50:10 +01003075 MBEDTLS_SSL_DEBUG_MSG(2, ("handshake: done"));
Jerry Yu03ed50b2022-04-16 17:13:30 +08003076
Gilles Peskine449bd832023-01-11 14:50:10 +01003077 mbedtls_ssl_tls13_handshake_wrapup(ssl);
Jerry Yue67bef42022-07-07 07:29:42 +00003078
Pengyu Lv3643fdb2023-01-12 16:46:28 +08003079#if defined(MBEDTLS_SSL_SESSION_TICKETS) && \
3080 defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED)
Pengyu Lva1aa31b2022-12-13 13:49:59 +08003081/* TODO: Remove the check of SOME_PSK_ENABLED since SESSION_TICKETS requires
3082 * SOME_PSK_ENABLED to be enabled. Here is just to make CI happy. It is
3083 * expected to be resolved with issue#6395.
3084 */
Pengyu Lvc7af2c42022-12-01 16:33:00 +08003085 /* Sent NewSessionTicket message only when client supports PSK */
Pengyu Lvb2cfafb2023-11-14 13:56:13 +08003086 if (mbedtls_ssl_tls13_is_some_psk_supported(ssl)) {
Xiaokang Qian9f1747b2023-03-30 02:50:04 +00003087 mbedtls_ssl_handshake_set_state(
3088 ssl, MBEDTLS_SSL_TLS1_3_NEW_SESSION_TICKET);
Pengyu Lvc7af2c42022-12-01 16:33:00 +08003089 } else
Jerry Yufca4d572022-07-21 10:37:48 +08003090#endif
Pengyu Lv3643fdb2023-01-12 16:46:28 +08003091 {
3092 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_HANDSHAKE_OVER);
3093 }
Gilles Peskine449bd832023-01-11 14:50:10 +01003094 return 0;
Jerry Yu69dd8d42022-04-16 12:51:26 +08003095}
3096
3097/*
Jerry Yua8d3c502022-10-30 14:51:23 +08003098 * Handler for MBEDTLS_SSL_TLS1_3_NEW_SESSION_TICKET
Jerry Yue67bef42022-07-07 07:29:42 +00003099 */
3100#define SSL_NEW_SESSION_TICKET_SKIP 0
3101#define SSL_NEW_SESSION_TICKET_WRITE 1
3102MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01003103static int ssl_tls13_write_new_session_ticket_coordinate(mbedtls_ssl_context *ssl)
Jerry Yue67bef42022-07-07 07:29:42 +00003104{
3105 /* Check whether the use of session tickets is enabled */
Gilles Peskine449bd832023-01-11 14:50:10 +01003106 if (ssl->conf->f_ticket_write == NULL) {
3107 MBEDTLS_SSL_DEBUG_MSG(2, ("NewSessionTicket: disabled,"
3108 " callback is not set"));
3109 return SSL_NEW_SESSION_TICKET_SKIP;
Jerry Yud0766ec2022-09-22 10:46:57 +08003110 }
Gilles Peskine449bd832023-01-11 14:50:10 +01003111 if (ssl->conf->new_session_tickets_count == 0) {
3112 MBEDTLS_SSL_DEBUG_MSG(2, ("NewSessionTicket: disabled,"
3113 " configured count is zero"));
3114 return SSL_NEW_SESSION_TICKET_SKIP;
Jerry Yud0766ec2022-09-22 10:46:57 +08003115 }
3116
Gilles Peskine449bd832023-01-11 14:50:10 +01003117 if (ssl->handshake->new_session_tickets_count == 0) {
3118 MBEDTLS_SSL_DEBUG_MSG(2, ("NewSessionTicket: all tickets have "
3119 "been sent."));
3120 return SSL_NEW_SESSION_TICKET_SKIP;
Jerry Yue67bef42022-07-07 07:29:42 +00003121 }
3122
Gilles Peskine449bd832023-01-11 14:50:10 +01003123 return SSL_NEW_SESSION_TICKET_WRITE;
Jerry Yue67bef42022-07-07 07:29:42 +00003124}
3125
3126#if defined(MBEDTLS_SSL_SESSION_TICKETS)
3127MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01003128static int ssl_tls13_prepare_new_session_ticket(mbedtls_ssl_context *ssl,
3129 unsigned char *ticket_nonce,
3130 size_t ticket_nonce_size)
Jerry Yue67bef42022-07-07 07:29:42 +00003131{
3132 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
3133 mbedtls_ssl_session *session = ssl->session;
3134 mbedtls_ssl_ciphersuite_t *ciphersuite_info;
3135 psa_algorithm_t psa_hash_alg;
3136 int hash_length;
3137
Gilles Peskine449bd832023-01-11 14:50:10 +01003138 MBEDTLS_SSL_DEBUG_MSG(2, ("=> prepare NewSessionTicket msg"));
Jerry Yue67bef42022-07-07 07:29:42 +00003139
Pengyu Lv9f926952022-11-17 15:22:33 +08003140 /* Set ticket_flags depends on the advertised psk key exchange mode */
Pengyu Lv94a42cc2023-12-06 10:04:17 +08003141 mbedtls_ssl_tls13_session_clear_ticket_flags(
Pengyu Lv9eacb442022-12-09 14:39:19 +08003142 session, MBEDTLS_SSL_TLS1_3_TICKET_FLAGS_MASK);
Pengyu Lve6487fe2022-12-06 09:30:29 +08003143#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED)
Pengyu Lv94a42cc2023-12-06 10:04:17 +08003144 mbedtls_ssl_tls13_session_set_ticket_flags(
Pengyu Lv9eacb442022-12-09 14:39:19 +08003145 session, ssl->handshake->tls13_kex_modes);
Pengyu Lve6487fe2022-12-06 09:30:29 +08003146#endif
Jerry Yu95648b02023-12-06 15:03:34 +08003147
3148#if defined(MBEDTLS_SSL_EARLY_DATA)
3149 if (ssl->conf->early_data_enabled == MBEDTLS_SSL_EARLY_DATA_ENABLED &&
3150 ssl->conf->max_early_data_size > 0) {
Pengyu Lv94a42cc2023-12-06 10:04:17 +08003151 mbedtls_ssl_tls13_session_set_ticket_flags(
Jerry Yu95648b02023-12-06 15:03:34 +08003152 session, MBEDTLS_SSL_TLS1_3_TICKET_ALLOW_EARLY_DATA);
3153 }
3154#endif /* MBEDTLS_SSL_EARLY_DATA */
3155
Pengyu Lv4938a562023-01-16 11:28:49 +08003156 MBEDTLS_SSL_PRINT_TICKET_FLAGS(4, session->ticket_flags);
Pengyu Lv9f926952022-11-17 15:22:33 +08003157
Jerry Yue67bef42022-07-07 07:29:42 +00003158 /* Generate ticket_age_add */
Gilles Peskine449bd832023-01-11 14:50:10 +01003159 if ((ret = ssl->conf->f_rng(ssl->conf->p_rng,
3160 (unsigned char *) &session->ticket_age_add,
3161 sizeof(session->ticket_age_add)) != 0)) {
3162 MBEDTLS_SSL_DEBUG_RET(1, "generate_ticket_age_add", ret);
3163 return ret;
Jerry Yue67bef42022-07-07 07:29:42 +00003164 }
Gilles Peskine449bd832023-01-11 14:50:10 +01003165 MBEDTLS_SSL_DEBUG_MSG(3, ("ticket_age_add: %u",
3166 (unsigned int) session->ticket_age_add));
Jerry Yue67bef42022-07-07 07:29:42 +00003167
3168 /* Generate ticket_nonce */
Gilles Peskine449bd832023-01-11 14:50:10 +01003169 ret = ssl->conf->f_rng(ssl->conf->p_rng, ticket_nonce, ticket_nonce_size);
3170 if (ret != 0) {
3171 MBEDTLS_SSL_DEBUG_RET(1, "generate_ticket_nonce", ret);
3172 return ret;
Jerry Yue67bef42022-07-07 07:29:42 +00003173 }
Gilles Peskine449bd832023-01-11 14:50:10 +01003174 MBEDTLS_SSL_DEBUG_BUF(3, "ticket_nonce:",
3175 ticket_nonce, ticket_nonce_size);
Jerry Yue67bef42022-07-07 07:29:42 +00003176
3177 ciphersuite_info =
Gilles Peskine449bd832023-01-11 14:50:10 +01003178 (mbedtls_ssl_ciphersuite_t *) ssl->handshake->ciphersuite_info;
Dave Rodgman2eab4622023-10-05 13:30:37 +01003179 psa_hash_alg = mbedtls_md_psa_alg_from_type((mbedtls_md_type_t) ciphersuite_info->mac);
Gilles Peskine449bd832023-01-11 14:50:10 +01003180 hash_length = PSA_HASH_LENGTH(psa_hash_alg);
3181 if (hash_length == -1 ||
3182 (size_t) hash_length > sizeof(session->resumption_key)) {
3183 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Jerry Yufca4d572022-07-21 10:37:48 +08003184 }
3185
Jerry Yue67bef42022-07-07 07:29:42 +00003186 /* In this code the psk key length equals the length of the hash */
3187 session->resumption_key_len = hash_length;
3188 session->ciphersuite = ciphersuite_info->id;
3189
3190 /* Compute resumption key
3191 *
3192 * HKDF-Expand-Label( resumption_master_secret,
3193 * "resumption", ticket_nonce, Hash.length )
3194 */
3195 ret = mbedtls_ssl_tls13_hkdf_expand_label(
Gilles Peskine449bd832023-01-11 14:50:10 +01003196 psa_hash_alg,
3197 session->app_secrets.resumption_master_secret,
3198 hash_length,
3199 MBEDTLS_SSL_TLS1_3_LBL_WITH_LEN(resumption),
3200 ticket_nonce,
3201 ticket_nonce_size,
3202 session->resumption_key,
3203 hash_length);
Jerry Yue67bef42022-07-07 07:29:42 +00003204
Gilles Peskine449bd832023-01-11 14:50:10 +01003205 if (ret != 0) {
3206 MBEDTLS_SSL_DEBUG_RET(2,
3207 "Creating the ticket-resumed PSK failed",
3208 ret);
3209 return ret;
Jerry Yue67bef42022-07-07 07:29:42 +00003210 }
Gilles Peskine449bd832023-01-11 14:50:10 +01003211 MBEDTLS_SSL_DEBUG_BUF(3, "Ticket-resumed PSK",
3212 session->resumption_key,
3213 session->resumption_key_len);
Jerry Yue67bef42022-07-07 07:29:42 +00003214
Gilles Peskine449bd832023-01-11 14:50:10 +01003215 MBEDTLS_SSL_DEBUG_BUF(3, "resumption_master_secret",
3216 session->app_secrets.resumption_master_secret,
3217 hash_length);
Jerry Yue67bef42022-07-07 07:29:42 +00003218
Gilles Peskine449bd832023-01-11 14:50:10 +01003219 return 0;
Jerry Yue67bef42022-07-07 07:29:42 +00003220}
3221
3222/* This function creates a NewSessionTicket message in the following format:
3223 *
3224 * struct {
3225 * uint32 ticket_lifetime;
3226 * uint32 ticket_age_add;
3227 * opaque ticket_nonce<0..255>;
3228 * opaque ticket<1..2^16-1>;
3229 * Extension extensions<0..2^16-2>;
3230 * } NewSessionTicket;
3231 *
3232 * The ticket inside the NewSessionTicket message is an encrypted container
3233 * carrying the necessary information so that the server is later able to
3234 * re-start the communication.
3235 *
3236 * The following fields are placed inside the ticket by the
3237 * f_ticket_write() function:
3238 *
Jerry Yu0069abc2023-11-23 21:07:28 +08003239 * - creation time (ticket_creation_time)
3240 * - flags (ticket_flags)
Jerry Yue67bef42022-07-07 07:29:42 +00003241 * - age add (ticket_age_add)
Jerry Yu0069abc2023-11-23 21:07:28 +08003242 * - key (resumption_key)
3243 * - key length (resumption_key_len)
Jerry Yue67bef42022-07-07 07:29:42 +00003244 * - ciphersuite (ciphersuite)
Jerry Yu0069abc2023-11-23 21:07:28 +08003245 * - max_early_data_size (max_early_data_size)
Jerry Yue67bef42022-07-07 07:29:42 +00003246 */
3247MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01003248static int ssl_tls13_write_new_session_ticket_body(mbedtls_ssl_context *ssl,
3249 unsigned char *buf,
3250 unsigned char *end,
3251 size_t *out_len,
3252 unsigned char *ticket_nonce,
3253 size_t ticket_nonce_size)
Jerry Yue67bef42022-07-07 07:29:42 +00003254{
3255 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
3256 unsigned char *p = buf;
3257 mbedtls_ssl_session *session = ssl->session;
3258 size_t ticket_len;
3259 uint32_t ticket_lifetime;
Jerry Yu01da35e2022-12-12 15:09:22 +08003260 unsigned char *p_extensions_len;
Jerry Yue67bef42022-07-07 07:29:42 +00003261
3262 *out_len = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01003263 MBEDTLS_SSL_DEBUG_MSG(2, ("=> write NewSessionTicket msg"));
Jerry Yue67bef42022-07-07 07:29:42 +00003264
3265 /*
3266 * ticket_lifetime 4 bytes
3267 * ticket_age_add 4 bytes
3268 * ticket_nonce 1 + ticket_nonce_size bytes
3269 * ticket >=2 bytes
3270 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003271 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 4 + 4 + 1 + ticket_nonce_size + 2);
Jerry Yue67bef42022-07-07 07:29:42 +00003272
3273 /* Generate ticket and ticket_lifetime */
Ronald Cron3c0072b2023-11-22 10:00:14 +01003274#if defined(MBEDTLS_HAVE_TIME)
3275 session->ticket_creation_time = mbedtls_ms_time();
3276#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01003277 ret = ssl->conf->f_ticket_write(ssl->conf->p_ticket,
3278 session,
3279 p + 9 + ticket_nonce_size + 2,
3280 end,
3281 &ticket_len,
3282 &ticket_lifetime);
3283 if (ret != 0) {
3284 MBEDTLS_SSL_DEBUG_RET(1, "write_ticket", ret);
3285 return ret;
Jerry Yue67bef42022-07-07 07:29:42 +00003286 }
3287 /* RFC 8446 4.6.1
3288 * ticket_lifetime: Indicates the lifetime in seconds as a 32-bit
3289 * unsigned integer in network byte order from the time of ticket
3290 * issuance. Servers MUST NOT use any value greater than
3291 * 604800 seconds (7 days). The value of zero indicates that the
3292 * ticket should be discarded immediately. Clients MUST NOT cache
3293 * tickets for longer than 7 days, regardless of the ticket_lifetime,
3294 * and MAY delete tickets earlier based on local policy. A server
3295 * MAY treat a ticket as valid for a shorter period of time than what
3296 * is stated in the ticket_lifetime.
3297 */
Jerry Yu8e0174a2023-11-10 13:58:16 +08003298 if (ticket_lifetime > MBEDTLS_SSL_TLS1_3_MAX_ALLOWED_TICKET_LIFETIME) {
3299 ticket_lifetime = MBEDTLS_SSL_TLS1_3_MAX_ALLOWED_TICKET_LIFETIME;
Gilles Peskine449bd832023-01-11 14:50:10 +01003300 }
3301 MBEDTLS_PUT_UINT32_BE(ticket_lifetime, p, 0);
3302 MBEDTLS_SSL_DEBUG_MSG(3, ("ticket_lifetime: %u",
3303 (unsigned int) ticket_lifetime));
Jerry Yue67bef42022-07-07 07:29:42 +00003304
3305 /* Write ticket_age_add */
Gilles Peskine449bd832023-01-11 14:50:10 +01003306 MBEDTLS_PUT_UINT32_BE(session->ticket_age_add, p, 4);
3307 MBEDTLS_SSL_DEBUG_MSG(3, ("ticket_age_add: %u",
3308 (unsigned int) session->ticket_age_add));
Jerry Yue67bef42022-07-07 07:29:42 +00003309
3310 /* Write ticket_nonce */
Gilles Peskine449bd832023-01-11 14:50:10 +01003311 p[8] = (unsigned char) ticket_nonce_size;
3312 if (ticket_nonce_size > 0) {
3313 memcpy(p + 9, ticket_nonce, ticket_nonce_size);
Jerry Yue67bef42022-07-07 07:29:42 +00003314 }
3315 p += 9 + ticket_nonce_size;
3316
3317 /* Write ticket */
Gilles Peskine449bd832023-01-11 14:50:10 +01003318 MBEDTLS_PUT_UINT16_BE(ticket_len, p, 0);
Jerry Yue67bef42022-07-07 07:29:42 +00003319 p += 2;
Gilles Peskine449bd832023-01-11 14:50:10 +01003320 MBEDTLS_SSL_DEBUG_BUF(4, "ticket", p, ticket_len);
Jerry Yue67bef42022-07-07 07:29:42 +00003321 p += ticket_len;
3322
3323 /* Ticket Extensions
3324 *
Jerry Yu01da35e2022-12-12 15:09:22 +08003325 * Extension extensions<0..2^16-2>;
Jerry Yue67bef42022-07-07 07:29:42 +00003326 */
Jerry Yuedab6372022-10-31 14:37:31 +08003327 ssl->handshake->sent_extensions = MBEDTLS_SSL_EXT_MASK_NONE;
3328
Gilles Peskine449bd832023-01-11 14:50:10 +01003329 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 2);
Jerry Yu01da35e2022-12-12 15:09:22 +08003330 p_extensions_len = p;
Jerry Yue67bef42022-07-07 07:29:42 +00003331 p += 2;
3332
Jerry Yu01da35e2022-12-12 15:09:22 +08003333#if defined(MBEDTLS_SSL_EARLY_DATA)
Pengyu Lv94a42cc2023-12-06 10:04:17 +08003334 if (mbedtls_ssl_tls13_session_ticket_allow_early_data(session)) {
Jerry Yu95648b02023-12-06 15:03:34 +08003335 size_t output_len;
3336
Jerry Yuf135bac2023-11-23 18:10:51 +08003337 if ((ret = mbedtls_ssl_tls13_write_early_data_ext(
Jerry Yuc59c5862023-12-05 10:40:49 +08003338 ssl, 1, p, end, &output_len)) != 0) {
Jerry Yuf135bac2023-11-23 18:10:51 +08003339 MBEDTLS_SSL_DEBUG_RET(
3340 1, "mbedtls_ssl_tls13_write_early_data_ext", ret);
3341 return ret;
3342 }
3343 p += output_len;
Jerry Yu9e7f9bc2023-11-27 16:52:07 +08003344 } else {
3345 MBEDTLS_SSL_DEBUG_MSG(
3346 4, ("early_data not allowed, "
3347 "skip early_data extension in NewSessionTicket"));
Jerry Yu01da35e2022-12-12 15:09:22 +08003348 }
Jerry Yuf135bac2023-11-23 18:10:51 +08003349
Jerry Yu01da35e2022-12-12 15:09:22 +08003350#endif /* MBEDTLS_SSL_EARLY_DATA */
3351
3352 MBEDTLS_PUT_UINT16_BE(p - p_extensions_len - 2, p_extensions_len, 0);
3353
Jerry Yue67bef42022-07-07 07:29:42 +00003354 *out_len = p - buf;
Gilles Peskine449bd832023-01-11 14:50:10 +01003355 MBEDTLS_SSL_DEBUG_BUF(4, "ticket", buf, *out_len);
3356 MBEDTLS_SSL_DEBUG_MSG(2, ("<= write new session ticket"));
Jerry Yue67bef42022-07-07 07:29:42 +00003357
Jerry Yu7de2ff02022-11-08 21:43:46 +08003358 MBEDTLS_SSL_PRINT_EXTS(
Gilles Peskine449bd832023-01-11 14:50:10 +01003359 3, MBEDTLS_SSL_HS_NEW_SESSION_TICKET, ssl->handshake->sent_extensions);
Jerry Yu4b8f2f72022-10-31 13:31:22 +08003360
Gilles Peskine449bd832023-01-11 14:50:10 +01003361 return 0;
Jerry Yue67bef42022-07-07 07:29:42 +00003362}
3363
3364/*
Jerry Yua8d3c502022-10-30 14:51:23 +08003365 * Handler for MBEDTLS_SSL_TLS1_3_NEW_SESSION_TICKET
Jerry Yue67bef42022-07-07 07:29:42 +00003366 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003367static int ssl_tls13_write_new_session_ticket(mbedtls_ssl_context *ssl)
Jerry Yue67bef42022-07-07 07:29:42 +00003368{
3369 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
3370
Gilles Peskine449bd832023-01-11 14:50:10 +01003371 MBEDTLS_SSL_PROC_CHK_NEG(ssl_tls13_write_new_session_ticket_coordinate(ssl));
Jerry Yue67bef42022-07-07 07:29:42 +00003372
Gilles Peskine449bd832023-01-11 14:50:10 +01003373 if (ret == SSL_NEW_SESSION_TICKET_WRITE) {
Jerry Yue67bef42022-07-07 07:29:42 +00003374 unsigned char ticket_nonce[MBEDTLS_SSL_TLS1_3_TICKET_NONCE_LENGTH];
3375 unsigned char *buf;
3376 size_t buf_len, msg_len;
3377
Gilles Peskine449bd832023-01-11 14:50:10 +01003378 MBEDTLS_SSL_PROC_CHK(ssl_tls13_prepare_new_session_ticket(
3379 ssl, ticket_nonce, sizeof(ticket_nonce)));
Jerry Yue67bef42022-07-07 07:29:42 +00003380
Xiaokang Qian9f1747b2023-03-30 02:50:04 +00003381 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_start_handshake_msg(
3382 ssl, MBEDTLS_SSL_HS_NEW_SESSION_TICKET,
3383 &buf, &buf_len));
Jerry Yue67bef42022-07-07 07:29:42 +00003384
Gilles Peskine449bd832023-01-11 14:50:10 +01003385 MBEDTLS_SSL_PROC_CHK(ssl_tls13_write_new_session_ticket_body(
3386 ssl, buf, buf + buf_len, &msg_len,
3387 ticket_nonce, sizeof(ticket_nonce)));
Jerry Yue67bef42022-07-07 07:29:42 +00003388
Gilles Peskine449bd832023-01-11 14:50:10 +01003389 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_finish_handshake_msg(
3390 ssl, buf_len, msg_len));
Jerry Yufca4d572022-07-21 10:37:48 +08003391
Jerry Yu359e65f2022-09-22 23:47:43 +08003392 /* Limit session tickets count to one when resumption connection.
3393 *
3394 * See document of mbedtls_ssl_conf_new_session_tickets.
3395 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003396 if (ssl->handshake->resume == 1) {
Jerry Yu359e65f2022-09-22 23:47:43 +08003397 ssl->handshake->new_session_tickets_count = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01003398 } else {
Jerry Yu359e65f2022-09-22 23:47:43 +08003399 ssl->handshake->new_session_tickets_count--;
Gilles Peskine449bd832023-01-11 14:50:10 +01003400 }
Jerry Yub7e3fa72022-09-22 11:07:18 +08003401
Jerry Yua8d3c502022-10-30 14:51:23 +08003402 mbedtls_ssl_handshake_set_state(
Gilles Peskine449bd832023-01-11 14:50:10 +01003403 ssl, MBEDTLS_SSL_TLS1_3_NEW_SESSION_TICKET_FLUSH);
3404 } else {
3405 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_HANDSHAKE_OVER);
Jerry Yue67bef42022-07-07 07:29:42 +00003406 }
3407
Jerry Yue67bef42022-07-07 07:29:42 +00003408cleanup:
3409
Gilles Peskine449bd832023-01-11 14:50:10 +01003410 return ret;
Jerry Yue67bef42022-07-07 07:29:42 +00003411}
3412#endif /* MBEDTLS_SSL_SESSION_TICKETS */
3413
3414/*
XiaokangQiane8ff3502022-04-22 02:34:40 +00003415 * TLS 1.3 State Machine -- server side
XiaokangQian7807f9f2022-02-15 10:04:37 +00003416 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003417int mbedtls_ssl_tls13_handshake_server_step(mbedtls_ssl_context *ssl)
Jerry Yub9930e72021-08-06 17:11:51 +08003418{
XiaokangQian08037552022-04-20 07:16:41 +00003419 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
XiaokangQian7807f9f2022-02-15 10:04:37 +00003420
Gilles Peskine449bd832023-01-11 14:50:10 +01003421 if (ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER || ssl->handshake == NULL) {
3422 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
3423 }
XiaokangQian7807f9f2022-02-15 10:04:37 +00003424
Gilles Peskine449bd832023-01-11 14:50:10 +01003425 MBEDTLS_SSL_DEBUG_MSG(2, ("tls13 server state: %s(%d)",
Dave Rodgman2eab4622023-10-05 13:30:37 +01003426 mbedtls_ssl_states_str((mbedtls_ssl_states) ssl->state),
Gilles Peskine449bd832023-01-11 14:50:10 +01003427 ssl->state));
Jerry Yu6e81b272021-09-27 11:16:17 +08003428
Gilles Peskine449bd832023-01-11 14:50:10 +01003429 switch (ssl->state) {
XiaokangQian7807f9f2022-02-15 10:04:37 +00003430 /* start state */
3431 case MBEDTLS_SSL_HELLO_REQUEST:
Gilles Peskine449bd832023-01-11 14:50:10 +01003432 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_CLIENT_HELLO);
XiaokangQian08037552022-04-20 07:16:41 +00003433 ret = 0;
XiaokangQian7807f9f2022-02-15 10:04:37 +00003434 break;
3435
XiaokangQian7807f9f2022-02-15 10:04:37 +00003436 case MBEDTLS_SSL_CLIENT_HELLO:
Gilles Peskine449bd832023-01-11 14:50:10 +01003437 ret = ssl_tls13_process_client_hello(ssl);
3438 if (ret != 0) {
3439 MBEDTLS_SSL_DEBUG_RET(1, "ssl_tls13_process_client_hello", ret);
3440 }
Jerry Yuf41553b2022-05-09 22:20:30 +08003441 break;
XiaokangQian7807f9f2022-02-15 10:04:37 +00003442
Jerry Yuf41553b2022-05-09 22:20:30 +08003443 case MBEDTLS_SSL_HELLO_RETRY_REQUEST:
Gilles Peskine449bd832023-01-11 14:50:10 +01003444 ret = ssl_tls13_write_hello_retry_request(ssl);
3445 if (ret != 0) {
3446 MBEDTLS_SSL_DEBUG_RET(1, "ssl_tls13_write_hello_retry_request", ret);
3447 return ret;
Jerry Yuf41553b2022-05-09 22:20:30 +08003448 }
XiaokangQian7807f9f2022-02-15 10:04:37 +00003449 break;
3450
Jerry Yu5b64ae92022-03-30 17:15:02 +08003451 case MBEDTLS_SSL_SERVER_HELLO:
Gilles Peskine449bd832023-01-11 14:50:10 +01003452 ret = ssl_tls13_write_server_hello(ssl);
Jerry Yu5b64ae92022-03-30 17:15:02 +08003453 break;
3454
Xiaofei Baicba64af2022-02-15 10:00:56 +00003455 case MBEDTLS_SSL_ENCRYPTED_EXTENSIONS:
Gilles Peskine449bd832023-01-11 14:50:10 +01003456 ret = ssl_tls13_write_encrypted_extensions(ssl);
3457 if (ret != 0) {
3458 MBEDTLS_SSL_DEBUG_RET(1, "ssl_tls13_write_encrypted_extensions", ret);
3459 return ret;
Xiaofei Baicba64af2022-02-15 10:00:56 +00003460 }
Jerry Yu48330562022-05-06 21:35:44 +08003461 break;
Jerry Yu6a2cd9e2022-05-05 11:14:19 +08003462
Ronald Cron928cbd32022-10-04 16:14:26 +02003463#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED)
Xiaofei Bai9ca09d42022-02-14 12:57:18 +00003464 case MBEDTLS_SSL_CERTIFICATE_REQUEST:
Gilles Peskine449bd832023-01-11 14:50:10 +01003465 ret = ssl_tls13_write_certificate_request(ssl);
Xiaofei Bai9ca09d42022-02-14 12:57:18 +00003466 break;
Xiaofei Bai9ca09d42022-02-14 12:57:18 +00003467
Jerry Yu83da34e2022-04-16 13:59:52 +08003468 case MBEDTLS_SSL_SERVER_CERTIFICATE:
Gilles Peskine449bd832023-01-11 14:50:10 +01003469 ret = ssl_tls13_write_server_certificate(ssl);
Jerry Yu83da34e2022-04-16 13:59:52 +08003470 break;
3471
3472 case MBEDTLS_SSL_CERTIFICATE_VERIFY:
Gilles Peskine449bd832023-01-11 14:50:10 +01003473 ret = ssl_tls13_write_certificate_verify(ssl);
Jerry Yu83da34e2022-04-16 13:59:52 +08003474 break;
Ronald Cron928cbd32022-10-04 16:14:26 +02003475#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED */
Jerry Yu83da34e2022-04-16 13:59:52 +08003476
Gilles Peskine449bd832023-01-11 14:50:10 +01003477 /*
3478 * Injection of dummy-CCS's for middlebox compatibility
3479 */
Gabor Mezei7b39bf12022-05-24 16:04:14 +02003480#if defined(MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE)
Gabor Mezeif7044ea2022-06-28 16:01:49 +02003481 case MBEDTLS_SSL_SERVER_CCS_AFTER_HELLO_RETRY_REQUEST:
Gilles Peskine449bd832023-01-11 14:50:10 +01003482 ret = mbedtls_ssl_tls13_write_change_cipher_spec(ssl);
3483 if (ret == 0) {
3484 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_CLIENT_HELLO);
3485 }
Gabor Mezei7b39bf12022-05-24 16:04:14 +02003486 break;
3487
3488 case MBEDTLS_SSL_SERVER_CCS_AFTER_SERVER_HELLO:
Ronald Crone273f722024-02-13 18:22:26 +01003489 ret = mbedtls_ssl_tls13_write_change_cipher_spec(ssl);
3490 if (ret != 0) {
3491 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01003492 }
Ronald Cronfe59ff72024-01-24 14:31:50 +01003493 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_ENCRYPTED_EXTENSIONS);
Gabor Mezei7b39bf12022-05-24 16:04:14 +02003494 break;
3495#endif /* MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE */
3496
Jerry Yu69dd8d42022-04-16 12:51:26 +08003497 case MBEDTLS_SSL_SERVER_FINISHED:
Gilles Peskine449bd832023-01-11 14:50:10 +01003498 ret = ssl_tls13_write_server_finished(ssl);
Jerry Yu69dd8d42022-04-16 12:51:26 +08003499 break;
3500
Jerry Yu87b5ed42022-12-12 13:07:07 +08003501#if defined(MBEDTLS_SSL_EARLY_DATA)
3502 case MBEDTLS_SSL_END_OF_EARLY_DATA:
Jerry Yu59d420f2023-12-01 16:30:34 +08003503 ret = ssl_tls13_process_end_of_early_data(ssl);
Jerry Yu87b5ed42022-12-12 13:07:07 +08003504 break;
3505#endif /* MBEDTLS_SSL_EARLY_DATA */
3506
Jerry Yu69dd8d42022-04-16 12:51:26 +08003507 case MBEDTLS_SSL_CLIENT_FINISHED:
Gilles Peskine449bd832023-01-11 14:50:10 +01003508 ret = ssl_tls13_process_client_finished(ssl);
Jerry Yu69dd8d42022-04-16 12:51:26 +08003509 break;
3510
Jerry Yu69dd8d42022-04-16 12:51:26 +08003511 case MBEDTLS_SSL_HANDSHAKE_WRAPUP:
Gilles Peskine449bd832023-01-11 14:50:10 +01003512 ret = ssl_tls13_handshake_wrapup(ssl);
Jerry Yu69dd8d42022-04-16 12:51:26 +08003513 break;
3514
Ronald Cron766c0cd2022-10-18 12:17:11 +02003515#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED)
XiaokangQian6b916b12022-04-25 07:29:34 +00003516 case MBEDTLS_SSL_CLIENT_CERTIFICATE:
Gilles Peskine449bd832023-01-11 14:50:10 +01003517 ret = mbedtls_ssl_tls13_process_certificate(ssl);
3518 if (ret == 0) {
3519 if (ssl->session_negotiate->peer_cert != NULL) {
Ronald Cron209cae92022-06-07 10:30:19 +02003520 mbedtls_ssl_handshake_set_state(
Gilles Peskine449bd832023-01-11 14:50:10 +01003521 ssl, MBEDTLS_SSL_CLIENT_CERTIFICATE_VERIFY);
3522 } else {
3523 MBEDTLS_SSL_DEBUG_MSG(2, ("skip parse certificate verify"));
Ronald Cron209cae92022-06-07 10:30:19 +02003524 mbedtls_ssl_handshake_set_state(
Gilles Peskine449bd832023-01-11 14:50:10 +01003525 ssl, MBEDTLS_SSL_CLIENT_FINISHED);
Ronald Cron19385882022-06-15 16:26:13 +02003526 }
XiaokangQian6b916b12022-04-25 07:29:34 +00003527 }
3528 break;
3529
3530 case MBEDTLS_SSL_CLIENT_CERTIFICATE_VERIFY:
Gilles Peskine449bd832023-01-11 14:50:10 +01003531 ret = mbedtls_ssl_tls13_process_certificate_verify(ssl);
3532 if (ret == 0) {
XiaokangQian6b916b12022-04-25 07:29:34 +00003533 mbedtls_ssl_handshake_set_state(
Gilles Peskine449bd832023-01-11 14:50:10 +01003534 ssl, MBEDTLS_SSL_CLIENT_FINISHED);
XiaokangQian6b916b12022-04-25 07:29:34 +00003535 }
3536 break;
Ronald Cron766c0cd2022-10-18 12:17:11 +02003537#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED */
XiaokangQian6b916b12022-04-25 07:29:34 +00003538
Jerry Yue67bef42022-07-07 07:29:42 +00003539#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Jerry Yua8d3c502022-10-30 14:51:23 +08003540 case MBEDTLS_SSL_TLS1_3_NEW_SESSION_TICKET:
Gilles Peskine449bd832023-01-11 14:50:10 +01003541 ret = ssl_tls13_write_new_session_ticket(ssl);
3542 if (ret != 0) {
3543 MBEDTLS_SSL_DEBUG_RET(1,
3544 "ssl_tls13_write_new_session_ticket ",
3545 ret);
Jerry Yue67bef42022-07-07 07:29:42 +00003546 }
3547 break;
Jerry Yua8d3c502022-10-30 14:51:23 +08003548 case MBEDTLS_SSL_TLS1_3_NEW_SESSION_TICKET_FLUSH:
Jerry Yue67bef42022-07-07 07:29:42 +00003549 /* This state is necessary to do the flush of the New Session
Jerry Yua8d3c502022-10-30 14:51:23 +08003550 * Ticket message written in MBEDTLS_SSL_TLS1_3_NEW_SESSION_TICKET
Jerry Yue67bef42022-07-07 07:29:42 +00003551 * as part of ssl_prepare_handshake_step.
3552 */
3553 ret = 0;
Jerry Yud4e75002022-08-09 13:33:50 +08003554
Gilles Peskine449bd832023-01-11 14:50:10 +01003555 if (ssl->handshake->new_session_tickets_count == 0) {
3556 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_HANDSHAKE_OVER);
3557 } else {
Xiaokang Qian9f1747b2023-03-30 02:50:04 +00003558 mbedtls_ssl_handshake_set_state(
3559 ssl, MBEDTLS_SSL_TLS1_3_NEW_SESSION_TICKET);
Gilles Peskine449bd832023-01-11 14:50:10 +01003560 }
Jerry Yue67bef42022-07-07 07:29:42 +00003561 break;
3562
3563#endif /* MBEDTLS_SSL_SESSION_TICKETS */
3564
XiaokangQian7807f9f2022-02-15 10:04:37 +00003565 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01003566 MBEDTLS_SSL_DEBUG_MSG(1, ("invalid state %d", ssl->state));
3567 return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
XiaokangQian7807f9f2022-02-15 10:04:37 +00003568 }
3569
Gilles Peskine449bd832023-01-11 14:50:10 +01003570 return ret;
Jerry Yub9930e72021-08-06 17:11:51 +08003571}
Jerry Yu3cc4c2a2021-08-06 16:29:08 +08003572
Jerry Yufb4b6472022-01-27 15:03:26 +08003573#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_PROTO_TLS1_3 */