blob: 49324d83cd0587782b8bcc054a7b5d795a7fbd7a [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 Lvdadeb202023-01-18 17:32:34 +08001029 return !ssl->handshake->resume &&
1030 mbedtls_ssl_conf_tls13_ephemeral_enabled(ssl) &&
Gilles Peskine449bd832023-01-11 14:50:10 +01001031 ssl_tls13_client_hello_has_exts_for_ephemeral_key_exchange(ssl);
Jerry Yue5991322022-11-07 14:03:44 +08001032#else
1033 ((void) ssl);
Gilles Peskine449bd832023-01-11 14:50:10 +01001034 return 0;
Jerry Yue5991322022-11-07 14:03:44 +08001035#endif
Jerry Yu77f01482022-07-11 07:03:24 +00001036}
XiaokangQian7807f9f2022-02-15 10:04:37 +00001037
Jerry Yu77f01482022-07-11 07:03:24 +00001038MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01001039static int ssl_tls13_check_psk_key_exchange(mbedtls_ssl_context *ssl)
Jerry Yu77f01482022-07-11 07:03:24 +00001040{
Jerry Yue5991322022-11-07 14:03:44 +08001041#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK_ENABLED)
Pengyu Lv52ad3332023-02-14 14:32:37 +08001042 return ssl_tls13_ticket_permission_check(
Pengyu Lv306a01d2023-02-02 16:35:47 +08001043 ssl, MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK) &&
1044 mbedtls_ssl_conf_tls13_psk_enabled(ssl) &&
Gilles Peskine449bd832023-01-11 14:50:10 +01001045 mbedtls_ssl_tls13_psk_enabled(ssl) &&
1046 ssl_tls13_client_hello_has_exts_for_psk_key_exchange(ssl);
Jerry Yu77f01482022-07-11 07:03:24 +00001047#else
1048 ((void) ssl);
Gilles Peskine449bd832023-01-11 14:50:10 +01001049 return 0;
Jerry Yu77f01482022-07-11 07:03:24 +00001050#endif
1051}
XiaokangQian7807f9f2022-02-15 10:04:37 +00001052
Jerry Yu77f01482022-07-11 07:03:24 +00001053MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01001054static int ssl_tls13_check_psk_ephemeral_key_exchange(mbedtls_ssl_context *ssl)
Jerry Yu77f01482022-07-11 07:03:24 +00001055{
Jerry Yue5991322022-11-07 14:03:44 +08001056#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK_EPHEMERAL_ENABLED)
Pengyu Lv52ad3332023-02-14 14:32:37 +08001057 return ssl_tls13_ticket_permission_check(
Pengyu Lv306a01d2023-02-02 16:35:47 +08001058 ssl, MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK_EPHEMERAL) &&
1059 mbedtls_ssl_conf_tls13_psk_ephemeral_enabled(ssl) &&
Gilles Peskine449bd832023-01-11 14:50:10 +01001060 mbedtls_ssl_tls13_psk_ephemeral_enabled(ssl) &&
1061 ssl_tls13_client_hello_has_exts_for_psk_ephemeral_key_exchange(ssl);
Jerry Yu77f01482022-07-11 07:03:24 +00001062#else
1063 ((void) ssl);
Gilles Peskine449bd832023-01-11 14:50:10 +01001064 return 0;
Jerry Yu77f01482022-07-11 07:03:24 +00001065#endif
1066}
1067
Gilles Peskine449bd832023-01-11 14:50:10 +01001068static int ssl_tls13_determine_key_exchange_mode(mbedtls_ssl_context *ssl)
Jerry Yu77f01482022-07-11 07:03:24 +00001069{
1070 /*
1071 * Determine the key exchange algorithm to use.
1072 * There are three types of key exchanges supported in TLS 1.3:
1073 * - (EC)DH with ECDSA,
1074 * - (EC)DH with PSK,
1075 * - plain PSK.
1076 *
1077 * The PSK-based key exchanges may additionally be used with 0-RTT.
1078 *
1079 * Our built-in order of preference is
Jerry Yuce6ed702022-07-22 21:49:53 +08001080 * 1 ) (EC)DHE-PSK Mode ( psk_ephemeral )
1081 * 2 ) Certificate Mode ( ephemeral )
1082 * 3 ) Plain PSK Mode ( psk )
Jerry Yu77f01482022-07-11 07:03:24 +00001083 */
1084
Xiaokang Qian9f1747b2023-03-30 02:50:04 +00001085 ssl->handshake->key_exchange_mode =
1086 MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_NONE;
Jerry Yu77f01482022-07-11 07:03:24 +00001087
Gilles Peskine449bd832023-01-11 14:50:10 +01001088 if (ssl_tls13_check_psk_ephemeral_key_exchange(ssl)) {
Jerry Yu77f01482022-07-11 07:03:24 +00001089 ssl->handshake->key_exchange_mode =
1090 MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK_EPHEMERAL;
Gilles Peskine449bd832023-01-11 14:50:10 +01001091 MBEDTLS_SSL_DEBUG_MSG(2, ("key exchange mode: psk_ephemeral"));
1092 } else
1093 if (ssl_tls13_check_ephemeral_key_exchange(ssl)) {
Jerry Yu77f01482022-07-11 07:03:24 +00001094 ssl->handshake->key_exchange_mode =
1095 MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL;
Gilles Peskine449bd832023-01-11 14:50:10 +01001096 MBEDTLS_SSL_DEBUG_MSG(2, ("key exchange mode: ephemeral"));
1097 } else
1098 if (ssl_tls13_check_psk_key_exchange(ssl)) {
Jerry Yuce6ed702022-07-22 21:49:53 +08001099 ssl->handshake->key_exchange_mode =
1100 MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK;
Gilles Peskine449bd832023-01-11 14:50:10 +01001101 MBEDTLS_SSL_DEBUG_MSG(2, ("key exchange mode: psk"));
1102 } else {
Jerry Yu77f01482022-07-11 07:03:24 +00001103 MBEDTLS_SSL_DEBUG_MSG(
Gilles Peskine449bd832023-01-11 14:50:10 +01001104 1,
1105 ("ClientHello message misses mandatory extensions."));
1106 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_MISSING_EXTENSION,
1107 MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER);
1108 return MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER;
Jerry Yu77f01482022-07-11 07:03:24 +00001109 }
1110
Gilles Peskine449bd832023-01-11 14:50:10 +01001111 return 0;
Jerry Yu77f01482022-07-11 07:03:24 +00001112
XiaokangQian7807f9f2022-02-15 10:04:37 +00001113}
1114
XiaokangQian81802f42022-06-10 13:25:22 +00001115#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
Ronald Cron928cbd32022-10-04 16:14:26 +02001116 defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED)
Ronald Cron67ea2542022-09-15 17:34:42 +02001117
1118#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01001119static psa_algorithm_t ssl_tls13_iana_sig_alg_to_psa_alg(uint16_t sig_alg)
Ronald Cron67ea2542022-09-15 17:34:42 +02001120{
Gilles Peskine449bd832023-01-11 14:50:10 +01001121 switch (sig_alg) {
Ronald Cron67ea2542022-09-15 17:34:42 +02001122 case MBEDTLS_TLS1_3_SIG_ECDSA_SECP256R1_SHA256:
Gilles Peskine449bd832023-01-11 14:50:10 +01001123 return PSA_ALG_ECDSA(PSA_ALG_SHA_256);
Ronald Cron67ea2542022-09-15 17:34:42 +02001124 case MBEDTLS_TLS1_3_SIG_ECDSA_SECP384R1_SHA384:
Gilles Peskine449bd832023-01-11 14:50:10 +01001125 return PSA_ALG_ECDSA(PSA_ALG_SHA_384);
Ronald Cron67ea2542022-09-15 17:34:42 +02001126 case MBEDTLS_TLS1_3_SIG_ECDSA_SECP521R1_SHA512:
Gilles Peskine449bd832023-01-11 14:50:10 +01001127 return PSA_ALG_ECDSA(PSA_ALG_SHA_512);
Ronald Cron67ea2542022-09-15 17:34:42 +02001128 case MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA256:
Gilles Peskine449bd832023-01-11 14:50:10 +01001129 return PSA_ALG_RSA_PSS(PSA_ALG_SHA_256);
Ronald Cron67ea2542022-09-15 17:34:42 +02001130 case MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA384:
Gilles Peskine449bd832023-01-11 14:50:10 +01001131 return PSA_ALG_RSA_PSS(PSA_ALG_SHA_384);
Ronald Cron67ea2542022-09-15 17:34:42 +02001132 case MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA512:
Gilles Peskine449bd832023-01-11 14:50:10 +01001133 return PSA_ALG_RSA_PSS(PSA_ALG_SHA_512);
Ronald Cron67ea2542022-09-15 17:34:42 +02001134 case MBEDTLS_TLS1_3_SIG_RSA_PKCS1_SHA256:
Gilles Peskine449bd832023-01-11 14:50:10 +01001135 return PSA_ALG_RSA_PKCS1V15_SIGN(PSA_ALG_SHA_256);
Ronald Cron67ea2542022-09-15 17:34:42 +02001136 case MBEDTLS_TLS1_3_SIG_RSA_PKCS1_SHA384:
Gilles Peskine449bd832023-01-11 14:50:10 +01001137 return PSA_ALG_RSA_PKCS1V15_SIGN(PSA_ALG_SHA_384);
Ronald Cron67ea2542022-09-15 17:34:42 +02001138 case MBEDTLS_TLS1_3_SIG_RSA_PKCS1_SHA512:
Gilles Peskine449bd832023-01-11 14:50:10 +01001139 return PSA_ALG_RSA_PKCS1V15_SIGN(PSA_ALG_SHA_512);
Ronald Cron67ea2542022-09-15 17:34:42 +02001140 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01001141 return PSA_ALG_NONE;
Ronald Cron67ea2542022-09-15 17:34:42 +02001142 }
1143}
1144#endif /* MBEDTLS_USE_PSA_CRYPTO */
1145
XiaokangQian23c5be62022-06-07 02:04:34 +00001146/*
XiaokangQianfb665a82022-06-15 03:57:21 +00001147 * Pick best ( private key, certificate chain ) pair based on the signature
1148 * algorithms supported by the client.
XiaokangQian23c5be62022-06-07 02:04:34 +00001149 */
Ronald Cronce7d76e2022-07-08 18:56:49 +02001150MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01001151static int ssl_tls13_pick_key_cert(mbedtls_ssl_context *ssl)
XiaokangQian23c5be62022-06-07 02:04:34 +00001152{
XiaokangQianfb665a82022-06-15 03:57:21 +00001153 mbedtls_ssl_key_cert *key_cert, *key_cert_list;
XiaokangQian81802f42022-06-10 13:25:22 +00001154 const uint16_t *sig_alg = ssl->handshake->received_sig_algs;
XiaokangQian23c5be62022-06-07 02:04:34 +00001155
1156#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Gilles Peskine449bd832023-01-11 14:50:10 +01001157 if (ssl->handshake->sni_key_cert != NULL) {
XiaokangQianfb665a82022-06-15 03:57:21 +00001158 key_cert_list = ssl->handshake->sni_key_cert;
Gilles Peskine449bd832023-01-11 14:50:10 +01001159 } else
XiaokangQian23c5be62022-06-07 02:04:34 +00001160#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
Gilles Peskine449bd832023-01-11 14:50:10 +01001161 key_cert_list = ssl->conf->key_cert;
XiaokangQian23c5be62022-06-07 02:04:34 +00001162
Gilles Peskine449bd832023-01-11 14:50:10 +01001163 if (key_cert_list == NULL) {
1164 MBEDTLS_SSL_DEBUG_MSG(3, ("server has no certificate"));
1165 return -1;
XiaokangQian23c5be62022-06-07 02:04:34 +00001166 }
1167
Gilles Peskine449bd832023-01-11 14:50:10 +01001168 for (; *sig_alg != MBEDTLS_TLS1_3_SIG_NONE; sig_alg++) {
1169 if (!mbedtls_ssl_sig_alg_is_offered(ssl, *sig_alg)) {
Ronald Cron67ea2542022-09-15 17:34:42 +02001170 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +01001171 }
Ronald Cron67ea2542022-09-15 17:34:42 +02001172
Gilles Peskine449bd832023-01-11 14:50:10 +01001173 if (!mbedtls_ssl_tls13_sig_alg_for_cert_verify_is_supported(*sig_alg)) {
Ronald Cron67ea2542022-09-15 17:34:42 +02001174 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +01001175 }
Ronald Cron67ea2542022-09-15 17:34:42 +02001176
Gilles Peskine449bd832023-01-11 14:50:10 +01001177 for (key_cert = key_cert_list; key_cert != NULL;
1178 key_cert = key_cert->next) {
Ronald Cron67ea2542022-09-15 17:34:42 +02001179#if defined(MBEDTLS_USE_PSA_CRYPTO)
1180 psa_algorithm_t psa_alg = PSA_ALG_NONE;
1181#endif /* MBEDTLS_USE_PSA_CRYPTO */
1182
Gilles Peskine449bd832023-01-11 14:50:10 +01001183 MBEDTLS_SSL_DEBUG_CRT(3, "certificate (chain) candidate",
1184 key_cert->cert);
XiaokangQian81802f42022-06-10 13:25:22 +00001185
1186 /*
Gilles Peskine449bd832023-01-11 14:50:10 +01001187 * This avoids sending the client a cert it'll reject based on
1188 * keyUsage or other extensions.
1189 */
1190 if (mbedtls_x509_crt_check_key_usage(
1191 key_cert->cert, MBEDTLS_X509_KU_DIGITAL_SIGNATURE) != 0 ||
XiaokangQian81802f42022-06-10 13:25:22 +00001192 mbedtls_x509_crt_check_extended_key_usage(
XiaokangQianfb665a82022-06-15 03:57:21 +00001193 key_cert->cert, MBEDTLS_OID_SERVER_AUTH,
Gilles Peskine449bd832023-01-11 14:50:10 +01001194 MBEDTLS_OID_SIZE(MBEDTLS_OID_SERVER_AUTH)) != 0) {
1195 MBEDTLS_SSL_DEBUG_MSG(3, ("certificate mismatch: "
1196 "(extended) key usage extension"));
XiaokangQian81802f42022-06-10 13:25:22 +00001197 continue;
1198 }
XiaokangQian23c5be62022-06-07 02:04:34 +00001199
Gilles Peskine449bd832023-01-11 14:50:10 +01001200 MBEDTLS_SSL_DEBUG_MSG(3,
1201 ("ssl_tls13_pick_key_cert:"
1202 "check signature algorithm %s [%04x]",
1203 mbedtls_ssl_sig_alg_to_str(*sig_alg),
1204 *sig_alg));
Ronald Cron67ea2542022-09-15 17:34:42 +02001205#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01001206 psa_alg = ssl_tls13_iana_sig_alg_to_psa_alg(*sig_alg);
Ronald Cron67ea2542022-09-15 17:34:42 +02001207#endif /* MBEDTLS_USE_PSA_CRYPTO */
1208
Gilles Peskine449bd832023-01-11 14:50:10 +01001209 if (mbedtls_ssl_tls13_check_sig_alg_cert_key_match(
1210 *sig_alg, &key_cert->cert->pk)
Ronald Cron67ea2542022-09-15 17:34:42 +02001211#if defined(MBEDTLS_USE_PSA_CRYPTO)
1212 && psa_alg != PSA_ALG_NONE &&
Gilles Peskine449bd832023-01-11 14:50:10 +01001213 mbedtls_pk_can_do_ext(&key_cert->cert->pk, psa_alg,
1214 PSA_KEY_USAGE_SIGN_HASH) == 1
Ronald Cron67ea2542022-09-15 17:34:42 +02001215#endif /* MBEDTLS_USE_PSA_CRYPTO */
Gilles Peskine449bd832023-01-11 14:50:10 +01001216 ) {
XiaokangQianfb665a82022-06-15 03:57:21 +00001217 ssl->handshake->key_cert = key_cert;
Gilles Peskine449bd832023-01-11 14:50:10 +01001218 MBEDTLS_SSL_DEBUG_MSG(3,
1219 ("ssl_tls13_pick_key_cert:"
1220 "selected signature algorithm"
1221 " %s [%04x]",
1222 mbedtls_ssl_sig_alg_to_str(*sig_alg),
1223 *sig_alg));
XiaokangQianfb665a82022-06-15 03:57:21 +00001224 MBEDTLS_SSL_DEBUG_CRT(
Gilles Peskine449bd832023-01-11 14:50:10 +01001225 3, "selected certificate (chain)",
1226 ssl->handshake->key_cert->cert);
1227 return 0;
XiaokangQian81802f42022-06-10 13:25:22 +00001228 }
XiaokangQian81802f42022-06-10 13:25:22 +00001229 }
XiaokangQian23c5be62022-06-07 02:04:34 +00001230 }
1231
Gilles Peskine449bd832023-01-11 14:50:10 +01001232 MBEDTLS_SSL_DEBUG_MSG(2, ("ssl_tls13_pick_key_cert:"
1233 "no suitable certificate found"));
1234 return -1;
XiaokangQian23c5be62022-06-07 02:04:34 +00001235}
XiaokangQian81802f42022-06-10 13:25:22 +00001236#endif /* MBEDTLS_X509_CRT_PARSE_C &&
Ronald Cron928cbd32022-10-04 16:14:26 +02001237 MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED */
XiaokangQian23c5be62022-06-07 02:04:34 +00001238
XiaokangQian4080a7f2022-04-11 09:55:18 +00001239/*
XiaokangQianed582dd2022-04-13 08:21:05 +00001240 *
1241 * STATE HANDLING: ClientHello
1242 *
XiaokangQianb67384d2022-04-19 00:02:38 +00001243 * There are three possible classes of outcomes when parsing the ClientHello:
XiaokangQianed582dd2022-04-13 08:21:05 +00001244 *
XiaokangQianb67384d2022-04-19 00:02:38 +00001245 * 1) The ClientHello was well-formed and matched the server's configuration.
XiaokangQianed582dd2022-04-13 08:21:05 +00001246 *
1247 * In this case, the server progresses to sending its ServerHello.
1248 *
XiaokangQianb67384d2022-04-19 00:02:38 +00001249 * 2) The ClientHello was well-formed but didn't match the server's
1250 * configuration.
XiaokangQianed582dd2022-04-13 08:21:05 +00001251 *
1252 * For example, the client might not have offered a key share which
1253 * the server supports, or the server might require a cookie.
1254 *
1255 * In this case, the server sends a HelloRetryRequest.
1256 *
XiaokangQianb67384d2022-04-19 00:02:38 +00001257 * 3) The ClientHello was ill-formed
XiaokangQianed582dd2022-04-13 08:21:05 +00001258 *
1259 * In this case, we abort the handshake.
1260 *
1261 */
1262
1263/*
XiaokangQian4080a7f2022-04-11 09:55:18 +00001264 * Structure of this message:
1265 *
XiaokangQiane8ff3502022-04-22 02:34:40 +00001266 * uint16 ProtocolVersion;
1267 * opaque Random[32];
1268 * uint8 CipherSuite[2]; // Cryptographic suite selector
XiaokangQian4080a7f2022-04-11 09:55:18 +00001269 *
XiaokangQiane8ff3502022-04-22 02:34:40 +00001270 * struct {
1271 * ProtocolVersion legacy_version = 0x0303; // TLS v1.2
1272 * Random random;
1273 * opaque legacy_session_id<0..32>;
1274 * CipherSuite cipher_suites<2..2^16-2>;
1275 * opaque legacy_compression_methods<1..2^8-1>;
1276 * Extension extensions<8..2^16-1>;
1277 * } ClientHello;
XiaokangQian4080a7f2022-04-11 09:55:18 +00001278 */
XiaokangQianed582dd2022-04-13 08:21:05 +00001279
1280#define SSL_CLIENT_HELLO_OK 0
1281#define SSL_CLIENT_HELLO_HRR_REQUIRED 1
Ronald Cron5af4c7f2023-03-07 20:46:59 +01001282#define SSL_CLIENT_HELLO_TLS1_2 2
XiaokangQianed582dd2022-04-13 08:21:05 +00001283
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001284MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01001285static int ssl_tls13_parse_client_hello(mbedtls_ssl_context *ssl,
1286 const unsigned char *buf,
1287 const unsigned char *end)
XiaokangQian7807f9f2022-02-15 10:04:37 +00001288{
XiaokangQianb67384d2022-04-19 00:02:38 +00001289 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1290 const unsigned char *p = buf;
Ronald Crond540d992023-03-07 09:41:48 +01001291 const unsigned char *random;
XiaokangQian4080a7f2022-04-11 09:55:18 +00001292 size_t legacy_session_id_len;
Ronald Croncada4102023-03-07 09:51:39 +01001293 const unsigned char *legacy_session_id;
XiaokangQian318dc762022-04-20 09:43:51 +00001294 size_t cipher_suites_len;
Ronald Cron2f16b4e2023-03-07 10:07:32 +01001295 const unsigned char *cipher_suites;
XiaokangQian060d8672022-04-21 09:24:56 +00001296 const unsigned char *cipher_suites_end;
XiaokangQianb67384d2022-04-19 00:02:38 +00001297 size_t extensions_len;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001298 const unsigned char *extensions_end;
Ronald Croneff56732023-04-03 17:36:31 +02001299 const unsigned char *supported_versions_data;
1300 const unsigned char *supported_versions_data_end;
Jerry Yuc4bf5d62022-10-29 09:08:47 +08001301 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
Jerry Yu49ca9282022-05-05 11:05:22 +08001302 int hrr_required = 0;
Ronald Cron8a74f072023-06-14 17:59:29 +02001303 int no_usable_share_for_key_agreement = 0;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001304
Ronald Cron41a443a2022-10-04 16:38:25 +02001305#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED)
Jerry Yu29d9faa2022-08-23 17:52:45 +08001306 const unsigned char *pre_shared_key_ext = NULL;
Jerry Yu1c105562022-07-10 06:32:38 +00001307 const unsigned char *pre_shared_key_ext_end = NULL;
Ronald Cron41a443a2022-10-04 16:38:25 +02001308#endif
XiaokangQian7807f9f2022-02-15 10:04:37 +00001309
XiaokangQian7807f9f2022-02-15 10:04:37 +00001310 /*
XiaokangQianb67384d2022-04-19 00:02:38 +00001311 * ClientHello layout:
XiaokangQian7807f9f2022-02-15 10:04:37 +00001312 * 0 . 1 protocol version
XiaokangQiane8ff3502022-04-22 02:34:40 +00001313 * 2 . 33 random bytes
XiaokangQianc5763b52022-04-02 03:34:37 +00001314 * 34 . 34 session id length ( 1 byte )
XiaokangQian7807f9f2022-02-15 10:04:37 +00001315 * 35 . 34+x session id
XiaokangQian7807f9f2022-02-15 10:04:37 +00001316 * .. . .. ciphersuite list length ( 2 bytes )
1317 * .. . .. ciphersuite list
1318 * .. . .. compression alg. list length ( 1 byte )
1319 * .. . .. compression alg. list
1320 * .. . .. extensions length ( 2 bytes, optional )
1321 * .. . .. extensions ( optional )
1322 */
1323
XiaokangQianb67384d2022-04-19 00:02:38 +00001324 /*
bootstrap-prime6dbbf442022-05-17 19:30:44 -04001325 * Minimal length ( with everything empty and extensions omitted ) is
XiaokangQian7807f9f2022-02-15 10:04:37 +00001326 * 2 + 32 + 1 + 2 + 1 = 38 bytes. Check that first, so that we can
1327 * read at least up to session id length without worrying.
1328 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001329 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, 38);
XiaokangQian7807f9f2022-02-15 10:04:37 +00001330
1331 /* ...
1332 * ProtocolVersion legacy_version = 0x0303; // TLS 1.2
1333 * ...
1334 * with ProtocolVersion defined as:
1335 * uint16 ProtocolVersion;
1336 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001337 if (mbedtls_ssl_read_version(p, ssl->conf->transport) !=
1338 MBEDTLS_SSL_VERSION_TLS1_2) {
1339 MBEDTLS_SSL_DEBUG_MSG(1, ("Unsupported version of TLS."));
1340 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_PROTOCOL_VERSION,
1341 MBEDTLS_ERR_SSL_BAD_PROTOCOL_VERSION);
1342 return MBEDTLS_ERR_SSL_BAD_PROTOCOL_VERSION;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001343 }
1344 p += 2;
1345
Jerry Yuc1be19f2022-04-23 16:11:39 +08001346 /* ...
1347 * Random random;
1348 * ...
XiaokangQianb67384d2022-04-19 00:02:38 +00001349 * with Random defined as:
1350 * opaque Random[32];
XiaokangQian7807f9f2022-02-15 10:04:37 +00001351 */
Ronald Crond540d992023-03-07 09:41:48 +01001352 random = p;
XiaokangQian08037552022-04-20 07:16:41 +00001353 p += MBEDTLS_CLIENT_HELLO_RANDOM_LEN;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001354
Jerry Yuc1be19f2022-04-23 16:11:39 +08001355 /* ...
XiaokangQianb67384d2022-04-19 00:02:38 +00001356 * opaque legacy_session_id<0..32>;
Jerry Yuc1be19f2022-04-23 16:11:39 +08001357 * ...
XiaokangQian7807f9f2022-02-15 10:04:37 +00001358 */
Ronald Croncada4102023-03-07 09:51:39 +01001359 legacy_session_id_len = *(p++);
1360 legacy_session_id = p;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001361
XiaokangQianb67384d2022-04-19 00:02:38 +00001362 /*
1363 * Check we have enough data for the legacy session identifier
Jerry Yue95c8af2022-07-26 15:48:20 +08001364 * and the ciphersuite list length.
XiaokangQianb67384d2022-04-19 00:02:38 +00001365 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001366 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, legacy_session_id_len + 2);
XiaokangQian4080a7f2022-04-11 09:55:18 +00001367 p += legacy_session_id_len;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001368
Ronald Cron2f16b4e2023-03-07 10:07:32 +01001369 /* ...
1370 * CipherSuite cipher_suites<2..2^16-2>;
1371 * ...
1372 * with CipherSuite defined as:
1373 * uint8 CipherSuite[2];
1374 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001375 cipher_suites_len = MBEDTLS_GET_UINT16_BE(p, 0);
XiaokangQian7807f9f2022-02-15 10:04:37 +00001376 p += 2;
Ronald Cron2f16b4e2023-03-07 10:07:32 +01001377 cipher_suites = p;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001378
Ronald Cronfc7ae872023-02-16 15:32:19 +01001379 /*
1380 * The length of the ciphersuite list has to be even.
1381 */
1382 if (cipher_suites_len & 1) {
1383 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR,
1384 MBEDTLS_ERR_SSL_DECODE_ERROR);
1385 return MBEDTLS_ERR_SSL_DECODE_ERROR;
1386 }
XiaokangQian7807f9f2022-02-15 10:04:37 +00001387
XiaokangQianb67384d2022-04-19 00:02:38 +00001388 /* Check we have enough data for the ciphersuite list, the legacy
1389 * compression methods and the length of the extensions.
Jerry Yue95c8af2022-07-26 15:48:20 +08001390 *
1391 * cipher_suites cipher_suites_len bytes
1392 * legacy_compression_methods 2 bytes
1393 * extensions_len 2 bytes
XiaokangQianb67384d2022-04-19 00:02:38 +00001394 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001395 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, cipher_suites_len + 2 + 2);
Ronald Cron8c527d02023-03-07 15:47:47 +01001396 p += cipher_suites_len;
1397 cipher_suites_end = p;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001398
Jerry Yu5725f1c2022-08-21 17:27:16 +08001399 /*
Ronald Cron8c527d02023-03-07 15:47:47 +01001400 * Search for the supported versions extension and parse it to determine
1401 * if the client supports TLS 1.3.
Jerry Yu24b8c812022-08-20 19:06:56 +08001402 */
Ronald Cron8c527d02023-03-07 15:47:47 +01001403 ret = mbedtls_ssl_tls13_is_supported_versions_ext_present_in_exts(
1404 ssl, p + 2, end,
Ronald Croneff56732023-04-03 17:36:31 +02001405 &supported_versions_data, &supported_versions_data_end);
Ronald Cron8c527d02023-03-07 15:47:47 +01001406 if (ret < 0) {
1407 MBEDTLS_SSL_DEBUG_RET(1,
1408 ("mbedtls_ssl_tls13_is_supported_versions_ext_present_in_exts"), ret);
1409 return ret;
1410 }
1411
1412 if (ret == 0) {
Ronald Cron5af4c7f2023-03-07 20:46:59 +01001413 return SSL_CLIENT_HELLO_TLS1_2;
Ronald Cron8c527d02023-03-07 15:47:47 +01001414 }
1415
Ronald Cron5af4c7f2023-03-07 20:46:59 +01001416 if (ret == 1) {
1417 ret = ssl_tls13_parse_supported_versions_ext(ssl,
Ronald Croneff56732023-04-03 17:36:31 +02001418 supported_versions_data,
1419 supported_versions_data_end);
Ronald Cron5af4c7f2023-03-07 20:46:59 +01001420 if (ret < 0) {
1421 MBEDTLS_SSL_DEBUG_RET(1,
1422 ("ssl_tls13_parse_supported_versions_ext"), ret);
1423 return ret;
1424 }
1425
Ronald Cronb828c7d2023-04-03 16:37:22 +02001426 /*
1427 * The supported versions extension was parsed successfully as the
1428 * value returned by ssl_tls13_parse_supported_versions_ext() is
1429 * positive. The return value is then equal to
1430 * MBEDTLS_SSL_VERSION_TLS1_2 or MBEDTLS_SSL_VERSION_TLS1_3, defining
1431 * the TLS version to negotiate.
1432 */
Ronald Cron5af4c7f2023-03-07 20:46:59 +01001433 if (MBEDTLS_SSL_VERSION_TLS1_2 == ret) {
1434 return SSL_CLIENT_HELLO_TLS1_2;
1435 }
Ronald Cron8c527d02023-03-07 15:47:47 +01001436 }
1437
1438 /*
1439 * We negotiate TLS 1.3.
Ronald Cron64582392023-03-07 09:21:40 +01001440 */
1441 ssl->tls_version = MBEDTLS_SSL_VERSION_TLS1_3;
1442
1443#if defined(MBEDTLS_SSL_SESSION_TICKETS)
1444 /* Store minor version for later use with ticket serialization. */
1445 ssl->session_negotiate->tls_version = MBEDTLS_SSL_VERSION_TLS1_3;
1446 ssl->session_negotiate->endpoint = ssl->conf->endpoint;
XiaokangQian060d8672022-04-21 09:24:56 +00001447#endif
Ronald Cron64582392023-03-07 09:21:40 +01001448
1449 /*
Ronald Crondad02b22023-04-06 09:57:52 +02001450 * We are negotiating the version 1.3 of the protocol. Do what we have
Ronald Croncada4102023-03-07 09:51:39 +01001451 * postponed: copy of the client random bytes, copy of the legacy session
Ronald Cron2f16b4e2023-03-07 10:07:32 +01001452 * identifier and selection of the TLS 1.3 cipher suite.
Ronald Crond540d992023-03-07 09:41:48 +01001453 */
1454 MBEDTLS_SSL_DEBUG_BUF(3, "client hello, random bytes",
1455 random, MBEDTLS_CLIENT_HELLO_RANDOM_LEN);
1456 memcpy(&handshake->randbytes[0], random, MBEDTLS_CLIENT_HELLO_RANDOM_LEN);
1457
Ronald Croncada4102023-03-07 09:51:39 +01001458 if (legacy_session_id_len > sizeof(ssl->session_negotiate->id)) {
1459 MBEDTLS_SSL_DEBUG_MSG(1, ("bad client hello message"));
1460 return MBEDTLS_ERR_SSL_DECODE_ERROR;
1461 }
1462 ssl->session_negotiate->id_len = legacy_session_id_len;
1463 MBEDTLS_SSL_DEBUG_BUF(3, "client hello, session id",
1464 legacy_session_id, legacy_session_id_len);
1465 memcpy(&ssl->session_negotiate->id[0],
1466 legacy_session_id, legacy_session_id_len);
Jerry Yu5725f1c2022-08-21 17:27:16 +08001467
1468 /*
1469 * Search for a matching ciphersuite
1470 */
Ronald Cron2f16b4e2023-03-07 10:07:32 +01001471 MBEDTLS_SSL_DEBUG_BUF(3, "client hello, list of cipher suites",
1472 cipher_suites, cipher_suites_len);
Ronald Crone45afd72023-04-04 15:10:06 +02001473 for (const unsigned char *cipher_suites_p = cipher_suites;
1474 cipher_suites_p < cipher_suites_end; cipher_suites_p += 2) {
XiaokangQiane8ff3502022-04-22 02:34:40 +00001475 uint16_t cipher_suite;
Gilles Peskine449bd832023-01-11 14:50:10 +01001476 const mbedtls_ssl_ciphersuite_t *ciphersuite_info;
Jerry Yu5725f1c2022-08-21 17:27:16 +08001477
Ronald Crond89360b2023-02-21 08:53:33 +01001478 /*
Ronald Crone45afd72023-04-04 15:10:06 +02001479 * "cipher_suites_end - cipher_suites_p is even" is an invariant of the
1480 * loop. As cipher_suites_end - cipher_suites_p > 0, we have
1481 * cipher_suites_end - cipher_suites_p >= 2 and it is thus safe to read
1482 * two bytes.
Ronald Crond89360b2023-02-21 08:53:33 +01001483 */
Ronald Crone45afd72023-04-04 15:10:06 +02001484 cipher_suite = MBEDTLS_GET_UINT16_BE(cipher_suites_p, 0);
Jerry Yuc5a23a02022-08-25 10:51:44 +08001485 ciphersuite_info = ssl_tls13_validate_peer_ciphersuite(
Gilles Peskine449bd832023-01-11 14:50:10 +01001486 ssl, cipher_suite);
1487 if (ciphersuite_info == NULL) {
Jerry Yu5725f1c2022-08-21 17:27:16 +08001488 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +01001489 }
Jerry Yu5725f1c2022-08-21 17:27:16 +08001490
Jerry Yu5725f1c2022-08-21 17:27:16 +08001491 ssl->session_negotiate->ciphersuite = cipher_suite;
Jerry Yuc4bf5d62022-10-29 09:08:47 +08001492 handshake->ciphersuite_info = ciphersuite_info;
Gilles Peskine449bd832023-01-11 14:50:10 +01001493 MBEDTLS_SSL_DEBUG_MSG(2, ("selected ciphersuite: %04x - %s",
1494 cipher_suite,
1495 ciphersuite_info->name));
Ronald Cron25e9ec62023-02-16 15:35:16 +01001496 break;
XiaokangQian17f974c2022-04-19 09:57:41 +00001497 }
Jerry Yu5725f1c2022-08-21 17:27:16 +08001498
Gilles Peskine449bd832023-01-11 14:50:10 +01001499 if (handshake->ciphersuite_info == NULL) {
1500 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE,
1501 MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE);
1502 return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
Jerry Yu5725f1c2022-08-21 17:27:16 +08001503 }
Jerry Yuc1be19f2022-04-23 16:11:39 +08001504
XiaokangQian4080a7f2022-04-11 09:55:18 +00001505 /* ...
XiaokangQianb67384d2022-04-19 00:02:38 +00001506 * opaque legacy_compression_methods<1..2^8-1>;
XiaokangQian4080a7f2022-04-11 09:55:18 +00001507 * ...
XiaokangQian7807f9f2022-02-15 10:04:37 +00001508 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001509 if (p[0] != 1 || p[1] != MBEDTLS_SSL_COMPRESS_NULL) {
1510 MBEDTLS_SSL_DEBUG_MSG(1, ("bad legacy compression method"));
1511 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER,
1512 MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER);
1513 return MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001514 }
XiaokangQianb67384d2022-04-19 00:02:38 +00001515 p += 2;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001516
Jerry Yuc1be19f2022-04-23 16:11:39 +08001517 /* ...
XiaokangQianb67384d2022-04-19 00:02:38 +00001518 * Extension extensions<8..2^16-1>;
Jerry Yuc1be19f2022-04-23 16:11:39 +08001519 * ...
XiaokangQianb67384d2022-04-19 00:02:38 +00001520 * with Extension defined as:
1521 * struct {
1522 * ExtensionType extension_type;
1523 * opaque extension_data<0..2^16-1>;
1524 * } Extension;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001525 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001526 extensions_len = MBEDTLS_GET_UINT16_BE(p, 0);
XiaokangQian7807f9f2022-02-15 10:04:37 +00001527 p += 2;
Gilles Peskine449bd832023-01-11 14:50:10 +01001528 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, extensions_len);
XiaokangQiane8ff3502022-04-22 02:34:40 +00001529 extensions_end = p + extensions_len;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001530
Gilles Peskine449bd832023-01-11 14:50:10 +01001531 MBEDTLS_SSL_DEBUG_BUF(3, "client hello extensions", p, extensions_len);
Jerry Yu63a459c2022-10-31 13:38:40 +08001532 handshake->received_extensions = MBEDTLS_SSL_EXT_MASK_NONE;
Jerry Yu0c354a22022-08-29 15:25:36 +08001533
Gilles Peskine449bd832023-01-11 14:50:10 +01001534 while (p < extensions_end) {
XiaokangQian7807f9f2022-02-15 10:04:37 +00001535 unsigned int extension_type;
1536 size_t extension_data_len;
1537 const unsigned char *extension_data_end;
1538
Jerry Yu0c354a22022-08-29 15:25:36 +08001539 /* RFC 8446, section 4.2.11
Jerry Yu1c9247c2022-07-21 12:37:39 +08001540 *
1541 * The "pre_shared_key" extension MUST be the last extension in the
1542 * ClientHello (this facilitates implementation as described below).
1543 * Servers MUST check that it is the last extension and otherwise fail
1544 * the handshake with an "illegal_parameter" alert.
1545 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001546 if (handshake->received_extensions & MBEDTLS_SSL_EXT_MASK(PRE_SHARED_KEY)) {
Jerry Yu1c9247c2022-07-21 12:37:39 +08001547 MBEDTLS_SSL_DEBUG_MSG(
Gilles Peskine449bd832023-01-11 14:50:10 +01001548 3, ("pre_shared_key is not last extension."));
Jerry Yu1c9247c2022-07-21 12:37:39 +08001549 MBEDTLS_SSL_PEND_FATAL_ALERT(
1550 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER,
Gilles Peskine449bd832023-01-11 14:50:10 +01001551 MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER);
1552 return MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER;
Jerry Yu1c9247c2022-07-21 12:37:39 +08001553 }
1554
Gilles Peskine449bd832023-01-11 14:50:10 +01001555 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, extensions_end, 4);
1556 extension_type = MBEDTLS_GET_UINT16_BE(p, 0);
1557 extension_data_len = MBEDTLS_GET_UINT16_BE(p, 2);
XiaokangQian7807f9f2022-02-15 10:04:37 +00001558 p += 4;
1559
Gilles Peskine449bd832023-01-11 14:50:10 +01001560 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, extensions_end, extension_data_len);
XiaokangQian7807f9f2022-02-15 10:04:37 +00001561 extension_data_end = p + extension_data_len;
1562
Jerry Yuc4bf5d62022-10-29 09:08:47 +08001563 ret = mbedtls_ssl_tls13_check_received_extension(
Gilles Peskine449bd832023-01-11 14:50:10 +01001564 ssl, MBEDTLS_SSL_HS_CLIENT_HELLO, extension_type,
1565 MBEDTLS_SSL_TLS1_3_ALLOWED_EXTS_OF_CH);
1566 if (ret != 0) {
1567 return ret;
1568 }
Jerry Yue18dc7e2022-08-04 16:29:22 +08001569
Gilles Peskine449bd832023-01-11 14:50:10 +01001570 switch (extension_type) {
XiaokangQian40a35232022-05-07 09:02:40 +00001571#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
1572 case MBEDTLS_TLS_EXT_SERVERNAME:
Gilles Peskine449bd832023-01-11 14:50:10 +01001573 MBEDTLS_SSL_DEBUG_MSG(3, ("found ServerName extension"));
1574 ret = mbedtls_ssl_parse_server_name_ext(ssl, p,
1575 extension_data_end);
1576 if (ret != 0) {
XiaokangQian40a35232022-05-07 09:02:40 +00001577 MBEDTLS_SSL_DEBUG_RET(
Gilles Peskine449bd832023-01-11 14:50:10 +01001578 1, "mbedtls_ssl_parse_servername_ext", ret);
1579 return ret;
XiaokangQian40a35232022-05-07 09:02:40 +00001580 }
XiaokangQian40a35232022-05-07 09:02:40 +00001581 break;
1582#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
1583
Przemek Stekiel8c0a9532023-06-15 16:48:19 +02001584#if defined(PSA_WANT_ALG_ECDH) || defined(PSA_WANT_ALG_FFDH)
XiaokangQian7807f9f2022-02-15 10:04:37 +00001585 case MBEDTLS_TLS_EXT_SUPPORTED_GROUPS:
Gilles Peskine449bd832023-01-11 14:50:10 +01001586 MBEDTLS_SSL_DEBUG_MSG(3, ("found supported group extension"));
XiaokangQian7807f9f2022-02-15 10:04:37 +00001587
1588 /* Supported Groups Extension
1589 *
1590 * When sent by the client, the "supported_groups" extension
1591 * indicates the named groups which the client supports,
1592 * ordered from most preferred to least preferred.
1593 */
Jerry Yuc1be19f2022-04-23 16:11:39 +08001594 ret = ssl_tls13_parse_supported_groups_ext(
Gilles Peskine449bd832023-01-11 14:50:10 +01001595 ssl, p, extension_data_end);
1596 if (ret != 0) {
Xiaokang Qian9f1747b2023-03-30 02:50:04 +00001597 MBEDTLS_SSL_DEBUG_RET(
Xiaokang Qian8bce0e62023-04-04 10:15:48 +00001598 1, "ssl_tls13_parse_supported_groups_ext", ret);
Gilles Peskine449bd832023-01-11 14:50:10 +01001599 return ret;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001600 }
1601
XiaokangQian7807f9f2022-02-15 10:04:37 +00001602 break;
Przemek Stekiel8c0a9532023-06-15 16:48:19 +02001603#endif /* PSA_WANT_ALG_ECDH || PSA_WANT_ALG_FFDH*/
XiaokangQian7807f9f2022-02-15 10:04:37 +00001604
Valerio Settic9ae8622023-07-25 11:23:50 +02001605#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_EPHEMERAL_ENABLED)
XiaokangQian7807f9f2022-02-15 10:04:37 +00001606 case MBEDTLS_TLS_EXT_KEY_SHARE:
Gilles Peskine449bd832023-01-11 14:50:10 +01001607 MBEDTLS_SSL_DEBUG_MSG(3, ("found key share extension"));
XiaokangQian7807f9f2022-02-15 10:04:37 +00001608
1609 /*
1610 * Key Share Extension
1611 *
1612 * When sent by the client, the "key_share" extension
1613 * contains the endpoint's cryptographic parameters for
1614 * ECDHE/DHE key establishment methods.
1615 */
Jerry Yuc1be19f2022-04-23 16:11:39 +08001616 ret = ssl_tls13_parse_key_shares_ext(
Gilles Peskine449bd832023-01-11 14:50:10 +01001617 ssl, p, extension_data_end);
1618 if (ret == SSL_TLS1_3_PARSE_KEY_SHARES_EXT_NO_MATCH) {
Ronald Cron8a74f072023-06-14 17:59:29 +02001619 MBEDTLS_SSL_DEBUG_MSG(2, ("No usable share for key agreement."));
1620 no_usable_share_for_key_agreement = 1;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001621 }
1622
Gilles Peskine449bd832023-01-11 14:50:10 +01001623 if (ret < 0) {
Jerry Yu582dd062022-04-22 21:59:01 +08001624 MBEDTLS_SSL_DEBUG_RET(
Gilles Peskine449bd832023-01-11 14:50:10 +01001625 1, "ssl_tls13_parse_key_shares_ext", ret);
1626 return ret;
Jerry Yu582dd062022-04-22 21:59:01 +08001627 }
XiaokangQian7807f9f2022-02-15 10:04:37 +00001628
XiaokangQian7807f9f2022-02-15 10:04:37 +00001629 break;
Valerio Settic9ae8622023-07-25 11:23:50 +02001630#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_EPHEMERAL_ENABLED */
XiaokangQian7807f9f2022-02-15 10:04:37 +00001631
1632 case MBEDTLS_TLS_EXT_SUPPORTED_VERSIONS:
Ronald Cron8c527d02023-03-07 15:47:47 +01001633 /* Already parsed */
XiaokangQian7807f9f2022-02-15 10:04:37 +00001634 break;
1635
Ronald Cron41a443a2022-10-04 16:38:25 +02001636#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED)
Jerry Yue19e3b92022-07-08 12:04:51 +00001637 case MBEDTLS_TLS_EXT_PSK_KEY_EXCHANGE_MODES:
Xiaokang Qian9f1747b2023-03-30 02:50:04 +00001638 MBEDTLS_SSL_DEBUG_MSG(
1639 3, ("found psk key exchange modes extension"));
Jerry Yue19e3b92022-07-08 12:04:51 +00001640
1641 ret = ssl_tls13_parse_key_exchange_modes_ext(
Gilles Peskine449bd832023-01-11 14:50:10 +01001642 ssl, p, extension_data_end);
1643 if (ret != 0) {
Jerry Yue19e3b92022-07-08 12:04:51 +00001644 MBEDTLS_SSL_DEBUG_RET(
Gilles Peskine449bd832023-01-11 14:50:10 +01001645 1, "ssl_tls13_parse_key_exchange_modes_ext", ret);
1646 return ret;
Jerry Yue19e3b92022-07-08 12:04:51 +00001647 }
1648
Jerry Yue19e3b92022-07-08 12:04:51 +00001649 break;
Ronald Cron41a443a2022-10-04 16:38:25 +02001650#endif
Jerry Yue19e3b92022-07-08 12:04:51 +00001651
Jerry Yu1c105562022-07-10 06:32:38 +00001652 case MBEDTLS_TLS_EXT_PRE_SHARED_KEY:
Gilles Peskine449bd832023-01-11 14:50:10 +01001653 MBEDTLS_SSL_DEBUG_MSG(3, ("found pre_shared_key extension"));
1654 if ((handshake->received_extensions &
1655 MBEDTLS_SSL_EXT_MASK(PSK_KEY_EXCHANGE_MODES)) == 0) {
Jerry Yu13ab81d2022-07-22 23:17:11 +08001656 MBEDTLS_SSL_PEND_FATAL_ALERT(
1657 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER,
Gilles Peskine449bd832023-01-11 14:50:10 +01001658 MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER);
1659 return MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER;
Jerry Yu13ab81d2022-07-22 23:17:11 +08001660 }
Ronald Cron41a443a2022-10-04 16:38:25 +02001661#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED)
Jerry Yu1c105562022-07-10 06:32:38 +00001662 /* Delay processing of the PSK identity once we have
1663 * found out which algorithms to use. We keep a pointer
1664 * to the buffer and the size for later processing.
1665 */
Jerry Yu29d9faa2022-08-23 17:52:45 +08001666 pre_shared_key_ext = p;
Jerry Yu1c105562022-07-10 06:32:38 +00001667 pre_shared_key_ext_end = extension_data_end;
Jerry Yue18dc7e2022-08-04 16:29:22 +08001668#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED */
Jerry Yu1c105562022-07-10 06:32:38 +00001669 break;
Jerry Yu1c105562022-07-10 06:32:38 +00001670
XiaokangQianacb39922022-06-17 10:18:48 +00001671#if defined(MBEDTLS_SSL_ALPN)
1672 case MBEDTLS_TLS_EXT_ALPN:
Gilles Peskine449bd832023-01-11 14:50:10 +01001673 MBEDTLS_SSL_DEBUG_MSG(3, ("found alpn extension"));
XiaokangQianacb39922022-06-17 10:18:48 +00001674
Gilles Peskine449bd832023-01-11 14:50:10 +01001675 ret = mbedtls_ssl_parse_alpn_ext(ssl, p, extension_data_end);
1676 if (ret != 0) {
XiaokangQianacb39922022-06-17 10:18:48 +00001677 MBEDTLS_SSL_DEBUG_RET(
Gilles Peskine449bd832023-01-11 14:50:10 +01001678 1, ("mbedtls_ssl_parse_alpn_ext"), ret);
1679 return ret;
XiaokangQianacb39922022-06-17 10:18:48 +00001680 }
XiaokangQianacb39922022-06-17 10:18:48 +00001681 break;
1682#endif /* MBEDTLS_SSL_ALPN */
1683
Ronald Cron928cbd32022-10-04 16:14:26 +02001684#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED)
XiaokangQian7807f9f2022-02-15 10:04:37 +00001685 case MBEDTLS_TLS_EXT_SIG_ALG:
Gilles Peskine449bd832023-01-11 14:50:10 +01001686 MBEDTLS_SSL_DEBUG_MSG(3, ("found signature_algorithms extension"));
XiaokangQian7807f9f2022-02-15 10:04:37 +00001687
Gabor Mezei078e8032022-04-27 21:17:56 +02001688 ret = mbedtls_ssl_parse_sig_alg_ext(
Gilles Peskine449bd832023-01-11 14:50:10 +01001689 ssl, p, extension_data_end);
1690 if (ret != 0) {
Xiaokang Qian49f39c12023-04-06 02:31:02 +00001691 MBEDTLS_SSL_DEBUG_RET(
1692 1, "mbedtls_ssl_parse_sig_alg_ext", ret);
Gilles Peskine449bd832023-01-11 14:50:10 +01001693 return ret;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001694 }
XiaokangQian7807f9f2022-02-15 10:04:37 +00001695 break;
Ronald Cron928cbd32022-10-04 16:14:26 +02001696#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED */
XiaokangQian7807f9f2022-02-15 10:04:37 +00001697
Jan Bruckner151f6422023-02-10 12:45:19 +01001698#if defined(MBEDTLS_SSL_RECORD_SIZE_LIMIT)
1699 case MBEDTLS_TLS_EXT_RECORD_SIZE_LIMIT:
1700 MBEDTLS_SSL_DEBUG_MSG(3, ("found record_size_limit extension"));
1701
Xiaokang Qian9f1747b2023-03-30 02:50:04 +00001702 ret = mbedtls_ssl_tls13_parse_record_size_limit_ext(
1703 ssl, p, extension_data_end);
Jan Bruckner151f6422023-02-10 12:45:19 +01001704
Xiaokang Qian09c3ccc2023-04-04 10:18:38 +00001705 /*
1706 * TODO: Return unconditionally here until we handle the record
1707 * size limit correctly.
1708 * Once handled correctly, only return in case of errors.
1709 */
Jan Bruckner151f6422023-02-10 12:45:19 +01001710 return ret;
1711
1712 break;
1713#endif /* MBEDTLS_SSL_RECORD_SIZE_LIMIT */
1714
XiaokangQian7807f9f2022-02-15 10:04:37 +00001715 default:
Jerry Yu79aa7212022-11-08 21:30:21 +08001716 MBEDTLS_SSL_PRINT_EXT(
Jerry Yu63a459c2022-10-31 13:38:40 +08001717 3, MBEDTLS_SSL_HS_CLIENT_HELLO,
Gilles Peskine449bd832023-01-11 14:50:10 +01001718 extension_type, "( ignored )");
Jerry Yu0c354a22022-08-29 15:25:36 +08001719 break;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001720 }
1721
1722 p += extension_data_len;
1723 }
1724
Gilles Peskine449bd832023-01-11 14:50:10 +01001725 MBEDTLS_SSL_PRINT_EXTS(3, MBEDTLS_SSL_HS_CLIENT_HELLO,
1726 handshake->received_extensions);
Jerry Yue18dc7e2022-08-04 16:29:22 +08001727
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +01001728 ret = mbedtls_ssl_add_hs_hdr_to_checksum(ssl,
1729 MBEDTLS_SSL_HS_CLIENT_HELLO,
1730 p - buf);
1731 if (0 != ret) {
1732 MBEDTLS_SSL_DEBUG_RET(1, ("mbedtls_ssl_add_hs_hdr_to_checksum"), ret);
1733 return ret;
1734 }
Jerry Yu032b15ce2022-07-11 06:10:03 +00001735
Ronald Cron41a443a2022-10-04 16:38:25 +02001736#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED)
XiaokangQian7807f9f2022-02-15 10:04:37 +00001737 /* Update checksum with either
1738 * - The entire content of the CH message, if no PSK extension is present
1739 * - The content up to but excluding the PSK extension, if present.
1740 */
Jerry Yu1c105562022-07-10 06:32:38 +00001741 /* If we've settled on a PSK-based exchange, parse PSK identity ext */
Pengyu Lvcfb23b82023-10-30 15:26:26 +08001742 if (ssl_tls13_check_psk_key_exchange(ssl) ||
1743 ssl_tls13_check_psk_ephemeral_key_exchange(ssl)) {
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +01001744 ret = handshake->update_checksum(ssl, buf,
1745 pre_shared_key_ext - buf);
1746 if (0 != ret) {
1747 MBEDTLS_SSL_DEBUG_RET(1, ("update_checksum"), ret);
1748 return ret;
1749 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001750 ret = ssl_tls13_parse_pre_shared_key_ext(ssl,
1751 pre_shared_key_ext,
1752 pre_shared_key_ext_end,
1753 cipher_suites,
1754 cipher_suites_end);
1755 if (ret == MBEDTLS_ERR_SSL_UNKNOWN_IDENTITY) {
1756 handshake->received_extensions &= ~MBEDTLS_SSL_EXT_MASK(PRE_SHARED_KEY);
1757 } else if (ret != 0) {
Jerry Yu63a459c2022-10-31 13:38:40 +08001758 MBEDTLS_SSL_DEBUG_RET(
Gilles Peskine449bd832023-01-11 14:50:10 +01001759 1, "ssl_tls13_parse_pre_shared_key_ext", ret);
1760 return ret;
Jerry Yu1c105562022-07-10 06:32:38 +00001761 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001762 } else
Ronald Cron41a443a2022-10-04 16:38:25 +02001763#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED */
Jerry Yu1c105562022-07-10 06:32:38 +00001764 {
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +01001765 ret = handshake->update_checksum(ssl, buf, p - buf);
1766 if (0 != ret) {
1767 MBEDTLS_SSL_DEBUG_RET(1, ("update_checksum"), ret);
1768 return ret;
1769 }
Jerry Yu1c105562022-07-10 06:32:38 +00001770 }
XiaokangQian7807f9f2022-02-15 10:04:37 +00001771
Gilles Peskine449bd832023-01-11 14:50:10 +01001772 ret = ssl_tls13_determine_key_exchange_mode(ssl);
1773 if (ret < 0) {
1774 return ret;
1775 }
XiaokangQian7807f9f2022-02-15 10:04:37 +00001776
Ronald Cron8a74f072023-06-14 17:59:29 +02001777 if (ssl->handshake->key_exchange_mode !=
1778 MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK) {
1779 hrr_required = (no_usable_share_for_key_agreement != 0);
1780 }
1781
Gilles Peskine449bd832023-01-11 14:50:10 +01001782 mbedtls_ssl_optimize_checksum(ssl, handshake->ciphersuite_info);
Jerry Yue5834fd2022-08-29 20:16:09 +08001783
Gilles Peskine449bd832023-01-11 14:50:10 +01001784 return hrr_required ? SSL_CLIENT_HELLO_HRR_REQUIRED : SSL_CLIENT_HELLO_OK;
XiaokangQian75fe8c72022-06-15 09:42:45 +00001785}
1786
1787/* Update the handshake state machine */
1788
Ronald Cronce7d76e2022-07-08 18:56:49 +02001789MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01001790static int ssl_tls13_postprocess_client_hello(mbedtls_ssl_context *ssl)
XiaokangQian75fe8c72022-06-15 09:42:45 +00001791{
1792 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1793
XiaokangQian7807f9f2022-02-15 10:04:37 +00001794 /*
XiaokangQianfb665a82022-06-15 03:57:21 +00001795 * Server certificate selection
1796 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001797 if (ssl->conf->f_cert_cb && (ret = ssl->conf->f_cert_cb(ssl)) != 0) {
1798 MBEDTLS_SSL_DEBUG_RET(1, "f_cert_cb", ret);
1799 return ret;
XiaokangQianfb665a82022-06-15 03:57:21 +00001800 }
1801#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
1802 ssl->handshake->sni_name = NULL;
1803 ssl->handshake->sni_name_len = 0;
1804#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
XiaokangQian7807f9f2022-02-15 10:04:37 +00001805
Gilles Peskine449bd832023-01-11 14:50:10 +01001806 ret = mbedtls_ssl_tls13_key_schedule_stage_early(ssl);
1807 if (ret != 0) {
1808 MBEDTLS_SSL_DEBUG_RET(1,
1809 "mbedtls_ssl_tls1_3_key_schedule_stage_early", ret);
1810 return ret;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001811 }
1812
Gilles Peskine449bd832023-01-11 14:50:10 +01001813 return 0;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001814
1815}
1816
1817/*
XiaokangQianed582dd2022-04-13 08:21:05 +00001818 * Main entry point from the state machine; orchestrates the otherfunctions.
1819 */
1820
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001821MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01001822static int ssl_tls13_process_client_hello(mbedtls_ssl_context *ssl)
XiaokangQianed582dd2022-04-13 08:21:05 +00001823{
1824
XiaokangQian08037552022-04-20 07:16:41 +00001825 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine449bd832023-01-11 14:50:10 +01001826 unsigned char *buf = NULL;
XiaokangQianed582dd2022-04-13 08:21:05 +00001827 size_t buflen = 0;
Jerry Yu4ca91402022-05-09 15:50:57 +08001828 int parse_client_hello_ret;
1829
Gilles Peskine449bd832023-01-11 14:50:10 +01001830 MBEDTLS_SSL_DEBUG_MSG(2, ("=> parse client hello"));
XiaokangQianed582dd2022-04-13 08:21:05 +00001831
Gilles Peskine449bd832023-01-11 14:50:10 +01001832 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_tls13_fetch_handshake_msg(
1833 ssl, MBEDTLS_SSL_HS_CLIENT_HELLO,
1834 &buf, &buflen));
XiaokangQianed582dd2022-04-13 08:21:05 +00001835
Gilles Peskine449bd832023-01-11 14:50:10 +01001836 MBEDTLS_SSL_PROC_CHK_NEG(ssl_tls13_parse_client_hello(ssl, buf,
1837 buf + buflen));
Ronald Cron5af4c7f2023-03-07 20:46:59 +01001838 parse_client_hello_ret = ret; /* Store positive return value of
1839 * parse_client_hello,
1840 * as negative error codes are handled
Jerry Yuf41553b2022-05-09 22:20:30 +08001841 * by MBEDTLS_SSL_PROC_CHK_NEG. */
Jerry Yu4ca91402022-05-09 15:50:57 +08001842
Ronald Cronb828c7d2023-04-03 16:37:22 +02001843 /*
1844 * Version 1.2 of the protocol has been chosen, set the
1845 * ssl->keep_current_message flag for the ClientHello to be kept and parsed
1846 * as a TLS 1.2 ClientHello. We also change ssl->tls_version to
1847 * MBEDTLS_SSL_VERSION_TLS1_2 thus from now on mbedtls_ssl_handshake_step()
1848 * will dispatch to the TLS 1.2 state machine.
1849 */
Ronald Cron5af4c7f2023-03-07 20:46:59 +01001850 if (SSL_CLIENT_HELLO_TLS1_2 == parse_client_hello_ret) {
1851 ssl->keep_current_message = 1;
1852 ssl->tls_version = MBEDTLS_SSL_VERSION_TLS1_2;
1853 return 0;
1854 }
1855
Gilles Peskine449bd832023-01-11 14:50:10 +01001856 MBEDTLS_SSL_PROC_CHK(ssl_tls13_postprocess_client_hello(ssl));
Jerry Yu582dd062022-04-22 21:59:01 +08001857
Ronald Cron5af4c7f2023-03-07 20:46:59 +01001858 if (SSL_CLIENT_HELLO_OK == parse_client_hello_ret) {
Gilles Peskine449bd832023-01-11 14:50:10 +01001859 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_SERVER_HELLO);
1860 } else {
1861 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_HELLO_RETRY_REQUEST);
1862 }
XiaokangQianed582dd2022-04-13 08:21:05 +00001863
1864cleanup:
1865
Gilles Peskine449bd832023-01-11 14:50:10 +01001866 MBEDTLS_SSL_DEBUG_MSG(2, ("<= parse client hello"));
1867 return ret;
XiaokangQianed582dd2022-04-13 08:21:05 +00001868}
1869
1870/*
Jerry Yu1c3e6882022-04-20 21:23:40 +08001871 * Handler for MBEDTLS_SSL_SERVER_HELLO
Jerry Yu5b64ae92022-03-30 17:15:02 +08001872 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001873MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01001874static int ssl_tls13_prepare_server_hello(mbedtls_ssl_context *ssl)
Jerry Yuf4b27e42022-03-30 17:32:21 +08001875{
Jerry Yu637a3f12022-04-20 21:37:58 +08001876 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1877 unsigned char *server_randbytes =
Gilles Peskine449bd832023-01-11 14:50:10 +01001878 ssl->handshake->randbytes + MBEDTLS_CLIENT_HELLO_RANDOM_LEN;
1879 if (ssl->conf->f_rng == NULL) {
1880 MBEDTLS_SSL_DEBUG_MSG(1, ("no RNG provided"));
1881 return MBEDTLS_ERR_SSL_NO_RNG;
Jerry Yuf4b27e42022-03-30 17:32:21 +08001882 }
1883
Gilles Peskine449bd832023-01-11 14:50:10 +01001884 if ((ret = ssl->conf->f_rng(ssl->conf->p_rng, server_randbytes,
1885 MBEDTLS_SERVER_HELLO_RANDOM_LEN)) != 0) {
1886 MBEDTLS_SSL_DEBUG_RET(1, "f_rng", ret);
1887 return ret;
Jerry Yuf4b27e42022-03-30 17:32:21 +08001888 }
1889
Gilles Peskine449bd832023-01-11 14:50:10 +01001890 MBEDTLS_SSL_DEBUG_BUF(3, "server hello, random bytes", server_randbytes,
1891 MBEDTLS_SERVER_HELLO_RANDOM_LEN);
Jerry Yuf4b27e42022-03-30 17:32:21 +08001892
1893#if defined(MBEDTLS_HAVE_TIME)
YxCda609132023-05-22 12:08:12 -07001894 ssl->session_negotiate->start = mbedtls_time(NULL);
Jerry Yuf4b27e42022-03-30 17:32:21 +08001895#endif /* MBEDTLS_HAVE_TIME */
1896
Gilles Peskine449bd832023-01-11 14:50:10 +01001897 return ret;
Jerry Yuf4b27e42022-03-30 17:32:21 +08001898}
1899
Jerry Yu3bf2c642022-03-30 22:02:12 +08001900/*
Jerry Yue74e04a2022-04-21 09:23:16 +08001901 * ssl_tls13_write_server_hello_supported_versions_ext ():
Jerry Yu3bf2c642022-03-30 22:02:12 +08001902 *
1903 * struct {
Jerry Yufb9f54d2022-04-06 10:08:34 +08001904 * ProtocolVersion selected_version;
Jerry Yu3bf2c642022-03-30 22:02:12 +08001905 * } SupportedVersions;
1906 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001907MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yue74e04a2022-04-21 09:23:16 +08001908static int ssl_tls13_write_server_hello_supported_versions_ext(
Gilles Peskine449bd832023-01-11 14:50:10 +01001909 mbedtls_ssl_context *ssl,
1910 unsigned char *buf,
1911 unsigned char *end,
1912 size_t *out_len)
Jerry Yu3bf2c642022-03-30 22:02:12 +08001913{
Jerry Yu3bf2c642022-03-30 22:02:12 +08001914 *out_len = 0;
1915
Gilles Peskine449bd832023-01-11 14:50:10 +01001916 MBEDTLS_SSL_DEBUG_MSG(3, ("server hello, write selected version"));
Jerry Yu3bf2c642022-03-30 22:02:12 +08001917
1918 /* Check if we have space to write the extension:
1919 * - extension_type (2 bytes)
1920 * - extension_data_length (2 bytes)
Jerry Yu349a6132022-04-14 20:52:56 +08001921 * - selected_version (2 bytes)
Jerry Yu3bf2c642022-03-30 22:02:12 +08001922 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001923 MBEDTLS_SSL_CHK_BUF_PTR(buf, end, 6);
Jerry Yu3bf2c642022-03-30 22:02:12 +08001924
Gilles Peskine449bd832023-01-11 14:50:10 +01001925 MBEDTLS_PUT_UINT16_BE(MBEDTLS_TLS_EXT_SUPPORTED_VERSIONS, buf, 0);
Jerry Yu3bf2c642022-03-30 22:02:12 +08001926
Gilles Peskine449bd832023-01-11 14:50:10 +01001927 MBEDTLS_PUT_UINT16_BE(2, buf, 2);
Jerry Yu3bf2c642022-03-30 22:02:12 +08001928
Gilles Peskine449bd832023-01-11 14:50:10 +01001929 mbedtls_ssl_write_version(buf + 4,
1930 ssl->conf->transport,
1931 ssl->tls_version);
Jerry Yu3bf2c642022-03-30 22:02:12 +08001932
Gilles Peskine449bd832023-01-11 14:50:10 +01001933 MBEDTLS_SSL_DEBUG_MSG(3, ("supported version: [%04x]",
1934 ssl->tls_version));
Jerry Yu3bf2c642022-03-30 22:02:12 +08001935
Jerry Yu349a6132022-04-14 20:52:56 +08001936 *out_len = 6;
Jerry Yu3bf2c642022-03-30 22:02:12 +08001937
Jerry Yub95dd362022-11-08 21:19:34 +08001938 mbedtls_ssl_tls13_set_hs_sent_ext_mask(
Gilles Peskine449bd832023-01-11 14:50:10 +01001939 ssl, MBEDTLS_TLS_EXT_SUPPORTED_VERSIONS);
Jerry Yub95dd362022-11-08 21:19:34 +08001940
Gilles Peskine449bd832023-01-11 14:50:10 +01001941 return 0;
Jerry Yu3bf2c642022-03-30 22:02:12 +08001942}
1943
Jerry Yud9436a12022-04-20 22:28:09 +08001944
Jerry Yu3bf2c642022-03-30 22:02:12 +08001945
1946/* Generate and export a single key share. For hybrid KEMs, this can
1947 * be called multiple times with the different components of the hybrid. */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001948MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01001949static int ssl_tls13_generate_and_write_key_share(mbedtls_ssl_context *ssl,
1950 uint16_t named_group,
1951 unsigned char *buf,
1952 unsigned char *end,
1953 size_t *out_len)
Jerry Yu3bf2c642022-03-30 22:02:12 +08001954{
1955 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yu955ddd72022-04-22 22:27:33 +08001956
1957 *out_len = 0;
1958
Valerio Settic9ae8622023-07-25 11:23:50 +02001959#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_EPHEMERAL_ENABLED)
Przemek Stekiel29c219c2023-05-31 15:21:04 +02001960 if (mbedtls_ssl_tls13_named_group_is_ecdhe(named_group) ||
Przemek Stekield5f79e72023-06-29 09:08:43 +02001961 mbedtls_ssl_tls13_named_group_is_ffdh(named_group)) {
Przemek Stekiel408569f2023-07-06 11:26:44 +02001962 ret = mbedtls_ssl_tls13_generate_and_write_xxdh_key_exchange(
Gilles Peskine449bd832023-01-11 14:50:10 +01001963 ssl, named_group, buf, end, out_len);
1964 if (ret != 0) {
Jerry Yu89e103c2022-03-30 22:43:29 +08001965 MBEDTLS_SSL_DEBUG_RET(
Przemek Stekiel408569f2023-07-06 11:26:44 +02001966 1, "mbedtls_ssl_tls13_generate_and_write_xxdh_key_exchange",
Gilles Peskine449bd832023-01-11 14:50:10 +01001967 ret);
1968 return ret;
Jerry Yu3bf2c642022-03-30 22:02:12 +08001969 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001970 } else
Valerio Settic9ae8622023-07-25 11:23:50 +02001971#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_EPHEMERAL_ENABLED */
Gilles Peskine449bd832023-01-11 14:50:10 +01001972 if (0 /* Other kinds of KEMs */) {
1973 } else {
Jerry Yu955ddd72022-04-22 22:27:33 +08001974 ((void) ssl);
1975 ((void) named_group);
1976 ((void) buf);
1977 ((void) end);
Jerry Yu3bf2c642022-03-30 22:02:12 +08001978 ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
1979 }
1980
Gilles Peskine449bd832023-01-11 14:50:10 +01001981 return ret;
Jerry Yu3bf2c642022-03-30 22:02:12 +08001982}
1983
1984/*
1985 * ssl_tls13_write_key_share_ext
1986 *
1987 * Structure of key_share extension in ServerHello:
1988 *
Jerry Yu1c3e6882022-04-20 21:23:40 +08001989 * struct {
1990 * NamedGroup group;
1991 * opaque key_exchange<1..2^16-1>;
1992 * } KeyShareEntry;
1993 * struct {
1994 * KeyShareEntry server_share;
1995 * } KeyShareServerHello;
Jerry Yu3bf2c642022-03-30 22:02:12 +08001996 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001997MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01001998static int ssl_tls13_write_key_share_ext(mbedtls_ssl_context *ssl,
1999 unsigned char *buf,
2000 unsigned char *end,
2001 size_t *out_len)
Jerry Yu3bf2c642022-03-30 22:02:12 +08002002{
Jerry Yu955ddd72022-04-22 22:27:33 +08002003 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yu3bf2c642022-03-30 22:02:12 +08002004 unsigned char *p = buf;
Jerry Yu57d48412022-04-20 21:50:42 +08002005 uint16_t group = ssl->handshake->offered_group_id;
Jerry Yu3bf2c642022-03-30 22:02:12 +08002006 unsigned char *server_share = buf + 4;
Jerry Yu3bf2c642022-03-30 22:02:12 +08002007 size_t key_exchange_length;
Jerry Yu3bf2c642022-03-30 22:02:12 +08002008
2009 *out_len = 0;
2010
Gilles Peskine449bd832023-01-11 14:50:10 +01002011 MBEDTLS_SSL_DEBUG_MSG(3, ("server hello, adding key share extension"));
Jerry Yu3bf2c642022-03-30 22:02:12 +08002012
Gilles Peskine449bd832023-01-11 14:50:10 +01002013 MBEDTLS_SSL_DEBUG_MSG(2, ("server hello, write selected_group: %s (%04x)",
2014 mbedtls_ssl_named_group_to_str(group),
2015 group));
Jerry Yu58af2332022-09-06 11:19:31 +08002016
Jerry Yu3bf2c642022-03-30 22:02:12 +08002017 /* Check if we have space for header and length fields:
2018 * - extension_type (2 bytes)
2019 * - extension_data_length (2 bytes)
2020 * - group (2 bytes)
2021 * - key_exchange_length (2 bytes)
2022 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002023 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 8);
2024 MBEDTLS_PUT_UINT16_BE(MBEDTLS_TLS_EXT_KEY_SHARE, p, 0);
2025 MBEDTLS_PUT_UINT16_BE(group, server_share, 0);
Jerry Yu3bf2c642022-03-30 22:02:12 +08002026 p += 8;
Jerry Yu57d48412022-04-20 21:50:42 +08002027
Jerry Yu3bf2c642022-03-30 22:02:12 +08002028 /* When we introduce PQC-ECDHE hybrids, we'll want to call this
2029 * function multiple times. */
Jerry Yu955ddd72022-04-22 22:27:33 +08002030 ret = ssl_tls13_generate_and_write_key_share(
Gilles Peskine449bd832023-01-11 14:50:10 +01002031 ssl, group, server_share + 4, end, &key_exchange_length);
2032 if (ret != 0) {
2033 return ret;
2034 }
Jerry Yu3bf2c642022-03-30 22:02:12 +08002035 p += key_exchange_length;
Jerry Yuc1be19f2022-04-23 16:11:39 +08002036
Gilles Peskine449bd832023-01-11 14:50:10 +01002037 MBEDTLS_PUT_UINT16_BE(key_exchange_length, server_share + 2, 0);
Jerry Yu3bf2c642022-03-30 22:02:12 +08002038
Gilles Peskine449bd832023-01-11 14:50:10 +01002039 MBEDTLS_PUT_UINT16_BE(p - server_share, buf, 2);
Jerry Yu3bf2c642022-03-30 22:02:12 +08002040
Jerry Yu57d48412022-04-20 21:50:42 +08002041 *out_len = p - buf;
Jerry Yu955ddd72022-04-22 22:27:33 +08002042
Gilles Peskine449bd832023-01-11 14:50:10 +01002043 mbedtls_ssl_tls13_set_hs_sent_ext_mask(ssl, MBEDTLS_TLS_EXT_KEY_SHARE);
Jerry Yub95dd362022-11-08 21:19:34 +08002044
Gilles Peskine449bd832023-01-11 14:50:10 +01002045 return 0;
Jerry Yu3bf2c642022-03-30 22:02:12 +08002046}
Jerry Yud9436a12022-04-20 22:28:09 +08002047
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002048MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002049static int ssl_tls13_write_hrr_key_share_ext(mbedtls_ssl_context *ssl,
2050 unsigned char *buf,
2051 unsigned char *end,
2052 size_t *out_len)
Jerry Yu23f7a6f2022-04-23 15:16:45 +08002053{
2054 uint16_t selected_group = ssl->handshake->hrr_selected_group;
2055 /* key_share Extension
2056 *
2057 * struct {
2058 * select (Handshake.msg_type) {
2059 * ...
2060 * case hello_retry_request:
2061 * NamedGroup selected_group;
2062 * ...
2063 * };
2064 * } KeyShare;
2065 */
2066
2067 *out_len = 0;
2068
Jerry Yub0ac10b2022-05-05 11:10:08 +08002069 /*
2070 * For a pure PSK key exchange, there is no group to agree upon. The purpose
2071 * of the HRR is then to transmit a cookie to force the client to demonstrate
2072 * reachability at their apparent network address (primarily useful for DTLS).
2073 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002074 if (!mbedtls_ssl_tls13_key_exchange_mode_with_ephemeral(ssl)) {
2075 return 0;
2076 }
Jerry Yu23f7a6f2022-04-23 15:16:45 +08002077
2078 /* We should only send the key_share extension if the client's initial
2079 * key share was not acceptable. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002080 if (ssl->handshake->offered_group_id != 0) {
2081 MBEDTLS_SSL_DEBUG_MSG(4, ("Skip key_share extension in HRR"));
2082 return 0;
Jerry Yu23f7a6f2022-04-23 15:16:45 +08002083 }
2084
Gilles Peskine449bd832023-01-11 14:50:10 +01002085 if (selected_group == 0) {
2086 MBEDTLS_SSL_DEBUG_MSG(1, ("no matching named group found"));
2087 return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
Jerry Yu23f7a6f2022-04-23 15:16:45 +08002088 }
2089
Jerry Yub0ac10b2022-05-05 11:10:08 +08002090 /* Check if we have enough space:
2091 * - extension_type (2 bytes)
2092 * - extension_data_length (2 bytes)
2093 * - selected_group (2 bytes)
2094 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002095 MBEDTLS_SSL_CHK_BUF_PTR(buf, end, 6);
Jerry Yu23f7a6f2022-04-23 15:16:45 +08002096
Gilles Peskine449bd832023-01-11 14:50:10 +01002097 MBEDTLS_PUT_UINT16_BE(MBEDTLS_TLS_EXT_KEY_SHARE, buf, 0);
2098 MBEDTLS_PUT_UINT16_BE(2, buf, 2);
2099 MBEDTLS_PUT_UINT16_BE(selected_group, buf, 4);
Jerry Yu23f7a6f2022-04-23 15:16:45 +08002100
Gilles Peskine449bd832023-01-11 14:50:10 +01002101 MBEDTLS_SSL_DEBUG_MSG(3,
2102 ("HRR selected_group: %s (%x)",
2103 mbedtls_ssl_named_group_to_str(selected_group),
2104 selected_group));
Jerry Yu23f7a6f2022-04-23 15:16:45 +08002105
2106 *out_len = 6;
Jerry Yu23f7a6f2022-04-23 15:16:45 +08002107
Gilles Peskine449bd832023-01-11 14:50:10 +01002108 mbedtls_ssl_tls13_set_hs_sent_ext_mask(ssl, MBEDTLS_TLS_EXT_KEY_SHARE);
Jerry Yub95dd362022-11-08 21:19:34 +08002109
Gilles Peskine449bd832023-01-11 14:50:10 +01002110 return 0;
Jerry Yu23f7a6f2022-04-23 15:16:45 +08002111}
Jerry Yu3bf2c642022-03-30 22:02:12 +08002112
2113/*
2114 * Structure of ServerHello message:
2115 *
2116 * struct {
2117 * ProtocolVersion legacy_version = 0x0303; // TLS v1.2
2118 * Random random;
2119 * opaque legacy_session_id_echo<0..32>;
2120 * CipherSuite cipher_suite;
2121 * uint8 legacy_compression_method = 0;
2122 * Extension extensions<6..2^16-1>;
2123 * } ServerHello;
2124 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002125MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002126static int ssl_tls13_write_server_hello_body(mbedtls_ssl_context *ssl,
2127 unsigned char *buf,
2128 unsigned char *end,
2129 size_t *out_len,
2130 int is_hrr)
Jerry Yu56404d72022-03-30 17:36:13 +08002131{
Jerry Yu955ddd72022-04-22 22:27:33 +08002132 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yu3bf2c642022-03-30 22:02:12 +08002133 unsigned char *p = buf;
Jerry Yud9436a12022-04-20 22:28:09 +08002134 unsigned char *p_extensions_len;
Jerry Yufbe3e642022-04-25 19:31:51 +08002135 size_t output_len;
Jerry Yu3bf2c642022-03-30 22:02:12 +08002136
2137 *out_len = 0;
Jerry Yu50e00e32022-10-31 14:45:01 +08002138 ssl->handshake->sent_extensions = MBEDTLS_SSL_EXT_MASK_NONE;
Jerry Yu3bf2c642022-03-30 22:02:12 +08002139
Jerry Yucfc04b32022-04-21 09:31:58 +08002140 /* ...
2141 * ProtocolVersion legacy_version = 0x0303; // TLS 1.2
2142 * ...
2143 * with ProtocolVersion defined as:
2144 * uint16 ProtocolVersion;
Jerry Yu3bf2c642022-03-30 22:02:12 +08002145 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002146 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 2);
2147 MBEDTLS_PUT_UINT16_BE(0x0303, p, 0);
Jerry Yu3bf2c642022-03-30 22:02:12 +08002148 p += 2;
2149
Jerry Yu1c3e6882022-04-20 21:23:40 +08002150 /* ...
2151 * Random random;
2152 * ...
Jerry Yucfc04b32022-04-21 09:31:58 +08002153 * with Random defined as:
2154 * opaque Random[MBEDTLS_SERVER_HELLO_RANDOM_LEN];
Jerry Yu1c3e6882022-04-20 21:23:40 +08002155 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002156 MBEDTLS_SSL_CHK_BUF_PTR(p, end, MBEDTLS_SERVER_HELLO_RANDOM_LEN);
2157 if (is_hrr) {
2158 memcpy(p, mbedtls_ssl_tls13_hello_retry_request_magic,
2159 MBEDTLS_SERVER_HELLO_RANDOM_LEN);
2160 } else {
2161 memcpy(p, &ssl->handshake->randbytes[MBEDTLS_CLIENT_HELLO_RANDOM_LEN],
2162 MBEDTLS_SERVER_HELLO_RANDOM_LEN);
Jerry Yu23f7a6f2022-04-23 15:16:45 +08002163 }
Gilles Peskine449bd832023-01-11 14:50:10 +01002164 MBEDTLS_SSL_DEBUG_BUF(3, "server hello, random bytes",
2165 p, MBEDTLS_SERVER_HELLO_RANDOM_LEN);
Jerry Yu3bf2c642022-03-30 22:02:12 +08002166 p += MBEDTLS_SERVER_HELLO_RANDOM_LEN;
2167
Jerry Yucfc04b32022-04-21 09:31:58 +08002168 /* ...
2169 * opaque legacy_session_id_echo<0..32>;
2170 * ...
Jerry Yu3bf2c642022-03-30 22:02:12 +08002171 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002172 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 1 + ssl->session_negotiate->id_len);
2173 *p++ = (unsigned char) ssl->session_negotiate->id_len;
2174 if (ssl->session_negotiate->id_len > 0) {
2175 memcpy(p, &ssl->session_negotiate->id[0],
2176 ssl->session_negotiate->id_len);
Jerry Yu3bf2c642022-03-30 22:02:12 +08002177 p += ssl->session_negotiate->id_len;
Jerry Yu955ddd72022-04-22 22:27:33 +08002178
Gilles Peskine449bd832023-01-11 14:50:10 +01002179 MBEDTLS_SSL_DEBUG_BUF(3, "session id", ssl->session_negotiate->id,
2180 ssl->session_negotiate->id_len);
Jerry Yu3bf2c642022-03-30 22:02:12 +08002181 }
2182
Jerry Yucfc04b32022-04-21 09:31:58 +08002183 /* ...
2184 * CipherSuite cipher_suite;
2185 * ...
2186 * with CipherSuite defined as:
2187 * uint8 CipherSuite[2];
Jerry Yu3bf2c642022-03-30 22:02:12 +08002188 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002189 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 2);
2190 MBEDTLS_PUT_UINT16_BE(ssl->session_negotiate->ciphersuite, p, 0);
Jerry Yu3bf2c642022-03-30 22:02:12 +08002191 p += 2;
Gilles Peskine449bd832023-01-11 14:50:10 +01002192 MBEDTLS_SSL_DEBUG_MSG(3,
2193 ("server hello, chosen ciphersuite: %s ( id=%d )",
2194 mbedtls_ssl_get_ciphersuite_name(
2195 ssl->session_negotiate->ciphersuite),
2196 ssl->session_negotiate->ciphersuite));
Jerry Yu3bf2c642022-03-30 22:02:12 +08002197
Jerry Yucfc04b32022-04-21 09:31:58 +08002198 /* ...
2199 * uint8 legacy_compression_method = 0;
2200 * ...
2201 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002202 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 1);
Thomas Daubney31e03a82022-07-25 15:59:25 +01002203 *p++ = MBEDTLS_SSL_COMPRESS_NULL;
Jerry Yu3bf2c642022-03-30 22:02:12 +08002204
Jerry Yucfc04b32022-04-21 09:31:58 +08002205 /* ...
2206 * Extension extensions<6..2^16-1>;
2207 * ...
2208 * struct {
2209 * ExtensionType extension_type; (2 bytes)
2210 * opaque extension_data<0..2^16-1>;
2211 * } Extension;
2212 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002213 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 2);
Jerry Yud9436a12022-04-20 22:28:09 +08002214 p_extensions_len = p;
Jerry Yu3bf2c642022-03-30 22:02:12 +08002215 p += 2;
2216
Gilles Peskine449bd832023-01-11 14:50:10 +01002217 if ((ret = ssl_tls13_write_server_hello_supported_versions_ext(
2218 ssl, p, end, &output_len)) != 0) {
Jerry Yu955ddd72022-04-22 22:27:33 +08002219 MBEDTLS_SSL_DEBUG_RET(
Gilles Peskine449bd832023-01-11 14:50:10 +01002220 1, "ssl_tls13_write_server_hello_supported_versions_ext", ret);
2221 return ret;
Jerry Yu3bf2c642022-03-30 22:02:12 +08002222 }
2223 p += output_len;
2224
Gilles Peskine449bd832023-01-11 14:50:10 +01002225 if (mbedtls_ssl_tls13_key_exchange_mode_with_ephemeral(ssl)) {
2226 if (is_hrr) {
2227 ret = ssl_tls13_write_hrr_key_share_ext(ssl, p, end, &output_len);
2228 } else {
2229 ret = ssl_tls13_write_key_share_ext(ssl, p, end, &output_len);
2230 }
2231 if (ret != 0) {
2232 return ret;
2233 }
Jerry Yu3bf2c642022-03-30 22:02:12 +08002234 p += output_len;
2235 }
Jerry Yu3bf2c642022-03-30 22:02:12 +08002236
Ronald Cron41a443a2022-10-04 16:38:25 +02002237#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01002238 if (!is_hrr && mbedtls_ssl_tls13_key_exchange_mode_with_psk(ssl)) {
2239 ret = ssl_tls13_write_server_pre_shared_key_ext(ssl, p, end, &output_len);
2240 if (ret != 0) {
2241 MBEDTLS_SSL_DEBUG_RET(1, "ssl_tls13_write_server_pre_shared_key_ext",
2242 ret);
2243 return ret;
Jerry Yu032b15ce2022-07-11 06:10:03 +00002244 }
2245 p += output_len;
2246 }
Ronald Cron41a443a2022-10-04 16:38:25 +02002247#endif
Jerry Yu032b15ce2022-07-11 06:10:03 +00002248
Gilles Peskine449bd832023-01-11 14:50:10 +01002249 MBEDTLS_PUT_UINT16_BE(p - p_extensions_len - 2, p_extensions_len, 0);
Jerry Yu3bf2c642022-03-30 22:02:12 +08002250
Gilles Peskine449bd832023-01-11 14:50:10 +01002251 MBEDTLS_SSL_DEBUG_BUF(4, "server hello extensions",
2252 p_extensions_len, p - p_extensions_len);
Jerry Yu3bf2c642022-03-30 22:02:12 +08002253
Jerry Yud9436a12022-04-20 22:28:09 +08002254 *out_len = p - buf;
Jerry Yu3bf2c642022-03-30 22:02:12 +08002255
Gilles Peskine449bd832023-01-11 14:50:10 +01002256 MBEDTLS_SSL_DEBUG_BUF(3, "server hello", buf, *out_len);
Jerry Yu3bf2c642022-03-30 22:02:12 +08002257
Jerry Yu7de2ff02022-11-08 21:43:46 +08002258 MBEDTLS_SSL_PRINT_EXTS(
Jerry Yu4b8f2f72022-10-31 13:31:22 +08002259 3, is_hrr ? MBEDTLS_SSL_TLS1_3_HS_HELLO_RETRY_REQUEST :
Gilles Peskine449bd832023-01-11 14:50:10 +01002260 MBEDTLS_SSL_HS_SERVER_HELLO,
2261 ssl->handshake->sent_extensions);
Jerry Yu4b8f2f72022-10-31 13:31:22 +08002262
Gilles Peskine449bd832023-01-11 14:50:10 +01002263 return ret;
Jerry Yu56404d72022-03-30 17:36:13 +08002264}
2265
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002266MBEDTLS_CHECK_RETURN_CRITICAL
Xiaokang Qian934ce6f2023-02-06 10:23:04 +00002267static int ssl_tls13_finalize_server_hello(mbedtls_ssl_context *ssl)
Jerry Yue110d252022-05-05 10:19:22 +08002268{
2269 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine449bd832023-01-11 14:50:10 +01002270 ret = mbedtls_ssl_tls13_compute_handshake_transform(ssl);
2271 if (ret != 0) {
2272 MBEDTLS_SSL_DEBUG_RET(1,
2273 "mbedtls_ssl_tls13_compute_handshake_transform",
2274 ret);
2275 return ret;
Jerry Yue110d252022-05-05 10:19:22 +08002276 }
2277
Gilles Peskine449bd832023-01-11 14:50:10 +01002278 return ret;
Jerry Yue110d252022-05-05 10:19:22 +08002279}
2280
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002281MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002282static int ssl_tls13_write_server_hello(mbedtls_ssl_context *ssl)
Jerry Yu5b64ae92022-03-30 17:15:02 +08002283{
Jerry Yu637a3f12022-04-20 21:37:58 +08002284 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yuf4b27e42022-03-30 17:32:21 +08002285 unsigned char *buf;
2286 size_t buf_len, msg_len;
2287
Gilles Peskine449bd832023-01-11 14:50:10 +01002288 MBEDTLS_SSL_DEBUG_MSG(2, ("=> write server hello"));
Jerry Yuf4b27e42022-03-30 17:32:21 +08002289
Gilles Peskine449bd832023-01-11 14:50:10 +01002290 MBEDTLS_SSL_PROC_CHK(ssl_tls13_prepare_server_hello(ssl));
Jerry Yuf4b27e42022-03-30 17:32:21 +08002291
Xiaokang Qian9f1747b2023-03-30 02:50:04 +00002292 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_start_handshake_msg(
2293 ssl, MBEDTLS_SSL_HS_SERVER_HELLO, &buf, &buf_len));
Jerry Yuf4b27e42022-03-30 17:32:21 +08002294
Gilles Peskine449bd832023-01-11 14:50:10 +01002295 MBEDTLS_SSL_PROC_CHK(ssl_tls13_write_server_hello_body(ssl, buf,
2296 buf + buf_len,
2297 &msg_len,
2298 0));
Jerry Yuf4b27e42022-03-30 17:32:21 +08002299
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +01002300 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_add_hs_msg_to_checksum(
Manuel Pégourié-Gonnard43cc1272023-02-06 11:48:19 +01002301 ssl, MBEDTLS_SSL_HS_SERVER_HELLO, buf, msg_len));
Jerry Yuf4b27e42022-03-30 17:32:21 +08002302
Gilles Peskine449bd832023-01-11 14:50:10 +01002303 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_finish_handshake_msg(
2304 ssl, buf_len, msg_len));
Jerry Yu637a3f12022-04-20 21:37:58 +08002305
Xiaokang Qian934ce6f2023-02-06 10:23:04 +00002306 MBEDTLS_SSL_PROC_CHK(ssl_tls13_finalize_server_hello(ssl));
Jerry Yue110d252022-05-05 10:19:22 +08002307
Gabor Mezei7b39bf12022-05-24 16:04:14 +02002308#if defined(MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE)
2309 /* The server sends a dummy change_cipher_spec record immediately
2310 * after its first handshake message. This may either be after
2311 * a ServerHello or a HelloRetryRequest.
2312 */
Gabor Mezei96ae9262022-06-28 11:45:18 +02002313 mbedtls_ssl_handshake_set_state(
Gilles Peskine449bd832023-01-11 14:50:10 +01002314 ssl, MBEDTLS_SSL_SERVER_CCS_AFTER_SERVER_HELLO);
Gabor Mezei7b39bf12022-05-24 16:04:14 +02002315#else
Gilles Peskine449bd832023-01-11 14:50:10 +01002316 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_ENCRYPTED_EXTENSIONS);
Gabor Mezei7b39bf12022-05-24 16:04:14 +02002317#endif /* MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE */
Jerry Yue110d252022-05-05 10:19:22 +08002318
Jerry Yuf4b27e42022-03-30 17:32:21 +08002319cleanup:
2320
Gilles Peskine449bd832023-01-11 14:50:10 +01002321 MBEDTLS_SSL_DEBUG_MSG(2, ("<= write server hello"));
2322 return ret;
Jerry Yu5b64ae92022-03-30 17:15:02 +08002323}
2324
Jerry Yu23d1a252022-05-12 18:08:59 +08002325
2326/*
2327 * Handler for MBEDTLS_SSL_HELLO_RETRY_REQUEST
2328 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002329MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002330static int ssl_tls13_prepare_hello_retry_request(mbedtls_ssl_context *ssl)
Jerry Yu23d1a252022-05-12 18:08:59 +08002331{
2332 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine449bd832023-01-11 14:50:10 +01002333 if (ssl->handshake->hello_retry_request_count > 0) {
2334 MBEDTLS_SSL_DEBUG_MSG(1, ("Too many HRRs"));
2335 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE,
2336 MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE);
2337 return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
Jerry Yu23d1a252022-05-12 18:08:59 +08002338 }
2339
2340 /*
2341 * Create stateless transcript hash for HRR
2342 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002343 MBEDTLS_SSL_DEBUG_MSG(4, ("Reset transcript for HRR"));
2344 ret = mbedtls_ssl_reset_transcript_for_hrr(ssl);
2345 if (ret != 0) {
2346 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_reset_transcript_for_hrr", ret);
2347 return ret;
Jerry Yu23d1a252022-05-12 18:08:59 +08002348 }
Gilles Peskine449bd832023-01-11 14:50:10 +01002349 mbedtls_ssl_session_reset_msg_layer(ssl, 0);
Jerry Yu23d1a252022-05-12 18:08:59 +08002350
Gilles Peskine449bd832023-01-11 14:50:10 +01002351 return 0;
Jerry Yu23d1a252022-05-12 18:08:59 +08002352}
2353
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002354MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002355static int ssl_tls13_write_hello_retry_request(mbedtls_ssl_context *ssl)
Jerry Yu23d1a252022-05-12 18:08:59 +08002356{
2357 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
2358 unsigned char *buf;
2359 size_t buf_len, msg_len;
2360
Gilles Peskine449bd832023-01-11 14:50:10 +01002361 MBEDTLS_SSL_DEBUG_MSG(2, ("=> write hello retry request"));
Jerry Yu23d1a252022-05-12 18:08:59 +08002362
Gilles Peskine449bd832023-01-11 14:50:10 +01002363 MBEDTLS_SSL_PROC_CHK(ssl_tls13_prepare_hello_retry_request(ssl));
Jerry Yu23d1a252022-05-12 18:08:59 +08002364
Gilles Peskine449bd832023-01-11 14:50:10 +01002365 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_start_handshake_msg(
2366 ssl, MBEDTLS_SSL_HS_SERVER_HELLO,
2367 &buf, &buf_len));
Jerry Yu23d1a252022-05-12 18:08:59 +08002368
Gilles Peskine449bd832023-01-11 14:50:10 +01002369 MBEDTLS_SSL_PROC_CHK(ssl_tls13_write_server_hello_body(ssl, buf,
2370 buf + buf_len,
2371 &msg_len,
2372 1));
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +01002373 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_add_hs_msg_to_checksum(
Manuel Pégourié-Gonnard43cc1272023-02-06 11:48:19 +01002374 ssl, MBEDTLS_SSL_HS_SERVER_HELLO, buf, msg_len));
Jerry Yu23d1a252022-05-12 18:08:59 +08002375
2376
Gilles Peskine449bd832023-01-11 14:50:10 +01002377 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_finish_handshake_msg(ssl, buf_len,
2378 msg_len));
Jerry Yu23d1a252022-05-12 18:08:59 +08002379
2380 ssl->handshake->hello_retry_request_count++;
2381
Gabor Mezei7b39bf12022-05-24 16:04:14 +02002382#if defined(MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE)
2383 /* The server sends a dummy change_cipher_spec record immediately
2384 * after its first handshake message. This may either be after
2385 * a ServerHello or a HelloRetryRequest.
2386 */
Gabor Mezei96ae9262022-06-28 11:45:18 +02002387 mbedtls_ssl_handshake_set_state(
Gilles Peskine449bd832023-01-11 14:50:10 +01002388 ssl, MBEDTLS_SSL_SERVER_CCS_AFTER_HELLO_RETRY_REQUEST);
Gabor Mezei7b39bf12022-05-24 16:04:14 +02002389#else
Gilles Peskine449bd832023-01-11 14:50:10 +01002390 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_CLIENT_HELLO);
Gabor Mezei7b39bf12022-05-24 16:04:14 +02002391#endif /* MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE */
Jerry Yu23d1a252022-05-12 18:08:59 +08002392
2393cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +01002394 MBEDTLS_SSL_DEBUG_MSG(2, ("<= write hello retry request"));
2395 return ret;
Jerry Yu23d1a252022-05-12 18:08:59 +08002396}
2397
Jerry Yu5b64ae92022-03-30 17:15:02 +08002398/*
Jerry Yu4d3841a2022-04-16 12:37:19 +08002399 * Handler for MBEDTLS_SSL_ENCRYPTED_EXTENSIONS
2400 */
Jerry Yu4d3841a2022-04-16 12:37:19 +08002401
2402/*
2403 * struct {
2404 * Extension extensions<0..2 ^ 16 - 1>;
2405 * } EncryptedExtensions;
2406 *
2407 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002408MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002409static int ssl_tls13_write_encrypted_extensions_body(mbedtls_ssl_context *ssl,
2410 unsigned char *buf,
2411 unsigned char *end,
2412 size_t *out_len)
Jerry Yu4d3841a2022-04-16 12:37:19 +08002413{
XiaokangQianacb39922022-06-17 10:18:48 +00002414 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yu4d3841a2022-04-16 12:37:19 +08002415 unsigned char *p = buf;
2416 size_t extensions_len = 0;
Jerry Yu8937eb42022-05-03 12:12:14 +08002417 unsigned char *p_extensions_len;
XiaokangQianacb39922022-06-17 10:18:48 +00002418 size_t output_len;
Jerry Yu9da5e5a2022-05-03 15:46:09 +08002419
Jerry Yu4d3841a2022-04-16 12:37:19 +08002420 *out_len = 0;
2421
Gilles Peskine449bd832023-01-11 14:50:10 +01002422 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 2);
Jerry Yu8937eb42022-05-03 12:12:14 +08002423 p_extensions_len = p;
Jerry Yu4d3841a2022-04-16 12:37:19 +08002424 p += 2;
2425
Jerry Yu8937eb42022-05-03 12:12:14 +08002426 ((void) ssl);
XiaokangQianacb39922022-06-17 10:18:48 +00002427 ((void) ret);
2428 ((void) output_len);
2429
2430#if defined(MBEDTLS_SSL_ALPN)
Gilles Peskine449bd832023-01-11 14:50:10 +01002431 ret = mbedtls_ssl_write_alpn_ext(ssl, p, end, &output_len);
2432 if (ret != 0) {
2433 return ret;
2434 }
XiaokangQian95d5f542022-06-24 02:29:26 +00002435 p += output_len;
XiaokangQianacb39922022-06-17 10:18:48 +00002436#endif /* MBEDTLS_SSL_ALPN */
Jerry Yu4d3841a2022-04-16 12:37:19 +08002437
Gilles Peskine449bd832023-01-11 14:50:10 +01002438 extensions_len = (p - p_extensions_len) - 2;
2439 MBEDTLS_PUT_UINT16_BE(extensions_len, p_extensions_len, 0);
Jerry Yu8937eb42022-05-03 12:12:14 +08002440
2441 *out_len = p - buf;
Jerry Yu4d3841a2022-04-16 12:37:19 +08002442
Gilles Peskine449bd832023-01-11 14:50:10 +01002443 MBEDTLS_SSL_DEBUG_BUF(4, "encrypted extensions", buf, *out_len);
Jerry Yu4d3841a2022-04-16 12:37:19 +08002444
Jerry Yu7de2ff02022-11-08 21:43:46 +08002445 MBEDTLS_SSL_PRINT_EXTS(
Gilles Peskine449bd832023-01-11 14:50:10 +01002446 3, MBEDTLS_SSL_HS_ENCRYPTED_EXTENSIONS, ssl->handshake->sent_extensions);
Jerry Yu4b8f2f72022-10-31 13:31:22 +08002447
Gilles Peskine449bd832023-01-11 14:50:10 +01002448 return 0;
Jerry Yu4d3841a2022-04-16 12:37:19 +08002449}
2450
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002451MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002452static int ssl_tls13_write_encrypted_extensions(mbedtls_ssl_context *ssl)
Jerry Yu4d3841a2022-04-16 12:37:19 +08002453{
2454 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
2455 unsigned char *buf;
Jerry Yu39730a72022-05-03 12:14:04 +08002456 size_t buf_len, msg_len;
Jerry Yu4d3841a2022-04-16 12:37:19 +08002457
Gilles Peskine449bd832023-01-11 14:50:10 +01002458 mbedtls_ssl_set_outbound_transform(ssl,
2459 ssl->handshake->transform_handshake);
Gabor Mezei54719122022-06-28 11:34:56 +02002460 MBEDTLS_SSL_DEBUG_MSG(
Gilles Peskine449bd832023-01-11 14:50:10 +01002461 3, ("switching to handshake transform for outbound data"));
Gabor Mezei54719122022-06-28 11:34:56 +02002462
Gilles Peskine449bd832023-01-11 14:50:10 +01002463 MBEDTLS_SSL_DEBUG_MSG(2, ("=> write encrypted extensions"));
Jerry Yu4d3841a2022-04-16 12:37:19 +08002464
Xiaokang Qian9f1747b2023-03-30 02:50:04 +00002465 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_start_handshake_msg(
2466 ssl, MBEDTLS_SSL_HS_ENCRYPTED_EXTENSIONS,
2467 &buf, &buf_len));
Jerry Yu4d3841a2022-04-16 12:37:19 +08002468
Gilles Peskine449bd832023-01-11 14:50:10 +01002469 MBEDTLS_SSL_PROC_CHK(ssl_tls13_write_encrypted_extensions_body(
2470 ssl, buf, buf + buf_len, &msg_len));
Jerry Yu4d3841a2022-04-16 12:37:19 +08002471
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +01002472 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_add_hs_msg_to_checksum(
Xiaokang Qian9f1747b2023-03-30 02:50:04 +00002473 ssl, MBEDTLS_SSL_HS_ENCRYPTED_EXTENSIONS,
2474 buf, msg_len));
Jerry Yu4d3841a2022-04-16 12:37:19 +08002475
Gilles Peskine449bd832023-01-11 14:50:10 +01002476 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_finish_handshake_msg(
2477 ssl, buf_len, msg_len));
Jerry Yu4d3841a2022-04-16 12:37:19 +08002478
Ronald Cron928cbd32022-10-04 16:14:26 +02002479#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01002480 if (mbedtls_ssl_tls13_key_exchange_mode_with_psk(ssl)) {
2481 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_SERVER_FINISHED);
2482 } else {
2483 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_CERTIFICATE_REQUEST);
2484 }
Jerry Yu8937eb42022-05-03 12:12:14 +08002485#else
Gilles Peskine449bd832023-01-11 14:50:10 +01002486 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_SERVER_FINISHED);
Jerry Yu8937eb42022-05-03 12:12:14 +08002487#endif
2488
Jerry Yu4d3841a2022-04-16 12:37:19 +08002489cleanup:
2490
Gilles Peskine449bd832023-01-11 14:50:10 +01002491 MBEDTLS_SSL_DEBUG_MSG(2, ("<= write encrypted extensions"));
2492 return ret;
Jerry Yu4d3841a2022-04-16 12:37:19 +08002493}
2494
Ronald Cron928cbd32022-10-04 16:14:26 +02002495#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED)
XiaokangQiana987e1d2022-05-07 01:25:58 +00002496#define SSL_CERTIFICATE_REQUEST_SEND_REQUEST 0
2497#define SSL_CERTIFICATE_REQUEST_SKIP 1
2498/* Coordination:
2499 * Check whether a CertificateRequest message should be written.
2500 * Returns a negative code on failure, or
2501 * - SSL_CERTIFICATE_REQUEST_SEND_REQUEST
2502 * - SSL_CERTIFICATE_REQUEST_SKIP
2503 * indicating if the writing of the CertificateRequest
2504 * should be skipped or not.
2505 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002506MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002507static int ssl_tls13_certificate_request_coordinate(mbedtls_ssl_context *ssl)
XiaokangQiana987e1d2022-05-07 01:25:58 +00002508{
2509 int authmode;
2510
XiaokangQian40a35232022-05-07 09:02:40 +00002511#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Gilles Peskine449bd832023-01-11 14:50:10 +01002512 if (ssl->handshake->sni_authmode != MBEDTLS_SSL_VERIFY_UNSET) {
XiaokangQian40a35232022-05-07 09:02:40 +00002513 authmode = ssl->handshake->sni_authmode;
Gilles Peskine449bd832023-01-11 14:50:10 +01002514 } else
XiaokangQian40a35232022-05-07 09:02:40 +00002515#endif
XiaokangQiana987e1d2022-05-07 01:25:58 +00002516 authmode = ssl->conf->authmode;
2517
Gilles Peskine449bd832023-01-11 14:50:10 +01002518 if (authmode == MBEDTLS_SSL_VERIFY_NONE) {
Ronald Croneac00ad2022-09-13 10:16:31 +02002519 ssl->session_negotiate->verify_result = MBEDTLS_X509_BADCERT_SKIP_VERIFY;
Gilles Peskine449bd832023-01-11 14:50:10 +01002520 return SSL_CERTIFICATE_REQUEST_SKIP;
Ronald Croneac00ad2022-09-13 10:16:31 +02002521 }
XiaokangQiana987e1d2022-05-07 01:25:58 +00002522
XiaokangQianc3017f62022-05-13 05:55:41 +00002523 ssl->handshake->certificate_request_sent = 1;
XiaokangQian189ded22022-05-10 08:12:17 +00002524
Gilles Peskine449bd832023-01-11 14:50:10 +01002525 return SSL_CERTIFICATE_REQUEST_SEND_REQUEST;
XiaokangQiana987e1d2022-05-07 01:25:58 +00002526}
2527
XiaokangQiancec9ae62022-05-06 07:28:50 +00002528/*
2529 * struct {
2530 * opaque certificate_request_context<0..2^8-1>;
2531 * Extension extensions<2..2^16-1>;
2532 * } CertificateRequest;
2533 *
2534 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002535MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002536static int ssl_tls13_write_certificate_request_body(mbedtls_ssl_context *ssl,
2537 unsigned char *buf,
2538 const unsigned char *end,
2539 size_t *out_len)
XiaokangQiancec9ae62022-05-06 07:28:50 +00002540{
2541 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
2542 unsigned char *p = buf;
XiaokangQianec6efb92022-05-06 09:53:10 +00002543 size_t output_len = 0;
XiaokangQiancec9ae62022-05-06 07:28:50 +00002544 unsigned char *p_extensions_len;
2545
2546 *out_len = 0;
2547
2548 /* Check if we have enough space:
2549 * - certificate_request_context (1 byte)
2550 * - extensions length (2 bytes)
2551 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002552 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 3);
XiaokangQiancec9ae62022-05-06 07:28:50 +00002553
2554 /*
2555 * Write certificate_request_context
2556 */
2557 /*
2558 * We use a zero length context for the normal handshake
2559 * messages. For post-authentication handshake messages
2560 * this request context would be set to a non-zero value.
2561 */
2562 *p++ = 0x0;
2563
2564 /*
2565 * Write extensions
2566 */
2567 /* The extensions must contain the signature_algorithms. */
2568 p_extensions_len = p;
2569 p += 2;
Gilles Peskine449bd832023-01-11 14:50:10 +01002570 ret = mbedtls_ssl_write_sig_alg_ext(ssl, p, end, &output_len);
2571 if (ret != 0) {
2572 return ret;
2573 }
XiaokangQiancec9ae62022-05-06 07:28:50 +00002574
XiaokangQianec6efb92022-05-06 09:53:10 +00002575 p += output_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01002576 MBEDTLS_PUT_UINT16_BE(p - p_extensions_len - 2, p_extensions_len, 0);
XiaokangQiancec9ae62022-05-06 07:28:50 +00002577
2578 *out_len = p - buf;
2579
Jerry Yu7de2ff02022-11-08 21:43:46 +08002580 MBEDTLS_SSL_PRINT_EXTS(
Gilles Peskine449bd832023-01-11 14:50:10 +01002581 3, MBEDTLS_SSL_HS_CERTIFICATE_REQUEST, ssl->handshake->sent_extensions);
Jerry Yu4b8f2f72022-10-31 13:31:22 +08002582
Gilles Peskine449bd832023-01-11 14:50:10 +01002583 return 0;
XiaokangQiancec9ae62022-05-06 07:28:50 +00002584}
2585
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002586MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002587static int ssl_tls13_write_certificate_request(mbedtls_ssl_context *ssl)
XiaokangQiancec9ae62022-05-06 07:28:50 +00002588{
2589 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
2590
Gilles Peskine449bd832023-01-11 14:50:10 +01002591 MBEDTLS_SSL_DEBUG_MSG(2, ("=> write certificate request"));
XiaokangQiancec9ae62022-05-06 07:28:50 +00002592
Gilles Peskine449bd832023-01-11 14:50:10 +01002593 MBEDTLS_SSL_PROC_CHK_NEG(ssl_tls13_certificate_request_coordinate(ssl));
XiaokangQiancec9ae62022-05-06 07:28:50 +00002594
Gilles Peskine449bd832023-01-11 14:50:10 +01002595 if (ret == SSL_CERTIFICATE_REQUEST_SEND_REQUEST) {
XiaokangQiancec9ae62022-05-06 07:28:50 +00002596 unsigned char *buf;
2597 size_t buf_len, msg_len;
2598
Xiaokang Qian9f1747b2023-03-30 02:50:04 +00002599 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_start_handshake_msg(
2600 ssl, MBEDTLS_SSL_HS_CERTIFICATE_REQUEST,
2601 &buf, &buf_len));
XiaokangQiancec9ae62022-05-06 07:28:50 +00002602
Gilles Peskine449bd832023-01-11 14:50:10 +01002603 MBEDTLS_SSL_PROC_CHK(ssl_tls13_write_certificate_request_body(
2604 ssl, buf, buf + buf_len, &msg_len));
XiaokangQiancec9ae62022-05-06 07:28:50 +00002605
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +01002606 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_add_hs_msg_to_checksum(
Xiaokang Qian9f1747b2023-03-30 02:50:04 +00002607 ssl, MBEDTLS_SSL_HS_CERTIFICATE_REQUEST,
2608 buf, msg_len));
XiaokangQiancec9ae62022-05-06 07:28:50 +00002609
Gilles Peskine449bd832023-01-11 14:50:10 +01002610 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_finish_handshake_msg(
2611 ssl, buf_len, msg_len));
2612 } else if (ret == SSL_CERTIFICATE_REQUEST_SKIP) {
2613 MBEDTLS_SSL_DEBUG_MSG(2, ("<= skip write certificate request"));
XiaokangQiancec9ae62022-05-06 07:28:50 +00002614 ret = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01002615 } else {
2616 MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
XiaokangQiancec9ae62022-05-06 07:28:50 +00002617 ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
2618 goto cleanup;
2619 }
2620
Gilles Peskine449bd832023-01-11 14:50:10 +01002621 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_SERVER_CERTIFICATE);
XiaokangQiancec9ae62022-05-06 07:28:50 +00002622cleanup:
2623
Gilles Peskine449bd832023-01-11 14:50:10 +01002624 MBEDTLS_SSL_DEBUG_MSG(2, ("<= write certificate request"));
2625 return ret;
XiaokangQiancec9ae62022-05-06 07:28:50 +00002626}
XiaokangQiancec9ae62022-05-06 07:28:50 +00002627
Jerry Yu4d3841a2022-04-16 12:37:19 +08002628/*
Jerry Yuc6e6dbf2022-04-16 19:42:57 +08002629 * Handler for MBEDTLS_SSL_SERVER_CERTIFICATE
Jerry Yu83da34e2022-04-16 13:59:52 +08002630 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002631MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002632static int ssl_tls13_write_server_certificate(mbedtls_ssl_context *ssl)
Jerry Yu83da34e2022-04-16 13:59:52 +08002633{
Jerry Yu5a26f302022-05-10 20:46:40 +08002634 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
XiaokangQianfb665a82022-06-15 03:57:21 +00002635
2636#if defined(MBEDTLS_X509_CRT_PARSE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01002637 if ((ssl_tls13_pick_key_cert(ssl) != 0) ||
2638 mbedtls_ssl_own_cert(ssl) == NULL) {
2639 MBEDTLS_SSL_DEBUG_MSG(2, ("No certificate available."));
2640 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE,
2641 MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE);
2642 return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
Jerry Yu5a26f302022-05-10 20:46:40 +08002643 }
XiaokangQianfb665a82022-06-15 03:57:21 +00002644#endif /* MBEDTLS_X509_CRT_PARSE_C */
Jerry Yu5a26f302022-05-10 20:46:40 +08002645
Gilles Peskine449bd832023-01-11 14:50:10 +01002646 ret = mbedtls_ssl_tls13_write_certificate(ssl);
2647 if (ret != 0) {
2648 return ret;
2649 }
2650 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_CERTIFICATE_VERIFY);
2651 return 0;
Jerry Yu83da34e2022-04-16 13:59:52 +08002652}
2653
2654/*
Jerry Yuc6e6dbf2022-04-16 19:42:57 +08002655 * Handler for MBEDTLS_SSL_CERTIFICATE_VERIFY
Jerry Yu83da34e2022-04-16 13:59:52 +08002656 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002657MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002658static int ssl_tls13_write_certificate_verify(mbedtls_ssl_context *ssl)
Jerry Yu83da34e2022-04-16 13:59:52 +08002659{
Gilles Peskine449bd832023-01-11 14:50:10 +01002660 int ret = mbedtls_ssl_tls13_write_certificate_verify(ssl);
2661 if (ret != 0) {
2662 return ret;
2663 }
2664 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_SERVER_FINISHED);
2665 return 0;
Jerry Yu83da34e2022-04-16 13:59:52 +08002666}
Ronald Cron928cbd32022-10-04 16:14:26 +02002667#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED */
Jerry Yu83da34e2022-04-16 13:59:52 +08002668
2669/*
Jerry Yud6e253d2022-05-18 13:59:24 +08002670 * Handler for MBEDTLS_SSL_SERVER_FINISHED
Jerry Yu69dd8d42022-04-16 12:51:26 +08002671 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002672MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002673static int ssl_tls13_write_server_finished(mbedtls_ssl_context *ssl)
Jerry Yu69dd8d42022-04-16 12:51:26 +08002674{
Jerry Yud6e253d2022-05-18 13:59:24 +08002675 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yu27bdc7c2022-04-16 13:33:27 +08002676
Gilles Peskine449bd832023-01-11 14:50:10 +01002677 ret = mbedtls_ssl_tls13_write_finished_message(ssl);
2678 if (ret != 0) {
2679 return ret;
2680 }
Jerry Yu27bdc7c2022-04-16 13:33:27 +08002681
Gilles Peskine449bd832023-01-11 14:50:10 +01002682 ret = mbedtls_ssl_tls13_compute_application_transform(ssl);
2683 if (ret != 0) {
Jerry Yue3d67cb2022-05-19 15:33:10 +08002684 MBEDTLS_SSL_PEND_FATAL_ALERT(
Gilles Peskine449bd832023-01-11 14:50:10 +01002685 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE,
2686 MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE);
2687 return ret;
Jerry Yue3d67cb2022-05-19 15:33:10 +08002688 }
XiaokangQianc3017f62022-05-13 05:55:41 +00002689
Gilles Peskine449bd832023-01-11 14:50:10 +01002690 MBEDTLS_SSL_DEBUG_MSG(1, ("Switch to handshake keys for inbound traffic"));
2691 mbedtls_ssl_set_inbound_transform(ssl, ssl->handshake->transform_handshake);
XiaokangQian189ded22022-05-10 08:12:17 +00002692
Gilles Peskine449bd832023-01-11 14:50:10 +01002693 if (ssl->handshake->certificate_request_sent) {
2694 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_CLIENT_CERTIFICATE);
2695 } else {
2696 MBEDTLS_SSL_DEBUG_MSG(2, ("skip parse certificate"));
2697 MBEDTLS_SSL_DEBUG_MSG(2, ("skip parse certificate verify"));
2698 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_CLIENT_FINISHED);
Ronald Cron19385882022-06-15 16:26:13 +02002699 }
Ronald Cron63dc4632022-05-31 14:41:53 +02002700
Gilles Peskine449bd832023-01-11 14:50:10 +01002701 return 0;
Jerry Yu69dd8d42022-04-16 12:51:26 +08002702}
2703
2704/*
Jerry Yud6e253d2022-05-18 13:59:24 +08002705 * Handler for MBEDTLS_SSL_CLIENT_FINISHED
Jerry Yu69dd8d42022-04-16 12:51:26 +08002706 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002707MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002708static int ssl_tls13_process_client_finished(mbedtls_ssl_context *ssl)
Jerry Yu69dd8d42022-04-16 12:51:26 +08002709{
Jerry Yud6e253d2022-05-18 13:59:24 +08002710 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
2711
Gilles Peskine449bd832023-01-11 14:50:10 +01002712 ret = mbedtls_ssl_tls13_process_finished_message(ssl);
2713 if (ret != 0) {
2714 return ret;
Jerry Yue3d67cb2022-05-19 15:33:10 +08002715 }
2716
Gilles Peskine449bd832023-01-11 14:50:10 +01002717 ret = mbedtls_ssl_tls13_compute_resumption_master_secret(ssl);
2718 if (ret != 0) {
Xiaokang Qian9f1747b2023-03-30 02:50:04 +00002719 MBEDTLS_SSL_DEBUG_RET(
2720 1, "mbedtls_ssl_tls13_compute_resumption_master_secret", ret);
Gilles Peskine449bd832023-01-11 14:50:10 +01002721 }
2722
2723 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_HANDSHAKE_WRAPUP);
2724 return 0;
Jerry Yu69dd8d42022-04-16 12:51:26 +08002725}
2726
2727/*
Jerry Yud6e253d2022-05-18 13:59:24 +08002728 * Handler for MBEDTLS_SSL_HANDSHAKE_WRAPUP
Jerry Yu69dd8d42022-04-16 12:51:26 +08002729 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002730MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002731static int ssl_tls13_handshake_wrapup(mbedtls_ssl_context *ssl)
Jerry Yu69dd8d42022-04-16 12:51:26 +08002732{
Gilles Peskine449bd832023-01-11 14:50:10 +01002733 MBEDTLS_SSL_DEBUG_MSG(2, ("handshake: done"));
Jerry Yu03ed50b2022-04-16 17:13:30 +08002734
Gilles Peskine449bd832023-01-11 14:50:10 +01002735 mbedtls_ssl_tls13_handshake_wrapup(ssl);
Jerry Yue67bef42022-07-07 07:29:42 +00002736
Pengyu Lv3643fdb2023-01-12 16:46:28 +08002737#if defined(MBEDTLS_SSL_SESSION_TICKETS) && \
2738 defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED)
Pengyu Lva1aa31b2022-12-13 13:49:59 +08002739/* TODO: Remove the check of SOME_PSK_ENABLED since SESSION_TICKETS requires
2740 * SOME_PSK_ENABLED to be enabled. Here is just to make CI happy. It is
2741 * expected to be resolved with issue#6395.
2742 */
Pengyu Lvc7af2c42022-12-01 16:33:00 +08002743 /* Sent NewSessionTicket message only when client supports PSK */
Pengyu Lv3643fdb2023-01-12 16:46:28 +08002744 if (mbedtls_ssl_tls13_some_psk_enabled(ssl)) {
Xiaokang Qian9f1747b2023-03-30 02:50:04 +00002745 mbedtls_ssl_handshake_set_state(
2746 ssl, MBEDTLS_SSL_TLS1_3_NEW_SESSION_TICKET);
Pengyu Lvc7af2c42022-12-01 16:33:00 +08002747 } else
Jerry Yufca4d572022-07-21 10:37:48 +08002748#endif
Pengyu Lv3643fdb2023-01-12 16:46:28 +08002749 {
2750 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_HANDSHAKE_OVER);
2751 }
Gilles Peskine449bd832023-01-11 14:50:10 +01002752 return 0;
Jerry Yu69dd8d42022-04-16 12:51:26 +08002753}
2754
2755/*
Jerry Yua8d3c502022-10-30 14:51:23 +08002756 * Handler for MBEDTLS_SSL_TLS1_3_NEW_SESSION_TICKET
Jerry Yue67bef42022-07-07 07:29:42 +00002757 */
2758#define SSL_NEW_SESSION_TICKET_SKIP 0
2759#define SSL_NEW_SESSION_TICKET_WRITE 1
2760MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002761static int ssl_tls13_write_new_session_ticket_coordinate(mbedtls_ssl_context *ssl)
Jerry Yue67bef42022-07-07 07:29:42 +00002762{
2763 /* Check whether the use of session tickets is enabled */
Gilles Peskine449bd832023-01-11 14:50:10 +01002764 if (ssl->conf->f_ticket_write == NULL) {
2765 MBEDTLS_SSL_DEBUG_MSG(2, ("NewSessionTicket: disabled,"
2766 " callback is not set"));
2767 return SSL_NEW_SESSION_TICKET_SKIP;
Jerry Yud0766ec2022-09-22 10:46:57 +08002768 }
Gilles Peskine449bd832023-01-11 14:50:10 +01002769 if (ssl->conf->new_session_tickets_count == 0) {
2770 MBEDTLS_SSL_DEBUG_MSG(2, ("NewSessionTicket: disabled,"
2771 " configured count is zero"));
2772 return SSL_NEW_SESSION_TICKET_SKIP;
Jerry Yud0766ec2022-09-22 10:46:57 +08002773 }
2774
Gilles Peskine449bd832023-01-11 14:50:10 +01002775 if (ssl->handshake->new_session_tickets_count == 0) {
2776 MBEDTLS_SSL_DEBUG_MSG(2, ("NewSessionTicket: all tickets have "
2777 "been sent."));
2778 return SSL_NEW_SESSION_TICKET_SKIP;
Jerry Yue67bef42022-07-07 07:29:42 +00002779 }
2780
Gilles Peskine449bd832023-01-11 14:50:10 +01002781 return SSL_NEW_SESSION_TICKET_WRITE;
Jerry Yue67bef42022-07-07 07:29:42 +00002782}
2783
2784#if defined(MBEDTLS_SSL_SESSION_TICKETS)
2785MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002786static int ssl_tls13_prepare_new_session_ticket(mbedtls_ssl_context *ssl,
2787 unsigned char *ticket_nonce,
2788 size_t ticket_nonce_size)
Jerry Yue67bef42022-07-07 07:29:42 +00002789{
2790 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
2791 mbedtls_ssl_session *session = ssl->session;
2792 mbedtls_ssl_ciphersuite_t *ciphersuite_info;
2793 psa_algorithm_t psa_hash_alg;
2794 int hash_length;
2795
Gilles Peskine449bd832023-01-11 14:50:10 +01002796 MBEDTLS_SSL_DEBUG_MSG(2, ("=> prepare NewSessionTicket msg"));
Jerry Yue67bef42022-07-07 07:29:42 +00002797
2798#if defined(MBEDTLS_HAVE_TIME)
Gilles Peskine449bd832023-01-11 14:50:10 +01002799 session->start = mbedtls_time(NULL);
Jerry Yue67bef42022-07-07 07:29:42 +00002800#endif
2801
Pengyu Lv9f926952022-11-17 15:22:33 +08002802 /* Set ticket_flags depends on the advertised psk key exchange mode */
Pengyu Lv80270b22023-01-12 11:54:04 +08002803 mbedtls_ssl_session_clear_ticket_flags(
Pengyu Lv9eacb442022-12-09 14:39:19 +08002804 session, MBEDTLS_SSL_TLS1_3_TICKET_FLAGS_MASK);
Pengyu Lve6487fe2022-12-06 09:30:29 +08002805#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED)
Pengyu Lv80270b22023-01-12 11:54:04 +08002806 mbedtls_ssl_session_set_ticket_flags(
Pengyu Lv9eacb442022-12-09 14:39:19 +08002807 session, ssl->handshake->tls13_kex_modes);
Pengyu Lve6487fe2022-12-06 09:30:29 +08002808#endif
Pengyu Lv4938a562023-01-16 11:28:49 +08002809 MBEDTLS_SSL_PRINT_TICKET_FLAGS(4, session->ticket_flags);
Pengyu Lv9f926952022-11-17 15:22:33 +08002810
Jerry Yue67bef42022-07-07 07:29:42 +00002811 /* Generate ticket_age_add */
Gilles Peskine449bd832023-01-11 14:50:10 +01002812 if ((ret = ssl->conf->f_rng(ssl->conf->p_rng,
2813 (unsigned char *) &session->ticket_age_add,
2814 sizeof(session->ticket_age_add)) != 0)) {
2815 MBEDTLS_SSL_DEBUG_RET(1, "generate_ticket_age_add", ret);
2816 return ret;
Jerry Yue67bef42022-07-07 07:29:42 +00002817 }
Gilles Peskine449bd832023-01-11 14:50:10 +01002818 MBEDTLS_SSL_DEBUG_MSG(3, ("ticket_age_add: %u",
2819 (unsigned int) session->ticket_age_add));
Jerry Yue67bef42022-07-07 07:29:42 +00002820
2821 /* Generate ticket_nonce */
Gilles Peskine449bd832023-01-11 14:50:10 +01002822 ret = ssl->conf->f_rng(ssl->conf->p_rng, ticket_nonce, ticket_nonce_size);
2823 if (ret != 0) {
2824 MBEDTLS_SSL_DEBUG_RET(1, "generate_ticket_nonce", ret);
2825 return ret;
Jerry Yue67bef42022-07-07 07:29:42 +00002826 }
Gilles Peskine449bd832023-01-11 14:50:10 +01002827 MBEDTLS_SSL_DEBUG_BUF(3, "ticket_nonce:",
2828 ticket_nonce, ticket_nonce_size);
Jerry Yue67bef42022-07-07 07:29:42 +00002829
2830 ciphersuite_info =
Gilles Peskine449bd832023-01-11 14:50:10 +01002831 (mbedtls_ssl_ciphersuite_t *) ssl->handshake->ciphersuite_info;
Dave Rodgman2eab4622023-10-05 13:30:37 +01002832 psa_hash_alg = mbedtls_md_psa_alg_from_type((mbedtls_md_type_t) ciphersuite_info->mac);
Gilles Peskine449bd832023-01-11 14:50:10 +01002833 hash_length = PSA_HASH_LENGTH(psa_hash_alg);
2834 if (hash_length == -1 ||
2835 (size_t) hash_length > sizeof(session->resumption_key)) {
2836 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Jerry Yufca4d572022-07-21 10:37:48 +08002837 }
2838
Jerry Yue67bef42022-07-07 07:29:42 +00002839 /* In this code the psk key length equals the length of the hash */
2840 session->resumption_key_len = hash_length;
2841 session->ciphersuite = ciphersuite_info->id;
2842
2843 /* Compute resumption key
2844 *
2845 * HKDF-Expand-Label( resumption_master_secret,
2846 * "resumption", ticket_nonce, Hash.length )
2847 */
2848 ret = mbedtls_ssl_tls13_hkdf_expand_label(
Gilles Peskine449bd832023-01-11 14:50:10 +01002849 psa_hash_alg,
2850 session->app_secrets.resumption_master_secret,
2851 hash_length,
2852 MBEDTLS_SSL_TLS1_3_LBL_WITH_LEN(resumption),
2853 ticket_nonce,
2854 ticket_nonce_size,
2855 session->resumption_key,
2856 hash_length);
Jerry Yue67bef42022-07-07 07:29:42 +00002857
Gilles Peskine449bd832023-01-11 14:50:10 +01002858 if (ret != 0) {
2859 MBEDTLS_SSL_DEBUG_RET(2,
2860 "Creating the ticket-resumed PSK failed",
2861 ret);
2862 return ret;
Jerry Yue67bef42022-07-07 07:29:42 +00002863 }
Gilles Peskine449bd832023-01-11 14:50:10 +01002864 MBEDTLS_SSL_DEBUG_BUF(3, "Ticket-resumed PSK",
2865 session->resumption_key,
2866 session->resumption_key_len);
Jerry Yue67bef42022-07-07 07:29:42 +00002867
Gilles Peskine449bd832023-01-11 14:50:10 +01002868 MBEDTLS_SSL_DEBUG_BUF(3, "resumption_master_secret",
2869 session->app_secrets.resumption_master_secret,
2870 hash_length);
Jerry Yue67bef42022-07-07 07:29:42 +00002871
Gilles Peskine449bd832023-01-11 14:50:10 +01002872 return 0;
Jerry Yue67bef42022-07-07 07:29:42 +00002873}
2874
2875/* This function creates a NewSessionTicket message in the following format:
2876 *
2877 * struct {
2878 * uint32 ticket_lifetime;
2879 * uint32 ticket_age_add;
2880 * opaque ticket_nonce<0..255>;
2881 * opaque ticket<1..2^16-1>;
2882 * Extension extensions<0..2^16-2>;
2883 * } NewSessionTicket;
2884 *
2885 * The ticket inside the NewSessionTicket message is an encrypted container
2886 * carrying the necessary information so that the server is later able to
2887 * re-start the communication.
2888 *
2889 * The following fields are placed inside the ticket by the
2890 * f_ticket_write() function:
2891 *
2892 * - creation time (start)
2893 * - flags (flags)
2894 * - age add (ticket_age_add)
2895 * - key (key)
2896 * - key length (key_len)
2897 * - ciphersuite (ciphersuite)
2898 */
2899MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002900static int ssl_tls13_write_new_session_ticket_body(mbedtls_ssl_context *ssl,
2901 unsigned char *buf,
2902 unsigned char *end,
2903 size_t *out_len,
2904 unsigned char *ticket_nonce,
2905 size_t ticket_nonce_size)
Jerry Yue67bef42022-07-07 07:29:42 +00002906{
2907 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
2908 unsigned char *p = buf;
2909 mbedtls_ssl_session *session = ssl->session;
2910 size_t ticket_len;
2911 uint32_t ticket_lifetime;
2912
2913 *out_len = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01002914 MBEDTLS_SSL_DEBUG_MSG(2, ("=> write NewSessionTicket msg"));
Jerry Yue67bef42022-07-07 07:29:42 +00002915
2916 /*
2917 * ticket_lifetime 4 bytes
2918 * ticket_age_add 4 bytes
2919 * ticket_nonce 1 + ticket_nonce_size bytes
2920 * ticket >=2 bytes
2921 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002922 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 4 + 4 + 1 + ticket_nonce_size + 2);
Jerry Yue67bef42022-07-07 07:29:42 +00002923
2924 /* Generate ticket and ticket_lifetime */
Gilles Peskine449bd832023-01-11 14:50:10 +01002925 ret = ssl->conf->f_ticket_write(ssl->conf->p_ticket,
2926 session,
2927 p + 9 + ticket_nonce_size + 2,
2928 end,
2929 &ticket_len,
2930 &ticket_lifetime);
2931 if (ret != 0) {
2932 MBEDTLS_SSL_DEBUG_RET(1, "write_ticket", ret);
2933 return ret;
Jerry Yue67bef42022-07-07 07:29:42 +00002934 }
2935 /* RFC 8446 4.6.1
2936 * ticket_lifetime: Indicates the lifetime in seconds as a 32-bit
2937 * unsigned integer in network byte order from the time of ticket
2938 * issuance. Servers MUST NOT use any value greater than
2939 * 604800 seconds (7 days). The value of zero indicates that the
2940 * ticket should be discarded immediately. Clients MUST NOT cache
2941 * tickets for longer than 7 days, regardless of the ticket_lifetime,
2942 * and MAY delete tickets earlier based on local policy. A server
2943 * MAY treat a ticket as valid for a shorter period of time than what
2944 * is stated in the ticket_lifetime.
2945 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002946 if (ticket_lifetime > 604800) {
Xiaokang Qian28af5012022-10-13 08:18:19 +00002947 ticket_lifetime = 604800;
Gilles Peskine449bd832023-01-11 14:50:10 +01002948 }
2949 MBEDTLS_PUT_UINT32_BE(ticket_lifetime, p, 0);
2950 MBEDTLS_SSL_DEBUG_MSG(3, ("ticket_lifetime: %u",
2951 (unsigned int) ticket_lifetime));
Jerry Yue67bef42022-07-07 07:29:42 +00002952
2953 /* Write ticket_age_add */
Gilles Peskine449bd832023-01-11 14:50:10 +01002954 MBEDTLS_PUT_UINT32_BE(session->ticket_age_add, p, 4);
2955 MBEDTLS_SSL_DEBUG_MSG(3, ("ticket_age_add: %u",
2956 (unsigned int) session->ticket_age_add));
Jerry Yue67bef42022-07-07 07:29:42 +00002957
2958 /* Write ticket_nonce */
Gilles Peskine449bd832023-01-11 14:50:10 +01002959 p[8] = (unsigned char) ticket_nonce_size;
2960 if (ticket_nonce_size > 0) {
2961 memcpy(p + 9, ticket_nonce, ticket_nonce_size);
Jerry Yue67bef42022-07-07 07:29:42 +00002962 }
2963 p += 9 + ticket_nonce_size;
2964
2965 /* Write ticket */
Gilles Peskine449bd832023-01-11 14:50:10 +01002966 MBEDTLS_PUT_UINT16_BE(ticket_len, p, 0);
Jerry Yue67bef42022-07-07 07:29:42 +00002967 p += 2;
Gilles Peskine449bd832023-01-11 14:50:10 +01002968 MBEDTLS_SSL_DEBUG_BUF(4, "ticket", p, ticket_len);
Jerry Yue67bef42022-07-07 07:29:42 +00002969 p += ticket_len;
2970
2971 /* Ticket Extensions
2972 *
2973 * Note: We currently don't have any extensions.
2974 * Set length to zero.
2975 */
Jerry Yuedab6372022-10-31 14:37:31 +08002976 ssl->handshake->sent_extensions = MBEDTLS_SSL_EXT_MASK_NONE;
2977
Gilles Peskine449bd832023-01-11 14:50:10 +01002978 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 2);
2979 MBEDTLS_PUT_UINT16_BE(0, p, 0);
Jerry Yue67bef42022-07-07 07:29:42 +00002980 p += 2;
2981
2982 *out_len = p - buf;
Gilles Peskine449bd832023-01-11 14:50:10 +01002983 MBEDTLS_SSL_DEBUG_BUF(4, "ticket", buf, *out_len);
2984 MBEDTLS_SSL_DEBUG_MSG(2, ("<= write new session ticket"));
Jerry Yue67bef42022-07-07 07:29:42 +00002985
Jerry Yu7de2ff02022-11-08 21:43:46 +08002986 MBEDTLS_SSL_PRINT_EXTS(
Gilles Peskine449bd832023-01-11 14:50:10 +01002987 3, MBEDTLS_SSL_HS_NEW_SESSION_TICKET, ssl->handshake->sent_extensions);
Jerry Yu4b8f2f72022-10-31 13:31:22 +08002988
Gilles Peskine449bd832023-01-11 14:50:10 +01002989 return 0;
Jerry Yue67bef42022-07-07 07:29:42 +00002990}
2991
2992/*
Jerry Yua8d3c502022-10-30 14:51:23 +08002993 * Handler for MBEDTLS_SSL_TLS1_3_NEW_SESSION_TICKET
Jerry Yue67bef42022-07-07 07:29:42 +00002994 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002995static int ssl_tls13_write_new_session_ticket(mbedtls_ssl_context *ssl)
Jerry Yue67bef42022-07-07 07:29:42 +00002996{
2997 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
2998
Gilles Peskine449bd832023-01-11 14:50:10 +01002999 MBEDTLS_SSL_PROC_CHK_NEG(ssl_tls13_write_new_session_ticket_coordinate(ssl));
Jerry Yue67bef42022-07-07 07:29:42 +00003000
Gilles Peskine449bd832023-01-11 14:50:10 +01003001 if (ret == SSL_NEW_SESSION_TICKET_WRITE) {
Jerry Yue67bef42022-07-07 07:29:42 +00003002 unsigned char ticket_nonce[MBEDTLS_SSL_TLS1_3_TICKET_NONCE_LENGTH];
3003 unsigned char *buf;
3004 size_t buf_len, msg_len;
3005
Gilles Peskine449bd832023-01-11 14:50:10 +01003006 MBEDTLS_SSL_PROC_CHK(ssl_tls13_prepare_new_session_ticket(
3007 ssl, ticket_nonce, sizeof(ticket_nonce)));
Jerry Yue67bef42022-07-07 07:29:42 +00003008
Xiaokang Qian9f1747b2023-03-30 02:50:04 +00003009 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_start_handshake_msg(
3010 ssl, MBEDTLS_SSL_HS_NEW_SESSION_TICKET,
3011 &buf, &buf_len));
Jerry Yue67bef42022-07-07 07:29:42 +00003012
Gilles Peskine449bd832023-01-11 14:50:10 +01003013 MBEDTLS_SSL_PROC_CHK(ssl_tls13_write_new_session_ticket_body(
3014 ssl, buf, buf + buf_len, &msg_len,
3015 ticket_nonce, sizeof(ticket_nonce)));
Jerry Yue67bef42022-07-07 07:29:42 +00003016
Gilles Peskine449bd832023-01-11 14:50:10 +01003017 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_finish_handshake_msg(
3018 ssl, buf_len, msg_len));
Jerry Yufca4d572022-07-21 10:37:48 +08003019
Jerry Yu359e65f2022-09-22 23:47:43 +08003020 /* Limit session tickets count to one when resumption connection.
3021 *
3022 * See document of mbedtls_ssl_conf_new_session_tickets.
3023 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003024 if (ssl->handshake->resume == 1) {
Jerry Yu359e65f2022-09-22 23:47:43 +08003025 ssl->handshake->new_session_tickets_count = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01003026 } else {
Jerry Yu359e65f2022-09-22 23:47:43 +08003027 ssl->handshake->new_session_tickets_count--;
Gilles Peskine449bd832023-01-11 14:50:10 +01003028 }
Jerry Yub7e3fa72022-09-22 11:07:18 +08003029
Jerry Yua8d3c502022-10-30 14:51:23 +08003030 mbedtls_ssl_handshake_set_state(
Gilles Peskine449bd832023-01-11 14:50:10 +01003031 ssl, MBEDTLS_SSL_TLS1_3_NEW_SESSION_TICKET_FLUSH);
3032 } else {
3033 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_HANDSHAKE_OVER);
Jerry Yue67bef42022-07-07 07:29:42 +00003034 }
3035
Jerry Yue67bef42022-07-07 07:29:42 +00003036cleanup:
3037
Gilles Peskine449bd832023-01-11 14:50:10 +01003038 return ret;
Jerry Yue67bef42022-07-07 07:29:42 +00003039}
3040#endif /* MBEDTLS_SSL_SESSION_TICKETS */
3041
3042/*
XiaokangQiane8ff3502022-04-22 02:34:40 +00003043 * TLS 1.3 State Machine -- server side
XiaokangQian7807f9f2022-02-15 10:04:37 +00003044 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003045int mbedtls_ssl_tls13_handshake_server_step(mbedtls_ssl_context *ssl)
Jerry Yub9930e72021-08-06 17:11:51 +08003046{
XiaokangQian08037552022-04-20 07:16:41 +00003047 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
XiaokangQian7807f9f2022-02-15 10:04:37 +00003048
Gilles Peskine449bd832023-01-11 14:50:10 +01003049 if (ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER || ssl->handshake == NULL) {
3050 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
3051 }
XiaokangQian7807f9f2022-02-15 10:04:37 +00003052
Gilles Peskine449bd832023-01-11 14:50:10 +01003053 MBEDTLS_SSL_DEBUG_MSG(2, ("tls13 server state: %s(%d)",
Dave Rodgman2eab4622023-10-05 13:30:37 +01003054 mbedtls_ssl_states_str((mbedtls_ssl_states) ssl->state),
Gilles Peskine449bd832023-01-11 14:50:10 +01003055 ssl->state));
Jerry Yu6e81b272021-09-27 11:16:17 +08003056
Gilles Peskine449bd832023-01-11 14:50:10 +01003057 switch (ssl->state) {
XiaokangQian7807f9f2022-02-15 10:04:37 +00003058 /* start state */
3059 case MBEDTLS_SSL_HELLO_REQUEST:
Gilles Peskine449bd832023-01-11 14:50:10 +01003060 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_CLIENT_HELLO);
XiaokangQian08037552022-04-20 07:16:41 +00003061 ret = 0;
XiaokangQian7807f9f2022-02-15 10:04:37 +00003062 break;
3063
XiaokangQian7807f9f2022-02-15 10:04:37 +00003064 case MBEDTLS_SSL_CLIENT_HELLO:
Gilles Peskine449bd832023-01-11 14:50:10 +01003065 ret = ssl_tls13_process_client_hello(ssl);
3066 if (ret != 0) {
3067 MBEDTLS_SSL_DEBUG_RET(1, "ssl_tls13_process_client_hello", ret);
3068 }
Jerry Yuf41553b2022-05-09 22:20:30 +08003069 break;
XiaokangQian7807f9f2022-02-15 10:04:37 +00003070
Jerry Yuf41553b2022-05-09 22:20:30 +08003071 case MBEDTLS_SSL_HELLO_RETRY_REQUEST:
Gilles Peskine449bd832023-01-11 14:50:10 +01003072 ret = ssl_tls13_write_hello_retry_request(ssl);
3073 if (ret != 0) {
3074 MBEDTLS_SSL_DEBUG_RET(1, "ssl_tls13_write_hello_retry_request", ret);
3075 return ret;
Jerry Yuf41553b2022-05-09 22:20:30 +08003076 }
XiaokangQian7807f9f2022-02-15 10:04:37 +00003077 break;
3078
Jerry Yu5b64ae92022-03-30 17:15:02 +08003079 case MBEDTLS_SSL_SERVER_HELLO:
Gilles Peskine449bd832023-01-11 14:50:10 +01003080 ret = ssl_tls13_write_server_hello(ssl);
Jerry Yu5b64ae92022-03-30 17:15:02 +08003081 break;
3082
Xiaofei Baicba64af2022-02-15 10:00:56 +00003083 case MBEDTLS_SSL_ENCRYPTED_EXTENSIONS:
Gilles Peskine449bd832023-01-11 14:50:10 +01003084 ret = ssl_tls13_write_encrypted_extensions(ssl);
3085 if (ret != 0) {
3086 MBEDTLS_SSL_DEBUG_RET(1, "ssl_tls13_write_encrypted_extensions", ret);
3087 return ret;
Xiaofei Baicba64af2022-02-15 10:00:56 +00003088 }
Jerry Yu48330562022-05-06 21:35:44 +08003089 break;
Jerry Yu6a2cd9e2022-05-05 11:14:19 +08003090
Ronald Cron928cbd32022-10-04 16:14:26 +02003091#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED)
Xiaofei Bai9ca09d42022-02-14 12:57:18 +00003092 case MBEDTLS_SSL_CERTIFICATE_REQUEST:
Gilles Peskine449bd832023-01-11 14:50:10 +01003093 ret = ssl_tls13_write_certificate_request(ssl);
Xiaofei Bai9ca09d42022-02-14 12:57:18 +00003094 break;
Xiaofei Bai9ca09d42022-02-14 12:57:18 +00003095
Jerry Yu83da34e2022-04-16 13:59:52 +08003096 case MBEDTLS_SSL_SERVER_CERTIFICATE:
Gilles Peskine449bd832023-01-11 14:50:10 +01003097 ret = ssl_tls13_write_server_certificate(ssl);
Jerry Yu83da34e2022-04-16 13:59:52 +08003098 break;
3099
3100 case MBEDTLS_SSL_CERTIFICATE_VERIFY:
Gilles Peskine449bd832023-01-11 14:50:10 +01003101 ret = ssl_tls13_write_certificate_verify(ssl);
Jerry Yu83da34e2022-04-16 13:59:52 +08003102 break;
Ronald Cron928cbd32022-10-04 16:14:26 +02003103#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED */
Jerry Yu83da34e2022-04-16 13:59:52 +08003104
Gilles Peskine449bd832023-01-11 14:50:10 +01003105 /*
3106 * Injection of dummy-CCS's for middlebox compatibility
3107 */
Gabor Mezei7b39bf12022-05-24 16:04:14 +02003108#if defined(MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE)
Gabor Mezeif7044ea2022-06-28 16:01:49 +02003109 case MBEDTLS_SSL_SERVER_CCS_AFTER_HELLO_RETRY_REQUEST:
Gilles Peskine449bd832023-01-11 14:50:10 +01003110 ret = mbedtls_ssl_tls13_write_change_cipher_spec(ssl);
3111 if (ret == 0) {
3112 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_CLIENT_HELLO);
3113 }
Gabor Mezei7b39bf12022-05-24 16:04:14 +02003114 break;
3115
3116 case MBEDTLS_SSL_SERVER_CCS_AFTER_SERVER_HELLO:
Gilles Peskine449bd832023-01-11 14:50:10 +01003117 ret = mbedtls_ssl_tls13_write_change_cipher_spec(ssl);
3118 if (ret == 0) {
3119 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_ENCRYPTED_EXTENSIONS);
3120 }
Gabor Mezei7b39bf12022-05-24 16:04:14 +02003121 break;
3122#endif /* MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE */
3123
Jerry Yu69dd8d42022-04-16 12:51:26 +08003124 case MBEDTLS_SSL_SERVER_FINISHED:
Gilles Peskine449bd832023-01-11 14:50:10 +01003125 ret = ssl_tls13_write_server_finished(ssl);
Jerry Yu69dd8d42022-04-16 12:51:26 +08003126 break;
3127
3128 case MBEDTLS_SSL_CLIENT_FINISHED:
Gilles Peskine449bd832023-01-11 14:50:10 +01003129 ret = ssl_tls13_process_client_finished(ssl);
Jerry Yu69dd8d42022-04-16 12:51:26 +08003130 break;
3131
Jerry Yu69dd8d42022-04-16 12:51:26 +08003132 case MBEDTLS_SSL_HANDSHAKE_WRAPUP:
Gilles Peskine449bd832023-01-11 14:50:10 +01003133 ret = ssl_tls13_handshake_wrapup(ssl);
Jerry Yu69dd8d42022-04-16 12:51:26 +08003134 break;
3135
Ronald Cron766c0cd2022-10-18 12:17:11 +02003136#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED)
XiaokangQian6b916b12022-04-25 07:29:34 +00003137 case MBEDTLS_SSL_CLIENT_CERTIFICATE:
Gilles Peskine449bd832023-01-11 14:50:10 +01003138 ret = mbedtls_ssl_tls13_process_certificate(ssl);
3139 if (ret == 0) {
3140 if (ssl->session_negotiate->peer_cert != NULL) {
Ronald Cron209cae92022-06-07 10:30:19 +02003141 mbedtls_ssl_handshake_set_state(
Gilles Peskine449bd832023-01-11 14:50:10 +01003142 ssl, MBEDTLS_SSL_CLIENT_CERTIFICATE_VERIFY);
3143 } else {
3144 MBEDTLS_SSL_DEBUG_MSG(2, ("skip parse certificate verify"));
Ronald Cron209cae92022-06-07 10:30:19 +02003145 mbedtls_ssl_handshake_set_state(
Gilles Peskine449bd832023-01-11 14:50:10 +01003146 ssl, MBEDTLS_SSL_CLIENT_FINISHED);
Ronald Cron19385882022-06-15 16:26:13 +02003147 }
XiaokangQian6b916b12022-04-25 07:29:34 +00003148 }
3149 break;
3150
3151 case MBEDTLS_SSL_CLIENT_CERTIFICATE_VERIFY:
Gilles Peskine449bd832023-01-11 14:50:10 +01003152 ret = mbedtls_ssl_tls13_process_certificate_verify(ssl);
3153 if (ret == 0) {
XiaokangQian6b916b12022-04-25 07:29:34 +00003154 mbedtls_ssl_handshake_set_state(
Gilles Peskine449bd832023-01-11 14:50:10 +01003155 ssl, MBEDTLS_SSL_CLIENT_FINISHED);
XiaokangQian6b916b12022-04-25 07:29:34 +00003156 }
3157 break;
Ronald Cron766c0cd2022-10-18 12:17:11 +02003158#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED */
XiaokangQian6b916b12022-04-25 07:29:34 +00003159
Jerry Yue67bef42022-07-07 07:29:42 +00003160#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Jerry Yua8d3c502022-10-30 14:51:23 +08003161 case MBEDTLS_SSL_TLS1_3_NEW_SESSION_TICKET:
Gilles Peskine449bd832023-01-11 14:50:10 +01003162 ret = ssl_tls13_write_new_session_ticket(ssl);
3163 if (ret != 0) {
3164 MBEDTLS_SSL_DEBUG_RET(1,
3165 "ssl_tls13_write_new_session_ticket ",
3166 ret);
Jerry Yue67bef42022-07-07 07:29:42 +00003167 }
3168 break;
Jerry Yua8d3c502022-10-30 14:51:23 +08003169 case MBEDTLS_SSL_TLS1_3_NEW_SESSION_TICKET_FLUSH:
Jerry Yue67bef42022-07-07 07:29:42 +00003170 /* This state is necessary to do the flush of the New Session
Jerry Yua8d3c502022-10-30 14:51:23 +08003171 * Ticket message written in MBEDTLS_SSL_TLS1_3_NEW_SESSION_TICKET
Jerry Yue67bef42022-07-07 07:29:42 +00003172 * as part of ssl_prepare_handshake_step.
3173 */
3174 ret = 0;
Jerry Yud4e75002022-08-09 13:33:50 +08003175
Gilles Peskine449bd832023-01-11 14:50:10 +01003176 if (ssl->handshake->new_session_tickets_count == 0) {
3177 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_HANDSHAKE_OVER);
3178 } else {
Xiaokang Qian9f1747b2023-03-30 02:50:04 +00003179 mbedtls_ssl_handshake_set_state(
3180 ssl, MBEDTLS_SSL_TLS1_3_NEW_SESSION_TICKET);
Gilles Peskine449bd832023-01-11 14:50:10 +01003181 }
Jerry Yue67bef42022-07-07 07:29:42 +00003182 break;
3183
3184#endif /* MBEDTLS_SSL_SESSION_TICKETS */
3185
XiaokangQian7807f9f2022-02-15 10:04:37 +00003186 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01003187 MBEDTLS_SSL_DEBUG_MSG(1, ("invalid state %d", ssl->state));
3188 return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
XiaokangQian7807f9f2022-02-15 10:04:37 +00003189 }
3190
Gilles Peskine449bd832023-01-11 14:50:10 +01003191 return ret;
Jerry Yub9930e72021-08-06 17:11:51 +08003192}
Jerry Yu3cc4c2a2021-08-06 16:29:08 +08003193
Jerry Yufb4b6472022-01-27 15:03:26 +08003194#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_PROTO_TLS1_3 */