blob: 08ed55c3e6cb168e1a3aa5eee36fc4c49e0c9e0d [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
5 * SPDX-License-Identifier: Apache-2.0
6 *
7 * Licensed under the Apache License, Version 2.0 (the "License"); you may
8 * not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
15 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
Gilles Peskine449bd832023-01-11 14:50:10 +010018 */
Jerry Yu3cc4c2a2021-08-06 16:29:08 +080019
20#include "common.h"
21
Jerry Yufb4b6472022-01-27 15:03:26 +080022#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_PROTO_TLS1_3)
Jerry Yu3cc4c2a2021-08-06 16:29:08 +080023
Jerry Yu687101b2021-09-14 16:03:56 +080024#include "mbedtls/debug.h"
Xiaofei Baicba64af2022-02-15 10:00:56 +000025#include "mbedtls/error.h"
26#include "mbedtls/platform.h"
Jerry Yu1c105562022-07-10 06:32:38 +000027#include "mbedtls/constant_time.h"
Manuel Pégourié-Gonnardd55d66f2023-06-20 10:14:58 +020028#include "mbedtls/oid.h"
Manuel Pégourié-Gonnard02b10d82023-03-28 12:33:20 +020029#include "md_psa.h"
Jerry Yu3bf2c642022-03-30 22:02:12 +080030
Xiaofei Bai9ca09d42022-02-14 12:57:18 +000031#include "ssl_misc.h"
32#include "ssl_tls13_keys.h"
33#include "ssl_debug_helpers.h"
34
Jerry Yuf35ba382022-08-23 17:58:26 +080035
Jerry Yuc5a23a02022-08-25 10:51:44 +080036static const mbedtls_ssl_ciphersuite_t *ssl_tls13_validate_peer_ciphersuite(
Gilles Peskine449bd832023-01-11 14:50:10 +010037 mbedtls_ssl_context *ssl,
38 unsigned int cipher_suite)
Jerry Yuf35ba382022-08-23 17:58:26 +080039{
40 const mbedtls_ssl_ciphersuite_t *ciphersuite_info;
Gilles Peskine449bd832023-01-11 14:50:10 +010041 if (!mbedtls_ssl_tls13_cipher_suite_is_offered(ssl, cipher_suite)) {
42 return NULL;
Jerry Yuf35ba382022-08-23 17:58:26 +080043 }
Gilles Peskine449bd832023-01-11 14:50:10 +010044
45 ciphersuite_info = mbedtls_ssl_ciphersuite_from_id(cipher_suite);
46 if ((mbedtls_ssl_validate_ciphersuite(ssl, ciphersuite_info,
47 ssl->tls_version,
48 ssl->tls_version) != 0)) {
49 return NULL;
50 }
51 return ciphersuite_info;
Jerry Yuf35ba382022-08-23 17:58:26 +080052}
53
Ronald Cron41a443a2022-10-04 16:38:25 +020054#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED)
Jerry Yue19e3b92022-07-08 12:04:51 +000055/* From RFC 8446:
56 *
57 * enum { psk_ke(0), psk_dhe_ke(1), (255) } PskKeyExchangeMode;
58 * struct {
59 * PskKeyExchangeMode ke_modes<1..255>;
60 * } PskKeyExchangeModes;
61 */
Jerry Yu6e74a7e2022-07-20 20:49:32 +080062MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +010063static int ssl_tls13_parse_key_exchange_modes_ext(mbedtls_ssl_context *ssl,
64 const unsigned char *buf,
65 const unsigned char *end)
Jerry Yue19e3b92022-07-08 12:04:51 +000066{
Jerry Yu299e31f2022-07-13 23:06:36 +080067 const unsigned char *p = buf;
Jerry Yue19e3b92022-07-08 12:04:51 +000068 size_t ke_modes_len;
69 int ke_modes = 0;
70
Jerry Yu854dd9e2022-07-15 14:28:27 +080071 /* Read ke_modes length (1 Byte) */
Gilles Peskine449bd832023-01-11 14:50:10 +010072 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, 1);
Jerry Yu299e31f2022-07-13 23:06:36 +080073 ke_modes_len = *p++;
Jerry Yue19e3b92022-07-08 12:04:51 +000074 /* Currently, there are only two PSK modes, so even without looking
75 * at the content, something's wrong if the list has more than 2 items. */
Gilles Peskine449bd832023-01-11 14:50:10 +010076 if (ke_modes_len > 2) {
77 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER,
78 MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER);
79 return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
Jerry Yu299e31f2022-07-13 23:06:36 +080080 }
Jerry Yue19e3b92022-07-08 12:04:51 +000081
Gilles Peskine449bd832023-01-11 14:50:10 +010082 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, ke_modes_len);
Jerry Yue19e3b92022-07-08 12:04:51 +000083
Gilles Peskine449bd832023-01-11 14:50:10 +010084 while (ke_modes_len-- != 0) {
85 switch (*p++) {
86 case MBEDTLS_SSL_TLS1_3_PSK_MODE_PURE:
87 ke_modes |= MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK;
88 MBEDTLS_SSL_DEBUG_MSG(3, ("Found PSK KEX MODE"));
89 break;
90 case MBEDTLS_SSL_TLS1_3_PSK_MODE_ECDHE:
91 ke_modes |= MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK_EPHEMERAL;
92 MBEDTLS_SSL_DEBUG_MSG(3, ("Found PSK_EPHEMERAL KEX MODE"));
93 break;
94 default:
95 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER,
96 MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER);
97 return MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER;
Jerry Yue19e3b92022-07-08 12:04:51 +000098 }
99 }
100
101 ssl->handshake->tls13_kex_modes = ke_modes;
Gilles Peskine449bd832023-01-11 14:50:10 +0100102 return 0;
Jerry Yue19e3b92022-07-08 12:04:51 +0000103}
Jerry Yu1c105562022-07-10 06:32:38 +0000104
Jerry Yue9d4fc02022-08-20 19:21:15 +0800105#define SSL_TLS1_3_OFFERED_PSK_NOT_MATCH 1
106#define SSL_TLS1_3_OFFERED_PSK_MATCH 0
Jerry Yu95699e72022-08-21 19:22:23 +0800107
108#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Pengyu Lv29daf4a2023-10-30 17:13:30 +0800109MBEDTLS_CHECK_RETURN_CRITICAL
110static int ssl_tls13_check_psk_key_exchange(mbedtls_ssl_context *ssl);
111MBEDTLS_CHECK_RETURN_CRITICAL
112static int ssl_tls13_check_psk_ephemeral_key_exchange(mbedtls_ssl_context *ssl);
Jerry Yu95699e72022-08-21 19:22:23 +0800113
114MBEDTLS_CHECK_RETURN_CRITICAL
115static int ssl_tls13_offered_psks_check_identity_match_ticket(
Gilles Peskine449bd832023-01-11 14:50:10 +0100116 mbedtls_ssl_context *ssl,
117 const unsigned char *identity,
118 size_t identity_len,
119 uint32_t obfuscated_ticket_age,
120 mbedtls_ssl_session *session)
Jerry Yu95699e72022-08-21 19:22:23 +0800121{
Jerry Yufd310eb2022-09-06 09:16:35 +0800122 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yu95699e72022-08-21 19:22:23 +0800123 unsigned char *ticket_buffer;
Pengyu Lv29daf4a2023-10-30 17:13:30 +0800124 unsigned int ticket_flags;
125 unsigned int key_exchanges;
Jerry Yu8d4bbba2022-09-13 14:15:48 +0800126#if defined(MBEDTLS_HAVE_TIME)
127 mbedtls_time_t now;
Jerry Yuacff8232022-09-14 14:35:11 +0800128 uint64_t age_in_s;
Jerry Yuf7dad3c2022-09-14 22:31:39 +0800129 int64_t age_diff_in_ms;
Jerry Yu8d4bbba2022-09-13 14:15:48 +0800130#endif
Jerry Yu95699e72022-08-21 19:22:23 +0800131
132 ((void) obfuscated_ticket_age);
133
Gilles Peskine449bd832023-01-11 14:50:10 +0100134 MBEDTLS_SSL_DEBUG_MSG(2, ("=> check_identity_match_ticket"));
Jerry Yu95699e72022-08-21 19:22:23 +0800135
Jerry Yufd310eb2022-09-06 09:16:35 +0800136 /* Ticket parser is not configured, Skip */
Gilles Peskine449bd832023-01-11 14:50:10 +0100137 if (ssl->conf->f_ticket_parse == NULL || identity_len == 0) {
138 return 0;
139 }
Jerry Yu95699e72022-08-21 19:22:23 +0800140
Jerry Yu4746b102022-09-13 11:11:48 +0800141 /* We create a copy of the encrypted ticket since the ticket parsing
142 * function is allowed to use its input buffer as an output buffer
143 * (in-place decryption). We do, however, need the original buffer for
144 * computing the PSK binder value.
Jerry Yu95699e72022-08-21 19:22:23 +0800145 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100146 ticket_buffer = mbedtls_calloc(1, identity_len);
147 if (ticket_buffer == NULL) {
148 MBEDTLS_SSL_DEBUG_MSG(1, ("buffer too small"));
149 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
Jerry Yu95699e72022-08-21 19:22:23 +0800150 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100151 memcpy(ticket_buffer, identity, identity_len);
Jerry Yu95699e72022-08-21 19:22:23 +0800152
Gilles Peskine449bd832023-01-11 14:50:10 +0100153 if ((ret = ssl->conf->f_ticket_parse(ssl->conf->p_ticket,
154 session,
155 ticket_buffer, identity_len)) != 0) {
156 if (ret == MBEDTLS_ERR_SSL_INVALID_MAC) {
157 MBEDTLS_SSL_DEBUG_MSG(3, ("ticket is not authentic"));
158 } else if (ret == MBEDTLS_ERR_SSL_SESSION_TICKET_EXPIRED) {
159 MBEDTLS_SSL_DEBUG_MSG(3, ("ticket is expired"));
160 } else {
161 MBEDTLS_SSL_DEBUG_RET(1, "ticket_parse", ret);
162 }
Jerry Yu95699e72022-08-21 19:22:23 +0800163 }
164
165 /* We delete the temporary buffer */
Gilles Peskine449bd832023-01-11 14:50:10 +0100166 mbedtls_free(ticket_buffer);
Jerry Yu95699e72022-08-21 19:22:23 +0800167
Gilles Peskine449bd832023-01-11 14:50:10 +0100168 if (ret != 0) {
Jerry Yu8d4bbba2022-09-13 14:15:48 +0800169 goto exit;
170 }
Jerry Yu95699e72022-08-21 19:22:23 +0800171
Pengyu Lv3eb49be2022-12-05 16:35:12 +0800172 /* RFC 8446 section 4.2.9
173 *
174 * Servers SHOULD NOT send NewSessionTicket with tickets that are not
175 * compatible with the advertised modes; however, if a server does so,
176 * the impact will just be that the client's attempts at resumption fail.
177 *
178 * We regard the ticket with incompatible key exchange modes as not match.
179 */
Pengyu Lv76679682023-02-02 15:04:27 +0800180 ret = MBEDTLS_ERR_ERROR_GENERIC_ERROR;
Pengyu Lv29daf4a2023-10-30 17:13:30 +0800181 MBEDTLS_SSL_PRINT_TICKET_FLAGS(4, session->ticket_flags);
182 ticket_flags = mbedtls_ssl_session_get_ticket_flags(
183 session, MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK_ALL);
184
185 key_exchanges = 0;
186 if ((ticket_flags & MBEDTLS_SSL_TLS1_3_TICKET_ALLOW_PSK_EPHEMERAL_RESUMPTION) &&
187 ssl_tls13_check_psk_ephemeral_key_exchange(ssl)) {
188 key_exchanges |= MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK_EPHEMERAL;
189 }
190 if ((ticket_flags & MBEDTLS_SSL_TLS1_3_TICKET_ALLOW_PSK_RESUMPTION) &&
191 ssl_tls13_check_psk_key_exchange(ssl)) {
192 key_exchanges |= MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK;
193 }
194
195 if (key_exchanges == 0) {
Pengyu Lv3eb49be2022-12-05 16:35:12 +0800196 MBEDTLS_SSL_DEBUG_MSG(3, ("No suitable key exchange mode"));
197 goto exit;
198 }
199
Gilles Peskine449bd832023-01-11 14:50:10 +0100200 ret = MBEDTLS_ERR_SSL_SESSION_TICKET_EXPIRED;
201#if defined(MBEDTLS_HAVE_TIME)
202 now = mbedtls_time(NULL);
203
204 if (now < session->start) {
205 MBEDTLS_SSL_DEBUG_MSG(
206 3, ("Invalid ticket start time ( now=%" MBEDTLS_PRINTF_LONGLONG
207 ", start=%" MBEDTLS_PRINTF_LONGLONG " )",
208 (long long) now, (long long) session->start));
209 goto exit;
210 }
211
212 age_in_s = (uint64_t) (now - session->start);
Jerry Yu95699e72022-08-21 19:22:23 +0800213
Jerry Yu8d4bbba2022-09-13 14:15:48 +0800214 /* RFC 8446 section 4.6.1
215 *
216 * Servers MUST NOT use any value greater than 604800 seconds (7 days).
217 *
218 * RFC 8446 section 4.2.11.1
219 *
220 * Clients MUST NOT attempt to use tickets which have ages greater than
221 * the "ticket_lifetime" value which was provided with the ticket.
222 *
223 * For time being, the age MUST be less than 604800 seconds (7 days).
224 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100225 if (age_in_s > 604800) {
Jerry Yu8d4bbba2022-09-13 14:15:48 +0800226 MBEDTLS_SSL_DEBUG_MSG(
Gilles Peskine449bd832023-01-11 14:50:10 +0100227 3, ("Ticket age exceeds limitation ticket_age=%lu",
228 (long unsigned int) age_in_s));
Jerry Yu8d4bbba2022-09-13 14:15:48 +0800229 goto exit;
230 }
Jerry Yu95699e72022-08-21 19:22:23 +0800231
Jerry Yu8d4bbba2022-09-13 14:15:48 +0800232 /* RFC 8446 section 4.2.10
233 *
234 * For PSKs provisioned via NewSessionTicket, a server MUST validate that
235 * the ticket age for the selected PSK identity (computed by subtracting
236 * ticket_age_add from PskIdentity.obfuscated_ticket_age modulo 2^32) is
237 * within a small tolerance of the time since the ticket was issued.
Jerry Yuf7dad3c2022-09-14 22:31:39 +0800238 *
Jerry Yu0a55cc62022-09-15 16:15:06 +0800239 * NOTE: When `now == session->start`, `age_diff_in_ms` may be negative
240 * as the age units are different on the server (s) and in the
241 * client (ms) side. Add a -1000 ms tolerance window to take this
242 * into account.
Jerry Yu8d4bbba2022-09-13 14:15:48 +0800243 */
Jerry Yuf7dad3c2022-09-14 22:31:39 +0800244 age_diff_in_ms = age_in_s * 1000;
Gilles Peskine449bd832023-01-11 14:50:10 +0100245 age_diff_in_ms -= (obfuscated_ticket_age - session->ticket_age_add);
246 if (age_diff_in_ms <= -1000 ||
247 age_diff_in_ms > MBEDTLS_SSL_TLS1_3_TICKET_AGE_TOLERANCE) {
Jerry Yu8d4bbba2022-09-13 14:15:48 +0800248 MBEDTLS_SSL_DEBUG_MSG(
Gilles Peskine449bd832023-01-11 14:50:10 +0100249 3, ("Ticket age outside tolerance window ( diff=%d )",
250 (int) age_diff_in_ms));
Jerry Yu8d4bbba2022-09-13 14:15:48 +0800251 goto exit;
252 }
253
254 ret = 0;
Jerry Yu95699e72022-08-21 19:22:23 +0800255
256#endif /* MBEDTLS_HAVE_TIME */
Jerry Yu8d4bbba2022-09-13 14:15:48 +0800257
258exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100259 if (ret != 0) {
260 mbedtls_ssl_session_free(session);
261 }
Jerry Yu95699e72022-08-21 19:22:23 +0800262
Gilles Peskine449bd832023-01-11 14:50:10 +0100263 MBEDTLS_SSL_DEBUG_MSG(2, ("<= check_identity_match_ticket"));
264 return ret;
Jerry Yu95699e72022-08-21 19:22:23 +0800265}
266#endif /* MBEDTLS_SSL_SESSION_TICKETS */
267
Jerry Yu6e74a7e2022-07-20 20:49:32 +0800268MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yu1c105562022-07-10 06:32:38 +0000269static int ssl_tls13_offered_psks_check_identity_match(
Gilles Peskine449bd832023-01-11 14:50:10 +0100270 mbedtls_ssl_context *ssl,
271 const unsigned char *identity,
272 size_t identity_len,
273 uint32_t obfuscated_ticket_age,
274 int *psk_type,
275 mbedtls_ssl_session *session)
Jerry Yu1c105562022-07-10 06:32:38 +0000276{
Ronald Cron606671e2023-02-17 11:36:33 +0100277 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
278
Jerry Yu95699e72022-08-21 19:22:23 +0800279 ((void) session);
280 ((void) obfuscated_ticket_age);
Jerry Yu5725f1c2022-08-21 17:27:16 +0800281 *psk_type = MBEDTLS_SSL_TLS1_3_PSK_EXTERNAL;
Jerry Yu95699e72022-08-21 19:22:23 +0800282
Gilles Peskine449bd832023-01-11 14:50:10 +0100283 MBEDTLS_SSL_DEBUG_BUF(4, "identity", identity, identity_len);
Jerry Yu95699e72022-08-21 19:22:23 +0800284 ssl->handshake->resume = 0;
285
Jerry Yu95699e72022-08-21 19:22:23 +0800286#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Gilles Peskine449bd832023-01-11 14:50:10 +0100287 if (ssl_tls13_offered_psks_check_identity_match_ticket(
Jerry Yu8d4bbba2022-09-13 14:15:48 +0800288 ssl, identity, identity_len, obfuscated_ticket_age,
Gilles Peskine449bd832023-01-11 14:50:10 +0100289 session) == SSL_TLS1_3_OFFERED_PSK_MATCH) {
Jerry Yu95699e72022-08-21 19:22:23 +0800290 ssl->handshake->resume = 1;
291 *psk_type = MBEDTLS_SSL_TLS1_3_PSK_RESUMPTION;
Ronald Cron606671e2023-02-17 11:36:33 +0100292 ret = mbedtls_ssl_set_hs_psk(ssl,
293 session->resumption_key,
294 session->resumption_key_len);
295 if (ret != 0) {
296 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_set_hs_psk", ret);
297 return ret;
298 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100299
300 MBEDTLS_SSL_DEBUG_BUF(4, "Ticket-resumed PSK:",
301 session->resumption_key,
302 session->resumption_key_len);
303 MBEDTLS_SSL_DEBUG_MSG(4, ("ticket: obfuscated_ticket_age: %u",
304 (unsigned) obfuscated_ticket_age));
305 return SSL_TLS1_3_OFFERED_PSK_MATCH;
Jerry Yu95699e72022-08-21 19:22:23 +0800306 }
307#endif /* MBEDTLS_SSL_SESSION_TICKETS */
308
Jerry Yu1c105562022-07-10 06:32:38 +0000309 /* Check identity with external configured function */
Gilles Peskine449bd832023-01-11 14:50:10 +0100310 if (ssl->conf->f_psk != NULL) {
311 if (ssl->conf->f_psk(
312 ssl->conf->p_psk, ssl, identity, identity_len) == 0) {
313 return SSL_TLS1_3_OFFERED_PSK_MATCH;
Jerry Yu1c105562022-07-10 06:32:38 +0000314 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100315 return SSL_TLS1_3_OFFERED_PSK_NOT_MATCH;
Jerry Yu1c105562022-07-10 06:32:38 +0000316 }
317
Gilles Peskine449bd832023-01-11 14:50:10 +0100318 MBEDTLS_SSL_DEBUG_BUF(5, "identity", identity, identity_len);
Jerry Yu1c105562022-07-10 06:32:38 +0000319 /* Check identity with pre-configured psk */
Gilles Peskine449bd832023-01-11 14:50:10 +0100320 if (ssl->conf->psk_identity != NULL &&
Jerry Yu2f0abc92022-07-22 19:34:48 +0800321 identity_len == ssl->conf->psk_identity_len &&
Gilles Peskine449bd832023-01-11 14:50:10 +0100322 mbedtls_ct_memcmp(ssl->conf->psk_identity,
323 identity, identity_len) == 0) {
Ronald Cron606671e2023-02-17 11:36:33 +0100324 ret = mbedtls_ssl_set_hs_psk(ssl, ssl->conf->psk, ssl->conf->psk_len);
325 if (ret != 0) {
326 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_set_hs_psk", ret);
327 return ret;
328 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100329 return SSL_TLS1_3_OFFERED_PSK_MATCH;
Jerry Yu1c105562022-07-10 06:32:38 +0000330 }
331
Gilles Peskine449bd832023-01-11 14:50:10 +0100332 return SSL_TLS1_3_OFFERED_PSK_NOT_MATCH;
Jerry Yu1c105562022-07-10 06:32:38 +0000333}
334
Jerry Yu6e74a7e2022-07-20 20:49:32 +0800335MBEDTLS_CHECK_RETURN_CRITICAL
Xiaokang Qian9f1747b2023-03-30 02:50:04 +0000336static int ssl_tls13_offered_psks_check_binder_match(
337 mbedtls_ssl_context *ssl,
338 const unsigned char *binder, size_t binder_len,
339 int psk_type, psa_algorithm_t psk_hash_alg)
Jerry Yu1c105562022-07-10 06:32:38 +0000340{
341 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yu96a2e362022-07-21 15:11:34 +0800342
Jerry Yudaf375a2022-07-20 21:31:43 +0800343 unsigned char transcript[PSA_HASH_MAX_SIZE];
Jerry Yu1c105562022-07-10 06:32:38 +0000344 size_t transcript_len;
Jerry Yu2f0abc92022-07-22 19:34:48 +0800345 unsigned char *psk;
Jerry Yu96a2e362022-07-21 15:11:34 +0800346 size_t psk_len;
Jerry Yudaf375a2022-07-20 21:31:43 +0800347 unsigned char server_computed_binder[PSA_HASH_MAX_SIZE];
Jerry Yu1c105562022-07-10 06:32:38 +0000348
Jerry Yu1c105562022-07-10 06:32:38 +0000349 /* Get current state of handshake transcript. */
Jerry Yu29d9faa2022-08-23 17:52:45 +0800350 ret = mbedtls_ssl_get_handshake_transcript(
Manuel Pégourié-Gonnard2d6d9932023-03-28 11:38:08 +0200351 ssl, mbedtls_md_type_from_psa_alg(psk_hash_alg),
Gilles Peskine449bd832023-01-11 14:50:10 +0100352 transcript, sizeof(transcript), &transcript_len);
353 if (ret != 0) {
354 return ret;
355 }
Jerry Yu1c105562022-07-10 06:32:38 +0000356
Gilles Peskine449bd832023-01-11 14:50:10 +0100357 ret = mbedtls_ssl_tls13_export_handshake_psk(ssl, &psk, &psk_len);
358 if (ret != 0) {
359 return ret;
360 }
Jerry Yu96a2e362022-07-21 15:11:34 +0800361
Gilles Peskine449bd832023-01-11 14:50:10 +0100362 ret = mbedtls_ssl_tls13_create_psk_binder(ssl, psk_hash_alg,
363 psk, psk_len, psk_type,
364 transcript,
365 server_computed_binder);
Jerry Yu96a2e362022-07-21 15:11:34 +0800366#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +0100367 mbedtls_free((void *) psk);
Jerry Yu96a2e362022-07-21 15:11:34 +0800368#endif
Gilles Peskine449bd832023-01-11 14:50:10 +0100369 if (ret != 0) {
370 MBEDTLS_SSL_DEBUG_MSG(1, ("PSK binder calculation failed."));
371 return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
Jerry Yu1c105562022-07-10 06:32:38 +0000372 }
373
Gilles Peskine449bd832023-01-11 14:50:10 +0100374 MBEDTLS_SSL_DEBUG_BUF(3, "psk binder ( computed ): ",
375 server_computed_binder, transcript_len);
376 MBEDTLS_SSL_DEBUG_BUF(3, "psk binder ( received ): ", binder, binder_len);
Jerry Yu1c105562022-07-10 06:32:38 +0000377
Gilles Peskine449bd832023-01-11 14:50:10 +0100378 if (mbedtls_ct_memcmp(server_computed_binder, binder, binder_len) == 0) {
379 return SSL_TLS1_3_OFFERED_PSK_MATCH;
Jerry Yu1c105562022-07-10 06:32:38 +0000380 }
381
Gilles Peskine449bd832023-01-11 14:50:10 +0100382 mbedtls_platform_zeroize(server_computed_binder,
383 sizeof(server_computed_binder));
384 return SSL_TLS1_3_OFFERED_PSK_NOT_MATCH;
Jerry Yu1c105562022-07-10 06:32:38 +0000385}
Jerry Yu96a2e362022-07-21 15:11:34 +0800386
Jerry Yu5725f1c2022-08-21 17:27:16 +0800387MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yuf35ba382022-08-23 17:58:26 +0800388static int ssl_tls13_select_ciphersuite_for_psk(
Gilles Peskine449bd832023-01-11 14:50:10 +0100389 mbedtls_ssl_context *ssl,
390 const unsigned char *cipher_suites,
391 const unsigned char *cipher_suites_end,
392 uint16_t *selected_ciphersuite,
393 const mbedtls_ssl_ciphersuite_t **selected_ciphersuite_info)
Jerry Yu5725f1c2022-08-21 17:27:16 +0800394{
Jerry Yuf35ba382022-08-23 17:58:26 +0800395 psa_algorithm_t psk_hash_alg = PSA_ALG_SHA_256;
Jerry Yu5725f1c2022-08-21 17:27:16 +0800396
Jerry Yu0baf9072022-08-25 11:21:04 +0800397 *selected_ciphersuite = 0;
398 *selected_ciphersuite_info = NULL;
399
Jerry Yuf35ba382022-08-23 17:58:26 +0800400 /* RFC 8446, page 55.
401 *
402 * For externally established PSKs, the Hash algorithm MUST be set when the
403 * PSK is established or default to SHA-256 if no such algorithm is defined.
404 *
405 */
Jerry Yu5725f1c2022-08-21 17:27:16 +0800406
Jerry Yu5725f1c2022-08-21 17:27:16 +0800407 /*
408 * Search for a matching ciphersuite
409 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100410 for (const unsigned char *p = cipher_suites;
411 p < cipher_suites_end; p += 2) {
Jerry Yu5725f1c2022-08-21 17:27:16 +0800412 uint16_t cipher_suite;
Jerry Yuf35ba382022-08-23 17:58:26 +0800413 const mbedtls_ssl_ciphersuite_t *ciphersuite_info;
Jerry Yu5725f1c2022-08-21 17:27:16 +0800414
Gilles Peskine449bd832023-01-11 14:50:10 +0100415 cipher_suite = MBEDTLS_GET_UINT16_BE(p, 0);
416 ciphersuite_info = ssl_tls13_validate_peer_ciphersuite(ssl,
417 cipher_suite);
418 if (ciphersuite_info == NULL) {
Jerry Yu5725f1c2022-08-21 17:27:16 +0800419 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +0100420 }
Jerry Yu5725f1c2022-08-21 17:27:16 +0800421
Jerry Yu5725f1c2022-08-21 17:27:16 +0800422 /* MAC of selected ciphersuite MUST be same with PSK binder if exist.
423 * Otherwise, client should reject.
424 */
Dave Rodgman2eab4622023-10-05 13:30:37 +0100425 if (psk_hash_alg ==
426 mbedtls_md_psa_alg_from_type((mbedtls_md_type_t) ciphersuite_info->mac)) {
Jerry Yuf35ba382022-08-23 17:58:26 +0800427 *selected_ciphersuite = cipher_suite;
428 *selected_ciphersuite_info = ciphersuite_info;
Gilles Peskine449bd832023-01-11 14:50:10 +0100429 return 0;
Jerry Yuf35ba382022-08-23 17:58:26 +0800430 }
431 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100432 MBEDTLS_SSL_DEBUG_MSG(2, ("No matched ciphersuite"));
433 return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
Jerry Yuf35ba382022-08-23 17:58:26 +0800434}
Jerry Yu5725f1c2022-08-21 17:27:16 +0800435
Jerry Yu82534862022-08-30 10:42:33 +0800436#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Jerry Yuf35ba382022-08-23 17:58:26 +0800437MBEDTLS_CHECK_RETURN_CRITICAL
438static int ssl_tls13_select_ciphersuite_for_resumption(
Gilles Peskine449bd832023-01-11 14:50:10 +0100439 mbedtls_ssl_context *ssl,
440 const unsigned char *cipher_suites,
441 const unsigned char *cipher_suites_end,
442 mbedtls_ssl_session *session,
443 uint16_t *selected_ciphersuite,
444 const mbedtls_ssl_ciphersuite_t **selected_ciphersuite_info)
Jerry Yuf35ba382022-08-23 17:58:26 +0800445{
Jerry Yu82534862022-08-30 10:42:33 +0800446
Jerry Yuf35ba382022-08-23 17:58:26 +0800447 *selected_ciphersuite = 0;
448 *selected_ciphersuite_info = NULL;
Gilles Peskine449bd832023-01-11 14:50:10 +0100449 for (const unsigned char *p = cipher_suites; p < cipher_suites_end; p += 2) {
450 uint16_t cipher_suite = MBEDTLS_GET_UINT16_BE(p, 0);
Jerry Yu82534862022-08-30 10:42:33 +0800451 const mbedtls_ssl_ciphersuite_t *ciphersuite_info;
452
Gilles Peskine449bd832023-01-11 14:50:10 +0100453 if (cipher_suite != session->ciphersuite) {
Jerry Yu82534862022-08-30 10:42:33 +0800454 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +0100455 }
Jerry Yu82534862022-08-30 10:42:33 +0800456
Gilles Peskine449bd832023-01-11 14:50:10 +0100457 ciphersuite_info = ssl_tls13_validate_peer_ciphersuite(ssl,
458 cipher_suite);
459 if (ciphersuite_info == NULL) {
Jerry Yu82534862022-08-30 10:42:33 +0800460 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +0100461 }
Jerry Yu82534862022-08-30 10:42:33 +0800462
Jerry Yu4746b102022-09-13 11:11:48 +0800463 *selected_ciphersuite = cipher_suite;
Jerry Yu82534862022-08-30 10:42:33 +0800464 *selected_ciphersuite_info = ciphersuite_info;
465
Gilles Peskine449bd832023-01-11 14:50:10 +0100466 return 0;
Jerry Yu82534862022-08-30 10:42:33 +0800467 }
468
Gilles Peskine449bd832023-01-11 14:50:10 +0100469 return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
Jerry Yu5725f1c2022-08-21 17:27:16 +0800470}
Jerry Yuf35ba382022-08-23 17:58:26 +0800471
Jerry Yu82534862022-08-30 10:42:33 +0800472MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100473static int ssl_tls13_session_copy_ticket(mbedtls_ssl_session *dst,
474 const mbedtls_ssl_session *src)
Jerry Yu82534862022-08-30 10:42:33 +0800475{
Jerry Yu82534862022-08-30 10:42:33 +0800476 dst->ticket_age_add = src->ticket_age_add;
477 dst->ticket_flags = src->ticket_flags;
478 dst->resumption_key_len = src->resumption_key_len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100479 if (src->resumption_key_len == 0) {
480 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
481 }
482 memcpy(dst->resumption_key, src->resumption_key, src->resumption_key_len);
Jerry Yu4746b102022-09-13 11:11:48 +0800483
Gilles Peskine449bd832023-01-11 14:50:10 +0100484 return 0;
Jerry Yu82534862022-08-30 10:42:33 +0800485}
486#endif /* MBEDTLS_SSL_SESSION_TICKETS */
487
Jerry Yu1c105562022-07-10 06:32:38 +0000488/* Parser for pre_shared_key extension in client hello
489 * struct {
490 * opaque identity<1..2^16-1>;
491 * uint32 obfuscated_ticket_age;
492 * } PskIdentity;
493 *
494 * opaque PskBinderEntry<32..255>;
495 *
496 * struct {
497 * PskIdentity identities<7..2^16-1>;
498 * PskBinderEntry binders<33..2^16-1>;
499 * } OfferedPsks;
500 *
501 * struct {
502 * select (Handshake.msg_type) {
503 * case client_hello: OfferedPsks;
504 * ....
505 * };
506 * } PreSharedKeyExtension;
507 */
Jerry Yu6e74a7e2022-07-20 20:49:32 +0800508MBEDTLS_CHECK_RETURN_CRITICAL
Xiaokang Qian9f1747b2023-03-30 02:50:04 +0000509static int ssl_tls13_parse_pre_shared_key_ext(
510 mbedtls_ssl_context *ssl,
511 const unsigned char *pre_shared_key_ext,
512 const unsigned char *pre_shared_key_ext_end,
513 const unsigned char *ciphersuites,
514 const unsigned char *ciphersuites_end)
Jerry Yu1c105562022-07-10 06:32:38 +0000515{
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +0100516 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yu29d9faa2022-08-23 17:52:45 +0800517 const unsigned char *identities = pre_shared_key_ext;
Jerry Yu568ec252022-07-22 21:27:34 +0800518 const unsigned char *p_identity_len;
519 size_t identities_len;
Jerry Yu1c105562022-07-10 06:32:38 +0000520 const unsigned char *identities_end;
Jerry Yu568ec252022-07-22 21:27:34 +0800521 const unsigned char *binders;
522 const unsigned char *p_binder_len;
523 size_t binders_len;
Jerry Yu1c105562022-07-10 06:32:38 +0000524 const unsigned char *binders_end;
Jerry Yu96a2e362022-07-21 15:11:34 +0800525 int matched_identity = -1;
526 int identity_id = -1;
Jerry Yu1c105562022-07-10 06:32:38 +0000527
Gilles Peskine449bd832023-01-11 14:50:10 +0100528 MBEDTLS_SSL_DEBUG_BUF(3, "pre_shared_key extension",
529 pre_shared_key_ext,
530 pre_shared_key_ext_end - pre_shared_key_ext);
Jerry Yu1c105562022-07-10 06:32:38 +0000531
Jerry Yu96a2e362022-07-21 15:11:34 +0800532 /* identities_len 2 bytes
533 * identities_data >= 7 bytes
534 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100535 MBEDTLS_SSL_CHK_BUF_READ_PTR(identities, pre_shared_key_ext_end, 7 + 2);
536 identities_len = MBEDTLS_GET_UINT16_BE(identities, 0);
Jerry Yu568ec252022-07-22 21:27:34 +0800537 p_identity_len = identities + 2;
Gilles Peskine449bd832023-01-11 14:50:10 +0100538 MBEDTLS_SSL_CHK_BUF_READ_PTR(p_identity_len, pre_shared_key_ext_end,
539 identities_len);
Jerry Yu568ec252022-07-22 21:27:34 +0800540 identities_end = p_identity_len + identities_len;
Jerry Yu1c105562022-07-10 06:32:38 +0000541
Jerry Yu96a2e362022-07-21 15:11:34 +0800542 /* binders_len 2 bytes
543 * binders >= 33 bytes
544 */
Jerry Yu568ec252022-07-22 21:27:34 +0800545 binders = identities_end;
Gilles Peskine449bd832023-01-11 14:50:10 +0100546 MBEDTLS_SSL_CHK_BUF_READ_PTR(binders, pre_shared_key_ext_end, 33 + 2);
547 binders_len = MBEDTLS_GET_UINT16_BE(binders, 0);
Jerry Yu568ec252022-07-22 21:27:34 +0800548 p_binder_len = binders + 2;
Gilles Peskine449bd832023-01-11 14:50:10 +0100549 MBEDTLS_SSL_CHK_BUF_READ_PTR(p_binder_len, pre_shared_key_ext_end, binders_len);
Jerry Yu568ec252022-07-22 21:27:34 +0800550 binders_end = p_binder_len + binders_len;
Jerry Yu1c105562022-07-10 06:32:38 +0000551
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +0100552 ret = ssl->handshake->update_checksum(ssl, pre_shared_key_ext,
553 identities_end - pre_shared_key_ext);
554 if (0 != ret) {
555 MBEDTLS_SSL_DEBUG_RET(1, ("update_checksum"), ret);
556 return ret;
557 }
Jerry Yu96a2e362022-07-21 15:11:34 +0800558
Gilles Peskine449bd832023-01-11 14:50:10 +0100559 while (p_identity_len < identities_end && p_binder_len < binders_end) {
Jerry Yu1c105562022-07-10 06:32:38 +0000560 const unsigned char *identity;
Jerry Yu568ec252022-07-22 21:27:34 +0800561 size_t identity_len;
Jerry Yu82534862022-08-30 10:42:33 +0800562 uint32_t obfuscated_ticket_age;
Jerry Yu96a2e362022-07-21 15:11:34 +0800563 const unsigned char *binder;
Jerry Yu568ec252022-07-22 21:27:34 +0800564 size_t binder_len;
Jerry Yu5725f1c2022-08-21 17:27:16 +0800565 int psk_type;
566 uint16_t cipher_suite;
Jerry Yu29d9faa2022-08-23 17:52:45 +0800567 const mbedtls_ssl_ciphersuite_t *ciphersuite_info;
Jerry Yu82534862022-08-30 10:42:33 +0800568#if defined(MBEDTLS_SSL_SESSION_TICKETS)
569 mbedtls_ssl_session session;
Gilles Peskine449bd832023-01-11 14:50:10 +0100570 mbedtls_ssl_session_init(&session);
Jerry Yu82534862022-08-30 10:42:33 +0800571#endif
Jerry Yubb852022022-07-20 21:10:44 +0800572
Gilles Peskine449bd832023-01-11 14:50:10 +0100573 MBEDTLS_SSL_CHK_BUF_READ_PTR(p_identity_len, identities_end, 2 + 1 + 4);
574 identity_len = MBEDTLS_GET_UINT16_BE(p_identity_len, 0);
Jerry Yu568ec252022-07-22 21:27:34 +0800575 identity = p_identity_len + 2;
Gilles Peskine449bd832023-01-11 14:50:10 +0100576 MBEDTLS_SSL_CHK_BUF_READ_PTR(identity, identities_end, identity_len + 4);
577 obfuscated_ticket_age = MBEDTLS_GET_UINT32_BE(identity, identity_len);
Jerry Yu568ec252022-07-22 21:27:34 +0800578 p_identity_len += identity_len + 6;
Jerry Yu1c105562022-07-10 06:32:38 +0000579
Gilles Peskine449bd832023-01-11 14:50:10 +0100580 MBEDTLS_SSL_CHK_BUF_READ_PTR(p_binder_len, binders_end, 1 + 32);
Jerry Yu568ec252022-07-22 21:27:34 +0800581 binder_len = *p_binder_len;
582 binder = p_binder_len + 1;
Gilles Peskine449bd832023-01-11 14:50:10 +0100583 MBEDTLS_SSL_CHK_BUF_READ_PTR(binder, binders_end, binder_len);
Jerry Yu568ec252022-07-22 21:27:34 +0800584 p_binder_len += binder_len + 1;
Jerry Yu96a2e362022-07-21 15:11:34 +0800585
Jerry Yu96a2e362022-07-21 15:11:34 +0800586 identity_id++;
Gilles Peskine449bd832023-01-11 14:50:10 +0100587 if (matched_identity != -1) {
Jerry Yu1c105562022-07-10 06:32:38 +0000588 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +0100589 }
Jerry Yu1c105562022-07-10 06:32:38 +0000590
Jerry Yu96a2e362022-07-21 15:11:34 +0800591 ret = ssl_tls13_offered_psks_check_identity_match(
Gilles Peskine449bd832023-01-11 14:50:10 +0100592 ssl, identity, identity_len, obfuscated_ticket_age,
593 &psk_type, &session);
594 if (ret != SSL_TLS1_3_OFFERED_PSK_MATCH) {
Jerry Yu96a2e362022-07-21 15:11:34 +0800595 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +0100596 }
Jerry Yu96a2e362022-07-21 15:11:34 +0800597
Gilles Peskine449bd832023-01-11 14:50:10 +0100598 MBEDTLS_SSL_DEBUG_MSG(4, ("found matched identity"));
599 switch (psk_type) {
Jerry Yu0baf9072022-08-25 11:21:04 +0800600 case MBEDTLS_SSL_TLS1_3_PSK_EXTERNAL:
601 ret = ssl_tls13_select_ciphersuite_for_psk(
Gilles Peskine449bd832023-01-11 14:50:10 +0100602 ssl, ciphersuites, ciphersuites_end,
603 &cipher_suite, &ciphersuite_info);
Jerry Yu0baf9072022-08-25 11:21:04 +0800604 break;
605 case MBEDTLS_SSL_TLS1_3_PSK_RESUMPTION:
Jerry Yu82534862022-08-30 10:42:33 +0800606#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Jerry Yu0baf9072022-08-25 11:21:04 +0800607 ret = ssl_tls13_select_ciphersuite_for_resumption(
Gilles Peskine449bd832023-01-11 14:50:10 +0100608 ssl, ciphersuites, ciphersuites_end, &session,
609 &cipher_suite, &ciphersuite_info);
610 if (ret != 0) {
611 mbedtls_ssl_session_free(&session);
612 }
Jerry Yu82534862022-08-30 10:42:33 +0800613#else
614 ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
615#endif
Jerry Yu0baf9072022-08-25 11:21:04 +0800616 break;
617 default:
Gilles Peskine449bd832023-01-11 14:50:10 +0100618 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Jerry Yu0baf9072022-08-25 11:21:04 +0800619 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100620 if (ret != 0) {
Jerry Yuf35ba382022-08-23 17:58:26 +0800621 /* See below, no cipher_suite available, abort handshake */
622 MBEDTLS_SSL_PEND_FATAL_ALERT(
623 MBEDTLS_SSL_ALERT_MSG_DECRYPT_ERROR,
Gilles Peskine449bd832023-01-11 14:50:10 +0100624 MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE);
Jerry Yuf35ba382022-08-23 17:58:26 +0800625 MBEDTLS_SSL_DEBUG_RET(
Gilles Peskine449bd832023-01-11 14:50:10 +0100626 2, "ssl_tls13_select_ciphersuite", ret);
627 return ret;
Jerry Yu5725f1c2022-08-21 17:27:16 +0800628 }
629
Jerry Yu96a2e362022-07-21 15:11:34 +0800630 ret = ssl_tls13_offered_psks_check_binder_match(
Gilles Peskine449bd832023-01-11 14:50:10 +0100631 ssl, binder, binder_len, psk_type,
Dave Rodgman2eab4622023-10-05 13:30:37 +0100632 mbedtls_md_psa_alg_from_type((mbedtls_md_type_t) ciphersuite_info->mac));
Gilles Peskine449bd832023-01-11 14:50:10 +0100633 if (ret != SSL_TLS1_3_OFFERED_PSK_MATCH) {
Jerry Yuc5a23a02022-08-25 10:51:44 +0800634 /* For security reasons, the handshake should be aborted when we
635 * fail to validate a binder value. See RFC 8446 section 4.2.11.2
636 * and appendix E.6. */
Jerry Yu82534862022-08-30 10:42:33 +0800637#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Gilles Peskine449bd832023-01-11 14:50:10 +0100638 mbedtls_ssl_session_free(&session);
Jerry Yu82534862022-08-30 10:42:33 +0800639#endif
Gilles Peskine449bd832023-01-11 14:50:10 +0100640 MBEDTLS_SSL_DEBUG_MSG(3, ("Invalid binder."));
Xiaokang Qian9f1747b2023-03-30 02:50:04 +0000641 MBEDTLS_SSL_DEBUG_RET(
642 1, "ssl_tls13_offered_psks_check_binder_match", ret);
Jerry Yu96a2e362022-07-21 15:11:34 +0800643 MBEDTLS_SSL_PEND_FATAL_ALERT(
644 MBEDTLS_SSL_ALERT_MSG_DECRYPT_ERROR,
Gilles Peskine449bd832023-01-11 14:50:10 +0100645 MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE);
646 return ret;
Jerry Yu96a2e362022-07-21 15:11:34 +0800647 }
Jerry Yu96a2e362022-07-21 15:11:34 +0800648
649 matched_identity = identity_id;
Jerry Yu5725f1c2022-08-21 17:27:16 +0800650
651 /* Update handshake parameters */
Jerry Yue5834fd2022-08-29 20:16:09 +0800652 ssl->handshake->ciphersuite_info = ciphersuite_info;
Jerry Yu4746b102022-09-13 11:11:48 +0800653 ssl->session_negotiate->ciphersuite = cipher_suite;
Gilles Peskine449bd832023-01-11 14:50:10 +0100654 MBEDTLS_SSL_DEBUG_MSG(2, ("overwrite ciphersuite: %04x - %s",
655 cipher_suite, ciphersuite_info->name));
Jerry Yu82534862022-08-30 10:42:33 +0800656#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Gilles Peskine449bd832023-01-11 14:50:10 +0100657 if (psk_type == MBEDTLS_SSL_TLS1_3_PSK_RESUMPTION) {
658 ret = ssl_tls13_session_copy_ticket(ssl->session_negotiate,
659 &session);
660 mbedtls_ssl_session_free(&session);
661 if (ret != 0) {
662 return ret;
663 }
Jerry Yu82534862022-08-30 10:42:33 +0800664 }
665#endif /* MBEDTLS_SSL_SESSION_TICKETS */
Jerry Yu1c105562022-07-10 06:32:38 +0000666 }
667
Gilles Peskine449bd832023-01-11 14:50:10 +0100668 if (p_identity_len != identities_end || p_binder_len != binders_end) {
669 MBEDTLS_SSL_DEBUG_MSG(3, ("pre_shared_key extension decode error"));
670 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR,
671 MBEDTLS_ERR_SSL_DECODE_ERROR);
672 return MBEDTLS_ERR_SSL_DECODE_ERROR;
Jerry Yu1c105562022-07-10 06:32:38 +0000673 }
674
Jerry Yu6f1db3f2022-07-22 23:05:59 +0800675 /* Update the handshake transcript with the binder list. */
Xiaokang Qian9f1747b2023-03-30 02:50:04 +0000676 ret = ssl->handshake->update_checksum(
677 ssl, identities_end, (size_t) (binders_end - identities_end));
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +0100678 if (0 != ret) {
679 MBEDTLS_SSL_DEBUG_RET(1, ("update_checksum"), ret);
680 return ret;
681 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100682 if (matched_identity == -1) {
683 MBEDTLS_SSL_DEBUG_MSG(3, ("No matched PSK or ticket."));
684 return MBEDTLS_ERR_SSL_UNKNOWN_IDENTITY;
Jerry Yu1c105562022-07-10 06:32:38 +0000685 }
686
Gilles Peskine449bd832023-01-11 14:50:10 +0100687 ssl->handshake->selected_identity = (uint16_t) matched_identity;
688 MBEDTLS_SSL_DEBUG_MSG(3, ("Pre shared key found"));
Jerry Yu1c105562022-07-10 06:32:38 +0000689
Gilles Peskine449bd832023-01-11 14:50:10 +0100690 return 0;
Jerry Yu1c105562022-07-10 06:32:38 +0000691}
Jerry Yu032b15ce2022-07-11 06:10:03 +0000692
693/*
694 * struct {
695 * select ( Handshake.msg_type ) {
696 * ....
697 * case server_hello:
698 * uint16 selected_identity;
699 * }
700 * } PreSharedKeyExtension;
701 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100702static int ssl_tls13_write_server_pre_shared_key_ext(mbedtls_ssl_context *ssl,
703 unsigned char *buf,
704 unsigned char *end,
705 size_t *olen)
Jerry Yu032b15ce2022-07-11 06:10:03 +0000706{
Gilles Peskine449bd832023-01-11 14:50:10 +0100707 unsigned char *p = (unsigned char *) buf;
Jerry Yu032b15ce2022-07-11 06:10:03 +0000708
709 *olen = 0;
710
David Horstmann21b89762022-10-06 18:34:28 +0100711 int not_using_psk = 0;
Jerry Yu032b15ce2022-07-11 06:10:03 +0000712#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +0100713 not_using_psk = (mbedtls_svc_key_id_is_null(ssl->handshake->psk_opaque));
Jerry Yu032b15ce2022-07-11 06:10:03 +0000714#else
Gilles Peskine449bd832023-01-11 14:50:10 +0100715 not_using_psk = (ssl->handshake->psk == NULL);
Jerry Yu032b15ce2022-07-11 06:10:03 +0000716#endif
Gilles Peskine449bd832023-01-11 14:50:10 +0100717 if (not_using_psk) {
Jerry Yu032b15ce2022-07-11 06:10:03 +0000718 /* We shouldn't have called this extension writer unless we've
719 * chosen to use a PSK. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100720 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Jerry Yu032b15ce2022-07-11 06:10:03 +0000721 }
722
Gilles Peskine449bd832023-01-11 14:50:10 +0100723 MBEDTLS_SSL_DEBUG_MSG(3, ("server hello, adding pre_shared_key extension"));
724 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 6);
Jerry Yu032b15ce2022-07-11 06:10:03 +0000725
Gilles Peskine449bd832023-01-11 14:50:10 +0100726 MBEDTLS_PUT_UINT16_BE(MBEDTLS_TLS_EXT_PRE_SHARED_KEY, p, 0);
727 MBEDTLS_PUT_UINT16_BE(2, p, 2);
Jerry Yu032b15ce2022-07-11 06:10:03 +0000728
Gilles Peskine449bd832023-01-11 14:50:10 +0100729 MBEDTLS_PUT_UINT16_BE(ssl->handshake->selected_identity, p, 4);
Jerry Yu032b15ce2022-07-11 06:10:03 +0000730
731 *olen = 6;
732
Gilles Peskine449bd832023-01-11 14:50:10 +0100733 MBEDTLS_SSL_DEBUG_MSG(4, ("sent selected_identity: %u",
734 ssl->handshake->selected_identity));
Jerry Yu032b15ce2022-07-11 06:10:03 +0000735
Gilles Peskine449bd832023-01-11 14:50:10 +0100736 mbedtls_ssl_tls13_set_hs_sent_ext_mask(ssl, MBEDTLS_TLS_EXT_PRE_SHARED_KEY);
Jerry Yub95dd362022-11-08 21:19:34 +0800737
Gilles Peskine449bd832023-01-11 14:50:10 +0100738 return 0;
Jerry Yu032b15ce2022-07-11 06:10:03 +0000739}
740
Ronald Cron41a443a2022-10-04 16:38:25 +0200741#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED */
Jerry Yue19e3b92022-07-08 12:04:51 +0000742
XiaokangQian7807f9f2022-02-15 10:04:37 +0000743/* From RFC 8446:
744 * struct {
XiaokangQiancfd925f2022-04-14 07:10:37 +0000745 * ProtocolVersion versions<2..254>;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000746 * } SupportedVersions;
747 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200748MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100749static int ssl_tls13_parse_supported_versions_ext(mbedtls_ssl_context *ssl,
750 const unsigned char *buf,
751 const unsigned char *end)
XiaokangQian7807f9f2022-02-15 10:04:37 +0000752{
XiaokangQian7807f9f2022-02-15 10:04:37 +0000753 const unsigned char *p = buf;
XiaokangQian8f9dfe42022-04-15 02:52:39 +0000754 size_t versions_len;
XiaokangQian4080a7f2022-04-11 09:55:18 +0000755 const unsigned char *versions_end;
XiaokangQian0a1b54e2022-04-21 03:01:38 +0000756 uint16_t tls_version;
Ronald Cron5af4c7f2023-03-07 20:46:59 +0100757 int found_supported_version = 0;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000758
Gilles Peskine449bd832023-01-11 14:50:10 +0100759 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, 1);
XiaokangQian4080a7f2022-04-11 09:55:18 +0000760 versions_len = p[0];
XiaokangQian7807f9f2022-02-15 10:04:37 +0000761 p += 1;
762
Gilles Peskine449bd832023-01-11 14:50:10 +0100763 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, versions_len);
XiaokangQian4080a7f2022-04-11 09:55:18 +0000764 versions_end = p + versions_len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100765 while (p < versions_end) {
766 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, versions_end, 2);
767 tls_version = mbedtls_ssl_read_version(p, ssl->conf->transport);
XiaokangQianb67384d2022-04-19 00:02:38 +0000768 p += 2;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000769
Ronald Cron3bd2b022023-04-03 16:45:39 +0200770 if (MBEDTLS_SSL_VERSION_TLS1_3 == tls_version) {
Ronald Cron5af4c7f2023-03-07 20:46:59 +0100771 found_supported_version = 1;
772 break;
773 }
774
Ronald Cron3bd2b022023-04-03 16:45:39 +0200775 if ((MBEDTLS_SSL_VERSION_TLS1_2 == tls_version) &&
776 mbedtls_ssl_conf_is_tls12_enabled(ssl->conf)) {
Ronald Cron5af4c7f2023-03-07 20:46:59 +0100777 found_supported_version = 1;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000778 break;
779 }
XiaokangQian7807f9f2022-02-15 10:04:37 +0000780 }
781
Ronald Cron5af4c7f2023-03-07 20:46:59 +0100782 if (!found_supported_version) {
783 MBEDTLS_SSL_DEBUG_MSG(1, ("No supported version found."));
XiaokangQian7807f9f2022-02-15 10:04:37 +0000784
Gilles Peskine449bd832023-01-11 14:50:10 +0100785 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_PROTOCOL_VERSION,
786 MBEDTLS_ERR_SSL_BAD_PROTOCOL_VERSION);
787 return MBEDTLS_ERR_SSL_BAD_PROTOCOL_VERSION;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000788 }
789
Ronald Cron5af4c7f2023-03-07 20:46:59 +0100790 MBEDTLS_SSL_DEBUG_MSG(1, ("Negotiated version: [%04x]",
Gilles Peskine449bd832023-01-11 14:50:10 +0100791 (unsigned int) tls_version));
XiaokangQian7807f9f2022-02-15 10:04:37 +0000792
Ronald Cron5af4c7f2023-03-07 20:46:59 +0100793 return (int) tls_version;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000794}
795
Przemek Stekiel8c0a9532023-06-15 16:48:19 +0200796#if defined(PSA_WANT_ALG_ECDH) || defined(PSA_WANT_ALG_FFDH)
XiaokangQiane8ff3502022-04-22 02:34:40 +0000797/*
XiaokangQian7807f9f2022-02-15 10:04:37 +0000798 *
799 * From RFC 8446:
800 * enum {
801 * ... (0xFFFF)
802 * } NamedGroup;
803 * struct {
804 * NamedGroup named_group_list<2..2^16-1>;
805 * } NamedGroupList;
806 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200807MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100808static int ssl_tls13_parse_supported_groups_ext(mbedtls_ssl_context *ssl,
809 const unsigned char *buf,
810 const unsigned char *end)
XiaokangQian7807f9f2022-02-15 10:04:37 +0000811{
XiaokangQian7807f9f2022-02-15 10:04:37 +0000812 const unsigned char *p = buf;
XiaokangQian84823772022-04-19 07:57:30 +0000813 size_t named_group_list_len;
XiaokangQian8f9dfe42022-04-15 02:52:39 +0000814 const unsigned char *named_group_list_end;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000815
Gilles Peskine449bd832023-01-11 14:50:10 +0100816 MBEDTLS_SSL_DEBUG_BUF(3, "supported_groups extension", p, end - buf);
817 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, 2);
818 named_group_list_len = MBEDTLS_GET_UINT16_BE(p, 0);
XiaokangQian7807f9f2022-02-15 10:04:37 +0000819 p += 2;
Gilles Peskine449bd832023-01-11 14:50:10 +0100820 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, named_group_list_len);
XiaokangQian8f9dfe42022-04-15 02:52:39 +0000821 named_group_list_end = p + named_group_list_len;
XiaokangQian4e8cd7b2022-04-21 09:48:09 +0000822 ssl->handshake->hrr_selected_group = 0;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000823
Gilles Peskine449bd832023-01-11 14:50:10 +0100824 while (p < named_group_list_end) {
XiaokangQian08037552022-04-20 07:16:41 +0000825 uint16_t named_group;
Gilles Peskine449bd832023-01-11 14:50:10 +0100826 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, named_group_list_end, 2);
827 named_group = MBEDTLS_GET_UINT16_BE(p, 0);
XiaokangQian08037552022-04-20 07:16:41 +0000828 p += 2;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000829
Gilles Peskine449bd832023-01-11 14:50:10 +0100830 MBEDTLS_SSL_DEBUG_MSG(2,
831 ("got named group: %s(%04x)",
832 mbedtls_ssl_named_group_to_str(named_group),
833 named_group));
XiaokangQian08037552022-04-20 07:16:41 +0000834
Gilles Peskine449bd832023-01-11 14:50:10 +0100835 if (!mbedtls_ssl_named_group_is_offered(ssl, named_group) ||
836 !mbedtls_ssl_named_group_is_supported(named_group) ||
837 ssl->handshake->hrr_selected_group != 0) {
XiaokangQian08037552022-04-20 07:16:41 +0000838 continue;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000839 }
840
Gilles Peskine449bd832023-01-11 14:50:10 +0100841 MBEDTLS_SSL_DEBUG_MSG(2,
842 ("add named group %s(%04x) into received list.",
843 mbedtls_ssl_named_group_to_str(named_group),
844 named_group));
Jerry Yuc1be19f2022-04-23 16:11:39 +0800845
XiaokangQian4e8cd7b2022-04-21 09:48:09 +0000846 ssl->handshake->hrr_selected_group = named_group;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000847 }
848
Gilles Peskine449bd832023-01-11 14:50:10 +0100849 return 0;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000850
851}
Przemek Stekiel8c0a9532023-06-15 16:48:19 +0200852#endif /* PSA_WANT_ALG_ECDH || PSA_WANT_ALG_FFDH */
XiaokangQian7807f9f2022-02-15 10:04:37 +0000853
XiaokangQian08037552022-04-20 07:16:41 +0000854#define SSL_TLS1_3_PARSE_KEY_SHARES_EXT_NO_MATCH 1
855
Valerio Settic9ae8622023-07-25 11:23:50 +0200856#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_EPHEMERAL_ENABLED)
XiaokangQian7807f9f2022-02-15 10:04:37 +0000857/*
858 * ssl_tls13_parse_key_shares_ext() verifies whether the information in the
Xiaokang Qian9f1747b2023-03-30 02:50:04 +0000859 * extension is correct and stores the first acceptable key share and its
860 * associated group.
XiaokangQian7807f9f2022-02-15 10:04:37 +0000861 *
862 * Possible return values are:
863 * - 0: Successful processing of the client provided key share extension.
Xiaokang Qian9f1747b2023-03-30 02:50:04 +0000864 * - SSL_TLS1_3_PARSE_KEY_SHARES_EXT_NO_MATCH: The key shares provided by
865 * the client does not match a group supported by the server. A
866 * HelloRetryRequest will be needed.
XiaokangQiane8ff3502022-04-22 02:34:40 +0000867 * - A negative value for fatal errors.
Jerry Yuc1be19f2022-04-23 16:11:39 +0800868 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200869MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100870static int ssl_tls13_parse_key_shares_ext(mbedtls_ssl_context *ssl,
871 const unsigned char *buf,
872 const unsigned char *end)
XiaokangQian7807f9f2022-02-15 10:04:37 +0000873{
XiaokangQianb67384d2022-04-19 00:02:38 +0000874 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000875 unsigned char const *p = buf;
XiaokangQian8f9dfe42022-04-15 02:52:39 +0000876 unsigned char const *client_shares_end;
Jerry Yuc1be19f2022-04-23 16:11:39 +0800877 size_t client_shares_len;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000878
879 /* From RFC 8446:
880 *
881 * struct {
882 * KeyShareEntry client_shares<0..2^16-1>;
883 * } KeyShareClientHello;
884 *
885 */
886
Gilles Peskine449bd832023-01-11 14:50:10 +0100887 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, 2);
888 client_shares_len = MBEDTLS_GET_UINT16_BE(p, 0);
XiaokangQian7807f9f2022-02-15 10:04:37 +0000889 p += 2;
Gilles Peskine449bd832023-01-11 14:50:10 +0100890 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, client_shares_len);
XiaokangQian7807f9f2022-02-15 10:04:37 +0000891
892 ssl->handshake->offered_group_id = 0;
XiaokangQian8f9dfe42022-04-15 02:52:39 +0000893 client_shares_end = p + client_shares_len;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000894
895 /* We try to find a suitable key share entry and copy it to the
896 * handshake context. Later, we have to find out whether we can do
897 * something with the provided key share or whether we have to
XiaokangQianc5763b52022-04-02 03:34:37 +0000898 * dismiss it and send a HelloRetryRequest message.
899 */
XiaokangQian7807f9f2022-02-15 10:04:37 +0000900
Gilles Peskine449bd832023-01-11 14:50:10 +0100901 while (p < client_shares_end) {
XiaokangQian9b5d04b2022-04-10 10:20:43 +0000902 uint16_t group;
Jerry Yuc1be19f2022-04-23 16:11:39 +0800903 size_t key_exchange_len;
Jerry Yu086edc22022-05-05 10:50:38 +0800904 const unsigned char *key_exchange;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000905
906 /*
907 * struct {
908 * NamedGroup group;
909 * opaque key_exchange<1..2^16-1>;
910 * } KeyShareEntry;
911 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100912 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, client_shares_end, 4);
913 group = MBEDTLS_GET_UINT16_BE(p, 0);
914 key_exchange_len = MBEDTLS_GET_UINT16_BE(p, 2);
Jerry Yuc1be19f2022-04-23 16:11:39 +0800915 p += 4;
Jerry Yu086edc22022-05-05 10:50:38 +0800916 key_exchange = p;
Gilles Peskine449bd832023-01-11 14:50:10 +0100917 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, client_shares_end, key_exchange_len);
Jerry Yu086edc22022-05-05 10:50:38 +0800918 p += key_exchange_len;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000919
920 /* Continue parsing even if we have already found a match,
XiaokangQianc5763b52022-04-02 03:34:37 +0000921 * for input validation purposes.
922 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100923 if (!mbedtls_ssl_named_group_is_offered(ssl, group) ||
924 !mbedtls_ssl_named_group_is_supported(group) ||
925 ssl->handshake->offered_group_id != 0) {
XiaokangQian060d8672022-04-21 09:24:56 +0000926 continue;
927 }
XiaokangQian060d8672022-04-21 09:24:56 +0000928
XiaokangQian7807f9f2022-02-15 10:04:37 +0000929 /*
Przemek Stekielc89f3ea2023-05-18 15:45:53 +0200930 * ECDHE and FFDHE groups are supported
XiaokangQian7807f9f2022-02-15 10:04:37 +0000931 */
Przemek Stekielc89f3ea2023-05-18 15:45:53 +0200932 if (mbedtls_ssl_tls13_named_group_is_ecdhe(group) ||
Przemek Stekield5f79e72023-06-29 09:08:43 +0200933 mbedtls_ssl_tls13_named_group_is_ffdh(group)) {
Przemek Stekielc89f3ea2023-05-18 15:45:53 +0200934 MBEDTLS_SSL_DEBUG_MSG(2, ("ECDH/FFDH group: %s (%04x)",
Gilles Peskine449bd832023-01-11 14:50:10 +0100935 mbedtls_ssl_named_group_to_str(group),
936 group));
Przemek Stekiel7ac93be2023-07-04 10:02:38 +0200937 ret = mbedtls_ssl_tls13_read_public_xxdhe_share(
Gilles Peskine449bd832023-01-11 14:50:10 +0100938 ssl, key_exchange - 2, key_exchange_len + 2);
939 if (ret != 0) {
940 return ret;
941 }
XiaokangQian4e8cd7b2022-04-21 09:48:09 +0000942
Gilles Peskine449bd832023-01-11 14:50:10 +0100943 } else {
944 MBEDTLS_SSL_DEBUG_MSG(4, ("Unrecognized NamedGroup %u",
945 (unsigned) group));
XiaokangQian7807f9f2022-02-15 10:04:37 +0000946 continue;
947 }
948
XiaokangQian9b5d04b2022-04-10 10:20:43 +0000949 ssl->handshake->offered_group_id = group;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000950 }
951
Jerry Yuc1be19f2022-04-23 16:11:39 +0800952
Gilles Peskine449bd832023-01-11 14:50:10 +0100953 if (ssl->handshake->offered_group_id == 0) {
954 MBEDTLS_SSL_DEBUG_MSG(1, ("no matching key share"));
955 return SSL_TLS1_3_PARSE_KEY_SHARES_EXT_NO_MATCH;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000956 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100957 return 0;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000958}
Valerio Settic9ae8622023-07-25 11:23:50 +0200959#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_EPHEMERAL_ENABLED */
XiaokangQian7807f9f2022-02-15 10:04:37 +0000960
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200961MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100962static int ssl_tls13_client_hello_has_exts(mbedtls_ssl_context *ssl,
963 int exts_mask)
XiaokangQian7807f9f2022-02-15 10:04:37 +0000964{
Jerry Yu0c354a22022-08-29 15:25:36 +0800965 int masked = ssl->handshake->received_extensions & exts_mask;
Gilles Peskine449bd832023-01-11 14:50:10 +0100966 return masked == exts_mask;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000967}
968
Jerry Yue5991322022-11-07 14:03:44 +0800969#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200970MBEDTLS_CHECK_RETURN_CRITICAL
XiaokangQianb67384d2022-04-19 00:02:38 +0000971static int ssl_tls13_client_hello_has_exts_for_ephemeral_key_exchange(
Gilles Peskine449bd832023-01-11 14:50:10 +0100972 mbedtls_ssl_context *ssl)
XiaokangQian7807f9f2022-02-15 10:04:37 +0000973{
Gilles Peskine449bd832023-01-11 14:50:10 +0100974 return ssl_tls13_client_hello_has_exts(
975 ssl,
976 MBEDTLS_SSL_EXT_MASK(SUPPORTED_GROUPS) |
977 MBEDTLS_SSL_EXT_MASK(KEY_SHARE) |
978 MBEDTLS_SSL_EXT_MASK(SIG_ALG));
XiaokangQian7807f9f2022-02-15 10:04:37 +0000979}
Jerry Yue5991322022-11-07 14:03:44 +0800980#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED */
XiaokangQian7807f9f2022-02-15 10:04:37 +0000981
Jerry Yue5991322022-11-07 14:03:44 +0800982#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK_ENABLED)
Jerry Yu77f01482022-07-11 07:03:24 +0000983MBEDTLS_CHECK_RETURN_CRITICAL
984static int ssl_tls13_client_hello_has_exts_for_psk_key_exchange(
Gilles Peskine449bd832023-01-11 14:50:10 +0100985 mbedtls_ssl_context *ssl)
Jerry Yu77f01482022-07-11 07:03:24 +0000986{
Gilles Peskine449bd832023-01-11 14:50:10 +0100987 return ssl_tls13_client_hello_has_exts(
988 ssl,
989 MBEDTLS_SSL_EXT_MASK(PRE_SHARED_KEY) |
990 MBEDTLS_SSL_EXT_MASK(PSK_KEY_EXCHANGE_MODES));
Jerry Yu77f01482022-07-11 07:03:24 +0000991}
Jerry Yue5991322022-11-07 14:03:44 +0800992#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK_ENABLED */
Jerry Yu77f01482022-07-11 07:03:24 +0000993
Jerry Yue5991322022-11-07 14:03:44 +0800994#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK_EPHEMERAL_ENABLED)
Jerry Yu77f01482022-07-11 07:03:24 +0000995MBEDTLS_CHECK_RETURN_CRITICAL
996static int ssl_tls13_client_hello_has_exts_for_psk_ephemeral_key_exchange(
Gilles Peskine449bd832023-01-11 14:50:10 +0100997 mbedtls_ssl_context *ssl)
Jerry Yu77f01482022-07-11 07:03:24 +0000998{
Gilles Peskine449bd832023-01-11 14:50:10 +0100999 return ssl_tls13_client_hello_has_exts(
1000 ssl,
1001 MBEDTLS_SSL_EXT_MASK(SUPPORTED_GROUPS) |
1002 MBEDTLS_SSL_EXT_MASK(KEY_SHARE) |
1003 MBEDTLS_SSL_EXT_MASK(PRE_SHARED_KEY) |
1004 MBEDTLS_SSL_EXT_MASK(PSK_KEY_EXCHANGE_MODES));
Jerry Yu77f01482022-07-11 07:03:24 +00001005}
Jerry Yue5991322022-11-07 14:03:44 +08001006#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK_EPHEMERAL_ENABLED */
Jerry Yu77f01482022-07-11 07:03:24 +00001007
Pengyu Lv306a01d2023-02-02 16:35:47 +08001008#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED)
1009MBEDTLS_CHECK_RETURN_CRITICAL
Pengyu Lv52ad3332023-02-14 14:32:37 +08001010static int ssl_tls13_ticket_permission_check(mbedtls_ssl_context *ssl,
1011 unsigned int kex_mode)
Pengyu Lv306a01d2023-02-02 16:35:47 +08001012{
1013#if defined(MBEDTLS_SSL_SESSION_TICKETS)
1014 if (ssl->handshake->resume) {
Pengyu Lv7b711712023-10-24 17:07:14 +08001015 if (mbedtls_ssl_session_check_ticket_flags(
Pengyu Lv306a01d2023-02-02 16:35:47 +08001016 ssl->session_negotiate, kex_mode)) {
1017 return 0;
1018 }
1019 }
1020#else
1021 ((void) ssl);
1022 ((void) kex_mode);
1023#endif
1024 return 1;
1025}
1026#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED */
1027
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001028MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01001029static int ssl_tls13_check_ephemeral_key_exchange(mbedtls_ssl_context *ssl)
XiaokangQian7807f9f2022-02-15 10:04:37 +00001030{
Jerry Yue5991322022-11-07 14:03:44 +08001031#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED)
Pengyu Lvdadeb202023-01-18 17:32:34 +08001032 return !ssl->handshake->resume &&
1033 mbedtls_ssl_conf_tls13_ephemeral_enabled(ssl) &&
Gilles Peskine449bd832023-01-11 14:50:10 +01001034 ssl_tls13_client_hello_has_exts_for_ephemeral_key_exchange(ssl);
Jerry Yue5991322022-11-07 14:03:44 +08001035#else
1036 ((void) ssl);
Gilles Peskine449bd832023-01-11 14:50:10 +01001037 return 0;
Jerry Yue5991322022-11-07 14:03:44 +08001038#endif
Jerry Yu77f01482022-07-11 07:03:24 +00001039}
XiaokangQian7807f9f2022-02-15 10:04:37 +00001040
Jerry Yu77f01482022-07-11 07:03:24 +00001041MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01001042static int ssl_tls13_check_psk_key_exchange(mbedtls_ssl_context *ssl)
Jerry Yu77f01482022-07-11 07:03:24 +00001043{
Jerry Yue5991322022-11-07 14:03:44 +08001044#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK_ENABLED)
Pengyu Lv52ad3332023-02-14 14:32:37 +08001045 return ssl_tls13_ticket_permission_check(
Pengyu Lv306a01d2023-02-02 16:35:47 +08001046 ssl, MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK) &&
1047 mbedtls_ssl_conf_tls13_psk_enabled(ssl) &&
Gilles Peskine449bd832023-01-11 14:50:10 +01001048 mbedtls_ssl_tls13_psk_enabled(ssl) &&
1049 ssl_tls13_client_hello_has_exts_for_psk_key_exchange(ssl);
Jerry Yu77f01482022-07-11 07:03:24 +00001050#else
1051 ((void) ssl);
Gilles Peskine449bd832023-01-11 14:50:10 +01001052 return 0;
Jerry Yu77f01482022-07-11 07:03:24 +00001053#endif
1054}
XiaokangQian7807f9f2022-02-15 10:04:37 +00001055
Jerry Yu77f01482022-07-11 07:03:24 +00001056MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01001057static int ssl_tls13_check_psk_ephemeral_key_exchange(mbedtls_ssl_context *ssl)
Jerry Yu77f01482022-07-11 07:03:24 +00001058{
Jerry Yue5991322022-11-07 14:03:44 +08001059#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK_EPHEMERAL_ENABLED)
Pengyu Lv52ad3332023-02-14 14:32:37 +08001060 return ssl_tls13_ticket_permission_check(
Pengyu Lv306a01d2023-02-02 16:35:47 +08001061 ssl, MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK_EPHEMERAL) &&
1062 mbedtls_ssl_conf_tls13_psk_ephemeral_enabled(ssl) &&
Gilles Peskine449bd832023-01-11 14:50:10 +01001063 mbedtls_ssl_tls13_psk_ephemeral_enabled(ssl) &&
1064 ssl_tls13_client_hello_has_exts_for_psk_ephemeral_key_exchange(ssl);
Jerry Yu77f01482022-07-11 07:03:24 +00001065#else
1066 ((void) ssl);
Gilles Peskine449bd832023-01-11 14:50:10 +01001067 return 0;
Jerry Yu77f01482022-07-11 07:03:24 +00001068#endif
1069}
1070
Gilles Peskine449bd832023-01-11 14:50:10 +01001071static int ssl_tls13_determine_key_exchange_mode(mbedtls_ssl_context *ssl)
Jerry Yu77f01482022-07-11 07:03:24 +00001072{
1073 /*
1074 * Determine the key exchange algorithm to use.
1075 * There are three types of key exchanges supported in TLS 1.3:
1076 * - (EC)DH with ECDSA,
1077 * - (EC)DH with PSK,
1078 * - plain PSK.
1079 *
1080 * The PSK-based key exchanges may additionally be used with 0-RTT.
1081 *
1082 * Our built-in order of preference is
Jerry Yuce6ed702022-07-22 21:49:53 +08001083 * 1 ) (EC)DHE-PSK Mode ( psk_ephemeral )
1084 * 2 ) Certificate Mode ( ephemeral )
1085 * 3 ) Plain PSK Mode ( psk )
Jerry Yu77f01482022-07-11 07:03:24 +00001086 */
1087
Xiaokang Qian9f1747b2023-03-30 02:50:04 +00001088 ssl->handshake->key_exchange_mode =
1089 MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_NONE;
Jerry Yu77f01482022-07-11 07:03:24 +00001090
Gilles Peskine449bd832023-01-11 14:50:10 +01001091 if (ssl_tls13_check_psk_ephemeral_key_exchange(ssl)) {
Jerry Yu77f01482022-07-11 07:03:24 +00001092 ssl->handshake->key_exchange_mode =
1093 MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK_EPHEMERAL;
Gilles Peskine449bd832023-01-11 14:50:10 +01001094 MBEDTLS_SSL_DEBUG_MSG(2, ("key exchange mode: psk_ephemeral"));
1095 } else
1096 if (ssl_tls13_check_ephemeral_key_exchange(ssl)) {
Jerry Yu77f01482022-07-11 07:03:24 +00001097 ssl->handshake->key_exchange_mode =
1098 MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL;
Gilles Peskine449bd832023-01-11 14:50:10 +01001099 MBEDTLS_SSL_DEBUG_MSG(2, ("key exchange mode: ephemeral"));
1100 } else
1101 if (ssl_tls13_check_psk_key_exchange(ssl)) {
Jerry Yuce6ed702022-07-22 21:49:53 +08001102 ssl->handshake->key_exchange_mode =
1103 MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK;
Gilles Peskine449bd832023-01-11 14:50:10 +01001104 MBEDTLS_SSL_DEBUG_MSG(2, ("key exchange mode: psk"));
1105 } else {
Jerry Yu77f01482022-07-11 07:03:24 +00001106 MBEDTLS_SSL_DEBUG_MSG(
Gilles Peskine449bd832023-01-11 14:50:10 +01001107 1,
1108 ("ClientHello message misses mandatory extensions."));
1109 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_MISSING_EXTENSION,
1110 MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER);
1111 return MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER;
Jerry Yu77f01482022-07-11 07:03:24 +00001112 }
1113
Gilles Peskine449bd832023-01-11 14:50:10 +01001114 return 0;
Jerry Yu77f01482022-07-11 07:03:24 +00001115
XiaokangQian7807f9f2022-02-15 10:04:37 +00001116}
1117
XiaokangQian81802f42022-06-10 13:25:22 +00001118#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
Ronald Cron928cbd32022-10-04 16:14:26 +02001119 defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED)
Ronald Cron67ea2542022-09-15 17:34:42 +02001120
1121#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01001122static psa_algorithm_t ssl_tls13_iana_sig_alg_to_psa_alg(uint16_t sig_alg)
Ronald Cron67ea2542022-09-15 17:34:42 +02001123{
Gilles Peskine449bd832023-01-11 14:50:10 +01001124 switch (sig_alg) {
Ronald Cron67ea2542022-09-15 17:34:42 +02001125 case MBEDTLS_TLS1_3_SIG_ECDSA_SECP256R1_SHA256:
Gilles Peskine449bd832023-01-11 14:50:10 +01001126 return PSA_ALG_ECDSA(PSA_ALG_SHA_256);
Ronald Cron67ea2542022-09-15 17:34:42 +02001127 case MBEDTLS_TLS1_3_SIG_ECDSA_SECP384R1_SHA384:
Gilles Peskine449bd832023-01-11 14:50:10 +01001128 return PSA_ALG_ECDSA(PSA_ALG_SHA_384);
Ronald Cron67ea2542022-09-15 17:34:42 +02001129 case MBEDTLS_TLS1_3_SIG_ECDSA_SECP521R1_SHA512:
Gilles Peskine449bd832023-01-11 14:50:10 +01001130 return PSA_ALG_ECDSA(PSA_ALG_SHA_512);
Ronald Cron67ea2542022-09-15 17:34:42 +02001131 case MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA256:
Gilles Peskine449bd832023-01-11 14:50:10 +01001132 return PSA_ALG_RSA_PSS(PSA_ALG_SHA_256);
Ronald Cron67ea2542022-09-15 17:34:42 +02001133 case MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA384:
Gilles Peskine449bd832023-01-11 14:50:10 +01001134 return PSA_ALG_RSA_PSS(PSA_ALG_SHA_384);
Ronald Cron67ea2542022-09-15 17:34:42 +02001135 case MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA512:
Gilles Peskine449bd832023-01-11 14:50:10 +01001136 return PSA_ALG_RSA_PSS(PSA_ALG_SHA_512);
Ronald Cron67ea2542022-09-15 17:34:42 +02001137 case MBEDTLS_TLS1_3_SIG_RSA_PKCS1_SHA256:
Gilles Peskine449bd832023-01-11 14:50:10 +01001138 return PSA_ALG_RSA_PKCS1V15_SIGN(PSA_ALG_SHA_256);
Ronald Cron67ea2542022-09-15 17:34:42 +02001139 case MBEDTLS_TLS1_3_SIG_RSA_PKCS1_SHA384:
Gilles Peskine449bd832023-01-11 14:50:10 +01001140 return PSA_ALG_RSA_PKCS1V15_SIGN(PSA_ALG_SHA_384);
Ronald Cron67ea2542022-09-15 17:34:42 +02001141 case MBEDTLS_TLS1_3_SIG_RSA_PKCS1_SHA512:
Gilles Peskine449bd832023-01-11 14:50:10 +01001142 return PSA_ALG_RSA_PKCS1V15_SIGN(PSA_ALG_SHA_512);
Ronald Cron67ea2542022-09-15 17:34:42 +02001143 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01001144 return PSA_ALG_NONE;
Ronald Cron67ea2542022-09-15 17:34:42 +02001145 }
1146}
1147#endif /* MBEDTLS_USE_PSA_CRYPTO */
1148
XiaokangQian23c5be62022-06-07 02:04:34 +00001149/*
XiaokangQianfb665a82022-06-15 03:57:21 +00001150 * Pick best ( private key, certificate chain ) pair based on the signature
1151 * algorithms supported by the client.
XiaokangQian23c5be62022-06-07 02:04:34 +00001152 */
Ronald Cronce7d76e2022-07-08 18:56:49 +02001153MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01001154static int ssl_tls13_pick_key_cert(mbedtls_ssl_context *ssl)
XiaokangQian23c5be62022-06-07 02:04:34 +00001155{
XiaokangQianfb665a82022-06-15 03:57:21 +00001156 mbedtls_ssl_key_cert *key_cert, *key_cert_list;
XiaokangQian81802f42022-06-10 13:25:22 +00001157 const uint16_t *sig_alg = ssl->handshake->received_sig_algs;
XiaokangQian23c5be62022-06-07 02:04:34 +00001158
1159#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Gilles Peskine449bd832023-01-11 14:50:10 +01001160 if (ssl->handshake->sni_key_cert != NULL) {
XiaokangQianfb665a82022-06-15 03:57:21 +00001161 key_cert_list = ssl->handshake->sni_key_cert;
Gilles Peskine449bd832023-01-11 14:50:10 +01001162 } else
XiaokangQian23c5be62022-06-07 02:04:34 +00001163#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
Gilles Peskine449bd832023-01-11 14:50:10 +01001164 key_cert_list = ssl->conf->key_cert;
XiaokangQian23c5be62022-06-07 02:04:34 +00001165
Gilles Peskine449bd832023-01-11 14:50:10 +01001166 if (key_cert_list == NULL) {
1167 MBEDTLS_SSL_DEBUG_MSG(3, ("server has no certificate"));
1168 return -1;
XiaokangQian23c5be62022-06-07 02:04:34 +00001169 }
1170
Gilles Peskine449bd832023-01-11 14:50:10 +01001171 for (; *sig_alg != MBEDTLS_TLS1_3_SIG_NONE; sig_alg++) {
1172 if (!mbedtls_ssl_sig_alg_is_offered(ssl, *sig_alg)) {
Ronald Cron67ea2542022-09-15 17:34:42 +02001173 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +01001174 }
Ronald Cron67ea2542022-09-15 17:34:42 +02001175
Gilles Peskine449bd832023-01-11 14:50:10 +01001176 if (!mbedtls_ssl_tls13_sig_alg_for_cert_verify_is_supported(*sig_alg)) {
Ronald Cron67ea2542022-09-15 17:34:42 +02001177 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +01001178 }
Ronald Cron67ea2542022-09-15 17:34:42 +02001179
Gilles Peskine449bd832023-01-11 14:50:10 +01001180 for (key_cert = key_cert_list; key_cert != NULL;
1181 key_cert = key_cert->next) {
Ronald Cron67ea2542022-09-15 17:34:42 +02001182#if defined(MBEDTLS_USE_PSA_CRYPTO)
1183 psa_algorithm_t psa_alg = PSA_ALG_NONE;
1184#endif /* MBEDTLS_USE_PSA_CRYPTO */
1185
Gilles Peskine449bd832023-01-11 14:50:10 +01001186 MBEDTLS_SSL_DEBUG_CRT(3, "certificate (chain) candidate",
1187 key_cert->cert);
XiaokangQian81802f42022-06-10 13:25:22 +00001188
1189 /*
Gilles Peskine449bd832023-01-11 14:50:10 +01001190 * This avoids sending the client a cert it'll reject based on
1191 * keyUsage or other extensions.
1192 */
1193 if (mbedtls_x509_crt_check_key_usage(
1194 key_cert->cert, MBEDTLS_X509_KU_DIGITAL_SIGNATURE) != 0 ||
XiaokangQian81802f42022-06-10 13:25:22 +00001195 mbedtls_x509_crt_check_extended_key_usage(
XiaokangQianfb665a82022-06-15 03:57:21 +00001196 key_cert->cert, MBEDTLS_OID_SERVER_AUTH,
Gilles Peskine449bd832023-01-11 14:50:10 +01001197 MBEDTLS_OID_SIZE(MBEDTLS_OID_SERVER_AUTH)) != 0) {
1198 MBEDTLS_SSL_DEBUG_MSG(3, ("certificate mismatch: "
1199 "(extended) key usage extension"));
XiaokangQian81802f42022-06-10 13:25:22 +00001200 continue;
1201 }
XiaokangQian23c5be62022-06-07 02:04:34 +00001202
Gilles Peskine449bd832023-01-11 14:50:10 +01001203 MBEDTLS_SSL_DEBUG_MSG(3,
1204 ("ssl_tls13_pick_key_cert:"
1205 "check signature algorithm %s [%04x]",
1206 mbedtls_ssl_sig_alg_to_str(*sig_alg),
1207 *sig_alg));
Ronald Cron67ea2542022-09-15 17:34:42 +02001208#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01001209 psa_alg = ssl_tls13_iana_sig_alg_to_psa_alg(*sig_alg);
Ronald Cron67ea2542022-09-15 17:34:42 +02001210#endif /* MBEDTLS_USE_PSA_CRYPTO */
1211
Gilles Peskine449bd832023-01-11 14:50:10 +01001212 if (mbedtls_ssl_tls13_check_sig_alg_cert_key_match(
1213 *sig_alg, &key_cert->cert->pk)
Ronald Cron67ea2542022-09-15 17:34:42 +02001214#if defined(MBEDTLS_USE_PSA_CRYPTO)
1215 && psa_alg != PSA_ALG_NONE &&
Gilles Peskine449bd832023-01-11 14:50:10 +01001216 mbedtls_pk_can_do_ext(&key_cert->cert->pk, psa_alg,
1217 PSA_KEY_USAGE_SIGN_HASH) == 1
Ronald Cron67ea2542022-09-15 17:34:42 +02001218#endif /* MBEDTLS_USE_PSA_CRYPTO */
Gilles Peskine449bd832023-01-11 14:50:10 +01001219 ) {
XiaokangQianfb665a82022-06-15 03:57:21 +00001220 ssl->handshake->key_cert = key_cert;
Gilles Peskine449bd832023-01-11 14:50:10 +01001221 MBEDTLS_SSL_DEBUG_MSG(3,
1222 ("ssl_tls13_pick_key_cert:"
1223 "selected signature algorithm"
1224 " %s [%04x]",
1225 mbedtls_ssl_sig_alg_to_str(*sig_alg),
1226 *sig_alg));
XiaokangQianfb665a82022-06-15 03:57:21 +00001227 MBEDTLS_SSL_DEBUG_CRT(
Gilles Peskine449bd832023-01-11 14:50:10 +01001228 3, "selected certificate (chain)",
1229 ssl->handshake->key_cert->cert);
1230 return 0;
XiaokangQian81802f42022-06-10 13:25:22 +00001231 }
XiaokangQian81802f42022-06-10 13:25:22 +00001232 }
XiaokangQian23c5be62022-06-07 02:04:34 +00001233 }
1234
Gilles Peskine449bd832023-01-11 14:50:10 +01001235 MBEDTLS_SSL_DEBUG_MSG(2, ("ssl_tls13_pick_key_cert:"
1236 "no suitable certificate found"));
1237 return -1;
XiaokangQian23c5be62022-06-07 02:04:34 +00001238}
XiaokangQian81802f42022-06-10 13:25:22 +00001239#endif /* MBEDTLS_X509_CRT_PARSE_C &&
Ronald Cron928cbd32022-10-04 16:14:26 +02001240 MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED */
XiaokangQian23c5be62022-06-07 02:04:34 +00001241
XiaokangQian4080a7f2022-04-11 09:55:18 +00001242/*
XiaokangQianed582dd2022-04-13 08:21:05 +00001243 *
1244 * STATE HANDLING: ClientHello
1245 *
XiaokangQianb67384d2022-04-19 00:02:38 +00001246 * There are three possible classes of outcomes when parsing the ClientHello:
XiaokangQianed582dd2022-04-13 08:21:05 +00001247 *
XiaokangQianb67384d2022-04-19 00:02:38 +00001248 * 1) The ClientHello was well-formed and matched the server's configuration.
XiaokangQianed582dd2022-04-13 08:21:05 +00001249 *
1250 * In this case, the server progresses to sending its ServerHello.
1251 *
XiaokangQianb67384d2022-04-19 00:02:38 +00001252 * 2) The ClientHello was well-formed but didn't match the server's
1253 * configuration.
XiaokangQianed582dd2022-04-13 08:21:05 +00001254 *
1255 * For example, the client might not have offered a key share which
1256 * the server supports, or the server might require a cookie.
1257 *
1258 * In this case, the server sends a HelloRetryRequest.
1259 *
XiaokangQianb67384d2022-04-19 00:02:38 +00001260 * 3) The ClientHello was ill-formed
XiaokangQianed582dd2022-04-13 08:21:05 +00001261 *
1262 * In this case, we abort the handshake.
1263 *
1264 */
1265
1266/*
XiaokangQian4080a7f2022-04-11 09:55:18 +00001267 * Structure of this message:
1268 *
XiaokangQiane8ff3502022-04-22 02:34:40 +00001269 * uint16 ProtocolVersion;
1270 * opaque Random[32];
1271 * uint8 CipherSuite[2]; // Cryptographic suite selector
XiaokangQian4080a7f2022-04-11 09:55:18 +00001272 *
XiaokangQiane8ff3502022-04-22 02:34:40 +00001273 * struct {
1274 * ProtocolVersion legacy_version = 0x0303; // TLS v1.2
1275 * Random random;
1276 * opaque legacy_session_id<0..32>;
1277 * CipherSuite cipher_suites<2..2^16-2>;
1278 * opaque legacy_compression_methods<1..2^8-1>;
1279 * Extension extensions<8..2^16-1>;
1280 * } ClientHello;
XiaokangQian4080a7f2022-04-11 09:55:18 +00001281 */
XiaokangQianed582dd2022-04-13 08:21:05 +00001282
1283#define SSL_CLIENT_HELLO_OK 0
1284#define SSL_CLIENT_HELLO_HRR_REQUIRED 1
Ronald Cron5af4c7f2023-03-07 20:46:59 +01001285#define SSL_CLIENT_HELLO_TLS1_2 2
XiaokangQianed582dd2022-04-13 08:21:05 +00001286
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001287MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01001288static int ssl_tls13_parse_client_hello(mbedtls_ssl_context *ssl,
1289 const unsigned char *buf,
1290 const unsigned char *end)
XiaokangQian7807f9f2022-02-15 10:04:37 +00001291{
XiaokangQianb67384d2022-04-19 00:02:38 +00001292 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1293 const unsigned char *p = buf;
Ronald Crond540d992023-03-07 09:41:48 +01001294 const unsigned char *random;
XiaokangQian4080a7f2022-04-11 09:55:18 +00001295 size_t legacy_session_id_len;
Ronald Croncada4102023-03-07 09:51:39 +01001296 const unsigned char *legacy_session_id;
XiaokangQian318dc762022-04-20 09:43:51 +00001297 size_t cipher_suites_len;
Ronald Cron2f16b4e2023-03-07 10:07:32 +01001298 const unsigned char *cipher_suites;
XiaokangQian060d8672022-04-21 09:24:56 +00001299 const unsigned char *cipher_suites_end;
XiaokangQianb67384d2022-04-19 00:02:38 +00001300 size_t extensions_len;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001301 const unsigned char *extensions_end;
Ronald Croneff56732023-04-03 17:36:31 +02001302 const unsigned char *supported_versions_data;
1303 const unsigned char *supported_versions_data_end;
Jerry Yuc4bf5d62022-10-29 09:08:47 +08001304 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
Jerry Yu49ca9282022-05-05 11:05:22 +08001305 int hrr_required = 0;
Ronald Cron8a74f072023-06-14 17:59:29 +02001306 int no_usable_share_for_key_agreement = 0;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001307
Ronald Cron41a443a2022-10-04 16:38:25 +02001308#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED)
Jerry Yu29d9faa2022-08-23 17:52:45 +08001309 const unsigned char *pre_shared_key_ext = NULL;
Jerry Yu1c105562022-07-10 06:32:38 +00001310 const unsigned char *pre_shared_key_ext_end = NULL;
Ronald Cron41a443a2022-10-04 16:38:25 +02001311#endif
XiaokangQian7807f9f2022-02-15 10:04:37 +00001312
XiaokangQian7807f9f2022-02-15 10:04:37 +00001313 /*
XiaokangQianb67384d2022-04-19 00:02:38 +00001314 * ClientHello layout:
XiaokangQian7807f9f2022-02-15 10:04:37 +00001315 * 0 . 1 protocol version
XiaokangQiane8ff3502022-04-22 02:34:40 +00001316 * 2 . 33 random bytes
XiaokangQianc5763b52022-04-02 03:34:37 +00001317 * 34 . 34 session id length ( 1 byte )
XiaokangQian7807f9f2022-02-15 10:04:37 +00001318 * 35 . 34+x session id
XiaokangQian7807f9f2022-02-15 10:04:37 +00001319 * .. . .. ciphersuite list length ( 2 bytes )
1320 * .. . .. ciphersuite list
1321 * .. . .. compression alg. list length ( 1 byte )
1322 * .. . .. compression alg. list
1323 * .. . .. extensions length ( 2 bytes, optional )
1324 * .. . .. extensions ( optional )
1325 */
1326
XiaokangQianb67384d2022-04-19 00:02:38 +00001327 /*
bootstrap-prime6dbbf442022-05-17 19:30:44 -04001328 * Minimal length ( with everything empty and extensions omitted ) is
XiaokangQian7807f9f2022-02-15 10:04:37 +00001329 * 2 + 32 + 1 + 2 + 1 = 38 bytes. Check that first, so that we can
1330 * read at least up to session id length without worrying.
1331 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001332 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, 38);
XiaokangQian7807f9f2022-02-15 10:04:37 +00001333
1334 /* ...
1335 * ProtocolVersion legacy_version = 0x0303; // TLS 1.2
1336 * ...
1337 * with ProtocolVersion defined as:
1338 * uint16 ProtocolVersion;
1339 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001340 if (mbedtls_ssl_read_version(p, ssl->conf->transport) !=
1341 MBEDTLS_SSL_VERSION_TLS1_2) {
1342 MBEDTLS_SSL_DEBUG_MSG(1, ("Unsupported version of TLS."));
1343 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_PROTOCOL_VERSION,
1344 MBEDTLS_ERR_SSL_BAD_PROTOCOL_VERSION);
1345 return MBEDTLS_ERR_SSL_BAD_PROTOCOL_VERSION;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001346 }
1347 p += 2;
1348
Jerry Yuc1be19f2022-04-23 16:11:39 +08001349 /* ...
1350 * Random random;
1351 * ...
XiaokangQianb67384d2022-04-19 00:02:38 +00001352 * with Random defined as:
1353 * opaque Random[32];
XiaokangQian7807f9f2022-02-15 10:04:37 +00001354 */
Ronald Crond540d992023-03-07 09:41:48 +01001355 random = p;
XiaokangQian08037552022-04-20 07:16:41 +00001356 p += MBEDTLS_CLIENT_HELLO_RANDOM_LEN;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001357
Jerry Yuc1be19f2022-04-23 16:11:39 +08001358 /* ...
XiaokangQianb67384d2022-04-19 00:02:38 +00001359 * opaque legacy_session_id<0..32>;
Jerry Yuc1be19f2022-04-23 16:11:39 +08001360 * ...
XiaokangQian7807f9f2022-02-15 10:04:37 +00001361 */
Ronald Croncada4102023-03-07 09:51:39 +01001362 legacy_session_id_len = *(p++);
1363 legacy_session_id = p;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001364
XiaokangQianb67384d2022-04-19 00:02:38 +00001365 /*
1366 * Check we have enough data for the legacy session identifier
Jerry Yue95c8af2022-07-26 15:48:20 +08001367 * and the ciphersuite list length.
XiaokangQianb67384d2022-04-19 00:02:38 +00001368 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001369 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, legacy_session_id_len + 2);
XiaokangQian4080a7f2022-04-11 09:55:18 +00001370 p += legacy_session_id_len;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001371
Ronald Cron2f16b4e2023-03-07 10:07:32 +01001372 /* ...
1373 * CipherSuite cipher_suites<2..2^16-2>;
1374 * ...
1375 * with CipherSuite defined as:
1376 * uint8 CipherSuite[2];
1377 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001378 cipher_suites_len = MBEDTLS_GET_UINT16_BE(p, 0);
XiaokangQian7807f9f2022-02-15 10:04:37 +00001379 p += 2;
Ronald Cron2f16b4e2023-03-07 10:07:32 +01001380 cipher_suites = p;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001381
Ronald Cronfc7ae872023-02-16 15:32:19 +01001382 /*
1383 * The length of the ciphersuite list has to be even.
1384 */
1385 if (cipher_suites_len & 1) {
1386 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR,
1387 MBEDTLS_ERR_SSL_DECODE_ERROR);
1388 return MBEDTLS_ERR_SSL_DECODE_ERROR;
1389 }
XiaokangQian7807f9f2022-02-15 10:04:37 +00001390
XiaokangQianb67384d2022-04-19 00:02:38 +00001391 /* Check we have enough data for the ciphersuite list, the legacy
1392 * compression methods and the length of the extensions.
Jerry Yue95c8af2022-07-26 15:48:20 +08001393 *
1394 * cipher_suites cipher_suites_len bytes
1395 * legacy_compression_methods 2 bytes
1396 * extensions_len 2 bytes
XiaokangQianb67384d2022-04-19 00:02:38 +00001397 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001398 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, cipher_suites_len + 2 + 2);
Ronald Cron8c527d02023-03-07 15:47:47 +01001399 p += cipher_suites_len;
1400 cipher_suites_end = p;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001401
Jerry Yu5725f1c2022-08-21 17:27:16 +08001402 /*
Ronald Cron8c527d02023-03-07 15:47:47 +01001403 * Search for the supported versions extension and parse it to determine
1404 * if the client supports TLS 1.3.
Jerry Yu24b8c812022-08-20 19:06:56 +08001405 */
Ronald Cron8c527d02023-03-07 15:47:47 +01001406 ret = mbedtls_ssl_tls13_is_supported_versions_ext_present_in_exts(
1407 ssl, p + 2, end,
Ronald Croneff56732023-04-03 17:36:31 +02001408 &supported_versions_data, &supported_versions_data_end);
Ronald Cron8c527d02023-03-07 15:47:47 +01001409 if (ret < 0) {
1410 MBEDTLS_SSL_DEBUG_RET(1,
1411 ("mbedtls_ssl_tls13_is_supported_versions_ext_present_in_exts"), ret);
1412 return ret;
1413 }
1414
1415 if (ret == 0) {
Ronald Cron5af4c7f2023-03-07 20:46:59 +01001416 return SSL_CLIENT_HELLO_TLS1_2;
Ronald Cron8c527d02023-03-07 15:47:47 +01001417 }
1418
Ronald Cron5af4c7f2023-03-07 20:46:59 +01001419 if (ret == 1) {
1420 ret = ssl_tls13_parse_supported_versions_ext(ssl,
Ronald Croneff56732023-04-03 17:36:31 +02001421 supported_versions_data,
1422 supported_versions_data_end);
Ronald Cron5af4c7f2023-03-07 20:46:59 +01001423 if (ret < 0) {
1424 MBEDTLS_SSL_DEBUG_RET(1,
1425 ("ssl_tls13_parse_supported_versions_ext"), ret);
1426 return ret;
1427 }
1428
Ronald Cronb828c7d2023-04-03 16:37:22 +02001429 /*
1430 * The supported versions extension was parsed successfully as the
1431 * value returned by ssl_tls13_parse_supported_versions_ext() is
1432 * positive. The return value is then equal to
1433 * MBEDTLS_SSL_VERSION_TLS1_2 or MBEDTLS_SSL_VERSION_TLS1_3, defining
1434 * the TLS version to negotiate.
1435 */
Ronald Cron5af4c7f2023-03-07 20:46:59 +01001436 if (MBEDTLS_SSL_VERSION_TLS1_2 == ret) {
1437 return SSL_CLIENT_HELLO_TLS1_2;
1438 }
Ronald Cron8c527d02023-03-07 15:47:47 +01001439 }
1440
1441 /*
1442 * We negotiate TLS 1.3.
Ronald Cron64582392023-03-07 09:21:40 +01001443 */
1444 ssl->tls_version = MBEDTLS_SSL_VERSION_TLS1_3;
1445
1446#if defined(MBEDTLS_SSL_SESSION_TICKETS)
1447 /* Store minor version for later use with ticket serialization. */
1448 ssl->session_negotiate->tls_version = MBEDTLS_SSL_VERSION_TLS1_3;
1449 ssl->session_negotiate->endpoint = ssl->conf->endpoint;
XiaokangQian060d8672022-04-21 09:24:56 +00001450#endif
Ronald Cron64582392023-03-07 09:21:40 +01001451
1452 /*
Ronald Crondad02b22023-04-06 09:57:52 +02001453 * We are negotiating the version 1.3 of the protocol. Do what we have
Ronald Croncada4102023-03-07 09:51:39 +01001454 * postponed: copy of the client random bytes, copy of the legacy session
Ronald Cron2f16b4e2023-03-07 10:07:32 +01001455 * identifier and selection of the TLS 1.3 cipher suite.
Ronald Crond540d992023-03-07 09:41:48 +01001456 */
1457 MBEDTLS_SSL_DEBUG_BUF(3, "client hello, random bytes",
1458 random, MBEDTLS_CLIENT_HELLO_RANDOM_LEN);
1459 memcpy(&handshake->randbytes[0], random, MBEDTLS_CLIENT_HELLO_RANDOM_LEN);
1460
Ronald Croncada4102023-03-07 09:51:39 +01001461 if (legacy_session_id_len > sizeof(ssl->session_negotiate->id)) {
1462 MBEDTLS_SSL_DEBUG_MSG(1, ("bad client hello message"));
1463 return MBEDTLS_ERR_SSL_DECODE_ERROR;
1464 }
1465 ssl->session_negotiate->id_len = legacy_session_id_len;
1466 MBEDTLS_SSL_DEBUG_BUF(3, "client hello, session id",
1467 legacy_session_id, legacy_session_id_len);
1468 memcpy(&ssl->session_negotiate->id[0],
1469 legacy_session_id, legacy_session_id_len);
Jerry Yu5725f1c2022-08-21 17:27:16 +08001470
1471 /*
1472 * Search for a matching ciphersuite
1473 */
Ronald Cron2f16b4e2023-03-07 10:07:32 +01001474 MBEDTLS_SSL_DEBUG_BUF(3, "client hello, list of cipher suites",
1475 cipher_suites, cipher_suites_len);
Ronald Crone45afd72023-04-04 15:10:06 +02001476 for (const unsigned char *cipher_suites_p = cipher_suites;
1477 cipher_suites_p < cipher_suites_end; cipher_suites_p += 2) {
XiaokangQiane8ff3502022-04-22 02:34:40 +00001478 uint16_t cipher_suite;
Gilles Peskine449bd832023-01-11 14:50:10 +01001479 const mbedtls_ssl_ciphersuite_t *ciphersuite_info;
Jerry Yu5725f1c2022-08-21 17:27:16 +08001480
Ronald Crond89360b2023-02-21 08:53:33 +01001481 /*
Ronald Crone45afd72023-04-04 15:10:06 +02001482 * "cipher_suites_end - cipher_suites_p is even" is an invariant of the
1483 * loop. As cipher_suites_end - cipher_suites_p > 0, we have
1484 * cipher_suites_end - cipher_suites_p >= 2 and it is thus safe to read
1485 * two bytes.
Ronald Crond89360b2023-02-21 08:53:33 +01001486 */
Ronald Crone45afd72023-04-04 15:10:06 +02001487 cipher_suite = MBEDTLS_GET_UINT16_BE(cipher_suites_p, 0);
Jerry Yuc5a23a02022-08-25 10:51:44 +08001488 ciphersuite_info = ssl_tls13_validate_peer_ciphersuite(
Gilles Peskine449bd832023-01-11 14:50:10 +01001489 ssl, cipher_suite);
1490 if (ciphersuite_info == NULL) {
Jerry Yu5725f1c2022-08-21 17:27:16 +08001491 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +01001492 }
Jerry Yu5725f1c2022-08-21 17:27:16 +08001493
Jerry Yu5725f1c2022-08-21 17:27:16 +08001494 ssl->session_negotiate->ciphersuite = cipher_suite;
Jerry Yuc4bf5d62022-10-29 09:08:47 +08001495 handshake->ciphersuite_info = ciphersuite_info;
Gilles Peskine449bd832023-01-11 14:50:10 +01001496 MBEDTLS_SSL_DEBUG_MSG(2, ("selected ciphersuite: %04x - %s",
1497 cipher_suite,
1498 ciphersuite_info->name));
Ronald Cron25e9ec62023-02-16 15:35:16 +01001499 break;
XiaokangQian17f974c2022-04-19 09:57:41 +00001500 }
Jerry Yu5725f1c2022-08-21 17:27:16 +08001501
Gilles Peskine449bd832023-01-11 14:50:10 +01001502 if (handshake->ciphersuite_info == NULL) {
1503 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE,
1504 MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE);
1505 return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
Jerry Yu5725f1c2022-08-21 17:27:16 +08001506 }
Jerry Yuc1be19f2022-04-23 16:11:39 +08001507
XiaokangQian4080a7f2022-04-11 09:55:18 +00001508 /* ...
XiaokangQianb67384d2022-04-19 00:02:38 +00001509 * opaque legacy_compression_methods<1..2^8-1>;
XiaokangQian4080a7f2022-04-11 09:55:18 +00001510 * ...
XiaokangQian7807f9f2022-02-15 10:04:37 +00001511 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001512 if (p[0] != 1 || p[1] != MBEDTLS_SSL_COMPRESS_NULL) {
1513 MBEDTLS_SSL_DEBUG_MSG(1, ("bad legacy compression method"));
1514 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER,
1515 MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER);
1516 return MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001517 }
XiaokangQianb67384d2022-04-19 00:02:38 +00001518 p += 2;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001519
Jerry Yuc1be19f2022-04-23 16:11:39 +08001520 /* ...
XiaokangQianb67384d2022-04-19 00:02:38 +00001521 * Extension extensions<8..2^16-1>;
Jerry Yuc1be19f2022-04-23 16:11:39 +08001522 * ...
XiaokangQianb67384d2022-04-19 00:02:38 +00001523 * with Extension defined as:
1524 * struct {
1525 * ExtensionType extension_type;
1526 * opaque extension_data<0..2^16-1>;
1527 * } Extension;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001528 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001529 extensions_len = MBEDTLS_GET_UINT16_BE(p, 0);
XiaokangQian7807f9f2022-02-15 10:04:37 +00001530 p += 2;
Gilles Peskine449bd832023-01-11 14:50:10 +01001531 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, extensions_len);
XiaokangQiane8ff3502022-04-22 02:34:40 +00001532 extensions_end = p + extensions_len;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001533
Gilles Peskine449bd832023-01-11 14:50:10 +01001534 MBEDTLS_SSL_DEBUG_BUF(3, "client hello extensions", p, extensions_len);
Jerry Yu63a459c2022-10-31 13:38:40 +08001535 handshake->received_extensions = MBEDTLS_SSL_EXT_MASK_NONE;
Jerry Yu0c354a22022-08-29 15:25:36 +08001536
Gilles Peskine449bd832023-01-11 14:50:10 +01001537 while (p < extensions_end) {
XiaokangQian7807f9f2022-02-15 10:04:37 +00001538 unsigned int extension_type;
1539 size_t extension_data_len;
1540 const unsigned char *extension_data_end;
1541
Jerry Yu0c354a22022-08-29 15:25:36 +08001542 /* RFC 8446, section 4.2.11
Jerry Yu1c9247c2022-07-21 12:37:39 +08001543 *
1544 * The "pre_shared_key" extension MUST be the last extension in the
1545 * ClientHello (this facilitates implementation as described below).
1546 * Servers MUST check that it is the last extension and otherwise fail
1547 * the handshake with an "illegal_parameter" alert.
1548 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001549 if (handshake->received_extensions & MBEDTLS_SSL_EXT_MASK(PRE_SHARED_KEY)) {
Jerry Yu1c9247c2022-07-21 12:37:39 +08001550 MBEDTLS_SSL_DEBUG_MSG(
Gilles Peskine449bd832023-01-11 14:50:10 +01001551 3, ("pre_shared_key is not last extension."));
Jerry Yu1c9247c2022-07-21 12:37:39 +08001552 MBEDTLS_SSL_PEND_FATAL_ALERT(
1553 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER,
Gilles Peskine449bd832023-01-11 14:50:10 +01001554 MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER);
1555 return MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER;
Jerry Yu1c9247c2022-07-21 12:37:39 +08001556 }
1557
Gilles Peskine449bd832023-01-11 14:50:10 +01001558 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, extensions_end, 4);
1559 extension_type = MBEDTLS_GET_UINT16_BE(p, 0);
1560 extension_data_len = MBEDTLS_GET_UINT16_BE(p, 2);
XiaokangQian7807f9f2022-02-15 10:04:37 +00001561 p += 4;
1562
Gilles Peskine449bd832023-01-11 14:50:10 +01001563 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, extensions_end, extension_data_len);
XiaokangQian7807f9f2022-02-15 10:04:37 +00001564 extension_data_end = p + extension_data_len;
1565
Jerry Yuc4bf5d62022-10-29 09:08:47 +08001566 ret = mbedtls_ssl_tls13_check_received_extension(
Gilles Peskine449bd832023-01-11 14:50:10 +01001567 ssl, MBEDTLS_SSL_HS_CLIENT_HELLO, extension_type,
1568 MBEDTLS_SSL_TLS1_3_ALLOWED_EXTS_OF_CH);
1569 if (ret != 0) {
1570 return ret;
1571 }
Jerry Yue18dc7e2022-08-04 16:29:22 +08001572
Gilles Peskine449bd832023-01-11 14:50:10 +01001573 switch (extension_type) {
XiaokangQian40a35232022-05-07 09:02:40 +00001574#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
1575 case MBEDTLS_TLS_EXT_SERVERNAME:
Gilles Peskine449bd832023-01-11 14:50:10 +01001576 MBEDTLS_SSL_DEBUG_MSG(3, ("found ServerName extension"));
1577 ret = mbedtls_ssl_parse_server_name_ext(ssl, p,
1578 extension_data_end);
1579 if (ret != 0) {
XiaokangQian40a35232022-05-07 09:02:40 +00001580 MBEDTLS_SSL_DEBUG_RET(
Gilles Peskine449bd832023-01-11 14:50:10 +01001581 1, "mbedtls_ssl_parse_servername_ext", ret);
1582 return ret;
XiaokangQian40a35232022-05-07 09:02:40 +00001583 }
XiaokangQian40a35232022-05-07 09:02:40 +00001584 break;
1585#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
1586
Przemek Stekiel8c0a9532023-06-15 16:48:19 +02001587#if defined(PSA_WANT_ALG_ECDH) || defined(PSA_WANT_ALG_FFDH)
XiaokangQian7807f9f2022-02-15 10:04:37 +00001588 case MBEDTLS_TLS_EXT_SUPPORTED_GROUPS:
Gilles Peskine449bd832023-01-11 14:50:10 +01001589 MBEDTLS_SSL_DEBUG_MSG(3, ("found supported group extension"));
XiaokangQian7807f9f2022-02-15 10:04:37 +00001590
1591 /* Supported Groups Extension
1592 *
1593 * When sent by the client, the "supported_groups" extension
1594 * indicates the named groups which the client supports,
1595 * ordered from most preferred to least preferred.
1596 */
Jerry Yuc1be19f2022-04-23 16:11:39 +08001597 ret = ssl_tls13_parse_supported_groups_ext(
Gilles Peskine449bd832023-01-11 14:50:10 +01001598 ssl, p, extension_data_end);
1599 if (ret != 0) {
Xiaokang Qian9f1747b2023-03-30 02:50:04 +00001600 MBEDTLS_SSL_DEBUG_RET(
Xiaokang Qian8bce0e62023-04-04 10:15:48 +00001601 1, "ssl_tls13_parse_supported_groups_ext", ret);
Gilles Peskine449bd832023-01-11 14:50:10 +01001602 return ret;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001603 }
1604
XiaokangQian7807f9f2022-02-15 10:04:37 +00001605 break;
Przemek Stekiel8c0a9532023-06-15 16:48:19 +02001606#endif /* PSA_WANT_ALG_ECDH || PSA_WANT_ALG_FFDH*/
XiaokangQian7807f9f2022-02-15 10:04:37 +00001607
Valerio Settic9ae8622023-07-25 11:23:50 +02001608#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_EPHEMERAL_ENABLED)
XiaokangQian7807f9f2022-02-15 10:04:37 +00001609 case MBEDTLS_TLS_EXT_KEY_SHARE:
Gilles Peskine449bd832023-01-11 14:50:10 +01001610 MBEDTLS_SSL_DEBUG_MSG(3, ("found key share extension"));
XiaokangQian7807f9f2022-02-15 10:04:37 +00001611
1612 /*
1613 * Key Share Extension
1614 *
1615 * When sent by the client, the "key_share" extension
1616 * contains the endpoint's cryptographic parameters for
1617 * ECDHE/DHE key establishment methods.
1618 */
Jerry Yuc1be19f2022-04-23 16:11:39 +08001619 ret = ssl_tls13_parse_key_shares_ext(
Gilles Peskine449bd832023-01-11 14:50:10 +01001620 ssl, p, extension_data_end);
1621 if (ret == SSL_TLS1_3_PARSE_KEY_SHARES_EXT_NO_MATCH) {
Ronald Cron8a74f072023-06-14 17:59:29 +02001622 MBEDTLS_SSL_DEBUG_MSG(2, ("No usable share for key agreement."));
1623 no_usable_share_for_key_agreement = 1;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001624 }
1625
Gilles Peskine449bd832023-01-11 14:50:10 +01001626 if (ret < 0) {
Jerry Yu582dd062022-04-22 21:59:01 +08001627 MBEDTLS_SSL_DEBUG_RET(
Gilles Peskine449bd832023-01-11 14:50:10 +01001628 1, "ssl_tls13_parse_key_shares_ext", ret);
1629 return ret;
Jerry Yu582dd062022-04-22 21:59:01 +08001630 }
XiaokangQian7807f9f2022-02-15 10:04:37 +00001631
XiaokangQian7807f9f2022-02-15 10:04:37 +00001632 break;
Valerio Settic9ae8622023-07-25 11:23:50 +02001633#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_EPHEMERAL_ENABLED */
XiaokangQian7807f9f2022-02-15 10:04:37 +00001634
1635 case MBEDTLS_TLS_EXT_SUPPORTED_VERSIONS:
Ronald Cron8c527d02023-03-07 15:47:47 +01001636 /* Already parsed */
XiaokangQian7807f9f2022-02-15 10:04:37 +00001637 break;
1638
Ronald Cron41a443a2022-10-04 16:38:25 +02001639#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED)
Jerry Yue19e3b92022-07-08 12:04:51 +00001640 case MBEDTLS_TLS_EXT_PSK_KEY_EXCHANGE_MODES:
Xiaokang Qian9f1747b2023-03-30 02:50:04 +00001641 MBEDTLS_SSL_DEBUG_MSG(
1642 3, ("found psk key exchange modes extension"));
Jerry Yue19e3b92022-07-08 12:04:51 +00001643
1644 ret = ssl_tls13_parse_key_exchange_modes_ext(
Gilles Peskine449bd832023-01-11 14:50:10 +01001645 ssl, p, extension_data_end);
1646 if (ret != 0) {
Jerry Yue19e3b92022-07-08 12:04:51 +00001647 MBEDTLS_SSL_DEBUG_RET(
Gilles Peskine449bd832023-01-11 14:50:10 +01001648 1, "ssl_tls13_parse_key_exchange_modes_ext", ret);
1649 return ret;
Jerry Yue19e3b92022-07-08 12:04:51 +00001650 }
1651
Jerry Yue19e3b92022-07-08 12:04:51 +00001652 break;
Ronald Cron41a443a2022-10-04 16:38:25 +02001653#endif
Jerry Yue19e3b92022-07-08 12:04:51 +00001654
Jerry Yu1c105562022-07-10 06:32:38 +00001655 case MBEDTLS_TLS_EXT_PRE_SHARED_KEY:
Gilles Peskine449bd832023-01-11 14:50:10 +01001656 MBEDTLS_SSL_DEBUG_MSG(3, ("found pre_shared_key extension"));
1657 if ((handshake->received_extensions &
1658 MBEDTLS_SSL_EXT_MASK(PSK_KEY_EXCHANGE_MODES)) == 0) {
Jerry Yu13ab81d2022-07-22 23:17:11 +08001659 MBEDTLS_SSL_PEND_FATAL_ALERT(
1660 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER,
Gilles Peskine449bd832023-01-11 14:50:10 +01001661 MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER);
1662 return MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER;
Jerry Yu13ab81d2022-07-22 23:17:11 +08001663 }
Ronald Cron41a443a2022-10-04 16:38:25 +02001664#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED)
Jerry Yu1c105562022-07-10 06:32:38 +00001665 /* Delay processing of the PSK identity once we have
1666 * found out which algorithms to use. We keep a pointer
1667 * to the buffer and the size for later processing.
1668 */
Jerry Yu29d9faa2022-08-23 17:52:45 +08001669 pre_shared_key_ext = p;
Jerry Yu1c105562022-07-10 06:32:38 +00001670 pre_shared_key_ext_end = extension_data_end;
Jerry Yue18dc7e2022-08-04 16:29:22 +08001671#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED */
Jerry Yu1c105562022-07-10 06:32:38 +00001672 break;
Jerry Yu1c105562022-07-10 06:32:38 +00001673
XiaokangQianacb39922022-06-17 10:18:48 +00001674#if defined(MBEDTLS_SSL_ALPN)
1675 case MBEDTLS_TLS_EXT_ALPN:
Gilles Peskine449bd832023-01-11 14:50:10 +01001676 MBEDTLS_SSL_DEBUG_MSG(3, ("found alpn extension"));
XiaokangQianacb39922022-06-17 10:18:48 +00001677
Gilles Peskine449bd832023-01-11 14:50:10 +01001678 ret = mbedtls_ssl_parse_alpn_ext(ssl, p, extension_data_end);
1679 if (ret != 0) {
XiaokangQianacb39922022-06-17 10:18:48 +00001680 MBEDTLS_SSL_DEBUG_RET(
Gilles Peskine449bd832023-01-11 14:50:10 +01001681 1, ("mbedtls_ssl_parse_alpn_ext"), ret);
1682 return ret;
XiaokangQianacb39922022-06-17 10:18:48 +00001683 }
XiaokangQianacb39922022-06-17 10:18:48 +00001684 break;
1685#endif /* MBEDTLS_SSL_ALPN */
1686
Ronald Cron928cbd32022-10-04 16:14:26 +02001687#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED)
XiaokangQian7807f9f2022-02-15 10:04:37 +00001688 case MBEDTLS_TLS_EXT_SIG_ALG:
Gilles Peskine449bd832023-01-11 14:50:10 +01001689 MBEDTLS_SSL_DEBUG_MSG(3, ("found signature_algorithms extension"));
XiaokangQian7807f9f2022-02-15 10:04:37 +00001690
Gabor Mezei078e8032022-04-27 21:17:56 +02001691 ret = mbedtls_ssl_parse_sig_alg_ext(
Gilles Peskine449bd832023-01-11 14:50:10 +01001692 ssl, p, extension_data_end);
1693 if (ret != 0) {
Xiaokang Qian49f39c12023-04-06 02:31:02 +00001694 MBEDTLS_SSL_DEBUG_RET(
1695 1, "mbedtls_ssl_parse_sig_alg_ext", ret);
Gilles Peskine449bd832023-01-11 14:50:10 +01001696 return ret;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001697 }
XiaokangQian7807f9f2022-02-15 10:04:37 +00001698 break;
Ronald Cron928cbd32022-10-04 16:14:26 +02001699#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED */
XiaokangQian7807f9f2022-02-15 10:04:37 +00001700
Jan Bruckner151f6422023-02-10 12:45:19 +01001701#if defined(MBEDTLS_SSL_RECORD_SIZE_LIMIT)
1702 case MBEDTLS_TLS_EXT_RECORD_SIZE_LIMIT:
1703 MBEDTLS_SSL_DEBUG_MSG(3, ("found record_size_limit extension"));
1704
Xiaokang Qian9f1747b2023-03-30 02:50:04 +00001705 ret = mbedtls_ssl_tls13_parse_record_size_limit_ext(
1706 ssl, p, extension_data_end);
Jan Bruckner151f6422023-02-10 12:45:19 +01001707
Xiaokang Qian09c3ccc2023-04-04 10:18:38 +00001708 /*
1709 * TODO: Return unconditionally here until we handle the record
1710 * size limit correctly.
1711 * Once handled correctly, only return in case of errors.
1712 */
Jan Bruckner151f6422023-02-10 12:45:19 +01001713 return ret;
1714
1715 break;
1716#endif /* MBEDTLS_SSL_RECORD_SIZE_LIMIT */
1717
XiaokangQian7807f9f2022-02-15 10:04:37 +00001718 default:
Jerry Yu79aa7212022-11-08 21:30:21 +08001719 MBEDTLS_SSL_PRINT_EXT(
Jerry Yu63a459c2022-10-31 13:38:40 +08001720 3, MBEDTLS_SSL_HS_CLIENT_HELLO,
Gilles Peskine449bd832023-01-11 14:50:10 +01001721 extension_type, "( ignored )");
Jerry Yu0c354a22022-08-29 15:25:36 +08001722 break;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001723 }
1724
1725 p += extension_data_len;
1726 }
1727
Gilles Peskine449bd832023-01-11 14:50:10 +01001728 MBEDTLS_SSL_PRINT_EXTS(3, MBEDTLS_SSL_HS_CLIENT_HELLO,
1729 handshake->received_extensions);
Jerry Yue18dc7e2022-08-04 16:29:22 +08001730
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +01001731 ret = mbedtls_ssl_add_hs_hdr_to_checksum(ssl,
1732 MBEDTLS_SSL_HS_CLIENT_HELLO,
1733 p - buf);
1734 if (0 != ret) {
1735 MBEDTLS_SSL_DEBUG_RET(1, ("mbedtls_ssl_add_hs_hdr_to_checksum"), ret);
1736 return ret;
1737 }
Jerry Yu032b15ce2022-07-11 06:10:03 +00001738
Ronald Cron41a443a2022-10-04 16:38:25 +02001739#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED)
XiaokangQian7807f9f2022-02-15 10:04:37 +00001740 /* Update checksum with either
1741 * - The entire content of the CH message, if no PSK extension is present
1742 * - The content up to but excluding the PSK extension, if present.
1743 */
Jerry Yu1c105562022-07-10 06:32:38 +00001744 /* If we've settled on a PSK-based exchange, parse PSK identity ext */
Pengyu Lvcfb23b82023-10-30 15:26:26 +08001745 if (ssl_tls13_check_psk_key_exchange(ssl) ||
1746 ssl_tls13_check_psk_ephemeral_key_exchange(ssl)) {
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +01001747 ret = handshake->update_checksum(ssl, buf,
1748 pre_shared_key_ext - buf);
1749 if (0 != ret) {
1750 MBEDTLS_SSL_DEBUG_RET(1, ("update_checksum"), ret);
1751 return ret;
1752 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001753 ret = ssl_tls13_parse_pre_shared_key_ext(ssl,
1754 pre_shared_key_ext,
1755 pre_shared_key_ext_end,
1756 cipher_suites,
1757 cipher_suites_end);
1758 if (ret == MBEDTLS_ERR_SSL_UNKNOWN_IDENTITY) {
1759 handshake->received_extensions &= ~MBEDTLS_SSL_EXT_MASK(PRE_SHARED_KEY);
1760 } else if (ret != 0) {
Jerry Yu63a459c2022-10-31 13:38:40 +08001761 MBEDTLS_SSL_DEBUG_RET(
Gilles Peskine449bd832023-01-11 14:50:10 +01001762 1, "ssl_tls13_parse_pre_shared_key_ext", ret);
1763 return ret;
Jerry Yu1c105562022-07-10 06:32:38 +00001764 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001765 } else
Ronald Cron41a443a2022-10-04 16:38:25 +02001766#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED */
Jerry Yu1c105562022-07-10 06:32:38 +00001767 {
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +01001768 ret = handshake->update_checksum(ssl, buf, p - buf);
1769 if (0 != ret) {
1770 MBEDTLS_SSL_DEBUG_RET(1, ("update_checksum"), ret);
1771 return ret;
1772 }
Jerry Yu1c105562022-07-10 06:32:38 +00001773 }
XiaokangQian7807f9f2022-02-15 10:04:37 +00001774
Gilles Peskine449bd832023-01-11 14:50:10 +01001775 ret = ssl_tls13_determine_key_exchange_mode(ssl);
1776 if (ret < 0) {
1777 return ret;
1778 }
XiaokangQian7807f9f2022-02-15 10:04:37 +00001779
Ronald Cron8a74f072023-06-14 17:59:29 +02001780 if (ssl->handshake->key_exchange_mode !=
1781 MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK) {
1782 hrr_required = (no_usable_share_for_key_agreement != 0);
1783 }
1784
Gilles Peskine449bd832023-01-11 14:50:10 +01001785 mbedtls_ssl_optimize_checksum(ssl, handshake->ciphersuite_info);
Jerry Yue5834fd2022-08-29 20:16:09 +08001786
Gilles Peskine449bd832023-01-11 14:50:10 +01001787 return hrr_required ? SSL_CLIENT_HELLO_HRR_REQUIRED : SSL_CLIENT_HELLO_OK;
XiaokangQian75fe8c72022-06-15 09:42:45 +00001788}
1789
1790/* Update the handshake state machine */
1791
Ronald Cronce7d76e2022-07-08 18:56:49 +02001792MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01001793static int ssl_tls13_postprocess_client_hello(mbedtls_ssl_context *ssl)
XiaokangQian75fe8c72022-06-15 09:42:45 +00001794{
1795 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1796
XiaokangQian7807f9f2022-02-15 10:04:37 +00001797 /*
XiaokangQianfb665a82022-06-15 03:57:21 +00001798 * Server certificate selection
1799 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001800 if (ssl->conf->f_cert_cb && (ret = ssl->conf->f_cert_cb(ssl)) != 0) {
1801 MBEDTLS_SSL_DEBUG_RET(1, "f_cert_cb", ret);
1802 return ret;
XiaokangQianfb665a82022-06-15 03:57:21 +00001803 }
1804#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
1805 ssl->handshake->sni_name = NULL;
1806 ssl->handshake->sni_name_len = 0;
1807#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
XiaokangQian7807f9f2022-02-15 10:04:37 +00001808
Gilles Peskine449bd832023-01-11 14:50:10 +01001809 ret = mbedtls_ssl_tls13_key_schedule_stage_early(ssl);
1810 if (ret != 0) {
1811 MBEDTLS_SSL_DEBUG_RET(1,
1812 "mbedtls_ssl_tls1_3_key_schedule_stage_early", ret);
1813 return ret;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001814 }
1815
Gilles Peskine449bd832023-01-11 14:50:10 +01001816 return 0;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001817
1818}
1819
1820/*
XiaokangQianed582dd2022-04-13 08:21:05 +00001821 * Main entry point from the state machine; orchestrates the otherfunctions.
1822 */
1823
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001824MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01001825static int ssl_tls13_process_client_hello(mbedtls_ssl_context *ssl)
XiaokangQianed582dd2022-04-13 08:21:05 +00001826{
1827
XiaokangQian08037552022-04-20 07:16:41 +00001828 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine449bd832023-01-11 14:50:10 +01001829 unsigned char *buf = NULL;
XiaokangQianed582dd2022-04-13 08:21:05 +00001830 size_t buflen = 0;
Jerry Yu4ca91402022-05-09 15:50:57 +08001831 int parse_client_hello_ret;
1832
Gilles Peskine449bd832023-01-11 14:50:10 +01001833 MBEDTLS_SSL_DEBUG_MSG(2, ("=> parse client hello"));
XiaokangQianed582dd2022-04-13 08:21:05 +00001834
Gilles Peskine449bd832023-01-11 14:50:10 +01001835 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_tls13_fetch_handshake_msg(
1836 ssl, MBEDTLS_SSL_HS_CLIENT_HELLO,
1837 &buf, &buflen));
XiaokangQianed582dd2022-04-13 08:21:05 +00001838
Gilles Peskine449bd832023-01-11 14:50:10 +01001839 MBEDTLS_SSL_PROC_CHK_NEG(ssl_tls13_parse_client_hello(ssl, buf,
1840 buf + buflen));
Ronald Cron5af4c7f2023-03-07 20:46:59 +01001841 parse_client_hello_ret = ret; /* Store positive return value of
1842 * parse_client_hello,
1843 * as negative error codes are handled
Jerry Yuf41553b2022-05-09 22:20:30 +08001844 * by MBEDTLS_SSL_PROC_CHK_NEG. */
Jerry Yu4ca91402022-05-09 15:50:57 +08001845
Ronald Cronb828c7d2023-04-03 16:37:22 +02001846 /*
1847 * Version 1.2 of the protocol has been chosen, set the
1848 * ssl->keep_current_message flag for the ClientHello to be kept and parsed
1849 * as a TLS 1.2 ClientHello. We also change ssl->tls_version to
1850 * MBEDTLS_SSL_VERSION_TLS1_2 thus from now on mbedtls_ssl_handshake_step()
1851 * will dispatch to the TLS 1.2 state machine.
1852 */
Ronald Cron5af4c7f2023-03-07 20:46:59 +01001853 if (SSL_CLIENT_HELLO_TLS1_2 == parse_client_hello_ret) {
1854 ssl->keep_current_message = 1;
1855 ssl->tls_version = MBEDTLS_SSL_VERSION_TLS1_2;
1856 return 0;
1857 }
1858
Gilles Peskine449bd832023-01-11 14:50:10 +01001859 MBEDTLS_SSL_PROC_CHK(ssl_tls13_postprocess_client_hello(ssl));
Jerry Yu582dd062022-04-22 21:59:01 +08001860
Ronald Cron5af4c7f2023-03-07 20:46:59 +01001861 if (SSL_CLIENT_HELLO_OK == parse_client_hello_ret) {
Gilles Peskine449bd832023-01-11 14:50:10 +01001862 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_SERVER_HELLO);
1863 } else {
1864 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_HELLO_RETRY_REQUEST);
1865 }
XiaokangQianed582dd2022-04-13 08:21:05 +00001866
1867cleanup:
1868
Gilles Peskine449bd832023-01-11 14:50:10 +01001869 MBEDTLS_SSL_DEBUG_MSG(2, ("<= parse client hello"));
1870 return ret;
XiaokangQianed582dd2022-04-13 08:21:05 +00001871}
1872
1873/*
Jerry Yu1c3e6882022-04-20 21:23:40 +08001874 * Handler for MBEDTLS_SSL_SERVER_HELLO
Jerry Yu5b64ae92022-03-30 17:15:02 +08001875 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001876MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01001877static int ssl_tls13_prepare_server_hello(mbedtls_ssl_context *ssl)
Jerry Yuf4b27e42022-03-30 17:32:21 +08001878{
Jerry Yu637a3f12022-04-20 21:37:58 +08001879 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1880 unsigned char *server_randbytes =
Gilles Peskine449bd832023-01-11 14:50:10 +01001881 ssl->handshake->randbytes + MBEDTLS_CLIENT_HELLO_RANDOM_LEN;
1882 if (ssl->conf->f_rng == NULL) {
1883 MBEDTLS_SSL_DEBUG_MSG(1, ("no RNG provided"));
1884 return MBEDTLS_ERR_SSL_NO_RNG;
Jerry Yuf4b27e42022-03-30 17:32:21 +08001885 }
1886
Gilles Peskine449bd832023-01-11 14:50:10 +01001887 if ((ret = ssl->conf->f_rng(ssl->conf->p_rng, server_randbytes,
1888 MBEDTLS_SERVER_HELLO_RANDOM_LEN)) != 0) {
1889 MBEDTLS_SSL_DEBUG_RET(1, "f_rng", ret);
1890 return ret;
Jerry Yuf4b27e42022-03-30 17:32:21 +08001891 }
1892
Gilles Peskine449bd832023-01-11 14:50:10 +01001893 MBEDTLS_SSL_DEBUG_BUF(3, "server hello, random bytes", server_randbytes,
1894 MBEDTLS_SERVER_HELLO_RANDOM_LEN);
Jerry Yuf4b27e42022-03-30 17:32:21 +08001895
1896#if defined(MBEDTLS_HAVE_TIME)
YxCda609132023-05-22 12:08:12 -07001897 ssl->session_negotiate->start = mbedtls_time(NULL);
Jerry Yuf4b27e42022-03-30 17:32:21 +08001898#endif /* MBEDTLS_HAVE_TIME */
1899
Gilles Peskine449bd832023-01-11 14:50:10 +01001900 return ret;
Jerry Yuf4b27e42022-03-30 17:32:21 +08001901}
1902
Jerry Yu3bf2c642022-03-30 22:02:12 +08001903/*
Jerry Yue74e04a2022-04-21 09:23:16 +08001904 * ssl_tls13_write_server_hello_supported_versions_ext ():
Jerry Yu3bf2c642022-03-30 22:02:12 +08001905 *
1906 * struct {
Jerry Yufb9f54d2022-04-06 10:08:34 +08001907 * ProtocolVersion selected_version;
Jerry Yu3bf2c642022-03-30 22:02:12 +08001908 * } SupportedVersions;
1909 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001910MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yue74e04a2022-04-21 09:23:16 +08001911static int ssl_tls13_write_server_hello_supported_versions_ext(
Gilles Peskine449bd832023-01-11 14:50:10 +01001912 mbedtls_ssl_context *ssl,
1913 unsigned char *buf,
1914 unsigned char *end,
1915 size_t *out_len)
Jerry Yu3bf2c642022-03-30 22:02:12 +08001916{
Jerry Yu3bf2c642022-03-30 22:02:12 +08001917 *out_len = 0;
1918
Gilles Peskine449bd832023-01-11 14:50:10 +01001919 MBEDTLS_SSL_DEBUG_MSG(3, ("server hello, write selected version"));
Jerry Yu3bf2c642022-03-30 22:02:12 +08001920
1921 /* Check if we have space to write the extension:
1922 * - extension_type (2 bytes)
1923 * - extension_data_length (2 bytes)
Jerry Yu349a6132022-04-14 20:52:56 +08001924 * - selected_version (2 bytes)
Jerry Yu3bf2c642022-03-30 22:02:12 +08001925 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001926 MBEDTLS_SSL_CHK_BUF_PTR(buf, end, 6);
Jerry Yu3bf2c642022-03-30 22:02:12 +08001927
Gilles Peskine449bd832023-01-11 14:50:10 +01001928 MBEDTLS_PUT_UINT16_BE(MBEDTLS_TLS_EXT_SUPPORTED_VERSIONS, buf, 0);
Jerry Yu3bf2c642022-03-30 22:02:12 +08001929
Gilles Peskine449bd832023-01-11 14:50:10 +01001930 MBEDTLS_PUT_UINT16_BE(2, buf, 2);
Jerry Yu3bf2c642022-03-30 22:02:12 +08001931
Gilles Peskine449bd832023-01-11 14:50:10 +01001932 mbedtls_ssl_write_version(buf + 4,
1933 ssl->conf->transport,
1934 ssl->tls_version);
Jerry Yu3bf2c642022-03-30 22:02:12 +08001935
Gilles Peskine449bd832023-01-11 14:50:10 +01001936 MBEDTLS_SSL_DEBUG_MSG(3, ("supported version: [%04x]",
1937 ssl->tls_version));
Jerry Yu3bf2c642022-03-30 22:02:12 +08001938
Jerry Yu349a6132022-04-14 20:52:56 +08001939 *out_len = 6;
Jerry Yu3bf2c642022-03-30 22:02:12 +08001940
Jerry Yub95dd362022-11-08 21:19:34 +08001941 mbedtls_ssl_tls13_set_hs_sent_ext_mask(
Gilles Peskine449bd832023-01-11 14:50:10 +01001942 ssl, MBEDTLS_TLS_EXT_SUPPORTED_VERSIONS);
Jerry Yub95dd362022-11-08 21:19:34 +08001943
Gilles Peskine449bd832023-01-11 14:50:10 +01001944 return 0;
Jerry Yu3bf2c642022-03-30 22:02:12 +08001945}
1946
Jerry Yud9436a12022-04-20 22:28:09 +08001947
Jerry Yu3bf2c642022-03-30 22:02:12 +08001948
1949/* Generate and export a single key share. For hybrid KEMs, this can
1950 * be called multiple times with the different components of the hybrid. */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001951MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01001952static int ssl_tls13_generate_and_write_key_share(mbedtls_ssl_context *ssl,
1953 uint16_t named_group,
1954 unsigned char *buf,
1955 unsigned char *end,
1956 size_t *out_len)
Jerry Yu3bf2c642022-03-30 22:02:12 +08001957{
1958 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yu955ddd72022-04-22 22:27:33 +08001959
1960 *out_len = 0;
1961
Valerio Settic9ae8622023-07-25 11:23:50 +02001962#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_EPHEMERAL_ENABLED)
Przemek Stekiel29c219c2023-05-31 15:21:04 +02001963 if (mbedtls_ssl_tls13_named_group_is_ecdhe(named_group) ||
Przemek Stekield5f79e72023-06-29 09:08:43 +02001964 mbedtls_ssl_tls13_named_group_is_ffdh(named_group)) {
Przemek Stekiel408569f2023-07-06 11:26:44 +02001965 ret = mbedtls_ssl_tls13_generate_and_write_xxdh_key_exchange(
Gilles Peskine449bd832023-01-11 14:50:10 +01001966 ssl, named_group, buf, end, out_len);
1967 if (ret != 0) {
Jerry Yu89e103c2022-03-30 22:43:29 +08001968 MBEDTLS_SSL_DEBUG_RET(
Przemek Stekiel408569f2023-07-06 11:26:44 +02001969 1, "mbedtls_ssl_tls13_generate_and_write_xxdh_key_exchange",
Gilles Peskine449bd832023-01-11 14:50:10 +01001970 ret);
1971 return ret;
Jerry Yu3bf2c642022-03-30 22:02:12 +08001972 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001973 } else
Valerio Settic9ae8622023-07-25 11:23:50 +02001974#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_EPHEMERAL_ENABLED */
Gilles Peskine449bd832023-01-11 14:50:10 +01001975 if (0 /* Other kinds of KEMs */) {
1976 } else {
Jerry Yu955ddd72022-04-22 22:27:33 +08001977 ((void) ssl);
1978 ((void) named_group);
1979 ((void) buf);
1980 ((void) end);
Jerry Yu3bf2c642022-03-30 22:02:12 +08001981 ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
1982 }
1983
Gilles Peskine449bd832023-01-11 14:50:10 +01001984 return ret;
Jerry Yu3bf2c642022-03-30 22:02:12 +08001985}
1986
1987/*
1988 * ssl_tls13_write_key_share_ext
1989 *
1990 * Structure of key_share extension in ServerHello:
1991 *
Jerry Yu1c3e6882022-04-20 21:23:40 +08001992 * struct {
1993 * NamedGroup group;
1994 * opaque key_exchange<1..2^16-1>;
1995 * } KeyShareEntry;
1996 * struct {
1997 * KeyShareEntry server_share;
1998 * } KeyShareServerHello;
Jerry Yu3bf2c642022-03-30 22:02:12 +08001999 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002000MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002001static int ssl_tls13_write_key_share_ext(mbedtls_ssl_context *ssl,
2002 unsigned char *buf,
2003 unsigned char *end,
2004 size_t *out_len)
Jerry Yu3bf2c642022-03-30 22:02:12 +08002005{
Jerry Yu955ddd72022-04-22 22:27:33 +08002006 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yu3bf2c642022-03-30 22:02:12 +08002007 unsigned char *p = buf;
Jerry Yu57d48412022-04-20 21:50:42 +08002008 uint16_t group = ssl->handshake->offered_group_id;
Jerry Yu3bf2c642022-03-30 22:02:12 +08002009 unsigned char *server_share = buf + 4;
Jerry Yu3bf2c642022-03-30 22:02:12 +08002010 size_t key_exchange_length;
Jerry Yu3bf2c642022-03-30 22:02:12 +08002011
2012 *out_len = 0;
2013
Gilles Peskine449bd832023-01-11 14:50:10 +01002014 MBEDTLS_SSL_DEBUG_MSG(3, ("server hello, adding key share extension"));
Jerry Yu3bf2c642022-03-30 22:02:12 +08002015
Gilles Peskine449bd832023-01-11 14:50:10 +01002016 MBEDTLS_SSL_DEBUG_MSG(2, ("server hello, write selected_group: %s (%04x)",
2017 mbedtls_ssl_named_group_to_str(group),
2018 group));
Jerry Yu58af2332022-09-06 11:19:31 +08002019
Jerry Yu3bf2c642022-03-30 22:02:12 +08002020 /* Check if we have space for header and length fields:
2021 * - extension_type (2 bytes)
2022 * - extension_data_length (2 bytes)
2023 * - group (2 bytes)
2024 * - key_exchange_length (2 bytes)
2025 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002026 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 8);
2027 MBEDTLS_PUT_UINT16_BE(MBEDTLS_TLS_EXT_KEY_SHARE, p, 0);
2028 MBEDTLS_PUT_UINT16_BE(group, server_share, 0);
Jerry Yu3bf2c642022-03-30 22:02:12 +08002029 p += 8;
Jerry Yu57d48412022-04-20 21:50:42 +08002030
Jerry Yu3bf2c642022-03-30 22:02:12 +08002031 /* When we introduce PQC-ECDHE hybrids, we'll want to call this
2032 * function multiple times. */
Jerry Yu955ddd72022-04-22 22:27:33 +08002033 ret = ssl_tls13_generate_and_write_key_share(
Gilles Peskine449bd832023-01-11 14:50:10 +01002034 ssl, group, server_share + 4, end, &key_exchange_length);
2035 if (ret != 0) {
2036 return ret;
2037 }
Jerry Yu3bf2c642022-03-30 22:02:12 +08002038 p += key_exchange_length;
Jerry Yuc1be19f2022-04-23 16:11:39 +08002039
Gilles Peskine449bd832023-01-11 14:50:10 +01002040 MBEDTLS_PUT_UINT16_BE(key_exchange_length, server_share + 2, 0);
Jerry Yu3bf2c642022-03-30 22:02:12 +08002041
Gilles Peskine449bd832023-01-11 14:50:10 +01002042 MBEDTLS_PUT_UINT16_BE(p - server_share, buf, 2);
Jerry Yu3bf2c642022-03-30 22:02:12 +08002043
Jerry Yu57d48412022-04-20 21:50:42 +08002044 *out_len = p - buf;
Jerry Yu955ddd72022-04-22 22:27:33 +08002045
Gilles Peskine449bd832023-01-11 14:50:10 +01002046 mbedtls_ssl_tls13_set_hs_sent_ext_mask(ssl, MBEDTLS_TLS_EXT_KEY_SHARE);
Jerry Yub95dd362022-11-08 21:19:34 +08002047
Gilles Peskine449bd832023-01-11 14:50:10 +01002048 return 0;
Jerry Yu3bf2c642022-03-30 22:02:12 +08002049}
Jerry Yud9436a12022-04-20 22:28:09 +08002050
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002051MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002052static int ssl_tls13_write_hrr_key_share_ext(mbedtls_ssl_context *ssl,
2053 unsigned char *buf,
2054 unsigned char *end,
2055 size_t *out_len)
Jerry Yu23f7a6f2022-04-23 15:16:45 +08002056{
2057 uint16_t selected_group = ssl->handshake->hrr_selected_group;
2058 /* key_share Extension
2059 *
2060 * struct {
2061 * select (Handshake.msg_type) {
2062 * ...
2063 * case hello_retry_request:
2064 * NamedGroup selected_group;
2065 * ...
2066 * };
2067 * } KeyShare;
2068 */
2069
2070 *out_len = 0;
2071
Jerry Yub0ac10b2022-05-05 11:10:08 +08002072 /*
2073 * For a pure PSK key exchange, there is no group to agree upon. The purpose
2074 * of the HRR is then to transmit a cookie to force the client to demonstrate
2075 * reachability at their apparent network address (primarily useful for DTLS).
2076 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002077 if (!mbedtls_ssl_tls13_key_exchange_mode_with_ephemeral(ssl)) {
2078 return 0;
2079 }
Jerry Yu23f7a6f2022-04-23 15:16:45 +08002080
2081 /* We should only send the key_share extension if the client's initial
2082 * key share was not acceptable. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002083 if (ssl->handshake->offered_group_id != 0) {
2084 MBEDTLS_SSL_DEBUG_MSG(4, ("Skip key_share extension in HRR"));
2085 return 0;
Jerry Yu23f7a6f2022-04-23 15:16:45 +08002086 }
2087
Gilles Peskine449bd832023-01-11 14:50:10 +01002088 if (selected_group == 0) {
2089 MBEDTLS_SSL_DEBUG_MSG(1, ("no matching named group found"));
2090 return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
Jerry Yu23f7a6f2022-04-23 15:16:45 +08002091 }
2092
Jerry Yub0ac10b2022-05-05 11:10:08 +08002093 /* Check if we have enough space:
2094 * - extension_type (2 bytes)
2095 * - extension_data_length (2 bytes)
2096 * - selected_group (2 bytes)
2097 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002098 MBEDTLS_SSL_CHK_BUF_PTR(buf, end, 6);
Jerry Yu23f7a6f2022-04-23 15:16:45 +08002099
Gilles Peskine449bd832023-01-11 14:50:10 +01002100 MBEDTLS_PUT_UINT16_BE(MBEDTLS_TLS_EXT_KEY_SHARE, buf, 0);
2101 MBEDTLS_PUT_UINT16_BE(2, buf, 2);
2102 MBEDTLS_PUT_UINT16_BE(selected_group, buf, 4);
Jerry Yu23f7a6f2022-04-23 15:16:45 +08002103
Gilles Peskine449bd832023-01-11 14:50:10 +01002104 MBEDTLS_SSL_DEBUG_MSG(3,
2105 ("HRR selected_group: %s (%x)",
2106 mbedtls_ssl_named_group_to_str(selected_group),
2107 selected_group));
Jerry Yu23f7a6f2022-04-23 15:16:45 +08002108
2109 *out_len = 6;
Jerry Yu23f7a6f2022-04-23 15:16:45 +08002110
Gilles Peskine449bd832023-01-11 14:50:10 +01002111 mbedtls_ssl_tls13_set_hs_sent_ext_mask(ssl, MBEDTLS_TLS_EXT_KEY_SHARE);
Jerry Yub95dd362022-11-08 21:19:34 +08002112
Gilles Peskine449bd832023-01-11 14:50:10 +01002113 return 0;
Jerry Yu23f7a6f2022-04-23 15:16:45 +08002114}
Jerry Yu3bf2c642022-03-30 22:02:12 +08002115
2116/*
2117 * Structure of ServerHello message:
2118 *
2119 * struct {
2120 * ProtocolVersion legacy_version = 0x0303; // TLS v1.2
2121 * Random random;
2122 * opaque legacy_session_id_echo<0..32>;
2123 * CipherSuite cipher_suite;
2124 * uint8 legacy_compression_method = 0;
2125 * Extension extensions<6..2^16-1>;
2126 * } ServerHello;
2127 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002128MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002129static int ssl_tls13_write_server_hello_body(mbedtls_ssl_context *ssl,
2130 unsigned char *buf,
2131 unsigned char *end,
2132 size_t *out_len,
2133 int is_hrr)
Jerry Yu56404d72022-03-30 17:36:13 +08002134{
Jerry Yu955ddd72022-04-22 22:27:33 +08002135 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yu3bf2c642022-03-30 22:02:12 +08002136 unsigned char *p = buf;
Jerry Yud9436a12022-04-20 22:28:09 +08002137 unsigned char *p_extensions_len;
Jerry Yufbe3e642022-04-25 19:31:51 +08002138 size_t output_len;
Jerry Yu3bf2c642022-03-30 22:02:12 +08002139
2140 *out_len = 0;
Jerry Yu50e00e32022-10-31 14:45:01 +08002141 ssl->handshake->sent_extensions = MBEDTLS_SSL_EXT_MASK_NONE;
Jerry Yu3bf2c642022-03-30 22:02:12 +08002142
Jerry Yucfc04b32022-04-21 09:31:58 +08002143 /* ...
2144 * ProtocolVersion legacy_version = 0x0303; // TLS 1.2
2145 * ...
2146 * with ProtocolVersion defined as:
2147 * uint16 ProtocolVersion;
Jerry Yu3bf2c642022-03-30 22:02:12 +08002148 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002149 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 2);
2150 MBEDTLS_PUT_UINT16_BE(0x0303, p, 0);
Jerry Yu3bf2c642022-03-30 22:02:12 +08002151 p += 2;
2152
Jerry Yu1c3e6882022-04-20 21:23:40 +08002153 /* ...
2154 * Random random;
2155 * ...
Jerry Yucfc04b32022-04-21 09:31:58 +08002156 * with Random defined as:
2157 * opaque Random[MBEDTLS_SERVER_HELLO_RANDOM_LEN];
Jerry Yu1c3e6882022-04-20 21:23:40 +08002158 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002159 MBEDTLS_SSL_CHK_BUF_PTR(p, end, MBEDTLS_SERVER_HELLO_RANDOM_LEN);
2160 if (is_hrr) {
2161 memcpy(p, mbedtls_ssl_tls13_hello_retry_request_magic,
2162 MBEDTLS_SERVER_HELLO_RANDOM_LEN);
2163 } else {
2164 memcpy(p, &ssl->handshake->randbytes[MBEDTLS_CLIENT_HELLO_RANDOM_LEN],
2165 MBEDTLS_SERVER_HELLO_RANDOM_LEN);
Jerry Yu23f7a6f2022-04-23 15:16:45 +08002166 }
Gilles Peskine449bd832023-01-11 14:50:10 +01002167 MBEDTLS_SSL_DEBUG_BUF(3, "server hello, random bytes",
2168 p, MBEDTLS_SERVER_HELLO_RANDOM_LEN);
Jerry Yu3bf2c642022-03-30 22:02:12 +08002169 p += MBEDTLS_SERVER_HELLO_RANDOM_LEN;
2170
Jerry Yucfc04b32022-04-21 09:31:58 +08002171 /* ...
2172 * opaque legacy_session_id_echo<0..32>;
2173 * ...
Jerry Yu3bf2c642022-03-30 22:02:12 +08002174 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002175 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 1 + ssl->session_negotiate->id_len);
2176 *p++ = (unsigned char) ssl->session_negotiate->id_len;
2177 if (ssl->session_negotiate->id_len > 0) {
2178 memcpy(p, &ssl->session_negotiate->id[0],
2179 ssl->session_negotiate->id_len);
Jerry Yu3bf2c642022-03-30 22:02:12 +08002180 p += ssl->session_negotiate->id_len;
Jerry Yu955ddd72022-04-22 22:27:33 +08002181
Gilles Peskine449bd832023-01-11 14:50:10 +01002182 MBEDTLS_SSL_DEBUG_BUF(3, "session id", ssl->session_negotiate->id,
2183 ssl->session_negotiate->id_len);
Jerry Yu3bf2c642022-03-30 22:02:12 +08002184 }
2185
Jerry Yucfc04b32022-04-21 09:31:58 +08002186 /* ...
2187 * CipherSuite cipher_suite;
2188 * ...
2189 * with CipherSuite defined as:
2190 * uint8 CipherSuite[2];
Jerry Yu3bf2c642022-03-30 22:02:12 +08002191 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002192 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 2);
2193 MBEDTLS_PUT_UINT16_BE(ssl->session_negotiate->ciphersuite, p, 0);
Jerry Yu3bf2c642022-03-30 22:02:12 +08002194 p += 2;
Gilles Peskine449bd832023-01-11 14:50:10 +01002195 MBEDTLS_SSL_DEBUG_MSG(3,
2196 ("server hello, chosen ciphersuite: %s ( id=%d )",
2197 mbedtls_ssl_get_ciphersuite_name(
2198 ssl->session_negotiate->ciphersuite),
2199 ssl->session_negotiate->ciphersuite));
Jerry Yu3bf2c642022-03-30 22:02:12 +08002200
Jerry Yucfc04b32022-04-21 09:31:58 +08002201 /* ...
2202 * uint8 legacy_compression_method = 0;
2203 * ...
2204 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002205 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 1);
Thomas Daubney31e03a82022-07-25 15:59:25 +01002206 *p++ = MBEDTLS_SSL_COMPRESS_NULL;
Jerry Yu3bf2c642022-03-30 22:02:12 +08002207
Jerry Yucfc04b32022-04-21 09:31:58 +08002208 /* ...
2209 * Extension extensions<6..2^16-1>;
2210 * ...
2211 * struct {
2212 * ExtensionType extension_type; (2 bytes)
2213 * opaque extension_data<0..2^16-1>;
2214 * } Extension;
2215 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002216 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 2);
Jerry Yud9436a12022-04-20 22:28:09 +08002217 p_extensions_len = p;
Jerry Yu3bf2c642022-03-30 22:02:12 +08002218 p += 2;
2219
Gilles Peskine449bd832023-01-11 14:50:10 +01002220 if ((ret = ssl_tls13_write_server_hello_supported_versions_ext(
2221 ssl, p, end, &output_len)) != 0) {
Jerry Yu955ddd72022-04-22 22:27:33 +08002222 MBEDTLS_SSL_DEBUG_RET(
Gilles Peskine449bd832023-01-11 14:50:10 +01002223 1, "ssl_tls13_write_server_hello_supported_versions_ext", ret);
2224 return ret;
Jerry Yu3bf2c642022-03-30 22:02:12 +08002225 }
2226 p += output_len;
2227
Gilles Peskine449bd832023-01-11 14:50:10 +01002228 if (mbedtls_ssl_tls13_key_exchange_mode_with_ephemeral(ssl)) {
2229 if (is_hrr) {
2230 ret = ssl_tls13_write_hrr_key_share_ext(ssl, p, end, &output_len);
2231 } else {
2232 ret = ssl_tls13_write_key_share_ext(ssl, p, end, &output_len);
2233 }
2234 if (ret != 0) {
2235 return ret;
2236 }
Jerry Yu3bf2c642022-03-30 22:02:12 +08002237 p += output_len;
2238 }
Jerry Yu3bf2c642022-03-30 22:02:12 +08002239
Ronald Cron41a443a2022-10-04 16:38:25 +02002240#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01002241 if (!is_hrr && mbedtls_ssl_tls13_key_exchange_mode_with_psk(ssl)) {
2242 ret = ssl_tls13_write_server_pre_shared_key_ext(ssl, p, end, &output_len);
2243 if (ret != 0) {
2244 MBEDTLS_SSL_DEBUG_RET(1, "ssl_tls13_write_server_pre_shared_key_ext",
2245 ret);
2246 return ret;
Jerry Yu032b15ce2022-07-11 06:10:03 +00002247 }
2248 p += output_len;
2249 }
Ronald Cron41a443a2022-10-04 16:38:25 +02002250#endif
Jerry Yu032b15ce2022-07-11 06:10:03 +00002251
Gilles Peskine449bd832023-01-11 14:50:10 +01002252 MBEDTLS_PUT_UINT16_BE(p - p_extensions_len - 2, p_extensions_len, 0);
Jerry Yu3bf2c642022-03-30 22:02:12 +08002253
Gilles Peskine449bd832023-01-11 14:50:10 +01002254 MBEDTLS_SSL_DEBUG_BUF(4, "server hello extensions",
2255 p_extensions_len, p - p_extensions_len);
Jerry Yu3bf2c642022-03-30 22:02:12 +08002256
Jerry Yud9436a12022-04-20 22:28:09 +08002257 *out_len = p - buf;
Jerry Yu3bf2c642022-03-30 22:02:12 +08002258
Gilles Peskine449bd832023-01-11 14:50:10 +01002259 MBEDTLS_SSL_DEBUG_BUF(3, "server hello", buf, *out_len);
Jerry Yu3bf2c642022-03-30 22:02:12 +08002260
Jerry Yu7de2ff02022-11-08 21:43:46 +08002261 MBEDTLS_SSL_PRINT_EXTS(
Jerry Yu4b8f2f72022-10-31 13:31:22 +08002262 3, is_hrr ? MBEDTLS_SSL_TLS1_3_HS_HELLO_RETRY_REQUEST :
Gilles Peskine449bd832023-01-11 14:50:10 +01002263 MBEDTLS_SSL_HS_SERVER_HELLO,
2264 ssl->handshake->sent_extensions);
Jerry Yu4b8f2f72022-10-31 13:31:22 +08002265
Gilles Peskine449bd832023-01-11 14:50:10 +01002266 return ret;
Jerry Yu56404d72022-03-30 17:36:13 +08002267}
2268
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002269MBEDTLS_CHECK_RETURN_CRITICAL
Xiaokang Qian934ce6f2023-02-06 10:23:04 +00002270static int ssl_tls13_finalize_server_hello(mbedtls_ssl_context *ssl)
Jerry Yue110d252022-05-05 10:19:22 +08002271{
2272 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine449bd832023-01-11 14:50:10 +01002273 ret = mbedtls_ssl_tls13_compute_handshake_transform(ssl);
2274 if (ret != 0) {
2275 MBEDTLS_SSL_DEBUG_RET(1,
2276 "mbedtls_ssl_tls13_compute_handshake_transform",
2277 ret);
2278 return ret;
Jerry Yue110d252022-05-05 10:19:22 +08002279 }
2280
Gilles Peskine449bd832023-01-11 14:50:10 +01002281 return ret;
Jerry Yue110d252022-05-05 10:19:22 +08002282}
2283
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002284MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002285static int ssl_tls13_write_server_hello(mbedtls_ssl_context *ssl)
Jerry Yu5b64ae92022-03-30 17:15:02 +08002286{
Jerry Yu637a3f12022-04-20 21:37:58 +08002287 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yuf4b27e42022-03-30 17:32:21 +08002288 unsigned char *buf;
2289 size_t buf_len, msg_len;
2290
Gilles Peskine449bd832023-01-11 14:50:10 +01002291 MBEDTLS_SSL_DEBUG_MSG(2, ("=> write server hello"));
Jerry Yuf4b27e42022-03-30 17:32:21 +08002292
Gilles Peskine449bd832023-01-11 14:50:10 +01002293 MBEDTLS_SSL_PROC_CHK(ssl_tls13_prepare_server_hello(ssl));
Jerry Yuf4b27e42022-03-30 17:32:21 +08002294
Xiaokang Qian9f1747b2023-03-30 02:50:04 +00002295 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_start_handshake_msg(
2296 ssl, MBEDTLS_SSL_HS_SERVER_HELLO, &buf, &buf_len));
Jerry Yuf4b27e42022-03-30 17:32:21 +08002297
Gilles Peskine449bd832023-01-11 14:50:10 +01002298 MBEDTLS_SSL_PROC_CHK(ssl_tls13_write_server_hello_body(ssl, buf,
2299 buf + buf_len,
2300 &msg_len,
2301 0));
Jerry Yuf4b27e42022-03-30 17:32:21 +08002302
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +01002303 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_add_hs_msg_to_checksum(
Manuel Pégourié-Gonnard43cc1272023-02-06 11:48:19 +01002304 ssl, MBEDTLS_SSL_HS_SERVER_HELLO, buf, msg_len));
Jerry Yuf4b27e42022-03-30 17:32:21 +08002305
Gilles Peskine449bd832023-01-11 14:50:10 +01002306 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_finish_handshake_msg(
2307 ssl, buf_len, msg_len));
Jerry Yu637a3f12022-04-20 21:37:58 +08002308
Xiaokang Qian934ce6f2023-02-06 10:23:04 +00002309 MBEDTLS_SSL_PROC_CHK(ssl_tls13_finalize_server_hello(ssl));
Jerry Yue110d252022-05-05 10:19:22 +08002310
Gabor Mezei7b39bf12022-05-24 16:04:14 +02002311#if defined(MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE)
2312 /* The server sends a dummy change_cipher_spec record immediately
2313 * after its first handshake message. This may either be after
2314 * a ServerHello or a HelloRetryRequest.
2315 */
Gabor Mezei96ae9262022-06-28 11:45:18 +02002316 mbedtls_ssl_handshake_set_state(
Gilles Peskine449bd832023-01-11 14:50:10 +01002317 ssl, MBEDTLS_SSL_SERVER_CCS_AFTER_SERVER_HELLO);
Gabor Mezei7b39bf12022-05-24 16:04:14 +02002318#else
Gilles Peskine449bd832023-01-11 14:50:10 +01002319 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_ENCRYPTED_EXTENSIONS);
Gabor Mezei7b39bf12022-05-24 16:04:14 +02002320#endif /* MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE */
Jerry Yue110d252022-05-05 10:19:22 +08002321
Jerry Yuf4b27e42022-03-30 17:32:21 +08002322cleanup:
2323
Gilles Peskine449bd832023-01-11 14:50:10 +01002324 MBEDTLS_SSL_DEBUG_MSG(2, ("<= write server hello"));
2325 return ret;
Jerry Yu5b64ae92022-03-30 17:15:02 +08002326}
2327
Jerry Yu23d1a252022-05-12 18:08:59 +08002328
2329/*
2330 * Handler for MBEDTLS_SSL_HELLO_RETRY_REQUEST
2331 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002332MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002333static int ssl_tls13_prepare_hello_retry_request(mbedtls_ssl_context *ssl)
Jerry Yu23d1a252022-05-12 18:08:59 +08002334{
2335 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine449bd832023-01-11 14:50:10 +01002336 if (ssl->handshake->hello_retry_request_count > 0) {
2337 MBEDTLS_SSL_DEBUG_MSG(1, ("Too many HRRs"));
2338 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE,
2339 MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE);
2340 return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
Jerry Yu23d1a252022-05-12 18:08:59 +08002341 }
2342
2343 /*
2344 * Create stateless transcript hash for HRR
2345 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002346 MBEDTLS_SSL_DEBUG_MSG(4, ("Reset transcript for HRR"));
2347 ret = mbedtls_ssl_reset_transcript_for_hrr(ssl);
2348 if (ret != 0) {
2349 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_reset_transcript_for_hrr", ret);
2350 return ret;
Jerry Yu23d1a252022-05-12 18:08:59 +08002351 }
Gilles Peskine449bd832023-01-11 14:50:10 +01002352 mbedtls_ssl_session_reset_msg_layer(ssl, 0);
Jerry Yu23d1a252022-05-12 18:08:59 +08002353
Gilles Peskine449bd832023-01-11 14:50:10 +01002354 return 0;
Jerry Yu23d1a252022-05-12 18:08:59 +08002355}
2356
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002357MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002358static int ssl_tls13_write_hello_retry_request(mbedtls_ssl_context *ssl)
Jerry Yu23d1a252022-05-12 18:08:59 +08002359{
2360 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
2361 unsigned char *buf;
2362 size_t buf_len, msg_len;
2363
Gilles Peskine449bd832023-01-11 14:50:10 +01002364 MBEDTLS_SSL_DEBUG_MSG(2, ("=> write hello retry request"));
Jerry Yu23d1a252022-05-12 18:08:59 +08002365
Gilles Peskine449bd832023-01-11 14:50:10 +01002366 MBEDTLS_SSL_PROC_CHK(ssl_tls13_prepare_hello_retry_request(ssl));
Jerry Yu23d1a252022-05-12 18:08:59 +08002367
Gilles Peskine449bd832023-01-11 14:50:10 +01002368 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_start_handshake_msg(
2369 ssl, MBEDTLS_SSL_HS_SERVER_HELLO,
2370 &buf, &buf_len));
Jerry Yu23d1a252022-05-12 18:08:59 +08002371
Gilles Peskine449bd832023-01-11 14:50:10 +01002372 MBEDTLS_SSL_PROC_CHK(ssl_tls13_write_server_hello_body(ssl, buf,
2373 buf + buf_len,
2374 &msg_len,
2375 1));
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +01002376 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_add_hs_msg_to_checksum(
Manuel Pégourié-Gonnard43cc1272023-02-06 11:48:19 +01002377 ssl, MBEDTLS_SSL_HS_SERVER_HELLO, buf, msg_len));
Jerry Yu23d1a252022-05-12 18:08:59 +08002378
2379
Gilles Peskine449bd832023-01-11 14:50:10 +01002380 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_finish_handshake_msg(ssl, buf_len,
2381 msg_len));
Jerry Yu23d1a252022-05-12 18:08:59 +08002382
2383 ssl->handshake->hello_retry_request_count++;
2384
Gabor Mezei7b39bf12022-05-24 16:04:14 +02002385#if defined(MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE)
2386 /* The server sends a dummy change_cipher_spec record immediately
2387 * after its first handshake message. This may either be after
2388 * a ServerHello or a HelloRetryRequest.
2389 */
Gabor Mezei96ae9262022-06-28 11:45:18 +02002390 mbedtls_ssl_handshake_set_state(
Gilles Peskine449bd832023-01-11 14:50:10 +01002391 ssl, MBEDTLS_SSL_SERVER_CCS_AFTER_HELLO_RETRY_REQUEST);
Gabor Mezei7b39bf12022-05-24 16:04:14 +02002392#else
Gilles Peskine449bd832023-01-11 14:50:10 +01002393 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_CLIENT_HELLO);
Gabor Mezei7b39bf12022-05-24 16:04:14 +02002394#endif /* MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE */
Jerry Yu23d1a252022-05-12 18:08:59 +08002395
2396cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +01002397 MBEDTLS_SSL_DEBUG_MSG(2, ("<= write hello retry request"));
2398 return ret;
Jerry Yu23d1a252022-05-12 18:08:59 +08002399}
2400
Jerry Yu5b64ae92022-03-30 17:15:02 +08002401/*
Jerry Yu4d3841a2022-04-16 12:37:19 +08002402 * Handler for MBEDTLS_SSL_ENCRYPTED_EXTENSIONS
2403 */
Jerry Yu4d3841a2022-04-16 12:37:19 +08002404
2405/*
2406 * struct {
2407 * Extension extensions<0..2 ^ 16 - 1>;
2408 * } EncryptedExtensions;
2409 *
2410 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002411MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002412static int ssl_tls13_write_encrypted_extensions_body(mbedtls_ssl_context *ssl,
2413 unsigned char *buf,
2414 unsigned char *end,
2415 size_t *out_len)
Jerry Yu4d3841a2022-04-16 12:37:19 +08002416{
XiaokangQianacb39922022-06-17 10:18:48 +00002417 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yu4d3841a2022-04-16 12:37:19 +08002418 unsigned char *p = buf;
2419 size_t extensions_len = 0;
Jerry Yu8937eb42022-05-03 12:12:14 +08002420 unsigned char *p_extensions_len;
XiaokangQianacb39922022-06-17 10:18:48 +00002421 size_t output_len;
Jerry Yu9da5e5a2022-05-03 15:46:09 +08002422
Jerry Yu4d3841a2022-04-16 12:37:19 +08002423 *out_len = 0;
2424
Gilles Peskine449bd832023-01-11 14:50:10 +01002425 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 2);
Jerry Yu8937eb42022-05-03 12:12:14 +08002426 p_extensions_len = p;
Jerry Yu4d3841a2022-04-16 12:37:19 +08002427 p += 2;
2428
Jerry Yu8937eb42022-05-03 12:12:14 +08002429 ((void) ssl);
XiaokangQianacb39922022-06-17 10:18:48 +00002430 ((void) ret);
2431 ((void) output_len);
2432
2433#if defined(MBEDTLS_SSL_ALPN)
Gilles Peskine449bd832023-01-11 14:50:10 +01002434 ret = mbedtls_ssl_write_alpn_ext(ssl, p, end, &output_len);
2435 if (ret != 0) {
2436 return ret;
2437 }
XiaokangQian95d5f542022-06-24 02:29:26 +00002438 p += output_len;
XiaokangQianacb39922022-06-17 10:18:48 +00002439#endif /* MBEDTLS_SSL_ALPN */
Jerry Yu4d3841a2022-04-16 12:37:19 +08002440
Gilles Peskine449bd832023-01-11 14:50:10 +01002441 extensions_len = (p - p_extensions_len) - 2;
2442 MBEDTLS_PUT_UINT16_BE(extensions_len, p_extensions_len, 0);
Jerry Yu8937eb42022-05-03 12:12:14 +08002443
2444 *out_len = p - buf;
Jerry Yu4d3841a2022-04-16 12:37:19 +08002445
Gilles Peskine449bd832023-01-11 14:50:10 +01002446 MBEDTLS_SSL_DEBUG_BUF(4, "encrypted extensions", buf, *out_len);
Jerry Yu4d3841a2022-04-16 12:37:19 +08002447
Jerry Yu7de2ff02022-11-08 21:43:46 +08002448 MBEDTLS_SSL_PRINT_EXTS(
Gilles Peskine449bd832023-01-11 14:50:10 +01002449 3, MBEDTLS_SSL_HS_ENCRYPTED_EXTENSIONS, ssl->handshake->sent_extensions);
Jerry Yu4b8f2f72022-10-31 13:31:22 +08002450
Gilles Peskine449bd832023-01-11 14:50:10 +01002451 return 0;
Jerry Yu4d3841a2022-04-16 12:37:19 +08002452}
2453
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002454MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002455static int ssl_tls13_write_encrypted_extensions(mbedtls_ssl_context *ssl)
Jerry Yu4d3841a2022-04-16 12:37:19 +08002456{
2457 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
2458 unsigned char *buf;
Jerry Yu39730a72022-05-03 12:14:04 +08002459 size_t buf_len, msg_len;
Jerry Yu4d3841a2022-04-16 12:37:19 +08002460
Gilles Peskine449bd832023-01-11 14:50:10 +01002461 mbedtls_ssl_set_outbound_transform(ssl,
2462 ssl->handshake->transform_handshake);
Gabor Mezei54719122022-06-28 11:34:56 +02002463 MBEDTLS_SSL_DEBUG_MSG(
Gilles Peskine449bd832023-01-11 14:50:10 +01002464 3, ("switching to handshake transform for outbound data"));
Gabor Mezei54719122022-06-28 11:34:56 +02002465
Gilles Peskine449bd832023-01-11 14:50:10 +01002466 MBEDTLS_SSL_DEBUG_MSG(2, ("=> write encrypted extensions"));
Jerry Yu4d3841a2022-04-16 12:37:19 +08002467
Xiaokang Qian9f1747b2023-03-30 02:50:04 +00002468 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_start_handshake_msg(
2469 ssl, MBEDTLS_SSL_HS_ENCRYPTED_EXTENSIONS,
2470 &buf, &buf_len));
Jerry Yu4d3841a2022-04-16 12:37:19 +08002471
Gilles Peskine449bd832023-01-11 14:50:10 +01002472 MBEDTLS_SSL_PROC_CHK(ssl_tls13_write_encrypted_extensions_body(
2473 ssl, buf, buf + buf_len, &msg_len));
Jerry Yu4d3841a2022-04-16 12:37:19 +08002474
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +01002475 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_add_hs_msg_to_checksum(
Xiaokang Qian9f1747b2023-03-30 02:50:04 +00002476 ssl, MBEDTLS_SSL_HS_ENCRYPTED_EXTENSIONS,
2477 buf, msg_len));
Jerry Yu4d3841a2022-04-16 12:37:19 +08002478
Gilles Peskine449bd832023-01-11 14:50:10 +01002479 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_finish_handshake_msg(
2480 ssl, buf_len, msg_len));
Jerry Yu4d3841a2022-04-16 12:37:19 +08002481
Ronald Cron928cbd32022-10-04 16:14:26 +02002482#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01002483 if (mbedtls_ssl_tls13_key_exchange_mode_with_psk(ssl)) {
2484 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_SERVER_FINISHED);
2485 } else {
2486 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_CERTIFICATE_REQUEST);
2487 }
Jerry Yu8937eb42022-05-03 12:12:14 +08002488#else
Gilles Peskine449bd832023-01-11 14:50:10 +01002489 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_SERVER_FINISHED);
Jerry Yu8937eb42022-05-03 12:12:14 +08002490#endif
2491
Jerry Yu4d3841a2022-04-16 12:37:19 +08002492cleanup:
2493
Gilles Peskine449bd832023-01-11 14:50:10 +01002494 MBEDTLS_SSL_DEBUG_MSG(2, ("<= write encrypted extensions"));
2495 return ret;
Jerry Yu4d3841a2022-04-16 12:37:19 +08002496}
2497
Ronald Cron928cbd32022-10-04 16:14:26 +02002498#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED)
XiaokangQiana987e1d2022-05-07 01:25:58 +00002499#define SSL_CERTIFICATE_REQUEST_SEND_REQUEST 0
2500#define SSL_CERTIFICATE_REQUEST_SKIP 1
2501/* Coordination:
2502 * Check whether a CertificateRequest message should be written.
2503 * Returns a negative code on failure, or
2504 * - SSL_CERTIFICATE_REQUEST_SEND_REQUEST
2505 * - SSL_CERTIFICATE_REQUEST_SKIP
2506 * indicating if the writing of the CertificateRequest
2507 * should be skipped or not.
2508 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002509MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002510static int ssl_tls13_certificate_request_coordinate(mbedtls_ssl_context *ssl)
XiaokangQiana987e1d2022-05-07 01:25:58 +00002511{
2512 int authmode;
2513
XiaokangQian40a35232022-05-07 09:02:40 +00002514#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Gilles Peskine449bd832023-01-11 14:50:10 +01002515 if (ssl->handshake->sni_authmode != MBEDTLS_SSL_VERIFY_UNSET) {
XiaokangQian40a35232022-05-07 09:02:40 +00002516 authmode = ssl->handshake->sni_authmode;
Gilles Peskine449bd832023-01-11 14:50:10 +01002517 } else
XiaokangQian40a35232022-05-07 09:02:40 +00002518#endif
XiaokangQiana987e1d2022-05-07 01:25:58 +00002519 authmode = ssl->conf->authmode;
2520
Gilles Peskine449bd832023-01-11 14:50:10 +01002521 if (authmode == MBEDTLS_SSL_VERIFY_NONE) {
Ronald Croneac00ad2022-09-13 10:16:31 +02002522 ssl->session_negotiate->verify_result = MBEDTLS_X509_BADCERT_SKIP_VERIFY;
Gilles Peskine449bd832023-01-11 14:50:10 +01002523 return SSL_CERTIFICATE_REQUEST_SKIP;
Ronald Croneac00ad2022-09-13 10:16:31 +02002524 }
XiaokangQiana987e1d2022-05-07 01:25:58 +00002525
XiaokangQianc3017f62022-05-13 05:55:41 +00002526 ssl->handshake->certificate_request_sent = 1;
XiaokangQian189ded22022-05-10 08:12:17 +00002527
Gilles Peskine449bd832023-01-11 14:50:10 +01002528 return SSL_CERTIFICATE_REQUEST_SEND_REQUEST;
XiaokangQiana987e1d2022-05-07 01:25:58 +00002529}
2530
XiaokangQiancec9ae62022-05-06 07:28:50 +00002531/*
2532 * struct {
2533 * opaque certificate_request_context<0..2^8-1>;
2534 * Extension extensions<2..2^16-1>;
2535 * } CertificateRequest;
2536 *
2537 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002538MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002539static int ssl_tls13_write_certificate_request_body(mbedtls_ssl_context *ssl,
2540 unsigned char *buf,
2541 const unsigned char *end,
2542 size_t *out_len)
XiaokangQiancec9ae62022-05-06 07:28:50 +00002543{
2544 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
2545 unsigned char *p = buf;
XiaokangQianec6efb92022-05-06 09:53:10 +00002546 size_t output_len = 0;
XiaokangQiancec9ae62022-05-06 07:28:50 +00002547 unsigned char *p_extensions_len;
2548
2549 *out_len = 0;
2550
2551 /* Check if we have enough space:
2552 * - certificate_request_context (1 byte)
2553 * - extensions length (2 bytes)
2554 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002555 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 3);
XiaokangQiancec9ae62022-05-06 07:28:50 +00002556
2557 /*
2558 * Write certificate_request_context
2559 */
2560 /*
2561 * We use a zero length context for the normal handshake
2562 * messages. For post-authentication handshake messages
2563 * this request context would be set to a non-zero value.
2564 */
2565 *p++ = 0x0;
2566
2567 /*
2568 * Write extensions
2569 */
2570 /* The extensions must contain the signature_algorithms. */
2571 p_extensions_len = p;
2572 p += 2;
Gilles Peskine449bd832023-01-11 14:50:10 +01002573 ret = mbedtls_ssl_write_sig_alg_ext(ssl, p, end, &output_len);
2574 if (ret != 0) {
2575 return ret;
2576 }
XiaokangQiancec9ae62022-05-06 07:28:50 +00002577
XiaokangQianec6efb92022-05-06 09:53:10 +00002578 p += output_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01002579 MBEDTLS_PUT_UINT16_BE(p - p_extensions_len - 2, p_extensions_len, 0);
XiaokangQiancec9ae62022-05-06 07:28:50 +00002580
2581 *out_len = p - buf;
2582
Jerry Yu7de2ff02022-11-08 21:43:46 +08002583 MBEDTLS_SSL_PRINT_EXTS(
Gilles Peskine449bd832023-01-11 14:50:10 +01002584 3, MBEDTLS_SSL_HS_CERTIFICATE_REQUEST, ssl->handshake->sent_extensions);
Jerry Yu4b8f2f72022-10-31 13:31:22 +08002585
Gilles Peskine449bd832023-01-11 14:50:10 +01002586 return 0;
XiaokangQiancec9ae62022-05-06 07:28:50 +00002587}
2588
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002589MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002590static int ssl_tls13_write_certificate_request(mbedtls_ssl_context *ssl)
XiaokangQiancec9ae62022-05-06 07:28:50 +00002591{
2592 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
2593
Gilles Peskine449bd832023-01-11 14:50:10 +01002594 MBEDTLS_SSL_DEBUG_MSG(2, ("=> write certificate request"));
XiaokangQiancec9ae62022-05-06 07:28:50 +00002595
Gilles Peskine449bd832023-01-11 14:50:10 +01002596 MBEDTLS_SSL_PROC_CHK_NEG(ssl_tls13_certificate_request_coordinate(ssl));
XiaokangQiancec9ae62022-05-06 07:28:50 +00002597
Gilles Peskine449bd832023-01-11 14:50:10 +01002598 if (ret == SSL_CERTIFICATE_REQUEST_SEND_REQUEST) {
XiaokangQiancec9ae62022-05-06 07:28:50 +00002599 unsigned char *buf;
2600 size_t buf_len, msg_len;
2601
Xiaokang Qian9f1747b2023-03-30 02:50:04 +00002602 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_start_handshake_msg(
2603 ssl, MBEDTLS_SSL_HS_CERTIFICATE_REQUEST,
2604 &buf, &buf_len));
XiaokangQiancec9ae62022-05-06 07:28:50 +00002605
Gilles Peskine449bd832023-01-11 14:50:10 +01002606 MBEDTLS_SSL_PROC_CHK(ssl_tls13_write_certificate_request_body(
2607 ssl, buf, buf + buf_len, &msg_len));
XiaokangQiancec9ae62022-05-06 07:28:50 +00002608
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +01002609 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_add_hs_msg_to_checksum(
Xiaokang Qian9f1747b2023-03-30 02:50:04 +00002610 ssl, MBEDTLS_SSL_HS_CERTIFICATE_REQUEST,
2611 buf, msg_len));
XiaokangQiancec9ae62022-05-06 07:28:50 +00002612
Gilles Peskine449bd832023-01-11 14:50:10 +01002613 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_finish_handshake_msg(
2614 ssl, buf_len, msg_len));
2615 } else if (ret == SSL_CERTIFICATE_REQUEST_SKIP) {
2616 MBEDTLS_SSL_DEBUG_MSG(2, ("<= skip write certificate request"));
XiaokangQiancec9ae62022-05-06 07:28:50 +00002617 ret = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01002618 } else {
2619 MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
XiaokangQiancec9ae62022-05-06 07:28:50 +00002620 ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
2621 goto cleanup;
2622 }
2623
Gilles Peskine449bd832023-01-11 14:50:10 +01002624 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_SERVER_CERTIFICATE);
XiaokangQiancec9ae62022-05-06 07:28:50 +00002625cleanup:
2626
Gilles Peskine449bd832023-01-11 14:50:10 +01002627 MBEDTLS_SSL_DEBUG_MSG(2, ("<= write certificate request"));
2628 return ret;
XiaokangQiancec9ae62022-05-06 07:28:50 +00002629}
XiaokangQiancec9ae62022-05-06 07:28:50 +00002630
Jerry Yu4d3841a2022-04-16 12:37:19 +08002631/*
Jerry Yuc6e6dbf2022-04-16 19:42:57 +08002632 * Handler for MBEDTLS_SSL_SERVER_CERTIFICATE
Jerry Yu83da34e2022-04-16 13:59:52 +08002633 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002634MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002635static int ssl_tls13_write_server_certificate(mbedtls_ssl_context *ssl)
Jerry Yu83da34e2022-04-16 13:59:52 +08002636{
Jerry Yu5a26f302022-05-10 20:46:40 +08002637 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
XiaokangQianfb665a82022-06-15 03:57:21 +00002638
2639#if defined(MBEDTLS_X509_CRT_PARSE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01002640 if ((ssl_tls13_pick_key_cert(ssl) != 0) ||
2641 mbedtls_ssl_own_cert(ssl) == NULL) {
2642 MBEDTLS_SSL_DEBUG_MSG(2, ("No certificate available."));
2643 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE,
2644 MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE);
2645 return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
Jerry Yu5a26f302022-05-10 20:46:40 +08002646 }
XiaokangQianfb665a82022-06-15 03:57:21 +00002647#endif /* MBEDTLS_X509_CRT_PARSE_C */
Jerry Yu5a26f302022-05-10 20:46:40 +08002648
Gilles Peskine449bd832023-01-11 14:50:10 +01002649 ret = mbedtls_ssl_tls13_write_certificate(ssl);
2650 if (ret != 0) {
2651 return ret;
2652 }
2653 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_CERTIFICATE_VERIFY);
2654 return 0;
Jerry Yu83da34e2022-04-16 13:59:52 +08002655}
2656
2657/*
Jerry Yuc6e6dbf2022-04-16 19:42:57 +08002658 * Handler for MBEDTLS_SSL_CERTIFICATE_VERIFY
Jerry Yu83da34e2022-04-16 13:59:52 +08002659 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002660MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002661static int ssl_tls13_write_certificate_verify(mbedtls_ssl_context *ssl)
Jerry Yu83da34e2022-04-16 13:59:52 +08002662{
Gilles Peskine449bd832023-01-11 14:50:10 +01002663 int ret = mbedtls_ssl_tls13_write_certificate_verify(ssl);
2664 if (ret != 0) {
2665 return ret;
2666 }
2667 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_SERVER_FINISHED);
2668 return 0;
Jerry Yu83da34e2022-04-16 13:59:52 +08002669}
Ronald Cron928cbd32022-10-04 16:14:26 +02002670#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED */
Jerry Yu83da34e2022-04-16 13:59:52 +08002671
2672/*
Jerry Yud6e253d2022-05-18 13:59:24 +08002673 * Handler for MBEDTLS_SSL_SERVER_FINISHED
Jerry Yu69dd8d42022-04-16 12:51:26 +08002674 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002675MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002676static int ssl_tls13_write_server_finished(mbedtls_ssl_context *ssl)
Jerry Yu69dd8d42022-04-16 12:51:26 +08002677{
Jerry Yud6e253d2022-05-18 13:59:24 +08002678 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yu27bdc7c2022-04-16 13:33:27 +08002679
Gilles Peskine449bd832023-01-11 14:50:10 +01002680 ret = mbedtls_ssl_tls13_write_finished_message(ssl);
2681 if (ret != 0) {
2682 return ret;
2683 }
Jerry Yu27bdc7c2022-04-16 13:33:27 +08002684
Gilles Peskine449bd832023-01-11 14:50:10 +01002685 ret = mbedtls_ssl_tls13_compute_application_transform(ssl);
2686 if (ret != 0) {
Jerry Yue3d67cb2022-05-19 15:33:10 +08002687 MBEDTLS_SSL_PEND_FATAL_ALERT(
Gilles Peskine449bd832023-01-11 14:50:10 +01002688 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE,
2689 MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE);
2690 return ret;
Jerry Yue3d67cb2022-05-19 15:33:10 +08002691 }
XiaokangQianc3017f62022-05-13 05:55:41 +00002692
Gilles Peskine449bd832023-01-11 14:50:10 +01002693 MBEDTLS_SSL_DEBUG_MSG(1, ("Switch to handshake keys for inbound traffic"));
2694 mbedtls_ssl_set_inbound_transform(ssl, ssl->handshake->transform_handshake);
XiaokangQian189ded22022-05-10 08:12:17 +00002695
Gilles Peskine449bd832023-01-11 14:50:10 +01002696 if (ssl->handshake->certificate_request_sent) {
2697 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_CLIENT_CERTIFICATE);
2698 } else {
2699 MBEDTLS_SSL_DEBUG_MSG(2, ("skip parse certificate"));
2700 MBEDTLS_SSL_DEBUG_MSG(2, ("skip parse certificate verify"));
2701 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_CLIENT_FINISHED);
Ronald Cron19385882022-06-15 16:26:13 +02002702 }
Ronald Cron63dc4632022-05-31 14:41:53 +02002703
Gilles Peskine449bd832023-01-11 14:50:10 +01002704 return 0;
Jerry Yu69dd8d42022-04-16 12:51:26 +08002705}
2706
2707/*
Jerry Yud6e253d2022-05-18 13:59:24 +08002708 * Handler for MBEDTLS_SSL_CLIENT_FINISHED
Jerry Yu69dd8d42022-04-16 12:51:26 +08002709 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002710MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002711static int ssl_tls13_process_client_finished(mbedtls_ssl_context *ssl)
Jerry Yu69dd8d42022-04-16 12:51:26 +08002712{
Jerry Yud6e253d2022-05-18 13:59:24 +08002713 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
2714
Gilles Peskine449bd832023-01-11 14:50:10 +01002715 ret = mbedtls_ssl_tls13_process_finished_message(ssl);
2716 if (ret != 0) {
2717 return ret;
Jerry Yue3d67cb2022-05-19 15:33:10 +08002718 }
2719
Gilles Peskine449bd832023-01-11 14:50:10 +01002720 ret = mbedtls_ssl_tls13_compute_resumption_master_secret(ssl);
2721 if (ret != 0) {
Xiaokang Qian9f1747b2023-03-30 02:50:04 +00002722 MBEDTLS_SSL_DEBUG_RET(
2723 1, "mbedtls_ssl_tls13_compute_resumption_master_secret", ret);
Gilles Peskine449bd832023-01-11 14:50:10 +01002724 }
2725
2726 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_HANDSHAKE_WRAPUP);
2727 return 0;
Jerry Yu69dd8d42022-04-16 12:51:26 +08002728}
2729
2730/*
Jerry Yud6e253d2022-05-18 13:59:24 +08002731 * Handler for MBEDTLS_SSL_HANDSHAKE_WRAPUP
Jerry Yu69dd8d42022-04-16 12:51:26 +08002732 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002733MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002734static int ssl_tls13_handshake_wrapup(mbedtls_ssl_context *ssl)
Jerry Yu69dd8d42022-04-16 12:51:26 +08002735{
Gilles Peskine449bd832023-01-11 14:50:10 +01002736 MBEDTLS_SSL_DEBUG_MSG(2, ("handshake: done"));
Jerry Yu03ed50b2022-04-16 17:13:30 +08002737
Gilles Peskine449bd832023-01-11 14:50:10 +01002738 mbedtls_ssl_tls13_handshake_wrapup(ssl);
Jerry Yue67bef42022-07-07 07:29:42 +00002739
Pengyu Lv3643fdb2023-01-12 16:46:28 +08002740#if defined(MBEDTLS_SSL_SESSION_TICKETS) && \
2741 defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED)
Pengyu Lva1aa31b2022-12-13 13:49:59 +08002742/* TODO: Remove the check of SOME_PSK_ENABLED since SESSION_TICKETS requires
2743 * SOME_PSK_ENABLED to be enabled. Here is just to make CI happy. It is
2744 * expected to be resolved with issue#6395.
2745 */
Pengyu Lvc7af2c42022-12-01 16:33:00 +08002746 /* Sent NewSessionTicket message only when client supports PSK */
Pengyu Lv3643fdb2023-01-12 16:46:28 +08002747 if (mbedtls_ssl_tls13_some_psk_enabled(ssl)) {
Xiaokang Qian9f1747b2023-03-30 02:50:04 +00002748 mbedtls_ssl_handshake_set_state(
2749 ssl, MBEDTLS_SSL_TLS1_3_NEW_SESSION_TICKET);
Pengyu Lvc7af2c42022-12-01 16:33:00 +08002750 } else
Jerry Yufca4d572022-07-21 10:37:48 +08002751#endif
Pengyu Lv3643fdb2023-01-12 16:46:28 +08002752 {
2753 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_HANDSHAKE_OVER);
2754 }
Gilles Peskine449bd832023-01-11 14:50:10 +01002755 return 0;
Jerry Yu69dd8d42022-04-16 12:51:26 +08002756}
2757
2758/*
Jerry Yua8d3c502022-10-30 14:51:23 +08002759 * Handler for MBEDTLS_SSL_TLS1_3_NEW_SESSION_TICKET
Jerry Yue67bef42022-07-07 07:29:42 +00002760 */
2761#define SSL_NEW_SESSION_TICKET_SKIP 0
2762#define SSL_NEW_SESSION_TICKET_WRITE 1
2763MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002764static int ssl_tls13_write_new_session_ticket_coordinate(mbedtls_ssl_context *ssl)
Jerry Yue67bef42022-07-07 07:29:42 +00002765{
2766 /* Check whether the use of session tickets is enabled */
Gilles Peskine449bd832023-01-11 14:50:10 +01002767 if (ssl->conf->f_ticket_write == NULL) {
2768 MBEDTLS_SSL_DEBUG_MSG(2, ("NewSessionTicket: disabled,"
2769 " callback is not set"));
2770 return SSL_NEW_SESSION_TICKET_SKIP;
Jerry Yud0766ec2022-09-22 10:46:57 +08002771 }
Gilles Peskine449bd832023-01-11 14:50:10 +01002772 if (ssl->conf->new_session_tickets_count == 0) {
2773 MBEDTLS_SSL_DEBUG_MSG(2, ("NewSessionTicket: disabled,"
2774 " configured count is zero"));
2775 return SSL_NEW_SESSION_TICKET_SKIP;
Jerry Yud0766ec2022-09-22 10:46:57 +08002776 }
2777
Gilles Peskine449bd832023-01-11 14:50:10 +01002778 if (ssl->handshake->new_session_tickets_count == 0) {
2779 MBEDTLS_SSL_DEBUG_MSG(2, ("NewSessionTicket: all tickets have "
2780 "been sent."));
2781 return SSL_NEW_SESSION_TICKET_SKIP;
Jerry Yue67bef42022-07-07 07:29:42 +00002782 }
2783
Gilles Peskine449bd832023-01-11 14:50:10 +01002784 return SSL_NEW_SESSION_TICKET_WRITE;
Jerry Yue67bef42022-07-07 07:29:42 +00002785}
2786
2787#if defined(MBEDTLS_SSL_SESSION_TICKETS)
2788MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002789static int ssl_tls13_prepare_new_session_ticket(mbedtls_ssl_context *ssl,
2790 unsigned char *ticket_nonce,
2791 size_t ticket_nonce_size)
Jerry Yue67bef42022-07-07 07:29:42 +00002792{
2793 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
2794 mbedtls_ssl_session *session = ssl->session;
2795 mbedtls_ssl_ciphersuite_t *ciphersuite_info;
2796 psa_algorithm_t psa_hash_alg;
2797 int hash_length;
2798
Gilles Peskine449bd832023-01-11 14:50:10 +01002799 MBEDTLS_SSL_DEBUG_MSG(2, ("=> prepare NewSessionTicket msg"));
Jerry Yue67bef42022-07-07 07:29:42 +00002800
2801#if defined(MBEDTLS_HAVE_TIME)
Gilles Peskine449bd832023-01-11 14:50:10 +01002802 session->start = mbedtls_time(NULL);
Jerry Yue67bef42022-07-07 07:29:42 +00002803#endif
2804
Pengyu Lv9f926952022-11-17 15:22:33 +08002805 /* Set ticket_flags depends on the advertised psk key exchange mode */
Pengyu Lv80270b22023-01-12 11:54:04 +08002806 mbedtls_ssl_session_clear_ticket_flags(
Pengyu Lv9eacb442022-12-09 14:39:19 +08002807 session, MBEDTLS_SSL_TLS1_3_TICKET_FLAGS_MASK);
Pengyu Lve6487fe2022-12-06 09:30:29 +08002808#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED)
Pengyu Lv80270b22023-01-12 11:54:04 +08002809 mbedtls_ssl_session_set_ticket_flags(
Pengyu Lv9eacb442022-12-09 14:39:19 +08002810 session, ssl->handshake->tls13_kex_modes);
Pengyu Lve6487fe2022-12-06 09:30:29 +08002811#endif
Pengyu Lv4938a562023-01-16 11:28:49 +08002812 MBEDTLS_SSL_PRINT_TICKET_FLAGS(4, session->ticket_flags);
Pengyu Lv9f926952022-11-17 15:22:33 +08002813
Jerry Yue67bef42022-07-07 07:29:42 +00002814 /* Generate ticket_age_add */
Gilles Peskine449bd832023-01-11 14:50:10 +01002815 if ((ret = ssl->conf->f_rng(ssl->conf->p_rng,
2816 (unsigned char *) &session->ticket_age_add,
2817 sizeof(session->ticket_age_add)) != 0)) {
2818 MBEDTLS_SSL_DEBUG_RET(1, "generate_ticket_age_add", ret);
2819 return ret;
Jerry Yue67bef42022-07-07 07:29:42 +00002820 }
Gilles Peskine449bd832023-01-11 14:50:10 +01002821 MBEDTLS_SSL_DEBUG_MSG(3, ("ticket_age_add: %u",
2822 (unsigned int) session->ticket_age_add));
Jerry Yue67bef42022-07-07 07:29:42 +00002823
2824 /* Generate ticket_nonce */
Gilles Peskine449bd832023-01-11 14:50:10 +01002825 ret = ssl->conf->f_rng(ssl->conf->p_rng, ticket_nonce, ticket_nonce_size);
2826 if (ret != 0) {
2827 MBEDTLS_SSL_DEBUG_RET(1, "generate_ticket_nonce", ret);
2828 return ret;
Jerry Yue67bef42022-07-07 07:29:42 +00002829 }
Gilles Peskine449bd832023-01-11 14:50:10 +01002830 MBEDTLS_SSL_DEBUG_BUF(3, "ticket_nonce:",
2831 ticket_nonce, ticket_nonce_size);
Jerry Yue67bef42022-07-07 07:29:42 +00002832
2833 ciphersuite_info =
Gilles Peskine449bd832023-01-11 14:50:10 +01002834 (mbedtls_ssl_ciphersuite_t *) ssl->handshake->ciphersuite_info;
Dave Rodgman2eab4622023-10-05 13:30:37 +01002835 psa_hash_alg = mbedtls_md_psa_alg_from_type((mbedtls_md_type_t) ciphersuite_info->mac);
Gilles Peskine449bd832023-01-11 14:50:10 +01002836 hash_length = PSA_HASH_LENGTH(psa_hash_alg);
2837 if (hash_length == -1 ||
2838 (size_t) hash_length > sizeof(session->resumption_key)) {
2839 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Jerry Yufca4d572022-07-21 10:37:48 +08002840 }
2841
Jerry Yue67bef42022-07-07 07:29:42 +00002842 /* In this code the psk key length equals the length of the hash */
2843 session->resumption_key_len = hash_length;
2844 session->ciphersuite = ciphersuite_info->id;
2845
2846 /* Compute resumption key
2847 *
2848 * HKDF-Expand-Label( resumption_master_secret,
2849 * "resumption", ticket_nonce, Hash.length )
2850 */
2851 ret = mbedtls_ssl_tls13_hkdf_expand_label(
Gilles Peskine449bd832023-01-11 14:50:10 +01002852 psa_hash_alg,
2853 session->app_secrets.resumption_master_secret,
2854 hash_length,
2855 MBEDTLS_SSL_TLS1_3_LBL_WITH_LEN(resumption),
2856 ticket_nonce,
2857 ticket_nonce_size,
2858 session->resumption_key,
2859 hash_length);
Jerry Yue67bef42022-07-07 07:29:42 +00002860
Gilles Peskine449bd832023-01-11 14:50:10 +01002861 if (ret != 0) {
2862 MBEDTLS_SSL_DEBUG_RET(2,
2863 "Creating the ticket-resumed PSK failed",
2864 ret);
2865 return ret;
Jerry Yue67bef42022-07-07 07:29:42 +00002866 }
Gilles Peskine449bd832023-01-11 14:50:10 +01002867 MBEDTLS_SSL_DEBUG_BUF(3, "Ticket-resumed PSK",
2868 session->resumption_key,
2869 session->resumption_key_len);
Jerry Yue67bef42022-07-07 07:29:42 +00002870
Gilles Peskine449bd832023-01-11 14:50:10 +01002871 MBEDTLS_SSL_DEBUG_BUF(3, "resumption_master_secret",
2872 session->app_secrets.resumption_master_secret,
2873 hash_length);
Jerry Yue67bef42022-07-07 07:29:42 +00002874
Gilles Peskine449bd832023-01-11 14:50:10 +01002875 return 0;
Jerry Yue67bef42022-07-07 07:29:42 +00002876}
2877
2878/* This function creates a NewSessionTicket message in the following format:
2879 *
2880 * struct {
2881 * uint32 ticket_lifetime;
2882 * uint32 ticket_age_add;
2883 * opaque ticket_nonce<0..255>;
2884 * opaque ticket<1..2^16-1>;
2885 * Extension extensions<0..2^16-2>;
2886 * } NewSessionTicket;
2887 *
2888 * The ticket inside the NewSessionTicket message is an encrypted container
2889 * carrying the necessary information so that the server is later able to
2890 * re-start the communication.
2891 *
2892 * The following fields are placed inside the ticket by the
2893 * f_ticket_write() function:
2894 *
2895 * - creation time (start)
2896 * - flags (flags)
2897 * - age add (ticket_age_add)
2898 * - key (key)
2899 * - key length (key_len)
2900 * - ciphersuite (ciphersuite)
2901 */
2902MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002903static int ssl_tls13_write_new_session_ticket_body(mbedtls_ssl_context *ssl,
2904 unsigned char *buf,
2905 unsigned char *end,
2906 size_t *out_len,
2907 unsigned char *ticket_nonce,
2908 size_t ticket_nonce_size)
Jerry Yue67bef42022-07-07 07:29:42 +00002909{
2910 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
2911 unsigned char *p = buf;
2912 mbedtls_ssl_session *session = ssl->session;
2913 size_t ticket_len;
2914 uint32_t ticket_lifetime;
2915
2916 *out_len = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01002917 MBEDTLS_SSL_DEBUG_MSG(2, ("=> write NewSessionTicket msg"));
Jerry Yue67bef42022-07-07 07:29:42 +00002918
2919 /*
2920 * ticket_lifetime 4 bytes
2921 * ticket_age_add 4 bytes
2922 * ticket_nonce 1 + ticket_nonce_size bytes
2923 * ticket >=2 bytes
2924 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002925 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 4 + 4 + 1 + ticket_nonce_size + 2);
Jerry Yue67bef42022-07-07 07:29:42 +00002926
2927 /* Generate ticket and ticket_lifetime */
Gilles Peskine449bd832023-01-11 14:50:10 +01002928 ret = ssl->conf->f_ticket_write(ssl->conf->p_ticket,
2929 session,
2930 p + 9 + ticket_nonce_size + 2,
2931 end,
2932 &ticket_len,
2933 &ticket_lifetime);
2934 if (ret != 0) {
2935 MBEDTLS_SSL_DEBUG_RET(1, "write_ticket", ret);
2936 return ret;
Jerry Yue67bef42022-07-07 07:29:42 +00002937 }
2938 /* RFC 8446 4.6.1
2939 * ticket_lifetime: Indicates the lifetime in seconds as a 32-bit
2940 * unsigned integer in network byte order from the time of ticket
2941 * issuance. Servers MUST NOT use any value greater than
2942 * 604800 seconds (7 days). The value of zero indicates that the
2943 * ticket should be discarded immediately. Clients MUST NOT cache
2944 * tickets for longer than 7 days, regardless of the ticket_lifetime,
2945 * and MAY delete tickets earlier based on local policy. A server
2946 * MAY treat a ticket as valid for a shorter period of time than what
2947 * is stated in the ticket_lifetime.
2948 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002949 if (ticket_lifetime > 604800) {
Xiaokang Qian28af5012022-10-13 08:18:19 +00002950 ticket_lifetime = 604800;
Gilles Peskine449bd832023-01-11 14:50:10 +01002951 }
2952 MBEDTLS_PUT_UINT32_BE(ticket_lifetime, p, 0);
2953 MBEDTLS_SSL_DEBUG_MSG(3, ("ticket_lifetime: %u",
2954 (unsigned int) ticket_lifetime));
Jerry Yue67bef42022-07-07 07:29:42 +00002955
2956 /* Write ticket_age_add */
Gilles Peskine449bd832023-01-11 14:50:10 +01002957 MBEDTLS_PUT_UINT32_BE(session->ticket_age_add, p, 4);
2958 MBEDTLS_SSL_DEBUG_MSG(3, ("ticket_age_add: %u",
2959 (unsigned int) session->ticket_age_add));
Jerry Yue67bef42022-07-07 07:29:42 +00002960
2961 /* Write ticket_nonce */
Gilles Peskine449bd832023-01-11 14:50:10 +01002962 p[8] = (unsigned char) ticket_nonce_size;
2963 if (ticket_nonce_size > 0) {
2964 memcpy(p + 9, ticket_nonce, ticket_nonce_size);
Jerry Yue67bef42022-07-07 07:29:42 +00002965 }
2966 p += 9 + ticket_nonce_size;
2967
2968 /* Write ticket */
Gilles Peskine449bd832023-01-11 14:50:10 +01002969 MBEDTLS_PUT_UINT16_BE(ticket_len, p, 0);
Jerry Yue67bef42022-07-07 07:29:42 +00002970 p += 2;
Gilles Peskine449bd832023-01-11 14:50:10 +01002971 MBEDTLS_SSL_DEBUG_BUF(4, "ticket", p, ticket_len);
Jerry Yue67bef42022-07-07 07:29:42 +00002972 p += ticket_len;
2973
2974 /* Ticket Extensions
2975 *
2976 * Note: We currently don't have any extensions.
2977 * Set length to zero.
2978 */
Jerry Yuedab6372022-10-31 14:37:31 +08002979 ssl->handshake->sent_extensions = MBEDTLS_SSL_EXT_MASK_NONE;
2980
Gilles Peskine449bd832023-01-11 14:50:10 +01002981 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 2);
2982 MBEDTLS_PUT_UINT16_BE(0, p, 0);
Jerry Yue67bef42022-07-07 07:29:42 +00002983 p += 2;
2984
2985 *out_len = p - buf;
Gilles Peskine449bd832023-01-11 14:50:10 +01002986 MBEDTLS_SSL_DEBUG_BUF(4, "ticket", buf, *out_len);
2987 MBEDTLS_SSL_DEBUG_MSG(2, ("<= write new session ticket"));
Jerry Yue67bef42022-07-07 07:29:42 +00002988
Jerry Yu7de2ff02022-11-08 21:43:46 +08002989 MBEDTLS_SSL_PRINT_EXTS(
Gilles Peskine449bd832023-01-11 14:50:10 +01002990 3, MBEDTLS_SSL_HS_NEW_SESSION_TICKET, ssl->handshake->sent_extensions);
Jerry Yu4b8f2f72022-10-31 13:31:22 +08002991
Gilles Peskine449bd832023-01-11 14:50:10 +01002992 return 0;
Jerry Yue67bef42022-07-07 07:29:42 +00002993}
2994
2995/*
Jerry Yua8d3c502022-10-30 14:51:23 +08002996 * Handler for MBEDTLS_SSL_TLS1_3_NEW_SESSION_TICKET
Jerry Yue67bef42022-07-07 07:29:42 +00002997 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002998static int ssl_tls13_write_new_session_ticket(mbedtls_ssl_context *ssl)
Jerry Yue67bef42022-07-07 07:29:42 +00002999{
3000 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
3001
Gilles Peskine449bd832023-01-11 14:50:10 +01003002 MBEDTLS_SSL_PROC_CHK_NEG(ssl_tls13_write_new_session_ticket_coordinate(ssl));
Jerry Yue67bef42022-07-07 07:29:42 +00003003
Gilles Peskine449bd832023-01-11 14:50:10 +01003004 if (ret == SSL_NEW_SESSION_TICKET_WRITE) {
Jerry Yue67bef42022-07-07 07:29:42 +00003005 unsigned char ticket_nonce[MBEDTLS_SSL_TLS1_3_TICKET_NONCE_LENGTH];
3006 unsigned char *buf;
3007 size_t buf_len, msg_len;
3008
Gilles Peskine449bd832023-01-11 14:50:10 +01003009 MBEDTLS_SSL_PROC_CHK(ssl_tls13_prepare_new_session_ticket(
3010 ssl, ticket_nonce, sizeof(ticket_nonce)));
Jerry Yue67bef42022-07-07 07:29:42 +00003011
Xiaokang Qian9f1747b2023-03-30 02:50:04 +00003012 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_start_handshake_msg(
3013 ssl, MBEDTLS_SSL_HS_NEW_SESSION_TICKET,
3014 &buf, &buf_len));
Jerry Yue67bef42022-07-07 07:29:42 +00003015
Gilles Peskine449bd832023-01-11 14:50:10 +01003016 MBEDTLS_SSL_PROC_CHK(ssl_tls13_write_new_session_ticket_body(
3017 ssl, buf, buf + buf_len, &msg_len,
3018 ticket_nonce, sizeof(ticket_nonce)));
Jerry Yue67bef42022-07-07 07:29:42 +00003019
Gilles Peskine449bd832023-01-11 14:50:10 +01003020 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_finish_handshake_msg(
3021 ssl, buf_len, msg_len));
Jerry Yufca4d572022-07-21 10:37:48 +08003022
Jerry Yu359e65f2022-09-22 23:47:43 +08003023 /* Limit session tickets count to one when resumption connection.
3024 *
3025 * See document of mbedtls_ssl_conf_new_session_tickets.
3026 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003027 if (ssl->handshake->resume == 1) {
Jerry Yu359e65f2022-09-22 23:47:43 +08003028 ssl->handshake->new_session_tickets_count = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01003029 } else {
Jerry Yu359e65f2022-09-22 23:47:43 +08003030 ssl->handshake->new_session_tickets_count--;
Gilles Peskine449bd832023-01-11 14:50:10 +01003031 }
Jerry Yub7e3fa72022-09-22 11:07:18 +08003032
Jerry Yua8d3c502022-10-30 14:51:23 +08003033 mbedtls_ssl_handshake_set_state(
Gilles Peskine449bd832023-01-11 14:50:10 +01003034 ssl, MBEDTLS_SSL_TLS1_3_NEW_SESSION_TICKET_FLUSH);
3035 } else {
3036 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_HANDSHAKE_OVER);
Jerry Yue67bef42022-07-07 07:29:42 +00003037 }
3038
Jerry Yue67bef42022-07-07 07:29:42 +00003039cleanup:
3040
Gilles Peskine449bd832023-01-11 14:50:10 +01003041 return ret;
Jerry Yue67bef42022-07-07 07:29:42 +00003042}
3043#endif /* MBEDTLS_SSL_SESSION_TICKETS */
3044
3045/*
XiaokangQiane8ff3502022-04-22 02:34:40 +00003046 * TLS 1.3 State Machine -- server side
XiaokangQian7807f9f2022-02-15 10:04:37 +00003047 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003048int mbedtls_ssl_tls13_handshake_server_step(mbedtls_ssl_context *ssl)
Jerry Yub9930e72021-08-06 17:11:51 +08003049{
XiaokangQian08037552022-04-20 07:16:41 +00003050 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
XiaokangQian7807f9f2022-02-15 10:04:37 +00003051
Gilles Peskine449bd832023-01-11 14:50:10 +01003052 if (ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER || ssl->handshake == NULL) {
3053 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
3054 }
XiaokangQian7807f9f2022-02-15 10:04:37 +00003055
Gilles Peskine449bd832023-01-11 14:50:10 +01003056 MBEDTLS_SSL_DEBUG_MSG(2, ("tls13 server state: %s(%d)",
Dave Rodgman2eab4622023-10-05 13:30:37 +01003057 mbedtls_ssl_states_str((mbedtls_ssl_states) ssl->state),
Gilles Peskine449bd832023-01-11 14:50:10 +01003058 ssl->state));
Jerry Yu6e81b272021-09-27 11:16:17 +08003059
Gilles Peskine449bd832023-01-11 14:50:10 +01003060 switch (ssl->state) {
XiaokangQian7807f9f2022-02-15 10:04:37 +00003061 /* start state */
3062 case MBEDTLS_SSL_HELLO_REQUEST:
Gilles Peskine449bd832023-01-11 14:50:10 +01003063 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_CLIENT_HELLO);
XiaokangQian08037552022-04-20 07:16:41 +00003064 ret = 0;
XiaokangQian7807f9f2022-02-15 10:04:37 +00003065 break;
3066
XiaokangQian7807f9f2022-02-15 10:04:37 +00003067 case MBEDTLS_SSL_CLIENT_HELLO:
Gilles Peskine449bd832023-01-11 14:50:10 +01003068 ret = ssl_tls13_process_client_hello(ssl);
3069 if (ret != 0) {
3070 MBEDTLS_SSL_DEBUG_RET(1, "ssl_tls13_process_client_hello", ret);
3071 }
Jerry Yuf41553b2022-05-09 22:20:30 +08003072 break;
XiaokangQian7807f9f2022-02-15 10:04:37 +00003073
Jerry Yuf41553b2022-05-09 22:20:30 +08003074 case MBEDTLS_SSL_HELLO_RETRY_REQUEST:
Gilles Peskine449bd832023-01-11 14:50:10 +01003075 ret = ssl_tls13_write_hello_retry_request(ssl);
3076 if (ret != 0) {
3077 MBEDTLS_SSL_DEBUG_RET(1, "ssl_tls13_write_hello_retry_request", ret);
3078 return ret;
Jerry Yuf41553b2022-05-09 22:20:30 +08003079 }
XiaokangQian7807f9f2022-02-15 10:04:37 +00003080 break;
3081
Jerry Yu5b64ae92022-03-30 17:15:02 +08003082 case MBEDTLS_SSL_SERVER_HELLO:
Gilles Peskine449bd832023-01-11 14:50:10 +01003083 ret = ssl_tls13_write_server_hello(ssl);
Jerry Yu5b64ae92022-03-30 17:15:02 +08003084 break;
3085
Xiaofei Baicba64af2022-02-15 10:00:56 +00003086 case MBEDTLS_SSL_ENCRYPTED_EXTENSIONS:
Gilles Peskine449bd832023-01-11 14:50:10 +01003087 ret = ssl_tls13_write_encrypted_extensions(ssl);
3088 if (ret != 0) {
3089 MBEDTLS_SSL_DEBUG_RET(1, "ssl_tls13_write_encrypted_extensions", ret);
3090 return ret;
Xiaofei Baicba64af2022-02-15 10:00:56 +00003091 }
Jerry Yu48330562022-05-06 21:35:44 +08003092 break;
Jerry Yu6a2cd9e2022-05-05 11:14:19 +08003093
Ronald Cron928cbd32022-10-04 16:14:26 +02003094#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED)
Xiaofei Bai9ca09d42022-02-14 12:57:18 +00003095 case MBEDTLS_SSL_CERTIFICATE_REQUEST:
Gilles Peskine449bd832023-01-11 14:50:10 +01003096 ret = ssl_tls13_write_certificate_request(ssl);
Xiaofei Bai9ca09d42022-02-14 12:57:18 +00003097 break;
Xiaofei Bai9ca09d42022-02-14 12:57:18 +00003098
Jerry Yu83da34e2022-04-16 13:59:52 +08003099 case MBEDTLS_SSL_SERVER_CERTIFICATE:
Gilles Peskine449bd832023-01-11 14:50:10 +01003100 ret = ssl_tls13_write_server_certificate(ssl);
Jerry Yu83da34e2022-04-16 13:59:52 +08003101 break;
3102
3103 case MBEDTLS_SSL_CERTIFICATE_VERIFY:
Gilles Peskine449bd832023-01-11 14:50:10 +01003104 ret = ssl_tls13_write_certificate_verify(ssl);
Jerry Yu83da34e2022-04-16 13:59:52 +08003105 break;
Ronald Cron928cbd32022-10-04 16:14:26 +02003106#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED */
Jerry Yu83da34e2022-04-16 13:59:52 +08003107
Gilles Peskine449bd832023-01-11 14:50:10 +01003108 /*
3109 * Injection of dummy-CCS's for middlebox compatibility
3110 */
Gabor Mezei7b39bf12022-05-24 16:04:14 +02003111#if defined(MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE)
Gabor Mezeif7044ea2022-06-28 16:01:49 +02003112 case MBEDTLS_SSL_SERVER_CCS_AFTER_HELLO_RETRY_REQUEST:
Gilles Peskine449bd832023-01-11 14:50:10 +01003113 ret = mbedtls_ssl_tls13_write_change_cipher_spec(ssl);
3114 if (ret == 0) {
3115 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_CLIENT_HELLO);
3116 }
Gabor Mezei7b39bf12022-05-24 16:04:14 +02003117 break;
3118
3119 case MBEDTLS_SSL_SERVER_CCS_AFTER_SERVER_HELLO:
Gilles Peskine449bd832023-01-11 14:50:10 +01003120 ret = mbedtls_ssl_tls13_write_change_cipher_spec(ssl);
3121 if (ret == 0) {
3122 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_ENCRYPTED_EXTENSIONS);
3123 }
Gabor Mezei7b39bf12022-05-24 16:04:14 +02003124 break;
3125#endif /* MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE */
3126
Jerry Yu69dd8d42022-04-16 12:51:26 +08003127 case MBEDTLS_SSL_SERVER_FINISHED:
Gilles Peskine449bd832023-01-11 14:50:10 +01003128 ret = ssl_tls13_write_server_finished(ssl);
Jerry Yu69dd8d42022-04-16 12:51:26 +08003129 break;
3130
3131 case MBEDTLS_SSL_CLIENT_FINISHED:
Gilles Peskine449bd832023-01-11 14:50:10 +01003132 ret = ssl_tls13_process_client_finished(ssl);
Jerry Yu69dd8d42022-04-16 12:51:26 +08003133 break;
3134
Jerry Yu69dd8d42022-04-16 12:51:26 +08003135 case MBEDTLS_SSL_HANDSHAKE_WRAPUP:
Gilles Peskine449bd832023-01-11 14:50:10 +01003136 ret = ssl_tls13_handshake_wrapup(ssl);
Jerry Yu69dd8d42022-04-16 12:51:26 +08003137 break;
3138
Ronald Cron766c0cd2022-10-18 12:17:11 +02003139#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED)
XiaokangQian6b916b12022-04-25 07:29:34 +00003140 case MBEDTLS_SSL_CLIENT_CERTIFICATE:
Gilles Peskine449bd832023-01-11 14:50:10 +01003141 ret = mbedtls_ssl_tls13_process_certificate(ssl);
3142 if (ret == 0) {
3143 if (ssl->session_negotiate->peer_cert != NULL) {
Ronald Cron209cae92022-06-07 10:30:19 +02003144 mbedtls_ssl_handshake_set_state(
Gilles Peskine449bd832023-01-11 14:50:10 +01003145 ssl, MBEDTLS_SSL_CLIENT_CERTIFICATE_VERIFY);
3146 } else {
3147 MBEDTLS_SSL_DEBUG_MSG(2, ("skip parse certificate verify"));
Ronald Cron209cae92022-06-07 10:30:19 +02003148 mbedtls_ssl_handshake_set_state(
Gilles Peskine449bd832023-01-11 14:50:10 +01003149 ssl, MBEDTLS_SSL_CLIENT_FINISHED);
Ronald Cron19385882022-06-15 16:26:13 +02003150 }
XiaokangQian6b916b12022-04-25 07:29:34 +00003151 }
3152 break;
3153
3154 case MBEDTLS_SSL_CLIENT_CERTIFICATE_VERIFY:
Gilles Peskine449bd832023-01-11 14:50:10 +01003155 ret = mbedtls_ssl_tls13_process_certificate_verify(ssl);
3156 if (ret == 0) {
XiaokangQian6b916b12022-04-25 07:29:34 +00003157 mbedtls_ssl_handshake_set_state(
Gilles Peskine449bd832023-01-11 14:50:10 +01003158 ssl, MBEDTLS_SSL_CLIENT_FINISHED);
XiaokangQian6b916b12022-04-25 07:29:34 +00003159 }
3160 break;
Ronald Cron766c0cd2022-10-18 12:17:11 +02003161#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED */
XiaokangQian6b916b12022-04-25 07:29:34 +00003162
Jerry Yue67bef42022-07-07 07:29:42 +00003163#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Jerry Yua8d3c502022-10-30 14:51:23 +08003164 case MBEDTLS_SSL_TLS1_3_NEW_SESSION_TICKET:
Gilles Peskine449bd832023-01-11 14:50:10 +01003165 ret = ssl_tls13_write_new_session_ticket(ssl);
3166 if (ret != 0) {
3167 MBEDTLS_SSL_DEBUG_RET(1,
3168 "ssl_tls13_write_new_session_ticket ",
3169 ret);
Jerry Yue67bef42022-07-07 07:29:42 +00003170 }
3171 break;
Jerry Yua8d3c502022-10-30 14:51:23 +08003172 case MBEDTLS_SSL_TLS1_3_NEW_SESSION_TICKET_FLUSH:
Jerry Yue67bef42022-07-07 07:29:42 +00003173 /* This state is necessary to do the flush of the New Session
Jerry Yua8d3c502022-10-30 14:51:23 +08003174 * Ticket message written in MBEDTLS_SSL_TLS1_3_NEW_SESSION_TICKET
Jerry Yue67bef42022-07-07 07:29:42 +00003175 * as part of ssl_prepare_handshake_step.
3176 */
3177 ret = 0;
Jerry Yud4e75002022-08-09 13:33:50 +08003178
Gilles Peskine449bd832023-01-11 14:50:10 +01003179 if (ssl->handshake->new_session_tickets_count == 0) {
3180 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_HANDSHAKE_OVER);
3181 } else {
Xiaokang Qian9f1747b2023-03-30 02:50:04 +00003182 mbedtls_ssl_handshake_set_state(
3183 ssl, MBEDTLS_SSL_TLS1_3_NEW_SESSION_TICKET);
Gilles Peskine449bd832023-01-11 14:50:10 +01003184 }
Jerry Yue67bef42022-07-07 07:29:42 +00003185 break;
3186
3187#endif /* MBEDTLS_SSL_SESSION_TICKETS */
3188
XiaokangQian7807f9f2022-02-15 10:04:37 +00003189 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01003190 MBEDTLS_SSL_DEBUG_MSG(1, ("invalid state %d", ssl->state));
3191 return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
XiaokangQian7807f9f2022-02-15 10:04:37 +00003192 }
3193
Gilles Peskine449bd832023-01-11 14:50:10 +01003194 return ret;
Jerry Yub9930e72021-08-06 17:11:51 +08003195}
Jerry Yu3cc4c2a2021-08-06 16:29:08 +08003196
Jerry Yufb4b6472022-01-27 15:03:26 +08003197#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_PROTO_TLS1_3 */