blob: b418ee63579c2d55a4a57ab7ce14239930d03729 [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
Jerry Yu33bf2402022-12-12 16:01:43 +0800475#if defined(MBEDTLS_SSL_EARLY_DATA)
476 dst->max_early_data_size = src->max_early_data_size;
477#endif
478
Gilles Peskine449bd832023-01-11 14:50:10 +0100479 return 0;
Jerry Yu82534862022-08-30 10:42:33 +0800480}
481#endif /* MBEDTLS_SSL_SESSION_TICKETS */
482
Jerry Yu1c105562022-07-10 06:32:38 +0000483/* Parser for pre_shared_key extension in client hello
484 * struct {
485 * opaque identity<1..2^16-1>;
486 * uint32 obfuscated_ticket_age;
487 * } PskIdentity;
488 *
489 * opaque PskBinderEntry<32..255>;
490 *
491 * struct {
492 * PskIdentity identities<7..2^16-1>;
493 * PskBinderEntry binders<33..2^16-1>;
494 * } OfferedPsks;
495 *
496 * struct {
497 * select (Handshake.msg_type) {
498 * case client_hello: OfferedPsks;
499 * ....
500 * };
501 * } PreSharedKeyExtension;
502 */
Jerry Yu6e74a7e2022-07-20 20:49:32 +0800503MBEDTLS_CHECK_RETURN_CRITICAL
Xiaokang Qian9f1747b2023-03-30 02:50:04 +0000504static int ssl_tls13_parse_pre_shared_key_ext(
505 mbedtls_ssl_context *ssl,
506 const unsigned char *pre_shared_key_ext,
507 const unsigned char *pre_shared_key_ext_end,
508 const unsigned char *ciphersuites,
509 const unsigned char *ciphersuites_end)
Jerry Yu1c105562022-07-10 06:32:38 +0000510{
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +0100511 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yu29d9faa2022-08-23 17:52:45 +0800512 const unsigned char *identities = pre_shared_key_ext;
Jerry Yu568ec252022-07-22 21:27:34 +0800513 const unsigned char *p_identity_len;
514 size_t identities_len;
Jerry Yu1c105562022-07-10 06:32:38 +0000515 const unsigned char *identities_end;
Jerry Yu568ec252022-07-22 21:27:34 +0800516 const unsigned char *binders;
517 const unsigned char *p_binder_len;
518 size_t binders_len;
Jerry Yu1c105562022-07-10 06:32:38 +0000519 const unsigned char *binders_end;
Jerry Yu96a2e362022-07-21 15:11:34 +0800520 int matched_identity = -1;
521 int identity_id = -1;
Jerry Yu1c105562022-07-10 06:32:38 +0000522
Gilles Peskine449bd832023-01-11 14:50:10 +0100523 MBEDTLS_SSL_DEBUG_BUF(3, "pre_shared_key extension",
524 pre_shared_key_ext,
525 pre_shared_key_ext_end - pre_shared_key_ext);
Jerry Yu1c105562022-07-10 06:32:38 +0000526
Jerry Yu96a2e362022-07-21 15:11:34 +0800527 /* identities_len 2 bytes
528 * identities_data >= 7 bytes
529 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100530 MBEDTLS_SSL_CHK_BUF_READ_PTR(identities, pre_shared_key_ext_end, 7 + 2);
531 identities_len = MBEDTLS_GET_UINT16_BE(identities, 0);
Jerry Yu568ec252022-07-22 21:27:34 +0800532 p_identity_len = identities + 2;
Gilles Peskine449bd832023-01-11 14:50:10 +0100533 MBEDTLS_SSL_CHK_BUF_READ_PTR(p_identity_len, pre_shared_key_ext_end,
534 identities_len);
Jerry Yu568ec252022-07-22 21:27:34 +0800535 identities_end = p_identity_len + identities_len;
Jerry Yu1c105562022-07-10 06:32:38 +0000536
Jerry Yu96a2e362022-07-21 15:11:34 +0800537 /* binders_len 2 bytes
538 * binders >= 33 bytes
539 */
Jerry Yu568ec252022-07-22 21:27:34 +0800540 binders = identities_end;
Gilles Peskine449bd832023-01-11 14:50:10 +0100541 MBEDTLS_SSL_CHK_BUF_READ_PTR(binders, pre_shared_key_ext_end, 33 + 2);
542 binders_len = MBEDTLS_GET_UINT16_BE(binders, 0);
Jerry Yu568ec252022-07-22 21:27:34 +0800543 p_binder_len = binders + 2;
Gilles Peskine449bd832023-01-11 14:50:10 +0100544 MBEDTLS_SSL_CHK_BUF_READ_PTR(p_binder_len, pre_shared_key_ext_end, binders_len);
Jerry Yu568ec252022-07-22 21:27:34 +0800545 binders_end = p_binder_len + binders_len;
Jerry Yu1c105562022-07-10 06:32:38 +0000546
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +0100547 ret = ssl->handshake->update_checksum(ssl, pre_shared_key_ext,
548 identities_end - pre_shared_key_ext);
549 if (0 != ret) {
550 MBEDTLS_SSL_DEBUG_RET(1, ("update_checksum"), ret);
551 return ret;
552 }
Jerry Yu96a2e362022-07-21 15:11:34 +0800553
Gilles Peskine449bd832023-01-11 14:50:10 +0100554 while (p_identity_len < identities_end && p_binder_len < binders_end) {
Jerry Yu1c105562022-07-10 06:32:38 +0000555 const unsigned char *identity;
Jerry Yu568ec252022-07-22 21:27:34 +0800556 size_t identity_len;
Jerry Yu82534862022-08-30 10:42:33 +0800557 uint32_t obfuscated_ticket_age;
Jerry Yu96a2e362022-07-21 15:11:34 +0800558 const unsigned char *binder;
Jerry Yu568ec252022-07-22 21:27:34 +0800559 size_t binder_len;
Jerry Yu5725f1c2022-08-21 17:27:16 +0800560 int psk_type;
561 uint16_t cipher_suite;
Jerry Yu29d9faa2022-08-23 17:52:45 +0800562 const mbedtls_ssl_ciphersuite_t *ciphersuite_info;
Jerry Yu82534862022-08-30 10:42:33 +0800563#if defined(MBEDTLS_SSL_SESSION_TICKETS)
564 mbedtls_ssl_session session;
Gilles Peskine449bd832023-01-11 14:50:10 +0100565 mbedtls_ssl_session_init(&session);
Jerry Yu82534862022-08-30 10:42:33 +0800566#endif
Jerry Yubb852022022-07-20 21:10:44 +0800567
Gilles Peskine449bd832023-01-11 14:50:10 +0100568 MBEDTLS_SSL_CHK_BUF_READ_PTR(p_identity_len, identities_end, 2 + 1 + 4);
569 identity_len = MBEDTLS_GET_UINT16_BE(p_identity_len, 0);
Jerry Yu568ec252022-07-22 21:27:34 +0800570 identity = p_identity_len + 2;
Gilles Peskine449bd832023-01-11 14:50:10 +0100571 MBEDTLS_SSL_CHK_BUF_READ_PTR(identity, identities_end, identity_len + 4);
572 obfuscated_ticket_age = MBEDTLS_GET_UINT32_BE(identity, identity_len);
Jerry Yu568ec252022-07-22 21:27:34 +0800573 p_identity_len += identity_len + 6;
Jerry Yu1c105562022-07-10 06:32:38 +0000574
Gilles Peskine449bd832023-01-11 14:50:10 +0100575 MBEDTLS_SSL_CHK_BUF_READ_PTR(p_binder_len, binders_end, 1 + 32);
Jerry Yu568ec252022-07-22 21:27:34 +0800576 binder_len = *p_binder_len;
577 binder = p_binder_len + 1;
Gilles Peskine449bd832023-01-11 14:50:10 +0100578 MBEDTLS_SSL_CHK_BUF_READ_PTR(binder, binders_end, binder_len);
Jerry Yu568ec252022-07-22 21:27:34 +0800579 p_binder_len += binder_len + 1;
Jerry Yu96a2e362022-07-21 15:11:34 +0800580
Jerry Yu96a2e362022-07-21 15:11:34 +0800581 identity_id++;
Gilles Peskine449bd832023-01-11 14:50:10 +0100582 if (matched_identity != -1) {
Jerry Yu1c105562022-07-10 06:32:38 +0000583 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +0100584 }
Jerry Yu1c105562022-07-10 06:32:38 +0000585
Jerry Yu96a2e362022-07-21 15:11:34 +0800586 ret = ssl_tls13_offered_psks_check_identity_match(
Gilles Peskine449bd832023-01-11 14:50:10 +0100587 ssl, identity, identity_len, obfuscated_ticket_age,
588 &psk_type, &session);
589 if (ret != SSL_TLS1_3_OFFERED_PSK_MATCH) {
Jerry Yu96a2e362022-07-21 15:11:34 +0800590 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +0100591 }
Jerry Yu96a2e362022-07-21 15:11:34 +0800592
Gilles Peskine449bd832023-01-11 14:50:10 +0100593 MBEDTLS_SSL_DEBUG_MSG(4, ("found matched identity"));
594 switch (psk_type) {
Jerry Yu0baf9072022-08-25 11:21:04 +0800595 case MBEDTLS_SSL_TLS1_3_PSK_EXTERNAL:
596 ret = ssl_tls13_select_ciphersuite_for_psk(
Gilles Peskine449bd832023-01-11 14:50:10 +0100597 ssl, ciphersuites, ciphersuites_end,
598 &cipher_suite, &ciphersuite_info);
Jerry Yu0baf9072022-08-25 11:21:04 +0800599 break;
600 case MBEDTLS_SSL_TLS1_3_PSK_RESUMPTION:
Jerry Yu82534862022-08-30 10:42:33 +0800601#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Jerry Yu0baf9072022-08-25 11:21:04 +0800602 ret = ssl_tls13_select_ciphersuite_for_resumption(
Gilles Peskine449bd832023-01-11 14:50:10 +0100603 ssl, ciphersuites, ciphersuites_end, &session,
604 &cipher_suite, &ciphersuite_info);
605 if (ret != 0) {
606 mbedtls_ssl_session_free(&session);
607 }
Jerry Yu82534862022-08-30 10:42:33 +0800608#else
609 ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
610#endif
Jerry Yu0baf9072022-08-25 11:21:04 +0800611 break;
612 default:
Gilles Peskine449bd832023-01-11 14:50:10 +0100613 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Jerry Yu0baf9072022-08-25 11:21:04 +0800614 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100615 if (ret != 0) {
Jerry Yuf35ba382022-08-23 17:58:26 +0800616 /* See below, no cipher_suite available, abort handshake */
617 MBEDTLS_SSL_PEND_FATAL_ALERT(
618 MBEDTLS_SSL_ALERT_MSG_DECRYPT_ERROR,
Gilles Peskine449bd832023-01-11 14:50:10 +0100619 MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE);
Jerry Yuf35ba382022-08-23 17:58:26 +0800620 MBEDTLS_SSL_DEBUG_RET(
Gilles Peskine449bd832023-01-11 14:50:10 +0100621 2, "ssl_tls13_select_ciphersuite", ret);
622 return ret;
Jerry Yu5725f1c2022-08-21 17:27:16 +0800623 }
624
Jerry Yu96a2e362022-07-21 15:11:34 +0800625 ret = ssl_tls13_offered_psks_check_binder_match(
Gilles Peskine449bd832023-01-11 14:50:10 +0100626 ssl, binder, binder_len, psk_type,
Dave Rodgman2eab4622023-10-05 13:30:37 +0100627 mbedtls_md_psa_alg_from_type((mbedtls_md_type_t) ciphersuite_info->mac));
Gilles Peskine449bd832023-01-11 14:50:10 +0100628 if (ret != SSL_TLS1_3_OFFERED_PSK_MATCH) {
Jerry Yuc5a23a02022-08-25 10:51:44 +0800629 /* For security reasons, the handshake should be aborted when we
630 * fail to validate a binder value. See RFC 8446 section 4.2.11.2
631 * and appendix E.6. */
Jerry Yu82534862022-08-30 10:42:33 +0800632#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Gilles Peskine449bd832023-01-11 14:50:10 +0100633 mbedtls_ssl_session_free(&session);
Jerry Yu82534862022-08-30 10:42:33 +0800634#endif
Gilles Peskine449bd832023-01-11 14:50:10 +0100635 MBEDTLS_SSL_DEBUG_MSG(3, ("Invalid binder."));
Xiaokang Qian9f1747b2023-03-30 02:50:04 +0000636 MBEDTLS_SSL_DEBUG_RET(
637 1, "ssl_tls13_offered_psks_check_binder_match", ret);
Jerry Yu96a2e362022-07-21 15:11:34 +0800638 MBEDTLS_SSL_PEND_FATAL_ALERT(
639 MBEDTLS_SSL_ALERT_MSG_DECRYPT_ERROR,
Gilles Peskine449bd832023-01-11 14:50:10 +0100640 MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE);
641 return ret;
Jerry Yu96a2e362022-07-21 15:11:34 +0800642 }
Jerry Yu96a2e362022-07-21 15:11:34 +0800643
644 matched_identity = identity_id;
Jerry Yu5725f1c2022-08-21 17:27:16 +0800645
646 /* Update handshake parameters */
Jerry Yue5834fd2022-08-29 20:16:09 +0800647 ssl->handshake->ciphersuite_info = ciphersuite_info;
Jerry Yu4746b102022-09-13 11:11:48 +0800648 ssl->session_negotiate->ciphersuite = cipher_suite;
Gilles Peskine449bd832023-01-11 14:50:10 +0100649 MBEDTLS_SSL_DEBUG_MSG(2, ("overwrite ciphersuite: %04x - %s",
650 cipher_suite, ciphersuite_info->name));
Jerry Yu82534862022-08-30 10:42:33 +0800651#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Gilles Peskine449bd832023-01-11 14:50:10 +0100652 if (psk_type == MBEDTLS_SSL_TLS1_3_PSK_RESUMPTION) {
653 ret = ssl_tls13_session_copy_ticket(ssl->session_negotiate,
654 &session);
655 mbedtls_ssl_session_free(&session);
656 if (ret != 0) {
657 return ret;
658 }
Jerry Yu82534862022-08-30 10:42:33 +0800659 }
660#endif /* MBEDTLS_SSL_SESSION_TICKETS */
Jerry Yu1c105562022-07-10 06:32:38 +0000661 }
662
Gilles Peskine449bd832023-01-11 14:50:10 +0100663 if (p_identity_len != identities_end || p_binder_len != binders_end) {
664 MBEDTLS_SSL_DEBUG_MSG(3, ("pre_shared_key extension decode error"));
665 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR,
666 MBEDTLS_ERR_SSL_DECODE_ERROR);
667 return MBEDTLS_ERR_SSL_DECODE_ERROR;
Jerry Yu1c105562022-07-10 06:32:38 +0000668 }
669
Jerry Yu6f1db3f2022-07-22 23:05:59 +0800670 /* Update the handshake transcript with the binder list. */
Xiaokang Qian9f1747b2023-03-30 02:50:04 +0000671 ret = ssl->handshake->update_checksum(
672 ssl, identities_end, (size_t) (binders_end - identities_end));
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +0100673 if (0 != ret) {
674 MBEDTLS_SSL_DEBUG_RET(1, ("update_checksum"), ret);
675 return ret;
676 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100677 if (matched_identity == -1) {
678 MBEDTLS_SSL_DEBUG_MSG(3, ("No matched PSK or ticket."));
679 return MBEDTLS_ERR_SSL_UNKNOWN_IDENTITY;
Jerry Yu1c105562022-07-10 06:32:38 +0000680 }
681
Gilles Peskine449bd832023-01-11 14:50:10 +0100682 ssl->handshake->selected_identity = (uint16_t) matched_identity;
683 MBEDTLS_SSL_DEBUG_MSG(3, ("Pre shared key found"));
Jerry Yu1c105562022-07-10 06:32:38 +0000684
Gilles Peskine449bd832023-01-11 14:50:10 +0100685 return 0;
Jerry Yu1c105562022-07-10 06:32:38 +0000686}
Jerry Yu032b15ce2022-07-11 06:10:03 +0000687
688/*
689 * struct {
690 * select ( Handshake.msg_type ) {
691 * ....
692 * case server_hello:
693 * uint16 selected_identity;
694 * }
695 * } PreSharedKeyExtension;
696 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100697static int ssl_tls13_write_server_pre_shared_key_ext(mbedtls_ssl_context *ssl,
698 unsigned char *buf,
699 unsigned char *end,
700 size_t *olen)
Jerry Yu032b15ce2022-07-11 06:10:03 +0000701{
Gilles Peskine449bd832023-01-11 14:50:10 +0100702 unsigned char *p = (unsigned char *) buf;
Jerry Yu032b15ce2022-07-11 06:10:03 +0000703
704 *olen = 0;
705
David Horstmann21b89762022-10-06 18:34:28 +0100706 int not_using_psk = 0;
Jerry Yu032b15ce2022-07-11 06:10:03 +0000707#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +0100708 not_using_psk = (mbedtls_svc_key_id_is_null(ssl->handshake->psk_opaque));
Jerry Yu032b15ce2022-07-11 06:10:03 +0000709#else
Gilles Peskine449bd832023-01-11 14:50:10 +0100710 not_using_psk = (ssl->handshake->psk == NULL);
Jerry Yu032b15ce2022-07-11 06:10:03 +0000711#endif
Gilles Peskine449bd832023-01-11 14:50:10 +0100712 if (not_using_psk) {
Jerry Yu032b15ce2022-07-11 06:10:03 +0000713 /* We shouldn't have called this extension writer unless we've
714 * chosen to use a PSK. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100715 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Jerry Yu032b15ce2022-07-11 06:10:03 +0000716 }
717
Gilles Peskine449bd832023-01-11 14:50:10 +0100718 MBEDTLS_SSL_DEBUG_MSG(3, ("server hello, adding pre_shared_key extension"));
719 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 6);
Jerry Yu032b15ce2022-07-11 06:10:03 +0000720
Gilles Peskine449bd832023-01-11 14:50:10 +0100721 MBEDTLS_PUT_UINT16_BE(MBEDTLS_TLS_EXT_PRE_SHARED_KEY, p, 0);
722 MBEDTLS_PUT_UINT16_BE(2, p, 2);
Jerry Yu032b15ce2022-07-11 06:10:03 +0000723
Gilles Peskine449bd832023-01-11 14:50:10 +0100724 MBEDTLS_PUT_UINT16_BE(ssl->handshake->selected_identity, p, 4);
Jerry Yu032b15ce2022-07-11 06:10:03 +0000725
726 *olen = 6;
727
Gilles Peskine449bd832023-01-11 14:50:10 +0100728 MBEDTLS_SSL_DEBUG_MSG(4, ("sent selected_identity: %u",
729 ssl->handshake->selected_identity));
Jerry Yu032b15ce2022-07-11 06:10:03 +0000730
Gilles Peskine449bd832023-01-11 14:50:10 +0100731 mbedtls_ssl_tls13_set_hs_sent_ext_mask(ssl, MBEDTLS_TLS_EXT_PRE_SHARED_KEY);
Jerry Yub95dd362022-11-08 21:19:34 +0800732
Gilles Peskine449bd832023-01-11 14:50:10 +0100733 return 0;
Jerry Yu032b15ce2022-07-11 06:10:03 +0000734}
735
Ronald Cron41a443a2022-10-04 16:38:25 +0200736#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED */
Jerry Yue19e3b92022-07-08 12:04:51 +0000737
XiaokangQian7807f9f2022-02-15 10:04:37 +0000738/* From RFC 8446:
739 * struct {
XiaokangQiancfd925f2022-04-14 07:10:37 +0000740 * ProtocolVersion versions<2..254>;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000741 * } SupportedVersions;
742 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200743MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100744static int ssl_tls13_parse_supported_versions_ext(mbedtls_ssl_context *ssl,
745 const unsigned char *buf,
746 const unsigned char *end)
XiaokangQian7807f9f2022-02-15 10:04:37 +0000747{
XiaokangQian7807f9f2022-02-15 10:04:37 +0000748 const unsigned char *p = buf;
XiaokangQian8f9dfe42022-04-15 02:52:39 +0000749 size_t versions_len;
XiaokangQian4080a7f2022-04-11 09:55:18 +0000750 const unsigned char *versions_end;
XiaokangQian0a1b54e2022-04-21 03:01:38 +0000751 uint16_t tls_version;
Ronald Cron5af4c7f2023-03-07 20:46:59 +0100752 int found_supported_version = 0;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000753
Gilles Peskine449bd832023-01-11 14:50:10 +0100754 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, 1);
XiaokangQian4080a7f2022-04-11 09:55:18 +0000755 versions_len = p[0];
XiaokangQian7807f9f2022-02-15 10:04:37 +0000756 p += 1;
757
Gilles Peskine449bd832023-01-11 14:50:10 +0100758 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, versions_len);
XiaokangQian4080a7f2022-04-11 09:55:18 +0000759 versions_end = p + versions_len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100760 while (p < versions_end) {
761 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, versions_end, 2);
762 tls_version = mbedtls_ssl_read_version(p, ssl->conf->transport);
XiaokangQianb67384d2022-04-19 00:02:38 +0000763 p += 2;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000764
Ronald Cron3bd2b022023-04-03 16:45:39 +0200765 if (MBEDTLS_SSL_VERSION_TLS1_3 == tls_version) {
Ronald Cron5af4c7f2023-03-07 20:46:59 +0100766 found_supported_version = 1;
767 break;
768 }
769
Ronald Cron3bd2b022023-04-03 16:45:39 +0200770 if ((MBEDTLS_SSL_VERSION_TLS1_2 == tls_version) &&
771 mbedtls_ssl_conf_is_tls12_enabled(ssl->conf)) {
Ronald Cron5af4c7f2023-03-07 20:46:59 +0100772 found_supported_version = 1;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000773 break;
774 }
XiaokangQian7807f9f2022-02-15 10:04:37 +0000775 }
776
Ronald Cron5af4c7f2023-03-07 20:46:59 +0100777 if (!found_supported_version) {
778 MBEDTLS_SSL_DEBUG_MSG(1, ("No supported version found."));
XiaokangQian7807f9f2022-02-15 10:04:37 +0000779
Gilles Peskine449bd832023-01-11 14:50:10 +0100780 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_PROTOCOL_VERSION,
781 MBEDTLS_ERR_SSL_BAD_PROTOCOL_VERSION);
782 return MBEDTLS_ERR_SSL_BAD_PROTOCOL_VERSION;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000783 }
784
Ronald Cron5af4c7f2023-03-07 20:46:59 +0100785 MBEDTLS_SSL_DEBUG_MSG(1, ("Negotiated version: [%04x]",
Gilles Peskine449bd832023-01-11 14:50:10 +0100786 (unsigned int) tls_version));
XiaokangQian7807f9f2022-02-15 10:04:37 +0000787
Ronald Cron5af4c7f2023-03-07 20:46:59 +0100788 return (int) tls_version;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000789}
790
Przemek Stekiel8c0a9532023-06-15 16:48:19 +0200791#if defined(PSA_WANT_ALG_ECDH) || defined(PSA_WANT_ALG_FFDH)
XiaokangQiane8ff3502022-04-22 02:34:40 +0000792/*
XiaokangQian7807f9f2022-02-15 10:04:37 +0000793 *
794 * From RFC 8446:
795 * enum {
796 * ... (0xFFFF)
797 * } NamedGroup;
798 * struct {
799 * NamedGroup named_group_list<2..2^16-1>;
800 * } NamedGroupList;
801 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200802MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100803static int ssl_tls13_parse_supported_groups_ext(mbedtls_ssl_context *ssl,
804 const unsigned char *buf,
805 const unsigned char *end)
XiaokangQian7807f9f2022-02-15 10:04:37 +0000806{
XiaokangQian7807f9f2022-02-15 10:04:37 +0000807 const unsigned char *p = buf;
XiaokangQian84823772022-04-19 07:57:30 +0000808 size_t named_group_list_len;
XiaokangQian8f9dfe42022-04-15 02:52:39 +0000809 const unsigned char *named_group_list_end;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000810
Gilles Peskine449bd832023-01-11 14:50:10 +0100811 MBEDTLS_SSL_DEBUG_BUF(3, "supported_groups extension", p, end - buf);
812 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, 2);
813 named_group_list_len = MBEDTLS_GET_UINT16_BE(p, 0);
XiaokangQian7807f9f2022-02-15 10:04:37 +0000814 p += 2;
Gilles Peskine449bd832023-01-11 14:50:10 +0100815 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, named_group_list_len);
XiaokangQian8f9dfe42022-04-15 02:52:39 +0000816 named_group_list_end = p + named_group_list_len;
XiaokangQian4e8cd7b2022-04-21 09:48:09 +0000817 ssl->handshake->hrr_selected_group = 0;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000818
Gilles Peskine449bd832023-01-11 14:50:10 +0100819 while (p < named_group_list_end) {
XiaokangQian08037552022-04-20 07:16:41 +0000820 uint16_t named_group;
Gilles Peskine449bd832023-01-11 14:50:10 +0100821 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, named_group_list_end, 2);
822 named_group = MBEDTLS_GET_UINT16_BE(p, 0);
XiaokangQian08037552022-04-20 07:16:41 +0000823 p += 2;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000824
Gilles Peskine449bd832023-01-11 14:50:10 +0100825 MBEDTLS_SSL_DEBUG_MSG(2,
826 ("got named group: %s(%04x)",
827 mbedtls_ssl_named_group_to_str(named_group),
828 named_group));
XiaokangQian08037552022-04-20 07:16:41 +0000829
Gilles Peskine449bd832023-01-11 14:50:10 +0100830 if (!mbedtls_ssl_named_group_is_offered(ssl, named_group) ||
831 !mbedtls_ssl_named_group_is_supported(named_group) ||
832 ssl->handshake->hrr_selected_group != 0) {
XiaokangQian08037552022-04-20 07:16:41 +0000833 continue;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000834 }
835
Gilles Peskine449bd832023-01-11 14:50:10 +0100836 MBEDTLS_SSL_DEBUG_MSG(2,
837 ("add named group %s(%04x) into received list.",
838 mbedtls_ssl_named_group_to_str(named_group),
839 named_group));
Jerry Yuc1be19f2022-04-23 16:11:39 +0800840
XiaokangQian4e8cd7b2022-04-21 09:48:09 +0000841 ssl->handshake->hrr_selected_group = named_group;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000842 }
843
Gilles Peskine449bd832023-01-11 14:50:10 +0100844 return 0;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000845
846}
Przemek Stekiel8c0a9532023-06-15 16:48:19 +0200847#endif /* PSA_WANT_ALG_ECDH || PSA_WANT_ALG_FFDH */
XiaokangQian7807f9f2022-02-15 10:04:37 +0000848
XiaokangQian08037552022-04-20 07:16:41 +0000849#define SSL_TLS1_3_PARSE_KEY_SHARES_EXT_NO_MATCH 1
850
Valerio Settic9ae8622023-07-25 11:23:50 +0200851#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_EPHEMERAL_ENABLED)
XiaokangQian7807f9f2022-02-15 10:04:37 +0000852/*
853 * ssl_tls13_parse_key_shares_ext() verifies whether the information in the
Xiaokang Qian9f1747b2023-03-30 02:50:04 +0000854 * extension is correct and stores the first acceptable key share and its
855 * associated group.
XiaokangQian7807f9f2022-02-15 10:04:37 +0000856 *
857 * Possible return values are:
858 * - 0: Successful processing of the client provided key share extension.
Xiaokang Qian9f1747b2023-03-30 02:50:04 +0000859 * - SSL_TLS1_3_PARSE_KEY_SHARES_EXT_NO_MATCH: The key shares provided by
860 * the client does not match a group supported by the server. A
861 * HelloRetryRequest will be needed.
XiaokangQiane8ff3502022-04-22 02:34:40 +0000862 * - A negative value for fatal errors.
Jerry Yuc1be19f2022-04-23 16:11:39 +0800863 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200864MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100865static int ssl_tls13_parse_key_shares_ext(mbedtls_ssl_context *ssl,
866 const unsigned char *buf,
867 const unsigned char *end)
XiaokangQian7807f9f2022-02-15 10:04:37 +0000868{
XiaokangQianb67384d2022-04-19 00:02:38 +0000869 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000870 unsigned char const *p = buf;
XiaokangQian8f9dfe42022-04-15 02:52:39 +0000871 unsigned char const *client_shares_end;
Jerry Yuc1be19f2022-04-23 16:11:39 +0800872 size_t client_shares_len;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000873
874 /* From RFC 8446:
875 *
876 * struct {
877 * KeyShareEntry client_shares<0..2^16-1>;
878 * } KeyShareClientHello;
879 *
880 */
881
Gilles Peskine449bd832023-01-11 14:50:10 +0100882 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, 2);
883 client_shares_len = MBEDTLS_GET_UINT16_BE(p, 0);
XiaokangQian7807f9f2022-02-15 10:04:37 +0000884 p += 2;
Gilles Peskine449bd832023-01-11 14:50:10 +0100885 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, client_shares_len);
XiaokangQian7807f9f2022-02-15 10:04:37 +0000886
887 ssl->handshake->offered_group_id = 0;
XiaokangQian8f9dfe42022-04-15 02:52:39 +0000888 client_shares_end = p + client_shares_len;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000889
890 /* We try to find a suitable key share entry and copy it to the
891 * handshake context. Later, we have to find out whether we can do
892 * something with the provided key share or whether we have to
XiaokangQianc5763b52022-04-02 03:34:37 +0000893 * dismiss it and send a HelloRetryRequest message.
894 */
XiaokangQian7807f9f2022-02-15 10:04:37 +0000895
Gilles Peskine449bd832023-01-11 14:50:10 +0100896 while (p < client_shares_end) {
XiaokangQian9b5d04b2022-04-10 10:20:43 +0000897 uint16_t group;
Jerry Yuc1be19f2022-04-23 16:11:39 +0800898 size_t key_exchange_len;
Jerry Yu086edc22022-05-05 10:50:38 +0800899 const unsigned char *key_exchange;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000900
901 /*
902 * struct {
903 * NamedGroup group;
904 * opaque key_exchange<1..2^16-1>;
905 * } KeyShareEntry;
906 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100907 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, client_shares_end, 4);
908 group = MBEDTLS_GET_UINT16_BE(p, 0);
909 key_exchange_len = MBEDTLS_GET_UINT16_BE(p, 2);
Jerry Yuc1be19f2022-04-23 16:11:39 +0800910 p += 4;
Jerry Yu086edc22022-05-05 10:50:38 +0800911 key_exchange = p;
Gilles Peskine449bd832023-01-11 14:50:10 +0100912 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, client_shares_end, key_exchange_len);
Jerry Yu086edc22022-05-05 10:50:38 +0800913 p += key_exchange_len;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000914
915 /* Continue parsing even if we have already found a match,
XiaokangQianc5763b52022-04-02 03:34:37 +0000916 * for input validation purposes.
917 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100918 if (!mbedtls_ssl_named_group_is_offered(ssl, group) ||
919 !mbedtls_ssl_named_group_is_supported(group) ||
920 ssl->handshake->offered_group_id != 0) {
XiaokangQian060d8672022-04-21 09:24:56 +0000921 continue;
922 }
XiaokangQian060d8672022-04-21 09:24:56 +0000923
XiaokangQian7807f9f2022-02-15 10:04:37 +0000924 /*
Przemek Stekielc89f3ea2023-05-18 15:45:53 +0200925 * ECDHE and FFDHE groups are supported
XiaokangQian7807f9f2022-02-15 10:04:37 +0000926 */
Przemek Stekielc89f3ea2023-05-18 15:45:53 +0200927 if (mbedtls_ssl_tls13_named_group_is_ecdhe(group) ||
Przemek Stekield5f79e72023-06-29 09:08:43 +0200928 mbedtls_ssl_tls13_named_group_is_ffdh(group)) {
Przemek Stekielc89f3ea2023-05-18 15:45:53 +0200929 MBEDTLS_SSL_DEBUG_MSG(2, ("ECDH/FFDH group: %s (%04x)",
Gilles Peskine449bd832023-01-11 14:50:10 +0100930 mbedtls_ssl_named_group_to_str(group),
931 group));
Przemek Stekiel7ac93be2023-07-04 10:02:38 +0200932 ret = mbedtls_ssl_tls13_read_public_xxdhe_share(
Gilles Peskine449bd832023-01-11 14:50:10 +0100933 ssl, key_exchange - 2, key_exchange_len + 2);
934 if (ret != 0) {
935 return ret;
936 }
XiaokangQian4e8cd7b2022-04-21 09:48:09 +0000937
Gilles Peskine449bd832023-01-11 14:50:10 +0100938 } else {
939 MBEDTLS_SSL_DEBUG_MSG(4, ("Unrecognized NamedGroup %u",
940 (unsigned) group));
XiaokangQian7807f9f2022-02-15 10:04:37 +0000941 continue;
942 }
943
XiaokangQian9b5d04b2022-04-10 10:20:43 +0000944 ssl->handshake->offered_group_id = group;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000945 }
946
Jerry Yuc1be19f2022-04-23 16:11:39 +0800947
Gilles Peskine449bd832023-01-11 14:50:10 +0100948 if (ssl->handshake->offered_group_id == 0) {
949 MBEDTLS_SSL_DEBUG_MSG(1, ("no matching key share"));
950 return SSL_TLS1_3_PARSE_KEY_SHARES_EXT_NO_MATCH;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000951 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100952 return 0;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000953}
Valerio Settic9ae8622023-07-25 11:23:50 +0200954#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_EPHEMERAL_ENABLED */
XiaokangQian7807f9f2022-02-15 10:04:37 +0000955
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200956MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100957static int ssl_tls13_client_hello_has_exts(mbedtls_ssl_context *ssl,
958 int exts_mask)
XiaokangQian7807f9f2022-02-15 10:04:37 +0000959{
Jerry Yu0c354a22022-08-29 15:25:36 +0800960 int masked = ssl->handshake->received_extensions & exts_mask;
Gilles Peskine449bd832023-01-11 14:50:10 +0100961 return masked == exts_mask;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000962}
963
Jerry Yue5991322022-11-07 14:03:44 +0800964#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200965MBEDTLS_CHECK_RETURN_CRITICAL
XiaokangQianb67384d2022-04-19 00:02:38 +0000966static int ssl_tls13_client_hello_has_exts_for_ephemeral_key_exchange(
Gilles Peskine449bd832023-01-11 14:50:10 +0100967 mbedtls_ssl_context *ssl)
XiaokangQian7807f9f2022-02-15 10:04:37 +0000968{
Gilles Peskine449bd832023-01-11 14:50:10 +0100969 return ssl_tls13_client_hello_has_exts(
970 ssl,
971 MBEDTLS_SSL_EXT_MASK(SUPPORTED_GROUPS) |
972 MBEDTLS_SSL_EXT_MASK(KEY_SHARE) |
973 MBEDTLS_SSL_EXT_MASK(SIG_ALG));
XiaokangQian7807f9f2022-02-15 10:04:37 +0000974}
Jerry Yue5991322022-11-07 14:03:44 +0800975#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED */
XiaokangQian7807f9f2022-02-15 10:04:37 +0000976
Jerry Yue5991322022-11-07 14:03:44 +0800977#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK_ENABLED)
Jerry Yu77f01482022-07-11 07:03:24 +0000978MBEDTLS_CHECK_RETURN_CRITICAL
979static int ssl_tls13_client_hello_has_exts_for_psk_key_exchange(
Gilles Peskine449bd832023-01-11 14:50:10 +0100980 mbedtls_ssl_context *ssl)
Jerry Yu77f01482022-07-11 07:03:24 +0000981{
Gilles Peskine449bd832023-01-11 14:50:10 +0100982 return ssl_tls13_client_hello_has_exts(
983 ssl,
984 MBEDTLS_SSL_EXT_MASK(PRE_SHARED_KEY) |
985 MBEDTLS_SSL_EXT_MASK(PSK_KEY_EXCHANGE_MODES));
Jerry Yu77f01482022-07-11 07:03:24 +0000986}
Jerry Yue5991322022-11-07 14:03:44 +0800987#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK_ENABLED */
Jerry Yu77f01482022-07-11 07:03:24 +0000988
Jerry Yue5991322022-11-07 14:03:44 +0800989#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK_EPHEMERAL_ENABLED)
Jerry Yu77f01482022-07-11 07:03:24 +0000990MBEDTLS_CHECK_RETURN_CRITICAL
991static int ssl_tls13_client_hello_has_exts_for_psk_ephemeral_key_exchange(
Gilles Peskine449bd832023-01-11 14:50:10 +0100992 mbedtls_ssl_context *ssl)
Jerry Yu77f01482022-07-11 07:03:24 +0000993{
Gilles Peskine449bd832023-01-11 14:50:10 +0100994 return ssl_tls13_client_hello_has_exts(
995 ssl,
996 MBEDTLS_SSL_EXT_MASK(SUPPORTED_GROUPS) |
997 MBEDTLS_SSL_EXT_MASK(KEY_SHARE) |
998 MBEDTLS_SSL_EXT_MASK(PRE_SHARED_KEY) |
999 MBEDTLS_SSL_EXT_MASK(PSK_KEY_EXCHANGE_MODES));
Jerry Yu77f01482022-07-11 07:03:24 +00001000}
Jerry Yue5991322022-11-07 14:03:44 +08001001#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK_EPHEMERAL_ENABLED */
Jerry Yu77f01482022-07-11 07:03:24 +00001002
Pengyu Lv306a01d2023-02-02 16:35:47 +08001003#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED)
1004MBEDTLS_CHECK_RETURN_CRITICAL
Pengyu Lv52ad3332023-02-14 14:32:37 +08001005static int ssl_tls13_ticket_permission_check(mbedtls_ssl_context *ssl,
1006 unsigned int kex_mode)
Pengyu Lv306a01d2023-02-02 16:35:47 +08001007{
1008#if defined(MBEDTLS_SSL_SESSION_TICKETS)
1009 if (ssl->handshake->resume) {
Pengyu Lv7b711712023-10-24 17:07:14 +08001010 if (mbedtls_ssl_session_check_ticket_flags(
Pengyu Lv306a01d2023-02-02 16:35:47 +08001011 ssl->session_negotiate, kex_mode)) {
1012 return 0;
1013 }
1014 }
1015#else
1016 ((void) ssl);
1017 ((void) kex_mode);
1018#endif
1019 return 1;
1020}
1021#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED */
1022
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001023MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01001024static int ssl_tls13_check_ephemeral_key_exchange(mbedtls_ssl_context *ssl)
XiaokangQian7807f9f2022-02-15 10:04:37 +00001025{
Jerry Yue5991322022-11-07 14:03:44 +08001026#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01001027 return mbedtls_ssl_conf_tls13_ephemeral_enabled(ssl) &&
1028 ssl_tls13_client_hello_has_exts_for_ephemeral_key_exchange(ssl);
Jerry Yue5991322022-11-07 14:03:44 +08001029#else
1030 ((void) ssl);
Gilles Peskine449bd832023-01-11 14:50:10 +01001031 return 0;
Jerry Yue5991322022-11-07 14:03:44 +08001032#endif
Jerry Yu77f01482022-07-11 07:03:24 +00001033}
XiaokangQian7807f9f2022-02-15 10:04:37 +00001034
Jerry Yu77f01482022-07-11 07:03:24 +00001035MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01001036static int ssl_tls13_check_psk_key_exchange(mbedtls_ssl_context *ssl)
Jerry Yu77f01482022-07-11 07:03:24 +00001037{
Jerry Yue5991322022-11-07 14:03:44 +08001038#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK_ENABLED)
Pengyu Lv52ad3332023-02-14 14:32:37 +08001039 return ssl_tls13_ticket_permission_check(
Pengyu Lv306a01d2023-02-02 16:35:47 +08001040 ssl, MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK) &&
1041 mbedtls_ssl_conf_tls13_psk_enabled(ssl) &&
Gilles Peskine449bd832023-01-11 14:50:10 +01001042 mbedtls_ssl_tls13_psk_enabled(ssl) &&
1043 ssl_tls13_client_hello_has_exts_for_psk_key_exchange(ssl);
Jerry Yu77f01482022-07-11 07:03:24 +00001044#else
1045 ((void) ssl);
Gilles Peskine449bd832023-01-11 14:50:10 +01001046 return 0;
Jerry Yu77f01482022-07-11 07:03:24 +00001047#endif
1048}
XiaokangQian7807f9f2022-02-15 10:04:37 +00001049
Jerry Yu77f01482022-07-11 07:03:24 +00001050MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01001051static int ssl_tls13_check_psk_ephemeral_key_exchange(mbedtls_ssl_context *ssl)
Jerry Yu77f01482022-07-11 07:03:24 +00001052{
Jerry Yue5991322022-11-07 14:03:44 +08001053#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK_EPHEMERAL_ENABLED)
Pengyu Lv52ad3332023-02-14 14:32:37 +08001054 return ssl_tls13_ticket_permission_check(
Pengyu Lv306a01d2023-02-02 16:35:47 +08001055 ssl, MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK_EPHEMERAL) &&
1056 mbedtls_ssl_conf_tls13_psk_ephemeral_enabled(ssl) &&
Gilles Peskine449bd832023-01-11 14:50:10 +01001057 mbedtls_ssl_tls13_psk_ephemeral_enabled(ssl) &&
1058 ssl_tls13_client_hello_has_exts_for_psk_ephemeral_key_exchange(ssl);
Jerry Yu77f01482022-07-11 07:03:24 +00001059#else
1060 ((void) ssl);
Gilles Peskine449bd832023-01-11 14:50:10 +01001061 return 0;
Jerry Yu77f01482022-07-11 07:03:24 +00001062#endif
1063}
1064
Gilles Peskine449bd832023-01-11 14:50:10 +01001065static int ssl_tls13_determine_key_exchange_mode(mbedtls_ssl_context *ssl)
Jerry Yu77f01482022-07-11 07:03:24 +00001066{
1067 /*
1068 * Determine the key exchange algorithm to use.
1069 * There are three types of key exchanges supported in TLS 1.3:
1070 * - (EC)DH with ECDSA,
1071 * - (EC)DH with PSK,
1072 * - plain PSK.
1073 *
1074 * The PSK-based key exchanges may additionally be used with 0-RTT.
1075 *
1076 * Our built-in order of preference is
Jerry Yuce6ed702022-07-22 21:49:53 +08001077 * 1 ) (EC)DHE-PSK Mode ( psk_ephemeral )
1078 * 2 ) Certificate Mode ( ephemeral )
1079 * 3 ) Plain PSK Mode ( psk )
Jerry Yu77f01482022-07-11 07:03:24 +00001080 */
1081
Xiaokang Qian9f1747b2023-03-30 02:50:04 +00001082 ssl->handshake->key_exchange_mode =
1083 MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_NONE;
Jerry Yu77f01482022-07-11 07:03:24 +00001084
Gilles Peskine449bd832023-01-11 14:50:10 +01001085 if (ssl_tls13_check_psk_ephemeral_key_exchange(ssl)) {
Jerry Yu77f01482022-07-11 07:03:24 +00001086 ssl->handshake->key_exchange_mode =
1087 MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK_EPHEMERAL;
Gilles Peskine449bd832023-01-11 14:50:10 +01001088 MBEDTLS_SSL_DEBUG_MSG(2, ("key exchange mode: psk_ephemeral"));
1089 } else
1090 if (ssl_tls13_check_ephemeral_key_exchange(ssl)) {
Jerry Yu77f01482022-07-11 07:03:24 +00001091 ssl->handshake->key_exchange_mode =
1092 MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL;
Gilles Peskine449bd832023-01-11 14:50:10 +01001093 MBEDTLS_SSL_DEBUG_MSG(2, ("key exchange mode: ephemeral"));
1094 } else
1095 if (ssl_tls13_check_psk_key_exchange(ssl)) {
Jerry Yuce6ed702022-07-22 21:49:53 +08001096 ssl->handshake->key_exchange_mode =
1097 MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK;
Gilles Peskine449bd832023-01-11 14:50:10 +01001098 MBEDTLS_SSL_DEBUG_MSG(2, ("key exchange mode: psk"));
1099 } else {
Jerry Yu77f01482022-07-11 07:03:24 +00001100 MBEDTLS_SSL_DEBUG_MSG(
Gilles Peskine449bd832023-01-11 14:50:10 +01001101 1,
1102 ("ClientHello message misses mandatory extensions."));
1103 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_MISSING_EXTENSION,
1104 MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER);
1105 return MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER;
Jerry Yu77f01482022-07-11 07:03:24 +00001106 }
1107
Gilles Peskine449bd832023-01-11 14:50:10 +01001108 return 0;
Jerry Yu77f01482022-07-11 07:03:24 +00001109
XiaokangQian7807f9f2022-02-15 10:04:37 +00001110}
1111
XiaokangQian81802f42022-06-10 13:25:22 +00001112#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
Ronald Cron928cbd32022-10-04 16:14:26 +02001113 defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED)
Ronald Cron67ea2542022-09-15 17:34:42 +02001114
1115#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01001116static psa_algorithm_t ssl_tls13_iana_sig_alg_to_psa_alg(uint16_t sig_alg)
Ronald Cron67ea2542022-09-15 17:34:42 +02001117{
Gilles Peskine449bd832023-01-11 14:50:10 +01001118 switch (sig_alg) {
Ronald Cron67ea2542022-09-15 17:34:42 +02001119 case MBEDTLS_TLS1_3_SIG_ECDSA_SECP256R1_SHA256:
Gilles Peskine449bd832023-01-11 14:50:10 +01001120 return PSA_ALG_ECDSA(PSA_ALG_SHA_256);
Ronald Cron67ea2542022-09-15 17:34:42 +02001121 case MBEDTLS_TLS1_3_SIG_ECDSA_SECP384R1_SHA384:
Gilles Peskine449bd832023-01-11 14:50:10 +01001122 return PSA_ALG_ECDSA(PSA_ALG_SHA_384);
Ronald Cron67ea2542022-09-15 17:34:42 +02001123 case MBEDTLS_TLS1_3_SIG_ECDSA_SECP521R1_SHA512:
Gilles Peskine449bd832023-01-11 14:50:10 +01001124 return PSA_ALG_ECDSA(PSA_ALG_SHA_512);
Ronald Cron67ea2542022-09-15 17:34:42 +02001125 case MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA256:
Gilles Peskine449bd832023-01-11 14:50:10 +01001126 return PSA_ALG_RSA_PSS(PSA_ALG_SHA_256);
Ronald Cron67ea2542022-09-15 17:34:42 +02001127 case MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA384:
Gilles Peskine449bd832023-01-11 14:50:10 +01001128 return PSA_ALG_RSA_PSS(PSA_ALG_SHA_384);
Ronald Cron67ea2542022-09-15 17:34:42 +02001129 case MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA512:
Gilles Peskine449bd832023-01-11 14:50:10 +01001130 return PSA_ALG_RSA_PSS(PSA_ALG_SHA_512);
Ronald Cron67ea2542022-09-15 17:34:42 +02001131 case MBEDTLS_TLS1_3_SIG_RSA_PKCS1_SHA256:
Gilles Peskine449bd832023-01-11 14:50:10 +01001132 return PSA_ALG_RSA_PKCS1V15_SIGN(PSA_ALG_SHA_256);
Ronald Cron67ea2542022-09-15 17:34:42 +02001133 case MBEDTLS_TLS1_3_SIG_RSA_PKCS1_SHA384:
Gilles Peskine449bd832023-01-11 14:50:10 +01001134 return PSA_ALG_RSA_PKCS1V15_SIGN(PSA_ALG_SHA_384);
Ronald Cron67ea2542022-09-15 17:34:42 +02001135 case MBEDTLS_TLS1_3_SIG_RSA_PKCS1_SHA512:
Gilles Peskine449bd832023-01-11 14:50:10 +01001136 return PSA_ALG_RSA_PKCS1V15_SIGN(PSA_ALG_SHA_512);
Ronald Cron67ea2542022-09-15 17:34:42 +02001137 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01001138 return PSA_ALG_NONE;
Ronald Cron67ea2542022-09-15 17:34:42 +02001139 }
1140}
1141#endif /* MBEDTLS_USE_PSA_CRYPTO */
1142
XiaokangQian23c5be62022-06-07 02:04:34 +00001143/*
XiaokangQianfb665a82022-06-15 03:57:21 +00001144 * Pick best ( private key, certificate chain ) pair based on the signature
1145 * algorithms supported by the client.
XiaokangQian23c5be62022-06-07 02:04:34 +00001146 */
Ronald Cronce7d76e2022-07-08 18:56:49 +02001147MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01001148static int ssl_tls13_pick_key_cert(mbedtls_ssl_context *ssl)
XiaokangQian23c5be62022-06-07 02:04:34 +00001149{
XiaokangQianfb665a82022-06-15 03:57:21 +00001150 mbedtls_ssl_key_cert *key_cert, *key_cert_list;
XiaokangQian81802f42022-06-10 13:25:22 +00001151 const uint16_t *sig_alg = ssl->handshake->received_sig_algs;
XiaokangQian23c5be62022-06-07 02:04:34 +00001152
1153#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Gilles Peskine449bd832023-01-11 14:50:10 +01001154 if (ssl->handshake->sni_key_cert != NULL) {
XiaokangQianfb665a82022-06-15 03:57:21 +00001155 key_cert_list = ssl->handshake->sni_key_cert;
Gilles Peskine449bd832023-01-11 14:50:10 +01001156 } else
XiaokangQian23c5be62022-06-07 02:04:34 +00001157#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
Gilles Peskine449bd832023-01-11 14:50:10 +01001158 key_cert_list = ssl->conf->key_cert;
XiaokangQian23c5be62022-06-07 02:04:34 +00001159
Gilles Peskine449bd832023-01-11 14:50:10 +01001160 if (key_cert_list == NULL) {
1161 MBEDTLS_SSL_DEBUG_MSG(3, ("server has no certificate"));
1162 return -1;
XiaokangQian23c5be62022-06-07 02:04:34 +00001163 }
1164
Gilles Peskine449bd832023-01-11 14:50:10 +01001165 for (; *sig_alg != MBEDTLS_TLS1_3_SIG_NONE; sig_alg++) {
1166 if (!mbedtls_ssl_sig_alg_is_offered(ssl, *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 if (!mbedtls_ssl_tls13_sig_alg_for_cert_verify_is_supported(*sig_alg)) {
Ronald Cron67ea2542022-09-15 17:34:42 +02001171 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +01001172 }
Ronald Cron67ea2542022-09-15 17:34:42 +02001173
Gilles Peskine449bd832023-01-11 14:50:10 +01001174 for (key_cert = key_cert_list; key_cert != NULL;
1175 key_cert = key_cert->next) {
Ronald Cron67ea2542022-09-15 17:34:42 +02001176#if defined(MBEDTLS_USE_PSA_CRYPTO)
1177 psa_algorithm_t psa_alg = PSA_ALG_NONE;
1178#endif /* MBEDTLS_USE_PSA_CRYPTO */
1179
Gilles Peskine449bd832023-01-11 14:50:10 +01001180 MBEDTLS_SSL_DEBUG_CRT(3, "certificate (chain) candidate",
1181 key_cert->cert);
XiaokangQian81802f42022-06-10 13:25:22 +00001182
1183 /*
Gilles Peskine449bd832023-01-11 14:50:10 +01001184 * This avoids sending the client a cert it'll reject based on
1185 * keyUsage or other extensions.
1186 */
1187 if (mbedtls_x509_crt_check_key_usage(
1188 key_cert->cert, MBEDTLS_X509_KU_DIGITAL_SIGNATURE) != 0 ||
XiaokangQian81802f42022-06-10 13:25:22 +00001189 mbedtls_x509_crt_check_extended_key_usage(
XiaokangQianfb665a82022-06-15 03:57:21 +00001190 key_cert->cert, MBEDTLS_OID_SERVER_AUTH,
Gilles Peskine449bd832023-01-11 14:50:10 +01001191 MBEDTLS_OID_SIZE(MBEDTLS_OID_SERVER_AUTH)) != 0) {
1192 MBEDTLS_SSL_DEBUG_MSG(3, ("certificate mismatch: "
1193 "(extended) key usage extension"));
XiaokangQian81802f42022-06-10 13:25:22 +00001194 continue;
1195 }
XiaokangQian23c5be62022-06-07 02:04:34 +00001196
Gilles Peskine449bd832023-01-11 14:50:10 +01001197 MBEDTLS_SSL_DEBUG_MSG(3,
1198 ("ssl_tls13_pick_key_cert:"
1199 "check signature algorithm %s [%04x]",
1200 mbedtls_ssl_sig_alg_to_str(*sig_alg),
1201 *sig_alg));
Ronald Cron67ea2542022-09-15 17:34:42 +02001202#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01001203 psa_alg = ssl_tls13_iana_sig_alg_to_psa_alg(*sig_alg);
Ronald Cron67ea2542022-09-15 17:34:42 +02001204#endif /* MBEDTLS_USE_PSA_CRYPTO */
1205
Gilles Peskine449bd832023-01-11 14:50:10 +01001206 if (mbedtls_ssl_tls13_check_sig_alg_cert_key_match(
1207 *sig_alg, &key_cert->cert->pk)
Ronald Cron67ea2542022-09-15 17:34:42 +02001208#if defined(MBEDTLS_USE_PSA_CRYPTO)
1209 && psa_alg != PSA_ALG_NONE &&
Gilles Peskine449bd832023-01-11 14:50:10 +01001210 mbedtls_pk_can_do_ext(&key_cert->cert->pk, psa_alg,
1211 PSA_KEY_USAGE_SIGN_HASH) == 1
Ronald Cron67ea2542022-09-15 17:34:42 +02001212#endif /* MBEDTLS_USE_PSA_CRYPTO */
Gilles Peskine449bd832023-01-11 14:50:10 +01001213 ) {
XiaokangQianfb665a82022-06-15 03:57:21 +00001214 ssl->handshake->key_cert = key_cert;
Gilles Peskine449bd832023-01-11 14:50:10 +01001215 MBEDTLS_SSL_DEBUG_MSG(3,
1216 ("ssl_tls13_pick_key_cert:"
1217 "selected signature algorithm"
1218 " %s [%04x]",
1219 mbedtls_ssl_sig_alg_to_str(*sig_alg),
1220 *sig_alg));
XiaokangQianfb665a82022-06-15 03:57:21 +00001221 MBEDTLS_SSL_DEBUG_CRT(
Gilles Peskine449bd832023-01-11 14:50:10 +01001222 3, "selected certificate (chain)",
1223 ssl->handshake->key_cert->cert);
1224 return 0;
XiaokangQian81802f42022-06-10 13:25:22 +00001225 }
XiaokangQian81802f42022-06-10 13:25:22 +00001226 }
XiaokangQian23c5be62022-06-07 02:04:34 +00001227 }
1228
Gilles Peskine449bd832023-01-11 14:50:10 +01001229 MBEDTLS_SSL_DEBUG_MSG(2, ("ssl_tls13_pick_key_cert:"
1230 "no suitable certificate found"));
1231 return -1;
XiaokangQian23c5be62022-06-07 02:04:34 +00001232}
XiaokangQian81802f42022-06-10 13:25:22 +00001233#endif /* MBEDTLS_X509_CRT_PARSE_C &&
Ronald Cron928cbd32022-10-04 16:14:26 +02001234 MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED */
XiaokangQian23c5be62022-06-07 02:04:34 +00001235
XiaokangQian4080a7f2022-04-11 09:55:18 +00001236/*
XiaokangQianed582dd2022-04-13 08:21:05 +00001237 *
1238 * STATE HANDLING: ClientHello
1239 *
XiaokangQianb67384d2022-04-19 00:02:38 +00001240 * There are three possible classes of outcomes when parsing the ClientHello:
XiaokangQianed582dd2022-04-13 08:21:05 +00001241 *
XiaokangQianb67384d2022-04-19 00:02:38 +00001242 * 1) The ClientHello was well-formed and matched the server's configuration.
XiaokangQianed582dd2022-04-13 08:21:05 +00001243 *
1244 * In this case, the server progresses to sending its ServerHello.
1245 *
XiaokangQianb67384d2022-04-19 00:02:38 +00001246 * 2) The ClientHello was well-formed but didn't match the server's
1247 * configuration.
XiaokangQianed582dd2022-04-13 08:21:05 +00001248 *
1249 * For example, the client might not have offered a key share which
1250 * the server supports, or the server might require a cookie.
1251 *
1252 * In this case, the server sends a HelloRetryRequest.
1253 *
XiaokangQianb67384d2022-04-19 00:02:38 +00001254 * 3) The ClientHello was ill-formed
XiaokangQianed582dd2022-04-13 08:21:05 +00001255 *
1256 * In this case, we abort the handshake.
1257 *
1258 */
1259
1260/*
XiaokangQian4080a7f2022-04-11 09:55:18 +00001261 * Structure of this message:
1262 *
XiaokangQiane8ff3502022-04-22 02:34:40 +00001263 * uint16 ProtocolVersion;
1264 * opaque Random[32];
1265 * uint8 CipherSuite[2]; // Cryptographic suite selector
XiaokangQian4080a7f2022-04-11 09:55:18 +00001266 *
XiaokangQiane8ff3502022-04-22 02:34:40 +00001267 * struct {
1268 * ProtocolVersion legacy_version = 0x0303; // TLS v1.2
1269 * Random random;
1270 * opaque legacy_session_id<0..32>;
1271 * CipherSuite cipher_suites<2..2^16-2>;
1272 * opaque legacy_compression_methods<1..2^8-1>;
1273 * Extension extensions<8..2^16-1>;
1274 * } ClientHello;
XiaokangQian4080a7f2022-04-11 09:55:18 +00001275 */
XiaokangQianed582dd2022-04-13 08:21:05 +00001276
1277#define SSL_CLIENT_HELLO_OK 0
1278#define SSL_CLIENT_HELLO_HRR_REQUIRED 1
Ronald Cron5af4c7f2023-03-07 20:46:59 +01001279#define SSL_CLIENT_HELLO_TLS1_2 2
XiaokangQianed582dd2022-04-13 08:21:05 +00001280
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001281MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01001282static int ssl_tls13_parse_client_hello(mbedtls_ssl_context *ssl,
1283 const unsigned char *buf,
1284 const unsigned char *end)
XiaokangQian7807f9f2022-02-15 10:04:37 +00001285{
XiaokangQianb67384d2022-04-19 00:02:38 +00001286 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1287 const unsigned char *p = buf;
Ronald Crond540d992023-03-07 09:41:48 +01001288 const unsigned char *random;
XiaokangQian4080a7f2022-04-11 09:55:18 +00001289 size_t legacy_session_id_len;
Ronald Croncada4102023-03-07 09:51:39 +01001290 const unsigned char *legacy_session_id;
XiaokangQian318dc762022-04-20 09:43:51 +00001291 size_t cipher_suites_len;
Ronald Cron2f16b4e2023-03-07 10:07:32 +01001292 const unsigned char *cipher_suites;
XiaokangQian060d8672022-04-21 09:24:56 +00001293 const unsigned char *cipher_suites_end;
XiaokangQianb67384d2022-04-19 00:02:38 +00001294 size_t extensions_len;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001295 const unsigned char *extensions_end;
Ronald Croneff56732023-04-03 17:36:31 +02001296 const unsigned char *supported_versions_data;
1297 const unsigned char *supported_versions_data_end;
Jerry Yuc4bf5d62022-10-29 09:08:47 +08001298 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
Jerry Yu49ca9282022-05-05 11:05:22 +08001299 int hrr_required = 0;
Ronald Cron8a74f072023-06-14 17:59:29 +02001300 int no_usable_share_for_key_agreement = 0;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001301
Ronald Cron41a443a2022-10-04 16:38:25 +02001302#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED)
Jerry Yu29d9faa2022-08-23 17:52:45 +08001303 const unsigned char *pre_shared_key_ext = NULL;
Jerry Yu1c105562022-07-10 06:32:38 +00001304 const unsigned char *pre_shared_key_ext_end = NULL;
Ronald Cron41a443a2022-10-04 16:38:25 +02001305#endif
XiaokangQian7807f9f2022-02-15 10:04:37 +00001306
XiaokangQian7807f9f2022-02-15 10:04:37 +00001307 /*
XiaokangQianb67384d2022-04-19 00:02:38 +00001308 * ClientHello layout:
XiaokangQian7807f9f2022-02-15 10:04:37 +00001309 * 0 . 1 protocol version
XiaokangQiane8ff3502022-04-22 02:34:40 +00001310 * 2 . 33 random bytes
XiaokangQianc5763b52022-04-02 03:34:37 +00001311 * 34 . 34 session id length ( 1 byte )
XiaokangQian7807f9f2022-02-15 10:04:37 +00001312 * 35 . 34+x session id
XiaokangQian7807f9f2022-02-15 10:04:37 +00001313 * .. . .. ciphersuite list length ( 2 bytes )
1314 * .. . .. ciphersuite list
1315 * .. . .. compression alg. list length ( 1 byte )
1316 * .. . .. compression alg. list
1317 * .. . .. extensions length ( 2 bytes, optional )
1318 * .. . .. extensions ( optional )
1319 */
1320
XiaokangQianb67384d2022-04-19 00:02:38 +00001321 /*
bootstrap-prime6dbbf442022-05-17 19:30:44 -04001322 * Minimal length ( with everything empty and extensions omitted ) is
XiaokangQian7807f9f2022-02-15 10:04:37 +00001323 * 2 + 32 + 1 + 2 + 1 = 38 bytes. Check that first, so that we can
1324 * read at least up to session id length without worrying.
1325 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001326 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, 38);
XiaokangQian7807f9f2022-02-15 10:04:37 +00001327
1328 /* ...
1329 * ProtocolVersion legacy_version = 0x0303; // TLS 1.2
1330 * ...
1331 * with ProtocolVersion defined as:
1332 * uint16 ProtocolVersion;
1333 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001334 if (mbedtls_ssl_read_version(p, ssl->conf->transport) !=
1335 MBEDTLS_SSL_VERSION_TLS1_2) {
1336 MBEDTLS_SSL_DEBUG_MSG(1, ("Unsupported version of TLS."));
1337 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_PROTOCOL_VERSION,
1338 MBEDTLS_ERR_SSL_BAD_PROTOCOL_VERSION);
1339 return MBEDTLS_ERR_SSL_BAD_PROTOCOL_VERSION;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001340 }
1341 p += 2;
1342
Jerry Yuc1be19f2022-04-23 16:11:39 +08001343 /* ...
1344 * Random random;
1345 * ...
XiaokangQianb67384d2022-04-19 00:02:38 +00001346 * with Random defined as:
1347 * opaque Random[32];
XiaokangQian7807f9f2022-02-15 10:04:37 +00001348 */
Ronald Crond540d992023-03-07 09:41:48 +01001349 random = p;
XiaokangQian08037552022-04-20 07:16:41 +00001350 p += MBEDTLS_CLIENT_HELLO_RANDOM_LEN;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001351
Jerry Yuc1be19f2022-04-23 16:11:39 +08001352 /* ...
XiaokangQianb67384d2022-04-19 00:02:38 +00001353 * opaque legacy_session_id<0..32>;
Jerry Yuc1be19f2022-04-23 16:11:39 +08001354 * ...
XiaokangQian7807f9f2022-02-15 10:04:37 +00001355 */
Ronald Croncada4102023-03-07 09:51:39 +01001356 legacy_session_id_len = *(p++);
1357 legacy_session_id = p;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001358
XiaokangQianb67384d2022-04-19 00:02:38 +00001359 /*
1360 * Check we have enough data for the legacy session identifier
Jerry Yue95c8af2022-07-26 15:48:20 +08001361 * and the ciphersuite list length.
XiaokangQianb67384d2022-04-19 00:02:38 +00001362 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001363 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, legacy_session_id_len + 2);
XiaokangQian4080a7f2022-04-11 09:55:18 +00001364 p += legacy_session_id_len;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001365
Ronald Cron2f16b4e2023-03-07 10:07:32 +01001366 /* ...
1367 * CipherSuite cipher_suites<2..2^16-2>;
1368 * ...
1369 * with CipherSuite defined as:
1370 * uint8 CipherSuite[2];
1371 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001372 cipher_suites_len = MBEDTLS_GET_UINT16_BE(p, 0);
XiaokangQian7807f9f2022-02-15 10:04:37 +00001373 p += 2;
Ronald Cron2f16b4e2023-03-07 10:07:32 +01001374 cipher_suites = p;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001375
Ronald Cronfc7ae872023-02-16 15:32:19 +01001376 /*
1377 * The length of the ciphersuite list has to be even.
1378 */
1379 if (cipher_suites_len & 1) {
1380 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR,
1381 MBEDTLS_ERR_SSL_DECODE_ERROR);
1382 return MBEDTLS_ERR_SSL_DECODE_ERROR;
1383 }
1384
XiaokangQianb67384d2022-04-19 00:02:38 +00001385 /* Check we have enough data for the ciphersuite list, the legacy
1386 * compression methods and the length of the extensions.
Jerry Yue95c8af2022-07-26 15:48:20 +08001387 *
1388 * cipher_suites cipher_suites_len bytes
1389 * legacy_compression_methods 2 bytes
1390 * extensions_len 2 bytes
XiaokangQianb67384d2022-04-19 00:02:38 +00001391 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001392 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, cipher_suites_len + 2 + 2);
Ronald Cron8c527d02023-03-07 15:47:47 +01001393 p += cipher_suites_len;
1394 cipher_suites_end = p;
Jerry Yu5725f1c2022-08-21 17:27:16 +08001395
1396 /*
Ronald Cron8c527d02023-03-07 15:47:47 +01001397 * Search for the supported versions extension and parse it to determine
1398 * if the client supports TLS 1.3.
1399 */
1400 ret = mbedtls_ssl_tls13_is_supported_versions_ext_present_in_exts(
1401 ssl, p + 2, end,
Ronald Croneff56732023-04-03 17:36:31 +02001402 &supported_versions_data, &supported_versions_data_end);
Ronald Cron8c527d02023-03-07 15:47:47 +01001403 if (ret < 0) {
1404 MBEDTLS_SSL_DEBUG_RET(1,
1405 ("mbedtls_ssl_tls13_is_supported_versions_ext_present_in_exts"), ret);
1406 return ret;
1407 }
1408
1409 if (ret == 0) {
Ronald Cron5af4c7f2023-03-07 20:46:59 +01001410 return SSL_CLIENT_HELLO_TLS1_2;
Ronald Cron8c527d02023-03-07 15:47:47 +01001411 }
1412
Ronald Cron5af4c7f2023-03-07 20:46:59 +01001413 if (ret == 1) {
1414 ret = ssl_tls13_parse_supported_versions_ext(ssl,
Ronald Croneff56732023-04-03 17:36:31 +02001415 supported_versions_data,
1416 supported_versions_data_end);
Ronald Cron5af4c7f2023-03-07 20:46:59 +01001417 if (ret < 0) {
1418 MBEDTLS_SSL_DEBUG_RET(1,
1419 ("ssl_tls13_parse_supported_versions_ext"), ret);
1420 return ret;
1421 }
1422
Ronald Cronb828c7d2023-04-03 16:37:22 +02001423 /*
1424 * The supported versions extension was parsed successfully as the
1425 * value returned by ssl_tls13_parse_supported_versions_ext() is
1426 * positive. The return value is then equal to
1427 * MBEDTLS_SSL_VERSION_TLS1_2 or MBEDTLS_SSL_VERSION_TLS1_3, defining
1428 * the TLS version to negotiate.
1429 */
Ronald Cron5af4c7f2023-03-07 20:46:59 +01001430 if (MBEDTLS_SSL_VERSION_TLS1_2 == ret) {
1431 return SSL_CLIENT_HELLO_TLS1_2;
1432 }
Ronald Cron8c527d02023-03-07 15:47:47 +01001433 }
1434
1435 /*
1436 * We negotiate TLS 1.3.
Ronald Cron64582392023-03-07 09:21:40 +01001437 */
1438 ssl->tls_version = MBEDTLS_SSL_VERSION_TLS1_3;
1439
1440#if defined(MBEDTLS_SSL_SESSION_TICKETS)
1441 /* Store minor version for later use with ticket serialization. */
1442 ssl->session_negotiate->tls_version = MBEDTLS_SSL_VERSION_TLS1_3;
1443 ssl->session_negotiate->endpoint = ssl->conf->endpoint;
1444#endif
1445
1446 /*
Ronald Crondad02b22023-04-06 09:57:52 +02001447 * We are negotiating the version 1.3 of the protocol. Do what we have
Ronald Croncada4102023-03-07 09:51:39 +01001448 * postponed: copy of the client random bytes, copy of the legacy session
Ronald Cron2f16b4e2023-03-07 10:07:32 +01001449 * identifier and selection of the TLS 1.3 cipher suite.
Ronald Crond540d992023-03-07 09:41:48 +01001450 */
1451 MBEDTLS_SSL_DEBUG_BUF(3, "client hello, random bytes",
1452 random, MBEDTLS_CLIENT_HELLO_RANDOM_LEN);
1453 memcpy(&handshake->randbytes[0], random, MBEDTLS_CLIENT_HELLO_RANDOM_LEN);
1454
Ronald Croncada4102023-03-07 09:51:39 +01001455 if (legacy_session_id_len > sizeof(ssl->session_negotiate->id)) {
1456 MBEDTLS_SSL_DEBUG_MSG(1, ("bad client hello message"));
1457 return MBEDTLS_ERR_SSL_DECODE_ERROR;
1458 }
1459 ssl->session_negotiate->id_len = legacy_session_id_len;
1460 MBEDTLS_SSL_DEBUG_BUF(3, "client hello, session id",
1461 legacy_session_id, legacy_session_id_len);
1462 memcpy(&ssl->session_negotiate->id[0],
1463 legacy_session_id, legacy_session_id_len);
1464
Ronald Crond540d992023-03-07 09:41:48 +01001465 /*
Jerry Yu5725f1c2022-08-21 17:27:16 +08001466 * Search for a matching ciphersuite
1467 */
Ronald Cron2f16b4e2023-03-07 10:07:32 +01001468 MBEDTLS_SSL_DEBUG_BUF(3, "client hello, list of cipher suites",
1469 cipher_suites, cipher_suites_len);
Ronald Crone45afd72023-04-04 15:10:06 +02001470 for (const unsigned char *cipher_suites_p = cipher_suites;
1471 cipher_suites_p < cipher_suites_end; cipher_suites_p += 2) {
XiaokangQiane8ff3502022-04-22 02:34:40 +00001472 uint16_t cipher_suite;
Gilles Peskine449bd832023-01-11 14:50:10 +01001473 const mbedtls_ssl_ciphersuite_t *ciphersuite_info;
Jerry Yu5725f1c2022-08-21 17:27:16 +08001474
Ronald Crond89360b2023-02-21 08:53:33 +01001475 /*
Ronald Crone45afd72023-04-04 15:10:06 +02001476 * "cipher_suites_end - cipher_suites_p is even" is an invariant of the
1477 * loop. As cipher_suites_end - cipher_suites_p > 0, we have
1478 * cipher_suites_end - cipher_suites_p >= 2 and it is thus safe to read
1479 * two bytes.
Ronald Crond89360b2023-02-21 08:53:33 +01001480 */
Ronald Crone45afd72023-04-04 15:10:06 +02001481 cipher_suite = MBEDTLS_GET_UINT16_BE(cipher_suites_p, 0);
Jerry Yuc5a23a02022-08-25 10:51:44 +08001482 ciphersuite_info = ssl_tls13_validate_peer_ciphersuite(
Gilles Peskine449bd832023-01-11 14:50:10 +01001483 ssl, cipher_suite);
1484 if (ciphersuite_info == NULL) {
Jerry Yu5725f1c2022-08-21 17:27:16 +08001485 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +01001486 }
Jerry Yu5725f1c2022-08-21 17:27:16 +08001487
Jerry Yu5725f1c2022-08-21 17:27:16 +08001488 ssl->session_negotiate->ciphersuite = cipher_suite;
Jerry Yuc4bf5d62022-10-29 09:08:47 +08001489 handshake->ciphersuite_info = ciphersuite_info;
Gilles Peskine449bd832023-01-11 14:50:10 +01001490 MBEDTLS_SSL_DEBUG_MSG(2, ("selected ciphersuite: %04x - %s",
1491 cipher_suite,
1492 ciphersuite_info->name));
Ronald Cron25e9ec62023-02-16 15:35:16 +01001493 break;
XiaokangQian17f974c2022-04-19 09:57:41 +00001494 }
Jerry Yu5725f1c2022-08-21 17:27:16 +08001495
Gilles Peskine449bd832023-01-11 14:50:10 +01001496 if (handshake->ciphersuite_info == NULL) {
1497 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE,
1498 MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE);
1499 return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
Jerry Yu5725f1c2022-08-21 17:27:16 +08001500 }
Jerry Yuc1be19f2022-04-23 16:11:39 +08001501
XiaokangQian4080a7f2022-04-11 09:55:18 +00001502 /* ...
XiaokangQianb67384d2022-04-19 00:02:38 +00001503 * opaque legacy_compression_methods<1..2^8-1>;
XiaokangQian4080a7f2022-04-11 09:55:18 +00001504 * ...
XiaokangQian7807f9f2022-02-15 10:04:37 +00001505 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001506 if (p[0] != 1 || p[1] != MBEDTLS_SSL_COMPRESS_NULL) {
1507 MBEDTLS_SSL_DEBUG_MSG(1, ("bad legacy compression method"));
1508 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER,
1509 MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER);
1510 return MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001511 }
XiaokangQianb67384d2022-04-19 00:02:38 +00001512 p += 2;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001513
Jerry Yuc1be19f2022-04-23 16:11:39 +08001514 /* ...
XiaokangQianb67384d2022-04-19 00:02:38 +00001515 * Extension extensions<8..2^16-1>;
Jerry Yuc1be19f2022-04-23 16:11:39 +08001516 * ...
XiaokangQianb67384d2022-04-19 00:02:38 +00001517 * with Extension defined as:
1518 * struct {
1519 * ExtensionType extension_type;
1520 * opaque extension_data<0..2^16-1>;
1521 * } Extension;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001522 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001523 extensions_len = MBEDTLS_GET_UINT16_BE(p, 0);
XiaokangQian7807f9f2022-02-15 10:04:37 +00001524 p += 2;
Gilles Peskine449bd832023-01-11 14:50:10 +01001525 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, extensions_len);
XiaokangQiane8ff3502022-04-22 02:34:40 +00001526 extensions_end = p + extensions_len;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001527
Gilles Peskine449bd832023-01-11 14:50:10 +01001528 MBEDTLS_SSL_DEBUG_BUF(3, "client hello extensions", p, extensions_len);
Jerry Yu63a459c2022-10-31 13:38:40 +08001529 handshake->received_extensions = MBEDTLS_SSL_EXT_MASK_NONE;
Jerry Yu0c354a22022-08-29 15:25:36 +08001530
Gilles Peskine449bd832023-01-11 14:50:10 +01001531 while (p < extensions_end) {
XiaokangQian7807f9f2022-02-15 10:04:37 +00001532 unsigned int extension_type;
1533 size_t extension_data_len;
1534 const unsigned char *extension_data_end;
1535
Jerry Yu0c354a22022-08-29 15:25:36 +08001536 /* RFC 8446, section 4.2.11
Jerry Yu1c9247c2022-07-21 12:37:39 +08001537 *
1538 * The "pre_shared_key" extension MUST be the last extension in the
1539 * ClientHello (this facilitates implementation as described below).
1540 * Servers MUST check that it is the last extension and otherwise fail
1541 * the handshake with an "illegal_parameter" alert.
1542 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001543 if (handshake->received_extensions & MBEDTLS_SSL_EXT_MASK(PRE_SHARED_KEY)) {
Jerry Yu1c9247c2022-07-21 12:37:39 +08001544 MBEDTLS_SSL_DEBUG_MSG(
Gilles Peskine449bd832023-01-11 14:50:10 +01001545 3, ("pre_shared_key is not last extension."));
Jerry Yu1c9247c2022-07-21 12:37:39 +08001546 MBEDTLS_SSL_PEND_FATAL_ALERT(
1547 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER,
Gilles Peskine449bd832023-01-11 14:50:10 +01001548 MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER);
1549 return MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER;
Jerry Yu1c9247c2022-07-21 12:37:39 +08001550 }
1551
Gilles Peskine449bd832023-01-11 14:50:10 +01001552 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, extensions_end, 4);
1553 extension_type = MBEDTLS_GET_UINT16_BE(p, 0);
1554 extension_data_len = MBEDTLS_GET_UINT16_BE(p, 2);
XiaokangQian7807f9f2022-02-15 10:04:37 +00001555 p += 4;
1556
Gilles Peskine449bd832023-01-11 14:50:10 +01001557 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, extensions_end, extension_data_len);
XiaokangQian7807f9f2022-02-15 10:04:37 +00001558 extension_data_end = p + extension_data_len;
1559
Jerry Yuc4bf5d62022-10-29 09:08:47 +08001560 ret = mbedtls_ssl_tls13_check_received_extension(
Gilles Peskine449bd832023-01-11 14:50:10 +01001561 ssl, MBEDTLS_SSL_HS_CLIENT_HELLO, extension_type,
1562 MBEDTLS_SSL_TLS1_3_ALLOWED_EXTS_OF_CH);
1563 if (ret != 0) {
1564 return ret;
1565 }
Jerry Yue18dc7e2022-08-04 16:29:22 +08001566
Gilles Peskine449bd832023-01-11 14:50:10 +01001567 switch (extension_type) {
XiaokangQian40a35232022-05-07 09:02:40 +00001568#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
1569 case MBEDTLS_TLS_EXT_SERVERNAME:
Gilles Peskine449bd832023-01-11 14:50:10 +01001570 MBEDTLS_SSL_DEBUG_MSG(3, ("found ServerName extension"));
1571 ret = mbedtls_ssl_parse_server_name_ext(ssl, p,
1572 extension_data_end);
1573 if (ret != 0) {
XiaokangQian40a35232022-05-07 09:02:40 +00001574 MBEDTLS_SSL_DEBUG_RET(
Gilles Peskine449bd832023-01-11 14:50:10 +01001575 1, "mbedtls_ssl_parse_servername_ext", ret);
1576 return ret;
XiaokangQian40a35232022-05-07 09:02:40 +00001577 }
XiaokangQian40a35232022-05-07 09:02:40 +00001578 break;
1579#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
1580
Przemek Stekiel8c0a9532023-06-15 16:48:19 +02001581#if defined(PSA_WANT_ALG_ECDH) || defined(PSA_WANT_ALG_FFDH)
XiaokangQian7807f9f2022-02-15 10:04:37 +00001582 case MBEDTLS_TLS_EXT_SUPPORTED_GROUPS:
Gilles Peskine449bd832023-01-11 14:50:10 +01001583 MBEDTLS_SSL_DEBUG_MSG(3, ("found supported group extension"));
XiaokangQian7807f9f2022-02-15 10:04:37 +00001584
1585 /* Supported Groups Extension
1586 *
1587 * When sent by the client, the "supported_groups" extension
1588 * indicates the named groups which the client supports,
1589 * ordered from most preferred to least preferred.
1590 */
Jerry Yuc1be19f2022-04-23 16:11:39 +08001591 ret = ssl_tls13_parse_supported_groups_ext(
Gilles Peskine449bd832023-01-11 14:50:10 +01001592 ssl, p, extension_data_end);
1593 if (ret != 0) {
Xiaokang Qian9f1747b2023-03-30 02:50:04 +00001594 MBEDTLS_SSL_DEBUG_RET(
Xiaokang Qian8bce0e62023-04-04 10:15:48 +00001595 1, "ssl_tls13_parse_supported_groups_ext", ret);
Gilles Peskine449bd832023-01-11 14:50:10 +01001596 return ret;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001597 }
1598
XiaokangQian7807f9f2022-02-15 10:04:37 +00001599 break;
Przemek Stekiel8c0a9532023-06-15 16:48:19 +02001600#endif /* PSA_WANT_ALG_ECDH || PSA_WANT_ALG_FFDH*/
XiaokangQian7807f9f2022-02-15 10:04:37 +00001601
Valerio Settic9ae8622023-07-25 11:23:50 +02001602#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_EPHEMERAL_ENABLED)
XiaokangQian7807f9f2022-02-15 10:04:37 +00001603 case MBEDTLS_TLS_EXT_KEY_SHARE:
Gilles Peskine449bd832023-01-11 14:50:10 +01001604 MBEDTLS_SSL_DEBUG_MSG(3, ("found key share extension"));
XiaokangQian7807f9f2022-02-15 10:04:37 +00001605
1606 /*
1607 * Key Share Extension
1608 *
1609 * When sent by the client, the "key_share" extension
1610 * contains the endpoint's cryptographic parameters for
1611 * ECDHE/DHE key establishment methods.
1612 */
Jerry Yuc1be19f2022-04-23 16:11:39 +08001613 ret = ssl_tls13_parse_key_shares_ext(
Gilles Peskine449bd832023-01-11 14:50:10 +01001614 ssl, p, extension_data_end);
1615 if (ret == SSL_TLS1_3_PARSE_KEY_SHARES_EXT_NO_MATCH) {
Ronald Cron8a74f072023-06-14 17:59:29 +02001616 MBEDTLS_SSL_DEBUG_MSG(2, ("No usable share for key agreement."));
1617 no_usable_share_for_key_agreement = 1;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001618 }
1619
Gilles Peskine449bd832023-01-11 14:50:10 +01001620 if (ret < 0) {
Jerry Yu582dd062022-04-22 21:59:01 +08001621 MBEDTLS_SSL_DEBUG_RET(
Gilles Peskine449bd832023-01-11 14:50:10 +01001622 1, "ssl_tls13_parse_key_shares_ext", ret);
1623 return ret;
Jerry Yu582dd062022-04-22 21:59:01 +08001624 }
XiaokangQian7807f9f2022-02-15 10:04:37 +00001625
XiaokangQian7807f9f2022-02-15 10:04:37 +00001626 break;
Valerio Settic9ae8622023-07-25 11:23:50 +02001627#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_EPHEMERAL_ENABLED */
XiaokangQian7807f9f2022-02-15 10:04:37 +00001628
1629 case MBEDTLS_TLS_EXT_SUPPORTED_VERSIONS:
Ronald Cron8c527d02023-03-07 15:47:47 +01001630 /* Already parsed */
XiaokangQian7807f9f2022-02-15 10:04:37 +00001631 break;
1632
Ronald Cron41a443a2022-10-04 16:38:25 +02001633#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED)
Jerry Yue19e3b92022-07-08 12:04:51 +00001634 case MBEDTLS_TLS_EXT_PSK_KEY_EXCHANGE_MODES:
Xiaokang Qian9f1747b2023-03-30 02:50:04 +00001635 MBEDTLS_SSL_DEBUG_MSG(
1636 3, ("found psk key exchange modes extension"));
Jerry Yue19e3b92022-07-08 12:04:51 +00001637
1638 ret = ssl_tls13_parse_key_exchange_modes_ext(
Gilles Peskine449bd832023-01-11 14:50:10 +01001639 ssl, p, extension_data_end);
1640 if (ret != 0) {
Jerry Yue19e3b92022-07-08 12:04:51 +00001641 MBEDTLS_SSL_DEBUG_RET(
Gilles Peskine449bd832023-01-11 14:50:10 +01001642 1, "ssl_tls13_parse_key_exchange_modes_ext", ret);
1643 return ret;
Jerry Yue19e3b92022-07-08 12:04:51 +00001644 }
1645
Jerry Yue19e3b92022-07-08 12:04:51 +00001646 break;
Ronald Cron41a443a2022-10-04 16:38:25 +02001647#endif
Jerry Yue19e3b92022-07-08 12:04:51 +00001648
Jerry Yu1c105562022-07-10 06:32:38 +00001649 case MBEDTLS_TLS_EXT_PRE_SHARED_KEY:
Gilles Peskine449bd832023-01-11 14:50:10 +01001650 MBEDTLS_SSL_DEBUG_MSG(3, ("found pre_shared_key extension"));
1651 if ((handshake->received_extensions &
1652 MBEDTLS_SSL_EXT_MASK(PSK_KEY_EXCHANGE_MODES)) == 0) {
Jerry Yu13ab81d2022-07-22 23:17:11 +08001653 MBEDTLS_SSL_PEND_FATAL_ALERT(
1654 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER,
Gilles Peskine449bd832023-01-11 14:50:10 +01001655 MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER);
1656 return MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER;
Jerry Yu13ab81d2022-07-22 23:17:11 +08001657 }
Ronald Cron41a443a2022-10-04 16:38:25 +02001658#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED)
Jerry Yu1c105562022-07-10 06:32:38 +00001659 /* Delay processing of the PSK identity once we have
1660 * found out which algorithms to use. We keep a pointer
1661 * to the buffer and the size for later processing.
1662 */
Jerry Yu29d9faa2022-08-23 17:52:45 +08001663 pre_shared_key_ext = p;
Jerry Yu1c105562022-07-10 06:32:38 +00001664 pre_shared_key_ext_end = extension_data_end;
Jerry Yue18dc7e2022-08-04 16:29:22 +08001665#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED */
Jerry Yu1c105562022-07-10 06:32:38 +00001666 break;
Jerry Yu1c105562022-07-10 06:32:38 +00001667
XiaokangQianacb39922022-06-17 10:18:48 +00001668#if defined(MBEDTLS_SSL_ALPN)
1669 case MBEDTLS_TLS_EXT_ALPN:
Gilles Peskine449bd832023-01-11 14:50:10 +01001670 MBEDTLS_SSL_DEBUG_MSG(3, ("found alpn extension"));
XiaokangQianacb39922022-06-17 10:18:48 +00001671
Gilles Peskine449bd832023-01-11 14:50:10 +01001672 ret = mbedtls_ssl_parse_alpn_ext(ssl, p, extension_data_end);
1673 if (ret != 0) {
XiaokangQianacb39922022-06-17 10:18:48 +00001674 MBEDTLS_SSL_DEBUG_RET(
Gilles Peskine449bd832023-01-11 14:50:10 +01001675 1, ("mbedtls_ssl_parse_alpn_ext"), ret);
1676 return ret;
XiaokangQianacb39922022-06-17 10:18:48 +00001677 }
XiaokangQianacb39922022-06-17 10:18:48 +00001678 break;
1679#endif /* MBEDTLS_SSL_ALPN */
1680
Ronald Cron928cbd32022-10-04 16:14:26 +02001681#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED)
XiaokangQian7807f9f2022-02-15 10:04:37 +00001682 case MBEDTLS_TLS_EXT_SIG_ALG:
Gilles Peskine449bd832023-01-11 14:50:10 +01001683 MBEDTLS_SSL_DEBUG_MSG(3, ("found signature_algorithms extension"));
XiaokangQian7807f9f2022-02-15 10:04:37 +00001684
Gabor Mezei078e8032022-04-27 21:17:56 +02001685 ret = mbedtls_ssl_parse_sig_alg_ext(
Gilles Peskine449bd832023-01-11 14:50:10 +01001686 ssl, p, extension_data_end);
1687 if (ret != 0) {
Xiaokang Qian49f39c12023-04-06 02:31:02 +00001688 MBEDTLS_SSL_DEBUG_RET(
1689 1, "mbedtls_ssl_parse_sig_alg_ext", ret);
Gilles Peskine449bd832023-01-11 14:50:10 +01001690 return ret;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001691 }
XiaokangQian7807f9f2022-02-15 10:04:37 +00001692 break;
Ronald Cron928cbd32022-10-04 16:14:26 +02001693#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED */
XiaokangQian7807f9f2022-02-15 10:04:37 +00001694
Jan Bruckner151f6422023-02-10 12:45:19 +01001695#if defined(MBEDTLS_SSL_RECORD_SIZE_LIMIT)
1696 case MBEDTLS_TLS_EXT_RECORD_SIZE_LIMIT:
1697 MBEDTLS_SSL_DEBUG_MSG(3, ("found record_size_limit extension"));
1698
Xiaokang Qian9f1747b2023-03-30 02:50:04 +00001699 ret = mbedtls_ssl_tls13_parse_record_size_limit_ext(
1700 ssl, p, extension_data_end);
Jan Bruckner151f6422023-02-10 12:45:19 +01001701
Xiaokang Qian09c3ccc2023-04-04 10:18:38 +00001702 /*
1703 * TODO: Return unconditionally here until we handle the record
1704 * size limit correctly.
1705 * Once handled correctly, only return in case of errors.
1706 */
Jan Bruckner151f6422023-02-10 12:45:19 +01001707 return ret;
1708
1709 break;
1710#endif /* MBEDTLS_SSL_RECORD_SIZE_LIMIT */
1711
XiaokangQian7807f9f2022-02-15 10:04:37 +00001712 default:
Jerry Yu79aa7212022-11-08 21:30:21 +08001713 MBEDTLS_SSL_PRINT_EXT(
Jerry Yu63a459c2022-10-31 13:38:40 +08001714 3, MBEDTLS_SSL_HS_CLIENT_HELLO,
Gilles Peskine449bd832023-01-11 14:50:10 +01001715 extension_type, "( ignored )");
Jerry Yu0c354a22022-08-29 15:25:36 +08001716 break;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001717 }
1718
1719 p += extension_data_len;
1720 }
1721
Gilles Peskine449bd832023-01-11 14:50:10 +01001722 MBEDTLS_SSL_PRINT_EXTS(3, MBEDTLS_SSL_HS_CLIENT_HELLO,
1723 handshake->received_extensions);
Jerry Yue18dc7e2022-08-04 16:29:22 +08001724
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +01001725 ret = mbedtls_ssl_add_hs_hdr_to_checksum(ssl,
1726 MBEDTLS_SSL_HS_CLIENT_HELLO,
1727 p - buf);
1728 if (0 != ret) {
1729 MBEDTLS_SSL_DEBUG_RET(1, ("mbedtls_ssl_add_hs_hdr_to_checksum"), ret);
1730 return ret;
1731 }
Jerry Yu032b15ce2022-07-11 06:10:03 +00001732
Ronald Cron41a443a2022-10-04 16:38:25 +02001733#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED)
XiaokangQian7807f9f2022-02-15 10:04:37 +00001734 /* Update checksum with either
1735 * - The entire content of the CH message, if no PSK extension is present
1736 * - The content up to but excluding the PSK extension, if present.
1737 */
Jerry Yu1c105562022-07-10 06:32:38 +00001738 /* If we've settled on a PSK-based exchange, parse PSK identity ext */
Pengyu Lvcfb23b82023-10-30 15:26:26 +08001739 if (ssl_tls13_check_psk_key_exchange(ssl) ||
1740 ssl_tls13_check_psk_ephemeral_key_exchange(ssl)) {
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +01001741 ret = handshake->update_checksum(ssl, buf,
1742 pre_shared_key_ext - buf);
1743 if (0 != ret) {
1744 MBEDTLS_SSL_DEBUG_RET(1, ("update_checksum"), ret);
1745 return ret;
1746 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001747 ret = ssl_tls13_parse_pre_shared_key_ext(ssl,
1748 pre_shared_key_ext,
1749 pre_shared_key_ext_end,
1750 cipher_suites,
1751 cipher_suites_end);
1752 if (ret == MBEDTLS_ERR_SSL_UNKNOWN_IDENTITY) {
1753 handshake->received_extensions &= ~MBEDTLS_SSL_EXT_MASK(PRE_SHARED_KEY);
1754 } else if (ret != 0) {
Jerry Yu63a459c2022-10-31 13:38:40 +08001755 MBEDTLS_SSL_DEBUG_RET(
Gilles Peskine449bd832023-01-11 14:50:10 +01001756 1, "ssl_tls13_parse_pre_shared_key_ext", ret);
1757 return ret;
Jerry Yu1c105562022-07-10 06:32:38 +00001758 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001759 } else
Ronald Cron41a443a2022-10-04 16:38:25 +02001760#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED */
Jerry Yu1c105562022-07-10 06:32:38 +00001761 {
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +01001762 ret = handshake->update_checksum(ssl, buf, p - buf);
1763 if (0 != ret) {
1764 MBEDTLS_SSL_DEBUG_RET(1, ("update_checksum"), ret);
1765 return ret;
1766 }
Jerry Yu1c105562022-07-10 06:32:38 +00001767 }
XiaokangQian7807f9f2022-02-15 10:04:37 +00001768
Gilles Peskine449bd832023-01-11 14:50:10 +01001769 ret = ssl_tls13_determine_key_exchange_mode(ssl);
1770 if (ret < 0) {
1771 return ret;
1772 }
XiaokangQian7807f9f2022-02-15 10:04:37 +00001773
Ronald Cron8a74f072023-06-14 17:59:29 +02001774 if (ssl->handshake->key_exchange_mode !=
1775 MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK) {
1776 hrr_required = (no_usable_share_for_key_agreement != 0);
1777 }
1778
Gilles Peskine449bd832023-01-11 14:50:10 +01001779 mbedtls_ssl_optimize_checksum(ssl, handshake->ciphersuite_info);
Jerry Yue5834fd2022-08-29 20:16:09 +08001780
Gilles Peskine449bd832023-01-11 14:50:10 +01001781 return hrr_required ? SSL_CLIENT_HELLO_HRR_REQUIRED : SSL_CLIENT_HELLO_OK;
XiaokangQian75fe8c72022-06-15 09:42:45 +00001782}
1783
Jerry Yuab0da372023-02-08 13:55:24 +08001784#if defined(MBEDTLS_SSL_EARLY_DATA)
1785static void ssl_tls13_update_early_data_status(mbedtls_ssl_context *ssl)
1786{
1787 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
1788
1789 if ((handshake->received_extensions &
1790 MBEDTLS_SSL_EXT_MASK(EARLY_DATA)) == 0) {
1791 MBEDTLS_SSL_DEBUG_MSG(
Jerry Yub47b2992023-10-18 15:50:35 +08001792 1, ("EarlyData: no early data extension received."));
Jerry Yuab0da372023-02-08 13:55:24 +08001793 ssl->early_data_status = MBEDTLS_SSL_EARLY_DATA_STATUS_NOT_RECEIVED;
1794 return;
1795 }
1796
1797 ssl->early_data_status = MBEDTLS_SSL_EARLY_DATA_STATUS_REJECTED;
1798
Jerry Yu985c9672022-12-04 14:06:30 +08001799 if (ssl->conf->early_data_enabled == MBEDTLS_SSL_EARLY_DATA_DISABLED) {
1800 MBEDTLS_SSL_DEBUG_MSG(
Jerry Yu985c9672022-12-04 14:06:30 +08001801 1,
Jerry Yu454dda32023-10-31 15:13:54 +08001802 ("EarlyData: rejected, feature disabled in server configuration."));
Jerry Yu985c9672022-12-04 14:06:30 +08001803 return;
1804 }
1805
Jerry Yu454dda32023-10-31 15:13:54 +08001806 if (!handshake->resume) {
1807 /* We currently support early data only in the case of PSKs established
1808 via a NewSessionTicket message thus in the case of a session
1809 resumption. */
Jerry Yu985c9672022-12-04 14:06:30 +08001810 MBEDTLS_SSL_DEBUG_MSG(
Jerry Yu7ef9fd82023-11-07 14:30:38 +08001811 1, ("EarlyData: rejected, not a session resumption."));
Jerry Yu985c9672022-12-04 14:06:30 +08001812 return;
1813 }
1814
Jerry Yu82fd6c12023-10-31 16:32:19 +08001815 /* RFC 8446 4.2.10
1816 *
1817 * In order to accept early data, the server MUST have accepted a PSK cipher
1818 * suite and selected the first key offered in the client's "pre_shared_key"
1819 * extension. In addition, it MUST verify that the following values are the
1820 * same as those associated with the selected PSK:
1821 * - The TLS version number
1822 * - The selected cipher suite
1823 * - The selected ALPN [RFC7301] protocol, if any
1824 *
1825 * NOTE:
Jerry Yu7ef9fd82023-11-07 14:30:38 +08001826 * - The TLS version number is checked in
1827 * ssl_tls13_offered_psks_check_identity_match_ticket().
1828 * - ALPN is not checked for the time being (TODO).
Jerry Yu82fd6c12023-10-31 16:32:19 +08001829 */
1830
1831 if (handshake->selected_identity != 0) {
1832 MBEDTLS_SSL_DEBUG_MSG(
Jerry Yu7ef9fd82023-11-07 14:30:38 +08001833 1, ("EarlyData: rejected, the selected key in "
1834 "`pre_shared_key` is not the first one."));
Jerry Yu82fd6c12023-10-31 16:32:19 +08001835 return;
1836 }
1837
1838 if (handshake->ciphersuite_info->id !=
1839 ssl->session_negotiate->ciphersuite) {
1840 MBEDTLS_SSL_DEBUG_MSG(
Jerry Yu7ef9fd82023-11-07 14:30:38 +08001841 1, ("EarlyData: rejected, the selected ciphersuite is not the one "
1842 "of the selected pre-shared key."));
Jerry Yu82fd6c12023-10-31 16:32:19 +08001843 return;
1844
1845 }
Jerry Yu985c9672022-12-04 14:06:30 +08001846
Jerry Yu985c9672022-12-04 14:06:30 +08001847
1848 ssl->early_data_status = MBEDTLS_SSL_EARLY_DATA_STATUS_ACCEPTED;
1849
Jerry Yuab0da372023-02-08 13:55:24 +08001850}
1851#endif /* MBEDTLS_SSL_EARLY_DATA */
1852
XiaokangQian75fe8c72022-06-15 09:42:45 +00001853/* Update the handshake state machine */
1854
Ronald Cronce7d76e2022-07-08 18:56:49 +02001855MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01001856static int ssl_tls13_postprocess_client_hello(mbedtls_ssl_context *ssl)
XiaokangQian75fe8c72022-06-15 09:42:45 +00001857{
1858 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1859
XiaokangQian7807f9f2022-02-15 10:04:37 +00001860 /*
XiaokangQianfb665a82022-06-15 03:57:21 +00001861 * Server certificate selection
1862 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001863 if (ssl->conf->f_cert_cb && (ret = ssl->conf->f_cert_cb(ssl)) != 0) {
1864 MBEDTLS_SSL_DEBUG_RET(1, "f_cert_cb", ret);
1865 return ret;
XiaokangQianfb665a82022-06-15 03:57:21 +00001866 }
1867#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
1868 ssl->handshake->sni_name = NULL;
1869 ssl->handshake->sni_name_len = 0;
1870#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
XiaokangQian7807f9f2022-02-15 10:04:37 +00001871
Gilles Peskine449bd832023-01-11 14:50:10 +01001872 ret = mbedtls_ssl_tls13_key_schedule_stage_early(ssl);
1873 if (ret != 0) {
1874 MBEDTLS_SSL_DEBUG_RET(1,
1875 "mbedtls_ssl_tls1_3_key_schedule_stage_early", ret);
1876 return ret;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001877 }
1878
Jerry Yuab0da372023-02-08 13:55:24 +08001879#if defined(MBEDTLS_SSL_EARLY_DATA)
1880 /* There is enough information, update early data state. */
1881 ssl_tls13_update_early_data_status(ssl);
Jerry Yuab0da372023-02-08 13:55:24 +08001882#endif /* MBEDTLS_SSL_EARLY_DATA */
1883
Gilles Peskine449bd832023-01-11 14:50:10 +01001884 return 0;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001885
1886}
1887
1888/*
XiaokangQianed582dd2022-04-13 08:21:05 +00001889 * Main entry point from the state machine; orchestrates the otherfunctions.
1890 */
1891
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001892MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01001893static int ssl_tls13_process_client_hello(mbedtls_ssl_context *ssl)
XiaokangQianed582dd2022-04-13 08:21:05 +00001894{
1895
XiaokangQian08037552022-04-20 07:16:41 +00001896 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine449bd832023-01-11 14:50:10 +01001897 unsigned char *buf = NULL;
XiaokangQianed582dd2022-04-13 08:21:05 +00001898 size_t buflen = 0;
Jerry Yu4ca91402022-05-09 15:50:57 +08001899 int parse_client_hello_ret;
1900
Gilles Peskine449bd832023-01-11 14:50:10 +01001901 MBEDTLS_SSL_DEBUG_MSG(2, ("=> parse client hello"));
XiaokangQianed582dd2022-04-13 08:21:05 +00001902
Gilles Peskine449bd832023-01-11 14:50:10 +01001903 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_tls13_fetch_handshake_msg(
1904 ssl, MBEDTLS_SSL_HS_CLIENT_HELLO,
1905 &buf, &buflen));
XiaokangQianed582dd2022-04-13 08:21:05 +00001906
Gilles Peskine449bd832023-01-11 14:50:10 +01001907 MBEDTLS_SSL_PROC_CHK_NEG(ssl_tls13_parse_client_hello(ssl, buf,
1908 buf + buflen));
Ronald Cron5af4c7f2023-03-07 20:46:59 +01001909 parse_client_hello_ret = ret; /* Store positive return value of
1910 * parse_client_hello,
1911 * as negative error codes are handled
Jerry Yuf41553b2022-05-09 22:20:30 +08001912 * by MBEDTLS_SSL_PROC_CHK_NEG. */
Jerry Yu4ca91402022-05-09 15:50:57 +08001913
Ronald Cronb828c7d2023-04-03 16:37:22 +02001914 /*
1915 * Version 1.2 of the protocol has been chosen, set the
1916 * ssl->keep_current_message flag for the ClientHello to be kept and parsed
1917 * as a TLS 1.2 ClientHello. We also change ssl->tls_version to
1918 * MBEDTLS_SSL_VERSION_TLS1_2 thus from now on mbedtls_ssl_handshake_step()
1919 * will dispatch to the TLS 1.2 state machine.
1920 */
Ronald Cron5af4c7f2023-03-07 20:46:59 +01001921 if (SSL_CLIENT_HELLO_TLS1_2 == parse_client_hello_ret) {
1922 ssl->keep_current_message = 1;
1923 ssl->tls_version = MBEDTLS_SSL_VERSION_TLS1_2;
1924 return 0;
1925 }
1926
Gilles Peskine449bd832023-01-11 14:50:10 +01001927 MBEDTLS_SSL_PROC_CHK(ssl_tls13_postprocess_client_hello(ssl));
Jerry Yu582dd062022-04-22 21:59:01 +08001928
Ronald Cron5af4c7f2023-03-07 20:46:59 +01001929 if (SSL_CLIENT_HELLO_OK == parse_client_hello_ret) {
Gilles Peskine449bd832023-01-11 14:50:10 +01001930 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_SERVER_HELLO);
1931 } else {
1932 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_HELLO_RETRY_REQUEST);
1933 }
XiaokangQianed582dd2022-04-13 08:21:05 +00001934
1935cleanup:
1936
Gilles Peskine449bd832023-01-11 14:50:10 +01001937 MBEDTLS_SSL_DEBUG_MSG(2, ("<= parse client hello"));
1938 return ret;
XiaokangQianed582dd2022-04-13 08:21:05 +00001939}
1940
1941/*
Jerry Yu1c3e6882022-04-20 21:23:40 +08001942 * Handler for MBEDTLS_SSL_SERVER_HELLO
Jerry Yu5b64ae92022-03-30 17:15:02 +08001943 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001944MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01001945static int ssl_tls13_prepare_server_hello(mbedtls_ssl_context *ssl)
Jerry Yuf4b27e42022-03-30 17:32:21 +08001946{
Jerry Yu637a3f12022-04-20 21:37:58 +08001947 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1948 unsigned char *server_randbytes =
Gilles Peskine449bd832023-01-11 14:50:10 +01001949 ssl->handshake->randbytes + MBEDTLS_CLIENT_HELLO_RANDOM_LEN;
1950 if (ssl->conf->f_rng == NULL) {
1951 MBEDTLS_SSL_DEBUG_MSG(1, ("no RNG provided"));
1952 return MBEDTLS_ERR_SSL_NO_RNG;
Jerry Yuf4b27e42022-03-30 17:32:21 +08001953 }
1954
Gilles Peskine449bd832023-01-11 14:50:10 +01001955 if ((ret = ssl->conf->f_rng(ssl->conf->p_rng, server_randbytes,
1956 MBEDTLS_SERVER_HELLO_RANDOM_LEN)) != 0) {
1957 MBEDTLS_SSL_DEBUG_RET(1, "f_rng", ret);
1958 return ret;
Jerry Yuf4b27e42022-03-30 17:32:21 +08001959 }
1960
Gilles Peskine449bd832023-01-11 14:50:10 +01001961 MBEDTLS_SSL_DEBUG_BUF(3, "server hello, random bytes", server_randbytes,
1962 MBEDTLS_SERVER_HELLO_RANDOM_LEN);
Jerry Yuf4b27e42022-03-30 17:32:21 +08001963
1964#if defined(MBEDTLS_HAVE_TIME)
YxCda609132023-05-22 12:08:12 -07001965 ssl->session_negotiate->start = mbedtls_time(NULL);
Jerry Yuf4b27e42022-03-30 17:32:21 +08001966#endif /* MBEDTLS_HAVE_TIME */
1967
Gilles Peskine449bd832023-01-11 14:50:10 +01001968 return ret;
Jerry Yuf4b27e42022-03-30 17:32:21 +08001969}
1970
Jerry Yu3bf2c642022-03-30 22:02:12 +08001971/*
Jerry Yue74e04a2022-04-21 09:23:16 +08001972 * ssl_tls13_write_server_hello_supported_versions_ext ():
Jerry Yu3bf2c642022-03-30 22:02:12 +08001973 *
1974 * struct {
Jerry Yufb9f54d2022-04-06 10:08:34 +08001975 * ProtocolVersion selected_version;
Jerry Yu3bf2c642022-03-30 22:02:12 +08001976 * } SupportedVersions;
1977 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001978MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yue74e04a2022-04-21 09:23:16 +08001979static int ssl_tls13_write_server_hello_supported_versions_ext(
Gilles Peskine449bd832023-01-11 14:50:10 +01001980 mbedtls_ssl_context *ssl,
1981 unsigned char *buf,
1982 unsigned char *end,
1983 size_t *out_len)
Jerry Yu3bf2c642022-03-30 22:02:12 +08001984{
Jerry Yu3bf2c642022-03-30 22:02:12 +08001985 *out_len = 0;
1986
Gilles Peskine449bd832023-01-11 14:50:10 +01001987 MBEDTLS_SSL_DEBUG_MSG(3, ("server hello, write selected version"));
Jerry Yu3bf2c642022-03-30 22:02:12 +08001988
1989 /* Check if we have space to write the extension:
1990 * - extension_type (2 bytes)
1991 * - extension_data_length (2 bytes)
Jerry Yu349a6132022-04-14 20:52:56 +08001992 * - selected_version (2 bytes)
Jerry Yu3bf2c642022-03-30 22:02:12 +08001993 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001994 MBEDTLS_SSL_CHK_BUF_PTR(buf, end, 6);
Jerry Yu3bf2c642022-03-30 22:02:12 +08001995
Gilles Peskine449bd832023-01-11 14:50:10 +01001996 MBEDTLS_PUT_UINT16_BE(MBEDTLS_TLS_EXT_SUPPORTED_VERSIONS, buf, 0);
Jerry Yu3bf2c642022-03-30 22:02:12 +08001997
Gilles Peskine449bd832023-01-11 14:50:10 +01001998 MBEDTLS_PUT_UINT16_BE(2, buf, 2);
Jerry Yu3bf2c642022-03-30 22:02:12 +08001999
Gilles Peskine449bd832023-01-11 14:50:10 +01002000 mbedtls_ssl_write_version(buf + 4,
2001 ssl->conf->transport,
2002 ssl->tls_version);
Jerry Yu3bf2c642022-03-30 22:02:12 +08002003
Gilles Peskine449bd832023-01-11 14:50:10 +01002004 MBEDTLS_SSL_DEBUG_MSG(3, ("supported version: [%04x]",
2005 ssl->tls_version));
Jerry Yu3bf2c642022-03-30 22:02:12 +08002006
Jerry Yu349a6132022-04-14 20:52:56 +08002007 *out_len = 6;
Jerry Yu3bf2c642022-03-30 22:02:12 +08002008
Jerry Yub95dd362022-11-08 21:19:34 +08002009 mbedtls_ssl_tls13_set_hs_sent_ext_mask(
Gilles Peskine449bd832023-01-11 14:50:10 +01002010 ssl, MBEDTLS_TLS_EXT_SUPPORTED_VERSIONS);
Jerry Yub95dd362022-11-08 21:19:34 +08002011
Gilles Peskine449bd832023-01-11 14:50:10 +01002012 return 0;
Jerry Yu3bf2c642022-03-30 22:02:12 +08002013}
2014
Jerry Yud9436a12022-04-20 22:28:09 +08002015
Jerry Yu3bf2c642022-03-30 22:02:12 +08002016
2017/* Generate and export a single key share. For hybrid KEMs, this can
2018 * be called multiple times with the different components of the hybrid. */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002019MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002020static int ssl_tls13_generate_and_write_key_share(mbedtls_ssl_context *ssl,
2021 uint16_t named_group,
2022 unsigned char *buf,
2023 unsigned char *end,
2024 size_t *out_len)
Jerry Yu3bf2c642022-03-30 22:02:12 +08002025{
2026 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yu955ddd72022-04-22 22:27:33 +08002027
2028 *out_len = 0;
2029
Valerio Settic9ae8622023-07-25 11:23:50 +02002030#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_EPHEMERAL_ENABLED)
Przemek Stekiel29c219c2023-05-31 15:21:04 +02002031 if (mbedtls_ssl_tls13_named_group_is_ecdhe(named_group) ||
Przemek Stekield5f79e72023-06-29 09:08:43 +02002032 mbedtls_ssl_tls13_named_group_is_ffdh(named_group)) {
Przemek Stekiel408569f2023-07-06 11:26:44 +02002033 ret = mbedtls_ssl_tls13_generate_and_write_xxdh_key_exchange(
Gilles Peskine449bd832023-01-11 14:50:10 +01002034 ssl, named_group, buf, end, out_len);
2035 if (ret != 0) {
Jerry Yu89e103c2022-03-30 22:43:29 +08002036 MBEDTLS_SSL_DEBUG_RET(
Przemek Stekiel408569f2023-07-06 11:26:44 +02002037 1, "mbedtls_ssl_tls13_generate_and_write_xxdh_key_exchange",
Gilles Peskine449bd832023-01-11 14:50:10 +01002038 ret);
2039 return ret;
Jerry Yu3bf2c642022-03-30 22:02:12 +08002040 }
Gilles Peskine449bd832023-01-11 14:50:10 +01002041 } else
Valerio Settic9ae8622023-07-25 11:23:50 +02002042#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_EPHEMERAL_ENABLED */
Gilles Peskine449bd832023-01-11 14:50:10 +01002043 if (0 /* Other kinds of KEMs */) {
2044 } else {
Jerry Yu955ddd72022-04-22 22:27:33 +08002045 ((void) ssl);
2046 ((void) named_group);
2047 ((void) buf);
2048 ((void) end);
Jerry Yu3bf2c642022-03-30 22:02:12 +08002049 ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
2050 }
2051
Gilles Peskine449bd832023-01-11 14:50:10 +01002052 return ret;
Jerry Yu3bf2c642022-03-30 22:02:12 +08002053}
2054
2055/*
2056 * ssl_tls13_write_key_share_ext
2057 *
2058 * Structure of key_share extension in ServerHello:
2059 *
Jerry Yu1c3e6882022-04-20 21:23:40 +08002060 * struct {
2061 * NamedGroup group;
2062 * opaque key_exchange<1..2^16-1>;
2063 * } KeyShareEntry;
2064 * struct {
2065 * KeyShareEntry server_share;
2066 * } KeyShareServerHello;
Jerry Yu3bf2c642022-03-30 22:02:12 +08002067 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002068MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002069static int ssl_tls13_write_key_share_ext(mbedtls_ssl_context *ssl,
2070 unsigned char *buf,
2071 unsigned char *end,
2072 size_t *out_len)
Jerry Yu3bf2c642022-03-30 22:02:12 +08002073{
Jerry Yu955ddd72022-04-22 22:27:33 +08002074 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yu3bf2c642022-03-30 22:02:12 +08002075 unsigned char *p = buf;
Jerry Yu57d48412022-04-20 21:50:42 +08002076 uint16_t group = ssl->handshake->offered_group_id;
Jerry Yu3bf2c642022-03-30 22:02:12 +08002077 unsigned char *server_share = buf + 4;
Jerry Yu3bf2c642022-03-30 22:02:12 +08002078 size_t key_exchange_length;
Jerry Yu3bf2c642022-03-30 22:02:12 +08002079
2080 *out_len = 0;
2081
Gilles Peskine449bd832023-01-11 14:50:10 +01002082 MBEDTLS_SSL_DEBUG_MSG(3, ("server hello, adding key share extension"));
Jerry Yu3bf2c642022-03-30 22:02:12 +08002083
Gilles Peskine449bd832023-01-11 14:50:10 +01002084 MBEDTLS_SSL_DEBUG_MSG(2, ("server hello, write selected_group: %s (%04x)",
2085 mbedtls_ssl_named_group_to_str(group),
2086 group));
Jerry Yu58af2332022-09-06 11:19:31 +08002087
Jerry Yu3bf2c642022-03-30 22:02:12 +08002088 /* Check if we have space for header and length fields:
2089 * - extension_type (2 bytes)
2090 * - extension_data_length (2 bytes)
2091 * - group (2 bytes)
2092 * - key_exchange_length (2 bytes)
2093 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002094 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 8);
2095 MBEDTLS_PUT_UINT16_BE(MBEDTLS_TLS_EXT_KEY_SHARE, p, 0);
2096 MBEDTLS_PUT_UINT16_BE(group, server_share, 0);
Jerry Yu3bf2c642022-03-30 22:02:12 +08002097 p += 8;
Jerry Yu57d48412022-04-20 21:50:42 +08002098
Jerry Yu3bf2c642022-03-30 22:02:12 +08002099 /* When we introduce PQC-ECDHE hybrids, we'll want to call this
2100 * function multiple times. */
Jerry Yu955ddd72022-04-22 22:27:33 +08002101 ret = ssl_tls13_generate_and_write_key_share(
Gilles Peskine449bd832023-01-11 14:50:10 +01002102 ssl, group, server_share + 4, end, &key_exchange_length);
2103 if (ret != 0) {
2104 return ret;
2105 }
Jerry Yu3bf2c642022-03-30 22:02:12 +08002106 p += key_exchange_length;
Jerry Yuc1be19f2022-04-23 16:11:39 +08002107
Gilles Peskine449bd832023-01-11 14:50:10 +01002108 MBEDTLS_PUT_UINT16_BE(key_exchange_length, server_share + 2, 0);
Jerry Yu3bf2c642022-03-30 22:02:12 +08002109
Gilles Peskine449bd832023-01-11 14:50:10 +01002110 MBEDTLS_PUT_UINT16_BE(p - server_share, buf, 2);
Jerry Yu3bf2c642022-03-30 22:02:12 +08002111
Jerry Yu57d48412022-04-20 21:50:42 +08002112 *out_len = p - buf;
Jerry Yu955ddd72022-04-22 22:27:33 +08002113
Gilles Peskine449bd832023-01-11 14:50:10 +01002114 mbedtls_ssl_tls13_set_hs_sent_ext_mask(ssl, MBEDTLS_TLS_EXT_KEY_SHARE);
Jerry Yub95dd362022-11-08 21:19:34 +08002115
Gilles Peskine449bd832023-01-11 14:50:10 +01002116 return 0;
Jerry Yu3bf2c642022-03-30 22:02:12 +08002117}
Jerry Yud9436a12022-04-20 22:28:09 +08002118
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002119MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002120static int ssl_tls13_write_hrr_key_share_ext(mbedtls_ssl_context *ssl,
2121 unsigned char *buf,
2122 unsigned char *end,
2123 size_t *out_len)
Jerry Yu23f7a6f2022-04-23 15:16:45 +08002124{
2125 uint16_t selected_group = ssl->handshake->hrr_selected_group;
2126 /* key_share Extension
2127 *
2128 * struct {
2129 * select (Handshake.msg_type) {
2130 * ...
2131 * case hello_retry_request:
2132 * NamedGroup selected_group;
2133 * ...
2134 * };
2135 * } KeyShare;
2136 */
2137
2138 *out_len = 0;
2139
Jerry Yub0ac10b2022-05-05 11:10:08 +08002140 /*
2141 * For a pure PSK key exchange, there is no group to agree upon. The purpose
2142 * of the HRR is then to transmit a cookie to force the client to demonstrate
2143 * reachability at their apparent network address (primarily useful for DTLS).
2144 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002145 if (!mbedtls_ssl_tls13_key_exchange_mode_with_ephemeral(ssl)) {
2146 return 0;
2147 }
Jerry Yu23f7a6f2022-04-23 15:16:45 +08002148
2149 /* We should only send the key_share extension if the client's initial
2150 * key share was not acceptable. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002151 if (ssl->handshake->offered_group_id != 0) {
2152 MBEDTLS_SSL_DEBUG_MSG(4, ("Skip key_share extension in HRR"));
2153 return 0;
Jerry Yu23f7a6f2022-04-23 15:16:45 +08002154 }
2155
Gilles Peskine449bd832023-01-11 14:50:10 +01002156 if (selected_group == 0) {
2157 MBEDTLS_SSL_DEBUG_MSG(1, ("no matching named group found"));
2158 return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
Jerry Yu23f7a6f2022-04-23 15:16:45 +08002159 }
2160
Jerry Yub0ac10b2022-05-05 11:10:08 +08002161 /* Check if we have enough space:
2162 * - extension_type (2 bytes)
2163 * - extension_data_length (2 bytes)
2164 * - selected_group (2 bytes)
2165 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002166 MBEDTLS_SSL_CHK_BUF_PTR(buf, end, 6);
Jerry Yu23f7a6f2022-04-23 15:16:45 +08002167
Gilles Peskine449bd832023-01-11 14:50:10 +01002168 MBEDTLS_PUT_UINT16_BE(MBEDTLS_TLS_EXT_KEY_SHARE, buf, 0);
2169 MBEDTLS_PUT_UINT16_BE(2, buf, 2);
2170 MBEDTLS_PUT_UINT16_BE(selected_group, buf, 4);
Jerry Yu23f7a6f2022-04-23 15:16:45 +08002171
Gilles Peskine449bd832023-01-11 14:50:10 +01002172 MBEDTLS_SSL_DEBUG_MSG(3,
2173 ("HRR selected_group: %s (%x)",
2174 mbedtls_ssl_named_group_to_str(selected_group),
2175 selected_group));
Jerry Yu23f7a6f2022-04-23 15:16:45 +08002176
2177 *out_len = 6;
Jerry Yu23f7a6f2022-04-23 15:16:45 +08002178
Gilles Peskine449bd832023-01-11 14:50:10 +01002179 mbedtls_ssl_tls13_set_hs_sent_ext_mask(ssl, MBEDTLS_TLS_EXT_KEY_SHARE);
Jerry Yub95dd362022-11-08 21:19:34 +08002180
Gilles Peskine449bd832023-01-11 14:50:10 +01002181 return 0;
Jerry Yu23f7a6f2022-04-23 15:16:45 +08002182}
Jerry Yu3bf2c642022-03-30 22:02:12 +08002183
2184/*
2185 * Structure of ServerHello message:
2186 *
2187 * struct {
2188 * ProtocolVersion legacy_version = 0x0303; // TLS v1.2
2189 * Random random;
2190 * opaque legacy_session_id_echo<0..32>;
2191 * CipherSuite cipher_suite;
2192 * uint8 legacy_compression_method = 0;
2193 * Extension extensions<6..2^16-1>;
2194 * } ServerHello;
2195 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002196MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002197static int ssl_tls13_write_server_hello_body(mbedtls_ssl_context *ssl,
2198 unsigned char *buf,
2199 unsigned char *end,
2200 size_t *out_len,
2201 int is_hrr)
Jerry Yu56404d72022-03-30 17:36:13 +08002202{
Jerry Yu955ddd72022-04-22 22:27:33 +08002203 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yu3bf2c642022-03-30 22:02:12 +08002204 unsigned char *p = buf;
Jerry Yud9436a12022-04-20 22:28:09 +08002205 unsigned char *p_extensions_len;
Jerry Yufbe3e642022-04-25 19:31:51 +08002206 size_t output_len;
Jerry Yu3bf2c642022-03-30 22:02:12 +08002207
2208 *out_len = 0;
Jerry Yu50e00e32022-10-31 14:45:01 +08002209 ssl->handshake->sent_extensions = MBEDTLS_SSL_EXT_MASK_NONE;
Jerry Yu3bf2c642022-03-30 22:02:12 +08002210
Jerry Yucfc04b32022-04-21 09:31:58 +08002211 /* ...
2212 * ProtocolVersion legacy_version = 0x0303; // TLS 1.2
2213 * ...
2214 * with ProtocolVersion defined as:
2215 * uint16 ProtocolVersion;
Jerry Yu3bf2c642022-03-30 22:02:12 +08002216 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002217 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 2);
2218 MBEDTLS_PUT_UINT16_BE(0x0303, p, 0);
Jerry Yu3bf2c642022-03-30 22:02:12 +08002219 p += 2;
2220
Jerry Yu1c3e6882022-04-20 21:23:40 +08002221 /* ...
2222 * Random random;
2223 * ...
Jerry Yucfc04b32022-04-21 09:31:58 +08002224 * with Random defined as:
2225 * opaque Random[MBEDTLS_SERVER_HELLO_RANDOM_LEN];
Jerry Yu1c3e6882022-04-20 21:23:40 +08002226 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002227 MBEDTLS_SSL_CHK_BUF_PTR(p, end, MBEDTLS_SERVER_HELLO_RANDOM_LEN);
2228 if (is_hrr) {
2229 memcpy(p, mbedtls_ssl_tls13_hello_retry_request_magic,
2230 MBEDTLS_SERVER_HELLO_RANDOM_LEN);
2231 } else {
2232 memcpy(p, &ssl->handshake->randbytes[MBEDTLS_CLIENT_HELLO_RANDOM_LEN],
2233 MBEDTLS_SERVER_HELLO_RANDOM_LEN);
Jerry Yu23f7a6f2022-04-23 15:16:45 +08002234 }
Gilles Peskine449bd832023-01-11 14:50:10 +01002235 MBEDTLS_SSL_DEBUG_BUF(3, "server hello, random bytes",
2236 p, MBEDTLS_SERVER_HELLO_RANDOM_LEN);
Jerry Yu3bf2c642022-03-30 22:02:12 +08002237 p += MBEDTLS_SERVER_HELLO_RANDOM_LEN;
2238
Jerry Yucfc04b32022-04-21 09:31:58 +08002239 /* ...
2240 * opaque legacy_session_id_echo<0..32>;
2241 * ...
Jerry Yu3bf2c642022-03-30 22:02:12 +08002242 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002243 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 1 + ssl->session_negotiate->id_len);
2244 *p++ = (unsigned char) ssl->session_negotiate->id_len;
2245 if (ssl->session_negotiate->id_len > 0) {
2246 memcpy(p, &ssl->session_negotiate->id[0],
2247 ssl->session_negotiate->id_len);
Jerry Yu3bf2c642022-03-30 22:02:12 +08002248 p += ssl->session_negotiate->id_len;
Jerry Yu955ddd72022-04-22 22:27:33 +08002249
Gilles Peskine449bd832023-01-11 14:50:10 +01002250 MBEDTLS_SSL_DEBUG_BUF(3, "session id", ssl->session_negotiate->id,
2251 ssl->session_negotiate->id_len);
Jerry Yu3bf2c642022-03-30 22:02:12 +08002252 }
2253
Jerry Yucfc04b32022-04-21 09:31:58 +08002254 /* ...
2255 * CipherSuite cipher_suite;
2256 * ...
2257 * with CipherSuite defined as:
2258 * uint8 CipherSuite[2];
Jerry Yu3bf2c642022-03-30 22:02:12 +08002259 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002260 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 2);
2261 MBEDTLS_PUT_UINT16_BE(ssl->session_negotiate->ciphersuite, p, 0);
Jerry Yu3bf2c642022-03-30 22:02:12 +08002262 p += 2;
Gilles Peskine449bd832023-01-11 14:50:10 +01002263 MBEDTLS_SSL_DEBUG_MSG(3,
2264 ("server hello, chosen ciphersuite: %s ( id=%d )",
2265 mbedtls_ssl_get_ciphersuite_name(
2266 ssl->session_negotiate->ciphersuite),
2267 ssl->session_negotiate->ciphersuite));
Jerry Yu3bf2c642022-03-30 22:02:12 +08002268
Jerry Yucfc04b32022-04-21 09:31:58 +08002269 /* ...
2270 * uint8 legacy_compression_method = 0;
2271 * ...
2272 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002273 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 1);
Thomas Daubney31e03a82022-07-25 15:59:25 +01002274 *p++ = MBEDTLS_SSL_COMPRESS_NULL;
Jerry Yu3bf2c642022-03-30 22:02:12 +08002275
Jerry Yucfc04b32022-04-21 09:31:58 +08002276 /* ...
2277 * Extension extensions<6..2^16-1>;
2278 * ...
2279 * struct {
2280 * ExtensionType extension_type; (2 bytes)
2281 * opaque extension_data<0..2^16-1>;
2282 * } Extension;
2283 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002284 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 2);
Jerry Yud9436a12022-04-20 22:28:09 +08002285 p_extensions_len = p;
Jerry Yu3bf2c642022-03-30 22:02:12 +08002286 p += 2;
2287
Gilles Peskine449bd832023-01-11 14:50:10 +01002288 if ((ret = ssl_tls13_write_server_hello_supported_versions_ext(
2289 ssl, p, end, &output_len)) != 0) {
Jerry Yu955ddd72022-04-22 22:27:33 +08002290 MBEDTLS_SSL_DEBUG_RET(
Gilles Peskine449bd832023-01-11 14:50:10 +01002291 1, "ssl_tls13_write_server_hello_supported_versions_ext", ret);
2292 return ret;
Jerry Yu3bf2c642022-03-30 22:02:12 +08002293 }
2294 p += output_len;
2295
Gilles Peskine449bd832023-01-11 14:50:10 +01002296 if (mbedtls_ssl_tls13_key_exchange_mode_with_ephemeral(ssl)) {
2297 if (is_hrr) {
2298 ret = ssl_tls13_write_hrr_key_share_ext(ssl, p, end, &output_len);
2299 } else {
2300 ret = ssl_tls13_write_key_share_ext(ssl, p, end, &output_len);
2301 }
2302 if (ret != 0) {
2303 return ret;
2304 }
Jerry Yu3bf2c642022-03-30 22:02:12 +08002305 p += output_len;
2306 }
Jerry Yu3bf2c642022-03-30 22:02:12 +08002307
Ronald Cron41a443a2022-10-04 16:38:25 +02002308#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01002309 if (!is_hrr && mbedtls_ssl_tls13_key_exchange_mode_with_psk(ssl)) {
2310 ret = ssl_tls13_write_server_pre_shared_key_ext(ssl, p, end, &output_len);
2311 if (ret != 0) {
2312 MBEDTLS_SSL_DEBUG_RET(1, "ssl_tls13_write_server_pre_shared_key_ext",
2313 ret);
2314 return ret;
Jerry Yu032b15ce2022-07-11 06:10:03 +00002315 }
2316 p += output_len;
2317 }
Ronald Cron41a443a2022-10-04 16:38:25 +02002318#endif
Jerry Yu032b15ce2022-07-11 06:10:03 +00002319
Gilles Peskine449bd832023-01-11 14:50:10 +01002320 MBEDTLS_PUT_UINT16_BE(p - p_extensions_len - 2, p_extensions_len, 0);
Jerry Yu3bf2c642022-03-30 22:02:12 +08002321
Gilles Peskine449bd832023-01-11 14:50:10 +01002322 MBEDTLS_SSL_DEBUG_BUF(4, "server hello extensions",
2323 p_extensions_len, p - p_extensions_len);
Jerry Yu3bf2c642022-03-30 22:02:12 +08002324
Jerry Yud9436a12022-04-20 22:28:09 +08002325 *out_len = p - buf;
Jerry Yu3bf2c642022-03-30 22:02:12 +08002326
Gilles Peskine449bd832023-01-11 14:50:10 +01002327 MBEDTLS_SSL_DEBUG_BUF(3, "server hello", buf, *out_len);
Jerry Yu3bf2c642022-03-30 22:02:12 +08002328
Jerry Yu7de2ff02022-11-08 21:43:46 +08002329 MBEDTLS_SSL_PRINT_EXTS(
Jerry Yu4b8f2f72022-10-31 13:31:22 +08002330 3, is_hrr ? MBEDTLS_SSL_TLS1_3_HS_HELLO_RETRY_REQUEST :
Gilles Peskine449bd832023-01-11 14:50:10 +01002331 MBEDTLS_SSL_HS_SERVER_HELLO,
2332 ssl->handshake->sent_extensions);
Jerry Yu4b8f2f72022-10-31 13:31:22 +08002333
Gilles Peskine449bd832023-01-11 14:50:10 +01002334 return ret;
Jerry Yu56404d72022-03-30 17:36:13 +08002335}
2336
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002337MBEDTLS_CHECK_RETURN_CRITICAL
Xiaokang Qian934ce6f2023-02-06 10:23:04 +00002338static int ssl_tls13_finalize_server_hello(mbedtls_ssl_context *ssl)
Jerry Yue110d252022-05-05 10:19:22 +08002339{
2340 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine449bd832023-01-11 14:50:10 +01002341 ret = mbedtls_ssl_tls13_compute_handshake_transform(ssl);
2342 if (ret != 0) {
2343 MBEDTLS_SSL_DEBUG_RET(1,
2344 "mbedtls_ssl_tls13_compute_handshake_transform",
2345 ret);
2346 return ret;
Jerry Yue110d252022-05-05 10:19:22 +08002347 }
2348
Gilles Peskine449bd832023-01-11 14:50:10 +01002349 return ret;
Jerry Yue110d252022-05-05 10:19:22 +08002350}
2351
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002352MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002353static int ssl_tls13_write_server_hello(mbedtls_ssl_context *ssl)
Jerry Yu5b64ae92022-03-30 17:15:02 +08002354{
Jerry Yu637a3f12022-04-20 21:37:58 +08002355 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yuf4b27e42022-03-30 17:32:21 +08002356 unsigned char *buf;
2357 size_t buf_len, msg_len;
2358
Gilles Peskine449bd832023-01-11 14:50:10 +01002359 MBEDTLS_SSL_DEBUG_MSG(2, ("=> write server hello"));
Jerry Yuf4b27e42022-03-30 17:32:21 +08002360
Gilles Peskine449bd832023-01-11 14:50:10 +01002361 MBEDTLS_SSL_PROC_CHK(ssl_tls13_prepare_server_hello(ssl));
Jerry Yuf4b27e42022-03-30 17:32:21 +08002362
Xiaokang Qian9f1747b2023-03-30 02:50:04 +00002363 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_start_handshake_msg(
2364 ssl, MBEDTLS_SSL_HS_SERVER_HELLO, &buf, &buf_len));
Jerry Yuf4b27e42022-03-30 17:32:21 +08002365
Gilles Peskine449bd832023-01-11 14:50:10 +01002366 MBEDTLS_SSL_PROC_CHK(ssl_tls13_write_server_hello_body(ssl, buf,
2367 buf + buf_len,
2368 &msg_len,
2369 0));
Jerry Yuf4b27e42022-03-30 17:32:21 +08002370
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +01002371 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_add_hs_msg_to_checksum(
Manuel Pégourié-Gonnard43cc1272023-02-06 11:48:19 +01002372 ssl, MBEDTLS_SSL_HS_SERVER_HELLO, buf, msg_len));
Jerry Yuf4b27e42022-03-30 17:32:21 +08002373
Gilles Peskine449bd832023-01-11 14:50:10 +01002374 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_finish_handshake_msg(
2375 ssl, buf_len, msg_len));
Jerry Yu637a3f12022-04-20 21:37:58 +08002376
Xiaokang Qian934ce6f2023-02-06 10:23:04 +00002377 MBEDTLS_SSL_PROC_CHK(ssl_tls13_finalize_server_hello(ssl));
Jerry Yue110d252022-05-05 10:19:22 +08002378
Gabor Mezei7b39bf12022-05-24 16:04:14 +02002379#if defined(MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE)
2380 /* The server sends a dummy change_cipher_spec record immediately
2381 * after its first handshake message. This may either be after
2382 * a ServerHello or a HelloRetryRequest.
2383 */
Gabor Mezei96ae9262022-06-28 11:45:18 +02002384 mbedtls_ssl_handshake_set_state(
Gilles Peskine449bd832023-01-11 14:50:10 +01002385 ssl, MBEDTLS_SSL_SERVER_CCS_AFTER_SERVER_HELLO);
Gabor Mezei7b39bf12022-05-24 16:04:14 +02002386#else
Gilles Peskine449bd832023-01-11 14:50:10 +01002387 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_ENCRYPTED_EXTENSIONS);
Gabor Mezei7b39bf12022-05-24 16:04:14 +02002388#endif /* MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE */
Jerry Yue110d252022-05-05 10:19:22 +08002389
Jerry Yuf4b27e42022-03-30 17:32:21 +08002390cleanup:
2391
Gilles Peskine449bd832023-01-11 14:50:10 +01002392 MBEDTLS_SSL_DEBUG_MSG(2, ("<= write server hello"));
2393 return ret;
Jerry Yu5b64ae92022-03-30 17:15:02 +08002394}
2395
Jerry Yu23d1a252022-05-12 18:08:59 +08002396
2397/*
2398 * Handler for MBEDTLS_SSL_HELLO_RETRY_REQUEST
2399 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002400MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002401static int ssl_tls13_prepare_hello_retry_request(mbedtls_ssl_context *ssl)
Jerry Yu23d1a252022-05-12 18:08:59 +08002402{
2403 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine449bd832023-01-11 14:50:10 +01002404 if (ssl->handshake->hello_retry_request_count > 0) {
2405 MBEDTLS_SSL_DEBUG_MSG(1, ("Too many HRRs"));
2406 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE,
2407 MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE);
2408 return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
Jerry Yu23d1a252022-05-12 18:08:59 +08002409 }
2410
2411 /*
2412 * Create stateless transcript hash for HRR
2413 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002414 MBEDTLS_SSL_DEBUG_MSG(4, ("Reset transcript for HRR"));
2415 ret = mbedtls_ssl_reset_transcript_for_hrr(ssl);
2416 if (ret != 0) {
2417 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_reset_transcript_for_hrr", ret);
2418 return ret;
Jerry Yu23d1a252022-05-12 18:08:59 +08002419 }
Gilles Peskine449bd832023-01-11 14:50:10 +01002420 mbedtls_ssl_session_reset_msg_layer(ssl, 0);
Jerry Yu23d1a252022-05-12 18:08:59 +08002421
Gilles Peskine449bd832023-01-11 14:50:10 +01002422 return 0;
Jerry Yu23d1a252022-05-12 18:08:59 +08002423}
2424
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002425MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002426static int ssl_tls13_write_hello_retry_request(mbedtls_ssl_context *ssl)
Jerry Yu23d1a252022-05-12 18:08:59 +08002427{
2428 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
2429 unsigned char *buf;
2430 size_t buf_len, msg_len;
2431
Gilles Peskine449bd832023-01-11 14:50:10 +01002432 MBEDTLS_SSL_DEBUG_MSG(2, ("=> write hello retry request"));
Jerry Yu23d1a252022-05-12 18:08:59 +08002433
Gilles Peskine449bd832023-01-11 14:50:10 +01002434 MBEDTLS_SSL_PROC_CHK(ssl_tls13_prepare_hello_retry_request(ssl));
Jerry Yu23d1a252022-05-12 18:08:59 +08002435
Gilles Peskine449bd832023-01-11 14:50:10 +01002436 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_start_handshake_msg(
2437 ssl, MBEDTLS_SSL_HS_SERVER_HELLO,
2438 &buf, &buf_len));
Jerry Yu23d1a252022-05-12 18:08:59 +08002439
Gilles Peskine449bd832023-01-11 14:50:10 +01002440 MBEDTLS_SSL_PROC_CHK(ssl_tls13_write_server_hello_body(ssl, buf,
2441 buf + buf_len,
2442 &msg_len,
2443 1));
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +01002444 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_add_hs_msg_to_checksum(
Manuel Pégourié-Gonnard43cc1272023-02-06 11:48:19 +01002445 ssl, MBEDTLS_SSL_HS_SERVER_HELLO, buf, msg_len));
Jerry Yu23d1a252022-05-12 18:08:59 +08002446
2447
Gilles Peskine449bd832023-01-11 14:50:10 +01002448 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_finish_handshake_msg(ssl, buf_len,
2449 msg_len));
Jerry Yu23d1a252022-05-12 18:08:59 +08002450
2451 ssl->handshake->hello_retry_request_count++;
2452
Gabor Mezei7b39bf12022-05-24 16:04:14 +02002453#if defined(MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE)
2454 /* The server sends a dummy change_cipher_spec record immediately
2455 * after its first handshake message. This may either be after
2456 * a ServerHello or a HelloRetryRequest.
2457 */
Gabor Mezei96ae9262022-06-28 11:45:18 +02002458 mbedtls_ssl_handshake_set_state(
Gilles Peskine449bd832023-01-11 14:50:10 +01002459 ssl, MBEDTLS_SSL_SERVER_CCS_AFTER_HELLO_RETRY_REQUEST);
Gabor Mezei7b39bf12022-05-24 16:04:14 +02002460#else
Gilles Peskine449bd832023-01-11 14:50:10 +01002461 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_CLIENT_HELLO);
Gabor Mezei7b39bf12022-05-24 16:04:14 +02002462#endif /* MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE */
Jerry Yu23d1a252022-05-12 18:08:59 +08002463
2464cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +01002465 MBEDTLS_SSL_DEBUG_MSG(2, ("<= write hello retry request"));
2466 return ret;
Jerry Yu23d1a252022-05-12 18:08:59 +08002467}
2468
Jerry Yu5b64ae92022-03-30 17:15:02 +08002469/*
Jerry Yu4d3841a2022-04-16 12:37:19 +08002470 * Handler for MBEDTLS_SSL_ENCRYPTED_EXTENSIONS
2471 */
Jerry Yu4d3841a2022-04-16 12:37:19 +08002472
2473/*
2474 * struct {
2475 * Extension extensions<0..2 ^ 16 - 1>;
2476 * } EncryptedExtensions;
2477 *
2478 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002479MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002480static int ssl_tls13_write_encrypted_extensions_body(mbedtls_ssl_context *ssl,
2481 unsigned char *buf,
2482 unsigned char *end,
2483 size_t *out_len)
Jerry Yu4d3841a2022-04-16 12:37:19 +08002484{
XiaokangQianacb39922022-06-17 10:18:48 +00002485 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yu4d3841a2022-04-16 12:37:19 +08002486 unsigned char *p = buf;
2487 size_t extensions_len = 0;
Jerry Yu8937eb42022-05-03 12:12:14 +08002488 unsigned char *p_extensions_len;
XiaokangQianacb39922022-06-17 10:18:48 +00002489 size_t output_len;
Jerry Yu9da5e5a2022-05-03 15:46:09 +08002490
Jerry Yu4d3841a2022-04-16 12:37:19 +08002491 *out_len = 0;
2492
Gilles Peskine449bd832023-01-11 14:50:10 +01002493 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 2);
Jerry Yu8937eb42022-05-03 12:12:14 +08002494 p_extensions_len = p;
Jerry Yu4d3841a2022-04-16 12:37:19 +08002495 p += 2;
2496
Jerry Yu8937eb42022-05-03 12:12:14 +08002497 ((void) ssl);
XiaokangQianacb39922022-06-17 10:18:48 +00002498 ((void) ret);
2499 ((void) output_len);
2500
2501#if defined(MBEDTLS_SSL_ALPN)
Gilles Peskine449bd832023-01-11 14:50:10 +01002502 ret = mbedtls_ssl_write_alpn_ext(ssl, p, end, &output_len);
2503 if (ret != 0) {
2504 return ret;
2505 }
XiaokangQian95d5f542022-06-24 02:29:26 +00002506 p += output_len;
XiaokangQianacb39922022-06-17 10:18:48 +00002507#endif /* MBEDTLS_SSL_ALPN */
Jerry Yu4d3841a2022-04-16 12:37:19 +08002508
Jerry Yu71c14f12022-12-12 11:10:35 +08002509#if defined(MBEDTLS_SSL_EARLY_DATA)
2510 if (ssl->early_data_status == MBEDTLS_SSL_EARLY_DATA_STATUS_ACCEPTED) {
2511 ret = mbedtls_ssl_tls13_write_early_data_ext(ssl, p, end, &output_len);
2512 if (ret != 0) {
2513 return ret;
2514 }
2515 p += output_len;
2516 }
2517#endif /* MBEDTLS_SSL_EARLY_DATA */
2518
Gilles Peskine449bd832023-01-11 14:50:10 +01002519 extensions_len = (p - p_extensions_len) - 2;
2520 MBEDTLS_PUT_UINT16_BE(extensions_len, p_extensions_len, 0);
Jerry Yu8937eb42022-05-03 12:12:14 +08002521
2522 *out_len = p - buf;
Jerry Yu4d3841a2022-04-16 12:37:19 +08002523
Gilles Peskine449bd832023-01-11 14:50:10 +01002524 MBEDTLS_SSL_DEBUG_BUF(4, "encrypted extensions", buf, *out_len);
Jerry Yu4d3841a2022-04-16 12:37:19 +08002525
Jerry Yu7de2ff02022-11-08 21:43:46 +08002526 MBEDTLS_SSL_PRINT_EXTS(
Gilles Peskine449bd832023-01-11 14:50:10 +01002527 3, MBEDTLS_SSL_HS_ENCRYPTED_EXTENSIONS, ssl->handshake->sent_extensions);
Jerry Yu4b8f2f72022-10-31 13:31:22 +08002528
Gilles Peskine449bd832023-01-11 14:50:10 +01002529 return 0;
Jerry Yu4d3841a2022-04-16 12:37:19 +08002530}
2531
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002532MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002533static int ssl_tls13_write_encrypted_extensions(mbedtls_ssl_context *ssl)
Jerry Yu4d3841a2022-04-16 12:37:19 +08002534{
2535 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
2536 unsigned char *buf;
Jerry Yu39730a72022-05-03 12:14:04 +08002537 size_t buf_len, msg_len;
Jerry Yu4d3841a2022-04-16 12:37:19 +08002538
Gilles Peskine449bd832023-01-11 14:50:10 +01002539 mbedtls_ssl_set_outbound_transform(ssl,
2540 ssl->handshake->transform_handshake);
Gabor Mezei54719122022-06-28 11:34:56 +02002541 MBEDTLS_SSL_DEBUG_MSG(
Gilles Peskine449bd832023-01-11 14:50:10 +01002542 3, ("switching to handshake transform for outbound data"));
Gabor Mezei54719122022-06-28 11:34:56 +02002543
Gilles Peskine449bd832023-01-11 14:50:10 +01002544 MBEDTLS_SSL_DEBUG_MSG(2, ("=> write encrypted extensions"));
Jerry Yu4d3841a2022-04-16 12:37:19 +08002545
Xiaokang Qian9f1747b2023-03-30 02:50:04 +00002546 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_start_handshake_msg(
2547 ssl, MBEDTLS_SSL_HS_ENCRYPTED_EXTENSIONS,
2548 &buf, &buf_len));
Jerry Yu4d3841a2022-04-16 12:37:19 +08002549
Gilles Peskine449bd832023-01-11 14:50:10 +01002550 MBEDTLS_SSL_PROC_CHK(ssl_tls13_write_encrypted_extensions_body(
2551 ssl, buf, buf + buf_len, &msg_len));
Jerry Yu4d3841a2022-04-16 12:37:19 +08002552
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +01002553 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_add_hs_msg_to_checksum(
Xiaokang Qian9f1747b2023-03-30 02:50:04 +00002554 ssl, MBEDTLS_SSL_HS_ENCRYPTED_EXTENSIONS,
2555 buf, msg_len));
Jerry Yu4d3841a2022-04-16 12:37:19 +08002556
Gilles Peskine449bd832023-01-11 14:50:10 +01002557 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_finish_handshake_msg(
2558 ssl, buf_len, msg_len));
Jerry Yu4d3841a2022-04-16 12:37:19 +08002559
Ronald Cron928cbd32022-10-04 16:14:26 +02002560#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01002561 if (mbedtls_ssl_tls13_key_exchange_mode_with_psk(ssl)) {
2562 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_SERVER_FINISHED);
2563 } else {
2564 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_CERTIFICATE_REQUEST);
2565 }
Jerry Yu8937eb42022-05-03 12:12:14 +08002566#else
Gilles Peskine449bd832023-01-11 14:50:10 +01002567 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_SERVER_FINISHED);
Jerry Yu8937eb42022-05-03 12:12:14 +08002568#endif
2569
Jerry Yu4d3841a2022-04-16 12:37:19 +08002570cleanup:
2571
Gilles Peskine449bd832023-01-11 14:50:10 +01002572 MBEDTLS_SSL_DEBUG_MSG(2, ("<= write encrypted extensions"));
2573 return ret;
Jerry Yu4d3841a2022-04-16 12:37:19 +08002574}
2575
Ronald Cron928cbd32022-10-04 16:14:26 +02002576#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED)
XiaokangQiana987e1d2022-05-07 01:25:58 +00002577#define SSL_CERTIFICATE_REQUEST_SEND_REQUEST 0
2578#define SSL_CERTIFICATE_REQUEST_SKIP 1
2579/* Coordination:
2580 * Check whether a CertificateRequest message should be written.
2581 * Returns a negative code on failure, or
2582 * - SSL_CERTIFICATE_REQUEST_SEND_REQUEST
2583 * - SSL_CERTIFICATE_REQUEST_SKIP
2584 * indicating if the writing of the CertificateRequest
2585 * should be skipped or not.
2586 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002587MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002588static int ssl_tls13_certificate_request_coordinate(mbedtls_ssl_context *ssl)
XiaokangQiana987e1d2022-05-07 01:25:58 +00002589{
2590 int authmode;
2591
XiaokangQian40a35232022-05-07 09:02:40 +00002592#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Gilles Peskine449bd832023-01-11 14:50:10 +01002593 if (ssl->handshake->sni_authmode != MBEDTLS_SSL_VERIFY_UNSET) {
XiaokangQian40a35232022-05-07 09:02:40 +00002594 authmode = ssl->handshake->sni_authmode;
Gilles Peskine449bd832023-01-11 14:50:10 +01002595 } else
XiaokangQian40a35232022-05-07 09:02:40 +00002596#endif
XiaokangQiana987e1d2022-05-07 01:25:58 +00002597 authmode = ssl->conf->authmode;
2598
Gilles Peskine449bd832023-01-11 14:50:10 +01002599 if (authmode == MBEDTLS_SSL_VERIFY_NONE) {
Ronald Croneac00ad2022-09-13 10:16:31 +02002600 ssl->session_negotiate->verify_result = MBEDTLS_X509_BADCERT_SKIP_VERIFY;
Gilles Peskine449bd832023-01-11 14:50:10 +01002601 return SSL_CERTIFICATE_REQUEST_SKIP;
Ronald Croneac00ad2022-09-13 10:16:31 +02002602 }
XiaokangQiana987e1d2022-05-07 01:25:58 +00002603
XiaokangQianc3017f62022-05-13 05:55:41 +00002604 ssl->handshake->certificate_request_sent = 1;
XiaokangQian189ded22022-05-10 08:12:17 +00002605
Gilles Peskine449bd832023-01-11 14:50:10 +01002606 return SSL_CERTIFICATE_REQUEST_SEND_REQUEST;
XiaokangQiana987e1d2022-05-07 01:25:58 +00002607}
2608
XiaokangQiancec9ae62022-05-06 07:28:50 +00002609/*
2610 * struct {
2611 * opaque certificate_request_context<0..2^8-1>;
2612 * Extension extensions<2..2^16-1>;
2613 * } CertificateRequest;
2614 *
2615 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002616MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002617static int ssl_tls13_write_certificate_request_body(mbedtls_ssl_context *ssl,
2618 unsigned char *buf,
2619 const unsigned char *end,
2620 size_t *out_len)
XiaokangQiancec9ae62022-05-06 07:28:50 +00002621{
2622 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
2623 unsigned char *p = buf;
XiaokangQianec6efb92022-05-06 09:53:10 +00002624 size_t output_len = 0;
XiaokangQiancec9ae62022-05-06 07:28:50 +00002625 unsigned char *p_extensions_len;
2626
2627 *out_len = 0;
2628
2629 /* Check if we have enough space:
2630 * - certificate_request_context (1 byte)
2631 * - extensions length (2 bytes)
2632 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002633 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 3);
XiaokangQiancec9ae62022-05-06 07:28:50 +00002634
2635 /*
2636 * Write certificate_request_context
2637 */
2638 /*
2639 * We use a zero length context for the normal handshake
2640 * messages. For post-authentication handshake messages
2641 * this request context would be set to a non-zero value.
2642 */
2643 *p++ = 0x0;
2644
2645 /*
2646 * Write extensions
2647 */
2648 /* The extensions must contain the signature_algorithms. */
2649 p_extensions_len = p;
2650 p += 2;
Gilles Peskine449bd832023-01-11 14:50:10 +01002651 ret = mbedtls_ssl_write_sig_alg_ext(ssl, p, end, &output_len);
2652 if (ret != 0) {
2653 return ret;
2654 }
XiaokangQiancec9ae62022-05-06 07:28:50 +00002655
XiaokangQianec6efb92022-05-06 09:53:10 +00002656 p += output_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01002657 MBEDTLS_PUT_UINT16_BE(p - p_extensions_len - 2, p_extensions_len, 0);
XiaokangQiancec9ae62022-05-06 07:28:50 +00002658
2659 *out_len = p - buf;
2660
Jerry Yu7de2ff02022-11-08 21:43:46 +08002661 MBEDTLS_SSL_PRINT_EXTS(
Gilles Peskine449bd832023-01-11 14:50:10 +01002662 3, MBEDTLS_SSL_HS_CERTIFICATE_REQUEST, ssl->handshake->sent_extensions);
Jerry Yu4b8f2f72022-10-31 13:31:22 +08002663
Gilles Peskine449bd832023-01-11 14:50:10 +01002664 return 0;
XiaokangQiancec9ae62022-05-06 07:28:50 +00002665}
2666
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002667MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002668static int ssl_tls13_write_certificate_request(mbedtls_ssl_context *ssl)
XiaokangQiancec9ae62022-05-06 07:28:50 +00002669{
2670 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
2671
Gilles Peskine449bd832023-01-11 14:50:10 +01002672 MBEDTLS_SSL_DEBUG_MSG(2, ("=> write certificate request"));
XiaokangQiancec9ae62022-05-06 07:28:50 +00002673
Gilles Peskine449bd832023-01-11 14:50:10 +01002674 MBEDTLS_SSL_PROC_CHK_NEG(ssl_tls13_certificate_request_coordinate(ssl));
XiaokangQiancec9ae62022-05-06 07:28:50 +00002675
Gilles Peskine449bd832023-01-11 14:50:10 +01002676 if (ret == SSL_CERTIFICATE_REQUEST_SEND_REQUEST) {
XiaokangQiancec9ae62022-05-06 07:28:50 +00002677 unsigned char *buf;
2678 size_t buf_len, msg_len;
2679
Xiaokang Qian9f1747b2023-03-30 02:50:04 +00002680 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_start_handshake_msg(
2681 ssl, MBEDTLS_SSL_HS_CERTIFICATE_REQUEST,
2682 &buf, &buf_len));
XiaokangQiancec9ae62022-05-06 07:28:50 +00002683
Gilles Peskine449bd832023-01-11 14:50:10 +01002684 MBEDTLS_SSL_PROC_CHK(ssl_tls13_write_certificate_request_body(
2685 ssl, buf, buf + buf_len, &msg_len));
XiaokangQiancec9ae62022-05-06 07:28:50 +00002686
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +01002687 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_add_hs_msg_to_checksum(
Xiaokang Qian9f1747b2023-03-30 02:50:04 +00002688 ssl, MBEDTLS_SSL_HS_CERTIFICATE_REQUEST,
2689 buf, msg_len));
XiaokangQiancec9ae62022-05-06 07:28:50 +00002690
Gilles Peskine449bd832023-01-11 14:50:10 +01002691 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_finish_handshake_msg(
2692 ssl, buf_len, msg_len));
2693 } else if (ret == SSL_CERTIFICATE_REQUEST_SKIP) {
2694 MBEDTLS_SSL_DEBUG_MSG(2, ("<= skip write certificate request"));
XiaokangQiancec9ae62022-05-06 07:28:50 +00002695 ret = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01002696 } else {
2697 MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
XiaokangQiancec9ae62022-05-06 07:28:50 +00002698 ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
2699 goto cleanup;
2700 }
2701
Gilles Peskine449bd832023-01-11 14:50:10 +01002702 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_SERVER_CERTIFICATE);
XiaokangQiancec9ae62022-05-06 07:28:50 +00002703cleanup:
2704
Gilles Peskine449bd832023-01-11 14:50:10 +01002705 MBEDTLS_SSL_DEBUG_MSG(2, ("<= write certificate request"));
2706 return ret;
XiaokangQiancec9ae62022-05-06 07:28:50 +00002707}
XiaokangQiancec9ae62022-05-06 07:28:50 +00002708
Jerry Yu4d3841a2022-04-16 12:37:19 +08002709/*
Jerry Yuc6e6dbf2022-04-16 19:42:57 +08002710 * Handler for MBEDTLS_SSL_SERVER_CERTIFICATE
Jerry Yu83da34e2022-04-16 13:59:52 +08002711 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002712MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002713static int ssl_tls13_write_server_certificate(mbedtls_ssl_context *ssl)
Jerry Yu83da34e2022-04-16 13:59:52 +08002714{
Jerry Yu5a26f302022-05-10 20:46:40 +08002715 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
XiaokangQianfb665a82022-06-15 03:57:21 +00002716
2717#if defined(MBEDTLS_X509_CRT_PARSE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01002718 if ((ssl_tls13_pick_key_cert(ssl) != 0) ||
2719 mbedtls_ssl_own_cert(ssl) == NULL) {
2720 MBEDTLS_SSL_DEBUG_MSG(2, ("No certificate available."));
2721 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE,
2722 MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE);
2723 return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
Jerry Yu5a26f302022-05-10 20:46:40 +08002724 }
XiaokangQianfb665a82022-06-15 03:57:21 +00002725#endif /* MBEDTLS_X509_CRT_PARSE_C */
Jerry Yu5a26f302022-05-10 20:46:40 +08002726
Gilles Peskine449bd832023-01-11 14:50:10 +01002727 ret = mbedtls_ssl_tls13_write_certificate(ssl);
2728 if (ret != 0) {
2729 return ret;
2730 }
2731 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_CERTIFICATE_VERIFY);
2732 return 0;
Jerry Yu83da34e2022-04-16 13:59:52 +08002733}
2734
2735/*
Jerry Yuc6e6dbf2022-04-16 19:42:57 +08002736 * Handler for MBEDTLS_SSL_CERTIFICATE_VERIFY
Jerry Yu83da34e2022-04-16 13:59:52 +08002737 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002738MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002739static int ssl_tls13_write_certificate_verify(mbedtls_ssl_context *ssl)
Jerry Yu83da34e2022-04-16 13:59:52 +08002740{
Gilles Peskine449bd832023-01-11 14:50:10 +01002741 int ret = mbedtls_ssl_tls13_write_certificate_verify(ssl);
2742 if (ret != 0) {
2743 return ret;
2744 }
2745 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_SERVER_FINISHED);
2746 return 0;
Jerry Yu83da34e2022-04-16 13:59:52 +08002747}
Ronald Cron928cbd32022-10-04 16:14:26 +02002748#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED */
Jerry Yu83da34e2022-04-16 13:59:52 +08002749
2750/*
Jerry Yud6e253d2022-05-18 13:59:24 +08002751 * Handler for MBEDTLS_SSL_SERVER_FINISHED
Jerry Yu69dd8d42022-04-16 12:51:26 +08002752 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002753MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002754static int ssl_tls13_write_server_finished(mbedtls_ssl_context *ssl)
Jerry Yu69dd8d42022-04-16 12:51:26 +08002755{
Jerry Yud6e253d2022-05-18 13:59:24 +08002756 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yu27bdc7c2022-04-16 13:33:27 +08002757
Gilles Peskine449bd832023-01-11 14:50:10 +01002758 ret = mbedtls_ssl_tls13_write_finished_message(ssl);
2759 if (ret != 0) {
2760 return ret;
2761 }
Jerry Yu27bdc7c2022-04-16 13:33:27 +08002762
Gilles Peskine449bd832023-01-11 14:50:10 +01002763 ret = mbedtls_ssl_tls13_compute_application_transform(ssl);
2764 if (ret != 0) {
Jerry Yue3d67cb2022-05-19 15:33:10 +08002765 MBEDTLS_SSL_PEND_FATAL_ALERT(
Gilles Peskine449bd832023-01-11 14:50:10 +01002766 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE,
2767 MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE);
2768 return ret;
Jerry Yue3d67cb2022-05-19 15:33:10 +08002769 }
XiaokangQianc3017f62022-05-13 05:55:41 +00002770
Gilles Peskine449bd832023-01-11 14:50:10 +01002771 MBEDTLS_SSL_DEBUG_MSG(1, ("Switch to handshake keys for inbound traffic"));
2772 mbedtls_ssl_set_inbound_transform(ssl, ssl->handshake->transform_handshake);
XiaokangQian189ded22022-05-10 08:12:17 +00002773
Gilles Peskine449bd832023-01-11 14:50:10 +01002774 if (ssl->handshake->certificate_request_sent) {
2775 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_CLIENT_CERTIFICATE);
2776 } else {
2777 MBEDTLS_SSL_DEBUG_MSG(2, ("skip parse certificate"));
2778 MBEDTLS_SSL_DEBUG_MSG(2, ("skip parse certificate verify"));
2779 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_CLIENT_FINISHED);
Ronald Cron19385882022-06-15 16:26:13 +02002780 }
Ronald Cron63dc4632022-05-31 14:41:53 +02002781
Gilles Peskine449bd832023-01-11 14:50:10 +01002782 return 0;
Jerry Yu69dd8d42022-04-16 12:51:26 +08002783}
2784
2785/*
Jerry Yud6e253d2022-05-18 13:59:24 +08002786 * Handler for MBEDTLS_SSL_CLIENT_FINISHED
Jerry Yu69dd8d42022-04-16 12:51:26 +08002787 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002788MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002789static int ssl_tls13_process_client_finished(mbedtls_ssl_context *ssl)
Jerry Yu69dd8d42022-04-16 12:51:26 +08002790{
Jerry Yud6e253d2022-05-18 13:59:24 +08002791 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
2792
Gilles Peskine449bd832023-01-11 14:50:10 +01002793 ret = mbedtls_ssl_tls13_process_finished_message(ssl);
2794 if (ret != 0) {
2795 return ret;
Jerry Yue3d67cb2022-05-19 15:33:10 +08002796 }
2797
Gilles Peskine449bd832023-01-11 14:50:10 +01002798 ret = mbedtls_ssl_tls13_compute_resumption_master_secret(ssl);
2799 if (ret != 0) {
Xiaokang Qian9f1747b2023-03-30 02:50:04 +00002800 MBEDTLS_SSL_DEBUG_RET(
2801 1, "mbedtls_ssl_tls13_compute_resumption_master_secret", ret);
Gilles Peskine449bd832023-01-11 14:50:10 +01002802 }
2803
2804 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_HANDSHAKE_WRAPUP);
2805 return 0;
Jerry Yu69dd8d42022-04-16 12:51:26 +08002806}
2807
2808/*
Jerry Yud6e253d2022-05-18 13:59:24 +08002809 * Handler for MBEDTLS_SSL_HANDSHAKE_WRAPUP
Jerry Yu69dd8d42022-04-16 12:51:26 +08002810 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002811MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002812static int ssl_tls13_handshake_wrapup(mbedtls_ssl_context *ssl)
Jerry Yu69dd8d42022-04-16 12:51:26 +08002813{
Gilles Peskine449bd832023-01-11 14:50:10 +01002814 MBEDTLS_SSL_DEBUG_MSG(2, ("handshake: done"));
Jerry Yu03ed50b2022-04-16 17:13:30 +08002815
Gilles Peskine449bd832023-01-11 14:50:10 +01002816 mbedtls_ssl_tls13_handshake_wrapup(ssl);
Jerry Yue67bef42022-07-07 07:29:42 +00002817
Pengyu Lv3643fdb2023-01-12 16:46:28 +08002818#if defined(MBEDTLS_SSL_SESSION_TICKETS) && \
2819 defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED)
Pengyu Lva1aa31b2022-12-13 13:49:59 +08002820/* TODO: Remove the check of SOME_PSK_ENABLED since SESSION_TICKETS requires
2821 * SOME_PSK_ENABLED to be enabled. Here is just to make CI happy. It is
2822 * expected to be resolved with issue#6395.
2823 */
Pengyu Lvc7af2c42022-12-01 16:33:00 +08002824 /* Sent NewSessionTicket message only when client supports PSK */
Pengyu Lv3643fdb2023-01-12 16:46:28 +08002825 if (mbedtls_ssl_tls13_some_psk_enabled(ssl)) {
Xiaokang Qian9f1747b2023-03-30 02:50:04 +00002826 mbedtls_ssl_handshake_set_state(
2827 ssl, MBEDTLS_SSL_TLS1_3_NEW_SESSION_TICKET);
Pengyu Lvc7af2c42022-12-01 16:33:00 +08002828 } else
Jerry Yufca4d572022-07-21 10:37:48 +08002829#endif
Pengyu Lv3643fdb2023-01-12 16:46:28 +08002830 {
2831 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_HANDSHAKE_OVER);
2832 }
Gilles Peskine449bd832023-01-11 14:50:10 +01002833 return 0;
Jerry Yu69dd8d42022-04-16 12:51:26 +08002834}
2835
2836/*
Jerry Yua8d3c502022-10-30 14:51:23 +08002837 * Handler for MBEDTLS_SSL_TLS1_3_NEW_SESSION_TICKET
Jerry Yue67bef42022-07-07 07:29:42 +00002838 */
2839#define SSL_NEW_SESSION_TICKET_SKIP 0
2840#define SSL_NEW_SESSION_TICKET_WRITE 1
2841MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002842static int ssl_tls13_write_new_session_ticket_coordinate(mbedtls_ssl_context *ssl)
Jerry Yue67bef42022-07-07 07:29:42 +00002843{
2844 /* Check whether the use of session tickets is enabled */
Gilles Peskine449bd832023-01-11 14:50:10 +01002845 if (ssl->conf->f_ticket_write == NULL) {
2846 MBEDTLS_SSL_DEBUG_MSG(2, ("NewSessionTicket: disabled,"
2847 " callback is not set"));
2848 return SSL_NEW_SESSION_TICKET_SKIP;
Jerry Yud0766ec2022-09-22 10:46:57 +08002849 }
Gilles Peskine449bd832023-01-11 14:50:10 +01002850 if (ssl->conf->new_session_tickets_count == 0) {
2851 MBEDTLS_SSL_DEBUG_MSG(2, ("NewSessionTicket: disabled,"
2852 " configured count is zero"));
2853 return SSL_NEW_SESSION_TICKET_SKIP;
Jerry Yud0766ec2022-09-22 10:46:57 +08002854 }
2855
Gilles Peskine449bd832023-01-11 14:50:10 +01002856 if (ssl->handshake->new_session_tickets_count == 0) {
2857 MBEDTLS_SSL_DEBUG_MSG(2, ("NewSessionTicket: all tickets have "
2858 "been sent."));
2859 return SSL_NEW_SESSION_TICKET_SKIP;
Jerry Yue67bef42022-07-07 07:29:42 +00002860 }
2861
Gilles Peskine449bd832023-01-11 14:50:10 +01002862 return SSL_NEW_SESSION_TICKET_WRITE;
Jerry Yue67bef42022-07-07 07:29:42 +00002863}
2864
2865#if defined(MBEDTLS_SSL_SESSION_TICKETS)
2866MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002867static int ssl_tls13_prepare_new_session_ticket(mbedtls_ssl_context *ssl,
2868 unsigned char *ticket_nonce,
2869 size_t ticket_nonce_size)
Jerry Yue67bef42022-07-07 07:29:42 +00002870{
2871 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
2872 mbedtls_ssl_session *session = ssl->session;
2873 mbedtls_ssl_ciphersuite_t *ciphersuite_info;
2874 psa_algorithm_t psa_hash_alg;
2875 int hash_length;
2876
Gilles Peskine449bd832023-01-11 14:50:10 +01002877 MBEDTLS_SSL_DEBUG_MSG(2, ("=> prepare NewSessionTicket msg"));
Jerry Yue67bef42022-07-07 07:29:42 +00002878
2879#if defined(MBEDTLS_HAVE_TIME)
Gilles Peskine449bd832023-01-11 14:50:10 +01002880 session->start = mbedtls_time(NULL);
Jerry Yue67bef42022-07-07 07:29:42 +00002881#endif
2882
Pengyu Lv9f926952022-11-17 15:22:33 +08002883 /* Set ticket_flags depends on the advertised psk key exchange mode */
Pengyu Lv80270b22023-01-12 11:54:04 +08002884 mbedtls_ssl_session_clear_ticket_flags(
Pengyu Lv9eacb442022-12-09 14:39:19 +08002885 session, MBEDTLS_SSL_TLS1_3_TICKET_FLAGS_MASK);
Pengyu Lve6487fe2022-12-06 09:30:29 +08002886#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED)
Pengyu Lv80270b22023-01-12 11:54:04 +08002887 mbedtls_ssl_session_set_ticket_flags(
Pengyu Lv9eacb442022-12-09 14:39:19 +08002888 session, ssl->handshake->tls13_kex_modes);
Pengyu Lve6487fe2022-12-06 09:30:29 +08002889#endif
Pengyu Lv4938a562023-01-16 11:28:49 +08002890 MBEDTLS_SSL_PRINT_TICKET_FLAGS(4, session->ticket_flags);
Pengyu Lv9f926952022-11-17 15:22:33 +08002891
Jerry Yue67bef42022-07-07 07:29:42 +00002892 /* Generate ticket_age_add */
Gilles Peskine449bd832023-01-11 14:50:10 +01002893 if ((ret = ssl->conf->f_rng(ssl->conf->p_rng,
2894 (unsigned char *) &session->ticket_age_add,
2895 sizeof(session->ticket_age_add)) != 0)) {
2896 MBEDTLS_SSL_DEBUG_RET(1, "generate_ticket_age_add", ret);
2897 return ret;
Jerry Yue67bef42022-07-07 07:29:42 +00002898 }
Gilles Peskine449bd832023-01-11 14:50:10 +01002899 MBEDTLS_SSL_DEBUG_MSG(3, ("ticket_age_add: %u",
2900 (unsigned int) session->ticket_age_add));
Jerry Yue67bef42022-07-07 07:29:42 +00002901
2902 /* Generate ticket_nonce */
Gilles Peskine449bd832023-01-11 14:50:10 +01002903 ret = ssl->conf->f_rng(ssl->conf->p_rng, ticket_nonce, ticket_nonce_size);
2904 if (ret != 0) {
2905 MBEDTLS_SSL_DEBUG_RET(1, "generate_ticket_nonce", ret);
2906 return ret;
Jerry Yue67bef42022-07-07 07:29:42 +00002907 }
Gilles Peskine449bd832023-01-11 14:50:10 +01002908 MBEDTLS_SSL_DEBUG_BUF(3, "ticket_nonce:",
2909 ticket_nonce, ticket_nonce_size);
Jerry Yue67bef42022-07-07 07:29:42 +00002910
2911 ciphersuite_info =
Gilles Peskine449bd832023-01-11 14:50:10 +01002912 (mbedtls_ssl_ciphersuite_t *) ssl->handshake->ciphersuite_info;
Dave Rodgman2eab4622023-10-05 13:30:37 +01002913 psa_hash_alg = mbedtls_md_psa_alg_from_type((mbedtls_md_type_t) ciphersuite_info->mac);
Gilles Peskine449bd832023-01-11 14:50:10 +01002914 hash_length = PSA_HASH_LENGTH(psa_hash_alg);
2915 if (hash_length == -1 ||
2916 (size_t) hash_length > sizeof(session->resumption_key)) {
2917 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Jerry Yufca4d572022-07-21 10:37:48 +08002918 }
2919
Jerry Yue67bef42022-07-07 07:29:42 +00002920 /* In this code the psk key length equals the length of the hash */
2921 session->resumption_key_len = hash_length;
2922 session->ciphersuite = ciphersuite_info->id;
2923
2924 /* Compute resumption key
2925 *
2926 * HKDF-Expand-Label( resumption_master_secret,
2927 * "resumption", ticket_nonce, Hash.length )
2928 */
2929 ret = mbedtls_ssl_tls13_hkdf_expand_label(
Gilles Peskine449bd832023-01-11 14:50:10 +01002930 psa_hash_alg,
2931 session->app_secrets.resumption_master_secret,
2932 hash_length,
2933 MBEDTLS_SSL_TLS1_3_LBL_WITH_LEN(resumption),
2934 ticket_nonce,
2935 ticket_nonce_size,
2936 session->resumption_key,
2937 hash_length);
Jerry Yue67bef42022-07-07 07:29:42 +00002938
Gilles Peskine449bd832023-01-11 14:50:10 +01002939 if (ret != 0) {
2940 MBEDTLS_SSL_DEBUG_RET(2,
2941 "Creating the ticket-resumed PSK failed",
2942 ret);
2943 return ret;
Jerry Yue67bef42022-07-07 07:29:42 +00002944 }
Gilles Peskine449bd832023-01-11 14:50:10 +01002945 MBEDTLS_SSL_DEBUG_BUF(3, "Ticket-resumed PSK",
2946 session->resumption_key,
2947 session->resumption_key_len);
Jerry Yue67bef42022-07-07 07:29:42 +00002948
Gilles Peskine449bd832023-01-11 14:50:10 +01002949 MBEDTLS_SSL_DEBUG_BUF(3, "resumption_master_secret",
2950 session->app_secrets.resumption_master_secret,
2951 hash_length);
Jerry Yue67bef42022-07-07 07:29:42 +00002952
Gilles Peskine449bd832023-01-11 14:50:10 +01002953 return 0;
Jerry Yue67bef42022-07-07 07:29:42 +00002954}
2955
2956/* This function creates a NewSessionTicket message in the following format:
2957 *
2958 * struct {
2959 * uint32 ticket_lifetime;
2960 * uint32 ticket_age_add;
2961 * opaque ticket_nonce<0..255>;
2962 * opaque ticket<1..2^16-1>;
2963 * Extension extensions<0..2^16-2>;
2964 * } NewSessionTicket;
2965 *
2966 * The ticket inside the NewSessionTicket message is an encrypted container
2967 * carrying the necessary information so that the server is later able to
2968 * re-start the communication.
2969 *
2970 * The following fields are placed inside the ticket by the
2971 * f_ticket_write() function:
2972 *
2973 * - creation time (start)
2974 * - flags (flags)
2975 * - age add (ticket_age_add)
2976 * - key (key)
2977 * - key length (key_len)
2978 * - ciphersuite (ciphersuite)
2979 */
2980MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002981static int ssl_tls13_write_new_session_ticket_body(mbedtls_ssl_context *ssl,
2982 unsigned char *buf,
2983 unsigned char *end,
2984 size_t *out_len,
2985 unsigned char *ticket_nonce,
2986 size_t ticket_nonce_size)
Jerry Yue67bef42022-07-07 07:29:42 +00002987{
2988 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
2989 unsigned char *p = buf;
2990 mbedtls_ssl_session *session = ssl->session;
2991 size_t ticket_len;
2992 uint32_t ticket_lifetime;
2993
2994 *out_len = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01002995 MBEDTLS_SSL_DEBUG_MSG(2, ("=> write NewSessionTicket msg"));
Jerry Yue67bef42022-07-07 07:29:42 +00002996
2997 /*
2998 * ticket_lifetime 4 bytes
2999 * ticket_age_add 4 bytes
3000 * ticket_nonce 1 + ticket_nonce_size bytes
3001 * ticket >=2 bytes
3002 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003003 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 4 + 4 + 1 + ticket_nonce_size + 2);
Jerry Yue67bef42022-07-07 07:29:42 +00003004
3005 /* Generate ticket and ticket_lifetime */
Gilles Peskine449bd832023-01-11 14:50:10 +01003006 ret = ssl->conf->f_ticket_write(ssl->conf->p_ticket,
3007 session,
3008 p + 9 + ticket_nonce_size + 2,
3009 end,
3010 &ticket_len,
3011 &ticket_lifetime);
3012 if (ret != 0) {
3013 MBEDTLS_SSL_DEBUG_RET(1, "write_ticket", ret);
3014 return ret;
Jerry Yue67bef42022-07-07 07:29:42 +00003015 }
3016 /* RFC 8446 4.6.1
3017 * ticket_lifetime: Indicates the lifetime in seconds as a 32-bit
3018 * unsigned integer in network byte order from the time of ticket
3019 * issuance. Servers MUST NOT use any value greater than
3020 * 604800 seconds (7 days). The value of zero indicates that the
3021 * ticket should be discarded immediately. Clients MUST NOT cache
3022 * tickets for longer than 7 days, regardless of the ticket_lifetime,
3023 * and MAY delete tickets earlier based on local policy. A server
3024 * MAY treat a ticket as valid for a shorter period of time than what
3025 * is stated in the ticket_lifetime.
3026 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003027 if (ticket_lifetime > 604800) {
Xiaokang Qian28af5012022-10-13 08:18:19 +00003028 ticket_lifetime = 604800;
Gilles Peskine449bd832023-01-11 14:50:10 +01003029 }
3030 MBEDTLS_PUT_UINT32_BE(ticket_lifetime, p, 0);
3031 MBEDTLS_SSL_DEBUG_MSG(3, ("ticket_lifetime: %u",
3032 (unsigned int) ticket_lifetime));
Jerry Yue67bef42022-07-07 07:29:42 +00003033
3034 /* Write ticket_age_add */
Gilles Peskine449bd832023-01-11 14:50:10 +01003035 MBEDTLS_PUT_UINT32_BE(session->ticket_age_add, p, 4);
3036 MBEDTLS_SSL_DEBUG_MSG(3, ("ticket_age_add: %u",
3037 (unsigned int) session->ticket_age_add));
Jerry Yue67bef42022-07-07 07:29:42 +00003038
3039 /* Write ticket_nonce */
Gilles Peskine449bd832023-01-11 14:50:10 +01003040 p[8] = (unsigned char) ticket_nonce_size;
3041 if (ticket_nonce_size > 0) {
3042 memcpy(p + 9, ticket_nonce, ticket_nonce_size);
Jerry Yue67bef42022-07-07 07:29:42 +00003043 }
3044 p += 9 + ticket_nonce_size;
3045
3046 /* Write ticket */
Gilles Peskine449bd832023-01-11 14:50:10 +01003047 MBEDTLS_PUT_UINT16_BE(ticket_len, p, 0);
Jerry Yue67bef42022-07-07 07:29:42 +00003048 p += 2;
Gilles Peskine449bd832023-01-11 14:50:10 +01003049 MBEDTLS_SSL_DEBUG_BUF(4, "ticket", p, ticket_len);
Jerry Yue67bef42022-07-07 07:29:42 +00003050 p += ticket_len;
3051
3052 /* Ticket Extensions
3053 *
3054 * Note: We currently don't have any extensions.
3055 * Set length to zero.
3056 */
Jerry Yuedab6372022-10-31 14:37:31 +08003057 ssl->handshake->sent_extensions = MBEDTLS_SSL_EXT_MASK_NONE;
3058
Gilles Peskine449bd832023-01-11 14:50:10 +01003059 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 2);
3060 MBEDTLS_PUT_UINT16_BE(0, p, 0);
Jerry Yue67bef42022-07-07 07:29:42 +00003061 p += 2;
3062
3063 *out_len = p - buf;
Gilles Peskine449bd832023-01-11 14:50:10 +01003064 MBEDTLS_SSL_DEBUG_BUF(4, "ticket", buf, *out_len);
3065 MBEDTLS_SSL_DEBUG_MSG(2, ("<= write new session ticket"));
Jerry Yue67bef42022-07-07 07:29:42 +00003066
Jerry Yu7de2ff02022-11-08 21:43:46 +08003067 MBEDTLS_SSL_PRINT_EXTS(
Gilles Peskine449bd832023-01-11 14:50:10 +01003068 3, MBEDTLS_SSL_HS_NEW_SESSION_TICKET, ssl->handshake->sent_extensions);
Jerry Yu4b8f2f72022-10-31 13:31:22 +08003069
Gilles Peskine449bd832023-01-11 14:50:10 +01003070 return 0;
Jerry Yue67bef42022-07-07 07:29:42 +00003071}
3072
3073/*
Jerry Yua8d3c502022-10-30 14:51:23 +08003074 * Handler for MBEDTLS_SSL_TLS1_3_NEW_SESSION_TICKET
Jerry Yue67bef42022-07-07 07:29:42 +00003075 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003076static int ssl_tls13_write_new_session_ticket(mbedtls_ssl_context *ssl)
Jerry Yue67bef42022-07-07 07:29:42 +00003077{
3078 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
3079
Gilles Peskine449bd832023-01-11 14:50:10 +01003080 MBEDTLS_SSL_PROC_CHK_NEG(ssl_tls13_write_new_session_ticket_coordinate(ssl));
Jerry Yue67bef42022-07-07 07:29:42 +00003081
Gilles Peskine449bd832023-01-11 14:50:10 +01003082 if (ret == SSL_NEW_SESSION_TICKET_WRITE) {
Jerry Yue67bef42022-07-07 07:29:42 +00003083 unsigned char ticket_nonce[MBEDTLS_SSL_TLS1_3_TICKET_NONCE_LENGTH];
3084 unsigned char *buf;
3085 size_t buf_len, msg_len;
3086
Gilles Peskine449bd832023-01-11 14:50:10 +01003087 MBEDTLS_SSL_PROC_CHK(ssl_tls13_prepare_new_session_ticket(
3088 ssl, ticket_nonce, sizeof(ticket_nonce)));
Jerry Yue67bef42022-07-07 07:29:42 +00003089
Xiaokang Qian9f1747b2023-03-30 02:50:04 +00003090 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_start_handshake_msg(
3091 ssl, MBEDTLS_SSL_HS_NEW_SESSION_TICKET,
3092 &buf, &buf_len));
Jerry Yue67bef42022-07-07 07:29:42 +00003093
Gilles Peskine449bd832023-01-11 14:50:10 +01003094 MBEDTLS_SSL_PROC_CHK(ssl_tls13_write_new_session_ticket_body(
3095 ssl, buf, buf + buf_len, &msg_len,
3096 ticket_nonce, sizeof(ticket_nonce)));
Jerry Yue67bef42022-07-07 07:29:42 +00003097
Gilles Peskine449bd832023-01-11 14:50:10 +01003098 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_finish_handshake_msg(
3099 ssl, buf_len, msg_len));
Jerry Yufca4d572022-07-21 10:37:48 +08003100
Jerry Yu359e65f2022-09-22 23:47:43 +08003101 /* Limit session tickets count to one when resumption connection.
3102 *
3103 * See document of mbedtls_ssl_conf_new_session_tickets.
3104 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003105 if (ssl->handshake->resume == 1) {
Jerry Yu359e65f2022-09-22 23:47:43 +08003106 ssl->handshake->new_session_tickets_count = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01003107 } else {
Jerry Yu359e65f2022-09-22 23:47:43 +08003108 ssl->handshake->new_session_tickets_count--;
Gilles Peskine449bd832023-01-11 14:50:10 +01003109 }
Jerry Yub7e3fa72022-09-22 11:07:18 +08003110
Jerry Yua8d3c502022-10-30 14:51:23 +08003111 mbedtls_ssl_handshake_set_state(
Gilles Peskine449bd832023-01-11 14:50:10 +01003112 ssl, MBEDTLS_SSL_TLS1_3_NEW_SESSION_TICKET_FLUSH);
3113 } else {
3114 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_HANDSHAKE_OVER);
Jerry Yue67bef42022-07-07 07:29:42 +00003115 }
3116
Jerry Yue67bef42022-07-07 07:29:42 +00003117cleanup:
3118
Gilles Peskine449bd832023-01-11 14:50:10 +01003119 return ret;
Jerry Yue67bef42022-07-07 07:29:42 +00003120}
3121#endif /* MBEDTLS_SSL_SESSION_TICKETS */
3122
3123/*
XiaokangQiane8ff3502022-04-22 02:34:40 +00003124 * TLS 1.3 State Machine -- server side
XiaokangQian7807f9f2022-02-15 10:04:37 +00003125 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003126int mbedtls_ssl_tls13_handshake_server_step(mbedtls_ssl_context *ssl)
Jerry Yub9930e72021-08-06 17:11:51 +08003127{
XiaokangQian08037552022-04-20 07:16:41 +00003128 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
XiaokangQian7807f9f2022-02-15 10:04:37 +00003129
Gilles Peskine449bd832023-01-11 14:50:10 +01003130 if (ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER || ssl->handshake == NULL) {
3131 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
3132 }
XiaokangQian7807f9f2022-02-15 10:04:37 +00003133
Gilles Peskine449bd832023-01-11 14:50:10 +01003134 MBEDTLS_SSL_DEBUG_MSG(2, ("tls13 server state: %s(%d)",
Dave Rodgman2eab4622023-10-05 13:30:37 +01003135 mbedtls_ssl_states_str((mbedtls_ssl_states) ssl->state),
Gilles Peskine449bd832023-01-11 14:50:10 +01003136 ssl->state));
Jerry Yu6e81b272021-09-27 11:16:17 +08003137
Gilles Peskine449bd832023-01-11 14:50:10 +01003138 switch (ssl->state) {
XiaokangQian7807f9f2022-02-15 10:04:37 +00003139 /* start state */
3140 case MBEDTLS_SSL_HELLO_REQUEST:
Gilles Peskine449bd832023-01-11 14:50:10 +01003141 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_CLIENT_HELLO);
XiaokangQian08037552022-04-20 07:16:41 +00003142 ret = 0;
XiaokangQian7807f9f2022-02-15 10:04:37 +00003143 break;
3144
XiaokangQian7807f9f2022-02-15 10:04:37 +00003145 case MBEDTLS_SSL_CLIENT_HELLO:
Gilles Peskine449bd832023-01-11 14:50:10 +01003146 ret = ssl_tls13_process_client_hello(ssl);
3147 if (ret != 0) {
3148 MBEDTLS_SSL_DEBUG_RET(1, "ssl_tls13_process_client_hello", ret);
3149 }
Jerry Yuf41553b2022-05-09 22:20:30 +08003150 break;
XiaokangQian7807f9f2022-02-15 10:04:37 +00003151
Jerry Yuf41553b2022-05-09 22:20:30 +08003152 case MBEDTLS_SSL_HELLO_RETRY_REQUEST:
Gilles Peskine449bd832023-01-11 14:50:10 +01003153 ret = ssl_tls13_write_hello_retry_request(ssl);
3154 if (ret != 0) {
3155 MBEDTLS_SSL_DEBUG_RET(1, "ssl_tls13_write_hello_retry_request", ret);
3156 return ret;
Jerry Yuf41553b2022-05-09 22:20:30 +08003157 }
XiaokangQian7807f9f2022-02-15 10:04:37 +00003158 break;
3159
Jerry Yu5b64ae92022-03-30 17:15:02 +08003160 case MBEDTLS_SSL_SERVER_HELLO:
Gilles Peskine449bd832023-01-11 14:50:10 +01003161 ret = ssl_tls13_write_server_hello(ssl);
Jerry Yu5b64ae92022-03-30 17:15:02 +08003162 break;
3163
Xiaofei Baicba64af2022-02-15 10:00:56 +00003164 case MBEDTLS_SSL_ENCRYPTED_EXTENSIONS:
Gilles Peskine449bd832023-01-11 14:50:10 +01003165 ret = ssl_tls13_write_encrypted_extensions(ssl);
3166 if (ret != 0) {
3167 MBEDTLS_SSL_DEBUG_RET(1, "ssl_tls13_write_encrypted_extensions", ret);
3168 return ret;
Xiaofei Baicba64af2022-02-15 10:00:56 +00003169 }
Jerry Yu48330562022-05-06 21:35:44 +08003170 break;
Jerry Yu6a2cd9e2022-05-05 11:14:19 +08003171
Ronald Cron928cbd32022-10-04 16:14:26 +02003172#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED)
Xiaofei Bai9ca09d42022-02-14 12:57:18 +00003173 case MBEDTLS_SSL_CERTIFICATE_REQUEST:
Gilles Peskine449bd832023-01-11 14:50:10 +01003174 ret = ssl_tls13_write_certificate_request(ssl);
Xiaofei Bai9ca09d42022-02-14 12:57:18 +00003175 break;
Xiaofei Bai9ca09d42022-02-14 12:57:18 +00003176
Jerry Yu83da34e2022-04-16 13:59:52 +08003177 case MBEDTLS_SSL_SERVER_CERTIFICATE:
Gilles Peskine449bd832023-01-11 14:50:10 +01003178 ret = ssl_tls13_write_server_certificate(ssl);
Jerry Yu83da34e2022-04-16 13:59:52 +08003179 break;
3180
3181 case MBEDTLS_SSL_CERTIFICATE_VERIFY:
Gilles Peskine449bd832023-01-11 14:50:10 +01003182 ret = ssl_tls13_write_certificate_verify(ssl);
Jerry Yu83da34e2022-04-16 13:59:52 +08003183 break;
Ronald Cron928cbd32022-10-04 16:14:26 +02003184#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED */
Jerry Yu83da34e2022-04-16 13:59:52 +08003185
Gilles Peskine449bd832023-01-11 14:50:10 +01003186 /*
3187 * Injection of dummy-CCS's for middlebox compatibility
3188 */
Gabor Mezei7b39bf12022-05-24 16:04:14 +02003189#if defined(MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE)
Gabor Mezeif7044ea2022-06-28 16:01:49 +02003190 case MBEDTLS_SSL_SERVER_CCS_AFTER_HELLO_RETRY_REQUEST:
Gilles Peskine449bd832023-01-11 14:50:10 +01003191 ret = mbedtls_ssl_tls13_write_change_cipher_spec(ssl);
3192 if (ret == 0) {
3193 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_CLIENT_HELLO);
3194 }
Gabor Mezei7b39bf12022-05-24 16:04:14 +02003195 break;
3196
3197 case MBEDTLS_SSL_SERVER_CCS_AFTER_SERVER_HELLO:
Gilles Peskine449bd832023-01-11 14:50:10 +01003198 ret = mbedtls_ssl_tls13_write_change_cipher_spec(ssl);
3199 if (ret == 0) {
3200 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_ENCRYPTED_EXTENSIONS);
3201 }
Gabor Mezei7b39bf12022-05-24 16:04:14 +02003202 break;
3203#endif /* MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE */
3204
Jerry Yu69dd8d42022-04-16 12:51:26 +08003205 case MBEDTLS_SSL_SERVER_FINISHED:
Gilles Peskine449bd832023-01-11 14:50:10 +01003206 ret = ssl_tls13_write_server_finished(ssl);
Jerry Yu69dd8d42022-04-16 12:51:26 +08003207 break;
3208
3209 case MBEDTLS_SSL_CLIENT_FINISHED:
Gilles Peskine449bd832023-01-11 14:50:10 +01003210 ret = ssl_tls13_process_client_finished(ssl);
Jerry Yu69dd8d42022-04-16 12:51:26 +08003211 break;
3212
Jerry Yu69dd8d42022-04-16 12:51:26 +08003213 case MBEDTLS_SSL_HANDSHAKE_WRAPUP:
Gilles Peskine449bd832023-01-11 14:50:10 +01003214 ret = ssl_tls13_handshake_wrapup(ssl);
Jerry Yu69dd8d42022-04-16 12:51:26 +08003215 break;
3216
Ronald Cron766c0cd2022-10-18 12:17:11 +02003217#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED)
XiaokangQian6b916b12022-04-25 07:29:34 +00003218 case MBEDTLS_SSL_CLIENT_CERTIFICATE:
Gilles Peskine449bd832023-01-11 14:50:10 +01003219 ret = mbedtls_ssl_tls13_process_certificate(ssl);
3220 if (ret == 0) {
3221 if (ssl->session_negotiate->peer_cert != NULL) {
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_CERTIFICATE_VERIFY);
3224 } else {
3225 MBEDTLS_SSL_DEBUG_MSG(2, ("skip parse certificate verify"));
Ronald Cron209cae92022-06-07 10:30:19 +02003226 mbedtls_ssl_handshake_set_state(
Gilles Peskine449bd832023-01-11 14:50:10 +01003227 ssl, MBEDTLS_SSL_CLIENT_FINISHED);
Ronald Cron19385882022-06-15 16:26:13 +02003228 }
XiaokangQian6b916b12022-04-25 07:29:34 +00003229 }
3230 break;
3231
3232 case MBEDTLS_SSL_CLIENT_CERTIFICATE_VERIFY:
Gilles Peskine449bd832023-01-11 14:50:10 +01003233 ret = mbedtls_ssl_tls13_process_certificate_verify(ssl);
3234 if (ret == 0) {
XiaokangQian6b916b12022-04-25 07:29:34 +00003235 mbedtls_ssl_handshake_set_state(
Gilles Peskine449bd832023-01-11 14:50:10 +01003236 ssl, MBEDTLS_SSL_CLIENT_FINISHED);
XiaokangQian6b916b12022-04-25 07:29:34 +00003237 }
3238 break;
Ronald Cron766c0cd2022-10-18 12:17:11 +02003239#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED */
XiaokangQian6b916b12022-04-25 07:29:34 +00003240
Jerry Yue67bef42022-07-07 07:29:42 +00003241#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Jerry Yua8d3c502022-10-30 14:51:23 +08003242 case MBEDTLS_SSL_TLS1_3_NEW_SESSION_TICKET:
Gilles Peskine449bd832023-01-11 14:50:10 +01003243 ret = ssl_tls13_write_new_session_ticket(ssl);
3244 if (ret != 0) {
3245 MBEDTLS_SSL_DEBUG_RET(1,
3246 "ssl_tls13_write_new_session_ticket ",
3247 ret);
Jerry Yue67bef42022-07-07 07:29:42 +00003248 }
3249 break;
Jerry Yua8d3c502022-10-30 14:51:23 +08003250 case MBEDTLS_SSL_TLS1_3_NEW_SESSION_TICKET_FLUSH:
Jerry Yue67bef42022-07-07 07:29:42 +00003251 /* This state is necessary to do the flush of the New Session
Jerry Yua8d3c502022-10-30 14:51:23 +08003252 * Ticket message written in MBEDTLS_SSL_TLS1_3_NEW_SESSION_TICKET
Jerry Yue67bef42022-07-07 07:29:42 +00003253 * as part of ssl_prepare_handshake_step.
3254 */
3255 ret = 0;
Jerry Yud4e75002022-08-09 13:33:50 +08003256
Gilles Peskine449bd832023-01-11 14:50:10 +01003257 if (ssl->handshake->new_session_tickets_count == 0) {
3258 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_HANDSHAKE_OVER);
3259 } else {
Xiaokang Qian9f1747b2023-03-30 02:50:04 +00003260 mbedtls_ssl_handshake_set_state(
3261 ssl, MBEDTLS_SSL_TLS1_3_NEW_SESSION_TICKET);
Gilles Peskine449bd832023-01-11 14:50:10 +01003262 }
Jerry Yue67bef42022-07-07 07:29:42 +00003263 break;
3264
3265#endif /* MBEDTLS_SSL_SESSION_TICKETS */
3266
XiaokangQian7807f9f2022-02-15 10:04:37 +00003267 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01003268 MBEDTLS_SSL_DEBUG_MSG(1, ("invalid state %d", ssl->state));
3269 return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
XiaokangQian7807f9f2022-02-15 10:04:37 +00003270 }
3271
Gilles Peskine449bd832023-01-11 14:50:10 +01003272 return ret;
Jerry Yub9930e72021-08-06 17:11:51 +08003273}
Jerry Yu3cc4c2a2021-08-06 16:29:08 +08003274
Jerry Yufb4b6472022-01-27 15:03:26 +08003275#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_PROTO_TLS1_3 */