blob: 873f90979338454a9e452b0d3164c627014ec34f [file] [log] [blame]
Jerry Yu3cc4c2a2021-08-06 16:29:08 +08001/*
Jerry Yub9930e72021-08-06 17:11:51 +08002 * TLS 1.3 server-side functions
Jerry Yu3cc4c2a2021-08-06 16:29:08 +08003 *
4 * Copyright The Mbed TLS Contributors
Dave Rodgman16799db2023-11-02 19:47:20 +00005 * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
Gilles Peskine449bd832023-01-11 14:50:10 +01006 */
Jerry Yu3cc4c2a2021-08-06 16:29:08 +08007
8#include "common.h"
9
Jerry Yufb4b6472022-01-27 15:03:26 +080010#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_PROTO_TLS1_3)
Jerry Yu3cc4c2a2021-08-06 16:29:08 +080011
Valerio Settib4f50762024-01-17 10:24:52 +010012#include "debug_internal.h"
Xiaofei Baicba64af2022-02-15 10:00:56 +000013#include "mbedtls/error.h"
14#include "mbedtls/platform.h"
Jerry Yu1c105562022-07-10 06:32:38 +000015#include "mbedtls/constant_time.h"
Manuel Pégourié-Gonnardd55d66f2023-06-20 10:14:58 +020016#include "mbedtls/oid.h"
Valerio Setti384fbde2024-01-02 13:26:40 +010017#include "mbedtls/psa_util.h"
Jerry Yu3bf2c642022-03-30 22:02:12 +080018
Xiaofei Bai9ca09d42022-02-14 12:57:18 +000019#include "ssl_misc.h"
20#include "ssl_tls13_keys.h"
21#include "ssl_debug_helpers.h"
22
Jerry Yuf35ba382022-08-23 17:58:26 +080023
Jerry Yuc5a23a02022-08-25 10:51:44 +080024static const mbedtls_ssl_ciphersuite_t *ssl_tls13_validate_peer_ciphersuite(
Gilles Peskine449bd832023-01-11 14:50:10 +010025 mbedtls_ssl_context *ssl,
26 unsigned int cipher_suite)
Jerry Yuf35ba382022-08-23 17:58:26 +080027{
28 const mbedtls_ssl_ciphersuite_t *ciphersuite_info;
Gilles Peskine449bd832023-01-11 14:50:10 +010029 if (!mbedtls_ssl_tls13_cipher_suite_is_offered(ssl, cipher_suite)) {
30 return NULL;
Jerry Yuf35ba382022-08-23 17:58:26 +080031 }
Gilles Peskine449bd832023-01-11 14:50:10 +010032
33 ciphersuite_info = mbedtls_ssl_ciphersuite_from_id(cipher_suite);
34 if ((mbedtls_ssl_validate_ciphersuite(ssl, ciphersuite_info,
35 ssl->tls_version,
36 ssl->tls_version) != 0)) {
37 return NULL;
38 }
39 return ciphersuite_info;
Jerry Yuf35ba382022-08-23 17:58:26 +080040}
41
Ronald Cron41a443a2022-10-04 16:38:25 +020042#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED)
Jerry Yue19e3b92022-07-08 12:04:51 +000043/* From RFC 8446:
44 *
45 * enum { psk_ke(0), psk_dhe_ke(1), (255) } PskKeyExchangeMode;
46 * struct {
47 * PskKeyExchangeMode ke_modes<1..255>;
48 * } PskKeyExchangeModes;
49 */
Jerry Yu6e74a7e2022-07-20 20:49:32 +080050MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +010051static int ssl_tls13_parse_key_exchange_modes_ext(mbedtls_ssl_context *ssl,
52 const unsigned char *buf,
53 const unsigned char *end)
Jerry Yue19e3b92022-07-08 12:04:51 +000054{
Jerry Yu299e31f2022-07-13 23:06:36 +080055 const unsigned char *p = buf;
Jerry Yue19e3b92022-07-08 12:04:51 +000056 size_t ke_modes_len;
57 int ke_modes = 0;
58
Jerry Yu854dd9e2022-07-15 14:28:27 +080059 /* Read ke_modes length (1 Byte) */
Gilles Peskine449bd832023-01-11 14:50:10 +010060 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, 1);
Jerry Yu299e31f2022-07-13 23:06:36 +080061 ke_modes_len = *p++;
Jerry Yue19e3b92022-07-08 12:04:51 +000062 /* Currently, there are only two PSK modes, so even without looking
63 * at the content, something's wrong if the list has more than 2 items. */
Gilles Peskine449bd832023-01-11 14:50:10 +010064 if (ke_modes_len > 2) {
65 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER,
66 MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER);
67 return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
Jerry Yu299e31f2022-07-13 23:06:36 +080068 }
Jerry Yue19e3b92022-07-08 12:04:51 +000069
Gilles Peskine449bd832023-01-11 14:50:10 +010070 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, ke_modes_len);
Jerry Yue19e3b92022-07-08 12:04:51 +000071
Gilles Peskine449bd832023-01-11 14:50:10 +010072 while (ke_modes_len-- != 0) {
73 switch (*p++) {
74 case MBEDTLS_SSL_TLS1_3_PSK_MODE_PURE:
75 ke_modes |= MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK;
76 MBEDTLS_SSL_DEBUG_MSG(3, ("Found PSK KEX MODE"));
77 break;
78 case MBEDTLS_SSL_TLS1_3_PSK_MODE_ECDHE:
79 ke_modes |= MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK_EPHEMERAL;
80 MBEDTLS_SSL_DEBUG_MSG(3, ("Found PSK_EPHEMERAL KEX MODE"));
81 break;
82 default:
83 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER,
84 MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER);
85 return MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER;
Jerry Yue19e3b92022-07-08 12:04:51 +000086 }
87 }
88
89 ssl->handshake->tls13_kex_modes = ke_modes;
Gilles Peskine449bd832023-01-11 14:50:10 +010090 return 0;
Jerry Yue19e3b92022-07-08 12:04:51 +000091}
Jerry Yu1c105562022-07-10 06:32:38 +000092
Ronald Cronfbae94a2023-12-05 18:15:14 +010093#define SSL_TLS1_3_PSK_IDENTITY_DOES_NOT_MATCH 2
94#define SSL_TLS1_3_PSK_IDENTITY_MATCH_BUT_PSK_NOT_USABLE 1
95#define SSL_TLS1_3_PSK_IDENTITY_MATCH 0
Jerry Yu95699e72022-08-21 19:22:23 +080096
97#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Pengyu Lv29daf4a2023-10-30 17:13:30 +080098MBEDTLS_CHECK_RETURN_CRITICAL
Pengyu Lvbc4aab72023-12-01 15:37:24 +080099static int ssl_tls13_key_exchange_is_psk_available(mbedtls_ssl_context *ssl);
Pengyu Lv29daf4a2023-10-30 17:13:30 +0800100MBEDTLS_CHECK_RETURN_CRITICAL
Pengyu Lvbc4aab72023-12-01 15:37:24 +0800101static int ssl_tls13_key_exchange_is_psk_ephemeral_available(mbedtls_ssl_context *ssl);
Jerry Yu95699e72022-08-21 19:22:23 +0800102
103MBEDTLS_CHECK_RETURN_CRITICAL
104static int ssl_tls13_offered_psks_check_identity_match_ticket(
Gilles Peskine449bd832023-01-11 14:50:10 +0100105 mbedtls_ssl_context *ssl,
106 const unsigned char *identity,
107 size_t identity_len,
108 uint32_t obfuscated_ticket_age,
109 mbedtls_ssl_session *session)
Jerry Yu95699e72022-08-21 19:22:23 +0800110{
Jerry Yufd310eb2022-09-06 09:16:35 +0800111 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yu95699e72022-08-21 19:22:23 +0800112 unsigned char *ticket_buffer;
Pengyu Lv29daf4a2023-10-30 17:13:30 +0800113 unsigned int key_exchanges;
Jerry Yu8d4bbba2022-09-13 14:15:48 +0800114#if defined(MBEDTLS_HAVE_TIME)
Jerry Yucebffc32022-12-15 18:00:05 +0800115 mbedtls_ms_time_t now;
116 mbedtls_ms_time_t server_age;
Jerry Yu713ce1f2023-11-17 15:32:12 +0800117 uint32_t client_age;
Jerry Yucebffc32022-12-15 18:00:05 +0800118 mbedtls_ms_time_t age_diff;
Jerry Yu8d4bbba2022-09-13 14:15:48 +0800119#endif
Jerry Yu95699e72022-08-21 19:22:23 +0800120
121 ((void) obfuscated_ticket_age);
122
Gilles Peskine449bd832023-01-11 14:50:10 +0100123 MBEDTLS_SSL_DEBUG_MSG(2, ("=> check_identity_match_ticket"));
Jerry Yu95699e72022-08-21 19:22:23 +0800124
Jerry Yufd310eb2022-09-06 09:16:35 +0800125 /* Ticket parser is not configured, Skip */
Gilles Peskine449bd832023-01-11 14:50:10 +0100126 if (ssl->conf->f_ticket_parse == NULL || identity_len == 0) {
Ronald Cronfbae94a2023-12-05 18:15:14 +0100127 return SSL_TLS1_3_PSK_IDENTITY_DOES_NOT_MATCH;
Gilles Peskine449bd832023-01-11 14:50:10 +0100128 }
Jerry Yu95699e72022-08-21 19:22:23 +0800129
Jerry Yu4746b102022-09-13 11:11:48 +0800130 /* We create a copy of the encrypted ticket since the ticket parsing
131 * function is allowed to use its input buffer as an output buffer
132 * (in-place decryption). We do, however, need the original buffer for
133 * computing the PSK binder value.
Jerry Yu95699e72022-08-21 19:22:23 +0800134 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100135 ticket_buffer = mbedtls_calloc(1, identity_len);
136 if (ticket_buffer == NULL) {
137 MBEDTLS_SSL_DEBUG_MSG(1, ("buffer too small"));
138 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
Jerry Yu95699e72022-08-21 19:22:23 +0800139 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100140 memcpy(ticket_buffer, identity, identity_len);
Jerry Yu95699e72022-08-21 19:22:23 +0800141
Ronald Cronfbae94a2023-12-05 18:15:14 +0100142 ret = ssl->conf->f_ticket_parse(ssl->conf->p_ticket,
143 session,
144 ticket_buffer, identity_len);
145 if (ret == 0) {
146 ret = SSL_TLS1_3_PSK_IDENTITY_MATCH;
147 } else {
148 if (ret == MBEDTLS_ERR_SSL_SESSION_TICKET_EXPIRED) {
Gilles Peskine449bd832023-01-11 14:50:10 +0100149 MBEDTLS_SSL_DEBUG_MSG(3, ("ticket is expired"));
Ronald Cronfbae94a2023-12-05 18:15:14 +0100150 ret = SSL_TLS1_3_PSK_IDENTITY_MATCH_BUT_PSK_NOT_USABLE;
Gilles Peskine449bd832023-01-11 14:50:10 +0100151 } else {
Ronald Cronfbae94a2023-12-05 18:15:14 +0100152 if (ret == MBEDTLS_ERR_SSL_INVALID_MAC) {
153 MBEDTLS_SSL_DEBUG_MSG(3, ("ticket is not authentic"));
154 } else {
155 MBEDTLS_SSL_DEBUG_RET(1, "ticket_parse", ret);
156 }
157 ret = SSL_TLS1_3_PSK_IDENTITY_DOES_NOT_MATCH;
Gilles Peskine449bd832023-01-11 14:50:10 +0100158 }
Jerry Yu95699e72022-08-21 19:22:23 +0800159 }
160
161 /* We delete the temporary buffer */
Gilles Peskine449bd832023-01-11 14:50:10 +0100162 mbedtls_free(ticket_buffer);
Jerry Yu95699e72022-08-21 19:22:23 +0800163
Ronald Cronfbae94a2023-12-05 18:15:14 +0100164 if (ret != SSL_TLS1_3_PSK_IDENTITY_MATCH) {
165 goto exit;
Jerry Yuce3b95e2023-10-31 16:02:04 +0800166 }
Jerry Yuce3b95e2023-10-31 16:02:04 +0800167
Ronald Cronfbae94a2023-12-05 18:15:14 +0100168 ret = SSL_TLS1_3_PSK_IDENTITY_MATCH_BUT_PSK_NOT_USABLE;
169
170 if (session->tls_version != MBEDTLS_SSL_VERSION_TLS1_3) {
171 MBEDTLS_SSL_DEBUG_MSG(3, ("Ticket TLS version is not 1.3."));
Jerry Yu8d4bbba2022-09-13 14:15:48 +0800172 goto exit;
173 }
Jerry Yu95699e72022-08-21 19:22:23 +0800174
Pengyu Lv3eb49be2022-12-05 16:35:12 +0800175 /* RFC 8446 section 4.2.9
176 *
177 * Servers SHOULD NOT send NewSessionTicket with tickets that are not
178 * compatible with the advertised modes; however, if a server does so,
179 * the impact will just be that the client's attempts at resumption fail.
180 *
181 * We regard the ticket with incompatible key exchange modes as not match.
182 */
Pengyu Lv29daf4a2023-10-30 17:13:30 +0800183 MBEDTLS_SSL_PRINT_TICKET_FLAGS(4, session->ticket_flags);
Pengyu Lv29daf4a2023-10-30 17:13:30 +0800184
185 key_exchanges = 0;
Pengyu Lv94a42cc2023-12-06 10:04:17 +0800186 if (mbedtls_ssl_tls13_session_ticket_allow_psk_ephemeral(session) &&
Pengyu Lvbc4aab72023-12-01 15:37:24 +0800187 ssl_tls13_key_exchange_is_psk_ephemeral_available(ssl)) {
Pengyu Lv29daf4a2023-10-30 17:13:30 +0800188 key_exchanges |= MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK_EPHEMERAL;
189 }
Pengyu Lv94a42cc2023-12-06 10:04:17 +0800190 if (mbedtls_ssl_tls13_session_ticket_allow_psk(session) &&
Pengyu Lvbc4aab72023-12-01 15:37:24 +0800191 ssl_tls13_key_exchange_is_psk_available(ssl)) {
Pengyu Lv29daf4a2023-10-30 17:13:30 +0800192 key_exchanges |= MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK;
193 }
194
195 if (key_exchanges == 0) {
Pengyu Lv3eb49be2022-12-05 16:35:12 +0800196 MBEDTLS_SSL_DEBUG_MSG(3, ("No suitable key exchange mode"));
197 goto exit;
198 }
199
Gilles Peskine449bd832023-01-11 14:50:10 +0100200#if defined(MBEDTLS_HAVE_TIME)
Jerry Yucebffc32022-12-15 18:00:05 +0800201 now = mbedtls_ms_time();
Gilles Peskine449bd832023-01-11 14:50:10 +0100202
Jerry Yu25ba4d42023-11-10 14:12:20 +0800203 if (now < session->ticket_creation_time) {
Gilles Peskine449bd832023-01-11 14:50:10 +0100204 MBEDTLS_SSL_DEBUG_MSG(
Jerry Yu713ce1f2023-11-17 15:32:12 +0800205 3, ("Invalid ticket creation time ( now = %" MBEDTLS_PRINTF_MS_TIME
206 ", creation_time = %" MBEDTLS_PRINTF_MS_TIME " )",
Jerry Yu25ba4d42023-11-10 14:12:20 +0800207 now, session->ticket_creation_time));
Gilles Peskine449bd832023-01-11 14:50:10 +0100208 goto exit;
209 }
210
Jerry Yu25ba4d42023-11-10 14:12:20 +0800211 server_age = now - session->ticket_creation_time;
Jerry Yu95699e72022-08-21 19:22:23 +0800212
Jerry Yu8d4bbba2022-09-13 14:15:48 +0800213 /* RFC 8446 section 4.6.1
214 *
215 * Servers MUST NOT use any value greater than 604800 seconds (7 days).
216 *
217 * RFC 8446 section 4.2.11.1
218 *
219 * Clients MUST NOT attempt to use tickets which have ages greater than
220 * the "ticket_lifetime" value which was provided with the ticket.
221 *
Jerry Yu8d4bbba2022-09-13 14:15:48 +0800222 */
Jerry Yu8e0174a2023-11-10 13:58:16 +0800223 if (server_age > MBEDTLS_SSL_TLS1_3_MAX_ALLOWED_TICKET_LIFETIME * 1000) {
Jerry Yu8d4bbba2022-09-13 14:15:48 +0800224 MBEDTLS_SSL_DEBUG_MSG(
Jerry Yud84c14f2023-11-16 13:33:57 +0800225 3, ("Ticket age exceeds limitation ticket_age = %" MBEDTLS_PRINTF_MS_TIME,
Jerry Yucebffc32022-12-15 18:00:05 +0800226 server_age));
Jerry Yu8d4bbba2022-09-13 14:15:48 +0800227 goto exit;
228 }
Jerry Yu95699e72022-08-21 19:22:23 +0800229
Jerry Yu8d4bbba2022-09-13 14:15:48 +0800230 /* RFC 8446 section 4.2.10
231 *
232 * For PSKs provisioned via NewSessionTicket, a server MUST validate that
233 * the ticket age for the selected PSK identity (computed by subtracting
234 * ticket_age_add from PskIdentity.obfuscated_ticket_age modulo 2^32) is
235 * within a small tolerance of the time since the ticket was issued.
Jerry Yuf7dad3c2022-09-14 22:31:39 +0800236 *
Jerry Yu31b601a2023-11-10 11:27:21 +0800237 * NOTE: The typical accuracy of an RTC crystal is ±100 to ±20 parts per
238 * million (360 to 72 milliseconds per hour). Default tolerance
Jerry Yu9cb953a2023-11-15 09:54:11 +0800239 * window is 6s, thus in the worst case clients and servers must
Jerry Yu31b601a2023-11-10 11:27:21 +0800240 * sync up their system time every 6000/360/2~=8 hours.
Jerry Yu8d4bbba2022-09-13 14:15:48 +0800241 */
Jerry Yucebffc32022-12-15 18:00:05 +0800242 client_age = obfuscated_ticket_age - session->ticket_age_add;
Jerry Yu60e99722023-11-20 09:55:24 +0800243 age_diff = server_age - (mbedtls_ms_time_t) client_age;
Jerry Yu28e7c552023-11-10 12:05:56 +0800244 if (age_diff < -MBEDTLS_SSL_TLS1_3_TICKET_AGE_TOLERANCE ||
Jerry Yucebffc32022-12-15 18:00:05 +0800245 age_diff > MBEDTLS_SSL_TLS1_3_TICKET_AGE_TOLERANCE) {
Jerry Yu8d4bbba2022-09-13 14:15:48 +0800246 MBEDTLS_SSL_DEBUG_MSG(
Jerry Yuf16efbc2023-10-30 11:06:24 +0800247 3, ("Ticket age outside tolerance window ( diff = %"
248 MBEDTLS_PRINTF_MS_TIME ")",
Jerry Yucebffc32022-12-15 18:00:05 +0800249 age_diff));
Jerry Yu8d4bbba2022-09-13 14:15:48 +0800250 goto exit;
251 }
Jerry Yu95699e72022-08-21 19:22:23 +0800252#endif /* MBEDTLS_HAVE_TIME */
Jerry Yu8d4bbba2022-09-13 14:15:48 +0800253
Ronald Cronfbae94a2023-12-05 18:15:14 +0100254 ret = SSL_TLS1_3_PSK_IDENTITY_MATCH;
255
Jerry Yu8d4bbba2022-09-13 14:15:48 +0800256exit:
Ronald Cronfbae94a2023-12-05 18:15:14 +0100257 if (ret != SSL_TLS1_3_PSK_IDENTITY_MATCH) {
Gilles Peskine449bd832023-01-11 14:50:10 +0100258 mbedtls_ssl_session_free(session);
259 }
Jerry Yu95699e72022-08-21 19:22:23 +0800260
Gilles Peskine449bd832023-01-11 14:50:10 +0100261 MBEDTLS_SSL_DEBUG_MSG(2, ("<= check_identity_match_ticket"));
262 return ret;
Jerry Yu95699e72022-08-21 19:22:23 +0800263}
264#endif /* MBEDTLS_SSL_SESSION_TICKETS */
265
Jerry Yu6e74a7e2022-07-20 20:49:32 +0800266MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yu1c105562022-07-10 06:32:38 +0000267static int ssl_tls13_offered_psks_check_identity_match(
Gilles Peskine449bd832023-01-11 14:50:10 +0100268 mbedtls_ssl_context *ssl,
269 const unsigned char *identity,
270 size_t identity_len,
271 uint32_t obfuscated_ticket_age,
272 int *psk_type,
273 mbedtls_ssl_session *session)
Jerry Yu1c105562022-07-10 06:32:38 +0000274{
Ronald Cron606671e2023-02-17 11:36:33 +0100275 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
276
Jerry Yu95699e72022-08-21 19:22:23 +0800277 ((void) session);
278 ((void) obfuscated_ticket_age);
Jerry Yu5725f1c2022-08-21 17:27:16 +0800279 *psk_type = MBEDTLS_SSL_TLS1_3_PSK_EXTERNAL;
Jerry Yu95699e72022-08-21 19:22:23 +0800280
Gilles Peskine449bd832023-01-11 14:50:10 +0100281 MBEDTLS_SSL_DEBUG_BUF(4, "identity", identity, identity_len);
Jerry Yu95699e72022-08-21 19:22:23 +0800282 ssl->handshake->resume = 0;
283
Jerry Yu95699e72022-08-21 19:22:23 +0800284#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Ronald Cron7a30cf52024-02-23 14:38:59 +0100285 ret = ssl_tls13_offered_psks_check_identity_match_ticket(
286 ssl, identity, identity_len, obfuscated_ticket_age, session);
287 if (ret == SSL_TLS1_3_PSK_IDENTITY_MATCH) {
Jerry Yu95699e72022-08-21 19:22:23 +0800288 ssl->handshake->resume = 1;
289 *psk_type = MBEDTLS_SSL_TLS1_3_PSK_RESUMPTION;
Ronald Cron606671e2023-02-17 11:36:33 +0100290 ret = mbedtls_ssl_set_hs_psk(ssl,
291 session->resumption_key,
292 session->resumption_key_len);
293 if (ret != 0) {
294 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_set_hs_psk", ret);
295 return ret;
296 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100297
298 MBEDTLS_SSL_DEBUG_BUF(4, "Ticket-resumed PSK:",
299 session->resumption_key,
300 session->resumption_key_len);
301 MBEDTLS_SSL_DEBUG_MSG(4, ("ticket: obfuscated_ticket_age: %u",
302 (unsigned) obfuscated_ticket_age));
Ronald Cronfbae94a2023-12-05 18:15:14 +0100303 return SSL_TLS1_3_PSK_IDENTITY_MATCH;
Ronald Cron7a30cf52024-02-23 14:38:59 +0100304 } else if (ret == SSL_TLS1_3_PSK_IDENTITY_MATCH_BUT_PSK_NOT_USABLE) {
305 return SSL_TLS1_3_PSK_IDENTITY_MATCH_BUT_PSK_NOT_USABLE;
Jerry Yu95699e72022-08-21 19:22:23 +0800306 }
307#endif /* MBEDTLS_SSL_SESSION_TICKETS */
308
Jerry Yu1c105562022-07-10 06:32:38 +0000309 /* Check identity with external configured function */
Gilles Peskine449bd832023-01-11 14:50:10 +0100310 if (ssl->conf->f_psk != NULL) {
311 if (ssl->conf->f_psk(
312 ssl->conf->p_psk, ssl, identity, identity_len) == 0) {
Ronald Cronfbae94a2023-12-05 18:15:14 +0100313 return SSL_TLS1_3_PSK_IDENTITY_MATCH;
Jerry Yu1c105562022-07-10 06:32:38 +0000314 }
Ronald Cronfbae94a2023-12-05 18:15:14 +0100315 return SSL_TLS1_3_PSK_IDENTITY_DOES_NOT_MATCH;
Jerry Yu1c105562022-07-10 06:32:38 +0000316 }
317
Gilles Peskine449bd832023-01-11 14:50:10 +0100318 MBEDTLS_SSL_DEBUG_BUF(5, "identity", identity, identity_len);
Jerry Yu1c105562022-07-10 06:32:38 +0000319 /* Check identity with pre-configured psk */
Gilles Peskine449bd832023-01-11 14:50:10 +0100320 if (ssl->conf->psk_identity != NULL &&
Jerry Yu2f0abc92022-07-22 19:34:48 +0800321 identity_len == ssl->conf->psk_identity_len &&
Gilles Peskine449bd832023-01-11 14:50:10 +0100322 mbedtls_ct_memcmp(ssl->conf->psk_identity,
323 identity, identity_len) == 0) {
Ronald Cron606671e2023-02-17 11:36:33 +0100324 ret = mbedtls_ssl_set_hs_psk(ssl, ssl->conf->psk, ssl->conf->psk_len);
325 if (ret != 0) {
326 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_set_hs_psk", ret);
327 return ret;
328 }
Ronald Cronfbae94a2023-12-05 18:15:14 +0100329 return SSL_TLS1_3_PSK_IDENTITY_MATCH;
Jerry Yu1c105562022-07-10 06:32:38 +0000330 }
331
Ronald Cronfbae94a2023-12-05 18:15:14 +0100332 return SSL_TLS1_3_PSK_IDENTITY_DOES_NOT_MATCH;
Jerry Yu1c105562022-07-10 06:32:38 +0000333}
334
Ronald Cron6e311272023-12-05 17:57:01 +0100335#define SSL_TLS1_3_BINDER_DOES_NOT_MATCH 1
336#define SSL_TLS1_3_BINDER_MATCH 0
Jerry Yu6e74a7e2022-07-20 20:49:32 +0800337MBEDTLS_CHECK_RETURN_CRITICAL
Xiaokang Qian9f1747b2023-03-30 02:50:04 +0000338static int ssl_tls13_offered_psks_check_binder_match(
339 mbedtls_ssl_context *ssl,
340 const unsigned char *binder, size_t binder_len,
341 int psk_type, psa_algorithm_t psk_hash_alg)
Jerry Yu1c105562022-07-10 06:32:38 +0000342{
343 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yu96a2e362022-07-21 15:11:34 +0800344
Jerry Yudaf375a2022-07-20 21:31:43 +0800345 unsigned char transcript[PSA_HASH_MAX_SIZE];
Jerry Yu1c105562022-07-10 06:32:38 +0000346 size_t transcript_len;
Jerry Yu2f0abc92022-07-22 19:34:48 +0800347 unsigned char *psk;
Jerry Yu96a2e362022-07-21 15:11:34 +0800348 size_t psk_len;
Jerry Yudaf375a2022-07-20 21:31:43 +0800349 unsigned char server_computed_binder[PSA_HASH_MAX_SIZE];
Jerry Yu1c105562022-07-10 06:32:38 +0000350
Jerry Yu1c105562022-07-10 06:32:38 +0000351 /* Get current state of handshake transcript. */
Jerry Yu29d9faa2022-08-23 17:52:45 +0800352 ret = mbedtls_ssl_get_handshake_transcript(
Manuel Pégourié-Gonnard2d6d9932023-03-28 11:38:08 +0200353 ssl, mbedtls_md_type_from_psa_alg(psk_hash_alg),
Gilles Peskine449bd832023-01-11 14:50:10 +0100354 transcript, sizeof(transcript), &transcript_len);
355 if (ret != 0) {
356 return ret;
357 }
Jerry Yu1c105562022-07-10 06:32:38 +0000358
Gilles Peskine449bd832023-01-11 14:50:10 +0100359 ret = mbedtls_ssl_tls13_export_handshake_psk(ssl, &psk, &psk_len);
360 if (ret != 0) {
361 return ret;
362 }
Jerry Yu96a2e362022-07-21 15:11:34 +0800363
Gilles Peskine449bd832023-01-11 14:50:10 +0100364 ret = mbedtls_ssl_tls13_create_psk_binder(ssl, psk_hash_alg,
365 psk, psk_len, psk_type,
366 transcript,
367 server_computed_binder);
Jerry Yu96a2e362022-07-21 15:11:34 +0800368#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +0100369 mbedtls_free((void *) psk);
Jerry Yu96a2e362022-07-21 15:11:34 +0800370#endif
Gilles Peskine449bd832023-01-11 14:50:10 +0100371 if (ret != 0) {
372 MBEDTLS_SSL_DEBUG_MSG(1, ("PSK binder calculation failed."));
373 return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
Jerry Yu1c105562022-07-10 06:32:38 +0000374 }
375
Gilles Peskine449bd832023-01-11 14:50:10 +0100376 MBEDTLS_SSL_DEBUG_BUF(3, "psk binder ( computed ): ",
377 server_computed_binder, transcript_len);
378 MBEDTLS_SSL_DEBUG_BUF(3, "psk binder ( received ): ", binder, binder_len);
Jerry Yu1c105562022-07-10 06:32:38 +0000379
Gilles Peskine449bd832023-01-11 14:50:10 +0100380 if (mbedtls_ct_memcmp(server_computed_binder, binder, binder_len) == 0) {
Ronald Cron6e311272023-12-05 17:57:01 +0100381 return SSL_TLS1_3_BINDER_MATCH;
Jerry Yu1c105562022-07-10 06:32:38 +0000382 }
383
Gilles Peskine449bd832023-01-11 14:50:10 +0100384 mbedtls_platform_zeroize(server_computed_binder,
385 sizeof(server_computed_binder));
Ronald Cron6e311272023-12-05 17:57:01 +0100386 return SSL_TLS1_3_BINDER_DOES_NOT_MATCH;
Jerry Yu1c105562022-07-10 06:32:38 +0000387}
Jerry Yu96a2e362022-07-21 15:11:34 +0800388
Jerry Yu5725f1c2022-08-21 17:27:16 +0800389MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yuf35ba382022-08-23 17:58:26 +0800390static int ssl_tls13_select_ciphersuite_for_psk(
Gilles Peskine449bd832023-01-11 14:50:10 +0100391 mbedtls_ssl_context *ssl,
392 const unsigned char *cipher_suites,
393 const unsigned char *cipher_suites_end,
394 uint16_t *selected_ciphersuite,
395 const mbedtls_ssl_ciphersuite_t **selected_ciphersuite_info)
Jerry Yu5725f1c2022-08-21 17:27:16 +0800396{
Jerry Yuf35ba382022-08-23 17:58:26 +0800397 psa_algorithm_t psk_hash_alg = PSA_ALG_SHA_256;
Jerry Yu5725f1c2022-08-21 17:27:16 +0800398
Jerry Yu0baf9072022-08-25 11:21:04 +0800399 *selected_ciphersuite = 0;
400 *selected_ciphersuite_info = NULL;
401
Jerry Yuf35ba382022-08-23 17:58:26 +0800402 /* RFC 8446, page 55.
403 *
404 * For externally established PSKs, the Hash algorithm MUST be set when the
405 * PSK is established or default to SHA-256 if no such algorithm is defined.
406 *
407 */
Jerry Yu5725f1c2022-08-21 17:27:16 +0800408
Jerry Yu5725f1c2022-08-21 17:27:16 +0800409 /*
410 * Search for a matching ciphersuite
411 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100412 for (const unsigned char *p = cipher_suites;
413 p < cipher_suites_end; p += 2) {
Jerry Yu5725f1c2022-08-21 17:27:16 +0800414 uint16_t cipher_suite;
Jerry Yuf35ba382022-08-23 17:58:26 +0800415 const mbedtls_ssl_ciphersuite_t *ciphersuite_info;
Jerry Yu5725f1c2022-08-21 17:27:16 +0800416
Gilles Peskine449bd832023-01-11 14:50:10 +0100417 cipher_suite = MBEDTLS_GET_UINT16_BE(p, 0);
418 ciphersuite_info = ssl_tls13_validate_peer_ciphersuite(ssl,
419 cipher_suite);
420 if (ciphersuite_info == NULL) {
Jerry Yu5725f1c2022-08-21 17:27:16 +0800421 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +0100422 }
Jerry Yu5725f1c2022-08-21 17:27:16 +0800423
Jerry Yu5725f1c2022-08-21 17:27:16 +0800424 /* MAC of selected ciphersuite MUST be same with PSK binder if exist.
425 * Otherwise, client should reject.
426 */
Dave Rodgman2eab4622023-10-05 13:30:37 +0100427 if (psk_hash_alg ==
428 mbedtls_md_psa_alg_from_type((mbedtls_md_type_t) ciphersuite_info->mac)) {
Jerry Yuf35ba382022-08-23 17:58:26 +0800429 *selected_ciphersuite = cipher_suite;
430 *selected_ciphersuite_info = ciphersuite_info;
Gilles Peskine449bd832023-01-11 14:50:10 +0100431 return 0;
Jerry Yuf35ba382022-08-23 17:58:26 +0800432 }
433 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100434 MBEDTLS_SSL_DEBUG_MSG(2, ("No matched ciphersuite"));
435 return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
Jerry Yuf35ba382022-08-23 17:58:26 +0800436}
Jerry Yu5725f1c2022-08-21 17:27:16 +0800437
Jerry Yu82534862022-08-30 10:42:33 +0800438#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Jerry Yuf35ba382022-08-23 17:58:26 +0800439MBEDTLS_CHECK_RETURN_CRITICAL
440static int ssl_tls13_select_ciphersuite_for_resumption(
Gilles Peskine449bd832023-01-11 14:50:10 +0100441 mbedtls_ssl_context *ssl,
442 const unsigned char *cipher_suites,
443 const unsigned char *cipher_suites_end,
444 mbedtls_ssl_session *session,
445 uint16_t *selected_ciphersuite,
446 const mbedtls_ssl_ciphersuite_t **selected_ciphersuite_info)
Jerry Yuf35ba382022-08-23 17:58:26 +0800447{
Jerry Yu82534862022-08-30 10:42:33 +0800448
Jerry Yuf35ba382022-08-23 17:58:26 +0800449 *selected_ciphersuite = 0;
450 *selected_ciphersuite_info = NULL;
Gilles Peskine449bd832023-01-11 14:50:10 +0100451 for (const unsigned char *p = cipher_suites; p < cipher_suites_end; p += 2) {
452 uint16_t cipher_suite = MBEDTLS_GET_UINT16_BE(p, 0);
Jerry Yu82534862022-08-30 10:42:33 +0800453 const mbedtls_ssl_ciphersuite_t *ciphersuite_info;
454
Gilles Peskine449bd832023-01-11 14:50:10 +0100455 if (cipher_suite != session->ciphersuite) {
Jerry Yu82534862022-08-30 10:42:33 +0800456 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +0100457 }
Jerry Yu82534862022-08-30 10:42:33 +0800458
Gilles Peskine449bd832023-01-11 14:50:10 +0100459 ciphersuite_info = ssl_tls13_validate_peer_ciphersuite(ssl,
460 cipher_suite);
461 if (ciphersuite_info == NULL) {
Jerry Yu82534862022-08-30 10:42:33 +0800462 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +0100463 }
Jerry Yu82534862022-08-30 10:42:33 +0800464
Jerry Yu4746b102022-09-13 11:11:48 +0800465 *selected_ciphersuite = cipher_suite;
Jerry Yu82534862022-08-30 10:42:33 +0800466 *selected_ciphersuite_info = ciphersuite_info;
467
Gilles Peskine449bd832023-01-11 14:50:10 +0100468 return 0;
Jerry Yu82534862022-08-30 10:42:33 +0800469 }
470
Gilles Peskine449bd832023-01-11 14:50:10 +0100471 return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
Jerry Yu5725f1c2022-08-21 17:27:16 +0800472}
Jerry Yuf35ba382022-08-23 17:58:26 +0800473
Jerry Yu82534862022-08-30 10:42:33 +0800474MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100475static int ssl_tls13_session_copy_ticket(mbedtls_ssl_session *dst,
476 const mbedtls_ssl_session *src)
Jerry Yu82534862022-08-30 10:42:33 +0800477{
Jerry Yu82534862022-08-30 10:42:33 +0800478 dst->ticket_age_add = src->ticket_age_add;
479 dst->ticket_flags = src->ticket_flags;
480 dst->resumption_key_len = src->resumption_key_len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100481 if (src->resumption_key_len == 0) {
482 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
483 }
484 memcpy(dst->resumption_key, src->resumption_key, src->resumption_key_len);
Jerry Yu4746b102022-09-13 11:11:48 +0800485
Jerry Yu33bf2402022-12-12 16:01:43 +0800486#if defined(MBEDTLS_SSL_EARLY_DATA)
487 dst->max_early_data_size = src->max_early_data_size;
488#endif
489
Gilles Peskine449bd832023-01-11 14:50:10 +0100490 return 0;
Jerry Yu82534862022-08-30 10:42:33 +0800491}
492#endif /* MBEDTLS_SSL_SESSION_TICKETS */
493
Jerry Yu1c105562022-07-10 06:32:38 +0000494/* Parser for pre_shared_key extension in client hello
495 * struct {
496 * opaque identity<1..2^16-1>;
497 * uint32 obfuscated_ticket_age;
498 * } PskIdentity;
499 *
500 * opaque PskBinderEntry<32..255>;
501 *
502 * struct {
503 * PskIdentity identities<7..2^16-1>;
504 * PskBinderEntry binders<33..2^16-1>;
505 * } OfferedPsks;
506 *
507 * struct {
508 * select (Handshake.msg_type) {
509 * case client_hello: OfferedPsks;
510 * ....
511 * };
512 * } PreSharedKeyExtension;
513 */
Jerry Yu6e74a7e2022-07-20 20:49:32 +0800514MBEDTLS_CHECK_RETURN_CRITICAL
Xiaokang Qian9f1747b2023-03-30 02:50:04 +0000515static int ssl_tls13_parse_pre_shared_key_ext(
516 mbedtls_ssl_context *ssl,
517 const unsigned char *pre_shared_key_ext,
518 const unsigned char *pre_shared_key_ext_end,
519 const unsigned char *ciphersuites,
520 const unsigned char *ciphersuites_end)
Jerry Yu1c105562022-07-10 06:32:38 +0000521{
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +0100522 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yu29d9faa2022-08-23 17:52:45 +0800523 const unsigned char *identities = pre_shared_key_ext;
Jerry Yu568ec252022-07-22 21:27:34 +0800524 const unsigned char *p_identity_len;
525 size_t identities_len;
Jerry Yu1c105562022-07-10 06:32:38 +0000526 const unsigned char *identities_end;
Jerry Yu568ec252022-07-22 21:27:34 +0800527 const unsigned char *binders;
528 const unsigned char *p_binder_len;
529 size_t binders_len;
Jerry Yu1c105562022-07-10 06:32:38 +0000530 const unsigned char *binders_end;
Jerry Yu96a2e362022-07-21 15:11:34 +0800531 int matched_identity = -1;
532 int identity_id = -1;
Jerry Yu1c105562022-07-10 06:32:38 +0000533
Gilles Peskine449bd832023-01-11 14:50:10 +0100534 MBEDTLS_SSL_DEBUG_BUF(3, "pre_shared_key extension",
535 pre_shared_key_ext,
536 pre_shared_key_ext_end - pre_shared_key_ext);
Jerry Yu1c105562022-07-10 06:32:38 +0000537
Jerry Yu96a2e362022-07-21 15:11:34 +0800538 /* identities_len 2 bytes
539 * identities_data >= 7 bytes
540 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100541 MBEDTLS_SSL_CHK_BUF_READ_PTR(identities, pre_shared_key_ext_end, 7 + 2);
542 identities_len = MBEDTLS_GET_UINT16_BE(identities, 0);
Jerry Yu568ec252022-07-22 21:27:34 +0800543 p_identity_len = identities + 2;
Gilles Peskine449bd832023-01-11 14:50:10 +0100544 MBEDTLS_SSL_CHK_BUF_READ_PTR(p_identity_len, pre_shared_key_ext_end,
545 identities_len);
Jerry Yu568ec252022-07-22 21:27:34 +0800546 identities_end = p_identity_len + identities_len;
Jerry Yu1c105562022-07-10 06:32:38 +0000547
Jerry Yu96a2e362022-07-21 15:11:34 +0800548 /* binders_len 2 bytes
549 * binders >= 33 bytes
550 */
Jerry Yu568ec252022-07-22 21:27:34 +0800551 binders = identities_end;
Gilles Peskine449bd832023-01-11 14:50:10 +0100552 MBEDTLS_SSL_CHK_BUF_READ_PTR(binders, pre_shared_key_ext_end, 33 + 2);
553 binders_len = MBEDTLS_GET_UINT16_BE(binders, 0);
Jerry Yu568ec252022-07-22 21:27:34 +0800554 p_binder_len = binders + 2;
Gilles Peskine449bd832023-01-11 14:50:10 +0100555 MBEDTLS_SSL_CHK_BUF_READ_PTR(p_binder_len, pre_shared_key_ext_end, binders_len);
Jerry Yu568ec252022-07-22 21:27:34 +0800556 binders_end = p_binder_len + binders_len;
Jerry Yu1c105562022-07-10 06:32:38 +0000557
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +0100558 ret = ssl->handshake->update_checksum(ssl, pre_shared_key_ext,
559 identities_end - pre_shared_key_ext);
560 if (0 != ret) {
561 MBEDTLS_SSL_DEBUG_RET(1, ("update_checksum"), ret);
562 return ret;
563 }
Jerry Yu96a2e362022-07-21 15:11:34 +0800564
Gilles Peskine449bd832023-01-11 14:50:10 +0100565 while (p_identity_len < identities_end && p_binder_len < binders_end) {
Jerry Yu1c105562022-07-10 06:32:38 +0000566 const unsigned char *identity;
Jerry Yu568ec252022-07-22 21:27:34 +0800567 size_t identity_len;
Jerry Yu82534862022-08-30 10:42:33 +0800568 uint32_t obfuscated_ticket_age;
Jerry Yu96a2e362022-07-21 15:11:34 +0800569 const unsigned char *binder;
Jerry Yu568ec252022-07-22 21:27:34 +0800570 size_t binder_len;
Jerry Yu5725f1c2022-08-21 17:27:16 +0800571 int psk_type;
572 uint16_t cipher_suite;
Jerry Yu29d9faa2022-08-23 17:52:45 +0800573 const mbedtls_ssl_ciphersuite_t *ciphersuite_info;
Jerry Yu82534862022-08-30 10:42:33 +0800574#if defined(MBEDTLS_SSL_SESSION_TICKETS)
575 mbedtls_ssl_session session;
Gilles Peskine449bd832023-01-11 14:50:10 +0100576 mbedtls_ssl_session_init(&session);
Jerry Yu82534862022-08-30 10:42:33 +0800577#endif
Jerry Yubb852022022-07-20 21:10:44 +0800578
Gilles Peskine449bd832023-01-11 14:50:10 +0100579 MBEDTLS_SSL_CHK_BUF_READ_PTR(p_identity_len, identities_end, 2 + 1 + 4);
580 identity_len = MBEDTLS_GET_UINT16_BE(p_identity_len, 0);
Jerry Yu568ec252022-07-22 21:27:34 +0800581 identity = p_identity_len + 2;
Gilles Peskine449bd832023-01-11 14:50:10 +0100582 MBEDTLS_SSL_CHK_BUF_READ_PTR(identity, identities_end, identity_len + 4);
583 obfuscated_ticket_age = MBEDTLS_GET_UINT32_BE(identity, identity_len);
Jerry Yu568ec252022-07-22 21:27:34 +0800584 p_identity_len += identity_len + 6;
Jerry Yu1c105562022-07-10 06:32:38 +0000585
Gilles Peskine449bd832023-01-11 14:50:10 +0100586 MBEDTLS_SSL_CHK_BUF_READ_PTR(p_binder_len, binders_end, 1 + 32);
Jerry Yu568ec252022-07-22 21:27:34 +0800587 binder_len = *p_binder_len;
588 binder = p_binder_len + 1;
Gilles Peskine449bd832023-01-11 14:50:10 +0100589 MBEDTLS_SSL_CHK_BUF_READ_PTR(binder, binders_end, binder_len);
Jerry Yu568ec252022-07-22 21:27:34 +0800590 p_binder_len += binder_len + 1;
Jerry Yu96a2e362022-07-21 15:11:34 +0800591
Jerry Yu96a2e362022-07-21 15:11:34 +0800592 identity_id++;
Gilles Peskine449bd832023-01-11 14:50:10 +0100593 if (matched_identity != -1) {
Jerry Yu1c105562022-07-10 06:32:38 +0000594 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +0100595 }
Jerry Yu1c105562022-07-10 06:32:38 +0000596
Jerry Yu96a2e362022-07-21 15:11:34 +0800597 ret = ssl_tls13_offered_psks_check_identity_match(
Gilles Peskine449bd832023-01-11 14:50:10 +0100598 ssl, identity, identity_len, obfuscated_ticket_age,
599 &psk_type, &session);
Ronald Cronfbae94a2023-12-05 18:15:14 +0100600 if (ret != SSL_TLS1_3_PSK_IDENTITY_MATCH) {
Jerry Yu96a2e362022-07-21 15:11:34 +0800601 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +0100602 }
Jerry Yu96a2e362022-07-21 15:11:34 +0800603
Gilles Peskine449bd832023-01-11 14:50:10 +0100604 MBEDTLS_SSL_DEBUG_MSG(4, ("found matched identity"));
605 switch (psk_type) {
Jerry Yu0baf9072022-08-25 11:21:04 +0800606 case MBEDTLS_SSL_TLS1_3_PSK_EXTERNAL:
607 ret = ssl_tls13_select_ciphersuite_for_psk(
Gilles Peskine449bd832023-01-11 14:50:10 +0100608 ssl, ciphersuites, ciphersuites_end,
609 &cipher_suite, &ciphersuite_info);
Jerry Yu0baf9072022-08-25 11:21:04 +0800610 break;
611 case MBEDTLS_SSL_TLS1_3_PSK_RESUMPTION:
Jerry Yu82534862022-08-30 10:42:33 +0800612#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Jerry Yu0baf9072022-08-25 11:21:04 +0800613 ret = ssl_tls13_select_ciphersuite_for_resumption(
Gilles Peskine449bd832023-01-11 14:50:10 +0100614 ssl, ciphersuites, ciphersuites_end, &session,
615 &cipher_suite, &ciphersuite_info);
616 if (ret != 0) {
617 mbedtls_ssl_session_free(&session);
618 }
Jerry Yu82534862022-08-30 10:42:33 +0800619#else
620 ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
621#endif
Jerry Yu0baf9072022-08-25 11:21:04 +0800622 break;
623 default:
Gilles Peskine449bd832023-01-11 14:50:10 +0100624 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Jerry Yu0baf9072022-08-25 11:21:04 +0800625 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100626 if (ret != 0) {
Jerry Yuf35ba382022-08-23 17:58:26 +0800627 /* See below, no cipher_suite available, abort handshake */
628 MBEDTLS_SSL_PEND_FATAL_ALERT(
629 MBEDTLS_SSL_ALERT_MSG_DECRYPT_ERROR,
Gilles Peskine449bd832023-01-11 14:50:10 +0100630 MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE);
Jerry Yuf35ba382022-08-23 17:58:26 +0800631 MBEDTLS_SSL_DEBUG_RET(
Gilles Peskine449bd832023-01-11 14:50:10 +0100632 2, "ssl_tls13_select_ciphersuite", ret);
633 return ret;
Jerry Yu5725f1c2022-08-21 17:27:16 +0800634 }
635
Jerry Yu96a2e362022-07-21 15:11:34 +0800636 ret = ssl_tls13_offered_psks_check_binder_match(
Gilles Peskine449bd832023-01-11 14:50:10 +0100637 ssl, binder, binder_len, psk_type,
Dave Rodgman2eab4622023-10-05 13:30:37 +0100638 mbedtls_md_psa_alg_from_type((mbedtls_md_type_t) ciphersuite_info->mac));
Ronald Cron6e311272023-12-05 17:57:01 +0100639 if (ret != SSL_TLS1_3_BINDER_MATCH) {
Jerry Yuc5a23a02022-08-25 10:51:44 +0800640 /* For security reasons, the handshake should be aborted when we
641 * fail to validate a binder value. See RFC 8446 section 4.2.11.2
642 * and appendix E.6. */
Jerry Yu82534862022-08-30 10:42:33 +0800643#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Gilles Peskine449bd832023-01-11 14:50:10 +0100644 mbedtls_ssl_session_free(&session);
Jerry Yu82534862022-08-30 10:42:33 +0800645#endif
Gilles Peskine449bd832023-01-11 14:50:10 +0100646 MBEDTLS_SSL_DEBUG_MSG(3, ("Invalid binder."));
Xiaokang Qian9f1747b2023-03-30 02:50:04 +0000647 MBEDTLS_SSL_DEBUG_RET(
648 1, "ssl_tls13_offered_psks_check_binder_match", ret);
Jerry Yu96a2e362022-07-21 15:11:34 +0800649 MBEDTLS_SSL_PEND_FATAL_ALERT(
650 MBEDTLS_SSL_ALERT_MSG_DECRYPT_ERROR,
Gilles Peskine449bd832023-01-11 14:50:10 +0100651 MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE);
652 return ret;
Jerry Yu96a2e362022-07-21 15:11:34 +0800653 }
Jerry Yu96a2e362022-07-21 15:11:34 +0800654
655 matched_identity = identity_id;
Jerry Yu5725f1c2022-08-21 17:27:16 +0800656
657 /* Update handshake parameters */
Jerry Yue5834fd2022-08-29 20:16:09 +0800658 ssl->handshake->ciphersuite_info = ciphersuite_info;
Jerry Yu4746b102022-09-13 11:11:48 +0800659 ssl->session_negotiate->ciphersuite = cipher_suite;
Gilles Peskine449bd832023-01-11 14:50:10 +0100660 MBEDTLS_SSL_DEBUG_MSG(2, ("overwrite ciphersuite: %04x - %s",
661 cipher_suite, ciphersuite_info->name));
Jerry Yu82534862022-08-30 10:42:33 +0800662#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Gilles Peskine449bd832023-01-11 14:50:10 +0100663 if (psk_type == MBEDTLS_SSL_TLS1_3_PSK_RESUMPTION) {
664 ret = ssl_tls13_session_copy_ticket(ssl->session_negotiate,
665 &session);
666 mbedtls_ssl_session_free(&session);
667 if (ret != 0) {
668 return ret;
669 }
Jerry Yu82534862022-08-30 10:42:33 +0800670 }
671#endif /* MBEDTLS_SSL_SESSION_TICKETS */
Jerry Yu1c105562022-07-10 06:32:38 +0000672 }
673
Gilles Peskine449bd832023-01-11 14:50:10 +0100674 if (p_identity_len != identities_end || p_binder_len != binders_end) {
675 MBEDTLS_SSL_DEBUG_MSG(3, ("pre_shared_key extension decode error"));
676 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR,
677 MBEDTLS_ERR_SSL_DECODE_ERROR);
678 return MBEDTLS_ERR_SSL_DECODE_ERROR;
Jerry Yu1c105562022-07-10 06:32:38 +0000679 }
680
Jerry Yu6f1db3f2022-07-22 23:05:59 +0800681 /* Update the handshake transcript with the binder list. */
Xiaokang Qian9f1747b2023-03-30 02:50:04 +0000682 ret = ssl->handshake->update_checksum(
683 ssl, identities_end, (size_t) (binders_end - identities_end));
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +0100684 if (0 != ret) {
685 MBEDTLS_SSL_DEBUG_RET(1, ("update_checksum"), ret);
686 return ret;
687 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100688 if (matched_identity == -1) {
689 MBEDTLS_SSL_DEBUG_MSG(3, ("No matched PSK or ticket."));
690 return MBEDTLS_ERR_SSL_UNKNOWN_IDENTITY;
Jerry Yu1c105562022-07-10 06:32:38 +0000691 }
692
Gilles Peskine449bd832023-01-11 14:50:10 +0100693 ssl->handshake->selected_identity = (uint16_t) matched_identity;
694 MBEDTLS_SSL_DEBUG_MSG(3, ("Pre shared key found"));
Jerry Yu1c105562022-07-10 06:32:38 +0000695
Gilles Peskine449bd832023-01-11 14:50:10 +0100696 return 0;
Jerry Yu1c105562022-07-10 06:32:38 +0000697}
Jerry Yu032b15ce2022-07-11 06:10:03 +0000698
699/*
700 * struct {
701 * select ( Handshake.msg_type ) {
702 * ....
703 * case server_hello:
704 * uint16 selected_identity;
705 * }
706 * } PreSharedKeyExtension;
707 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100708static int ssl_tls13_write_server_pre_shared_key_ext(mbedtls_ssl_context *ssl,
709 unsigned char *buf,
710 unsigned char *end,
711 size_t *olen)
Jerry Yu032b15ce2022-07-11 06:10:03 +0000712{
Gilles Peskine449bd832023-01-11 14:50:10 +0100713 unsigned char *p = (unsigned char *) buf;
Jerry Yu032b15ce2022-07-11 06:10:03 +0000714
715 *olen = 0;
716
David Horstmann21b89762022-10-06 18:34:28 +0100717 int not_using_psk = 0;
Jerry Yu032b15ce2022-07-11 06:10:03 +0000718#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +0100719 not_using_psk = (mbedtls_svc_key_id_is_null(ssl->handshake->psk_opaque));
Jerry Yu032b15ce2022-07-11 06:10:03 +0000720#else
Gilles Peskine449bd832023-01-11 14:50:10 +0100721 not_using_psk = (ssl->handshake->psk == NULL);
Jerry Yu032b15ce2022-07-11 06:10:03 +0000722#endif
Gilles Peskine449bd832023-01-11 14:50:10 +0100723 if (not_using_psk) {
Jerry Yu032b15ce2022-07-11 06:10:03 +0000724 /* We shouldn't have called this extension writer unless we've
725 * chosen to use a PSK. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100726 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Jerry Yu032b15ce2022-07-11 06:10:03 +0000727 }
728
Gilles Peskine449bd832023-01-11 14:50:10 +0100729 MBEDTLS_SSL_DEBUG_MSG(3, ("server hello, adding pre_shared_key extension"));
730 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 6);
Jerry Yu032b15ce2022-07-11 06:10:03 +0000731
Gilles Peskine449bd832023-01-11 14:50:10 +0100732 MBEDTLS_PUT_UINT16_BE(MBEDTLS_TLS_EXT_PRE_SHARED_KEY, p, 0);
733 MBEDTLS_PUT_UINT16_BE(2, p, 2);
Jerry Yu032b15ce2022-07-11 06:10:03 +0000734
Gilles Peskine449bd832023-01-11 14:50:10 +0100735 MBEDTLS_PUT_UINT16_BE(ssl->handshake->selected_identity, p, 4);
Jerry Yu032b15ce2022-07-11 06:10:03 +0000736
737 *olen = 6;
738
Gilles Peskine449bd832023-01-11 14:50:10 +0100739 MBEDTLS_SSL_DEBUG_MSG(4, ("sent selected_identity: %u",
740 ssl->handshake->selected_identity));
Jerry Yu032b15ce2022-07-11 06:10:03 +0000741
Gilles Peskine449bd832023-01-11 14:50:10 +0100742 mbedtls_ssl_tls13_set_hs_sent_ext_mask(ssl, MBEDTLS_TLS_EXT_PRE_SHARED_KEY);
Jerry Yub95dd362022-11-08 21:19:34 +0800743
Gilles Peskine449bd832023-01-11 14:50:10 +0100744 return 0;
Jerry Yu032b15ce2022-07-11 06:10:03 +0000745}
746
Ronald Cron41a443a2022-10-04 16:38:25 +0200747#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED */
Jerry Yue19e3b92022-07-08 12:04:51 +0000748
XiaokangQian7807f9f2022-02-15 10:04:37 +0000749/* From RFC 8446:
750 * struct {
XiaokangQiancfd925f2022-04-14 07:10:37 +0000751 * ProtocolVersion versions<2..254>;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000752 * } SupportedVersions;
753 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200754MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100755static int ssl_tls13_parse_supported_versions_ext(mbedtls_ssl_context *ssl,
756 const unsigned char *buf,
757 const unsigned char *end)
XiaokangQian7807f9f2022-02-15 10:04:37 +0000758{
XiaokangQian7807f9f2022-02-15 10:04:37 +0000759 const unsigned char *p = buf;
XiaokangQian8f9dfe42022-04-15 02:52:39 +0000760 size_t versions_len;
XiaokangQian4080a7f2022-04-11 09:55:18 +0000761 const unsigned char *versions_end;
XiaokangQian0a1b54e2022-04-21 03:01:38 +0000762 uint16_t tls_version;
Ronald Cron5af4c7f2023-03-07 20:46:59 +0100763 int found_supported_version = 0;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000764
Gilles Peskine449bd832023-01-11 14:50:10 +0100765 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, 1);
XiaokangQian4080a7f2022-04-11 09:55:18 +0000766 versions_len = p[0];
XiaokangQian7807f9f2022-02-15 10:04:37 +0000767 p += 1;
768
Gilles Peskine449bd832023-01-11 14:50:10 +0100769 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, versions_len);
XiaokangQian4080a7f2022-04-11 09:55:18 +0000770 versions_end = p + versions_len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100771 while (p < versions_end) {
772 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, versions_end, 2);
773 tls_version = mbedtls_ssl_read_version(p, ssl->conf->transport);
XiaokangQianb67384d2022-04-19 00:02:38 +0000774 p += 2;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000775
Ronald Cron3bd2b022023-04-03 16:45:39 +0200776 if (MBEDTLS_SSL_VERSION_TLS1_3 == tls_version) {
Ronald Cron5af4c7f2023-03-07 20:46:59 +0100777 found_supported_version = 1;
778 break;
779 }
780
Ronald Cron3bd2b022023-04-03 16:45:39 +0200781 if ((MBEDTLS_SSL_VERSION_TLS1_2 == tls_version) &&
782 mbedtls_ssl_conf_is_tls12_enabled(ssl->conf)) {
Ronald Cron5af4c7f2023-03-07 20:46:59 +0100783 found_supported_version = 1;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000784 break;
785 }
XiaokangQian7807f9f2022-02-15 10:04:37 +0000786 }
787
Ronald Cron5af4c7f2023-03-07 20:46:59 +0100788 if (!found_supported_version) {
789 MBEDTLS_SSL_DEBUG_MSG(1, ("No supported version found."));
XiaokangQian7807f9f2022-02-15 10:04:37 +0000790
Gilles Peskine449bd832023-01-11 14:50:10 +0100791 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_PROTOCOL_VERSION,
792 MBEDTLS_ERR_SSL_BAD_PROTOCOL_VERSION);
793 return MBEDTLS_ERR_SSL_BAD_PROTOCOL_VERSION;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000794 }
795
Ronald Cron5af4c7f2023-03-07 20:46:59 +0100796 MBEDTLS_SSL_DEBUG_MSG(1, ("Negotiated version: [%04x]",
Gilles Peskine449bd832023-01-11 14:50:10 +0100797 (unsigned int) tls_version));
XiaokangQian7807f9f2022-02-15 10:04:37 +0000798
Ronald Cron5af4c7f2023-03-07 20:46:59 +0100799 return (int) tls_version;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000800}
801
Przemek Stekiel8c0a9532023-06-15 16:48:19 +0200802#if defined(PSA_WANT_ALG_ECDH) || defined(PSA_WANT_ALG_FFDH)
XiaokangQiane8ff3502022-04-22 02:34:40 +0000803/*
XiaokangQian7807f9f2022-02-15 10:04:37 +0000804 *
805 * From RFC 8446:
806 * enum {
807 * ... (0xFFFF)
808 * } NamedGroup;
809 * struct {
810 * NamedGroup named_group_list<2..2^16-1>;
811 * } NamedGroupList;
812 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200813MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100814static int ssl_tls13_parse_supported_groups_ext(mbedtls_ssl_context *ssl,
815 const unsigned char *buf,
816 const unsigned char *end)
XiaokangQian7807f9f2022-02-15 10:04:37 +0000817{
XiaokangQian7807f9f2022-02-15 10:04:37 +0000818 const unsigned char *p = buf;
XiaokangQian84823772022-04-19 07:57:30 +0000819 size_t named_group_list_len;
XiaokangQian8f9dfe42022-04-15 02:52:39 +0000820 const unsigned char *named_group_list_end;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000821
Gilles Peskine449bd832023-01-11 14:50:10 +0100822 MBEDTLS_SSL_DEBUG_BUF(3, "supported_groups extension", p, end - buf);
823 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, 2);
824 named_group_list_len = MBEDTLS_GET_UINT16_BE(p, 0);
XiaokangQian7807f9f2022-02-15 10:04:37 +0000825 p += 2;
Gilles Peskine449bd832023-01-11 14:50:10 +0100826 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, named_group_list_len);
XiaokangQian8f9dfe42022-04-15 02:52:39 +0000827 named_group_list_end = p + named_group_list_len;
XiaokangQian4e8cd7b2022-04-21 09:48:09 +0000828 ssl->handshake->hrr_selected_group = 0;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000829
Gilles Peskine449bd832023-01-11 14:50:10 +0100830 while (p < named_group_list_end) {
XiaokangQian08037552022-04-20 07:16:41 +0000831 uint16_t named_group;
Gilles Peskine449bd832023-01-11 14:50:10 +0100832 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, named_group_list_end, 2);
833 named_group = MBEDTLS_GET_UINT16_BE(p, 0);
XiaokangQian08037552022-04-20 07:16:41 +0000834 p += 2;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000835
Gilles Peskine449bd832023-01-11 14:50:10 +0100836 MBEDTLS_SSL_DEBUG_MSG(2,
837 ("got named group: %s(%04x)",
838 mbedtls_ssl_named_group_to_str(named_group),
839 named_group));
XiaokangQian08037552022-04-20 07:16:41 +0000840
Gilles Peskine449bd832023-01-11 14:50:10 +0100841 if (!mbedtls_ssl_named_group_is_offered(ssl, named_group) ||
842 !mbedtls_ssl_named_group_is_supported(named_group) ||
843 ssl->handshake->hrr_selected_group != 0) {
XiaokangQian08037552022-04-20 07:16:41 +0000844 continue;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000845 }
846
Gilles Peskine449bd832023-01-11 14:50:10 +0100847 MBEDTLS_SSL_DEBUG_MSG(2,
848 ("add named group %s(%04x) into received list.",
849 mbedtls_ssl_named_group_to_str(named_group),
850 named_group));
Jerry Yuc1be19f2022-04-23 16:11:39 +0800851
XiaokangQian4e8cd7b2022-04-21 09:48:09 +0000852 ssl->handshake->hrr_selected_group = named_group;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000853 }
854
Gilles Peskine449bd832023-01-11 14:50:10 +0100855 return 0;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000856
857}
Przemek Stekiel8c0a9532023-06-15 16:48:19 +0200858#endif /* PSA_WANT_ALG_ECDH || PSA_WANT_ALG_FFDH */
XiaokangQian7807f9f2022-02-15 10:04:37 +0000859
XiaokangQian08037552022-04-20 07:16:41 +0000860#define SSL_TLS1_3_PARSE_KEY_SHARES_EXT_NO_MATCH 1
861
Valerio Settic9ae8622023-07-25 11:23:50 +0200862#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_EPHEMERAL_ENABLED)
XiaokangQian7807f9f2022-02-15 10:04:37 +0000863/*
864 * ssl_tls13_parse_key_shares_ext() verifies whether the information in the
Xiaokang Qian9f1747b2023-03-30 02:50:04 +0000865 * extension is correct and stores the first acceptable key share and its
866 * associated group.
XiaokangQian7807f9f2022-02-15 10:04:37 +0000867 *
868 * Possible return values are:
869 * - 0: Successful processing of the client provided key share extension.
Xiaokang Qian9f1747b2023-03-30 02:50:04 +0000870 * - SSL_TLS1_3_PARSE_KEY_SHARES_EXT_NO_MATCH: The key shares provided by
871 * the client does not match a group supported by the server. A
872 * HelloRetryRequest will be needed.
XiaokangQiane8ff3502022-04-22 02:34:40 +0000873 * - A negative value for fatal errors.
Jerry Yuc1be19f2022-04-23 16:11:39 +0800874 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200875MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100876static int ssl_tls13_parse_key_shares_ext(mbedtls_ssl_context *ssl,
877 const unsigned char *buf,
878 const unsigned char *end)
XiaokangQian7807f9f2022-02-15 10:04:37 +0000879{
XiaokangQianb67384d2022-04-19 00:02:38 +0000880 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000881 unsigned char const *p = buf;
XiaokangQian8f9dfe42022-04-15 02:52:39 +0000882 unsigned char const *client_shares_end;
Jerry Yuc1be19f2022-04-23 16:11:39 +0800883 size_t client_shares_len;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000884
885 /* From RFC 8446:
886 *
887 * struct {
888 * KeyShareEntry client_shares<0..2^16-1>;
889 * } KeyShareClientHello;
890 *
891 */
892
Gilles Peskine449bd832023-01-11 14:50:10 +0100893 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, 2);
894 client_shares_len = MBEDTLS_GET_UINT16_BE(p, 0);
XiaokangQian7807f9f2022-02-15 10:04:37 +0000895 p += 2;
Gilles Peskine449bd832023-01-11 14:50:10 +0100896 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, client_shares_len);
XiaokangQian7807f9f2022-02-15 10:04:37 +0000897
898 ssl->handshake->offered_group_id = 0;
XiaokangQian8f9dfe42022-04-15 02:52:39 +0000899 client_shares_end = p + client_shares_len;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000900
901 /* We try to find a suitable key share entry and copy it to the
902 * handshake context. Later, we have to find out whether we can do
903 * something with the provided key share or whether we have to
XiaokangQianc5763b52022-04-02 03:34:37 +0000904 * dismiss it and send a HelloRetryRequest message.
905 */
XiaokangQian7807f9f2022-02-15 10:04:37 +0000906
Gilles Peskine449bd832023-01-11 14:50:10 +0100907 while (p < client_shares_end) {
XiaokangQian9b5d04b2022-04-10 10:20:43 +0000908 uint16_t group;
Jerry Yuc1be19f2022-04-23 16:11:39 +0800909 size_t key_exchange_len;
Jerry Yu086edc22022-05-05 10:50:38 +0800910 const unsigned char *key_exchange;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000911
912 /*
913 * struct {
914 * NamedGroup group;
915 * opaque key_exchange<1..2^16-1>;
916 * } KeyShareEntry;
917 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100918 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, client_shares_end, 4);
919 group = MBEDTLS_GET_UINT16_BE(p, 0);
920 key_exchange_len = MBEDTLS_GET_UINT16_BE(p, 2);
Jerry Yuc1be19f2022-04-23 16:11:39 +0800921 p += 4;
Jerry Yu086edc22022-05-05 10:50:38 +0800922 key_exchange = p;
Gilles Peskine449bd832023-01-11 14:50:10 +0100923 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, client_shares_end, key_exchange_len);
Jerry Yu086edc22022-05-05 10:50:38 +0800924 p += key_exchange_len;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000925
926 /* Continue parsing even if we have already found a match,
XiaokangQianc5763b52022-04-02 03:34:37 +0000927 * for input validation purposes.
928 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100929 if (!mbedtls_ssl_named_group_is_offered(ssl, group) ||
930 !mbedtls_ssl_named_group_is_supported(group) ||
931 ssl->handshake->offered_group_id != 0) {
XiaokangQian060d8672022-04-21 09:24:56 +0000932 continue;
933 }
XiaokangQian060d8672022-04-21 09:24:56 +0000934
XiaokangQian7807f9f2022-02-15 10:04:37 +0000935 /*
Przemek Stekielc89f3ea2023-05-18 15:45:53 +0200936 * ECDHE and FFDHE groups are supported
XiaokangQian7807f9f2022-02-15 10:04:37 +0000937 */
Przemek Stekielc89f3ea2023-05-18 15:45:53 +0200938 if (mbedtls_ssl_tls13_named_group_is_ecdhe(group) ||
Przemek Stekield5f79e72023-06-29 09:08:43 +0200939 mbedtls_ssl_tls13_named_group_is_ffdh(group)) {
Przemek Stekielc89f3ea2023-05-18 15:45:53 +0200940 MBEDTLS_SSL_DEBUG_MSG(2, ("ECDH/FFDH group: %s (%04x)",
Gilles Peskine449bd832023-01-11 14:50:10 +0100941 mbedtls_ssl_named_group_to_str(group),
942 group));
Przemek Stekiel7ac93be2023-07-04 10:02:38 +0200943 ret = mbedtls_ssl_tls13_read_public_xxdhe_share(
Gilles Peskine449bd832023-01-11 14:50:10 +0100944 ssl, key_exchange - 2, key_exchange_len + 2);
945 if (ret != 0) {
946 return ret;
947 }
XiaokangQian4e8cd7b2022-04-21 09:48:09 +0000948
Gilles Peskine449bd832023-01-11 14:50:10 +0100949 } else {
950 MBEDTLS_SSL_DEBUG_MSG(4, ("Unrecognized NamedGroup %u",
951 (unsigned) group));
XiaokangQian7807f9f2022-02-15 10:04:37 +0000952 continue;
953 }
954
XiaokangQian9b5d04b2022-04-10 10:20:43 +0000955 ssl->handshake->offered_group_id = group;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000956 }
957
Jerry Yuc1be19f2022-04-23 16:11:39 +0800958
Gilles Peskine449bd832023-01-11 14:50:10 +0100959 if (ssl->handshake->offered_group_id == 0) {
960 MBEDTLS_SSL_DEBUG_MSG(1, ("no matching key share"));
961 return SSL_TLS1_3_PARSE_KEY_SHARES_EXT_NO_MATCH;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000962 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100963 return 0;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000964}
Valerio Settic9ae8622023-07-25 11:23:50 +0200965#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_EPHEMERAL_ENABLED */
XiaokangQian7807f9f2022-02-15 10:04:37 +0000966
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200967MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100968static int ssl_tls13_client_hello_has_exts(mbedtls_ssl_context *ssl,
969 int exts_mask)
XiaokangQian7807f9f2022-02-15 10:04:37 +0000970{
Jerry Yu0c354a22022-08-29 15:25:36 +0800971 int masked = ssl->handshake->received_extensions & exts_mask;
Gilles Peskine449bd832023-01-11 14:50:10 +0100972 return masked == exts_mask;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000973}
974
Jerry Yue5991322022-11-07 14:03:44 +0800975#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200976MBEDTLS_CHECK_RETURN_CRITICAL
XiaokangQianb67384d2022-04-19 00:02:38 +0000977static int ssl_tls13_client_hello_has_exts_for_ephemeral_key_exchange(
Gilles Peskine449bd832023-01-11 14:50:10 +0100978 mbedtls_ssl_context *ssl)
XiaokangQian7807f9f2022-02-15 10:04:37 +0000979{
Gilles Peskine449bd832023-01-11 14:50:10 +0100980 return ssl_tls13_client_hello_has_exts(
981 ssl,
982 MBEDTLS_SSL_EXT_MASK(SUPPORTED_GROUPS) |
983 MBEDTLS_SSL_EXT_MASK(KEY_SHARE) |
984 MBEDTLS_SSL_EXT_MASK(SIG_ALG));
XiaokangQian7807f9f2022-02-15 10:04:37 +0000985}
Jerry Yue5991322022-11-07 14:03:44 +0800986#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED */
XiaokangQian7807f9f2022-02-15 10:04:37 +0000987
Jerry Yue5991322022-11-07 14:03:44 +0800988#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK_ENABLED)
Jerry Yu77f01482022-07-11 07:03:24 +0000989MBEDTLS_CHECK_RETURN_CRITICAL
990static int ssl_tls13_client_hello_has_exts_for_psk_key_exchange(
Gilles Peskine449bd832023-01-11 14:50:10 +0100991 mbedtls_ssl_context *ssl)
Jerry Yu77f01482022-07-11 07:03:24 +0000992{
Gilles Peskine449bd832023-01-11 14:50:10 +0100993 return ssl_tls13_client_hello_has_exts(
994 ssl,
995 MBEDTLS_SSL_EXT_MASK(PRE_SHARED_KEY) |
996 MBEDTLS_SSL_EXT_MASK(PSK_KEY_EXCHANGE_MODES));
Jerry Yu77f01482022-07-11 07:03:24 +0000997}
Jerry Yue5991322022-11-07 14:03:44 +0800998#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK_ENABLED */
Jerry Yu77f01482022-07-11 07:03:24 +0000999
Jerry Yue5991322022-11-07 14:03:44 +08001000#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK_EPHEMERAL_ENABLED)
Jerry Yu77f01482022-07-11 07:03:24 +00001001MBEDTLS_CHECK_RETURN_CRITICAL
1002static int ssl_tls13_client_hello_has_exts_for_psk_ephemeral_key_exchange(
Gilles Peskine449bd832023-01-11 14:50:10 +01001003 mbedtls_ssl_context *ssl)
Jerry Yu77f01482022-07-11 07:03:24 +00001004{
Gilles Peskine449bd832023-01-11 14:50:10 +01001005 return ssl_tls13_client_hello_has_exts(
1006 ssl,
1007 MBEDTLS_SSL_EXT_MASK(SUPPORTED_GROUPS) |
1008 MBEDTLS_SSL_EXT_MASK(KEY_SHARE) |
1009 MBEDTLS_SSL_EXT_MASK(PRE_SHARED_KEY) |
1010 MBEDTLS_SSL_EXT_MASK(PSK_KEY_EXCHANGE_MODES));
Jerry Yu77f01482022-07-11 07:03:24 +00001011}
Jerry Yue5991322022-11-07 14:03:44 +08001012#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK_EPHEMERAL_ENABLED */
Jerry Yu77f01482022-07-11 07:03:24 +00001013
Pengyu Lv306a01d2023-02-02 16:35:47 +08001014#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED)
1015MBEDTLS_CHECK_RETURN_CRITICAL
Pengyu Lvd72e8582023-11-10 10:37:18 +08001016static int ssl_tls13_ticket_is_kex_mode_permitted(mbedtls_ssl_context *ssl,
1017 unsigned int kex_mode)
Pengyu Lv306a01d2023-02-02 16:35:47 +08001018{
1019#if defined(MBEDTLS_SSL_SESSION_TICKETS)
1020 if (ssl->handshake->resume) {
Pengyu Lv94a42cc2023-12-06 10:04:17 +08001021 if (!mbedtls_ssl_tls13_session_ticket_has_flags(
Pengyu Lv306a01d2023-02-02 16:35:47 +08001022 ssl->session_negotiate, kex_mode)) {
1023 return 0;
1024 }
1025 }
1026#else
1027 ((void) ssl);
1028 ((void) kex_mode);
1029#endif
1030 return 1;
1031}
1032#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED */
1033
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001034MBEDTLS_CHECK_RETURN_CRITICAL
Pengyu Lvbc4aab72023-12-01 15:37:24 +08001035static int ssl_tls13_key_exchange_is_ephemeral_available(mbedtls_ssl_context *ssl)
XiaokangQian7807f9f2022-02-15 10:04:37 +00001036{
Jerry Yue5991322022-11-07 14:03:44 +08001037#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED)
Pengyu Lv0a1ff2b2023-11-14 11:03:32 +08001038 return mbedtls_ssl_conf_tls13_is_ephemeral_enabled(ssl) &&
Gilles Peskine449bd832023-01-11 14:50:10 +01001039 ssl_tls13_client_hello_has_exts_for_ephemeral_key_exchange(ssl);
Jerry Yue5991322022-11-07 14:03:44 +08001040#else
1041 ((void) ssl);
Gilles Peskine449bd832023-01-11 14:50:10 +01001042 return 0;
Jerry Yue5991322022-11-07 14:03:44 +08001043#endif
Jerry Yu77f01482022-07-11 07:03:24 +00001044}
XiaokangQian7807f9f2022-02-15 10:04:37 +00001045
Jerry Yu77f01482022-07-11 07:03:24 +00001046MBEDTLS_CHECK_RETURN_CRITICAL
Pengyu Lvbc4aab72023-12-01 15:37:24 +08001047static int ssl_tls13_key_exchange_is_psk_available(mbedtls_ssl_context *ssl)
Jerry Yu77f01482022-07-11 07:03:24 +00001048{
Jerry Yue5991322022-11-07 14:03:44 +08001049#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK_ENABLED)
Pengyu Lvd72e8582023-11-10 10:37:18 +08001050 return ssl_tls13_ticket_is_kex_mode_permitted(
Pengyu Lv306a01d2023-02-02 16:35:47 +08001051 ssl, MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK) &&
Pengyu Lv0a1ff2b2023-11-14 11:03:32 +08001052 mbedtls_ssl_conf_tls13_is_psk_enabled(ssl) &&
Pengyu Lvb2cfafb2023-11-14 13:56:13 +08001053 mbedtls_ssl_tls13_is_psk_supported(ssl) &&
Gilles Peskine449bd832023-01-11 14:50:10 +01001054 ssl_tls13_client_hello_has_exts_for_psk_key_exchange(ssl);
Jerry Yu77f01482022-07-11 07:03:24 +00001055#else
1056 ((void) ssl);
Gilles Peskine449bd832023-01-11 14:50:10 +01001057 return 0;
Jerry Yu77f01482022-07-11 07:03:24 +00001058#endif
1059}
XiaokangQian7807f9f2022-02-15 10:04:37 +00001060
Jerry Yu77f01482022-07-11 07:03:24 +00001061MBEDTLS_CHECK_RETURN_CRITICAL
Pengyu Lvbc4aab72023-12-01 15:37:24 +08001062static int ssl_tls13_key_exchange_is_psk_ephemeral_available(mbedtls_ssl_context *ssl)
Jerry Yu77f01482022-07-11 07:03:24 +00001063{
Jerry Yue5991322022-11-07 14:03:44 +08001064#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK_EPHEMERAL_ENABLED)
Pengyu Lvd72e8582023-11-10 10:37:18 +08001065 return ssl_tls13_ticket_is_kex_mode_permitted(
Pengyu Lv306a01d2023-02-02 16:35:47 +08001066 ssl, MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK_EPHEMERAL) &&
Pengyu Lv0a1ff2b2023-11-14 11:03:32 +08001067 mbedtls_ssl_conf_tls13_is_psk_ephemeral_enabled(ssl) &&
Pengyu Lvb2cfafb2023-11-14 13:56:13 +08001068 mbedtls_ssl_tls13_is_psk_ephemeral_supported(ssl) &&
Gilles Peskine449bd832023-01-11 14:50:10 +01001069 ssl_tls13_client_hello_has_exts_for_psk_ephemeral_key_exchange(ssl);
Jerry Yu77f01482022-07-11 07:03:24 +00001070#else
1071 ((void) ssl);
Gilles Peskine449bd832023-01-11 14:50:10 +01001072 return 0;
Jerry Yu77f01482022-07-11 07:03:24 +00001073#endif
1074}
1075
Gilles Peskine449bd832023-01-11 14:50:10 +01001076static int ssl_tls13_determine_key_exchange_mode(mbedtls_ssl_context *ssl)
Jerry Yu77f01482022-07-11 07:03:24 +00001077{
1078 /*
1079 * Determine the key exchange algorithm to use.
1080 * There are three types of key exchanges supported in TLS 1.3:
1081 * - (EC)DH with ECDSA,
1082 * - (EC)DH with PSK,
1083 * - plain PSK.
1084 *
1085 * The PSK-based key exchanges may additionally be used with 0-RTT.
1086 *
1087 * Our built-in order of preference is
Jerry Yuce6ed702022-07-22 21:49:53 +08001088 * 1 ) (EC)DHE-PSK Mode ( psk_ephemeral )
1089 * 2 ) Certificate Mode ( ephemeral )
1090 * 3 ) Plain PSK Mode ( psk )
Jerry Yu77f01482022-07-11 07:03:24 +00001091 */
1092
Xiaokang Qian9f1747b2023-03-30 02:50:04 +00001093 ssl->handshake->key_exchange_mode =
1094 MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_NONE;
Jerry Yu77f01482022-07-11 07:03:24 +00001095
Pengyu Lvbc4aab72023-12-01 15:37:24 +08001096 if (ssl_tls13_key_exchange_is_psk_ephemeral_available(ssl)) {
Jerry Yu77f01482022-07-11 07:03:24 +00001097 ssl->handshake->key_exchange_mode =
1098 MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK_EPHEMERAL;
Gilles Peskine449bd832023-01-11 14:50:10 +01001099 MBEDTLS_SSL_DEBUG_MSG(2, ("key exchange mode: psk_ephemeral"));
1100 } else
Pengyu Lvbc4aab72023-12-01 15:37:24 +08001101 if (ssl_tls13_key_exchange_is_ephemeral_available(ssl)) {
Jerry Yu77f01482022-07-11 07:03:24 +00001102 ssl->handshake->key_exchange_mode =
1103 MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL;
Gilles Peskine449bd832023-01-11 14:50:10 +01001104 MBEDTLS_SSL_DEBUG_MSG(2, ("key exchange mode: ephemeral"));
1105 } else
Pengyu Lvbc4aab72023-12-01 15:37:24 +08001106 if (ssl_tls13_key_exchange_is_psk_available(ssl)) {
Jerry Yuce6ed702022-07-22 21:49:53 +08001107 ssl->handshake->key_exchange_mode =
1108 MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK;
Gilles Peskine449bd832023-01-11 14:50:10 +01001109 MBEDTLS_SSL_DEBUG_MSG(2, ("key exchange mode: psk"));
1110 } else {
Jerry Yu77f01482022-07-11 07:03:24 +00001111 MBEDTLS_SSL_DEBUG_MSG(
Gilles Peskine449bd832023-01-11 14:50:10 +01001112 1,
1113 ("ClientHello message misses mandatory extensions."));
1114 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_MISSING_EXTENSION,
1115 MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER);
1116 return MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER;
Jerry Yu77f01482022-07-11 07:03:24 +00001117 }
1118
Gilles Peskine449bd832023-01-11 14:50:10 +01001119 return 0;
Jerry Yu77f01482022-07-11 07:03:24 +00001120
XiaokangQian7807f9f2022-02-15 10:04:37 +00001121}
1122
XiaokangQian81802f42022-06-10 13:25:22 +00001123#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
Ronald Cron928cbd32022-10-04 16:14:26 +02001124 defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED)
Ronald Cron67ea2542022-09-15 17:34:42 +02001125
1126#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01001127static psa_algorithm_t ssl_tls13_iana_sig_alg_to_psa_alg(uint16_t sig_alg)
Ronald Cron67ea2542022-09-15 17:34:42 +02001128{
Gilles Peskine449bd832023-01-11 14:50:10 +01001129 switch (sig_alg) {
Ronald Cron67ea2542022-09-15 17:34:42 +02001130 case MBEDTLS_TLS1_3_SIG_ECDSA_SECP256R1_SHA256:
Gilles Peskine449bd832023-01-11 14:50:10 +01001131 return PSA_ALG_ECDSA(PSA_ALG_SHA_256);
Ronald Cron67ea2542022-09-15 17:34:42 +02001132 case MBEDTLS_TLS1_3_SIG_ECDSA_SECP384R1_SHA384:
Gilles Peskine449bd832023-01-11 14:50:10 +01001133 return PSA_ALG_ECDSA(PSA_ALG_SHA_384);
Ronald Cron67ea2542022-09-15 17:34:42 +02001134 case MBEDTLS_TLS1_3_SIG_ECDSA_SECP521R1_SHA512:
Gilles Peskine449bd832023-01-11 14:50:10 +01001135 return PSA_ALG_ECDSA(PSA_ALG_SHA_512);
Ronald Cron67ea2542022-09-15 17:34:42 +02001136 case MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA256:
Gilles Peskine449bd832023-01-11 14:50:10 +01001137 return PSA_ALG_RSA_PSS(PSA_ALG_SHA_256);
Ronald Cron67ea2542022-09-15 17:34:42 +02001138 case MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA384:
Gilles Peskine449bd832023-01-11 14:50:10 +01001139 return PSA_ALG_RSA_PSS(PSA_ALG_SHA_384);
Ronald Cron67ea2542022-09-15 17:34:42 +02001140 case MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA512:
Gilles Peskine449bd832023-01-11 14:50:10 +01001141 return PSA_ALG_RSA_PSS(PSA_ALG_SHA_512);
Ronald Cron67ea2542022-09-15 17:34:42 +02001142 case MBEDTLS_TLS1_3_SIG_RSA_PKCS1_SHA256:
Gilles Peskine449bd832023-01-11 14:50:10 +01001143 return PSA_ALG_RSA_PKCS1V15_SIGN(PSA_ALG_SHA_256);
Ronald Cron67ea2542022-09-15 17:34:42 +02001144 case MBEDTLS_TLS1_3_SIG_RSA_PKCS1_SHA384:
Gilles Peskine449bd832023-01-11 14:50:10 +01001145 return PSA_ALG_RSA_PKCS1V15_SIGN(PSA_ALG_SHA_384);
Ronald Cron67ea2542022-09-15 17:34:42 +02001146 case MBEDTLS_TLS1_3_SIG_RSA_PKCS1_SHA512:
Gilles Peskine449bd832023-01-11 14:50:10 +01001147 return PSA_ALG_RSA_PKCS1V15_SIGN(PSA_ALG_SHA_512);
Ronald Cron67ea2542022-09-15 17:34:42 +02001148 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01001149 return PSA_ALG_NONE;
Ronald Cron67ea2542022-09-15 17:34:42 +02001150 }
1151}
1152#endif /* MBEDTLS_USE_PSA_CRYPTO */
1153
XiaokangQian23c5be62022-06-07 02:04:34 +00001154/*
XiaokangQianfb665a82022-06-15 03:57:21 +00001155 * Pick best ( private key, certificate chain ) pair based on the signature
1156 * algorithms supported by the client.
XiaokangQian23c5be62022-06-07 02:04:34 +00001157 */
Ronald Cronce7d76e2022-07-08 18:56:49 +02001158MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01001159static int ssl_tls13_pick_key_cert(mbedtls_ssl_context *ssl)
XiaokangQian23c5be62022-06-07 02:04:34 +00001160{
XiaokangQianfb665a82022-06-15 03:57:21 +00001161 mbedtls_ssl_key_cert *key_cert, *key_cert_list;
XiaokangQian81802f42022-06-10 13:25:22 +00001162 const uint16_t *sig_alg = ssl->handshake->received_sig_algs;
XiaokangQian23c5be62022-06-07 02:04:34 +00001163
1164#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Gilles Peskine449bd832023-01-11 14:50:10 +01001165 if (ssl->handshake->sni_key_cert != NULL) {
XiaokangQianfb665a82022-06-15 03:57:21 +00001166 key_cert_list = ssl->handshake->sni_key_cert;
Gilles Peskine449bd832023-01-11 14:50:10 +01001167 } else
XiaokangQian23c5be62022-06-07 02:04:34 +00001168#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
Gilles Peskine449bd832023-01-11 14:50:10 +01001169 key_cert_list = ssl->conf->key_cert;
XiaokangQian23c5be62022-06-07 02:04:34 +00001170
Gilles Peskine449bd832023-01-11 14:50:10 +01001171 if (key_cert_list == NULL) {
1172 MBEDTLS_SSL_DEBUG_MSG(3, ("server has no certificate"));
1173 return -1;
XiaokangQian23c5be62022-06-07 02:04:34 +00001174 }
1175
Gilles Peskine449bd832023-01-11 14:50:10 +01001176 for (; *sig_alg != MBEDTLS_TLS1_3_SIG_NONE; sig_alg++) {
1177 if (!mbedtls_ssl_sig_alg_is_offered(ssl, *sig_alg)) {
Ronald Cron67ea2542022-09-15 17:34:42 +02001178 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +01001179 }
Ronald Cron67ea2542022-09-15 17:34:42 +02001180
Gilles Peskine449bd832023-01-11 14:50:10 +01001181 if (!mbedtls_ssl_tls13_sig_alg_for_cert_verify_is_supported(*sig_alg)) {
Ronald Cron67ea2542022-09-15 17:34:42 +02001182 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +01001183 }
Ronald Cron67ea2542022-09-15 17:34:42 +02001184
Gilles Peskine449bd832023-01-11 14:50:10 +01001185 for (key_cert = key_cert_list; key_cert != NULL;
1186 key_cert = key_cert->next) {
Ronald Cron67ea2542022-09-15 17:34:42 +02001187#if defined(MBEDTLS_USE_PSA_CRYPTO)
1188 psa_algorithm_t psa_alg = PSA_ALG_NONE;
1189#endif /* MBEDTLS_USE_PSA_CRYPTO */
1190
Gilles Peskine449bd832023-01-11 14:50:10 +01001191 MBEDTLS_SSL_DEBUG_CRT(3, "certificate (chain) candidate",
1192 key_cert->cert);
XiaokangQian81802f42022-06-10 13:25:22 +00001193
1194 /*
Gilles Peskine449bd832023-01-11 14:50:10 +01001195 * This avoids sending the client a cert it'll reject based on
1196 * keyUsage or other extensions.
1197 */
1198 if (mbedtls_x509_crt_check_key_usage(
1199 key_cert->cert, MBEDTLS_X509_KU_DIGITAL_SIGNATURE) != 0 ||
XiaokangQian81802f42022-06-10 13:25:22 +00001200 mbedtls_x509_crt_check_extended_key_usage(
XiaokangQianfb665a82022-06-15 03:57:21 +00001201 key_cert->cert, MBEDTLS_OID_SERVER_AUTH,
Gilles Peskine449bd832023-01-11 14:50:10 +01001202 MBEDTLS_OID_SIZE(MBEDTLS_OID_SERVER_AUTH)) != 0) {
1203 MBEDTLS_SSL_DEBUG_MSG(3, ("certificate mismatch: "
1204 "(extended) key usage extension"));
XiaokangQian81802f42022-06-10 13:25:22 +00001205 continue;
1206 }
XiaokangQian23c5be62022-06-07 02:04:34 +00001207
Gilles Peskine449bd832023-01-11 14:50:10 +01001208 MBEDTLS_SSL_DEBUG_MSG(3,
1209 ("ssl_tls13_pick_key_cert:"
1210 "check signature algorithm %s [%04x]",
1211 mbedtls_ssl_sig_alg_to_str(*sig_alg),
1212 *sig_alg));
Ronald Cron67ea2542022-09-15 17:34:42 +02001213#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01001214 psa_alg = ssl_tls13_iana_sig_alg_to_psa_alg(*sig_alg);
Ronald Cron67ea2542022-09-15 17:34:42 +02001215#endif /* MBEDTLS_USE_PSA_CRYPTO */
1216
Gilles Peskine449bd832023-01-11 14:50:10 +01001217 if (mbedtls_ssl_tls13_check_sig_alg_cert_key_match(
1218 *sig_alg, &key_cert->cert->pk)
Ronald Cron67ea2542022-09-15 17:34:42 +02001219#if defined(MBEDTLS_USE_PSA_CRYPTO)
1220 && psa_alg != PSA_ALG_NONE &&
Gilles Peskine449bd832023-01-11 14:50:10 +01001221 mbedtls_pk_can_do_ext(&key_cert->cert->pk, psa_alg,
1222 PSA_KEY_USAGE_SIGN_HASH) == 1
Ronald Cron67ea2542022-09-15 17:34:42 +02001223#endif /* MBEDTLS_USE_PSA_CRYPTO */
Gilles Peskine449bd832023-01-11 14:50:10 +01001224 ) {
XiaokangQianfb665a82022-06-15 03:57:21 +00001225 ssl->handshake->key_cert = key_cert;
Gilles Peskine449bd832023-01-11 14:50:10 +01001226 MBEDTLS_SSL_DEBUG_MSG(3,
1227 ("ssl_tls13_pick_key_cert:"
1228 "selected signature algorithm"
1229 " %s [%04x]",
1230 mbedtls_ssl_sig_alg_to_str(*sig_alg),
1231 *sig_alg));
XiaokangQianfb665a82022-06-15 03:57:21 +00001232 MBEDTLS_SSL_DEBUG_CRT(
Gilles Peskine449bd832023-01-11 14:50:10 +01001233 3, "selected certificate (chain)",
1234 ssl->handshake->key_cert->cert);
1235 return 0;
XiaokangQian81802f42022-06-10 13:25:22 +00001236 }
XiaokangQian81802f42022-06-10 13:25:22 +00001237 }
XiaokangQian23c5be62022-06-07 02:04:34 +00001238 }
1239
Gilles Peskine449bd832023-01-11 14:50:10 +01001240 MBEDTLS_SSL_DEBUG_MSG(2, ("ssl_tls13_pick_key_cert:"
1241 "no suitable certificate found"));
1242 return -1;
XiaokangQian23c5be62022-06-07 02:04:34 +00001243}
XiaokangQian81802f42022-06-10 13:25:22 +00001244#endif /* MBEDTLS_X509_CRT_PARSE_C &&
Ronald Cron928cbd32022-10-04 16:14:26 +02001245 MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED */
XiaokangQian23c5be62022-06-07 02:04:34 +00001246
XiaokangQian4080a7f2022-04-11 09:55:18 +00001247/*
XiaokangQianed582dd2022-04-13 08:21:05 +00001248 *
1249 * STATE HANDLING: ClientHello
1250 *
XiaokangQianb67384d2022-04-19 00:02:38 +00001251 * There are three possible classes of outcomes when parsing the ClientHello:
XiaokangQianed582dd2022-04-13 08:21:05 +00001252 *
XiaokangQianb67384d2022-04-19 00:02:38 +00001253 * 1) The ClientHello was well-formed and matched the server's configuration.
XiaokangQianed582dd2022-04-13 08:21:05 +00001254 *
1255 * In this case, the server progresses to sending its ServerHello.
1256 *
XiaokangQianb67384d2022-04-19 00:02:38 +00001257 * 2) The ClientHello was well-formed but didn't match the server's
1258 * configuration.
XiaokangQianed582dd2022-04-13 08:21:05 +00001259 *
1260 * For example, the client might not have offered a key share which
1261 * the server supports, or the server might require a cookie.
1262 *
1263 * In this case, the server sends a HelloRetryRequest.
1264 *
XiaokangQianb67384d2022-04-19 00:02:38 +00001265 * 3) The ClientHello was ill-formed
XiaokangQianed582dd2022-04-13 08:21:05 +00001266 *
1267 * In this case, we abort the handshake.
1268 *
1269 */
1270
1271/*
XiaokangQian4080a7f2022-04-11 09:55:18 +00001272 * Structure of this message:
1273 *
XiaokangQiane8ff3502022-04-22 02:34:40 +00001274 * uint16 ProtocolVersion;
1275 * opaque Random[32];
1276 * uint8 CipherSuite[2]; // Cryptographic suite selector
XiaokangQian4080a7f2022-04-11 09:55:18 +00001277 *
XiaokangQiane8ff3502022-04-22 02:34:40 +00001278 * struct {
1279 * ProtocolVersion legacy_version = 0x0303; // TLS v1.2
1280 * Random random;
1281 * opaque legacy_session_id<0..32>;
1282 * CipherSuite cipher_suites<2..2^16-2>;
1283 * opaque legacy_compression_methods<1..2^8-1>;
1284 * Extension extensions<8..2^16-1>;
1285 * } ClientHello;
XiaokangQian4080a7f2022-04-11 09:55:18 +00001286 */
XiaokangQianed582dd2022-04-13 08:21:05 +00001287
1288#define SSL_CLIENT_HELLO_OK 0
1289#define SSL_CLIENT_HELLO_HRR_REQUIRED 1
Ronald Cron5af4c7f2023-03-07 20:46:59 +01001290#define SSL_CLIENT_HELLO_TLS1_2 2
XiaokangQianed582dd2022-04-13 08:21:05 +00001291
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001292MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01001293static int ssl_tls13_parse_client_hello(mbedtls_ssl_context *ssl,
1294 const unsigned char *buf,
1295 const unsigned char *end)
XiaokangQian7807f9f2022-02-15 10:04:37 +00001296{
XiaokangQianb67384d2022-04-19 00:02:38 +00001297 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1298 const unsigned char *p = buf;
Ronald Crond540d992023-03-07 09:41:48 +01001299 const unsigned char *random;
XiaokangQian4080a7f2022-04-11 09:55:18 +00001300 size_t legacy_session_id_len;
Ronald Croncada4102023-03-07 09:51:39 +01001301 const unsigned char *legacy_session_id;
XiaokangQian318dc762022-04-20 09:43:51 +00001302 size_t cipher_suites_len;
Ronald Cron2f16b4e2023-03-07 10:07:32 +01001303 const unsigned char *cipher_suites;
XiaokangQian060d8672022-04-21 09:24:56 +00001304 const unsigned char *cipher_suites_end;
XiaokangQianb67384d2022-04-19 00:02:38 +00001305 size_t extensions_len;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001306 const unsigned char *extensions_end;
Ronald Croneff56732023-04-03 17:36:31 +02001307 const unsigned char *supported_versions_data;
1308 const unsigned char *supported_versions_data_end;
Jerry Yuc4bf5d62022-10-29 09:08:47 +08001309 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
Jerry Yu49ca9282022-05-05 11:05:22 +08001310 int hrr_required = 0;
Ronald Cron8a74f072023-06-14 17:59:29 +02001311 int no_usable_share_for_key_agreement = 0;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001312
Ronald Cron41a443a2022-10-04 16:38:25 +02001313#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED)
Jerry Yu29d9faa2022-08-23 17:52:45 +08001314 const unsigned char *pre_shared_key_ext = NULL;
Jerry Yu1c105562022-07-10 06:32:38 +00001315 const unsigned char *pre_shared_key_ext_end = NULL;
Ronald Cron41a443a2022-10-04 16:38:25 +02001316#endif
XiaokangQian7807f9f2022-02-15 10:04:37 +00001317
XiaokangQian7807f9f2022-02-15 10:04:37 +00001318 /*
XiaokangQianb67384d2022-04-19 00:02:38 +00001319 * ClientHello layout:
XiaokangQian7807f9f2022-02-15 10:04:37 +00001320 * 0 . 1 protocol version
XiaokangQiane8ff3502022-04-22 02:34:40 +00001321 * 2 . 33 random bytes
XiaokangQianc5763b52022-04-02 03:34:37 +00001322 * 34 . 34 session id length ( 1 byte )
XiaokangQian7807f9f2022-02-15 10:04:37 +00001323 * 35 . 34+x session id
XiaokangQian7807f9f2022-02-15 10:04:37 +00001324 * .. . .. ciphersuite list length ( 2 bytes )
1325 * .. . .. ciphersuite list
1326 * .. . .. compression alg. list length ( 1 byte )
1327 * .. . .. compression alg. list
1328 * .. . .. extensions length ( 2 bytes, optional )
1329 * .. . .. extensions ( optional )
1330 */
1331
XiaokangQianb67384d2022-04-19 00:02:38 +00001332 /*
bootstrap-prime6dbbf442022-05-17 19:30:44 -04001333 * Minimal length ( with everything empty and extensions omitted ) is
XiaokangQian7807f9f2022-02-15 10:04:37 +00001334 * 2 + 32 + 1 + 2 + 1 = 38 bytes. Check that first, so that we can
1335 * read at least up to session id length without worrying.
1336 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001337 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, 38);
XiaokangQian7807f9f2022-02-15 10:04:37 +00001338
1339 /* ...
1340 * ProtocolVersion legacy_version = 0x0303; // TLS 1.2
1341 * ...
1342 * with ProtocolVersion defined as:
1343 * uint16 ProtocolVersion;
1344 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001345 if (mbedtls_ssl_read_version(p, ssl->conf->transport) !=
1346 MBEDTLS_SSL_VERSION_TLS1_2) {
1347 MBEDTLS_SSL_DEBUG_MSG(1, ("Unsupported version of TLS."));
1348 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_PROTOCOL_VERSION,
1349 MBEDTLS_ERR_SSL_BAD_PROTOCOL_VERSION);
1350 return MBEDTLS_ERR_SSL_BAD_PROTOCOL_VERSION;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001351 }
1352 p += 2;
1353
Jerry Yuc1be19f2022-04-23 16:11:39 +08001354 /* ...
1355 * Random random;
1356 * ...
XiaokangQianb67384d2022-04-19 00:02:38 +00001357 * with Random defined as:
1358 * opaque Random[32];
XiaokangQian7807f9f2022-02-15 10:04:37 +00001359 */
Ronald Crond540d992023-03-07 09:41:48 +01001360 random = p;
XiaokangQian08037552022-04-20 07:16:41 +00001361 p += MBEDTLS_CLIENT_HELLO_RANDOM_LEN;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001362
Jerry Yuc1be19f2022-04-23 16:11:39 +08001363 /* ...
XiaokangQianb67384d2022-04-19 00:02:38 +00001364 * opaque legacy_session_id<0..32>;
Jerry Yuc1be19f2022-04-23 16:11:39 +08001365 * ...
XiaokangQian7807f9f2022-02-15 10:04:37 +00001366 */
Ronald Croncada4102023-03-07 09:51:39 +01001367 legacy_session_id_len = *(p++);
1368 legacy_session_id = p;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001369
XiaokangQianb67384d2022-04-19 00:02:38 +00001370 /*
1371 * Check we have enough data for the legacy session identifier
Jerry Yue95c8af2022-07-26 15:48:20 +08001372 * and the ciphersuite list length.
XiaokangQianb67384d2022-04-19 00:02:38 +00001373 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001374 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, legacy_session_id_len + 2);
XiaokangQian4080a7f2022-04-11 09:55:18 +00001375 p += legacy_session_id_len;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001376
Ronald Cron2f16b4e2023-03-07 10:07:32 +01001377 /* ...
1378 * CipherSuite cipher_suites<2..2^16-2>;
1379 * ...
1380 * with CipherSuite defined as:
1381 * uint8 CipherSuite[2];
1382 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001383 cipher_suites_len = MBEDTLS_GET_UINT16_BE(p, 0);
XiaokangQian7807f9f2022-02-15 10:04:37 +00001384 p += 2;
Ronald Cron2f16b4e2023-03-07 10:07:32 +01001385 cipher_suites = p;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001386
Ronald Cronfc7ae872023-02-16 15:32:19 +01001387 /*
1388 * The length of the ciphersuite list has to be even.
1389 */
1390 if (cipher_suites_len & 1) {
1391 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR,
1392 MBEDTLS_ERR_SSL_DECODE_ERROR);
1393 return MBEDTLS_ERR_SSL_DECODE_ERROR;
1394 }
1395
XiaokangQianb67384d2022-04-19 00:02:38 +00001396 /* Check we have enough data for the ciphersuite list, the legacy
1397 * compression methods and the length of the extensions.
Jerry Yue95c8af2022-07-26 15:48:20 +08001398 *
1399 * cipher_suites cipher_suites_len bytes
1400 * legacy_compression_methods 2 bytes
1401 * extensions_len 2 bytes
XiaokangQianb67384d2022-04-19 00:02:38 +00001402 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001403 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, cipher_suites_len + 2 + 2);
Ronald Cron8c527d02023-03-07 15:47:47 +01001404 p += cipher_suites_len;
1405 cipher_suites_end = p;
Jerry Yu5725f1c2022-08-21 17:27:16 +08001406
1407 /*
Ronald Cron8c527d02023-03-07 15:47:47 +01001408 * Search for the supported versions extension and parse it to determine
1409 * if the client supports TLS 1.3.
1410 */
1411 ret = mbedtls_ssl_tls13_is_supported_versions_ext_present_in_exts(
1412 ssl, p + 2, end,
Ronald Croneff56732023-04-03 17:36:31 +02001413 &supported_versions_data, &supported_versions_data_end);
Ronald Cron8c527d02023-03-07 15:47:47 +01001414 if (ret < 0) {
1415 MBEDTLS_SSL_DEBUG_RET(1,
1416 ("mbedtls_ssl_tls13_is_supported_versions_ext_present_in_exts"), ret);
1417 return ret;
1418 }
1419
1420 if (ret == 0) {
Ronald Cron5af4c7f2023-03-07 20:46:59 +01001421 return SSL_CLIENT_HELLO_TLS1_2;
Ronald Cron8c527d02023-03-07 15:47:47 +01001422 }
1423
Ronald Cron5af4c7f2023-03-07 20:46:59 +01001424 if (ret == 1) {
1425 ret = ssl_tls13_parse_supported_versions_ext(ssl,
Ronald Croneff56732023-04-03 17:36:31 +02001426 supported_versions_data,
1427 supported_versions_data_end);
Ronald Cron5af4c7f2023-03-07 20:46:59 +01001428 if (ret < 0) {
1429 MBEDTLS_SSL_DEBUG_RET(1,
1430 ("ssl_tls13_parse_supported_versions_ext"), ret);
1431 return ret;
1432 }
1433
Ronald Cronb828c7d2023-04-03 16:37:22 +02001434 /*
1435 * The supported versions extension was parsed successfully as the
1436 * value returned by ssl_tls13_parse_supported_versions_ext() is
1437 * positive. The return value is then equal to
1438 * MBEDTLS_SSL_VERSION_TLS1_2 or MBEDTLS_SSL_VERSION_TLS1_3, defining
1439 * the TLS version to negotiate.
1440 */
Ronald Cron5af4c7f2023-03-07 20:46:59 +01001441 if (MBEDTLS_SSL_VERSION_TLS1_2 == ret) {
1442 return SSL_CLIENT_HELLO_TLS1_2;
1443 }
Ronald Cron8c527d02023-03-07 15:47:47 +01001444 }
1445
1446 /*
1447 * We negotiate TLS 1.3.
Ronald Cron64582392023-03-07 09:21:40 +01001448 */
1449 ssl->tls_version = MBEDTLS_SSL_VERSION_TLS1_3;
Ronald Cron64582392023-03-07 09:21:40 +01001450 ssl->session_negotiate->tls_version = MBEDTLS_SSL_VERSION_TLS1_3;
1451 ssl->session_negotiate->endpoint = ssl->conf->endpoint;
Ronald Cron64582392023-03-07 09:21:40 +01001452
1453 /*
Ronald Crondad02b22023-04-06 09:57:52 +02001454 * We are negotiating the version 1.3 of the protocol. Do what we have
Ronald Croncada4102023-03-07 09:51:39 +01001455 * postponed: copy of the client random bytes, copy of the legacy session
Ronald Cron2f16b4e2023-03-07 10:07:32 +01001456 * identifier and selection of the TLS 1.3 cipher suite.
Ronald Crond540d992023-03-07 09:41:48 +01001457 */
1458 MBEDTLS_SSL_DEBUG_BUF(3, "client hello, random bytes",
1459 random, MBEDTLS_CLIENT_HELLO_RANDOM_LEN);
1460 memcpy(&handshake->randbytes[0], random, MBEDTLS_CLIENT_HELLO_RANDOM_LEN);
1461
Ronald Croncada4102023-03-07 09:51:39 +01001462 if (legacy_session_id_len > sizeof(ssl->session_negotiate->id)) {
1463 MBEDTLS_SSL_DEBUG_MSG(1, ("bad client hello message"));
1464 return MBEDTLS_ERR_SSL_DECODE_ERROR;
1465 }
1466 ssl->session_negotiate->id_len = legacy_session_id_len;
1467 MBEDTLS_SSL_DEBUG_BUF(3, "client hello, session id",
1468 legacy_session_id, legacy_session_id_len);
1469 memcpy(&ssl->session_negotiate->id[0],
1470 legacy_session_id, legacy_session_id_len);
1471
Ronald Crond540d992023-03-07 09:41:48 +01001472 /*
Jerry Yu5725f1c2022-08-21 17:27:16 +08001473 * Search for a matching ciphersuite
1474 */
Ronald Cron2f16b4e2023-03-07 10:07:32 +01001475 MBEDTLS_SSL_DEBUG_BUF(3, "client hello, list of cipher suites",
1476 cipher_suites, cipher_suites_len);
Ronald Crone45afd72023-04-04 15:10:06 +02001477 for (const unsigned char *cipher_suites_p = cipher_suites;
1478 cipher_suites_p < cipher_suites_end; cipher_suites_p += 2) {
XiaokangQiane8ff3502022-04-22 02:34:40 +00001479 uint16_t cipher_suite;
Gilles Peskine449bd832023-01-11 14:50:10 +01001480 const mbedtls_ssl_ciphersuite_t *ciphersuite_info;
Jerry Yu5725f1c2022-08-21 17:27:16 +08001481
Ronald Crond89360b2023-02-21 08:53:33 +01001482 /*
Ronald Crone45afd72023-04-04 15:10:06 +02001483 * "cipher_suites_end - cipher_suites_p is even" is an invariant of the
1484 * loop. As cipher_suites_end - cipher_suites_p > 0, we have
1485 * cipher_suites_end - cipher_suites_p >= 2 and it is thus safe to read
1486 * two bytes.
Ronald Crond89360b2023-02-21 08:53:33 +01001487 */
Ronald Crone45afd72023-04-04 15:10:06 +02001488 cipher_suite = MBEDTLS_GET_UINT16_BE(cipher_suites_p, 0);
Jerry Yuc5a23a02022-08-25 10:51:44 +08001489 ciphersuite_info = ssl_tls13_validate_peer_ciphersuite(
Gilles Peskine449bd832023-01-11 14:50:10 +01001490 ssl, cipher_suite);
1491 if (ciphersuite_info == NULL) {
Jerry Yu5725f1c2022-08-21 17:27:16 +08001492 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +01001493 }
Jerry Yu5725f1c2022-08-21 17:27:16 +08001494
Jerry Yu5725f1c2022-08-21 17:27:16 +08001495 ssl->session_negotiate->ciphersuite = cipher_suite;
Jerry Yuc4bf5d62022-10-29 09:08:47 +08001496 handshake->ciphersuite_info = ciphersuite_info;
Gilles Peskine449bd832023-01-11 14:50:10 +01001497 MBEDTLS_SSL_DEBUG_MSG(2, ("selected ciphersuite: %04x - %s",
1498 cipher_suite,
1499 ciphersuite_info->name));
Ronald Cron25e9ec62023-02-16 15:35:16 +01001500 break;
XiaokangQian17f974c2022-04-19 09:57:41 +00001501 }
Jerry Yu5725f1c2022-08-21 17:27:16 +08001502
Gilles Peskine449bd832023-01-11 14:50:10 +01001503 if (handshake->ciphersuite_info == NULL) {
1504 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE,
1505 MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE);
1506 return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
Jerry Yu5725f1c2022-08-21 17:27:16 +08001507 }
Jerry Yuc1be19f2022-04-23 16:11:39 +08001508
XiaokangQian4080a7f2022-04-11 09:55:18 +00001509 /* ...
XiaokangQianb67384d2022-04-19 00:02:38 +00001510 * opaque legacy_compression_methods<1..2^8-1>;
XiaokangQian4080a7f2022-04-11 09:55:18 +00001511 * ...
XiaokangQian7807f9f2022-02-15 10:04:37 +00001512 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001513 if (p[0] != 1 || p[1] != MBEDTLS_SSL_COMPRESS_NULL) {
1514 MBEDTLS_SSL_DEBUG_MSG(1, ("bad legacy compression method"));
1515 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER,
1516 MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER);
1517 return MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001518 }
XiaokangQianb67384d2022-04-19 00:02:38 +00001519 p += 2;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001520
Jerry Yuc1be19f2022-04-23 16:11:39 +08001521 /* ...
XiaokangQianb67384d2022-04-19 00:02:38 +00001522 * Extension extensions<8..2^16-1>;
Jerry Yuc1be19f2022-04-23 16:11:39 +08001523 * ...
XiaokangQianb67384d2022-04-19 00:02:38 +00001524 * with Extension defined as:
1525 * struct {
1526 * ExtensionType extension_type;
1527 * opaque extension_data<0..2^16-1>;
1528 * } Extension;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001529 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001530 extensions_len = MBEDTLS_GET_UINT16_BE(p, 0);
XiaokangQian7807f9f2022-02-15 10:04:37 +00001531 p += 2;
Gilles Peskine449bd832023-01-11 14:50:10 +01001532 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, extensions_len);
XiaokangQiane8ff3502022-04-22 02:34:40 +00001533 extensions_end = p + extensions_len;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001534
Gilles Peskine449bd832023-01-11 14:50:10 +01001535 MBEDTLS_SSL_DEBUG_BUF(3, "client hello extensions", p, extensions_len);
Jerry Yu63a459c2022-10-31 13:38:40 +08001536 handshake->received_extensions = MBEDTLS_SSL_EXT_MASK_NONE;
Jerry Yu0c354a22022-08-29 15:25:36 +08001537
Gilles Peskine449bd832023-01-11 14:50:10 +01001538 while (p < extensions_end) {
XiaokangQian7807f9f2022-02-15 10:04:37 +00001539 unsigned int extension_type;
1540 size_t extension_data_len;
1541 const unsigned char *extension_data_end;
Jerry Yu263dbf72022-10-26 10:51:27 +08001542 uint32_t allowed_exts = MBEDTLS_SSL_TLS1_3_ALLOWED_EXTS_OF_CH;
1543
Ronald Cron5fbd2702024-02-14 10:03:36 +01001544 if (ssl->handshake->hello_retry_request_flag) {
Jerry Yu263dbf72022-10-26 10:51:27 +08001545 /* Do not accept early data extension in 2nd ClientHello */
1546 allowed_exts &= ~MBEDTLS_SSL_EXT_MASK(EARLY_DATA);
1547 }
XiaokangQian7807f9f2022-02-15 10:04:37 +00001548
Jerry Yu0c354a22022-08-29 15:25:36 +08001549 /* RFC 8446, section 4.2.11
Jerry Yu1c9247c2022-07-21 12:37:39 +08001550 *
1551 * The "pre_shared_key" extension MUST be the last extension in the
1552 * ClientHello (this facilitates implementation as described below).
1553 * Servers MUST check that it is the last extension and otherwise fail
1554 * the handshake with an "illegal_parameter" alert.
1555 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001556 if (handshake->received_extensions & MBEDTLS_SSL_EXT_MASK(PRE_SHARED_KEY)) {
Jerry Yu1c9247c2022-07-21 12:37:39 +08001557 MBEDTLS_SSL_DEBUG_MSG(
Gilles Peskine449bd832023-01-11 14:50:10 +01001558 3, ("pre_shared_key is not last extension."));
Jerry Yu1c9247c2022-07-21 12:37:39 +08001559 MBEDTLS_SSL_PEND_FATAL_ALERT(
1560 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER,
Gilles Peskine449bd832023-01-11 14:50:10 +01001561 MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER);
1562 return MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER;
Jerry Yu1c9247c2022-07-21 12:37:39 +08001563 }
1564
Gilles Peskine449bd832023-01-11 14:50:10 +01001565 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, extensions_end, 4);
1566 extension_type = MBEDTLS_GET_UINT16_BE(p, 0);
1567 extension_data_len = MBEDTLS_GET_UINT16_BE(p, 2);
XiaokangQian7807f9f2022-02-15 10:04:37 +00001568 p += 4;
1569
Gilles Peskine449bd832023-01-11 14:50:10 +01001570 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, extensions_end, extension_data_len);
XiaokangQian7807f9f2022-02-15 10:04:37 +00001571 extension_data_end = p + extension_data_len;
1572
Jerry Yuc4bf5d62022-10-29 09:08:47 +08001573 ret = mbedtls_ssl_tls13_check_received_extension(
Gilles Peskine449bd832023-01-11 14:50:10 +01001574 ssl, MBEDTLS_SSL_HS_CLIENT_HELLO, extension_type,
Jerry Yu263dbf72022-10-26 10:51:27 +08001575 allowed_exts);
Gilles Peskine449bd832023-01-11 14:50:10 +01001576 if (ret != 0) {
1577 return ret;
1578 }
Jerry Yue18dc7e2022-08-04 16:29:22 +08001579
Gilles Peskine449bd832023-01-11 14:50:10 +01001580 switch (extension_type) {
XiaokangQian40a35232022-05-07 09:02:40 +00001581#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
1582 case MBEDTLS_TLS_EXT_SERVERNAME:
Gilles Peskine449bd832023-01-11 14:50:10 +01001583 MBEDTLS_SSL_DEBUG_MSG(3, ("found ServerName extension"));
1584 ret = mbedtls_ssl_parse_server_name_ext(ssl, p,
1585 extension_data_end);
1586 if (ret != 0) {
XiaokangQian40a35232022-05-07 09:02:40 +00001587 MBEDTLS_SSL_DEBUG_RET(
Gilles Peskine449bd832023-01-11 14:50:10 +01001588 1, "mbedtls_ssl_parse_servername_ext", ret);
1589 return ret;
XiaokangQian40a35232022-05-07 09:02:40 +00001590 }
XiaokangQian40a35232022-05-07 09:02:40 +00001591 break;
1592#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
1593
Przemek Stekiel8c0a9532023-06-15 16:48:19 +02001594#if defined(PSA_WANT_ALG_ECDH) || defined(PSA_WANT_ALG_FFDH)
XiaokangQian7807f9f2022-02-15 10:04:37 +00001595 case MBEDTLS_TLS_EXT_SUPPORTED_GROUPS:
Gilles Peskine449bd832023-01-11 14:50:10 +01001596 MBEDTLS_SSL_DEBUG_MSG(3, ("found supported group extension"));
XiaokangQian7807f9f2022-02-15 10:04:37 +00001597
1598 /* Supported Groups Extension
1599 *
1600 * When sent by the client, the "supported_groups" extension
1601 * indicates the named groups which the client supports,
1602 * ordered from most preferred to least preferred.
1603 */
Jerry Yuc1be19f2022-04-23 16:11:39 +08001604 ret = ssl_tls13_parse_supported_groups_ext(
Gilles Peskine449bd832023-01-11 14:50:10 +01001605 ssl, p, extension_data_end);
1606 if (ret != 0) {
Xiaokang Qian9f1747b2023-03-30 02:50:04 +00001607 MBEDTLS_SSL_DEBUG_RET(
Xiaokang Qian8bce0e62023-04-04 10:15:48 +00001608 1, "ssl_tls13_parse_supported_groups_ext", ret);
Gilles Peskine449bd832023-01-11 14:50:10 +01001609 return ret;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001610 }
1611
XiaokangQian7807f9f2022-02-15 10:04:37 +00001612 break;
Przemek Stekiel8c0a9532023-06-15 16:48:19 +02001613#endif /* PSA_WANT_ALG_ECDH || PSA_WANT_ALG_FFDH*/
XiaokangQian7807f9f2022-02-15 10:04:37 +00001614
Valerio Settic9ae8622023-07-25 11:23:50 +02001615#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_EPHEMERAL_ENABLED)
XiaokangQian7807f9f2022-02-15 10:04:37 +00001616 case MBEDTLS_TLS_EXT_KEY_SHARE:
Gilles Peskine449bd832023-01-11 14:50:10 +01001617 MBEDTLS_SSL_DEBUG_MSG(3, ("found key share extension"));
XiaokangQian7807f9f2022-02-15 10:04:37 +00001618
1619 /*
1620 * Key Share Extension
1621 *
1622 * When sent by the client, the "key_share" extension
1623 * contains the endpoint's cryptographic parameters for
1624 * ECDHE/DHE key establishment methods.
1625 */
Jerry Yuc1be19f2022-04-23 16:11:39 +08001626 ret = ssl_tls13_parse_key_shares_ext(
Gilles Peskine449bd832023-01-11 14:50:10 +01001627 ssl, p, extension_data_end);
1628 if (ret == SSL_TLS1_3_PARSE_KEY_SHARES_EXT_NO_MATCH) {
Ronald Cron8a74f072023-06-14 17:59:29 +02001629 MBEDTLS_SSL_DEBUG_MSG(2, ("No usable share for key agreement."));
1630 no_usable_share_for_key_agreement = 1;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001631 }
1632
Gilles Peskine449bd832023-01-11 14:50:10 +01001633 if (ret < 0) {
Jerry Yu582dd062022-04-22 21:59:01 +08001634 MBEDTLS_SSL_DEBUG_RET(
Gilles Peskine449bd832023-01-11 14:50:10 +01001635 1, "ssl_tls13_parse_key_shares_ext", ret);
1636 return ret;
Jerry Yu582dd062022-04-22 21:59:01 +08001637 }
XiaokangQian7807f9f2022-02-15 10:04:37 +00001638
XiaokangQian7807f9f2022-02-15 10:04:37 +00001639 break;
Valerio Settic9ae8622023-07-25 11:23:50 +02001640#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_EPHEMERAL_ENABLED */
XiaokangQian7807f9f2022-02-15 10:04:37 +00001641
1642 case MBEDTLS_TLS_EXT_SUPPORTED_VERSIONS:
Ronald Cron8c527d02023-03-07 15:47:47 +01001643 /* Already parsed */
XiaokangQian7807f9f2022-02-15 10:04:37 +00001644 break;
1645
Ronald Cron41a443a2022-10-04 16:38:25 +02001646#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED)
Jerry Yue19e3b92022-07-08 12:04:51 +00001647 case MBEDTLS_TLS_EXT_PSK_KEY_EXCHANGE_MODES:
Xiaokang Qian9f1747b2023-03-30 02:50:04 +00001648 MBEDTLS_SSL_DEBUG_MSG(
1649 3, ("found psk key exchange modes extension"));
Jerry Yue19e3b92022-07-08 12:04:51 +00001650
1651 ret = ssl_tls13_parse_key_exchange_modes_ext(
Gilles Peskine449bd832023-01-11 14:50:10 +01001652 ssl, p, extension_data_end);
1653 if (ret != 0) {
Jerry Yue19e3b92022-07-08 12:04:51 +00001654 MBEDTLS_SSL_DEBUG_RET(
Gilles Peskine449bd832023-01-11 14:50:10 +01001655 1, "ssl_tls13_parse_key_exchange_modes_ext", ret);
1656 return ret;
Jerry Yue19e3b92022-07-08 12:04:51 +00001657 }
1658
Jerry Yue19e3b92022-07-08 12:04:51 +00001659 break;
Ronald Cron41a443a2022-10-04 16:38:25 +02001660#endif
Jerry Yue19e3b92022-07-08 12:04:51 +00001661
Jerry Yu1c105562022-07-10 06:32:38 +00001662 case MBEDTLS_TLS_EXT_PRE_SHARED_KEY:
Gilles Peskine449bd832023-01-11 14:50:10 +01001663 MBEDTLS_SSL_DEBUG_MSG(3, ("found pre_shared_key extension"));
1664 if ((handshake->received_extensions &
1665 MBEDTLS_SSL_EXT_MASK(PSK_KEY_EXCHANGE_MODES)) == 0) {
Jerry Yu13ab81d2022-07-22 23:17:11 +08001666 MBEDTLS_SSL_PEND_FATAL_ALERT(
1667 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER,
Gilles Peskine449bd832023-01-11 14:50:10 +01001668 MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER);
1669 return MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER;
Jerry Yu13ab81d2022-07-22 23:17:11 +08001670 }
Ronald Cron41a443a2022-10-04 16:38:25 +02001671#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED)
Jerry Yu1c105562022-07-10 06:32:38 +00001672 /* Delay processing of the PSK identity once we have
1673 * found out which algorithms to use. We keep a pointer
1674 * to the buffer and the size for later processing.
1675 */
Jerry Yu29d9faa2022-08-23 17:52:45 +08001676 pre_shared_key_ext = p;
Jerry Yu1c105562022-07-10 06:32:38 +00001677 pre_shared_key_ext_end = extension_data_end;
Jerry Yue18dc7e2022-08-04 16:29:22 +08001678#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED */
Jerry Yu1c105562022-07-10 06:32:38 +00001679 break;
Jerry Yu1c105562022-07-10 06:32:38 +00001680
XiaokangQianacb39922022-06-17 10:18:48 +00001681#if defined(MBEDTLS_SSL_ALPN)
1682 case MBEDTLS_TLS_EXT_ALPN:
Gilles Peskine449bd832023-01-11 14:50:10 +01001683 MBEDTLS_SSL_DEBUG_MSG(3, ("found alpn extension"));
XiaokangQianacb39922022-06-17 10:18:48 +00001684
Gilles Peskine449bd832023-01-11 14:50:10 +01001685 ret = mbedtls_ssl_parse_alpn_ext(ssl, p, extension_data_end);
1686 if (ret != 0) {
XiaokangQianacb39922022-06-17 10:18:48 +00001687 MBEDTLS_SSL_DEBUG_RET(
Gilles Peskine449bd832023-01-11 14:50:10 +01001688 1, ("mbedtls_ssl_parse_alpn_ext"), ret);
1689 return ret;
XiaokangQianacb39922022-06-17 10:18:48 +00001690 }
XiaokangQianacb39922022-06-17 10:18:48 +00001691 break;
1692#endif /* MBEDTLS_SSL_ALPN */
1693
Ronald Cron928cbd32022-10-04 16:14:26 +02001694#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED)
XiaokangQian7807f9f2022-02-15 10:04:37 +00001695 case MBEDTLS_TLS_EXT_SIG_ALG:
Gilles Peskine449bd832023-01-11 14:50:10 +01001696 MBEDTLS_SSL_DEBUG_MSG(3, ("found signature_algorithms extension"));
XiaokangQian7807f9f2022-02-15 10:04:37 +00001697
Gabor Mezei078e8032022-04-27 21:17:56 +02001698 ret = mbedtls_ssl_parse_sig_alg_ext(
Gilles Peskine449bd832023-01-11 14:50:10 +01001699 ssl, p, extension_data_end);
1700 if (ret != 0) {
Xiaokang Qian49f39c12023-04-06 02:31:02 +00001701 MBEDTLS_SSL_DEBUG_RET(
1702 1, "mbedtls_ssl_parse_sig_alg_ext", ret);
Gilles Peskine449bd832023-01-11 14:50:10 +01001703 return ret;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001704 }
XiaokangQian7807f9f2022-02-15 10:04:37 +00001705 break;
Ronald Cron928cbd32022-10-04 16:14:26 +02001706#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED */
XiaokangQian7807f9f2022-02-15 10:04:37 +00001707
Jan Bruckner151f6422023-02-10 12:45:19 +01001708#if defined(MBEDTLS_SSL_RECORD_SIZE_LIMIT)
1709 case MBEDTLS_TLS_EXT_RECORD_SIZE_LIMIT:
1710 MBEDTLS_SSL_DEBUG_MSG(3, ("found record_size_limit extension"));
1711
Xiaokang Qian9f1747b2023-03-30 02:50:04 +00001712 ret = mbedtls_ssl_tls13_parse_record_size_limit_ext(
1713 ssl, p, extension_data_end);
Jan Brucknerf482dcc2023-03-15 09:09:06 +01001714 if (ret != 0) {
1715 MBEDTLS_SSL_DEBUG_RET(
1716 1, ("mbedtls_ssl_tls13_parse_record_size_limit_ext"), ret);
1717 return ret;
1718 }
Jan Bruckner151f6422023-02-10 12:45:19 +01001719 break;
1720#endif /* MBEDTLS_SSL_RECORD_SIZE_LIMIT */
1721
XiaokangQian7807f9f2022-02-15 10:04:37 +00001722 default:
Jerry Yu79aa7212022-11-08 21:30:21 +08001723 MBEDTLS_SSL_PRINT_EXT(
Jerry Yu63a459c2022-10-31 13:38:40 +08001724 3, MBEDTLS_SSL_HS_CLIENT_HELLO,
Gilles Peskine449bd832023-01-11 14:50:10 +01001725 extension_type, "( ignored )");
Jerry Yu0c354a22022-08-29 15:25:36 +08001726 break;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001727 }
1728
1729 p += extension_data_len;
1730 }
1731
Gilles Peskine449bd832023-01-11 14:50:10 +01001732 MBEDTLS_SSL_PRINT_EXTS(3, MBEDTLS_SSL_HS_CLIENT_HELLO,
1733 handshake->received_extensions);
Jerry Yue18dc7e2022-08-04 16:29:22 +08001734
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +01001735 ret = mbedtls_ssl_add_hs_hdr_to_checksum(ssl,
1736 MBEDTLS_SSL_HS_CLIENT_HELLO,
1737 p - buf);
1738 if (0 != ret) {
1739 MBEDTLS_SSL_DEBUG_RET(1, ("mbedtls_ssl_add_hs_hdr_to_checksum"), ret);
1740 return ret;
1741 }
Jerry Yu032b15ce2022-07-11 06:10:03 +00001742
Ronald Cron41a443a2022-10-04 16:38:25 +02001743#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED)
XiaokangQian7807f9f2022-02-15 10:04:37 +00001744 /* Update checksum with either
1745 * - The entire content of the CH message, if no PSK extension is present
1746 * - The content up to but excluding the PSK extension, if present.
1747 */
Jerry Yu1c105562022-07-10 06:32:38 +00001748 /* If we've settled on a PSK-based exchange, parse PSK identity ext */
Pengyu Lvbc4aab72023-12-01 15:37:24 +08001749 if (ssl_tls13_key_exchange_is_psk_available(ssl) ||
1750 ssl_tls13_key_exchange_is_psk_ephemeral_available(ssl)) {
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +01001751 ret = handshake->update_checksum(ssl, buf,
1752 pre_shared_key_ext - buf);
1753 if (0 != ret) {
1754 MBEDTLS_SSL_DEBUG_RET(1, ("update_checksum"), ret);
1755 return ret;
1756 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001757 ret = ssl_tls13_parse_pre_shared_key_ext(ssl,
1758 pre_shared_key_ext,
1759 pre_shared_key_ext_end,
1760 cipher_suites,
1761 cipher_suites_end);
1762 if (ret == MBEDTLS_ERR_SSL_UNKNOWN_IDENTITY) {
1763 handshake->received_extensions &= ~MBEDTLS_SSL_EXT_MASK(PRE_SHARED_KEY);
1764 } else if (ret != 0) {
Jerry Yu63a459c2022-10-31 13:38:40 +08001765 MBEDTLS_SSL_DEBUG_RET(
Gilles Peskine449bd832023-01-11 14:50:10 +01001766 1, "ssl_tls13_parse_pre_shared_key_ext", ret);
1767 return ret;
Jerry Yu1c105562022-07-10 06:32:38 +00001768 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001769 } else
Ronald Cron41a443a2022-10-04 16:38:25 +02001770#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED */
Jerry Yu1c105562022-07-10 06:32:38 +00001771 {
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +01001772 ret = handshake->update_checksum(ssl, buf, p - buf);
1773 if (0 != ret) {
1774 MBEDTLS_SSL_DEBUG_RET(1, ("update_checksum"), ret);
1775 return ret;
1776 }
Jerry Yu1c105562022-07-10 06:32:38 +00001777 }
XiaokangQian7807f9f2022-02-15 10:04:37 +00001778
Gilles Peskine449bd832023-01-11 14:50:10 +01001779 ret = ssl_tls13_determine_key_exchange_mode(ssl);
1780 if (ret < 0) {
1781 return ret;
1782 }
XiaokangQian7807f9f2022-02-15 10:04:37 +00001783
Ronald Cron8a74f072023-06-14 17:59:29 +02001784 if (ssl->handshake->key_exchange_mode !=
1785 MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK) {
1786 hrr_required = (no_usable_share_for_key_agreement != 0);
1787 }
1788
Gilles Peskine449bd832023-01-11 14:50:10 +01001789 mbedtls_ssl_optimize_checksum(ssl, handshake->ciphersuite_info);
Jerry Yue5834fd2022-08-29 20:16:09 +08001790
Gilles Peskine449bd832023-01-11 14:50:10 +01001791 return hrr_required ? SSL_CLIENT_HELLO_HRR_REQUIRED : SSL_CLIENT_HELLO_OK;
XiaokangQian75fe8c72022-06-15 09:42:45 +00001792}
1793
Jerry Yuab0da372023-02-08 13:55:24 +08001794#if defined(MBEDTLS_SSL_EARLY_DATA)
Jerry Yu4caf3ca2023-11-15 16:13:47 +08001795static int ssl_tls13_check_early_data_requirements(mbedtls_ssl_context *ssl)
Jerry Yuab0da372023-02-08 13:55:24 +08001796{
1797 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
1798
Jerry Yu985c9672022-12-04 14:06:30 +08001799 if (ssl->conf->early_data_enabled == MBEDTLS_SSL_EARLY_DATA_DISABLED) {
1800 MBEDTLS_SSL_DEBUG_MSG(
Jerry Yu985c9672022-12-04 14:06:30 +08001801 1,
Jerry Yu454dda32023-10-31 15:13:54 +08001802 ("EarlyData: rejected, feature disabled in server configuration."));
Jerry Yu4caf3ca2023-11-15 16:13:47 +08001803 return -1;
Ronald Cron7b6ee942024-01-12 10:29:55 +01001804 }
1805
Jerry Yu454dda32023-10-31 15:13:54 +08001806 if (!handshake->resume) {
1807 /* We currently support early data only in the case of PSKs established
1808 via a NewSessionTicket message thus in the case of a session
1809 resumption. */
Jerry Yu985c9672022-12-04 14:06:30 +08001810 MBEDTLS_SSL_DEBUG_MSG(
Jerry Yu7ef9fd82023-11-07 14:30:38 +08001811 1, ("EarlyData: rejected, not a session resumption."));
Jerry Yu4caf3ca2023-11-15 16:13:47 +08001812 return -1;
Jerry Yu985c9672022-12-04 14:06:30 +08001813 }
1814
Jerry Yu82fd6c12023-10-31 16:32:19 +08001815 /* RFC 8446 4.2.10
1816 *
1817 * In order to accept early data, the server MUST have accepted a PSK cipher
1818 * suite and selected the first key offered in the client's "pre_shared_key"
1819 * extension. In addition, it MUST verify that the following values are the
1820 * same as those associated with the selected PSK:
1821 * - The TLS version number
1822 * - The selected cipher suite
1823 * - The selected ALPN [RFC7301] protocol, if any
1824 *
1825 * NOTE:
Jerry Yu7ef9fd82023-11-07 14:30:38 +08001826 * - The TLS version number is checked in
1827 * ssl_tls13_offered_psks_check_identity_match_ticket().
1828 * - ALPN is not checked for the time being (TODO).
Jerry Yu82fd6c12023-10-31 16:32:19 +08001829 */
1830
1831 if (handshake->selected_identity != 0) {
1832 MBEDTLS_SSL_DEBUG_MSG(
Jerry Yu7ef9fd82023-11-07 14:30:38 +08001833 1, ("EarlyData: rejected, the selected key in "
1834 "`pre_shared_key` is not the first one."));
Jerry Yu4caf3ca2023-11-15 16:13:47 +08001835 return -1;
Jerry Yu82fd6c12023-10-31 16:32:19 +08001836 }
1837
1838 if (handshake->ciphersuite_info->id !=
1839 ssl->session_negotiate->ciphersuite) {
1840 MBEDTLS_SSL_DEBUG_MSG(
Jerry Yu7ef9fd82023-11-07 14:30:38 +08001841 1, ("EarlyData: rejected, the selected ciphersuite is not the one "
1842 "of the selected pre-shared key."));
Jerry Yu4caf3ca2023-11-15 16:13:47 +08001843 return -1;
Jerry Yu82fd6c12023-10-31 16:32:19 +08001844
1845 }
Jerry Yu985c9672022-12-04 14:06:30 +08001846
Pengyu Lv94a42cc2023-12-06 10:04:17 +08001847 if (!mbedtls_ssl_tls13_session_ticket_allow_early_data(ssl->session_negotiate)) {
Jerry Yufceddb32022-12-12 15:30:34 +08001848 MBEDTLS_SSL_DEBUG_MSG(
1849 1,
Jerry Yuea96ac32023-11-21 17:06:36 +08001850 ("EarlyData: rejected, early_data not allowed in ticket "
1851 "permission bits."));
Jerry Yu4caf3ca2023-11-15 16:13:47 +08001852 return -1;
Jerry Yufceddb32022-12-12 15:30:34 +08001853 }
Jerry Yu985c9672022-12-04 14:06:30 +08001854
Jerry Yu4caf3ca2023-11-15 16:13:47 +08001855 return 0;
Jerry Yuab0da372023-02-08 13:55:24 +08001856}
1857#endif /* MBEDTLS_SSL_EARLY_DATA */
1858
XiaokangQian75fe8c72022-06-15 09:42:45 +00001859/* Update the handshake state machine */
1860
Ronald Cronce7d76e2022-07-08 18:56:49 +02001861MBEDTLS_CHECK_RETURN_CRITICAL
Ronald Cron7b6ee942024-01-12 10:29:55 +01001862static int ssl_tls13_postprocess_client_hello(mbedtls_ssl_context *ssl,
1863 int hrr_required)
XiaokangQian75fe8c72022-06-15 09:42:45 +00001864{
1865 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1866
XiaokangQian7807f9f2022-02-15 10:04:37 +00001867 /*
XiaokangQianfb665a82022-06-15 03:57:21 +00001868 * Server certificate selection
1869 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001870 if (ssl->conf->f_cert_cb && (ret = ssl->conf->f_cert_cb(ssl)) != 0) {
1871 MBEDTLS_SSL_DEBUG_RET(1, "f_cert_cb", ret);
1872 return ret;
XiaokangQianfb665a82022-06-15 03:57:21 +00001873 }
1874#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
1875 ssl->handshake->sni_name = NULL;
1876 ssl->handshake->sni_name_len = 0;
1877#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
XiaokangQian7807f9f2022-02-15 10:04:37 +00001878
Gilles Peskine449bd832023-01-11 14:50:10 +01001879 ret = mbedtls_ssl_tls13_key_schedule_stage_early(ssl);
1880 if (ret != 0) {
1881 MBEDTLS_SSL_DEBUG_RET(1,
1882 "mbedtls_ssl_tls1_3_key_schedule_stage_early", ret);
1883 return ret;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001884 }
1885
Jerry Yuab0da372023-02-08 13:55:24 +08001886#if defined(MBEDTLS_SSL_EARLY_DATA)
Jerry Yu4caf3ca2023-11-15 16:13:47 +08001887 if (ssl->handshake->received_extensions & MBEDTLS_SSL_EXT_MASK(EARLY_DATA)) {
Ronald Cron31e2d832024-02-05 16:45:57 +01001888 ssl->handshake->early_data_accepted =
1889 (!hrr_required) && (ssl_tls13_check_early_data_requirements(ssl) == 0);
Jerry Yu4e9b70e2022-12-04 14:08:02 +08001890
Jerry Yu4caf3ca2023-11-15 16:13:47 +08001891 if (ssl->handshake->early_data_accepted) {
1892 ret = mbedtls_ssl_tls13_compute_early_transform(ssl);
1893 if (ret != 0) {
1894 MBEDTLS_SSL_DEBUG_RET(
1895 1, "mbedtls_ssl_tls13_compute_early_transform", ret);
1896 return ret;
1897 }
1898 } else {
1899 ssl->discard_early_data_record =
1900 hrr_required ?
1901 MBEDTLS_SSL_EARLY_DATA_DISCARD :
1902 MBEDTLS_SSL_EARLY_DATA_TRY_TO_DEPROTECT_AND_DISCARD;
Jerry Yu4e9b70e2022-12-04 14:08:02 +08001903 }
1904 }
Ronald Cron7b6ee942024-01-12 10:29:55 +01001905#else
1906 ((void) hrr_required);
Jerry Yuab0da372023-02-08 13:55:24 +08001907#endif /* MBEDTLS_SSL_EARLY_DATA */
1908
Gilles Peskine449bd832023-01-11 14:50:10 +01001909 return 0;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001910}
1911
1912/*
XiaokangQianed582dd2022-04-13 08:21:05 +00001913 * Main entry point from the state machine; orchestrates the otherfunctions.
1914 */
1915
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001916MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01001917static int ssl_tls13_process_client_hello(mbedtls_ssl_context *ssl)
XiaokangQianed582dd2022-04-13 08:21:05 +00001918{
1919
XiaokangQian08037552022-04-20 07:16:41 +00001920 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine449bd832023-01-11 14:50:10 +01001921 unsigned char *buf = NULL;
XiaokangQianed582dd2022-04-13 08:21:05 +00001922 size_t buflen = 0;
Jerry Yu4ca91402022-05-09 15:50:57 +08001923 int parse_client_hello_ret;
1924
Gilles Peskine449bd832023-01-11 14:50:10 +01001925 MBEDTLS_SSL_DEBUG_MSG(2, ("=> parse client hello"));
XiaokangQianed582dd2022-04-13 08:21:05 +00001926
Gilles Peskine449bd832023-01-11 14:50:10 +01001927 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_tls13_fetch_handshake_msg(
1928 ssl, MBEDTLS_SSL_HS_CLIENT_HELLO,
1929 &buf, &buflen));
XiaokangQianed582dd2022-04-13 08:21:05 +00001930
Gilles Peskine449bd832023-01-11 14:50:10 +01001931 MBEDTLS_SSL_PROC_CHK_NEG(ssl_tls13_parse_client_hello(ssl, buf,
1932 buf + buflen));
Ronald Cron5af4c7f2023-03-07 20:46:59 +01001933 parse_client_hello_ret = ret; /* Store positive return value of
1934 * parse_client_hello,
1935 * as negative error codes are handled
Jerry Yuf41553b2022-05-09 22:20:30 +08001936 * by MBEDTLS_SSL_PROC_CHK_NEG. */
Jerry Yu4ca91402022-05-09 15:50:57 +08001937
Ronald Cronb828c7d2023-04-03 16:37:22 +02001938 /*
Yanray Wang90acdc62023-12-08 10:29:42 +08001939 * Version 1.2 of the protocol has to be used for the handshake.
1940 * If TLS 1.2 is not supported, abort the handshake. Otherwise, set the
Ronald Cronb828c7d2023-04-03 16:37:22 +02001941 * ssl->keep_current_message flag for the ClientHello to be kept and parsed
1942 * as a TLS 1.2 ClientHello. We also change ssl->tls_version to
1943 * MBEDTLS_SSL_VERSION_TLS1_2 thus from now on mbedtls_ssl_handshake_step()
1944 * will dispatch to the TLS 1.2 state machine.
1945 */
Ronald Cron5af4c7f2023-03-07 20:46:59 +01001946 if (SSL_CLIENT_HELLO_TLS1_2 == parse_client_hello_ret) {
Yanray Wangfb0f47b2023-12-04 15:27:28 +08001947 /* Check if server supports TLS 1.2 */
Yanray Wang408ba6f2023-12-08 10:18:03 +08001948 if (!mbedtls_ssl_conf_is_tls12_enabled(ssl->conf)) {
Yanray Wangfb0f47b2023-12-04 15:27:28 +08001949 MBEDTLS_SSL_DEBUG_MSG(
Yanray Wang177e49a2023-12-08 10:51:04 +08001950 1, ("TLS 1.2 not supported."));
Yanray Wangfb0f47b2023-12-04 15:27:28 +08001951 MBEDTLS_SSL_PEND_FATAL_ALERT(
Yanray Wang2bef9172023-12-08 10:21:53 +08001952 MBEDTLS_SSL_ALERT_MSG_PROTOCOL_VERSION,
1953 MBEDTLS_ERR_SSL_BAD_PROTOCOL_VERSION);
1954 return MBEDTLS_ERR_SSL_BAD_PROTOCOL_VERSION;
Yanray Wangfb0f47b2023-12-04 15:27:28 +08001955 }
Ronald Cron5af4c7f2023-03-07 20:46:59 +01001956 ssl->keep_current_message = 1;
1957 ssl->tls_version = MBEDTLS_SSL_VERSION_TLS1_2;
1958 return 0;
1959 }
1960
Ronald Cron7b6ee942024-01-12 10:29:55 +01001961 MBEDTLS_SSL_PROC_CHK(
1962 ssl_tls13_postprocess_client_hello(ssl, parse_client_hello_ret ==
1963 SSL_CLIENT_HELLO_HRR_REQUIRED));
Jerry Yu582dd062022-04-22 21:59:01 +08001964
Ronald Cron5af4c7f2023-03-07 20:46:59 +01001965 if (SSL_CLIENT_HELLO_OK == parse_client_hello_ret) {
Gilles Peskine449bd832023-01-11 14:50:10 +01001966 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_SERVER_HELLO);
1967 } else {
1968 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_HELLO_RETRY_REQUEST);
1969 }
XiaokangQianed582dd2022-04-13 08:21:05 +00001970
1971cleanup:
1972
Gilles Peskine449bd832023-01-11 14:50:10 +01001973 MBEDTLS_SSL_DEBUG_MSG(2, ("<= parse client hello"));
1974 return ret;
XiaokangQianed582dd2022-04-13 08:21:05 +00001975}
1976
1977/*
Jerry Yu1c3e6882022-04-20 21:23:40 +08001978 * Handler for MBEDTLS_SSL_SERVER_HELLO
Jerry Yu5b64ae92022-03-30 17:15:02 +08001979 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001980MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01001981static int ssl_tls13_prepare_server_hello(mbedtls_ssl_context *ssl)
Jerry Yuf4b27e42022-03-30 17:32:21 +08001982{
Jerry Yu637a3f12022-04-20 21:37:58 +08001983 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1984 unsigned char *server_randbytes =
Gilles Peskine449bd832023-01-11 14:50:10 +01001985 ssl->handshake->randbytes + MBEDTLS_CLIENT_HELLO_RANDOM_LEN;
1986 if (ssl->conf->f_rng == NULL) {
1987 MBEDTLS_SSL_DEBUG_MSG(1, ("no RNG provided"));
1988 return MBEDTLS_ERR_SSL_NO_RNG;
Jerry Yuf4b27e42022-03-30 17:32:21 +08001989 }
1990
Gilles Peskine449bd832023-01-11 14:50:10 +01001991 if ((ret = ssl->conf->f_rng(ssl->conf->p_rng, server_randbytes,
1992 MBEDTLS_SERVER_HELLO_RANDOM_LEN)) != 0) {
1993 MBEDTLS_SSL_DEBUG_RET(1, "f_rng", ret);
1994 return ret;
Jerry Yuf4b27e42022-03-30 17:32:21 +08001995 }
1996
Gilles Peskine449bd832023-01-11 14:50:10 +01001997 MBEDTLS_SSL_DEBUG_BUF(3, "server hello, random bytes", server_randbytes,
1998 MBEDTLS_SERVER_HELLO_RANDOM_LEN);
Jerry Yuf4b27e42022-03-30 17:32:21 +08001999
2000#if defined(MBEDTLS_HAVE_TIME)
YxCda609132023-05-22 12:08:12 -07002001 ssl->session_negotiate->start = mbedtls_time(NULL);
Jerry Yuf4b27e42022-03-30 17:32:21 +08002002#endif /* MBEDTLS_HAVE_TIME */
2003
Gilles Peskine449bd832023-01-11 14:50:10 +01002004 return ret;
Jerry Yuf4b27e42022-03-30 17:32:21 +08002005}
2006
Jerry Yu3bf2c642022-03-30 22:02:12 +08002007/*
Jerry Yue74e04a2022-04-21 09:23:16 +08002008 * ssl_tls13_write_server_hello_supported_versions_ext ():
Jerry Yu3bf2c642022-03-30 22:02:12 +08002009 *
2010 * struct {
Jerry Yufb9f54d2022-04-06 10:08:34 +08002011 * ProtocolVersion selected_version;
Jerry Yu3bf2c642022-03-30 22:02:12 +08002012 * } SupportedVersions;
2013 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002014MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yue74e04a2022-04-21 09:23:16 +08002015static int ssl_tls13_write_server_hello_supported_versions_ext(
Gilles Peskine449bd832023-01-11 14:50:10 +01002016 mbedtls_ssl_context *ssl,
2017 unsigned char *buf,
2018 unsigned char *end,
2019 size_t *out_len)
Jerry Yu3bf2c642022-03-30 22:02:12 +08002020{
Jerry Yu3bf2c642022-03-30 22:02:12 +08002021 *out_len = 0;
2022
Gilles Peskine449bd832023-01-11 14:50:10 +01002023 MBEDTLS_SSL_DEBUG_MSG(3, ("server hello, write selected version"));
Jerry Yu3bf2c642022-03-30 22:02:12 +08002024
2025 /* Check if we have space to write the extension:
2026 * - extension_type (2 bytes)
2027 * - extension_data_length (2 bytes)
Jerry Yu349a6132022-04-14 20:52:56 +08002028 * - selected_version (2 bytes)
Jerry Yu3bf2c642022-03-30 22:02:12 +08002029 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002030 MBEDTLS_SSL_CHK_BUF_PTR(buf, end, 6);
Jerry Yu3bf2c642022-03-30 22:02:12 +08002031
Gilles Peskine449bd832023-01-11 14:50:10 +01002032 MBEDTLS_PUT_UINT16_BE(MBEDTLS_TLS_EXT_SUPPORTED_VERSIONS, buf, 0);
Jerry Yu3bf2c642022-03-30 22:02:12 +08002033
Gilles Peskine449bd832023-01-11 14:50:10 +01002034 MBEDTLS_PUT_UINT16_BE(2, buf, 2);
Jerry Yu3bf2c642022-03-30 22:02:12 +08002035
Gilles Peskine449bd832023-01-11 14:50:10 +01002036 mbedtls_ssl_write_version(buf + 4,
2037 ssl->conf->transport,
2038 ssl->tls_version);
Jerry Yu3bf2c642022-03-30 22:02:12 +08002039
Gilles Peskine449bd832023-01-11 14:50:10 +01002040 MBEDTLS_SSL_DEBUG_MSG(3, ("supported version: [%04x]",
2041 ssl->tls_version));
Jerry Yu3bf2c642022-03-30 22:02:12 +08002042
Jerry Yu349a6132022-04-14 20:52:56 +08002043 *out_len = 6;
Jerry Yu3bf2c642022-03-30 22:02:12 +08002044
Jerry Yub95dd362022-11-08 21:19:34 +08002045 mbedtls_ssl_tls13_set_hs_sent_ext_mask(
Gilles Peskine449bd832023-01-11 14:50:10 +01002046 ssl, MBEDTLS_TLS_EXT_SUPPORTED_VERSIONS);
Jerry Yub95dd362022-11-08 21:19:34 +08002047
Gilles Peskine449bd832023-01-11 14:50:10 +01002048 return 0;
Jerry Yu3bf2c642022-03-30 22:02:12 +08002049}
2050
Jerry Yud9436a12022-04-20 22:28:09 +08002051
Jerry Yu3bf2c642022-03-30 22:02:12 +08002052
2053/* Generate and export a single key share. For hybrid KEMs, this can
2054 * be called multiple times with the different components of the hybrid. */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002055MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002056static int ssl_tls13_generate_and_write_key_share(mbedtls_ssl_context *ssl,
2057 uint16_t named_group,
2058 unsigned char *buf,
2059 unsigned char *end,
2060 size_t *out_len)
Jerry Yu3bf2c642022-03-30 22:02:12 +08002061{
2062 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yu955ddd72022-04-22 22:27:33 +08002063
2064 *out_len = 0;
2065
Valerio Settic9ae8622023-07-25 11:23:50 +02002066#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_EPHEMERAL_ENABLED)
Przemek Stekiel29c219c2023-05-31 15:21:04 +02002067 if (mbedtls_ssl_tls13_named_group_is_ecdhe(named_group) ||
Przemek Stekield5f79e72023-06-29 09:08:43 +02002068 mbedtls_ssl_tls13_named_group_is_ffdh(named_group)) {
Przemek Stekiel408569f2023-07-06 11:26:44 +02002069 ret = mbedtls_ssl_tls13_generate_and_write_xxdh_key_exchange(
Gilles Peskine449bd832023-01-11 14:50:10 +01002070 ssl, named_group, buf, end, out_len);
2071 if (ret != 0) {
Jerry Yu89e103c2022-03-30 22:43:29 +08002072 MBEDTLS_SSL_DEBUG_RET(
Przemek Stekiel408569f2023-07-06 11:26:44 +02002073 1, "mbedtls_ssl_tls13_generate_and_write_xxdh_key_exchange",
Gilles Peskine449bd832023-01-11 14:50:10 +01002074 ret);
2075 return ret;
Jerry Yu3bf2c642022-03-30 22:02:12 +08002076 }
Gilles Peskine449bd832023-01-11 14:50:10 +01002077 } else
Valerio Settic9ae8622023-07-25 11:23:50 +02002078#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_EPHEMERAL_ENABLED */
Gilles Peskine449bd832023-01-11 14:50:10 +01002079 if (0 /* Other kinds of KEMs */) {
2080 } else {
Jerry Yu955ddd72022-04-22 22:27:33 +08002081 ((void) ssl);
2082 ((void) named_group);
2083 ((void) buf);
2084 ((void) end);
Jerry Yu3bf2c642022-03-30 22:02:12 +08002085 ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
2086 }
2087
Gilles Peskine449bd832023-01-11 14:50:10 +01002088 return ret;
Jerry Yu3bf2c642022-03-30 22:02:12 +08002089}
2090
2091/*
2092 * ssl_tls13_write_key_share_ext
2093 *
2094 * Structure of key_share extension in ServerHello:
2095 *
Jerry Yu1c3e6882022-04-20 21:23:40 +08002096 * struct {
2097 * NamedGroup group;
2098 * opaque key_exchange<1..2^16-1>;
2099 * } KeyShareEntry;
2100 * struct {
2101 * KeyShareEntry server_share;
2102 * } KeyShareServerHello;
Jerry Yu3bf2c642022-03-30 22:02:12 +08002103 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002104MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002105static int ssl_tls13_write_key_share_ext(mbedtls_ssl_context *ssl,
2106 unsigned char *buf,
2107 unsigned char *end,
2108 size_t *out_len)
Jerry Yu3bf2c642022-03-30 22:02:12 +08002109{
Jerry Yu955ddd72022-04-22 22:27:33 +08002110 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yu3bf2c642022-03-30 22:02:12 +08002111 unsigned char *p = buf;
Jerry Yu57d48412022-04-20 21:50:42 +08002112 uint16_t group = ssl->handshake->offered_group_id;
Jerry Yu3bf2c642022-03-30 22:02:12 +08002113 unsigned char *server_share = buf + 4;
Jerry Yu3bf2c642022-03-30 22:02:12 +08002114 size_t key_exchange_length;
Jerry Yu3bf2c642022-03-30 22:02:12 +08002115
2116 *out_len = 0;
2117
Gilles Peskine449bd832023-01-11 14:50:10 +01002118 MBEDTLS_SSL_DEBUG_MSG(3, ("server hello, adding key share extension"));
Jerry Yu3bf2c642022-03-30 22:02:12 +08002119
Gilles Peskine449bd832023-01-11 14:50:10 +01002120 MBEDTLS_SSL_DEBUG_MSG(2, ("server hello, write selected_group: %s (%04x)",
2121 mbedtls_ssl_named_group_to_str(group),
2122 group));
Jerry Yu58af2332022-09-06 11:19:31 +08002123
Jerry Yu3bf2c642022-03-30 22:02:12 +08002124 /* Check if we have space for header and length fields:
2125 * - extension_type (2 bytes)
2126 * - extension_data_length (2 bytes)
2127 * - group (2 bytes)
2128 * - key_exchange_length (2 bytes)
2129 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002130 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 8);
2131 MBEDTLS_PUT_UINT16_BE(MBEDTLS_TLS_EXT_KEY_SHARE, p, 0);
2132 MBEDTLS_PUT_UINT16_BE(group, server_share, 0);
Jerry Yu3bf2c642022-03-30 22:02:12 +08002133 p += 8;
Jerry Yu57d48412022-04-20 21:50:42 +08002134
Jerry Yu3bf2c642022-03-30 22:02:12 +08002135 /* When we introduce PQC-ECDHE hybrids, we'll want to call this
2136 * function multiple times. */
Jerry Yu955ddd72022-04-22 22:27:33 +08002137 ret = ssl_tls13_generate_and_write_key_share(
Gilles Peskine449bd832023-01-11 14:50:10 +01002138 ssl, group, server_share + 4, end, &key_exchange_length);
2139 if (ret != 0) {
2140 return ret;
2141 }
Jerry Yu3bf2c642022-03-30 22:02:12 +08002142 p += key_exchange_length;
Jerry Yuc1be19f2022-04-23 16:11:39 +08002143
Gilles Peskine449bd832023-01-11 14:50:10 +01002144 MBEDTLS_PUT_UINT16_BE(key_exchange_length, server_share + 2, 0);
Jerry Yu3bf2c642022-03-30 22:02:12 +08002145
Gilles Peskine449bd832023-01-11 14:50:10 +01002146 MBEDTLS_PUT_UINT16_BE(p - server_share, buf, 2);
Jerry Yu3bf2c642022-03-30 22:02:12 +08002147
Jerry Yu57d48412022-04-20 21:50:42 +08002148 *out_len = p - buf;
Jerry Yu955ddd72022-04-22 22:27:33 +08002149
Gilles Peskine449bd832023-01-11 14:50:10 +01002150 mbedtls_ssl_tls13_set_hs_sent_ext_mask(ssl, MBEDTLS_TLS_EXT_KEY_SHARE);
Jerry Yub95dd362022-11-08 21:19:34 +08002151
Gilles Peskine449bd832023-01-11 14:50:10 +01002152 return 0;
Jerry Yu3bf2c642022-03-30 22:02:12 +08002153}
Jerry Yud9436a12022-04-20 22:28:09 +08002154
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002155MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002156static int ssl_tls13_write_hrr_key_share_ext(mbedtls_ssl_context *ssl,
2157 unsigned char *buf,
2158 unsigned char *end,
2159 size_t *out_len)
Jerry Yu23f7a6f2022-04-23 15:16:45 +08002160{
2161 uint16_t selected_group = ssl->handshake->hrr_selected_group;
2162 /* key_share Extension
2163 *
2164 * struct {
2165 * select (Handshake.msg_type) {
2166 * ...
2167 * case hello_retry_request:
2168 * NamedGroup selected_group;
2169 * ...
2170 * };
2171 * } KeyShare;
2172 */
2173
2174 *out_len = 0;
2175
Jerry Yub0ac10b2022-05-05 11:10:08 +08002176 /*
2177 * For a pure PSK key exchange, there is no group to agree upon. The purpose
2178 * of the HRR is then to transmit a cookie to force the client to demonstrate
2179 * reachability at their apparent network address (primarily useful for DTLS).
2180 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002181 if (!mbedtls_ssl_tls13_key_exchange_mode_with_ephemeral(ssl)) {
2182 return 0;
2183 }
Jerry Yu23f7a6f2022-04-23 15:16:45 +08002184
2185 /* We should only send the key_share extension if the client's initial
2186 * key share was not acceptable. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002187 if (ssl->handshake->offered_group_id != 0) {
2188 MBEDTLS_SSL_DEBUG_MSG(4, ("Skip key_share extension in HRR"));
2189 return 0;
Jerry Yu23f7a6f2022-04-23 15:16:45 +08002190 }
2191
Gilles Peskine449bd832023-01-11 14:50:10 +01002192 if (selected_group == 0) {
2193 MBEDTLS_SSL_DEBUG_MSG(1, ("no matching named group found"));
2194 return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
Jerry Yu23f7a6f2022-04-23 15:16:45 +08002195 }
2196
Jerry Yub0ac10b2022-05-05 11:10:08 +08002197 /* Check if we have enough space:
2198 * - extension_type (2 bytes)
2199 * - extension_data_length (2 bytes)
2200 * - selected_group (2 bytes)
2201 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002202 MBEDTLS_SSL_CHK_BUF_PTR(buf, end, 6);
Jerry Yu23f7a6f2022-04-23 15:16:45 +08002203
Gilles Peskine449bd832023-01-11 14:50:10 +01002204 MBEDTLS_PUT_UINT16_BE(MBEDTLS_TLS_EXT_KEY_SHARE, buf, 0);
2205 MBEDTLS_PUT_UINT16_BE(2, buf, 2);
2206 MBEDTLS_PUT_UINT16_BE(selected_group, buf, 4);
Jerry Yu23f7a6f2022-04-23 15:16:45 +08002207
Gilles Peskine449bd832023-01-11 14:50:10 +01002208 MBEDTLS_SSL_DEBUG_MSG(3,
2209 ("HRR selected_group: %s (%x)",
2210 mbedtls_ssl_named_group_to_str(selected_group),
2211 selected_group));
Jerry Yu23f7a6f2022-04-23 15:16:45 +08002212
2213 *out_len = 6;
Jerry Yu23f7a6f2022-04-23 15:16:45 +08002214
Gilles Peskine449bd832023-01-11 14:50:10 +01002215 mbedtls_ssl_tls13_set_hs_sent_ext_mask(ssl, MBEDTLS_TLS_EXT_KEY_SHARE);
Jerry Yub95dd362022-11-08 21:19:34 +08002216
Gilles Peskine449bd832023-01-11 14:50:10 +01002217 return 0;
Jerry Yu23f7a6f2022-04-23 15:16:45 +08002218}
Jerry Yu3bf2c642022-03-30 22:02:12 +08002219
2220/*
2221 * Structure of ServerHello message:
2222 *
2223 * struct {
2224 * ProtocolVersion legacy_version = 0x0303; // TLS v1.2
2225 * Random random;
2226 * opaque legacy_session_id_echo<0..32>;
2227 * CipherSuite cipher_suite;
2228 * uint8 legacy_compression_method = 0;
2229 * Extension extensions<6..2^16-1>;
2230 * } ServerHello;
2231 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002232MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002233static int ssl_tls13_write_server_hello_body(mbedtls_ssl_context *ssl,
2234 unsigned char *buf,
2235 unsigned char *end,
2236 size_t *out_len,
2237 int is_hrr)
Jerry Yu56404d72022-03-30 17:36:13 +08002238{
Jerry Yu955ddd72022-04-22 22:27:33 +08002239 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yu3bf2c642022-03-30 22:02:12 +08002240 unsigned char *p = buf;
Jerry Yud9436a12022-04-20 22:28:09 +08002241 unsigned char *p_extensions_len;
Jerry Yufbe3e642022-04-25 19:31:51 +08002242 size_t output_len;
Jerry Yu3bf2c642022-03-30 22:02:12 +08002243
2244 *out_len = 0;
Jerry Yu50e00e32022-10-31 14:45:01 +08002245 ssl->handshake->sent_extensions = MBEDTLS_SSL_EXT_MASK_NONE;
Jerry Yu3bf2c642022-03-30 22:02:12 +08002246
Jerry Yucfc04b32022-04-21 09:31:58 +08002247 /* ...
2248 * ProtocolVersion legacy_version = 0x0303; // TLS 1.2
2249 * ...
2250 * with ProtocolVersion defined as:
2251 * uint16 ProtocolVersion;
Jerry Yu3bf2c642022-03-30 22:02:12 +08002252 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002253 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 2);
2254 MBEDTLS_PUT_UINT16_BE(0x0303, p, 0);
Jerry Yu3bf2c642022-03-30 22:02:12 +08002255 p += 2;
2256
Jerry Yu1c3e6882022-04-20 21:23:40 +08002257 /* ...
2258 * Random random;
2259 * ...
Jerry Yucfc04b32022-04-21 09:31:58 +08002260 * with Random defined as:
2261 * opaque Random[MBEDTLS_SERVER_HELLO_RANDOM_LEN];
Jerry Yu1c3e6882022-04-20 21:23:40 +08002262 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002263 MBEDTLS_SSL_CHK_BUF_PTR(p, end, MBEDTLS_SERVER_HELLO_RANDOM_LEN);
2264 if (is_hrr) {
2265 memcpy(p, mbedtls_ssl_tls13_hello_retry_request_magic,
2266 MBEDTLS_SERVER_HELLO_RANDOM_LEN);
2267 } else {
2268 memcpy(p, &ssl->handshake->randbytes[MBEDTLS_CLIENT_HELLO_RANDOM_LEN],
2269 MBEDTLS_SERVER_HELLO_RANDOM_LEN);
Jerry Yu23f7a6f2022-04-23 15:16:45 +08002270 }
Gilles Peskine449bd832023-01-11 14:50:10 +01002271 MBEDTLS_SSL_DEBUG_BUF(3, "server hello, random bytes",
2272 p, MBEDTLS_SERVER_HELLO_RANDOM_LEN);
Jerry Yu3bf2c642022-03-30 22:02:12 +08002273 p += MBEDTLS_SERVER_HELLO_RANDOM_LEN;
2274
Jerry Yucfc04b32022-04-21 09:31:58 +08002275 /* ...
2276 * opaque legacy_session_id_echo<0..32>;
2277 * ...
Jerry Yu3bf2c642022-03-30 22:02:12 +08002278 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002279 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 1 + ssl->session_negotiate->id_len);
2280 *p++ = (unsigned char) ssl->session_negotiate->id_len;
2281 if (ssl->session_negotiate->id_len > 0) {
2282 memcpy(p, &ssl->session_negotiate->id[0],
2283 ssl->session_negotiate->id_len);
Jerry Yu3bf2c642022-03-30 22:02:12 +08002284 p += ssl->session_negotiate->id_len;
Jerry Yu955ddd72022-04-22 22:27:33 +08002285
Gilles Peskine449bd832023-01-11 14:50:10 +01002286 MBEDTLS_SSL_DEBUG_BUF(3, "session id", ssl->session_negotiate->id,
2287 ssl->session_negotiate->id_len);
Jerry Yu3bf2c642022-03-30 22:02:12 +08002288 }
2289
Jerry Yucfc04b32022-04-21 09:31:58 +08002290 /* ...
2291 * CipherSuite cipher_suite;
2292 * ...
2293 * with CipherSuite defined as:
2294 * uint8 CipherSuite[2];
Jerry Yu3bf2c642022-03-30 22:02:12 +08002295 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002296 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 2);
2297 MBEDTLS_PUT_UINT16_BE(ssl->session_negotiate->ciphersuite, p, 0);
Jerry Yu3bf2c642022-03-30 22:02:12 +08002298 p += 2;
Gilles Peskine449bd832023-01-11 14:50:10 +01002299 MBEDTLS_SSL_DEBUG_MSG(3,
2300 ("server hello, chosen ciphersuite: %s ( id=%d )",
2301 mbedtls_ssl_get_ciphersuite_name(
2302 ssl->session_negotiate->ciphersuite),
2303 ssl->session_negotiate->ciphersuite));
Jerry Yu3bf2c642022-03-30 22:02:12 +08002304
Jerry Yucfc04b32022-04-21 09:31:58 +08002305 /* ...
2306 * uint8 legacy_compression_method = 0;
2307 * ...
2308 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002309 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 1);
Thomas Daubney31e03a82022-07-25 15:59:25 +01002310 *p++ = MBEDTLS_SSL_COMPRESS_NULL;
Jerry Yu3bf2c642022-03-30 22:02:12 +08002311
Jerry Yucfc04b32022-04-21 09:31:58 +08002312 /* ...
2313 * Extension extensions<6..2^16-1>;
2314 * ...
2315 * struct {
2316 * ExtensionType extension_type; (2 bytes)
2317 * opaque extension_data<0..2^16-1>;
2318 * } Extension;
2319 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002320 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 2);
Jerry Yud9436a12022-04-20 22:28:09 +08002321 p_extensions_len = p;
Jerry Yu3bf2c642022-03-30 22:02:12 +08002322 p += 2;
2323
Gilles Peskine449bd832023-01-11 14:50:10 +01002324 if ((ret = ssl_tls13_write_server_hello_supported_versions_ext(
2325 ssl, p, end, &output_len)) != 0) {
Jerry Yu955ddd72022-04-22 22:27:33 +08002326 MBEDTLS_SSL_DEBUG_RET(
Gilles Peskine449bd832023-01-11 14:50:10 +01002327 1, "ssl_tls13_write_server_hello_supported_versions_ext", ret);
2328 return ret;
Jerry Yu3bf2c642022-03-30 22:02:12 +08002329 }
2330 p += output_len;
2331
Gilles Peskine449bd832023-01-11 14:50:10 +01002332 if (mbedtls_ssl_tls13_key_exchange_mode_with_ephemeral(ssl)) {
2333 if (is_hrr) {
2334 ret = ssl_tls13_write_hrr_key_share_ext(ssl, p, end, &output_len);
2335 } else {
2336 ret = ssl_tls13_write_key_share_ext(ssl, p, end, &output_len);
2337 }
2338 if (ret != 0) {
2339 return ret;
2340 }
Jerry Yu3bf2c642022-03-30 22:02:12 +08002341 p += output_len;
2342 }
Jerry Yu3bf2c642022-03-30 22:02:12 +08002343
Ronald Cron41a443a2022-10-04 16:38:25 +02002344#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01002345 if (!is_hrr && mbedtls_ssl_tls13_key_exchange_mode_with_psk(ssl)) {
2346 ret = ssl_tls13_write_server_pre_shared_key_ext(ssl, p, end, &output_len);
2347 if (ret != 0) {
2348 MBEDTLS_SSL_DEBUG_RET(1, "ssl_tls13_write_server_pre_shared_key_ext",
2349 ret);
2350 return ret;
Jerry Yu032b15ce2022-07-11 06:10:03 +00002351 }
2352 p += output_len;
2353 }
Ronald Cron41a443a2022-10-04 16:38:25 +02002354#endif
Jerry Yu032b15ce2022-07-11 06:10:03 +00002355
Gilles Peskine449bd832023-01-11 14:50:10 +01002356 MBEDTLS_PUT_UINT16_BE(p - p_extensions_len - 2, p_extensions_len, 0);
Jerry Yu3bf2c642022-03-30 22:02:12 +08002357
Gilles Peskine449bd832023-01-11 14:50:10 +01002358 MBEDTLS_SSL_DEBUG_BUF(4, "server hello extensions",
2359 p_extensions_len, p - p_extensions_len);
Jerry Yu3bf2c642022-03-30 22:02:12 +08002360
Jerry Yud9436a12022-04-20 22:28:09 +08002361 *out_len = p - buf;
Jerry Yu3bf2c642022-03-30 22:02:12 +08002362
Gilles Peskine449bd832023-01-11 14:50:10 +01002363 MBEDTLS_SSL_DEBUG_BUF(3, "server hello", buf, *out_len);
Jerry Yu3bf2c642022-03-30 22:02:12 +08002364
Jerry Yu7de2ff02022-11-08 21:43:46 +08002365 MBEDTLS_SSL_PRINT_EXTS(
Jerry Yu4b8f2f72022-10-31 13:31:22 +08002366 3, is_hrr ? MBEDTLS_SSL_TLS1_3_HS_HELLO_RETRY_REQUEST :
Gilles Peskine449bd832023-01-11 14:50:10 +01002367 MBEDTLS_SSL_HS_SERVER_HELLO,
2368 ssl->handshake->sent_extensions);
Jerry Yu4b8f2f72022-10-31 13:31:22 +08002369
Gilles Peskine449bd832023-01-11 14:50:10 +01002370 return ret;
Jerry Yu56404d72022-03-30 17:36:13 +08002371}
2372
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002373MBEDTLS_CHECK_RETURN_CRITICAL
Xiaokang Qian934ce6f2023-02-06 10:23:04 +00002374static int ssl_tls13_finalize_server_hello(mbedtls_ssl_context *ssl)
Jerry Yue110d252022-05-05 10:19:22 +08002375{
2376 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine449bd832023-01-11 14:50:10 +01002377 ret = mbedtls_ssl_tls13_compute_handshake_transform(ssl);
2378 if (ret != 0) {
2379 MBEDTLS_SSL_DEBUG_RET(1,
2380 "mbedtls_ssl_tls13_compute_handshake_transform",
2381 ret);
2382 return ret;
Jerry Yue110d252022-05-05 10:19:22 +08002383 }
2384
Gilles Peskine449bd832023-01-11 14:50:10 +01002385 return ret;
Jerry Yue110d252022-05-05 10:19:22 +08002386}
2387
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002388MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002389static int ssl_tls13_write_server_hello(mbedtls_ssl_context *ssl)
Jerry Yu5b64ae92022-03-30 17:15:02 +08002390{
Jerry Yu637a3f12022-04-20 21:37:58 +08002391 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yuf4b27e42022-03-30 17:32:21 +08002392 unsigned char *buf;
2393 size_t buf_len, msg_len;
2394
Gilles Peskine449bd832023-01-11 14:50:10 +01002395 MBEDTLS_SSL_DEBUG_MSG(2, ("=> write server hello"));
Jerry Yuf4b27e42022-03-30 17:32:21 +08002396
Gilles Peskine449bd832023-01-11 14:50:10 +01002397 MBEDTLS_SSL_PROC_CHK(ssl_tls13_prepare_server_hello(ssl));
Jerry Yuf4b27e42022-03-30 17:32:21 +08002398
Xiaokang Qian9f1747b2023-03-30 02:50:04 +00002399 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_start_handshake_msg(
2400 ssl, MBEDTLS_SSL_HS_SERVER_HELLO, &buf, &buf_len));
Jerry Yuf4b27e42022-03-30 17:32:21 +08002401
Gilles Peskine449bd832023-01-11 14:50:10 +01002402 MBEDTLS_SSL_PROC_CHK(ssl_tls13_write_server_hello_body(ssl, buf,
2403 buf + buf_len,
2404 &msg_len,
2405 0));
Jerry Yuf4b27e42022-03-30 17:32:21 +08002406
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +01002407 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_add_hs_msg_to_checksum(
Manuel Pégourié-Gonnard43cc1272023-02-06 11:48:19 +01002408 ssl, MBEDTLS_SSL_HS_SERVER_HELLO, buf, msg_len));
Jerry Yuf4b27e42022-03-30 17:32:21 +08002409
Gilles Peskine449bd832023-01-11 14:50:10 +01002410 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_finish_handshake_msg(
2411 ssl, buf_len, msg_len));
Jerry Yu637a3f12022-04-20 21:37:58 +08002412
Xiaokang Qian934ce6f2023-02-06 10:23:04 +00002413 MBEDTLS_SSL_PROC_CHK(ssl_tls13_finalize_server_hello(ssl));
Jerry Yue110d252022-05-05 10:19:22 +08002414
Gabor Mezei7b39bf12022-05-24 16:04:14 +02002415#if defined(MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE)
2416 /* The server sends a dummy change_cipher_spec record immediately
2417 * after its first handshake message. This may either be after
2418 * a ServerHello or a HelloRetryRequest.
2419 */
Gabor Mezei96ae9262022-06-28 11:45:18 +02002420 mbedtls_ssl_handshake_set_state(
Gilles Peskine449bd832023-01-11 14:50:10 +01002421 ssl, MBEDTLS_SSL_SERVER_CCS_AFTER_SERVER_HELLO);
Gabor Mezei7b39bf12022-05-24 16:04:14 +02002422#else
Gilles Peskine449bd832023-01-11 14:50:10 +01002423 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_ENCRYPTED_EXTENSIONS);
Gabor Mezei7b39bf12022-05-24 16:04:14 +02002424#endif /* MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE */
Jerry Yue110d252022-05-05 10:19:22 +08002425
Jerry Yuf4b27e42022-03-30 17:32:21 +08002426cleanup:
2427
Gilles Peskine449bd832023-01-11 14:50:10 +01002428 MBEDTLS_SSL_DEBUG_MSG(2, ("<= write server hello"));
2429 return ret;
Jerry Yu5b64ae92022-03-30 17:15:02 +08002430}
2431
Jerry Yu23d1a252022-05-12 18:08:59 +08002432
2433/*
2434 * Handler for MBEDTLS_SSL_HELLO_RETRY_REQUEST
2435 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002436MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002437static int ssl_tls13_prepare_hello_retry_request(mbedtls_ssl_context *ssl)
Jerry Yu23d1a252022-05-12 18:08:59 +08002438{
2439 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Ronald Cron5fbd2702024-02-14 10:03:36 +01002440 if (ssl->handshake->hello_retry_request_flag) {
Gilles Peskine449bd832023-01-11 14:50:10 +01002441 MBEDTLS_SSL_DEBUG_MSG(1, ("Too many HRRs"));
2442 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE,
2443 MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE);
2444 return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
Jerry Yu23d1a252022-05-12 18:08:59 +08002445 }
2446
2447 /*
2448 * Create stateless transcript hash for HRR
2449 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002450 MBEDTLS_SSL_DEBUG_MSG(4, ("Reset transcript for HRR"));
2451 ret = mbedtls_ssl_reset_transcript_for_hrr(ssl);
2452 if (ret != 0) {
2453 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_reset_transcript_for_hrr", ret);
2454 return ret;
Jerry Yu23d1a252022-05-12 18:08:59 +08002455 }
Gilles Peskine449bd832023-01-11 14:50:10 +01002456 mbedtls_ssl_session_reset_msg_layer(ssl, 0);
Jerry Yu23d1a252022-05-12 18:08:59 +08002457
Gilles Peskine449bd832023-01-11 14:50:10 +01002458 return 0;
Jerry Yu23d1a252022-05-12 18:08:59 +08002459}
2460
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002461MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002462static int ssl_tls13_write_hello_retry_request(mbedtls_ssl_context *ssl)
Jerry Yu23d1a252022-05-12 18:08:59 +08002463{
2464 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
2465 unsigned char *buf;
2466 size_t buf_len, msg_len;
2467
Gilles Peskine449bd832023-01-11 14:50:10 +01002468 MBEDTLS_SSL_DEBUG_MSG(2, ("=> write hello retry request"));
Jerry Yu23d1a252022-05-12 18:08:59 +08002469
Gilles Peskine449bd832023-01-11 14:50:10 +01002470 MBEDTLS_SSL_PROC_CHK(ssl_tls13_prepare_hello_retry_request(ssl));
Jerry Yu23d1a252022-05-12 18:08:59 +08002471
Gilles Peskine449bd832023-01-11 14:50:10 +01002472 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_start_handshake_msg(
2473 ssl, MBEDTLS_SSL_HS_SERVER_HELLO,
2474 &buf, &buf_len));
Jerry Yu23d1a252022-05-12 18:08:59 +08002475
Gilles Peskine449bd832023-01-11 14:50:10 +01002476 MBEDTLS_SSL_PROC_CHK(ssl_tls13_write_server_hello_body(ssl, buf,
2477 buf + buf_len,
2478 &msg_len,
2479 1));
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +01002480 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_add_hs_msg_to_checksum(
Manuel Pégourié-Gonnard43cc1272023-02-06 11:48:19 +01002481 ssl, MBEDTLS_SSL_HS_SERVER_HELLO, buf, msg_len));
Jerry Yu23d1a252022-05-12 18:08:59 +08002482
2483
Gilles Peskine449bd832023-01-11 14:50:10 +01002484 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_finish_handshake_msg(ssl, buf_len,
2485 msg_len));
Jerry Yu23d1a252022-05-12 18:08:59 +08002486
Ronald Cron5fbd2702024-02-14 10:03:36 +01002487 ssl->handshake->hello_retry_request_flag = 1;
Jerry Yu23d1a252022-05-12 18:08:59 +08002488
Gabor Mezei7b39bf12022-05-24 16:04:14 +02002489#if defined(MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE)
2490 /* The server sends a dummy change_cipher_spec record immediately
2491 * after its first handshake message. This may either be after
2492 * a ServerHello or a HelloRetryRequest.
2493 */
Gabor Mezei96ae9262022-06-28 11:45:18 +02002494 mbedtls_ssl_handshake_set_state(
Gilles Peskine449bd832023-01-11 14:50:10 +01002495 ssl, MBEDTLS_SSL_SERVER_CCS_AFTER_HELLO_RETRY_REQUEST);
Gabor Mezei7b39bf12022-05-24 16:04:14 +02002496#else
Gilles Peskine449bd832023-01-11 14:50:10 +01002497 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_CLIENT_HELLO);
Gabor Mezei7b39bf12022-05-24 16:04:14 +02002498#endif /* MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE */
Jerry Yu23d1a252022-05-12 18:08:59 +08002499
2500cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +01002501 MBEDTLS_SSL_DEBUG_MSG(2, ("<= write hello retry request"));
2502 return ret;
Jerry Yu23d1a252022-05-12 18:08:59 +08002503}
2504
Jerry Yu5b64ae92022-03-30 17:15:02 +08002505/*
Jerry Yu4d3841a2022-04-16 12:37:19 +08002506 * Handler for MBEDTLS_SSL_ENCRYPTED_EXTENSIONS
2507 */
Jerry Yu4d3841a2022-04-16 12:37:19 +08002508
2509/*
2510 * struct {
2511 * Extension extensions<0..2 ^ 16 - 1>;
2512 * } EncryptedExtensions;
2513 *
2514 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002515MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002516static int ssl_tls13_write_encrypted_extensions_body(mbedtls_ssl_context *ssl,
2517 unsigned char *buf,
2518 unsigned char *end,
2519 size_t *out_len)
Jerry Yu4d3841a2022-04-16 12:37:19 +08002520{
XiaokangQianacb39922022-06-17 10:18:48 +00002521 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yu4d3841a2022-04-16 12:37:19 +08002522 unsigned char *p = buf;
2523 size_t extensions_len = 0;
Jerry Yu8937eb42022-05-03 12:12:14 +08002524 unsigned char *p_extensions_len;
XiaokangQianacb39922022-06-17 10:18:48 +00002525 size_t output_len;
Jerry Yu9da5e5a2022-05-03 15:46:09 +08002526
Jerry Yu4d3841a2022-04-16 12:37:19 +08002527 *out_len = 0;
2528
Gilles Peskine449bd832023-01-11 14:50:10 +01002529 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 2);
Jerry Yu8937eb42022-05-03 12:12:14 +08002530 p_extensions_len = p;
Jerry Yu4d3841a2022-04-16 12:37:19 +08002531 p += 2;
2532
Jerry Yu8937eb42022-05-03 12:12:14 +08002533 ((void) ssl);
XiaokangQianacb39922022-06-17 10:18:48 +00002534 ((void) ret);
2535 ((void) output_len);
2536
2537#if defined(MBEDTLS_SSL_ALPN)
Gilles Peskine449bd832023-01-11 14:50:10 +01002538 ret = mbedtls_ssl_write_alpn_ext(ssl, p, end, &output_len);
2539 if (ret != 0) {
2540 return ret;
2541 }
XiaokangQian95d5f542022-06-24 02:29:26 +00002542 p += output_len;
XiaokangQianacb39922022-06-17 10:18:48 +00002543#endif /* MBEDTLS_SSL_ALPN */
Jerry Yu4d3841a2022-04-16 12:37:19 +08002544
Jerry Yu71c14f12022-12-12 11:10:35 +08002545#if defined(MBEDTLS_SSL_EARLY_DATA)
Ronald Cron78a38f62024-02-01 18:30:31 +01002546 if (ssl->handshake->early_data_accepted) {
Jerry Yu52335392023-11-23 18:06:06 +08002547 ret = mbedtls_ssl_tls13_write_early_data_ext(
Jerry Yuc59c5862023-12-05 10:40:49 +08002548 ssl, 0, p, end, &output_len);
Jerry Yu71c14f12022-12-12 11:10:35 +08002549 if (ret != 0) {
2550 return ret;
2551 }
2552 p += output_len;
2553 }
2554#endif /* MBEDTLS_SSL_EARLY_DATA */
2555
Waleed Elmelegy47d29462024-01-03 17:31:52 +00002556#if defined(MBEDTLS_SSL_RECORD_SIZE_LIMIT)
Waleed Elmelegyfbe42742024-01-05 18:11:10 +00002557 if (ssl->handshake->received_extensions & MBEDTLS_SSL_EXT_MASK(RECORD_SIZE_LIMIT)) {
Waleed Elmelegyd2fc90e2024-01-04 18:04:53 +00002558 ret = mbedtls_ssl_tls13_write_record_size_limit_ext(
2559 ssl, p, end, &output_len);
2560 if (ret != 0) {
2561 return ret;
2562 }
2563 p += output_len;
Waleed Elmelegy47d29462024-01-03 17:31:52 +00002564 }
Waleed Elmelegy47d29462024-01-03 17:31:52 +00002565#endif
2566
Gilles Peskine449bd832023-01-11 14:50:10 +01002567 extensions_len = (p - p_extensions_len) - 2;
2568 MBEDTLS_PUT_UINT16_BE(extensions_len, p_extensions_len, 0);
Jerry Yu8937eb42022-05-03 12:12:14 +08002569
2570 *out_len = p - buf;
Jerry Yu4d3841a2022-04-16 12:37:19 +08002571
Gilles Peskine449bd832023-01-11 14:50:10 +01002572 MBEDTLS_SSL_DEBUG_BUF(4, "encrypted extensions", buf, *out_len);
Jerry Yu4d3841a2022-04-16 12:37:19 +08002573
Jerry Yu7de2ff02022-11-08 21:43:46 +08002574 MBEDTLS_SSL_PRINT_EXTS(
Gilles Peskine449bd832023-01-11 14:50:10 +01002575 3, MBEDTLS_SSL_HS_ENCRYPTED_EXTENSIONS, ssl->handshake->sent_extensions);
Jerry Yu4b8f2f72022-10-31 13:31:22 +08002576
Gilles Peskine449bd832023-01-11 14:50:10 +01002577 return 0;
Jerry Yu4d3841a2022-04-16 12:37:19 +08002578}
2579
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002580MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002581static int ssl_tls13_write_encrypted_extensions(mbedtls_ssl_context *ssl)
Jerry Yu4d3841a2022-04-16 12:37:19 +08002582{
2583 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
2584 unsigned char *buf;
Jerry Yu39730a72022-05-03 12:14:04 +08002585 size_t buf_len, msg_len;
Jerry Yu4d3841a2022-04-16 12:37:19 +08002586
Gilles Peskine449bd832023-01-11 14:50:10 +01002587 mbedtls_ssl_set_outbound_transform(ssl,
2588 ssl->handshake->transform_handshake);
Gabor Mezei54719122022-06-28 11:34:56 +02002589 MBEDTLS_SSL_DEBUG_MSG(
Gilles Peskine449bd832023-01-11 14:50:10 +01002590 3, ("switching to handshake transform for outbound data"));
Gabor Mezei54719122022-06-28 11:34:56 +02002591
Gilles Peskine449bd832023-01-11 14:50:10 +01002592 MBEDTLS_SSL_DEBUG_MSG(2, ("=> write encrypted extensions"));
Jerry Yu4d3841a2022-04-16 12:37:19 +08002593
Xiaokang Qian9f1747b2023-03-30 02:50:04 +00002594 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_start_handshake_msg(
2595 ssl, MBEDTLS_SSL_HS_ENCRYPTED_EXTENSIONS,
2596 &buf, &buf_len));
Jerry Yu4d3841a2022-04-16 12:37:19 +08002597
Gilles Peskine449bd832023-01-11 14:50:10 +01002598 MBEDTLS_SSL_PROC_CHK(ssl_tls13_write_encrypted_extensions_body(
2599 ssl, buf, buf + buf_len, &msg_len));
Jerry Yu4d3841a2022-04-16 12:37:19 +08002600
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +01002601 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_add_hs_msg_to_checksum(
Xiaokang Qian9f1747b2023-03-30 02:50:04 +00002602 ssl, MBEDTLS_SSL_HS_ENCRYPTED_EXTENSIONS,
2603 buf, msg_len));
Jerry Yu4d3841a2022-04-16 12:37:19 +08002604
Gilles Peskine449bd832023-01-11 14:50:10 +01002605 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_finish_handshake_msg(
2606 ssl, buf_len, msg_len));
Jerry Yu4d3841a2022-04-16 12:37:19 +08002607
Ronald Cron928cbd32022-10-04 16:14:26 +02002608#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01002609 if (mbedtls_ssl_tls13_key_exchange_mode_with_psk(ssl)) {
2610 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_SERVER_FINISHED);
2611 } else {
2612 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_CERTIFICATE_REQUEST);
2613 }
Jerry Yu8937eb42022-05-03 12:12:14 +08002614#else
Gilles Peskine449bd832023-01-11 14:50:10 +01002615 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_SERVER_FINISHED);
Jerry Yu8937eb42022-05-03 12:12:14 +08002616#endif
2617
Jerry Yu4d3841a2022-04-16 12:37:19 +08002618cleanup:
2619
Gilles Peskine449bd832023-01-11 14:50:10 +01002620 MBEDTLS_SSL_DEBUG_MSG(2, ("<= write encrypted extensions"));
2621 return ret;
Jerry Yu4d3841a2022-04-16 12:37:19 +08002622}
2623
Ronald Cron928cbd32022-10-04 16:14:26 +02002624#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED)
XiaokangQiana987e1d2022-05-07 01:25:58 +00002625#define SSL_CERTIFICATE_REQUEST_SEND_REQUEST 0
2626#define SSL_CERTIFICATE_REQUEST_SKIP 1
2627/* Coordination:
2628 * Check whether a CertificateRequest message should be written.
2629 * Returns a negative code on failure, or
2630 * - SSL_CERTIFICATE_REQUEST_SEND_REQUEST
2631 * - SSL_CERTIFICATE_REQUEST_SKIP
2632 * indicating if the writing of the CertificateRequest
2633 * should be skipped or not.
2634 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002635MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002636static int ssl_tls13_certificate_request_coordinate(mbedtls_ssl_context *ssl)
XiaokangQiana987e1d2022-05-07 01:25:58 +00002637{
2638 int authmode;
2639
XiaokangQian40a35232022-05-07 09:02:40 +00002640#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Gilles Peskine449bd832023-01-11 14:50:10 +01002641 if (ssl->handshake->sni_authmode != MBEDTLS_SSL_VERIFY_UNSET) {
XiaokangQian40a35232022-05-07 09:02:40 +00002642 authmode = ssl->handshake->sni_authmode;
Gilles Peskine449bd832023-01-11 14:50:10 +01002643 } else
XiaokangQian40a35232022-05-07 09:02:40 +00002644#endif
XiaokangQiana987e1d2022-05-07 01:25:58 +00002645 authmode = ssl->conf->authmode;
2646
Gilles Peskine449bd832023-01-11 14:50:10 +01002647 if (authmode == MBEDTLS_SSL_VERIFY_NONE) {
Ronald Croneac00ad2022-09-13 10:16:31 +02002648 ssl->session_negotiate->verify_result = MBEDTLS_X509_BADCERT_SKIP_VERIFY;
Gilles Peskine449bd832023-01-11 14:50:10 +01002649 return SSL_CERTIFICATE_REQUEST_SKIP;
Ronald Croneac00ad2022-09-13 10:16:31 +02002650 }
XiaokangQiana987e1d2022-05-07 01:25:58 +00002651
XiaokangQianc3017f62022-05-13 05:55:41 +00002652 ssl->handshake->certificate_request_sent = 1;
XiaokangQian189ded22022-05-10 08:12:17 +00002653
Gilles Peskine449bd832023-01-11 14:50:10 +01002654 return SSL_CERTIFICATE_REQUEST_SEND_REQUEST;
XiaokangQiana987e1d2022-05-07 01:25:58 +00002655}
2656
XiaokangQiancec9ae62022-05-06 07:28:50 +00002657/*
2658 * struct {
2659 * opaque certificate_request_context<0..2^8-1>;
2660 * Extension extensions<2..2^16-1>;
2661 * } CertificateRequest;
2662 *
2663 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002664MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002665static int ssl_tls13_write_certificate_request_body(mbedtls_ssl_context *ssl,
2666 unsigned char *buf,
2667 const unsigned char *end,
2668 size_t *out_len)
XiaokangQiancec9ae62022-05-06 07:28:50 +00002669{
2670 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
2671 unsigned char *p = buf;
XiaokangQianec6efb92022-05-06 09:53:10 +00002672 size_t output_len = 0;
XiaokangQiancec9ae62022-05-06 07:28:50 +00002673 unsigned char *p_extensions_len;
2674
2675 *out_len = 0;
2676
2677 /* Check if we have enough space:
2678 * - certificate_request_context (1 byte)
2679 * - extensions length (2 bytes)
2680 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002681 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 3);
XiaokangQiancec9ae62022-05-06 07:28:50 +00002682
2683 /*
2684 * Write certificate_request_context
2685 */
2686 /*
2687 * We use a zero length context for the normal handshake
2688 * messages. For post-authentication handshake messages
2689 * this request context would be set to a non-zero value.
2690 */
2691 *p++ = 0x0;
2692
2693 /*
2694 * Write extensions
2695 */
2696 /* The extensions must contain the signature_algorithms. */
2697 p_extensions_len = p;
2698 p += 2;
Gilles Peskine449bd832023-01-11 14:50:10 +01002699 ret = mbedtls_ssl_write_sig_alg_ext(ssl, p, end, &output_len);
2700 if (ret != 0) {
2701 return ret;
2702 }
XiaokangQiancec9ae62022-05-06 07:28:50 +00002703
XiaokangQianec6efb92022-05-06 09:53:10 +00002704 p += output_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01002705 MBEDTLS_PUT_UINT16_BE(p - p_extensions_len - 2, p_extensions_len, 0);
XiaokangQiancec9ae62022-05-06 07:28:50 +00002706
2707 *out_len = p - buf;
2708
Jerry Yu7de2ff02022-11-08 21:43:46 +08002709 MBEDTLS_SSL_PRINT_EXTS(
Gilles Peskine449bd832023-01-11 14:50:10 +01002710 3, MBEDTLS_SSL_HS_CERTIFICATE_REQUEST, ssl->handshake->sent_extensions);
Jerry Yu4b8f2f72022-10-31 13:31:22 +08002711
Gilles Peskine449bd832023-01-11 14:50:10 +01002712 return 0;
XiaokangQiancec9ae62022-05-06 07:28:50 +00002713}
2714
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002715MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002716static int ssl_tls13_write_certificate_request(mbedtls_ssl_context *ssl)
XiaokangQiancec9ae62022-05-06 07:28:50 +00002717{
2718 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
2719
Gilles Peskine449bd832023-01-11 14:50:10 +01002720 MBEDTLS_SSL_DEBUG_MSG(2, ("=> write certificate request"));
XiaokangQiancec9ae62022-05-06 07:28:50 +00002721
Gilles Peskine449bd832023-01-11 14:50:10 +01002722 MBEDTLS_SSL_PROC_CHK_NEG(ssl_tls13_certificate_request_coordinate(ssl));
XiaokangQiancec9ae62022-05-06 07:28:50 +00002723
Gilles Peskine449bd832023-01-11 14:50:10 +01002724 if (ret == SSL_CERTIFICATE_REQUEST_SEND_REQUEST) {
XiaokangQiancec9ae62022-05-06 07:28:50 +00002725 unsigned char *buf;
2726 size_t buf_len, msg_len;
2727
Xiaokang Qian9f1747b2023-03-30 02:50:04 +00002728 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_start_handshake_msg(
2729 ssl, MBEDTLS_SSL_HS_CERTIFICATE_REQUEST,
2730 &buf, &buf_len));
XiaokangQiancec9ae62022-05-06 07:28:50 +00002731
Gilles Peskine449bd832023-01-11 14:50:10 +01002732 MBEDTLS_SSL_PROC_CHK(ssl_tls13_write_certificate_request_body(
2733 ssl, buf, buf + buf_len, &msg_len));
XiaokangQiancec9ae62022-05-06 07:28:50 +00002734
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +01002735 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_add_hs_msg_to_checksum(
Xiaokang Qian9f1747b2023-03-30 02:50:04 +00002736 ssl, MBEDTLS_SSL_HS_CERTIFICATE_REQUEST,
2737 buf, msg_len));
XiaokangQiancec9ae62022-05-06 07:28:50 +00002738
Gilles Peskine449bd832023-01-11 14:50:10 +01002739 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_finish_handshake_msg(
2740 ssl, buf_len, msg_len));
2741 } else if (ret == SSL_CERTIFICATE_REQUEST_SKIP) {
2742 MBEDTLS_SSL_DEBUG_MSG(2, ("<= skip write certificate request"));
XiaokangQiancec9ae62022-05-06 07:28:50 +00002743 ret = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01002744 } else {
2745 MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
XiaokangQiancec9ae62022-05-06 07:28:50 +00002746 ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
2747 goto cleanup;
2748 }
2749
Gilles Peskine449bd832023-01-11 14:50:10 +01002750 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_SERVER_CERTIFICATE);
XiaokangQiancec9ae62022-05-06 07:28:50 +00002751cleanup:
2752
Gilles Peskine449bd832023-01-11 14:50:10 +01002753 MBEDTLS_SSL_DEBUG_MSG(2, ("<= write certificate request"));
2754 return ret;
XiaokangQiancec9ae62022-05-06 07:28:50 +00002755}
XiaokangQiancec9ae62022-05-06 07:28:50 +00002756
Jerry Yu4d3841a2022-04-16 12:37:19 +08002757/*
Jerry Yuc6e6dbf2022-04-16 19:42:57 +08002758 * Handler for MBEDTLS_SSL_SERVER_CERTIFICATE
Jerry Yu83da34e2022-04-16 13:59:52 +08002759 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002760MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002761static int ssl_tls13_write_server_certificate(mbedtls_ssl_context *ssl)
Jerry Yu83da34e2022-04-16 13:59:52 +08002762{
Jerry Yu5a26f302022-05-10 20:46:40 +08002763 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
XiaokangQianfb665a82022-06-15 03:57:21 +00002764
2765#if defined(MBEDTLS_X509_CRT_PARSE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01002766 if ((ssl_tls13_pick_key_cert(ssl) != 0) ||
2767 mbedtls_ssl_own_cert(ssl) == NULL) {
2768 MBEDTLS_SSL_DEBUG_MSG(2, ("No certificate available."));
2769 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE,
2770 MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE);
2771 return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
Jerry Yu5a26f302022-05-10 20:46:40 +08002772 }
XiaokangQianfb665a82022-06-15 03:57:21 +00002773#endif /* MBEDTLS_X509_CRT_PARSE_C */
Jerry Yu5a26f302022-05-10 20:46:40 +08002774
Gilles Peskine449bd832023-01-11 14:50:10 +01002775 ret = mbedtls_ssl_tls13_write_certificate(ssl);
2776 if (ret != 0) {
2777 return ret;
2778 }
2779 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_CERTIFICATE_VERIFY);
2780 return 0;
Jerry Yu83da34e2022-04-16 13:59:52 +08002781}
2782
2783/*
Jerry Yuc6e6dbf2022-04-16 19:42:57 +08002784 * Handler for MBEDTLS_SSL_CERTIFICATE_VERIFY
Jerry Yu83da34e2022-04-16 13:59:52 +08002785 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002786MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002787static int ssl_tls13_write_certificate_verify(mbedtls_ssl_context *ssl)
Jerry Yu83da34e2022-04-16 13:59:52 +08002788{
Gilles Peskine449bd832023-01-11 14:50:10 +01002789 int ret = mbedtls_ssl_tls13_write_certificate_verify(ssl);
2790 if (ret != 0) {
2791 return ret;
2792 }
2793 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_SERVER_FINISHED);
2794 return 0;
Jerry Yu83da34e2022-04-16 13:59:52 +08002795}
Ronald Cron928cbd32022-10-04 16:14:26 +02002796#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED */
Jerry Yu83da34e2022-04-16 13:59:52 +08002797
Jerry Yu9b72e392023-12-01 16:27:08 +08002798/*
2799 * RFC 8446 section A.2
2800 *
2801 * | Send ServerHello
2802 * | K_send = handshake
2803 * | Send EncryptedExtensions
2804 * | [Send CertificateRequest]
2805 * Can send | [Send Certificate + CertificateVerify]
2806 * app data | Send Finished
2807 * after --> | K_send = application
2808 * here +--------+--------+
2809 * No 0-RTT | | 0-RTT
2810 * | |
2811 * K_recv = handshake | | K_recv = early data
2812 * [Skip decrypt errors] | +------> WAIT_EOED -+
2813 * | | Recv | | Recv EndOfEarlyData
2814 * | | early data | | K_recv = handshake
2815 * | +------------+ |
2816 * | |
2817 * +> WAIT_FLIGHT2 <--------+
2818 * |
2819 * +--------+--------+
2820 * No auth | | Client auth
2821 * | |
2822 * | v
2823 * | WAIT_CERT
2824 * | Recv | | Recv Certificate
2825 * | empty | v
2826 * | Certificate | WAIT_CV
2827 * | | | Recv
2828 * | v | CertificateVerify
2829 * +-> WAIT_FINISHED <---+
2830 * | Recv Finished
2831 *
2832 *
2833 * The following function handles the state changes after WAIT_FLIGHT2 in the
Jerry Yu3be85072023-12-04 09:58:54 +08002834 * above diagram. We are not going to receive early data related messages
2835 * anymore, prepare to receive the first handshake message of the client
2836 * second flight.
Jerry Yu9b72e392023-12-01 16:27:08 +08002837 */
Jerry Yu3be85072023-12-04 09:58:54 +08002838static void ssl_tls13_prepare_for_handshake_second_flight(
2839 mbedtls_ssl_context *ssl)
Jerry Yu9b72e392023-12-01 16:27:08 +08002840{
Jerry Yu9b72e392023-12-01 16:27:08 +08002841 if (ssl->handshake->certificate_request_sent) {
2842 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_CLIENT_CERTIFICATE);
2843 } else {
Jerry Yu42020fb2023-12-05 17:35:53 +08002844 MBEDTLS_SSL_DEBUG_MSG(2, ("skip parse certificate"));
Jerry Yuebb1b1d2023-12-05 11:02:15 +08002845 MBEDTLS_SSL_DEBUG_MSG(2, ("skip parse certificate verify"));
Jerry Yuebb1b1d2023-12-05 11:02:15 +08002846
Jerry Yu9b72e392023-12-01 16:27:08 +08002847 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_CLIENT_FINISHED);
2848 }
Jerry Yu9b72e392023-12-01 16:27:08 +08002849}
2850
Jerry Yu83da34e2022-04-16 13:59:52 +08002851/*
Jerry Yud6e253d2022-05-18 13:59:24 +08002852 * Handler for MBEDTLS_SSL_SERVER_FINISHED
Jerry Yu69dd8d42022-04-16 12:51:26 +08002853 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002854MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002855static int ssl_tls13_write_server_finished(mbedtls_ssl_context *ssl)
Jerry Yu69dd8d42022-04-16 12:51:26 +08002856{
Jerry Yud6e253d2022-05-18 13:59:24 +08002857 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yu27bdc7c2022-04-16 13:33:27 +08002858
Gilles Peskine449bd832023-01-11 14:50:10 +01002859 ret = mbedtls_ssl_tls13_write_finished_message(ssl);
2860 if (ret != 0) {
2861 return ret;
2862 }
Jerry Yu27bdc7c2022-04-16 13:33:27 +08002863
Gilles Peskine449bd832023-01-11 14:50:10 +01002864 ret = mbedtls_ssl_tls13_compute_application_transform(ssl);
2865 if (ret != 0) {
Jerry Yue3d67cb2022-05-19 15:33:10 +08002866 MBEDTLS_SSL_PEND_FATAL_ALERT(
Gilles Peskine449bd832023-01-11 14:50:10 +01002867 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE,
2868 MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE);
2869 return ret;
Jerry Yue3d67cb2022-05-19 15:33:10 +08002870 }
XiaokangQianc3017f62022-05-13 05:55:41 +00002871
Jerry Yu87b5ed42022-12-12 13:07:07 +08002872#if defined(MBEDTLS_SSL_EARLY_DATA)
Ronald Cron78a38f62024-02-01 18:30:31 +01002873 if (ssl->handshake->early_data_accepted) {
Jerry Yu0af63dc2023-12-01 17:14:51 +08002874 /* See RFC 8446 section A.2 for more information */
Jerry Yu87b5ed42022-12-12 13:07:07 +08002875 MBEDTLS_SSL_DEBUG_MSG(
2876 1, ("Switch to early keys for inbound traffic. "
2877 "( K_recv = early data )"));
2878 mbedtls_ssl_set_inbound_transform(
2879 ssl, ssl->handshake->transform_earlydata);
2880 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_END_OF_EARLY_DATA);
2881 return 0;
2882 }
2883#endif /* MBEDTLS_SSL_EARLY_DATA */
Jerry Yu0af63dc2023-12-01 17:14:51 +08002884 MBEDTLS_SSL_DEBUG_MSG(
2885 1, ("Switch to handshake keys for inbound traffic "
2886 "( K_recv = handshake )"));
Gilles Peskine449bd832023-01-11 14:50:10 +01002887 mbedtls_ssl_set_inbound_transform(ssl, ssl->handshake->transform_handshake);
XiaokangQian189ded22022-05-10 08:12:17 +00002888
Jerry Yu3be85072023-12-04 09:58:54 +08002889 ssl_tls13_prepare_for_handshake_second_flight(ssl);
Jerry Yu7d8c3fe2022-12-12 12:59:44 +08002890
2891 return 0;
2892}
2893
Jerry Yu87b5ed42022-12-12 13:07:07 +08002894#if defined(MBEDTLS_SSL_EARLY_DATA)
2895/*
Jerry Yu59d420f2023-12-01 16:30:34 +08002896 * Handler for MBEDTLS_SSL_END_OF_EARLY_DATA
Jerry Yu87b5ed42022-12-12 13:07:07 +08002897 */
Jerry Yu3be85072023-12-04 09:58:54 +08002898#define SSL_GOT_END_OF_EARLY_DATA 0
Jerry Yub55f9eb2023-12-05 10:27:17 +08002899#define SSL_GOT_EARLY_DATA 1
Jerry Yud5c34962023-12-01 16:32:31 +08002900/* Coordination:
Jerry Yu3be85072023-12-04 09:58:54 +08002901 * Deals with the ambiguity of not knowing if the next message is an
2902 * EndOfEarlyData message or an application message containing early data.
Jerry Yud5c34962023-12-01 16:32:31 +08002903 * Returns a negative code on failure, or
Jerry Yu3be85072023-12-04 09:58:54 +08002904 * - SSL_GOT_END_OF_EARLY_DATA
Jerry Yub55f9eb2023-12-05 10:27:17 +08002905 * - SSL_GOT_EARLY_DATA
Jerry Yud5c34962023-12-01 16:32:31 +08002906 * indicating which message is received.
2907 */
2908MBEDTLS_CHECK_RETURN_CRITICAL
2909static int ssl_tls13_end_of_early_data_coordinate(mbedtls_ssl_context *ssl)
2910{
2911 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yub4ed4602023-12-01 16:34:00 +08002912
2913 if ((ret = mbedtls_ssl_read_record(ssl, 0)) != 0) {
2914 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_read_record", ret);
2915 return ret;
2916 }
2917 ssl->keep_current_message = 1;
2918
2919 if (ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
2920 ssl->in_msg[0] == MBEDTLS_SSL_HS_END_OF_EARLY_DATA) {
Jerry Yub55f9eb2023-12-05 10:27:17 +08002921 MBEDTLS_SSL_DEBUG_MSG(3, ("Received an end_of_early_data message."));
Jerry Yu3be85072023-12-04 09:58:54 +08002922 return SSL_GOT_END_OF_EARLY_DATA;
Jerry Yub4ed4602023-12-01 16:34:00 +08002923 }
2924
2925 if (ssl->in_msgtype == MBEDTLS_SSL_MSG_APPLICATION_DATA) {
Jerry Yub55f9eb2023-12-05 10:27:17 +08002926 MBEDTLS_SSL_DEBUG_MSG(3, ("Received early data"));
Jerry Yu6a5904d2023-12-06 17:11:12 +08002927 /* RFC 8446 section 4.6.1
2928 *
2929 * A server receiving more than max_early_data_size bytes of 0-RTT data
2930 * SHOULD terminate the connection with an "unexpected_message" alert.
2931 *
2932 * TODO: Add received data size check here.
2933 */
Ronald Croned7d4bf2024-01-31 07:55:19 +01002934 if (ssl->in_offt == NULL) {
2935 /* Set the reading pointer */
2936 ssl->in_offt = ssl->in_msg;
2937 }
Jerry Yub55f9eb2023-12-05 10:27:17 +08002938 return SSL_GOT_EARLY_DATA;
Jerry Yub4ed4602023-12-01 16:34:00 +08002939 }
2940
Jerry Yu7bb40a32023-12-04 10:04:15 +08002941 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE,
2942 MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE);
Jerry Yub4ed4602023-12-01 16:34:00 +08002943 return MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE;
Jerry Yud5c34962023-12-01 16:32:31 +08002944}
2945
2946MBEDTLS_CHECK_RETURN_CRITICAL
2947static int ssl_tls13_parse_end_of_early_data(mbedtls_ssl_context *ssl,
2948 const unsigned char *buf,
2949 const unsigned char *end)
2950{
Jerry Yu75c9ab72023-12-01 16:41:40 +08002951 /* RFC 8446 section 4.5
2952 *
2953 * struct {} EndOfEarlyData;
2954 */
Jerry Yufbf03992023-12-04 10:00:37 +08002955 if (buf != end) {
2956 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR,
2957 MBEDTLS_ERR_SSL_DECODE_ERROR);
2958 return MBEDTLS_ERR_SSL_DECODE_ERROR;
2959 }
2960 return 0;
Jerry Yud5c34962023-12-01 16:32:31 +08002961}
2962
Jerry Yud5c34962023-12-01 16:32:31 +08002963/*
2964 * RFC 8446 section A.2
2965 *
2966 * | Send ServerHello
2967 * | K_send = handshake
2968 * | Send EncryptedExtensions
2969 * | [Send CertificateRequest]
2970 * Can send | [Send Certificate + CertificateVerify]
2971 * app data | Send Finished
2972 * after --> | K_send = application
2973 * here +--------+--------+
2974 * No 0-RTT | | 0-RTT
2975 * | |
2976 * K_recv = handshake | | K_recv = early data
2977 * [Skip decrypt errors] | +------> WAIT_EOED -+
2978 * | | Recv | | Recv EndOfEarlyData
2979 * | | early data | | K_recv = handshake
2980 * | +------------+ |
2981 * | |
2982 * +> WAIT_FLIGHT2 <--------+
2983 * |
2984 * +--------+--------+
2985 * No auth | | Client auth
2986 * | |
2987 * | v
2988 * | WAIT_CERT
2989 * | Recv | | Recv Certificate
2990 * | empty | v
2991 * | Certificate | WAIT_CV
2992 * | | | Recv
2993 * | v | CertificateVerify
2994 * +-> WAIT_FINISHED <---+
2995 * | Recv Finished
2996 *
2997 * The function handles actions and state changes from 0-RTT to WAIT_FLIGHT2 in
2998 * the above diagram.
2999 */
Jerry Yu87b5ed42022-12-12 13:07:07 +08003000MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yu59d420f2023-12-01 16:30:34 +08003001static int ssl_tls13_process_end_of_early_data(mbedtls_ssl_context *ssl)
Jerry Yu87b5ed42022-12-12 13:07:07 +08003002{
3003 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yud5c34962023-12-01 16:32:31 +08003004
3005 MBEDTLS_SSL_DEBUG_MSG(2, ("=> ssl_tls13_process_end_of_early_data"));
3006
3007 MBEDTLS_SSL_PROC_CHK_NEG(ssl_tls13_end_of_early_data_coordinate(ssl));
3008
Jerry Yu3be85072023-12-04 09:58:54 +08003009 if (ret == SSL_GOT_END_OF_EARLY_DATA) {
Jerry Yud5c34962023-12-01 16:32:31 +08003010 unsigned char *buf;
3011 size_t buf_len;
3012
3013 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_tls13_fetch_handshake_msg(
3014 ssl, MBEDTLS_SSL_HS_END_OF_EARLY_DATA,
3015 &buf, &buf_len));
3016
3017 MBEDTLS_SSL_PROC_CHK(ssl_tls13_parse_end_of_early_data(
3018 ssl, buf, buf + buf_len));
3019
Jerry Yue9655122023-12-01 16:44:40 +08003020 MBEDTLS_SSL_DEBUG_MSG(
3021 1, ("Switch to handshake keys for inbound traffic"
3022 "( K_recv = handshake )"));
3023 mbedtls_ssl_set_inbound_transform(
3024 ssl, ssl->handshake->transform_handshake);
3025
Jerry Yud5c34962023-12-01 16:32:31 +08003026 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_add_hs_msg_to_checksum(
3027 ssl, MBEDTLS_SSL_HS_END_OF_EARLY_DATA,
3028 buf, buf_len));
Jerry Yue9655122023-12-01 16:44:40 +08003029
Jerry Yu3be85072023-12-04 09:58:54 +08003030 ssl_tls13_prepare_for_handshake_second_flight(ssl);
Jerry Yud5c34962023-12-01 16:32:31 +08003031
Jerry Yub55f9eb2023-12-05 10:27:17 +08003032 } else if (ret == SSL_GOT_EARLY_DATA) {
Jerry Yud9ca3542023-12-06 17:23:52 +08003033 ret = MBEDTLS_ERR_SSL_RECEIVED_EARLY_DATA;
3034 goto cleanup;
Jerry Yud5c34962023-12-01 16:32:31 +08003035 } else {
3036 MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
3037 ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
3038 goto cleanup;
3039 }
3040
Jerry Yud5c34962023-12-01 16:32:31 +08003041cleanup:
3042 MBEDTLS_SSL_DEBUG_MSG(2, ("<= ssl_tls13_process_end_of_early_data"));
Jerry Yu87b5ed42022-12-12 13:07:07 +08003043 return ret;
3044}
3045#endif /* MBEDTLS_SSL_EARLY_DATA */
3046
Jerry Yu69dd8d42022-04-16 12:51:26 +08003047/*
Jerry Yud6e253d2022-05-18 13:59:24 +08003048 * Handler for MBEDTLS_SSL_CLIENT_FINISHED
Jerry Yu69dd8d42022-04-16 12:51:26 +08003049 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02003050MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01003051static int ssl_tls13_process_client_finished(mbedtls_ssl_context *ssl)
Jerry Yu69dd8d42022-04-16 12:51:26 +08003052{
Jerry Yud6e253d2022-05-18 13:59:24 +08003053 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
3054
Gilles Peskine449bd832023-01-11 14:50:10 +01003055 ret = mbedtls_ssl_tls13_process_finished_message(ssl);
3056 if (ret != 0) {
3057 return ret;
Jerry Yue3d67cb2022-05-19 15:33:10 +08003058 }
3059
Gilles Peskine449bd832023-01-11 14:50:10 +01003060 ret = mbedtls_ssl_tls13_compute_resumption_master_secret(ssl);
3061 if (ret != 0) {
Xiaokang Qian9f1747b2023-03-30 02:50:04 +00003062 MBEDTLS_SSL_DEBUG_RET(
3063 1, "mbedtls_ssl_tls13_compute_resumption_master_secret", ret);
Gilles Peskine449bd832023-01-11 14:50:10 +01003064 }
3065
3066 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_HANDSHAKE_WRAPUP);
3067 return 0;
Jerry Yu69dd8d42022-04-16 12:51:26 +08003068}
3069
3070/*
Jerry Yud6e253d2022-05-18 13:59:24 +08003071 * Handler for MBEDTLS_SSL_HANDSHAKE_WRAPUP
Jerry Yu69dd8d42022-04-16 12:51:26 +08003072 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02003073MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01003074static int ssl_tls13_handshake_wrapup(mbedtls_ssl_context *ssl)
Jerry Yu69dd8d42022-04-16 12:51:26 +08003075{
Gilles Peskine449bd832023-01-11 14:50:10 +01003076 MBEDTLS_SSL_DEBUG_MSG(2, ("handshake: done"));
Jerry Yu03ed50b2022-04-16 17:13:30 +08003077
Gilles Peskine449bd832023-01-11 14:50:10 +01003078 mbedtls_ssl_tls13_handshake_wrapup(ssl);
Jerry Yue67bef42022-07-07 07:29:42 +00003079
Pengyu Lv3643fdb2023-01-12 16:46:28 +08003080#if defined(MBEDTLS_SSL_SESSION_TICKETS) && \
3081 defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED)
Pengyu Lva1aa31b2022-12-13 13:49:59 +08003082/* TODO: Remove the check of SOME_PSK_ENABLED since SESSION_TICKETS requires
3083 * SOME_PSK_ENABLED to be enabled. Here is just to make CI happy. It is
3084 * expected to be resolved with issue#6395.
3085 */
Pengyu Lvc7af2c42022-12-01 16:33:00 +08003086 /* Sent NewSessionTicket message only when client supports PSK */
Pengyu Lvb2cfafb2023-11-14 13:56:13 +08003087 if (mbedtls_ssl_tls13_is_some_psk_supported(ssl)) {
Xiaokang Qian9f1747b2023-03-30 02:50:04 +00003088 mbedtls_ssl_handshake_set_state(
3089 ssl, MBEDTLS_SSL_TLS1_3_NEW_SESSION_TICKET);
Pengyu Lvc7af2c42022-12-01 16:33:00 +08003090 } else
Jerry Yufca4d572022-07-21 10:37:48 +08003091#endif
Pengyu Lv3643fdb2023-01-12 16:46:28 +08003092 {
3093 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_HANDSHAKE_OVER);
3094 }
Gilles Peskine449bd832023-01-11 14:50:10 +01003095 return 0;
Jerry Yu69dd8d42022-04-16 12:51:26 +08003096}
3097
3098/*
Jerry Yua8d3c502022-10-30 14:51:23 +08003099 * Handler for MBEDTLS_SSL_TLS1_3_NEW_SESSION_TICKET
Jerry Yue67bef42022-07-07 07:29:42 +00003100 */
3101#define SSL_NEW_SESSION_TICKET_SKIP 0
3102#define SSL_NEW_SESSION_TICKET_WRITE 1
3103MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01003104static int ssl_tls13_write_new_session_ticket_coordinate(mbedtls_ssl_context *ssl)
Jerry Yue67bef42022-07-07 07:29:42 +00003105{
3106 /* Check whether the use of session tickets is enabled */
Gilles Peskine449bd832023-01-11 14:50:10 +01003107 if (ssl->conf->f_ticket_write == NULL) {
3108 MBEDTLS_SSL_DEBUG_MSG(2, ("NewSessionTicket: disabled,"
3109 " callback is not set"));
3110 return SSL_NEW_SESSION_TICKET_SKIP;
Jerry Yud0766ec2022-09-22 10:46:57 +08003111 }
Gilles Peskine449bd832023-01-11 14:50:10 +01003112 if (ssl->conf->new_session_tickets_count == 0) {
3113 MBEDTLS_SSL_DEBUG_MSG(2, ("NewSessionTicket: disabled,"
3114 " configured count is zero"));
3115 return SSL_NEW_SESSION_TICKET_SKIP;
Jerry Yud0766ec2022-09-22 10:46:57 +08003116 }
3117
Gilles Peskine449bd832023-01-11 14:50:10 +01003118 if (ssl->handshake->new_session_tickets_count == 0) {
3119 MBEDTLS_SSL_DEBUG_MSG(2, ("NewSessionTicket: all tickets have "
3120 "been sent."));
3121 return SSL_NEW_SESSION_TICKET_SKIP;
Jerry Yue67bef42022-07-07 07:29:42 +00003122 }
3123
Gilles Peskine449bd832023-01-11 14:50:10 +01003124 return SSL_NEW_SESSION_TICKET_WRITE;
Jerry Yue67bef42022-07-07 07:29:42 +00003125}
3126
3127#if defined(MBEDTLS_SSL_SESSION_TICKETS)
3128MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01003129static int ssl_tls13_prepare_new_session_ticket(mbedtls_ssl_context *ssl,
3130 unsigned char *ticket_nonce,
3131 size_t ticket_nonce_size)
Jerry Yue67bef42022-07-07 07:29:42 +00003132{
3133 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
3134 mbedtls_ssl_session *session = ssl->session;
3135 mbedtls_ssl_ciphersuite_t *ciphersuite_info;
3136 psa_algorithm_t psa_hash_alg;
3137 int hash_length;
3138
Gilles Peskine449bd832023-01-11 14:50:10 +01003139 MBEDTLS_SSL_DEBUG_MSG(2, ("=> prepare NewSessionTicket msg"));
Jerry Yue67bef42022-07-07 07:29:42 +00003140
Pengyu Lv9f926952022-11-17 15:22:33 +08003141 /* Set ticket_flags depends on the advertised psk key exchange mode */
Pengyu Lv94a42cc2023-12-06 10:04:17 +08003142 mbedtls_ssl_tls13_session_clear_ticket_flags(
Pengyu Lv9eacb442022-12-09 14:39:19 +08003143 session, MBEDTLS_SSL_TLS1_3_TICKET_FLAGS_MASK);
Pengyu Lve6487fe2022-12-06 09:30:29 +08003144#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED)
Pengyu Lv94a42cc2023-12-06 10:04:17 +08003145 mbedtls_ssl_tls13_session_set_ticket_flags(
Pengyu Lv9eacb442022-12-09 14:39:19 +08003146 session, ssl->handshake->tls13_kex_modes);
Pengyu Lve6487fe2022-12-06 09:30:29 +08003147#endif
Jerry Yu95648b02023-12-06 15:03:34 +08003148
3149#if defined(MBEDTLS_SSL_EARLY_DATA)
3150 if (ssl->conf->early_data_enabled == MBEDTLS_SSL_EARLY_DATA_ENABLED &&
3151 ssl->conf->max_early_data_size > 0) {
Pengyu Lv94a42cc2023-12-06 10:04:17 +08003152 mbedtls_ssl_tls13_session_set_ticket_flags(
Jerry Yu95648b02023-12-06 15:03:34 +08003153 session, MBEDTLS_SSL_TLS1_3_TICKET_ALLOW_EARLY_DATA);
3154 }
3155#endif /* MBEDTLS_SSL_EARLY_DATA */
3156
Pengyu Lv4938a562023-01-16 11:28:49 +08003157 MBEDTLS_SSL_PRINT_TICKET_FLAGS(4, session->ticket_flags);
Pengyu Lv9f926952022-11-17 15:22:33 +08003158
Jerry Yue67bef42022-07-07 07:29:42 +00003159 /* Generate ticket_age_add */
Gilles Peskine449bd832023-01-11 14:50:10 +01003160 if ((ret = ssl->conf->f_rng(ssl->conf->p_rng,
3161 (unsigned char *) &session->ticket_age_add,
3162 sizeof(session->ticket_age_add)) != 0)) {
3163 MBEDTLS_SSL_DEBUG_RET(1, "generate_ticket_age_add", ret);
3164 return ret;
Jerry Yue67bef42022-07-07 07:29:42 +00003165 }
Gilles Peskine449bd832023-01-11 14:50:10 +01003166 MBEDTLS_SSL_DEBUG_MSG(3, ("ticket_age_add: %u",
3167 (unsigned int) session->ticket_age_add));
Jerry Yue67bef42022-07-07 07:29:42 +00003168
3169 /* Generate ticket_nonce */
Gilles Peskine449bd832023-01-11 14:50:10 +01003170 ret = ssl->conf->f_rng(ssl->conf->p_rng, ticket_nonce, ticket_nonce_size);
3171 if (ret != 0) {
3172 MBEDTLS_SSL_DEBUG_RET(1, "generate_ticket_nonce", ret);
3173 return ret;
Jerry Yue67bef42022-07-07 07:29:42 +00003174 }
Gilles Peskine449bd832023-01-11 14:50:10 +01003175 MBEDTLS_SSL_DEBUG_BUF(3, "ticket_nonce:",
3176 ticket_nonce, ticket_nonce_size);
Jerry Yue67bef42022-07-07 07:29:42 +00003177
3178 ciphersuite_info =
Gilles Peskine449bd832023-01-11 14:50:10 +01003179 (mbedtls_ssl_ciphersuite_t *) ssl->handshake->ciphersuite_info;
Dave Rodgman2eab4622023-10-05 13:30:37 +01003180 psa_hash_alg = mbedtls_md_psa_alg_from_type((mbedtls_md_type_t) ciphersuite_info->mac);
Gilles Peskine449bd832023-01-11 14:50:10 +01003181 hash_length = PSA_HASH_LENGTH(psa_hash_alg);
3182 if (hash_length == -1 ||
3183 (size_t) hash_length > sizeof(session->resumption_key)) {
3184 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Jerry Yufca4d572022-07-21 10:37:48 +08003185 }
3186
Jerry Yue67bef42022-07-07 07:29:42 +00003187 /* In this code the psk key length equals the length of the hash */
3188 session->resumption_key_len = hash_length;
3189 session->ciphersuite = ciphersuite_info->id;
3190
3191 /* Compute resumption key
3192 *
3193 * HKDF-Expand-Label( resumption_master_secret,
3194 * "resumption", ticket_nonce, Hash.length )
3195 */
3196 ret = mbedtls_ssl_tls13_hkdf_expand_label(
Gilles Peskine449bd832023-01-11 14:50:10 +01003197 psa_hash_alg,
3198 session->app_secrets.resumption_master_secret,
3199 hash_length,
3200 MBEDTLS_SSL_TLS1_3_LBL_WITH_LEN(resumption),
3201 ticket_nonce,
3202 ticket_nonce_size,
3203 session->resumption_key,
3204 hash_length);
Jerry Yue67bef42022-07-07 07:29:42 +00003205
Gilles Peskine449bd832023-01-11 14:50:10 +01003206 if (ret != 0) {
3207 MBEDTLS_SSL_DEBUG_RET(2,
3208 "Creating the ticket-resumed PSK failed",
3209 ret);
3210 return ret;
Jerry Yue67bef42022-07-07 07:29:42 +00003211 }
Gilles Peskine449bd832023-01-11 14:50:10 +01003212 MBEDTLS_SSL_DEBUG_BUF(3, "Ticket-resumed PSK",
3213 session->resumption_key,
3214 session->resumption_key_len);
Jerry Yue67bef42022-07-07 07:29:42 +00003215
Gilles Peskine449bd832023-01-11 14:50:10 +01003216 MBEDTLS_SSL_DEBUG_BUF(3, "resumption_master_secret",
3217 session->app_secrets.resumption_master_secret,
3218 hash_length);
Jerry Yue67bef42022-07-07 07:29:42 +00003219
Gilles Peskine449bd832023-01-11 14:50:10 +01003220 return 0;
Jerry Yue67bef42022-07-07 07:29:42 +00003221}
3222
3223/* This function creates a NewSessionTicket message in the following format:
3224 *
3225 * struct {
3226 * uint32 ticket_lifetime;
3227 * uint32 ticket_age_add;
3228 * opaque ticket_nonce<0..255>;
3229 * opaque ticket<1..2^16-1>;
3230 * Extension extensions<0..2^16-2>;
3231 * } NewSessionTicket;
3232 *
3233 * The ticket inside the NewSessionTicket message is an encrypted container
3234 * carrying the necessary information so that the server is later able to
3235 * re-start the communication.
3236 *
3237 * The following fields are placed inside the ticket by the
3238 * f_ticket_write() function:
3239 *
Jerry Yu0069abc2023-11-23 21:07:28 +08003240 * - creation time (ticket_creation_time)
3241 * - flags (ticket_flags)
Jerry Yue67bef42022-07-07 07:29:42 +00003242 * - age add (ticket_age_add)
Jerry Yu0069abc2023-11-23 21:07:28 +08003243 * - key (resumption_key)
3244 * - key length (resumption_key_len)
Jerry Yue67bef42022-07-07 07:29:42 +00003245 * - ciphersuite (ciphersuite)
Jerry Yu0069abc2023-11-23 21:07:28 +08003246 * - max_early_data_size (max_early_data_size)
Jerry Yue67bef42022-07-07 07:29:42 +00003247 */
3248MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01003249static int ssl_tls13_write_new_session_ticket_body(mbedtls_ssl_context *ssl,
3250 unsigned char *buf,
3251 unsigned char *end,
3252 size_t *out_len,
3253 unsigned char *ticket_nonce,
3254 size_t ticket_nonce_size)
Jerry Yue67bef42022-07-07 07:29:42 +00003255{
3256 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
3257 unsigned char *p = buf;
3258 mbedtls_ssl_session *session = ssl->session;
3259 size_t ticket_len;
3260 uint32_t ticket_lifetime;
Jerry Yu01da35e2022-12-12 15:09:22 +08003261 unsigned char *p_extensions_len;
Jerry Yue67bef42022-07-07 07:29:42 +00003262
3263 *out_len = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01003264 MBEDTLS_SSL_DEBUG_MSG(2, ("=> write NewSessionTicket msg"));
Jerry Yue67bef42022-07-07 07:29:42 +00003265
3266 /*
3267 * ticket_lifetime 4 bytes
3268 * ticket_age_add 4 bytes
3269 * ticket_nonce 1 + ticket_nonce_size bytes
3270 * ticket >=2 bytes
3271 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003272 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 4 + 4 + 1 + ticket_nonce_size + 2);
Jerry Yue67bef42022-07-07 07:29:42 +00003273
3274 /* Generate ticket and ticket_lifetime */
Ronald Cron3c0072b2023-11-22 10:00:14 +01003275#if defined(MBEDTLS_HAVE_TIME)
3276 session->ticket_creation_time = mbedtls_ms_time();
3277#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01003278 ret = ssl->conf->f_ticket_write(ssl->conf->p_ticket,
3279 session,
3280 p + 9 + ticket_nonce_size + 2,
3281 end,
3282 &ticket_len,
3283 &ticket_lifetime);
3284 if (ret != 0) {
3285 MBEDTLS_SSL_DEBUG_RET(1, "write_ticket", ret);
3286 return ret;
Jerry Yue67bef42022-07-07 07:29:42 +00003287 }
3288 /* RFC 8446 4.6.1
3289 * ticket_lifetime: Indicates the lifetime in seconds as a 32-bit
3290 * unsigned integer in network byte order from the time of ticket
3291 * issuance. Servers MUST NOT use any value greater than
3292 * 604800 seconds (7 days). The value of zero indicates that the
3293 * ticket should be discarded immediately. Clients MUST NOT cache
3294 * tickets for longer than 7 days, regardless of the ticket_lifetime,
3295 * and MAY delete tickets earlier based on local policy. A server
3296 * MAY treat a ticket as valid for a shorter period of time than what
3297 * is stated in the ticket_lifetime.
3298 */
Jerry Yu8e0174a2023-11-10 13:58:16 +08003299 if (ticket_lifetime > MBEDTLS_SSL_TLS1_3_MAX_ALLOWED_TICKET_LIFETIME) {
3300 ticket_lifetime = MBEDTLS_SSL_TLS1_3_MAX_ALLOWED_TICKET_LIFETIME;
Gilles Peskine449bd832023-01-11 14:50:10 +01003301 }
3302 MBEDTLS_PUT_UINT32_BE(ticket_lifetime, p, 0);
3303 MBEDTLS_SSL_DEBUG_MSG(3, ("ticket_lifetime: %u",
3304 (unsigned int) ticket_lifetime));
Jerry Yue67bef42022-07-07 07:29:42 +00003305
3306 /* Write ticket_age_add */
Gilles Peskine449bd832023-01-11 14:50:10 +01003307 MBEDTLS_PUT_UINT32_BE(session->ticket_age_add, p, 4);
3308 MBEDTLS_SSL_DEBUG_MSG(3, ("ticket_age_add: %u",
3309 (unsigned int) session->ticket_age_add));
Jerry Yue67bef42022-07-07 07:29:42 +00003310
3311 /* Write ticket_nonce */
Gilles Peskine449bd832023-01-11 14:50:10 +01003312 p[8] = (unsigned char) ticket_nonce_size;
3313 if (ticket_nonce_size > 0) {
3314 memcpy(p + 9, ticket_nonce, ticket_nonce_size);
Jerry Yue67bef42022-07-07 07:29:42 +00003315 }
3316 p += 9 + ticket_nonce_size;
3317
3318 /* Write ticket */
Gilles Peskine449bd832023-01-11 14:50:10 +01003319 MBEDTLS_PUT_UINT16_BE(ticket_len, p, 0);
Jerry Yue67bef42022-07-07 07:29:42 +00003320 p += 2;
Gilles Peskine449bd832023-01-11 14:50:10 +01003321 MBEDTLS_SSL_DEBUG_BUF(4, "ticket", p, ticket_len);
Jerry Yue67bef42022-07-07 07:29:42 +00003322 p += ticket_len;
3323
3324 /* Ticket Extensions
3325 *
Jerry Yu01da35e2022-12-12 15:09:22 +08003326 * Extension extensions<0..2^16-2>;
Jerry Yue67bef42022-07-07 07:29:42 +00003327 */
Jerry Yuedab6372022-10-31 14:37:31 +08003328 ssl->handshake->sent_extensions = MBEDTLS_SSL_EXT_MASK_NONE;
3329
Gilles Peskine449bd832023-01-11 14:50:10 +01003330 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 2);
Jerry Yu01da35e2022-12-12 15:09:22 +08003331 p_extensions_len = p;
Jerry Yue67bef42022-07-07 07:29:42 +00003332 p += 2;
3333
Jerry Yu01da35e2022-12-12 15:09:22 +08003334#if defined(MBEDTLS_SSL_EARLY_DATA)
Pengyu Lv94a42cc2023-12-06 10:04:17 +08003335 if (mbedtls_ssl_tls13_session_ticket_allow_early_data(session)) {
Jerry Yu95648b02023-12-06 15:03:34 +08003336 size_t output_len;
3337
Jerry Yuf135bac2023-11-23 18:10:51 +08003338 if ((ret = mbedtls_ssl_tls13_write_early_data_ext(
Jerry Yuc59c5862023-12-05 10:40:49 +08003339 ssl, 1, p, end, &output_len)) != 0) {
Jerry Yuf135bac2023-11-23 18:10:51 +08003340 MBEDTLS_SSL_DEBUG_RET(
3341 1, "mbedtls_ssl_tls13_write_early_data_ext", ret);
3342 return ret;
3343 }
3344 p += output_len;
Jerry Yu9e7f9bc2023-11-27 16:52:07 +08003345 } else {
3346 MBEDTLS_SSL_DEBUG_MSG(
3347 4, ("early_data not allowed, "
3348 "skip early_data extension in NewSessionTicket"));
Jerry Yu01da35e2022-12-12 15:09:22 +08003349 }
Jerry Yuf135bac2023-11-23 18:10:51 +08003350
Jerry Yu01da35e2022-12-12 15:09:22 +08003351#endif /* MBEDTLS_SSL_EARLY_DATA */
3352
3353 MBEDTLS_PUT_UINT16_BE(p - p_extensions_len - 2, p_extensions_len, 0);
3354
Jerry Yue67bef42022-07-07 07:29:42 +00003355 *out_len = p - buf;
Gilles Peskine449bd832023-01-11 14:50:10 +01003356 MBEDTLS_SSL_DEBUG_BUF(4, "ticket", buf, *out_len);
3357 MBEDTLS_SSL_DEBUG_MSG(2, ("<= write new session ticket"));
Jerry Yue67bef42022-07-07 07:29:42 +00003358
Jerry Yu7de2ff02022-11-08 21:43:46 +08003359 MBEDTLS_SSL_PRINT_EXTS(
Gilles Peskine449bd832023-01-11 14:50:10 +01003360 3, MBEDTLS_SSL_HS_NEW_SESSION_TICKET, ssl->handshake->sent_extensions);
Jerry Yu4b8f2f72022-10-31 13:31:22 +08003361
Gilles Peskine449bd832023-01-11 14:50:10 +01003362 return 0;
Jerry Yue67bef42022-07-07 07:29:42 +00003363}
3364
3365/*
Jerry Yua8d3c502022-10-30 14:51:23 +08003366 * Handler for MBEDTLS_SSL_TLS1_3_NEW_SESSION_TICKET
Jerry Yue67bef42022-07-07 07:29:42 +00003367 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003368static int ssl_tls13_write_new_session_ticket(mbedtls_ssl_context *ssl)
Jerry Yue67bef42022-07-07 07:29:42 +00003369{
3370 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
3371
Gilles Peskine449bd832023-01-11 14:50:10 +01003372 MBEDTLS_SSL_PROC_CHK_NEG(ssl_tls13_write_new_session_ticket_coordinate(ssl));
Jerry Yue67bef42022-07-07 07:29:42 +00003373
Gilles Peskine449bd832023-01-11 14:50:10 +01003374 if (ret == SSL_NEW_SESSION_TICKET_WRITE) {
Jerry Yue67bef42022-07-07 07:29:42 +00003375 unsigned char ticket_nonce[MBEDTLS_SSL_TLS1_3_TICKET_NONCE_LENGTH];
3376 unsigned char *buf;
3377 size_t buf_len, msg_len;
3378
Gilles Peskine449bd832023-01-11 14:50:10 +01003379 MBEDTLS_SSL_PROC_CHK(ssl_tls13_prepare_new_session_ticket(
3380 ssl, ticket_nonce, sizeof(ticket_nonce)));
Jerry Yue67bef42022-07-07 07:29:42 +00003381
Xiaokang Qian9f1747b2023-03-30 02:50:04 +00003382 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_start_handshake_msg(
3383 ssl, MBEDTLS_SSL_HS_NEW_SESSION_TICKET,
3384 &buf, &buf_len));
Jerry Yue67bef42022-07-07 07:29:42 +00003385
Gilles Peskine449bd832023-01-11 14:50:10 +01003386 MBEDTLS_SSL_PROC_CHK(ssl_tls13_write_new_session_ticket_body(
3387 ssl, buf, buf + buf_len, &msg_len,
3388 ticket_nonce, sizeof(ticket_nonce)));
Jerry Yue67bef42022-07-07 07:29:42 +00003389
Gilles Peskine449bd832023-01-11 14:50:10 +01003390 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_finish_handshake_msg(
3391 ssl, buf_len, msg_len));
Jerry Yufca4d572022-07-21 10:37:48 +08003392
Jerry Yu359e65f2022-09-22 23:47:43 +08003393 /* Limit session tickets count to one when resumption connection.
3394 *
3395 * See document of mbedtls_ssl_conf_new_session_tickets.
3396 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003397 if (ssl->handshake->resume == 1) {
Jerry Yu359e65f2022-09-22 23:47:43 +08003398 ssl->handshake->new_session_tickets_count = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01003399 } else {
Jerry Yu359e65f2022-09-22 23:47:43 +08003400 ssl->handshake->new_session_tickets_count--;
Gilles Peskine449bd832023-01-11 14:50:10 +01003401 }
Jerry Yub7e3fa72022-09-22 11:07:18 +08003402
Jerry Yua8d3c502022-10-30 14:51:23 +08003403 mbedtls_ssl_handshake_set_state(
Gilles Peskine449bd832023-01-11 14:50:10 +01003404 ssl, MBEDTLS_SSL_TLS1_3_NEW_SESSION_TICKET_FLUSH);
3405 } else {
3406 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_HANDSHAKE_OVER);
Jerry Yue67bef42022-07-07 07:29:42 +00003407 }
3408
Jerry Yue67bef42022-07-07 07:29:42 +00003409cleanup:
3410
Gilles Peskine449bd832023-01-11 14:50:10 +01003411 return ret;
Jerry Yue67bef42022-07-07 07:29:42 +00003412}
3413#endif /* MBEDTLS_SSL_SESSION_TICKETS */
3414
3415/*
XiaokangQiane8ff3502022-04-22 02:34:40 +00003416 * TLS 1.3 State Machine -- server side
XiaokangQian7807f9f2022-02-15 10:04:37 +00003417 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003418int mbedtls_ssl_tls13_handshake_server_step(mbedtls_ssl_context *ssl)
Jerry Yub9930e72021-08-06 17:11:51 +08003419{
XiaokangQian08037552022-04-20 07:16:41 +00003420 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
XiaokangQian7807f9f2022-02-15 10:04:37 +00003421
Gilles Peskine449bd832023-01-11 14:50:10 +01003422 if (ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER || ssl->handshake == NULL) {
3423 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
3424 }
XiaokangQian7807f9f2022-02-15 10:04:37 +00003425
Gilles Peskine449bd832023-01-11 14:50:10 +01003426 MBEDTLS_SSL_DEBUG_MSG(2, ("tls13 server state: %s(%d)",
Dave Rodgman2eab4622023-10-05 13:30:37 +01003427 mbedtls_ssl_states_str((mbedtls_ssl_states) ssl->state),
Gilles Peskine449bd832023-01-11 14:50:10 +01003428 ssl->state));
Jerry Yu6e81b272021-09-27 11:16:17 +08003429
Gilles Peskine449bd832023-01-11 14:50:10 +01003430 switch (ssl->state) {
XiaokangQian7807f9f2022-02-15 10:04:37 +00003431 /* start state */
3432 case MBEDTLS_SSL_HELLO_REQUEST:
Gilles Peskine449bd832023-01-11 14:50:10 +01003433 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_CLIENT_HELLO);
XiaokangQian08037552022-04-20 07:16:41 +00003434 ret = 0;
XiaokangQian7807f9f2022-02-15 10:04:37 +00003435 break;
3436
XiaokangQian7807f9f2022-02-15 10:04:37 +00003437 case MBEDTLS_SSL_CLIENT_HELLO:
Gilles Peskine449bd832023-01-11 14:50:10 +01003438 ret = ssl_tls13_process_client_hello(ssl);
3439 if (ret != 0) {
3440 MBEDTLS_SSL_DEBUG_RET(1, "ssl_tls13_process_client_hello", ret);
3441 }
Jerry Yuf41553b2022-05-09 22:20:30 +08003442 break;
XiaokangQian7807f9f2022-02-15 10:04:37 +00003443
Jerry Yuf41553b2022-05-09 22:20:30 +08003444 case MBEDTLS_SSL_HELLO_RETRY_REQUEST:
Gilles Peskine449bd832023-01-11 14:50:10 +01003445 ret = ssl_tls13_write_hello_retry_request(ssl);
3446 if (ret != 0) {
3447 MBEDTLS_SSL_DEBUG_RET(1, "ssl_tls13_write_hello_retry_request", ret);
3448 return ret;
Jerry Yuf41553b2022-05-09 22:20:30 +08003449 }
XiaokangQian7807f9f2022-02-15 10:04:37 +00003450 break;
3451
Jerry Yu5b64ae92022-03-30 17:15:02 +08003452 case MBEDTLS_SSL_SERVER_HELLO:
Gilles Peskine449bd832023-01-11 14:50:10 +01003453 ret = ssl_tls13_write_server_hello(ssl);
Jerry Yu5b64ae92022-03-30 17:15:02 +08003454 break;
3455
Xiaofei Baicba64af2022-02-15 10:00:56 +00003456 case MBEDTLS_SSL_ENCRYPTED_EXTENSIONS:
Gilles Peskine449bd832023-01-11 14:50:10 +01003457 ret = ssl_tls13_write_encrypted_extensions(ssl);
3458 if (ret != 0) {
3459 MBEDTLS_SSL_DEBUG_RET(1, "ssl_tls13_write_encrypted_extensions", ret);
3460 return ret;
Xiaofei Baicba64af2022-02-15 10:00:56 +00003461 }
Jerry Yu48330562022-05-06 21:35:44 +08003462 break;
Jerry Yu6a2cd9e2022-05-05 11:14:19 +08003463
Ronald Cron928cbd32022-10-04 16:14:26 +02003464#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED)
Xiaofei Bai9ca09d42022-02-14 12:57:18 +00003465 case MBEDTLS_SSL_CERTIFICATE_REQUEST:
Gilles Peskine449bd832023-01-11 14:50:10 +01003466 ret = ssl_tls13_write_certificate_request(ssl);
Xiaofei Bai9ca09d42022-02-14 12:57:18 +00003467 break;
Xiaofei Bai9ca09d42022-02-14 12:57:18 +00003468
Jerry Yu83da34e2022-04-16 13:59:52 +08003469 case MBEDTLS_SSL_SERVER_CERTIFICATE:
Gilles Peskine449bd832023-01-11 14:50:10 +01003470 ret = ssl_tls13_write_server_certificate(ssl);
Jerry Yu83da34e2022-04-16 13:59:52 +08003471 break;
3472
3473 case MBEDTLS_SSL_CERTIFICATE_VERIFY:
Gilles Peskine449bd832023-01-11 14:50:10 +01003474 ret = ssl_tls13_write_certificate_verify(ssl);
Jerry Yu83da34e2022-04-16 13:59:52 +08003475 break;
Ronald Cron928cbd32022-10-04 16:14:26 +02003476#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED */
Jerry Yu83da34e2022-04-16 13:59:52 +08003477
Gilles Peskine449bd832023-01-11 14:50:10 +01003478 /*
3479 * Injection of dummy-CCS's for middlebox compatibility
3480 */
Gabor Mezei7b39bf12022-05-24 16:04:14 +02003481#if defined(MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE)
Gabor Mezeif7044ea2022-06-28 16:01:49 +02003482 case MBEDTLS_SSL_SERVER_CCS_AFTER_HELLO_RETRY_REQUEST:
Gilles Peskine449bd832023-01-11 14:50:10 +01003483 ret = mbedtls_ssl_tls13_write_change_cipher_spec(ssl);
3484 if (ret == 0) {
3485 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_CLIENT_HELLO);
3486 }
Gabor Mezei7b39bf12022-05-24 16:04:14 +02003487 break;
3488
3489 case MBEDTLS_SSL_SERVER_CCS_AFTER_SERVER_HELLO:
Ronald Crone273f722024-02-13 18:22:26 +01003490 ret = mbedtls_ssl_tls13_write_change_cipher_spec(ssl);
3491 if (ret != 0) {
3492 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01003493 }
Ronald Cronfe59ff72024-01-24 14:31:50 +01003494 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_ENCRYPTED_EXTENSIONS);
Gabor Mezei7b39bf12022-05-24 16:04:14 +02003495 break;
3496#endif /* MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE */
3497
Jerry Yu69dd8d42022-04-16 12:51:26 +08003498 case MBEDTLS_SSL_SERVER_FINISHED:
Gilles Peskine449bd832023-01-11 14:50:10 +01003499 ret = ssl_tls13_write_server_finished(ssl);
Jerry Yu69dd8d42022-04-16 12:51:26 +08003500 break;
3501
Jerry Yu87b5ed42022-12-12 13:07:07 +08003502#if defined(MBEDTLS_SSL_EARLY_DATA)
3503 case MBEDTLS_SSL_END_OF_EARLY_DATA:
Jerry Yu59d420f2023-12-01 16:30:34 +08003504 ret = ssl_tls13_process_end_of_early_data(ssl);
Jerry Yu87b5ed42022-12-12 13:07:07 +08003505 break;
3506#endif /* MBEDTLS_SSL_EARLY_DATA */
3507
Jerry Yu69dd8d42022-04-16 12:51:26 +08003508 case MBEDTLS_SSL_CLIENT_FINISHED:
Gilles Peskine449bd832023-01-11 14:50:10 +01003509 ret = ssl_tls13_process_client_finished(ssl);
Jerry Yu69dd8d42022-04-16 12:51:26 +08003510 break;
3511
Jerry Yu69dd8d42022-04-16 12:51:26 +08003512 case MBEDTLS_SSL_HANDSHAKE_WRAPUP:
Gilles Peskine449bd832023-01-11 14:50:10 +01003513 ret = ssl_tls13_handshake_wrapup(ssl);
Jerry Yu69dd8d42022-04-16 12:51:26 +08003514 break;
3515
Ronald Cron766c0cd2022-10-18 12:17:11 +02003516#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED)
XiaokangQian6b916b12022-04-25 07:29:34 +00003517 case MBEDTLS_SSL_CLIENT_CERTIFICATE:
Gilles Peskine449bd832023-01-11 14:50:10 +01003518 ret = mbedtls_ssl_tls13_process_certificate(ssl);
3519 if (ret == 0) {
3520 if (ssl->session_negotiate->peer_cert != NULL) {
Ronald Cron209cae92022-06-07 10:30:19 +02003521 mbedtls_ssl_handshake_set_state(
Gilles Peskine449bd832023-01-11 14:50:10 +01003522 ssl, MBEDTLS_SSL_CLIENT_CERTIFICATE_VERIFY);
3523 } else {
3524 MBEDTLS_SSL_DEBUG_MSG(2, ("skip parse certificate verify"));
Ronald Cron209cae92022-06-07 10:30:19 +02003525 mbedtls_ssl_handshake_set_state(
Gilles Peskine449bd832023-01-11 14:50:10 +01003526 ssl, MBEDTLS_SSL_CLIENT_FINISHED);
Ronald Cron19385882022-06-15 16:26:13 +02003527 }
XiaokangQian6b916b12022-04-25 07:29:34 +00003528 }
3529 break;
3530
3531 case MBEDTLS_SSL_CLIENT_CERTIFICATE_VERIFY:
Gilles Peskine449bd832023-01-11 14:50:10 +01003532 ret = mbedtls_ssl_tls13_process_certificate_verify(ssl);
3533 if (ret == 0) {
XiaokangQian6b916b12022-04-25 07:29:34 +00003534 mbedtls_ssl_handshake_set_state(
Gilles Peskine449bd832023-01-11 14:50:10 +01003535 ssl, MBEDTLS_SSL_CLIENT_FINISHED);
XiaokangQian6b916b12022-04-25 07:29:34 +00003536 }
3537 break;
Ronald Cron766c0cd2022-10-18 12:17:11 +02003538#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED */
XiaokangQian6b916b12022-04-25 07:29:34 +00003539
Jerry Yue67bef42022-07-07 07:29:42 +00003540#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Jerry Yua8d3c502022-10-30 14:51:23 +08003541 case MBEDTLS_SSL_TLS1_3_NEW_SESSION_TICKET:
Gilles Peskine449bd832023-01-11 14:50:10 +01003542 ret = ssl_tls13_write_new_session_ticket(ssl);
3543 if (ret != 0) {
3544 MBEDTLS_SSL_DEBUG_RET(1,
3545 "ssl_tls13_write_new_session_ticket ",
3546 ret);
Jerry Yue67bef42022-07-07 07:29:42 +00003547 }
3548 break;
Jerry Yua8d3c502022-10-30 14:51:23 +08003549 case MBEDTLS_SSL_TLS1_3_NEW_SESSION_TICKET_FLUSH:
Jerry Yue67bef42022-07-07 07:29:42 +00003550 /* This state is necessary to do the flush of the New Session
Jerry Yua8d3c502022-10-30 14:51:23 +08003551 * Ticket message written in MBEDTLS_SSL_TLS1_3_NEW_SESSION_TICKET
Jerry Yue67bef42022-07-07 07:29:42 +00003552 * as part of ssl_prepare_handshake_step.
3553 */
3554 ret = 0;
Jerry Yud4e75002022-08-09 13:33:50 +08003555
Gilles Peskine449bd832023-01-11 14:50:10 +01003556 if (ssl->handshake->new_session_tickets_count == 0) {
3557 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_HANDSHAKE_OVER);
3558 } else {
Xiaokang Qian9f1747b2023-03-30 02:50:04 +00003559 mbedtls_ssl_handshake_set_state(
3560 ssl, MBEDTLS_SSL_TLS1_3_NEW_SESSION_TICKET);
Gilles Peskine449bd832023-01-11 14:50:10 +01003561 }
Jerry Yue67bef42022-07-07 07:29:42 +00003562 break;
3563
3564#endif /* MBEDTLS_SSL_SESSION_TICKETS */
3565
XiaokangQian7807f9f2022-02-15 10:04:37 +00003566 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01003567 MBEDTLS_SSL_DEBUG_MSG(1, ("invalid state %d", ssl->state));
3568 return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
XiaokangQian7807f9f2022-02-15 10:04:37 +00003569 }
3570
Gilles Peskine449bd832023-01-11 14:50:10 +01003571 return ret;
Jerry Yub9930e72021-08-06 17:11:51 +08003572}
Jerry Yu3cc4c2a2021-08-06 16:29:08 +08003573
Jerry Yufb4b6472022-01-27 15:03:26 +08003574#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_PROTO_TLS1_3 */