blob: 4ed332f596cf1e9e7b5cc6ac4f1f2fc22489ff0e [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"
Jerry Yu3bf2c642022-03-30 22:02:12 +080028
Xiaofei Bai9ca09d42022-02-14 12:57:18 +000029#include "ssl_misc.h"
30#include "ssl_tls13_keys.h"
31#include "ssl_debug_helpers.h"
32
Jerry Yuf35ba382022-08-23 17:58:26 +080033
Jerry Yuc5a23a02022-08-25 10:51:44 +080034static const mbedtls_ssl_ciphersuite_t *ssl_tls13_validate_peer_ciphersuite(
Gilles Peskine449bd832023-01-11 14:50:10 +010035 mbedtls_ssl_context *ssl,
36 unsigned int cipher_suite)
Jerry Yuf35ba382022-08-23 17:58:26 +080037{
38 const mbedtls_ssl_ciphersuite_t *ciphersuite_info;
Gilles Peskine449bd832023-01-11 14:50:10 +010039 if (!mbedtls_ssl_tls13_cipher_suite_is_offered(ssl, cipher_suite)) {
40 return NULL;
Jerry Yuf35ba382022-08-23 17:58:26 +080041 }
Gilles Peskine449bd832023-01-11 14:50:10 +010042
43 ciphersuite_info = mbedtls_ssl_ciphersuite_from_id(cipher_suite);
44 if ((mbedtls_ssl_validate_ciphersuite(ssl, ciphersuite_info,
45 ssl->tls_version,
46 ssl->tls_version) != 0)) {
47 return NULL;
48 }
49 return ciphersuite_info;
Jerry Yuf35ba382022-08-23 17:58:26 +080050}
51
Ronald Cron41a443a2022-10-04 16:38:25 +020052#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED)
Jerry Yue19e3b92022-07-08 12:04:51 +000053/* From RFC 8446:
54 *
55 * enum { psk_ke(0), psk_dhe_ke(1), (255) } PskKeyExchangeMode;
56 * struct {
57 * PskKeyExchangeMode ke_modes<1..255>;
58 * } PskKeyExchangeModes;
59 */
Jerry Yu6e74a7e2022-07-20 20:49:32 +080060MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +010061static int ssl_tls13_parse_key_exchange_modes_ext(mbedtls_ssl_context *ssl,
62 const unsigned char *buf,
63 const unsigned char *end)
Jerry Yue19e3b92022-07-08 12:04:51 +000064{
Jerry Yu299e31f2022-07-13 23:06:36 +080065 const unsigned char *p = buf;
Jerry Yue19e3b92022-07-08 12:04:51 +000066 size_t ke_modes_len;
67 int ke_modes = 0;
68
Jerry Yu854dd9e2022-07-15 14:28:27 +080069 /* Read ke_modes length (1 Byte) */
Gilles Peskine449bd832023-01-11 14:50:10 +010070 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, 1);
Jerry Yu299e31f2022-07-13 23:06:36 +080071 ke_modes_len = *p++;
Jerry Yue19e3b92022-07-08 12:04:51 +000072 /* Currently, there are only two PSK modes, so even without looking
73 * at the content, something's wrong if the list has more than 2 items. */
Gilles Peskine449bd832023-01-11 14:50:10 +010074 if (ke_modes_len > 2) {
75 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER,
76 MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER);
77 return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
Jerry Yu299e31f2022-07-13 23:06:36 +080078 }
Jerry Yue19e3b92022-07-08 12:04:51 +000079
Gilles Peskine449bd832023-01-11 14:50:10 +010080 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, ke_modes_len);
Jerry Yue19e3b92022-07-08 12:04:51 +000081
Gilles Peskine449bd832023-01-11 14:50:10 +010082 while (ke_modes_len-- != 0) {
83 switch (*p++) {
84 case MBEDTLS_SSL_TLS1_3_PSK_MODE_PURE:
85 ke_modes |= MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK;
86 MBEDTLS_SSL_DEBUG_MSG(3, ("Found PSK KEX MODE"));
87 break;
88 case MBEDTLS_SSL_TLS1_3_PSK_MODE_ECDHE:
89 ke_modes |= MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK_EPHEMERAL;
90 MBEDTLS_SSL_DEBUG_MSG(3, ("Found PSK_EPHEMERAL KEX MODE"));
91 break;
92 default:
93 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER,
94 MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER);
95 return MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER;
Jerry Yue19e3b92022-07-08 12:04:51 +000096 }
97 }
98
99 ssl->handshake->tls13_kex_modes = ke_modes;
Gilles Peskine449bd832023-01-11 14:50:10 +0100100 return 0;
Jerry Yue19e3b92022-07-08 12:04:51 +0000101}
Jerry Yu1c105562022-07-10 06:32:38 +0000102
Jerry Yue9d4fc02022-08-20 19:21:15 +0800103#define SSL_TLS1_3_OFFERED_PSK_NOT_MATCH 1
104#define SSL_TLS1_3_OFFERED_PSK_MATCH 0
Jerry Yu95699e72022-08-21 19:22:23 +0800105
106#if defined(MBEDTLS_SSL_SESSION_TICKETS)
107
108MBEDTLS_CHECK_RETURN_CRITICAL
109static int ssl_tls13_offered_psks_check_identity_match_ticket(
Gilles Peskine449bd832023-01-11 14:50:10 +0100110 mbedtls_ssl_context *ssl,
111 const unsigned char *identity,
112 size_t identity_len,
113 uint32_t obfuscated_ticket_age,
114 mbedtls_ssl_session *session)
Jerry Yu95699e72022-08-21 19:22:23 +0800115{
Jerry Yufd310eb2022-09-06 09:16:35 +0800116 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yu95699e72022-08-21 19:22:23 +0800117 unsigned char *ticket_buffer;
Jerry Yu8d4bbba2022-09-13 14:15:48 +0800118#if defined(MBEDTLS_HAVE_TIME)
119 mbedtls_time_t now;
Jerry Yuacff8232022-09-14 14:35:11 +0800120 uint64_t age_in_s;
Jerry Yuf7dad3c2022-09-14 22:31:39 +0800121 int64_t age_diff_in_ms;
Jerry Yu8d4bbba2022-09-13 14:15:48 +0800122#endif
Jerry Yu95699e72022-08-21 19:22:23 +0800123
124 ((void) obfuscated_ticket_age);
125
Gilles Peskine449bd832023-01-11 14:50:10 +0100126 MBEDTLS_SSL_DEBUG_MSG(2, ("=> check_identity_match_ticket"));
Jerry Yu95699e72022-08-21 19:22:23 +0800127
Jerry Yufd310eb2022-09-06 09:16:35 +0800128 /* Ticket parser is not configured, Skip */
Gilles Peskine449bd832023-01-11 14:50:10 +0100129 if (ssl->conf->f_ticket_parse == NULL || identity_len == 0) {
130 return 0;
131 }
Jerry Yu95699e72022-08-21 19:22:23 +0800132
Jerry Yu4746b102022-09-13 11:11:48 +0800133 /* We create a copy of the encrypted ticket since the ticket parsing
134 * function is allowed to use its input buffer as an output buffer
135 * (in-place decryption). We do, however, need the original buffer for
136 * computing the PSK binder value.
Jerry Yu95699e72022-08-21 19:22:23 +0800137 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100138 ticket_buffer = mbedtls_calloc(1, identity_len);
139 if (ticket_buffer == NULL) {
140 MBEDTLS_SSL_DEBUG_MSG(1, ("buffer too small"));
141 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
Jerry Yu95699e72022-08-21 19:22:23 +0800142 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100143 memcpy(ticket_buffer, identity, identity_len);
Jerry Yu95699e72022-08-21 19:22:23 +0800144
Gilles Peskine449bd832023-01-11 14:50:10 +0100145 if ((ret = ssl->conf->f_ticket_parse(ssl->conf->p_ticket,
146 session,
147 ticket_buffer, identity_len)) != 0) {
148 if (ret == MBEDTLS_ERR_SSL_INVALID_MAC) {
149 MBEDTLS_SSL_DEBUG_MSG(3, ("ticket is not authentic"));
150 } else if (ret == MBEDTLS_ERR_SSL_SESSION_TICKET_EXPIRED) {
151 MBEDTLS_SSL_DEBUG_MSG(3, ("ticket is expired"));
152 } else {
153 MBEDTLS_SSL_DEBUG_RET(1, "ticket_parse", ret);
154 }
Jerry Yu95699e72022-08-21 19:22:23 +0800155 }
156
157 /* We delete the temporary buffer */
Gilles Peskine449bd832023-01-11 14:50:10 +0100158 mbedtls_free(ticket_buffer);
Jerry Yu95699e72022-08-21 19:22:23 +0800159
Gilles Peskine449bd832023-01-11 14:50:10 +0100160 if (ret != 0) {
Jerry Yu8d4bbba2022-09-13 14:15:48 +0800161 goto exit;
162 }
Jerry Yu95699e72022-08-21 19:22:23 +0800163
Pengyu Lv3eb49be2022-12-05 16:35:12 +0800164 /* RFC 8446 section 4.2.9
165 *
166 * Servers SHOULD NOT send NewSessionTicket with tickets that are not
167 * compatible with the advertised modes; however, if a server does so,
168 * the impact will just be that the client's attempts at resumption fail.
169 *
170 * We regard the ticket with incompatible key exchange modes as not match.
171 */
Pengyu Lv18946532023-01-12 12:28:09 +0800172 ret = MBEDTLS_ERR_ERROR_GENERIC_ERROR;
Pengyu Lv4938a562023-01-16 11:28:49 +0800173 MBEDTLS_SSL_PRINT_TICKET_FLAGS(4,
Pengyu Lv80270b22023-01-12 11:54:04 +0800174 session->ticket_flags);
Pengyu Lve2f1dbf2023-01-16 12:28:27 +0800175 if (mbedtls_ssl_tls13_check_kex_modes(
176 ssl,
177 mbedtls_ssl_session_get_ticket_flags(
178 session,
179 MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK_ALL))) {
Pengyu Lv3eb49be2022-12-05 16:35:12 +0800180 MBEDTLS_SSL_DEBUG_MSG(3, ("No suitable key exchange mode"));
181 goto exit;
182 }
183
Gilles Peskine449bd832023-01-11 14:50:10 +0100184 ret = MBEDTLS_ERR_SSL_SESSION_TICKET_EXPIRED;
185#if defined(MBEDTLS_HAVE_TIME)
186 now = mbedtls_time(NULL);
187
188 if (now < session->start) {
189 MBEDTLS_SSL_DEBUG_MSG(
190 3, ("Invalid ticket start time ( now=%" MBEDTLS_PRINTF_LONGLONG
191 ", start=%" MBEDTLS_PRINTF_LONGLONG " )",
192 (long long) now, (long long) session->start));
193 goto exit;
194 }
195
196 age_in_s = (uint64_t) (now - session->start);
Jerry Yu95699e72022-08-21 19:22:23 +0800197
Jerry Yu8d4bbba2022-09-13 14:15:48 +0800198 /* RFC 8446 section 4.6.1
199 *
200 * Servers MUST NOT use any value greater than 604800 seconds (7 days).
201 *
202 * RFC 8446 section 4.2.11.1
203 *
204 * Clients MUST NOT attempt to use tickets which have ages greater than
205 * the "ticket_lifetime" value which was provided with the ticket.
206 *
207 * For time being, the age MUST be less than 604800 seconds (7 days).
208 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100209 if (age_in_s > 604800) {
Jerry Yu8d4bbba2022-09-13 14:15:48 +0800210 MBEDTLS_SSL_DEBUG_MSG(
Gilles Peskine449bd832023-01-11 14:50:10 +0100211 3, ("Ticket age exceeds limitation ticket_age=%lu",
212 (long unsigned int) age_in_s));
Jerry Yu8d4bbba2022-09-13 14:15:48 +0800213 goto exit;
214 }
Jerry Yu95699e72022-08-21 19:22:23 +0800215
Jerry Yu8d4bbba2022-09-13 14:15:48 +0800216 /* RFC 8446 section 4.2.10
217 *
218 * For PSKs provisioned via NewSessionTicket, a server MUST validate that
219 * the ticket age for the selected PSK identity (computed by subtracting
220 * ticket_age_add from PskIdentity.obfuscated_ticket_age modulo 2^32) is
221 * within a small tolerance of the time since the ticket was issued.
Jerry Yuf7dad3c2022-09-14 22:31:39 +0800222 *
Jerry Yu0a55cc62022-09-15 16:15:06 +0800223 * NOTE: When `now == session->start`, `age_diff_in_ms` may be negative
224 * as the age units are different on the server (s) and in the
225 * client (ms) side. Add a -1000 ms tolerance window to take this
226 * into account.
Jerry Yu8d4bbba2022-09-13 14:15:48 +0800227 */
Jerry Yuf7dad3c2022-09-14 22:31:39 +0800228 age_diff_in_ms = age_in_s * 1000;
Gilles Peskine449bd832023-01-11 14:50:10 +0100229 age_diff_in_ms -= (obfuscated_ticket_age - session->ticket_age_add);
230 if (age_diff_in_ms <= -1000 ||
231 age_diff_in_ms > MBEDTLS_SSL_TLS1_3_TICKET_AGE_TOLERANCE) {
Jerry Yu8d4bbba2022-09-13 14:15:48 +0800232 MBEDTLS_SSL_DEBUG_MSG(
Gilles Peskine449bd832023-01-11 14:50:10 +0100233 3, ("Ticket age outside tolerance window ( diff=%d )",
234 (int) age_diff_in_ms));
Jerry Yu8d4bbba2022-09-13 14:15:48 +0800235 goto exit;
236 }
237
238 ret = 0;
Jerry Yu95699e72022-08-21 19:22:23 +0800239
240#endif /* MBEDTLS_HAVE_TIME */
Jerry Yu8d4bbba2022-09-13 14:15:48 +0800241
242exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100243 if (ret != 0) {
244 mbedtls_ssl_session_free(session);
245 }
Jerry Yu95699e72022-08-21 19:22:23 +0800246
Gilles Peskine449bd832023-01-11 14:50:10 +0100247 MBEDTLS_SSL_DEBUG_MSG(2, ("<= check_identity_match_ticket"));
248 return ret;
Jerry Yu95699e72022-08-21 19:22:23 +0800249}
250#endif /* MBEDTLS_SSL_SESSION_TICKETS */
251
Jerry Yu6e74a7e2022-07-20 20:49:32 +0800252MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yu1c105562022-07-10 06:32:38 +0000253static int ssl_tls13_offered_psks_check_identity_match(
Gilles Peskine449bd832023-01-11 14:50:10 +0100254 mbedtls_ssl_context *ssl,
255 const unsigned char *identity,
256 size_t identity_len,
257 uint32_t obfuscated_ticket_age,
258 int *psk_type,
259 mbedtls_ssl_session *session)
Jerry Yu1c105562022-07-10 06:32:38 +0000260{
Ronald Cron606671e2023-02-17 11:36:33 +0100261 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
262
Jerry Yu95699e72022-08-21 19:22:23 +0800263 ((void) session);
264 ((void) obfuscated_ticket_age);
Jerry Yu5725f1c2022-08-21 17:27:16 +0800265 *psk_type = MBEDTLS_SSL_TLS1_3_PSK_EXTERNAL;
Jerry Yu95699e72022-08-21 19:22:23 +0800266
Gilles Peskine449bd832023-01-11 14:50:10 +0100267 MBEDTLS_SSL_DEBUG_BUF(4, "identity", identity, identity_len);
Jerry Yu95699e72022-08-21 19:22:23 +0800268 ssl->handshake->resume = 0;
269
Jerry Yu95699e72022-08-21 19:22:23 +0800270#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Gilles Peskine449bd832023-01-11 14:50:10 +0100271 if (ssl_tls13_offered_psks_check_identity_match_ticket(
Jerry Yu8d4bbba2022-09-13 14:15:48 +0800272 ssl, identity, identity_len, obfuscated_ticket_age,
Gilles Peskine449bd832023-01-11 14:50:10 +0100273 session) == SSL_TLS1_3_OFFERED_PSK_MATCH) {
Jerry Yu95699e72022-08-21 19:22:23 +0800274 ssl->handshake->resume = 1;
275 *psk_type = MBEDTLS_SSL_TLS1_3_PSK_RESUMPTION;
Ronald Cron606671e2023-02-17 11:36:33 +0100276 ret = mbedtls_ssl_set_hs_psk(ssl,
277 session->resumption_key,
278 session->resumption_key_len);
279 if (ret != 0) {
280 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_set_hs_psk", ret);
281 return ret;
282 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100283
284 MBEDTLS_SSL_DEBUG_BUF(4, "Ticket-resumed PSK:",
285 session->resumption_key,
286 session->resumption_key_len);
287 MBEDTLS_SSL_DEBUG_MSG(4, ("ticket: obfuscated_ticket_age: %u",
288 (unsigned) obfuscated_ticket_age));
289 return SSL_TLS1_3_OFFERED_PSK_MATCH;
Jerry Yu95699e72022-08-21 19:22:23 +0800290 }
291#endif /* MBEDTLS_SSL_SESSION_TICKETS */
292
Jerry Yu1c105562022-07-10 06:32:38 +0000293 /* Check identity with external configured function */
Gilles Peskine449bd832023-01-11 14:50:10 +0100294 if (ssl->conf->f_psk != NULL) {
295 if (ssl->conf->f_psk(
296 ssl->conf->p_psk, ssl, identity, identity_len) == 0) {
297 return SSL_TLS1_3_OFFERED_PSK_MATCH;
Jerry Yu1c105562022-07-10 06:32:38 +0000298 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100299 return SSL_TLS1_3_OFFERED_PSK_NOT_MATCH;
Jerry Yu1c105562022-07-10 06:32:38 +0000300 }
301
Gilles Peskine449bd832023-01-11 14:50:10 +0100302 MBEDTLS_SSL_DEBUG_BUF(5, "identity", identity, identity_len);
Jerry Yu1c105562022-07-10 06:32:38 +0000303 /* Check identity with pre-configured psk */
Gilles Peskine449bd832023-01-11 14:50:10 +0100304 if (ssl->conf->psk_identity != NULL &&
Jerry Yu2f0abc92022-07-22 19:34:48 +0800305 identity_len == ssl->conf->psk_identity_len &&
Gilles Peskine449bd832023-01-11 14:50:10 +0100306 mbedtls_ct_memcmp(ssl->conf->psk_identity,
307 identity, identity_len) == 0) {
Ronald Cron606671e2023-02-17 11:36:33 +0100308 ret = mbedtls_ssl_set_hs_psk(ssl, ssl->conf->psk, ssl->conf->psk_len);
309 if (ret != 0) {
310 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_set_hs_psk", ret);
311 return ret;
312 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100313 return SSL_TLS1_3_OFFERED_PSK_MATCH;
Jerry Yu1c105562022-07-10 06:32:38 +0000314 }
315
Gilles Peskine449bd832023-01-11 14:50:10 +0100316 return SSL_TLS1_3_OFFERED_PSK_NOT_MATCH;
Jerry Yu1c105562022-07-10 06:32:38 +0000317}
318
Jerry Yu6e74a7e2022-07-20 20:49:32 +0800319MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100320static int ssl_tls13_offered_psks_check_binder_match(mbedtls_ssl_context *ssl,
321 const unsigned char *binder,
322 size_t binder_len,
323 int psk_type,
324 psa_algorithm_t psk_hash_alg)
Jerry Yu1c105562022-07-10 06:32:38 +0000325{
326 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yu96a2e362022-07-21 15:11:34 +0800327
Jerry Yudaf375a2022-07-20 21:31:43 +0800328 unsigned char transcript[PSA_HASH_MAX_SIZE];
Jerry Yu1c105562022-07-10 06:32:38 +0000329 size_t transcript_len;
Jerry Yu2f0abc92022-07-22 19:34:48 +0800330 unsigned char *psk;
Jerry Yu96a2e362022-07-21 15:11:34 +0800331 size_t psk_len;
Jerry Yudaf375a2022-07-20 21:31:43 +0800332 unsigned char server_computed_binder[PSA_HASH_MAX_SIZE];
Jerry Yu1c105562022-07-10 06:32:38 +0000333
Jerry Yu1c105562022-07-10 06:32:38 +0000334 /* Get current state of handshake transcript. */
Jerry Yu29d9faa2022-08-23 17:52:45 +0800335 ret = mbedtls_ssl_get_handshake_transcript(
Gilles Peskine449bd832023-01-11 14:50:10 +0100336 ssl, mbedtls_hash_info_md_from_psa(psk_hash_alg),
337 transcript, sizeof(transcript), &transcript_len);
338 if (ret != 0) {
339 return ret;
340 }
Jerry Yu1c105562022-07-10 06:32:38 +0000341
Gilles Peskine449bd832023-01-11 14:50:10 +0100342 ret = mbedtls_ssl_tls13_export_handshake_psk(ssl, &psk, &psk_len);
343 if (ret != 0) {
344 return ret;
345 }
Jerry Yu96a2e362022-07-21 15:11:34 +0800346
Gilles Peskine449bd832023-01-11 14:50:10 +0100347 ret = mbedtls_ssl_tls13_create_psk_binder(ssl, psk_hash_alg,
348 psk, psk_len, psk_type,
349 transcript,
350 server_computed_binder);
Jerry Yu96a2e362022-07-21 15:11:34 +0800351#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +0100352 mbedtls_free((void *) psk);
Jerry Yu96a2e362022-07-21 15:11:34 +0800353#endif
Gilles Peskine449bd832023-01-11 14:50:10 +0100354 if (ret != 0) {
355 MBEDTLS_SSL_DEBUG_MSG(1, ("PSK binder calculation failed."));
356 return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
Jerry Yu1c105562022-07-10 06:32:38 +0000357 }
358
Gilles Peskine449bd832023-01-11 14:50:10 +0100359 MBEDTLS_SSL_DEBUG_BUF(3, "psk binder ( computed ): ",
360 server_computed_binder, transcript_len);
361 MBEDTLS_SSL_DEBUG_BUF(3, "psk binder ( received ): ", binder, binder_len);
Jerry Yu1c105562022-07-10 06:32:38 +0000362
Gilles Peskine449bd832023-01-11 14:50:10 +0100363 if (mbedtls_ct_memcmp(server_computed_binder, binder, binder_len) == 0) {
364 return SSL_TLS1_3_OFFERED_PSK_MATCH;
Jerry Yu1c105562022-07-10 06:32:38 +0000365 }
366
Gilles Peskine449bd832023-01-11 14:50:10 +0100367 mbedtls_platform_zeroize(server_computed_binder,
368 sizeof(server_computed_binder));
369 return SSL_TLS1_3_OFFERED_PSK_NOT_MATCH;
Jerry Yu1c105562022-07-10 06:32:38 +0000370}
Jerry Yu96a2e362022-07-21 15:11:34 +0800371
Jerry Yu5725f1c2022-08-21 17:27:16 +0800372MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yuf35ba382022-08-23 17:58:26 +0800373static int ssl_tls13_select_ciphersuite_for_psk(
Gilles Peskine449bd832023-01-11 14:50:10 +0100374 mbedtls_ssl_context *ssl,
375 const unsigned char *cipher_suites,
376 const unsigned char *cipher_suites_end,
377 uint16_t *selected_ciphersuite,
378 const mbedtls_ssl_ciphersuite_t **selected_ciphersuite_info)
Jerry Yu5725f1c2022-08-21 17:27:16 +0800379{
Jerry Yuf35ba382022-08-23 17:58:26 +0800380 psa_algorithm_t psk_hash_alg = PSA_ALG_SHA_256;
Jerry Yu5725f1c2022-08-21 17:27:16 +0800381
Jerry Yu0baf9072022-08-25 11:21:04 +0800382 *selected_ciphersuite = 0;
383 *selected_ciphersuite_info = NULL;
384
Jerry Yuf35ba382022-08-23 17:58:26 +0800385 /* RFC 8446, page 55.
386 *
387 * For externally established PSKs, the Hash algorithm MUST be set when the
388 * PSK is established or default to SHA-256 if no such algorithm is defined.
389 *
390 */
Jerry Yu5725f1c2022-08-21 17:27:16 +0800391
Jerry Yu5725f1c2022-08-21 17:27:16 +0800392 /*
393 * Search for a matching ciphersuite
394 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100395 for (const unsigned char *p = cipher_suites;
396 p < cipher_suites_end; p += 2) {
Jerry Yu5725f1c2022-08-21 17:27:16 +0800397 uint16_t cipher_suite;
Jerry Yuf35ba382022-08-23 17:58:26 +0800398 const mbedtls_ssl_ciphersuite_t *ciphersuite_info;
Jerry Yu5725f1c2022-08-21 17:27:16 +0800399
Gilles Peskine449bd832023-01-11 14:50:10 +0100400 cipher_suite = MBEDTLS_GET_UINT16_BE(p, 0);
401 ciphersuite_info = ssl_tls13_validate_peer_ciphersuite(ssl,
402 cipher_suite);
403 if (ciphersuite_info == NULL) {
Jerry Yu5725f1c2022-08-21 17:27:16 +0800404 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +0100405 }
Jerry Yu5725f1c2022-08-21 17:27:16 +0800406
Jerry Yu5725f1c2022-08-21 17:27:16 +0800407 /* MAC of selected ciphersuite MUST be same with PSK binder if exist.
408 * Otherwise, client should reject.
409 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100410 if (psk_hash_alg == mbedtls_psa_translate_md(ciphersuite_info->mac)) {
Jerry Yuf35ba382022-08-23 17:58:26 +0800411 *selected_ciphersuite = cipher_suite;
412 *selected_ciphersuite_info = ciphersuite_info;
Gilles Peskine449bd832023-01-11 14:50:10 +0100413 return 0;
Jerry Yuf35ba382022-08-23 17:58:26 +0800414 }
415 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100416 MBEDTLS_SSL_DEBUG_MSG(2, ("No matched ciphersuite"));
417 return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
Jerry Yuf35ba382022-08-23 17:58:26 +0800418}
Jerry Yu5725f1c2022-08-21 17:27:16 +0800419
Jerry Yu82534862022-08-30 10:42:33 +0800420#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Jerry Yuf35ba382022-08-23 17:58:26 +0800421MBEDTLS_CHECK_RETURN_CRITICAL
422static int ssl_tls13_select_ciphersuite_for_resumption(
Gilles Peskine449bd832023-01-11 14:50:10 +0100423 mbedtls_ssl_context *ssl,
424 const unsigned char *cipher_suites,
425 const unsigned char *cipher_suites_end,
426 mbedtls_ssl_session *session,
427 uint16_t *selected_ciphersuite,
428 const mbedtls_ssl_ciphersuite_t **selected_ciphersuite_info)
Jerry Yuf35ba382022-08-23 17:58:26 +0800429{
Jerry Yu82534862022-08-30 10:42:33 +0800430
Jerry Yuf35ba382022-08-23 17:58:26 +0800431 *selected_ciphersuite = 0;
432 *selected_ciphersuite_info = NULL;
Gilles Peskine449bd832023-01-11 14:50:10 +0100433 for (const unsigned char *p = cipher_suites; p < cipher_suites_end; p += 2) {
434 uint16_t cipher_suite = MBEDTLS_GET_UINT16_BE(p, 0);
Jerry Yu82534862022-08-30 10:42:33 +0800435 const mbedtls_ssl_ciphersuite_t *ciphersuite_info;
436
Gilles Peskine449bd832023-01-11 14:50:10 +0100437 if (cipher_suite != session->ciphersuite) {
Jerry Yu82534862022-08-30 10:42:33 +0800438 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +0100439 }
Jerry Yu82534862022-08-30 10:42:33 +0800440
Gilles Peskine449bd832023-01-11 14:50:10 +0100441 ciphersuite_info = ssl_tls13_validate_peer_ciphersuite(ssl,
442 cipher_suite);
443 if (ciphersuite_info == NULL) {
Jerry Yu82534862022-08-30 10:42:33 +0800444 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +0100445 }
Jerry Yu82534862022-08-30 10:42:33 +0800446
Jerry Yu4746b102022-09-13 11:11:48 +0800447 *selected_ciphersuite = cipher_suite;
Jerry Yu82534862022-08-30 10:42:33 +0800448 *selected_ciphersuite_info = ciphersuite_info;
449
Gilles Peskine449bd832023-01-11 14:50:10 +0100450 return 0;
Jerry Yu82534862022-08-30 10:42:33 +0800451 }
452
Gilles Peskine449bd832023-01-11 14:50:10 +0100453 return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
Jerry Yu5725f1c2022-08-21 17:27:16 +0800454}
Jerry Yuf35ba382022-08-23 17:58:26 +0800455
Jerry Yu82534862022-08-30 10:42:33 +0800456MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100457static int ssl_tls13_session_copy_ticket(mbedtls_ssl_session *dst,
458 const mbedtls_ssl_session *src)
Jerry Yu82534862022-08-30 10:42:33 +0800459{
Jerry Yu82534862022-08-30 10:42:33 +0800460 dst->ticket_age_add = src->ticket_age_add;
461 dst->ticket_flags = src->ticket_flags;
462 dst->resumption_key_len = src->resumption_key_len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100463 if (src->resumption_key_len == 0) {
464 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
465 }
466 memcpy(dst->resumption_key, src->resumption_key, src->resumption_key_len);
Jerry Yu4746b102022-09-13 11:11:48 +0800467
Gilles Peskine449bd832023-01-11 14:50:10 +0100468 return 0;
Jerry Yu82534862022-08-30 10:42:33 +0800469}
470#endif /* MBEDTLS_SSL_SESSION_TICKETS */
471
Jerry Yu1c105562022-07-10 06:32:38 +0000472/* Parser for pre_shared_key extension in client hello
473 * struct {
474 * opaque identity<1..2^16-1>;
475 * uint32 obfuscated_ticket_age;
476 * } PskIdentity;
477 *
478 * opaque PskBinderEntry<32..255>;
479 *
480 * struct {
481 * PskIdentity identities<7..2^16-1>;
482 * PskBinderEntry binders<33..2^16-1>;
483 * } OfferedPsks;
484 *
485 * struct {
486 * select (Handshake.msg_type) {
487 * case client_hello: OfferedPsks;
488 * ....
489 * };
490 * } PreSharedKeyExtension;
491 */
Jerry Yu6e74a7e2022-07-20 20:49:32 +0800492MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100493static int ssl_tls13_parse_pre_shared_key_ext(mbedtls_ssl_context *ssl,
494 const unsigned char *pre_shared_key_ext,
495 const unsigned char *pre_shared_key_ext_end,
496 const unsigned char *ciphersuites,
497 const unsigned char *ciphersuites_end)
Jerry Yu1c105562022-07-10 06:32:38 +0000498{
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +0100499 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yu29d9faa2022-08-23 17:52:45 +0800500 const unsigned char *identities = pre_shared_key_ext;
Jerry Yu568ec252022-07-22 21:27:34 +0800501 const unsigned char *p_identity_len;
502 size_t identities_len;
Jerry Yu1c105562022-07-10 06:32:38 +0000503 const unsigned char *identities_end;
Jerry Yu568ec252022-07-22 21:27:34 +0800504 const unsigned char *binders;
505 const unsigned char *p_binder_len;
506 size_t binders_len;
Jerry Yu1c105562022-07-10 06:32:38 +0000507 const unsigned char *binders_end;
Jerry Yu96a2e362022-07-21 15:11:34 +0800508 int matched_identity = -1;
509 int identity_id = -1;
Jerry Yu1c105562022-07-10 06:32:38 +0000510
Gilles Peskine449bd832023-01-11 14:50:10 +0100511 MBEDTLS_SSL_DEBUG_BUF(3, "pre_shared_key extension",
512 pre_shared_key_ext,
513 pre_shared_key_ext_end - pre_shared_key_ext);
Jerry Yu1c105562022-07-10 06:32:38 +0000514
Jerry Yu96a2e362022-07-21 15:11:34 +0800515 /* identities_len 2 bytes
516 * identities_data >= 7 bytes
517 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100518 MBEDTLS_SSL_CHK_BUF_READ_PTR(identities, pre_shared_key_ext_end, 7 + 2);
519 identities_len = MBEDTLS_GET_UINT16_BE(identities, 0);
Jerry Yu568ec252022-07-22 21:27:34 +0800520 p_identity_len = identities + 2;
Gilles Peskine449bd832023-01-11 14:50:10 +0100521 MBEDTLS_SSL_CHK_BUF_READ_PTR(p_identity_len, pre_shared_key_ext_end,
522 identities_len);
Jerry Yu568ec252022-07-22 21:27:34 +0800523 identities_end = p_identity_len + identities_len;
Jerry Yu1c105562022-07-10 06:32:38 +0000524
Jerry Yu96a2e362022-07-21 15:11:34 +0800525 /* binders_len 2 bytes
526 * binders >= 33 bytes
527 */
Jerry Yu568ec252022-07-22 21:27:34 +0800528 binders = identities_end;
Gilles Peskine449bd832023-01-11 14:50:10 +0100529 MBEDTLS_SSL_CHK_BUF_READ_PTR(binders, pre_shared_key_ext_end, 33 + 2);
530 binders_len = MBEDTLS_GET_UINT16_BE(binders, 0);
Jerry Yu568ec252022-07-22 21:27:34 +0800531 p_binder_len = binders + 2;
Gilles Peskine449bd832023-01-11 14:50:10 +0100532 MBEDTLS_SSL_CHK_BUF_READ_PTR(p_binder_len, pre_shared_key_ext_end, binders_len);
Jerry Yu568ec252022-07-22 21:27:34 +0800533 binders_end = p_binder_len + binders_len;
Jerry Yu1c105562022-07-10 06:32:38 +0000534
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +0100535 ret = ssl->handshake->update_checksum(ssl, pre_shared_key_ext,
536 identities_end - pre_shared_key_ext);
537 if (0 != ret) {
538 MBEDTLS_SSL_DEBUG_RET(1, ("update_checksum"), ret);
539 return ret;
540 }
Jerry Yu96a2e362022-07-21 15:11:34 +0800541
Gilles Peskine449bd832023-01-11 14:50:10 +0100542 while (p_identity_len < identities_end && p_binder_len < binders_end) {
Jerry Yu1c105562022-07-10 06:32:38 +0000543 const unsigned char *identity;
Jerry Yu568ec252022-07-22 21:27:34 +0800544 size_t identity_len;
Jerry Yu82534862022-08-30 10:42:33 +0800545 uint32_t obfuscated_ticket_age;
Jerry Yu96a2e362022-07-21 15:11:34 +0800546 const unsigned char *binder;
Jerry Yu568ec252022-07-22 21:27:34 +0800547 size_t binder_len;
Jerry Yu5725f1c2022-08-21 17:27:16 +0800548 int psk_type;
549 uint16_t cipher_suite;
Jerry Yu29d9faa2022-08-23 17:52:45 +0800550 const mbedtls_ssl_ciphersuite_t *ciphersuite_info;
Jerry Yu82534862022-08-30 10:42:33 +0800551#if defined(MBEDTLS_SSL_SESSION_TICKETS)
552 mbedtls_ssl_session session;
Gilles Peskine449bd832023-01-11 14:50:10 +0100553 mbedtls_ssl_session_init(&session);
Jerry Yu82534862022-08-30 10:42:33 +0800554#endif
Jerry Yubb852022022-07-20 21:10:44 +0800555
Gilles Peskine449bd832023-01-11 14:50:10 +0100556 MBEDTLS_SSL_CHK_BUF_READ_PTR(p_identity_len, identities_end, 2 + 1 + 4);
557 identity_len = MBEDTLS_GET_UINT16_BE(p_identity_len, 0);
Jerry Yu568ec252022-07-22 21:27:34 +0800558 identity = p_identity_len + 2;
Gilles Peskine449bd832023-01-11 14:50:10 +0100559 MBEDTLS_SSL_CHK_BUF_READ_PTR(identity, identities_end, identity_len + 4);
560 obfuscated_ticket_age = MBEDTLS_GET_UINT32_BE(identity, identity_len);
Jerry Yu568ec252022-07-22 21:27:34 +0800561 p_identity_len += identity_len + 6;
Jerry Yu1c105562022-07-10 06:32:38 +0000562
Gilles Peskine449bd832023-01-11 14:50:10 +0100563 MBEDTLS_SSL_CHK_BUF_READ_PTR(p_binder_len, binders_end, 1 + 32);
Jerry Yu568ec252022-07-22 21:27:34 +0800564 binder_len = *p_binder_len;
565 binder = p_binder_len + 1;
Gilles Peskine449bd832023-01-11 14:50:10 +0100566 MBEDTLS_SSL_CHK_BUF_READ_PTR(binder, binders_end, binder_len);
Jerry Yu568ec252022-07-22 21:27:34 +0800567 p_binder_len += binder_len + 1;
Jerry Yu96a2e362022-07-21 15:11:34 +0800568
Jerry Yu96a2e362022-07-21 15:11:34 +0800569 identity_id++;
Gilles Peskine449bd832023-01-11 14:50:10 +0100570 if (matched_identity != -1) {
Jerry Yu1c105562022-07-10 06:32:38 +0000571 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +0100572 }
Jerry Yu1c105562022-07-10 06:32:38 +0000573
Jerry Yu96a2e362022-07-21 15:11:34 +0800574 ret = ssl_tls13_offered_psks_check_identity_match(
Gilles Peskine449bd832023-01-11 14:50:10 +0100575 ssl, identity, identity_len, obfuscated_ticket_age,
576 &psk_type, &session);
577 if (ret != SSL_TLS1_3_OFFERED_PSK_MATCH) {
Jerry Yu96a2e362022-07-21 15:11:34 +0800578 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +0100579 }
Jerry Yu96a2e362022-07-21 15:11:34 +0800580
Gilles Peskine449bd832023-01-11 14:50:10 +0100581 MBEDTLS_SSL_DEBUG_MSG(4, ("found matched identity"));
582 switch (psk_type) {
Jerry Yu0baf9072022-08-25 11:21:04 +0800583 case MBEDTLS_SSL_TLS1_3_PSK_EXTERNAL:
584 ret = ssl_tls13_select_ciphersuite_for_psk(
Gilles Peskine449bd832023-01-11 14:50:10 +0100585 ssl, ciphersuites, ciphersuites_end,
586 &cipher_suite, &ciphersuite_info);
Jerry Yu0baf9072022-08-25 11:21:04 +0800587 break;
588 case MBEDTLS_SSL_TLS1_3_PSK_RESUMPTION:
Jerry Yu82534862022-08-30 10:42:33 +0800589#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Jerry Yu0baf9072022-08-25 11:21:04 +0800590 ret = ssl_tls13_select_ciphersuite_for_resumption(
Gilles Peskine449bd832023-01-11 14:50:10 +0100591 ssl, ciphersuites, ciphersuites_end, &session,
592 &cipher_suite, &ciphersuite_info);
593 if (ret != 0) {
594 mbedtls_ssl_session_free(&session);
595 }
Jerry Yu82534862022-08-30 10:42:33 +0800596#else
597 ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
598#endif
Jerry Yu0baf9072022-08-25 11:21:04 +0800599 break;
600 default:
Gilles Peskine449bd832023-01-11 14:50:10 +0100601 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Jerry Yu0baf9072022-08-25 11:21:04 +0800602 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100603 if (ret != 0) {
Jerry Yuf35ba382022-08-23 17:58:26 +0800604 /* See below, no cipher_suite available, abort handshake */
605 MBEDTLS_SSL_PEND_FATAL_ALERT(
606 MBEDTLS_SSL_ALERT_MSG_DECRYPT_ERROR,
Gilles Peskine449bd832023-01-11 14:50:10 +0100607 MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE);
Jerry Yuf35ba382022-08-23 17:58:26 +0800608 MBEDTLS_SSL_DEBUG_RET(
Gilles Peskine449bd832023-01-11 14:50:10 +0100609 2, "ssl_tls13_select_ciphersuite", ret);
610 return ret;
Jerry Yu5725f1c2022-08-21 17:27:16 +0800611 }
612
Jerry Yu96a2e362022-07-21 15:11:34 +0800613 ret = ssl_tls13_offered_psks_check_binder_match(
Gilles Peskine449bd832023-01-11 14:50:10 +0100614 ssl, binder, binder_len, psk_type,
615 mbedtls_psa_translate_md(ciphersuite_info->mac));
616 if (ret != SSL_TLS1_3_OFFERED_PSK_MATCH) {
Jerry Yuc5a23a02022-08-25 10:51:44 +0800617 /* For security reasons, the handshake should be aborted when we
618 * fail to validate a binder value. See RFC 8446 section 4.2.11.2
619 * and appendix E.6. */
Jerry Yu82534862022-08-30 10:42:33 +0800620#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Gilles Peskine449bd832023-01-11 14:50:10 +0100621 mbedtls_ssl_session_free(&session);
Jerry Yu82534862022-08-30 10:42:33 +0800622#endif
Gilles Peskine449bd832023-01-11 14:50:10 +0100623 MBEDTLS_SSL_DEBUG_MSG(3, ("Invalid binder."));
624 MBEDTLS_SSL_DEBUG_RET(1,
625 "ssl_tls13_offered_psks_check_binder_match", ret);
Jerry Yu96a2e362022-07-21 15:11:34 +0800626 MBEDTLS_SSL_PEND_FATAL_ALERT(
627 MBEDTLS_SSL_ALERT_MSG_DECRYPT_ERROR,
Gilles Peskine449bd832023-01-11 14:50:10 +0100628 MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE);
629 return ret;
Jerry Yu96a2e362022-07-21 15:11:34 +0800630 }
Jerry Yu96a2e362022-07-21 15:11:34 +0800631
632 matched_identity = identity_id;
Jerry Yu5725f1c2022-08-21 17:27:16 +0800633
634 /* Update handshake parameters */
Jerry Yue5834fd2022-08-29 20:16:09 +0800635 ssl->handshake->ciphersuite_info = ciphersuite_info;
Jerry Yu4746b102022-09-13 11:11:48 +0800636 ssl->session_negotiate->ciphersuite = cipher_suite;
Gilles Peskine449bd832023-01-11 14:50:10 +0100637 MBEDTLS_SSL_DEBUG_MSG(2, ("overwrite ciphersuite: %04x - %s",
638 cipher_suite, ciphersuite_info->name));
Jerry Yu82534862022-08-30 10:42:33 +0800639#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Gilles Peskine449bd832023-01-11 14:50:10 +0100640 if (psk_type == MBEDTLS_SSL_TLS1_3_PSK_RESUMPTION) {
641 ret = ssl_tls13_session_copy_ticket(ssl->session_negotiate,
642 &session);
643 mbedtls_ssl_session_free(&session);
644 if (ret != 0) {
645 return ret;
646 }
Jerry Yu82534862022-08-30 10:42:33 +0800647 }
648#endif /* MBEDTLS_SSL_SESSION_TICKETS */
Jerry Yu1c105562022-07-10 06:32:38 +0000649 }
650
Gilles Peskine449bd832023-01-11 14:50:10 +0100651 if (p_identity_len != identities_end || p_binder_len != binders_end) {
652 MBEDTLS_SSL_DEBUG_MSG(3, ("pre_shared_key extension decode error"));
653 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR,
654 MBEDTLS_ERR_SSL_DECODE_ERROR);
655 return MBEDTLS_ERR_SSL_DECODE_ERROR;
Jerry Yu1c105562022-07-10 06:32:38 +0000656 }
657
Jerry Yu6f1db3f2022-07-22 23:05:59 +0800658 /* Update the handshake transcript with the binder list. */
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +0100659 ret = ssl->handshake->update_checksum(ssl,
660 identities_end,
661 (size_t) (binders_end - identities_end));
662 if (0 != ret) {
663 MBEDTLS_SSL_DEBUG_RET(1, ("update_checksum"), ret);
664 return ret;
665 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100666 if (matched_identity == -1) {
667 MBEDTLS_SSL_DEBUG_MSG(3, ("No matched PSK or ticket."));
668 return MBEDTLS_ERR_SSL_UNKNOWN_IDENTITY;
Jerry Yu1c105562022-07-10 06:32:38 +0000669 }
670
Gilles Peskine449bd832023-01-11 14:50:10 +0100671 ssl->handshake->selected_identity = (uint16_t) matched_identity;
672 MBEDTLS_SSL_DEBUG_MSG(3, ("Pre shared key found"));
Jerry Yu1c105562022-07-10 06:32:38 +0000673
Gilles Peskine449bd832023-01-11 14:50:10 +0100674 return 0;
Jerry Yu1c105562022-07-10 06:32:38 +0000675}
Jerry Yu032b15ce2022-07-11 06:10:03 +0000676
677/*
678 * struct {
679 * select ( Handshake.msg_type ) {
680 * ....
681 * case server_hello:
682 * uint16 selected_identity;
683 * }
684 * } PreSharedKeyExtension;
685 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100686static int ssl_tls13_write_server_pre_shared_key_ext(mbedtls_ssl_context *ssl,
687 unsigned char *buf,
688 unsigned char *end,
689 size_t *olen)
Jerry Yu032b15ce2022-07-11 06:10:03 +0000690{
Gilles Peskine449bd832023-01-11 14:50:10 +0100691 unsigned char *p = (unsigned char *) buf;
Jerry Yu032b15ce2022-07-11 06:10:03 +0000692
693 *olen = 0;
694
David Horstmann21b89762022-10-06 18:34:28 +0100695 int not_using_psk = 0;
Jerry Yu032b15ce2022-07-11 06:10:03 +0000696#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +0100697 not_using_psk = (mbedtls_svc_key_id_is_null(ssl->handshake->psk_opaque));
Jerry Yu032b15ce2022-07-11 06:10:03 +0000698#else
Gilles Peskine449bd832023-01-11 14:50:10 +0100699 not_using_psk = (ssl->handshake->psk == NULL);
Jerry Yu032b15ce2022-07-11 06:10:03 +0000700#endif
Gilles Peskine449bd832023-01-11 14:50:10 +0100701 if (not_using_psk) {
Jerry Yu032b15ce2022-07-11 06:10:03 +0000702 /* We shouldn't have called this extension writer unless we've
703 * chosen to use a PSK. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100704 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Jerry Yu032b15ce2022-07-11 06:10:03 +0000705 }
706
Gilles Peskine449bd832023-01-11 14:50:10 +0100707 MBEDTLS_SSL_DEBUG_MSG(3, ("server hello, adding pre_shared_key extension"));
708 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 6);
Jerry Yu032b15ce2022-07-11 06:10:03 +0000709
Gilles Peskine449bd832023-01-11 14:50:10 +0100710 MBEDTLS_PUT_UINT16_BE(MBEDTLS_TLS_EXT_PRE_SHARED_KEY, p, 0);
711 MBEDTLS_PUT_UINT16_BE(2, p, 2);
Jerry Yu032b15ce2022-07-11 06:10:03 +0000712
Gilles Peskine449bd832023-01-11 14:50:10 +0100713 MBEDTLS_PUT_UINT16_BE(ssl->handshake->selected_identity, p, 4);
Jerry Yu032b15ce2022-07-11 06:10:03 +0000714
715 *olen = 6;
716
Gilles Peskine449bd832023-01-11 14:50:10 +0100717 MBEDTLS_SSL_DEBUG_MSG(4, ("sent selected_identity: %u",
718 ssl->handshake->selected_identity));
Jerry Yu032b15ce2022-07-11 06:10:03 +0000719
Gilles Peskine449bd832023-01-11 14:50:10 +0100720 mbedtls_ssl_tls13_set_hs_sent_ext_mask(ssl, MBEDTLS_TLS_EXT_PRE_SHARED_KEY);
Jerry Yub95dd362022-11-08 21:19:34 +0800721
Gilles Peskine449bd832023-01-11 14:50:10 +0100722 return 0;
Jerry Yu032b15ce2022-07-11 06:10:03 +0000723}
724
Ronald Cron41a443a2022-10-04 16:38:25 +0200725#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED */
Jerry Yue19e3b92022-07-08 12:04:51 +0000726
XiaokangQian7807f9f2022-02-15 10:04:37 +0000727/* From RFC 8446:
728 * struct {
XiaokangQiancfd925f2022-04-14 07:10:37 +0000729 * ProtocolVersion versions<2..254>;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000730 * } SupportedVersions;
731 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200732MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100733static int ssl_tls13_parse_supported_versions_ext(mbedtls_ssl_context *ssl,
734 const unsigned char *buf,
735 const unsigned char *end)
XiaokangQian7807f9f2022-02-15 10:04:37 +0000736{
XiaokangQian7807f9f2022-02-15 10:04:37 +0000737 const unsigned char *p = buf;
XiaokangQian8f9dfe42022-04-15 02:52:39 +0000738 size_t versions_len;
XiaokangQian4080a7f2022-04-11 09:55:18 +0000739 const unsigned char *versions_end;
XiaokangQian0a1b54e2022-04-21 03:01:38 +0000740 uint16_t tls_version;
Ronald Cron5af4c7f2023-03-07 20:46:59 +0100741 int found_supported_version = 0;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000742
Gilles Peskine449bd832023-01-11 14:50:10 +0100743 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, 1);
XiaokangQian4080a7f2022-04-11 09:55:18 +0000744 versions_len = p[0];
XiaokangQian7807f9f2022-02-15 10:04:37 +0000745 p += 1;
746
Gilles Peskine449bd832023-01-11 14:50:10 +0100747 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, versions_len);
XiaokangQian4080a7f2022-04-11 09:55:18 +0000748 versions_end = p + versions_len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100749 while (p < versions_end) {
750 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, versions_end, 2);
751 tls_version = mbedtls_ssl_read_version(p, ssl->conf->transport);
XiaokangQianb67384d2022-04-19 00:02:38 +0000752 p += 2;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000753
Ronald Cron5af4c7f2023-03-07 20:46:59 +0100754 if ((MBEDTLS_SSL_VERSION_TLS1_2 == tls_version) &&
755 mbedtls_ssl_conf_is_tls12_enabled(ssl->conf)) {
756 found_supported_version = 1;
757 break;
758 }
759
760 if (MBEDTLS_SSL_VERSION_TLS1_3 == tls_version) {
761 found_supported_version = 1;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000762 break;
763 }
XiaokangQian7807f9f2022-02-15 10:04:37 +0000764 }
765
Ronald Cron5af4c7f2023-03-07 20:46:59 +0100766 if (!found_supported_version) {
767 MBEDTLS_SSL_DEBUG_MSG(1, ("No supported version found."));
XiaokangQian7807f9f2022-02-15 10:04:37 +0000768
Gilles Peskine449bd832023-01-11 14:50:10 +0100769 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_PROTOCOL_VERSION,
770 MBEDTLS_ERR_SSL_BAD_PROTOCOL_VERSION);
771 return MBEDTLS_ERR_SSL_BAD_PROTOCOL_VERSION;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000772 }
773
Ronald Cron5af4c7f2023-03-07 20:46:59 +0100774 MBEDTLS_SSL_DEBUG_MSG(1, ("Negotiated version: [%04x]",
Gilles Peskine449bd832023-01-11 14:50:10 +0100775 (unsigned int) tls_version));
XiaokangQian7807f9f2022-02-15 10:04:37 +0000776
Ronald Cron5af4c7f2023-03-07 20:46:59 +0100777 return (int) tls_version;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000778}
779
Valerio Setti080a22b2023-03-20 15:22:47 +0100780#if defined(PSA_WANT_ALG_ECDH)
XiaokangQiane8ff3502022-04-22 02:34:40 +0000781/*
XiaokangQian7807f9f2022-02-15 10:04:37 +0000782 *
783 * From RFC 8446:
784 * enum {
785 * ... (0xFFFF)
786 * } NamedGroup;
787 * struct {
788 * NamedGroup named_group_list<2..2^16-1>;
789 * } NamedGroupList;
790 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200791MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100792static int ssl_tls13_parse_supported_groups_ext(mbedtls_ssl_context *ssl,
793 const unsigned char *buf,
794 const unsigned char *end)
XiaokangQian7807f9f2022-02-15 10:04:37 +0000795{
XiaokangQian7807f9f2022-02-15 10:04:37 +0000796 const unsigned char *p = buf;
XiaokangQian84823772022-04-19 07:57:30 +0000797 size_t named_group_list_len;
XiaokangQian8f9dfe42022-04-15 02:52:39 +0000798 const unsigned char *named_group_list_end;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000799
Gilles Peskine449bd832023-01-11 14:50:10 +0100800 MBEDTLS_SSL_DEBUG_BUF(3, "supported_groups extension", p, end - buf);
801 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, 2);
802 named_group_list_len = MBEDTLS_GET_UINT16_BE(p, 0);
XiaokangQian7807f9f2022-02-15 10:04:37 +0000803 p += 2;
Gilles Peskine449bd832023-01-11 14:50:10 +0100804 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, named_group_list_len);
XiaokangQian8f9dfe42022-04-15 02:52:39 +0000805 named_group_list_end = p + named_group_list_len;
XiaokangQian4e8cd7b2022-04-21 09:48:09 +0000806 ssl->handshake->hrr_selected_group = 0;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000807
Gilles Peskine449bd832023-01-11 14:50:10 +0100808 while (p < named_group_list_end) {
XiaokangQian08037552022-04-20 07:16:41 +0000809 uint16_t named_group;
Gilles Peskine449bd832023-01-11 14:50:10 +0100810 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, named_group_list_end, 2);
811 named_group = MBEDTLS_GET_UINT16_BE(p, 0);
XiaokangQian08037552022-04-20 07:16:41 +0000812 p += 2;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000813
Gilles Peskine449bd832023-01-11 14:50:10 +0100814 MBEDTLS_SSL_DEBUG_MSG(2,
815 ("got named group: %s(%04x)",
816 mbedtls_ssl_named_group_to_str(named_group),
817 named_group));
XiaokangQian08037552022-04-20 07:16:41 +0000818
Gilles Peskine449bd832023-01-11 14:50:10 +0100819 if (!mbedtls_ssl_named_group_is_offered(ssl, named_group) ||
820 !mbedtls_ssl_named_group_is_supported(named_group) ||
821 ssl->handshake->hrr_selected_group != 0) {
XiaokangQian08037552022-04-20 07:16:41 +0000822 continue;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000823 }
824
Gilles Peskine449bd832023-01-11 14:50:10 +0100825 MBEDTLS_SSL_DEBUG_MSG(2,
826 ("add named group %s(%04x) into received list.",
827 mbedtls_ssl_named_group_to_str(named_group),
828 named_group));
Jerry Yuc1be19f2022-04-23 16:11:39 +0800829
XiaokangQian4e8cd7b2022-04-21 09:48:09 +0000830 ssl->handshake->hrr_selected_group = named_group;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000831 }
832
Gilles Peskine449bd832023-01-11 14:50:10 +0100833 return 0;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000834
835}
Valerio Setti080a22b2023-03-20 15:22:47 +0100836#endif /* PSA_WANT_ALG_ECDH */
XiaokangQian7807f9f2022-02-15 10:04:37 +0000837
XiaokangQian08037552022-04-20 07:16:41 +0000838#define SSL_TLS1_3_PARSE_KEY_SHARES_EXT_NO_MATCH 1
839
Valerio Setti080a22b2023-03-20 15:22:47 +0100840#if defined(PSA_WANT_ALG_ECDH)
XiaokangQian7807f9f2022-02-15 10:04:37 +0000841/*
842 * ssl_tls13_parse_key_shares_ext() verifies whether the information in the
XiaokangQiane8ff3502022-04-22 02:34:40 +0000843 * extension is correct and stores the first acceptable key share and its associated group.
XiaokangQian7807f9f2022-02-15 10:04:37 +0000844 *
845 * Possible return values are:
846 * - 0: Successful processing of the client provided key share extension.
XiaokangQian08037552022-04-20 07:16:41 +0000847 * - SSL_TLS1_3_PARSE_KEY_SHARES_EXT_NO_MATCH: The key shares provided by the client
XiaokangQian7807f9f2022-02-15 10:04:37 +0000848 * does not match a group supported by the server. A HelloRetryRequest will
849 * be needed.
XiaokangQiane8ff3502022-04-22 02:34:40 +0000850 * - A negative value for fatal errors.
Jerry Yuc1be19f2022-04-23 16:11:39 +0800851 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200852MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100853static int ssl_tls13_parse_key_shares_ext(mbedtls_ssl_context *ssl,
854 const unsigned char *buf,
855 const unsigned char *end)
XiaokangQian7807f9f2022-02-15 10:04:37 +0000856{
XiaokangQianb67384d2022-04-19 00:02:38 +0000857 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000858 unsigned char const *p = buf;
XiaokangQian8f9dfe42022-04-15 02:52:39 +0000859 unsigned char const *client_shares_end;
Jerry Yuc1be19f2022-04-23 16:11:39 +0800860 size_t client_shares_len;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000861
862 /* From RFC 8446:
863 *
864 * struct {
865 * KeyShareEntry client_shares<0..2^16-1>;
866 * } KeyShareClientHello;
867 *
868 */
869
Gilles Peskine449bd832023-01-11 14:50:10 +0100870 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, 2);
871 client_shares_len = MBEDTLS_GET_UINT16_BE(p, 0);
XiaokangQian7807f9f2022-02-15 10:04:37 +0000872 p += 2;
Gilles Peskine449bd832023-01-11 14:50:10 +0100873 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, client_shares_len);
XiaokangQian7807f9f2022-02-15 10:04:37 +0000874
875 ssl->handshake->offered_group_id = 0;
XiaokangQian8f9dfe42022-04-15 02:52:39 +0000876 client_shares_end = p + client_shares_len;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000877
878 /* We try to find a suitable key share entry and copy it to the
879 * handshake context. Later, we have to find out whether we can do
880 * something with the provided key share or whether we have to
XiaokangQianc5763b52022-04-02 03:34:37 +0000881 * dismiss it and send a HelloRetryRequest message.
882 */
XiaokangQian7807f9f2022-02-15 10:04:37 +0000883
Gilles Peskine449bd832023-01-11 14:50:10 +0100884 while (p < client_shares_end) {
XiaokangQian9b5d04b2022-04-10 10:20:43 +0000885 uint16_t group;
Jerry Yuc1be19f2022-04-23 16:11:39 +0800886 size_t key_exchange_len;
Jerry Yu086edc22022-05-05 10:50:38 +0800887 const unsigned char *key_exchange;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000888
889 /*
890 * struct {
891 * NamedGroup group;
892 * opaque key_exchange<1..2^16-1>;
893 * } KeyShareEntry;
894 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100895 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, client_shares_end, 4);
896 group = MBEDTLS_GET_UINT16_BE(p, 0);
897 key_exchange_len = MBEDTLS_GET_UINT16_BE(p, 2);
Jerry Yuc1be19f2022-04-23 16:11:39 +0800898 p += 4;
Jerry Yu086edc22022-05-05 10:50:38 +0800899 key_exchange = p;
Gilles Peskine449bd832023-01-11 14:50:10 +0100900 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, client_shares_end, key_exchange_len);
Jerry Yu086edc22022-05-05 10:50:38 +0800901 p += key_exchange_len;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000902
903 /* Continue parsing even if we have already found a match,
XiaokangQianc5763b52022-04-02 03:34:37 +0000904 * for input validation purposes.
905 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100906 if (!mbedtls_ssl_named_group_is_offered(ssl, group) ||
907 !mbedtls_ssl_named_group_is_supported(group) ||
908 ssl->handshake->offered_group_id != 0) {
XiaokangQian060d8672022-04-21 09:24:56 +0000909 continue;
910 }
XiaokangQian060d8672022-04-21 09:24:56 +0000911
XiaokangQian7807f9f2022-02-15 10:04:37 +0000912 /*
XiaokangQian8f9dfe42022-04-15 02:52:39 +0000913 * For now, we only support ECDHE groups.
XiaokangQian7807f9f2022-02-15 10:04:37 +0000914 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100915 if (mbedtls_ssl_tls13_named_group_is_ecdhe(group)) {
916 MBEDTLS_SSL_DEBUG_MSG(2, ("ECDH group: %s (%04x)",
917 mbedtls_ssl_named_group_to_str(group),
918 group));
XiaokangQian318dc762022-04-20 09:43:51 +0000919 ret = mbedtls_ssl_tls13_read_public_ecdhe_share(
Gilles Peskine449bd832023-01-11 14:50:10 +0100920 ssl, key_exchange - 2, key_exchange_len + 2);
921 if (ret != 0) {
922 return ret;
923 }
XiaokangQian4e8cd7b2022-04-21 09:48:09 +0000924
Gilles Peskine449bd832023-01-11 14:50:10 +0100925 } else {
926 MBEDTLS_SSL_DEBUG_MSG(4, ("Unrecognized NamedGroup %u",
927 (unsigned) group));
XiaokangQian7807f9f2022-02-15 10:04:37 +0000928 continue;
929 }
930
XiaokangQian9b5d04b2022-04-10 10:20:43 +0000931 ssl->handshake->offered_group_id = group;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000932 }
933
Jerry Yuc1be19f2022-04-23 16:11:39 +0800934
Gilles Peskine449bd832023-01-11 14:50:10 +0100935 if (ssl->handshake->offered_group_id == 0) {
936 MBEDTLS_SSL_DEBUG_MSG(1, ("no matching key share"));
937 return SSL_TLS1_3_PARSE_KEY_SHARES_EXT_NO_MATCH;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000938 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100939 return 0;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000940}
Valerio Setti080a22b2023-03-20 15:22:47 +0100941#endif /* PSA_WANT_ALG_ECDH */
XiaokangQian7807f9f2022-02-15 10:04:37 +0000942
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200943MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100944static int ssl_tls13_client_hello_has_exts(mbedtls_ssl_context *ssl,
945 int exts_mask)
XiaokangQian7807f9f2022-02-15 10:04:37 +0000946{
Jerry Yu0c354a22022-08-29 15:25:36 +0800947 int masked = ssl->handshake->received_extensions & exts_mask;
Gilles Peskine449bd832023-01-11 14:50:10 +0100948 return masked == exts_mask;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000949}
950
Jerry Yue5991322022-11-07 14:03:44 +0800951#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200952MBEDTLS_CHECK_RETURN_CRITICAL
XiaokangQianb67384d2022-04-19 00:02:38 +0000953static int ssl_tls13_client_hello_has_exts_for_ephemeral_key_exchange(
Gilles Peskine449bd832023-01-11 14:50:10 +0100954 mbedtls_ssl_context *ssl)
XiaokangQian7807f9f2022-02-15 10:04:37 +0000955{
Gilles Peskine449bd832023-01-11 14:50:10 +0100956 return ssl_tls13_client_hello_has_exts(
957 ssl,
958 MBEDTLS_SSL_EXT_MASK(SUPPORTED_GROUPS) |
959 MBEDTLS_SSL_EXT_MASK(KEY_SHARE) |
960 MBEDTLS_SSL_EXT_MASK(SIG_ALG));
XiaokangQian7807f9f2022-02-15 10:04:37 +0000961}
Jerry Yue5991322022-11-07 14:03:44 +0800962#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED */
XiaokangQian7807f9f2022-02-15 10:04:37 +0000963
Jerry Yue5991322022-11-07 14:03:44 +0800964#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK_ENABLED)
Jerry Yu77f01482022-07-11 07:03:24 +0000965MBEDTLS_CHECK_RETURN_CRITICAL
966static int ssl_tls13_client_hello_has_exts_for_psk_key_exchange(
Gilles Peskine449bd832023-01-11 14:50:10 +0100967 mbedtls_ssl_context *ssl)
Jerry Yu77f01482022-07-11 07:03:24 +0000968{
Gilles Peskine449bd832023-01-11 14:50:10 +0100969 return ssl_tls13_client_hello_has_exts(
970 ssl,
971 MBEDTLS_SSL_EXT_MASK(PRE_SHARED_KEY) |
972 MBEDTLS_SSL_EXT_MASK(PSK_KEY_EXCHANGE_MODES));
Jerry Yu77f01482022-07-11 07:03:24 +0000973}
Jerry Yue5991322022-11-07 14:03:44 +0800974#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK_ENABLED */
Jerry Yu77f01482022-07-11 07:03:24 +0000975
Jerry Yue5991322022-11-07 14:03:44 +0800976#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK_EPHEMERAL_ENABLED)
Jerry Yu77f01482022-07-11 07:03:24 +0000977MBEDTLS_CHECK_RETURN_CRITICAL
978static int ssl_tls13_client_hello_has_exts_for_psk_ephemeral_key_exchange(
Gilles Peskine449bd832023-01-11 14:50:10 +0100979 mbedtls_ssl_context *ssl)
Jerry Yu77f01482022-07-11 07:03:24 +0000980{
Gilles Peskine449bd832023-01-11 14:50:10 +0100981 return ssl_tls13_client_hello_has_exts(
982 ssl,
983 MBEDTLS_SSL_EXT_MASK(SUPPORTED_GROUPS) |
984 MBEDTLS_SSL_EXT_MASK(KEY_SHARE) |
985 MBEDTLS_SSL_EXT_MASK(PRE_SHARED_KEY) |
986 MBEDTLS_SSL_EXT_MASK(PSK_KEY_EXCHANGE_MODES));
Jerry Yu77f01482022-07-11 07:03:24 +0000987}
Jerry Yue5991322022-11-07 14:03:44 +0800988#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK_EPHEMERAL_ENABLED */
Jerry Yu77f01482022-07-11 07:03:24 +0000989
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200990MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100991static int ssl_tls13_check_ephemeral_key_exchange(mbedtls_ssl_context *ssl)
XiaokangQian7807f9f2022-02-15 10:04:37 +0000992{
Jerry Yue5991322022-11-07 14:03:44 +0800993#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +0100994 return mbedtls_ssl_conf_tls13_ephemeral_enabled(ssl) &&
995 ssl_tls13_client_hello_has_exts_for_ephemeral_key_exchange(ssl);
Jerry Yue5991322022-11-07 14:03:44 +0800996#else
997 ((void) ssl);
Gilles Peskine449bd832023-01-11 14:50:10 +0100998 return 0;
Jerry Yue5991322022-11-07 14:03:44 +0800999#endif
Jerry Yu77f01482022-07-11 07:03:24 +00001000}
XiaokangQian7807f9f2022-02-15 10:04:37 +00001001
Jerry Yu77f01482022-07-11 07:03:24 +00001002MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01001003static int ssl_tls13_check_psk_key_exchange(mbedtls_ssl_context *ssl)
Jerry Yu77f01482022-07-11 07:03:24 +00001004{
Jerry Yue5991322022-11-07 14:03:44 +08001005#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01001006 return mbedtls_ssl_conf_tls13_psk_enabled(ssl) &&
1007 mbedtls_ssl_tls13_psk_enabled(ssl) &&
1008 ssl_tls13_client_hello_has_exts_for_psk_key_exchange(ssl);
Jerry Yu77f01482022-07-11 07:03:24 +00001009#else
1010 ((void) ssl);
Gilles Peskine449bd832023-01-11 14:50:10 +01001011 return 0;
Jerry Yu77f01482022-07-11 07:03:24 +00001012#endif
1013}
XiaokangQian7807f9f2022-02-15 10:04:37 +00001014
Jerry Yu77f01482022-07-11 07:03:24 +00001015MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01001016static int ssl_tls13_check_psk_ephemeral_key_exchange(mbedtls_ssl_context *ssl)
Jerry Yu77f01482022-07-11 07:03:24 +00001017{
Jerry Yue5991322022-11-07 14:03:44 +08001018#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK_EPHEMERAL_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01001019 return mbedtls_ssl_conf_tls13_psk_ephemeral_enabled(ssl) &&
1020 mbedtls_ssl_tls13_psk_ephemeral_enabled(ssl) &&
1021 ssl_tls13_client_hello_has_exts_for_psk_ephemeral_key_exchange(ssl);
Jerry Yu77f01482022-07-11 07:03:24 +00001022#else
1023 ((void) ssl);
Gilles Peskine449bd832023-01-11 14:50:10 +01001024 return 0;
Jerry Yu77f01482022-07-11 07:03:24 +00001025#endif
1026}
1027
Gilles Peskine449bd832023-01-11 14:50:10 +01001028static int ssl_tls13_determine_key_exchange_mode(mbedtls_ssl_context *ssl)
Jerry Yu77f01482022-07-11 07:03:24 +00001029{
1030 /*
1031 * Determine the key exchange algorithm to use.
1032 * There are three types of key exchanges supported in TLS 1.3:
1033 * - (EC)DH with ECDSA,
1034 * - (EC)DH with PSK,
1035 * - plain PSK.
1036 *
1037 * The PSK-based key exchanges may additionally be used with 0-RTT.
1038 *
1039 * Our built-in order of preference is
Jerry Yuce6ed702022-07-22 21:49:53 +08001040 * 1 ) (EC)DHE-PSK Mode ( psk_ephemeral )
1041 * 2 ) Certificate Mode ( ephemeral )
1042 * 3 ) Plain PSK Mode ( psk )
Jerry Yu77f01482022-07-11 07:03:24 +00001043 */
1044
1045 ssl->handshake->key_exchange_mode = MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_NONE;
1046
Gilles Peskine449bd832023-01-11 14:50:10 +01001047 if (ssl_tls13_check_psk_ephemeral_key_exchange(ssl)) {
Jerry Yu77f01482022-07-11 07:03:24 +00001048 ssl->handshake->key_exchange_mode =
1049 MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK_EPHEMERAL;
Gilles Peskine449bd832023-01-11 14:50:10 +01001050 MBEDTLS_SSL_DEBUG_MSG(2, ("key exchange mode: psk_ephemeral"));
1051 } else
1052 if (ssl_tls13_check_ephemeral_key_exchange(ssl)) {
Jerry Yu77f01482022-07-11 07:03:24 +00001053 ssl->handshake->key_exchange_mode =
1054 MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL;
Gilles Peskine449bd832023-01-11 14:50:10 +01001055 MBEDTLS_SSL_DEBUG_MSG(2, ("key exchange mode: ephemeral"));
1056 } else
1057 if (ssl_tls13_check_psk_key_exchange(ssl)) {
Jerry Yuce6ed702022-07-22 21:49:53 +08001058 ssl->handshake->key_exchange_mode =
1059 MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK;
Gilles Peskine449bd832023-01-11 14:50:10 +01001060 MBEDTLS_SSL_DEBUG_MSG(2, ("key exchange mode: psk"));
1061 } else {
Jerry Yu77f01482022-07-11 07:03:24 +00001062 MBEDTLS_SSL_DEBUG_MSG(
Gilles Peskine449bd832023-01-11 14:50:10 +01001063 1,
1064 ("ClientHello message misses mandatory extensions."));
1065 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_MISSING_EXTENSION,
1066 MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER);
1067 return MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER;
Jerry Yu77f01482022-07-11 07:03:24 +00001068 }
1069
Gilles Peskine449bd832023-01-11 14:50:10 +01001070 return 0;
Jerry Yu77f01482022-07-11 07:03:24 +00001071
XiaokangQian7807f9f2022-02-15 10:04:37 +00001072}
1073
XiaokangQian81802f42022-06-10 13:25:22 +00001074#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
Ronald Cron928cbd32022-10-04 16:14:26 +02001075 defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED)
Ronald Cron67ea2542022-09-15 17:34:42 +02001076
1077#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01001078static psa_algorithm_t ssl_tls13_iana_sig_alg_to_psa_alg(uint16_t sig_alg)
Ronald Cron67ea2542022-09-15 17:34:42 +02001079{
Gilles Peskine449bd832023-01-11 14:50:10 +01001080 switch (sig_alg) {
Ronald Cron67ea2542022-09-15 17:34:42 +02001081 case MBEDTLS_TLS1_3_SIG_ECDSA_SECP256R1_SHA256:
Gilles Peskine449bd832023-01-11 14:50:10 +01001082 return PSA_ALG_ECDSA(PSA_ALG_SHA_256);
Ronald Cron67ea2542022-09-15 17:34:42 +02001083 case MBEDTLS_TLS1_3_SIG_ECDSA_SECP384R1_SHA384:
Gilles Peskine449bd832023-01-11 14:50:10 +01001084 return PSA_ALG_ECDSA(PSA_ALG_SHA_384);
Ronald Cron67ea2542022-09-15 17:34:42 +02001085 case MBEDTLS_TLS1_3_SIG_ECDSA_SECP521R1_SHA512:
Gilles Peskine449bd832023-01-11 14:50:10 +01001086 return PSA_ALG_ECDSA(PSA_ALG_SHA_512);
Ronald Cron67ea2542022-09-15 17:34:42 +02001087 case MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA256:
Gilles Peskine449bd832023-01-11 14:50:10 +01001088 return PSA_ALG_RSA_PSS(PSA_ALG_SHA_256);
Ronald Cron67ea2542022-09-15 17:34:42 +02001089 case MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA384:
Gilles Peskine449bd832023-01-11 14:50:10 +01001090 return PSA_ALG_RSA_PSS(PSA_ALG_SHA_384);
Ronald Cron67ea2542022-09-15 17:34:42 +02001091 case MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA512:
Gilles Peskine449bd832023-01-11 14:50:10 +01001092 return PSA_ALG_RSA_PSS(PSA_ALG_SHA_512);
Ronald Cron67ea2542022-09-15 17:34:42 +02001093 case MBEDTLS_TLS1_3_SIG_RSA_PKCS1_SHA256:
Gilles Peskine449bd832023-01-11 14:50:10 +01001094 return PSA_ALG_RSA_PKCS1V15_SIGN(PSA_ALG_SHA_256);
Ronald Cron67ea2542022-09-15 17:34:42 +02001095 case MBEDTLS_TLS1_3_SIG_RSA_PKCS1_SHA384:
Gilles Peskine449bd832023-01-11 14:50:10 +01001096 return PSA_ALG_RSA_PKCS1V15_SIGN(PSA_ALG_SHA_384);
Ronald Cron67ea2542022-09-15 17:34:42 +02001097 case MBEDTLS_TLS1_3_SIG_RSA_PKCS1_SHA512:
Gilles Peskine449bd832023-01-11 14:50:10 +01001098 return PSA_ALG_RSA_PKCS1V15_SIGN(PSA_ALG_SHA_512);
Ronald Cron67ea2542022-09-15 17:34:42 +02001099 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01001100 return PSA_ALG_NONE;
Ronald Cron67ea2542022-09-15 17:34:42 +02001101 }
1102}
1103#endif /* MBEDTLS_USE_PSA_CRYPTO */
1104
XiaokangQian23c5be62022-06-07 02:04:34 +00001105/*
XiaokangQianfb665a82022-06-15 03:57:21 +00001106 * Pick best ( private key, certificate chain ) pair based on the signature
1107 * algorithms supported by the client.
XiaokangQian23c5be62022-06-07 02:04:34 +00001108 */
Ronald Cronce7d76e2022-07-08 18:56:49 +02001109MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01001110static int ssl_tls13_pick_key_cert(mbedtls_ssl_context *ssl)
XiaokangQian23c5be62022-06-07 02:04:34 +00001111{
XiaokangQianfb665a82022-06-15 03:57:21 +00001112 mbedtls_ssl_key_cert *key_cert, *key_cert_list;
XiaokangQian81802f42022-06-10 13:25:22 +00001113 const uint16_t *sig_alg = ssl->handshake->received_sig_algs;
XiaokangQian23c5be62022-06-07 02:04:34 +00001114
1115#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Gilles Peskine449bd832023-01-11 14:50:10 +01001116 if (ssl->handshake->sni_key_cert != NULL) {
XiaokangQianfb665a82022-06-15 03:57:21 +00001117 key_cert_list = ssl->handshake->sni_key_cert;
Gilles Peskine449bd832023-01-11 14:50:10 +01001118 } else
XiaokangQian23c5be62022-06-07 02:04:34 +00001119#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
Gilles Peskine449bd832023-01-11 14:50:10 +01001120 key_cert_list = ssl->conf->key_cert;
XiaokangQian23c5be62022-06-07 02:04:34 +00001121
Gilles Peskine449bd832023-01-11 14:50:10 +01001122 if (key_cert_list == NULL) {
1123 MBEDTLS_SSL_DEBUG_MSG(3, ("server has no certificate"));
1124 return -1;
XiaokangQian23c5be62022-06-07 02:04:34 +00001125 }
1126
Gilles Peskine449bd832023-01-11 14:50:10 +01001127 for (; *sig_alg != MBEDTLS_TLS1_3_SIG_NONE; sig_alg++) {
1128 if (!mbedtls_ssl_sig_alg_is_offered(ssl, *sig_alg)) {
Ronald Cron67ea2542022-09-15 17:34:42 +02001129 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +01001130 }
Ronald Cron67ea2542022-09-15 17:34:42 +02001131
Gilles Peskine449bd832023-01-11 14:50:10 +01001132 if (!mbedtls_ssl_tls13_sig_alg_for_cert_verify_is_supported(*sig_alg)) {
Ronald Cron67ea2542022-09-15 17:34:42 +02001133 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +01001134 }
Ronald Cron67ea2542022-09-15 17:34:42 +02001135
Gilles Peskine449bd832023-01-11 14:50:10 +01001136 for (key_cert = key_cert_list; key_cert != NULL;
1137 key_cert = key_cert->next) {
Ronald Cron67ea2542022-09-15 17:34:42 +02001138#if defined(MBEDTLS_USE_PSA_CRYPTO)
1139 psa_algorithm_t psa_alg = PSA_ALG_NONE;
1140#endif /* MBEDTLS_USE_PSA_CRYPTO */
1141
Gilles Peskine449bd832023-01-11 14:50:10 +01001142 MBEDTLS_SSL_DEBUG_CRT(3, "certificate (chain) candidate",
1143 key_cert->cert);
XiaokangQian81802f42022-06-10 13:25:22 +00001144
1145 /*
Gilles Peskine449bd832023-01-11 14:50:10 +01001146 * This avoids sending the client a cert it'll reject based on
1147 * keyUsage or other extensions.
1148 */
1149 if (mbedtls_x509_crt_check_key_usage(
1150 key_cert->cert, MBEDTLS_X509_KU_DIGITAL_SIGNATURE) != 0 ||
XiaokangQian81802f42022-06-10 13:25:22 +00001151 mbedtls_x509_crt_check_extended_key_usage(
XiaokangQianfb665a82022-06-15 03:57:21 +00001152 key_cert->cert, MBEDTLS_OID_SERVER_AUTH,
Gilles Peskine449bd832023-01-11 14:50:10 +01001153 MBEDTLS_OID_SIZE(MBEDTLS_OID_SERVER_AUTH)) != 0) {
1154 MBEDTLS_SSL_DEBUG_MSG(3, ("certificate mismatch: "
1155 "(extended) key usage extension"));
XiaokangQian81802f42022-06-10 13:25:22 +00001156 continue;
1157 }
XiaokangQian23c5be62022-06-07 02:04:34 +00001158
Gilles Peskine449bd832023-01-11 14:50:10 +01001159 MBEDTLS_SSL_DEBUG_MSG(3,
1160 ("ssl_tls13_pick_key_cert:"
1161 "check signature algorithm %s [%04x]",
1162 mbedtls_ssl_sig_alg_to_str(*sig_alg),
1163 *sig_alg));
Ronald Cron67ea2542022-09-15 17:34:42 +02001164#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01001165 psa_alg = ssl_tls13_iana_sig_alg_to_psa_alg(*sig_alg);
Ronald Cron67ea2542022-09-15 17:34:42 +02001166#endif /* MBEDTLS_USE_PSA_CRYPTO */
1167
Gilles Peskine449bd832023-01-11 14:50:10 +01001168 if (mbedtls_ssl_tls13_check_sig_alg_cert_key_match(
1169 *sig_alg, &key_cert->cert->pk)
Ronald Cron67ea2542022-09-15 17:34:42 +02001170#if defined(MBEDTLS_USE_PSA_CRYPTO)
1171 && psa_alg != PSA_ALG_NONE &&
Gilles Peskine449bd832023-01-11 14:50:10 +01001172 mbedtls_pk_can_do_ext(&key_cert->cert->pk, psa_alg,
1173 PSA_KEY_USAGE_SIGN_HASH) == 1
Ronald Cron67ea2542022-09-15 17:34:42 +02001174#endif /* MBEDTLS_USE_PSA_CRYPTO */
Gilles Peskine449bd832023-01-11 14:50:10 +01001175 ) {
XiaokangQianfb665a82022-06-15 03:57:21 +00001176 ssl->handshake->key_cert = key_cert;
Gilles Peskine449bd832023-01-11 14:50:10 +01001177 MBEDTLS_SSL_DEBUG_MSG(3,
1178 ("ssl_tls13_pick_key_cert:"
1179 "selected signature algorithm"
1180 " %s [%04x]",
1181 mbedtls_ssl_sig_alg_to_str(*sig_alg),
1182 *sig_alg));
XiaokangQianfb665a82022-06-15 03:57:21 +00001183 MBEDTLS_SSL_DEBUG_CRT(
Gilles Peskine449bd832023-01-11 14:50:10 +01001184 3, "selected certificate (chain)",
1185 ssl->handshake->key_cert->cert);
1186 return 0;
XiaokangQian81802f42022-06-10 13:25:22 +00001187 }
XiaokangQian81802f42022-06-10 13:25:22 +00001188 }
XiaokangQian23c5be62022-06-07 02:04:34 +00001189 }
1190
Gilles Peskine449bd832023-01-11 14:50:10 +01001191 MBEDTLS_SSL_DEBUG_MSG(2, ("ssl_tls13_pick_key_cert:"
1192 "no suitable certificate found"));
1193 return -1;
XiaokangQian23c5be62022-06-07 02:04:34 +00001194}
XiaokangQian81802f42022-06-10 13:25:22 +00001195#endif /* MBEDTLS_X509_CRT_PARSE_C &&
Ronald Cron928cbd32022-10-04 16:14:26 +02001196 MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED */
XiaokangQian23c5be62022-06-07 02:04:34 +00001197
XiaokangQian4080a7f2022-04-11 09:55:18 +00001198/*
XiaokangQianed582dd2022-04-13 08:21:05 +00001199 *
1200 * STATE HANDLING: ClientHello
1201 *
XiaokangQianb67384d2022-04-19 00:02:38 +00001202 * There are three possible classes of outcomes when parsing the ClientHello:
XiaokangQianed582dd2022-04-13 08:21:05 +00001203 *
XiaokangQianb67384d2022-04-19 00:02:38 +00001204 * 1) The ClientHello was well-formed and matched the server's configuration.
XiaokangQianed582dd2022-04-13 08:21:05 +00001205 *
1206 * In this case, the server progresses to sending its ServerHello.
1207 *
XiaokangQianb67384d2022-04-19 00:02:38 +00001208 * 2) The ClientHello was well-formed but didn't match the server's
1209 * configuration.
XiaokangQianed582dd2022-04-13 08:21:05 +00001210 *
1211 * For example, the client might not have offered a key share which
1212 * the server supports, or the server might require a cookie.
1213 *
1214 * In this case, the server sends a HelloRetryRequest.
1215 *
XiaokangQianb67384d2022-04-19 00:02:38 +00001216 * 3) The ClientHello was ill-formed
XiaokangQianed582dd2022-04-13 08:21:05 +00001217 *
1218 * In this case, we abort the handshake.
1219 *
1220 */
1221
1222/*
XiaokangQian4080a7f2022-04-11 09:55:18 +00001223 * Structure of this message:
1224 *
XiaokangQiane8ff3502022-04-22 02:34:40 +00001225 * uint16 ProtocolVersion;
1226 * opaque Random[32];
1227 * uint8 CipherSuite[2]; // Cryptographic suite selector
XiaokangQian4080a7f2022-04-11 09:55:18 +00001228 *
XiaokangQiane8ff3502022-04-22 02:34:40 +00001229 * struct {
1230 * ProtocolVersion legacy_version = 0x0303; // TLS v1.2
1231 * Random random;
1232 * opaque legacy_session_id<0..32>;
1233 * CipherSuite cipher_suites<2..2^16-2>;
1234 * opaque legacy_compression_methods<1..2^8-1>;
1235 * Extension extensions<8..2^16-1>;
1236 * } ClientHello;
XiaokangQian4080a7f2022-04-11 09:55:18 +00001237 */
XiaokangQianed582dd2022-04-13 08:21:05 +00001238
1239#define SSL_CLIENT_HELLO_OK 0
1240#define SSL_CLIENT_HELLO_HRR_REQUIRED 1
Ronald Cron5af4c7f2023-03-07 20:46:59 +01001241#define SSL_CLIENT_HELLO_TLS1_2 2
XiaokangQianed582dd2022-04-13 08:21:05 +00001242
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001243MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01001244static int ssl_tls13_parse_client_hello(mbedtls_ssl_context *ssl,
1245 const unsigned char *buf,
1246 const unsigned char *end)
XiaokangQian7807f9f2022-02-15 10:04:37 +00001247{
XiaokangQianb67384d2022-04-19 00:02:38 +00001248 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1249 const unsigned char *p = buf;
Ronald Crond540d992023-03-07 09:41:48 +01001250 const unsigned char *random;
XiaokangQian4080a7f2022-04-11 09:55:18 +00001251 size_t legacy_session_id_len;
Ronald Croncada4102023-03-07 09:51:39 +01001252 const unsigned char *legacy_session_id;
XiaokangQian318dc762022-04-20 09:43:51 +00001253 size_t cipher_suites_len;
Ronald Cron2f16b4e2023-03-07 10:07:32 +01001254 const unsigned char *cipher_suites;
XiaokangQian060d8672022-04-21 09:24:56 +00001255 const unsigned char *cipher_suites_end;
XiaokangQianb67384d2022-04-19 00:02:38 +00001256 size_t extensions_len;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001257 const unsigned char *extensions_end;
Ronald Cron8c527d02023-03-07 15:47:47 +01001258 const unsigned char *supported_versions_ext;
1259 const unsigned char *supported_versions_ext_end;
Jerry Yuc4bf5d62022-10-29 09:08:47 +08001260 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
Jerry Yu49ca9282022-05-05 11:05:22 +08001261 int hrr_required = 0;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001262
Ronald Cron41a443a2022-10-04 16:38:25 +02001263#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED)
Jerry Yu29d9faa2022-08-23 17:52:45 +08001264 const unsigned char *pre_shared_key_ext = NULL;
Jerry Yu1c105562022-07-10 06:32:38 +00001265 const unsigned char *pre_shared_key_ext_end = NULL;
Ronald Cron41a443a2022-10-04 16:38:25 +02001266#endif
XiaokangQian7807f9f2022-02-15 10:04:37 +00001267
XiaokangQian7807f9f2022-02-15 10:04:37 +00001268 /*
XiaokangQianb67384d2022-04-19 00:02:38 +00001269 * ClientHello layout:
XiaokangQian7807f9f2022-02-15 10:04:37 +00001270 * 0 . 1 protocol version
XiaokangQiane8ff3502022-04-22 02:34:40 +00001271 * 2 . 33 random bytes
XiaokangQianc5763b52022-04-02 03:34:37 +00001272 * 34 . 34 session id length ( 1 byte )
XiaokangQian7807f9f2022-02-15 10:04:37 +00001273 * 35 . 34+x session id
XiaokangQian7807f9f2022-02-15 10:04:37 +00001274 * .. . .. ciphersuite list length ( 2 bytes )
1275 * .. . .. ciphersuite list
1276 * .. . .. compression alg. list length ( 1 byte )
1277 * .. . .. compression alg. list
1278 * .. . .. extensions length ( 2 bytes, optional )
1279 * .. . .. extensions ( optional )
1280 */
1281
XiaokangQianb67384d2022-04-19 00:02:38 +00001282 /*
bootstrap-prime6dbbf442022-05-17 19:30:44 -04001283 * Minimal length ( with everything empty and extensions omitted ) is
XiaokangQian7807f9f2022-02-15 10:04:37 +00001284 * 2 + 32 + 1 + 2 + 1 = 38 bytes. Check that first, so that we can
1285 * read at least up to session id length without worrying.
1286 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001287 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, 38);
XiaokangQian7807f9f2022-02-15 10:04:37 +00001288
1289 /* ...
1290 * ProtocolVersion legacy_version = 0x0303; // TLS 1.2
1291 * ...
1292 * with ProtocolVersion defined as:
1293 * uint16 ProtocolVersion;
1294 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001295 if (mbedtls_ssl_read_version(p, ssl->conf->transport) !=
1296 MBEDTLS_SSL_VERSION_TLS1_2) {
1297 MBEDTLS_SSL_DEBUG_MSG(1, ("Unsupported version of TLS."));
1298 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_PROTOCOL_VERSION,
1299 MBEDTLS_ERR_SSL_BAD_PROTOCOL_VERSION);
1300 return MBEDTLS_ERR_SSL_BAD_PROTOCOL_VERSION;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001301 }
1302 p += 2;
1303
Jerry Yuc1be19f2022-04-23 16:11:39 +08001304 /* ...
1305 * Random random;
1306 * ...
XiaokangQianb67384d2022-04-19 00:02:38 +00001307 * with Random defined as:
1308 * opaque Random[32];
XiaokangQian7807f9f2022-02-15 10:04:37 +00001309 */
Ronald Crond540d992023-03-07 09:41:48 +01001310 random = p;
XiaokangQian08037552022-04-20 07:16:41 +00001311 p += MBEDTLS_CLIENT_HELLO_RANDOM_LEN;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001312
Jerry Yuc1be19f2022-04-23 16:11:39 +08001313 /* ...
XiaokangQianb67384d2022-04-19 00:02:38 +00001314 * opaque legacy_session_id<0..32>;
Jerry Yuc1be19f2022-04-23 16:11:39 +08001315 * ...
XiaokangQian7807f9f2022-02-15 10:04:37 +00001316 */
Ronald Croncada4102023-03-07 09:51:39 +01001317 legacy_session_id_len = *(p++);
1318 legacy_session_id = p;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001319
XiaokangQianb67384d2022-04-19 00:02:38 +00001320 /*
1321 * Check we have enough data for the legacy session identifier
Jerry Yue95c8af2022-07-26 15:48:20 +08001322 * and the ciphersuite list length.
XiaokangQianb67384d2022-04-19 00:02:38 +00001323 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001324 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, legacy_session_id_len + 2);
XiaokangQian4080a7f2022-04-11 09:55:18 +00001325 p += legacy_session_id_len;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001326
Ronald Cron2f16b4e2023-03-07 10:07:32 +01001327 /* ...
1328 * CipherSuite cipher_suites<2..2^16-2>;
1329 * ...
1330 * with CipherSuite defined as:
1331 * uint8 CipherSuite[2];
1332 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001333 cipher_suites_len = MBEDTLS_GET_UINT16_BE(p, 0);
XiaokangQian7807f9f2022-02-15 10:04:37 +00001334 p += 2;
Ronald Cron2f16b4e2023-03-07 10:07:32 +01001335 cipher_suites = p;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001336
Ronald Cronfc7ae872023-02-16 15:32:19 +01001337 /*
1338 * The length of the ciphersuite list has to be even.
1339 */
1340 if (cipher_suites_len & 1) {
1341 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR,
1342 MBEDTLS_ERR_SSL_DECODE_ERROR);
1343 return MBEDTLS_ERR_SSL_DECODE_ERROR;
1344 }
1345
XiaokangQianb67384d2022-04-19 00:02:38 +00001346 /* Check we have enough data for the ciphersuite list, the legacy
1347 * compression methods and the length of the extensions.
Jerry Yue95c8af2022-07-26 15:48:20 +08001348 *
1349 * cipher_suites cipher_suites_len bytes
1350 * legacy_compression_methods 2 bytes
1351 * extensions_len 2 bytes
XiaokangQianb67384d2022-04-19 00:02:38 +00001352 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001353 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, cipher_suites_len + 2 + 2);
Ronald Cron8c527d02023-03-07 15:47:47 +01001354 p += cipher_suites_len;
1355 cipher_suites_end = p;
Jerry Yu5725f1c2022-08-21 17:27:16 +08001356
1357 /*
Ronald Cron8c527d02023-03-07 15:47:47 +01001358 * Search for the supported versions extension and parse it to determine
1359 * if the client supports TLS 1.3.
1360 */
1361 ret = mbedtls_ssl_tls13_is_supported_versions_ext_present_in_exts(
1362 ssl, p + 2, end,
1363 &supported_versions_ext, &supported_versions_ext_end);
1364 if (ret < 0) {
1365 MBEDTLS_SSL_DEBUG_RET(1,
1366 ("mbedtls_ssl_tls13_is_supported_versions_ext_present_in_exts"), ret);
1367 return ret;
1368 }
1369
1370 if (ret == 0) {
Ronald Cron5af4c7f2023-03-07 20:46:59 +01001371 return SSL_CLIENT_HELLO_TLS1_2;
Ronald Cron8c527d02023-03-07 15:47:47 +01001372 }
1373
Ronald Cron5af4c7f2023-03-07 20:46:59 +01001374 if (ret == 1) {
1375 ret = ssl_tls13_parse_supported_versions_ext(ssl,
1376 supported_versions_ext,
1377 supported_versions_ext_end);
1378 if (ret < 0) {
1379 MBEDTLS_SSL_DEBUG_RET(1,
1380 ("ssl_tls13_parse_supported_versions_ext"), ret);
1381 return ret;
1382 }
1383
Ronald Cronb828c7d2023-04-03 16:37:22 +02001384 /*
1385 * The supported versions extension was parsed successfully as the
1386 * value returned by ssl_tls13_parse_supported_versions_ext() is
1387 * positive. The return value is then equal to
1388 * MBEDTLS_SSL_VERSION_TLS1_2 or MBEDTLS_SSL_VERSION_TLS1_3, defining
1389 * the TLS version to negotiate.
1390 */
Ronald Cron5af4c7f2023-03-07 20:46:59 +01001391 if (MBEDTLS_SSL_VERSION_TLS1_2 == ret) {
1392 return SSL_CLIENT_HELLO_TLS1_2;
1393 }
Ronald Cron8c527d02023-03-07 15:47:47 +01001394 }
1395
1396 /*
1397 * We negotiate TLS 1.3.
Ronald Cron64582392023-03-07 09:21:40 +01001398 */
1399 ssl->tls_version = MBEDTLS_SSL_VERSION_TLS1_3;
1400
1401#if defined(MBEDTLS_SSL_SESSION_TICKETS)
1402 /* Store minor version for later use with ticket serialization. */
1403 ssl->session_negotiate->tls_version = MBEDTLS_SSL_VERSION_TLS1_3;
1404 ssl->session_negotiate->endpoint = ssl->conf->endpoint;
1405#endif
1406
1407 /*
Ronald Crond540d992023-03-07 09:41:48 +01001408 * We are negotiation the version 1.3 of the protocol. Do what we have
Ronald Croncada4102023-03-07 09:51:39 +01001409 * postponed: copy of the client random bytes, copy of the legacy session
Ronald Cron2f16b4e2023-03-07 10:07:32 +01001410 * identifier and selection of the TLS 1.3 cipher suite.
Ronald Crond540d992023-03-07 09:41:48 +01001411 */
1412 MBEDTLS_SSL_DEBUG_BUF(3, "client hello, random bytes",
1413 random, MBEDTLS_CLIENT_HELLO_RANDOM_LEN);
1414 memcpy(&handshake->randbytes[0], random, MBEDTLS_CLIENT_HELLO_RANDOM_LEN);
1415
Ronald Croncada4102023-03-07 09:51:39 +01001416 if (legacy_session_id_len > sizeof(ssl->session_negotiate->id)) {
1417 MBEDTLS_SSL_DEBUG_MSG(1, ("bad client hello message"));
1418 return MBEDTLS_ERR_SSL_DECODE_ERROR;
1419 }
1420 ssl->session_negotiate->id_len = legacy_session_id_len;
1421 MBEDTLS_SSL_DEBUG_BUF(3, "client hello, session id",
1422 legacy_session_id, legacy_session_id_len);
1423 memcpy(&ssl->session_negotiate->id[0],
1424 legacy_session_id, legacy_session_id_len);
1425
Ronald Crond540d992023-03-07 09:41:48 +01001426 /*
Jerry Yu5725f1c2022-08-21 17:27:16 +08001427 * Search for a matching ciphersuite
1428 */
Ronald Cron2f16b4e2023-03-07 10:07:32 +01001429 MBEDTLS_SSL_DEBUG_BUF(3, "client hello, list of cipher suites",
1430 cipher_suites, cipher_suites_len);
1431 for (p = cipher_suites; p < cipher_suites_end; p += 2) {
XiaokangQiane8ff3502022-04-22 02:34:40 +00001432 uint16_t cipher_suite;
Gilles Peskine449bd832023-01-11 14:50:10 +01001433 const mbedtls_ssl_ciphersuite_t *ciphersuite_info;
Jerry Yu5725f1c2022-08-21 17:27:16 +08001434
Ronald Crond89360b2023-02-21 08:53:33 +01001435 /*
1436 * "cipher_suite_end - p is even" is an invariant of the loop. As
1437 * cipher_suites_end - p > 0, we have cipher_suites_end - p >= 2 and
1438 * it is thus safe to read two bytes.
1439 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001440 cipher_suite = MBEDTLS_GET_UINT16_BE(p, 0);
Jerry Yuc5a23a02022-08-25 10:51:44 +08001441 ciphersuite_info = ssl_tls13_validate_peer_ciphersuite(
Gilles Peskine449bd832023-01-11 14:50:10 +01001442 ssl, cipher_suite);
1443 if (ciphersuite_info == NULL) {
Jerry Yu5725f1c2022-08-21 17:27:16 +08001444 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +01001445 }
Jerry Yu5725f1c2022-08-21 17:27:16 +08001446
Jerry Yu5725f1c2022-08-21 17:27:16 +08001447 ssl->session_negotiate->ciphersuite = cipher_suite;
Jerry Yuc4bf5d62022-10-29 09:08:47 +08001448 handshake->ciphersuite_info = ciphersuite_info;
Gilles Peskine449bd832023-01-11 14:50:10 +01001449 MBEDTLS_SSL_DEBUG_MSG(2, ("selected ciphersuite: %04x - %s",
1450 cipher_suite,
1451 ciphersuite_info->name));
Ronald Cron25e9ec62023-02-16 15:35:16 +01001452 break;
XiaokangQian17f974c2022-04-19 09:57:41 +00001453 }
Jerry Yu5725f1c2022-08-21 17:27:16 +08001454
Gilles Peskine449bd832023-01-11 14:50:10 +01001455 if (handshake->ciphersuite_info == NULL) {
1456 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE,
1457 MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE);
1458 return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
Jerry Yu5725f1c2022-08-21 17:27:16 +08001459 }
Ronald Cron25e9ec62023-02-16 15:35:16 +01001460 p = cipher_suites_end;
Jerry Yuc1be19f2022-04-23 16:11:39 +08001461
XiaokangQian4080a7f2022-04-11 09:55:18 +00001462 /* ...
XiaokangQianb67384d2022-04-19 00:02:38 +00001463 * opaque legacy_compression_methods<1..2^8-1>;
XiaokangQian4080a7f2022-04-11 09:55:18 +00001464 * ...
XiaokangQian7807f9f2022-02-15 10:04:37 +00001465 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001466 if (p[0] != 1 || p[1] != MBEDTLS_SSL_COMPRESS_NULL) {
1467 MBEDTLS_SSL_DEBUG_MSG(1, ("bad legacy compression method"));
1468 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER,
1469 MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER);
1470 return MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001471 }
XiaokangQianb67384d2022-04-19 00:02:38 +00001472 p += 2;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001473
Jerry Yuc1be19f2022-04-23 16:11:39 +08001474 /* ...
XiaokangQianb67384d2022-04-19 00:02:38 +00001475 * Extension extensions<8..2^16-1>;
Jerry Yuc1be19f2022-04-23 16:11:39 +08001476 * ...
XiaokangQianb67384d2022-04-19 00:02:38 +00001477 * with Extension defined as:
1478 * struct {
1479 * ExtensionType extension_type;
1480 * opaque extension_data<0..2^16-1>;
1481 * } Extension;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001482 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001483 extensions_len = MBEDTLS_GET_UINT16_BE(p, 0);
XiaokangQian7807f9f2022-02-15 10:04:37 +00001484 p += 2;
Gilles Peskine449bd832023-01-11 14:50:10 +01001485 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, extensions_len);
XiaokangQiane8ff3502022-04-22 02:34:40 +00001486 extensions_end = p + extensions_len;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001487
Gilles Peskine449bd832023-01-11 14:50:10 +01001488 MBEDTLS_SSL_DEBUG_BUF(3, "client hello extensions", p, extensions_len);
Jerry Yu63a459c2022-10-31 13:38:40 +08001489 handshake->received_extensions = MBEDTLS_SSL_EXT_MASK_NONE;
Jerry Yu0c354a22022-08-29 15:25:36 +08001490
Gilles Peskine449bd832023-01-11 14:50:10 +01001491 while (p < extensions_end) {
XiaokangQian7807f9f2022-02-15 10:04:37 +00001492 unsigned int extension_type;
1493 size_t extension_data_len;
1494 const unsigned char *extension_data_end;
1495
Jerry Yu0c354a22022-08-29 15:25:36 +08001496 /* RFC 8446, section 4.2.11
Jerry Yu1c9247c2022-07-21 12:37:39 +08001497 *
1498 * The "pre_shared_key" extension MUST be the last extension in the
1499 * ClientHello (this facilitates implementation as described below).
1500 * Servers MUST check that it is the last extension and otherwise fail
1501 * the handshake with an "illegal_parameter" alert.
1502 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001503 if (handshake->received_extensions & MBEDTLS_SSL_EXT_MASK(PRE_SHARED_KEY)) {
Jerry Yu1c9247c2022-07-21 12:37:39 +08001504 MBEDTLS_SSL_DEBUG_MSG(
Gilles Peskine449bd832023-01-11 14:50:10 +01001505 3, ("pre_shared_key is not last extension."));
Jerry Yu1c9247c2022-07-21 12:37:39 +08001506 MBEDTLS_SSL_PEND_FATAL_ALERT(
1507 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER,
Gilles Peskine449bd832023-01-11 14:50:10 +01001508 MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER);
1509 return MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER;
Jerry Yu1c9247c2022-07-21 12:37:39 +08001510 }
1511
Gilles Peskine449bd832023-01-11 14:50:10 +01001512 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, extensions_end, 4);
1513 extension_type = MBEDTLS_GET_UINT16_BE(p, 0);
1514 extension_data_len = MBEDTLS_GET_UINT16_BE(p, 2);
XiaokangQian7807f9f2022-02-15 10:04:37 +00001515 p += 4;
1516
Gilles Peskine449bd832023-01-11 14:50:10 +01001517 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, extensions_end, extension_data_len);
XiaokangQian7807f9f2022-02-15 10:04:37 +00001518 extension_data_end = p + extension_data_len;
1519
Jerry Yuc4bf5d62022-10-29 09:08:47 +08001520 ret = mbedtls_ssl_tls13_check_received_extension(
Gilles Peskine449bd832023-01-11 14:50:10 +01001521 ssl, MBEDTLS_SSL_HS_CLIENT_HELLO, extension_type,
1522 MBEDTLS_SSL_TLS1_3_ALLOWED_EXTS_OF_CH);
1523 if (ret != 0) {
1524 return ret;
1525 }
Jerry Yue18dc7e2022-08-04 16:29:22 +08001526
Gilles Peskine449bd832023-01-11 14:50:10 +01001527 switch (extension_type) {
XiaokangQian40a35232022-05-07 09:02:40 +00001528#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
1529 case MBEDTLS_TLS_EXT_SERVERNAME:
Gilles Peskine449bd832023-01-11 14:50:10 +01001530 MBEDTLS_SSL_DEBUG_MSG(3, ("found ServerName extension"));
1531 ret = mbedtls_ssl_parse_server_name_ext(ssl, p,
1532 extension_data_end);
1533 if (ret != 0) {
XiaokangQian40a35232022-05-07 09:02:40 +00001534 MBEDTLS_SSL_DEBUG_RET(
Gilles Peskine449bd832023-01-11 14:50:10 +01001535 1, "mbedtls_ssl_parse_servername_ext", ret);
1536 return ret;
XiaokangQian40a35232022-05-07 09:02:40 +00001537 }
XiaokangQian40a35232022-05-07 09:02:40 +00001538 break;
1539#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
1540
Valerio Setti080a22b2023-03-20 15:22:47 +01001541#if defined(PSA_WANT_ALG_ECDH)
XiaokangQian7807f9f2022-02-15 10:04:37 +00001542 case MBEDTLS_TLS_EXT_SUPPORTED_GROUPS:
Gilles Peskine449bd832023-01-11 14:50:10 +01001543 MBEDTLS_SSL_DEBUG_MSG(3, ("found supported group extension"));
XiaokangQian7807f9f2022-02-15 10:04:37 +00001544
1545 /* Supported Groups Extension
1546 *
1547 * When sent by the client, the "supported_groups" extension
1548 * indicates the named groups which the client supports,
1549 * ordered from most preferred to least preferred.
1550 */
Jerry Yuc1be19f2022-04-23 16:11:39 +08001551 ret = ssl_tls13_parse_supported_groups_ext(
Gilles Peskine449bd832023-01-11 14:50:10 +01001552 ssl, p, extension_data_end);
1553 if (ret != 0) {
1554 MBEDTLS_SSL_DEBUG_RET(1,
1555 "mbedtls_ssl_parse_supported_groups_ext", ret);
1556 return ret;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001557 }
1558
XiaokangQian7807f9f2022-02-15 10:04:37 +00001559 break;
Valerio Setti080a22b2023-03-20 15:22:47 +01001560#endif /* PSA_WANT_ALG_ECDH */
XiaokangQian7807f9f2022-02-15 10:04:37 +00001561
Valerio Setti080a22b2023-03-20 15:22:47 +01001562#if defined(PSA_WANT_ALG_ECDH)
XiaokangQian7807f9f2022-02-15 10:04:37 +00001563 case MBEDTLS_TLS_EXT_KEY_SHARE:
Gilles Peskine449bd832023-01-11 14:50:10 +01001564 MBEDTLS_SSL_DEBUG_MSG(3, ("found key share extension"));
XiaokangQian7807f9f2022-02-15 10:04:37 +00001565
1566 /*
1567 * Key Share Extension
1568 *
1569 * When sent by the client, the "key_share" extension
1570 * contains the endpoint's cryptographic parameters for
1571 * ECDHE/DHE key establishment methods.
1572 */
Jerry Yuc1be19f2022-04-23 16:11:39 +08001573 ret = ssl_tls13_parse_key_shares_ext(
Gilles Peskine449bd832023-01-11 14:50:10 +01001574 ssl, p, extension_data_end);
1575 if (ret == SSL_TLS1_3_PARSE_KEY_SHARES_EXT_NO_MATCH) {
1576 MBEDTLS_SSL_DEBUG_MSG(2, ("HRR needed "));
Jerry Yu49ca9282022-05-05 11:05:22 +08001577 hrr_required = 1;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001578 }
1579
Gilles Peskine449bd832023-01-11 14:50:10 +01001580 if (ret < 0) {
Jerry Yu582dd062022-04-22 21:59:01 +08001581 MBEDTLS_SSL_DEBUG_RET(
Gilles Peskine449bd832023-01-11 14:50:10 +01001582 1, "ssl_tls13_parse_key_shares_ext", ret);
1583 return ret;
Jerry Yu582dd062022-04-22 21:59:01 +08001584 }
XiaokangQian7807f9f2022-02-15 10:04:37 +00001585
XiaokangQian7807f9f2022-02-15 10:04:37 +00001586 break;
Valerio Setti080a22b2023-03-20 15:22:47 +01001587#endif /* PSA_WANT_ALG_ECDH */
XiaokangQian7807f9f2022-02-15 10:04:37 +00001588
1589 case MBEDTLS_TLS_EXT_SUPPORTED_VERSIONS:
Ronald Cron8c527d02023-03-07 15:47:47 +01001590 /* Already parsed */
XiaokangQian7807f9f2022-02-15 10:04:37 +00001591 break;
1592
Ronald Cron41a443a2022-10-04 16:38:25 +02001593#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED)
Jerry Yue19e3b92022-07-08 12:04:51 +00001594 case MBEDTLS_TLS_EXT_PSK_KEY_EXCHANGE_MODES:
Gilles Peskine449bd832023-01-11 14:50:10 +01001595 MBEDTLS_SSL_DEBUG_MSG(3, ("found psk key exchange modes extension"));
Jerry Yue19e3b92022-07-08 12:04:51 +00001596
1597 ret = ssl_tls13_parse_key_exchange_modes_ext(
Gilles Peskine449bd832023-01-11 14:50:10 +01001598 ssl, p, extension_data_end);
1599 if (ret != 0) {
Jerry Yue19e3b92022-07-08 12:04:51 +00001600 MBEDTLS_SSL_DEBUG_RET(
Gilles Peskine449bd832023-01-11 14:50:10 +01001601 1, "ssl_tls13_parse_key_exchange_modes_ext", ret);
1602 return ret;
Jerry Yue19e3b92022-07-08 12:04:51 +00001603 }
1604
Jerry Yue19e3b92022-07-08 12:04:51 +00001605 break;
Ronald Cron41a443a2022-10-04 16:38:25 +02001606#endif
Jerry Yue19e3b92022-07-08 12:04:51 +00001607
Jerry Yu1c105562022-07-10 06:32:38 +00001608 case MBEDTLS_TLS_EXT_PRE_SHARED_KEY:
Gilles Peskine449bd832023-01-11 14:50:10 +01001609 MBEDTLS_SSL_DEBUG_MSG(3, ("found pre_shared_key extension"));
1610 if ((handshake->received_extensions &
1611 MBEDTLS_SSL_EXT_MASK(PSK_KEY_EXCHANGE_MODES)) == 0) {
Jerry Yu13ab81d2022-07-22 23:17:11 +08001612 MBEDTLS_SSL_PEND_FATAL_ALERT(
1613 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER,
Gilles Peskine449bd832023-01-11 14:50:10 +01001614 MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER);
1615 return MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER;
Jerry Yu13ab81d2022-07-22 23:17:11 +08001616 }
Ronald Cron41a443a2022-10-04 16:38:25 +02001617#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED)
Jerry Yu1c105562022-07-10 06:32:38 +00001618 /* Delay processing of the PSK identity once we have
1619 * found out which algorithms to use. We keep a pointer
1620 * to the buffer and the size for later processing.
1621 */
Jerry Yu29d9faa2022-08-23 17:52:45 +08001622 pre_shared_key_ext = p;
Jerry Yu1c105562022-07-10 06:32:38 +00001623 pre_shared_key_ext_end = extension_data_end;
Jerry Yue18dc7e2022-08-04 16:29:22 +08001624#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED */
Jerry Yu1c105562022-07-10 06:32:38 +00001625 break;
Jerry Yu1c105562022-07-10 06:32:38 +00001626
XiaokangQianacb39922022-06-17 10:18:48 +00001627#if defined(MBEDTLS_SSL_ALPN)
1628 case MBEDTLS_TLS_EXT_ALPN:
Gilles Peskine449bd832023-01-11 14:50:10 +01001629 MBEDTLS_SSL_DEBUG_MSG(3, ("found alpn extension"));
XiaokangQianacb39922022-06-17 10:18:48 +00001630
Gilles Peskine449bd832023-01-11 14:50:10 +01001631 ret = mbedtls_ssl_parse_alpn_ext(ssl, p, extension_data_end);
1632 if (ret != 0) {
XiaokangQianacb39922022-06-17 10:18:48 +00001633 MBEDTLS_SSL_DEBUG_RET(
Gilles Peskine449bd832023-01-11 14:50:10 +01001634 1, ("mbedtls_ssl_parse_alpn_ext"), ret);
1635 return ret;
XiaokangQianacb39922022-06-17 10:18:48 +00001636 }
XiaokangQianacb39922022-06-17 10:18:48 +00001637 break;
1638#endif /* MBEDTLS_SSL_ALPN */
1639
Ronald Cron928cbd32022-10-04 16:14:26 +02001640#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED)
XiaokangQian7807f9f2022-02-15 10:04:37 +00001641 case MBEDTLS_TLS_EXT_SIG_ALG:
Gilles Peskine449bd832023-01-11 14:50:10 +01001642 MBEDTLS_SSL_DEBUG_MSG(3, ("found signature_algorithms extension"));
XiaokangQian7807f9f2022-02-15 10:04:37 +00001643
Gabor Mezei078e8032022-04-27 21:17:56 +02001644 ret = mbedtls_ssl_parse_sig_alg_ext(
Gilles Peskine449bd832023-01-11 14:50:10 +01001645 ssl, p, extension_data_end);
1646 if (ret != 0) {
1647 MBEDTLS_SSL_DEBUG_MSG(1,
1648 (
1649 "ssl_parse_supported_signature_algorithms_server_ext ( %d )",
1650 ret));
1651 return ret;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001652 }
XiaokangQian7807f9f2022-02-15 10:04:37 +00001653 break;
Ronald Cron928cbd32022-10-04 16:14:26 +02001654#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED */
XiaokangQian7807f9f2022-02-15 10:04:37 +00001655
Jan Bruckner151f6422023-02-10 12:45:19 +01001656#if defined(MBEDTLS_SSL_RECORD_SIZE_LIMIT)
1657 case MBEDTLS_TLS_EXT_RECORD_SIZE_LIMIT:
1658 MBEDTLS_SSL_DEBUG_MSG(3, ("found record_size_limit extension"));
1659
1660 ret = mbedtls_ssl_tls13_parse_record_size_limit_ext(ssl, p, extension_data_end);
1661
Jan Bruckner1a38e542023-03-15 14:15:11 +01001662 /* TODO: Return unconditionally here until we handle the record size limit correctly.
1663 * Once handled correctly, only return in case of errors. */
Jan Bruckner151f6422023-02-10 12:45:19 +01001664 return ret;
1665
1666 break;
1667#endif /* MBEDTLS_SSL_RECORD_SIZE_LIMIT */
1668
XiaokangQian7807f9f2022-02-15 10:04:37 +00001669 default:
Jerry Yu79aa7212022-11-08 21:30:21 +08001670 MBEDTLS_SSL_PRINT_EXT(
Jerry Yu63a459c2022-10-31 13:38:40 +08001671 3, MBEDTLS_SSL_HS_CLIENT_HELLO,
Gilles Peskine449bd832023-01-11 14:50:10 +01001672 extension_type, "( ignored )");
Jerry Yu0c354a22022-08-29 15:25:36 +08001673 break;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001674 }
1675
1676 p += extension_data_len;
1677 }
1678
Gilles Peskine449bd832023-01-11 14:50:10 +01001679 MBEDTLS_SSL_PRINT_EXTS(3, MBEDTLS_SSL_HS_CLIENT_HELLO,
1680 handshake->received_extensions);
Jerry Yue18dc7e2022-08-04 16:29:22 +08001681
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +01001682 ret = mbedtls_ssl_add_hs_hdr_to_checksum(ssl,
1683 MBEDTLS_SSL_HS_CLIENT_HELLO,
1684 p - buf);
1685 if (0 != ret) {
1686 MBEDTLS_SSL_DEBUG_RET(1, ("mbedtls_ssl_add_hs_hdr_to_checksum"), ret);
1687 return ret;
1688 }
Jerry Yu032b15ce2022-07-11 06:10:03 +00001689
Ronald Cron41a443a2022-10-04 16:38:25 +02001690#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED)
XiaokangQian7807f9f2022-02-15 10:04:37 +00001691 /* Update checksum with either
1692 * - The entire content of the CH message, if no PSK extension is present
1693 * - The content up to but excluding the PSK extension, if present.
1694 */
Jerry Yu1c105562022-07-10 06:32:38 +00001695 /* If we've settled on a PSK-based exchange, parse PSK identity ext */
Gilles Peskine449bd832023-01-11 14:50:10 +01001696 if (mbedtls_ssl_tls13_some_psk_enabled(ssl) &&
1697 mbedtls_ssl_conf_tls13_some_psk_enabled(ssl) &&
1698 (handshake->received_extensions & MBEDTLS_SSL_EXT_MASK(PRE_SHARED_KEY))) {
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +01001699 ret = handshake->update_checksum(ssl, buf,
1700 pre_shared_key_ext - buf);
1701 if (0 != ret) {
1702 MBEDTLS_SSL_DEBUG_RET(1, ("update_checksum"), ret);
1703 return ret;
1704 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001705 ret = ssl_tls13_parse_pre_shared_key_ext(ssl,
1706 pre_shared_key_ext,
1707 pre_shared_key_ext_end,
1708 cipher_suites,
1709 cipher_suites_end);
1710 if (ret == MBEDTLS_ERR_SSL_UNKNOWN_IDENTITY) {
1711 handshake->received_extensions &= ~MBEDTLS_SSL_EXT_MASK(PRE_SHARED_KEY);
1712 } else if (ret != 0) {
Jerry Yu63a459c2022-10-31 13:38:40 +08001713 MBEDTLS_SSL_DEBUG_RET(
Gilles Peskine449bd832023-01-11 14:50:10 +01001714 1, "ssl_tls13_parse_pre_shared_key_ext", ret);
1715 return ret;
Jerry Yu1c105562022-07-10 06:32:38 +00001716 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001717 } else
Ronald Cron41a443a2022-10-04 16:38:25 +02001718#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED */
Jerry Yu1c105562022-07-10 06:32:38 +00001719 {
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +01001720 ret = handshake->update_checksum(ssl, buf, p - buf);
1721 if (0 != ret) {
1722 MBEDTLS_SSL_DEBUG_RET(1, ("update_checksum"), ret);
1723 return ret;
1724 }
Jerry Yu1c105562022-07-10 06:32:38 +00001725 }
XiaokangQian7807f9f2022-02-15 10:04:37 +00001726
Gilles Peskine449bd832023-01-11 14:50:10 +01001727 ret = ssl_tls13_determine_key_exchange_mode(ssl);
1728 if (ret < 0) {
1729 return ret;
1730 }
XiaokangQian7807f9f2022-02-15 10:04:37 +00001731
Gilles Peskine449bd832023-01-11 14:50:10 +01001732 mbedtls_ssl_optimize_checksum(ssl, handshake->ciphersuite_info);
Jerry Yue5834fd2022-08-29 20:16:09 +08001733
Gilles Peskine449bd832023-01-11 14:50:10 +01001734 return hrr_required ? SSL_CLIENT_HELLO_HRR_REQUIRED : SSL_CLIENT_HELLO_OK;
XiaokangQian75fe8c72022-06-15 09:42:45 +00001735}
1736
1737/* Update the handshake state machine */
1738
Ronald Cronce7d76e2022-07-08 18:56:49 +02001739MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01001740static int ssl_tls13_postprocess_client_hello(mbedtls_ssl_context *ssl)
XiaokangQian75fe8c72022-06-15 09:42:45 +00001741{
1742 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1743
XiaokangQian7807f9f2022-02-15 10:04:37 +00001744 /*
XiaokangQianfb665a82022-06-15 03:57:21 +00001745 * Server certificate selection
1746 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001747 if (ssl->conf->f_cert_cb && (ret = ssl->conf->f_cert_cb(ssl)) != 0) {
1748 MBEDTLS_SSL_DEBUG_RET(1, "f_cert_cb", ret);
1749 return ret;
XiaokangQianfb665a82022-06-15 03:57:21 +00001750 }
1751#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
1752 ssl->handshake->sni_name = NULL;
1753 ssl->handshake->sni_name_len = 0;
1754#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
XiaokangQian7807f9f2022-02-15 10:04:37 +00001755
Gilles Peskine449bd832023-01-11 14:50:10 +01001756 ret = mbedtls_ssl_tls13_key_schedule_stage_early(ssl);
1757 if (ret != 0) {
1758 MBEDTLS_SSL_DEBUG_RET(1,
1759 "mbedtls_ssl_tls1_3_key_schedule_stage_early", ret);
1760 return ret;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001761 }
1762
Gilles Peskine449bd832023-01-11 14:50:10 +01001763 return 0;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001764
1765}
1766
1767/*
XiaokangQianed582dd2022-04-13 08:21:05 +00001768 * Main entry point from the state machine; orchestrates the otherfunctions.
1769 */
1770
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001771MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01001772static int ssl_tls13_process_client_hello(mbedtls_ssl_context *ssl)
XiaokangQianed582dd2022-04-13 08:21:05 +00001773{
1774
XiaokangQian08037552022-04-20 07:16:41 +00001775 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine449bd832023-01-11 14:50:10 +01001776 unsigned char *buf = NULL;
XiaokangQianed582dd2022-04-13 08:21:05 +00001777 size_t buflen = 0;
Jerry Yu4ca91402022-05-09 15:50:57 +08001778 int parse_client_hello_ret;
1779
Gilles Peskine449bd832023-01-11 14:50:10 +01001780 MBEDTLS_SSL_DEBUG_MSG(2, ("=> parse client hello"));
XiaokangQianed582dd2022-04-13 08:21:05 +00001781
Gilles Peskine449bd832023-01-11 14:50:10 +01001782 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_tls13_fetch_handshake_msg(
1783 ssl, MBEDTLS_SSL_HS_CLIENT_HELLO,
1784 &buf, &buflen));
XiaokangQianed582dd2022-04-13 08:21:05 +00001785
Gilles Peskine449bd832023-01-11 14:50:10 +01001786 MBEDTLS_SSL_PROC_CHK_NEG(ssl_tls13_parse_client_hello(ssl, buf,
1787 buf + buflen));
Ronald Cron5af4c7f2023-03-07 20:46:59 +01001788 parse_client_hello_ret = ret; /* Store positive return value of
1789 * parse_client_hello,
1790 * as negative error codes are handled
Jerry Yuf41553b2022-05-09 22:20:30 +08001791 * by MBEDTLS_SSL_PROC_CHK_NEG. */
Jerry Yu4ca91402022-05-09 15:50:57 +08001792
Ronald Cronb828c7d2023-04-03 16:37:22 +02001793 /*
1794 * Version 1.2 of the protocol has been chosen, set the
1795 * ssl->keep_current_message flag for the ClientHello to be kept and parsed
1796 * as a TLS 1.2 ClientHello. We also change ssl->tls_version to
1797 * MBEDTLS_SSL_VERSION_TLS1_2 thus from now on mbedtls_ssl_handshake_step()
1798 * will dispatch to the TLS 1.2 state machine.
1799 */
Ronald Cron5af4c7f2023-03-07 20:46:59 +01001800 if (SSL_CLIENT_HELLO_TLS1_2 == parse_client_hello_ret) {
1801 ssl->keep_current_message = 1;
1802 ssl->tls_version = MBEDTLS_SSL_VERSION_TLS1_2;
1803 return 0;
1804 }
1805
Gilles Peskine449bd832023-01-11 14:50:10 +01001806 MBEDTLS_SSL_PROC_CHK(ssl_tls13_postprocess_client_hello(ssl));
Jerry Yu582dd062022-04-22 21:59:01 +08001807
Ronald Cron5af4c7f2023-03-07 20:46:59 +01001808 if (SSL_CLIENT_HELLO_OK == parse_client_hello_ret) {
Gilles Peskine449bd832023-01-11 14:50:10 +01001809 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_SERVER_HELLO);
1810 } else {
1811 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_HELLO_RETRY_REQUEST);
1812 }
XiaokangQianed582dd2022-04-13 08:21:05 +00001813
1814cleanup:
1815
Gilles Peskine449bd832023-01-11 14:50:10 +01001816 MBEDTLS_SSL_DEBUG_MSG(2, ("<= parse client hello"));
1817 return ret;
XiaokangQianed582dd2022-04-13 08:21:05 +00001818}
1819
1820/*
Jerry Yu1c3e6882022-04-20 21:23:40 +08001821 * Handler for MBEDTLS_SSL_SERVER_HELLO
Jerry Yu5b64ae92022-03-30 17:15:02 +08001822 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001823MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01001824static int ssl_tls13_prepare_server_hello(mbedtls_ssl_context *ssl)
Jerry Yuf4b27e42022-03-30 17:32:21 +08001825{
Jerry Yu637a3f12022-04-20 21:37:58 +08001826 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1827 unsigned char *server_randbytes =
Gilles Peskine449bd832023-01-11 14:50:10 +01001828 ssl->handshake->randbytes + MBEDTLS_CLIENT_HELLO_RANDOM_LEN;
1829 if (ssl->conf->f_rng == NULL) {
1830 MBEDTLS_SSL_DEBUG_MSG(1, ("no RNG provided"));
1831 return MBEDTLS_ERR_SSL_NO_RNG;
Jerry Yuf4b27e42022-03-30 17:32:21 +08001832 }
1833
Gilles Peskine449bd832023-01-11 14:50:10 +01001834 if ((ret = ssl->conf->f_rng(ssl->conf->p_rng, server_randbytes,
1835 MBEDTLS_SERVER_HELLO_RANDOM_LEN)) != 0) {
1836 MBEDTLS_SSL_DEBUG_RET(1, "f_rng", ret);
1837 return ret;
Jerry Yuf4b27e42022-03-30 17:32:21 +08001838 }
1839
Gilles Peskine449bd832023-01-11 14:50:10 +01001840 MBEDTLS_SSL_DEBUG_BUF(3, "server hello, random bytes", server_randbytes,
1841 MBEDTLS_SERVER_HELLO_RANDOM_LEN);
Jerry Yuf4b27e42022-03-30 17:32:21 +08001842
1843#if defined(MBEDTLS_HAVE_TIME)
Gilles Peskine449bd832023-01-11 14:50:10 +01001844 ssl->session_negotiate->start = time(NULL);
Jerry Yuf4b27e42022-03-30 17:32:21 +08001845#endif /* MBEDTLS_HAVE_TIME */
1846
Gilles Peskine449bd832023-01-11 14:50:10 +01001847 return ret;
Jerry Yuf4b27e42022-03-30 17:32:21 +08001848}
1849
Jerry Yu3bf2c642022-03-30 22:02:12 +08001850/*
Jerry Yue74e04a2022-04-21 09:23:16 +08001851 * ssl_tls13_write_server_hello_supported_versions_ext ():
Jerry Yu3bf2c642022-03-30 22:02:12 +08001852 *
1853 * struct {
Jerry Yufb9f54d2022-04-06 10:08:34 +08001854 * ProtocolVersion selected_version;
Jerry Yu3bf2c642022-03-30 22:02:12 +08001855 * } SupportedVersions;
1856 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001857MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yue74e04a2022-04-21 09:23:16 +08001858static int ssl_tls13_write_server_hello_supported_versions_ext(
Gilles Peskine449bd832023-01-11 14:50:10 +01001859 mbedtls_ssl_context *ssl,
1860 unsigned char *buf,
1861 unsigned char *end,
1862 size_t *out_len)
Jerry Yu3bf2c642022-03-30 22:02:12 +08001863{
Jerry Yu3bf2c642022-03-30 22:02:12 +08001864 *out_len = 0;
1865
Gilles Peskine449bd832023-01-11 14:50:10 +01001866 MBEDTLS_SSL_DEBUG_MSG(3, ("server hello, write selected version"));
Jerry Yu3bf2c642022-03-30 22:02:12 +08001867
1868 /* Check if we have space to write the extension:
1869 * - extension_type (2 bytes)
1870 * - extension_data_length (2 bytes)
Jerry Yu349a6132022-04-14 20:52:56 +08001871 * - selected_version (2 bytes)
Jerry Yu3bf2c642022-03-30 22:02:12 +08001872 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001873 MBEDTLS_SSL_CHK_BUF_PTR(buf, end, 6);
Jerry Yu3bf2c642022-03-30 22:02:12 +08001874
Gilles Peskine449bd832023-01-11 14:50:10 +01001875 MBEDTLS_PUT_UINT16_BE(MBEDTLS_TLS_EXT_SUPPORTED_VERSIONS, buf, 0);
Jerry Yu3bf2c642022-03-30 22:02:12 +08001876
Gilles Peskine449bd832023-01-11 14:50:10 +01001877 MBEDTLS_PUT_UINT16_BE(2, buf, 2);
Jerry Yu3bf2c642022-03-30 22:02:12 +08001878
Gilles Peskine449bd832023-01-11 14:50:10 +01001879 mbedtls_ssl_write_version(buf + 4,
1880 ssl->conf->transport,
1881 ssl->tls_version);
Jerry Yu3bf2c642022-03-30 22:02:12 +08001882
Gilles Peskine449bd832023-01-11 14:50:10 +01001883 MBEDTLS_SSL_DEBUG_MSG(3, ("supported version: [%04x]",
1884 ssl->tls_version));
Jerry Yu3bf2c642022-03-30 22:02:12 +08001885
Jerry Yu349a6132022-04-14 20:52:56 +08001886 *out_len = 6;
Jerry Yu3bf2c642022-03-30 22:02:12 +08001887
Jerry Yub95dd362022-11-08 21:19:34 +08001888 mbedtls_ssl_tls13_set_hs_sent_ext_mask(
Gilles Peskine449bd832023-01-11 14:50:10 +01001889 ssl, MBEDTLS_TLS_EXT_SUPPORTED_VERSIONS);
Jerry Yub95dd362022-11-08 21:19:34 +08001890
Gilles Peskine449bd832023-01-11 14:50:10 +01001891 return 0;
Jerry Yu3bf2c642022-03-30 22:02:12 +08001892}
1893
Jerry Yud9436a12022-04-20 22:28:09 +08001894
Jerry Yu3bf2c642022-03-30 22:02:12 +08001895
1896/* Generate and export a single key share. For hybrid KEMs, this can
1897 * be called multiple times with the different components of the hybrid. */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001898MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01001899static int ssl_tls13_generate_and_write_key_share(mbedtls_ssl_context *ssl,
1900 uint16_t named_group,
1901 unsigned char *buf,
1902 unsigned char *end,
1903 size_t *out_len)
Jerry Yu3bf2c642022-03-30 22:02:12 +08001904{
1905 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yu955ddd72022-04-22 22:27:33 +08001906
1907 *out_len = 0;
1908
Valerio Setti080a22b2023-03-20 15:22:47 +01001909#if defined(PSA_WANT_ALG_ECDH)
Gilles Peskine449bd832023-01-11 14:50:10 +01001910 if (mbedtls_ssl_tls13_named_group_is_ecdhe(named_group)) {
Jerry Yu89e103c2022-03-30 22:43:29 +08001911 ret = mbedtls_ssl_tls13_generate_and_write_ecdh_key_exchange(
Gilles Peskine449bd832023-01-11 14:50:10 +01001912 ssl, named_group, buf, end, out_len);
1913 if (ret != 0) {
Jerry Yu89e103c2022-03-30 22:43:29 +08001914 MBEDTLS_SSL_DEBUG_RET(
1915 1, "mbedtls_ssl_tls13_generate_and_write_ecdh_key_exchange",
Gilles Peskine449bd832023-01-11 14:50:10 +01001916 ret);
1917 return ret;
Jerry Yu3bf2c642022-03-30 22:02:12 +08001918 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001919 } else
Valerio Setti080a22b2023-03-20 15:22:47 +01001920#endif /* PSA_WANT_ALG_ECDH */
Gilles Peskine449bd832023-01-11 14:50:10 +01001921 if (0 /* Other kinds of KEMs */) {
1922 } else {
Jerry Yu955ddd72022-04-22 22:27:33 +08001923 ((void) ssl);
1924 ((void) named_group);
1925 ((void) buf);
1926 ((void) end);
Jerry Yu3bf2c642022-03-30 22:02:12 +08001927 ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
1928 }
1929
Gilles Peskine449bd832023-01-11 14:50:10 +01001930 return ret;
Jerry Yu3bf2c642022-03-30 22:02:12 +08001931}
1932
1933/*
1934 * ssl_tls13_write_key_share_ext
1935 *
1936 * Structure of key_share extension in ServerHello:
1937 *
Jerry Yu1c3e6882022-04-20 21:23:40 +08001938 * struct {
1939 * NamedGroup group;
1940 * opaque key_exchange<1..2^16-1>;
1941 * } KeyShareEntry;
1942 * struct {
1943 * KeyShareEntry server_share;
1944 * } KeyShareServerHello;
Jerry Yu3bf2c642022-03-30 22:02:12 +08001945 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001946MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01001947static int ssl_tls13_write_key_share_ext(mbedtls_ssl_context *ssl,
1948 unsigned char *buf,
1949 unsigned char *end,
1950 size_t *out_len)
Jerry Yu3bf2c642022-03-30 22:02:12 +08001951{
Jerry Yu955ddd72022-04-22 22:27:33 +08001952 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yu3bf2c642022-03-30 22:02:12 +08001953 unsigned char *p = buf;
Jerry Yu57d48412022-04-20 21:50:42 +08001954 uint16_t group = ssl->handshake->offered_group_id;
Jerry Yu3bf2c642022-03-30 22:02:12 +08001955 unsigned char *server_share = buf + 4;
Jerry Yu3bf2c642022-03-30 22:02:12 +08001956 size_t key_exchange_length;
Jerry Yu3bf2c642022-03-30 22:02:12 +08001957
1958 *out_len = 0;
1959
Gilles Peskine449bd832023-01-11 14:50:10 +01001960 MBEDTLS_SSL_DEBUG_MSG(3, ("server hello, adding key share extension"));
Jerry Yu3bf2c642022-03-30 22:02:12 +08001961
Gilles Peskine449bd832023-01-11 14:50:10 +01001962 MBEDTLS_SSL_DEBUG_MSG(2, ("server hello, write selected_group: %s (%04x)",
1963 mbedtls_ssl_named_group_to_str(group),
1964 group));
Jerry Yu58af2332022-09-06 11:19:31 +08001965
Jerry Yu3bf2c642022-03-30 22:02:12 +08001966 /* Check if we have space for header and length fields:
1967 * - extension_type (2 bytes)
1968 * - extension_data_length (2 bytes)
1969 * - group (2 bytes)
1970 * - key_exchange_length (2 bytes)
1971 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001972 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 8);
1973 MBEDTLS_PUT_UINT16_BE(MBEDTLS_TLS_EXT_KEY_SHARE, p, 0);
1974 MBEDTLS_PUT_UINT16_BE(group, server_share, 0);
Jerry Yu3bf2c642022-03-30 22:02:12 +08001975 p += 8;
Jerry Yu57d48412022-04-20 21:50:42 +08001976
Jerry Yu3bf2c642022-03-30 22:02:12 +08001977 /* When we introduce PQC-ECDHE hybrids, we'll want to call this
1978 * function multiple times. */
Jerry Yu955ddd72022-04-22 22:27:33 +08001979 ret = ssl_tls13_generate_and_write_key_share(
Gilles Peskine449bd832023-01-11 14:50:10 +01001980 ssl, group, server_share + 4, end, &key_exchange_length);
1981 if (ret != 0) {
1982 return ret;
1983 }
Jerry Yu3bf2c642022-03-30 22:02:12 +08001984 p += key_exchange_length;
Jerry Yuc1be19f2022-04-23 16:11:39 +08001985
Gilles Peskine449bd832023-01-11 14:50:10 +01001986 MBEDTLS_PUT_UINT16_BE(key_exchange_length, server_share + 2, 0);
Jerry Yu3bf2c642022-03-30 22:02:12 +08001987
Gilles Peskine449bd832023-01-11 14:50:10 +01001988 MBEDTLS_PUT_UINT16_BE(p - server_share, buf, 2);
Jerry Yu3bf2c642022-03-30 22:02:12 +08001989
Jerry Yu57d48412022-04-20 21:50:42 +08001990 *out_len = p - buf;
Jerry Yu955ddd72022-04-22 22:27:33 +08001991
Gilles Peskine449bd832023-01-11 14:50:10 +01001992 mbedtls_ssl_tls13_set_hs_sent_ext_mask(ssl, MBEDTLS_TLS_EXT_KEY_SHARE);
Jerry Yub95dd362022-11-08 21:19:34 +08001993
Gilles Peskine449bd832023-01-11 14:50:10 +01001994 return 0;
Jerry Yu3bf2c642022-03-30 22:02:12 +08001995}
Jerry Yud9436a12022-04-20 22:28:09 +08001996
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001997MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01001998static int ssl_tls13_write_hrr_key_share_ext(mbedtls_ssl_context *ssl,
1999 unsigned char *buf,
2000 unsigned char *end,
2001 size_t *out_len)
Jerry Yu23f7a6f2022-04-23 15:16:45 +08002002{
2003 uint16_t selected_group = ssl->handshake->hrr_selected_group;
2004 /* key_share Extension
2005 *
2006 * struct {
2007 * select (Handshake.msg_type) {
2008 * ...
2009 * case hello_retry_request:
2010 * NamedGroup selected_group;
2011 * ...
2012 * };
2013 * } KeyShare;
2014 */
2015
2016 *out_len = 0;
2017
Jerry Yub0ac10b2022-05-05 11:10:08 +08002018 /*
2019 * For a pure PSK key exchange, there is no group to agree upon. The purpose
2020 * of the HRR is then to transmit a cookie to force the client to demonstrate
2021 * reachability at their apparent network address (primarily useful for DTLS).
2022 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002023 if (!mbedtls_ssl_tls13_key_exchange_mode_with_ephemeral(ssl)) {
2024 return 0;
2025 }
Jerry Yu23f7a6f2022-04-23 15:16:45 +08002026
2027 /* We should only send the key_share extension if the client's initial
2028 * key share was not acceptable. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002029 if (ssl->handshake->offered_group_id != 0) {
2030 MBEDTLS_SSL_DEBUG_MSG(4, ("Skip key_share extension in HRR"));
2031 return 0;
Jerry Yu23f7a6f2022-04-23 15:16:45 +08002032 }
2033
Gilles Peskine449bd832023-01-11 14:50:10 +01002034 if (selected_group == 0) {
2035 MBEDTLS_SSL_DEBUG_MSG(1, ("no matching named group found"));
2036 return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
Jerry Yu23f7a6f2022-04-23 15:16:45 +08002037 }
2038
Jerry Yub0ac10b2022-05-05 11:10:08 +08002039 /* Check if we have enough space:
2040 * - extension_type (2 bytes)
2041 * - extension_data_length (2 bytes)
2042 * - selected_group (2 bytes)
2043 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002044 MBEDTLS_SSL_CHK_BUF_PTR(buf, end, 6);
Jerry Yu23f7a6f2022-04-23 15:16:45 +08002045
Gilles Peskine449bd832023-01-11 14:50:10 +01002046 MBEDTLS_PUT_UINT16_BE(MBEDTLS_TLS_EXT_KEY_SHARE, buf, 0);
2047 MBEDTLS_PUT_UINT16_BE(2, buf, 2);
2048 MBEDTLS_PUT_UINT16_BE(selected_group, buf, 4);
Jerry Yu23f7a6f2022-04-23 15:16:45 +08002049
Gilles Peskine449bd832023-01-11 14:50:10 +01002050 MBEDTLS_SSL_DEBUG_MSG(3,
2051 ("HRR selected_group: %s (%x)",
2052 mbedtls_ssl_named_group_to_str(selected_group),
2053 selected_group));
Jerry Yu23f7a6f2022-04-23 15:16:45 +08002054
2055 *out_len = 6;
Jerry Yu23f7a6f2022-04-23 15:16:45 +08002056
Gilles Peskine449bd832023-01-11 14:50:10 +01002057 mbedtls_ssl_tls13_set_hs_sent_ext_mask(ssl, MBEDTLS_TLS_EXT_KEY_SHARE);
Jerry Yub95dd362022-11-08 21:19:34 +08002058
Gilles Peskine449bd832023-01-11 14:50:10 +01002059 return 0;
Jerry Yu23f7a6f2022-04-23 15:16:45 +08002060}
Jerry Yu3bf2c642022-03-30 22:02:12 +08002061
2062/*
2063 * Structure of ServerHello message:
2064 *
2065 * struct {
2066 * ProtocolVersion legacy_version = 0x0303; // TLS v1.2
2067 * Random random;
2068 * opaque legacy_session_id_echo<0..32>;
2069 * CipherSuite cipher_suite;
2070 * uint8 legacy_compression_method = 0;
2071 * Extension extensions<6..2^16-1>;
2072 * } ServerHello;
2073 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002074MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002075static int ssl_tls13_write_server_hello_body(mbedtls_ssl_context *ssl,
2076 unsigned char *buf,
2077 unsigned char *end,
2078 size_t *out_len,
2079 int is_hrr)
Jerry Yu56404d72022-03-30 17:36:13 +08002080{
Jerry Yu955ddd72022-04-22 22:27:33 +08002081 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yu3bf2c642022-03-30 22:02:12 +08002082 unsigned char *p = buf;
Jerry Yud9436a12022-04-20 22:28:09 +08002083 unsigned char *p_extensions_len;
Jerry Yufbe3e642022-04-25 19:31:51 +08002084 size_t output_len;
Jerry Yu3bf2c642022-03-30 22:02:12 +08002085
2086 *out_len = 0;
Jerry Yu50e00e32022-10-31 14:45:01 +08002087 ssl->handshake->sent_extensions = MBEDTLS_SSL_EXT_MASK_NONE;
Jerry Yu3bf2c642022-03-30 22:02:12 +08002088
Jerry Yucfc04b32022-04-21 09:31:58 +08002089 /* ...
2090 * ProtocolVersion legacy_version = 0x0303; // TLS 1.2
2091 * ...
2092 * with ProtocolVersion defined as:
2093 * uint16 ProtocolVersion;
Jerry Yu3bf2c642022-03-30 22:02:12 +08002094 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002095 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 2);
2096 MBEDTLS_PUT_UINT16_BE(0x0303, p, 0);
Jerry Yu3bf2c642022-03-30 22:02:12 +08002097 p += 2;
2098
Jerry Yu1c3e6882022-04-20 21:23:40 +08002099 /* ...
2100 * Random random;
2101 * ...
Jerry Yucfc04b32022-04-21 09:31:58 +08002102 * with Random defined as:
2103 * opaque Random[MBEDTLS_SERVER_HELLO_RANDOM_LEN];
Jerry Yu1c3e6882022-04-20 21:23:40 +08002104 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002105 MBEDTLS_SSL_CHK_BUF_PTR(p, end, MBEDTLS_SERVER_HELLO_RANDOM_LEN);
2106 if (is_hrr) {
2107 memcpy(p, mbedtls_ssl_tls13_hello_retry_request_magic,
2108 MBEDTLS_SERVER_HELLO_RANDOM_LEN);
2109 } else {
2110 memcpy(p, &ssl->handshake->randbytes[MBEDTLS_CLIENT_HELLO_RANDOM_LEN],
2111 MBEDTLS_SERVER_HELLO_RANDOM_LEN);
Jerry Yu23f7a6f2022-04-23 15:16:45 +08002112 }
Gilles Peskine449bd832023-01-11 14:50:10 +01002113 MBEDTLS_SSL_DEBUG_BUF(3, "server hello, random bytes",
2114 p, MBEDTLS_SERVER_HELLO_RANDOM_LEN);
Jerry Yu3bf2c642022-03-30 22:02:12 +08002115 p += MBEDTLS_SERVER_HELLO_RANDOM_LEN;
2116
Jerry Yucfc04b32022-04-21 09:31:58 +08002117 /* ...
2118 * opaque legacy_session_id_echo<0..32>;
2119 * ...
Jerry Yu3bf2c642022-03-30 22:02:12 +08002120 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002121 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 1 + ssl->session_negotiate->id_len);
2122 *p++ = (unsigned char) ssl->session_negotiate->id_len;
2123 if (ssl->session_negotiate->id_len > 0) {
2124 memcpy(p, &ssl->session_negotiate->id[0],
2125 ssl->session_negotiate->id_len);
Jerry Yu3bf2c642022-03-30 22:02:12 +08002126 p += ssl->session_negotiate->id_len;
Jerry Yu955ddd72022-04-22 22:27:33 +08002127
Gilles Peskine449bd832023-01-11 14:50:10 +01002128 MBEDTLS_SSL_DEBUG_BUF(3, "session id", ssl->session_negotiate->id,
2129 ssl->session_negotiate->id_len);
Jerry Yu3bf2c642022-03-30 22:02:12 +08002130 }
2131
Jerry Yucfc04b32022-04-21 09:31:58 +08002132 /* ...
2133 * CipherSuite cipher_suite;
2134 * ...
2135 * with CipherSuite defined as:
2136 * uint8 CipherSuite[2];
Jerry Yu3bf2c642022-03-30 22:02:12 +08002137 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002138 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 2);
2139 MBEDTLS_PUT_UINT16_BE(ssl->session_negotiate->ciphersuite, p, 0);
Jerry Yu3bf2c642022-03-30 22:02:12 +08002140 p += 2;
Gilles Peskine449bd832023-01-11 14:50:10 +01002141 MBEDTLS_SSL_DEBUG_MSG(3,
2142 ("server hello, chosen ciphersuite: %s ( id=%d )",
2143 mbedtls_ssl_get_ciphersuite_name(
2144 ssl->session_negotiate->ciphersuite),
2145 ssl->session_negotiate->ciphersuite));
Jerry Yu3bf2c642022-03-30 22:02:12 +08002146
Jerry Yucfc04b32022-04-21 09:31:58 +08002147 /* ...
2148 * uint8 legacy_compression_method = 0;
2149 * ...
2150 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002151 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 1);
Thomas Daubney31e03a82022-07-25 15:59:25 +01002152 *p++ = MBEDTLS_SSL_COMPRESS_NULL;
Jerry Yu3bf2c642022-03-30 22:02:12 +08002153
Jerry Yucfc04b32022-04-21 09:31:58 +08002154 /* ...
2155 * Extension extensions<6..2^16-1>;
2156 * ...
2157 * struct {
2158 * ExtensionType extension_type; (2 bytes)
2159 * opaque extension_data<0..2^16-1>;
2160 * } Extension;
2161 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002162 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 2);
Jerry Yud9436a12022-04-20 22:28:09 +08002163 p_extensions_len = p;
Jerry Yu3bf2c642022-03-30 22:02:12 +08002164 p += 2;
2165
Gilles Peskine449bd832023-01-11 14:50:10 +01002166 if ((ret = ssl_tls13_write_server_hello_supported_versions_ext(
2167 ssl, p, end, &output_len)) != 0) {
Jerry Yu955ddd72022-04-22 22:27:33 +08002168 MBEDTLS_SSL_DEBUG_RET(
Gilles Peskine449bd832023-01-11 14:50:10 +01002169 1, "ssl_tls13_write_server_hello_supported_versions_ext", ret);
2170 return ret;
Jerry Yu3bf2c642022-03-30 22:02:12 +08002171 }
2172 p += output_len;
2173
Gilles Peskine449bd832023-01-11 14:50:10 +01002174 if (mbedtls_ssl_tls13_key_exchange_mode_with_ephemeral(ssl)) {
2175 if (is_hrr) {
2176 ret = ssl_tls13_write_hrr_key_share_ext(ssl, p, end, &output_len);
2177 } else {
2178 ret = ssl_tls13_write_key_share_ext(ssl, p, end, &output_len);
2179 }
2180 if (ret != 0) {
2181 return ret;
2182 }
Jerry Yu3bf2c642022-03-30 22:02:12 +08002183 p += output_len;
2184 }
Jerry Yu3bf2c642022-03-30 22:02:12 +08002185
Ronald Cron41a443a2022-10-04 16:38:25 +02002186#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01002187 if (!is_hrr && mbedtls_ssl_tls13_key_exchange_mode_with_psk(ssl)) {
2188 ret = ssl_tls13_write_server_pre_shared_key_ext(ssl, p, end, &output_len);
2189 if (ret != 0) {
2190 MBEDTLS_SSL_DEBUG_RET(1, "ssl_tls13_write_server_pre_shared_key_ext",
2191 ret);
2192 return ret;
Jerry Yu032b15ce2022-07-11 06:10:03 +00002193 }
2194 p += output_len;
2195 }
Ronald Cron41a443a2022-10-04 16:38:25 +02002196#endif
Jerry Yu032b15ce2022-07-11 06:10:03 +00002197
Gilles Peskine449bd832023-01-11 14:50:10 +01002198 MBEDTLS_PUT_UINT16_BE(p - p_extensions_len - 2, p_extensions_len, 0);
Jerry Yu3bf2c642022-03-30 22:02:12 +08002199
Gilles Peskine449bd832023-01-11 14:50:10 +01002200 MBEDTLS_SSL_DEBUG_BUF(4, "server hello extensions",
2201 p_extensions_len, p - p_extensions_len);
Jerry Yu3bf2c642022-03-30 22:02:12 +08002202
Jerry Yud9436a12022-04-20 22:28:09 +08002203 *out_len = p - buf;
Jerry Yu3bf2c642022-03-30 22:02:12 +08002204
Gilles Peskine449bd832023-01-11 14:50:10 +01002205 MBEDTLS_SSL_DEBUG_BUF(3, "server hello", buf, *out_len);
Jerry Yu3bf2c642022-03-30 22:02:12 +08002206
Jerry Yu7de2ff02022-11-08 21:43:46 +08002207 MBEDTLS_SSL_PRINT_EXTS(
Jerry Yu4b8f2f72022-10-31 13:31:22 +08002208 3, is_hrr ? MBEDTLS_SSL_TLS1_3_HS_HELLO_RETRY_REQUEST :
Gilles Peskine449bd832023-01-11 14:50:10 +01002209 MBEDTLS_SSL_HS_SERVER_HELLO,
2210 ssl->handshake->sent_extensions);
Jerry Yu4b8f2f72022-10-31 13:31:22 +08002211
Gilles Peskine449bd832023-01-11 14:50:10 +01002212 return ret;
Jerry Yu56404d72022-03-30 17:36:13 +08002213}
2214
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002215MBEDTLS_CHECK_RETURN_CRITICAL
Xiaokang Qian934ce6f2023-02-06 10:23:04 +00002216static int ssl_tls13_finalize_server_hello(mbedtls_ssl_context *ssl)
Jerry Yue110d252022-05-05 10:19:22 +08002217{
2218 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine449bd832023-01-11 14:50:10 +01002219 ret = mbedtls_ssl_tls13_compute_handshake_transform(ssl);
2220 if (ret != 0) {
2221 MBEDTLS_SSL_DEBUG_RET(1,
2222 "mbedtls_ssl_tls13_compute_handshake_transform",
2223 ret);
2224 return ret;
Jerry Yue110d252022-05-05 10:19:22 +08002225 }
2226
Gilles Peskine449bd832023-01-11 14:50:10 +01002227 return ret;
Jerry Yue110d252022-05-05 10:19:22 +08002228}
2229
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002230MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002231static int ssl_tls13_write_server_hello(mbedtls_ssl_context *ssl)
Jerry Yu5b64ae92022-03-30 17:15:02 +08002232{
Jerry Yu637a3f12022-04-20 21:37:58 +08002233 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yuf4b27e42022-03-30 17:32:21 +08002234 unsigned char *buf;
2235 size_t buf_len, msg_len;
2236
Gilles Peskine449bd832023-01-11 14:50:10 +01002237 MBEDTLS_SSL_DEBUG_MSG(2, ("=> write server hello"));
Jerry Yuf4b27e42022-03-30 17:32:21 +08002238
Gilles Peskine449bd832023-01-11 14:50:10 +01002239 MBEDTLS_SSL_PROC_CHK(ssl_tls13_prepare_server_hello(ssl));
Jerry Yuf4b27e42022-03-30 17:32:21 +08002240
Gilles Peskine449bd832023-01-11 14:50:10 +01002241 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_start_handshake_msg(ssl,
2242 MBEDTLS_SSL_HS_SERVER_HELLO, &buf,
2243 &buf_len));
Jerry Yuf4b27e42022-03-30 17:32:21 +08002244
Gilles Peskine449bd832023-01-11 14:50:10 +01002245 MBEDTLS_SSL_PROC_CHK(ssl_tls13_write_server_hello_body(ssl, buf,
2246 buf + buf_len,
2247 &msg_len,
2248 0));
Jerry Yuf4b27e42022-03-30 17:32:21 +08002249
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +01002250 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_add_hs_msg_to_checksum(
Manuel Pégourié-Gonnard43cc1272023-02-06 11:48:19 +01002251 ssl, MBEDTLS_SSL_HS_SERVER_HELLO, buf, msg_len));
Jerry Yuf4b27e42022-03-30 17:32:21 +08002252
Gilles Peskine449bd832023-01-11 14:50:10 +01002253 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_finish_handshake_msg(
2254 ssl, buf_len, msg_len));
Jerry Yu637a3f12022-04-20 21:37:58 +08002255
Xiaokang Qian934ce6f2023-02-06 10:23:04 +00002256 MBEDTLS_SSL_PROC_CHK(ssl_tls13_finalize_server_hello(ssl));
Jerry Yue110d252022-05-05 10:19:22 +08002257
Gabor Mezei7b39bf12022-05-24 16:04:14 +02002258#if defined(MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE)
2259 /* The server sends a dummy change_cipher_spec record immediately
2260 * after its first handshake message. This may either be after
2261 * a ServerHello or a HelloRetryRequest.
2262 */
Gabor Mezei96ae9262022-06-28 11:45:18 +02002263 mbedtls_ssl_handshake_set_state(
Gilles Peskine449bd832023-01-11 14:50:10 +01002264 ssl, MBEDTLS_SSL_SERVER_CCS_AFTER_SERVER_HELLO);
Gabor Mezei7b39bf12022-05-24 16:04:14 +02002265#else
Gilles Peskine449bd832023-01-11 14:50:10 +01002266 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_ENCRYPTED_EXTENSIONS);
Gabor Mezei7b39bf12022-05-24 16:04:14 +02002267#endif /* MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE */
Jerry Yue110d252022-05-05 10:19:22 +08002268
Jerry Yuf4b27e42022-03-30 17:32:21 +08002269cleanup:
2270
Gilles Peskine449bd832023-01-11 14:50:10 +01002271 MBEDTLS_SSL_DEBUG_MSG(2, ("<= write server hello"));
2272 return ret;
Jerry Yu5b64ae92022-03-30 17:15:02 +08002273}
2274
Jerry Yu23d1a252022-05-12 18:08:59 +08002275
2276/*
2277 * Handler for MBEDTLS_SSL_HELLO_RETRY_REQUEST
2278 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002279MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002280static int ssl_tls13_prepare_hello_retry_request(mbedtls_ssl_context *ssl)
Jerry Yu23d1a252022-05-12 18:08:59 +08002281{
2282 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine449bd832023-01-11 14:50:10 +01002283 if (ssl->handshake->hello_retry_request_count > 0) {
2284 MBEDTLS_SSL_DEBUG_MSG(1, ("Too many HRRs"));
2285 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE,
2286 MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE);
2287 return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
Jerry Yu23d1a252022-05-12 18:08:59 +08002288 }
2289
2290 /*
2291 * Create stateless transcript hash for HRR
2292 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002293 MBEDTLS_SSL_DEBUG_MSG(4, ("Reset transcript for HRR"));
2294 ret = mbedtls_ssl_reset_transcript_for_hrr(ssl);
2295 if (ret != 0) {
2296 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_reset_transcript_for_hrr", ret);
2297 return ret;
Jerry Yu23d1a252022-05-12 18:08:59 +08002298 }
Gilles Peskine449bd832023-01-11 14:50:10 +01002299 mbedtls_ssl_session_reset_msg_layer(ssl, 0);
Jerry Yu23d1a252022-05-12 18:08:59 +08002300
Gilles Peskine449bd832023-01-11 14:50:10 +01002301 return 0;
Jerry Yu23d1a252022-05-12 18:08:59 +08002302}
2303
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002304MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002305static int ssl_tls13_write_hello_retry_request(mbedtls_ssl_context *ssl)
Jerry Yu23d1a252022-05-12 18:08:59 +08002306{
2307 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
2308 unsigned char *buf;
2309 size_t buf_len, msg_len;
2310
Gilles Peskine449bd832023-01-11 14:50:10 +01002311 MBEDTLS_SSL_DEBUG_MSG(2, ("=> write hello retry request"));
Jerry Yu23d1a252022-05-12 18:08:59 +08002312
Gilles Peskine449bd832023-01-11 14:50:10 +01002313 MBEDTLS_SSL_PROC_CHK(ssl_tls13_prepare_hello_retry_request(ssl));
Jerry Yu23d1a252022-05-12 18:08:59 +08002314
Gilles Peskine449bd832023-01-11 14:50:10 +01002315 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_start_handshake_msg(
2316 ssl, MBEDTLS_SSL_HS_SERVER_HELLO,
2317 &buf, &buf_len));
Jerry Yu23d1a252022-05-12 18:08:59 +08002318
Gilles Peskine449bd832023-01-11 14:50:10 +01002319 MBEDTLS_SSL_PROC_CHK(ssl_tls13_write_server_hello_body(ssl, buf,
2320 buf + buf_len,
2321 &msg_len,
2322 1));
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +01002323 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_add_hs_msg_to_checksum(
Manuel Pégourié-Gonnard43cc1272023-02-06 11:48:19 +01002324 ssl, MBEDTLS_SSL_HS_SERVER_HELLO, buf, msg_len));
Jerry Yu23d1a252022-05-12 18:08:59 +08002325
2326
Gilles Peskine449bd832023-01-11 14:50:10 +01002327 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_finish_handshake_msg(ssl, buf_len,
2328 msg_len));
Jerry Yu23d1a252022-05-12 18:08:59 +08002329
2330 ssl->handshake->hello_retry_request_count++;
2331
Gabor Mezei7b39bf12022-05-24 16:04:14 +02002332#if defined(MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE)
2333 /* The server sends a dummy change_cipher_spec record immediately
2334 * after its first handshake message. This may either be after
2335 * a ServerHello or a HelloRetryRequest.
2336 */
Gabor Mezei96ae9262022-06-28 11:45:18 +02002337 mbedtls_ssl_handshake_set_state(
Gilles Peskine449bd832023-01-11 14:50:10 +01002338 ssl, MBEDTLS_SSL_SERVER_CCS_AFTER_HELLO_RETRY_REQUEST);
Gabor Mezei7b39bf12022-05-24 16:04:14 +02002339#else
Gilles Peskine449bd832023-01-11 14:50:10 +01002340 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_CLIENT_HELLO);
Gabor Mezei7b39bf12022-05-24 16:04:14 +02002341#endif /* MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE */
Jerry Yu23d1a252022-05-12 18:08:59 +08002342
2343cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +01002344 MBEDTLS_SSL_DEBUG_MSG(2, ("<= write hello retry request"));
2345 return ret;
Jerry Yu23d1a252022-05-12 18:08:59 +08002346}
2347
Jerry Yu5b64ae92022-03-30 17:15:02 +08002348/*
Jerry Yu4d3841a2022-04-16 12:37:19 +08002349 * Handler for MBEDTLS_SSL_ENCRYPTED_EXTENSIONS
2350 */
Jerry Yu4d3841a2022-04-16 12:37:19 +08002351
2352/*
2353 * struct {
2354 * Extension extensions<0..2 ^ 16 - 1>;
2355 * } EncryptedExtensions;
2356 *
2357 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002358MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002359static int ssl_tls13_write_encrypted_extensions_body(mbedtls_ssl_context *ssl,
2360 unsigned char *buf,
2361 unsigned char *end,
2362 size_t *out_len)
Jerry Yu4d3841a2022-04-16 12:37:19 +08002363{
XiaokangQianacb39922022-06-17 10:18:48 +00002364 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yu4d3841a2022-04-16 12:37:19 +08002365 unsigned char *p = buf;
2366 size_t extensions_len = 0;
Jerry Yu8937eb42022-05-03 12:12:14 +08002367 unsigned char *p_extensions_len;
XiaokangQianacb39922022-06-17 10:18:48 +00002368 size_t output_len;
Jerry Yu9da5e5a2022-05-03 15:46:09 +08002369
Jerry Yu4d3841a2022-04-16 12:37:19 +08002370 *out_len = 0;
2371
Gilles Peskine449bd832023-01-11 14:50:10 +01002372 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 2);
Jerry Yu8937eb42022-05-03 12:12:14 +08002373 p_extensions_len = p;
Jerry Yu4d3841a2022-04-16 12:37:19 +08002374 p += 2;
2375
Jerry Yu8937eb42022-05-03 12:12:14 +08002376 ((void) ssl);
XiaokangQianacb39922022-06-17 10:18:48 +00002377 ((void) ret);
2378 ((void) output_len);
2379
2380#if defined(MBEDTLS_SSL_ALPN)
Gilles Peskine449bd832023-01-11 14:50:10 +01002381 ret = mbedtls_ssl_write_alpn_ext(ssl, p, end, &output_len);
2382 if (ret != 0) {
2383 return ret;
2384 }
XiaokangQian95d5f542022-06-24 02:29:26 +00002385 p += output_len;
XiaokangQianacb39922022-06-17 10:18:48 +00002386#endif /* MBEDTLS_SSL_ALPN */
Jerry Yu4d3841a2022-04-16 12:37:19 +08002387
Gilles Peskine449bd832023-01-11 14:50:10 +01002388 extensions_len = (p - p_extensions_len) - 2;
2389 MBEDTLS_PUT_UINT16_BE(extensions_len, p_extensions_len, 0);
Jerry Yu8937eb42022-05-03 12:12:14 +08002390
2391 *out_len = p - buf;
Jerry Yu4d3841a2022-04-16 12:37:19 +08002392
Gilles Peskine449bd832023-01-11 14:50:10 +01002393 MBEDTLS_SSL_DEBUG_BUF(4, "encrypted extensions", buf, *out_len);
Jerry Yu4d3841a2022-04-16 12:37:19 +08002394
Jerry Yu7de2ff02022-11-08 21:43:46 +08002395 MBEDTLS_SSL_PRINT_EXTS(
Gilles Peskine449bd832023-01-11 14:50:10 +01002396 3, MBEDTLS_SSL_HS_ENCRYPTED_EXTENSIONS, ssl->handshake->sent_extensions);
Jerry Yu4b8f2f72022-10-31 13:31:22 +08002397
Gilles Peskine449bd832023-01-11 14:50:10 +01002398 return 0;
Jerry Yu4d3841a2022-04-16 12:37:19 +08002399}
2400
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002401MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002402static int ssl_tls13_write_encrypted_extensions(mbedtls_ssl_context *ssl)
Jerry Yu4d3841a2022-04-16 12:37:19 +08002403{
2404 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
2405 unsigned char *buf;
Jerry Yu39730a72022-05-03 12:14:04 +08002406 size_t buf_len, msg_len;
Jerry Yu4d3841a2022-04-16 12:37:19 +08002407
Gilles Peskine449bd832023-01-11 14:50:10 +01002408 mbedtls_ssl_set_outbound_transform(ssl,
2409 ssl->handshake->transform_handshake);
Gabor Mezei54719122022-06-28 11:34:56 +02002410 MBEDTLS_SSL_DEBUG_MSG(
Gilles Peskine449bd832023-01-11 14:50:10 +01002411 3, ("switching to handshake transform for outbound data"));
Gabor Mezei54719122022-06-28 11:34:56 +02002412
Gilles Peskine449bd832023-01-11 14:50:10 +01002413 MBEDTLS_SSL_DEBUG_MSG(2, ("=> write encrypted extensions"));
Jerry Yu4d3841a2022-04-16 12:37:19 +08002414
Gilles Peskine449bd832023-01-11 14:50:10 +01002415 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_start_handshake_msg(ssl,
2416 MBEDTLS_SSL_HS_ENCRYPTED_EXTENSIONS, &buf,
2417 &buf_len));
Jerry Yu4d3841a2022-04-16 12:37:19 +08002418
Gilles Peskine449bd832023-01-11 14:50:10 +01002419 MBEDTLS_SSL_PROC_CHK(ssl_tls13_write_encrypted_extensions_body(
2420 ssl, buf, buf + buf_len, &msg_len));
Jerry Yu4d3841a2022-04-16 12:37:19 +08002421
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +01002422 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_add_hs_msg_to_checksum(
Manuel Pégourié-Gonnard43cc1272023-02-06 11:48:19 +01002423 ssl, MBEDTLS_SSL_HS_ENCRYPTED_EXTENSIONS, buf, msg_len));
Jerry Yu4d3841a2022-04-16 12:37:19 +08002424
Gilles Peskine449bd832023-01-11 14:50:10 +01002425 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_finish_handshake_msg(
2426 ssl, buf_len, msg_len));
Jerry Yu4d3841a2022-04-16 12:37:19 +08002427
Ronald Cron928cbd32022-10-04 16:14:26 +02002428#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01002429 if (mbedtls_ssl_tls13_key_exchange_mode_with_psk(ssl)) {
2430 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_SERVER_FINISHED);
2431 } else {
2432 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_CERTIFICATE_REQUEST);
2433 }
Jerry Yu8937eb42022-05-03 12:12:14 +08002434#else
Gilles Peskine449bd832023-01-11 14:50:10 +01002435 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_SERVER_FINISHED);
Jerry Yu8937eb42022-05-03 12:12:14 +08002436#endif
2437
Jerry Yu4d3841a2022-04-16 12:37:19 +08002438cleanup:
2439
Gilles Peskine449bd832023-01-11 14:50:10 +01002440 MBEDTLS_SSL_DEBUG_MSG(2, ("<= write encrypted extensions"));
2441 return ret;
Jerry Yu4d3841a2022-04-16 12:37:19 +08002442}
2443
Ronald Cron928cbd32022-10-04 16:14:26 +02002444#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED)
XiaokangQiana987e1d2022-05-07 01:25:58 +00002445#define SSL_CERTIFICATE_REQUEST_SEND_REQUEST 0
2446#define SSL_CERTIFICATE_REQUEST_SKIP 1
2447/* Coordination:
2448 * Check whether a CertificateRequest message should be written.
2449 * Returns a negative code on failure, or
2450 * - SSL_CERTIFICATE_REQUEST_SEND_REQUEST
2451 * - SSL_CERTIFICATE_REQUEST_SKIP
2452 * indicating if the writing of the CertificateRequest
2453 * should be skipped or not.
2454 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002455MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002456static int ssl_tls13_certificate_request_coordinate(mbedtls_ssl_context *ssl)
XiaokangQiana987e1d2022-05-07 01:25:58 +00002457{
2458 int authmode;
2459
XiaokangQian40a35232022-05-07 09:02:40 +00002460#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Gilles Peskine449bd832023-01-11 14:50:10 +01002461 if (ssl->handshake->sni_authmode != MBEDTLS_SSL_VERIFY_UNSET) {
XiaokangQian40a35232022-05-07 09:02:40 +00002462 authmode = ssl->handshake->sni_authmode;
Gilles Peskine449bd832023-01-11 14:50:10 +01002463 } else
XiaokangQian40a35232022-05-07 09:02:40 +00002464#endif
XiaokangQiana987e1d2022-05-07 01:25:58 +00002465 authmode = ssl->conf->authmode;
2466
Gilles Peskine449bd832023-01-11 14:50:10 +01002467 if (authmode == MBEDTLS_SSL_VERIFY_NONE) {
Ronald Croneac00ad2022-09-13 10:16:31 +02002468 ssl->session_negotiate->verify_result = MBEDTLS_X509_BADCERT_SKIP_VERIFY;
Gilles Peskine449bd832023-01-11 14:50:10 +01002469 return SSL_CERTIFICATE_REQUEST_SKIP;
Ronald Croneac00ad2022-09-13 10:16:31 +02002470 }
XiaokangQiana987e1d2022-05-07 01:25:58 +00002471
XiaokangQianc3017f62022-05-13 05:55:41 +00002472 ssl->handshake->certificate_request_sent = 1;
XiaokangQian189ded22022-05-10 08:12:17 +00002473
Gilles Peskine449bd832023-01-11 14:50:10 +01002474 return SSL_CERTIFICATE_REQUEST_SEND_REQUEST;
XiaokangQiana987e1d2022-05-07 01:25:58 +00002475}
2476
XiaokangQiancec9ae62022-05-06 07:28:50 +00002477/*
2478 * struct {
2479 * opaque certificate_request_context<0..2^8-1>;
2480 * Extension extensions<2..2^16-1>;
2481 * } CertificateRequest;
2482 *
2483 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002484MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002485static int ssl_tls13_write_certificate_request_body(mbedtls_ssl_context *ssl,
2486 unsigned char *buf,
2487 const unsigned char *end,
2488 size_t *out_len)
XiaokangQiancec9ae62022-05-06 07:28:50 +00002489{
2490 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
2491 unsigned char *p = buf;
XiaokangQianec6efb92022-05-06 09:53:10 +00002492 size_t output_len = 0;
XiaokangQiancec9ae62022-05-06 07:28:50 +00002493 unsigned char *p_extensions_len;
2494
2495 *out_len = 0;
2496
2497 /* Check if we have enough space:
2498 * - certificate_request_context (1 byte)
2499 * - extensions length (2 bytes)
2500 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002501 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 3);
XiaokangQiancec9ae62022-05-06 07:28:50 +00002502
2503 /*
2504 * Write certificate_request_context
2505 */
2506 /*
2507 * We use a zero length context for the normal handshake
2508 * messages. For post-authentication handshake messages
2509 * this request context would be set to a non-zero value.
2510 */
2511 *p++ = 0x0;
2512
2513 /*
2514 * Write extensions
2515 */
2516 /* The extensions must contain the signature_algorithms. */
2517 p_extensions_len = p;
2518 p += 2;
Gilles Peskine449bd832023-01-11 14:50:10 +01002519 ret = mbedtls_ssl_write_sig_alg_ext(ssl, p, end, &output_len);
2520 if (ret != 0) {
2521 return ret;
2522 }
XiaokangQiancec9ae62022-05-06 07:28:50 +00002523
XiaokangQianec6efb92022-05-06 09:53:10 +00002524 p += output_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01002525 MBEDTLS_PUT_UINT16_BE(p - p_extensions_len - 2, p_extensions_len, 0);
XiaokangQiancec9ae62022-05-06 07:28:50 +00002526
2527 *out_len = p - buf;
2528
Jerry Yu7de2ff02022-11-08 21:43:46 +08002529 MBEDTLS_SSL_PRINT_EXTS(
Gilles Peskine449bd832023-01-11 14:50:10 +01002530 3, MBEDTLS_SSL_HS_CERTIFICATE_REQUEST, ssl->handshake->sent_extensions);
Jerry Yu4b8f2f72022-10-31 13:31:22 +08002531
Gilles Peskine449bd832023-01-11 14:50:10 +01002532 return 0;
XiaokangQiancec9ae62022-05-06 07:28:50 +00002533}
2534
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002535MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002536static int ssl_tls13_write_certificate_request(mbedtls_ssl_context *ssl)
XiaokangQiancec9ae62022-05-06 07:28:50 +00002537{
2538 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
2539
Gilles Peskine449bd832023-01-11 14:50:10 +01002540 MBEDTLS_SSL_DEBUG_MSG(2, ("=> write certificate request"));
XiaokangQiancec9ae62022-05-06 07:28:50 +00002541
Gilles Peskine449bd832023-01-11 14:50:10 +01002542 MBEDTLS_SSL_PROC_CHK_NEG(ssl_tls13_certificate_request_coordinate(ssl));
XiaokangQiancec9ae62022-05-06 07:28:50 +00002543
Gilles Peskine449bd832023-01-11 14:50:10 +01002544 if (ret == SSL_CERTIFICATE_REQUEST_SEND_REQUEST) {
XiaokangQiancec9ae62022-05-06 07:28:50 +00002545 unsigned char *buf;
2546 size_t buf_len, msg_len;
2547
Gilles Peskine449bd832023-01-11 14:50:10 +01002548 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_start_handshake_msg(ssl,
2549 MBEDTLS_SSL_HS_CERTIFICATE_REQUEST,
2550 &buf, &buf_len));
XiaokangQiancec9ae62022-05-06 07:28:50 +00002551
Gilles Peskine449bd832023-01-11 14:50:10 +01002552 MBEDTLS_SSL_PROC_CHK(ssl_tls13_write_certificate_request_body(
2553 ssl, buf, buf + buf_len, &msg_len));
XiaokangQiancec9ae62022-05-06 07:28:50 +00002554
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +01002555 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_add_hs_msg_to_checksum(
Manuel Pégourié-Gonnard43cc1272023-02-06 11:48:19 +01002556 ssl, MBEDTLS_SSL_HS_CERTIFICATE_REQUEST, buf, msg_len));
XiaokangQiancec9ae62022-05-06 07:28:50 +00002557
Gilles Peskine449bd832023-01-11 14:50:10 +01002558 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_finish_handshake_msg(
2559 ssl, buf_len, msg_len));
2560 } else if (ret == SSL_CERTIFICATE_REQUEST_SKIP) {
2561 MBEDTLS_SSL_DEBUG_MSG(2, ("<= skip write certificate request"));
XiaokangQiancec9ae62022-05-06 07:28:50 +00002562 ret = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01002563 } else {
2564 MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
XiaokangQiancec9ae62022-05-06 07:28:50 +00002565 ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
2566 goto cleanup;
2567 }
2568
Gilles Peskine449bd832023-01-11 14:50:10 +01002569 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_SERVER_CERTIFICATE);
XiaokangQiancec9ae62022-05-06 07:28:50 +00002570cleanup:
2571
Gilles Peskine449bd832023-01-11 14:50:10 +01002572 MBEDTLS_SSL_DEBUG_MSG(2, ("<= write certificate request"));
2573 return ret;
XiaokangQiancec9ae62022-05-06 07:28:50 +00002574}
XiaokangQiancec9ae62022-05-06 07:28:50 +00002575
Jerry Yu4d3841a2022-04-16 12:37:19 +08002576/*
Jerry Yuc6e6dbf2022-04-16 19:42:57 +08002577 * Handler for MBEDTLS_SSL_SERVER_CERTIFICATE
Jerry Yu83da34e2022-04-16 13:59:52 +08002578 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002579MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002580static int ssl_tls13_write_server_certificate(mbedtls_ssl_context *ssl)
Jerry Yu83da34e2022-04-16 13:59:52 +08002581{
Jerry Yu5a26f302022-05-10 20:46:40 +08002582 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
XiaokangQianfb665a82022-06-15 03:57:21 +00002583
2584#if defined(MBEDTLS_X509_CRT_PARSE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01002585 if ((ssl_tls13_pick_key_cert(ssl) != 0) ||
2586 mbedtls_ssl_own_cert(ssl) == NULL) {
2587 MBEDTLS_SSL_DEBUG_MSG(2, ("No certificate available."));
2588 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE,
2589 MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE);
2590 return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
Jerry Yu5a26f302022-05-10 20:46:40 +08002591 }
XiaokangQianfb665a82022-06-15 03:57:21 +00002592#endif /* MBEDTLS_X509_CRT_PARSE_C */
Jerry Yu5a26f302022-05-10 20:46:40 +08002593
Gilles Peskine449bd832023-01-11 14:50:10 +01002594 ret = mbedtls_ssl_tls13_write_certificate(ssl);
2595 if (ret != 0) {
2596 return ret;
2597 }
2598 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_CERTIFICATE_VERIFY);
2599 return 0;
Jerry Yu83da34e2022-04-16 13:59:52 +08002600}
2601
2602/*
Jerry Yuc6e6dbf2022-04-16 19:42:57 +08002603 * Handler for MBEDTLS_SSL_CERTIFICATE_VERIFY
Jerry Yu83da34e2022-04-16 13:59:52 +08002604 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002605MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002606static int ssl_tls13_write_certificate_verify(mbedtls_ssl_context *ssl)
Jerry Yu83da34e2022-04-16 13:59:52 +08002607{
Gilles Peskine449bd832023-01-11 14:50:10 +01002608 int ret = mbedtls_ssl_tls13_write_certificate_verify(ssl);
2609 if (ret != 0) {
2610 return ret;
2611 }
2612 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_SERVER_FINISHED);
2613 return 0;
Jerry Yu83da34e2022-04-16 13:59:52 +08002614}
Ronald Cron928cbd32022-10-04 16:14:26 +02002615#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED */
Jerry Yu83da34e2022-04-16 13:59:52 +08002616
2617/*
Jerry Yud6e253d2022-05-18 13:59:24 +08002618 * Handler for MBEDTLS_SSL_SERVER_FINISHED
Jerry Yu69dd8d42022-04-16 12:51:26 +08002619 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002620MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002621static int ssl_tls13_write_server_finished(mbedtls_ssl_context *ssl)
Jerry Yu69dd8d42022-04-16 12:51:26 +08002622{
Jerry Yud6e253d2022-05-18 13:59:24 +08002623 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yu27bdc7c2022-04-16 13:33:27 +08002624
Gilles Peskine449bd832023-01-11 14:50:10 +01002625 ret = mbedtls_ssl_tls13_write_finished_message(ssl);
2626 if (ret != 0) {
2627 return ret;
2628 }
Jerry Yu27bdc7c2022-04-16 13:33:27 +08002629
Gilles Peskine449bd832023-01-11 14:50:10 +01002630 ret = mbedtls_ssl_tls13_compute_application_transform(ssl);
2631 if (ret != 0) {
Jerry Yue3d67cb2022-05-19 15:33:10 +08002632 MBEDTLS_SSL_PEND_FATAL_ALERT(
Gilles Peskine449bd832023-01-11 14:50:10 +01002633 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE,
2634 MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE);
2635 return ret;
Jerry Yue3d67cb2022-05-19 15:33:10 +08002636 }
XiaokangQianc3017f62022-05-13 05:55:41 +00002637
Gilles Peskine449bd832023-01-11 14:50:10 +01002638 MBEDTLS_SSL_DEBUG_MSG(1, ("Switch to handshake keys for inbound traffic"));
2639 mbedtls_ssl_set_inbound_transform(ssl, ssl->handshake->transform_handshake);
XiaokangQian189ded22022-05-10 08:12:17 +00002640
Gilles Peskine449bd832023-01-11 14:50:10 +01002641 if (ssl->handshake->certificate_request_sent) {
2642 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_CLIENT_CERTIFICATE);
2643 } else {
2644 MBEDTLS_SSL_DEBUG_MSG(2, ("skip parse certificate"));
2645 MBEDTLS_SSL_DEBUG_MSG(2, ("skip parse certificate verify"));
2646 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_CLIENT_FINISHED);
Ronald Cron19385882022-06-15 16:26:13 +02002647 }
Ronald Cron63dc4632022-05-31 14:41:53 +02002648
Gilles Peskine449bd832023-01-11 14:50:10 +01002649 return 0;
Jerry Yu69dd8d42022-04-16 12:51:26 +08002650}
2651
2652/*
Jerry Yud6e253d2022-05-18 13:59:24 +08002653 * Handler for MBEDTLS_SSL_CLIENT_FINISHED
Jerry Yu69dd8d42022-04-16 12:51:26 +08002654 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002655MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002656static int ssl_tls13_process_client_finished(mbedtls_ssl_context *ssl)
Jerry Yu69dd8d42022-04-16 12:51:26 +08002657{
Jerry Yud6e253d2022-05-18 13:59:24 +08002658 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
2659
Gilles Peskine449bd832023-01-11 14:50:10 +01002660 ret = mbedtls_ssl_tls13_process_finished_message(ssl);
2661 if (ret != 0) {
2662 return ret;
Jerry Yue3d67cb2022-05-19 15:33:10 +08002663 }
2664
Gilles Peskine449bd832023-01-11 14:50:10 +01002665 ret = mbedtls_ssl_tls13_compute_resumption_master_secret(ssl);
2666 if (ret != 0) {
2667 MBEDTLS_SSL_DEBUG_RET(1,
2668 "mbedtls_ssl_tls13_compute_resumption_master_secret", ret);
2669 }
2670
2671 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_HANDSHAKE_WRAPUP);
2672 return 0;
Jerry Yu69dd8d42022-04-16 12:51:26 +08002673}
2674
2675/*
Jerry Yud6e253d2022-05-18 13:59:24 +08002676 * Handler for MBEDTLS_SSL_HANDSHAKE_WRAPUP
Jerry Yu69dd8d42022-04-16 12:51:26 +08002677 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002678MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002679static int ssl_tls13_handshake_wrapup(mbedtls_ssl_context *ssl)
Jerry Yu69dd8d42022-04-16 12:51:26 +08002680{
Gilles Peskine449bd832023-01-11 14:50:10 +01002681 MBEDTLS_SSL_DEBUG_MSG(2, ("handshake: done"));
Jerry Yu03ed50b2022-04-16 17:13:30 +08002682
Gilles Peskine449bd832023-01-11 14:50:10 +01002683 mbedtls_ssl_tls13_handshake_wrapup(ssl);
Jerry Yue67bef42022-07-07 07:29:42 +00002684
Pengyu Lv3643fdb2023-01-12 16:46:28 +08002685#if defined(MBEDTLS_SSL_SESSION_TICKETS) && \
2686 defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED)
Pengyu Lva1aa31b2022-12-13 13:49:59 +08002687/* TODO: Remove the check of SOME_PSK_ENABLED since SESSION_TICKETS requires
2688 * SOME_PSK_ENABLED to be enabled. Here is just to make CI happy. It is
2689 * expected to be resolved with issue#6395.
2690 */
Pengyu Lvc7af2c42022-12-01 16:33:00 +08002691 /* Sent NewSessionTicket message only when client supports PSK */
Pengyu Lv3643fdb2023-01-12 16:46:28 +08002692 if (mbedtls_ssl_tls13_some_psk_enabled(ssl)) {
2693 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_TLS1_3_NEW_SESSION_TICKET);
Pengyu Lvc7af2c42022-12-01 16:33:00 +08002694 } else
Jerry Yufca4d572022-07-21 10:37:48 +08002695#endif
Pengyu Lv3643fdb2023-01-12 16:46:28 +08002696 {
2697 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_HANDSHAKE_OVER);
2698 }
Gilles Peskine449bd832023-01-11 14:50:10 +01002699 return 0;
Jerry Yu69dd8d42022-04-16 12:51:26 +08002700}
2701
2702/*
Jerry Yua8d3c502022-10-30 14:51:23 +08002703 * Handler for MBEDTLS_SSL_TLS1_3_NEW_SESSION_TICKET
Jerry Yue67bef42022-07-07 07:29:42 +00002704 */
2705#define SSL_NEW_SESSION_TICKET_SKIP 0
2706#define SSL_NEW_SESSION_TICKET_WRITE 1
2707MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002708static int ssl_tls13_write_new_session_ticket_coordinate(mbedtls_ssl_context *ssl)
Jerry Yue67bef42022-07-07 07:29:42 +00002709{
2710 /* Check whether the use of session tickets is enabled */
Gilles Peskine449bd832023-01-11 14:50:10 +01002711 if (ssl->conf->f_ticket_write == NULL) {
2712 MBEDTLS_SSL_DEBUG_MSG(2, ("NewSessionTicket: disabled,"
2713 " callback is not set"));
2714 return SSL_NEW_SESSION_TICKET_SKIP;
Jerry Yud0766ec2022-09-22 10:46:57 +08002715 }
Gilles Peskine449bd832023-01-11 14:50:10 +01002716 if (ssl->conf->new_session_tickets_count == 0) {
2717 MBEDTLS_SSL_DEBUG_MSG(2, ("NewSessionTicket: disabled,"
2718 " configured count is zero"));
2719 return SSL_NEW_SESSION_TICKET_SKIP;
Jerry Yud0766ec2022-09-22 10:46:57 +08002720 }
2721
Gilles Peskine449bd832023-01-11 14:50:10 +01002722 if (ssl->handshake->new_session_tickets_count == 0) {
2723 MBEDTLS_SSL_DEBUG_MSG(2, ("NewSessionTicket: all tickets have "
2724 "been sent."));
2725 return SSL_NEW_SESSION_TICKET_SKIP;
Jerry Yue67bef42022-07-07 07:29:42 +00002726 }
2727
Gilles Peskine449bd832023-01-11 14:50:10 +01002728 return SSL_NEW_SESSION_TICKET_WRITE;
Jerry Yue67bef42022-07-07 07:29:42 +00002729}
2730
2731#if defined(MBEDTLS_SSL_SESSION_TICKETS)
2732MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002733static int ssl_tls13_prepare_new_session_ticket(mbedtls_ssl_context *ssl,
2734 unsigned char *ticket_nonce,
2735 size_t ticket_nonce_size)
Jerry Yue67bef42022-07-07 07:29:42 +00002736{
2737 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
2738 mbedtls_ssl_session *session = ssl->session;
2739 mbedtls_ssl_ciphersuite_t *ciphersuite_info;
2740 psa_algorithm_t psa_hash_alg;
2741 int hash_length;
2742
Gilles Peskine449bd832023-01-11 14:50:10 +01002743 MBEDTLS_SSL_DEBUG_MSG(2, ("=> prepare NewSessionTicket msg"));
Jerry Yue67bef42022-07-07 07:29:42 +00002744
2745#if defined(MBEDTLS_HAVE_TIME)
Gilles Peskine449bd832023-01-11 14:50:10 +01002746 session->start = mbedtls_time(NULL);
Jerry Yue67bef42022-07-07 07:29:42 +00002747#endif
2748
Pengyu Lv9f926952022-11-17 15:22:33 +08002749 /* Set ticket_flags depends on the advertised psk key exchange mode */
Pengyu Lv80270b22023-01-12 11:54:04 +08002750 mbedtls_ssl_session_clear_ticket_flags(
Pengyu Lv9eacb442022-12-09 14:39:19 +08002751 session, MBEDTLS_SSL_TLS1_3_TICKET_FLAGS_MASK);
Pengyu Lve6487fe2022-12-06 09:30:29 +08002752#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED)
Pengyu Lv80270b22023-01-12 11:54:04 +08002753 mbedtls_ssl_session_set_ticket_flags(
Pengyu Lv9eacb442022-12-09 14:39:19 +08002754 session, ssl->handshake->tls13_kex_modes);
Pengyu Lve6487fe2022-12-06 09:30:29 +08002755#endif
Pengyu Lv4938a562023-01-16 11:28:49 +08002756 MBEDTLS_SSL_PRINT_TICKET_FLAGS(4, session->ticket_flags);
Pengyu Lv9f926952022-11-17 15:22:33 +08002757
Jerry Yue67bef42022-07-07 07:29:42 +00002758 /* Generate ticket_age_add */
Gilles Peskine449bd832023-01-11 14:50:10 +01002759 if ((ret = ssl->conf->f_rng(ssl->conf->p_rng,
2760 (unsigned char *) &session->ticket_age_add,
2761 sizeof(session->ticket_age_add)) != 0)) {
2762 MBEDTLS_SSL_DEBUG_RET(1, "generate_ticket_age_add", ret);
2763 return ret;
Jerry Yue67bef42022-07-07 07:29:42 +00002764 }
Gilles Peskine449bd832023-01-11 14:50:10 +01002765 MBEDTLS_SSL_DEBUG_MSG(3, ("ticket_age_add: %u",
2766 (unsigned int) session->ticket_age_add));
Jerry Yue67bef42022-07-07 07:29:42 +00002767
2768 /* Generate ticket_nonce */
Gilles Peskine449bd832023-01-11 14:50:10 +01002769 ret = ssl->conf->f_rng(ssl->conf->p_rng, ticket_nonce, ticket_nonce_size);
2770 if (ret != 0) {
2771 MBEDTLS_SSL_DEBUG_RET(1, "generate_ticket_nonce", ret);
2772 return ret;
Jerry Yue67bef42022-07-07 07:29:42 +00002773 }
Gilles Peskine449bd832023-01-11 14:50:10 +01002774 MBEDTLS_SSL_DEBUG_BUF(3, "ticket_nonce:",
2775 ticket_nonce, ticket_nonce_size);
Jerry Yue67bef42022-07-07 07:29:42 +00002776
2777 ciphersuite_info =
Gilles Peskine449bd832023-01-11 14:50:10 +01002778 (mbedtls_ssl_ciphersuite_t *) ssl->handshake->ciphersuite_info;
2779 psa_hash_alg = mbedtls_psa_translate_md(ciphersuite_info->mac);
2780 hash_length = PSA_HASH_LENGTH(psa_hash_alg);
2781 if (hash_length == -1 ||
2782 (size_t) hash_length > sizeof(session->resumption_key)) {
2783 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Jerry Yufca4d572022-07-21 10:37:48 +08002784 }
2785
Jerry Yue67bef42022-07-07 07:29:42 +00002786 /* In this code the psk key length equals the length of the hash */
2787 session->resumption_key_len = hash_length;
2788 session->ciphersuite = ciphersuite_info->id;
2789
2790 /* Compute resumption key
2791 *
2792 * HKDF-Expand-Label( resumption_master_secret,
2793 * "resumption", ticket_nonce, Hash.length )
2794 */
2795 ret = mbedtls_ssl_tls13_hkdf_expand_label(
Gilles Peskine449bd832023-01-11 14:50:10 +01002796 psa_hash_alg,
2797 session->app_secrets.resumption_master_secret,
2798 hash_length,
2799 MBEDTLS_SSL_TLS1_3_LBL_WITH_LEN(resumption),
2800 ticket_nonce,
2801 ticket_nonce_size,
2802 session->resumption_key,
2803 hash_length);
Jerry Yue67bef42022-07-07 07:29:42 +00002804
Gilles Peskine449bd832023-01-11 14:50:10 +01002805 if (ret != 0) {
2806 MBEDTLS_SSL_DEBUG_RET(2,
2807 "Creating the ticket-resumed PSK failed",
2808 ret);
2809 return ret;
Jerry Yue67bef42022-07-07 07:29:42 +00002810 }
Gilles Peskine449bd832023-01-11 14:50:10 +01002811 MBEDTLS_SSL_DEBUG_BUF(3, "Ticket-resumed PSK",
2812 session->resumption_key,
2813 session->resumption_key_len);
Jerry Yue67bef42022-07-07 07:29:42 +00002814
Gilles Peskine449bd832023-01-11 14:50:10 +01002815 MBEDTLS_SSL_DEBUG_BUF(3, "resumption_master_secret",
2816 session->app_secrets.resumption_master_secret,
2817 hash_length);
Jerry Yue67bef42022-07-07 07:29:42 +00002818
Gilles Peskine449bd832023-01-11 14:50:10 +01002819 return 0;
Jerry Yue67bef42022-07-07 07:29:42 +00002820}
2821
2822/* This function creates a NewSessionTicket message in the following format:
2823 *
2824 * struct {
2825 * uint32 ticket_lifetime;
2826 * uint32 ticket_age_add;
2827 * opaque ticket_nonce<0..255>;
2828 * opaque ticket<1..2^16-1>;
2829 * Extension extensions<0..2^16-2>;
2830 * } NewSessionTicket;
2831 *
2832 * The ticket inside the NewSessionTicket message is an encrypted container
2833 * carrying the necessary information so that the server is later able to
2834 * re-start the communication.
2835 *
2836 * The following fields are placed inside the ticket by the
2837 * f_ticket_write() function:
2838 *
2839 * - creation time (start)
2840 * - flags (flags)
2841 * - age add (ticket_age_add)
2842 * - key (key)
2843 * - key length (key_len)
2844 * - ciphersuite (ciphersuite)
2845 */
2846MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002847static int ssl_tls13_write_new_session_ticket_body(mbedtls_ssl_context *ssl,
2848 unsigned char *buf,
2849 unsigned char *end,
2850 size_t *out_len,
2851 unsigned char *ticket_nonce,
2852 size_t ticket_nonce_size)
Jerry Yue67bef42022-07-07 07:29:42 +00002853{
2854 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
2855 unsigned char *p = buf;
2856 mbedtls_ssl_session *session = ssl->session;
2857 size_t ticket_len;
2858 uint32_t ticket_lifetime;
2859
2860 *out_len = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01002861 MBEDTLS_SSL_DEBUG_MSG(2, ("=> write NewSessionTicket msg"));
Jerry Yue67bef42022-07-07 07:29:42 +00002862
2863 /*
2864 * ticket_lifetime 4 bytes
2865 * ticket_age_add 4 bytes
2866 * ticket_nonce 1 + ticket_nonce_size bytes
2867 * ticket >=2 bytes
2868 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002869 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 4 + 4 + 1 + ticket_nonce_size + 2);
Jerry Yue67bef42022-07-07 07:29:42 +00002870
2871 /* Generate ticket and ticket_lifetime */
Gilles Peskine449bd832023-01-11 14:50:10 +01002872 ret = ssl->conf->f_ticket_write(ssl->conf->p_ticket,
2873 session,
2874 p + 9 + ticket_nonce_size + 2,
2875 end,
2876 &ticket_len,
2877 &ticket_lifetime);
2878 if (ret != 0) {
2879 MBEDTLS_SSL_DEBUG_RET(1, "write_ticket", ret);
2880 return ret;
Jerry Yue67bef42022-07-07 07:29:42 +00002881 }
2882 /* RFC 8446 4.6.1
2883 * ticket_lifetime: Indicates the lifetime in seconds as a 32-bit
2884 * unsigned integer in network byte order from the time of ticket
2885 * issuance. Servers MUST NOT use any value greater than
2886 * 604800 seconds (7 days). The value of zero indicates that the
2887 * ticket should be discarded immediately. Clients MUST NOT cache
2888 * tickets for longer than 7 days, regardless of the ticket_lifetime,
2889 * and MAY delete tickets earlier based on local policy. A server
2890 * MAY treat a ticket as valid for a shorter period of time than what
2891 * is stated in the ticket_lifetime.
2892 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002893 if (ticket_lifetime > 604800) {
Xiaokang Qian28af5012022-10-13 08:18:19 +00002894 ticket_lifetime = 604800;
Gilles Peskine449bd832023-01-11 14:50:10 +01002895 }
2896 MBEDTLS_PUT_UINT32_BE(ticket_lifetime, p, 0);
2897 MBEDTLS_SSL_DEBUG_MSG(3, ("ticket_lifetime: %u",
2898 (unsigned int) ticket_lifetime));
Jerry Yue67bef42022-07-07 07:29:42 +00002899
2900 /* Write ticket_age_add */
Gilles Peskine449bd832023-01-11 14:50:10 +01002901 MBEDTLS_PUT_UINT32_BE(session->ticket_age_add, p, 4);
2902 MBEDTLS_SSL_DEBUG_MSG(3, ("ticket_age_add: %u",
2903 (unsigned int) session->ticket_age_add));
Jerry Yue67bef42022-07-07 07:29:42 +00002904
2905 /* Write ticket_nonce */
Gilles Peskine449bd832023-01-11 14:50:10 +01002906 p[8] = (unsigned char) ticket_nonce_size;
2907 if (ticket_nonce_size > 0) {
2908 memcpy(p + 9, ticket_nonce, ticket_nonce_size);
Jerry Yue67bef42022-07-07 07:29:42 +00002909 }
2910 p += 9 + ticket_nonce_size;
2911
2912 /* Write ticket */
Gilles Peskine449bd832023-01-11 14:50:10 +01002913 MBEDTLS_PUT_UINT16_BE(ticket_len, p, 0);
Jerry Yue67bef42022-07-07 07:29:42 +00002914 p += 2;
Gilles Peskine449bd832023-01-11 14:50:10 +01002915 MBEDTLS_SSL_DEBUG_BUF(4, "ticket", p, ticket_len);
Jerry Yue67bef42022-07-07 07:29:42 +00002916 p += ticket_len;
2917
2918 /* Ticket Extensions
2919 *
2920 * Note: We currently don't have any extensions.
2921 * Set length to zero.
2922 */
Jerry Yuedab6372022-10-31 14:37:31 +08002923 ssl->handshake->sent_extensions = MBEDTLS_SSL_EXT_MASK_NONE;
2924
Gilles Peskine449bd832023-01-11 14:50:10 +01002925 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 2);
2926 MBEDTLS_PUT_UINT16_BE(0, p, 0);
Jerry Yue67bef42022-07-07 07:29:42 +00002927 p += 2;
2928
2929 *out_len = p - buf;
Gilles Peskine449bd832023-01-11 14:50:10 +01002930 MBEDTLS_SSL_DEBUG_BUF(4, "ticket", buf, *out_len);
2931 MBEDTLS_SSL_DEBUG_MSG(2, ("<= write new session ticket"));
Jerry Yue67bef42022-07-07 07:29:42 +00002932
Jerry Yu7de2ff02022-11-08 21:43:46 +08002933 MBEDTLS_SSL_PRINT_EXTS(
Gilles Peskine449bd832023-01-11 14:50:10 +01002934 3, MBEDTLS_SSL_HS_NEW_SESSION_TICKET, ssl->handshake->sent_extensions);
Jerry Yu4b8f2f72022-10-31 13:31:22 +08002935
Gilles Peskine449bd832023-01-11 14:50:10 +01002936 return 0;
Jerry Yue67bef42022-07-07 07:29:42 +00002937}
2938
2939/*
Jerry Yua8d3c502022-10-30 14:51:23 +08002940 * Handler for MBEDTLS_SSL_TLS1_3_NEW_SESSION_TICKET
Jerry Yue67bef42022-07-07 07:29:42 +00002941 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002942static int ssl_tls13_write_new_session_ticket(mbedtls_ssl_context *ssl)
Jerry Yue67bef42022-07-07 07:29:42 +00002943{
2944 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
2945
Gilles Peskine449bd832023-01-11 14:50:10 +01002946 MBEDTLS_SSL_PROC_CHK_NEG(ssl_tls13_write_new_session_ticket_coordinate(ssl));
Jerry Yue67bef42022-07-07 07:29:42 +00002947
Gilles Peskine449bd832023-01-11 14:50:10 +01002948 if (ret == SSL_NEW_SESSION_TICKET_WRITE) {
Jerry Yue67bef42022-07-07 07:29:42 +00002949 unsigned char ticket_nonce[MBEDTLS_SSL_TLS1_3_TICKET_NONCE_LENGTH];
2950 unsigned char *buf;
2951 size_t buf_len, msg_len;
2952
Gilles Peskine449bd832023-01-11 14:50:10 +01002953 MBEDTLS_SSL_PROC_CHK(ssl_tls13_prepare_new_session_ticket(
2954 ssl, ticket_nonce, sizeof(ticket_nonce)));
Jerry Yue67bef42022-07-07 07:29:42 +00002955
Gilles Peskine449bd832023-01-11 14:50:10 +01002956 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_start_handshake_msg(ssl,
2957 MBEDTLS_SSL_HS_NEW_SESSION_TICKET,
2958 &buf, &buf_len));
Jerry Yue67bef42022-07-07 07:29:42 +00002959
Gilles Peskine449bd832023-01-11 14:50:10 +01002960 MBEDTLS_SSL_PROC_CHK(ssl_tls13_write_new_session_ticket_body(
2961 ssl, buf, buf + buf_len, &msg_len,
2962 ticket_nonce, sizeof(ticket_nonce)));
Jerry Yue67bef42022-07-07 07:29:42 +00002963
Gilles Peskine449bd832023-01-11 14:50:10 +01002964 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_finish_handshake_msg(
2965 ssl, buf_len, msg_len));
Jerry Yufca4d572022-07-21 10:37:48 +08002966
Jerry Yu359e65f2022-09-22 23:47:43 +08002967 /* Limit session tickets count to one when resumption connection.
2968 *
2969 * See document of mbedtls_ssl_conf_new_session_tickets.
2970 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002971 if (ssl->handshake->resume == 1) {
Jerry Yu359e65f2022-09-22 23:47:43 +08002972 ssl->handshake->new_session_tickets_count = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01002973 } else {
Jerry Yu359e65f2022-09-22 23:47:43 +08002974 ssl->handshake->new_session_tickets_count--;
Gilles Peskine449bd832023-01-11 14:50:10 +01002975 }
Jerry Yub7e3fa72022-09-22 11:07:18 +08002976
Jerry Yua8d3c502022-10-30 14:51:23 +08002977 mbedtls_ssl_handshake_set_state(
Gilles Peskine449bd832023-01-11 14:50:10 +01002978 ssl, MBEDTLS_SSL_TLS1_3_NEW_SESSION_TICKET_FLUSH);
2979 } else {
2980 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_HANDSHAKE_OVER);
Jerry Yue67bef42022-07-07 07:29:42 +00002981 }
2982
Jerry Yue67bef42022-07-07 07:29:42 +00002983cleanup:
2984
Gilles Peskine449bd832023-01-11 14:50:10 +01002985 return ret;
Jerry Yue67bef42022-07-07 07:29:42 +00002986}
2987#endif /* MBEDTLS_SSL_SESSION_TICKETS */
2988
2989/*
XiaokangQiane8ff3502022-04-22 02:34:40 +00002990 * TLS 1.3 State Machine -- server side
XiaokangQian7807f9f2022-02-15 10:04:37 +00002991 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002992int mbedtls_ssl_tls13_handshake_server_step(mbedtls_ssl_context *ssl)
Jerry Yub9930e72021-08-06 17:11:51 +08002993{
XiaokangQian08037552022-04-20 07:16:41 +00002994 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
XiaokangQian7807f9f2022-02-15 10:04:37 +00002995
Gilles Peskine449bd832023-01-11 14:50:10 +01002996 if (ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER || ssl->handshake == NULL) {
2997 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
2998 }
XiaokangQian7807f9f2022-02-15 10:04:37 +00002999
Gilles Peskine449bd832023-01-11 14:50:10 +01003000 MBEDTLS_SSL_DEBUG_MSG(2, ("tls13 server state: %s(%d)",
3001 mbedtls_ssl_states_str(ssl->state),
3002 ssl->state));
Jerry Yu6e81b272021-09-27 11:16:17 +08003003
Gilles Peskine449bd832023-01-11 14:50:10 +01003004 switch (ssl->state) {
XiaokangQian7807f9f2022-02-15 10:04:37 +00003005 /* start state */
3006 case MBEDTLS_SSL_HELLO_REQUEST:
Gilles Peskine449bd832023-01-11 14:50:10 +01003007 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_CLIENT_HELLO);
XiaokangQian08037552022-04-20 07:16:41 +00003008 ret = 0;
XiaokangQian7807f9f2022-02-15 10:04:37 +00003009 break;
3010
XiaokangQian7807f9f2022-02-15 10:04:37 +00003011 case MBEDTLS_SSL_CLIENT_HELLO:
Gilles Peskine449bd832023-01-11 14:50:10 +01003012 ret = ssl_tls13_process_client_hello(ssl);
3013 if (ret != 0) {
3014 MBEDTLS_SSL_DEBUG_RET(1, "ssl_tls13_process_client_hello", ret);
3015 }
Jerry Yuf41553b2022-05-09 22:20:30 +08003016 break;
XiaokangQian7807f9f2022-02-15 10:04:37 +00003017
Jerry Yuf41553b2022-05-09 22:20:30 +08003018 case MBEDTLS_SSL_HELLO_RETRY_REQUEST:
Gilles Peskine449bd832023-01-11 14:50:10 +01003019 ret = ssl_tls13_write_hello_retry_request(ssl);
3020 if (ret != 0) {
3021 MBEDTLS_SSL_DEBUG_RET(1, "ssl_tls13_write_hello_retry_request", ret);
3022 return ret;
Jerry Yuf41553b2022-05-09 22:20:30 +08003023 }
XiaokangQian7807f9f2022-02-15 10:04:37 +00003024 break;
3025
Jerry Yu5b64ae92022-03-30 17:15:02 +08003026 case MBEDTLS_SSL_SERVER_HELLO:
Gilles Peskine449bd832023-01-11 14:50:10 +01003027 ret = ssl_tls13_write_server_hello(ssl);
Jerry Yu5b64ae92022-03-30 17:15:02 +08003028 break;
3029
Xiaofei Baicba64af2022-02-15 10:00:56 +00003030 case MBEDTLS_SSL_ENCRYPTED_EXTENSIONS:
Gilles Peskine449bd832023-01-11 14:50:10 +01003031 ret = ssl_tls13_write_encrypted_extensions(ssl);
3032 if (ret != 0) {
3033 MBEDTLS_SSL_DEBUG_RET(1, "ssl_tls13_write_encrypted_extensions", ret);
3034 return ret;
Xiaofei Baicba64af2022-02-15 10:00:56 +00003035 }
Jerry Yu48330562022-05-06 21:35:44 +08003036 break;
Jerry Yu6a2cd9e2022-05-05 11:14:19 +08003037
Ronald Cron928cbd32022-10-04 16:14:26 +02003038#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED)
Xiaofei Bai9ca09d42022-02-14 12:57:18 +00003039 case MBEDTLS_SSL_CERTIFICATE_REQUEST:
Gilles Peskine449bd832023-01-11 14:50:10 +01003040 ret = ssl_tls13_write_certificate_request(ssl);
Xiaofei Bai9ca09d42022-02-14 12:57:18 +00003041 break;
Xiaofei Bai9ca09d42022-02-14 12:57:18 +00003042
Jerry Yu83da34e2022-04-16 13:59:52 +08003043 case MBEDTLS_SSL_SERVER_CERTIFICATE:
Gilles Peskine449bd832023-01-11 14:50:10 +01003044 ret = ssl_tls13_write_server_certificate(ssl);
Jerry Yu83da34e2022-04-16 13:59:52 +08003045 break;
3046
3047 case MBEDTLS_SSL_CERTIFICATE_VERIFY:
Gilles Peskine449bd832023-01-11 14:50:10 +01003048 ret = ssl_tls13_write_certificate_verify(ssl);
Jerry Yu83da34e2022-04-16 13:59:52 +08003049 break;
Ronald Cron928cbd32022-10-04 16:14:26 +02003050#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED */
Jerry Yu83da34e2022-04-16 13:59:52 +08003051
Gilles Peskine449bd832023-01-11 14:50:10 +01003052 /*
3053 * Injection of dummy-CCS's for middlebox compatibility
3054 */
Gabor Mezei7b39bf12022-05-24 16:04:14 +02003055#if defined(MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE)
Gabor Mezeif7044ea2022-06-28 16:01:49 +02003056 case MBEDTLS_SSL_SERVER_CCS_AFTER_HELLO_RETRY_REQUEST:
Gilles Peskine449bd832023-01-11 14:50:10 +01003057 ret = mbedtls_ssl_tls13_write_change_cipher_spec(ssl);
3058 if (ret == 0) {
3059 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_CLIENT_HELLO);
3060 }
Gabor Mezei7b39bf12022-05-24 16:04:14 +02003061 break;
3062
3063 case MBEDTLS_SSL_SERVER_CCS_AFTER_SERVER_HELLO:
Gilles Peskine449bd832023-01-11 14:50:10 +01003064 ret = mbedtls_ssl_tls13_write_change_cipher_spec(ssl);
3065 if (ret == 0) {
3066 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_ENCRYPTED_EXTENSIONS);
3067 }
Gabor Mezei7b39bf12022-05-24 16:04:14 +02003068 break;
3069#endif /* MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE */
3070
Jerry Yu69dd8d42022-04-16 12:51:26 +08003071 case MBEDTLS_SSL_SERVER_FINISHED:
Gilles Peskine449bd832023-01-11 14:50:10 +01003072 ret = ssl_tls13_write_server_finished(ssl);
Jerry Yu69dd8d42022-04-16 12:51:26 +08003073 break;
3074
3075 case MBEDTLS_SSL_CLIENT_FINISHED:
Gilles Peskine449bd832023-01-11 14:50:10 +01003076 ret = ssl_tls13_process_client_finished(ssl);
Jerry Yu69dd8d42022-04-16 12:51:26 +08003077 break;
3078
Jerry Yu69dd8d42022-04-16 12:51:26 +08003079 case MBEDTLS_SSL_HANDSHAKE_WRAPUP:
Gilles Peskine449bd832023-01-11 14:50:10 +01003080 ret = ssl_tls13_handshake_wrapup(ssl);
Jerry Yu69dd8d42022-04-16 12:51:26 +08003081 break;
3082
Ronald Cron766c0cd2022-10-18 12:17:11 +02003083#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED)
XiaokangQian6b916b12022-04-25 07:29:34 +00003084 case MBEDTLS_SSL_CLIENT_CERTIFICATE:
Gilles Peskine449bd832023-01-11 14:50:10 +01003085 ret = mbedtls_ssl_tls13_process_certificate(ssl);
3086 if (ret == 0) {
3087 if (ssl->session_negotiate->peer_cert != NULL) {
Ronald Cron209cae92022-06-07 10:30:19 +02003088 mbedtls_ssl_handshake_set_state(
Gilles Peskine449bd832023-01-11 14:50:10 +01003089 ssl, MBEDTLS_SSL_CLIENT_CERTIFICATE_VERIFY);
3090 } else {
3091 MBEDTLS_SSL_DEBUG_MSG(2, ("skip parse certificate verify"));
Ronald Cron209cae92022-06-07 10:30:19 +02003092 mbedtls_ssl_handshake_set_state(
Gilles Peskine449bd832023-01-11 14:50:10 +01003093 ssl, MBEDTLS_SSL_CLIENT_FINISHED);
Ronald Cron19385882022-06-15 16:26:13 +02003094 }
XiaokangQian6b916b12022-04-25 07:29:34 +00003095 }
3096 break;
3097
3098 case MBEDTLS_SSL_CLIENT_CERTIFICATE_VERIFY:
Gilles Peskine449bd832023-01-11 14:50:10 +01003099 ret = mbedtls_ssl_tls13_process_certificate_verify(ssl);
3100 if (ret == 0) {
XiaokangQian6b916b12022-04-25 07:29:34 +00003101 mbedtls_ssl_handshake_set_state(
Gilles Peskine449bd832023-01-11 14:50:10 +01003102 ssl, MBEDTLS_SSL_CLIENT_FINISHED);
XiaokangQian6b916b12022-04-25 07:29:34 +00003103 }
3104 break;
Ronald Cron766c0cd2022-10-18 12:17:11 +02003105#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED */
XiaokangQian6b916b12022-04-25 07:29:34 +00003106
Jerry Yue67bef42022-07-07 07:29:42 +00003107#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Jerry Yua8d3c502022-10-30 14:51:23 +08003108 case MBEDTLS_SSL_TLS1_3_NEW_SESSION_TICKET:
Gilles Peskine449bd832023-01-11 14:50:10 +01003109 ret = ssl_tls13_write_new_session_ticket(ssl);
3110 if (ret != 0) {
3111 MBEDTLS_SSL_DEBUG_RET(1,
3112 "ssl_tls13_write_new_session_ticket ",
3113 ret);
Jerry Yue67bef42022-07-07 07:29:42 +00003114 }
3115 break;
Jerry Yua8d3c502022-10-30 14:51:23 +08003116 case MBEDTLS_SSL_TLS1_3_NEW_SESSION_TICKET_FLUSH:
Jerry Yue67bef42022-07-07 07:29:42 +00003117 /* This state is necessary to do the flush of the New Session
Jerry Yua8d3c502022-10-30 14:51:23 +08003118 * Ticket message written in MBEDTLS_SSL_TLS1_3_NEW_SESSION_TICKET
Jerry Yue67bef42022-07-07 07:29:42 +00003119 * as part of ssl_prepare_handshake_step.
3120 */
3121 ret = 0;
Jerry Yud4e75002022-08-09 13:33:50 +08003122
Gilles Peskine449bd832023-01-11 14:50:10 +01003123 if (ssl->handshake->new_session_tickets_count == 0) {
3124 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_HANDSHAKE_OVER);
3125 } else {
3126 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_TLS1_3_NEW_SESSION_TICKET);
3127 }
Jerry Yue67bef42022-07-07 07:29:42 +00003128 break;
3129
3130#endif /* MBEDTLS_SSL_SESSION_TICKETS */
3131
XiaokangQian7807f9f2022-02-15 10:04:37 +00003132 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01003133 MBEDTLS_SSL_DEBUG_MSG(1, ("invalid state %d", ssl->state));
3134 return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
XiaokangQian7807f9f2022-02-15 10:04:37 +00003135 }
3136
Gilles Peskine449bd832023-01-11 14:50:10 +01003137 return ret;
Jerry Yub9930e72021-08-06 17:11:51 +08003138}
Jerry Yu3cc4c2a2021-08-06 16:29:08 +08003139
Jerry Yufb4b6472022-01-27 15:03:26 +08003140#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_PROTO_TLS1_3 */