blob: 061dcf7fd66e9a6c8ff328d385155a4307d3d1d3 [file] [log] [blame]
Jerry Yu3cc4c2a2021-08-06 16:29:08 +08001/*
Jerry Yub9930e72021-08-06 17:11:51 +08002 * TLS 1.3 server-side functions
Jerry Yu3cc4c2a2021-08-06 16:29:08 +08003 *
4 * Copyright The Mbed TLS Contributors
Dave Rodgman16799db2023-11-02 19:47:20 +00005 * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
Gilles Peskine449bd832023-01-11 14:50:10 +01006 */
Jerry Yu3cc4c2a2021-08-06 16:29:08 +08007
8#include "common.h"
9
Jerry Yufb4b6472022-01-27 15:03:26 +080010#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_PROTO_TLS1_3)
Jerry Yu3cc4c2a2021-08-06 16:29:08 +080011
Jerry Yu687101b2021-09-14 16:03:56 +080012#include "mbedtls/debug.h"
Xiaofei Baicba64af2022-02-15 10:00:56 +000013#include "mbedtls/error.h"
14#include "mbedtls/platform.h"
Jerry Yu1c105562022-07-10 06:32:38 +000015#include "mbedtls/constant_time.h"
Manuel Pégourié-Gonnardd55d66f2023-06-20 10:14:58 +020016#include "mbedtls/oid.h"
Manuel Pégourié-Gonnard02b10d82023-03-28 12:33:20 +020017#include "md_psa.h"
Jerry Yu3bf2c642022-03-30 22:02:12 +080018
Xiaofei Bai9ca09d42022-02-14 12:57:18 +000019#include "ssl_misc.h"
20#include "ssl_tls13_keys.h"
21#include "ssl_debug_helpers.h"
22
Jerry Yuf35ba382022-08-23 17:58:26 +080023
Jerry Yuc5a23a02022-08-25 10:51:44 +080024static const mbedtls_ssl_ciphersuite_t *ssl_tls13_validate_peer_ciphersuite(
Gilles Peskine449bd832023-01-11 14:50:10 +010025 mbedtls_ssl_context *ssl,
26 unsigned int cipher_suite)
Jerry Yuf35ba382022-08-23 17:58:26 +080027{
28 const mbedtls_ssl_ciphersuite_t *ciphersuite_info;
Gilles Peskine449bd832023-01-11 14:50:10 +010029 if (!mbedtls_ssl_tls13_cipher_suite_is_offered(ssl, cipher_suite)) {
30 return NULL;
Jerry Yuf35ba382022-08-23 17:58:26 +080031 }
Gilles Peskine449bd832023-01-11 14:50:10 +010032
33 ciphersuite_info = mbedtls_ssl_ciphersuite_from_id(cipher_suite);
34 if ((mbedtls_ssl_validate_ciphersuite(ssl, ciphersuite_info,
35 ssl->tls_version,
36 ssl->tls_version) != 0)) {
37 return NULL;
38 }
39 return ciphersuite_info;
Jerry Yuf35ba382022-08-23 17:58:26 +080040}
41
Ronald Cron41a443a2022-10-04 16:38:25 +020042#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED)
Jerry Yue19e3b92022-07-08 12:04:51 +000043/* From RFC 8446:
44 *
45 * enum { psk_ke(0), psk_dhe_ke(1), (255) } PskKeyExchangeMode;
46 * struct {
47 * PskKeyExchangeMode ke_modes<1..255>;
48 * } PskKeyExchangeModes;
49 */
Jerry Yu6e74a7e2022-07-20 20:49:32 +080050MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +010051static int ssl_tls13_parse_key_exchange_modes_ext(mbedtls_ssl_context *ssl,
52 const unsigned char *buf,
53 const unsigned char *end)
Jerry Yue19e3b92022-07-08 12:04:51 +000054{
Jerry Yu299e31f2022-07-13 23:06:36 +080055 const unsigned char *p = buf;
Jerry Yue19e3b92022-07-08 12:04:51 +000056 size_t ke_modes_len;
57 int ke_modes = 0;
58
Jerry Yu854dd9e2022-07-15 14:28:27 +080059 /* Read ke_modes length (1 Byte) */
Gilles Peskine449bd832023-01-11 14:50:10 +010060 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, 1);
Jerry Yu299e31f2022-07-13 23:06:36 +080061 ke_modes_len = *p++;
Jerry Yue19e3b92022-07-08 12:04:51 +000062 /* Currently, there are only two PSK modes, so even without looking
63 * at the content, something's wrong if the list has more than 2 items. */
Gilles Peskine449bd832023-01-11 14:50:10 +010064 if (ke_modes_len > 2) {
65 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER,
66 MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER);
67 return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
Jerry Yu299e31f2022-07-13 23:06:36 +080068 }
Jerry Yue19e3b92022-07-08 12:04:51 +000069
Gilles Peskine449bd832023-01-11 14:50:10 +010070 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, ke_modes_len);
Jerry Yue19e3b92022-07-08 12:04:51 +000071
Gilles Peskine449bd832023-01-11 14:50:10 +010072 while (ke_modes_len-- != 0) {
73 switch (*p++) {
74 case MBEDTLS_SSL_TLS1_3_PSK_MODE_PURE:
75 ke_modes |= MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK;
76 MBEDTLS_SSL_DEBUG_MSG(3, ("Found PSK KEX MODE"));
77 break;
78 case MBEDTLS_SSL_TLS1_3_PSK_MODE_ECDHE:
79 ke_modes |= MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK_EPHEMERAL;
80 MBEDTLS_SSL_DEBUG_MSG(3, ("Found PSK_EPHEMERAL KEX MODE"));
81 break;
82 default:
83 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER,
84 MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER);
85 return MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER;
Jerry Yue19e3b92022-07-08 12:04:51 +000086 }
87 }
88
89 ssl->handshake->tls13_kex_modes = ke_modes;
Gilles Peskine449bd832023-01-11 14:50:10 +010090 return 0;
Jerry Yue19e3b92022-07-08 12:04:51 +000091}
Jerry Yu1c105562022-07-10 06:32:38 +000092
Jerry Yue9d4fc02022-08-20 19:21:15 +080093#define SSL_TLS1_3_OFFERED_PSK_NOT_MATCH 1
94#define SSL_TLS1_3_OFFERED_PSK_MATCH 0
Jerry Yu95699e72022-08-21 19:22:23 +080095
96#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Pengyu Lv29daf4a2023-10-30 17:13:30 +080097MBEDTLS_CHECK_RETURN_CRITICAL
98static int ssl_tls13_check_psk_key_exchange(mbedtls_ssl_context *ssl);
99MBEDTLS_CHECK_RETURN_CRITICAL
100static int ssl_tls13_check_psk_ephemeral_key_exchange(mbedtls_ssl_context *ssl);
Jerry Yu95699e72022-08-21 19:22:23 +0800101
102MBEDTLS_CHECK_RETURN_CRITICAL
103static int ssl_tls13_offered_psks_check_identity_match_ticket(
Gilles Peskine449bd832023-01-11 14:50:10 +0100104 mbedtls_ssl_context *ssl,
105 const unsigned char *identity,
106 size_t identity_len,
107 uint32_t obfuscated_ticket_age,
108 mbedtls_ssl_session *session)
Jerry Yu95699e72022-08-21 19:22:23 +0800109{
Jerry Yufd310eb2022-09-06 09:16:35 +0800110 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yu95699e72022-08-21 19:22:23 +0800111 unsigned char *ticket_buffer;
Pengyu Lv29daf4a2023-10-30 17:13:30 +0800112 unsigned int key_exchanges;
Jerry Yu8d4bbba2022-09-13 14:15:48 +0800113#if defined(MBEDTLS_HAVE_TIME)
114 mbedtls_time_t now;
Jerry Yuacff8232022-09-14 14:35:11 +0800115 uint64_t age_in_s;
Jerry Yuf7dad3c2022-09-14 22:31:39 +0800116 int64_t age_diff_in_ms;
Jerry Yu8d4bbba2022-09-13 14:15:48 +0800117#endif
Jerry Yu95699e72022-08-21 19:22:23 +0800118
119 ((void) obfuscated_ticket_age);
120
Gilles Peskine449bd832023-01-11 14:50:10 +0100121 MBEDTLS_SSL_DEBUG_MSG(2, ("=> check_identity_match_ticket"));
Jerry Yu95699e72022-08-21 19:22:23 +0800122
Jerry Yufd310eb2022-09-06 09:16:35 +0800123 /* Ticket parser is not configured, Skip */
Gilles Peskine449bd832023-01-11 14:50:10 +0100124 if (ssl->conf->f_ticket_parse == NULL || identity_len == 0) {
125 return 0;
126 }
Jerry Yu95699e72022-08-21 19:22:23 +0800127
Jerry Yu4746b102022-09-13 11:11:48 +0800128 /* We create a copy of the encrypted ticket since the ticket parsing
129 * function is allowed to use its input buffer as an output buffer
130 * (in-place decryption). We do, however, need the original buffer for
131 * computing the PSK binder value.
Jerry Yu95699e72022-08-21 19:22:23 +0800132 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100133 ticket_buffer = mbedtls_calloc(1, identity_len);
134 if (ticket_buffer == NULL) {
135 MBEDTLS_SSL_DEBUG_MSG(1, ("buffer too small"));
136 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
Jerry Yu95699e72022-08-21 19:22:23 +0800137 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100138 memcpy(ticket_buffer, identity, identity_len);
Jerry Yu95699e72022-08-21 19:22:23 +0800139
Gilles Peskine449bd832023-01-11 14:50:10 +0100140 if ((ret = ssl->conf->f_ticket_parse(ssl->conf->p_ticket,
141 session,
142 ticket_buffer, identity_len)) != 0) {
143 if (ret == MBEDTLS_ERR_SSL_INVALID_MAC) {
144 MBEDTLS_SSL_DEBUG_MSG(3, ("ticket is not authentic"));
145 } else if (ret == MBEDTLS_ERR_SSL_SESSION_TICKET_EXPIRED) {
146 MBEDTLS_SSL_DEBUG_MSG(3, ("ticket is expired"));
147 } else {
148 MBEDTLS_SSL_DEBUG_RET(1, "ticket_parse", ret);
149 }
Jerry Yu95699e72022-08-21 19:22:23 +0800150 }
151
152 /* We delete the temporary buffer */
Gilles Peskine449bd832023-01-11 14:50:10 +0100153 mbedtls_free(ticket_buffer);
Jerry Yu95699e72022-08-21 19:22:23 +0800154
Jerry Yuce3b95e2023-10-31 16:02:04 +0800155 if (ret == 0 && session->tls_version != MBEDTLS_SSL_VERSION_TLS1_3) {
Jerry Yu7ef9fd82023-11-07 14:30:38 +0800156 MBEDTLS_SSL_DEBUG_MSG(3, ("Ticket TLS version is not 1.3."));
157 /* TODO: Define new return value for this case. */
Jerry Yuce3b95e2023-10-31 16:02:04 +0800158 ret = MBEDTLS_ERR_SSL_BAD_PROTOCOL_VERSION;
159 }
Jerry Yuce3b95e2023-10-31 16:02:04 +0800160
Gilles Peskine449bd832023-01-11 14:50:10 +0100161 if (ret != 0) {
Jerry Yu8d4bbba2022-09-13 14:15:48 +0800162 goto exit;
163 }
Jerry Yu95699e72022-08-21 19:22:23 +0800164
Pengyu Lv3eb49be2022-12-05 16:35:12 +0800165 /* RFC 8446 section 4.2.9
166 *
167 * Servers SHOULD NOT send NewSessionTicket with tickets that are not
168 * compatible with the advertised modes; however, if a server does so,
169 * the impact will just be that the client's attempts at resumption fail.
170 *
171 * We regard the ticket with incompatible key exchange modes as not match.
172 */
Pengyu Lv18946532023-01-12 12:28:09 +0800173 ret = MBEDTLS_ERR_ERROR_GENERIC_ERROR;
Pengyu Lv29daf4a2023-10-30 17:13:30 +0800174 MBEDTLS_SSL_PRINT_TICKET_FLAGS(4, session->ticket_flags);
Pengyu Lv29daf4a2023-10-30 17:13:30 +0800175
176 key_exchanges = 0;
Pengyu Lvdbd1e0d2023-10-31 10:08:10 +0800177 if (mbedtls_ssl_session_ticket_allow_psk_ephemeral(session) &&
Pengyu Lv29daf4a2023-10-30 17:13:30 +0800178 ssl_tls13_check_psk_ephemeral_key_exchange(ssl)) {
179 key_exchanges |= MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK_EPHEMERAL;
180 }
Pengyu Lvdbd1e0d2023-10-31 10:08:10 +0800181 if (mbedtls_ssl_session_ticket_allow_psk(session) &&
Pengyu Lv29daf4a2023-10-30 17:13:30 +0800182 ssl_tls13_check_psk_key_exchange(ssl)) {
183 key_exchanges |= MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK;
184 }
185
186 if (key_exchanges == 0) {
Pengyu Lv3eb49be2022-12-05 16:35:12 +0800187 MBEDTLS_SSL_DEBUG_MSG(3, ("No suitable key exchange mode"));
188 goto exit;
189 }
190
Gilles Peskine449bd832023-01-11 14:50:10 +0100191 ret = MBEDTLS_ERR_SSL_SESSION_TICKET_EXPIRED;
192#if defined(MBEDTLS_HAVE_TIME)
193 now = mbedtls_time(NULL);
194
195 if (now < session->start) {
196 MBEDTLS_SSL_DEBUG_MSG(
197 3, ("Invalid ticket start time ( now=%" MBEDTLS_PRINTF_LONGLONG
198 ", start=%" MBEDTLS_PRINTF_LONGLONG " )",
199 (long long) now, (long long) session->start));
200 goto exit;
201 }
202
203 age_in_s = (uint64_t) (now - session->start);
Jerry Yu95699e72022-08-21 19:22:23 +0800204
Jerry Yu8d4bbba2022-09-13 14:15:48 +0800205 /* RFC 8446 section 4.6.1
206 *
207 * Servers MUST NOT use any value greater than 604800 seconds (7 days).
208 *
209 * RFC 8446 section 4.2.11.1
210 *
211 * Clients MUST NOT attempt to use tickets which have ages greater than
212 * the "ticket_lifetime" value which was provided with the ticket.
213 *
214 * For time being, the age MUST be less than 604800 seconds (7 days).
215 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100216 if (age_in_s > 604800) {
Jerry Yu8d4bbba2022-09-13 14:15:48 +0800217 MBEDTLS_SSL_DEBUG_MSG(
Gilles Peskine449bd832023-01-11 14:50:10 +0100218 3, ("Ticket age exceeds limitation ticket_age=%lu",
219 (long unsigned int) age_in_s));
Jerry Yu8d4bbba2022-09-13 14:15:48 +0800220 goto exit;
221 }
Jerry Yu95699e72022-08-21 19:22:23 +0800222
Jerry Yu8d4bbba2022-09-13 14:15:48 +0800223 /* RFC 8446 section 4.2.10
224 *
225 * For PSKs provisioned via NewSessionTicket, a server MUST validate that
226 * the ticket age for the selected PSK identity (computed by subtracting
227 * ticket_age_add from PskIdentity.obfuscated_ticket_age modulo 2^32) is
228 * within a small tolerance of the time since the ticket was issued.
Jerry Yuf7dad3c2022-09-14 22:31:39 +0800229 *
Jerry Yu0a55cc62022-09-15 16:15:06 +0800230 * NOTE: When `now == session->start`, `age_diff_in_ms` may be negative
231 * as the age units are different on the server (s) and in the
232 * client (ms) side. Add a -1000 ms tolerance window to take this
233 * into account.
Jerry Yu8d4bbba2022-09-13 14:15:48 +0800234 */
Jerry Yuf7dad3c2022-09-14 22:31:39 +0800235 age_diff_in_ms = age_in_s * 1000;
Gilles Peskine449bd832023-01-11 14:50:10 +0100236 age_diff_in_ms -= (obfuscated_ticket_age - session->ticket_age_add);
237 if (age_diff_in_ms <= -1000 ||
238 age_diff_in_ms > MBEDTLS_SSL_TLS1_3_TICKET_AGE_TOLERANCE) {
Jerry Yu8d4bbba2022-09-13 14:15:48 +0800239 MBEDTLS_SSL_DEBUG_MSG(
Gilles Peskine449bd832023-01-11 14:50:10 +0100240 3, ("Ticket age outside tolerance window ( diff=%d )",
241 (int) age_diff_in_ms));
Jerry Yu8d4bbba2022-09-13 14:15:48 +0800242 goto exit;
243 }
244
245 ret = 0;
Jerry Yu95699e72022-08-21 19:22:23 +0800246
247#endif /* MBEDTLS_HAVE_TIME */
Jerry Yu8d4bbba2022-09-13 14:15:48 +0800248
249exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100250 if (ret != 0) {
251 mbedtls_ssl_session_free(session);
252 }
Jerry Yu95699e72022-08-21 19:22:23 +0800253
Gilles Peskine449bd832023-01-11 14:50:10 +0100254 MBEDTLS_SSL_DEBUG_MSG(2, ("<= check_identity_match_ticket"));
255 return ret;
Jerry Yu95699e72022-08-21 19:22:23 +0800256}
257#endif /* MBEDTLS_SSL_SESSION_TICKETS */
258
Jerry Yu6e74a7e2022-07-20 20:49:32 +0800259MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yu1c105562022-07-10 06:32:38 +0000260static int ssl_tls13_offered_psks_check_identity_match(
Gilles Peskine449bd832023-01-11 14:50:10 +0100261 mbedtls_ssl_context *ssl,
262 const unsigned char *identity,
263 size_t identity_len,
264 uint32_t obfuscated_ticket_age,
265 int *psk_type,
266 mbedtls_ssl_session *session)
Jerry Yu1c105562022-07-10 06:32:38 +0000267{
Ronald Cron606671e2023-02-17 11:36:33 +0100268 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
269
Jerry Yu95699e72022-08-21 19:22:23 +0800270 ((void) session);
271 ((void) obfuscated_ticket_age);
Jerry Yu5725f1c2022-08-21 17:27:16 +0800272 *psk_type = MBEDTLS_SSL_TLS1_3_PSK_EXTERNAL;
Jerry Yu95699e72022-08-21 19:22:23 +0800273
Gilles Peskine449bd832023-01-11 14:50:10 +0100274 MBEDTLS_SSL_DEBUG_BUF(4, "identity", identity, identity_len);
Jerry Yu95699e72022-08-21 19:22:23 +0800275 ssl->handshake->resume = 0;
276
Jerry Yu95699e72022-08-21 19:22:23 +0800277#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Gilles Peskine449bd832023-01-11 14:50:10 +0100278 if (ssl_tls13_offered_psks_check_identity_match_ticket(
Jerry Yu8d4bbba2022-09-13 14:15:48 +0800279 ssl, identity, identity_len, obfuscated_ticket_age,
Gilles Peskine449bd832023-01-11 14:50:10 +0100280 session) == SSL_TLS1_3_OFFERED_PSK_MATCH) {
Jerry Yu95699e72022-08-21 19:22:23 +0800281 ssl->handshake->resume = 1;
282 *psk_type = MBEDTLS_SSL_TLS1_3_PSK_RESUMPTION;
Ronald Cron606671e2023-02-17 11:36:33 +0100283 ret = mbedtls_ssl_set_hs_psk(ssl,
284 session->resumption_key,
285 session->resumption_key_len);
286 if (ret != 0) {
287 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_set_hs_psk", ret);
288 return ret;
289 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100290
291 MBEDTLS_SSL_DEBUG_BUF(4, "Ticket-resumed PSK:",
292 session->resumption_key,
293 session->resumption_key_len);
294 MBEDTLS_SSL_DEBUG_MSG(4, ("ticket: obfuscated_ticket_age: %u",
295 (unsigned) obfuscated_ticket_age));
296 return SSL_TLS1_3_OFFERED_PSK_MATCH;
Jerry Yu95699e72022-08-21 19:22:23 +0800297 }
298#endif /* MBEDTLS_SSL_SESSION_TICKETS */
299
Jerry Yu1c105562022-07-10 06:32:38 +0000300 /* Check identity with external configured function */
Gilles Peskine449bd832023-01-11 14:50:10 +0100301 if (ssl->conf->f_psk != NULL) {
302 if (ssl->conf->f_psk(
303 ssl->conf->p_psk, ssl, identity, identity_len) == 0) {
304 return SSL_TLS1_3_OFFERED_PSK_MATCH;
Jerry Yu1c105562022-07-10 06:32:38 +0000305 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100306 return SSL_TLS1_3_OFFERED_PSK_NOT_MATCH;
Jerry Yu1c105562022-07-10 06:32:38 +0000307 }
308
Gilles Peskine449bd832023-01-11 14:50:10 +0100309 MBEDTLS_SSL_DEBUG_BUF(5, "identity", identity, identity_len);
Jerry Yu1c105562022-07-10 06:32:38 +0000310 /* Check identity with pre-configured psk */
Gilles Peskine449bd832023-01-11 14:50:10 +0100311 if (ssl->conf->psk_identity != NULL &&
Jerry Yu2f0abc92022-07-22 19:34:48 +0800312 identity_len == ssl->conf->psk_identity_len &&
Gilles Peskine449bd832023-01-11 14:50:10 +0100313 mbedtls_ct_memcmp(ssl->conf->psk_identity,
314 identity, identity_len) == 0) {
Ronald Cron606671e2023-02-17 11:36:33 +0100315 ret = mbedtls_ssl_set_hs_psk(ssl, ssl->conf->psk, ssl->conf->psk_len);
316 if (ret != 0) {
317 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_set_hs_psk", ret);
318 return ret;
319 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100320 return SSL_TLS1_3_OFFERED_PSK_MATCH;
Jerry Yu1c105562022-07-10 06:32:38 +0000321 }
322
Gilles Peskine449bd832023-01-11 14:50:10 +0100323 return SSL_TLS1_3_OFFERED_PSK_NOT_MATCH;
Jerry Yu1c105562022-07-10 06:32:38 +0000324}
325
Jerry Yu6e74a7e2022-07-20 20:49:32 +0800326MBEDTLS_CHECK_RETURN_CRITICAL
Xiaokang Qian9f1747b2023-03-30 02:50:04 +0000327static int ssl_tls13_offered_psks_check_binder_match(
328 mbedtls_ssl_context *ssl,
329 const unsigned char *binder, size_t binder_len,
330 int psk_type, psa_algorithm_t psk_hash_alg)
Jerry Yu1c105562022-07-10 06:32:38 +0000331{
332 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yu96a2e362022-07-21 15:11:34 +0800333
Jerry Yudaf375a2022-07-20 21:31:43 +0800334 unsigned char transcript[PSA_HASH_MAX_SIZE];
Jerry Yu1c105562022-07-10 06:32:38 +0000335 size_t transcript_len;
Jerry Yu2f0abc92022-07-22 19:34:48 +0800336 unsigned char *psk;
Jerry Yu96a2e362022-07-21 15:11:34 +0800337 size_t psk_len;
Jerry Yudaf375a2022-07-20 21:31:43 +0800338 unsigned char server_computed_binder[PSA_HASH_MAX_SIZE];
Jerry Yu1c105562022-07-10 06:32:38 +0000339
Jerry Yu1c105562022-07-10 06:32:38 +0000340 /* Get current state of handshake transcript. */
Jerry Yu29d9faa2022-08-23 17:52:45 +0800341 ret = mbedtls_ssl_get_handshake_transcript(
Manuel Pégourié-Gonnard2d6d9932023-03-28 11:38:08 +0200342 ssl, mbedtls_md_type_from_psa_alg(psk_hash_alg),
Gilles Peskine449bd832023-01-11 14:50:10 +0100343 transcript, sizeof(transcript), &transcript_len);
344 if (ret != 0) {
345 return ret;
346 }
Jerry Yu1c105562022-07-10 06:32:38 +0000347
Gilles Peskine449bd832023-01-11 14:50:10 +0100348 ret = mbedtls_ssl_tls13_export_handshake_psk(ssl, &psk, &psk_len);
349 if (ret != 0) {
350 return ret;
351 }
Jerry Yu96a2e362022-07-21 15:11:34 +0800352
Gilles Peskine449bd832023-01-11 14:50:10 +0100353 ret = mbedtls_ssl_tls13_create_psk_binder(ssl, psk_hash_alg,
354 psk, psk_len, psk_type,
355 transcript,
356 server_computed_binder);
Jerry Yu96a2e362022-07-21 15:11:34 +0800357#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +0100358 mbedtls_free((void *) psk);
Jerry Yu96a2e362022-07-21 15:11:34 +0800359#endif
Gilles Peskine449bd832023-01-11 14:50:10 +0100360 if (ret != 0) {
361 MBEDTLS_SSL_DEBUG_MSG(1, ("PSK binder calculation failed."));
362 return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
Jerry Yu1c105562022-07-10 06:32:38 +0000363 }
364
Gilles Peskine449bd832023-01-11 14:50:10 +0100365 MBEDTLS_SSL_DEBUG_BUF(3, "psk binder ( computed ): ",
366 server_computed_binder, transcript_len);
367 MBEDTLS_SSL_DEBUG_BUF(3, "psk binder ( received ): ", binder, binder_len);
Jerry Yu1c105562022-07-10 06:32:38 +0000368
Gilles Peskine449bd832023-01-11 14:50:10 +0100369 if (mbedtls_ct_memcmp(server_computed_binder, binder, binder_len) == 0) {
370 return SSL_TLS1_3_OFFERED_PSK_MATCH;
Jerry Yu1c105562022-07-10 06:32:38 +0000371 }
372
Gilles Peskine449bd832023-01-11 14:50:10 +0100373 mbedtls_platform_zeroize(server_computed_binder,
374 sizeof(server_computed_binder));
375 return SSL_TLS1_3_OFFERED_PSK_NOT_MATCH;
Jerry Yu1c105562022-07-10 06:32:38 +0000376}
Jerry Yu96a2e362022-07-21 15:11:34 +0800377
Jerry Yu5725f1c2022-08-21 17:27:16 +0800378MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yuf35ba382022-08-23 17:58:26 +0800379static int ssl_tls13_select_ciphersuite_for_psk(
Gilles Peskine449bd832023-01-11 14:50:10 +0100380 mbedtls_ssl_context *ssl,
381 const unsigned char *cipher_suites,
382 const unsigned char *cipher_suites_end,
383 uint16_t *selected_ciphersuite,
384 const mbedtls_ssl_ciphersuite_t **selected_ciphersuite_info)
Jerry Yu5725f1c2022-08-21 17:27:16 +0800385{
Jerry Yuf35ba382022-08-23 17:58:26 +0800386 psa_algorithm_t psk_hash_alg = PSA_ALG_SHA_256;
Jerry Yu5725f1c2022-08-21 17:27:16 +0800387
Jerry Yu0baf9072022-08-25 11:21:04 +0800388 *selected_ciphersuite = 0;
389 *selected_ciphersuite_info = NULL;
390
Jerry Yuf35ba382022-08-23 17:58:26 +0800391 /* RFC 8446, page 55.
392 *
393 * For externally established PSKs, the Hash algorithm MUST be set when the
394 * PSK is established or default to SHA-256 if no such algorithm is defined.
395 *
396 */
Jerry Yu5725f1c2022-08-21 17:27:16 +0800397
Jerry Yu5725f1c2022-08-21 17:27:16 +0800398 /*
399 * Search for a matching ciphersuite
400 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100401 for (const unsigned char *p = cipher_suites;
402 p < cipher_suites_end; p += 2) {
Jerry Yu5725f1c2022-08-21 17:27:16 +0800403 uint16_t cipher_suite;
Jerry Yuf35ba382022-08-23 17:58:26 +0800404 const mbedtls_ssl_ciphersuite_t *ciphersuite_info;
Jerry Yu5725f1c2022-08-21 17:27:16 +0800405
Gilles Peskine449bd832023-01-11 14:50:10 +0100406 cipher_suite = MBEDTLS_GET_UINT16_BE(p, 0);
407 ciphersuite_info = ssl_tls13_validate_peer_ciphersuite(ssl,
408 cipher_suite);
409 if (ciphersuite_info == NULL) {
Jerry Yu5725f1c2022-08-21 17:27:16 +0800410 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +0100411 }
Jerry Yu5725f1c2022-08-21 17:27:16 +0800412
Jerry Yu5725f1c2022-08-21 17:27:16 +0800413 /* MAC of selected ciphersuite MUST be same with PSK binder if exist.
414 * Otherwise, client should reject.
415 */
Dave Rodgman2eab4622023-10-05 13:30:37 +0100416 if (psk_hash_alg ==
417 mbedtls_md_psa_alg_from_type((mbedtls_md_type_t) ciphersuite_info->mac)) {
Jerry Yuf35ba382022-08-23 17:58:26 +0800418 *selected_ciphersuite = cipher_suite;
419 *selected_ciphersuite_info = ciphersuite_info;
Gilles Peskine449bd832023-01-11 14:50:10 +0100420 return 0;
Jerry Yuf35ba382022-08-23 17:58:26 +0800421 }
422 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100423 MBEDTLS_SSL_DEBUG_MSG(2, ("No matched ciphersuite"));
424 return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
Jerry Yuf35ba382022-08-23 17:58:26 +0800425}
Jerry Yu5725f1c2022-08-21 17:27:16 +0800426
Jerry Yu82534862022-08-30 10:42:33 +0800427#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Jerry Yuf35ba382022-08-23 17:58:26 +0800428MBEDTLS_CHECK_RETURN_CRITICAL
429static int ssl_tls13_select_ciphersuite_for_resumption(
Gilles Peskine449bd832023-01-11 14:50:10 +0100430 mbedtls_ssl_context *ssl,
431 const unsigned char *cipher_suites,
432 const unsigned char *cipher_suites_end,
433 mbedtls_ssl_session *session,
434 uint16_t *selected_ciphersuite,
435 const mbedtls_ssl_ciphersuite_t **selected_ciphersuite_info)
Jerry Yuf35ba382022-08-23 17:58:26 +0800436{
Jerry Yu82534862022-08-30 10:42:33 +0800437
Jerry Yuf35ba382022-08-23 17:58:26 +0800438 *selected_ciphersuite = 0;
439 *selected_ciphersuite_info = NULL;
Gilles Peskine449bd832023-01-11 14:50:10 +0100440 for (const unsigned char *p = cipher_suites; p < cipher_suites_end; p += 2) {
441 uint16_t cipher_suite = MBEDTLS_GET_UINT16_BE(p, 0);
Jerry Yu82534862022-08-30 10:42:33 +0800442 const mbedtls_ssl_ciphersuite_t *ciphersuite_info;
443
Gilles Peskine449bd832023-01-11 14:50:10 +0100444 if (cipher_suite != session->ciphersuite) {
Jerry Yu82534862022-08-30 10:42:33 +0800445 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +0100446 }
Jerry Yu82534862022-08-30 10:42:33 +0800447
Gilles Peskine449bd832023-01-11 14:50:10 +0100448 ciphersuite_info = ssl_tls13_validate_peer_ciphersuite(ssl,
449 cipher_suite);
450 if (ciphersuite_info == NULL) {
Jerry Yu82534862022-08-30 10:42:33 +0800451 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +0100452 }
Jerry Yu82534862022-08-30 10:42:33 +0800453
Jerry Yu4746b102022-09-13 11:11:48 +0800454 *selected_ciphersuite = cipher_suite;
Jerry Yu82534862022-08-30 10:42:33 +0800455 *selected_ciphersuite_info = ciphersuite_info;
456
Gilles Peskine449bd832023-01-11 14:50:10 +0100457 return 0;
Jerry Yu82534862022-08-30 10:42:33 +0800458 }
459
Gilles Peskine449bd832023-01-11 14:50:10 +0100460 return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
Jerry Yu5725f1c2022-08-21 17:27:16 +0800461}
Jerry Yuf35ba382022-08-23 17:58:26 +0800462
Jerry Yu82534862022-08-30 10:42:33 +0800463MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100464static int ssl_tls13_session_copy_ticket(mbedtls_ssl_session *dst,
465 const mbedtls_ssl_session *src)
Jerry Yu82534862022-08-30 10:42:33 +0800466{
Jerry Yu82534862022-08-30 10:42:33 +0800467 dst->ticket_age_add = src->ticket_age_add;
468 dst->ticket_flags = src->ticket_flags;
469 dst->resumption_key_len = src->resumption_key_len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100470 if (src->resumption_key_len == 0) {
471 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
472 }
473 memcpy(dst->resumption_key, src->resumption_key, src->resumption_key_len);
Jerry Yu4746b102022-09-13 11:11:48 +0800474
Gilles Peskine449bd832023-01-11 14:50:10 +0100475 return 0;
Jerry Yu82534862022-08-30 10:42:33 +0800476}
477#endif /* MBEDTLS_SSL_SESSION_TICKETS */
478
Jerry Yu1c105562022-07-10 06:32:38 +0000479/* Parser for pre_shared_key extension in client hello
480 * struct {
481 * opaque identity<1..2^16-1>;
482 * uint32 obfuscated_ticket_age;
483 * } PskIdentity;
484 *
485 * opaque PskBinderEntry<32..255>;
486 *
487 * struct {
488 * PskIdentity identities<7..2^16-1>;
489 * PskBinderEntry binders<33..2^16-1>;
490 * } OfferedPsks;
491 *
492 * struct {
493 * select (Handshake.msg_type) {
494 * case client_hello: OfferedPsks;
495 * ....
496 * };
497 * } PreSharedKeyExtension;
498 */
Jerry Yu6e74a7e2022-07-20 20:49:32 +0800499MBEDTLS_CHECK_RETURN_CRITICAL
Xiaokang Qian9f1747b2023-03-30 02:50:04 +0000500static int ssl_tls13_parse_pre_shared_key_ext(
501 mbedtls_ssl_context *ssl,
502 const unsigned char *pre_shared_key_ext,
503 const unsigned char *pre_shared_key_ext_end,
504 const unsigned char *ciphersuites,
505 const unsigned char *ciphersuites_end)
Jerry Yu1c105562022-07-10 06:32:38 +0000506{
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +0100507 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yu29d9faa2022-08-23 17:52:45 +0800508 const unsigned char *identities = pre_shared_key_ext;
Jerry Yu568ec252022-07-22 21:27:34 +0800509 const unsigned char *p_identity_len;
510 size_t identities_len;
Jerry Yu1c105562022-07-10 06:32:38 +0000511 const unsigned char *identities_end;
Jerry Yu568ec252022-07-22 21:27:34 +0800512 const unsigned char *binders;
513 const unsigned char *p_binder_len;
514 size_t binders_len;
Jerry Yu1c105562022-07-10 06:32:38 +0000515 const unsigned char *binders_end;
Jerry Yu96a2e362022-07-21 15:11:34 +0800516 int matched_identity = -1;
517 int identity_id = -1;
Jerry Yu1c105562022-07-10 06:32:38 +0000518
Gilles Peskine449bd832023-01-11 14:50:10 +0100519 MBEDTLS_SSL_DEBUG_BUF(3, "pre_shared_key extension",
520 pre_shared_key_ext,
521 pre_shared_key_ext_end - pre_shared_key_ext);
Jerry Yu1c105562022-07-10 06:32:38 +0000522
Jerry Yu96a2e362022-07-21 15:11:34 +0800523 /* identities_len 2 bytes
524 * identities_data >= 7 bytes
525 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100526 MBEDTLS_SSL_CHK_BUF_READ_PTR(identities, pre_shared_key_ext_end, 7 + 2);
527 identities_len = MBEDTLS_GET_UINT16_BE(identities, 0);
Jerry Yu568ec252022-07-22 21:27:34 +0800528 p_identity_len = identities + 2;
Gilles Peskine449bd832023-01-11 14:50:10 +0100529 MBEDTLS_SSL_CHK_BUF_READ_PTR(p_identity_len, pre_shared_key_ext_end,
530 identities_len);
Jerry Yu568ec252022-07-22 21:27:34 +0800531 identities_end = p_identity_len + identities_len;
Jerry Yu1c105562022-07-10 06:32:38 +0000532
Jerry Yu96a2e362022-07-21 15:11:34 +0800533 /* binders_len 2 bytes
534 * binders >= 33 bytes
535 */
Jerry Yu568ec252022-07-22 21:27:34 +0800536 binders = identities_end;
Gilles Peskine449bd832023-01-11 14:50:10 +0100537 MBEDTLS_SSL_CHK_BUF_READ_PTR(binders, pre_shared_key_ext_end, 33 + 2);
538 binders_len = MBEDTLS_GET_UINT16_BE(binders, 0);
Jerry Yu568ec252022-07-22 21:27:34 +0800539 p_binder_len = binders + 2;
Gilles Peskine449bd832023-01-11 14:50:10 +0100540 MBEDTLS_SSL_CHK_BUF_READ_PTR(p_binder_len, pre_shared_key_ext_end, binders_len);
Jerry Yu568ec252022-07-22 21:27:34 +0800541 binders_end = p_binder_len + binders_len;
Jerry Yu1c105562022-07-10 06:32:38 +0000542
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +0100543 ret = ssl->handshake->update_checksum(ssl, pre_shared_key_ext,
544 identities_end - pre_shared_key_ext);
545 if (0 != ret) {
546 MBEDTLS_SSL_DEBUG_RET(1, ("update_checksum"), ret);
547 return ret;
548 }
Jerry Yu96a2e362022-07-21 15:11:34 +0800549
Gilles Peskine449bd832023-01-11 14:50:10 +0100550 while (p_identity_len < identities_end && p_binder_len < binders_end) {
Jerry Yu1c105562022-07-10 06:32:38 +0000551 const unsigned char *identity;
Jerry Yu568ec252022-07-22 21:27:34 +0800552 size_t identity_len;
Jerry Yu82534862022-08-30 10:42:33 +0800553 uint32_t obfuscated_ticket_age;
Jerry Yu96a2e362022-07-21 15:11:34 +0800554 const unsigned char *binder;
Jerry Yu568ec252022-07-22 21:27:34 +0800555 size_t binder_len;
Jerry Yu5725f1c2022-08-21 17:27:16 +0800556 int psk_type;
557 uint16_t cipher_suite;
Jerry Yu29d9faa2022-08-23 17:52:45 +0800558 const mbedtls_ssl_ciphersuite_t *ciphersuite_info;
Jerry Yu82534862022-08-30 10:42:33 +0800559#if defined(MBEDTLS_SSL_SESSION_TICKETS)
560 mbedtls_ssl_session session;
Gilles Peskine449bd832023-01-11 14:50:10 +0100561 mbedtls_ssl_session_init(&session);
Jerry Yu82534862022-08-30 10:42:33 +0800562#endif
Jerry Yubb852022022-07-20 21:10:44 +0800563
Gilles Peskine449bd832023-01-11 14:50:10 +0100564 MBEDTLS_SSL_CHK_BUF_READ_PTR(p_identity_len, identities_end, 2 + 1 + 4);
565 identity_len = MBEDTLS_GET_UINT16_BE(p_identity_len, 0);
Jerry Yu568ec252022-07-22 21:27:34 +0800566 identity = p_identity_len + 2;
Gilles Peskine449bd832023-01-11 14:50:10 +0100567 MBEDTLS_SSL_CHK_BUF_READ_PTR(identity, identities_end, identity_len + 4);
568 obfuscated_ticket_age = MBEDTLS_GET_UINT32_BE(identity, identity_len);
Jerry Yu568ec252022-07-22 21:27:34 +0800569 p_identity_len += identity_len + 6;
Jerry Yu1c105562022-07-10 06:32:38 +0000570
Gilles Peskine449bd832023-01-11 14:50:10 +0100571 MBEDTLS_SSL_CHK_BUF_READ_PTR(p_binder_len, binders_end, 1 + 32);
Jerry Yu568ec252022-07-22 21:27:34 +0800572 binder_len = *p_binder_len;
573 binder = p_binder_len + 1;
Gilles Peskine449bd832023-01-11 14:50:10 +0100574 MBEDTLS_SSL_CHK_BUF_READ_PTR(binder, binders_end, binder_len);
Jerry Yu568ec252022-07-22 21:27:34 +0800575 p_binder_len += binder_len + 1;
Jerry Yu96a2e362022-07-21 15:11:34 +0800576
Jerry Yu96a2e362022-07-21 15:11:34 +0800577 identity_id++;
Gilles Peskine449bd832023-01-11 14:50:10 +0100578 if (matched_identity != -1) {
Jerry Yu1c105562022-07-10 06:32:38 +0000579 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +0100580 }
Jerry Yu1c105562022-07-10 06:32:38 +0000581
Jerry Yu96a2e362022-07-21 15:11:34 +0800582 ret = ssl_tls13_offered_psks_check_identity_match(
Gilles Peskine449bd832023-01-11 14:50:10 +0100583 ssl, identity, identity_len, obfuscated_ticket_age,
584 &psk_type, &session);
585 if (ret != SSL_TLS1_3_OFFERED_PSK_MATCH) {
Jerry Yu96a2e362022-07-21 15:11:34 +0800586 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +0100587 }
Jerry Yu96a2e362022-07-21 15:11:34 +0800588
Gilles Peskine449bd832023-01-11 14:50:10 +0100589 MBEDTLS_SSL_DEBUG_MSG(4, ("found matched identity"));
590 switch (psk_type) {
Jerry Yu0baf9072022-08-25 11:21:04 +0800591 case MBEDTLS_SSL_TLS1_3_PSK_EXTERNAL:
592 ret = ssl_tls13_select_ciphersuite_for_psk(
Gilles Peskine449bd832023-01-11 14:50:10 +0100593 ssl, ciphersuites, ciphersuites_end,
594 &cipher_suite, &ciphersuite_info);
Jerry Yu0baf9072022-08-25 11:21:04 +0800595 break;
596 case MBEDTLS_SSL_TLS1_3_PSK_RESUMPTION:
Jerry Yu82534862022-08-30 10:42:33 +0800597#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Jerry Yu0baf9072022-08-25 11:21:04 +0800598 ret = ssl_tls13_select_ciphersuite_for_resumption(
Gilles Peskine449bd832023-01-11 14:50:10 +0100599 ssl, ciphersuites, ciphersuites_end, &session,
600 &cipher_suite, &ciphersuite_info);
601 if (ret != 0) {
602 mbedtls_ssl_session_free(&session);
603 }
Jerry Yu82534862022-08-30 10:42:33 +0800604#else
605 ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
606#endif
Jerry Yu0baf9072022-08-25 11:21:04 +0800607 break;
608 default:
Gilles Peskine449bd832023-01-11 14:50:10 +0100609 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Jerry Yu0baf9072022-08-25 11:21:04 +0800610 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100611 if (ret != 0) {
Jerry Yuf35ba382022-08-23 17:58:26 +0800612 /* See below, no cipher_suite available, abort handshake */
613 MBEDTLS_SSL_PEND_FATAL_ALERT(
614 MBEDTLS_SSL_ALERT_MSG_DECRYPT_ERROR,
Gilles Peskine449bd832023-01-11 14:50:10 +0100615 MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE);
Jerry Yuf35ba382022-08-23 17:58:26 +0800616 MBEDTLS_SSL_DEBUG_RET(
Gilles Peskine449bd832023-01-11 14:50:10 +0100617 2, "ssl_tls13_select_ciphersuite", ret);
618 return ret;
Jerry Yu5725f1c2022-08-21 17:27:16 +0800619 }
620
Jerry Yu96a2e362022-07-21 15:11:34 +0800621 ret = ssl_tls13_offered_psks_check_binder_match(
Gilles Peskine449bd832023-01-11 14:50:10 +0100622 ssl, binder, binder_len, psk_type,
Dave Rodgman2eab4622023-10-05 13:30:37 +0100623 mbedtls_md_psa_alg_from_type((mbedtls_md_type_t) ciphersuite_info->mac));
Gilles Peskine449bd832023-01-11 14:50:10 +0100624 if (ret != SSL_TLS1_3_OFFERED_PSK_MATCH) {
Jerry Yuc5a23a02022-08-25 10:51:44 +0800625 /* For security reasons, the handshake should be aborted when we
626 * fail to validate a binder value. See RFC 8446 section 4.2.11.2
627 * and appendix E.6. */
Jerry Yu82534862022-08-30 10:42:33 +0800628#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Gilles Peskine449bd832023-01-11 14:50:10 +0100629 mbedtls_ssl_session_free(&session);
Jerry Yu82534862022-08-30 10:42:33 +0800630#endif
Gilles Peskine449bd832023-01-11 14:50:10 +0100631 MBEDTLS_SSL_DEBUG_MSG(3, ("Invalid binder."));
Xiaokang Qian9f1747b2023-03-30 02:50:04 +0000632 MBEDTLS_SSL_DEBUG_RET(
633 1, "ssl_tls13_offered_psks_check_binder_match", ret);
Jerry Yu96a2e362022-07-21 15:11:34 +0800634 MBEDTLS_SSL_PEND_FATAL_ALERT(
635 MBEDTLS_SSL_ALERT_MSG_DECRYPT_ERROR,
Gilles Peskine449bd832023-01-11 14:50:10 +0100636 MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE);
637 return ret;
Jerry Yu96a2e362022-07-21 15:11:34 +0800638 }
Jerry Yu96a2e362022-07-21 15:11:34 +0800639
640 matched_identity = identity_id;
Jerry Yu5725f1c2022-08-21 17:27:16 +0800641
642 /* Update handshake parameters */
Jerry Yue5834fd2022-08-29 20:16:09 +0800643 ssl->handshake->ciphersuite_info = ciphersuite_info;
Jerry Yu4746b102022-09-13 11:11:48 +0800644 ssl->session_negotiate->ciphersuite = cipher_suite;
Gilles Peskine449bd832023-01-11 14:50:10 +0100645 MBEDTLS_SSL_DEBUG_MSG(2, ("overwrite ciphersuite: %04x - %s",
646 cipher_suite, ciphersuite_info->name));
Jerry Yu82534862022-08-30 10:42:33 +0800647#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Gilles Peskine449bd832023-01-11 14:50:10 +0100648 if (psk_type == MBEDTLS_SSL_TLS1_3_PSK_RESUMPTION) {
649 ret = ssl_tls13_session_copy_ticket(ssl->session_negotiate,
650 &session);
651 mbedtls_ssl_session_free(&session);
652 if (ret != 0) {
653 return ret;
654 }
Jerry Yu82534862022-08-30 10:42:33 +0800655 }
656#endif /* MBEDTLS_SSL_SESSION_TICKETS */
Jerry Yu1c105562022-07-10 06:32:38 +0000657 }
658
Gilles Peskine449bd832023-01-11 14:50:10 +0100659 if (p_identity_len != identities_end || p_binder_len != binders_end) {
660 MBEDTLS_SSL_DEBUG_MSG(3, ("pre_shared_key extension decode error"));
661 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR,
662 MBEDTLS_ERR_SSL_DECODE_ERROR);
663 return MBEDTLS_ERR_SSL_DECODE_ERROR;
Jerry Yu1c105562022-07-10 06:32:38 +0000664 }
665
Jerry Yu6f1db3f2022-07-22 23:05:59 +0800666 /* Update the handshake transcript with the binder list. */
Xiaokang Qian9f1747b2023-03-30 02:50:04 +0000667 ret = ssl->handshake->update_checksum(
668 ssl, identities_end, (size_t) (binders_end - identities_end));
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +0100669 if (0 != ret) {
670 MBEDTLS_SSL_DEBUG_RET(1, ("update_checksum"), ret);
671 return ret;
672 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100673 if (matched_identity == -1) {
674 MBEDTLS_SSL_DEBUG_MSG(3, ("No matched PSK or ticket."));
675 return MBEDTLS_ERR_SSL_UNKNOWN_IDENTITY;
Jerry Yu1c105562022-07-10 06:32:38 +0000676 }
677
Gilles Peskine449bd832023-01-11 14:50:10 +0100678 ssl->handshake->selected_identity = (uint16_t) matched_identity;
679 MBEDTLS_SSL_DEBUG_MSG(3, ("Pre shared key found"));
Jerry Yu1c105562022-07-10 06:32:38 +0000680
Gilles Peskine449bd832023-01-11 14:50:10 +0100681 return 0;
Jerry Yu1c105562022-07-10 06:32:38 +0000682}
Jerry Yu032b15ce2022-07-11 06:10:03 +0000683
684/*
685 * struct {
686 * select ( Handshake.msg_type ) {
687 * ....
688 * case server_hello:
689 * uint16 selected_identity;
690 * }
691 * } PreSharedKeyExtension;
692 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100693static int ssl_tls13_write_server_pre_shared_key_ext(mbedtls_ssl_context *ssl,
694 unsigned char *buf,
695 unsigned char *end,
696 size_t *olen)
Jerry Yu032b15ce2022-07-11 06:10:03 +0000697{
Gilles Peskine449bd832023-01-11 14:50:10 +0100698 unsigned char *p = (unsigned char *) buf;
Jerry Yu032b15ce2022-07-11 06:10:03 +0000699
700 *olen = 0;
701
David Horstmann21b89762022-10-06 18:34:28 +0100702 int not_using_psk = 0;
Jerry Yu032b15ce2022-07-11 06:10:03 +0000703#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +0100704 not_using_psk = (mbedtls_svc_key_id_is_null(ssl->handshake->psk_opaque));
Jerry Yu032b15ce2022-07-11 06:10:03 +0000705#else
Gilles Peskine449bd832023-01-11 14:50:10 +0100706 not_using_psk = (ssl->handshake->psk == NULL);
Jerry Yu032b15ce2022-07-11 06:10:03 +0000707#endif
Gilles Peskine449bd832023-01-11 14:50:10 +0100708 if (not_using_psk) {
Jerry Yu032b15ce2022-07-11 06:10:03 +0000709 /* We shouldn't have called this extension writer unless we've
710 * chosen to use a PSK. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100711 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Jerry Yu032b15ce2022-07-11 06:10:03 +0000712 }
713
Gilles Peskine449bd832023-01-11 14:50:10 +0100714 MBEDTLS_SSL_DEBUG_MSG(3, ("server hello, adding pre_shared_key extension"));
715 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 6);
Jerry Yu032b15ce2022-07-11 06:10:03 +0000716
Gilles Peskine449bd832023-01-11 14:50:10 +0100717 MBEDTLS_PUT_UINT16_BE(MBEDTLS_TLS_EXT_PRE_SHARED_KEY, p, 0);
718 MBEDTLS_PUT_UINT16_BE(2, p, 2);
Jerry Yu032b15ce2022-07-11 06:10:03 +0000719
Gilles Peskine449bd832023-01-11 14:50:10 +0100720 MBEDTLS_PUT_UINT16_BE(ssl->handshake->selected_identity, p, 4);
Jerry Yu032b15ce2022-07-11 06:10:03 +0000721
722 *olen = 6;
723
Gilles Peskine449bd832023-01-11 14:50:10 +0100724 MBEDTLS_SSL_DEBUG_MSG(4, ("sent selected_identity: %u",
725 ssl->handshake->selected_identity));
Jerry Yu032b15ce2022-07-11 06:10:03 +0000726
Gilles Peskine449bd832023-01-11 14:50:10 +0100727 mbedtls_ssl_tls13_set_hs_sent_ext_mask(ssl, MBEDTLS_TLS_EXT_PRE_SHARED_KEY);
Jerry Yub95dd362022-11-08 21:19:34 +0800728
Gilles Peskine449bd832023-01-11 14:50:10 +0100729 return 0;
Jerry Yu032b15ce2022-07-11 06:10:03 +0000730}
731
Ronald Cron41a443a2022-10-04 16:38:25 +0200732#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED */
Jerry Yue19e3b92022-07-08 12:04:51 +0000733
XiaokangQian7807f9f2022-02-15 10:04:37 +0000734/* From RFC 8446:
735 * struct {
XiaokangQiancfd925f2022-04-14 07:10:37 +0000736 * ProtocolVersion versions<2..254>;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000737 * } SupportedVersions;
738 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200739MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100740static int ssl_tls13_parse_supported_versions_ext(mbedtls_ssl_context *ssl,
741 const unsigned char *buf,
742 const unsigned char *end)
XiaokangQian7807f9f2022-02-15 10:04:37 +0000743{
XiaokangQian7807f9f2022-02-15 10:04:37 +0000744 const unsigned char *p = buf;
XiaokangQian8f9dfe42022-04-15 02:52:39 +0000745 size_t versions_len;
XiaokangQian4080a7f2022-04-11 09:55:18 +0000746 const unsigned char *versions_end;
XiaokangQian0a1b54e2022-04-21 03:01:38 +0000747 uint16_t tls_version;
Ronald Cron5af4c7f2023-03-07 20:46:59 +0100748 int found_supported_version = 0;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000749
Gilles Peskine449bd832023-01-11 14:50:10 +0100750 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, 1);
XiaokangQian4080a7f2022-04-11 09:55:18 +0000751 versions_len = p[0];
XiaokangQian7807f9f2022-02-15 10:04:37 +0000752 p += 1;
753
Gilles Peskine449bd832023-01-11 14:50:10 +0100754 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, versions_len);
XiaokangQian4080a7f2022-04-11 09:55:18 +0000755 versions_end = p + versions_len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100756 while (p < versions_end) {
757 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, versions_end, 2);
758 tls_version = mbedtls_ssl_read_version(p, ssl->conf->transport);
XiaokangQianb67384d2022-04-19 00:02:38 +0000759 p += 2;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000760
Ronald Cron3bd2b022023-04-03 16:45:39 +0200761 if (MBEDTLS_SSL_VERSION_TLS1_3 == tls_version) {
Ronald Cron5af4c7f2023-03-07 20:46:59 +0100762 found_supported_version = 1;
763 break;
764 }
765
Ronald Cron3bd2b022023-04-03 16:45:39 +0200766 if ((MBEDTLS_SSL_VERSION_TLS1_2 == tls_version) &&
767 mbedtls_ssl_conf_is_tls12_enabled(ssl->conf)) {
Ronald Cron5af4c7f2023-03-07 20:46:59 +0100768 found_supported_version = 1;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000769 break;
770 }
XiaokangQian7807f9f2022-02-15 10:04:37 +0000771 }
772
Ronald Cron5af4c7f2023-03-07 20:46:59 +0100773 if (!found_supported_version) {
774 MBEDTLS_SSL_DEBUG_MSG(1, ("No supported version found."));
XiaokangQian7807f9f2022-02-15 10:04:37 +0000775
Gilles Peskine449bd832023-01-11 14:50:10 +0100776 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_PROTOCOL_VERSION,
777 MBEDTLS_ERR_SSL_BAD_PROTOCOL_VERSION);
778 return MBEDTLS_ERR_SSL_BAD_PROTOCOL_VERSION;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000779 }
780
Ronald Cron5af4c7f2023-03-07 20:46:59 +0100781 MBEDTLS_SSL_DEBUG_MSG(1, ("Negotiated version: [%04x]",
Gilles Peskine449bd832023-01-11 14:50:10 +0100782 (unsigned int) tls_version));
XiaokangQian7807f9f2022-02-15 10:04:37 +0000783
Ronald Cron5af4c7f2023-03-07 20:46:59 +0100784 return (int) tls_version;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000785}
786
Przemek Stekiel8c0a9532023-06-15 16:48:19 +0200787#if defined(PSA_WANT_ALG_ECDH) || defined(PSA_WANT_ALG_FFDH)
XiaokangQiane8ff3502022-04-22 02:34:40 +0000788/*
XiaokangQian7807f9f2022-02-15 10:04:37 +0000789 *
790 * From RFC 8446:
791 * enum {
792 * ... (0xFFFF)
793 * } NamedGroup;
794 * struct {
795 * NamedGroup named_group_list<2..2^16-1>;
796 * } NamedGroupList;
797 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200798MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100799static int ssl_tls13_parse_supported_groups_ext(mbedtls_ssl_context *ssl,
800 const unsigned char *buf,
801 const unsigned char *end)
XiaokangQian7807f9f2022-02-15 10:04:37 +0000802{
XiaokangQian7807f9f2022-02-15 10:04:37 +0000803 const unsigned char *p = buf;
XiaokangQian84823772022-04-19 07:57:30 +0000804 size_t named_group_list_len;
XiaokangQian8f9dfe42022-04-15 02:52:39 +0000805 const unsigned char *named_group_list_end;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000806
Gilles Peskine449bd832023-01-11 14:50:10 +0100807 MBEDTLS_SSL_DEBUG_BUF(3, "supported_groups extension", p, end - buf);
808 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, 2);
809 named_group_list_len = MBEDTLS_GET_UINT16_BE(p, 0);
XiaokangQian7807f9f2022-02-15 10:04:37 +0000810 p += 2;
Gilles Peskine449bd832023-01-11 14:50:10 +0100811 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, named_group_list_len);
XiaokangQian8f9dfe42022-04-15 02:52:39 +0000812 named_group_list_end = p + named_group_list_len;
XiaokangQian4e8cd7b2022-04-21 09:48:09 +0000813 ssl->handshake->hrr_selected_group = 0;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000814
Gilles Peskine449bd832023-01-11 14:50:10 +0100815 while (p < named_group_list_end) {
XiaokangQian08037552022-04-20 07:16:41 +0000816 uint16_t named_group;
Gilles Peskine449bd832023-01-11 14:50:10 +0100817 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, named_group_list_end, 2);
818 named_group = MBEDTLS_GET_UINT16_BE(p, 0);
XiaokangQian08037552022-04-20 07:16:41 +0000819 p += 2;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000820
Gilles Peskine449bd832023-01-11 14:50:10 +0100821 MBEDTLS_SSL_DEBUG_MSG(2,
822 ("got named group: %s(%04x)",
823 mbedtls_ssl_named_group_to_str(named_group),
824 named_group));
XiaokangQian08037552022-04-20 07:16:41 +0000825
Gilles Peskine449bd832023-01-11 14:50:10 +0100826 if (!mbedtls_ssl_named_group_is_offered(ssl, named_group) ||
827 !mbedtls_ssl_named_group_is_supported(named_group) ||
828 ssl->handshake->hrr_selected_group != 0) {
XiaokangQian08037552022-04-20 07:16:41 +0000829 continue;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000830 }
831
Gilles Peskine449bd832023-01-11 14:50:10 +0100832 MBEDTLS_SSL_DEBUG_MSG(2,
833 ("add named group %s(%04x) into received list.",
834 mbedtls_ssl_named_group_to_str(named_group),
835 named_group));
Jerry Yuc1be19f2022-04-23 16:11:39 +0800836
XiaokangQian4e8cd7b2022-04-21 09:48:09 +0000837 ssl->handshake->hrr_selected_group = named_group;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000838 }
839
Gilles Peskine449bd832023-01-11 14:50:10 +0100840 return 0;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000841
842}
Przemek Stekiel8c0a9532023-06-15 16:48:19 +0200843#endif /* PSA_WANT_ALG_ECDH || PSA_WANT_ALG_FFDH */
XiaokangQian7807f9f2022-02-15 10:04:37 +0000844
XiaokangQian08037552022-04-20 07:16:41 +0000845#define SSL_TLS1_3_PARSE_KEY_SHARES_EXT_NO_MATCH 1
846
Valerio Settic9ae8622023-07-25 11:23:50 +0200847#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_EPHEMERAL_ENABLED)
XiaokangQian7807f9f2022-02-15 10:04:37 +0000848/*
849 * ssl_tls13_parse_key_shares_ext() verifies whether the information in the
Xiaokang Qian9f1747b2023-03-30 02:50:04 +0000850 * extension is correct and stores the first acceptable key share and its
851 * associated group.
XiaokangQian7807f9f2022-02-15 10:04:37 +0000852 *
853 * Possible return values are:
854 * - 0: Successful processing of the client provided key share extension.
Xiaokang Qian9f1747b2023-03-30 02:50:04 +0000855 * - SSL_TLS1_3_PARSE_KEY_SHARES_EXT_NO_MATCH: The key shares provided by
856 * the client does not match a group supported by the server. A
857 * HelloRetryRequest will be needed.
XiaokangQiane8ff3502022-04-22 02:34:40 +0000858 * - A negative value for fatal errors.
Jerry Yuc1be19f2022-04-23 16:11:39 +0800859 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200860MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100861static int ssl_tls13_parse_key_shares_ext(mbedtls_ssl_context *ssl,
862 const unsigned char *buf,
863 const unsigned char *end)
XiaokangQian7807f9f2022-02-15 10:04:37 +0000864{
XiaokangQianb67384d2022-04-19 00:02:38 +0000865 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000866 unsigned char const *p = buf;
XiaokangQian8f9dfe42022-04-15 02:52:39 +0000867 unsigned char const *client_shares_end;
Jerry Yuc1be19f2022-04-23 16:11:39 +0800868 size_t client_shares_len;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000869
870 /* From RFC 8446:
871 *
872 * struct {
873 * KeyShareEntry client_shares<0..2^16-1>;
874 * } KeyShareClientHello;
875 *
876 */
877
Gilles Peskine449bd832023-01-11 14:50:10 +0100878 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, 2);
879 client_shares_len = MBEDTLS_GET_UINT16_BE(p, 0);
XiaokangQian7807f9f2022-02-15 10:04:37 +0000880 p += 2;
Gilles Peskine449bd832023-01-11 14:50:10 +0100881 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, client_shares_len);
XiaokangQian7807f9f2022-02-15 10:04:37 +0000882
883 ssl->handshake->offered_group_id = 0;
XiaokangQian8f9dfe42022-04-15 02:52:39 +0000884 client_shares_end = p + client_shares_len;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000885
886 /* We try to find a suitable key share entry and copy it to the
887 * handshake context. Later, we have to find out whether we can do
888 * something with the provided key share or whether we have to
XiaokangQianc5763b52022-04-02 03:34:37 +0000889 * dismiss it and send a HelloRetryRequest message.
890 */
XiaokangQian7807f9f2022-02-15 10:04:37 +0000891
Gilles Peskine449bd832023-01-11 14:50:10 +0100892 while (p < client_shares_end) {
XiaokangQian9b5d04b2022-04-10 10:20:43 +0000893 uint16_t group;
Jerry Yuc1be19f2022-04-23 16:11:39 +0800894 size_t key_exchange_len;
Jerry Yu086edc22022-05-05 10:50:38 +0800895 const unsigned char *key_exchange;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000896
897 /*
898 * struct {
899 * NamedGroup group;
900 * opaque key_exchange<1..2^16-1>;
901 * } KeyShareEntry;
902 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100903 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, client_shares_end, 4);
904 group = MBEDTLS_GET_UINT16_BE(p, 0);
905 key_exchange_len = MBEDTLS_GET_UINT16_BE(p, 2);
Jerry Yuc1be19f2022-04-23 16:11:39 +0800906 p += 4;
Jerry Yu086edc22022-05-05 10:50:38 +0800907 key_exchange = p;
Gilles Peskine449bd832023-01-11 14:50:10 +0100908 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, client_shares_end, key_exchange_len);
Jerry Yu086edc22022-05-05 10:50:38 +0800909 p += key_exchange_len;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000910
911 /* Continue parsing even if we have already found a match,
XiaokangQianc5763b52022-04-02 03:34:37 +0000912 * for input validation purposes.
913 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100914 if (!mbedtls_ssl_named_group_is_offered(ssl, group) ||
915 !mbedtls_ssl_named_group_is_supported(group) ||
916 ssl->handshake->offered_group_id != 0) {
XiaokangQian060d8672022-04-21 09:24:56 +0000917 continue;
918 }
XiaokangQian060d8672022-04-21 09:24:56 +0000919
XiaokangQian7807f9f2022-02-15 10:04:37 +0000920 /*
Przemek Stekielc89f3ea2023-05-18 15:45:53 +0200921 * ECDHE and FFDHE groups are supported
XiaokangQian7807f9f2022-02-15 10:04:37 +0000922 */
Przemek Stekielc89f3ea2023-05-18 15:45:53 +0200923 if (mbedtls_ssl_tls13_named_group_is_ecdhe(group) ||
Przemek Stekield5f79e72023-06-29 09:08:43 +0200924 mbedtls_ssl_tls13_named_group_is_ffdh(group)) {
Przemek Stekielc89f3ea2023-05-18 15:45:53 +0200925 MBEDTLS_SSL_DEBUG_MSG(2, ("ECDH/FFDH group: %s (%04x)",
Gilles Peskine449bd832023-01-11 14:50:10 +0100926 mbedtls_ssl_named_group_to_str(group),
927 group));
Przemek Stekiel7ac93be2023-07-04 10:02:38 +0200928 ret = mbedtls_ssl_tls13_read_public_xxdhe_share(
Gilles Peskine449bd832023-01-11 14:50:10 +0100929 ssl, key_exchange - 2, key_exchange_len + 2);
930 if (ret != 0) {
931 return ret;
932 }
XiaokangQian4e8cd7b2022-04-21 09:48:09 +0000933
Gilles Peskine449bd832023-01-11 14:50:10 +0100934 } else {
935 MBEDTLS_SSL_DEBUG_MSG(4, ("Unrecognized NamedGroup %u",
936 (unsigned) group));
XiaokangQian7807f9f2022-02-15 10:04:37 +0000937 continue;
938 }
939
XiaokangQian9b5d04b2022-04-10 10:20:43 +0000940 ssl->handshake->offered_group_id = group;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000941 }
942
Jerry Yuc1be19f2022-04-23 16:11:39 +0800943
Gilles Peskine449bd832023-01-11 14:50:10 +0100944 if (ssl->handshake->offered_group_id == 0) {
945 MBEDTLS_SSL_DEBUG_MSG(1, ("no matching key share"));
946 return SSL_TLS1_3_PARSE_KEY_SHARES_EXT_NO_MATCH;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000947 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100948 return 0;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000949}
Valerio Settic9ae8622023-07-25 11:23:50 +0200950#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_EPHEMERAL_ENABLED */
XiaokangQian7807f9f2022-02-15 10:04:37 +0000951
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200952MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100953static int ssl_tls13_client_hello_has_exts(mbedtls_ssl_context *ssl,
954 int exts_mask)
XiaokangQian7807f9f2022-02-15 10:04:37 +0000955{
Jerry Yu0c354a22022-08-29 15:25:36 +0800956 int masked = ssl->handshake->received_extensions & exts_mask;
Gilles Peskine449bd832023-01-11 14:50:10 +0100957 return masked == exts_mask;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000958}
959
Jerry Yue5991322022-11-07 14:03:44 +0800960#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200961MBEDTLS_CHECK_RETURN_CRITICAL
XiaokangQianb67384d2022-04-19 00:02:38 +0000962static int ssl_tls13_client_hello_has_exts_for_ephemeral_key_exchange(
Gilles Peskine449bd832023-01-11 14:50:10 +0100963 mbedtls_ssl_context *ssl)
XiaokangQian7807f9f2022-02-15 10:04:37 +0000964{
Gilles Peskine449bd832023-01-11 14:50:10 +0100965 return ssl_tls13_client_hello_has_exts(
966 ssl,
967 MBEDTLS_SSL_EXT_MASK(SUPPORTED_GROUPS) |
968 MBEDTLS_SSL_EXT_MASK(KEY_SHARE) |
969 MBEDTLS_SSL_EXT_MASK(SIG_ALG));
XiaokangQian7807f9f2022-02-15 10:04:37 +0000970}
Jerry Yue5991322022-11-07 14:03:44 +0800971#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED */
XiaokangQian7807f9f2022-02-15 10:04:37 +0000972
Jerry Yue5991322022-11-07 14:03:44 +0800973#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK_ENABLED)
Jerry Yu77f01482022-07-11 07:03:24 +0000974MBEDTLS_CHECK_RETURN_CRITICAL
975static int ssl_tls13_client_hello_has_exts_for_psk_key_exchange(
Gilles Peskine449bd832023-01-11 14:50:10 +0100976 mbedtls_ssl_context *ssl)
Jerry Yu77f01482022-07-11 07:03:24 +0000977{
Gilles Peskine449bd832023-01-11 14:50:10 +0100978 return ssl_tls13_client_hello_has_exts(
979 ssl,
980 MBEDTLS_SSL_EXT_MASK(PRE_SHARED_KEY) |
981 MBEDTLS_SSL_EXT_MASK(PSK_KEY_EXCHANGE_MODES));
Jerry Yu77f01482022-07-11 07:03:24 +0000982}
Jerry Yue5991322022-11-07 14:03:44 +0800983#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK_ENABLED */
Jerry Yu77f01482022-07-11 07:03:24 +0000984
Jerry Yue5991322022-11-07 14:03:44 +0800985#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK_EPHEMERAL_ENABLED)
Jerry Yu77f01482022-07-11 07:03:24 +0000986MBEDTLS_CHECK_RETURN_CRITICAL
987static int ssl_tls13_client_hello_has_exts_for_psk_ephemeral_key_exchange(
Gilles Peskine449bd832023-01-11 14:50:10 +0100988 mbedtls_ssl_context *ssl)
Jerry Yu77f01482022-07-11 07:03:24 +0000989{
Gilles Peskine449bd832023-01-11 14:50:10 +0100990 return ssl_tls13_client_hello_has_exts(
991 ssl,
992 MBEDTLS_SSL_EXT_MASK(SUPPORTED_GROUPS) |
993 MBEDTLS_SSL_EXT_MASK(KEY_SHARE) |
994 MBEDTLS_SSL_EXT_MASK(PRE_SHARED_KEY) |
995 MBEDTLS_SSL_EXT_MASK(PSK_KEY_EXCHANGE_MODES));
Jerry Yu77f01482022-07-11 07:03:24 +0000996}
Jerry Yue5991322022-11-07 14:03:44 +0800997#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK_EPHEMERAL_ENABLED */
Jerry Yu77f01482022-07-11 07:03:24 +0000998
Pengyu Lv306a01d2023-02-02 16:35:47 +0800999#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED)
1000MBEDTLS_CHECK_RETURN_CRITICAL
Pengyu Lv52ad3332023-02-14 14:32:37 +08001001static int ssl_tls13_ticket_permission_check(mbedtls_ssl_context *ssl,
1002 unsigned int kex_mode)
Pengyu Lv306a01d2023-02-02 16:35:47 +08001003{
1004#if defined(MBEDTLS_SSL_SESSION_TICKETS)
1005 if (ssl->handshake->resume) {
Pengyu Lv7b711712023-10-24 17:07:14 +08001006 if (mbedtls_ssl_session_check_ticket_flags(
Pengyu Lv306a01d2023-02-02 16:35:47 +08001007 ssl->session_negotiate, kex_mode)) {
1008 return 0;
1009 }
1010 }
1011#else
1012 ((void) ssl);
1013 ((void) kex_mode);
1014#endif
1015 return 1;
1016}
1017#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED */
1018
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001019MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01001020static int ssl_tls13_check_ephemeral_key_exchange(mbedtls_ssl_context *ssl)
XiaokangQian7807f9f2022-02-15 10:04:37 +00001021{
Jerry Yue5991322022-11-07 14:03:44 +08001022#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01001023 return mbedtls_ssl_conf_tls13_ephemeral_enabled(ssl) &&
1024 ssl_tls13_client_hello_has_exts_for_ephemeral_key_exchange(ssl);
Jerry Yue5991322022-11-07 14:03:44 +08001025#else
1026 ((void) ssl);
Gilles Peskine449bd832023-01-11 14:50:10 +01001027 return 0;
Jerry Yue5991322022-11-07 14:03:44 +08001028#endif
Jerry Yu77f01482022-07-11 07:03:24 +00001029}
XiaokangQian7807f9f2022-02-15 10:04:37 +00001030
Jerry Yu77f01482022-07-11 07:03:24 +00001031MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01001032static int ssl_tls13_check_psk_key_exchange(mbedtls_ssl_context *ssl)
Jerry Yu77f01482022-07-11 07:03:24 +00001033{
Jerry Yue5991322022-11-07 14:03:44 +08001034#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK_ENABLED)
Pengyu Lv52ad3332023-02-14 14:32:37 +08001035 return ssl_tls13_ticket_permission_check(
Pengyu Lv306a01d2023-02-02 16:35:47 +08001036 ssl, MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK) &&
1037 mbedtls_ssl_conf_tls13_psk_enabled(ssl) &&
Gilles Peskine449bd832023-01-11 14:50:10 +01001038 mbedtls_ssl_tls13_psk_enabled(ssl) &&
1039 ssl_tls13_client_hello_has_exts_for_psk_key_exchange(ssl);
Jerry Yu77f01482022-07-11 07:03:24 +00001040#else
1041 ((void) ssl);
Gilles Peskine449bd832023-01-11 14:50:10 +01001042 return 0;
Jerry Yu77f01482022-07-11 07:03:24 +00001043#endif
1044}
XiaokangQian7807f9f2022-02-15 10:04:37 +00001045
Jerry Yu77f01482022-07-11 07:03:24 +00001046MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01001047static int ssl_tls13_check_psk_ephemeral_key_exchange(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_EPHEMERAL_ENABLED)
Pengyu Lv52ad3332023-02-14 14:32:37 +08001050 return ssl_tls13_ticket_permission_check(
Pengyu Lv306a01d2023-02-02 16:35:47 +08001051 ssl, MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK_EPHEMERAL) &&
1052 mbedtls_ssl_conf_tls13_psk_ephemeral_enabled(ssl) &&
Gilles Peskine449bd832023-01-11 14:50:10 +01001053 mbedtls_ssl_tls13_psk_ephemeral_enabled(ssl) &&
1054 ssl_tls13_client_hello_has_exts_for_psk_ephemeral_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}
1060
Gilles Peskine449bd832023-01-11 14:50:10 +01001061static int ssl_tls13_determine_key_exchange_mode(mbedtls_ssl_context *ssl)
Jerry Yu77f01482022-07-11 07:03:24 +00001062{
1063 /*
1064 * Determine the key exchange algorithm to use.
1065 * There are three types of key exchanges supported in TLS 1.3:
1066 * - (EC)DH with ECDSA,
1067 * - (EC)DH with PSK,
1068 * - plain PSK.
1069 *
1070 * The PSK-based key exchanges may additionally be used with 0-RTT.
1071 *
1072 * Our built-in order of preference is
Jerry Yuce6ed702022-07-22 21:49:53 +08001073 * 1 ) (EC)DHE-PSK Mode ( psk_ephemeral )
1074 * 2 ) Certificate Mode ( ephemeral )
1075 * 3 ) Plain PSK Mode ( psk )
Jerry Yu77f01482022-07-11 07:03:24 +00001076 */
1077
Xiaokang Qian9f1747b2023-03-30 02:50:04 +00001078 ssl->handshake->key_exchange_mode =
1079 MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_NONE;
Jerry Yu77f01482022-07-11 07:03:24 +00001080
Gilles Peskine449bd832023-01-11 14:50:10 +01001081 if (ssl_tls13_check_psk_ephemeral_key_exchange(ssl)) {
Jerry Yu77f01482022-07-11 07:03:24 +00001082 ssl->handshake->key_exchange_mode =
1083 MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK_EPHEMERAL;
Gilles Peskine449bd832023-01-11 14:50:10 +01001084 MBEDTLS_SSL_DEBUG_MSG(2, ("key exchange mode: psk_ephemeral"));
1085 } else
1086 if (ssl_tls13_check_ephemeral_key_exchange(ssl)) {
Jerry Yu77f01482022-07-11 07:03:24 +00001087 ssl->handshake->key_exchange_mode =
1088 MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL;
Gilles Peskine449bd832023-01-11 14:50:10 +01001089 MBEDTLS_SSL_DEBUG_MSG(2, ("key exchange mode: ephemeral"));
1090 } else
1091 if (ssl_tls13_check_psk_key_exchange(ssl)) {
Jerry Yuce6ed702022-07-22 21:49:53 +08001092 ssl->handshake->key_exchange_mode =
1093 MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK;
Gilles Peskine449bd832023-01-11 14:50:10 +01001094 MBEDTLS_SSL_DEBUG_MSG(2, ("key exchange mode: psk"));
1095 } else {
Jerry Yu77f01482022-07-11 07:03:24 +00001096 MBEDTLS_SSL_DEBUG_MSG(
Gilles Peskine449bd832023-01-11 14:50:10 +01001097 1,
1098 ("ClientHello message misses mandatory extensions."));
1099 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_MISSING_EXTENSION,
1100 MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER);
1101 return MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER;
Jerry Yu77f01482022-07-11 07:03:24 +00001102 }
1103
Gilles Peskine449bd832023-01-11 14:50:10 +01001104 return 0;
Jerry Yu77f01482022-07-11 07:03:24 +00001105
XiaokangQian7807f9f2022-02-15 10:04:37 +00001106}
1107
XiaokangQian81802f42022-06-10 13:25:22 +00001108#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
Ronald Cron928cbd32022-10-04 16:14:26 +02001109 defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED)
Ronald Cron67ea2542022-09-15 17:34:42 +02001110
1111#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01001112static psa_algorithm_t ssl_tls13_iana_sig_alg_to_psa_alg(uint16_t sig_alg)
Ronald Cron67ea2542022-09-15 17:34:42 +02001113{
Gilles Peskine449bd832023-01-11 14:50:10 +01001114 switch (sig_alg) {
Ronald Cron67ea2542022-09-15 17:34:42 +02001115 case MBEDTLS_TLS1_3_SIG_ECDSA_SECP256R1_SHA256:
Gilles Peskine449bd832023-01-11 14:50:10 +01001116 return PSA_ALG_ECDSA(PSA_ALG_SHA_256);
Ronald Cron67ea2542022-09-15 17:34:42 +02001117 case MBEDTLS_TLS1_3_SIG_ECDSA_SECP384R1_SHA384:
Gilles Peskine449bd832023-01-11 14:50:10 +01001118 return PSA_ALG_ECDSA(PSA_ALG_SHA_384);
Ronald Cron67ea2542022-09-15 17:34:42 +02001119 case MBEDTLS_TLS1_3_SIG_ECDSA_SECP521R1_SHA512:
Gilles Peskine449bd832023-01-11 14:50:10 +01001120 return PSA_ALG_ECDSA(PSA_ALG_SHA_512);
Ronald Cron67ea2542022-09-15 17:34:42 +02001121 case MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA256:
Gilles Peskine449bd832023-01-11 14:50:10 +01001122 return PSA_ALG_RSA_PSS(PSA_ALG_SHA_256);
Ronald Cron67ea2542022-09-15 17:34:42 +02001123 case MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA384:
Gilles Peskine449bd832023-01-11 14:50:10 +01001124 return PSA_ALG_RSA_PSS(PSA_ALG_SHA_384);
Ronald Cron67ea2542022-09-15 17:34:42 +02001125 case MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA512:
Gilles Peskine449bd832023-01-11 14:50:10 +01001126 return PSA_ALG_RSA_PSS(PSA_ALG_SHA_512);
Ronald Cron67ea2542022-09-15 17:34:42 +02001127 case MBEDTLS_TLS1_3_SIG_RSA_PKCS1_SHA256:
Gilles Peskine449bd832023-01-11 14:50:10 +01001128 return PSA_ALG_RSA_PKCS1V15_SIGN(PSA_ALG_SHA_256);
Ronald Cron67ea2542022-09-15 17:34:42 +02001129 case MBEDTLS_TLS1_3_SIG_RSA_PKCS1_SHA384:
Gilles Peskine449bd832023-01-11 14:50:10 +01001130 return PSA_ALG_RSA_PKCS1V15_SIGN(PSA_ALG_SHA_384);
Ronald Cron67ea2542022-09-15 17:34:42 +02001131 case MBEDTLS_TLS1_3_SIG_RSA_PKCS1_SHA512:
Gilles Peskine449bd832023-01-11 14:50:10 +01001132 return PSA_ALG_RSA_PKCS1V15_SIGN(PSA_ALG_SHA_512);
Ronald Cron67ea2542022-09-15 17:34:42 +02001133 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01001134 return PSA_ALG_NONE;
Ronald Cron67ea2542022-09-15 17:34:42 +02001135 }
1136}
1137#endif /* MBEDTLS_USE_PSA_CRYPTO */
1138
XiaokangQian23c5be62022-06-07 02:04:34 +00001139/*
XiaokangQianfb665a82022-06-15 03:57:21 +00001140 * Pick best ( private key, certificate chain ) pair based on the signature
1141 * algorithms supported by the client.
XiaokangQian23c5be62022-06-07 02:04:34 +00001142 */
Ronald Cronce7d76e2022-07-08 18:56:49 +02001143MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01001144static int ssl_tls13_pick_key_cert(mbedtls_ssl_context *ssl)
XiaokangQian23c5be62022-06-07 02:04:34 +00001145{
XiaokangQianfb665a82022-06-15 03:57:21 +00001146 mbedtls_ssl_key_cert *key_cert, *key_cert_list;
XiaokangQian81802f42022-06-10 13:25:22 +00001147 const uint16_t *sig_alg = ssl->handshake->received_sig_algs;
XiaokangQian23c5be62022-06-07 02:04:34 +00001148
1149#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Gilles Peskine449bd832023-01-11 14:50:10 +01001150 if (ssl->handshake->sni_key_cert != NULL) {
XiaokangQianfb665a82022-06-15 03:57:21 +00001151 key_cert_list = ssl->handshake->sni_key_cert;
Gilles Peskine449bd832023-01-11 14:50:10 +01001152 } else
XiaokangQian23c5be62022-06-07 02:04:34 +00001153#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
Gilles Peskine449bd832023-01-11 14:50:10 +01001154 key_cert_list = ssl->conf->key_cert;
XiaokangQian23c5be62022-06-07 02:04:34 +00001155
Gilles Peskine449bd832023-01-11 14:50:10 +01001156 if (key_cert_list == NULL) {
1157 MBEDTLS_SSL_DEBUG_MSG(3, ("server has no certificate"));
1158 return -1;
XiaokangQian23c5be62022-06-07 02:04:34 +00001159 }
1160
Gilles Peskine449bd832023-01-11 14:50:10 +01001161 for (; *sig_alg != MBEDTLS_TLS1_3_SIG_NONE; sig_alg++) {
1162 if (!mbedtls_ssl_sig_alg_is_offered(ssl, *sig_alg)) {
Ronald Cron67ea2542022-09-15 17:34:42 +02001163 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +01001164 }
Ronald Cron67ea2542022-09-15 17:34:42 +02001165
Gilles Peskine449bd832023-01-11 14:50:10 +01001166 if (!mbedtls_ssl_tls13_sig_alg_for_cert_verify_is_supported(*sig_alg)) {
Ronald Cron67ea2542022-09-15 17:34:42 +02001167 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +01001168 }
Ronald Cron67ea2542022-09-15 17:34:42 +02001169
Gilles Peskine449bd832023-01-11 14:50:10 +01001170 for (key_cert = key_cert_list; key_cert != NULL;
1171 key_cert = key_cert->next) {
Ronald Cron67ea2542022-09-15 17:34:42 +02001172#if defined(MBEDTLS_USE_PSA_CRYPTO)
1173 psa_algorithm_t psa_alg = PSA_ALG_NONE;
1174#endif /* MBEDTLS_USE_PSA_CRYPTO */
1175
Gilles Peskine449bd832023-01-11 14:50:10 +01001176 MBEDTLS_SSL_DEBUG_CRT(3, "certificate (chain) candidate",
1177 key_cert->cert);
XiaokangQian81802f42022-06-10 13:25:22 +00001178
1179 /*
Gilles Peskine449bd832023-01-11 14:50:10 +01001180 * This avoids sending the client a cert it'll reject based on
1181 * keyUsage or other extensions.
1182 */
1183 if (mbedtls_x509_crt_check_key_usage(
1184 key_cert->cert, MBEDTLS_X509_KU_DIGITAL_SIGNATURE) != 0 ||
XiaokangQian81802f42022-06-10 13:25:22 +00001185 mbedtls_x509_crt_check_extended_key_usage(
XiaokangQianfb665a82022-06-15 03:57:21 +00001186 key_cert->cert, MBEDTLS_OID_SERVER_AUTH,
Gilles Peskine449bd832023-01-11 14:50:10 +01001187 MBEDTLS_OID_SIZE(MBEDTLS_OID_SERVER_AUTH)) != 0) {
1188 MBEDTLS_SSL_DEBUG_MSG(3, ("certificate mismatch: "
1189 "(extended) key usage extension"));
XiaokangQian81802f42022-06-10 13:25:22 +00001190 continue;
1191 }
XiaokangQian23c5be62022-06-07 02:04:34 +00001192
Gilles Peskine449bd832023-01-11 14:50:10 +01001193 MBEDTLS_SSL_DEBUG_MSG(3,
1194 ("ssl_tls13_pick_key_cert:"
1195 "check signature algorithm %s [%04x]",
1196 mbedtls_ssl_sig_alg_to_str(*sig_alg),
1197 *sig_alg));
Ronald Cron67ea2542022-09-15 17:34:42 +02001198#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01001199 psa_alg = ssl_tls13_iana_sig_alg_to_psa_alg(*sig_alg);
Ronald Cron67ea2542022-09-15 17:34:42 +02001200#endif /* MBEDTLS_USE_PSA_CRYPTO */
1201
Gilles Peskine449bd832023-01-11 14:50:10 +01001202 if (mbedtls_ssl_tls13_check_sig_alg_cert_key_match(
1203 *sig_alg, &key_cert->cert->pk)
Ronald Cron67ea2542022-09-15 17:34:42 +02001204#if defined(MBEDTLS_USE_PSA_CRYPTO)
1205 && psa_alg != PSA_ALG_NONE &&
Gilles Peskine449bd832023-01-11 14:50:10 +01001206 mbedtls_pk_can_do_ext(&key_cert->cert->pk, psa_alg,
1207 PSA_KEY_USAGE_SIGN_HASH) == 1
Ronald Cron67ea2542022-09-15 17:34:42 +02001208#endif /* MBEDTLS_USE_PSA_CRYPTO */
Gilles Peskine449bd832023-01-11 14:50:10 +01001209 ) {
XiaokangQianfb665a82022-06-15 03:57:21 +00001210 ssl->handshake->key_cert = key_cert;
Gilles Peskine449bd832023-01-11 14:50:10 +01001211 MBEDTLS_SSL_DEBUG_MSG(3,
1212 ("ssl_tls13_pick_key_cert:"
1213 "selected signature algorithm"
1214 " %s [%04x]",
1215 mbedtls_ssl_sig_alg_to_str(*sig_alg),
1216 *sig_alg));
XiaokangQianfb665a82022-06-15 03:57:21 +00001217 MBEDTLS_SSL_DEBUG_CRT(
Gilles Peskine449bd832023-01-11 14:50:10 +01001218 3, "selected certificate (chain)",
1219 ssl->handshake->key_cert->cert);
1220 return 0;
XiaokangQian81802f42022-06-10 13:25:22 +00001221 }
XiaokangQian81802f42022-06-10 13:25:22 +00001222 }
XiaokangQian23c5be62022-06-07 02:04:34 +00001223 }
1224
Gilles Peskine449bd832023-01-11 14:50:10 +01001225 MBEDTLS_SSL_DEBUG_MSG(2, ("ssl_tls13_pick_key_cert:"
1226 "no suitable certificate found"));
1227 return -1;
XiaokangQian23c5be62022-06-07 02:04:34 +00001228}
XiaokangQian81802f42022-06-10 13:25:22 +00001229#endif /* MBEDTLS_X509_CRT_PARSE_C &&
Ronald Cron928cbd32022-10-04 16:14:26 +02001230 MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED */
XiaokangQian23c5be62022-06-07 02:04:34 +00001231
XiaokangQian4080a7f2022-04-11 09:55:18 +00001232/*
XiaokangQianed582dd2022-04-13 08:21:05 +00001233 *
1234 * STATE HANDLING: ClientHello
1235 *
XiaokangQianb67384d2022-04-19 00:02:38 +00001236 * There are three possible classes of outcomes when parsing the ClientHello:
XiaokangQianed582dd2022-04-13 08:21:05 +00001237 *
XiaokangQianb67384d2022-04-19 00:02:38 +00001238 * 1) The ClientHello was well-formed and matched the server's configuration.
XiaokangQianed582dd2022-04-13 08:21:05 +00001239 *
1240 * In this case, the server progresses to sending its ServerHello.
1241 *
XiaokangQianb67384d2022-04-19 00:02:38 +00001242 * 2) The ClientHello was well-formed but didn't match the server's
1243 * configuration.
XiaokangQianed582dd2022-04-13 08:21:05 +00001244 *
1245 * For example, the client might not have offered a key share which
1246 * the server supports, or the server might require a cookie.
1247 *
1248 * In this case, the server sends a HelloRetryRequest.
1249 *
XiaokangQianb67384d2022-04-19 00:02:38 +00001250 * 3) The ClientHello was ill-formed
XiaokangQianed582dd2022-04-13 08:21:05 +00001251 *
1252 * In this case, we abort the handshake.
1253 *
1254 */
1255
1256/*
XiaokangQian4080a7f2022-04-11 09:55:18 +00001257 * Structure of this message:
1258 *
XiaokangQiane8ff3502022-04-22 02:34:40 +00001259 * uint16 ProtocolVersion;
1260 * opaque Random[32];
1261 * uint8 CipherSuite[2]; // Cryptographic suite selector
XiaokangQian4080a7f2022-04-11 09:55:18 +00001262 *
XiaokangQiane8ff3502022-04-22 02:34:40 +00001263 * struct {
1264 * ProtocolVersion legacy_version = 0x0303; // TLS v1.2
1265 * Random random;
1266 * opaque legacy_session_id<0..32>;
1267 * CipherSuite cipher_suites<2..2^16-2>;
1268 * opaque legacy_compression_methods<1..2^8-1>;
1269 * Extension extensions<8..2^16-1>;
1270 * } ClientHello;
XiaokangQian4080a7f2022-04-11 09:55:18 +00001271 */
XiaokangQianed582dd2022-04-13 08:21:05 +00001272
1273#define SSL_CLIENT_HELLO_OK 0
1274#define SSL_CLIENT_HELLO_HRR_REQUIRED 1
Ronald Cron5af4c7f2023-03-07 20:46:59 +01001275#define SSL_CLIENT_HELLO_TLS1_2 2
XiaokangQianed582dd2022-04-13 08:21:05 +00001276
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001277MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01001278static int ssl_tls13_parse_client_hello(mbedtls_ssl_context *ssl,
1279 const unsigned char *buf,
1280 const unsigned char *end)
XiaokangQian7807f9f2022-02-15 10:04:37 +00001281{
XiaokangQianb67384d2022-04-19 00:02:38 +00001282 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1283 const unsigned char *p = buf;
Ronald Crond540d992023-03-07 09:41:48 +01001284 const unsigned char *random;
XiaokangQian4080a7f2022-04-11 09:55:18 +00001285 size_t legacy_session_id_len;
Ronald Croncada4102023-03-07 09:51:39 +01001286 const unsigned char *legacy_session_id;
XiaokangQian318dc762022-04-20 09:43:51 +00001287 size_t cipher_suites_len;
Ronald Cron2f16b4e2023-03-07 10:07:32 +01001288 const unsigned char *cipher_suites;
XiaokangQian060d8672022-04-21 09:24:56 +00001289 const unsigned char *cipher_suites_end;
XiaokangQianb67384d2022-04-19 00:02:38 +00001290 size_t extensions_len;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001291 const unsigned char *extensions_end;
Ronald Croneff56732023-04-03 17:36:31 +02001292 const unsigned char *supported_versions_data;
1293 const unsigned char *supported_versions_data_end;
Jerry Yuc4bf5d62022-10-29 09:08:47 +08001294 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
Jerry Yu49ca9282022-05-05 11:05:22 +08001295 int hrr_required = 0;
Ronald Cron8a74f072023-06-14 17:59:29 +02001296 int no_usable_share_for_key_agreement = 0;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001297
Ronald Cron41a443a2022-10-04 16:38:25 +02001298#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED)
Jerry Yu29d9faa2022-08-23 17:52:45 +08001299 const unsigned char *pre_shared_key_ext = NULL;
Jerry Yu1c105562022-07-10 06:32:38 +00001300 const unsigned char *pre_shared_key_ext_end = NULL;
Ronald Cron41a443a2022-10-04 16:38:25 +02001301#endif
XiaokangQian7807f9f2022-02-15 10:04:37 +00001302
XiaokangQian7807f9f2022-02-15 10:04:37 +00001303 /*
XiaokangQianb67384d2022-04-19 00:02:38 +00001304 * ClientHello layout:
XiaokangQian7807f9f2022-02-15 10:04:37 +00001305 * 0 . 1 protocol version
XiaokangQiane8ff3502022-04-22 02:34:40 +00001306 * 2 . 33 random bytes
XiaokangQianc5763b52022-04-02 03:34:37 +00001307 * 34 . 34 session id length ( 1 byte )
XiaokangQian7807f9f2022-02-15 10:04:37 +00001308 * 35 . 34+x session id
XiaokangQian7807f9f2022-02-15 10:04:37 +00001309 * .. . .. ciphersuite list length ( 2 bytes )
1310 * .. . .. ciphersuite list
1311 * .. . .. compression alg. list length ( 1 byte )
1312 * .. . .. compression alg. list
1313 * .. . .. extensions length ( 2 bytes, optional )
1314 * .. . .. extensions ( optional )
1315 */
1316
XiaokangQianb67384d2022-04-19 00:02:38 +00001317 /*
bootstrap-prime6dbbf442022-05-17 19:30:44 -04001318 * Minimal length ( with everything empty and extensions omitted ) is
XiaokangQian7807f9f2022-02-15 10:04:37 +00001319 * 2 + 32 + 1 + 2 + 1 = 38 bytes. Check that first, so that we can
1320 * read at least up to session id length without worrying.
1321 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001322 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, 38);
XiaokangQian7807f9f2022-02-15 10:04:37 +00001323
1324 /* ...
1325 * ProtocolVersion legacy_version = 0x0303; // TLS 1.2
1326 * ...
1327 * with ProtocolVersion defined as:
1328 * uint16 ProtocolVersion;
1329 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001330 if (mbedtls_ssl_read_version(p, ssl->conf->transport) !=
1331 MBEDTLS_SSL_VERSION_TLS1_2) {
1332 MBEDTLS_SSL_DEBUG_MSG(1, ("Unsupported version of TLS."));
1333 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_PROTOCOL_VERSION,
1334 MBEDTLS_ERR_SSL_BAD_PROTOCOL_VERSION);
1335 return MBEDTLS_ERR_SSL_BAD_PROTOCOL_VERSION;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001336 }
1337 p += 2;
1338
Jerry Yuc1be19f2022-04-23 16:11:39 +08001339 /* ...
1340 * Random random;
1341 * ...
XiaokangQianb67384d2022-04-19 00:02:38 +00001342 * with Random defined as:
1343 * opaque Random[32];
XiaokangQian7807f9f2022-02-15 10:04:37 +00001344 */
Ronald Crond540d992023-03-07 09:41:48 +01001345 random = p;
XiaokangQian08037552022-04-20 07:16:41 +00001346 p += MBEDTLS_CLIENT_HELLO_RANDOM_LEN;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001347
Jerry Yuc1be19f2022-04-23 16:11:39 +08001348 /* ...
XiaokangQianb67384d2022-04-19 00:02:38 +00001349 * opaque legacy_session_id<0..32>;
Jerry Yuc1be19f2022-04-23 16:11:39 +08001350 * ...
XiaokangQian7807f9f2022-02-15 10:04:37 +00001351 */
Ronald Croncada4102023-03-07 09:51:39 +01001352 legacy_session_id_len = *(p++);
1353 legacy_session_id = p;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001354
XiaokangQianb67384d2022-04-19 00:02:38 +00001355 /*
1356 * Check we have enough data for the legacy session identifier
Jerry Yue95c8af2022-07-26 15:48:20 +08001357 * and the ciphersuite list length.
XiaokangQianb67384d2022-04-19 00:02:38 +00001358 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001359 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, legacy_session_id_len + 2);
XiaokangQian4080a7f2022-04-11 09:55:18 +00001360 p += legacy_session_id_len;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001361
Ronald Cron2f16b4e2023-03-07 10:07:32 +01001362 /* ...
1363 * CipherSuite cipher_suites<2..2^16-2>;
1364 * ...
1365 * with CipherSuite defined as:
1366 * uint8 CipherSuite[2];
1367 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001368 cipher_suites_len = MBEDTLS_GET_UINT16_BE(p, 0);
XiaokangQian7807f9f2022-02-15 10:04:37 +00001369 p += 2;
Ronald Cron2f16b4e2023-03-07 10:07:32 +01001370 cipher_suites = p;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001371
Ronald Cronfc7ae872023-02-16 15:32:19 +01001372 /*
1373 * The length of the ciphersuite list has to be even.
1374 */
1375 if (cipher_suites_len & 1) {
1376 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR,
1377 MBEDTLS_ERR_SSL_DECODE_ERROR);
1378 return MBEDTLS_ERR_SSL_DECODE_ERROR;
1379 }
1380
XiaokangQianb67384d2022-04-19 00:02:38 +00001381 /* Check we have enough data for the ciphersuite list, the legacy
1382 * compression methods and the length of the extensions.
Jerry Yue95c8af2022-07-26 15:48:20 +08001383 *
1384 * cipher_suites cipher_suites_len bytes
1385 * legacy_compression_methods 2 bytes
1386 * extensions_len 2 bytes
XiaokangQianb67384d2022-04-19 00:02:38 +00001387 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001388 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, cipher_suites_len + 2 + 2);
Ronald Cron8c527d02023-03-07 15:47:47 +01001389 p += cipher_suites_len;
1390 cipher_suites_end = p;
Jerry Yu5725f1c2022-08-21 17:27:16 +08001391
1392 /*
Ronald Cron8c527d02023-03-07 15:47:47 +01001393 * Search for the supported versions extension and parse it to determine
1394 * if the client supports TLS 1.3.
1395 */
1396 ret = mbedtls_ssl_tls13_is_supported_versions_ext_present_in_exts(
1397 ssl, p + 2, end,
Ronald Croneff56732023-04-03 17:36:31 +02001398 &supported_versions_data, &supported_versions_data_end);
Ronald Cron8c527d02023-03-07 15:47:47 +01001399 if (ret < 0) {
1400 MBEDTLS_SSL_DEBUG_RET(1,
1401 ("mbedtls_ssl_tls13_is_supported_versions_ext_present_in_exts"), ret);
1402 return ret;
1403 }
1404
1405 if (ret == 0) {
Ronald Cron5af4c7f2023-03-07 20:46:59 +01001406 return SSL_CLIENT_HELLO_TLS1_2;
Ronald Cron8c527d02023-03-07 15:47:47 +01001407 }
1408
Ronald Cron5af4c7f2023-03-07 20:46:59 +01001409 if (ret == 1) {
1410 ret = ssl_tls13_parse_supported_versions_ext(ssl,
Ronald Croneff56732023-04-03 17:36:31 +02001411 supported_versions_data,
1412 supported_versions_data_end);
Ronald Cron5af4c7f2023-03-07 20:46:59 +01001413 if (ret < 0) {
1414 MBEDTLS_SSL_DEBUG_RET(1,
1415 ("ssl_tls13_parse_supported_versions_ext"), ret);
1416 return ret;
1417 }
1418
Ronald Cronb828c7d2023-04-03 16:37:22 +02001419 /*
1420 * The supported versions extension was parsed successfully as the
1421 * value returned by ssl_tls13_parse_supported_versions_ext() is
1422 * positive. The return value is then equal to
1423 * MBEDTLS_SSL_VERSION_TLS1_2 or MBEDTLS_SSL_VERSION_TLS1_3, defining
1424 * the TLS version to negotiate.
1425 */
Ronald Cron5af4c7f2023-03-07 20:46:59 +01001426 if (MBEDTLS_SSL_VERSION_TLS1_2 == ret) {
1427 return SSL_CLIENT_HELLO_TLS1_2;
1428 }
Ronald Cron8c527d02023-03-07 15:47:47 +01001429 }
1430
1431 /*
1432 * We negotiate TLS 1.3.
Ronald Cron64582392023-03-07 09:21:40 +01001433 */
1434 ssl->tls_version = MBEDTLS_SSL_VERSION_TLS1_3;
1435
1436#if defined(MBEDTLS_SSL_SESSION_TICKETS)
1437 /* Store minor version for later use with ticket serialization. */
1438 ssl->session_negotiate->tls_version = MBEDTLS_SSL_VERSION_TLS1_3;
1439 ssl->session_negotiate->endpoint = ssl->conf->endpoint;
1440#endif
1441
1442 /*
Ronald Crondad02b22023-04-06 09:57:52 +02001443 * We are negotiating the version 1.3 of the protocol. Do what we have
Ronald Croncada4102023-03-07 09:51:39 +01001444 * postponed: copy of the client random bytes, copy of the legacy session
Ronald Cron2f16b4e2023-03-07 10:07:32 +01001445 * identifier and selection of the TLS 1.3 cipher suite.
Ronald Crond540d992023-03-07 09:41:48 +01001446 */
1447 MBEDTLS_SSL_DEBUG_BUF(3, "client hello, random bytes",
1448 random, MBEDTLS_CLIENT_HELLO_RANDOM_LEN);
1449 memcpy(&handshake->randbytes[0], random, MBEDTLS_CLIENT_HELLO_RANDOM_LEN);
1450
Ronald Croncada4102023-03-07 09:51:39 +01001451 if (legacy_session_id_len > sizeof(ssl->session_negotiate->id)) {
1452 MBEDTLS_SSL_DEBUG_MSG(1, ("bad client hello message"));
1453 return MBEDTLS_ERR_SSL_DECODE_ERROR;
1454 }
1455 ssl->session_negotiate->id_len = legacy_session_id_len;
1456 MBEDTLS_SSL_DEBUG_BUF(3, "client hello, session id",
1457 legacy_session_id, legacy_session_id_len);
1458 memcpy(&ssl->session_negotiate->id[0],
1459 legacy_session_id, legacy_session_id_len);
1460
Ronald Crond540d992023-03-07 09:41:48 +01001461 /*
Jerry Yu5725f1c2022-08-21 17:27:16 +08001462 * Search for a matching ciphersuite
1463 */
Ronald Cron2f16b4e2023-03-07 10:07:32 +01001464 MBEDTLS_SSL_DEBUG_BUF(3, "client hello, list of cipher suites",
1465 cipher_suites, cipher_suites_len);
Ronald Crone45afd72023-04-04 15:10:06 +02001466 for (const unsigned char *cipher_suites_p = cipher_suites;
1467 cipher_suites_p < cipher_suites_end; cipher_suites_p += 2) {
XiaokangQiane8ff3502022-04-22 02:34:40 +00001468 uint16_t cipher_suite;
Gilles Peskine449bd832023-01-11 14:50:10 +01001469 const mbedtls_ssl_ciphersuite_t *ciphersuite_info;
Jerry Yu5725f1c2022-08-21 17:27:16 +08001470
Ronald Crond89360b2023-02-21 08:53:33 +01001471 /*
Ronald Crone45afd72023-04-04 15:10:06 +02001472 * "cipher_suites_end - cipher_suites_p is even" is an invariant of the
1473 * loop. As cipher_suites_end - cipher_suites_p > 0, we have
1474 * cipher_suites_end - cipher_suites_p >= 2 and it is thus safe to read
1475 * two bytes.
Ronald Crond89360b2023-02-21 08:53:33 +01001476 */
Ronald Crone45afd72023-04-04 15:10:06 +02001477 cipher_suite = MBEDTLS_GET_UINT16_BE(cipher_suites_p, 0);
Jerry Yuc5a23a02022-08-25 10:51:44 +08001478 ciphersuite_info = ssl_tls13_validate_peer_ciphersuite(
Gilles Peskine449bd832023-01-11 14:50:10 +01001479 ssl, cipher_suite);
1480 if (ciphersuite_info == NULL) {
Jerry Yu5725f1c2022-08-21 17:27:16 +08001481 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +01001482 }
Jerry Yu5725f1c2022-08-21 17:27:16 +08001483
Jerry Yu5725f1c2022-08-21 17:27:16 +08001484 ssl->session_negotiate->ciphersuite = cipher_suite;
Jerry Yuc4bf5d62022-10-29 09:08:47 +08001485 handshake->ciphersuite_info = ciphersuite_info;
Gilles Peskine449bd832023-01-11 14:50:10 +01001486 MBEDTLS_SSL_DEBUG_MSG(2, ("selected ciphersuite: %04x - %s",
1487 cipher_suite,
1488 ciphersuite_info->name));
Ronald Cron25e9ec62023-02-16 15:35:16 +01001489 break;
XiaokangQian17f974c2022-04-19 09:57:41 +00001490 }
Jerry Yu5725f1c2022-08-21 17:27:16 +08001491
Gilles Peskine449bd832023-01-11 14:50:10 +01001492 if (handshake->ciphersuite_info == NULL) {
1493 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE,
1494 MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE);
1495 return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
Jerry Yu5725f1c2022-08-21 17:27:16 +08001496 }
Jerry Yuc1be19f2022-04-23 16:11:39 +08001497
XiaokangQian4080a7f2022-04-11 09:55:18 +00001498 /* ...
XiaokangQianb67384d2022-04-19 00:02:38 +00001499 * opaque legacy_compression_methods<1..2^8-1>;
XiaokangQian4080a7f2022-04-11 09:55:18 +00001500 * ...
XiaokangQian7807f9f2022-02-15 10:04:37 +00001501 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001502 if (p[0] != 1 || p[1] != MBEDTLS_SSL_COMPRESS_NULL) {
1503 MBEDTLS_SSL_DEBUG_MSG(1, ("bad legacy compression method"));
1504 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER,
1505 MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER);
1506 return MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001507 }
XiaokangQianb67384d2022-04-19 00:02:38 +00001508 p += 2;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001509
Jerry Yuc1be19f2022-04-23 16:11:39 +08001510 /* ...
XiaokangQianb67384d2022-04-19 00:02:38 +00001511 * Extension extensions<8..2^16-1>;
Jerry Yuc1be19f2022-04-23 16:11:39 +08001512 * ...
XiaokangQianb67384d2022-04-19 00:02:38 +00001513 * with Extension defined as:
1514 * struct {
1515 * ExtensionType extension_type;
1516 * opaque extension_data<0..2^16-1>;
1517 * } Extension;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001518 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001519 extensions_len = MBEDTLS_GET_UINT16_BE(p, 0);
XiaokangQian7807f9f2022-02-15 10:04:37 +00001520 p += 2;
Gilles Peskine449bd832023-01-11 14:50:10 +01001521 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, extensions_len);
XiaokangQiane8ff3502022-04-22 02:34:40 +00001522 extensions_end = p + extensions_len;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001523
Gilles Peskine449bd832023-01-11 14:50:10 +01001524 MBEDTLS_SSL_DEBUG_BUF(3, "client hello extensions", p, extensions_len);
Jerry Yu63a459c2022-10-31 13:38:40 +08001525 handshake->received_extensions = MBEDTLS_SSL_EXT_MASK_NONE;
Jerry Yu0c354a22022-08-29 15:25:36 +08001526
Gilles Peskine449bd832023-01-11 14:50:10 +01001527 while (p < extensions_end) {
XiaokangQian7807f9f2022-02-15 10:04:37 +00001528 unsigned int extension_type;
1529 size_t extension_data_len;
1530 const unsigned char *extension_data_end;
1531
Jerry Yu0c354a22022-08-29 15:25:36 +08001532 /* RFC 8446, section 4.2.11
Jerry Yu1c9247c2022-07-21 12:37:39 +08001533 *
1534 * The "pre_shared_key" extension MUST be the last extension in the
1535 * ClientHello (this facilitates implementation as described below).
1536 * Servers MUST check that it is the last extension and otherwise fail
1537 * the handshake with an "illegal_parameter" alert.
1538 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001539 if (handshake->received_extensions & MBEDTLS_SSL_EXT_MASK(PRE_SHARED_KEY)) {
Jerry Yu1c9247c2022-07-21 12:37:39 +08001540 MBEDTLS_SSL_DEBUG_MSG(
Gilles Peskine449bd832023-01-11 14:50:10 +01001541 3, ("pre_shared_key is not last extension."));
Jerry Yu1c9247c2022-07-21 12:37:39 +08001542 MBEDTLS_SSL_PEND_FATAL_ALERT(
1543 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER,
Gilles Peskine449bd832023-01-11 14:50:10 +01001544 MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER);
1545 return MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER;
Jerry Yu1c9247c2022-07-21 12:37:39 +08001546 }
1547
Gilles Peskine449bd832023-01-11 14:50:10 +01001548 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, extensions_end, 4);
1549 extension_type = MBEDTLS_GET_UINT16_BE(p, 0);
1550 extension_data_len = MBEDTLS_GET_UINT16_BE(p, 2);
XiaokangQian7807f9f2022-02-15 10:04:37 +00001551 p += 4;
1552
Gilles Peskine449bd832023-01-11 14:50:10 +01001553 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, extensions_end, extension_data_len);
XiaokangQian7807f9f2022-02-15 10:04:37 +00001554 extension_data_end = p + extension_data_len;
1555
Jerry Yuc4bf5d62022-10-29 09:08:47 +08001556 ret = mbedtls_ssl_tls13_check_received_extension(
Gilles Peskine449bd832023-01-11 14:50:10 +01001557 ssl, MBEDTLS_SSL_HS_CLIENT_HELLO, extension_type,
1558 MBEDTLS_SSL_TLS1_3_ALLOWED_EXTS_OF_CH);
1559 if (ret != 0) {
1560 return ret;
1561 }
Jerry Yue18dc7e2022-08-04 16:29:22 +08001562
Gilles Peskine449bd832023-01-11 14:50:10 +01001563 switch (extension_type) {
XiaokangQian40a35232022-05-07 09:02:40 +00001564#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
1565 case MBEDTLS_TLS_EXT_SERVERNAME:
Gilles Peskine449bd832023-01-11 14:50:10 +01001566 MBEDTLS_SSL_DEBUG_MSG(3, ("found ServerName extension"));
1567 ret = mbedtls_ssl_parse_server_name_ext(ssl, p,
1568 extension_data_end);
1569 if (ret != 0) {
XiaokangQian40a35232022-05-07 09:02:40 +00001570 MBEDTLS_SSL_DEBUG_RET(
Gilles Peskine449bd832023-01-11 14:50:10 +01001571 1, "mbedtls_ssl_parse_servername_ext", ret);
1572 return ret;
XiaokangQian40a35232022-05-07 09:02:40 +00001573 }
XiaokangQian40a35232022-05-07 09:02:40 +00001574 break;
1575#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
1576
Przemek Stekiel8c0a9532023-06-15 16:48:19 +02001577#if defined(PSA_WANT_ALG_ECDH) || defined(PSA_WANT_ALG_FFDH)
XiaokangQian7807f9f2022-02-15 10:04:37 +00001578 case MBEDTLS_TLS_EXT_SUPPORTED_GROUPS:
Gilles Peskine449bd832023-01-11 14:50:10 +01001579 MBEDTLS_SSL_DEBUG_MSG(3, ("found supported group extension"));
XiaokangQian7807f9f2022-02-15 10:04:37 +00001580
1581 /* Supported Groups Extension
1582 *
1583 * When sent by the client, the "supported_groups" extension
1584 * indicates the named groups which the client supports,
1585 * ordered from most preferred to least preferred.
1586 */
Jerry Yuc1be19f2022-04-23 16:11:39 +08001587 ret = ssl_tls13_parse_supported_groups_ext(
Gilles Peskine449bd832023-01-11 14:50:10 +01001588 ssl, p, extension_data_end);
1589 if (ret != 0) {
Xiaokang Qian9f1747b2023-03-30 02:50:04 +00001590 MBEDTLS_SSL_DEBUG_RET(
Xiaokang Qian8bce0e62023-04-04 10:15:48 +00001591 1, "ssl_tls13_parse_supported_groups_ext", ret);
Gilles Peskine449bd832023-01-11 14:50:10 +01001592 return ret;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001593 }
1594
XiaokangQian7807f9f2022-02-15 10:04:37 +00001595 break;
Przemek Stekiel8c0a9532023-06-15 16:48:19 +02001596#endif /* PSA_WANT_ALG_ECDH || PSA_WANT_ALG_FFDH*/
XiaokangQian7807f9f2022-02-15 10:04:37 +00001597
Valerio Settic9ae8622023-07-25 11:23:50 +02001598#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_EPHEMERAL_ENABLED)
XiaokangQian7807f9f2022-02-15 10:04:37 +00001599 case MBEDTLS_TLS_EXT_KEY_SHARE:
Gilles Peskine449bd832023-01-11 14:50:10 +01001600 MBEDTLS_SSL_DEBUG_MSG(3, ("found key share extension"));
XiaokangQian7807f9f2022-02-15 10:04:37 +00001601
1602 /*
1603 * Key Share Extension
1604 *
1605 * When sent by the client, the "key_share" extension
1606 * contains the endpoint's cryptographic parameters for
1607 * ECDHE/DHE key establishment methods.
1608 */
Jerry Yuc1be19f2022-04-23 16:11:39 +08001609 ret = ssl_tls13_parse_key_shares_ext(
Gilles Peskine449bd832023-01-11 14:50:10 +01001610 ssl, p, extension_data_end);
1611 if (ret == SSL_TLS1_3_PARSE_KEY_SHARES_EXT_NO_MATCH) {
Ronald Cron8a74f072023-06-14 17:59:29 +02001612 MBEDTLS_SSL_DEBUG_MSG(2, ("No usable share for key agreement."));
1613 no_usable_share_for_key_agreement = 1;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001614 }
1615
Gilles Peskine449bd832023-01-11 14:50:10 +01001616 if (ret < 0) {
Jerry Yu582dd062022-04-22 21:59:01 +08001617 MBEDTLS_SSL_DEBUG_RET(
Gilles Peskine449bd832023-01-11 14:50:10 +01001618 1, "ssl_tls13_parse_key_shares_ext", ret);
1619 return ret;
Jerry Yu582dd062022-04-22 21:59:01 +08001620 }
XiaokangQian7807f9f2022-02-15 10:04:37 +00001621
XiaokangQian7807f9f2022-02-15 10:04:37 +00001622 break;
Valerio Settic9ae8622023-07-25 11:23:50 +02001623#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_EPHEMERAL_ENABLED */
XiaokangQian7807f9f2022-02-15 10:04:37 +00001624
1625 case MBEDTLS_TLS_EXT_SUPPORTED_VERSIONS:
Ronald Cron8c527d02023-03-07 15:47:47 +01001626 /* Already parsed */
XiaokangQian7807f9f2022-02-15 10:04:37 +00001627 break;
1628
Ronald Cron41a443a2022-10-04 16:38:25 +02001629#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED)
Jerry Yue19e3b92022-07-08 12:04:51 +00001630 case MBEDTLS_TLS_EXT_PSK_KEY_EXCHANGE_MODES:
Xiaokang Qian9f1747b2023-03-30 02:50:04 +00001631 MBEDTLS_SSL_DEBUG_MSG(
1632 3, ("found psk key exchange modes extension"));
Jerry Yue19e3b92022-07-08 12:04:51 +00001633
1634 ret = ssl_tls13_parse_key_exchange_modes_ext(
Gilles Peskine449bd832023-01-11 14:50:10 +01001635 ssl, p, extension_data_end);
1636 if (ret != 0) {
Jerry Yue19e3b92022-07-08 12:04:51 +00001637 MBEDTLS_SSL_DEBUG_RET(
Gilles Peskine449bd832023-01-11 14:50:10 +01001638 1, "ssl_tls13_parse_key_exchange_modes_ext", ret);
1639 return ret;
Jerry Yue19e3b92022-07-08 12:04:51 +00001640 }
1641
Jerry Yue19e3b92022-07-08 12:04:51 +00001642 break;
Ronald Cron41a443a2022-10-04 16:38:25 +02001643#endif
Jerry Yue19e3b92022-07-08 12:04:51 +00001644
Jerry Yu1c105562022-07-10 06:32:38 +00001645 case MBEDTLS_TLS_EXT_PRE_SHARED_KEY:
Gilles Peskine449bd832023-01-11 14:50:10 +01001646 MBEDTLS_SSL_DEBUG_MSG(3, ("found pre_shared_key extension"));
1647 if ((handshake->received_extensions &
1648 MBEDTLS_SSL_EXT_MASK(PSK_KEY_EXCHANGE_MODES)) == 0) {
Jerry Yu13ab81d2022-07-22 23:17:11 +08001649 MBEDTLS_SSL_PEND_FATAL_ALERT(
1650 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER,
Gilles Peskine449bd832023-01-11 14:50:10 +01001651 MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER);
1652 return MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER;
Jerry Yu13ab81d2022-07-22 23:17:11 +08001653 }
Ronald Cron41a443a2022-10-04 16:38:25 +02001654#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED)
Jerry Yu1c105562022-07-10 06:32:38 +00001655 /* Delay processing of the PSK identity once we have
1656 * found out which algorithms to use. We keep a pointer
1657 * to the buffer and the size for later processing.
1658 */
Jerry Yu29d9faa2022-08-23 17:52:45 +08001659 pre_shared_key_ext = p;
Jerry Yu1c105562022-07-10 06:32:38 +00001660 pre_shared_key_ext_end = extension_data_end;
Jerry Yue18dc7e2022-08-04 16:29:22 +08001661#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED */
Jerry Yu1c105562022-07-10 06:32:38 +00001662 break;
Jerry Yu1c105562022-07-10 06:32:38 +00001663
XiaokangQianacb39922022-06-17 10:18:48 +00001664#if defined(MBEDTLS_SSL_ALPN)
1665 case MBEDTLS_TLS_EXT_ALPN:
Gilles Peskine449bd832023-01-11 14:50:10 +01001666 MBEDTLS_SSL_DEBUG_MSG(3, ("found alpn extension"));
XiaokangQianacb39922022-06-17 10:18:48 +00001667
Gilles Peskine449bd832023-01-11 14:50:10 +01001668 ret = mbedtls_ssl_parse_alpn_ext(ssl, p, extension_data_end);
1669 if (ret != 0) {
XiaokangQianacb39922022-06-17 10:18:48 +00001670 MBEDTLS_SSL_DEBUG_RET(
Gilles Peskine449bd832023-01-11 14:50:10 +01001671 1, ("mbedtls_ssl_parse_alpn_ext"), ret);
1672 return ret;
XiaokangQianacb39922022-06-17 10:18:48 +00001673 }
XiaokangQianacb39922022-06-17 10:18:48 +00001674 break;
1675#endif /* MBEDTLS_SSL_ALPN */
1676
Ronald Cron928cbd32022-10-04 16:14:26 +02001677#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED)
XiaokangQian7807f9f2022-02-15 10:04:37 +00001678 case MBEDTLS_TLS_EXT_SIG_ALG:
Gilles Peskine449bd832023-01-11 14:50:10 +01001679 MBEDTLS_SSL_DEBUG_MSG(3, ("found signature_algorithms extension"));
XiaokangQian7807f9f2022-02-15 10:04:37 +00001680
Gabor Mezei078e8032022-04-27 21:17:56 +02001681 ret = mbedtls_ssl_parse_sig_alg_ext(
Gilles Peskine449bd832023-01-11 14:50:10 +01001682 ssl, p, extension_data_end);
1683 if (ret != 0) {
Xiaokang Qian49f39c12023-04-06 02:31:02 +00001684 MBEDTLS_SSL_DEBUG_RET(
1685 1, "mbedtls_ssl_parse_sig_alg_ext", ret);
Gilles Peskine449bd832023-01-11 14:50:10 +01001686 return ret;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001687 }
XiaokangQian7807f9f2022-02-15 10:04:37 +00001688 break;
Ronald Cron928cbd32022-10-04 16:14:26 +02001689#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED */
XiaokangQian7807f9f2022-02-15 10:04:37 +00001690
Jan Bruckner151f6422023-02-10 12:45:19 +01001691#if defined(MBEDTLS_SSL_RECORD_SIZE_LIMIT)
1692 case MBEDTLS_TLS_EXT_RECORD_SIZE_LIMIT:
1693 MBEDTLS_SSL_DEBUG_MSG(3, ("found record_size_limit extension"));
1694
Xiaokang Qian9f1747b2023-03-30 02:50:04 +00001695 ret = mbedtls_ssl_tls13_parse_record_size_limit_ext(
1696 ssl, p, extension_data_end);
Jan Bruckner151f6422023-02-10 12:45:19 +01001697
Xiaokang Qian09c3ccc2023-04-04 10:18:38 +00001698 /*
1699 * TODO: Return unconditionally here until we handle the record
1700 * size limit correctly.
1701 * Once handled correctly, only return in case of errors.
1702 */
Jan Bruckner151f6422023-02-10 12:45:19 +01001703 return ret;
1704
1705 break;
1706#endif /* MBEDTLS_SSL_RECORD_SIZE_LIMIT */
1707
XiaokangQian7807f9f2022-02-15 10:04:37 +00001708 default:
Jerry Yu79aa7212022-11-08 21:30:21 +08001709 MBEDTLS_SSL_PRINT_EXT(
Jerry Yu63a459c2022-10-31 13:38:40 +08001710 3, MBEDTLS_SSL_HS_CLIENT_HELLO,
Gilles Peskine449bd832023-01-11 14:50:10 +01001711 extension_type, "( ignored )");
Jerry Yu0c354a22022-08-29 15:25:36 +08001712 break;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001713 }
1714
1715 p += extension_data_len;
1716 }
1717
Gilles Peskine449bd832023-01-11 14:50:10 +01001718 MBEDTLS_SSL_PRINT_EXTS(3, MBEDTLS_SSL_HS_CLIENT_HELLO,
1719 handshake->received_extensions);
Jerry Yue18dc7e2022-08-04 16:29:22 +08001720
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +01001721 ret = mbedtls_ssl_add_hs_hdr_to_checksum(ssl,
1722 MBEDTLS_SSL_HS_CLIENT_HELLO,
1723 p - buf);
1724 if (0 != ret) {
1725 MBEDTLS_SSL_DEBUG_RET(1, ("mbedtls_ssl_add_hs_hdr_to_checksum"), ret);
1726 return ret;
1727 }
Jerry Yu032b15ce2022-07-11 06:10:03 +00001728
Ronald Cron41a443a2022-10-04 16:38:25 +02001729#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED)
XiaokangQian7807f9f2022-02-15 10:04:37 +00001730 /* Update checksum with either
1731 * - The entire content of the CH message, if no PSK extension is present
1732 * - The content up to but excluding the PSK extension, if present.
1733 */
Jerry Yu1c105562022-07-10 06:32:38 +00001734 /* If we've settled on a PSK-based exchange, parse PSK identity ext */
Pengyu Lvcfb23b82023-10-30 15:26:26 +08001735 if (ssl_tls13_check_psk_key_exchange(ssl) ||
1736 ssl_tls13_check_psk_ephemeral_key_exchange(ssl)) {
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +01001737 ret = handshake->update_checksum(ssl, buf,
1738 pre_shared_key_ext - buf);
1739 if (0 != ret) {
1740 MBEDTLS_SSL_DEBUG_RET(1, ("update_checksum"), ret);
1741 return ret;
1742 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001743 ret = ssl_tls13_parse_pre_shared_key_ext(ssl,
1744 pre_shared_key_ext,
1745 pre_shared_key_ext_end,
1746 cipher_suites,
1747 cipher_suites_end);
1748 if (ret == MBEDTLS_ERR_SSL_UNKNOWN_IDENTITY) {
1749 handshake->received_extensions &= ~MBEDTLS_SSL_EXT_MASK(PRE_SHARED_KEY);
1750 } else if (ret != 0) {
Jerry Yu63a459c2022-10-31 13:38:40 +08001751 MBEDTLS_SSL_DEBUG_RET(
Gilles Peskine449bd832023-01-11 14:50:10 +01001752 1, "ssl_tls13_parse_pre_shared_key_ext", ret);
1753 return ret;
Jerry Yu1c105562022-07-10 06:32:38 +00001754 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001755 } else
Ronald Cron41a443a2022-10-04 16:38:25 +02001756#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED */
Jerry Yu1c105562022-07-10 06:32:38 +00001757 {
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +01001758 ret = handshake->update_checksum(ssl, buf, p - buf);
1759 if (0 != ret) {
1760 MBEDTLS_SSL_DEBUG_RET(1, ("update_checksum"), ret);
1761 return ret;
1762 }
Jerry Yu1c105562022-07-10 06:32:38 +00001763 }
XiaokangQian7807f9f2022-02-15 10:04:37 +00001764
Gilles Peskine449bd832023-01-11 14:50:10 +01001765 ret = ssl_tls13_determine_key_exchange_mode(ssl);
1766 if (ret < 0) {
1767 return ret;
1768 }
XiaokangQian7807f9f2022-02-15 10:04:37 +00001769
Ronald Cron8a74f072023-06-14 17:59:29 +02001770 if (ssl->handshake->key_exchange_mode !=
1771 MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK) {
1772 hrr_required = (no_usable_share_for_key_agreement != 0);
1773 }
1774
Gilles Peskine449bd832023-01-11 14:50:10 +01001775 mbedtls_ssl_optimize_checksum(ssl, handshake->ciphersuite_info);
Jerry Yue5834fd2022-08-29 20:16:09 +08001776
Gilles Peskine449bd832023-01-11 14:50:10 +01001777 return hrr_required ? SSL_CLIENT_HELLO_HRR_REQUIRED : SSL_CLIENT_HELLO_OK;
XiaokangQian75fe8c72022-06-15 09:42:45 +00001778}
1779
Jerry Yuab0da372023-02-08 13:55:24 +08001780#if defined(MBEDTLS_SSL_EARLY_DATA)
1781static void ssl_tls13_update_early_data_status(mbedtls_ssl_context *ssl)
1782{
1783 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
1784
1785 if ((handshake->received_extensions &
1786 MBEDTLS_SSL_EXT_MASK(EARLY_DATA)) == 0) {
1787 MBEDTLS_SSL_DEBUG_MSG(
Jerry Yub47b2992023-10-18 15:50:35 +08001788 1, ("EarlyData: no early data extension received."));
Jerry Yuab0da372023-02-08 13:55:24 +08001789 ssl->early_data_status = MBEDTLS_SSL_EARLY_DATA_STATUS_NOT_RECEIVED;
1790 return;
1791 }
1792
1793 ssl->early_data_status = MBEDTLS_SSL_EARLY_DATA_STATUS_REJECTED;
1794
Jerry Yu985c9672022-12-04 14:06:30 +08001795 if (ssl->conf->early_data_enabled == MBEDTLS_SSL_EARLY_DATA_DISABLED) {
1796 MBEDTLS_SSL_DEBUG_MSG(
Jerry Yu985c9672022-12-04 14:06:30 +08001797 1,
Jerry Yu454dda32023-10-31 15:13:54 +08001798 ("EarlyData: rejected, feature disabled in server configuration."));
Jerry Yu985c9672022-12-04 14:06:30 +08001799 return;
1800 }
1801
Jerry Yu454dda32023-10-31 15:13:54 +08001802 if (!handshake->resume) {
1803 /* We currently support early data only in the case of PSKs established
1804 via a NewSessionTicket message thus in the case of a session
1805 resumption. */
Jerry Yu985c9672022-12-04 14:06:30 +08001806 MBEDTLS_SSL_DEBUG_MSG(
Jerry Yu7ef9fd82023-11-07 14:30:38 +08001807 1, ("EarlyData: rejected, not a session resumption."));
Jerry Yu985c9672022-12-04 14:06:30 +08001808 return;
1809 }
1810
Jerry Yu82fd6c12023-10-31 16:32:19 +08001811 /* RFC 8446 4.2.10
1812 *
1813 * In order to accept early data, the server MUST have accepted a PSK cipher
1814 * suite and selected the first key offered in the client's "pre_shared_key"
1815 * extension. In addition, it MUST verify that the following values are the
1816 * same as those associated with the selected PSK:
1817 * - The TLS version number
1818 * - The selected cipher suite
1819 * - The selected ALPN [RFC7301] protocol, if any
1820 *
1821 * NOTE:
Jerry Yu7ef9fd82023-11-07 14:30:38 +08001822 * - The TLS version number is checked in
1823 * ssl_tls13_offered_psks_check_identity_match_ticket().
1824 * - ALPN is not checked for the time being (TODO).
Jerry Yu82fd6c12023-10-31 16:32:19 +08001825 */
1826
1827 if (handshake->selected_identity != 0) {
1828 MBEDTLS_SSL_DEBUG_MSG(
Jerry Yu7ef9fd82023-11-07 14:30:38 +08001829 1, ("EarlyData: rejected, the selected key in "
1830 "`pre_shared_key` is not the first one."));
Jerry Yu82fd6c12023-10-31 16:32:19 +08001831 return;
1832 }
1833
1834 if (handshake->ciphersuite_info->id !=
1835 ssl->session_negotiate->ciphersuite) {
1836 MBEDTLS_SSL_DEBUG_MSG(
Jerry Yu7ef9fd82023-11-07 14:30:38 +08001837 1, ("EarlyData: rejected, the selected ciphersuite is not the one "
1838 "of the selected pre-shared key."));
Jerry Yu82fd6c12023-10-31 16:32:19 +08001839 return;
1840
1841 }
Jerry Yu985c9672022-12-04 14:06:30 +08001842
Jerry Yu985c9672022-12-04 14:06:30 +08001843
1844 ssl->early_data_status = MBEDTLS_SSL_EARLY_DATA_STATUS_ACCEPTED;
1845
Jerry Yuab0da372023-02-08 13:55:24 +08001846}
1847#endif /* MBEDTLS_SSL_EARLY_DATA */
1848
XiaokangQian75fe8c72022-06-15 09:42:45 +00001849/* Update the handshake state machine */
1850
Ronald Cronce7d76e2022-07-08 18:56:49 +02001851MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01001852static int ssl_tls13_postprocess_client_hello(mbedtls_ssl_context *ssl)
XiaokangQian75fe8c72022-06-15 09:42:45 +00001853{
1854 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1855
XiaokangQian7807f9f2022-02-15 10:04:37 +00001856 /*
XiaokangQianfb665a82022-06-15 03:57:21 +00001857 * Server certificate selection
1858 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001859 if (ssl->conf->f_cert_cb && (ret = ssl->conf->f_cert_cb(ssl)) != 0) {
1860 MBEDTLS_SSL_DEBUG_RET(1, "f_cert_cb", ret);
1861 return ret;
XiaokangQianfb665a82022-06-15 03:57:21 +00001862 }
1863#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
1864 ssl->handshake->sni_name = NULL;
1865 ssl->handshake->sni_name_len = 0;
1866#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
XiaokangQian7807f9f2022-02-15 10:04:37 +00001867
Gilles Peskine449bd832023-01-11 14:50:10 +01001868 ret = mbedtls_ssl_tls13_key_schedule_stage_early(ssl);
1869 if (ret != 0) {
1870 MBEDTLS_SSL_DEBUG_RET(1,
1871 "mbedtls_ssl_tls1_3_key_schedule_stage_early", ret);
1872 return ret;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001873 }
1874
Jerry Yuab0da372023-02-08 13:55:24 +08001875#if defined(MBEDTLS_SSL_EARLY_DATA)
1876 /* There is enough information, update early data state. */
1877 ssl_tls13_update_early_data_status(ssl);
Jerry Yuab0da372023-02-08 13:55:24 +08001878#endif /* MBEDTLS_SSL_EARLY_DATA */
1879
Gilles Peskine449bd832023-01-11 14:50:10 +01001880 return 0;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001881
1882}
1883
1884/*
XiaokangQianed582dd2022-04-13 08:21:05 +00001885 * Main entry point from the state machine; orchestrates the otherfunctions.
1886 */
1887
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001888MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01001889static int ssl_tls13_process_client_hello(mbedtls_ssl_context *ssl)
XiaokangQianed582dd2022-04-13 08:21:05 +00001890{
1891
XiaokangQian08037552022-04-20 07:16:41 +00001892 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine449bd832023-01-11 14:50:10 +01001893 unsigned char *buf = NULL;
XiaokangQianed582dd2022-04-13 08:21:05 +00001894 size_t buflen = 0;
Jerry Yu4ca91402022-05-09 15:50:57 +08001895 int parse_client_hello_ret;
1896
Gilles Peskine449bd832023-01-11 14:50:10 +01001897 MBEDTLS_SSL_DEBUG_MSG(2, ("=> parse client hello"));
XiaokangQianed582dd2022-04-13 08:21:05 +00001898
Gilles Peskine449bd832023-01-11 14:50:10 +01001899 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_tls13_fetch_handshake_msg(
1900 ssl, MBEDTLS_SSL_HS_CLIENT_HELLO,
1901 &buf, &buflen));
XiaokangQianed582dd2022-04-13 08:21:05 +00001902
Gilles Peskine449bd832023-01-11 14:50:10 +01001903 MBEDTLS_SSL_PROC_CHK_NEG(ssl_tls13_parse_client_hello(ssl, buf,
1904 buf + buflen));
Ronald Cron5af4c7f2023-03-07 20:46:59 +01001905 parse_client_hello_ret = ret; /* Store positive return value of
1906 * parse_client_hello,
1907 * as negative error codes are handled
Jerry Yuf41553b2022-05-09 22:20:30 +08001908 * by MBEDTLS_SSL_PROC_CHK_NEG. */
Jerry Yu4ca91402022-05-09 15:50:57 +08001909
Ronald Cronb828c7d2023-04-03 16:37:22 +02001910 /*
1911 * Version 1.2 of the protocol has been chosen, set the
1912 * ssl->keep_current_message flag for the ClientHello to be kept and parsed
1913 * as a TLS 1.2 ClientHello. We also change ssl->tls_version to
1914 * MBEDTLS_SSL_VERSION_TLS1_2 thus from now on mbedtls_ssl_handshake_step()
1915 * will dispatch to the TLS 1.2 state machine.
1916 */
Ronald Cron5af4c7f2023-03-07 20:46:59 +01001917 if (SSL_CLIENT_HELLO_TLS1_2 == parse_client_hello_ret) {
1918 ssl->keep_current_message = 1;
1919 ssl->tls_version = MBEDTLS_SSL_VERSION_TLS1_2;
1920 return 0;
1921 }
1922
Gilles Peskine449bd832023-01-11 14:50:10 +01001923 MBEDTLS_SSL_PROC_CHK(ssl_tls13_postprocess_client_hello(ssl));
Jerry Yu582dd062022-04-22 21:59:01 +08001924
Ronald Cron5af4c7f2023-03-07 20:46:59 +01001925 if (SSL_CLIENT_HELLO_OK == parse_client_hello_ret) {
Gilles Peskine449bd832023-01-11 14:50:10 +01001926 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_SERVER_HELLO);
1927 } else {
1928 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_HELLO_RETRY_REQUEST);
1929 }
XiaokangQianed582dd2022-04-13 08:21:05 +00001930
1931cleanup:
1932
Gilles Peskine449bd832023-01-11 14:50:10 +01001933 MBEDTLS_SSL_DEBUG_MSG(2, ("<= parse client hello"));
1934 return ret;
XiaokangQianed582dd2022-04-13 08:21:05 +00001935}
1936
1937/*
Jerry Yu1c3e6882022-04-20 21:23:40 +08001938 * Handler for MBEDTLS_SSL_SERVER_HELLO
Jerry Yu5b64ae92022-03-30 17:15:02 +08001939 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001940MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01001941static int ssl_tls13_prepare_server_hello(mbedtls_ssl_context *ssl)
Jerry Yuf4b27e42022-03-30 17:32:21 +08001942{
Jerry Yu637a3f12022-04-20 21:37:58 +08001943 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1944 unsigned char *server_randbytes =
Gilles Peskine449bd832023-01-11 14:50:10 +01001945 ssl->handshake->randbytes + MBEDTLS_CLIENT_HELLO_RANDOM_LEN;
1946 if (ssl->conf->f_rng == NULL) {
1947 MBEDTLS_SSL_DEBUG_MSG(1, ("no RNG provided"));
1948 return MBEDTLS_ERR_SSL_NO_RNG;
Jerry Yuf4b27e42022-03-30 17:32:21 +08001949 }
1950
Gilles Peskine449bd832023-01-11 14:50:10 +01001951 if ((ret = ssl->conf->f_rng(ssl->conf->p_rng, server_randbytes,
1952 MBEDTLS_SERVER_HELLO_RANDOM_LEN)) != 0) {
1953 MBEDTLS_SSL_DEBUG_RET(1, "f_rng", ret);
1954 return ret;
Jerry Yuf4b27e42022-03-30 17:32:21 +08001955 }
1956
Gilles Peskine449bd832023-01-11 14:50:10 +01001957 MBEDTLS_SSL_DEBUG_BUF(3, "server hello, random bytes", server_randbytes,
1958 MBEDTLS_SERVER_HELLO_RANDOM_LEN);
Jerry Yuf4b27e42022-03-30 17:32:21 +08001959
1960#if defined(MBEDTLS_HAVE_TIME)
YxCda609132023-05-22 12:08:12 -07001961 ssl->session_negotiate->start = mbedtls_time(NULL);
Jerry Yuf4b27e42022-03-30 17:32:21 +08001962#endif /* MBEDTLS_HAVE_TIME */
1963
Gilles Peskine449bd832023-01-11 14:50:10 +01001964 return ret;
Jerry Yuf4b27e42022-03-30 17:32:21 +08001965}
1966
Jerry Yu3bf2c642022-03-30 22:02:12 +08001967/*
Jerry Yue74e04a2022-04-21 09:23:16 +08001968 * ssl_tls13_write_server_hello_supported_versions_ext ():
Jerry Yu3bf2c642022-03-30 22:02:12 +08001969 *
1970 * struct {
Jerry Yufb9f54d2022-04-06 10:08:34 +08001971 * ProtocolVersion selected_version;
Jerry Yu3bf2c642022-03-30 22:02:12 +08001972 * } SupportedVersions;
1973 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001974MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yue74e04a2022-04-21 09:23:16 +08001975static int ssl_tls13_write_server_hello_supported_versions_ext(
Gilles Peskine449bd832023-01-11 14:50:10 +01001976 mbedtls_ssl_context *ssl,
1977 unsigned char *buf,
1978 unsigned char *end,
1979 size_t *out_len)
Jerry Yu3bf2c642022-03-30 22:02:12 +08001980{
Jerry Yu3bf2c642022-03-30 22:02:12 +08001981 *out_len = 0;
1982
Gilles Peskine449bd832023-01-11 14:50:10 +01001983 MBEDTLS_SSL_DEBUG_MSG(3, ("server hello, write selected version"));
Jerry Yu3bf2c642022-03-30 22:02:12 +08001984
1985 /* Check if we have space to write the extension:
1986 * - extension_type (2 bytes)
1987 * - extension_data_length (2 bytes)
Jerry Yu349a6132022-04-14 20:52:56 +08001988 * - selected_version (2 bytes)
Jerry Yu3bf2c642022-03-30 22:02:12 +08001989 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001990 MBEDTLS_SSL_CHK_BUF_PTR(buf, end, 6);
Jerry Yu3bf2c642022-03-30 22:02:12 +08001991
Gilles Peskine449bd832023-01-11 14:50:10 +01001992 MBEDTLS_PUT_UINT16_BE(MBEDTLS_TLS_EXT_SUPPORTED_VERSIONS, buf, 0);
Jerry Yu3bf2c642022-03-30 22:02:12 +08001993
Gilles Peskine449bd832023-01-11 14:50:10 +01001994 MBEDTLS_PUT_UINT16_BE(2, buf, 2);
Jerry Yu3bf2c642022-03-30 22:02:12 +08001995
Gilles Peskine449bd832023-01-11 14:50:10 +01001996 mbedtls_ssl_write_version(buf + 4,
1997 ssl->conf->transport,
1998 ssl->tls_version);
Jerry Yu3bf2c642022-03-30 22:02:12 +08001999
Gilles Peskine449bd832023-01-11 14:50:10 +01002000 MBEDTLS_SSL_DEBUG_MSG(3, ("supported version: [%04x]",
2001 ssl->tls_version));
Jerry Yu3bf2c642022-03-30 22:02:12 +08002002
Jerry Yu349a6132022-04-14 20:52:56 +08002003 *out_len = 6;
Jerry Yu3bf2c642022-03-30 22:02:12 +08002004
Jerry Yub95dd362022-11-08 21:19:34 +08002005 mbedtls_ssl_tls13_set_hs_sent_ext_mask(
Gilles Peskine449bd832023-01-11 14:50:10 +01002006 ssl, MBEDTLS_TLS_EXT_SUPPORTED_VERSIONS);
Jerry Yub95dd362022-11-08 21:19:34 +08002007
Gilles Peskine449bd832023-01-11 14:50:10 +01002008 return 0;
Jerry Yu3bf2c642022-03-30 22:02:12 +08002009}
2010
Jerry Yud9436a12022-04-20 22:28:09 +08002011
Jerry Yu3bf2c642022-03-30 22:02:12 +08002012
2013/* Generate and export a single key share. For hybrid KEMs, this can
2014 * be called multiple times with the different components of the hybrid. */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002015MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002016static int ssl_tls13_generate_and_write_key_share(mbedtls_ssl_context *ssl,
2017 uint16_t named_group,
2018 unsigned char *buf,
2019 unsigned char *end,
2020 size_t *out_len)
Jerry Yu3bf2c642022-03-30 22:02:12 +08002021{
2022 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yu955ddd72022-04-22 22:27:33 +08002023
2024 *out_len = 0;
2025
Valerio Settic9ae8622023-07-25 11:23:50 +02002026#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_EPHEMERAL_ENABLED)
Przemek Stekiel29c219c2023-05-31 15:21:04 +02002027 if (mbedtls_ssl_tls13_named_group_is_ecdhe(named_group) ||
Przemek Stekield5f79e72023-06-29 09:08:43 +02002028 mbedtls_ssl_tls13_named_group_is_ffdh(named_group)) {
Przemek Stekiel408569f2023-07-06 11:26:44 +02002029 ret = mbedtls_ssl_tls13_generate_and_write_xxdh_key_exchange(
Gilles Peskine449bd832023-01-11 14:50:10 +01002030 ssl, named_group, buf, end, out_len);
2031 if (ret != 0) {
Jerry Yu89e103c2022-03-30 22:43:29 +08002032 MBEDTLS_SSL_DEBUG_RET(
Przemek Stekiel408569f2023-07-06 11:26:44 +02002033 1, "mbedtls_ssl_tls13_generate_and_write_xxdh_key_exchange",
Gilles Peskine449bd832023-01-11 14:50:10 +01002034 ret);
2035 return ret;
Jerry Yu3bf2c642022-03-30 22:02:12 +08002036 }
Gilles Peskine449bd832023-01-11 14:50:10 +01002037 } else
Valerio Settic9ae8622023-07-25 11:23:50 +02002038#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_EPHEMERAL_ENABLED */
Gilles Peskine449bd832023-01-11 14:50:10 +01002039 if (0 /* Other kinds of KEMs */) {
2040 } else {
Jerry Yu955ddd72022-04-22 22:27:33 +08002041 ((void) ssl);
2042 ((void) named_group);
2043 ((void) buf);
2044 ((void) end);
Jerry Yu3bf2c642022-03-30 22:02:12 +08002045 ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
2046 }
2047
Gilles Peskine449bd832023-01-11 14:50:10 +01002048 return ret;
Jerry Yu3bf2c642022-03-30 22:02:12 +08002049}
2050
2051/*
2052 * ssl_tls13_write_key_share_ext
2053 *
2054 * Structure of key_share extension in ServerHello:
2055 *
Jerry Yu1c3e6882022-04-20 21:23:40 +08002056 * struct {
2057 * NamedGroup group;
2058 * opaque key_exchange<1..2^16-1>;
2059 * } KeyShareEntry;
2060 * struct {
2061 * KeyShareEntry server_share;
2062 * } KeyShareServerHello;
Jerry Yu3bf2c642022-03-30 22:02:12 +08002063 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002064MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002065static int ssl_tls13_write_key_share_ext(mbedtls_ssl_context *ssl,
2066 unsigned char *buf,
2067 unsigned char *end,
2068 size_t *out_len)
Jerry Yu3bf2c642022-03-30 22:02:12 +08002069{
Jerry Yu955ddd72022-04-22 22:27:33 +08002070 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yu3bf2c642022-03-30 22:02:12 +08002071 unsigned char *p = buf;
Jerry Yu57d48412022-04-20 21:50:42 +08002072 uint16_t group = ssl->handshake->offered_group_id;
Jerry Yu3bf2c642022-03-30 22:02:12 +08002073 unsigned char *server_share = buf + 4;
Jerry Yu3bf2c642022-03-30 22:02:12 +08002074 size_t key_exchange_length;
Jerry Yu3bf2c642022-03-30 22:02:12 +08002075
2076 *out_len = 0;
2077
Gilles Peskine449bd832023-01-11 14:50:10 +01002078 MBEDTLS_SSL_DEBUG_MSG(3, ("server hello, adding key share extension"));
Jerry Yu3bf2c642022-03-30 22:02:12 +08002079
Gilles Peskine449bd832023-01-11 14:50:10 +01002080 MBEDTLS_SSL_DEBUG_MSG(2, ("server hello, write selected_group: %s (%04x)",
2081 mbedtls_ssl_named_group_to_str(group),
2082 group));
Jerry Yu58af2332022-09-06 11:19:31 +08002083
Jerry Yu3bf2c642022-03-30 22:02:12 +08002084 /* Check if we have space for header and length fields:
2085 * - extension_type (2 bytes)
2086 * - extension_data_length (2 bytes)
2087 * - group (2 bytes)
2088 * - key_exchange_length (2 bytes)
2089 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002090 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 8);
2091 MBEDTLS_PUT_UINT16_BE(MBEDTLS_TLS_EXT_KEY_SHARE, p, 0);
2092 MBEDTLS_PUT_UINT16_BE(group, server_share, 0);
Jerry Yu3bf2c642022-03-30 22:02:12 +08002093 p += 8;
Jerry Yu57d48412022-04-20 21:50:42 +08002094
Jerry Yu3bf2c642022-03-30 22:02:12 +08002095 /* When we introduce PQC-ECDHE hybrids, we'll want to call this
2096 * function multiple times. */
Jerry Yu955ddd72022-04-22 22:27:33 +08002097 ret = ssl_tls13_generate_and_write_key_share(
Gilles Peskine449bd832023-01-11 14:50:10 +01002098 ssl, group, server_share + 4, end, &key_exchange_length);
2099 if (ret != 0) {
2100 return ret;
2101 }
Jerry Yu3bf2c642022-03-30 22:02:12 +08002102 p += key_exchange_length;
Jerry Yuc1be19f2022-04-23 16:11:39 +08002103
Gilles Peskine449bd832023-01-11 14:50:10 +01002104 MBEDTLS_PUT_UINT16_BE(key_exchange_length, server_share + 2, 0);
Jerry Yu3bf2c642022-03-30 22:02:12 +08002105
Gilles Peskine449bd832023-01-11 14:50:10 +01002106 MBEDTLS_PUT_UINT16_BE(p - server_share, buf, 2);
Jerry Yu3bf2c642022-03-30 22:02:12 +08002107
Jerry Yu57d48412022-04-20 21:50:42 +08002108 *out_len = p - buf;
Jerry Yu955ddd72022-04-22 22:27:33 +08002109
Gilles Peskine449bd832023-01-11 14:50:10 +01002110 mbedtls_ssl_tls13_set_hs_sent_ext_mask(ssl, MBEDTLS_TLS_EXT_KEY_SHARE);
Jerry Yub95dd362022-11-08 21:19:34 +08002111
Gilles Peskine449bd832023-01-11 14:50:10 +01002112 return 0;
Jerry Yu3bf2c642022-03-30 22:02:12 +08002113}
Jerry Yud9436a12022-04-20 22:28:09 +08002114
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002115MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002116static int ssl_tls13_write_hrr_key_share_ext(mbedtls_ssl_context *ssl,
2117 unsigned char *buf,
2118 unsigned char *end,
2119 size_t *out_len)
Jerry Yu23f7a6f2022-04-23 15:16:45 +08002120{
2121 uint16_t selected_group = ssl->handshake->hrr_selected_group;
2122 /* key_share Extension
2123 *
2124 * struct {
2125 * select (Handshake.msg_type) {
2126 * ...
2127 * case hello_retry_request:
2128 * NamedGroup selected_group;
2129 * ...
2130 * };
2131 * } KeyShare;
2132 */
2133
2134 *out_len = 0;
2135
Jerry Yub0ac10b2022-05-05 11:10:08 +08002136 /*
2137 * For a pure PSK key exchange, there is no group to agree upon. The purpose
2138 * of the HRR is then to transmit a cookie to force the client to demonstrate
2139 * reachability at their apparent network address (primarily useful for DTLS).
2140 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002141 if (!mbedtls_ssl_tls13_key_exchange_mode_with_ephemeral(ssl)) {
2142 return 0;
2143 }
Jerry Yu23f7a6f2022-04-23 15:16:45 +08002144
2145 /* We should only send the key_share extension if the client's initial
2146 * key share was not acceptable. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002147 if (ssl->handshake->offered_group_id != 0) {
2148 MBEDTLS_SSL_DEBUG_MSG(4, ("Skip key_share extension in HRR"));
2149 return 0;
Jerry Yu23f7a6f2022-04-23 15:16:45 +08002150 }
2151
Gilles Peskine449bd832023-01-11 14:50:10 +01002152 if (selected_group == 0) {
2153 MBEDTLS_SSL_DEBUG_MSG(1, ("no matching named group found"));
2154 return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
Jerry Yu23f7a6f2022-04-23 15:16:45 +08002155 }
2156
Jerry Yub0ac10b2022-05-05 11:10:08 +08002157 /* Check if we have enough space:
2158 * - extension_type (2 bytes)
2159 * - extension_data_length (2 bytes)
2160 * - selected_group (2 bytes)
2161 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002162 MBEDTLS_SSL_CHK_BUF_PTR(buf, end, 6);
Jerry Yu23f7a6f2022-04-23 15:16:45 +08002163
Gilles Peskine449bd832023-01-11 14:50:10 +01002164 MBEDTLS_PUT_UINT16_BE(MBEDTLS_TLS_EXT_KEY_SHARE, buf, 0);
2165 MBEDTLS_PUT_UINT16_BE(2, buf, 2);
2166 MBEDTLS_PUT_UINT16_BE(selected_group, buf, 4);
Jerry Yu23f7a6f2022-04-23 15:16:45 +08002167
Gilles Peskine449bd832023-01-11 14:50:10 +01002168 MBEDTLS_SSL_DEBUG_MSG(3,
2169 ("HRR selected_group: %s (%x)",
2170 mbedtls_ssl_named_group_to_str(selected_group),
2171 selected_group));
Jerry Yu23f7a6f2022-04-23 15:16:45 +08002172
2173 *out_len = 6;
Jerry Yu23f7a6f2022-04-23 15:16:45 +08002174
Gilles Peskine449bd832023-01-11 14:50:10 +01002175 mbedtls_ssl_tls13_set_hs_sent_ext_mask(ssl, MBEDTLS_TLS_EXT_KEY_SHARE);
Jerry Yub95dd362022-11-08 21:19:34 +08002176
Gilles Peskine449bd832023-01-11 14:50:10 +01002177 return 0;
Jerry Yu23f7a6f2022-04-23 15:16:45 +08002178}
Jerry Yu3bf2c642022-03-30 22:02:12 +08002179
2180/*
2181 * Structure of ServerHello message:
2182 *
2183 * struct {
2184 * ProtocolVersion legacy_version = 0x0303; // TLS v1.2
2185 * Random random;
2186 * opaque legacy_session_id_echo<0..32>;
2187 * CipherSuite cipher_suite;
2188 * uint8 legacy_compression_method = 0;
2189 * Extension extensions<6..2^16-1>;
2190 * } ServerHello;
2191 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002192MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002193static int ssl_tls13_write_server_hello_body(mbedtls_ssl_context *ssl,
2194 unsigned char *buf,
2195 unsigned char *end,
2196 size_t *out_len,
2197 int is_hrr)
Jerry Yu56404d72022-03-30 17:36:13 +08002198{
Jerry Yu955ddd72022-04-22 22:27:33 +08002199 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yu3bf2c642022-03-30 22:02:12 +08002200 unsigned char *p = buf;
Jerry Yud9436a12022-04-20 22:28:09 +08002201 unsigned char *p_extensions_len;
Jerry Yufbe3e642022-04-25 19:31:51 +08002202 size_t output_len;
Jerry Yu3bf2c642022-03-30 22:02:12 +08002203
2204 *out_len = 0;
Jerry Yu50e00e32022-10-31 14:45:01 +08002205 ssl->handshake->sent_extensions = MBEDTLS_SSL_EXT_MASK_NONE;
Jerry Yu3bf2c642022-03-30 22:02:12 +08002206
Jerry Yucfc04b32022-04-21 09:31:58 +08002207 /* ...
2208 * ProtocolVersion legacy_version = 0x0303; // TLS 1.2
2209 * ...
2210 * with ProtocolVersion defined as:
2211 * uint16 ProtocolVersion;
Jerry Yu3bf2c642022-03-30 22:02:12 +08002212 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002213 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 2);
2214 MBEDTLS_PUT_UINT16_BE(0x0303, p, 0);
Jerry Yu3bf2c642022-03-30 22:02:12 +08002215 p += 2;
2216
Jerry Yu1c3e6882022-04-20 21:23:40 +08002217 /* ...
2218 * Random random;
2219 * ...
Jerry Yucfc04b32022-04-21 09:31:58 +08002220 * with Random defined as:
2221 * opaque Random[MBEDTLS_SERVER_HELLO_RANDOM_LEN];
Jerry Yu1c3e6882022-04-20 21:23:40 +08002222 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002223 MBEDTLS_SSL_CHK_BUF_PTR(p, end, MBEDTLS_SERVER_HELLO_RANDOM_LEN);
2224 if (is_hrr) {
2225 memcpy(p, mbedtls_ssl_tls13_hello_retry_request_magic,
2226 MBEDTLS_SERVER_HELLO_RANDOM_LEN);
2227 } else {
2228 memcpy(p, &ssl->handshake->randbytes[MBEDTLS_CLIENT_HELLO_RANDOM_LEN],
2229 MBEDTLS_SERVER_HELLO_RANDOM_LEN);
Jerry Yu23f7a6f2022-04-23 15:16:45 +08002230 }
Gilles Peskine449bd832023-01-11 14:50:10 +01002231 MBEDTLS_SSL_DEBUG_BUF(3, "server hello, random bytes",
2232 p, MBEDTLS_SERVER_HELLO_RANDOM_LEN);
Jerry Yu3bf2c642022-03-30 22:02:12 +08002233 p += MBEDTLS_SERVER_HELLO_RANDOM_LEN;
2234
Jerry Yucfc04b32022-04-21 09:31:58 +08002235 /* ...
2236 * opaque legacy_session_id_echo<0..32>;
2237 * ...
Jerry Yu3bf2c642022-03-30 22:02:12 +08002238 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002239 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 1 + ssl->session_negotiate->id_len);
2240 *p++ = (unsigned char) ssl->session_negotiate->id_len;
2241 if (ssl->session_negotiate->id_len > 0) {
2242 memcpy(p, &ssl->session_negotiate->id[0],
2243 ssl->session_negotiate->id_len);
Jerry Yu3bf2c642022-03-30 22:02:12 +08002244 p += ssl->session_negotiate->id_len;
Jerry Yu955ddd72022-04-22 22:27:33 +08002245
Gilles Peskine449bd832023-01-11 14:50:10 +01002246 MBEDTLS_SSL_DEBUG_BUF(3, "session id", ssl->session_negotiate->id,
2247 ssl->session_negotiate->id_len);
Jerry Yu3bf2c642022-03-30 22:02:12 +08002248 }
2249
Jerry Yucfc04b32022-04-21 09:31:58 +08002250 /* ...
2251 * CipherSuite cipher_suite;
2252 * ...
2253 * with CipherSuite defined as:
2254 * uint8 CipherSuite[2];
Jerry Yu3bf2c642022-03-30 22:02:12 +08002255 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002256 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 2);
2257 MBEDTLS_PUT_UINT16_BE(ssl->session_negotiate->ciphersuite, p, 0);
Jerry Yu3bf2c642022-03-30 22:02:12 +08002258 p += 2;
Gilles Peskine449bd832023-01-11 14:50:10 +01002259 MBEDTLS_SSL_DEBUG_MSG(3,
2260 ("server hello, chosen ciphersuite: %s ( id=%d )",
2261 mbedtls_ssl_get_ciphersuite_name(
2262 ssl->session_negotiate->ciphersuite),
2263 ssl->session_negotiate->ciphersuite));
Jerry Yu3bf2c642022-03-30 22:02:12 +08002264
Jerry Yucfc04b32022-04-21 09:31:58 +08002265 /* ...
2266 * uint8 legacy_compression_method = 0;
2267 * ...
2268 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002269 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 1);
Thomas Daubney31e03a82022-07-25 15:59:25 +01002270 *p++ = MBEDTLS_SSL_COMPRESS_NULL;
Jerry Yu3bf2c642022-03-30 22:02:12 +08002271
Jerry Yucfc04b32022-04-21 09:31:58 +08002272 /* ...
2273 * Extension extensions<6..2^16-1>;
2274 * ...
2275 * struct {
2276 * ExtensionType extension_type; (2 bytes)
2277 * opaque extension_data<0..2^16-1>;
2278 * } Extension;
2279 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002280 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 2);
Jerry Yud9436a12022-04-20 22:28:09 +08002281 p_extensions_len = p;
Jerry Yu3bf2c642022-03-30 22:02:12 +08002282 p += 2;
2283
Gilles Peskine449bd832023-01-11 14:50:10 +01002284 if ((ret = ssl_tls13_write_server_hello_supported_versions_ext(
2285 ssl, p, end, &output_len)) != 0) {
Jerry Yu955ddd72022-04-22 22:27:33 +08002286 MBEDTLS_SSL_DEBUG_RET(
Gilles Peskine449bd832023-01-11 14:50:10 +01002287 1, "ssl_tls13_write_server_hello_supported_versions_ext", ret);
2288 return ret;
Jerry Yu3bf2c642022-03-30 22:02:12 +08002289 }
2290 p += output_len;
2291
Gilles Peskine449bd832023-01-11 14:50:10 +01002292 if (mbedtls_ssl_tls13_key_exchange_mode_with_ephemeral(ssl)) {
2293 if (is_hrr) {
2294 ret = ssl_tls13_write_hrr_key_share_ext(ssl, p, end, &output_len);
2295 } else {
2296 ret = ssl_tls13_write_key_share_ext(ssl, p, end, &output_len);
2297 }
2298 if (ret != 0) {
2299 return ret;
2300 }
Jerry Yu3bf2c642022-03-30 22:02:12 +08002301 p += output_len;
2302 }
Jerry Yu3bf2c642022-03-30 22:02:12 +08002303
Ronald Cron41a443a2022-10-04 16:38:25 +02002304#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01002305 if (!is_hrr && mbedtls_ssl_tls13_key_exchange_mode_with_psk(ssl)) {
2306 ret = ssl_tls13_write_server_pre_shared_key_ext(ssl, p, end, &output_len);
2307 if (ret != 0) {
2308 MBEDTLS_SSL_DEBUG_RET(1, "ssl_tls13_write_server_pre_shared_key_ext",
2309 ret);
2310 return ret;
Jerry Yu032b15ce2022-07-11 06:10:03 +00002311 }
2312 p += output_len;
2313 }
Ronald Cron41a443a2022-10-04 16:38:25 +02002314#endif
Jerry Yu032b15ce2022-07-11 06:10:03 +00002315
Gilles Peskine449bd832023-01-11 14:50:10 +01002316 MBEDTLS_PUT_UINT16_BE(p - p_extensions_len - 2, p_extensions_len, 0);
Jerry Yu3bf2c642022-03-30 22:02:12 +08002317
Gilles Peskine449bd832023-01-11 14:50:10 +01002318 MBEDTLS_SSL_DEBUG_BUF(4, "server hello extensions",
2319 p_extensions_len, p - p_extensions_len);
Jerry Yu3bf2c642022-03-30 22:02:12 +08002320
Jerry Yud9436a12022-04-20 22:28:09 +08002321 *out_len = p - buf;
Jerry Yu3bf2c642022-03-30 22:02:12 +08002322
Gilles Peskine449bd832023-01-11 14:50:10 +01002323 MBEDTLS_SSL_DEBUG_BUF(3, "server hello", buf, *out_len);
Jerry Yu3bf2c642022-03-30 22:02:12 +08002324
Jerry Yu7de2ff02022-11-08 21:43:46 +08002325 MBEDTLS_SSL_PRINT_EXTS(
Jerry Yu4b8f2f72022-10-31 13:31:22 +08002326 3, is_hrr ? MBEDTLS_SSL_TLS1_3_HS_HELLO_RETRY_REQUEST :
Gilles Peskine449bd832023-01-11 14:50:10 +01002327 MBEDTLS_SSL_HS_SERVER_HELLO,
2328 ssl->handshake->sent_extensions);
Jerry Yu4b8f2f72022-10-31 13:31:22 +08002329
Gilles Peskine449bd832023-01-11 14:50:10 +01002330 return ret;
Jerry Yu56404d72022-03-30 17:36:13 +08002331}
2332
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002333MBEDTLS_CHECK_RETURN_CRITICAL
Xiaokang Qian934ce6f2023-02-06 10:23:04 +00002334static int ssl_tls13_finalize_server_hello(mbedtls_ssl_context *ssl)
Jerry Yue110d252022-05-05 10:19:22 +08002335{
2336 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine449bd832023-01-11 14:50:10 +01002337 ret = mbedtls_ssl_tls13_compute_handshake_transform(ssl);
2338 if (ret != 0) {
2339 MBEDTLS_SSL_DEBUG_RET(1,
2340 "mbedtls_ssl_tls13_compute_handshake_transform",
2341 ret);
2342 return ret;
Jerry Yue110d252022-05-05 10:19:22 +08002343 }
2344
Gilles Peskine449bd832023-01-11 14:50:10 +01002345 return ret;
Jerry Yue110d252022-05-05 10:19:22 +08002346}
2347
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002348MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002349static int ssl_tls13_write_server_hello(mbedtls_ssl_context *ssl)
Jerry Yu5b64ae92022-03-30 17:15:02 +08002350{
Jerry Yu637a3f12022-04-20 21:37:58 +08002351 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yuf4b27e42022-03-30 17:32:21 +08002352 unsigned char *buf;
2353 size_t buf_len, msg_len;
2354
Gilles Peskine449bd832023-01-11 14:50:10 +01002355 MBEDTLS_SSL_DEBUG_MSG(2, ("=> write server hello"));
Jerry Yuf4b27e42022-03-30 17:32:21 +08002356
Gilles Peskine449bd832023-01-11 14:50:10 +01002357 MBEDTLS_SSL_PROC_CHK(ssl_tls13_prepare_server_hello(ssl));
Jerry Yuf4b27e42022-03-30 17:32:21 +08002358
Xiaokang Qian9f1747b2023-03-30 02:50:04 +00002359 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_start_handshake_msg(
2360 ssl, MBEDTLS_SSL_HS_SERVER_HELLO, &buf, &buf_len));
Jerry Yuf4b27e42022-03-30 17:32:21 +08002361
Gilles Peskine449bd832023-01-11 14:50:10 +01002362 MBEDTLS_SSL_PROC_CHK(ssl_tls13_write_server_hello_body(ssl, buf,
2363 buf + buf_len,
2364 &msg_len,
2365 0));
Jerry Yuf4b27e42022-03-30 17:32:21 +08002366
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +01002367 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_add_hs_msg_to_checksum(
Manuel Pégourié-Gonnard43cc1272023-02-06 11:48:19 +01002368 ssl, MBEDTLS_SSL_HS_SERVER_HELLO, buf, msg_len));
Jerry Yuf4b27e42022-03-30 17:32:21 +08002369
Gilles Peskine449bd832023-01-11 14:50:10 +01002370 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_finish_handshake_msg(
2371 ssl, buf_len, msg_len));
Jerry Yu637a3f12022-04-20 21:37:58 +08002372
Xiaokang Qian934ce6f2023-02-06 10:23:04 +00002373 MBEDTLS_SSL_PROC_CHK(ssl_tls13_finalize_server_hello(ssl));
Jerry Yue110d252022-05-05 10:19:22 +08002374
Gabor Mezei7b39bf12022-05-24 16:04:14 +02002375#if defined(MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE)
2376 /* The server sends a dummy change_cipher_spec record immediately
2377 * after its first handshake message. This may either be after
2378 * a ServerHello or a HelloRetryRequest.
2379 */
Gabor Mezei96ae9262022-06-28 11:45:18 +02002380 mbedtls_ssl_handshake_set_state(
Gilles Peskine449bd832023-01-11 14:50:10 +01002381 ssl, MBEDTLS_SSL_SERVER_CCS_AFTER_SERVER_HELLO);
Gabor Mezei7b39bf12022-05-24 16:04:14 +02002382#else
Gilles Peskine449bd832023-01-11 14:50:10 +01002383 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_ENCRYPTED_EXTENSIONS);
Gabor Mezei7b39bf12022-05-24 16:04:14 +02002384#endif /* MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE */
Jerry Yue110d252022-05-05 10:19:22 +08002385
Jerry Yuf4b27e42022-03-30 17:32:21 +08002386cleanup:
2387
Gilles Peskine449bd832023-01-11 14:50:10 +01002388 MBEDTLS_SSL_DEBUG_MSG(2, ("<= write server hello"));
2389 return ret;
Jerry Yu5b64ae92022-03-30 17:15:02 +08002390}
2391
Jerry Yu23d1a252022-05-12 18:08:59 +08002392
2393/*
2394 * Handler for MBEDTLS_SSL_HELLO_RETRY_REQUEST
2395 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002396MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002397static int ssl_tls13_prepare_hello_retry_request(mbedtls_ssl_context *ssl)
Jerry Yu23d1a252022-05-12 18:08:59 +08002398{
2399 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine449bd832023-01-11 14:50:10 +01002400 if (ssl->handshake->hello_retry_request_count > 0) {
2401 MBEDTLS_SSL_DEBUG_MSG(1, ("Too many HRRs"));
2402 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE,
2403 MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE);
2404 return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
Jerry Yu23d1a252022-05-12 18:08:59 +08002405 }
2406
2407 /*
2408 * Create stateless transcript hash for HRR
2409 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002410 MBEDTLS_SSL_DEBUG_MSG(4, ("Reset transcript for HRR"));
2411 ret = mbedtls_ssl_reset_transcript_for_hrr(ssl);
2412 if (ret != 0) {
2413 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_reset_transcript_for_hrr", ret);
2414 return ret;
Jerry Yu23d1a252022-05-12 18:08:59 +08002415 }
Gilles Peskine449bd832023-01-11 14:50:10 +01002416 mbedtls_ssl_session_reset_msg_layer(ssl, 0);
Jerry Yu23d1a252022-05-12 18:08:59 +08002417
Gilles Peskine449bd832023-01-11 14:50:10 +01002418 return 0;
Jerry Yu23d1a252022-05-12 18:08:59 +08002419}
2420
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002421MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002422static int ssl_tls13_write_hello_retry_request(mbedtls_ssl_context *ssl)
Jerry Yu23d1a252022-05-12 18:08:59 +08002423{
2424 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
2425 unsigned char *buf;
2426 size_t buf_len, msg_len;
2427
Gilles Peskine449bd832023-01-11 14:50:10 +01002428 MBEDTLS_SSL_DEBUG_MSG(2, ("=> write hello retry request"));
Jerry Yu23d1a252022-05-12 18:08:59 +08002429
Gilles Peskine449bd832023-01-11 14:50:10 +01002430 MBEDTLS_SSL_PROC_CHK(ssl_tls13_prepare_hello_retry_request(ssl));
Jerry Yu23d1a252022-05-12 18:08:59 +08002431
Gilles Peskine449bd832023-01-11 14:50:10 +01002432 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_start_handshake_msg(
2433 ssl, MBEDTLS_SSL_HS_SERVER_HELLO,
2434 &buf, &buf_len));
Jerry Yu23d1a252022-05-12 18:08:59 +08002435
Gilles Peskine449bd832023-01-11 14:50:10 +01002436 MBEDTLS_SSL_PROC_CHK(ssl_tls13_write_server_hello_body(ssl, buf,
2437 buf + buf_len,
2438 &msg_len,
2439 1));
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +01002440 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_add_hs_msg_to_checksum(
Manuel Pégourié-Gonnard43cc1272023-02-06 11:48:19 +01002441 ssl, MBEDTLS_SSL_HS_SERVER_HELLO, buf, msg_len));
Jerry Yu23d1a252022-05-12 18:08:59 +08002442
2443
Gilles Peskine449bd832023-01-11 14:50:10 +01002444 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_finish_handshake_msg(ssl, buf_len,
2445 msg_len));
Jerry Yu23d1a252022-05-12 18:08:59 +08002446
2447 ssl->handshake->hello_retry_request_count++;
2448
Gabor Mezei7b39bf12022-05-24 16:04:14 +02002449#if defined(MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE)
2450 /* The server sends a dummy change_cipher_spec record immediately
2451 * after its first handshake message. This may either be after
2452 * a ServerHello or a HelloRetryRequest.
2453 */
Gabor Mezei96ae9262022-06-28 11:45:18 +02002454 mbedtls_ssl_handshake_set_state(
Gilles Peskine449bd832023-01-11 14:50:10 +01002455 ssl, MBEDTLS_SSL_SERVER_CCS_AFTER_HELLO_RETRY_REQUEST);
Gabor Mezei7b39bf12022-05-24 16:04:14 +02002456#else
Gilles Peskine449bd832023-01-11 14:50:10 +01002457 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_CLIENT_HELLO);
Gabor Mezei7b39bf12022-05-24 16:04:14 +02002458#endif /* MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE */
Jerry Yu23d1a252022-05-12 18:08:59 +08002459
2460cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +01002461 MBEDTLS_SSL_DEBUG_MSG(2, ("<= write hello retry request"));
2462 return ret;
Jerry Yu23d1a252022-05-12 18:08:59 +08002463}
2464
Jerry Yu5b64ae92022-03-30 17:15:02 +08002465/*
Jerry Yu4d3841a2022-04-16 12:37:19 +08002466 * Handler for MBEDTLS_SSL_ENCRYPTED_EXTENSIONS
2467 */
Jerry Yu4d3841a2022-04-16 12:37:19 +08002468
2469/*
2470 * struct {
2471 * Extension extensions<0..2 ^ 16 - 1>;
2472 * } EncryptedExtensions;
2473 *
2474 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002475MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002476static int ssl_tls13_write_encrypted_extensions_body(mbedtls_ssl_context *ssl,
2477 unsigned char *buf,
2478 unsigned char *end,
2479 size_t *out_len)
Jerry Yu4d3841a2022-04-16 12:37:19 +08002480{
XiaokangQianacb39922022-06-17 10:18:48 +00002481 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yu4d3841a2022-04-16 12:37:19 +08002482 unsigned char *p = buf;
2483 size_t extensions_len = 0;
Jerry Yu8937eb42022-05-03 12:12:14 +08002484 unsigned char *p_extensions_len;
XiaokangQianacb39922022-06-17 10:18:48 +00002485 size_t output_len;
Jerry Yu9da5e5a2022-05-03 15:46:09 +08002486
Jerry Yu4d3841a2022-04-16 12:37:19 +08002487 *out_len = 0;
2488
Gilles Peskine449bd832023-01-11 14:50:10 +01002489 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 2);
Jerry Yu8937eb42022-05-03 12:12:14 +08002490 p_extensions_len = p;
Jerry Yu4d3841a2022-04-16 12:37:19 +08002491 p += 2;
2492
Jerry Yu8937eb42022-05-03 12:12:14 +08002493 ((void) ssl);
XiaokangQianacb39922022-06-17 10:18:48 +00002494 ((void) ret);
2495 ((void) output_len);
2496
2497#if defined(MBEDTLS_SSL_ALPN)
Gilles Peskine449bd832023-01-11 14:50:10 +01002498 ret = mbedtls_ssl_write_alpn_ext(ssl, p, end, &output_len);
2499 if (ret != 0) {
2500 return ret;
2501 }
XiaokangQian95d5f542022-06-24 02:29:26 +00002502 p += output_len;
XiaokangQianacb39922022-06-17 10:18:48 +00002503#endif /* MBEDTLS_SSL_ALPN */
Jerry Yu4d3841a2022-04-16 12:37:19 +08002504
Jerry Yu71c14f12022-12-12 11:10:35 +08002505#if defined(MBEDTLS_SSL_EARLY_DATA)
2506 if (ssl->early_data_status == MBEDTLS_SSL_EARLY_DATA_STATUS_ACCEPTED) {
2507 ret = mbedtls_ssl_tls13_write_early_data_ext(ssl, p, end, &output_len);
2508 if (ret != 0) {
2509 return ret;
2510 }
2511 p += output_len;
2512 }
2513#endif /* MBEDTLS_SSL_EARLY_DATA */
2514
Gilles Peskine449bd832023-01-11 14:50:10 +01002515 extensions_len = (p - p_extensions_len) - 2;
2516 MBEDTLS_PUT_UINT16_BE(extensions_len, p_extensions_len, 0);
Jerry Yu8937eb42022-05-03 12:12:14 +08002517
2518 *out_len = p - buf;
Jerry Yu4d3841a2022-04-16 12:37:19 +08002519
Gilles Peskine449bd832023-01-11 14:50:10 +01002520 MBEDTLS_SSL_DEBUG_BUF(4, "encrypted extensions", buf, *out_len);
Jerry Yu4d3841a2022-04-16 12:37:19 +08002521
Jerry Yu7de2ff02022-11-08 21:43:46 +08002522 MBEDTLS_SSL_PRINT_EXTS(
Gilles Peskine449bd832023-01-11 14:50:10 +01002523 3, MBEDTLS_SSL_HS_ENCRYPTED_EXTENSIONS, ssl->handshake->sent_extensions);
Jerry Yu4b8f2f72022-10-31 13:31:22 +08002524
Gilles Peskine449bd832023-01-11 14:50:10 +01002525 return 0;
Jerry Yu4d3841a2022-04-16 12:37:19 +08002526}
2527
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002528MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002529static int ssl_tls13_write_encrypted_extensions(mbedtls_ssl_context *ssl)
Jerry Yu4d3841a2022-04-16 12:37:19 +08002530{
2531 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
2532 unsigned char *buf;
Jerry Yu39730a72022-05-03 12:14:04 +08002533 size_t buf_len, msg_len;
Jerry Yu4d3841a2022-04-16 12:37:19 +08002534
Gilles Peskine449bd832023-01-11 14:50:10 +01002535 mbedtls_ssl_set_outbound_transform(ssl,
2536 ssl->handshake->transform_handshake);
Gabor Mezei54719122022-06-28 11:34:56 +02002537 MBEDTLS_SSL_DEBUG_MSG(
Gilles Peskine449bd832023-01-11 14:50:10 +01002538 3, ("switching to handshake transform for outbound data"));
Gabor Mezei54719122022-06-28 11:34:56 +02002539
Gilles Peskine449bd832023-01-11 14:50:10 +01002540 MBEDTLS_SSL_DEBUG_MSG(2, ("=> write encrypted extensions"));
Jerry Yu4d3841a2022-04-16 12:37:19 +08002541
Xiaokang Qian9f1747b2023-03-30 02:50:04 +00002542 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_start_handshake_msg(
2543 ssl, MBEDTLS_SSL_HS_ENCRYPTED_EXTENSIONS,
2544 &buf, &buf_len));
Jerry Yu4d3841a2022-04-16 12:37:19 +08002545
Gilles Peskine449bd832023-01-11 14:50:10 +01002546 MBEDTLS_SSL_PROC_CHK(ssl_tls13_write_encrypted_extensions_body(
2547 ssl, buf, buf + buf_len, &msg_len));
Jerry Yu4d3841a2022-04-16 12:37:19 +08002548
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +01002549 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_add_hs_msg_to_checksum(
Xiaokang Qian9f1747b2023-03-30 02:50:04 +00002550 ssl, MBEDTLS_SSL_HS_ENCRYPTED_EXTENSIONS,
2551 buf, msg_len));
Jerry Yu4d3841a2022-04-16 12:37:19 +08002552
Gilles Peskine449bd832023-01-11 14:50:10 +01002553 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_finish_handshake_msg(
2554 ssl, buf_len, msg_len));
Jerry Yu4d3841a2022-04-16 12:37:19 +08002555
Ronald Cron928cbd32022-10-04 16:14:26 +02002556#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01002557 if (mbedtls_ssl_tls13_key_exchange_mode_with_psk(ssl)) {
2558 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_SERVER_FINISHED);
2559 } else {
2560 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_CERTIFICATE_REQUEST);
2561 }
Jerry Yu8937eb42022-05-03 12:12:14 +08002562#else
Gilles Peskine449bd832023-01-11 14:50:10 +01002563 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_SERVER_FINISHED);
Jerry Yu8937eb42022-05-03 12:12:14 +08002564#endif
2565
Jerry Yu4d3841a2022-04-16 12:37:19 +08002566cleanup:
2567
Gilles Peskine449bd832023-01-11 14:50:10 +01002568 MBEDTLS_SSL_DEBUG_MSG(2, ("<= write encrypted extensions"));
2569 return ret;
Jerry Yu4d3841a2022-04-16 12:37:19 +08002570}
2571
Ronald Cron928cbd32022-10-04 16:14:26 +02002572#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED)
XiaokangQiana987e1d2022-05-07 01:25:58 +00002573#define SSL_CERTIFICATE_REQUEST_SEND_REQUEST 0
2574#define SSL_CERTIFICATE_REQUEST_SKIP 1
2575/* Coordination:
2576 * Check whether a CertificateRequest message should be written.
2577 * Returns a negative code on failure, or
2578 * - SSL_CERTIFICATE_REQUEST_SEND_REQUEST
2579 * - SSL_CERTIFICATE_REQUEST_SKIP
2580 * indicating if the writing of the CertificateRequest
2581 * should be skipped or not.
2582 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002583MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002584static int ssl_tls13_certificate_request_coordinate(mbedtls_ssl_context *ssl)
XiaokangQiana987e1d2022-05-07 01:25:58 +00002585{
2586 int authmode;
2587
XiaokangQian40a35232022-05-07 09:02:40 +00002588#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Gilles Peskine449bd832023-01-11 14:50:10 +01002589 if (ssl->handshake->sni_authmode != MBEDTLS_SSL_VERIFY_UNSET) {
XiaokangQian40a35232022-05-07 09:02:40 +00002590 authmode = ssl->handshake->sni_authmode;
Gilles Peskine449bd832023-01-11 14:50:10 +01002591 } else
XiaokangQian40a35232022-05-07 09:02:40 +00002592#endif
XiaokangQiana987e1d2022-05-07 01:25:58 +00002593 authmode = ssl->conf->authmode;
2594
Gilles Peskine449bd832023-01-11 14:50:10 +01002595 if (authmode == MBEDTLS_SSL_VERIFY_NONE) {
Ronald Croneac00ad2022-09-13 10:16:31 +02002596 ssl->session_negotiate->verify_result = MBEDTLS_X509_BADCERT_SKIP_VERIFY;
Gilles Peskine449bd832023-01-11 14:50:10 +01002597 return SSL_CERTIFICATE_REQUEST_SKIP;
Ronald Croneac00ad2022-09-13 10:16:31 +02002598 }
XiaokangQiana987e1d2022-05-07 01:25:58 +00002599
XiaokangQianc3017f62022-05-13 05:55:41 +00002600 ssl->handshake->certificate_request_sent = 1;
XiaokangQian189ded22022-05-10 08:12:17 +00002601
Gilles Peskine449bd832023-01-11 14:50:10 +01002602 return SSL_CERTIFICATE_REQUEST_SEND_REQUEST;
XiaokangQiana987e1d2022-05-07 01:25:58 +00002603}
2604
XiaokangQiancec9ae62022-05-06 07:28:50 +00002605/*
2606 * struct {
2607 * opaque certificate_request_context<0..2^8-1>;
2608 * Extension extensions<2..2^16-1>;
2609 * } CertificateRequest;
2610 *
2611 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002612MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002613static int ssl_tls13_write_certificate_request_body(mbedtls_ssl_context *ssl,
2614 unsigned char *buf,
2615 const unsigned char *end,
2616 size_t *out_len)
XiaokangQiancec9ae62022-05-06 07:28:50 +00002617{
2618 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
2619 unsigned char *p = buf;
XiaokangQianec6efb92022-05-06 09:53:10 +00002620 size_t output_len = 0;
XiaokangQiancec9ae62022-05-06 07:28:50 +00002621 unsigned char *p_extensions_len;
2622
2623 *out_len = 0;
2624
2625 /* Check if we have enough space:
2626 * - certificate_request_context (1 byte)
2627 * - extensions length (2 bytes)
2628 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002629 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 3);
XiaokangQiancec9ae62022-05-06 07:28:50 +00002630
2631 /*
2632 * Write certificate_request_context
2633 */
2634 /*
2635 * We use a zero length context for the normal handshake
2636 * messages. For post-authentication handshake messages
2637 * this request context would be set to a non-zero value.
2638 */
2639 *p++ = 0x0;
2640
2641 /*
2642 * Write extensions
2643 */
2644 /* The extensions must contain the signature_algorithms. */
2645 p_extensions_len = p;
2646 p += 2;
Gilles Peskine449bd832023-01-11 14:50:10 +01002647 ret = mbedtls_ssl_write_sig_alg_ext(ssl, p, end, &output_len);
2648 if (ret != 0) {
2649 return ret;
2650 }
XiaokangQiancec9ae62022-05-06 07:28:50 +00002651
XiaokangQianec6efb92022-05-06 09:53:10 +00002652 p += output_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01002653 MBEDTLS_PUT_UINT16_BE(p - p_extensions_len - 2, p_extensions_len, 0);
XiaokangQiancec9ae62022-05-06 07:28:50 +00002654
2655 *out_len = p - buf;
2656
Jerry Yu7de2ff02022-11-08 21:43:46 +08002657 MBEDTLS_SSL_PRINT_EXTS(
Gilles Peskine449bd832023-01-11 14:50:10 +01002658 3, MBEDTLS_SSL_HS_CERTIFICATE_REQUEST, ssl->handshake->sent_extensions);
Jerry Yu4b8f2f72022-10-31 13:31:22 +08002659
Gilles Peskine449bd832023-01-11 14:50:10 +01002660 return 0;
XiaokangQiancec9ae62022-05-06 07:28:50 +00002661}
2662
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002663MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002664static int ssl_tls13_write_certificate_request(mbedtls_ssl_context *ssl)
XiaokangQiancec9ae62022-05-06 07:28:50 +00002665{
2666 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
2667
Gilles Peskine449bd832023-01-11 14:50:10 +01002668 MBEDTLS_SSL_DEBUG_MSG(2, ("=> write certificate request"));
XiaokangQiancec9ae62022-05-06 07:28:50 +00002669
Gilles Peskine449bd832023-01-11 14:50:10 +01002670 MBEDTLS_SSL_PROC_CHK_NEG(ssl_tls13_certificate_request_coordinate(ssl));
XiaokangQiancec9ae62022-05-06 07:28:50 +00002671
Gilles Peskine449bd832023-01-11 14:50:10 +01002672 if (ret == SSL_CERTIFICATE_REQUEST_SEND_REQUEST) {
XiaokangQiancec9ae62022-05-06 07:28:50 +00002673 unsigned char *buf;
2674 size_t buf_len, msg_len;
2675
Xiaokang Qian9f1747b2023-03-30 02:50:04 +00002676 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_start_handshake_msg(
2677 ssl, MBEDTLS_SSL_HS_CERTIFICATE_REQUEST,
2678 &buf, &buf_len));
XiaokangQiancec9ae62022-05-06 07:28:50 +00002679
Gilles Peskine449bd832023-01-11 14:50:10 +01002680 MBEDTLS_SSL_PROC_CHK(ssl_tls13_write_certificate_request_body(
2681 ssl, buf, buf + buf_len, &msg_len));
XiaokangQiancec9ae62022-05-06 07:28:50 +00002682
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +01002683 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_add_hs_msg_to_checksum(
Xiaokang Qian9f1747b2023-03-30 02:50:04 +00002684 ssl, MBEDTLS_SSL_HS_CERTIFICATE_REQUEST,
2685 buf, msg_len));
XiaokangQiancec9ae62022-05-06 07:28:50 +00002686
Gilles Peskine449bd832023-01-11 14:50:10 +01002687 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_finish_handshake_msg(
2688 ssl, buf_len, msg_len));
2689 } else if (ret == SSL_CERTIFICATE_REQUEST_SKIP) {
2690 MBEDTLS_SSL_DEBUG_MSG(2, ("<= skip write certificate request"));
XiaokangQiancec9ae62022-05-06 07:28:50 +00002691 ret = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01002692 } else {
2693 MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
XiaokangQiancec9ae62022-05-06 07:28:50 +00002694 ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
2695 goto cleanup;
2696 }
2697
Gilles Peskine449bd832023-01-11 14:50:10 +01002698 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_SERVER_CERTIFICATE);
XiaokangQiancec9ae62022-05-06 07:28:50 +00002699cleanup:
2700
Gilles Peskine449bd832023-01-11 14:50:10 +01002701 MBEDTLS_SSL_DEBUG_MSG(2, ("<= write certificate request"));
2702 return ret;
XiaokangQiancec9ae62022-05-06 07:28:50 +00002703}
XiaokangQiancec9ae62022-05-06 07:28:50 +00002704
Jerry Yu4d3841a2022-04-16 12:37:19 +08002705/*
Jerry Yuc6e6dbf2022-04-16 19:42:57 +08002706 * Handler for MBEDTLS_SSL_SERVER_CERTIFICATE
Jerry Yu83da34e2022-04-16 13:59:52 +08002707 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002708MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002709static int ssl_tls13_write_server_certificate(mbedtls_ssl_context *ssl)
Jerry Yu83da34e2022-04-16 13:59:52 +08002710{
Jerry Yu5a26f302022-05-10 20:46:40 +08002711 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
XiaokangQianfb665a82022-06-15 03:57:21 +00002712
2713#if defined(MBEDTLS_X509_CRT_PARSE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01002714 if ((ssl_tls13_pick_key_cert(ssl) != 0) ||
2715 mbedtls_ssl_own_cert(ssl) == NULL) {
2716 MBEDTLS_SSL_DEBUG_MSG(2, ("No certificate available."));
2717 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE,
2718 MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE);
2719 return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
Jerry Yu5a26f302022-05-10 20:46:40 +08002720 }
XiaokangQianfb665a82022-06-15 03:57:21 +00002721#endif /* MBEDTLS_X509_CRT_PARSE_C */
Jerry Yu5a26f302022-05-10 20:46:40 +08002722
Gilles Peskine449bd832023-01-11 14:50:10 +01002723 ret = mbedtls_ssl_tls13_write_certificate(ssl);
2724 if (ret != 0) {
2725 return ret;
2726 }
2727 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_CERTIFICATE_VERIFY);
2728 return 0;
Jerry Yu83da34e2022-04-16 13:59:52 +08002729}
2730
2731/*
Jerry Yuc6e6dbf2022-04-16 19:42:57 +08002732 * Handler for MBEDTLS_SSL_CERTIFICATE_VERIFY
Jerry Yu83da34e2022-04-16 13:59:52 +08002733 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002734MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002735static int ssl_tls13_write_certificate_verify(mbedtls_ssl_context *ssl)
Jerry Yu83da34e2022-04-16 13:59:52 +08002736{
Gilles Peskine449bd832023-01-11 14:50:10 +01002737 int ret = mbedtls_ssl_tls13_write_certificate_verify(ssl);
2738 if (ret != 0) {
2739 return ret;
2740 }
2741 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_SERVER_FINISHED);
2742 return 0;
Jerry Yu83da34e2022-04-16 13:59:52 +08002743}
Ronald Cron928cbd32022-10-04 16:14:26 +02002744#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED */
Jerry Yu83da34e2022-04-16 13:59:52 +08002745
2746/*
Jerry Yud6e253d2022-05-18 13:59:24 +08002747 * Handler for MBEDTLS_SSL_SERVER_FINISHED
Jerry Yu69dd8d42022-04-16 12:51:26 +08002748 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002749MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002750static int ssl_tls13_write_server_finished(mbedtls_ssl_context *ssl)
Jerry Yu69dd8d42022-04-16 12:51:26 +08002751{
Jerry Yud6e253d2022-05-18 13:59:24 +08002752 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yu27bdc7c2022-04-16 13:33:27 +08002753
Gilles Peskine449bd832023-01-11 14:50:10 +01002754 ret = mbedtls_ssl_tls13_write_finished_message(ssl);
2755 if (ret != 0) {
2756 return ret;
2757 }
Jerry Yu27bdc7c2022-04-16 13:33:27 +08002758
Gilles Peskine449bd832023-01-11 14:50:10 +01002759 ret = mbedtls_ssl_tls13_compute_application_transform(ssl);
2760 if (ret != 0) {
Jerry Yue3d67cb2022-05-19 15:33:10 +08002761 MBEDTLS_SSL_PEND_FATAL_ALERT(
Gilles Peskine449bd832023-01-11 14:50:10 +01002762 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE,
2763 MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE);
2764 return ret;
Jerry Yue3d67cb2022-05-19 15:33:10 +08002765 }
XiaokangQianc3017f62022-05-13 05:55:41 +00002766
Gilles Peskine449bd832023-01-11 14:50:10 +01002767 MBEDTLS_SSL_DEBUG_MSG(1, ("Switch to handshake keys for inbound traffic"));
2768 mbedtls_ssl_set_inbound_transform(ssl, ssl->handshake->transform_handshake);
XiaokangQian189ded22022-05-10 08:12:17 +00002769
Gilles Peskine449bd832023-01-11 14:50:10 +01002770 if (ssl->handshake->certificate_request_sent) {
2771 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_CLIENT_CERTIFICATE);
2772 } else {
2773 MBEDTLS_SSL_DEBUG_MSG(2, ("skip parse certificate"));
2774 MBEDTLS_SSL_DEBUG_MSG(2, ("skip parse certificate verify"));
2775 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_CLIENT_FINISHED);
Ronald Cron19385882022-06-15 16:26:13 +02002776 }
Ronald Cron63dc4632022-05-31 14:41:53 +02002777
Gilles Peskine449bd832023-01-11 14:50:10 +01002778 return 0;
Jerry Yu69dd8d42022-04-16 12:51:26 +08002779}
2780
2781/*
Jerry Yud6e253d2022-05-18 13:59:24 +08002782 * Handler for MBEDTLS_SSL_CLIENT_FINISHED
Jerry Yu69dd8d42022-04-16 12:51:26 +08002783 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002784MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002785static int ssl_tls13_process_client_finished(mbedtls_ssl_context *ssl)
Jerry Yu69dd8d42022-04-16 12:51:26 +08002786{
Jerry Yud6e253d2022-05-18 13:59:24 +08002787 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
2788
Gilles Peskine449bd832023-01-11 14:50:10 +01002789 ret = mbedtls_ssl_tls13_process_finished_message(ssl);
2790 if (ret != 0) {
2791 return ret;
Jerry Yue3d67cb2022-05-19 15:33:10 +08002792 }
2793
Gilles Peskine449bd832023-01-11 14:50:10 +01002794 ret = mbedtls_ssl_tls13_compute_resumption_master_secret(ssl);
2795 if (ret != 0) {
Xiaokang Qian9f1747b2023-03-30 02:50:04 +00002796 MBEDTLS_SSL_DEBUG_RET(
2797 1, "mbedtls_ssl_tls13_compute_resumption_master_secret", ret);
Gilles Peskine449bd832023-01-11 14:50:10 +01002798 }
2799
2800 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_HANDSHAKE_WRAPUP);
2801 return 0;
Jerry Yu69dd8d42022-04-16 12:51:26 +08002802}
2803
2804/*
Jerry Yud6e253d2022-05-18 13:59:24 +08002805 * Handler for MBEDTLS_SSL_HANDSHAKE_WRAPUP
Jerry Yu69dd8d42022-04-16 12:51:26 +08002806 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002807MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002808static int ssl_tls13_handshake_wrapup(mbedtls_ssl_context *ssl)
Jerry Yu69dd8d42022-04-16 12:51:26 +08002809{
Gilles Peskine449bd832023-01-11 14:50:10 +01002810 MBEDTLS_SSL_DEBUG_MSG(2, ("handshake: done"));
Jerry Yu03ed50b2022-04-16 17:13:30 +08002811
Gilles Peskine449bd832023-01-11 14:50:10 +01002812 mbedtls_ssl_tls13_handshake_wrapup(ssl);
Jerry Yue67bef42022-07-07 07:29:42 +00002813
Pengyu Lv3643fdb2023-01-12 16:46:28 +08002814#if defined(MBEDTLS_SSL_SESSION_TICKETS) && \
2815 defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED)
Pengyu Lva1aa31b2022-12-13 13:49:59 +08002816/* TODO: Remove the check of SOME_PSK_ENABLED since SESSION_TICKETS requires
2817 * SOME_PSK_ENABLED to be enabled. Here is just to make CI happy. It is
2818 * expected to be resolved with issue#6395.
2819 */
Pengyu Lvc7af2c42022-12-01 16:33:00 +08002820 /* Sent NewSessionTicket message only when client supports PSK */
Pengyu Lv3643fdb2023-01-12 16:46:28 +08002821 if (mbedtls_ssl_tls13_some_psk_enabled(ssl)) {
Xiaokang Qian9f1747b2023-03-30 02:50:04 +00002822 mbedtls_ssl_handshake_set_state(
2823 ssl, MBEDTLS_SSL_TLS1_3_NEW_SESSION_TICKET);
Pengyu Lvc7af2c42022-12-01 16:33:00 +08002824 } else
Jerry Yufca4d572022-07-21 10:37:48 +08002825#endif
Pengyu Lv3643fdb2023-01-12 16:46:28 +08002826 {
2827 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_HANDSHAKE_OVER);
2828 }
Gilles Peskine449bd832023-01-11 14:50:10 +01002829 return 0;
Jerry Yu69dd8d42022-04-16 12:51:26 +08002830}
2831
2832/*
Jerry Yua8d3c502022-10-30 14:51:23 +08002833 * Handler for MBEDTLS_SSL_TLS1_3_NEW_SESSION_TICKET
Jerry Yue67bef42022-07-07 07:29:42 +00002834 */
2835#define SSL_NEW_SESSION_TICKET_SKIP 0
2836#define SSL_NEW_SESSION_TICKET_WRITE 1
2837MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002838static int ssl_tls13_write_new_session_ticket_coordinate(mbedtls_ssl_context *ssl)
Jerry Yue67bef42022-07-07 07:29:42 +00002839{
2840 /* Check whether the use of session tickets is enabled */
Gilles Peskine449bd832023-01-11 14:50:10 +01002841 if (ssl->conf->f_ticket_write == NULL) {
2842 MBEDTLS_SSL_DEBUG_MSG(2, ("NewSessionTicket: disabled,"
2843 " callback is not set"));
2844 return SSL_NEW_SESSION_TICKET_SKIP;
Jerry Yud0766ec2022-09-22 10:46:57 +08002845 }
Gilles Peskine449bd832023-01-11 14:50:10 +01002846 if (ssl->conf->new_session_tickets_count == 0) {
2847 MBEDTLS_SSL_DEBUG_MSG(2, ("NewSessionTicket: disabled,"
2848 " configured count is zero"));
2849 return SSL_NEW_SESSION_TICKET_SKIP;
Jerry Yud0766ec2022-09-22 10:46:57 +08002850 }
2851
Gilles Peskine449bd832023-01-11 14:50:10 +01002852 if (ssl->handshake->new_session_tickets_count == 0) {
2853 MBEDTLS_SSL_DEBUG_MSG(2, ("NewSessionTicket: all tickets have "
2854 "been sent."));
2855 return SSL_NEW_SESSION_TICKET_SKIP;
Jerry Yue67bef42022-07-07 07:29:42 +00002856 }
2857
Gilles Peskine449bd832023-01-11 14:50:10 +01002858 return SSL_NEW_SESSION_TICKET_WRITE;
Jerry Yue67bef42022-07-07 07:29:42 +00002859}
2860
2861#if defined(MBEDTLS_SSL_SESSION_TICKETS)
2862MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002863static int ssl_tls13_prepare_new_session_ticket(mbedtls_ssl_context *ssl,
2864 unsigned char *ticket_nonce,
2865 size_t ticket_nonce_size)
Jerry Yue67bef42022-07-07 07:29:42 +00002866{
2867 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
2868 mbedtls_ssl_session *session = ssl->session;
2869 mbedtls_ssl_ciphersuite_t *ciphersuite_info;
2870 psa_algorithm_t psa_hash_alg;
2871 int hash_length;
2872
Gilles Peskine449bd832023-01-11 14:50:10 +01002873 MBEDTLS_SSL_DEBUG_MSG(2, ("=> prepare NewSessionTicket msg"));
Jerry Yue67bef42022-07-07 07:29:42 +00002874
2875#if defined(MBEDTLS_HAVE_TIME)
Gilles Peskine449bd832023-01-11 14:50:10 +01002876 session->start = mbedtls_time(NULL);
Jerry Yue67bef42022-07-07 07:29:42 +00002877#endif
2878
Pengyu Lv9f926952022-11-17 15:22:33 +08002879 /* Set ticket_flags depends on the advertised psk key exchange mode */
Pengyu Lv80270b22023-01-12 11:54:04 +08002880 mbedtls_ssl_session_clear_ticket_flags(
Pengyu Lv9eacb442022-12-09 14:39:19 +08002881 session, MBEDTLS_SSL_TLS1_3_TICKET_FLAGS_MASK);
Pengyu Lve6487fe2022-12-06 09:30:29 +08002882#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED)
Pengyu Lv80270b22023-01-12 11:54:04 +08002883 mbedtls_ssl_session_set_ticket_flags(
Pengyu Lv9eacb442022-12-09 14:39:19 +08002884 session, ssl->handshake->tls13_kex_modes);
Pengyu Lve6487fe2022-12-06 09:30:29 +08002885#endif
Pengyu Lv4938a562023-01-16 11:28:49 +08002886 MBEDTLS_SSL_PRINT_TICKET_FLAGS(4, session->ticket_flags);
Pengyu Lv9f926952022-11-17 15:22:33 +08002887
Jerry Yue67bef42022-07-07 07:29:42 +00002888 /* Generate ticket_age_add */
Gilles Peskine449bd832023-01-11 14:50:10 +01002889 if ((ret = ssl->conf->f_rng(ssl->conf->p_rng,
2890 (unsigned char *) &session->ticket_age_add,
2891 sizeof(session->ticket_age_add)) != 0)) {
2892 MBEDTLS_SSL_DEBUG_RET(1, "generate_ticket_age_add", ret);
2893 return ret;
Jerry Yue67bef42022-07-07 07:29:42 +00002894 }
Gilles Peskine449bd832023-01-11 14:50:10 +01002895 MBEDTLS_SSL_DEBUG_MSG(3, ("ticket_age_add: %u",
2896 (unsigned int) session->ticket_age_add));
Jerry Yue67bef42022-07-07 07:29:42 +00002897
2898 /* Generate ticket_nonce */
Gilles Peskine449bd832023-01-11 14:50:10 +01002899 ret = ssl->conf->f_rng(ssl->conf->p_rng, ticket_nonce, ticket_nonce_size);
2900 if (ret != 0) {
2901 MBEDTLS_SSL_DEBUG_RET(1, "generate_ticket_nonce", ret);
2902 return ret;
Jerry Yue67bef42022-07-07 07:29:42 +00002903 }
Gilles Peskine449bd832023-01-11 14:50:10 +01002904 MBEDTLS_SSL_DEBUG_BUF(3, "ticket_nonce:",
2905 ticket_nonce, ticket_nonce_size);
Jerry Yue67bef42022-07-07 07:29:42 +00002906
2907 ciphersuite_info =
Gilles Peskine449bd832023-01-11 14:50:10 +01002908 (mbedtls_ssl_ciphersuite_t *) ssl->handshake->ciphersuite_info;
Dave Rodgman2eab4622023-10-05 13:30:37 +01002909 psa_hash_alg = mbedtls_md_psa_alg_from_type((mbedtls_md_type_t) ciphersuite_info->mac);
Gilles Peskine449bd832023-01-11 14:50:10 +01002910 hash_length = PSA_HASH_LENGTH(psa_hash_alg);
2911 if (hash_length == -1 ||
2912 (size_t) hash_length > sizeof(session->resumption_key)) {
2913 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Jerry Yufca4d572022-07-21 10:37:48 +08002914 }
2915
Jerry Yue67bef42022-07-07 07:29:42 +00002916 /* In this code the psk key length equals the length of the hash */
2917 session->resumption_key_len = hash_length;
2918 session->ciphersuite = ciphersuite_info->id;
2919
2920 /* Compute resumption key
2921 *
2922 * HKDF-Expand-Label( resumption_master_secret,
2923 * "resumption", ticket_nonce, Hash.length )
2924 */
2925 ret = mbedtls_ssl_tls13_hkdf_expand_label(
Gilles Peskine449bd832023-01-11 14:50:10 +01002926 psa_hash_alg,
2927 session->app_secrets.resumption_master_secret,
2928 hash_length,
2929 MBEDTLS_SSL_TLS1_3_LBL_WITH_LEN(resumption),
2930 ticket_nonce,
2931 ticket_nonce_size,
2932 session->resumption_key,
2933 hash_length);
Jerry Yue67bef42022-07-07 07:29:42 +00002934
Gilles Peskine449bd832023-01-11 14:50:10 +01002935 if (ret != 0) {
2936 MBEDTLS_SSL_DEBUG_RET(2,
2937 "Creating the ticket-resumed PSK failed",
2938 ret);
2939 return ret;
Jerry Yue67bef42022-07-07 07:29:42 +00002940 }
Gilles Peskine449bd832023-01-11 14:50:10 +01002941 MBEDTLS_SSL_DEBUG_BUF(3, "Ticket-resumed PSK",
2942 session->resumption_key,
2943 session->resumption_key_len);
Jerry Yue67bef42022-07-07 07:29:42 +00002944
Gilles Peskine449bd832023-01-11 14:50:10 +01002945 MBEDTLS_SSL_DEBUG_BUF(3, "resumption_master_secret",
2946 session->app_secrets.resumption_master_secret,
2947 hash_length);
Jerry Yue67bef42022-07-07 07:29:42 +00002948
Gilles Peskine449bd832023-01-11 14:50:10 +01002949 return 0;
Jerry Yue67bef42022-07-07 07:29:42 +00002950}
2951
2952/* This function creates a NewSessionTicket message in the following format:
2953 *
2954 * struct {
2955 * uint32 ticket_lifetime;
2956 * uint32 ticket_age_add;
2957 * opaque ticket_nonce<0..255>;
2958 * opaque ticket<1..2^16-1>;
2959 * Extension extensions<0..2^16-2>;
2960 * } NewSessionTicket;
2961 *
2962 * The ticket inside the NewSessionTicket message is an encrypted container
2963 * carrying the necessary information so that the server is later able to
2964 * re-start the communication.
2965 *
2966 * The following fields are placed inside the ticket by the
2967 * f_ticket_write() function:
2968 *
2969 * - creation time (start)
2970 * - flags (flags)
2971 * - age add (ticket_age_add)
2972 * - key (key)
2973 * - key length (key_len)
2974 * - ciphersuite (ciphersuite)
2975 */
2976MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002977static int ssl_tls13_write_new_session_ticket_body(mbedtls_ssl_context *ssl,
2978 unsigned char *buf,
2979 unsigned char *end,
2980 size_t *out_len,
2981 unsigned char *ticket_nonce,
2982 size_t ticket_nonce_size)
Jerry Yue67bef42022-07-07 07:29:42 +00002983{
2984 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
2985 unsigned char *p = buf;
2986 mbedtls_ssl_session *session = ssl->session;
2987 size_t ticket_len;
2988 uint32_t ticket_lifetime;
2989
2990 *out_len = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01002991 MBEDTLS_SSL_DEBUG_MSG(2, ("=> write NewSessionTicket msg"));
Jerry Yue67bef42022-07-07 07:29:42 +00002992
2993 /*
2994 * ticket_lifetime 4 bytes
2995 * ticket_age_add 4 bytes
2996 * ticket_nonce 1 + ticket_nonce_size bytes
2997 * ticket >=2 bytes
2998 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002999 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 4 + 4 + 1 + ticket_nonce_size + 2);
Jerry Yue67bef42022-07-07 07:29:42 +00003000
3001 /* Generate ticket and ticket_lifetime */
Gilles Peskine449bd832023-01-11 14:50:10 +01003002 ret = ssl->conf->f_ticket_write(ssl->conf->p_ticket,
3003 session,
3004 p + 9 + ticket_nonce_size + 2,
3005 end,
3006 &ticket_len,
3007 &ticket_lifetime);
3008 if (ret != 0) {
3009 MBEDTLS_SSL_DEBUG_RET(1, "write_ticket", ret);
3010 return ret;
Jerry Yue67bef42022-07-07 07:29:42 +00003011 }
3012 /* RFC 8446 4.6.1
3013 * ticket_lifetime: Indicates the lifetime in seconds as a 32-bit
3014 * unsigned integer in network byte order from the time of ticket
3015 * issuance. Servers MUST NOT use any value greater than
3016 * 604800 seconds (7 days). The value of zero indicates that the
3017 * ticket should be discarded immediately. Clients MUST NOT cache
3018 * tickets for longer than 7 days, regardless of the ticket_lifetime,
3019 * and MAY delete tickets earlier based on local policy. A server
3020 * MAY treat a ticket as valid for a shorter period of time than what
3021 * is stated in the ticket_lifetime.
3022 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003023 if (ticket_lifetime > 604800) {
Xiaokang Qian28af5012022-10-13 08:18:19 +00003024 ticket_lifetime = 604800;
Gilles Peskine449bd832023-01-11 14:50:10 +01003025 }
3026 MBEDTLS_PUT_UINT32_BE(ticket_lifetime, p, 0);
3027 MBEDTLS_SSL_DEBUG_MSG(3, ("ticket_lifetime: %u",
3028 (unsigned int) ticket_lifetime));
Jerry Yue67bef42022-07-07 07:29:42 +00003029
3030 /* Write ticket_age_add */
Gilles Peskine449bd832023-01-11 14:50:10 +01003031 MBEDTLS_PUT_UINT32_BE(session->ticket_age_add, p, 4);
3032 MBEDTLS_SSL_DEBUG_MSG(3, ("ticket_age_add: %u",
3033 (unsigned int) session->ticket_age_add));
Jerry Yue67bef42022-07-07 07:29:42 +00003034
3035 /* Write ticket_nonce */
Gilles Peskine449bd832023-01-11 14:50:10 +01003036 p[8] = (unsigned char) ticket_nonce_size;
3037 if (ticket_nonce_size > 0) {
3038 memcpy(p + 9, ticket_nonce, ticket_nonce_size);
Jerry Yue67bef42022-07-07 07:29:42 +00003039 }
3040 p += 9 + ticket_nonce_size;
3041
3042 /* Write ticket */
Gilles Peskine449bd832023-01-11 14:50:10 +01003043 MBEDTLS_PUT_UINT16_BE(ticket_len, p, 0);
Jerry Yue67bef42022-07-07 07:29:42 +00003044 p += 2;
Gilles Peskine449bd832023-01-11 14:50:10 +01003045 MBEDTLS_SSL_DEBUG_BUF(4, "ticket", p, ticket_len);
Jerry Yue67bef42022-07-07 07:29:42 +00003046 p += ticket_len;
3047
3048 /* Ticket Extensions
3049 *
3050 * Note: We currently don't have any extensions.
3051 * Set length to zero.
3052 */
Jerry Yuedab6372022-10-31 14:37:31 +08003053 ssl->handshake->sent_extensions = MBEDTLS_SSL_EXT_MASK_NONE;
3054
Gilles Peskine449bd832023-01-11 14:50:10 +01003055 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 2);
3056 MBEDTLS_PUT_UINT16_BE(0, p, 0);
Jerry Yue67bef42022-07-07 07:29:42 +00003057 p += 2;
3058
3059 *out_len = p - buf;
Gilles Peskine449bd832023-01-11 14:50:10 +01003060 MBEDTLS_SSL_DEBUG_BUF(4, "ticket", buf, *out_len);
3061 MBEDTLS_SSL_DEBUG_MSG(2, ("<= write new session ticket"));
Jerry Yue67bef42022-07-07 07:29:42 +00003062
Jerry Yu7de2ff02022-11-08 21:43:46 +08003063 MBEDTLS_SSL_PRINT_EXTS(
Gilles Peskine449bd832023-01-11 14:50:10 +01003064 3, MBEDTLS_SSL_HS_NEW_SESSION_TICKET, ssl->handshake->sent_extensions);
Jerry Yu4b8f2f72022-10-31 13:31:22 +08003065
Gilles Peskine449bd832023-01-11 14:50:10 +01003066 return 0;
Jerry Yue67bef42022-07-07 07:29:42 +00003067}
3068
3069/*
Jerry Yua8d3c502022-10-30 14:51:23 +08003070 * Handler for MBEDTLS_SSL_TLS1_3_NEW_SESSION_TICKET
Jerry Yue67bef42022-07-07 07:29:42 +00003071 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003072static int ssl_tls13_write_new_session_ticket(mbedtls_ssl_context *ssl)
Jerry Yue67bef42022-07-07 07:29:42 +00003073{
3074 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
3075
Gilles Peskine449bd832023-01-11 14:50:10 +01003076 MBEDTLS_SSL_PROC_CHK_NEG(ssl_tls13_write_new_session_ticket_coordinate(ssl));
Jerry Yue67bef42022-07-07 07:29:42 +00003077
Gilles Peskine449bd832023-01-11 14:50:10 +01003078 if (ret == SSL_NEW_SESSION_TICKET_WRITE) {
Jerry Yue67bef42022-07-07 07:29:42 +00003079 unsigned char ticket_nonce[MBEDTLS_SSL_TLS1_3_TICKET_NONCE_LENGTH];
3080 unsigned char *buf;
3081 size_t buf_len, msg_len;
3082
Gilles Peskine449bd832023-01-11 14:50:10 +01003083 MBEDTLS_SSL_PROC_CHK(ssl_tls13_prepare_new_session_ticket(
3084 ssl, ticket_nonce, sizeof(ticket_nonce)));
Jerry Yue67bef42022-07-07 07:29:42 +00003085
Xiaokang Qian9f1747b2023-03-30 02:50:04 +00003086 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_start_handshake_msg(
3087 ssl, MBEDTLS_SSL_HS_NEW_SESSION_TICKET,
3088 &buf, &buf_len));
Jerry Yue67bef42022-07-07 07:29:42 +00003089
Gilles Peskine449bd832023-01-11 14:50:10 +01003090 MBEDTLS_SSL_PROC_CHK(ssl_tls13_write_new_session_ticket_body(
3091 ssl, buf, buf + buf_len, &msg_len,
3092 ticket_nonce, sizeof(ticket_nonce)));
Jerry Yue67bef42022-07-07 07:29:42 +00003093
Gilles Peskine449bd832023-01-11 14:50:10 +01003094 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_finish_handshake_msg(
3095 ssl, buf_len, msg_len));
Jerry Yufca4d572022-07-21 10:37:48 +08003096
Jerry Yu359e65f2022-09-22 23:47:43 +08003097 /* Limit session tickets count to one when resumption connection.
3098 *
3099 * See document of mbedtls_ssl_conf_new_session_tickets.
3100 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003101 if (ssl->handshake->resume == 1) {
Jerry Yu359e65f2022-09-22 23:47:43 +08003102 ssl->handshake->new_session_tickets_count = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01003103 } else {
Jerry Yu359e65f2022-09-22 23:47:43 +08003104 ssl->handshake->new_session_tickets_count--;
Gilles Peskine449bd832023-01-11 14:50:10 +01003105 }
Jerry Yub7e3fa72022-09-22 11:07:18 +08003106
Jerry Yua8d3c502022-10-30 14:51:23 +08003107 mbedtls_ssl_handshake_set_state(
Gilles Peskine449bd832023-01-11 14:50:10 +01003108 ssl, MBEDTLS_SSL_TLS1_3_NEW_SESSION_TICKET_FLUSH);
3109 } else {
3110 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_HANDSHAKE_OVER);
Jerry Yue67bef42022-07-07 07:29:42 +00003111 }
3112
Jerry Yue67bef42022-07-07 07:29:42 +00003113cleanup:
3114
Gilles Peskine449bd832023-01-11 14:50:10 +01003115 return ret;
Jerry Yue67bef42022-07-07 07:29:42 +00003116}
3117#endif /* MBEDTLS_SSL_SESSION_TICKETS */
3118
3119/*
XiaokangQiane8ff3502022-04-22 02:34:40 +00003120 * TLS 1.3 State Machine -- server side
XiaokangQian7807f9f2022-02-15 10:04:37 +00003121 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003122int mbedtls_ssl_tls13_handshake_server_step(mbedtls_ssl_context *ssl)
Jerry Yub9930e72021-08-06 17:11:51 +08003123{
XiaokangQian08037552022-04-20 07:16:41 +00003124 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
XiaokangQian7807f9f2022-02-15 10:04:37 +00003125
Gilles Peskine449bd832023-01-11 14:50:10 +01003126 if (ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER || ssl->handshake == NULL) {
3127 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
3128 }
XiaokangQian7807f9f2022-02-15 10:04:37 +00003129
Gilles Peskine449bd832023-01-11 14:50:10 +01003130 MBEDTLS_SSL_DEBUG_MSG(2, ("tls13 server state: %s(%d)",
Dave Rodgman2eab4622023-10-05 13:30:37 +01003131 mbedtls_ssl_states_str((mbedtls_ssl_states) ssl->state),
Gilles Peskine449bd832023-01-11 14:50:10 +01003132 ssl->state));
Jerry Yu6e81b272021-09-27 11:16:17 +08003133
Gilles Peskine449bd832023-01-11 14:50:10 +01003134 switch (ssl->state) {
XiaokangQian7807f9f2022-02-15 10:04:37 +00003135 /* start state */
3136 case MBEDTLS_SSL_HELLO_REQUEST:
Gilles Peskine449bd832023-01-11 14:50:10 +01003137 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_CLIENT_HELLO);
XiaokangQian08037552022-04-20 07:16:41 +00003138 ret = 0;
XiaokangQian7807f9f2022-02-15 10:04:37 +00003139 break;
3140
XiaokangQian7807f9f2022-02-15 10:04:37 +00003141 case MBEDTLS_SSL_CLIENT_HELLO:
Gilles Peskine449bd832023-01-11 14:50:10 +01003142 ret = ssl_tls13_process_client_hello(ssl);
3143 if (ret != 0) {
3144 MBEDTLS_SSL_DEBUG_RET(1, "ssl_tls13_process_client_hello", ret);
3145 }
Jerry Yuf41553b2022-05-09 22:20:30 +08003146 break;
XiaokangQian7807f9f2022-02-15 10:04:37 +00003147
Jerry Yuf41553b2022-05-09 22:20:30 +08003148 case MBEDTLS_SSL_HELLO_RETRY_REQUEST:
Gilles Peskine449bd832023-01-11 14:50:10 +01003149 ret = ssl_tls13_write_hello_retry_request(ssl);
3150 if (ret != 0) {
3151 MBEDTLS_SSL_DEBUG_RET(1, "ssl_tls13_write_hello_retry_request", ret);
3152 return ret;
Jerry Yuf41553b2022-05-09 22:20:30 +08003153 }
XiaokangQian7807f9f2022-02-15 10:04:37 +00003154 break;
3155
Jerry Yu5b64ae92022-03-30 17:15:02 +08003156 case MBEDTLS_SSL_SERVER_HELLO:
Gilles Peskine449bd832023-01-11 14:50:10 +01003157 ret = ssl_tls13_write_server_hello(ssl);
Jerry Yu5b64ae92022-03-30 17:15:02 +08003158 break;
3159
Xiaofei Baicba64af2022-02-15 10:00:56 +00003160 case MBEDTLS_SSL_ENCRYPTED_EXTENSIONS:
Gilles Peskine449bd832023-01-11 14:50:10 +01003161 ret = ssl_tls13_write_encrypted_extensions(ssl);
3162 if (ret != 0) {
3163 MBEDTLS_SSL_DEBUG_RET(1, "ssl_tls13_write_encrypted_extensions", ret);
3164 return ret;
Xiaofei Baicba64af2022-02-15 10:00:56 +00003165 }
Jerry Yu48330562022-05-06 21:35:44 +08003166 break;
Jerry Yu6a2cd9e2022-05-05 11:14:19 +08003167
Ronald Cron928cbd32022-10-04 16:14:26 +02003168#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED)
Xiaofei Bai9ca09d42022-02-14 12:57:18 +00003169 case MBEDTLS_SSL_CERTIFICATE_REQUEST:
Gilles Peskine449bd832023-01-11 14:50:10 +01003170 ret = ssl_tls13_write_certificate_request(ssl);
Xiaofei Bai9ca09d42022-02-14 12:57:18 +00003171 break;
Xiaofei Bai9ca09d42022-02-14 12:57:18 +00003172
Jerry Yu83da34e2022-04-16 13:59:52 +08003173 case MBEDTLS_SSL_SERVER_CERTIFICATE:
Gilles Peskine449bd832023-01-11 14:50:10 +01003174 ret = ssl_tls13_write_server_certificate(ssl);
Jerry Yu83da34e2022-04-16 13:59:52 +08003175 break;
3176
3177 case MBEDTLS_SSL_CERTIFICATE_VERIFY:
Gilles Peskine449bd832023-01-11 14:50:10 +01003178 ret = ssl_tls13_write_certificate_verify(ssl);
Jerry Yu83da34e2022-04-16 13:59:52 +08003179 break;
Ronald Cron928cbd32022-10-04 16:14:26 +02003180#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED */
Jerry Yu83da34e2022-04-16 13:59:52 +08003181
Gilles Peskine449bd832023-01-11 14:50:10 +01003182 /*
3183 * Injection of dummy-CCS's for middlebox compatibility
3184 */
Gabor Mezei7b39bf12022-05-24 16:04:14 +02003185#if defined(MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE)
Gabor Mezeif7044ea2022-06-28 16:01:49 +02003186 case MBEDTLS_SSL_SERVER_CCS_AFTER_HELLO_RETRY_REQUEST:
Gilles Peskine449bd832023-01-11 14:50:10 +01003187 ret = mbedtls_ssl_tls13_write_change_cipher_spec(ssl);
3188 if (ret == 0) {
3189 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_CLIENT_HELLO);
3190 }
Gabor Mezei7b39bf12022-05-24 16:04:14 +02003191 break;
3192
3193 case MBEDTLS_SSL_SERVER_CCS_AFTER_SERVER_HELLO:
Gilles Peskine449bd832023-01-11 14:50:10 +01003194 ret = mbedtls_ssl_tls13_write_change_cipher_spec(ssl);
3195 if (ret == 0) {
3196 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_ENCRYPTED_EXTENSIONS);
3197 }
Gabor Mezei7b39bf12022-05-24 16:04:14 +02003198 break;
3199#endif /* MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE */
3200
Jerry Yu69dd8d42022-04-16 12:51:26 +08003201 case MBEDTLS_SSL_SERVER_FINISHED:
Gilles Peskine449bd832023-01-11 14:50:10 +01003202 ret = ssl_tls13_write_server_finished(ssl);
Jerry Yu69dd8d42022-04-16 12:51:26 +08003203 break;
3204
3205 case MBEDTLS_SSL_CLIENT_FINISHED:
Gilles Peskine449bd832023-01-11 14:50:10 +01003206 ret = ssl_tls13_process_client_finished(ssl);
Jerry Yu69dd8d42022-04-16 12:51:26 +08003207 break;
3208
Jerry Yu69dd8d42022-04-16 12:51:26 +08003209 case MBEDTLS_SSL_HANDSHAKE_WRAPUP:
Gilles Peskine449bd832023-01-11 14:50:10 +01003210 ret = ssl_tls13_handshake_wrapup(ssl);
Jerry Yu69dd8d42022-04-16 12:51:26 +08003211 break;
3212
Ronald Cron766c0cd2022-10-18 12:17:11 +02003213#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED)
XiaokangQian6b916b12022-04-25 07:29:34 +00003214 case MBEDTLS_SSL_CLIENT_CERTIFICATE:
Gilles Peskine449bd832023-01-11 14:50:10 +01003215 ret = mbedtls_ssl_tls13_process_certificate(ssl);
3216 if (ret == 0) {
3217 if (ssl->session_negotiate->peer_cert != NULL) {
Ronald Cron209cae92022-06-07 10:30:19 +02003218 mbedtls_ssl_handshake_set_state(
Gilles Peskine449bd832023-01-11 14:50:10 +01003219 ssl, MBEDTLS_SSL_CLIENT_CERTIFICATE_VERIFY);
3220 } else {
3221 MBEDTLS_SSL_DEBUG_MSG(2, ("skip parse certificate verify"));
Ronald Cron209cae92022-06-07 10:30:19 +02003222 mbedtls_ssl_handshake_set_state(
Gilles Peskine449bd832023-01-11 14:50:10 +01003223 ssl, MBEDTLS_SSL_CLIENT_FINISHED);
Ronald Cron19385882022-06-15 16:26:13 +02003224 }
XiaokangQian6b916b12022-04-25 07:29:34 +00003225 }
3226 break;
3227
3228 case MBEDTLS_SSL_CLIENT_CERTIFICATE_VERIFY:
Gilles Peskine449bd832023-01-11 14:50:10 +01003229 ret = mbedtls_ssl_tls13_process_certificate_verify(ssl);
3230 if (ret == 0) {
XiaokangQian6b916b12022-04-25 07:29:34 +00003231 mbedtls_ssl_handshake_set_state(
Gilles Peskine449bd832023-01-11 14:50:10 +01003232 ssl, MBEDTLS_SSL_CLIENT_FINISHED);
XiaokangQian6b916b12022-04-25 07:29:34 +00003233 }
3234 break;
Ronald Cron766c0cd2022-10-18 12:17:11 +02003235#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED */
XiaokangQian6b916b12022-04-25 07:29:34 +00003236
Jerry Yue67bef42022-07-07 07:29:42 +00003237#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Jerry Yua8d3c502022-10-30 14:51:23 +08003238 case MBEDTLS_SSL_TLS1_3_NEW_SESSION_TICKET:
Gilles Peskine449bd832023-01-11 14:50:10 +01003239 ret = ssl_tls13_write_new_session_ticket(ssl);
3240 if (ret != 0) {
3241 MBEDTLS_SSL_DEBUG_RET(1,
3242 "ssl_tls13_write_new_session_ticket ",
3243 ret);
Jerry Yue67bef42022-07-07 07:29:42 +00003244 }
3245 break;
Jerry Yua8d3c502022-10-30 14:51:23 +08003246 case MBEDTLS_SSL_TLS1_3_NEW_SESSION_TICKET_FLUSH:
Jerry Yue67bef42022-07-07 07:29:42 +00003247 /* This state is necessary to do the flush of the New Session
Jerry Yua8d3c502022-10-30 14:51:23 +08003248 * Ticket message written in MBEDTLS_SSL_TLS1_3_NEW_SESSION_TICKET
Jerry Yue67bef42022-07-07 07:29:42 +00003249 * as part of ssl_prepare_handshake_step.
3250 */
3251 ret = 0;
Jerry Yud4e75002022-08-09 13:33:50 +08003252
Gilles Peskine449bd832023-01-11 14:50:10 +01003253 if (ssl->handshake->new_session_tickets_count == 0) {
3254 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_HANDSHAKE_OVER);
3255 } else {
Xiaokang Qian9f1747b2023-03-30 02:50:04 +00003256 mbedtls_ssl_handshake_set_state(
3257 ssl, MBEDTLS_SSL_TLS1_3_NEW_SESSION_TICKET);
Gilles Peskine449bd832023-01-11 14:50:10 +01003258 }
Jerry Yue67bef42022-07-07 07:29:42 +00003259 break;
3260
3261#endif /* MBEDTLS_SSL_SESSION_TICKETS */
3262
XiaokangQian7807f9f2022-02-15 10:04:37 +00003263 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01003264 MBEDTLS_SSL_DEBUG_MSG(1, ("invalid state %d", ssl->state));
3265 return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
XiaokangQian7807f9f2022-02-15 10:04:37 +00003266 }
3267
Gilles Peskine449bd832023-01-11 14:50:10 +01003268 return ret;
Jerry Yub9930e72021-08-06 17:11:51 +08003269}
Jerry Yu3cc4c2a2021-08-06 16:29:08 +08003270
Jerry Yufb4b6472022-01-27 15:03:26 +08003271#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_PROTO_TLS1_3 */