blob: 128c46039162bcfd1e9ebec7a466b9e9bcdbfeda [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;
XiaokangQian8f9dfe42022-04-15 02:52:39 +0000741 int tls13_supported = 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
754 /* In this implementation we only support TLS 1.3 and DTLS 1.3. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100755 if (tls_version == MBEDTLS_SSL_VERSION_TLS1_3) {
XiaokangQian7807f9f2022-02-15 10:04:37 +0000756 tls13_supported = 1;
757 break;
758 }
XiaokangQian7807f9f2022-02-15 10:04:37 +0000759 }
760
Gilles Peskine449bd832023-01-11 14:50:10 +0100761 if (!tls13_supported) {
762 MBEDTLS_SSL_DEBUG_MSG(1, ("TLS 1.3 is not supported by the client"));
XiaokangQian7807f9f2022-02-15 10:04:37 +0000763
Gilles Peskine449bd832023-01-11 14:50:10 +0100764 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_PROTOCOL_VERSION,
765 MBEDTLS_ERR_SSL_BAD_PROTOCOL_VERSION);
766 return MBEDTLS_ERR_SSL_BAD_PROTOCOL_VERSION;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000767 }
768
Gilles Peskine449bd832023-01-11 14:50:10 +0100769 MBEDTLS_SSL_DEBUG_MSG(1, ("Negotiated version. Supported is [%04x]",
770 (unsigned int) tls_version));
XiaokangQian7807f9f2022-02-15 10:04:37 +0000771
Gilles Peskine449bd832023-01-11 14:50:10 +0100772 return 0;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000773}
774
Valerio Setti080a22b2023-03-20 15:22:47 +0100775#if defined(PSA_WANT_ALG_ECDH)
XiaokangQiane8ff3502022-04-22 02:34:40 +0000776/*
XiaokangQian7807f9f2022-02-15 10:04:37 +0000777 *
778 * From RFC 8446:
779 * enum {
780 * ... (0xFFFF)
781 * } NamedGroup;
782 * struct {
783 * NamedGroup named_group_list<2..2^16-1>;
784 * } NamedGroupList;
785 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200786MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100787static int ssl_tls13_parse_supported_groups_ext(mbedtls_ssl_context *ssl,
788 const unsigned char *buf,
789 const unsigned char *end)
XiaokangQian7807f9f2022-02-15 10:04:37 +0000790{
XiaokangQian7807f9f2022-02-15 10:04:37 +0000791 const unsigned char *p = buf;
XiaokangQian84823772022-04-19 07:57:30 +0000792 size_t named_group_list_len;
XiaokangQian8f9dfe42022-04-15 02:52:39 +0000793 const unsigned char *named_group_list_end;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000794
Gilles Peskine449bd832023-01-11 14:50:10 +0100795 MBEDTLS_SSL_DEBUG_BUF(3, "supported_groups extension", p, end - buf);
796 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, 2);
797 named_group_list_len = MBEDTLS_GET_UINT16_BE(p, 0);
XiaokangQian7807f9f2022-02-15 10:04:37 +0000798 p += 2;
Gilles Peskine449bd832023-01-11 14:50:10 +0100799 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, named_group_list_len);
XiaokangQian8f9dfe42022-04-15 02:52:39 +0000800 named_group_list_end = p + named_group_list_len;
XiaokangQian4e8cd7b2022-04-21 09:48:09 +0000801 ssl->handshake->hrr_selected_group = 0;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000802
Gilles Peskine449bd832023-01-11 14:50:10 +0100803 while (p < named_group_list_end) {
XiaokangQian08037552022-04-20 07:16:41 +0000804 uint16_t named_group;
Gilles Peskine449bd832023-01-11 14:50:10 +0100805 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, named_group_list_end, 2);
806 named_group = MBEDTLS_GET_UINT16_BE(p, 0);
XiaokangQian08037552022-04-20 07:16:41 +0000807 p += 2;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000808
Gilles Peskine449bd832023-01-11 14:50:10 +0100809 MBEDTLS_SSL_DEBUG_MSG(2,
810 ("got named group: %s(%04x)",
811 mbedtls_ssl_named_group_to_str(named_group),
812 named_group));
XiaokangQian08037552022-04-20 07:16:41 +0000813
Gilles Peskine449bd832023-01-11 14:50:10 +0100814 if (!mbedtls_ssl_named_group_is_offered(ssl, named_group) ||
815 !mbedtls_ssl_named_group_is_supported(named_group) ||
816 ssl->handshake->hrr_selected_group != 0) {
XiaokangQian08037552022-04-20 07:16:41 +0000817 continue;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000818 }
819
Gilles Peskine449bd832023-01-11 14:50:10 +0100820 MBEDTLS_SSL_DEBUG_MSG(2,
821 ("add named group %s(%04x) into received list.",
822 mbedtls_ssl_named_group_to_str(named_group),
823 named_group));
Jerry Yuc1be19f2022-04-23 16:11:39 +0800824
XiaokangQian4e8cd7b2022-04-21 09:48:09 +0000825 ssl->handshake->hrr_selected_group = named_group;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000826 }
827
Gilles Peskine449bd832023-01-11 14:50:10 +0100828 return 0;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000829
830}
Valerio Setti080a22b2023-03-20 15:22:47 +0100831#endif /* PSA_WANT_ALG_ECDH */
XiaokangQian7807f9f2022-02-15 10:04:37 +0000832
XiaokangQian08037552022-04-20 07:16:41 +0000833#define SSL_TLS1_3_PARSE_KEY_SHARES_EXT_NO_MATCH 1
834
Valerio Setti080a22b2023-03-20 15:22:47 +0100835#if defined(PSA_WANT_ALG_ECDH)
XiaokangQian7807f9f2022-02-15 10:04:37 +0000836/*
837 * ssl_tls13_parse_key_shares_ext() verifies whether the information in the
XiaokangQiane8ff3502022-04-22 02:34:40 +0000838 * extension is correct and stores the first acceptable key share and its associated group.
XiaokangQian7807f9f2022-02-15 10:04:37 +0000839 *
840 * Possible return values are:
841 * - 0: Successful processing of the client provided key share extension.
XiaokangQian08037552022-04-20 07:16:41 +0000842 * - SSL_TLS1_3_PARSE_KEY_SHARES_EXT_NO_MATCH: The key shares provided by the client
XiaokangQian7807f9f2022-02-15 10:04:37 +0000843 * does not match a group supported by the server. A HelloRetryRequest will
844 * be needed.
XiaokangQiane8ff3502022-04-22 02:34:40 +0000845 * - A negative value for fatal errors.
Jerry Yuc1be19f2022-04-23 16:11:39 +0800846 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200847MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100848static int ssl_tls13_parse_key_shares_ext(mbedtls_ssl_context *ssl,
849 const unsigned char *buf,
850 const unsigned char *end)
XiaokangQian7807f9f2022-02-15 10:04:37 +0000851{
XiaokangQianb67384d2022-04-19 00:02:38 +0000852 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000853 unsigned char const *p = buf;
XiaokangQian8f9dfe42022-04-15 02:52:39 +0000854 unsigned char const *client_shares_end;
Jerry Yuc1be19f2022-04-23 16:11:39 +0800855 size_t client_shares_len;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000856
857 /* From RFC 8446:
858 *
859 * struct {
860 * KeyShareEntry client_shares<0..2^16-1>;
861 * } KeyShareClientHello;
862 *
863 */
864
Gilles Peskine449bd832023-01-11 14:50:10 +0100865 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, 2);
866 client_shares_len = MBEDTLS_GET_UINT16_BE(p, 0);
XiaokangQian7807f9f2022-02-15 10:04:37 +0000867 p += 2;
Gilles Peskine449bd832023-01-11 14:50:10 +0100868 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, client_shares_len);
XiaokangQian7807f9f2022-02-15 10:04:37 +0000869
870 ssl->handshake->offered_group_id = 0;
XiaokangQian8f9dfe42022-04-15 02:52:39 +0000871 client_shares_end = p + client_shares_len;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000872
873 /* We try to find a suitable key share entry and copy it to the
874 * handshake context. Later, we have to find out whether we can do
875 * something with the provided key share or whether we have to
XiaokangQianc5763b52022-04-02 03:34:37 +0000876 * dismiss it and send a HelloRetryRequest message.
877 */
XiaokangQian7807f9f2022-02-15 10:04:37 +0000878
Gilles Peskine449bd832023-01-11 14:50:10 +0100879 while (p < client_shares_end) {
XiaokangQian9b5d04b2022-04-10 10:20:43 +0000880 uint16_t group;
Jerry Yuc1be19f2022-04-23 16:11:39 +0800881 size_t key_exchange_len;
Jerry Yu086edc22022-05-05 10:50:38 +0800882 const unsigned char *key_exchange;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000883
884 /*
885 * struct {
886 * NamedGroup group;
887 * opaque key_exchange<1..2^16-1>;
888 * } KeyShareEntry;
889 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100890 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, client_shares_end, 4);
891 group = MBEDTLS_GET_UINT16_BE(p, 0);
892 key_exchange_len = MBEDTLS_GET_UINT16_BE(p, 2);
Jerry Yuc1be19f2022-04-23 16:11:39 +0800893 p += 4;
Jerry Yu086edc22022-05-05 10:50:38 +0800894 key_exchange = p;
Gilles Peskine449bd832023-01-11 14:50:10 +0100895 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, client_shares_end, key_exchange_len);
Jerry Yu086edc22022-05-05 10:50:38 +0800896 p += key_exchange_len;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000897
898 /* Continue parsing even if we have already found a match,
XiaokangQianc5763b52022-04-02 03:34:37 +0000899 * for input validation purposes.
900 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100901 if (!mbedtls_ssl_named_group_is_offered(ssl, group) ||
902 !mbedtls_ssl_named_group_is_supported(group) ||
903 ssl->handshake->offered_group_id != 0) {
XiaokangQian060d8672022-04-21 09:24:56 +0000904 continue;
905 }
XiaokangQian060d8672022-04-21 09:24:56 +0000906
XiaokangQian7807f9f2022-02-15 10:04:37 +0000907 /*
XiaokangQian8f9dfe42022-04-15 02:52:39 +0000908 * For now, we only support ECDHE groups.
XiaokangQian7807f9f2022-02-15 10:04:37 +0000909 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100910 if (mbedtls_ssl_tls13_named_group_is_ecdhe(group)) {
911 MBEDTLS_SSL_DEBUG_MSG(2, ("ECDH group: %s (%04x)",
912 mbedtls_ssl_named_group_to_str(group),
913 group));
XiaokangQian318dc762022-04-20 09:43:51 +0000914 ret = mbedtls_ssl_tls13_read_public_ecdhe_share(
Gilles Peskine449bd832023-01-11 14:50:10 +0100915 ssl, key_exchange - 2, key_exchange_len + 2);
916 if (ret != 0) {
917 return ret;
918 }
XiaokangQian4e8cd7b2022-04-21 09:48:09 +0000919
Gilles Peskine449bd832023-01-11 14:50:10 +0100920 } else {
921 MBEDTLS_SSL_DEBUG_MSG(4, ("Unrecognized NamedGroup %u",
922 (unsigned) group));
XiaokangQian7807f9f2022-02-15 10:04:37 +0000923 continue;
924 }
925
XiaokangQian9b5d04b2022-04-10 10:20:43 +0000926 ssl->handshake->offered_group_id = group;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000927 }
928
Jerry Yuc1be19f2022-04-23 16:11:39 +0800929
Gilles Peskine449bd832023-01-11 14:50:10 +0100930 if (ssl->handshake->offered_group_id == 0) {
931 MBEDTLS_SSL_DEBUG_MSG(1, ("no matching key share"));
932 return SSL_TLS1_3_PARSE_KEY_SHARES_EXT_NO_MATCH;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000933 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100934 return 0;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000935}
Valerio Setti080a22b2023-03-20 15:22:47 +0100936#endif /* PSA_WANT_ALG_ECDH */
XiaokangQian7807f9f2022-02-15 10:04:37 +0000937
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200938MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100939static int ssl_tls13_client_hello_has_exts(mbedtls_ssl_context *ssl,
940 int exts_mask)
XiaokangQian7807f9f2022-02-15 10:04:37 +0000941{
Jerry Yu0c354a22022-08-29 15:25:36 +0800942 int masked = ssl->handshake->received_extensions & exts_mask;
Gilles Peskine449bd832023-01-11 14:50:10 +0100943 return masked == exts_mask;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000944}
945
Jerry Yue5991322022-11-07 14:03:44 +0800946#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200947MBEDTLS_CHECK_RETURN_CRITICAL
XiaokangQianb67384d2022-04-19 00:02:38 +0000948static int ssl_tls13_client_hello_has_exts_for_ephemeral_key_exchange(
Gilles Peskine449bd832023-01-11 14:50:10 +0100949 mbedtls_ssl_context *ssl)
XiaokangQian7807f9f2022-02-15 10:04:37 +0000950{
Gilles Peskine449bd832023-01-11 14:50:10 +0100951 return ssl_tls13_client_hello_has_exts(
952 ssl,
953 MBEDTLS_SSL_EXT_MASK(SUPPORTED_GROUPS) |
954 MBEDTLS_SSL_EXT_MASK(KEY_SHARE) |
955 MBEDTLS_SSL_EXT_MASK(SIG_ALG));
XiaokangQian7807f9f2022-02-15 10:04:37 +0000956}
Jerry Yue5991322022-11-07 14:03:44 +0800957#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED */
XiaokangQian7807f9f2022-02-15 10:04:37 +0000958
Jerry Yue5991322022-11-07 14:03:44 +0800959#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK_ENABLED)
Jerry Yu77f01482022-07-11 07:03:24 +0000960MBEDTLS_CHECK_RETURN_CRITICAL
961static int ssl_tls13_client_hello_has_exts_for_psk_key_exchange(
Gilles Peskine449bd832023-01-11 14:50:10 +0100962 mbedtls_ssl_context *ssl)
Jerry Yu77f01482022-07-11 07:03:24 +0000963{
Gilles Peskine449bd832023-01-11 14:50:10 +0100964 return ssl_tls13_client_hello_has_exts(
965 ssl,
966 MBEDTLS_SSL_EXT_MASK(PRE_SHARED_KEY) |
967 MBEDTLS_SSL_EXT_MASK(PSK_KEY_EXCHANGE_MODES));
Jerry Yu77f01482022-07-11 07:03:24 +0000968}
Jerry Yue5991322022-11-07 14:03:44 +0800969#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK_ENABLED */
Jerry Yu77f01482022-07-11 07:03:24 +0000970
Jerry Yue5991322022-11-07 14:03:44 +0800971#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK_EPHEMERAL_ENABLED)
Jerry Yu77f01482022-07-11 07:03:24 +0000972MBEDTLS_CHECK_RETURN_CRITICAL
973static int ssl_tls13_client_hello_has_exts_for_psk_ephemeral_key_exchange(
Gilles Peskine449bd832023-01-11 14:50:10 +0100974 mbedtls_ssl_context *ssl)
Jerry Yu77f01482022-07-11 07:03:24 +0000975{
Gilles Peskine449bd832023-01-11 14:50:10 +0100976 return ssl_tls13_client_hello_has_exts(
977 ssl,
978 MBEDTLS_SSL_EXT_MASK(SUPPORTED_GROUPS) |
979 MBEDTLS_SSL_EXT_MASK(KEY_SHARE) |
980 MBEDTLS_SSL_EXT_MASK(PRE_SHARED_KEY) |
981 MBEDTLS_SSL_EXT_MASK(PSK_KEY_EXCHANGE_MODES));
Jerry Yu77f01482022-07-11 07:03:24 +0000982}
Jerry Yue5991322022-11-07 14:03:44 +0800983#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK_EPHEMERAL_ENABLED */
Jerry Yu77f01482022-07-11 07:03:24 +0000984
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200985MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100986static int ssl_tls13_check_ephemeral_key_exchange(mbedtls_ssl_context *ssl)
XiaokangQian7807f9f2022-02-15 10:04:37 +0000987{
Jerry Yue5991322022-11-07 14:03:44 +0800988#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +0100989 return mbedtls_ssl_conf_tls13_ephemeral_enabled(ssl) &&
990 ssl_tls13_client_hello_has_exts_for_ephemeral_key_exchange(ssl);
Jerry Yue5991322022-11-07 14:03:44 +0800991#else
992 ((void) ssl);
Gilles Peskine449bd832023-01-11 14:50:10 +0100993 return 0;
Jerry Yue5991322022-11-07 14:03:44 +0800994#endif
Jerry Yu77f01482022-07-11 07:03:24 +0000995}
XiaokangQian7807f9f2022-02-15 10:04:37 +0000996
Jerry Yu77f01482022-07-11 07:03:24 +0000997MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100998static int ssl_tls13_check_psk_key_exchange(mbedtls_ssl_context *ssl)
Jerry Yu77f01482022-07-11 07:03:24 +0000999{
Jerry Yue5991322022-11-07 14:03:44 +08001000#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01001001 return mbedtls_ssl_conf_tls13_psk_enabled(ssl) &&
1002 mbedtls_ssl_tls13_psk_enabled(ssl) &&
1003 ssl_tls13_client_hello_has_exts_for_psk_key_exchange(ssl);
Jerry Yu77f01482022-07-11 07:03:24 +00001004#else
1005 ((void) ssl);
Gilles Peskine449bd832023-01-11 14:50:10 +01001006 return 0;
Jerry Yu77f01482022-07-11 07:03:24 +00001007#endif
1008}
XiaokangQian7807f9f2022-02-15 10:04:37 +00001009
Jerry Yu77f01482022-07-11 07:03:24 +00001010MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01001011static int ssl_tls13_check_psk_ephemeral_key_exchange(mbedtls_ssl_context *ssl)
Jerry Yu77f01482022-07-11 07:03:24 +00001012{
Jerry Yue5991322022-11-07 14:03:44 +08001013#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK_EPHEMERAL_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01001014 return mbedtls_ssl_conf_tls13_psk_ephemeral_enabled(ssl) &&
1015 mbedtls_ssl_tls13_psk_ephemeral_enabled(ssl) &&
1016 ssl_tls13_client_hello_has_exts_for_psk_ephemeral_key_exchange(ssl);
Jerry Yu77f01482022-07-11 07:03:24 +00001017#else
1018 ((void) ssl);
Gilles Peskine449bd832023-01-11 14:50:10 +01001019 return 0;
Jerry Yu77f01482022-07-11 07:03:24 +00001020#endif
1021}
1022
Gilles Peskine449bd832023-01-11 14:50:10 +01001023static int ssl_tls13_determine_key_exchange_mode(mbedtls_ssl_context *ssl)
Jerry Yu77f01482022-07-11 07:03:24 +00001024{
1025 /*
1026 * Determine the key exchange algorithm to use.
1027 * There are three types of key exchanges supported in TLS 1.3:
1028 * - (EC)DH with ECDSA,
1029 * - (EC)DH with PSK,
1030 * - plain PSK.
1031 *
1032 * The PSK-based key exchanges may additionally be used with 0-RTT.
1033 *
1034 * Our built-in order of preference is
Jerry Yuce6ed702022-07-22 21:49:53 +08001035 * 1 ) (EC)DHE-PSK Mode ( psk_ephemeral )
1036 * 2 ) Certificate Mode ( ephemeral )
1037 * 3 ) Plain PSK Mode ( psk )
Jerry Yu77f01482022-07-11 07:03:24 +00001038 */
1039
1040 ssl->handshake->key_exchange_mode = MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_NONE;
1041
Gilles Peskine449bd832023-01-11 14:50:10 +01001042 if (ssl_tls13_check_psk_ephemeral_key_exchange(ssl)) {
Jerry Yu77f01482022-07-11 07:03:24 +00001043 ssl->handshake->key_exchange_mode =
1044 MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK_EPHEMERAL;
Gilles Peskine449bd832023-01-11 14:50:10 +01001045 MBEDTLS_SSL_DEBUG_MSG(2, ("key exchange mode: psk_ephemeral"));
1046 } else
1047 if (ssl_tls13_check_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_EPHEMERAL;
Gilles Peskine449bd832023-01-11 14:50:10 +01001050 MBEDTLS_SSL_DEBUG_MSG(2, ("key exchange mode: ephemeral"));
1051 } else
1052 if (ssl_tls13_check_psk_key_exchange(ssl)) {
Jerry Yuce6ed702022-07-22 21:49:53 +08001053 ssl->handshake->key_exchange_mode =
1054 MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK;
Gilles Peskine449bd832023-01-11 14:50:10 +01001055 MBEDTLS_SSL_DEBUG_MSG(2, ("key exchange mode: psk"));
1056 } else {
Jerry Yu77f01482022-07-11 07:03:24 +00001057 MBEDTLS_SSL_DEBUG_MSG(
Gilles Peskine449bd832023-01-11 14:50:10 +01001058 1,
1059 ("ClientHello message misses mandatory extensions."));
1060 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_MISSING_EXTENSION,
1061 MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER);
1062 return MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER;
Jerry Yu77f01482022-07-11 07:03:24 +00001063 }
1064
Gilles Peskine449bd832023-01-11 14:50:10 +01001065 return 0;
Jerry Yu77f01482022-07-11 07:03:24 +00001066
XiaokangQian7807f9f2022-02-15 10:04:37 +00001067}
1068
XiaokangQian81802f42022-06-10 13:25:22 +00001069#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
Ronald Cron928cbd32022-10-04 16:14:26 +02001070 defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED)
Ronald Cron67ea2542022-09-15 17:34:42 +02001071
1072#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01001073static psa_algorithm_t ssl_tls13_iana_sig_alg_to_psa_alg(uint16_t sig_alg)
Ronald Cron67ea2542022-09-15 17:34:42 +02001074{
Gilles Peskine449bd832023-01-11 14:50:10 +01001075 switch (sig_alg) {
Ronald Cron67ea2542022-09-15 17:34:42 +02001076 case MBEDTLS_TLS1_3_SIG_ECDSA_SECP256R1_SHA256:
Gilles Peskine449bd832023-01-11 14:50:10 +01001077 return PSA_ALG_ECDSA(PSA_ALG_SHA_256);
Ronald Cron67ea2542022-09-15 17:34:42 +02001078 case MBEDTLS_TLS1_3_SIG_ECDSA_SECP384R1_SHA384:
Gilles Peskine449bd832023-01-11 14:50:10 +01001079 return PSA_ALG_ECDSA(PSA_ALG_SHA_384);
Ronald Cron67ea2542022-09-15 17:34:42 +02001080 case MBEDTLS_TLS1_3_SIG_ECDSA_SECP521R1_SHA512:
Gilles Peskine449bd832023-01-11 14:50:10 +01001081 return PSA_ALG_ECDSA(PSA_ALG_SHA_512);
Ronald Cron67ea2542022-09-15 17:34:42 +02001082 case MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA256:
Gilles Peskine449bd832023-01-11 14:50:10 +01001083 return PSA_ALG_RSA_PSS(PSA_ALG_SHA_256);
Ronald Cron67ea2542022-09-15 17:34:42 +02001084 case MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA384:
Gilles Peskine449bd832023-01-11 14:50:10 +01001085 return PSA_ALG_RSA_PSS(PSA_ALG_SHA_384);
Ronald Cron67ea2542022-09-15 17:34:42 +02001086 case MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA512:
Gilles Peskine449bd832023-01-11 14:50:10 +01001087 return PSA_ALG_RSA_PSS(PSA_ALG_SHA_512);
Ronald Cron67ea2542022-09-15 17:34:42 +02001088 case MBEDTLS_TLS1_3_SIG_RSA_PKCS1_SHA256:
Gilles Peskine449bd832023-01-11 14:50:10 +01001089 return PSA_ALG_RSA_PKCS1V15_SIGN(PSA_ALG_SHA_256);
Ronald Cron67ea2542022-09-15 17:34:42 +02001090 case MBEDTLS_TLS1_3_SIG_RSA_PKCS1_SHA384:
Gilles Peskine449bd832023-01-11 14:50:10 +01001091 return PSA_ALG_RSA_PKCS1V15_SIGN(PSA_ALG_SHA_384);
Ronald Cron67ea2542022-09-15 17:34:42 +02001092 case MBEDTLS_TLS1_3_SIG_RSA_PKCS1_SHA512:
Gilles Peskine449bd832023-01-11 14:50:10 +01001093 return PSA_ALG_RSA_PKCS1V15_SIGN(PSA_ALG_SHA_512);
Ronald Cron67ea2542022-09-15 17:34:42 +02001094 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01001095 return PSA_ALG_NONE;
Ronald Cron67ea2542022-09-15 17:34:42 +02001096 }
1097}
1098#endif /* MBEDTLS_USE_PSA_CRYPTO */
1099
XiaokangQian23c5be62022-06-07 02:04:34 +00001100/*
XiaokangQianfb665a82022-06-15 03:57:21 +00001101 * Pick best ( private key, certificate chain ) pair based on the signature
1102 * algorithms supported by the client.
XiaokangQian23c5be62022-06-07 02:04:34 +00001103 */
Ronald Cronce7d76e2022-07-08 18:56:49 +02001104MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01001105static int ssl_tls13_pick_key_cert(mbedtls_ssl_context *ssl)
XiaokangQian23c5be62022-06-07 02:04:34 +00001106{
XiaokangQianfb665a82022-06-15 03:57:21 +00001107 mbedtls_ssl_key_cert *key_cert, *key_cert_list;
XiaokangQian81802f42022-06-10 13:25:22 +00001108 const uint16_t *sig_alg = ssl->handshake->received_sig_algs;
XiaokangQian23c5be62022-06-07 02:04:34 +00001109
1110#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Gilles Peskine449bd832023-01-11 14:50:10 +01001111 if (ssl->handshake->sni_key_cert != NULL) {
XiaokangQianfb665a82022-06-15 03:57:21 +00001112 key_cert_list = ssl->handshake->sni_key_cert;
Gilles Peskine449bd832023-01-11 14:50:10 +01001113 } else
XiaokangQian23c5be62022-06-07 02:04:34 +00001114#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
Gilles Peskine449bd832023-01-11 14:50:10 +01001115 key_cert_list = ssl->conf->key_cert;
XiaokangQian23c5be62022-06-07 02:04:34 +00001116
Gilles Peskine449bd832023-01-11 14:50:10 +01001117 if (key_cert_list == NULL) {
1118 MBEDTLS_SSL_DEBUG_MSG(3, ("server has no certificate"));
1119 return -1;
XiaokangQian23c5be62022-06-07 02:04:34 +00001120 }
1121
Gilles Peskine449bd832023-01-11 14:50:10 +01001122 for (; *sig_alg != MBEDTLS_TLS1_3_SIG_NONE; sig_alg++) {
1123 if (!mbedtls_ssl_sig_alg_is_offered(ssl, *sig_alg)) {
Ronald Cron67ea2542022-09-15 17:34:42 +02001124 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +01001125 }
Ronald Cron67ea2542022-09-15 17:34:42 +02001126
Gilles Peskine449bd832023-01-11 14:50:10 +01001127 if (!mbedtls_ssl_tls13_sig_alg_for_cert_verify_is_supported(*sig_alg)) {
Ronald Cron67ea2542022-09-15 17:34:42 +02001128 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +01001129 }
Ronald Cron67ea2542022-09-15 17:34:42 +02001130
Gilles Peskine449bd832023-01-11 14:50:10 +01001131 for (key_cert = key_cert_list; key_cert != NULL;
1132 key_cert = key_cert->next) {
Ronald Cron67ea2542022-09-15 17:34:42 +02001133#if defined(MBEDTLS_USE_PSA_CRYPTO)
1134 psa_algorithm_t psa_alg = PSA_ALG_NONE;
1135#endif /* MBEDTLS_USE_PSA_CRYPTO */
1136
Gilles Peskine449bd832023-01-11 14:50:10 +01001137 MBEDTLS_SSL_DEBUG_CRT(3, "certificate (chain) candidate",
1138 key_cert->cert);
XiaokangQian81802f42022-06-10 13:25:22 +00001139
1140 /*
Gilles Peskine449bd832023-01-11 14:50:10 +01001141 * This avoids sending the client a cert it'll reject based on
1142 * keyUsage or other extensions.
1143 */
1144 if (mbedtls_x509_crt_check_key_usage(
1145 key_cert->cert, MBEDTLS_X509_KU_DIGITAL_SIGNATURE) != 0 ||
XiaokangQian81802f42022-06-10 13:25:22 +00001146 mbedtls_x509_crt_check_extended_key_usage(
XiaokangQianfb665a82022-06-15 03:57:21 +00001147 key_cert->cert, MBEDTLS_OID_SERVER_AUTH,
Gilles Peskine449bd832023-01-11 14:50:10 +01001148 MBEDTLS_OID_SIZE(MBEDTLS_OID_SERVER_AUTH)) != 0) {
1149 MBEDTLS_SSL_DEBUG_MSG(3, ("certificate mismatch: "
1150 "(extended) key usage extension"));
XiaokangQian81802f42022-06-10 13:25:22 +00001151 continue;
1152 }
XiaokangQian23c5be62022-06-07 02:04:34 +00001153
Gilles Peskine449bd832023-01-11 14:50:10 +01001154 MBEDTLS_SSL_DEBUG_MSG(3,
1155 ("ssl_tls13_pick_key_cert:"
1156 "check signature algorithm %s [%04x]",
1157 mbedtls_ssl_sig_alg_to_str(*sig_alg),
1158 *sig_alg));
Ronald Cron67ea2542022-09-15 17:34:42 +02001159#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01001160 psa_alg = ssl_tls13_iana_sig_alg_to_psa_alg(*sig_alg);
Ronald Cron67ea2542022-09-15 17:34:42 +02001161#endif /* MBEDTLS_USE_PSA_CRYPTO */
1162
Gilles Peskine449bd832023-01-11 14:50:10 +01001163 if (mbedtls_ssl_tls13_check_sig_alg_cert_key_match(
1164 *sig_alg, &key_cert->cert->pk)
Ronald Cron67ea2542022-09-15 17:34:42 +02001165#if defined(MBEDTLS_USE_PSA_CRYPTO)
1166 && psa_alg != PSA_ALG_NONE &&
Gilles Peskine449bd832023-01-11 14:50:10 +01001167 mbedtls_pk_can_do_ext(&key_cert->cert->pk, psa_alg,
1168 PSA_KEY_USAGE_SIGN_HASH) == 1
Ronald Cron67ea2542022-09-15 17:34:42 +02001169#endif /* MBEDTLS_USE_PSA_CRYPTO */
Gilles Peskine449bd832023-01-11 14:50:10 +01001170 ) {
XiaokangQianfb665a82022-06-15 03:57:21 +00001171 ssl->handshake->key_cert = key_cert;
Gilles Peskine449bd832023-01-11 14:50:10 +01001172 MBEDTLS_SSL_DEBUG_MSG(3,
1173 ("ssl_tls13_pick_key_cert:"
1174 "selected signature algorithm"
1175 " %s [%04x]",
1176 mbedtls_ssl_sig_alg_to_str(*sig_alg),
1177 *sig_alg));
XiaokangQianfb665a82022-06-15 03:57:21 +00001178 MBEDTLS_SSL_DEBUG_CRT(
Gilles Peskine449bd832023-01-11 14:50:10 +01001179 3, "selected certificate (chain)",
1180 ssl->handshake->key_cert->cert);
1181 return 0;
XiaokangQian81802f42022-06-10 13:25:22 +00001182 }
XiaokangQian81802f42022-06-10 13:25:22 +00001183 }
XiaokangQian23c5be62022-06-07 02:04:34 +00001184 }
1185
Gilles Peskine449bd832023-01-11 14:50:10 +01001186 MBEDTLS_SSL_DEBUG_MSG(2, ("ssl_tls13_pick_key_cert:"
1187 "no suitable certificate found"));
1188 return -1;
XiaokangQian23c5be62022-06-07 02:04:34 +00001189}
XiaokangQian81802f42022-06-10 13:25:22 +00001190#endif /* MBEDTLS_X509_CRT_PARSE_C &&
Ronald Cron928cbd32022-10-04 16:14:26 +02001191 MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED */
XiaokangQian23c5be62022-06-07 02:04:34 +00001192
XiaokangQian4080a7f2022-04-11 09:55:18 +00001193/*
XiaokangQianed582dd2022-04-13 08:21:05 +00001194 *
1195 * STATE HANDLING: ClientHello
1196 *
XiaokangQianb67384d2022-04-19 00:02:38 +00001197 * There are three possible classes of outcomes when parsing the ClientHello:
XiaokangQianed582dd2022-04-13 08:21:05 +00001198 *
XiaokangQianb67384d2022-04-19 00:02:38 +00001199 * 1) The ClientHello was well-formed and matched the server's configuration.
XiaokangQianed582dd2022-04-13 08:21:05 +00001200 *
1201 * In this case, the server progresses to sending its ServerHello.
1202 *
XiaokangQianb67384d2022-04-19 00:02:38 +00001203 * 2) The ClientHello was well-formed but didn't match the server's
1204 * configuration.
XiaokangQianed582dd2022-04-13 08:21:05 +00001205 *
1206 * For example, the client might not have offered a key share which
1207 * the server supports, or the server might require a cookie.
1208 *
1209 * In this case, the server sends a HelloRetryRequest.
1210 *
XiaokangQianb67384d2022-04-19 00:02:38 +00001211 * 3) The ClientHello was ill-formed
XiaokangQianed582dd2022-04-13 08:21:05 +00001212 *
1213 * In this case, we abort the handshake.
1214 *
1215 */
1216
1217/*
XiaokangQian4080a7f2022-04-11 09:55:18 +00001218 * Structure of this message:
1219 *
XiaokangQiane8ff3502022-04-22 02:34:40 +00001220 * uint16 ProtocolVersion;
1221 * opaque Random[32];
1222 * uint8 CipherSuite[2]; // Cryptographic suite selector
XiaokangQian4080a7f2022-04-11 09:55:18 +00001223 *
XiaokangQiane8ff3502022-04-22 02:34:40 +00001224 * struct {
1225 * ProtocolVersion legacy_version = 0x0303; // TLS v1.2
1226 * Random random;
1227 * opaque legacy_session_id<0..32>;
1228 * CipherSuite cipher_suites<2..2^16-2>;
1229 * opaque legacy_compression_methods<1..2^8-1>;
1230 * Extension extensions<8..2^16-1>;
1231 * } ClientHello;
XiaokangQian4080a7f2022-04-11 09:55:18 +00001232 */
XiaokangQianed582dd2022-04-13 08:21:05 +00001233
1234#define SSL_CLIENT_HELLO_OK 0
1235#define SSL_CLIENT_HELLO_HRR_REQUIRED 1
1236
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001237MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01001238static int ssl_tls13_parse_client_hello(mbedtls_ssl_context *ssl,
1239 const unsigned char *buf,
1240 const unsigned char *end)
XiaokangQian7807f9f2022-02-15 10:04:37 +00001241{
XiaokangQianb67384d2022-04-19 00:02:38 +00001242 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1243 const unsigned char *p = buf;
Ronald Crond540d992023-03-07 09:41:48 +01001244 const unsigned char *random;
XiaokangQian4080a7f2022-04-11 09:55:18 +00001245 size_t legacy_session_id_len;
XiaokangQian318dc762022-04-20 09:43:51 +00001246 size_t cipher_suites_len;
XiaokangQian060d8672022-04-21 09:24:56 +00001247 const unsigned char *cipher_suites_end;
XiaokangQianb67384d2022-04-19 00:02:38 +00001248 size_t extensions_len;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001249 const unsigned char *extensions_end;
Jerry Yuc4bf5d62022-10-29 09:08:47 +08001250 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
Jerry Yu49ca9282022-05-05 11:05:22 +08001251 int hrr_required = 0;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001252
Ronald Cron41a443a2022-10-04 16:38:25 +02001253#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED)
Jerry Yu5725f1c2022-08-21 17:27:16 +08001254 const unsigned char *cipher_suites;
Jerry Yu29d9faa2022-08-23 17:52:45 +08001255 const unsigned char *pre_shared_key_ext = NULL;
Jerry Yu1c105562022-07-10 06:32:38 +00001256 const unsigned char *pre_shared_key_ext_end = NULL;
Ronald Cron41a443a2022-10-04 16:38:25 +02001257#endif
XiaokangQian7807f9f2022-02-15 10:04:37 +00001258
XiaokangQian7807f9f2022-02-15 10:04:37 +00001259 /*
XiaokangQianb67384d2022-04-19 00:02:38 +00001260 * ClientHello layout:
XiaokangQian7807f9f2022-02-15 10:04:37 +00001261 * 0 . 1 protocol version
XiaokangQiane8ff3502022-04-22 02:34:40 +00001262 * 2 . 33 random bytes
XiaokangQianc5763b52022-04-02 03:34:37 +00001263 * 34 . 34 session id length ( 1 byte )
XiaokangQian7807f9f2022-02-15 10:04:37 +00001264 * 35 . 34+x session id
XiaokangQian7807f9f2022-02-15 10:04:37 +00001265 * .. . .. ciphersuite list length ( 2 bytes )
1266 * .. . .. ciphersuite list
1267 * .. . .. compression alg. list length ( 1 byte )
1268 * .. . .. compression alg. list
1269 * .. . .. extensions length ( 2 bytes, optional )
1270 * .. . .. extensions ( optional )
1271 */
1272
XiaokangQianb67384d2022-04-19 00:02:38 +00001273 /*
bootstrap-prime6dbbf442022-05-17 19:30:44 -04001274 * Minimal length ( with everything empty and extensions omitted ) is
XiaokangQian7807f9f2022-02-15 10:04:37 +00001275 * 2 + 32 + 1 + 2 + 1 = 38 bytes. Check that first, so that we can
1276 * read at least up to session id length without worrying.
1277 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001278 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, 38);
XiaokangQian7807f9f2022-02-15 10:04:37 +00001279
1280 /* ...
1281 * ProtocolVersion legacy_version = 0x0303; // TLS 1.2
1282 * ...
1283 * with ProtocolVersion defined as:
1284 * uint16 ProtocolVersion;
1285 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001286 if (mbedtls_ssl_read_version(p, ssl->conf->transport) !=
1287 MBEDTLS_SSL_VERSION_TLS1_2) {
1288 MBEDTLS_SSL_DEBUG_MSG(1, ("Unsupported version of TLS."));
1289 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_PROTOCOL_VERSION,
1290 MBEDTLS_ERR_SSL_BAD_PROTOCOL_VERSION);
1291 return MBEDTLS_ERR_SSL_BAD_PROTOCOL_VERSION;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001292 }
1293 p += 2;
1294
Jerry Yuc1be19f2022-04-23 16:11:39 +08001295 /* ...
1296 * Random random;
1297 * ...
XiaokangQianb67384d2022-04-19 00:02:38 +00001298 * with Random defined as:
1299 * opaque Random[32];
XiaokangQian7807f9f2022-02-15 10:04:37 +00001300 */
Ronald Crond540d992023-03-07 09:41:48 +01001301 random = p;
XiaokangQian08037552022-04-20 07:16:41 +00001302 p += MBEDTLS_CLIENT_HELLO_RANDOM_LEN;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001303
Jerry Yuc1be19f2022-04-23 16:11:39 +08001304 /* ...
XiaokangQianb67384d2022-04-19 00:02:38 +00001305 * opaque legacy_session_id<0..32>;
Jerry Yuc1be19f2022-04-23 16:11:39 +08001306 * ...
XiaokangQian7807f9f2022-02-15 10:04:37 +00001307 */
XiaokangQian4080a7f2022-04-11 09:55:18 +00001308 legacy_session_id_len = p[0];
XiaokangQianc5763b52022-04-02 03:34:37 +00001309 p++;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001310
Gilles Peskine449bd832023-01-11 14:50:10 +01001311 if (legacy_session_id_len > sizeof(ssl->session_negotiate->id)) {
1312 MBEDTLS_SSL_DEBUG_MSG(1, ("bad client hello message"));
1313 return MBEDTLS_ERR_SSL_DECODE_ERROR;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001314 }
1315
XiaokangQian4080a7f2022-04-11 09:55:18 +00001316 ssl->session_negotiate->id_len = legacy_session_id_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01001317 MBEDTLS_SSL_DEBUG_BUF(3, "client hello, session id",
1318 p, legacy_session_id_len);
XiaokangQianb67384d2022-04-19 00:02:38 +00001319 /*
1320 * Check we have enough data for the legacy session identifier
Jerry Yue95c8af2022-07-26 15:48:20 +08001321 * and the ciphersuite list length.
XiaokangQianb67384d2022-04-19 00:02:38 +00001322 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001323 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, legacy_session_id_len + 2);
XiaokangQian7807f9f2022-02-15 10:04:37 +00001324
Gilles Peskine449bd832023-01-11 14:50:10 +01001325 memcpy(&ssl->session_negotiate->id[0], p, legacy_session_id_len);
XiaokangQian4080a7f2022-04-11 09:55:18 +00001326 p += legacy_session_id_len;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001327
Gilles Peskine449bd832023-01-11 14:50:10 +01001328 cipher_suites_len = MBEDTLS_GET_UINT16_BE(p, 0);
XiaokangQian7807f9f2022-02-15 10:04:37 +00001329 p += 2;
1330
Ronald Cronfc7ae872023-02-16 15:32:19 +01001331 /*
1332 * The length of the ciphersuite list has to be even.
1333 */
1334 if (cipher_suites_len & 1) {
1335 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR,
1336 MBEDTLS_ERR_SSL_DECODE_ERROR);
1337 return MBEDTLS_ERR_SSL_DECODE_ERROR;
1338 }
1339
XiaokangQianb67384d2022-04-19 00:02:38 +00001340 /* Check we have enough data for the ciphersuite list, the legacy
1341 * compression methods and the length of the extensions.
Jerry Yue95c8af2022-07-26 15:48:20 +08001342 *
1343 * cipher_suites cipher_suites_len bytes
1344 * legacy_compression_methods 2 bytes
1345 * extensions_len 2 bytes
XiaokangQianb67384d2022-04-19 00:02:38 +00001346 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001347 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, cipher_suites_len + 2 + 2);
XiaokangQian7807f9f2022-02-15 10:04:37 +00001348
Gilles Peskine449bd832023-01-11 14:50:10 +01001349 /* ...
1350 * CipherSuite cipher_suites<2..2^16-2>;
1351 * ...
1352 * with CipherSuite defined as:
1353 * uint8 CipherSuite[2];
1354 */
Ronald Cron41a443a2022-10-04 16:38:25 +02001355#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED)
XiaokangQian060d8672022-04-21 09:24:56 +00001356 cipher_suites = p;
Jerry Yu5725f1c2022-08-21 17:27:16 +08001357#endif
XiaokangQian060d8672022-04-21 09:24:56 +00001358 cipher_suites_end = p + cipher_suites_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01001359 MBEDTLS_SSL_DEBUG_BUF(3, "client hello, ciphersuitelist",
1360 p, cipher_suites_len);
Jerry Yu5725f1c2022-08-21 17:27:16 +08001361
1362 /*
Ronald Cron64582392023-03-07 09:21:40 +01001363 * Only support TLS 1.3 currently, temporarily set the version.
1364 */
1365 ssl->tls_version = MBEDTLS_SSL_VERSION_TLS1_3;
1366
1367#if defined(MBEDTLS_SSL_SESSION_TICKETS)
1368 /* Store minor version for later use with ticket serialization. */
1369 ssl->session_negotiate->tls_version = MBEDTLS_SSL_VERSION_TLS1_3;
1370 ssl->session_negotiate->endpoint = ssl->conf->endpoint;
1371#endif
1372
1373 /*
Ronald Crond540d992023-03-07 09:41:48 +01001374 * We are negotiation the version 1.3 of the protocol. Do what we have
1375 * postponed: copy of the client random bytes.
1376 */
1377 MBEDTLS_SSL_DEBUG_BUF(3, "client hello, random bytes",
1378 random, MBEDTLS_CLIENT_HELLO_RANDOM_LEN);
1379 memcpy(&handshake->randbytes[0], random, MBEDTLS_CLIENT_HELLO_RANDOM_LEN);
1380
1381 /*
Jerry Yu5725f1c2022-08-21 17:27:16 +08001382 * Search for a matching ciphersuite
1383 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001384 for (; p < cipher_suites_end; p += 2) {
XiaokangQiane8ff3502022-04-22 02:34:40 +00001385 uint16_t cipher_suite;
Gilles Peskine449bd832023-01-11 14:50:10 +01001386 const mbedtls_ssl_ciphersuite_t *ciphersuite_info;
Jerry Yu5725f1c2022-08-21 17:27:16 +08001387
Ronald Crond89360b2023-02-21 08:53:33 +01001388 /*
1389 * "cipher_suite_end - p is even" is an invariant of the loop. As
1390 * cipher_suites_end - p > 0, we have cipher_suites_end - p >= 2 and
1391 * it is thus safe to read two bytes.
1392 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001393 cipher_suite = MBEDTLS_GET_UINT16_BE(p, 0);
Jerry Yuc5a23a02022-08-25 10:51:44 +08001394 ciphersuite_info = ssl_tls13_validate_peer_ciphersuite(
Gilles Peskine449bd832023-01-11 14:50:10 +01001395 ssl, cipher_suite);
1396 if (ciphersuite_info == NULL) {
Jerry Yu5725f1c2022-08-21 17:27:16 +08001397 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +01001398 }
Jerry Yu5725f1c2022-08-21 17:27:16 +08001399
Jerry Yu5725f1c2022-08-21 17:27:16 +08001400 ssl->session_negotiate->ciphersuite = cipher_suite;
Jerry Yuc4bf5d62022-10-29 09:08:47 +08001401 handshake->ciphersuite_info = ciphersuite_info;
Gilles Peskine449bd832023-01-11 14:50:10 +01001402 MBEDTLS_SSL_DEBUG_MSG(2, ("selected ciphersuite: %04x - %s",
1403 cipher_suite,
1404 ciphersuite_info->name));
Ronald Cron25e9ec62023-02-16 15:35:16 +01001405 break;
XiaokangQian17f974c2022-04-19 09:57:41 +00001406 }
Jerry Yu5725f1c2022-08-21 17:27:16 +08001407
Gilles Peskine449bd832023-01-11 14:50:10 +01001408 if (handshake->ciphersuite_info == NULL) {
1409 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE,
1410 MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE);
1411 return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
Jerry Yu5725f1c2022-08-21 17:27:16 +08001412 }
Ronald Cron25e9ec62023-02-16 15:35:16 +01001413 p = cipher_suites_end;
Jerry Yuc1be19f2022-04-23 16:11:39 +08001414
XiaokangQian4080a7f2022-04-11 09:55:18 +00001415 /* ...
XiaokangQianb67384d2022-04-19 00:02:38 +00001416 * opaque legacy_compression_methods<1..2^8-1>;
XiaokangQian4080a7f2022-04-11 09:55:18 +00001417 * ...
XiaokangQian7807f9f2022-02-15 10:04:37 +00001418 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001419 if (p[0] != 1 || p[1] != MBEDTLS_SSL_COMPRESS_NULL) {
1420 MBEDTLS_SSL_DEBUG_MSG(1, ("bad legacy compression method"));
1421 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER,
1422 MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER);
1423 return MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001424 }
XiaokangQianb67384d2022-04-19 00:02:38 +00001425 p += 2;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001426
Jerry Yuc1be19f2022-04-23 16:11:39 +08001427 /* ...
XiaokangQianb67384d2022-04-19 00:02:38 +00001428 * Extension extensions<8..2^16-1>;
Jerry Yuc1be19f2022-04-23 16:11:39 +08001429 * ...
XiaokangQianb67384d2022-04-19 00:02:38 +00001430 * with Extension defined as:
1431 * struct {
1432 * ExtensionType extension_type;
1433 * opaque extension_data<0..2^16-1>;
1434 * } Extension;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001435 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001436 extensions_len = MBEDTLS_GET_UINT16_BE(p, 0);
XiaokangQian7807f9f2022-02-15 10:04:37 +00001437 p += 2;
Gilles Peskine449bd832023-01-11 14:50:10 +01001438 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, extensions_len);
XiaokangQiane8ff3502022-04-22 02:34:40 +00001439 extensions_end = p + extensions_len;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001440
Gilles Peskine449bd832023-01-11 14:50:10 +01001441 MBEDTLS_SSL_DEBUG_BUF(3, "client hello extensions", p, extensions_len);
XiaokangQian7807f9f2022-02-15 10:04:37 +00001442
Jerry Yu63a459c2022-10-31 13:38:40 +08001443 handshake->received_extensions = MBEDTLS_SSL_EXT_MASK_NONE;
Jerry Yu0c354a22022-08-29 15:25:36 +08001444
Gilles Peskine449bd832023-01-11 14:50:10 +01001445 while (p < extensions_end) {
XiaokangQian7807f9f2022-02-15 10:04:37 +00001446 unsigned int extension_type;
1447 size_t extension_data_len;
1448 const unsigned char *extension_data_end;
1449
Jerry Yu0c354a22022-08-29 15:25:36 +08001450 /* RFC 8446, section 4.2.11
Jerry Yu1c9247c2022-07-21 12:37:39 +08001451 *
1452 * The "pre_shared_key" extension MUST be the last extension in the
1453 * ClientHello (this facilitates implementation as described below).
1454 * Servers MUST check that it is the last extension and otherwise fail
1455 * the handshake with an "illegal_parameter" alert.
1456 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001457 if (handshake->received_extensions & MBEDTLS_SSL_EXT_MASK(PRE_SHARED_KEY)) {
Jerry Yu1c9247c2022-07-21 12:37:39 +08001458 MBEDTLS_SSL_DEBUG_MSG(
Gilles Peskine449bd832023-01-11 14:50:10 +01001459 3, ("pre_shared_key is not last extension."));
Jerry Yu1c9247c2022-07-21 12:37:39 +08001460 MBEDTLS_SSL_PEND_FATAL_ALERT(
1461 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER,
Gilles Peskine449bd832023-01-11 14:50:10 +01001462 MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER);
1463 return MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER;
Jerry Yu1c9247c2022-07-21 12:37:39 +08001464 }
1465
Gilles Peskine449bd832023-01-11 14:50:10 +01001466 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, extensions_end, 4);
1467 extension_type = MBEDTLS_GET_UINT16_BE(p, 0);
1468 extension_data_len = MBEDTLS_GET_UINT16_BE(p, 2);
XiaokangQian7807f9f2022-02-15 10:04:37 +00001469 p += 4;
1470
Gilles Peskine449bd832023-01-11 14:50:10 +01001471 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, extensions_end, extension_data_len);
XiaokangQian7807f9f2022-02-15 10:04:37 +00001472 extension_data_end = p + extension_data_len;
1473
Jerry Yuc4bf5d62022-10-29 09:08:47 +08001474 ret = mbedtls_ssl_tls13_check_received_extension(
Gilles Peskine449bd832023-01-11 14:50:10 +01001475 ssl, MBEDTLS_SSL_HS_CLIENT_HELLO, extension_type,
1476 MBEDTLS_SSL_TLS1_3_ALLOWED_EXTS_OF_CH);
1477 if (ret != 0) {
1478 return ret;
1479 }
Jerry Yue18dc7e2022-08-04 16:29:22 +08001480
Gilles Peskine449bd832023-01-11 14:50:10 +01001481 switch (extension_type) {
XiaokangQian40a35232022-05-07 09:02:40 +00001482#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
1483 case MBEDTLS_TLS_EXT_SERVERNAME:
Gilles Peskine449bd832023-01-11 14:50:10 +01001484 MBEDTLS_SSL_DEBUG_MSG(3, ("found ServerName extension"));
1485 ret = mbedtls_ssl_parse_server_name_ext(ssl, p,
1486 extension_data_end);
1487 if (ret != 0) {
XiaokangQian40a35232022-05-07 09:02:40 +00001488 MBEDTLS_SSL_DEBUG_RET(
Gilles Peskine449bd832023-01-11 14:50:10 +01001489 1, "mbedtls_ssl_parse_servername_ext", ret);
1490 return ret;
XiaokangQian40a35232022-05-07 09:02:40 +00001491 }
XiaokangQian40a35232022-05-07 09:02:40 +00001492 break;
1493#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
1494
Valerio Setti080a22b2023-03-20 15:22:47 +01001495#if defined(PSA_WANT_ALG_ECDH)
XiaokangQian7807f9f2022-02-15 10:04:37 +00001496 case MBEDTLS_TLS_EXT_SUPPORTED_GROUPS:
Gilles Peskine449bd832023-01-11 14:50:10 +01001497 MBEDTLS_SSL_DEBUG_MSG(3, ("found supported group extension"));
XiaokangQian7807f9f2022-02-15 10:04:37 +00001498
1499 /* Supported Groups Extension
1500 *
1501 * When sent by the client, the "supported_groups" extension
1502 * indicates the named groups which the client supports,
1503 * ordered from most preferred to least preferred.
1504 */
Jerry Yuc1be19f2022-04-23 16:11:39 +08001505 ret = ssl_tls13_parse_supported_groups_ext(
Gilles Peskine449bd832023-01-11 14:50:10 +01001506 ssl, p, extension_data_end);
1507 if (ret != 0) {
1508 MBEDTLS_SSL_DEBUG_RET(1,
1509 "mbedtls_ssl_parse_supported_groups_ext", ret);
1510 return ret;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001511 }
1512
XiaokangQian7807f9f2022-02-15 10:04:37 +00001513 break;
Valerio Setti080a22b2023-03-20 15:22:47 +01001514#endif /* PSA_WANT_ALG_ECDH */
XiaokangQian7807f9f2022-02-15 10:04:37 +00001515
Valerio Setti080a22b2023-03-20 15:22:47 +01001516#if defined(PSA_WANT_ALG_ECDH)
XiaokangQian7807f9f2022-02-15 10:04:37 +00001517 case MBEDTLS_TLS_EXT_KEY_SHARE:
Gilles Peskine449bd832023-01-11 14:50:10 +01001518 MBEDTLS_SSL_DEBUG_MSG(3, ("found key share extension"));
XiaokangQian7807f9f2022-02-15 10:04:37 +00001519
1520 /*
1521 * Key Share Extension
1522 *
1523 * When sent by the client, the "key_share" extension
1524 * contains the endpoint's cryptographic parameters for
1525 * ECDHE/DHE key establishment methods.
1526 */
Jerry Yuc1be19f2022-04-23 16:11:39 +08001527 ret = ssl_tls13_parse_key_shares_ext(
Gilles Peskine449bd832023-01-11 14:50:10 +01001528 ssl, p, extension_data_end);
1529 if (ret == SSL_TLS1_3_PARSE_KEY_SHARES_EXT_NO_MATCH) {
1530 MBEDTLS_SSL_DEBUG_MSG(2, ("HRR needed "));
Jerry Yu49ca9282022-05-05 11:05:22 +08001531 hrr_required = 1;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001532 }
1533
Gilles Peskine449bd832023-01-11 14:50:10 +01001534 if (ret < 0) {
Jerry Yu582dd062022-04-22 21:59:01 +08001535 MBEDTLS_SSL_DEBUG_RET(
Gilles Peskine449bd832023-01-11 14:50:10 +01001536 1, "ssl_tls13_parse_key_shares_ext", ret);
1537 return ret;
Jerry Yu582dd062022-04-22 21:59:01 +08001538 }
XiaokangQian7807f9f2022-02-15 10:04:37 +00001539
XiaokangQian7807f9f2022-02-15 10:04:37 +00001540 break;
Valerio Setti080a22b2023-03-20 15:22:47 +01001541#endif /* PSA_WANT_ALG_ECDH */
XiaokangQian7807f9f2022-02-15 10:04:37 +00001542
1543 case MBEDTLS_TLS_EXT_SUPPORTED_VERSIONS:
Gilles Peskine449bd832023-01-11 14:50:10 +01001544 MBEDTLS_SSL_DEBUG_MSG(3, ("found supported versions extension"));
XiaokangQian7807f9f2022-02-15 10:04:37 +00001545
1546 ret = ssl_tls13_parse_supported_versions_ext(
Gilles Peskine449bd832023-01-11 14:50:10 +01001547 ssl, p, extension_data_end);
1548 if (ret != 0) {
1549 MBEDTLS_SSL_DEBUG_RET(1,
1550 ("ssl_tls13_parse_supported_versions_ext"), ret);
1551 return ret;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001552 }
XiaokangQian7807f9f2022-02-15 10:04:37 +00001553 break;
1554
Ronald Cron41a443a2022-10-04 16:38:25 +02001555#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED)
Jerry Yue19e3b92022-07-08 12:04:51 +00001556 case MBEDTLS_TLS_EXT_PSK_KEY_EXCHANGE_MODES:
Gilles Peskine449bd832023-01-11 14:50:10 +01001557 MBEDTLS_SSL_DEBUG_MSG(3, ("found psk key exchange modes extension"));
Jerry Yue19e3b92022-07-08 12:04:51 +00001558
1559 ret = ssl_tls13_parse_key_exchange_modes_ext(
Gilles Peskine449bd832023-01-11 14:50:10 +01001560 ssl, p, extension_data_end);
1561 if (ret != 0) {
Jerry Yue19e3b92022-07-08 12:04:51 +00001562 MBEDTLS_SSL_DEBUG_RET(
Gilles Peskine449bd832023-01-11 14:50:10 +01001563 1, "ssl_tls13_parse_key_exchange_modes_ext", ret);
1564 return ret;
Jerry Yue19e3b92022-07-08 12:04:51 +00001565 }
1566
Jerry Yue19e3b92022-07-08 12:04:51 +00001567 break;
Ronald Cron41a443a2022-10-04 16:38:25 +02001568#endif
Jerry Yue19e3b92022-07-08 12:04:51 +00001569
Jerry Yu1c105562022-07-10 06:32:38 +00001570 case MBEDTLS_TLS_EXT_PRE_SHARED_KEY:
Gilles Peskine449bd832023-01-11 14:50:10 +01001571 MBEDTLS_SSL_DEBUG_MSG(3, ("found pre_shared_key extension"));
1572 if ((handshake->received_extensions &
1573 MBEDTLS_SSL_EXT_MASK(PSK_KEY_EXCHANGE_MODES)) == 0) {
Jerry Yu13ab81d2022-07-22 23:17:11 +08001574 MBEDTLS_SSL_PEND_FATAL_ALERT(
1575 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER,
Gilles Peskine449bd832023-01-11 14:50:10 +01001576 MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER);
1577 return MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER;
Jerry Yu13ab81d2022-07-22 23:17:11 +08001578 }
Ronald Cron41a443a2022-10-04 16:38:25 +02001579#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED)
Jerry Yu1c105562022-07-10 06:32:38 +00001580 /* Delay processing of the PSK identity once we have
1581 * found out which algorithms to use. We keep a pointer
1582 * to the buffer and the size for later processing.
1583 */
Jerry Yu29d9faa2022-08-23 17:52:45 +08001584 pre_shared_key_ext = p;
Jerry Yu1c105562022-07-10 06:32:38 +00001585 pre_shared_key_ext_end = extension_data_end;
Jerry Yue18dc7e2022-08-04 16:29:22 +08001586#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED */
Jerry Yu1c105562022-07-10 06:32:38 +00001587 break;
Jerry Yu1c105562022-07-10 06:32:38 +00001588
XiaokangQianacb39922022-06-17 10:18:48 +00001589#if defined(MBEDTLS_SSL_ALPN)
1590 case MBEDTLS_TLS_EXT_ALPN:
Gilles Peskine449bd832023-01-11 14:50:10 +01001591 MBEDTLS_SSL_DEBUG_MSG(3, ("found alpn extension"));
XiaokangQianacb39922022-06-17 10:18:48 +00001592
Gilles Peskine449bd832023-01-11 14:50:10 +01001593 ret = mbedtls_ssl_parse_alpn_ext(ssl, p, extension_data_end);
1594 if (ret != 0) {
XiaokangQianacb39922022-06-17 10:18:48 +00001595 MBEDTLS_SSL_DEBUG_RET(
Gilles Peskine449bd832023-01-11 14:50:10 +01001596 1, ("mbedtls_ssl_parse_alpn_ext"), ret);
1597 return ret;
XiaokangQianacb39922022-06-17 10:18:48 +00001598 }
XiaokangQianacb39922022-06-17 10:18:48 +00001599 break;
1600#endif /* MBEDTLS_SSL_ALPN */
1601
Ronald Cron928cbd32022-10-04 16:14:26 +02001602#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED)
XiaokangQian7807f9f2022-02-15 10:04:37 +00001603 case MBEDTLS_TLS_EXT_SIG_ALG:
Gilles Peskine449bd832023-01-11 14:50:10 +01001604 MBEDTLS_SSL_DEBUG_MSG(3, ("found signature_algorithms extension"));
XiaokangQian7807f9f2022-02-15 10:04:37 +00001605
Gabor Mezei078e8032022-04-27 21:17:56 +02001606 ret = mbedtls_ssl_parse_sig_alg_ext(
Gilles Peskine449bd832023-01-11 14:50:10 +01001607 ssl, p, extension_data_end);
1608 if (ret != 0) {
1609 MBEDTLS_SSL_DEBUG_MSG(1,
1610 (
1611 "ssl_parse_supported_signature_algorithms_server_ext ( %d )",
1612 ret));
1613 return ret;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001614 }
XiaokangQian7807f9f2022-02-15 10:04:37 +00001615 break;
Ronald Cron928cbd32022-10-04 16:14:26 +02001616#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED */
XiaokangQian7807f9f2022-02-15 10:04:37 +00001617
Jan Bruckner151f6422023-02-10 12:45:19 +01001618#if defined(MBEDTLS_SSL_RECORD_SIZE_LIMIT)
1619 case MBEDTLS_TLS_EXT_RECORD_SIZE_LIMIT:
1620 MBEDTLS_SSL_DEBUG_MSG(3, ("found record_size_limit extension"));
1621
1622 ret = mbedtls_ssl_tls13_parse_record_size_limit_ext(ssl, p, extension_data_end);
1623
Jan Bruckner1a38e542023-03-15 14:15:11 +01001624 /* TODO: Return unconditionally here until we handle the record size limit correctly.
1625 * Once handled correctly, only return in case of errors. */
Jan Bruckner151f6422023-02-10 12:45:19 +01001626 return ret;
1627
1628 break;
1629#endif /* MBEDTLS_SSL_RECORD_SIZE_LIMIT */
1630
XiaokangQian7807f9f2022-02-15 10:04:37 +00001631 default:
Jerry Yu79aa7212022-11-08 21:30:21 +08001632 MBEDTLS_SSL_PRINT_EXT(
Jerry Yu63a459c2022-10-31 13:38:40 +08001633 3, MBEDTLS_SSL_HS_CLIENT_HELLO,
Gilles Peskine449bd832023-01-11 14:50:10 +01001634 extension_type, "( ignored )");
Jerry Yu0c354a22022-08-29 15:25:36 +08001635 break;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001636 }
1637
1638 p += extension_data_len;
1639 }
1640
Gilles Peskine449bd832023-01-11 14:50:10 +01001641 MBEDTLS_SSL_PRINT_EXTS(3, MBEDTLS_SSL_HS_CLIENT_HELLO,
1642 handshake->received_extensions);
Jerry Yue18dc7e2022-08-04 16:29:22 +08001643
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +01001644 ret = mbedtls_ssl_add_hs_hdr_to_checksum(ssl,
1645 MBEDTLS_SSL_HS_CLIENT_HELLO,
1646 p - buf);
1647 if (0 != ret) {
1648 MBEDTLS_SSL_DEBUG_RET(1, ("mbedtls_ssl_add_hs_hdr_to_checksum"), ret);
1649 return ret;
1650 }
Jerry Yu032b15ce2022-07-11 06:10:03 +00001651
Ronald Cron41a443a2022-10-04 16:38:25 +02001652#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED)
XiaokangQian7807f9f2022-02-15 10:04:37 +00001653 /* Update checksum with either
1654 * - The entire content of the CH message, if no PSK extension is present
1655 * - The content up to but excluding the PSK extension, if present.
1656 */
Jerry Yu1c105562022-07-10 06:32:38 +00001657 /* If we've settled on a PSK-based exchange, parse PSK identity ext */
Gilles Peskine449bd832023-01-11 14:50:10 +01001658 if (mbedtls_ssl_tls13_some_psk_enabled(ssl) &&
1659 mbedtls_ssl_conf_tls13_some_psk_enabled(ssl) &&
1660 (handshake->received_extensions & MBEDTLS_SSL_EXT_MASK(PRE_SHARED_KEY))) {
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +01001661 ret = handshake->update_checksum(ssl, buf,
1662 pre_shared_key_ext - buf);
1663 if (0 != ret) {
1664 MBEDTLS_SSL_DEBUG_RET(1, ("update_checksum"), ret);
1665 return ret;
1666 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001667 ret = ssl_tls13_parse_pre_shared_key_ext(ssl,
1668 pre_shared_key_ext,
1669 pre_shared_key_ext_end,
1670 cipher_suites,
1671 cipher_suites_end);
1672 if (ret == MBEDTLS_ERR_SSL_UNKNOWN_IDENTITY) {
1673 handshake->received_extensions &= ~MBEDTLS_SSL_EXT_MASK(PRE_SHARED_KEY);
1674 } else if (ret != 0) {
Jerry Yu63a459c2022-10-31 13:38:40 +08001675 MBEDTLS_SSL_DEBUG_RET(
Gilles Peskine449bd832023-01-11 14:50:10 +01001676 1, "ssl_tls13_parse_pre_shared_key_ext", ret);
1677 return ret;
Jerry Yu1c105562022-07-10 06:32:38 +00001678 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001679 } else
Ronald Cron41a443a2022-10-04 16:38:25 +02001680#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED */
Jerry Yu1c105562022-07-10 06:32:38 +00001681 {
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +01001682 ret = handshake->update_checksum(ssl, buf, p - buf);
1683 if (0 != ret) {
1684 MBEDTLS_SSL_DEBUG_RET(1, ("update_checksum"), ret);
1685 return ret;
1686 }
Jerry Yu1c105562022-07-10 06:32:38 +00001687 }
XiaokangQian7807f9f2022-02-15 10:04:37 +00001688
Gilles Peskine449bd832023-01-11 14:50:10 +01001689 ret = ssl_tls13_determine_key_exchange_mode(ssl);
1690 if (ret < 0) {
1691 return ret;
1692 }
XiaokangQian7807f9f2022-02-15 10:04:37 +00001693
Gilles Peskine449bd832023-01-11 14:50:10 +01001694 mbedtls_ssl_optimize_checksum(ssl, handshake->ciphersuite_info);
Jerry Yue5834fd2022-08-29 20:16:09 +08001695
Gilles Peskine449bd832023-01-11 14:50:10 +01001696 return hrr_required ? SSL_CLIENT_HELLO_HRR_REQUIRED : SSL_CLIENT_HELLO_OK;
XiaokangQian75fe8c72022-06-15 09:42:45 +00001697}
1698
1699/* Update the handshake state machine */
1700
Ronald Cronce7d76e2022-07-08 18:56:49 +02001701MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01001702static int ssl_tls13_postprocess_client_hello(mbedtls_ssl_context *ssl)
XiaokangQian75fe8c72022-06-15 09:42:45 +00001703{
1704 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1705
XiaokangQian7807f9f2022-02-15 10:04:37 +00001706 /*
XiaokangQianfb665a82022-06-15 03:57:21 +00001707 * Server certificate selection
1708 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001709 if (ssl->conf->f_cert_cb && (ret = ssl->conf->f_cert_cb(ssl)) != 0) {
1710 MBEDTLS_SSL_DEBUG_RET(1, "f_cert_cb", ret);
1711 return ret;
XiaokangQianfb665a82022-06-15 03:57:21 +00001712 }
1713#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
1714 ssl->handshake->sni_name = NULL;
1715 ssl->handshake->sni_name_len = 0;
1716#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
XiaokangQian7807f9f2022-02-15 10:04:37 +00001717
Gilles Peskine449bd832023-01-11 14:50:10 +01001718 ret = mbedtls_ssl_tls13_key_schedule_stage_early(ssl);
1719 if (ret != 0) {
1720 MBEDTLS_SSL_DEBUG_RET(1,
1721 "mbedtls_ssl_tls1_3_key_schedule_stage_early", ret);
1722 return ret;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001723 }
1724
Gilles Peskine449bd832023-01-11 14:50:10 +01001725 return 0;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001726
1727}
1728
1729/*
XiaokangQianed582dd2022-04-13 08:21:05 +00001730 * Main entry point from the state machine; orchestrates the otherfunctions.
1731 */
1732
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001733MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01001734static int ssl_tls13_process_client_hello(mbedtls_ssl_context *ssl)
XiaokangQianed582dd2022-04-13 08:21:05 +00001735{
1736
XiaokangQian08037552022-04-20 07:16:41 +00001737 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine449bd832023-01-11 14:50:10 +01001738 unsigned char *buf = NULL;
XiaokangQianed582dd2022-04-13 08:21:05 +00001739 size_t buflen = 0;
Jerry Yu4ca91402022-05-09 15:50:57 +08001740 int parse_client_hello_ret;
1741
Gilles Peskine449bd832023-01-11 14:50:10 +01001742 MBEDTLS_SSL_DEBUG_MSG(2, ("=> parse client hello"));
XiaokangQianed582dd2022-04-13 08:21:05 +00001743
Gilles Peskine449bd832023-01-11 14:50:10 +01001744 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_tls13_fetch_handshake_msg(
1745 ssl, MBEDTLS_SSL_HS_CLIENT_HELLO,
1746 &buf, &buflen));
XiaokangQianed582dd2022-04-13 08:21:05 +00001747
Gilles Peskine449bd832023-01-11 14:50:10 +01001748 MBEDTLS_SSL_PROC_CHK_NEG(ssl_tls13_parse_client_hello(ssl, buf,
1749 buf + buflen));
Jerry Yuf41553b2022-05-09 22:20:30 +08001750 parse_client_hello_ret = ret; /* Store return value of parse_client_hello,
1751 * only SSL_CLIENT_HELLO_OK or
1752 * SSL_CLIENT_HELLO_HRR_REQUIRED at this
1753 * stage as negative error codes are handled
1754 * by MBEDTLS_SSL_PROC_CHK_NEG. */
Jerry Yu4ca91402022-05-09 15:50:57 +08001755
Gilles Peskine449bd832023-01-11 14:50:10 +01001756 MBEDTLS_SSL_PROC_CHK(ssl_tls13_postprocess_client_hello(ssl));
Jerry Yu582dd062022-04-22 21:59:01 +08001757
Gilles Peskine449bd832023-01-11 14:50:10 +01001758 if (parse_client_hello_ret == SSL_CLIENT_HELLO_OK) {
1759 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_SERVER_HELLO);
1760 } else {
1761 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_HELLO_RETRY_REQUEST);
1762 }
XiaokangQianed582dd2022-04-13 08:21:05 +00001763
1764cleanup:
1765
Gilles Peskine449bd832023-01-11 14:50:10 +01001766 MBEDTLS_SSL_DEBUG_MSG(2, ("<= parse client hello"));
1767 return ret;
XiaokangQianed582dd2022-04-13 08:21:05 +00001768}
1769
1770/*
Jerry Yu1c3e6882022-04-20 21:23:40 +08001771 * Handler for MBEDTLS_SSL_SERVER_HELLO
Jerry Yu5b64ae92022-03-30 17:15:02 +08001772 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001773MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01001774static int ssl_tls13_prepare_server_hello(mbedtls_ssl_context *ssl)
Jerry Yuf4b27e42022-03-30 17:32:21 +08001775{
Jerry Yu637a3f12022-04-20 21:37:58 +08001776 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1777 unsigned char *server_randbytes =
Gilles Peskine449bd832023-01-11 14:50:10 +01001778 ssl->handshake->randbytes + MBEDTLS_CLIENT_HELLO_RANDOM_LEN;
1779 if (ssl->conf->f_rng == NULL) {
1780 MBEDTLS_SSL_DEBUG_MSG(1, ("no RNG provided"));
1781 return MBEDTLS_ERR_SSL_NO_RNG;
Jerry Yuf4b27e42022-03-30 17:32:21 +08001782 }
1783
Gilles Peskine449bd832023-01-11 14:50:10 +01001784 if ((ret = ssl->conf->f_rng(ssl->conf->p_rng, server_randbytes,
1785 MBEDTLS_SERVER_HELLO_RANDOM_LEN)) != 0) {
1786 MBEDTLS_SSL_DEBUG_RET(1, "f_rng", ret);
1787 return ret;
Jerry Yuf4b27e42022-03-30 17:32:21 +08001788 }
1789
Gilles Peskine449bd832023-01-11 14:50:10 +01001790 MBEDTLS_SSL_DEBUG_BUF(3, "server hello, random bytes", server_randbytes,
1791 MBEDTLS_SERVER_HELLO_RANDOM_LEN);
Jerry Yuf4b27e42022-03-30 17:32:21 +08001792
1793#if defined(MBEDTLS_HAVE_TIME)
Gilles Peskine449bd832023-01-11 14:50:10 +01001794 ssl->session_negotiate->start = time(NULL);
Jerry Yuf4b27e42022-03-30 17:32:21 +08001795#endif /* MBEDTLS_HAVE_TIME */
1796
Gilles Peskine449bd832023-01-11 14:50:10 +01001797 return ret;
Jerry Yuf4b27e42022-03-30 17:32:21 +08001798}
1799
Jerry Yu3bf2c642022-03-30 22:02:12 +08001800/*
Jerry Yue74e04a2022-04-21 09:23:16 +08001801 * ssl_tls13_write_server_hello_supported_versions_ext ():
Jerry Yu3bf2c642022-03-30 22:02:12 +08001802 *
1803 * struct {
Jerry Yufb9f54d2022-04-06 10:08:34 +08001804 * ProtocolVersion selected_version;
Jerry Yu3bf2c642022-03-30 22:02:12 +08001805 * } SupportedVersions;
1806 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001807MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yue74e04a2022-04-21 09:23:16 +08001808static int ssl_tls13_write_server_hello_supported_versions_ext(
Gilles Peskine449bd832023-01-11 14:50:10 +01001809 mbedtls_ssl_context *ssl,
1810 unsigned char *buf,
1811 unsigned char *end,
1812 size_t *out_len)
Jerry Yu3bf2c642022-03-30 22:02:12 +08001813{
Jerry Yu3bf2c642022-03-30 22:02:12 +08001814 *out_len = 0;
1815
Gilles Peskine449bd832023-01-11 14:50:10 +01001816 MBEDTLS_SSL_DEBUG_MSG(3, ("server hello, write selected version"));
Jerry Yu3bf2c642022-03-30 22:02:12 +08001817
1818 /* Check if we have space to write the extension:
1819 * - extension_type (2 bytes)
1820 * - extension_data_length (2 bytes)
Jerry Yu349a6132022-04-14 20:52:56 +08001821 * - selected_version (2 bytes)
Jerry Yu3bf2c642022-03-30 22:02:12 +08001822 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001823 MBEDTLS_SSL_CHK_BUF_PTR(buf, end, 6);
Jerry Yu3bf2c642022-03-30 22:02:12 +08001824
Gilles Peskine449bd832023-01-11 14:50:10 +01001825 MBEDTLS_PUT_UINT16_BE(MBEDTLS_TLS_EXT_SUPPORTED_VERSIONS, buf, 0);
Jerry Yu3bf2c642022-03-30 22:02:12 +08001826
Gilles Peskine449bd832023-01-11 14:50:10 +01001827 MBEDTLS_PUT_UINT16_BE(2, buf, 2);
Jerry Yu3bf2c642022-03-30 22:02:12 +08001828
Gilles Peskine449bd832023-01-11 14:50:10 +01001829 mbedtls_ssl_write_version(buf + 4,
1830 ssl->conf->transport,
1831 ssl->tls_version);
Jerry Yu3bf2c642022-03-30 22:02:12 +08001832
Gilles Peskine449bd832023-01-11 14:50:10 +01001833 MBEDTLS_SSL_DEBUG_MSG(3, ("supported version: [%04x]",
1834 ssl->tls_version));
Jerry Yu3bf2c642022-03-30 22:02:12 +08001835
Jerry Yu349a6132022-04-14 20:52:56 +08001836 *out_len = 6;
Jerry Yu3bf2c642022-03-30 22:02:12 +08001837
Jerry Yub95dd362022-11-08 21:19:34 +08001838 mbedtls_ssl_tls13_set_hs_sent_ext_mask(
Gilles Peskine449bd832023-01-11 14:50:10 +01001839 ssl, MBEDTLS_TLS_EXT_SUPPORTED_VERSIONS);
Jerry Yub95dd362022-11-08 21:19:34 +08001840
Gilles Peskine449bd832023-01-11 14:50:10 +01001841 return 0;
Jerry Yu3bf2c642022-03-30 22:02:12 +08001842}
1843
Jerry Yud9436a12022-04-20 22:28:09 +08001844
Jerry Yu3bf2c642022-03-30 22:02:12 +08001845
1846/* Generate and export a single key share. For hybrid KEMs, this can
1847 * be called multiple times with the different components of the hybrid. */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001848MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01001849static int ssl_tls13_generate_and_write_key_share(mbedtls_ssl_context *ssl,
1850 uint16_t named_group,
1851 unsigned char *buf,
1852 unsigned char *end,
1853 size_t *out_len)
Jerry Yu3bf2c642022-03-30 22:02:12 +08001854{
1855 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yu955ddd72022-04-22 22:27:33 +08001856
1857 *out_len = 0;
1858
Valerio Setti080a22b2023-03-20 15:22:47 +01001859#if defined(PSA_WANT_ALG_ECDH)
Gilles Peskine449bd832023-01-11 14:50:10 +01001860 if (mbedtls_ssl_tls13_named_group_is_ecdhe(named_group)) {
Jerry Yu89e103c2022-03-30 22:43:29 +08001861 ret = mbedtls_ssl_tls13_generate_and_write_ecdh_key_exchange(
Gilles Peskine449bd832023-01-11 14:50:10 +01001862 ssl, named_group, buf, end, out_len);
1863 if (ret != 0) {
Jerry Yu89e103c2022-03-30 22:43:29 +08001864 MBEDTLS_SSL_DEBUG_RET(
1865 1, "mbedtls_ssl_tls13_generate_and_write_ecdh_key_exchange",
Gilles Peskine449bd832023-01-11 14:50:10 +01001866 ret);
1867 return ret;
Jerry Yu3bf2c642022-03-30 22:02:12 +08001868 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001869 } else
Valerio Setti080a22b2023-03-20 15:22:47 +01001870#endif /* PSA_WANT_ALG_ECDH */
Gilles Peskine449bd832023-01-11 14:50:10 +01001871 if (0 /* Other kinds of KEMs */) {
1872 } else {
Jerry Yu955ddd72022-04-22 22:27:33 +08001873 ((void) ssl);
1874 ((void) named_group);
1875 ((void) buf);
1876 ((void) end);
Jerry Yu3bf2c642022-03-30 22:02:12 +08001877 ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
1878 }
1879
Gilles Peskine449bd832023-01-11 14:50:10 +01001880 return ret;
Jerry Yu3bf2c642022-03-30 22:02:12 +08001881}
1882
1883/*
1884 * ssl_tls13_write_key_share_ext
1885 *
1886 * Structure of key_share extension in ServerHello:
1887 *
Jerry Yu1c3e6882022-04-20 21:23:40 +08001888 * struct {
1889 * NamedGroup group;
1890 * opaque key_exchange<1..2^16-1>;
1891 * } KeyShareEntry;
1892 * struct {
1893 * KeyShareEntry server_share;
1894 * } KeyShareServerHello;
Jerry Yu3bf2c642022-03-30 22:02:12 +08001895 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001896MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01001897static int ssl_tls13_write_key_share_ext(mbedtls_ssl_context *ssl,
1898 unsigned char *buf,
1899 unsigned char *end,
1900 size_t *out_len)
Jerry Yu3bf2c642022-03-30 22:02:12 +08001901{
Jerry Yu955ddd72022-04-22 22:27:33 +08001902 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yu3bf2c642022-03-30 22:02:12 +08001903 unsigned char *p = buf;
Jerry Yu57d48412022-04-20 21:50:42 +08001904 uint16_t group = ssl->handshake->offered_group_id;
Jerry Yu3bf2c642022-03-30 22:02:12 +08001905 unsigned char *server_share = buf + 4;
Jerry Yu3bf2c642022-03-30 22:02:12 +08001906 size_t key_exchange_length;
Jerry Yu3bf2c642022-03-30 22:02:12 +08001907
1908 *out_len = 0;
1909
Gilles Peskine449bd832023-01-11 14:50:10 +01001910 MBEDTLS_SSL_DEBUG_MSG(3, ("server hello, adding key share extension"));
Jerry Yu3bf2c642022-03-30 22:02:12 +08001911
Gilles Peskine449bd832023-01-11 14:50:10 +01001912 MBEDTLS_SSL_DEBUG_MSG(2, ("server hello, write selected_group: %s (%04x)",
1913 mbedtls_ssl_named_group_to_str(group),
1914 group));
Jerry Yu58af2332022-09-06 11:19:31 +08001915
Jerry Yu3bf2c642022-03-30 22:02:12 +08001916 /* Check if we have space for header and length fields:
1917 * - extension_type (2 bytes)
1918 * - extension_data_length (2 bytes)
1919 * - group (2 bytes)
1920 * - key_exchange_length (2 bytes)
1921 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001922 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 8);
1923 MBEDTLS_PUT_UINT16_BE(MBEDTLS_TLS_EXT_KEY_SHARE, p, 0);
1924 MBEDTLS_PUT_UINT16_BE(group, server_share, 0);
Jerry Yu3bf2c642022-03-30 22:02:12 +08001925 p += 8;
Jerry Yu57d48412022-04-20 21:50:42 +08001926
Jerry Yu3bf2c642022-03-30 22:02:12 +08001927 /* When we introduce PQC-ECDHE hybrids, we'll want to call this
1928 * function multiple times. */
Jerry Yu955ddd72022-04-22 22:27:33 +08001929 ret = ssl_tls13_generate_and_write_key_share(
Gilles Peskine449bd832023-01-11 14:50:10 +01001930 ssl, group, server_share + 4, end, &key_exchange_length);
1931 if (ret != 0) {
1932 return ret;
1933 }
Jerry Yu3bf2c642022-03-30 22:02:12 +08001934 p += key_exchange_length;
Jerry Yuc1be19f2022-04-23 16:11:39 +08001935
Gilles Peskine449bd832023-01-11 14:50:10 +01001936 MBEDTLS_PUT_UINT16_BE(key_exchange_length, server_share + 2, 0);
Jerry Yu3bf2c642022-03-30 22:02:12 +08001937
Gilles Peskine449bd832023-01-11 14:50:10 +01001938 MBEDTLS_PUT_UINT16_BE(p - server_share, buf, 2);
Jerry Yu3bf2c642022-03-30 22:02:12 +08001939
Jerry Yu57d48412022-04-20 21:50:42 +08001940 *out_len = p - buf;
Jerry Yu955ddd72022-04-22 22:27:33 +08001941
Gilles Peskine449bd832023-01-11 14:50:10 +01001942 mbedtls_ssl_tls13_set_hs_sent_ext_mask(ssl, MBEDTLS_TLS_EXT_KEY_SHARE);
Jerry Yub95dd362022-11-08 21:19:34 +08001943
Gilles Peskine449bd832023-01-11 14:50:10 +01001944 return 0;
Jerry Yu3bf2c642022-03-30 22:02:12 +08001945}
Jerry Yud9436a12022-04-20 22:28:09 +08001946
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001947MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01001948static int ssl_tls13_write_hrr_key_share_ext(mbedtls_ssl_context *ssl,
1949 unsigned char *buf,
1950 unsigned char *end,
1951 size_t *out_len)
Jerry Yu23f7a6f2022-04-23 15:16:45 +08001952{
1953 uint16_t selected_group = ssl->handshake->hrr_selected_group;
1954 /* key_share Extension
1955 *
1956 * struct {
1957 * select (Handshake.msg_type) {
1958 * ...
1959 * case hello_retry_request:
1960 * NamedGroup selected_group;
1961 * ...
1962 * };
1963 * } KeyShare;
1964 */
1965
1966 *out_len = 0;
1967
Jerry Yub0ac10b2022-05-05 11:10:08 +08001968 /*
1969 * For a pure PSK key exchange, there is no group to agree upon. The purpose
1970 * of the HRR is then to transmit a cookie to force the client to demonstrate
1971 * reachability at their apparent network address (primarily useful for DTLS).
1972 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001973 if (!mbedtls_ssl_tls13_key_exchange_mode_with_ephemeral(ssl)) {
1974 return 0;
1975 }
Jerry Yu23f7a6f2022-04-23 15:16:45 +08001976
1977 /* We should only send the key_share extension if the client's initial
1978 * key share was not acceptable. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001979 if (ssl->handshake->offered_group_id != 0) {
1980 MBEDTLS_SSL_DEBUG_MSG(4, ("Skip key_share extension in HRR"));
1981 return 0;
Jerry Yu23f7a6f2022-04-23 15:16:45 +08001982 }
1983
Gilles Peskine449bd832023-01-11 14:50:10 +01001984 if (selected_group == 0) {
1985 MBEDTLS_SSL_DEBUG_MSG(1, ("no matching named group found"));
1986 return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
Jerry Yu23f7a6f2022-04-23 15:16:45 +08001987 }
1988
Jerry Yub0ac10b2022-05-05 11:10:08 +08001989 /* Check if we have enough space:
1990 * - extension_type (2 bytes)
1991 * - extension_data_length (2 bytes)
1992 * - selected_group (2 bytes)
1993 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001994 MBEDTLS_SSL_CHK_BUF_PTR(buf, end, 6);
Jerry Yu23f7a6f2022-04-23 15:16:45 +08001995
Gilles Peskine449bd832023-01-11 14:50:10 +01001996 MBEDTLS_PUT_UINT16_BE(MBEDTLS_TLS_EXT_KEY_SHARE, buf, 0);
1997 MBEDTLS_PUT_UINT16_BE(2, buf, 2);
1998 MBEDTLS_PUT_UINT16_BE(selected_group, buf, 4);
Jerry Yu23f7a6f2022-04-23 15:16:45 +08001999
Gilles Peskine449bd832023-01-11 14:50:10 +01002000 MBEDTLS_SSL_DEBUG_MSG(3,
2001 ("HRR selected_group: %s (%x)",
2002 mbedtls_ssl_named_group_to_str(selected_group),
2003 selected_group));
Jerry Yu23f7a6f2022-04-23 15:16:45 +08002004
2005 *out_len = 6;
Jerry Yu23f7a6f2022-04-23 15:16:45 +08002006
Gilles Peskine449bd832023-01-11 14:50:10 +01002007 mbedtls_ssl_tls13_set_hs_sent_ext_mask(ssl, MBEDTLS_TLS_EXT_KEY_SHARE);
Jerry Yub95dd362022-11-08 21:19:34 +08002008
Gilles Peskine449bd832023-01-11 14:50:10 +01002009 return 0;
Jerry Yu23f7a6f2022-04-23 15:16:45 +08002010}
Jerry Yu3bf2c642022-03-30 22:02:12 +08002011
2012/*
2013 * Structure of ServerHello message:
2014 *
2015 * struct {
2016 * ProtocolVersion legacy_version = 0x0303; // TLS v1.2
2017 * Random random;
2018 * opaque legacy_session_id_echo<0..32>;
2019 * CipherSuite cipher_suite;
2020 * uint8 legacy_compression_method = 0;
2021 * Extension extensions<6..2^16-1>;
2022 * } ServerHello;
2023 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002024MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002025static int ssl_tls13_write_server_hello_body(mbedtls_ssl_context *ssl,
2026 unsigned char *buf,
2027 unsigned char *end,
2028 size_t *out_len,
2029 int is_hrr)
Jerry Yu56404d72022-03-30 17:36:13 +08002030{
Jerry Yu955ddd72022-04-22 22:27:33 +08002031 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yu3bf2c642022-03-30 22:02:12 +08002032 unsigned char *p = buf;
Jerry Yud9436a12022-04-20 22:28:09 +08002033 unsigned char *p_extensions_len;
Jerry Yufbe3e642022-04-25 19:31:51 +08002034 size_t output_len;
Jerry Yu3bf2c642022-03-30 22:02:12 +08002035
2036 *out_len = 0;
Jerry Yu50e00e32022-10-31 14:45:01 +08002037 ssl->handshake->sent_extensions = MBEDTLS_SSL_EXT_MASK_NONE;
Jerry Yu3bf2c642022-03-30 22:02:12 +08002038
Jerry Yucfc04b32022-04-21 09:31:58 +08002039 /* ...
2040 * ProtocolVersion legacy_version = 0x0303; // TLS 1.2
2041 * ...
2042 * with ProtocolVersion defined as:
2043 * uint16 ProtocolVersion;
Jerry Yu3bf2c642022-03-30 22:02:12 +08002044 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002045 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 2);
2046 MBEDTLS_PUT_UINT16_BE(0x0303, p, 0);
Jerry Yu3bf2c642022-03-30 22:02:12 +08002047 p += 2;
2048
Jerry Yu1c3e6882022-04-20 21:23:40 +08002049 /* ...
2050 * Random random;
2051 * ...
Jerry Yucfc04b32022-04-21 09:31:58 +08002052 * with Random defined as:
2053 * opaque Random[MBEDTLS_SERVER_HELLO_RANDOM_LEN];
Jerry Yu1c3e6882022-04-20 21:23:40 +08002054 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002055 MBEDTLS_SSL_CHK_BUF_PTR(p, end, MBEDTLS_SERVER_HELLO_RANDOM_LEN);
2056 if (is_hrr) {
2057 memcpy(p, mbedtls_ssl_tls13_hello_retry_request_magic,
2058 MBEDTLS_SERVER_HELLO_RANDOM_LEN);
2059 } else {
2060 memcpy(p, &ssl->handshake->randbytes[MBEDTLS_CLIENT_HELLO_RANDOM_LEN],
2061 MBEDTLS_SERVER_HELLO_RANDOM_LEN);
Jerry Yu23f7a6f2022-04-23 15:16:45 +08002062 }
Gilles Peskine449bd832023-01-11 14:50:10 +01002063 MBEDTLS_SSL_DEBUG_BUF(3, "server hello, random bytes",
2064 p, MBEDTLS_SERVER_HELLO_RANDOM_LEN);
Jerry Yu3bf2c642022-03-30 22:02:12 +08002065 p += MBEDTLS_SERVER_HELLO_RANDOM_LEN;
2066
Jerry Yucfc04b32022-04-21 09:31:58 +08002067 /* ...
2068 * opaque legacy_session_id_echo<0..32>;
2069 * ...
Jerry Yu3bf2c642022-03-30 22:02:12 +08002070 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002071 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 1 + ssl->session_negotiate->id_len);
2072 *p++ = (unsigned char) ssl->session_negotiate->id_len;
2073 if (ssl->session_negotiate->id_len > 0) {
2074 memcpy(p, &ssl->session_negotiate->id[0],
2075 ssl->session_negotiate->id_len);
Jerry Yu3bf2c642022-03-30 22:02:12 +08002076 p += ssl->session_negotiate->id_len;
Jerry Yu955ddd72022-04-22 22:27:33 +08002077
Gilles Peskine449bd832023-01-11 14:50:10 +01002078 MBEDTLS_SSL_DEBUG_BUF(3, "session id", ssl->session_negotiate->id,
2079 ssl->session_negotiate->id_len);
Jerry Yu3bf2c642022-03-30 22:02:12 +08002080 }
2081
Jerry Yucfc04b32022-04-21 09:31:58 +08002082 /* ...
2083 * CipherSuite cipher_suite;
2084 * ...
2085 * with CipherSuite defined as:
2086 * uint8 CipherSuite[2];
Jerry Yu3bf2c642022-03-30 22:02:12 +08002087 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002088 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 2);
2089 MBEDTLS_PUT_UINT16_BE(ssl->session_negotiate->ciphersuite, p, 0);
Jerry Yu3bf2c642022-03-30 22:02:12 +08002090 p += 2;
Gilles Peskine449bd832023-01-11 14:50:10 +01002091 MBEDTLS_SSL_DEBUG_MSG(3,
2092 ("server hello, chosen ciphersuite: %s ( id=%d )",
2093 mbedtls_ssl_get_ciphersuite_name(
2094 ssl->session_negotiate->ciphersuite),
2095 ssl->session_negotiate->ciphersuite));
Jerry Yu3bf2c642022-03-30 22:02:12 +08002096
Jerry Yucfc04b32022-04-21 09:31:58 +08002097 /* ...
2098 * uint8 legacy_compression_method = 0;
2099 * ...
2100 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002101 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 1);
Thomas Daubney31e03a82022-07-25 15:59:25 +01002102 *p++ = MBEDTLS_SSL_COMPRESS_NULL;
Jerry Yu3bf2c642022-03-30 22:02:12 +08002103
Jerry Yucfc04b32022-04-21 09:31:58 +08002104 /* ...
2105 * Extension extensions<6..2^16-1>;
2106 * ...
2107 * struct {
2108 * ExtensionType extension_type; (2 bytes)
2109 * opaque extension_data<0..2^16-1>;
2110 * } Extension;
2111 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002112 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 2);
Jerry Yud9436a12022-04-20 22:28:09 +08002113 p_extensions_len = p;
Jerry Yu3bf2c642022-03-30 22:02:12 +08002114 p += 2;
2115
Gilles Peskine449bd832023-01-11 14:50:10 +01002116 if ((ret = ssl_tls13_write_server_hello_supported_versions_ext(
2117 ssl, p, end, &output_len)) != 0) {
Jerry Yu955ddd72022-04-22 22:27:33 +08002118 MBEDTLS_SSL_DEBUG_RET(
Gilles Peskine449bd832023-01-11 14:50:10 +01002119 1, "ssl_tls13_write_server_hello_supported_versions_ext", ret);
2120 return ret;
Jerry Yu3bf2c642022-03-30 22:02:12 +08002121 }
2122 p += output_len;
2123
Gilles Peskine449bd832023-01-11 14:50:10 +01002124 if (mbedtls_ssl_tls13_key_exchange_mode_with_ephemeral(ssl)) {
2125 if (is_hrr) {
2126 ret = ssl_tls13_write_hrr_key_share_ext(ssl, p, end, &output_len);
2127 } else {
2128 ret = ssl_tls13_write_key_share_ext(ssl, p, end, &output_len);
2129 }
2130 if (ret != 0) {
2131 return ret;
2132 }
Jerry Yu3bf2c642022-03-30 22:02:12 +08002133 p += output_len;
2134 }
Jerry Yu3bf2c642022-03-30 22:02:12 +08002135
Ronald Cron41a443a2022-10-04 16:38:25 +02002136#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01002137 if (!is_hrr && mbedtls_ssl_tls13_key_exchange_mode_with_psk(ssl)) {
2138 ret = ssl_tls13_write_server_pre_shared_key_ext(ssl, p, end, &output_len);
2139 if (ret != 0) {
2140 MBEDTLS_SSL_DEBUG_RET(1, "ssl_tls13_write_server_pre_shared_key_ext",
2141 ret);
2142 return ret;
Jerry Yu032b15ce2022-07-11 06:10:03 +00002143 }
2144 p += output_len;
2145 }
Ronald Cron41a443a2022-10-04 16:38:25 +02002146#endif
Jerry Yu032b15ce2022-07-11 06:10:03 +00002147
Gilles Peskine449bd832023-01-11 14:50:10 +01002148 MBEDTLS_PUT_UINT16_BE(p - p_extensions_len - 2, p_extensions_len, 0);
Jerry Yu3bf2c642022-03-30 22:02:12 +08002149
Gilles Peskine449bd832023-01-11 14:50:10 +01002150 MBEDTLS_SSL_DEBUG_BUF(4, "server hello extensions",
2151 p_extensions_len, p - p_extensions_len);
Jerry Yu3bf2c642022-03-30 22:02:12 +08002152
Jerry Yud9436a12022-04-20 22:28:09 +08002153 *out_len = p - buf;
Jerry Yu3bf2c642022-03-30 22:02:12 +08002154
Gilles Peskine449bd832023-01-11 14:50:10 +01002155 MBEDTLS_SSL_DEBUG_BUF(3, "server hello", buf, *out_len);
Jerry Yu3bf2c642022-03-30 22:02:12 +08002156
Jerry Yu7de2ff02022-11-08 21:43:46 +08002157 MBEDTLS_SSL_PRINT_EXTS(
Jerry Yu4b8f2f72022-10-31 13:31:22 +08002158 3, is_hrr ? MBEDTLS_SSL_TLS1_3_HS_HELLO_RETRY_REQUEST :
Gilles Peskine449bd832023-01-11 14:50:10 +01002159 MBEDTLS_SSL_HS_SERVER_HELLO,
2160 ssl->handshake->sent_extensions);
Jerry Yu4b8f2f72022-10-31 13:31:22 +08002161
Gilles Peskine449bd832023-01-11 14:50:10 +01002162 return ret;
Jerry Yu56404d72022-03-30 17:36:13 +08002163}
2164
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002165MBEDTLS_CHECK_RETURN_CRITICAL
Xiaokang Qian934ce6f2023-02-06 10:23:04 +00002166static int ssl_tls13_finalize_server_hello(mbedtls_ssl_context *ssl)
Jerry Yue110d252022-05-05 10:19:22 +08002167{
2168 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine449bd832023-01-11 14:50:10 +01002169 ret = mbedtls_ssl_tls13_compute_handshake_transform(ssl);
2170 if (ret != 0) {
2171 MBEDTLS_SSL_DEBUG_RET(1,
2172 "mbedtls_ssl_tls13_compute_handshake_transform",
2173 ret);
2174 return ret;
Jerry Yue110d252022-05-05 10:19:22 +08002175 }
2176
Gilles Peskine449bd832023-01-11 14:50:10 +01002177 return ret;
Jerry Yue110d252022-05-05 10:19:22 +08002178}
2179
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002180MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002181static int ssl_tls13_write_server_hello(mbedtls_ssl_context *ssl)
Jerry Yu5b64ae92022-03-30 17:15:02 +08002182{
Jerry Yu637a3f12022-04-20 21:37:58 +08002183 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yuf4b27e42022-03-30 17:32:21 +08002184 unsigned char *buf;
2185 size_t buf_len, msg_len;
2186
Gilles Peskine449bd832023-01-11 14:50:10 +01002187 MBEDTLS_SSL_DEBUG_MSG(2, ("=> write server hello"));
Jerry Yuf4b27e42022-03-30 17:32:21 +08002188
Gilles Peskine449bd832023-01-11 14:50:10 +01002189 MBEDTLS_SSL_PROC_CHK(ssl_tls13_prepare_server_hello(ssl));
Jerry Yuf4b27e42022-03-30 17:32:21 +08002190
Gilles Peskine449bd832023-01-11 14:50:10 +01002191 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_start_handshake_msg(ssl,
2192 MBEDTLS_SSL_HS_SERVER_HELLO, &buf,
2193 &buf_len));
Jerry Yuf4b27e42022-03-30 17:32:21 +08002194
Gilles Peskine449bd832023-01-11 14:50:10 +01002195 MBEDTLS_SSL_PROC_CHK(ssl_tls13_write_server_hello_body(ssl, buf,
2196 buf + buf_len,
2197 &msg_len,
2198 0));
Jerry Yuf4b27e42022-03-30 17:32:21 +08002199
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +01002200 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_add_hs_msg_to_checksum(
Manuel Pégourié-Gonnard43cc1272023-02-06 11:48:19 +01002201 ssl, MBEDTLS_SSL_HS_SERVER_HELLO, buf, msg_len));
Jerry Yuf4b27e42022-03-30 17:32:21 +08002202
Gilles Peskine449bd832023-01-11 14:50:10 +01002203 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_finish_handshake_msg(
2204 ssl, buf_len, msg_len));
Jerry Yu637a3f12022-04-20 21:37:58 +08002205
Xiaokang Qian934ce6f2023-02-06 10:23:04 +00002206 MBEDTLS_SSL_PROC_CHK(ssl_tls13_finalize_server_hello(ssl));
Jerry Yue110d252022-05-05 10:19:22 +08002207
Gabor Mezei7b39bf12022-05-24 16:04:14 +02002208#if defined(MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE)
2209 /* The server sends a dummy change_cipher_spec record immediately
2210 * after its first handshake message. This may either be after
2211 * a ServerHello or a HelloRetryRequest.
2212 */
Gabor Mezei96ae9262022-06-28 11:45:18 +02002213 mbedtls_ssl_handshake_set_state(
Gilles Peskine449bd832023-01-11 14:50:10 +01002214 ssl, MBEDTLS_SSL_SERVER_CCS_AFTER_SERVER_HELLO);
Gabor Mezei7b39bf12022-05-24 16:04:14 +02002215#else
Gilles Peskine449bd832023-01-11 14:50:10 +01002216 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_ENCRYPTED_EXTENSIONS);
Gabor Mezei7b39bf12022-05-24 16:04:14 +02002217#endif /* MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE */
Jerry Yue110d252022-05-05 10:19:22 +08002218
Jerry Yuf4b27e42022-03-30 17:32:21 +08002219cleanup:
2220
Gilles Peskine449bd832023-01-11 14:50:10 +01002221 MBEDTLS_SSL_DEBUG_MSG(2, ("<= write server hello"));
2222 return ret;
Jerry Yu5b64ae92022-03-30 17:15:02 +08002223}
2224
Jerry Yu23d1a252022-05-12 18:08:59 +08002225
2226/*
2227 * Handler for MBEDTLS_SSL_HELLO_RETRY_REQUEST
2228 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002229MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002230static int ssl_tls13_prepare_hello_retry_request(mbedtls_ssl_context *ssl)
Jerry Yu23d1a252022-05-12 18:08:59 +08002231{
2232 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine449bd832023-01-11 14:50:10 +01002233 if (ssl->handshake->hello_retry_request_count > 0) {
2234 MBEDTLS_SSL_DEBUG_MSG(1, ("Too many HRRs"));
2235 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE,
2236 MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE);
2237 return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
Jerry Yu23d1a252022-05-12 18:08:59 +08002238 }
2239
2240 /*
2241 * Create stateless transcript hash for HRR
2242 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002243 MBEDTLS_SSL_DEBUG_MSG(4, ("Reset transcript for HRR"));
2244 ret = mbedtls_ssl_reset_transcript_for_hrr(ssl);
2245 if (ret != 0) {
2246 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_reset_transcript_for_hrr", ret);
2247 return ret;
Jerry Yu23d1a252022-05-12 18:08:59 +08002248 }
Gilles Peskine449bd832023-01-11 14:50:10 +01002249 mbedtls_ssl_session_reset_msg_layer(ssl, 0);
Jerry Yu23d1a252022-05-12 18:08:59 +08002250
Gilles Peskine449bd832023-01-11 14:50:10 +01002251 return 0;
Jerry Yu23d1a252022-05-12 18:08:59 +08002252}
2253
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002254MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002255static int ssl_tls13_write_hello_retry_request(mbedtls_ssl_context *ssl)
Jerry Yu23d1a252022-05-12 18:08:59 +08002256{
2257 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
2258 unsigned char *buf;
2259 size_t buf_len, msg_len;
2260
Gilles Peskine449bd832023-01-11 14:50:10 +01002261 MBEDTLS_SSL_DEBUG_MSG(2, ("=> write hello retry request"));
Jerry Yu23d1a252022-05-12 18:08:59 +08002262
Gilles Peskine449bd832023-01-11 14:50:10 +01002263 MBEDTLS_SSL_PROC_CHK(ssl_tls13_prepare_hello_retry_request(ssl));
Jerry Yu23d1a252022-05-12 18:08:59 +08002264
Gilles Peskine449bd832023-01-11 14:50:10 +01002265 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_start_handshake_msg(
2266 ssl, MBEDTLS_SSL_HS_SERVER_HELLO,
2267 &buf, &buf_len));
Jerry Yu23d1a252022-05-12 18:08:59 +08002268
Gilles Peskine449bd832023-01-11 14:50:10 +01002269 MBEDTLS_SSL_PROC_CHK(ssl_tls13_write_server_hello_body(ssl, buf,
2270 buf + buf_len,
2271 &msg_len,
2272 1));
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +01002273 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_add_hs_msg_to_checksum(
Manuel Pégourié-Gonnard43cc1272023-02-06 11:48:19 +01002274 ssl, MBEDTLS_SSL_HS_SERVER_HELLO, buf, msg_len));
Jerry Yu23d1a252022-05-12 18:08:59 +08002275
2276
Gilles Peskine449bd832023-01-11 14:50:10 +01002277 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_finish_handshake_msg(ssl, buf_len,
2278 msg_len));
Jerry Yu23d1a252022-05-12 18:08:59 +08002279
2280 ssl->handshake->hello_retry_request_count++;
2281
Gabor Mezei7b39bf12022-05-24 16:04:14 +02002282#if defined(MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE)
2283 /* The server sends a dummy change_cipher_spec record immediately
2284 * after its first handshake message. This may either be after
2285 * a ServerHello or a HelloRetryRequest.
2286 */
Gabor Mezei96ae9262022-06-28 11:45:18 +02002287 mbedtls_ssl_handshake_set_state(
Gilles Peskine449bd832023-01-11 14:50:10 +01002288 ssl, MBEDTLS_SSL_SERVER_CCS_AFTER_HELLO_RETRY_REQUEST);
Gabor Mezei7b39bf12022-05-24 16:04:14 +02002289#else
Gilles Peskine449bd832023-01-11 14:50:10 +01002290 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_CLIENT_HELLO);
Gabor Mezei7b39bf12022-05-24 16:04:14 +02002291#endif /* MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE */
Jerry Yu23d1a252022-05-12 18:08:59 +08002292
2293cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +01002294 MBEDTLS_SSL_DEBUG_MSG(2, ("<= write hello retry request"));
2295 return ret;
Jerry Yu23d1a252022-05-12 18:08:59 +08002296}
2297
Jerry Yu5b64ae92022-03-30 17:15:02 +08002298/*
Jerry Yu4d3841a2022-04-16 12:37:19 +08002299 * Handler for MBEDTLS_SSL_ENCRYPTED_EXTENSIONS
2300 */
Jerry Yu4d3841a2022-04-16 12:37:19 +08002301
2302/*
2303 * struct {
2304 * Extension extensions<0..2 ^ 16 - 1>;
2305 * } EncryptedExtensions;
2306 *
2307 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002308MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002309static int ssl_tls13_write_encrypted_extensions_body(mbedtls_ssl_context *ssl,
2310 unsigned char *buf,
2311 unsigned char *end,
2312 size_t *out_len)
Jerry Yu4d3841a2022-04-16 12:37:19 +08002313{
XiaokangQianacb39922022-06-17 10:18:48 +00002314 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yu4d3841a2022-04-16 12:37:19 +08002315 unsigned char *p = buf;
2316 size_t extensions_len = 0;
Jerry Yu8937eb42022-05-03 12:12:14 +08002317 unsigned char *p_extensions_len;
XiaokangQianacb39922022-06-17 10:18:48 +00002318 size_t output_len;
Jerry Yu9da5e5a2022-05-03 15:46:09 +08002319
Jerry Yu4d3841a2022-04-16 12:37:19 +08002320 *out_len = 0;
2321
Gilles Peskine449bd832023-01-11 14:50:10 +01002322 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 2);
Jerry Yu8937eb42022-05-03 12:12:14 +08002323 p_extensions_len = p;
Jerry Yu4d3841a2022-04-16 12:37:19 +08002324 p += 2;
2325
Jerry Yu8937eb42022-05-03 12:12:14 +08002326 ((void) ssl);
XiaokangQianacb39922022-06-17 10:18:48 +00002327 ((void) ret);
2328 ((void) output_len);
2329
2330#if defined(MBEDTLS_SSL_ALPN)
Gilles Peskine449bd832023-01-11 14:50:10 +01002331 ret = mbedtls_ssl_write_alpn_ext(ssl, p, end, &output_len);
2332 if (ret != 0) {
2333 return ret;
2334 }
XiaokangQian95d5f542022-06-24 02:29:26 +00002335 p += output_len;
XiaokangQianacb39922022-06-17 10:18:48 +00002336#endif /* MBEDTLS_SSL_ALPN */
Jerry Yu4d3841a2022-04-16 12:37:19 +08002337
Gilles Peskine449bd832023-01-11 14:50:10 +01002338 extensions_len = (p - p_extensions_len) - 2;
2339 MBEDTLS_PUT_UINT16_BE(extensions_len, p_extensions_len, 0);
Jerry Yu8937eb42022-05-03 12:12:14 +08002340
2341 *out_len = p - buf;
Jerry Yu4d3841a2022-04-16 12:37:19 +08002342
Gilles Peskine449bd832023-01-11 14:50:10 +01002343 MBEDTLS_SSL_DEBUG_BUF(4, "encrypted extensions", buf, *out_len);
Jerry Yu4d3841a2022-04-16 12:37:19 +08002344
Jerry Yu7de2ff02022-11-08 21:43:46 +08002345 MBEDTLS_SSL_PRINT_EXTS(
Gilles Peskine449bd832023-01-11 14:50:10 +01002346 3, MBEDTLS_SSL_HS_ENCRYPTED_EXTENSIONS, ssl->handshake->sent_extensions);
Jerry Yu4b8f2f72022-10-31 13:31:22 +08002347
Gilles Peskine449bd832023-01-11 14:50:10 +01002348 return 0;
Jerry Yu4d3841a2022-04-16 12:37:19 +08002349}
2350
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002351MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002352static int ssl_tls13_write_encrypted_extensions(mbedtls_ssl_context *ssl)
Jerry Yu4d3841a2022-04-16 12:37:19 +08002353{
2354 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
2355 unsigned char *buf;
Jerry Yu39730a72022-05-03 12:14:04 +08002356 size_t buf_len, msg_len;
Jerry Yu4d3841a2022-04-16 12:37:19 +08002357
Gilles Peskine449bd832023-01-11 14:50:10 +01002358 mbedtls_ssl_set_outbound_transform(ssl,
2359 ssl->handshake->transform_handshake);
Gabor Mezei54719122022-06-28 11:34:56 +02002360 MBEDTLS_SSL_DEBUG_MSG(
Gilles Peskine449bd832023-01-11 14:50:10 +01002361 3, ("switching to handshake transform for outbound data"));
Gabor Mezei54719122022-06-28 11:34:56 +02002362
Gilles Peskine449bd832023-01-11 14:50:10 +01002363 MBEDTLS_SSL_DEBUG_MSG(2, ("=> write encrypted extensions"));
Jerry Yu4d3841a2022-04-16 12:37:19 +08002364
Gilles Peskine449bd832023-01-11 14:50:10 +01002365 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_start_handshake_msg(ssl,
2366 MBEDTLS_SSL_HS_ENCRYPTED_EXTENSIONS, &buf,
2367 &buf_len));
Jerry Yu4d3841a2022-04-16 12:37:19 +08002368
Gilles Peskine449bd832023-01-11 14:50:10 +01002369 MBEDTLS_SSL_PROC_CHK(ssl_tls13_write_encrypted_extensions_body(
2370 ssl, buf, buf + buf_len, &msg_len));
Jerry Yu4d3841a2022-04-16 12:37:19 +08002371
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +01002372 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_add_hs_msg_to_checksum(
Manuel Pégourié-Gonnard43cc1272023-02-06 11:48:19 +01002373 ssl, MBEDTLS_SSL_HS_ENCRYPTED_EXTENSIONS, buf, msg_len));
Jerry Yu4d3841a2022-04-16 12:37:19 +08002374
Gilles Peskine449bd832023-01-11 14:50:10 +01002375 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_finish_handshake_msg(
2376 ssl, buf_len, msg_len));
Jerry Yu4d3841a2022-04-16 12:37:19 +08002377
Ronald Cron928cbd32022-10-04 16:14:26 +02002378#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01002379 if (mbedtls_ssl_tls13_key_exchange_mode_with_psk(ssl)) {
2380 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_SERVER_FINISHED);
2381 } else {
2382 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_CERTIFICATE_REQUEST);
2383 }
Jerry Yu8937eb42022-05-03 12:12:14 +08002384#else
Gilles Peskine449bd832023-01-11 14:50:10 +01002385 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_SERVER_FINISHED);
Jerry Yu8937eb42022-05-03 12:12:14 +08002386#endif
2387
Jerry Yu4d3841a2022-04-16 12:37:19 +08002388cleanup:
2389
Gilles Peskine449bd832023-01-11 14:50:10 +01002390 MBEDTLS_SSL_DEBUG_MSG(2, ("<= write encrypted extensions"));
2391 return ret;
Jerry Yu4d3841a2022-04-16 12:37:19 +08002392}
2393
Ronald Cron928cbd32022-10-04 16:14:26 +02002394#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED)
XiaokangQiana987e1d2022-05-07 01:25:58 +00002395#define SSL_CERTIFICATE_REQUEST_SEND_REQUEST 0
2396#define SSL_CERTIFICATE_REQUEST_SKIP 1
2397/* Coordination:
2398 * Check whether a CertificateRequest message should be written.
2399 * Returns a negative code on failure, or
2400 * - SSL_CERTIFICATE_REQUEST_SEND_REQUEST
2401 * - SSL_CERTIFICATE_REQUEST_SKIP
2402 * indicating if the writing of the CertificateRequest
2403 * should be skipped or not.
2404 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002405MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002406static int ssl_tls13_certificate_request_coordinate(mbedtls_ssl_context *ssl)
XiaokangQiana987e1d2022-05-07 01:25:58 +00002407{
2408 int authmode;
2409
XiaokangQian40a35232022-05-07 09:02:40 +00002410#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Gilles Peskine449bd832023-01-11 14:50:10 +01002411 if (ssl->handshake->sni_authmode != MBEDTLS_SSL_VERIFY_UNSET) {
XiaokangQian40a35232022-05-07 09:02:40 +00002412 authmode = ssl->handshake->sni_authmode;
Gilles Peskine449bd832023-01-11 14:50:10 +01002413 } else
XiaokangQian40a35232022-05-07 09:02:40 +00002414#endif
XiaokangQiana987e1d2022-05-07 01:25:58 +00002415 authmode = ssl->conf->authmode;
2416
Gilles Peskine449bd832023-01-11 14:50:10 +01002417 if (authmode == MBEDTLS_SSL_VERIFY_NONE) {
Ronald Croneac00ad2022-09-13 10:16:31 +02002418 ssl->session_negotiate->verify_result = MBEDTLS_X509_BADCERT_SKIP_VERIFY;
Gilles Peskine449bd832023-01-11 14:50:10 +01002419 return SSL_CERTIFICATE_REQUEST_SKIP;
Ronald Croneac00ad2022-09-13 10:16:31 +02002420 }
XiaokangQiana987e1d2022-05-07 01:25:58 +00002421
XiaokangQianc3017f62022-05-13 05:55:41 +00002422 ssl->handshake->certificate_request_sent = 1;
XiaokangQian189ded22022-05-10 08:12:17 +00002423
Gilles Peskine449bd832023-01-11 14:50:10 +01002424 return SSL_CERTIFICATE_REQUEST_SEND_REQUEST;
XiaokangQiana987e1d2022-05-07 01:25:58 +00002425}
2426
XiaokangQiancec9ae62022-05-06 07:28:50 +00002427/*
2428 * struct {
2429 * opaque certificate_request_context<0..2^8-1>;
2430 * Extension extensions<2..2^16-1>;
2431 * } CertificateRequest;
2432 *
2433 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002434MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002435static int ssl_tls13_write_certificate_request_body(mbedtls_ssl_context *ssl,
2436 unsigned char *buf,
2437 const unsigned char *end,
2438 size_t *out_len)
XiaokangQiancec9ae62022-05-06 07:28:50 +00002439{
2440 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
2441 unsigned char *p = buf;
XiaokangQianec6efb92022-05-06 09:53:10 +00002442 size_t output_len = 0;
XiaokangQiancec9ae62022-05-06 07:28:50 +00002443 unsigned char *p_extensions_len;
2444
2445 *out_len = 0;
2446
2447 /* Check if we have enough space:
2448 * - certificate_request_context (1 byte)
2449 * - extensions length (2 bytes)
2450 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002451 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 3);
XiaokangQiancec9ae62022-05-06 07:28:50 +00002452
2453 /*
2454 * Write certificate_request_context
2455 */
2456 /*
2457 * We use a zero length context for the normal handshake
2458 * messages. For post-authentication handshake messages
2459 * this request context would be set to a non-zero value.
2460 */
2461 *p++ = 0x0;
2462
2463 /*
2464 * Write extensions
2465 */
2466 /* The extensions must contain the signature_algorithms. */
2467 p_extensions_len = p;
2468 p += 2;
Gilles Peskine449bd832023-01-11 14:50:10 +01002469 ret = mbedtls_ssl_write_sig_alg_ext(ssl, p, end, &output_len);
2470 if (ret != 0) {
2471 return ret;
2472 }
XiaokangQiancec9ae62022-05-06 07:28:50 +00002473
XiaokangQianec6efb92022-05-06 09:53:10 +00002474 p += output_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01002475 MBEDTLS_PUT_UINT16_BE(p - p_extensions_len - 2, p_extensions_len, 0);
XiaokangQiancec9ae62022-05-06 07:28:50 +00002476
2477 *out_len = p - buf;
2478
Jerry Yu7de2ff02022-11-08 21:43:46 +08002479 MBEDTLS_SSL_PRINT_EXTS(
Gilles Peskine449bd832023-01-11 14:50:10 +01002480 3, MBEDTLS_SSL_HS_CERTIFICATE_REQUEST, ssl->handshake->sent_extensions);
Jerry Yu4b8f2f72022-10-31 13:31:22 +08002481
Gilles Peskine449bd832023-01-11 14:50:10 +01002482 return 0;
XiaokangQiancec9ae62022-05-06 07:28:50 +00002483}
2484
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002485MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002486static int ssl_tls13_write_certificate_request(mbedtls_ssl_context *ssl)
XiaokangQiancec9ae62022-05-06 07:28:50 +00002487{
2488 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
2489
Gilles Peskine449bd832023-01-11 14:50:10 +01002490 MBEDTLS_SSL_DEBUG_MSG(2, ("=> write certificate request"));
XiaokangQiancec9ae62022-05-06 07:28:50 +00002491
Gilles Peskine449bd832023-01-11 14:50:10 +01002492 MBEDTLS_SSL_PROC_CHK_NEG(ssl_tls13_certificate_request_coordinate(ssl));
XiaokangQiancec9ae62022-05-06 07:28:50 +00002493
Gilles Peskine449bd832023-01-11 14:50:10 +01002494 if (ret == SSL_CERTIFICATE_REQUEST_SEND_REQUEST) {
XiaokangQiancec9ae62022-05-06 07:28:50 +00002495 unsigned char *buf;
2496 size_t buf_len, msg_len;
2497
Gilles Peskine449bd832023-01-11 14:50:10 +01002498 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_start_handshake_msg(ssl,
2499 MBEDTLS_SSL_HS_CERTIFICATE_REQUEST,
2500 &buf, &buf_len));
XiaokangQiancec9ae62022-05-06 07:28:50 +00002501
Gilles Peskine449bd832023-01-11 14:50:10 +01002502 MBEDTLS_SSL_PROC_CHK(ssl_tls13_write_certificate_request_body(
2503 ssl, buf, buf + buf_len, &msg_len));
XiaokangQiancec9ae62022-05-06 07:28:50 +00002504
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +01002505 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_add_hs_msg_to_checksum(
Manuel Pégourié-Gonnard43cc1272023-02-06 11:48:19 +01002506 ssl, MBEDTLS_SSL_HS_CERTIFICATE_REQUEST, buf, msg_len));
XiaokangQiancec9ae62022-05-06 07:28:50 +00002507
Gilles Peskine449bd832023-01-11 14:50:10 +01002508 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_finish_handshake_msg(
2509 ssl, buf_len, msg_len));
2510 } else if (ret == SSL_CERTIFICATE_REQUEST_SKIP) {
2511 MBEDTLS_SSL_DEBUG_MSG(2, ("<= skip write certificate request"));
XiaokangQiancec9ae62022-05-06 07:28:50 +00002512 ret = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01002513 } else {
2514 MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
XiaokangQiancec9ae62022-05-06 07:28:50 +00002515 ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
2516 goto cleanup;
2517 }
2518
Gilles Peskine449bd832023-01-11 14:50:10 +01002519 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_SERVER_CERTIFICATE);
XiaokangQiancec9ae62022-05-06 07:28:50 +00002520cleanup:
2521
Gilles Peskine449bd832023-01-11 14:50:10 +01002522 MBEDTLS_SSL_DEBUG_MSG(2, ("<= write certificate request"));
2523 return ret;
XiaokangQiancec9ae62022-05-06 07:28:50 +00002524}
XiaokangQiancec9ae62022-05-06 07:28:50 +00002525
Jerry Yu4d3841a2022-04-16 12:37:19 +08002526/*
Jerry Yuc6e6dbf2022-04-16 19:42:57 +08002527 * Handler for MBEDTLS_SSL_SERVER_CERTIFICATE
Jerry Yu83da34e2022-04-16 13:59:52 +08002528 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002529MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002530static int ssl_tls13_write_server_certificate(mbedtls_ssl_context *ssl)
Jerry Yu83da34e2022-04-16 13:59:52 +08002531{
Jerry Yu5a26f302022-05-10 20:46:40 +08002532 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
XiaokangQianfb665a82022-06-15 03:57:21 +00002533
2534#if defined(MBEDTLS_X509_CRT_PARSE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01002535 if ((ssl_tls13_pick_key_cert(ssl) != 0) ||
2536 mbedtls_ssl_own_cert(ssl) == NULL) {
2537 MBEDTLS_SSL_DEBUG_MSG(2, ("No certificate available."));
2538 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE,
2539 MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE);
2540 return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
Jerry Yu5a26f302022-05-10 20:46:40 +08002541 }
XiaokangQianfb665a82022-06-15 03:57:21 +00002542#endif /* MBEDTLS_X509_CRT_PARSE_C */
Jerry Yu5a26f302022-05-10 20:46:40 +08002543
Gilles Peskine449bd832023-01-11 14:50:10 +01002544 ret = mbedtls_ssl_tls13_write_certificate(ssl);
2545 if (ret != 0) {
2546 return ret;
2547 }
2548 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_CERTIFICATE_VERIFY);
2549 return 0;
Jerry Yu83da34e2022-04-16 13:59:52 +08002550}
2551
2552/*
Jerry Yuc6e6dbf2022-04-16 19:42:57 +08002553 * Handler for MBEDTLS_SSL_CERTIFICATE_VERIFY
Jerry Yu83da34e2022-04-16 13:59:52 +08002554 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002555MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002556static int ssl_tls13_write_certificate_verify(mbedtls_ssl_context *ssl)
Jerry Yu83da34e2022-04-16 13:59:52 +08002557{
Gilles Peskine449bd832023-01-11 14:50:10 +01002558 int ret = mbedtls_ssl_tls13_write_certificate_verify(ssl);
2559 if (ret != 0) {
2560 return ret;
2561 }
2562 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_SERVER_FINISHED);
2563 return 0;
Jerry Yu83da34e2022-04-16 13:59:52 +08002564}
Ronald Cron928cbd32022-10-04 16:14:26 +02002565#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED */
Jerry Yu83da34e2022-04-16 13:59:52 +08002566
2567/*
Jerry Yud6e253d2022-05-18 13:59:24 +08002568 * Handler for MBEDTLS_SSL_SERVER_FINISHED
Jerry Yu69dd8d42022-04-16 12:51:26 +08002569 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002570MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002571static int ssl_tls13_write_server_finished(mbedtls_ssl_context *ssl)
Jerry Yu69dd8d42022-04-16 12:51:26 +08002572{
Jerry Yud6e253d2022-05-18 13:59:24 +08002573 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yu27bdc7c2022-04-16 13:33:27 +08002574
Gilles Peskine449bd832023-01-11 14:50:10 +01002575 ret = mbedtls_ssl_tls13_write_finished_message(ssl);
2576 if (ret != 0) {
2577 return ret;
2578 }
Jerry Yu27bdc7c2022-04-16 13:33:27 +08002579
Gilles Peskine449bd832023-01-11 14:50:10 +01002580 ret = mbedtls_ssl_tls13_compute_application_transform(ssl);
2581 if (ret != 0) {
Jerry Yue3d67cb2022-05-19 15:33:10 +08002582 MBEDTLS_SSL_PEND_FATAL_ALERT(
Gilles Peskine449bd832023-01-11 14:50:10 +01002583 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE,
2584 MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE);
2585 return ret;
Jerry Yue3d67cb2022-05-19 15:33:10 +08002586 }
XiaokangQianc3017f62022-05-13 05:55:41 +00002587
Gilles Peskine449bd832023-01-11 14:50:10 +01002588 MBEDTLS_SSL_DEBUG_MSG(1, ("Switch to handshake keys for inbound traffic"));
2589 mbedtls_ssl_set_inbound_transform(ssl, ssl->handshake->transform_handshake);
XiaokangQian189ded22022-05-10 08:12:17 +00002590
Gilles Peskine449bd832023-01-11 14:50:10 +01002591 if (ssl->handshake->certificate_request_sent) {
2592 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_CLIENT_CERTIFICATE);
2593 } else {
2594 MBEDTLS_SSL_DEBUG_MSG(2, ("skip parse certificate"));
2595 MBEDTLS_SSL_DEBUG_MSG(2, ("skip parse certificate verify"));
2596 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_CLIENT_FINISHED);
Ronald Cron19385882022-06-15 16:26:13 +02002597 }
Ronald Cron63dc4632022-05-31 14:41:53 +02002598
Gilles Peskine449bd832023-01-11 14:50:10 +01002599 return 0;
Jerry Yu69dd8d42022-04-16 12:51:26 +08002600}
2601
2602/*
Jerry Yud6e253d2022-05-18 13:59:24 +08002603 * Handler for MBEDTLS_SSL_CLIENT_FINISHED
Jerry Yu69dd8d42022-04-16 12:51:26 +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_process_client_finished(mbedtls_ssl_context *ssl)
Jerry Yu69dd8d42022-04-16 12:51:26 +08002607{
Jerry Yud6e253d2022-05-18 13:59:24 +08002608 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
2609
Gilles Peskine449bd832023-01-11 14:50:10 +01002610 ret = mbedtls_ssl_tls13_process_finished_message(ssl);
2611 if (ret != 0) {
2612 return ret;
Jerry Yue3d67cb2022-05-19 15:33:10 +08002613 }
2614
Gilles Peskine449bd832023-01-11 14:50:10 +01002615 ret = mbedtls_ssl_tls13_compute_resumption_master_secret(ssl);
2616 if (ret != 0) {
2617 MBEDTLS_SSL_DEBUG_RET(1,
2618 "mbedtls_ssl_tls13_compute_resumption_master_secret", ret);
2619 }
2620
2621 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_HANDSHAKE_WRAPUP);
2622 return 0;
Jerry Yu69dd8d42022-04-16 12:51:26 +08002623}
2624
2625/*
Jerry Yud6e253d2022-05-18 13:59:24 +08002626 * Handler for MBEDTLS_SSL_HANDSHAKE_WRAPUP
Jerry Yu69dd8d42022-04-16 12:51:26 +08002627 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002628MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002629static int ssl_tls13_handshake_wrapup(mbedtls_ssl_context *ssl)
Jerry Yu69dd8d42022-04-16 12:51:26 +08002630{
Gilles Peskine449bd832023-01-11 14:50:10 +01002631 MBEDTLS_SSL_DEBUG_MSG(2, ("handshake: done"));
Jerry Yu03ed50b2022-04-16 17:13:30 +08002632
Gilles Peskine449bd832023-01-11 14:50:10 +01002633 mbedtls_ssl_tls13_handshake_wrapup(ssl);
Jerry Yue67bef42022-07-07 07:29:42 +00002634
Pengyu Lv3643fdb2023-01-12 16:46:28 +08002635#if defined(MBEDTLS_SSL_SESSION_TICKETS) && \
2636 defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED)
Pengyu Lva1aa31b2022-12-13 13:49:59 +08002637/* TODO: Remove the check of SOME_PSK_ENABLED since SESSION_TICKETS requires
2638 * SOME_PSK_ENABLED to be enabled. Here is just to make CI happy. It is
2639 * expected to be resolved with issue#6395.
2640 */
Pengyu Lvc7af2c42022-12-01 16:33:00 +08002641 /* Sent NewSessionTicket message only when client supports PSK */
Pengyu Lv3643fdb2023-01-12 16:46:28 +08002642 if (mbedtls_ssl_tls13_some_psk_enabled(ssl)) {
2643 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_TLS1_3_NEW_SESSION_TICKET);
Pengyu Lvc7af2c42022-12-01 16:33:00 +08002644 } else
Jerry Yufca4d572022-07-21 10:37:48 +08002645#endif
Pengyu Lv3643fdb2023-01-12 16:46:28 +08002646 {
2647 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_HANDSHAKE_OVER);
2648 }
Gilles Peskine449bd832023-01-11 14:50:10 +01002649 return 0;
Jerry Yu69dd8d42022-04-16 12:51:26 +08002650}
2651
2652/*
Jerry Yua8d3c502022-10-30 14:51:23 +08002653 * Handler for MBEDTLS_SSL_TLS1_3_NEW_SESSION_TICKET
Jerry Yue67bef42022-07-07 07:29:42 +00002654 */
2655#define SSL_NEW_SESSION_TICKET_SKIP 0
2656#define SSL_NEW_SESSION_TICKET_WRITE 1
2657MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002658static int ssl_tls13_write_new_session_ticket_coordinate(mbedtls_ssl_context *ssl)
Jerry Yue67bef42022-07-07 07:29:42 +00002659{
2660 /* Check whether the use of session tickets is enabled */
Gilles Peskine449bd832023-01-11 14:50:10 +01002661 if (ssl->conf->f_ticket_write == NULL) {
2662 MBEDTLS_SSL_DEBUG_MSG(2, ("NewSessionTicket: disabled,"
2663 " callback is not set"));
2664 return SSL_NEW_SESSION_TICKET_SKIP;
Jerry Yud0766ec2022-09-22 10:46:57 +08002665 }
Gilles Peskine449bd832023-01-11 14:50:10 +01002666 if (ssl->conf->new_session_tickets_count == 0) {
2667 MBEDTLS_SSL_DEBUG_MSG(2, ("NewSessionTicket: disabled,"
2668 " configured count is zero"));
2669 return SSL_NEW_SESSION_TICKET_SKIP;
Jerry Yud0766ec2022-09-22 10:46:57 +08002670 }
2671
Gilles Peskine449bd832023-01-11 14:50:10 +01002672 if (ssl->handshake->new_session_tickets_count == 0) {
2673 MBEDTLS_SSL_DEBUG_MSG(2, ("NewSessionTicket: all tickets have "
2674 "been sent."));
2675 return SSL_NEW_SESSION_TICKET_SKIP;
Jerry Yue67bef42022-07-07 07:29:42 +00002676 }
2677
Gilles Peskine449bd832023-01-11 14:50:10 +01002678 return SSL_NEW_SESSION_TICKET_WRITE;
Jerry Yue67bef42022-07-07 07:29:42 +00002679}
2680
2681#if defined(MBEDTLS_SSL_SESSION_TICKETS)
2682MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002683static int ssl_tls13_prepare_new_session_ticket(mbedtls_ssl_context *ssl,
2684 unsigned char *ticket_nonce,
2685 size_t ticket_nonce_size)
Jerry Yue67bef42022-07-07 07:29:42 +00002686{
2687 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
2688 mbedtls_ssl_session *session = ssl->session;
2689 mbedtls_ssl_ciphersuite_t *ciphersuite_info;
2690 psa_algorithm_t psa_hash_alg;
2691 int hash_length;
2692
Gilles Peskine449bd832023-01-11 14:50:10 +01002693 MBEDTLS_SSL_DEBUG_MSG(2, ("=> prepare NewSessionTicket msg"));
Jerry Yue67bef42022-07-07 07:29:42 +00002694
2695#if defined(MBEDTLS_HAVE_TIME)
Gilles Peskine449bd832023-01-11 14:50:10 +01002696 session->start = mbedtls_time(NULL);
Jerry Yue67bef42022-07-07 07:29:42 +00002697#endif
2698
Pengyu Lv9f926952022-11-17 15:22:33 +08002699 /* Set ticket_flags depends on the advertised psk key exchange mode */
Pengyu Lv80270b22023-01-12 11:54:04 +08002700 mbedtls_ssl_session_clear_ticket_flags(
Pengyu Lv9eacb442022-12-09 14:39:19 +08002701 session, MBEDTLS_SSL_TLS1_3_TICKET_FLAGS_MASK);
Pengyu Lve6487fe2022-12-06 09:30:29 +08002702#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED)
Pengyu Lv80270b22023-01-12 11:54:04 +08002703 mbedtls_ssl_session_set_ticket_flags(
Pengyu Lv9eacb442022-12-09 14:39:19 +08002704 session, ssl->handshake->tls13_kex_modes);
Pengyu Lve6487fe2022-12-06 09:30:29 +08002705#endif
Pengyu Lv4938a562023-01-16 11:28:49 +08002706 MBEDTLS_SSL_PRINT_TICKET_FLAGS(4, session->ticket_flags);
Pengyu Lv9f926952022-11-17 15:22:33 +08002707
Jerry Yue67bef42022-07-07 07:29:42 +00002708 /* Generate ticket_age_add */
Gilles Peskine449bd832023-01-11 14:50:10 +01002709 if ((ret = ssl->conf->f_rng(ssl->conf->p_rng,
2710 (unsigned char *) &session->ticket_age_add,
2711 sizeof(session->ticket_age_add)) != 0)) {
2712 MBEDTLS_SSL_DEBUG_RET(1, "generate_ticket_age_add", ret);
2713 return ret;
Jerry Yue67bef42022-07-07 07:29:42 +00002714 }
Gilles Peskine449bd832023-01-11 14:50:10 +01002715 MBEDTLS_SSL_DEBUG_MSG(3, ("ticket_age_add: %u",
2716 (unsigned int) session->ticket_age_add));
Jerry Yue67bef42022-07-07 07:29:42 +00002717
2718 /* Generate ticket_nonce */
Gilles Peskine449bd832023-01-11 14:50:10 +01002719 ret = ssl->conf->f_rng(ssl->conf->p_rng, ticket_nonce, ticket_nonce_size);
2720 if (ret != 0) {
2721 MBEDTLS_SSL_DEBUG_RET(1, "generate_ticket_nonce", ret);
2722 return ret;
Jerry Yue67bef42022-07-07 07:29:42 +00002723 }
Gilles Peskine449bd832023-01-11 14:50:10 +01002724 MBEDTLS_SSL_DEBUG_BUF(3, "ticket_nonce:",
2725 ticket_nonce, ticket_nonce_size);
Jerry Yue67bef42022-07-07 07:29:42 +00002726
2727 ciphersuite_info =
Gilles Peskine449bd832023-01-11 14:50:10 +01002728 (mbedtls_ssl_ciphersuite_t *) ssl->handshake->ciphersuite_info;
2729 psa_hash_alg = mbedtls_psa_translate_md(ciphersuite_info->mac);
2730 hash_length = PSA_HASH_LENGTH(psa_hash_alg);
2731 if (hash_length == -1 ||
2732 (size_t) hash_length > sizeof(session->resumption_key)) {
2733 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Jerry Yufca4d572022-07-21 10:37:48 +08002734 }
2735
Jerry Yue67bef42022-07-07 07:29:42 +00002736 /* In this code the psk key length equals the length of the hash */
2737 session->resumption_key_len = hash_length;
2738 session->ciphersuite = ciphersuite_info->id;
2739
2740 /* Compute resumption key
2741 *
2742 * HKDF-Expand-Label( resumption_master_secret,
2743 * "resumption", ticket_nonce, Hash.length )
2744 */
2745 ret = mbedtls_ssl_tls13_hkdf_expand_label(
Gilles Peskine449bd832023-01-11 14:50:10 +01002746 psa_hash_alg,
2747 session->app_secrets.resumption_master_secret,
2748 hash_length,
2749 MBEDTLS_SSL_TLS1_3_LBL_WITH_LEN(resumption),
2750 ticket_nonce,
2751 ticket_nonce_size,
2752 session->resumption_key,
2753 hash_length);
Jerry Yue67bef42022-07-07 07:29:42 +00002754
Gilles Peskine449bd832023-01-11 14:50:10 +01002755 if (ret != 0) {
2756 MBEDTLS_SSL_DEBUG_RET(2,
2757 "Creating the ticket-resumed PSK failed",
2758 ret);
2759 return ret;
Jerry Yue67bef42022-07-07 07:29:42 +00002760 }
Gilles Peskine449bd832023-01-11 14:50:10 +01002761 MBEDTLS_SSL_DEBUG_BUF(3, "Ticket-resumed PSK",
2762 session->resumption_key,
2763 session->resumption_key_len);
Jerry Yue67bef42022-07-07 07:29:42 +00002764
Gilles Peskine449bd832023-01-11 14:50:10 +01002765 MBEDTLS_SSL_DEBUG_BUF(3, "resumption_master_secret",
2766 session->app_secrets.resumption_master_secret,
2767 hash_length);
Jerry Yue67bef42022-07-07 07:29:42 +00002768
Gilles Peskine449bd832023-01-11 14:50:10 +01002769 return 0;
Jerry Yue67bef42022-07-07 07:29:42 +00002770}
2771
2772/* This function creates a NewSessionTicket message in the following format:
2773 *
2774 * struct {
2775 * uint32 ticket_lifetime;
2776 * uint32 ticket_age_add;
2777 * opaque ticket_nonce<0..255>;
2778 * opaque ticket<1..2^16-1>;
2779 * Extension extensions<0..2^16-2>;
2780 * } NewSessionTicket;
2781 *
2782 * The ticket inside the NewSessionTicket message is an encrypted container
2783 * carrying the necessary information so that the server is later able to
2784 * re-start the communication.
2785 *
2786 * The following fields are placed inside the ticket by the
2787 * f_ticket_write() function:
2788 *
2789 * - creation time (start)
2790 * - flags (flags)
2791 * - age add (ticket_age_add)
2792 * - key (key)
2793 * - key length (key_len)
2794 * - ciphersuite (ciphersuite)
2795 */
2796MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002797static int ssl_tls13_write_new_session_ticket_body(mbedtls_ssl_context *ssl,
2798 unsigned char *buf,
2799 unsigned char *end,
2800 size_t *out_len,
2801 unsigned char *ticket_nonce,
2802 size_t ticket_nonce_size)
Jerry Yue67bef42022-07-07 07:29:42 +00002803{
2804 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
2805 unsigned char *p = buf;
2806 mbedtls_ssl_session *session = ssl->session;
2807 size_t ticket_len;
2808 uint32_t ticket_lifetime;
2809
2810 *out_len = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01002811 MBEDTLS_SSL_DEBUG_MSG(2, ("=> write NewSessionTicket msg"));
Jerry Yue67bef42022-07-07 07:29:42 +00002812
2813 /*
2814 * ticket_lifetime 4 bytes
2815 * ticket_age_add 4 bytes
2816 * ticket_nonce 1 + ticket_nonce_size bytes
2817 * ticket >=2 bytes
2818 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002819 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 4 + 4 + 1 + ticket_nonce_size + 2);
Jerry Yue67bef42022-07-07 07:29:42 +00002820
2821 /* Generate ticket and ticket_lifetime */
Gilles Peskine449bd832023-01-11 14:50:10 +01002822 ret = ssl->conf->f_ticket_write(ssl->conf->p_ticket,
2823 session,
2824 p + 9 + ticket_nonce_size + 2,
2825 end,
2826 &ticket_len,
2827 &ticket_lifetime);
2828 if (ret != 0) {
2829 MBEDTLS_SSL_DEBUG_RET(1, "write_ticket", ret);
2830 return ret;
Jerry Yue67bef42022-07-07 07:29:42 +00002831 }
2832 /* RFC 8446 4.6.1
2833 * ticket_lifetime: Indicates the lifetime in seconds as a 32-bit
2834 * unsigned integer in network byte order from the time of ticket
2835 * issuance. Servers MUST NOT use any value greater than
2836 * 604800 seconds (7 days). The value of zero indicates that the
2837 * ticket should be discarded immediately. Clients MUST NOT cache
2838 * tickets for longer than 7 days, regardless of the ticket_lifetime,
2839 * and MAY delete tickets earlier based on local policy. A server
2840 * MAY treat a ticket as valid for a shorter period of time than what
2841 * is stated in the ticket_lifetime.
2842 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002843 if (ticket_lifetime > 604800) {
Xiaokang Qian28af5012022-10-13 08:18:19 +00002844 ticket_lifetime = 604800;
Gilles Peskine449bd832023-01-11 14:50:10 +01002845 }
2846 MBEDTLS_PUT_UINT32_BE(ticket_lifetime, p, 0);
2847 MBEDTLS_SSL_DEBUG_MSG(3, ("ticket_lifetime: %u",
2848 (unsigned int) ticket_lifetime));
Jerry Yue67bef42022-07-07 07:29:42 +00002849
2850 /* Write ticket_age_add */
Gilles Peskine449bd832023-01-11 14:50:10 +01002851 MBEDTLS_PUT_UINT32_BE(session->ticket_age_add, p, 4);
2852 MBEDTLS_SSL_DEBUG_MSG(3, ("ticket_age_add: %u",
2853 (unsigned int) session->ticket_age_add));
Jerry Yue67bef42022-07-07 07:29:42 +00002854
2855 /* Write ticket_nonce */
Gilles Peskine449bd832023-01-11 14:50:10 +01002856 p[8] = (unsigned char) ticket_nonce_size;
2857 if (ticket_nonce_size > 0) {
2858 memcpy(p + 9, ticket_nonce, ticket_nonce_size);
Jerry Yue67bef42022-07-07 07:29:42 +00002859 }
2860 p += 9 + ticket_nonce_size;
2861
2862 /* Write ticket */
Gilles Peskine449bd832023-01-11 14:50:10 +01002863 MBEDTLS_PUT_UINT16_BE(ticket_len, p, 0);
Jerry Yue67bef42022-07-07 07:29:42 +00002864 p += 2;
Gilles Peskine449bd832023-01-11 14:50:10 +01002865 MBEDTLS_SSL_DEBUG_BUF(4, "ticket", p, ticket_len);
Jerry Yue67bef42022-07-07 07:29:42 +00002866 p += ticket_len;
2867
2868 /* Ticket Extensions
2869 *
2870 * Note: We currently don't have any extensions.
2871 * Set length to zero.
2872 */
Jerry Yuedab6372022-10-31 14:37:31 +08002873 ssl->handshake->sent_extensions = MBEDTLS_SSL_EXT_MASK_NONE;
2874
Gilles Peskine449bd832023-01-11 14:50:10 +01002875 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 2);
2876 MBEDTLS_PUT_UINT16_BE(0, p, 0);
Jerry Yue67bef42022-07-07 07:29:42 +00002877 p += 2;
2878
2879 *out_len = p - buf;
Gilles Peskine449bd832023-01-11 14:50:10 +01002880 MBEDTLS_SSL_DEBUG_BUF(4, "ticket", buf, *out_len);
2881 MBEDTLS_SSL_DEBUG_MSG(2, ("<= write new session ticket"));
Jerry Yue67bef42022-07-07 07:29:42 +00002882
Jerry Yu7de2ff02022-11-08 21:43:46 +08002883 MBEDTLS_SSL_PRINT_EXTS(
Gilles Peskine449bd832023-01-11 14:50:10 +01002884 3, MBEDTLS_SSL_HS_NEW_SESSION_TICKET, ssl->handshake->sent_extensions);
Jerry Yu4b8f2f72022-10-31 13:31:22 +08002885
Gilles Peskine449bd832023-01-11 14:50:10 +01002886 return 0;
Jerry Yue67bef42022-07-07 07:29:42 +00002887}
2888
2889/*
Jerry Yua8d3c502022-10-30 14:51:23 +08002890 * Handler for MBEDTLS_SSL_TLS1_3_NEW_SESSION_TICKET
Jerry Yue67bef42022-07-07 07:29:42 +00002891 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002892static int ssl_tls13_write_new_session_ticket(mbedtls_ssl_context *ssl)
Jerry Yue67bef42022-07-07 07:29:42 +00002893{
2894 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
2895
Gilles Peskine449bd832023-01-11 14:50:10 +01002896 MBEDTLS_SSL_PROC_CHK_NEG(ssl_tls13_write_new_session_ticket_coordinate(ssl));
Jerry Yue67bef42022-07-07 07:29:42 +00002897
Gilles Peskine449bd832023-01-11 14:50:10 +01002898 if (ret == SSL_NEW_SESSION_TICKET_WRITE) {
Jerry Yue67bef42022-07-07 07:29:42 +00002899 unsigned char ticket_nonce[MBEDTLS_SSL_TLS1_3_TICKET_NONCE_LENGTH];
2900 unsigned char *buf;
2901 size_t buf_len, msg_len;
2902
Gilles Peskine449bd832023-01-11 14:50:10 +01002903 MBEDTLS_SSL_PROC_CHK(ssl_tls13_prepare_new_session_ticket(
2904 ssl, ticket_nonce, sizeof(ticket_nonce)));
Jerry Yue67bef42022-07-07 07:29:42 +00002905
Gilles Peskine449bd832023-01-11 14:50:10 +01002906 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_start_handshake_msg(ssl,
2907 MBEDTLS_SSL_HS_NEW_SESSION_TICKET,
2908 &buf, &buf_len));
Jerry Yue67bef42022-07-07 07:29:42 +00002909
Gilles Peskine449bd832023-01-11 14:50:10 +01002910 MBEDTLS_SSL_PROC_CHK(ssl_tls13_write_new_session_ticket_body(
2911 ssl, buf, buf + buf_len, &msg_len,
2912 ticket_nonce, sizeof(ticket_nonce)));
Jerry Yue67bef42022-07-07 07:29:42 +00002913
Gilles Peskine449bd832023-01-11 14:50:10 +01002914 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_finish_handshake_msg(
2915 ssl, buf_len, msg_len));
Jerry Yufca4d572022-07-21 10:37:48 +08002916
Jerry Yu359e65f2022-09-22 23:47:43 +08002917 /* Limit session tickets count to one when resumption connection.
2918 *
2919 * See document of mbedtls_ssl_conf_new_session_tickets.
2920 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002921 if (ssl->handshake->resume == 1) {
Jerry Yu359e65f2022-09-22 23:47:43 +08002922 ssl->handshake->new_session_tickets_count = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01002923 } else {
Jerry Yu359e65f2022-09-22 23:47:43 +08002924 ssl->handshake->new_session_tickets_count--;
Gilles Peskine449bd832023-01-11 14:50:10 +01002925 }
Jerry Yub7e3fa72022-09-22 11:07:18 +08002926
Jerry Yua8d3c502022-10-30 14:51:23 +08002927 mbedtls_ssl_handshake_set_state(
Gilles Peskine449bd832023-01-11 14:50:10 +01002928 ssl, MBEDTLS_SSL_TLS1_3_NEW_SESSION_TICKET_FLUSH);
2929 } else {
2930 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_HANDSHAKE_OVER);
Jerry Yue67bef42022-07-07 07:29:42 +00002931 }
2932
Jerry Yue67bef42022-07-07 07:29:42 +00002933cleanup:
2934
Gilles Peskine449bd832023-01-11 14:50:10 +01002935 return ret;
Jerry Yue67bef42022-07-07 07:29:42 +00002936}
2937#endif /* MBEDTLS_SSL_SESSION_TICKETS */
2938
2939/*
XiaokangQiane8ff3502022-04-22 02:34:40 +00002940 * TLS 1.3 State Machine -- server side
XiaokangQian7807f9f2022-02-15 10:04:37 +00002941 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002942int mbedtls_ssl_tls13_handshake_server_step(mbedtls_ssl_context *ssl)
Jerry Yub9930e72021-08-06 17:11:51 +08002943{
XiaokangQian08037552022-04-20 07:16:41 +00002944 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
XiaokangQian7807f9f2022-02-15 10:04:37 +00002945
Gilles Peskine449bd832023-01-11 14:50:10 +01002946 if (ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER || ssl->handshake == NULL) {
2947 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
2948 }
XiaokangQian7807f9f2022-02-15 10:04:37 +00002949
Gilles Peskine449bd832023-01-11 14:50:10 +01002950 MBEDTLS_SSL_DEBUG_MSG(2, ("tls13 server state: %s(%d)",
2951 mbedtls_ssl_states_str(ssl->state),
2952 ssl->state));
Jerry Yu6e81b272021-09-27 11:16:17 +08002953
Gilles Peskine449bd832023-01-11 14:50:10 +01002954 switch (ssl->state) {
XiaokangQian7807f9f2022-02-15 10:04:37 +00002955 /* start state */
2956 case MBEDTLS_SSL_HELLO_REQUEST:
Gilles Peskine449bd832023-01-11 14:50:10 +01002957 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_CLIENT_HELLO);
XiaokangQian08037552022-04-20 07:16:41 +00002958 ret = 0;
XiaokangQian7807f9f2022-02-15 10:04:37 +00002959 break;
2960
XiaokangQian7807f9f2022-02-15 10:04:37 +00002961 case MBEDTLS_SSL_CLIENT_HELLO:
Gilles Peskine449bd832023-01-11 14:50:10 +01002962 ret = ssl_tls13_process_client_hello(ssl);
2963 if (ret != 0) {
2964 MBEDTLS_SSL_DEBUG_RET(1, "ssl_tls13_process_client_hello", ret);
2965 }
Jerry Yuf41553b2022-05-09 22:20:30 +08002966 break;
XiaokangQian7807f9f2022-02-15 10:04:37 +00002967
Jerry Yuf41553b2022-05-09 22:20:30 +08002968 case MBEDTLS_SSL_HELLO_RETRY_REQUEST:
Gilles Peskine449bd832023-01-11 14:50:10 +01002969 ret = ssl_tls13_write_hello_retry_request(ssl);
2970 if (ret != 0) {
2971 MBEDTLS_SSL_DEBUG_RET(1, "ssl_tls13_write_hello_retry_request", ret);
2972 return ret;
Jerry Yuf41553b2022-05-09 22:20:30 +08002973 }
XiaokangQian7807f9f2022-02-15 10:04:37 +00002974 break;
2975
Jerry Yu5b64ae92022-03-30 17:15:02 +08002976 case MBEDTLS_SSL_SERVER_HELLO:
Gilles Peskine449bd832023-01-11 14:50:10 +01002977 ret = ssl_tls13_write_server_hello(ssl);
Jerry Yu5b64ae92022-03-30 17:15:02 +08002978 break;
2979
Xiaofei Baicba64af2022-02-15 10:00:56 +00002980 case MBEDTLS_SSL_ENCRYPTED_EXTENSIONS:
Gilles Peskine449bd832023-01-11 14:50:10 +01002981 ret = ssl_tls13_write_encrypted_extensions(ssl);
2982 if (ret != 0) {
2983 MBEDTLS_SSL_DEBUG_RET(1, "ssl_tls13_write_encrypted_extensions", ret);
2984 return ret;
Xiaofei Baicba64af2022-02-15 10:00:56 +00002985 }
Jerry Yu48330562022-05-06 21:35:44 +08002986 break;
Jerry Yu6a2cd9e2022-05-05 11:14:19 +08002987
Ronald Cron928cbd32022-10-04 16:14:26 +02002988#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED)
Xiaofei Bai9ca09d42022-02-14 12:57:18 +00002989 case MBEDTLS_SSL_CERTIFICATE_REQUEST:
Gilles Peskine449bd832023-01-11 14:50:10 +01002990 ret = ssl_tls13_write_certificate_request(ssl);
Xiaofei Bai9ca09d42022-02-14 12:57:18 +00002991 break;
Xiaofei Bai9ca09d42022-02-14 12:57:18 +00002992
Jerry Yu83da34e2022-04-16 13:59:52 +08002993 case MBEDTLS_SSL_SERVER_CERTIFICATE:
Gilles Peskine449bd832023-01-11 14:50:10 +01002994 ret = ssl_tls13_write_server_certificate(ssl);
Jerry Yu83da34e2022-04-16 13:59:52 +08002995 break;
2996
2997 case MBEDTLS_SSL_CERTIFICATE_VERIFY:
Gilles Peskine449bd832023-01-11 14:50:10 +01002998 ret = ssl_tls13_write_certificate_verify(ssl);
Jerry Yu83da34e2022-04-16 13:59:52 +08002999 break;
Ronald Cron928cbd32022-10-04 16:14:26 +02003000#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED */
Jerry Yu83da34e2022-04-16 13:59:52 +08003001
Gilles Peskine449bd832023-01-11 14:50:10 +01003002 /*
3003 * Injection of dummy-CCS's for middlebox compatibility
3004 */
Gabor Mezei7b39bf12022-05-24 16:04:14 +02003005#if defined(MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE)
Gabor Mezeif7044ea2022-06-28 16:01:49 +02003006 case MBEDTLS_SSL_SERVER_CCS_AFTER_HELLO_RETRY_REQUEST:
Gilles Peskine449bd832023-01-11 14:50:10 +01003007 ret = mbedtls_ssl_tls13_write_change_cipher_spec(ssl);
3008 if (ret == 0) {
3009 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_CLIENT_HELLO);
3010 }
Gabor Mezei7b39bf12022-05-24 16:04:14 +02003011 break;
3012
3013 case MBEDTLS_SSL_SERVER_CCS_AFTER_SERVER_HELLO:
Gilles Peskine449bd832023-01-11 14:50:10 +01003014 ret = mbedtls_ssl_tls13_write_change_cipher_spec(ssl);
3015 if (ret == 0) {
3016 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_ENCRYPTED_EXTENSIONS);
3017 }
Gabor Mezei7b39bf12022-05-24 16:04:14 +02003018 break;
3019#endif /* MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE */
3020
Jerry Yu69dd8d42022-04-16 12:51:26 +08003021 case MBEDTLS_SSL_SERVER_FINISHED:
Gilles Peskine449bd832023-01-11 14:50:10 +01003022 ret = ssl_tls13_write_server_finished(ssl);
Jerry Yu69dd8d42022-04-16 12:51:26 +08003023 break;
3024
3025 case MBEDTLS_SSL_CLIENT_FINISHED:
Gilles Peskine449bd832023-01-11 14:50:10 +01003026 ret = ssl_tls13_process_client_finished(ssl);
Jerry Yu69dd8d42022-04-16 12:51:26 +08003027 break;
3028
Jerry Yu69dd8d42022-04-16 12:51:26 +08003029 case MBEDTLS_SSL_HANDSHAKE_WRAPUP:
Gilles Peskine449bd832023-01-11 14:50:10 +01003030 ret = ssl_tls13_handshake_wrapup(ssl);
Jerry Yu69dd8d42022-04-16 12:51:26 +08003031 break;
3032
Ronald Cron766c0cd2022-10-18 12:17:11 +02003033#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED)
XiaokangQian6b916b12022-04-25 07:29:34 +00003034 case MBEDTLS_SSL_CLIENT_CERTIFICATE:
Gilles Peskine449bd832023-01-11 14:50:10 +01003035 ret = mbedtls_ssl_tls13_process_certificate(ssl);
3036 if (ret == 0) {
3037 if (ssl->session_negotiate->peer_cert != NULL) {
Ronald Cron209cae92022-06-07 10:30:19 +02003038 mbedtls_ssl_handshake_set_state(
Gilles Peskine449bd832023-01-11 14:50:10 +01003039 ssl, MBEDTLS_SSL_CLIENT_CERTIFICATE_VERIFY);
3040 } else {
3041 MBEDTLS_SSL_DEBUG_MSG(2, ("skip parse certificate verify"));
Ronald Cron209cae92022-06-07 10:30:19 +02003042 mbedtls_ssl_handshake_set_state(
Gilles Peskine449bd832023-01-11 14:50:10 +01003043 ssl, MBEDTLS_SSL_CLIENT_FINISHED);
Ronald Cron19385882022-06-15 16:26:13 +02003044 }
XiaokangQian6b916b12022-04-25 07:29:34 +00003045 }
3046 break;
3047
3048 case MBEDTLS_SSL_CLIENT_CERTIFICATE_VERIFY:
Gilles Peskine449bd832023-01-11 14:50:10 +01003049 ret = mbedtls_ssl_tls13_process_certificate_verify(ssl);
3050 if (ret == 0) {
XiaokangQian6b916b12022-04-25 07:29:34 +00003051 mbedtls_ssl_handshake_set_state(
Gilles Peskine449bd832023-01-11 14:50:10 +01003052 ssl, MBEDTLS_SSL_CLIENT_FINISHED);
XiaokangQian6b916b12022-04-25 07:29:34 +00003053 }
3054 break;
Ronald Cron766c0cd2022-10-18 12:17:11 +02003055#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED */
XiaokangQian6b916b12022-04-25 07:29:34 +00003056
Jerry Yue67bef42022-07-07 07:29:42 +00003057#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Jerry Yua8d3c502022-10-30 14:51:23 +08003058 case MBEDTLS_SSL_TLS1_3_NEW_SESSION_TICKET:
Gilles Peskine449bd832023-01-11 14:50:10 +01003059 ret = ssl_tls13_write_new_session_ticket(ssl);
3060 if (ret != 0) {
3061 MBEDTLS_SSL_DEBUG_RET(1,
3062 "ssl_tls13_write_new_session_ticket ",
3063 ret);
Jerry Yue67bef42022-07-07 07:29:42 +00003064 }
3065 break;
Jerry Yua8d3c502022-10-30 14:51:23 +08003066 case MBEDTLS_SSL_TLS1_3_NEW_SESSION_TICKET_FLUSH:
Jerry Yue67bef42022-07-07 07:29:42 +00003067 /* This state is necessary to do the flush of the New Session
Jerry Yua8d3c502022-10-30 14:51:23 +08003068 * Ticket message written in MBEDTLS_SSL_TLS1_3_NEW_SESSION_TICKET
Jerry Yue67bef42022-07-07 07:29:42 +00003069 * as part of ssl_prepare_handshake_step.
3070 */
3071 ret = 0;
Jerry Yud4e75002022-08-09 13:33:50 +08003072
Gilles Peskine449bd832023-01-11 14:50:10 +01003073 if (ssl->handshake->new_session_tickets_count == 0) {
3074 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_HANDSHAKE_OVER);
3075 } else {
3076 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_TLS1_3_NEW_SESSION_TICKET);
3077 }
Jerry Yue67bef42022-07-07 07:29:42 +00003078 break;
3079
3080#endif /* MBEDTLS_SSL_SESSION_TICKETS */
3081
XiaokangQian7807f9f2022-02-15 10:04:37 +00003082 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01003083 MBEDTLS_SSL_DEBUG_MSG(1, ("invalid state %d", ssl->state));
3084 return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
XiaokangQian7807f9f2022-02-15 10:04:37 +00003085 }
3086
Gilles Peskine449bd832023-01-11 14:50:10 +01003087 return ret;
Jerry Yub9930e72021-08-06 17:11:51 +08003088}
Jerry Yu3cc4c2a2021-08-06 16:29:08 +08003089
Jerry Yufb4b6472022-01-27 15:03:26 +08003090#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_PROTO_TLS1_3 */