blob: dc3c2f070d3999ced1fcc8f01e575179156b86ab [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
Xiaokang Qian9f1747b2023-03-30 02:50:04 +0000320static int ssl_tls13_offered_psks_check_binder_match(
321 mbedtls_ssl_context *ssl,
322 const unsigned char *binder, size_t binder_len,
323 int psk_type, psa_algorithm_t psk_hash_alg)
Jerry Yu1c105562022-07-10 06:32:38 +0000324{
325 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yu96a2e362022-07-21 15:11:34 +0800326
Jerry Yudaf375a2022-07-20 21:31:43 +0800327 unsigned char transcript[PSA_HASH_MAX_SIZE];
Jerry Yu1c105562022-07-10 06:32:38 +0000328 size_t transcript_len;
Jerry Yu2f0abc92022-07-22 19:34:48 +0800329 unsigned char *psk;
Jerry Yu96a2e362022-07-21 15:11:34 +0800330 size_t psk_len;
Jerry Yudaf375a2022-07-20 21:31:43 +0800331 unsigned char server_computed_binder[PSA_HASH_MAX_SIZE];
Jerry Yu1c105562022-07-10 06:32:38 +0000332
Jerry Yu1c105562022-07-10 06:32:38 +0000333 /* Get current state of handshake transcript. */
Jerry Yu29d9faa2022-08-23 17:52:45 +0800334 ret = mbedtls_ssl_get_handshake_transcript(
Gilles Peskine449bd832023-01-11 14:50:10 +0100335 ssl, mbedtls_hash_info_md_from_psa(psk_hash_alg),
336 transcript, sizeof(transcript), &transcript_len);
337 if (ret != 0) {
338 return ret;
339 }
Jerry Yu1c105562022-07-10 06:32:38 +0000340
Gilles Peskine449bd832023-01-11 14:50:10 +0100341 ret = mbedtls_ssl_tls13_export_handshake_psk(ssl, &psk, &psk_len);
342 if (ret != 0) {
343 return ret;
344 }
Jerry Yu96a2e362022-07-21 15:11:34 +0800345
Gilles Peskine449bd832023-01-11 14:50:10 +0100346 ret = mbedtls_ssl_tls13_create_psk_binder(ssl, psk_hash_alg,
347 psk, psk_len, psk_type,
348 transcript,
349 server_computed_binder);
Jerry Yu96a2e362022-07-21 15:11:34 +0800350#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +0100351 mbedtls_free((void *) psk);
Jerry Yu96a2e362022-07-21 15:11:34 +0800352#endif
Gilles Peskine449bd832023-01-11 14:50:10 +0100353 if (ret != 0) {
354 MBEDTLS_SSL_DEBUG_MSG(1, ("PSK binder calculation failed."));
355 return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
Jerry Yu1c105562022-07-10 06:32:38 +0000356 }
357
Gilles Peskine449bd832023-01-11 14:50:10 +0100358 MBEDTLS_SSL_DEBUG_BUF(3, "psk binder ( computed ): ",
359 server_computed_binder, transcript_len);
360 MBEDTLS_SSL_DEBUG_BUF(3, "psk binder ( received ): ", binder, binder_len);
Jerry Yu1c105562022-07-10 06:32:38 +0000361
Gilles Peskine449bd832023-01-11 14:50:10 +0100362 if (mbedtls_ct_memcmp(server_computed_binder, binder, binder_len) == 0) {
363 return SSL_TLS1_3_OFFERED_PSK_MATCH;
Jerry Yu1c105562022-07-10 06:32:38 +0000364 }
365
Gilles Peskine449bd832023-01-11 14:50:10 +0100366 mbedtls_platform_zeroize(server_computed_binder,
367 sizeof(server_computed_binder));
368 return SSL_TLS1_3_OFFERED_PSK_NOT_MATCH;
Jerry Yu1c105562022-07-10 06:32:38 +0000369}
Jerry Yu96a2e362022-07-21 15:11:34 +0800370
Jerry Yu5725f1c2022-08-21 17:27:16 +0800371MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yuf35ba382022-08-23 17:58:26 +0800372static int ssl_tls13_select_ciphersuite_for_psk(
Gilles Peskine449bd832023-01-11 14:50:10 +0100373 mbedtls_ssl_context *ssl,
374 const unsigned char *cipher_suites,
375 const unsigned char *cipher_suites_end,
376 uint16_t *selected_ciphersuite,
377 const mbedtls_ssl_ciphersuite_t **selected_ciphersuite_info)
Jerry Yu5725f1c2022-08-21 17:27:16 +0800378{
Jerry Yuf35ba382022-08-23 17:58:26 +0800379 psa_algorithm_t psk_hash_alg = PSA_ALG_SHA_256;
Jerry Yu5725f1c2022-08-21 17:27:16 +0800380
Jerry Yu0baf9072022-08-25 11:21:04 +0800381 *selected_ciphersuite = 0;
382 *selected_ciphersuite_info = NULL;
383
Jerry Yuf35ba382022-08-23 17:58:26 +0800384 /* RFC 8446, page 55.
385 *
386 * For externally established PSKs, the Hash algorithm MUST be set when the
387 * PSK is established or default to SHA-256 if no such algorithm is defined.
388 *
389 */
Jerry Yu5725f1c2022-08-21 17:27:16 +0800390
Jerry Yu5725f1c2022-08-21 17:27:16 +0800391 /*
392 * Search for a matching ciphersuite
393 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100394 for (const unsigned char *p = cipher_suites;
395 p < cipher_suites_end; p += 2) {
Jerry Yu5725f1c2022-08-21 17:27:16 +0800396 uint16_t cipher_suite;
Jerry Yuf35ba382022-08-23 17:58:26 +0800397 const mbedtls_ssl_ciphersuite_t *ciphersuite_info;
Jerry Yu5725f1c2022-08-21 17:27:16 +0800398
Gilles Peskine449bd832023-01-11 14:50:10 +0100399 cipher_suite = MBEDTLS_GET_UINT16_BE(p, 0);
400 ciphersuite_info = ssl_tls13_validate_peer_ciphersuite(ssl,
401 cipher_suite);
402 if (ciphersuite_info == NULL) {
Jerry Yu5725f1c2022-08-21 17:27:16 +0800403 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +0100404 }
Jerry Yu5725f1c2022-08-21 17:27:16 +0800405
Jerry Yu5725f1c2022-08-21 17:27:16 +0800406 /* MAC of selected ciphersuite MUST be same with PSK binder if exist.
407 * Otherwise, client should reject.
408 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100409 if (psk_hash_alg == mbedtls_psa_translate_md(ciphersuite_info->mac)) {
Jerry Yuf35ba382022-08-23 17:58:26 +0800410 *selected_ciphersuite = cipher_suite;
411 *selected_ciphersuite_info = ciphersuite_info;
Gilles Peskine449bd832023-01-11 14:50:10 +0100412 return 0;
Jerry Yuf35ba382022-08-23 17:58:26 +0800413 }
414 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100415 MBEDTLS_SSL_DEBUG_MSG(2, ("No matched ciphersuite"));
416 return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
Jerry Yuf35ba382022-08-23 17:58:26 +0800417}
Jerry Yu5725f1c2022-08-21 17:27:16 +0800418
Jerry Yu82534862022-08-30 10:42:33 +0800419#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Jerry Yuf35ba382022-08-23 17:58:26 +0800420MBEDTLS_CHECK_RETURN_CRITICAL
421static int ssl_tls13_select_ciphersuite_for_resumption(
Gilles Peskine449bd832023-01-11 14:50:10 +0100422 mbedtls_ssl_context *ssl,
423 const unsigned char *cipher_suites,
424 const unsigned char *cipher_suites_end,
425 mbedtls_ssl_session *session,
426 uint16_t *selected_ciphersuite,
427 const mbedtls_ssl_ciphersuite_t **selected_ciphersuite_info)
Jerry Yuf35ba382022-08-23 17:58:26 +0800428{
Jerry Yu82534862022-08-30 10:42:33 +0800429
Jerry Yuf35ba382022-08-23 17:58:26 +0800430 *selected_ciphersuite = 0;
431 *selected_ciphersuite_info = NULL;
Gilles Peskine449bd832023-01-11 14:50:10 +0100432 for (const unsigned char *p = cipher_suites; p < cipher_suites_end; p += 2) {
433 uint16_t cipher_suite = MBEDTLS_GET_UINT16_BE(p, 0);
Jerry Yu82534862022-08-30 10:42:33 +0800434 const mbedtls_ssl_ciphersuite_t *ciphersuite_info;
435
Gilles Peskine449bd832023-01-11 14:50:10 +0100436 if (cipher_suite != session->ciphersuite) {
Jerry Yu82534862022-08-30 10:42:33 +0800437 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +0100438 }
Jerry Yu82534862022-08-30 10:42:33 +0800439
Gilles Peskine449bd832023-01-11 14:50:10 +0100440 ciphersuite_info = ssl_tls13_validate_peer_ciphersuite(ssl,
441 cipher_suite);
442 if (ciphersuite_info == NULL) {
Jerry Yu82534862022-08-30 10:42:33 +0800443 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +0100444 }
Jerry Yu82534862022-08-30 10:42:33 +0800445
Jerry Yu4746b102022-09-13 11:11:48 +0800446 *selected_ciphersuite = cipher_suite;
Jerry Yu82534862022-08-30 10:42:33 +0800447 *selected_ciphersuite_info = ciphersuite_info;
448
Gilles Peskine449bd832023-01-11 14:50:10 +0100449 return 0;
Jerry Yu82534862022-08-30 10:42:33 +0800450 }
451
Gilles Peskine449bd832023-01-11 14:50:10 +0100452 return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
Jerry Yu5725f1c2022-08-21 17:27:16 +0800453}
Jerry Yuf35ba382022-08-23 17:58:26 +0800454
Jerry Yu82534862022-08-30 10:42:33 +0800455MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100456static int ssl_tls13_session_copy_ticket(mbedtls_ssl_session *dst,
457 const mbedtls_ssl_session *src)
Jerry Yu82534862022-08-30 10:42:33 +0800458{
Jerry Yu82534862022-08-30 10:42:33 +0800459 dst->ticket_age_add = src->ticket_age_add;
460 dst->ticket_flags = src->ticket_flags;
461 dst->resumption_key_len = src->resumption_key_len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100462 if (src->resumption_key_len == 0) {
463 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
464 }
465 memcpy(dst->resumption_key, src->resumption_key, src->resumption_key_len);
Jerry Yu4746b102022-09-13 11:11:48 +0800466
Gilles Peskine449bd832023-01-11 14:50:10 +0100467 return 0;
Jerry Yu82534862022-08-30 10:42:33 +0800468}
469#endif /* MBEDTLS_SSL_SESSION_TICKETS */
470
Jerry Yu1c105562022-07-10 06:32:38 +0000471/* Parser for pre_shared_key extension in client hello
472 * struct {
473 * opaque identity<1..2^16-1>;
474 * uint32 obfuscated_ticket_age;
475 * } PskIdentity;
476 *
477 * opaque PskBinderEntry<32..255>;
478 *
479 * struct {
480 * PskIdentity identities<7..2^16-1>;
481 * PskBinderEntry binders<33..2^16-1>;
482 * } OfferedPsks;
483 *
484 * struct {
485 * select (Handshake.msg_type) {
486 * case client_hello: OfferedPsks;
487 * ....
488 * };
489 * } PreSharedKeyExtension;
490 */
Jerry Yu6e74a7e2022-07-20 20:49:32 +0800491MBEDTLS_CHECK_RETURN_CRITICAL
Xiaokang Qian9f1747b2023-03-30 02:50:04 +0000492static int ssl_tls13_parse_pre_shared_key_ext(
493 mbedtls_ssl_context *ssl,
494 const unsigned char *pre_shared_key_ext,
495 const unsigned char *pre_shared_key_ext_end,
496 const unsigned char *ciphersuites,
497 const unsigned char *ciphersuites_end)
Jerry Yu1c105562022-07-10 06:32:38 +0000498{
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +0100499 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yu29d9faa2022-08-23 17:52:45 +0800500 const unsigned char *identities = pre_shared_key_ext;
Jerry Yu568ec252022-07-22 21:27:34 +0800501 const unsigned char *p_identity_len;
502 size_t identities_len;
Jerry Yu1c105562022-07-10 06:32:38 +0000503 const unsigned char *identities_end;
Jerry Yu568ec252022-07-22 21:27:34 +0800504 const unsigned char *binders;
505 const unsigned char *p_binder_len;
506 size_t binders_len;
Jerry Yu1c105562022-07-10 06:32:38 +0000507 const unsigned char *binders_end;
Jerry Yu96a2e362022-07-21 15:11:34 +0800508 int matched_identity = -1;
509 int identity_id = -1;
Jerry Yu1c105562022-07-10 06:32:38 +0000510
Gilles Peskine449bd832023-01-11 14:50:10 +0100511 MBEDTLS_SSL_DEBUG_BUF(3, "pre_shared_key extension",
512 pre_shared_key_ext,
513 pre_shared_key_ext_end - pre_shared_key_ext);
Jerry Yu1c105562022-07-10 06:32:38 +0000514
Jerry Yu96a2e362022-07-21 15:11:34 +0800515 /* identities_len 2 bytes
516 * identities_data >= 7 bytes
517 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100518 MBEDTLS_SSL_CHK_BUF_READ_PTR(identities, pre_shared_key_ext_end, 7 + 2);
519 identities_len = MBEDTLS_GET_UINT16_BE(identities, 0);
Jerry Yu568ec252022-07-22 21:27:34 +0800520 p_identity_len = identities + 2;
Gilles Peskine449bd832023-01-11 14:50:10 +0100521 MBEDTLS_SSL_CHK_BUF_READ_PTR(p_identity_len, pre_shared_key_ext_end,
522 identities_len);
Jerry Yu568ec252022-07-22 21:27:34 +0800523 identities_end = p_identity_len + identities_len;
Jerry Yu1c105562022-07-10 06:32:38 +0000524
Jerry Yu96a2e362022-07-21 15:11:34 +0800525 /* binders_len 2 bytes
526 * binders >= 33 bytes
527 */
Jerry Yu568ec252022-07-22 21:27:34 +0800528 binders = identities_end;
Gilles Peskine449bd832023-01-11 14:50:10 +0100529 MBEDTLS_SSL_CHK_BUF_READ_PTR(binders, pre_shared_key_ext_end, 33 + 2);
530 binders_len = MBEDTLS_GET_UINT16_BE(binders, 0);
Jerry Yu568ec252022-07-22 21:27:34 +0800531 p_binder_len = binders + 2;
Gilles Peskine449bd832023-01-11 14:50:10 +0100532 MBEDTLS_SSL_CHK_BUF_READ_PTR(p_binder_len, pre_shared_key_ext_end, binders_len);
Jerry Yu568ec252022-07-22 21:27:34 +0800533 binders_end = p_binder_len + binders_len;
Jerry Yu1c105562022-07-10 06:32:38 +0000534
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +0100535 ret = ssl->handshake->update_checksum(ssl, pre_shared_key_ext,
536 identities_end - pre_shared_key_ext);
537 if (0 != ret) {
538 MBEDTLS_SSL_DEBUG_RET(1, ("update_checksum"), ret);
539 return ret;
540 }
Jerry Yu96a2e362022-07-21 15:11:34 +0800541
Gilles Peskine449bd832023-01-11 14:50:10 +0100542 while (p_identity_len < identities_end && p_binder_len < binders_end) {
Jerry Yu1c105562022-07-10 06:32:38 +0000543 const unsigned char *identity;
Jerry Yu568ec252022-07-22 21:27:34 +0800544 size_t identity_len;
Jerry Yu82534862022-08-30 10:42:33 +0800545 uint32_t obfuscated_ticket_age;
Jerry Yu96a2e362022-07-21 15:11:34 +0800546 const unsigned char *binder;
Jerry Yu568ec252022-07-22 21:27:34 +0800547 size_t binder_len;
Jerry Yu5725f1c2022-08-21 17:27:16 +0800548 int psk_type;
549 uint16_t cipher_suite;
Jerry Yu29d9faa2022-08-23 17:52:45 +0800550 const mbedtls_ssl_ciphersuite_t *ciphersuite_info;
Jerry Yu82534862022-08-30 10:42:33 +0800551#if defined(MBEDTLS_SSL_SESSION_TICKETS)
552 mbedtls_ssl_session session;
Gilles Peskine449bd832023-01-11 14:50:10 +0100553 mbedtls_ssl_session_init(&session);
Jerry Yu82534862022-08-30 10:42:33 +0800554#endif
Jerry Yubb852022022-07-20 21:10:44 +0800555
Gilles Peskine449bd832023-01-11 14:50:10 +0100556 MBEDTLS_SSL_CHK_BUF_READ_PTR(p_identity_len, identities_end, 2 + 1 + 4);
557 identity_len = MBEDTLS_GET_UINT16_BE(p_identity_len, 0);
Jerry Yu568ec252022-07-22 21:27:34 +0800558 identity = p_identity_len + 2;
Gilles Peskine449bd832023-01-11 14:50:10 +0100559 MBEDTLS_SSL_CHK_BUF_READ_PTR(identity, identities_end, identity_len + 4);
560 obfuscated_ticket_age = MBEDTLS_GET_UINT32_BE(identity, identity_len);
Jerry Yu568ec252022-07-22 21:27:34 +0800561 p_identity_len += identity_len + 6;
Jerry Yu1c105562022-07-10 06:32:38 +0000562
Gilles Peskine449bd832023-01-11 14:50:10 +0100563 MBEDTLS_SSL_CHK_BUF_READ_PTR(p_binder_len, binders_end, 1 + 32);
Jerry Yu568ec252022-07-22 21:27:34 +0800564 binder_len = *p_binder_len;
565 binder = p_binder_len + 1;
Gilles Peskine449bd832023-01-11 14:50:10 +0100566 MBEDTLS_SSL_CHK_BUF_READ_PTR(binder, binders_end, binder_len);
Jerry Yu568ec252022-07-22 21:27:34 +0800567 p_binder_len += binder_len + 1;
Jerry Yu96a2e362022-07-21 15:11:34 +0800568
Jerry Yu96a2e362022-07-21 15:11:34 +0800569 identity_id++;
Gilles Peskine449bd832023-01-11 14:50:10 +0100570 if (matched_identity != -1) {
Jerry Yu1c105562022-07-10 06:32:38 +0000571 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +0100572 }
Jerry Yu1c105562022-07-10 06:32:38 +0000573
Jerry Yu96a2e362022-07-21 15:11:34 +0800574 ret = ssl_tls13_offered_psks_check_identity_match(
Gilles Peskine449bd832023-01-11 14:50:10 +0100575 ssl, identity, identity_len, obfuscated_ticket_age,
576 &psk_type, &session);
577 if (ret != SSL_TLS1_3_OFFERED_PSK_MATCH) {
Jerry Yu96a2e362022-07-21 15:11:34 +0800578 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +0100579 }
Jerry Yu96a2e362022-07-21 15:11:34 +0800580
Gilles Peskine449bd832023-01-11 14:50:10 +0100581 MBEDTLS_SSL_DEBUG_MSG(4, ("found matched identity"));
582 switch (psk_type) {
Jerry Yu0baf9072022-08-25 11:21:04 +0800583 case MBEDTLS_SSL_TLS1_3_PSK_EXTERNAL:
584 ret = ssl_tls13_select_ciphersuite_for_psk(
Gilles Peskine449bd832023-01-11 14:50:10 +0100585 ssl, ciphersuites, ciphersuites_end,
586 &cipher_suite, &ciphersuite_info);
Jerry Yu0baf9072022-08-25 11:21:04 +0800587 break;
588 case MBEDTLS_SSL_TLS1_3_PSK_RESUMPTION:
Jerry Yu82534862022-08-30 10:42:33 +0800589#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Jerry Yu0baf9072022-08-25 11:21:04 +0800590 ret = ssl_tls13_select_ciphersuite_for_resumption(
Gilles Peskine449bd832023-01-11 14:50:10 +0100591 ssl, ciphersuites, ciphersuites_end, &session,
592 &cipher_suite, &ciphersuite_info);
593 if (ret != 0) {
594 mbedtls_ssl_session_free(&session);
595 }
Jerry Yu82534862022-08-30 10:42:33 +0800596#else
597 ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
598#endif
Jerry Yu0baf9072022-08-25 11:21:04 +0800599 break;
600 default:
Gilles Peskine449bd832023-01-11 14:50:10 +0100601 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Jerry Yu0baf9072022-08-25 11:21:04 +0800602 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100603 if (ret != 0) {
Jerry Yuf35ba382022-08-23 17:58:26 +0800604 /* See below, no cipher_suite available, abort handshake */
605 MBEDTLS_SSL_PEND_FATAL_ALERT(
606 MBEDTLS_SSL_ALERT_MSG_DECRYPT_ERROR,
Gilles Peskine449bd832023-01-11 14:50:10 +0100607 MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE);
Jerry Yuf35ba382022-08-23 17:58:26 +0800608 MBEDTLS_SSL_DEBUG_RET(
Gilles Peskine449bd832023-01-11 14:50:10 +0100609 2, "ssl_tls13_select_ciphersuite", ret);
610 return ret;
Jerry Yu5725f1c2022-08-21 17:27:16 +0800611 }
612
Jerry Yu96a2e362022-07-21 15:11:34 +0800613 ret = ssl_tls13_offered_psks_check_binder_match(
Gilles Peskine449bd832023-01-11 14:50:10 +0100614 ssl, binder, binder_len, psk_type,
615 mbedtls_psa_translate_md(ciphersuite_info->mac));
616 if (ret != SSL_TLS1_3_OFFERED_PSK_MATCH) {
Jerry Yuc5a23a02022-08-25 10:51:44 +0800617 /* For security reasons, the handshake should be aborted when we
618 * fail to validate a binder value. See RFC 8446 section 4.2.11.2
619 * and appendix E.6. */
Jerry Yu82534862022-08-30 10:42:33 +0800620#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Gilles Peskine449bd832023-01-11 14:50:10 +0100621 mbedtls_ssl_session_free(&session);
Jerry Yu82534862022-08-30 10:42:33 +0800622#endif
Gilles Peskine449bd832023-01-11 14:50:10 +0100623 MBEDTLS_SSL_DEBUG_MSG(3, ("Invalid binder."));
Xiaokang Qian9f1747b2023-03-30 02:50:04 +0000624 MBEDTLS_SSL_DEBUG_RET(
625 1, "ssl_tls13_offered_psks_check_binder_match", ret);
Jerry Yu96a2e362022-07-21 15:11:34 +0800626 MBEDTLS_SSL_PEND_FATAL_ALERT(
627 MBEDTLS_SSL_ALERT_MSG_DECRYPT_ERROR,
Gilles Peskine449bd832023-01-11 14:50:10 +0100628 MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE);
629 return ret;
Jerry Yu96a2e362022-07-21 15:11:34 +0800630 }
Jerry Yu96a2e362022-07-21 15:11:34 +0800631
632 matched_identity = identity_id;
Jerry Yu5725f1c2022-08-21 17:27:16 +0800633
634 /* Update handshake parameters */
Jerry Yue5834fd2022-08-29 20:16:09 +0800635 ssl->handshake->ciphersuite_info = ciphersuite_info;
Jerry Yu4746b102022-09-13 11:11:48 +0800636 ssl->session_negotiate->ciphersuite = cipher_suite;
Gilles Peskine449bd832023-01-11 14:50:10 +0100637 MBEDTLS_SSL_DEBUG_MSG(2, ("overwrite ciphersuite: %04x - %s",
638 cipher_suite, ciphersuite_info->name));
Jerry Yu82534862022-08-30 10:42:33 +0800639#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Gilles Peskine449bd832023-01-11 14:50:10 +0100640 if (psk_type == MBEDTLS_SSL_TLS1_3_PSK_RESUMPTION) {
641 ret = ssl_tls13_session_copy_ticket(ssl->session_negotiate,
642 &session);
643 mbedtls_ssl_session_free(&session);
644 if (ret != 0) {
645 return ret;
646 }
Jerry Yu82534862022-08-30 10:42:33 +0800647 }
648#endif /* MBEDTLS_SSL_SESSION_TICKETS */
Jerry Yu1c105562022-07-10 06:32:38 +0000649 }
650
Gilles Peskine449bd832023-01-11 14:50:10 +0100651 if (p_identity_len != identities_end || p_binder_len != binders_end) {
652 MBEDTLS_SSL_DEBUG_MSG(3, ("pre_shared_key extension decode error"));
653 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR,
654 MBEDTLS_ERR_SSL_DECODE_ERROR);
655 return MBEDTLS_ERR_SSL_DECODE_ERROR;
Jerry Yu1c105562022-07-10 06:32:38 +0000656 }
657
Jerry Yu6f1db3f2022-07-22 23:05:59 +0800658 /* Update the handshake transcript with the binder list. */
Xiaokang Qian9f1747b2023-03-30 02:50:04 +0000659 ret = ssl->handshake->update_checksum(
660 ssl, identities_end, (size_t) (binders_end - identities_end));
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +0100661 if (0 != ret) {
662 MBEDTLS_SSL_DEBUG_RET(1, ("update_checksum"), ret);
663 return ret;
664 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100665 if (matched_identity == -1) {
666 MBEDTLS_SSL_DEBUG_MSG(3, ("No matched PSK or ticket."));
667 return MBEDTLS_ERR_SSL_UNKNOWN_IDENTITY;
Jerry Yu1c105562022-07-10 06:32:38 +0000668 }
669
Gilles Peskine449bd832023-01-11 14:50:10 +0100670 ssl->handshake->selected_identity = (uint16_t) matched_identity;
671 MBEDTLS_SSL_DEBUG_MSG(3, ("Pre shared key found"));
Jerry Yu1c105562022-07-10 06:32:38 +0000672
Gilles Peskine449bd832023-01-11 14:50:10 +0100673 return 0;
Jerry Yu1c105562022-07-10 06:32:38 +0000674}
Jerry Yu032b15ce2022-07-11 06:10:03 +0000675
676/*
677 * struct {
678 * select ( Handshake.msg_type ) {
679 * ....
680 * case server_hello:
681 * uint16 selected_identity;
682 * }
683 * } PreSharedKeyExtension;
684 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100685static int ssl_tls13_write_server_pre_shared_key_ext(mbedtls_ssl_context *ssl,
686 unsigned char *buf,
687 unsigned char *end,
688 size_t *olen)
Jerry Yu032b15ce2022-07-11 06:10:03 +0000689{
Gilles Peskine449bd832023-01-11 14:50:10 +0100690 unsigned char *p = (unsigned char *) buf;
Jerry Yu032b15ce2022-07-11 06:10:03 +0000691
692 *olen = 0;
693
David Horstmann21b89762022-10-06 18:34:28 +0100694 int not_using_psk = 0;
Jerry Yu032b15ce2022-07-11 06:10:03 +0000695#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +0100696 not_using_psk = (mbedtls_svc_key_id_is_null(ssl->handshake->psk_opaque));
Jerry Yu032b15ce2022-07-11 06:10:03 +0000697#else
Gilles Peskine449bd832023-01-11 14:50:10 +0100698 not_using_psk = (ssl->handshake->psk == NULL);
Jerry Yu032b15ce2022-07-11 06:10:03 +0000699#endif
Gilles Peskine449bd832023-01-11 14:50:10 +0100700 if (not_using_psk) {
Jerry Yu032b15ce2022-07-11 06:10:03 +0000701 /* We shouldn't have called this extension writer unless we've
702 * chosen to use a PSK. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100703 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Jerry Yu032b15ce2022-07-11 06:10:03 +0000704 }
705
Gilles Peskine449bd832023-01-11 14:50:10 +0100706 MBEDTLS_SSL_DEBUG_MSG(3, ("server hello, adding pre_shared_key extension"));
707 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 6);
Jerry Yu032b15ce2022-07-11 06:10:03 +0000708
Gilles Peskine449bd832023-01-11 14:50:10 +0100709 MBEDTLS_PUT_UINT16_BE(MBEDTLS_TLS_EXT_PRE_SHARED_KEY, p, 0);
710 MBEDTLS_PUT_UINT16_BE(2, p, 2);
Jerry Yu032b15ce2022-07-11 06:10:03 +0000711
Gilles Peskine449bd832023-01-11 14:50:10 +0100712 MBEDTLS_PUT_UINT16_BE(ssl->handshake->selected_identity, p, 4);
Jerry Yu032b15ce2022-07-11 06:10:03 +0000713
714 *olen = 6;
715
Gilles Peskine449bd832023-01-11 14:50:10 +0100716 MBEDTLS_SSL_DEBUG_MSG(4, ("sent selected_identity: %u",
717 ssl->handshake->selected_identity));
Jerry Yu032b15ce2022-07-11 06:10:03 +0000718
Gilles Peskine449bd832023-01-11 14:50:10 +0100719 mbedtls_ssl_tls13_set_hs_sent_ext_mask(ssl, MBEDTLS_TLS_EXT_PRE_SHARED_KEY);
Jerry Yub95dd362022-11-08 21:19:34 +0800720
Gilles Peskine449bd832023-01-11 14:50:10 +0100721 return 0;
Jerry Yu032b15ce2022-07-11 06:10:03 +0000722}
723
Ronald Cron41a443a2022-10-04 16:38:25 +0200724#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED */
Jerry Yue19e3b92022-07-08 12:04:51 +0000725
XiaokangQian7807f9f2022-02-15 10:04:37 +0000726/* From RFC 8446:
727 * struct {
XiaokangQiancfd925f2022-04-14 07:10:37 +0000728 * ProtocolVersion versions<2..254>;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000729 * } SupportedVersions;
730 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200731MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100732static int ssl_tls13_parse_supported_versions_ext(mbedtls_ssl_context *ssl,
733 const unsigned char *buf,
734 const unsigned char *end)
XiaokangQian7807f9f2022-02-15 10:04:37 +0000735{
XiaokangQian7807f9f2022-02-15 10:04:37 +0000736 const unsigned char *p = buf;
XiaokangQian8f9dfe42022-04-15 02:52:39 +0000737 size_t versions_len;
XiaokangQian4080a7f2022-04-11 09:55:18 +0000738 const unsigned char *versions_end;
XiaokangQian0a1b54e2022-04-21 03:01:38 +0000739 uint16_t tls_version;
Ronald Cron5af4c7f2023-03-07 20:46:59 +0100740 int found_supported_version = 0;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000741
Gilles Peskine449bd832023-01-11 14:50:10 +0100742 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, 1);
XiaokangQian4080a7f2022-04-11 09:55:18 +0000743 versions_len = p[0];
XiaokangQian7807f9f2022-02-15 10:04:37 +0000744 p += 1;
745
Gilles Peskine449bd832023-01-11 14:50:10 +0100746 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, versions_len);
XiaokangQian4080a7f2022-04-11 09:55:18 +0000747 versions_end = p + versions_len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100748 while (p < versions_end) {
749 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, versions_end, 2);
750 tls_version = mbedtls_ssl_read_version(p, ssl->conf->transport);
XiaokangQianb67384d2022-04-19 00:02:38 +0000751 p += 2;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000752
Ronald Cron3bd2b022023-04-03 16:45:39 +0200753 if (MBEDTLS_SSL_VERSION_TLS1_3 == tls_version) {
Ronald Cron5af4c7f2023-03-07 20:46:59 +0100754 found_supported_version = 1;
755 break;
756 }
757
Ronald Cron3bd2b022023-04-03 16:45:39 +0200758 if ((MBEDTLS_SSL_VERSION_TLS1_2 == tls_version) &&
759 mbedtls_ssl_conf_is_tls12_enabled(ssl->conf)) {
Ronald Cron5af4c7f2023-03-07 20:46:59 +0100760 found_supported_version = 1;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000761 break;
762 }
XiaokangQian7807f9f2022-02-15 10:04:37 +0000763 }
764
Ronald Cron5af4c7f2023-03-07 20:46:59 +0100765 if (!found_supported_version) {
766 MBEDTLS_SSL_DEBUG_MSG(1, ("No supported version found."));
XiaokangQian7807f9f2022-02-15 10:04:37 +0000767
Gilles Peskine449bd832023-01-11 14:50:10 +0100768 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_PROTOCOL_VERSION,
769 MBEDTLS_ERR_SSL_BAD_PROTOCOL_VERSION);
770 return MBEDTLS_ERR_SSL_BAD_PROTOCOL_VERSION;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000771 }
772
Ronald Cron5af4c7f2023-03-07 20:46:59 +0100773 MBEDTLS_SSL_DEBUG_MSG(1, ("Negotiated version: [%04x]",
Gilles Peskine449bd832023-01-11 14:50:10 +0100774 (unsigned int) tls_version));
XiaokangQian7807f9f2022-02-15 10:04:37 +0000775
Ronald Cron5af4c7f2023-03-07 20:46:59 +0100776 return (int) tls_version;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000777}
778
Valerio Setti080a22b2023-03-20 15:22:47 +0100779#if defined(PSA_WANT_ALG_ECDH)
XiaokangQiane8ff3502022-04-22 02:34:40 +0000780/*
XiaokangQian7807f9f2022-02-15 10:04:37 +0000781 *
782 * From RFC 8446:
783 * enum {
784 * ... (0xFFFF)
785 * } NamedGroup;
786 * struct {
787 * NamedGroup named_group_list<2..2^16-1>;
788 * } NamedGroupList;
789 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200790MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100791static int ssl_tls13_parse_supported_groups_ext(mbedtls_ssl_context *ssl,
792 const unsigned char *buf,
793 const unsigned char *end)
XiaokangQian7807f9f2022-02-15 10:04:37 +0000794{
XiaokangQian7807f9f2022-02-15 10:04:37 +0000795 const unsigned char *p = buf;
XiaokangQian84823772022-04-19 07:57:30 +0000796 size_t named_group_list_len;
XiaokangQian8f9dfe42022-04-15 02:52:39 +0000797 const unsigned char *named_group_list_end;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000798
Gilles Peskine449bd832023-01-11 14:50:10 +0100799 MBEDTLS_SSL_DEBUG_BUF(3, "supported_groups extension", p, end - buf);
800 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, 2);
801 named_group_list_len = MBEDTLS_GET_UINT16_BE(p, 0);
XiaokangQian7807f9f2022-02-15 10:04:37 +0000802 p += 2;
Gilles Peskine449bd832023-01-11 14:50:10 +0100803 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, named_group_list_len);
XiaokangQian8f9dfe42022-04-15 02:52:39 +0000804 named_group_list_end = p + named_group_list_len;
XiaokangQian4e8cd7b2022-04-21 09:48:09 +0000805 ssl->handshake->hrr_selected_group = 0;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000806
Gilles Peskine449bd832023-01-11 14:50:10 +0100807 while (p < named_group_list_end) {
XiaokangQian08037552022-04-20 07:16:41 +0000808 uint16_t named_group;
Gilles Peskine449bd832023-01-11 14:50:10 +0100809 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, named_group_list_end, 2);
810 named_group = MBEDTLS_GET_UINT16_BE(p, 0);
XiaokangQian08037552022-04-20 07:16:41 +0000811 p += 2;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000812
Gilles Peskine449bd832023-01-11 14:50:10 +0100813 MBEDTLS_SSL_DEBUG_MSG(2,
814 ("got named group: %s(%04x)",
815 mbedtls_ssl_named_group_to_str(named_group),
816 named_group));
XiaokangQian08037552022-04-20 07:16:41 +0000817
Gilles Peskine449bd832023-01-11 14:50:10 +0100818 if (!mbedtls_ssl_named_group_is_offered(ssl, named_group) ||
819 !mbedtls_ssl_named_group_is_supported(named_group) ||
820 ssl->handshake->hrr_selected_group != 0) {
XiaokangQian08037552022-04-20 07:16:41 +0000821 continue;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000822 }
823
Gilles Peskine449bd832023-01-11 14:50:10 +0100824 MBEDTLS_SSL_DEBUG_MSG(2,
825 ("add named group %s(%04x) into received list.",
826 mbedtls_ssl_named_group_to_str(named_group),
827 named_group));
Jerry Yuc1be19f2022-04-23 16:11:39 +0800828
XiaokangQian4e8cd7b2022-04-21 09:48:09 +0000829 ssl->handshake->hrr_selected_group = named_group;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000830 }
831
Gilles Peskine449bd832023-01-11 14:50:10 +0100832 return 0;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000833
834}
Valerio Setti080a22b2023-03-20 15:22:47 +0100835#endif /* PSA_WANT_ALG_ECDH */
XiaokangQian7807f9f2022-02-15 10:04:37 +0000836
XiaokangQian08037552022-04-20 07:16:41 +0000837#define SSL_TLS1_3_PARSE_KEY_SHARES_EXT_NO_MATCH 1
838
Valerio Setti080a22b2023-03-20 15:22:47 +0100839#if defined(PSA_WANT_ALG_ECDH)
XiaokangQian7807f9f2022-02-15 10:04:37 +0000840/*
841 * ssl_tls13_parse_key_shares_ext() verifies whether the information in the
Xiaokang Qian9f1747b2023-03-30 02:50:04 +0000842 * extension is correct and stores the first acceptable key share and its
843 * associated group.
XiaokangQian7807f9f2022-02-15 10:04:37 +0000844 *
845 * Possible return values are:
846 * - 0: Successful processing of the client provided key share extension.
Xiaokang Qian9f1747b2023-03-30 02:50:04 +0000847 * - SSL_TLS1_3_PARSE_KEY_SHARES_EXT_NO_MATCH: The key shares provided by
848 * the client does not match a group supported by the server. A
849 * HelloRetryRequest will be needed.
XiaokangQiane8ff3502022-04-22 02:34:40 +0000850 * - A negative value for fatal errors.
Jerry Yuc1be19f2022-04-23 16:11:39 +0800851 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200852MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100853static int ssl_tls13_parse_key_shares_ext(mbedtls_ssl_context *ssl,
854 const unsigned char *buf,
855 const unsigned char *end)
XiaokangQian7807f9f2022-02-15 10:04:37 +0000856{
XiaokangQianb67384d2022-04-19 00:02:38 +0000857 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000858 unsigned char const *p = buf;
XiaokangQian8f9dfe42022-04-15 02:52:39 +0000859 unsigned char const *client_shares_end;
Jerry Yuc1be19f2022-04-23 16:11:39 +0800860 size_t client_shares_len;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000861
862 /* From RFC 8446:
863 *
864 * struct {
865 * KeyShareEntry client_shares<0..2^16-1>;
866 * } KeyShareClientHello;
867 *
868 */
869
Gilles Peskine449bd832023-01-11 14:50:10 +0100870 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, 2);
871 client_shares_len = MBEDTLS_GET_UINT16_BE(p, 0);
XiaokangQian7807f9f2022-02-15 10:04:37 +0000872 p += 2;
Gilles Peskine449bd832023-01-11 14:50:10 +0100873 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, client_shares_len);
XiaokangQian7807f9f2022-02-15 10:04:37 +0000874
875 ssl->handshake->offered_group_id = 0;
XiaokangQian8f9dfe42022-04-15 02:52:39 +0000876 client_shares_end = p + client_shares_len;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000877
878 /* We try to find a suitable key share entry and copy it to the
879 * handshake context. Later, we have to find out whether we can do
880 * something with the provided key share or whether we have to
XiaokangQianc5763b52022-04-02 03:34:37 +0000881 * dismiss it and send a HelloRetryRequest message.
882 */
XiaokangQian7807f9f2022-02-15 10:04:37 +0000883
Gilles Peskine449bd832023-01-11 14:50:10 +0100884 while (p < client_shares_end) {
XiaokangQian9b5d04b2022-04-10 10:20:43 +0000885 uint16_t group;
Jerry Yuc1be19f2022-04-23 16:11:39 +0800886 size_t key_exchange_len;
Jerry Yu086edc22022-05-05 10:50:38 +0800887 const unsigned char *key_exchange;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000888
889 /*
890 * struct {
891 * NamedGroup group;
892 * opaque key_exchange<1..2^16-1>;
893 * } KeyShareEntry;
894 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100895 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, client_shares_end, 4);
896 group = MBEDTLS_GET_UINT16_BE(p, 0);
897 key_exchange_len = MBEDTLS_GET_UINT16_BE(p, 2);
Jerry Yuc1be19f2022-04-23 16:11:39 +0800898 p += 4;
Jerry Yu086edc22022-05-05 10:50:38 +0800899 key_exchange = p;
Gilles Peskine449bd832023-01-11 14:50:10 +0100900 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, client_shares_end, key_exchange_len);
Jerry Yu086edc22022-05-05 10:50:38 +0800901 p += key_exchange_len;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000902
903 /* Continue parsing even if we have already found a match,
XiaokangQianc5763b52022-04-02 03:34:37 +0000904 * for input validation purposes.
905 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100906 if (!mbedtls_ssl_named_group_is_offered(ssl, group) ||
907 !mbedtls_ssl_named_group_is_supported(group) ||
908 ssl->handshake->offered_group_id != 0) {
XiaokangQian060d8672022-04-21 09:24:56 +0000909 continue;
910 }
XiaokangQian060d8672022-04-21 09:24:56 +0000911
XiaokangQian7807f9f2022-02-15 10:04:37 +0000912 /*
XiaokangQian8f9dfe42022-04-15 02:52:39 +0000913 * For now, we only support ECDHE groups.
XiaokangQian7807f9f2022-02-15 10:04:37 +0000914 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100915 if (mbedtls_ssl_tls13_named_group_is_ecdhe(group)) {
916 MBEDTLS_SSL_DEBUG_MSG(2, ("ECDH group: %s (%04x)",
917 mbedtls_ssl_named_group_to_str(group),
918 group));
XiaokangQian318dc762022-04-20 09:43:51 +0000919 ret = mbedtls_ssl_tls13_read_public_ecdhe_share(
Gilles Peskine449bd832023-01-11 14:50:10 +0100920 ssl, key_exchange - 2, key_exchange_len + 2);
921 if (ret != 0) {
922 return ret;
923 }
XiaokangQian4e8cd7b2022-04-21 09:48:09 +0000924
Gilles Peskine449bd832023-01-11 14:50:10 +0100925 } else {
926 MBEDTLS_SSL_DEBUG_MSG(4, ("Unrecognized NamedGroup %u",
927 (unsigned) group));
XiaokangQian7807f9f2022-02-15 10:04:37 +0000928 continue;
929 }
930
XiaokangQian9b5d04b2022-04-10 10:20:43 +0000931 ssl->handshake->offered_group_id = group;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000932 }
933
Jerry Yuc1be19f2022-04-23 16:11:39 +0800934
Gilles Peskine449bd832023-01-11 14:50:10 +0100935 if (ssl->handshake->offered_group_id == 0) {
936 MBEDTLS_SSL_DEBUG_MSG(1, ("no matching key share"));
937 return SSL_TLS1_3_PARSE_KEY_SHARES_EXT_NO_MATCH;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000938 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100939 return 0;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000940}
Valerio Setti080a22b2023-03-20 15:22:47 +0100941#endif /* PSA_WANT_ALG_ECDH */
XiaokangQian7807f9f2022-02-15 10:04:37 +0000942
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200943MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100944static int ssl_tls13_client_hello_has_exts(mbedtls_ssl_context *ssl,
945 int exts_mask)
XiaokangQian7807f9f2022-02-15 10:04:37 +0000946{
Jerry Yu0c354a22022-08-29 15:25:36 +0800947 int masked = ssl->handshake->received_extensions & exts_mask;
Gilles Peskine449bd832023-01-11 14:50:10 +0100948 return masked == exts_mask;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000949}
950
Jerry Yue5991322022-11-07 14:03:44 +0800951#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200952MBEDTLS_CHECK_RETURN_CRITICAL
XiaokangQianb67384d2022-04-19 00:02:38 +0000953static int ssl_tls13_client_hello_has_exts_for_ephemeral_key_exchange(
Gilles Peskine449bd832023-01-11 14:50:10 +0100954 mbedtls_ssl_context *ssl)
XiaokangQian7807f9f2022-02-15 10:04:37 +0000955{
Gilles Peskine449bd832023-01-11 14:50:10 +0100956 return ssl_tls13_client_hello_has_exts(
957 ssl,
958 MBEDTLS_SSL_EXT_MASK(SUPPORTED_GROUPS) |
959 MBEDTLS_SSL_EXT_MASK(KEY_SHARE) |
960 MBEDTLS_SSL_EXT_MASK(SIG_ALG));
XiaokangQian7807f9f2022-02-15 10:04:37 +0000961}
Jerry Yue5991322022-11-07 14:03:44 +0800962#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED */
XiaokangQian7807f9f2022-02-15 10:04:37 +0000963
Jerry Yue5991322022-11-07 14:03:44 +0800964#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK_ENABLED)
Jerry Yu77f01482022-07-11 07:03:24 +0000965MBEDTLS_CHECK_RETURN_CRITICAL
966static int ssl_tls13_client_hello_has_exts_for_psk_key_exchange(
Gilles Peskine449bd832023-01-11 14:50:10 +0100967 mbedtls_ssl_context *ssl)
Jerry Yu77f01482022-07-11 07:03:24 +0000968{
Gilles Peskine449bd832023-01-11 14:50:10 +0100969 return ssl_tls13_client_hello_has_exts(
970 ssl,
971 MBEDTLS_SSL_EXT_MASK(PRE_SHARED_KEY) |
972 MBEDTLS_SSL_EXT_MASK(PSK_KEY_EXCHANGE_MODES));
Jerry Yu77f01482022-07-11 07:03:24 +0000973}
Jerry Yue5991322022-11-07 14:03:44 +0800974#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK_ENABLED */
Jerry Yu77f01482022-07-11 07:03:24 +0000975
Jerry Yue5991322022-11-07 14:03:44 +0800976#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK_EPHEMERAL_ENABLED)
Jerry Yu77f01482022-07-11 07:03:24 +0000977MBEDTLS_CHECK_RETURN_CRITICAL
978static int ssl_tls13_client_hello_has_exts_for_psk_ephemeral_key_exchange(
Gilles Peskine449bd832023-01-11 14:50:10 +0100979 mbedtls_ssl_context *ssl)
Jerry Yu77f01482022-07-11 07:03:24 +0000980{
Gilles Peskine449bd832023-01-11 14:50:10 +0100981 return ssl_tls13_client_hello_has_exts(
982 ssl,
983 MBEDTLS_SSL_EXT_MASK(SUPPORTED_GROUPS) |
984 MBEDTLS_SSL_EXT_MASK(KEY_SHARE) |
985 MBEDTLS_SSL_EXT_MASK(PRE_SHARED_KEY) |
986 MBEDTLS_SSL_EXT_MASK(PSK_KEY_EXCHANGE_MODES));
Jerry Yu77f01482022-07-11 07:03:24 +0000987}
Jerry Yue5991322022-11-07 14:03:44 +0800988#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK_EPHEMERAL_ENABLED */
Jerry Yu77f01482022-07-11 07:03:24 +0000989
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200990MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100991static int ssl_tls13_check_ephemeral_key_exchange(mbedtls_ssl_context *ssl)
XiaokangQian7807f9f2022-02-15 10:04:37 +0000992{
Jerry Yue5991322022-11-07 14:03:44 +0800993#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +0100994 return mbedtls_ssl_conf_tls13_ephemeral_enabled(ssl) &&
995 ssl_tls13_client_hello_has_exts_for_ephemeral_key_exchange(ssl);
Jerry Yue5991322022-11-07 14:03:44 +0800996#else
997 ((void) ssl);
Gilles Peskine449bd832023-01-11 14:50:10 +0100998 return 0;
Jerry Yue5991322022-11-07 14:03:44 +0800999#endif
Jerry Yu77f01482022-07-11 07:03:24 +00001000}
XiaokangQian7807f9f2022-02-15 10:04:37 +00001001
Jerry Yu77f01482022-07-11 07:03:24 +00001002MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01001003static int ssl_tls13_check_psk_key_exchange(mbedtls_ssl_context *ssl)
Jerry Yu77f01482022-07-11 07:03:24 +00001004{
Jerry Yue5991322022-11-07 14:03:44 +08001005#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01001006 return mbedtls_ssl_conf_tls13_psk_enabled(ssl) &&
1007 mbedtls_ssl_tls13_psk_enabled(ssl) &&
1008 ssl_tls13_client_hello_has_exts_for_psk_key_exchange(ssl);
Jerry Yu77f01482022-07-11 07:03:24 +00001009#else
1010 ((void) ssl);
Gilles Peskine449bd832023-01-11 14:50:10 +01001011 return 0;
Jerry Yu77f01482022-07-11 07:03:24 +00001012#endif
1013}
XiaokangQian7807f9f2022-02-15 10:04:37 +00001014
Jerry Yu77f01482022-07-11 07:03:24 +00001015MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01001016static int ssl_tls13_check_psk_ephemeral_key_exchange(mbedtls_ssl_context *ssl)
Jerry Yu77f01482022-07-11 07:03:24 +00001017{
Jerry Yue5991322022-11-07 14:03:44 +08001018#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK_EPHEMERAL_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01001019 return mbedtls_ssl_conf_tls13_psk_ephemeral_enabled(ssl) &&
1020 mbedtls_ssl_tls13_psk_ephemeral_enabled(ssl) &&
1021 ssl_tls13_client_hello_has_exts_for_psk_ephemeral_key_exchange(ssl);
Jerry Yu77f01482022-07-11 07:03:24 +00001022#else
1023 ((void) ssl);
Gilles Peskine449bd832023-01-11 14:50:10 +01001024 return 0;
Jerry Yu77f01482022-07-11 07:03:24 +00001025#endif
1026}
1027
Gilles Peskine449bd832023-01-11 14:50:10 +01001028static int ssl_tls13_determine_key_exchange_mode(mbedtls_ssl_context *ssl)
Jerry Yu77f01482022-07-11 07:03:24 +00001029{
1030 /*
1031 * Determine the key exchange algorithm to use.
1032 * There are three types of key exchanges supported in TLS 1.3:
1033 * - (EC)DH with ECDSA,
1034 * - (EC)DH with PSK,
1035 * - plain PSK.
1036 *
1037 * The PSK-based key exchanges may additionally be used with 0-RTT.
1038 *
1039 * Our built-in order of preference is
Jerry Yuce6ed702022-07-22 21:49:53 +08001040 * 1 ) (EC)DHE-PSK Mode ( psk_ephemeral )
1041 * 2 ) Certificate Mode ( ephemeral )
1042 * 3 ) Plain PSK Mode ( psk )
Jerry Yu77f01482022-07-11 07:03:24 +00001043 */
1044
Xiaokang Qian9f1747b2023-03-30 02:50:04 +00001045 ssl->handshake->key_exchange_mode =
1046 MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_NONE;
Jerry Yu77f01482022-07-11 07:03:24 +00001047
Gilles Peskine449bd832023-01-11 14:50:10 +01001048 if (ssl_tls13_check_psk_ephemeral_key_exchange(ssl)) {
Jerry Yu77f01482022-07-11 07:03:24 +00001049 ssl->handshake->key_exchange_mode =
1050 MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK_EPHEMERAL;
Gilles Peskine449bd832023-01-11 14:50:10 +01001051 MBEDTLS_SSL_DEBUG_MSG(2, ("key exchange mode: psk_ephemeral"));
1052 } else
1053 if (ssl_tls13_check_ephemeral_key_exchange(ssl)) {
Jerry Yu77f01482022-07-11 07:03:24 +00001054 ssl->handshake->key_exchange_mode =
1055 MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL;
Gilles Peskine449bd832023-01-11 14:50:10 +01001056 MBEDTLS_SSL_DEBUG_MSG(2, ("key exchange mode: ephemeral"));
1057 } else
1058 if (ssl_tls13_check_psk_key_exchange(ssl)) {
Jerry Yuce6ed702022-07-22 21:49:53 +08001059 ssl->handshake->key_exchange_mode =
1060 MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK;
Gilles Peskine449bd832023-01-11 14:50:10 +01001061 MBEDTLS_SSL_DEBUG_MSG(2, ("key exchange mode: psk"));
1062 } else {
Jerry Yu77f01482022-07-11 07:03:24 +00001063 MBEDTLS_SSL_DEBUG_MSG(
Gilles Peskine449bd832023-01-11 14:50:10 +01001064 1,
1065 ("ClientHello message misses mandatory extensions."));
1066 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_MISSING_EXTENSION,
1067 MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER);
1068 return MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER;
Jerry Yu77f01482022-07-11 07:03:24 +00001069 }
1070
Gilles Peskine449bd832023-01-11 14:50:10 +01001071 return 0;
Jerry Yu77f01482022-07-11 07:03:24 +00001072
XiaokangQian7807f9f2022-02-15 10:04:37 +00001073}
1074
XiaokangQian81802f42022-06-10 13:25:22 +00001075#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
Ronald Cron928cbd32022-10-04 16:14:26 +02001076 defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED)
Ronald Cron67ea2542022-09-15 17:34:42 +02001077
1078#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01001079static psa_algorithm_t ssl_tls13_iana_sig_alg_to_psa_alg(uint16_t sig_alg)
Ronald Cron67ea2542022-09-15 17:34:42 +02001080{
Gilles Peskine449bd832023-01-11 14:50:10 +01001081 switch (sig_alg) {
Ronald Cron67ea2542022-09-15 17:34:42 +02001082 case MBEDTLS_TLS1_3_SIG_ECDSA_SECP256R1_SHA256:
Gilles Peskine449bd832023-01-11 14:50:10 +01001083 return PSA_ALG_ECDSA(PSA_ALG_SHA_256);
Ronald Cron67ea2542022-09-15 17:34:42 +02001084 case MBEDTLS_TLS1_3_SIG_ECDSA_SECP384R1_SHA384:
Gilles Peskine449bd832023-01-11 14:50:10 +01001085 return PSA_ALG_ECDSA(PSA_ALG_SHA_384);
Ronald Cron67ea2542022-09-15 17:34:42 +02001086 case MBEDTLS_TLS1_3_SIG_ECDSA_SECP521R1_SHA512:
Gilles Peskine449bd832023-01-11 14:50:10 +01001087 return PSA_ALG_ECDSA(PSA_ALG_SHA_512);
Ronald Cron67ea2542022-09-15 17:34:42 +02001088 case MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA256:
Gilles Peskine449bd832023-01-11 14:50:10 +01001089 return PSA_ALG_RSA_PSS(PSA_ALG_SHA_256);
Ronald Cron67ea2542022-09-15 17:34:42 +02001090 case MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA384:
Gilles Peskine449bd832023-01-11 14:50:10 +01001091 return PSA_ALG_RSA_PSS(PSA_ALG_SHA_384);
Ronald Cron67ea2542022-09-15 17:34:42 +02001092 case MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA512:
Gilles Peskine449bd832023-01-11 14:50:10 +01001093 return PSA_ALG_RSA_PSS(PSA_ALG_SHA_512);
Ronald Cron67ea2542022-09-15 17:34:42 +02001094 case MBEDTLS_TLS1_3_SIG_RSA_PKCS1_SHA256:
Gilles Peskine449bd832023-01-11 14:50:10 +01001095 return PSA_ALG_RSA_PKCS1V15_SIGN(PSA_ALG_SHA_256);
Ronald Cron67ea2542022-09-15 17:34:42 +02001096 case MBEDTLS_TLS1_3_SIG_RSA_PKCS1_SHA384:
Gilles Peskine449bd832023-01-11 14:50:10 +01001097 return PSA_ALG_RSA_PKCS1V15_SIGN(PSA_ALG_SHA_384);
Ronald Cron67ea2542022-09-15 17:34:42 +02001098 case MBEDTLS_TLS1_3_SIG_RSA_PKCS1_SHA512:
Gilles Peskine449bd832023-01-11 14:50:10 +01001099 return PSA_ALG_RSA_PKCS1V15_SIGN(PSA_ALG_SHA_512);
Ronald Cron67ea2542022-09-15 17:34:42 +02001100 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01001101 return PSA_ALG_NONE;
Ronald Cron67ea2542022-09-15 17:34:42 +02001102 }
1103}
1104#endif /* MBEDTLS_USE_PSA_CRYPTO */
1105
XiaokangQian23c5be62022-06-07 02:04:34 +00001106/*
XiaokangQianfb665a82022-06-15 03:57:21 +00001107 * Pick best ( private key, certificate chain ) pair based on the signature
1108 * algorithms supported by the client.
XiaokangQian23c5be62022-06-07 02:04:34 +00001109 */
Ronald Cronce7d76e2022-07-08 18:56:49 +02001110MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01001111static int ssl_tls13_pick_key_cert(mbedtls_ssl_context *ssl)
XiaokangQian23c5be62022-06-07 02:04:34 +00001112{
XiaokangQianfb665a82022-06-15 03:57:21 +00001113 mbedtls_ssl_key_cert *key_cert, *key_cert_list;
XiaokangQian81802f42022-06-10 13:25:22 +00001114 const uint16_t *sig_alg = ssl->handshake->received_sig_algs;
XiaokangQian23c5be62022-06-07 02:04:34 +00001115
1116#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Gilles Peskine449bd832023-01-11 14:50:10 +01001117 if (ssl->handshake->sni_key_cert != NULL) {
XiaokangQianfb665a82022-06-15 03:57:21 +00001118 key_cert_list = ssl->handshake->sni_key_cert;
Gilles Peskine449bd832023-01-11 14:50:10 +01001119 } else
XiaokangQian23c5be62022-06-07 02:04:34 +00001120#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
Gilles Peskine449bd832023-01-11 14:50:10 +01001121 key_cert_list = ssl->conf->key_cert;
XiaokangQian23c5be62022-06-07 02:04:34 +00001122
Gilles Peskine449bd832023-01-11 14:50:10 +01001123 if (key_cert_list == NULL) {
1124 MBEDTLS_SSL_DEBUG_MSG(3, ("server has no certificate"));
1125 return -1;
XiaokangQian23c5be62022-06-07 02:04:34 +00001126 }
1127
Gilles Peskine449bd832023-01-11 14:50:10 +01001128 for (; *sig_alg != MBEDTLS_TLS1_3_SIG_NONE; sig_alg++) {
1129 if (!mbedtls_ssl_sig_alg_is_offered(ssl, *sig_alg)) {
Ronald Cron67ea2542022-09-15 17:34:42 +02001130 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +01001131 }
Ronald Cron67ea2542022-09-15 17:34:42 +02001132
Gilles Peskine449bd832023-01-11 14:50:10 +01001133 if (!mbedtls_ssl_tls13_sig_alg_for_cert_verify_is_supported(*sig_alg)) {
Ronald Cron67ea2542022-09-15 17:34:42 +02001134 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +01001135 }
Ronald Cron67ea2542022-09-15 17:34:42 +02001136
Gilles Peskine449bd832023-01-11 14:50:10 +01001137 for (key_cert = key_cert_list; key_cert != NULL;
1138 key_cert = key_cert->next) {
Ronald Cron67ea2542022-09-15 17:34:42 +02001139#if defined(MBEDTLS_USE_PSA_CRYPTO)
1140 psa_algorithm_t psa_alg = PSA_ALG_NONE;
1141#endif /* MBEDTLS_USE_PSA_CRYPTO */
1142
Gilles Peskine449bd832023-01-11 14:50:10 +01001143 MBEDTLS_SSL_DEBUG_CRT(3, "certificate (chain) candidate",
1144 key_cert->cert);
XiaokangQian81802f42022-06-10 13:25:22 +00001145
1146 /*
Gilles Peskine449bd832023-01-11 14:50:10 +01001147 * This avoids sending the client a cert it'll reject based on
1148 * keyUsage or other extensions.
1149 */
1150 if (mbedtls_x509_crt_check_key_usage(
1151 key_cert->cert, MBEDTLS_X509_KU_DIGITAL_SIGNATURE) != 0 ||
XiaokangQian81802f42022-06-10 13:25:22 +00001152 mbedtls_x509_crt_check_extended_key_usage(
XiaokangQianfb665a82022-06-15 03:57:21 +00001153 key_cert->cert, MBEDTLS_OID_SERVER_AUTH,
Gilles Peskine449bd832023-01-11 14:50:10 +01001154 MBEDTLS_OID_SIZE(MBEDTLS_OID_SERVER_AUTH)) != 0) {
1155 MBEDTLS_SSL_DEBUG_MSG(3, ("certificate mismatch: "
1156 "(extended) key usage extension"));
XiaokangQian81802f42022-06-10 13:25:22 +00001157 continue;
1158 }
XiaokangQian23c5be62022-06-07 02:04:34 +00001159
Gilles Peskine449bd832023-01-11 14:50:10 +01001160 MBEDTLS_SSL_DEBUG_MSG(3,
1161 ("ssl_tls13_pick_key_cert:"
1162 "check signature algorithm %s [%04x]",
1163 mbedtls_ssl_sig_alg_to_str(*sig_alg),
1164 *sig_alg));
Ronald Cron67ea2542022-09-15 17:34:42 +02001165#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01001166 psa_alg = ssl_tls13_iana_sig_alg_to_psa_alg(*sig_alg);
Ronald Cron67ea2542022-09-15 17:34:42 +02001167#endif /* MBEDTLS_USE_PSA_CRYPTO */
1168
Gilles Peskine449bd832023-01-11 14:50:10 +01001169 if (mbedtls_ssl_tls13_check_sig_alg_cert_key_match(
1170 *sig_alg, &key_cert->cert->pk)
Ronald Cron67ea2542022-09-15 17:34:42 +02001171#if defined(MBEDTLS_USE_PSA_CRYPTO)
1172 && psa_alg != PSA_ALG_NONE &&
Gilles Peskine449bd832023-01-11 14:50:10 +01001173 mbedtls_pk_can_do_ext(&key_cert->cert->pk, psa_alg,
1174 PSA_KEY_USAGE_SIGN_HASH) == 1
Ronald Cron67ea2542022-09-15 17:34:42 +02001175#endif /* MBEDTLS_USE_PSA_CRYPTO */
Gilles Peskine449bd832023-01-11 14:50:10 +01001176 ) {
XiaokangQianfb665a82022-06-15 03:57:21 +00001177 ssl->handshake->key_cert = key_cert;
Gilles Peskine449bd832023-01-11 14:50:10 +01001178 MBEDTLS_SSL_DEBUG_MSG(3,
1179 ("ssl_tls13_pick_key_cert:"
1180 "selected signature algorithm"
1181 " %s [%04x]",
1182 mbedtls_ssl_sig_alg_to_str(*sig_alg),
1183 *sig_alg));
XiaokangQianfb665a82022-06-15 03:57:21 +00001184 MBEDTLS_SSL_DEBUG_CRT(
Gilles Peskine449bd832023-01-11 14:50:10 +01001185 3, "selected certificate (chain)",
1186 ssl->handshake->key_cert->cert);
1187 return 0;
XiaokangQian81802f42022-06-10 13:25:22 +00001188 }
XiaokangQian81802f42022-06-10 13:25:22 +00001189 }
XiaokangQian23c5be62022-06-07 02:04:34 +00001190 }
1191
Gilles Peskine449bd832023-01-11 14:50:10 +01001192 MBEDTLS_SSL_DEBUG_MSG(2, ("ssl_tls13_pick_key_cert:"
1193 "no suitable certificate found"));
1194 return -1;
XiaokangQian23c5be62022-06-07 02:04:34 +00001195}
XiaokangQian81802f42022-06-10 13:25:22 +00001196#endif /* MBEDTLS_X509_CRT_PARSE_C &&
Ronald Cron928cbd32022-10-04 16:14:26 +02001197 MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED */
XiaokangQian23c5be62022-06-07 02:04:34 +00001198
XiaokangQian4080a7f2022-04-11 09:55:18 +00001199/*
XiaokangQianed582dd2022-04-13 08:21:05 +00001200 *
1201 * STATE HANDLING: ClientHello
1202 *
XiaokangQianb67384d2022-04-19 00:02:38 +00001203 * There are three possible classes of outcomes when parsing the ClientHello:
XiaokangQianed582dd2022-04-13 08:21:05 +00001204 *
XiaokangQianb67384d2022-04-19 00:02:38 +00001205 * 1) The ClientHello was well-formed and matched the server's configuration.
XiaokangQianed582dd2022-04-13 08:21:05 +00001206 *
1207 * In this case, the server progresses to sending its ServerHello.
1208 *
XiaokangQianb67384d2022-04-19 00:02:38 +00001209 * 2) The ClientHello was well-formed but didn't match the server's
1210 * configuration.
XiaokangQianed582dd2022-04-13 08:21:05 +00001211 *
1212 * For example, the client might not have offered a key share which
1213 * the server supports, or the server might require a cookie.
1214 *
1215 * In this case, the server sends a HelloRetryRequest.
1216 *
XiaokangQianb67384d2022-04-19 00:02:38 +00001217 * 3) The ClientHello was ill-formed
XiaokangQianed582dd2022-04-13 08:21:05 +00001218 *
1219 * In this case, we abort the handshake.
1220 *
1221 */
1222
1223/*
XiaokangQian4080a7f2022-04-11 09:55:18 +00001224 * Structure of this message:
1225 *
XiaokangQiane8ff3502022-04-22 02:34:40 +00001226 * uint16 ProtocolVersion;
1227 * opaque Random[32];
1228 * uint8 CipherSuite[2]; // Cryptographic suite selector
XiaokangQian4080a7f2022-04-11 09:55:18 +00001229 *
XiaokangQiane8ff3502022-04-22 02:34:40 +00001230 * struct {
1231 * ProtocolVersion legacy_version = 0x0303; // TLS v1.2
1232 * Random random;
1233 * opaque legacy_session_id<0..32>;
1234 * CipherSuite cipher_suites<2..2^16-2>;
1235 * opaque legacy_compression_methods<1..2^8-1>;
1236 * Extension extensions<8..2^16-1>;
1237 * } ClientHello;
XiaokangQian4080a7f2022-04-11 09:55:18 +00001238 */
XiaokangQianed582dd2022-04-13 08:21:05 +00001239
1240#define SSL_CLIENT_HELLO_OK 0
1241#define SSL_CLIENT_HELLO_HRR_REQUIRED 1
Ronald Cron5af4c7f2023-03-07 20:46:59 +01001242#define SSL_CLIENT_HELLO_TLS1_2 2
XiaokangQianed582dd2022-04-13 08:21:05 +00001243
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001244MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01001245static int ssl_tls13_parse_client_hello(mbedtls_ssl_context *ssl,
1246 const unsigned char *buf,
1247 const unsigned char *end)
XiaokangQian7807f9f2022-02-15 10:04:37 +00001248{
XiaokangQianb67384d2022-04-19 00:02:38 +00001249 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1250 const unsigned char *p = buf;
Ronald Crond540d992023-03-07 09:41:48 +01001251 const unsigned char *random;
XiaokangQian4080a7f2022-04-11 09:55:18 +00001252 size_t legacy_session_id_len;
Ronald Croncada4102023-03-07 09:51:39 +01001253 const unsigned char *legacy_session_id;
XiaokangQian318dc762022-04-20 09:43:51 +00001254 size_t cipher_suites_len;
Ronald Cron2f16b4e2023-03-07 10:07:32 +01001255 const unsigned char *cipher_suites;
XiaokangQian060d8672022-04-21 09:24:56 +00001256 const unsigned char *cipher_suites_end;
XiaokangQianb67384d2022-04-19 00:02:38 +00001257 size_t extensions_len;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001258 const unsigned char *extensions_end;
Ronald Croneff56732023-04-03 17:36:31 +02001259 const unsigned char *supported_versions_data;
1260 const unsigned char *supported_versions_data_end;
Jerry Yuc4bf5d62022-10-29 09:08:47 +08001261 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
Jerry Yu49ca9282022-05-05 11:05:22 +08001262 int hrr_required = 0;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001263
Ronald Cron41a443a2022-10-04 16:38:25 +02001264#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED)
Jerry Yu29d9faa2022-08-23 17:52:45 +08001265 const unsigned char *pre_shared_key_ext = NULL;
Jerry Yu1c105562022-07-10 06:32:38 +00001266 const unsigned char *pre_shared_key_ext_end = NULL;
Ronald Cron41a443a2022-10-04 16:38:25 +02001267#endif
XiaokangQian7807f9f2022-02-15 10:04:37 +00001268
XiaokangQian7807f9f2022-02-15 10:04:37 +00001269 /*
XiaokangQianb67384d2022-04-19 00:02:38 +00001270 * ClientHello layout:
XiaokangQian7807f9f2022-02-15 10:04:37 +00001271 * 0 . 1 protocol version
XiaokangQiane8ff3502022-04-22 02:34:40 +00001272 * 2 . 33 random bytes
XiaokangQianc5763b52022-04-02 03:34:37 +00001273 * 34 . 34 session id length ( 1 byte )
XiaokangQian7807f9f2022-02-15 10:04:37 +00001274 * 35 . 34+x session id
XiaokangQian7807f9f2022-02-15 10:04:37 +00001275 * .. . .. ciphersuite list length ( 2 bytes )
1276 * .. . .. ciphersuite list
1277 * .. . .. compression alg. list length ( 1 byte )
1278 * .. . .. compression alg. list
1279 * .. . .. extensions length ( 2 bytes, optional )
1280 * .. . .. extensions ( optional )
1281 */
1282
XiaokangQianb67384d2022-04-19 00:02:38 +00001283 /*
bootstrap-prime6dbbf442022-05-17 19:30:44 -04001284 * Minimal length ( with everything empty and extensions omitted ) is
XiaokangQian7807f9f2022-02-15 10:04:37 +00001285 * 2 + 32 + 1 + 2 + 1 = 38 bytes. Check that first, so that we can
1286 * read at least up to session id length without worrying.
1287 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001288 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, 38);
XiaokangQian7807f9f2022-02-15 10:04:37 +00001289
1290 /* ...
1291 * ProtocolVersion legacy_version = 0x0303; // TLS 1.2
1292 * ...
1293 * with ProtocolVersion defined as:
1294 * uint16 ProtocolVersion;
1295 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001296 if (mbedtls_ssl_read_version(p, ssl->conf->transport) !=
1297 MBEDTLS_SSL_VERSION_TLS1_2) {
1298 MBEDTLS_SSL_DEBUG_MSG(1, ("Unsupported version of TLS."));
1299 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_PROTOCOL_VERSION,
1300 MBEDTLS_ERR_SSL_BAD_PROTOCOL_VERSION);
1301 return MBEDTLS_ERR_SSL_BAD_PROTOCOL_VERSION;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001302 }
1303 p += 2;
1304
Jerry Yuc1be19f2022-04-23 16:11:39 +08001305 /* ...
1306 * Random random;
1307 * ...
XiaokangQianb67384d2022-04-19 00:02:38 +00001308 * with Random defined as:
1309 * opaque Random[32];
XiaokangQian7807f9f2022-02-15 10:04:37 +00001310 */
Ronald Crond540d992023-03-07 09:41:48 +01001311 random = p;
XiaokangQian08037552022-04-20 07:16:41 +00001312 p += MBEDTLS_CLIENT_HELLO_RANDOM_LEN;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001313
Jerry Yuc1be19f2022-04-23 16:11:39 +08001314 /* ...
XiaokangQianb67384d2022-04-19 00:02:38 +00001315 * opaque legacy_session_id<0..32>;
Jerry Yuc1be19f2022-04-23 16:11:39 +08001316 * ...
XiaokangQian7807f9f2022-02-15 10:04:37 +00001317 */
Ronald Croncada4102023-03-07 09:51:39 +01001318 legacy_session_id_len = *(p++);
1319 legacy_session_id = p;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001320
XiaokangQianb67384d2022-04-19 00:02:38 +00001321 /*
1322 * Check we have enough data for the legacy session identifier
Jerry Yue95c8af2022-07-26 15:48:20 +08001323 * and the ciphersuite list length.
XiaokangQianb67384d2022-04-19 00:02:38 +00001324 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001325 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, legacy_session_id_len + 2);
XiaokangQian4080a7f2022-04-11 09:55:18 +00001326 p += legacy_session_id_len;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001327
Ronald Cron2f16b4e2023-03-07 10:07:32 +01001328 /* ...
1329 * CipherSuite cipher_suites<2..2^16-2>;
1330 * ...
1331 * with CipherSuite defined as:
1332 * uint8 CipherSuite[2];
1333 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001334 cipher_suites_len = MBEDTLS_GET_UINT16_BE(p, 0);
XiaokangQian7807f9f2022-02-15 10:04:37 +00001335 p += 2;
Ronald Cron2f16b4e2023-03-07 10:07:32 +01001336 cipher_suites = p;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001337
Ronald Cronfc7ae872023-02-16 15:32:19 +01001338 /*
1339 * The length of the ciphersuite list has to be even.
1340 */
1341 if (cipher_suites_len & 1) {
1342 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR,
1343 MBEDTLS_ERR_SSL_DECODE_ERROR);
1344 return MBEDTLS_ERR_SSL_DECODE_ERROR;
1345 }
1346
XiaokangQianb67384d2022-04-19 00:02:38 +00001347 /* Check we have enough data for the ciphersuite list, the legacy
1348 * compression methods and the length of the extensions.
Jerry Yue95c8af2022-07-26 15:48:20 +08001349 *
1350 * cipher_suites cipher_suites_len bytes
1351 * legacy_compression_methods 2 bytes
1352 * extensions_len 2 bytes
XiaokangQianb67384d2022-04-19 00:02:38 +00001353 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001354 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, cipher_suites_len + 2 + 2);
Ronald Cron8c527d02023-03-07 15:47:47 +01001355 p += cipher_suites_len;
1356 cipher_suites_end = p;
Jerry Yu5725f1c2022-08-21 17:27:16 +08001357
1358 /*
Ronald Cron8c527d02023-03-07 15:47:47 +01001359 * Search for the supported versions extension and parse it to determine
1360 * if the client supports TLS 1.3.
1361 */
1362 ret = mbedtls_ssl_tls13_is_supported_versions_ext_present_in_exts(
1363 ssl, p + 2, end,
Ronald Croneff56732023-04-03 17:36:31 +02001364 &supported_versions_data, &supported_versions_data_end);
Ronald Cron8c527d02023-03-07 15:47:47 +01001365 if (ret < 0) {
1366 MBEDTLS_SSL_DEBUG_RET(1,
1367 ("mbedtls_ssl_tls13_is_supported_versions_ext_present_in_exts"), ret);
1368 return ret;
1369 }
1370
1371 if (ret == 0) {
Ronald Cron5af4c7f2023-03-07 20:46:59 +01001372 return SSL_CLIENT_HELLO_TLS1_2;
Ronald Cron8c527d02023-03-07 15:47:47 +01001373 }
1374
Ronald Cron5af4c7f2023-03-07 20:46:59 +01001375 if (ret == 1) {
1376 ret = ssl_tls13_parse_supported_versions_ext(ssl,
Ronald Croneff56732023-04-03 17:36:31 +02001377 supported_versions_data,
1378 supported_versions_data_end);
Ronald Cron5af4c7f2023-03-07 20:46:59 +01001379 if (ret < 0) {
1380 MBEDTLS_SSL_DEBUG_RET(1,
1381 ("ssl_tls13_parse_supported_versions_ext"), ret);
1382 return ret;
1383 }
1384
Ronald Cronb828c7d2023-04-03 16:37:22 +02001385 /*
1386 * The supported versions extension was parsed successfully as the
1387 * value returned by ssl_tls13_parse_supported_versions_ext() is
1388 * positive. The return value is then equal to
1389 * MBEDTLS_SSL_VERSION_TLS1_2 or MBEDTLS_SSL_VERSION_TLS1_3, defining
1390 * the TLS version to negotiate.
1391 */
Ronald Cron5af4c7f2023-03-07 20:46:59 +01001392 if (MBEDTLS_SSL_VERSION_TLS1_2 == ret) {
1393 return SSL_CLIENT_HELLO_TLS1_2;
1394 }
Ronald Cron8c527d02023-03-07 15:47:47 +01001395 }
1396
1397 /*
1398 * We negotiate TLS 1.3.
Ronald Cron64582392023-03-07 09:21:40 +01001399 */
1400 ssl->tls_version = MBEDTLS_SSL_VERSION_TLS1_3;
1401
1402#if defined(MBEDTLS_SSL_SESSION_TICKETS)
1403 /* Store minor version for later use with ticket serialization. */
1404 ssl->session_negotiate->tls_version = MBEDTLS_SSL_VERSION_TLS1_3;
1405 ssl->session_negotiate->endpoint = ssl->conf->endpoint;
1406#endif
1407
1408 /*
Ronald Crondad02b22023-04-06 09:57:52 +02001409 * We are negotiating the version 1.3 of the protocol. Do what we have
Ronald Croncada4102023-03-07 09:51:39 +01001410 * postponed: copy of the client random bytes, copy of the legacy session
Ronald Cron2f16b4e2023-03-07 10:07:32 +01001411 * identifier and selection of the TLS 1.3 cipher suite.
Ronald Crond540d992023-03-07 09:41:48 +01001412 */
1413 MBEDTLS_SSL_DEBUG_BUF(3, "client hello, random bytes",
1414 random, MBEDTLS_CLIENT_HELLO_RANDOM_LEN);
1415 memcpy(&handshake->randbytes[0], random, MBEDTLS_CLIENT_HELLO_RANDOM_LEN);
1416
Ronald Croncada4102023-03-07 09:51:39 +01001417 if (legacy_session_id_len > sizeof(ssl->session_negotiate->id)) {
1418 MBEDTLS_SSL_DEBUG_MSG(1, ("bad client hello message"));
1419 return MBEDTLS_ERR_SSL_DECODE_ERROR;
1420 }
1421 ssl->session_negotiate->id_len = legacy_session_id_len;
1422 MBEDTLS_SSL_DEBUG_BUF(3, "client hello, session id",
1423 legacy_session_id, legacy_session_id_len);
1424 memcpy(&ssl->session_negotiate->id[0],
1425 legacy_session_id, legacy_session_id_len);
1426
Ronald Crond540d992023-03-07 09:41:48 +01001427 /*
Jerry Yu5725f1c2022-08-21 17:27:16 +08001428 * Search for a matching ciphersuite
1429 */
Ronald Cron2f16b4e2023-03-07 10:07:32 +01001430 MBEDTLS_SSL_DEBUG_BUF(3, "client hello, list of cipher suites",
1431 cipher_suites, cipher_suites_len);
Ronald Crone45afd72023-04-04 15:10:06 +02001432 for (const unsigned char *cipher_suites_p = cipher_suites;
1433 cipher_suites_p < cipher_suites_end; cipher_suites_p += 2) {
XiaokangQiane8ff3502022-04-22 02:34:40 +00001434 uint16_t cipher_suite;
Gilles Peskine449bd832023-01-11 14:50:10 +01001435 const mbedtls_ssl_ciphersuite_t *ciphersuite_info;
Jerry Yu5725f1c2022-08-21 17:27:16 +08001436
Ronald Crond89360b2023-02-21 08:53:33 +01001437 /*
Ronald Crone45afd72023-04-04 15:10:06 +02001438 * "cipher_suites_end - cipher_suites_p is even" is an invariant of the
1439 * loop. As cipher_suites_end - cipher_suites_p > 0, we have
1440 * cipher_suites_end - cipher_suites_p >= 2 and it is thus safe to read
1441 * two bytes.
Ronald Crond89360b2023-02-21 08:53:33 +01001442 */
Ronald Crone45afd72023-04-04 15:10:06 +02001443 cipher_suite = MBEDTLS_GET_UINT16_BE(cipher_suites_p, 0);
Jerry Yuc5a23a02022-08-25 10:51:44 +08001444 ciphersuite_info = ssl_tls13_validate_peer_ciphersuite(
Gilles Peskine449bd832023-01-11 14:50:10 +01001445 ssl, cipher_suite);
1446 if (ciphersuite_info == NULL) {
Jerry Yu5725f1c2022-08-21 17:27:16 +08001447 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +01001448 }
Jerry Yu5725f1c2022-08-21 17:27:16 +08001449
Jerry Yu5725f1c2022-08-21 17:27:16 +08001450 ssl->session_negotiate->ciphersuite = cipher_suite;
Jerry Yuc4bf5d62022-10-29 09:08:47 +08001451 handshake->ciphersuite_info = ciphersuite_info;
Gilles Peskine449bd832023-01-11 14:50:10 +01001452 MBEDTLS_SSL_DEBUG_MSG(2, ("selected ciphersuite: %04x - %s",
1453 cipher_suite,
1454 ciphersuite_info->name));
Ronald Cron25e9ec62023-02-16 15:35:16 +01001455 break;
XiaokangQian17f974c2022-04-19 09:57:41 +00001456 }
Jerry Yu5725f1c2022-08-21 17:27:16 +08001457
Gilles Peskine449bd832023-01-11 14:50:10 +01001458 if (handshake->ciphersuite_info == NULL) {
1459 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE,
1460 MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE);
1461 return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
Jerry Yu5725f1c2022-08-21 17:27:16 +08001462 }
Jerry Yuc1be19f2022-04-23 16:11:39 +08001463
XiaokangQian4080a7f2022-04-11 09:55:18 +00001464 /* ...
XiaokangQianb67384d2022-04-19 00:02:38 +00001465 * opaque legacy_compression_methods<1..2^8-1>;
XiaokangQian4080a7f2022-04-11 09:55:18 +00001466 * ...
XiaokangQian7807f9f2022-02-15 10:04:37 +00001467 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001468 if (p[0] != 1 || p[1] != MBEDTLS_SSL_COMPRESS_NULL) {
1469 MBEDTLS_SSL_DEBUG_MSG(1, ("bad legacy compression method"));
1470 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER,
1471 MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER);
1472 return MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001473 }
XiaokangQianb67384d2022-04-19 00:02:38 +00001474 p += 2;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001475
Jerry Yuc1be19f2022-04-23 16:11:39 +08001476 /* ...
XiaokangQianb67384d2022-04-19 00:02:38 +00001477 * Extension extensions<8..2^16-1>;
Jerry Yuc1be19f2022-04-23 16:11:39 +08001478 * ...
XiaokangQianb67384d2022-04-19 00:02:38 +00001479 * with Extension defined as:
1480 * struct {
1481 * ExtensionType extension_type;
1482 * opaque extension_data<0..2^16-1>;
1483 * } Extension;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001484 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001485 extensions_len = MBEDTLS_GET_UINT16_BE(p, 0);
XiaokangQian7807f9f2022-02-15 10:04:37 +00001486 p += 2;
Gilles Peskine449bd832023-01-11 14:50:10 +01001487 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, extensions_len);
XiaokangQiane8ff3502022-04-22 02:34:40 +00001488 extensions_end = p + extensions_len;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001489
Gilles Peskine449bd832023-01-11 14:50:10 +01001490 MBEDTLS_SSL_DEBUG_BUF(3, "client hello extensions", p, extensions_len);
Jerry Yu63a459c2022-10-31 13:38:40 +08001491 handshake->received_extensions = MBEDTLS_SSL_EXT_MASK_NONE;
Jerry Yu0c354a22022-08-29 15:25:36 +08001492
Gilles Peskine449bd832023-01-11 14:50:10 +01001493 while (p < extensions_end) {
XiaokangQian7807f9f2022-02-15 10:04:37 +00001494 unsigned int extension_type;
1495 size_t extension_data_len;
1496 const unsigned char *extension_data_end;
1497
Jerry Yu0c354a22022-08-29 15:25:36 +08001498 /* RFC 8446, section 4.2.11
Jerry Yu1c9247c2022-07-21 12:37:39 +08001499 *
1500 * The "pre_shared_key" extension MUST be the last extension in the
1501 * ClientHello (this facilitates implementation as described below).
1502 * Servers MUST check that it is the last extension and otherwise fail
1503 * the handshake with an "illegal_parameter" alert.
1504 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001505 if (handshake->received_extensions & MBEDTLS_SSL_EXT_MASK(PRE_SHARED_KEY)) {
Jerry Yu1c9247c2022-07-21 12:37:39 +08001506 MBEDTLS_SSL_DEBUG_MSG(
Gilles Peskine449bd832023-01-11 14:50:10 +01001507 3, ("pre_shared_key is not last extension."));
Jerry Yu1c9247c2022-07-21 12:37:39 +08001508 MBEDTLS_SSL_PEND_FATAL_ALERT(
1509 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER,
Gilles Peskine449bd832023-01-11 14:50:10 +01001510 MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER);
1511 return MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER;
Jerry Yu1c9247c2022-07-21 12:37:39 +08001512 }
1513
Gilles Peskine449bd832023-01-11 14:50:10 +01001514 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, extensions_end, 4);
1515 extension_type = MBEDTLS_GET_UINT16_BE(p, 0);
1516 extension_data_len = MBEDTLS_GET_UINT16_BE(p, 2);
XiaokangQian7807f9f2022-02-15 10:04:37 +00001517 p += 4;
1518
Gilles Peskine449bd832023-01-11 14:50:10 +01001519 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, extensions_end, extension_data_len);
XiaokangQian7807f9f2022-02-15 10:04:37 +00001520 extension_data_end = p + extension_data_len;
1521
Jerry Yuc4bf5d62022-10-29 09:08:47 +08001522 ret = mbedtls_ssl_tls13_check_received_extension(
Gilles Peskine449bd832023-01-11 14:50:10 +01001523 ssl, MBEDTLS_SSL_HS_CLIENT_HELLO, extension_type,
1524 MBEDTLS_SSL_TLS1_3_ALLOWED_EXTS_OF_CH);
1525 if (ret != 0) {
1526 return ret;
1527 }
Jerry Yue18dc7e2022-08-04 16:29:22 +08001528
Gilles Peskine449bd832023-01-11 14:50:10 +01001529 switch (extension_type) {
XiaokangQian40a35232022-05-07 09:02:40 +00001530#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
1531 case MBEDTLS_TLS_EXT_SERVERNAME:
Gilles Peskine449bd832023-01-11 14:50:10 +01001532 MBEDTLS_SSL_DEBUG_MSG(3, ("found ServerName extension"));
1533 ret = mbedtls_ssl_parse_server_name_ext(ssl, p,
1534 extension_data_end);
1535 if (ret != 0) {
XiaokangQian40a35232022-05-07 09:02:40 +00001536 MBEDTLS_SSL_DEBUG_RET(
Gilles Peskine449bd832023-01-11 14:50:10 +01001537 1, "mbedtls_ssl_parse_servername_ext", ret);
1538 return ret;
XiaokangQian40a35232022-05-07 09:02:40 +00001539 }
XiaokangQian40a35232022-05-07 09:02:40 +00001540 break;
1541#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
1542
Valerio Setti080a22b2023-03-20 15:22:47 +01001543#if defined(PSA_WANT_ALG_ECDH)
XiaokangQian7807f9f2022-02-15 10:04:37 +00001544 case MBEDTLS_TLS_EXT_SUPPORTED_GROUPS:
Gilles Peskine449bd832023-01-11 14:50:10 +01001545 MBEDTLS_SSL_DEBUG_MSG(3, ("found supported group extension"));
XiaokangQian7807f9f2022-02-15 10:04:37 +00001546
1547 /* Supported Groups Extension
1548 *
1549 * When sent by the client, the "supported_groups" extension
1550 * indicates the named groups which the client supports,
1551 * ordered from most preferred to least preferred.
1552 */
Jerry Yuc1be19f2022-04-23 16:11:39 +08001553 ret = ssl_tls13_parse_supported_groups_ext(
Gilles Peskine449bd832023-01-11 14:50:10 +01001554 ssl, p, extension_data_end);
1555 if (ret != 0) {
Xiaokang Qian9f1747b2023-03-30 02:50:04 +00001556 MBEDTLS_SSL_DEBUG_RET(
Xiaokang Qian8bce0e62023-04-04 10:15:48 +00001557 1, "ssl_tls13_parse_supported_groups_ext", ret);
Gilles Peskine449bd832023-01-11 14:50:10 +01001558 return ret;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001559 }
1560
XiaokangQian7807f9f2022-02-15 10:04:37 +00001561 break;
Valerio Setti080a22b2023-03-20 15:22:47 +01001562#endif /* PSA_WANT_ALG_ECDH */
XiaokangQian7807f9f2022-02-15 10:04:37 +00001563
Valerio Setti080a22b2023-03-20 15:22:47 +01001564#if defined(PSA_WANT_ALG_ECDH)
XiaokangQian7807f9f2022-02-15 10:04:37 +00001565 case MBEDTLS_TLS_EXT_KEY_SHARE:
Gilles Peskine449bd832023-01-11 14:50:10 +01001566 MBEDTLS_SSL_DEBUG_MSG(3, ("found key share extension"));
XiaokangQian7807f9f2022-02-15 10:04:37 +00001567
1568 /*
1569 * Key Share Extension
1570 *
1571 * When sent by the client, the "key_share" extension
1572 * contains the endpoint's cryptographic parameters for
1573 * ECDHE/DHE key establishment methods.
1574 */
Jerry Yuc1be19f2022-04-23 16:11:39 +08001575 ret = ssl_tls13_parse_key_shares_ext(
Gilles Peskine449bd832023-01-11 14:50:10 +01001576 ssl, p, extension_data_end);
1577 if (ret == SSL_TLS1_3_PARSE_KEY_SHARES_EXT_NO_MATCH) {
1578 MBEDTLS_SSL_DEBUG_MSG(2, ("HRR needed "));
Jerry Yu49ca9282022-05-05 11:05:22 +08001579 hrr_required = 1;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001580 }
1581
Gilles Peskine449bd832023-01-11 14:50:10 +01001582 if (ret < 0) {
Jerry Yu582dd062022-04-22 21:59:01 +08001583 MBEDTLS_SSL_DEBUG_RET(
Gilles Peskine449bd832023-01-11 14:50:10 +01001584 1, "ssl_tls13_parse_key_shares_ext", ret);
1585 return ret;
Jerry Yu582dd062022-04-22 21:59:01 +08001586 }
XiaokangQian7807f9f2022-02-15 10:04:37 +00001587
XiaokangQian7807f9f2022-02-15 10:04:37 +00001588 break;
Valerio Setti080a22b2023-03-20 15:22:47 +01001589#endif /* PSA_WANT_ALG_ECDH */
XiaokangQian7807f9f2022-02-15 10:04:37 +00001590
1591 case MBEDTLS_TLS_EXT_SUPPORTED_VERSIONS:
Ronald Cron8c527d02023-03-07 15:47:47 +01001592 /* Already parsed */
XiaokangQian7807f9f2022-02-15 10:04:37 +00001593 break;
1594
Ronald Cron41a443a2022-10-04 16:38:25 +02001595#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED)
Jerry Yue19e3b92022-07-08 12:04:51 +00001596 case MBEDTLS_TLS_EXT_PSK_KEY_EXCHANGE_MODES:
Xiaokang Qian9f1747b2023-03-30 02:50:04 +00001597 MBEDTLS_SSL_DEBUG_MSG(
1598 3, ("found psk key exchange modes extension"));
Jerry Yue19e3b92022-07-08 12:04:51 +00001599
1600 ret = ssl_tls13_parse_key_exchange_modes_ext(
Gilles Peskine449bd832023-01-11 14:50:10 +01001601 ssl, p, extension_data_end);
1602 if (ret != 0) {
Jerry Yue19e3b92022-07-08 12:04:51 +00001603 MBEDTLS_SSL_DEBUG_RET(
Gilles Peskine449bd832023-01-11 14:50:10 +01001604 1, "ssl_tls13_parse_key_exchange_modes_ext", ret);
1605 return ret;
Jerry Yue19e3b92022-07-08 12:04:51 +00001606 }
1607
Jerry Yue19e3b92022-07-08 12:04:51 +00001608 break;
Ronald Cron41a443a2022-10-04 16:38:25 +02001609#endif
Jerry Yue19e3b92022-07-08 12:04:51 +00001610
Jerry Yu1c105562022-07-10 06:32:38 +00001611 case MBEDTLS_TLS_EXT_PRE_SHARED_KEY:
Gilles Peskine449bd832023-01-11 14:50:10 +01001612 MBEDTLS_SSL_DEBUG_MSG(3, ("found pre_shared_key extension"));
1613 if ((handshake->received_extensions &
1614 MBEDTLS_SSL_EXT_MASK(PSK_KEY_EXCHANGE_MODES)) == 0) {
Jerry Yu13ab81d2022-07-22 23:17:11 +08001615 MBEDTLS_SSL_PEND_FATAL_ALERT(
1616 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER,
Gilles Peskine449bd832023-01-11 14:50:10 +01001617 MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER);
1618 return MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER;
Jerry Yu13ab81d2022-07-22 23:17:11 +08001619 }
Ronald Cron41a443a2022-10-04 16:38:25 +02001620#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED)
Jerry Yu1c105562022-07-10 06:32:38 +00001621 /* Delay processing of the PSK identity once we have
1622 * found out which algorithms to use. We keep a pointer
1623 * to the buffer and the size for later processing.
1624 */
Jerry Yu29d9faa2022-08-23 17:52:45 +08001625 pre_shared_key_ext = p;
Jerry Yu1c105562022-07-10 06:32:38 +00001626 pre_shared_key_ext_end = extension_data_end;
Jerry Yue18dc7e2022-08-04 16:29:22 +08001627#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED */
Jerry Yu1c105562022-07-10 06:32:38 +00001628 break;
Jerry Yu1c105562022-07-10 06:32:38 +00001629
XiaokangQianacb39922022-06-17 10:18:48 +00001630#if defined(MBEDTLS_SSL_ALPN)
1631 case MBEDTLS_TLS_EXT_ALPN:
Gilles Peskine449bd832023-01-11 14:50:10 +01001632 MBEDTLS_SSL_DEBUG_MSG(3, ("found alpn extension"));
XiaokangQianacb39922022-06-17 10:18:48 +00001633
Gilles Peskine449bd832023-01-11 14:50:10 +01001634 ret = mbedtls_ssl_parse_alpn_ext(ssl, p, extension_data_end);
1635 if (ret != 0) {
XiaokangQianacb39922022-06-17 10:18:48 +00001636 MBEDTLS_SSL_DEBUG_RET(
Gilles Peskine449bd832023-01-11 14:50:10 +01001637 1, ("mbedtls_ssl_parse_alpn_ext"), ret);
1638 return ret;
XiaokangQianacb39922022-06-17 10:18:48 +00001639 }
XiaokangQianacb39922022-06-17 10:18:48 +00001640 break;
1641#endif /* MBEDTLS_SSL_ALPN */
1642
Ronald Cron928cbd32022-10-04 16:14:26 +02001643#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED)
XiaokangQian7807f9f2022-02-15 10:04:37 +00001644 case MBEDTLS_TLS_EXT_SIG_ALG:
Gilles Peskine449bd832023-01-11 14:50:10 +01001645 MBEDTLS_SSL_DEBUG_MSG(3, ("found signature_algorithms extension"));
XiaokangQian7807f9f2022-02-15 10:04:37 +00001646
Gabor Mezei078e8032022-04-27 21:17:56 +02001647 ret = mbedtls_ssl_parse_sig_alg_ext(
Gilles Peskine449bd832023-01-11 14:50:10 +01001648 ssl, p, extension_data_end);
1649 if (ret != 0) {
Xiaokang Qian49f39c12023-04-06 02:31:02 +00001650 MBEDTLS_SSL_DEBUG_RET(
1651 1, "mbedtls_ssl_parse_sig_alg_ext", ret);
Gilles Peskine449bd832023-01-11 14:50:10 +01001652 return ret;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001653 }
XiaokangQian7807f9f2022-02-15 10:04:37 +00001654 break;
Ronald Cron928cbd32022-10-04 16:14:26 +02001655#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED */
XiaokangQian7807f9f2022-02-15 10:04:37 +00001656
Jan Bruckner151f6422023-02-10 12:45:19 +01001657#if defined(MBEDTLS_SSL_RECORD_SIZE_LIMIT)
1658 case MBEDTLS_TLS_EXT_RECORD_SIZE_LIMIT:
1659 MBEDTLS_SSL_DEBUG_MSG(3, ("found record_size_limit extension"));
1660
Xiaokang Qian9f1747b2023-03-30 02:50:04 +00001661 ret = mbedtls_ssl_tls13_parse_record_size_limit_ext(
1662 ssl, p, extension_data_end);
Jan Bruckner151f6422023-02-10 12:45:19 +01001663
Xiaokang Qian09c3ccc2023-04-04 10:18:38 +00001664 /*
1665 * TODO: Return unconditionally here until we handle the record
1666 * size limit correctly.
1667 * Once handled correctly, only return in case of errors.
1668 */
Jan Bruckner151f6422023-02-10 12:45:19 +01001669 return ret;
1670
1671 break;
1672#endif /* MBEDTLS_SSL_RECORD_SIZE_LIMIT */
1673
XiaokangQian7807f9f2022-02-15 10:04:37 +00001674 default:
Jerry Yu79aa7212022-11-08 21:30:21 +08001675 MBEDTLS_SSL_PRINT_EXT(
Jerry Yu63a459c2022-10-31 13:38:40 +08001676 3, MBEDTLS_SSL_HS_CLIENT_HELLO,
Gilles Peskine449bd832023-01-11 14:50:10 +01001677 extension_type, "( ignored )");
Jerry Yu0c354a22022-08-29 15:25:36 +08001678 break;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001679 }
1680
1681 p += extension_data_len;
1682 }
1683
Gilles Peskine449bd832023-01-11 14:50:10 +01001684 MBEDTLS_SSL_PRINT_EXTS(3, MBEDTLS_SSL_HS_CLIENT_HELLO,
1685 handshake->received_extensions);
Jerry Yue18dc7e2022-08-04 16:29:22 +08001686
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +01001687 ret = mbedtls_ssl_add_hs_hdr_to_checksum(ssl,
1688 MBEDTLS_SSL_HS_CLIENT_HELLO,
1689 p - buf);
1690 if (0 != ret) {
1691 MBEDTLS_SSL_DEBUG_RET(1, ("mbedtls_ssl_add_hs_hdr_to_checksum"), ret);
1692 return ret;
1693 }
Jerry Yu032b15ce2022-07-11 06:10:03 +00001694
Ronald Cron41a443a2022-10-04 16:38:25 +02001695#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED)
XiaokangQian7807f9f2022-02-15 10:04:37 +00001696 /* Update checksum with either
1697 * - The entire content of the CH message, if no PSK extension is present
1698 * - The content up to but excluding the PSK extension, if present.
1699 */
Jerry Yu1c105562022-07-10 06:32:38 +00001700 /* If we've settled on a PSK-based exchange, parse PSK identity ext */
Gilles Peskine449bd832023-01-11 14:50:10 +01001701 if (mbedtls_ssl_tls13_some_psk_enabled(ssl) &&
1702 mbedtls_ssl_conf_tls13_some_psk_enabled(ssl) &&
1703 (handshake->received_extensions & MBEDTLS_SSL_EXT_MASK(PRE_SHARED_KEY))) {
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +01001704 ret = handshake->update_checksum(ssl, buf,
1705 pre_shared_key_ext - buf);
1706 if (0 != ret) {
1707 MBEDTLS_SSL_DEBUG_RET(1, ("update_checksum"), ret);
1708 return ret;
1709 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001710 ret = ssl_tls13_parse_pre_shared_key_ext(ssl,
1711 pre_shared_key_ext,
1712 pre_shared_key_ext_end,
1713 cipher_suites,
1714 cipher_suites_end);
1715 if (ret == MBEDTLS_ERR_SSL_UNKNOWN_IDENTITY) {
1716 handshake->received_extensions &= ~MBEDTLS_SSL_EXT_MASK(PRE_SHARED_KEY);
1717 } else if (ret != 0) {
Jerry Yu63a459c2022-10-31 13:38:40 +08001718 MBEDTLS_SSL_DEBUG_RET(
Gilles Peskine449bd832023-01-11 14:50:10 +01001719 1, "ssl_tls13_parse_pre_shared_key_ext", ret);
1720 return ret;
Jerry Yu1c105562022-07-10 06:32:38 +00001721 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001722 } else
Ronald Cron41a443a2022-10-04 16:38:25 +02001723#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED */
Jerry Yu1c105562022-07-10 06:32:38 +00001724 {
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +01001725 ret = handshake->update_checksum(ssl, buf, p - buf);
1726 if (0 != ret) {
1727 MBEDTLS_SSL_DEBUG_RET(1, ("update_checksum"), ret);
1728 return ret;
1729 }
Jerry Yu1c105562022-07-10 06:32:38 +00001730 }
XiaokangQian7807f9f2022-02-15 10:04:37 +00001731
Gilles Peskine449bd832023-01-11 14:50:10 +01001732 ret = ssl_tls13_determine_key_exchange_mode(ssl);
1733 if (ret < 0) {
1734 return ret;
1735 }
XiaokangQian7807f9f2022-02-15 10:04:37 +00001736
Gilles Peskine449bd832023-01-11 14:50:10 +01001737 mbedtls_ssl_optimize_checksum(ssl, handshake->ciphersuite_info);
Jerry Yue5834fd2022-08-29 20:16:09 +08001738
Gilles Peskine449bd832023-01-11 14:50:10 +01001739 return hrr_required ? SSL_CLIENT_HELLO_HRR_REQUIRED : SSL_CLIENT_HELLO_OK;
XiaokangQian75fe8c72022-06-15 09:42:45 +00001740}
1741
1742/* Update the handshake state machine */
1743
Ronald Cronce7d76e2022-07-08 18:56:49 +02001744MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01001745static int ssl_tls13_postprocess_client_hello(mbedtls_ssl_context *ssl)
XiaokangQian75fe8c72022-06-15 09:42:45 +00001746{
1747 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1748
XiaokangQian7807f9f2022-02-15 10:04:37 +00001749 /*
XiaokangQianfb665a82022-06-15 03:57:21 +00001750 * Server certificate selection
1751 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001752 if (ssl->conf->f_cert_cb && (ret = ssl->conf->f_cert_cb(ssl)) != 0) {
1753 MBEDTLS_SSL_DEBUG_RET(1, "f_cert_cb", ret);
1754 return ret;
XiaokangQianfb665a82022-06-15 03:57:21 +00001755 }
1756#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
1757 ssl->handshake->sni_name = NULL;
1758 ssl->handshake->sni_name_len = 0;
1759#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
XiaokangQian7807f9f2022-02-15 10:04:37 +00001760
Gilles Peskine449bd832023-01-11 14:50:10 +01001761 ret = mbedtls_ssl_tls13_key_schedule_stage_early(ssl);
1762 if (ret != 0) {
1763 MBEDTLS_SSL_DEBUG_RET(1,
1764 "mbedtls_ssl_tls1_3_key_schedule_stage_early", ret);
1765 return ret;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001766 }
1767
Gilles Peskine449bd832023-01-11 14:50:10 +01001768 return 0;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001769
1770}
1771
1772/*
XiaokangQianed582dd2022-04-13 08:21:05 +00001773 * Main entry point from the state machine; orchestrates the otherfunctions.
1774 */
1775
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001776MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01001777static int ssl_tls13_process_client_hello(mbedtls_ssl_context *ssl)
XiaokangQianed582dd2022-04-13 08:21:05 +00001778{
1779
XiaokangQian08037552022-04-20 07:16:41 +00001780 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine449bd832023-01-11 14:50:10 +01001781 unsigned char *buf = NULL;
XiaokangQianed582dd2022-04-13 08:21:05 +00001782 size_t buflen = 0;
Jerry Yu4ca91402022-05-09 15:50:57 +08001783 int parse_client_hello_ret;
1784
Gilles Peskine449bd832023-01-11 14:50:10 +01001785 MBEDTLS_SSL_DEBUG_MSG(2, ("=> parse client hello"));
XiaokangQianed582dd2022-04-13 08:21:05 +00001786
Gilles Peskine449bd832023-01-11 14:50:10 +01001787 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_tls13_fetch_handshake_msg(
1788 ssl, MBEDTLS_SSL_HS_CLIENT_HELLO,
1789 &buf, &buflen));
XiaokangQianed582dd2022-04-13 08:21:05 +00001790
Gilles Peskine449bd832023-01-11 14:50:10 +01001791 MBEDTLS_SSL_PROC_CHK_NEG(ssl_tls13_parse_client_hello(ssl, buf,
1792 buf + buflen));
Ronald Cron5af4c7f2023-03-07 20:46:59 +01001793 parse_client_hello_ret = ret; /* Store positive return value of
1794 * parse_client_hello,
1795 * as negative error codes are handled
Jerry Yuf41553b2022-05-09 22:20:30 +08001796 * by MBEDTLS_SSL_PROC_CHK_NEG. */
Jerry Yu4ca91402022-05-09 15:50:57 +08001797
Ronald Cronb828c7d2023-04-03 16:37:22 +02001798 /*
1799 * Version 1.2 of the protocol has been chosen, set the
1800 * ssl->keep_current_message flag for the ClientHello to be kept and parsed
1801 * as a TLS 1.2 ClientHello. We also change ssl->tls_version to
1802 * MBEDTLS_SSL_VERSION_TLS1_2 thus from now on mbedtls_ssl_handshake_step()
1803 * will dispatch to the TLS 1.2 state machine.
1804 */
Ronald Cron5af4c7f2023-03-07 20:46:59 +01001805 if (SSL_CLIENT_HELLO_TLS1_2 == parse_client_hello_ret) {
1806 ssl->keep_current_message = 1;
1807 ssl->tls_version = MBEDTLS_SSL_VERSION_TLS1_2;
1808 return 0;
1809 }
1810
Gilles Peskine449bd832023-01-11 14:50:10 +01001811 MBEDTLS_SSL_PROC_CHK(ssl_tls13_postprocess_client_hello(ssl));
Jerry Yu582dd062022-04-22 21:59:01 +08001812
Ronald Cron5af4c7f2023-03-07 20:46:59 +01001813 if (SSL_CLIENT_HELLO_OK == parse_client_hello_ret) {
Gilles Peskine449bd832023-01-11 14:50:10 +01001814 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_SERVER_HELLO);
1815 } else {
1816 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_HELLO_RETRY_REQUEST);
1817 }
XiaokangQianed582dd2022-04-13 08:21:05 +00001818
1819cleanup:
1820
Gilles Peskine449bd832023-01-11 14:50:10 +01001821 MBEDTLS_SSL_DEBUG_MSG(2, ("<= parse client hello"));
1822 return ret;
XiaokangQianed582dd2022-04-13 08:21:05 +00001823}
1824
1825/*
Jerry Yu1c3e6882022-04-20 21:23:40 +08001826 * Handler for MBEDTLS_SSL_SERVER_HELLO
Jerry Yu5b64ae92022-03-30 17:15:02 +08001827 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001828MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01001829static int ssl_tls13_prepare_server_hello(mbedtls_ssl_context *ssl)
Jerry Yuf4b27e42022-03-30 17:32:21 +08001830{
Jerry Yu637a3f12022-04-20 21:37:58 +08001831 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1832 unsigned char *server_randbytes =
Gilles Peskine449bd832023-01-11 14:50:10 +01001833 ssl->handshake->randbytes + MBEDTLS_CLIENT_HELLO_RANDOM_LEN;
1834 if (ssl->conf->f_rng == NULL) {
1835 MBEDTLS_SSL_DEBUG_MSG(1, ("no RNG provided"));
1836 return MBEDTLS_ERR_SSL_NO_RNG;
Jerry Yuf4b27e42022-03-30 17:32:21 +08001837 }
1838
Gilles Peskine449bd832023-01-11 14:50:10 +01001839 if ((ret = ssl->conf->f_rng(ssl->conf->p_rng, server_randbytes,
1840 MBEDTLS_SERVER_HELLO_RANDOM_LEN)) != 0) {
1841 MBEDTLS_SSL_DEBUG_RET(1, "f_rng", ret);
1842 return ret;
Jerry Yuf4b27e42022-03-30 17:32:21 +08001843 }
1844
Gilles Peskine449bd832023-01-11 14:50:10 +01001845 MBEDTLS_SSL_DEBUG_BUF(3, "server hello, random bytes", server_randbytes,
1846 MBEDTLS_SERVER_HELLO_RANDOM_LEN);
Jerry Yuf4b27e42022-03-30 17:32:21 +08001847
1848#if defined(MBEDTLS_HAVE_TIME)
YxCda609132023-05-22 12:08:12 -07001849 ssl->session_negotiate->start = mbedtls_time(NULL);
Jerry Yuf4b27e42022-03-30 17:32:21 +08001850#endif /* MBEDTLS_HAVE_TIME */
1851
Gilles Peskine449bd832023-01-11 14:50:10 +01001852 return ret;
Jerry Yuf4b27e42022-03-30 17:32:21 +08001853}
1854
Jerry Yu3bf2c642022-03-30 22:02:12 +08001855/*
Jerry Yue74e04a2022-04-21 09:23:16 +08001856 * ssl_tls13_write_server_hello_supported_versions_ext ():
Jerry Yu3bf2c642022-03-30 22:02:12 +08001857 *
1858 * struct {
Jerry Yufb9f54d2022-04-06 10:08:34 +08001859 * ProtocolVersion selected_version;
Jerry Yu3bf2c642022-03-30 22:02:12 +08001860 * } SupportedVersions;
1861 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001862MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yue74e04a2022-04-21 09:23:16 +08001863static int ssl_tls13_write_server_hello_supported_versions_ext(
Gilles Peskine449bd832023-01-11 14:50:10 +01001864 mbedtls_ssl_context *ssl,
1865 unsigned char *buf,
1866 unsigned char *end,
1867 size_t *out_len)
Jerry Yu3bf2c642022-03-30 22:02:12 +08001868{
Jerry Yu3bf2c642022-03-30 22:02:12 +08001869 *out_len = 0;
1870
Gilles Peskine449bd832023-01-11 14:50:10 +01001871 MBEDTLS_SSL_DEBUG_MSG(3, ("server hello, write selected version"));
Jerry Yu3bf2c642022-03-30 22:02:12 +08001872
1873 /* Check if we have space to write the extension:
1874 * - extension_type (2 bytes)
1875 * - extension_data_length (2 bytes)
Jerry Yu349a6132022-04-14 20:52:56 +08001876 * - selected_version (2 bytes)
Jerry Yu3bf2c642022-03-30 22:02:12 +08001877 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001878 MBEDTLS_SSL_CHK_BUF_PTR(buf, end, 6);
Jerry Yu3bf2c642022-03-30 22:02:12 +08001879
Gilles Peskine449bd832023-01-11 14:50:10 +01001880 MBEDTLS_PUT_UINT16_BE(MBEDTLS_TLS_EXT_SUPPORTED_VERSIONS, buf, 0);
Jerry Yu3bf2c642022-03-30 22:02:12 +08001881
Gilles Peskine449bd832023-01-11 14:50:10 +01001882 MBEDTLS_PUT_UINT16_BE(2, buf, 2);
Jerry Yu3bf2c642022-03-30 22:02:12 +08001883
Gilles Peskine449bd832023-01-11 14:50:10 +01001884 mbedtls_ssl_write_version(buf + 4,
1885 ssl->conf->transport,
1886 ssl->tls_version);
Jerry Yu3bf2c642022-03-30 22:02:12 +08001887
Gilles Peskine449bd832023-01-11 14:50:10 +01001888 MBEDTLS_SSL_DEBUG_MSG(3, ("supported version: [%04x]",
1889 ssl->tls_version));
Jerry Yu3bf2c642022-03-30 22:02:12 +08001890
Jerry Yu349a6132022-04-14 20:52:56 +08001891 *out_len = 6;
Jerry Yu3bf2c642022-03-30 22:02:12 +08001892
Jerry Yub95dd362022-11-08 21:19:34 +08001893 mbedtls_ssl_tls13_set_hs_sent_ext_mask(
Gilles Peskine449bd832023-01-11 14:50:10 +01001894 ssl, MBEDTLS_TLS_EXT_SUPPORTED_VERSIONS);
Jerry Yub95dd362022-11-08 21:19:34 +08001895
Gilles Peskine449bd832023-01-11 14:50:10 +01001896 return 0;
Jerry Yu3bf2c642022-03-30 22:02:12 +08001897}
1898
Jerry Yud9436a12022-04-20 22:28:09 +08001899
Jerry Yu3bf2c642022-03-30 22:02:12 +08001900
1901/* Generate and export a single key share. For hybrid KEMs, this can
1902 * be called multiple times with the different components of the hybrid. */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001903MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01001904static int ssl_tls13_generate_and_write_key_share(mbedtls_ssl_context *ssl,
1905 uint16_t named_group,
1906 unsigned char *buf,
1907 unsigned char *end,
1908 size_t *out_len)
Jerry Yu3bf2c642022-03-30 22:02:12 +08001909{
1910 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yu955ddd72022-04-22 22:27:33 +08001911
1912 *out_len = 0;
1913
Valerio Setti080a22b2023-03-20 15:22:47 +01001914#if defined(PSA_WANT_ALG_ECDH)
Gilles Peskine449bd832023-01-11 14:50:10 +01001915 if (mbedtls_ssl_tls13_named_group_is_ecdhe(named_group)) {
Jerry Yu89e103c2022-03-30 22:43:29 +08001916 ret = mbedtls_ssl_tls13_generate_and_write_ecdh_key_exchange(
Gilles Peskine449bd832023-01-11 14:50:10 +01001917 ssl, named_group, buf, end, out_len);
1918 if (ret != 0) {
Jerry Yu89e103c2022-03-30 22:43:29 +08001919 MBEDTLS_SSL_DEBUG_RET(
1920 1, "mbedtls_ssl_tls13_generate_and_write_ecdh_key_exchange",
Gilles Peskine449bd832023-01-11 14:50:10 +01001921 ret);
1922 return ret;
Jerry Yu3bf2c642022-03-30 22:02:12 +08001923 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001924 } else
Valerio Setti080a22b2023-03-20 15:22:47 +01001925#endif /* PSA_WANT_ALG_ECDH */
Gilles Peskine449bd832023-01-11 14:50:10 +01001926 if (0 /* Other kinds of KEMs */) {
1927 } else {
Jerry Yu955ddd72022-04-22 22:27:33 +08001928 ((void) ssl);
1929 ((void) named_group);
1930 ((void) buf);
1931 ((void) end);
Jerry Yu3bf2c642022-03-30 22:02:12 +08001932 ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
1933 }
1934
Gilles Peskine449bd832023-01-11 14:50:10 +01001935 return ret;
Jerry Yu3bf2c642022-03-30 22:02:12 +08001936}
1937
1938/*
1939 * ssl_tls13_write_key_share_ext
1940 *
1941 * Structure of key_share extension in ServerHello:
1942 *
Jerry Yu1c3e6882022-04-20 21:23:40 +08001943 * struct {
1944 * NamedGroup group;
1945 * opaque key_exchange<1..2^16-1>;
1946 * } KeyShareEntry;
1947 * struct {
1948 * KeyShareEntry server_share;
1949 * } KeyShareServerHello;
Jerry Yu3bf2c642022-03-30 22:02:12 +08001950 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001951MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01001952static int ssl_tls13_write_key_share_ext(mbedtls_ssl_context *ssl,
1953 unsigned char *buf,
1954 unsigned char *end,
1955 size_t *out_len)
Jerry Yu3bf2c642022-03-30 22:02:12 +08001956{
Jerry Yu955ddd72022-04-22 22:27:33 +08001957 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yu3bf2c642022-03-30 22:02:12 +08001958 unsigned char *p = buf;
Jerry Yu57d48412022-04-20 21:50:42 +08001959 uint16_t group = ssl->handshake->offered_group_id;
Jerry Yu3bf2c642022-03-30 22:02:12 +08001960 unsigned char *server_share = buf + 4;
Jerry Yu3bf2c642022-03-30 22:02:12 +08001961 size_t key_exchange_length;
Jerry Yu3bf2c642022-03-30 22:02:12 +08001962
1963 *out_len = 0;
1964
Gilles Peskine449bd832023-01-11 14:50:10 +01001965 MBEDTLS_SSL_DEBUG_MSG(3, ("server hello, adding key share extension"));
Jerry Yu3bf2c642022-03-30 22:02:12 +08001966
Gilles Peskine449bd832023-01-11 14:50:10 +01001967 MBEDTLS_SSL_DEBUG_MSG(2, ("server hello, write selected_group: %s (%04x)",
1968 mbedtls_ssl_named_group_to_str(group),
1969 group));
Jerry Yu58af2332022-09-06 11:19:31 +08001970
Jerry Yu3bf2c642022-03-30 22:02:12 +08001971 /* Check if we have space for header and length fields:
1972 * - extension_type (2 bytes)
1973 * - extension_data_length (2 bytes)
1974 * - group (2 bytes)
1975 * - key_exchange_length (2 bytes)
1976 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001977 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 8);
1978 MBEDTLS_PUT_UINT16_BE(MBEDTLS_TLS_EXT_KEY_SHARE, p, 0);
1979 MBEDTLS_PUT_UINT16_BE(group, server_share, 0);
Jerry Yu3bf2c642022-03-30 22:02:12 +08001980 p += 8;
Jerry Yu57d48412022-04-20 21:50:42 +08001981
Jerry Yu3bf2c642022-03-30 22:02:12 +08001982 /* When we introduce PQC-ECDHE hybrids, we'll want to call this
1983 * function multiple times. */
Jerry Yu955ddd72022-04-22 22:27:33 +08001984 ret = ssl_tls13_generate_and_write_key_share(
Gilles Peskine449bd832023-01-11 14:50:10 +01001985 ssl, group, server_share + 4, end, &key_exchange_length);
1986 if (ret != 0) {
1987 return ret;
1988 }
Jerry Yu3bf2c642022-03-30 22:02:12 +08001989 p += key_exchange_length;
Jerry Yuc1be19f2022-04-23 16:11:39 +08001990
Gilles Peskine449bd832023-01-11 14:50:10 +01001991 MBEDTLS_PUT_UINT16_BE(key_exchange_length, server_share + 2, 0);
Jerry Yu3bf2c642022-03-30 22:02:12 +08001992
Gilles Peskine449bd832023-01-11 14:50:10 +01001993 MBEDTLS_PUT_UINT16_BE(p - server_share, buf, 2);
Jerry Yu3bf2c642022-03-30 22:02:12 +08001994
Jerry Yu57d48412022-04-20 21:50:42 +08001995 *out_len = p - buf;
Jerry Yu955ddd72022-04-22 22:27:33 +08001996
Gilles Peskine449bd832023-01-11 14:50:10 +01001997 mbedtls_ssl_tls13_set_hs_sent_ext_mask(ssl, MBEDTLS_TLS_EXT_KEY_SHARE);
Jerry Yub95dd362022-11-08 21:19:34 +08001998
Gilles Peskine449bd832023-01-11 14:50:10 +01001999 return 0;
Jerry Yu3bf2c642022-03-30 22:02:12 +08002000}
Jerry Yud9436a12022-04-20 22:28:09 +08002001
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002002MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002003static int ssl_tls13_write_hrr_key_share_ext(mbedtls_ssl_context *ssl,
2004 unsigned char *buf,
2005 unsigned char *end,
2006 size_t *out_len)
Jerry Yu23f7a6f2022-04-23 15:16:45 +08002007{
2008 uint16_t selected_group = ssl->handshake->hrr_selected_group;
2009 /* key_share Extension
2010 *
2011 * struct {
2012 * select (Handshake.msg_type) {
2013 * ...
2014 * case hello_retry_request:
2015 * NamedGroup selected_group;
2016 * ...
2017 * };
2018 * } KeyShare;
2019 */
2020
2021 *out_len = 0;
2022
Jerry Yub0ac10b2022-05-05 11:10:08 +08002023 /*
2024 * For a pure PSK key exchange, there is no group to agree upon. The purpose
2025 * of the HRR is then to transmit a cookie to force the client to demonstrate
2026 * reachability at their apparent network address (primarily useful for DTLS).
2027 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002028 if (!mbedtls_ssl_tls13_key_exchange_mode_with_ephemeral(ssl)) {
2029 return 0;
2030 }
Jerry Yu23f7a6f2022-04-23 15:16:45 +08002031
2032 /* We should only send the key_share extension if the client's initial
2033 * key share was not acceptable. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002034 if (ssl->handshake->offered_group_id != 0) {
2035 MBEDTLS_SSL_DEBUG_MSG(4, ("Skip key_share extension in HRR"));
2036 return 0;
Jerry Yu23f7a6f2022-04-23 15:16:45 +08002037 }
2038
Gilles Peskine449bd832023-01-11 14:50:10 +01002039 if (selected_group == 0) {
2040 MBEDTLS_SSL_DEBUG_MSG(1, ("no matching named group found"));
2041 return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
Jerry Yu23f7a6f2022-04-23 15:16:45 +08002042 }
2043
Jerry Yub0ac10b2022-05-05 11:10:08 +08002044 /* Check if we have enough space:
2045 * - extension_type (2 bytes)
2046 * - extension_data_length (2 bytes)
2047 * - selected_group (2 bytes)
2048 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002049 MBEDTLS_SSL_CHK_BUF_PTR(buf, end, 6);
Jerry Yu23f7a6f2022-04-23 15:16:45 +08002050
Gilles Peskine449bd832023-01-11 14:50:10 +01002051 MBEDTLS_PUT_UINT16_BE(MBEDTLS_TLS_EXT_KEY_SHARE, buf, 0);
2052 MBEDTLS_PUT_UINT16_BE(2, buf, 2);
2053 MBEDTLS_PUT_UINT16_BE(selected_group, buf, 4);
Jerry Yu23f7a6f2022-04-23 15:16:45 +08002054
Gilles Peskine449bd832023-01-11 14:50:10 +01002055 MBEDTLS_SSL_DEBUG_MSG(3,
2056 ("HRR selected_group: %s (%x)",
2057 mbedtls_ssl_named_group_to_str(selected_group),
2058 selected_group));
Jerry Yu23f7a6f2022-04-23 15:16:45 +08002059
2060 *out_len = 6;
Jerry Yu23f7a6f2022-04-23 15:16:45 +08002061
Gilles Peskine449bd832023-01-11 14:50:10 +01002062 mbedtls_ssl_tls13_set_hs_sent_ext_mask(ssl, MBEDTLS_TLS_EXT_KEY_SHARE);
Jerry Yub95dd362022-11-08 21:19:34 +08002063
Gilles Peskine449bd832023-01-11 14:50:10 +01002064 return 0;
Jerry Yu23f7a6f2022-04-23 15:16:45 +08002065}
Jerry Yu3bf2c642022-03-30 22:02:12 +08002066
2067/*
2068 * Structure of ServerHello message:
2069 *
2070 * struct {
2071 * ProtocolVersion legacy_version = 0x0303; // TLS v1.2
2072 * Random random;
2073 * opaque legacy_session_id_echo<0..32>;
2074 * CipherSuite cipher_suite;
2075 * uint8 legacy_compression_method = 0;
2076 * Extension extensions<6..2^16-1>;
2077 * } ServerHello;
2078 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002079MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002080static int ssl_tls13_write_server_hello_body(mbedtls_ssl_context *ssl,
2081 unsigned char *buf,
2082 unsigned char *end,
2083 size_t *out_len,
2084 int is_hrr)
Jerry Yu56404d72022-03-30 17:36:13 +08002085{
Jerry Yu955ddd72022-04-22 22:27:33 +08002086 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yu3bf2c642022-03-30 22:02:12 +08002087 unsigned char *p = buf;
Jerry Yud9436a12022-04-20 22:28:09 +08002088 unsigned char *p_extensions_len;
Jerry Yufbe3e642022-04-25 19:31:51 +08002089 size_t output_len;
Jerry Yu3bf2c642022-03-30 22:02:12 +08002090
2091 *out_len = 0;
Jerry Yu50e00e32022-10-31 14:45:01 +08002092 ssl->handshake->sent_extensions = MBEDTLS_SSL_EXT_MASK_NONE;
Jerry Yu3bf2c642022-03-30 22:02:12 +08002093
Jerry Yucfc04b32022-04-21 09:31:58 +08002094 /* ...
2095 * ProtocolVersion legacy_version = 0x0303; // TLS 1.2
2096 * ...
2097 * with ProtocolVersion defined as:
2098 * uint16 ProtocolVersion;
Jerry Yu3bf2c642022-03-30 22:02:12 +08002099 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002100 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 2);
2101 MBEDTLS_PUT_UINT16_BE(0x0303, p, 0);
Jerry Yu3bf2c642022-03-30 22:02:12 +08002102 p += 2;
2103
Jerry Yu1c3e6882022-04-20 21:23:40 +08002104 /* ...
2105 * Random random;
2106 * ...
Jerry Yucfc04b32022-04-21 09:31:58 +08002107 * with Random defined as:
2108 * opaque Random[MBEDTLS_SERVER_HELLO_RANDOM_LEN];
Jerry Yu1c3e6882022-04-20 21:23:40 +08002109 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002110 MBEDTLS_SSL_CHK_BUF_PTR(p, end, MBEDTLS_SERVER_HELLO_RANDOM_LEN);
2111 if (is_hrr) {
2112 memcpy(p, mbedtls_ssl_tls13_hello_retry_request_magic,
2113 MBEDTLS_SERVER_HELLO_RANDOM_LEN);
2114 } else {
2115 memcpy(p, &ssl->handshake->randbytes[MBEDTLS_CLIENT_HELLO_RANDOM_LEN],
2116 MBEDTLS_SERVER_HELLO_RANDOM_LEN);
Jerry Yu23f7a6f2022-04-23 15:16:45 +08002117 }
Gilles Peskine449bd832023-01-11 14:50:10 +01002118 MBEDTLS_SSL_DEBUG_BUF(3, "server hello, random bytes",
2119 p, MBEDTLS_SERVER_HELLO_RANDOM_LEN);
Jerry Yu3bf2c642022-03-30 22:02:12 +08002120 p += MBEDTLS_SERVER_HELLO_RANDOM_LEN;
2121
Jerry Yucfc04b32022-04-21 09:31:58 +08002122 /* ...
2123 * opaque legacy_session_id_echo<0..32>;
2124 * ...
Jerry Yu3bf2c642022-03-30 22:02:12 +08002125 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002126 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 1 + ssl->session_negotiate->id_len);
2127 *p++ = (unsigned char) ssl->session_negotiate->id_len;
2128 if (ssl->session_negotiate->id_len > 0) {
2129 memcpy(p, &ssl->session_negotiate->id[0],
2130 ssl->session_negotiate->id_len);
Jerry Yu3bf2c642022-03-30 22:02:12 +08002131 p += ssl->session_negotiate->id_len;
Jerry Yu955ddd72022-04-22 22:27:33 +08002132
Gilles Peskine449bd832023-01-11 14:50:10 +01002133 MBEDTLS_SSL_DEBUG_BUF(3, "session id", ssl->session_negotiate->id,
2134 ssl->session_negotiate->id_len);
Jerry Yu3bf2c642022-03-30 22:02:12 +08002135 }
2136
Jerry Yucfc04b32022-04-21 09:31:58 +08002137 /* ...
2138 * CipherSuite cipher_suite;
2139 * ...
2140 * with CipherSuite defined as:
2141 * uint8 CipherSuite[2];
Jerry Yu3bf2c642022-03-30 22:02:12 +08002142 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002143 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 2);
2144 MBEDTLS_PUT_UINT16_BE(ssl->session_negotiate->ciphersuite, p, 0);
Jerry Yu3bf2c642022-03-30 22:02:12 +08002145 p += 2;
Gilles Peskine449bd832023-01-11 14:50:10 +01002146 MBEDTLS_SSL_DEBUG_MSG(3,
2147 ("server hello, chosen ciphersuite: %s ( id=%d )",
2148 mbedtls_ssl_get_ciphersuite_name(
2149 ssl->session_negotiate->ciphersuite),
2150 ssl->session_negotiate->ciphersuite));
Jerry Yu3bf2c642022-03-30 22:02:12 +08002151
Jerry Yucfc04b32022-04-21 09:31:58 +08002152 /* ...
2153 * uint8 legacy_compression_method = 0;
2154 * ...
2155 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002156 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 1);
Thomas Daubney31e03a82022-07-25 15:59:25 +01002157 *p++ = MBEDTLS_SSL_COMPRESS_NULL;
Jerry Yu3bf2c642022-03-30 22:02:12 +08002158
Jerry Yucfc04b32022-04-21 09:31:58 +08002159 /* ...
2160 * Extension extensions<6..2^16-1>;
2161 * ...
2162 * struct {
2163 * ExtensionType extension_type; (2 bytes)
2164 * opaque extension_data<0..2^16-1>;
2165 * } Extension;
2166 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002167 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 2);
Jerry Yud9436a12022-04-20 22:28:09 +08002168 p_extensions_len = p;
Jerry Yu3bf2c642022-03-30 22:02:12 +08002169 p += 2;
2170
Gilles Peskine449bd832023-01-11 14:50:10 +01002171 if ((ret = ssl_tls13_write_server_hello_supported_versions_ext(
2172 ssl, p, end, &output_len)) != 0) {
Jerry Yu955ddd72022-04-22 22:27:33 +08002173 MBEDTLS_SSL_DEBUG_RET(
Gilles Peskine449bd832023-01-11 14:50:10 +01002174 1, "ssl_tls13_write_server_hello_supported_versions_ext", ret);
2175 return ret;
Jerry Yu3bf2c642022-03-30 22:02:12 +08002176 }
2177 p += output_len;
2178
Gilles Peskine449bd832023-01-11 14:50:10 +01002179 if (mbedtls_ssl_tls13_key_exchange_mode_with_ephemeral(ssl)) {
2180 if (is_hrr) {
2181 ret = ssl_tls13_write_hrr_key_share_ext(ssl, p, end, &output_len);
2182 } else {
2183 ret = ssl_tls13_write_key_share_ext(ssl, p, end, &output_len);
2184 }
2185 if (ret != 0) {
2186 return ret;
2187 }
Jerry Yu3bf2c642022-03-30 22:02:12 +08002188 p += output_len;
2189 }
Jerry Yu3bf2c642022-03-30 22:02:12 +08002190
Ronald Cron41a443a2022-10-04 16:38:25 +02002191#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01002192 if (!is_hrr && mbedtls_ssl_tls13_key_exchange_mode_with_psk(ssl)) {
2193 ret = ssl_tls13_write_server_pre_shared_key_ext(ssl, p, end, &output_len);
2194 if (ret != 0) {
2195 MBEDTLS_SSL_DEBUG_RET(1, "ssl_tls13_write_server_pre_shared_key_ext",
2196 ret);
2197 return ret;
Jerry Yu032b15ce2022-07-11 06:10:03 +00002198 }
2199 p += output_len;
2200 }
Ronald Cron41a443a2022-10-04 16:38:25 +02002201#endif
Jerry Yu032b15ce2022-07-11 06:10:03 +00002202
Gilles Peskine449bd832023-01-11 14:50:10 +01002203 MBEDTLS_PUT_UINT16_BE(p - p_extensions_len - 2, p_extensions_len, 0);
Jerry Yu3bf2c642022-03-30 22:02:12 +08002204
Gilles Peskine449bd832023-01-11 14:50:10 +01002205 MBEDTLS_SSL_DEBUG_BUF(4, "server hello extensions",
2206 p_extensions_len, p - p_extensions_len);
Jerry Yu3bf2c642022-03-30 22:02:12 +08002207
Jerry Yud9436a12022-04-20 22:28:09 +08002208 *out_len = p - buf;
Jerry Yu3bf2c642022-03-30 22:02:12 +08002209
Gilles Peskine449bd832023-01-11 14:50:10 +01002210 MBEDTLS_SSL_DEBUG_BUF(3, "server hello", buf, *out_len);
Jerry Yu3bf2c642022-03-30 22:02:12 +08002211
Jerry Yu7de2ff02022-11-08 21:43:46 +08002212 MBEDTLS_SSL_PRINT_EXTS(
Jerry Yu4b8f2f72022-10-31 13:31:22 +08002213 3, is_hrr ? MBEDTLS_SSL_TLS1_3_HS_HELLO_RETRY_REQUEST :
Gilles Peskine449bd832023-01-11 14:50:10 +01002214 MBEDTLS_SSL_HS_SERVER_HELLO,
2215 ssl->handshake->sent_extensions);
Jerry Yu4b8f2f72022-10-31 13:31:22 +08002216
Gilles Peskine449bd832023-01-11 14:50:10 +01002217 return ret;
Jerry Yu56404d72022-03-30 17:36:13 +08002218}
2219
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002220MBEDTLS_CHECK_RETURN_CRITICAL
Xiaokang Qian934ce6f2023-02-06 10:23:04 +00002221static int ssl_tls13_finalize_server_hello(mbedtls_ssl_context *ssl)
Jerry Yue110d252022-05-05 10:19:22 +08002222{
2223 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine449bd832023-01-11 14:50:10 +01002224 ret = mbedtls_ssl_tls13_compute_handshake_transform(ssl);
2225 if (ret != 0) {
2226 MBEDTLS_SSL_DEBUG_RET(1,
2227 "mbedtls_ssl_tls13_compute_handshake_transform",
2228 ret);
2229 return ret;
Jerry Yue110d252022-05-05 10:19:22 +08002230 }
2231
Gilles Peskine449bd832023-01-11 14:50:10 +01002232 return ret;
Jerry Yue110d252022-05-05 10:19:22 +08002233}
2234
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002235MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002236static int ssl_tls13_write_server_hello(mbedtls_ssl_context *ssl)
Jerry Yu5b64ae92022-03-30 17:15:02 +08002237{
Jerry Yu637a3f12022-04-20 21:37:58 +08002238 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yuf4b27e42022-03-30 17:32:21 +08002239 unsigned char *buf;
2240 size_t buf_len, msg_len;
2241
Gilles Peskine449bd832023-01-11 14:50:10 +01002242 MBEDTLS_SSL_DEBUG_MSG(2, ("=> write server hello"));
Jerry Yuf4b27e42022-03-30 17:32:21 +08002243
Gilles Peskine449bd832023-01-11 14:50:10 +01002244 MBEDTLS_SSL_PROC_CHK(ssl_tls13_prepare_server_hello(ssl));
Jerry Yuf4b27e42022-03-30 17:32:21 +08002245
Xiaokang Qian9f1747b2023-03-30 02:50:04 +00002246 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_start_handshake_msg(
2247 ssl, MBEDTLS_SSL_HS_SERVER_HELLO, &buf, &buf_len));
Jerry Yuf4b27e42022-03-30 17:32:21 +08002248
Gilles Peskine449bd832023-01-11 14:50:10 +01002249 MBEDTLS_SSL_PROC_CHK(ssl_tls13_write_server_hello_body(ssl, buf,
2250 buf + buf_len,
2251 &msg_len,
2252 0));
Jerry Yuf4b27e42022-03-30 17:32:21 +08002253
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +01002254 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_add_hs_msg_to_checksum(
Manuel Pégourié-Gonnard43cc1272023-02-06 11:48:19 +01002255 ssl, MBEDTLS_SSL_HS_SERVER_HELLO, buf, msg_len));
Jerry Yuf4b27e42022-03-30 17:32:21 +08002256
Gilles Peskine449bd832023-01-11 14:50:10 +01002257 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_finish_handshake_msg(
2258 ssl, buf_len, msg_len));
Jerry Yu637a3f12022-04-20 21:37:58 +08002259
Xiaokang Qian934ce6f2023-02-06 10:23:04 +00002260 MBEDTLS_SSL_PROC_CHK(ssl_tls13_finalize_server_hello(ssl));
Jerry Yue110d252022-05-05 10:19:22 +08002261
Gabor Mezei7b39bf12022-05-24 16:04:14 +02002262#if defined(MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE)
2263 /* The server sends a dummy change_cipher_spec record immediately
2264 * after its first handshake message. This may either be after
2265 * a ServerHello or a HelloRetryRequest.
2266 */
Gabor Mezei96ae9262022-06-28 11:45:18 +02002267 mbedtls_ssl_handshake_set_state(
Gilles Peskine449bd832023-01-11 14:50:10 +01002268 ssl, MBEDTLS_SSL_SERVER_CCS_AFTER_SERVER_HELLO);
Gabor Mezei7b39bf12022-05-24 16:04:14 +02002269#else
Gilles Peskine449bd832023-01-11 14:50:10 +01002270 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_ENCRYPTED_EXTENSIONS);
Gabor Mezei7b39bf12022-05-24 16:04:14 +02002271#endif /* MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE */
Jerry Yue110d252022-05-05 10:19:22 +08002272
Jerry Yuf4b27e42022-03-30 17:32:21 +08002273cleanup:
2274
Gilles Peskine449bd832023-01-11 14:50:10 +01002275 MBEDTLS_SSL_DEBUG_MSG(2, ("<= write server hello"));
2276 return ret;
Jerry Yu5b64ae92022-03-30 17:15:02 +08002277}
2278
Jerry Yu23d1a252022-05-12 18:08:59 +08002279
2280/*
2281 * Handler for MBEDTLS_SSL_HELLO_RETRY_REQUEST
2282 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002283MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002284static int ssl_tls13_prepare_hello_retry_request(mbedtls_ssl_context *ssl)
Jerry Yu23d1a252022-05-12 18:08:59 +08002285{
2286 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine449bd832023-01-11 14:50:10 +01002287 if (ssl->handshake->hello_retry_request_count > 0) {
2288 MBEDTLS_SSL_DEBUG_MSG(1, ("Too many HRRs"));
2289 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE,
2290 MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE);
2291 return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
Jerry Yu23d1a252022-05-12 18:08:59 +08002292 }
2293
2294 /*
2295 * Create stateless transcript hash for HRR
2296 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002297 MBEDTLS_SSL_DEBUG_MSG(4, ("Reset transcript for HRR"));
2298 ret = mbedtls_ssl_reset_transcript_for_hrr(ssl);
2299 if (ret != 0) {
2300 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_reset_transcript_for_hrr", ret);
2301 return ret;
Jerry Yu23d1a252022-05-12 18:08:59 +08002302 }
Gilles Peskine449bd832023-01-11 14:50:10 +01002303 mbedtls_ssl_session_reset_msg_layer(ssl, 0);
Jerry Yu23d1a252022-05-12 18:08:59 +08002304
Gilles Peskine449bd832023-01-11 14:50:10 +01002305 return 0;
Jerry Yu23d1a252022-05-12 18:08:59 +08002306}
2307
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002308MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002309static int ssl_tls13_write_hello_retry_request(mbedtls_ssl_context *ssl)
Jerry Yu23d1a252022-05-12 18:08:59 +08002310{
2311 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
2312 unsigned char *buf;
2313 size_t buf_len, msg_len;
2314
Gilles Peskine449bd832023-01-11 14:50:10 +01002315 MBEDTLS_SSL_DEBUG_MSG(2, ("=> write hello retry request"));
Jerry Yu23d1a252022-05-12 18:08:59 +08002316
Gilles Peskine449bd832023-01-11 14:50:10 +01002317 MBEDTLS_SSL_PROC_CHK(ssl_tls13_prepare_hello_retry_request(ssl));
Jerry Yu23d1a252022-05-12 18:08:59 +08002318
Gilles Peskine449bd832023-01-11 14:50:10 +01002319 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_start_handshake_msg(
2320 ssl, MBEDTLS_SSL_HS_SERVER_HELLO,
2321 &buf, &buf_len));
Jerry Yu23d1a252022-05-12 18:08:59 +08002322
Gilles Peskine449bd832023-01-11 14:50:10 +01002323 MBEDTLS_SSL_PROC_CHK(ssl_tls13_write_server_hello_body(ssl, buf,
2324 buf + buf_len,
2325 &msg_len,
2326 1));
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +01002327 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_add_hs_msg_to_checksum(
Manuel Pégourié-Gonnard43cc1272023-02-06 11:48:19 +01002328 ssl, MBEDTLS_SSL_HS_SERVER_HELLO, buf, msg_len));
Jerry Yu23d1a252022-05-12 18:08:59 +08002329
2330
Gilles Peskine449bd832023-01-11 14:50:10 +01002331 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_finish_handshake_msg(ssl, buf_len,
2332 msg_len));
Jerry Yu23d1a252022-05-12 18:08:59 +08002333
2334 ssl->handshake->hello_retry_request_count++;
2335
Gabor Mezei7b39bf12022-05-24 16:04:14 +02002336#if defined(MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE)
2337 /* The server sends a dummy change_cipher_spec record immediately
2338 * after its first handshake message. This may either be after
2339 * a ServerHello or a HelloRetryRequest.
2340 */
Gabor Mezei96ae9262022-06-28 11:45:18 +02002341 mbedtls_ssl_handshake_set_state(
Gilles Peskine449bd832023-01-11 14:50:10 +01002342 ssl, MBEDTLS_SSL_SERVER_CCS_AFTER_HELLO_RETRY_REQUEST);
Gabor Mezei7b39bf12022-05-24 16:04:14 +02002343#else
Gilles Peskine449bd832023-01-11 14:50:10 +01002344 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_CLIENT_HELLO);
Gabor Mezei7b39bf12022-05-24 16:04:14 +02002345#endif /* MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE */
Jerry Yu23d1a252022-05-12 18:08:59 +08002346
2347cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +01002348 MBEDTLS_SSL_DEBUG_MSG(2, ("<= write hello retry request"));
2349 return ret;
Jerry Yu23d1a252022-05-12 18:08:59 +08002350}
2351
Jerry Yu5b64ae92022-03-30 17:15:02 +08002352/*
Jerry Yu4d3841a2022-04-16 12:37:19 +08002353 * Handler for MBEDTLS_SSL_ENCRYPTED_EXTENSIONS
2354 */
Jerry Yu4d3841a2022-04-16 12:37:19 +08002355
2356/*
2357 * struct {
2358 * Extension extensions<0..2 ^ 16 - 1>;
2359 * } EncryptedExtensions;
2360 *
2361 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002362MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002363static int ssl_tls13_write_encrypted_extensions_body(mbedtls_ssl_context *ssl,
2364 unsigned char *buf,
2365 unsigned char *end,
2366 size_t *out_len)
Jerry Yu4d3841a2022-04-16 12:37:19 +08002367{
XiaokangQianacb39922022-06-17 10:18:48 +00002368 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yu4d3841a2022-04-16 12:37:19 +08002369 unsigned char *p = buf;
2370 size_t extensions_len = 0;
Jerry Yu8937eb42022-05-03 12:12:14 +08002371 unsigned char *p_extensions_len;
XiaokangQianacb39922022-06-17 10:18:48 +00002372 size_t output_len;
Jerry Yu9da5e5a2022-05-03 15:46:09 +08002373
Jerry Yu4d3841a2022-04-16 12:37:19 +08002374 *out_len = 0;
2375
Gilles Peskine449bd832023-01-11 14:50:10 +01002376 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 2);
Jerry Yu8937eb42022-05-03 12:12:14 +08002377 p_extensions_len = p;
Jerry Yu4d3841a2022-04-16 12:37:19 +08002378 p += 2;
2379
Jerry Yu8937eb42022-05-03 12:12:14 +08002380 ((void) ssl);
XiaokangQianacb39922022-06-17 10:18:48 +00002381 ((void) ret);
2382 ((void) output_len);
2383
2384#if defined(MBEDTLS_SSL_ALPN)
Gilles Peskine449bd832023-01-11 14:50:10 +01002385 ret = mbedtls_ssl_write_alpn_ext(ssl, p, end, &output_len);
2386 if (ret != 0) {
2387 return ret;
2388 }
XiaokangQian95d5f542022-06-24 02:29:26 +00002389 p += output_len;
XiaokangQianacb39922022-06-17 10:18:48 +00002390#endif /* MBEDTLS_SSL_ALPN */
Jerry Yu4d3841a2022-04-16 12:37:19 +08002391
Gilles Peskine449bd832023-01-11 14:50:10 +01002392 extensions_len = (p - p_extensions_len) - 2;
2393 MBEDTLS_PUT_UINT16_BE(extensions_len, p_extensions_len, 0);
Jerry Yu8937eb42022-05-03 12:12:14 +08002394
2395 *out_len = p - buf;
Jerry Yu4d3841a2022-04-16 12:37:19 +08002396
Gilles Peskine449bd832023-01-11 14:50:10 +01002397 MBEDTLS_SSL_DEBUG_BUF(4, "encrypted extensions", buf, *out_len);
Jerry Yu4d3841a2022-04-16 12:37:19 +08002398
Jerry Yu7de2ff02022-11-08 21:43:46 +08002399 MBEDTLS_SSL_PRINT_EXTS(
Gilles Peskine449bd832023-01-11 14:50:10 +01002400 3, MBEDTLS_SSL_HS_ENCRYPTED_EXTENSIONS, ssl->handshake->sent_extensions);
Jerry Yu4b8f2f72022-10-31 13:31:22 +08002401
Gilles Peskine449bd832023-01-11 14:50:10 +01002402 return 0;
Jerry Yu4d3841a2022-04-16 12:37:19 +08002403}
2404
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002405MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002406static int ssl_tls13_write_encrypted_extensions(mbedtls_ssl_context *ssl)
Jerry Yu4d3841a2022-04-16 12:37:19 +08002407{
2408 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
2409 unsigned char *buf;
Jerry Yu39730a72022-05-03 12:14:04 +08002410 size_t buf_len, msg_len;
Jerry Yu4d3841a2022-04-16 12:37:19 +08002411
Gilles Peskine449bd832023-01-11 14:50:10 +01002412 mbedtls_ssl_set_outbound_transform(ssl,
2413 ssl->handshake->transform_handshake);
Gabor Mezei54719122022-06-28 11:34:56 +02002414 MBEDTLS_SSL_DEBUG_MSG(
Gilles Peskine449bd832023-01-11 14:50:10 +01002415 3, ("switching to handshake transform for outbound data"));
Gabor Mezei54719122022-06-28 11:34:56 +02002416
Gilles Peskine449bd832023-01-11 14:50:10 +01002417 MBEDTLS_SSL_DEBUG_MSG(2, ("=> write encrypted extensions"));
Jerry Yu4d3841a2022-04-16 12:37:19 +08002418
Xiaokang Qian9f1747b2023-03-30 02:50:04 +00002419 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_start_handshake_msg(
2420 ssl, MBEDTLS_SSL_HS_ENCRYPTED_EXTENSIONS,
2421 &buf, &buf_len));
Jerry Yu4d3841a2022-04-16 12:37:19 +08002422
Gilles Peskine449bd832023-01-11 14:50:10 +01002423 MBEDTLS_SSL_PROC_CHK(ssl_tls13_write_encrypted_extensions_body(
2424 ssl, buf, buf + buf_len, &msg_len));
Jerry Yu4d3841a2022-04-16 12:37:19 +08002425
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +01002426 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_add_hs_msg_to_checksum(
Xiaokang Qian9f1747b2023-03-30 02:50:04 +00002427 ssl, MBEDTLS_SSL_HS_ENCRYPTED_EXTENSIONS,
2428 buf, msg_len));
Jerry Yu4d3841a2022-04-16 12:37:19 +08002429
Gilles Peskine449bd832023-01-11 14:50:10 +01002430 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_finish_handshake_msg(
2431 ssl, buf_len, msg_len));
Jerry Yu4d3841a2022-04-16 12:37:19 +08002432
Ronald Cron928cbd32022-10-04 16:14:26 +02002433#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01002434 if (mbedtls_ssl_tls13_key_exchange_mode_with_psk(ssl)) {
2435 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_SERVER_FINISHED);
2436 } else {
2437 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_CERTIFICATE_REQUEST);
2438 }
Jerry Yu8937eb42022-05-03 12:12:14 +08002439#else
Gilles Peskine449bd832023-01-11 14:50:10 +01002440 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_SERVER_FINISHED);
Jerry Yu8937eb42022-05-03 12:12:14 +08002441#endif
2442
Jerry Yu4d3841a2022-04-16 12:37:19 +08002443cleanup:
2444
Gilles Peskine449bd832023-01-11 14:50:10 +01002445 MBEDTLS_SSL_DEBUG_MSG(2, ("<= write encrypted extensions"));
2446 return ret;
Jerry Yu4d3841a2022-04-16 12:37:19 +08002447}
2448
Ronald Cron928cbd32022-10-04 16:14:26 +02002449#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED)
XiaokangQiana987e1d2022-05-07 01:25:58 +00002450#define SSL_CERTIFICATE_REQUEST_SEND_REQUEST 0
2451#define SSL_CERTIFICATE_REQUEST_SKIP 1
2452/* Coordination:
2453 * Check whether a CertificateRequest message should be written.
2454 * Returns a negative code on failure, or
2455 * - SSL_CERTIFICATE_REQUEST_SEND_REQUEST
2456 * - SSL_CERTIFICATE_REQUEST_SKIP
2457 * indicating if the writing of the CertificateRequest
2458 * should be skipped or not.
2459 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002460MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002461static int ssl_tls13_certificate_request_coordinate(mbedtls_ssl_context *ssl)
XiaokangQiana987e1d2022-05-07 01:25:58 +00002462{
2463 int authmode;
2464
XiaokangQian40a35232022-05-07 09:02:40 +00002465#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Gilles Peskine449bd832023-01-11 14:50:10 +01002466 if (ssl->handshake->sni_authmode != MBEDTLS_SSL_VERIFY_UNSET) {
XiaokangQian40a35232022-05-07 09:02:40 +00002467 authmode = ssl->handshake->sni_authmode;
Gilles Peskine449bd832023-01-11 14:50:10 +01002468 } else
XiaokangQian40a35232022-05-07 09:02:40 +00002469#endif
XiaokangQiana987e1d2022-05-07 01:25:58 +00002470 authmode = ssl->conf->authmode;
2471
Gilles Peskine449bd832023-01-11 14:50:10 +01002472 if (authmode == MBEDTLS_SSL_VERIFY_NONE) {
Ronald Croneac00ad2022-09-13 10:16:31 +02002473 ssl->session_negotiate->verify_result = MBEDTLS_X509_BADCERT_SKIP_VERIFY;
Gilles Peskine449bd832023-01-11 14:50:10 +01002474 return SSL_CERTIFICATE_REQUEST_SKIP;
Ronald Croneac00ad2022-09-13 10:16:31 +02002475 }
XiaokangQiana987e1d2022-05-07 01:25:58 +00002476
XiaokangQianc3017f62022-05-13 05:55:41 +00002477 ssl->handshake->certificate_request_sent = 1;
XiaokangQian189ded22022-05-10 08:12:17 +00002478
Gilles Peskine449bd832023-01-11 14:50:10 +01002479 return SSL_CERTIFICATE_REQUEST_SEND_REQUEST;
XiaokangQiana987e1d2022-05-07 01:25:58 +00002480}
2481
XiaokangQiancec9ae62022-05-06 07:28:50 +00002482/*
2483 * struct {
2484 * opaque certificate_request_context<0..2^8-1>;
2485 * Extension extensions<2..2^16-1>;
2486 * } CertificateRequest;
2487 *
2488 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002489MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002490static int ssl_tls13_write_certificate_request_body(mbedtls_ssl_context *ssl,
2491 unsigned char *buf,
2492 const unsigned char *end,
2493 size_t *out_len)
XiaokangQiancec9ae62022-05-06 07:28:50 +00002494{
2495 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
2496 unsigned char *p = buf;
XiaokangQianec6efb92022-05-06 09:53:10 +00002497 size_t output_len = 0;
XiaokangQiancec9ae62022-05-06 07:28:50 +00002498 unsigned char *p_extensions_len;
2499
2500 *out_len = 0;
2501
2502 /* Check if we have enough space:
2503 * - certificate_request_context (1 byte)
2504 * - extensions length (2 bytes)
2505 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002506 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 3);
XiaokangQiancec9ae62022-05-06 07:28:50 +00002507
2508 /*
2509 * Write certificate_request_context
2510 */
2511 /*
2512 * We use a zero length context for the normal handshake
2513 * messages. For post-authentication handshake messages
2514 * this request context would be set to a non-zero value.
2515 */
2516 *p++ = 0x0;
2517
2518 /*
2519 * Write extensions
2520 */
2521 /* The extensions must contain the signature_algorithms. */
2522 p_extensions_len = p;
2523 p += 2;
Gilles Peskine449bd832023-01-11 14:50:10 +01002524 ret = mbedtls_ssl_write_sig_alg_ext(ssl, p, end, &output_len);
2525 if (ret != 0) {
2526 return ret;
2527 }
XiaokangQiancec9ae62022-05-06 07:28:50 +00002528
XiaokangQianec6efb92022-05-06 09:53:10 +00002529 p += output_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01002530 MBEDTLS_PUT_UINT16_BE(p - p_extensions_len - 2, p_extensions_len, 0);
XiaokangQiancec9ae62022-05-06 07:28:50 +00002531
2532 *out_len = p - buf;
2533
Jerry Yu7de2ff02022-11-08 21:43:46 +08002534 MBEDTLS_SSL_PRINT_EXTS(
Gilles Peskine449bd832023-01-11 14:50:10 +01002535 3, MBEDTLS_SSL_HS_CERTIFICATE_REQUEST, ssl->handshake->sent_extensions);
Jerry Yu4b8f2f72022-10-31 13:31:22 +08002536
Gilles Peskine449bd832023-01-11 14:50:10 +01002537 return 0;
XiaokangQiancec9ae62022-05-06 07:28:50 +00002538}
2539
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002540MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002541static int ssl_tls13_write_certificate_request(mbedtls_ssl_context *ssl)
XiaokangQiancec9ae62022-05-06 07:28:50 +00002542{
2543 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
2544
Gilles Peskine449bd832023-01-11 14:50:10 +01002545 MBEDTLS_SSL_DEBUG_MSG(2, ("=> write certificate request"));
XiaokangQiancec9ae62022-05-06 07:28:50 +00002546
Gilles Peskine449bd832023-01-11 14:50:10 +01002547 MBEDTLS_SSL_PROC_CHK_NEG(ssl_tls13_certificate_request_coordinate(ssl));
XiaokangQiancec9ae62022-05-06 07:28:50 +00002548
Gilles Peskine449bd832023-01-11 14:50:10 +01002549 if (ret == SSL_CERTIFICATE_REQUEST_SEND_REQUEST) {
XiaokangQiancec9ae62022-05-06 07:28:50 +00002550 unsigned char *buf;
2551 size_t buf_len, msg_len;
2552
Xiaokang Qian9f1747b2023-03-30 02:50:04 +00002553 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_start_handshake_msg(
2554 ssl, MBEDTLS_SSL_HS_CERTIFICATE_REQUEST,
2555 &buf, &buf_len));
XiaokangQiancec9ae62022-05-06 07:28:50 +00002556
Gilles Peskine449bd832023-01-11 14:50:10 +01002557 MBEDTLS_SSL_PROC_CHK(ssl_tls13_write_certificate_request_body(
2558 ssl, buf, buf + buf_len, &msg_len));
XiaokangQiancec9ae62022-05-06 07:28:50 +00002559
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +01002560 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_add_hs_msg_to_checksum(
Xiaokang Qian9f1747b2023-03-30 02:50:04 +00002561 ssl, MBEDTLS_SSL_HS_CERTIFICATE_REQUEST,
2562 buf, msg_len));
XiaokangQiancec9ae62022-05-06 07:28:50 +00002563
Gilles Peskine449bd832023-01-11 14:50:10 +01002564 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_finish_handshake_msg(
2565 ssl, buf_len, msg_len));
2566 } else if (ret == SSL_CERTIFICATE_REQUEST_SKIP) {
2567 MBEDTLS_SSL_DEBUG_MSG(2, ("<= skip write certificate request"));
XiaokangQiancec9ae62022-05-06 07:28:50 +00002568 ret = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01002569 } else {
2570 MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
XiaokangQiancec9ae62022-05-06 07:28:50 +00002571 ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
2572 goto cleanup;
2573 }
2574
Gilles Peskine449bd832023-01-11 14:50:10 +01002575 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_SERVER_CERTIFICATE);
XiaokangQiancec9ae62022-05-06 07:28:50 +00002576cleanup:
2577
Gilles Peskine449bd832023-01-11 14:50:10 +01002578 MBEDTLS_SSL_DEBUG_MSG(2, ("<= write certificate request"));
2579 return ret;
XiaokangQiancec9ae62022-05-06 07:28:50 +00002580}
XiaokangQiancec9ae62022-05-06 07:28:50 +00002581
Jerry Yu4d3841a2022-04-16 12:37:19 +08002582/*
Jerry Yuc6e6dbf2022-04-16 19:42:57 +08002583 * Handler for MBEDTLS_SSL_SERVER_CERTIFICATE
Jerry Yu83da34e2022-04-16 13:59:52 +08002584 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002585MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002586static int ssl_tls13_write_server_certificate(mbedtls_ssl_context *ssl)
Jerry Yu83da34e2022-04-16 13:59:52 +08002587{
Jerry Yu5a26f302022-05-10 20:46:40 +08002588 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
XiaokangQianfb665a82022-06-15 03:57:21 +00002589
2590#if defined(MBEDTLS_X509_CRT_PARSE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01002591 if ((ssl_tls13_pick_key_cert(ssl) != 0) ||
2592 mbedtls_ssl_own_cert(ssl) == NULL) {
2593 MBEDTLS_SSL_DEBUG_MSG(2, ("No certificate available."));
2594 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE,
2595 MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE);
2596 return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
Jerry Yu5a26f302022-05-10 20:46:40 +08002597 }
XiaokangQianfb665a82022-06-15 03:57:21 +00002598#endif /* MBEDTLS_X509_CRT_PARSE_C */
Jerry Yu5a26f302022-05-10 20:46:40 +08002599
Gilles Peskine449bd832023-01-11 14:50:10 +01002600 ret = mbedtls_ssl_tls13_write_certificate(ssl);
2601 if (ret != 0) {
2602 return ret;
2603 }
2604 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_CERTIFICATE_VERIFY);
2605 return 0;
Jerry Yu83da34e2022-04-16 13:59:52 +08002606}
2607
2608/*
Jerry Yuc6e6dbf2022-04-16 19:42:57 +08002609 * Handler for MBEDTLS_SSL_CERTIFICATE_VERIFY
Jerry Yu83da34e2022-04-16 13:59:52 +08002610 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002611MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002612static int ssl_tls13_write_certificate_verify(mbedtls_ssl_context *ssl)
Jerry Yu83da34e2022-04-16 13:59:52 +08002613{
Gilles Peskine449bd832023-01-11 14:50:10 +01002614 int ret = mbedtls_ssl_tls13_write_certificate_verify(ssl);
2615 if (ret != 0) {
2616 return ret;
2617 }
2618 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_SERVER_FINISHED);
2619 return 0;
Jerry Yu83da34e2022-04-16 13:59:52 +08002620}
Ronald Cron928cbd32022-10-04 16:14:26 +02002621#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED */
Jerry Yu83da34e2022-04-16 13:59:52 +08002622
2623/*
Jerry Yud6e253d2022-05-18 13:59:24 +08002624 * Handler for MBEDTLS_SSL_SERVER_FINISHED
Jerry Yu69dd8d42022-04-16 12:51:26 +08002625 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002626MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002627static int ssl_tls13_write_server_finished(mbedtls_ssl_context *ssl)
Jerry Yu69dd8d42022-04-16 12:51:26 +08002628{
Jerry Yud6e253d2022-05-18 13:59:24 +08002629 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yu27bdc7c2022-04-16 13:33:27 +08002630
Gilles Peskine449bd832023-01-11 14:50:10 +01002631 ret = mbedtls_ssl_tls13_write_finished_message(ssl);
2632 if (ret != 0) {
2633 return ret;
2634 }
Jerry Yu27bdc7c2022-04-16 13:33:27 +08002635
Gilles Peskine449bd832023-01-11 14:50:10 +01002636 ret = mbedtls_ssl_tls13_compute_application_transform(ssl);
2637 if (ret != 0) {
Jerry Yue3d67cb2022-05-19 15:33:10 +08002638 MBEDTLS_SSL_PEND_FATAL_ALERT(
Gilles Peskine449bd832023-01-11 14:50:10 +01002639 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE,
2640 MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE);
2641 return ret;
Jerry Yue3d67cb2022-05-19 15:33:10 +08002642 }
XiaokangQianc3017f62022-05-13 05:55:41 +00002643
Gilles Peskine449bd832023-01-11 14:50:10 +01002644 MBEDTLS_SSL_DEBUG_MSG(1, ("Switch to handshake keys for inbound traffic"));
2645 mbedtls_ssl_set_inbound_transform(ssl, ssl->handshake->transform_handshake);
XiaokangQian189ded22022-05-10 08:12:17 +00002646
Gilles Peskine449bd832023-01-11 14:50:10 +01002647 if (ssl->handshake->certificate_request_sent) {
2648 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_CLIENT_CERTIFICATE);
2649 } else {
2650 MBEDTLS_SSL_DEBUG_MSG(2, ("skip parse certificate"));
2651 MBEDTLS_SSL_DEBUG_MSG(2, ("skip parse certificate verify"));
2652 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_CLIENT_FINISHED);
Ronald Cron19385882022-06-15 16:26:13 +02002653 }
Ronald Cron63dc4632022-05-31 14:41:53 +02002654
Gilles Peskine449bd832023-01-11 14:50:10 +01002655 return 0;
Jerry Yu69dd8d42022-04-16 12:51:26 +08002656}
2657
2658/*
Jerry Yud6e253d2022-05-18 13:59:24 +08002659 * Handler for MBEDTLS_SSL_CLIENT_FINISHED
Jerry Yu69dd8d42022-04-16 12:51:26 +08002660 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002661MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002662static int ssl_tls13_process_client_finished(mbedtls_ssl_context *ssl)
Jerry Yu69dd8d42022-04-16 12:51:26 +08002663{
Jerry Yud6e253d2022-05-18 13:59:24 +08002664 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
2665
Gilles Peskine449bd832023-01-11 14:50:10 +01002666 ret = mbedtls_ssl_tls13_process_finished_message(ssl);
2667 if (ret != 0) {
2668 return ret;
Jerry Yue3d67cb2022-05-19 15:33:10 +08002669 }
2670
Gilles Peskine449bd832023-01-11 14:50:10 +01002671 ret = mbedtls_ssl_tls13_compute_resumption_master_secret(ssl);
2672 if (ret != 0) {
Xiaokang Qian9f1747b2023-03-30 02:50:04 +00002673 MBEDTLS_SSL_DEBUG_RET(
2674 1, "mbedtls_ssl_tls13_compute_resumption_master_secret", ret);
Gilles Peskine449bd832023-01-11 14:50:10 +01002675 }
2676
2677 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_HANDSHAKE_WRAPUP);
2678 return 0;
Jerry Yu69dd8d42022-04-16 12:51:26 +08002679}
2680
2681/*
Jerry Yud6e253d2022-05-18 13:59:24 +08002682 * Handler for MBEDTLS_SSL_HANDSHAKE_WRAPUP
Jerry Yu69dd8d42022-04-16 12:51:26 +08002683 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002684MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002685static int ssl_tls13_handshake_wrapup(mbedtls_ssl_context *ssl)
Jerry Yu69dd8d42022-04-16 12:51:26 +08002686{
Gilles Peskine449bd832023-01-11 14:50:10 +01002687 MBEDTLS_SSL_DEBUG_MSG(2, ("handshake: done"));
Jerry Yu03ed50b2022-04-16 17:13:30 +08002688
Gilles Peskine449bd832023-01-11 14:50:10 +01002689 mbedtls_ssl_tls13_handshake_wrapup(ssl);
Jerry Yue67bef42022-07-07 07:29:42 +00002690
Pengyu Lv3643fdb2023-01-12 16:46:28 +08002691#if defined(MBEDTLS_SSL_SESSION_TICKETS) && \
2692 defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED)
Pengyu Lva1aa31b2022-12-13 13:49:59 +08002693/* TODO: Remove the check of SOME_PSK_ENABLED since SESSION_TICKETS requires
2694 * SOME_PSK_ENABLED to be enabled. Here is just to make CI happy. It is
2695 * expected to be resolved with issue#6395.
2696 */
Pengyu Lvc7af2c42022-12-01 16:33:00 +08002697 /* Sent NewSessionTicket message only when client supports PSK */
Pengyu Lv3643fdb2023-01-12 16:46:28 +08002698 if (mbedtls_ssl_tls13_some_psk_enabled(ssl)) {
Xiaokang Qian9f1747b2023-03-30 02:50:04 +00002699 mbedtls_ssl_handshake_set_state(
2700 ssl, MBEDTLS_SSL_TLS1_3_NEW_SESSION_TICKET);
Pengyu Lvc7af2c42022-12-01 16:33:00 +08002701 } else
Jerry Yufca4d572022-07-21 10:37:48 +08002702#endif
Pengyu Lv3643fdb2023-01-12 16:46:28 +08002703 {
2704 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_HANDSHAKE_OVER);
2705 }
Gilles Peskine449bd832023-01-11 14:50:10 +01002706 return 0;
Jerry Yu69dd8d42022-04-16 12:51:26 +08002707}
2708
2709/*
Jerry Yua8d3c502022-10-30 14:51:23 +08002710 * Handler for MBEDTLS_SSL_TLS1_3_NEW_SESSION_TICKET
Jerry Yue67bef42022-07-07 07:29:42 +00002711 */
2712#define SSL_NEW_SESSION_TICKET_SKIP 0
2713#define SSL_NEW_SESSION_TICKET_WRITE 1
2714MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002715static int ssl_tls13_write_new_session_ticket_coordinate(mbedtls_ssl_context *ssl)
Jerry Yue67bef42022-07-07 07:29:42 +00002716{
2717 /* Check whether the use of session tickets is enabled */
Gilles Peskine449bd832023-01-11 14:50:10 +01002718 if (ssl->conf->f_ticket_write == NULL) {
2719 MBEDTLS_SSL_DEBUG_MSG(2, ("NewSessionTicket: disabled,"
2720 " callback is not set"));
2721 return SSL_NEW_SESSION_TICKET_SKIP;
Jerry Yud0766ec2022-09-22 10:46:57 +08002722 }
Gilles Peskine449bd832023-01-11 14:50:10 +01002723 if (ssl->conf->new_session_tickets_count == 0) {
2724 MBEDTLS_SSL_DEBUG_MSG(2, ("NewSessionTicket: disabled,"
2725 " configured count is zero"));
2726 return SSL_NEW_SESSION_TICKET_SKIP;
Jerry Yud0766ec2022-09-22 10:46:57 +08002727 }
2728
Gilles Peskine449bd832023-01-11 14:50:10 +01002729 if (ssl->handshake->new_session_tickets_count == 0) {
2730 MBEDTLS_SSL_DEBUG_MSG(2, ("NewSessionTicket: all tickets have "
2731 "been sent."));
2732 return SSL_NEW_SESSION_TICKET_SKIP;
Jerry Yue67bef42022-07-07 07:29:42 +00002733 }
2734
Gilles Peskine449bd832023-01-11 14:50:10 +01002735 return SSL_NEW_SESSION_TICKET_WRITE;
Jerry Yue67bef42022-07-07 07:29:42 +00002736}
2737
2738#if defined(MBEDTLS_SSL_SESSION_TICKETS)
2739MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002740static int ssl_tls13_prepare_new_session_ticket(mbedtls_ssl_context *ssl,
2741 unsigned char *ticket_nonce,
2742 size_t ticket_nonce_size)
Jerry Yue67bef42022-07-07 07:29:42 +00002743{
2744 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
2745 mbedtls_ssl_session *session = ssl->session;
2746 mbedtls_ssl_ciphersuite_t *ciphersuite_info;
2747 psa_algorithm_t psa_hash_alg;
2748 int hash_length;
2749
Gilles Peskine449bd832023-01-11 14:50:10 +01002750 MBEDTLS_SSL_DEBUG_MSG(2, ("=> prepare NewSessionTicket msg"));
Jerry Yue67bef42022-07-07 07:29:42 +00002751
2752#if defined(MBEDTLS_HAVE_TIME)
Gilles Peskine449bd832023-01-11 14:50:10 +01002753 session->start = mbedtls_time(NULL);
Jerry Yue67bef42022-07-07 07:29:42 +00002754#endif
2755
Pengyu Lv9f926952022-11-17 15:22:33 +08002756 /* Set ticket_flags depends on the advertised psk key exchange mode */
Pengyu Lv80270b22023-01-12 11:54:04 +08002757 mbedtls_ssl_session_clear_ticket_flags(
Pengyu Lv9eacb442022-12-09 14:39:19 +08002758 session, MBEDTLS_SSL_TLS1_3_TICKET_FLAGS_MASK);
Pengyu Lve6487fe2022-12-06 09:30:29 +08002759#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED)
Pengyu Lv80270b22023-01-12 11:54:04 +08002760 mbedtls_ssl_session_set_ticket_flags(
Pengyu Lv9eacb442022-12-09 14:39:19 +08002761 session, ssl->handshake->tls13_kex_modes);
Pengyu Lve6487fe2022-12-06 09:30:29 +08002762#endif
Pengyu Lv4938a562023-01-16 11:28:49 +08002763 MBEDTLS_SSL_PRINT_TICKET_FLAGS(4, session->ticket_flags);
Pengyu Lv9f926952022-11-17 15:22:33 +08002764
Jerry Yue67bef42022-07-07 07:29:42 +00002765 /* Generate ticket_age_add */
Gilles Peskine449bd832023-01-11 14:50:10 +01002766 if ((ret = ssl->conf->f_rng(ssl->conf->p_rng,
2767 (unsigned char *) &session->ticket_age_add,
2768 sizeof(session->ticket_age_add)) != 0)) {
2769 MBEDTLS_SSL_DEBUG_RET(1, "generate_ticket_age_add", ret);
2770 return ret;
Jerry Yue67bef42022-07-07 07:29:42 +00002771 }
Gilles Peskine449bd832023-01-11 14:50:10 +01002772 MBEDTLS_SSL_DEBUG_MSG(3, ("ticket_age_add: %u",
2773 (unsigned int) session->ticket_age_add));
Jerry Yue67bef42022-07-07 07:29:42 +00002774
2775 /* Generate ticket_nonce */
Gilles Peskine449bd832023-01-11 14:50:10 +01002776 ret = ssl->conf->f_rng(ssl->conf->p_rng, ticket_nonce, ticket_nonce_size);
2777 if (ret != 0) {
2778 MBEDTLS_SSL_DEBUG_RET(1, "generate_ticket_nonce", ret);
2779 return ret;
Jerry Yue67bef42022-07-07 07:29:42 +00002780 }
Gilles Peskine449bd832023-01-11 14:50:10 +01002781 MBEDTLS_SSL_DEBUG_BUF(3, "ticket_nonce:",
2782 ticket_nonce, ticket_nonce_size);
Jerry Yue67bef42022-07-07 07:29:42 +00002783
2784 ciphersuite_info =
Gilles Peskine449bd832023-01-11 14:50:10 +01002785 (mbedtls_ssl_ciphersuite_t *) ssl->handshake->ciphersuite_info;
2786 psa_hash_alg = mbedtls_psa_translate_md(ciphersuite_info->mac);
2787 hash_length = PSA_HASH_LENGTH(psa_hash_alg);
2788 if (hash_length == -1 ||
2789 (size_t) hash_length > sizeof(session->resumption_key)) {
2790 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Jerry Yufca4d572022-07-21 10:37:48 +08002791 }
2792
Jerry Yue67bef42022-07-07 07:29:42 +00002793 /* In this code the psk key length equals the length of the hash */
2794 session->resumption_key_len = hash_length;
2795 session->ciphersuite = ciphersuite_info->id;
2796
2797 /* Compute resumption key
2798 *
2799 * HKDF-Expand-Label( resumption_master_secret,
2800 * "resumption", ticket_nonce, Hash.length )
2801 */
2802 ret = mbedtls_ssl_tls13_hkdf_expand_label(
Gilles Peskine449bd832023-01-11 14:50:10 +01002803 psa_hash_alg,
2804 session->app_secrets.resumption_master_secret,
2805 hash_length,
2806 MBEDTLS_SSL_TLS1_3_LBL_WITH_LEN(resumption),
2807 ticket_nonce,
2808 ticket_nonce_size,
2809 session->resumption_key,
2810 hash_length);
Jerry Yue67bef42022-07-07 07:29:42 +00002811
Gilles Peskine449bd832023-01-11 14:50:10 +01002812 if (ret != 0) {
2813 MBEDTLS_SSL_DEBUG_RET(2,
2814 "Creating the ticket-resumed PSK failed",
2815 ret);
2816 return ret;
Jerry Yue67bef42022-07-07 07:29:42 +00002817 }
Gilles Peskine449bd832023-01-11 14:50:10 +01002818 MBEDTLS_SSL_DEBUG_BUF(3, "Ticket-resumed PSK",
2819 session->resumption_key,
2820 session->resumption_key_len);
Jerry Yue67bef42022-07-07 07:29:42 +00002821
Gilles Peskine449bd832023-01-11 14:50:10 +01002822 MBEDTLS_SSL_DEBUG_BUF(3, "resumption_master_secret",
2823 session->app_secrets.resumption_master_secret,
2824 hash_length);
Jerry Yue67bef42022-07-07 07:29:42 +00002825
Gilles Peskine449bd832023-01-11 14:50:10 +01002826 return 0;
Jerry Yue67bef42022-07-07 07:29:42 +00002827}
2828
2829/* This function creates a NewSessionTicket message in the following format:
2830 *
2831 * struct {
2832 * uint32 ticket_lifetime;
2833 * uint32 ticket_age_add;
2834 * opaque ticket_nonce<0..255>;
2835 * opaque ticket<1..2^16-1>;
2836 * Extension extensions<0..2^16-2>;
2837 * } NewSessionTicket;
2838 *
2839 * The ticket inside the NewSessionTicket message is an encrypted container
2840 * carrying the necessary information so that the server is later able to
2841 * re-start the communication.
2842 *
2843 * The following fields are placed inside the ticket by the
2844 * f_ticket_write() function:
2845 *
2846 * - creation time (start)
2847 * - flags (flags)
2848 * - age add (ticket_age_add)
2849 * - key (key)
2850 * - key length (key_len)
2851 * - ciphersuite (ciphersuite)
2852 */
2853MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002854static int ssl_tls13_write_new_session_ticket_body(mbedtls_ssl_context *ssl,
2855 unsigned char *buf,
2856 unsigned char *end,
2857 size_t *out_len,
2858 unsigned char *ticket_nonce,
2859 size_t ticket_nonce_size)
Jerry Yue67bef42022-07-07 07:29:42 +00002860{
2861 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
2862 unsigned char *p = buf;
2863 mbedtls_ssl_session *session = ssl->session;
2864 size_t ticket_len;
2865 uint32_t ticket_lifetime;
2866
2867 *out_len = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01002868 MBEDTLS_SSL_DEBUG_MSG(2, ("=> write NewSessionTicket msg"));
Jerry Yue67bef42022-07-07 07:29:42 +00002869
2870 /*
2871 * ticket_lifetime 4 bytes
2872 * ticket_age_add 4 bytes
2873 * ticket_nonce 1 + ticket_nonce_size bytes
2874 * ticket >=2 bytes
2875 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002876 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 4 + 4 + 1 + ticket_nonce_size + 2);
Jerry Yue67bef42022-07-07 07:29:42 +00002877
2878 /* Generate ticket and ticket_lifetime */
Gilles Peskine449bd832023-01-11 14:50:10 +01002879 ret = ssl->conf->f_ticket_write(ssl->conf->p_ticket,
2880 session,
2881 p + 9 + ticket_nonce_size + 2,
2882 end,
2883 &ticket_len,
2884 &ticket_lifetime);
2885 if (ret != 0) {
2886 MBEDTLS_SSL_DEBUG_RET(1, "write_ticket", ret);
2887 return ret;
Jerry Yue67bef42022-07-07 07:29:42 +00002888 }
2889 /* RFC 8446 4.6.1
2890 * ticket_lifetime: Indicates the lifetime in seconds as a 32-bit
2891 * unsigned integer in network byte order from the time of ticket
2892 * issuance. Servers MUST NOT use any value greater than
2893 * 604800 seconds (7 days). The value of zero indicates that the
2894 * ticket should be discarded immediately. Clients MUST NOT cache
2895 * tickets for longer than 7 days, regardless of the ticket_lifetime,
2896 * and MAY delete tickets earlier based on local policy. A server
2897 * MAY treat a ticket as valid for a shorter period of time than what
2898 * is stated in the ticket_lifetime.
2899 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002900 if (ticket_lifetime > 604800) {
Xiaokang Qian28af5012022-10-13 08:18:19 +00002901 ticket_lifetime = 604800;
Gilles Peskine449bd832023-01-11 14:50:10 +01002902 }
2903 MBEDTLS_PUT_UINT32_BE(ticket_lifetime, p, 0);
2904 MBEDTLS_SSL_DEBUG_MSG(3, ("ticket_lifetime: %u",
2905 (unsigned int) ticket_lifetime));
Jerry Yue67bef42022-07-07 07:29:42 +00002906
2907 /* Write ticket_age_add */
Gilles Peskine449bd832023-01-11 14:50:10 +01002908 MBEDTLS_PUT_UINT32_BE(session->ticket_age_add, p, 4);
2909 MBEDTLS_SSL_DEBUG_MSG(3, ("ticket_age_add: %u",
2910 (unsigned int) session->ticket_age_add));
Jerry Yue67bef42022-07-07 07:29:42 +00002911
2912 /* Write ticket_nonce */
Gilles Peskine449bd832023-01-11 14:50:10 +01002913 p[8] = (unsigned char) ticket_nonce_size;
2914 if (ticket_nonce_size > 0) {
2915 memcpy(p + 9, ticket_nonce, ticket_nonce_size);
Jerry Yue67bef42022-07-07 07:29:42 +00002916 }
2917 p += 9 + ticket_nonce_size;
2918
2919 /* Write ticket */
Gilles Peskine449bd832023-01-11 14:50:10 +01002920 MBEDTLS_PUT_UINT16_BE(ticket_len, p, 0);
Jerry Yue67bef42022-07-07 07:29:42 +00002921 p += 2;
Gilles Peskine449bd832023-01-11 14:50:10 +01002922 MBEDTLS_SSL_DEBUG_BUF(4, "ticket", p, ticket_len);
Jerry Yue67bef42022-07-07 07:29:42 +00002923 p += ticket_len;
2924
2925 /* Ticket Extensions
2926 *
2927 * Note: We currently don't have any extensions.
2928 * Set length to zero.
2929 */
Jerry Yuedab6372022-10-31 14:37:31 +08002930 ssl->handshake->sent_extensions = MBEDTLS_SSL_EXT_MASK_NONE;
2931
Gilles Peskine449bd832023-01-11 14:50:10 +01002932 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 2);
2933 MBEDTLS_PUT_UINT16_BE(0, p, 0);
Jerry Yue67bef42022-07-07 07:29:42 +00002934 p += 2;
2935
2936 *out_len = p - buf;
Gilles Peskine449bd832023-01-11 14:50:10 +01002937 MBEDTLS_SSL_DEBUG_BUF(4, "ticket", buf, *out_len);
2938 MBEDTLS_SSL_DEBUG_MSG(2, ("<= write new session ticket"));
Jerry Yue67bef42022-07-07 07:29:42 +00002939
Jerry Yu7de2ff02022-11-08 21:43:46 +08002940 MBEDTLS_SSL_PRINT_EXTS(
Gilles Peskine449bd832023-01-11 14:50:10 +01002941 3, MBEDTLS_SSL_HS_NEW_SESSION_TICKET, ssl->handshake->sent_extensions);
Jerry Yu4b8f2f72022-10-31 13:31:22 +08002942
Gilles Peskine449bd832023-01-11 14:50:10 +01002943 return 0;
Jerry Yue67bef42022-07-07 07:29:42 +00002944}
2945
2946/*
Jerry Yua8d3c502022-10-30 14:51:23 +08002947 * Handler for MBEDTLS_SSL_TLS1_3_NEW_SESSION_TICKET
Jerry Yue67bef42022-07-07 07:29:42 +00002948 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002949static int ssl_tls13_write_new_session_ticket(mbedtls_ssl_context *ssl)
Jerry Yue67bef42022-07-07 07:29:42 +00002950{
2951 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
2952
Gilles Peskine449bd832023-01-11 14:50:10 +01002953 MBEDTLS_SSL_PROC_CHK_NEG(ssl_tls13_write_new_session_ticket_coordinate(ssl));
Jerry Yue67bef42022-07-07 07:29:42 +00002954
Gilles Peskine449bd832023-01-11 14:50:10 +01002955 if (ret == SSL_NEW_SESSION_TICKET_WRITE) {
Jerry Yue67bef42022-07-07 07:29:42 +00002956 unsigned char ticket_nonce[MBEDTLS_SSL_TLS1_3_TICKET_NONCE_LENGTH];
2957 unsigned char *buf;
2958 size_t buf_len, msg_len;
2959
Gilles Peskine449bd832023-01-11 14:50:10 +01002960 MBEDTLS_SSL_PROC_CHK(ssl_tls13_prepare_new_session_ticket(
2961 ssl, ticket_nonce, sizeof(ticket_nonce)));
Jerry Yue67bef42022-07-07 07:29:42 +00002962
Xiaokang Qian9f1747b2023-03-30 02:50:04 +00002963 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_start_handshake_msg(
2964 ssl, MBEDTLS_SSL_HS_NEW_SESSION_TICKET,
2965 &buf, &buf_len));
Jerry Yue67bef42022-07-07 07:29:42 +00002966
Gilles Peskine449bd832023-01-11 14:50:10 +01002967 MBEDTLS_SSL_PROC_CHK(ssl_tls13_write_new_session_ticket_body(
2968 ssl, buf, buf + buf_len, &msg_len,
2969 ticket_nonce, sizeof(ticket_nonce)));
Jerry Yue67bef42022-07-07 07:29:42 +00002970
Gilles Peskine449bd832023-01-11 14:50:10 +01002971 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_finish_handshake_msg(
2972 ssl, buf_len, msg_len));
Jerry Yufca4d572022-07-21 10:37:48 +08002973
Jerry Yu359e65f2022-09-22 23:47:43 +08002974 /* Limit session tickets count to one when resumption connection.
2975 *
2976 * See document of mbedtls_ssl_conf_new_session_tickets.
2977 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002978 if (ssl->handshake->resume == 1) {
Jerry Yu359e65f2022-09-22 23:47:43 +08002979 ssl->handshake->new_session_tickets_count = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01002980 } else {
Jerry Yu359e65f2022-09-22 23:47:43 +08002981 ssl->handshake->new_session_tickets_count--;
Gilles Peskine449bd832023-01-11 14:50:10 +01002982 }
Jerry Yub7e3fa72022-09-22 11:07:18 +08002983
Jerry Yua8d3c502022-10-30 14:51:23 +08002984 mbedtls_ssl_handshake_set_state(
Gilles Peskine449bd832023-01-11 14:50:10 +01002985 ssl, MBEDTLS_SSL_TLS1_3_NEW_SESSION_TICKET_FLUSH);
2986 } else {
2987 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_HANDSHAKE_OVER);
Jerry Yue67bef42022-07-07 07:29:42 +00002988 }
2989
Jerry Yue67bef42022-07-07 07:29:42 +00002990cleanup:
2991
Gilles Peskine449bd832023-01-11 14:50:10 +01002992 return ret;
Jerry Yue67bef42022-07-07 07:29:42 +00002993}
2994#endif /* MBEDTLS_SSL_SESSION_TICKETS */
2995
2996/*
XiaokangQiane8ff3502022-04-22 02:34:40 +00002997 * TLS 1.3 State Machine -- server side
XiaokangQian7807f9f2022-02-15 10:04:37 +00002998 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002999int mbedtls_ssl_tls13_handshake_server_step(mbedtls_ssl_context *ssl)
Jerry Yub9930e72021-08-06 17:11:51 +08003000{
XiaokangQian08037552022-04-20 07:16:41 +00003001 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
XiaokangQian7807f9f2022-02-15 10:04:37 +00003002
Gilles Peskine449bd832023-01-11 14:50:10 +01003003 if (ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER || ssl->handshake == NULL) {
3004 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
3005 }
XiaokangQian7807f9f2022-02-15 10:04:37 +00003006
Gilles Peskine449bd832023-01-11 14:50:10 +01003007 MBEDTLS_SSL_DEBUG_MSG(2, ("tls13 server state: %s(%d)",
3008 mbedtls_ssl_states_str(ssl->state),
3009 ssl->state));
Jerry Yu6e81b272021-09-27 11:16:17 +08003010
Gilles Peskine449bd832023-01-11 14:50:10 +01003011 switch (ssl->state) {
XiaokangQian7807f9f2022-02-15 10:04:37 +00003012 /* start state */
3013 case MBEDTLS_SSL_HELLO_REQUEST:
Gilles Peskine449bd832023-01-11 14:50:10 +01003014 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_CLIENT_HELLO);
XiaokangQian08037552022-04-20 07:16:41 +00003015 ret = 0;
XiaokangQian7807f9f2022-02-15 10:04:37 +00003016 break;
3017
XiaokangQian7807f9f2022-02-15 10:04:37 +00003018 case MBEDTLS_SSL_CLIENT_HELLO:
Gilles Peskine449bd832023-01-11 14:50:10 +01003019 ret = ssl_tls13_process_client_hello(ssl);
3020 if (ret != 0) {
3021 MBEDTLS_SSL_DEBUG_RET(1, "ssl_tls13_process_client_hello", ret);
3022 }
Jerry Yuf41553b2022-05-09 22:20:30 +08003023 break;
XiaokangQian7807f9f2022-02-15 10:04:37 +00003024
Jerry Yuf41553b2022-05-09 22:20:30 +08003025 case MBEDTLS_SSL_HELLO_RETRY_REQUEST:
Gilles Peskine449bd832023-01-11 14:50:10 +01003026 ret = ssl_tls13_write_hello_retry_request(ssl);
3027 if (ret != 0) {
3028 MBEDTLS_SSL_DEBUG_RET(1, "ssl_tls13_write_hello_retry_request", ret);
3029 return ret;
Jerry Yuf41553b2022-05-09 22:20:30 +08003030 }
XiaokangQian7807f9f2022-02-15 10:04:37 +00003031 break;
3032
Jerry Yu5b64ae92022-03-30 17:15:02 +08003033 case MBEDTLS_SSL_SERVER_HELLO:
Gilles Peskine449bd832023-01-11 14:50:10 +01003034 ret = ssl_tls13_write_server_hello(ssl);
Jerry Yu5b64ae92022-03-30 17:15:02 +08003035 break;
3036
Xiaofei Baicba64af2022-02-15 10:00:56 +00003037 case MBEDTLS_SSL_ENCRYPTED_EXTENSIONS:
Gilles Peskine449bd832023-01-11 14:50:10 +01003038 ret = ssl_tls13_write_encrypted_extensions(ssl);
3039 if (ret != 0) {
3040 MBEDTLS_SSL_DEBUG_RET(1, "ssl_tls13_write_encrypted_extensions", ret);
3041 return ret;
Xiaofei Baicba64af2022-02-15 10:00:56 +00003042 }
Jerry Yu48330562022-05-06 21:35:44 +08003043 break;
Jerry Yu6a2cd9e2022-05-05 11:14:19 +08003044
Ronald Cron928cbd32022-10-04 16:14:26 +02003045#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED)
Xiaofei Bai9ca09d42022-02-14 12:57:18 +00003046 case MBEDTLS_SSL_CERTIFICATE_REQUEST:
Gilles Peskine449bd832023-01-11 14:50:10 +01003047 ret = ssl_tls13_write_certificate_request(ssl);
Xiaofei Bai9ca09d42022-02-14 12:57:18 +00003048 break;
Xiaofei Bai9ca09d42022-02-14 12:57:18 +00003049
Jerry Yu83da34e2022-04-16 13:59:52 +08003050 case MBEDTLS_SSL_SERVER_CERTIFICATE:
Gilles Peskine449bd832023-01-11 14:50:10 +01003051 ret = ssl_tls13_write_server_certificate(ssl);
Jerry Yu83da34e2022-04-16 13:59:52 +08003052 break;
3053
3054 case MBEDTLS_SSL_CERTIFICATE_VERIFY:
Gilles Peskine449bd832023-01-11 14:50:10 +01003055 ret = ssl_tls13_write_certificate_verify(ssl);
Jerry Yu83da34e2022-04-16 13:59:52 +08003056 break;
Ronald Cron928cbd32022-10-04 16:14:26 +02003057#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED */
Jerry Yu83da34e2022-04-16 13:59:52 +08003058
Gilles Peskine449bd832023-01-11 14:50:10 +01003059 /*
3060 * Injection of dummy-CCS's for middlebox compatibility
3061 */
Gabor Mezei7b39bf12022-05-24 16:04:14 +02003062#if defined(MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE)
Gabor Mezeif7044ea2022-06-28 16:01:49 +02003063 case MBEDTLS_SSL_SERVER_CCS_AFTER_HELLO_RETRY_REQUEST:
Gilles Peskine449bd832023-01-11 14:50:10 +01003064 ret = mbedtls_ssl_tls13_write_change_cipher_spec(ssl);
3065 if (ret == 0) {
3066 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_CLIENT_HELLO);
3067 }
Gabor Mezei7b39bf12022-05-24 16:04:14 +02003068 break;
3069
3070 case MBEDTLS_SSL_SERVER_CCS_AFTER_SERVER_HELLO:
Gilles Peskine449bd832023-01-11 14:50:10 +01003071 ret = mbedtls_ssl_tls13_write_change_cipher_spec(ssl);
3072 if (ret == 0) {
3073 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_ENCRYPTED_EXTENSIONS);
3074 }
Gabor Mezei7b39bf12022-05-24 16:04:14 +02003075 break;
3076#endif /* MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE */
3077
Jerry Yu69dd8d42022-04-16 12:51:26 +08003078 case MBEDTLS_SSL_SERVER_FINISHED:
Gilles Peskine449bd832023-01-11 14:50:10 +01003079 ret = ssl_tls13_write_server_finished(ssl);
Jerry Yu69dd8d42022-04-16 12:51:26 +08003080 break;
3081
3082 case MBEDTLS_SSL_CLIENT_FINISHED:
Gilles Peskine449bd832023-01-11 14:50:10 +01003083 ret = ssl_tls13_process_client_finished(ssl);
Jerry Yu69dd8d42022-04-16 12:51:26 +08003084 break;
3085
Jerry Yu69dd8d42022-04-16 12:51:26 +08003086 case MBEDTLS_SSL_HANDSHAKE_WRAPUP:
Gilles Peskine449bd832023-01-11 14:50:10 +01003087 ret = ssl_tls13_handshake_wrapup(ssl);
Jerry Yu69dd8d42022-04-16 12:51:26 +08003088 break;
3089
Ronald Cron766c0cd2022-10-18 12:17:11 +02003090#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED)
XiaokangQian6b916b12022-04-25 07:29:34 +00003091 case MBEDTLS_SSL_CLIENT_CERTIFICATE:
Gilles Peskine449bd832023-01-11 14:50:10 +01003092 ret = mbedtls_ssl_tls13_process_certificate(ssl);
3093 if (ret == 0) {
3094 if (ssl->session_negotiate->peer_cert != NULL) {
Ronald Cron209cae92022-06-07 10:30:19 +02003095 mbedtls_ssl_handshake_set_state(
Gilles Peskine449bd832023-01-11 14:50:10 +01003096 ssl, MBEDTLS_SSL_CLIENT_CERTIFICATE_VERIFY);
3097 } else {
3098 MBEDTLS_SSL_DEBUG_MSG(2, ("skip parse certificate verify"));
Ronald Cron209cae92022-06-07 10:30:19 +02003099 mbedtls_ssl_handshake_set_state(
Gilles Peskine449bd832023-01-11 14:50:10 +01003100 ssl, MBEDTLS_SSL_CLIENT_FINISHED);
Ronald Cron19385882022-06-15 16:26:13 +02003101 }
XiaokangQian6b916b12022-04-25 07:29:34 +00003102 }
3103 break;
3104
3105 case MBEDTLS_SSL_CLIENT_CERTIFICATE_VERIFY:
Gilles Peskine449bd832023-01-11 14:50:10 +01003106 ret = mbedtls_ssl_tls13_process_certificate_verify(ssl);
3107 if (ret == 0) {
XiaokangQian6b916b12022-04-25 07:29:34 +00003108 mbedtls_ssl_handshake_set_state(
Gilles Peskine449bd832023-01-11 14:50:10 +01003109 ssl, MBEDTLS_SSL_CLIENT_FINISHED);
XiaokangQian6b916b12022-04-25 07:29:34 +00003110 }
3111 break;
Ronald Cron766c0cd2022-10-18 12:17:11 +02003112#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED */
XiaokangQian6b916b12022-04-25 07:29:34 +00003113
Jerry Yue67bef42022-07-07 07:29:42 +00003114#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Jerry Yua8d3c502022-10-30 14:51:23 +08003115 case MBEDTLS_SSL_TLS1_3_NEW_SESSION_TICKET:
Gilles Peskine449bd832023-01-11 14:50:10 +01003116 ret = ssl_tls13_write_new_session_ticket(ssl);
3117 if (ret != 0) {
3118 MBEDTLS_SSL_DEBUG_RET(1,
3119 "ssl_tls13_write_new_session_ticket ",
3120 ret);
Jerry Yue67bef42022-07-07 07:29:42 +00003121 }
3122 break;
Jerry Yua8d3c502022-10-30 14:51:23 +08003123 case MBEDTLS_SSL_TLS1_3_NEW_SESSION_TICKET_FLUSH:
Jerry Yue67bef42022-07-07 07:29:42 +00003124 /* This state is necessary to do the flush of the New Session
Jerry Yua8d3c502022-10-30 14:51:23 +08003125 * Ticket message written in MBEDTLS_SSL_TLS1_3_NEW_SESSION_TICKET
Jerry Yue67bef42022-07-07 07:29:42 +00003126 * as part of ssl_prepare_handshake_step.
3127 */
3128 ret = 0;
Jerry Yud4e75002022-08-09 13:33:50 +08003129
Gilles Peskine449bd832023-01-11 14:50:10 +01003130 if (ssl->handshake->new_session_tickets_count == 0) {
3131 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_HANDSHAKE_OVER);
3132 } else {
Xiaokang Qian9f1747b2023-03-30 02:50:04 +00003133 mbedtls_ssl_handshake_set_state(
3134 ssl, MBEDTLS_SSL_TLS1_3_NEW_SESSION_TICKET);
Gilles Peskine449bd832023-01-11 14:50:10 +01003135 }
Jerry Yue67bef42022-07-07 07:29:42 +00003136 break;
3137
3138#endif /* MBEDTLS_SSL_SESSION_TICKETS */
3139
XiaokangQian7807f9f2022-02-15 10:04:37 +00003140 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01003141 MBEDTLS_SSL_DEBUG_MSG(1, ("invalid state %d", ssl->state));
3142 return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
XiaokangQian7807f9f2022-02-15 10:04:37 +00003143 }
3144
Gilles Peskine449bd832023-01-11 14:50:10 +01003145 return ret;
Jerry Yub9930e72021-08-06 17:11:51 +08003146}
Jerry Yu3cc4c2a2021-08-06 16:29:08 +08003147
Jerry Yufb4b6472022-01-27 15:03:26 +08003148#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_PROTO_TLS1_3 */