blob: ca5e112cad0ab7b732655a1556a06f853f922e7d [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)
Gilles Peskine449bd832023-01-11 14:50:10 +0100285 if (ssl_tls13_offered_psks_check_identity_match_ticket(
Jerry Yu8d4bbba2022-09-13 14:15:48 +0800286 ssl, identity, identity_len, obfuscated_ticket_age,
Ronald Cronfbae94a2023-12-05 18:15:14 +0100287 session) == 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;
Jerry Yu95699e72022-08-21 19:22:23 +0800304 }
305#endif /* MBEDTLS_SSL_SESSION_TICKETS */
306
Jerry Yu1c105562022-07-10 06:32:38 +0000307 /* Check identity with external configured function */
Gilles Peskine449bd832023-01-11 14:50:10 +0100308 if (ssl->conf->f_psk != NULL) {
309 if (ssl->conf->f_psk(
310 ssl->conf->p_psk, ssl, identity, identity_len) == 0) {
Ronald Cronfbae94a2023-12-05 18:15:14 +0100311 return SSL_TLS1_3_PSK_IDENTITY_MATCH;
Jerry Yu1c105562022-07-10 06:32:38 +0000312 }
Ronald Cronfbae94a2023-12-05 18:15:14 +0100313 return SSL_TLS1_3_PSK_IDENTITY_DOES_NOT_MATCH;
Jerry Yu1c105562022-07-10 06:32:38 +0000314 }
315
Gilles Peskine449bd832023-01-11 14:50:10 +0100316 MBEDTLS_SSL_DEBUG_BUF(5, "identity", identity, identity_len);
Jerry Yu1c105562022-07-10 06:32:38 +0000317 /* Check identity with pre-configured psk */
Gilles Peskine449bd832023-01-11 14:50:10 +0100318 if (ssl->conf->psk_identity != NULL &&
Jerry Yu2f0abc92022-07-22 19:34:48 +0800319 identity_len == ssl->conf->psk_identity_len &&
Gilles Peskine449bd832023-01-11 14:50:10 +0100320 mbedtls_ct_memcmp(ssl->conf->psk_identity,
321 identity, identity_len) == 0) {
Ronald Cron606671e2023-02-17 11:36:33 +0100322 ret = mbedtls_ssl_set_hs_psk(ssl, ssl->conf->psk, ssl->conf->psk_len);
323 if (ret != 0) {
324 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_set_hs_psk", ret);
325 return ret;
326 }
Ronald Cronfbae94a2023-12-05 18:15:14 +0100327 return SSL_TLS1_3_PSK_IDENTITY_MATCH;
Jerry Yu1c105562022-07-10 06:32:38 +0000328 }
329
Ronald Cronfbae94a2023-12-05 18:15:14 +0100330 return SSL_TLS1_3_PSK_IDENTITY_DOES_NOT_MATCH;
Jerry Yu1c105562022-07-10 06:32:38 +0000331}
332
Ronald Cron6e311272023-12-05 17:57:01 +0100333#define SSL_TLS1_3_BINDER_DOES_NOT_MATCH 1
334#define SSL_TLS1_3_BINDER_MATCH 0
Jerry Yu6e74a7e2022-07-20 20:49:32 +0800335MBEDTLS_CHECK_RETURN_CRITICAL
Xiaokang Qian9f1747b2023-03-30 02:50:04 +0000336static int ssl_tls13_offered_psks_check_binder_match(
337 mbedtls_ssl_context *ssl,
338 const unsigned char *binder, size_t binder_len,
339 int psk_type, psa_algorithm_t psk_hash_alg)
Jerry Yu1c105562022-07-10 06:32:38 +0000340{
341 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yu96a2e362022-07-21 15:11:34 +0800342
Jerry Yudaf375a2022-07-20 21:31:43 +0800343 unsigned char transcript[PSA_HASH_MAX_SIZE];
Jerry Yu1c105562022-07-10 06:32:38 +0000344 size_t transcript_len;
Jerry Yu2f0abc92022-07-22 19:34:48 +0800345 unsigned char *psk;
Jerry Yu96a2e362022-07-21 15:11:34 +0800346 size_t psk_len;
Jerry Yudaf375a2022-07-20 21:31:43 +0800347 unsigned char server_computed_binder[PSA_HASH_MAX_SIZE];
Jerry Yu1c105562022-07-10 06:32:38 +0000348
Jerry Yu1c105562022-07-10 06:32:38 +0000349 /* Get current state of handshake transcript. */
Jerry Yu29d9faa2022-08-23 17:52:45 +0800350 ret = mbedtls_ssl_get_handshake_transcript(
Manuel Pégourié-Gonnard2d6d9932023-03-28 11:38:08 +0200351 ssl, mbedtls_md_type_from_psa_alg(psk_hash_alg),
Gilles Peskine449bd832023-01-11 14:50:10 +0100352 transcript, sizeof(transcript), &transcript_len);
353 if (ret != 0) {
354 return ret;
355 }
Jerry Yu1c105562022-07-10 06:32:38 +0000356
Gilles Peskine449bd832023-01-11 14:50:10 +0100357 ret = mbedtls_ssl_tls13_export_handshake_psk(ssl, &psk, &psk_len);
358 if (ret != 0) {
359 return ret;
360 }
Jerry Yu96a2e362022-07-21 15:11:34 +0800361
Gilles Peskine449bd832023-01-11 14:50:10 +0100362 ret = mbedtls_ssl_tls13_create_psk_binder(ssl, psk_hash_alg,
363 psk, psk_len, psk_type,
364 transcript,
365 server_computed_binder);
Jerry Yu96a2e362022-07-21 15:11:34 +0800366#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +0100367 mbedtls_free((void *) psk);
Jerry Yu96a2e362022-07-21 15:11:34 +0800368#endif
Gilles Peskine449bd832023-01-11 14:50:10 +0100369 if (ret != 0) {
370 MBEDTLS_SSL_DEBUG_MSG(1, ("PSK binder calculation failed."));
371 return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
Jerry Yu1c105562022-07-10 06:32:38 +0000372 }
373
Gilles Peskine449bd832023-01-11 14:50:10 +0100374 MBEDTLS_SSL_DEBUG_BUF(3, "psk binder ( computed ): ",
375 server_computed_binder, transcript_len);
376 MBEDTLS_SSL_DEBUG_BUF(3, "psk binder ( received ): ", binder, binder_len);
Jerry Yu1c105562022-07-10 06:32:38 +0000377
Gilles Peskine449bd832023-01-11 14:50:10 +0100378 if (mbedtls_ct_memcmp(server_computed_binder, binder, binder_len) == 0) {
Ronald Cron6e311272023-12-05 17:57:01 +0100379 return SSL_TLS1_3_BINDER_MATCH;
Jerry Yu1c105562022-07-10 06:32:38 +0000380 }
381
Gilles Peskine449bd832023-01-11 14:50:10 +0100382 mbedtls_platform_zeroize(server_computed_binder,
383 sizeof(server_computed_binder));
Ronald Cron6e311272023-12-05 17:57:01 +0100384 return SSL_TLS1_3_BINDER_DOES_NOT_MATCH;
Jerry Yu1c105562022-07-10 06:32:38 +0000385}
Jerry Yu96a2e362022-07-21 15:11:34 +0800386
Jerry Yu5725f1c2022-08-21 17:27:16 +0800387MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yuf35ba382022-08-23 17:58:26 +0800388static int ssl_tls13_select_ciphersuite_for_psk(
Gilles Peskine449bd832023-01-11 14:50:10 +0100389 mbedtls_ssl_context *ssl,
390 const unsigned char *cipher_suites,
391 const unsigned char *cipher_suites_end,
392 uint16_t *selected_ciphersuite,
393 const mbedtls_ssl_ciphersuite_t **selected_ciphersuite_info)
Jerry Yu5725f1c2022-08-21 17:27:16 +0800394{
Jerry Yuf35ba382022-08-23 17:58:26 +0800395 psa_algorithm_t psk_hash_alg = PSA_ALG_SHA_256;
Jerry Yu5725f1c2022-08-21 17:27:16 +0800396
Jerry Yu0baf9072022-08-25 11:21:04 +0800397 *selected_ciphersuite = 0;
398 *selected_ciphersuite_info = NULL;
399
Jerry Yuf35ba382022-08-23 17:58:26 +0800400 /* RFC 8446, page 55.
401 *
402 * For externally established PSKs, the Hash algorithm MUST be set when the
403 * PSK is established or default to SHA-256 if no such algorithm is defined.
404 *
405 */
Jerry Yu5725f1c2022-08-21 17:27:16 +0800406
Jerry Yu5725f1c2022-08-21 17:27:16 +0800407 /*
408 * Search for a matching ciphersuite
409 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100410 for (const unsigned char *p = cipher_suites;
411 p < cipher_suites_end; p += 2) {
Jerry Yu5725f1c2022-08-21 17:27:16 +0800412 uint16_t cipher_suite;
Jerry Yuf35ba382022-08-23 17:58:26 +0800413 const mbedtls_ssl_ciphersuite_t *ciphersuite_info;
Jerry Yu5725f1c2022-08-21 17:27:16 +0800414
Gilles Peskine449bd832023-01-11 14:50:10 +0100415 cipher_suite = MBEDTLS_GET_UINT16_BE(p, 0);
416 ciphersuite_info = ssl_tls13_validate_peer_ciphersuite(ssl,
417 cipher_suite);
418 if (ciphersuite_info == NULL) {
Jerry Yu5725f1c2022-08-21 17:27:16 +0800419 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +0100420 }
Jerry Yu5725f1c2022-08-21 17:27:16 +0800421
Jerry Yu5725f1c2022-08-21 17:27:16 +0800422 /* MAC of selected ciphersuite MUST be same with PSK binder if exist.
423 * Otherwise, client should reject.
424 */
Dave Rodgman2eab4622023-10-05 13:30:37 +0100425 if (psk_hash_alg ==
426 mbedtls_md_psa_alg_from_type((mbedtls_md_type_t) ciphersuite_info->mac)) {
Jerry Yuf35ba382022-08-23 17:58:26 +0800427 *selected_ciphersuite = cipher_suite;
428 *selected_ciphersuite_info = ciphersuite_info;
Gilles Peskine449bd832023-01-11 14:50:10 +0100429 return 0;
Jerry Yuf35ba382022-08-23 17:58:26 +0800430 }
431 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100432 MBEDTLS_SSL_DEBUG_MSG(2, ("No matched ciphersuite"));
433 return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
Jerry Yuf35ba382022-08-23 17:58:26 +0800434}
Jerry Yu5725f1c2022-08-21 17:27:16 +0800435
Jerry Yu82534862022-08-30 10:42:33 +0800436#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Jerry Yuf35ba382022-08-23 17:58:26 +0800437MBEDTLS_CHECK_RETURN_CRITICAL
438static int ssl_tls13_select_ciphersuite_for_resumption(
Gilles Peskine449bd832023-01-11 14:50:10 +0100439 mbedtls_ssl_context *ssl,
440 const unsigned char *cipher_suites,
441 const unsigned char *cipher_suites_end,
442 mbedtls_ssl_session *session,
443 uint16_t *selected_ciphersuite,
444 const mbedtls_ssl_ciphersuite_t **selected_ciphersuite_info)
Jerry Yuf35ba382022-08-23 17:58:26 +0800445{
Jerry Yu82534862022-08-30 10:42:33 +0800446
Jerry Yuf35ba382022-08-23 17:58:26 +0800447 *selected_ciphersuite = 0;
448 *selected_ciphersuite_info = NULL;
Gilles Peskine449bd832023-01-11 14:50:10 +0100449 for (const unsigned char *p = cipher_suites; p < cipher_suites_end; p += 2) {
450 uint16_t cipher_suite = MBEDTLS_GET_UINT16_BE(p, 0);
Jerry Yu82534862022-08-30 10:42:33 +0800451 const mbedtls_ssl_ciphersuite_t *ciphersuite_info;
452
Gilles Peskine449bd832023-01-11 14:50:10 +0100453 if (cipher_suite != session->ciphersuite) {
Jerry Yu82534862022-08-30 10:42:33 +0800454 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +0100455 }
Jerry Yu82534862022-08-30 10:42:33 +0800456
Gilles Peskine449bd832023-01-11 14:50:10 +0100457 ciphersuite_info = ssl_tls13_validate_peer_ciphersuite(ssl,
458 cipher_suite);
459 if (ciphersuite_info == NULL) {
Jerry Yu82534862022-08-30 10:42:33 +0800460 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +0100461 }
Jerry Yu82534862022-08-30 10:42:33 +0800462
Jerry Yu4746b102022-09-13 11:11:48 +0800463 *selected_ciphersuite = cipher_suite;
Jerry Yu82534862022-08-30 10:42:33 +0800464 *selected_ciphersuite_info = ciphersuite_info;
465
Gilles Peskine449bd832023-01-11 14:50:10 +0100466 return 0;
Jerry Yu82534862022-08-30 10:42:33 +0800467 }
468
Gilles Peskine449bd832023-01-11 14:50:10 +0100469 return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
Jerry Yu5725f1c2022-08-21 17:27:16 +0800470}
Jerry Yuf35ba382022-08-23 17:58:26 +0800471
Jerry Yu82534862022-08-30 10:42:33 +0800472MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100473static int ssl_tls13_session_copy_ticket(mbedtls_ssl_session *dst,
474 const mbedtls_ssl_session *src)
Jerry Yu82534862022-08-30 10:42:33 +0800475{
Jerry Yu82534862022-08-30 10:42:33 +0800476 dst->ticket_age_add = src->ticket_age_add;
477 dst->ticket_flags = src->ticket_flags;
478 dst->resumption_key_len = src->resumption_key_len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100479 if (src->resumption_key_len == 0) {
480 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
481 }
482 memcpy(dst->resumption_key, src->resumption_key, src->resumption_key_len);
Jerry Yu4746b102022-09-13 11:11:48 +0800483
Jerry Yu33bf2402022-12-12 16:01:43 +0800484#if defined(MBEDTLS_SSL_EARLY_DATA)
485 dst->max_early_data_size = src->max_early_data_size;
486#endif
487
Gilles Peskine449bd832023-01-11 14:50:10 +0100488 return 0;
Jerry Yu82534862022-08-30 10:42:33 +0800489}
490#endif /* MBEDTLS_SSL_SESSION_TICKETS */
491
Jerry Yu1c105562022-07-10 06:32:38 +0000492/* Parser for pre_shared_key extension in client hello
493 * struct {
494 * opaque identity<1..2^16-1>;
495 * uint32 obfuscated_ticket_age;
496 * } PskIdentity;
497 *
498 * opaque PskBinderEntry<32..255>;
499 *
500 * struct {
501 * PskIdentity identities<7..2^16-1>;
502 * PskBinderEntry binders<33..2^16-1>;
503 * } OfferedPsks;
504 *
505 * struct {
506 * select (Handshake.msg_type) {
507 * case client_hello: OfferedPsks;
508 * ....
509 * };
510 * } PreSharedKeyExtension;
511 */
Jerry Yu6e74a7e2022-07-20 20:49:32 +0800512MBEDTLS_CHECK_RETURN_CRITICAL
Xiaokang Qian9f1747b2023-03-30 02:50:04 +0000513static int ssl_tls13_parse_pre_shared_key_ext(
514 mbedtls_ssl_context *ssl,
515 const unsigned char *pre_shared_key_ext,
516 const unsigned char *pre_shared_key_ext_end,
517 const unsigned char *ciphersuites,
518 const unsigned char *ciphersuites_end)
Jerry Yu1c105562022-07-10 06:32:38 +0000519{
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +0100520 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yu29d9faa2022-08-23 17:52:45 +0800521 const unsigned char *identities = pre_shared_key_ext;
Jerry Yu568ec252022-07-22 21:27:34 +0800522 const unsigned char *p_identity_len;
523 size_t identities_len;
Jerry Yu1c105562022-07-10 06:32:38 +0000524 const unsigned char *identities_end;
Jerry Yu568ec252022-07-22 21:27:34 +0800525 const unsigned char *binders;
526 const unsigned char *p_binder_len;
527 size_t binders_len;
Jerry Yu1c105562022-07-10 06:32:38 +0000528 const unsigned char *binders_end;
Jerry Yu96a2e362022-07-21 15:11:34 +0800529 int matched_identity = -1;
530 int identity_id = -1;
Jerry Yu1c105562022-07-10 06:32:38 +0000531
Gilles Peskine449bd832023-01-11 14:50:10 +0100532 MBEDTLS_SSL_DEBUG_BUF(3, "pre_shared_key extension",
533 pre_shared_key_ext,
534 pre_shared_key_ext_end - pre_shared_key_ext);
Jerry Yu1c105562022-07-10 06:32:38 +0000535
Jerry Yu96a2e362022-07-21 15:11:34 +0800536 /* identities_len 2 bytes
537 * identities_data >= 7 bytes
538 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100539 MBEDTLS_SSL_CHK_BUF_READ_PTR(identities, pre_shared_key_ext_end, 7 + 2);
540 identities_len = MBEDTLS_GET_UINT16_BE(identities, 0);
Jerry Yu568ec252022-07-22 21:27:34 +0800541 p_identity_len = identities + 2;
Gilles Peskine449bd832023-01-11 14:50:10 +0100542 MBEDTLS_SSL_CHK_BUF_READ_PTR(p_identity_len, pre_shared_key_ext_end,
543 identities_len);
Jerry Yu568ec252022-07-22 21:27:34 +0800544 identities_end = p_identity_len + identities_len;
Jerry Yu1c105562022-07-10 06:32:38 +0000545
Jerry Yu96a2e362022-07-21 15:11:34 +0800546 /* binders_len 2 bytes
547 * binders >= 33 bytes
548 */
Jerry Yu568ec252022-07-22 21:27:34 +0800549 binders = identities_end;
Gilles Peskine449bd832023-01-11 14:50:10 +0100550 MBEDTLS_SSL_CHK_BUF_READ_PTR(binders, pre_shared_key_ext_end, 33 + 2);
551 binders_len = MBEDTLS_GET_UINT16_BE(binders, 0);
Jerry Yu568ec252022-07-22 21:27:34 +0800552 p_binder_len = binders + 2;
Gilles Peskine449bd832023-01-11 14:50:10 +0100553 MBEDTLS_SSL_CHK_BUF_READ_PTR(p_binder_len, pre_shared_key_ext_end, binders_len);
Jerry Yu568ec252022-07-22 21:27:34 +0800554 binders_end = p_binder_len + binders_len;
Jerry Yu1c105562022-07-10 06:32:38 +0000555
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +0100556 ret = ssl->handshake->update_checksum(ssl, pre_shared_key_ext,
557 identities_end - pre_shared_key_ext);
558 if (0 != ret) {
559 MBEDTLS_SSL_DEBUG_RET(1, ("update_checksum"), ret);
560 return ret;
561 }
Jerry Yu96a2e362022-07-21 15:11:34 +0800562
Gilles Peskine449bd832023-01-11 14:50:10 +0100563 while (p_identity_len < identities_end && p_binder_len < binders_end) {
Jerry Yu1c105562022-07-10 06:32:38 +0000564 const unsigned char *identity;
Jerry Yu568ec252022-07-22 21:27:34 +0800565 size_t identity_len;
Jerry Yu82534862022-08-30 10:42:33 +0800566 uint32_t obfuscated_ticket_age;
Jerry Yu96a2e362022-07-21 15:11:34 +0800567 const unsigned char *binder;
Jerry Yu568ec252022-07-22 21:27:34 +0800568 size_t binder_len;
Jerry Yu5725f1c2022-08-21 17:27:16 +0800569 int psk_type;
570 uint16_t cipher_suite;
Jerry Yu29d9faa2022-08-23 17:52:45 +0800571 const mbedtls_ssl_ciphersuite_t *ciphersuite_info;
Jerry Yu82534862022-08-30 10:42:33 +0800572#if defined(MBEDTLS_SSL_SESSION_TICKETS)
573 mbedtls_ssl_session session;
Gilles Peskine449bd832023-01-11 14:50:10 +0100574 mbedtls_ssl_session_init(&session);
Jerry Yu82534862022-08-30 10:42:33 +0800575#endif
Jerry Yubb852022022-07-20 21:10:44 +0800576
Gilles Peskine449bd832023-01-11 14:50:10 +0100577 MBEDTLS_SSL_CHK_BUF_READ_PTR(p_identity_len, identities_end, 2 + 1 + 4);
578 identity_len = MBEDTLS_GET_UINT16_BE(p_identity_len, 0);
Jerry Yu568ec252022-07-22 21:27:34 +0800579 identity = p_identity_len + 2;
Gilles Peskine449bd832023-01-11 14:50:10 +0100580 MBEDTLS_SSL_CHK_BUF_READ_PTR(identity, identities_end, identity_len + 4);
581 obfuscated_ticket_age = MBEDTLS_GET_UINT32_BE(identity, identity_len);
Jerry Yu568ec252022-07-22 21:27:34 +0800582 p_identity_len += identity_len + 6;
Jerry Yu1c105562022-07-10 06:32:38 +0000583
Gilles Peskine449bd832023-01-11 14:50:10 +0100584 MBEDTLS_SSL_CHK_BUF_READ_PTR(p_binder_len, binders_end, 1 + 32);
Jerry Yu568ec252022-07-22 21:27:34 +0800585 binder_len = *p_binder_len;
586 binder = p_binder_len + 1;
Gilles Peskine449bd832023-01-11 14:50:10 +0100587 MBEDTLS_SSL_CHK_BUF_READ_PTR(binder, binders_end, binder_len);
Jerry Yu568ec252022-07-22 21:27:34 +0800588 p_binder_len += binder_len + 1;
Jerry Yu96a2e362022-07-21 15:11:34 +0800589
Jerry Yu96a2e362022-07-21 15:11:34 +0800590 identity_id++;
Gilles Peskine449bd832023-01-11 14:50:10 +0100591 if (matched_identity != -1) {
Jerry Yu1c105562022-07-10 06:32:38 +0000592 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +0100593 }
Jerry Yu1c105562022-07-10 06:32:38 +0000594
Jerry Yu96a2e362022-07-21 15:11:34 +0800595 ret = ssl_tls13_offered_psks_check_identity_match(
Gilles Peskine449bd832023-01-11 14:50:10 +0100596 ssl, identity, identity_len, obfuscated_ticket_age,
597 &psk_type, &session);
Ronald Cronfbae94a2023-12-05 18:15:14 +0100598 if (ret != SSL_TLS1_3_PSK_IDENTITY_MATCH) {
Jerry Yu96a2e362022-07-21 15:11:34 +0800599 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +0100600 }
Jerry Yu96a2e362022-07-21 15:11:34 +0800601
Gilles Peskine449bd832023-01-11 14:50:10 +0100602 MBEDTLS_SSL_DEBUG_MSG(4, ("found matched identity"));
603 switch (psk_type) {
Jerry Yu0baf9072022-08-25 11:21:04 +0800604 case MBEDTLS_SSL_TLS1_3_PSK_EXTERNAL:
605 ret = ssl_tls13_select_ciphersuite_for_psk(
Gilles Peskine449bd832023-01-11 14:50:10 +0100606 ssl, ciphersuites, ciphersuites_end,
607 &cipher_suite, &ciphersuite_info);
Jerry Yu0baf9072022-08-25 11:21:04 +0800608 break;
609 case MBEDTLS_SSL_TLS1_3_PSK_RESUMPTION:
Jerry Yu82534862022-08-30 10:42:33 +0800610#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Jerry Yu0baf9072022-08-25 11:21:04 +0800611 ret = ssl_tls13_select_ciphersuite_for_resumption(
Gilles Peskine449bd832023-01-11 14:50:10 +0100612 ssl, ciphersuites, ciphersuites_end, &session,
613 &cipher_suite, &ciphersuite_info);
614 if (ret != 0) {
615 mbedtls_ssl_session_free(&session);
616 }
Jerry Yu82534862022-08-30 10:42:33 +0800617#else
618 ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
619#endif
Jerry Yu0baf9072022-08-25 11:21:04 +0800620 break;
621 default:
Gilles Peskine449bd832023-01-11 14:50:10 +0100622 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Jerry Yu0baf9072022-08-25 11:21:04 +0800623 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100624 if (ret != 0) {
Jerry Yuf35ba382022-08-23 17:58:26 +0800625 /* See below, no cipher_suite available, abort handshake */
626 MBEDTLS_SSL_PEND_FATAL_ALERT(
627 MBEDTLS_SSL_ALERT_MSG_DECRYPT_ERROR,
Gilles Peskine449bd832023-01-11 14:50:10 +0100628 MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE);
Jerry Yuf35ba382022-08-23 17:58:26 +0800629 MBEDTLS_SSL_DEBUG_RET(
Gilles Peskine449bd832023-01-11 14:50:10 +0100630 2, "ssl_tls13_select_ciphersuite", ret);
631 return ret;
Jerry Yu5725f1c2022-08-21 17:27:16 +0800632 }
633
Jerry Yu96a2e362022-07-21 15:11:34 +0800634 ret = ssl_tls13_offered_psks_check_binder_match(
Gilles Peskine449bd832023-01-11 14:50:10 +0100635 ssl, binder, binder_len, psk_type,
Dave Rodgman2eab4622023-10-05 13:30:37 +0100636 mbedtls_md_psa_alg_from_type((mbedtls_md_type_t) ciphersuite_info->mac));
Ronald Cron6e311272023-12-05 17:57:01 +0100637 if (ret != SSL_TLS1_3_BINDER_MATCH) {
Jerry Yuc5a23a02022-08-25 10:51:44 +0800638 /* For security reasons, the handshake should be aborted when we
639 * fail to validate a binder value. See RFC 8446 section 4.2.11.2
640 * and appendix E.6. */
Jerry Yu82534862022-08-30 10:42:33 +0800641#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Gilles Peskine449bd832023-01-11 14:50:10 +0100642 mbedtls_ssl_session_free(&session);
Jerry Yu82534862022-08-30 10:42:33 +0800643#endif
Gilles Peskine449bd832023-01-11 14:50:10 +0100644 MBEDTLS_SSL_DEBUG_MSG(3, ("Invalid binder."));
Xiaokang Qian9f1747b2023-03-30 02:50:04 +0000645 MBEDTLS_SSL_DEBUG_RET(
646 1, "ssl_tls13_offered_psks_check_binder_match", ret);
Jerry Yu96a2e362022-07-21 15:11:34 +0800647 MBEDTLS_SSL_PEND_FATAL_ALERT(
648 MBEDTLS_SSL_ALERT_MSG_DECRYPT_ERROR,
Gilles Peskine449bd832023-01-11 14:50:10 +0100649 MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE);
650 return ret;
Jerry Yu96a2e362022-07-21 15:11:34 +0800651 }
Jerry Yu96a2e362022-07-21 15:11:34 +0800652
653 matched_identity = identity_id;
Jerry Yu5725f1c2022-08-21 17:27:16 +0800654
655 /* Update handshake parameters */
Jerry Yue5834fd2022-08-29 20:16:09 +0800656 ssl->handshake->ciphersuite_info = ciphersuite_info;
Jerry Yu4746b102022-09-13 11:11:48 +0800657 ssl->session_negotiate->ciphersuite = cipher_suite;
Gilles Peskine449bd832023-01-11 14:50:10 +0100658 MBEDTLS_SSL_DEBUG_MSG(2, ("overwrite ciphersuite: %04x - %s",
659 cipher_suite, ciphersuite_info->name));
Jerry Yu82534862022-08-30 10:42:33 +0800660#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Gilles Peskine449bd832023-01-11 14:50:10 +0100661 if (psk_type == MBEDTLS_SSL_TLS1_3_PSK_RESUMPTION) {
662 ret = ssl_tls13_session_copy_ticket(ssl->session_negotiate,
663 &session);
664 mbedtls_ssl_session_free(&session);
665 if (ret != 0) {
666 return ret;
667 }
Jerry Yu82534862022-08-30 10:42:33 +0800668 }
669#endif /* MBEDTLS_SSL_SESSION_TICKETS */
Jerry Yu1c105562022-07-10 06:32:38 +0000670 }
671
Gilles Peskine449bd832023-01-11 14:50:10 +0100672 if (p_identity_len != identities_end || p_binder_len != binders_end) {
673 MBEDTLS_SSL_DEBUG_MSG(3, ("pre_shared_key extension decode error"));
674 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR,
675 MBEDTLS_ERR_SSL_DECODE_ERROR);
676 return MBEDTLS_ERR_SSL_DECODE_ERROR;
Jerry Yu1c105562022-07-10 06:32:38 +0000677 }
678
Jerry Yu6f1db3f2022-07-22 23:05:59 +0800679 /* Update the handshake transcript with the binder list. */
Xiaokang Qian9f1747b2023-03-30 02:50:04 +0000680 ret = ssl->handshake->update_checksum(
681 ssl, identities_end, (size_t) (binders_end - identities_end));
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +0100682 if (0 != ret) {
683 MBEDTLS_SSL_DEBUG_RET(1, ("update_checksum"), ret);
684 return ret;
685 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100686 if (matched_identity == -1) {
687 MBEDTLS_SSL_DEBUG_MSG(3, ("No matched PSK or ticket."));
688 return MBEDTLS_ERR_SSL_UNKNOWN_IDENTITY;
Jerry Yu1c105562022-07-10 06:32:38 +0000689 }
690
Gilles Peskine449bd832023-01-11 14:50:10 +0100691 ssl->handshake->selected_identity = (uint16_t) matched_identity;
692 MBEDTLS_SSL_DEBUG_MSG(3, ("Pre shared key found"));
Jerry Yu1c105562022-07-10 06:32:38 +0000693
Gilles Peskine449bd832023-01-11 14:50:10 +0100694 return 0;
Jerry Yu1c105562022-07-10 06:32:38 +0000695}
Jerry Yu032b15ce2022-07-11 06:10:03 +0000696
697/*
698 * struct {
699 * select ( Handshake.msg_type ) {
700 * ....
701 * case server_hello:
702 * uint16 selected_identity;
703 * }
704 * } PreSharedKeyExtension;
705 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100706static int ssl_tls13_write_server_pre_shared_key_ext(mbedtls_ssl_context *ssl,
707 unsigned char *buf,
708 unsigned char *end,
709 size_t *olen)
Jerry Yu032b15ce2022-07-11 06:10:03 +0000710{
Gilles Peskine449bd832023-01-11 14:50:10 +0100711 unsigned char *p = (unsigned char *) buf;
Jerry Yu032b15ce2022-07-11 06:10:03 +0000712
713 *olen = 0;
714
David Horstmann21b89762022-10-06 18:34:28 +0100715 int not_using_psk = 0;
Jerry Yu032b15ce2022-07-11 06:10:03 +0000716#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +0100717 not_using_psk = (mbedtls_svc_key_id_is_null(ssl->handshake->psk_opaque));
Jerry Yu032b15ce2022-07-11 06:10:03 +0000718#else
Gilles Peskine449bd832023-01-11 14:50:10 +0100719 not_using_psk = (ssl->handshake->psk == NULL);
Jerry Yu032b15ce2022-07-11 06:10:03 +0000720#endif
Gilles Peskine449bd832023-01-11 14:50:10 +0100721 if (not_using_psk) {
Jerry Yu032b15ce2022-07-11 06:10:03 +0000722 /* We shouldn't have called this extension writer unless we've
723 * chosen to use a PSK. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100724 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Jerry Yu032b15ce2022-07-11 06:10:03 +0000725 }
726
Gilles Peskine449bd832023-01-11 14:50:10 +0100727 MBEDTLS_SSL_DEBUG_MSG(3, ("server hello, adding pre_shared_key extension"));
728 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 6);
Jerry Yu032b15ce2022-07-11 06:10:03 +0000729
Gilles Peskine449bd832023-01-11 14:50:10 +0100730 MBEDTLS_PUT_UINT16_BE(MBEDTLS_TLS_EXT_PRE_SHARED_KEY, p, 0);
731 MBEDTLS_PUT_UINT16_BE(2, p, 2);
Jerry Yu032b15ce2022-07-11 06:10:03 +0000732
Gilles Peskine449bd832023-01-11 14:50:10 +0100733 MBEDTLS_PUT_UINT16_BE(ssl->handshake->selected_identity, p, 4);
Jerry Yu032b15ce2022-07-11 06:10:03 +0000734
735 *olen = 6;
736
Gilles Peskine449bd832023-01-11 14:50:10 +0100737 MBEDTLS_SSL_DEBUG_MSG(4, ("sent selected_identity: %u",
738 ssl->handshake->selected_identity));
Jerry Yu032b15ce2022-07-11 06:10:03 +0000739
Gilles Peskine449bd832023-01-11 14:50:10 +0100740 mbedtls_ssl_tls13_set_hs_sent_ext_mask(ssl, MBEDTLS_TLS_EXT_PRE_SHARED_KEY);
Jerry Yub95dd362022-11-08 21:19:34 +0800741
Gilles Peskine449bd832023-01-11 14:50:10 +0100742 return 0;
Jerry Yu032b15ce2022-07-11 06:10:03 +0000743}
744
Ronald Cron41a443a2022-10-04 16:38:25 +0200745#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED */
Jerry Yue19e3b92022-07-08 12:04:51 +0000746
XiaokangQian7807f9f2022-02-15 10:04:37 +0000747/* From RFC 8446:
748 * struct {
XiaokangQiancfd925f2022-04-14 07:10:37 +0000749 * ProtocolVersion versions<2..254>;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000750 * } SupportedVersions;
751 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200752MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100753static int ssl_tls13_parse_supported_versions_ext(mbedtls_ssl_context *ssl,
754 const unsigned char *buf,
755 const unsigned char *end)
XiaokangQian7807f9f2022-02-15 10:04:37 +0000756{
XiaokangQian7807f9f2022-02-15 10:04:37 +0000757 const unsigned char *p = buf;
XiaokangQian8f9dfe42022-04-15 02:52:39 +0000758 size_t versions_len;
XiaokangQian4080a7f2022-04-11 09:55:18 +0000759 const unsigned char *versions_end;
XiaokangQian0a1b54e2022-04-21 03:01:38 +0000760 uint16_t tls_version;
Ronald Cron5af4c7f2023-03-07 20:46:59 +0100761 int found_supported_version = 0;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000762
Gilles Peskine449bd832023-01-11 14:50:10 +0100763 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, 1);
XiaokangQian4080a7f2022-04-11 09:55:18 +0000764 versions_len = p[0];
XiaokangQian7807f9f2022-02-15 10:04:37 +0000765 p += 1;
766
Gilles Peskine449bd832023-01-11 14:50:10 +0100767 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, versions_len);
XiaokangQian4080a7f2022-04-11 09:55:18 +0000768 versions_end = p + versions_len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100769 while (p < versions_end) {
770 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, versions_end, 2);
771 tls_version = mbedtls_ssl_read_version(p, ssl->conf->transport);
XiaokangQianb67384d2022-04-19 00:02:38 +0000772 p += 2;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000773
Ronald Cron3bd2b022023-04-03 16:45:39 +0200774 if (MBEDTLS_SSL_VERSION_TLS1_3 == tls_version) {
Ronald Cron5af4c7f2023-03-07 20:46:59 +0100775 found_supported_version = 1;
776 break;
777 }
778
Ronald Cron3bd2b022023-04-03 16:45:39 +0200779 if ((MBEDTLS_SSL_VERSION_TLS1_2 == tls_version) &&
780 mbedtls_ssl_conf_is_tls12_enabled(ssl->conf)) {
Ronald Cron5af4c7f2023-03-07 20:46:59 +0100781 found_supported_version = 1;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000782 break;
783 }
XiaokangQian7807f9f2022-02-15 10:04:37 +0000784 }
785
Ronald Cron5af4c7f2023-03-07 20:46:59 +0100786 if (!found_supported_version) {
787 MBEDTLS_SSL_DEBUG_MSG(1, ("No supported version found."));
XiaokangQian7807f9f2022-02-15 10:04:37 +0000788
Gilles Peskine449bd832023-01-11 14:50:10 +0100789 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_PROTOCOL_VERSION,
790 MBEDTLS_ERR_SSL_BAD_PROTOCOL_VERSION);
791 return MBEDTLS_ERR_SSL_BAD_PROTOCOL_VERSION;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000792 }
793
Ronald Cron5af4c7f2023-03-07 20:46:59 +0100794 MBEDTLS_SSL_DEBUG_MSG(1, ("Negotiated version: [%04x]",
Gilles Peskine449bd832023-01-11 14:50:10 +0100795 (unsigned int) tls_version));
XiaokangQian7807f9f2022-02-15 10:04:37 +0000796
Ronald Cron5af4c7f2023-03-07 20:46:59 +0100797 return (int) tls_version;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000798}
799
Przemek Stekiel8c0a9532023-06-15 16:48:19 +0200800#if defined(PSA_WANT_ALG_ECDH) || defined(PSA_WANT_ALG_FFDH)
XiaokangQiane8ff3502022-04-22 02:34:40 +0000801/*
XiaokangQian7807f9f2022-02-15 10:04:37 +0000802 *
803 * From RFC 8446:
804 * enum {
805 * ... (0xFFFF)
806 * } NamedGroup;
807 * struct {
808 * NamedGroup named_group_list<2..2^16-1>;
809 * } NamedGroupList;
810 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200811MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100812static int ssl_tls13_parse_supported_groups_ext(mbedtls_ssl_context *ssl,
813 const unsigned char *buf,
814 const unsigned char *end)
XiaokangQian7807f9f2022-02-15 10:04:37 +0000815{
XiaokangQian7807f9f2022-02-15 10:04:37 +0000816 const unsigned char *p = buf;
XiaokangQian84823772022-04-19 07:57:30 +0000817 size_t named_group_list_len;
XiaokangQian8f9dfe42022-04-15 02:52:39 +0000818 const unsigned char *named_group_list_end;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000819
Gilles Peskine449bd832023-01-11 14:50:10 +0100820 MBEDTLS_SSL_DEBUG_BUF(3, "supported_groups extension", p, end - buf);
821 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, 2);
822 named_group_list_len = MBEDTLS_GET_UINT16_BE(p, 0);
XiaokangQian7807f9f2022-02-15 10:04:37 +0000823 p += 2;
Gilles Peskine449bd832023-01-11 14:50:10 +0100824 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, named_group_list_len);
XiaokangQian8f9dfe42022-04-15 02:52:39 +0000825 named_group_list_end = p + named_group_list_len;
XiaokangQian4e8cd7b2022-04-21 09:48:09 +0000826 ssl->handshake->hrr_selected_group = 0;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000827
Gilles Peskine449bd832023-01-11 14:50:10 +0100828 while (p < named_group_list_end) {
XiaokangQian08037552022-04-20 07:16:41 +0000829 uint16_t named_group;
Gilles Peskine449bd832023-01-11 14:50:10 +0100830 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, named_group_list_end, 2);
831 named_group = MBEDTLS_GET_UINT16_BE(p, 0);
XiaokangQian08037552022-04-20 07:16:41 +0000832 p += 2;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000833
Gilles Peskine449bd832023-01-11 14:50:10 +0100834 MBEDTLS_SSL_DEBUG_MSG(2,
835 ("got named group: %s(%04x)",
836 mbedtls_ssl_named_group_to_str(named_group),
837 named_group));
XiaokangQian08037552022-04-20 07:16:41 +0000838
Gilles Peskine449bd832023-01-11 14:50:10 +0100839 if (!mbedtls_ssl_named_group_is_offered(ssl, named_group) ||
840 !mbedtls_ssl_named_group_is_supported(named_group) ||
841 ssl->handshake->hrr_selected_group != 0) {
XiaokangQian08037552022-04-20 07:16:41 +0000842 continue;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000843 }
844
Gilles Peskine449bd832023-01-11 14:50:10 +0100845 MBEDTLS_SSL_DEBUG_MSG(2,
846 ("add named group %s(%04x) into received list.",
847 mbedtls_ssl_named_group_to_str(named_group),
848 named_group));
Jerry Yuc1be19f2022-04-23 16:11:39 +0800849
XiaokangQian4e8cd7b2022-04-21 09:48:09 +0000850 ssl->handshake->hrr_selected_group = named_group;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000851 }
852
Gilles Peskine449bd832023-01-11 14:50:10 +0100853 return 0;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000854
855}
Przemek Stekiel8c0a9532023-06-15 16:48:19 +0200856#endif /* PSA_WANT_ALG_ECDH || PSA_WANT_ALG_FFDH */
XiaokangQian7807f9f2022-02-15 10:04:37 +0000857
XiaokangQian08037552022-04-20 07:16:41 +0000858#define SSL_TLS1_3_PARSE_KEY_SHARES_EXT_NO_MATCH 1
859
Valerio Settic9ae8622023-07-25 11:23:50 +0200860#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_EPHEMERAL_ENABLED)
XiaokangQian7807f9f2022-02-15 10:04:37 +0000861/*
862 * ssl_tls13_parse_key_shares_ext() verifies whether the information in the
Xiaokang Qian9f1747b2023-03-30 02:50:04 +0000863 * extension is correct and stores the first acceptable key share and its
864 * associated group.
XiaokangQian7807f9f2022-02-15 10:04:37 +0000865 *
866 * Possible return values are:
867 * - 0: Successful processing of the client provided key share extension.
Xiaokang Qian9f1747b2023-03-30 02:50:04 +0000868 * - SSL_TLS1_3_PARSE_KEY_SHARES_EXT_NO_MATCH: The key shares provided by
869 * the client does not match a group supported by the server. A
870 * HelloRetryRequest will be needed.
XiaokangQiane8ff3502022-04-22 02:34:40 +0000871 * - A negative value for fatal errors.
Jerry Yuc1be19f2022-04-23 16:11:39 +0800872 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200873MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100874static int ssl_tls13_parse_key_shares_ext(mbedtls_ssl_context *ssl,
875 const unsigned char *buf,
876 const unsigned char *end)
XiaokangQian7807f9f2022-02-15 10:04:37 +0000877{
XiaokangQianb67384d2022-04-19 00:02:38 +0000878 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000879 unsigned char const *p = buf;
XiaokangQian8f9dfe42022-04-15 02:52:39 +0000880 unsigned char const *client_shares_end;
Jerry Yuc1be19f2022-04-23 16:11:39 +0800881 size_t client_shares_len;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000882
883 /* From RFC 8446:
884 *
885 * struct {
886 * KeyShareEntry client_shares<0..2^16-1>;
887 * } KeyShareClientHello;
888 *
889 */
890
Gilles Peskine449bd832023-01-11 14:50:10 +0100891 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, 2);
892 client_shares_len = MBEDTLS_GET_UINT16_BE(p, 0);
XiaokangQian7807f9f2022-02-15 10:04:37 +0000893 p += 2;
Gilles Peskine449bd832023-01-11 14:50:10 +0100894 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, client_shares_len);
XiaokangQian7807f9f2022-02-15 10:04:37 +0000895
896 ssl->handshake->offered_group_id = 0;
XiaokangQian8f9dfe42022-04-15 02:52:39 +0000897 client_shares_end = p + client_shares_len;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000898
899 /* We try to find a suitable key share entry and copy it to the
900 * handshake context. Later, we have to find out whether we can do
901 * something with the provided key share or whether we have to
XiaokangQianc5763b52022-04-02 03:34:37 +0000902 * dismiss it and send a HelloRetryRequest message.
903 */
XiaokangQian7807f9f2022-02-15 10:04:37 +0000904
Gilles Peskine449bd832023-01-11 14:50:10 +0100905 while (p < client_shares_end) {
XiaokangQian9b5d04b2022-04-10 10:20:43 +0000906 uint16_t group;
Jerry Yuc1be19f2022-04-23 16:11:39 +0800907 size_t key_exchange_len;
Jerry Yu086edc22022-05-05 10:50:38 +0800908 const unsigned char *key_exchange;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000909
910 /*
911 * struct {
912 * NamedGroup group;
913 * opaque key_exchange<1..2^16-1>;
914 * } KeyShareEntry;
915 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100916 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, client_shares_end, 4);
917 group = MBEDTLS_GET_UINT16_BE(p, 0);
918 key_exchange_len = MBEDTLS_GET_UINT16_BE(p, 2);
Jerry Yuc1be19f2022-04-23 16:11:39 +0800919 p += 4;
Jerry Yu086edc22022-05-05 10:50:38 +0800920 key_exchange = p;
Gilles Peskine449bd832023-01-11 14:50:10 +0100921 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, client_shares_end, key_exchange_len);
Jerry Yu086edc22022-05-05 10:50:38 +0800922 p += key_exchange_len;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000923
924 /* Continue parsing even if we have already found a match,
XiaokangQianc5763b52022-04-02 03:34:37 +0000925 * for input validation purposes.
926 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100927 if (!mbedtls_ssl_named_group_is_offered(ssl, group) ||
928 !mbedtls_ssl_named_group_is_supported(group) ||
929 ssl->handshake->offered_group_id != 0) {
XiaokangQian060d8672022-04-21 09:24:56 +0000930 continue;
931 }
XiaokangQian060d8672022-04-21 09:24:56 +0000932
XiaokangQian7807f9f2022-02-15 10:04:37 +0000933 /*
Przemek Stekielc89f3ea2023-05-18 15:45:53 +0200934 * ECDHE and FFDHE groups are supported
XiaokangQian7807f9f2022-02-15 10:04:37 +0000935 */
Przemek Stekielc89f3ea2023-05-18 15:45:53 +0200936 if (mbedtls_ssl_tls13_named_group_is_ecdhe(group) ||
Przemek Stekield5f79e72023-06-29 09:08:43 +0200937 mbedtls_ssl_tls13_named_group_is_ffdh(group)) {
Przemek Stekielc89f3ea2023-05-18 15:45:53 +0200938 MBEDTLS_SSL_DEBUG_MSG(2, ("ECDH/FFDH group: %s (%04x)",
Gilles Peskine449bd832023-01-11 14:50:10 +0100939 mbedtls_ssl_named_group_to_str(group),
940 group));
Przemek Stekiel7ac93be2023-07-04 10:02:38 +0200941 ret = mbedtls_ssl_tls13_read_public_xxdhe_share(
Gilles Peskine449bd832023-01-11 14:50:10 +0100942 ssl, key_exchange - 2, key_exchange_len + 2);
943 if (ret != 0) {
944 return ret;
945 }
XiaokangQian4e8cd7b2022-04-21 09:48:09 +0000946
Gilles Peskine449bd832023-01-11 14:50:10 +0100947 } else {
948 MBEDTLS_SSL_DEBUG_MSG(4, ("Unrecognized NamedGroup %u",
949 (unsigned) group));
XiaokangQian7807f9f2022-02-15 10:04:37 +0000950 continue;
951 }
952
XiaokangQian9b5d04b2022-04-10 10:20:43 +0000953 ssl->handshake->offered_group_id = group;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000954 }
955
Jerry Yuc1be19f2022-04-23 16:11:39 +0800956
Gilles Peskine449bd832023-01-11 14:50:10 +0100957 if (ssl->handshake->offered_group_id == 0) {
958 MBEDTLS_SSL_DEBUG_MSG(1, ("no matching key share"));
959 return SSL_TLS1_3_PARSE_KEY_SHARES_EXT_NO_MATCH;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000960 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100961 return 0;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000962}
Valerio Settic9ae8622023-07-25 11:23:50 +0200963#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_EPHEMERAL_ENABLED */
XiaokangQian7807f9f2022-02-15 10:04:37 +0000964
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200965MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100966static int ssl_tls13_client_hello_has_exts(mbedtls_ssl_context *ssl,
967 int exts_mask)
XiaokangQian7807f9f2022-02-15 10:04:37 +0000968{
Jerry Yu0c354a22022-08-29 15:25:36 +0800969 int masked = ssl->handshake->received_extensions & exts_mask;
Gilles Peskine449bd832023-01-11 14:50:10 +0100970 return masked == exts_mask;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000971}
972
Jerry Yue5991322022-11-07 14:03:44 +0800973#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200974MBEDTLS_CHECK_RETURN_CRITICAL
XiaokangQianb67384d2022-04-19 00:02:38 +0000975static int ssl_tls13_client_hello_has_exts_for_ephemeral_key_exchange(
Gilles Peskine449bd832023-01-11 14:50:10 +0100976 mbedtls_ssl_context *ssl)
XiaokangQian7807f9f2022-02-15 10:04:37 +0000977{
Gilles Peskine449bd832023-01-11 14:50:10 +0100978 return ssl_tls13_client_hello_has_exts(
979 ssl,
980 MBEDTLS_SSL_EXT_MASK(SUPPORTED_GROUPS) |
981 MBEDTLS_SSL_EXT_MASK(KEY_SHARE) |
982 MBEDTLS_SSL_EXT_MASK(SIG_ALG));
XiaokangQian7807f9f2022-02-15 10:04:37 +0000983}
Jerry Yue5991322022-11-07 14:03:44 +0800984#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED */
XiaokangQian7807f9f2022-02-15 10:04:37 +0000985
Jerry Yue5991322022-11-07 14:03:44 +0800986#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK_ENABLED)
Jerry Yu77f01482022-07-11 07:03:24 +0000987MBEDTLS_CHECK_RETURN_CRITICAL
988static int ssl_tls13_client_hello_has_exts_for_psk_key_exchange(
Gilles Peskine449bd832023-01-11 14:50:10 +0100989 mbedtls_ssl_context *ssl)
Jerry Yu77f01482022-07-11 07:03:24 +0000990{
Gilles Peskine449bd832023-01-11 14:50:10 +0100991 return ssl_tls13_client_hello_has_exts(
992 ssl,
993 MBEDTLS_SSL_EXT_MASK(PRE_SHARED_KEY) |
994 MBEDTLS_SSL_EXT_MASK(PSK_KEY_EXCHANGE_MODES));
Jerry Yu77f01482022-07-11 07:03:24 +0000995}
Jerry Yue5991322022-11-07 14:03:44 +0800996#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK_ENABLED */
Jerry Yu77f01482022-07-11 07:03:24 +0000997
Jerry Yue5991322022-11-07 14:03:44 +0800998#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK_EPHEMERAL_ENABLED)
Jerry Yu77f01482022-07-11 07:03:24 +0000999MBEDTLS_CHECK_RETURN_CRITICAL
1000static int ssl_tls13_client_hello_has_exts_for_psk_ephemeral_key_exchange(
Gilles Peskine449bd832023-01-11 14:50:10 +01001001 mbedtls_ssl_context *ssl)
Jerry Yu77f01482022-07-11 07:03:24 +00001002{
Gilles Peskine449bd832023-01-11 14:50:10 +01001003 return ssl_tls13_client_hello_has_exts(
1004 ssl,
1005 MBEDTLS_SSL_EXT_MASK(SUPPORTED_GROUPS) |
1006 MBEDTLS_SSL_EXT_MASK(KEY_SHARE) |
1007 MBEDTLS_SSL_EXT_MASK(PRE_SHARED_KEY) |
1008 MBEDTLS_SSL_EXT_MASK(PSK_KEY_EXCHANGE_MODES));
Jerry Yu77f01482022-07-11 07:03:24 +00001009}
Jerry Yue5991322022-11-07 14:03:44 +08001010#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK_EPHEMERAL_ENABLED */
Jerry Yu77f01482022-07-11 07:03:24 +00001011
Pengyu Lv306a01d2023-02-02 16:35:47 +08001012#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED)
1013MBEDTLS_CHECK_RETURN_CRITICAL
Pengyu Lvd72e8582023-11-10 10:37:18 +08001014static int ssl_tls13_ticket_is_kex_mode_permitted(mbedtls_ssl_context *ssl,
1015 unsigned int kex_mode)
Pengyu Lv306a01d2023-02-02 16:35:47 +08001016{
1017#if defined(MBEDTLS_SSL_SESSION_TICKETS)
1018 if (ssl->handshake->resume) {
Pengyu Lv94a42cc2023-12-06 10:04:17 +08001019 if (!mbedtls_ssl_tls13_session_ticket_has_flags(
Pengyu Lv306a01d2023-02-02 16:35:47 +08001020 ssl->session_negotiate, kex_mode)) {
1021 return 0;
1022 }
1023 }
1024#else
1025 ((void) ssl);
1026 ((void) kex_mode);
1027#endif
1028 return 1;
1029}
1030#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED */
1031
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001032MBEDTLS_CHECK_RETURN_CRITICAL
Pengyu Lvbc4aab72023-12-01 15:37:24 +08001033static int ssl_tls13_key_exchange_is_ephemeral_available(mbedtls_ssl_context *ssl)
XiaokangQian7807f9f2022-02-15 10:04:37 +00001034{
Jerry Yue5991322022-11-07 14:03:44 +08001035#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED)
Pengyu Lv0a1ff2b2023-11-14 11:03:32 +08001036 return mbedtls_ssl_conf_tls13_is_ephemeral_enabled(ssl) &&
Gilles Peskine449bd832023-01-11 14:50:10 +01001037 ssl_tls13_client_hello_has_exts_for_ephemeral_key_exchange(ssl);
Jerry Yue5991322022-11-07 14:03:44 +08001038#else
1039 ((void) ssl);
Gilles Peskine449bd832023-01-11 14:50:10 +01001040 return 0;
Jerry Yue5991322022-11-07 14:03:44 +08001041#endif
Jerry Yu77f01482022-07-11 07:03:24 +00001042}
XiaokangQian7807f9f2022-02-15 10:04:37 +00001043
Jerry Yu77f01482022-07-11 07:03:24 +00001044MBEDTLS_CHECK_RETURN_CRITICAL
Pengyu Lvbc4aab72023-12-01 15:37:24 +08001045static int ssl_tls13_key_exchange_is_psk_available(mbedtls_ssl_context *ssl)
Jerry Yu77f01482022-07-11 07:03:24 +00001046{
Jerry Yue5991322022-11-07 14:03:44 +08001047#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK_ENABLED)
Pengyu Lvd72e8582023-11-10 10:37:18 +08001048 return ssl_tls13_ticket_is_kex_mode_permitted(
Pengyu Lv306a01d2023-02-02 16:35:47 +08001049 ssl, MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK) &&
Pengyu Lv0a1ff2b2023-11-14 11:03:32 +08001050 mbedtls_ssl_conf_tls13_is_psk_enabled(ssl) &&
Pengyu Lvb2cfafb2023-11-14 13:56:13 +08001051 mbedtls_ssl_tls13_is_psk_supported(ssl) &&
Gilles Peskine449bd832023-01-11 14:50:10 +01001052 ssl_tls13_client_hello_has_exts_for_psk_key_exchange(ssl);
Jerry Yu77f01482022-07-11 07:03:24 +00001053#else
1054 ((void) ssl);
Gilles Peskine449bd832023-01-11 14:50:10 +01001055 return 0;
Jerry Yu77f01482022-07-11 07:03:24 +00001056#endif
1057}
XiaokangQian7807f9f2022-02-15 10:04:37 +00001058
Jerry Yu77f01482022-07-11 07:03:24 +00001059MBEDTLS_CHECK_RETURN_CRITICAL
Pengyu Lvbc4aab72023-12-01 15:37:24 +08001060static int ssl_tls13_key_exchange_is_psk_ephemeral_available(mbedtls_ssl_context *ssl)
Jerry Yu77f01482022-07-11 07:03:24 +00001061{
Jerry Yue5991322022-11-07 14:03:44 +08001062#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK_EPHEMERAL_ENABLED)
Pengyu Lvd72e8582023-11-10 10:37:18 +08001063 return ssl_tls13_ticket_is_kex_mode_permitted(
Pengyu Lv306a01d2023-02-02 16:35:47 +08001064 ssl, MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK_EPHEMERAL) &&
Pengyu Lv0a1ff2b2023-11-14 11:03:32 +08001065 mbedtls_ssl_conf_tls13_is_psk_ephemeral_enabled(ssl) &&
Pengyu Lvb2cfafb2023-11-14 13:56:13 +08001066 mbedtls_ssl_tls13_is_psk_ephemeral_supported(ssl) &&
Gilles Peskine449bd832023-01-11 14:50:10 +01001067 ssl_tls13_client_hello_has_exts_for_psk_ephemeral_key_exchange(ssl);
Jerry Yu77f01482022-07-11 07:03:24 +00001068#else
1069 ((void) ssl);
Gilles Peskine449bd832023-01-11 14:50:10 +01001070 return 0;
Jerry Yu77f01482022-07-11 07:03:24 +00001071#endif
1072}
1073
Gilles Peskine449bd832023-01-11 14:50:10 +01001074static int ssl_tls13_determine_key_exchange_mode(mbedtls_ssl_context *ssl)
Jerry Yu77f01482022-07-11 07:03:24 +00001075{
1076 /*
1077 * Determine the key exchange algorithm to use.
1078 * There are three types of key exchanges supported in TLS 1.3:
1079 * - (EC)DH with ECDSA,
1080 * - (EC)DH with PSK,
1081 * - plain PSK.
1082 *
1083 * The PSK-based key exchanges may additionally be used with 0-RTT.
1084 *
1085 * Our built-in order of preference is
Jerry Yuce6ed702022-07-22 21:49:53 +08001086 * 1 ) (EC)DHE-PSK Mode ( psk_ephemeral )
1087 * 2 ) Certificate Mode ( ephemeral )
1088 * 3 ) Plain PSK Mode ( psk )
Jerry Yu77f01482022-07-11 07:03:24 +00001089 */
1090
Xiaokang Qian9f1747b2023-03-30 02:50:04 +00001091 ssl->handshake->key_exchange_mode =
1092 MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_NONE;
Jerry Yu77f01482022-07-11 07:03:24 +00001093
Pengyu Lvbc4aab72023-12-01 15:37:24 +08001094 if (ssl_tls13_key_exchange_is_psk_ephemeral_available(ssl)) {
Jerry Yu77f01482022-07-11 07:03:24 +00001095 ssl->handshake->key_exchange_mode =
1096 MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK_EPHEMERAL;
Gilles Peskine449bd832023-01-11 14:50:10 +01001097 MBEDTLS_SSL_DEBUG_MSG(2, ("key exchange mode: psk_ephemeral"));
1098 } else
Pengyu Lvbc4aab72023-12-01 15:37:24 +08001099 if (ssl_tls13_key_exchange_is_ephemeral_available(ssl)) {
Jerry Yu77f01482022-07-11 07:03:24 +00001100 ssl->handshake->key_exchange_mode =
1101 MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL;
Gilles Peskine449bd832023-01-11 14:50:10 +01001102 MBEDTLS_SSL_DEBUG_MSG(2, ("key exchange mode: ephemeral"));
1103 } else
Pengyu Lvbc4aab72023-12-01 15:37:24 +08001104 if (ssl_tls13_key_exchange_is_psk_available(ssl)) {
Jerry Yuce6ed702022-07-22 21:49:53 +08001105 ssl->handshake->key_exchange_mode =
1106 MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK;
Gilles Peskine449bd832023-01-11 14:50:10 +01001107 MBEDTLS_SSL_DEBUG_MSG(2, ("key exchange mode: psk"));
1108 } else {
Jerry Yu77f01482022-07-11 07:03:24 +00001109 MBEDTLS_SSL_DEBUG_MSG(
Gilles Peskine449bd832023-01-11 14:50:10 +01001110 1,
1111 ("ClientHello message misses mandatory extensions."));
1112 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_MISSING_EXTENSION,
1113 MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER);
1114 return MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER;
Jerry Yu77f01482022-07-11 07:03:24 +00001115 }
1116
Gilles Peskine449bd832023-01-11 14:50:10 +01001117 return 0;
Jerry Yu77f01482022-07-11 07:03:24 +00001118
XiaokangQian7807f9f2022-02-15 10:04:37 +00001119}
1120
XiaokangQian81802f42022-06-10 13:25:22 +00001121#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
Ronald Cron928cbd32022-10-04 16:14:26 +02001122 defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED)
Ronald Cron67ea2542022-09-15 17:34:42 +02001123
1124#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01001125static psa_algorithm_t ssl_tls13_iana_sig_alg_to_psa_alg(uint16_t sig_alg)
Ronald Cron67ea2542022-09-15 17:34:42 +02001126{
Gilles Peskine449bd832023-01-11 14:50:10 +01001127 switch (sig_alg) {
Ronald Cron67ea2542022-09-15 17:34:42 +02001128 case MBEDTLS_TLS1_3_SIG_ECDSA_SECP256R1_SHA256:
Gilles Peskine449bd832023-01-11 14:50:10 +01001129 return PSA_ALG_ECDSA(PSA_ALG_SHA_256);
Ronald Cron67ea2542022-09-15 17:34:42 +02001130 case MBEDTLS_TLS1_3_SIG_ECDSA_SECP384R1_SHA384:
Gilles Peskine449bd832023-01-11 14:50:10 +01001131 return PSA_ALG_ECDSA(PSA_ALG_SHA_384);
Ronald Cron67ea2542022-09-15 17:34:42 +02001132 case MBEDTLS_TLS1_3_SIG_ECDSA_SECP521R1_SHA512:
Gilles Peskine449bd832023-01-11 14:50:10 +01001133 return PSA_ALG_ECDSA(PSA_ALG_SHA_512);
Ronald Cron67ea2542022-09-15 17:34:42 +02001134 case MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA256:
Gilles Peskine449bd832023-01-11 14:50:10 +01001135 return PSA_ALG_RSA_PSS(PSA_ALG_SHA_256);
Ronald Cron67ea2542022-09-15 17:34:42 +02001136 case MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA384:
Gilles Peskine449bd832023-01-11 14:50:10 +01001137 return PSA_ALG_RSA_PSS(PSA_ALG_SHA_384);
Ronald Cron67ea2542022-09-15 17:34:42 +02001138 case MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA512:
Gilles Peskine449bd832023-01-11 14:50:10 +01001139 return PSA_ALG_RSA_PSS(PSA_ALG_SHA_512);
Ronald Cron67ea2542022-09-15 17:34:42 +02001140 case MBEDTLS_TLS1_3_SIG_RSA_PKCS1_SHA256:
Gilles Peskine449bd832023-01-11 14:50:10 +01001141 return PSA_ALG_RSA_PKCS1V15_SIGN(PSA_ALG_SHA_256);
Ronald Cron67ea2542022-09-15 17:34:42 +02001142 case MBEDTLS_TLS1_3_SIG_RSA_PKCS1_SHA384:
Gilles Peskine449bd832023-01-11 14:50:10 +01001143 return PSA_ALG_RSA_PKCS1V15_SIGN(PSA_ALG_SHA_384);
Ronald Cron67ea2542022-09-15 17:34:42 +02001144 case MBEDTLS_TLS1_3_SIG_RSA_PKCS1_SHA512:
Gilles Peskine449bd832023-01-11 14:50:10 +01001145 return PSA_ALG_RSA_PKCS1V15_SIGN(PSA_ALG_SHA_512);
Ronald Cron67ea2542022-09-15 17:34:42 +02001146 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01001147 return PSA_ALG_NONE;
Ronald Cron67ea2542022-09-15 17:34:42 +02001148 }
1149}
1150#endif /* MBEDTLS_USE_PSA_CRYPTO */
1151
XiaokangQian23c5be62022-06-07 02:04:34 +00001152/*
XiaokangQianfb665a82022-06-15 03:57:21 +00001153 * Pick best ( private key, certificate chain ) pair based on the signature
1154 * algorithms supported by the client.
XiaokangQian23c5be62022-06-07 02:04:34 +00001155 */
Ronald Cronce7d76e2022-07-08 18:56:49 +02001156MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01001157static int ssl_tls13_pick_key_cert(mbedtls_ssl_context *ssl)
XiaokangQian23c5be62022-06-07 02:04:34 +00001158{
XiaokangQianfb665a82022-06-15 03:57:21 +00001159 mbedtls_ssl_key_cert *key_cert, *key_cert_list;
XiaokangQian81802f42022-06-10 13:25:22 +00001160 const uint16_t *sig_alg = ssl->handshake->received_sig_algs;
XiaokangQian23c5be62022-06-07 02:04:34 +00001161
1162#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Gilles Peskine449bd832023-01-11 14:50:10 +01001163 if (ssl->handshake->sni_key_cert != NULL) {
XiaokangQianfb665a82022-06-15 03:57:21 +00001164 key_cert_list = ssl->handshake->sni_key_cert;
Gilles Peskine449bd832023-01-11 14:50:10 +01001165 } else
XiaokangQian23c5be62022-06-07 02:04:34 +00001166#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
Gilles Peskine449bd832023-01-11 14:50:10 +01001167 key_cert_list = ssl->conf->key_cert;
XiaokangQian23c5be62022-06-07 02:04:34 +00001168
Gilles Peskine449bd832023-01-11 14:50:10 +01001169 if (key_cert_list == NULL) {
1170 MBEDTLS_SSL_DEBUG_MSG(3, ("server has no certificate"));
1171 return -1;
XiaokangQian23c5be62022-06-07 02:04:34 +00001172 }
1173
Gilles Peskine449bd832023-01-11 14:50:10 +01001174 for (; *sig_alg != MBEDTLS_TLS1_3_SIG_NONE; sig_alg++) {
1175 if (!mbedtls_ssl_sig_alg_is_offered(ssl, *sig_alg)) {
Ronald Cron67ea2542022-09-15 17:34:42 +02001176 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +01001177 }
Ronald Cron67ea2542022-09-15 17:34:42 +02001178
Gilles Peskine449bd832023-01-11 14:50:10 +01001179 if (!mbedtls_ssl_tls13_sig_alg_for_cert_verify_is_supported(*sig_alg)) {
Ronald Cron67ea2542022-09-15 17:34:42 +02001180 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +01001181 }
Ronald Cron67ea2542022-09-15 17:34:42 +02001182
Gilles Peskine449bd832023-01-11 14:50:10 +01001183 for (key_cert = key_cert_list; key_cert != NULL;
1184 key_cert = key_cert->next) {
Ronald Cron67ea2542022-09-15 17:34:42 +02001185#if defined(MBEDTLS_USE_PSA_CRYPTO)
1186 psa_algorithm_t psa_alg = PSA_ALG_NONE;
1187#endif /* MBEDTLS_USE_PSA_CRYPTO */
1188
Gilles Peskine449bd832023-01-11 14:50:10 +01001189 MBEDTLS_SSL_DEBUG_CRT(3, "certificate (chain) candidate",
1190 key_cert->cert);
XiaokangQian81802f42022-06-10 13:25:22 +00001191
1192 /*
Gilles Peskine449bd832023-01-11 14:50:10 +01001193 * This avoids sending the client a cert it'll reject based on
1194 * keyUsage or other extensions.
1195 */
1196 if (mbedtls_x509_crt_check_key_usage(
1197 key_cert->cert, MBEDTLS_X509_KU_DIGITAL_SIGNATURE) != 0 ||
XiaokangQian81802f42022-06-10 13:25:22 +00001198 mbedtls_x509_crt_check_extended_key_usage(
XiaokangQianfb665a82022-06-15 03:57:21 +00001199 key_cert->cert, MBEDTLS_OID_SERVER_AUTH,
Gilles Peskine449bd832023-01-11 14:50:10 +01001200 MBEDTLS_OID_SIZE(MBEDTLS_OID_SERVER_AUTH)) != 0) {
1201 MBEDTLS_SSL_DEBUG_MSG(3, ("certificate mismatch: "
1202 "(extended) key usage extension"));
XiaokangQian81802f42022-06-10 13:25:22 +00001203 continue;
1204 }
XiaokangQian23c5be62022-06-07 02:04:34 +00001205
Gilles Peskine449bd832023-01-11 14:50:10 +01001206 MBEDTLS_SSL_DEBUG_MSG(3,
1207 ("ssl_tls13_pick_key_cert:"
1208 "check signature algorithm %s [%04x]",
1209 mbedtls_ssl_sig_alg_to_str(*sig_alg),
1210 *sig_alg));
Ronald Cron67ea2542022-09-15 17:34:42 +02001211#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01001212 psa_alg = ssl_tls13_iana_sig_alg_to_psa_alg(*sig_alg);
Ronald Cron67ea2542022-09-15 17:34:42 +02001213#endif /* MBEDTLS_USE_PSA_CRYPTO */
1214
Gilles Peskine449bd832023-01-11 14:50:10 +01001215 if (mbedtls_ssl_tls13_check_sig_alg_cert_key_match(
1216 *sig_alg, &key_cert->cert->pk)
Ronald Cron67ea2542022-09-15 17:34:42 +02001217#if defined(MBEDTLS_USE_PSA_CRYPTO)
1218 && psa_alg != PSA_ALG_NONE &&
Gilles Peskine449bd832023-01-11 14:50:10 +01001219 mbedtls_pk_can_do_ext(&key_cert->cert->pk, psa_alg,
1220 PSA_KEY_USAGE_SIGN_HASH) == 1
Ronald Cron67ea2542022-09-15 17:34:42 +02001221#endif /* MBEDTLS_USE_PSA_CRYPTO */
Gilles Peskine449bd832023-01-11 14:50:10 +01001222 ) {
XiaokangQianfb665a82022-06-15 03:57:21 +00001223 ssl->handshake->key_cert = key_cert;
Gilles Peskine449bd832023-01-11 14:50:10 +01001224 MBEDTLS_SSL_DEBUG_MSG(3,
1225 ("ssl_tls13_pick_key_cert:"
1226 "selected signature algorithm"
1227 " %s [%04x]",
1228 mbedtls_ssl_sig_alg_to_str(*sig_alg),
1229 *sig_alg));
XiaokangQianfb665a82022-06-15 03:57:21 +00001230 MBEDTLS_SSL_DEBUG_CRT(
Gilles Peskine449bd832023-01-11 14:50:10 +01001231 3, "selected certificate (chain)",
1232 ssl->handshake->key_cert->cert);
1233 return 0;
XiaokangQian81802f42022-06-10 13:25:22 +00001234 }
XiaokangQian81802f42022-06-10 13:25:22 +00001235 }
XiaokangQian23c5be62022-06-07 02:04:34 +00001236 }
1237
Gilles Peskine449bd832023-01-11 14:50:10 +01001238 MBEDTLS_SSL_DEBUG_MSG(2, ("ssl_tls13_pick_key_cert:"
1239 "no suitable certificate found"));
1240 return -1;
XiaokangQian23c5be62022-06-07 02:04:34 +00001241}
XiaokangQian81802f42022-06-10 13:25:22 +00001242#endif /* MBEDTLS_X509_CRT_PARSE_C &&
Ronald Cron928cbd32022-10-04 16:14:26 +02001243 MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED */
XiaokangQian23c5be62022-06-07 02:04:34 +00001244
XiaokangQian4080a7f2022-04-11 09:55:18 +00001245/*
XiaokangQianed582dd2022-04-13 08:21:05 +00001246 *
1247 * STATE HANDLING: ClientHello
1248 *
XiaokangQianb67384d2022-04-19 00:02:38 +00001249 * There are three possible classes of outcomes when parsing the ClientHello:
XiaokangQianed582dd2022-04-13 08:21:05 +00001250 *
XiaokangQianb67384d2022-04-19 00:02:38 +00001251 * 1) The ClientHello was well-formed and matched the server's configuration.
XiaokangQianed582dd2022-04-13 08:21:05 +00001252 *
1253 * In this case, the server progresses to sending its ServerHello.
1254 *
XiaokangQianb67384d2022-04-19 00:02:38 +00001255 * 2) The ClientHello was well-formed but didn't match the server's
1256 * configuration.
XiaokangQianed582dd2022-04-13 08:21:05 +00001257 *
1258 * For example, the client might not have offered a key share which
1259 * the server supports, or the server might require a cookie.
1260 *
1261 * In this case, the server sends a HelloRetryRequest.
1262 *
XiaokangQianb67384d2022-04-19 00:02:38 +00001263 * 3) The ClientHello was ill-formed
XiaokangQianed582dd2022-04-13 08:21:05 +00001264 *
1265 * In this case, we abort the handshake.
1266 *
1267 */
1268
1269/*
XiaokangQian4080a7f2022-04-11 09:55:18 +00001270 * Structure of this message:
1271 *
XiaokangQiane8ff3502022-04-22 02:34:40 +00001272 * uint16 ProtocolVersion;
1273 * opaque Random[32];
1274 * uint8 CipherSuite[2]; // Cryptographic suite selector
XiaokangQian4080a7f2022-04-11 09:55:18 +00001275 *
XiaokangQiane8ff3502022-04-22 02:34:40 +00001276 * struct {
1277 * ProtocolVersion legacy_version = 0x0303; // TLS v1.2
1278 * Random random;
1279 * opaque legacy_session_id<0..32>;
1280 * CipherSuite cipher_suites<2..2^16-2>;
1281 * opaque legacy_compression_methods<1..2^8-1>;
1282 * Extension extensions<8..2^16-1>;
1283 * } ClientHello;
XiaokangQian4080a7f2022-04-11 09:55:18 +00001284 */
XiaokangQianed582dd2022-04-13 08:21:05 +00001285
1286#define SSL_CLIENT_HELLO_OK 0
1287#define SSL_CLIENT_HELLO_HRR_REQUIRED 1
Ronald Cron5af4c7f2023-03-07 20:46:59 +01001288#define SSL_CLIENT_HELLO_TLS1_2 2
XiaokangQianed582dd2022-04-13 08:21:05 +00001289
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001290MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01001291static int ssl_tls13_parse_client_hello(mbedtls_ssl_context *ssl,
1292 const unsigned char *buf,
1293 const unsigned char *end)
XiaokangQian7807f9f2022-02-15 10:04:37 +00001294{
XiaokangQianb67384d2022-04-19 00:02:38 +00001295 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1296 const unsigned char *p = buf;
Ronald Crond540d992023-03-07 09:41:48 +01001297 const unsigned char *random;
XiaokangQian4080a7f2022-04-11 09:55:18 +00001298 size_t legacy_session_id_len;
Ronald Croncada4102023-03-07 09:51:39 +01001299 const unsigned char *legacy_session_id;
XiaokangQian318dc762022-04-20 09:43:51 +00001300 size_t cipher_suites_len;
Ronald Cron2f16b4e2023-03-07 10:07:32 +01001301 const unsigned char *cipher_suites;
XiaokangQian060d8672022-04-21 09:24:56 +00001302 const unsigned char *cipher_suites_end;
XiaokangQianb67384d2022-04-19 00:02:38 +00001303 size_t extensions_len;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001304 const unsigned char *extensions_end;
Ronald Croneff56732023-04-03 17:36:31 +02001305 const unsigned char *supported_versions_data;
1306 const unsigned char *supported_versions_data_end;
Jerry Yuc4bf5d62022-10-29 09:08:47 +08001307 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
Jerry Yu49ca9282022-05-05 11:05:22 +08001308 int hrr_required = 0;
Ronald Cron8a74f072023-06-14 17:59:29 +02001309 int no_usable_share_for_key_agreement = 0;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001310
Ronald Cron41a443a2022-10-04 16:38:25 +02001311#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED)
Jerry Yu29d9faa2022-08-23 17:52:45 +08001312 const unsigned char *pre_shared_key_ext = NULL;
Jerry Yu1c105562022-07-10 06:32:38 +00001313 const unsigned char *pre_shared_key_ext_end = NULL;
Ronald Cron41a443a2022-10-04 16:38:25 +02001314#endif
XiaokangQian7807f9f2022-02-15 10:04:37 +00001315
XiaokangQian7807f9f2022-02-15 10:04:37 +00001316 /*
XiaokangQianb67384d2022-04-19 00:02:38 +00001317 * ClientHello layout:
XiaokangQian7807f9f2022-02-15 10:04:37 +00001318 * 0 . 1 protocol version
XiaokangQiane8ff3502022-04-22 02:34:40 +00001319 * 2 . 33 random bytes
XiaokangQianc5763b52022-04-02 03:34:37 +00001320 * 34 . 34 session id length ( 1 byte )
XiaokangQian7807f9f2022-02-15 10:04:37 +00001321 * 35 . 34+x session id
XiaokangQian7807f9f2022-02-15 10:04:37 +00001322 * .. . .. ciphersuite list length ( 2 bytes )
1323 * .. . .. ciphersuite list
1324 * .. . .. compression alg. list length ( 1 byte )
1325 * .. . .. compression alg. list
1326 * .. . .. extensions length ( 2 bytes, optional )
1327 * .. . .. extensions ( optional )
1328 */
1329
XiaokangQianb67384d2022-04-19 00:02:38 +00001330 /*
bootstrap-prime6dbbf442022-05-17 19:30:44 -04001331 * Minimal length ( with everything empty and extensions omitted ) is
XiaokangQian7807f9f2022-02-15 10:04:37 +00001332 * 2 + 32 + 1 + 2 + 1 = 38 bytes. Check that first, so that we can
1333 * read at least up to session id length without worrying.
1334 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001335 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, 38);
XiaokangQian7807f9f2022-02-15 10:04:37 +00001336
1337 /* ...
1338 * ProtocolVersion legacy_version = 0x0303; // TLS 1.2
1339 * ...
1340 * with ProtocolVersion defined as:
1341 * uint16 ProtocolVersion;
1342 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001343 if (mbedtls_ssl_read_version(p, ssl->conf->transport) !=
1344 MBEDTLS_SSL_VERSION_TLS1_2) {
1345 MBEDTLS_SSL_DEBUG_MSG(1, ("Unsupported version of TLS."));
1346 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_PROTOCOL_VERSION,
1347 MBEDTLS_ERR_SSL_BAD_PROTOCOL_VERSION);
1348 return MBEDTLS_ERR_SSL_BAD_PROTOCOL_VERSION;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001349 }
1350 p += 2;
1351
Jerry Yuc1be19f2022-04-23 16:11:39 +08001352 /* ...
1353 * Random random;
1354 * ...
XiaokangQianb67384d2022-04-19 00:02:38 +00001355 * with Random defined as:
1356 * opaque Random[32];
XiaokangQian7807f9f2022-02-15 10:04:37 +00001357 */
Ronald Crond540d992023-03-07 09:41:48 +01001358 random = p;
XiaokangQian08037552022-04-20 07:16:41 +00001359 p += MBEDTLS_CLIENT_HELLO_RANDOM_LEN;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001360
Jerry Yuc1be19f2022-04-23 16:11:39 +08001361 /* ...
XiaokangQianb67384d2022-04-19 00:02:38 +00001362 * opaque legacy_session_id<0..32>;
Jerry Yuc1be19f2022-04-23 16:11:39 +08001363 * ...
XiaokangQian7807f9f2022-02-15 10:04:37 +00001364 */
Ronald Croncada4102023-03-07 09:51:39 +01001365 legacy_session_id_len = *(p++);
1366 legacy_session_id = p;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001367
XiaokangQianb67384d2022-04-19 00:02:38 +00001368 /*
1369 * Check we have enough data for the legacy session identifier
Jerry Yue95c8af2022-07-26 15:48:20 +08001370 * and the ciphersuite list length.
XiaokangQianb67384d2022-04-19 00:02:38 +00001371 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001372 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, legacy_session_id_len + 2);
XiaokangQian4080a7f2022-04-11 09:55:18 +00001373 p += legacy_session_id_len;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001374
Ronald Cron2f16b4e2023-03-07 10:07:32 +01001375 /* ...
1376 * CipherSuite cipher_suites<2..2^16-2>;
1377 * ...
1378 * with CipherSuite defined as:
1379 * uint8 CipherSuite[2];
1380 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001381 cipher_suites_len = MBEDTLS_GET_UINT16_BE(p, 0);
XiaokangQian7807f9f2022-02-15 10:04:37 +00001382 p += 2;
Ronald Cron2f16b4e2023-03-07 10:07:32 +01001383 cipher_suites = p;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001384
Ronald Cronfc7ae872023-02-16 15:32:19 +01001385 /*
1386 * The length of the ciphersuite list has to be even.
1387 */
1388 if (cipher_suites_len & 1) {
1389 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR,
1390 MBEDTLS_ERR_SSL_DECODE_ERROR);
1391 return MBEDTLS_ERR_SSL_DECODE_ERROR;
1392 }
1393
XiaokangQianb67384d2022-04-19 00:02:38 +00001394 /* Check we have enough data for the ciphersuite list, the legacy
1395 * compression methods and the length of the extensions.
Jerry Yue95c8af2022-07-26 15:48:20 +08001396 *
1397 * cipher_suites cipher_suites_len bytes
1398 * legacy_compression_methods 2 bytes
1399 * extensions_len 2 bytes
XiaokangQianb67384d2022-04-19 00:02:38 +00001400 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001401 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, cipher_suites_len + 2 + 2);
Ronald Cron8c527d02023-03-07 15:47:47 +01001402 p += cipher_suites_len;
1403 cipher_suites_end = p;
Jerry Yu5725f1c2022-08-21 17:27:16 +08001404
1405 /*
Ronald Cron8c527d02023-03-07 15:47:47 +01001406 * Search for the supported versions extension and parse it to determine
1407 * if the client supports TLS 1.3.
1408 */
1409 ret = mbedtls_ssl_tls13_is_supported_versions_ext_present_in_exts(
1410 ssl, p + 2, end,
Ronald Croneff56732023-04-03 17:36:31 +02001411 &supported_versions_data, &supported_versions_data_end);
Ronald Cron8c527d02023-03-07 15:47:47 +01001412 if (ret < 0) {
1413 MBEDTLS_SSL_DEBUG_RET(1,
1414 ("mbedtls_ssl_tls13_is_supported_versions_ext_present_in_exts"), ret);
1415 return ret;
1416 }
1417
1418 if (ret == 0) {
Ronald Cron5af4c7f2023-03-07 20:46:59 +01001419 return SSL_CLIENT_HELLO_TLS1_2;
Ronald Cron8c527d02023-03-07 15:47:47 +01001420 }
1421
Ronald Cron5af4c7f2023-03-07 20:46:59 +01001422 if (ret == 1) {
1423 ret = ssl_tls13_parse_supported_versions_ext(ssl,
Ronald Croneff56732023-04-03 17:36:31 +02001424 supported_versions_data,
1425 supported_versions_data_end);
Ronald Cron5af4c7f2023-03-07 20:46:59 +01001426 if (ret < 0) {
1427 MBEDTLS_SSL_DEBUG_RET(1,
1428 ("ssl_tls13_parse_supported_versions_ext"), ret);
1429 return ret;
1430 }
1431
Ronald Cronb828c7d2023-04-03 16:37:22 +02001432 /*
1433 * The supported versions extension was parsed successfully as the
1434 * value returned by ssl_tls13_parse_supported_versions_ext() is
1435 * positive. The return value is then equal to
1436 * MBEDTLS_SSL_VERSION_TLS1_2 or MBEDTLS_SSL_VERSION_TLS1_3, defining
1437 * the TLS version to negotiate.
1438 */
Ronald Cron5af4c7f2023-03-07 20:46:59 +01001439 if (MBEDTLS_SSL_VERSION_TLS1_2 == ret) {
1440 return SSL_CLIENT_HELLO_TLS1_2;
1441 }
Ronald Cron8c527d02023-03-07 15:47:47 +01001442 }
1443
1444 /*
1445 * We negotiate TLS 1.3.
Ronald Cron64582392023-03-07 09:21:40 +01001446 */
1447 ssl->tls_version = MBEDTLS_SSL_VERSION_TLS1_3;
Ronald Cron64582392023-03-07 09:21:40 +01001448 ssl->session_negotiate->tls_version = MBEDTLS_SSL_VERSION_TLS1_3;
1449 ssl->session_negotiate->endpoint = ssl->conf->endpoint;
Ronald Cron64582392023-03-07 09:21:40 +01001450
1451 /*
Ronald Crondad02b22023-04-06 09:57:52 +02001452 * We are negotiating the version 1.3 of the protocol. Do what we have
Ronald Croncada4102023-03-07 09:51:39 +01001453 * postponed: copy of the client random bytes, copy of the legacy session
Ronald Cron2f16b4e2023-03-07 10:07:32 +01001454 * identifier and selection of the TLS 1.3 cipher suite.
Ronald Crond540d992023-03-07 09:41:48 +01001455 */
1456 MBEDTLS_SSL_DEBUG_BUF(3, "client hello, random bytes",
1457 random, MBEDTLS_CLIENT_HELLO_RANDOM_LEN);
1458 memcpy(&handshake->randbytes[0], random, MBEDTLS_CLIENT_HELLO_RANDOM_LEN);
1459
Ronald Croncada4102023-03-07 09:51:39 +01001460 if (legacy_session_id_len > sizeof(ssl->session_negotiate->id)) {
1461 MBEDTLS_SSL_DEBUG_MSG(1, ("bad client hello message"));
1462 return MBEDTLS_ERR_SSL_DECODE_ERROR;
1463 }
1464 ssl->session_negotiate->id_len = legacy_session_id_len;
1465 MBEDTLS_SSL_DEBUG_BUF(3, "client hello, session id",
1466 legacy_session_id, legacy_session_id_len);
1467 memcpy(&ssl->session_negotiate->id[0],
1468 legacy_session_id, legacy_session_id_len);
1469
Ronald Crond540d992023-03-07 09:41:48 +01001470 /*
Jerry Yu5725f1c2022-08-21 17:27:16 +08001471 * Search for a matching ciphersuite
1472 */
Ronald Cron2f16b4e2023-03-07 10:07:32 +01001473 MBEDTLS_SSL_DEBUG_BUF(3, "client hello, list of cipher suites",
1474 cipher_suites, cipher_suites_len);
Ronald Crone45afd72023-04-04 15:10:06 +02001475 for (const unsigned char *cipher_suites_p = cipher_suites;
1476 cipher_suites_p < cipher_suites_end; cipher_suites_p += 2) {
XiaokangQiane8ff3502022-04-22 02:34:40 +00001477 uint16_t cipher_suite;
Gilles Peskine449bd832023-01-11 14:50:10 +01001478 const mbedtls_ssl_ciphersuite_t *ciphersuite_info;
Jerry Yu5725f1c2022-08-21 17:27:16 +08001479
Ronald Crond89360b2023-02-21 08:53:33 +01001480 /*
Ronald Crone45afd72023-04-04 15:10:06 +02001481 * "cipher_suites_end - cipher_suites_p is even" is an invariant of the
1482 * loop. As cipher_suites_end - cipher_suites_p > 0, we have
1483 * cipher_suites_end - cipher_suites_p >= 2 and it is thus safe to read
1484 * two bytes.
Ronald Crond89360b2023-02-21 08:53:33 +01001485 */
Ronald Crone45afd72023-04-04 15:10:06 +02001486 cipher_suite = MBEDTLS_GET_UINT16_BE(cipher_suites_p, 0);
Jerry Yuc5a23a02022-08-25 10:51:44 +08001487 ciphersuite_info = ssl_tls13_validate_peer_ciphersuite(
Gilles Peskine449bd832023-01-11 14:50:10 +01001488 ssl, cipher_suite);
1489 if (ciphersuite_info == NULL) {
Jerry Yu5725f1c2022-08-21 17:27:16 +08001490 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +01001491 }
Jerry Yu5725f1c2022-08-21 17:27:16 +08001492
Jerry Yu5725f1c2022-08-21 17:27:16 +08001493 ssl->session_negotiate->ciphersuite = cipher_suite;
Jerry Yuc4bf5d62022-10-29 09:08:47 +08001494 handshake->ciphersuite_info = ciphersuite_info;
Gilles Peskine449bd832023-01-11 14:50:10 +01001495 MBEDTLS_SSL_DEBUG_MSG(2, ("selected ciphersuite: %04x - %s",
1496 cipher_suite,
1497 ciphersuite_info->name));
Ronald Cron25e9ec62023-02-16 15:35:16 +01001498 break;
XiaokangQian17f974c2022-04-19 09:57:41 +00001499 }
Jerry Yu5725f1c2022-08-21 17:27:16 +08001500
Gilles Peskine449bd832023-01-11 14:50:10 +01001501 if (handshake->ciphersuite_info == NULL) {
1502 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE,
1503 MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE);
1504 return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
Jerry Yu5725f1c2022-08-21 17:27:16 +08001505 }
Jerry Yuc1be19f2022-04-23 16:11:39 +08001506
XiaokangQian4080a7f2022-04-11 09:55:18 +00001507 /* ...
XiaokangQianb67384d2022-04-19 00:02:38 +00001508 * opaque legacy_compression_methods<1..2^8-1>;
XiaokangQian4080a7f2022-04-11 09:55:18 +00001509 * ...
XiaokangQian7807f9f2022-02-15 10:04:37 +00001510 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001511 if (p[0] != 1 || p[1] != MBEDTLS_SSL_COMPRESS_NULL) {
1512 MBEDTLS_SSL_DEBUG_MSG(1, ("bad legacy compression method"));
1513 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER,
1514 MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER);
1515 return MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001516 }
XiaokangQianb67384d2022-04-19 00:02:38 +00001517 p += 2;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001518
Jerry Yuc1be19f2022-04-23 16:11:39 +08001519 /* ...
XiaokangQianb67384d2022-04-19 00:02:38 +00001520 * Extension extensions<8..2^16-1>;
Jerry Yuc1be19f2022-04-23 16:11:39 +08001521 * ...
XiaokangQianb67384d2022-04-19 00:02:38 +00001522 * with Extension defined as:
1523 * struct {
1524 * ExtensionType extension_type;
1525 * opaque extension_data<0..2^16-1>;
1526 * } Extension;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001527 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001528 extensions_len = MBEDTLS_GET_UINT16_BE(p, 0);
XiaokangQian7807f9f2022-02-15 10:04:37 +00001529 p += 2;
Gilles Peskine449bd832023-01-11 14:50:10 +01001530 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, extensions_len);
XiaokangQiane8ff3502022-04-22 02:34:40 +00001531 extensions_end = p + extensions_len;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001532
Gilles Peskine449bd832023-01-11 14:50:10 +01001533 MBEDTLS_SSL_DEBUG_BUF(3, "client hello extensions", p, extensions_len);
Jerry Yu63a459c2022-10-31 13:38:40 +08001534 handshake->received_extensions = MBEDTLS_SSL_EXT_MASK_NONE;
Jerry Yu0c354a22022-08-29 15:25:36 +08001535
Gilles Peskine449bd832023-01-11 14:50:10 +01001536 while (p < extensions_end) {
XiaokangQian7807f9f2022-02-15 10:04:37 +00001537 unsigned int extension_type;
1538 size_t extension_data_len;
1539 const unsigned char *extension_data_end;
Jerry Yu263dbf72022-10-26 10:51:27 +08001540 uint32_t allowed_exts = MBEDTLS_SSL_TLS1_3_ALLOWED_EXTS_OF_CH;
1541
Ronald Cron5fbd2702024-02-14 10:03:36 +01001542 if (ssl->handshake->hello_retry_request_flag) {
Jerry Yu263dbf72022-10-26 10:51:27 +08001543 /* Do not accept early data extension in 2nd ClientHello */
1544 allowed_exts &= ~MBEDTLS_SSL_EXT_MASK(EARLY_DATA);
1545 }
XiaokangQian7807f9f2022-02-15 10:04:37 +00001546
Jerry Yu0c354a22022-08-29 15:25:36 +08001547 /* RFC 8446, section 4.2.11
Jerry Yu1c9247c2022-07-21 12:37:39 +08001548 *
1549 * The "pre_shared_key" extension MUST be the last extension in the
1550 * ClientHello (this facilitates implementation as described below).
1551 * Servers MUST check that it is the last extension and otherwise fail
1552 * the handshake with an "illegal_parameter" alert.
1553 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001554 if (handshake->received_extensions & MBEDTLS_SSL_EXT_MASK(PRE_SHARED_KEY)) {
Jerry Yu1c9247c2022-07-21 12:37:39 +08001555 MBEDTLS_SSL_DEBUG_MSG(
Gilles Peskine449bd832023-01-11 14:50:10 +01001556 3, ("pre_shared_key is not last extension."));
Jerry Yu1c9247c2022-07-21 12:37:39 +08001557 MBEDTLS_SSL_PEND_FATAL_ALERT(
1558 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER,
Gilles Peskine449bd832023-01-11 14:50:10 +01001559 MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER);
1560 return MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER;
Jerry Yu1c9247c2022-07-21 12:37:39 +08001561 }
1562
Gilles Peskine449bd832023-01-11 14:50:10 +01001563 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, extensions_end, 4);
1564 extension_type = MBEDTLS_GET_UINT16_BE(p, 0);
1565 extension_data_len = MBEDTLS_GET_UINT16_BE(p, 2);
XiaokangQian7807f9f2022-02-15 10:04:37 +00001566 p += 4;
1567
Gilles Peskine449bd832023-01-11 14:50:10 +01001568 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, extensions_end, extension_data_len);
XiaokangQian7807f9f2022-02-15 10:04:37 +00001569 extension_data_end = p + extension_data_len;
1570
Jerry Yuc4bf5d62022-10-29 09:08:47 +08001571 ret = mbedtls_ssl_tls13_check_received_extension(
Gilles Peskine449bd832023-01-11 14:50:10 +01001572 ssl, MBEDTLS_SSL_HS_CLIENT_HELLO, extension_type,
Jerry Yu263dbf72022-10-26 10:51:27 +08001573 allowed_exts);
Gilles Peskine449bd832023-01-11 14:50:10 +01001574 if (ret != 0) {
1575 return ret;
1576 }
Jerry Yue18dc7e2022-08-04 16:29:22 +08001577
Gilles Peskine449bd832023-01-11 14:50:10 +01001578 switch (extension_type) {
XiaokangQian40a35232022-05-07 09:02:40 +00001579#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
1580 case MBEDTLS_TLS_EXT_SERVERNAME:
Gilles Peskine449bd832023-01-11 14:50:10 +01001581 MBEDTLS_SSL_DEBUG_MSG(3, ("found ServerName extension"));
1582 ret = mbedtls_ssl_parse_server_name_ext(ssl, p,
1583 extension_data_end);
1584 if (ret != 0) {
XiaokangQian40a35232022-05-07 09:02:40 +00001585 MBEDTLS_SSL_DEBUG_RET(
Gilles Peskine449bd832023-01-11 14:50:10 +01001586 1, "mbedtls_ssl_parse_servername_ext", ret);
1587 return ret;
XiaokangQian40a35232022-05-07 09:02:40 +00001588 }
XiaokangQian40a35232022-05-07 09:02:40 +00001589 break;
1590#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
1591
Przemek Stekiel8c0a9532023-06-15 16:48:19 +02001592#if defined(PSA_WANT_ALG_ECDH) || defined(PSA_WANT_ALG_FFDH)
XiaokangQian7807f9f2022-02-15 10:04:37 +00001593 case MBEDTLS_TLS_EXT_SUPPORTED_GROUPS:
Gilles Peskine449bd832023-01-11 14:50:10 +01001594 MBEDTLS_SSL_DEBUG_MSG(3, ("found supported group extension"));
XiaokangQian7807f9f2022-02-15 10:04:37 +00001595
1596 /* Supported Groups Extension
1597 *
1598 * When sent by the client, the "supported_groups" extension
1599 * indicates the named groups which the client supports,
1600 * ordered from most preferred to least preferred.
1601 */
Jerry Yuc1be19f2022-04-23 16:11:39 +08001602 ret = ssl_tls13_parse_supported_groups_ext(
Gilles Peskine449bd832023-01-11 14:50:10 +01001603 ssl, p, extension_data_end);
1604 if (ret != 0) {
Xiaokang Qian9f1747b2023-03-30 02:50:04 +00001605 MBEDTLS_SSL_DEBUG_RET(
Xiaokang Qian8bce0e62023-04-04 10:15:48 +00001606 1, "ssl_tls13_parse_supported_groups_ext", ret);
Gilles Peskine449bd832023-01-11 14:50:10 +01001607 return ret;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001608 }
1609
XiaokangQian7807f9f2022-02-15 10:04:37 +00001610 break;
Przemek Stekiel8c0a9532023-06-15 16:48:19 +02001611#endif /* PSA_WANT_ALG_ECDH || PSA_WANT_ALG_FFDH*/
XiaokangQian7807f9f2022-02-15 10:04:37 +00001612
Valerio Settic9ae8622023-07-25 11:23:50 +02001613#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_EPHEMERAL_ENABLED)
XiaokangQian7807f9f2022-02-15 10:04:37 +00001614 case MBEDTLS_TLS_EXT_KEY_SHARE:
Gilles Peskine449bd832023-01-11 14:50:10 +01001615 MBEDTLS_SSL_DEBUG_MSG(3, ("found key share extension"));
XiaokangQian7807f9f2022-02-15 10:04:37 +00001616
1617 /*
1618 * Key Share Extension
1619 *
1620 * When sent by the client, the "key_share" extension
1621 * contains the endpoint's cryptographic parameters for
1622 * ECDHE/DHE key establishment methods.
1623 */
Jerry Yuc1be19f2022-04-23 16:11:39 +08001624 ret = ssl_tls13_parse_key_shares_ext(
Gilles Peskine449bd832023-01-11 14:50:10 +01001625 ssl, p, extension_data_end);
1626 if (ret == SSL_TLS1_3_PARSE_KEY_SHARES_EXT_NO_MATCH) {
Ronald Cron8a74f072023-06-14 17:59:29 +02001627 MBEDTLS_SSL_DEBUG_MSG(2, ("No usable share for key agreement."));
1628 no_usable_share_for_key_agreement = 1;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001629 }
1630
Gilles Peskine449bd832023-01-11 14:50:10 +01001631 if (ret < 0) {
Jerry Yu582dd062022-04-22 21:59:01 +08001632 MBEDTLS_SSL_DEBUG_RET(
Gilles Peskine449bd832023-01-11 14:50:10 +01001633 1, "ssl_tls13_parse_key_shares_ext", ret);
1634 return ret;
Jerry Yu582dd062022-04-22 21:59:01 +08001635 }
XiaokangQian7807f9f2022-02-15 10:04:37 +00001636
XiaokangQian7807f9f2022-02-15 10:04:37 +00001637 break;
Valerio Settic9ae8622023-07-25 11:23:50 +02001638#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_EPHEMERAL_ENABLED */
XiaokangQian7807f9f2022-02-15 10:04:37 +00001639
1640 case MBEDTLS_TLS_EXT_SUPPORTED_VERSIONS:
Ronald Cron8c527d02023-03-07 15:47:47 +01001641 /* Already parsed */
XiaokangQian7807f9f2022-02-15 10:04:37 +00001642 break;
1643
Ronald Cron41a443a2022-10-04 16:38:25 +02001644#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED)
Jerry Yue19e3b92022-07-08 12:04:51 +00001645 case MBEDTLS_TLS_EXT_PSK_KEY_EXCHANGE_MODES:
Xiaokang Qian9f1747b2023-03-30 02:50:04 +00001646 MBEDTLS_SSL_DEBUG_MSG(
1647 3, ("found psk key exchange modes extension"));
Jerry Yue19e3b92022-07-08 12:04:51 +00001648
1649 ret = ssl_tls13_parse_key_exchange_modes_ext(
Gilles Peskine449bd832023-01-11 14:50:10 +01001650 ssl, p, extension_data_end);
1651 if (ret != 0) {
Jerry Yue19e3b92022-07-08 12:04:51 +00001652 MBEDTLS_SSL_DEBUG_RET(
Gilles Peskine449bd832023-01-11 14:50:10 +01001653 1, "ssl_tls13_parse_key_exchange_modes_ext", ret);
1654 return ret;
Jerry Yue19e3b92022-07-08 12:04:51 +00001655 }
1656
Jerry Yue19e3b92022-07-08 12:04:51 +00001657 break;
Ronald Cron41a443a2022-10-04 16:38:25 +02001658#endif
Jerry Yue19e3b92022-07-08 12:04:51 +00001659
Jerry Yu1c105562022-07-10 06:32:38 +00001660 case MBEDTLS_TLS_EXT_PRE_SHARED_KEY:
Gilles Peskine449bd832023-01-11 14:50:10 +01001661 MBEDTLS_SSL_DEBUG_MSG(3, ("found pre_shared_key extension"));
1662 if ((handshake->received_extensions &
1663 MBEDTLS_SSL_EXT_MASK(PSK_KEY_EXCHANGE_MODES)) == 0) {
Jerry Yu13ab81d2022-07-22 23:17:11 +08001664 MBEDTLS_SSL_PEND_FATAL_ALERT(
1665 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER,
Gilles Peskine449bd832023-01-11 14:50:10 +01001666 MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER);
1667 return MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER;
Jerry Yu13ab81d2022-07-22 23:17:11 +08001668 }
Ronald Cron41a443a2022-10-04 16:38:25 +02001669#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED)
Jerry Yu1c105562022-07-10 06:32:38 +00001670 /* Delay processing of the PSK identity once we have
1671 * found out which algorithms to use. We keep a pointer
1672 * to the buffer and the size for later processing.
1673 */
Jerry Yu29d9faa2022-08-23 17:52:45 +08001674 pre_shared_key_ext = p;
Jerry Yu1c105562022-07-10 06:32:38 +00001675 pre_shared_key_ext_end = extension_data_end;
Jerry Yue18dc7e2022-08-04 16:29:22 +08001676#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED */
Jerry Yu1c105562022-07-10 06:32:38 +00001677 break;
Jerry Yu1c105562022-07-10 06:32:38 +00001678
XiaokangQianacb39922022-06-17 10:18:48 +00001679#if defined(MBEDTLS_SSL_ALPN)
1680 case MBEDTLS_TLS_EXT_ALPN:
Gilles Peskine449bd832023-01-11 14:50:10 +01001681 MBEDTLS_SSL_DEBUG_MSG(3, ("found alpn extension"));
XiaokangQianacb39922022-06-17 10:18:48 +00001682
Gilles Peskine449bd832023-01-11 14:50:10 +01001683 ret = mbedtls_ssl_parse_alpn_ext(ssl, p, extension_data_end);
1684 if (ret != 0) {
XiaokangQianacb39922022-06-17 10:18:48 +00001685 MBEDTLS_SSL_DEBUG_RET(
Gilles Peskine449bd832023-01-11 14:50:10 +01001686 1, ("mbedtls_ssl_parse_alpn_ext"), ret);
1687 return ret;
XiaokangQianacb39922022-06-17 10:18:48 +00001688 }
XiaokangQianacb39922022-06-17 10:18:48 +00001689 break;
1690#endif /* MBEDTLS_SSL_ALPN */
1691
Ronald Cron928cbd32022-10-04 16:14:26 +02001692#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED)
XiaokangQian7807f9f2022-02-15 10:04:37 +00001693 case MBEDTLS_TLS_EXT_SIG_ALG:
Gilles Peskine449bd832023-01-11 14:50:10 +01001694 MBEDTLS_SSL_DEBUG_MSG(3, ("found signature_algorithms extension"));
XiaokangQian7807f9f2022-02-15 10:04:37 +00001695
Gabor Mezei078e8032022-04-27 21:17:56 +02001696 ret = mbedtls_ssl_parse_sig_alg_ext(
Gilles Peskine449bd832023-01-11 14:50:10 +01001697 ssl, p, extension_data_end);
1698 if (ret != 0) {
Xiaokang Qian49f39c12023-04-06 02:31:02 +00001699 MBEDTLS_SSL_DEBUG_RET(
1700 1, "mbedtls_ssl_parse_sig_alg_ext", ret);
Gilles Peskine449bd832023-01-11 14:50:10 +01001701 return ret;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001702 }
XiaokangQian7807f9f2022-02-15 10:04:37 +00001703 break;
Ronald Cron928cbd32022-10-04 16:14:26 +02001704#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED */
XiaokangQian7807f9f2022-02-15 10:04:37 +00001705
Jan Bruckner151f6422023-02-10 12:45:19 +01001706#if defined(MBEDTLS_SSL_RECORD_SIZE_LIMIT)
1707 case MBEDTLS_TLS_EXT_RECORD_SIZE_LIMIT:
1708 MBEDTLS_SSL_DEBUG_MSG(3, ("found record_size_limit extension"));
1709
Xiaokang Qian9f1747b2023-03-30 02:50:04 +00001710 ret = mbedtls_ssl_tls13_parse_record_size_limit_ext(
1711 ssl, p, extension_data_end);
Jan Brucknerf482dcc2023-03-15 09:09:06 +01001712 if (ret != 0) {
1713 MBEDTLS_SSL_DEBUG_RET(
1714 1, ("mbedtls_ssl_tls13_parse_record_size_limit_ext"), ret);
1715 return ret;
1716 }
Jan Bruckner151f6422023-02-10 12:45:19 +01001717 break;
1718#endif /* MBEDTLS_SSL_RECORD_SIZE_LIMIT */
1719
XiaokangQian7807f9f2022-02-15 10:04:37 +00001720 default:
Jerry Yu79aa7212022-11-08 21:30:21 +08001721 MBEDTLS_SSL_PRINT_EXT(
Jerry Yu63a459c2022-10-31 13:38:40 +08001722 3, MBEDTLS_SSL_HS_CLIENT_HELLO,
Gilles Peskine449bd832023-01-11 14:50:10 +01001723 extension_type, "( ignored )");
Jerry Yu0c354a22022-08-29 15:25:36 +08001724 break;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001725 }
1726
1727 p += extension_data_len;
1728 }
1729
Gilles Peskine449bd832023-01-11 14:50:10 +01001730 MBEDTLS_SSL_PRINT_EXTS(3, MBEDTLS_SSL_HS_CLIENT_HELLO,
1731 handshake->received_extensions);
Jerry Yue18dc7e2022-08-04 16:29:22 +08001732
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +01001733 ret = mbedtls_ssl_add_hs_hdr_to_checksum(ssl,
1734 MBEDTLS_SSL_HS_CLIENT_HELLO,
1735 p - buf);
1736 if (0 != ret) {
1737 MBEDTLS_SSL_DEBUG_RET(1, ("mbedtls_ssl_add_hs_hdr_to_checksum"), ret);
1738 return ret;
1739 }
Jerry Yu032b15ce2022-07-11 06:10:03 +00001740
Ronald Cron41a443a2022-10-04 16:38:25 +02001741#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED)
XiaokangQian7807f9f2022-02-15 10:04:37 +00001742 /* Update checksum with either
1743 * - The entire content of the CH message, if no PSK extension is present
1744 * - The content up to but excluding the PSK extension, if present.
1745 */
Jerry Yu1c105562022-07-10 06:32:38 +00001746 /* If we've settled on a PSK-based exchange, parse PSK identity ext */
Pengyu Lvbc4aab72023-12-01 15:37:24 +08001747 if (ssl_tls13_key_exchange_is_psk_available(ssl) ||
1748 ssl_tls13_key_exchange_is_psk_ephemeral_available(ssl)) {
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +01001749 ret = handshake->update_checksum(ssl, buf,
1750 pre_shared_key_ext - buf);
1751 if (0 != ret) {
1752 MBEDTLS_SSL_DEBUG_RET(1, ("update_checksum"), ret);
1753 return ret;
1754 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001755 ret = ssl_tls13_parse_pre_shared_key_ext(ssl,
1756 pre_shared_key_ext,
1757 pre_shared_key_ext_end,
1758 cipher_suites,
1759 cipher_suites_end);
1760 if (ret == MBEDTLS_ERR_SSL_UNKNOWN_IDENTITY) {
1761 handshake->received_extensions &= ~MBEDTLS_SSL_EXT_MASK(PRE_SHARED_KEY);
1762 } else if (ret != 0) {
Jerry Yu63a459c2022-10-31 13:38:40 +08001763 MBEDTLS_SSL_DEBUG_RET(
Gilles Peskine449bd832023-01-11 14:50:10 +01001764 1, "ssl_tls13_parse_pre_shared_key_ext", ret);
1765 return ret;
Jerry Yu1c105562022-07-10 06:32:38 +00001766 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001767 } else
Ronald Cron41a443a2022-10-04 16:38:25 +02001768#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED */
Jerry Yu1c105562022-07-10 06:32:38 +00001769 {
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +01001770 ret = handshake->update_checksum(ssl, buf, p - buf);
1771 if (0 != ret) {
1772 MBEDTLS_SSL_DEBUG_RET(1, ("update_checksum"), ret);
1773 return ret;
1774 }
Jerry Yu1c105562022-07-10 06:32:38 +00001775 }
XiaokangQian7807f9f2022-02-15 10:04:37 +00001776
Gilles Peskine449bd832023-01-11 14:50:10 +01001777 ret = ssl_tls13_determine_key_exchange_mode(ssl);
1778 if (ret < 0) {
1779 return ret;
1780 }
XiaokangQian7807f9f2022-02-15 10:04:37 +00001781
Ronald Cron8a74f072023-06-14 17:59:29 +02001782 if (ssl->handshake->key_exchange_mode !=
1783 MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK) {
1784 hrr_required = (no_usable_share_for_key_agreement != 0);
1785 }
1786
Gilles Peskine449bd832023-01-11 14:50:10 +01001787 mbedtls_ssl_optimize_checksum(ssl, handshake->ciphersuite_info);
Jerry Yue5834fd2022-08-29 20:16:09 +08001788
Gilles Peskine449bd832023-01-11 14:50:10 +01001789 return hrr_required ? SSL_CLIENT_HELLO_HRR_REQUIRED : SSL_CLIENT_HELLO_OK;
XiaokangQian75fe8c72022-06-15 09:42:45 +00001790}
1791
Jerry Yuab0da372023-02-08 13:55:24 +08001792#if defined(MBEDTLS_SSL_EARLY_DATA)
Jerry Yu4caf3ca2023-11-15 16:13:47 +08001793static int ssl_tls13_check_early_data_requirements(mbedtls_ssl_context *ssl)
Jerry Yuab0da372023-02-08 13:55:24 +08001794{
1795 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
1796
Jerry Yu985c9672022-12-04 14:06:30 +08001797 if (ssl->conf->early_data_enabled == MBEDTLS_SSL_EARLY_DATA_DISABLED) {
1798 MBEDTLS_SSL_DEBUG_MSG(
Jerry Yu985c9672022-12-04 14:06:30 +08001799 1,
Jerry Yu454dda32023-10-31 15:13:54 +08001800 ("EarlyData: rejected, feature disabled in server configuration."));
Jerry Yu4caf3ca2023-11-15 16:13:47 +08001801 return -1;
Ronald Cron7b6ee942024-01-12 10:29:55 +01001802 }
1803
Jerry Yu454dda32023-10-31 15:13:54 +08001804 if (!handshake->resume) {
1805 /* We currently support early data only in the case of PSKs established
1806 via a NewSessionTicket message thus in the case of a session
1807 resumption. */
Jerry Yu985c9672022-12-04 14:06:30 +08001808 MBEDTLS_SSL_DEBUG_MSG(
Jerry Yu7ef9fd82023-11-07 14:30:38 +08001809 1, ("EarlyData: rejected, not a session resumption."));
Jerry Yu4caf3ca2023-11-15 16:13:47 +08001810 return -1;
Jerry Yu985c9672022-12-04 14:06:30 +08001811 }
1812
Jerry Yu82fd6c12023-10-31 16:32:19 +08001813 /* RFC 8446 4.2.10
1814 *
1815 * In order to accept early data, the server MUST have accepted a PSK cipher
1816 * suite and selected the first key offered in the client's "pre_shared_key"
1817 * extension. In addition, it MUST verify that the following values are the
1818 * same as those associated with the selected PSK:
1819 * - The TLS version number
1820 * - The selected cipher suite
1821 * - The selected ALPN [RFC7301] protocol, if any
1822 *
1823 * NOTE:
Jerry Yu7ef9fd82023-11-07 14:30:38 +08001824 * - The TLS version number is checked in
1825 * ssl_tls13_offered_psks_check_identity_match_ticket().
1826 * - ALPN is not checked for the time being (TODO).
Jerry Yu82fd6c12023-10-31 16:32:19 +08001827 */
1828
1829 if (handshake->selected_identity != 0) {
1830 MBEDTLS_SSL_DEBUG_MSG(
Jerry Yu7ef9fd82023-11-07 14:30:38 +08001831 1, ("EarlyData: rejected, the selected key in "
1832 "`pre_shared_key` is not the first one."));
Jerry Yu4caf3ca2023-11-15 16:13:47 +08001833 return -1;
Jerry Yu82fd6c12023-10-31 16:32:19 +08001834 }
1835
1836 if (handshake->ciphersuite_info->id !=
1837 ssl->session_negotiate->ciphersuite) {
1838 MBEDTLS_SSL_DEBUG_MSG(
Jerry Yu7ef9fd82023-11-07 14:30:38 +08001839 1, ("EarlyData: rejected, the selected ciphersuite is not the one "
1840 "of the selected pre-shared key."));
Jerry Yu4caf3ca2023-11-15 16:13:47 +08001841 return -1;
Jerry Yu82fd6c12023-10-31 16:32:19 +08001842
1843 }
Jerry Yu985c9672022-12-04 14:06:30 +08001844
Pengyu Lv94a42cc2023-12-06 10:04:17 +08001845 if (!mbedtls_ssl_tls13_session_ticket_allow_early_data(ssl->session_negotiate)) {
Jerry Yufceddb32022-12-12 15:30:34 +08001846 MBEDTLS_SSL_DEBUG_MSG(
1847 1,
Jerry Yuea96ac32023-11-21 17:06:36 +08001848 ("EarlyData: rejected, early_data not allowed in ticket "
1849 "permission bits."));
Jerry Yu4caf3ca2023-11-15 16:13:47 +08001850 return -1;
Jerry Yufceddb32022-12-12 15:30:34 +08001851 }
Jerry Yu985c9672022-12-04 14:06:30 +08001852
Jerry Yu4caf3ca2023-11-15 16:13:47 +08001853 return 0;
Jerry Yuab0da372023-02-08 13:55:24 +08001854}
1855#endif /* MBEDTLS_SSL_EARLY_DATA */
1856
XiaokangQian75fe8c72022-06-15 09:42:45 +00001857/* Update the handshake state machine */
1858
Ronald Cronce7d76e2022-07-08 18:56:49 +02001859MBEDTLS_CHECK_RETURN_CRITICAL
Ronald Cron7b6ee942024-01-12 10:29:55 +01001860static int ssl_tls13_postprocess_client_hello(mbedtls_ssl_context *ssl,
1861 int hrr_required)
XiaokangQian75fe8c72022-06-15 09:42:45 +00001862{
1863 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1864
XiaokangQian7807f9f2022-02-15 10:04:37 +00001865 /*
XiaokangQianfb665a82022-06-15 03:57:21 +00001866 * Server certificate selection
1867 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001868 if (ssl->conf->f_cert_cb && (ret = ssl->conf->f_cert_cb(ssl)) != 0) {
1869 MBEDTLS_SSL_DEBUG_RET(1, "f_cert_cb", ret);
1870 return ret;
XiaokangQianfb665a82022-06-15 03:57:21 +00001871 }
1872#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
1873 ssl->handshake->sni_name = NULL;
1874 ssl->handshake->sni_name_len = 0;
1875#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
XiaokangQian7807f9f2022-02-15 10:04:37 +00001876
Gilles Peskine449bd832023-01-11 14:50:10 +01001877 ret = mbedtls_ssl_tls13_key_schedule_stage_early(ssl);
1878 if (ret != 0) {
1879 MBEDTLS_SSL_DEBUG_RET(1,
1880 "mbedtls_ssl_tls1_3_key_schedule_stage_early", ret);
1881 return ret;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001882 }
1883
Jerry Yuab0da372023-02-08 13:55:24 +08001884#if defined(MBEDTLS_SSL_EARLY_DATA)
Jerry Yu4caf3ca2023-11-15 16:13:47 +08001885 if (ssl->handshake->received_extensions & MBEDTLS_SSL_EXT_MASK(EARLY_DATA)) {
Ronald Cron31e2d832024-02-05 16:45:57 +01001886 ssl->handshake->early_data_accepted =
1887 (!hrr_required) && (ssl_tls13_check_early_data_requirements(ssl) == 0);
Jerry Yu4e9b70e2022-12-04 14:08:02 +08001888
Jerry Yu4caf3ca2023-11-15 16:13:47 +08001889 if (ssl->handshake->early_data_accepted) {
1890 ret = mbedtls_ssl_tls13_compute_early_transform(ssl);
1891 if (ret != 0) {
1892 MBEDTLS_SSL_DEBUG_RET(
1893 1, "mbedtls_ssl_tls13_compute_early_transform", ret);
1894 return ret;
1895 }
1896 } else {
1897 ssl->discard_early_data_record =
1898 hrr_required ?
1899 MBEDTLS_SSL_EARLY_DATA_DISCARD :
1900 MBEDTLS_SSL_EARLY_DATA_TRY_TO_DEPROTECT_AND_DISCARD;
Jerry Yu4e9b70e2022-12-04 14:08:02 +08001901 }
1902 }
Ronald Cron7b6ee942024-01-12 10:29:55 +01001903#else
1904 ((void) hrr_required);
Jerry Yuab0da372023-02-08 13:55:24 +08001905#endif /* MBEDTLS_SSL_EARLY_DATA */
1906
Gilles Peskine449bd832023-01-11 14:50:10 +01001907 return 0;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001908}
1909
1910/*
XiaokangQianed582dd2022-04-13 08:21:05 +00001911 * Main entry point from the state machine; orchestrates the otherfunctions.
1912 */
1913
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001914MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01001915static int ssl_tls13_process_client_hello(mbedtls_ssl_context *ssl)
XiaokangQianed582dd2022-04-13 08:21:05 +00001916{
1917
XiaokangQian08037552022-04-20 07:16:41 +00001918 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine449bd832023-01-11 14:50:10 +01001919 unsigned char *buf = NULL;
XiaokangQianed582dd2022-04-13 08:21:05 +00001920 size_t buflen = 0;
Jerry Yu4ca91402022-05-09 15:50:57 +08001921 int parse_client_hello_ret;
1922
Gilles Peskine449bd832023-01-11 14:50:10 +01001923 MBEDTLS_SSL_DEBUG_MSG(2, ("=> parse client hello"));
XiaokangQianed582dd2022-04-13 08:21:05 +00001924
Gilles Peskine449bd832023-01-11 14:50:10 +01001925 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_tls13_fetch_handshake_msg(
1926 ssl, MBEDTLS_SSL_HS_CLIENT_HELLO,
1927 &buf, &buflen));
XiaokangQianed582dd2022-04-13 08:21:05 +00001928
Gilles Peskine449bd832023-01-11 14:50:10 +01001929 MBEDTLS_SSL_PROC_CHK_NEG(ssl_tls13_parse_client_hello(ssl, buf,
1930 buf + buflen));
Ronald Cron5af4c7f2023-03-07 20:46:59 +01001931 parse_client_hello_ret = ret; /* Store positive return value of
1932 * parse_client_hello,
1933 * as negative error codes are handled
Jerry Yuf41553b2022-05-09 22:20:30 +08001934 * by MBEDTLS_SSL_PROC_CHK_NEG. */
Jerry Yu4ca91402022-05-09 15:50:57 +08001935
Ronald Cronb828c7d2023-04-03 16:37:22 +02001936 /*
Yanray Wang90acdc62023-12-08 10:29:42 +08001937 * Version 1.2 of the protocol has to be used for the handshake.
1938 * If TLS 1.2 is not supported, abort the handshake. Otherwise, set the
Ronald Cronb828c7d2023-04-03 16:37:22 +02001939 * ssl->keep_current_message flag for the ClientHello to be kept and parsed
1940 * as a TLS 1.2 ClientHello. We also change ssl->tls_version to
1941 * MBEDTLS_SSL_VERSION_TLS1_2 thus from now on mbedtls_ssl_handshake_step()
1942 * will dispatch to the TLS 1.2 state machine.
1943 */
Ronald Cron5af4c7f2023-03-07 20:46:59 +01001944 if (SSL_CLIENT_HELLO_TLS1_2 == parse_client_hello_ret) {
Yanray Wangfb0f47b2023-12-04 15:27:28 +08001945 /* Check if server supports TLS 1.2 */
Yanray Wang408ba6f2023-12-08 10:18:03 +08001946 if (!mbedtls_ssl_conf_is_tls12_enabled(ssl->conf)) {
Yanray Wangfb0f47b2023-12-04 15:27:28 +08001947 MBEDTLS_SSL_DEBUG_MSG(
Yanray Wang177e49a2023-12-08 10:51:04 +08001948 1, ("TLS 1.2 not supported."));
Yanray Wangfb0f47b2023-12-04 15:27:28 +08001949 MBEDTLS_SSL_PEND_FATAL_ALERT(
Yanray Wang2bef9172023-12-08 10:21:53 +08001950 MBEDTLS_SSL_ALERT_MSG_PROTOCOL_VERSION,
1951 MBEDTLS_ERR_SSL_BAD_PROTOCOL_VERSION);
1952 return MBEDTLS_ERR_SSL_BAD_PROTOCOL_VERSION;
Yanray Wangfb0f47b2023-12-04 15:27:28 +08001953 }
Ronald Cron5af4c7f2023-03-07 20:46:59 +01001954 ssl->keep_current_message = 1;
1955 ssl->tls_version = MBEDTLS_SSL_VERSION_TLS1_2;
1956 return 0;
1957 }
1958
Ronald Cron7b6ee942024-01-12 10:29:55 +01001959 MBEDTLS_SSL_PROC_CHK(
1960 ssl_tls13_postprocess_client_hello(ssl, parse_client_hello_ret ==
1961 SSL_CLIENT_HELLO_HRR_REQUIRED));
Jerry Yu582dd062022-04-22 21:59:01 +08001962
Ronald Cron5af4c7f2023-03-07 20:46:59 +01001963 if (SSL_CLIENT_HELLO_OK == parse_client_hello_ret) {
Gilles Peskine449bd832023-01-11 14:50:10 +01001964 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_SERVER_HELLO);
1965 } else {
1966 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_HELLO_RETRY_REQUEST);
1967 }
XiaokangQianed582dd2022-04-13 08:21:05 +00001968
1969cleanup:
1970
Gilles Peskine449bd832023-01-11 14:50:10 +01001971 MBEDTLS_SSL_DEBUG_MSG(2, ("<= parse client hello"));
1972 return ret;
XiaokangQianed582dd2022-04-13 08:21:05 +00001973}
1974
1975/*
Jerry Yu1c3e6882022-04-20 21:23:40 +08001976 * Handler for MBEDTLS_SSL_SERVER_HELLO
Jerry Yu5b64ae92022-03-30 17:15:02 +08001977 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001978MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01001979static int ssl_tls13_prepare_server_hello(mbedtls_ssl_context *ssl)
Jerry Yuf4b27e42022-03-30 17:32:21 +08001980{
Jerry Yu637a3f12022-04-20 21:37:58 +08001981 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1982 unsigned char *server_randbytes =
Gilles Peskine449bd832023-01-11 14:50:10 +01001983 ssl->handshake->randbytes + MBEDTLS_CLIENT_HELLO_RANDOM_LEN;
1984 if (ssl->conf->f_rng == NULL) {
1985 MBEDTLS_SSL_DEBUG_MSG(1, ("no RNG provided"));
1986 return MBEDTLS_ERR_SSL_NO_RNG;
Jerry Yuf4b27e42022-03-30 17:32:21 +08001987 }
1988
Gilles Peskine449bd832023-01-11 14:50:10 +01001989 if ((ret = ssl->conf->f_rng(ssl->conf->p_rng, server_randbytes,
1990 MBEDTLS_SERVER_HELLO_RANDOM_LEN)) != 0) {
1991 MBEDTLS_SSL_DEBUG_RET(1, "f_rng", ret);
1992 return ret;
Jerry Yuf4b27e42022-03-30 17:32:21 +08001993 }
1994
Gilles Peskine449bd832023-01-11 14:50:10 +01001995 MBEDTLS_SSL_DEBUG_BUF(3, "server hello, random bytes", server_randbytes,
1996 MBEDTLS_SERVER_HELLO_RANDOM_LEN);
Jerry Yuf4b27e42022-03-30 17:32:21 +08001997
1998#if defined(MBEDTLS_HAVE_TIME)
YxCda609132023-05-22 12:08:12 -07001999 ssl->session_negotiate->start = mbedtls_time(NULL);
Jerry Yuf4b27e42022-03-30 17:32:21 +08002000#endif /* MBEDTLS_HAVE_TIME */
2001
Gilles Peskine449bd832023-01-11 14:50:10 +01002002 return ret;
Jerry Yuf4b27e42022-03-30 17:32:21 +08002003}
2004
Jerry Yu3bf2c642022-03-30 22:02:12 +08002005/*
Jerry Yue74e04a2022-04-21 09:23:16 +08002006 * ssl_tls13_write_server_hello_supported_versions_ext ():
Jerry Yu3bf2c642022-03-30 22:02:12 +08002007 *
2008 * struct {
Jerry Yufb9f54d2022-04-06 10:08:34 +08002009 * ProtocolVersion selected_version;
Jerry Yu3bf2c642022-03-30 22:02:12 +08002010 * } SupportedVersions;
2011 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002012MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yue74e04a2022-04-21 09:23:16 +08002013static int ssl_tls13_write_server_hello_supported_versions_ext(
Gilles Peskine449bd832023-01-11 14:50:10 +01002014 mbedtls_ssl_context *ssl,
2015 unsigned char *buf,
2016 unsigned char *end,
2017 size_t *out_len)
Jerry Yu3bf2c642022-03-30 22:02:12 +08002018{
Jerry Yu3bf2c642022-03-30 22:02:12 +08002019 *out_len = 0;
2020
Gilles Peskine449bd832023-01-11 14:50:10 +01002021 MBEDTLS_SSL_DEBUG_MSG(3, ("server hello, write selected version"));
Jerry Yu3bf2c642022-03-30 22:02:12 +08002022
2023 /* Check if we have space to write the extension:
2024 * - extension_type (2 bytes)
2025 * - extension_data_length (2 bytes)
Jerry Yu349a6132022-04-14 20:52:56 +08002026 * - selected_version (2 bytes)
Jerry Yu3bf2c642022-03-30 22:02:12 +08002027 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002028 MBEDTLS_SSL_CHK_BUF_PTR(buf, end, 6);
Jerry Yu3bf2c642022-03-30 22:02:12 +08002029
Gilles Peskine449bd832023-01-11 14:50:10 +01002030 MBEDTLS_PUT_UINT16_BE(MBEDTLS_TLS_EXT_SUPPORTED_VERSIONS, buf, 0);
Jerry Yu3bf2c642022-03-30 22:02:12 +08002031
Gilles Peskine449bd832023-01-11 14:50:10 +01002032 MBEDTLS_PUT_UINT16_BE(2, buf, 2);
Jerry Yu3bf2c642022-03-30 22:02:12 +08002033
Gilles Peskine449bd832023-01-11 14:50:10 +01002034 mbedtls_ssl_write_version(buf + 4,
2035 ssl->conf->transport,
2036 ssl->tls_version);
Jerry Yu3bf2c642022-03-30 22:02:12 +08002037
Gilles Peskine449bd832023-01-11 14:50:10 +01002038 MBEDTLS_SSL_DEBUG_MSG(3, ("supported version: [%04x]",
2039 ssl->tls_version));
Jerry Yu3bf2c642022-03-30 22:02:12 +08002040
Jerry Yu349a6132022-04-14 20:52:56 +08002041 *out_len = 6;
Jerry Yu3bf2c642022-03-30 22:02:12 +08002042
Jerry Yub95dd362022-11-08 21:19:34 +08002043 mbedtls_ssl_tls13_set_hs_sent_ext_mask(
Gilles Peskine449bd832023-01-11 14:50:10 +01002044 ssl, MBEDTLS_TLS_EXT_SUPPORTED_VERSIONS);
Jerry Yub95dd362022-11-08 21:19:34 +08002045
Gilles Peskine449bd832023-01-11 14:50:10 +01002046 return 0;
Jerry Yu3bf2c642022-03-30 22:02:12 +08002047}
2048
Jerry Yud9436a12022-04-20 22:28:09 +08002049
Jerry Yu3bf2c642022-03-30 22:02:12 +08002050
2051/* Generate and export a single key share. For hybrid KEMs, this can
2052 * be called multiple times with the different components of the hybrid. */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002053MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002054static int ssl_tls13_generate_and_write_key_share(mbedtls_ssl_context *ssl,
2055 uint16_t named_group,
2056 unsigned char *buf,
2057 unsigned char *end,
2058 size_t *out_len)
Jerry Yu3bf2c642022-03-30 22:02:12 +08002059{
2060 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yu955ddd72022-04-22 22:27:33 +08002061
2062 *out_len = 0;
2063
Valerio Settic9ae8622023-07-25 11:23:50 +02002064#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_EPHEMERAL_ENABLED)
Przemek Stekiel29c219c2023-05-31 15:21:04 +02002065 if (mbedtls_ssl_tls13_named_group_is_ecdhe(named_group) ||
Przemek Stekield5f79e72023-06-29 09:08:43 +02002066 mbedtls_ssl_tls13_named_group_is_ffdh(named_group)) {
Przemek Stekiel408569f2023-07-06 11:26:44 +02002067 ret = mbedtls_ssl_tls13_generate_and_write_xxdh_key_exchange(
Gilles Peskine449bd832023-01-11 14:50:10 +01002068 ssl, named_group, buf, end, out_len);
2069 if (ret != 0) {
Jerry Yu89e103c2022-03-30 22:43:29 +08002070 MBEDTLS_SSL_DEBUG_RET(
Przemek Stekiel408569f2023-07-06 11:26:44 +02002071 1, "mbedtls_ssl_tls13_generate_and_write_xxdh_key_exchange",
Gilles Peskine449bd832023-01-11 14:50:10 +01002072 ret);
2073 return ret;
Jerry Yu3bf2c642022-03-30 22:02:12 +08002074 }
Gilles Peskine449bd832023-01-11 14:50:10 +01002075 } else
Valerio Settic9ae8622023-07-25 11:23:50 +02002076#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_EPHEMERAL_ENABLED */
Gilles Peskine449bd832023-01-11 14:50:10 +01002077 if (0 /* Other kinds of KEMs */) {
2078 } else {
Jerry Yu955ddd72022-04-22 22:27:33 +08002079 ((void) ssl);
2080 ((void) named_group);
2081 ((void) buf);
2082 ((void) end);
Jerry Yu3bf2c642022-03-30 22:02:12 +08002083 ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
2084 }
2085
Gilles Peskine449bd832023-01-11 14:50:10 +01002086 return ret;
Jerry Yu3bf2c642022-03-30 22:02:12 +08002087}
2088
2089/*
2090 * ssl_tls13_write_key_share_ext
2091 *
2092 * Structure of key_share extension in ServerHello:
2093 *
Jerry Yu1c3e6882022-04-20 21:23:40 +08002094 * struct {
2095 * NamedGroup group;
2096 * opaque key_exchange<1..2^16-1>;
2097 * } KeyShareEntry;
2098 * struct {
2099 * KeyShareEntry server_share;
2100 * } KeyShareServerHello;
Jerry Yu3bf2c642022-03-30 22:02:12 +08002101 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002102MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002103static int ssl_tls13_write_key_share_ext(mbedtls_ssl_context *ssl,
2104 unsigned char *buf,
2105 unsigned char *end,
2106 size_t *out_len)
Jerry Yu3bf2c642022-03-30 22:02:12 +08002107{
Jerry Yu955ddd72022-04-22 22:27:33 +08002108 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yu3bf2c642022-03-30 22:02:12 +08002109 unsigned char *p = buf;
Jerry Yu57d48412022-04-20 21:50:42 +08002110 uint16_t group = ssl->handshake->offered_group_id;
Jerry Yu3bf2c642022-03-30 22:02:12 +08002111 unsigned char *server_share = buf + 4;
Jerry Yu3bf2c642022-03-30 22:02:12 +08002112 size_t key_exchange_length;
Jerry Yu3bf2c642022-03-30 22:02:12 +08002113
2114 *out_len = 0;
2115
Gilles Peskine449bd832023-01-11 14:50:10 +01002116 MBEDTLS_SSL_DEBUG_MSG(3, ("server hello, adding key share extension"));
Jerry Yu3bf2c642022-03-30 22:02:12 +08002117
Gilles Peskine449bd832023-01-11 14:50:10 +01002118 MBEDTLS_SSL_DEBUG_MSG(2, ("server hello, write selected_group: %s (%04x)",
2119 mbedtls_ssl_named_group_to_str(group),
2120 group));
Jerry Yu58af2332022-09-06 11:19:31 +08002121
Jerry Yu3bf2c642022-03-30 22:02:12 +08002122 /* Check if we have space for header and length fields:
2123 * - extension_type (2 bytes)
2124 * - extension_data_length (2 bytes)
2125 * - group (2 bytes)
2126 * - key_exchange_length (2 bytes)
2127 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002128 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 8);
2129 MBEDTLS_PUT_UINT16_BE(MBEDTLS_TLS_EXT_KEY_SHARE, p, 0);
2130 MBEDTLS_PUT_UINT16_BE(group, server_share, 0);
Jerry Yu3bf2c642022-03-30 22:02:12 +08002131 p += 8;
Jerry Yu57d48412022-04-20 21:50:42 +08002132
Jerry Yu3bf2c642022-03-30 22:02:12 +08002133 /* When we introduce PQC-ECDHE hybrids, we'll want to call this
2134 * function multiple times. */
Jerry Yu955ddd72022-04-22 22:27:33 +08002135 ret = ssl_tls13_generate_and_write_key_share(
Gilles Peskine449bd832023-01-11 14:50:10 +01002136 ssl, group, server_share + 4, end, &key_exchange_length);
2137 if (ret != 0) {
2138 return ret;
2139 }
Jerry Yu3bf2c642022-03-30 22:02:12 +08002140 p += key_exchange_length;
Jerry Yuc1be19f2022-04-23 16:11:39 +08002141
Gilles Peskine449bd832023-01-11 14:50:10 +01002142 MBEDTLS_PUT_UINT16_BE(key_exchange_length, server_share + 2, 0);
Jerry Yu3bf2c642022-03-30 22:02:12 +08002143
Gilles Peskine449bd832023-01-11 14:50:10 +01002144 MBEDTLS_PUT_UINT16_BE(p - server_share, buf, 2);
Jerry Yu3bf2c642022-03-30 22:02:12 +08002145
Jerry Yu57d48412022-04-20 21:50:42 +08002146 *out_len = p - buf;
Jerry Yu955ddd72022-04-22 22:27:33 +08002147
Gilles Peskine449bd832023-01-11 14:50:10 +01002148 mbedtls_ssl_tls13_set_hs_sent_ext_mask(ssl, MBEDTLS_TLS_EXT_KEY_SHARE);
Jerry Yub95dd362022-11-08 21:19:34 +08002149
Gilles Peskine449bd832023-01-11 14:50:10 +01002150 return 0;
Jerry Yu3bf2c642022-03-30 22:02:12 +08002151}
Jerry Yud9436a12022-04-20 22:28:09 +08002152
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002153MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002154static int ssl_tls13_write_hrr_key_share_ext(mbedtls_ssl_context *ssl,
2155 unsigned char *buf,
2156 unsigned char *end,
2157 size_t *out_len)
Jerry Yu23f7a6f2022-04-23 15:16:45 +08002158{
2159 uint16_t selected_group = ssl->handshake->hrr_selected_group;
2160 /* key_share Extension
2161 *
2162 * struct {
2163 * select (Handshake.msg_type) {
2164 * ...
2165 * case hello_retry_request:
2166 * NamedGroup selected_group;
2167 * ...
2168 * };
2169 * } KeyShare;
2170 */
2171
2172 *out_len = 0;
2173
Jerry Yub0ac10b2022-05-05 11:10:08 +08002174 /*
2175 * For a pure PSK key exchange, there is no group to agree upon. The purpose
2176 * of the HRR is then to transmit a cookie to force the client to demonstrate
2177 * reachability at their apparent network address (primarily useful for DTLS).
2178 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002179 if (!mbedtls_ssl_tls13_key_exchange_mode_with_ephemeral(ssl)) {
2180 return 0;
2181 }
Jerry Yu23f7a6f2022-04-23 15:16:45 +08002182
2183 /* We should only send the key_share extension if the client's initial
2184 * key share was not acceptable. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002185 if (ssl->handshake->offered_group_id != 0) {
2186 MBEDTLS_SSL_DEBUG_MSG(4, ("Skip key_share extension in HRR"));
2187 return 0;
Jerry Yu23f7a6f2022-04-23 15:16:45 +08002188 }
2189
Gilles Peskine449bd832023-01-11 14:50:10 +01002190 if (selected_group == 0) {
2191 MBEDTLS_SSL_DEBUG_MSG(1, ("no matching named group found"));
2192 return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
Jerry Yu23f7a6f2022-04-23 15:16:45 +08002193 }
2194
Jerry Yub0ac10b2022-05-05 11:10:08 +08002195 /* Check if we have enough space:
2196 * - extension_type (2 bytes)
2197 * - extension_data_length (2 bytes)
2198 * - selected_group (2 bytes)
2199 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002200 MBEDTLS_SSL_CHK_BUF_PTR(buf, end, 6);
Jerry Yu23f7a6f2022-04-23 15:16:45 +08002201
Gilles Peskine449bd832023-01-11 14:50:10 +01002202 MBEDTLS_PUT_UINT16_BE(MBEDTLS_TLS_EXT_KEY_SHARE, buf, 0);
2203 MBEDTLS_PUT_UINT16_BE(2, buf, 2);
2204 MBEDTLS_PUT_UINT16_BE(selected_group, buf, 4);
Jerry Yu23f7a6f2022-04-23 15:16:45 +08002205
Gilles Peskine449bd832023-01-11 14:50:10 +01002206 MBEDTLS_SSL_DEBUG_MSG(3,
2207 ("HRR selected_group: %s (%x)",
2208 mbedtls_ssl_named_group_to_str(selected_group),
2209 selected_group));
Jerry Yu23f7a6f2022-04-23 15:16:45 +08002210
2211 *out_len = 6;
Jerry Yu23f7a6f2022-04-23 15:16:45 +08002212
Gilles Peskine449bd832023-01-11 14:50:10 +01002213 mbedtls_ssl_tls13_set_hs_sent_ext_mask(ssl, MBEDTLS_TLS_EXT_KEY_SHARE);
Jerry Yub95dd362022-11-08 21:19:34 +08002214
Gilles Peskine449bd832023-01-11 14:50:10 +01002215 return 0;
Jerry Yu23f7a6f2022-04-23 15:16:45 +08002216}
Jerry Yu3bf2c642022-03-30 22:02:12 +08002217
2218/*
2219 * Structure of ServerHello message:
2220 *
2221 * struct {
2222 * ProtocolVersion legacy_version = 0x0303; // TLS v1.2
2223 * Random random;
2224 * opaque legacy_session_id_echo<0..32>;
2225 * CipherSuite cipher_suite;
2226 * uint8 legacy_compression_method = 0;
2227 * Extension extensions<6..2^16-1>;
2228 * } ServerHello;
2229 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002230MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002231static int ssl_tls13_write_server_hello_body(mbedtls_ssl_context *ssl,
2232 unsigned char *buf,
2233 unsigned char *end,
2234 size_t *out_len,
2235 int is_hrr)
Jerry Yu56404d72022-03-30 17:36:13 +08002236{
Jerry Yu955ddd72022-04-22 22:27:33 +08002237 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yu3bf2c642022-03-30 22:02:12 +08002238 unsigned char *p = buf;
Jerry Yud9436a12022-04-20 22:28:09 +08002239 unsigned char *p_extensions_len;
Jerry Yufbe3e642022-04-25 19:31:51 +08002240 size_t output_len;
Jerry Yu3bf2c642022-03-30 22:02:12 +08002241
2242 *out_len = 0;
Jerry Yu50e00e32022-10-31 14:45:01 +08002243 ssl->handshake->sent_extensions = MBEDTLS_SSL_EXT_MASK_NONE;
Jerry Yu3bf2c642022-03-30 22:02:12 +08002244
Jerry Yucfc04b32022-04-21 09:31:58 +08002245 /* ...
2246 * ProtocolVersion legacy_version = 0x0303; // TLS 1.2
2247 * ...
2248 * with ProtocolVersion defined as:
2249 * uint16 ProtocolVersion;
Jerry Yu3bf2c642022-03-30 22:02:12 +08002250 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002251 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 2);
2252 MBEDTLS_PUT_UINT16_BE(0x0303, p, 0);
Jerry Yu3bf2c642022-03-30 22:02:12 +08002253 p += 2;
2254
Jerry Yu1c3e6882022-04-20 21:23:40 +08002255 /* ...
2256 * Random random;
2257 * ...
Jerry Yucfc04b32022-04-21 09:31:58 +08002258 * with Random defined as:
2259 * opaque Random[MBEDTLS_SERVER_HELLO_RANDOM_LEN];
Jerry Yu1c3e6882022-04-20 21:23:40 +08002260 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002261 MBEDTLS_SSL_CHK_BUF_PTR(p, end, MBEDTLS_SERVER_HELLO_RANDOM_LEN);
2262 if (is_hrr) {
2263 memcpy(p, mbedtls_ssl_tls13_hello_retry_request_magic,
2264 MBEDTLS_SERVER_HELLO_RANDOM_LEN);
2265 } else {
2266 memcpy(p, &ssl->handshake->randbytes[MBEDTLS_CLIENT_HELLO_RANDOM_LEN],
2267 MBEDTLS_SERVER_HELLO_RANDOM_LEN);
Jerry Yu23f7a6f2022-04-23 15:16:45 +08002268 }
Gilles Peskine449bd832023-01-11 14:50:10 +01002269 MBEDTLS_SSL_DEBUG_BUF(3, "server hello, random bytes",
2270 p, MBEDTLS_SERVER_HELLO_RANDOM_LEN);
Jerry Yu3bf2c642022-03-30 22:02:12 +08002271 p += MBEDTLS_SERVER_HELLO_RANDOM_LEN;
2272
Jerry Yucfc04b32022-04-21 09:31:58 +08002273 /* ...
2274 * opaque legacy_session_id_echo<0..32>;
2275 * ...
Jerry Yu3bf2c642022-03-30 22:02:12 +08002276 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002277 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 1 + ssl->session_negotiate->id_len);
2278 *p++ = (unsigned char) ssl->session_negotiate->id_len;
2279 if (ssl->session_negotiate->id_len > 0) {
2280 memcpy(p, &ssl->session_negotiate->id[0],
2281 ssl->session_negotiate->id_len);
Jerry Yu3bf2c642022-03-30 22:02:12 +08002282 p += ssl->session_negotiate->id_len;
Jerry Yu955ddd72022-04-22 22:27:33 +08002283
Gilles Peskine449bd832023-01-11 14:50:10 +01002284 MBEDTLS_SSL_DEBUG_BUF(3, "session id", ssl->session_negotiate->id,
2285 ssl->session_negotiate->id_len);
Jerry Yu3bf2c642022-03-30 22:02:12 +08002286 }
2287
Jerry Yucfc04b32022-04-21 09:31:58 +08002288 /* ...
2289 * CipherSuite cipher_suite;
2290 * ...
2291 * with CipherSuite defined as:
2292 * uint8 CipherSuite[2];
Jerry Yu3bf2c642022-03-30 22:02:12 +08002293 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002294 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 2);
2295 MBEDTLS_PUT_UINT16_BE(ssl->session_negotiate->ciphersuite, p, 0);
Jerry Yu3bf2c642022-03-30 22:02:12 +08002296 p += 2;
Gilles Peskine449bd832023-01-11 14:50:10 +01002297 MBEDTLS_SSL_DEBUG_MSG(3,
2298 ("server hello, chosen ciphersuite: %s ( id=%d )",
2299 mbedtls_ssl_get_ciphersuite_name(
2300 ssl->session_negotiate->ciphersuite),
2301 ssl->session_negotiate->ciphersuite));
Jerry Yu3bf2c642022-03-30 22:02:12 +08002302
Jerry Yucfc04b32022-04-21 09:31:58 +08002303 /* ...
2304 * uint8 legacy_compression_method = 0;
2305 * ...
2306 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002307 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 1);
Thomas Daubney31e03a82022-07-25 15:59:25 +01002308 *p++ = MBEDTLS_SSL_COMPRESS_NULL;
Jerry Yu3bf2c642022-03-30 22:02:12 +08002309
Jerry Yucfc04b32022-04-21 09:31:58 +08002310 /* ...
2311 * Extension extensions<6..2^16-1>;
2312 * ...
2313 * struct {
2314 * ExtensionType extension_type; (2 bytes)
2315 * opaque extension_data<0..2^16-1>;
2316 * } Extension;
2317 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002318 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 2);
Jerry Yud9436a12022-04-20 22:28:09 +08002319 p_extensions_len = p;
Jerry Yu3bf2c642022-03-30 22:02:12 +08002320 p += 2;
2321
Gilles Peskine449bd832023-01-11 14:50:10 +01002322 if ((ret = ssl_tls13_write_server_hello_supported_versions_ext(
2323 ssl, p, end, &output_len)) != 0) {
Jerry Yu955ddd72022-04-22 22:27:33 +08002324 MBEDTLS_SSL_DEBUG_RET(
Gilles Peskine449bd832023-01-11 14:50:10 +01002325 1, "ssl_tls13_write_server_hello_supported_versions_ext", ret);
2326 return ret;
Jerry Yu3bf2c642022-03-30 22:02:12 +08002327 }
2328 p += output_len;
2329
Gilles Peskine449bd832023-01-11 14:50:10 +01002330 if (mbedtls_ssl_tls13_key_exchange_mode_with_ephemeral(ssl)) {
2331 if (is_hrr) {
2332 ret = ssl_tls13_write_hrr_key_share_ext(ssl, p, end, &output_len);
2333 } else {
2334 ret = ssl_tls13_write_key_share_ext(ssl, p, end, &output_len);
2335 }
2336 if (ret != 0) {
2337 return ret;
2338 }
Jerry Yu3bf2c642022-03-30 22:02:12 +08002339 p += output_len;
2340 }
Jerry Yu3bf2c642022-03-30 22:02:12 +08002341
Ronald Cron41a443a2022-10-04 16:38:25 +02002342#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01002343 if (!is_hrr && mbedtls_ssl_tls13_key_exchange_mode_with_psk(ssl)) {
2344 ret = ssl_tls13_write_server_pre_shared_key_ext(ssl, p, end, &output_len);
2345 if (ret != 0) {
2346 MBEDTLS_SSL_DEBUG_RET(1, "ssl_tls13_write_server_pre_shared_key_ext",
2347 ret);
2348 return ret;
Jerry Yu032b15ce2022-07-11 06:10:03 +00002349 }
2350 p += output_len;
2351 }
Ronald Cron41a443a2022-10-04 16:38:25 +02002352#endif
Jerry Yu032b15ce2022-07-11 06:10:03 +00002353
Gilles Peskine449bd832023-01-11 14:50:10 +01002354 MBEDTLS_PUT_UINT16_BE(p - p_extensions_len - 2, p_extensions_len, 0);
Jerry Yu3bf2c642022-03-30 22:02:12 +08002355
Gilles Peskine449bd832023-01-11 14:50:10 +01002356 MBEDTLS_SSL_DEBUG_BUF(4, "server hello extensions",
2357 p_extensions_len, p - p_extensions_len);
Jerry Yu3bf2c642022-03-30 22:02:12 +08002358
Jerry Yud9436a12022-04-20 22:28:09 +08002359 *out_len = p - buf;
Jerry Yu3bf2c642022-03-30 22:02:12 +08002360
Gilles Peskine449bd832023-01-11 14:50:10 +01002361 MBEDTLS_SSL_DEBUG_BUF(3, "server hello", buf, *out_len);
Jerry Yu3bf2c642022-03-30 22:02:12 +08002362
Jerry Yu7de2ff02022-11-08 21:43:46 +08002363 MBEDTLS_SSL_PRINT_EXTS(
Jerry Yu4b8f2f72022-10-31 13:31:22 +08002364 3, is_hrr ? MBEDTLS_SSL_TLS1_3_HS_HELLO_RETRY_REQUEST :
Gilles Peskine449bd832023-01-11 14:50:10 +01002365 MBEDTLS_SSL_HS_SERVER_HELLO,
2366 ssl->handshake->sent_extensions);
Jerry Yu4b8f2f72022-10-31 13:31:22 +08002367
Gilles Peskine449bd832023-01-11 14:50:10 +01002368 return ret;
Jerry Yu56404d72022-03-30 17:36:13 +08002369}
2370
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002371MBEDTLS_CHECK_RETURN_CRITICAL
Xiaokang Qian934ce6f2023-02-06 10:23:04 +00002372static int ssl_tls13_finalize_server_hello(mbedtls_ssl_context *ssl)
Jerry Yue110d252022-05-05 10:19:22 +08002373{
2374 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine449bd832023-01-11 14:50:10 +01002375 ret = mbedtls_ssl_tls13_compute_handshake_transform(ssl);
2376 if (ret != 0) {
2377 MBEDTLS_SSL_DEBUG_RET(1,
2378 "mbedtls_ssl_tls13_compute_handshake_transform",
2379 ret);
2380 return ret;
Jerry Yue110d252022-05-05 10:19:22 +08002381 }
2382
Gilles Peskine449bd832023-01-11 14:50:10 +01002383 return ret;
Jerry Yue110d252022-05-05 10:19:22 +08002384}
2385
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002386MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002387static int ssl_tls13_write_server_hello(mbedtls_ssl_context *ssl)
Jerry Yu5b64ae92022-03-30 17:15:02 +08002388{
Jerry Yu637a3f12022-04-20 21:37:58 +08002389 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yuf4b27e42022-03-30 17:32:21 +08002390 unsigned char *buf;
2391 size_t buf_len, msg_len;
2392
Gilles Peskine449bd832023-01-11 14:50:10 +01002393 MBEDTLS_SSL_DEBUG_MSG(2, ("=> write server hello"));
Jerry Yuf4b27e42022-03-30 17:32:21 +08002394
Gilles Peskine449bd832023-01-11 14:50:10 +01002395 MBEDTLS_SSL_PROC_CHK(ssl_tls13_prepare_server_hello(ssl));
Jerry Yuf4b27e42022-03-30 17:32:21 +08002396
Xiaokang Qian9f1747b2023-03-30 02:50:04 +00002397 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_start_handshake_msg(
2398 ssl, MBEDTLS_SSL_HS_SERVER_HELLO, &buf, &buf_len));
Jerry Yuf4b27e42022-03-30 17:32:21 +08002399
Gilles Peskine449bd832023-01-11 14:50:10 +01002400 MBEDTLS_SSL_PROC_CHK(ssl_tls13_write_server_hello_body(ssl, buf,
2401 buf + buf_len,
2402 &msg_len,
2403 0));
Jerry Yuf4b27e42022-03-30 17:32:21 +08002404
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +01002405 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_add_hs_msg_to_checksum(
Manuel Pégourié-Gonnard43cc1272023-02-06 11:48:19 +01002406 ssl, MBEDTLS_SSL_HS_SERVER_HELLO, buf, msg_len));
Jerry Yuf4b27e42022-03-30 17:32:21 +08002407
Gilles Peskine449bd832023-01-11 14:50:10 +01002408 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_finish_handshake_msg(
2409 ssl, buf_len, msg_len));
Jerry Yu637a3f12022-04-20 21:37:58 +08002410
Xiaokang Qian934ce6f2023-02-06 10:23:04 +00002411 MBEDTLS_SSL_PROC_CHK(ssl_tls13_finalize_server_hello(ssl));
Jerry Yue110d252022-05-05 10:19:22 +08002412
Gabor Mezei7b39bf12022-05-24 16:04:14 +02002413#if defined(MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE)
2414 /* The server sends a dummy change_cipher_spec record immediately
2415 * after its first handshake message. This may either be after
2416 * a ServerHello or a HelloRetryRequest.
2417 */
Gabor Mezei96ae9262022-06-28 11:45:18 +02002418 mbedtls_ssl_handshake_set_state(
Gilles Peskine449bd832023-01-11 14:50:10 +01002419 ssl, MBEDTLS_SSL_SERVER_CCS_AFTER_SERVER_HELLO);
Gabor Mezei7b39bf12022-05-24 16:04:14 +02002420#else
Gilles Peskine449bd832023-01-11 14:50:10 +01002421 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_ENCRYPTED_EXTENSIONS);
Gabor Mezei7b39bf12022-05-24 16:04:14 +02002422#endif /* MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE */
Jerry Yue110d252022-05-05 10:19:22 +08002423
Jerry Yuf4b27e42022-03-30 17:32:21 +08002424cleanup:
2425
Gilles Peskine449bd832023-01-11 14:50:10 +01002426 MBEDTLS_SSL_DEBUG_MSG(2, ("<= write server hello"));
2427 return ret;
Jerry Yu5b64ae92022-03-30 17:15:02 +08002428}
2429
Jerry Yu23d1a252022-05-12 18:08:59 +08002430
2431/*
2432 * Handler for MBEDTLS_SSL_HELLO_RETRY_REQUEST
2433 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002434MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002435static int ssl_tls13_prepare_hello_retry_request(mbedtls_ssl_context *ssl)
Jerry Yu23d1a252022-05-12 18:08:59 +08002436{
2437 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Ronald Cron5fbd2702024-02-14 10:03:36 +01002438 if (ssl->handshake->hello_retry_request_flag) {
Gilles Peskine449bd832023-01-11 14:50:10 +01002439 MBEDTLS_SSL_DEBUG_MSG(1, ("Too many HRRs"));
2440 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE,
2441 MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE);
2442 return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
Jerry Yu23d1a252022-05-12 18:08:59 +08002443 }
2444
2445 /*
2446 * Create stateless transcript hash for HRR
2447 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002448 MBEDTLS_SSL_DEBUG_MSG(4, ("Reset transcript for HRR"));
2449 ret = mbedtls_ssl_reset_transcript_for_hrr(ssl);
2450 if (ret != 0) {
2451 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_reset_transcript_for_hrr", ret);
2452 return ret;
Jerry Yu23d1a252022-05-12 18:08:59 +08002453 }
Gilles Peskine449bd832023-01-11 14:50:10 +01002454 mbedtls_ssl_session_reset_msg_layer(ssl, 0);
Jerry Yu23d1a252022-05-12 18:08:59 +08002455
Gilles Peskine449bd832023-01-11 14:50:10 +01002456 return 0;
Jerry Yu23d1a252022-05-12 18:08:59 +08002457}
2458
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002459MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002460static int ssl_tls13_write_hello_retry_request(mbedtls_ssl_context *ssl)
Jerry Yu23d1a252022-05-12 18:08:59 +08002461{
2462 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
2463 unsigned char *buf;
2464 size_t buf_len, msg_len;
2465
Gilles Peskine449bd832023-01-11 14:50:10 +01002466 MBEDTLS_SSL_DEBUG_MSG(2, ("=> write hello retry request"));
Jerry Yu23d1a252022-05-12 18:08:59 +08002467
Gilles Peskine449bd832023-01-11 14:50:10 +01002468 MBEDTLS_SSL_PROC_CHK(ssl_tls13_prepare_hello_retry_request(ssl));
Jerry Yu23d1a252022-05-12 18:08:59 +08002469
Gilles Peskine449bd832023-01-11 14:50:10 +01002470 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_start_handshake_msg(
2471 ssl, MBEDTLS_SSL_HS_SERVER_HELLO,
2472 &buf, &buf_len));
Jerry Yu23d1a252022-05-12 18:08:59 +08002473
Gilles Peskine449bd832023-01-11 14:50:10 +01002474 MBEDTLS_SSL_PROC_CHK(ssl_tls13_write_server_hello_body(ssl, buf,
2475 buf + buf_len,
2476 &msg_len,
2477 1));
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +01002478 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_add_hs_msg_to_checksum(
Manuel Pégourié-Gonnard43cc1272023-02-06 11:48:19 +01002479 ssl, MBEDTLS_SSL_HS_SERVER_HELLO, buf, msg_len));
Jerry Yu23d1a252022-05-12 18:08:59 +08002480
2481
Gilles Peskine449bd832023-01-11 14:50:10 +01002482 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_finish_handshake_msg(ssl, buf_len,
2483 msg_len));
Jerry Yu23d1a252022-05-12 18:08:59 +08002484
Ronald Cron5fbd2702024-02-14 10:03:36 +01002485 ssl->handshake->hello_retry_request_flag = 1;
Jerry Yu23d1a252022-05-12 18:08:59 +08002486
Gabor Mezei7b39bf12022-05-24 16:04:14 +02002487#if defined(MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE)
2488 /* The server sends a dummy change_cipher_spec record immediately
2489 * after its first handshake message. This may either be after
2490 * a ServerHello or a HelloRetryRequest.
2491 */
Gabor Mezei96ae9262022-06-28 11:45:18 +02002492 mbedtls_ssl_handshake_set_state(
Gilles Peskine449bd832023-01-11 14:50:10 +01002493 ssl, MBEDTLS_SSL_SERVER_CCS_AFTER_HELLO_RETRY_REQUEST);
Gabor Mezei7b39bf12022-05-24 16:04:14 +02002494#else
Gilles Peskine449bd832023-01-11 14:50:10 +01002495 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_CLIENT_HELLO);
Gabor Mezei7b39bf12022-05-24 16:04:14 +02002496#endif /* MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE */
Jerry Yu23d1a252022-05-12 18:08:59 +08002497
2498cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +01002499 MBEDTLS_SSL_DEBUG_MSG(2, ("<= write hello retry request"));
2500 return ret;
Jerry Yu23d1a252022-05-12 18:08:59 +08002501}
2502
Jerry Yu5b64ae92022-03-30 17:15:02 +08002503/*
Jerry Yu4d3841a2022-04-16 12:37:19 +08002504 * Handler for MBEDTLS_SSL_ENCRYPTED_EXTENSIONS
2505 */
Jerry Yu4d3841a2022-04-16 12:37:19 +08002506
2507/*
2508 * struct {
2509 * Extension extensions<0..2 ^ 16 - 1>;
2510 * } EncryptedExtensions;
2511 *
2512 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002513MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002514static int ssl_tls13_write_encrypted_extensions_body(mbedtls_ssl_context *ssl,
2515 unsigned char *buf,
2516 unsigned char *end,
2517 size_t *out_len)
Jerry Yu4d3841a2022-04-16 12:37:19 +08002518{
XiaokangQianacb39922022-06-17 10:18:48 +00002519 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yu4d3841a2022-04-16 12:37:19 +08002520 unsigned char *p = buf;
2521 size_t extensions_len = 0;
Jerry Yu8937eb42022-05-03 12:12:14 +08002522 unsigned char *p_extensions_len;
XiaokangQianacb39922022-06-17 10:18:48 +00002523 size_t output_len;
Jerry Yu9da5e5a2022-05-03 15:46:09 +08002524
Jerry Yu4d3841a2022-04-16 12:37:19 +08002525 *out_len = 0;
2526
Gilles Peskine449bd832023-01-11 14:50:10 +01002527 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 2);
Jerry Yu8937eb42022-05-03 12:12:14 +08002528 p_extensions_len = p;
Jerry Yu4d3841a2022-04-16 12:37:19 +08002529 p += 2;
2530
Jerry Yu8937eb42022-05-03 12:12:14 +08002531 ((void) ssl);
XiaokangQianacb39922022-06-17 10:18:48 +00002532 ((void) ret);
2533 ((void) output_len);
2534
2535#if defined(MBEDTLS_SSL_ALPN)
Gilles Peskine449bd832023-01-11 14:50:10 +01002536 ret = mbedtls_ssl_write_alpn_ext(ssl, p, end, &output_len);
2537 if (ret != 0) {
2538 return ret;
2539 }
XiaokangQian95d5f542022-06-24 02:29:26 +00002540 p += output_len;
XiaokangQianacb39922022-06-17 10:18:48 +00002541#endif /* MBEDTLS_SSL_ALPN */
Jerry Yu4d3841a2022-04-16 12:37:19 +08002542
Jerry Yu71c14f12022-12-12 11:10:35 +08002543#if defined(MBEDTLS_SSL_EARLY_DATA)
Ronald Cron78a38f62024-02-01 18:30:31 +01002544 if (ssl->handshake->early_data_accepted) {
Jerry Yu52335392023-11-23 18:06:06 +08002545 ret = mbedtls_ssl_tls13_write_early_data_ext(
Jerry Yuc59c5862023-12-05 10:40:49 +08002546 ssl, 0, p, end, &output_len);
Jerry Yu71c14f12022-12-12 11:10:35 +08002547 if (ret != 0) {
2548 return ret;
2549 }
2550 p += output_len;
2551 }
2552#endif /* MBEDTLS_SSL_EARLY_DATA */
2553
Waleed Elmelegy47d29462024-01-03 17:31:52 +00002554#if defined(MBEDTLS_SSL_RECORD_SIZE_LIMIT)
Waleed Elmelegyfbe42742024-01-05 18:11:10 +00002555 if (ssl->handshake->received_extensions & MBEDTLS_SSL_EXT_MASK(RECORD_SIZE_LIMIT)) {
Waleed Elmelegyd2fc90e2024-01-04 18:04:53 +00002556 ret = mbedtls_ssl_tls13_write_record_size_limit_ext(
2557 ssl, p, end, &output_len);
2558 if (ret != 0) {
2559 return ret;
2560 }
2561 p += output_len;
Waleed Elmelegy47d29462024-01-03 17:31:52 +00002562 }
Waleed Elmelegy47d29462024-01-03 17:31:52 +00002563#endif
2564
Gilles Peskine449bd832023-01-11 14:50:10 +01002565 extensions_len = (p - p_extensions_len) - 2;
2566 MBEDTLS_PUT_UINT16_BE(extensions_len, p_extensions_len, 0);
Jerry Yu8937eb42022-05-03 12:12:14 +08002567
2568 *out_len = p - buf;
Jerry Yu4d3841a2022-04-16 12:37:19 +08002569
Gilles Peskine449bd832023-01-11 14:50:10 +01002570 MBEDTLS_SSL_DEBUG_BUF(4, "encrypted extensions", buf, *out_len);
Jerry Yu4d3841a2022-04-16 12:37:19 +08002571
Jerry Yu7de2ff02022-11-08 21:43:46 +08002572 MBEDTLS_SSL_PRINT_EXTS(
Gilles Peskine449bd832023-01-11 14:50:10 +01002573 3, MBEDTLS_SSL_HS_ENCRYPTED_EXTENSIONS, ssl->handshake->sent_extensions);
Jerry Yu4b8f2f72022-10-31 13:31:22 +08002574
Gilles Peskine449bd832023-01-11 14:50:10 +01002575 return 0;
Jerry Yu4d3841a2022-04-16 12:37:19 +08002576}
2577
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002578MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002579static int ssl_tls13_write_encrypted_extensions(mbedtls_ssl_context *ssl)
Jerry Yu4d3841a2022-04-16 12:37:19 +08002580{
2581 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
2582 unsigned char *buf;
Jerry Yu39730a72022-05-03 12:14:04 +08002583 size_t buf_len, msg_len;
Jerry Yu4d3841a2022-04-16 12:37:19 +08002584
Gilles Peskine449bd832023-01-11 14:50:10 +01002585 mbedtls_ssl_set_outbound_transform(ssl,
2586 ssl->handshake->transform_handshake);
Gabor Mezei54719122022-06-28 11:34:56 +02002587 MBEDTLS_SSL_DEBUG_MSG(
Gilles Peskine449bd832023-01-11 14:50:10 +01002588 3, ("switching to handshake transform for outbound data"));
Gabor Mezei54719122022-06-28 11:34:56 +02002589
Gilles Peskine449bd832023-01-11 14:50:10 +01002590 MBEDTLS_SSL_DEBUG_MSG(2, ("=> write encrypted extensions"));
Jerry Yu4d3841a2022-04-16 12:37:19 +08002591
Xiaokang Qian9f1747b2023-03-30 02:50:04 +00002592 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_start_handshake_msg(
2593 ssl, MBEDTLS_SSL_HS_ENCRYPTED_EXTENSIONS,
2594 &buf, &buf_len));
Jerry Yu4d3841a2022-04-16 12:37:19 +08002595
Gilles Peskine449bd832023-01-11 14:50:10 +01002596 MBEDTLS_SSL_PROC_CHK(ssl_tls13_write_encrypted_extensions_body(
2597 ssl, buf, buf + buf_len, &msg_len));
Jerry Yu4d3841a2022-04-16 12:37:19 +08002598
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +01002599 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_add_hs_msg_to_checksum(
Xiaokang Qian9f1747b2023-03-30 02:50:04 +00002600 ssl, MBEDTLS_SSL_HS_ENCRYPTED_EXTENSIONS,
2601 buf, msg_len));
Jerry Yu4d3841a2022-04-16 12:37:19 +08002602
Gilles Peskine449bd832023-01-11 14:50:10 +01002603 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_finish_handshake_msg(
2604 ssl, buf_len, msg_len));
Jerry Yu4d3841a2022-04-16 12:37:19 +08002605
Ronald Cron928cbd32022-10-04 16:14:26 +02002606#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01002607 if (mbedtls_ssl_tls13_key_exchange_mode_with_psk(ssl)) {
2608 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_SERVER_FINISHED);
2609 } else {
2610 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_CERTIFICATE_REQUEST);
2611 }
Jerry Yu8937eb42022-05-03 12:12:14 +08002612#else
Gilles Peskine449bd832023-01-11 14:50:10 +01002613 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_SERVER_FINISHED);
Jerry Yu8937eb42022-05-03 12:12:14 +08002614#endif
2615
Jerry Yu4d3841a2022-04-16 12:37:19 +08002616cleanup:
2617
Gilles Peskine449bd832023-01-11 14:50:10 +01002618 MBEDTLS_SSL_DEBUG_MSG(2, ("<= write encrypted extensions"));
2619 return ret;
Jerry Yu4d3841a2022-04-16 12:37:19 +08002620}
2621
Ronald Cron928cbd32022-10-04 16:14:26 +02002622#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED)
XiaokangQiana987e1d2022-05-07 01:25:58 +00002623#define SSL_CERTIFICATE_REQUEST_SEND_REQUEST 0
2624#define SSL_CERTIFICATE_REQUEST_SKIP 1
2625/* Coordination:
2626 * Check whether a CertificateRequest message should be written.
2627 * Returns a negative code on failure, or
2628 * - SSL_CERTIFICATE_REQUEST_SEND_REQUEST
2629 * - SSL_CERTIFICATE_REQUEST_SKIP
2630 * indicating if the writing of the CertificateRequest
2631 * should be skipped or not.
2632 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002633MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002634static int ssl_tls13_certificate_request_coordinate(mbedtls_ssl_context *ssl)
XiaokangQiana987e1d2022-05-07 01:25:58 +00002635{
2636 int authmode;
2637
XiaokangQian40a35232022-05-07 09:02:40 +00002638#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Gilles Peskine449bd832023-01-11 14:50:10 +01002639 if (ssl->handshake->sni_authmode != MBEDTLS_SSL_VERIFY_UNSET) {
XiaokangQian40a35232022-05-07 09:02:40 +00002640 authmode = ssl->handshake->sni_authmode;
Gilles Peskine449bd832023-01-11 14:50:10 +01002641 } else
XiaokangQian40a35232022-05-07 09:02:40 +00002642#endif
XiaokangQiana987e1d2022-05-07 01:25:58 +00002643 authmode = ssl->conf->authmode;
2644
Gilles Peskine449bd832023-01-11 14:50:10 +01002645 if (authmode == MBEDTLS_SSL_VERIFY_NONE) {
Ronald Croneac00ad2022-09-13 10:16:31 +02002646 ssl->session_negotiate->verify_result = MBEDTLS_X509_BADCERT_SKIP_VERIFY;
Gilles Peskine449bd832023-01-11 14:50:10 +01002647 return SSL_CERTIFICATE_REQUEST_SKIP;
Ronald Croneac00ad2022-09-13 10:16:31 +02002648 }
XiaokangQiana987e1d2022-05-07 01:25:58 +00002649
XiaokangQianc3017f62022-05-13 05:55:41 +00002650 ssl->handshake->certificate_request_sent = 1;
XiaokangQian189ded22022-05-10 08:12:17 +00002651
Gilles Peskine449bd832023-01-11 14:50:10 +01002652 return SSL_CERTIFICATE_REQUEST_SEND_REQUEST;
XiaokangQiana987e1d2022-05-07 01:25:58 +00002653}
2654
XiaokangQiancec9ae62022-05-06 07:28:50 +00002655/*
2656 * struct {
2657 * opaque certificate_request_context<0..2^8-1>;
2658 * Extension extensions<2..2^16-1>;
2659 * } CertificateRequest;
2660 *
2661 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002662MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002663static int ssl_tls13_write_certificate_request_body(mbedtls_ssl_context *ssl,
2664 unsigned char *buf,
2665 const unsigned char *end,
2666 size_t *out_len)
XiaokangQiancec9ae62022-05-06 07:28:50 +00002667{
2668 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
2669 unsigned char *p = buf;
XiaokangQianec6efb92022-05-06 09:53:10 +00002670 size_t output_len = 0;
XiaokangQiancec9ae62022-05-06 07:28:50 +00002671 unsigned char *p_extensions_len;
2672
2673 *out_len = 0;
2674
2675 /* Check if we have enough space:
2676 * - certificate_request_context (1 byte)
2677 * - extensions length (2 bytes)
2678 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002679 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 3);
XiaokangQiancec9ae62022-05-06 07:28:50 +00002680
2681 /*
2682 * Write certificate_request_context
2683 */
2684 /*
2685 * We use a zero length context for the normal handshake
2686 * messages. For post-authentication handshake messages
2687 * this request context would be set to a non-zero value.
2688 */
2689 *p++ = 0x0;
2690
2691 /*
2692 * Write extensions
2693 */
2694 /* The extensions must contain the signature_algorithms. */
2695 p_extensions_len = p;
2696 p += 2;
Gilles Peskine449bd832023-01-11 14:50:10 +01002697 ret = mbedtls_ssl_write_sig_alg_ext(ssl, p, end, &output_len);
2698 if (ret != 0) {
2699 return ret;
2700 }
XiaokangQiancec9ae62022-05-06 07:28:50 +00002701
XiaokangQianec6efb92022-05-06 09:53:10 +00002702 p += output_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01002703 MBEDTLS_PUT_UINT16_BE(p - p_extensions_len - 2, p_extensions_len, 0);
XiaokangQiancec9ae62022-05-06 07:28:50 +00002704
2705 *out_len = p - buf;
2706
Jerry Yu7de2ff02022-11-08 21:43:46 +08002707 MBEDTLS_SSL_PRINT_EXTS(
Gilles Peskine449bd832023-01-11 14:50:10 +01002708 3, MBEDTLS_SSL_HS_CERTIFICATE_REQUEST, ssl->handshake->sent_extensions);
Jerry Yu4b8f2f72022-10-31 13:31:22 +08002709
Gilles Peskine449bd832023-01-11 14:50:10 +01002710 return 0;
XiaokangQiancec9ae62022-05-06 07:28:50 +00002711}
2712
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002713MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002714static int ssl_tls13_write_certificate_request(mbedtls_ssl_context *ssl)
XiaokangQiancec9ae62022-05-06 07:28:50 +00002715{
2716 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
2717
Gilles Peskine449bd832023-01-11 14:50:10 +01002718 MBEDTLS_SSL_DEBUG_MSG(2, ("=> write certificate request"));
XiaokangQiancec9ae62022-05-06 07:28:50 +00002719
Gilles Peskine449bd832023-01-11 14:50:10 +01002720 MBEDTLS_SSL_PROC_CHK_NEG(ssl_tls13_certificate_request_coordinate(ssl));
XiaokangQiancec9ae62022-05-06 07:28:50 +00002721
Gilles Peskine449bd832023-01-11 14:50:10 +01002722 if (ret == SSL_CERTIFICATE_REQUEST_SEND_REQUEST) {
XiaokangQiancec9ae62022-05-06 07:28:50 +00002723 unsigned char *buf;
2724 size_t buf_len, msg_len;
2725
Xiaokang Qian9f1747b2023-03-30 02:50:04 +00002726 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_start_handshake_msg(
2727 ssl, MBEDTLS_SSL_HS_CERTIFICATE_REQUEST,
2728 &buf, &buf_len));
XiaokangQiancec9ae62022-05-06 07:28:50 +00002729
Gilles Peskine449bd832023-01-11 14:50:10 +01002730 MBEDTLS_SSL_PROC_CHK(ssl_tls13_write_certificate_request_body(
2731 ssl, buf, buf + buf_len, &msg_len));
XiaokangQiancec9ae62022-05-06 07:28:50 +00002732
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +01002733 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_add_hs_msg_to_checksum(
Xiaokang Qian9f1747b2023-03-30 02:50:04 +00002734 ssl, MBEDTLS_SSL_HS_CERTIFICATE_REQUEST,
2735 buf, msg_len));
XiaokangQiancec9ae62022-05-06 07:28:50 +00002736
Gilles Peskine449bd832023-01-11 14:50:10 +01002737 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_finish_handshake_msg(
2738 ssl, buf_len, msg_len));
2739 } else if (ret == SSL_CERTIFICATE_REQUEST_SKIP) {
2740 MBEDTLS_SSL_DEBUG_MSG(2, ("<= skip write certificate request"));
XiaokangQiancec9ae62022-05-06 07:28:50 +00002741 ret = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01002742 } else {
2743 MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
XiaokangQiancec9ae62022-05-06 07:28:50 +00002744 ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
2745 goto cleanup;
2746 }
2747
Gilles Peskine449bd832023-01-11 14:50:10 +01002748 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_SERVER_CERTIFICATE);
XiaokangQiancec9ae62022-05-06 07:28:50 +00002749cleanup:
2750
Gilles Peskine449bd832023-01-11 14:50:10 +01002751 MBEDTLS_SSL_DEBUG_MSG(2, ("<= write certificate request"));
2752 return ret;
XiaokangQiancec9ae62022-05-06 07:28:50 +00002753}
XiaokangQiancec9ae62022-05-06 07:28:50 +00002754
Jerry Yu4d3841a2022-04-16 12:37:19 +08002755/*
Jerry Yuc6e6dbf2022-04-16 19:42:57 +08002756 * Handler for MBEDTLS_SSL_SERVER_CERTIFICATE
Jerry Yu83da34e2022-04-16 13:59:52 +08002757 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002758MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002759static int ssl_tls13_write_server_certificate(mbedtls_ssl_context *ssl)
Jerry Yu83da34e2022-04-16 13:59:52 +08002760{
Jerry Yu5a26f302022-05-10 20:46:40 +08002761 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
XiaokangQianfb665a82022-06-15 03:57:21 +00002762
2763#if defined(MBEDTLS_X509_CRT_PARSE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01002764 if ((ssl_tls13_pick_key_cert(ssl) != 0) ||
2765 mbedtls_ssl_own_cert(ssl) == NULL) {
2766 MBEDTLS_SSL_DEBUG_MSG(2, ("No certificate available."));
2767 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE,
2768 MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE);
2769 return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
Jerry Yu5a26f302022-05-10 20:46:40 +08002770 }
XiaokangQianfb665a82022-06-15 03:57:21 +00002771#endif /* MBEDTLS_X509_CRT_PARSE_C */
Jerry Yu5a26f302022-05-10 20:46:40 +08002772
Gilles Peskine449bd832023-01-11 14:50:10 +01002773 ret = mbedtls_ssl_tls13_write_certificate(ssl);
2774 if (ret != 0) {
2775 return ret;
2776 }
2777 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_CERTIFICATE_VERIFY);
2778 return 0;
Jerry Yu83da34e2022-04-16 13:59:52 +08002779}
2780
2781/*
Jerry Yuc6e6dbf2022-04-16 19:42:57 +08002782 * Handler for MBEDTLS_SSL_CERTIFICATE_VERIFY
Jerry Yu83da34e2022-04-16 13:59:52 +08002783 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002784MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002785static int ssl_tls13_write_certificate_verify(mbedtls_ssl_context *ssl)
Jerry Yu83da34e2022-04-16 13:59:52 +08002786{
Gilles Peskine449bd832023-01-11 14:50:10 +01002787 int ret = mbedtls_ssl_tls13_write_certificate_verify(ssl);
2788 if (ret != 0) {
2789 return ret;
2790 }
2791 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_SERVER_FINISHED);
2792 return 0;
Jerry Yu83da34e2022-04-16 13:59:52 +08002793}
Ronald Cron928cbd32022-10-04 16:14:26 +02002794#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED */
Jerry Yu83da34e2022-04-16 13:59:52 +08002795
Jerry Yu9b72e392023-12-01 16:27:08 +08002796/*
2797 * RFC 8446 section A.2
2798 *
2799 * | Send ServerHello
2800 * | K_send = handshake
2801 * | Send EncryptedExtensions
2802 * | [Send CertificateRequest]
2803 * Can send | [Send Certificate + CertificateVerify]
2804 * app data | Send Finished
2805 * after --> | K_send = application
2806 * here +--------+--------+
2807 * No 0-RTT | | 0-RTT
2808 * | |
2809 * K_recv = handshake | | K_recv = early data
2810 * [Skip decrypt errors] | +------> WAIT_EOED -+
2811 * | | Recv | | Recv EndOfEarlyData
2812 * | | early data | | K_recv = handshake
2813 * | +------------+ |
2814 * | |
2815 * +> WAIT_FLIGHT2 <--------+
2816 * |
2817 * +--------+--------+
2818 * No auth | | Client auth
2819 * | |
2820 * | v
2821 * | WAIT_CERT
2822 * | Recv | | Recv Certificate
2823 * | empty | v
2824 * | Certificate | WAIT_CV
2825 * | | | Recv
2826 * | v | CertificateVerify
2827 * +-> WAIT_FINISHED <---+
2828 * | Recv Finished
2829 *
2830 *
2831 * The following function handles the state changes after WAIT_FLIGHT2 in the
Jerry Yu3be85072023-12-04 09:58:54 +08002832 * above diagram. We are not going to receive early data related messages
2833 * anymore, prepare to receive the first handshake message of the client
2834 * second flight.
Jerry Yu9b72e392023-12-01 16:27:08 +08002835 */
Jerry Yu3be85072023-12-04 09:58:54 +08002836static void ssl_tls13_prepare_for_handshake_second_flight(
2837 mbedtls_ssl_context *ssl)
Jerry Yu9b72e392023-12-01 16:27:08 +08002838{
Jerry Yu9b72e392023-12-01 16:27:08 +08002839 if (ssl->handshake->certificate_request_sent) {
2840 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_CLIENT_CERTIFICATE);
2841 } else {
Jerry Yu42020fb2023-12-05 17:35:53 +08002842 MBEDTLS_SSL_DEBUG_MSG(2, ("skip parse certificate"));
Jerry Yuebb1b1d2023-12-05 11:02:15 +08002843 MBEDTLS_SSL_DEBUG_MSG(2, ("skip parse certificate verify"));
Jerry Yuebb1b1d2023-12-05 11:02:15 +08002844
Jerry Yu9b72e392023-12-01 16:27:08 +08002845 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_CLIENT_FINISHED);
2846 }
Jerry Yu9b72e392023-12-01 16:27:08 +08002847}
2848
Jerry Yu83da34e2022-04-16 13:59:52 +08002849/*
Jerry Yud6e253d2022-05-18 13:59:24 +08002850 * Handler for MBEDTLS_SSL_SERVER_FINISHED
Jerry Yu69dd8d42022-04-16 12:51:26 +08002851 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002852MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002853static int ssl_tls13_write_server_finished(mbedtls_ssl_context *ssl)
Jerry Yu69dd8d42022-04-16 12:51:26 +08002854{
Jerry Yud6e253d2022-05-18 13:59:24 +08002855 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yu27bdc7c2022-04-16 13:33:27 +08002856
Gilles Peskine449bd832023-01-11 14:50:10 +01002857 ret = mbedtls_ssl_tls13_write_finished_message(ssl);
2858 if (ret != 0) {
2859 return ret;
2860 }
Jerry Yu27bdc7c2022-04-16 13:33:27 +08002861
Gilles Peskine449bd832023-01-11 14:50:10 +01002862 ret = mbedtls_ssl_tls13_compute_application_transform(ssl);
2863 if (ret != 0) {
Jerry Yue3d67cb2022-05-19 15:33:10 +08002864 MBEDTLS_SSL_PEND_FATAL_ALERT(
Gilles Peskine449bd832023-01-11 14:50:10 +01002865 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE,
2866 MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE);
2867 return ret;
Jerry Yue3d67cb2022-05-19 15:33:10 +08002868 }
XiaokangQianc3017f62022-05-13 05:55:41 +00002869
Jerry Yu87b5ed42022-12-12 13:07:07 +08002870#if defined(MBEDTLS_SSL_EARLY_DATA)
Ronald Cron78a38f62024-02-01 18:30:31 +01002871 if (ssl->handshake->early_data_accepted) {
Jerry Yu0af63dc2023-12-01 17:14:51 +08002872 /* See RFC 8446 section A.2 for more information */
Jerry Yu87b5ed42022-12-12 13:07:07 +08002873 MBEDTLS_SSL_DEBUG_MSG(
2874 1, ("Switch to early keys for inbound traffic. "
2875 "( K_recv = early data )"));
2876 mbedtls_ssl_set_inbound_transform(
2877 ssl, ssl->handshake->transform_earlydata);
2878 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_END_OF_EARLY_DATA);
2879 return 0;
2880 }
2881#endif /* MBEDTLS_SSL_EARLY_DATA */
Jerry Yu0af63dc2023-12-01 17:14:51 +08002882 MBEDTLS_SSL_DEBUG_MSG(
2883 1, ("Switch to handshake keys for inbound traffic "
2884 "( K_recv = handshake )"));
Gilles Peskine449bd832023-01-11 14:50:10 +01002885 mbedtls_ssl_set_inbound_transform(ssl, ssl->handshake->transform_handshake);
XiaokangQian189ded22022-05-10 08:12:17 +00002886
Jerry Yu3be85072023-12-04 09:58:54 +08002887 ssl_tls13_prepare_for_handshake_second_flight(ssl);
Jerry Yu7d8c3fe2022-12-12 12:59:44 +08002888
2889 return 0;
2890}
2891
Jerry Yu87b5ed42022-12-12 13:07:07 +08002892#if defined(MBEDTLS_SSL_EARLY_DATA)
2893/*
Jerry Yu59d420f2023-12-01 16:30:34 +08002894 * Handler for MBEDTLS_SSL_END_OF_EARLY_DATA
Jerry Yu87b5ed42022-12-12 13:07:07 +08002895 */
Jerry Yu3be85072023-12-04 09:58:54 +08002896#define SSL_GOT_END_OF_EARLY_DATA 0
Jerry Yub55f9eb2023-12-05 10:27:17 +08002897#define SSL_GOT_EARLY_DATA 1
Jerry Yud5c34962023-12-01 16:32:31 +08002898/* Coordination:
Jerry Yu3be85072023-12-04 09:58:54 +08002899 * Deals with the ambiguity of not knowing if the next message is an
2900 * EndOfEarlyData message or an application message containing early data.
Jerry Yud5c34962023-12-01 16:32:31 +08002901 * Returns a negative code on failure, or
Jerry Yu3be85072023-12-04 09:58:54 +08002902 * - SSL_GOT_END_OF_EARLY_DATA
Jerry Yub55f9eb2023-12-05 10:27:17 +08002903 * - SSL_GOT_EARLY_DATA
Jerry Yud5c34962023-12-01 16:32:31 +08002904 * indicating which message is received.
2905 */
2906MBEDTLS_CHECK_RETURN_CRITICAL
2907static int ssl_tls13_end_of_early_data_coordinate(mbedtls_ssl_context *ssl)
2908{
2909 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yub4ed4602023-12-01 16:34:00 +08002910
2911 if ((ret = mbedtls_ssl_read_record(ssl, 0)) != 0) {
2912 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_read_record", ret);
2913 return ret;
2914 }
2915 ssl->keep_current_message = 1;
2916
2917 if (ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
2918 ssl->in_msg[0] == MBEDTLS_SSL_HS_END_OF_EARLY_DATA) {
Jerry Yub55f9eb2023-12-05 10:27:17 +08002919 MBEDTLS_SSL_DEBUG_MSG(3, ("Received an end_of_early_data message."));
Jerry Yu3be85072023-12-04 09:58:54 +08002920 return SSL_GOT_END_OF_EARLY_DATA;
Jerry Yub4ed4602023-12-01 16:34:00 +08002921 }
2922
2923 if (ssl->in_msgtype == MBEDTLS_SSL_MSG_APPLICATION_DATA) {
Jerry Yub55f9eb2023-12-05 10:27:17 +08002924 MBEDTLS_SSL_DEBUG_MSG(3, ("Received early data"));
Jerry Yu6a5904d2023-12-06 17:11:12 +08002925 /* RFC 8446 section 4.6.1
2926 *
2927 * A server receiving more than max_early_data_size bytes of 0-RTT data
2928 * SHOULD terminate the connection with an "unexpected_message" alert.
2929 *
2930 * TODO: Add received data size check here.
2931 */
Ronald Croned7d4bf2024-01-31 07:55:19 +01002932 if (ssl->in_offt == NULL) {
2933 /* Set the reading pointer */
2934 ssl->in_offt = ssl->in_msg;
2935 }
Jerry Yub55f9eb2023-12-05 10:27:17 +08002936 return SSL_GOT_EARLY_DATA;
Jerry Yub4ed4602023-12-01 16:34:00 +08002937 }
2938
Jerry Yu7bb40a32023-12-04 10:04:15 +08002939 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE,
2940 MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE);
Jerry Yub4ed4602023-12-01 16:34:00 +08002941 return MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE;
Jerry Yud5c34962023-12-01 16:32:31 +08002942}
2943
2944MBEDTLS_CHECK_RETURN_CRITICAL
2945static int ssl_tls13_parse_end_of_early_data(mbedtls_ssl_context *ssl,
2946 const unsigned char *buf,
2947 const unsigned char *end)
2948{
Jerry Yu75c9ab72023-12-01 16:41:40 +08002949 /* RFC 8446 section 4.5
2950 *
2951 * struct {} EndOfEarlyData;
2952 */
Jerry Yufbf03992023-12-04 10:00:37 +08002953 if (buf != end) {
2954 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR,
2955 MBEDTLS_ERR_SSL_DECODE_ERROR);
2956 return MBEDTLS_ERR_SSL_DECODE_ERROR;
2957 }
2958 return 0;
Jerry Yud5c34962023-12-01 16:32:31 +08002959}
2960
Jerry Yud5c34962023-12-01 16:32:31 +08002961/*
2962 * RFC 8446 section A.2
2963 *
2964 * | Send ServerHello
2965 * | K_send = handshake
2966 * | Send EncryptedExtensions
2967 * | [Send CertificateRequest]
2968 * Can send | [Send Certificate + CertificateVerify]
2969 * app data | Send Finished
2970 * after --> | K_send = application
2971 * here +--------+--------+
2972 * No 0-RTT | | 0-RTT
2973 * | |
2974 * K_recv = handshake | | K_recv = early data
2975 * [Skip decrypt errors] | +------> WAIT_EOED -+
2976 * | | Recv | | Recv EndOfEarlyData
2977 * | | early data | | K_recv = handshake
2978 * | +------------+ |
2979 * | |
2980 * +> WAIT_FLIGHT2 <--------+
2981 * |
2982 * +--------+--------+
2983 * No auth | | Client auth
2984 * | |
2985 * | v
2986 * | WAIT_CERT
2987 * | Recv | | Recv Certificate
2988 * | empty | v
2989 * | Certificate | WAIT_CV
2990 * | | | Recv
2991 * | v | CertificateVerify
2992 * +-> WAIT_FINISHED <---+
2993 * | Recv Finished
2994 *
2995 * The function handles actions and state changes from 0-RTT to WAIT_FLIGHT2 in
2996 * the above diagram.
2997 */
Jerry Yu87b5ed42022-12-12 13:07:07 +08002998MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yu59d420f2023-12-01 16:30:34 +08002999static int ssl_tls13_process_end_of_early_data(mbedtls_ssl_context *ssl)
Jerry Yu87b5ed42022-12-12 13:07:07 +08003000{
3001 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yud5c34962023-12-01 16:32:31 +08003002
3003 MBEDTLS_SSL_DEBUG_MSG(2, ("=> ssl_tls13_process_end_of_early_data"));
3004
3005 MBEDTLS_SSL_PROC_CHK_NEG(ssl_tls13_end_of_early_data_coordinate(ssl));
3006
Jerry Yu3be85072023-12-04 09:58:54 +08003007 if (ret == SSL_GOT_END_OF_EARLY_DATA) {
Jerry Yud5c34962023-12-01 16:32:31 +08003008 unsigned char *buf;
3009 size_t buf_len;
3010
3011 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_tls13_fetch_handshake_msg(
3012 ssl, MBEDTLS_SSL_HS_END_OF_EARLY_DATA,
3013 &buf, &buf_len));
3014
3015 MBEDTLS_SSL_PROC_CHK(ssl_tls13_parse_end_of_early_data(
3016 ssl, buf, buf + buf_len));
3017
Jerry Yue9655122023-12-01 16:44:40 +08003018 MBEDTLS_SSL_DEBUG_MSG(
3019 1, ("Switch to handshake keys for inbound traffic"
3020 "( K_recv = handshake )"));
3021 mbedtls_ssl_set_inbound_transform(
3022 ssl, ssl->handshake->transform_handshake);
3023
Jerry Yud5c34962023-12-01 16:32:31 +08003024 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_add_hs_msg_to_checksum(
3025 ssl, MBEDTLS_SSL_HS_END_OF_EARLY_DATA,
3026 buf, buf_len));
Jerry Yue9655122023-12-01 16:44:40 +08003027
Jerry Yu3be85072023-12-04 09:58:54 +08003028 ssl_tls13_prepare_for_handshake_second_flight(ssl);
Jerry Yud5c34962023-12-01 16:32:31 +08003029
Jerry Yub55f9eb2023-12-05 10:27:17 +08003030 } else if (ret == SSL_GOT_EARLY_DATA) {
Jerry Yud9ca3542023-12-06 17:23:52 +08003031 ret = MBEDTLS_ERR_SSL_RECEIVED_EARLY_DATA;
3032 goto cleanup;
Jerry Yud5c34962023-12-01 16:32:31 +08003033 } else {
3034 MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
3035 ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
3036 goto cleanup;
3037 }
3038
Jerry Yud5c34962023-12-01 16:32:31 +08003039cleanup:
3040 MBEDTLS_SSL_DEBUG_MSG(2, ("<= ssl_tls13_process_end_of_early_data"));
Jerry Yu87b5ed42022-12-12 13:07:07 +08003041 return ret;
3042}
3043#endif /* MBEDTLS_SSL_EARLY_DATA */
3044
Jerry Yu69dd8d42022-04-16 12:51:26 +08003045/*
Jerry Yud6e253d2022-05-18 13:59:24 +08003046 * Handler for MBEDTLS_SSL_CLIENT_FINISHED
Jerry Yu69dd8d42022-04-16 12:51:26 +08003047 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02003048MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01003049static int ssl_tls13_process_client_finished(mbedtls_ssl_context *ssl)
Jerry Yu69dd8d42022-04-16 12:51:26 +08003050{
Jerry Yud6e253d2022-05-18 13:59:24 +08003051 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
3052
Gilles Peskine449bd832023-01-11 14:50:10 +01003053 ret = mbedtls_ssl_tls13_process_finished_message(ssl);
3054 if (ret != 0) {
3055 return ret;
Jerry Yue3d67cb2022-05-19 15:33:10 +08003056 }
3057
Gilles Peskine449bd832023-01-11 14:50:10 +01003058 ret = mbedtls_ssl_tls13_compute_resumption_master_secret(ssl);
3059 if (ret != 0) {
Xiaokang Qian9f1747b2023-03-30 02:50:04 +00003060 MBEDTLS_SSL_DEBUG_RET(
3061 1, "mbedtls_ssl_tls13_compute_resumption_master_secret", ret);
Gilles Peskine449bd832023-01-11 14:50:10 +01003062 }
3063
3064 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_HANDSHAKE_WRAPUP);
3065 return 0;
Jerry Yu69dd8d42022-04-16 12:51:26 +08003066}
3067
3068/*
Jerry Yud6e253d2022-05-18 13:59:24 +08003069 * Handler for MBEDTLS_SSL_HANDSHAKE_WRAPUP
Jerry Yu69dd8d42022-04-16 12:51:26 +08003070 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02003071MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01003072static int ssl_tls13_handshake_wrapup(mbedtls_ssl_context *ssl)
Jerry Yu69dd8d42022-04-16 12:51:26 +08003073{
Gilles Peskine449bd832023-01-11 14:50:10 +01003074 MBEDTLS_SSL_DEBUG_MSG(2, ("handshake: done"));
Jerry Yu03ed50b2022-04-16 17:13:30 +08003075
Gilles Peskine449bd832023-01-11 14:50:10 +01003076 mbedtls_ssl_tls13_handshake_wrapup(ssl);
Jerry Yue67bef42022-07-07 07:29:42 +00003077
Pengyu Lv3643fdb2023-01-12 16:46:28 +08003078#if defined(MBEDTLS_SSL_SESSION_TICKETS) && \
3079 defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED)
Pengyu Lva1aa31b2022-12-13 13:49:59 +08003080/* TODO: Remove the check of SOME_PSK_ENABLED since SESSION_TICKETS requires
3081 * SOME_PSK_ENABLED to be enabled. Here is just to make CI happy. It is
3082 * expected to be resolved with issue#6395.
3083 */
Pengyu Lvc7af2c42022-12-01 16:33:00 +08003084 /* Sent NewSessionTicket message only when client supports PSK */
Pengyu Lvb2cfafb2023-11-14 13:56:13 +08003085 if (mbedtls_ssl_tls13_is_some_psk_supported(ssl)) {
Xiaokang Qian9f1747b2023-03-30 02:50:04 +00003086 mbedtls_ssl_handshake_set_state(
3087 ssl, MBEDTLS_SSL_TLS1_3_NEW_SESSION_TICKET);
Pengyu Lvc7af2c42022-12-01 16:33:00 +08003088 } else
Jerry Yufca4d572022-07-21 10:37:48 +08003089#endif
Pengyu Lv3643fdb2023-01-12 16:46:28 +08003090 {
3091 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_HANDSHAKE_OVER);
3092 }
Gilles Peskine449bd832023-01-11 14:50:10 +01003093 return 0;
Jerry Yu69dd8d42022-04-16 12:51:26 +08003094}
3095
3096/*
Jerry Yua8d3c502022-10-30 14:51:23 +08003097 * Handler for MBEDTLS_SSL_TLS1_3_NEW_SESSION_TICKET
Jerry Yue67bef42022-07-07 07:29:42 +00003098 */
3099#define SSL_NEW_SESSION_TICKET_SKIP 0
3100#define SSL_NEW_SESSION_TICKET_WRITE 1
3101MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01003102static int ssl_tls13_write_new_session_ticket_coordinate(mbedtls_ssl_context *ssl)
Jerry Yue67bef42022-07-07 07:29:42 +00003103{
3104 /* Check whether the use of session tickets is enabled */
Gilles Peskine449bd832023-01-11 14:50:10 +01003105 if (ssl->conf->f_ticket_write == NULL) {
3106 MBEDTLS_SSL_DEBUG_MSG(2, ("NewSessionTicket: disabled,"
3107 " callback is not set"));
3108 return SSL_NEW_SESSION_TICKET_SKIP;
Jerry Yud0766ec2022-09-22 10:46:57 +08003109 }
Gilles Peskine449bd832023-01-11 14:50:10 +01003110 if (ssl->conf->new_session_tickets_count == 0) {
3111 MBEDTLS_SSL_DEBUG_MSG(2, ("NewSessionTicket: disabled,"
3112 " configured count is zero"));
3113 return SSL_NEW_SESSION_TICKET_SKIP;
Jerry Yud0766ec2022-09-22 10:46:57 +08003114 }
3115
Gilles Peskine449bd832023-01-11 14:50:10 +01003116 if (ssl->handshake->new_session_tickets_count == 0) {
3117 MBEDTLS_SSL_DEBUG_MSG(2, ("NewSessionTicket: all tickets have "
3118 "been sent."));
3119 return SSL_NEW_SESSION_TICKET_SKIP;
Jerry Yue67bef42022-07-07 07:29:42 +00003120 }
3121
Gilles Peskine449bd832023-01-11 14:50:10 +01003122 return SSL_NEW_SESSION_TICKET_WRITE;
Jerry Yue67bef42022-07-07 07:29:42 +00003123}
3124
3125#if defined(MBEDTLS_SSL_SESSION_TICKETS)
3126MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01003127static int ssl_tls13_prepare_new_session_ticket(mbedtls_ssl_context *ssl,
3128 unsigned char *ticket_nonce,
3129 size_t ticket_nonce_size)
Jerry Yue67bef42022-07-07 07:29:42 +00003130{
3131 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
3132 mbedtls_ssl_session *session = ssl->session;
3133 mbedtls_ssl_ciphersuite_t *ciphersuite_info;
3134 psa_algorithm_t psa_hash_alg;
3135 int hash_length;
3136
Gilles Peskine449bd832023-01-11 14:50:10 +01003137 MBEDTLS_SSL_DEBUG_MSG(2, ("=> prepare NewSessionTicket msg"));
Jerry Yue67bef42022-07-07 07:29:42 +00003138
Pengyu Lv9f926952022-11-17 15:22:33 +08003139 /* Set ticket_flags depends on the advertised psk key exchange mode */
Pengyu Lv94a42cc2023-12-06 10:04:17 +08003140 mbedtls_ssl_tls13_session_clear_ticket_flags(
Pengyu Lv9eacb442022-12-09 14:39:19 +08003141 session, MBEDTLS_SSL_TLS1_3_TICKET_FLAGS_MASK);
Pengyu Lve6487fe2022-12-06 09:30:29 +08003142#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED)
Pengyu Lv94a42cc2023-12-06 10:04:17 +08003143 mbedtls_ssl_tls13_session_set_ticket_flags(
Pengyu Lv9eacb442022-12-09 14:39:19 +08003144 session, ssl->handshake->tls13_kex_modes);
Pengyu Lve6487fe2022-12-06 09:30:29 +08003145#endif
Jerry Yu95648b02023-12-06 15:03:34 +08003146
3147#if defined(MBEDTLS_SSL_EARLY_DATA)
3148 if (ssl->conf->early_data_enabled == MBEDTLS_SSL_EARLY_DATA_ENABLED &&
3149 ssl->conf->max_early_data_size > 0) {
Pengyu Lv94a42cc2023-12-06 10:04:17 +08003150 mbedtls_ssl_tls13_session_set_ticket_flags(
Jerry Yu95648b02023-12-06 15:03:34 +08003151 session, MBEDTLS_SSL_TLS1_3_TICKET_ALLOW_EARLY_DATA);
3152 }
3153#endif /* MBEDTLS_SSL_EARLY_DATA */
3154
Pengyu Lv4938a562023-01-16 11:28:49 +08003155 MBEDTLS_SSL_PRINT_TICKET_FLAGS(4, session->ticket_flags);
Pengyu Lv9f926952022-11-17 15:22:33 +08003156
Jerry Yue67bef42022-07-07 07:29:42 +00003157 /* Generate ticket_age_add */
Gilles Peskine449bd832023-01-11 14:50:10 +01003158 if ((ret = ssl->conf->f_rng(ssl->conf->p_rng,
3159 (unsigned char *) &session->ticket_age_add,
3160 sizeof(session->ticket_age_add)) != 0)) {
3161 MBEDTLS_SSL_DEBUG_RET(1, "generate_ticket_age_add", ret);
3162 return ret;
Jerry Yue67bef42022-07-07 07:29:42 +00003163 }
Gilles Peskine449bd832023-01-11 14:50:10 +01003164 MBEDTLS_SSL_DEBUG_MSG(3, ("ticket_age_add: %u",
3165 (unsigned int) session->ticket_age_add));
Jerry Yue67bef42022-07-07 07:29:42 +00003166
3167 /* Generate ticket_nonce */
Gilles Peskine449bd832023-01-11 14:50:10 +01003168 ret = ssl->conf->f_rng(ssl->conf->p_rng, ticket_nonce, ticket_nonce_size);
3169 if (ret != 0) {
3170 MBEDTLS_SSL_DEBUG_RET(1, "generate_ticket_nonce", ret);
3171 return ret;
Jerry Yue67bef42022-07-07 07:29:42 +00003172 }
Gilles Peskine449bd832023-01-11 14:50:10 +01003173 MBEDTLS_SSL_DEBUG_BUF(3, "ticket_nonce:",
3174 ticket_nonce, ticket_nonce_size);
Jerry Yue67bef42022-07-07 07:29:42 +00003175
3176 ciphersuite_info =
Gilles Peskine449bd832023-01-11 14:50:10 +01003177 (mbedtls_ssl_ciphersuite_t *) ssl->handshake->ciphersuite_info;
Dave Rodgman2eab4622023-10-05 13:30:37 +01003178 psa_hash_alg = mbedtls_md_psa_alg_from_type((mbedtls_md_type_t) ciphersuite_info->mac);
Gilles Peskine449bd832023-01-11 14:50:10 +01003179 hash_length = PSA_HASH_LENGTH(psa_hash_alg);
3180 if (hash_length == -1 ||
3181 (size_t) hash_length > sizeof(session->resumption_key)) {
3182 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Jerry Yufca4d572022-07-21 10:37:48 +08003183 }
3184
Jerry Yue67bef42022-07-07 07:29:42 +00003185 /* In this code the psk key length equals the length of the hash */
3186 session->resumption_key_len = hash_length;
3187 session->ciphersuite = ciphersuite_info->id;
3188
3189 /* Compute resumption key
3190 *
3191 * HKDF-Expand-Label( resumption_master_secret,
3192 * "resumption", ticket_nonce, Hash.length )
3193 */
3194 ret = mbedtls_ssl_tls13_hkdf_expand_label(
Gilles Peskine449bd832023-01-11 14:50:10 +01003195 psa_hash_alg,
3196 session->app_secrets.resumption_master_secret,
3197 hash_length,
3198 MBEDTLS_SSL_TLS1_3_LBL_WITH_LEN(resumption),
3199 ticket_nonce,
3200 ticket_nonce_size,
3201 session->resumption_key,
3202 hash_length);
Jerry Yue67bef42022-07-07 07:29:42 +00003203
Gilles Peskine449bd832023-01-11 14:50:10 +01003204 if (ret != 0) {
3205 MBEDTLS_SSL_DEBUG_RET(2,
3206 "Creating the ticket-resumed PSK failed",
3207 ret);
3208 return ret;
Jerry Yue67bef42022-07-07 07:29:42 +00003209 }
Gilles Peskine449bd832023-01-11 14:50:10 +01003210 MBEDTLS_SSL_DEBUG_BUF(3, "Ticket-resumed PSK",
3211 session->resumption_key,
3212 session->resumption_key_len);
Jerry Yue67bef42022-07-07 07:29:42 +00003213
Gilles Peskine449bd832023-01-11 14:50:10 +01003214 MBEDTLS_SSL_DEBUG_BUF(3, "resumption_master_secret",
3215 session->app_secrets.resumption_master_secret,
3216 hash_length);
Jerry Yue67bef42022-07-07 07:29:42 +00003217
Gilles Peskine449bd832023-01-11 14:50:10 +01003218 return 0;
Jerry Yue67bef42022-07-07 07:29:42 +00003219}
3220
3221/* This function creates a NewSessionTicket message in the following format:
3222 *
3223 * struct {
3224 * uint32 ticket_lifetime;
3225 * uint32 ticket_age_add;
3226 * opaque ticket_nonce<0..255>;
3227 * opaque ticket<1..2^16-1>;
3228 * Extension extensions<0..2^16-2>;
3229 * } NewSessionTicket;
3230 *
3231 * The ticket inside the NewSessionTicket message is an encrypted container
3232 * carrying the necessary information so that the server is later able to
3233 * re-start the communication.
3234 *
3235 * The following fields are placed inside the ticket by the
3236 * f_ticket_write() function:
3237 *
Jerry Yu0069abc2023-11-23 21:07:28 +08003238 * - creation time (ticket_creation_time)
3239 * - flags (ticket_flags)
Jerry Yue67bef42022-07-07 07:29:42 +00003240 * - age add (ticket_age_add)
Jerry Yu0069abc2023-11-23 21:07:28 +08003241 * - key (resumption_key)
3242 * - key length (resumption_key_len)
Jerry Yue67bef42022-07-07 07:29:42 +00003243 * - ciphersuite (ciphersuite)
Jerry Yu0069abc2023-11-23 21:07:28 +08003244 * - max_early_data_size (max_early_data_size)
Jerry Yue67bef42022-07-07 07:29:42 +00003245 */
3246MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01003247static int ssl_tls13_write_new_session_ticket_body(mbedtls_ssl_context *ssl,
3248 unsigned char *buf,
3249 unsigned char *end,
3250 size_t *out_len,
3251 unsigned char *ticket_nonce,
3252 size_t ticket_nonce_size)
Jerry Yue67bef42022-07-07 07:29:42 +00003253{
3254 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
3255 unsigned char *p = buf;
3256 mbedtls_ssl_session *session = ssl->session;
3257 size_t ticket_len;
3258 uint32_t ticket_lifetime;
Jerry Yu01da35e2022-12-12 15:09:22 +08003259 unsigned char *p_extensions_len;
Jerry Yue67bef42022-07-07 07:29:42 +00003260
3261 *out_len = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01003262 MBEDTLS_SSL_DEBUG_MSG(2, ("=> write NewSessionTicket msg"));
Jerry Yue67bef42022-07-07 07:29:42 +00003263
3264 /*
3265 * ticket_lifetime 4 bytes
3266 * ticket_age_add 4 bytes
3267 * ticket_nonce 1 + ticket_nonce_size bytes
3268 * ticket >=2 bytes
3269 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003270 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 4 + 4 + 1 + ticket_nonce_size + 2);
Jerry Yue67bef42022-07-07 07:29:42 +00003271
3272 /* Generate ticket and ticket_lifetime */
Ronald Cron3c0072b2023-11-22 10:00:14 +01003273#if defined(MBEDTLS_HAVE_TIME)
3274 session->ticket_creation_time = mbedtls_ms_time();
3275#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01003276 ret = ssl->conf->f_ticket_write(ssl->conf->p_ticket,
3277 session,
3278 p + 9 + ticket_nonce_size + 2,
3279 end,
3280 &ticket_len,
3281 &ticket_lifetime);
3282 if (ret != 0) {
3283 MBEDTLS_SSL_DEBUG_RET(1, "write_ticket", ret);
3284 return ret;
Jerry Yue67bef42022-07-07 07:29:42 +00003285 }
3286 /* RFC 8446 4.6.1
3287 * ticket_lifetime: Indicates the lifetime in seconds as a 32-bit
3288 * unsigned integer in network byte order from the time of ticket
3289 * issuance. Servers MUST NOT use any value greater than
3290 * 604800 seconds (7 days). The value of zero indicates that the
3291 * ticket should be discarded immediately. Clients MUST NOT cache
3292 * tickets for longer than 7 days, regardless of the ticket_lifetime,
3293 * and MAY delete tickets earlier based on local policy. A server
3294 * MAY treat a ticket as valid for a shorter period of time than what
3295 * is stated in the ticket_lifetime.
3296 */
Jerry Yu8e0174a2023-11-10 13:58:16 +08003297 if (ticket_lifetime > MBEDTLS_SSL_TLS1_3_MAX_ALLOWED_TICKET_LIFETIME) {
3298 ticket_lifetime = MBEDTLS_SSL_TLS1_3_MAX_ALLOWED_TICKET_LIFETIME;
Gilles Peskine449bd832023-01-11 14:50:10 +01003299 }
3300 MBEDTLS_PUT_UINT32_BE(ticket_lifetime, p, 0);
3301 MBEDTLS_SSL_DEBUG_MSG(3, ("ticket_lifetime: %u",
3302 (unsigned int) ticket_lifetime));
Jerry Yue67bef42022-07-07 07:29:42 +00003303
3304 /* Write ticket_age_add */
Gilles Peskine449bd832023-01-11 14:50:10 +01003305 MBEDTLS_PUT_UINT32_BE(session->ticket_age_add, p, 4);
3306 MBEDTLS_SSL_DEBUG_MSG(3, ("ticket_age_add: %u",
3307 (unsigned int) session->ticket_age_add));
Jerry Yue67bef42022-07-07 07:29:42 +00003308
3309 /* Write ticket_nonce */
Gilles Peskine449bd832023-01-11 14:50:10 +01003310 p[8] = (unsigned char) ticket_nonce_size;
3311 if (ticket_nonce_size > 0) {
3312 memcpy(p + 9, ticket_nonce, ticket_nonce_size);
Jerry Yue67bef42022-07-07 07:29:42 +00003313 }
3314 p += 9 + ticket_nonce_size;
3315
3316 /* Write ticket */
Gilles Peskine449bd832023-01-11 14:50:10 +01003317 MBEDTLS_PUT_UINT16_BE(ticket_len, p, 0);
Jerry Yue67bef42022-07-07 07:29:42 +00003318 p += 2;
Gilles Peskine449bd832023-01-11 14:50:10 +01003319 MBEDTLS_SSL_DEBUG_BUF(4, "ticket", p, ticket_len);
Jerry Yue67bef42022-07-07 07:29:42 +00003320 p += ticket_len;
3321
3322 /* Ticket Extensions
3323 *
Jerry Yu01da35e2022-12-12 15:09:22 +08003324 * Extension extensions<0..2^16-2>;
Jerry Yue67bef42022-07-07 07:29:42 +00003325 */
Jerry Yuedab6372022-10-31 14:37:31 +08003326 ssl->handshake->sent_extensions = MBEDTLS_SSL_EXT_MASK_NONE;
3327
Gilles Peskine449bd832023-01-11 14:50:10 +01003328 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 2);
Jerry Yu01da35e2022-12-12 15:09:22 +08003329 p_extensions_len = p;
Jerry Yue67bef42022-07-07 07:29:42 +00003330 p += 2;
3331
Jerry Yu01da35e2022-12-12 15:09:22 +08003332#if defined(MBEDTLS_SSL_EARLY_DATA)
Pengyu Lv94a42cc2023-12-06 10:04:17 +08003333 if (mbedtls_ssl_tls13_session_ticket_allow_early_data(session)) {
Jerry Yu95648b02023-12-06 15:03:34 +08003334 size_t output_len;
3335
Jerry Yuf135bac2023-11-23 18:10:51 +08003336 if ((ret = mbedtls_ssl_tls13_write_early_data_ext(
Jerry Yuc59c5862023-12-05 10:40:49 +08003337 ssl, 1, p, end, &output_len)) != 0) {
Jerry Yuf135bac2023-11-23 18:10:51 +08003338 MBEDTLS_SSL_DEBUG_RET(
3339 1, "mbedtls_ssl_tls13_write_early_data_ext", ret);
3340 return ret;
3341 }
3342 p += output_len;
Jerry Yu9e7f9bc2023-11-27 16:52:07 +08003343 } else {
3344 MBEDTLS_SSL_DEBUG_MSG(
3345 4, ("early_data not allowed, "
3346 "skip early_data extension in NewSessionTicket"));
Jerry Yu01da35e2022-12-12 15:09:22 +08003347 }
Jerry Yuf135bac2023-11-23 18:10:51 +08003348
Jerry Yu01da35e2022-12-12 15:09:22 +08003349#endif /* MBEDTLS_SSL_EARLY_DATA */
3350
3351 MBEDTLS_PUT_UINT16_BE(p - p_extensions_len - 2, p_extensions_len, 0);
3352
Jerry Yue67bef42022-07-07 07:29:42 +00003353 *out_len = p - buf;
Gilles Peskine449bd832023-01-11 14:50:10 +01003354 MBEDTLS_SSL_DEBUG_BUF(4, "ticket", buf, *out_len);
3355 MBEDTLS_SSL_DEBUG_MSG(2, ("<= write new session ticket"));
Jerry Yue67bef42022-07-07 07:29:42 +00003356
Jerry Yu7de2ff02022-11-08 21:43:46 +08003357 MBEDTLS_SSL_PRINT_EXTS(
Gilles Peskine449bd832023-01-11 14:50:10 +01003358 3, MBEDTLS_SSL_HS_NEW_SESSION_TICKET, ssl->handshake->sent_extensions);
Jerry Yu4b8f2f72022-10-31 13:31:22 +08003359
Gilles Peskine449bd832023-01-11 14:50:10 +01003360 return 0;
Jerry Yue67bef42022-07-07 07:29:42 +00003361}
3362
3363/*
Jerry Yua8d3c502022-10-30 14:51:23 +08003364 * Handler for MBEDTLS_SSL_TLS1_3_NEW_SESSION_TICKET
Jerry Yue67bef42022-07-07 07:29:42 +00003365 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003366static int ssl_tls13_write_new_session_ticket(mbedtls_ssl_context *ssl)
Jerry Yue67bef42022-07-07 07:29:42 +00003367{
3368 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
3369
Gilles Peskine449bd832023-01-11 14:50:10 +01003370 MBEDTLS_SSL_PROC_CHK_NEG(ssl_tls13_write_new_session_ticket_coordinate(ssl));
Jerry Yue67bef42022-07-07 07:29:42 +00003371
Gilles Peskine449bd832023-01-11 14:50:10 +01003372 if (ret == SSL_NEW_SESSION_TICKET_WRITE) {
Jerry Yue67bef42022-07-07 07:29:42 +00003373 unsigned char ticket_nonce[MBEDTLS_SSL_TLS1_3_TICKET_NONCE_LENGTH];
3374 unsigned char *buf;
3375 size_t buf_len, msg_len;
3376
Gilles Peskine449bd832023-01-11 14:50:10 +01003377 MBEDTLS_SSL_PROC_CHK(ssl_tls13_prepare_new_session_ticket(
3378 ssl, ticket_nonce, sizeof(ticket_nonce)));
Jerry Yue67bef42022-07-07 07:29:42 +00003379
Xiaokang Qian9f1747b2023-03-30 02:50:04 +00003380 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_start_handshake_msg(
3381 ssl, MBEDTLS_SSL_HS_NEW_SESSION_TICKET,
3382 &buf, &buf_len));
Jerry Yue67bef42022-07-07 07:29:42 +00003383
Gilles Peskine449bd832023-01-11 14:50:10 +01003384 MBEDTLS_SSL_PROC_CHK(ssl_tls13_write_new_session_ticket_body(
3385 ssl, buf, buf + buf_len, &msg_len,
3386 ticket_nonce, sizeof(ticket_nonce)));
Jerry Yue67bef42022-07-07 07:29:42 +00003387
Gilles Peskine449bd832023-01-11 14:50:10 +01003388 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_finish_handshake_msg(
3389 ssl, buf_len, msg_len));
Jerry Yufca4d572022-07-21 10:37:48 +08003390
Jerry Yu359e65f2022-09-22 23:47:43 +08003391 /* Limit session tickets count to one when resumption connection.
3392 *
3393 * See document of mbedtls_ssl_conf_new_session_tickets.
3394 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003395 if (ssl->handshake->resume == 1) {
Jerry Yu359e65f2022-09-22 23:47:43 +08003396 ssl->handshake->new_session_tickets_count = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01003397 } else {
Jerry Yu359e65f2022-09-22 23:47:43 +08003398 ssl->handshake->new_session_tickets_count--;
Gilles Peskine449bd832023-01-11 14:50:10 +01003399 }
Jerry Yub7e3fa72022-09-22 11:07:18 +08003400
Jerry Yua8d3c502022-10-30 14:51:23 +08003401 mbedtls_ssl_handshake_set_state(
Gilles Peskine449bd832023-01-11 14:50:10 +01003402 ssl, MBEDTLS_SSL_TLS1_3_NEW_SESSION_TICKET_FLUSH);
3403 } else {
3404 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_HANDSHAKE_OVER);
Jerry Yue67bef42022-07-07 07:29:42 +00003405 }
3406
Jerry Yue67bef42022-07-07 07:29:42 +00003407cleanup:
3408
Gilles Peskine449bd832023-01-11 14:50:10 +01003409 return ret;
Jerry Yue67bef42022-07-07 07:29:42 +00003410}
3411#endif /* MBEDTLS_SSL_SESSION_TICKETS */
3412
3413/*
XiaokangQiane8ff3502022-04-22 02:34:40 +00003414 * TLS 1.3 State Machine -- server side
XiaokangQian7807f9f2022-02-15 10:04:37 +00003415 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003416int mbedtls_ssl_tls13_handshake_server_step(mbedtls_ssl_context *ssl)
Jerry Yub9930e72021-08-06 17:11:51 +08003417{
XiaokangQian08037552022-04-20 07:16:41 +00003418 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
XiaokangQian7807f9f2022-02-15 10:04:37 +00003419
Gilles Peskine449bd832023-01-11 14:50:10 +01003420 if (ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER || ssl->handshake == NULL) {
3421 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
3422 }
XiaokangQian7807f9f2022-02-15 10:04:37 +00003423
Gilles Peskine449bd832023-01-11 14:50:10 +01003424 MBEDTLS_SSL_DEBUG_MSG(2, ("tls13 server state: %s(%d)",
Dave Rodgman2eab4622023-10-05 13:30:37 +01003425 mbedtls_ssl_states_str((mbedtls_ssl_states) ssl->state),
Gilles Peskine449bd832023-01-11 14:50:10 +01003426 ssl->state));
Jerry Yu6e81b272021-09-27 11:16:17 +08003427
Gilles Peskine449bd832023-01-11 14:50:10 +01003428 switch (ssl->state) {
XiaokangQian7807f9f2022-02-15 10:04:37 +00003429 /* start state */
3430 case MBEDTLS_SSL_HELLO_REQUEST:
Gilles Peskine449bd832023-01-11 14:50:10 +01003431 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_CLIENT_HELLO);
XiaokangQian08037552022-04-20 07:16:41 +00003432 ret = 0;
XiaokangQian7807f9f2022-02-15 10:04:37 +00003433 break;
3434
XiaokangQian7807f9f2022-02-15 10:04:37 +00003435 case MBEDTLS_SSL_CLIENT_HELLO:
Gilles Peskine449bd832023-01-11 14:50:10 +01003436 ret = ssl_tls13_process_client_hello(ssl);
3437 if (ret != 0) {
3438 MBEDTLS_SSL_DEBUG_RET(1, "ssl_tls13_process_client_hello", ret);
3439 }
Jerry Yuf41553b2022-05-09 22:20:30 +08003440 break;
XiaokangQian7807f9f2022-02-15 10:04:37 +00003441
Jerry Yuf41553b2022-05-09 22:20:30 +08003442 case MBEDTLS_SSL_HELLO_RETRY_REQUEST:
Gilles Peskine449bd832023-01-11 14:50:10 +01003443 ret = ssl_tls13_write_hello_retry_request(ssl);
3444 if (ret != 0) {
3445 MBEDTLS_SSL_DEBUG_RET(1, "ssl_tls13_write_hello_retry_request", ret);
3446 return ret;
Jerry Yuf41553b2022-05-09 22:20:30 +08003447 }
XiaokangQian7807f9f2022-02-15 10:04:37 +00003448 break;
3449
Jerry Yu5b64ae92022-03-30 17:15:02 +08003450 case MBEDTLS_SSL_SERVER_HELLO:
Gilles Peskine449bd832023-01-11 14:50:10 +01003451 ret = ssl_tls13_write_server_hello(ssl);
Jerry Yu5b64ae92022-03-30 17:15:02 +08003452 break;
3453
Xiaofei Baicba64af2022-02-15 10:00:56 +00003454 case MBEDTLS_SSL_ENCRYPTED_EXTENSIONS:
Gilles Peskine449bd832023-01-11 14:50:10 +01003455 ret = ssl_tls13_write_encrypted_extensions(ssl);
3456 if (ret != 0) {
3457 MBEDTLS_SSL_DEBUG_RET(1, "ssl_tls13_write_encrypted_extensions", ret);
3458 return ret;
Xiaofei Baicba64af2022-02-15 10:00:56 +00003459 }
Jerry Yu48330562022-05-06 21:35:44 +08003460 break;
Jerry Yu6a2cd9e2022-05-05 11:14:19 +08003461
Ronald Cron928cbd32022-10-04 16:14:26 +02003462#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED)
Xiaofei Bai9ca09d42022-02-14 12:57:18 +00003463 case MBEDTLS_SSL_CERTIFICATE_REQUEST:
Gilles Peskine449bd832023-01-11 14:50:10 +01003464 ret = ssl_tls13_write_certificate_request(ssl);
Xiaofei Bai9ca09d42022-02-14 12:57:18 +00003465 break;
Xiaofei Bai9ca09d42022-02-14 12:57:18 +00003466
Jerry Yu83da34e2022-04-16 13:59:52 +08003467 case MBEDTLS_SSL_SERVER_CERTIFICATE:
Gilles Peskine449bd832023-01-11 14:50:10 +01003468 ret = ssl_tls13_write_server_certificate(ssl);
Jerry Yu83da34e2022-04-16 13:59:52 +08003469 break;
3470
3471 case MBEDTLS_SSL_CERTIFICATE_VERIFY:
Gilles Peskine449bd832023-01-11 14:50:10 +01003472 ret = ssl_tls13_write_certificate_verify(ssl);
Jerry Yu83da34e2022-04-16 13:59:52 +08003473 break;
Ronald Cron928cbd32022-10-04 16:14:26 +02003474#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED */
Jerry Yu83da34e2022-04-16 13:59:52 +08003475
Gilles Peskine449bd832023-01-11 14:50:10 +01003476 /*
3477 * Injection of dummy-CCS's for middlebox compatibility
3478 */
Gabor Mezei7b39bf12022-05-24 16:04:14 +02003479#if defined(MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE)
Gabor Mezeif7044ea2022-06-28 16:01:49 +02003480 case MBEDTLS_SSL_SERVER_CCS_AFTER_HELLO_RETRY_REQUEST:
Gilles Peskine449bd832023-01-11 14:50:10 +01003481 ret = mbedtls_ssl_tls13_write_change_cipher_spec(ssl);
3482 if (ret == 0) {
3483 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_CLIENT_HELLO);
3484 }
Gabor Mezei7b39bf12022-05-24 16:04:14 +02003485 break;
3486
3487 case MBEDTLS_SSL_SERVER_CCS_AFTER_SERVER_HELLO:
Ronald Crone273f722024-02-13 18:22:26 +01003488 ret = mbedtls_ssl_tls13_write_change_cipher_spec(ssl);
3489 if (ret != 0) {
3490 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01003491 }
Ronald Cronfe59ff72024-01-24 14:31:50 +01003492 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_ENCRYPTED_EXTENSIONS);
Gabor Mezei7b39bf12022-05-24 16:04:14 +02003493 break;
3494#endif /* MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE */
3495
Jerry Yu69dd8d42022-04-16 12:51:26 +08003496 case MBEDTLS_SSL_SERVER_FINISHED:
Gilles Peskine449bd832023-01-11 14:50:10 +01003497 ret = ssl_tls13_write_server_finished(ssl);
Jerry Yu69dd8d42022-04-16 12:51:26 +08003498 break;
3499
Jerry Yu87b5ed42022-12-12 13:07:07 +08003500#if defined(MBEDTLS_SSL_EARLY_DATA)
3501 case MBEDTLS_SSL_END_OF_EARLY_DATA:
Jerry Yu59d420f2023-12-01 16:30:34 +08003502 ret = ssl_tls13_process_end_of_early_data(ssl);
Jerry Yu87b5ed42022-12-12 13:07:07 +08003503 break;
3504#endif /* MBEDTLS_SSL_EARLY_DATA */
3505
Jerry Yu69dd8d42022-04-16 12:51:26 +08003506 case MBEDTLS_SSL_CLIENT_FINISHED:
Gilles Peskine449bd832023-01-11 14:50:10 +01003507 ret = ssl_tls13_process_client_finished(ssl);
Jerry Yu69dd8d42022-04-16 12:51:26 +08003508 break;
3509
Jerry Yu69dd8d42022-04-16 12:51:26 +08003510 case MBEDTLS_SSL_HANDSHAKE_WRAPUP:
Gilles Peskine449bd832023-01-11 14:50:10 +01003511 ret = ssl_tls13_handshake_wrapup(ssl);
Jerry Yu69dd8d42022-04-16 12:51:26 +08003512 break;
3513
Ronald Cron766c0cd2022-10-18 12:17:11 +02003514#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED)
XiaokangQian6b916b12022-04-25 07:29:34 +00003515 case MBEDTLS_SSL_CLIENT_CERTIFICATE:
Gilles Peskine449bd832023-01-11 14:50:10 +01003516 ret = mbedtls_ssl_tls13_process_certificate(ssl);
3517 if (ret == 0) {
3518 if (ssl->session_negotiate->peer_cert != NULL) {
Ronald Cron209cae92022-06-07 10:30:19 +02003519 mbedtls_ssl_handshake_set_state(
Gilles Peskine449bd832023-01-11 14:50:10 +01003520 ssl, MBEDTLS_SSL_CLIENT_CERTIFICATE_VERIFY);
3521 } else {
3522 MBEDTLS_SSL_DEBUG_MSG(2, ("skip parse certificate verify"));
Ronald Cron209cae92022-06-07 10:30:19 +02003523 mbedtls_ssl_handshake_set_state(
Gilles Peskine449bd832023-01-11 14:50:10 +01003524 ssl, MBEDTLS_SSL_CLIENT_FINISHED);
Ronald Cron19385882022-06-15 16:26:13 +02003525 }
XiaokangQian6b916b12022-04-25 07:29:34 +00003526 }
3527 break;
3528
3529 case MBEDTLS_SSL_CLIENT_CERTIFICATE_VERIFY:
Gilles Peskine449bd832023-01-11 14:50:10 +01003530 ret = mbedtls_ssl_tls13_process_certificate_verify(ssl);
3531 if (ret == 0) {
XiaokangQian6b916b12022-04-25 07:29:34 +00003532 mbedtls_ssl_handshake_set_state(
Gilles Peskine449bd832023-01-11 14:50:10 +01003533 ssl, MBEDTLS_SSL_CLIENT_FINISHED);
XiaokangQian6b916b12022-04-25 07:29:34 +00003534 }
3535 break;
Ronald Cron766c0cd2022-10-18 12:17:11 +02003536#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED */
XiaokangQian6b916b12022-04-25 07:29:34 +00003537
Jerry Yue67bef42022-07-07 07:29:42 +00003538#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Jerry Yua8d3c502022-10-30 14:51:23 +08003539 case MBEDTLS_SSL_TLS1_3_NEW_SESSION_TICKET:
Gilles Peskine449bd832023-01-11 14:50:10 +01003540 ret = ssl_tls13_write_new_session_ticket(ssl);
3541 if (ret != 0) {
3542 MBEDTLS_SSL_DEBUG_RET(1,
3543 "ssl_tls13_write_new_session_ticket ",
3544 ret);
Jerry Yue67bef42022-07-07 07:29:42 +00003545 }
3546 break;
Jerry Yua8d3c502022-10-30 14:51:23 +08003547 case MBEDTLS_SSL_TLS1_3_NEW_SESSION_TICKET_FLUSH:
Jerry Yue67bef42022-07-07 07:29:42 +00003548 /* This state is necessary to do the flush of the New Session
Jerry Yua8d3c502022-10-30 14:51:23 +08003549 * Ticket message written in MBEDTLS_SSL_TLS1_3_NEW_SESSION_TICKET
Jerry Yue67bef42022-07-07 07:29:42 +00003550 * as part of ssl_prepare_handshake_step.
3551 */
3552 ret = 0;
Jerry Yud4e75002022-08-09 13:33:50 +08003553
Gilles Peskine449bd832023-01-11 14:50:10 +01003554 if (ssl->handshake->new_session_tickets_count == 0) {
3555 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_HANDSHAKE_OVER);
3556 } else {
Xiaokang Qian9f1747b2023-03-30 02:50:04 +00003557 mbedtls_ssl_handshake_set_state(
3558 ssl, MBEDTLS_SSL_TLS1_3_NEW_SESSION_TICKET);
Gilles Peskine449bd832023-01-11 14:50:10 +01003559 }
Jerry Yue67bef42022-07-07 07:29:42 +00003560 break;
3561
3562#endif /* MBEDTLS_SSL_SESSION_TICKETS */
3563
XiaokangQian7807f9f2022-02-15 10:04:37 +00003564 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01003565 MBEDTLS_SSL_DEBUG_MSG(1, ("invalid state %d", ssl->state));
3566 return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
XiaokangQian7807f9f2022-02-15 10:04:37 +00003567 }
3568
Gilles Peskine449bd832023-01-11 14:50:10 +01003569 return ret;
Jerry Yub9930e72021-08-06 17:11:51 +08003570}
Jerry Yu3cc4c2a2021-08-06 16:29:08 +08003571
Jerry Yufb4b6472022-01-27 15:03:26 +08003572#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_PROTO_TLS1_3 */