blob: 8aff191115fec7a087cb24e31ca2de260060d5c5 [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{
Jerry Yu29d9faa2022-08-23 17:52:45 +0800499 const unsigned char *identities = pre_shared_key_ext;
Jerry Yu568ec252022-07-22 21:27:34 +0800500 const unsigned char *p_identity_len;
501 size_t identities_len;
Jerry Yu1c105562022-07-10 06:32:38 +0000502 const unsigned char *identities_end;
Jerry Yu568ec252022-07-22 21:27:34 +0800503 const unsigned char *binders;
504 const unsigned char *p_binder_len;
505 size_t binders_len;
Jerry Yu1c105562022-07-10 06:32:38 +0000506 const unsigned char *binders_end;
Jerry Yu96a2e362022-07-21 15:11:34 +0800507 int matched_identity = -1;
508 int identity_id = -1;
Jerry Yu1c105562022-07-10 06:32:38 +0000509
Gilles Peskine449bd832023-01-11 14:50:10 +0100510 MBEDTLS_SSL_DEBUG_BUF(3, "pre_shared_key extension",
511 pre_shared_key_ext,
512 pre_shared_key_ext_end - pre_shared_key_ext);
Jerry Yu1c105562022-07-10 06:32:38 +0000513
Jerry Yu96a2e362022-07-21 15:11:34 +0800514 /* identities_len 2 bytes
515 * identities_data >= 7 bytes
516 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100517 MBEDTLS_SSL_CHK_BUF_READ_PTR(identities, pre_shared_key_ext_end, 7 + 2);
518 identities_len = MBEDTLS_GET_UINT16_BE(identities, 0);
Jerry Yu568ec252022-07-22 21:27:34 +0800519 p_identity_len = identities + 2;
Gilles Peskine449bd832023-01-11 14:50:10 +0100520 MBEDTLS_SSL_CHK_BUF_READ_PTR(p_identity_len, pre_shared_key_ext_end,
521 identities_len);
Jerry Yu568ec252022-07-22 21:27:34 +0800522 identities_end = p_identity_len + identities_len;
Jerry Yu1c105562022-07-10 06:32:38 +0000523
Jerry Yu96a2e362022-07-21 15:11:34 +0800524 /* binders_len 2 bytes
525 * binders >= 33 bytes
526 */
Jerry Yu568ec252022-07-22 21:27:34 +0800527 binders = identities_end;
Gilles Peskine449bd832023-01-11 14:50:10 +0100528 MBEDTLS_SSL_CHK_BUF_READ_PTR(binders, pre_shared_key_ext_end, 33 + 2);
529 binders_len = MBEDTLS_GET_UINT16_BE(binders, 0);
Jerry Yu568ec252022-07-22 21:27:34 +0800530 p_binder_len = binders + 2;
Gilles Peskine449bd832023-01-11 14:50:10 +0100531 MBEDTLS_SSL_CHK_BUF_READ_PTR(p_binder_len, pre_shared_key_ext_end, binders_len);
Jerry Yu568ec252022-07-22 21:27:34 +0800532 binders_end = p_binder_len + binders_len;
Jerry Yu1c105562022-07-10 06:32:38 +0000533
Gilles Peskine449bd832023-01-11 14:50:10 +0100534 ssl->handshake->update_checksum(ssl, pre_shared_key_ext,
535 identities_end - pre_shared_key_ext);
Jerry Yu96a2e362022-07-21 15:11:34 +0800536
Gilles Peskine449bd832023-01-11 14:50:10 +0100537 while (p_identity_len < identities_end && p_binder_len < binders_end) {
Jerry Yu1c105562022-07-10 06:32:38 +0000538 const unsigned char *identity;
Jerry Yu568ec252022-07-22 21:27:34 +0800539 size_t identity_len;
Jerry Yu82534862022-08-30 10:42:33 +0800540 uint32_t obfuscated_ticket_age;
Jerry Yu96a2e362022-07-21 15:11:34 +0800541 const unsigned char *binder;
Jerry Yu568ec252022-07-22 21:27:34 +0800542 size_t binder_len;
Jerry Yu96a2e362022-07-21 15:11:34 +0800543 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yu5725f1c2022-08-21 17:27:16 +0800544 int psk_type;
545 uint16_t cipher_suite;
Jerry Yu29d9faa2022-08-23 17:52:45 +0800546 const mbedtls_ssl_ciphersuite_t *ciphersuite_info;
Jerry Yu82534862022-08-30 10:42:33 +0800547#if defined(MBEDTLS_SSL_SESSION_TICKETS)
548 mbedtls_ssl_session session;
Gilles Peskine449bd832023-01-11 14:50:10 +0100549 mbedtls_ssl_session_init(&session);
Jerry Yu82534862022-08-30 10:42:33 +0800550#endif
Jerry Yubb852022022-07-20 21:10:44 +0800551
Gilles Peskine449bd832023-01-11 14:50:10 +0100552 MBEDTLS_SSL_CHK_BUF_READ_PTR(p_identity_len, identities_end, 2 + 1 + 4);
553 identity_len = MBEDTLS_GET_UINT16_BE(p_identity_len, 0);
Jerry Yu568ec252022-07-22 21:27:34 +0800554 identity = p_identity_len + 2;
Gilles Peskine449bd832023-01-11 14:50:10 +0100555 MBEDTLS_SSL_CHK_BUF_READ_PTR(identity, identities_end, identity_len + 4);
556 obfuscated_ticket_age = MBEDTLS_GET_UINT32_BE(identity, identity_len);
Jerry Yu568ec252022-07-22 21:27:34 +0800557 p_identity_len += identity_len + 6;
Jerry Yu1c105562022-07-10 06:32:38 +0000558
Gilles Peskine449bd832023-01-11 14:50:10 +0100559 MBEDTLS_SSL_CHK_BUF_READ_PTR(p_binder_len, binders_end, 1 + 32);
Jerry Yu568ec252022-07-22 21:27:34 +0800560 binder_len = *p_binder_len;
561 binder = p_binder_len + 1;
Gilles Peskine449bd832023-01-11 14:50:10 +0100562 MBEDTLS_SSL_CHK_BUF_READ_PTR(binder, binders_end, binder_len);
Jerry Yu568ec252022-07-22 21:27:34 +0800563 p_binder_len += binder_len + 1;
Jerry Yu96a2e362022-07-21 15:11:34 +0800564
Jerry Yu96a2e362022-07-21 15:11:34 +0800565 identity_id++;
Gilles Peskine449bd832023-01-11 14:50:10 +0100566 if (matched_identity != -1) {
Jerry Yu1c105562022-07-10 06:32:38 +0000567 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +0100568 }
Jerry Yu1c105562022-07-10 06:32:38 +0000569
Jerry Yu96a2e362022-07-21 15:11:34 +0800570 ret = ssl_tls13_offered_psks_check_identity_match(
Gilles Peskine449bd832023-01-11 14:50:10 +0100571 ssl, identity, identity_len, obfuscated_ticket_age,
572 &psk_type, &session);
573 if (ret != SSL_TLS1_3_OFFERED_PSK_MATCH) {
Jerry Yu96a2e362022-07-21 15:11:34 +0800574 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +0100575 }
Jerry Yu96a2e362022-07-21 15:11:34 +0800576
Gilles Peskine449bd832023-01-11 14:50:10 +0100577 MBEDTLS_SSL_DEBUG_MSG(4, ("found matched identity"));
578 switch (psk_type) {
Jerry Yu0baf9072022-08-25 11:21:04 +0800579 case MBEDTLS_SSL_TLS1_3_PSK_EXTERNAL:
580 ret = ssl_tls13_select_ciphersuite_for_psk(
Gilles Peskine449bd832023-01-11 14:50:10 +0100581 ssl, ciphersuites, ciphersuites_end,
582 &cipher_suite, &ciphersuite_info);
Jerry Yu0baf9072022-08-25 11:21:04 +0800583 break;
584 case MBEDTLS_SSL_TLS1_3_PSK_RESUMPTION:
Jerry Yu82534862022-08-30 10:42:33 +0800585#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Jerry Yu0baf9072022-08-25 11:21:04 +0800586 ret = ssl_tls13_select_ciphersuite_for_resumption(
Gilles Peskine449bd832023-01-11 14:50:10 +0100587 ssl, ciphersuites, ciphersuites_end, &session,
588 &cipher_suite, &ciphersuite_info);
589 if (ret != 0) {
590 mbedtls_ssl_session_free(&session);
591 }
Jerry Yu82534862022-08-30 10:42:33 +0800592#else
593 ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
594#endif
Jerry Yu0baf9072022-08-25 11:21:04 +0800595 break;
596 default:
Gilles Peskine449bd832023-01-11 14:50:10 +0100597 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Jerry Yu0baf9072022-08-25 11:21:04 +0800598 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100599 if (ret != 0) {
Jerry Yuf35ba382022-08-23 17:58:26 +0800600 /* See below, no cipher_suite available, abort handshake */
601 MBEDTLS_SSL_PEND_FATAL_ALERT(
602 MBEDTLS_SSL_ALERT_MSG_DECRYPT_ERROR,
Gilles Peskine449bd832023-01-11 14:50:10 +0100603 MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE);
Jerry Yuf35ba382022-08-23 17:58:26 +0800604 MBEDTLS_SSL_DEBUG_RET(
Gilles Peskine449bd832023-01-11 14:50:10 +0100605 2, "ssl_tls13_select_ciphersuite", ret);
606 return ret;
Jerry Yu5725f1c2022-08-21 17:27:16 +0800607 }
608
Jerry Yu96a2e362022-07-21 15:11:34 +0800609 ret = ssl_tls13_offered_psks_check_binder_match(
Gilles Peskine449bd832023-01-11 14:50:10 +0100610 ssl, binder, binder_len, psk_type,
611 mbedtls_psa_translate_md(ciphersuite_info->mac));
612 if (ret != SSL_TLS1_3_OFFERED_PSK_MATCH) {
Jerry Yuc5a23a02022-08-25 10:51:44 +0800613 /* For security reasons, the handshake should be aborted when we
614 * fail to validate a binder value. See RFC 8446 section 4.2.11.2
615 * and appendix E.6. */
Jerry Yu82534862022-08-30 10:42:33 +0800616#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Gilles Peskine449bd832023-01-11 14:50:10 +0100617 mbedtls_ssl_session_free(&session);
Jerry Yu82534862022-08-30 10:42:33 +0800618#endif
Gilles Peskine449bd832023-01-11 14:50:10 +0100619 MBEDTLS_SSL_DEBUG_MSG(3, ("Invalid binder."));
620 MBEDTLS_SSL_DEBUG_RET(1,
621 "ssl_tls13_offered_psks_check_binder_match", ret);
Jerry Yu96a2e362022-07-21 15:11:34 +0800622 MBEDTLS_SSL_PEND_FATAL_ALERT(
623 MBEDTLS_SSL_ALERT_MSG_DECRYPT_ERROR,
Gilles Peskine449bd832023-01-11 14:50:10 +0100624 MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE);
625 return ret;
Jerry Yu96a2e362022-07-21 15:11:34 +0800626 }
Jerry Yu96a2e362022-07-21 15:11:34 +0800627
628 matched_identity = identity_id;
Jerry Yu5725f1c2022-08-21 17:27:16 +0800629
630 /* Update handshake parameters */
Jerry Yue5834fd2022-08-29 20:16:09 +0800631 ssl->handshake->ciphersuite_info = ciphersuite_info;
Jerry Yu4746b102022-09-13 11:11:48 +0800632 ssl->session_negotiate->ciphersuite = cipher_suite;
Gilles Peskine449bd832023-01-11 14:50:10 +0100633 MBEDTLS_SSL_DEBUG_MSG(2, ("overwrite ciphersuite: %04x - %s",
634 cipher_suite, ciphersuite_info->name));
Jerry Yu82534862022-08-30 10:42:33 +0800635#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Gilles Peskine449bd832023-01-11 14:50:10 +0100636 if (psk_type == MBEDTLS_SSL_TLS1_3_PSK_RESUMPTION) {
637 ret = ssl_tls13_session_copy_ticket(ssl->session_negotiate,
638 &session);
639 mbedtls_ssl_session_free(&session);
640 if (ret != 0) {
641 return ret;
642 }
Jerry Yu82534862022-08-30 10:42:33 +0800643 }
644#endif /* MBEDTLS_SSL_SESSION_TICKETS */
Jerry Yu1c105562022-07-10 06:32:38 +0000645 }
646
Gilles Peskine449bd832023-01-11 14:50:10 +0100647 if (p_identity_len != identities_end || p_binder_len != binders_end) {
648 MBEDTLS_SSL_DEBUG_MSG(3, ("pre_shared_key extension decode error"));
649 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR,
650 MBEDTLS_ERR_SSL_DECODE_ERROR);
651 return MBEDTLS_ERR_SSL_DECODE_ERROR;
Jerry Yu1c105562022-07-10 06:32:38 +0000652 }
653
Jerry Yu6f1db3f2022-07-22 23:05:59 +0800654 /* Update the handshake transcript with the binder list. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100655 ssl->handshake->update_checksum(ssl,
656 identities_end,
657 (size_t) (binders_end - identities_end));
658 if (matched_identity == -1) {
659 MBEDTLS_SSL_DEBUG_MSG(3, ("No matched PSK or ticket."));
660 return MBEDTLS_ERR_SSL_UNKNOWN_IDENTITY;
Jerry Yu1c105562022-07-10 06:32:38 +0000661 }
662
Gilles Peskine449bd832023-01-11 14:50:10 +0100663 ssl->handshake->selected_identity = (uint16_t) matched_identity;
664 MBEDTLS_SSL_DEBUG_MSG(3, ("Pre shared key found"));
Jerry Yu1c105562022-07-10 06:32:38 +0000665
Gilles Peskine449bd832023-01-11 14:50:10 +0100666 return 0;
Jerry Yu1c105562022-07-10 06:32:38 +0000667}
Jerry Yu032b15ce2022-07-11 06:10:03 +0000668
669/*
670 * struct {
671 * select ( Handshake.msg_type ) {
672 * ....
673 * case server_hello:
674 * uint16 selected_identity;
675 * }
676 * } PreSharedKeyExtension;
677 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100678static int ssl_tls13_write_server_pre_shared_key_ext(mbedtls_ssl_context *ssl,
679 unsigned char *buf,
680 unsigned char *end,
681 size_t *olen)
Jerry Yu032b15ce2022-07-11 06:10:03 +0000682{
Gilles Peskine449bd832023-01-11 14:50:10 +0100683 unsigned char *p = (unsigned char *) buf;
Jerry Yu032b15ce2022-07-11 06:10:03 +0000684
685 *olen = 0;
686
David Horstmann21b89762022-10-06 18:34:28 +0100687 int not_using_psk = 0;
Jerry Yu032b15ce2022-07-11 06:10:03 +0000688#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +0100689 not_using_psk = (mbedtls_svc_key_id_is_null(ssl->handshake->psk_opaque));
Jerry Yu032b15ce2022-07-11 06:10:03 +0000690#else
Gilles Peskine449bd832023-01-11 14:50:10 +0100691 not_using_psk = (ssl->handshake->psk == NULL);
Jerry Yu032b15ce2022-07-11 06:10:03 +0000692#endif
Gilles Peskine449bd832023-01-11 14:50:10 +0100693 if (not_using_psk) {
Jerry Yu032b15ce2022-07-11 06:10:03 +0000694 /* We shouldn't have called this extension writer unless we've
695 * chosen to use a PSK. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100696 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Jerry Yu032b15ce2022-07-11 06:10:03 +0000697 }
698
Gilles Peskine449bd832023-01-11 14:50:10 +0100699 MBEDTLS_SSL_DEBUG_MSG(3, ("server hello, adding pre_shared_key extension"));
700 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 6);
Jerry Yu032b15ce2022-07-11 06:10:03 +0000701
Gilles Peskine449bd832023-01-11 14:50:10 +0100702 MBEDTLS_PUT_UINT16_BE(MBEDTLS_TLS_EXT_PRE_SHARED_KEY, p, 0);
703 MBEDTLS_PUT_UINT16_BE(2, p, 2);
Jerry Yu032b15ce2022-07-11 06:10:03 +0000704
Gilles Peskine449bd832023-01-11 14:50:10 +0100705 MBEDTLS_PUT_UINT16_BE(ssl->handshake->selected_identity, p, 4);
Jerry Yu032b15ce2022-07-11 06:10:03 +0000706
707 *olen = 6;
708
Gilles Peskine449bd832023-01-11 14:50:10 +0100709 MBEDTLS_SSL_DEBUG_MSG(4, ("sent selected_identity: %u",
710 ssl->handshake->selected_identity));
Jerry Yu032b15ce2022-07-11 06:10:03 +0000711
Gilles Peskine449bd832023-01-11 14:50:10 +0100712 mbedtls_ssl_tls13_set_hs_sent_ext_mask(ssl, MBEDTLS_TLS_EXT_PRE_SHARED_KEY);
Jerry Yub95dd362022-11-08 21:19:34 +0800713
Gilles Peskine449bd832023-01-11 14:50:10 +0100714 return 0;
Jerry Yu032b15ce2022-07-11 06:10:03 +0000715}
716
Ronald Cron41a443a2022-10-04 16:38:25 +0200717#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED */
Jerry Yue19e3b92022-07-08 12:04:51 +0000718
XiaokangQian7807f9f2022-02-15 10:04:37 +0000719/* From RFC 8446:
720 * struct {
XiaokangQiancfd925f2022-04-14 07:10:37 +0000721 * ProtocolVersion versions<2..254>;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000722 * } SupportedVersions;
723 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200724MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100725static int ssl_tls13_parse_supported_versions_ext(mbedtls_ssl_context *ssl,
726 const unsigned char *buf,
727 const unsigned char *end)
XiaokangQian7807f9f2022-02-15 10:04:37 +0000728{
XiaokangQian7807f9f2022-02-15 10:04:37 +0000729 const unsigned char *p = buf;
XiaokangQian8f9dfe42022-04-15 02:52:39 +0000730 size_t versions_len;
XiaokangQian4080a7f2022-04-11 09:55:18 +0000731 const unsigned char *versions_end;
XiaokangQian0a1b54e2022-04-21 03:01:38 +0000732 uint16_t tls_version;
XiaokangQian8f9dfe42022-04-15 02:52:39 +0000733 int tls13_supported = 0;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000734
Gilles Peskine449bd832023-01-11 14:50:10 +0100735 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, 1);
XiaokangQian4080a7f2022-04-11 09:55:18 +0000736 versions_len = p[0];
XiaokangQian7807f9f2022-02-15 10:04:37 +0000737 p += 1;
738
Gilles Peskine449bd832023-01-11 14:50:10 +0100739 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, versions_len);
XiaokangQian4080a7f2022-04-11 09:55:18 +0000740 versions_end = p + versions_len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100741 while (p < versions_end) {
742 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, versions_end, 2);
743 tls_version = mbedtls_ssl_read_version(p, ssl->conf->transport);
XiaokangQianb67384d2022-04-19 00:02:38 +0000744 p += 2;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000745
746 /* In this implementation we only support TLS 1.3 and DTLS 1.3. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100747 if (tls_version == MBEDTLS_SSL_VERSION_TLS1_3) {
XiaokangQian7807f9f2022-02-15 10:04:37 +0000748 tls13_supported = 1;
749 break;
750 }
XiaokangQian7807f9f2022-02-15 10:04:37 +0000751 }
752
Gilles Peskine449bd832023-01-11 14:50:10 +0100753 if (!tls13_supported) {
754 MBEDTLS_SSL_DEBUG_MSG(1, ("TLS 1.3 is not supported by the client"));
XiaokangQian7807f9f2022-02-15 10:04:37 +0000755
Gilles Peskine449bd832023-01-11 14:50:10 +0100756 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_PROTOCOL_VERSION,
757 MBEDTLS_ERR_SSL_BAD_PROTOCOL_VERSION);
758 return MBEDTLS_ERR_SSL_BAD_PROTOCOL_VERSION;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000759 }
760
Gilles Peskine449bd832023-01-11 14:50:10 +0100761 MBEDTLS_SSL_DEBUG_MSG(1, ("Negotiated version. Supported is [%04x]",
762 (unsigned int) tls_version));
XiaokangQian7807f9f2022-02-15 10:04:37 +0000763
Gilles Peskine449bd832023-01-11 14:50:10 +0100764 return 0;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000765}
766
XiaokangQian8f9dfe42022-04-15 02:52:39 +0000767#if defined(MBEDTLS_ECDH_C)
XiaokangQiane8ff3502022-04-22 02:34:40 +0000768/*
XiaokangQian7807f9f2022-02-15 10:04:37 +0000769 *
770 * From RFC 8446:
771 * enum {
772 * ... (0xFFFF)
773 * } NamedGroup;
774 * struct {
775 * NamedGroup named_group_list<2..2^16-1>;
776 * } NamedGroupList;
777 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200778MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100779static int ssl_tls13_parse_supported_groups_ext(mbedtls_ssl_context *ssl,
780 const unsigned char *buf,
781 const unsigned char *end)
XiaokangQian7807f9f2022-02-15 10:04:37 +0000782{
XiaokangQian7807f9f2022-02-15 10:04:37 +0000783 const unsigned char *p = buf;
XiaokangQian84823772022-04-19 07:57:30 +0000784 size_t named_group_list_len;
XiaokangQian8f9dfe42022-04-15 02:52:39 +0000785 const unsigned char *named_group_list_end;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000786
Gilles Peskine449bd832023-01-11 14:50:10 +0100787 MBEDTLS_SSL_DEBUG_BUF(3, "supported_groups extension", p, end - buf);
788 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, 2);
789 named_group_list_len = MBEDTLS_GET_UINT16_BE(p, 0);
XiaokangQian7807f9f2022-02-15 10:04:37 +0000790 p += 2;
Gilles Peskine449bd832023-01-11 14:50:10 +0100791 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, named_group_list_len);
XiaokangQian8f9dfe42022-04-15 02:52:39 +0000792 named_group_list_end = p + named_group_list_len;
XiaokangQian4e8cd7b2022-04-21 09:48:09 +0000793 ssl->handshake->hrr_selected_group = 0;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000794
Gilles Peskine449bd832023-01-11 14:50:10 +0100795 while (p < named_group_list_end) {
XiaokangQian08037552022-04-20 07:16:41 +0000796 uint16_t named_group;
Gilles Peskine449bd832023-01-11 14:50:10 +0100797 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, named_group_list_end, 2);
798 named_group = MBEDTLS_GET_UINT16_BE(p, 0);
XiaokangQian08037552022-04-20 07:16:41 +0000799 p += 2;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000800
Gilles Peskine449bd832023-01-11 14:50:10 +0100801 MBEDTLS_SSL_DEBUG_MSG(2,
802 ("got named group: %s(%04x)",
803 mbedtls_ssl_named_group_to_str(named_group),
804 named_group));
XiaokangQian08037552022-04-20 07:16:41 +0000805
Gilles Peskine449bd832023-01-11 14:50:10 +0100806 if (!mbedtls_ssl_named_group_is_offered(ssl, named_group) ||
807 !mbedtls_ssl_named_group_is_supported(named_group) ||
808 ssl->handshake->hrr_selected_group != 0) {
XiaokangQian08037552022-04-20 07:16:41 +0000809 continue;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000810 }
811
Gilles Peskine449bd832023-01-11 14:50:10 +0100812 MBEDTLS_SSL_DEBUG_MSG(2,
813 ("add named group %s(%04x) into received list.",
814 mbedtls_ssl_named_group_to_str(named_group),
815 named_group));
Jerry Yuc1be19f2022-04-23 16:11:39 +0800816
XiaokangQian4e8cd7b2022-04-21 09:48:09 +0000817 ssl->handshake->hrr_selected_group = named_group;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000818 }
819
Gilles Peskine449bd832023-01-11 14:50:10 +0100820 return 0;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000821
822}
XiaokangQian8f9dfe42022-04-15 02:52:39 +0000823#endif /* MBEDTLS_ECDH_C */
XiaokangQian7807f9f2022-02-15 10:04:37 +0000824
XiaokangQian08037552022-04-20 07:16:41 +0000825#define SSL_TLS1_3_PARSE_KEY_SHARES_EXT_NO_MATCH 1
826
XiaokangQian88408882022-04-02 10:15:03 +0000827#if defined(MBEDTLS_ECDH_C)
XiaokangQian7807f9f2022-02-15 10:04:37 +0000828/*
829 * ssl_tls13_parse_key_shares_ext() verifies whether the information in the
XiaokangQiane8ff3502022-04-22 02:34:40 +0000830 * extension is correct and stores the first acceptable key share and its associated group.
XiaokangQian7807f9f2022-02-15 10:04:37 +0000831 *
832 * Possible return values are:
833 * - 0: Successful processing of the client provided key share extension.
XiaokangQian08037552022-04-20 07:16:41 +0000834 * - SSL_TLS1_3_PARSE_KEY_SHARES_EXT_NO_MATCH: The key shares provided by the client
XiaokangQian7807f9f2022-02-15 10:04:37 +0000835 * does not match a group supported by the server. A HelloRetryRequest will
836 * be needed.
XiaokangQiane8ff3502022-04-22 02:34:40 +0000837 * - A negative value for fatal errors.
Jerry Yuc1be19f2022-04-23 16:11:39 +0800838 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200839MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100840static int ssl_tls13_parse_key_shares_ext(mbedtls_ssl_context *ssl,
841 const unsigned char *buf,
842 const unsigned char *end)
XiaokangQian7807f9f2022-02-15 10:04:37 +0000843{
XiaokangQianb67384d2022-04-19 00:02:38 +0000844 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000845 unsigned char const *p = buf;
XiaokangQian8f9dfe42022-04-15 02:52:39 +0000846 unsigned char const *client_shares_end;
Jerry Yuc1be19f2022-04-23 16:11:39 +0800847 size_t client_shares_len;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000848
849 /* From RFC 8446:
850 *
851 * struct {
852 * KeyShareEntry client_shares<0..2^16-1>;
853 * } KeyShareClientHello;
854 *
855 */
856
Gilles Peskine449bd832023-01-11 14:50:10 +0100857 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, 2);
858 client_shares_len = MBEDTLS_GET_UINT16_BE(p, 0);
XiaokangQian7807f9f2022-02-15 10:04:37 +0000859 p += 2;
Gilles Peskine449bd832023-01-11 14:50:10 +0100860 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, client_shares_len);
XiaokangQian7807f9f2022-02-15 10:04:37 +0000861
862 ssl->handshake->offered_group_id = 0;
XiaokangQian8f9dfe42022-04-15 02:52:39 +0000863 client_shares_end = p + client_shares_len;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000864
865 /* We try to find a suitable key share entry and copy it to the
866 * handshake context. Later, we have to find out whether we can do
867 * something with the provided key share or whether we have to
XiaokangQianc5763b52022-04-02 03:34:37 +0000868 * dismiss it and send a HelloRetryRequest message.
869 */
XiaokangQian7807f9f2022-02-15 10:04:37 +0000870
Gilles Peskine449bd832023-01-11 14:50:10 +0100871 while (p < client_shares_end) {
XiaokangQian9b5d04b2022-04-10 10:20:43 +0000872 uint16_t group;
Jerry Yuc1be19f2022-04-23 16:11:39 +0800873 size_t key_exchange_len;
Jerry Yu086edc22022-05-05 10:50:38 +0800874 const unsigned char *key_exchange;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000875
876 /*
877 * struct {
878 * NamedGroup group;
879 * opaque key_exchange<1..2^16-1>;
880 * } KeyShareEntry;
881 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100882 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, client_shares_end, 4);
883 group = MBEDTLS_GET_UINT16_BE(p, 0);
884 key_exchange_len = MBEDTLS_GET_UINT16_BE(p, 2);
Jerry Yuc1be19f2022-04-23 16:11:39 +0800885 p += 4;
Jerry Yu086edc22022-05-05 10:50:38 +0800886 key_exchange = p;
Gilles Peskine449bd832023-01-11 14:50:10 +0100887 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, client_shares_end, key_exchange_len);
Jerry Yu086edc22022-05-05 10:50:38 +0800888 p += key_exchange_len;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000889
890 /* Continue parsing even if we have already found a match,
XiaokangQianc5763b52022-04-02 03:34:37 +0000891 * for input validation purposes.
892 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100893 if (!mbedtls_ssl_named_group_is_offered(ssl, group) ||
894 !mbedtls_ssl_named_group_is_supported(group) ||
895 ssl->handshake->offered_group_id != 0) {
XiaokangQian060d8672022-04-21 09:24:56 +0000896 continue;
897 }
XiaokangQian060d8672022-04-21 09:24:56 +0000898
XiaokangQian7807f9f2022-02-15 10:04:37 +0000899 /*
XiaokangQian8f9dfe42022-04-15 02:52:39 +0000900 * For now, we only support ECDHE groups.
XiaokangQian7807f9f2022-02-15 10:04:37 +0000901 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100902 if (mbedtls_ssl_tls13_named_group_is_ecdhe(group)) {
903 MBEDTLS_SSL_DEBUG_MSG(2, ("ECDH group: %s (%04x)",
904 mbedtls_ssl_named_group_to_str(group),
905 group));
XiaokangQian318dc762022-04-20 09:43:51 +0000906 ret = mbedtls_ssl_tls13_read_public_ecdhe_share(
Gilles Peskine449bd832023-01-11 14:50:10 +0100907 ssl, key_exchange - 2, key_exchange_len + 2);
908 if (ret != 0) {
909 return ret;
910 }
XiaokangQian4e8cd7b2022-04-21 09:48:09 +0000911
Gilles Peskine449bd832023-01-11 14:50:10 +0100912 } else {
913 MBEDTLS_SSL_DEBUG_MSG(4, ("Unrecognized NamedGroup %u",
914 (unsigned) group));
XiaokangQian7807f9f2022-02-15 10:04:37 +0000915 continue;
916 }
917
XiaokangQian9b5d04b2022-04-10 10:20:43 +0000918 ssl->handshake->offered_group_id = group;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000919 }
920
Jerry Yuc1be19f2022-04-23 16:11:39 +0800921
Gilles Peskine449bd832023-01-11 14:50:10 +0100922 if (ssl->handshake->offered_group_id == 0) {
923 MBEDTLS_SSL_DEBUG_MSG(1, ("no matching key share"));
924 return SSL_TLS1_3_PARSE_KEY_SHARES_EXT_NO_MATCH;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000925 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100926 return 0;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000927}
XiaokangQian88408882022-04-02 10:15:03 +0000928#endif /* MBEDTLS_ECDH_C */
XiaokangQian7807f9f2022-02-15 10:04:37 +0000929
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200930MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100931static int ssl_tls13_client_hello_has_exts(mbedtls_ssl_context *ssl,
932 int exts_mask)
XiaokangQian7807f9f2022-02-15 10:04:37 +0000933{
Jerry Yu0c354a22022-08-29 15:25:36 +0800934 int masked = ssl->handshake->received_extensions & exts_mask;
Gilles Peskine449bd832023-01-11 14:50:10 +0100935 return masked == exts_mask;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000936}
937
Jerry Yue5991322022-11-07 14:03:44 +0800938#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200939MBEDTLS_CHECK_RETURN_CRITICAL
XiaokangQianb67384d2022-04-19 00:02:38 +0000940static int ssl_tls13_client_hello_has_exts_for_ephemeral_key_exchange(
Gilles Peskine449bd832023-01-11 14:50:10 +0100941 mbedtls_ssl_context *ssl)
XiaokangQian7807f9f2022-02-15 10:04:37 +0000942{
Gilles Peskine449bd832023-01-11 14:50:10 +0100943 return ssl_tls13_client_hello_has_exts(
944 ssl,
945 MBEDTLS_SSL_EXT_MASK(SUPPORTED_GROUPS) |
946 MBEDTLS_SSL_EXT_MASK(KEY_SHARE) |
947 MBEDTLS_SSL_EXT_MASK(SIG_ALG));
XiaokangQian7807f9f2022-02-15 10:04:37 +0000948}
Jerry Yue5991322022-11-07 14:03:44 +0800949#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED */
XiaokangQian7807f9f2022-02-15 10:04:37 +0000950
Jerry Yue5991322022-11-07 14:03:44 +0800951#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK_ENABLED)
Jerry Yu77f01482022-07-11 07:03:24 +0000952MBEDTLS_CHECK_RETURN_CRITICAL
953static int ssl_tls13_client_hello_has_exts_for_psk_key_exchange(
Gilles Peskine449bd832023-01-11 14:50:10 +0100954 mbedtls_ssl_context *ssl)
Jerry Yu77f01482022-07-11 07:03:24 +0000955{
Gilles Peskine449bd832023-01-11 14:50:10 +0100956 return ssl_tls13_client_hello_has_exts(
957 ssl,
958 MBEDTLS_SSL_EXT_MASK(PRE_SHARED_KEY) |
959 MBEDTLS_SSL_EXT_MASK(PSK_KEY_EXCHANGE_MODES));
Jerry Yu77f01482022-07-11 07:03:24 +0000960}
Jerry Yue5991322022-11-07 14:03:44 +0800961#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK_ENABLED */
Jerry Yu77f01482022-07-11 07:03:24 +0000962
Jerry Yue5991322022-11-07 14:03:44 +0800963#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK_EPHEMERAL_ENABLED)
Jerry Yu77f01482022-07-11 07:03:24 +0000964MBEDTLS_CHECK_RETURN_CRITICAL
965static int ssl_tls13_client_hello_has_exts_for_psk_ephemeral_key_exchange(
Gilles Peskine449bd832023-01-11 14:50:10 +0100966 mbedtls_ssl_context *ssl)
Jerry Yu77f01482022-07-11 07:03:24 +0000967{
Gilles Peskine449bd832023-01-11 14:50:10 +0100968 return ssl_tls13_client_hello_has_exts(
969 ssl,
970 MBEDTLS_SSL_EXT_MASK(SUPPORTED_GROUPS) |
971 MBEDTLS_SSL_EXT_MASK(KEY_SHARE) |
972 MBEDTLS_SSL_EXT_MASK(PRE_SHARED_KEY) |
973 MBEDTLS_SSL_EXT_MASK(PSK_KEY_EXCHANGE_MODES));
Jerry Yu77f01482022-07-11 07:03:24 +0000974}
Jerry Yue5991322022-11-07 14:03:44 +0800975#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK_EPHEMERAL_ENABLED */
Jerry Yu77f01482022-07-11 07:03:24 +0000976
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200977MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100978static int ssl_tls13_check_ephemeral_key_exchange(mbedtls_ssl_context *ssl)
XiaokangQian7807f9f2022-02-15 10:04:37 +0000979{
Jerry Yue5991322022-11-07 14:03:44 +0800980#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +0100981 return mbedtls_ssl_conf_tls13_ephemeral_enabled(ssl) &&
982 ssl_tls13_client_hello_has_exts_for_ephemeral_key_exchange(ssl);
Jerry Yue5991322022-11-07 14:03:44 +0800983#else
984 ((void) ssl);
Gilles Peskine449bd832023-01-11 14:50:10 +0100985 return 0;
Jerry Yue5991322022-11-07 14:03:44 +0800986#endif
Jerry Yu77f01482022-07-11 07:03:24 +0000987}
XiaokangQian7807f9f2022-02-15 10:04:37 +0000988
Jerry Yu77f01482022-07-11 07:03:24 +0000989MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100990static int ssl_tls13_check_psk_key_exchange(mbedtls_ssl_context *ssl)
Jerry Yu77f01482022-07-11 07:03:24 +0000991{
Jerry Yue5991322022-11-07 14:03:44 +0800992#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +0100993 return mbedtls_ssl_conf_tls13_psk_enabled(ssl) &&
994 mbedtls_ssl_tls13_psk_enabled(ssl) &&
995 ssl_tls13_client_hello_has_exts_for_psk_key_exchange(ssl);
Jerry Yu77f01482022-07-11 07:03:24 +0000996#else
997 ((void) ssl);
Gilles Peskine449bd832023-01-11 14:50:10 +0100998 return 0;
Jerry Yu77f01482022-07-11 07:03:24 +0000999#endif
1000}
XiaokangQian7807f9f2022-02-15 10:04:37 +00001001
Jerry Yu77f01482022-07-11 07:03:24 +00001002MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01001003static int ssl_tls13_check_psk_ephemeral_key_exchange(mbedtls_ssl_context *ssl)
Jerry Yu77f01482022-07-11 07:03:24 +00001004{
Jerry Yue5991322022-11-07 14:03:44 +08001005#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK_EPHEMERAL_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01001006 return mbedtls_ssl_conf_tls13_psk_ephemeral_enabled(ssl) &&
1007 mbedtls_ssl_tls13_psk_ephemeral_enabled(ssl) &&
1008 ssl_tls13_client_hello_has_exts_for_psk_ephemeral_key_exchange(ssl);
Jerry Yu77f01482022-07-11 07:03:24 +00001009#else
1010 ((void) ssl);
Gilles Peskine449bd832023-01-11 14:50:10 +01001011 return 0;
Jerry Yu77f01482022-07-11 07:03:24 +00001012#endif
1013}
1014
Gilles Peskine449bd832023-01-11 14:50:10 +01001015static int ssl_tls13_determine_key_exchange_mode(mbedtls_ssl_context *ssl)
Jerry Yu77f01482022-07-11 07:03:24 +00001016{
1017 /*
1018 * Determine the key exchange algorithm to use.
1019 * There are three types of key exchanges supported in TLS 1.3:
1020 * - (EC)DH with ECDSA,
1021 * - (EC)DH with PSK,
1022 * - plain PSK.
1023 *
1024 * The PSK-based key exchanges may additionally be used with 0-RTT.
1025 *
1026 * Our built-in order of preference is
Jerry Yuce6ed702022-07-22 21:49:53 +08001027 * 1 ) (EC)DHE-PSK Mode ( psk_ephemeral )
1028 * 2 ) Certificate Mode ( ephemeral )
1029 * 3 ) Plain PSK Mode ( psk )
Jerry Yu77f01482022-07-11 07:03:24 +00001030 */
1031
1032 ssl->handshake->key_exchange_mode = MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_NONE;
1033
Gilles Peskine449bd832023-01-11 14:50:10 +01001034 if (ssl_tls13_check_psk_ephemeral_key_exchange(ssl)) {
Jerry Yu77f01482022-07-11 07:03:24 +00001035 ssl->handshake->key_exchange_mode =
1036 MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK_EPHEMERAL;
Gilles Peskine449bd832023-01-11 14:50:10 +01001037 MBEDTLS_SSL_DEBUG_MSG(2, ("key exchange mode: psk_ephemeral"));
1038 } else
1039 if (ssl_tls13_check_ephemeral_key_exchange(ssl)) {
Jerry Yu77f01482022-07-11 07:03:24 +00001040 ssl->handshake->key_exchange_mode =
1041 MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL;
Gilles Peskine449bd832023-01-11 14:50:10 +01001042 MBEDTLS_SSL_DEBUG_MSG(2, ("key exchange mode: ephemeral"));
1043 } else
1044 if (ssl_tls13_check_psk_key_exchange(ssl)) {
Jerry Yuce6ed702022-07-22 21:49:53 +08001045 ssl->handshake->key_exchange_mode =
1046 MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK;
Gilles Peskine449bd832023-01-11 14:50:10 +01001047 MBEDTLS_SSL_DEBUG_MSG(2, ("key exchange mode: psk"));
1048 } else {
Jerry Yu77f01482022-07-11 07:03:24 +00001049 MBEDTLS_SSL_DEBUG_MSG(
Gilles Peskine449bd832023-01-11 14:50:10 +01001050 1,
1051 ("ClientHello message misses mandatory extensions."));
1052 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_MISSING_EXTENSION,
1053 MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER);
1054 return MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER;
Jerry Yu77f01482022-07-11 07:03:24 +00001055 }
1056
Gilles Peskine449bd832023-01-11 14:50:10 +01001057 return 0;
Jerry Yu77f01482022-07-11 07:03:24 +00001058
XiaokangQian7807f9f2022-02-15 10:04:37 +00001059}
1060
XiaokangQian81802f42022-06-10 13:25:22 +00001061#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
Ronald Cron928cbd32022-10-04 16:14:26 +02001062 defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED)
Ronald Cron67ea2542022-09-15 17:34:42 +02001063
1064#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01001065static psa_algorithm_t ssl_tls13_iana_sig_alg_to_psa_alg(uint16_t sig_alg)
Ronald Cron67ea2542022-09-15 17:34:42 +02001066{
Gilles Peskine449bd832023-01-11 14:50:10 +01001067 switch (sig_alg) {
Ronald Cron67ea2542022-09-15 17:34:42 +02001068 case MBEDTLS_TLS1_3_SIG_ECDSA_SECP256R1_SHA256:
Gilles Peskine449bd832023-01-11 14:50:10 +01001069 return PSA_ALG_ECDSA(PSA_ALG_SHA_256);
Ronald Cron67ea2542022-09-15 17:34:42 +02001070 case MBEDTLS_TLS1_3_SIG_ECDSA_SECP384R1_SHA384:
Gilles Peskine449bd832023-01-11 14:50:10 +01001071 return PSA_ALG_ECDSA(PSA_ALG_SHA_384);
Ronald Cron67ea2542022-09-15 17:34:42 +02001072 case MBEDTLS_TLS1_3_SIG_ECDSA_SECP521R1_SHA512:
Gilles Peskine449bd832023-01-11 14:50:10 +01001073 return PSA_ALG_ECDSA(PSA_ALG_SHA_512);
Ronald Cron67ea2542022-09-15 17:34:42 +02001074 case MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA256:
Gilles Peskine449bd832023-01-11 14:50:10 +01001075 return PSA_ALG_RSA_PSS(PSA_ALG_SHA_256);
Ronald Cron67ea2542022-09-15 17:34:42 +02001076 case MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA384:
Gilles Peskine449bd832023-01-11 14:50:10 +01001077 return PSA_ALG_RSA_PSS(PSA_ALG_SHA_384);
Ronald Cron67ea2542022-09-15 17:34:42 +02001078 case MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA512:
Gilles Peskine449bd832023-01-11 14:50:10 +01001079 return PSA_ALG_RSA_PSS(PSA_ALG_SHA_512);
Ronald Cron67ea2542022-09-15 17:34:42 +02001080 case MBEDTLS_TLS1_3_SIG_RSA_PKCS1_SHA256:
Gilles Peskine449bd832023-01-11 14:50:10 +01001081 return PSA_ALG_RSA_PKCS1V15_SIGN(PSA_ALG_SHA_256);
Ronald Cron67ea2542022-09-15 17:34:42 +02001082 case MBEDTLS_TLS1_3_SIG_RSA_PKCS1_SHA384:
Gilles Peskine449bd832023-01-11 14:50:10 +01001083 return PSA_ALG_RSA_PKCS1V15_SIGN(PSA_ALG_SHA_384);
Ronald Cron67ea2542022-09-15 17:34:42 +02001084 case MBEDTLS_TLS1_3_SIG_RSA_PKCS1_SHA512:
Gilles Peskine449bd832023-01-11 14:50:10 +01001085 return PSA_ALG_RSA_PKCS1V15_SIGN(PSA_ALG_SHA_512);
Ronald Cron67ea2542022-09-15 17:34:42 +02001086 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01001087 return PSA_ALG_NONE;
Ronald Cron67ea2542022-09-15 17:34:42 +02001088 }
1089}
1090#endif /* MBEDTLS_USE_PSA_CRYPTO */
1091
XiaokangQian23c5be62022-06-07 02:04:34 +00001092/*
XiaokangQianfb665a82022-06-15 03:57:21 +00001093 * Pick best ( private key, certificate chain ) pair based on the signature
1094 * algorithms supported by the client.
XiaokangQian23c5be62022-06-07 02:04:34 +00001095 */
Ronald Cronce7d76e2022-07-08 18:56:49 +02001096MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01001097static int ssl_tls13_pick_key_cert(mbedtls_ssl_context *ssl)
XiaokangQian23c5be62022-06-07 02:04:34 +00001098{
XiaokangQianfb665a82022-06-15 03:57:21 +00001099 mbedtls_ssl_key_cert *key_cert, *key_cert_list;
XiaokangQian81802f42022-06-10 13:25:22 +00001100 const uint16_t *sig_alg = ssl->handshake->received_sig_algs;
XiaokangQian23c5be62022-06-07 02:04:34 +00001101
1102#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Gilles Peskine449bd832023-01-11 14:50:10 +01001103 if (ssl->handshake->sni_key_cert != NULL) {
XiaokangQianfb665a82022-06-15 03:57:21 +00001104 key_cert_list = ssl->handshake->sni_key_cert;
Gilles Peskine449bd832023-01-11 14:50:10 +01001105 } else
XiaokangQian23c5be62022-06-07 02:04:34 +00001106#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
Gilles Peskine449bd832023-01-11 14:50:10 +01001107 key_cert_list = ssl->conf->key_cert;
XiaokangQian23c5be62022-06-07 02:04:34 +00001108
Gilles Peskine449bd832023-01-11 14:50:10 +01001109 if (key_cert_list == NULL) {
1110 MBEDTLS_SSL_DEBUG_MSG(3, ("server has no certificate"));
1111 return -1;
XiaokangQian23c5be62022-06-07 02:04:34 +00001112 }
1113
Gilles Peskine449bd832023-01-11 14:50:10 +01001114 for (; *sig_alg != MBEDTLS_TLS1_3_SIG_NONE; sig_alg++) {
1115 if (!mbedtls_ssl_sig_alg_is_offered(ssl, *sig_alg)) {
Ronald Cron67ea2542022-09-15 17:34:42 +02001116 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +01001117 }
Ronald Cron67ea2542022-09-15 17:34:42 +02001118
Gilles Peskine449bd832023-01-11 14:50:10 +01001119 if (!mbedtls_ssl_tls13_sig_alg_for_cert_verify_is_supported(*sig_alg)) {
Ronald Cron67ea2542022-09-15 17:34:42 +02001120 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +01001121 }
Ronald Cron67ea2542022-09-15 17:34:42 +02001122
Gilles Peskine449bd832023-01-11 14:50:10 +01001123 for (key_cert = key_cert_list; key_cert != NULL;
1124 key_cert = key_cert->next) {
Ronald Cron67ea2542022-09-15 17:34:42 +02001125#if defined(MBEDTLS_USE_PSA_CRYPTO)
1126 psa_algorithm_t psa_alg = PSA_ALG_NONE;
1127#endif /* MBEDTLS_USE_PSA_CRYPTO */
1128
Gilles Peskine449bd832023-01-11 14:50:10 +01001129 MBEDTLS_SSL_DEBUG_CRT(3, "certificate (chain) candidate",
1130 key_cert->cert);
XiaokangQian81802f42022-06-10 13:25:22 +00001131
1132 /*
Gilles Peskine449bd832023-01-11 14:50:10 +01001133 * This avoids sending the client a cert it'll reject based on
1134 * keyUsage or other extensions.
1135 */
1136 if (mbedtls_x509_crt_check_key_usage(
1137 key_cert->cert, MBEDTLS_X509_KU_DIGITAL_SIGNATURE) != 0 ||
XiaokangQian81802f42022-06-10 13:25:22 +00001138 mbedtls_x509_crt_check_extended_key_usage(
XiaokangQianfb665a82022-06-15 03:57:21 +00001139 key_cert->cert, MBEDTLS_OID_SERVER_AUTH,
Gilles Peskine449bd832023-01-11 14:50:10 +01001140 MBEDTLS_OID_SIZE(MBEDTLS_OID_SERVER_AUTH)) != 0) {
1141 MBEDTLS_SSL_DEBUG_MSG(3, ("certificate mismatch: "
1142 "(extended) key usage extension"));
XiaokangQian81802f42022-06-10 13:25:22 +00001143 continue;
1144 }
XiaokangQian23c5be62022-06-07 02:04:34 +00001145
Gilles Peskine449bd832023-01-11 14:50:10 +01001146 MBEDTLS_SSL_DEBUG_MSG(3,
1147 ("ssl_tls13_pick_key_cert:"
1148 "check signature algorithm %s [%04x]",
1149 mbedtls_ssl_sig_alg_to_str(*sig_alg),
1150 *sig_alg));
Ronald Cron67ea2542022-09-15 17:34:42 +02001151#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01001152 psa_alg = ssl_tls13_iana_sig_alg_to_psa_alg(*sig_alg);
Ronald Cron67ea2542022-09-15 17:34:42 +02001153#endif /* MBEDTLS_USE_PSA_CRYPTO */
1154
Gilles Peskine449bd832023-01-11 14:50:10 +01001155 if (mbedtls_ssl_tls13_check_sig_alg_cert_key_match(
1156 *sig_alg, &key_cert->cert->pk)
Ronald Cron67ea2542022-09-15 17:34:42 +02001157#if defined(MBEDTLS_USE_PSA_CRYPTO)
1158 && psa_alg != PSA_ALG_NONE &&
Gilles Peskine449bd832023-01-11 14:50:10 +01001159 mbedtls_pk_can_do_ext(&key_cert->cert->pk, psa_alg,
1160 PSA_KEY_USAGE_SIGN_HASH) == 1
Ronald Cron67ea2542022-09-15 17:34:42 +02001161#endif /* MBEDTLS_USE_PSA_CRYPTO */
Gilles Peskine449bd832023-01-11 14:50:10 +01001162 ) {
XiaokangQianfb665a82022-06-15 03:57:21 +00001163 ssl->handshake->key_cert = key_cert;
Gilles Peskine449bd832023-01-11 14:50:10 +01001164 MBEDTLS_SSL_DEBUG_MSG(3,
1165 ("ssl_tls13_pick_key_cert:"
1166 "selected signature algorithm"
1167 " %s [%04x]",
1168 mbedtls_ssl_sig_alg_to_str(*sig_alg),
1169 *sig_alg));
XiaokangQianfb665a82022-06-15 03:57:21 +00001170 MBEDTLS_SSL_DEBUG_CRT(
Gilles Peskine449bd832023-01-11 14:50:10 +01001171 3, "selected certificate (chain)",
1172 ssl->handshake->key_cert->cert);
1173 return 0;
XiaokangQian81802f42022-06-10 13:25:22 +00001174 }
XiaokangQian81802f42022-06-10 13:25:22 +00001175 }
XiaokangQian23c5be62022-06-07 02:04:34 +00001176 }
1177
Gilles Peskine449bd832023-01-11 14:50:10 +01001178 MBEDTLS_SSL_DEBUG_MSG(2, ("ssl_tls13_pick_key_cert:"
1179 "no suitable certificate found"));
1180 return -1;
XiaokangQian23c5be62022-06-07 02:04:34 +00001181}
XiaokangQian81802f42022-06-10 13:25:22 +00001182#endif /* MBEDTLS_X509_CRT_PARSE_C &&
Ronald Cron928cbd32022-10-04 16:14:26 +02001183 MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED */
XiaokangQian23c5be62022-06-07 02:04:34 +00001184
XiaokangQian4080a7f2022-04-11 09:55:18 +00001185/*
XiaokangQianed582dd2022-04-13 08:21:05 +00001186 *
1187 * STATE HANDLING: ClientHello
1188 *
XiaokangQianb67384d2022-04-19 00:02:38 +00001189 * There are three possible classes of outcomes when parsing the ClientHello:
XiaokangQianed582dd2022-04-13 08:21:05 +00001190 *
XiaokangQianb67384d2022-04-19 00:02:38 +00001191 * 1) The ClientHello was well-formed and matched the server's configuration.
XiaokangQianed582dd2022-04-13 08:21:05 +00001192 *
1193 * In this case, the server progresses to sending its ServerHello.
1194 *
XiaokangQianb67384d2022-04-19 00:02:38 +00001195 * 2) The ClientHello was well-formed but didn't match the server's
1196 * configuration.
XiaokangQianed582dd2022-04-13 08:21:05 +00001197 *
1198 * For example, the client might not have offered a key share which
1199 * the server supports, or the server might require a cookie.
1200 *
1201 * In this case, the server sends a HelloRetryRequest.
1202 *
XiaokangQianb67384d2022-04-19 00:02:38 +00001203 * 3) The ClientHello was ill-formed
XiaokangQianed582dd2022-04-13 08:21:05 +00001204 *
1205 * In this case, we abort the handshake.
1206 *
1207 */
1208
1209/*
XiaokangQian4080a7f2022-04-11 09:55:18 +00001210 * Structure of this message:
1211 *
XiaokangQiane8ff3502022-04-22 02:34:40 +00001212 * uint16 ProtocolVersion;
1213 * opaque Random[32];
1214 * uint8 CipherSuite[2]; // Cryptographic suite selector
XiaokangQian4080a7f2022-04-11 09:55:18 +00001215 *
XiaokangQiane8ff3502022-04-22 02:34:40 +00001216 * struct {
1217 * ProtocolVersion legacy_version = 0x0303; // TLS v1.2
1218 * Random random;
1219 * opaque legacy_session_id<0..32>;
1220 * CipherSuite cipher_suites<2..2^16-2>;
1221 * opaque legacy_compression_methods<1..2^8-1>;
1222 * Extension extensions<8..2^16-1>;
1223 * } ClientHello;
XiaokangQian4080a7f2022-04-11 09:55:18 +00001224 */
XiaokangQianed582dd2022-04-13 08:21:05 +00001225
1226#define SSL_CLIENT_HELLO_OK 0
1227#define SSL_CLIENT_HELLO_HRR_REQUIRED 1
1228
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001229MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01001230static int ssl_tls13_parse_client_hello(mbedtls_ssl_context *ssl,
1231 const unsigned char *buf,
1232 const unsigned char *end)
XiaokangQian7807f9f2022-02-15 10:04:37 +00001233{
XiaokangQianb67384d2022-04-19 00:02:38 +00001234 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1235 const unsigned char *p = buf;
XiaokangQian4080a7f2022-04-11 09:55:18 +00001236 size_t legacy_session_id_len;
XiaokangQian318dc762022-04-20 09:43:51 +00001237 size_t cipher_suites_len;
XiaokangQian060d8672022-04-21 09:24:56 +00001238 const unsigned char *cipher_suites_end;
XiaokangQianb67384d2022-04-19 00:02:38 +00001239 size_t extensions_len;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001240 const unsigned char *extensions_end;
Jerry Yuc4bf5d62022-10-29 09:08:47 +08001241 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
Jerry Yu49ca9282022-05-05 11:05:22 +08001242 int hrr_required = 0;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001243
Ronald Cron41a443a2022-10-04 16:38:25 +02001244#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED)
Jerry Yu5725f1c2022-08-21 17:27:16 +08001245 const unsigned char *cipher_suites;
Jerry Yu29d9faa2022-08-23 17:52:45 +08001246 const unsigned char *pre_shared_key_ext = NULL;
Jerry Yu1c105562022-07-10 06:32:38 +00001247 const unsigned char *pre_shared_key_ext_end = NULL;
Ronald Cron41a443a2022-10-04 16:38:25 +02001248#endif
XiaokangQian7807f9f2022-02-15 10:04:37 +00001249
XiaokangQian7807f9f2022-02-15 10:04:37 +00001250 /*
XiaokangQianb67384d2022-04-19 00:02:38 +00001251 * ClientHello layout:
XiaokangQian7807f9f2022-02-15 10:04:37 +00001252 * 0 . 1 protocol version
XiaokangQiane8ff3502022-04-22 02:34:40 +00001253 * 2 . 33 random bytes
XiaokangQianc5763b52022-04-02 03:34:37 +00001254 * 34 . 34 session id length ( 1 byte )
XiaokangQian7807f9f2022-02-15 10:04:37 +00001255 * 35 . 34+x session id
XiaokangQian7807f9f2022-02-15 10:04:37 +00001256 * .. . .. ciphersuite list length ( 2 bytes )
1257 * .. . .. ciphersuite list
1258 * .. . .. compression alg. list length ( 1 byte )
1259 * .. . .. compression alg. list
1260 * .. . .. extensions length ( 2 bytes, optional )
1261 * .. . .. extensions ( optional )
1262 */
1263
XiaokangQianb67384d2022-04-19 00:02:38 +00001264 /*
bootstrap-prime6dbbf442022-05-17 19:30:44 -04001265 * Minimal length ( with everything empty and extensions omitted ) is
XiaokangQian7807f9f2022-02-15 10:04:37 +00001266 * 2 + 32 + 1 + 2 + 1 = 38 bytes. Check that first, so that we can
1267 * read at least up to session id length without worrying.
1268 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001269 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, 38);
XiaokangQian7807f9f2022-02-15 10:04:37 +00001270
1271 /* ...
1272 * ProtocolVersion legacy_version = 0x0303; // TLS 1.2
1273 * ...
1274 * with ProtocolVersion defined as:
1275 * uint16 ProtocolVersion;
1276 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001277 if (mbedtls_ssl_read_version(p, ssl->conf->transport) !=
1278 MBEDTLS_SSL_VERSION_TLS1_2) {
1279 MBEDTLS_SSL_DEBUG_MSG(1, ("Unsupported version of TLS."));
1280 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_PROTOCOL_VERSION,
1281 MBEDTLS_ERR_SSL_BAD_PROTOCOL_VERSION);
1282 return MBEDTLS_ERR_SSL_BAD_PROTOCOL_VERSION;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001283 }
1284 p += 2;
1285
1286 /*
XiaokangQianf8ceb942022-04-15 11:43:27 +00001287 * Only support TLS 1.3 currently, temporarily set the version.
1288 */
XiaokangQiande333912022-04-20 08:49:42 +00001289 ssl->tls_version = MBEDTLS_SSL_VERSION_TLS1_3;
XiaokangQianf8ceb942022-04-15 11:43:27 +00001290
Jerry Yue67bef42022-07-07 07:29:42 +00001291#if defined(MBEDTLS_SSL_SESSION_TICKETS)
1292 /* Store minor version for later use with ticket serialization. */
1293 ssl->session_negotiate->tls_version = MBEDTLS_SSL_VERSION_TLS1_3;
Jerry Yua66fece2022-07-13 14:30:29 +08001294 ssl->session_negotiate->endpoint = ssl->conf->endpoint;
Jerry Yufca4d572022-07-21 10:37:48 +08001295#endif
Jerry Yue67bef42022-07-07 07:29:42 +00001296
Jerry Yuc1be19f2022-04-23 16:11:39 +08001297 /* ...
1298 * Random random;
1299 * ...
XiaokangQianb67384d2022-04-19 00:02:38 +00001300 * with Random defined as:
1301 * opaque Random[32];
XiaokangQian7807f9f2022-02-15 10:04:37 +00001302 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001303 MBEDTLS_SSL_DEBUG_BUF(3, "client hello, random bytes",
1304 p, MBEDTLS_CLIENT_HELLO_RANDOM_LEN);
XiaokangQian7807f9f2022-02-15 10:04:37 +00001305
Gilles Peskine449bd832023-01-11 14:50:10 +01001306 memcpy(&handshake->randbytes[0], p, MBEDTLS_CLIENT_HELLO_RANDOM_LEN);
XiaokangQian08037552022-04-20 07:16:41 +00001307 p += MBEDTLS_CLIENT_HELLO_RANDOM_LEN;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001308
Jerry Yuc1be19f2022-04-23 16:11:39 +08001309 /* ...
XiaokangQianb67384d2022-04-19 00:02:38 +00001310 * opaque legacy_session_id<0..32>;
Jerry Yuc1be19f2022-04-23 16:11:39 +08001311 * ...
XiaokangQian7807f9f2022-02-15 10:04:37 +00001312 */
XiaokangQian4080a7f2022-04-11 09:55:18 +00001313 legacy_session_id_len = p[0];
XiaokangQianc5763b52022-04-02 03:34:37 +00001314 p++;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001315
Gilles Peskine449bd832023-01-11 14:50:10 +01001316 if (legacy_session_id_len > sizeof(ssl->session_negotiate->id)) {
1317 MBEDTLS_SSL_DEBUG_MSG(1, ("bad client hello message"));
1318 return MBEDTLS_ERR_SSL_DECODE_ERROR;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001319 }
1320
XiaokangQian4080a7f2022-04-11 09:55:18 +00001321 ssl->session_negotiate->id_len = legacy_session_id_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01001322 MBEDTLS_SSL_DEBUG_BUF(3, "client hello, session id",
1323 p, legacy_session_id_len);
XiaokangQianb67384d2022-04-19 00:02:38 +00001324 /*
1325 * Check we have enough data for the legacy session identifier
Jerry Yue95c8af2022-07-26 15:48:20 +08001326 * and the ciphersuite list length.
XiaokangQianb67384d2022-04-19 00:02:38 +00001327 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001328 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, legacy_session_id_len + 2);
XiaokangQian7807f9f2022-02-15 10:04:37 +00001329
Gilles Peskine449bd832023-01-11 14:50:10 +01001330 memcpy(&ssl->session_negotiate->id[0], p, legacy_session_id_len);
XiaokangQian4080a7f2022-04-11 09:55:18 +00001331 p += legacy_session_id_len;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001332
Gilles Peskine449bd832023-01-11 14:50:10 +01001333 cipher_suites_len = MBEDTLS_GET_UINT16_BE(p, 0);
XiaokangQian7807f9f2022-02-15 10:04:37 +00001334 p += 2;
1335
Ronald Cronfc7ae872023-02-16 15:32:19 +01001336 /*
1337 * The length of the ciphersuite list has to be even.
1338 */
1339 if (cipher_suites_len & 1) {
1340 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR,
1341 MBEDTLS_ERR_SSL_DECODE_ERROR);
1342 return MBEDTLS_ERR_SSL_DECODE_ERROR;
1343 }
1344
XiaokangQianb67384d2022-04-19 00:02:38 +00001345 /* Check we have enough data for the ciphersuite list, the legacy
1346 * compression methods and the length of the extensions.
Jerry Yue95c8af2022-07-26 15:48:20 +08001347 *
1348 * cipher_suites cipher_suites_len bytes
1349 * legacy_compression_methods 2 bytes
1350 * extensions_len 2 bytes
XiaokangQianb67384d2022-04-19 00:02:38 +00001351 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001352 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, cipher_suites_len + 2 + 2);
XiaokangQian7807f9f2022-02-15 10:04:37 +00001353
Gilles Peskine449bd832023-01-11 14:50:10 +01001354 /* ...
1355 * CipherSuite cipher_suites<2..2^16-2>;
1356 * ...
1357 * with CipherSuite defined as:
1358 * uint8 CipherSuite[2];
1359 */
Ronald Cron41a443a2022-10-04 16:38:25 +02001360#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED)
XiaokangQian060d8672022-04-21 09:24:56 +00001361 cipher_suites = p;
Jerry Yu5725f1c2022-08-21 17:27:16 +08001362#endif
XiaokangQian060d8672022-04-21 09:24:56 +00001363 cipher_suites_end = p + cipher_suites_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01001364 MBEDTLS_SSL_DEBUG_BUF(3, "client hello, ciphersuitelist",
1365 p, cipher_suites_len);
Jerry Yu5725f1c2022-08-21 17:27:16 +08001366
1367 /*
1368 * Search for a matching ciphersuite
1369 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001370 for (; p < cipher_suites_end; p += 2) {
XiaokangQiane8ff3502022-04-22 02:34:40 +00001371 uint16_t cipher_suite;
Gilles Peskine449bd832023-01-11 14:50:10 +01001372 const mbedtls_ssl_ciphersuite_t *ciphersuite_info;
Jerry Yu5725f1c2022-08-21 17:27:16 +08001373
Gilles Peskine449bd832023-01-11 14:50:10 +01001374 cipher_suite = MBEDTLS_GET_UINT16_BE(p, 0);
Jerry Yuc5a23a02022-08-25 10:51:44 +08001375 ciphersuite_info = ssl_tls13_validate_peer_ciphersuite(
Gilles Peskine449bd832023-01-11 14:50:10 +01001376 ssl, cipher_suite);
1377 if (ciphersuite_info == NULL) {
Jerry Yu5725f1c2022-08-21 17:27:16 +08001378 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +01001379 }
Jerry Yu5725f1c2022-08-21 17:27:16 +08001380
Jerry Yu5725f1c2022-08-21 17:27:16 +08001381 ssl->session_negotiate->ciphersuite = cipher_suite;
Jerry Yuc4bf5d62022-10-29 09:08:47 +08001382 handshake->ciphersuite_info = ciphersuite_info;
Gilles Peskine449bd832023-01-11 14:50:10 +01001383 MBEDTLS_SSL_DEBUG_MSG(2, ("selected ciphersuite: %04x - %s",
1384 cipher_suite,
1385 ciphersuite_info->name));
Ronald Cron25e9ec62023-02-16 15:35:16 +01001386 break;
XiaokangQian17f974c2022-04-19 09:57:41 +00001387 }
Jerry Yu5725f1c2022-08-21 17:27:16 +08001388
Gilles Peskine449bd832023-01-11 14:50:10 +01001389 if (handshake->ciphersuite_info == NULL) {
1390 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE,
1391 MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE);
1392 return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
Jerry Yu5725f1c2022-08-21 17:27:16 +08001393 }
Ronald Cron25e9ec62023-02-16 15:35:16 +01001394 p = cipher_suites_end;
Jerry Yuc1be19f2022-04-23 16:11:39 +08001395
XiaokangQian4080a7f2022-04-11 09:55:18 +00001396 /* ...
XiaokangQianb67384d2022-04-19 00:02:38 +00001397 * opaque legacy_compression_methods<1..2^8-1>;
XiaokangQian4080a7f2022-04-11 09:55:18 +00001398 * ...
XiaokangQian7807f9f2022-02-15 10:04:37 +00001399 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001400 if (p[0] != 1 || p[1] != MBEDTLS_SSL_COMPRESS_NULL) {
1401 MBEDTLS_SSL_DEBUG_MSG(1, ("bad legacy compression method"));
1402 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER,
1403 MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER);
1404 return MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001405 }
XiaokangQianb67384d2022-04-19 00:02:38 +00001406 p += 2;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001407
Jerry Yuc1be19f2022-04-23 16:11:39 +08001408 /* ...
XiaokangQianb67384d2022-04-19 00:02:38 +00001409 * Extension extensions<8..2^16-1>;
Jerry Yuc1be19f2022-04-23 16:11:39 +08001410 * ...
XiaokangQianb67384d2022-04-19 00:02:38 +00001411 * with Extension defined as:
1412 * struct {
1413 * ExtensionType extension_type;
1414 * opaque extension_data<0..2^16-1>;
1415 * } Extension;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001416 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001417 extensions_len = MBEDTLS_GET_UINT16_BE(p, 0);
XiaokangQian7807f9f2022-02-15 10:04:37 +00001418 p += 2;
Gilles Peskine449bd832023-01-11 14:50:10 +01001419 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, extensions_len);
XiaokangQiane8ff3502022-04-22 02:34:40 +00001420 extensions_end = p + extensions_len;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001421
Gilles Peskine449bd832023-01-11 14:50:10 +01001422 MBEDTLS_SSL_DEBUG_BUF(3, "client hello extensions", p, extensions_len);
XiaokangQian7807f9f2022-02-15 10:04:37 +00001423
Jerry Yu63a459c2022-10-31 13:38:40 +08001424 handshake->received_extensions = MBEDTLS_SSL_EXT_MASK_NONE;
Jerry Yu0c354a22022-08-29 15:25:36 +08001425
Gilles Peskine449bd832023-01-11 14:50:10 +01001426 while (p < extensions_end) {
XiaokangQian7807f9f2022-02-15 10:04:37 +00001427 unsigned int extension_type;
1428 size_t extension_data_len;
1429 const unsigned char *extension_data_end;
1430
Jerry Yu0c354a22022-08-29 15:25:36 +08001431 /* RFC 8446, section 4.2.11
Jerry Yu1c9247c2022-07-21 12:37:39 +08001432 *
1433 * The "pre_shared_key" extension MUST be the last extension in the
1434 * ClientHello (this facilitates implementation as described below).
1435 * Servers MUST check that it is the last extension and otherwise fail
1436 * the handshake with an "illegal_parameter" alert.
1437 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001438 if (handshake->received_extensions & MBEDTLS_SSL_EXT_MASK(PRE_SHARED_KEY)) {
Jerry Yu1c9247c2022-07-21 12:37:39 +08001439 MBEDTLS_SSL_DEBUG_MSG(
Gilles Peskine449bd832023-01-11 14:50:10 +01001440 3, ("pre_shared_key is not last extension."));
Jerry Yu1c9247c2022-07-21 12:37:39 +08001441 MBEDTLS_SSL_PEND_FATAL_ALERT(
1442 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER,
Gilles Peskine449bd832023-01-11 14:50:10 +01001443 MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER);
1444 return MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER;
Jerry Yu1c9247c2022-07-21 12:37:39 +08001445 }
1446
Gilles Peskine449bd832023-01-11 14:50:10 +01001447 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, extensions_end, 4);
1448 extension_type = MBEDTLS_GET_UINT16_BE(p, 0);
1449 extension_data_len = MBEDTLS_GET_UINT16_BE(p, 2);
XiaokangQian7807f9f2022-02-15 10:04:37 +00001450 p += 4;
1451
Gilles Peskine449bd832023-01-11 14:50:10 +01001452 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, extensions_end, extension_data_len);
XiaokangQian7807f9f2022-02-15 10:04:37 +00001453 extension_data_end = p + extension_data_len;
1454
Jerry Yuc4bf5d62022-10-29 09:08:47 +08001455 ret = mbedtls_ssl_tls13_check_received_extension(
Gilles Peskine449bd832023-01-11 14:50:10 +01001456 ssl, MBEDTLS_SSL_HS_CLIENT_HELLO, extension_type,
1457 MBEDTLS_SSL_TLS1_3_ALLOWED_EXTS_OF_CH);
1458 if (ret != 0) {
1459 return ret;
1460 }
Jerry Yue18dc7e2022-08-04 16:29:22 +08001461
Gilles Peskine449bd832023-01-11 14:50:10 +01001462 switch (extension_type) {
XiaokangQian40a35232022-05-07 09:02:40 +00001463#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
1464 case MBEDTLS_TLS_EXT_SERVERNAME:
Gilles Peskine449bd832023-01-11 14:50:10 +01001465 MBEDTLS_SSL_DEBUG_MSG(3, ("found ServerName extension"));
1466 ret = mbedtls_ssl_parse_server_name_ext(ssl, p,
1467 extension_data_end);
1468 if (ret != 0) {
XiaokangQian40a35232022-05-07 09:02:40 +00001469 MBEDTLS_SSL_DEBUG_RET(
Gilles Peskine449bd832023-01-11 14:50:10 +01001470 1, "mbedtls_ssl_parse_servername_ext", ret);
1471 return ret;
XiaokangQian40a35232022-05-07 09:02:40 +00001472 }
XiaokangQian40a35232022-05-07 09:02:40 +00001473 break;
1474#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
1475
XiaokangQianb67384d2022-04-19 00:02:38 +00001476#if defined(MBEDTLS_ECDH_C)
XiaokangQian7807f9f2022-02-15 10:04:37 +00001477 case MBEDTLS_TLS_EXT_SUPPORTED_GROUPS:
Gilles Peskine449bd832023-01-11 14:50:10 +01001478 MBEDTLS_SSL_DEBUG_MSG(3, ("found supported group extension"));
XiaokangQian7807f9f2022-02-15 10:04:37 +00001479
1480 /* Supported Groups Extension
1481 *
1482 * When sent by the client, the "supported_groups" extension
1483 * indicates the named groups which the client supports,
1484 * ordered from most preferred to least preferred.
1485 */
Jerry Yuc1be19f2022-04-23 16:11:39 +08001486 ret = ssl_tls13_parse_supported_groups_ext(
Gilles Peskine449bd832023-01-11 14:50:10 +01001487 ssl, p, extension_data_end);
1488 if (ret != 0) {
1489 MBEDTLS_SSL_DEBUG_RET(1,
1490 "mbedtls_ssl_parse_supported_groups_ext", ret);
1491 return ret;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001492 }
1493
XiaokangQian7807f9f2022-02-15 10:04:37 +00001494 break;
XiaokangQianb67384d2022-04-19 00:02:38 +00001495#endif /* MBEDTLS_ECDH_C */
XiaokangQian7807f9f2022-02-15 10:04:37 +00001496
XiaokangQian88408882022-04-02 10:15:03 +00001497#if defined(MBEDTLS_ECDH_C)
XiaokangQian7807f9f2022-02-15 10:04:37 +00001498 case MBEDTLS_TLS_EXT_KEY_SHARE:
Gilles Peskine449bd832023-01-11 14:50:10 +01001499 MBEDTLS_SSL_DEBUG_MSG(3, ("found key share extension"));
XiaokangQian7807f9f2022-02-15 10:04:37 +00001500
1501 /*
1502 * Key Share Extension
1503 *
1504 * When sent by the client, the "key_share" extension
1505 * contains the endpoint's cryptographic parameters for
1506 * ECDHE/DHE key establishment methods.
1507 */
Jerry Yuc1be19f2022-04-23 16:11:39 +08001508 ret = ssl_tls13_parse_key_shares_ext(
Gilles Peskine449bd832023-01-11 14:50:10 +01001509 ssl, p, extension_data_end);
1510 if (ret == SSL_TLS1_3_PARSE_KEY_SHARES_EXT_NO_MATCH) {
1511 MBEDTLS_SSL_DEBUG_MSG(2, ("HRR needed "));
Jerry Yu49ca9282022-05-05 11:05:22 +08001512 hrr_required = 1;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001513 }
1514
Gilles Peskine449bd832023-01-11 14:50:10 +01001515 if (ret < 0) {
Jerry Yu582dd062022-04-22 21:59:01 +08001516 MBEDTLS_SSL_DEBUG_RET(
Gilles Peskine449bd832023-01-11 14:50:10 +01001517 1, "ssl_tls13_parse_key_shares_ext", ret);
1518 return ret;
Jerry Yu582dd062022-04-22 21:59:01 +08001519 }
XiaokangQian7807f9f2022-02-15 10:04:37 +00001520
XiaokangQian7807f9f2022-02-15 10:04:37 +00001521 break;
XiaokangQian88408882022-04-02 10:15:03 +00001522#endif /* MBEDTLS_ECDH_C */
XiaokangQian7807f9f2022-02-15 10:04:37 +00001523
1524 case MBEDTLS_TLS_EXT_SUPPORTED_VERSIONS:
Gilles Peskine449bd832023-01-11 14:50:10 +01001525 MBEDTLS_SSL_DEBUG_MSG(3, ("found supported versions extension"));
XiaokangQian7807f9f2022-02-15 10:04:37 +00001526
1527 ret = ssl_tls13_parse_supported_versions_ext(
Gilles Peskine449bd832023-01-11 14:50:10 +01001528 ssl, p, extension_data_end);
1529 if (ret != 0) {
1530 MBEDTLS_SSL_DEBUG_RET(1,
1531 ("ssl_tls13_parse_supported_versions_ext"), ret);
1532 return ret;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001533 }
XiaokangQian7807f9f2022-02-15 10:04:37 +00001534 break;
1535
Ronald Cron41a443a2022-10-04 16:38:25 +02001536#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED)
Jerry Yue19e3b92022-07-08 12:04:51 +00001537 case MBEDTLS_TLS_EXT_PSK_KEY_EXCHANGE_MODES:
Gilles Peskine449bd832023-01-11 14:50:10 +01001538 MBEDTLS_SSL_DEBUG_MSG(3, ("found psk key exchange modes extension"));
Jerry Yue19e3b92022-07-08 12:04:51 +00001539
1540 ret = ssl_tls13_parse_key_exchange_modes_ext(
Gilles Peskine449bd832023-01-11 14:50:10 +01001541 ssl, p, extension_data_end);
1542 if (ret != 0) {
Jerry Yue19e3b92022-07-08 12:04:51 +00001543 MBEDTLS_SSL_DEBUG_RET(
Gilles Peskine449bd832023-01-11 14:50:10 +01001544 1, "ssl_tls13_parse_key_exchange_modes_ext", ret);
1545 return ret;
Jerry Yue19e3b92022-07-08 12:04:51 +00001546 }
1547
Jerry Yue19e3b92022-07-08 12:04:51 +00001548 break;
Ronald Cron41a443a2022-10-04 16:38:25 +02001549#endif
Jerry Yue19e3b92022-07-08 12:04:51 +00001550
Jerry Yu1c105562022-07-10 06:32:38 +00001551 case MBEDTLS_TLS_EXT_PRE_SHARED_KEY:
Gilles Peskine449bd832023-01-11 14:50:10 +01001552 MBEDTLS_SSL_DEBUG_MSG(3, ("found pre_shared_key extension"));
1553 if ((handshake->received_extensions &
1554 MBEDTLS_SSL_EXT_MASK(PSK_KEY_EXCHANGE_MODES)) == 0) {
Jerry Yu13ab81d2022-07-22 23:17:11 +08001555 MBEDTLS_SSL_PEND_FATAL_ALERT(
1556 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER,
Gilles Peskine449bd832023-01-11 14:50:10 +01001557 MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER);
1558 return MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER;
Jerry Yu13ab81d2022-07-22 23:17:11 +08001559 }
Ronald Cron41a443a2022-10-04 16:38:25 +02001560#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED)
Jerry Yu1c105562022-07-10 06:32:38 +00001561 /* Delay processing of the PSK identity once we have
1562 * found out which algorithms to use. We keep a pointer
1563 * to the buffer and the size for later processing.
1564 */
Jerry Yu29d9faa2022-08-23 17:52:45 +08001565 pre_shared_key_ext = p;
Jerry Yu1c105562022-07-10 06:32:38 +00001566 pre_shared_key_ext_end = extension_data_end;
Jerry Yue18dc7e2022-08-04 16:29:22 +08001567#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED */
Jerry Yu1c105562022-07-10 06:32:38 +00001568 break;
Jerry Yu1c105562022-07-10 06:32:38 +00001569
XiaokangQianacb39922022-06-17 10:18:48 +00001570#if defined(MBEDTLS_SSL_ALPN)
1571 case MBEDTLS_TLS_EXT_ALPN:
Gilles Peskine449bd832023-01-11 14:50:10 +01001572 MBEDTLS_SSL_DEBUG_MSG(3, ("found alpn extension"));
XiaokangQianacb39922022-06-17 10:18:48 +00001573
Gilles Peskine449bd832023-01-11 14:50:10 +01001574 ret = mbedtls_ssl_parse_alpn_ext(ssl, p, extension_data_end);
1575 if (ret != 0) {
XiaokangQianacb39922022-06-17 10:18:48 +00001576 MBEDTLS_SSL_DEBUG_RET(
Gilles Peskine449bd832023-01-11 14:50:10 +01001577 1, ("mbedtls_ssl_parse_alpn_ext"), ret);
1578 return ret;
XiaokangQianacb39922022-06-17 10:18:48 +00001579 }
XiaokangQianacb39922022-06-17 10:18:48 +00001580 break;
1581#endif /* MBEDTLS_SSL_ALPN */
1582
Ronald Cron928cbd32022-10-04 16:14:26 +02001583#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED)
XiaokangQian7807f9f2022-02-15 10:04:37 +00001584 case MBEDTLS_TLS_EXT_SIG_ALG:
Gilles Peskine449bd832023-01-11 14:50:10 +01001585 MBEDTLS_SSL_DEBUG_MSG(3, ("found signature_algorithms extension"));
XiaokangQian7807f9f2022-02-15 10:04:37 +00001586
Gabor Mezei078e8032022-04-27 21:17:56 +02001587 ret = mbedtls_ssl_parse_sig_alg_ext(
Gilles Peskine449bd832023-01-11 14:50:10 +01001588 ssl, p, extension_data_end);
1589 if (ret != 0) {
1590 MBEDTLS_SSL_DEBUG_MSG(1,
1591 (
1592 "ssl_parse_supported_signature_algorithms_server_ext ( %d )",
1593 ret));
1594 return ret;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001595 }
XiaokangQian7807f9f2022-02-15 10:04:37 +00001596 break;
Ronald Cron928cbd32022-10-04 16:14:26 +02001597#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED */
XiaokangQian7807f9f2022-02-15 10:04:37 +00001598
1599 default:
Jerry Yu79aa7212022-11-08 21:30:21 +08001600 MBEDTLS_SSL_PRINT_EXT(
Jerry Yu63a459c2022-10-31 13:38:40 +08001601 3, MBEDTLS_SSL_HS_CLIENT_HELLO,
Gilles Peskine449bd832023-01-11 14:50:10 +01001602 extension_type, "( ignored )");
Jerry Yu0c354a22022-08-29 15:25:36 +08001603 break;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001604 }
1605
1606 p += extension_data_len;
1607 }
1608
Gilles Peskine449bd832023-01-11 14:50:10 +01001609 MBEDTLS_SSL_PRINT_EXTS(3, MBEDTLS_SSL_HS_CLIENT_HELLO,
1610 handshake->received_extensions);
Jerry Yue18dc7e2022-08-04 16:29:22 +08001611
Gilles Peskine449bd832023-01-11 14:50:10 +01001612 mbedtls_ssl_add_hs_hdr_to_checksum(ssl,
1613 MBEDTLS_SSL_HS_CLIENT_HELLO,
1614 p - buf);
Jerry Yu032b15ce2022-07-11 06:10:03 +00001615
Ronald Cron41a443a2022-10-04 16:38:25 +02001616#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED)
XiaokangQian7807f9f2022-02-15 10:04:37 +00001617 /* Update checksum with either
1618 * - The entire content of the CH message, if no PSK extension is present
1619 * - The content up to but excluding the PSK extension, if present.
1620 */
Jerry Yu1c105562022-07-10 06:32:38 +00001621 /* If we've settled on a PSK-based exchange, parse PSK identity ext */
Gilles Peskine449bd832023-01-11 14:50:10 +01001622 if (mbedtls_ssl_tls13_some_psk_enabled(ssl) &&
1623 mbedtls_ssl_conf_tls13_some_psk_enabled(ssl) &&
1624 (handshake->received_extensions & MBEDTLS_SSL_EXT_MASK(PRE_SHARED_KEY))) {
1625 handshake->update_checksum(ssl, buf,
1626 pre_shared_key_ext - buf);
1627 ret = ssl_tls13_parse_pre_shared_key_ext(ssl,
1628 pre_shared_key_ext,
1629 pre_shared_key_ext_end,
1630 cipher_suites,
1631 cipher_suites_end);
1632 if (ret == MBEDTLS_ERR_SSL_UNKNOWN_IDENTITY) {
1633 handshake->received_extensions &= ~MBEDTLS_SSL_EXT_MASK(PRE_SHARED_KEY);
1634 } else if (ret != 0) {
Jerry Yu63a459c2022-10-31 13:38:40 +08001635 MBEDTLS_SSL_DEBUG_RET(
Gilles Peskine449bd832023-01-11 14:50:10 +01001636 1, "ssl_tls13_parse_pre_shared_key_ext", ret);
1637 return ret;
Jerry Yu1c105562022-07-10 06:32:38 +00001638 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001639 } else
Ronald Cron41a443a2022-10-04 16:38:25 +02001640#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED */
Jerry Yu1c105562022-07-10 06:32:38 +00001641 {
Gilles Peskine449bd832023-01-11 14:50:10 +01001642 handshake->update_checksum(ssl, buf, p - buf);
Jerry Yu1c105562022-07-10 06:32:38 +00001643 }
XiaokangQian7807f9f2022-02-15 10:04:37 +00001644
Gilles Peskine449bd832023-01-11 14:50:10 +01001645 ret = ssl_tls13_determine_key_exchange_mode(ssl);
1646 if (ret < 0) {
1647 return ret;
1648 }
XiaokangQian7807f9f2022-02-15 10:04:37 +00001649
Gilles Peskine449bd832023-01-11 14:50:10 +01001650 mbedtls_ssl_optimize_checksum(ssl, handshake->ciphersuite_info);
Jerry Yue5834fd2022-08-29 20:16:09 +08001651
Gilles Peskine449bd832023-01-11 14:50:10 +01001652 return hrr_required ? SSL_CLIENT_HELLO_HRR_REQUIRED : SSL_CLIENT_HELLO_OK;
XiaokangQian75fe8c72022-06-15 09:42:45 +00001653}
1654
1655/* Update the handshake state machine */
1656
Ronald Cronce7d76e2022-07-08 18:56:49 +02001657MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01001658static int ssl_tls13_postprocess_client_hello(mbedtls_ssl_context *ssl)
XiaokangQian75fe8c72022-06-15 09:42:45 +00001659{
1660 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1661
XiaokangQian7807f9f2022-02-15 10:04:37 +00001662 /*
XiaokangQianfb665a82022-06-15 03:57:21 +00001663 * Server certificate selection
1664 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001665 if (ssl->conf->f_cert_cb && (ret = ssl->conf->f_cert_cb(ssl)) != 0) {
1666 MBEDTLS_SSL_DEBUG_RET(1, "f_cert_cb", ret);
1667 return ret;
XiaokangQianfb665a82022-06-15 03:57:21 +00001668 }
1669#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
1670 ssl->handshake->sni_name = NULL;
1671 ssl->handshake->sni_name_len = 0;
1672#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
XiaokangQian7807f9f2022-02-15 10:04:37 +00001673
Gilles Peskine449bd832023-01-11 14:50:10 +01001674 ret = mbedtls_ssl_tls13_key_schedule_stage_early(ssl);
1675 if (ret != 0) {
1676 MBEDTLS_SSL_DEBUG_RET(1,
1677 "mbedtls_ssl_tls1_3_key_schedule_stage_early", ret);
1678 return ret;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001679 }
1680
Gilles Peskine449bd832023-01-11 14:50:10 +01001681 return 0;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001682
1683}
1684
1685/*
XiaokangQianed582dd2022-04-13 08:21:05 +00001686 * Main entry point from the state machine; orchestrates the otherfunctions.
1687 */
1688
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001689MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01001690static int ssl_tls13_process_client_hello(mbedtls_ssl_context *ssl)
XiaokangQianed582dd2022-04-13 08:21:05 +00001691{
1692
XiaokangQian08037552022-04-20 07:16:41 +00001693 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine449bd832023-01-11 14:50:10 +01001694 unsigned char *buf = NULL;
XiaokangQianed582dd2022-04-13 08:21:05 +00001695 size_t buflen = 0;
Jerry Yu4ca91402022-05-09 15:50:57 +08001696 int parse_client_hello_ret;
1697
Gilles Peskine449bd832023-01-11 14:50:10 +01001698 MBEDTLS_SSL_DEBUG_MSG(2, ("=> parse client hello"));
XiaokangQianed582dd2022-04-13 08:21:05 +00001699
Gilles Peskine449bd832023-01-11 14:50:10 +01001700 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_tls13_fetch_handshake_msg(
1701 ssl, MBEDTLS_SSL_HS_CLIENT_HELLO,
1702 &buf, &buflen));
XiaokangQianed582dd2022-04-13 08:21:05 +00001703
Gilles Peskine449bd832023-01-11 14:50:10 +01001704 MBEDTLS_SSL_PROC_CHK_NEG(ssl_tls13_parse_client_hello(ssl, buf,
1705 buf + buflen));
Jerry Yuf41553b2022-05-09 22:20:30 +08001706 parse_client_hello_ret = ret; /* Store return value of parse_client_hello,
1707 * only SSL_CLIENT_HELLO_OK or
1708 * SSL_CLIENT_HELLO_HRR_REQUIRED at this
1709 * stage as negative error codes are handled
1710 * by MBEDTLS_SSL_PROC_CHK_NEG. */
Jerry Yu4ca91402022-05-09 15:50:57 +08001711
Gilles Peskine449bd832023-01-11 14:50:10 +01001712 MBEDTLS_SSL_PROC_CHK(ssl_tls13_postprocess_client_hello(ssl));
Jerry Yu582dd062022-04-22 21:59:01 +08001713
Gilles Peskine449bd832023-01-11 14:50:10 +01001714 if (parse_client_hello_ret == SSL_CLIENT_HELLO_OK) {
1715 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_SERVER_HELLO);
1716 } else {
1717 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_HELLO_RETRY_REQUEST);
1718 }
XiaokangQianed582dd2022-04-13 08:21:05 +00001719
1720cleanup:
1721
Gilles Peskine449bd832023-01-11 14:50:10 +01001722 MBEDTLS_SSL_DEBUG_MSG(2, ("<= parse client hello"));
1723 return ret;
XiaokangQianed582dd2022-04-13 08:21:05 +00001724}
1725
1726/*
Jerry Yu1c3e6882022-04-20 21:23:40 +08001727 * Handler for MBEDTLS_SSL_SERVER_HELLO
Jerry Yu5b64ae92022-03-30 17:15:02 +08001728 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001729MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01001730static int ssl_tls13_prepare_server_hello(mbedtls_ssl_context *ssl)
Jerry Yuf4b27e42022-03-30 17:32:21 +08001731{
Jerry Yu637a3f12022-04-20 21:37:58 +08001732 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1733 unsigned char *server_randbytes =
Gilles Peskine449bd832023-01-11 14:50:10 +01001734 ssl->handshake->randbytes + MBEDTLS_CLIENT_HELLO_RANDOM_LEN;
1735 if (ssl->conf->f_rng == NULL) {
1736 MBEDTLS_SSL_DEBUG_MSG(1, ("no RNG provided"));
1737 return MBEDTLS_ERR_SSL_NO_RNG;
Jerry Yuf4b27e42022-03-30 17:32:21 +08001738 }
1739
Gilles Peskine449bd832023-01-11 14:50:10 +01001740 if ((ret = ssl->conf->f_rng(ssl->conf->p_rng, server_randbytes,
1741 MBEDTLS_SERVER_HELLO_RANDOM_LEN)) != 0) {
1742 MBEDTLS_SSL_DEBUG_RET(1, "f_rng", ret);
1743 return ret;
Jerry Yuf4b27e42022-03-30 17:32:21 +08001744 }
1745
Gilles Peskine449bd832023-01-11 14:50:10 +01001746 MBEDTLS_SSL_DEBUG_BUF(3, "server hello, random bytes", server_randbytes,
1747 MBEDTLS_SERVER_HELLO_RANDOM_LEN);
Jerry Yuf4b27e42022-03-30 17:32:21 +08001748
1749#if defined(MBEDTLS_HAVE_TIME)
Gilles Peskine449bd832023-01-11 14:50:10 +01001750 ssl->session_negotiate->start = time(NULL);
Jerry Yuf4b27e42022-03-30 17:32:21 +08001751#endif /* MBEDTLS_HAVE_TIME */
1752
Gilles Peskine449bd832023-01-11 14:50:10 +01001753 return ret;
Jerry Yuf4b27e42022-03-30 17:32:21 +08001754}
1755
Jerry Yu3bf2c642022-03-30 22:02:12 +08001756/*
Jerry Yue74e04a2022-04-21 09:23:16 +08001757 * ssl_tls13_write_server_hello_supported_versions_ext ():
Jerry Yu3bf2c642022-03-30 22:02:12 +08001758 *
1759 * struct {
Jerry Yufb9f54d2022-04-06 10:08:34 +08001760 * ProtocolVersion selected_version;
Jerry Yu3bf2c642022-03-30 22:02:12 +08001761 * } SupportedVersions;
1762 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001763MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yue74e04a2022-04-21 09:23:16 +08001764static int ssl_tls13_write_server_hello_supported_versions_ext(
Gilles Peskine449bd832023-01-11 14:50:10 +01001765 mbedtls_ssl_context *ssl,
1766 unsigned char *buf,
1767 unsigned char *end,
1768 size_t *out_len)
Jerry Yu3bf2c642022-03-30 22:02:12 +08001769{
Jerry Yu3bf2c642022-03-30 22:02:12 +08001770 *out_len = 0;
1771
Gilles Peskine449bd832023-01-11 14:50:10 +01001772 MBEDTLS_SSL_DEBUG_MSG(3, ("server hello, write selected version"));
Jerry Yu3bf2c642022-03-30 22:02:12 +08001773
1774 /* Check if we have space to write the extension:
1775 * - extension_type (2 bytes)
1776 * - extension_data_length (2 bytes)
Jerry Yu349a6132022-04-14 20:52:56 +08001777 * - selected_version (2 bytes)
Jerry Yu3bf2c642022-03-30 22:02:12 +08001778 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001779 MBEDTLS_SSL_CHK_BUF_PTR(buf, end, 6);
Jerry Yu3bf2c642022-03-30 22:02:12 +08001780
Gilles Peskine449bd832023-01-11 14:50:10 +01001781 MBEDTLS_PUT_UINT16_BE(MBEDTLS_TLS_EXT_SUPPORTED_VERSIONS, buf, 0);
Jerry Yu3bf2c642022-03-30 22:02:12 +08001782
Gilles Peskine449bd832023-01-11 14:50:10 +01001783 MBEDTLS_PUT_UINT16_BE(2, buf, 2);
Jerry Yu3bf2c642022-03-30 22:02:12 +08001784
Gilles Peskine449bd832023-01-11 14:50:10 +01001785 mbedtls_ssl_write_version(buf + 4,
1786 ssl->conf->transport,
1787 ssl->tls_version);
Jerry Yu3bf2c642022-03-30 22:02:12 +08001788
Gilles Peskine449bd832023-01-11 14:50:10 +01001789 MBEDTLS_SSL_DEBUG_MSG(3, ("supported version: [%04x]",
1790 ssl->tls_version));
Jerry Yu3bf2c642022-03-30 22:02:12 +08001791
Jerry Yu349a6132022-04-14 20:52:56 +08001792 *out_len = 6;
Jerry Yu3bf2c642022-03-30 22:02:12 +08001793
Jerry Yub95dd362022-11-08 21:19:34 +08001794 mbedtls_ssl_tls13_set_hs_sent_ext_mask(
Gilles Peskine449bd832023-01-11 14:50:10 +01001795 ssl, MBEDTLS_TLS_EXT_SUPPORTED_VERSIONS);
Jerry Yub95dd362022-11-08 21:19:34 +08001796
Gilles Peskine449bd832023-01-11 14:50:10 +01001797 return 0;
Jerry Yu3bf2c642022-03-30 22:02:12 +08001798}
1799
Jerry Yud9436a12022-04-20 22:28:09 +08001800
Jerry Yu3bf2c642022-03-30 22:02:12 +08001801
1802/* Generate and export a single key share. For hybrid KEMs, this can
1803 * be called multiple times with the different components of the hybrid. */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001804MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01001805static int ssl_tls13_generate_and_write_key_share(mbedtls_ssl_context *ssl,
1806 uint16_t named_group,
1807 unsigned char *buf,
1808 unsigned char *end,
1809 size_t *out_len)
Jerry Yu3bf2c642022-03-30 22:02:12 +08001810{
1811 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yu955ddd72022-04-22 22:27:33 +08001812
1813 *out_len = 0;
1814
Jerry Yu89e103c2022-03-30 22:43:29 +08001815#if defined(MBEDTLS_ECDH_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001816 if (mbedtls_ssl_tls13_named_group_is_ecdhe(named_group)) {
Jerry Yu89e103c2022-03-30 22:43:29 +08001817 ret = mbedtls_ssl_tls13_generate_and_write_ecdh_key_exchange(
Gilles Peskine449bd832023-01-11 14:50:10 +01001818 ssl, named_group, buf, end, out_len);
1819 if (ret != 0) {
Jerry Yu89e103c2022-03-30 22:43:29 +08001820 MBEDTLS_SSL_DEBUG_RET(
1821 1, "mbedtls_ssl_tls13_generate_and_write_ecdh_key_exchange",
Gilles Peskine449bd832023-01-11 14:50:10 +01001822 ret);
1823 return ret;
Jerry Yu3bf2c642022-03-30 22:02:12 +08001824 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001825 } else
Jerry Yu89e103c2022-03-30 22:43:29 +08001826#endif /* MBEDTLS_ECDH_C */
Gilles Peskine449bd832023-01-11 14:50:10 +01001827 if (0 /* Other kinds of KEMs */) {
1828 } else {
Jerry Yu955ddd72022-04-22 22:27:33 +08001829 ((void) ssl);
1830 ((void) named_group);
1831 ((void) buf);
1832 ((void) end);
Jerry Yu3bf2c642022-03-30 22:02:12 +08001833 ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
1834 }
1835
Gilles Peskine449bd832023-01-11 14:50:10 +01001836 return ret;
Jerry Yu3bf2c642022-03-30 22:02:12 +08001837}
1838
1839/*
1840 * ssl_tls13_write_key_share_ext
1841 *
1842 * Structure of key_share extension in ServerHello:
1843 *
Jerry Yu1c3e6882022-04-20 21:23:40 +08001844 * struct {
1845 * NamedGroup group;
1846 * opaque key_exchange<1..2^16-1>;
1847 * } KeyShareEntry;
1848 * struct {
1849 * KeyShareEntry server_share;
1850 * } KeyShareServerHello;
Jerry Yu3bf2c642022-03-30 22:02:12 +08001851 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001852MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01001853static int ssl_tls13_write_key_share_ext(mbedtls_ssl_context *ssl,
1854 unsigned char *buf,
1855 unsigned char *end,
1856 size_t *out_len)
Jerry Yu3bf2c642022-03-30 22:02:12 +08001857{
Jerry Yu955ddd72022-04-22 22:27:33 +08001858 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yu3bf2c642022-03-30 22:02:12 +08001859 unsigned char *p = buf;
Jerry Yu57d48412022-04-20 21:50:42 +08001860 uint16_t group = ssl->handshake->offered_group_id;
Jerry Yu3bf2c642022-03-30 22:02:12 +08001861 unsigned char *server_share = buf + 4;
Jerry Yu3bf2c642022-03-30 22:02:12 +08001862 size_t key_exchange_length;
Jerry Yu3bf2c642022-03-30 22:02:12 +08001863
1864 *out_len = 0;
1865
Gilles Peskine449bd832023-01-11 14:50:10 +01001866 MBEDTLS_SSL_DEBUG_MSG(3, ("server hello, adding key share extension"));
Jerry Yu3bf2c642022-03-30 22:02:12 +08001867
Gilles Peskine449bd832023-01-11 14:50:10 +01001868 MBEDTLS_SSL_DEBUG_MSG(2, ("server hello, write selected_group: %s (%04x)",
1869 mbedtls_ssl_named_group_to_str(group),
1870 group));
Jerry Yu58af2332022-09-06 11:19:31 +08001871
Jerry Yu3bf2c642022-03-30 22:02:12 +08001872 /* Check if we have space for header and length fields:
1873 * - extension_type (2 bytes)
1874 * - extension_data_length (2 bytes)
1875 * - group (2 bytes)
1876 * - key_exchange_length (2 bytes)
1877 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001878 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 8);
1879 MBEDTLS_PUT_UINT16_BE(MBEDTLS_TLS_EXT_KEY_SHARE, p, 0);
1880 MBEDTLS_PUT_UINT16_BE(group, server_share, 0);
Jerry Yu3bf2c642022-03-30 22:02:12 +08001881 p += 8;
Jerry Yu57d48412022-04-20 21:50:42 +08001882
Jerry Yu3bf2c642022-03-30 22:02:12 +08001883 /* When we introduce PQC-ECDHE hybrids, we'll want to call this
1884 * function multiple times. */
Jerry Yu955ddd72022-04-22 22:27:33 +08001885 ret = ssl_tls13_generate_and_write_key_share(
Gilles Peskine449bd832023-01-11 14:50:10 +01001886 ssl, group, server_share + 4, end, &key_exchange_length);
1887 if (ret != 0) {
1888 return ret;
1889 }
Jerry Yu3bf2c642022-03-30 22:02:12 +08001890 p += key_exchange_length;
Jerry Yuc1be19f2022-04-23 16:11:39 +08001891
Gilles Peskine449bd832023-01-11 14:50:10 +01001892 MBEDTLS_PUT_UINT16_BE(key_exchange_length, server_share + 2, 0);
Jerry Yu3bf2c642022-03-30 22:02:12 +08001893
Gilles Peskine449bd832023-01-11 14:50:10 +01001894 MBEDTLS_PUT_UINT16_BE(p - server_share, buf, 2);
Jerry Yu3bf2c642022-03-30 22:02:12 +08001895
Jerry Yu57d48412022-04-20 21:50:42 +08001896 *out_len = p - buf;
Jerry Yu955ddd72022-04-22 22:27:33 +08001897
Gilles Peskine449bd832023-01-11 14:50:10 +01001898 mbedtls_ssl_tls13_set_hs_sent_ext_mask(ssl, MBEDTLS_TLS_EXT_KEY_SHARE);
Jerry Yub95dd362022-11-08 21:19:34 +08001899
Gilles Peskine449bd832023-01-11 14:50:10 +01001900 return 0;
Jerry Yu3bf2c642022-03-30 22:02:12 +08001901}
Jerry Yud9436a12022-04-20 22:28:09 +08001902
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001903MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01001904static int ssl_tls13_write_hrr_key_share_ext(mbedtls_ssl_context *ssl,
1905 unsigned char *buf,
1906 unsigned char *end,
1907 size_t *out_len)
Jerry Yu23f7a6f2022-04-23 15:16:45 +08001908{
1909 uint16_t selected_group = ssl->handshake->hrr_selected_group;
1910 /* key_share Extension
1911 *
1912 * struct {
1913 * select (Handshake.msg_type) {
1914 * ...
1915 * case hello_retry_request:
1916 * NamedGroup selected_group;
1917 * ...
1918 * };
1919 * } KeyShare;
1920 */
1921
1922 *out_len = 0;
1923
Jerry Yub0ac10b2022-05-05 11:10:08 +08001924 /*
1925 * For a pure PSK key exchange, there is no group to agree upon. The purpose
1926 * of the HRR is then to transmit a cookie to force the client to demonstrate
1927 * reachability at their apparent network address (primarily useful for DTLS).
1928 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001929 if (!mbedtls_ssl_tls13_key_exchange_mode_with_ephemeral(ssl)) {
1930 return 0;
1931 }
Jerry Yu23f7a6f2022-04-23 15:16:45 +08001932
1933 /* We should only send the key_share extension if the client's initial
1934 * key share was not acceptable. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001935 if (ssl->handshake->offered_group_id != 0) {
1936 MBEDTLS_SSL_DEBUG_MSG(4, ("Skip key_share extension in HRR"));
1937 return 0;
Jerry Yu23f7a6f2022-04-23 15:16:45 +08001938 }
1939
Gilles Peskine449bd832023-01-11 14:50:10 +01001940 if (selected_group == 0) {
1941 MBEDTLS_SSL_DEBUG_MSG(1, ("no matching named group found"));
1942 return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
Jerry Yu23f7a6f2022-04-23 15:16:45 +08001943 }
1944
Jerry Yub0ac10b2022-05-05 11:10:08 +08001945 /* Check if we have enough space:
1946 * - extension_type (2 bytes)
1947 * - extension_data_length (2 bytes)
1948 * - selected_group (2 bytes)
1949 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001950 MBEDTLS_SSL_CHK_BUF_PTR(buf, end, 6);
Jerry Yu23f7a6f2022-04-23 15:16:45 +08001951
Gilles Peskine449bd832023-01-11 14:50:10 +01001952 MBEDTLS_PUT_UINT16_BE(MBEDTLS_TLS_EXT_KEY_SHARE, buf, 0);
1953 MBEDTLS_PUT_UINT16_BE(2, buf, 2);
1954 MBEDTLS_PUT_UINT16_BE(selected_group, buf, 4);
Jerry Yu23f7a6f2022-04-23 15:16:45 +08001955
Gilles Peskine449bd832023-01-11 14:50:10 +01001956 MBEDTLS_SSL_DEBUG_MSG(3,
1957 ("HRR selected_group: %s (%x)",
1958 mbedtls_ssl_named_group_to_str(selected_group),
1959 selected_group));
Jerry Yu23f7a6f2022-04-23 15:16:45 +08001960
1961 *out_len = 6;
Jerry Yu23f7a6f2022-04-23 15:16:45 +08001962
Gilles Peskine449bd832023-01-11 14:50:10 +01001963 mbedtls_ssl_tls13_set_hs_sent_ext_mask(ssl, MBEDTLS_TLS_EXT_KEY_SHARE);
Jerry Yub95dd362022-11-08 21:19:34 +08001964
Gilles Peskine449bd832023-01-11 14:50:10 +01001965 return 0;
Jerry Yu23f7a6f2022-04-23 15:16:45 +08001966}
Jerry Yu3bf2c642022-03-30 22:02:12 +08001967
1968/*
1969 * Structure of ServerHello message:
1970 *
1971 * struct {
1972 * ProtocolVersion legacy_version = 0x0303; // TLS v1.2
1973 * Random random;
1974 * opaque legacy_session_id_echo<0..32>;
1975 * CipherSuite cipher_suite;
1976 * uint8 legacy_compression_method = 0;
1977 * Extension extensions<6..2^16-1>;
1978 * } ServerHello;
1979 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001980MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01001981static int ssl_tls13_write_server_hello_body(mbedtls_ssl_context *ssl,
1982 unsigned char *buf,
1983 unsigned char *end,
1984 size_t *out_len,
1985 int is_hrr)
Jerry Yu56404d72022-03-30 17:36:13 +08001986{
Jerry Yu955ddd72022-04-22 22:27:33 +08001987 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yu3bf2c642022-03-30 22:02:12 +08001988 unsigned char *p = buf;
Jerry Yud9436a12022-04-20 22:28:09 +08001989 unsigned char *p_extensions_len;
Jerry Yufbe3e642022-04-25 19:31:51 +08001990 size_t output_len;
Jerry Yu3bf2c642022-03-30 22:02:12 +08001991
1992 *out_len = 0;
Jerry Yu50e00e32022-10-31 14:45:01 +08001993 ssl->handshake->sent_extensions = MBEDTLS_SSL_EXT_MASK_NONE;
Jerry Yu3bf2c642022-03-30 22:02:12 +08001994
Jerry Yucfc04b32022-04-21 09:31:58 +08001995 /* ...
1996 * ProtocolVersion legacy_version = 0x0303; // TLS 1.2
1997 * ...
1998 * with ProtocolVersion defined as:
1999 * uint16 ProtocolVersion;
Jerry Yu3bf2c642022-03-30 22:02:12 +08002000 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002001 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 2);
2002 MBEDTLS_PUT_UINT16_BE(0x0303, p, 0);
Jerry Yu3bf2c642022-03-30 22:02:12 +08002003 p += 2;
2004
Jerry Yu1c3e6882022-04-20 21:23:40 +08002005 /* ...
2006 * Random random;
2007 * ...
Jerry Yucfc04b32022-04-21 09:31:58 +08002008 * with Random defined as:
2009 * opaque Random[MBEDTLS_SERVER_HELLO_RANDOM_LEN];
Jerry Yu1c3e6882022-04-20 21:23:40 +08002010 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002011 MBEDTLS_SSL_CHK_BUF_PTR(p, end, MBEDTLS_SERVER_HELLO_RANDOM_LEN);
2012 if (is_hrr) {
2013 memcpy(p, mbedtls_ssl_tls13_hello_retry_request_magic,
2014 MBEDTLS_SERVER_HELLO_RANDOM_LEN);
2015 } else {
2016 memcpy(p, &ssl->handshake->randbytes[MBEDTLS_CLIENT_HELLO_RANDOM_LEN],
2017 MBEDTLS_SERVER_HELLO_RANDOM_LEN);
Jerry Yu23f7a6f2022-04-23 15:16:45 +08002018 }
Gilles Peskine449bd832023-01-11 14:50:10 +01002019 MBEDTLS_SSL_DEBUG_BUF(3, "server hello, random bytes",
2020 p, MBEDTLS_SERVER_HELLO_RANDOM_LEN);
Jerry Yu3bf2c642022-03-30 22:02:12 +08002021 p += MBEDTLS_SERVER_HELLO_RANDOM_LEN;
2022
Jerry Yucfc04b32022-04-21 09:31:58 +08002023 /* ...
2024 * opaque legacy_session_id_echo<0..32>;
2025 * ...
Jerry Yu3bf2c642022-03-30 22:02:12 +08002026 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002027 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 1 + ssl->session_negotiate->id_len);
2028 *p++ = (unsigned char) ssl->session_negotiate->id_len;
2029 if (ssl->session_negotiate->id_len > 0) {
2030 memcpy(p, &ssl->session_negotiate->id[0],
2031 ssl->session_negotiate->id_len);
Jerry Yu3bf2c642022-03-30 22:02:12 +08002032 p += ssl->session_negotiate->id_len;
Jerry Yu955ddd72022-04-22 22:27:33 +08002033
Gilles Peskine449bd832023-01-11 14:50:10 +01002034 MBEDTLS_SSL_DEBUG_BUF(3, "session id", ssl->session_negotiate->id,
2035 ssl->session_negotiate->id_len);
Jerry Yu3bf2c642022-03-30 22:02:12 +08002036 }
2037
Jerry Yucfc04b32022-04-21 09:31:58 +08002038 /* ...
2039 * CipherSuite cipher_suite;
2040 * ...
2041 * with CipherSuite defined as:
2042 * uint8 CipherSuite[2];
Jerry Yu3bf2c642022-03-30 22:02:12 +08002043 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002044 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 2);
2045 MBEDTLS_PUT_UINT16_BE(ssl->session_negotiate->ciphersuite, p, 0);
Jerry Yu3bf2c642022-03-30 22:02:12 +08002046 p += 2;
Gilles Peskine449bd832023-01-11 14:50:10 +01002047 MBEDTLS_SSL_DEBUG_MSG(3,
2048 ("server hello, chosen ciphersuite: %s ( id=%d )",
2049 mbedtls_ssl_get_ciphersuite_name(
2050 ssl->session_negotiate->ciphersuite),
2051 ssl->session_negotiate->ciphersuite));
Jerry Yu3bf2c642022-03-30 22:02:12 +08002052
Jerry Yucfc04b32022-04-21 09:31:58 +08002053 /* ...
2054 * uint8 legacy_compression_method = 0;
2055 * ...
2056 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002057 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 1);
Thomas Daubney31e03a82022-07-25 15:59:25 +01002058 *p++ = MBEDTLS_SSL_COMPRESS_NULL;
Jerry Yu3bf2c642022-03-30 22:02:12 +08002059
Jerry Yucfc04b32022-04-21 09:31:58 +08002060 /* ...
2061 * Extension extensions<6..2^16-1>;
2062 * ...
2063 * struct {
2064 * ExtensionType extension_type; (2 bytes)
2065 * opaque extension_data<0..2^16-1>;
2066 * } Extension;
2067 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002068 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 2);
Jerry Yud9436a12022-04-20 22:28:09 +08002069 p_extensions_len = p;
Jerry Yu3bf2c642022-03-30 22:02:12 +08002070 p += 2;
2071
Gilles Peskine449bd832023-01-11 14:50:10 +01002072 if ((ret = ssl_tls13_write_server_hello_supported_versions_ext(
2073 ssl, p, end, &output_len)) != 0) {
Jerry Yu955ddd72022-04-22 22:27:33 +08002074 MBEDTLS_SSL_DEBUG_RET(
Gilles Peskine449bd832023-01-11 14:50:10 +01002075 1, "ssl_tls13_write_server_hello_supported_versions_ext", ret);
2076 return ret;
Jerry Yu3bf2c642022-03-30 22:02:12 +08002077 }
2078 p += output_len;
2079
Gilles Peskine449bd832023-01-11 14:50:10 +01002080 if (mbedtls_ssl_tls13_key_exchange_mode_with_ephemeral(ssl)) {
2081 if (is_hrr) {
2082 ret = ssl_tls13_write_hrr_key_share_ext(ssl, p, end, &output_len);
2083 } else {
2084 ret = ssl_tls13_write_key_share_ext(ssl, p, end, &output_len);
2085 }
2086 if (ret != 0) {
2087 return ret;
2088 }
Jerry Yu3bf2c642022-03-30 22:02:12 +08002089 p += output_len;
2090 }
Jerry Yu3bf2c642022-03-30 22:02:12 +08002091
Ronald Cron41a443a2022-10-04 16:38:25 +02002092#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01002093 if (!is_hrr && mbedtls_ssl_tls13_key_exchange_mode_with_psk(ssl)) {
2094 ret = ssl_tls13_write_server_pre_shared_key_ext(ssl, p, end, &output_len);
2095 if (ret != 0) {
2096 MBEDTLS_SSL_DEBUG_RET(1, "ssl_tls13_write_server_pre_shared_key_ext",
2097 ret);
2098 return ret;
Jerry Yu032b15ce2022-07-11 06:10:03 +00002099 }
2100 p += output_len;
2101 }
Ronald Cron41a443a2022-10-04 16:38:25 +02002102#endif
Jerry Yu032b15ce2022-07-11 06:10:03 +00002103
Gilles Peskine449bd832023-01-11 14:50:10 +01002104 MBEDTLS_PUT_UINT16_BE(p - p_extensions_len - 2, p_extensions_len, 0);
Jerry Yu3bf2c642022-03-30 22:02:12 +08002105
Gilles Peskine449bd832023-01-11 14:50:10 +01002106 MBEDTLS_SSL_DEBUG_BUF(4, "server hello extensions",
2107 p_extensions_len, p - p_extensions_len);
Jerry Yu3bf2c642022-03-30 22:02:12 +08002108
Jerry Yud9436a12022-04-20 22:28:09 +08002109 *out_len = p - buf;
Jerry Yu3bf2c642022-03-30 22:02:12 +08002110
Gilles Peskine449bd832023-01-11 14:50:10 +01002111 MBEDTLS_SSL_DEBUG_BUF(3, "server hello", buf, *out_len);
Jerry Yu3bf2c642022-03-30 22:02:12 +08002112
Jerry Yu7de2ff02022-11-08 21:43:46 +08002113 MBEDTLS_SSL_PRINT_EXTS(
Jerry Yu4b8f2f72022-10-31 13:31:22 +08002114 3, is_hrr ? MBEDTLS_SSL_TLS1_3_HS_HELLO_RETRY_REQUEST :
Gilles Peskine449bd832023-01-11 14:50:10 +01002115 MBEDTLS_SSL_HS_SERVER_HELLO,
2116 ssl->handshake->sent_extensions);
Jerry Yu4b8f2f72022-10-31 13:31:22 +08002117
Gilles Peskine449bd832023-01-11 14:50:10 +01002118 return ret;
Jerry Yu56404d72022-03-30 17:36:13 +08002119}
2120
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002121MBEDTLS_CHECK_RETURN_CRITICAL
Xiaokang Qian934ce6f2023-02-06 10:23:04 +00002122static int ssl_tls13_finalize_server_hello(mbedtls_ssl_context *ssl)
Jerry Yue110d252022-05-05 10:19:22 +08002123{
2124 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine449bd832023-01-11 14:50:10 +01002125 ret = mbedtls_ssl_tls13_compute_handshake_transform(ssl);
2126 if (ret != 0) {
2127 MBEDTLS_SSL_DEBUG_RET(1,
2128 "mbedtls_ssl_tls13_compute_handshake_transform",
2129 ret);
2130 return ret;
Jerry Yue110d252022-05-05 10:19:22 +08002131 }
2132
Gilles Peskine449bd832023-01-11 14:50:10 +01002133 return ret;
Jerry Yue110d252022-05-05 10:19:22 +08002134}
2135
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002136MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002137static int ssl_tls13_write_server_hello(mbedtls_ssl_context *ssl)
Jerry Yu5b64ae92022-03-30 17:15:02 +08002138{
Jerry Yu637a3f12022-04-20 21:37:58 +08002139 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yuf4b27e42022-03-30 17:32:21 +08002140 unsigned char *buf;
2141 size_t buf_len, msg_len;
2142
Gilles Peskine449bd832023-01-11 14:50:10 +01002143 MBEDTLS_SSL_DEBUG_MSG(2, ("=> write server hello"));
Jerry Yuf4b27e42022-03-30 17:32:21 +08002144
Gilles Peskine449bd832023-01-11 14:50:10 +01002145 MBEDTLS_SSL_PROC_CHK(ssl_tls13_prepare_server_hello(ssl));
Jerry Yuf4b27e42022-03-30 17:32:21 +08002146
Gilles Peskine449bd832023-01-11 14:50:10 +01002147 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_start_handshake_msg(ssl,
2148 MBEDTLS_SSL_HS_SERVER_HELLO, &buf,
2149 &buf_len));
Jerry Yuf4b27e42022-03-30 17:32:21 +08002150
Gilles Peskine449bd832023-01-11 14:50:10 +01002151 MBEDTLS_SSL_PROC_CHK(ssl_tls13_write_server_hello_body(ssl, buf,
2152 buf + buf_len,
2153 &msg_len,
2154 0));
Jerry Yuf4b27e42022-03-30 17:32:21 +08002155
Jerry Yu3bf2c642022-03-30 22:02:12 +08002156 mbedtls_ssl_add_hs_msg_to_checksum(
Gilles Peskine449bd832023-01-11 14:50:10 +01002157 ssl, MBEDTLS_SSL_HS_SERVER_HELLO, buf, msg_len);
Jerry Yuf4b27e42022-03-30 17:32:21 +08002158
Gilles Peskine449bd832023-01-11 14:50:10 +01002159 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_finish_handshake_msg(
2160 ssl, buf_len, msg_len));
Jerry Yu637a3f12022-04-20 21:37:58 +08002161
Xiaokang Qian934ce6f2023-02-06 10:23:04 +00002162 MBEDTLS_SSL_PROC_CHK(ssl_tls13_finalize_server_hello(ssl));
Jerry Yue110d252022-05-05 10:19:22 +08002163
Gabor Mezei7b39bf12022-05-24 16:04:14 +02002164#if defined(MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE)
2165 /* The server sends a dummy change_cipher_spec record immediately
2166 * after its first handshake message. This may either be after
2167 * a ServerHello or a HelloRetryRequest.
2168 */
Gabor Mezei96ae9262022-06-28 11:45:18 +02002169 mbedtls_ssl_handshake_set_state(
Gilles Peskine449bd832023-01-11 14:50:10 +01002170 ssl, MBEDTLS_SSL_SERVER_CCS_AFTER_SERVER_HELLO);
Gabor Mezei7b39bf12022-05-24 16:04:14 +02002171#else
Gilles Peskine449bd832023-01-11 14:50:10 +01002172 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_ENCRYPTED_EXTENSIONS);
Gabor Mezei7b39bf12022-05-24 16:04:14 +02002173#endif /* MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE */
Jerry Yue110d252022-05-05 10:19:22 +08002174
Jerry Yuf4b27e42022-03-30 17:32:21 +08002175cleanup:
2176
Gilles Peskine449bd832023-01-11 14:50:10 +01002177 MBEDTLS_SSL_DEBUG_MSG(2, ("<= write server hello"));
2178 return ret;
Jerry Yu5b64ae92022-03-30 17:15:02 +08002179}
2180
Jerry Yu23d1a252022-05-12 18:08:59 +08002181
2182/*
2183 * Handler for MBEDTLS_SSL_HELLO_RETRY_REQUEST
2184 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002185MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002186static int ssl_tls13_prepare_hello_retry_request(mbedtls_ssl_context *ssl)
Jerry Yu23d1a252022-05-12 18:08:59 +08002187{
2188 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine449bd832023-01-11 14:50:10 +01002189 if (ssl->handshake->hello_retry_request_count > 0) {
2190 MBEDTLS_SSL_DEBUG_MSG(1, ("Too many HRRs"));
2191 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE,
2192 MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE);
2193 return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
Jerry Yu23d1a252022-05-12 18:08:59 +08002194 }
2195
2196 /*
2197 * Create stateless transcript hash for HRR
2198 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002199 MBEDTLS_SSL_DEBUG_MSG(4, ("Reset transcript for HRR"));
2200 ret = mbedtls_ssl_reset_transcript_for_hrr(ssl);
2201 if (ret != 0) {
2202 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_reset_transcript_for_hrr", ret);
2203 return ret;
Jerry Yu23d1a252022-05-12 18:08:59 +08002204 }
Gilles Peskine449bd832023-01-11 14:50:10 +01002205 mbedtls_ssl_session_reset_msg_layer(ssl, 0);
Jerry Yu23d1a252022-05-12 18:08:59 +08002206
Gilles Peskine449bd832023-01-11 14:50:10 +01002207 return 0;
Jerry Yu23d1a252022-05-12 18:08:59 +08002208}
2209
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002210MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002211static int ssl_tls13_write_hello_retry_request(mbedtls_ssl_context *ssl)
Jerry Yu23d1a252022-05-12 18:08:59 +08002212{
2213 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
2214 unsigned char *buf;
2215 size_t buf_len, msg_len;
2216
Gilles Peskine449bd832023-01-11 14:50:10 +01002217 MBEDTLS_SSL_DEBUG_MSG(2, ("=> write hello retry request"));
Jerry Yu23d1a252022-05-12 18:08:59 +08002218
Gilles Peskine449bd832023-01-11 14:50:10 +01002219 MBEDTLS_SSL_PROC_CHK(ssl_tls13_prepare_hello_retry_request(ssl));
Jerry Yu23d1a252022-05-12 18:08:59 +08002220
Gilles Peskine449bd832023-01-11 14:50:10 +01002221 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_start_handshake_msg(
2222 ssl, MBEDTLS_SSL_HS_SERVER_HELLO,
2223 &buf, &buf_len));
Jerry Yu23d1a252022-05-12 18:08:59 +08002224
Gilles Peskine449bd832023-01-11 14:50:10 +01002225 MBEDTLS_SSL_PROC_CHK(ssl_tls13_write_server_hello_body(ssl, buf,
2226 buf + buf_len,
2227 &msg_len,
2228 1));
Jerry Yu23d1a252022-05-12 18:08:59 +08002229 mbedtls_ssl_add_hs_msg_to_checksum(
Gilles Peskine449bd832023-01-11 14:50:10 +01002230 ssl, MBEDTLS_SSL_HS_SERVER_HELLO, buf, msg_len);
Jerry Yu23d1a252022-05-12 18:08:59 +08002231
2232
Gilles Peskine449bd832023-01-11 14:50:10 +01002233 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_finish_handshake_msg(ssl, buf_len,
2234 msg_len));
Jerry Yu23d1a252022-05-12 18:08:59 +08002235
2236 ssl->handshake->hello_retry_request_count++;
2237
Gabor Mezei7b39bf12022-05-24 16:04:14 +02002238#if defined(MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE)
2239 /* The server sends a dummy change_cipher_spec record immediately
2240 * after its first handshake message. This may either be after
2241 * a ServerHello or a HelloRetryRequest.
2242 */
Gabor Mezei96ae9262022-06-28 11:45:18 +02002243 mbedtls_ssl_handshake_set_state(
Gilles Peskine449bd832023-01-11 14:50:10 +01002244 ssl, MBEDTLS_SSL_SERVER_CCS_AFTER_HELLO_RETRY_REQUEST);
Gabor Mezei7b39bf12022-05-24 16:04:14 +02002245#else
Gilles Peskine449bd832023-01-11 14:50:10 +01002246 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_CLIENT_HELLO);
Gabor Mezei7b39bf12022-05-24 16:04:14 +02002247#endif /* MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE */
Jerry Yu23d1a252022-05-12 18:08:59 +08002248
2249cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +01002250 MBEDTLS_SSL_DEBUG_MSG(2, ("<= write hello retry request"));
2251 return ret;
Jerry Yu23d1a252022-05-12 18:08:59 +08002252}
2253
Jerry Yu5b64ae92022-03-30 17:15:02 +08002254/*
Jerry Yu4d3841a2022-04-16 12:37:19 +08002255 * Handler for MBEDTLS_SSL_ENCRYPTED_EXTENSIONS
2256 */
Jerry Yu4d3841a2022-04-16 12:37:19 +08002257
2258/*
2259 * struct {
2260 * Extension extensions<0..2 ^ 16 - 1>;
2261 * } EncryptedExtensions;
2262 *
2263 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002264MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002265static int ssl_tls13_write_encrypted_extensions_body(mbedtls_ssl_context *ssl,
2266 unsigned char *buf,
2267 unsigned char *end,
2268 size_t *out_len)
Jerry Yu4d3841a2022-04-16 12:37:19 +08002269{
XiaokangQianacb39922022-06-17 10:18:48 +00002270 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yu4d3841a2022-04-16 12:37:19 +08002271 unsigned char *p = buf;
2272 size_t extensions_len = 0;
Jerry Yu8937eb42022-05-03 12:12:14 +08002273 unsigned char *p_extensions_len;
XiaokangQianacb39922022-06-17 10:18:48 +00002274 size_t output_len;
Jerry Yu9da5e5a2022-05-03 15:46:09 +08002275
Jerry Yu4d3841a2022-04-16 12:37:19 +08002276 *out_len = 0;
2277
Gilles Peskine449bd832023-01-11 14:50:10 +01002278 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 2);
Jerry Yu8937eb42022-05-03 12:12:14 +08002279 p_extensions_len = p;
Jerry Yu4d3841a2022-04-16 12:37:19 +08002280 p += 2;
2281
Jerry Yu8937eb42022-05-03 12:12:14 +08002282 ((void) ssl);
XiaokangQianacb39922022-06-17 10:18:48 +00002283 ((void) ret);
2284 ((void) output_len);
2285
2286#if defined(MBEDTLS_SSL_ALPN)
Gilles Peskine449bd832023-01-11 14:50:10 +01002287 ret = mbedtls_ssl_write_alpn_ext(ssl, p, end, &output_len);
2288 if (ret != 0) {
2289 return ret;
2290 }
XiaokangQian95d5f542022-06-24 02:29:26 +00002291 p += output_len;
XiaokangQianacb39922022-06-17 10:18:48 +00002292#endif /* MBEDTLS_SSL_ALPN */
Jerry Yu4d3841a2022-04-16 12:37:19 +08002293
Gilles Peskine449bd832023-01-11 14:50:10 +01002294 extensions_len = (p - p_extensions_len) - 2;
2295 MBEDTLS_PUT_UINT16_BE(extensions_len, p_extensions_len, 0);
Jerry Yu8937eb42022-05-03 12:12:14 +08002296
2297 *out_len = p - buf;
Jerry Yu4d3841a2022-04-16 12:37:19 +08002298
Gilles Peskine449bd832023-01-11 14:50:10 +01002299 MBEDTLS_SSL_DEBUG_BUF(4, "encrypted extensions", buf, *out_len);
Jerry Yu4d3841a2022-04-16 12:37:19 +08002300
Jerry Yu7de2ff02022-11-08 21:43:46 +08002301 MBEDTLS_SSL_PRINT_EXTS(
Gilles Peskine449bd832023-01-11 14:50:10 +01002302 3, MBEDTLS_SSL_HS_ENCRYPTED_EXTENSIONS, ssl->handshake->sent_extensions);
Jerry Yu4b8f2f72022-10-31 13:31:22 +08002303
Gilles Peskine449bd832023-01-11 14:50:10 +01002304 return 0;
Jerry Yu4d3841a2022-04-16 12:37:19 +08002305}
2306
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002307MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002308static int ssl_tls13_write_encrypted_extensions(mbedtls_ssl_context *ssl)
Jerry Yu4d3841a2022-04-16 12:37:19 +08002309{
2310 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
2311 unsigned char *buf;
Jerry Yu39730a72022-05-03 12:14:04 +08002312 size_t buf_len, msg_len;
Jerry Yu4d3841a2022-04-16 12:37:19 +08002313
Gilles Peskine449bd832023-01-11 14:50:10 +01002314 mbedtls_ssl_set_outbound_transform(ssl,
2315 ssl->handshake->transform_handshake);
Gabor Mezei54719122022-06-28 11:34:56 +02002316 MBEDTLS_SSL_DEBUG_MSG(
Gilles Peskine449bd832023-01-11 14:50:10 +01002317 3, ("switching to handshake transform for outbound data"));
Gabor Mezei54719122022-06-28 11:34:56 +02002318
Gilles Peskine449bd832023-01-11 14:50:10 +01002319 MBEDTLS_SSL_DEBUG_MSG(2, ("=> write encrypted extensions"));
Jerry Yu4d3841a2022-04-16 12:37:19 +08002320
Gilles Peskine449bd832023-01-11 14:50:10 +01002321 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_start_handshake_msg(ssl,
2322 MBEDTLS_SSL_HS_ENCRYPTED_EXTENSIONS, &buf,
2323 &buf_len));
Jerry Yu4d3841a2022-04-16 12:37:19 +08002324
Gilles Peskine449bd832023-01-11 14:50:10 +01002325 MBEDTLS_SSL_PROC_CHK(ssl_tls13_write_encrypted_extensions_body(
2326 ssl, buf, buf + buf_len, &msg_len));
Jerry Yu4d3841a2022-04-16 12:37:19 +08002327
2328 mbedtls_ssl_add_hs_msg_to_checksum(
Gilles Peskine449bd832023-01-11 14:50:10 +01002329 ssl, MBEDTLS_SSL_HS_ENCRYPTED_EXTENSIONS, buf, msg_len);
Jerry Yu4d3841a2022-04-16 12:37:19 +08002330
Gilles Peskine449bd832023-01-11 14:50:10 +01002331 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_finish_handshake_msg(
2332 ssl, buf_len, msg_len));
Jerry Yu4d3841a2022-04-16 12:37:19 +08002333
Ronald Cron928cbd32022-10-04 16:14:26 +02002334#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01002335 if (mbedtls_ssl_tls13_key_exchange_mode_with_psk(ssl)) {
2336 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_SERVER_FINISHED);
2337 } else {
2338 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_CERTIFICATE_REQUEST);
2339 }
Jerry Yu8937eb42022-05-03 12:12:14 +08002340#else
Gilles Peskine449bd832023-01-11 14:50:10 +01002341 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_SERVER_FINISHED);
Jerry Yu8937eb42022-05-03 12:12:14 +08002342#endif
2343
Jerry Yu4d3841a2022-04-16 12:37:19 +08002344cleanup:
2345
Gilles Peskine449bd832023-01-11 14:50:10 +01002346 MBEDTLS_SSL_DEBUG_MSG(2, ("<= write encrypted extensions"));
2347 return ret;
Jerry Yu4d3841a2022-04-16 12:37:19 +08002348}
2349
Ronald Cron928cbd32022-10-04 16:14:26 +02002350#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED)
XiaokangQiana987e1d2022-05-07 01:25:58 +00002351#define SSL_CERTIFICATE_REQUEST_SEND_REQUEST 0
2352#define SSL_CERTIFICATE_REQUEST_SKIP 1
2353/* Coordination:
2354 * Check whether a CertificateRequest message should be written.
2355 * Returns a negative code on failure, or
2356 * - SSL_CERTIFICATE_REQUEST_SEND_REQUEST
2357 * - SSL_CERTIFICATE_REQUEST_SKIP
2358 * indicating if the writing of the CertificateRequest
2359 * should be skipped or not.
2360 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002361MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002362static int ssl_tls13_certificate_request_coordinate(mbedtls_ssl_context *ssl)
XiaokangQiana987e1d2022-05-07 01:25:58 +00002363{
2364 int authmode;
2365
XiaokangQian40a35232022-05-07 09:02:40 +00002366#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Gilles Peskine449bd832023-01-11 14:50:10 +01002367 if (ssl->handshake->sni_authmode != MBEDTLS_SSL_VERIFY_UNSET) {
XiaokangQian40a35232022-05-07 09:02:40 +00002368 authmode = ssl->handshake->sni_authmode;
Gilles Peskine449bd832023-01-11 14:50:10 +01002369 } else
XiaokangQian40a35232022-05-07 09:02:40 +00002370#endif
XiaokangQiana987e1d2022-05-07 01:25:58 +00002371 authmode = ssl->conf->authmode;
2372
Gilles Peskine449bd832023-01-11 14:50:10 +01002373 if (authmode == MBEDTLS_SSL_VERIFY_NONE) {
Ronald Croneac00ad2022-09-13 10:16:31 +02002374 ssl->session_negotiate->verify_result = MBEDTLS_X509_BADCERT_SKIP_VERIFY;
Gilles Peskine449bd832023-01-11 14:50:10 +01002375 return SSL_CERTIFICATE_REQUEST_SKIP;
Ronald Croneac00ad2022-09-13 10:16:31 +02002376 }
XiaokangQiana987e1d2022-05-07 01:25:58 +00002377
XiaokangQianc3017f62022-05-13 05:55:41 +00002378 ssl->handshake->certificate_request_sent = 1;
XiaokangQian189ded22022-05-10 08:12:17 +00002379
Gilles Peskine449bd832023-01-11 14:50:10 +01002380 return SSL_CERTIFICATE_REQUEST_SEND_REQUEST;
XiaokangQiana987e1d2022-05-07 01:25:58 +00002381}
2382
XiaokangQiancec9ae62022-05-06 07:28:50 +00002383/*
2384 * struct {
2385 * opaque certificate_request_context<0..2^8-1>;
2386 * Extension extensions<2..2^16-1>;
2387 * } CertificateRequest;
2388 *
2389 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002390MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002391static int ssl_tls13_write_certificate_request_body(mbedtls_ssl_context *ssl,
2392 unsigned char *buf,
2393 const unsigned char *end,
2394 size_t *out_len)
XiaokangQiancec9ae62022-05-06 07:28:50 +00002395{
2396 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
2397 unsigned char *p = buf;
XiaokangQianec6efb92022-05-06 09:53:10 +00002398 size_t output_len = 0;
XiaokangQiancec9ae62022-05-06 07:28:50 +00002399 unsigned char *p_extensions_len;
2400
2401 *out_len = 0;
2402
2403 /* Check if we have enough space:
2404 * - certificate_request_context (1 byte)
2405 * - extensions length (2 bytes)
2406 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002407 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 3);
XiaokangQiancec9ae62022-05-06 07:28:50 +00002408
2409 /*
2410 * Write certificate_request_context
2411 */
2412 /*
2413 * We use a zero length context for the normal handshake
2414 * messages. For post-authentication handshake messages
2415 * this request context would be set to a non-zero value.
2416 */
2417 *p++ = 0x0;
2418
2419 /*
2420 * Write extensions
2421 */
2422 /* The extensions must contain the signature_algorithms. */
2423 p_extensions_len = p;
2424 p += 2;
Gilles Peskine449bd832023-01-11 14:50:10 +01002425 ret = mbedtls_ssl_write_sig_alg_ext(ssl, p, end, &output_len);
2426 if (ret != 0) {
2427 return ret;
2428 }
XiaokangQiancec9ae62022-05-06 07:28:50 +00002429
XiaokangQianec6efb92022-05-06 09:53:10 +00002430 p += output_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01002431 MBEDTLS_PUT_UINT16_BE(p - p_extensions_len - 2, p_extensions_len, 0);
XiaokangQiancec9ae62022-05-06 07:28:50 +00002432
2433 *out_len = p - buf;
2434
Jerry Yu7de2ff02022-11-08 21:43:46 +08002435 MBEDTLS_SSL_PRINT_EXTS(
Gilles Peskine449bd832023-01-11 14:50:10 +01002436 3, MBEDTLS_SSL_HS_CERTIFICATE_REQUEST, ssl->handshake->sent_extensions);
Jerry Yu4b8f2f72022-10-31 13:31:22 +08002437
Gilles Peskine449bd832023-01-11 14:50:10 +01002438 return 0;
XiaokangQiancec9ae62022-05-06 07:28:50 +00002439}
2440
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002441MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002442static int ssl_tls13_write_certificate_request(mbedtls_ssl_context *ssl)
XiaokangQiancec9ae62022-05-06 07:28:50 +00002443{
2444 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
2445
Gilles Peskine449bd832023-01-11 14:50:10 +01002446 MBEDTLS_SSL_DEBUG_MSG(2, ("=> write certificate request"));
XiaokangQiancec9ae62022-05-06 07:28:50 +00002447
Gilles Peskine449bd832023-01-11 14:50:10 +01002448 MBEDTLS_SSL_PROC_CHK_NEG(ssl_tls13_certificate_request_coordinate(ssl));
XiaokangQiancec9ae62022-05-06 07:28:50 +00002449
Gilles Peskine449bd832023-01-11 14:50:10 +01002450 if (ret == SSL_CERTIFICATE_REQUEST_SEND_REQUEST) {
XiaokangQiancec9ae62022-05-06 07:28:50 +00002451 unsigned char *buf;
2452 size_t buf_len, msg_len;
2453
Gilles Peskine449bd832023-01-11 14:50:10 +01002454 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_start_handshake_msg(ssl,
2455 MBEDTLS_SSL_HS_CERTIFICATE_REQUEST,
2456 &buf, &buf_len));
XiaokangQiancec9ae62022-05-06 07:28:50 +00002457
Gilles Peskine449bd832023-01-11 14:50:10 +01002458 MBEDTLS_SSL_PROC_CHK(ssl_tls13_write_certificate_request_body(
2459 ssl, buf, buf + buf_len, &msg_len));
XiaokangQiancec9ae62022-05-06 07:28:50 +00002460
2461 mbedtls_ssl_add_hs_msg_to_checksum(
Gilles Peskine449bd832023-01-11 14:50:10 +01002462 ssl, MBEDTLS_SSL_HS_CERTIFICATE_REQUEST, buf, msg_len);
XiaokangQiancec9ae62022-05-06 07:28:50 +00002463
Gilles Peskine449bd832023-01-11 14:50:10 +01002464 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_finish_handshake_msg(
2465 ssl, buf_len, msg_len));
2466 } else if (ret == SSL_CERTIFICATE_REQUEST_SKIP) {
2467 MBEDTLS_SSL_DEBUG_MSG(2, ("<= skip write certificate request"));
XiaokangQiancec9ae62022-05-06 07:28:50 +00002468 ret = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01002469 } else {
2470 MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
XiaokangQiancec9ae62022-05-06 07:28:50 +00002471 ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
2472 goto cleanup;
2473 }
2474
Gilles Peskine449bd832023-01-11 14:50:10 +01002475 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_SERVER_CERTIFICATE);
XiaokangQiancec9ae62022-05-06 07:28:50 +00002476cleanup:
2477
Gilles Peskine449bd832023-01-11 14:50:10 +01002478 MBEDTLS_SSL_DEBUG_MSG(2, ("<= write certificate request"));
2479 return ret;
XiaokangQiancec9ae62022-05-06 07:28:50 +00002480}
XiaokangQiancec9ae62022-05-06 07:28:50 +00002481
Jerry Yu4d3841a2022-04-16 12:37:19 +08002482/*
Jerry Yuc6e6dbf2022-04-16 19:42:57 +08002483 * Handler for MBEDTLS_SSL_SERVER_CERTIFICATE
Jerry Yu83da34e2022-04-16 13:59:52 +08002484 */
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_server_certificate(mbedtls_ssl_context *ssl)
Jerry Yu83da34e2022-04-16 13:59:52 +08002487{
Jerry Yu5a26f302022-05-10 20:46:40 +08002488 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
XiaokangQianfb665a82022-06-15 03:57:21 +00002489
2490#if defined(MBEDTLS_X509_CRT_PARSE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01002491 if ((ssl_tls13_pick_key_cert(ssl) != 0) ||
2492 mbedtls_ssl_own_cert(ssl) == NULL) {
2493 MBEDTLS_SSL_DEBUG_MSG(2, ("No certificate available."));
2494 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE,
2495 MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE);
2496 return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
Jerry Yu5a26f302022-05-10 20:46:40 +08002497 }
XiaokangQianfb665a82022-06-15 03:57:21 +00002498#endif /* MBEDTLS_X509_CRT_PARSE_C */
Jerry Yu5a26f302022-05-10 20:46:40 +08002499
Gilles Peskine449bd832023-01-11 14:50:10 +01002500 ret = mbedtls_ssl_tls13_write_certificate(ssl);
2501 if (ret != 0) {
2502 return ret;
2503 }
2504 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_CERTIFICATE_VERIFY);
2505 return 0;
Jerry Yu83da34e2022-04-16 13:59:52 +08002506}
2507
2508/*
Jerry Yuc6e6dbf2022-04-16 19:42:57 +08002509 * Handler for MBEDTLS_SSL_CERTIFICATE_VERIFY
Jerry Yu83da34e2022-04-16 13:59:52 +08002510 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002511MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002512static int ssl_tls13_write_certificate_verify(mbedtls_ssl_context *ssl)
Jerry Yu83da34e2022-04-16 13:59:52 +08002513{
Gilles Peskine449bd832023-01-11 14:50:10 +01002514 int ret = mbedtls_ssl_tls13_write_certificate_verify(ssl);
2515 if (ret != 0) {
2516 return ret;
2517 }
2518 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_SERVER_FINISHED);
2519 return 0;
Jerry Yu83da34e2022-04-16 13:59:52 +08002520}
Ronald Cron928cbd32022-10-04 16:14:26 +02002521#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED */
Jerry Yu83da34e2022-04-16 13:59:52 +08002522
2523/*
Jerry Yud6e253d2022-05-18 13:59:24 +08002524 * Handler for MBEDTLS_SSL_SERVER_FINISHED
Jerry Yu69dd8d42022-04-16 12:51:26 +08002525 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002526MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002527static int ssl_tls13_write_server_finished(mbedtls_ssl_context *ssl)
Jerry Yu69dd8d42022-04-16 12:51:26 +08002528{
Jerry Yud6e253d2022-05-18 13:59:24 +08002529 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yu27bdc7c2022-04-16 13:33:27 +08002530
Gilles Peskine449bd832023-01-11 14:50:10 +01002531 ret = mbedtls_ssl_tls13_write_finished_message(ssl);
2532 if (ret != 0) {
2533 return ret;
2534 }
Jerry Yu27bdc7c2022-04-16 13:33:27 +08002535
Gilles Peskine449bd832023-01-11 14:50:10 +01002536 ret = mbedtls_ssl_tls13_compute_application_transform(ssl);
2537 if (ret != 0) {
Jerry Yue3d67cb2022-05-19 15:33:10 +08002538 MBEDTLS_SSL_PEND_FATAL_ALERT(
Gilles Peskine449bd832023-01-11 14:50:10 +01002539 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE,
2540 MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE);
2541 return ret;
Jerry Yue3d67cb2022-05-19 15:33:10 +08002542 }
XiaokangQianc3017f62022-05-13 05:55:41 +00002543
Gilles Peskine449bd832023-01-11 14:50:10 +01002544 MBEDTLS_SSL_DEBUG_MSG(1, ("Switch to handshake keys for inbound traffic"));
2545 mbedtls_ssl_set_inbound_transform(ssl, ssl->handshake->transform_handshake);
XiaokangQian189ded22022-05-10 08:12:17 +00002546
Gilles Peskine449bd832023-01-11 14:50:10 +01002547 if (ssl->handshake->certificate_request_sent) {
2548 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_CLIENT_CERTIFICATE);
2549 } else {
2550 MBEDTLS_SSL_DEBUG_MSG(2, ("skip parse certificate"));
2551 MBEDTLS_SSL_DEBUG_MSG(2, ("skip parse certificate verify"));
2552 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_CLIENT_FINISHED);
Ronald Cron19385882022-06-15 16:26:13 +02002553 }
Ronald Cron63dc4632022-05-31 14:41:53 +02002554
Gilles Peskine449bd832023-01-11 14:50:10 +01002555 return 0;
Jerry Yu69dd8d42022-04-16 12:51:26 +08002556}
2557
2558/*
Jerry Yud6e253d2022-05-18 13:59:24 +08002559 * Handler for MBEDTLS_SSL_CLIENT_FINISHED
Jerry Yu69dd8d42022-04-16 12:51:26 +08002560 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002561MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002562static int ssl_tls13_process_client_finished(mbedtls_ssl_context *ssl)
Jerry Yu69dd8d42022-04-16 12:51:26 +08002563{
Jerry Yud6e253d2022-05-18 13:59:24 +08002564 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
2565
Gilles Peskine449bd832023-01-11 14:50:10 +01002566 ret = mbedtls_ssl_tls13_process_finished_message(ssl);
2567 if (ret != 0) {
2568 return ret;
Jerry Yue3d67cb2022-05-19 15:33:10 +08002569 }
2570
Gilles Peskine449bd832023-01-11 14:50:10 +01002571 ret = mbedtls_ssl_tls13_compute_resumption_master_secret(ssl);
2572 if (ret != 0) {
2573 MBEDTLS_SSL_DEBUG_RET(1,
2574 "mbedtls_ssl_tls13_compute_resumption_master_secret", ret);
2575 }
2576
2577 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_HANDSHAKE_WRAPUP);
2578 return 0;
Jerry Yu69dd8d42022-04-16 12:51:26 +08002579}
2580
2581/*
Jerry Yud6e253d2022-05-18 13:59:24 +08002582 * Handler for MBEDTLS_SSL_HANDSHAKE_WRAPUP
Jerry Yu69dd8d42022-04-16 12:51:26 +08002583 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002584MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002585static int ssl_tls13_handshake_wrapup(mbedtls_ssl_context *ssl)
Jerry Yu69dd8d42022-04-16 12:51:26 +08002586{
Gilles Peskine449bd832023-01-11 14:50:10 +01002587 MBEDTLS_SSL_DEBUG_MSG(2, ("handshake: done"));
Jerry Yu03ed50b2022-04-16 17:13:30 +08002588
Gilles Peskine449bd832023-01-11 14:50:10 +01002589 mbedtls_ssl_tls13_handshake_wrapup(ssl);
Jerry Yue67bef42022-07-07 07:29:42 +00002590
Pengyu Lv3643fdb2023-01-12 16:46:28 +08002591#if defined(MBEDTLS_SSL_SESSION_TICKETS) && \
2592 defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED)
Pengyu Lva1aa31b2022-12-13 13:49:59 +08002593/* TODO: Remove the check of SOME_PSK_ENABLED since SESSION_TICKETS requires
2594 * SOME_PSK_ENABLED to be enabled. Here is just to make CI happy. It is
2595 * expected to be resolved with issue#6395.
2596 */
Pengyu Lvc7af2c42022-12-01 16:33:00 +08002597 /* Sent NewSessionTicket message only when client supports PSK */
Pengyu Lv3643fdb2023-01-12 16:46:28 +08002598 if (mbedtls_ssl_tls13_some_psk_enabled(ssl)) {
2599 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_TLS1_3_NEW_SESSION_TICKET);
Pengyu Lvc7af2c42022-12-01 16:33:00 +08002600 } else
Jerry Yufca4d572022-07-21 10:37:48 +08002601#endif
Pengyu Lv3643fdb2023-01-12 16:46:28 +08002602 {
2603 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_HANDSHAKE_OVER);
2604 }
Gilles Peskine449bd832023-01-11 14:50:10 +01002605 return 0;
Jerry Yu69dd8d42022-04-16 12:51:26 +08002606}
2607
2608/*
Jerry Yua8d3c502022-10-30 14:51:23 +08002609 * Handler for MBEDTLS_SSL_TLS1_3_NEW_SESSION_TICKET
Jerry Yue67bef42022-07-07 07:29:42 +00002610 */
2611#define SSL_NEW_SESSION_TICKET_SKIP 0
2612#define SSL_NEW_SESSION_TICKET_WRITE 1
2613MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002614static int ssl_tls13_write_new_session_ticket_coordinate(mbedtls_ssl_context *ssl)
Jerry Yue67bef42022-07-07 07:29:42 +00002615{
2616 /* Check whether the use of session tickets is enabled */
Gilles Peskine449bd832023-01-11 14:50:10 +01002617 if (ssl->conf->f_ticket_write == NULL) {
2618 MBEDTLS_SSL_DEBUG_MSG(2, ("NewSessionTicket: disabled,"
2619 " callback is not set"));
2620 return SSL_NEW_SESSION_TICKET_SKIP;
Jerry Yud0766ec2022-09-22 10:46:57 +08002621 }
Gilles Peskine449bd832023-01-11 14:50:10 +01002622 if (ssl->conf->new_session_tickets_count == 0) {
2623 MBEDTLS_SSL_DEBUG_MSG(2, ("NewSessionTicket: disabled,"
2624 " configured count is zero"));
2625 return SSL_NEW_SESSION_TICKET_SKIP;
Jerry Yud0766ec2022-09-22 10:46:57 +08002626 }
2627
Gilles Peskine449bd832023-01-11 14:50:10 +01002628 if (ssl->handshake->new_session_tickets_count == 0) {
2629 MBEDTLS_SSL_DEBUG_MSG(2, ("NewSessionTicket: all tickets have "
2630 "been sent."));
2631 return SSL_NEW_SESSION_TICKET_SKIP;
Jerry Yue67bef42022-07-07 07:29:42 +00002632 }
2633
Gilles Peskine449bd832023-01-11 14:50:10 +01002634 return SSL_NEW_SESSION_TICKET_WRITE;
Jerry Yue67bef42022-07-07 07:29:42 +00002635}
2636
2637#if defined(MBEDTLS_SSL_SESSION_TICKETS)
2638MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002639static int ssl_tls13_prepare_new_session_ticket(mbedtls_ssl_context *ssl,
2640 unsigned char *ticket_nonce,
2641 size_t ticket_nonce_size)
Jerry Yue67bef42022-07-07 07:29:42 +00002642{
2643 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
2644 mbedtls_ssl_session *session = ssl->session;
2645 mbedtls_ssl_ciphersuite_t *ciphersuite_info;
2646 psa_algorithm_t psa_hash_alg;
2647 int hash_length;
2648
Gilles Peskine449bd832023-01-11 14:50:10 +01002649 MBEDTLS_SSL_DEBUG_MSG(2, ("=> prepare NewSessionTicket msg"));
Jerry Yue67bef42022-07-07 07:29:42 +00002650
2651#if defined(MBEDTLS_HAVE_TIME)
Gilles Peskine449bd832023-01-11 14:50:10 +01002652 session->start = mbedtls_time(NULL);
Jerry Yue67bef42022-07-07 07:29:42 +00002653#endif
2654
Pengyu Lv9f926952022-11-17 15:22:33 +08002655 /* Set ticket_flags depends on the advertised psk key exchange mode */
Pengyu Lv80270b22023-01-12 11:54:04 +08002656 mbedtls_ssl_session_clear_ticket_flags(
Pengyu Lv9eacb442022-12-09 14:39:19 +08002657 session, MBEDTLS_SSL_TLS1_3_TICKET_FLAGS_MASK);
Pengyu Lve6487fe2022-12-06 09:30:29 +08002658#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED)
Pengyu Lv80270b22023-01-12 11:54:04 +08002659 mbedtls_ssl_session_set_ticket_flags(
Pengyu Lv9eacb442022-12-09 14:39:19 +08002660 session, ssl->handshake->tls13_kex_modes);
Pengyu Lve6487fe2022-12-06 09:30:29 +08002661#endif
Pengyu Lv4938a562023-01-16 11:28:49 +08002662 MBEDTLS_SSL_PRINT_TICKET_FLAGS(4, session->ticket_flags);
Pengyu Lv9f926952022-11-17 15:22:33 +08002663
Jerry Yue67bef42022-07-07 07:29:42 +00002664 /* Generate ticket_age_add */
Gilles Peskine449bd832023-01-11 14:50:10 +01002665 if ((ret = ssl->conf->f_rng(ssl->conf->p_rng,
2666 (unsigned char *) &session->ticket_age_add,
2667 sizeof(session->ticket_age_add)) != 0)) {
2668 MBEDTLS_SSL_DEBUG_RET(1, "generate_ticket_age_add", ret);
2669 return ret;
Jerry Yue67bef42022-07-07 07:29:42 +00002670 }
Gilles Peskine449bd832023-01-11 14:50:10 +01002671 MBEDTLS_SSL_DEBUG_MSG(3, ("ticket_age_add: %u",
2672 (unsigned int) session->ticket_age_add));
Jerry Yue67bef42022-07-07 07:29:42 +00002673
2674 /* Generate ticket_nonce */
Gilles Peskine449bd832023-01-11 14:50:10 +01002675 ret = ssl->conf->f_rng(ssl->conf->p_rng, ticket_nonce, ticket_nonce_size);
2676 if (ret != 0) {
2677 MBEDTLS_SSL_DEBUG_RET(1, "generate_ticket_nonce", ret);
2678 return ret;
Jerry Yue67bef42022-07-07 07:29:42 +00002679 }
Gilles Peskine449bd832023-01-11 14:50:10 +01002680 MBEDTLS_SSL_DEBUG_BUF(3, "ticket_nonce:",
2681 ticket_nonce, ticket_nonce_size);
Jerry Yue67bef42022-07-07 07:29:42 +00002682
2683 ciphersuite_info =
Gilles Peskine449bd832023-01-11 14:50:10 +01002684 (mbedtls_ssl_ciphersuite_t *) ssl->handshake->ciphersuite_info;
2685 psa_hash_alg = mbedtls_psa_translate_md(ciphersuite_info->mac);
2686 hash_length = PSA_HASH_LENGTH(psa_hash_alg);
2687 if (hash_length == -1 ||
2688 (size_t) hash_length > sizeof(session->resumption_key)) {
2689 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Jerry Yufca4d572022-07-21 10:37:48 +08002690 }
2691
Jerry Yue67bef42022-07-07 07:29:42 +00002692 /* In this code the psk key length equals the length of the hash */
2693 session->resumption_key_len = hash_length;
2694 session->ciphersuite = ciphersuite_info->id;
2695
2696 /* Compute resumption key
2697 *
2698 * HKDF-Expand-Label( resumption_master_secret,
2699 * "resumption", ticket_nonce, Hash.length )
2700 */
2701 ret = mbedtls_ssl_tls13_hkdf_expand_label(
Gilles Peskine449bd832023-01-11 14:50:10 +01002702 psa_hash_alg,
2703 session->app_secrets.resumption_master_secret,
2704 hash_length,
2705 MBEDTLS_SSL_TLS1_3_LBL_WITH_LEN(resumption),
2706 ticket_nonce,
2707 ticket_nonce_size,
2708 session->resumption_key,
2709 hash_length);
Jerry Yue67bef42022-07-07 07:29:42 +00002710
Gilles Peskine449bd832023-01-11 14:50:10 +01002711 if (ret != 0) {
2712 MBEDTLS_SSL_DEBUG_RET(2,
2713 "Creating the ticket-resumed PSK failed",
2714 ret);
2715 return ret;
Jerry Yue67bef42022-07-07 07:29:42 +00002716 }
Gilles Peskine449bd832023-01-11 14:50:10 +01002717 MBEDTLS_SSL_DEBUG_BUF(3, "Ticket-resumed PSK",
2718 session->resumption_key,
2719 session->resumption_key_len);
Jerry Yue67bef42022-07-07 07:29:42 +00002720
Gilles Peskine449bd832023-01-11 14:50:10 +01002721 MBEDTLS_SSL_DEBUG_BUF(3, "resumption_master_secret",
2722 session->app_secrets.resumption_master_secret,
2723 hash_length);
Jerry Yue67bef42022-07-07 07:29:42 +00002724
Gilles Peskine449bd832023-01-11 14:50:10 +01002725 return 0;
Jerry Yue67bef42022-07-07 07:29:42 +00002726}
2727
2728/* This function creates a NewSessionTicket message in the following format:
2729 *
2730 * struct {
2731 * uint32 ticket_lifetime;
2732 * uint32 ticket_age_add;
2733 * opaque ticket_nonce<0..255>;
2734 * opaque ticket<1..2^16-1>;
2735 * Extension extensions<0..2^16-2>;
2736 * } NewSessionTicket;
2737 *
2738 * The ticket inside the NewSessionTicket message is an encrypted container
2739 * carrying the necessary information so that the server is later able to
2740 * re-start the communication.
2741 *
2742 * The following fields are placed inside the ticket by the
2743 * f_ticket_write() function:
2744 *
2745 * - creation time (start)
2746 * - flags (flags)
2747 * - age add (ticket_age_add)
2748 * - key (key)
2749 * - key length (key_len)
2750 * - ciphersuite (ciphersuite)
2751 */
2752MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002753static int ssl_tls13_write_new_session_ticket_body(mbedtls_ssl_context *ssl,
2754 unsigned char *buf,
2755 unsigned char *end,
2756 size_t *out_len,
2757 unsigned char *ticket_nonce,
2758 size_t ticket_nonce_size)
Jerry Yue67bef42022-07-07 07:29:42 +00002759{
2760 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
2761 unsigned char *p = buf;
2762 mbedtls_ssl_session *session = ssl->session;
2763 size_t ticket_len;
2764 uint32_t ticket_lifetime;
2765
2766 *out_len = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01002767 MBEDTLS_SSL_DEBUG_MSG(2, ("=> write NewSessionTicket msg"));
Jerry Yue67bef42022-07-07 07:29:42 +00002768
2769 /*
2770 * ticket_lifetime 4 bytes
2771 * ticket_age_add 4 bytes
2772 * ticket_nonce 1 + ticket_nonce_size bytes
2773 * ticket >=2 bytes
2774 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002775 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 4 + 4 + 1 + ticket_nonce_size + 2);
Jerry Yue67bef42022-07-07 07:29:42 +00002776
2777 /* Generate ticket and ticket_lifetime */
Gilles Peskine449bd832023-01-11 14:50:10 +01002778 ret = ssl->conf->f_ticket_write(ssl->conf->p_ticket,
2779 session,
2780 p + 9 + ticket_nonce_size + 2,
2781 end,
2782 &ticket_len,
2783 &ticket_lifetime);
2784 if (ret != 0) {
2785 MBEDTLS_SSL_DEBUG_RET(1, "write_ticket", ret);
2786 return ret;
Jerry Yue67bef42022-07-07 07:29:42 +00002787 }
2788 /* RFC 8446 4.6.1
2789 * ticket_lifetime: Indicates the lifetime in seconds as a 32-bit
2790 * unsigned integer in network byte order from the time of ticket
2791 * issuance. Servers MUST NOT use any value greater than
2792 * 604800 seconds (7 days). The value of zero indicates that the
2793 * ticket should be discarded immediately. Clients MUST NOT cache
2794 * tickets for longer than 7 days, regardless of the ticket_lifetime,
2795 * and MAY delete tickets earlier based on local policy. A server
2796 * MAY treat a ticket as valid for a shorter period of time than what
2797 * is stated in the ticket_lifetime.
2798 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002799 if (ticket_lifetime > 604800) {
Xiaokang Qian28af5012022-10-13 08:18:19 +00002800 ticket_lifetime = 604800;
Gilles Peskine449bd832023-01-11 14:50:10 +01002801 }
2802 MBEDTLS_PUT_UINT32_BE(ticket_lifetime, p, 0);
2803 MBEDTLS_SSL_DEBUG_MSG(3, ("ticket_lifetime: %u",
2804 (unsigned int) ticket_lifetime));
Jerry Yue67bef42022-07-07 07:29:42 +00002805
2806 /* Write ticket_age_add */
Gilles Peskine449bd832023-01-11 14:50:10 +01002807 MBEDTLS_PUT_UINT32_BE(session->ticket_age_add, p, 4);
2808 MBEDTLS_SSL_DEBUG_MSG(3, ("ticket_age_add: %u",
2809 (unsigned int) session->ticket_age_add));
Jerry Yue67bef42022-07-07 07:29:42 +00002810
2811 /* Write ticket_nonce */
Gilles Peskine449bd832023-01-11 14:50:10 +01002812 p[8] = (unsigned char) ticket_nonce_size;
2813 if (ticket_nonce_size > 0) {
2814 memcpy(p + 9, ticket_nonce, ticket_nonce_size);
Jerry Yue67bef42022-07-07 07:29:42 +00002815 }
2816 p += 9 + ticket_nonce_size;
2817
2818 /* Write ticket */
Gilles Peskine449bd832023-01-11 14:50:10 +01002819 MBEDTLS_PUT_UINT16_BE(ticket_len, p, 0);
Jerry Yue67bef42022-07-07 07:29:42 +00002820 p += 2;
Gilles Peskine449bd832023-01-11 14:50:10 +01002821 MBEDTLS_SSL_DEBUG_BUF(4, "ticket", p, ticket_len);
Jerry Yue67bef42022-07-07 07:29:42 +00002822 p += ticket_len;
2823
2824 /* Ticket Extensions
2825 *
2826 * Note: We currently don't have any extensions.
2827 * Set length to zero.
2828 */
Jerry Yuedab6372022-10-31 14:37:31 +08002829 ssl->handshake->sent_extensions = MBEDTLS_SSL_EXT_MASK_NONE;
2830
Gilles Peskine449bd832023-01-11 14:50:10 +01002831 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 2);
2832 MBEDTLS_PUT_UINT16_BE(0, p, 0);
Jerry Yue67bef42022-07-07 07:29:42 +00002833 p += 2;
2834
2835 *out_len = p - buf;
Gilles Peskine449bd832023-01-11 14:50:10 +01002836 MBEDTLS_SSL_DEBUG_BUF(4, "ticket", buf, *out_len);
2837 MBEDTLS_SSL_DEBUG_MSG(2, ("<= write new session ticket"));
Jerry Yue67bef42022-07-07 07:29:42 +00002838
Jerry Yu7de2ff02022-11-08 21:43:46 +08002839 MBEDTLS_SSL_PRINT_EXTS(
Gilles Peskine449bd832023-01-11 14:50:10 +01002840 3, MBEDTLS_SSL_HS_NEW_SESSION_TICKET, ssl->handshake->sent_extensions);
Jerry Yu4b8f2f72022-10-31 13:31:22 +08002841
Gilles Peskine449bd832023-01-11 14:50:10 +01002842 return 0;
Jerry Yue67bef42022-07-07 07:29:42 +00002843}
2844
2845/*
Jerry Yua8d3c502022-10-30 14:51:23 +08002846 * Handler for MBEDTLS_SSL_TLS1_3_NEW_SESSION_TICKET
Jerry Yue67bef42022-07-07 07:29:42 +00002847 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002848static int ssl_tls13_write_new_session_ticket(mbedtls_ssl_context *ssl)
Jerry Yue67bef42022-07-07 07:29:42 +00002849{
2850 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
2851
Gilles Peskine449bd832023-01-11 14:50:10 +01002852 MBEDTLS_SSL_PROC_CHK_NEG(ssl_tls13_write_new_session_ticket_coordinate(ssl));
Jerry Yue67bef42022-07-07 07:29:42 +00002853
Gilles Peskine449bd832023-01-11 14:50:10 +01002854 if (ret == SSL_NEW_SESSION_TICKET_WRITE) {
Jerry Yue67bef42022-07-07 07:29:42 +00002855 unsigned char ticket_nonce[MBEDTLS_SSL_TLS1_3_TICKET_NONCE_LENGTH];
2856 unsigned char *buf;
2857 size_t buf_len, msg_len;
2858
Gilles Peskine449bd832023-01-11 14:50:10 +01002859 MBEDTLS_SSL_PROC_CHK(ssl_tls13_prepare_new_session_ticket(
2860 ssl, ticket_nonce, sizeof(ticket_nonce)));
Jerry Yue67bef42022-07-07 07:29:42 +00002861
Gilles Peskine449bd832023-01-11 14:50:10 +01002862 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_start_handshake_msg(ssl,
2863 MBEDTLS_SSL_HS_NEW_SESSION_TICKET,
2864 &buf, &buf_len));
Jerry Yue67bef42022-07-07 07:29:42 +00002865
Gilles Peskine449bd832023-01-11 14:50:10 +01002866 MBEDTLS_SSL_PROC_CHK(ssl_tls13_write_new_session_ticket_body(
2867 ssl, buf, buf + buf_len, &msg_len,
2868 ticket_nonce, sizeof(ticket_nonce)));
Jerry Yue67bef42022-07-07 07:29:42 +00002869
Gilles Peskine449bd832023-01-11 14:50:10 +01002870 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_finish_handshake_msg(
2871 ssl, buf_len, msg_len));
Jerry Yufca4d572022-07-21 10:37:48 +08002872
Jerry Yu359e65f2022-09-22 23:47:43 +08002873 /* Limit session tickets count to one when resumption connection.
2874 *
2875 * See document of mbedtls_ssl_conf_new_session_tickets.
2876 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002877 if (ssl->handshake->resume == 1) {
Jerry Yu359e65f2022-09-22 23:47:43 +08002878 ssl->handshake->new_session_tickets_count = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01002879 } else {
Jerry Yu359e65f2022-09-22 23:47:43 +08002880 ssl->handshake->new_session_tickets_count--;
Gilles Peskine449bd832023-01-11 14:50:10 +01002881 }
Jerry Yub7e3fa72022-09-22 11:07:18 +08002882
Jerry Yua8d3c502022-10-30 14:51:23 +08002883 mbedtls_ssl_handshake_set_state(
Gilles Peskine449bd832023-01-11 14:50:10 +01002884 ssl, MBEDTLS_SSL_TLS1_3_NEW_SESSION_TICKET_FLUSH);
2885 } else {
2886 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_HANDSHAKE_OVER);
Jerry Yue67bef42022-07-07 07:29:42 +00002887 }
2888
Jerry Yue67bef42022-07-07 07:29:42 +00002889cleanup:
2890
Gilles Peskine449bd832023-01-11 14:50:10 +01002891 return ret;
Jerry Yue67bef42022-07-07 07:29:42 +00002892}
2893#endif /* MBEDTLS_SSL_SESSION_TICKETS */
2894
2895/*
XiaokangQiane8ff3502022-04-22 02:34:40 +00002896 * TLS 1.3 State Machine -- server side
XiaokangQian7807f9f2022-02-15 10:04:37 +00002897 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002898int mbedtls_ssl_tls13_handshake_server_step(mbedtls_ssl_context *ssl)
Jerry Yub9930e72021-08-06 17:11:51 +08002899{
XiaokangQian08037552022-04-20 07:16:41 +00002900 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
XiaokangQian7807f9f2022-02-15 10:04:37 +00002901
Gilles Peskine449bd832023-01-11 14:50:10 +01002902 if (ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER || ssl->handshake == NULL) {
2903 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
2904 }
XiaokangQian7807f9f2022-02-15 10:04:37 +00002905
Gilles Peskine449bd832023-01-11 14:50:10 +01002906 MBEDTLS_SSL_DEBUG_MSG(2, ("tls13 server state: %s(%d)",
2907 mbedtls_ssl_states_str(ssl->state),
2908 ssl->state));
Jerry Yu6e81b272021-09-27 11:16:17 +08002909
Gilles Peskine449bd832023-01-11 14:50:10 +01002910 switch (ssl->state) {
XiaokangQian7807f9f2022-02-15 10:04:37 +00002911 /* start state */
2912 case MBEDTLS_SSL_HELLO_REQUEST:
Gilles Peskine449bd832023-01-11 14:50:10 +01002913 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_CLIENT_HELLO);
XiaokangQian08037552022-04-20 07:16:41 +00002914 ret = 0;
XiaokangQian7807f9f2022-02-15 10:04:37 +00002915 break;
2916
XiaokangQian7807f9f2022-02-15 10:04:37 +00002917 case MBEDTLS_SSL_CLIENT_HELLO:
Gilles Peskine449bd832023-01-11 14:50:10 +01002918 ret = ssl_tls13_process_client_hello(ssl);
2919 if (ret != 0) {
2920 MBEDTLS_SSL_DEBUG_RET(1, "ssl_tls13_process_client_hello", ret);
2921 }
Jerry Yuf41553b2022-05-09 22:20:30 +08002922 break;
XiaokangQian7807f9f2022-02-15 10:04:37 +00002923
Jerry Yuf41553b2022-05-09 22:20:30 +08002924 case MBEDTLS_SSL_HELLO_RETRY_REQUEST:
Gilles Peskine449bd832023-01-11 14:50:10 +01002925 ret = ssl_tls13_write_hello_retry_request(ssl);
2926 if (ret != 0) {
2927 MBEDTLS_SSL_DEBUG_RET(1, "ssl_tls13_write_hello_retry_request", ret);
2928 return ret;
Jerry Yuf41553b2022-05-09 22:20:30 +08002929 }
XiaokangQian7807f9f2022-02-15 10:04:37 +00002930 break;
2931
Jerry Yu5b64ae92022-03-30 17:15:02 +08002932 case MBEDTLS_SSL_SERVER_HELLO:
Gilles Peskine449bd832023-01-11 14:50:10 +01002933 ret = ssl_tls13_write_server_hello(ssl);
Jerry Yu5b64ae92022-03-30 17:15:02 +08002934 break;
2935
Xiaofei Baicba64af2022-02-15 10:00:56 +00002936 case MBEDTLS_SSL_ENCRYPTED_EXTENSIONS:
Gilles Peskine449bd832023-01-11 14:50:10 +01002937 ret = ssl_tls13_write_encrypted_extensions(ssl);
2938 if (ret != 0) {
2939 MBEDTLS_SSL_DEBUG_RET(1, "ssl_tls13_write_encrypted_extensions", ret);
2940 return ret;
Xiaofei Baicba64af2022-02-15 10:00:56 +00002941 }
Jerry Yu48330562022-05-06 21:35:44 +08002942 break;
Jerry Yu6a2cd9e2022-05-05 11:14:19 +08002943
Ronald Cron928cbd32022-10-04 16:14:26 +02002944#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED)
Xiaofei Bai9ca09d42022-02-14 12:57:18 +00002945 case MBEDTLS_SSL_CERTIFICATE_REQUEST:
Gilles Peskine449bd832023-01-11 14:50:10 +01002946 ret = ssl_tls13_write_certificate_request(ssl);
Xiaofei Bai9ca09d42022-02-14 12:57:18 +00002947 break;
Xiaofei Bai9ca09d42022-02-14 12:57:18 +00002948
Jerry Yu83da34e2022-04-16 13:59:52 +08002949 case MBEDTLS_SSL_SERVER_CERTIFICATE:
Gilles Peskine449bd832023-01-11 14:50:10 +01002950 ret = ssl_tls13_write_server_certificate(ssl);
Jerry Yu83da34e2022-04-16 13:59:52 +08002951 break;
2952
2953 case MBEDTLS_SSL_CERTIFICATE_VERIFY:
Gilles Peskine449bd832023-01-11 14:50:10 +01002954 ret = ssl_tls13_write_certificate_verify(ssl);
Jerry Yu83da34e2022-04-16 13:59:52 +08002955 break;
Ronald Cron928cbd32022-10-04 16:14:26 +02002956#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED */
Jerry Yu83da34e2022-04-16 13:59:52 +08002957
Gilles Peskine449bd832023-01-11 14:50:10 +01002958 /*
2959 * Injection of dummy-CCS's for middlebox compatibility
2960 */
Gabor Mezei7b39bf12022-05-24 16:04:14 +02002961#if defined(MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE)
Gabor Mezeif7044ea2022-06-28 16:01:49 +02002962 case MBEDTLS_SSL_SERVER_CCS_AFTER_HELLO_RETRY_REQUEST:
Gilles Peskine449bd832023-01-11 14:50:10 +01002963 ret = mbedtls_ssl_tls13_write_change_cipher_spec(ssl);
2964 if (ret == 0) {
2965 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_CLIENT_HELLO);
2966 }
Gabor Mezei7b39bf12022-05-24 16:04:14 +02002967 break;
2968
2969 case MBEDTLS_SSL_SERVER_CCS_AFTER_SERVER_HELLO:
Gilles Peskine449bd832023-01-11 14:50:10 +01002970 ret = mbedtls_ssl_tls13_write_change_cipher_spec(ssl);
2971 if (ret == 0) {
2972 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_ENCRYPTED_EXTENSIONS);
2973 }
Gabor Mezei7b39bf12022-05-24 16:04:14 +02002974 break;
2975#endif /* MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE */
2976
Jerry Yu69dd8d42022-04-16 12:51:26 +08002977 case MBEDTLS_SSL_SERVER_FINISHED:
Gilles Peskine449bd832023-01-11 14:50:10 +01002978 ret = ssl_tls13_write_server_finished(ssl);
Jerry Yu69dd8d42022-04-16 12:51:26 +08002979 break;
2980
2981 case MBEDTLS_SSL_CLIENT_FINISHED:
Gilles Peskine449bd832023-01-11 14:50:10 +01002982 ret = ssl_tls13_process_client_finished(ssl);
Jerry Yu69dd8d42022-04-16 12:51:26 +08002983 break;
2984
Jerry Yu69dd8d42022-04-16 12:51:26 +08002985 case MBEDTLS_SSL_HANDSHAKE_WRAPUP:
Gilles Peskine449bd832023-01-11 14:50:10 +01002986 ret = ssl_tls13_handshake_wrapup(ssl);
Jerry Yu69dd8d42022-04-16 12:51:26 +08002987 break;
2988
Ronald Cron766c0cd2022-10-18 12:17:11 +02002989#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED)
XiaokangQian6b916b12022-04-25 07:29:34 +00002990 case MBEDTLS_SSL_CLIENT_CERTIFICATE:
Gilles Peskine449bd832023-01-11 14:50:10 +01002991 ret = mbedtls_ssl_tls13_process_certificate(ssl);
2992 if (ret == 0) {
2993 if (ssl->session_negotiate->peer_cert != NULL) {
Ronald Cron209cae92022-06-07 10:30:19 +02002994 mbedtls_ssl_handshake_set_state(
Gilles Peskine449bd832023-01-11 14:50:10 +01002995 ssl, MBEDTLS_SSL_CLIENT_CERTIFICATE_VERIFY);
2996 } else {
2997 MBEDTLS_SSL_DEBUG_MSG(2, ("skip parse certificate verify"));
Ronald Cron209cae92022-06-07 10:30:19 +02002998 mbedtls_ssl_handshake_set_state(
Gilles Peskine449bd832023-01-11 14:50:10 +01002999 ssl, MBEDTLS_SSL_CLIENT_FINISHED);
Ronald Cron19385882022-06-15 16:26:13 +02003000 }
XiaokangQian6b916b12022-04-25 07:29:34 +00003001 }
3002 break;
3003
3004 case MBEDTLS_SSL_CLIENT_CERTIFICATE_VERIFY:
Gilles Peskine449bd832023-01-11 14:50:10 +01003005 ret = mbedtls_ssl_tls13_process_certificate_verify(ssl);
3006 if (ret == 0) {
XiaokangQian6b916b12022-04-25 07:29:34 +00003007 mbedtls_ssl_handshake_set_state(
Gilles Peskine449bd832023-01-11 14:50:10 +01003008 ssl, MBEDTLS_SSL_CLIENT_FINISHED);
XiaokangQian6b916b12022-04-25 07:29:34 +00003009 }
3010 break;
Ronald Cron766c0cd2022-10-18 12:17:11 +02003011#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED */
XiaokangQian6b916b12022-04-25 07:29:34 +00003012
Jerry Yue67bef42022-07-07 07:29:42 +00003013#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Jerry Yua8d3c502022-10-30 14:51:23 +08003014 case MBEDTLS_SSL_TLS1_3_NEW_SESSION_TICKET:
Gilles Peskine449bd832023-01-11 14:50:10 +01003015 ret = ssl_tls13_write_new_session_ticket(ssl);
3016 if (ret != 0) {
3017 MBEDTLS_SSL_DEBUG_RET(1,
3018 "ssl_tls13_write_new_session_ticket ",
3019 ret);
Jerry Yue67bef42022-07-07 07:29:42 +00003020 }
3021 break;
Jerry Yua8d3c502022-10-30 14:51:23 +08003022 case MBEDTLS_SSL_TLS1_3_NEW_SESSION_TICKET_FLUSH:
Jerry Yue67bef42022-07-07 07:29:42 +00003023 /* This state is necessary to do the flush of the New Session
Jerry Yua8d3c502022-10-30 14:51:23 +08003024 * Ticket message written in MBEDTLS_SSL_TLS1_3_NEW_SESSION_TICKET
Jerry Yue67bef42022-07-07 07:29:42 +00003025 * as part of ssl_prepare_handshake_step.
3026 */
3027 ret = 0;
Jerry Yud4e75002022-08-09 13:33:50 +08003028
Gilles Peskine449bd832023-01-11 14:50:10 +01003029 if (ssl->handshake->new_session_tickets_count == 0) {
3030 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_HANDSHAKE_OVER);
3031 } else {
3032 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_TLS1_3_NEW_SESSION_TICKET);
3033 }
Jerry Yue67bef42022-07-07 07:29:42 +00003034 break;
3035
3036#endif /* MBEDTLS_SSL_SESSION_TICKETS */
3037
XiaokangQian7807f9f2022-02-15 10:04:37 +00003038 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01003039 MBEDTLS_SSL_DEBUG_MSG(1, ("invalid state %d", ssl->state));
3040 return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
XiaokangQian7807f9f2022-02-15 10:04:37 +00003041 }
3042
Gilles Peskine449bd832023-01-11 14:50:10 +01003043 return ret;
Jerry Yub9930e72021-08-06 17:11:51 +08003044}
Jerry Yu3cc4c2a2021-08-06 16:29:08 +08003045
Jerry Yufb4b6472022-01-27 15:03:26 +08003046#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_PROTO_TLS1_3 */