blob: 3b7fc831f72384851bffd8e34f10a8a7c3e289de [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;
611 case MBEDTLS_SSL_TLS1_3_PSK_RESUMPTION:
Jerry Yu82534862022-08-30 10:42:33 +0800612#if defined(MBEDTLS_SSL_SESSION_TICKETS)
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 Yu82534862022-08-30 10:42:33 +0800619#else
620 ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
621#endif
Jerry Yu0baf9072022-08-25 11:21:04 +0800622 break;
623 default:
Gilles Peskine449bd832023-01-11 14:50:10 +0100624 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Jerry Yu0baf9072022-08-25 11:21:04 +0800625 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100626 if (ret != 0) {
Jerry Yuf35ba382022-08-23 17:58:26 +0800627 /* See below, no cipher_suite available, abort handshake */
628 MBEDTLS_SSL_PEND_FATAL_ALERT(
629 MBEDTLS_SSL_ALERT_MSG_DECRYPT_ERROR,
Gilles Peskine449bd832023-01-11 14:50:10 +0100630 MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE);
Jerry Yuf35ba382022-08-23 17:58:26 +0800631 MBEDTLS_SSL_DEBUG_RET(
Gilles Peskine449bd832023-01-11 14:50:10 +0100632 2, "ssl_tls13_select_ciphersuite", ret);
633 return ret;
Jerry Yu5725f1c2022-08-21 17:27:16 +0800634 }
635
Jerry Yu96a2e362022-07-21 15:11:34 +0800636 ret = ssl_tls13_offered_psks_check_binder_match(
Gilles Peskine449bd832023-01-11 14:50:10 +0100637 ssl, binder, binder_len, psk_type,
Dave Rodgman2eab4622023-10-05 13:30:37 +0100638 mbedtls_md_psa_alg_from_type((mbedtls_md_type_t) ciphersuite_info->mac));
Ronald Cron6e311272023-12-05 17:57:01 +0100639 if (ret != SSL_TLS1_3_BINDER_MATCH) {
Jerry Yuc5a23a02022-08-25 10:51:44 +0800640 /* For security reasons, the handshake should be aborted when we
641 * fail to validate a binder value. See RFC 8446 section 4.2.11.2
642 * and appendix E.6. */
Jerry Yu82534862022-08-30 10:42:33 +0800643#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Gilles Peskine449bd832023-01-11 14:50:10 +0100644 mbedtls_ssl_session_free(&session);
Jerry Yu82534862022-08-30 10:42:33 +0800645#endif
Gilles Peskine449bd832023-01-11 14:50:10 +0100646 MBEDTLS_SSL_DEBUG_MSG(3, ("Invalid binder."));
Xiaokang Qian9f1747b2023-03-30 02:50:04 +0000647 MBEDTLS_SSL_DEBUG_RET(
648 1, "ssl_tls13_offered_psks_check_binder_match", ret);
Jerry Yu96a2e362022-07-21 15:11:34 +0800649 MBEDTLS_SSL_PEND_FATAL_ALERT(
650 MBEDTLS_SSL_ALERT_MSG_DECRYPT_ERROR,
Gilles Peskine449bd832023-01-11 14:50:10 +0100651 MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE);
652 return ret;
Jerry Yu96a2e362022-07-21 15:11:34 +0800653 }
Jerry Yu96a2e362022-07-21 15:11:34 +0800654
655 matched_identity = identity_id;
Jerry Yu5725f1c2022-08-21 17:27:16 +0800656
657 /* Update handshake parameters */
Jerry Yue5834fd2022-08-29 20:16:09 +0800658 ssl->handshake->ciphersuite_info = ciphersuite_info;
Jerry Yu4746b102022-09-13 11:11:48 +0800659 ssl->session_negotiate->ciphersuite = cipher_suite;
Gilles Peskine449bd832023-01-11 14:50:10 +0100660 MBEDTLS_SSL_DEBUG_MSG(2, ("overwrite ciphersuite: %04x - %s",
661 cipher_suite, ciphersuite_info->name));
Jerry Yu82534862022-08-30 10:42:33 +0800662#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Gilles Peskine449bd832023-01-11 14:50:10 +0100663 if (psk_type == MBEDTLS_SSL_TLS1_3_PSK_RESUMPTION) {
664 ret = ssl_tls13_session_copy_ticket(ssl->session_negotiate,
665 &session);
666 mbedtls_ssl_session_free(&session);
667 if (ret != 0) {
668 return ret;
669 }
Jerry Yu82534862022-08-30 10:42:33 +0800670 }
671#endif /* MBEDTLS_SSL_SESSION_TICKETS */
Jerry Yu1c105562022-07-10 06:32:38 +0000672 }
673
Gilles Peskine449bd832023-01-11 14:50:10 +0100674 if (p_identity_len != identities_end || p_binder_len != binders_end) {
675 MBEDTLS_SSL_DEBUG_MSG(3, ("pre_shared_key extension decode error"));
676 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR,
677 MBEDTLS_ERR_SSL_DECODE_ERROR);
678 return MBEDTLS_ERR_SSL_DECODE_ERROR;
Jerry Yu1c105562022-07-10 06:32:38 +0000679 }
680
Jerry Yu6f1db3f2022-07-22 23:05:59 +0800681 /* Update the handshake transcript with the binder list. */
Xiaokang Qian9f1747b2023-03-30 02:50:04 +0000682 ret = ssl->handshake->update_checksum(
683 ssl, identities_end, (size_t) (binders_end - identities_end));
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +0100684 if (0 != ret) {
685 MBEDTLS_SSL_DEBUG_RET(1, ("update_checksum"), ret);
686 return ret;
687 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100688 if (matched_identity == -1) {
689 MBEDTLS_SSL_DEBUG_MSG(3, ("No matched PSK or ticket."));
690 return MBEDTLS_ERR_SSL_UNKNOWN_IDENTITY;
Jerry Yu1c105562022-07-10 06:32:38 +0000691 }
692
Gilles Peskine449bd832023-01-11 14:50:10 +0100693 ssl->handshake->selected_identity = (uint16_t) matched_identity;
694 MBEDTLS_SSL_DEBUG_MSG(3, ("Pre shared key found"));
Jerry Yu1c105562022-07-10 06:32:38 +0000695
Gilles Peskine449bd832023-01-11 14:50:10 +0100696 return 0;
Jerry Yu1c105562022-07-10 06:32:38 +0000697}
Jerry Yu032b15ce2022-07-11 06:10:03 +0000698
699/*
700 * struct {
701 * select ( Handshake.msg_type ) {
702 * ....
703 * case server_hello:
704 * uint16 selected_identity;
705 * }
706 * } PreSharedKeyExtension;
707 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100708static int ssl_tls13_write_server_pre_shared_key_ext(mbedtls_ssl_context *ssl,
709 unsigned char *buf,
710 unsigned char *end,
711 size_t *olen)
Jerry Yu032b15ce2022-07-11 06:10:03 +0000712{
Gilles Peskine449bd832023-01-11 14:50:10 +0100713 unsigned char *p = (unsigned char *) buf;
Jerry Yu032b15ce2022-07-11 06:10:03 +0000714
715 *olen = 0;
716
David Horstmann21b89762022-10-06 18:34:28 +0100717 int not_using_psk = 0;
Jerry Yu032b15ce2022-07-11 06:10:03 +0000718#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +0100719 not_using_psk = (mbedtls_svc_key_id_is_null(ssl->handshake->psk_opaque));
Jerry Yu032b15ce2022-07-11 06:10:03 +0000720#else
Gilles Peskine449bd832023-01-11 14:50:10 +0100721 not_using_psk = (ssl->handshake->psk == NULL);
Jerry Yu032b15ce2022-07-11 06:10:03 +0000722#endif
Gilles Peskine449bd832023-01-11 14:50:10 +0100723 if (not_using_psk) {
Jerry Yu032b15ce2022-07-11 06:10:03 +0000724 /* We shouldn't have called this extension writer unless we've
725 * chosen to use a PSK. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100726 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Jerry Yu032b15ce2022-07-11 06:10:03 +0000727 }
728
Gilles Peskine449bd832023-01-11 14:50:10 +0100729 MBEDTLS_SSL_DEBUG_MSG(3, ("server hello, adding pre_shared_key extension"));
730 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 6);
Jerry Yu032b15ce2022-07-11 06:10:03 +0000731
Gilles Peskine449bd832023-01-11 14:50:10 +0100732 MBEDTLS_PUT_UINT16_BE(MBEDTLS_TLS_EXT_PRE_SHARED_KEY, p, 0);
733 MBEDTLS_PUT_UINT16_BE(2, p, 2);
Jerry Yu032b15ce2022-07-11 06:10:03 +0000734
Gilles Peskine449bd832023-01-11 14:50:10 +0100735 MBEDTLS_PUT_UINT16_BE(ssl->handshake->selected_identity, p, 4);
Jerry Yu032b15ce2022-07-11 06:10:03 +0000736
737 *olen = 6;
738
Gilles Peskine449bd832023-01-11 14:50:10 +0100739 MBEDTLS_SSL_DEBUG_MSG(4, ("sent selected_identity: %u",
740 ssl->handshake->selected_identity));
Jerry Yu032b15ce2022-07-11 06:10:03 +0000741
Gilles Peskine449bd832023-01-11 14:50:10 +0100742 mbedtls_ssl_tls13_set_hs_sent_ext_mask(ssl, MBEDTLS_TLS_EXT_PRE_SHARED_KEY);
Jerry Yub95dd362022-11-08 21:19:34 +0800743
Gilles Peskine449bd832023-01-11 14:50:10 +0100744 return 0;
Jerry Yu032b15ce2022-07-11 06:10:03 +0000745}
746
Ronald Cron41a443a2022-10-04 16:38:25 +0200747#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED */
Jerry Yue19e3b92022-07-08 12:04:51 +0000748
XiaokangQian7807f9f2022-02-15 10:04:37 +0000749/* From RFC 8446:
750 * struct {
XiaokangQiancfd925f2022-04-14 07:10:37 +0000751 * ProtocolVersion versions<2..254>;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000752 * } SupportedVersions;
753 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200754MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100755static int ssl_tls13_parse_supported_versions_ext(mbedtls_ssl_context *ssl,
756 const unsigned char *buf,
757 const unsigned char *end)
XiaokangQian7807f9f2022-02-15 10:04:37 +0000758{
XiaokangQian7807f9f2022-02-15 10:04:37 +0000759 const unsigned char *p = buf;
XiaokangQian8f9dfe42022-04-15 02:52:39 +0000760 size_t versions_len;
XiaokangQian4080a7f2022-04-11 09:55:18 +0000761 const unsigned char *versions_end;
XiaokangQian0a1b54e2022-04-21 03:01:38 +0000762 uint16_t tls_version;
Ronald Cron5af4c7f2023-03-07 20:46:59 +0100763 int found_supported_version = 0;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000764
Gilles Peskine449bd832023-01-11 14:50:10 +0100765 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, 1);
XiaokangQian4080a7f2022-04-11 09:55:18 +0000766 versions_len = p[0];
XiaokangQian7807f9f2022-02-15 10:04:37 +0000767 p += 1;
768
Gilles Peskine449bd832023-01-11 14:50:10 +0100769 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, versions_len);
XiaokangQian4080a7f2022-04-11 09:55:18 +0000770 versions_end = p + versions_len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100771 while (p < versions_end) {
772 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, versions_end, 2);
773 tls_version = mbedtls_ssl_read_version(p, ssl->conf->transport);
XiaokangQianb67384d2022-04-19 00:02:38 +0000774 p += 2;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000775
Ronald Cron3bd2b022023-04-03 16:45:39 +0200776 if (MBEDTLS_SSL_VERSION_TLS1_3 == tls_version) {
Ronald Cron5af4c7f2023-03-07 20:46:59 +0100777 found_supported_version = 1;
778 break;
779 }
780
Ronald Cron3bd2b022023-04-03 16:45:39 +0200781 if ((MBEDTLS_SSL_VERSION_TLS1_2 == tls_version) &&
782 mbedtls_ssl_conf_is_tls12_enabled(ssl->conf)) {
Ronald Cron5af4c7f2023-03-07 20:46:59 +0100783 found_supported_version = 1;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000784 break;
785 }
XiaokangQian7807f9f2022-02-15 10:04:37 +0000786 }
787
Ronald Cron5af4c7f2023-03-07 20:46:59 +0100788 if (!found_supported_version) {
789 MBEDTLS_SSL_DEBUG_MSG(1, ("No supported version found."));
XiaokangQian7807f9f2022-02-15 10:04:37 +0000790
Gilles Peskine449bd832023-01-11 14:50:10 +0100791 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_PROTOCOL_VERSION,
792 MBEDTLS_ERR_SSL_BAD_PROTOCOL_VERSION);
793 return MBEDTLS_ERR_SSL_BAD_PROTOCOL_VERSION;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000794 }
795
Ronald Cron5af4c7f2023-03-07 20:46:59 +0100796 MBEDTLS_SSL_DEBUG_MSG(1, ("Negotiated version: [%04x]",
Gilles Peskine449bd832023-01-11 14:50:10 +0100797 (unsigned int) tls_version));
XiaokangQian7807f9f2022-02-15 10:04:37 +0000798
Ronald Cron5af4c7f2023-03-07 20:46:59 +0100799 return (int) tls_version;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000800}
801
Przemek Stekiel8c0a9532023-06-15 16:48:19 +0200802#if defined(PSA_WANT_ALG_ECDH) || defined(PSA_WANT_ALG_FFDH)
XiaokangQiane8ff3502022-04-22 02:34:40 +0000803/*
XiaokangQian7807f9f2022-02-15 10:04:37 +0000804 *
805 * From RFC 8446:
806 * enum {
807 * ... (0xFFFF)
808 * } NamedGroup;
809 * struct {
810 * NamedGroup named_group_list<2..2^16-1>;
811 * } NamedGroupList;
812 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200813MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100814static int ssl_tls13_parse_supported_groups_ext(mbedtls_ssl_context *ssl,
815 const unsigned char *buf,
816 const unsigned char *end)
XiaokangQian7807f9f2022-02-15 10:04:37 +0000817{
XiaokangQian7807f9f2022-02-15 10:04:37 +0000818 const unsigned char *p = buf;
XiaokangQian84823772022-04-19 07:57:30 +0000819 size_t named_group_list_len;
XiaokangQian8f9dfe42022-04-15 02:52:39 +0000820 const unsigned char *named_group_list_end;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000821
Gilles Peskine449bd832023-01-11 14:50:10 +0100822 MBEDTLS_SSL_DEBUG_BUF(3, "supported_groups extension", p, end - buf);
823 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, 2);
824 named_group_list_len = MBEDTLS_GET_UINT16_BE(p, 0);
XiaokangQian7807f9f2022-02-15 10:04:37 +0000825 p += 2;
Gilles Peskine449bd832023-01-11 14:50:10 +0100826 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, named_group_list_len);
XiaokangQian8f9dfe42022-04-15 02:52:39 +0000827 named_group_list_end = p + named_group_list_len;
XiaokangQian4e8cd7b2022-04-21 09:48:09 +0000828 ssl->handshake->hrr_selected_group = 0;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000829
Gilles Peskine449bd832023-01-11 14:50:10 +0100830 while (p < named_group_list_end) {
XiaokangQian08037552022-04-20 07:16:41 +0000831 uint16_t named_group;
Gilles Peskine449bd832023-01-11 14:50:10 +0100832 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, named_group_list_end, 2);
833 named_group = MBEDTLS_GET_UINT16_BE(p, 0);
XiaokangQian08037552022-04-20 07:16:41 +0000834 p += 2;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000835
Gilles Peskine449bd832023-01-11 14:50:10 +0100836 MBEDTLS_SSL_DEBUG_MSG(2,
837 ("got named group: %s(%04x)",
838 mbedtls_ssl_named_group_to_str(named_group),
839 named_group));
XiaokangQian08037552022-04-20 07:16:41 +0000840
Gilles Peskine449bd832023-01-11 14:50:10 +0100841 if (!mbedtls_ssl_named_group_is_offered(ssl, named_group) ||
842 !mbedtls_ssl_named_group_is_supported(named_group) ||
843 ssl->handshake->hrr_selected_group != 0) {
XiaokangQian08037552022-04-20 07:16:41 +0000844 continue;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000845 }
846
Gilles Peskine449bd832023-01-11 14:50:10 +0100847 MBEDTLS_SSL_DEBUG_MSG(2,
848 ("add named group %s(%04x) into received list.",
849 mbedtls_ssl_named_group_to_str(named_group),
850 named_group));
Jerry Yuc1be19f2022-04-23 16:11:39 +0800851
XiaokangQian4e8cd7b2022-04-21 09:48:09 +0000852 ssl->handshake->hrr_selected_group = named_group;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000853 }
854
Gilles Peskine449bd832023-01-11 14:50:10 +0100855 return 0;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000856
857}
Przemek Stekiel8c0a9532023-06-15 16:48:19 +0200858#endif /* PSA_WANT_ALG_ECDH || PSA_WANT_ALG_FFDH */
XiaokangQian7807f9f2022-02-15 10:04:37 +0000859
XiaokangQian08037552022-04-20 07:16:41 +0000860#define SSL_TLS1_3_PARSE_KEY_SHARES_EXT_NO_MATCH 1
861
Valerio Settic9ae8622023-07-25 11:23:50 +0200862#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_EPHEMERAL_ENABLED)
XiaokangQian7807f9f2022-02-15 10:04:37 +0000863/*
864 * ssl_tls13_parse_key_shares_ext() verifies whether the information in the
Xiaokang Qian9f1747b2023-03-30 02:50:04 +0000865 * extension is correct and stores the first acceptable key share and its
866 * associated group.
XiaokangQian7807f9f2022-02-15 10:04:37 +0000867 *
868 * Possible return values are:
869 * - 0: Successful processing of the client provided key share extension.
Xiaokang Qian9f1747b2023-03-30 02:50:04 +0000870 * - SSL_TLS1_3_PARSE_KEY_SHARES_EXT_NO_MATCH: The key shares provided by
871 * the client does not match a group supported by the server. A
872 * HelloRetryRequest will be needed.
XiaokangQiane8ff3502022-04-22 02:34:40 +0000873 * - A negative value for fatal errors.
Jerry Yuc1be19f2022-04-23 16:11:39 +0800874 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200875MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100876static int ssl_tls13_parse_key_shares_ext(mbedtls_ssl_context *ssl,
877 const unsigned char *buf,
878 const unsigned char *end)
XiaokangQian7807f9f2022-02-15 10:04:37 +0000879{
XiaokangQianb67384d2022-04-19 00:02:38 +0000880 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000881 unsigned char const *p = buf;
XiaokangQian8f9dfe42022-04-15 02:52:39 +0000882 unsigned char const *client_shares_end;
Jerry Yuc1be19f2022-04-23 16:11:39 +0800883 size_t client_shares_len;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000884
885 /* From RFC 8446:
886 *
887 * struct {
888 * KeyShareEntry client_shares<0..2^16-1>;
889 * } KeyShareClientHello;
890 *
891 */
892
Gilles Peskine449bd832023-01-11 14:50:10 +0100893 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, 2);
894 client_shares_len = MBEDTLS_GET_UINT16_BE(p, 0);
XiaokangQian7807f9f2022-02-15 10:04:37 +0000895 p += 2;
Gilles Peskine449bd832023-01-11 14:50:10 +0100896 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, client_shares_len);
XiaokangQian7807f9f2022-02-15 10:04:37 +0000897
898 ssl->handshake->offered_group_id = 0;
XiaokangQian8f9dfe42022-04-15 02:52:39 +0000899 client_shares_end = p + client_shares_len;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000900
901 /* We try to find a suitable key share entry and copy it to the
902 * handshake context. Later, we have to find out whether we can do
903 * something with the provided key share or whether we have to
XiaokangQianc5763b52022-04-02 03:34:37 +0000904 * dismiss it and send a HelloRetryRequest message.
905 */
XiaokangQian7807f9f2022-02-15 10:04:37 +0000906
Gilles Peskine449bd832023-01-11 14:50:10 +0100907 while (p < client_shares_end) {
XiaokangQian9b5d04b2022-04-10 10:20:43 +0000908 uint16_t group;
Jerry Yuc1be19f2022-04-23 16:11:39 +0800909 size_t key_exchange_len;
Jerry Yu086edc22022-05-05 10:50:38 +0800910 const unsigned char *key_exchange;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000911
912 /*
913 * struct {
914 * NamedGroup group;
915 * opaque key_exchange<1..2^16-1>;
916 * } KeyShareEntry;
917 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100918 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, client_shares_end, 4);
919 group = MBEDTLS_GET_UINT16_BE(p, 0);
920 key_exchange_len = MBEDTLS_GET_UINT16_BE(p, 2);
Jerry Yuc1be19f2022-04-23 16:11:39 +0800921 p += 4;
Jerry Yu086edc22022-05-05 10:50:38 +0800922 key_exchange = p;
Gilles Peskine449bd832023-01-11 14:50:10 +0100923 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, client_shares_end, key_exchange_len);
Jerry Yu086edc22022-05-05 10:50:38 +0800924 p += key_exchange_len;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000925
926 /* Continue parsing even if we have already found a match,
XiaokangQianc5763b52022-04-02 03:34:37 +0000927 * for input validation purposes.
928 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100929 if (!mbedtls_ssl_named_group_is_offered(ssl, group) ||
930 !mbedtls_ssl_named_group_is_supported(group) ||
931 ssl->handshake->offered_group_id != 0) {
XiaokangQian060d8672022-04-21 09:24:56 +0000932 continue;
933 }
XiaokangQian060d8672022-04-21 09:24:56 +0000934
XiaokangQian7807f9f2022-02-15 10:04:37 +0000935 /*
Przemek Stekielc89f3ea2023-05-18 15:45:53 +0200936 * ECDHE and FFDHE groups are supported
XiaokangQian7807f9f2022-02-15 10:04:37 +0000937 */
Przemek Stekielc89f3ea2023-05-18 15:45:53 +0200938 if (mbedtls_ssl_tls13_named_group_is_ecdhe(group) ||
Przemek Stekield5f79e72023-06-29 09:08:43 +0200939 mbedtls_ssl_tls13_named_group_is_ffdh(group)) {
Przemek Stekielc89f3ea2023-05-18 15:45:53 +0200940 MBEDTLS_SSL_DEBUG_MSG(2, ("ECDH/FFDH group: %s (%04x)",
Gilles Peskine449bd832023-01-11 14:50:10 +0100941 mbedtls_ssl_named_group_to_str(group),
942 group));
Przemek Stekiel7ac93be2023-07-04 10:02:38 +0200943 ret = mbedtls_ssl_tls13_read_public_xxdhe_share(
Gilles Peskine449bd832023-01-11 14:50:10 +0100944 ssl, key_exchange - 2, key_exchange_len + 2);
945 if (ret != 0) {
946 return ret;
947 }
XiaokangQian4e8cd7b2022-04-21 09:48:09 +0000948
Gilles Peskine449bd832023-01-11 14:50:10 +0100949 } else {
950 MBEDTLS_SSL_DEBUG_MSG(4, ("Unrecognized NamedGroup %u",
951 (unsigned) group));
XiaokangQian7807f9f2022-02-15 10:04:37 +0000952 continue;
953 }
954
XiaokangQian9b5d04b2022-04-10 10:20:43 +0000955 ssl->handshake->offered_group_id = group;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000956 }
957
Jerry Yuc1be19f2022-04-23 16:11:39 +0800958
Gilles Peskine449bd832023-01-11 14:50:10 +0100959 if (ssl->handshake->offered_group_id == 0) {
960 MBEDTLS_SSL_DEBUG_MSG(1, ("no matching key share"));
961 return SSL_TLS1_3_PARSE_KEY_SHARES_EXT_NO_MATCH;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000962 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100963 return 0;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000964}
Valerio Settic9ae8622023-07-25 11:23:50 +0200965#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_EPHEMERAL_ENABLED */
XiaokangQian7807f9f2022-02-15 10:04:37 +0000966
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200967MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100968static int ssl_tls13_client_hello_has_exts(mbedtls_ssl_context *ssl,
969 int exts_mask)
XiaokangQian7807f9f2022-02-15 10:04:37 +0000970{
Jerry Yu0c354a22022-08-29 15:25:36 +0800971 int masked = ssl->handshake->received_extensions & exts_mask;
Gilles Peskine449bd832023-01-11 14:50:10 +0100972 return masked == exts_mask;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000973}
974
Jerry Yue5991322022-11-07 14:03:44 +0800975#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200976MBEDTLS_CHECK_RETURN_CRITICAL
XiaokangQianb67384d2022-04-19 00:02:38 +0000977static int ssl_tls13_client_hello_has_exts_for_ephemeral_key_exchange(
Gilles Peskine449bd832023-01-11 14:50:10 +0100978 mbedtls_ssl_context *ssl)
XiaokangQian7807f9f2022-02-15 10:04:37 +0000979{
Gilles Peskine449bd832023-01-11 14:50:10 +0100980 return ssl_tls13_client_hello_has_exts(
981 ssl,
982 MBEDTLS_SSL_EXT_MASK(SUPPORTED_GROUPS) |
983 MBEDTLS_SSL_EXT_MASK(KEY_SHARE) |
984 MBEDTLS_SSL_EXT_MASK(SIG_ALG));
XiaokangQian7807f9f2022-02-15 10:04:37 +0000985}
Jerry Yue5991322022-11-07 14:03:44 +0800986#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED */
XiaokangQian7807f9f2022-02-15 10:04:37 +0000987
Jerry Yue5991322022-11-07 14:03:44 +0800988#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK_ENABLED)
Jerry Yu77f01482022-07-11 07:03:24 +0000989MBEDTLS_CHECK_RETURN_CRITICAL
990static int ssl_tls13_client_hello_has_exts_for_psk_key_exchange(
Gilles Peskine449bd832023-01-11 14:50:10 +0100991 mbedtls_ssl_context *ssl)
Jerry Yu77f01482022-07-11 07:03:24 +0000992{
Gilles Peskine449bd832023-01-11 14:50:10 +0100993 return ssl_tls13_client_hello_has_exts(
994 ssl,
995 MBEDTLS_SSL_EXT_MASK(PRE_SHARED_KEY) |
996 MBEDTLS_SSL_EXT_MASK(PSK_KEY_EXCHANGE_MODES));
Jerry Yu77f01482022-07-11 07:03:24 +0000997}
Jerry Yue5991322022-11-07 14:03:44 +0800998#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK_ENABLED */
Jerry Yu77f01482022-07-11 07:03:24 +0000999
Jerry Yue5991322022-11-07 14:03:44 +08001000#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK_EPHEMERAL_ENABLED)
Jerry Yu77f01482022-07-11 07:03:24 +00001001MBEDTLS_CHECK_RETURN_CRITICAL
1002static int ssl_tls13_client_hello_has_exts_for_psk_ephemeral_key_exchange(
Gilles Peskine449bd832023-01-11 14:50:10 +01001003 mbedtls_ssl_context *ssl)
Jerry Yu77f01482022-07-11 07:03:24 +00001004{
Gilles Peskine449bd832023-01-11 14:50:10 +01001005 return ssl_tls13_client_hello_has_exts(
1006 ssl,
1007 MBEDTLS_SSL_EXT_MASK(SUPPORTED_GROUPS) |
1008 MBEDTLS_SSL_EXT_MASK(KEY_SHARE) |
1009 MBEDTLS_SSL_EXT_MASK(PRE_SHARED_KEY) |
1010 MBEDTLS_SSL_EXT_MASK(PSK_KEY_EXCHANGE_MODES));
Jerry Yu77f01482022-07-11 07:03:24 +00001011}
Jerry Yue5991322022-11-07 14:03:44 +08001012#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK_EPHEMERAL_ENABLED */
Jerry Yu77f01482022-07-11 07:03:24 +00001013
Pengyu Lv306a01d2023-02-02 16:35:47 +08001014#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED)
1015MBEDTLS_CHECK_RETURN_CRITICAL
Pengyu Lvd72e8582023-11-10 10:37:18 +08001016static int ssl_tls13_ticket_is_kex_mode_permitted(mbedtls_ssl_context *ssl,
1017 unsigned int kex_mode)
Pengyu Lv306a01d2023-02-02 16:35:47 +08001018{
1019#if defined(MBEDTLS_SSL_SESSION_TICKETS)
1020 if (ssl->handshake->resume) {
Pengyu Lv94a42cc2023-12-06 10:04:17 +08001021 if (!mbedtls_ssl_tls13_session_ticket_has_flags(
Pengyu Lv306a01d2023-02-02 16:35:47 +08001022 ssl->session_negotiate, kex_mode)) {
1023 return 0;
1024 }
1025 }
1026#else
1027 ((void) ssl);
1028 ((void) kex_mode);
1029#endif
1030 return 1;
1031}
1032#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED */
1033
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001034MBEDTLS_CHECK_RETURN_CRITICAL
Pengyu Lvbc4aab72023-12-01 15:37:24 +08001035static int ssl_tls13_key_exchange_is_ephemeral_available(mbedtls_ssl_context *ssl)
XiaokangQian7807f9f2022-02-15 10:04:37 +00001036{
Jerry Yue5991322022-11-07 14:03:44 +08001037#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED)
Pengyu Lv0a1ff2b2023-11-14 11:03:32 +08001038 return mbedtls_ssl_conf_tls13_is_ephemeral_enabled(ssl) &&
Gilles Peskine449bd832023-01-11 14:50:10 +01001039 ssl_tls13_client_hello_has_exts_for_ephemeral_key_exchange(ssl);
Jerry Yue5991322022-11-07 14:03:44 +08001040#else
1041 ((void) ssl);
Gilles Peskine449bd832023-01-11 14:50:10 +01001042 return 0;
Jerry Yue5991322022-11-07 14:03:44 +08001043#endif
Jerry Yu77f01482022-07-11 07:03:24 +00001044}
XiaokangQian7807f9f2022-02-15 10:04:37 +00001045
Jerry Yu77f01482022-07-11 07:03:24 +00001046MBEDTLS_CHECK_RETURN_CRITICAL
Pengyu Lvbc4aab72023-12-01 15:37:24 +08001047static int ssl_tls13_key_exchange_is_psk_available(mbedtls_ssl_context *ssl)
Jerry Yu77f01482022-07-11 07:03:24 +00001048{
Jerry Yue5991322022-11-07 14:03:44 +08001049#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK_ENABLED)
Pengyu Lvd72e8582023-11-10 10:37:18 +08001050 return ssl_tls13_ticket_is_kex_mode_permitted(
Pengyu Lv306a01d2023-02-02 16:35:47 +08001051 ssl, MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK) &&
Pengyu Lv0a1ff2b2023-11-14 11:03:32 +08001052 mbedtls_ssl_conf_tls13_is_psk_enabled(ssl) &&
Pengyu Lvb2cfafb2023-11-14 13:56:13 +08001053 mbedtls_ssl_tls13_is_psk_supported(ssl) &&
Gilles Peskine449bd832023-01-11 14:50:10 +01001054 ssl_tls13_client_hello_has_exts_for_psk_key_exchange(ssl);
Jerry Yu77f01482022-07-11 07:03:24 +00001055#else
1056 ((void) ssl);
Gilles Peskine449bd832023-01-11 14:50:10 +01001057 return 0;
Jerry Yu77f01482022-07-11 07:03:24 +00001058#endif
1059}
XiaokangQian7807f9f2022-02-15 10:04:37 +00001060
Jerry Yu77f01482022-07-11 07:03:24 +00001061MBEDTLS_CHECK_RETURN_CRITICAL
Pengyu Lvbc4aab72023-12-01 15:37:24 +08001062static int ssl_tls13_key_exchange_is_psk_ephemeral_available(mbedtls_ssl_context *ssl)
Jerry Yu77f01482022-07-11 07:03:24 +00001063{
Jerry Yue5991322022-11-07 14:03:44 +08001064#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK_EPHEMERAL_ENABLED)
Pengyu Lvd72e8582023-11-10 10:37:18 +08001065 return ssl_tls13_ticket_is_kex_mode_permitted(
Pengyu Lv306a01d2023-02-02 16:35:47 +08001066 ssl, MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK_EPHEMERAL) &&
Pengyu Lv0a1ff2b2023-11-14 11:03:32 +08001067 mbedtls_ssl_conf_tls13_is_psk_ephemeral_enabled(ssl) &&
Pengyu Lvb2cfafb2023-11-14 13:56:13 +08001068 mbedtls_ssl_tls13_is_psk_ephemeral_supported(ssl) &&
Gilles Peskine449bd832023-01-11 14:50:10 +01001069 ssl_tls13_client_hello_has_exts_for_psk_ephemeral_key_exchange(ssl);
Jerry Yu77f01482022-07-11 07:03:24 +00001070#else
1071 ((void) ssl);
Gilles Peskine449bd832023-01-11 14:50:10 +01001072 return 0;
Jerry Yu77f01482022-07-11 07:03:24 +00001073#endif
1074}
1075
Gilles Peskine449bd832023-01-11 14:50:10 +01001076static int ssl_tls13_determine_key_exchange_mode(mbedtls_ssl_context *ssl)
Jerry Yu77f01482022-07-11 07:03:24 +00001077{
1078 /*
1079 * Determine the key exchange algorithm to use.
1080 * There are three types of key exchanges supported in TLS 1.3:
1081 * - (EC)DH with ECDSA,
1082 * - (EC)DH with PSK,
1083 * - plain PSK.
1084 *
1085 * The PSK-based key exchanges may additionally be used with 0-RTT.
1086 *
1087 * Our built-in order of preference is
Jerry Yuce6ed702022-07-22 21:49:53 +08001088 * 1 ) (EC)DHE-PSK Mode ( psk_ephemeral )
1089 * 2 ) Certificate Mode ( ephemeral )
1090 * 3 ) Plain PSK Mode ( psk )
Jerry Yu77f01482022-07-11 07:03:24 +00001091 */
1092
Xiaokang Qian9f1747b2023-03-30 02:50:04 +00001093 ssl->handshake->key_exchange_mode =
1094 MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_NONE;
Jerry Yu77f01482022-07-11 07:03:24 +00001095
Pengyu Lvbc4aab72023-12-01 15:37:24 +08001096 if (ssl_tls13_key_exchange_is_psk_ephemeral_available(ssl)) {
Jerry Yu77f01482022-07-11 07:03:24 +00001097 ssl->handshake->key_exchange_mode =
1098 MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK_EPHEMERAL;
Gilles Peskine449bd832023-01-11 14:50:10 +01001099 MBEDTLS_SSL_DEBUG_MSG(2, ("key exchange mode: psk_ephemeral"));
1100 } else
Pengyu Lvbc4aab72023-12-01 15:37:24 +08001101 if (ssl_tls13_key_exchange_is_ephemeral_available(ssl)) {
Jerry Yu77f01482022-07-11 07:03:24 +00001102 ssl->handshake->key_exchange_mode =
1103 MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL;
Gilles Peskine449bd832023-01-11 14:50:10 +01001104 MBEDTLS_SSL_DEBUG_MSG(2, ("key exchange mode: ephemeral"));
1105 } else
Pengyu Lvbc4aab72023-12-01 15:37:24 +08001106 if (ssl_tls13_key_exchange_is_psk_available(ssl)) {
Jerry Yuce6ed702022-07-22 21:49:53 +08001107 ssl->handshake->key_exchange_mode =
1108 MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK;
Gilles Peskine449bd832023-01-11 14:50:10 +01001109 MBEDTLS_SSL_DEBUG_MSG(2, ("key exchange mode: psk"));
1110 } else {
Jerry Yu77f01482022-07-11 07:03:24 +00001111 MBEDTLS_SSL_DEBUG_MSG(
Gilles Peskine449bd832023-01-11 14:50:10 +01001112 1,
1113 ("ClientHello message misses mandatory extensions."));
1114 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_MISSING_EXTENSION,
1115 MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER);
1116 return MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER;
Jerry Yu77f01482022-07-11 07:03:24 +00001117 }
1118
Gilles Peskine449bd832023-01-11 14:50:10 +01001119 return 0;
Jerry Yu77f01482022-07-11 07:03:24 +00001120
XiaokangQian7807f9f2022-02-15 10:04:37 +00001121}
1122
XiaokangQian81802f42022-06-10 13:25:22 +00001123#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
Ronald Cron928cbd32022-10-04 16:14:26 +02001124 defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED)
Ronald Cron67ea2542022-09-15 17:34:42 +02001125
1126#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01001127static psa_algorithm_t ssl_tls13_iana_sig_alg_to_psa_alg(uint16_t sig_alg)
Ronald Cron67ea2542022-09-15 17:34:42 +02001128{
Gilles Peskine449bd832023-01-11 14:50:10 +01001129 switch (sig_alg) {
Ronald Cron67ea2542022-09-15 17:34:42 +02001130 case MBEDTLS_TLS1_3_SIG_ECDSA_SECP256R1_SHA256:
Gilles Peskine449bd832023-01-11 14:50:10 +01001131 return PSA_ALG_ECDSA(PSA_ALG_SHA_256);
Ronald Cron67ea2542022-09-15 17:34:42 +02001132 case MBEDTLS_TLS1_3_SIG_ECDSA_SECP384R1_SHA384:
Gilles Peskine449bd832023-01-11 14:50:10 +01001133 return PSA_ALG_ECDSA(PSA_ALG_SHA_384);
Ronald Cron67ea2542022-09-15 17:34:42 +02001134 case MBEDTLS_TLS1_3_SIG_ECDSA_SECP521R1_SHA512:
Gilles Peskine449bd832023-01-11 14:50:10 +01001135 return PSA_ALG_ECDSA(PSA_ALG_SHA_512);
Ronald Cron67ea2542022-09-15 17:34:42 +02001136 case MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA256:
Gilles Peskine449bd832023-01-11 14:50:10 +01001137 return PSA_ALG_RSA_PSS(PSA_ALG_SHA_256);
Ronald Cron67ea2542022-09-15 17:34:42 +02001138 case MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA384:
Gilles Peskine449bd832023-01-11 14:50:10 +01001139 return PSA_ALG_RSA_PSS(PSA_ALG_SHA_384);
Ronald Cron67ea2542022-09-15 17:34:42 +02001140 case MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA512:
Gilles Peskine449bd832023-01-11 14:50:10 +01001141 return PSA_ALG_RSA_PSS(PSA_ALG_SHA_512);
Ronald Cron67ea2542022-09-15 17:34:42 +02001142 case MBEDTLS_TLS1_3_SIG_RSA_PKCS1_SHA256:
Gilles Peskine449bd832023-01-11 14:50:10 +01001143 return PSA_ALG_RSA_PKCS1V15_SIGN(PSA_ALG_SHA_256);
Ronald Cron67ea2542022-09-15 17:34:42 +02001144 case MBEDTLS_TLS1_3_SIG_RSA_PKCS1_SHA384:
Gilles Peskine449bd832023-01-11 14:50:10 +01001145 return PSA_ALG_RSA_PKCS1V15_SIGN(PSA_ALG_SHA_384);
Ronald Cron67ea2542022-09-15 17:34:42 +02001146 case MBEDTLS_TLS1_3_SIG_RSA_PKCS1_SHA512:
Gilles Peskine449bd832023-01-11 14:50:10 +01001147 return PSA_ALG_RSA_PKCS1V15_SIGN(PSA_ALG_SHA_512);
Ronald Cron67ea2542022-09-15 17:34:42 +02001148 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01001149 return PSA_ALG_NONE;
Ronald Cron67ea2542022-09-15 17:34:42 +02001150 }
1151}
1152#endif /* MBEDTLS_USE_PSA_CRYPTO */
1153
XiaokangQian23c5be62022-06-07 02:04:34 +00001154/*
XiaokangQianfb665a82022-06-15 03:57:21 +00001155 * Pick best ( private key, certificate chain ) pair based on the signature
1156 * algorithms supported by the client.
XiaokangQian23c5be62022-06-07 02:04:34 +00001157 */
Ronald Cronce7d76e2022-07-08 18:56:49 +02001158MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01001159static int ssl_tls13_pick_key_cert(mbedtls_ssl_context *ssl)
XiaokangQian23c5be62022-06-07 02:04:34 +00001160{
XiaokangQianfb665a82022-06-15 03:57:21 +00001161 mbedtls_ssl_key_cert *key_cert, *key_cert_list;
XiaokangQian81802f42022-06-10 13:25:22 +00001162 const uint16_t *sig_alg = ssl->handshake->received_sig_algs;
XiaokangQian23c5be62022-06-07 02:04:34 +00001163
1164#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Gilles Peskine449bd832023-01-11 14:50:10 +01001165 if (ssl->handshake->sni_key_cert != NULL) {
XiaokangQianfb665a82022-06-15 03:57:21 +00001166 key_cert_list = ssl->handshake->sni_key_cert;
Gilles Peskine449bd832023-01-11 14:50:10 +01001167 } else
XiaokangQian23c5be62022-06-07 02:04:34 +00001168#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
Gilles Peskine449bd832023-01-11 14:50:10 +01001169 key_cert_list = ssl->conf->key_cert;
XiaokangQian23c5be62022-06-07 02:04:34 +00001170
Gilles Peskine449bd832023-01-11 14:50:10 +01001171 if (key_cert_list == NULL) {
1172 MBEDTLS_SSL_DEBUG_MSG(3, ("server has no certificate"));
1173 return -1;
XiaokangQian23c5be62022-06-07 02:04:34 +00001174 }
1175
Gilles Peskine449bd832023-01-11 14:50:10 +01001176 for (; *sig_alg != MBEDTLS_TLS1_3_SIG_NONE; sig_alg++) {
1177 if (!mbedtls_ssl_sig_alg_is_offered(ssl, *sig_alg)) {
Ronald Cron67ea2542022-09-15 17:34:42 +02001178 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +01001179 }
Ronald Cron67ea2542022-09-15 17:34:42 +02001180
Gilles Peskine449bd832023-01-11 14:50:10 +01001181 if (!mbedtls_ssl_tls13_sig_alg_for_cert_verify_is_supported(*sig_alg)) {
Ronald Cron67ea2542022-09-15 17:34:42 +02001182 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +01001183 }
Ronald Cron67ea2542022-09-15 17:34:42 +02001184
Gilles Peskine449bd832023-01-11 14:50:10 +01001185 for (key_cert = key_cert_list; key_cert != NULL;
1186 key_cert = key_cert->next) {
Ronald Cron67ea2542022-09-15 17:34:42 +02001187#if defined(MBEDTLS_USE_PSA_CRYPTO)
1188 psa_algorithm_t psa_alg = PSA_ALG_NONE;
1189#endif /* MBEDTLS_USE_PSA_CRYPTO */
1190
Gilles Peskine449bd832023-01-11 14:50:10 +01001191 MBEDTLS_SSL_DEBUG_CRT(3, "certificate (chain) candidate",
1192 key_cert->cert);
XiaokangQian81802f42022-06-10 13:25:22 +00001193
1194 /*
Gilles Peskine449bd832023-01-11 14:50:10 +01001195 * This avoids sending the client a cert it'll reject based on
1196 * keyUsage or other extensions.
1197 */
1198 if (mbedtls_x509_crt_check_key_usage(
1199 key_cert->cert, MBEDTLS_X509_KU_DIGITAL_SIGNATURE) != 0 ||
XiaokangQian81802f42022-06-10 13:25:22 +00001200 mbedtls_x509_crt_check_extended_key_usage(
XiaokangQianfb665a82022-06-15 03:57:21 +00001201 key_cert->cert, MBEDTLS_OID_SERVER_AUTH,
Gilles Peskine449bd832023-01-11 14:50:10 +01001202 MBEDTLS_OID_SIZE(MBEDTLS_OID_SERVER_AUTH)) != 0) {
1203 MBEDTLS_SSL_DEBUG_MSG(3, ("certificate mismatch: "
1204 "(extended) key usage extension"));
XiaokangQian81802f42022-06-10 13:25:22 +00001205 continue;
1206 }
XiaokangQian23c5be62022-06-07 02:04:34 +00001207
Gilles Peskine449bd832023-01-11 14:50:10 +01001208 MBEDTLS_SSL_DEBUG_MSG(3,
1209 ("ssl_tls13_pick_key_cert:"
1210 "check signature algorithm %s [%04x]",
1211 mbedtls_ssl_sig_alg_to_str(*sig_alg),
1212 *sig_alg));
Ronald Cron67ea2542022-09-15 17:34:42 +02001213#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01001214 psa_alg = ssl_tls13_iana_sig_alg_to_psa_alg(*sig_alg);
Ronald Cron67ea2542022-09-15 17:34:42 +02001215#endif /* MBEDTLS_USE_PSA_CRYPTO */
1216
Gilles Peskine449bd832023-01-11 14:50:10 +01001217 if (mbedtls_ssl_tls13_check_sig_alg_cert_key_match(
1218 *sig_alg, &key_cert->cert->pk)
Ronald Cron67ea2542022-09-15 17:34:42 +02001219#if defined(MBEDTLS_USE_PSA_CRYPTO)
1220 && psa_alg != PSA_ALG_NONE &&
Gilles Peskine449bd832023-01-11 14:50:10 +01001221 mbedtls_pk_can_do_ext(&key_cert->cert->pk, psa_alg,
1222 PSA_KEY_USAGE_SIGN_HASH) == 1
Ronald Cron67ea2542022-09-15 17:34:42 +02001223#endif /* MBEDTLS_USE_PSA_CRYPTO */
Gilles Peskine449bd832023-01-11 14:50:10 +01001224 ) {
XiaokangQianfb665a82022-06-15 03:57:21 +00001225 ssl->handshake->key_cert = key_cert;
Gilles Peskine449bd832023-01-11 14:50:10 +01001226 MBEDTLS_SSL_DEBUG_MSG(3,
1227 ("ssl_tls13_pick_key_cert:"
1228 "selected signature algorithm"
1229 " %s [%04x]",
1230 mbedtls_ssl_sig_alg_to_str(*sig_alg),
1231 *sig_alg));
XiaokangQianfb665a82022-06-15 03:57:21 +00001232 MBEDTLS_SSL_DEBUG_CRT(
Gilles Peskine449bd832023-01-11 14:50:10 +01001233 3, "selected certificate (chain)",
1234 ssl->handshake->key_cert->cert);
1235 return 0;
XiaokangQian81802f42022-06-10 13:25:22 +00001236 }
XiaokangQian81802f42022-06-10 13:25:22 +00001237 }
XiaokangQian23c5be62022-06-07 02:04:34 +00001238 }
1239
Gilles Peskine449bd832023-01-11 14:50:10 +01001240 MBEDTLS_SSL_DEBUG_MSG(2, ("ssl_tls13_pick_key_cert:"
1241 "no suitable certificate found"));
1242 return -1;
XiaokangQian23c5be62022-06-07 02:04:34 +00001243}
XiaokangQian81802f42022-06-10 13:25:22 +00001244#endif /* MBEDTLS_X509_CRT_PARSE_C &&
Ronald Cron928cbd32022-10-04 16:14:26 +02001245 MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED */
XiaokangQian23c5be62022-06-07 02:04:34 +00001246
XiaokangQian4080a7f2022-04-11 09:55:18 +00001247/*
XiaokangQianed582dd2022-04-13 08:21:05 +00001248 *
1249 * STATE HANDLING: ClientHello
1250 *
XiaokangQianb67384d2022-04-19 00:02:38 +00001251 * There are three possible classes of outcomes when parsing the ClientHello:
XiaokangQianed582dd2022-04-13 08:21:05 +00001252 *
XiaokangQianb67384d2022-04-19 00:02:38 +00001253 * 1) The ClientHello was well-formed and matched the server's configuration.
XiaokangQianed582dd2022-04-13 08:21:05 +00001254 *
1255 * In this case, the server progresses to sending its ServerHello.
1256 *
XiaokangQianb67384d2022-04-19 00:02:38 +00001257 * 2) The ClientHello was well-formed but didn't match the server's
1258 * configuration.
XiaokangQianed582dd2022-04-13 08:21:05 +00001259 *
1260 * For example, the client might not have offered a key share which
1261 * the server supports, or the server might require a cookie.
1262 *
1263 * In this case, the server sends a HelloRetryRequest.
1264 *
XiaokangQianb67384d2022-04-19 00:02:38 +00001265 * 3) The ClientHello was ill-formed
XiaokangQianed582dd2022-04-13 08:21:05 +00001266 *
1267 * In this case, we abort the handshake.
1268 *
1269 */
1270
1271/*
XiaokangQian4080a7f2022-04-11 09:55:18 +00001272 * Structure of this message:
1273 *
XiaokangQiane8ff3502022-04-22 02:34:40 +00001274 * uint16 ProtocolVersion;
1275 * opaque Random[32];
1276 * uint8 CipherSuite[2]; // Cryptographic suite selector
XiaokangQian4080a7f2022-04-11 09:55:18 +00001277 *
XiaokangQiane8ff3502022-04-22 02:34:40 +00001278 * struct {
1279 * ProtocolVersion legacy_version = 0x0303; // TLS v1.2
1280 * Random random;
1281 * opaque legacy_session_id<0..32>;
1282 * CipherSuite cipher_suites<2..2^16-2>;
1283 * opaque legacy_compression_methods<1..2^8-1>;
1284 * Extension extensions<8..2^16-1>;
1285 * } ClientHello;
XiaokangQian4080a7f2022-04-11 09:55:18 +00001286 */
XiaokangQianed582dd2022-04-13 08:21:05 +00001287
1288#define SSL_CLIENT_HELLO_OK 0
1289#define SSL_CLIENT_HELLO_HRR_REQUIRED 1
Ronald Cron5af4c7f2023-03-07 20:46:59 +01001290#define SSL_CLIENT_HELLO_TLS1_2 2
XiaokangQianed582dd2022-04-13 08:21:05 +00001291
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001292MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01001293static int ssl_tls13_parse_client_hello(mbedtls_ssl_context *ssl,
1294 const unsigned char *buf,
1295 const unsigned char *end)
XiaokangQian7807f9f2022-02-15 10:04:37 +00001296{
XiaokangQianb67384d2022-04-19 00:02:38 +00001297 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1298 const unsigned char *p = buf;
Ronald Crond540d992023-03-07 09:41:48 +01001299 const unsigned char *random;
XiaokangQian4080a7f2022-04-11 09:55:18 +00001300 size_t legacy_session_id_len;
Ronald Croncada4102023-03-07 09:51:39 +01001301 const unsigned char *legacy_session_id;
XiaokangQian318dc762022-04-20 09:43:51 +00001302 size_t cipher_suites_len;
Ronald Cron2f16b4e2023-03-07 10:07:32 +01001303 const unsigned char *cipher_suites;
XiaokangQian060d8672022-04-21 09:24:56 +00001304 const unsigned char *cipher_suites_end;
XiaokangQianb67384d2022-04-19 00:02:38 +00001305 size_t extensions_len;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001306 const unsigned char *extensions_end;
Ronald Croneff56732023-04-03 17:36:31 +02001307 const unsigned char *supported_versions_data;
1308 const unsigned char *supported_versions_data_end;
Jerry Yuc4bf5d62022-10-29 09:08:47 +08001309 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
Jerry Yu49ca9282022-05-05 11:05:22 +08001310 int hrr_required = 0;
Ronald Cron8a74f072023-06-14 17:59:29 +02001311 int no_usable_share_for_key_agreement = 0;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001312
Ronald Cron41a443a2022-10-04 16:38:25 +02001313#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED)
Jerry Yu29d9faa2022-08-23 17:52:45 +08001314 const unsigned char *pre_shared_key_ext = NULL;
Jerry Yu1c105562022-07-10 06:32:38 +00001315 const unsigned char *pre_shared_key_ext_end = NULL;
Ronald Cron41a443a2022-10-04 16:38:25 +02001316#endif
XiaokangQian7807f9f2022-02-15 10:04:37 +00001317
XiaokangQian7807f9f2022-02-15 10:04:37 +00001318 /*
XiaokangQianb67384d2022-04-19 00:02:38 +00001319 * ClientHello layout:
XiaokangQian7807f9f2022-02-15 10:04:37 +00001320 * 0 . 1 protocol version
XiaokangQiane8ff3502022-04-22 02:34:40 +00001321 * 2 . 33 random bytes
XiaokangQianc5763b52022-04-02 03:34:37 +00001322 * 34 . 34 session id length ( 1 byte )
XiaokangQian7807f9f2022-02-15 10:04:37 +00001323 * 35 . 34+x session id
XiaokangQian7807f9f2022-02-15 10:04:37 +00001324 * .. . .. ciphersuite list length ( 2 bytes )
1325 * .. . .. ciphersuite list
1326 * .. . .. compression alg. list length ( 1 byte )
1327 * .. . .. compression alg. list
1328 * .. . .. extensions length ( 2 bytes, optional )
1329 * .. . .. extensions ( optional )
1330 */
1331
XiaokangQianb67384d2022-04-19 00:02:38 +00001332 /*
bootstrap-prime6dbbf442022-05-17 19:30:44 -04001333 * Minimal length ( with everything empty and extensions omitted ) is
XiaokangQian7807f9f2022-02-15 10:04:37 +00001334 * 2 + 32 + 1 + 2 + 1 = 38 bytes. Check that first, so that we can
1335 * read at least up to session id length without worrying.
1336 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001337 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, 38);
XiaokangQian7807f9f2022-02-15 10:04:37 +00001338
1339 /* ...
1340 * ProtocolVersion legacy_version = 0x0303; // TLS 1.2
1341 * ...
1342 * with ProtocolVersion defined as:
1343 * uint16 ProtocolVersion;
1344 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001345 if (mbedtls_ssl_read_version(p, ssl->conf->transport) !=
1346 MBEDTLS_SSL_VERSION_TLS1_2) {
1347 MBEDTLS_SSL_DEBUG_MSG(1, ("Unsupported version of TLS."));
1348 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_PROTOCOL_VERSION,
1349 MBEDTLS_ERR_SSL_BAD_PROTOCOL_VERSION);
1350 return MBEDTLS_ERR_SSL_BAD_PROTOCOL_VERSION;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001351 }
1352 p += 2;
1353
Jerry Yuc1be19f2022-04-23 16:11:39 +08001354 /* ...
1355 * Random random;
1356 * ...
XiaokangQianb67384d2022-04-19 00:02:38 +00001357 * with Random defined as:
1358 * opaque Random[32];
XiaokangQian7807f9f2022-02-15 10:04:37 +00001359 */
Ronald Crond540d992023-03-07 09:41:48 +01001360 random = p;
XiaokangQian08037552022-04-20 07:16:41 +00001361 p += MBEDTLS_CLIENT_HELLO_RANDOM_LEN;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001362
Jerry Yuc1be19f2022-04-23 16:11:39 +08001363 /* ...
XiaokangQianb67384d2022-04-19 00:02:38 +00001364 * opaque legacy_session_id<0..32>;
Jerry Yuc1be19f2022-04-23 16:11:39 +08001365 * ...
XiaokangQian7807f9f2022-02-15 10:04:37 +00001366 */
Ronald Croncada4102023-03-07 09:51:39 +01001367 legacy_session_id_len = *(p++);
1368 legacy_session_id = p;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001369
XiaokangQianb67384d2022-04-19 00:02:38 +00001370 /*
1371 * Check we have enough data for the legacy session identifier
Jerry Yue95c8af2022-07-26 15:48:20 +08001372 * and the ciphersuite list length.
XiaokangQianb67384d2022-04-19 00:02:38 +00001373 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001374 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, legacy_session_id_len + 2);
XiaokangQian4080a7f2022-04-11 09:55:18 +00001375 p += legacy_session_id_len;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001376
Ronald Cron2f16b4e2023-03-07 10:07:32 +01001377 /* ...
1378 * CipherSuite cipher_suites<2..2^16-2>;
1379 * ...
1380 * with CipherSuite defined as:
1381 * uint8 CipherSuite[2];
1382 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001383 cipher_suites_len = MBEDTLS_GET_UINT16_BE(p, 0);
XiaokangQian7807f9f2022-02-15 10:04:37 +00001384 p += 2;
Ronald Cron2f16b4e2023-03-07 10:07:32 +01001385 cipher_suites = p;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001386
Ronald Cronfc7ae872023-02-16 15:32:19 +01001387 /*
1388 * The length of the ciphersuite list has to be even.
1389 */
1390 if (cipher_suites_len & 1) {
1391 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR,
1392 MBEDTLS_ERR_SSL_DECODE_ERROR);
1393 return MBEDTLS_ERR_SSL_DECODE_ERROR;
1394 }
1395
XiaokangQianb67384d2022-04-19 00:02:38 +00001396 /* Check we have enough data for the ciphersuite list, the legacy
1397 * compression methods and the length of the extensions.
Jerry Yue95c8af2022-07-26 15:48:20 +08001398 *
1399 * cipher_suites cipher_suites_len bytes
1400 * legacy_compression_methods 2 bytes
1401 * extensions_len 2 bytes
XiaokangQianb67384d2022-04-19 00:02:38 +00001402 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001403 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, cipher_suites_len + 2 + 2);
Ronald Cron8c527d02023-03-07 15:47:47 +01001404 p += cipher_suites_len;
1405 cipher_suites_end = p;
Jerry Yu5725f1c2022-08-21 17:27:16 +08001406
1407 /*
Ronald Cron8c527d02023-03-07 15:47:47 +01001408 * Search for the supported versions extension and parse it to determine
1409 * if the client supports TLS 1.3.
1410 */
1411 ret = mbedtls_ssl_tls13_is_supported_versions_ext_present_in_exts(
1412 ssl, p + 2, end,
Ronald Croneff56732023-04-03 17:36:31 +02001413 &supported_versions_data, &supported_versions_data_end);
Ronald Cron8c527d02023-03-07 15:47:47 +01001414 if (ret < 0) {
1415 MBEDTLS_SSL_DEBUG_RET(1,
1416 ("mbedtls_ssl_tls13_is_supported_versions_ext_present_in_exts"), ret);
1417 return ret;
1418 }
1419
1420 if (ret == 0) {
Ronald Cron5af4c7f2023-03-07 20:46:59 +01001421 return SSL_CLIENT_HELLO_TLS1_2;
Ronald Cron8c527d02023-03-07 15:47:47 +01001422 }
1423
Ronald Cron5af4c7f2023-03-07 20:46:59 +01001424 if (ret == 1) {
1425 ret = ssl_tls13_parse_supported_versions_ext(ssl,
Ronald Croneff56732023-04-03 17:36:31 +02001426 supported_versions_data,
1427 supported_versions_data_end);
Ronald Cron5af4c7f2023-03-07 20:46:59 +01001428 if (ret < 0) {
1429 MBEDTLS_SSL_DEBUG_RET(1,
1430 ("ssl_tls13_parse_supported_versions_ext"), ret);
1431 return ret;
1432 }
1433
Ronald Cronb828c7d2023-04-03 16:37:22 +02001434 /*
1435 * The supported versions extension was parsed successfully as the
1436 * value returned by ssl_tls13_parse_supported_versions_ext() is
1437 * positive. The return value is then equal to
1438 * MBEDTLS_SSL_VERSION_TLS1_2 or MBEDTLS_SSL_VERSION_TLS1_3, defining
1439 * the TLS version to negotiate.
1440 */
Ronald Cron5af4c7f2023-03-07 20:46:59 +01001441 if (MBEDTLS_SSL_VERSION_TLS1_2 == ret) {
1442 return SSL_CLIENT_HELLO_TLS1_2;
1443 }
Ronald Cron8c527d02023-03-07 15:47:47 +01001444 }
1445
1446 /*
1447 * We negotiate TLS 1.3.
Ronald Cron64582392023-03-07 09:21:40 +01001448 */
1449 ssl->tls_version = MBEDTLS_SSL_VERSION_TLS1_3;
Ronald Cron64582392023-03-07 09:21:40 +01001450 ssl->session_negotiate->tls_version = MBEDTLS_SSL_VERSION_TLS1_3;
1451 ssl->session_negotiate->endpoint = ssl->conf->endpoint;
Ronald Cron64582392023-03-07 09:21:40 +01001452
1453 /*
Ronald Crondad02b22023-04-06 09:57:52 +02001454 * We are negotiating the version 1.3 of the protocol. Do what we have
Ronald Croncada4102023-03-07 09:51:39 +01001455 * postponed: copy of the client random bytes, copy of the legacy session
Ronald Cron2f16b4e2023-03-07 10:07:32 +01001456 * identifier and selection of the TLS 1.3 cipher suite.
Ronald Crond540d992023-03-07 09:41:48 +01001457 */
1458 MBEDTLS_SSL_DEBUG_BUF(3, "client hello, random bytes",
1459 random, MBEDTLS_CLIENT_HELLO_RANDOM_LEN);
1460 memcpy(&handshake->randbytes[0], random, MBEDTLS_CLIENT_HELLO_RANDOM_LEN);
1461
Ronald Croncada4102023-03-07 09:51:39 +01001462 if (legacy_session_id_len > sizeof(ssl->session_negotiate->id)) {
1463 MBEDTLS_SSL_DEBUG_MSG(1, ("bad client hello message"));
1464 return MBEDTLS_ERR_SSL_DECODE_ERROR;
1465 }
1466 ssl->session_negotiate->id_len = legacy_session_id_len;
1467 MBEDTLS_SSL_DEBUG_BUF(3, "client hello, session id",
1468 legacy_session_id, legacy_session_id_len);
1469 memcpy(&ssl->session_negotiate->id[0],
1470 legacy_session_id, legacy_session_id_len);
1471
Ronald Crond540d992023-03-07 09:41:48 +01001472 /*
Jerry Yu5725f1c2022-08-21 17:27:16 +08001473 * Search for a matching ciphersuite
1474 */
Ronald Cron2f16b4e2023-03-07 10:07:32 +01001475 MBEDTLS_SSL_DEBUG_BUF(3, "client hello, list of cipher suites",
1476 cipher_suites, cipher_suites_len);
Ronald Crone45afd72023-04-04 15:10:06 +02001477 for (const unsigned char *cipher_suites_p = cipher_suites;
1478 cipher_suites_p < cipher_suites_end; cipher_suites_p += 2) {
XiaokangQiane8ff3502022-04-22 02:34:40 +00001479 uint16_t cipher_suite;
Gilles Peskine449bd832023-01-11 14:50:10 +01001480 const mbedtls_ssl_ciphersuite_t *ciphersuite_info;
Jerry Yu5725f1c2022-08-21 17:27:16 +08001481
Ronald Crond89360b2023-02-21 08:53:33 +01001482 /*
Ronald Crone45afd72023-04-04 15:10:06 +02001483 * "cipher_suites_end - cipher_suites_p is even" is an invariant of the
1484 * loop. As cipher_suites_end - cipher_suites_p > 0, we have
1485 * cipher_suites_end - cipher_suites_p >= 2 and it is thus safe to read
1486 * two bytes.
Ronald Crond89360b2023-02-21 08:53:33 +01001487 */
Ronald Crone45afd72023-04-04 15:10:06 +02001488 cipher_suite = MBEDTLS_GET_UINT16_BE(cipher_suites_p, 0);
Jerry Yuc5a23a02022-08-25 10:51:44 +08001489 ciphersuite_info = ssl_tls13_validate_peer_ciphersuite(
Gilles Peskine449bd832023-01-11 14:50:10 +01001490 ssl, cipher_suite);
1491 if (ciphersuite_info == NULL) {
Jerry Yu5725f1c2022-08-21 17:27:16 +08001492 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +01001493 }
Jerry Yu5725f1c2022-08-21 17:27:16 +08001494
Jerry Yu5725f1c2022-08-21 17:27:16 +08001495 ssl->session_negotiate->ciphersuite = cipher_suite;
Jerry Yuc4bf5d62022-10-29 09:08:47 +08001496 handshake->ciphersuite_info = ciphersuite_info;
Gilles Peskine449bd832023-01-11 14:50:10 +01001497 MBEDTLS_SSL_DEBUG_MSG(2, ("selected ciphersuite: %04x - %s",
1498 cipher_suite,
1499 ciphersuite_info->name));
Ronald Cron25e9ec62023-02-16 15:35:16 +01001500 break;
XiaokangQian17f974c2022-04-19 09:57:41 +00001501 }
Jerry Yu5725f1c2022-08-21 17:27:16 +08001502
Gilles Peskine449bd832023-01-11 14:50:10 +01001503 if (handshake->ciphersuite_info == NULL) {
1504 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE,
1505 MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE);
1506 return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
Jerry Yu5725f1c2022-08-21 17:27:16 +08001507 }
Jerry Yuc1be19f2022-04-23 16:11:39 +08001508
XiaokangQian4080a7f2022-04-11 09:55:18 +00001509 /* ...
XiaokangQianb67384d2022-04-19 00:02:38 +00001510 * opaque legacy_compression_methods<1..2^8-1>;
XiaokangQian4080a7f2022-04-11 09:55:18 +00001511 * ...
XiaokangQian7807f9f2022-02-15 10:04:37 +00001512 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001513 if (p[0] != 1 || p[1] != MBEDTLS_SSL_COMPRESS_NULL) {
1514 MBEDTLS_SSL_DEBUG_MSG(1, ("bad legacy compression method"));
1515 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER,
1516 MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER);
1517 return MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001518 }
XiaokangQianb67384d2022-04-19 00:02:38 +00001519 p += 2;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001520
Jerry Yuc1be19f2022-04-23 16:11:39 +08001521 /* ...
XiaokangQianb67384d2022-04-19 00:02:38 +00001522 * Extension extensions<8..2^16-1>;
Jerry Yuc1be19f2022-04-23 16:11:39 +08001523 * ...
XiaokangQianb67384d2022-04-19 00:02:38 +00001524 * with Extension defined as:
1525 * struct {
1526 * ExtensionType extension_type;
1527 * opaque extension_data<0..2^16-1>;
1528 * } Extension;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001529 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001530 extensions_len = MBEDTLS_GET_UINT16_BE(p, 0);
XiaokangQian7807f9f2022-02-15 10:04:37 +00001531 p += 2;
Gilles Peskine449bd832023-01-11 14:50:10 +01001532 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, extensions_len);
XiaokangQiane8ff3502022-04-22 02:34:40 +00001533 extensions_end = p + extensions_len;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001534
Gilles Peskine449bd832023-01-11 14:50:10 +01001535 MBEDTLS_SSL_DEBUG_BUF(3, "client hello extensions", p, extensions_len);
Jerry Yu63a459c2022-10-31 13:38:40 +08001536 handshake->received_extensions = MBEDTLS_SSL_EXT_MASK_NONE;
Jerry Yu0c354a22022-08-29 15:25:36 +08001537
Gilles Peskine449bd832023-01-11 14:50:10 +01001538 while (p < extensions_end) {
XiaokangQian7807f9f2022-02-15 10:04:37 +00001539 unsigned int extension_type;
1540 size_t extension_data_len;
1541 const unsigned char *extension_data_end;
Jerry Yu263dbf72022-10-26 10:51:27 +08001542 uint32_t allowed_exts = MBEDTLS_SSL_TLS1_3_ALLOWED_EXTS_OF_CH;
1543
Ronald Cron5fbd2702024-02-14 10:03:36 +01001544 if (ssl->handshake->hello_retry_request_flag) {
Jerry Yu263dbf72022-10-26 10:51:27 +08001545 /* Do not accept early data extension in 2nd ClientHello */
1546 allowed_exts &= ~MBEDTLS_SSL_EXT_MASK(EARLY_DATA);
1547 }
XiaokangQian7807f9f2022-02-15 10:04:37 +00001548
Jerry Yu0c354a22022-08-29 15:25:36 +08001549 /* RFC 8446, section 4.2.11
Jerry Yu1c9247c2022-07-21 12:37:39 +08001550 *
1551 * The "pre_shared_key" extension MUST be the last extension in the
1552 * ClientHello (this facilitates implementation as described below).
1553 * Servers MUST check that it is the last extension and otherwise fail
1554 * the handshake with an "illegal_parameter" alert.
1555 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001556 if (handshake->received_extensions & MBEDTLS_SSL_EXT_MASK(PRE_SHARED_KEY)) {
Jerry Yu1c9247c2022-07-21 12:37:39 +08001557 MBEDTLS_SSL_DEBUG_MSG(
Gilles Peskine449bd832023-01-11 14:50:10 +01001558 3, ("pre_shared_key is not last extension."));
Jerry Yu1c9247c2022-07-21 12:37:39 +08001559 MBEDTLS_SSL_PEND_FATAL_ALERT(
1560 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER,
Gilles Peskine449bd832023-01-11 14:50:10 +01001561 MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER);
1562 return MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER;
Jerry Yu1c9247c2022-07-21 12:37:39 +08001563 }
1564
Gilles Peskine449bd832023-01-11 14:50:10 +01001565 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, extensions_end, 4);
1566 extension_type = MBEDTLS_GET_UINT16_BE(p, 0);
1567 extension_data_len = MBEDTLS_GET_UINT16_BE(p, 2);
XiaokangQian7807f9f2022-02-15 10:04:37 +00001568 p += 4;
1569
Gilles Peskine449bd832023-01-11 14:50:10 +01001570 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, extensions_end, extension_data_len);
XiaokangQian7807f9f2022-02-15 10:04:37 +00001571 extension_data_end = p + extension_data_len;
1572
Jerry Yuc4bf5d62022-10-29 09:08:47 +08001573 ret = mbedtls_ssl_tls13_check_received_extension(
Gilles Peskine449bd832023-01-11 14:50:10 +01001574 ssl, MBEDTLS_SSL_HS_CLIENT_HELLO, extension_type,
Jerry Yu263dbf72022-10-26 10:51:27 +08001575 allowed_exts);
Gilles Peskine449bd832023-01-11 14:50:10 +01001576 if (ret != 0) {
1577 return ret;
1578 }
Jerry Yue18dc7e2022-08-04 16:29:22 +08001579
Gilles Peskine449bd832023-01-11 14:50:10 +01001580 switch (extension_type) {
XiaokangQian40a35232022-05-07 09:02:40 +00001581#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
1582 case MBEDTLS_TLS_EXT_SERVERNAME:
Gilles Peskine449bd832023-01-11 14:50:10 +01001583 MBEDTLS_SSL_DEBUG_MSG(3, ("found ServerName extension"));
1584 ret = mbedtls_ssl_parse_server_name_ext(ssl, p,
1585 extension_data_end);
1586 if (ret != 0) {
XiaokangQian40a35232022-05-07 09:02:40 +00001587 MBEDTLS_SSL_DEBUG_RET(
Gilles Peskine449bd832023-01-11 14:50:10 +01001588 1, "mbedtls_ssl_parse_servername_ext", ret);
1589 return ret;
XiaokangQian40a35232022-05-07 09:02:40 +00001590 }
XiaokangQian40a35232022-05-07 09:02:40 +00001591 break;
1592#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
1593
Przemek Stekiel8c0a9532023-06-15 16:48:19 +02001594#if defined(PSA_WANT_ALG_ECDH) || defined(PSA_WANT_ALG_FFDH)
XiaokangQian7807f9f2022-02-15 10:04:37 +00001595 case MBEDTLS_TLS_EXT_SUPPORTED_GROUPS:
Gilles Peskine449bd832023-01-11 14:50:10 +01001596 MBEDTLS_SSL_DEBUG_MSG(3, ("found supported group extension"));
XiaokangQian7807f9f2022-02-15 10:04:37 +00001597
1598 /* Supported Groups Extension
1599 *
1600 * When sent by the client, the "supported_groups" extension
1601 * indicates the named groups which the client supports,
1602 * ordered from most preferred to least preferred.
1603 */
Jerry Yuc1be19f2022-04-23 16:11:39 +08001604 ret = ssl_tls13_parse_supported_groups_ext(
Gilles Peskine449bd832023-01-11 14:50:10 +01001605 ssl, p, extension_data_end);
1606 if (ret != 0) {
Xiaokang Qian9f1747b2023-03-30 02:50:04 +00001607 MBEDTLS_SSL_DEBUG_RET(
Xiaokang Qian8bce0e62023-04-04 10:15:48 +00001608 1, "ssl_tls13_parse_supported_groups_ext", ret);
Gilles Peskine449bd832023-01-11 14:50:10 +01001609 return ret;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001610 }
1611
XiaokangQian7807f9f2022-02-15 10:04:37 +00001612 break;
Przemek Stekiel8c0a9532023-06-15 16:48:19 +02001613#endif /* PSA_WANT_ALG_ECDH || PSA_WANT_ALG_FFDH*/
XiaokangQian7807f9f2022-02-15 10:04:37 +00001614
Valerio Settic9ae8622023-07-25 11:23:50 +02001615#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_EPHEMERAL_ENABLED)
XiaokangQian7807f9f2022-02-15 10:04:37 +00001616 case MBEDTLS_TLS_EXT_KEY_SHARE:
Gilles Peskine449bd832023-01-11 14:50:10 +01001617 MBEDTLS_SSL_DEBUG_MSG(3, ("found key share extension"));
XiaokangQian7807f9f2022-02-15 10:04:37 +00001618
1619 /*
1620 * Key Share Extension
1621 *
1622 * When sent by the client, the "key_share" extension
1623 * contains the endpoint's cryptographic parameters for
1624 * ECDHE/DHE key establishment methods.
1625 */
Jerry Yuc1be19f2022-04-23 16:11:39 +08001626 ret = ssl_tls13_parse_key_shares_ext(
Gilles Peskine449bd832023-01-11 14:50:10 +01001627 ssl, p, extension_data_end);
1628 if (ret == SSL_TLS1_3_PARSE_KEY_SHARES_EXT_NO_MATCH) {
Ronald Cron8a74f072023-06-14 17:59:29 +02001629 MBEDTLS_SSL_DEBUG_MSG(2, ("No usable share for key agreement."));
1630 no_usable_share_for_key_agreement = 1;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001631 }
1632
Gilles Peskine449bd832023-01-11 14:50:10 +01001633 if (ret < 0) {
Jerry Yu582dd062022-04-22 21:59:01 +08001634 MBEDTLS_SSL_DEBUG_RET(
Gilles Peskine449bd832023-01-11 14:50:10 +01001635 1, "ssl_tls13_parse_key_shares_ext", ret);
1636 return ret;
Jerry Yu582dd062022-04-22 21:59:01 +08001637 }
XiaokangQian7807f9f2022-02-15 10:04:37 +00001638
XiaokangQian7807f9f2022-02-15 10:04:37 +00001639 break;
Valerio Settic9ae8622023-07-25 11:23:50 +02001640#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_EPHEMERAL_ENABLED */
XiaokangQian7807f9f2022-02-15 10:04:37 +00001641
1642 case MBEDTLS_TLS_EXT_SUPPORTED_VERSIONS:
Ronald Cron8c527d02023-03-07 15:47:47 +01001643 /* Already parsed */
XiaokangQian7807f9f2022-02-15 10:04:37 +00001644 break;
1645
Ronald Cron41a443a2022-10-04 16:38:25 +02001646#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED)
Jerry Yue19e3b92022-07-08 12:04:51 +00001647 case MBEDTLS_TLS_EXT_PSK_KEY_EXCHANGE_MODES:
Xiaokang Qian9f1747b2023-03-30 02:50:04 +00001648 MBEDTLS_SSL_DEBUG_MSG(
1649 3, ("found psk key exchange modes extension"));
Jerry Yue19e3b92022-07-08 12:04:51 +00001650
1651 ret = ssl_tls13_parse_key_exchange_modes_ext(
Gilles Peskine449bd832023-01-11 14:50:10 +01001652 ssl, p, extension_data_end);
1653 if (ret != 0) {
Jerry Yue19e3b92022-07-08 12:04:51 +00001654 MBEDTLS_SSL_DEBUG_RET(
Gilles Peskine449bd832023-01-11 14:50:10 +01001655 1, "ssl_tls13_parse_key_exchange_modes_ext", ret);
1656 return ret;
Jerry Yue19e3b92022-07-08 12:04:51 +00001657 }
1658
Jerry Yue19e3b92022-07-08 12:04:51 +00001659 break;
Ronald Cron41a443a2022-10-04 16:38:25 +02001660#endif
Jerry Yue19e3b92022-07-08 12:04:51 +00001661
Jerry Yu1c105562022-07-10 06:32:38 +00001662 case MBEDTLS_TLS_EXT_PRE_SHARED_KEY:
Gilles Peskine449bd832023-01-11 14:50:10 +01001663 MBEDTLS_SSL_DEBUG_MSG(3, ("found pre_shared_key extension"));
1664 if ((handshake->received_extensions &
1665 MBEDTLS_SSL_EXT_MASK(PSK_KEY_EXCHANGE_MODES)) == 0) {
Jerry Yu13ab81d2022-07-22 23:17:11 +08001666 MBEDTLS_SSL_PEND_FATAL_ALERT(
1667 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER,
Gilles Peskine449bd832023-01-11 14:50:10 +01001668 MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER);
1669 return MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER;
Jerry Yu13ab81d2022-07-22 23:17:11 +08001670 }
Ronald Cron41a443a2022-10-04 16:38:25 +02001671#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED)
Jerry Yu1c105562022-07-10 06:32:38 +00001672 /* Delay processing of the PSK identity once we have
1673 * found out which algorithms to use. We keep a pointer
1674 * to the buffer and the size for later processing.
1675 */
Jerry Yu29d9faa2022-08-23 17:52:45 +08001676 pre_shared_key_ext = p;
Jerry Yu1c105562022-07-10 06:32:38 +00001677 pre_shared_key_ext_end = extension_data_end;
Jerry Yue18dc7e2022-08-04 16:29:22 +08001678#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED */
Jerry Yu1c105562022-07-10 06:32:38 +00001679 break;
Jerry Yu1c105562022-07-10 06:32:38 +00001680
XiaokangQianacb39922022-06-17 10:18:48 +00001681#if defined(MBEDTLS_SSL_ALPN)
1682 case MBEDTLS_TLS_EXT_ALPN:
Gilles Peskine449bd832023-01-11 14:50:10 +01001683 MBEDTLS_SSL_DEBUG_MSG(3, ("found alpn extension"));
XiaokangQianacb39922022-06-17 10:18:48 +00001684
Gilles Peskine449bd832023-01-11 14:50:10 +01001685 ret = mbedtls_ssl_parse_alpn_ext(ssl, p, extension_data_end);
1686 if (ret != 0) {
XiaokangQianacb39922022-06-17 10:18:48 +00001687 MBEDTLS_SSL_DEBUG_RET(
Gilles Peskine449bd832023-01-11 14:50:10 +01001688 1, ("mbedtls_ssl_parse_alpn_ext"), ret);
1689 return ret;
XiaokangQianacb39922022-06-17 10:18:48 +00001690 }
XiaokangQianacb39922022-06-17 10:18:48 +00001691 break;
1692#endif /* MBEDTLS_SSL_ALPN */
1693
Ronald Cron928cbd32022-10-04 16:14:26 +02001694#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED)
XiaokangQian7807f9f2022-02-15 10:04:37 +00001695 case MBEDTLS_TLS_EXT_SIG_ALG:
Gilles Peskine449bd832023-01-11 14:50:10 +01001696 MBEDTLS_SSL_DEBUG_MSG(3, ("found signature_algorithms extension"));
XiaokangQian7807f9f2022-02-15 10:04:37 +00001697
Gabor Mezei078e8032022-04-27 21:17:56 +02001698 ret = mbedtls_ssl_parse_sig_alg_ext(
Gilles Peskine449bd832023-01-11 14:50:10 +01001699 ssl, p, extension_data_end);
1700 if (ret != 0) {
Xiaokang Qian49f39c12023-04-06 02:31:02 +00001701 MBEDTLS_SSL_DEBUG_RET(
1702 1, "mbedtls_ssl_parse_sig_alg_ext", ret);
Gilles Peskine449bd832023-01-11 14:50:10 +01001703 return ret;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001704 }
XiaokangQian7807f9f2022-02-15 10:04:37 +00001705 break;
Ronald Cron928cbd32022-10-04 16:14:26 +02001706#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED */
XiaokangQian7807f9f2022-02-15 10:04:37 +00001707
Jan Bruckner151f6422023-02-10 12:45:19 +01001708#if defined(MBEDTLS_SSL_RECORD_SIZE_LIMIT)
1709 case MBEDTLS_TLS_EXT_RECORD_SIZE_LIMIT:
1710 MBEDTLS_SSL_DEBUG_MSG(3, ("found record_size_limit extension"));
1711
Xiaokang Qian9f1747b2023-03-30 02:50:04 +00001712 ret = mbedtls_ssl_tls13_parse_record_size_limit_ext(
1713 ssl, p, extension_data_end);
Jan Brucknerf482dcc2023-03-15 09:09:06 +01001714 if (ret != 0) {
1715 MBEDTLS_SSL_DEBUG_RET(
1716 1, ("mbedtls_ssl_tls13_parse_record_size_limit_ext"), ret);
1717 return ret;
1718 }
Jan Bruckner151f6422023-02-10 12:45:19 +01001719 break;
1720#endif /* MBEDTLS_SSL_RECORD_SIZE_LIMIT */
1721
XiaokangQian7807f9f2022-02-15 10:04:37 +00001722 default:
Jerry Yu79aa7212022-11-08 21:30:21 +08001723 MBEDTLS_SSL_PRINT_EXT(
Jerry Yu63a459c2022-10-31 13:38:40 +08001724 3, MBEDTLS_SSL_HS_CLIENT_HELLO,
Gilles Peskine449bd832023-01-11 14:50:10 +01001725 extension_type, "( ignored )");
Jerry Yu0c354a22022-08-29 15:25:36 +08001726 break;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001727 }
1728
1729 p += extension_data_len;
1730 }
1731
Gilles Peskine449bd832023-01-11 14:50:10 +01001732 MBEDTLS_SSL_PRINT_EXTS(3, MBEDTLS_SSL_HS_CLIENT_HELLO,
1733 handshake->received_extensions);
Jerry Yue18dc7e2022-08-04 16:29:22 +08001734
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +01001735 ret = mbedtls_ssl_add_hs_hdr_to_checksum(ssl,
1736 MBEDTLS_SSL_HS_CLIENT_HELLO,
1737 p - buf);
1738 if (0 != ret) {
1739 MBEDTLS_SSL_DEBUG_RET(1, ("mbedtls_ssl_add_hs_hdr_to_checksum"), ret);
1740 return ret;
1741 }
Jerry Yu032b15ce2022-07-11 06:10:03 +00001742
Ronald Cron41a443a2022-10-04 16:38:25 +02001743#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED)
XiaokangQian7807f9f2022-02-15 10:04:37 +00001744 /* Update checksum with either
1745 * - The entire content of the CH message, if no PSK extension is present
1746 * - The content up to but excluding the PSK extension, if present.
Ronald Cron12e72f12024-02-14 15:49:38 +01001747 * Always parse the pre-shared key extension when present in the
1748 * ClientHello even if some pre-requisites for PSK key exchange modes are
1749 * not met. That way we always validate the syntax of the extension.
XiaokangQian7807f9f2022-02-15 10:04:37 +00001750 */
Ronald Cron12e72f12024-02-14 15:49:38 +01001751 if (handshake->received_extensions & MBEDTLS_SSL_EXT_MASK(PRE_SHARED_KEY)) {
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +01001752 ret = handshake->update_checksum(ssl, buf,
1753 pre_shared_key_ext - buf);
1754 if (0 != ret) {
1755 MBEDTLS_SSL_DEBUG_RET(1, ("update_checksum"), ret);
1756 return ret;
1757 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001758 ret = ssl_tls13_parse_pre_shared_key_ext(ssl,
1759 pre_shared_key_ext,
1760 pre_shared_key_ext_end,
1761 cipher_suites,
1762 cipher_suites_end);
1763 if (ret == MBEDTLS_ERR_SSL_UNKNOWN_IDENTITY) {
1764 handshake->received_extensions &= ~MBEDTLS_SSL_EXT_MASK(PRE_SHARED_KEY);
1765 } else if (ret != 0) {
Jerry Yu63a459c2022-10-31 13:38:40 +08001766 MBEDTLS_SSL_DEBUG_RET(
Gilles Peskine449bd832023-01-11 14:50:10 +01001767 1, "ssl_tls13_parse_pre_shared_key_ext", ret);
1768 return ret;
Jerry Yu1c105562022-07-10 06:32:38 +00001769 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001770 } else
Ronald Cron41a443a2022-10-04 16:38:25 +02001771#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED */
Jerry Yu1c105562022-07-10 06:32:38 +00001772 {
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +01001773 ret = handshake->update_checksum(ssl, buf, p - buf);
1774 if (0 != ret) {
1775 MBEDTLS_SSL_DEBUG_RET(1, ("update_checksum"), ret);
1776 return ret;
1777 }
Jerry Yu1c105562022-07-10 06:32:38 +00001778 }
XiaokangQian7807f9f2022-02-15 10:04:37 +00001779
Gilles Peskine449bd832023-01-11 14:50:10 +01001780 ret = ssl_tls13_determine_key_exchange_mode(ssl);
1781 if (ret < 0) {
1782 return ret;
1783 }
XiaokangQian7807f9f2022-02-15 10:04:37 +00001784
Ronald Cron8a74f072023-06-14 17:59:29 +02001785 if (ssl->handshake->key_exchange_mode !=
1786 MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK) {
1787 hrr_required = (no_usable_share_for_key_agreement != 0);
1788 }
1789
Gilles Peskine449bd832023-01-11 14:50:10 +01001790 mbedtls_ssl_optimize_checksum(ssl, handshake->ciphersuite_info);
Jerry Yue5834fd2022-08-29 20:16:09 +08001791
Gilles Peskine449bd832023-01-11 14:50:10 +01001792 return hrr_required ? SSL_CLIENT_HELLO_HRR_REQUIRED : SSL_CLIENT_HELLO_OK;
XiaokangQian75fe8c72022-06-15 09:42:45 +00001793}
1794
Jerry Yuab0da372023-02-08 13:55:24 +08001795#if defined(MBEDTLS_SSL_EARLY_DATA)
Jerry Yu4caf3ca2023-11-15 16:13:47 +08001796static int ssl_tls13_check_early_data_requirements(mbedtls_ssl_context *ssl)
Jerry Yuab0da372023-02-08 13:55:24 +08001797{
1798 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
1799
Jerry Yu985c9672022-12-04 14:06:30 +08001800 if (ssl->conf->early_data_enabled == MBEDTLS_SSL_EARLY_DATA_DISABLED) {
1801 MBEDTLS_SSL_DEBUG_MSG(
Jerry Yu985c9672022-12-04 14:06:30 +08001802 1,
Jerry Yu454dda32023-10-31 15:13:54 +08001803 ("EarlyData: rejected, feature disabled in server configuration."));
Jerry Yu4caf3ca2023-11-15 16:13:47 +08001804 return -1;
Ronald Cron7b6ee942024-01-12 10:29:55 +01001805 }
1806
Jerry Yu454dda32023-10-31 15:13:54 +08001807 if (!handshake->resume) {
1808 /* We currently support early data only in the case of PSKs established
1809 via a NewSessionTicket message thus in the case of a session
1810 resumption. */
Jerry Yu985c9672022-12-04 14:06:30 +08001811 MBEDTLS_SSL_DEBUG_MSG(
Jerry Yu7ef9fd82023-11-07 14:30:38 +08001812 1, ("EarlyData: rejected, not a session resumption."));
Jerry Yu4caf3ca2023-11-15 16:13:47 +08001813 return -1;
Jerry Yu985c9672022-12-04 14:06:30 +08001814 }
1815
Jerry Yu82fd6c12023-10-31 16:32:19 +08001816 /* RFC 8446 4.2.10
1817 *
1818 * In order to accept early data, the server MUST have accepted a PSK cipher
1819 * suite and selected the first key offered in the client's "pre_shared_key"
1820 * extension. In addition, it MUST verify that the following values are the
1821 * same as those associated with the selected PSK:
1822 * - The TLS version number
1823 * - The selected cipher suite
1824 * - The selected ALPN [RFC7301] protocol, if any
1825 *
1826 * NOTE:
Jerry Yu7ef9fd82023-11-07 14:30:38 +08001827 * - The TLS version number is checked in
1828 * ssl_tls13_offered_psks_check_identity_match_ticket().
1829 * - ALPN is not checked for the time being (TODO).
Jerry Yu82fd6c12023-10-31 16:32:19 +08001830 */
1831
1832 if (handshake->selected_identity != 0) {
1833 MBEDTLS_SSL_DEBUG_MSG(
Jerry Yu7ef9fd82023-11-07 14:30:38 +08001834 1, ("EarlyData: rejected, the selected key in "
1835 "`pre_shared_key` is not the first one."));
Jerry Yu4caf3ca2023-11-15 16:13:47 +08001836 return -1;
Jerry Yu82fd6c12023-10-31 16:32:19 +08001837 }
1838
1839 if (handshake->ciphersuite_info->id !=
1840 ssl->session_negotiate->ciphersuite) {
1841 MBEDTLS_SSL_DEBUG_MSG(
Jerry Yu7ef9fd82023-11-07 14:30:38 +08001842 1, ("EarlyData: rejected, the selected ciphersuite is not the one "
1843 "of the selected pre-shared key."));
Jerry Yu4caf3ca2023-11-15 16:13:47 +08001844 return -1;
Jerry Yu82fd6c12023-10-31 16:32:19 +08001845
1846 }
Jerry Yu985c9672022-12-04 14:06:30 +08001847
Pengyu Lv94a42cc2023-12-06 10:04:17 +08001848 if (!mbedtls_ssl_tls13_session_ticket_allow_early_data(ssl->session_negotiate)) {
Jerry Yufceddb32022-12-12 15:30:34 +08001849 MBEDTLS_SSL_DEBUG_MSG(
1850 1,
Jerry Yuea96ac32023-11-21 17:06:36 +08001851 ("EarlyData: rejected, early_data not allowed in ticket "
1852 "permission bits."));
Jerry Yu4caf3ca2023-11-15 16:13:47 +08001853 return -1;
Jerry Yufceddb32022-12-12 15:30:34 +08001854 }
Jerry Yu985c9672022-12-04 14:06:30 +08001855
Jerry Yu4caf3ca2023-11-15 16:13:47 +08001856 return 0;
Jerry Yuab0da372023-02-08 13:55:24 +08001857}
1858#endif /* MBEDTLS_SSL_EARLY_DATA */
1859
XiaokangQian75fe8c72022-06-15 09:42:45 +00001860/* Update the handshake state machine */
1861
Ronald Cronce7d76e2022-07-08 18:56:49 +02001862MBEDTLS_CHECK_RETURN_CRITICAL
Ronald Cron7b6ee942024-01-12 10:29:55 +01001863static int ssl_tls13_postprocess_client_hello(mbedtls_ssl_context *ssl,
1864 int hrr_required)
XiaokangQian75fe8c72022-06-15 09:42:45 +00001865{
1866 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1867
XiaokangQian7807f9f2022-02-15 10:04:37 +00001868 /*
XiaokangQianfb665a82022-06-15 03:57:21 +00001869 * Server certificate selection
1870 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001871 if (ssl->conf->f_cert_cb && (ret = ssl->conf->f_cert_cb(ssl)) != 0) {
1872 MBEDTLS_SSL_DEBUG_RET(1, "f_cert_cb", ret);
1873 return ret;
XiaokangQianfb665a82022-06-15 03:57:21 +00001874 }
1875#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
1876 ssl->handshake->sni_name = NULL;
1877 ssl->handshake->sni_name_len = 0;
1878#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
XiaokangQian7807f9f2022-02-15 10:04:37 +00001879
Gilles Peskine449bd832023-01-11 14:50:10 +01001880 ret = mbedtls_ssl_tls13_key_schedule_stage_early(ssl);
1881 if (ret != 0) {
1882 MBEDTLS_SSL_DEBUG_RET(1,
1883 "mbedtls_ssl_tls1_3_key_schedule_stage_early", ret);
1884 return ret;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001885 }
1886
Jerry Yuab0da372023-02-08 13:55:24 +08001887#if defined(MBEDTLS_SSL_EARLY_DATA)
Jerry Yu4caf3ca2023-11-15 16:13:47 +08001888 if (ssl->handshake->received_extensions & MBEDTLS_SSL_EXT_MASK(EARLY_DATA)) {
Ronald Cron31e2d832024-02-05 16:45:57 +01001889 ssl->handshake->early_data_accepted =
1890 (!hrr_required) && (ssl_tls13_check_early_data_requirements(ssl) == 0);
Jerry Yu4e9b70e2022-12-04 14:08:02 +08001891
Jerry Yu4caf3ca2023-11-15 16:13:47 +08001892 if (ssl->handshake->early_data_accepted) {
1893 ret = mbedtls_ssl_tls13_compute_early_transform(ssl);
1894 if (ret != 0) {
1895 MBEDTLS_SSL_DEBUG_RET(
1896 1, "mbedtls_ssl_tls13_compute_early_transform", ret);
1897 return ret;
1898 }
1899 } else {
1900 ssl->discard_early_data_record =
1901 hrr_required ?
1902 MBEDTLS_SSL_EARLY_DATA_DISCARD :
1903 MBEDTLS_SSL_EARLY_DATA_TRY_TO_DEPROTECT_AND_DISCARD;
Jerry Yu4e9b70e2022-12-04 14:08:02 +08001904 }
1905 }
Ronald Cron7b6ee942024-01-12 10:29:55 +01001906#else
1907 ((void) hrr_required);
Jerry Yuab0da372023-02-08 13:55:24 +08001908#endif /* MBEDTLS_SSL_EARLY_DATA */
1909
Gilles Peskine449bd832023-01-11 14:50:10 +01001910 return 0;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001911}
1912
1913/*
XiaokangQianed582dd2022-04-13 08:21:05 +00001914 * Main entry point from the state machine; orchestrates the otherfunctions.
1915 */
1916
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001917MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01001918static int ssl_tls13_process_client_hello(mbedtls_ssl_context *ssl)
XiaokangQianed582dd2022-04-13 08:21:05 +00001919{
1920
XiaokangQian08037552022-04-20 07:16:41 +00001921 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine449bd832023-01-11 14:50:10 +01001922 unsigned char *buf = NULL;
XiaokangQianed582dd2022-04-13 08:21:05 +00001923 size_t buflen = 0;
Jerry Yu4ca91402022-05-09 15:50:57 +08001924 int parse_client_hello_ret;
1925
Gilles Peskine449bd832023-01-11 14:50:10 +01001926 MBEDTLS_SSL_DEBUG_MSG(2, ("=> parse client hello"));
XiaokangQianed582dd2022-04-13 08:21:05 +00001927
Gilles Peskine449bd832023-01-11 14:50:10 +01001928 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_tls13_fetch_handshake_msg(
1929 ssl, MBEDTLS_SSL_HS_CLIENT_HELLO,
1930 &buf, &buflen));
XiaokangQianed582dd2022-04-13 08:21:05 +00001931
Gilles Peskine449bd832023-01-11 14:50:10 +01001932 MBEDTLS_SSL_PROC_CHK_NEG(ssl_tls13_parse_client_hello(ssl, buf,
1933 buf + buflen));
Ronald Cron5af4c7f2023-03-07 20:46:59 +01001934 parse_client_hello_ret = ret; /* Store positive return value of
1935 * parse_client_hello,
1936 * as negative error codes are handled
Jerry Yuf41553b2022-05-09 22:20:30 +08001937 * by MBEDTLS_SSL_PROC_CHK_NEG. */
Jerry Yu4ca91402022-05-09 15:50:57 +08001938
Ronald Cronb828c7d2023-04-03 16:37:22 +02001939 /*
Yanray Wang90acdc62023-12-08 10:29:42 +08001940 * Version 1.2 of the protocol has to be used for the handshake.
1941 * If TLS 1.2 is not supported, abort the handshake. Otherwise, set the
Ronald Cronb828c7d2023-04-03 16:37:22 +02001942 * ssl->keep_current_message flag for the ClientHello to be kept and parsed
1943 * as a TLS 1.2 ClientHello. We also change ssl->tls_version to
1944 * MBEDTLS_SSL_VERSION_TLS1_2 thus from now on mbedtls_ssl_handshake_step()
1945 * will dispatch to the TLS 1.2 state machine.
1946 */
Ronald Cron5af4c7f2023-03-07 20:46:59 +01001947 if (SSL_CLIENT_HELLO_TLS1_2 == parse_client_hello_ret) {
Yanray Wangfb0f47b2023-12-04 15:27:28 +08001948 /* Check if server supports TLS 1.2 */
Yanray Wang408ba6f2023-12-08 10:18:03 +08001949 if (!mbedtls_ssl_conf_is_tls12_enabled(ssl->conf)) {
Yanray Wangfb0f47b2023-12-04 15:27:28 +08001950 MBEDTLS_SSL_DEBUG_MSG(
Yanray Wang177e49a2023-12-08 10:51:04 +08001951 1, ("TLS 1.2 not supported."));
Yanray Wangfb0f47b2023-12-04 15:27:28 +08001952 MBEDTLS_SSL_PEND_FATAL_ALERT(
Yanray Wang2bef9172023-12-08 10:21:53 +08001953 MBEDTLS_SSL_ALERT_MSG_PROTOCOL_VERSION,
1954 MBEDTLS_ERR_SSL_BAD_PROTOCOL_VERSION);
1955 return MBEDTLS_ERR_SSL_BAD_PROTOCOL_VERSION;
Yanray Wangfb0f47b2023-12-04 15:27:28 +08001956 }
Ronald Cron5af4c7f2023-03-07 20:46:59 +01001957 ssl->keep_current_message = 1;
1958 ssl->tls_version = MBEDTLS_SSL_VERSION_TLS1_2;
1959 return 0;
1960 }
1961
Ronald Cron7b6ee942024-01-12 10:29:55 +01001962 MBEDTLS_SSL_PROC_CHK(
1963 ssl_tls13_postprocess_client_hello(ssl, parse_client_hello_ret ==
1964 SSL_CLIENT_HELLO_HRR_REQUIRED));
Jerry Yu582dd062022-04-22 21:59:01 +08001965
Ronald Cron5af4c7f2023-03-07 20:46:59 +01001966 if (SSL_CLIENT_HELLO_OK == parse_client_hello_ret) {
Gilles Peskine449bd832023-01-11 14:50:10 +01001967 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_SERVER_HELLO);
1968 } else {
1969 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_HELLO_RETRY_REQUEST);
1970 }
XiaokangQianed582dd2022-04-13 08:21:05 +00001971
1972cleanup:
1973
Gilles Peskine449bd832023-01-11 14:50:10 +01001974 MBEDTLS_SSL_DEBUG_MSG(2, ("<= parse client hello"));
1975 return ret;
XiaokangQianed582dd2022-04-13 08:21:05 +00001976}
1977
1978/*
Jerry Yu1c3e6882022-04-20 21:23:40 +08001979 * Handler for MBEDTLS_SSL_SERVER_HELLO
Jerry Yu5b64ae92022-03-30 17:15:02 +08001980 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001981MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01001982static int ssl_tls13_prepare_server_hello(mbedtls_ssl_context *ssl)
Jerry Yuf4b27e42022-03-30 17:32:21 +08001983{
Jerry Yu637a3f12022-04-20 21:37:58 +08001984 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1985 unsigned char *server_randbytes =
Gilles Peskine449bd832023-01-11 14:50:10 +01001986 ssl->handshake->randbytes + MBEDTLS_CLIENT_HELLO_RANDOM_LEN;
1987 if (ssl->conf->f_rng == NULL) {
1988 MBEDTLS_SSL_DEBUG_MSG(1, ("no RNG provided"));
1989 return MBEDTLS_ERR_SSL_NO_RNG;
Jerry Yuf4b27e42022-03-30 17:32:21 +08001990 }
1991
Gilles Peskine449bd832023-01-11 14:50:10 +01001992 if ((ret = ssl->conf->f_rng(ssl->conf->p_rng, server_randbytes,
1993 MBEDTLS_SERVER_HELLO_RANDOM_LEN)) != 0) {
1994 MBEDTLS_SSL_DEBUG_RET(1, "f_rng", ret);
1995 return ret;
Jerry Yuf4b27e42022-03-30 17:32:21 +08001996 }
1997
Gilles Peskine449bd832023-01-11 14:50:10 +01001998 MBEDTLS_SSL_DEBUG_BUF(3, "server hello, random bytes", server_randbytes,
1999 MBEDTLS_SERVER_HELLO_RANDOM_LEN);
Jerry Yuf4b27e42022-03-30 17:32:21 +08002000
2001#if defined(MBEDTLS_HAVE_TIME)
YxCda609132023-05-22 12:08:12 -07002002 ssl->session_negotiate->start = mbedtls_time(NULL);
Jerry Yuf4b27e42022-03-30 17:32:21 +08002003#endif /* MBEDTLS_HAVE_TIME */
2004
Gilles Peskine449bd832023-01-11 14:50:10 +01002005 return ret;
Jerry Yuf4b27e42022-03-30 17:32:21 +08002006}
2007
Jerry Yu3bf2c642022-03-30 22:02:12 +08002008/*
Jerry Yue74e04a2022-04-21 09:23:16 +08002009 * ssl_tls13_write_server_hello_supported_versions_ext ():
Jerry Yu3bf2c642022-03-30 22:02:12 +08002010 *
2011 * struct {
Jerry Yufb9f54d2022-04-06 10:08:34 +08002012 * ProtocolVersion selected_version;
Jerry Yu3bf2c642022-03-30 22:02:12 +08002013 * } SupportedVersions;
2014 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002015MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yue74e04a2022-04-21 09:23:16 +08002016static int ssl_tls13_write_server_hello_supported_versions_ext(
Gilles Peskine449bd832023-01-11 14:50:10 +01002017 mbedtls_ssl_context *ssl,
2018 unsigned char *buf,
2019 unsigned char *end,
2020 size_t *out_len)
Jerry Yu3bf2c642022-03-30 22:02:12 +08002021{
Jerry Yu3bf2c642022-03-30 22:02:12 +08002022 *out_len = 0;
2023
Gilles Peskine449bd832023-01-11 14:50:10 +01002024 MBEDTLS_SSL_DEBUG_MSG(3, ("server hello, write selected version"));
Jerry Yu3bf2c642022-03-30 22:02:12 +08002025
2026 /* Check if we have space to write the extension:
2027 * - extension_type (2 bytes)
2028 * - extension_data_length (2 bytes)
Jerry Yu349a6132022-04-14 20:52:56 +08002029 * - selected_version (2 bytes)
Jerry Yu3bf2c642022-03-30 22:02:12 +08002030 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002031 MBEDTLS_SSL_CHK_BUF_PTR(buf, end, 6);
Jerry Yu3bf2c642022-03-30 22:02:12 +08002032
Gilles Peskine449bd832023-01-11 14:50:10 +01002033 MBEDTLS_PUT_UINT16_BE(MBEDTLS_TLS_EXT_SUPPORTED_VERSIONS, buf, 0);
Jerry Yu3bf2c642022-03-30 22:02:12 +08002034
Gilles Peskine449bd832023-01-11 14:50:10 +01002035 MBEDTLS_PUT_UINT16_BE(2, buf, 2);
Jerry Yu3bf2c642022-03-30 22:02:12 +08002036
Gilles Peskine449bd832023-01-11 14:50:10 +01002037 mbedtls_ssl_write_version(buf + 4,
2038 ssl->conf->transport,
2039 ssl->tls_version);
Jerry Yu3bf2c642022-03-30 22:02:12 +08002040
Gilles Peskine449bd832023-01-11 14:50:10 +01002041 MBEDTLS_SSL_DEBUG_MSG(3, ("supported version: [%04x]",
2042 ssl->tls_version));
Jerry Yu3bf2c642022-03-30 22:02:12 +08002043
Jerry Yu349a6132022-04-14 20:52:56 +08002044 *out_len = 6;
Jerry Yu3bf2c642022-03-30 22:02:12 +08002045
Jerry Yub95dd362022-11-08 21:19:34 +08002046 mbedtls_ssl_tls13_set_hs_sent_ext_mask(
Gilles Peskine449bd832023-01-11 14:50:10 +01002047 ssl, MBEDTLS_TLS_EXT_SUPPORTED_VERSIONS);
Jerry Yub95dd362022-11-08 21:19:34 +08002048
Gilles Peskine449bd832023-01-11 14:50:10 +01002049 return 0;
Jerry Yu3bf2c642022-03-30 22:02:12 +08002050}
2051
Jerry Yud9436a12022-04-20 22:28:09 +08002052
Jerry Yu3bf2c642022-03-30 22:02:12 +08002053
2054/* Generate and export a single key share. For hybrid KEMs, this can
2055 * be called multiple times with the different components of the hybrid. */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002056MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002057static int ssl_tls13_generate_and_write_key_share(mbedtls_ssl_context *ssl,
2058 uint16_t named_group,
2059 unsigned char *buf,
2060 unsigned char *end,
2061 size_t *out_len)
Jerry Yu3bf2c642022-03-30 22:02:12 +08002062{
2063 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yu955ddd72022-04-22 22:27:33 +08002064
2065 *out_len = 0;
2066
Valerio Settic9ae8622023-07-25 11:23:50 +02002067#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_EPHEMERAL_ENABLED)
Przemek Stekiel29c219c2023-05-31 15:21:04 +02002068 if (mbedtls_ssl_tls13_named_group_is_ecdhe(named_group) ||
Przemek Stekield5f79e72023-06-29 09:08:43 +02002069 mbedtls_ssl_tls13_named_group_is_ffdh(named_group)) {
Przemek Stekiel408569f2023-07-06 11:26:44 +02002070 ret = mbedtls_ssl_tls13_generate_and_write_xxdh_key_exchange(
Gilles Peskine449bd832023-01-11 14:50:10 +01002071 ssl, named_group, buf, end, out_len);
2072 if (ret != 0) {
Jerry Yu89e103c2022-03-30 22:43:29 +08002073 MBEDTLS_SSL_DEBUG_RET(
Przemek Stekiel408569f2023-07-06 11:26:44 +02002074 1, "mbedtls_ssl_tls13_generate_and_write_xxdh_key_exchange",
Gilles Peskine449bd832023-01-11 14:50:10 +01002075 ret);
2076 return ret;
Jerry Yu3bf2c642022-03-30 22:02:12 +08002077 }
Gilles Peskine449bd832023-01-11 14:50:10 +01002078 } else
Valerio Settic9ae8622023-07-25 11:23:50 +02002079#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_EPHEMERAL_ENABLED */
Gilles Peskine449bd832023-01-11 14:50:10 +01002080 if (0 /* Other kinds of KEMs */) {
2081 } else {
Jerry Yu955ddd72022-04-22 22:27:33 +08002082 ((void) ssl);
2083 ((void) named_group);
2084 ((void) buf);
2085 ((void) end);
Jerry Yu3bf2c642022-03-30 22:02:12 +08002086 ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
2087 }
2088
Gilles Peskine449bd832023-01-11 14:50:10 +01002089 return ret;
Jerry Yu3bf2c642022-03-30 22:02:12 +08002090}
2091
2092/*
2093 * ssl_tls13_write_key_share_ext
2094 *
2095 * Structure of key_share extension in ServerHello:
2096 *
Jerry Yu1c3e6882022-04-20 21:23:40 +08002097 * struct {
2098 * NamedGroup group;
2099 * opaque key_exchange<1..2^16-1>;
2100 * } KeyShareEntry;
2101 * struct {
2102 * KeyShareEntry server_share;
2103 * } KeyShareServerHello;
Jerry Yu3bf2c642022-03-30 22:02:12 +08002104 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002105MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002106static int ssl_tls13_write_key_share_ext(mbedtls_ssl_context *ssl,
2107 unsigned char *buf,
2108 unsigned char *end,
2109 size_t *out_len)
Jerry Yu3bf2c642022-03-30 22:02:12 +08002110{
Jerry Yu955ddd72022-04-22 22:27:33 +08002111 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yu3bf2c642022-03-30 22:02:12 +08002112 unsigned char *p = buf;
Jerry Yu57d48412022-04-20 21:50:42 +08002113 uint16_t group = ssl->handshake->offered_group_id;
Jerry Yu3bf2c642022-03-30 22:02:12 +08002114 unsigned char *server_share = buf + 4;
Jerry Yu3bf2c642022-03-30 22:02:12 +08002115 size_t key_exchange_length;
Jerry Yu3bf2c642022-03-30 22:02:12 +08002116
2117 *out_len = 0;
2118
Gilles Peskine449bd832023-01-11 14:50:10 +01002119 MBEDTLS_SSL_DEBUG_MSG(3, ("server hello, adding key share extension"));
Jerry Yu3bf2c642022-03-30 22:02:12 +08002120
Gilles Peskine449bd832023-01-11 14:50:10 +01002121 MBEDTLS_SSL_DEBUG_MSG(2, ("server hello, write selected_group: %s (%04x)",
2122 mbedtls_ssl_named_group_to_str(group),
2123 group));
Jerry Yu58af2332022-09-06 11:19:31 +08002124
Jerry Yu3bf2c642022-03-30 22:02:12 +08002125 /* Check if we have space for header and length fields:
2126 * - extension_type (2 bytes)
2127 * - extension_data_length (2 bytes)
2128 * - group (2 bytes)
2129 * - key_exchange_length (2 bytes)
2130 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002131 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 8);
2132 MBEDTLS_PUT_UINT16_BE(MBEDTLS_TLS_EXT_KEY_SHARE, p, 0);
2133 MBEDTLS_PUT_UINT16_BE(group, server_share, 0);
Jerry Yu3bf2c642022-03-30 22:02:12 +08002134 p += 8;
Jerry Yu57d48412022-04-20 21:50:42 +08002135
Jerry Yu3bf2c642022-03-30 22:02:12 +08002136 /* When we introduce PQC-ECDHE hybrids, we'll want to call this
2137 * function multiple times. */
Jerry Yu955ddd72022-04-22 22:27:33 +08002138 ret = ssl_tls13_generate_and_write_key_share(
Gilles Peskine449bd832023-01-11 14:50:10 +01002139 ssl, group, server_share + 4, end, &key_exchange_length);
2140 if (ret != 0) {
2141 return ret;
2142 }
Jerry Yu3bf2c642022-03-30 22:02:12 +08002143 p += key_exchange_length;
Jerry Yuc1be19f2022-04-23 16:11:39 +08002144
Gilles Peskine449bd832023-01-11 14:50:10 +01002145 MBEDTLS_PUT_UINT16_BE(key_exchange_length, server_share + 2, 0);
Jerry Yu3bf2c642022-03-30 22:02:12 +08002146
Gilles Peskine449bd832023-01-11 14:50:10 +01002147 MBEDTLS_PUT_UINT16_BE(p - server_share, buf, 2);
Jerry Yu3bf2c642022-03-30 22:02:12 +08002148
Jerry Yu57d48412022-04-20 21:50:42 +08002149 *out_len = p - buf;
Jerry Yu955ddd72022-04-22 22:27:33 +08002150
Gilles Peskine449bd832023-01-11 14:50:10 +01002151 mbedtls_ssl_tls13_set_hs_sent_ext_mask(ssl, MBEDTLS_TLS_EXT_KEY_SHARE);
Jerry Yub95dd362022-11-08 21:19:34 +08002152
Gilles Peskine449bd832023-01-11 14:50:10 +01002153 return 0;
Jerry Yu3bf2c642022-03-30 22:02:12 +08002154}
Jerry Yud9436a12022-04-20 22:28:09 +08002155
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002156MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002157static int ssl_tls13_write_hrr_key_share_ext(mbedtls_ssl_context *ssl,
2158 unsigned char *buf,
2159 unsigned char *end,
2160 size_t *out_len)
Jerry Yu23f7a6f2022-04-23 15:16:45 +08002161{
2162 uint16_t selected_group = ssl->handshake->hrr_selected_group;
2163 /* key_share Extension
2164 *
2165 * struct {
2166 * select (Handshake.msg_type) {
2167 * ...
2168 * case hello_retry_request:
2169 * NamedGroup selected_group;
2170 * ...
2171 * };
2172 * } KeyShare;
2173 */
2174
2175 *out_len = 0;
2176
Jerry Yub0ac10b2022-05-05 11:10:08 +08002177 /*
2178 * For a pure PSK key exchange, there is no group to agree upon. The purpose
2179 * of the HRR is then to transmit a cookie to force the client to demonstrate
2180 * reachability at their apparent network address (primarily useful for DTLS).
2181 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002182 if (!mbedtls_ssl_tls13_key_exchange_mode_with_ephemeral(ssl)) {
2183 return 0;
2184 }
Jerry Yu23f7a6f2022-04-23 15:16:45 +08002185
2186 /* We should only send the key_share extension if the client's initial
2187 * key share was not acceptable. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002188 if (ssl->handshake->offered_group_id != 0) {
2189 MBEDTLS_SSL_DEBUG_MSG(4, ("Skip key_share extension in HRR"));
2190 return 0;
Jerry Yu23f7a6f2022-04-23 15:16:45 +08002191 }
2192
Gilles Peskine449bd832023-01-11 14:50:10 +01002193 if (selected_group == 0) {
2194 MBEDTLS_SSL_DEBUG_MSG(1, ("no matching named group found"));
2195 return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
Jerry Yu23f7a6f2022-04-23 15:16:45 +08002196 }
2197
Jerry Yub0ac10b2022-05-05 11:10:08 +08002198 /* Check if we have enough space:
2199 * - extension_type (2 bytes)
2200 * - extension_data_length (2 bytes)
2201 * - selected_group (2 bytes)
2202 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002203 MBEDTLS_SSL_CHK_BUF_PTR(buf, end, 6);
Jerry Yu23f7a6f2022-04-23 15:16:45 +08002204
Gilles Peskine449bd832023-01-11 14:50:10 +01002205 MBEDTLS_PUT_UINT16_BE(MBEDTLS_TLS_EXT_KEY_SHARE, buf, 0);
2206 MBEDTLS_PUT_UINT16_BE(2, buf, 2);
2207 MBEDTLS_PUT_UINT16_BE(selected_group, buf, 4);
Jerry Yu23f7a6f2022-04-23 15:16:45 +08002208
Gilles Peskine449bd832023-01-11 14:50:10 +01002209 MBEDTLS_SSL_DEBUG_MSG(3,
2210 ("HRR selected_group: %s (%x)",
2211 mbedtls_ssl_named_group_to_str(selected_group),
2212 selected_group));
Jerry Yu23f7a6f2022-04-23 15:16:45 +08002213
2214 *out_len = 6;
Jerry Yu23f7a6f2022-04-23 15:16:45 +08002215
Gilles Peskine449bd832023-01-11 14:50:10 +01002216 mbedtls_ssl_tls13_set_hs_sent_ext_mask(ssl, MBEDTLS_TLS_EXT_KEY_SHARE);
Jerry Yub95dd362022-11-08 21:19:34 +08002217
Gilles Peskine449bd832023-01-11 14:50:10 +01002218 return 0;
Jerry Yu23f7a6f2022-04-23 15:16:45 +08002219}
Jerry Yu3bf2c642022-03-30 22:02:12 +08002220
2221/*
2222 * Structure of ServerHello message:
2223 *
2224 * struct {
2225 * ProtocolVersion legacy_version = 0x0303; // TLS v1.2
2226 * Random random;
2227 * opaque legacy_session_id_echo<0..32>;
2228 * CipherSuite cipher_suite;
2229 * uint8 legacy_compression_method = 0;
2230 * Extension extensions<6..2^16-1>;
2231 * } ServerHello;
2232 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002233MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002234static int ssl_tls13_write_server_hello_body(mbedtls_ssl_context *ssl,
2235 unsigned char *buf,
2236 unsigned char *end,
2237 size_t *out_len,
2238 int is_hrr)
Jerry Yu56404d72022-03-30 17:36:13 +08002239{
Jerry Yu955ddd72022-04-22 22:27:33 +08002240 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yu3bf2c642022-03-30 22:02:12 +08002241 unsigned char *p = buf;
Jerry Yud9436a12022-04-20 22:28:09 +08002242 unsigned char *p_extensions_len;
Jerry Yufbe3e642022-04-25 19:31:51 +08002243 size_t output_len;
Jerry Yu3bf2c642022-03-30 22:02:12 +08002244
2245 *out_len = 0;
Jerry Yu50e00e32022-10-31 14:45:01 +08002246 ssl->handshake->sent_extensions = MBEDTLS_SSL_EXT_MASK_NONE;
Jerry Yu3bf2c642022-03-30 22:02:12 +08002247
Jerry Yucfc04b32022-04-21 09:31:58 +08002248 /* ...
2249 * ProtocolVersion legacy_version = 0x0303; // TLS 1.2
2250 * ...
2251 * with ProtocolVersion defined as:
2252 * uint16 ProtocolVersion;
Jerry Yu3bf2c642022-03-30 22:02:12 +08002253 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002254 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 2);
2255 MBEDTLS_PUT_UINT16_BE(0x0303, p, 0);
Jerry Yu3bf2c642022-03-30 22:02:12 +08002256 p += 2;
2257
Jerry Yu1c3e6882022-04-20 21:23:40 +08002258 /* ...
2259 * Random random;
2260 * ...
Jerry Yucfc04b32022-04-21 09:31:58 +08002261 * with Random defined as:
2262 * opaque Random[MBEDTLS_SERVER_HELLO_RANDOM_LEN];
Jerry Yu1c3e6882022-04-20 21:23:40 +08002263 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002264 MBEDTLS_SSL_CHK_BUF_PTR(p, end, MBEDTLS_SERVER_HELLO_RANDOM_LEN);
2265 if (is_hrr) {
2266 memcpy(p, mbedtls_ssl_tls13_hello_retry_request_magic,
2267 MBEDTLS_SERVER_HELLO_RANDOM_LEN);
2268 } else {
2269 memcpy(p, &ssl->handshake->randbytes[MBEDTLS_CLIENT_HELLO_RANDOM_LEN],
2270 MBEDTLS_SERVER_HELLO_RANDOM_LEN);
Jerry Yu23f7a6f2022-04-23 15:16:45 +08002271 }
Gilles Peskine449bd832023-01-11 14:50:10 +01002272 MBEDTLS_SSL_DEBUG_BUF(3, "server hello, random bytes",
2273 p, MBEDTLS_SERVER_HELLO_RANDOM_LEN);
Jerry Yu3bf2c642022-03-30 22:02:12 +08002274 p += MBEDTLS_SERVER_HELLO_RANDOM_LEN;
2275
Jerry Yucfc04b32022-04-21 09:31:58 +08002276 /* ...
2277 * opaque legacy_session_id_echo<0..32>;
2278 * ...
Jerry Yu3bf2c642022-03-30 22:02:12 +08002279 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002280 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 1 + ssl->session_negotiate->id_len);
2281 *p++ = (unsigned char) ssl->session_negotiate->id_len;
2282 if (ssl->session_negotiate->id_len > 0) {
2283 memcpy(p, &ssl->session_negotiate->id[0],
2284 ssl->session_negotiate->id_len);
Jerry Yu3bf2c642022-03-30 22:02:12 +08002285 p += ssl->session_negotiate->id_len;
Jerry Yu955ddd72022-04-22 22:27:33 +08002286
Gilles Peskine449bd832023-01-11 14:50:10 +01002287 MBEDTLS_SSL_DEBUG_BUF(3, "session id", ssl->session_negotiate->id,
2288 ssl->session_negotiate->id_len);
Jerry Yu3bf2c642022-03-30 22:02:12 +08002289 }
2290
Jerry Yucfc04b32022-04-21 09:31:58 +08002291 /* ...
2292 * CipherSuite cipher_suite;
2293 * ...
2294 * with CipherSuite defined as:
2295 * uint8 CipherSuite[2];
Jerry Yu3bf2c642022-03-30 22:02:12 +08002296 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002297 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 2);
2298 MBEDTLS_PUT_UINT16_BE(ssl->session_negotiate->ciphersuite, p, 0);
Jerry Yu3bf2c642022-03-30 22:02:12 +08002299 p += 2;
Gilles Peskine449bd832023-01-11 14:50:10 +01002300 MBEDTLS_SSL_DEBUG_MSG(3,
2301 ("server hello, chosen ciphersuite: %s ( id=%d )",
2302 mbedtls_ssl_get_ciphersuite_name(
2303 ssl->session_negotiate->ciphersuite),
2304 ssl->session_negotiate->ciphersuite));
Jerry Yu3bf2c642022-03-30 22:02:12 +08002305
Jerry Yucfc04b32022-04-21 09:31:58 +08002306 /* ...
2307 * uint8 legacy_compression_method = 0;
2308 * ...
2309 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002310 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 1);
Thomas Daubney31e03a82022-07-25 15:59:25 +01002311 *p++ = MBEDTLS_SSL_COMPRESS_NULL;
Jerry Yu3bf2c642022-03-30 22:02:12 +08002312
Jerry Yucfc04b32022-04-21 09:31:58 +08002313 /* ...
2314 * Extension extensions<6..2^16-1>;
2315 * ...
2316 * struct {
2317 * ExtensionType extension_type; (2 bytes)
2318 * opaque extension_data<0..2^16-1>;
2319 * } Extension;
2320 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002321 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 2);
Jerry Yud9436a12022-04-20 22:28:09 +08002322 p_extensions_len = p;
Jerry Yu3bf2c642022-03-30 22:02:12 +08002323 p += 2;
2324
Gilles Peskine449bd832023-01-11 14:50:10 +01002325 if ((ret = ssl_tls13_write_server_hello_supported_versions_ext(
2326 ssl, p, end, &output_len)) != 0) {
Jerry Yu955ddd72022-04-22 22:27:33 +08002327 MBEDTLS_SSL_DEBUG_RET(
Gilles Peskine449bd832023-01-11 14:50:10 +01002328 1, "ssl_tls13_write_server_hello_supported_versions_ext", ret);
2329 return ret;
Jerry Yu3bf2c642022-03-30 22:02:12 +08002330 }
2331 p += output_len;
2332
Gilles Peskine449bd832023-01-11 14:50:10 +01002333 if (mbedtls_ssl_tls13_key_exchange_mode_with_ephemeral(ssl)) {
2334 if (is_hrr) {
2335 ret = ssl_tls13_write_hrr_key_share_ext(ssl, p, end, &output_len);
2336 } else {
2337 ret = ssl_tls13_write_key_share_ext(ssl, p, end, &output_len);
2338 }
2339 if (ret != 0) {
2340 return ret;
2341 }
Jerry Yu3bf2c642022-03-30 22:02:12 +08002342 p += output_len;
2343 }
Jerry Yu3bf2c642022-03-30 22:02:12 +08002344
Ronald Cron41a443a2022-10-04 16:38:25 +02002345#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01002346 if (!is_hrr && mbedtls_ssl_tls13_key_exchange_mode_with_psk(ssl)) {
2347 ret = ssl_tls13_write_server_pre_shared_key_ext(ssl, p, end, &output_len);
2348 if (ret != 0) {
2349 MBEDTLS_SSL_DEBUG_RET(1, "ssl_tls13_write_server_pre_shared_key_ext",
2350 ret);
2351 return ret;
Jerry Yu032b15ce2022-07-11 06:10:03 +00002352 }
2353 p += output_len;
2354 }
Ronald Cron41a443a2022-10-04 16:38:25 +02002355#endif
Jerry Yu032b15ce2022-07-11 06:10:03 +00002356
Gilles Peskine449bd832023-01-11 14:50:10 +01002357 MBEDTLS_PUT_UINT16_BE(p - p_extensions_len - 2, p_extensions_len, 0);
Jerry Yu3bf2c642022-03-30 22:02:12 +08002358
Gilles Peskine449bd832023-01-11 14:50:10 +01002359 MBEDTLS_SSL_DEBUG_BUF(4, "server hello extensions",
2360 p_extensions_len, p - p_extensions_len);
Jerry Yu3bf2c642022-03-30 22:02:12 +08002361
Jerry Yud9436a12022-04-20 22:28:09 +08002362 *out_len = p - buf;
Jerry Yu3bf2c642022-03-30 22:02:12 +08002363
Gilles Peskine449bd832023-01-11 14:50:10 +01002364 MBEDTLS_SSL_DEBUG_BUF(3, "server hello", buf, *out_len);
Jerry Yu3bf2c642022-03-30 22:02:12 +08002365
Jerry Yu7de2ff02022-11-08 21:43:46 +08002366 MBEDTLS_SSL_PRINT_EXTS(
Jerry Yu4b8f2f72022-10-31 13:31:22 +08002367 3, is_hrr ? MBEDTLS_SSL_TLS1_3_HS_HELLO_RETRY_REQUEST :
Gilles Peskine449bd832023-01-11 14:50:10 +01002368 MBEDTLS_SSL_HS_SERVER_HELLO,
2369 ssl->handshake->sent_extensions);
Jerry Yu4b8f2f72022-10-31 13:31:22 +08002370
Gilles Peskine449bd832023-01-11 14:50:10 +01002371 return ret;
Jerry Yu56404d72022-03-30 17:36:13 +08002372}
2373
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002374MBEDTLS_CHECK_RETURN_CRITICAL
Xiaokang Qian934ce6f2023-02-06 10:23:04 +00002375static int ssl_tls13_finalize_server_hello(mbedtls_ssl_context *ssl)
Jerry Yue110d252022-05-05 10:19:22 +08002376{
2377 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine449bd832023-01-11 14:50:10 +01002378 ret = mbedtls_ssl_tls13_compute_handshake_transform(ssl);
2379 if (ret != 0) {
2380 MBEDTLS_SSL_DEBUG_RET(1,
2381 "mbedtls_ssl_tls13_compute_handshake_transform",
2382 ret);
2383 return ret;
Jerry Yue110d252022-05-05 10:19:22 +08002384 }
2385
Gilles Peskine449bd832023-01-11 14:50:10 +01002386 return ret;
Jerry Yue110d252022-05-05 10:19:22 +08002387}
2388
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002389MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002390static int ssl_tls13_write_server_hello(mbedtls_ssl_context *ssl)
Jerry Yu5b64ae92022-03-30 17:15:02 +08002391{
Jerry Yu637a3f12022-04-20 21:37:58 +08002392 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yuf4b27e42022-03-30 17:32:21 +08002393 unsigned char *buf;
2394 size_t buf_len, msg_len;
2395
Gilles Peskine449bd832023-01-11 14:50:10 +01002396 MBEDTLS_SSL_DEBUG_MSG(2, ("=> write server hello"));
Jerry Yuf4b27e42022-03-30 17:32:21 +08002397
Gilles Peskine449bd832023-01-11 14:50:10 +01002398 MBEDTLS_SSL_PROC_CHK(ssl_tls13_prepare_server_hello(ssl));
Jerry Yuf4b27e42022-03-30 17:32:21 +08002399
Xiaokang Qian9f1747b2023-03-30 02:50:04 +00002400 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_start_handshake_msg(
2401 ssl, MBEDTLS_SSL_HS_SERVER_HELLO, &buf, &buf_len));
Jerry Yuf4b27e42022-03-30 17:32:21 +08002402
Gilles Peskine449bd832023-01-11 14:50:10 +01002403 MBEDTLS_SSL_PROC_CHK(ssl_tls13_write_server_hello_body(ssl, buf,
2404 buf + buf_len,
2405 &msg_len,
2406 0));
Jerry Yuf4b27e42022-03-30 17:32:21 +08002407
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +01002408 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_add_hs_msg_to_checksum(
Manuel Pégourié-Gonnard43cc1272023-02-06 11:48:19 +01002409 ssl, MBEDTLS_SSL_HS_SERVER_HELLO, buf, msg_len));
Jerry Yuf4b27e42022-03-30 17:32:21 +08002410
Gilles Peskine449bd832023-01-11 14:50:10 +01002411 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_finish_handshake_msg(
2412 ssl, buf_len, msg_len));
Jerry Yu637a3f12022-04-20 21:37:58 +08002413
Xiaokang Qian934ce6f2023-02-06 10:23:04 +00002414 MBEDTLS_SSL_PROC_CHK(ssl_tls13_finalize_server_hello(ssl));
Jerry Yue110d252022-05-05 10:19:22 +08002415
Gabor Mezei7b39bf12022-05-24 16:04:14 +02002416#if defined(MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE)
2417 /* The server sends a dummy change_cipher_spec record immediately
2418 * after its first handshake message. This may either be after
2419 * a ServerHello or a HelloRetryRequest.
2420 */
Gabor Mezei96ae9262022-06-28 11:45:18 +02002421 mbedtls_ssl_handshake_set_state(
Gilles Peskine449bd832023-01-11 14:50:10 +01002422 ssl, MBEDTLS_SSL_SERVER_CCS_AFTER_SERVER_HELLO);
Gabor Mezei7b39bf12022-05-24 16:04:14 +02002423#else
Gilles Peskine449bd832023-01-11 14:50:10 +01002424 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_ENCRYPTED_EXTENSIONS);
Gabor Mezei7b39bf12022-05-24 16:04:14 +02002425#endif /* MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE */
Jerry Yue110d252022-05-05 10:19:22 +08002426
Jerry Yuf4b27e42022-03-30 17:32:21 +08002427cleanup:
2428
Gilles Peskine449bd832023-01-11 14:50:10 +01002429 MBEDTLS_SSL_DEBUG_MSG(2, ("<= write server hello"));
2430 return ret;
Jerry Yu5b64ae92022-03-30 17:15:02 +08002431}
2432
Jerry Yu23d1a252022-05-12 18:08:59 +08002433
2434/*
2435 * Handler for MBEDTLS_SSL_HELLO_RETRY_REQUEST
2436 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002437MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002438static int ssl_tls13_prepare_hello_retry_request(mbedtls_ssl_context *ssl)
Jerry Yu23d1a252022-05-12 18:08:59 +08002439{
2440 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Ronald Cron5fbd2702024-02-14 10:03:36 +01002441 if (ssl->handshake->hello_retry_request_flag) {
Gilles Peskine449bd832023-01-11 14:50:10 +01002442 MBEDTLS_SSL_DEBUG_MSG(1, ("Too many HRRs"));
2443 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE,
2444 MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE);
2445 return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
Jerry Yu23d1a252022-05-12 18:08:59 +08002446 }
2447
2448 /*
2449 * Create stateless transcript hash for HRR
2450 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002451 MBEDTLS_SSL_DEBUG_MSG(4, ("Reset transcript for HRR"));
2452 ret = mbedtls_ssl_reset_transcript_for_hrr(ssl);
2453 if (ret != 0) {
2454 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_reset_transcript_for_hrr", ret);
2455 return ret;
Jerry Yu23d1a252022-05-12 18:08:59 +08002456 }
Gilles Peskine449bd832023-01-11 14:50:10 +01002457 mbedtls_ssl_session_reset_msg_layer(ssl, 0);
Jerry Yu23d1a252022-05-12 18:08:59 +08002458
Gilles Peskine449bd832023-01-11 14:50:10 +01002459 return 0;
Jerry Yu23d1a252022-05-12 18:08:59 +08002460}
2461
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002462MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002463static int ssl_tls13_write_hello_retry_request(mbedtls_ssl_context *ssl)
Jerry Yu23d1a252022-05-12 18:08:59 +08002464{
2465 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
2466 unsigned char *buf;
2467 size_t buf_len, msg_len;
2468
Gilles Peskine449bd832023-01-11 14:50:10 +01002469 MBEDTLS_SSL_DEBUG_MSG(2, ("=> write hello retry request"));
Jerry Yu23d1a252022-05-12 18:08:59 +08002470
Gilles Peskine449bd832023-01-11 14:50:10 +01002471 MBEDTLS_SSL_PROC_CHK(ssl_tls13_prepare_hello_retry_request(ssl));
Jerry Yu23d1a252022-05-12 18:08:59 +08002472
Gilles Peskine449bd832023-01-11 14:50:10 +01002473 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_start_handshake_msg(
2474 ssl, MBEDTLS_SSL_HS_SERVER_HELLO,
2475 &buf, &buf_len));
Jerry Yu23d1a252022-05-12 18:08:59 +08002476
Gilles Peskine449bd832023-01-11 14:50:10 +01002477 MBEDTLS_SSL_PROC_CHK(ssl_tls13_write_server_hello_body(ssl, buf,
2478 buf + buf_len,
2479 &msg_len,
2480 1));
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +01002481 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_add_hs_msg_to_checksum(
Manuel Pégourié-Gonnard43cc1272023-02-06 11:48:19 +01002482 ssl, MBEDTLS_SSL_HS_SERVER_HELLO, buf, msg_len));
Jerry Yu23d1a252022-05-12 18:08:59 +08002483
2484
Gilles Peskine449bd832023-01-11 14:50:10 +01002485 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_finish_handshake_msg(ssl, buf_len,
2486 msg_len));
Jerry Yu23d1a252022-05-12 18:08:59 +08002487
Ronald Cron5fbd2702024-02-14 10:03:36 +01002488 ssl->handshake->hello_retry_request_flag = 1;
Jerry Yu23d1a252022-05-12 18:08:59 +08002489
Gabor Mezei7b39bf12022-05-24 16:04:14 +02002490#if defined(MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE)
2491 /* The server sends a dummy change_cipher_spec record immediately
2492 * after its first handshake message. This may either be after
2493 * a ServerHello or a HelloRetryRequest.
2494 */
Gabor Mezei96ae9262022-06-28 11:45:18 +02002495 mbedtls_ssl_handshake_set_state(
Gilles Peskine449bd832023-01-11 14:50:10 +01002496 ssl, MBEDTLS_SSL_SERVER_CCS_AFTER_HELLO_RETRY_REQUEST);
Gabor Mezei7b39bf12022-05-24 16:04:14 +02002497#else
Gilles Peskine449bd832023-01-11 14:50:10 +01002498 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_CLIENT_HELLO);
Gabor Mezei7b39bf12022-05-24 16:04:14 +02002499#endif /* MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE */
Jerry Yu23d1a252022-05-12 18:08:59 +08002500
2501cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +01002502 MBEDTLS_SSL_DEBUG_MSG(2, ("<= write hello retry request"));
2503 return ret;
Jerry Yu23d1a252022-05-12 18:08:59 +08002504}
2505
Jerry Yu5b64ae92022-03-30 17:15:02 +08002506/*
Jerry Yu4d3841a2022-04-16 12:37:19 +08002507 * Handler for MBEDTLS_SSL_ENCRYPTED_EXTENSIONS
2508 */
Jerry Yu4d3841a2022-04-16 12:37:19 +08002509
2510/*
2511 * struct {
2512 * Extension extensions<0..2 ^ 16 - 1>;
2513 * } EncryptedExtensions;
2514 *
2515 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002516MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002517static int ssl_tls13_write_encrypted_extensions_body(mbedtls_ssl_context *ssl,
2518 unsigned char *buf,
2519 unsigned char *end,
2520 size_t *out_len)
Jerry Yu4d3841a2022-04-16 12:37:19 +08002521{
XiaokangQianacb39922022-06-17 10:18:48 +00002522 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yu4d3841a2022-04-16 12:37:19 +08002523 unsigned char *p = buf;
2524 size_t extensions_len = 0;
Jerry Yu8937eb42022-05-03 12:12:14 +08002525 unsigned char *p_extensions_len;
XiaokangQianacb39922022-06-17 10:18:48 +00002526 size_t output_len;
Jerry Yu9da5e5a2022-05-03 15:46:09 +08002527
Jerry Yu4d3841a2022-04-16 12:37:19 +08002528 *out_len = 0;
2529
Gilles Peskine449bd832023-01-11 14:50:10 +01002530 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 2);
Jerry Yu8937eb42022-05-03 12:12:14 +08002531 p_extensions_len = p;
Jerry Yu4d3841a2022-04-16 12:37:19 +08002532 p += 2;
2533
Jerry Yu8937eb42022-05-03 12:12:14 +08002534 ((void) ssl);
XiaokangQianacb39922022-06-17 10:18:48 +00002535 ((void) ret);
2536 ((void) output_len);
2537
2538#if defined(MBEDTLS_SSL_ALPN)
Gilles Peskine449bd832023-01-11 14:50:10 +01002539 ret = mbedtls_ssl_write_alpn_ext(ssl, p, end, &output_len);
2540 if (ret != 0) {
2541 return ret;
2542 }
XiaokangQian95d5f542022-06-24 02:29:26 +00002543 p += output_len;
XiaokangQianacb39922022-06-17 10:18:48 +00002544#endif /* MBEDTLS_SSL_ALPN */
Jerry Yu4d3841a2022-04-16 12:37:19 +08002545
Jerry Yu71c14f12022-12-12 11:10:35 +08002546#if defined(MBEDTLS_SSL_EARLY_DATA)
Ronald Cron78a38f62024-02-01 18:30:31 +01002547 if (ssl->handshake->early_data_accepted) {
Jerry Yu52335392023-11-23 18:06:06 +08002548 ret = mbedtls_ssl_tls13_write_early_data_ext(
Jerry Yuc59c5862023-12-05 10:40:49 +08002549 ssl, 0, p, end, &output_len);
Jerry Yu71c14f12022-12-12 11:10:35 +08002550 if (ret != 0) {
2551 return ret;
2552 }
2553 p += output_len;
2554 }
2555#endif /* MBEDTLS_SSL_EARLY_DATA */
2556
Waleed Elmelegy47d29462024-01-03 17:31:52 +00002557#if defined(MBEDTLS_SSL_RECORD_SIZE_LIMIT)
Waleed Elmelegyfbe42742024-01-05 18:11:10 +00002558 if (ssl->handshake->received_extensions & MBEDTLS_SSL_EXT_MASK(RECORD_SIZE_LIMIT)) {
Waleed Elmelegyd2fc90e2024-01-04 18:04:53 +00002559 ret = mbedtls_ssl_tls13_write_record_size_limit_ext(
2560 ssl, p, end, &output_len);
2561 if (ret != 0) {
2562 return ret;
2563 }
2564 p += output_len;
Waleed Elmelegy47d29462024-01-03 17:31:52 +00002565 }
Waleed Elmelegy47d29462024-01-03 17:31:52 +00002566#endif
2567
Gilles Peskine449bd832023-01-11 14:50:10 +01002568 extensions_len = (p - p_extensions_len) - 2;
2569 MBEDTLS_PUT_UINT16_BE(extensions_len, p_extensions_len, 0);
Jerry Yu8937eb42022-05-03 12:12:14 +08002570
2571 *out_len = p - buf;
Jerry Yu4d3841a2022-04-16 12:37:19 +08002572
Gilles Peskine449bd832023-01-11 14:50:10 +01002573 MBEDTLS_SSL_DEBUG_BUF(4, "encrypted extensions", buf, *out_len);
Jerry Yu4d3841a2022-04-16 12:37:19 +08002574
Jerry Yu7de2ff02022-11-08 21:43:46 +08002575 MBEDTLS_SSL_PRINT_EXTS(
Gilles Peskine449bd832023-01-11 14:50:10 +01002576 3, MBEDTLS_SSL_HS_ENCRYPTED_EXTENSIONS, ssl->handshake->sent_extensions);
Jerry Yu4b8f2f72022-10-31 13:31:22 +08002577
Gilles Peskine449bd832023-01-11 14:50:10 +01002578 return 0;
Jerry Yu4d3841a2022-04-16 12:37:19 +08002579}
2580
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002581MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002582static int ssl_tls13_write_encrypted_extensions(mbedtls_ssl_context *ssl)
Jerry Yu4d3841a2022-04-16 12:37:19 +08002583{
2584 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
2585 unsigned char *buf;
Jerry Yu39730a72022-05-03 12:14:04 +08002586 size_t buf_len, msg_len;
Jerry Yu4d3841a2022-04-16 12:37:19 +08002587
Gilles Peskine449bd832023-01-11 14:50:10 +01002588 mbedtls_ssl_set_outbound_transform(ssl,
2589 ssl->handshake->transform_handshake);
Gabor Mezei54719122022-06-28 11:34:56 +02002590 MBEDTLS_SSL_DEBUG_MSG(
Gilles Peskine449bd832023-01-11 14:50:10 +01002591 3, ("switching to handshake transform for outbound data"));
Gabor Mezei54719122022-06-28 11:34:56 +02002592
Gilles Peskine449bd832023-01-11 14:50:10 +01002593 MBEDTLS_SSL_DEBUG_MSG(2, ("=> write encrypted extensions"));
Jerry Yu4d3841a2022-04-16 12:37:19 +08002594
Xiaokang Qian9f1747b2023-03-30 02:50:04 +00002595 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_start_handshake_msg(
2596 ssl, MBEDTLS_SSL_HS_ENCRYPTED_EXTENSIONS,
2597 &buf, &buf_len));
Jerry Yu4d3841a2022-04-16 12:37:19 +08002598
Gilles Peskine449bd832023-01-11 14:50:10 +01002599 MBEDTLS_SSL_PROC_CHK(ssl_tls13_write_encrypted_extensions_body(
2600 ssl, buf, buf + buf_len, &msg_len));
Jerry Yu4d3841a2022-04-16 12:37:19 +08002601
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +01002602 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_add_hs_msg_to_checksum(
Xiaokang Qian9f1747b2023-03-30 02:50:04 +00002603 ssl, MBEDTLS_SSL_HS_ENCRYPTED_EXTENSIONS,
2604 buf, msg_len));
Jerry Yu4d3841a2022-04-16 12:37:19 +08002605
Gilles Peskine449bd832023-01-11 14:50:10 +01002606 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_finish_handshake_msg(
2607 ssl, buf_len, msg_len));
Jerry Yu4d3841a2022-04-16 12:37:19 +08002608
Ronald Cron928cbd32022-10-04 16:14:26 +02002609#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01002610 if (mbedtls_ssl_tls13_key_exchange_mode_with_psk(ssl)) {
2611 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_SERVER_FINISHED);
2612 } else {
2613 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_CERTIFICATE_REQUEST);
2614 }
Jerry Yu8937eb42022-05-03 12:12:14 +08002615#else
Gilles Peskine449bd832023-01-11 14:50:10 +01002616 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_SERVER_FINISHED);
Jerry Yu8937eb42022-05-03 12:12:14 +08002617#endif
2618
Jerry Yu4d3841a2022-04-16 12:37:19 +08002619cleanup:
2620
Gilles Peskine449bd832023-01-11 14:50:10 +01002621 MBEDTLS_SSL_DEBUG_MSG(2, ("<= write encrypted extensions"));
2622 return ret;
Jerry Yu4d3841a2022-04-16 12:37:19 +08002623}
2624
Ronald Cron928cbd32022-10-04 16:14:26 +02002625#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED)
XiaokangQiana987e1d2022-05-07 01:25:58 +00002626#define SSL_CERTIFICATE_REQUEST_SEND_REQUEST 0
2627#define SSL_CERTIFICATE_REQUEST_SKIP 1
2628/* Coordination:
2629 * Check whether a CertificateRequest message should be written.
2630 * Returns a negative code on failure, or
2631 * - SSL_CERTIFICATE_REQUEST_SEND_REQUEST
2632 * - SSL_CERTIFICATE_REQUEST_SKIP
2633 * indicating if the writing of the CertificateRequest
2634 * should be skipped or not.
2635 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002636MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002637static int ssl_tls13_certificate_request_coordinate(mbedtls_ssl_context *ssl)
XiaokangQiana987e1d2022-05-07 01:25:58 +00002638{
2639 int authmode;
2640
XiaokangQian40a35232022-05-07 09:02:40 +00002641#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Gilles Peskine449bd832023-01-11 14:50:10 +01002642 if (ssl->handshake->sni_authmode != MBEDTLS_SSL_VERIFY_UNSET) {
XiaokangQian40a35232022-05-07 09:02:40 +00002643 authmode = ssl->handshake->sni_authmode;
Gilles Peskine449bd832023-01-11 14:50:10 +01002644 } else
XiaokangQian40a35232022-05-07 09:02:40 +00002645#endif
XiaokangQiana987e1d2022-05-07 01:25:58 +00002646 authmode = ssl->conf->authmode;
2647
Gilles Peskine449bd832023-01-11 14:50:10 +01002648 if (authmode == MBEDTLS_SSL_VERIFY_NONE) {
Ronald Croneac00ad2022-09-13 10:16:31 +02002649 ssl->session_negotiate->verify_result = MBEDTLS_X509_BADCERT_SKIP_VERIFY;
Gilles Peskine449bd832023-01-11 14:50:10 +01002650 return SSL_CERTIFICATE_REQUEST_SKIP;
Ronald Croneac00ad2022-09-13 10:16:31 +02002651 }
XiaokangQiana987e1d2022-05-07 01:25:58 +00002652
XiaokangQianc3017f62022-05-13 05:55:41 +00002653 ssl->handshake->certificate_request_sent = 1;
XiaokangQian189ded22022-05-10 08:12:17 +00002654
Gilles Peskine449bd832023-01-11 14:50:10 +01002655 return SSL_CERTIFICATE_REQUEST_SEND_REQUEST;
XiaokangQiana987e1d2022-05-07 01:25:58 +00002656}
2657
XiaokangQiancec9ae62022-05-06 07:28:50 +00002658/*
2659 * struct {
2660 * opaque certificate_request_context<0..2^8-1>;
2661 * Extension extensions<2..2^16-1>;
2662 * } CertificateRequest;
2663 *
2664 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002665MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002666static int ssl_tls13_write_certificate_request_body(mbedtls_ssl_context *ssl,
2667 unsigned char *buf,
2668 const unsigned char *end,
2669 size_t *out_len)
XiaokangQiancec9ae62022-05-06 07:28:50 +00002670{
2671 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
2672 unsigned char *p = buf;
XiaokangQianec6efb92022-05-06 09:53:10 +00002673 size_t output_len = 0;
XiaokangQiancec9ae62022-05-06 07:28:50 +00002674 unsigned char *p_extensions_len;
2675
2676 *out_len = 0;
2677
2678 /* Check if we have enough space:
2679 * - certificate_request_context (1 byte)
2680 * - extensions length (2 bytes)
2681 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002682 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 3);
XiaokangQiancec9ae62022-05-06 07:28:50 +00002683
2684 /*
2685 * Write certificate_request_context
2686 */
2687 /*
2688 * We use a zero length context for the normal handshake
2689 * messages. For post-authentication handshake messages
2690 * this request context would be set to a non-zero value.
2691 */
2692 *p++ = 0x0;
2693
2694 /*
2695 * Write extensions
2696 */
2697 /* The extensions must contain the signature_algorithms. */
2698 p_extensions_len = p;
2699 p += 2;
Gilles Peskine449bd832023-01-11 14:50:10 +01002700 ret = mbedtls_ssl_write_sig_alg_ext(ssl, p, end, &output_len);
2701 if (ret != 0) {
2702 return ret;
2703 }
XiaokangQiancec9ae62022-05-06 07:28:50 +00002704
XiaokangQianec6efb92022-05-06 09:53:10 +00002705 p += output_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01002706 MBEDTLS_PUT_UINT16_BE(p - p_extensions_len - 2, p_extensions_len, 0);
XiaokangQiancec9ae62022-05-06 07:28:50 +00002707
2708 *out_len = p - buf;
2709
Jerry Yu7de2ff02022-11-08 21:43:46 +08002710 MBEDTLS_SSL_PRINT_EXTS(
Gilles Peskine449bd832023-01-11 14:50:10 +01002711 3, MBEDTLS_SSL_HS_CERTIFICATE_REQUEST, ssl->handshake->sent_extensions);
Jerry Yu4b8f2f72022-10-31 13:31:22 +08002712
Gilles Peskine449bd832023-01-11 14:50:10 +01002713 return 0;
XiaokangQiancec9ae62022-05-06 07:28:50 +00002714}
2715
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002716MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002717static int ssl_tls13_write_certificate_request(mbedtls_ssl_context *ssl)
XiaokangQiancec9ae62022-05-06 07:28:50 +00002718{
2719 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
2720
Gilles Peskine449bd832023-01-11 14:50:10 +01002721 MBEDTLS_SSL_DEBUG_MSG(2, ("=> write certificate request"));
XiaokangQiancec9ae62022-05-06 07:28:50 +00002722
Gilles Peskine449bd832023-01-11 14:50:10 +01002723 MBEDTLS_SSL_PROC_CHK_NEG(ssl_tls13_certificate_request_coordinate(ssl));
XiaokangQiancec9ae62022-05-06 07:28:50 +00002724
Gilles Peskine449bd832023-01-11 14:50:10 +01002725 if (ret == SSL_CERTIFICATE_REQUEST_SEND_REQUEST) {
XiaokangQiancec9ae62022-05-06 07:28:50 +00002726 unsigned char *buf;
2727 size_t buf_len, msg_len;
2728
Xiaokang Qian9f1747b2023-03-30 02:50:04 +00002729 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_start_handshake_msg(
2730 ssl, MBEDTLS_SSL_HS_CERTIFICATE_REQUEST,
2731 &buf, &buf_len));
XiaokangQiancec9ae62022-05-06 07:28:50 +00002732
Gilles Peskine449bd832023-01-11 14:50:10 +01002733 MBEDTLS_SSL_PROC_CHK(ssl_tls13_write_certificate_request_body(
2734 ssl, buf, buf + buf_len, &msg_len));
XiaokangQiancec9ae62022-05-06 07:28:50 +00002735
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +01002736 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_add_hs_msg_to_checksum(
Xiaokang Qian9f1747b2023-03-30 02:50:04 +00002737 ssl, MBEDTLS_SSL_HS_CERTIFICATE_REQUEST,
2738 buf, msg_len));
XiaokangQiancec9ae62022-05-06 07:28:50 +00002739
Gilles Peskine449bd832023-01-11 14:50:10 +01002740 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_finish_handshake_msg(
2741 ssl, buf_len, msg_len));
2742 } else if (ret == SSL_CERTIFICATE_REQUEST_SKIP) {
2743 MBEDTLS_SSL_DEBUG_MSG(2, ("<= skip write certificate request"));
XiaokangQiancec9ae62022-05-06 07:28:50 +00002744 ret = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01002745 } else {
2746 MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
XiaokangQiancec9ae62022-05-06 07:28:50 +00002747 ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
2748 goto cleanup;
2749 }
2750
Gilles Peskine449bd832023-01-11 14:50:10 +01002751 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_SERVER_CERTIFICATE);
XiaokangQiancec9ae62022-05-06 07:28:50 +00002752cleanup:
2753
Gilles Peskine449bd832023-01-11 14:50:10 +01002754 MBEDTLS_SSL_DEBUG_MSG(2, ("<= write certificate request"));
2755 return ret;
XiaokangQiancec9ae62022-05-06 07:28:50 +00002756}
XiaokangQiancec9ae62022-05-06 07:28:50 +00002757
Jerry Yu4d3841a2022-04-16 12:37:19 +08002758/*
Jerry Yuc6e6dbf2022-04-16 19:42:57 +08002759 * Handler for MBEDTLS_SSL_SERVER_CERTIFICATE
Jerry Yu83da34e2022-04-16 13:59:52 +08002760 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002761MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002762static int ssl_tls13_write_server_certificate(mbedtls_ssl_context *ssl)
Jerry Yu83da34e2022-04-16 13:59:52 +08002763{
Jerry Yu5a26f302022-05-10 20:46:40 +08002764 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
XiaokangQianfb665a82022-06-15 03:57:21 +00002765
2766#if defined(MBEDTLS_X509_CRT_PARSE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01002767 if ((ssl_tls13_pick_key_cert(ssl) != 0) ||
2768 mbedtls_ssl_own_cert(ssl) == NULL) {
2769 MBEDTLS_SSL_DEBUG_MSG(2, ("No certificate available."));
2770 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE,
2771 MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE);
2772 return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
Jerry Yu5a26f302022-05-10 20:46:40 +08002773 }
XiaokangQianfb665a82022-06-15 03:57:21 +00002774#endif /* MBEDTLS_X509_CRT_PARSE_C */
Jerry Yu5a26f302022-05-10 20:46:40 +08002775
Gilles Peskine449bd832023-01-11 14:50:10 +01002776 ret = mbedtls_ssl_tls13_write_certificate(ssl);
2777 if (ret != 0) {
2778 return ret;
2779 }
2780 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_CERTIFICATE_VERIFY);
2781 return 0;
Jerry Yu83da34e2022-04-16 13:59:52 +08002782}
2783
2784/*
Jerry Yuc6e6dbf2022-04-16 19:42:57 +08002785 * Handler for MBEDTLS_SSL_CERTIFICATE_VERIFY
Jerry Yu83da34e2022-04-16 13:59:52 +08002786 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002787MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002788static int ssl_tls13_write_certificate_verify(mbedtls_ssl_context *ssl)
Jerry Yu83da34e2022-04-16 13:59:52 +08002789{
Gilles Peskine449bd832023-01-11 14:50:10 +01002790 int ret = mbedtls_ssl_tls13_write_certificate_verify(ssl);
2791 if (ret != 0) {
2792 return ret;
2793 }
2794 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_SERVER_FINISHED);
2795 return 0;
Jerry Yu83da34e2022-04-16 13:59:52 +08002796}
Ronald Cron928cbd32022-10-04 16:14:26 +02002797#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED */
Jerry Yu83da34e2022-04-16 13:59:52 +08002798
Jerry Yu9b72e392023-12-01 16:27:08 +08002799/*
2800 * RFC 8446 section A.2
2801 *
2802 * | Send ServerHello
2803 * | K_send = handshake
2804 * | Send EncryptedExtensions
2805 * | [Send CertificateRequest]
2806 * Can send | [Send Certificate + CertificateVerify]
2807 * app data | Send Finished
2808 * after --> | K_send = application
2809 * here +--------+--------+
2810 * No 0-RTT | | 0-RTT
2811 * | |
2812 * K_recv = handshake | | K_recv = early data
2813 * [Skip decrypt errors] | +------> WAIT_EOED -+
2814 * | | Recv | | Recv EndOfEarlyData
2815 * | | early data | | K_recv = handshake
2816 * | +------------+ |
2817 * | |
2818 * +> WAIT_FLIGHT2 <--------+
2819 * |
2820 * +--------+--------+
2821 * No auth | | Client auth
2822 * | |
2823 * | v
2824 * | WAIT_CERT
2825 * | Recv | | Recv Certificate
2826 * | empty | v
2827 * | Certificate | WAIT_CV
2828 * | | | Recv
2829 * | v | CertificateVerify
2830 * +-> WAIT_FINISHED <---+
2831 * | Recv Finished
2832 *
2833 *
2834 * The following function handles the state changes after WAIT_FLIGHT2 in the
Jerry Yu3be85072023-12-04 09:58:54 +08002835 * above diagram. We are not going to receive early data related messages
2836 * anymore, prepare to receive the first handshake message of the client
2837 * second flight.
Jerry Yu9b72e392023-12-01 16:27:08 +08002838 */
Jerry Yu3be85072023-12-04 09:58:54 +08002839static void ssl_tls13_prepare_for_handshake_second_flight(
2840 mbedtls_ssl_context *ssl)
Jerry Yu9b72e392023-12-01 16:27:08 +08002841{
Jerry Yu9b72e392023-12-01 16:27:08 +08002842 if (ssl->handshake->certificate_request_sent) {
2843 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_CLIENT_CERTIFICATE);
2844 } else {
Jerry Yu42020fb2023-12-05 17:35:53 +08002845 MBEDTLS_SSL_DEBUG_MSG(2, ("skip parse certificate"));
Jerry Yuebb1b1d2023-12-05 11:02:15 +08002846 MBEDTLS_SSL_DEBUG_MSG(2, ("skip parse certificate verify"));
Jerry Yuebb1b1d2023-12-05 11:02:15 +08002847
Jerry Yu9b72e392023-12-01 16:27:08 +08002848 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_CLIENT_FINISHED);
2849 }
Jerry Yu9b72e392023-12-01 16:27:08 +08002850}
2851
Jerry Yu83da34e2022-04-16 13:59:52 +08002852/*
Jerry Yud6e253d2022-05-18 13:59:24 +08002853 * Handler for MBEDTLS_SSL_SERVER_FINISHED
Jerry Yu69dd8d42022-04-16 12:51:26 +08002854 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002855MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002856static int ssl_tls13_write_server_finished(mbedtls_ssl_context *ssl)
Jerry Yu69dd8d42022-04-16 12:51:26 +08002857{
Jerry Yud6e253d2022-05-18 13:59:24 +08002858 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yu27bdc7c2022-04-16 13:33:27 +08002859
Gilles Peskine449bd832023-01-11 14:50:10 +01002860 ret = mbedtls_ssl_tls13_write_finished_message(ssl);
2861 if (ret != 0) {
2862 return ret;
2863 }
Jerry Yu27bdc7c2022-04-16 13:33:27 +08002864
Gilles Peskine449bd832023-01-11 14:50:10 +01002865 ret = mbedtls_ssl_tls13_compute_application_transform(ssl);
2866 if (ret != 0) {
Jerry Yue3d67cb2022-05-19 15:33:10 +08002867 MBEDTLS_SSL_PEND_FATAL_ALERT(
Gilles Peskine449bd832023-01-11 14:50:10 +01002868 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE,
2869 MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE);
2870 return ret;
Jerry Yue3d67cb2022-05-19 15:33:10 +08002871 }
XiaokangQianc3017f62022-05-13 05:55:41 +00002872
Jerry Yu87b5ed42022-12-12 13:07:07 +08002873#if defined(MBEDTLS_SSL_EARLY_DATA)
Ronald Cron78a38f62024-02-01 18:30:31 +01002874 if (ssl->handshake->early_data_accepted) {
Jerry Yu0af63dc2023-12-01 17:14:51 +08002875 /* See RFC 8446 section A.2 for more information */
Jerry Yu87b5ed42022-12-12 13:07:07 +08002876 MBEDTLS_SSL_DEBUG_MSG(
2877 1, ("Switch to early keys for inbound traffic. "
2878 "( K_recv = early data )"));
2879 mbedtls_ssl_set_inbound_transform(
2880 ssl, ssl->handshake->transform_earlydata);
2881 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_END_OF_EARLY_DATA);
2882 return 0;
2883 }
2884#endif /* MBEDTLS_SSL_EARLY_DATA */
Jerry Yu0af63dc2023-12-01 17:14:51 +08002885 MBEDTLS_SSL_DEBUG_MSG(
2886 1, ("Switch to handshake keys for inbound traffic "
2887 "( K_recv = handshake )"));
Gilles Peskine449bd832023-01-11 14:50:10 +01002888 mbedtls_ssl_set_inbound_transform(ssl, ssl->handshake->transform_handshake);
XiaokangQian189ded22022-05-10 08:12:17 +00002889
Jerry Yu3be85072023-12-04 09:58:54 +08002890 ssl_tls13_prepare_for_handshake_second_flight(ssl);
Jerry Yu7d8c3fe2022-12-12 12:59:44 +08002891
2892 return 0;
2893}
2894
Jerry Yu87b5ed42022-12-12 13:07:07 +08002895#if defined(MBEDTLS_SSL_EARLY_DATA)
2896/*
Jerry Yu59d420f2023-12-01 16:30:34 +08002897 * Handler for MBEDTLS_SSL_END_OF_EARLY_DATA
Jerry Yu87b5ed42022-12-12 13:07:07 +08002898 */
Jerry Yu3be85072023-12-04 09:58:54 +08002899#define SSL_GOT_END_OF_EARLY_DATA 0
Jerry Yub55f9eb2023-12-05 10:27:17 +08002900#define SSL_GOT_EARLY_DATA 1
Jerry Yud5c34962023-12-01 16:32:31 +08002901/* Coordination:
Jerry Yu3be85072023-12-04 09:58:54 +08002902 * Deals with the ambiguity of not knowing if the next message is an
2903 * EndOfEarlyData message or an application message containing early data.
Jerry Yud5c34962023-12-01 16:32:31 +08002904 * Returns a negative code on failure, or
Jerry Yu3be85072023-12-04 09:58:54 +08002905 * - SSL_GOT_END_OF_EARLY_DATA
Jerry Yub55f9eb2023-12-05 10:27:17 +08002906 * - SSL_GOT_EARLY_DATA
Jerry Yud5c34962023-12-01 16:32:31 +08002907 * indicating which message is received.
2908 */
2909MBEDTLS_CHECK_RETURN_CRITICAL
2910static int ssl_tls13_end_of_early_data_coordinate(mbedtls_ssl_context *ssl)
2911{
2912 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yub4ed4602023-12-01 16:34:00 +08002913
2914 if ((ret = mbedtls_ssl_read_record(ssl, 0)) != 0) {
2915 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_read_record", ret);
2916 return ret;
2917 }
2918 ssl->keep_current_message = 1;
2919
2920 if (ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
2921 ssl->in_msg[0] == MBEDTLS_SSL_HS_END_OF_EARLY_DATA) {
Jerry Yub55f9eb2023-12-05 10:27:17 +08002922 MBEDTLS_SSL_DEBUG_MSG(3, ("Received an end_of_early_data message."));
Jerry Yu3be85072023-12-04 09:58:54 +08002923 return SSL_GOT_END_OF_EARLY_DATA;
Jerry Yub4ed4602023-12-01 16:34:00 +08002924 }
2925
2926 if (ssl->in_msgtype == MBEDTLS_SSL_MSG_APPLICATION_DATA) {
Jerry Yub55f9eb2023-12-05 10:27:17 +08002927 MBEDTLS_SSL_DEBUG_MSG(3, ("Received early data"));
Jerry Yu6a5904d2023-12-06 17:11:12 +08002928 /* RFC 8446 section 4.6.1
2929 *
2930 * A server receiving more than max_early_data_size bytes of 0-RTT data
2931 * SHOULD terminate the connection with an "unexpected_message" alert.
2932 *
2933 * TODO: Add received data size check here.
2934 */
Ronald Croned7d4bf2024-01-31 07:55:19 +01002935 if (ssl->in_offt == NULL) {
2936 /* Set the reading pointer */
2937 ssl->in_offt = ssl->in_msg;
2938 }
Jerry Yub55f9eb2023-12-05 10:27:17 +08002939 return SSL_GOT_EARLY_DATA;
Jerry Yub4ed4602023-12-01 16:34:00 +08002940 }
2941
Jerry Yu7bb40a32023-12-04 10:04:15 +08002942 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE,
2943 MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE);
Jerry Yub4ed4602023-12-01 16:34:00 +08002944 return MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE;
Jerry Yud5c34962023-12-01 16:32:31 +08002945}
2946
2947MBEDTLS_CHECK_RETURN_CRITICAL
2948static int ssl_tls13_parse_end_of_early_data(mbedtls_ssl_context *ssl,
2949 const unsigned char *buf,
2950 const unsigned char *end)
2951{
Jerry Yu75c9ab72023-12-01 16:41:40 +08002952 /* RFC 8446 section 4.5
2953 *
2954 * struct {} EndOfEarlyData;
2955 */
Jerry Yufbf03992023-12-04 10:00:37 +08002956 if (buf != end) {
2957 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR,
2958 MBEDTLS_ERR_SSL_DECODE_ERROR);
2959 return MBEDTLS_ERR_SSL_DECODE_ERROR;
2960 }
2961 return 0;
Jerry Yud5c34962023-12-01 16:32:31 +08002962}
2963
Jerry Yud5c34962023-12-01 16:32:31 +08002964/*
2965 * RFC 8446 section A.2
2966 *
2967 * | Send ServerHello
2968 * | K_send = handshake
2969 * | Send EncryptedExtensions
2970 * | [Send CertificateRequest]
2971 * Can send | [Send Certificate + CertificateVerify]
2972 * app data | Send Finished
2973 * after --> | K_send = application
2974 * here +--------+--------+
2975 * No 0-RTT | | 0-RTT
2976 * | |
2977 * K_recv = handshake | | K_recv = early data
2978 * [Skip decrypt errors] | +------> WAIT_EOED -+
2979 * | | Recv | | Recv EndOfEarlyData
2980 * | | early data | | K_recv = handshake
2981 * | +------------+ |
2982 * | |
2983 * +> WAIT_FLIGHT2 <--------+
2984 * |
2985 * +--------+--------+
2986 * No auth | | Client auth
2987 * | |
2988 * | v
2989 * | WAIT_CERT
2990 * | Recv | | Recv Certificate
2991 * | empty | v
2992 * | Certificate | WAIT_CV
2993 * | | | Recv
2994 * | v | CertificateVerify
2995 * +-> WAIT_FINISHED <---+
2996 * | Recv Finished
2997 *
2998 * The function handles actions and state changes from 0-RTT to WAIT_FLIGHT2 in
2999 * the above diagram.
3000 */
Jerry Yu87b5ed42022-12-12 13:07:07 +08003001MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yu59d420f2023-12-01 16:30:34 +08003002static int ssl_tls13_process_end_of_early_data(mbedtls_ssl_context *ssl)
Jerry Yu87b5ed42022-12-12 13:07:07 +08003003{
3004 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yud5c34962023-12-01 16:32:31 +08003005
3006 MBEDTLS_SSL_DEBUG_MSG(2, ("=> ssl_tls13_process_end_of_early_data"));
3007
3008 MBEDTLS_SSL_PROC_CHK_NEG(ssl_tls13_end_of_early_data_coordinate(ssl));
3009
Jerry Yu3be85072023-12-04 09:58:54 +08003010 if (ret == SSL_GOT_END_OF_EARLY_DATA) {
Jerry Yud5c34962023-12-01 16:32:31 +08003011 unsigned char *buf;
3012 size_t buf_len;
3013
3014 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_tls13_fetch_handshake_msg(
3015 ssl, MBEDTLS_SSL_HS_END_OF_EARLY_DATA,
3016 &buf, &buf_len));
3017
3018 MBEDTLS_SSL_PROC_CHK(ssl_tls13_parse_end_of_early_data(
3019 ssl, buf, buf + buf_len));
3020
Jerry Yue9655122023-12-01 16:44:40 +08003021 MBEDTLS_SSL_DEBUG_MSG(
3022 1, ("Switch to handshake keys for inbound traffic"
3023 "( K_recv = handshake )"));
3024 mbedtls_ssl_set_inbound_transform(
3025 ssl, ssl->handshake->transform_handshake);
3026
Jerry Yud5c34962023-12-01 16:32:31 +08003027 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_add_hs_msg_to_checksum(
3028 ssl, MBEDTLS_SSL_HS_END_OF_EARLY_DATA,
3029 buf, buf_len));
Jerry Yue9655122023-12-01 16:44:40 +08003030
Jerry Yu3be85072023-12-04 09:58:54 +08003031 ssl_tls13_prepare_for_handshake_second_flight(ssl);
Jerry Yud5c34962023-12-01 16:32:31 +08003032
Jerry Yub55f9eb2023-12-05 10:27:17 +08003033 } else if (ret == SSL_GOT_EARLY_DATA) {
Jerry Yud9ca3542023-12-06 17:23:52 +08003034 ret = MBEDTLS_ERR_SSL_RECEIVED_EARLY_DATA;
3035 goto cleanup;
Jerry Yud5c34962023-12-01 16:32:31 +08003036 } else {
3037 MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
3038 ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
3039 goto cleanup;
3040 }
3041
Jerry Yud5c34962023-12-01 16:32:31 +08003042cleanup:
3043 MBEDTLS_SSL_DEBUG_MSG(2, ("<= ssl_tls13_process_end_of_early_data"));
Jerry Yu87b5ed42022-12-12 13:07:07 +08003044 return ret;
3045}
3046#endif /* MBEDTLS_SSL_EARLY_DATA */
3047
Jerry Yu69dd8d42022-04-16 12:51:26 +08003048/*
Jerry Yud6e253d2022-05-18 13:59:24 +08003049 * Handler for MBEDTLS_SSL_CLIENT_FINISHED
Jerry Yu69dd8d42022-04-16 12:51:26 +08003050 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02003051MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01003052static int ssl_tls13_process_client_finished(mbedtls_ssl_context *ssl)
Jerry Yu69dd8d42022-04-16 12:51:26 +08003053{
Jerry Yud6e253d2022-05-18 13:59:24 +08003054 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
3055
Gilles Peskine449bd832023-01-11 14:50:10 +01003056 ret = mbedtls_ssl_tls13_process_finished_message(ssl);
3057 if (ret != 0) {
3058 return ret;
Jerry Yue3d67cb2022-05-19 15:33:10 +08003059 }
3060
Gilles Peskine449bd832023-01-11 14:50:10 +01003061 ret = mbedtls_ssl_tls13_compute_resumption_master_secret(ssl);
3062 if (ret != 0) {
Xiaokang Qian9f1747b2023-03-30 02:50:04 +00003063 MBEDTLS_SSL_DEBUG_RET(
3064 1, "mbedtls_ssl_tls13_compute_resumption_master_secret", ret);
Gilles Peskine449bd832023-01-11 14:50:10 +01003065 }
3066
3067 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_HANDSHAKE_WRAPUP);
3068 return 0;
Jerry Yu69dd8d42022-04-16 12:51:26 +08003069}
3070
3071/*
Jerry Yud6e253d2022-05-18 13:59:24 +08003072 * Handler for MBEDTLS_SSL_HANDSHAKE_WRAPUP
Jerry Yu69dd8d42022-04-16 12:51:26 +08003073 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02003074MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01003075static int ssl_tls13_handshake_wrapup(mbedtls_ssl_context *ssl)
Jerry Yu69dd8d42022-04-16 12:51:26 +08003076{
Gilles Peskine449bd832023-01-11 14:50:10 +01003077 MBEDTLS_SSL_DEBUG_MSG(2, ("handshake: done"));
Jerry Yu03ed50b2022-04-16 17:13:30 +08003078
Gilles Peskine449bd832023-01-11 14:50:10 +01003079 mbedtls_ssl_tls13_handshake_wrapup(ssl);
Jerry Yue67bef42022-07-07 07:29:42 +00003080
Pengyu Lv3643fdb2023-01-12 16:46:28 +08003081#if defined(MBEDTLS_SSL_SESSION_TICKETS) && \
3082 defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED)
Pengyu Lva1aa31b2022-12-13 13:49:59 +08003083/* TODO: Remove the check of SOME_PSK_ENABLED since SESSION_TICKETS requires
3084 * SOME_PSK_ENABLED to be enabled. Here is just to make CI happy. It is
3085 * expected to be resolved with issue#6395.
3086 */
Pengyu Lvc7af2c42022-12-01 16:33:00 +08003087 /* Sent NewSessionTicket message only when client supports PSK */
Pengyu Lvb2cfafb2023-11-14 13:56:13 +08003088 if (mbedtls_ssl_tls13_is_some_psk_supported(ssl)) {
Xiaokang Qian9f1747b2023-03-30 02:50:04 +00003089 mbedtls_ssl_handshake_set_state(
3090 ssl, MBEDTLS_SSL_TLS1_3_NEW_SESSION_TICKET);
Pengyu Lvc7af2c42022-12-01 16:33:00 +08003091 } else
Jerry Yufca4d572022-07-21 10:37:48 +08003092#endif
Pengyu Lv3643fdb2023-01-12 16:46:28 +08003093 {
3094 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_HANDSHAKE_OVER);
3095 }
Gilles Peskine449bd832023-01-11 14:50:10 +01003096 return 0;
Jerry Yu69dd8d42022-04-16 12:51:26 +08003097}
3098
3099/*
Jerry Yua8d3c502022-10-30 14:51:23 +08003100 * Handler for MBEDTLS_SSL_TLS1_3_NEW_SESSION_TICKET
Jerry Yue67bef42022-07-07 07:29:42 +00003101 */
3102#define SSL_NEW_SESSION_TICKET_SKIP 0
3103#define SSL_NEW_SESSION_TICKET_WRITE 1
3104MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01003105static int ssl_tls13_write_new_session_ticket_coordinate(mbedtls_ssl_context *ssl)
Jerry Yue67bef42022-07-07 07:29:42 +00003106{
3107 /* Check whether the use of session tickets is enabled */
Gilles Peskine449bd832023-01-11 14:50:10 +01003108 if (ssl->conf->f_ticket_write == NULL) {
3109 MBEDTLS_SSL_DEBUG_MSG(2, ("NewSessionTicket: disabled,"
3110 " callback is not set"));
3111 return SSL_NEW_SESSION_TICKET_SKIP;
Jerry Yud0766ec2022-09-22 10:46:57 +08003112 }
Gilles Peskine449bd832023-01-11 14:50:10 +01003113 if (ssl->conf->new_session_tickets_count == 0) {
3114 MBEDTLS_SSL_DEBUG_MSG(2, ("NewSessionTicket: disabled,"
3115 " configured count is zero"));
3116 return SSL_NEW_SESSION_TICKET_SKIP;
Jerry Yud0766ec2022-09-22 10:46:57 +08003117 }
3118
Gilles Peskine449bd832023-01-11 14:50:10 +01003119 if (ssl->handshake->new_session_tickets_count == 0) {
3120 MBEDTLS_SSL_DEBUG_MSG(2, ("NewSessionTicket: all tickets have "
3121 "been sent."));
3122 return SSL_NEW_SESSION_TICKET_SKIP;
Jerry Yue67bef42022-07-07 07:29:42 +00003123 }
3124
Gilles Peskine449bd832023-01-11 14:50:10 +01003125 return SSL_NEW_SESSION_TICKET_WRITE;
Jerry Yue67bef42022-07-07 07:29:42 +00003126}
3127
3128#if defined(MBEDTLS_SSL_SESSION_TICKETS)
3129MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01003130static int ssl_tls13_prepare_new_session_ticket(mbedtls_ssl_context *ssl,
3131 unsigned char *ticket_nonce,
3132 size_t ticket_nonce_size)
Jerry Yue67bef42022-07-07 07:29:42 +00003133{
3134 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
3135 mbedtls_ssl_session *session = ssl->session;
3136 mbedtls_ssl_ciphersuite_t *ciphersuite_info;
3137 psa_algorithm_t psa_hash_alg;
3138 int hash_length;
3139
Gilles Peskine449bd832023-01-11 14:50:10 +01003140 MBEDTLS_SSL_DEBUG_MSG(2, ("=> prepare NewSessionTicket msg"));
Jerry Yue67bef42022-07-07 07:29:42 +00003141
Pengyu Lv9f926952022-11-17 15:22:33 +08003142 /* Set ticket_flags depends on the advertised psk key exchange mode */
Pengyu Lv94a42cc2023-12-06 10:04:17 +08003143 mbedtls_ssl_tls13_session_clear_ticket_flags(
Pengyu Lv9eacb442022-12-09 14:39:19 +08003144 session, MBEDTLS_SSL_TLS1_3_TICKET_FLAGS_MASK);
Pengyu Lve6487fe2022-12-06 09:30:29 +08003145#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED)
Pengyu Lv94a42cc2023-12-06 10:04:17 +08003146 mbedtls_ssl_tls13_session_set_ticket_flags(
Pengyu Lv9eacb442022-12-09 14:39:19 +08003147 session, ssl->handshake->tls13_kex_modes);
Pengyu Lve6487fe2022-12-06 09:30:29 +08003148#endif
Jerry Yu95648b02023-12-06 15:03:34 +08003149
3150#if defined(MBEDTLS_SSL_EARLY_DATA)
3151 if (ssl->conf->early_data_enabled == MBEDTLS_SSL_EARLY_DATA_ENABLED &&
3152 ssl->conf->max_early_data_size > 0) {
Pengyu Lv94a42cc2023-12-06 10:04:17 +08003153 mbedtls_ssl_tls13_session_set_ticket_flags(
Jerry Yu95648b02023-12-06 15:03:34 +08003154 session, MBEDTLS_SSL_TLS1_3_TICKET_ALLOW_EARLY_DATA);
3155 }
3156#endif /* MBEDTLS_SSL_EARLY_DATA */
3157
Pengyu Lv4938a562023-01-16 11:28:49 +08003158 MBEDTLS_SSL_PRINT_TICKET_FLAGS(4, session->ticket_flags);
Pengyu Lv9f926952022-11-17 15:22:33 +08003159
Jerry Yue67bef42022-07-07 07:29:42 +00003160 /* Generate ticket_age_add */
Gilles Peskine449bd832023-01-11 14:50:10 +01003161 if ((ret = ssl->conf->f_rng(ssl->conf->p_rng,
3162 (unsigned char *) &session->ticket_age_add,
3163 sizeof(session->ticket_age_add)) != 0)) {
3164 MBEDTLS_SSL_DEBUG_RET(1, "generate_ticket_age_add", ret);
3165 return ret;
Jerry Yue67bef42022-07-07 07:29:42 +00003166 }
Gilles Peskine449bd832023-01-11 14:50:10 +01003167 MBEDTLS_SSL_DEBUG_MSG(3, ("ticket_age_add: %u",
3168 (unsigned int) session->ticket_age_add));
Jerry Yue67bef42022-07-07 07:29:42 +00003169
3170 /* Generate ticket_nonce */
Gilles Peskine449bd832023-01-11 14:50:10 +01003171 ret = ssl->conf->f_rng(ssl->conf->p_rng, ticket_nonce, ticket_nonce_size);
3172 if (ret != 0) {
3173 MBEDTLS_SSL_DEBUG_RET(1, "generate_ticket_nonce", ret);
3174 return ret;
Jerry Yue67bef42022-07-07 07:29:42 +00003175 }
Gilles Peskine449bd832023-01-11 14:50:10 +01003176 MBEDTLS_SSL_DEBUG_BUF(3, "ticket_nonce:",
3177 ticket_nonce, ticket_nonce_size);
Jerry Yue67bef42022-07-07 07:29:42 +00003178
3179 ciphersuite_info =
Gilles Peskine449bd832023-01-11 14:50:10 +01003180 (mbedtls_ssl_ciphersuite_t *) ssl->handshake->ciphersuite_info;
Dave Rodgman2eab4622023-10-05 13:30:37 +01003181 psa_hash_alg = mbedtls_md_psa_alg_from_type((mbedtls_md_type_t) ciphersuite_info->mac);
Gilles Peskine449bd832023-01-11 14:50:10 +01003182 hash_length = PSA_HASH_LENGTH(psa_hash_alg);
3183 if (hash_length == -1 ||
3184 (size_t) hash_length > sizeof(session->resumption_key)) {
3185 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Jerry Yufca4d572022-07-21 10:37:48 +08003186 }
3187
Jerry Yue67bef42022-07-07 07:29:42 +00003188 /* In this code the psk key length equals the length of the hash */
3189 session->resumption_key_len = hash_length;
3190 session->ciphersuite = ciphersuite_info->id;
3191
3192 /* Compute resumption key
3193 *
3194 * HKDF-Expand-Label( resumption_master_secret,
3195 * "resumption", ticket_nonce, Hash.length )
3196 */
3197 ret = mbedtls_ssl_tls13_hkdf_expand_label(
Gilles Peskine449bd832023-01-11 14:50:10 +01003198 psa_hash_alg,
3199 session->app_secrets.resumption_master_secret,
3200 hash_length,
3201 MBEDTLS_SSL_TLS1_3_LBL_WITH_LEN(resumption),
3202 ticket_nonce,
3203 ticket_nonce_size,
3204 session->resumption_key,
3205 hash_length);
Jerry Yue67bef42022-07-07 07:29:42 +00003206
Gilles Peskine449bd832023-01-11 14:50:10 +01003207 if (ret != 0) {
3208 MBEDTLS_SSL_DEBUG_RET(2,
3209 "Creating the ticket-resumed PSK failed",
3210 ret);
3211 return ret;
Jerry Yue67bef42022-07-07 07:29:42 +00003212 }
Gilles Peskine449bd832023-01-11 14:50:10 +01003213 MBEDTLS_SSL_DEBUG_BUF(3, "Ticket-resumed PSK",
3214 session->resumption_key,
3215 session->resumption_key_len);
Jerry Yue67bef42022-07-07 07:29:42 +00003216
Gilles Peskine449bd832023-01-11 14:50:10 +01003217 MBEDTLS_SSL_DEBUG_BUF(3, "resumption_master_secret",
3218 session->app_secrets.resumption_master_secret,
3219 hash_length);
Jerry Yue67bef42022-07-07 07:29:42 +00003220
Gilles Peskine449bd832023-01-11 14:50:10 +01003221 return 0;
Jerry Yue67bef42022-07-07 07:29:42 +00003222}
3223
3224/* This function creates a NewSessionTicket message in the following format:
3225 *
3226 * struct {
3227 * uint32 ticket_lifetime;
3228 * uint32 ticket_age_add;
3229 * opaque ticket_nonce<0..255>;
3230 * opaque ticket<1..2^16-1>;
3231 * Extension extensions<0..2^16-2>;
3232 * } NewSessionTicket;
3233 *
3234 * The ticket inside the NewSessionTicket message is an encrypted container
3235 * carrying the necessary information so that the server is later able to
3236 * re-start the communication.
3237 *
3238 * The following fields are placed inside the ticket by the
3239 * f_ticket_write() function:
3240 *
Jerry Yu0069abc2023-11-23 21:07:28 +08003241 * - creation time (ticket_creation_time)
3242 * - flags (ticket_flags)
Jerry Yue67bef42022-07-07 07:29:42 +00003243 * - age add (ticket_age_add)
Jerry Yu0069abc2023-11-23 21:07:28 +08003244 * - key (resumption_key)
3245 * - key length (resumption_key_len)
Jerry Yue67bef42022-07-07 07:29:42 +00003246 * - ciphersuite (ciphersuite)
Jerry Yu0069abc2023-11-23 21:07:28 +08003247 * - max_early_data_size (max_early_data_size)
Jerry Yue67bef42022-07-07 07:29:42 +00003248 */
3249MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01003250static int ssl_tls13_write_new_session_ticket_body(mbedtls_ssl_context *ssl,
3251 unsigned char *buf,
3252 unsigned char *end,
3253 size_t *out_len,
3254 unsigned char *ticket_nonce,
3255 size_t ticket_nonce_size)
Jerry Yue67bef42022-07-07 07:29:42 +00003256{
3257 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
3258 unsigned char *p = buf;
3259 mbedtls_ssl_session *session = ssl->session;
3260 size_t ticket_len;
3261 uint32_t ticket_lifetime;
Jerry Yu01da35e2022-12-12 15:09:22 +08003262 unsigned char *p_extensions_len;
Jerry Yue67bef42022-07-07 07:29:42 +00003263
3264 *out_len = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01003265 MBEDTLS_SSL_DEBUG_MSG(2, ("=> write NewSessionTicket msg"));
Jerry Yue67bef42022-07-07 07:29:42 +00003266
3267 /*
3268 * ticket_lifetime 4 bytes
3269 * ticket_age_add 4 bytes
3270 * ticket_nonce 1 + ticket_nonce_size bytes
3271 * ticket >=2 bytes
3272 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003273 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 4 + 4 + 1 + ticket_nonce_size + 2);
Jerry Yue67bef42022-07-07 07:29:42 +00003274
3275 /* Generate ticket and ticket_lifetime */
Ronald Cron3c0072b2023-11-22 10:00:14 +01003276#if defined(MBEDTLS_HAVE_TIME)
3277 session->ticket_creation_time = mbedtls_ms_time();
3278#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01003279 ret = ssl->conf->f_ticket_write(ssl->conf->p_ticket,
3280 session,
3281 p + 9 + ticket_nonce_size + 2,
3282 end,
3283 &ticket_len,
3284 &ticket_lifetime);
3285 if (ret != 0) {
3286 MBEDTLS_SSL_DEBUG_RET(1, "write_ticket", ret);
3287 return ret;
Jerry Yue67bef42022-07-07 07:29:42 +00003288 }
3289 /* RFC 8446 4.6.1
3290 * ticket_lifetime: Indicates the lifetime in seconds as a 32-bit
3291 * unsigned integer in network byte order from the time of ticket
3292 * issuance. Servers MUST NOT use any value greater than
3293 * 604800 seconds (7 days). The value of zero indicates that the
3294 * ticket should be discarded immediately. Clients MUST NOT cache
3295 * tickets for longer than 7 days, regardless of the ticket_lifetime,
3296 * and MAY delete tickets earlier based on local policy. A server
3297 * MAY treat a ticket as valid for a shorter period of time than what
3298 * is stated in the ticket_lifetime.
3299 */
Jerry Yu8e0174a2023-11-10 13:58:16 +08003300 if (ticket_lifetime > MBEDTLS_SSL_TLS1_3_MAX_ALLOWED_TICKET_LIFETIME) {
3301 ticket_lifetime = MBEDTLS_SSL_TLS1_3_MAX_ALLOWED_TICKET_LIFETIME;
Gilles Peskine449bd832023-01-11 14:50:10 +01003302 }
3303 MBEDTLS_PUT_UINT32_BE(ticket_lifetime, p, 0);
3304 MBEDTLS_SSL_DEBUG_MSG(3, ("ticket_lifetime: %u",
3305 (unsigned int) ticket_lifetime));
Jerry Yue67bef42022-07-07 07:29:42 +00003306
3307 /* Write ticket_age_add */
Gilles Peskine449bd832023-01-11 14:50:10 +01003308 MBEDTLS_PUT_UINT32_BE(session->ticket_age_add, p, 4);
3309 MBEDTLS_SSL_DEBUG_MSG(3, ("ticket_age_add: %u",
3310 (unsigned int) session->ticket_age_add));
Jerry Yue67bef42022-07-07 07:29:42 +00003311
3312 /* Write ticket_nonce */
Gilles Peskine449bd832023-01-11 14:50:10 +01003313 p[8] = (unsigned char) ticket_nonce_size;
3314 if (ticket_nonce_size > 0) {
3315 memcpy(p + 9, ticket_nonce, ticket_nonce_size);
Jerry Yue67bef42022-07-07 07:29:42 +00003316 }
3317 p += 9 + ticket_nonce_size;
3318
3319 /* Write ticket */
Gilles Peskine449bd832023-01-11 14:50:10 +01003320 MBEDTLS_PUT_UINT16_BE(ticket_len, p, 0);
Jerry Yue67bef42022-07-07 07:29:42 +00003321 p += 2;
Gilles Peskine449bd832023-01-11 14:50:10 +01003322 MBEDTLS_SSL_DEBUG_BUF(4, "ticket", p, ticket_len);
Jerry Yue67bef42022-07-07 07:29:42 +00003323 p += ticket_len;
3324
3325 /* Ticket Extensions
3326 *
Jerry Yu01da35e2022-12-12 15:09:22 +08003327 * Extension extensions<0..2^16-2>;
Jerry Yue67bef42022-07-07 07:29:42 +00003328 */
Jerry Yuedab6372022-10-31 14:37:31 +08003329 ssl->handshake->sent_extensions = MBEDTLS_SSL_EXT_MASK_NONE;
3330
Gilles Peskine449bd832023-01-11 14:50:10 +01003331 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 2);
Jerry Yu01da35e2022-12-12 15:09:22 +08003332 p_extensions_len = p;
Jerry Yue67bef42022-07-07 07:29:42 +00003333 p += 2;
3334
Jerry Yu01da35e2022-12-12 15:09:22 +08003335#if defined(MBEDTLS_SSL_EARLY_DATA)
Pengyu Lv94a42cc2023-12-06 10:04:17 +08003336 if (mbedtls_ssl_tls13_session_ticket_allow_early_data(session)) {
Jerry Yu95648b02023-12-06 15:03:34 +08003337 size_t output_len;
3338
Jerry Yuf135bac2023-11-23 18:10:51 +08003339 if ((ret = mbedtls_ssl_tls13_write_early_data_ext(
Jerry Yuc59c5862023-12-05 10:40:49 +08003340 ssl, 1, p, end, &output_len)) != 0) {
Jerry Yuf135bac2023-11-23 18:10:51 +08003341 MBEDTLS_SSL_DEBUG_RET(
3342 1, "mbedtls_ssl_tls13_write_early_data_ext", ret);
3343 return ret;
3344 }
3345 p += output_len;
Jerry Yu9e7f9bc2023-11-27 16:52:07 +08003346 } else {
3347 MBEDTLS_SSL_DEBUG_MSG(
3348 4, ("early_data not allowed, "
3349 "skip early_data extension in NewSessionTicket"));
Jerry Yu01da35e2022-12-12 15:09:22 +08003350 }
Jerry Yuf135bac2023-11-23 18:10:51 +08003351
Jerry Yu01da35e2022-12-12 15:09:22 +08003352#endif /* MBEDTLS_SSL_EARLY_DATA */
3353
3354 MBEDTLS_PUT_UINT16_BE(p - p_extensions_len - 2, p_extensions_len, 0);
3355
Jerry Yue67bef42022-07-07 07:29:42 +00003356 *out_len = p - buf;
Gilles Peskine449bd832023-01-11 14:50:10 +01003357 MBEDTLS_SSL_DEBUG_BUF(4, "ticket", buf, *out_len);
3358 MBEDTLS_SSL_DEBUG_MSG(2, ("<= write new session ticket"));
Jerry Yue67bef42022-07-07 07:29:42 +00003359
Jerry Yu7de2ff02022-11-08 21:43:46 +08003360 MBEDTLS_SSL_PRINT_EXTS(
Gilles Peskine449bd832023-01-11 14:50:10 +01003361 3, MBEDTLS_SSL_HS_NEW_SESSION_TICKET, ssl->handshake->sent_extensions);
Jerry Yu4b8f2f72022-10-31 13:31:22 +08003362
Gilles Peskine449bd832023-01-11 14:50:10 +01003363 return 0;
Jerry Yue67bef42022-07-07 07:29:42 +00003364}
3365
3366/*
Jerry Yua8d3c502022-10-30 14:51:23 +08003367 * Handler for MBEDTLS_SSL_TLS1_3_NEW_SESSION_TICKET
Jerry Yue67bef42022-07-07 07:29:42 +00003368 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003369static int ssl_tls13_write_new_session_ticket(mbedtls_ssl_context *ssl)
Jerry Yue67bef42022-07-07 07:29:42 +00003370{
3371 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
3372
Gilles Peskine449bd832023-01-11 14:50:10 +01003373 MBEDTLS_SSL_PROC_CHK_NEG(ssl_tls13_write_new_session_ticket_coordinate(ssl));
Jerry Yue67bef42022-07-07 07:29:42 +00003374
Gilles Peskine449bd832023-01-11 14:50:10 +01003375 if (ret == SSL_NEW_SESSION_TICKET_WRITE) {
Jerry Yue67bef42022-07-07 07:29:42 +00003376 unsigned char ticket_nonce[MBEDTLS_SSL_TLS1_3_TICKET_NONCE_LENGTH];
3377 unsigned char *buf;
3378 size_t buf_len, msg_len;
3379
Gilles Peskine449bd832023-01-11 14:50:10 +01003380 MBEDTLS_SSL_PROC_CHK(ssl_tls13_prepare_new_session_ticket(
3381 ssl, ticket_nonce, sizeof(ticket_nonce)));
Jerry Yue67bef42022-07-07 07:29:42 +00003382
Xiaokang Qian9f1747b2023-03-30 02:50:04 +00003383 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_start_handshake_msg(
3384 ssl, MBEDTLS_SSL_HS_NEW_SESSION_TICKET,
3385 &buf, &buf_len));
Jerry Yue67bef42022-07-07 07:29:42 +00003386
Gilles Peskine449bd832023-01-11 14:50:10 +01003387 MBEDTLS_SSL_PROC_CHK(ssl_tls13_write_new_session_ticket_body(
3388 ssl, buf, buf + buf_len, &msg_len,
3389 ticket_nonce, sizeof(ticket_nonce)));
Jerry Yue67bef42022-07-07 07:29:42 +00003390
Gilles Peskine449bd832023-01-11 14:50:10 +01003391 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_finish_handshake_msg(
3392 ssl, buf_len, msg_len));
Jerry Yufca4d572022-07-21 10:37:48 +08003393
Jerry Yu359e65f2022-09-22 23:47:43 +08003394 /* Limit session tickets count to one when resumption connection.
3395 *
3396 * See document of mbedtls_ssl_conf_new_session_tickets.
3397 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003398 if (ssl->handshake->resume == 1) {
Jerry Yu359e65f2022-09-22 23:47:43 +08003399 ssl->handshake->new_session_tickets_count = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01003400 } else {
Jerry Yu359e65f2022-09-22 23:47:43 +08003401 ssl->handshake->new_session_tickets_count--;
Gilles Peskine449bd832023-01-11 14:50:10 +01003402 }
Jerry Yub7e3fa72022-09-22 11:07:18 +08003403
Jerry Yua8d3c502022-10-30 14:51:23 +08003404 mbedtls_ssl_handshake_set_state(
Gilles Peskine449bd832023-01-11 14:50:10 +01003405 ssl, MBEDTLS_SSL_TLS1_3_NEW_SESSION_TICKET_FLUSH);
3406 } else {
3407 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_HANDSHAKE_OVER);
Jerry Yue67bef42022-07-07 07:29:42 +00003408 }
3409
Jerry Yue67bef42022-07-07 07:29:42 +00003410cleanup:
3411
Gilles Peskine449bd832023-01-11 14:50:10 +01003412 return ret;
Jerry Yue67bef42022-07-07 07:29:42 +00003413}
3414#endif /* MBEDTLS_SSL_SESSION_TICKETS */
3415
3416/*
XiaokangQiane8ff3502022-04-22 02:34:40 +00003417 * TLS 1.3 State Machine -- server side
XiaokangQian7807f9f2022-02-15 10:04:37 +00003418 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003419int mbedtls_ssl_tls13_handshake_server_step(mbedtls_ssl_context *ssl)
Jerry Yub9930e72021-08-06 17:11:51 +08003420{
XiaokangQian08037552022-04-20 07:16:41 +00003421 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
XiaokangQian7807f9f2022-02-15 10:04:37 +00003422
Gilles Peskine449bd832023-01-11 14:50:10 +01003423 if (ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER || ssl->handshake == NULL) {
3424 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
3425 }
XiaokangQian7807f9f2022-02-15 10:04:37 +00003426
Gilles Peskine449bd832023-01-11 14:50:10 +01003427 MBEDTLS_SSL_DEBUG_MSG(2, ("tls13 server state: %s(%d)",
Dave Rodgman2eab4622023-10-05 13:30:37 +01003428 mbedtls_ssl_states_str((mbedtls_ssl_states) ssl->state),
Gilles Peskine449bd832023-01-11 14:50:10 +01003429 ssl->state));
Jerry Yu6e81b272021-09-27 11:16:17 +08003430
Gilles Peskine449bd832023-01-11 14:50:10 +01003431 switch (ssl->state) {
XiaokangQian7807f9f2022-02-15 10:04:37 +00003432 /* start state */
3433 case MBEDTLS_SSL_HELLO_REQUEST:
Gilles Peskine449bd832023-01-11 14:50:10 +01003434 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_CLIENT_HELLO);
XiaokangQian08037552022-04-20 07:16:41 +00003435 ret = 0;
XiaokangQian7807f9f2022-02-15 10:04:37 +00003436 break;
3437
XiaokangQian7807f9f2022-02-15 10:04:37 +00003438 case MBEDTLS_SSL_CLIENT_HELLO:
Gilles Peskine449bd832023-01-11 14:50:10 +01003439 ret = ssl_tls13_process_client_hello(ssl);
3440 if (ret != 0) {
3441 MBEDTLS_SSL_DEBUG_RET(1, "ssl_tls13_process_client_hello", ret);
3442 }
Jerry Yuf41553b2022-05-09 22:20:30 +08003443 break;
XiaokangQian7807f9f2022-02-15 10:04:37 +00003444
Jerry Yuf41553b2022-05-09 22:20:30 +08003445 case MBEDTLS_SSL_HELLO_RETRY_REQUEST:
Gilles Peskine449bd832023-01-11 14:50:10 +01003446 ret = ssl_tls13_write_hello_retry_request(ssl);
3447 if (ret != 0) {
3448 MBEDTLS_SSL_DEBUG_RET(1, "ssl_tls13_write_hello_retry_request", ret);
3449 return ret;
Jerry Yuf41553b2022-05-09 22:20:30 +08003450 }
XiaokangQian7807f9f2022-02-15 10:04:37 +00003451 break;
3452
Jerry Yu5b64ae92022-03-30 17:15:02 +08003453 case MBEDTLS_SSL_SERVER_HELLO:
Gilles Peskine449bd832023-01-11 14:50:10 +01003454 ret = ssl_tls13_write_server_hello(ssl);
Jerry Yu5b64ae92022-03-30 17:15:02 +08003455 break;
3456
Xiaofei Baicba64af2022-02-15 10:00:56 +00003457 case MBEDTLS_SSL_ENCRYPTED_EXTENSIONS:
Gilles Peskine449bd832023-01-11 14:50:10 +01003458 ret = ssl_tls13_write_encrypted_extensions(ssl);
3459 if (ret != 0) {
3460 MBEDTLS_SSL_DEBUG_RET(1, "ssl_tls13_write_encrypted_extensions", ret);
3461 return ret;
Xiaofei Baicba64af2022-02-15 10:00:56 +00003462 }
Jerry Yu48330562022-05-06 21:35:44 +08003463 break;
Jerry Yu6a2cd9e2022-05-05 11:14:19 +08003464
Ronald Cron928cbd32022-10-04 16:14:26 +02003465#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED)
Xiaofei Bai9ca09d42022-02-14 12:57:18 +00003466 case MBEDTLS_SSL_CERTIFICATE_REQUEST:
Gilles Peskine449bd832023-01-11 14:50:10 +01003467 ret = ssl_tls13_write_certificate_request(ssl);
Xiaofei Bai9ca09d42022-02-14 12:57:18 +00003468 break;
Xiaofei Bai9ca09d42022-02-14 12:57:18 +00003469
Jerry Yu83da34e2022-04-16 13:59:52 +08003470 case MBEDTLS_SSL_SERVER_CERTIFICATE:
Gilles Peskine449bd832023-01-11 14:50:10 +01003471 ret = ssl_tls13_write_server_certificate(ssl);
Jerry Yu83da34e2022-04-16 13:59:52 +08003472 break;
3473
3474 case MBEDTLS_SSL_CERTIFICATE_VERIFY:
Gilles Peskine449bd832023-01-11 14:50:10 +01003475 ret = ssl_tls13_write_certificate_verify(ssl);
Jerry Yu83da34e2022-04-16 13:59:52 +08003476 break;
Ronald Cron928cbd32022-10-04 16:14:26 +02003477#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED */
Jerry Yu83da34e2022-04-16 13:59:52 +08003478
Gilles Peskine449bd832023-01-11 14:50:10 +01003479 /*
3480 * Injection of dummy-CCS's for middlebox compatibility
3481 */
Gabor Mezei7b39bf12022-05-24 16:04:14 +02003482#if defined(MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE)
Gabor Mezeif7044ea2022-06-28 16:01:49 +02003483 case MBEDTLS_SSL_SERVER_CCS_AFTER_HELLO_RETRY_REQUEST:
Gilles Peskine449bd832023-01-11 14:50:10 +01003484 ret = mbedtls_ssl_tls13_write_change_cipher_spec(ssl);
3485 if (ret == 0) {
3486 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_CLIENT_HELLO);
3487 }
Gabor Mezei7b39bf12022-05-24 16:04:14 +02003488 break;
3489
3490 case MBEDTLS_SSL_SERVER_CCS_AFTER_SERVER_HELLO:
Ronald Crone273f722024-02-13 18:22:26 +01003491 ret = mbedtls_ssl_tls13_write_change_cipher_spec(ssl);
3492 if (ret != 0) {
3493 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01003494 }
Ronald Cronfe59ff72024-01-24 14:31:50 +01003495 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_ENCRYPTED_EXTENSIONS);
Gabor Mezei7b39bf12022-05-24 16:04:14 +02003496 break;
3497#endif /* MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE */
3498
Jerry Yu69dd8d42022-04-16 12:51:26 +08003499 case MBEDTLS_SSL_SERVER_FINISHED:
Gilles Peskine449bd832023-01-11 14:50:10 +01003500 ret = ssl_tls13_write_server_finished(ssl);
Jerry Yu69dd8d42022-04-16 12:51:26 +08003501 break;
3502
Jerry Yu87b5ed42022-12-12 13:07:07 +08003503#if defined(MBEDTLS_SSL_EARLY_DATA)
3504 case MBEDTLS_SSL_END_OF_EARLY_DATA:
Jerry Yu59d420f2023-12-01 16:30:34 +08003505 ret = ssl_tls13_process_end_of_early_data(ssl);
Jerry Yu87b5ed42022-12-12 13:07:07 +08003506 break;
3507#endif /* MBEDTLS_SSL_EARLY_DATA */
3508
Jerry Yu69dd8d42022-04-16 12:51:26 +08003509 case MBEDTLS_SSL_CLIENT_FINISHED:
Gilles Peskine449bd832023-01-11 14:50:10 +01003510 ret = ssl_tls13_process_client_finished(ssl);
Jerry Yu69dd8d42022-04-16 12:51:26 +08003511 break;
3512
Jerry Yu69dd8d42022-04-16 12:51:26 +08003513 case MBEDTLS_SSL_HANDSHAKE_WRAPUP:
Gilles Peskine449bd832023-01-11 14:50:10 +01003514 ret = ssl_tls13_handshake_wrapup(ssl);
Jerry Yu69dd8d42022-04-16 12:51:26 +08003515 break;
3516
Ronald Cron766c0cd2022-10-18 12:17:11 +02003517#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED)
XiaokangQian6b916b12022-04-25 07:29:34 +00003518 case MBEDTLS_SSL_CLIENT_CERTIFICATE:
Gilles Peskine449bd832023-01-11 14:50:10 +01003519 ret = mbedtls_ssl_tls13_process_certificate(ssl);
3520 if (ret == 0) {
3521 if (ssl->session_negotiate->peer_cert != NULL) {
Ronald Cron209cae92022-06-07 10:30:19 +02003522 mbedtls_ssl_handshake_set_state(
Gilles Peskine449bd832023-01-11 14:50:10 +01003523 ssl, MBEDTLS_SSL_CLIENT_CERTIFICATE_VERIFY);
3524 } else {
3525 MBEDTLS_SSL_DEBUG_MSG(2, ("skip parse certificate verify"));
Ronald Cron209cae92022-06-07 10:30:19 +02003526 mbedtls_ssl_handshake_set_state(
Gilles Peskine449bd832023-01-11 14:50:10 +01003527 ssl, MBEDTLS_SSL_CLIENT_FINISHED);
Ronald Cron19385882022-06-15 16:26:13 +02003528 }
XiaokangQian6b916b12022-04-25 07:29:34 +00003529 }
3530 break;
3531
3532 case MBEDTLS_SSL_CLIENT_CERTIFICATE_VERIFY:
Gilles Peskine449bd832023-01-11 14:50:10 +01003533 ret = mbedtls_ssl_tls13_process_certificate_verify(ssl);
3534 if (ret == 0) {
XiaokangQian6b916b12022-04-25 07:29:34 +00003535 mbedtls_ssl_handshake_set_state(
Gilles Peskine449bd832023-01-11 14:50:10 +01003536 ssl, MBEDTLS_SSL_CLIENT_FINISHED);
XiaokangQian6b916b12022-04-25 07:29:34 +00003537 }
3538 break;
Ronald Cron766c0cd2022-10-18 12:17:11 +02003539#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED */
XiaokangQian6b916b12022-04-25 07:29:34 +00003540
Jerry Yue67bef42022-07-07 07:29:42 +00003541#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Jerry Yua8d3c502022-10-30 14:51:23 +08003542 case MBEDTLS_SSL_TLS1_3_NEW_SESSION_TICKET:
Gilles Peskine449bd832023-01-11 14:50:10 +01003543 ret = ssl_tls13_write_new_session_ticket(ssl);
3544 if (ret != 0) {
3545 MBEDTLS_SSL_DEBUG_RET(1,
3546 "ssl_tls13_write_new_session_ticket ",
3547 ret);
Jerry Yue67bef42022-07-07 07:29:42 +00003548 }
3549 break;
Jerry Yua8d3c502022-10-30 14:51:23 +08003550 case MBEDTLS_SSL_TLS1_3_NEW_SESSION_TICKET_FLUSH:
Jerry Yue67bef42022-07-07 07:29:42 +00003551 /* This state is necessary to do the flush of the New Session
Jerry Yua8d3c502022-10-30 14:51:23 +08003552 * Ticket message written in MBEDTLS_SSL_TLS1_3_NEW_SESSION_TICKET
Jerry Yue67bef42022-07-07 07:29:42 +00003553 * as part of ssl_prepare_handshake_step.
3554 */
3555 ret = 0;
Jerry Yud4e75002022-08-09 13:33:50 +08003556
Gilles Peskine449bd832023-01-11 14:50:10 +01003557 if (ssl->handshake->new_session_tickets_count == 0) {
3558 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_HANDSHAKE_OVER);
3559 } else {
Xiaokang Qian9f1747b2023-03-30 02:50:04 +00003560 mbedtls_ssl_handshake_set_state(
3561 ssl, MBEDTLS_SSL_TLS1_3_NEW_SESSION_TICKET);
Gilles Peskine449bd832023-01-11 14:50:10 +01003562 }
Jerry Yue67bef42022-07-07 07:29:42 +00003563 break;
3564
3565#endif /* MBEDTLS_SSL_SESSION_TICKETS */
3566
XiaokangQian7807f9f2022-02-15 10:04:37 +00003567 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01003568 MBEDTLS_SSL_DEBUG_MSG(1, ("invalid state %d", ssl->state));
3569 return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
XiaokangQian7807f9f2022-02-15 10:04:37 +00003570 }
3571
Gilles Peskine449bd832023-01-11 14:50:10 +01003572 return ret;
Jerry Yub9930e72021-08-06 17:11:51 +08003573}
Jerry Yu3cc4c2a2021-08-06 16:29:08 +08003574
Jerry Yufb4b6472022-01-27 15:03:26 +08003575#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_PROTO_TLS1_3 */