blob: de60a7ae8fd0809ca9c7b55ab41e43f7b484a4ca [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{
Jerry Yu95699e72022-08-21 19:22:23 +0800261 ((void) session);
262 ((void) obfuscated_ticket_age);
Jerry Yu5725f1c2022-08-21 17:27:16 +0800263 *psk_type = MBEDTLS_SSL_TLS1_3_PSK_EXTERNAL;
Jerry Yu95699e72022-08-21 19:22:23 +0800264
Gilles Peskine449bd832023-01-11 14:50:10 +0100265 MBEDTLS_SSL_DEBUG_BUF(4, "identity", identity, identity_len);
Jerry Yu95699e72022-08-21 19:22:23 +0800266 ssl->handshake->resume = 0;
267
Jerry Yu95699e72022-08-21 19:22:23 +0800268#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Gilles Peskine449bd832023-01-11 14:50:10 +0100269 if (ssl_tls13_offered_psks_check_identity_match_ticket(
Jerry Yu8d4bbba2022-09-13 14:15:48 +0800270 ssl, identity, identity_len, obfuscated_ticket_age,
Gilles Peskine449bd832023-01-11 14:50:10 +0100271 session) == SSL_TLS1_3_OFFERED_PSK_MATCH) {
Jerry Yu95699e72022-08-21 19:22:23 +0800272 ssl->handshake->resume = 1;
273 *psk_type = MBEDTLS_SSL_TLS1_3_PSK_RESUMPTION;
Gilles Peskine449bd832023-01-11 14:50:10 +0100274 mbedtls_ssl_set_hs_psk(ssl,
Jerry Yu0a55cc62022-09-15 16:15:06 +0800275 session->resumption_key,
Gilles Peskine449bd832023-01-11 14:50:10 +0100276 session->resumption_key_len);
277
278 MBEDTLS_SSL_DEBUG_BUF(4, "Ticket-resumed PSK:",
279 session->resumption_key,
280 session->resumption_key_len);
281 MBEDTLS_SSL_DEBUG_MSG(4, ("ticket: obfuscated_ticket_age: %u",
282 (unsigned) obfuscated_ticket_age));
283 return SSL_TLS1_3_OFFERED_PSK_MATCH;
Jerry Yu95699e72022-08-21 19:22:23 +0800284 }
285#endif /* MBEDTLS_SSL_SESSION_TICKETS */
286
Jerry Yu1c105562022-07-10 06:32:38 +0000287 /* Check identity with external configured function */
Gilles Peskine449bd832023-01-11 14:50:10 +0100288 if (ssl->conf->f_psk != NULL) {
289 if (ssl->conf->f_psk(
290 ssl->conf->p_psk, ssl, identity, identity_len) == 0) {
291 return SSL_TLS1_3_OFFERED_PSK_MATCH;
Jerry Yu1c105562022-07-10 06:32:38 +0000292 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100293 return SSL_TLS1_3_OFFERED_PSK_NOT_MATCH;
Jerry Yu1c105562022-07-10 06:32:38 +0000294 }
295
Gilles Peskine449bd832023-01-11 14:50:10 +0100296 MBEDTLS_SSL_DEBUG_BUF(5, "identity", identity, identity_len);
Jerry Yu1c105562022-07-10 06:32:38 +0000297 /* Check identity with pre-configured psk */
Gilles Peskine449bd832023-01-11 14:50:10 +0100298 if (ssl->conf->psk_identity != NULL &&
Jerry Yu2f0abc92022-07-22 19:34:48 +0800299 identity_len == ssl->conf->psk_identity_len &&
Gilles Peskine449bd832023-01-11 14:50:10 +0100300 mbedtls_ct_memcmp(ssl->conf->psk_identity,
301 identity, identity_len) == 0) {
302 mbedtls_ssl_set_hs_psk(ssl, ssl->conf->psk, ssl->conf->psk_len);
303 return SSL_TLS1_3_OFFERED_PSK_MATCH;
Jerry Yu1c105562022-07-10 06:32:38 +0000304 }
305
Gilles Peskine449bd832023-01-11 14:50:10 +0100306 return SSL_TLS1_3_OFFERED_PSK_NOT_MATCH;
Jerry Yu1c105562022-07-10 06:32:38 +0000307}
308
Jerry Yu6e74a7e2022-07-20 20:49:32 +0800309MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100310static int ssl_tls13_offered_psks_check_binder_match(mbedtls_ssl_context *ssl,
311 const unsigned char *binder,
312 size_t binder_len,
313 int psk_type,
314 psa_algorithm_t psk_hash_alg)
Jerry Yu1c105562022-07-10 06:32:38 +0000315{
316 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yu96a2e362022-07-21 15:11:34 +0800317
Jerry Yudaf375a2022-07-20 21:31:43 +0800318 unsigned char transcript[PSA_HASH_MAX_SIZE];
Jerry Yu1c105562022-07-10 06:32:38 +0000319 size_t transcript_len;
Jerry Yu2f0abc92022-07-22 19:34:48 +0800320 unsigned char *psk;
Jerry Yu96a2e362022-07-21 15:11:34 +0800321 size_t psk_len;
Jerry Yudaf375a2022-07-20 21:31:43 +0800322 unsigned char server_computed_binder[PSA_HASH_MAX_SIZE];
Jerry Yu1c105562022-07-10 06:32:38 +0000323
Jerry Yu1c105562022-07-10 06:32:38 +0000324 /* Get current state of handshake transcript. */
Jerry Yu29d9faa2022-08-23 17:52:45 +0800325 ret = mbedtls_ssl_get_handshake_transcript(
Gilles Peskine449bd832023-01-11 14:50:10 +0100326 ssl, mbedtls_hash_info_md_from_psa(psk_hash_alg),
327 transcript, sizeof(transcript), &transcript_len);
328 if (ret != 0) {
329 return ret;
330 }
Jerry Yu1c105562022-07-10 06:32:38 +0000331
Gilles Peskine449bd832023-01-11 14:50:10 +0100332 ret = mbedtls_ssl_tls13_export_handshake_psk(ssl, &psk, &psk_len);
333 if (ret != 0) {
334 return ret;
335 }
Jerry Yu96a2e362022-07-21 15:11:34 +0800336
Gilles Peskine449bd832023-01-11 14:50:10 +0100337 ret = mbedtls_ssl_tls13_create_psk_binder(ssl, psk_hash_alg,
338 psk, psk_len, psk_type,
339 transcript,
340 server_computed_binder);
Jerry Yu96a2e362022-07-21 15:11:34 +0800341#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +0100342 mbedtls_free((void *) psk);
Jerry Yu96a2e362022-07-21 15:11:34 +0800343#endif
Gilles Peskine449bd832023-01-11 14:50:10 +0100344 if (ret != 0) {
345 MBEDTLS_SSL_DEBUG_MSG(1, ("PSK binder calculation failed."));
346 return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
Jerry Yu1c105562022-07-10 06:32:38 +0000347 }
348
Gilles Peskine449bd832023-01-11 14:50:10 +0100349 MBEDTLS_SSL_DEBUG_BUF(3, "psk binder ( computed ): ",
350 server_computed_binder, transcript_len);
351 MBEDTLS_SSL_DEBUG_BUF(3, "psk binder ( received ): ", binder, binder_len);
Jerry Yu1c105562022-07-10 06:32:38 +0000352
Gilles Peskine449bd832023-01-11 14:50:10 +0100353 if (mbedtls_ct_memcmp(server_computed_binder, binder, binder_len) == 0) {
354 return SSL_TLS1_3_OFFERED_PSK_MATCH;
Jerry Yu1c105562022-07-10 06:32:38 +0000355 }
356
Gilles Peskine449bd832023-01-11 14:50:10 +0100357 mbedtls_platform_zeroize(server_computed_binder,
358 sizeof(server_computed_binder));
359 return SSL_TLS1_3_OFFERED_PSK_NOT_MATCH;
Jerry Yu1c105562022-07-10 06:32:38 +0000360}
Jerry Yu96a2e362022-07-21 15:11:34 +0800361
Jerry Yu5725f1c2022-08-21 17:27:16 +0800362MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yuf35ba382022-08-23 17:58:26 +0800363static int ssl_tls13_select_ciphersuite_for_psk(
Gilles Peskine449bd832023-01-11 14:50:10 +0100364 mbedtls_ssl_context *ssl,
365 const unsigned char *cipher_suites,
366 const unsigned char *cipher_suites_end,
367 uint16_t *selected_ciphersuite,
368 const mbedtls_ssl_ciphersuite_t **selected_ciphersuite_info)
Jerry Yu5725f1c2022-08-21 17:27:16 +0800369{
Jerry Yuf35ba382022-08-23 17:58:26 +0800370 psa_algorithm_t psk_hash_alg = PSA_ALG_SHA_256;
Jerry Yu5725f1c2022-08-21 17:27:16 +0800371
Jerry Yu0baf9072022-08-25 11:21:04 +0800372 *selected_ciphersuite = 0;
373 *selected_ciphersuite_info = NULL;
374
Jerry Yuf35ba382022-08-23 17:58:26 +0800375 /* RFC 8446, page 55.
376 *
377 * For externally established PSKs, the Hash algorithm MUST be set when the
378 * PSK is established or default to SHA-256 if no such algorithm is defined.
379 *
380 */
Jerry Yu5725f1c2022-08-21 17:27:16 +0800381
Jerry Yu5725f1c2022-08-21 17:27:16 +0800382 /*
383 * Search for a matching ciphersuite
384 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100385 for (const unsigned char *p = cipher_suites;
386 p < cipher_suites_end; p += 2) {
Jerry Yu5725f1c2022-08-21 17:27:16 +0800387 uint16_t cipher_suite;
Jerry Yuf35ba382022-08-23 17:58:26 +0800388 const mbedtls_ssl_ciphersuite_t *ciphersuite_info;
Jerry Yu5725f1c2022-08-21 17:27:16 +0800389
Gilles Peskine449bd832023-01-11 14:50:10 +0100390 cipher_suite = MBEDTLS_GET_UINT16_BE(p, 0);
391 ciphersuite_info = ssl_tls13_validate_peer_ciphersuite(ssl,
392 cipher_suite);
393 if (ciphersuite_info == NULL) {
Jerry Yu5725f1c2022-08-21 17:27:16 +0800394 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +0100395 }
Jerry Yu5725f1c2022-08-21 17:27:16 +0800396
Jerry Yu5725f1c2022-08-21 17:27:16 +0800397 /* MAC of selected ciphersuite MUST be same with PSK binder if exist.
398 * Otherwise, client should reject.
399 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100400 if (psk_hash_alg == mbedtls_psa_translate_md(ciphersuite_info->mac)) {
Jerry Yuf35ba382022-08-23 17:58:26 +0800401 *selected_ciphersuite = cipher_suite;
402 *selected_ciphersuite_info = ciphersuite_info;
Gilles Peskine449bd832023-01-11 14:50:10 +0100403 return 0;
Jerry Yuf35ba382022-08-23 17:58:26 +0800404 }
405 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100406 MBEDTLS_SSL_DEBUG_MSG(2, ("No matched ciphersuite"));
407 return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
Jerry Yuf35ba382022-08-23 17:58:26 +0800408}
Jerry Yu5725f1c2022-08-21 17:27:16 +0800409
Jerry Yu82534862022-08-30 10:42:33 +0800410#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Jerry Yuf35ba382022-08-23 17:58:26 +0800411MBEDTLS_CHECK_RETURN_CRITICAL
412static int ssl_tls13_select_ciphersuite_for_resumption(
Gilles Peskine449bd832023-01-11 14:50:10 +0100413 mbedtls_ssl_context *ssl,
414 const unsigned char *cipher_suites,
415 const unsigned char *cipher_suites_end,
416 mbedtls_ssl_session *session,
417 uint16_t *selected_ciphersuite,
418 const mbedtls_ssl_ciphersuite_t **selected_ciphersuite_info)
Jerry Yuf35ba382022-08-23 17:58:26 +0800419{
Jerry Yu82534862022-08-30 10:42:33 +0800420
Jerry Yuf35ba382022-08-23 17:58:26 +0800421 *selected_ciphersuite = 0;
422 *selected_ciphersuite_info = NULL;
Gilles Peskine449bd832023-01-11 14:50:10 +0100423 for (const unsigned char *p = cipher_suites; p < cipher_suites_end; p += 2) {
424 uint16_t cipher_suite = MBEDTLS_GET_UINT16_BE(p, 0);
Jerry Yu82534862022-08-30 10:42:33 +0800425 const mbedtls_ssl_ciphersuite_t *ciphersuite_info;
426
Gilles Peskine449bd832023-01-11 14:50:10 +0100427 if (cipher_suite != session->ciphersuite) {
Jerry Yu82534862022-08-30 10:42:33 +0800428 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +0100429 }
Jerry Yu82534862022-08-30 10:42:33 +0800430
Gilles Peskine449bd832023-01-11 14:50:10 +0100431 ciphersuite_info = ssl_tls13_validate_peer_ciphersuite(ssl,
432 cipher_suite);
433 if (ciphersuite_info == NULL) {
Jerry Yu82534862022-08-30 10:42:33 +0800434 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +0100435 }
Jerry Yu82534862022-08-30 10:42:33 +0800436
Jerry Yu4746b102022-09-13 11:11:48 +0800437 *selected_ciphersuite = cipher_suite;
Jerry Yu82534862022-08-30 10:42:33 +0800438 *selected_ciphersuite_info = ciphersuite_info;
439
Gilles Peskine449bd832023-01-11 14:50:10 +0100440 return 0;
Jerry Yu82534862022-08-30 10:42:33 +0800441 }
442
Gilles Peskine449bd832023-01-11 14:50:10 +0100443 return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
Jerry Yu5725f1c2022-08-21 17:27:16 +0800444}
Jerry Yuf35ba382022-08-23 17:58:26 +0800445
Jerry Yu82534862022-08-30 10:42:33 +0800446MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100447static int ssl_tls13_session_copy_ticket(mbedtls_ssl_session *dst,
448 const mbedtls_ssl_session *src)
Jerry Yu82534862022-08-30 10:42:33 +0800449{
Jerry Yu82534862022-08-30 10:42:33 +0800450 dst->ticket_age_add = src->ticket_age_add;
451 dst->ticket_flags = src->ticket_flags;
452 dst->resumption_key_len = src->resumption_key_len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100453 if (src->resumption_key_len == 0) {
454 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
455 }
456 memcpy(dst->resumption_key, src->resumption_key, src->resumption_key_len);
Jerry Yu4746b102022-09-13 11:11:48 +0800457
Gilles Peskine449bd832023-01-11 14:50:10 +0100458 return 0;
Jerry Yu82534862022-08-30 10:42:33 +0800459}
460#endif /* MBEDTLS_SSL_SESSION_TICKETS */
461
Jerry Yu1c105562022-07-10 06:32:38 +0000462/* Parser for pre_shared_key extension in client hello
463 * struct {
464 * opaque identity<1..2^16-1>;
465 * uint32 obfuscated_ticket_age;
466 * } PskIdentity;
467 *
468 * opaque PskBinderEntry<32..255>;
469 *
470 * struct {
471 * PskIdentity identities<7..2^16-1>;
472 * PskBinderEntry binders<33..2^16-1>;
473 * } OfferedPsks;
474 *
475 * struct {
476 * select (Handshake.msg_type) {
477 * case client_hello: OfferedPsks;
478 * ....
479 * };
480 * } PreSharedKeyExtension;
481 */
Jerry Yu6e74a7e2022-07-20 20:49:32 +0800482MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100483static int ssl_tls13_parse_pre_shared_key_ext(mbedtls_ssl_context *ssl,
484 const unsigned char *pre_shared_key_ext,
485 const unsigned char *pre_shared_key_ext_end,
486 const unsigned char *ciphersuites,
487 const unsigned char *ciphersuites_end)
Jerry Yu1c105562022-07-10 06:32:38 +0000488{
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +0100489 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yu29d9faa2022-08-23 17:52:45 +0800490 const unsigned char *identities = pre_shared_key_ext;
Jerry Yu568ec252022-07-22 21:27:34 +0800491 const unsigned char *p_identity_len;
492 size_t identities_len;
Jerry Yu1c105562022-07-10 06:32:38 +0000493 const unsigned char *identities_end;
Jerry Yu568ec252022-07-22 21:27:34 +0800494 const unsigned char *binders;
495 const unsigned char *p_binder_len;
496 size_t binders_len;
Jerry Yu1c105562022-07-10 06:32:38 +0000497 const unsigned char *binders_end;
Jerry Yu96a2e362022-07-21 15:11:34 +0800498 int matched_identity = -1;
499 int identity_id = -1;
Jerry Yu1c105562022-07-10 06:32:38 +0000500
Gilles Peskine449bd832023-01-11 14:50:10 +0100501 MBEDTLS_SSL_DEBUG_BUF(3, "pre_shared_key extension",
502 pre_shared_key_ext,
503 pre_shared_key_ext_end - pre_shared_key_ext);
Jerry Yu1c105562022-07-10 06:32:38 +0000504
Jerry Yu96a2e362022-07-21 15:11:34 +0800505 /* identities_len 2 bytes
506 * identities_data >= 7 bytes
507 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100508 MBEDTLS_SSL_CHK_BUF_READ_PTR(identities, pre_shared_key_ext_end, 7 + 2);
509 identities_len = MBEDTLS_GET_UINT16_BE(identities, 0);
Jerry Yu568ec252022-07-22 21:27:34 +0800510 p_identity_len = identities + 2;
Gilles Peskine449bd832023-01-11 14:50:10 +0100511 MBEDTLS_SSL_CHK_BUF_READ_PTR(p_identity_len, pre_shared_key_ext_end,
512 identities_len);
Jerry Yu568ec252022-07-22 21:27:34 +0800513 identities_end = p_identity_len + identities_len;
Jerry Yu1c105562022-07-10 06:32:38 +0000514
Jerry Yu96a2e362022-07-21 15:11:34 +0800515 /* binders_len 2 bytes
516 * binders >= 33 bytes
517 */
Jerry Yu568ec252022-07-22 21:27:34 +0800518 binders = identities_end;
Gilles Peskine449bd832023-01-11 14:50:10 +0100519 MBEDTLS_SSL_CHK_BUF_READ_PTR(binders, pre_shared_key_ext_end, 33 + 2);
520 binders_len = MBEDTLS_GET_UINT16_BE(binders, 0);
Jerry Yu568ec252022-07-22 21:27:34 +0800521 p_binder_len = binders + 2;
Gilles Peskine449bd832023-01-11 14:50:10 +0100522 MBEDTLS_SSL_CHK_BUF_READ_PTR(p_binder_len, pre_shared_key_ext_end, binders_len);
Jerry Yu568ec252022-07-22 21:27:34 +0800523 binders_end = p_binder_len + binders_len;
Jerry Yu1c105562022-07-10 06:32:38 +0000524
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +0100525 ret = ssl->handshake->update_checksum(ssl, pre_shared_key_ext,
526 identities_end - pre_shared_key_ext);
527 if (0 != ret) {
528 MBEDTLS_SSL_DEBUG_RET(1, ("update_checksum"), ret);
529 return ret;
530 }
Jerry Yu96a2e362022-07-21 15:11:34 +0800531
Gilles Peskine449bd832023-01-11 14:50:10 +0100532 while (p_identity_len < identities_end && p_binder_len < binders_end) {
Jerry Yu1c105562022-07-10 06:32:38 +0000533 const unsigned char *identity;
Jerry Yu568ec252022-07-22 21:27:34 +0800534 size_t identity_len;
Jerry Yu82534862022-08-30 10:42:33 +0800535 uint32_t obfuscated_ticket_age;
Jerry Yu96a2e362022-07-21 15:11:34 +0800536 const unsigned char *binder;
Jerry Yu568ec252022-07-22 21:27:34 +0800537 size_t binder_len;
Jerry Yu5725f1c2022-08-21 17:27:16 +0800538 int psk_type;
539 uint16_t cipher_suite;
Jerry Yu29d9faa2022-08-23 17:52:45 +0800540 const mbedtls_ssl_ciphersuite_t *ciphersuite_info;
Jerry Yu82534862022-08-30 10:42:33 +0800541#if defined(MBEDTLS_SSL_SESSION_TICKETS)
542 mbedtls_ssl_session session;
Gilles Peskine449bd832023-01-11 14:50:10 +0100543 mbedtls_ssl_session_init(&session);
Jerry Yu82534862022-08-30 10:42:33 +0800544#endif
Jerry Yubb852022022-07-20 21:10:44 +0800545
Gilles Peskine449bd832023-01-11 14:50:10 +0100546 MBEDTLS_SSL_CHK_BUF_READ_PTR(p_identity_len, identities_end, 2 + 1 + 4);
547 identity_len = MBEDTLS_GET_UINT16_BE(p_identity_len, 0);
Jerry Yu568ec252022-07-22 21:27:34 +0800548 identity = p_identity_len + 2;
Gilles Peskine449bd832023-01-11 14:50:10 +0100549 MBEDTLS_SSL_CHK_BUF_READ_PTR(identity, identities_end, identity_len + 4);
550 obfuscated_ticket_age = MBEDTLS_GET_UINT32_BE(identity, identity_len);
Jerry Yu568ec252022-07-22 21:27:34 +0800551 p_identity_len += identity_len + 6;
Jerry Yu1c105562022-07-10 06:32:38 +0000552
Gilles Peskine449bd832023-01-11 14:50:10 +0100553 MBEDTLS_SSL_CHK_BUF_READ_PTR(p_binder_len, binders_end, 1 + 32);
Jerry Yu568ec252022-07-22 21:27:34 +0800554 binder_len = *p_binder_len;
555 binder = p_binder_len + 1;
Gilles Peskine449bd832023-01-11 14:50:10 +0100556 MBEDTLS_SSL_CHK_BUF_READ_PTR(binder, binders_end, binder_len);
Jerry Yu568ec252022-07-22 21:27:34 +0800557 p_binder_len += binder_len + 1;
Jerry Yu96a2e362022-07-21 15:11:34 +0800558
Jerry Yu96a2e362022-07-21 15:11:34 +0800559 identity_id++;
Gilles Peskine449bd832023-01-11 14:50:10 +0100560 if (matched_identity != -1) {
Jerry Yu1c105562022-07-10 06:32:38 +0000561 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +0100562 }
Jerry Yu1c105562022-07-10 06:32:38 +0000563
Jerry Yu96a2e362022-07-21 15:11:34 +0800564 ret = ssl_tls13_offered_psks_check_identity_match(
Gilles Peskine449bd832023-01-11 14:50:10 +0100565 ssl, identity, identity_len, obfuscated_ticket_age,
566 &psk_type, &session);
567 if (ret != SSL_TLS1_3_OFFERED_PSK_MATCH) {
Jerry Yu96a2e362022-07-21 15:11:34 +0800568 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +0100569 }
Jerry Yu96a2e362022-07-21 15:11:34 +0800570
Gilles Peskine449bd832023-01-11 14:50:10 +0100571 MBEDTLS_SSL_DEBUG_MSG(4, ("found matched identity"));
572 switch (psk_type) {
Jerry Yu0baf9072022-08-25 11:21:04 +0800573 case MBEDTLS_SSL_TLS1_3_PSK_EXTERNAL:
574 ret = ssl_tls13_select_ciphersuite_for_psk(
Gilles Peskine449bd832023-01-11 14:50:10 +0100575 ssl, ciphersuites, ciphersuites_end,
576 &cipher_suite, &ciphersuite_info);
Jerry Yu0baf9072022-08-25 11:21:04 +0800577 break;
578 case MBEDTLS_SSL_TLS1_3_PSK_RESUMPTION:
Jerry Yu82534862022-08-30 10:42:33 +0800579#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Jerry Yu0baf9072022-08-25 11:21:04 +0800580 ret = ssl_tls13_select_ciphersuite_for_resumption(
Gilles Peskine449bd832023-01-11 14:50:10 +0100581 ssl, ciphersuites, ciphersuites_end, &session,
582 &cipher_suite, &ciphersuite_info);
583 if (ret != 0) {
584 mbedtls_ssl_session_free(&session);
585 }
Jerry Yu82534862022-08-30 10:42:33 +0800586#else
587 ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
588#endif
Jerry Yu0baf9072022-08-25 11:21:04 +0800589 break;
590 default:
Gilles Peskine449bd832023-01-11 14:50:10 +0100591 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Jerry Yu0baf9072022-08-25 11:21:04 +0800592 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100593 if (ret != 0) {
Jerry Yuf35ba382022-08-23 17:58:26 +0800594 /* See below, no cipher_suite available, abort handshake */
595 MBEDTLS_SSL_PEND_FATAL_ALERT(
596 MBEDTLS_SSL_ALERT_MSG_DECRYPT_ERROR,
Gilles Peskine449bd832023-01-11 14:50:10 +0100597 MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE);
Jerry Yuf35ba382022-08-23 17:58:26 +0800598 MBEDTLS_SSL_DEBUG_RET(
Gilles Peskine449bd832023-01-11 14:50:10 +0100599 2, "ssl_tls13_select_ciphersuite", ret);
600 return ret;
Jerry Yu5725f1c2022-08-21 17:27:16 +0800601 }
602
Jerry Yu96a2e362022-07-21 15:11:34 +0800603 ret = ssl_tls13_offered_psks_check_binder_match(
Gilles Peskine449bd832023-01-11 14:50:10 +0100604 ssl, binder, binder_len, psk_type,
605 mbedtls_psa_translate_md(ciphersuite_info->mac));
606 if (ret != SSL_TLS1_3_OFFERED_PSK_MATCH) {
Jerry Yuc5a23a02022-08-25 10:51:44 +0800607 /* For security reasons, the handshake should be aborted when we
608 * fail to validate a binder value. See RFC 8446 section 4.2.11.2
609 * and appendix E.6. */
Jerry Yu82534862022-08-30 10:42:33 +0800610#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Gilles Peskine449bd832023-01-11 14:50:10 +0100611 mbedtls_ssl_session_free(&session);
Jerry Yu82534862022-08-30 10:42:33 +0800612#endif
Gilles Peskine449bd832023-01-11 14:50:10 +0100613 MBEDTLS_SSL_DEBUG_MSG(3, ("Invalid binder."));
614 MBEDTLS_SSL_DEBUG_RET(1,
615 "ssl_tls13_offered_psks_check_binder_match", ret);
Jerry Yu96a2e362022-07-21 15:11:34 +0800616 MBEDTLS_SSL_PEND_FATAL_ALERT(
617 MBEDTLS_SSL_ALERT_MSG_DECRYPT_ERROR,
Gilles Peskine449bd832023-01-11 14:50:10 +0100618 MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE);
619 return ret;
Jerry Yu96a2e362022-07-21 15:11:34 +0800620 }
Jerry Yu96a2e362022-07-21 15:11:34 +0800621
622 matched_identity = identity_id;
Jerry Yu5725f1c2022-08-21 17:27:16 +0800623
624 /* Update handshake parameters */
Jerry Yue5834fd2022-08-29 20:16:09 +0800625 ssl->handshake->ciphersuite_info = ciphersuite_info;
Jerry Yu4746b102022-09-13 11:11:48 +0800626 ssl->session_negotiate->ciphersuite = cipher_suite;
Gilles Peskine449bd832023-01-11 14:50:10 +0100627 MBEDTLS_SSL_DEBUG_MSG(2, ("overwrite ciphersuite: %04x - %s",
628 cipher_suite, ciphersuite_info->name));
Jerry Yu82534862022-08-30 10:42:33 +0800629#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Gilles Peskine449bd832023-01-11 14:50:10 +0100630 if (psk_type == MBEDTLS_SSL_TLS1_3_PSK_RESUMPTION) {
631 ret = ssl_tls13_session_copy_ticket(ssl->session_negotiate,
632 &session);
633 mbedtls_ssl_session_free(&session);
634 if (ret != 0) {
635 return ret;
636 }
Jerry Yu82534862022-08-30 10:42:33 +0800637 }
638#endif /* MBEDTLS_SSL_SESSION_TICKETS */
Jerry Yu1c105562022-07-10 06:32:38 +0000639 }
640
Gilles Peskine449bd832023-01-11 14:50:10 +0100641 if (p_identity_len != identities_end || p_binder_len != binders_end) {
642 MBEDTLS_SSL_DEBUG_MSG(3, ("pre_shared_key extension decode error"));
643 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR,
644 MBEDTLS_ERR_SSL_DECODE_ERROR);
645 return MBEDTLS_ERR_SSL_DECODE_ERROR;
Jerry Yu1c105562022-07-10 06:32:38 +0000646 }
647
Jerry Yu6f1db3f2022-07-22 23:05:59 +0800648 /* Update the handshake transcript with the binder list. */
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +0100649 ret = ssl->handshake->update_checksum(ssl,
650 identities_end,
651 (size_t) (binders_end - identities_end));
652 if (0 != ret) {
653 MBEDTLS_SSL_DEBUG_RET(1, ("update_checksum"), ret);
654 return ret;
655 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100656 if (matched_identity == -1) {
657 MBEDTLS_SSL_DEBUG_MSG(3, ("No matched PSK or ticket."));
658 return MBEDTLS_ERR_SSL_UNKNOWN_IDENTITY;
Jerry Yu1c105562022-07-10 06:32:38 +0000659 }
660
Gilles Peskine449bd832023-01-11 14:50:10 +0100661 ssl->handshake->selected_identity = (uint16_t) matched_identity;
662 MBEDTLS_SSL_DEBUG_MSG(3, ("Pre shared key found"));
Jerry Yu1c105562022-07-10 06:32:38 +0000663
Gilles Peskine449bd832023-01-11 14:50:10 +0100664 return 0;
Jerry Yu1c105562022-07-10 06:32:38 +0000665}
Jerry Yu032b15ce2022-07-11 06:10:03 +0000666
667/*
668 * struct {
669 * select ( Handshake.msg_type ) {
670 * ....
671 * case server_hello:
672 * uint16 selected_identity;
673 * }
674 * } PreSharedKeyExtension;
675 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100676static int ssl_tls13_write_server_pre_shared_key_ext(mbedtls_ssl_context *ssl,
677 unsigned char *buf,
678 unsigned char *end,
679 size_t *olen)
Jerry Yu032b15ce2022-07-11 06:10:03 +0000680{
Gilles Peskine449bd832023-01-11 14:50:10 +0100681 unsigned char *p = (unsigned char *) buf;
Jerry Yu032b15ce2022-07-11 06:10:03 +0000682
683 *olen = 0;
684
David Horstmann21b89762022-10-06 18:34:28 +0100685 int not_using_psk = 0;
Jerry Yu032b15ce2022-07-11 06:10:03 +0000686#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +0100687 not_using_psk = (mbedtls_svc_key_id_is_null(ssl->handshake->psk_opaque));
Jerry Yu032b15ce2022-07-11 06:10:03 +0000688#else
Gilles Peskine449bd832023-01-11 14:50:10 +0100689 not_using_psk = (ssl->handshake->psk == NULL);
Jerry Yu032b15ce2022-07-11 06:10:03 +0000690#endif
Gilles Peskine449bd832023-01-11 14:50:10 +0100691 if (not_using_psk) {
Jerry Yu032b15ce2022-07-11 06:10:03 +0000692 /* We shouldn't have called this extension writer unless we've
693 * chosen to use a PSK. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100694 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Jerry Yu032b15ce2022-07-11 06:10:03 +0000695 }
696
Gilles Peskine449bd832023-01-11 14:50:10 +0100697 MBEDTLS_SSL_DEBUG_MSG(3, ("server hello, adding pre_shared_key extension"));
698 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 6);
Jerry Yu032b15ce2022-07-11 06:10:03 +0000699
Gilles Peskine449bd832023-01-11 14:50:10 +0100700 MBEDTLS_PUT_UINT16_BE(MBEDTLS_TLS_EXT_PRE_SHARED_KEY, p, 0);
701 MBEDTLS_PUT_UINT16_BE(2, p, 2);
Jerry Yu032b15ce2022-07-11 06:10:03 +0000702
Gilles Peskine449bd832023-01-11 14:50:10 +0100703 MBEDTLS_PUT_UINT16_BE(ssl->handshake->selected_identity, p, 4);
Jerry Yu032b15ce2022-07-11 06:10:03 +0000704
705 *olen = 6;
706
Gilles Peskine449bd832023-01-11 14:50:10 +0100707 MBEDTLS_SSL_DEBUG_MSG(4, ("sent selected_identity: %u",
708 ssl->handshake->selected_identity));
Jerry Yu032b15ce2022-07-11 06:10:03 +0000709
Gilles Peskine449bd832023-01-11 14:50:10 +0100710 mbedtls_ssl_tls13_set_hs_sent_ext_mask(ssl, MBEDTLS_TLS_EXT_PRE_SHARED_KEY);
Jerry Yub95dd362022-11-08 21:19:34 +0800711
Gilles Peskine449bd832023-01-11 14:50:10 +0100712 return 0;
Jerry Yu032b15ce2022-07-11 06:10:03 +0000713}
714
Ronald Cron41a443a2022-10-04 16:38:25 +0200715#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED */
Jerry Yue19e3b92022-07-08 12:04:51 +0000716
XiaokangQian7807f9f2022-02-15 10:04:37 +0000717/* From RFC 8446:
718 * struct {
XiaokangQiancfd925f2022-04-14 07:10:37 +0000719 * ProtocolVersion versions<2..254>;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000720 * } SupportedVersions;
721 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200722MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100723static int ssl_tls13_parse_supported_versions_ext(mbedtls_ssl_context *ssl,
724 const unsigned char *buf,
725 const unsigned char *end)
XiaokangQian7807f9f2022-02-15 10:04:37 +0000726{
XiaokangQian7807f9f2022-02-15 10:04:37 +0000727 const unsigned char *p = buf;
XiaokangQian8f9dfe42022-04-15 02:52:39 +0000728 size_t versions_len;
XiaokangQian4080a7f2022-04-11 09:55:18 +0000729 const unsigned char *versions_end;
XiaokangQian0a1b54e2022-04-21 03:01:38 +0000730 uint16_t tls_version;
XiaokangQian8f9dfe42022-04-15 02:52:39 +0000731 int tls13_supported = 0;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000732
Gilles Peskine449bd832023-01-11 14:50:10 +0100733 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, 1);
XiaokangQian4080a7f2022-04-11 09:55:18 +0000734 versions_len = p[0];
XiaokangQian7807f9f2022-02-15 10:04:37 +0000735 p += 1;
736
Gilles Peskine449bd832023-01-11 14:50:10 +0100737 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, versions_len);
XiaokangQian4080a7f2022-04-11 09:55:18 +0000738 versions_end = p + versions_len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100739 while (p < versions_end) {
740 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, versions_end, 2);
741 tls_version = mbedtls_ssl_read_version(p, ssl->conf->transport);
XiaokangQianb67384d2022-04-19 00:02:38 +0000742 p += 2;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000743
744 /* In this implementation we only support TLS 1.3 and DTLS 1.3. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100745 if (tls_version == MBEDTLS_SSL_VERSION_TLS1_3) {
XiaokangQian7807f9f2022-02-15 10:04:37 +0000746 tls13_supported = 1;
747 break;
748 }
XiaokangQian7807f9f2022-02-15 10:04:37 +0000749 }
750
Gilles Peskine449bd832023-01-11 14:50:10 +0100751 if (!tls13_supported) {
752 MBEDTLS_SSL_DEBUG_MSG(1, ("TLS 1.3 is not supported by the client"));
XiaokangQian7807f9f2022-02-15 10:04:37 +0000753
Gilles Peskine449bd832023-01-11 14:50:10 +0100754 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_PROTOCOL_VERSION,
755 MBEDTLS_ERR_SSL_BAD_PROTOCOL_VERSION);
756 return MBEDTLS_ERR_SSL_BAD_PROTOCOL_VERSION;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000757 }
758
Gilles Peskine449bd832023-01-11 14:50:10 +0100759 MBEDTLS_SSL_DEBUG_MSG(1, ("Negotiated version. Supported is [%04x]",
760 (unsigned int) tls_version));
XiaokangQian7807f9f2022-02-15 10:04:37 +0000761
Gilles Peskine449bd832023-01-11 14:50:10 +0100762 return 0;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000763}
764
XiaokangQian8f9dfe42022-04-15 02:52:39 +0000765#if defined(MBEDTLS_ECDH_C)
XiaokangQiane8ff3502022-04-22 02:34:40 +0000766/*
XiaokangQian7807f9f2022-02-15 10:04:37 +0000767 *
768 * From RFC 8446:
769 * enum {
770 * ... (0xFFFF)
771 * } NamedGroup;
772 * struct {
773 * NamedGroup named_group_list<2..2^16-1>;
774 * } NamedGroupList;
775 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200776MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100777static int ssl_tls13_parse_supported_groups_ext(mbedtls_ssl_context *ssl,
778 const unsigned char *buf,
779 const unsigned char *end)
XiaokangQian7807f9f2022-02-15 10:04:37 +0000780{
XiaokangQian7807f9f2022-02-15 10:04:37 +0000781 const unsigned char *p = buf;
XiaokangQian84823772022-04-19 07:57:30 +0000782 size_t named_group_list_len;
XiaokangQian8f9dfe42022-04-15 02:52:39 +0000783 const unsigned char *named_group_list_end;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000784
Gilles Peskine449bd832023-01-11 14:50:10 +0100785 MBEDTLS_SSL_DEBUG_BUF(3, "supported_groups extension", p, end - buf);
786 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, 2);
787 named_group_list_len = MBEDTLS_GET_UINT16_BE(p, 0);
XiaokangQian7807f9f2022-02-15 10:04:37 +0000788 p += 2;
Gilles Peskine449bd832023-01-11 14:50:10 +0100789 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, named_group_list_len);
XiaokangQian8f9dfe42022-04-15 02:52:39 +0000790 named_group_list_end = p + named_group_list_len;
XiaokangQian4e8cd7b2022-04-21 09:48:09 +0000791 ssl->handshake->hrr_selected_group = 0;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000792
Gilles Peskine449bd832023-01-11 14:50:10 +0100793 while (p < named_group_list_end) {
XiaokangQian08037552022-04-20 07:16:41 +0000794 uint16_t named_group;
Gilles Peskine449bd832023-01-11 14:50:10 +0100795 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, named_group_list_end, 2);
796 named_group = MBEDTLS_GET_UINT16_BE(p, 0);
XiaokangQian08037552022-04-20 07:16:41 +0000797 p += 2;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000798
Gilles Peskine449bd832023-01-11 14:50:10 +0100799 MBEDTLS_SSL_DEBUG_MSG(2,
800 ("got named group: %s(%04x)",
801 mbedtls_ssl_named_group_to_str(named_group),
802 named_group));
XiaokangQian08037552022-04-20 07:16:41 +0000803
Gilles Peskine449bd832023-01-11 14:50:10 +0100804 if (!mbedtls_ssl_named_group_is_offered(ssl, named_group) ||
805 !mbedtls_ssl_named_group_is_supported(named_group) ||
806 ssl->handshake->hrr_selected_group != 0) {
XiaokangQian08037552022-04-20 07:16:41 +0000807 continue;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000808 }
809
Gilles Peskine449bd832023-01-11 14:50:10 +0100810 MBEDTLS_SSL_DEBUG_MSG(2,
811 ("add named group %s(%04x) into received list.",
812 mbedtls_ssl_named_group_to_str(named_group),
813 named_group));
Jerry Yuc1be19f2022-04-23 16:11:39 +0800814
XiaokangQian4e8cd7b2022-04-21 09:48:09 +0000815 ssl->handshake->hrr_selected_group = named_group;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000816 }
817
Gilles Peskine449bd832023-01-11 14:50:10 +0100818 return 0;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000819
820}
XiaokangQian8f9dfe42022-04-15 02:52:39 +0000821#endif /* MBEDTLS_ECDH_C */
XiaokangQian7807f9f2022-02-15 10:04:37 +0000822
XiaokangQian08037552022-04-20 07:16:41 +0000823#define SSL_TLS1_3_PARSE_KEY_SHARES_EXT_NO_MATCH 1
824
XiaokangQian88408882022-04-02 10:15:03 +0000825#if defined(MBEDTLS_ECDH_C)
XiaokangQian7807f9f2022-02-15 10:04:37 +0000826/*
827 * ssl_tls13_parse_key_shares_ext() verifies whether the information in the
XiaokangQiane8ff3502022-04-22 02:34:40 +0000828 * extension is correct and stores the first acceptable key share and its associated group.
XiaokangQian7807f9f2022-02-15 10:04:37 +0000829 *
830 * Possible return values are:
831 * - 0: Successful processing of the client provided key share extension.
XiaokangQian08037552022-04-20 07:16:41 +0000832 * - SSL_TLS1_3_PARSE_KEY_SHARES_EXT_NO_MATCH: The key shares provided by the client
XiaokangQian7807f9f2022-02-15 10:04:37 +0000833 * does not match a group supported by the server. A HelloRetryRequest will
834 * be needed.
XiaokangQiane8ff3502022-04-22 02:34:40 +0000835 * - A negative value for fatal errors.
Jerry Yuc1be19f2022-04-23 16:11:39 +0800836 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200837MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100838static int ssl_tls13_parse_key_shares_ext(mbedtls_ssl_context *ssl,
839 const unsigned char *buf,
840 const unsigned char *end)
XiaokangQian7807f9f2022-02-15 10:04:37 +0000841{
XiaokangQianb67384d2022-04-19 00:02:38 +0000842 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000843 unsigned char const *p = buf;
XiaokangQian8f9dfe42022-04-15 02:52:39 +0000844 unsigned char const *client_shares_end;
Jerry Yuc1be19f2022-04-23 16:11:39 +0800845 size_t client_shares_len;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000846
847 /* From RFC 8446:
848 *
849 * struct {
850 * KeyShareEntry client_shares<0..2^16-1>;
851 * } KeyShareClientHello;
852 *
853 */
854
Gilles Peskine449bd832023-01-11 14:50:10 +0100855 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, 2);
856 client_shares_len = MBEDTLS_GET_UINT16_BE(p, 0);
XiaokangQian7807f9f2022-02-15 10:04:37 +0000857 p += 2;
Gilles Peskine449bd832023-01-11 14:50:10 +0100858 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, client_shares_len);
XiaokangQian7807f9f2022-02-15 10:04:37 +0000859
860 ssl->handshake->offered_group_id = 0;
XiaokangQian8f9dfe42022-04-15 02:52:39 +0000861 client_shares_end = p + client_shares_len;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000862
863 /* We try to find a suitable key share entry and copy it to the
864 * handshake context. Later, we have to find out whether we can do
865 * something with the provided key share or whether we have to
XiaokangQianc5763b52022-04-02 03:34:37 +0000866 * dismiss it and send a HelloRetryRequest message.
867 */
XiaokangQian7807f9f2022-02-15 10:04:37 +0000868
Gilles Peskine449bd832023-01-11 14:50:10 +0100869 while (p < client_shares_end) {
XiaokangQian9b5d04b2022-04-10 10:20:43 +0000870 uint16_t group;
Jerry Yuc1be19f2022-04-23 16:11:39 +0800871 size_t key_exchange_len;
Jerry Yu086edc22022-05-05 10:50:38 +0800872 const unsigned char *key_exchange;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000873
874 /*
875 * struct {
876 * NamedGroup group;
877 * opaque key_exchange<1..2^16-1>;
878 * } KeyShareEntry;
879 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100880 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, client_shares_end, 4);
881 group = MBEDTLS_GET_UINT16_BE(p, 0);
882 key_exchange_len = MBEDTLS_GET_UINT16_BE(p, 2);
Jerry Yuc1be19f2022-04-23 16:11:39 +0800883 p += 4;
Jerry Yu086edc22022-05-05 10:50:38 +0800884 key_exchange = p;
Gilles Peskine449bd832023-01-11 14:50:10 +0100885 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, client_shares_end, key_exchange_len);
Jerry Yu086edc22022-05-05 10:50:38 +0800886 p += key_exchange_len;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000887
888 /* Continue parsing even if we have already found a match,
XiaokangQianc5763b52022-04-02 03:34:37 +0000889 * for input validation purposes.
890 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100891 if (!mbedtls_ssl_named_group_is_offered(ssl, group) ||
892 !mbedtls_ssl_named_group_is_supported(group) ||
893 ssl->handshake->offered_group_id != 0) {
XiaokangQian060d8672022-04-21 09:24:56 +0000894 continue;
895 }
XiaokangQian060d8672022-04-21 09:24:56 +0000896
XiaokangQian7807f9f2022-02-15 10:04:37 +0000897 /*
XiaokangQian8f9dfe42022-04-15 02:52:39 +0000898 * For now, we only support ECDHE groups.
XiaokangQian7807f9f2022-02-15 10:04:37 +0000899 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100900 if (mbedtls_ssl_tls13_named_group_is_ecdhe(group)) {
901 MBEDTLS_SSL_DEBUG_MSG(2, ("ECDH group: %s (%04x)",
902 mbedtls_ssl_named_group_to_str(group),
903 group));
XiaokangQian318dc762022-04-20 09:43:51 +0000904 ret = mbedtls_ssl_tls13_read_public_ecdhe_share(
Gilles Peskine449bd832023-01-11 14:50:10 +0100905 ssl, key_exchange - 2, key_exchange_len + 2);
906 if (ret != 0) {
907 return ret;
908 }
XiaokangQian4e8cd7b2022-04-21 09:48:09 +0000909
Gilles Peskine449bd832023-01-11 14:50:10 +0100910 } else {
911 MBEDTLS_SSL_DEBUG_MSG(4, ("Unrecognized NamedGroup %u",
912 (unsigned) group));
XiaokangQian7807f9f2022-02-15 10:04:37 +0000913 continue;
914 }
915
XiaokangQian9b5d04b2022-04-10 10:20:43 +0000916 ssl->handshake->offered_group_id = group;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000917 }
918
Jerry Yuc1be19f2022-04-23 16:11:39 +0800919
Gilles Peskine449bd832023-01-11 14:50:10 +0100920 if (ssl->handshake->offered_group_id == 0) {
921 MBEDTLS_SSL_DEBUG_MSG(1, ("no matching key share"));
922 return SSL_TLS1_3_PARSE_KEY_SHARES_EXT_NO_MATCH;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000923 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100924 return 0;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000925}
XiaokangQian88408882022-04-02 10:15:03 +0000926#endif /* MBEDTLS_ECDH_C */
XiaokangQian7807f9f2022-02-15 10:04:37 +0000927
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200928MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100929static int ssl_tls13_client_hello_has_exts(mbedtls_ssl_context *ssl,
930 int exts_mask)
XiaokangQian7807f9f2022-02-15 10:04:37 +0000931{
Jerry Yu0c354a22022-08-29 15:25:36 +0800932 int masked = ssl->handshake->received_extensions & exts_mask;
Gilles Peskine449bd832023-01-11 14:50:10 +0100933 return masked == exts_mask;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000934}
935
Jerry Yue5991322022-11-07 14:03:44 +0800936#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200937MBEDTLS_CHECK_RETURN_CRITICAL
XiaokangQianb67384d2022-04-19 00:02:38 +0000938static int ssl_tls13_client_hello_has_exts_for_ephemeral_key_exchange(
Gilles Peskine449bd832023-01-11 14:50:10 +0100939 mbedtls_ssl_context *ssl)
XiaokangQian7807f9f2022-02-15 10:04:37 +0000940{
Gilles Peskine449bd832023-01-11 14:50:10 +0100941 return ssl_tls13_client_hello_has_exts(
942 ssl,
943 MBEDTLS_SSL_EXT_MASK(SUPPORTED_GROUPS) |
944 MBEDTLS_SSL_EXT_MASK(KEY_SHARE) |
945 MBEDTLS_SSL_EXT_MASK(SIG_ALG));
XiaokangQian7807f9f2022-02-15 10:04:37 +0000946}
Jerry Yue5991322022-11-07 14:03:44 +0800947#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED */
XiaokangQian7807f9f2022-02-15 10:04:37 +0000948
Jerry Yue5991322022-11-07 14:03:44 +0800949#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK_ENABLED)
Jerry Yu77f01482022-07-11 07:03:24 +0000950MBEDTLS_CHECK_RETURN_CRITICAL
951static int ssl_tls13_client_hello_has_exts_for_psk_key_exchange(
Gilles Peskine449bd832023-01-11 14:50:10 +0100952 mbedtls_ssl_context *ssl)
Jerry Yu77f01482022-07-11 07:03:24 +0000953{
Gilles Peskine449bd832023-01-11 14:50:10 +0100954 return ssl_tls13_client_hello_has_exts(
955 ssl,
956 MBEDTLS_SSL_EXT_MASK(PRE_SHARED_KEY) |
957 MBEDTLS_SSL_EXT_MASK(PSK_KEY_EXCHANGE_MODES));
Jerry Yu77f01482022-07-11 07:03:24 +0000958}
Jerry Yue5991322022-11-07 14:03:44 +0800959#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK_ENABLED */
Jerry Yu77f01482022-07-11 07:03:24 +0000960
Jerry Yue5991322022-11-07 14:03:44 +0800961#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK_EPHEMERAL_ENABLED)
Jerry Yu77f01482022-07-11 07:03:24 +0000962MBEDTLS_CHECK_RETURN_CRITICAL
963static int ssl_tls13_client_hello_has_exts_for_psk_ephemeral_key_exchange(
Gilles Peskine449bd832023-01-11 14:50:10 +0100964 mbedtls_ssl_context *ssl)
Jerry Yu77f01482022-07-11 07:03:24 +0000965{
Gilles Peskine449bd832023-01-11 14:50:10 +0100966 return ssl_tls13_client_hello_has_exts(
967 ssl,
968 MBEDTLS_SSL_EXT_MASK(SUPPORTED_GROUPS) |
969 MBEDTLS_SSL_EXT_MASK(KEY_SHARE) |
970 MBEDTLS_SSL_EXT_MASK(PRE_SHARED_KEY) |
971 MBEDTLS_SSL_EXT_MASK(PSK_KEY_EXCHANGE_MODES));
Jerry Yu77f01482022-07-11 07:03:24 +0000972}
Jerry Yue5991322022-11-07 14:03:44 +0800973#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK_EPHEMERAL_ENABLED */
Jerry Yu77f01482022-07-11 07:03:24 +0000974
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200975MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100976static int ssl_tls13_check_ephemeral_key_exchange(mbedtls_ssl_context *ssl)
XiaokangQian7807f9f2022-02-15 10:04:37 +0000977{
Jerry Yue5991322022-11-07 14:03:44 +0800978#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +0100979 return mbedtls_ssl_conf_tls13_ephemeral_enabled(ssl) &&
980 ssl_tls13_client_hello_has_exts_for_ephemeral_key_exchange(ssl);
Jerry Yue5991322022-11-07 14:03:44 +0800981#else
982 ((void) ssl);
Gilles Peskine449bd832023-01-11 14:50:10 +0100983 return 0;
Jerry Yue5991322022-11-07 14:03:44 +0800984#endif
Jerry Yu77f01482022-07-11 07:03:24 +0000985}
XiaokangQian7807f9f2022-02-15 10:04:37 +0000986
Jerry Yu77f01482022-07-11 07:03:24 +0000987MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100988static int ssl_tls13_check_psk_key_exchange(mbedtls_ssl_context *ssl)
Jerry Yu77f01482022-07-11 07:03:24 +0000989{
Jerry Yue5991322022-11-07 14:03:44 +0800990#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +0100991 return mbedtls_ssl_conf_tls13_psk_enabled(ssl) &&
992 mbedtls_ssl_tls13_psk_enabled(ssl) &&
993 ssl_tls13_client_hello_has_exts_for_psk_key_exchange(ssl);
Jerry Yu77f01482022-07-11 07:03:24 +0000994#else
995 ((void) ssl);
Gilles Peskine449bd832023-01-11 14:50:10 +0100996 return 0;
Jerry Yu77f01482022-07-11 07:03:24 +0000997#endif
998}
XiaokangQian7807f9f2022-02-15 10:04:37 +0000999
Jerry Yu77f01482022-07-11 07:03:24 +00001000MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01001001static int ssl_tls13_check_psk_ephemeral_key_exchange(mbedtls_ssl_context *ssl)
Jerry Yu77f01482022-07-11 07:03:24 +00001002{
Jerry Yue5991322022-11-07 14:03:44 +08001003#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK_EPHEMERAL_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01001004 return mbedtls_ssl_conf_tls13_psk_ephemeral_enabled(ssl) &&
1005 mbedtls_ssl_tls13_psk_ephemeral_enabled(ssl) &&
1006 ssl_tls13_client_hello_has_exts_for_psk_ephemeral_key_exchange(ssl);
Jerry Yu77f01482022-07-11 07:03:24 +00001007#else
1008 ((void) ssl);
Gilles Peskine449bd832023-01-11 14:50:10 +01001009 return 0;
Jerry Yu77f01482022-07-11 07:03:24 +00001010#endif
1011}
1012
Gilles Peskine449bd832023-01-11 14:50:10 +01001013static int ssl_tls13_determine_key_exchange_mode(mbedtls_ssl_context *ssl)
Jerry Yu77f01482022-07-11 07:03:24 +00001014{
1015 /*
1016 * Determine the key exchange algorithm to use.
1017 * There are three types of key exchanges supported in TLS 1.3:
1018 * - (EC)DH with ECDSA,
1019 * - (EC)DH with PSK,
1020 * - plain PSK.
1021 *
1022 * The PSK-based key exchanges may additionally be used with 0-RTT.
1023 *
1024 * Our built-in order of preference is
Jerry Yuce6ed702022-07-22 21:49:53 +08001025 * 1 ) (EC)DHE-PSK Mode ( psk_ephemeral )
1026 * 2 ) Certificate Mode ( ephemeral )
1027 * 3 ) Plain PSK Mode ( psk )
Jerry Yu77f01482022-07-11 07:03:24 +00001028 */
1029
1030 ssl->handshake->key_exchange_mode = MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_NONE;
1031
Gilles Peskine449bd832023-01-11 14:50:10 +01001032 if (ssl_tls13_check_psk_ephemeral_key_exchange(ssl)) {
Jerry Yu77f01482022-07-11 07:03:24 +00001033 ssl->handshake->key_exchange_mode =
1034 MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK_EPHEMERAL;
Gilles Peskine449bd832023-01-11 14:50:10 +01001035 MBEDTLS_SSL_DEBUG_MSG(2, ("key exchange mode: psk_ephemeral"));
1036 } else
1037 if (ssl_tls13_check_ephemeral_key_exchange(ssl)) {
Jerry Yu77f01482022-07-11 07:03:24 +00001038 ssl->handshake->key_exchange_mode =
1039 MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL;
Gilles Peskine449bd832023-01-11 14:50:10 +01001040 MBEDTLS_SSL_DEBUG_MSG(2, ("key exchange mode: ephemeral"));
1041 } else
1042 if (ssl_tls13_check_psk_key_exchange(ssl)) {
Jerry Yuce6ed702022-07-22 21:49:53 +08001043 ssl->handshake->key_exchange_mode =
1044 MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK;
Gilles Peskine449bd832023-01-11 14:50:10 +01001045 MBEDTLS_SSL_DEBUG_MSG(2, ("key exchange mode: psk"));
1046 } else {
Jerry Yu77f01482022-07-11 07:03:24 +00001047 MBEDTLS_SSL_DEBUG_MSG(
Gilles Peskine449bd832023-01-11 14:50:10 +01001048 1,
1049 ("ClientHello message misses mandatory extensions."));
1050 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_MISSING_EXTENSION,
1051 MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER);
1052 return MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER;
Jerry Yu77f01482022-07-11 07:03:24 +00001053 }
1054
Gilles Peskine449bd832023-01-11 14:50:10 +01001055 return 0;
Jerry Yu77f01482022-07-11 07:03:24 +00001056
XiaokangQian7807f9f2022-02-15 10:04:37 +00001057}
1058
XiaokangQian81802f42022-06-10 13:25:22 +00001059#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
Ronald Cron928cbd32022-10-04 16:14:26 +02001060 defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED)
Ronald Cron67ea2542022-09-15 17:34:42 +02001061
1062#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01001063static psa_algorithm_t ssl_tls13_iana_sig_alg_to_psa_alg(uint16_t sig_alg)
Ronald Cron67ea2542022-09-15 17:34:42 +02001064{
Gilles Peskine449bd832023-01-11 14:50:10 +01001065 switch (sig_alg) {
Ronald Cron67ea2542022-09-15 17:34:42 +02001066 case MBEDTLS_TLS1_3_SIG_ECDSA_SECP256R1_SHA256:
Gilles Peskine449bd832023-01-11 14:50:10 +01001067 return PSA_ALG_ECDSA(PSA_ALG_SHA_256);
Ronald Cron67ea2542022-09-15 17:34:42 +02001068 case MBEDTLS_TLS1_3_SIG_ECDSA_SECP384R1_SHA384:
Gilles Peskine449bd832023-01-11 14:50:10 +01001069 return PSA_ALG_ECDSA(PSA_ALG_SHA_384);
Ronald Cron67ea2542022-09-15 17:34:42 +02001070 case MBEDTLS_TLS1_3_SIG_ECDSA_SECP521R1_SHA512:
Gilles Peskine449bd832023-01-11 14:50:10 +01001071 return PSA_ALG_ECDSA(PSA_ALG_SHA_512);
Ronald Cron67ea2542022-09-15 17:34:42 +02001072 case MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA256:
Gilles Peskine449bd832023-01-11 14:50:10 +01001073 return PSA_ALG_RSA_PSS(PSA_ALG_SHA_256);
Ronald Cron67ea2542022-09-15 17:34:42 +02001074 case MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA384:
Gilles Peskine449bd832023-01-11 14:50:10 +01001075 return PSA_ALG_RSA_PSS(PSA_ALG_SHA_384);
Ronald Cron67ea2542022-09-15 17:34:42 +02001076 case MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA512:
Gilles Peskine449bd832023-01-11 14:50:10 +01001077 return PSA_ALG_RSA_PSS(PSA_ALG_SHA_512);
Ronald Cron67ea2542022-09-15 17:34:42 +02001078 case MBEDTLS_TLS1_3_SIG_RSA_PKCS1_SHA256:
Gilles Peskine449bd832023-01-11 14:50:10 +01001079 return PSA_ALG_RSA_PKCS1V15_SIGN(PSA_ALG_SHA_256);
Ronald Cron67ea2542022-09-15 17:34:42 +02001080 case MBEDTLS_TLS1_3_SIG_RSA_PKCS1_SHA384:
Gilles Peskine449bd832023-01-11 14:50:10 +01001081 return PSA_ALG_RSA_PKCS1V15_SIGN(PSA_ALG_SHA_384);
Ronald Cron67ea2542022-09-15 17:34:42 +02001082 case MBEDTLS_TLS1_3_SIG_RSA_PKCS1_SHA512:
Gilles Peskine449bd832023-01-11 14:50:10 +01001083 return PSA_ALG_RSA_PKCS1V15_SIGN(PSA_ALG_SHA_512);
Ronald Cron67ea2542022-09-15 17:34:42 +02001084 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01001085 return PSA_ALG_NONE;
Ronald Cron67ea2542022-09-15 17:34:42 +02001086 }
1087}
1088#endif /* MBEDTLS_USE_PSA_CRYPTO */
1089
XiaokangQian23c5be62022-06-07 02:04:34 +00001090/*
XiaokangQianfb665a82022-06-15 03:57:21 +00001091 * Pick best ( private key, certificate chain ) pair based on the signature
1092 * algorithms supported by the client.
XiaokangQian23c5be62022-06-07 02:04:34 +00001093 */
Ronald Cronce7d76e2022-07-08 18:56:49 +02001094MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01001095static int ssl_tls13_pick_key_cert(mbedtls_ssl_context *ssl)
XiaokangQian23c5be62022-06-07 02:04:34 +00001096{
XiaokangQianfb665a82022-06-15 03:57:21 +00001097 mbedtls_ssl_key_cert *key_cert, *key_cert_list;
XiaokangQian81802f42022-06-10 13:25:22 +00001098 const uint16_t *sig_alg = ssl->handshake->received_sig_algs;
XiaokangQian23c5be62022-06-07 02:04:34 +00001099
1100#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Gilles Peskine449bd832023-01-11 14:50:10 +01001101 if (ssl->handshake->sni_key_cert != NULL) {
XiaokangQianfb665a82022-06-15 03:57:21 +00001102 key_cert_list = ssl->handshake->sni_key_cert;
Gilles Peskine449bd832023-01-11 14:50:10 +01001103 } else
XiaokangQian23c5be62022-06-07 02:04:34 +00001104#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
Gilles Peskine449bd832023-01-11 14:50:10 +01001105 key_cert_list = ssl->conf->key_cert;
XiaokangQian23c5be62022-06-07 02:04:34 +00001106
Gilles Peskine449bd832023-01-11 14:50:10 +01001107 if (key_cert_list == NULL) {
1108 MBEDTLS_SSL_DEBUG_MSG(3, ("server has no certificate"));
1109 return -1;
XiaokangQian23c5be62022-06-07 02:04:34 +00001110 }
1111
Gilles Peskine449bd832023-01-11 14:50:10 +01001112 for (; *sig_alg != MBEDTLS_TLS1_3_SIG_NONE; sig_alg++) {
1113 if (!mbedtls_ssl_sig_alg_is_offered(ssl, *sig_alg)) {
Ronald Cron67ea2542022-09-15 17:34:42 +02001114 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +01001115 }
Ronald Cron67ea2542022-09-15 17:34:42 +02001116
Gilles Peskine449bd832023-01-11 14:50:10 +01001117 if (!mbedtls_ssl_tls13_sig_alg_for_cert_verify_is_supported(*sig_alg)) {
Ronald Cron67ea2542022-09-15 17:34:42 +02001118 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +01001119 }
Ronald Cron67ea2542022-09-15 17:34:42 +02001120
Gilles Peskine449bd832023-01-11 14:50:10 +01001121 for (key_cert = key_cert_list; key_cert != NULL;
1122 key_cert = key_cert->next) {
Ronald Cron67ea2542022-09-15 17:34:42 +02001123#if defined(MBEDTLS_USE_PSA_CRYPTO)
1124 psa_algorithm_t psa_alg = PSA_ALG_NONE;
1125#endif /* MBEDTLS_USE_PSA_CRYPTO */
1126
Gilles Peskine449bd832023-01-11 14:50:10 +01001127 MBEDTLS_SSL_DEBUG_CRT(3, "certificate (chain) candidate",
1128 key_cert->cert);
XiaokangQian81802f42022-06-10 13:25:22 +00001129
1130 /*
Gilles Peskine449bd832023-01-11 14:50:10 +01001131 * This avoids sending the client a cert it'll reject based on
1132 * keyUsage or other extensions.
1133 */
1134 if (mbedtls_x509_crt_check_key_usage(
1135 key_cert->cert, MBEDTLS_X509_KU_DIGITAL_SIGNATURE) != 0 ||
XiaokangQian81802f42022-06-10 13:25:22 +00001136 mbedtls_x509_crt_check_extended_key_usage(
XiaokangQianfb665a82022-06-15 03:57:21 +00001137 key_cert->cert, MBEDTLS_OID_SERVER_AUTH,
Gilles Peskine449bd832023-01-11 14:50:10 +01001138 MBEDTLS_OID_SIZE(MBEDTLS_OID_SERVER_AUTH)) != 0) {
1139 MBEDTLS_SSL_DEBUG_MSG(3, ("certificate mismatch: "
1140 "(extended) key usage extension"));
XiaokangQian81802f42022-06-10 13:25:22 +00001141 continue;
1142 }
XiaokangQian23c5be62022-06-07 02:04:34 +00001143
Gilles Peskine449bd832023-01-11 14:50:10 +01001144 MBEDTLS_SSL_DEBUG_MSG(3,
1145 ("ssl_tls13_pick_key_cert:"
1146 "check signature algorithm %s [%04x]",
1147 mbedtls_ssl_sig_alg_to_str(*sig_alg),
1148 *sig_alg));
Ronald Cron67ea2542022-09-15 17:34:42 +02001149#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01001150 psa_alg = ssl_tls13_iana_sig_alg_to_psa_alg(*sig_alg);
Ronald Cron67ea2542022-09-15 17:34:42 +02001151#endif /* MBEDTLS_USE_PSA_CRYPTO */
1152
Gilles Peskine449bd832023-01-11 14:50:10 +01001153 if (mbedtls_ssl_tls13_check_sig_alg_cert_key_match(
1154 *sig_alg, &key_cert->cert->pk)
Ronald Cron67ea2542022-09-15 17:34:42 +02001155#if defined(MBEDTLS_USE_PSA_CRYPTO)
1156 && psa_alg != PSA_ALG_NONE &&
Gilles Peskine449bd832023-01-11 14:50:10 +01001157 mbedtls_pk_can_do_ext(&key_cert->cert->pk, psa_alg,
1158 PSA_KEY_USAGE_SIGN_HASH) == 1
Ronald Cron67ea2542022-09-15 17:34:42 +02001159#endif /* MBEDTLS_USE_PSA_CRYPTO */
Gilles Peskine449bd832023-01-11 14:50:10 +01001160 ) {
XiaokangQianfb665a82022-06-15 03:57:21 +00001161 ssl->handshake->key_cert = key_cert;
Gilles Peskine449bd832023-01-11 14:50:10 +01001162 MBEDTLS_SSL_DEBUG_MSG(3,
1163 ("ssl_tls13_pick_key_cert:"
1164 "selected signature algorithm"
1165 " %s [%04x]",
1166 mbedtls_ssl_sig_alg_to_str(*sig_alg),
1167 *sig_alg));
XiaokangQianfb665a82022-06-15 03:57:21 +00001168 MBEDTLS_SSL_DEBUG_CRT(
Gilles Peskine449bd832023-01-11 14:50:10 +01001169 3, "selected certificate (chain)",
1170 ssl->handshake->key_cert->cert);
1171 return 0;
XiaokangQian81802f42022-06-10 13:25:22 +00001172 }
XiaokangQian81802f42022-06-10 13:25:22 +00001173 }
XiaokangQian23c5be62022-06-07 02:04:34 +00001174 }
1175
Gilles Peskine449bd832023-01-11 14:50:10 +01001176 MBEDTLS_SSL_DEBUG_MSG(2, ("ssl_tls13_pick_key_cert:"
1177 "no suitable certificate found"));
1178 return -1;
XiaokangQian23c5be62022-06-07 02:04:34 +00001179}
XiaokangQian81802f42022-06-10 13:25:22 +00001180#endif /* MBEDTLS_X509_CRT_PARSE_C &&
Ronald Cron928cbd32022-10-04 16:14:26 +02001181 MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED */
XiaokangQian23c5be62022-06-07 02:04:34 +00001182
XiaokangQian4080a7f2022-04-11 09:55:18 +00001183/*
XiaokangQianed582dd2022-04-13 08:21:05 +00001184 *
1185 * STATE HANDLING: ClientHello
1186 *
XiaokangQianb67384d2022-04-19 00:02:38 +00001187 * There are three possible classes of outcomes when parsing the ClientHello:
XiaokangQianed582dd2022-04-13 08:21:05 +00001188 *
XiaokangQianb67384d2022-04-19 00:02:38 +00001189 * 1) The ClientHello was well-formed and matched the server's configuration.
XiaokangQianed582dd2022-04-13 08:21:05 +00001190 *
1191 * In this case, the server progresses to sending its ServerHello.
1192 *
XiaokangQianb67384d2022-04-19 00:02:38 +00001193 * 2) The ClientHello was well-formed but didn't match the server's
1194 * configuration.
XiaokangQianed582dd2022-04-13 08:21:05 +00001195 *
1196 * For example, the client might not have offered a key share which
1197 * the server supports, or the server might require a cookie.
1198 *
1199 * In this case, the server sends a HelloRetryRequest.
1200 *
XiaokangQianb67384d2022-04-19 00:02:38 +00001201 * 3) The ClientHello was ill-formed
XiaokangQianed582dd2022-04-13 08:21:05 +00001202 *
1203 * In this case, we abort the handshake.
1204 *
1205 */
1206
1207/*
XiaokangQian4080a7f2022-04-11 09:55:18 +00001208 * Structure of this message:
1209 *
XiaokangQiane8ff3502022-04-22 02:34:40 +00001210 * uint16 ProtocolVersion;
1211 * opaque Random[32];
1212 * uint8 CipherSuite[2]; // Cryptographic suite selector
XiaokangQian4080a7f2022-04-11 09:55:18 +00001213 *
XiaokangQiane8ff3502022-04-22 02:34:40 +00001214 * struct {
1215 * ProtocolVersion legacy_version = 0x0303; // TLS v1.2
1216 * Random random;
1217 * opaque legacy_session_id<0..32>;
1218 * CipherSuite cipher_suites<2..2^16-2>;
1219 * opaque legacy_compression_methods<1..2^8-1>;
1220 * Extension extensions<8..2^16-1>;
1221 * } ClientHello;
XiaokangQian4080a7f2022-04-11 09:55:18 +00001222 */
XiaokangQianed582dd2022-04-13 08:21:05 +00001223
1224#define SSL_CLIENT_HELLO_OK 0
1225#define SSL_CLIENT_HELLO_HRR_REQUIRED 1
1226
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001227MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01001228static int ssl_tls13_parse_client_hello(mbedtls_ssl_context *ssl,
1229 const unsigned char *buf,
1230 const unsigned char *end)
XiaokangQian7807f9f2022-02-15 10:04:37 +00001231{
XiaokangQianb67384d2022-04-19 00:02:38 +00001232 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1233 const unsigned char *p = buf;
XiaokangQian4080a7f2022-04-11 09:55:18 +00001234 size_t legacy_session_id_len;
XiaokangQian318dc762022-04-20 09:43:51 +00001235 size_t cipher_suites_len;
XiaokangQian060d8672022-04-21 09:24:56 +00001236 const unsigned char *cipher_suites_end;
XiaokangQianb67384d2022-04-19 00:02:38 +00001237 size_t extensions_len;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001238 const unsigned char *extensions_end;
Jerry Yuc4bf5d62022-10-29 09:08:47 +08001239 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
Jerry Yu49ca9282022-05-05 11:05:22 +08001240 int hrr_required = 0;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001241
Ronald Cron41a443a2022-10-04 16:38:25 +02001242#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED)
Jerry Yu5725f1c2022-08-21 17:27:16 +08001243 const unsigned char *cipher_suites;
Jerry Yu29d9faa2022-08-23 17:52:45 +08001244 const unsigned char *pre_shared_key_ext = NULL;
Jerry Yu1c105562022-07-10 06:32:38 +00001245 const unsigned char *pre_shared_key_ext_end = NULL;
Ronald Cron41a443a2022-10-04 16:38:25 +02001246#endif
XiaokangQian7807f9f2022-02-15 10:04:37 +00001247
XiaokangQian7807f9f2022-02-15 10:04:37 +00001248 /*
XiaokangQianb67384d2022-04-19 00:02:38 +00001249 * ClientHello layout:
XiaokangQian7807f9f2022-02-15 10:04:37 +00001250 * 0 . 1 protocol version
XiaokangQiane8ff3502022-04-22 02:34:40 +00001251 * 2 . 33 random bytes
XiaokangQianc5763b52022-04-02 03:34:37 +00001252 * 34 . 34 session id length ( 1 byte )
XiaokangQian7807f9f2022-02-15 10:04:37 +00001253 * 35 . 34+x session id
XiaokangQian7807f9f2022-02-15 10:04:37 +00001254 * .. . .. ciphersuite list length ( 2 bytes )
1255 * .. . .. ciphersuite list
1256 * .. . .. compression alg. list length ( 1 byte )
1257 * .. . .. compression alg. list
1258 * .. . .. extensions length ( 2 bytes, optional )
1259 * .. . .. extensions ( optional )
1260 */
1261
XiaokangQianb67384d2022-04-19 00:02:38 +00001262 /*
bootstrap-prime6dbbf442022-05-17 19:30:44 -04001263 * Minimal length ( with everything empty and extensions omitted ) is
XiaokangQian7807f9f2022-02-15 10:04:37 +00001264 * 2 + 32 + 1 + 2 + 1 = 38 bytes. Check that first, so that we can
1265 * read at least up to session id length without worrying.
1266 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001267 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, 38);
XiaokangQian7807f9f2022-02-15 10:04:37 +00001268
1269 /* ...
1270 * ProtocolVersion legacy_version = 0x0303; // TLS 1.2
1271 * ...
1272 * with ProtocolVersion defined as:
1273 * uint16 ProtocolVersion;
1274 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001275 if (mbedtls_ssl_read_version(p, ssl->conf->transport) !=
1276 MBEDTLS_SSL_VERSION_TLS1_2) {
1277 MBEDTLS_SSL_DEBUG_MSG(1, ("Unsupported version of TLS."));
1278 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_PROTOCOL_VERSION,
1279 MBEDTLS_ERR_SSL_BAD_PROTOCOL_VERSION);
1280 return MBEDTLS_ERR_SSL_BAD_PROTOCOL_VERSION;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001281 }
1282 p += 2;
1283
1284 /*
XiaokangQianf8ceb942022-04-15 11:43:27 +00001285 * Only support TLS 1.3 currently, temporarily set the version.
1286 */
XiaokangQiande333912022-04-20 08:49:42 +00001287 ssl->tls_version = MBEDTLS_SSL_VERSION_TLS1_3;
XiaokangQianf8ceb942022-04-15 11:43:27 +00001288
Jerry Yue67bef42022-07-07 07:29:42 +00001289#if defined(MBEDTLS_SSL_SESSION_TICKETS)
1290 /* Store minor version for later use with ticket serialization. */
1291 ssl->session_negotiate->tls_version = MBEDTLS_SSL_VERSION_TLS1_3;
Jerry Yua66fece2022-07-13 14:30:29 +08001292 ssl->session_negotiate->endpoint = ssl->conf->endpoint;
Jerry Yufca4d572022-07-21 10:37:48 +08001293#endif
Jerry Yue67bef42022-07-07 07:29:42 +00001294
Jerry Yuc1be19f2022-04-23 16:11:39 +08001295 /* ...
1296 * Random random;
1297 * ...
XiaokangQianb67384d2022-04-19 00:02:38 +00001298 * with Random defined as:
1299 * opaque Random[32];
XiaokangQian7807f9f2022-02-15 10:04:37 +00001300 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001301 MBEDTLS_SSL_DEBUG_BUF(3, "client hello, random bytes",
1302 p, MBEDTLS_CLIENT_HELLO_RANDOM_LEN);
XiaokangQian7807f9f2022-02-15 10:04:37 +00001303
Gilles Peskine449bd832023-01-11 14:50:10 +01001304 memcpy(&handshake->randbytes[0], p, MBEDTLS_CLIENT_HELLO_RANDOM_LEN);
XiaokangQian08037552022-04-20 07:16:41 +00001305 p += MBEDTLS_CLIENT_HELLO_RANDOM_LEN;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001306
Jerry Yuc1be19f2022-04-23 16:11:39 +08001307 /* ...
XiaokangQianb67384d2022-04-19 00:02:38 +00001308 * opaque legacy_session_id<0..32>;
Jerry Yuc1be19f2022-04-23 16:11:39 +08001309 * ...
XiaokangQian7807f9f2022-02-15 10:04:37 +00001310 */
XiaokangQian4080a7f2022-04-11 09:55:18 +00001311 legacy_session_id_len = p[0];
XiaokangQianc5763b52022-04-02 03:34:37 +00001312 p++;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001313
Gilles Peskine449bd832023-01-11 14:50:10 +01001314 if (legacy_session_id_len > sizeof(ssl->session_negotiate->id)) {
1315 MBEDTLS_SSL_DEBUG_MSG(1, ("bad client hello message"));
1316 return MBEDTLS_ERR_SSL_DECODE_ERROR;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001317 }
1318
XiaokangQian4080a7f2022-04-11 09:55:18 +00001319 ssl->session_negotiate->id_len = legacy_session_id_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01001320 MBEDTLS_SSL_DEBUG_BUF(3, "client hello, session id",
1321 p, legacy_session_id_len);
XiaokangQianb67384d2022-04-19 00:02:38 +00001322 /*
1323 * Check we have enough data for the legacy session identifier
Jerry Yue95c8af2022-07-26 15:48:20 +08001324 * and the ciphersuite list length.
XiaokangQianb67384d2022-04-19 00:02:38 +00001325 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001326 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, legacy_session_id_len + 2);
XiaokangQian7807f9f2022-02-15 10:04:37 +00001327
Gilles Peskine449bd832023-01-11 14:50:10 +01001328 memcpy(&ssl->session_negotiate->id[0], p, legacy_session_id_len);
XiaokangQian4080a7f2022-04-11 09:55:18 +00001329 p += legacy_session_id_len;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001330
Gilles Peskine449bd832023-01-11 14:50:10 +01001331 cipher_suites_len = MBEDTLS_GET_UINT16_BE(p, 0);
XiaokangQian7807f9f2022-02-15 10:04:37 +00001332 p += 2;
1333
XiaokangQianb67384d2022-04-19 00:02:38 +00001334 /* Check we have enough data for the ciphersuite list, the legacy
1335 * compression methods and the length of the extensions.
Jerry Yue95c8af2022-07-26 15:48:20 +08001336 *
1337 * cipher_suites cipher_suites_len bytes
1338 * legacy_compression_methods 2 bytes
1339 * extensions_len 2 bytes
XiaokangQianb67384d2022-04-19 00:02:38 +00001340 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001341 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, cipher_suites_len + 2 + 2);
XiaokangQian7807f9f2022-02-15 10:04:37 +00001342
Gilles Peskine449bd832023-01-11 14:50:10 +01001343 /* ...
1344 * CipherSuite cipher_suites<2..2^16-2>;
1345 * ...
1346 * with CipherSuite defined as:
1347 * uint8 CipherSuite[2];
1348 */
Ronald Cron41a443a2022-10-04 16:38:25 +02001349#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED)
XiaokangQian060d8672022-04-21 09:24:56 +00001350 cipher_suites = p;
Jerry Yu5725f1c2022-08-21 17:27:16 +08001351#endif
XiaokangQian060d8672022-04-21 09:24:56 +00001352 cipher_suites_end = p + cipher_suites_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01001353 MBEDTLS_SSL_DEBUG_BUF(3, "client hello, ciphersuitelist",
1354 p, cipher_suites_len);
Jerry Yu5725f1c2022-08-21 17:27:16 +08001355
1356 /*
1357 * Search for a matching ciphersuite
1358 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001359 for (; p < cipher_suites_end; p += 2) {
XiaokangQiane8ff3502022-04-22 02:34:40 +00001360 uint16_t cipher_suite;
Gilles Peskine449bd832023-01-11 14:50:10 +01001361 const mbedtls_ssl_ciphersuite_t *ciphersuite_info;
Jerry Yu5725f1c2022-08-21 17:27:16 +08001362
Gilles Peskine449bd832023-01-11 14:50:10 +01001363 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, cipher_suites_end, 2);
Jerry Yu5725f1c2022-08-21 17:27:16 +08001364
Gilles Peskine449bd832023-01-11 14:50:10 +01001365 cipher_suite = MBEDTLS_GET_UINT16_BE(p, 0);
Jerry Yuc5a23a02022-08-25 10:51:44 +08001366 ciphersuite_info = ssl_tls13_validate_peer_ciphersuite(
Gilles Peskine449bd832023-01-11 14:50:10 +01001367 ssl, cipher_suite);
1368 if (ciphersuite_info == NULL) {
Jerry Yu5725f1c2022-08-21 17:27:16 +08001369 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +01001370 }
Jerry Yu5725f1c2022-08-21 17:27:16 +08001371
Jerry Yu5725f1c2022-08-21 17:27:16 +08001372 ssl->session_negotiate->ciphersuite = cipher_suite;
Jerry Yuc4bf5d62022-10-29 09:08:47 +08001373 handshake->ciphersuite_info = ciphersuite_info;
Gilles Peskine449bd832023-01-11 14:50:10 +01001374 MBEDTLS_SSL_DEBUG_MSG(2, ("selected ciphersuite: %04x - %s",
1375 cipher_suite,
1376 ciphersuite_info->name));
XiaokangQian17f974c2022-04-19 09:57:41 +00001377 }
Jerry Yu5725f1c2022-08-21 17:27:16 +08001378
Gilles Peskine449bd832023-01-11 14:50:10 +01001379 if (handshake->ciphersuite_info == NULL) {
1380 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE,
1381 MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE);
1382 return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
Jerry Yu5725f1c2022-08-21 17:27:16 +08001383 }
Jerry Yuc1be19f2022-04-23 16:11:39 +08001384
XiaokangQian4080a7f2022-04-11 09:55:18 +00001385 /* ...
XiaokangQianb67384d2022-04-19 00:02:38 +00001386 * opaque legacy_compression_methods<1..2^8-1>;
XiaokangQian4080a7f2022-04-11 09:55:18 +00001387 * ...
XiaokangQian7807f9f2022-02-15 10:04:37 +00001388 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001389 if (p[0] != 1 || p[1] != MBEDTLS_SSL_COMPRESS_NULL) {
1390 MBEDTLS_SSL_DEBUG_MSG(1, ("bad legacy compression method"));
1391 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER,
1392 MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER);
1393 return MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001394 }
XiaokangQianb67384d2022-04-19 00:02:38 +00001395 p += 2;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001396
Jerry Yuc1be19f2022-04-23 16:11:39 +08001397 /* ...
XiaokangQianb67384d2022-04-19 00:02:38 +00001398 * Extension extensions<8..2^16-1>;
Jerry Yuc1be19f2022-04-23 16:11:39 +08001399 * ...
XiaokangQianb67384d2022-04-19 00:02:38 +00001400 * with Extension defined as:
1401 * struct {
1402 * ExtensionType extension_type;
1403 * opaque extension_data<0..2^16-1>;
1404 * } Extension;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001405 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001406 extensions_len = MBEDTLS_GET_UINT16_BE(p, 0);
XiaokangQian7807f9f2022-02-15 10:04:37 +00001407 p += 2;
Gilles Peskine449bd832023-01-11 14:50:10 +01001408 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, extensions_len);
XiaokangQiane8ff3502022-04-22 02:34:40 +00001409 extensions_end = p + extensions_len;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001410
Gilles Peskine449bd832023-01-11 14:50:10 +01001411 MBEDTLS_SSL_DEBUG_BUF(3, "client hello extensions", p, extensions_len);
XiaokangQian7807f9f2022-02-15 10:04:37 +00001412
Jerry Yu63a459c2022-10-31 13:38:40 +08001413 handshake->received_extensions = MBEDTLS_SSL_EXT_MASK_NONE;
Jerry Yu0c354a22022-08-29 15:25:36 +08001414
Gilles Peskine449bd832023-01-11 14:50:10 +01001415 while (p < extensions_end) {
XiaokangQian7807f9f2022-02-15 10:04:37 +00001416 unsigned int extension_type;
1417 size_t extension_data_len;
1418 const unsigned char *extension_data_end;
1419
Jerry Yu0c354a22022-08-29 15:25:36 +08001420 /* RFC 8446, section 4.2.11
Jerry Yu1c9247c2022-07-21 12:37:39 +08001421 *
1422 * The "pre_shared_key" extension MUST be the last extension in the
1423 * ClientHello (this facilitates implementation as described below).
1424 * Servers MUST check that it is the last extension and otherwise fail
1425 * the handshake with an "illegal_parameter" alert.
1426 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001427 if (handshake->received_extensions & MBEDTLS_SSL_EXT_MASK(PRE_SHARED_KEY)) {
Jerry Yu1c9247c2022-07-21 12:37:39 +08001428 MBEDTLS_SSL_DEBUG_MSG(
Gilles Peskine449bd832023-01-11 14:50:10 +01001429 3, ("pre_shared_key is not last extension."));
Jerry Yu1c9247c2022-07-21 12:37:39 +08001430 MBEDTLS_SSL_PEND_FATAL_ALERT(
1431 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER,
Gilles Peskine449bd832023-01-11 14:50:10 +01001432 MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER);
1433 return MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER;
Jerry Yu1c9247c2022-07-21 12:37:39 +08001434 }
1435
Gilles Peskine449bd832023-01-11 14:50:10 +01001436 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, extensions_end, 4);
1437 extension_type = MBEDTLS_GET_UINT16_BE(p, 0);
1438 extension_data_len = MBEDTLS_GET_UINT16_BE(p, 2);
XiaokangQian7807f9f2022-02-15 10:04:37 +00001439 p += 4;
1440
Gilles Peskine449bd832023-01-11 14:50:10 +01001441 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, extensions_end, extension_data_len);
XiaokangQian7807f9f2022-02-15 10:04:37 +00001442 extension_data_end = p + extension_data_len;
1443
Jerry Yuc4bf5d62022-10-29 09:08:47 +08001444 ret = mbedtls_ssl_tls13_check_received_extension(
Gilles Peskine449bd832023-01-11 14:50:10 +01001445 ssl, MBEDTLS_SSL_HS_CLIENT_HELLO, extension_type,
1446 MBEDTLS_SSL_TLS1_3_ALLOWED_EXTS_OF_CH);
1447 if (ret != 0) {
1448 return ret;
1449 }
Jerry Yue18dc7e2022-08-04 16:29:22 +08001450
Gilles Peskine449bd832023-01-11 14:50:10 +01001451 switch (extension_type) {
XiaokangQian40a35232022-05-07 09:02:40 +00001452#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
1453 case MBEDTLS_TLS_EXT_SERVERNAME:
Gilles Peskine449bd832023-01-11 14:50:10 +01001454 MBEDTLS_SSL_DEBUG_MSG(3, ("found ServerName extension"));
1455 ret = mbedtls_ssl_parse_server_name_ext(ssl, p,
1456 extension_data_end);
1457 if (ret != 0) {
XiaokangQian40a35232022-05-07 09:02:40 +00001458 MBEDTLS_SSL_DEBUG_RET(
Gilles Peskine449bd832023-01-11 14:50:10 +01001459 1, "mbedtls_ssl_parse_servername_ext", ret);
1460 return ret;
XiaokangQian40a35232022-05-07 09:02:40 +00001461 }
XiaokangQian40a35232022-05-07 09:02:40 +00001462 break;
1463#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
1464
XiaokangQianb67384d2022-04-19 00:02:38 +00001465#if defined(MBEDTLS_ECDH_C)
XiaokangQian7807f9f2022-02-15 10:04:37 +00001466 case MBEDTLS_TLS_EXT_SUPPORTED_GROUPS:
Gilles Peskine449bd832023-01-11 14:50:10 +01001467 MBEDTLS_SSL_DEBUG_MSG(3, ("found supported group extension"));
XiaokangQian7807f9f2022-02-15 10:04:37 +00001468
1469 /* Supported Groups Extension
1470 *
1471 * When sent by the client, the "supported_groups" extension
1472 * indicates the named groups which the client supports,
1473 * ordered from most preferred to least preferred.
1474 */
Jerry Yuc1be19f2022-04-23 16:11:39 +08001475 ret = ssl_tls13_parse_supported_groups_ext(
Gilles Peskine449bd832023-01-11 14:50:10 +01001476 ssl, p, extension_data_end);
1477 if (ret != 0) {
1478 MBEDTLS_SSL_DEBUG_RET(1,
1479 "mbedtls_ssl_parse_supported_groups_ext", ret);
1480 return ret;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001481 }
1482
XiaokangQian7807f9f2022-02-15 10:04:37 +00001483 break;
XiaokangQianb67384d2022-04-19 00:02:38 +00001484#endif /* MBEDTLS_ECDH_C */
XiaokangQian7807f9f2022-02-15 10:04:37 +00001485
XiaokangQian88408882022-04-02 10:15:03 +00001486#if defined(MBEDTLS_ECDH_C)
XiaokangQian7807f9f2022-02-15 10:04:37 +00001487 case MBEDTLS_TLS_EXT_KEY_SHARE:
Gilles Peskine449bd832023-01-11 14:50:10 +01001488 MBEDTLS_SSL_DEBUG_MSG(3, ("found key share extension"));
XiaokangQian7807f9f2022-02-15 10:04:37 +00001489
1490 /*
1491 * Key Share Extension
1492 *
1493 * When sent by the client, the "key_share" extension
1494 * contains the endpoint's cryptographic parameters for
1495 * ECDHE/DHE key establishment methods.
1496 */
Jerry Yuc1be19f2022-04-23 16:11:39 +08001497 ret = ssl_tls13_parse_key_shares_ext(
Gilles Peskine449bd832023-01-11 14:50:10 +01001498 ssl, p, extension_data_end);
1499 if (ret == SSL_TLS1_3_PARSE_KEY_SHARES_EXT_NO_MATCH) {
1500 MBEDTLS_SSL_DEBUG_MSG(2, ("HRR needed "));
Jerry Yu49ca9282022-05-05 11:05:22 +08001501 hrr_required = 1;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001502 }
1503
Gilles Peskine449bd832023-01-11 14:50:10 +01001504 if (ret < 0) {
Jerry Yu582dd062022-04-22 21:59:01 +08001505 MBEDTLS_SSL_DEBUG_RET(
Gilles Peskine449bd832023-01-11 14:50:10 +01001506 1, "ssl_tls13_parse_key_shares_ext", ret);
1507 return ret;
Jerry Yu582dd062022-04-22 21:59:01 +08001508 }
XiaokangQian7807f9f2022-02-15 10:04:37 +00001509
XiaokangQian7807f9f2022-02-15 10:04:37 +00001510 break;
XiaokangQian88408882022-04-02 10:15:03 +00001511#endif /* MBEDTLS_ECDH_C */
XiaokangQian7807f9f2022-02-15 10:04:37 +00001512
1513 case MBEDTLS_TLS_EXT_SUPPORTED_VERSIONS:
Gilles Peskine449bd832023-01-11 14:50:10 +01001514 MBEDTLS_SSL_DEBUG_MSG(3, ("found supported versions extension"));
XiaokangQian7807f9f2022-02-15 10:04:37 +00001515
1516 ret = ssl_tls13_parse_supported_versions_ext(
Gilles Peskine449bd832023-01-11 14:50:10 +01001517 ssl, p, extension_data_end);
1518 if (ret != 0) {
1519 MBEDTLS_SSL_DEBUG_RET(1,
1520 ("ssl_tls13_parse_supported_versions_ext"), ret);
1521 return ret;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001522 }
XiaokangQian7807f9f2022-02-15 10:04:37 +00001523 break;
1524
Ronald Cron41a443a2022-10-04 16:38:25 +02001525#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED)
Jerry Yue19e3b92022-07-08 12:04:51 +00001526 case MBEDTLS_TLS_EXT_PSK_KEY_EXCHANGE_MODES:
Gilles Peskine449bd832023-01-11 14:50:10 +01001527 MBEDTLS_SSL_DEBUG_MSG(3, ("found psk key exchange modes extension"));
Jerry Yue19e3b92022-07-08 12:04:51 +00001528
1529 ret = ssl_tls13_parse_key_exchange_modes_ext(
Gilles Peskine449bd832023-01-11 14:50:10 +01001530 ssl, p, extension_data_end);
1531 if (ret != 0) {
Jerry Yue19e3b92022-07-08 12:04:51 +00001532 MBEDTLS_SSL_DEBUG_RET(
Gilles Peskine449bd832023-01-11 14:50:10 +01001533 1, "ssl_tls13_parse_key_exchange_modes_ext", ret);
1534 return ret;
Jerry Yue19e3b92022-07-08 12:04:51 +00001535 }
1536
Jerry Yue19e3b92022-07-08 12:04:51 +00001537 break;
Ronald Cron41a443a2022-10-04 16:38:25 +02001538#endif
Jerry Yue19e3b92022-07-08 12:04:51 +00001539
Jerry Yu1c105562022-07-10 06:32:38 +00001540 case MBEDTLS_TLS_EXT_PRE_SHARED_KEY:
Gilles Peskine449bd832023-01-11 14:50:10 +01001541 MBEDTLS_SSL_DEBUG_MSG(3, ("found pre_shared_key extension"));
1542 if ((handshake->received_extensions &
1543 MBEDTLS_SSL_EXT_MASK(PSK_KEY_EXCHANGE_MODES)) == 0) {
Jerry Yu13ab81d2022-07-22 23:17:11 +08001544 MBEDTLS_SSL_PEND_FATAL_ALERT(
1545 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER,
Gilles Peskine449bd832023-01-11 14:50:10 +01001546 MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER);
1547 return MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER;
Jerry Yu13ab81d2022-07-22 23:17:11 +08001548 }
Ronald Cron41a443a2022-10-04 16:38:25 +02001549#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED)
Jerry Yu1c105562022-07-10 06:32:38 +00001550 /* Delay processing of the PSK identity once we have
1551 * found out which algorithms to use. We keep a pointer
1552 * to the buffer and the size for later processing.
1553 */
Jerry Yu29d9faa2022-08-23 17:52:45 +08001554 pre_shared_key_ext = p;
Jerry Yu1c105562022-07-10 06:32:38 +00001555 pre_shared_key_ext_end = extension_data_end;
Jerry Yue18dc7e2022-08-04 16:29:22 +08001556#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED */
Jerry Yu1c105562022-07-10 06:32:38 +00001557 break;
Jerry Yu1c105562022-07-10 06:32:38 +00001558
XiaokangQianacb39922022-06-17 10:18:48 +00001559#if defined(MBEDTLS_SSL_ALPN)
1560 case MBEDTLS_TLS_EXT_ALPN:
Gilles Peskine449bd832023-01-11 14:50:10 +01001561 MBEDTLS_SSL_DEBUG_MSG(3, ("found alpn extension"));
XiaokangQianacb39922022-06-17 10:18:48 +00001562
Gilles Peskine449bd832023-01-11 14:50:10 +01001563 ret = mbedtls_ssl_parse_alpn_ext(ssl, p, extension_data_end);
1564 if (ret != 0) {
XiaokangQianacb39922022-06-17 10:18:48 +00001565 MBEDTLS_SSL_DEBUG_RET(
Gilles Peskine449bd832023-01-11 14:50:10 +01001566 1, ("mbedtls_ssl_parse_alpn_ext"), ret);
1567 return ret;
XiaokangQianacb39922022-06-17 10:18:48 +00001568 }
XiaokangQianacb39922022-06-17 10:18:48 +00001569 break;
1570#endif /* MBEDTLS_SSL_ALPN */
1571
Ronald Cron928cbd32022-10-04 16:14:26 +02001572#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED)
XiaokangQian7807f9f2022-02-15 10:04:37 +00001573 case MBEDTLS_TLS_EXT_SIG_ALG:
Gilles Peskine449bd832023-01-11 14:50:10 +01001574 MBEDTLS_SSL_DEBUG_MSG(3, ("found signature_algorithms extension"));
XiaokangQian7807f9f2022-02-15 10:04:37 +00001575
Gabor Mezei078e8032022-04-27 21:17:56 +02001576 ret = mbedtls_ssl_parse_sig_alg_ext(
Gilles Peskine449bd832023-01-11 14:50:10 +01001577 ssl, p, extension_data_end);
1578 if (ret != 0) {
1579 MBEDTLS_SSL_DEBUG_MSG(1,
1580 (
1581 "ssl_parse_supported_signature_algorithms_server_ext ( %d )",
1582 ret));
1583 return ret;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001584 }
XiaokangQian7807f9f2022-02-15 10:04:37 +00001585 break;
Ronald Cron928cbd32022-10-04 16:14:26 +02001586#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED */
XiaokangQian7807f9f2022-02-15 10:04:37 +00001587
Jan Bruckner151f6422023-02-10 12:45:19 +01001588#if defined(MBEDTLS_SSL_RECORD_SIZE_LIMIT)
1589 case MBEDTLS_TLS_EXT_RECORD_SIZE_LIMIT:
1590 MBEDTLS_SSL_DEBUG_MSG(3, ("found record_size_limit extension"));
1591
1592 ret = mbedtls_ssl_tls13_parse_record_size_limit_ext(ssl, p, extension_data_end);
1593
1594 return ret;
1595
1596 break;
1597#endif /* MBEDTLS_SSL_RECORD_SIZE_LIMIT */
1598
XiaokangQian7807f9f2022-02-15 10:04:37 +00001599 default:
Jerry Yu79aa7212022-11-08 21:30:21 +08001600 MBEDTLS_SSL_PRINT_EXT(
Jerry Yu63a459c2022-10-31 13:38:40 +08001601 3, MBEDTLS_SSL_HS_CLIENT_HELLO,
Gilles Peskine449bd832023-01-11 14:50:10 +01001602 extension_type, "( ignored )");
Jerry Yu0c354a22022-08-29 15:25:36 +08001603 break;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001604 }
1605
1606 p += extension_data_len;
1607 }
1608
Gilles Peskine449bd832023-01-11 14:50:10 +01001609 MBEDTLS_SSL_PRINT_EXTS(3, MBEDTLS_SSL_HS_CLIENT_HELLO,
1610 handshake->received_extensions);
Jerry Yue18dc7e2022-08-04 16:29:22 +08001611
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +01001612 ret = mbedtls_ssl_add_hs_hdr_to_checksum(ssl,
1613 MBEDTLS_SSL_HS_CLIENT_HELLO,
1614 p - buf);
1615 if (0 != ret) {
1616 MBEDTLS_SSL_DEBUG_RET(1, ("mbedtls_ssl_add_hs_hdr_to_checksum"), ret);
1617 return ret;
1618 }
Jerry Yu032b15ce2022-07-11 06:10:03 +00001619
Ronald Cron41a443a2022-10-04 16:38:25 +02001620#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED)
XiaokangQian7807f9f2022-02-15 10:04:37 +00001621 /* Update checksum with either
1622 * - The entire content of the CH message, if no PSK extension is present
1623 * - The content up to but excluding the PSK extension, if present.
1624 */
Jerry Yu1c105562022-07-10 06:32:38 +00001625 /* If we've settled on a PSK-based exchange, parse PSK identity ext */
Gilles Peskine449bd832023-01-11 14:50:10 +01001626 if (mbedtls_ssl_tls13_some_psk_enabled(ssl) &&
1627 mbedtls_ssl_conf_tls13_some_psk_enabled(ssl) &&
1628 (handshake->received_extensions & MBEDTLS_SSL_EXT_MASK(PRE_SHARED_KEY))) {
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +01001629 ret = handshake->update_checksum(ssl, buf,
1630 pre_shared_key_ext - buf);
1631 if (0 != ret) {
1632 MBEDTLS_SSL_DEBUG_RET(1, ("update_checksum"), ret);
1633 return ret;
1634 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001635 ret = ssl_tls13_parse_pre_shared_key_ext(ssl,
1636 pre_shared_key_ext,
1637 pre_shared_key_ext_end,
1638 cipher_suites,
1639 cipher_suites_end);
1640 if (ret == MBEDTLS_ERR_SSL_UNKNOWN_IDENTITY) {
1641 handshake->received_extensions &= ~MBEDTLS_SSL_EXT_MASK(PRE_SHARED_KEY);
1642 } else if (ret != 0) {
Jerry Yu63a459c2022-10-31 13:38:40 +08001643 MBEDTLS_SSL_DEBUG_RET(
Gilles Peskine449bd832023-01-11 14:50:10 +01001644 1, "ssl_tls13_parse_pre_shared_key_ext", ret);
1645 return ret;
Jerry Yu1c105562022-07-10 06:32:38 +00001646 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001647 } else
Ronald Cron41a443a2022-10-04 16:38:25 +02001648#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED */
Jerry Yu1c105562022-07-10 06:32:38 +00001649 {
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +01001650 ret = handshake->update_checksum(ssl, buf, p - buf);
1651 if (0 != ret) {
1652 MBEDTLS_SSL_DEBUG_RET(1, ("update_checksum"), ret);
1653 return ret;
1654 }
Jerry Yu1c105562022-07-10 06:32:38 +00001655 }
XiaokangQian7807f9f2022-02-15 10:04:37 +00001656
Gilles Peskine449bd832023-01-11 14:50:10 +01001657 ret = ssl_tls13_determine_key_exchange_mode(ssl);
1658 if (ret < 0) {
1659 return ret;
1660 }
XiaokangQian7807f9f2022-02-15 10:04:37 +00001661
Gilles Peskine449bd832023-01-11 14:50:10 +01001662 mbedtls_ssl_optimize_checksum(ssl, handshake->ciphersuite_info);
Jerry Yue5834fd2022-08-29 20:16:09 +08001663
Gilles Peskine449bd832023-01-11 14:50:10 +01001664 return hrr_required ? SSL_CLIENT_HELLO_HRR_REQUIRED : SSL_CLIENT_HELLO_OK;
XiaokangQian75fe8c72022-06-15 09:42:45 +00001665}
1666
1667/* Update the handshake state machine */
1668
Ronald Cronce7d76e2022-07-08 18:56:49 +02001669MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01001670static int ssl_tls13_postprocess_client_hello(mbedtls_ssl_context *ssl)
XiaokangQian75fe8c72022-06-15 09:42:45 +00001671{
1672 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1673
XiaokangQian7807f9f2022-02-15 10:04:37 +00001674 /*
XiaokangQianfb665a82022-06-15 03:57:21 +00001675 * Server certificate selection
1676 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001677 if (ssl->conf->f_cert_cb && (ret = ssl->conf->f_cert_cb(ssl)) != 0) {
1678 MBEDTLS_SSL_DEBUG_RET(1, "f_cert_cb", ret);
1679 return ret;
XiaokangQianfb665a82022-06-15 03:57:21 +00001680 }
1681#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
1682 ssl->handshake->sni_name = NULL;
1683 ssl->handshake->sni_name_len = 0;
1684#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
XiaokangQian7807f9f2022-02-15 10:04:37 +00001685
Gilles Peskine449bd832023-01-11 14:50:10 +01001686 ret = mbedtls_ssl_tls13_key_schedule_stage_early(ssl);
1687 if (ret != 0) {
1688 MBEDTLS_SSL_DEBUG_RET(1,
1689 "mbedtls_ssl_tls1_3_key_schedule_stage_early", ret);
1690 return ret;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001691 }
1692
Gilles Peskine449bd832023-01-11 14:50:10 +01001693 return 0;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001694
1695}
1696
1697/*
XiaokangQianed582dd2022-04-13 08:21:05 +00001698 * Main entry point from the state machine; orchestrates the otherfunctions.
1699 */
1700
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001701MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01001702static int ssl_tls13_process_client_hello(mbedtls_ssl_context *ssl)
XiaokangQianed582dd2022-04-13 08:21:05 +00001703{
1704
XiaokangQian08037552022-04-20 07:16:41 +00001705 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine449bd832023-01-11 14:50:10 +01001706 unsigned char *buf = NULL;
XiaokangQianed582dd2022-04-13 08:21:05 +00001707 size_t buflen = 0;
Jerry Yu4ca91402022-05-09 15:50:57 +08001708 int parse_client_hello_ret;
1709
Gilles Peskine449bd832023-01-11 14:50:10 +01001710 MBEDTLS_SSL_DEBUG_MSG(2, ("=> parse client hello"));
XiaokangQianed582dd2022-04-13 08:21:05 +00001711
Gilles Peskine449bd832023-01-11 14:50:10 +01001712 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_tls13_fetch_handshake_msg(
1713 ssl, MBEDTLS_SSL_HS_CLIENT_HELLO,
1714 &buf, &buflen));
XiaokangQianed582dd2022-04-13 08:21:05 +00001715
Gilles Peskine449bd832023-01-11 14:50:10 +01001716 MBEDTLS_SSL_PROC_CHK_NEG(ssl_tls13_parse_client_hello(ssl, buf,
1717 buf + buflen));
Jerry Yuf41553b2022-05-09 22:20:30 +08001718 parse_client_hello_ret = ret; /* Store return value of parse_client_hello,
1719 * only SSL_CLIENT_HELLO_OK or
1720 * SSL_CLIENT_HELLO_HRR_REQUIRED at this
1721 * stage as negative error codes are handled
1722 * by MBEDTLS_SSL_PROC_CHK_NEG. */
Jerry Yu4ca91402022-05-09 15:50:57 +08001723
Gilles Peskine449bd832023-01-11 14:50:10 +01001724 MBEDTLS_SSL_PROC_CHK(ssl_tls13_postprocess_client_hello(ssl));
Jerry Yu582dd062022-04-22 21:59:01 +08001725
Gilles Peskine449bd832023-01-11 14:50:10 +01001726 if (parse_client_hello_ret == SSL_CLIENT_HELLO_OK) {
1727 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_SERVER_HELLO);
1728 } else {
1729 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_HELLO_RETRY_REQUEST);
1730 }
XiaokangQianed582dd2022-04-13 08:21:05 +00001731
1732cleanup:
1733
Gilles Peskine449bd832023-01-11 14:50:10 +01001734 MBEDTLS_SSL_DEBUG_MSG(2, ("<= parse client hello"));
1735 return ret;
XiaokangQianed582dd2022-04-13 08:21:05 +00001736}
1737
1738/*
Jerry Yu1c3e6882022-04-20 21:23:40 +08001739 * Handler for MBEDTLS_SSL_SERVER_HELLO
Jerry Yu5b64ae92022-03-30 17:15:02 +08001740 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001741MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01001742static int ssl_tls13_prepare_server_hello(mbedtls_ssl_context *ssl)
Jerry Yuf4b27e42022-03-30 17:32:21 +08001743{
Jerry Yu637a3f12022-04-20 21:37:58 +08001744 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1745 unsigned char *server_randbytes =
Gilles Peskine449bd832023-01-11 14:50:10 +01001746 ssl->handshake->randbytes + MBEDTLS_CLIENT_HELLO_RANDOM_LEN;
1747 if (ssl->conf->f_rng == NULL) {
1748 MBEDTLS_SSL_DEBUG_MSG(1, ("no RNG provided"));
1749 return MBEDTLS_ERR_SSL_NO_RNG;
Jerry Yuf4b27e42022-03-30 17:32:21 +08001750 }
1751
Gilles Peskine449bd832023-01-11 14:50:10 +01001752 if ((ret = ssl->conf->f_rng(ssl->conf->p_rng, server_randbytes,
1753 MBEDTLS_SERVER_HELLO_RANDOM_LEN)) != 0) {
1754 MBEDTLS_SSL_DEBUG_RET(1, "f_rng", ret);
1755 return ret;
Jerry Yuf4b27e42022-03-30 17:32:21 +08001756 }
1757
Gilles Peskine449bd832023-01-11 14:50:10 +01001758 MBEDTLS_SSL_DEBUG_BUF(3, "server hello, random bytes", server_randbytes,
1759 MBEDTLS_SERVER_HELLO_RANDOM_LEN);
Jerry Yuf4b27e42022-03-30 17:32:21 +08001760
1761#if defined(MBEDTLS_HAVE_TIME)
Gilles Peskine449bd832023-01-11 14:50:10 +01001762 ssl->session_negotiate->start = time(NULL);
Jerry Yuf4b27e42022-03-30 17:32:21 +08001763#endif /* MBEDTLS_HAVE_TIME */
1764
Gilles Peskine449bd832023-01-11 14:50:10 +01001765 return ret;
Jerry Yuf4b27e42022-03-30 17:32:21 +08001766}
1767
Jerry Yu3bf2c642022-03-30 22:02:12 +08001768/*
Jerry Yue74e04a2022-04-21 09:23:16 +08001769 * ssl_tls13_write_server_hello_supported_versions_ext ():
Jerry Yu3bf2c642022-03-30 22:02:12 +08001770 *
1771 * struct {
Jerry Yufb9f54d2022-04-06 10:08:34 +08001772 * ProtocolVersion selected_version;
Jerry Yu3bf2c642022-03-30 22:02:12 +08001773 * } SupportedVersions;
1774 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001775MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yue74e04a2022-04-21 09:23:16 +08001776static int ssl_tls13_write_server_hello_supported_versions_ext(
Gilles Peskine449bd832023-01-11 14:50:10 +01001777 mbedtls_ssl_context *ssl,
1778 unsigned char *buf,
1779 unsigned char *end,
1780 size_t *out_len)
Jerry Yu3bf2c642022-03-30 22:02:12 +08001781{
Jerry Yu3bf2c642022-03-30 22:02:12 +08001782 *out_len = 0;
1783
Gilles Peskine449bd832023-01-11 14:50:10 +01001784 MBEDTLS_SSL_DEBUG_MSG(3, ("server hello, write selected version"));
Jerry Yu3bf2c642022-03-30 22:02:12 +08001785
1786 /* Check if we have space to write the extension:
1787 * - extension_type (2 bytes)
1788 * - extension_data_length (2 bytes)
Jerry Yu349a6132022-04-14 20:52:56 +08001789 * - selected_version (2 bytes)
Jerry Yu3bf2c642022-03-30 22:02:12 +08001790 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001791 MBEDTLS_SSL_CHK_BUF_PTR(buf, end, 6);
Jerry Yu3bf2c642022-03-30 22:02:12 +08001792
Gilles Peskine449bd832023-01-11 14:50:10 +01001793 MBEDTLS_PUT_UINT16_BE(MBEDTLS_TLS_EXT_SUPPORTED_VERSIONS, buf, 0);
Jerry Yu3bf2c642022-03-30 22:02:12 +08001794
Gilles Peskine449bd832023-01-11 14:50:10 +01001795 MBEDTLS_PUT_UINT16_BE(2, buf, 2);
Jerry Yu3bf2c642022-03-30 22:02:12 +08001796
Gilles Peskine449bd832023-01-11 14:50:10 +01001797 mbedtls_ssl_write_version(buf + 4,
1798 ssl->conf->transport,
1799 ssl->tls_version);
Jerry Yu3bf2c642022-03-30 22:02:12 +08001800
Gilles Peskine449bd832023-01-11 14:50:10 +01001801 MBEDTLS_SSL_DEBUG_MSG(3, ("supported version: [%04x]",
1802 ssl->tls_version));
Jerry Yu3bf2c642022-03-30 22:02:12 +08001803
Jerry Yu349a6132022-04-14 20:52:56 +08001804 *out_len = 6;
Jerry Yu3bf2c642022-03-30 22:02:12 +08001805
Jerry Yub95dd362022-11-08 21:19:34 +08001806 mbedtls_ssl_tls13_set_hs_sent_ext_mask(
Gilles Peskine449bd832023-01-11 14:50:10 +01001807 ssl, MBEDTLS_TLS_EXT_SUPPORTED_VERSIONS);
Jerry Yub95dd362022-11-08 21:19:34 +08001808
Gilles Peskine449bd832023-01-11 14:50:10 +01001809 return 0;
Jerry Yu3bf2c642022-03-30 22:02:12 +08001810}
1811
Jerry Yud9436a12022-04-20 22:28:09 +08001812
Jerry Yu3bf2c642022-03-30 22:02:12 +08001813
1814/* Generate and export a single key share. For hybrid KEMs, this can
1815 * be called multiple times with the different components of the hybrid. */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001816MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01001817static int ssl_tls13_generate_and_write_key_share(mbedtls_ssl_context *ssl,
1818 uint16_t named_group,
1819 unsigned char *buf,
1820 unsigned char *end,
1821 size_t *out_len)
Jerry Yu3bf2c642022-03-30 22:02:12 +08001822{
1823 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yu955ddd72022-04-22 22:27:33 +08001824
1825 *out_len = 0;
1826
Jerry Yu89e103c2022-03-30 22:43:29 +08001827#if defined(MBEDTLS_ECDH_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001828 if (mbedtls_ssl_tls13_named_group_is_ecdhe(named_group)) {
Jerry Yu89e103c2022-03-30 22:43:29 +08001829 ret = mbedtls_ssl_tls13_generate_and_write_ecdh_key_exchange(
Gilles Peskine449bd832023-01-11 14:50:10 +01001830 ssl, named_group, buf, end, out_len);
1831 if (ret != 0) {
Jerry Yu89e103c2022-03-30 22:43:29 +08001832 MBEDTLS_SSL_DEBUG_RET(
1833 1, "mbedtls_ssl_tls13_generate_and_write_ecdh_key_exchange",
Gilles Peskine449bd832023-01-11 14:50:10 +01001834 ret);
1835 return ret;
Jerry Yu3bf2c642022-03-30 22:02:12 +08001836 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001837 } else
Jerry Yu89e103c2022-03-30 22:43:29 +08001838#endif /* MBEDTLS_ECDH_C */
Gilles Peskine449bd832023-01-11 14:50:10 +01001839 if (0 /* Other kinds of KEMs */) {
1840 } else {
Jerry Yu955ddd72022-04-22 22:27:33 +08001841 ((void) ssl);
1842 ((void) named_group);
1843 ((void) buf);
1844 ((void) end);
Jerry Yu3bf2c642022-03-30 22:02:12 +08001845 ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
1846 }
1847
Gilles Peskine449bd832023-01-11 14:50:10 +01001848 return ret;
Jerry Yu3bf2c642022-03-30 22:02:12 +08001849}
1850
1851/*
1852 * ssl_tls13_write_key_share_ext
1853 *
1854 * Structure of key_share extension in ServerHello:
1855 *
Jerry Yu1c3e6882022-04-20 21:23:40 +08001856 * struct {
1857 * NamedGroup group;
1858 * opaque key_exchange<1..2^16-1>;
1859 * } KeyShareEntry;
1860 * struct {
1861 * KeyShareEntry server_share;
1862 * } KeyShareServerHello;
Jerry Yu3bf2c642022-03-30 22:02:12 +08001863 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001864MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01001865static int ssl_tls13_write_key_share_ext(mbedtls_ssl_context *ssl,
1866 unsigned char *buf,
1867 unsigned char *end,
1868 size_t *out_len)
Jerry Yu3bf2c642022-03-30 22:02:12 +08001869{
Jerry Yu955ddd72022-04-22 22:27:33 +08001870 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yu3bf2c642022-03-30 22:02:12 +08001871 unsigned char *p = buf;
Jerry Yu57d48412022-04-20 21:50:42 +08001872 uint16_t group = ssl->handshake->offered_group_id;
Jerry Yu3bf2c642022-03-30 22:02:12 +08001873 unsigned char *server_share = buf + 4;
Jerry Yu3bf2c642022-03-30 22:02:12 +08001874 size_t key_exchange_length;
Jerry Yu3bf2c642022-03-30 22:02:12 +08001875
1876 *out_len = 0;
1877
Gilles Peskine449bd832023-01-11 14:50:10 +01001878 MBEDTLS_SSL_DEBUG_MSG(3, ("server hello, adding key share extension"));
Jerry Yu3bf2c642022-03-30 22:02:12 +08001879
Gilles Peskine449bd832023-01-11 14:50:10 +01001880 MBEDTLS_SSL_DEBUG_MSG(2, ("server hello, write selected_group: %s (%04x)",
1881 mbedtls_ssl_named_group_to_str(group),
1882 group));
Jerry Yu58af2332022-09-06 11:19:31 +08001883
Jerry Yu3bf2c642022-03-30 22:02:12 +08001884 /* Check if we have space for header and length fields:
1885 * - extension_type (2 bytes)
1886 * - extension_data_length (2 bytes)
1887 * - group (2 bytes)
1888 * - key_exchange_length (2 bytes)
1889 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001890 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 8);
1891 MBEDTLS_PUT_UINT16_BE(MBEDTLS_TLS_EXT_KEY_SHARE, p, 0);
1892 MBEDTLS_PUT_UINT16_BE(group, server_share, 0);
Jerry Yu3bf2c642022-03-30 22:02:12 +08001893 p += 8;
Jerry Yu57d48412022-04-20 21:50:42 +08001894
Jerry Yu3bf2c642022-03-30 22:02:12 +08001895 /* When we introduce PQC-ECDHE hybrids, we'll want to call this
1896 * function multiple times. */
Jerry Yu955ddd72022-04-22 22:27:33 +08001897 ret = ssl_tls13_generate_and_write_key_share(
Gilles Peskine449bd832023-01-11 14:50:10 +01001898 ssl, group, server_share + 4, end, &key_exchange_length);
1899 if (ret != 0) {
1900 return ret;
1901 }
Jerry Yu3bf2c642022-03-30 22:02:12 +08001902 p += key_exchange_length;
Jerry Yuc1be19f2022-04-23 16:11:39 +08001903
Gilles Peskine449bd832023-01-11 14:50:10 +01001904 MBEDTLS_PUT_UINT16_BE(key_exchange_length, server_share + 2, 0);
Jerry Yu3bf2c642022-03-30 22:02:12 +08001905
Gilles Peskine449bd832023-01-11 14:50:10 +01001906 MBEDTLS_PUT_UINT16_BE(p - server_share, buf, 2);
Jerry Yu3bf2c642022-03-30 22:02:12 +08001907
Jerry Yu57d48412022-04-20 21:50:42 +08001908 *out_len = p - buf;
Jerry Yu955ddd72022-04-22 22:27:33 +08001909
Gilles Peskine449bd832023-01-11 14:50:10 +01001910 mbedtls_ssl_tls13_set_hs_sent_ext_mask(ssl, MBEDTLS_TLS_EXT_KEY_SHARE);
Jerry Yub95dd362022-11-08 21:19:34 +08001911
Gilles Peskine449bd832023-01-11 14:50:10 +01001912 return 0;
Jerry Yu3bf2c642022-03-30 22:02:12 +08001913}
Jerry Yud9436a12022-04-20 22:28:09 +08001914
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001915MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01001916static int ssl_tls13_write_hrr_key_share_ext(mbedtls_ssl_context *ssl,
1917 unsigned char *buf,
1918 unsigned char *end,
1919 size_t *out_len)
Jerry Yu23f7a6f2022-04-23 15:16:45 +08001920{
1921 uint16_t selected_group = ssl->handshake->hrr_selected_group;
1922 /* key_share Extension
1923 *
1924 * struct {
1925 * select (Handshake.msg_type) {
1926 * ...
1927 * case hello_retry_request:
1928 * NamedGroup selected_group;
1929 * ...
1930 * };
1931 * } KeyShare;
1932 */
1933
1934 *out_len = 0;
1935
Jerry Yub0ac10b2022-05-05 11:10:08 +08001936 /*
1937 * For a pure PSK key exchange, there is no group to agree upon. The purpose
1938 * of the HRR is then to transmit a cookie to force the client to demonstrate
1939 * reachability at their apparent network address (primarily useful for DTLS).
1940 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001941 if (!mbedtls_ssl_tls13_key_exchange_mode_with_ephemeral(ssl)) {
1942 return 0;
1943 }
Jerry Yu23f7a6f2022-04-23 15:16:45 +08001944
1945 /* We should only send the key_share extension if the client's initial
1946 * key share was not acceptable. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001947 if (ssl->handshake->offered_group_id != 0) {
1948 MBEDTLS_SSL_DEBUG_MSG(4, ("Skip key_share extension in HRR"));
1949 return 0;
Jerry Yu23f7a6f2022-04-23 15:16:45 +08001950 }
1951
Gilles Peskine449bd832023-01-11 14:50:10 +01001952 if (selected_group == 0) {
1953 MBEDTLS_SSL_DEBUG_MSG(1, ("no matching named group found"));
1954 return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
Jerry Yu23f7a6f2022-04-23 15:16:45 +08001955 }
1956
Jerry Yub0ac10b2022-05-05 11:10:08 +08001957 /* Check if we have enough space:
1958 * - extension_type (2 bytes)
1959 * - extension_data_length (2 bytes)
1960 * - selected_group (2 bytes)
1961 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001962 MBEDTLS_SSL_CHK_BUF_PTR(buf, end, 6);
Jerry Yu23f7a6f2022-04-23 15:16:45 +08001963
Gilles Peskine449bd832023-01-11 14:50:10 +01001964 MBEDTLS_PUT_UINT16_BE(MBEDTLS_TLS_EXT_KEY_SHARE, buf, 0);
1965 MBEDTLS_PUT_UINT16_BE(2, buf, 2);
1966 MBEDTLS_PUT_UINT16_BE(selected_group, buf, 4);
Jerry Yu23f7a6f2022-04-23 15:16:45 +08001967
Gilles Peskine449bd832023-01-11 14:50:10 +01001968 MBEDTLS_SSL_DEBUG_MSG(3,
1969 ("HRR selected_group: %s (%x)",
1970 mbedtls_ssl_named_group_to_str(selected_group),
1971 selected_group));
Jerry Yu23f7a6f2022-04-23 15:16:45 +08001972
1973 *out_len = 6;
Jerry Yu23f7a6f2022-04-23 15:16:45 +08001974
Gilles Peskine449bd832023-01-11 14:50:10 +01001975 mbedtls_ssl_tls13_set_hs_sent_ext_mask(ssl, MBEDTLS_TLS_EXT_KEY_SHARE);
Jerry Yub95dd362022-11-08 21:19:34 +08001976
Gilles Peskine449bd832023-01-11 14:50:10 +01001977 return 0;
Jerry Yu23f7a6f2022-04-23 15:16:45 +08001978}
Jerry Yu3bf2c642022-03-30 22:02:12 +08001979
1980/*
1981 * Structure of ServerHello message:
1982 *
1983 * struct {
1984 * ProtocolVersion legacy_version = 0x0303; // TLS v1.2
1985 * Random random;
1986 * opaque legacy_session_id_echo<0..32>;
1987 * CipherSuite cipher_suite;
1988 * uint8 legacy_compression_method = 0;
1989 * Extension extensions<6..2^16-1>;
1990 * } ServerHello;
1991 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001992MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01001993static int ssl_tls13_write_server_hello_body(mbedtls_ssl_context *ssl,
1994 unsigned char *buf,
1995 unsigned char *end,
1996 size_t *out_len,
1997 int is_hrr)
Jerry Yu56404d72022-03-30 17:36:13 +08001998{
Jerry Yu955ddd72022-04-22 22:27:33 +08001999 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yu3bf2c642022-03-30 22:02:12 +08002000 unsigned char *p = buf;
Jerry Yud9436a12022-04-20 22:28:09 +08002001 unsigned char *p_extensions_len;
Jerry Yufbe3e642022-04-25 19:31:51 +08002002 size_t output_len;
Jerry Yu3bf2c642022-03-30 22:02:12 +08002003
2004 *out_len = 0;
Jerry Yu50e00e32022-10-31 14:45:01 +08002005 ssl->handshake->sent_extensions = MBEDTLS_SSL_EXT_MASK_NONE;
Jerry Yu3bf2c642022-03-30 22:02:12 +08002006
Jerry Yucfc04b32022-04-21 09:31:58 +08002007 /* ...
2008 * ProtocolVersion legacy_version = 0x0303; // TLS 1.2
2009 * ...
2010 * with ProtocolVersion defined as:
2011 * uint16 ProtocolVersion;
Jerry Yu3bf2c642022-03-30 22:02:12 +08002012 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002013 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 2);
2014 MBEDTLS_PUT_UINT16_BE(0x0303, p, 0);
Jerry Yu3bf2c642022-03-30 22:02:12 +08002015 p += 2;
2016
Jerry Yu1c3e6882022-04-20 21:23:40 +08002017 /* ...
2018 * Random random;
2019 * ...
Jerry Yucfc04b32022-04-21 09:31:58 +08002020 * with Random defined as:
2021 * opaque Random[MBEDTLS_SERVER_HELLO_RANDOM_LEN];
Jerry Yu1c3e6882022-04-20 21:23:40 +08002022 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002023 MBEDTLS_SSL_CHK_BUF_PTR(p, end, MBEDTLS_SERVER_HELLO_RANDOM_LEN);
2024 if (is_hrr) {
2025 memcpy(p, mbedtls_ssl_tls13_hello_retry_request_magic,
2026 MBEDTLS_SERVER_HELLO_RANDOM_LEN);
2027 } else {
2028 memcpy(p, &ssl->handshake->randbytes[MBEDTLS_CLIENT_HELLO_RANDOM_LEN],
2029 MBEDTLS_SERVER_HELLO_RANDOM_LEN);
Jerry Yu23f7a6f2022-04-23 15:16:45 +08002030 }
Gilles Peskine449bd832023-01-11 14:50:10 +01002031 MBEDTLS_SSL_DEBUG_BUF(3, "server hello, random bytes",
2032 p, MBEDTLS_SERVER_HELLO_RANDOM_LEN);
Jerry Yu3bf2c642022-03-30 22:02:12 +08002033 p += MBEDTLS_SERVER_HELLO_RANDOM_LEN;
2034
Jerry Yucfc04b32022-04-21 09:31:58 +08002035 /* ...
2036 * opaque legacy_session_id_echo<0..32>;
2037 * ...
Jerry Yu3bf2c642022-03-30 22:02:12 +08002038 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002039 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 1 + ssl->session_negotiate->id_len);
2040 *p++ = (unsigned char) ssl->session_negotiate->id_len;
2041 if (ssl->session_negotiate->id_len > 0) {
2042 memcpy(p, &ssl->session_negotiate->id[0],
2043 ssl->session_negotiate->id_len);
Jerry Yu3bf2c642022-03-30 22:02:12 +08002044 p += ssl->session_negotiate->id_len;
Jerry Yu955ddd72022-04-22 22:27:33 +08002045
Gilles Peskine449bd832023-01-11 14:50:10 +01002046 MBEDTLS_SSL_DEBUG_BUF(3, "session id", ssl->session_negotiate->id,
2047 ssl->session_negotiate->id_len);
Jerry Yu3bf2c642022-03-30 22:02:12 +08002048 }
2049
Jerry Yucfc04b32022-04-21 09:31:58 +08002050 /* ...
2051 * CipherSuite cipher_suite;
2052 * ...
2053 * with CipherSuite defined as:
2054 * uint8 CipherSuite[2];
Jerry Yu3bf2c642022-03-30 22:02:12 +08002055 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002056 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 2);
2057 MBEDTLS_PUT_UINT16_BE(ssl->session_negotiate->ciphersuite, p, 0);
Jerry Yu3bf2c642022-03-30 22:02:12 +08002058 p += 2;
Gilles Peskine449bd832023-01-11 14:50:10 +01002059 MBEDTLS_SSL_DEBUG_MSG(3,
2060 ("server hello, chosen ciphersuite: %s ( id=%d )",
2061 mbedtls_ssl_get_ciphersuite_name(
2062 ssl->session_negotiate->ciphersuite),
2063 ssl->session_negotiate->ciphersuite));
Jerry Yu3bf2c642022-03-30 22:02:12 +08002064
Jerry Yucfc04b32022-04-21 09:31:58 +08002065 /* ...
2066 * uint8 legacy_compression_method = 0;
2067 * ...
2068 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002069 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 1);
Thomas Daubney31e03a82022-07-25 15:59:25 +01002070 *p++ = MBEDTLS_SSL_COMPRESS_NULL;
Jerry Yu3bf2c642022-03-30 22:02:12 +08002071
Jerry Yucfc04b32022-04-21 09:31:58 +08002072 /* ...
2073 * Extension extensions<6..2^16-1>;
2074 * ...
2075 * struct {
2076 * ExtensionType extension_type; (2 bytes)
2077 * opaque extension_data<0..2^16-1>;
2078 * } Extension;
2079 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002080 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 2);
Jerry Yud9436a12022-04-20 22:28:09 +08002081 p_extensions_len = p;
Jerry Yu3bf2c642022-03-30 22:02:12 +08002082 p += 2;
2083
Gilles Peskine449bd832023-01-11 14:50:10 +01002084 if ((ret = ssl_tls13_write_server_hello_supported_versions_ext(
2085 ssl, p, end, &output_len)) != 0) {
Jerry Yu955ddd72022-04-22 22:27:33 +08002086 MBEDTLS_SSL_DEBUG_RET(
Gilles Peskine449bd832023-01-11 14:50:10 +01002087 1, "ssl_tls13_write_server_hello_supported_versions_ext", ret);
2088 return ret;
Jerry Yu3bf2c642022-03-30 22:02:12 +08002089 }
2090 p += output_len;
2091
Gilles Peskine449bd832023-01-11 14:50:10 +01002092 if (mbedtls_ssl_tls13_key_exchange_mode_with_ephemeral(ssl)) {
2093 if (is_hrr) {
2094 ret = ssl_tls13_write_hrr_key_share_ext(ssl, p, end, &output_len);
2095 } else {
2096 ret = ssl_tls13_write_key_share_ext(ssl, p, end, &output_len);
2097 }
2098 if (ret != 0) {
2099 return ret;
2100 }
Jerry Yu3bf2c642022-03-30 22:02:12 +08002101 p += output_len;
2102 }
Jerry Yu3bf2c642022-03-30 22:02:12 +08002103
Ronald Cron41a443a2022-10-04 16:38:25 +02002104#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01002105 if (!is_hrr && mbedtls_ssl_tls13_key_exchange_mode_with_psk(ssl)) {
2106 ret = ssl_tls13_write_server_pre_shared_key_ext(ssl, p, end, &output_len);
2107 if (ret != 0) {
2108 MBEDTLS_SSL_DEBUG_RET(1, "ssl_tls13_write_server_pre_shared_key_ext",
2109 ret);
2110 return ret;
Jerry Yu032b15ce2022-07-11 06:10:03 +00002111 }
2112 p += output_len;
2113 }
Ronald Cron41a443a2022-10-04 16:38:25 +02002114#endif
Jerry Yu032b15ce2022-07-11 06:10:03 +00002115
Gilles Peskine449bd832023-01-11 14:50:10 +01002116 MBEDTLS_PUT_UINT16_BE(p - p_extensions_len - 2, p_extensions_len, 0);
Jerry Yu3bf2c642022-03-30 22:02:12 +08002117
Gilles Peskine449bd832023-01-11 14:50:10 +01002118 MBEDTLS_SSL_DEBUG_BUF(4, "server hello extensions",
2119 p_extensions_len, p - p_extensions_len);
Jerry Yu3bf2c642022-03-30 22:02:12 +08002120
Jerry Yud9436a12022-04-20 22:28:09 +08002121 *out_len = p - buf;
Jerry Yu3bf2c642022-03-30 22:02:12 +08002122
Gilles Peskine449bd832023-01-11 14:50:10 +01002123 MBEDTLS_SSL_DEBUG_BUF(3, "server hello", buf, *out_len);
Jerry Yu3bf2c642022-03-30 22:02:12 +08002124
Jerry Yu7de2ff02022-11-08 21:43:46 +08002125 MBEDTLS_SSL_PRINT_EXTS(
Jerry Yu4b8f2f72022-10-31 13:31:22 +08002126 3, is_hrr ? MBEDTLS_SSL_TLS1_3_HS_HELLO_RETRY_REQUEST :
Gilles Peskine449bd832023-01-11 14:50:10 +01002127 MBEDTLS_SSL_HS_SERVER_HELLO,
2128 ssl->handshake->sent_extensions);
Jerry Yu4b8f2f72022-10-31 13:31:22 +08002129
Gilles Peskine449bd832023-01-11 14:50:10 +01002130 return ret;
Jerry Yu56404d72022-03-30 17:36:13 +08002131}
2132
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002133MBEDTLS_CHECK_RETURN_CRITICAL
Xiaokang Qian934ce6f2023-02-06 10:23:04 +00002134static int ssl_tls13_finalize_server_hello(mbedtls_ssl_context *ssl)
Jerry Yue110d252022-05-05 10:19:22 +08002135{
2136 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine449bd832023-01-11 14:50:10 +01002137 ret = mbedtls_ssl_tls13_compute_handshake_transform(ssl);
2138 if (ret != 0) {
2139 MBEDTLS_SSL_DEBUG_RET(1,
2140 "mbedtls_ssl_tls13_compute_handshake_transform",
2141 ret);
2142 return ret;
Jerry Yue110d252022-05-05 10:19:22 +08002143 }
2144
Gilles Peskine449bd832023-01-11 14:50:10 +01002145 return ret;
Jerry Yue110d252022-05-05 10:19:22 +08002146}
2147
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002148MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002149static int ssl_tls13_write_server_hello(mbedtls_ssl_context *ssl)
Jerry Yu5b64ae92022-03-30 17:15:02 +08002150{
Jerry Yu637a3f12022-04-20 21:37:58 +08002151 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yuf4b27e42022-03-30 17:32:21 +08002152 unsigned char *buf;
2153 size_t buf_len, msg_len;
2154
Gilles Peskine449bd832023-01-11 14:50:10 +01002155 MBEDTLS_SSL_DEBUG_MSG(2, ("=> write server hello"));
Jerry Yuf4b27e42022-03-30 17:32:21 +08002156
Gilles Peskine449bd832023-01-11 14:50:10 +01002157 MBEDTLS_SSL_PROC_CHK(ssl_tls13_prepare_server_hello(ssl));
Jerry Yuf4b27e42022-03-30 17:32:21 +08002158
Gilles Peskine449bd832023-01-11 14:50:10 +01002159 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_start_handshake_msg(ssl,
2160 MBEDTLS_SSL_HS_SERVER_HELLO, &buf,
2161 &buf_len));
Jerry Yuf4b27e42022-03-30 17:32:21 +08002162
Gilles Peskine449bd832023-01-11 14:50:10 +01002163 MBEDTLS_SSL_PROC_CHK(ssl_tls13_write_server_hello_body(ssl, buf,
2164 buf + buf_len,
2165 &msg_len,
2166 0));
Jerry Yuf4b27e42022-03-30 17:32:21 +08002167
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +01002168 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_add_hs_msg_to_checksum(
Manuel Pégourié-Gonnard43cc1272023-02-06 11:48:19 +01002169 ssl, MBEDTLS_SSL_HS_SERVER_HELLO, buf, msg_len));
Jerry Yuf4b27e42022-03-30 17:32:21 +08002170
Gilles Peskine449bd832023-01-11 14:50:10 +01002171 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_finish_handshake_msg(
2172 ssl, buf_len, msg_len));
Jerry Yu637a3f12022-04-20 21:37:58 +08002173
Xiaokang Qian934ce6f2023-02-06 10:23:04 +00002174 MBEDTLS_SSL_PROC_CHK(ssl_tls13_finalize_server_hello(ssl));
Jerry Yue110d252022-05-05 10:19:22 +08002175
Gabor Mezei7b39bf12022-05-24 16:04:14 +02002176#if defined(MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE)
2177 /* The server sends a dummy change_cipher_spec record immediately
2178 * after its first handshake message. This may either be after
2179 * a ServerHello or a HelloRetryRequest.
2180 */
Gabor Mezei96ae9262022-06-28 11:45:18 +02002181 mbedtls_ssl_handshake_set_state(
Gilles Peskine449bd832023-01-11 14:50:10 +01002182 ssl, MBEDTLS_SSL_SERVER_CCS_AFTER_SERVER_HELLO);
Gabor Mezei7b39bf12022-05-24 16:04:14 +02002183#else
Gilles Peskine449bd832023-01-11 14:50:10 +01002184 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_ENCRYPTED_EXTENSIONS);
Gabor Mezei7b39bf12022-05-24 16:04:14 +02002185#endif /* MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE */
Jerry Yue110d252022-05-05 10:19:22 +08002186
Jerry Yuf4b27e42022-03-30 17:32:21 +08002187cleanup:
2188
Gilles Peskine449bd832023-01-11 14:50:10 +01002189 MBEDTLS_SSL_DEBUG_MSG(2, ("<= write server hello"));
2190 return ret;
Jerry Yu5b64ae92022-03-30 17:15:02 +08002191}
2192
Jerry Yu23d1a252022-05-12 18:08:59 +08002193
2194/*
2195 * Handler for MBEDTLS_SSL_HELLO_RETRY_REQUEST
2196 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002197MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002198static int ssl_tls13_prepare_hello_retry_request(mbedtls_ssl_context *ssl)
Jerry Yu23d1a252022-05-12 18:08:59 +08002199{
2200 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine449bd832023-01-11 14:50:10 +01002201 if (ssl->handshake->hello_retry_request_count > 0) {
2202 MBEDTLS_SSL_DEBUG_MSG(1, ("Too many HRRs"));
2203 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE,
2204 MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE);
2205 return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
Jerry Yu23d1a252022-05-12 18:08:59 +08002206 }
2207
2208 /*
2209 * Create stateless transcript hash for HRR
2210 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002211 MBEDTLS_SSL_DEBUG_MSG(4, ("Reset transcript for HRR"));
2212 ret = mbedtls_ssl_reset_transcript_for_hrr(ssl);
2213 if (ret != 0) {
2214 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_reset_transcript_for_hrr", ret);
2215 return ret;
Jerry Yu23d1a252022-05-12 18:08:59 +08002216 }
Gilles Peskine449bd832023-01-11 14:50:10 +01002217 mbedtls_ssl_session_reset_msg_layer(ssl, 0);
Jerry Yu23d1a252022-05-12 18:08:59 +08002218
Gilles Peskine449bd832023-01-11 14:50:10 +01002219 return 0;
Jerry Yu23d1a252022-05-12 18:08:59 +08002220}
2221
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002222MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002223static int ssl_tls13_write_hello_retry_request(mbedtls_ssl_context *ssl)
Jerry Yu23d1a252022-05-12 18:08:59 +08002224{
2225 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
2226 unsigned char *buf;
2227 size_t buf_len, msg_len;
2228
Gilles Peskine449bd832023-01-11 14:50:10 +01002229 MBEDTLS_SSL_DEBUG_MSG(2, ("=> write hello retry request"));
Jerry Yu23d1a252022-05-12 18:08:59 +08002230
Gilles Peskine449bd832023-01-11 14:50:10 +01002231 MBEDTLS_SSL_PROC_CHK(ssl_tls13_prepare_hello_retry_request(ssl));
Jerry Yu23d1a252022-05-12 18:08:59 +08002232
Gilles Peskine449bd832023-01-11 14:50:10 +01002233 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_start_handshake_msg(
2234 ssl, MBEDTLS_SSL_HS_SERVER_HELLO,
2235 &buf, &buf_len));
Jerry Yu23d1a252022-05-12 18:08:59 +08002236
Gilles Peskine449bd832023-01-11 14:50:10 +01002237 MBEDTLS_SSL_PROC_CHK(ssl_tls13_write_server_hello_body(ssl, buf,
2238 buf + buf_len,
2239 &msg_len,
2240 1));
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +01002241 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_add_hs_msg_to_checksum(
Manuel Pégourié-Gonnard43cc1272023-02-06 11:48:19 +01002242 ssl, MBEDTLS_SSL_HS_SERVER_HELLO, buf, msg_len));
Jerry Yu23d1a252022-05-12 18:08:59 +08002243
2244
Gilles Peskine449bd832023-01-11 14:50:10 +01002245 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_finish_handshake_msg(ssl, buf_len,
2246 msg_len));
Jerry Yu23d1a252022-05-12 18:08:59 +08002247
2248 ssl->handshake->hello_retry_request_count++;
2249
Gabor Mezei7b39bf12022-05-24 16:04:14 +02002250#if defined(MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE)
2251 /* The server sends a dummy change_cipher_spec record immediately
2252 * after its first handshake message. This may either be after
2253 * a ServerHello or a HelloRetryRequest.
2254 */
Gabor Mezei96ae9262022-06-28 11:45:18 +02002255 mbedtls_ssl_handshake_set_state(
Gilles Peskine449bd832023-01-11 14:50:10 +01002256 ssl, MBEDTLS_SSL_SERVER_CCS_AFTER_HELLO_RETRY_REQUEST);
Gabor Mezei7b39bf12022-05-24 16:04:14 +02002257#else
Gilles Peskine449bd832023-01-11 14:50:10 +01002258 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_CLIENT_HELLO);
Gabor Mezei7b39bf12022-05-24 16:04:14 +02002259#endif /* MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE */
Jerry Yu23d1a252022-05-12 18:08:59 +08002260
2261cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +01002262 MBEDTLS_SSL_DEBUG_MSG(2, ("<= write hello retry request"));
2263 return ret;
Jerry Yu23d1a252022-05-12 18:08:59 +08002264}
2265
Jerry Yu5b64ae92022-03-30 17:15:02 +08002266/*
Jerry Yu4d3841a2022-04-16 12:37:19 +08002267 * Handler for MBEDTLS_SSL_ENCRYPTED_EXTENSIONS
2268 */
Jerry Yu4d3841a2022-04-16 12:37:19 +08002269
2270/*
2271 * struct {
2272 * Extension extensions<0..2 ^ 16 - 1>;
2273 * } EncryptedExtensions;
2274 *
2275 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002276MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002277static int ssl_tls13_write_encrypted_extensions_body(mbedtls_ssl_context *ssl,
2278 unsigned char *buf,
2279 unsigned char *end,
2280 size_t *out_len)
Jerry Yu4d3841a2022-04-16 12:37:19 +08002281{
XiaokangQianacb39922022-06-17 10:18:48 +00002282 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yu4d3841a2022-04-16 12:37:19 +08002283 unsigned char *p = buf;
2284 size_t extensions_len = 0;
Jerry Yu8937eb42022-05-03 12:12:14 +08002285 unsigned char *p_extensions_len;
XiaokangQianacb39922022-06-17 10:18:48 +00002286 size_t output_len;
Jerry Yu9da5e5a2022-05-03 15:46:09 +08002287
Jerry Yu4d3841a2022-04-16 12:37:19 +08002288 *out_len = 0;
2289
Gilles Peskine449bd832023-01-11 14:50:10 +01002290 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 2);
Jerry Yu8937eb42022-05-03 12:12:14 +08002291 p_extensions_len = p;
Jerry Yu4d3841a2022-04-16 12:37:19 +08002292 p += 2;
2293
Jerry Yu8937eb42022-05-03 12:12:14 +08002294 ((void) ssl);
XiaokangQianacb39922022-06-17 10:18:48 +00002295 ((void) ret);
2296 ((void) output_len);
2297
2298#if defined(MBEDTLS_SSL_ALPN)
Gilles Peskine449bd832023-01-11 14:50:10 +01002299 ret = mbedtls_ssl_write_alpn_ext(ssl, p, end, &output_len);
2300 if (ret != 0) {
2301 return ret;
2302 }
XiaokangQian95d5f542022-06-24 02:29:26 +00002303 p += output_len;
XiaokangQianacb39922022-06-17 10:18:48 +00002304#endif /* MBEDTLS_SSL_ALPN */
Jerry Yu4d3841a2022-04-16 12:37:19 +08002305
Gilles Peskine449bd832023-01-11 14:50:10 +01002306 extensions_len = (p - p_extensions_len) - 2;
2307 MBEDTLS_PUT_UINT16_BE(extensions_len, p_extensions_len, 0);
Jerry Yu8937eb42022-05-03 12:12:14 +08002308
2309 *out_len = p - buf;
Jerry Yu4d3841a2022-04-16 12:37:19 +08002310
Gilles Peskine449bd832023-01-11 14:50:10 +01002311 MBEDTLS_SSL_DEBUG_BUF(4, "encrypted extensions", buf, *out_len);
Jerry Yu4d3841a2022-04-16 12:37:19 +08002312
Jerry Yu7de2ff02022-11-08 21:43:46 +08002313 MBEDTLS_SSL_PRINT_EXTS(
Gilles Peskine449bd832023-01-11 14:50:10 +01002314 3, MBEDTLS_SSL_HS_ENCRYPTED_EXTENSIONS, ssl->handshake->sent_extensions);
Jerry Yu4b8f2f72022-10-31 13:31:22 +08002315
Gilles Peskine449bd832023-01-11 14:50:10 +01002316 return 0;
Jerry Yu4d3841a2022-04-16 12:37:19 +08002317}
2318
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002319MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002320static int ssl_tls13_write_encrypted_extensions(mbedtls_ssl_context *ssl)
Jerry Yu4d3841a2022-04-16 12:37:19 +08002321{
2322 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
2323 unsigned char *buf;
Jerry Yu39730a72022-05-03 12:14:04 +08002324 size_t buf_len, msg_len;
Jerry Yu4d3841a2022-04-16 12:37:19 +08002325
Gilles Peskine449bd832023-01-11 14:50:10 +01002326 mbedtls_ssl_set_outbound_transform(ssl,
2327 ssl->handshake->transform_handshake);
Gabor Mezei54719122022-06-28 11:34:56 +02002328 MBEDTLS_SSL_DEBUG_MSG(
Gilles Peskine449bd832023-01-11 14:50:10 +01002329 3, ("switching to handshake transform for outbound data"));
Gabor Mezei54719122022-06-28 11:34:56 +02002330
Gilles Peskine449bd832023-01-11 14:50:10 +01002331 MBEDTLS_SSL_DEBUG_MSG(2, ("=> write encrypted extensions"));
Jerry Yu4d3841a2022-04-16 12:37:19 +08002332
Gilles Peskine449bd832023-01-11 14:50:10 +01002333 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_start_handshake_msg(ssl,
2334 MBEDTLS_SSL_HS_ENCRYPTED_EXTENSIONS, &buf,
2335 &buf_len));
Jerry Yu4d3841a2022-04-16 12:37:19 +08002336
Gilles Peskine449bd832023-01-11 14:50:10 +01002337 MBEDTLS_SSL_PROC_CHK(ssl_tls13_write_encrypted_extensions_body(
2338 ssl, buf, buf + buf_len, &msg_len));
Jerry Yu4d3841a2022-04-16 12:37:19 +08002339
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +01002340 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_add_hs_msg_to_checksum(
Manuel Pégourié-Gonnard43cc1272023-02-06 11:48:19 +01002341 ssl, MBEDTLS_SSL_HS_ENCRYPTED_EXTENSIONS, buf, msg_len));
Jerry Yu4d3841a2022-04-16 12:37:19 +08002342
Gilles Peskine449bd832023-01-11 14:50:10 +01002343 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_finish_handshake_msg(
2344 ssl, buf_len, msg_len));
Jerry Yu4d3841a2022-04-16 12:37:19 +08002345
Ronald Cron928cbd32022-10-04 16:14:26 +02002346#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01002347 if (mbedtls_ssl_tls13_key_exchange_mode_with_psk(ssl)) {
2348 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_SERVER_FINISHED);
2349 } else {
2350 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_CERTIFICATE_REQUEST);
2351 }
Jerry Yu8937eb42022-05-03 12:12:14 +08002352#else
Gilles Peskine449bd832023-01-11 14:50:10 +01002353 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_SERVER_FINISHED);
Jerry Yu8937eb42022-05-03 12:12:14 +08002354#endif
2355
Jerry Yu4d3841a2022-04-16 12:37:19 +08002356cleanup:
2357
Gilles Peskine449bd832023-01-11 14:50:10 +01002358 MBEDTLS_SSL_DEBUG_MSG(2, ("<= write encrypted extensions"));
2359 return ret;
Jerry Yu4d3841a2022-04-16 12:37:19 +08002360}
2361
Ronald Cron928cbd32022-10-04 16:14:26 +02002362#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED)
XiaokangQiana987e1d2022-05-07 01:25:58 +00002363#define SSL_CERTIFICATE_REQUEST_SEND_REQUEST 0
2364#define SSL_CERTIFICATE_REQUEST_SKIP 1
2365/* Coordination:
2366 * Check whether a CertificateRequest message should be written.
2367 * Returns a negative code on failure, or
2368 * - SSL_CERTIFICATE_REQUEST_SEND_REQUEST
2369 * - SSL_CERTIFICATE_REQUEST_SKIP
2370 * indicating if the writing of the CertificateRequest
2371 * should be skipped or not.
2372 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002373MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002374static int ssl_tls13_certificate_request_coordinate(mbedtls_ssl_context *ssl)
XiaokangQiana987e1d2022-05-07 01:25:58 +00002375{
2376 int authmode;
2377
XiaokangQian40a35232022-05-07 09:02:40 +00002378#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Gilles Peskine449bd832023-01-11 14:50:10 +01002379 if (ssl->handshake->sni_authmode != MBEDTLS_SSL_VERIFY_UNSET) {
XiaokangQian40a35232022-05-07 09:02:40 +00002380 authmode = ssl->handshake->sni_authmode;
Gilles Peskine449bd832023-01-11 14:50:10 +01002381 } else
XiaokangQian40a35232022-05-07 09:02:40 +00002382#endif
XiaokangQiana987e1d2022-05-07 01:25:58 +00002383 authmode = ssl->conf->authmode;
2384
Gilles Peskine449bd832023-01-11 14:50:10 +01002385 if (authmode == MBEDTLS_SSL_VERIFY_NONE) {
Ronald Croneac00ad2022-09-13 10:16:31 +02002386 ssl->session_negotiate->verify_result = MBEDTLS_X509_BADCERT_SKIP_VERIFY;
Gilles Peskine449bd832023-01-11 14:50:10 +01002387 return SSL_CERTIFICATE_REQUEST_SKIP;
Ronald Croneac00ad2022-09-13 10:16:31 +02002388 }
XiaokangQiana987e1d2022-05-07 01:25:58 +00002389
XiaokangQianc3017f62022-05-13 05:55:41 +00002390 ssl->handshake->certificate_request_sent = 1;
XiaokangQian189ded22022-05-10 08:12:17 +00002391
Gilles Peskine449bd832023-01-11 14:50:10 +01002392 return SSL_CERTIFICATE_REQUEST_SEND_REQUEST;
XiaokangQiana987e1d2022-05-07 01:25:58 +00002393}
2394
XiaokangQiancec9ae62022-05-06 07:28:50 +00002395/*
2396 * struct {
2397 * opaque certificate_request_context<0..2^8-1>;
2398 * Extension extensions<2..2^16-1>;
2399 * } CertificateRequest;
2400 *
2401 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002402MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002403static int ssl_tls13_write_certificate_request_body(mbedtls_ssl_context *ssl,
2404 unsigned char *buf,
2405 const unsigned char *end,
2406 size_t *out_len)
XiaokangQiancec9ae62022-05-06 07:28:50 +00002407{
2408 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
2409 unsigned char *p = buf;
XiaokangQianec6efb92022-05-06 09:53:10 +00002410 size_t output_len = 0;
XiaokangQiancec9ae62022-05-06 07:28:50 +00002411 unsigned char *p_extensions_len;
2412
2413 *out_len = 0;
2414
2415 /* Check if we have enough space:
2416 * - certificate_request_context (1 byte)
2417 * - extensions length (2 bytes)
2418 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002419 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 3);
XiaokangQiancec9ae62022-05-06 07:28:50 +00002420
2421 /*
2422 * Write certificate_request_context
2423 */
2424 /*
2425 * We use a zero length context for the normal handshake
2426 * messages. For post-authentication handshake messages
2427 * this request context would be set to a non-zero value.
2428 */
2429 *p++ = 0x0;
2430
2431 /*
2432 * Write extensions
2433 */
2434 /* The extensions must contain the signature_algorithms. */
2435 p_extensions_len = p;
2436 p += 2;
Gilles Peskine449bd832023-01-11 14:50:10 +01002437 ret = mbedtls_ssl_write_sig_alg_ext(ssl, p, end, &output_len);
2438 if (ret != 0) {
2439 return ret;
2440 }
XiaokangQiancec9ae62022-05-06 07:28:50 +00002441
XiaokangQianec6efb92022-05-06 09:53:10 +00002442 p += output_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01002443 MBEDTLS_PUT_UINT16_BE(p - p_extensions_len - 2, p_extensions_len, 0);
XiaokangQiancec9ae62022-05-06 07:28:50 +00002444
2445 *out_len = p - buf;
2446
Jerry Yu7de2ff02022-11-08 21:43:46 +08002447 MBEDTLS_SSL_PRINT_EXTS(
Gilles Peskine449bd832023-01-11 14:50:10 +01002448 3, MBEDTLS_SSL_HS_CERTIFICATE_REQUEST, ssl->handshake->sent_extensions);
Jerry Yu4b8f2f72022-10-31 13:31:22 +08002449
Gilles Peskine449bd832023-01-11 14:50:10 +01002450 return 0;
XiaokangQiancec9ae62022-05-06 07:28:50 +00002451}
2452
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002453MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002454static int ssl_tls13_write_certificate_request(mbedtls_ssl_context *ssl)
XiaokangQiancec9ae62022-05-06 07:28:50 +00002455{
2456 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
2457
Gilles Peskine449bd832023-01-11 14:50:10 +01002458 MBEDTLS_SSL_DEBUG_MSG(2, ("=> write certificate request"));
XiaokangQiancec9ae62022-05-06 07:28:50 +00002459
Gilles Peskine449bd832023-01-11 14:50:10 +01002460 MBEDTLS_SSL_PROC_CHK_NEG(ssl_tls13_certificate_request_coordinate(ssl));
XiaokangQiancec9ae62022-05-06 07:28:50 +00002461
Gilles Peskine449bd832023-01-11 14:50:10 +01002462 if (ret == SSL_CERTIFICATE_REQUEST_SEND_REQUEST) {
XiaokangQiancec9ae62022-05-06 07:28:50 +00002463 unsigned char *buf;
2464 size_t buf_len, msg_len;
2465
Gilles Peskine449bd832023-01-11 14:50:10 +01002466 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_start_handshake_msg(ssl,
2467 MBEDTLS_SSL_HS_CERTIFICATE_REQUEST,
2468 &buf, &buf_len));
XiaokangQiancec9ae62022-05-06 07:28:50 +00002469
Gilles Peskine449bd832023-01-11 14:50:10 +01002470 MBEDTLS_SSL_PROC_CHK(ssl_tls13_write_certificate_request_body(
2471 ssl, buf, buf + buf_len, &msg_len));
XiaokangQiancec9ae62022-05-06 07:28:50 +00002472
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +01002473 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_add_hs_msg_to_checksum(
Manuel Pégourié-Gonnard43cc1272023-02-06 11:48:19 +01002474 ssl, MBEDTLS_SSL_HS_CERTIFICATE_REQUEST, buf, msg_len));
XiaokangQiancec9ae62022-05-06 07:28:50 +00002475
Gilles Peskine449bd832023-01-11 14:50:10 +01002476 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_finish_handshake_msg(
2477 ssl, buf_len, msg_len));
2478 } else if (ret == SSL_CERTIFICATE_REQUEST_SKIP) {
2479 MBEDTLS_SSL_DEBUG_MSG(2, ("<= skip write certificate request"));
XiaokangQiancec9ae62022-05-06 07:28:50 +00002480 ret = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01002481 } else {
2482 MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
XiaokangQiancec9ae62022-05-06 07:28:50 +00002483 ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
2484 goto cleanup;
2485 }
2486
Gilles Peskine449bd832023-01-11 14:50:10 +01002487 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_SERVER_CERTIFICATE);
XiaokangQiancec9ae62022-05-06 07:28:50 +00002488cleanup:
2489
Gilles Peskine449bd832023-01-11 14:50:10 +01002490 MBEDTLS_SSL_DEBUG_MSG(2, ("<= write certificate request"));
2491 return ret;
XiaokangQiancec9ae62022-05-06 07:28:50 +00002492}
XiaokangQiancec9ae62022-05-06 07:28:50 +00002493
Jerry Yu4d3841a2022-04-16 12:37:19 +08002494/*
Jerry Yuc6e6dbf2022-04-16 19:42:57 +08002495 * Handler for MBEDTLS_SSL_SERVER_CERTIFICATE
Jerry Yu83da34e2022-04-16 13:59:52 +08002496 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002497MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002498static int ssl_tls13_write_server_certificate(mbedtls_ssl_context *ssl)
Jerry Yu83da34e2022-04-16 13:59:52 +08002499{
Jerry Yu5a26f302022-05-10 20:46:40 +08002500 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
XiaokangQianfb665a82022-06-15 03:57:21 +00002501
2502#if defined(MBEDTLS_X509_CRT_PARSE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01002503 if ((ssl_tls13_pick_key_cert(ssl) != 0) ||
2504 mbedtls_ssl_own_cert(ssl) == NULL) {
2505 MBEDTLS_SSL_DEBUG_MSG(2, ("No certificate available."));
2506 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE,
2507 MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE);
2508 return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
Jerry Yu5a26f302022-05-10 20:46:40 +08002509 }
XiaokangQianfb665a82022-06-15 03:57:21 +00002510#endif /* MBEDTLS_X509_CRT_PARSE_C */
Jerry Yu5a26f302022-05-10 20:46:40 +08002511
Gilles Peskine449bd832023-01-11 14:50:10 +01002512 ret = mbedtls_ssl_tls13_write_certificate(ssl);
2513 if (ret != 0) {
2514 return ret;
2515 }
2516 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_CERTIFICATE_VERIFY);
2517 return 0;
Jerry Yu83da34e2022-04-16 13:59:52 +08002518}
2519
2520/*
Jerry Yuc6e6dbf2022-04-16 19:42:57 +08002521 * Handler for MBEDTLS_SSL_CERTIFICATE_VERIFY
Jerry Yu83da34e2022-04-16 13:59:52 +08002522 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002523MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002524static int ssl_tls13_write_certificate_verify(mbedtls_ssl_context *ssl)
Jerry Yu83da34e2022-04-16 13:59:52 +08002525{
Gilles Peskine449bd832023-01-11 14:50:10 +01002526 int ret = mbedtls_ssl_tls13_write_certificate_verify(ssl);
2527 if (ret != 0) {
2528 return ret;
2529 }
2530 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_SERVER_FINISHED);
2531 return 0;
Jerry Yu83da34e2022-04-16 13:59:52 +08002532}
Ronald Cron928cbd32022-10-04 16:14:26 +02002533#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED */
Jerry Yu83da34e2022-04-16 13:59:52 +08002534
2535/*
Jerry Yud6e253d2022-05-18 13:59:24 +08002536 * Handler for MBEDTLS_SSL_SERVER_FINISHED
Jerry Yu69dd8d42022-04-16 12:51:26 +08002537 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002538MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002539static int ssl_tls13_write_server_finished(mbedtls_ssl_context *ssl)
Jerry Yu69dd8d42022-04-16 12:51:26 +08002540{
Jerry Yud6e253d2022-05-18 13:59:24 +08002541 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yu27bdc7c2022-04-16 13:33:27 +08002542
Gilles Peskine449bd832023-01-11 14:50:10 +01002543 ret = mbedtls_ssl_tls13_write_finished_message(ssl);
2544 if (ret != 0) {
2545 return ret;
2546 }
Jerry Yu27bdc7c2022-04-16 13:33:27 +08002547
Gilles Peskine449bd832023-01-11 14:50:10 +01002548 ret = mbedtls_ssl_tls13_compute_application_transform(ssl);
2549 if (ret != 0) {
Jerry Yue3d67cb2022-05-19 15:33:10 +08002550 MBEDTLS_SSL_PEND_FATAL_ALERT(
Gilles Peskine449bd832023-01-11 14:50:10 +01002551 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE,
2552 MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE);
2553 return ret;
Jerry Yue3d67cb2022-05-19 15:33:10 +08002554 }
XiaokangQianc3017f62022-05-13 05:55:41 +00002555
Gilles Peskine449bd832023-01-11 14:50:10 +01002556 MBEDTLS_SSL_DEBUG_MSG(1, ("Switch to handshake keys for inbound traffic"));
2557 mbedtls_ssl_set_inbound_transform(ssl, ssl->handshake->transform_handshake);
XiaokangQian189ded22022-05-10 08:12:17 +00002558
Gilles Peskine449bd832023-01-11 14:50:10 +01002559 if (ssl->handshake->certificate_request_sent) {
2560 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_CLIENT_CERTIFICATE);
2561 } else {
2562 MBEDTLS_SSL_DEBUG_MSG(2, ("skip parse certificate"));
2563 MBEDTLS_SSL_DEBUG_MSG(2, ("skip parse certificate verify"));
2564 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_CLIENT_FINISHED);
Ronald Cron19385882022-06-15 16:26:13 +02002565 }
Ronald Cron63dc4632022-05-31 14:41:53 +02002566
Gilles Peskine449bd832023-01-11 14:50:10 +01002567 return 0;
Jerry Yu69dd8d42022-04-16 12:51:26 +08002568}
2569
2570/*
Jerry Yud6e253d2022-05-18 13:59:24 +08002571 * Handler for MBEDTLS_SSL_CLIENT_FINISHED
Jerry Yu69dd8d42022-04-16 12:51:26 +08002572 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002573MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002574static int ssl_tls13_process_client_finished(mbedtls_ssl_context *ssl)
Jerry Yu69dd8d42022-04-16 12:51:26 +08002575{
Jerry Yud6e253d2022-05-18 13:59:24 +08002576 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
2577
Gilles Peskine449bd832023-01-11 14:50:10 +01002578 ret = mbedtls_ssl_tls13_process_finished_message(ssl);
2579 if (ret != 0) {
2580 return ret;
Jerry Yue3d67cb2022-05-19 15:33:10 +08002581 }
2582
Gilles Peskine449bd832023-01-11 14:50:10 +01002583 ret = mbedtls_ssl_tls13_compute_resumption_master_secret(ssl);
2584 if (ret != 0) {
2585 MBEDTLS_SSL_DEBUG_RET(1,
2586 "mbedtls_ssl_tls13_compute_resumption_master_secret", ret);
2587 }
2588
2589 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_HANDSHAKE_WRAPUP);
2590 return 0;
Jerry Yu69dd8d42022-04-16 12:51:26 +08002591}
2592
2593/*
Jerry Yud6e253d2022-05-18 13:59:24 +08002594 * Handler for MBEDTLS_SSL_HANDSHAKE_WRAPUP
Jerry Yu69dd8d42022-04-16 12:51:26 +08002595 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002596MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002597static int ssl_tls13_handshake_wrapup(mbedtls_ssl_context *ssl)
Jerry Yu69dd8d42022-04-16 12:51:26 +08002598{
Gilles Peskine449bd832023-01-11 14:50:10 +01002599 MBEDTLS_SSL_DEBUG_MSG(2, ("handshake: done"));
Jerry Yu03ed50b2022-04-16 17:13:30 +08002600
Gilles Peskine449bd832023-01-11 14:50:10 +01002601 mbedtls_ssl_tls13_handshake_wrapup(ssl);
Jerry Yue67bef42022-07-07 07:29:42 +00002602
Pengyu Lv3643fdb2023-01-12 16:46:28 +08002603#if defined(MBEDTLS_SSL_SESSION_TICKETS) && \
2604 defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED)
Pengyu Lva1aa31b2022-12-13 13:49:59 +08002605/* TODO: Remove the check of SOME_PSK_ENABLED since SESSION_TICKETS requires
2606 * SOME_PSK_ENABLED to be enabled. Here is just to make CI happy. It is
2607 * expected to be resolved with issue#6395.
2608 */
Pengyu Lvc7af2c42022-12-01 16:33:00 +08002609 /* Sent NewSessionTicket message only when client supports PSK */
Pengyu Lv3643fdb2023-01-12 16:46:28 +08002610 if (mbedtls_ssl_tls13_some_psk_enabled(ssl)) {
2611 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_TLS1_3_NEW_SESSION_TICKET);
Pengyu Lvc7af2c42022-12-01 16:33:00 +08002612 } else
Jerry Yufca4d572022-07-21 10:37:48 +08002613#endif
Pengyu Lv3643fdb2023-01-12 16:46:28 +08002614 {
2615 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_HANDSHAKE_OVER);
2616 }
Gilles Peskine449bd832023-01-11 14:50:10 +01002617 return 0;
Jerry Yu69dd8d42022-04-16 12:51:26 +08002618}
2619
2620/*
Jerry Yua8d3c502022-10-30 14:51:23 +08002621 * Handler for MBEDTLS_SSL_TLS1_3_NEW_SESSION_TICKET
Jerry Yue67bef42022-07-07 07:29:42 +00002622 */
2623#define SSL_NEW_SESSION_TICKET_SKIP 0
2624#define SSL_NEW_SESSION_TICKET_WRITE 1
2625MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002626static int ssl_tls13_write_new_session_ticket_coordinate(mbedtls_ssl_context *ssl)
Jerry Yue67bef42022-07-07 07:29:42 +00002627{
2628 /* Check whether the use of session tickets is enabled */
Gilles Peskine449bd832023-01-11 14:50:10 +01002629 if (ssl->conf->f_ticket_write == NULL) {
2630 MBEDTLS_SSL_DEBUG_MSG(2, ("NewSessionTicket: disabled,"
2631 " callback is not set"));
2632 return SSL_NEW_SESSION_TICKET_SKIP;
Jerry Yud0766ec2022-09-22 10:46:57 +08002633 }
Gilles Peskine449bd832023-01-11 14:50:10 +01002634 if (ssl->conf->new_session_tickets_count == 0) {
2635 MBEDTLS_SSL_DEBUG_MSG(2, ("NewSessionTicket: disabled,"
2636 " configured count is zero"));
2637 return SSL_NEW_SESSION_TICKET_SKIP;
Jerry Yud0766ec2022-09-22 10:46:57 +08002638 }
2639
Gilles Peskine449bd832023-01-11 14:50:10 +01002640 if (ssl->handshake->new_session_tickets_count == 0) {
2641 MBEDTLS_SSL_DEBUG_MSG(2, ("NewSessionTicket: all tickets have "
2642 "been sent."));
2643 return SSL_NEW_SESSION_TICKET_SKIP;
Jerry Yue67bef42022-07-07 07:29:42 +00002644 }
2645
Gilles Peskine449bd832023-01-11 14:50:10 +01002646 return SSL_NEW_SESSION_TICKET_WRITE;
Jerry Yue67bef42022-07-07 07:29:42 +00002647}
2648
2649#if defined(MBEDTLS_SSL_SESSION_TICKETS)
2650MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002651static int ssl_tls13_prepare_new_session_ticket(mbedtls_ssl_context *ssl,
2652 unsigned char *ticket_nonce,
2653 size_t ticket_nonce_size)
Jerry Yue67bef42022-07-07 07:29:42 +00002654{
2655 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
2656 mbedtls_ssl_session *session = ssl->session;
2657 mbedtls_ssl_ciphersuite_t *ciphersuite_info;
2658 psa_algorithm_t psa_hash_alg;
2659 int hash_length;
2660
Gilles Peskine449bd832023-01-11 14:50:10 +01002661 MBEDTLS_SSL_DEBUG_MSG(2, ("=> prepare NewSessionTicket msg"));
Jerry Yue67bef42022-07-07 07:29:42 +00002662
2663#if defined(MBEDTLS_HAVE_TIME)
Gilles Peskine449bd832023-01-11 14:50:10 +01002664 session->start = mbedtls_time(NULL);
Jerry Yue67bef42022-07-07 07:29:42 +00002665#endif
2666
Pengyu Lv9f926952022-11-17 15:22:33 +08002667 /* Set ticket_flags depends on the advertised psk key exchange mode */
Pengyu Lv80270b22023-01-12 11:54:04 +08002668 mbedtls_ssl_session_clear_ticket_flags(
Pengyu Lv9eacb442022-12-09 14:39:19 +08002669 session, MBEDTLS_SSL_TLS1_3_TICKET_FLAGS_MASK);
Pengyu Lve6487fe2022-12-06 09:30:29 +08002670#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED)
Pengyu Lv80270b22023-01-12 11:54:04 +08002671 mbedtls_ssl_session_set_ticket_flags(
Pengyu Lv9eacb442022-12-09 14:39:19 +08002672 session, ssl->handshake->tls13_kex_modes);
Pengyu Lve6487fe2022-12-06 09:30:29 +08002673#endif
Pengyu Lv4938a562023-01-16 11:28:49 +08002674 MBEDTLS_SSL_PRINT_TICKET_FLAGS(4, session->ticket_flags);
Pengyu Lv9f926952022-11-17 15:22:33 +08002675
Jerry Yue67bef42022-07-07 07:29:42 +00002676 /* Generate ticket_age_add */
Gilles Peskine449bd832023-01-11 14:50:10 +01002677 if ((ret = ssl->conf->f_rng(ssl->conf->p_rng,
2678 (unsigned char *) &session->ticket_age_add,
2679 sizeof(session->ticket_age_add)) != 0)) {
2680 MBEDTLS_SSL_DEBUG_RET(1, "generate_ticket_age_add", ret);
2681 return ret;
Jerry Yue67bef42022-07-07 07:29:42 +00002682 }
Gilles Peskine449bd832023-01-11 14:50:10 +01002683 MBEDTLS_SSL_DEBUG_MSG(3, ("ticket_age_add: %u",
2684 (unsigned int) session->ticket_age_add));
Jerry Yue67bef42022-07-07 07:29:42 +00002685
2686 /* Generate ticket_nonce */
Gilles Peskine449bd832023-01-11 14:50:10 +01002687 ret = ssl->conf->f_rng(ssl->conf->p_rng, ticket_nonce, ticket_nonce_size);
2688 if (ret != 0) {
2689 MBEDTLS_SSL_DEBUG_RET(1, "generate_ticket_nonce", ret);
2690 return ret;
Jerry Yue67bef42022-07-07 07:29:42 +00002691 }
Gilles Peskine449bd832023-01-11 14:50:10 +01002692 MBEDTLS_SSL_DEBUG_BUF(3, "ticket_nonce:",
2693 ticket_nonce, ticket_nonce_size);
Jerry Yue67bef42022-07-07 07:29:42 +00002694
2695 ciphersuite_info =
Gilles Peskine449bd832023-01-11 14:50:10 +01002696 (mbedtls_ssl_ciphersuite_t *) ssl->handshake->ciphersuite_info;
2697 psa_hash_alg = mbedtls_psa_translate_md(ciphersuite_info->mac);
2698 hash_length = PSA_HASH_LENGTH(psa_hash_alg);
2699 if (hash_length == -1 ||
2700 (size_t) hash_length > sizeof(session->resumption_key)) {
2701 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Jerry Yufca4d572022-07-21 10:37:48 +08002702 }
2703
Jerry Yue67bef42022-07-07 07:29:42 +00002704 /* In this code the psk key length equals the length of the hash */
2705 session->resumption_key_len = hash_length;
2706 session->ciphersuite = ciphersuite_info->id;
2707
2708 /* Compute resumption key
2709 *
2710 * HKDF-Expand-Label( resumption_master_secret,
2711 * "resumption", ticket_nonce, Hash.length )
2712 */
2713 ret = mbedtls_ssl_tls13_hkdf_expand_label(
Gilles Peskine449bd832023-01-11 14:50:10 +01002714 psa_hash_alg,
2715 session->app_secrets.resumption_master_secret,
2716 hash_length,
2717 MBEDTLS_SSL_TLS1_3_LBL_WITH_LEN(resumption),
2718 ticket_nonce,
2719 ticket_nonce_size,
2720 session->resumption_key,
2721 hash_length);
Jerry Yue67bef42022-07-07 07:29:42 +00002722
Gilles Peskine449bd832023-01-11 14:50:10 +01002723 if (ret != 0) {
2724 MBEDTLS_SSL_DEBUG_RET(2,
2725 "Creating the ticket-resumed PSK failed",
2726 ret);
2727 return ret;
Jerry Yue67bef42022-07-07 07:29:42 +00002728 }
Gilles Peskine449bd832023-01-11 14:50:10 +01002729 MBEDTLS_SSL_DEBUG_BUF(3, "Ticket-resumed PSK",
2730 session->resumption_key,
2731 session->resumption_key_len);
Jerry Yue67bef42022-07-07 07:29:42 +00002732
Gilles Peskine449bd832023-01-11 14:50:10 +01002733 MBEDTLS_SSL_DEBUG_BUF(3, "resumption_master_secret",
2734 session->app_secrets.resumption_master_secret,
2735 hash_length);
Jerry Yue67bef42022-07-07 07:29:42 +00002736
Gilles Peskine449bd832023-01-11 14:50:10 +01002737 return 0;
Jerry Yue67bef42022-07-07 07:29:42 +00002738}
2739
2740/* This function creates a NewSessionTicket message in the following format:
2741 *
2742 * struct {
2743 * uint32 ticket_lifetime;
2744 * uint32 ticket_age_add;
2745 * opaque ticket_nonce<0..255>;
2746 * opaque ticket<1..2^16-1>;
2747 * Extension extensions<0..2^16-2>;
2748 * } NewSessionTicket;
2749 *
2750 * The ticket inside the NewSessionTicket message is an encrypted container
2751 * carrying the necessary information so that the server is later able to
2752 * re-start the communication.
2753 *
2754 * The following fields are placed inside the ticket by the
2755 * f_ticket_write() function:
2756 *
2757 * - creation time (start)
2758 * - flags (flags)
2759 * - age add (ticket_age_add)
2760 * - key (key)
2761 * - key length (key_len)
2762 * - ciphersuite (ciphersuite)
2763 */
2764MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002765static int ssl_tls13_write_new_session_ticket_body(mbedtls_ssl_context *ssl,
2766 unsigned char *buf,
2767 unsigned char *end,
2768 size_t *out_len,
2769 unsigned char *ticket_nonce,
2770 size_t ticket_nonce_size)
Jerry Yue67bef42022-07-07 07:29:42 +00002771{
2772 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
2773 unsigned char *p = buf;
2774 mbedtls_ssl_session *session = ssl->session;
2775 size_t ticket_len;
2776 uint32_t ticket_lifetime;
2777
2778 *out_len = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01002779 MBEDTLS_SSL_DEBUG_MSG(2, ("=> write NewSessionTicket msg"));
Jerry Yue67bef42022-07-07 07:29:42 +00002780
2781 /*
2782 * ticket_lifetime 4 bytes
2783 * ticket_age_add 4 bytes
2784 * ticket_nonce 1 + ticket_nonce_size bytes
2785 * ticket >=2 bytes
2786 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002787 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 4 + 4 + 1 + ticket_nonce_size + 2);
Jerry Yue67bef42022-07-07 07:29:42 +00002788
2789 /* Generate ticket and ticket_lifetime */
Gilles Peskine449bd832023-01-11 14:50:10 +01002790 ret = ssl->conf->f_ticket_write(ssl->conf->p_ticket,
2791 session,
2792 p + 9 + ticket_nonce_size + 2,
2793 end,
2794 &ticket_len,
2795 &ticket_lifetime);
2796 if (ret != 0) {
2797 MBEDTLS_SSL_DEBUG_RET(1, "write_ticket", ret);
2798 return ret;
Jerry Yue67bef42022-07-07 07:29:42 +00002799 }
2800 /* RFC 8446 4.6.1
2801 * ticket_lifetime: Indicates the lifetime in seconds as a 32-bit
2802 * unsigned integer in network byte order from the time of ticket
2803 * issuance. Servers MUST NOT use any value greater than
2804 * 604800 seconds (7 days). The value of zero indicates that the
2805 * ticket should be discarded immediately. Clients MUST NOT cache
2806 * tickets for longer than 7 days, regardless of the ticket_lifetime,
2807 * and MAY delete tickets earlier based on local policy. A server
2808 * MAY treat a ticket as valid for a shorter period of time than what
2809 * is stated in the ticket_lifetime.
2810 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002811 if (ticket_lifetime > 604800) {
Xiaokang Qian28af5012022-10-13 08:18:19 +00002812 ticket_lifetime = 604800;
Gilles Peskine449bd832023-01-11 14:50:10 +01002813 }
2814 MBEDTLS_PUT_UINT32_BE(ticket_lifetime, p, 0);
2815 MBEDTLS_SSL_DEBUG_MSG(3, ("ticket_lifetime: %u",
2816 (unsigned int) ticket_lifetime));
Jerry Yue67bef42022-07-07 07:29:42 +00002817
2818 /* Write ticket_age_add */
Gilles Peskine449bd832023-01-11 14:50:10 +01002819 MBEDTLS_PUT_UINT32_BE(session->ticket_age_add, p, 4);
2820 MBEDTLS_SSL_DEBUG_MSG(3, ("ticket_age_add: %u",
2821 (unsigned int) session->ticket_age_add));
Jerry Yue67bef42022-07-07 07:29:42 +00002822
2823 /* Write ticket_nonce */
Gilles Peskine449bd832023-01-11 14:50:10 +01002824 p[8] = (unsigned char) ticket_nonce_size;
2825 if (ticket_nonce_size > 0) {
2826 memcpy(p + 9, ticket_nonce, ticket_nonce_size);
Jerry Yue67bef42022-07-07 07:29:42 +00002827 }
2828 p += 9 + ticket_nonce_size;
2829
2830 /* Write ticket */
Gilles Peskine449bd832023-01-11 14:50:10 +01002831 MBEDTLS_PUT_UINT16_BE(ticket_len, p, 0);
Jerry Yue67bef42022-07-07 07:29:42 +00002832 p += 2;
Gilles Peskine449bd832023-01-11 14:50:10 +01002833 MBEDTLS_SSL_DEBUG_BUF(4, "ticket", p, ticket_len);
Jerry Yue67bef42022-07-07 07:29:42 +00002834 p += ticket_len;
2835
2836 /* Ticket Extensions
2837 *
2838 * Note: We currently don't have any extensions.
2839 * Set length to zero.
2840 */
Jerry Yuedab6372022-10-31 14:37:31 +08002841 ssl->handshake->sent_extensions = MBEDTLS_SSL_EXT_MASK_NONE;
2842
Gilles Peskine449bd832023-01-11 14:50:10 +01002843 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 2);
2844 MBEDTLS_PUT_UINT16_BE(0, p, 0);
Jerry Yue67bef42022-07-07 07:29:42 +00002845 p += 2;
2846
2847 *out_len = p - buf;
Gilles Peskine449bd832023-01-11 14:50:10 +01002848 MBEDTLS_SSL_DEBUG_BUF(4, "ticket", buf, *out_len);
2849 MBEDTLS_SSL_DEBUG_MSG(2, ("<= write new session ticket"));
Jerry Yue67bef42022-07-07 07:29:42 +00002850
Jerry Yu7de2ff02022-11-08 21:43:46 +08002851 MBEDTLS_SSL_PRINT_EXTS(
Gilles Peskine449bd832023-01-11 14:50:10 +01002852 3, MBEDTLS_SSL_HS_NEW_SESSION_TICKET, ssl->handshake->sent_extensions);
Jerry Yu4b8f2f72022-10-31 13:31:22 +08002853
Gilles Peskine449bd832023-01-11 14:50:10 +01002854 return 0;
Jerry Yue67bef42022-07-07 07:29:42 +00002855}
2856
2857/*
Jerry Yua8d3c502022-10-30 14:51:23 +08002858 * Handler for MBEDTLS_SSL_TLS1_3_NEW_SESSION_TICKET
Jerry Yue67bef42022-07-07 07:29:42 +00002859 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002860static int ssl_tls13_write_new_session_ticket(mbedtls_ssl_context *ssl)
Jerry Yue67bef42022-07-07 07:29:42 +00002861{
2862 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
2863
Gilles Peskine449bd832023-01-11 14:50:10 +01002864 MBEDTLS_SSL_PROC_CHK_NEG(ssl_tls13_write_new_session_ticket_coordinate(ssl));
Jerry Yue67bef42022-07-07 07:29:42 +00002865
Gilles Peskine449bd832023-01-11 14:50:10 +01002866 if (ret == SSL_NEW_SESSION_TICKET_WRITE) {
Jerry Yue67bef42022-07-07 07:29:42 +00002867 unsigned char ticket_nonce[MBEDTLS_SSL_TLS1_3_TICKET_NONCE_LENGTH];
2868 unsigned char *buf;
2869 size_t buf_len, msg_len;
2870
Gilles Peskine449bd832023-01-11 14:50:10 +01002871 MBEDTLS_SSL_PROC_CHK(ssl_tls13_prepare_new_session_ticket(
2872 ssl, ticket_nonce, sizeof(ticket_nonce)));
Jerry Yue67bef42022-07-07 07:29:42 +00002873
Gilles Peskine449bd832023-01-11 14:50:10 +01002874 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_start_handshake_msg(ssl,
2875 MBEDTLS_SSL_HS_NEW_SESSION_TICKET,
2876 &buf, &buf_len));
Jerry Yue67bef42022-07-07 07:29:42 +00002877
Gilles Peskine449bd832023-01-11 14:50:10 +01002878 MBEDTLS_SSL_PROC_CHK(ssl_tls13_write_new_session_ticket_body(
2879 ssl, buf, buf + buf_len, &msg_len,
2880 ticket_nonce, sizeof(ticket_nonce)));
Jerry Yue67bef42022-07-07 07:29:42 +00002881
Gilles Peskine449bd832023-01-11 14:50:10 +01002882 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_finish_handshake_msg(
2883 ssl, buf_len, msg_len));
Jerry Yufca4d572022-07-21 10:37:48 +08002884
Jerry Yu359e65f2022-09-22 23:47:43 +08002885 /* Limit session tickets count to one when resumption connection.
2886 *
2887 * See document of mbedtls_ssl_conf_new_session_tickets.
2888 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002889 if (ssl->handshake->resume == 1) {
Jerry Yu359e65f2022-09-22 23:47:43 +08002890 ssl->handshake->new_session_tickets_count = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01002891 } else {
Jerry Yu359e65f2022-09-22 23:47:43 +08002892 ssl->handshake->new_session_tickets_count--;
Gilles Peskine449bd832023-01-11 14:50:10 +01002893 }
Jerry Yub7e3fa72022-09-22 11:07:18 +08002894
Jerry Yua8d3c502022-10-30 14:51:23 +08002895 mbedtls_ssl_handshake_set_state(
Gilles Peskine449bd832023-01-11 14:50:10 +01002896 ssl, MBEDTLS_SSL_TLS1_3_NEW_SESSION_TICKET_FLUSH);
2897 } else {
2898 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_HANDSHAKE_OVER);
Jerry Yue67bef42022-07-07 07:29:42 +00002899 }
2900
Jerry Yue67bef42022-07-07 07:29:42 +00002901cleanup:
2902
Gilles Peskine449bd832023-01-11 14:50:10 +01002903 return ret;
Jerry Yue67bef42022-07-07 07:29:42 +00002904}
2905#endif /* MBEDTLS_SSL_SESSION_TICKETS */
2906
2907/*
XiaokangQiane8ff3502022-04-22 02:34:40 +00002908 * TLS 1.3 State Machine -- server side
XiaokangQian7807f9f2022-02-15 10:04:37 +00002909 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002910int mbedtls_ssl_tls13_handshake_server_step(mbedtls_ssl_context *ssl)
Jerry Yub9930e72021-08-06 17:11:51 +08002911{
XiaokangQian08037552022-04-20 07:16:41 +00002912 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
XiaokangQian7807f9f2022-02-15 10:04:37 +00002913
Gilles Peskine449bd832023-01-11 14:50:10 +01002914 if (ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER || ssl->handshake == NULL) {
2915 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
2916 }
XiaokangQian7807f9f2022-02-15 10:04:37 +00002917
Gilles Peskine449bd832023-01-11 14:50:10 +01002918 MBEDTLS_SSL_DEBUG_MSG(2, ("tls13 server state: %s(%d)",
2919 mbedtls_ssl_states_str(ssl->state),
2920 ssl->state));
Jerry Yu6e81b272021-09-27 11:16:17 +08002921
Gilles Peskine449bd832023-01-11 14:50:10 +01002922 switch (ssl->state) {
XiaokangQian7807f9f2022-02-15 10:04:37 +00002923 /* start state */
2924 case MBEDTLS_SSL_HELLO_REQUEST:
Gilles Peskine449bd832023-01-11 14:50:10 +01002925 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_CLIENT_HELLO);
XiaokangQian08037552022-04-20 07:16:41 +00002926 ret = 0;
XiaokangQian7807f9f2022-02-15 10:04:37 +00002927 break;
2928
XiaokangQian7807f9f2022-02-15 10:04:37 +00002929 case MBEDTLS_SSL_CLIENT_HELLO:
Gilles Peskine449bd832023-01-11 14:50:10 +01002930 ret = ssl_tls13_process_client_hello(ssl);
2931 if (ret != 0) {
2932 MBEDTLS_SSL_DEBUG_RET(1, "ssl_tls13_process_client_hello", ret);
2933 }
Jerry Yuf41553b2022-05-09 22:20:30 +08002934 break;
XiaokangQian7807f9f2022-02-15 10:04:37 +00002935
Jerry Yuf41553b2022-05-09 22:20:30 +08002936 case MBEDTLS_SSL_HELLO_RETRY_REQUEST:
Gilles Peskine449bd832023-01-11 14:50:10 +01002937 ret = ssl_tls13_write_hello_retry_request(ssl);
2938 if (ret != 0) {
2939 MBEDTLS_SSL_DEBUG_RET(1, "ssl_tls13_write_hello_retry_request", ret);
2940 return ret;
Jerry Yuf41553b2022-05-09 22:20:30 +08002941 }
XiaokangQian7807f9f2022-02-15 10:04:37 +00002942 break;
2943
Jerry Yu5b64ae92022-03-30 17:15:02 +08002944 case MBEDTLS_SSL_SERVER_HELLO:
Gilles Peskine449bd832023-01-11 14:50:10 +01002945 ret = ssl_tls13_write_server_hello(ssl);
Jerry Yu5b64ae92022-03-30 17:15:02 +08002946 break;
2947
Xiaofei Baicba64af2022-02-15 10:00:56 +00002948 case MBEDTLS_SSL_ENCRYPTED_EXTENSIONS:
Gilles Peskine449bd832023-01-11 14:50:10 +01002949 ret = ssl_tls13_write_encrypted_extensions(ssl);
2950 if (ret != 0) {
2951 MBEDTLS_SSL_DEBUG_RET(1, "ssl_tls13_write_encrypted_extensions", ret);
2952 return ret;
Xiaofei Baicba64af2022-02-15 10:00:56 +00002953 }
Jerry Yu48330562022-05-06 21:35:44 +08002954 break;
Jerry Yu6a2cd9e2022-05-05 11:14:19 +08002955
Ronald Cron928cbd32022-10-04 16:14:26 +02002956#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED)
Xiaofei Bai9ca09d42022-02-14 12:57:18 +00002957 case MBEDTLS_SSL_CERTIFICATE_REQUEST:
Gilles Peskine449bd832023-01-11 14:50:10 +01002958 ret = ssl_tls13_write_certificate_request(ssl);
Xiaofei Bai9ca09d42022-02-14 12:57:18 +00002959 break;
Xiaofei Bai9ca09d42022-02-14 12:57:18 +00002960
Jerry Yu83da34e2022-04-16 13:59:52 +08002961 case MBEDTLS_SSL_SERVER_CERTIFICATE:
Gilles Peskine449bd832023-01-11 14:50:10 +01002962 ret = ssl_tls13_write_server_certificate(ssl);
Jerry Yu83da34e2022-04-16 13:59:52 +08002963 break;
2964
2965 case MBEDTLS_SSL_CERTIFICATE_VERIFY:
Gilles Peskine449bd832023-01-11 14:50:10 +01002966 ret = ssl_tls13_write_certificate_verify(ssl);
Jerry Yu83da34e2022-04-16 13:59:52 +08002967 break;
Ronald Cron928cbd32022-10-04 16:14:26 +02002968#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED */
Jerry Yu83da34e2022-04-16 13:59:52 +08002969
Gilles Peskine449bd832023-01-11 14:50:10 +01002970 /*
2971 * Injection of dummy-CCS's for middlebox compatibility
2972 */
Gabor Mezei7b39bf12022-05-24 16:04:14 +02002973#if defined(MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE)
Gabor Mezeif7044ea2022-06-28 16:01:49 +02002974 case MBEDTLS_SSL_SERVER_CCS_AFTER_HELLO_RETRY_REQUEST:
Gilles Peskine449bd832023-01-11 14:50:10 +01002975 ret = mbedtls_ssl_tls13_write_change_cipher_spec(ssl);
2976 if (ret == 0) {
2977 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_CLIENT_HELLO);
2978 }
Gabor Mezei7b39bf12022-05-24 16:04:14 +02002979 break;
2980
2981 case MBEDTLS_SSL_SERVER_CCS_AFTER_SERVER_HELLO:
Gilles Peskine449bd832023-01-11 14:50:10 +01002982 ret = mbedtls_ssl_tls13_write_change_cipher_spec(ssl);
2983 if (ret == 0) {
2984 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_ENCRYPTED_EXTENSIONS);
2985 }
Gabor Mezei7b39bf12022-05-24 16:04:14 +02002986 break;
2987#endif /* MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE */
2988
Jerry Yu69dd8d42022-04-16 12:51:26 +08002989 case MBEDTLS_SSL_SERVER_FINISHED:
Gilles Peskine449bd832023-01-11 14:50:10 +01002990 ret = ssl_tls13_write_server_finished(ssl);
Jerry Yu69dd8d42022-04-16 12:51:26 +08002991 break;
2992
2993 case MBEDTLS_SSL_CLIENT_FINISHED:
Gilles Peskine449bd832023-01-11 14:50:10 +01002994 ret = ssl_tls13_process_client_finished(ssl);
Jerry Yu69dd8d42022-04-16 12:51:26 +08002995 break;
2996
Jerry Yu69dd8d42022-04-16 12:51:26 +08002997 case MBEDTLS_SSL_HANDSHAKE_WRAPUP:
Gilles Peskine449bd832023-01-11 14:50:10 +01002998 ret = ssl_tls13_handshake_wrapup(ssl);
Jerry Yu69dd8d42022-04-16 12:51:26 +08002999 break;
3000
Ronald Cron766c0cd2022-10-18 12:17:11 +02003001#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED)
XiaokangQian6b916b12022-04-25 07:29:34 +00003002 case MBEDTLS_SSL_CLIENT_CERTIFICATE:
Gilles Peskine449bd832023-01-11 14:50:10 +01003003 ret = mbedtls_ssl_tls13_process_certificate(ssl);
3004 if (ret == 0) {
3005 if (ssl->session_negotiate->peer_cert != NULL) {
Ronald Cron209cae92022-06-07 10:30:19 +02003006 mbedtls_ssl_handshake_set_state(
Gilles Peskine449bd832023-01-11 14:50:10 +01003007 ssl, MBEDTLS_SSL_CLIENT_CERTIFICATE_VERIFY);
3008 } else {
3009 MBEDTLS_SSL_DEBUG_MSG(2, ("skip parse certificate verify"));
Ronald Cron209cae92022-06-07 10:30:19 +02003010 mbedtls_ssl_handshake_set_state(
Gilles Peskine449bd832023-01-11 14:50:10 +01003011 ssl, MBEDTLS_SSL_CLIENT_FINISHED);
Ronald Cron19385882022-06-15 16:26:13 +02003012 }
XiaokangQian6b916b12022-04-25 07:29:34 +00003013 }
3014 break;
3015
3016 case MBEDTLS_SSL_CLIENT_CERTIFICATE_VERIFY:
Gilles Peskine449bd832023-01-11 14:50:10 +01003017 ret = mbedtls_ssl_tls13_process_certificate_verify(ssl);
3018 if (ret == 0) {
XiaokangQian6b916b12022-04-25 07:29:34 +00003019 mbedtls_ssl_handshake_set_state(
Gilles Peskine449bd832023-01-11 14:50:10 +01003020 ssl, MBEDTLS_SSL_CLIENT_FINISHED);
XiaokangQian6b916b12022-04-25 07:29:34 +00003021 }
3022 break;
Ronald Cron766c0cd2022-10-18 12:17:11 +02003023#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED */
XiaokangQian6b916b12022-04-25 07:29:34 +00003024
Jerry Yue67bef42022-07-07 07:29:42 +00003025#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Jerry Yua8d3c502022-10-30 14:51:23 +08003026 case MBEDTLS_SSL_TLS1_3_NEW_SESSION_TICKET:
Gilles Peskine449bd832023-01-11 14:50:10 +01003027 ret = ssl_tls13_write_new_session_ticket(ssl);
3028 if (ret != 0) {
3029 MBEDTLS_SSL_DEBUG_RET(1,
3030 "ssl_tls13_write_new_session_ticket ",
3031 ret);
Jerry Yue67bef42022-07-07 07:29:42 +00003032 }
3033 break;
Jerry Yua8d3c502022-10-30 14:51:23 +08003034 case MBEDTLS_SSL_TLS1_3_NEW_SESSION_TICKET_FLUSH:
Jerry Yue67bef42022-07-07 07:29:42 +00003035 /* This state is necessary to do the flush of the New Session
Jerry Yua8d3c502022-10-30 14:51:23 +08003036 * Ticket message written in MBEDTLS_SSL_TLS1_3_NEW_SESSION_TICKET
Jerry Yue67bef42022-07-07 07:29:42 +00003037 * as part of ssl_prepare_handshake_step.
3038 */
3039 ret = 0;
Jerry Yud4e75002022-08-09 13:33:50 +08003040
Gilles Peskine449bd832023-01-11 14:50:10 +01003041 if (ssl->handshake->new_session_tickets_count == 0) {
3042 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_HANDSHAKE_OVER);
3043 } else {
3044 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_TLS1_3_NEW_SESSION_TICKET);
3045 }
Jerry Yue67bef42022-07-07 07:29:42 +00003046 break;
3047
3048#endif /* MBEDTLS_SSL_SESSION_TICKETS */
3049
XiaokangQian7807f9f2022-02-15 10:04:37 +00003050 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01003051 MBEDTLS_SSL_DEBUG_MSG(1, ("invalid state %d", ssl->state));
3052 return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
XiaokangQian7807f9f2022-02-15 10:04:37 +00003053 }
3054
Gilles Peskine449bd832023-01-11 14:50:10 +01003055 return ret;
Jerry Yub9930e72021-08-06 17:11:51 +08003056}
Jerry Yu3cc4c2a2021-08-06 16:29:08 +08003057
Jerry Yufb4b6472022-01-27 15:03:26 +08003058#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_PROTO_TLS1_3 */