blob: 5d86660fd8e7ebfc2a1b4cad6e2a4a758c688261 [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"
Manuel Pégourié-Gonnardd55d66f2023-06-20 10:14:58 +020028#include "mbedtls/oid.h"
Manuel Pégourié-Gonnard02b10d82023-03-28 12:33:20 +020029#include "md_psa.h"
Jerry Yu3bf2c642022-03-30 22:02:12 +080030
Xiaofei Bai9ca09d42022-02-14 12:57:18 +000031#include "ssl_misc.h"
32#include "ssl_tls13_keys.h"
33#include "ssl_debug_helpers.h"
34
Jerry Yuf35ba382022-08-23 17:58:26 +080035
Jerry Yuc5a23a02022-08-25 10:51:44 +080036static const mbedtls_ssl_ciphersuite_t *ssl_tls13_validate_peer_ciphersuite(
Gilles Peskine449bd832023-01-11 14:50:10 +010037 mbedtls_ssl_context *ssl,
38 unsigned int cipher_suite)
Jerry Yuf35ba382022-08-23 17:58:26 +080039{
40 const mbedtls_ssl_ciphersuite_t *ciphersuite_info;
Gilles Peskine449bd832023-01-11 14:50:10 +010041 if (!mbedtls_ssl_tls13_cipher_suite_is_offered(ssl, cipher_suite)) {
42 return NULL;
Jerry Yuf35ba382022-08-23 17:58:26 +080043 }
Gilles Peskine449bd832023-01-11 14:50:10 +010044
45 ciphersuite_info = mbedtls_ssl_ciphersuite_from_id(cipher_suite);
46 if ((mbedtls_ssl_validate_ciphersuite(ssl, ciphersuite_info,
47 ssl->tls_version,
48 ssl->tls_version) != 0)) {
49 return NULL;
50 }
51 return ciphersuite_info;
Jerry Yuf35ba382022-08-23 17:58:26 +080052}
53
Ronald Cron41a443a2022-10-04 16:38:25 +020054#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED)
Jerry Yue19e3b92022-07-08 12:04:51 +000055/* From RFC 8446:
56 *
57 * enum { psk_ke(0), psk_dhe_ke(1), (255) } PskKeyExchangeMode;
58 * struct {
59 * PskKeyExchangeMode ke_modes<1..255>;
60 * } PskKeyExchangeModes;
61 */
Jerry Yu6e74a7e2022-07-20 20:49:32 +080062MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +010063static int ssl_tls13_parse_key_exchange_modes_ext(mbedtls_ssl_context *ssl,
64 const unsigned char *buf,
65 const unsigned char *end)
Jerry Yue19e3b92022-07-08 12:04:51 +000066{
Jerry Yu299e31f2022-07-13 23:06:36 +080067 const unsigned char *p = buf;
Jerry Yue19e3b92022-07-08 12:04:51 +000068 size_t ke_modes_len;
69 int ke_modes = 0;
70
Jerry Yu854dd9e2022-07-15 14:28:27 +080071 /* Read ke_modes length (1 Byte) */
Gilles Peskine449bd832023-01-11 14:50:10 +010072 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, 1);
Jerry Yu299e31f2022-07-13 23:06:36 +080073 ke_modes_len = *p++;
Jerry Yue19e3b92022-07-08 12:04:51 +000074 /* Currently, there are only two PSK modes, so even without looking
75 * at the content, something's wrong if the list has more than 2 items. */
Gilles Peskine449bd832023-01-11 14:50:10 +010076 if (ke_modes_len > 2) {
77 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER,
78 MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER);
79 return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
Jerry Yu299e31f2022-07-13 23:06:36 +080080 }
Jerry Yue19e3b92022-07-08 12:04:51 +000081
Gilles Peskine449bd832023-01-11 14:50:10 +010082 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, ke_modes_len);
Jerry Yue19e3b92022-07-08 12:04:51 +000083
Gilles Peskine449bd832023-01-11 14:50:10 +010084 while (ke_modes_len-- != 0) {
85 switch (*p++) {
86 case MBEDTLS_SSL_TLS1_3_PSK_MODE_PURE:
87 ke_modes |= MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK;
88 MBEDTLS_SSL_DEBUG_MSG(3, ("Found PSK KEX MODE"));
89 break;
90 case MBEDTLS_SSL_TLS1_3_PSK_MODE_ECDHE:
91 ke_modes |= MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK_EPHEMERAL;
92 MBEDTLS_SSL_DEBUG_MSG(3, ("Found PSK_EPHEMERAL KEX MODE"));
93 break;
94 default:
95 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER,
96 MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER);
97 return MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER;
Jerry Yue19e3b92022-07-08 12:04:51 +000098 }
99 }
100
101 ssl->handshake->tls13_kex_modes = ke_modes;
Gilles Peskine449bd832023-01-11 14:50:10 +0100102 return 0;
Jerry Yue19e3b92022-07-08 12:04:51 +0000103}
Jerry Yu1c105562022-07-10 06:32:38 +0000104
Jerry Yue9d4fc02022-08-20 19:21:15 +0800105#define SSL_TLS1_3_OFFERED_PSK_NOT_MATCH 1
106#define SSL_TLS1_3_OFFERED_PSK_MATCH 0
Jerry Yu95699e72022-08-21 19:22:23 +0800107
108#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Pengyu Lv29daf4a2023-10-30 17:13:30 +0800109MBEDTLS_CHECK_RETURN_CRITICAL
110static int ssl_tls13_check_psk_key_exchange(mbedtls_ssl_context *ssl);
111MBEDTLS_CHECK_RETURN_CRITICAL
112static int ssl_tls13_check_psk_ephemeral_key_exchange(mbedtls_ssl_context *ssl);
Jerry Yu95699e72022-08-21 19:22:23 +0800113
114MBEDTLS_CHECK_RETURN_CRITICAL
115static int ssl_tls13_offered_psks_check_identity_match_ticket(
Gilles Peskine449bd832023-01-11 14:50:10 +0100116 mbedtls_ssl_context *ssl,
117 const unsigned char *identity,
118 size_t identity_len,
119 uint32_t obfuscated_ticket_age,
120 mbedtls_ssl_session *session)
Jerry Yu95699e72022-08-21 19:22:23 +0800121{
Jerry Yufd310eb2022-09-06 09:16:35 +0800122 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yu95699e72022-08-21 19:22:23 +0800123 unsigned char *ticket_buffer;
Pengyu Lv29daf4a2023-10-30 17:13:30 +0800124 unsigned int key_exchanges;
Jerry Yu8d4bbba2022-09-13 14:15:48 +0800125#if defined(MBEDTLS_HAVE_TIME)
126 mbedtls_time_t now;
Jerry Yuacff8232022-09-14 14:35:11 +0800127 uint64_t age_in_s;
Jerry Yuf7dad3c2022-09-14 22:31:39 +0800128 int64_t age_diff_in_ms;
Jerry Yu8d4bbba2022-09-13 14:15:48 +0800129#endif
Jerry Yu95699e72022-08-21 19:22:23 +0800130
131 ((void) obfuscated_ticket_age);
132
Gilles Peskine449bd832023-01-11 14:50:10 +0100133 MBEDTLS_SSL_DEBUG_MSG(2, ("=> check_identity_match_ticket"));
Jerry Yu95699e72022-08-21 19:22:23 +0800134
Jerry Yufd310eb2022-09-06 09:16:35 +0800135 /* Ticket parser is not configured, Skip */
Gilles Peskine449bd832023-01-11 14:50:10 +0100136 if (ssl->conf->f_ticket_parse == NULL || identity_len == 0) {
137 return 0;
138 }
Jerry Yu95699e72022-08-21 19:22:23 +0800139
Jerry Yu4746b102022-09-13 11:11:48 +0800140 /* We create a copy of the encrypted ticket since the ticket parsing
141 * function is allowed to use its input buffer as an output buffer
142 * (in-place decryption). We do, however, need the original buffer for
143 * computing the PSK binder value.
Jerry Yu95699e72022-08-21 19:22:23 +0800144 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100145 ticket_buffer = mbedtls_calloc(1, identity_len);
146 if (ticket_buffer == NULL) {
147 MBEDTLS_SSL_DEBUG_MSG(1, ("buffer too small"));
148 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
Jerry Yu95699e72022-08-21 19:22:23 +0800149 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100150 memcpy(ticket_buffer, identity, identity_len);
Jerry Yu95699e72022-08-21 19:22:23 +0800151
Gilles Peskine449bd832023-01-11 14:50:10 +0100152 if ((ret = ssl->conf->f_ticket_parse(ssl->conf->p_ticket,
153 session,
154 ticket_buffer, identity_len)) != 0) {
155 if (ret == MBEDTLS_ERR_SSL_INVALID_MAC) {
156 MBEDTLS_SSL_DEBUG_MSG(3, ("ticket is not authentic"));
157 } else if (ret == MBEDTLS_ERR_SSL_SESSION_TICKET_EXPIRED) {
158 MBEDTLS_SSL_DEBUG_MSG(3, ("ticket is expired"));
159 } else {
160 MBEDTLS_SSL_DEBUG_RET(1, "ticket_parse", ret);
161 }
Jerry Yu95699e72022-08-21 19:22:23 +0800162 }
163
164 /* We delete the temporary buffer */
Gilles Peskine449bd832023-01-11 14:50:10 +0100165 mbedtls_free(ticket_buffer);
Jerry Yu95699e72022-08-21 19:22:23 +0800166
Gilles Peskine449bd832023-01-11 14:50:10 +0100167 if (ret != 0) {
Jerry Yu8d4bbba2022-09-13 14:15:48 +0800168 goto exit;
169 }
Jerry Yu95699e72022-08-21 19:22:23 +0800170
Pengyu Lv3eb49be2022-12-05 16:35:12 +0800171 /* RFC 8446 section 4.2.9
172 *
173 * Servers SHOULD NOT send NewSessionTicket with tickets that are not
174 * compatible with the advertised modes; however, if a server does so,
175 * the impact will just be that the client's attempts at resumption fail.
176 *
177 * We regard the ticket with incompatible key exchange modes as not match.
178 */
Pengyu Lv76679682023-02-02 15:04:27 +0800179 ret = MBEDTLS_ERR_ERROR_GENERIC_ERROR;
Pengyu Lv29daf4a2023-10-30 17:13:30 +0800180 MBEDTLS_SSL_PRINT_TICKET_FLAGS(4, session->ticket_flags);
Pengyu Lv29daf4a2023-10-30 17:13:30 +0800181
182 key_exchanges = 0;
Pengyu Lvdbd1e0d2023-10-31 10:08:10 +0800183 if (mbedtls_ssl_session_ticket_allow_psk_ephemeral(session) &&
Pengyu Lv29daf4a2023-10-30 17:13:30 +0800184 ssl_tls13_check_psk_ephemeral_key_exchange(ssl)) {
185 key_exchanges |= MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK_EPHEMERAL;
186 }
Pengyu Lvdbd1e0d2023-10-31 10:08:10 +0800187 if (mbedtls_ssl_session_ticket_allow_psk(session) &&
Pengyu Lv29daf4a2023-10-30 17:13:30 +0800188 ssl_tls13_check_psk_key_exchange(ssl)) {
189 key_exchanges |= MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK;
190 }
191
192 if (key_exchanges == 0) {
Pengyu Lv3eb49be2022-12-05 16:35:12 +0800193 MBEDTLS_SSL_DEBUG_MSG(3, ("No suitable key exchange mode"));
194 goto exit;
195 }
196
Gilles Peskine449bd832023-01-11 14:50:10 +0100197 ret = MBEDTLS_ERR_SSL_SESSION_TICKET_EXPIRED;
198#if defined(MBEDTLS_HAVE_TIME)
199 now = mbedtls_time(NULL);
200
201 if (now < session->start) {
202 MBEDTLS_SSL_DEBUG_MSG(
203 3, ("Invalid ticket start time ( now=%" MBEDTLS_PRINTF_LONGLONG
204 ", start=%" MBEDTLS_PRINTF_LONGLONG " )",
205 (long long) now, (long long) session->start));
206 goto exit;
207 }
208
209 age_in_s = (uint64_t) (now - session->start);
Jerry Yu95699e72022-08-21 19:22:23 +0800210
Jerry Yu8d4bbba2022-09-13 14:15:48 +0800211 /* RFC 8446 section 4.6.1
212 *
213 * Servers MUST NOT use any value greater than 604800 seconds (7 days).
214 *
215 * RFC 8446 section 4.2.11.1
216 *
217 * Clients MUST NOT attempt to use tickets which have ages greater than
218 * the "ticket_lifetime" value which was provided with the ticket.
219 *
220 * For time being, the age MUST be less than 604800 seconds (7 days).
221 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100222 if (age_in_s > 604800) {
Jerry Yu8d4bbba2022-09-13 14:15:48 +0800223 MBEDTLS_SSL_DEBUG_MSG(
Gilles Peskine449bd832023-01-11 14:50:10 +0100224 3, ("Ticket age exceeds limitation ticket_age=%lu",
225 (long unsigned int) age_in_s));
Jerry Yu8d4bbba2022-09-13 14:15:48 +0800226 goto exit;
227 }
Jerry Yu95699e72022-08-21 19:22:23 +0800228
Jerry Yu8d4bbba2022-09-13 14:15:48 +0800229 /* RFC 8446 section 4.2.10
230 *
231 * For PSKs provisioned via NewSessionTicket, a server MUST validate that
232 * the ticket age for the selected PSK identity (computed by subtracting
233 * ticket_age_add from PskIdentity.obfuscated_ticket_age modulo 2^32) is
234 * within a small tolerance of the time since the ticket was issued.
Jerry Yuf7dad3c2022-09-14 22:31:39 +0800235 *
Jerry Yu0a55cc62022-09-15 16:15:06 +0800236 * NOTE: When `now == session->start`, `age_diff_in_ms` may be negative
237 * as the age units are different on the server (s) and in the
238 * client (ms) side. Add a -1000 ms tolerance window to take this
239 * into account.
Jerry Yu8d4bbba2022-09-13 14:15:48 +0800240 */
Jerry Yuf7dad3c2022-09-14 22:31:39 +0800241 age_diff_in_ms = age_in_s * 1000;
Gilles Peskine449bd832023-01-11 14:50:10 +0100242 age_diff_in_ms -= (obfuscated_ticket_age - session->ticket_age_add);
243 if (age_diff_in_ms <= -1000 ||
244 age_diff_in_ms > MBEDTLS_SSL_TLS1_3_TICKET_AGE_TOLERANCE) {
Jerry Yu8d4bbba2022-09-13 14:15:48 +0800245 MBEDTLS_SSL_DEBUG_MSG(
Gilles Peskine449bd832023-01-11 14:50:10 +0100246 3, ("Ticket age outside tolerance window ( diff=%d )",
247 (int) age_diff_in_ms));
Jerry Yu8d4bbba2022-09-13 14:15:48 +0800248 goto exit;
249 }
250
251 ret = 0;
Jerry Yu95699e72022-08-21 19:22:23 +0800252
253#endif /* MBEDTLS_HAVE_TIME */
Jerry Yu8d4bbba2022-09-13 14:15:48 +0800254
255exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100256 if (ret != 0) {
257 mbedtls_ssl_session_free(session);
258 }
Jerry Yu95699e72022-08-21 19:22:23 +0800259
Gilles Peskine449bd832023-01-11 14:50:10 +0100260 MBEDTLS_SSL_DEBUG_MSG(2, ("<= check_identity_match_ticket"));
261 return ret;
Jerry Yu95699e72022-08-21 19:22:23 +0800262}
263#endif /* MBEDTLS_SSL_SESSION_TICKETS */
264
Jerry Yu6e74a7e2022-07-20 20:49:32 +0800265MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yu1c105562022-07-10 06:32:38 +0000266static int ssl_tls13_offered_psks_check_identity_match(
Gilles Peskine449bd832023-01-11 14:50:10 +0100267 mbedtls_ssl_context *ssl,
268 const unsigned char *identity,
269 size_t identity_len,
270 uint32_t obfuscated_ticket_age,
271 int *psk_type,
272 mbedtls_ssl_session *session)
Jerry Yu1c105562022-07-10 06:32:38 +0000273{
Ronald Cron606671e2023-02-17 11:36:33 +0100274 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
275
Jerry Yu95699e72022-08-21 19:22:23 +0800276 ((void) session);
277 ((void) obfuscated_ticket_age);
Jerry Yu5725f1c2022-08-21 17:27:16 +0800278 *psk_type = MBEDTLS_SSL_TLS1_3_PSK_EXTERNAL;
Jerry Yu95699e72022-08-21 19:22:23 +0800279
Gilles Peskine449bd832023-01-11 14:50:10 +0100280 MBEDTLS_SSL_DEBUG_BUF(4, "identity", identity, identity_len);
Jerry Yu95699e72022-08-21 19:22:23 +0800281 ssl->handshake->resume = 0;
282
Jerry Yu95699e72022-08-21 19:22:23 +0800283#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Gilles Peskine449bd832023-01-11 14:50:10 +0100284 if (ssl_tls13_offered_psks_check_identity_match_ticket(
Jerry Yu8d4bbba2022-09-13 14:15:48 +0800285 ssl, identity, identity_len, obfuscated_ticket_age,
Gilles Peskine449bd832023-01-11 14:50:10 +0100286 session) == SSL_TLS1_3_OFFERED_PSK_MATCH) {
Jerry Yu95699e72022-08-21 19:22:23 +0800287 ssl->handshake->resume = 1;
288 *psk_type = MBEDTLS_SSL_TLS1_3_PSK_RESUMPTION;
Ronald Cron606671e2023-02-17 11:36:33 +0100289 ret = mbedtls_ssl_set_hs_psk(ssl,
290 session->resumption_key,
291 session->resumption_key_len);
292 if (ret != 0) {
293 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_set_hs_psk", ret);
294 return ret;
295 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100296
297 MBEDTLS_SSL_DEBUG_BUF(4, "Ticket-resumed PSK:",
298 session->resumption_key,
299 session->resumption_key_len);
300 MBEDTLS_SSL_DEBUG_MSG(4, ("ticket: obfuscated_ticket_age: %u",
301 (unsigned) obfuscated_ticket_age));
302 return SSL_TLS1_3_OFFERED_PSK_MATCH;
Jerry Yu95699e72022-08-21 19:22:23 +0800303 }
304#endif /* MBEDTLS_SSL_SESSION_TICKETS */
305
Jerry Yu1c105562022-07-10 06:32:38 +0000306 /* Check identity with external configured function */
Gilles Peskine449bd832023-01-11 14:50:10 +0100307 if (ssl->conf->f_psk != NULL) {
308 if (ssl->conf->f_psk(
309 ssl->conf->p_psk, ssl, identity, identity_len) == 0) {
310 return SSL_TLS1_3_OFFERED_PSK_MATCH;
Jerry Yu1c105562022-07-10 06:32:38 +0000311 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100312 return SSL_TLS1_3_OFFERED_PSK_NOT_MATCH;
Jerry Yu1c105562022-07-10 06:32:38 +0000313 }
314
Gilles Peskine449bd832023-01-11 14:50:10 +0100315 MBEDTLS_SSL_DEBUG_BUF(5, "identity", identity, identity_len);
Jerry Yu1c105562022-07-10 06:32:38 +0000316 /* Check identity with pre-configured psk */
Gilles Peskine449bd832023-01-11 14:50:10 +0100317 if (ssl->conf->psk_identity != NULL &&
Jerry Yu2f0abc92022-07-22 19:34:48 +0800318 identity_len == ssl->conf->psk_identity_len &&
Gilles Peskine449bd832023-01-11 14:50:10 +0100319 mbedtls_ct_memcmp(ssl->conf->psk_identity,
320 identity, identity_len) == 0) {
Ronald Cron606671e2023-02-17 11:36:33 +0100321 ret = mbedtls_ssl_set_hs_psk(ssl, ssl->conf->psk, ssl->conf->psk_len);
322 if (ret != 0) {
323 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_set_hs_psk", ret);
324 return ret;
325 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100326 return SSL_TLS1_3_OFFERED_PSK_MATCH;
Jerry Yu1c105562022-07-10 06:32:38 +0000327 }
328
Gilles Peskine449bd832023-01-11 14:50:10 +0100329 return SSL_TLS1_3_OFFERED_PSK_NOT_MATCH;
Jerry Yu1c105562022-07-10 06:32:38 +0000330}
331
Jerry Yu6e74a7e2022-07-20 20:49:32 +0800332MBEDTLS_CHECK_RETURN_CRITICAL
Xiaokang Qian9f1747b2023-03-30 02:50:04 +0000333static int ssl_tls13_offered_psks_check_binder_match(
334 mbedtls_ssl_context *ssl,
335 const unsigned char *binder, size_t binder_len,
336 int psk_type, psa_algorithm_t psk_hash_alg)
Jerry Yu1c105562022-07-10 06:32:38 +0000337{
338 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yu96a2e362022-07-21 15:11:34 +0800339
Jerry Yudaf375a2022-07-20 21:31:43 +0800340 unsigned char transcript[PSA_HASH_MAX_SIZE];
Jerry Yu1c105562022-07-10 06:32:38 +0000341 size_t transcript_len;
Jerry Yu2f0abc92022-07-22 19:34:48 +0800342 unsigned char *psk;
Jerry Yu96a2e362022-07-21 15:11:34 +0800343 size_t psk_len;
Jerry Yudaf375a2022-07-20 21:31:43 +0800344 unsigned char server_computed_binder[PSA_HASH_MAX_SIZE];
Jerry Yu1c105562022-07-10 06:32:38 +0000345
Jerry Yu1c105562022-07-10 06:32:38 +0000346 /* Get current state of handshake transcript. */
Jerry Yu29d9faa2022-08-23 17:52:45 +0800347 ret = mbedtls_ssl_get_handshake_transcript(
Manuel Pégourié-Gonnard2d6d9932023-03-28 11:38:08 +0200348 ssl, mbedtls_md_type_from_psa_alg(psk_hash_alg),
Gilles Peskine449bd832023-01-11 14:50:10 +0100349 transcript, sizeof(transcript), &transcript_len);
350 if (ret != 0) {
351 return ret;
352 }
Jerry Yu1c105562022-07-10 06:32:38 +0000353
Gilles Peskine449bd832023-01-11 14:50:10 +0100354 ret = mbedtls_ssl_tls13_export_handshake_psk(ssl, &psk, &psk_len);
355 if (ret != 0) {
356 return ret;
357 }
Jerry Yu96a2e362022-07-21 15:11:34 +0800358
Gilles Peskine449bd832023-01-11 14:50:10 +0100359 ret = mbedtls_ssl_tls13_create_psk_binder(ssl, psk_hash_alg,
360 psk, psk_len, psk_type,
361 transcript,
362 server_computed_binder);
Jerry Yu96a2e362022-07-21 15:11:34 +0800363#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +0100364 mbedtls_free((void *) psk);
Jerry Yu96a2e362022-07-21 15:11:34 +0800365#endif
Gilles Peskine449bd832023-01-11 14:50:10 +0100366 if (ret != 0) {
367 MBEDTLS_SSL_DEBUG_MSG(1, ("PSK binder calculation failed."));
368 return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
Jerry Yu1c105562022-07-10 06:32:38 +0000369 }
370
Gilles Peskine449bd832023-01-11 14:50:10 +0100371 MBEDTLS_SSL_DEBUG_BUF(3, "psk binder ( computed ): ",
372 server_computed_binder, transcript_len);
373 MBEDTLS_SSL_DEBUG_BUF(3, "psk binder ( received ): ", binder, binder_len);
Jerry Yu1c105562022-07-10 06:32:38 +0000374
Gilles Peskine449bd832023-01-11 14:50:10 +0100375 if (mbedtls_ct_memcmp(server_computed_binder, binder, binder_len) == 0) {
376 return SSL_TLS1_3_OFFERED_PSK_MATCH;
Jerry Yu1c105562022-07-10 06:32:38 +0000377 }
378
Gilles Peskine449bd832023-01-11 14:50:10 +0100379 mbedtls_platform_zeroize(server_computed_binder,
380 sizeof(server_computed_binder));
381 return SSL_TLS1_3_OFFERED_PSK_NOT_MATCH;
Jerry Yu1c105562022-07-10 06:32:38 +0000382}
Jerry Yu96a2e362022-07-21 15:11:34 +0800383
Jerry Yu5725f1c2022-08-21 17:27:16 +0800384MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yuf35ba382022-08-23 17:58:26 +0800385static int ssl_tls13_select_ciphersuite_for_psk(
Gilles Peskine449bd832023-01-11 14:50:10 +0100386 mbedtls_ssl_context *ssl,
387 const unsigned char *cipher_suites,
388 const unsigned char *cipher_suites_end,
389 uint16_t *selected_ciphersuite,
390 const mbedtls_ssl_ciphersuite_t **selected_ciphersuite_info)
Jerry Yu5725f1c2022-08-21 17:27:16 +0800391{
Jerry Yuf35ba382022-08-23 17:58:26 +0800392 psa_algorithm_t psk_hash_alg = PSA_ALG_SHA_256;
Jerry Yu5725f1c2022-08-21 17:27:16 +0800393
Jerry Yu0baf9072022-08-25 11:21:04 +0800394 *selected_ciphersuite = 0;
395 *selected_ciphersuite_info = NULL;
396
Jerry Yuf35ba382022-08-23 17:58:26 +0800397 /* RFC 8446, page 55.
398 *
399 * For externally established PSKs, the Hash algorithm MUST be set when the
400 * PSK is established or default to SHA-256 if no such algorithm is defined.
401 *
402 */
Jerry Yu5725f1c2022-08-21 17:27:16 +0800403
Jerry Yu5725f1c2022-08-21 17:27:16 +0800404 /*
405 * Search for a matching ciphersuite
406 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100407 for (const unsigned char *p = cipher_suites;
408 p < cipher_suites_end; p += 2) {
Jerry Yu5725f1c2022-08-21 17:27:16 +0800409 uint16_t cipher_suite;
Jerry Yuf35ba382022-08-23 17:58:26 +0800410 const mbedtls_ssl_ciphersuite_t *ciphersuite_info;
Jerry Yu5725f1c2022-08-21 17:27:16 +0800411
Gilles Peskine449bd832023-01-11 14:50:10 +0100412 cipher_suite = MBEDTLS_GET_UINT16_BE(p, 0);
413 ciphersuite_info = ssl_tls13_validate_peer_ciphersuite(ssl,
414 cipher_suite);
415 if (ciphersuite_info == NULL) {
Jerry Yu5725f1c2022-08-21 17:27:16 +0800416 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +0100417 }
Jerry Yu5725f1c2022-08-21 17:27:16 +0800418
Jerry Yu5725f1c2022-08-21 17:27:16 +0800419 /* MAC of selected ciphersuite MUST be same with PSK binder if exist.
420 * Otherwise, client should reject.
421 */
Dave Rodgman2eab4622023-10-05 13:30:37 +0100422 if (psk_hash_alg ==
423 mbedtls_md_psa_alg_from_type((mbedtls_md_type_t) ciphersuite_info->mac)) {
Jerry Yuf35ba382022-08-23 17:58:26 +0800424 *selected_ciphersuite = cipher_suite;
425 *selected_ciphersuite_info = ciphersuite_info;
Gilles Peskine449bd832023-01-11 14:50:10 +0100426 return 0;
Jerry Yuf35ba382022-08-23 17:58:26 +0800427 }
428 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100429 MBEDTLS_SSL_DEBUG_MSG(2, ("No matched ciphersuite"));
430 return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
Jerry Yuf35ba382022-08-23 17:58:26 +0800431}
Jerry Yu5725f1c2022-08-21 17:27:16 +0800432
Jerry Yu82534862022-08-30 10:42:33 +0800433#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Jerry Yuf35ba382022-08-23 17:58:26 +0800434MBEDTLS_CHECK_RETURN_CRITICAL
435static int ssl_tls13_select_ciphersuite_for_resumption(
Gilles Peskine449bd832023-01-11 14:50:10 +0100436 mbedtls_ssl_context *ssl,
437 const unsigned char *cipher_suites,
438 const unsigned char *cipher_suites_end,
439 mbedtls_ssl_session *session,
440 uint16_t *selected_ciphersuite,
441 const mbedtls_ssl_ciphersuite_t **selected_ciphersuite_info)
Jerry Yuf35ba382022-08-23 17:58:26 +0800442{
Jerry Yu82534862022-08-30 10:42:33 +0800443
Jerry Yuf35ba382022-08-23 17:58:26 +0800444 *selected_ciphersuite = 0;
445 *selected_ciphersuite_info = NULL;
Gilles Peskine449bd832023-01-11 14:50:10 +0100446 for (const unsigned char *p = cipher_suites; p < cipher_suites_end; p += 2) {
447 uint16_t cipher_suite = MBEDTLS_GET_UINT16_BE(p, 0);
Jerry Yu82534862022-08-30 10:42:33 +0800448 const mbedtls_ssl_ciphersuite_t *ciphersuite_info;
449
Gilles Peskine449bd832023-01-11 14:50:10 +0100450 if (cipher_suite != session->ciphersuite) {
Jerry Yu82534862022-08-30 10:42:33 +0800451 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +0100452 }
Jerry Yu82534862022-08-30 10:42:33 +0800453
Gilles Peskine449bd832023-01-11 14:50:10 +0100454 ciphersuite_info = ssl_tls13_validate_peer_ciphersuite(ssl,
455 cipher_suite);
456 if (ciphersuite_info == NULL) {
Jerry Yu82534862022-08-30 10:42:33 +0800457 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +0100458 }
Jerry Yu82534862022-08-30 10:42:33 +0800459
Jerry Yu4746b102022-09-13 11:11:48 +0800460 *selected_ciphersuite = cipher_suite;
Jerry Yu82534862022-08-30 10:42:33 +0800461 *selected_ciphersuite_info = ciphersuite_info;
462
Gilles Peskine449bd832023-01-11 14:50:10 +0100463 return 0;
Jerry Yu82534862022-08-30 10:42:33 +0800464 }
465
Gilles Peskine449bd832023-01-11 14:50:10 +0100466 return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
Jerry Yu5725f1c2022-08-21 17:27:16 +0800467}
Jerry Yuf35ba382022-08-23 17:58:26 +0800468
Jerry Yu82534862022-08-30 10:42:33 +0800469MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100470static int ssl_tls13_session_copy_ticket(mbedtls_ssl_session *dst,
471 const mbedtls_ssl_session *src)
Jerry Yu82534862022-08-30 10:42:33 +0800472{
Jerry Yu82534862022-08-30 10:42:33 +0800473 dst->ticket_age_add = src->ticket_age_add;
474 dst->ticket_flags = src->ticket_flags;
475 dst->resumption_key_len = src->resumption_key_len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100476 if (src->resumption_key_len == 0) {
477 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
478 }
479 memcpy(dst->resumption_key, src->resumption_key, src->resumption_key_len);
Jerry Yu4746b102022-09-13 11:11:48 +0800480
Gilles Peskine449bd832023-01-11 14:50:10 +0100481 return 0;
Jerry Yu82534862022-08-30 10:42:33 +0800482}
483#endif /* MBEDTLS_SSL_SESSION_TICKETS */
484
Jerry Yu1c105562022-07-10 06:32:38 +0000485/* Parser for pre_shared_key extension in client hello
486 * struct {
487 * opaque identity<1..2^16-1>;
488 * uint32 obfuscated_ticket_age;
489 * } PskIdentity;
490 *
491 * opaque PskBinderEntry<32..255>;
492 *
493 * struct {
494 * PskIdentity identities<7..2^16-1>;
495 * PskBinderEntry binders<33..2^16-1>;
496 * } OfferedPsks;
497 *
498 * struct {
499 * select (Handshake.msg_type) {
500 * case client_hello: OfferedPsks;
501 * ....
502 * };
503 * } PreSharedKeyExtension;
504 */
Jerry Yu6e74a7e2022-07-20 20:49:32 +0800505MBEDTLS_CHECK_RETURN_CRITICAL
Xiaokang Qian9f1747b2023-03-30 02:50:04 +0000506static int ssl_tls13_parse_pre_shared_key_ext(
507 mbedtls_ssl_context *ssl,
508 const unsigned char *pre_shared_key_ext,
509 const unsigned char *pre_shared_key_ext_end,
510 const unsigned char *ciphersuites,
511 const unsigned char *ciphersuites_end)
Jerry Yu1c105562022-07-10 06:32:38 +0000512{
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +0100513 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yu29d9faa2022-08-23 17:52:45 +0800514 const unsigned char *identities = pre_shared_key_ext;
Jerry Yu568ec252022-07-22 21:27:34 +0800515 const unsigned char *p_identity_len;
516 size_t identities_len;
Jerry Yu1c105562022-07-10 06:32:38 +0000517 const unsigned char *identities_end;
Jerry Yu568ec252022-07-22 21:27:34 +0800518 const unsigned char *binders;
519 const unsigned char *p_binder_len;
520 size_t binders_len;
Jerry Yu1c105562022-07-10 06:32:38 +0000521 const unsigned char *binders_end;
Jerry Yu96a2e362022-07-21 15:11:34 +0800522 int matched_identity = -1;
523 int identity_id = -1;
Jerry Yu1c105562022-07-10 06:32:38 +0000524
Gilles Peskine449bd832023-01-11 14:50:10 +0100525 MBEDTLS_SSL_DEBUG_BUF(3, "pre_shared_key extension",
526 pre_shared_key_ext,
527 pre_shared_key_ext_end - pre_shared_key_ext);
Jerry Yu1c105562022-07-10 06:32:38 +0000528
Jerry Yu96a2e362022-07-21 15:11:34 +0800529 /* identities_len 2 bytes
530 * identities_data >= 7 bytes
531 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100532 MBEDTLS_SSL_CHK_BUF_READ_PTR(identities, pre_shared_key_ext_end, 7 + 2);
533 identities_len = MBEDTLS_GET_UINT16_BE(identities, 0);
Jerry Yu568ec252022-07-22 21:27:34 +0800534 p_identity_len = identities + 2;
Gilles Peskine449bd832023-01-11 14:50:10 +0100535 MBEDTLS_SSL_CHK_BUF_READ_PTR(p_identity_len, pre_shared_key_ext_end,
536 identities_len);
Jerry Yu568ec252022-07-22 21:27:34 +0800537 identities_end = p_identity_len + identities_len;
Jerry Yu1c105562022-07-10 06:32:38 +0000538
Jerry Yu96a2e362022-07-21 15:11:34 +0800539 /* binders_len 2 bytes
540 * binders >= 33 bytes
541 */
Jerry Yu568ec252022-07-22 21:27:34 +0800542 binders = identities_end;
Gilles Peskine449bd832023-01-11 14:50:10 +0100543 MBEDTLS_SSL_CHK_BUF_READ_PTR(binders, pre_shared_key_ext_end, 33 + 2);
544 binders_len = MBEDTLS_GET_UINT16_BE(binders, 0);
Jerry Yu568ec252022-07-22 21:27:34 +0800545 p_binder_len = binders + 2;
Gilles Peskine449bd832023-01-11 14:50:10 +0100546 MBEDTLS_SSL_CHK_BUF_READ_PTR(p_binder_len, pre_shared_key_ext_end, binders_len);
Jerry Yu568ec252022-07-22 21:27:34 +0800547 binders_end = p_binder_len + binders_len;
Jerry Yu1c105562022-07-10 06:32:38 +0000548
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +0100549 ret = ssl->handshake->update_checksum(ssl, pre_shared_key_ext,
550 identities_end - pre_shared_key_ext);
551 if (0 != ret) {
552 MBEDTLS_SSL_DEBUG_RET(1, ("update_checksum"), ret);
553 return ret;
554 }
Jerry Yu96a2e362022-07-21 15:11:34 +0800555
Gilles Peskine449bd832023-01-11 14:50:10 +0100556 while (p_identity_len < identities_end && p_binder_len < binders_end) {
Jerry Yu1c105562022-07-10 06:32:38 +0000557 const unsigned char *identity;
Jerry Yu568ec252022-07-22 21:27:34 +0800558 size_t identity_len;
Jerry Yu82534862022-08-30 10:42:33 +0800559 uint32_t obfuscated_ticket_age;
Jerry Yu96a2e362022-07-21 15:11:34 +0800560 const unsigned char *binder;
Jerry Yu568ec252022-07-22 21:27:34 +0800561 size_t binder_len;
Jerry Yu5725f1c2022-08-21 17:27:16 +0800562 int psk_type;
563 uint16_t cipher_suite;
Jerry Yu29d9faa2022-08-23 17:52:45 +0800564 const mbedtls_ssl_ciphersuite_t *ciphersuite_info;
Jerry Yu82534862022-08-30 10:42:33 +0800565#if defined(MBEDTLS_SSL_SESSION_TICKETS)
566 mbedtls_ssl_session session;
Gilles Peskine449bd832023-01-11 14:50:10 +0100567 mbedtls_ssl_session_init(&session);
Jerry Yu82534862022-08-30 10:42:33 +0800568#endif
Jerry Yubb852022022-07-20 21:10:44 +0800569
Gilles Peskine449bd832023-01-11 14:50:10 +0100570 MBEDTLS_SSL_CHK_BUF_READ_PTR(p_identity_len, identities_end, 2 + 1 + 4);
571 identity_len = MBEDTLS_GET_UINT16_BE(p_identity_len, 0);
Jerry Yu568ec252022-07-22 21:27:34 +0800572 identity = p_identity_len + 2;
Gilles Peskine449bd832023-01-11 14:50:10 +0100573 MBEDTLS_SSL_CHK_BUF_READ_PTR(identity, identities_end, identity_len + 4);
574 obfuscated_ticket_age = MBEDTLS_GET_UINT32_BE(identity, identity_len);
Jerry Yu568ec252022-07-22 21:27:34 +0800575 p_identity_len += identity_len + 6;
Jerry Yu1c105562022-07-10 06:32:38 +0000576
Gilles Peskine449bd832023-01-11 14:50:10 +0100577 MBEDTLS_SSL_CHK_BUF_READ_PTR(p_binder_len, binders_end, 1 + 32);
Jerry Yu568ec252022-07-22 21:27:34 +0800578 binder_len = *p_binder_len;
579 binder = p_binder_len + 1;
Gilles Peskine449bd832023-01-11 14:50:10 +0100580 MBEDTLS_SSL_CHK_BUF_READ_PTR(binder, binders_end, binder_len);
Jerry Yu568ec252022-07-22 21:27:34 +0800581 p_binder_len += binder_len + 1;
Jerry Yu96a2e362022-07-21 15:11:34 +0800582
Jerry Yu96a2e362022-07-21 15:11:34 +0800583 identity_id++;
Gilles Peskine449bd832023-01-11 14:50:10 +0100584 if (matched_identity != -1) {
Jerry Yu1c105562022-07-10 06:32:38 +0000585 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +0100586 }
Jerry Yu1c105562022-07-10 06:32:38 +0000587
Jerry Yu96a2e362022-07-21 15:11:34 +0800588 ret = ssl_tls13_offered_psks_check_identity_match(
Gilles Peskine449bd832023-01-11 14:50:10 +0100589 ssl, identity, identity_len, obfuscated_ticket_age,
590 &psk_type, &session);
591 if (ret != SSL_TLS1_3_OFFERED_PSK_MATCH) {
Jerry Yu96a2e362022-07-21 15:11:34 +0800592 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +0100593 }
Jerry Yu96a2e362022-07-21 15:11:34 +0800594
Gilles Peskine449bd832023-01-11 14:50:10 +0100595 MBEDTLS_SSL_DEBUG_MSG(4, ("found matched identity"));
596 switch (psk_type) {
Jerry Yu0baf9072022-08-25 11:21:04 +0800597 case MBEDTLS_SSL_TLS1_3_PSK_EXTERNAL:
598 ret = ssl_tls13_select_ciphersuite_for_psk(
Gilles Peskine449bd832023-01-11 14:50:10 +0100599 ssl, ciphersuites, ciphersuites_end,
600 &cipher_suite, &ciphersuite_info);
Jerry Yu0baf9072022-08-25 11:21:04 +0800601 break;
602 case MBEDTLS_SSL_TLS1_3_PSK_RESUMPTION:
Jerry Yu82534862022-08-30 10:42:33 +0800603#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Jerry Yu0baf9072022-08-25 11:21:04 +0800604 ret = ssl_tls13_select_ciphersuite_for_resumption(
Gilles Peskine449bd832023-01-11 14:50:10 +0100605 ssl, ciphersuites, ciphersuites_end, &session,
606 &cipher_suite, &ciphersuite_info);
607 if (ret != 0) {
608 mbedtls_ssl_session_free(&session);
609 }
Jerry Yu82534862022-08-30 10:42:33 +0800610#else
611 ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
612#endif
Jerry Yu0baf9072022-08-25 11:21:04 +0800613 break;
614 default:
Gilles Peskine449bd832023-01-11 14:50:10 +0100615 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Jerry Yu0baf9072022-08-25 11:21:04 +0800616 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100617 if (ret != 0) {
Jerry Yuf35ba382022-08-23 17:58:26 +0800618 /* See below, no cipher_suite available, abort handshake */
619 MBEDTLS_SSL_PEND_FATAL_ALERT(
620 MBEDTLS_SSL_ALERT_MSG_DECRYPT_ERROR,
Gilles Peskine449bd832023-01-11 14:50:10 +0100621 MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE);
Jerry Yuf35ba382022-08-23 17:58:26 +0800622 MBEDTLS_SSL_DEBUG_RET(
Gilles Peskine449bd832023-01-11 14:50:10 +0100623 2, "ssl_tls13_select_ciphersuite", ret);
624 return ret;
Jerry Yu5725f1c2022-08-21 17:27:16 +0800625 }
626
Jerry Yu96a2e362022-07-21 15:11:34 +0800627 ret = ssl_tls13_offered_psks_check_binder_match(
Gilles Peskine449bd832023-01-11 14:50:10 +0100628 ssl, binder, binder_len, psk_type,
Dave Rodgman2eab4622023-10-05 13:30:37 +0100629 mbedtls_md_psa_alg_from_type((mbedtls_md_type_t) ciphersuite_info->mac));
Gilles Peskine449bd832023-01-11 14:50:10 +0100630 if (ret != SSL_TLS1_3_OFFERED_PSK_MATCH) {
Jerry Yuc5a23a02022-08-25 10:51:44 +0800631 /* For security reasons, the handshake should be aborted when we
632 * fail to validate a binder value. See RFC 8446 section 4.2.11.2
633 * and appendix E.6. */
Jerry Yu82534862022-08-30 10:42:33 +0800634#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Gilles Peskine449bd832023-01-11 14:50:10 +0100635 mbedtls_ssl_session_free(&session);
Jerry Yu82534862022-08-30 10:42:33 +0800636#endif
Gilles Peskine449bd832023-01-11 14:50:10 +0100637 MBEDTLS_SSL_DEBUG_MSG(3, ("Invalid binder."));
Xiaokang Qian9f1747b2023-03-30 02:50:04 +0000638 MBEDTLS_SSL_DEBUG_RET(
639 1, "ssl_tls13_offered_psks_check_binder_match", ret);
Jerry Yu96a2e362022-07-21 15:11:34 +0800640 MBEDTLS_SSL_PEND_FATAL_ALERT(
641 MBEDTLS_SSL_ALERT_MSG_DECRYPT_ERROR,
Gilles Peskine449bd832023-01-11 14:50:10 +0100642 MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE);
643 return ret;
Jerry Yu96a2e362022-07-21 15:11:34 +0800644 }
Jerry Yu96a2e362022-07-21 15:11:34 +0800645
646 matched_identity = identity_id;
Jerry Yu5725f1c2022-08-21 17:27:16 +0800647
648 /* Update handshake parameters */
Jerry Yue5834fd2022-08-29 20:16:09 +0800649 ssl->handshake->ciphersuite_info = ciphersuite_info;
Jerry Yu4746b102022-09-13 11:11:48 +0800650 ssl->session_negotiate->ciphersuite = cipher_suite;
Gilles Peskine449bd832023-01-11 14:50:10 +0100651 MBEDTLS_SSL_DEBUG_MSG(2, ("overwrite ciphersuite: %04x - %s",
652 cipher_suite, ciphersuite_info->name));
Jerry Yu82534862022-08-30 10:42:33 +0800653#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Gilles Peskine449bd832023-01-11 14:50:10 +0100654 if (psk_type == MBEDTLS_SSL_TLS1_3_PSK_RESUMPTION) {
655 ret = ssl_tls13_session_copy_ticket(ssl->session_negotiate,
656 &session);
657 mbedtls_ssl_session_free(&session);
658 if (ret != 0) {
659 return ret;
660 }
Jerry Yu82534862022-08-30 10:42:33 +0800661 }
662#endif /* MBEDTLS_SSL_SESSION_TICKETS */
Jerry Yu1c105562022-07-10 06:32:38 +0000663 }
664
Gilles Peskine449bd832023-01-11 14:50:10 +0100665 if (p_identity_len != identities_end || p_binder_len != binders_end) {
666 MBEDTLS_SSL_DEBUG_MSG(3, ("pre_shared_key extension decode error"));
667 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR,
668 MBEDTLS_ERR_SSL_DECODE_ERROR);
669 return MBEDTLS_ERR_SSL_DECODE_ERROR;
Jerry Yu1c105562022-07-10 06:32:38 +0000670 }
671
Jerry Yu6f1db3f2022-07-22 23:05:59 +0800672 /* Update the handshake transcript with the binder list. */
Xiaokang Qian9f1747b2023-03-30 02:50:04 +0000673 ret = ssl->handshake->update_checksum(
674 ssl, identities_end, (size_t) (binders_end - identities_end));
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +0100675 if (0 != ret) {
676 MBEDTLS_SSL_DEBUG_RET(1, ("update_checksum"), ret);
677 return ret;
678 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100679 if (matched_identity == -1) {
680 MBEDTLS_SSL_DEBUG_MSG(3, ("No matched PSK or ticket."));
681 return MBEDTLS_ERR_SSL_UNKNOWN_IDENTITY;
Jerry Yu1c105562022-07-10 06:32:38 +0000682 }
683
Gilles Peskine449bd832023-01-11 14:50:10 +0100684 ssl->handshake->selected_identity = (uint16_t) matched_identity;
685 MBEDTLS_SSL_DEBUG_MSG(3, ("Pre shared key found"));
Jerry Yu1c105562022-07-10 06:32:38 +0000686
Gilles Peskine449bd832023-01-11 14:50:10 +0100687 return 0;
Jerry Yu1c105562022-07-10 06:32:38 +0000688}
Jerry Yu032b15ce2022-07-11 06:10:03 +0000689
690/*
691 * struct {
692 * select ( Handshake.msg_type ) {
693 * ....
694 * case server_hello:
695 * uint16 selected_identity;
696 * }
697 * } PreSharedKeyExtension;
698 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100699static int ssl_tls13_write_server_pre_shared_key_ext(mbedtls_ssl_context *ssl,
700 unsigned char *buf,
701 unsigned char *end,
702 size_t *olen)
Jerry Yu032b15ce2022-07-11 06:10:03 +0000703{
Gilles Peskine449bd832023-01-11 14:50:10 +0100704 unsigned char *p = (unsigned char *) buf;
Jerry Yu032b15ce2022-07-11 06:10:03 +0000705
706 *olen = 0;
707
David Horstmann21b89762022-10-06 18:34:28 +0100708 int not_using_psk = 0;
Jerry Yu032b15ce2022-07-11 06:10:03 +0000709#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +0100710 not_using_psk = (mbedtls_svc_key_id_is_null(ssl->handshake->psk_opaque));
Jerry Yu032b15ce2022-07-11 06:10:03 +0000711#else
Gilles Peskine449bd832023-01-11 14:50:10 +0100712 not_using_psk = (ssl->handshake->psk == NULL);
Jerry Yu032b15ce2022-07-11 06:10:03 +0000713#endif
Gilles Peskine449bd832023-01-11 14:50:10 +0100714 if (not_using_psk) {
Jerry Yu032b15ce2022-07-11 06:10:03 +0000715 /* We shouldn't have called this extension writer unless we've
716 * chosen to use a PSK. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100717 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Jerry Yu032b15ce2022-07-11 06:10:03 +0000718 }
719
Gilles Peskine449bd832023-01-11 14:50:10 +0100720 MBEDTLS_SSL_DEBUG_MSG(3, ("server hello, adding pre_shared_key extension"));
721 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 6);
Jerry Yu032b15ce2022-07-11 06:10:03 +0000722
Gilles Peskine449bd832023-01-11 14:50:10 +0100723 MBEDTLS_PUT_UINT16_BE(MBEDTLS_TLS_EXT_PRE_SHARED_KEY, p, 0);
724 MBEDTLS_PUT_UINT16_BE(2, p, 2);
Jerry Yu032b15ce2022-07-11 06:10:03 +0000725
Gilles Peskine449bd832023-01-11 14:50:10 +0100726 MBEDTLS_PUT_UINT16_BE(ssl->handshake->selected_identity, p, 4);
Jerry Yu032b15ce2022-07-11 06:10:03 +0000727
728 *olen = 6;
729
Gilles Peskine449bd832023-01-11 14:50:10 +0100730 MBEDTLS_SSL_DEBUG_MSG(4, ("sent selected_identity: %u",
731 ssl->handshake->selected_identity));
Jerry Yu032b15ce2022-07-11 06:10:03 +0000732
Gilles Peskine449bd832023-01-11 14:50:10 +0100733 mbedtls_ssl_tls13_set_hs_sent_ext_mask(ssl, MBEDTLS_TLS_EXT_PRE_SHARED_KEY);
Jerry Yub95dd362022-11-08 21:19:34 +0800734
Gilles Peskine449bd832023-01-11 14:50:10 +0100735 return 0;
Jerry Yu032b15ce2022-07-11 06:10:03 +0000736}
737
Ronald Cron41a443a2022-10-04 16:38:25 +0200738#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED */
Jerry Yue19e3b92022-07-08 12:04:51 +0000739
XiaokangQian7807f9f2022-02-15 10:04:37 +0000740/* From RFC 8446:
741 * struct {
XiaokangQiancfd925f2022-04-14 07:10:37 +0000742 * ProtocolVersion versions<2..254>;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000743 * } SupportedVersions;
744 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200745MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100746static int ssl_tls13_parse_supported_versions_ext(mbedtls_ssl_context *ssl,
747 const unsigned char *buf,
748 const unsigned char *end)
XiaokangQian7807f9f2022-02-15 10:04:37 +0000749{
XiaokangQian7807f9f2022-02-15 10:04:37 +0000750 const unsigned char *p = buf;
XiaokangQian8f9dfe42022-04-15 02:52:39 +0000751 size_t versions_len;
XiaokangQian4080a7f2022-04-11 09:55:18 +0000752 const unsigned char *versions_end;
XiaokangQian0a1b54e2022-04-21 03:01:38 +0000753 uint16_t tls_version;
Ronald Cron5af4c7f2023-03-07 20:46:59 +0100754 int found_supported_version = 0;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000755
Gilles Peskine449bd832023-01-11 14:50:10 +0100756 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, 1);
XiaokangQian4080a7f2022-04-11 09:55:18 +0000757 versions_len = p[0];
XiaokangQian7807f9f2022-02-15 10:04:37 +0000758 p += 1;
759
Gilles Peskine449bd832023-01-11 14:50:10 +0100760 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, versions_len);
XiaokangQian4080a7f2022-04-11 09:55:18 +0000761 versions_end = p + versions_len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100762 while (p < versions_end) {
763 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, versions_end, 2);
764 tls_version = mbedtls_ssl_read_version(p, ssl->conf->transport);
XiaokangQianb67384d2022-04-19 00:02:38 +0000765 p += 2;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000766
Ronald Cron3bd2b022023-04-03 16:45:39 +0200767 if (MBEDTLS_SSL_VERSION_TLS1_3 == tls_version) {
Ronald Cron5af4c7f2023-03-07 20:46:59 +0100768 found_supported_version = 1;
769 break;
770 }
771
Ronald Cron3bd2b022023-04-03 16:45:39 +0200772 if ((MBEDTLS_SSL_VERSION_TLS1_2 == tls_version) &&
773 mbedtls_ssl_conf_is_tls12_enabled(ssl->conf)) {
Ronald Cron5af4c7f2023-03-07 20:46:59 +0100774 found_supported_version = 1;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000775 break;
776 }
XiaokangQian7807f9f2022-02-15 10:04:37 +0000777 }
778
Ronald Cron5af4c7f2023-03-07 20:46:59 +0100779 if (!found_supported_version) {
780 MBEDTLS_SSL_DEBUG_MSG(1, ("No supported version found."));
XiaokangQian7807f9f2022-02-15 10:04:37 +0000781
Gilles Peskine449bd832023-01-11 14:50:10 +0100782 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_PROTOCOL_VERSION,
783 MBEDTLS_ERR_SSL_BAD_PROTOCOL_VERSION);
784 return MBEDTLS_ERR_SSL_BAD_PROTOCOL_VERSION;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000785 }
786
Ronald Cron5af4c7f2023-03-07 20:46:59 +0100787 MBEDTLS_SSL_DEBUG_MSG(1, ("Negotiated version: [%04x]",
Gilles Peskine449bd832023-01-11 14:50:10 +0100788 (unsigned int) tls_version));
XiaokangQian7807f9f2022-02-15 10:04:37 +0000789
Ronald Cron5af4c7f2023-03-07 20:46:59 +0100790 return (int) tls_version;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000791}
792
Przemek Stekiel8c0a9532023-06-15 16:48:19 +0200793#if defined(PSA_WANT_ALG_ECDH) || defined(PSA_WANT_ALG_FFDH)
XiaokangQiane8ff3502022-04-22 02:34:40 +0000794/*
XiaokangQian7807f9f2022-02-15 10:04:37 +0000795 *
796 * From RFC 8446:
797 * enum {
798 * ... (0xFFFF)
799 * } NamedGroup;
800 * struct {
801 * NamedGroup named_group_list<2..2^16-1>;
802 * } NamedGroupList;
803 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200804MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100805static int ssl_tls13_parse_supported_groups_ext(mbedtls_ssl_context *ssl,
806 const unsigned char *buf,
807 const unsigned char *end)
XiaokangQian7807f9f2022-02-15 10:04:37 +0000808{
XiaokangQian7807f9f2022-02-15 10:04:37 +0000809 const unsigned char *p = buf;
XiaokangQian84823772022-04-19 07:57:30 +0000810 size_t named_group_list_len;
XiaokangQian8f9dfe42022-04-15 02:52:39 +0000811 const unsigned char *named_group_list_end;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000812
Gilles Peskine449bd832023-01-11 14:50:10 +0100813 MBEDTLS_SSL_DEBUG_BUF(3, "supported_groups extension", p, end - buf);
814 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, 2);
815 named_group_list_len = MBEDTLS_GET_UINT16_BE(p, 0);
XiaokangQian7807f9f2022-02-15 10:04:37 +0000816 p += 2;
Gilles Peskine449bd832023-01-11 14:50:10 +0100817 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, named_group_list_len);
XiaokangQian8f9dfe42022-04-15 02:52:39 +0000818 named_group_list_end = p + named_group_list_len;
XiaokangQian4e8cd7b2022-04-21 09:48:09 +0000819 ssl->handshake->hrr_selected_group = 0;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000820
Gilles Peskine449bd832023-01-11 14:50:10 +0100821 while (p < named_group_list_end) {
XiaokangQian08037552022-04-20 07:16:41 +0000822 uint16_t named_group;
Gilles Peskine449bd832023-01-11 14:50:10 +0100823 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, named_group_list_end, 2);
824 named_group = MBEDTLS_GET_UINT16_BE(p, 0);
XiaokangQian08037552022-04-20 07:16:41 +0000825 p += 2;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000826
Gilles Peskine449bd832023-01-11 14:50:10 +0100827 MBEDTLS_SSL_DEBUG_MSG(2,
828 ("got named group: %s(%04x)",
829 mbedtls_ssl_named_group_to_str(named_group),
830 named_group));
XiaokangQian08037552022-04-20 07:16:41 +0000831
Gilles Peskine449bd832023-01-11 14:50:10 +0100832 if (!mbedtls_ssl_named_group_is_offered(ssl, named_group) ||
833 !mbedtls_ssl_named_group_is_supported(named_group) ||
834 ssl->handshake->hrr_selected_group != 0) {
XiaokangQian08037552022-04-20 07:16:41 +0000835 continue;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000836 }
837
Gilles Peskine449bd832023-01-11 14:50:10 +0100838 MBEDTLS_SSL_DEBUG_MSG(2,
839 ("add named group %s(%04x) into received list.",
840 mbedtls_ssl_named_group_to_str(named_group),
841 named_group));
Jerry Yuc1be19f2022-04-23 16:11:39 +0800842
XiaokangQian4e8cd7b2022-04-21 09:48:09 +0000843 ssl->handshake->hrr_selected_group = named_group;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000844 }
845
Gilles Peskine449bd832023-01-11 14:50:10 +0100846 return 0;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000847
848}
Przemek Stekiel8c0a9532023-06-15 16:48:19 +0200849#endif /* PSA_WANT_ALG_ECDH || PSA_WANT_ALG_FFDH */
XiaokangQian7807f9f2022-02-15 10:04:37 +0000850
XiaokangQian08037552022-04-20 07:16:41 +0000851#define SSL_TLS1_3_PARSE_KEY_SHARES_EXT_NO_MATCH 1
852
Valerio Settic9ae8622023-07-25 11:23:50 +0200853#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_EPHEMERAL_ENABLED)
XiaokangQian7807f9f2022-02-15 10:04:37 +0000854/*
855 * ssl_tls13_parse_key_shares_ext() verifies whether the information in the
Xiaokang Qian9f1747b2023-03-30 02:50:04 +0000856 * extension is correct and stores the first acceptable key share and its
857 * associated group.
XiaokangQian7807f9f2022-02-15 10:04:37 +0000858 *
859 * Possible return values are:
860 * - 0: Successful processing of the client provided key share extension.
Xiaokang Qian9f1747b2023-03-30 02:50:04 +0000861 * - SSL_TLS1_3_PARSE_KEY_SHARES_EXT_NO_MATCH: The key shares provided by
862 * the client does not match a group supported by the server. A
863 * HelloRetryRequest will be needed.
XiaokangQiane8ff3502022-04-22 02:34:40 +0000864 * - A negative value for fatal errors.
Jerry Yuc1be19f2022-04-23 16:11:39 +0800865 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200866MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100867static int ssl_tls13_parse_key_shares_ext(mbedtls_ssl_context *ssl,
868 const unsigned char *buf,
869 const unsigned char *end)
XiaokangQian7807f9f2022-02-15 10:04:37 +0000870{
XiaokangQianb67384d2022-04-19 00:02:38 +0000871 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000872 unsigned char const *p = buf;
XiaokangQian8f9dfe42022-04-15 02:52:39 +0000873 unsigned char const *client_shares_end;
Jerry Yuc1be19f2022-04-23 16:11:39 +0800874 size_t client_shares_len;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000875
876 /* From RFC 8446:
877 *
878 * struct {
879 * KeyShareEntry client_shares<0..2^16-1>;
880 * } KeyShareClientHello;
881 *
882 */
883
Gilles Peskine449bd832023-01-11 14:50:10 +0100884 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, 2);
885 client_shares_len = MBEDTLS_GET_UINT16_BE(p, 0);
XiaokangQian7807f9f2022-02-15 10:04:37 +0000886 p += 2;
Gilles Peskine449bd832023-01-11 14:50:10 +0100887 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, client_shares_len);
XiaokangQian7807f9f2022-02-15 10:04:37 +0000888
889 ssl->handshake->offered_group_id = 0;
XiaokangQian8f9dfe42022-04-15 02:52:39 +0000890 client_shares_end = p + client_shares_len;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000891
892 /* We try to find a suitable key share entry and copy it to the
893 * handshake context. Later, we have to find out whether we can do
894 * something with the provided key share or whether we have to
XiaokangQianc5763b52022-04-02 03:34:37 +0000895 * dismiss it and send a HelloRetryRequest message.
896 */
XiaokangQian7807f9f2022-02-15 10:04:37 +0000897
Gilles Peskine449bd832023-01-11 14:50:10 +0100898 while (p < client_shares_end) {
XiaokangQian9b5d04b2022-04-10 10:20:43 +0000899 uint16_t group;
Jerry Yuc1be19f2022-04-23 16:11:39 +0800900 size_t key_exchange_len;
Jerry Yu086edc22022-05-05 10:50:38 +0800901 const unsigned char *key_exchange;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000902
903 /*
904 * struct {
905 * NamedGroup group;
906 * opaque key_exchange<1..2^16-1>;
907 * } KeyShareEntry;
908 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100909 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, client_shares_end, 4);
910 group = MBEDTLS_GET_UINT16_BE(p, 0);
911 key_exchange_len = MBEDTLS_GET_UINT16_BE(p, 2);
Jerry Yuc1be19f2022-04-23 16:11:39 +0800912 p += 4;
Jerry Yu086edc22022-05-05 10:50:38 +0800913 key_exchange = p;
Gilles Peskine449bd832023-01-11 14:50:10 +0100914 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, client_shares_end, key_exchange_len);
Jerry Yu086edc22022-05-05 10:50:38 +0800915 p += key_exchange_len;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000916
917 /* Continue parsing even if we have already found a match,
XiaokangQianc5763b52022-04-02 03:34:37 +0000918 * for input validation purposes.
919 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100920 if (!mbedtls_ssl_named_group_is_offered(ssl, group) ||
921 !mbedtls_ssl_named_group_is_supported(group) ||
922 ssl->handshake->offered_group_id != 0) {
XiaokangQian060d8672022-04-21 09:24:56 +0000923 continue;
924 }
XiaokangQian060d8672022-04-21 09:24:56 +0000925
XiaokangQian7807f9f2022-02-15 10:04:37 +0000926 /*
Przemek Stekielc89f3ea2023-05-18 15:45:53 +0200927 * ECDHE and FFDHE groups are supported
XiaokangQian7807f9f2022-02-15 10:04:37 +0000928 */
Przemek Stekielc89f3ea2023-05-18 15:45:53 +0200929 if (mbedtls_ssl_tls13_named_group_is_ecdhe(group) ||
Przemek Stekield5f79e72023-06-29 09:08:43 +0200930 mbedtls_ssl_tls13_named_group_is_ffdh(group)) {
Przemek Stekielc89f3ea2023-05-18 15:45:53 +0200931 MBEDTLS_SSL_DEBUG_MSG(2, ("ECDH/FFDH group: %s (%04x)",
Gilles Peskine449bd832023-01-11 14:50:10 +0100932 mbedtls_ssl_named_group_to_str(group),
933 group));
Przemek Stekiel7ac93be2023-07-04 10:02:38 +0200934 ret = mbedtls_ssl_tls13_read_public_xxdhe_share(
Gilles Peskine449bd832023-01-11 14:50:10 +0100935 ssl, key_exchange - 2, key_exchange_len + 2);
936 if (ret != 0) {
937 return ret;
938 }
XiaokangQian4e8cd7b2022-04-21 09:48:09 +0000939
Gilles Peskine449bd832023-01-11 14:50:10 +0100940 } else {
941 MBEDTLS_SSL_DEBUG_MSG(4, ("Unrecognized NamedGroup %u",
942 (unsigned) group));
XiaokangQian7807f9f2022-02-15 10:04:37 +0000943 continue;
944 }
945
XiaokangQian9b5d04b2022-04-10 10:20:43 +0000946 ssl->handshake->offered_group_id = group;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000947 }
948
Jerry Yuc1be19f2022-04-23 16:11:39 +0800949
Gilles Peskine449bd832023-01-11 14:50:10 +0100950 if (ssl->handshake->offered_group_id == 0) {
951 MBEDTLS_SSL_DEBUG_MSG(1, ("no matching key share"));
952 return SSL_TLS1_3_PARSE_KEY_SHARES_EXT_NO_MATCH;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000953 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100954 return 0;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000955}
Valerio Settic9ae8622023-07-25 11:23:50 +0200956#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_EPHEMERAL_ENABLED */
XiaokangQian7807f9f2022-02-15 10:04:37 +0000957
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200958MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100959static int ssl_tls13_client_hello_has_exts(mbedtls_ssl_context *ssl,
960 int exts_mask)
XiaokangQian7807f9f2022-02-15 10:04:37 +0000961{
Jerry Yu0c354a22022-08-29 15:25:36 +0800962 int masked = ssl->handshake->received_extensions & exts_mask;
Gilles Peskine449bd832023-01-11 14:50:10 +0100963 return masked == exts_mask;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000964}
965
Jerry Yue5991322022-11-07 14:03:44 +0800966#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200967MBEDTLS_CHECK_RETURN_CRITICAL
XiaokangQianb67384d2022-04-19 00:02:38 +0000968static int ssl_tls13_client_hello_has_exts_for_ephemeral_key_exchange(
Gilles Peskine449bd832023-01-11 14:50:10 +0100969 mbedtls_ssl_context *ssl)
XiaokangQian7807f9f2022-02-15 10:04:37 +0000970{
Gilles Peskine449bd832023-01-11 14:50:10 +0100971 return ssl_tls13_client_hello_has_exts(
972 ssl,
973 MBEDTLS_SSL_EXT_MASK(SUPPORTED_GROUPS) |
974 MBEDTLS_SSL_EXT_MASK(KEY_SHARE) |
975 MBEDTLS_SSL_EXT_MASK(SIG_ALG));
XiaokangQian7807f9f2022-02-15 10:04:37 +0000976}
Jerry Yue5991322022-11-07 14:03:44 +0800977#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED */
XiaokangQian7807f9f2022-02-15 10:04:37 +0000978
Jerry Yue5991322022-11-07 14:03:44 +0800979#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK_ENABLED)
Jerry Yu77f01482022-07-11 07:03:24 +0000980MBEDTLS_CHECK_RETURN_CRITICAL
981static int ssl_tls13_client_hello_has_exts_for_psk_key_exchange(
Gilles Peskine449bd832023-01-11 14:50:10 +0100982 mbedtls_ssl_context *ssl)
Jerry Yu77f01482022-07-11 07:03:24 +0000983{
Gilles Peskine449bd832023-01-11 14:50:10 +0100984 return ssl_tls13_client_hello_has_exts(
985 ssl,
986 MBEDTLS_SSL_EXT_MASK(PRE_SHARED_KEY) |
987 MBEDTLS_SSL_EXT_MASK(PSK_KEY_EXCHANGE_MODES));
Jerry Yu77f01482022-07-11 07:03:24 +0000988}
Jerry Yue5991322022-11-07 14:03:44 +0800989#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK_ENABLED */
Jerry Yu77f01482022-07-11 07:03:24 +0000990
Jerry Yue5991322022-11-07 14:03:44 +0800991#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK_EPHEMERAL_ENABLED)
Jerry Yu77f01482022-07-11 07:03:24 +0000992MBEDTLS_CHECK_RETURN_CRITICAL
993static int ssl_tls13_client_hello_has_exts_for_psk_ephemeral_key_exchange(
Gilles Peskine449bd832023-01-11 14:50:10 +0100994 mbedtls_ssl_context *ssl)
Jerry Yu77f01482022-07-11 07:03:24 +0000995{
Gilles Peskine449bd832023-01-11 14:50:10 +0100996 return ssl_tls13_client_hello_has_exts(
997 ssl,
998 MBEDTLS_SSL_EXT_MASK(SUPPORTED_GROUPS) |
999 MBEDTLS_SSL_EXT_MASK(KEY_SHARE) |
1000 MBEDTLS_SSL_EXT_MASK(PRE_SHARED_KEY) |
1001 MBEDTLS_SSL_EXT_MASK(PSK_KEY_EXCHANGE_MODES));
Jerry Yu77f01482022-07-11 07:03:24 +00001002}
Jerry Yue5991322022-11-07 14:03:44 +08001003#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK_EPHEMERAL_ENABLED */
Jerry Yu77f01482022-07-11 07:03:24 +00001004
Pengyu Lv306a01d2023-02-02 16:35:47 +08001005#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED)
1006MBEDTLS_CHECK_RETURN_CRITICAL
Pengyu Lv52ad3332023-02-14 14:32:37 +08001007static int ssl_tls13_ticket_permission_check(mbedtls_ssl_context *ssl,
1008 unsigned int kex_mode)
Pengyu Lv306a01d2023-02-02 16:35:47 +08001009{
1010#if defined(MBEDTLS_SSL_SESSION_TICKETS)
1011 if (ssl->handshake->resume) {
Pengyu Lv7b711712023-10-24 17:07:14 +08001012 if (mbedtls_ssl_session_check_ticket_flags(
Pengyu Lv306a01d2023-02-02 16:35:47 +08001013 ssl->session_negotiate, kex_mode)) {
1014 return 0;
1015 }
1016 }
1017#else
1018 ((void) ssl);
1019 ((void) kex_mode);
1020#endif
1021 return 1;
1022}
1023#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED */
1024
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001025MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01001026static int ssl_tls13_check_ephemeral_key_exchange(mbedtls_ssl_context *ssl)
XiaokangQian7807f9f2022-02-15 10:04:37 +00001027{
Jerry Yue5991322022-11-07 14:03:44 +08001028#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED)
Pengyu Lv44670c62023-11-07 09:58:53 +08001029 return mbedtls_ssl_conf_tls13_ephemeral_enabled(ssl) &&
Gilles Peskine449bd832023-01-11 14:50:10 +01001030 ssl_tls13_client_hello_has_exts_for_ephemeral_key_exchange(ssl);
Jerry Yue5991322022-11-07 14:03:44 +08001031#else
1032 ((void) ssl);
Gilles Peskine449bd832023-01-11 14:50:10 +01001033 return 0;
Jerry Yue5991322022-11-07 14:03:44 +08001034#endif
Jerry Yu77f01482022-07-11 07:03:24 +00001035}
XiaokangQian7807f9f2022-02-15 10:04:37 +00001036
Jerry Yu77f01482022-07-11 07:03:24 +00001037MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01001038static int ssl_tls13_check_psk_key_exchange(mbedtls_ssl_context *ssl)
Jerry Yu77f01482022-07-11 07:03:24 +00001039{
Jerry Yue5991322022-11-07 14:03:44 +08001040#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK_ENABLED)
Pengyu Lv52ad3332023-02-14 14:32:37 +08001041 return ssl_tls13_ticket_permission_check(
Pengyu Lv306a01d2023-02-02 16:35:47 +08001042 ssl, MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK) &&
1043 mbedtls_ssl_conf_tls13_psk_enabled(ssl) &&
Gilles Peskine449bd832023-01-11 14:50:10 +01001044 mbedtls_ssl_tls13_psk_enabled(ssl) &&
1045 ssl_tls13_client_hello_has_exts_for_psk_key_exchange(ssl);
Jerry Yu77f01482022-07-11 07:03:24 +00001046#else
1047 ((void) ssl);
Gilles Peskine449bd832023-01-11 14:50:10 +01001048 return 0;
Jerry Yu77f01482022-07-11 07:03:24 +00001049#endif
1050}
XiaokangQian7807f9f2022-02-15 10:04:37 +00001051
Jerry Yu77f01482022-07-11 07:03:24 +00001052MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01001053static int ssl_tls13_check_psk_ephemeral_key_exchange(mbedtls_ssl_context *ssl)
Jerry Yu77f01482022-07-11 07:03:24 +00001054{
Jerry Yue5991322022-11-07 14:03:44 +08001055#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK_EPHEMERAL_ENABLED)
Pengyu Lv52ad3332023-02-14 14:32:37 +08001056 return ssl_tls13_ticket_permission_check(
Pengyu Lv306a01d2023-02-02 16:35:47 +08001057 ssl, MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK_EPHEMERAL) &&
1058 mbedtls_ssl_conf_tls13_psk_ephemeral_enabled(ssl) &&
Gilles Peskine449bd832023-01-11 14:50:10 +01001059 mbedtls_ssl_tls13_psk_ephemeral_enabled(ssl) &&
1060 ssl_tls13_client_hello_has_exts_for_psk_ephemeral_key_exchange(ssl);
Jerry Yu77f01482022-07-11 07:03:24 +00001061#else
1062 ((void) ssl);
Gilles Peskine449bd832023-01-11 14:50:10 +01001063 return 0;
Jerry Yu77f01482022-07-11 07:03:24 +00001064#endif
1065}
1066
Gilles Peskine449bd832023-01-11 14:50:10 +01001067static int ssl_tls13_determine_key_exchange_mode(mbedtls_ssl_context *ssl)
Jerry Yu77f01482022-07-11 07:03:24 +00001068{
1069 /*
1070 * Determine the key exchange algorithm to use.
1071 * There are three types of key exchanges supported in TLS 1.3:
1072 * - (EC)DH with ECDSA,
1073 * - (EC)DH with PSK,
1074 * - plain PSK.
1075 *
1076 * The PSK-based key exchanges may additionally be used with 0-RTT.
1077 *
1078 * Our built-in order of preference is
Jerry Yuce6ed702022-07-22 21:49:53 +08001079 * 1 ) (EC)DHE-PSK Mode ( psk_ephemeral )
1080 * 2 ) Certificate Mode ( ephemeral )
1081 * 3 ) Plain PSK Mode ( psk )
Jerry Yu77f01482022-07-11 07:03:24 +00001082 */
1083
Xiaokang Qian9f1747b2023-03-30 02:50:04 +00001084 ssl->handshake->key_exchange_mode =
1085 MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_NONE;
Jerry Yu77f01482022-07-11 07:03:24 +00001086
Gilles Peskine449bd832023-01-11 14:50:10 +01001087 if (ssl_tls13_check_psk_ephemeral_key_exchange(ssl)) {
Jerry Yu77f01482022-07-11 07:03:24 +00001088 ssl->handshake->key_exchange_mode =
1089 MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK_EPHEMERAL;
Gilles Peskine449bd832023-01-11 14:50:10 +01001090 MBEDTLS_SSL_DEBUG_MSG(2, ("key exchange mode: psk_ephemeral"));
1091 } else
1092 if (ssl_tls13_check_ephemeral_key_exchange(ssl)) {
Jerry Yu77f01482022-07-11 07:03:24 +00001093 ssl->handshake->key_exchange_mode =
1094 MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL;
Gilles Peskine449bd832023-01-11 14:50:10 +01001095 MBEDTLS_SSL_DEBUG_MSG(2, ("key exchange mode: ephemeral"));
1096 } else
1097 if (ssl_tls13_check_psk_key_exchange(ssl)) {
Jerry Yuce6ed702022-07-22 21:49:53 +08001098 ssl->handshake->key_exchange_mode =
1099 MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK;
Gilles Peskine449bd832023-01-11 14:50:10 +01001100 MBEDTLS_SSL_DEBUG_MSG(2, ("key exchange mode: psk"));
1101 } else {
Jerry Yu77f01482022-07-11 07:03:24 +00001102 MBEDTLS_SSL_DEBUG_MSG(
Gilles Peskine449bd832023-01-11 14:50:10 +01001103 1,
1104 ("ClientHello message misses mandatory extensions."));
1105 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_MISSING_EXTENSION,
1106 MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER);
1107 return MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER;
Jerry Yu77f01482022-07-11 07:03:24 +00001108 }
1109
Gilles Peskine449bd832023-01-11 14:50:10 +01001110 return 0;
Jerry Yu77f01482022-07-11 07:03:24 +00001111
XiaokangQian7807f9f2022-02-15 10:04:37 +00001112}
1113
XiaokangQian81802f42022-06-10 13:25:22 +00001114#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
Ronald Cron928cbd32022-10-04 16:14:26 +02001115 defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED)
Ronald Cron67ea2542022-09-15 17:34:42 +02001116
1117#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01001118static psa_algorithm_t ssl_tls13_iana_sig_alg_to_psa_alg(uint16_t sig_alg)
Ronald Cron67ea2542022-09-15 17:34:42 +02001119{
Gilles Peskine449bd832023-01-11 14:50:10 +01001120 switch (sig_alg) {
Ronald Cron67ea2542022-09-15 17:34:42 +02001121 case MBEDTLS_TLS1_3_SIG_ECDSA_SECP256R1_SHA256:
Gilles Peskine449bd832023-01-11 14:50:10 +01001122 return PSA_ALG_ECDSA(PSA_ALG_SHA_256);
Ronald Cron67ea2542022-09-15 17:34:42 +02001123 case MBEDTLS_TLS1_3_SIG_ECDSA_SECP384R1_SHA384:
Gilles Peskine449bd832023-01-11 14:50:10 +01001124 return PSA_ALG_ECDSA(PSA_ALG_SHA_384);
Ronald Cron67ea2542022-09-15 17:34:42 +02001125 case MBEDTLS_TLS1_3_SIG_ECDSA_SECP521R1_SHA512:
Gilles Peskine449bd832023-01-11 14:50:10 +01001126 return PSA_ALG_ECDSA(PSA_ALG_SHA_512);
Ronald Cron67ea2542022-09-15 17:34:42 +02001127 case MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA256:
Gilles Peskine449bd832023-01-11 14:50:10 +01001128 return PSA_ALG_RSA_PSS(PSA_ALG_SHA_256);
Ronald Cron67ea2542022-09-15 17:34:42 +02001129 case MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA384:
Gilles Peskine449bd832023-01-11 14:50:10 +01001130 return PSA_ALG_RSA_PSS(PSA_ALG_SHA_384);
Ronald Cron67ea2542022-09-15 17:34:42 +02001131 case MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA512:
Gilles Peskine449bd832023-01-11 14:50:10 +01001132 return PSA_ALG_RSA_PSS(PSA_ALG_SHA_512);
Ronald Cron67ea2542022-09-15 17:34:42 +02001133 case MBEDTLS_TLS1_3_SIG_RSA_PKCS1_SHA256:
Gilles Peskine449bd832023-01-11 14:50:10 +01001134 return PSA_ALG_RSA_PKCS1V15_SIGN(PSA_ALG_SHA_256);
Ronald Cron67ea2542022-09-15 17:34:42 +02001135 case MBEDTLS_TLS1_3_SIG_RSA_PKCS1_SHA384:
Gilles Peskine449bd832023-01-11 14:50:10 +01001136 return PSA_ALG_RSA_PKCS1V15_SIGN(PSA_ALG_SHA_384);
Ronald Cron67ea2542022-09-15 17:34:42 +02001137 case MBEDTLS_TLS1_3_SIG_RSA_PKCS1_SHA512:
Gilles Peskine449bd832023-01-11 14:50:10 +01001138 return PSA_ALG_RSA_PKCS1V15_SIGN(PSA_ALG_SHA_512);
Ronald Cron67ea2542022-09-15 17:34:42 +02001139 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01001140 return PSA_ALG_NONE;
Ronald Cron67ea2542022-09-15 17:34:42 +02001141 }
1142}
1143#endif /* MBEDTLS_USE_PSA_CRYPTO */
1144
XiaokangQian23c5be62022-06-07 02:04:34 +00001145/*
XiaokangQianfb665a82022-06-15 03:57:21 +00001146 * Pick best ( private key, certificate chain ) pair based on the signature
1147 * algorithms supported by the client.
XiaokangQian23c5be62022-06-07 02:04:34 +00001148 */
Ronald Cronce7d76e2022-07-08 18:56:49 +02001149MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01001150static int ssl_tls13_pick_key_cert(mbedtls_ssl_context *ssl)
XiaokangQian23c5be62022-06-07 02:04:34 +00001151{
XiaokangQianfb665a82022-06-15 03:57:21 +00001152 mbedtls_ssl_key_cert *key_cert, *key_cert_list;
XiaokangQian81802f42022-06-10 13:25:22 +00001153 const uint16_t *sig_alg = ssl->handshake->received_sig_algs;
XiaokangQian23c5be62022-06-07 02:04:34 +00001154
1155#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Gilles Peskine449bd832023-01-11 14:50:10 +01001156 if (ssl->handshake->sni_key_cert != NULL) {
XiaokangQianfb665a82022-06-15 03:57:21 +00001157 key_cert_list = ssl->handshake->sni_key_cert;
Gilles Peskine449bd832023-01-11 14:50:10 +01001158 } else
XiaokangQian23c5be62022-06-07 02:04:34 +00001159#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
Gilles Peskine449bd832023-01-11 14:50:10 +01001160 key_cert_list = ssl->conf->key_cert;
XiaokangQian23c5be62022-06-07 02:04:34 +00001161
Gilles Peskine449bd832023-01-11 14:50:10 +01001162 if (key_cert_list == NULL) {
1163 MBEDTLS_SSL_DEBUG_MSG(3, ("server has no certificate"));
1164 return -1;
XiaokangQian23c5be62022-06-07 02:04:34 +00001165 }
1166
Gilles Peskine449bd832023-01-11 14:50:10 +01001167 for (; *sig_alg != MBEDTLS_TLS1_3_SIG_NONE; sig_alg++) {
1168 if (!mbedtls_ssl_sig_alg_is_offered(ssl, *sig_alg)) {
Ronald Cron67ea2542022-09-15 17:34:42 +02001169 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +01001170 }
Ronald Cron67ea2542022-09-15 17:34:42 +02001171
Gilles Peskine449bd832023-01-11 14:50:10 +01001172 if (!mbedtls_ssl_tls13_sig_alg_for_cert_verify_is_supported(*sig_alg)) {
Ronald Cron67ea2542022-09-15 17:34:42 +02001173 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +01001174 }
Ronald Cron67ea2542022-09-15 17:34:42 +02001175
Gilles Peskine449bd832023-01-11 14:50:10 +01001176 for (key_cert = key_cert_list; key_cert != NULL;
1177 key_cert = key_cert->next) {
Ronald Cron67ea2542022-09-15 17:34:42 +02001178#if defined(MBEDTLS_USE_PSA_CRYPTO)
1179 psa_algorithm_t psa_alg = PSA_ALG_NONE;
1180#endif /* MBEDTLS_USE_PSA_CRYPTO */
1181
Gilles Peskine449bd832023-01-11 14:50:10 +01001182 MBEDTLS_SSL_DEBUG_CRT(3, "certificate (chain) candidate",
1183 key_cert->cert);
XiaokangQian81802f42022-06-10 13:25:22 +00001184
1185 /*
Gilles Peskine449bd832023-01-11 14:50:10 +01001186 * This avoids sending the client a cert it'll reject based on
1187 * keyUsage or other extensions.
1188 */
1189 if (mbedtls_x509_crt_check_key_usage(
1190 key_cert->cert, MBEDTLS_X509_KU_DIGITAL_SIGNATURE) != 0 ||
XiaokangQian81802f42022-06-10 13:25:22 +00001191 mbedtls_x509_crt_check_extended_key_usage(
XiaokangQianfb665a82022-06-15 03:57:21 +00001192 key_cert->cert, MBEDTLS_OID_SERVER_AUTH,
Gilles Peskine449bd832023-01-11 14:50:10 +01001193 MBEDTLS_OID_SIZE(MBEDTLS_OID_SERVER_AUTH)) != 0) {
1194 MBEDTLS_SSL_DEBUG_MSG(3, ("certificate mismatch: "
1195 "(extended) key usage extension"));
XiaokangQian81802f42022-06-10 13:25:22 +00001196 continue;
1197 }
XiaokangQian23c5be62022-06-07 02:04:34 +00001198
Gilles Peskine449bd832023-01-11 14:50:10 +01001199 MBEDTLS_SSL_DEBUG_MSG(3,
1200 ("ssl_tls13_pick_key_cert:"
1201 "check signature algorithm %s [%04x]",
1202 mbedtls_ssl_sig_alg_to_str(*sig_alg),
1203 *sig_alg));
Ronald Cron67ea2542022-09-15 17:34:42 +02001204#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01001205 psa_alg = ssl_tls13_iana_sig_alg_to_psa_alg(*sig_alg);
Ronald Cron67ea2542022-09-15 17:34:42 +02001206#endif /* MBEDTLS_USE_PSA_CRYPTO */
1207
Gilles Peskine449bd832023-01-11 14:50:10 +01001208 if (mbedtls_ssl_tls13_check_sig_alg_cert_key_match(
1209 *sig_alg, &key_cert->cert->pk)
Ronald Cron67ea2542022-09-15 17:34:42 +02001210#if defined(MBEDTLS_USE_PSA_CRYPTO)
1211 && psa_alg != PSA_ALG_NONE &&
Gilles Peskine449bd832023-01-11 14:50:10 +01001212 mbedtls_pk_can_do_ext(&key_cert->cert->pk, psa_alg,
1213 PSA_KEY_USAGE_SIGN_HASH) == 1
Ronald Cron67ea2542022-09-15 17:34:42 +02001214#endif /* MBEDTLS_USE_PSA_CRYPTO */
Gilles Peskine449bd832023-01-11 14:50:10 +01001215 ) {
XiaokangQianfb665a82022-06-15 03:57:21 +00001216 ssl->handshake->key_cert = key_cert;
Gilles Peskine449bd832023-01-11 14:50:10 +01001217 MBEDTLS_SSL_DEBUG_MSG(3,
1218 ("ssl_tls13_pick_key_cert:"
1219 "selected signature algorithm"
1220 " %s [%04x]",
1221 mbedtls_ssl_sig_alg_to_str(*sig_alg),
1222 *sig_alg));
XiaokangQianfb665a82022-06-15 03:57:21 +00001223 MBEDTLS_SSL_DEBUG_CRT(
Gilles Peskine449bd832023-01-11 14:50:10 +01001224 3, "selected certificate (chain)",
1225 ssl->handshake->key_cert->cert);
1226 return 0;
XiaokangQian81802f42022-06-10 13:25:22 +00001227 }
XiaokangQian81802f42022-06-10 13:25:22 +00001228 }
XiaokangQian23c5be62022-06-07 02:04:34 +00001229 }
1230
Gilles Peskine449bd832023-01-11 14:50:10 +01001231 MBEDTLS_SSL_DEBUG_MSG(2, ("ssl_tls13_pick_key_cert:"
1232 "no suitable certificate found"));
1233 return -1;
XiaokangQian23c5be62022-06-07 02:04:34 +00001234}
XiaokangQian81802f42022-06-10 13:25:22 +00001235#endif /* MBEDTLS_X509_CRT_PARSE_C &&
Ronald Cron928cbd32022-10-04 16:14:26 +02001236 MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED */
XiaokangQian23c5be62022-06-07 02:04:34 +00001237
XiaokangQian4080a7f2022-04-11 09:55:18 +00001238/*
XiaokangQianed582dd2022-04-13 08:21:05 +00001239 *
1240 * STATE HANDLING: ClientHello
1241 *
XiaokangQianb67384d2022-04-19 00:02:38 +00001242 * There are three possible classes of outcomes when parsing the ClientHello:
XiaokangQianed582dd2022-04-13 08:21:05 +00001243 *
XiaokangQianb67384d2022-04-19 00:02:38 +00001244 * 1) The ClientHello was well-formed and matched the server's configuration.
XiaokangQianed582dd2022-04-13 08:21:05 +00001245 *
1246 * In this case, the server progresses to sending its ServerHello.
1247 *
XiaokangQianb67384d2022-04-19 00:02:38 +00001248 * 2) The ClientHello was well-formed but didn't match the server's
1249 * configuration.
XiaokangQianed582dd2022-04-13 08:21:05 +00001250 *
1251 * For example, the client might not have offered a key share which
1252 * the server supports, or the server might require a cookie.
1253 *
1254 * In this case, the server sends a HelloRetryRequest.
1255 *
XiaokangQianb67384d2022-04-19 00:02:38 +00001256 * 3) The ClientHello was ill-formed
XiaokangQianed582dd2022-04-13 08:21:05 +00001257 *
1258 * In this case, we abort the handshake.
1259 *
1260 */
1261
1262/*
XiaokangQian4080a7f2022-04-11 09:55:18 +00001263 * Structure of this message:
1264 *
XiaokangQiane8ff3502022-04-22 02:34:40 +00001265 * uint16 ProtocolVersion;
1266 * opaque Random[32];
1267 * uint8 CipherSuite[2]; // Cryptographic suite selector
XiaokangQian4080a7f2022-04-11 09:55:18 +00001268 *
XiaokangQiane8ff3502022-04-22 02:34:40 +00001269 * struct {
1270 * ProtocolVersion legacy_version = 0x0303; // TLS v1.2
1271 * Random random;
1272 * opaque legacy_session_id<0..32>;
1273 * CipherSuite cipher_suites<2..2^16-2>;
1274 * opaque legacy_compression_methods<1..2^8-1>;
1275 * Extension extensions<8..2^16-1>;
1276 * } ClientHello;
XiaokangQian4080a7f2022-04-11 09:55:18 +00001277 */
XiaokangQianed582dd2022-04-13 08:21:05 +00001278
1279#define SSL_CLIENT_HELLO_OK 0
1280#define SSL_CLIENT_HELLO_HRR_REQUIRED 1
Ronald Cron5af4c7f2023-03-07 20:46:59 +01001281#define SSL_CLIENT_HELLO_TLS1_2 2
XiaokangQianed582dd2022-04-13 08:21:05 +00001282
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001283MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01001284static int ssl_tls13_parse_client_hello(mbedtls_ssl_context *ssl,
1285 const unsigned char *buf,
1286 const unsigned char *end)
XiaokangQian7807f9f2022-02-15 10:04:37 +00001287{
XiaokangQianb67384d2022-04-19 00:02:38 +00001288 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1289 const unsigned char *p = buf;
Ronald Crond540d992023-03-07 09:41:48 +01001290 const unsigned char *random;
XiaokangQian4080a7f2022-04-11 09:55:18 +00001291 size_t legacy_session_id_len;
Ronald Croncada4102023-03-07 09:51:39 +01001292 const unsigned char *legacy_session_id;
XiaokangQian318dc762022-04-20 09:43:51 +00001293 size_t cipher_suites_len;
Ronald Cron2f16b4e2023-03-07 10:07:32 +01001294 const unsigned char *cipher_suites;
XiaokangQian060d8672022-04-21 09:24:56 +00001295 const unsigned char *cipher_suites_end;
XiaokangQianb67384d2022-04-19 00:02:38 +00001296 size_t extensions_len;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001297 const unsigned char *extensions_end;
Ronald Croneff56732023-04-03 17:36:31 +02001298 const unsigned char *supported_versions_data;
1299 const unsigned char *supported_versions_data_end;
Jerry Yuc4bf5d62022-10-29 09:08:47 +08001300 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
Jerry Yu49ca9282022-05-05 11:05:22 +08001301 int hrr_required = 0;
Ronald Cron8a74f072023-06-14 17:59:29 +02001302 int no_usable_share_for_key_agreement = 0;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001303
Ronald Cron41a443a2022-10-04 16:38:25 +02001304#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED)
Jerry Yu29d9faa2022-08-23 17:52:45 +08001305 const unsigned char *pre_shared_key_ext = NULL;
Jerry Yu1c105562022-07-10 06:32:38 +00001306 const unsigned char *pre_shared_key_ext_end = NULL;
Ronald Cron41a443a2022-10-04 16:38:25 +02001307#endif
XiaokangQian7807f9f2022-02-15 10:04:37 +00001308
XiaokangQian7807f9f2022-02-15 10:04:37 +00001309 /*
XiaokangQianb67384d2022-04-19 00:02:38 +00001310 * ClientHello layout:
XiaokangQian7807f9f2022-02-15 10:04:37 +00001311 * 0 . 1 protocol version
XiaokangQiane8ff3502022-04-22 02:34:40 +00001312 * 2 . 33 random bytes
XiaokangQianc5763b52022-04-02 03:34:37 +00001313 * 34 . 34 session id length ( 1 byte )
XiaokangQian7807f9f2022-02-15 10:04:37 +00001314 * 35 . 34+x session id
XiaokangQian7807f9f2022-02-15 10:04:37 +00001315 * .. . .. ciphersuite list length ( 2 bytes )
1316 * .. . .. ciphersuite list
1317 * .. . .. compression alg. list length ( 1 byte )
1318 * .. . .. compression alg. list
1319 * .. . .. extensions length ( 2 bytes, optional )
1320 * .. . .. extensions ( optional )
1321 */
1322
XiaokangQianb67384d2022-04-19 00:02:38 +00001323 /*
bootstrap-prime6dbbf442022-05-17 19:30:44 -04001324 * Minimal length ( with everything empty and extensions omitted ) is
XiaokangQian7807f9f2022-02-15 10:04:37 +00001325 * 2 + 32 + 1 + 2 + 1 = 38 bytes. Check that first, so that we can
1326 * read at least up to session id length without worrying.
1327 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001328 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, 38);
XiaokangQian7807f9f2022-02-15 10:04:37 +00001329
1330 /* ...
1331 * ProtocolVersion legacy_version = 0x0303; // TLS 1.2
1332 * ...
1333 * with ProtocolVersion defined as:
1334 * uint16 ProtocolVersion;
1335 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001336 if (mbedtls_ssl_read_version(p, ssl->conf->transport) !=
1337 MBEDTLS_SSL_VERSION_TLS1_2) {
1338 MBEDTLS_SSL_DEBUG_MSG(1, ("Unsupported version of TLS."));
1339 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_PROTOCOL_VERSION,
1340 MBEDTLS_ERR_SSL_BAD_PROTOCOL_VERSION);
1341 return MBEDTLS_ERR_SSL_BAD_PROTOCOL_VERSION;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001342 }
1343 p += 2;
1344
Jerry Yuc1be19f2022-04-23 16:11:39 +08001345 /* ...
1346 * Random random;
1347 * ...
XiaokangQianb67384d2022-04-19 00:02:38 +00001348 * with Random defined as:
1349 * opaque Random[32];
XiaokangQian7807f9f2022-02-15 10:04:37 +00001350 */
Ronald Crond540d992023-03-07 09:41:48 +01001351 random = p;
XiaokangQian08037552022-04-20 07:16:41 +00001352 p += MBEDTLS_CLIENT_HELLO_RANDOM_LEN;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001353
Jerry Yuc1be19f2022-04-23 16:11:39 +08001354 /* ...
XiaokangQianb67384d2022-04-19 00:02:38 +00001355 * opaque legacy_session_id<0..32>;
Jerry Yuc1be19f2022-04-23 16:11:39 +08001356 * ...
XiaokangQian7807f9f2022-02-15 10:04:37 +00001357 */
Ronald Croncada4102023-03-07 09:51:39 +01001358 legacy_session_id_len = *(p++);
1359 legacy_session_id = p;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001360
XiaokangQianb67384d2022-04-19 00:02:38 +00001361 /*
1362 * Check we have enough data for the legacy session identifier
Jerry Yue95c8af2022-07-26 15:48:20 +08001363 * and the ciphersuite list length.
XiaokangQianb67384d2022-04-19 00:02:38 +00001364 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001365 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, legacy_session_id_len + 2);
XiaokangQian4080a7f2022-04-11 09:55:18 +00001366 p += legacy_session_id_len;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001367
Ronald Cron2f16b4e2023-03-07 10:07:32 +01001368 /* ...
1369 * CipherSuite cipher_suites<2..2^16-2>;
1370 * ...
1371 * with CipherSuite defined as:
1372 * uint8 CipherSuite[2];
1373 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001374 cipher_suites_len = MBEDTLS_GET_UINT16_BE(p, 0);
XiaokangQian7807f9f2022-02-15 10:04:37 +00001375 p += 2;
Ronald Cron2f16b4e2023-03-07 10:07:32 +01001376 cipher_suites = p;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001377
Ronald Cronfc7ae872023-02-16 15:32:19 +01001378 /*
1379 * The length of the ciphersuite list has to be even.
1380 */
1381 if (cipher_suites_len & 1) {
1382 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR,
1383 MBEDTLS_ERR_SSL_DECODE_ERROR);
1384 return MBEDTLS_ERR_SSL_DECODE_ERROR;
1385 }
XiaokangQian7807f9f2022-02-15 10:04:37 +00001386
XiaokangQianb67384d2022-04-19 00:02:38 +00001387 /* Check we have enough data for the ciphersuite list, the legacy
1388 * compression methods and the length of the extensions.
Jerry Yue95c8af2022-07-26 15:48:20 +08001389 *
1390 * cipher_suites cipher_suites_len bytes
1391 * legacy_compression_methods 2 bytes
1392 * extensions_len 2 bytes
XiaokangQianb67384d2022-04-19 00:02:38 +00001393 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001394 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, cipher_suites_len + 2 + 2);
Ronald Cron8c527d02023-03-07 15:47:47 +01001395 p += cipher_suites_len;
1396 cipher_suites_end = p;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001397
Jerry Yu5725f1c2022-08-21 17:27:16 +08001398 /*
Ronald Cron8c527d02023-03-07 15:47:47 +01001399 * Search for the supported versions extension and parse it to determine
1400 * if the client supports TLS 1.3.
Jerry Yu24b8c812022-08-20 19:06:56 +08001401 */
Ronald Cron8c527d02023-03-07 15:47:47 +01001402 ret = mbedtls_ssl_tls13_is_supported_versions_ext_present_in_exts(
1403 ssl, p + 2, end,
Ronald Croneff56732023-04-03 17:36:31 +02001404 &supported_versions_data, &supported_versions_data_end);
Ronald Cron8c527d02023-03-07 15:47:47 +01001405 if (ret < 0) {
1406 MBEDTLS_SSL_DEBUG_RET(1,
1407 ("mbedtls_ssl_tls13_is_supported_versions_ext_present_in_exts"), ret);
1408 return ret;
1409 }
1410
1411 if (ret == 0) {
Ronald Cron5af4c7f2023-03-07 20:46:59 +01001412 return SSL_CLIENT_HELLO_TLS1_2;
Ronald Cron8c527d02023-03-07 15:47:47 +01001413 }
1414
Ronald Cron5af4c7f2023-03-07 20:46:59 +01001415 if (ret == 1) {
1416 ret = ssl_tls13_parse_supported_versions_ext(ssl,
Ronald Croneff56732023-04-03 17:36:31 +02001417 supported_versions_data,
1418 supported_versions_data_end);
Ronald Cron5af4c7f2023-03-07 20:46:59 +01001419 if (ret < 0) {
1420 MBEDTLS_SSL_DEBUG_RET(1,
1421 ("ssl_tls13_parse_supported_versions_ext"), ret);
1422 return ret;
1423 }
1424
Ronald Cronb828c7d2023-04-03 16:37:22 +02001425 /*
1426 * The supported versions extension was parsed successfully as the
1427 * value returned by ssl_tls13_parse_supported_versions_ext() is
1428 * positive. The return value is then equal to
1429 * MBEDTLS_SSL_VERSION_TLS1_2 or MBEDTLS_SSL_VERSION_TLS1_3, defining
1430 * the TLS version to negotiate.
1431 */
Ronald Cron5af4c7f2023-03-07 20:46:59 +01001432 if (MBEDTLS_SSL_VERSION_TLS1_2 == ret) {
1433 return SSL_CLIENT_HELLO_TLS1_2;
1434 }
Ronald Cron8c527d02023-03-07 15:47:47 +01001435 }
1436
1437 /*
1438 * We negotiate TLS 1.3.
Ronald Cron64582392023-03-07 09:21:40 +01001439 */
1440 ssl->tls_version = MBEDTLS_SSL_VERSION_TLS1_3;
1441
1442#if defined(MBEDTLS_SSL_SESSION_TICKETS)
1443 /* Store minor version for later use with ticket serialization. */
1444 ssl->session_negotiate->tls_version = MBEDTLS_SSL_VERSION_TLS1_3;
1445 ssl->session_negotiate->endpoint = ssl->conf->endpoint;
XiaokangQian060d8672022-04-21 09:24:56 +00001446#endif
Ronald Cron64582392023-03-07 09:21:40 +01001447
1448 /*
Ronald Crondad02b22023-04-06 09:57:52 +02001449 * We are negotiating the version 1.3 of the protocol. Do what we have
Ronald Croncada4102023-03-07 09:51:39 +01001450 * postponed: copy of the client random bytes, copy of the legacy session
Ronald Cron2f16b4e2023-03-07 10:07:32 +01001451 * identifier and selection of the TLS 1.3 cipher suite.
Ronald Crond540d992023-03-07 09:41:48 +01001452 */
1453 MBEDTLS_SSL_DEBUG_BUF(3, "client hello, random bytes",
1454 random, MBEDTLS_CLIENT_HELLO_RANDOM_LEN);
1455 memcpy(&handshake->randbytes[0], random, MBEDTLS_CLIENT_HELLO_RANDOM_LEN);
1456
Ronald Croncada4102023-03-07 09:51:39 +01001457 if (legacy_session_id_len > sizeof(ssl->session_negotiate->id)) {
1458 MBEDTLS_SSL_DEBUG_MSG(1, ("bad client hello message"));
1459 return MBEDTLS_ERR_SSL_DECODE_ERROR;
1460 }
1461 ssl->session_negotiate->id_len = legacy_session_id_len;
1462 MBEDTLS_SSL_DEBUG_BUF(3, "client hello, session id",
1463 legacy_session_id, legacy_session_id_len);
1464 memcpy(&ssl->session_negotiate->id[0],
1465 legacy_session_id, legacy_session_id_len);
Jerry Yu5725f1c2022-08-21 17:27:16 +08001466
1467 /*
1468 * Search for a matching ciphersuite
1469 */
Ronald Cron2f16b4e2023-03-07 10:07:32 +01001470 MBEDTLS_SSL_DEBUG_BUF(3, "client hello, list of cipher suites",
1471 cipher_suites, cipher_suites_len);
Ronald Crone45afd72023-04-04 15:10:06 +02001472 for (const unsigned char *cipher_suites_p = cipher_suites;
1473 cipher_suites_p < cipher_suites_end; cipher_suites_p += 2) {
XiaokangQiane8ff3502022-04-22 02:34:40 +00001474 uint16_t cipher_suite;
Gilles Peskine449bd832023-01-11 14:50:10 +01001475 const mbedtls_ssl_ciphersuite_t *ciphersuite_info;
Jerry Yu5725f1c2022-08-21 17:27:16 +08001476
Ronald Crond89360b2023-02-21 08:53:33 +01001477 /*
Ronald Crone45afd72023-04-04 15:10:06 +02001478 * "cipher_suites_end - cipher_suites_p is even" is an invariant of the
1479 * loop. As cipher_suites_end - cipher_suites_p > 0, we have
1480 * cipher_suites_end - cipher_suites_p >= 2 and it is thus safe to read
1481 * two bytes.
Ronald Crond89360b2023-02-21 08:53:33 +01001482 */
Ronald Crone45afd72023-04-04 15:10:06 +02001483 cipher_suite = MBEDTLS_GET_UINT16_BE(cipher_suites_p, 0);
Jerry Yuc5a23a02022-08-25 10:51:44 +08001484 ciphersuite_info = ssl_tls13_validate_peer_ciphersuite(
Gilles Peskine449bd832023-01-11 14:50:10 +01001485 ssl, cipher_suite);
1486 if (ciphersuite_info == NULL) {
Jerry Yu5725f1c2022-08-21 17:27:16 +08001487 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +01001488 }
Jerry Yu5725f1c2022-08-21 17:27:16 +08001489
Jerry Yu5725f1c2022-08-21 17:27:16 +08001490 ssl->session_negotiate->ciphersuite = cipher_suite;
Jerry Yuc4bf5d62022-10-29 09:08:47 +08001491 handshake->ciphersuite_info = ciphersuite_info;
Gilles Peskine449bd832023-01-11 14:50:10 +01001492 MBEDTLS_SSL_DEBUG_MSG(2, ("selected ciphersuite: %04x - %s",
1493 cipher_suite,
1494 ciphersuite_info->name));
Ronald Cron25e9ec62023-02-16 15:35:16 +01001495 break;
XiaokangQian17f974c2022-04-19 09:57:41 +00001496 }
Jerry Yu5725f1c2022-08-21 17:27:16 +08001497
Gilles Peskine449bd832023-01-11 14:50:10 +01001498 if (handshake->ciphersuite_info == NULL) {
1499 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE,
1500 MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE);
1501 return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
Jerry Yu5725f1c2022-08-21 17:27:16 +08001502 }
Jerry Yuc1be19f2022-04-23 16:11:39 +08001503
XiaokangQian4080a7f2022-04-11 09:55:18 +00001504 /* ...
XiaokangQianb67384d2022-04-19 00:02:38 +00001505 * opaque legacy_compression_methods<1..2^8-1>;
XiaokangQian4080a7f2022-04-11 09:55:18 +00001506 * ...
XiaokangQian7807f9f2022-02-15 10:04:37 +00001507 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001508 if (p[0] != 1 || p[1] != MBEDTLS_SSL_COMPRESS_NULL) {
1509 MBEDTLS_SSL_DEBUG_MSG(1, ("bad legacy compression method"));
1510 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER,
1511 MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER);
1512 return MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001513 }
XiaokangQianb67384d2022-04-19 00:02:38 +00001514 p += 2;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001515
Jerry Yuc1be19f2022-04-23 16:11:39 +08001516 /* ...
XiaokangQianb67384d2022-04-19 00:02:38 +00001517 * Extension extensions<8..2^16-1>;
Jerry Yuc1be19f2022-04-23 16:11:39 +08001518 * ...
XiaokangQianb67384d2022-04-19 00:02:38 +00001519 * with Extension defined as:
1520 * struct {
1521 * ExtensionType extension_type;
1522 * opaque extension_data<0..2^16-1>;
1523 * } Extension;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001524 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001525 extensions_len = MBEDTLS_GET_UINT16_BE(p, 0);
XiaokangQian7807f9f2022-02-15 10:04:37 +00001526 p += 2;
Gilles Peskine449bd832023-01-11 14:50:10 +01001527 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, extensions_len);
XiaokangQiane8ff3502022-04-22 02:34:40 +00001528 extensions_end = p + extensions_len;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001529
Gilles Peskine449bd832023-01-11 14:50:10 +01001530 MBEDTLS_SSL_DEBUG_BUF(3, "client hello extensions", p, extensions_len);
Jerry Yu63a459c2022-10-31 13:38:40 +08001531 handshake->received_extensions = MBEDTLS_SSL_EXT_MASK_NONE;
Jerry Yu0c354a22022-08-29 15:25:36 +08001532
Gilles Peskine449bd832023-01-11 14:50:10 +01001533 while (p < extensions_end) {
XiaokangQian7807f9f2022-02-15 10:04:37 +00001534 unsigned int extension_type;
1535 size_t extension_data_len;
1536 const unsigned char *extension_data_end;
1537
Jerry Yu0c354a22022-08-29 15:25:36 +08001538 /* RFC 8446, section 4.2.11
Jerry Yu1c9247c2022-07-21 12:37:39 +08001539 *
1540 * The "pre_shared_key" extension MUST be the last extension in the
1541 * ClientHello (this facilitates implementation as described below).
1542 * Servers MUST check that it is the last extension and otherwise fail
1543 * the handshake with an "illegal_parameter" alert.
1544 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001545 if (handshake->received_extensions & MBEDTLS_SSL_EXT_MASK(PRE_SHARED_KEY)) {
Jerry Yu1c9247c2022-07-21 12:37:39 +08001546 MBEDTLS_SSL_DEBUG_MSG(
Gilles Peskine449bd832023-01-11 14:50:10 +01001547 3, ("pre_shared_key is not last extension."));
Jerry Yu1c9247c2022-07-21 12:37:39 +08001548 MBEDTLS_SSL_PEND_FATAL_ALERT(
1549 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER,
Gilles Peskine449bd832023-01-11 14:50:10 +01001550 MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER);
1551 return MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER;
Jerry Yu1c9247c2022-07-21 12:37:39 +08001552 }
1553
Gilles Peskine449bd832023-01-11 14:50:10 +01001554 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, extensions_end, 4);
1555 extension_type = MBEDTLS_GET_UINT16_BE(p, 0);
1556 extension_data_len = MBEDTLS_GET_UINT16_BE(p, 2);
XiaokangQian7807f9f2022-02-15 10:04:37 +00001557 p += 4;
1558
Gilles Peskine449bd832023-01-11 14:50:10 +01001559 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, extensions_end, extension_data_len);
XiaokangQian7807f9f2022-02-15 10:04:37 +00001560 extension_data_end = p + extension_data_len;
1561
Jerry Yuc4bf5d62022-10-29 09:08:47 +08001562 ret = mbedtls_ssl_tls13_check_received_extension(
Gilles Peskine449bd832023-01-11 14:50:10 +01001563 ssl, MBEDTLS_SSL_HS_CLIENT_HELLO, extension_type,
1564 MBEDTLS_SSL_TLS1_3_ALLOWED_EXTS_OF_CH);
1565 if (ret != 0) {
1566 return ret;
1567 }
Jerry Yue18dc7e2022-08-04 16:29:22 +08001568
Gilles Peskine449bd832023-01-11 14:50:10 +01001569 switch (extension_type) {
XiaokangQian40a35232022-05-07 09:02:40 +00001570#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
1571 case MBEDTLS_TLS_EXT_SERVERNAME:
Gilles Peskine449bd832023-01-11 14:50:10 +01001572 MBEDTLS_SSL_DEBUG_MSG(3, ("found ServerName extension"));
1573 ret = mbedtls_ssl_parse_server_name_ext(ssl, p,
1574 extension_data_end);
1575 if (ret != 0) {
XiaokangQian40a35232022-05-07 09:02:40 +00001576 MBEDTLS_SSL_DEBUG_RET(
Gilles Peskine449bd832023-01-11 14:50:10 +01001577 1, "mbedtls_ssl_parse_servername_ext", ret);
1578 return ret;
XiaokangQian40a35232022-05-07 09:02:40 +00001579 }
XiaokangQian40a35232022-05-07 09:02:40 +00001580 break;
1581#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
1582
Przemek Stekiel8c0a9532023-06-15 16:48:19 +02001583#if defined(PSA_WANT_ALG_ECDH) || defined(PSA_WANT_ALG_FFDH)
XiaokangQian7807f9f2022-02-15 10:04:37 +00001584 case MBEDTLS_TLS_EXT_SUPPORTED_GROUPS:
Gilles Peskine449bd832023-01-11 14:50:10 +01001585 MBEDTLS_SSL_DEBUG_MSG(3, ("found supported group extension"));
XiaokangQian7807f9f2022-02-15 10:04:37 +00001586
1587 /* Supported Groups Extension
1588 *
1589 * When sent by the client, the "supported_groups" extension
1590 * indicates the named groups which the client supports,
1591 * ordered from most preferred to least preferred.
1592 */
Jerry Yuc1be19f2022-04-23 16:11:39 +08001593 ret = ssl_tls13_parse_supported_groups_ext(
Gilles Peskine449bd832023-01-11 14:50:10 +01001594 ssl, p, extension_data_end);
1595 if (ret != 0) {
Xiaokang Qian9f1747b2023-03-30 02:50:04 +00001596 MBEDTLS_SSL_DEBUG_RET(
Xiaokang Qian8bce0e62023-04-04 10:15:48 +00001597 1, "ssl_tls13_parse_supported_groups_ext", ret);
Gilles Peskine449bd832023-01-11 14:50:10 +01001598 return ret;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001599 }
1600
XiaokangQian7807f9f2022-02-15 10:04:37 +00001601 break;
Przemek Stekiel8c0a9532023-06-15 16:48:19 +02001602#endif /* PSA_WANT_ALG_ECDH || PSA_WANT_ALG_FFDH*/
XiaokangQian7807f9f2022-02-15 10:04:37 +00001603
Valerio Settic9ae8622023-07-25 11:23:50 +02001604#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_EPHEMERAL_ENABLED)
XiaokangQian7807f9f2022-02-15 10:04:37 +00001605 case MBEDTLS_TLS_EXT_KEY_SHARE:
Gilles Peskine449bd832023-01-11 14:50:10 +01001606 MBEDTLS_SSL_DEBUG_MSG(3, ("found key share extension"));
XiaokangQian7807f9f2022-02-15 10:04:37 +00001607
1608 /*
1609 * Key Share Extension
1610 *
1611 * When sent by the client, the "key_share" extension
1612 * contains the endpoint's cryptographic parameters for
1613 * ECDHE/DHE key establishment methods.
1614 */
Jerry Yuc1be19f2022-04-23 16:11:39 +08001615 ret = ssl_tls13_parse_key_shares_ext(
Gilles Peskine449bd832023-01-11 14:50:10 +01001616 ssl, p, extension_data_end);
1617 if (ret == SSL_TLS1_3_PARSE_KEY_SHARES_EXT_NO_MATCH) {
Ronald Cron8a74f072023-06-14 17:59:29 +02001618 MBEDTLS_SSL_DEBUG_MSG(2, ("No usable share for key agreement."));
1619 no_usable_share_for_key_agreement = 1;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001620 }
1621
Gilles Peskine449bd832023-01-11 14:50:10 +01001622 if (ret < 0) {
Jerry Yu582dd062022-04-22 21:59:01 +08001623 MBEDTLS_SSL_DEBUG_RET(
Gilles Peskine449bd832023-01-11 14:50:10 +01001624 1, "ssl_tls13_parse_key_shares_ext", ret);
1625 return ret;
Jerry Yu582dd062022-04-22 21:59:01 +08001626 }
XiaokangQian7807f9f2022-02-15 10:04:37 +00001627
XiaokangQian7807f9f2022-02-15 10:04:37 +00001628 break;
Valerio Settic9ae8622023-07-25 11:23:50 +02001629#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_EPHEMERAL_ENABLED */
XiaokangQian7807f9f2022-02-15 10:04:37 +00001630
1631 case MBEDTLS_TLS_EXT_SUPPORTED_VERSIONS:
Ronald Cron8c527d02023-03-07 15:47:47 +01001632 /* Already parsed */
XiaokangQian7807f9f2022-02-15 10:04:37 +00001633 break;
1634
Ronald Cron41a443a2022-10-04 16:38:25 +02001635#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED)
Jerry Yue19e3b92022-07-08 12:04:51 +00001636 case MBEDTLS_TLS_EXT_PSK_KEY_EXCHANGE_MODES:
Xiaokang Qian9f1747b2023-03-30 02:50:04 +00001637 MBEDTLS_SSL_DEBUG_MSG(
1638 3, ("found psk key exchange modes extension"));
Jerry Yue19e3b92022-07-08 12:04:51 +00001639
1640 ret = ssl_tls13_parse_key_exchange_modes_ext(
Gilles Peskine449bd832023-01-11 14:50:10 +01001641 ssl, p, extension_data_end);
1642 if (ret != 0) {
Jerry Yue19e3b92022-07-08 12:04:51 +00001643 MBEDTLS_SSL_DEBUG_RET(
Gilles Peskine449bd832023-01-11 14:50:10 +01001644 1, "ssl_tls13_parse_key_exchange_modes_ext", ret);
1645 return ret;
Jerry Yue19e3b92022-07-08 12:04:51 +00001646 }
1647
Jerry Yue19e3b92022-07-08 12:04:51 +00001648 break;
Ronald Cron41a443a2022-10-04 16:38:25 +02001649#endif
Jerry Yue19e3b92022-07-08 12:04:51 +00001650
Jerry Yu1c105562022-07-10 06:32:38 +00001651 case MBEDTLS_TLS_EXT_PRE_SHARED_KEY:
Gilles Peskine449bd832023-01-11 14:50:10 +01001652 MBEDTLS_SSL_DEBUG_MSG(3, ("found pre_shared_key extension"));
1653 if ((handshake->received_extensions &
1654 MBEDTLS_SSL_EXT_MASK(PSK_KEY_EXCHANGE_MODES)) == 0) {
Jerry Yu13ab81d2022-07-22 23:17:11 +08001655 MBEDTLS_SSL_PEND_FATAL_ALERT(
1656 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER,
Gilles Peskine449bd832023-01-11 14:50:10 +01001657 MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER);
1658 return MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER;
Jerry Yu13ab81d2022-07-22 23:17:11 +08001659 }
Ronald Cron41a443a2022-10-04 16:38:25 +02001660#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED)
Jerry Yu1c105562022-07-10 06:32:38 +00001661 /* Delay processing of the PSK identity once we have
1662 * found out which algorithms to use. We keep a pointer
1663 * to the buffer and the size for later processing.
1664 */
Jerry Yu29d9faa2022-08-23 17:52:45 +08001665 pre_shared_key_ext = p;
Jerry Yu1c105562022-07-10 06:32:38 +00001666 pre_shared_key_ext_end = extension_data_end;
Jerry Yue18dc7e2022-08-04 16:29:22 +08001667#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED */
Jerry Yu1c105562022-07-10 06:32:38 +00001668 break;
Jerry Yu1c105562022-07-10 06:32:38 +00001669
XiaokangQianacb39922022-06-17 10:18:48 +00001670#if defined(MBEDTLS_SSL_ALPN)
1671 case MBEDTLS_TLS_EXT_ALPN:
Gilles Peskine449bd832023-01-11 14:50:10 +01001672 MBEDTLS_SSL_DEBUG_MSG(3, ("found alpn extension"));
XiaokangQianacb39922022-06-17 10:18:48 +00001673
Gilles Peskine449bd832023-01-11 14:50:10 +01001674 ret = mbedtls_ssl_parse_alpn_ext(ssl, p, extension_data_end);
1675 if (ret != 0) {
XiaokangQianacb39922022-06-17 10:18:48 +00001676 MBEDTLS_SSL_DEBUG_RET(
Gilles Peskine449bd832023-01-11 14:50:10 +01001677 1, ("mbedtls_ssl_parse_alpn_ext"), ret);
1678 return ret;
XiaokangQianacb39922022-06-17 10:18:48 +00001679 }
XiaokangQianacb39922022-06-17 10:18:48 +00001680 break;
1681#endif /* MBEDTLS_SSL_ALPN */
1682
Ronald Cron928cbd32022-10-04 16:14:26 +02001683#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED)
XiaokangQian7807f9f2022-02-15 10:04:37 +00001684 case MBEDTLS_TLS_EXT_SIG_ALG:
Gilles Peskine449bd832023-01-11 14:50:10 +01001685 MBEDTLS_SSL_DEBUG_MSG(3, ("found signature_algorithms extension"));
XiaokangQian7807f9f2022-02-15 10:04:37 +00001686
Gabor Mezei078e8032022-04-27 21:17:56 +02001687 ret = mbedtls_ssl_parse_sig_alg_ext(
Gilles Peskine449bd832023-01-11 14:50:10 +01001688 ssl, p, extension_data_end);
1689 if (ret != 0) {
Xiaokang Qian49f39c12023-04-06 02:31:02 +00001690 MBEDTLS_SSL_DEBUG_RET(
1691 1, "mbedtls_ssl_parse_sig_alg_ext", ret);
Gilles Peskine449bd832023-01-11 14:50:10 +01001692 return ret;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001693 }
XiaokangQian7807f9f2022-02-15 10:04:37 +00001694 break;
Ronald Cron928cbd32022-10-04 16:14:26 +02001695#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED */
XiaokangQian7807f9f2022-02-15 10:04:37 +00001696
Jan Bruckner151f6422023-02-10 12:45:19 +01001697#if defined(MBEDTLS_SSL_RECORD_SIZE_LIMIT)
1698 case MBEDTLS_TLS_EXT_RECORD_SIZE_LIMIT:
1699 MBEDTLS_SSL_DEBUG_MSG(3, ("found record_size_limit extension"));
1700
Xiaokang Qian9f1747b2023-03-30 02:50:04 +00001701 ret = mbedtls_ssl_tls13_parse_record_size_limit_ext(
1702 ssl, p, extension_data_end);
Jan Bruckner151f6422023-02-10 12:45:19 +01001703
Xiaokang Qian09c3ccc2023-04-04 10:18:38 +00001704 /*
1705 * TODO: Return unconditionally here until we handle the record
1706 * size limit correctly.
1707 * Once handled correctly, only return in case of errors.
1708 */
Jan Bruckner151f6422023-02-10 12:45:19 +01001709 return ret;
1710
1711 break;
1712#endif /* MBEDTLS_SSL_RECORD_SIZE_LIMIT */
1713
XiaokangQian7807f9f2022-02-15 10:04:37 +00001714 default:
Jerry Yu79aa7212022-11-08 21:30:21 +08001715 MBEDTLS_SSL_PRINT_EXT(
Jerry Yu63a459c2022-10-31 13:38:40 +08001716 3, MBEDTLS_SSL_HS_CLIENT_HELLO,
Gilles Peskine449bd832023-01-11 14:50:10 +01001717 extension_type, "( ignored )");
Jerry Yu0c354a22022-08-29 15:25:36 +08001718 break;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001719 }
1720
1721 p += extension_data_len;
1722 }
1723
Gilles Peskine449bd832023-01-11 14:50:10 +01001724 MBEDTLS_SSL_PRINT_EXTS(3, MBEDTLS_SSL_HS_CLIENT_HELLO,
1725 handshake->received_extensions);
Jerry Yue18dc7e2022-08-04 16:29:22 +08001726
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +01001727 ret = mbedtls_ssl_add_hs_hdr_to_checksum(ssl,
1728 MBEDTLS_SSL_HS_CLIENT_HELLO,
1729 p - buf);
1730 if (0 != ret) {
1731 MBEDTLS_SSL_DEBUG_RET(1, ("mbedtls_ssl_add_hs_hdr_to_checksum"), ret);
1732 return ret;
1733 }
Jerry Yu032b15ce2022-07-11 06:10:03 +00001734
Ronald Cron41a443a2022-10-04 16:38:25 +02001735#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED)
XiaokangQian7807f9f2022-02-15 10:04:37 +00001736 /* Update checksum with either
1737 * - The entire content of the CH message, if no PSK extension is present
1738 * - The content up to but excluding the PSK extension, if present.
1739 */
Jerry Yu1c105562022-07-10 06:32:38 +00001740 /* If we've settled on a PSK-based exchange, parse PSK identity ext */
Pengyu Lvcfb23b82023-10-30 15:26:26 +08001741 if (ssl_tls13_check_psk_key_exchange(ssl) ||
1742 ssl_tls13_check_psk_ephemeral_key_exchange(ssl)) {
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +01001743 ret = handshake->update_checksum(ssl, buf,
1744 pre_shared_key_ext - buf);
1745 if (0 != ret) {
1746 MBEDTLS_SSL_DEBUG_RET(1, ("update_checksum"), ret);
1747 return ret;
1748 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001749 ret = ssl_tls13_parse_pre_shared_key_ext(ssl,
1750 pre_shared_key_ext,
1751 pre_shared_key_ext_end,
1752 cipher_suites,
1753 cipher_suites_end);
1754 if (ret == MBEDTLS_ERR_SSL_UNKNOWN_IDENTITY) {
1755 handshake->received_extensions &= ~MBEDTLS_SSL_EXT_MASK(PRE_SHARED_KEY);
1756 } else if (ret != 0) {
Jerry Yu63a459c2022-10-31 13:38:40 +08001757 MBEDTLS_SSL_DEBUG_RET(
Gilles Peskine449bd832023-01-11 14:50:10 +01001758 1, "ssl_tls13_parse_pre_shared_key_ext", ret);
1759 return ret;
Jerry Yu1c105562022-07-10 06:32:38 +00001760 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001761 } else
Ronald Cron41a443a2022-10-04 16:38:25 +02001762#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED */
Jerry Yu1c105562022-07-10 06:32:38 +00001763 {
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +01001764 ret = handshake->update_checksum(ssl, buf, p - buf);
1765 if (0 != ret) {
1766 MBEDTLS_SSL_DEBUG_RET(1, ("update_checksum"), ret);
1767 return ret;
1768 }
Jerry Yu1c105562022-07-10 06:32:38 +00001769 }
XiaokangQian7807f9f2022-02-15 10:04:37 +00001770
Gilles Peskine449bd832023-01-11 14:50:10 +01001771 ret = ssl_tls13_determine_key_exchange_mode(ssl);
1772 if (ret < 0) {
1773 return ret;
1774 }
XiaokangQian7807f9f2022-02-15 10:04:37 +00001775
Ronald Cron8a74f072023-06-14 17:59:29 +02001776 if (ssl->handshake->key_exchange_mode !=
1777 MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK) {
1778 hrr_required = (no_usable_share_for_key_agreement != 0);
1779 }
1780
Gilles Peskine449bd832023-01-11 14:50:10 +01001781 mbedtls_ssl_optimize_checksum(ssl, handshake->ciphersuite_info);
Jerry Yue5834fd2022-08-29 20:16:09 +08001782
Gilles Peskine449bd832023-01-11 14:50:10 +01001783 return hrr_required ? SSL_CLIENT_HELLO_HRR_REQUIRED : SSL_CLIENT_HELLO_OK;
XiaokangQian75fe8c72022-06-15 09:42:45 +00001784}
1785
1786/* Update the handshake state machine */
1787
Ronald Cronce7d76e2022-07-08 18:56:49 +02001788MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01001789static int ssl_tls13_postprocess_client_hello(mbedtls_ssl_context *ssl)
XiaokangQian75fe8c72022-06-15 09:42:45 +00001790{
1791 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1792
XiaokangQian7807f9f2022-02-15 10:04:37 +00001793 /*
XiaokangQianfb665a82022-06-15 03:57:21 +00001794 * Server certificate selection
1795 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001796 if (ssl->conf->f_cert_cb && (ret = ssl->conf->f_cert_cb(ssl)) != 0) {
1797 MBEDTLS_SSL_DEBUG_RET(1, "f_cert_cb", ret);
1798 return ret;
XiaokangQianfb665a82022-06-15 03:57:21 +00001799 }
1800#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
1801 ssl->handshake->sni_name = NULL;
1802 ssl->handshake->sni_name_len = 0;
1803#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
XiaokangQian7807f9f2022-02-15 10:04:37 +00001804
Gilles Peskine449bd832023-01-11 14:50:10 +01001805 ret = mbedtls_ssl_tls13_key_schedule_stage_early(ssl);
1806 if (ret != 0) {
1807 MBEDTLS_SSL_DEBUG_RET(1,
1808 "mbedtls_ssl_tls1_3_key_schedule_stage_early", ret);
1809 return ret;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001810 }
1811
Gilles Peskine449bd832023-01-11 14:50:10 +01001812 return 0;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001813
1814}
1815
1816/*
XiaokangQianed582dd2022-04-13 08:21:05 +00001817 * Main entry point from the state machine; orchestrates the otherfunctions.
1818 */
1819
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001820MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01001821static int ssl_tls13_process_client_hello(mbedtls_ssl_context *ssl)
XiaokangQianed582dd2022-04-13 08:21:05 +00001822{
1823
XiaokangQian08037552022-04-20 07:16:41 +00001824 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine449bd832023-01-11 14:50:10 +01001825 unsigned char *buf = NULL;
XiaokangQianed582dd2022-04-13 08:21:05 +00001826 size_t buflen = 0;
Jerry Yu4ca91402022-05-09 15:50:57 +08001827 int parse_client_hello_ret;
1828
Gilles Peskine449bd832023-01-11 14:50:10 +01001829 MBEDTLS_SSL_DEBUG_MSG(2, ("=> parse client hello"));
XiaokangQianed582dd2022-04-13 08:21:05 +00001830
Gilles Peskine449bd832023-01-11 14:50:10 +01001831 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_tls13_fetch_handshake_msg(
1832 ssl, MBEDTLS_SSL_HS_CLIENT_HELLO,
1833 &buf, &buflen));
XiaokangQianed582dd2022-04-13 08:21:05 +00001834
Gilles Peskine449bd832023-01-11 14:50:10 +01001835 MBEDTLS_SSL_PROC_CHK_NEG(ssl_tls13_parse_client_hello(ssl, buf,
1836 buf + buflen));
Ronald Cron5af4c7f2023-03-07 20:46:59 +01001837 parse_client_hello_ret = ret; /* Store positive return value of
1838 * parse_client_hello,
1839 * as negative error codes are handled
Jerry Yuf41553b2022-05-09 22:20:30 +08001840 * by MBEDTLS_SSL_PROC_CHK_NEG. */
Jerry Yu4ca91402022-05-09 15:50:57 +08001841
Ronald Cronb828c7d2023-04-03 16:37:22 +02001842 /*
1843 * Version 1.2 of the protocol has been chosen, set the
1844 * ssl->keep_current_message flag for the ClientHello to be kept and parsed
1845 * as a TLS 1.2 ClientHello. We also change ssl->tls_version to
1846 * MBEDTLS_SSL_VERSION_TLS1_2 thus from now on mbedtls_ssl_handshake_step()
1847 * will dispatch to the TLS 1.2 state machine.
1848 */
Ronald Cron5af4c7f2023-03-07 20:46:59 +01001849 if (SSL_CLIENT_HELLO_TLS1_2 == parse_client_hello_ret) {
1850 ssl->keep_current_message = 1;
1851 ssl->tls_version = MBEDTLS_SSL_VERSION_TLS1_2;
1852 return 0;
1853 }
1854
Gilles Peskine449bd832023-01-11 14:50:10 +01001855 MBEDTLS_SSL_PROC_CHK(ssl_tls13_postprocess_client_hello(ssl));
Jerry Yu582dd062022-04-22 21:59:01 +08001856
Ronald Cron5af4c7f2023-03-07 20:46:59 +01001857 if (SSL_CLIENT_HELLO_OK == parse_client_hello_ret) {
Gilles Peskine449bd832023-01-11 14:50:10 +01001858 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_SERVER_HELLO);
1859 } else {
1860 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_HELLO_RETRY_REQUEST);
1861 }
XiaokangQianed582dd2022-04-13 08:21:05 +00001862
1863cleanup:
1864
Gilles Peskine449bd832023-01-11 14:50:10 +01001865 MBEDTLS_SSL_DEBUG_MSG(2, ("<= parse client hello"));
1866 return ret;
XiaokangQianed582dd2022-04-13 08:21:05 +00001867}
1868
1869/*
Jerry Yu1c3e6882022-04-20 21:23:40 +08001870 * Handler for MBEDTLS_SSL_SERVER_HELLO
Jerry Yu5b64ae92022-03-30 17:15:02 +08001871 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001872MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01001873static int ssl_tls13_prepare_server_hello(mbedtls_ssl_context *ssl)
Jerry Yuf4b27e42022-03-30 17:32:21 +08001874{
Jerry Yu637a3f12022-04-20 21:37:58 +08001875 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1876 unsigned char *server_randbytes =
Gilles Peskine449bd832023-01-11 14:50:10 +01001877 ssl->handshake->randbytes + MBEDTLS_CLIENT_HELLO_RANDOM_LEN;
1878 if (ssl->conf->f_rng == NULL) {
1879 MBEDTLS_SSL_DEBUG_MSG(1, ("no RNG provided"));
1880 return MBEDTLS_ERR_SSL_NO_RNG;
Jerry Yuf4b27e42022-03-30 17:32:21 +08001881 }
1882
Gilles Peskine449bd832023-01-11 14:50:10 +01001883 if ((ret = ssl->conf->f_rng(ssl->conf->p_rng, server_randbytes,
1884 MBEDTLS_SERVER_HELLO_RANDOM_LEN)) != 0) {
1885 MBEDTLS_SSL_DEBUG_RET(1, "f_rng", ret);
1886 return ret;
Jerry Yuf4b27e42022-03-30 17:32:21 +08001887 }
1888
Gilles Peskine449bd832023-01-11 14:50:10 +01001889 MBEDTLS_SSL_DEBUG_BUF(3, "server hello, random bytes", server_randbytes,
1890 MBEDTLS_SERVER_HELLO_RANDOM_LEN);
Jerry Yuf4b27e42022-03-30 17:32:21 +08001891
1892#if defined(MBEDTLS_HAVE_TIME)
YxCda609132023-05-22 12:08:12 -07001893 ssl->session_negotiate->start = mbedtls_time(NULL);
Jerry Yuf4b27e42022-03-30 17:32:21 +08001894#endif /* MBEDTLS_HAVE_TIME */
1895
Gilles Peskine449bd832023-01-11 14:50:10 +01001896 return ret;
Jerry Yuf4b27e42022-03-30 17:32:21 +08001897}
1898
Jerry Yu3bf2c642022-03-30 22:02:12 +08001899/*
Jerry Yue74e04a2022-04-21 09:23:16 +08001900 * ssl_tls13_write_server_hello_supported_versions_ext ():
Jerry Yu3bf2c642022-03-30 22:02:12 +08001901 *
1902 * struct {
Jerry Yufb9f54d2022-04-06 10:08:34 +08001903 * ProtocolVersion selected_version;
Jerry Yu3bf2c642022-03-30 22:02:12 +08001904 * } SupportedVersions;
1905 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001906MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yue74e04a2022-04-21 09:23:16 +08001907static int ssl_tls13_write_server_hello_supported_versions_ext(
Gilles Peskine449bd832023-01-11 14:50:10 +01001908 mbedtls_ssl_context *ssl,
1909 unsigned char *buf,
1910 unsigned char *end,
1911 size_t *out_len)
Jerry Yu3bf2c642022-03-30 22:02:12 +08001912{
Jerry Yu3bf2c642022-03-30 22:02:12 +08001913 *out_len = 0;
1914
Gilles Peskine449bd832023-01-11 14:50:10 +01001915 MBEDTLS_SSL_DEBUG_MSG(3, ("server hello, write selected version"));
Jerry Yu3bf2c642022-03-30 22:02:12 +08001916
1917 /* Check if we have space to write the extension:
1918 * - extension_type (2 bytes)
1919 * - extension_data_length (2 bytes)
Jerry Yu349a6132022-04-14 20:52:56 +08001920 * - selected_version (2 bytes)
Jerry Yu3bf2c642022-03-30 22:02:12 +08001921 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001922 MBEDTLS_SSL_CHK_BUF_PTR(buf, end, 6);
Jerry Yu3bf2c642022-03-30 22:02:12 +08001923
Gilles Peskine449bd832023-01-11 14:50:10 +01001924 MBEDTLS_PUT_UINT16_BE(MBEDTLS_TLS_EXT_SUPPORTED_VERSIONS, buf, 0);
Jerry Yu3bf2c642022-03-30 22:02:12 +08001925
Gilles Peskine449bd832023-01-11 14:50:10 +01001926 MBEDTLS_PUT_UINT16_BE(2, buf, 2);
Jerry Yu3bf2c642022-03-30 22:02:12 +08001927
Gilles Peskine449bd832023-01-11 14:50:10 +01001928 mbedtls_ssl_write_version(buf + 4,
1929 ssl->conf->transport,
1930 ssl->tls_version);
Jerry Yu3bf2c642022-03-30 22:02:12 +08001931
Gilles Peskine449bd832023-01-11 14:50:10 +01001932 MBEDTLS_SSL_DEBUG_MSG(3, ("supported version: [%04x]",
1933 ssl->tls_version));
Jerry Yu3bf2c642022-03-30 22:02:12 +08001934
Jerry Yu349a6132022-04-14 20:52:56 +08001935 *out_len = 6;
Jerry Yu3bf2c642022-03-30 22:02:12 +08001936
Jerry Yub95dd362022-11-08 21:19:34 +08001937 mbedtls_ssl_tls13_set_hs_sent_ext_mask(
Gilles Peskine449bd832023-01-11 14:50:10 +01001938 ssl, MBEDTLS_TLS_EXT_SUPPORTED_VERSIONS);
Jerry Yub95dd362022-11-08 21:19:34 +08001939
Gilles Peskine449bd832023-01-11 14:50:10 +01001940 return 0;
Jerry Yu3bf2c642022-03-30 22:02:12 +08001941}
1942
Jerry Yud9436a12022-04-20 22:28:09 +08001943
Jerry Yu3bf2c642022-03-30 22:02:12 +08001944
1945/* Generate and export a single key share. For hybrid KEMs, this can
1946 * be called multiple times with the different components of the hybrid. */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001947MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01001948static int ssl_tls13_generate_and_write_key_share(mbedtls_ssl_context *ssl,
1949 uint16_t named_group,
1950 unsigned char *buf,
1951 unsigned char *end,
1952 size_t *out_len)
Jerry Yu3bf2c642022-03-30 22:02:12 +08001953{
1954 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yu955ddd72022-04-22 22:27:33 +08001955
1956 *out_len = 0;
1957
Valerio Settic9ae8622023-07-25 11:23:50 +02001958#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_EPHEMERAL_ENABLED)
Przemek Stekiel29c219c2023-05-31 15:21:04 +02001959 if (mbedtls_ssl_tls13_named_group_is_ecdhe(named_group) ||
Przemek Stekield5f79e72023-06-29 09:08:43 +02001960 mbedtls_ssl_tls13_named_group_is_ffdh(named_group)) {
Przemek Stekiel408569f2023-07-06 11:26:44 +02001961 ret = mbedtls_ssl_tls13_generate_and_write_xxdh_key_exchange(
Gilles Peskine449bd832023-01-11 14:50:10 +01001962 ssl, named_group, buf, end, out_len);
1963 if (ret != 0) {
Jerry Yu89e103c2022-03-30 22:43:29 +08001964 MBEDTLS_SSL_DEBUG_RET(
Przemek Stekiel408569f2023-07-06 11:26:44 +02001965 1, "mbedtls_ssl_tls13_generate_and_write_xxdh_key_exchange",
Gilles Peskine449bd832023-01-11 14:50:10 +01001966 ret);
1967 return ret;
Jerry Yu3bf2c642022-03-30 22:02:12 +08001968 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001969 } else
Valerio Settic9ae8622023-07-25 11:23:50 +02001970#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_EPHEMERAL_ENABLED */
Gilles Peskine449bd832023-01-11 14:50:10 +01001971 if (0 /* Other kinds of KEMs */) {
1972 } else {
Jerry Yu955ddd72022-04-22 22:27:33 +08001973 ((void) ssl);
1974 ((void) named_group);
1975 ((void) buf);
1976 ((void) end);
Jerry Yu3bf2c642022-03-30 22:02:12 +08001977 ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
1978 }
1979
Gilles Peskine449bd832023-01-11 14:50:10 +01001980 return ret;
Jerry Yu3bf2c642022-03-30 22:02:12 +08001981}
1982
1983/*
1984 * ssl_tls13_write_key_share_ext
1985 *
1986 * Structure of key_share extension in ServerHello:
1987 *
Jerry Yu1c3e6882022-04-20 21:23:40 +08001988 * struct {
1989 * NamedGroup group;
1990 * opaque key_exchange<1..2^16-1>;
1991 * } KeyShareEntry;
1992 * struct {
1993 * KeyShareEntry server_share;
1994 * } KeyShareServerHello;
Jerry Yu3bf2c642022-03-30 22:02:12 +08001995 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001996MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01001997static int ssl_tls13_write_key_share_ext(mbedtls_ssl_context *ssl,
1998 unsigned char *buf,
1999 unsigned char *end,
2000 size_t *out_len)
Jerry Yu3bf2c642022-03-30 22:02:12 +08002001{
Jerry Yu955ddd72022-04-22 22:27:33 +08002002 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yu3bf2c642022-03-30 22:02:12 +08002003 unsigned char *p = buf;
Jerry Yu57d48412022-04-20 21:50:42 +08002004 uint16_t group = ssl->handshake->offered_group_id;
Jerry Yu3bf2c642022-03-30 22:02:12 +08002005 unsigned char *server_share = buf + 4;
Jerry Yu3bf2c642022-03-30 22:02:12 +08002006 size_t key_exchange_length;
Jerry Yu3bf2c642022-03-30 22:02:12 +08002007
2008 *out_len = 0;
2009
Gilles Peskine449bd832023-01-11 14:50:10 +01002010 MBEDTLS_SSL_DEBUG_MSG(3, ("server hello, adding key share extension"));
Jerry Yu3bf2c642022-03-30 22:02:12 +08002011
Gilles Peskine449bd832023-01-11 14:50:10 +01002012 MBEDTLS_SSL_DEBUG_MSG(2, ("server hello, write selected_group: %s (%04x)",
2013 mbedtls_ssl_named_group_to_str(group),
2014 group));
Jerry Yu58af2332022-09-06 11:19:31 +08002015
Jerry Yu3bf2c642022-03-30 22:02:12 +08002016 /* Check if we have space for header and length fields:
2017 * - extension_type (2 bytes)
2018 * - extension_data_length (2 bytes)
2019 * - group (2 bytes)
2020 * - key_exchange_length (2 bytes)
2021 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002022 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 8);
2023 MBEDTLS_PUT_UINT16_BE(MBEDTLS_TLS_EXT_KEY_SHARE, p, 0);
2024 MBEDTLS_PUT_UINT16_BE(group, server_share, 0);
Jerry Yu3bf2c642022-03-30 22:02:12 +08002025 p += 8;
Jerry Yu57d48412022-04-20 21:50:42 +08002026
Jerry Yu3bf2c642022-03-30 22:02:12 +08002027 /* When we introduce PQC-ECDHE hybrids, we'll want to call this
2028 * function multiple times. */
Jerry Yu955ddd72022-04-22 22:27:33 +08002029 ret = ssl_tls13_generate_and_write_key_share(
Gilles Peskine449bd832023-01-11 14:50:10 +01002030 ssl, group, server_share + 4, end, &key_exchange_length);
2031 if (ret != 0) {
2032 return ret;
2033 }
Jerry Yu3bf2c642022-03-30 22:02:12 +08002034 p += key_exchange_length;
Jerry Yuc1be19f2022-04-23 16:11:39 +08002035
Gilles Peskine449bd832023-01-11 14:50:10 +01002036 MBEDTLS_PUT_UINT16_BE(key_exchange_length, server_share + 2, 0);
Jerry Yu3bf2c642022-03-30 22:02:12 +08002037
Gilles Peskine449bd832023-01-11 14:50:10 +01002038 MBEDTLS_PUT_UINT16_BE(p - server_share, buf, 2);
Jerry Yu3bf2c642022-03-30 22:02:12 +08002039
Jerry Yu57d48412022-04-20 21:50:42 +08002040 *out_len = p - buf;
Jerry Yu955ddd72022-04-22 22:27:33 +08002041
Gilles Peskine449bd832023-01-11 14:50:10 +01002042 mbedtls_ssl_tls13_set_hs_sent_ext_mask(ssl, MBEDTLS_TLS_EXT_KEY_SHARE);
Jerry Yub95dd362022-11-08 21:19:34 +08002043
Gilles Peskine449bd832023-01-11 14:50:10 +01002044 return 0;
Jerry Yu3bf2c642022-03-30 22:02:12 +08002045}
Jerry Yud9436a12022-04-20 22:28:09 +08002046
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002047MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002048static int ssl_tls13_write_hrr_key_share_ext(mbedtls_ssl_context *ssl,
2049 unsigned char *buf,
2050 unsigned char *end,
2051 size_t *out_len)
Jerry Yu23f7a6f2022-04-23 15:16:45 +08002052{
2053 uint16_t selected_group = ssl->handshake->hrr_selected_group;
2054 /* key_share Extension
2055 *
2056 * struct {
2057 * select (Handshake.msg_type) {
2058 * ...
2059 * case hello_retry_request:
2060 * NamedGroup selected_group;
2061 * ...
2062 * };
2063 * } KeyShare;
2064 */
2065
2066 *out_len = 0;
2067
Jerry Yub0ac10b2022-05-05 11:10:08 +08002068 /*
2069 * For a pure PSK key exchange, there is no group to agree upon. The purpose
2070 * of the HRR is then to transmit a cookie to force the client to demonstrate
2071 * reachability at their apparent network address (primarily useful for DTLS).
2072 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002073 if (!mbedtls_ssl_tls13_key_exchange_mode_with_ephemeral(ssl)) {
2074 return 0;
2075 }
Jerry Yu23f7a6f2022-04-23 15:16:45 +08002076
2077 /* We should only send the key_share extension if the client's initial
2078 * key share was not acceptable. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002079 if (ssl->handshake->offered_group_id != 0) {
2080 MBEDTLS_SSL_DEBUG_MSG(4, ("Skip key_share extension in HRR"));
2081 return 0;
Jerry Yu23f7a6f2022-04-23 15:16:45 +08002082 }
2083
Gilles Peskine449bd832023-01-11 14:50:10 +01002084 if (selected_group == 0) {
2085 MBEDTLS_SSL_DEBUG_MSG(1, ("no matching named group found"));
2086 return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
Jerry Yu23f7a6f2022-04-23 15:16:45 +08002087 }
2088
Jerry Yub0ac10b2022-05-05 11:10:08 +08002089 /* Check if we have enough space:
2090 * - extension_type (2 bytes)
2091 * - extension_data_length (2 bytes)
2092 * - selected_group (2 bytes)
2093 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002094 MBEDTLS_SSL_CHK_BUF_PTR(buf, end, 6);
Jerry Yu23f7a6f2022-04-23 15:16:45 +08002095
Gilles Peskine449bd832023-01-11 14:50:10 +01002096 MBEDTLS_PUT_UINT16_BE(MBEDTLS_TLS_EXT_KEY_SHARE, buf, 0);
2097 MBEDTLS_PUT_UINT16_BE(2, buf, 2);
2098 MBEDTLS_PUT_UINT16_BE(selected_group, buf, 4);
Jerry Yu23f7a6f2022-04-23 15:16:45 +08002099
Gilles Peskine449bd832023-01-11 14:50:10 +01002100 MBEDTLS_SSL_DEBUG_MSG(3,
2101 ("HRR selected_group: %s (%x)",
2102 mbedtls_ssl_named_group_to_str(selected_group),
2103 selected_group));
Jerry Yu23f7a6f2022-04-23 15:16:45 +08002104
2105 *out_len = 6;
Jerry Yu23f7a6f2022-04-23 15:16:45 +08002106
Gilles Peskine449bd832023-01-11 14:50:10 +01002107 mbedtls_ssl_tls13_set_hs_sent_ext_mask(ssl, MBEDTLS_TLS_EXT_KEY_SHARE);
Jerry Yub95dd362022-11-08 21:19:34 +08002108
Gilles Peskine449bd832023-01-11 14:50:10 +01002109 return 0;
Jerry Yu23f7a6f2022-04-23 15:16:45 +08002110}
Jerry Yu3bf2c642022-03-30 22:02:12 +08002111
2112/*
2113 * Structure of ServerHello message:
2114 *
2115 * struct {
2116 * ProtocolVersion legacy_version = 0x0303; // TLS v1.2
2117 * Random random;
2118 * opaque legacy_session_id_echo<0..32>;
2119 * CipherSuite cipher_suite;
2120 * uint8 legacy_compression_method = 0;
2121 * Extension extensions<6..2^16-1>;
2122 * } ServerHello;
2123 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002124MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002125static int ssl_tls13_write_server_hello_body(mbedtls_ssl_context *ssl,
2126 unsigned char *buf,
2127 unsigned char *end,
2128 size_t *out_len,
2129 int is_hrr)
Jerry Yu56404d72022-03-30 17:36:13 +08002130{
Jerry Yu955ddd72022-04-22 22:27:33 +08002131 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yu3bf2c642022-03-30 22:02:12 +08002132 unsigned char *p = buf;
Jerry Yud9436a12022-04-20 22:28:09 +08002133 unsigned char *p_extensions_len;
Jerry Yufbe3e642022-04-25 19:31:51 +08002134 size_t output_len;
Jerry Yu3bf2c642022-03-30 22:02:12 +08002135
2136 *out_len = 0;
Jerry Yu50e00e32022-10-31 14:45:01 +08002137 ssl->handshake->sent_extensions = MBEDTLS_SSL_EXT_MASK_NONE;
Jerry Yu3bf2c642022-03-30 22:02:12 +08002138
Jerry Yucfc04b32022-04-21 09:31:58 +08002139 /* ...
2140 * ProtocolVersion legacy_version = 0x0303; // TLS 1.2
2141 * ...
2142 * with ProtocolVersion defined as:
2143 * uint16 ProtocolVersion;
Jerry Yu3bf2c642022-03-30 22:02:12 +08002144 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002145 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 2);
2146 MBEDTLS_PUT_UINT16_BE(0x0303, p, 0);
Jerry Yu3bf2c642022-03-30 22:02:12 +08002147 p += 2;
2148
Jerry Yu1c3e6882022-04-20 21:23:40 +08002149 /* ...
2150 * Random random;
2151 * ...
Jerry Yucfc04b32022-04-21 09:31:58 +08002152 * with Random defined as:
2153 * opaque Random[MBEDTLS_SERVER_HELLO_RANDOM_LEN];
Jerry Yu1c3e6882022-04-20 21:23:40 +08002154 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002155 MBEDTLS_SSL_CHK_BUF_PTR(p, end, MBEDTLS_SERVER_HELLO_RANDOM_LEN);
2156 if (is_hrr) {
2157 memcpy(p, mbedtls_ssl_tls13_hello_retry_request_magic,
2158 MBEDTLS_SERVER_HELLO_RANDOM_LEN);
2159 } else {
2160 memcpy(p, &ssl->handshake->randbytes[MBEDTLS_CLIENT_HELLO_RANDOM_LEN],
2161 MBEDTLS_SERVER_HELLO_RANDOM_LEN);
Jerry Yu23f7a6f2022-04-23 15:16:45 +08002162 }
Gilles Peskine449bd832023-01-11 14:50:10 +01002163 MBEDTLS_SSL_DEBUG_BUF(3, "server hello, random bytes",
2164 p, MBEDTLS_SERVER_HELLO_RANDOM_LEN);
Jerry Yu3bf2c642022-03-30 22:02:12 +08002165 p += MBEDTLS_SERVER_HELLO_RANDOM_LEN;
2166
Jerry Yucfc04b32022-04-21 09:31:58 +08002167 /* ...
2168 * opaque legacy_session_id_echo<0..32>;
2169 * ...
Jerry Yu3bf2c642022-03-30 22:02:12 +08002170 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002171 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 1 + ssl->session_negotiate->id_len);
2172 *p++ = (unsigned char) ssl->session_negotiate->id_len;
2173 if (ssl->session_negotiate->id_len > 0) {
2174 memcpy(p, &ssl->session_negotiate->id[0],
2175 ssl->session_negotiate->id_len);
Jerry Yu3bf2c642022-03-30 22:02:12 +08002176 p += ssl->session_negotiate->id_len;
Jerry Yu955ddd72022-04-22 22:27:33 +08002177
Gilles Peskine449bd832023-01-11 14:50:10 +01002178 MBEDTLS_SSL_DEBUG_BUF(3, "session id", ssl->session_negotiate->id,
2179 ssl->session_negotiate->id_len);
Jerry Yu3bf2c642022-03-30 22:02:12 +08002180 }
2181
Jerry Yucfc04b32022-04-21 09:31:58 +08002182 /* ...
2183 * CipherSuite cipher_suite;
2184 * ...
2185 * with CipherSuite defined as:
2186 * uint8 CipherSuite[2];
Jerry Yu3bf2c642022-03-30 22:02:12 +08002187 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002188 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 2);
2189 MBEDTLS_PUT_UINT16_BE(ssl->session_negotiate->ciphersuite, p, 0);
Jerry Yu3bf2c642022-03-30 22:02:12 +08002190 p += 2;
Gilles Peskine449bd832023-01-11 14:50:10 +01002191 MBEDTLS_SSL_DEBUG_MSG(3,
2192 ("server hello, chosen ciphersuite: %s ( id=%d )",
2193 mbedtls_ssl_get_ciphersuite_name(
2194 ssl->session_negotiate->ciphersuite),
2195 ssl->session_negotiate->ciphersuite));
Jerry Yu3bf2c642022-03-30 22:02:12 +08002196
Jerry Yucfc04b32022-04-21 09:31:58 +08002197 /* ...
2198 * uint8 legacy_compression_method = 0;
2199 * ...
2200 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002201 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 1);
Thomas Daubney31e03a82022-07-25 15:59:25 +01002202 *p++ = MBEDTLS_SSL_COMPRESS_NULL;
Jerry Yu3bf2c642022-03-30 22:02:12 +08002203
Jerry Yucfc04b32022-04-21 09:31:58 +08002204 /* ...
2205 * Extension extensions<6..2^16-1>;
2206 * ...
2207 * struct {
2208 * ExtensionType extension_type; (2 bytes)
2209 * opaque extension_data<0..2^16-1>;
2210 * } Extension;
2211 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002212 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 2);
Jerry Yud9436a12022-04-20 22:28:09 +08002213 p_extensions_len = p;
Jerry Yu3bf2c642022-03-30 22:02:12 +08002214 p += 2;
2215
Gilles Peskine449bd832023-01-11 14:50:10 +01002216 if ((ret = ssl_tls13_write_server_hello_supported_versions_ext(
2217 ssl, p, end, &output_len)) != 0) {
Jerry Yu955ddd72022-04-22 22:27:33 +08002218 MBEDTLS_SSL_DEBUG_RET(
Gilles Peskine449bd832023-01-11 14:50:10 +01002219 1, "ssl_tls13_write_server_hello_supported_versions_ext", ret);
2220 return ret;
Jerry Yu3bf2c642022-03-30 22:02:12 +08002221 }
2222 p += output_len;
2223
Gilles Peskine449bd832023-01-11 14:50:10 +01002224 if (mbedtls_ssl_tls13_key_exchange_mode_with_ephemeral(ssl)) {
2225 if (is_hrr) {
2226 ret = ssl_tls13_write_hrr_key_share_ext(ssl, p, end, &output_len);
2227 } else {
2228 ret = ssl_tls13_write_key_share_ext(ssl, p, end, &output_len);
2229 }
2230 if (ret != 0) {
2231 return ret;
2232 }
Jerry Yu3bf2c642022-03-30 22:02:12 +08002233 p += output_len;
2234 }
Jerry Yu3bf2c642022-03-30 22:02:12 +08002235
Ronald Cron41a443a2022-10-04 16:38:25 +02002236#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01002237 if (!is_hrr && mbedtls_ssl_tls13_key_exchange_mode_with_psk(ssl)) {
2238 ret = ssl_tls13_write_server_pre_shared_key_ext(ssl, p, end, &output_len);
2239 if (ret != 0) {
2240 MBEDTLS_SSL_DEBUG_RET(1, "ssl_tls13_write_server_pre_shared_key_ext",
2241 ret);
2242 return ret;
Jerry Yu032b15ce2022-07-11 06:10:03 +00002243 }
2244 p += output_len;
2245 }
Ronald Cron41a443a2022-10-04 16:38:25 +02002246#endif
Jerry Yu032b15ce2022-07-11 06:10:03 +00002247
Gilles Peskine449bd832023-01-11 14:50:10 +01002248 MBEDTLS_PUT_UINT16_BE(p - p_extensions_len - 2, p_extensions_len, 0);
Jerry Yu3bf2c642022-03-30 22:02:12 +08002249
Gilles Peskine449bd832023-01-11 14:50:10 +01002250 MBEDTLS_SSL_DEBUG_BUF(4, "server hello extensions",
2251 p_extensions_len, p - p_extensions_len);
Jerry Yu3bf2c642022-03-30 22:02:12 +08002252
Jerry Yud9436a12022-04-20 22:28:09 +08002253 *out_len = p - buf;
Jerry Yu3bf2c642022-03-30 22:02:12 +08002254
Gilles Peskine449bd832023-01-11 14:50:10 +01002255 MBEDTLS_SSL_DEBUG_BUF(3, "server hello", buf, *out_len);
Jerry Yu3bf2c642022-03-30 22:02:12 +08002256
Jerry Yu7de2ff02022-11-08 21:43:46 +08002257 MBEDTLS_SSL_PRINT_EXTS(
Jerry Yu4b8f2f72022-10-31 13:31:22 +08002258 3, is_hrr ? MBEDTLS_SSL_TLS1_3_HS_HELLO_RETRY_REQUEST :
Gilles Peskine449bd832023-01-11 14:50:10 +01002259 MBEDTLS_SSL_HS_SERVER_HELLO,
2260 ssl->handshake->sent_extensions);
Jerry Yu4b8f2f72022-10-31 13:31:22 +08002261
Gilles Peskine449bd832023-01-11 14:50:10 +01002262 return ret;
Jerry Yu56404d72022-03-30 17:36:13 +08002263}
2264
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002265MBEDTLS_CHECK_RETURN_CRITICAL
Xiaokang Qian934ce6f2023-02-06 10:23:04 +00002266static int ssl_tls13_finalize_server_hello(mbedtls_ssl_context *ssl)
Jerry Yue110d252022-05-05 10:19:22 +08002267{
2268 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine449bd832023-01-11 14:50:10 +01002269 ret = mbedtls_ssl_tls13_compute_handshake_transform(ssl);
2270 if (ret != 0) {
2271 MBEDTLS_SSL_DEBUG_RET(1,
2272 "mbedtls_ssl_tls13_compute_handshake_transform",
2273 ret);
2274 return ret;
Jerry Yue110d252022-05-05 10:19:22 +08002275 }
2276
Gilles Peskine449bd832023-01-11 14:50:10 +01002277 return ret;
Jerry Yue110d252022-05-05 10:19:22 +08002278}
2279
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002280MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002281static int ssl_tls13_write_server_hello(mbedtls_ssl_context *ssl)
Jerry Yu5b64ae92022-03-30 17:15:02 +08002282{
Jerry Yu637a3f12022-04-20 21:37:58 +08002283 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yuf4b27e42022-03-30 17:32:21 +08002284 unsigned char *buf;
2285 size_t buf_len, msg_len;
2286
Gilles Peskine449bd832023-01-11 14:50:10 +01002287 MBEDTLS_SSL_DEBUG_MSG(2, ("=> write server hello"));
Jerry Yuf4b27e42022-03-30 17:32:21 +08002288
Gilles Peskine449bd832023-01-11 14:50:10 +01002289 MBEDTLS_SSL_PROC_CHK(ssl_tls13_prepare_server_hello(ssl));
Jerry Yuf4b27e42022-03-30 17:32:21 +08002290
Xiaokang Qian9f1747b2023-03-30 02:50:04 +00002291 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_start_handshake_msg(
2292 ssl, MBEDTLS_SSL_HS_SERVER_HELLO, &buf, &buf_len));
Jerry Yuf4b27e42022-03-30 17:32:21 +08002293
Gilles Peskine449bd832023-01-11 14:50:10 +01002294 MBEDTLS_SSL_PROC_CHK(ssl_tls13_write_server_hello_body(ssl, buf,
2295 buf + buf_len,
2296 &msg_len,
2297 0));
Jerry Yuf4b27e42022-03-30 17:32:21 +08002298
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +01002299 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_add_hs_msg_to_checksum(
Manuel Pégourié-Gonnard43cc1272023-02-06 11:48:19 +01002300 ssl, MBEDTLS_SSL_HS_SERVER_HELLO, buf, msg_len));
Jerry Yuf4b27e42022-03-30 17:32:21 +08002301
Gilles Peskine449bd832023-01-11 14:50:10 +01002302 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_finish_handshake_msg(
2303 ssl, buf_len, msg_len));
Jerry Yu637a3f12022-04-20 21:37:58 +08002304
Xiaokang Qian934ce6f2023-02-06 10:23:04 +00002305 MBEDTLS_SSL_PROC_CHK(ssl_tls13_finalize_server_hello(ssl));
Jerry Yue110d252022-05-05 10:19:22 +08002306
Gabor Mezei7b39bf12022-05-24 16:04:14 +02002307#if defined(MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE)
2308 /* The server sends a dummy change_cipher_spec record immediately
2309 * after its first handshake message. This may either be after
2310 * a ServerHello or a HelloRetryRequest.
2311 */
Gabor Mezei96ae9262022-06-28 11:45:18 +02002312 mbedtls_ssl_handshake_set_state(
Gilles Peskine449bd832023-01-11 14:50:10 +01002313 ssl, MBEDTLS_SSL_SERVER_CCS_AFTER_SERVER_HELLO);
Gabor Mezei7b39bf12022-05-24 16:04:14 +02002314#else
Gilles Peskine449bd832023-01-11 14:50:10 +01002315 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_ENCRYPTED_EXTENSIONS);
Gabor Mezei7b39bf12022-05-24 16:04:14 +02002316#endif /* MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE */
Jerry Yue110d252022-05-05 10:19:22 +08002317
Jerry Yuf4b27e42022-03-30 17:32:21 +08002318cleanup:
2319
Gilles Peskine449bd832023-01-11 14:50:10 +01002320 MBEDTLS_SSL_DEBUG_MSG(2, ("<= write server hello"));
2321 return ret;
Jerry Yu5b64ae92022-03-30 17:15:02 +08002322}
2323
Jerry Yu23d1a252022-05-12 18:08:59 +08002324
2325/*
2326 * Handler for MBEDTLS_SSL_HELLO_RETRY_REQUEST
2327 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002328MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002329static int ssl_tls13_prepare_hello_retry_request(mbedtls_ssl_context *ssl)
Jerry Yu23d1a252022-05-12 18:08:59 +08002330{
2331 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine449bd832023-01-11 14:50:10 +01002332 if (ssl->handshake->hello_retry_request_count > 0) {
2333 MBEDTLS_SSL_DEBUG_MSG(1, ("Too many HRRs"));
2334 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE,
2335 MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE);
2336 return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
Jerry Yu23d1a252022-05-12 18:08:59 +08002337 }
2338
2339 /*
2340 * Create stateless transcript hash for HRR
2341 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002342 MBEDTLS_SSL_DEBUG_MSG(4, ("Reset transcript for HRR"));
2343 ret = mbedtls_ssl_reset_transcript_for_hrr(ssl);
2344 if (ret != 0) {
2345 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_reset_transcript_for_hrr", ret);
2346 return ret;
Jerry Yu23d1a252022-05-12 18:08:59 +08002347 }
Gilles Peskine449bd832023-01-11 14:50:10 +01002348 mbedtls_ssl_session_reset_msg_layer(ssl, 0);
Jerry Yu23d1a252022-05-12 18:08:59 +08002349
Gilles Peskine449bd832023-01-11 14:50:10 +01002350 return 0;
Jerry Yu23d1a252022-05-12 18:08:59 +08002351}
2352
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002353MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002354static int ssl_tls13_write_hello_retry_request(mbedtls_ssl_context *ssl)
Jerry Yu23d1a252022-05-12 18:08:59 +08002355{
2356 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
2357 unsigned char *buf;
2358 size_t buf_len, msg_len;
2359
Gilles Peskine449bd832023-01-11 14:50:10 +01002360 MBEDTLS_SSL_DEBUG_MSG(2, ("=> write hello retry request"));
Jerry Yu23d1a252022-05-12 18:08:59 +08002361
Gilles Peskine449bd832023-01-11 14:50:10 +01002362 MBEDTLS_SSL_PROC_CHK(ssl_tls13_prepare_hello_retry_request(ssl));
Jerry Yu23d1a252022-05-12 18:08:59 +08002363
Gilles Peskine449bd832023-01-11 14:50:10 +01002364 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_start_handshake_msg(
2365 ssl, MBEDTLS_SSL_HS_SERVER_HELLO,
2366 &buf, &buf_len));
Jerry Yu23d1a252022-05-12 18:08:59 +08002367
Gilles Peskine449bd832023-01-11 14:50:10 +01002368 MBEDTLS_SSL_PROC_CHK(ssl_tls13_write_server_hello_body(ssl, buf,
2369 buf + buf_len,
2370 &msg_len,
2371 1));
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +01002372 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_add_hs_msg_to_checksum(
Manuel Pégourié-Gonnard43cc1272023-02-06 11:48:19 +01002373 ssl, MBEDTLS_SSL_HS_SERVER_HELLO, buf, msg_len));
Jerry Yu23d1a252022-05-12 18:08:59 +08002374
2375
Gilles Peskine449bd832023-01-11 14:50:10 +01002376 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_finish_handshake_msg(ssl, buf_len,
2377 msg_len));
Jerry Yu23d1a252022-05-12 18:08:59 +08002378
2379 ssl->handshake->hello_retry_request_count++;
2380
Gabor Mezei7b39bf12022-05-24 16:04:14 +02002381#if defined(MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE)
2382 /* The server sends a dummy change_cipher_spec record immediately
2383 * after its first handshake message. This may either be after
2384 * a ServerHello or a HelloRetryRequest.
2385 */
Gabor Mezei96ae9262022-06-28 11:45:18 +02002386 mbedtls_ssl_handshake_set_state(
Gilles Peskine449bd832023-01-11 14:50:10 +01002387 ssl, MBEDTLS_SSL_SERVER_CCS_AFTER_HELLO_RETRY_REQUEST);
Gabor Mezei7b39bf12022-05-24 16:04:14 +02002388#else
Gilles Peskine449bd832023-01-11 14:50:10 +01002389 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_CLIENT_HELLO);
Gabor Mezei7b39bf12022-05-24 16:04:14 +02002390#endif /* MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE */
Jerry Yu23d1a252022-05-12 18:08:59 +08002391
2392cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +01002393 MBEDTLS_SSL_DEBUG_MSG(2, ("<= write hello retry request"));
2394 return ret;
Jerry Yu23d1a252022-05-12 18:08:59 +08002395}
2396
Jerry Yu5b64ae92022-03-30 17:15:02 +08002397/*
Jerry Yu4d3841a2022-04-16 12:37:19 +08002398 * Handler for MBEDTLS_SSL_ENCRYPTED_EXTENSIONS
2399 */
Jerry Yu4d3841a2022-04-16 12:37:19 +08002400
2401/*
2402 * struct {
2403 * Extension extensions<0..2 ^ 16 - 1>;
2404 * } EncryptedExtensions;
2405 *
2406 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002407MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002408static int ssl_tls13_write_encrypted_extensions_body(mbedtls_ssl_context *ssl,
2409 unsigned char *buf,
2410 unsigned char *end,
2411 size_t *out_len)
Jerry Yu4d3841a2022-04-16 12:37:19 +08002412{
XiaokangQianacb39922022-06-17 10:18:48 +00002413 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yu4d3841a2022-04-16 12:37:19 +08002414 unsigned char *p = buf;
2415 size_t extensions_len = 0;
Jerry Yu8937eb42022-05-03 12:12:14 +08002416 unsigned char *p_extensions_len;
XiaokangQianacb39922022-06-17 10:18:48 +00002417 size_t output_len;
Jerry Yu9da5e5a2022-05-03 15:46:09 +08002418
Jerry Yu4d3841a2022-04-16 12:37:19 +08002419 *out_len = 0;
2420
Gilles Peskine449bd832023-01-11 14:50:10 +01002421 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 2);
Jerry Yu8937eb42022-05-03 12:12:14 +08002422 p_extensions_len = p;
Jerry Yu4d3841a2022-04-16 12:37:19 +08002423 p += 2;
2424
Jerry Yu8937eb42022-05-03 12:12:14 +08002425 ((void) ssl);
XiaokangQianacb39922022-06-17 10:18:48 +00002426 ((void) ret);
2427 ((void) output_len);
2428
2429#if defined(MBEDTLS_SSL_ALPN)
Gilles Peskine449bd832023-01-11 14:50:10 +01002430 ret = mbedtls_ssl_write_alpn_ext(ssl, p, end, &output_len);
2431 if (ret != 0) {
2432 return ret;
2433 }
XiaokangQian95d5f542022-06-24 02:29:26 +00002434 p += output_len;
XiaokangQianacb39922022-06-17 10:18:48 +00002435#endif /* MBEDTLS_SSL_ALPN */
Jerry Yu4d3841a2022-04-16 12:37:19 +08002436
Gilles Peskine449bd832023-01-11 14:50:10 +01002437 extensions_len = (p - p_extensions_len) - 2;
2438 MBEDTLS_PUT_UINT16_BE(extensions_len, p_extensions_len, 0);
Jerry Yu8937eb42022-05-03 12:12:14 +08002439
2440 *out_len = p - buf;
Jerry Yu4d3841a2022-04-16 12:37:19 +08002441
Gilles Peskine449bd832023-01-11 14:50:10 +01002442 MBEDTLS_SSL_DEBUG_BUF(4, "encrypted extensions", buf, *out_len);
Jerry Yu4d3841a2022-04-16 12:37:19 +08002443
Jerry Yu7de2ff02022-11-08 21:43:46 +08002444 MBEDTLS_SSL_PRINT_EXTS(
Gilles Peskine449bd832023-01-11 14:50:10 +01002445 3, MBEDTLS_SSL_HS_ENCRYPTED_EXTENSIONS, ssl->handshake->sent_extensions);
Jerry Yu4b8f2f72022-10-31 13:31:22 +08002446
Gilles Peskine449bd832023-01-11 14:50:10 +01002447 return 0;
Jerry Yu4d3841a2022-04-16 12:37:19 +08002448}
2449
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002450MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002451static int ssl_tls13_write_encrypted_extensions(mbedtls_ssl_context *ssl)
Jerry Yu4d3841a2022-04-16 12:37:19 +08002452{
2453 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
2454 unsigned char *buf;
Jerry Yu39730a72022-05-03 12:14:04 +08002455 size_t buf_len, msg_len;
Jerry Yu4d3841a2022-04-16 12:37:19 +08002456
Gilles Peskine449bd832023-01-11 14:50:10 +01002457 mbedtls_ssl_set_outbound_transform(ssl,
2458 ssl->handshake->transform_handshake);
Gabor Mezei54719122022-06-28 11:34:56 +02002459 MBEDTLS_SSL_DEBUG_MSG(
Gilles Peskine449bd832023-01-11 14:50:10 +01002460 3, ("switching to handshake transform for outbound data"));
Gabor Mezei54719122022-06-28 11:34:56 +02002461
Gilles Peskine449bd832023-01-11 14:50:10 +01002462 MBEDTLS_SSL_DEBUG_MSG(2, ("=> write encrypted extensions"));
Jerry Yu4d3841a2022-04-16 12:37:19 +08002463
Xiaokang Qian9f1747b2023-03-30 02:50:04 +00002464 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_start_handshake_msg(
2465 ssl, MBEDTLS_SSL_HS_ENCRYPTED_EXTENSIONS,
2466 &buf, &buf_len));
Jerry Yu4d3841a2022-04-16 12:37:19 +08002467
Gilles Peskine449bd832023-01-11 14:50:10 +01002468 MBEDTLS_SSL_PROC_CHK(ssl_tls13_write_encrypted_extensions_body(
2469 ssl, buf, buf + buf_len, &msg_len));
Jerry Yu4d3841a2022-04-16 12:37:19 +08002470
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +01002471 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_add_hs_msg_to_checksum(
Xiaokang Qian9f1747b2023-03-30 02:50:04 +00002472 ssl, MBEDTLS_SSL_HS_ENCRYPTED_EXTENSIONS,
2473 buf, msg_len));
Jerry Yu4d3841a2022-04-16 12:37:19 +08002474
Gilles Peskine449bd832023-01-11 14:50:10 +01002475 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_finish_handshake_msg(
2476 ssl, buf_len, msg_len));
Jerry Yu4d3841a2022-04-16 12:37:19 +08002477
Ronald Cron928cbd32022-10-04 16:14:26 +02002478#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01002479 if (mbedtls_ssl_tls13_key_exchange_mode_with_psk(ssl)) {
2480 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_SERVER_FINISHED);
2481 } else {
2482 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_CERTIFICATE_REQUEST);
2483 }
Jerry Yu8937eb42022-05-03 12:12:14 +08002484#else
Gilles Peskine449bd832023-01-11 14:50:10 +01002485 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_SERVER_FINISHED);
Jerry Yu8937eb42022-05-03 12:12:14 +08002486#endif
2487
Jerry Yu4d3841a2022-04-16 12:37:19 +08002488cleanup:
2489
Gilles Peskine449bd832023-01-11 14:50:10 +01002490 MBEDTLS_SSL_DEBUG_MSG(2, ("<= write encrypted extensions"));
2491 return ret;
Jerry Yu4d3841a2022-04-16 12:37:19 +08002492}
2493
Ronald Cron928cbd32022-10-04 16:14:26 +02002494#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED)
XiaokangQiana987e1d2022-05-07 01:25:58 +00002495#define SSL_CERTIFICATE_REQUEST_SEND_REQUEST 0
2496#define SSL_CERTIFICATE_REQUEST_SKIP 1
2497/* Coordination:
2498 * Check whether a CertificateRequest message should be written.
2499 * Returns a negative code on failure, or
2500 * - SSL_CERTIFICATE_REQUEST_SEND_REQUEST
2501 * - SSL_CERTIFICATE_REQUEST_SKIP
2502 * indicating if the writing of the CertificateRequest
2503 * should be skipped or not.
2504 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002505MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002506static int ssl_tls13_certificate_request_coordinate(mbedtls_ssl_context *ssl)
XiaokangQiana987e1d2022-05-07 01:25:58 +00002507{
2508 int authmode;
2509
XiaokangQian40a35232022-05-07 09:02:40 +00002510#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Gilles Peskine449bd832023-01-11 14:50:10 +01002511 if (ssl->handshake->sni_authmode != MBEDTLS_SSL_VERIFY_UNSET) {
XiaokangQian40a35232022-05-07 09:02:40 +00002512 authmode = ssl->handshake->sni_authmode;
Gilles Peskine449bd832023-01-11 14:50:10 +01002513 } else
XiaokangQian40a35232022-05-07 09:02:40 +00002514#endif
XiaokangQiana987e1d2022-05-07 01:25:58 +00002515 authmode = ssl->conf->authmode;
2516
Gilles Peskine449bd832023-01-11 14:50:10 +01002517 if (authmode == MBEDTLS_SSL_VERIFY_NONE) {
Ronald Croneac00ad2022-09-13 10:16:31 +02002518 ssl->session_negotiate->verify_result = MBEDTLS_X509_BADCERT_SKIP_VERIFY;
Gilles Peskine449bd832023-01-11 14:50:10 +01002519 return SSL_CERTIFICATE_REQUEST_SKIP;
Ronald Croneac00ad2022-09-13 10:16:31 +02002520 }
XiaokangQiana987e1d2022-05-07 01:25:58 +00002521
XiaokangQianc3017f62022-05-13 05:55:41 +00002522 ssl->handshake->certificate_request_sent = 1;
XiaokangQian189ded22022-05-10 08:12:17 +00002523
Gilles Peskine449bd832023-01-11 14:50:10 +01002524 return SSL_CERTIFICATE_REQUEST_SEND_REQUEST;
XiaokangQiana987e1d2022-05-07 01:25:58 +00002525}
2526
XiaokangQiancec9ae62022-05-06 07:28:50 +00002527/*
2528 * struct {
2529 * opaque certificate_request_context<0..2^8-1>;
2530 * Extension extensions<2..2^16-1>;
2531 * } CertificateRequest;
2532 *
2533 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002534MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002535static int ssl_tls13_write_certificate_request_body(mbedtls_ssl_context *ssl,
2536 unsigned char *buf,
2537 const unsigned char *end,
2538 size_t *out_len)
XiaokangQiancec9ae62022-05-06 07:28:50 +00002539{
2540 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
2541 unsigned char *p = buf;
XiaokangQianec6efb92022-05-06 09:53:10 +00002542 size_t output_len = 0;
XiaokangQiancec9ae62022-05-06 07:28:50 +00002543 unsigned char *p_extensions_len;
2544
2545 *out_len = 0;
2546
2547 /* Check if we have enough space:
2548 * - certificate_request_context (1 byte)
2549 * - extensions length (2 bytes)
2550 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002551 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 3);
XiaokangQiancec9ae62022-05-06 07:28:50 +00002552
2553 /*
2554 * Write certificate_request_context
2555 */
2556 /*
2557 * We use a zero length context for the normal handshake
2558 * messages. For post-authentication handshake messages
2559 * this request context would be set to a non-zero value.
2560 */
2561 *p++ = 0x0;
2562
2563 /*
2564 * Write extensions
2565 */
2566 /* The extensions must contain the signature_algorithms. */
2567 p_extensions_len = p;
2568 p += 2;
Gilles Peskine449bd832023-01-11 14:50:10 +01002569 ret = mbedtls_ssl_write_sig_alg_ext(ssl, p, end, &output_len);
2570 if (ret != 0) {
2571 return ret;
2572 }
XiaokangQiancec9ae62022-05-06 07:28:50 +00002573
XiaokangQianec6efb92022-05-06 09:53:10 +00002574 p += output_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01002575 MBEDTLS_PUT_UINT16_BE(p - p_extensions_len - 2, p_extensions_len, 0);
XiaokangQiancec9ae62022-05-06 07:28:50 +00002576
2577 *out_len = p - buf;
2578
Jerry Yu7de2ff02022-11-08 21:43:46 +08002579 MBEDTLS_SSL_PRINT_EXTS(
Gilles Peskine449bd832023-01-11 14:50:10 +01002580 3, MBEDTLS_SSL_HS_CERTIFICATE_REQUEST, ssl->handshake->sent_extensions);
Jerry Yu4b8f2f72022-10-31 13:31:22 +08002581
Gilles Peskine449bd832023-01-11 14:50:10 +01002582 return 0;
XiaokangQiancec9ae62022-05-06 07:28:50 +00002583}
2584
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002585MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002586static int ssl_tls13_write_certificate_request(mbedtls_ssl_context *ssl)
XiaokangQiancec9ae62022-05-06 07:28:50 +00002587{
2588 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
2589
Gilles Peskine449bd832023-01-11 14:50:10 +01002590 MBEDTLS_SSL_DEBUG_MSG(2, ("=> write certificate request"));
XiaokangQiancec9ae62022-05-06 07:28:50 +00002591
Gilles Peskine449bd832023-01-11 14:50:10 +01002592 MBEDTLS_SSL_PROC_CHK_NEG(ssl_tls13_certificate_request_coordinate(ssl));
XiaokangQiancec9ae62022-05-06 07:28:50 +00002593
Gilles Peskine449bd832023-01-11 14:50:10 +01002594 if (ret == SSL_CERTIFICATE_REQUEST_SEND_REQUEST) {
XiaokangQiancec9ae62022-05-06 07:28:50 +00002595 unsigned char *buf;
2596 size_t buf_len, msg_len;
2597
Xiaokang Qian9f1747b2023-03-30 02:50:04 +00002598 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_start_handshake_msg(
2599 ssl, MBEDTLS_SSL_HS_CERTIFICATE_REQUEST,
2600 &buf, &buf_len));
XiaokangQiancec9ae62022-05-06 07:28:50 +00002601
Gilles Peskine449bd832023-01-11 14:50:10 +01002602 MBEDTLS_SSL_PROC_CHK(ssl_tls13_write_certificate_request_body(
2603 ssl, buf, buf + buf_len, &msg_len));
XiaokangQiancec9ae62022-05-06 07:28:50 +00002604
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +01002605 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_add_hs_msg_to_checksum(
Xiaokang Qian9f1747b2023-03-30 02:50:04 +00002606 ssl, MBEDTLS_SSL_HS_CERTIFICATE_REQUEST,
2607 buf, msg_len));
XiaokangQiancec9ae62022-05-06 07:28:50 +00002608
Gilles Peskine449bd832023-01-11 14:50:10 +01002609 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_finish_handshake_msg(
2610 ssl, buf_len, msg_len));
2611 } else if (ret == SSL_CERTIFICATE_REQUEST_SKIP) {
2612 MBEDTLS_SSL_DEBUG_MSG(2, ("<= skip write certificate request"));
XiaokangQiancec9ae62022-05-06 07:28:50 +00002613 ret = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01002614 } else {
2615 MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
XiaokangQiancec9ae62022-05-06 07:28:50 +00002616 ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
2617 goto cleanup;
2618 }
2619
Gilles Peskine449bd832023-01-11 14:50:10 +01002620 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_SERVER_CERTIFICATE);
XiaokangQiancec9ae62022-05-06 07:28:50 +00002621cleanup:
2622
Gilles Peskine449bd832023-01-11 14:50:10 +01002623 MBEDTLS_SSL_DEBUG_MSG(2, ("<= write certificate request"));
2624 return ret;
XiaokangQiancec9ae62022-05-06 07:28:50 +00002625}
XiaokangQiancec9ae62022-05-06 07:28:50 +00002626
Jerry Yu4d3841a2022-04-16 12:37:19 +08002627/*
Jerry Yuc6e6dbf2022-04-16 19:42:57 +08002628 * Handler for MBEDTLS_SSL_SERVER_CERTIFICATE
Jerry Yu83da34e2022-04-16 13:59:52 +08002629 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002630MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002631static int ssl_tls13_write_server_certificate(mbedtls_ssl_context *ssl)
Jerry Yu83da34e2022-04-16 13:59:52 +08002632{
Jerry Yu5a26f302022-05-10 20:46:40 +08002633 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
XiaokangQianfb665a82022-06-15 03:57:21 +00002634
2635#if defined(MBEDTLS_X509_CRT_PARSE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01002636 if ((ssl_tls13_pick_key_cert(ssl) != 0) ||
2637 mbedtls_ssl_own_cert(ssl) == NULL) {
2638 MBEDTLS_SSL_DEBUG_MSG(2, ("No certificate available."));
2639 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE,
2640 MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE);
2641 return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
Jerry Yu5a26f302022-05-10 20:46:40 +08002642 }
XiaokangQianfb665a82022-06-15 03:57:21 +00002643#endif /* MBEDTLS_X509_CRT_PARSE_C */
Jerry Yu5a26f302022-05-10 20:46:40 +08002644
Gilles Peskine449bd832023-01-11 14:50:10 +01002645 ret = mbedtls_ssl_tls13_write_certificate(ssl);
2646 if (ret != 0) {
2647 return ret;
2648 }
2649 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_CERTIFICATE_VERIFY);
2650 return 0;
Jerry Yu83da34e2022-04-16 13:59:52 +08002651}
2652
2653/*
Jerry Yuc6e6dbf2022-04-16 19:42:57 +08002654 * Handler for MBEDTLS_SSL_CERTIFICATE_VERIFY
Jerry Yu83da34e2022-04-16 13:59:52 +08002655 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002656MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002657static int ssl_tls13_write_certificate_verify(mbedtls_ssl_context *ssl)
Jerry Yu83da34e2022-04-16 13:59:52 +08002658{
Gilles Peskine449bd832023-01-11 14:50:10 +01002659 int ret = mbedtls_ssl_tls13_write_certificate_verify(ssl);
2660 if (ret != 0) {
2661 return ret;
2662 }
2663 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_SERVER_FINISHED);
2664 return 0;
Jerry Yu83da34e2022-04-16 13:59:52 +08002665}
Ronald Cron928cbd32022-10-04 16:14:26 +02002666#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED */
Jerry Yu83da34e2022-04-16 13:59:52 +08002667
2668/*
Jerry Yud6e253d2022-05-18 13:59:24 +08002669 * Handler for MBEDTLS_SSL_SERVER_FINISHED
Jerry Yu69dd8d42022-04-16 12:51:26 +08002670 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002671MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002672static int ssl_tls13_write_server_finished(mbedtls_ssl_context *ssl)
Jerry Yu69dd8d42022-04-16 12:51:26 +08002673{
Jerry Yud6e253d2022-05-18 13:59:24 +08002674 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yu27bdc7c2022-04-16 13:33:27 +08002675
Gilles Peskine449bd832023-01-11 14:50:10 +01002676 ret = mbedtls_ssl_tls13_write_finished_message(ssl);
2677 if (ret != 0) {
2678 return ret;
2679 }
Jerry Yu27bdc7c2022-04-16 13:33:27 +08002680
Gilles Peskine449bd832023-01-11 14:50:10 +01002681 ret = mbedtls_ssl_tls13_compute_application_transform(ssl);
2682 if (ret != 0) {
Jerry Yue3d67cb2022-05-19 15:33:10 +08002683 MBEDTLS_SSL_PEND_FATAL_ALERT(
Gilles Peskine449bd832023-01-11 14:50:10 +01002684 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE,
2685 MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE);
2686 return ret;
Jerry Yue3d67cb2022-05-19 15:33:10 +08002687 }
XiaokangQianc3017f62022-05-13 05:55:41 +00002688
Gilles Peskine449bd832023-01-11 14:50:10 +01002689 MBEDTLS_SSL_DEBUG_MSG(1, ("Switch to handshake keys for inbound traffic"));
2690 mbedtls_ssl_set_inbound_transform(ssl, ssl->handshake->transform_handshake);
XiaokangQian189ded22022-05-10 08:12:17 +00002691
Gilles Peskine449bd832023-01-11 14:50:10 +01002692 if (ssl->handshake->certificate_request_sent) {
2693 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_CLIENT_CERTIFICATE);
2694 } else {
2695 MBEDTLS_SSL_DEBUG_MSG(2, ("skip parse certificate"));
2696 MBEDTLS_SSL_DEBUG_MSG(2, ("skip parse certificate verify"));
2697 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_CLIENT_FINISHED);
Ronald Cron19385882022-06-15 16:26:13 +02002698 }
Ronald Cron63dc4632022-05-31 14:41:53 +02002699
Gilles Peskine449bd832023-01-11 14:50:10 +01002700 return 0;
Jerry Yu69dd8d42022-04-16 12:51:26 +08002701}
2702
2703/*
Jerry Yud6e253d2022-05-18 13:59:24 +08002704 * Handler for MBEDTLS_SSL_CLIENT_FINISHED
Jerry Yu69dd8d42022-04-16 12:51:26 +08002705 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002706MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002707static int ssl_tls13_process_client_finished(mbedtls_ssl_context *ssl)
Jerry Yu69dd8d42022-04-16 12:51:26 +08002708{
Jerry Yud6e253d2022-05-18 13:59:24 +08002709 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
2710
Gilles Peskine449bd832023-01-11 14:50:10 +01002711 ret = mbedtls_ssl_tls13_process_finished_message(ssl);
2712 if (ret != 0) {
2713 return ret;
Jerry Yue3d67cb2022-05-19 15:33:10 +08002714 }
2715
Gilles Peskine449bd832023-01-11 14:50:10 +01002716 ret = mbedtls_ssl_tls13_compute_resumption_master_secret(ssl);
2717 if (ret != 0) {
Xiaokang Qian9f1747b2023-03-30 02:50:04 +00002718 MBEDTLS_SSL_DEBUG_RET(
2719 1, "mbedtls_ssl_tls13_compute_resumption_master_secret", ret);
Gilles Peskine449bd832023-01-11 14:50:10 +01002720 }
2721
2722 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_HANDSHAKE_WRAPUP);
2723 return 0;
Jerry Yu69dd8d42022-04-16 12:51:26 +08002724}
2725
2726/*
Jerry Yud6e253d2022-05-18 13:59:24 +08002727 * Handler for MBEDTLS_SSL_HANDSHAKE_WRAPUP
Jerry Yu69dd8d42022-04-16 12:51:26 +08002728 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002729MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002730static int ssl_tls13_handshake_wrapup(mbedtls_ssl_context *ssl)
Jerry Yu69dd8d42022-04-16 12:51:26 +08002731{
Gilles Peskine449bd832023-01-11 14:50:10 +01002732 MBEDTLS_SSL_DEBUG_MSG(2, ("handshake: done"));
Jerry Yu03ed50b2022-04-16 17:13:30 +08002733
Gilles Peskine449bd832023-01-11 14:50:10 +01002734 mbedtls_ssl_tls13_handshake_wrapup(ssl);
Jerry Yue67bef42022-07-07 07:29:42 +00002735
Pengyu Lv3643fdb2023-01-12 16:46:28 +08002736#if defined(MBEDTLS_SSL_SESSION_TICKETS) && \
2737 defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED)
Pengyu Lva1aa31b2022-12-13 13:49:59 +08002738/* TODO: Remove the check of SOME_PSK_ENABLED since SESSION_TICKETS requires
2739 * SOME_PSK_ENABLED to be enabled. Here is just to make CI happy. It is
2740 * expected to be resolved with issue#6395.
2741 */
Pengyu Lvc7af2c42022-12-01 16:33:00 +08002742 /* Sent NewSessionTicket message only when client supports PSK */
Pengyu Lv3643fdb2023-01-12 16:46:28 +08002743 if (mbedtls_ssl_tls13_some_psk_enabled(ssl)) {
Xiaokang Qian9f1747b2023-03-30 02:50:04 +00002744 mbedtls_ssl_handshake_set_state(
2745 ssl, MBEDTLS_SSL_TLS1_3_NEW_SESSION_TICKET);
Pengyu Lvc7af2c42022-12-01 16:33:00 +08002746 } else
Jerry Yufca4d572022-07-21 10:37:48 +08002747#endif
Pengyu Lv3643fdb2023-01-12 16:46:28 +08002748 {
2749 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_HANDSHAKE_OVER);
2750 }
Gilles Peskine449bd832023-01-11 14:50:10 +01002751 return 0;
Jerry Yu69dd8d42022-04-16 12:51:26 +08002752}
2753
2754/*
Jerry Yua8d3c502022-10-30 14:51:23 +08002755 * Handler for MBEDTLS_SSL_TLS1_3_NEW_SESSION_TICKET
Jerry Yue67bef42022-07-07 07:29:42 +00002756 */
2757#define SSL_NEW_SESSION_TICKET_SKIP 0
2758#define SSL_NEW_SESSION_TICKET_WRITE 1
2759MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002760static int ssl_tls13_write_new_session_ticket_coordinate(mbedtls_ssl_context *ssl)
Jerry Yue67bef42022-07-07 07:29:42 +00002761{
2762 /* Check whether the use of session tickets is enabled */
Gilles Peskine449bd832023-01-11 14:50:10 +01002763 if (ssl->conf->f_ticket_write == NULL) {
2764 MBEDTLS_SSL_DEBUG_MSG(2, ("NewSessionTicket: disabled,"
2765 " callback is not set"));
2766 return SSL_NEW_SESSION_TICKET_SKIP;
Jerry Yud0766ec2022-09-22 10:46:57 +08002767 }
Gilles Peskine449bd832023-01-11 14:50:10 +01002768 if (ssl->conf->new_session_tickets_count == 0) {
2769 MBEDTLS_SSL_DEBUG_MSG(2, ("NewSessionTicket: disabled,"
2770 " configured count is zero"));
2771 return SSL_NEW_SESSION_TICKET_SKIP;
Jerry Yud0766ec2022-09-22 10:46:57 +08002772 }
2773
Gilles Peskine449bd832023-01-11 14:50:10 +01002774 if (ssl->handshake->new_session_tickets_count == 0) {
2775 MBEDTLS_SSL_DEBUG_MSG(2, ("NewSessionTicket: all tickets have "
2776 "been sent."));
2777 return SSL_NEW_SESSION_TICKET_SKIP;
Jerry Yue67bef42022-07-07 07:29:42 +00002778 }
2779
Gilles Peskine449bd832023-01-11 14:50:10 +01002780 return SSL_NEW_SESSION_TICKET_WRITE;
Jerry Yue67bef42022-07-07 07:29:42 +00002781}
2782
2783#if defined(MBEDTLS_SSL_SESSION_TICKETS)
2784MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002785static int ssl_tls13_prepare_new_session_ticket(mbedtls_ssl_context *ssl,
2786 unsigned char *ticket_nonce,
2787 size_t ticket_nonce_size)
Jerry Yue67bef42022-07-07 07:29:42 +00002788{
2789 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
2790 mbedtls_ssl_session *session = ssl->session;
2791 mbedtls_ssl_ciphersuite_t *ciphersuite_info;
2792 psa_algorithm_t psa_hash_alg;
2793 int hash_length;
2794
Gilles Peskine449bd832023-01-11 14:50:10 +01002795 MBEDTLS_SSL_DEBUG_MSG(2, ("=> prepare NewSessionTicket msg"));
Jerry Yue67bef42022-07-07 07:29:42 +00002796
2797#if defined(MBEDTLS_HAVE_TIME)
Gilles Peskine449bd832023-01-11 14:50:10 +01002798 session->start = mbedtls_time(NULL);
Jerry Yue67bef42022-07-07 07:29:42 +00002799#endif
2800
Pengyu Lv9f926952022-11-17 15:22:33 +08002801 /* Set ticket_flags depends on the advertised psk key exchange mode */
Pengyu Lv80270b22023-01-12 11:54:04 +08002802 mbedtls_ssl_session_clear_ticket_flags(
Pengyu Lv9eacb442022-12-09 14:39:19 +08002803 session, MBEDTLS_SSL_TLS1_3_TICKET_FLAGS_MASK);
Pengyu Lve6487fe2022-12-06 09:30:29 +08002804#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED)
Pengyu Lv80270b22023-01-12 11:54:04 +08002805 mbedtls_ssl_session_set_ticket_flags(
Pengyu Lv9eacb442022-12-09 14:39:19 +08002806 session, ssl->handshake->tls13_kex_modes);
Pengyu Lve6487fe2022-12-06 09:30:29 +08002807#endif
Pengyu Lv4938a562023-01-16 11:28:49 +08002808 MBEDTLS_SSL_PRINT_TICKET_FLAGS(4, session->ticket_flags);
Pengyu Lv9f926952022-11-17 15:22:33 +08002809
Jerry Yue67bef42022-07-07 07:29:42 +00002810 /* Generate ticket_age_add */
Gilles Peskine449bd832023-01-11 14:50:10 +01002811 if ((ret = ssl->conf->f_rng(ssl->conf->p_rng,
2812 (unsigned char *) &session->ticket_age_add,
2813 sizeof(session->ticket_age_add)) != 0)) {
2814 MBEDTLS_SSL_DEBUG_RET(1, "generate_ticket_age_add", ret);
2815 return ret;
Jerry Yue67bef42022-07-07 07:29:42 +00002816 }
Gilles Peskine449bd832023-01-11 14:50:10 +01002817 MBEDTLS_SSL_DEBUG_MSG(3, ("ticket_age_add: %u",
2818 (unsigned int) session->ticket_age_add));
Jerry Yue67bef42022-07-07 07:29:42 +00002819
2820 /* Generate ticket_nonce */
Gilles Peskine449bd832023-01-11 14:50:10 +01002821 ret = ssl->conf->f_rng(ssl->conf->p_rng, ticket_nonce, ticket_nonce_size);
2822 if (ret != 0) {
2823 MBEDTLS_SSL_DEBUG_RET(1, "generate_ticket_nonce", ret);
2824 return ret;
Jerry Yue67bef42022-07-07 07:29:42 +00002825 }
Gilles Peskine449bd832023-01-11 14:50:10 +01002826 MBEDTLS_SSL_DEBUG_BUF(3, "ticket_nonce:",
2827 ticket_nonce, ticket_nonce_size);
Jerry Yue67bef42022-07-07 07:29:42 +00002828
2829 ciphersuite_info =
Gilles Peskine449bd832023-01-11 14:50:10 +01002830 (mbedtls_ssl_ciphersuite_t *) ssl->handshake->ciphersuite_info;
Dave Rodgman2eab4622023-10-05 13:30:37 +01002831 psa_hash_alg = mbedtls_md_psa_alg_from_type((mbedtls_md_type_t) ciphersuite_info->mac);
Gilles Peskine449bd832023-01-11 14:50:10 +01002832 hash_length = PSA_HASH_LENGTH(psa_hash_alg);
2833 if (hash_length == -1 ||
2834 (size_t) hash_length > sizeof(session->resumption_key)) {
2835 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Jerry Yufca4d572022-07-21 10:37:48 +08002836 }
2837
Jerry Yue67bef42022-07-07 07:29:42 +00002838 /* In this code the psk key length equals the length of the hash */
2839 session->resumption_key_len = hash_length;
2840 session->ciphersuite = ciphersuite_info->id;
2841
2842 /* Compute resumption key
2843 *
2844 * HKDF-Expand-Label( resumption_master_secret,
2845 * "resumption", ticket_nonce, Hash.length )
2846 */
2847 ret = mbedtls_ssl_tls13_hkdf_expand_label(
Gilles Peskine449bd832023-01-11 14:50:10 +01002848 psa_hash_alg,
2849 session->app_secrets.resumption_master_secret,
2850 hash_length,
2851 MBEDTLS_SSL_TLS1_3_LBL_WITH_LEN(resumption),
2852 ticket_nonce,
2853 ticket_nonce_size,
2854 session->resumption_key,
2855 hash_length);
Jerry Yue67bef42022-07-07 07:29:42 +00002856
Gilles Peskine449bd832023-01-11 14:50:10 +01002857 if (ret != 0) {
2858 MBEDTLS_SSL_DEBUG_RET(2,
2859 "Creating the ticket-resumed PSK failed",
2860 ret);
2861 return ret;
Jerry Yue67bef42022-07-07 07:29:42 +00002862 }
Gilles Peskine449bd832023-01-11 14:50:10 +01002863 MBEDTLS_SSL_DEBUG_BUF(3, "Ticket-resumed PSK",
2864 session->resumption_key,
2865 session->resumption_key_len);
Jerry Yue67bef42022-07-07 07:29:42 +00002866
Gilles Peskine449bd832023-01-11 14:50:10 +01002867 MBEDTLS_SSL_DEBUG_BUF(3, "resumption_master_secret",
2868 session->app_secrets.resumption_master_secret,
2869 hash_length);
Jerry Yue67bef42022-07-07 07:29:42 +00002870
Gilles Peskine449bd832023-01-11 14:50:10 +01002871 return 0;
Jerry Yue67bef42022-07-07 07:29:42 +00002872}
2873
2874/* This function creates a NewSessionTicket message in the following format:
2875 *
2876 * struct {
2877 * uint32 ticket_lifetime;
2878 * uint32 ticket_age_add;
2879 * opaque ticket_nonce<0..255>;
2880 * opaque ticket<1..2^16-1>;
2881 * Extension extensions<0..2^16-2>;
2882 * } NewSessionTicket;
2883 *
2884 * The ticket inside the NewSessionTicket message is an encrypted container
2885 * carrying the necessary information so that the server is later able to
2886 * re-start the communication.
2887 *
2888 * The following fields are placed inside the ticket by the
2889 * f_ticket_write() function:
2890 *
2891 * - creation time (start)
2892 * - flags (flags)
2893 * - age add (ticket_age_add)
2894 * - key (key)
2895 * - key length (key_len)
2896 * - ciphersuite (ciphersuite)
2897 */
2898MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002899static int ssl_tls13_write_new_session_ticket_body(mbedtls_ssl_context *ssl,
2900 unsigned char *buf,
2901 unsigned char *end,
2902 size_t *out_len,
2903 unsigned char *ticket_nonce,
2904 size_t ticket_nonce_size)
Jerry Yue67bef42022-07-07 07:29:42 +00002905{
2906 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
2907 unsigned char *p = buf;
2908 mbedtls_ssl_session *session = ssl->session;
2909 size_t ticket_len;
2910 uint32_t ticket_lifetime;
2911
2912 *out_len = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01002913 MBEDTLS_SSL_DEBUG_MSG(2, ("=> write NewSessionTicket msg"));
Jerry Yue67bef42022-07-07 07:29:42 +00002914
2915 /*
2916 * ticket_lifetime 4 bytes
2917 * ticket_age_add 4 bytes
2918 * ticket_nonce 1 + ticket_nonce_size bytes
2919 * ticket >=2 bytes
2920 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002921 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 4 + 4 + 1 + ticket_nonce_size + 2);
Jerry Yue67bef42022-07-07 07:29:42 +00002922
2923 /* Generate ticket and ticket_lifetime */
Gilles Peskine449bd832023-01-11 14:50:10 +01002924 ret = ssl->conf->f_ticket_write(ssl->conf->p_ticket,
2925 session,
2926 p + 9 + ticket_nonce_size + 2,
2927 end,
2928 &ticket_len,
2929 &ticket_lifetime);
2930 if (ret != 0) {
2931 MBEDTLS_SSL_DEBUG_RET(1, "write_ticket", ret);
2932 return ret;
Jerry Yue67bef42022-07-07 07:29:42 +00002933 }
2934 /* RFC 8446 4.6.1
2935 * ticket_lifetime: Indicates the lifetime in seconds as a 32-bit
2936 * unsigned integer in network byte order from the time of ticket
2937 * issuance. Servers MUST NOT use any value greater than
2938 * 604800 seconds (7 days). The value of zero indicates that the
2939 * ticket should be discarded immediately. Clients MUST NOT cache
2940 * tickets for longer than 7 days, regardless of the ticket_lifetime,
2941 * and MAY delete tickets earlier based on local policy. A server
2942 * MAY treat a ticket as valid for a shorter period of time than what
2943 * is stated in the ticket_lifetime.
2944 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002945 if (ticket_lifetime > 604800) {
Xiaokang Qian28af5012022-10-13 08:18:19 +00002946 ticket_lifetime = 604800;
Gilles Peskine449bd832023-01-11 14:50:10 +01002947 }
2948 MBEDTLS_PUT_UINT32_BE(ticket_lifetime, p, 0);
2949 MBEDTLS_SSL_DEBUG_MSG(3, ("ticket_lifetime: %u",
2950 (unsigned int) ticket_lifetime));
Jerry Yue67bef42022-07-07 07:29:42 +00002951
2952 /* Write ticket_age_add */
Gilles Peskine449bd832023-01-11 14:50:10 +01002953 MBEDTLS_PUT_UINT32_BE(session->ticket_age_add, p, 4);
2954 MBEDTLS_SSL_DEBUG_MSG(3, ("ticket_age_add: %u",
2955 (unsigned int) session->ticket_age_add));
Jerry Yue67bef42022-07-07 07:29:42 +00002956
2957 /* Write ticket_nonce */
Gilles Peskine449bd832023-01-11 14:50:10 +01002958 p[8] = (unsigned char) ticket_nonce_size;
2959 if (ticket_nonce_size > 0) {
2960 memcpy(p + 9, ticket_nonce, ticket_nonce_size);
Jerry Yue67bef42022-07-07 07:29:42 +00002961 }
2962 p += 9 + ticket_nonce_size;
2963
2964 /* Write ticket */
Gilles Peskine449bd832023-01-11 14:50:10 +01002965 MBEDTLS_PUT_UINT16_BE(ticket_len, p, 0);
Jerry Yue67bef42022-07-07 07:29:42 +00002966 p += 2;
Gilles Peskine449bd832023-01-11 14:50:10 +01002967 MBEDTLS_SSL_DEBUG_BUF(4, "ticket", p, ticket_len);
Jerry Yue67bef42022-07-07 07:29:42 +00002968 p += ticket_len;
2969
2970 /* Ticket Extensions
2971 *
2972 * Note: We currently don't have any extensions.
2973 * Set length to zero.
2974 */
Jerry Yuedab6372022-10-31 14:37:31 +08002975 ssl->handshake->sent_extensions = MBEDTLS_SSL_EXT_MASK_NONE;
2976
Gilles Peskine449bd832023-01-11 14:50:10 +01002977 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 2);
2978 MBEDTLS_PUT_UINT16_BE(0, p, 0);
Jerry Yue67bef42022-07-07 07:29:42 +00002979 p += 2;
2980
2981 *out_len = p - buf;
Gilles Peskine449bd832023-01-11 14:50:10 +01002982 MBEDTLS_SSL_DEBUG_BUF(4, "ticket", buf, *out_len);
2983 MBEDTLS_SSL_DEBUG_MSG(2, ("<= write new session ticket"));
Jerry Yue67bef42022-07-07 07:29:42 +00002984
Jerry Yu7de2ff02022-11-08 21:43:46 +08002985 MBEDTLS_SSL_PRINT_EXTS(
Gilles Peskine449bd832023-01-11 14:50:10 +01002986 3, MBEDTLS_SSL_HS_NEW_SESSION_TICKET, ssl->handshake->sent_extensions);
Jerry Yu4b8f2f72022-10-31 13:31:22 +08002987
Gilles Peskine449bd832023-01-11 14:50:10 +01002988 return 0;
Jerry Yue67bef42022-07-07 07:29:42 +00002989}
2990
2991/*
Jerry Yua8d3c502022-10-30 14:51:23 +08002992 * Handler for MBEDTLS_SSL_TLS1_3_NEW_SESSION_TICKET
Jerry Yue67bef42022-07-07 07:29:42 +00002993 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002994static int ssl_tls13_write_new_session_ticket(mbedtls_ssl_context *ssl)
Jerry Yue67bef42022-07-07 07:29:42 +00002995{
2996 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
2997
Gilles Peskine449bd832023-01-11 14:50:10 +01002998 MBEDTLS_SSL_PROC_CHK_NEG(ssl_tls13_write_new_session_ticket_coordinate(ssl));
Jerry Yue67bef42022-07-07 07:29:42 +00002999
Gilles Peskine449bd832023-01-11 14:50:10 +01003000 if (ret == SSL_NEW_SESSION_TICKET_WRITE) {
Jerry Yue67bef42022-07-07 07:29:42 +00003001 unsigned char ticket_nonce[MBEDTLS_SSL_TLS1_3_TICKET_NONCE_LENGTH];
3002 unsigned char *buf;
3003 size_t buf_len, msg_len;
3004
Gilles Peskine449bd832023-01-11 14:50:10 +01003005 MBEDTLS_SSL_PROC_CHK(ssl_tls13_prepare_new_session_ticket(
3006 ssl, ticket_nonce, sizeof(ticket_nonce)));
Jerry Yue67bef42022-07-07 07:29:42 +00003007
Xiaokang Qian9f1747b2023-03-30 02:50:04 +00003008 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_start_handshake_msg(
3009 ssl, MBEDTLS_SSL_HS_NEW_SESSION_TICKET,
3010 &buf, &buf_len));
Jerry Yue67bef42022-07-07 07:29:42 +00003011
Gilles Peskine449bd832023-01-11 14:50:10 +01003012 MBEDTLS_SSL_PROC_CHK(ssl_tls13_write_new_session_ticket_body(
3013 ssl, buf, buf + buf_len, &msg_len,
3014 ticket_nonce, sizeof(ticket_nonce)));
Jerry Yue67bef42022-07-07 07:29:42 +00003015
Gilles Peskine449bd832023-01-11 14:50:10 +01003016 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_finish_handshake_msg(
3017 ssl, buf_len, msg_len));
Jerry Yufca4d572022-07-21 10:37:48 +08003018
Jerry Yu359e65f2022-09-22 23:47:43 +08003019 /* Limit session tickets count to one when resumption connection.
3020 *
3021 * See document of mbedtls_ssl_conf_new_session_tickets.
3022 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003023 if (ssl->handshake->resume == 1) {
Jerry Yu359e65f2022-09-22 23:47:43 +08003024 ssl->handshake->new_session_tickets_count = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01003025 } else {
Jerry Yu359e65f2022-09-22 23:47:43 +08003026 ssl->handshake->new_session_tickets_count--;
Gilles Peskine449bd832023-01-11 14:50:10 +01003027 }
Jerry Yub7e3fa72022-09-22 11:07:18 +08003028
Jerry Yua8d3c502022-10-30 14:51:23 +08003029 mbedtls_ssl_handshake_set_state(
Gilles Peskine449bd832023-01-11 14:50:10 +01003030 ssl, MBEDTLS_SSL_TLS1_3_NEW_SESSION_TICKET_FLUSH);
3031 } else {
3032 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_HANDSHAKE_OVER);
Jerry Yue67bef42022-07-07 07:29:42 +00003033 }
3034
Jerry Yue67bef42022-07-07 07:29:42 +00003035cleanup:
3036
Gilles Peskine449bd832023-01-11 14:50:10 +01003037 return ret;
Jerry Yue67bef42022-07-07 07:29:42 +00003038}
3039#endif /* MBEDTLS_SSL_SESSION_TICKETS */
3040
3041/*
XiaokangQiane8ff3502022-04-22 02:34:40 +00003042 * TLS 1.3 State Machine -- server side
XiaokangQian7807f9f2022-02-15 10:04:37 +00003043 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003044int mbedtls_ssl_tls13_handshake_server_step(mbedtls_ssl_context *ssl)
Jerry Yub9930e72021-08-06 17:11:51 +08003045{
XiaokangQian08037552022-04-20 07:16:41 +00003046 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
XiaokangQian7807f9f2022-02-15 10:04:37 +00003047
Gilles Peskine449bd832023-01-11 14:50:10 +01003048 if (ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER || ssl->handshake == NULL) {
3049 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
3050 }
XiaokangQian7807f9f2022-02-15 10:04:37 +00003051
Gilles Peskine449bd832023-01-11 14:50:10 +01003052 MBEDTLS_SSL_DEBUG_MSG(2, ("tls13 server state: %s(%d)",
Dave Rodgman2eab4622023-10-05 13:30:37 +01003053 mbedtls_ssl_states_str((mbedtls_ssl_states) ssl->state),
Gilles Peskine449bd832023-01-11 14:50:10 +01003054 ssl->state));
Jerry Yu6e81b272021-09-27 11:16:17 +08003055
Gilles Peskine449bd832023-01-11 14:50:10 +01003056 switch (ssl->state) {
XiaokangQian7807f9f2022-02-15 10:04:37 +00003057 /* start state */
3058 case MBEDTLS_SSL_HELLO_REQUEST:
Gilles Peskine449bd832023-01-11 14:50:10 +01003059 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_CLIENT_HELLO);
XiaokangQian08037552022-04-20 07:16:41 +00003060 ret = 0;
XiaokangQian7807f9f2022-02-15 10:04:37 +00003061 break;
3062
XiaokangQian7807f9f2022-02-15 10:04:37 +00003063 case MBEDTLS_SSL_CLIENT_HELLO:
Gilles Peskine449bd832023-01-11 14:50:10 +01003064 ret = ssl_tls13_process_client_hello(ssl);
3065 if (ret != 0) {
3066 MBEDTLS_SSL_DEBUG_RET(1, "ssl_tls13_process_client_hello", ret);
3067 }
Jerry Yuf41553b2022-05-09 22:20:30 +08003068 break;
XiaokangQian7807f9f2022-02-15 10:04:37 +00003069
Jerry Yuf41553b2022-05-09 22:20:30 +08003070 case MBEDTLS_SSL_HELLO_RETRY_REQUEST:
Gilles Peskine449bd832023-01-11 14:50:10 +01003071 ret = ssl_tls13_write_hello_retry_request(ssl);
3072 if (ret != 0) {
3073 MBEDTLS_SSL_DEBUG_RET(1, "ssl_tls13_write_hello_retry_request", ret);
3074 return ret;
Jerry Yuf41553b2022-05-09 22:20:30 +08003075 }
XiaokangQian7807f9f2022-02-15 10:04:37 +00003076 break;
3077
Jerry Yu5b64ae92022-03-30 17:15:02 +08003078 case MBEDTLS_SSL_SERVER_HELLO:
Gilles Peskine449bd832023-01-11 14:50:10 +01003079 ret = ssl_tls13_write_server_hello(ssl);
Jerry Yu5b64ae92022-03-30 17:15:02 +08003080 break;
3081
Xiaofei Baicba64af2022-02-15 10:00:56 +00003082 case MBEDTLS_SSL_ENCRYPTED_EXTENSIONS:
Gilles Peskine449bd832023-01-11 14:50:10 +01003083 ret = ssl_tls13_write_encrypted_extensions(ssl);
3084 if (ret != 0) {
3085 MBEDTLS_SSL_DEBUG_RET(1, "ssl_tls13_write_encrypted_extensions", ret);
3086 return ret;
Xiaofei Baicba64af2022-02-15 10:00:56 +00003087 }
Jerry Yu48330562022-05-06 21:35:44 +08003088 break;
Jerry Yu6a2cd9e2022-05-05 11:14:19 +08003089
Ronald Cron928cbd32022-10-04 16:14:26 +02003090#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED)
Xiaofei Bai9ca09d42022-02-14 12:57:18 +00003091 case MBEDTLS_SSL_CERTIFICATE_REQUEST:
Gilles Peskine449bd832023-01-11 14:50:10 +01003092 ret = ssl_tls13_write_certificate_request(ssl);
Xiaofei Bai9ca09d42022-02-14 12:57:18 +00003093 break;
Xiaofei Bai9ca09d42022-02-14 12:57:18 +00003094
Jerry Yu83da34e2022-04-16 13:59:52 +08003095 case MBEDTLS_SSL_SERVER_CERTIFICATE:
Gilles Peskine449bd832023-01-11 14:50:10 +01003096 ret = ssl_tls13_write_server_certificate(ssl);
Jerry Yu83da34e2022-04-16 13:59:52 +08003097 break;
3098
3099 case MBEDTLS_SSL_CERTIFICATE_VERIFY:
Gilles Peskine449bd832023-01-11 14:50:10 +01003100 ret = ssl_tls13_write_certificate_verify(ssl);
Jerry Yu83da34e2022-04-16 13:59:52 +08003101 break;
Ronald Cron928cbd32022-10-04 16:14:26 +02003102#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED */
Jerry Yu83da34e2022-04-16 13:59:52 +08003103
Gilles Peskine449bd832023-01-11 14:50:10 +01003104 /*
3105 * Injection of dummy-CCS's for middlebox compatibility
3106 */
Gabor Mezei7b39bf12022-05-24 16:04:14 +02003107#if defined(MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE)
Gabor Mezeif7044ea2022-06-28 16:01:49 +02003108 case MBEDTLS_SSL_SERVER_CCS_AFTER_HELLO_RETRY_REQUEST:
Gilles Peskine449bd832023-01-11 14:50:10 +01003109 ret = mbedtls_ssl_tls13_write_change_cipher_spec(ssl);
3110 if (ret == 0) {
3111 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_CLIENT_HELLO);
3112 }
Gabor Mezei7b39bf12022-05-24 16:04:14 +02003113 break;
3114
3115 case MBEDTLS_SSL_SERVER_CCS_AFTER_SERVER_HELLO:
Gilles Peskine449bd832023-01-11 14:50:10 +01003116 ret = mbedtls_ssl_tls13_write_change_cipher_spec(ssl);
3117 if (ret == 0) {
3118 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_ENCRYPTED_EXTENSIONS);
3119 }
Gabor Mezei7b39bf12022-05-24 16:04:14 +02003120 break;
3121#endif /* MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE */
3122
Jerry Yu69dd8d42022-04-16 12:51:26 +08003123 case MBEDTLS_SSL_SERVER_FINISHED:
Gilles Peskine449bd832023-01-11 14:50:10 +01003124 ret = ssl_tls13_write_server_finished(ssl);
Jerry Yu69dd8d42022-04-16 12:51:26 +08003125 break;
3126
3127 case MBEDTLS_SSL_CLIENT_FINISHED:
Gilles Peskine449bd832023-01-11 14:50:10 +01003128 ret = ssl_tls13_process_client_finished(ssl);
Jerry Yu69dd8d42022-04-16 12:51:26 +08003129 break;
3130
Jerry Yu69dd8d42022-04-16 12:51:26 +08003131 case MBEDTLS_SSL_HANDSHAKE_WRAPUP:
Gilles Peskine449bd832023-01-11 14:50:10 +01003132 ret = ssl_tls13_handshake_wrapup(ssl);
Jerry Yu69dd8d42022-04-16 12:51:26 +08003133 break;
3134
Ronald Cron766c0cd2022-10-18 12:17:11 +02003135#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED)
XiaokangQian6b916b12022-04-25 07:29:34 +00003136 case MBEDTLS_SSL_CLIENT_CERTIFICATE:
Gilles Peskine449bd832023-01-11 14:50:10 +01003137 ret = mbedtls_ssl_tls13_process_certificate(ssl);
3138 if (ret == 0) {
3139 if (ssl->session_negotiate->peer_cert != NULL) {
Ronald Cron209cae92022-06-07 10:30:19 +02003140 mbedtls_ssl_handshake_set_state(
Gilles Peskine449bd832023-01-11 14:50:10 +01003141 ssl, MBEDTLS_SSL_CLIENT_CERTIFICATE_VERIFY);
3142 } else {
3143 MBEDTLS_SSL_DEBUG_MSG(2, ("skip parse certificate verify"));
Ronald Cron209cae92022-06-07 10:30:19 +02003144 mbedtls_ssl_handshake_set_state(
Gilles Peskine449bd832023-01-11 14:50:10 +01003145 ssl, MBEDTLS_SSL_CLIENT_FINISHED);
Ronald Cron19385882022-06-15 16:26:13 +02003146 }
XiaokangQian6b916b12022-04-25 07:29:34 +00003147 }
3148 break;
3149
3150 case MBEDTLS_SSL_CLIENT_CERTIFICATE_VERIFY:
Gilles Peskine449bd832023-01-11 14:50:10 +01003151 ret = mbedtls_ssl_tls13_process_certificate_verify(ssl);
3152 if (ret == 0) {
XiaokangQian6b916b12022-04-25 07:29:34 +00003153 mbedtls_ssl_handshake_set_state(
Gilles Peskine449bd832023-01-11 14:50:10 +01003154 ssl, MBEDTLS_SSL_CLIENT_FINISHED);
XiaokangQian6b916b12022-04-25 07:29:34 +00003155 }
3156 break;
Ronald Cron766c0cd2022-10-18 12:17:11 +02003157#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED */
XiaokangQian6b916b12022-04-25 07:29:34 +00003158
Jerry Yue67bef42022-07-07 07:29:42 +00003159#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Jerry Yua8d3c502022-10-30 14:51:23 +08003160 case MBEDTLS_SSL_TLS1_3_NEW_SESSION_TICKET:
Gilles Peskine449bd832023-01-11 14:50:10 +01003161 ret = ssl_tls13_write_new_session_ticket(ssl);
3162 if (ret != 0) {
3163 MBEDTLS_SSL_DEBUG_RET(1,
3164 "ssl_tls13_write_new_session_ticket ",
3165 ret);
Jerry Yue67bef42022-07-07 07:29:42 +00003166 }
3167 break;
Jerry Yua8d3c502022-10-30 14:51:23 +08003168 case MBEDTLS_SSL_TLS1_3_NEW_SESSION_TICKET_FLUSH:
Jerry Yue67bef42022-07-07 07:29:42 +00003169 /* This state is necessary to do the flush of the New Session
Jerry Yua8d3c502022-10-30 14:51:23 +08003170 * Ticket message written in MBEDTLS_SSL_TLS1_3_NEW_SESSION_TICKET
Jerry Yue67bef42022-07-07 07:29:42 +00003171 * as part of ssl_prepare_handshake_step.
3172 */
3173 ret = 0;
Jerry Yud4e75002022-08-09 13:33:50 +08003174
Gilles Peskine449bd832023-01-11 14:50:10 +01003175 if (ssl->handshake->new_session_tickets_count == 0) {
3176 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_HANDSHAKE_OVER);
3177 } else {
Xiaokang Qian9f1747b2023-03-30 02:50:04 +00003178 mbedtls_ssl_handshake_set_state(
3179 ssl, MBEDTLS_SSL_TLS1_3_NEW_SESSION_TICKET);
Gilles Peskine449bd832023-01-11 14:50:10 +01003180 }
Jerry Yue67bef42022-07-07 07:29:42 +00003181 break;
3182
3183#endif /* MBEDTLS_SSL_SESSION_TICKETS */
3184
XiaokangQian7807f9f2022-02-15 10:04:37 +00003185 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01003186 MBEDTLS_SSL_DEBUG_MSG(1, ("invalid state %d", ssl->state));
3187 return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
XiaokangQian7807f9f2022-02-15 10:04:37 +00003188 }
3189
Gilles Peskine449bd832023-01-11 14:50:10 +01003190 return ret;
Jerry Yub9930e72021-08-06 17:11:51 +08003191}
Jerry Yu3cc4c2a2021-08-06 16:29:08 +08003192
Jerry Yufb4b6472022-01-27 15:03:26 +08003193#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_PROTO_TLS1_3 */