blob: cf611918773dd8eb3e806dfeaaadb2e69a9e1071 [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é-Gonnard02b10d82023-03-28 12:33:20 +020028#include "md_psa.h"
Jerry Yu3bf2c642022-03-30 22:02:12 +080029
Xiaofei Bai9ca09d42022-02-14 12:57:18 +000030#include "ssl_misc.h"
31#include "ssl_tls13_keys.h"
32#include "ssl_debug_helpers.h"
33
Jerry Yuf35ba382022-08-23 17:58:26 +080034
Jerry Yuc5a23a02022-08-25 10:51:44 +080035static const mbedtls_ssl_ciphersuite_t *ssl_tls13_validate_peer_ciphersuite(
Gilles Peskine449bd832023-01-11 14:50:10 +010036 mbedtls_ssl_context *ssl,
37 unsigned int cipher_suite)
Jerry Yuf35ba382022-08-23 17:58:26 +080038{
39 const mbedtls_ssl_ciphersuite_t *ciphersuite_info;
Gilles Peskine449bd832023-01-11 14:50:10 +010040 if (!mbedtls_ssl_tls13_cipher_suite_is_offered(ssl, cipher_suite)) {
41 return NULL;
Jerry Yuf35ba382022-08-23 17:58:26 +080042 }
Gilles Peskine449bd832023-01-11 14:50:10 +010043
44 ciphersuite_info = mbedtls_ssl_ciphersuite_from_id(cipher_suite);
45 if ((mbedtls_ssl_validate_ciphersuite(ssl, ciphersuite_info,
46 ssl->tls_version,
47 ssl->tls_version) != 0)) {
48 return NULL;
49 }
50 return ciphersuite_info;
Jerry Yuf35ba382022-08-23 17:58:26 +080051}
52
Ronald Cron41a443a2022-10-04 16:38:25 +020053#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED)
Jerry Yue19e3b92022-07-08 12:04:51 +000054/* From RFC 8446:
55 *
56 * enum { psk_ke(0), psk_dhe_ke(1), (255) } PskKeyExchangeMode;
57 * struct {
58 * PskKeyExchangeMode ke_modes<1..255>;
59 * } PskKeyExchangeModes;
60 */
Jerry Yu6e74a7e2022-07-20 20:49:32 +080061MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +010062static int ssl_tls13_parse_key_exchange_modes_ext(mbedtls_ssl_context *ssl,
63 const unsigned char *buf,
64 const unsigned char *end)
Jerry Yue19e3b92022-07-08 12:04:51 +000065{
Jerry Yu299e31f2022-07-13 23:06:36 +080066 const unsigned char *p = buf;
Jerry Yue19e3b92022-07-08 12:04:51 +000067 size_t ke_modes_len;
68 int ke_modes = 0;
69
Jerry Yu854dd9e2022-07-15 14:28:27 +080070 /* Read ke_modes length (1 Byte) */
Gilles Peskine449bd832023-01-11 14:50:10 +010071 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, 1);
Jerry Yu299e31f2022-07-13 23:06:36 +080072 ke_modes_len = *p++;
Jerry Yue19e3b92022-07-08 12:04:51 +000073 /* Currently, there are only two PSK modes, so even without looking
74 * at the content, something's wrong if the list has more than 2 items. */
Gilles Peskine449bd832023-01-11 14:50:10 +010075 if (ke_modes_len > 2) {
76 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER,
77 MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER);
78 return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
Jerry Yu299e31f2022-07-13 23:06:36 +080079 }
Jerry Yue19e3b92022-07-08 12:04:51 +000080
Gilles Peskine449bd832023-01-11 14:50:10 +010081 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, ke_modes_len);
Jerry Yue19e3b92022-07-08 12:04:51 +000082
Gilles Peskine449bd832023-01-11 14:50:10 +010083 while (ke_modes_len-- != 0) {
84 switch (*p++) {
85 case MBEDTLS_SSL_TLS1_3_PSK_MODE_PURE:
86 ke_modes |= MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK;
87 MBEDTLS_SSL_DEBUG_MSG(3, ("Found PSK KEX MODE"));
88 break;
89 case MBEDTLS_SSL_TLS1_3_PSK_MODE_ECDHE:
90 ke_modes |= MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK_EPHEMERAL;
91 MBEDTLS_SSL_DEBUG_MSG(3, ("Found PSK_EPHEMERAL KEX MODE"));
92 break;
93 default:
94 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER,
95 MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER);
96 return MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER;
Jerry Yue19e3b92022-07-08 12:04:51 +000097 }
98 }
99
100 ssl->handshake->tls13_kex_modes = ke_modes;
Gilles Peskine449bd832023-01-11 14:50:10 +0100101 return 0;
Jerry Yue19e3b92022-07-08 12:04:51 +0000102}
Jerry Yu1c105562022-07-10 06:32:38 +0000103
Jerry Yue9d4fc02022-08-20 19:21:15 +0800104#define SSL_TLS1_3_OFFERED_PSK_NOT_MATCH 1
105#define SSL_TLS1_3_OFFERED_PSK_MATCH 0
Jerry Yu95699e72022-08-21 19:22:23 +0800106
107#if defined(MBEDTLS_SSL_SESSION_TICKETS)
108
109MBEDTLS_CHECK_RETURN_CRITICAL
110static int ssl_tls13_offered_psks_check_identity_match_ticket(
Gilles Peskine449bd832023-01-11 14:50:10 +0100111 mbedtls_ssl_context *ssl,
112 const unsigned char *identity,
113 size_t identity_len,
114 uint32_t obfuscated_ticket_age,
115 mbedtls_ssl_session *session)
Jerry Yu95699e72022-08-21 19:22:23 +0800116{
Jerry Yufd310eb2022-09-06 09:16:35 +0800117 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yu95699e72022-08-21 19:22:23 +0800118 unsigned char *ticket_buffer;
Jerry Yu8d4bbba2022-09-13 14:15:48 +0800119#if defined(MBEDTLS_HAVE_TIME)
120 mbedtls_time_t now;
Jerry Yuacff8232022-09-14 14:35:11 +0800121 uint64_t age_in_s;
Jerry Yuf7dad3c2022-09-14 22:31:39 +0800122 int64_t age_diff_in_ms;
Jerry Yu8d4bbba2022-09-13 14:15:48 +0800123#endif
Jerry Yu95699e72022-08-21 19:22:23 +0800124
125 ((void) obfuscated_ticket_age);
126
Gilles Peskine449bd832023-01-11 14:50:10 +0100127 MBEDTLS_SSL_DEBUG_MSG(2, ("=> check_identity_match_ticket"));
Jerry Yu95699e72022-08-21 19:22:23 +0800128
Jerry Yufd310eb2022-09-06 09:16:35 +0800129 /* Ticket parser is not configured, Skip */
Gilles Peskine449bd832023-01-11 14:50:10 +0100130 if (ssl->conf->f_ticket_parse == NULL || identity_len == 0) {
131 return 0;
132 }
Jerry Yu95699e72022-08-21 19:22:23 +0800133
Jerry Yu4746b102022-09-13 11:11:48 +0800134 /* We create a copy of the encrypted ticket since the ticket parsing
135 * function is allowed to use its input buffer as an output buffer
136 * (in-place decryption). We do, however, need the original buffer for
137 * computing the PSK binder value.
Jerry Yu95699e72022-08-21 19:22:23 +0800138 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100139 ticket_buffer = mbedtls_calloc(1, identity_len);
140 if (ticket_buffer == NULL) {
141 MBEDTLS_SSL_DEBUG_MSG(1, ("buffer too small"));
142 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
Jerry Yu95699e72022-08-21 19:22:23 +0800143 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100144 memcpy(ticket_buffer, identity, identity_len);
Jerry Yu95699e72022-08-21 19:22:23 +0800145
Gilles Peskine449bd832023-01-11 14:50:10 +0100146 if ((ret = ssl->conf->f_ticket_parse(ssl->conf->p_ticket,
147 session,
148 ticket_buffer, identity_len)) != 0) {
149 if (ret == MBEDTLS_ERR_SSL_INVALID_MAC) {
150 MBEDTLS_SSL_DEBUG_MSG(3, ("ticket is not authentic"));
151 } else if (ret == MBEDTLS_ERR_SSL_SESSION_TICKET_EXPIRED) {
152 MBEDTLS_SSL_DEBUG_MSG(3, ("ticket is expired"));
153 } else {
154 MBEDTLS_SSL_DEBUG_RET(1, "ticket_parse", ret);
155 }
Jerry Yu95699e72022-08-21 19:22:23 +0800156 }
157
158 /* We delete the temporary buffer */
Gilles Peskine449bd832023-01-11 14:50:10 +0100159 mbedtls_free(ticket_buffer);
Jerry Yu95699e72022-08-21 19:22:23 +0800160
Gilles Peskine449bd832023-01-11 14:50:10 +0100161 if (ret != 0) {
Jerry Yu8d4bbba2022-09-13 14:15:48 +0800162 goto exit;
163 }
Jerry Yu95699e72022-08-21 19:22:23 +0800164
Pengyu Lv3eb49be2022-12-05 16:35:12 +0800165 /* RFC 8446 section 4.2.9
166 *
167 * Servers SHOULD NOT send NewSessionTicket with tickets that are not
168 * compatible with the advertised modes; however, if a server does so,
169 * the impact will just be that the client's attempts at resumption fail.
170 *
171 * We regard the ticket with incompatible key exchange modes as not match.
172 */
Pengyu Lv18946532023-01-12 12:28:09 +0800173 ret = MBEDTLS_ERR_ERROR_GENERIC_ERROR;
Pengyu Lv4938a562023-01-16 11:28:49 +0800174 MBEDTLS_SSL_PRINT_TICKET_FLAGS(4,
Pengyu Lv80270b22023-01-12 11:54:04 +0800175 session->ticket_flags);
Pengyu Lve2f1dbf2023-01-16 12:28:27 +0800176 if (mbedtls_ssl_tls13_check_kex_modes(
177 ssl,
178 mbedtls_ssl_session_get_ticket_flags(
179 session,
180 MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK_ALL))) {
Pengyu Lv3eb49be2022-12-05 16:35:12 +0800181 MBEDTLS_SSL_DEBUG_MSG(3, ("No suitable key exchange mode"));
182 goto exit;
183 }
184
Gilles Peskine449bd832023-01-11 14:50:10 +0100185 ret = MBEDTLS_ERR_SSL_SESSION_TICKET_EXPIRED;
186#if defined(MBEDTLS_HAVE_TIME)
187 now = mbedtls_time(NULL);
188
189 if (now < session->start) {
190 MBEDTLS_SSL_DEBUG_MSG(
191 3, ("Invalid ticket start time ( now=%" MBEDTLS_PRINTF_LONGLONG
192 ", start=%" MBEDTLS_PRINTF_LONGLONG " )",
193 (long long) now, (long long) session->start));
194 goto exit;
195 }
196
197 age_in_s = (uint64_t) (now - session->start);
Jerry Yu95699e72022-08-21 19:22:23 +0800198
Jerry Yu8d4bbba2022-09-13 14:15:48 +0800199 /* RFC 8446 section 4.6.1
200 *
201 * Servers MUST NOT use any value greater than 604800 seconds (7 days).
202 *
203 * RFC 8446 section 4.2.11.1
204 *
205 * Clients MUST NOT attempt to use tickets which have ages greater than
206 * the "ticket_lifetime" value which was provided with the ticket.
207 *
208 * For time being, the age MUST be less than 604800 seconds (7 days).
209 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100210 if (age_in_s > 604800) {
Jerry Yu8d4bbba2022-09-13 14:15:48 +0800211 MBEDTLS_SSL_DEBUG_MSG(
Gilles Peskine449bd832023-01-11 14:50:10 +0100212 3, ("Ticket age exceeds limitation ticket_age=%lu",
213 (long unsigned int) age_in_s));
Jerry Yu8d4bbba2022-09-13 14:15:48 +0800214 goto exit;
215 }
Jerry Yu95699e72022-08-21 19:22:23 +0800216
Jerry Yu8d4bbba2022-09-13 14:15:48 +0800217 /* RFC 8446 section 4.2.10
218 *
219 * For PSKs provisioned via NewSessionTicket, a server MUST validate that
220 * the ticket age for the selected PSK identity (computed by subtracting
221 * ticket_age_add from PskIdentity.obfuscated_ticket_age modulo 2^32) is
222 * within a small tolerance of the time since the ticket was issued.
Jerry Yuf7dad3c2022-09-14 22:31:39 +0800223 *
Jerry Yu0a55cc62022-09-15 16:15:06 +0800224 * NOTE: When `now == session->start`, `age_diff_in_ms` may be negative
225 * as the age units are different on the server (s) and in the
226 * client (ms) side. Add a -1000 ms tolerance window to take this
227 * into account.
Jerry Yu8d4bbba2022-09-13 14:15:48 +0800228 */
Jerry Yuf7dad3c2022-09-14 22:31:39 +0800229 age_diff_in_ms = age_in_s * 1000;
Gilles Peskine449bd832023-01-11 14:50:10 +0100230 age_diff_in_ms -= (obfuscated_ticket_age - session->ticket_age_add);
231 if (age_diff_in_ms <= -1000 ||
232 age_diff_in_ms > MBEDTLS_SSL_TLS1_3_TICKET_AGE_TOLERANCE) {
Jerry Yu8d4bbba2022-09-13 14:15:48 +0800233 MBEDTLS_SSL_DEBUG_MSG(
Gilles Peskine449bd832023-01-11 14:50:10 +0100234 3, ("Ticket age outside tolerance window ( diff=%d )",
235 (int) age_diff_in_ms));
Jerry Yu8d4bbba2022-09-13 14:15:48 +0800236 goto exit;
237 }
238
239 ret = 0;
Jerry Yu95699e72022-08-21 19:22:23 +0800240
241#endif /* MBEDTLS_HAVE_TIME */
Jerry Yu8d4bbba2022-09-13 14:15:48 +0800242
243exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100244 if (ret != 0) {
245 mbedtls_ssl_session_free(session);
246 }
Jerry Yu95699e72022-08-21 19:22:23 +0800247
Gilles Peskine449bd832023-01-11 14:50:10 +0100248 MBEDTLS_SSL_DEBUG_MSG(2, ("<= check_identity_match_ticket"));
249 return ret;
Jerry Yu95699e72022-08-21 19:22:23 +0800250}
251#endif /* MBEDTLS_SSL_SESSION_TICKETS */
252
Jerry Yu6e74a7e2022-07-20 20:49:32 +0800253MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yu1c105562022-07-10 06:32:38 +0000254static int ssl_tls13_offered_psks_check_identity_match(
Gilles Peskine449bd832023-01-11 14:50:10 +0100255 mbedtls_ssl_context *ssl,
256 const unsigned char *identity,
257 size_t identity_len,
258 uint32_t obfuscated_ticket_age,
259 int *psk_type,
260 mbedtls_ssl_session *session)
Jerry Yu1c105562022-07-10 06:32:38 +0000261{
Ronald Cron606671e2023-02-17 11:36:33 +0100262 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
263
Jerry Yu95699e72022-08-21 19:22:23 +0800264 ((void) session);
265 ((void) obfuscated_ticket_age);
Jerry Yu5725f1c2022-08-21 17:27:16 +0800266 *psk_type = MBEDTLS_SSL_TLS1_3_PSK_EXTERNAL;
Jerry Yu95699e72022-08-21 19:22:23 +0800267
Gilles Peskine449bd832023-01-11 14:50:10 +0100268 MBEDTLS_SSL_DEBUG_BUF(4, "identity", identity, identity_len);
Jerry Yu95699e72022-08-21 19:22:23 +0800269 ssl->handshake->resume = 0;
270
Jerry Yu95699e72022-08-21 19:22:23 +0800271#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Gilles Peskine449bd832023-01-11 14:50:10 +0100272 if (ssl_tls13_offered_psks_check_identity_match_ticket(
Jerry Yu8d4bbba2022-09-13 14:15:48 +0800273 ssl, identity, identity_len, obfuscated_ticket_age,
Gilles Peskine449bd832023-01-11 14:50:10 +0100274 session) == SSL_TLS1_3_OFFERED_PSK_MATCH) {
Jerry Yu95699e72022-08-21 19:22:23 +0800275 ssl->handshake->resume = 1;
276 *psk_type = MBEDTLS_SSL_TLS1_3_PSK_RESUMPTION;
Ronald Cron606671e2023-02-17 11:36:33 +0100277 ret = mbedtls_ssl_set_hs_psk(ssl,
278 session->resumption_key,
279 session->resumption_key_len);
280 if (ret != 0) {
281 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_set_hs_psk", ret);
282 return ret;
283 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100284
285 MBEDTLS_SSL_DEBUG_BUF(4, "Ticket-resumed PSK:",
286 session->resumption_key,
287 session->resumption_key_len);
288 MBEDTLS_SSL_DEBUG_MSG(4, ("ticket: obfuscated_ticket_age: %u",
289 (unsigned) obfuscated_ticket_age));
290 return SSL_TLS1_3_OFFERED_PSK_MATCH;
Jerry Yu95699e72022-08-21 19:22:23 +0800291 }
292#endif /* MBEDTLS_SSL_SESSION_TICKETS */
293
Jerry Yu1c105562022-07-10 06:32:38 +0000294 /* Check identity with external configured function */
Gilles Peskine449bd832023-01-11 14:50:10 +0100295 if (ssl->conf->f_psk != NULL) {
296 if (ssl->conf->f_psk(
297 ssl->conf->p_psk, ssl, identity, identity_len) == 0) {
298 return SSL_TLS1_3_OFFERED_PSK_MATCH;
Jerry Yu1c105562022-07-10 06:32:38 +0000299 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100300 return SSL_TLS1_3_OFFERED_PSK_NOT_MATCH;
Jerry Yu1c105562022-07-10 06:32:38 +0000301 }
302
Gilles Peskine449bd832023-01-11 14:50:10 +0100303 MBEDTLS_SSL_DEBUG_BUF(5, "identity", identity, identity_len);
Jerry Yu1c105562022-07-10 06:32:38 +0000304 /* Check identity with pre-configured psk */
Gilles Peskine449bd832023-01-11 14:50:10 +0100305 if (ssl->conf->psk_identity != NULL &&
Jerry Yu2f0abc92022-07-22 19:34:48 +0800306 identity_len == ssl->conf->psk_identity_len &&
Gilles Peskine449bd832023-01-11 14:50:10 +0100307 mbedtls_ct_memcmp(ssl->conf->psk_identity,
308 identity, identity_len) == 0) {
Ronald Cron606671e2023-02-17 11:36:33 +0100309 ret = mbedtls_ssl_set_hs_psk(ssl, ssl->conf->psk, ssl->conf->psk_len);
310 if (ret != 0) {
311 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_set_hs_psk", ret);
312 return ret;
313 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100314 return SSL_TLS1_3_OFFERED_PSK_MATCH;
Jerry Yu1c105562022-07-10 06:32:38 +0000315 }
316
Gilles Peskine449bd832023-01-11 14:50:10 +0100317 return SSL_TLS1_3_OFFERED_PSK_NOT_MATCH;
Jerry Yu1c105562022-07-10 06:32:38 +0000318}
319
Jerry Yu6e74a7e2022-07-20 20:49:32 +0800320MBEDTLS_CHECK_RETURN_CRITICAL
Xiaokang Qian9f1747b2023-03-30 02:50:04 +0000321static int ssl_tls13_offered_psks_check_binder_match(
322 mbedtls_ssl_context *ssl,
323 const unsigned char *binder, size_t binder_len,
324 int psk_type, psa_algorithm_t psk_hash_alg)
Jerry Yu1c105562022-07-10 06:32:38 +0000325{
326 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yu96a2e362022-07-21 15:11:34 +0800327
Jerry Yudaf375a2022-07-20 21:31:43 +0800328 unsigned char transcript[PSA_HASH_MAX_SIZE];
Jerry Yu1c105562022-07-10 06:32:38 +0000329 size_t transcript_len;
Jerry Yu2f0abc92022-07-22 19:34:48 +0800330 unsigned char *psk;
Jerry Yu96a2e362022-07-21 15:11:34 +0800331 size_t psk_len;
Jerry Yudaf375a2022-07-20 21:31:43 +0800332 unsigned char server_computed_binder[PSA_HASH_MAX_SIZE];
Jerry Yu1c105562022-07-10 06:32:38 +0000333
Jerry Yu1c105562022-07-10 06:32:38 +0000334 /* Get current state of handshake transcript. */
Jerry Yu29d9faa2022-08-23 17:52:45 +0800335 ret = mbedtls_ssl_get_handshake_transcript(
Manuel Pégourié-Gonnard2d6d9932023-03-28 11:38:08 +0200336 ssl, mbedtls_md_type_from_psa_alg(psk_hash_alg),
Gilles Peskine449bd832023-01-11 14:50:10 +0100337 transcript, sizeof(transcript), &transcript_len);
338 if (ret != 0) {
339 return ret;
340 }
Jerry Yu1c105562022-07-10 06:32:38 +0000341
Gilles Peskine449bd832023-01-11 14:50:10 +0100342 ret = mbedtls_ssl_tls13_export_handshake_psk(ssl, &psk, &psk_len);
343 if (ret != 0) {
344 return ret;
345 }
Jerry Yu96a2e362022-07-21 15:11:34 +0800346
Gilles Peskine449bd832023-01-11 14:50:10 +0100347 ret = mbedtls_ssl_tls13_create_psk_binder(ssl, psk_hash_alg,
348 psk, psk_len, psk_type,
349 transcript,
350 server_computed_binder);
Jerry Yu96a2e362022-07-21 15:11:34 +0800351#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +0100352 mbedtls_free((void *) psk);
Jerry Yu96a2e362022-07-21 15:11:34 +0800353#endif
Gilles Peskine449bd832023-01-11 14:50:10 +0100354 if (ret != 0) {
355 MBEDTLS_SSL_DEBUG_MSG(1, ("PSK binder calculation failed."));
356 return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
Jerry Yu1c105562022-07-10 06:32:38 +0000357 }
358
Gilles Peskine449bd832023-01-11 14:50:10 +0100359 MBEDTLS_SSL_DEBUG_BUF(3, "psk binder ( computed ): ",
360 server_computed_binder, transcript_len);
361 MBEDTLS_SSL_DEBUG_BUF(3, "psk binder ( received ): ", binder, binder_len);
Jerry Yu1c105562022-07-10 06:32:38 +0000362
Gilles Peskine449bd832023-01-11 14:50:10 +0100363 if (mbedtls_ct_memcmp(server_computed_binder, binder, binder_len) == 0) {
364 return SSL_TLS1_3_OFFERED_PSK_MATCH;
Jerry Yu1c105562022-07-10 06:32:38 +0000365 }
366
Gilles Peskine449bd832023-01-11 14:50:10 +0100367 mbedtls_platform_zeroize(server_computed_binder,
368 sizeof(server_computed_binder));
369 return SSL_TLS1_3_OFFERED_PSK_NOT_MATCH;
Jerry Yu1c105562022-07-10 06:32:38 +0000370}
Jerry Yu96a2e362022-07-21 15:11:34 +0800371
Jerry Yu5725f1c2022-08-21 17:27:16 +0800372MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yuf35ba382022-08-23 17:58:26 +0800373static int ssl_tls13_select_ciphersuite_for_psk(
Gilles Peskine449bd832023-01-11 14:50:10 +0100374 mbedtls_ssl_context *ssl,
375 const unsigned char *cipher_suites,
376 const unsigned char *cipher_suites_end,
377 uint16_t *selected_ciphersuite,
378 const mbedtls_ssl_ciphersuite_t **selected_ciphersuite_info)
Jerry Yu5725f1c2022-08-21 17:27:16 +0800379{
Jerry Yuf35ba382022-08-23 17:58:26 +0800380 psa_algorithm_t psk_hash_alg = PSA_ALG_SHA_256;
Jerry Yu5725f1c2022-08-21 17:27:16 +0800381
Jerry Yu0baf9072022-08-25 11:21:04 +0800382 *selected_ciphersuite = 0;
383 *selected_ciphersuite_info = NULL;
384
Jerry Yuf35ba382022-08-23 17:58:26 +0800385 /* RFC 8446, page 55.
386 *
387 * For externally established PSKs, the Hash algorithm MUST be set when the
388 * PSK is established or default to SHA-256 if no such algorithm is defined.
389 *
390 */
Jerry Yu5725f1c2022-08-21 17:27:16 +0800391
Jerry Yu5725f1c2022-08-21 17:27:16 +0800392 /*
393 * Search for a matching ciphersuite
394 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100395 for (const unsigned char *p = cipher_suites;
396 p < cipher_suites_end; p += 2) {
Jerry Yu5725f1c2022-08-21 17:27:16 +0800397 uint16_t cipher_suite;
Jerry Yuf35ba382022-08-23 17:58:26 +0800398 const mbedtls_ssl_ciphersuite_t *ciphersuite_info;
Jerry Yu5725f1c2022-08-21 17:27:16 +0800399
Gilles Peskine449bd832023-01-11 14:50:10 +0100400 cipher_suite = MBEDTLS_GET_UINT16_BE(p, 0);
401 ciphersuite_info = ssl_tls13_validate_peer_ciphersuite(ssl,
402 cipher_suite);
403 if (ciphersuite_info == NULL) {
Jerry Yu5725f1c2022-08-21 17:27:16 +0800404 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +0100405 }
Jerry Yu5725f1c2022-08-21 17:27:16 +0800406
Jerry Yu5725f1c2022-08-21 17:27:16 +0800407 /* MAC of selected ciphersuite MUST be same with PSK binder if exist.
408 * Otherwise, client should reject.
409 */
Manuel Pégourié-Gonnard1f2a5872023-03-28 11:46:17 +0200410 if (psk_hash_alg == mbedtls_md_psa_alg_from_type(ciphersuite_info->mac)) {
Jerry Yuf35ba382022-08-23 17:58:26 +0800411 *selected_ciphersuite = cipher_suite;
412 *selected_ciphersuite_info = ciphersuite_info;
Gilles Peskine449bd832023-01-11 14:50:10 +0100413 return 0;
Jerry Yuf35ba382022-08-23 17:58:26 +0800414 }
415 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100416 MBEDTLS_SSL_DEBUG_MSG(2, ("No matched ciphersuite"));
417 return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
Jerry Yuf35ba382022-08-23 17:58:26 +0800418}
Jerry Yu5725f1c2022-08-21 17:27:16 +0800419
Jerry Yu82534862022-08-30 10:42:33 +0800420#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Jerry Yuf35ba382022-08-23 17:58:26 +0800421MBEDTLS_CHECK_RETURN_CRITICAL
422static int ssl_tls13_select_ciphersuite_for_resumption(
Gilles Peskine449bd832023-01-11 14:50:10 +0100423 mbedtls_ssl_context *ssl,
424 const unsigned char *cipher_suites,
425 const unsigned char *cipher_suites_end,
426 mbedtls_ssl_session *session,
427 uint16_t *selected_ciphersuite,
428 const mbedtls_ssl_ciphersuite_t **selected_ciphersuite_info)
Jerry Yuf35ba382022-08-23 17:58:26 +0800429{
Jerry Yu82534862022-08-30 10:42:33 +0800430
Jerry Yuf35ba382022-08-23 17:58:26 +0800431 *selected_ciphersuite = 0;
432 *selected_ciphersuite_info = NULL;
Gilles Peskine449bd832023-01-11 14:50:10 +0100433 for (const unsigned char *p = cipher_suites; p < cipher_suites_end; p += 2) {
434 uint16_t cipher_suite = MBEDTLS_GET_UINT16_BE(p, 0);
Jerry Yu82534862022-08-30 10:42:33 +0800435 const mbedtls_ssl_ciphersuite_t *ciphersuite_info;
436
Gilles Peskine449bd832023-01-11 14:50:10 +0100437 if (cipher_suite != session->ciphersuite) {
Jerry Yu82534862022-08-30 10:42:33 +0800438 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +0100439 }
Jerry Yu82534862022-08-30 10:42:33 +0800440
Gilles Peskine449bd832023-01-11 14:50:10 +0100441 ciphersuite_info = ssl_tls13_validate_peer_ciphersuite(ssl,
442 cipher_suite);
443 if (ciphersuite_info == NULL) {
Jerry Yu82534862022-08-30 10:42:33 +0800444 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +0100445 }
Jerry Yu82534862022-08-30 10:42:33 +0800446
Jerry Yu4746b102022-09-13 11:11:48 +0800447 *selected_ciphersuite = cipher_suite;
Jerry Yu82534862022-08-30 10:42:33 +0800448 *selected_ciphersuite_info = ciphersuite_info;
449
Gilles Peskine449bd832023-01-11 14:50:10 +0100450 return 0;
Jerry Yu82534862022-08-30 10:42:33 +0800451 }
452
Gilles Peskine449bd832023-01-11 14:50:10 +0100453 return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
Jerry Yu5725f1c2022-08-21 17:27:16 +0800454}
Jerry Yuf35ba382022-08-23 17:58:26 +0800455
Jerry Yu82534862022-08-30 10:42:33 +0800456MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100457static int ssl_tls13_session_copy_ticket(mbedtls_ssl_session *dst,
458 const mbedtls_ssl_session *src)
Jerry Yu82534862022-08-30 10:42:33 +0800459{
Jerry Yu82534862022-08-30 10:42:33 +0800460 dst->ticket_age_add = src->ticket_age_add;
461 dst->ticket_flags = src->ticket_flags;
462 dst->resumption_key_len = src->resumption_key_len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100463 if (src->resumption_key_len == 0) {
464 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
465 }
466 memcpy(dst->resumption_key, src->resumption_key, src->resumption_key_len);
Jerry Yu4746b102022-09-13 11:11:48 +0800467
Gilles Peskine449bd832023-01-11 14:50:10 +0100468 return 0;
Jerry Yu82534862022-08-30 10:42:33 +0800469}
470#endif /* MBEDTLS_SSL_SESSION_TICKETS */
471
Jerry Yu1c105562022-07-10 06:32:38 +0000472/* Parser for pre_shared_key extension in client hello
473 * struct {
474 * opaque identity<1..2^16-1>;
475 * uint32 obfuscated_ticket_age;
476 * } PskIdentity;
477 *
478 * opaque PskBinderEntry<32..255>;
479 *
480 * struct {
481 * PskIdentity identities<7..2^16-1>;
482 * PskBinderEntry binders<33..2^16-1>;
483 * } OfferedPsks;
484 *
485 * struct {
486 * select (Handshake.msg_type) {
487 * case client_hello: OfferedPsks;
488 * ....
489 * };
490 * } PreSharedKeyExtension;
491 */
Jerry Yu6e74a7e2022-07-20 20:49:32 +0800492MBEDTLS_CHECK_RETURN_CRITICAL
Xiaokang Qian9f1747b2023-03-30 02:50:04 +0000493static int ssl_tls13_parse_pre_shared_key_ext(
494 mbedtls_ssl_context *ssl,
495 const unsigned char *pre_shared_key_ext,
496 const unsigned char *pre_shared_key_ext_end,
497 const unsigned char *ciphersuites,
498 const unsigned char *ciphersuites_end)
Jerry Yu1c105562022-07-10 06:32:38 +0000499{
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +0100500 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yu29d9faa2022-08-23 17:52:45 +0800501 const unsigned char *identities = pre_shared_key_ext;
Jerry Yu568ec252022-07-22 21:27:34 +0800502 const unsigned char *p_identity_len;
503 size_t identities_len;
Jerry Yu1c105562022-07-10 06:32:38 +0000504 const unsigned char *identities_end;
Jerry Yu568ec252022-07-22 21:27:34 +0800505 const unsigned char *binders;
506 const unsigned char *p_binder_len;
507 size_t binders_len;
Jerry Yu1c105562022-07-10 06:32:38 +0000508 const unsigned char *binders_end;
Jerry Yu96a2e362022-07-21 15:11:34 +0800509 int matched_identity = -1;
510 int identity_id = -1;
Jerry Yu1c105562022-07-10 06:32:38 +0000511
Gilles Peskine449bd832023-01-11 14:50:10 +0100512 MBEDTLS_SSL_DEBUG_BUF(3, "pre_shared_key extension",
513 pre_shared_key_ext,
514 pre_shared_key_ext_end - pre_shared_key_ext);
Jerry Yu1c105562022-07-10 06:32:38 +0000515
Jerry Yu96a2e362022-07-21 15:11:34 +0800516 /* identities_len 2 bytes
517 * identities_data >= 7 bytes
518 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100519 MBEDTLS_SSL_CHK_BUF_READ_PTR(identities, pre_shared_key_ext_end, 7 + 2);
520 identities_len = MBEDTLS_GET_UINT16_BE(identities, 0);
Jerry Yu568ec252022-07-22 21:27:34 +0800521 p_identity_len = identities + 2;
Gilles Peskine449bd832023-01-11 14:50:10 +0100522 MBEDTLS_SSL_CHK_BUF_READ_PTR(p_identity_len, pre_shared_key_ext_end,
523 identities_len);
Jerry Yu568ec252022-07-22 21:27:34 +0800524 identities_end = p_identity_len + identities_len;
Jerry Yu1c105562022-07-10 06:32:38 +0000525
Jerry Yu96a2e362022-07-21 15:11:34 +0800526 /* binders_len 2 bytes
527 * binders >= 33 bytes
528 */
Jerry Yu568ec252022-07-22 21:27:34 +0800529 binders = identities_end;
Gilles Peskine449bd832023-01-11 14:50:10 +0100530 MBEDTLS_SSL_CHK_BUF_READ_PTR(binders, pre_shared_key_ext_end, 33 + 2);
531 binders_len = MBEDTLS_GET_UINT16_BE(binders, 0);
Jerry Yu568ec252022-07-22 21:27:34 +0800532 p_binder_len = binders + 2;
Gilles Peskine449bd832023-01-11 14:50:10 +0100533 MBEDTLS_SSL_CHK_BUF_READ_PTR(p_binder_len, pre_shared_key_ext_end, binders_len);
Jerry Yu568ec252022-07-22 21:27:34 +0800534 binders_end = p_binder_len + binders_len;
Jerry Yu1c105562022-07-10 06:32:38 +0000535
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +0100536 ret = ssl->handshake->update_checksum(ssl, pre_shared_key_ext,
537 identities_end - pre_shared_key_ext);
538 if (0 != ret) {
539 MBEDTLS_SSL_DEBUG_RET(1, ("update_checksum"), ret);
540 return ret;
541 }
Jerry Yu96a2e362022-07-21 15:11:34 +0800542
Gilles Peskine449bd832023-01-11 14:50:10 +0100543 while (p_identity_len < identities_end && p_binder_len < binders_end) {
Jerry Yu1c105562022-07-10 06:32:38 +0000544 const unsigned char *identity;
Jerry Yu568ec252022-07-22 21:27:34 +0800545 size_t identity_len;
Jerry Yu82534862022-08-30 10:42:33 +0800546 uint32_t obfuscated_ticket_age;
Jerry Yu96a2e362022-07-21 15:11:34 +0800547 const unsigned char *binder;
Jerry Yu568ec252022-07-22 21:27:34 +0800548 size_t binder_len;
Jerry Yu5725f1c2022-08-21 17:27:16 +0800549 int psk_type;
550 uint16_t cipher_suite;
Jerry Yu29d9faa2022-08-23 17:52:45 +0800551 const mbedtls_ssl_ciphersuite_t *ciphersuite_info;
Jerry Yu82534862022-08-30 10:42:33 +0800552#if defined(MBEDTLS_SSL_SESSION_TICKETS)
553 mbedtls_ssl_session session;
Gilles Peskine449bd832023-01-11 14:50:10 +0100554 mbedtls_ssl_session_init(&session);
Jerry Yu82534862022-08-30 10:42:33 +0800555#endif
Jerry Yubb852022022-07-20 21:10:44 +0800556
Gilles Peskine449bd832023-01-11 14:50:10 +0100557 MBEDTLS_SSL_CHK_BUF_READ_PTR(p_identity_len, identities_end, 2 + 1 + 4);
558 identity_len = MBEDTLS_GET_UINT16_BE(p_identity_len, 0);
Jerry Yu568ec252022-07-22 21:27:34 +0800559 identity = p_identity_len + 2;
Gilles Peskine449bd832023-01-11 14:50:10 +0100560 MBEDTLS_SSL_CHK_BUF_READ_PTR(identity, identities_end, identity_len + 4);
561 obfuscated_ticket_age = MBEDTLS_GET_UINT32_BE(identity, identity_len);
Jerry Yu568ec252022-07-22 21:27:34 +0800562 p_identity_len += identity_len + 6;
Jerry Yu1c105562022-07-10 06:32:38 +0000563
Gilles Peskine449bd832023-01-11 14:50:10 +0100564 MBEDTLS_SSL_CHK_BUF_READ_PTR(p_binder_len, binders_end, 1 + 32);
Jerry Yu568ec252022-07-22 21:27:34 +0800565 binder_len = *p_binder_len;
566 binder = p_binder_len + 1;
Gilles Peskine449bd832023-01-11 14:50:10 +0100567 MBEDTLS_SSL_CHK_BUF_READ_PTR(binder, binders_end, binder_len);
Jerry Yu568ec252022-07-22 21:27:34 +0800568 p_binder_len += binder_len + 1;
Jerry Yu96a2e362022-07-21 15:11:34 +0800569
Jerry Yu96a2e362022-07-21 15:11:34 +0800570 identity_id++;
Gilles Peskine449bd832023-01-11 14:50:10 +0100571 if (matched_identity != -1) {
Jerry Yu1c105562022-07-10 06:32:38 +0000572 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +0100573 }
Jerry Yu1c105562022-07-10 06:32:38 +0000574
Jerry Yu96a2e362022-07-21 15:11:34 +0800575 ret = ssl_tls13_offered_psks_check_identity_match(
Gilles Peskine449bd832023-01-11 14:50:10 +0100576 ssl, identity, identity_len, obfuscated_ticket_age,
577 &psk_type, &session);
578 if (ret != SSL_TLS1_3_OFFERED_PSK_MATCH) {
Jerry Yu96a2e362022-07-21 15:11:34 +0800579 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +0100580 }
Jerry Yu96a2e362022-07-21 15:11:34 +0800581
Gilles Peskine449bd832023-01-11 14:50:10 +0100582 MBEDTLS_SSL_DEBUG_MSG(4, ("found matched identity"));
583 switch (psk_type) {
Jerry Yu0baf9072022-08-25 11:21:04 +0800584 case MBEDTLS_SSL_TLS1_3_PSK_EXTERNAL:
585 ret = ssl_tls13_select_ciphersuite_for_psk(
Gilles Peskine449bd832023-01-11 14:50:10 +0100586 ssl, ciphersuites, ciphersuites_end,
587 &cipher_suite, &ciphersuite_info);
Jerry Yu0baf9072022-08-25 11:21:04 +0800588 break;
589 case MBEDTLS_SSL_TLS1_3_PSK_RESUMPTION:
Jerry Yu82534862022-08-30 10:42:33 +0800590#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Jerry Yu0baf9072022-08-25 11:21:04 +0800591 ret = ssl_tls13_select_ciphersuite_for_resumption(
Gilles Peskine449bd832023-01-11 14:50:10 +0100592 ssl, ciphersuites, ciphersuites_end, &session,
593 &cipher_suite, &ciphersuite_info);
594 if (ret != 0) {
595 mbedtls_ssl_session_free(&session);
596 }
Jerry Yu82534862022-08-30 10:42:33 +0800597#else
598 ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
599#endif
Jerry Yu0baf9072022-08-25 11:21:04 +0800600 break;
601 default:
Gilles Peskine449bd832023-01-11 14:50:10 +0100602 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Jerry Yu0baf9072022-08-25 11:21:04 +0800603 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100604 if (ret != 0) {
Jerry Yuf35ba382022-08-23 17:58:26 +0800605 /* See below, no cipher_suite available, abort handshake */
606 MBEDTLS_SSL_PEND_FATAL_ALERT(
607 MBEDTLS_SSL_ALERT_MSG_DECRYPT_ERROR,
Gilles Peskine449bd832023-01-11 14:50:10 +0100608 MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE);
Jerry Yuf35ba382022-08-23 17:58:26 +0800609 MBEDTLS_SSL_DEBUG_RET(
Gilles Peskine449bd832023-01-11 14:50:10 +0100610 2, "ssl_tls13_select_ciphersuite", ret);
611 return ret;
Jerry Yu5725f1c2022-08-21 17:27:16 +0800612 }
613
Jerry Yu96a2e362022-07-21 15:11:34 +0800614 ret = ssl_tls13_offered_psks_check_binder_match(
Gilles Peskine449bd832023-01-11 14:50:10 +0100615 ssl, binder, binder_len, psk_type,
Manuel Pégourié-Gonnard1f2a5872023-03-28 11:46:17 +0200616 mbedtls_md_psa_alg_from_type(ciphersuite_info->mac));
Gilles Peskine449bd832023-01-11 14:50:10 +0100617 if (ret != SSL_TLS1_3_OFFERED_PSK_MATCH) {
Jerry Yuc5a23a02022-08-25 10:51:44 +0800618 /* For security reasons, the handshake should be aborted when we
619 * fail to validate a binder value. See RFC 8446 section 4.2.11.2
620 * and appendix E.6. */
Jerry Yu82534862022-08-30 10:42:33 +0800621#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Gilles Peskine449bd832023-01-11 14:50:10 +0100622 mbedtls_ssl_session_free(&session);
Jerry Yu82534862022-08-30 10:42:33 +0800623#endif
Gilles Peskine449bd832023-01-11 14:50:10 +0100624 MBEDTLS_SSL_DEBUG_MSG(3, ("Invalid binder."));
Xiaokang Qian9f1747b2023-03-30 02:50:04 +0000625 MBEDTLS_SSL_DEBUG_RET(
626 1, "ssl_tls13_offered_psks_check_binder_match", ret);
Jerry Yu96a2e362022-07-21 15:11:34 +0800627 MBEDTLS_SSL_PEND_FATAL_ALERT(
628 MBEDTLS_SSL_ALERT_MSG_DECRYPT_ERROR,
Gilles Peskine449bd832023-01-11 14:50:10 +0100629 MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE);
630 return ret;
Jerry Yu96a2e362022-07-21 15:11:34 +0800631 }
Jerry Yu96a2e362022-07-21 15:11:34 +0800632
633 matched_identity = identity_id;
Jerry Yu5725f1c2022-08-21 17:27:16 +0800634
635 /* Update handshake parameters */
Jerry Yue5834fd2022-08-29 20:16:09 +0800636 ssl->handshake->ciphersuite_info = ciphersuite_info;
Jerry Yu4746b102022-09-13 11:11:48 +0800637 ssl->session_negotiate->ciphersuite = cipher_suite;
Gilles Peskine449bd832023-01-11 14:50:10 +0100638 MBEDTLS_SSL_DEBUG_MSG(2, ("overwrite ciphersuite: %04x - %s",
639 cipher_suite, ciphersuite_info->name));
Jerry Yu82534862022-08-30 10:42:33 +0800640#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Gilles Peskine449bd832023-01-11 14:50:10 +0100641 if (psk_type == MBEDTLS_SSL_TLS1_3_PSK_RESUMPTION) {
642 ret = ssl_tls13_session_copy_ticket(ssl->session_negotiate,
643 &session);
644 mbedtls_ssl_session_free(&session);
645 if (ret != 0) {
646 return ret;
647 }
Jerry Yu82534862022-08-30 10:42:33 +0800648 }
649#endif /* MBEDTLS_SSL_SESSION_TICKETS */
Jerry Yu1c105562022-07-10 06:32:38 +0000650 }
651
Gilles Peskine449bd832023-01-11 14:50:10 +0100652 if (p_identity_len != identities_end || p_binder_len != binders_end) {
653 MBEDTLS_SSL_DEBUG_MSG(3, ("pre_shared_key extension decode error"));
654 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR,
655 MBEDTLS_ERR_SSL_DECODE_ERROR);
656 return MBEDTLS_ERR_SSL_DECODE_ERROR;
Jerry Yu1c105562022-07-10 06:32:38 +0000657 }
658
Jerry Yu6f1db3f2022-07-22 23:05:59 +0800659 /* Update the handshake transcript with the binder list. */
Xiaokang Qian9f1747b2023-03-30 02:50:04 +0000660 ret = ssl->handshake->update_checksum(
661 ssl, identities_end, (size_t) (binders_end - identities_end));
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +0100662 if (0 != ret) {
663 MBEDTLS_SSL_DEBUG_RET(1, ("update_checksum"), ret);
664 return ret;
665 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100666 if (matched_identity == -1) {
667 MBEDTLS_SSL_DEBUG_MSG(3, ("No matched PSK or ticket."));
668 return MBEDTLS_ERR_SSL_UNKNOWN_IDENTITY;
Jerry Yu1c105562022-07-10 06:32:38 +0000669 }
670
Gilles Peskine449bd832023-01-11 14:50:10 +0100671 ssl->handshake->selected_identity = (uint16_t) matched_identity;
672 MBEDTLS_SSL_DEBUG_MSG(3, ("Pre shared key found"));
Jerry Yu1c105562022-07-10 06:32:38 +0000673
Gilles Peskine449bd832023-01-11 14:50:10 +0100674 return 0;
Jerry Yu1c105562022-07-10 06:32:38 +0000675}
Jerry Yu032b15ce2022-07-11 06:10:03 +0000676
677/*
678 * struct {
679 * select ( Handshake.msg_type ) {
680 * ....
681 * case server_hello:
682 * uint16 selected_identity;
683 * }
684 * } PreSharedKeyExtension;
685 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100686static int ssl_tls13_write_server_pre_shared_key_ext(mbedtls_ssl_context *ssl,
687 unsigned char *buf,
688 unsigned char *end,
689 size_t *olen)
Jerry Yu032b15ce2022-07-11 06:10:03 +0000690{
Gilles Peskine449bd832023-01-11 14:50:10 +0100691 unsigned char *p = (unsigned char *) buf;
Jerry Yu032b15ce2022-07-11 06:10:03 +0000692
693 *olen = 0;
694
David Horstmann21b89762022-10-06 18:34:28 +0100695 int not_using_psk = 0;
Jerry Yu032b15ce2022-07-11 06:10:03 +0000696#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +0100697 not_using_psk = (mbedtls_svc_key_id_is_null(ssl->handshake->psk_opaque));
Jerry Yu032b15ce2022-07-11 06:10:03 +0000698#else
Gilles Peskine449bd832023-01-11 14:50:10 +0100699 not_using_psk = (ssl->handshake->psk == NULL);
Jerry Yu032b15ce2022-07-11 06:10:03 +0000700#endif
Gilles Peskine449bd832023-01-11 14:50:10 +0100701 if (not_using_psk) {
Jerry Yu032b15ce2022-07-11 06:10:03 +0000702 /* We shouldn't have called this extension writer unless we've
703 * chosen to use a PSK. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100704 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Jerry Yu032b15ce2022-07-11 06:10:03 +0000705 }
706
Gilles Peskine449bd832023-01-11 14:50:10 +0100707 MBEDTLS_SSL_DEBUG_MSG(3, ("server hello, adding pre_shared_key extension"));
708 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 6);
Jerry Yu032b15ce2022-07-11 06:10:03 +0000709
Gilles Peskine449bd832023-01-11 14:50:10 +0100710 MBEDTLS_PUT_UINT16_BE(MBEDTLS_TLS_EXT_PRE_SHARED_KEY, p, 0);
711 MBEDTLS_PUT_UINT16_BE(2, p, 2);
Jerry Yu032b15ce2022-07-11 06:10:03 +0000712
Gilles Peskine449bd832023-01-11 14:50:10 +0100713 MBEDTLS_PUT_UINT16_BE(ssl->handshake->selected_identity, p, 4);
Jerry Yu032b15ce2022-07-11 06:10:03 +0000714
715 *olen = 6;
716
Gilles Peskine449bd832023-01-11 14:50:10 +0100717 MBEDTLS_SSL_DEBUG_MSG(4, ("sent selected_identity: %u",
718 ssl->handshake->selected_identity));
Jerry Yu032b15ce2022-07-11 06:10:03 +0000719
Gilles Peskine449bd832023-01-11 14:50:10 +0100720 mbedtls_ssl_tls13_set_hs_sent_ext_mask(ssl, MBEDTLS_TLS_EXT_PRE_SHARED_KEY);
Jerry Yub95dd362022-11-08 21:19:34 +0800721
Gilles Peskine449bd832023-01-11 14:50:10 +0100722 return 0;
Jerry Yu032b15ce2022-07-11 06:10:03 +0000723}
724
Ronald Cron41a443a2022-10-04 16:38:25 +0200725#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED */
Jerry Yue19e3b92022-07-08 12:04:51 +0000726
XiaokangQian7807f9f2022-02-15 10:04:37 +0000727/* From RFC 8446:
728 * struct {
XiaokangQiancfd925f2022-04-14 07:10:37 +0000729 * ProtocolVersion versions<2..254>;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000730 * } SupportedVersions;
731 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200732MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100733static int ssl_tls13_parse_supported_versions_ext(mbedtls_ssl_context *ssl,
734 const unsigned char *buf,
735 const unsigned char *end)
XiaokangQian7807f9f2022-02-15 10:04:37 +0000736{
XiaokangQian7807f9f2022-02-15 10:04:37 +0000737 const unsigned char *p = buf;
XiaokangQian8f9dfe42022-04-15 02:52:39 +0000738 size_t versions_len;
XiaokangQian4080a7f2022-04-11 09:55:18 +0000739 const unsigned char *versions_end;
XiaokangQian0a1b54e2022-04-21 03:01:38 +0000740 uint16_t tls_version;
Ronald Cron5af4c7f2023-03-07 20:46:59 +0100741 int found_supported_version = 0;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000742
Gilles Peskine449bd832023-01-11 14:50:10 +0100743 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, 1);
XiaokangQian4080a7f2022-04-11 09:55:18 +0000744 versions_len = p[0];
XiaokangQian7807f9f2022-02-15 10:04:37 +0000745 p += 1;
746
Gilles Peskine449bd832023-01-11 14:50:10 +0100747 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, versions_len);
XiaokangQian4080a7f2022-04-11 09:55:18 +0000748 versions_end = p + versions_len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100749 while (p < versions_end) {
750 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, versions_end, 2);
751 tls_version = mbedtls_ssl_read_version(p, ssl->conf->transport);
XiaokangQianb67384d2022-04-19 00:02:38 +0000752 p += 2;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000753
Ronald Cron3bd2b022023-04-03 16:45:39 +0200754 if (MBEDTLS_SSL_VERSION_TLS1_3 == tls_version) {
Ronald Cron5af4c7f2023-03-07 20:46:59 +0100755 found_supported_version = 1;
756 break;
757 }
758
Ronald Cron3bd2b022023-04-03 16:45:39 +0200759 if ((MBEDTLS_SSL_VERSION_TLS1_2 == tls_version) &&
760 mbedtls_ssl_conf_is_tls12_enabled(ssl->conf)) {
Ronald Cron5af4c7f2023-03-07 20:46:59 +0100761 found_supported_version = 1;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000762 break;
763 }
XiaokangQian7807f9f2022-02-15 10:04:37 +0000764 }
765
Ronald Cron5af4c7f2023-03-07 20:46:59 +0100766 if (!found_supported_version) {
767 MBEDTLS_SSL_DEBUG_MSG(1, ("No supported version found."));
XiaokangQian7807f9f2022-02-15 10:04:37 +0000768
Gilles Peskine449bd832023-01-11 14:50:10 +0100769 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_PROTOCOL_VERSION,
770 MBEDTLS_ERR_SSL_BAD_PROTOCOL_VERSION);
771 return MBEDTLS_ERR_SSL_BAD_PROTOCOL_VERSION;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000772 }
773
Ronald Cron5af4c7f2023-03-07 20:46:59 +0100774 MBEDTLS_SSL_DEBUG_MSG(1, ("Negotiated version: [%04x]",
Gilles Peskine449bd832023-01-11 14:50:10 +0100775 (unsigned int) tls_version));
XiaokangQian7807f9f2022-02-15 10:04:37 +0000776
Ronald Cron5af4c7f2023-03-07 20:46:59 +0100777 return (int) tls_version;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000778}
779
Valerio Setti080a22b2023-03-20 15:22:47 +0100780#if defined(PSA_WANT_ALG_ECDH)
XiaokangQiane8ff3502022-04-22 02:34:40 +0000781/*
XiaokangQian7807f9f2022-02-15 10:04:37 +0000782 *
783 * From RFC 8446:
784 * enum {
785 * ... (0xFFFF)
786 * } NamedGroup;
787 * struct {
788 * NamedGroup named_group_list<2..2^16-1>;
789 * } NamedGroupList;
790 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200791MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100792static int ssl_tls13_parse_supported_groups_ext(mbedtls_ssl_context *ssl,
793 const unsigned char *buf,
794 const unsigned char *end)
XiaokangQian7807f9f2022-02-15 10:04:37 +0000795{
XiaokangQian7807f9f2022-02-15 10:04:37 +0000796 const unsigned char *p = buf;
XiaokangQian84823772022-04-19 07:57:30 +0000797 size_t named_group_list_len;
XiaokangQian8f9dfe42022-04-15 02:52:39 +0000798 const unsigned char *named_group_list_end;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000799
Gilles Peskine449bd832023-01-11 14:50:10 +0100800 MBEDTLS_SSL_DEBUG_BUF(3, "supported_groups extension", p, end - buf);
801 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, 2);
802 named_group_list_len = MBEDTLS_GET_UINT16_BE(p, 0);
XiaokangQian7807f9f2022-02-15 10:04:37 +0000803 p += 2;
Gilles Peskine449bd832023-01-11 14:50:10 +0100804 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, named_group_list_len);
XiaokangQian8f9dfe42022-04-15 02:52:39 +0000805 named_group_list_end = p + named_group_list_len;
XiaokangQian4e8cd7b2022-04-21 09:48:09 +0000806 ssl->handshake->hrr_selected_group = 0;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000807
Gilles Peskine449bd832023-01-11 14:50:10 +0100808 while (p < named_group_list_end) {
XiaokangQian08037552022-04-20 07:16:41 +0000809 uint16_t named_group;
Gilles Peskine449bd832023-01-11 14:50:10 +0100810 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, named_group_list_end, 2);
811 named_group = MBEDTLS_GET_UINT16_BE(p, 0);
XiaokangQian08037552022-04-20 07:16:41 +0000812 p += 2;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000813
Gilles Peskine449bd832023-01-11 14:50:10 +0100814 MBEDTLS_SSL_DEBUG_MSG(2,
815 ("got named group: %s(%04x)",
816 mbedtls_ssl_named_group_to_str(named_group),
817 named_group));
XiaokangQian08037552022-04-20 07:16:41 +0000818
Gilles Peskine449bd832023-01-11 14:50:10 +0100819 if (!mbedtls_ssl_named_group_is_offered(ssl, named_group) ||
820 !mbedtls_ssl_named_group_is_supported(named_group) ||
821 ssl->handshake->hrr_selected_group != 0) {
XiaokangQian08037552022-04-20 07:16:41 +0000822 continue;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000823 }
824
Gilles Peskine449bd832023-01-11 14:50:10 +0100825 MBEDTLS_SSL_DEBUG_MSG(2,
826 ("add named group %s(%04x) into received list.",
827 mbedtls_ssl_named_group_to_str(named_group),
828 named_group));
Jerry Yuc1be19f2022-04-23 16:11:39 +0800829
XiaokangQian4e8cd7b2022-04-21 09:48:09 +0000830 ssl->handshake->hrr_selected_group = named_group;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000831 }
832
Gilles Peskine449bd832023-01-11 14:50:10 +0100833 return 0;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000834
835}
Valerio Setti080a22b2023-03-20 15:22:47 +0100836#endif /* PSA_WANT_ALG_ECDH */
XiaokangQian7807f9f2022-02-15 10:04:37 +0000837
XiaokangQian08037552022-04-20 07:16:41 +0000838#define SSL_TLS1_3_PARSE_KEY_SHARES_EXT_NO_MATCH 1
839
Valerio Setti080a22b2023-03-20 15:22:47 +0100840#if defined(PSA_WANT_ALG_ECDH)
XiaokangQian7807f9f2022-02-15 10:04:37 +0000841/*
842 * ssl_tls13_parse_key_shares_ext() verifies whether the information in the
Xiaokang Qian9f1747b2023-03-30 02:50:04 +0000843 * extension is correct and stores the first acceptable key share and its
844 * associated group.
XiaokangQian7807f9f2022-02-15 10:04:37 +0000845 *
846 * Possible return values are:
847 * - 0: Successful processing of the client provided key share extension.
Xiaokang Qian9f1747b2023-03-30 02:50:04 +0000848 * - SSL_TLS1_3_PARSE_KEY_SHARES_EXT_NO_MATCH: The key shares provided by
849 * the client does not match a group supported by the server. A
850 * HelloRetryRequest will be needed.
XiaokangQiane8ff3502022-04-22 02:34:40 +0000851 * - A negative value for fatal errors.
Jerry Yuc1be19f2022-04-23 16:11:39 +0800852 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200853MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100854static int ssl_tls13_parse_key_shares_ext(mbedtls_ssl_context *ssl,
855 const unsigned char *buf,
856 const unsigned char *end)
XiaokangQian7807f9f2022-02-15 10:04:37 +0000857{
XiaokangQianb67384d2022-04-19 00:02:38 +0000858 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000859 unsigned char const *p = buf;
XiaokangQian8f9dfe42022-04-15 02:52:39 +0000860 unsigned char const *client_shares_end;
Jerry Yuc1be19f2022-04-23 16:11:39 +0800861 size_t client_shares_len;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000862
863 /* From RFC 8446:
864 *
865 * struct {
866 * KeyShareEntry client_shares<0..2^16-1>;
867 * } KeyShareClientHello;
868 *
869 */
870
Gilles Peskine449bd832023-01-11 14:50:10 +0100871 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, 2);
872 client_shares_len = MBEDTLS_GET_UINT16_BE(p, 0);
XiaokangQian7807f9f2022-02-15 10:04:37 +0000873 p += 2;
Gilles Peskine449bd832023-01-11 14:50:10 +0100874 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, client_shares_len);
XiaokangQian7807f9f2022-02-15 10:04:37 +0000875
876 ssl->handshake->offered_group_id = 0;
XiaokangQian8f9dfe42022-04-15 02:52:39 +0000877 client_shares_end = p + client_shares_len;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000878
879 /* We try to find a suitable key share entry and copy it to the
880 * handshake context. Later, we have to find out whether we can do
881 * something with the provided key share or whether we have to
XiaokangQianc5763b52022-04-02 03:34:37 +0000882 * dismiss it and send a HelloRetryRequest message.
883 */
XiaokangQian7807f9f2022-02-15 10:04:37 +0000884
Gilles Peskine449bd832023-01-11 14:50:10 +0100885 while (p < client_shares_end) {
XiaokangQian9b5d04b2022-04-10 10:20:43 +0000886 uint16_t group;
Jerry Yuc1be19f2022-04-23 16:11:39 +0800887 size_t key_exchange_len;
Jerry Yu086edc22022-05-05 10:50:38 +0800888 const unsigned char *key_exchange;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000889
890 /*
891 * struct {
892 * NamedGroup group;
893 * opaque key_exchange<1..2^16-1>;
894 * } KeyShareEntry;
895 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100896 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, client_shares_end, 4);
897 group = MBEDTLS_GET_UINT16_BE(p, 0);
898 key_exchange_len = MBEDTLS_GET_UINT16_BE(p, 2);
Jerry Yuc1be19f2022-04-23 16:11:39 +0800899 p += 4;
Jerry Yu086edc22022-05-05 10:50:38 +0800900 key_exchange = p;
Gilles Peskine449bd832023-01-11 14:50:10 +0100901 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, client_shares_end, key_exchange_len);
Jerry Yu086edc22022-05-05 10:50:38 +0800902 p += key_exchange_len;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000903
904 /* Continue parsing even if we have already found a match,
XiaokangQianc5763b52022-04-02 03:34:37 +0000905 * for input validation purposes.
906 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100907 if (!mbedtls_ssl_named_group_is_offered(ssl, group) ||
908 !mbedtls_ssl_named_group_is_supported(group) ||
909 ssl->handshake->offered_group_id != 0) {
XiaokangQian060d8672022-04-21 09:24:56 +0000910 continue;
911 }
XiaokangQian060d8672022-04-21 09:24:56 +0000912
XiaokangQian7807f9f2022-02-15 10:04:37 +0000913 /*
XiaokangQian8f9dfe42022-04-15 02:52:39 +0000914 * For now, we only support ECDHE groups.
XiaokangQian7807f9f2022-02-15 10:04:37 +0000915 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100916 if (mbedtls_ssl_tls13_named_group_is_ecdhe(group)) {
917 MBEDTLS_SSL_DEBUG_MSG(2, ("ECDH group: %s (%04x)",
918 mbedtls_ssl_named_group_to_str(group),
919 group));
XiaokangQian318dc762022-04-20 09:43:51 +0000920 ret = mbedtls_ssl_tls13_read_public_ecdhe_share(
Gilles Peskine449bd832023-01-11 14:50:10 +0100921 ssl, key_exchange - 2, key_exchange_len + 2);
922 if (ret != 0) {
923 return ret;
924 }
XiaokangQian4e8cd7b2022-04-21 09:48:09 +0000925
Gilles Peskine449bd832023-01-11 14:50:10 +0100926 } else {
927 MBEDTLS_SSL_DEBUG_MSG(4, ("Unrecognized NamedGroup %u",
928 (unsigned) group));
XiaokangQian7807f9f2022-02-15 10:04:37 +0000929 continue;
930 }
931
XiaokangQian9b5d04b2022-04-10 10:20:43 +0000932 ssl->handshake->offered_group_id = group;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000933 }
934
Jerry Yuc1be19f2022-04-23 16:11:39 +0800935
Gilles Peskine449bd832023-01-11 14:50:10 +0100936 if (ssl->handshake->offered_group_id == 0) {
937 MBEDTLS_SSL_DEBUG_MSG(1, ("no matching key share"));
938 return SSL_TLS1_3_PARSE_KEY_SHARES_EXT_NO_MATCH;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000939 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100940 return 0;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000941}
Valerio Setti080a22b2023-03-20 15:22:47 +0100942#endif /* PSA_WANT_ALG_ECDH */
XiaokangQian7807f9f2022-02-15 10:04:37 +0000943
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200944MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100945static int ssl_tls13_client_hello_has_exts(mbedtls_ssl_context *ssl,
946 int exts_mask)
XiaokangQian7807f9f2022-02-15 10:04:37 +0000947{
Jerry Yu0c354a22022-08-29 15:25:36 +0800948 int masked = ssl->handshake->received_extensions & exts_mask;
Gilles Peskine449bd832023-01-11 14:50:10 +0100949 return masked == exts_mask;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000950}
951
Jerry Yue5991322022-11-07 14:03:44 +0800952#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200953MBEDTLS_CHECK_RETURN_CRITICAL
XiaokangQianb67384d2022-04-19 00:02:38 +0000954static int ssl_tls13_client_hello_has_exts_for_ephemeral_key_exchange(
Gilles Peskine449bd832023-01-11 14:50:10 +0100955 mbedtls_ssl_context *ssl)
XiaokangQian7807f9f2022-02-15 10:04:37 +0000956{
Gilles Peskine449bd832023-01-11 14:50:10 +0100957 return ssl_tls13_client_hello_has_exts(
958 ssl,
959 MBEDTLS_SSL_EXT_MASK(SUPPORTED_GROUPS) |
960 MBEDTLS_SSL_EXT_MASK(KEY_SHARE) |
961 MBEDTLS_SSL_EXT_MASK(SIG_ALG));
XiaokangQian7807f9f2022-02-15 10:04:37 +0000962}
Jerry Yue5991322022-11-07 14:03:44 +0800963#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED */
XiaokangQian7807f9f2022-02-15 10:04:37 +0000964
Jerry Yue5991322022-11-07 14:03:44 +0800965#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK_ENABLED)
Jerry Yu77f01482022-07-11 07:03:24 +0000966MBEDTLS_CHECK_RETURN_CRITICAL
967static int ssl_tls13_client_hello_has_exts_for_psk_key_exchange(
Gilles Peskine449bd832023-01-11 14:50:10 +0100968 mbedtls_ssl_context *ssl)
Jerry Yu77f01482022-07-11 07:03:24 +0000969{
Gilles Peskine449bd832023-01-11 14:50:10 +0100970 return ssl_tls13_client_hello_has_exts(
971 ssl,
972 MBEDTLS_SSL_EXT_MASK(PRE_SHARED_KEY) |
973 MBEDTLS_SSL_EXT_MASK(PSK_KEY_EXCHANGE_MODES));
Jerry Yu77f01482022-07-11 07:03:24 +0000974}
Jerry Yue5991322022-11-07 14:03:44 +0800975#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK_ENABLED */
Jerry Yu77f01482022-07-11 07:03:24 +0000976
Jerry Yue5991322022-11-07 14:03:44 +0800977#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK_EPHEMERAL_ENABLED)
Jerry Yu77f01482022-07-11 07:03:24 +0000978MBEDTLS_CHECK_RETURN_CRITICAL
979static int ssl_tls13_client_hello_has_exts_for_psk_ephemeral_key_exchange(
Gilles Peskine449bd832023-01-11 14:50:10 +0100980 mbedtls_ssl_context *ssl)
Jerry Yu77f01482022-07-11 07:03:24 +0000981{
Gilles Peskine449bd832023-01-11 14:50:10 +0100982 return ssl_tls13_client_hello_has_exts(
983 ssl,
984 MBEDTLS_SSL_EXT_MASK(SUPPORTED_GROUPS) |
985 MBEDTLS_SSL_EXT_MASK(KEY_SHARE) |
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_EPHEMERAL_ENABLED */
Jerry Yu77f01482022-07-11 07:03:24 +0000990
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200991MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100992static int ssl_tls13_check_ephemeral_key_exchange(mbedtls_ssl_context *ssl)
XiaokangQian7807f9f2022-02-15 10:04:37 +0000993{
Jerry Yue5991322022-11-07 14:03:44 +0800994#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +0100995 return mbedtls_ssl_conf_tls13_ephemeral_enabled(ssl) &&
996 ssl_tls13_client_hello_has_exts_for_ephemeral_key_exchange(ssl);
Jerry Yue5991322022-11-07 14:03:44 +0800997#else
998 ((void) ssl);
Gilles Peskine449bd832023-01-11 14:50:10 +0100999 return 0;
Jerry Yue5991322022-11-07 14:03:44 +08001000#endif
Jerry Yu77f01482022-07-11 07:03:24 +00001001}
XiaokangQian7807f9f2022-02-15 10:04:37 +00001002
Jerry Yu77f01482022-07-11 07:03:24 +00001003MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01001004static int ssl_tls13_check_psk_key_exchange(mbedtls_ssl_context *ssl)
Jerry Yu77f01482022-07-11 07:03:24 +00001005{
Jerry Yue5991322022-11-07 14:03:44 +08001006#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01001007 return mbedtls_ssl_conf_tls13_psk_enabled(ssl) &&
1008 mbedtls_ssl_tls13_psk_enabled(ssl) &&
1009 ssl_tls13_client_hello_has_exts_for_psk_key_exchange(ssl);
Jerry Yu77f01482022-07-11 07:03:24 +00001010#else
1011 ((void) ssl);
Gilles Peskine449bd832023-01-11 14:50:10 +01001012 return 0;
Jerry Yu77f01482022-07-11 07:03:24 +00001013#endif
1014}
XiaokangQian7807f9f2022-02-15 10:04:37 +00001015
Jerry Yu77f01482022-07-11 07:03:24 +00001016MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01001017static int ssl_tls13_check_psk_ephemeral_key_exchange(mbedtls_ssl_context *ssl)
Jerry Yu77f01482022-07-11 07:03:24 +00001018{
Jerry Yue5991322022-11-07 14:03:44 +08001019#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK_EPHEMERAL_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01001020 return mbedtls_ssl_conf_tls13_psk_ephemeral_enabled(ssl) &&
1021 mbedtls_ssl_tls13_psk_ephemeral_enabled(ssl) &&
1022 ssl_tls13_client_hello_has_exts_for_psk_ephemeral_key_exchange(ssl);
Jerry Yu77f01482022-07-11 07:03:24 +00001023#else
1024 ((void) ssl);
Gilles Peskine449bd832023-01-11 14:50:10 +01001025 return 0;
Jerry Yu77f01482022-07-11 07:03:24 +00001026#endif
1027}
1028
Gilles Peskine449bd832023-01-11 14:50:10 +01001029static int ssl_tls13_determine_key_exchange_mode(mbedtls_ssl_context *ssl)
Jerry Yu77f01482022-07-11 07:03:24 +00001030{
1031 /*
1032 * Determine the key exchange algorithm to use.
1033 * There are three types of key exchanges supported in TLS 1.3:
1034 * - (EC)DH with ECDSA,
1035 * - (EC)DH with PSK,
1036 * - plain PSK.
1037 *
1038 * The PSK-based key exchanges may additionally be used with 0-RTT.
1039 *
1040 * Our built-in order of preference is
Jerry Yuce6ed702022-07-22 21:49:53 +08001041 * 1 ) (EC)DHE-PSK Mode ( psk_ephemeral )
1042 * 2 ) Certificate Mode ( ephemeral )
1043 * 3 ) Plain PSK Mode ( psk )
Jerry Yu77f01482022-07-11 07:03:24 +00001044 */
1045
Xiaokang Qian9f1747b2023-03-30 02:50:04 +00001046 ssl->handshake->key_exchange_mode =
1047 MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_NONE;
Jerry Yu77f01482022-07-11 07:03:24 +00001048
Gilles Peskine449bd832023-01-11 14:50:10 +01001049 if (ssl_tls13_check_psk_ephemeral_key_exchange(ssl)) {
Jerry Yu77f01482022-07-11 07:03:24 +00001050 ssl->handshake->key_exchange_mode =
1051 MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK_EPHEMERAL;
Gilles Peskine449bd832023-01-11 14:50:10 +01001052 MBEDTLS_SSL_DEBUG_MSG(2, ("key exchange mode: psk_ephemeral"));
1053 } else
1054 if (ssl_tls13_check_ephemeral_key_exchange(ssl)) {
Jerry Yu77f01482022-07-11 07:03:24 +00001055 ssl->handshake->key_exchange_mode =
1056 MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL;
Gilles Peskine449bd832023-01-11 14:50:10 +01001057 MBEDTLS_SSL_DEBUG_MSG(2, ("key exchange mode: ephemeral"));
1058 } else
1059 if (ssl_tls13_check_psk_key_exchange(ssl)) {
Jerry Yuce6ed702022-07-22 21:49:53 +08001060 ssl->handshake->key_exchange_mode =
1061 MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK;
Gilles Peskine449bd832023-01-11 14:50:10 +01001062 MBEDTLS_SSL_DEBUG_MSG(2, ("key exchange mode: psk"));
1063 } else {
Jerry Yu77f01482022-07-11 07:03:24 +00001064 MBEDTLS_SSL_DEBUG_MSG(
Gilles Peskine449bd832023-01-11 14:50:10 +01001065 1,
1066 ("ClientHello message misses mandatory extensions."));
1067 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_MISSING_EXTENSION,
1068 MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER);
1069 return MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER;
Jerry Yu77f01482022-07-11 07:03:24 +00001070 }
1071
Gilles Peskine449bd832023-01-11 14:50:10 +01001072 return 0;
Jerry Yu77f01482022-07-11 07:03:24 +00001073
XiaokangQian7807f9f2022-02-15 10:04:37 +00001074}
1075
XiaokangQian81802f42022-06-10 13:25:22 +00001076#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
Ronald Cron928cbd32022-10-04 16:14:26 +02001077 defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED)
Ronald Cron67ea2542022-09-15 17:34:42 +02001078
1079#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01001080static psa_algorithm_t ssl_tls13_iana_sig_alg_to_psa_alg(uint16_t sig_alg)
Ronald Cron67ea2542022-09-15 17:34:42 +02001081{
Gilles Peskine449bd832023-01-11 14:50:10 +01001082 switch (sig_alg) {
Ronald Cron67ea2542022-09-15 17:34:42 +02001083 case MBEDTLS_TLS1_3_SIG_ECDSA_SECP256R1_SHA256:
Gilles Peskine449bd832023-01-11 14:50:10 +01001084 return PSA_ALG_ECDSA(PSA_ALG_SHA_256);
Ronald Cron67ea2542022-09-15 17:34:42 +02001085 case MBEDTLS_TLS1_3_SIG_ECDSA_SECP384R1_SHA384:
Gilles Peskine449bd832023-01-11 14:50:10 +01001086 return PSA_ALG_ECDSA(PSA_ALG_SHA_384);
Ronald Cron67ea2542022-09-15 17:34:42 +02001087 case MBEDTLS_TLS1_3_SIG_ECDSA_SECP521R1_SHA512:
Gilles Peskine449bd832023-01-11 14:50:10 +01001088 return PSA_ALG_ECDSA(PSA_ALG_SHA_512);
Ronald Cron67ea2542022-09-15 17:34:42 +02001089 case MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA256:
Gilles Peskine449bd832023-01-11 14:50:10 +01001090 return PSA_ALG_RSA_PSS(PSA_ALG_SHA_256);
Ronald Cron67ea2542022-09-15 17:34:42 +02001091 case MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA384:
Gilles Peskine449bd832023-01-11 14:50:10 +01001092 return PSA_ALG_RSA_PSS(PSA_ALG_SHA_384);
Ronald Cron67ea2542022-09-15 17:34:42 +02001093 case MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA512:
Gilles Peskine449bd832023-01-11 14:50:10 +01001094 return PSA_ALG_RSA_PSS(PSA_ALG_SHA_512);
Ronald Cron67ea2542022-09-15 17:34:42 +02001095 case MBEDTLS_TLS1_3_SIG_RSA_PKCS1_SHA256:
Gilles Peskine449bd832023-01-11 14:50:10 +01001096 return PSA_ALG_RSA_PKCS1V15_SIGN(PSA_ALG_SHA_256);
Ronald Cron67ea2542022-09-15 17:34:42 +02001097 case MBEDTLS_TLS1_3_SIG_RSA_PKCS1_SHA384:
Gilles Peskine449bd832023-01-11 14:50:10 +01001098 return PSA_ALG_RSA_PKCS1V15_SIGN(PSA_ALG_SHA_384);
Ronald Cron67ea2542022-09-15 17:34:42 +02001099 case MBEDTLS_TLS1_3_SIG_RSA_PKCS1_SHA512:
Gilles Peskine449bd832023-01-11 14:50:10 +01001100 return PSA_ALG_RSA_PKCS1V15_SIGN(PSA_ALG_SHA_512);
Ronald Cron67ea2542022-09-15 17:34:42 +02001101 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01001102 return PSA_ALG_NONE;
Ronald Cron67ea2542022-09-15 17:34:42 +02001103 }
1104}
1105#endif /* MBEDTLS_USE_PSA_CRYPTO */
1106
XiaokangQian23c5be62022-06-07 02:04:34 +00001107/*
XiaokangQianfb665a82022-06-15 03:57:21 +00001108 * Pick best ( private key, certificate chain ) pair based on the signature
1109 * algorithms supported by the client.
XiaokangQian23c5be62022-06-07 02:04:34 +00001110 */
Ronald Cronce7d76e2022-07-08 18:56:49 +02001111MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01001112static int ssl_tls13_pick_key_cert(mbedtls_ssl_context *ssl)
XiaokangQian23c5be62022-06-07 02:04:34 +00001113{
XiaokangQianfb665a82022-06-15 03:57:21 +00001114 mbedtls_ssl_key_cert *key_cert, *key_cert_list;
XiaokangQian81802f42022-06-10 13:25:22 +00001115 const uint16_t *sig_alg = ssl->handshake->received_sig_algs;
XiaokangQian23c5be62022-06-07 02:04:34 +00001116
1117#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Gilles Peskine449bd832023-01-11 14:50:10 +01001118 if (ssl->handshake->sni_key_cert != NULL) {
XiaokangQianfb665a82022-06-15 03:57:21 +00001119 key_cert_list = ssl->handshake->sni_key_cert;
Gilles Peskine449bd832023-01-11 14:50:10 +01001120 } else
XiaokangQian23c5be62022-06-07 02:04:34 +00001121#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
Gilles Peskine449bd832023-01-11 14:50:10 +01001122 key_cert_list = ssl->conf->key_cert;
XiaokangQian23c5be62022-06-07 02:04:34 +00001123
Gilles Peskine449bd832023-01-11 14:50:10 +01001124 if (key_cert_list == NULL) {
1125 MBEDTLS_SSL_DEBUG_MSG(3, ("server has no certificate"));
1126 return -1;
XiaokangQian23c5be62022-06-07 02:04:34 +00001127 }
1128
Gilles Peskine449bd832023-01-11 14:50:10 +01001129 for (; *sig_alg != MBEDTLS_TLS1_3_SIG_NONE; sig_alg++) {
1130 if (!mbedtls_ssl_sig_alg_is_offered(ssl, *sig_alg)) {
Ronald Cron67ea2542022-09-15 17:34:42 +02001131 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +01001132 }
Ronald Cron67ea2542022-09-15 17:34:42 +02001133
Gilles Peskine449bd832023-01-11 14:50:10 +01001134 if (!mbedtls_ssl_tls13_sig_alg_for_cert_verify_is_supported(*sig_alg)) {
Ronald Cron67ea2542022-09-15 17:34:42 +02001135 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +01001136 }
Ronald Cron67ea2542022-09-15 17:34:42 +02001137
Gilles Peskine449bd832023-01-11 14:50:10 +01001138 for (key_cert = key_cert_list; key_cert != NULL;
1139 key_cert = key_cert->next) {
Ronald Cron67ea2542022-09-15 17:34:42 +02001140#if defined(MBEDTLS_USE_PSA_CRYPTO)
1141 psa_algorithm_t psa_alg = PSA_ALG_NONE;
1142#endif /* MBEDTLS_USE_PSA_CRYPTO */
1143
Gilles Peskine449bd832023-01-11 14:50:10 +01001144 MBEDTLS_SSL_DEBUG_CRT(3, "certificate (chain) candidate",
1145 key_cert->cert);
XiaokangQian81802f42022-06-10 13:25:22 +00001146
1147 /*
Gilles Peskine449bd832023-01-11 14:50:10 +01001148 * This avoids sending the client a cert it'll reject based on
1149 * keyUsage or other extensions.
1150 */
1151 if (mbedtls_x509_crt_check_key_usage(
1152 key_cert->cert, MBEDTLS_X509_KU_DIGITAL_SIGNATURE) != 0 ||
XiaokangQian81802f42022-06-10 13:25:22 +00001153 mbedtls_x509_crt_check_extended_key_usage(
XiaokangQianfb665a82022-06-15 03:57:21 +00001154 key_cert->cert, MBEDTLS_OID_SERVER_AUTH,
Gilles Peskine449bd832023-01-11 14:50:10 +01001155 MBEDTLS_OID_SIZE(MBEDTLS_OID_SERVER_AUTH)) != 0) {
1156 MBEDTLS_SSL_DEBUG_MSG(3, ("certificate mismatch: "
1157 "(extended) key usage extension"));
XiaokangQian81802f42022-06-10 13:25:22 +00001158 continue;
1159 }
XiaokangQian23c5be62022-06-07 02:04:34 +00001160
Gilles Peskine449bd832023-01-11 14:50:10 +01001161 MBEDTLS_SSL_DEBUG_MSG(3,
1162 ("ssl_tls13_pick_key_cert:"
1163 "check signature algorithm %s [%04x]",
1164 mbedtls_ssl_sig_alg_to_str(*sig_alg),
1165 *sig_alg));
Ronald Cron67ea2542022-09-15 17:34:42 +02001166#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01001167 psa_alg = ssl_tls13_iana_sig_alg_to_psa_alg(*sig_alg);
Ronald Cron67ea2542022-09-15 17:34:42 +02001168#endif /* MBEDTLS_USE_PSA_CRYPTO */
1169
Gilles Peskine449bd832023-01-11 14:50:10 +01001170 if (mbedtls_ssl_tls13_check_sig_alg_cert_key_match(
1171 *sig_alg, &key_cert->cert->pk)
Ronald Cron67ea2542022-09-15 17:34:42 +02001172#if defined(MBEDTLS_USE_PSA_CRYPTO)
1173 && psa_alg != PSA_ALG_NONE &&
Gilles Peskine449bd832023-01-11 14:50:10 +01001174 mbedtls_pk_can_do_ext(&key_cert->cert->pk, psa_alg,
1175 PSA_KEY_USAGE_SIGN_HASH) == 1
Ronald Cron67ea2542022-09-15 17:34:42 +02001176#endif /* MBEDTLS_USE_PSA_CRYPTO */
Gilles Peskine449bd832023-01-11 14:50:10 +01001177 ) {
XiaokangQianfb665a82022-06-15 03:57:21 +00001178 ssl->handshake->key_cert = key_cert;
Gilles Peskine449bd832023-01-11 14:50:10 +01001179 MBEDTLS_SSL_DEBUG_MSG(3,
1180 ("ssl_tls13_pick_key_cert:"
1181 "selected signature algorithm"
1182 " %s [%04x]",
1183 mbedtls_ssl_sig_alg_to_str(*sig_alg),
1184 *sig_alg));
XiaokangQianfb665a82022-06-15 03:57:21 +00001185 MBEDTLS_SSL_DEBUG_CRT(
Gilles Peskine449bd832023-01-11 14:50:10 +01001186 3, "selected certificate (chain)",
1187 ssl->handshake->key_cert->cert);
1188 return 0;
XiaokangQian81802f42022-06-10 13:25:22 +00001189 }
XiaokangQian81802f42022-06-10 13:25:22 +00001190 }
XiaokangQian23c5be62022-06-07 02:04:34 +00001191 }
1192
Gilles Peskine449bd832023-01-11 14:50:10 +01001193 MBEDTLS_SSL_DEBUG_MSG(2, ("ssl_tls13_pick_key_cert:"
1194 "no suitable certificate found"));
1195 return -1;
XiaokangQian23c5be62022-06-07 02:04:34 +00001196}
XiaokangQian81802f42022-06-10 13:25:22 +00001197#endif /* MBEDTLS_X509_CRT_PARSE_C &&
Ronald Cron928cbd32022-10-04 16:14:26 +02001198 MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED */
XiaokangQian23c5be62022-06-07 02:04:34 +00001199
XiaokangQian4080a7f2022-04-11 09:55:18 +00001200/*
XiaokangQianed582dd2022-04-13 08:21:05 +00001201 *
1202 * STATE HANDLING: ClientHello
1203 *
XiaokangQianb67384d2022-04-19 00:02:38 +00001204 * There are three possible classes of outcomes when parsing the ClientHello:
XiaokangQianed582dd2022-04-13 08:21:05 +00001205 *
XiaokangQianb67384d2022-04-19 00:02:38 +00001206 * 1) The ClientHello was well-formed and matched the server's configuration.
XiaokangQianed582dd2022-04-13 08:21:05 +00001207 *
1208 * In this case, the server progresses to sending its ServerHello.
1209 *
XiaokangQianb67384d2022-04-19 00:02:38 +00001210 * 2) The ClientHello was well-formed but didn't match the server's
1211 * configuration.
XiaokangQianed582dd2022-04-13 08:21:05 +00001212 *
1213 * For example, the client might not have offered a key share which
1214 * the server supports, or the server might require a cookie.
1215 *
1216 * In this case, the server sends a HelloRetryRequest.
1217 *
XiaokangQianb67384d2022-04-19 00:02:38 +00001218 * 3) The ClientHello was ill-formed
XiaokangQianed582dd2022-04-13 08:21:05 +00001219 *
1220 * In this case, we abort the handshake.
1221 *
1222 */
1223
1224/*
XiaokangQian4080a7f2022-04-11 09:55:18 +00001225 * Structure of this message:
1226 *
XiaokangQiane8ff3502022-04-22 02:34:40 +00001227 * uint16 ProtocolVersion;
1228 * opaque Random[32];
1229 * uint8 CipherSuite[2]; // Cryptographic suite selector
XiaokangQian4080a7f2022-04-11 09:55:18 +00001230 *
XiaokangQiane8ff3502022-04-22 02:34:40 +00001231 * struct {
1232 * ProtocolVersion legacy_version = 0x0303; // TLS v1.2
1233 * Random random;
1234 * opaque legacy_session_id<0..32>;
1235 * CipherSuite cipher_suites<2..2^16-2>;
1236 * opaque legacy_compression_methods<1..2^8-1>;
1237 * Extension extensions<8..2^16-1>;
1238 * } ClientHello;
XiaokangQian4080a7f2022-04-11 09:55:18 +00001239 */
XiaokangQianed582dd2022-04-13 08:21:05 +00001240
1241#define SSL_CLIENT_HELLO_OK 0
1242#define SSL_CLIENT_HELLO_HRR_REQUIRED 1
Ronald Cron5af4c7f2023-03-07 20:46:59 +01001243#define SSL_CLIENT_HELLO_TLS1_2 2
XiaokangQianed582dd2022-04-13 08:21:05 +00001244
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001245MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01001246static int ssl_tls13_parse_client_hello(mbedtls_ssl_context *ssl,
1247 const unsigned char *buf,
1248 const unsigned char *end)
XiaokangQian7807f9f2022-02-15 10:04:37 +00001249{
XiaokangQianb67384d2022-04-19 00:02:38 +00001250 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1251 const unsigned char *p = buf;
Ronald Crond540d992023-03-07 09:41:48 +01001252 const unsigned char *random;
XiaokangQian4080a7f2022-04-11 09:55:18 +00001253 size_t legacy_session_id_len;
Ronald Croncada4102023-03-07 09:51:39 +01001254 const unsigned char *legacy_session_id;
XiaokangQian318dc762022-04-20 09:43:51 +00001255 size_t cipher_suites_len;
Ronald Cron2f16b4e2023-03-07 10:07:32 +01001256 const unsigned char *cipher_suites;
XiaokangQian060d8672022-04-21 09:24:56 +00001257 const unsigned char *cipher_suites_end;
XiaokangQianb67384d2022-04-19 00:02:38 +00001258 size_t extensions_len;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001259 const unsigned char *extensions_end;
Ronald Croneff56732023-04-03 17:36:31 +02001260 const unsigned char *supported_versions_data;
1261 const unsigned char *supported_versions_data_end;
Jerry Yuc4bf5d62022-10-29 09:08:47 +08001262 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
Jerry Yu49ca9282022-05-05 11:05:22 +08001263 int hrr_required = 0;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001264
Ronald Cron41a443a2022-10-04 16:38:25 +02001265#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED)
Jerry Yu29d9faa2022-08-23 17:52:45 +08001266 const unsigned char *pre_shared_key_ext = NULL;
Jerry Yu1c105562022-07-10 06:32:38 +00001267 const unsigned char *pre_shared_key_ext_end = NULL;
Ronald Cron41a443a2022-10-04 16:38:25 +02001268#endif
XiaokangQian7807f9f2022-02-15 10:04:37 +00001269
XiaokangQian7807f9f2022-02-15 10:04:37 +00001270 /*
XiaokangQianb67384d2022-04-19 00:02:38 +00001271 * ClientHello layout:
XiaokangQian7807f9f2022-02-15 10:04:37 +00001272 * 0 . 1 protocol version
XiaokangQiane8ff3502022-04-22 02:34:40 +00001273 * 2 . 33 random bytes
XiaokangQianc5763b52022-04-02 03:34:37 +00001274 * 34 . 34 session id length ( 1 byte )
XiaokangQian7807f9f2022-02-15 10:04:37 +00001275 * 35 . 34+x session id
XiaokangQian7807f9f2022-02-15 10:04:37 +00001276 * .. . .. ciphersuite list length ( 2 bytes )
1277 * .. . .. ciphersuite list
1278 * .. . .. compression alg. list length ( 1 byte )
1279 * .. . .. compression alg. list
1280 * .. . .. extensions length ( 2 bytes, optional )
1281 * .. . .. extensions ( optional )
1282 */
1283
XiaokangQianb67384d2022-04-19 00:02:38 +00001284 /*
bootstrap-prime6dbbf442022-05-17 19:30:44 -04001285 * Minimal length ( with everything empty and extensions omitted ) is
XiaokangQian7807f9f2022-02-15 10:04:37 +00001286 * 2 + 32 + 1 + 2 + 1 = 38 bytes. Check that first, so that we can
1287 * read at least up to session id length without worrying.
1288 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001289 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, 38);
XiaokangQian7807f9f2022-02-15 10:04:37 +00001290
1291 /* ...
1292 * ProtocolVersion legacy_version = 0x0303; // TLS 1.2
1293 * ...
1294 * with ProtocolVersion defined as:
1295 * uint16 ProtocolVersion;
1296 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001297 if (mbedtls_ssl_read_version(p, ssl->conf->transport) !=
1298 MBEDTLS_SSL_VERSION_TLS1_2) {
1299 MBEDTLS_SSL_DEBUG_MSG(1, ("Unsupported version of TLS."));
1300 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_PROTOCOL_VERSION,
1301 MBEDTLS_ERR_SSL_BAD_PROTOCOL_VERSION);
1302 return MBEDTLS_ERR_SSL_BAD_PROTOCOL_VERSION;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001303 }
1304 p += 2;
1305
Jerry Yuc1be19f2022-04-23 16:11:39 +08001306 /* ...
1307 * Random random;
1308 * ...
XiaokangQianb67384d2022-04-19 00:02:38 +00001309 * with Random defined as:
1310 * opaque Random[32];
XiaokangQian7807f9f2022-02-15 10:04:37 +00001311 */
Ronald Crond540d992023-03-07 09:41:48 +01001312 random = p;
XiaokangQian08037552022-04-20 07:16:41 +00001313 p += MBEDTLS_CLIENT_HELLO_RANDOM_LEN;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001314
Jerry Yuc1be19f2022-04-23 16:11:39 +08001315 /* ...
XiaokangQianb67384d2022-04-19 00:02:38 +00001316 * opaque legacy_session_id<0..32>;
Jerry Yuc1be19f2022-04-23 16:11:39 +08001317 * ...
XiaokangQian7807f9f2022-02-15 10:04:37 +00001318 */
Ronald Croncada4102023-03-07 09:51:39 +01001319 legacy_session_id_len = *(p++);
1320 legacy_session_id = p;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001321
XiaokangQianb67384d2022-04-19 00:02:38 +00001322 /*
1323 * Check we have enough data for the legacy session identifier
Jerry Yue95c8af2022-07-26 15:48:20 +08001324 * and the ciphersuite list length.
XiaokangQianb67384d2022-04-19 00:02:38 +00001325 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001326 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, legacy_session_id_len + 2);
XiaokangQian4080a7f2022-04-11 09:55:18 +00001327 p += legacy_session_id_len;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001328
Ronald Cron2f16b4e2023-03-07 10:07:32 +01001329 /* ...
1330 * CipherSuite cipher_suites<2..2^16-2>;
1331 * ...
1332 * with CipherSuite defined as:
1333 * uint8 CipherSuite[2];
1334 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001335 cipher_suites_len = MBEDTLS_GET_UINT16_BE(p, 0);
XiaokangQian7807f9f2022-02-15 10:04:37 +00001336 p += 2;
Ronald Cron2f16b4e2023-03-07 10:07:32 +01001337 cipher_suites = p;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001338
Ronald Cronfc7ae872023-02-16 15:32:19 +01001339 /*
1340 * The length of the ciphersuite list has to be even.
1341 */
1342 if (cipher_suites_len & 1) {
1343 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR,
1344 MBEDTLS_ERR_SSL_DECODE_ERROR);
1345 return MBEDTLS_ERR_SSL_DECODE_ERROR;
1346 }
1347
XiaokangQianb67384d2022-04-19 00:02:38 +00001348 /* Check we have enough data for the ciphersuite list, the legacy
1349 * compression methods and the length of the extensions.
Jerry Yue95c8af2022-07-26 15:48:20 +08001350 *
1351 * cipher_suites cipher_suites_len bytes
1352 * legacy_compression_methods 2 bytes
1353 * extensions_len 2 bytes
XiaokangQianb67384d2022-04-19 00:02:38 +00001354 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001355 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, cipher_suites_len + 2 + 2);
Ronald Cron8c527d02023-03-07 15:47:47 +01001356 p += cipher_suites_len;
1357 cipher_suites_end = p;
Jerry Yu5725f1c2022-08-21 17:27:16 +08001358
1359 /*
Ronald Cron8c527d02023-03-07 15:47:47 +01001360 * Search for the supported versions extension and parse it to determine
1361 * if the client supports TLS 1.3.
1362 */
1363 ret = mbedtls_ssl_tls13_is_supported_versions_ext_present_in_exts(
1364 ssl, p + 2, end,
Ronald Croneff56732023-04-03 17:36:31 +02001365 &supported_versions_data, &supported_versions_data_end);
Ronald Cron8c527d02023-03-07 15:47:47 +01001366 if (ret < 0) {
1367 MBEDTLS_SSL_DEBUG_RET(1,
1368 ("mbedtls_ssl_tls13_is_supported_versions_ext_present_in_exts"), ret);
1369 return ret;
1370 }
1371
1372 if (ret == 0) {
Ronald Cron5af4c7f2023-03-07 20:46:59 +01001373 return SSL_CLIENT_HELLO_TLS1_2;
Ronald Cron8c527d02023-03-07 15:47:47 +01001374 }
1375
Ronald Cron5af4c7f2023-03-07 20:46:59 +01001376 if (ret == 1) {
1377 ret = ssl_tls13_parse_supported_versions_ext(ssl,
Ronald Croneff56732023-04-03 17:36:31 +02001378 supported_versions_data,
1379 supported_versions_data_end);
Ronald Cron5af4c7f2023-03-07 20:46:59 +01001380 if (ret < 0) {
1381 MBEDTLS_SSL_DEBUG_RET(1,
1382 ("ssl_tls13_parse_supported_versions_ext"), ret);
1383 return ret;
1384 }
1385
Ronald Cronb828c7d2023-04-03 16:37:22 +02001386 /*
1387 * The supported versions extension was parsed successfully as the
1388 * value returned by ssl_tls13_parse_supported_versions_ext() is
1389 * positive. The return value is then equal to
1390 * MBEDTLS_SSL_VERSION_TLS1_2 or MBEDTLS_SSL_VERSION_TLS1_3, defining
1391 * the TLS version to negotiate.
1392 */
Ronald Cron5af4c7f2023-03-07 20:46:59 +01001393 if (MBEDTLS_SSL_VERSION_TLS1_2 == ret) {
1394 return SSL_CLIENT_HELLO_TLS1_2;
1395 }
Ronald Cron8c527d02023-03-07 15:47:47 +01001396 }
1397
1398 /*
1399 * We negotiate TLS 1.3.
Ronald Cron64582392023-03-07 09:21:40 +01001400 */
1401 ssl->tls_version = MBEDTLS_SSL_VERSION_TLS1_3;
1402
1403#if defined(MBEDTLS_SSL_SESSION_TICKETS)
1404 /* Store minor version for later use with ticket serialization. */
1405 ssl->session_negotiate->tls_version = MBEDTLS_SSL_VERSION_TLS1_3;
1406 ssl->session_negotiate->endpoint = ssl->conf->endpoint;
1407#endif
1408
1409 /*
Ronald Crondad02b22023-04-06 09:57:52 +02001410 * We are negotiating the version 1.3 of the protocol. Do what we have
Ronald Croncada4102023-03-07 09:51:39 +01001411 * postponed: copy of the client random bytes, copy of the legacy session
Ronald Cron2f16b4e2023-03-07 10:07:32 +01001412 * identifier and selection of the TLS 1.3 cipher suite.
Ronald Crond540d992023-03-07 09:41:48 +01001413 */
1414 MBEDTLS_SSL_DEBUG_BUF(3, "client hello, random bytes",
1415 random, MBEDTLS_CLIENT_HELLO_RANDOM_LEN);
1416 memcpy(&handshake->randbytes[0], random, MBEDTLS_CLIENT_HELLO_RANDOM_LEN);
1417
Ronald Croncada4102023-03-07 09:51:39 +01001418 if (legacy_session_id_len > sizeof(ssl->session_negotiate->id)) {
1419 MBEDTLS_SSL_DEBUG_MSG(1, ("bad client hello message"));
1420 return MBEDTLS_ERR_SSL_DECODE_ERROR;
1421 }
1422 ssl->session_negotiate->id_len = legacy_session_id_len;
1423 MBEDTLS_SSL_DEBUG_BUF(3, "client hello, session id",
1424 legacy_session_id, legacy_session_id_len);
1425 memcpy(&ssl->session_negotiate->id[0],
1426 legacy_session_id, legacy_session_id_len);
1427
Ronald Crond540d992023-03-07 09:41:48 +01001428 /*
Jerry Yu5725f1c2022-08-21 17:27:16 +08001429 * Search for a matching ciphersuite
1430 */
Ronald Cron2f16b4e2023-03-07 10:07:32 +01001431 MBEDTLS_SSL_DEBUG_BUF(3, "client hello, list of cipher suites",
1432 cipher_suites, cipher_suites_len);
Ronald Crone45afd72023-04-04 15:10:06 +02001433 for (const unsigned char *cipher_suites_p = cipher_suites;
1434 cipher_suites_p < cipher_suites_end; cipher_suites_p += 2) {
XiaokangQiane8ff3502022-04-22 02:34:40 +00001435 uint16_t cipher_suite;
Gilles Peskine449bd832023-01-11 14:50:10 +01001436 const mbedtls_ssl_ciphersuite_t *ciphersuite_info;
Jerry Yu5725f1c2022-08-21 17:27:16 +08001437
Ronald Crond89360b2023-02-21 08:53:33 +01001438 /*
Ronald Crone45afd72023-04-04 15:10:06 +02001439 * "cipher_suites_end - cipher_suites_p is even" is an invariant of the
1440 * loop. As cipher_suites_end - cipher_suites_p > 0, we have
1441 * cipher_suites_end - cipher_suites_p >= 2 and it is thus safe to read
1442 * two bytes.
Ronald Crond89360b2023-02-21 08:53:33 +01001443 */
Ronald Crone45afd72023-04-04 15:10:06 +02001444 cipher_suite = MBEDTLS_GET_UINT16_BE(cipher_suites_p, 0);
Jerry Yuc5a23a02022-08-25 10:51:44 +08001445 ciphersuite_info = ssl_tls13_validate_peer_ciphersuite(
Gilles Peskine449bd832023-01-11 14:50:10 +01001446 ssl, cipher_suite);
1447 if (ciphersuite_info == NULL) {
Jerry Yu5725f1c2022-08-21 17:27:16 +08001448 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +01001449 }
Jerry Yu5725f1c2022-08-21 17:27:16 +08001450
Jerry Yu5725f1c2022-08-21 17:27:16 +08001451 ssl->session_negotiate->ciphersuite = cipher_suite;
Jerry Yuc4bf5d62022-10-29 09:08:47 +08001452 handshake->ciphersuite_info = ciphersuite_info;
Gilles Peskine449bd832023-01-11 14:50:10 +01001453 MBEDTLS_SSL_DEBUG_MSG(2, ("selected ciphersuite: %04x - %s",
1454 cipher_suite,
1455 ciphersuite_info->name));
Ronald Cron25e9ec62023-02-16 15:35:16 +01001456 break;
XiaokangQian17f974c2022-04-19 09:57:41 +00001457 }
Jerry Yu5725f1c2022-08-21 17:27:16 +08001458
Gilles Peskine449bd832023-01-11 14:50:10 +01001459 if (handshake->ciphersuite_info == NULL) {
1460 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE,
1461 MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE);
1462 return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
Jerry Yu5725f1c2022-08-21 17:27:16 +08001463 }
Jerry Yuc1be19f2022-04-23 16:11:39 +08001464
XiaokangQian4080a7f2022-04-11 09:55:18 +00001465 /* ...
XiaokangQianb67384d2022-04-19 00:02:38 +00001466 * opaque legacy_compression_methods<1..2^8-1>;
XiaokangQian4080a7f2022-04-11 09:55:18 +00001467 * ...
XiaokangQian7807f9f2022-02-15 10:04:37 +00001468 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001469 if (p[0] != 1 || p[1] != MBEDTLS_SSL_COMPRESS_NULL) {
1470 MBEDTLS_SSL_DEBUG_MSG(1, ("bad legacy compression method"));
1471 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER,
1472 MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER);
1473 return MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001474 }
XiaokangQianb67384d2022-04-19 00:02:38 +00001475 p += 2;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001476
Jerry Yuc1be19f2022-04-23 16:11:39 +08001477 /* ...
XiaokangQianb67384d2022-04-19 00:02:38 +00001478 * Extension extensions<8..2^16-1>;
Jerry Yuc1be19f2022-04-23 16:11:39 +08001479 * ...
XiaokangQianb67384d2022-04-19 00:02:38 +00001480 * with Extension defined as:
1481 * struct {
1482 * ExtensionType extension_type;
1483 * opaque extension_data<0..2^16-1>;
1484 * } Extension;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001485 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001486 extensions_len = MBEDTLS_GET_UINT16_BE(p, 0);
XiaokangQian7807f9f2022-02-15 10:04:37 +00001487 p += 2;
Gilles Peskine449bd832023-01-11 14:50:10 +01001488 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, extensions_len);
XiaokangQiane8ff3502022-04-22 02:34:40 +00001489 extensions_end = p + extensions_len;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001490
Gilles Peskine449bd832023-01-11 14:50:10 +01001491 MBEDTLS_SSL_DEBUG_BUF(3, "client hello extensions", p, extensions_len);
Jerry Yu63a459c2022-10-31 13:38:40 +08001492 handshake->received_extensions = MBEDTLS_SSL_EXT_MASK_NONE;
Jerry Yu0c354a22022-08-29 15:25:36 +08001493
Gilles Peskine449bd832023-01-11 14:50:10 +01001494 while (p < extensions_end) {
XiaokangQian7807f9f2022-02-15 10:04:37 +00001495 unsigned int extension_type;
1496 size_t extension_data_len;
1497 const unsigned char *extension_data_end;
1498
Jerry Yu0c354a22022-08-29 15:25:36 +08001499 /* RFC 8446, section 4.2.11
Jerry Yu1c9247c2022-07-21 12:37:39 +08001500 *
1501 * The "pre_shared_key" extension MUST be the last extension in the
1502 * ClientHello (this facilitates implementation as described below).
1503 * Servers MUST check that it is the last extension and otherwise fail
1504 * the handshake with an "illegal_parameter" alert.
1505 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001506 if (handshake->received_extensions & MBEDTLS_SSL_EXT_MASK(PRE_SHARED_KEY)) {
Jerry Yu1c9247c2022-07-21 12:37:39 +08001507 MBEDTLS_SSL_DEBUG_MSG(
Gilles Peskine449bd832023-01-11 14:50:10 +01001508 3, ("pre_shared_key is not last extension."));
Jerry Yu1c9247c2022-07-21 12:37:39 +08001509 MBEDTLS_SSL_PEND_FATAL_ALERT(
1510 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER,
Gilles Peskine449bd832023-01-11 14:50:10 +01001511 MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER);
1512 return MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER;
Jerry Yu1c9247c2022-07-21 12:37:39 +08001513 }
1514
Gilles Peskine449bd832023-01-11 14:50:10 +01001515 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, extensions_end, 4);
1516 extension_type = MBEDTLS_GET_UINT16_BE(p, 0);
1517 extension_data_len = MBEDTLS_GET_UINT16_BE(p, 2);
XiaokangQian7807f9f2022-02-15 10:04:37 +00001518 p += 4;
1519
Gilles Peskine449bd832023-01-11 14:50:10 +01001520 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, extensions_end, extension_data_len);
XiaokangQian7807f9f2022-02-15 10:04:37 +00001521 extension_data_end = p + extension_data_len;
1522
Jerry Yuc4bf5d62022-10-29 09:08:47 +08001523 ret = mbedtls_ssl_tls13_check_received_extension(
Gilles Peskine449bd832023-01-11 14:50:10 +01001524 ssl, MBEDTLS_SSL_HS_CLIENT_HELLO, extension_type,
1525 MBEDTLS_SSL_TLS1_3_ALLOWED_EXTS_OF_CH);
1526 if (ret != 0) {
1527 return ret;
1528 }
Jerry Yue18dc7e2022-08-04 16:29:22 +08001529
Gilles Peskine449bd832023-01-11 14:50:10 +01001530 switch (extension_type) {
XiaokangQian40a35232022-05-07 09:02:40 +00001531#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
1532 case MBEDTLS_TLS_EXT_SERVERNAME:
Gilles Peskine449bd832023-01-11 14:50:10 +01001533 MBEDTLS_SSL_DEBUG_MSG(3, ("found ServerName extension"));
1534 ret = mbedtls_ssl_parse_server_name_ext(ssl, p,
1535 extension_data_end);
1536 if (ret != 0) {
XiaokangQian40a35232022-05-07 09:02:40 +00001537 MBEDTLS_SSL_DEBUG_RET(
Gilles Peskine449bd832023-01-11 14:50:10 +01001538 1, "mbedtls_ssl_parse_servername_ext", ret);
1539 return ret;
XiaokangQian40a35232022-05-07 09:02:40 +00001540 }
XiaokangQian40a35232022-05-07 09:02:40 +00001541 break;
1542#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
1543
Valerio Setti080a22b2023-03-20 15:22:47 +01001544#if defined(PSA_WANT_ALG_ECDH)
XiaokangQian7807f9f2022-02-15 10:04:37 +00001545 case MBEDTLS_TLS_EXT_SUPPORTED_GROUPS:
Gilles Peskine449bd832023-01-11 14:50:10 +01001546 MBEDTLS_SSL_DEBUG_MSG(3, ("found supported group extension"));
XiaokangQian7807f9f2022-02-15 10:04:37 +00001547
1548 /* Supported Groups Extension
1549 *
1550 * When sent by the client, the "supported_groups" extension
1551 * indicates the named groups which the client supports,
1552 * ordered from most preferred to least preferred.
1553 */
Jerry Yuc1be19f2022-04-23 16:11:39 +08001554 ret = ssl_tls13_parse_supported_groups_ext(
Gilles Peskine449bd832023-01-11 14:50:10 +01001555 ssl, p, extension_data_end);
1556 if (ret != 0) {
Xiaokang Qian9f1747b2023-03-30 02:50:04 +00001557 MBEDTLS_SSL_DEBUG_RET(
Xiaokang Qian8bce0e62023-04-04 10:15:48 +00001558 1, "ssl_tls13_parse_supported_groups_ext", ret);
Gilles Peskine449bd832023-01-11 14:50:10 +01001559 return ret;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001560 }
1561
XiaokangQian7807f9f2022-02-15 10:04:37 +00001562 break;
Valerio Setti080a22b2023-03-20 15:22:47 +01001563#endif /* PSA_WANT_ALG_ECDH */
XiaokangQian7807f9f2022-02-15 10:04:37 +00001564
Valerio Setti080a22b2023-03-20 15:22:47 +01001565#if defined(PSA_WANT_ALG_ECDH)
XiaokangQian7807f9f2022-02-15 10:04:37 +00001566 case MBEDTLS_TLS_EXT_KEY_SHARE:
Gilles Peskine449bd832023-01-11 14:50:10 +01001567 MBEDTLS_SSL_DEBUG_MSG(3, ("found key share extension"));
XiaokangQian7807f9f2022-02-15 10:04:37 +00001568
1569 /*
1570 * Key Share Extension
1571 *
1572 * When sent by the client, the "key_share" extension
1573 * contains the endpoint's cryptographic parameters for
1574 * ECDHE/DHE key establishment methods.
1575 */
Jerry Yuc1be19f2022-04-23 16:11:39 +08001576 ret = ssl_tls13_parse_key_shares_ext(
Gilles Peskine449bd832023-01-11 14:50:10 +01001577 ssl, p, extension_data_end);
1578 if (ret == SSL_TLS1_3_PARSE_KEY_SHARES_EXT_NO_MATCH) {
1579 MBEDTLS_SSL_DEBUG_MSG(2, ("HRR needed "));
Jerry Yu49ca9282022-05-05 11:05:22 +08001580 hrr_required = 1;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001581 }
1582
Gilles Peskine449bd832023-01-11 14:50:10 +01001583 if (ret < 0) {
Jerry Yu582dd062022-04-22 21:59:01 +08001584 MBEDTLS_SSL_DEBUG_RET(
Gilles Peskine449bd832023-01-11 14:50:10 +01001585 1, "ssl_tls13_parse_key_shares_ext", ret);
1586 return ret;
Jerry Yu582dd062022-04-22 21:59:01 +08001587 }
XiaokangQian7807f9f2022-02-15 10:04:37 +00001588
XiaokangQian7807f9f2022-02-15 10:04:37 +00001589 break;
Valerio Setti080a22b2023-03-20 15:22:47 +01001590#endif /* PSA_WANT_ALG_ECDH */
XiaokangQian7807f9f2022-02-15 10:04:37 +00001591
1592 case MBEDTLS_TLS_EXT_SUPPORTED_VERSIONS:
Ronald Cron8c527d02023-03-07 15:47:47 +01001593 /* Already parsed */
XiaokangQian7807f9f2022-02-15 10:04:37 +00001594 break;
1595
Ronald Cron41a443a2022-10-04 16:38:25 +02001596#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED)
Jerry Yue19e3b92022-07-08 12:04:51 +00001597 case MBEDTLS_TLS_EXT_PSK_KEY_EXCHANGE_MODES:
Xiaokang Qian9f1747b2023-03-30 02:50:04 +00001598 MBEDTLS_SSL_DEBUG_MSG(
1599 3, ("found psk key exchange modes extension"));
Jerry Yue19e3b92022-07-08 12:04:51 +00001600
1601 ret = ssl_tls13_parse_key_exchange_modes_ext(
Gilles Peskine449bd832023-01-11 14:50:10 +01001602 ssl, p, extension_data_end);
1603 if (ret != 0) {
Jerry Yue19e3b92022-07-08 12:04:51 +00001604 MBEDTLS_SSL_DEBUG_RET(
Gilles Peskine449bd832023-01-11 14:50:10 +01001605 1, "ssl_tls13_parse_key_exchange_modes_ext", ret);
1606 return ret;
Jerry Yue19e3b92022-07-08 12:04:51 +00001607 }
1608
Jerry Yue19e3b92022-07-08 12:04:51 +00001609 break;
Ronald Cron41a443a2022-10-04 16:38:25 +02001610#endif
Jerry Yue19e3b92022-07-08 12:04:51 +00001611
Jerry Yu1c105562022-07-10 06:32:38 +00001612 case MBEDTLS_TLS_EXT_PRE_SHARED_KEY:
Gilles Peskine449bd832023-01-11 14:50:10 +01001613 MBEDTLS_SSL_DEBUG_MSG(3, ("found pre_shared_key extension"));
1614 if ((handshake->received_extensions &
1615 MBEDTLS_SSL_EXT_MASK(PSK_KEY_EXCHANGE_MODES)) == 0) {
Jerry Yu13ab81d2022-07-22 23:17:11 +08001616 MBEDTLS_SSL_PEND_FATAL_ALERT(
1617 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER,
Gilles Peskine449bd832023-01-11 14:50:10 +01001618 MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER);
1619 return MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER;
Jerry Yu13ab81d2022-07-22 23:17:11 +08001620 }
Ronald Cron41a443a2022-10-04 16:38:25 +02001621#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED)
Jerry Yu1c105562022-07-10 06:32:38 +00001622 /* Delay processing of the PSK identity once we have
1623 * found out which algorithms to use. We keep a pointer
1624 * to the buffer and the size for later processing.
1625 */
Jerry Yu29d9faa2022-08-23 17:52:45 +08001626 pre_shared_key_ext = p;
Jerry Yu1c105562022-07-10 06:32:38 +00001627 pre_shared_key_ext_end = extension_data_end;
Jerry Yue18dc7e2022-08-04 16:29:22 +08001628#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED */
Jerry Yu1c105562022-07-10 06:32:38 +00001629 break;
Jerry Yu1c105562022-07-10 06:32:38 +00001630
XiaokangQianacb39922022-06-17 10:18:48 +00001631#if defined(MBEDTLS_SSL_ALPN)
1632 case MBEDTLS_TLS_EXT_ALPN:
Gilles Peskine449bd832023-01-11 14:50:10 +01001633 MBEDTLS_SSL_DEBUG_MSG(3, ("found alpn extension"));
XiaokangQianacb39922022-06-17 10:18:48 +00001634
Gilles Peskine449bd832023-01-11 14:50:10 +01001635 ret = mbedtls_ssl_parse_alpn_ext(ssl, p, extension_data_end);
1636 if (ret != 0) {
XiaokangQianacb39922022-06-17 10:18:48 +00001637 MBEDTLS_SSL_DEBUG_RET(
Gilles Peskine449bd832023-01-11 14:50:10 +01001638 1, ("mbedtls_ssl_parse_alpn_ext"), ret);
1639 return ret;
XiaokangQianacb39922022-06-17 10:18:48 +00001640 }
XiaokangQianacb39922022-06-17 10:18:48 +00001641 break;
1642#endif /* MBEDTLS_SSL_ALPN */
1643
Ronald Cron928cbd32022-10-04 16:14:26 +02001644#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED)
XiaokangQian7807f9f2022-02-15 10:04:37 +00001645 case MBEDTLS_TLS_EXT_SIG_ALG:
Gilles Peskine449bd832023-01-11 14:50:10 +01001646 MBEDTLS_SSL_DEBUG_MSG(3, ("found signature_algorithms extension"));
XiaokangQian7807f9f2022-02-15 10:04:37 +00001647
Gabor Mezei078e8032022-04-27 21:17:56 +02001648 ret = mbedtls_ssl_parse_sig_alg_ext(
Gilles Peskine449bd832023-01-11 14:50:10 +01001649 ssl, p, extension_data_end);
1650 if (ret != 0) {
Xiaokang Qian49f39c12023-04-06 02:31:02 +00001651 MBEDTLS_SSL_DEBUG_RET(
1652 1, "mbedtls_ssl_parse_sig_alg_ext", ret);
Gilles Peskine449bd832023-01-11 14:50:10 +01001653 return ret;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001654 }
XiaokangQian7807f9f2022-02-15 10:04:37 +00001655 break;
Ronald Cron928cbd32022-10-04 16:14:26 +02001656#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED */
XiaokangQian7807f9f2022-02-15 10:04:37 +00001657
Jan Bruckner151f6422023-02-10 12:45:19 +01001658#if defined(MBEDTLS_SSL_RECORD_SIZE_LIMIT)
1659 case MBEDTLS_TLS_EXT_RECORD_SIZE_LIMIT:
1660 MBEDTLS_SSL_DEBUG_MSG(3, ("found record_size_limit extension"));
1661
Xiaokang Qian9f1747b2023-03-30 02:50:04 +00001662 ret = mbedtls_ssl_tls13_parse_record_size_limit_ext(
1663 ssl, p, extension_data_end);
Jan Bruckner151f6422023-02-10 12:45:19 +01001664
Xiaokang Qian09c3ccc2023-04-04 10:18:38 +00001665 /*
1666 * TODO: Return unconditionally here until we handle the record
1667 * size limit correctly.
1668 * Once handled correctly, only return in case of errors.
1669 */
Jan Bruckner151f6422023-02-10 12:45:19 +01001670 return ret;
1671
1672 break;
1673#endif /* MBEDTLS_SSL_RECORD_SIZE_LIMIT */
1674
XiaokangQian7807f9f2022-02-15 10:04:37 +00001675 default:
Jerry Yu79aa7212022-11-08 21:30:21 +08001676 MBEDTLS_SSL_PRINT_EXT(
Jerry Yu63a459c2022-10-31 13:38:40 +08001677 3, MBEDTLS_SSL_HS_CLIENT_HELLO,
Gilles Peskine449bd832023-01-11 14:50:10 +01001678 extension_type, "( ignored )");
Jerry Yu0c354a22022-08-29 15:25:36 +08001679 break;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001680 }
1681
1682 p += extension_data_len;
1683 }
1684
Gilles Peskine449bd832023-01-11 14:50:10 +01001685 MBEDTLS_SSL_PRINT_EXTS(3, MBEDTLS_SSL_HS_CLIENT_HELLO,
1686 handshake->received_extensions);
Jerry Yue18dc7e2022-08-04 16:29:22 +08001687
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +01001688 ret = mbedtls_ssl_add_hs_hdr_to_checksum(ssl,
1689 MBEDTLS_SSL_HS_CLIENT_HELLO,
1690 p - buf);
1691 if (0 != ret) {
1692 MBEDTLS_SSL_DEBUG_RET(1, ("mbedtls_ssl_add_hs_hdr_to_checksum"), ret);
1693 return ret;
1694 }
Jerry Yu032b15ce2022-07-11 06:10:03 +00001695
Ronald Cron41a443a2022-10-04 16:38:25 +02001696#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED)
XiaokangQian7807f9f2022-02-15 10:04:37 +00001697 /* Update checksum with either
1698 * - The entire content of the CH message, if no PSK extension is present
1699 * - The content up to but excluding the PSK extension, if present.
1700 */
Jerry Yu1c105562022-07-10 06:32:38 +00001701 /* If we've settled on a PSK-based exchange, parse PSK identity ext */
Gilles Peskine449bd832023-01-11 14:50:10 +01001702 if (mbedtls_ssl_tls13_some_psk_enabled(ssl) &&
1703 mbedtls_ssl_conf_tls13_some_psk_enabled(ssl) &&
1704 (handshake->received_extensions & MBEDTLS_SSL_EXT_MASK(PRE_SHARED_KEY))) {
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +01001705 ret = handshake->update_checksum(ssl, buf,
1706 pre_shared_key_ext - buf);
1707 if (0 != ret) {
1708 MBEDTLS_SSL_DEBUG_RET(1, ("update_checksum"), ret);
1709 return ret;
1710 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001711 ret = ssl_tls13_parse_pre_shared_key_ext(ssl,
1712 pre_shared_key_ext,
1713 pre_shared_key_ext_end,
1714 cipher_suites,
1715 cipher_suites_end);
1716 if (ret == MBEDTLS_ERR_SSL_UNKNOWN_IDENTITY) {
1717 handshake->received_extensions &= ~MBEDTLS_SSL_EXT_MASK(PRE_SHARED_KEY);
1718 } else if (ret != 0) {
Jerry Yu63a459c2022-10-31 13:38:40 +08001719 MBEDTLS_SSL_DEBUG_RET(
Gilles Peskine449bd832023-01-11 14:50:10 +01001720 1, "ssl_tls13_parse_pre_shared_key_ext", ret);
1721 return ret;
Jerry Yu1c105562022-07-10 06:32:38 +00001722 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001723 } else
Ronald Cron41a443a2022-10-04 16:38:25 +02001724#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED */
Jerry Yu1c105562022-07-10 06:32:38 +00001725 {
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +01001726 ret = handshake->update_checksum(ssl, buf, p - buf);
1727 if (0 != ret) {
1728 MBEDTLS_SSL_DEBUG_RET(1, ("update_checksum"), ret);
1729 return ret;
1730 }
Jerry Yu1c105562022-07-10 06:32:38 +00001731 }
XiaokangQian7807f9f2022-02-15 10:04:37 +00001732
Gilles Peskine449bd832023-01-11 14:50:10 +01001733 ret = ssl_tls13_determine_key_exchange_mode(ssl);
1734 if (ret < 0) {
1735 return ret;
1736 }
XiaokangQian7807f9f2022-02-15 10:04:37 +00001737
Gilles Peskine449bd832023-01-11 14:50:10 +01001738 mbedtls_ssl_optimize_checksum(ssl, handshake->ciphersuite_info);
Jerry Yue5834fd2022-08-29 20:16:09 +08001739
Gilles Peskine449bd832023-01-11 14:50:10 +01001740 return hrr_required ? SSL_CLIENT_HELLO_HRR_REQUIRED : SSL_CLIENT_HELLO_OK;
XiaokangQian75fe8c72022-06-15 09:42:45 +00001741}
1742
1743/* Update the handshake state machine */
1744
Ronald Cronce7d76e2022-07-08 18:56:49 +02001745MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01001746static int ssl_tls13_postprocess_client_hello(mbedtls_ssl_context *ssl)
XiaokangQian75fe8c72022-06-15 09:42:45 +00001747{
1748 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1749
XiaokangQian7807f9f2022-02-15 10:04:37 +00001750 /*
XiaokangQianfb665a82022-06-15 03:57:21 +00001751 * Server certificate selection
1752 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001753 if (ssl->conf->f_cert_cb && (ret = ssl->conf->f_cert_cb(ssl)) != 0) {
1754 MBEDTLS_SSL_DEBUG_RET(1, "f_cert_cb", ret);
1755 return ret;
XiaokangQianfb665a82022-06-15 03:57:21 +00001756 }
1757#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
1758 ssl->handshake->sni_name = NULL;
1759 ssl->handshake->sni_name_len = 0;
1760#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
XiaokangQian7807f9f2022-02-15 10:04:37 +00001761
Gilles Peskine449bd832023-01-11 14:50:10 +01001762 ret = mbedtls_ssl_tls13_key_schedule_stage_early(ssl);
1763 if (ret != 0) {
1764 MBEDTLS_SSL_DEBUG_RET(1,
1765 "mbedtls_ssl_tls1_3_key_schedule_stage_early", ret);
1766 return ret;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001767 }
1768
Gilles Peskine449bd832023-01-11 14:50:10 +01001769 return 0;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001770
1771}
1772
1773/*
XiaokangQianed582dd2022-04-13 08:21:05 +00001774 * Main entry point from the state machine; orchestrates the otherfunctions.
1775 */
1776
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001777MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01001778static int ssl_tls13_process_client_hello(mbedtls_ssl_context *ssl)
XiaokangQianed582dd2022-04-13 08:21:05 +00001779{
1780
XiaokangQian08037552022-04-20 07:16:41 +00001781 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine449bd832023-01-11 14:50:10 +01001782 unsigned char *buf = NULL;
XiaokangQianed582dd2022-04-13 08:21:05 +00001783 size_t buflen = 0;
Jerry Yu4ca91402022-05-09 15:50:57 +08001784 int parse_client_hello_ret;
1785
Gilles Peskine449bd832023-01-11 14:50:10 +01001786 MBEDTLS_SSL_DEBUG_MSG(2, ("=> parse client hello"));
XiaokangQianed582dd2022-04-13 08:21:05 +00001787
Gilles Peskine449bd832023-01-11 14:50:10 +01001788 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_tls13_fetch_handshake_msg(
1789 ssl, MBEDTLS_SSL_HS_CLIENT_HELLO,
1790 &buf, &buflen));
XiaokangQianed582dd2022-04-13 08:21:05 +00001791
Gilles Peskine449bd832023-01-11 14:50:10 +01001792 MBEDTLS_SSL_PROC_CHK_NEG(ssl_tls13_parse_client_hello(ssl, buf,
1793 buf + buflen));
Ronald Cron5af4c7f2023-03-07 20:46:59 +01001794 parse_client_hello_ret = ret; /* Store positive return value of
1795 * parse_client_hello,
1796 * as negative error codes are handled
Jerry Yuf41553b2022-05-09 22:20:30 +08001797 * by MBEDTLS_SSL_PROC_CHK_NEG. */
Jerry Yu4ca91402022-05-09 15:50:57 +08001798
Ronald Cronb828c7d2023-04-03 16:37:22 +02001799 /*
1800 * Version 1.2 of the protocol has been chosen, set the
1801 * ssl->keep_current_message flag for the ClientHello to be kept and parsed
1802 * as a TLS 1.2 ClientHello. We also change ssl->tls_version to
1803 * MBEDTLS_SSL_VERSION_TLS1_2 thus from now on mbedtls_ssl_handshake_step()
1804 * will dispatch to the TLS 1.2 state machine.
1805 */
Ronald Cron5af4c7f2023-03-07 20:46:59 +01001806 if (SSL_CLIENT_HELLO_TLS1_2 == parse_client_hello_ret) {
1807 ssl->keep_current_message = 1;
1808 ssl->tls_version = MBEDTLS_SSL_VERSION_TLS1_2;
1809 return 0;
1810 }
1811
Gilles Peskine449bd832023-01-11 14:50:10 +01001812 MBEDTLS_SSL_PROC_CHK(ssl_tls13_postprocess_client_hello(ssl));
Jerry Yu582dd062022-04-22 21:59:01 +08001813
Ronald Cron5af4c7f2023-03-07 20:46:59 +01001814 if (SSL_CLIENT_HELLO_OK == parse_client_hello_ret) {
Gilles Peskine449bd832023-01-11 14:50:10 +01001815 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_SERVER_HELLO);
1816 } else {
1817 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_HELLO_RETRY_REQUEST);
1818 }
XiaokangQianed582dd2022-04-13 08:21:05 +00001819
1820cleanup:
1821
Gilles Peskine449bd832023-01-11 14:50:10 +01001822 MBEDTLS_SSL_DEBUG_MSG(2, ("<= parse client hello"));
1823 return ret;
XiaokangQianed582dd2022-04-13 08:21:05 +00001824}
1825
1826/*
Jerry Yu1c3e6882022-04-20 21:23:40 +08001827 * Handler for MBEDTLS_SSL_SERVER_HELLO
Jerry Yu5b64ae92022-03-30 17:15:02 +08001828 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001829MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01001830static int ssl_tls13_prepare_server_hello(mbedtls_ssl_context *ssl)
Jerry Yuf4b27e42022-03-30 17:32:21 +08001831{
Jerry Yu637a3f12022-04-20 21:37:58 +08001832 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1833 unsigned char *server_randbytes =
Gilles Peskine449bd832023-01-11 14:50:10 +01001834 ssl->handshake->randbytes + MBEDTLS_CLIENT_HELLO_RANDOM_LEN;
1835 if (ssl->conf->f_rng == NULL) {
1836 MBEDTLS_SSL_DEBUG_MSG(1, ("no RNG provided"));
1837 return MBEDTLS_ERR_SSL_NO_RNG;
Jerry Yuf4b27e42022-03-30 17:32:21 +08001838 }
1839
Gilles Peskine449bd832023-01-11 14:50:10 +01001840 if ((ret = ssl->conf->f_rng(ssl->conf->p_rng, server_randbytes,
1841 MBEDTLS_SERVER_HELLO_RANDOM_LEN)) != 0) {
1842 MBEDTLS_SSL_DEBUG_RET(1, "f_rng", ret);
1843 return ret;
Jerry Yuf4b27e42022-03-30 17:32:21 +08001844 }
1845
Gilles Peskine449bd832023-01-11 14:50:10 +01001846 MBEDTLS_SSL_DEBUG_BUF(3, "server hello, random bytes", server_randbytes,
1847 MBEDTLS_SERVER_HELLO_RANDOM_LEN);
Jerry Yuf4b27e42022-03-30 17:32:21 +08001848
1849#if defined(MBEDTLS_HAVE_TIME)
YxCda609132023-05-22 12:08:12 -07001850 ssl->session_negotiate->start = mbedtls_time(NULL);
Jerry Yuf4b27e42022-03-30 17:32:21 +08001851#endif /* MBEDTLS_HAVE_TIME */
1852
Gilles Peskine449bd832023-01-11 14:50:10 +01001853 return ret;
Jerry Yuf4b27e42022-03-30 17:32:21 +08001854}
1855
Jerry Yu3bf2c642022-03-30 22:02:12 +08001856/*
Jerry Yue74e04a2022-04-21 09:23:16 +08001857 * ssl_tls13_write_server_hello_supported_versions_ext ():
Jerry Yu3bf2c642022-03-30 22:02:12 +08001858 *
1859 * struct {
Jerry Yufb9f54d2022-04-06 10:08:34 +08001860 * ProtocolVersion selected_version;
Jerry Yu3bf2c642022-03-30 22:02:12 +08001861 * } SupportedVersions;
1862 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001863MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yue74e04a2022-04-21 09:23:16 +08001864static int ssl_tls13_write_server_hello_supported_versions_ext(
Gilles Peskine449bd832023-01-11 14:50:10 +01001865 mbedtls_ssl_context *ssl,
1866 unsigned char *buf,
1867 unsigned char *end,
1868 size_t *out_len)
Jerry Yu3bf2c642022-03-30 22:02:12 +08001869{
Jerry Yu3bf2c642022-03-30 22:02:12 +08001870 *out_len = 0;
1871
Gilles Peskine449bd832023-01-11 14:50:10 +01001872 MBEDTLS_SSL_DEBUG_MSG(3, ("server hello, write selected version"));
Jerry Yu3bf2c642022-03-30 22:02:12 +08001873
1874 /* Check if we have space to write the extension:
1875 * - extension_type (2 bytes)
1876 * - extension_data_length (2 bytes)
Jerry Yu349a6132022-04-14 20:52:56 +08001877 * - selected_version (2 bytes)
Jerry Yu3bf2c642022-03-30 22:02:12 +08001878 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001879 MBEDTLS_SSL_CHK_BUF_PTR(buf, end, 6);
Jerry Yu3bf2c642022-03-30 22:02:12 +08001880
Gilles Peskine449bd832023-01-11 14:50:10 +01001881 MBEDTLS_PUT_UINT16_BE(MBEDTLS_TLS_EXT_SUPPORTED_VERSIONS, buf, 0);
Jerry Yu3bf2c642022-03-30 22:02:12 +08001882
Gilles Peskine449bd832023-01-11 14:50:10 +01001883 MBEDTLS_PUT_UINT16_BE(2, buf, 2);
Jerry Yu3bf2c642022-03-30 22:02:12 +08001884
Gilles Peskine449bd832023-01-11 14:50:10 +01001885 mbedtls_ssl_write_version(buf + 4,
1886 ssl->conf->transport,
1887 ssl->tls_version);
Jerry Yu3bf2c642022-03-30 22:02:12 +08001888
Gilles Peskine449bd832023-01-11 14:50:10 +01001889 MBEDTLS_SSL_DEBUG_MSG(3, ("supported version: [%04x]",
1890 ssl->tls_version));
Jerry Yu3bf2c642022-03-30 22:02:12 +08001891
Jerry Yu349a6132022-04-14 20:52:56 +08001892 *out_len = 6;
Jerry Yu3bf2c642022-03-30 22:02:12 +08001893
Jerry Yub95dd362022-11-08 21:19:34 +08001894 mbedtls_ssl_tls13_set_hs_sent_ext_mask(
Gilles Peskine449bd832023-01-11 14:50:10 +01001895 ssl, MBEDTLS_TLS_EXT_SUPPORTED_VERSIONS);
Jerry Yub95dd362022-11-08 21:19:34 +08001896
Gilles Peskine449bd832023-01-11 14:50:10 +01001897 return 0;
Jerry Yu3bf2c642022-03-30 22:02:12 +08001898}
1899
Jerry Yud9436a12022-04-20 22:28:09 +08001900
Jerry Yu3bf2c642022-03-30 22:02:12 +08001901
1902/* Generate and export a single key share. For hybrid KEMs, this can
1903 * be called multiple times with the different components of the hybrid. */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001904MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01001905static int ssl_tls13_generate_and_write_key_share(mbedtls_ssl_context *ssl,
1906 uint16_t named_group,
1907 unsigned char *buf,
1908 unsigned char *end,
1909 size_t *out_len)
Jerry Yu3bf2c642022-03-30 22:02:12 +08001910{
1911 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yu955ddd72022-04-22 22:27:33 +08001912
1913 *out_len = 0;
1914
Valerio Setti080a22b2023-03-20 15:22:47 +01001915#if defined(PSA_WANT_ALG_ECDH)
Gilles Peskine449bd832023-01-11 14:50:10 +01001916 if (mbedtls_ssl_tls13_named_group_is_ecdhe(named_group)) {
Jerry Yu89e103c2022-03-30 22:43:29 +08001917 ret = mbedtls_ssl_tls13_generate_and_write_ecdh_key_exchange(
Gilles Peskine449bd832023-01-11 14:50:10 +01001918 ssl, named_group, buf, end, out_len);
1919 if (ret != 0) {
Jerry Yu89e103c2022-03-30 22:43:29 +08001920 MBEDTLS_SSL_DEBUG_RET(
1921 1, "mbedtls_ssl_tls13_generate_and_write_ecdh_key_exchange",
Gilles Peskine449bd832023-01-11 14:50:10 +01001922 ret);
1923 return ret;
Jerry Yu3bf2c642022-03-30 22:02:12 +08001924 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001925 } else
Valerio Setti080a22b2023-03-20 15:22:47 +01001926#endif /* PSA_WANT_ALG_ECDH */
Gilles Peskine449bd832023-01-11 14:50:10 +01001927 if (0 /* Other kinds of KEMs */) {
1928 } else {
Jerry Yu955ddd72022-04-22 22:27:33 +08001929 ((void) ssl);
1930 ((void) named_group);
1931 ((void) buf);
1932 ((void) end);
Jerry Yu3bf2c642022-03-30 22:02:12 +08001933 ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
1934 }
1935
Gilles Peskine449bd832023-01-11 14:50:10 +01001936 return ret;
Jerry Yu3bf2c642022-03-30 22:02:12 +08001937}
1938
1939/*
1940 * ssl_tls13_write_key_share_ext
1941 *
1942 * Structure of key_share extension in ServerHello:
1943 *
Jerry Yu1c3e6882022-04-20 21:23:40 +08001944 * struct {
1945 * NamedGroup group;
1946 * opaque key_exchange<1..2^16-1>;
1947 * } KeyShareEntry;
1948 * struct {
1949 * KeyShareEntry server_share;
1950 * } KeyShareServerHello;
Jerry Yu3bf2c642022-03-30 22:02:12 +08001951 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001952MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01001953static int ssl_tls13_write_key_share_ext(mbedtls_ssl_context *ssl,
1954 unsigned char *buf,
1955 unsigned char *end,
1956 size_t *out_len)
Jerry Yu3bf2c642022-03-30 22:02:12 +08001957{
Jerry Yu955ddd72022-04-22 22:27:33 +08001958 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yu3bf2c642022-03-30 22:02:12 +08001959 unsigned char *p = buf;
Jerry Yu57d48412022-04-20 21:50:42 +08001960 uint16_t group = ssl->handshake->offered_group_id;
Jerry Yu3bf2c642022-03-30 22:02:12 +08001961 unsigned char *server_share = buf + 4;
Jerry Yu3bf2c642022-03-30 22:02:12 +08001962 size_t key_exchange_length;
Jerry Yu3bf2c642022-03-30 22:02:12 +08001963
1964 *out_len = 0;
1965
Gilles Peskine449bd832023-01-11 14:50:10 +01001966 MBEDTLS_SSL_DEBUG_MSG(3, ("server hello, adding key share extension"));
Jerry Yu3bf2c642022-03-30 22:02:12 +08001967
Gilles Peskine449bd832023-01-11 14:50:10 +01001968 MBEDTLS_SSL_DEBUG_MSG(2, ("server hello, write selected_group: %s (%04x)",
1969 mbedtls_ssl_named_group_to_str(group),
1970 group));
Jerry Yu58af2332022-09-06 11:19:31 +08001971
Jerry Yu3bf2c642022-03-30 22:02:12 +08001972 /* Check if we have space for header and length fields:
1973 * - extension_type (2 bytes)
1974 * - extension_data_length (2 bytes)
1975 * - group (2 bytes)
1976 * - key_exchange_length (2 bytes)
1977 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001978 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 8);
1979 MBEDTLS_PUT_UINT16_BE(MBEDTLS_TLS_EXT_KEY_SHARE, p, 0);
1980 MBEDTLS_PUT_UINT16_BE(group, server_share, 0);
Jerry Yu3bf2c642022-03-30 22:02:12 +08001981 p += 8;
Jerry Yu57d48412022-04-20 21:50:42 +08001982
Jerry Yu3bf2c642022-03-30 22:02:12 +08001983 /* When we introduce PQC-ECDHE hybrids, we'll want to call this
1984 * function multiple times. */
Jerry Yu955ddd72022-04-22 22:27:33 +08001985 ret = ssl_tls13_generate_and_write_key_share(
Gilles Peskine449bd832023-01-11 14:50:10 +01001986 ssl, group, server_share + 4, end, &key_exchange_length);
1987 if (ret != 0) {
1988 return ret;
1989 }
Jerry Yu3bf2c642022-03-30 22:02:12 +08001990 p += key_exchange_length;
Jerry Yuc1be19f2022-04-23 16:11:39 +08001991
Gilles Peskine449bd832023-01-11 14:50:10 +01001992 MBEDTLS_PUT_UINT16_BE(key_exchange_length, server_share + 2, 0);
Jerry Yu3bf2c642022-03-30 22:02:12 +08001993
Gilles Peskine449bd832023-01-11 14:50:10 +01001994 MBEDTLS_PUT_UINT16_BE(p - server_share, buf, 2);
Jerry Yu3bf2c642022-03-30 22:02:12 +08001995
Jerry Yu57d48412022-04-20 21:50:42 +08001996 *out_len = p - buf;
Jerry Yu955ddd72022-04-22 22:27:33 +08001997
Gilles Peskine449bd832023-01-11 14:50:10 +01001998 mbedtls_ssl_tls13_set_hs_sent_ext_mask(ssl, MBEDTLS_TLS_EXT_KEY_SHARE);
Jerry Yub95dd362022-11-08 21:19:34 +08001999
Gilles Peskine449bd832023-01-11 14:50:10 +01002000 return 0;
Jerry Yu3bf2c642022-03-30 22:02:12 +08002001}
Jerry Yud9436a12022-04-20 22:28:09 +08002002
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002003MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002004static int ssl_tls13_write_hrr_key_share_ext(mbedtls_ssl_context *ssl,
2005 unsigned char *buf,
2006 unsigned char *end,
2007 size_t *out_len)
Jerry Yu23f7a6f2022-04-23 15:16:45 +08002008{
2009 uint16_t selected_group = ssl->handshake->hrr_selected_group;
2010 /* key_share Extension
2011 *
2012 * struct {
2013 * select (Handshake.msg_type) {
2014 * ...
2015 * case hello_retry_request:
2016 * NamedGroup selected_group;
2017 * ...
2018 * };
2019 * } KeyShare;
2020 */
2021
2022 *out_len = 0;
2023
Jerry Yub0ac10b2022-05-05 11:10:08 +08002024 /*
2025 * For a pure PSK key exchange, there is no group to agree upon. The purpose
2026 * of the HRR is then to transmit a cookie to force the client to demonstrate
2027 * reachability at their apparent network address (primarily useful for DTLS).
2028 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002029 if (!mbedtls_ssl_tls13_key_exchange_mode_with_ephemeral(ssl)) {
2030 return 0;
2031 }
Jerry Yu23f7a6f2022-04-23 15:16:45 +08002032
2033 /* We should only send the key_share extension if the client's initial
2034 * key share was not acceptable. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002035 if (ssl->handshake->offered_group_id != 0) {
2036 MBEDTLS_SSL_DEBUG_MSG(4, ("Skip key_share extension in HRR"));
2037 return 0;
Jerry Yu23f7a6f2022-04-23 15:16:45 +08002038 }
2039
Gilles Peskine449bd832023-01-11 14:50:10 +01002040 if (selected_group == 0) {
2041 MBEDTLS_SSL_DEBUG_MSG(1, ("no matching named group found"));
2042 return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
Jerry Yu23f7a6f2022-04-23 15:16:45 +08002043 }
2044
Jerry Yub0ac10b2022-05-05 11:10:08 +08002045 /* Check if we have enough space:
2046 * - extension_type (2 bytes)
2047 * - extension_data_length (2 bytes)
2048 * - selected_group (2 bytes)
2049 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002050 MBEDTLS_SSL_CHK_BUF_PTR(buf, end, 6);
Jerry Yu23f7a6f2022-04-23 15:16:45 +08002051
Gilles Peskine449bd832023-01-11 14:50:10 +01002052 MBEDTLS_PUT_UINT16_BE(MBEDTLS_TLS_EXT_KEY_SHARE, buf, 0);
2053 MBEDTLS_PUT_UINT16_BE(2, buf, 2);
2054 MBEDTLS_PUT_UINT16_BE(selected_group, buf, 4);
Jerry Yu23f7a6f2022-04-23 15:16:45 +08002055
Gilles Peskine449bd832023-01-11 14:50:10 +01002056 MBEDTLS_SSL_DEBUG_MSG(3,
2057 ("HRR selected_group: %s (%x)",
2058 mbedtls_ssl_named_group_to_str(selected_group),
2059 selected_group));
Jerry Yu23f7a6f2022-04-23 15:16:45 +08002060
2061 *out_len = 6;
Jerry Yu23f7a6f2022-04-23 15:16:45 +08002062
Gilles Peskine449bd832023-01-11 14:50:10 +01002063 mbedtls_ssl_tls13_set_hs_sent_ext_mask(ssl, MBEDTLS_TLS_EXT_KEY_SHARE);
Jerry Yub95dd362022-11-08 21:19:34 +08002064
Gilles Peskine449bd832023-01-11 14:50:10 +01002065 return 0;
Jerry Yu23f7a6f2022-04-23 15:16:45 +08002066}
Jerry Yu3bf2c642022-03-30 22:02:12 +08002067
2068/*
2069 * Structure of ServerHello message:
2070 *
2071 * struct {
2072 * ProtocolVersion legacy_version = 0x0303; // TLS v1.2
2073 * Random random;
2074 * opaque legacy_session_id_echo<0..32>;
2075 * CipherSuite cipher_suite;
2076 * uint8 legacy_compression_method = 0;
2077 * Extension extensions<6..2^16-1>;
2078 * } ServerHello;
2079 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002080MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002081static int ssl_tls13_write_server_hello_body(mbedtls_ssl_context *ssl,
2082 unsigned char *buf,
2083 unsigned char *end,
2084 size_t *out_len,
2085 int is_hrr)
Jerry Yu56404d72022-03-30 17:36:13 +08002086{
Jerry Yu955ddd72022-04-22 22:27:33 +08002087 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yu3bf2c642022-03-30 22:02:12 +08002088 unsigned char *p = buf;
Jerry Yud9436a12022-04-20 22:28:09 +08002089 unsigned char *p_extensions_len;
Jerry Yufbe3e642022-04-25 19:31:51 +08002090 size_t output_len;
Jerry Yu3bf2c642022-03-30 22:02:12 +08002091
2092 *out_len = 0;
Jerry Yu50e00e32022-10-31 14:45:01 +08002093 ssl->handshake->sent_extensions = MBEDTLS_SSL_EXT_MASK_NONE;
Jerry Yu3bf2c642022-03-30 22:02:12 +08002094
Jerry Yucfc04b32022-04-21 09:31:58 +08002095 /* ...
2096 * ProtocolVersion legacy_version = 0x0303; // TLS 1.2
2097 * ...
2098 * with ProtocolVersion defined as:
2099 * uint16 ProtocolVersion;
Jerry Yu3bf2c642022-03-30 22:02:12 +08002100 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002101 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 2);
2102 MBEDTLS_PUT_UINT16_BE(0x0303, p, 0);
Jerry Yu3bf2c642022-03-30 22:02:12 +08002103 p += 2;
2104
Jerry Yu1c3e6882022-04-20 21:23:40 +08002105 /* ...
2106 * Random random;
2107 * ...
Jerry Yucfc04b32022-04-21 09:31:58 +08002108 * with Random defined as:
2109 * opaque Random[MBEDTLS_SERVER_HELLO_RANDOM_LEN];
Jerry Yu1c3e6882022-04-20 21:23:40 +08002110 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002111 MBEDTLS_SSL_CHK_BUF_PTR(p, end, MBEDTLS_SERVER_HELLO_RANDOM_LEN);
2112 if (is_hrr) {
2113 memcpy(p, mbedtls_ssl_tls13_hello_retry_request_magic,
2114 MBEDTLS_SERVER_HELLO_RANDOM_LEN);
2115 } else {
2116 memcpy(p, &ssl->handshake->randbytes[MBEDTLS_CLIENT_HELLO_RANDOM_LEN],
2117 MBEDTLS_SERVER_HELLO_RANDOM_LEN);
Jerry Yu23f7a6f2022-04-23 15:16:45 +08002118 }
Gilles Peskine449bd832023-01-11 14:50:10 +01002119 MBEDTLS_SSL_DEBUG_BUF(3, "server hello, random bytes",
2120 p, MBEDTLS_SERVER_HELLO_RANDOM_LEN);
Jerry Yu3bf2c642022-03-30 22:02:12 +08002121 p += MBEDTLS_SERVER_HELLO_RANDOM_LEN;
2122
Jerry Yucfc04b32022-04-21 09:31:58 +08002123 /* ...
2124 * opaque legacy_session_id_echo<0..32>;
2125 * ...
Jerry Yu3bf2c642022-03-30 22:02:12 +08002126 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002127 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 1 + ssl->session_negotiate->id_len);
2128 *p++ = (unsigned char) ssl->session_negotiate->id_len;
2129 if (ssl->session_negotiate->id_len > 0) {
2130 memcpy(p, &ssl->session_negotiate->id[0],
2131 ssl->session_negotiate->id_len);
Jerry Yu3bf2c642022-03-30 22:02:12 +08002132 p += ssl->session_negotiate->id_len;
Jerry Yu955ddd72022-04-22 22:27:33 +08002133
Gilles Peskine449bd832023-01-11 14:50:10 +01002134 MBEDTLS_SSL_DEBUG_BUF(3, "session id", ssl->session_negotiate->id,
2135 ssl->session_negotiate->id_len);
Jerry Yu3bf2c642022-03-30 22:02:12 +08002136 }
2137
Jerry Yucfc04b32022-04-21 09:31:58 +08002138 /* ...
2139 * CipherSuite cipher_suite;
2140 * ...
2141 * with CipherSuite defined as:
2142 * uint8 CipherSuite[2];
Jerry Yu3bf2c642022-03-30 22:02:12 +08002143 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002144 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 2);
2145 MBEDTLS_PUT_UINT16_BE(ssl->session_negotiate->ciphersuite, p, 0);
Jerry Yu3bf2c642022-03-30 22:02:12 +08002146 p += 2;
Gilles Peskine449bd832023-01-11 14:50:10 +01002147 MBEDTLS_SSL_DEBUG_MSG(3,
2148 ("server hello, chosen ciphersuite: %s ( id=%d )",
2149 mbedtls_ssl_get_ciphersuite_name(
2150 ssl->session_negotiate->ciphersuite),
2151 ssl->session_negotiate->ciphersuite));
Jerry Yu3bf2c642022-03-30 22:02:12 +08002152
Jerry Yucfc04b32022-04-21 09:31:58 +08002153 /* ...
2154 * uint8 legacy_compression_method = 0;
2155 * ...
2156 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002157 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 1);
Thomas Daubney31e03a82022-07-25 15:59:25 +01002158 *p++ = MBEDTLS_SSL_COMPRESS_NULL;
Jerry Yu3bf2c642022-03-30 22:02:12 +08002159
Jerry Yucfc04b32022-04-21 09:31:58 +08002160 /* ...
2161 * Extension extensions<6..2^16-1>;
2162 * ...
2163 * struct {
2164 * ExtensionType extension_type; (2 bytes)
2165 * opaque extension_data<0..2^16-1>;
2166 * } Extension;
2167 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002168 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 2);
Jerry Yud9436a12022-04-20 22:28:09 +08002169 p_extensions_len = p;
Jerry Yu3bf2c642022-03-30 22:02:12 +08002170 p += 2;
2171
Gilles Peskine449bd832023-01-11 14:50:10 +01002172 if ((ret = ssl_tls13_write_server_hello_supported_versions_ext(
2173 ssl, p, end, &output_len)) != 0) {
Jerry Yu955ddd72022-04-22 22:27:33 +08002174 MBEDTLS_SSL_DEBUG_RET(
Gilles Peskine449bd832023-01-11 14:50:10 +01002175 1, "ssl_tls13_write_server_hello_supported_versions_ext", ret);
2176 return ret;
Jerry Yu3bf2c642022-03-30 22:02:12 +08002177 }
2178 p += output_len;
2179
Gilles Peskine449bd832023-01-11 14:50:10 +01002180 if (mbedtls_ssl_tls13_key_exchange_mode_with_ephemeral(ssl)) {
2181 if (is_hrr) {
2182 ret = ssl_tls13_write_hrr_key_share_ext(ssl, p, end, &output_len);
2183 } else {
2184 ret = ssl_tls13_write_key_share_ext(ssl, p, end, &output_len);
2185 }
2186 if (ret != 0) {
2187 return ret;
2188 }
Jerry Yu3bf2c642022-03-30 22:02:12 +08002189 p += output_len;
2190 }
Jerry Yu3bf2c642022-03-30 22:02:12 +08002191
Ronald Cron41a443a2022-10-04 16:38:25 +02002192#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01002193 if (!is_hrr && mbedtls_ssl_tls13_key_exchange_mode_with_psk(ssl)) {
2194 ret = ssl_tls13_write_server_pre_shared_key_ext(ssl, p, end, &output_len);
2195 if (ret != 0) {
2196 MBEDTLS_SSL_DEBUG_RET(1, "ssl_tls13_write_server_pre_shared_key_ext",
2197 ret);
2198 return ret;
Jerry Yu032b15ce2022-07-11 06:10:03 +00002199 }
2200 p += output_len;
2201 }
Ronald Cron41a443a2022-10-04 16:38:25 +02002202#endif
Jerry Yu032b15ce2022-07-11 06:10:03 +00002203
Gilles Peskine449bd832023-01-11 14:50:10 +01002204 MBEDTLS_PUT_UINT16_BE(p - p_extensions_len - 2, p_extensions_len, 0);
Jerry Yu3bf2c642022-03-30 22:02:12 +08002205
Gilles Peskine449bd832023-01-11 14:50:10 +01002206 MBEDTLS_SSL_DEBUG_BUF(4, "server hello extensions",
2207 p_extensions_len, p - p_extensions_len);
Jerry Yu3bf2c642022-03-30 22:02:12 +08002208
Jerry Yud9436a12022-04-20 22:28:09 +08002209 *out_len = p - buf;
Jerry Yu3bf2c642022-03-30 22:02:12 +08002210
Gilles Peskine449bd832023-01-11 14:50:10 +01002211 MBEDTLS_SSL_DEBUG_BUF(3, "server hello", buf, *out_len);
Jerry Yu3bf2c642022-03-30 22:02:12 +08002212
Jerry Yu7de2ff02022-11-08 21:43:46 +08002213 MBEDTLS_SSL_PRINT_EXTS(
Jerry Yu4b8f2f72022-10-31 13:31:22 +08002214 3, is_hrr ? MBEDTLS_SSL_TLS1_3_HS_HELLO_RETRY_REQUEST :
Gilles Peskine449bd832023-01-11 14:50:10 +01002215 MBEDTLS_SSL_HS_SERVER_HELLO,
2216 ssl->handshake->sent_extensions);
Jerry Yu4b8f2f72022-10-31 13:31:22 +08002217
Gilles Peskine449bd832023-01-11 14:50:10 +01002218 return ret;
Jerry Yu56404d72022-03-30 17:36:13 +08002219}
2220
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002221MBEDTLS_CHECK_RETURN_CRITICAL
Xiaokang Qian934ce6f2023-02-06 10:23:04 +00002222static int ssl_tls13_finalize_server_hello(mbedtls_ssl_context *ssl)
Jerry Yue110d252022-05-05 10:19:22 +08002223{
2224 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine449bd832023-01-11 14:50:10 +01002225 ret = mbedtls_ssl_tls13_compute_handshake_transform(ssl);
2226 if (ret != 0) {
2227 MBEDTLS_SSL_DEBUG_RET(1,
2228 "mbedtls_ssl_tls13_compute_handshake_transform",
2229 ret);
2230 return ret;
Jerry Yue110d252022-05-05 10:19:22 +08002231 }
2232
Gilles Peskine449bd832023-01-11 14:50:10 +01002233 return ret;
Jerry Yue110d252022-05-05 10:19:22 +08002234}
2235
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002236MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002237static int ssl_tls13_write_server_hello(mbedtls_ssl_context *ssl)
Jerry Yu5b64ae92022-03-30 17:15:02 +08002238{
Jerry Yu637a3f12022-04-20 21:37:58 +08002239 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yuf4b27e42022-03-30 17:32:21 +08002240 unsigned char *buf;
2241 size_t buf_len, msg_len;
2242
Gilles Peskine449bd832023-01-11 14:50:10 +01002243 MBEDTLS_SSL_DEBUG_MSG(2, ("=> write server hello"));
Jerry Yuf4b27e42022-03-30 17:32:21 +08002244
Gilles Peskine449bd832023-01-11 14:50:10 +01002245 MBEDTLS_SSL_PROC_CHK(ssl_tls13_prepare_server_hello(ssl));
Jerry Yuf4b27e42022-03-30 17:32:21 +08002246
Xiaokang Qian9f1747b2023-03-30 02:50:04 +00002247 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_start_handshake_msg(
2248 ssl, MBEDTLS_SSL_HS_SERVER_HELLO, &buf, &buf_len));
Jerry Yuf4b27e42022-03-30 17:32:21 +08002249
Gilles Peskine449bd832023-01-11 14:50:10 +01002250 MBEDTLS_SSL_PROC_CHK(ssl_tls13_write_server_hello_body(ssl, buf,
2251 buf + buf_len,
2252 &msg_len,
2253 0));
Jerry Yuf4b27e42022-03-30 17:32:21 +08002254
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +01002255 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_add_hs_msg_to_checksum(
Manuel Pégourié-Gonnard43cc1272023-02-06 11:48:19 +01002256 ssl, MBEDTLS_SSL_HS_SERVER_HELLO, buf, msg_len));
Jerry Yuf4b27e42022-03-30 17:32:21 +08002257
Gilles Peskine449bd832023-01-11 14:50:10 +01002258 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_finish_handshake_msg(
2259 ssl, buf_len, msg_len));
Jerry Yu637a3f12022-04-20 21:37:58 +08002260
Xiaokang Qian934ce6f2023-02-06 10:23:04 +00002261 MBEDTLS_SSL_PROC_CHK(ssl_tls13_finalize_server_hello(ssl));
Jerry Yue110d252022-05-05 10:19:22 +08002262
Gabor Mezei7b39bf12022-05-24 16:04:14 +02002263#if defined(MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE)
2264 /* The server sends a dummy change_cipher_spec record immediately
2265 * after its first handshake message. This may either be after
2266 * a ServerHello or a HelloRetryRequest.
2267 */
Gabor Mezei96ae9262022-06-28 11:45:18 +02002268 mbedtls_ssl_handshake_set_state(
Gilles Peskine449bd832023-01-11 14:50:10 +01002269 ssl, MBEDTLS_SSL_SERVER_CCS_AFTER_SERVER_HELLO);
Gabor Mezei7b39bf12022-05-24 16:04:14 +02002270#else
Gilles Peskine449bd832023-01-11 14:50:10 +01002271 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_ENCRYPTED_EXTENSIONS);
Gabor Mezei7b39bf12022-05-24 16:04:14 +02002272#endif /* MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE */
Jerry Yue110d252022-05-05 10:19:22 +08002273
Jerry Yuf4b27e42022-03-30 17:32:21 +08002274cleanup:
2275
Gilles Peskine449bd832023-01-11 14:50:10 +01002276 MBEDTLS_SSL_DEBUG_MSG(2, ("<= write server hello"));
2277 return ret;
Jerry Yu5b64ae92022-03-30 17:15:02 +08002278}
2279
Jerry Yu23d1a252022-05-12 18:08:59 +08002280
2281/*
2282 * Handler for MBEDTLS_SSL_HELLO_RETRY_REQUEST
2283 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002284MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002285static int ssl_tls13_prepare_hello_retry_request(mbedtls_ssl_context *ssl)
Jerry Yu23d1a252022-05-12 18:08:59 +08002286{
2287 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine449bd832023-01-11 14:50:10 +01002288 if (ssl->handshake->hello_retry_request_count > 0) {
2289 MBEDTLS_SSL_DEBUG_MSG(1, ("Too many HRRs"));
2290 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE,
2291 MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE);
2292 return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
Jerry Yu23d1a252022-05-12 18:08:59 +08002293 }
2294
2295 /*
2296 * Create stateless transcript hash for HRR
2297 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002298 MBEDTLS_SSL_DEBUG_MSG(4, ("Reset transcript for HRR"));
2299 ret = mbedtls_ssl_reset_transcript_for_hrr(ssl);
2300 if (ret != 0) {
2301 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_reset_transcript_for_hrr", ret);
2302 return ret;
Jerry Yu23d1a252022-05-12 18:08:59 +08002303 }
Gilles Peskine449bd832023-01-11 14:50:10 +01002304 mbedtls_ssl_session_reset_msg_layer(ssl, 0);
Jerry Yu23d1a252022-05-12 18:08:59 +08002305
Gilles Peskine449bd832023-01-11 14:50:10 +01002306 return 0;
Jerry Yu23d1a252022-05-12 18:08:59 +08002307}
2308
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002309MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002310static int ssl_tls13_write_hello_retry_request(mbedtls_ssl_context *ssl)
Jerry Yu23d1a252022-05-12 18:08:59 +08002311{
2312 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
2313 unsigned char *buf;
2314 size_t buf_len, msg_len;
2315
Gilles Peskine449bd832023-01-11 14:50:10 +01002316 MBEDTLS_SSL_DEBUG_MSG(2, ("=> write hello retry request"));
Jerry Yu23d1a252022-05-12 18:08:59 +08002317
Gilles Peskine449bd832023-01-11 14:50:10 +01002318 MBEDTLS_SSL_PROC_CHK(ssl_tls13_prepare_hello_retry_request(ssl));
Jerry Yu23d1a252022-05-12 18:08:59 +08002319
Gilles Peskine449bd832023-01-11 14:50:10 +01002320 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_start_handshake_msg(
2321 ssl, MBEDTLS_SSL_HS_SERVER_HELLO,
2322 &buf, &buf_len));
Jerry Yu23d1a252022-05-12 18:08:59 +08002323
Gilles Peskine449bd832023-01-11 14:50:10 +01002324 MBEDTLS_SSL_PROC_CHK(ssl_tls13_write_server_hello_body(ssl, buf,
2325 buf + buf_len,
2326 &msg_len,
2327 1));
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +01002328 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_add_hs_msg_to_checksum(
Manuel Pégourié-Gonnard43cc1272023-02-06 11:48:19 +01002329 ssl, MBEDTLS_SSL_HS_SERVER_HELLO, buf, msg_len));
Jerry Yu23d1a252022-05-12 18:08:59 +08002330
2331
Gilles Peskine449bd832023-01-11 14:50:10 +01002332 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_finish_handshake_msg(ssl, buf_len,
2333 msg_len));
Jerry Yu23d1a252022-05-12 18:08:59 +08002334
2335 ssl->handshake->hello_retry_request_count++;
2336
Gabor Mezei7b39bf12022-05-24 16:04:14 +02002337#if defined(MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE)
2338 /* The server sends a dummy change_cipher_spec record immediately
2339 * after its first handshake message. This may either be after
2340 * a ServerHello or a HelloRetryRequest.
2341 */
Gabor Mezei96ae9262022-06-28 11:45:18 +02002342 mbedtls_ssl_handshake_set_state(
Gilles Peskine449bd832023-01-11 14:50:10 +01002343 ssl, MBEDTLS_SSL_SERVER_CCS_AFTER_HELLO_RETRY_REQUEST);
Gabor Mezei7b39bf12022-05-24 16:04:14 +02002344#else
Gilles Peskine449bd832023-01-11 14:50:10 +01002345 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_CLIENT_HELLO);
Gabor Mezei7b39bf12022-05-24 16:04:14 +02002346#endif /* MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE */
Jerry Yu23d1a252022-05-12 18:08:59 +08002347
2348cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +01002349 MBEDTLS_SSL_DEBUG_MSG(2, ("<= write hello retry request"));
2350 return ret;
Jerry Yu23d1a252022-05-12 18:08:59 +08002351}
2352
Jerry Yu5b64ae92022-03-30 17:15:02 +08002353/*
Jerry Yu4d3841a2022-04-16 12:37:19 +08002354 * Handler for MBEDTLS_SSL_ENCRYPTED_EXTENSIONS
2355 */
Jerry Yu4d3841a2022-04-16 12:37:19 +08002356
2357/*
2358 * struct {
2359 * Extension extensions<0..2 ^ 16 - 1>;
2360 * } EncryptedExtensions;
2361 *
2362 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002363MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002364static int ssl_tls13_write_encrypted_extensions_body(mbedtls_ssl_context *ssl,
2365 unsigned char *buf,
2366 unsigned char *end,
2367 size_t *out_len)
Jerry Yu4d3841a2022-04-16 12:37:19 +08002368{
XiaokangQianacb39922022-06-17 10:18:48 +00002369 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yu4d3841a2022-04-16 12:37:19 +08002370 unsigned char *p = buf;
2371 size_t extensions_len = 0;
Jerry Yu8937eb42022-05-03 12:12:14 +08002372 unsigned char *p_extensions_len;
XiaokangQianacb39922022-06-17 10:18:48 +00002373 size_t output_len;
Jerry Yu9da5e5a2022-05-03 15:46:09 +08002374
Jerry Yu4d3841a2022-04-16 12:37:19 +08002375 *out_len = 0;
2376
Gilles Peskine449bd832023-01-11 14:50:10 +01002377 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 2);
Jerry Yu8937eb42022-05-03 12:12:14 +08002378 p_extensions_len = p;
Jerry Yu4d3841a2022-04-16 12:37:19 +08002379 p += 2;
2380
Jerry Yu8937eb42022-05-03 12:12:14 +08002381 ((void) ssl);
XiaokangQianacb39922022-06-17 10:18:48 +00002382 ((void) ret);
2383 ((void) output_len);
2384
2385#if defined(MBEDTLS_SSL_ALPN)
Gilles Peskine449bd832023-01-11 14:50:10 +01002386 ret = mbedtls_ssl_write_alpn_ext(ssl, p, end, &output_len);
2387 if (ret != 0) {
2388 return ret;
2389 }
XiaokangQian95d5f542022-06-24 02:29:26 +00002390 p += output_len;
XiaokangQianacb39922022-06-17 10:18:48 +00002391#endif /* MBEDTLS_SSL_ALPN */
Jerry Yu4d3841a2022-04-16 12:37:19 +08002392
Gilles Peskine449bd832023-01-11 14:50:10 +01002393 extensions_len = (p - p_extensions_len) - 2;
2394 MBEDTLS_PUT_UINT16_BE(extensions_len, p_extensions_len, 0);
Jerry Yu8937eb42022-05-03 12:12:14 +08002395
2396 *out_len = p - buf;
Jerry Yu4d3841a2022-04-16 12:37:19 +08002397
Gilles Peskine449bd832023-01-11 14:50:10 +01002398 MBEDTLS_SSL_DEBUG_BUF(4, "encrypted extensions", buf, *out_len);
Jerry Yu4d3841a2022-04-16 12:37:19 +08002399
Jerry Yu7de2ff02022-11-08 21:43:46 +08002400 MBEDTLS_SSL_PRINT_EXTS(
Gilles Peskine449bd832023-01-11 14:50:10 +01002401 3, MBEDTLS_SSL_HS_ENCRYPTED_EXTENSIONS, ssl->handshake->sent_extensions);
Jerry Yu4b8f2f72022-10-31 13:31:22 +08002402
Gilles Peskine449bd832023-01-11 14:50:10 +01002403 return 0;
Jerry Yu4d3841a2022-04-16 12:37:19 +08002404}
2405
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002406MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002407static int ssl_tls13_write_encrypted_extensions(mbedtls_ssl_context *ssl)
Jerry Yu4d3841a2022-04-16 12:37:19 +08002408{
2409 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
2410 unsigned char *buf;
Jerry Yu39730a72022-05-03 12:14:04 +08002411 size_t buf_len, msg_len;
Jerry Yu4d3841a2022-04-16 12:37:19 +08002412
Gilles Peskine449bd832023-01-11 14:50:10 +01002413 mbedtls_ssl_set_outbound_transform(ssl,
2414 ssl->handshake->transform_handshake);
Gabor Mezei54719122022-06-28 11:34:56 +02002415 MBEDTLS_SSL_DEBUG_MSG(
Gilles Peskine449bd832023-01-11 14:50:10 +01002416 3, ("switching to handshake transform for outbound data"));
Gabor Mezei54719122022-06-28 11:34:56 +02002417
Gilles Peskine449bd832023-01-11 14:50:10 +01002418 MBEDTLS_SSL_DEBUG_MSG(2, ("=> write encrypted extensions"));
Jerry Yu4d3841a2022-04-16 12:37:19 +08002419
Xiaokang Qian9f1747b2023-03-30 02:50:04 +00002420 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_start_handshake_msg(
2421 ssl, MBEDTLS_SSL_HS_ENCRYPTED_EXTENSIONS,
2422 &buf, &buf_len));
Jerry Yu4d3841a2022-04-16 12:37:19 +08002423
Gilles Peskine449bd832023-01-11 14:50:10 +01002424 MBEDTLS_SSL_PROC_CHK(ssl_tls13_write_encrypted_extensions_body(
2425 ssl, buf, buf + buf_len, &msg_len));
Jerry Yu4d3841a2022-04-16 12:37:19 +08002426
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +01002427 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_add_hs_msg_to_checksum(
Xiaokang Qian9f1747b2023-03-30 02:50:04 +00002428 ssl, MBEDTLS_SSL_HS_ENCRYPTED_EXTENSIONS,
2429 buf, msg_len));
Jerry Yu4d3841a2022-04-16 12:37:19 +08002430
Gilles Peskine449bd832023-01-11 14:50:10 +01002431 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_finish_handshake_msg(
2432 ssl, buf_len, msg_len));
Jerry Yu4d3841a2022-04-16 12:37:19 +08002433
Ronald Cron928cbd32022-10-04 16:14:26 +02002434#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01002435 if (mbedtls_ssl_tls13_key_exchange_mode_with_psk(ssl)) {
2436 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_SERVER_FINISHED);
2437 } else {
2438 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_CERTIFICATE_REQUEST);
2439 }
Jerry Yu8937eb42022-05-03 12:12:14 +08002440#else
Gilles Peskine449bd832023-01-11 14:50:10 +01002441 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_SERVER_FINISHED);
Jerry Yu8937eb42022-05-03 12:12:14 +08002442#endif
2443
Jerry Yu4d3841a2022-04-16 12:37:19 +08002444cleanup:
2445
Gilles Peskine449bd832023-01-11 14:50:10 +01002446 MBEDTLS_SSL_DEBUG_MSG(2, ("<= write encrypted extensions"));
2447 return ret;
Jerry Yu4d3841a2022-04-16 12:37:19 +08002448}
2449
Ronald Cron928cbd32022-10-04 16:14:26 +02002450#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED)
XiaokangQiana987e1d2022-05-07 01:25:58 +00002451#define SSL_CERTIFICATE_REQUEST_SEND_REQUEST 0
2452#define SSL_CERTIFICATE_REQUEST_SKIP 1
2453/* Coordination:
2454 * Check whether a CertificateRequest message should be written.
2455 * Returns a negative code on failure, or
2456 * - SSL_CERTIFICATE_REQUEST_SEND_REQUEST
2457 * - SSL_CERTIFICATE_REQUEST_SKIP
2458 * indicating if the writing of the CertificateRequest
2459 * should be skipped or not.
2460 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002461MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002462static int ssl_tls13_certificate_request_coordinate(mbedtls_ssl_context *ssl)
XiaokangQiana987e1d2022-05-07 01:25:58 +00002463{
2464 int authmode;
2465
XiaokangQian40a35232022-05-07 09:02:40 +00002466#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Gilles Peskine449bd832023-01-11 14:50:10 +01002467 if (ssl->handshake->sni_authmode != MBEDTLS_SSL_VERIFY_UNSET) {
XiaokangQian40a35232022-05-07 09:02:40 +00002468 authmode = ssl->handshake->sni_authmode;
Gilles Peskine449bd832023-01-11 14:50:10 +01002469 } else
XiaokangQian40a35232022-05-07 09:02:40 +00002470#endif
XiaokangQiana987e1d2022-05-07 01:25:58 +00002471 authmode = ssl->conf->authmode;
2472
Gilles Peskine449bd832023-01-11 14:50:10 +01002473 if (authmode == MBEDTLS_SSL_VERIFY_NONE) {
Ronald Croneac00ad2022-09-13 10:16:31 +02002474 ssl->session_negotiate->verify_result = MBEDTLS_X509_BADCERT_SKIP_VERIFY;
Gilles Peskine449bd832023-01-11 14:50:10 +01002475 return SSL_CERTIFICATE_REQUEST_SKIP;
Ronald Croneac00ad2022-09-13 10:16:31 +02002476 }
XiaokangQiana987e1d2022-05-07 01:25:58 +00002477
XiaokangQianc3017f62022-05-13 05:55:41 +00002478 ssl->handshake->certificate_request_sent = 1;
XiaokangQian189ded22022-05-10 08:12:17 +00002479
Gilles Peskine449bd832023-01-11 14:50:10 +01002480 return SSL_CERTIFICATE_REQUEST_SEND_REQUEST;
XiaokangQiana987e1d2022-05-07 01:25:58 +00002481}
2482
XiaokangQiancec9ae62022-05-06 07:28:50 +00002483/*
2484 * struct {
2485 * opaque certificate_request_context<0..2^8-1>;
2486 * Extension extensions<2..2^16-1>;
2487 * } CertificateRequest;
2488 *
2489 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002490MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002491static int ssl_tls13_write_certificate_request_body(mbedtls_ssl_context *ssl,
2492 unsigned char *buf,
2493 const unsigned char *end,
2494 size_t *out_len)
XiaokangQiancec9ae62022-05-06 07:28:50 +00002495{
2496 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
2497 unsigned char *p = buf;
XiaokangQianec6efb92022-05-06 09:53:10 +00002498 size_t output_len = 0;
XiaokangQiancec9ae62022-05-06 07:28:50 +00002499 unsigned char *p_extensions_len;
2500
2501 *out_len = 0;
2502
2503 /* Check if we have enough space:
2504 * - certificate_request_context (1 byte)
2505 * - extensions length (2 bytes)
2506 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002507 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 3);
XiaokangQiancec9ae62022-05-06 07:28:50 +00002508
2509 /*
2510 * Write certificate_request_context
2511 */
2512 /*
2513 * We use a zero length context for the normal handshake
2514 * messages. For post-authentication handshake messages
2515 * this request context would be set to a non-zero value.
2516 */
2517 *p++ = 0x0;
2518
2519 /*
2520 * Write extensions
2521 */
2522 /* The extensions must contain the signature_algorithms. */
2523 p_extensions_len = p;
2524 p += 2;
Gilles Peskine449bd832023-01-11 14:50:10 +01002525 ret = mbedtls_ssl_write_sig_alg_ext(ssl, p, end, &output_len);
2526 if (ret != 0) {
2527 return ret;
2528 }
XiaokangQiancec9ae62022-05-06 07:28:50 +00002529
XiaokangQianec6efb92022-05-06 09:53:10 +00002530 p += output_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01002531 MBEDTLS_PUT_UINT16_BE(p - p_extensions_len - 2, p_extensions_len, 0);
XiaokangQiancec9ae62022-05-06 07:28:50 +00002532
2533 *out_len = p - buf;
2534
Jerry Yu7de2ff02022-11-08 21:43:46 +08002535 MBEDTLS_SSL_PRINT_EXTS(
Gilles Peskine449bd832023-01-11 14:50:10 +01002536 3, MBEDTLS_SSL_HS_CERTIFICATE_REQUEST, ssl->handshake->sent_extensions);
Jerry Yu4b8f2f72022-10-31 13:31:22 +08002537
Gilles Peskine449bd832023-01-11 14:50:10 +01002538 return 0;
XiaokangQiancec9ae62022-05-06 07:28:50 +00002539}
2540
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002541MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002542static int ssl_tls13_write_certificate_request(mbedtls_ssl_context *ssl)
XiaokangQiancec9ae62022-05-06 07:28:50 +00002543{
2544 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
2545
Gilles Peskine449bd832023-01-11 14:50:10 +01002546 MBEDTLS_SSL_DEBUG_MSG(2, ("=> write certificate request"));
XiaokangQiancec9ae62022-05-06 07:28:50 +00002547
Gilles Peskine449bd832023-01-11 14:50:10 +01002548 MBEDTLS_SSL_PROC_CHK_NEG(ssl_tls13_certificate_request_coordinate(ssl));
XiaokangQiancec9ae62022-05-06 07:28:50 +00002549
Gilles Peskine449bd832023-01-11 14:50:10 +01002550 if (ret == SSL_CERTIFICATE_REQUEST_SEND_REQUEST) {
XiaokangQiancec9ae62022-05-06 07:28:50 +00002551 unsigned char *buf;
2552 size_t buf_len, msg_len;
2553
Xiaokang Qian9f1747b2023-03-30 02:50:04 +00002554 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_start_handshake_msg(
2555 ssl, MBEDTLS_SSL_HS_CERTIFICATE_REQUEST,
2556 &buf, &buf_len));
XiaokangQiancec9ae62022-05-06 07:28:50 +00002557
Gilles Peskine449bd832023-01-11 14:50:10 +01002558 MBEDTLS_SSL_PROC_CHK(ssl_tls13_write_certificate_request_body(
2559 ssl, buf, buf + buf_len, &msg_len));
XiaokangQiancec9ae62022-05-06 07:28:50 +00002560
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +01002561 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_add_hs_msg_to_checksum(
Xiaokang Qian9f1747b2023-03-30 02:50:04 +00002562 ssl, MBEDTLS_SSL_HS_CERTIFICATE_REQUEST,
2563 buf, msg_len));
XiaokangQiancec9ae62022-05-06 07:28:50 +00002564
Gilles Peskine449bd832023-01-11 14:50:10 +01002565 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_finish_handshake_msg(
2566 ssl, buf_len, msg_len));
2567 } else if (ret == SSL_CERTIFICATE_REQUEST_SKIP) {
2568 MBEDTLS_SSL_DEBUG_MSG(2, ("<= skip write certificate request"));
XiaokangQiancec9ae62022-05-06 07:28:50 +00002569 ret = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01002570 } else {
2571 MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
XiaokangQiancec9ae62022-05-06 07:28:50 +00002572 ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
2573 goto cleanup;
2574 }
2575
Gilles Peskine449bd832023-01-11 14:50:10 +01002576 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_SERVER_CERTIFICATE);
XiaokangQiancec9ae62022-05-06 07:28:50 +00002577cleanup:
2578
Gilles Peskine449bd832023-01-11 14:50:10 +01002579 MBEDTLS_SSL_DEBUG_MSG(2, ("<= write certificate request"));
2580 return ret;
XiaokangQiancec9ae62022-05-06 07:28:50 +00002581}
XiaokangQiancec9ae62022-05-06 07:28:50 +00002582
Jerry Yu4d3841a2022-04-16 12:37:19 +08002583/*
Jerry Yuc6e6dbf2022-04-16 19:42:57 +08002584 * Handler for MBEDTLS_SSL_SERVER_CERTIFICATE
Jerry Yu83da34e2022-04-16 13:59:52 +08002585 */
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_server_certificate(mbedtls_ssl_context *ssl)
Jerry Yu83da34e2022-04-16 13:59:52 +08002588{
Jerry Yu5a26f302022-05-10 20:46:40 +08002589 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
XiaokangQianfb665a82022-06-15 03:57:21 +00002590
2591#if defined(MBEDTLS_X509_CRT_PARSE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01002592 if ((ssl_tls13_pick_key_cert(ssl) != 0) ||
2593 mbedtls_ssl_own_cert(ssl) == NULL) {
2594 MBEDTLS_SSL_DEBUG_MSG(2, ("No certificate available."));
2595 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE,
2596 MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE);
2597 return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
Jerry Yu5a26f302022-05-10 20:46:40 +08002598 }
XiaokangQianfb665a82022-06-15 03:57:21 +00002599#endif /* MBEDTLS_X509_CRT_PARSE_C */
Jerry Yu5a26f302022-05-10 20:46:40 +08002600
Gilles Peskine449bd832023-01-11 14:50:10 +01002601 ret = mbedtls_ssl_tls13_write_certificate(ssl);
2602 if (ret != 0) {
2603 return ret;
2604 }
2605 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_CERTIFICATE_VERIFY);
2606 return 0;
Jerry Yu83da34e2022-04-16 13:59:52 +08002607}
2608
2609/*
Jerry Yuc6e6dbf2022-04-16 19:42:57 +08002610 * Handler for MBEDTLS_SSL_CERTIFICATE_VERIFY
Jerry Yu83da34e2022-04-16 13:59:52 +08002611 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002612MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002613static int ssl_tls13_write_certificate_verify(mbedtls_ssl_context *ssl)
Jerry Yu83da34e2022-04-16 13:59:52 +08002614{
Gilles Peskine449bd832023-01-11 14:50:10 +01002615 int ret = mbedtls_ssl_tls13_write_certificate_verify(ssl);
2616 if (ret != 0) {
2617 return ret;
2618 }
2619 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_SERVER_FINISHED);
2620 return 0;
Jerry Yu83da34e2022-04-16 13:59:52 +08002621}
Ronald Cron928cbd32022-10-04 16:14:26 +02002622#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED */
Jerry Yu83da34e2022-04-16 13:59:52 +08002623
2624/*
Jerry Yud6e253d2022-05-18 13:59:24 +08002625 * Handler for MBEDTLS_SSL_SERVER_FINISHED
Jerry Yu69dd8d42022-04-16 12:51:26 +08002626 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002627MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002628static int ssl_tls13_write_server_finished(mbedtls_ssl_context *ssl)
Jerry Yu69dd8d42022-04-16 12:51:26 +08002629{
Jerry Yud6e253d2022-05-18 13:59:24 +08002630 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yu27bdc7c2022-04-16 13:33:27 +08002631
Gilles Peskine449bd832023-01-11 14:50:10 +01002632 ret = mbedtls_ssl_tls13_write_finished_message(ssl);
2633 if (ret != 0) {
2634 return ret;
2635 }
Jerry Yu27bdc7c2022-04-16 13:33:27 +08002636
Gilles Peskine449bd832023-01-11 14:50:10 +01002637 ret = mbedtls_ssl_tls13_compute_application_transform(ssl);
2638 if (ret != 0) {
Jerry Yue3d67cb2022-05-19 15:33:10 +08002639 MBEDTLS_SSL_PEND_FATAL_ALERT(
Gilles Peskine449bd832023-01-11 14:50:10 +01002640 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE,
2641 MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE);
2642 return ret;
Jerry Yue3d67cb2022-05-19 15:33:10 +08002643 }
XiaokangQianc3017f62022-05-13 05:55:41 +00002644
Gilles Peskine449bd832023-01-11 14:50:10 +01002645 MBEDTLS_SSL_DEBUG_MSG(1, ("Switch to handshake keys for inbound traffic"));
2646 mbedtls_ssl_set_inbound_transform(ssl, ssl->handshake->transform_handshake);
XiaokangQian189ded22022-05-10 08:12:17 +00002647
Gilles Peskine449bd832023-01-11 14:50:10 +01002648 if (ssl->handshake->certificate_request_sent) {
2649 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_CLIENT_CERTIFICATE);
2650 } else {
2651 MBEDTLS_SSL_DEBUG_MSG(2, ("skip parse certificate"));
2652 MBEDTLS_SSL_DEBUG_MSG(2, ("skip parse certificate verify"));
2653 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_CLIENT_FINISHED);
Ronald Cron19385882022-06-15 16:26:13 +02002654 }
Ronald Cron63dc4632022-05-31 14:41:53 +02002655
Gilles Peskine449bd832023-01-11 14:50:10 +01002656 return 0;
Jerry Yu69dd8d42022-04-16 12:51:26 +08002657}
2658
2659/*
Jerry Yud6e253d2022-05-18 13:59:24 +08002660 * Handler for MBEDTLS_SSL_CLIENT_FINISHED
Jerry Yu69dd8d42022-04-16 12:51:26 +08002661 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002662MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002663static int ssl_tls13_process_client_finished(mbedtls_ssl_context *ssl)
Jerry Yu69dd8d42022-04-16 12:51:26 +08002664{
Jerry Yud6e253d2022-05-18 13:59:24 +08002665 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
2666
Gilles Peskine449bd832023-01-11 14:50:10 +01002667 ret = mbedtls_ssl_tls13_process_finished_message(ssl);
2668 if (ret != 0) {
2669 return ret;
Jerry Yue3d67cb2022-05-19 15:33:10 +08002670 }
2671
Gilles Peskine449bd832023-01-11 14:50:10 +01002672 ret = mbedtls_ssl_tls13_compute_resumption_master_secret(ssl);
2673 if (ret != 0) {
Xiaokang Qian9f1747b2023-03-30 02:50:04 +00002674 MBEDTLS_SSL_DEBUG_RET(
2675 1, "mbedtls_ssl_tls13_compute_resumption_master_secret", ret);
Gilles Peskine449bd832023-01-11 14:50:10 +01002676 }
2677
2678 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_HANDSHAKE_WRAPUP);
2679 return 0;
Jerry Yu69dd8d42022-04-16 12:51:26 +08002680}
2681
2682/*
Jerry Yud6e253d2022-05-18 13:59:24 +08002683 * Handler for MBEDTLS_SSL_HANDSHAKE_WRAPUP
Jerry Yu69dd8d42022-04-16 12:51:26 +08002684 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002685MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002686static int ssl_tls13_handshake_wrapup(mbedtls_ssl_context *ssl)
Jerry Yu69dd8d42022-04-16 12:51:26 +08002687{
Gilles Peskine449bd832023-01-11 14:50:10 +01002688 MBEDTLS_SSL_DEBUG_MSG(2, ("handshake: done"));
Jerry Yu03ed50b2022-04-16 17:13:30 +08002689
Gilles Peskine449bd832023-01-11 14:50:10 +01002690 mbedtls_ssl_tls13_handshake_wrapup(ssl);
Jerry Yue67bef42022-07-07 07:29:42 +00002691
Pengyu Lv3643fdb2023-01-12 16:46:28 +08002692#if defined(MBEDTLS_SSL_SESSION_TICKETS) && \
2693 defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED)
Pengyu Lva1aa31b2022-12-13 13:49:59 +08002694/* TODO: Remove the check of SOME_PSK_ENABLED since SESSION_TICKETS requires
2695 * SOME_PSK_ENABLED to be enabled. Here is just to make CI happy. It is
2696 * expected to be resolved with issue#6395.
2697 */
Pengyu Lvc7af2c42022-12-01 16:33:00 +08002698 /* Sent NewSessionTicket message only when client supports PSK */
Pengyu Lv3643fdb2023-01-12 16:46:28 +08002699 if (mbedtls_ssl_tls13_some_psk_enabled(ssl)) {
Xiaokang Qian9f1747b2023-03-30 02:50:04 +00002700 mbedtls_ssl_handshake_set_state(
2701 ssl, MBEDTLS_SSL_TLS1_3_NEW_SESSION_TICKET);
Pengyu Lvc7af2c42022-12-01 16:33:00 +08002702 } else
Jerry Yufca4d572022-07-21 10:37:48 +08002703#endif
Pengyu Lv3643fdb2023-01-12 16:46:28 +08002704 {
2705 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_HANDSHAKE_OVER);
2706 }
Gilles Peskine449bd832023-01-11 14:50:10 +01002707 return 0;
Jerry Yu69dd8d42022-04-16 12:51:26 +08002708}
2709
2710/*
Jerry Yua8d3c502022-10-30 14:51:23 +08002711 * Handler for MBEDTLS_SSL_TLS1_3_NEW_SESSION_TICKET
Jerry Yue67bef42022-07-07 07:29:42 +00002712 */
2713#define SSL_NEW_SESSION_TICKET_SKIP 0
2714#define SSL_NEW_SESSION_TICKET_WRITE 1
2715MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002716static int ssl_tls13_write_new_session_ticket_coordinate(mbedtls_ssl_context *ssl)
Jerry Yue67bef42022-07-07 07:29:42 +00002717{
2718 /* Check whether the use of session tickets is enabled */
Gilles Peskine449bd832023-01-11 14:50:10 +01002719 if (ssl->conf->f_ticket_write == NULL) {
2720 MBEDTLS_SSL_DEBUG_MSG(2, ("NewSessionTicket: disabled,"
2721 " callback is not set"));
2722 return SSL_NEW_SESSION_TICKET_SKIP;
Jerry Yud0766ec2022-09-22 10:46:57 +08002723 }
Gilles Peskine449bd832023-01-11 14:50:10 +01002724 if (ssl->conf->new_session_tickets_count == 0) {
2725 MBEDTLS_SSL_DEBUG_MSG(2, ("NewSessionTicket: disabled,"
2726 " configured count is zero"));
2727 return SSL_NEW_SESSION_TICKET_SKIP;
Jerry Yud0766ec2022-09-22 10:46:57 +08002728 }
2729
Gilles Peskine449bd832023-01-11 14:50:10 +01002730 if (ssl->handshake->new_session_tickets_count == 0) {
2731 MBEDTLS_SSL_DEBUG_MSG(2, ("NewSessionTicket: all tickets have "
2732 "been sent."));
2733 return SSL_NEW_SESSION_TICKET_SKIP;
Jerry Yue67bef42022-07-07 07:29:42 +00002734 }
2735
Gilles Peskine449bd832023-01-11 14:50:10 +01002736 return SSL_NEW_SESSION_TICKET_WRITE;
Jerry Yue67bef42022-07-07 07:29:42 +00002737}
2738
2739#if defined(MBEDTLS_SSL_SESSION_TICKETS)
2740MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002741static int ssl_tls13_prepare_new_session_ticket(mbedtls_ssl_context *ssl,
2742 unsigned char *ticket_nonce,
2743 size_t ticket_nonce_size)
Jerry Yue67bef42022-07-07 07:29:42 +00002744{
2745 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
2746 mbedtls_ssl_session *session = ssl->session;
2747 mbedtls_ssl_ciphersuite_t *ciphersuite_info;
2748 psa_algorithm_t psa_hash_alg;
2749 int hash_length;
2750
Gilles Peskine449bd832023-01-11 14:50:10 +01002751 MBEDTLS_SSL_DEBUG_MSG(2, ("=> prepare NewSessionTicket msg"));
Jerry Yue67bef42022-07-07 07:29:42 +00002752
2753#if defined(MBEDTLS_HAVE_TIME)
Gilles Peskine449bd832023-01-11 14:50:10 +01002754 session->start = mbedtls_time(NULL);
Jerry Yue67bef42022-07-07 07:29:42 +00002755#endif
2756
Pengyu Lv9f926952022-11-17 15:22:33 +08002757 /* Set ticket_flags depends on the advertised psk key exchange mode */
Pengyu Lv80270b22023-01-12 11:54:04 +08002758 mbedtls_ssl_session_clear_ticket_flags(
Pengyu Lv9eacb442022-12-09 14:39:19 +08002759 session, MBEDTLS_SSL_TLS1_3_TICKET_FLAGS_MASK);
Pengyu Lve6487fe2022-12-06 09:30:29 +08002760#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED)
Pengyu Lv80270b22023-01-12 11:54:04 +08002761 mbedtls_ssl_session_set_ticket_flags(
Pengyu Lv9eacb442022-12-09 14:39:19 +08002762 session, ssl->handshake->tls13_kex_modes);
Pengyu Lve6487fe2022-12-06 09:30:29 +08002763#endif
Pengyu Lv4938a562023-01-16 11:28:49 +08002764 MBEDTLS_SSL_PRINT_TICKET_FLAGS(4, session->ticket_flags);
Pengyu Lv9f926952022-11-17 15:22:33 +08002765
Jerry Yue67bef42022-07-07 07:29:42 +00002766 /* Generate ticket_age_add */
Gilles Peskine449bd832023-01-11 14:50:10 +01002767 if ((ret = ssl->conf->f_rng(ssl->conf->p_rng,
2768 (unsigned char *) &session->ticket_age_add,
2769 sizeof(session->ticket_age_add)) != 0)) {
2770 MBEDTLS_SSL_DEBUG_RET(1, "generate_ticket_age_add", ret);
2771 return ret;
Jerry Yue67bef42022-07-07 07:29:42 +00002772 }
Gilles Peskine449bd832023-01-11 14:50:10 +01002773 MBEDTLS_SSL_DEBUG_MSG(3, ("ticket_age_add: %u",
2774 (unsigned int) session->ticket_age_add));
Jerry Yue67bef42022-07-07 07:29:42 +00002775
2776 /* Generate ticket_nonce */
Gilles Peskine449bd832023-01-11 14:50:10 +01002777 ret = ssl->conf->f_rng(ssl->conf->p_rng, ticket_nonce, ticket_nonce_size);
2778 if (ret != 0) {
2779 MBEDTLS_SSL_DEBUG_RET(1, "generate_ticket_nonce", ret);
2780 return ret;
Jerry Yue67bef42022-07-07 07:29:42 +00002781 }
Gilles Peskine449bd832023-01-11 14:50:10 +01002782 MBEDTLS_SSL_DEBUG_BUF(3, "ticket_nonce:",
2783 ticket_nonce, ticket_nonce_size);
Jerry Yue67bef42022-07-07 07:29:42 +00002784
2785 ciphersuite_info =
Gilles Peskine449bd832023-01-11 14:50:10 +01002786 (mbedtls_ssl_ciphersuite_t *) ssl->handshake->ciphersuite_info;
Manuel Pégourié-Gonnard1f2a5872023-03-28 11:46:17 +02002787 psa_hash_alg = mbedtls_md_psa_alg_from_type(ciphersuite_info->mac);
Gilles Peskine449bd832023-01-11 14:50:10 +01002788 hash_length = PSA_HASH_LENGTH(psa_hash_alg);
2789 if (hash_length == -1 ||
2790 (size_t) hash_length > sizeof(session->resumption_key)) {
2791 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Jerry Yufca4d572022-07-21 10:37:48 +08002792 }
2793
Jerry Yue67bef42022-07-07 07:29:42 +00002794 /* In this code the psk key length equals the length of the hash */
2795 session->resumption_key_len = hash_length;
2796 session->ciphersuite = ciphersuite_info->id;
2797
2798 /* Compute resumption key
2799 *
2800 * HKDF-Expand-Label( resumption_master_secret,
2801 * "resumption", ticket_nonce, Hash.length )
2802 */
2803 ret = mbedtls_ssl_tls13_hkdf_expand_label(
Gilles Peskine449bd832023-01-11 14:50:10 +01002804 psa_hash_alg,
2805 session->app_secrets.resumption_master_secret,
2806 hash_length,
2807 MBEDTLS_SSL_TLS1_3_LBL_WITH_LEN(resumption),
2808 ticket_nonce,
2809 ticket_nonce_size,
2810 session->resumption_key,
2811 hash_length);
Jerry Yue67bef42022-07-07 07:29:42 +00002812
Gilles Peskine449bd832023-01-11 14:50:10 +01002813 if (ret != 0) {
2814 MBEDTLS_SSL_DEBUG_RET(2,
2815 "Creating the ticket-resumed PSK failed",
2816 ret);
2817 return ret;
Jerry Yue67bef42022-07-07 07:29:42 +00002818 }
Gilles Peskine449bd832023-01-11 14:50:10 +01002819 MBEDTLS_SSL_DEBUG_BUF(3, "Ticket-resumed PSK",
2820 session->resumption_key,
2821 session->resumption_key_len);
Jerry Yue67bef42022-07-07 07:29:42 +00002822
Gilles Peskine449bd832023-01-11 14:50:10 +01002823 MBEDTLS_SSL_DEBUG_BUF(3, "resumption_master_secret",
2824 session->app_secrets.resumption_master_secret,
2825 hash_length);
Jerry Yue67bef42022-07-07 07:29:42 +00002826
Gilles Peskine449bd832023-01-11 14:50:10 +01002827 return 0;
Jerry Yue67bef42022-07-07 07:29:42 +00002828}
2829
2830/* This function creates a NewSessionTicket message in the following format:
2831 *
2832 * struct {
2833 * uint32 ticket_lifetime;
2834 * uint32 ticket_age_add;
2835 * opaque ticket_nonce<0..255>;
2836 * opaque ticket<1..2^16-1>;
2837 * Extension extensions<0..2^16-2>;
2838 * } NewSessionTicket;
2839 *
2840 * The ticket inside the NewSessionTicket message is an encrypted container
2841 * carrying the necessary information so that the server is later able to
2842 * re-start the communication.
2843 *
2844 * The following fields are placed inside the ticket by the
2845 * f_ticket_write() function:
2846 *
2847 * - creation time (start)
2848 * - flags (flags)
2849 * - age add (ticket_age_add)
2850 * - key (key)
2851 * - key length (key_len)
2852 * - ciphersuite (ciphersuite)
2853 */
2854MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002855static int ssl_tls13_write_new_session_ticket_body(mbedtls_ssl_context *ssl,
2856 unsigned char *buf,
2857 unsigned char *end,
2858 size_t *out_len,
2859 unsigned char *ticket_nonce,
2860 size_t ticket_nonce_size)
Jerry Yue67bef42022-07-07 07:29:42 +00002861{
2862 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
2863 unsigned char *p = buf;
2864 mbedtls_ssl_session *session = ssl->session;
2865 size_t ticket_len;
2866 uint32_t ticket_lifetime;
2867
2868 *out_len = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01002869 MBEDTLS_SSL_DEBUG_MSG(2, ("=> write NewSessionTicket msg"));
Jerry Yue67bef42022-07-07 07:29:42 +00002870
2871 /*
2872 * ticket_lifetime 4 bytes
2873 * ticket_age_add 4 bytes
2874 * ticket_nonce 1 + ticket_nonce_size bytes
2875 * ticket >=2 bytes
2876 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002877 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 4 + 4 + 1 + ticket_nonce_size + 2);
Jerry Yue67bef42022-07-07 07:29:42 +00002878
2879 /* Generate ticket and ticket_lifetime */
Gilles Peskine449bd832023-01-11 14:50:10 +01002880 ret = ssl->conf->f_ticket_write(ssl->conf->p_ticket,
2881 session,
2882 p + 9 + ticket_nonce_size + 2,
2883 end,
2884 &ticket_len,
2885 &ticket_lifetime);
2886 if (ret != 0) {
2887 MBEDTLS_SSL_DEBUG_RET(1, "write_ticket", ret);
2888 return ret;
Jerry Yue67bef42022-07-07 07:29:42 +00002889 }
2890 /* RFC 8446 4.6.1
2891 * ticket_lifetime: Indicates the lifetime in seconds as a 32-bit
2892 * unsigned integer in network byte order from the time of ticket
2893 * issuance. Servers MUST NOT use any value greater than
2894 * 604800 seconds (7 days). The value of zero indicates that the
2895 * ticket should be discarded immediately. Clients MUST NOT cache
2896 * tickets for longer than 7 days, regardless of the ticket_lifetime,
2897 * and MAY delete tickets earlier based on local policy. A server
2898 * MAY treat a ticket as valid for a shorter period of time than what
2899 * is stated in the ticket_lifetime.
2900 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002901 if (ticket_lifetime > 604800) {
Xiaokang Qian28af5012022-10-13 08:18:19 +00002902 ticket_lifetime = 604800;
Gilles Peskine449bd832023-01-11 14:50:10 +01002903 }
2904 MBEDTLS_PUT_UINT32_BE(ticket_lifetime, p, 0);
2905 MBEDTLS_SSL_DEBUG_MSG(3, ("ticket_lifetime: %u",
2906 (unsigned int) ticket_lifetime));
Jerry Yue67bef42022-07-07 07:29:42 +00002907
2908 /* Write ticket_age_add */
Gilles Peskine449bd832023-01-11 14:50:10 +01002909 MBEDTLS_PUT_UINT32_BE(session->ticket_age_add, p, 4);
2910 MBEDTLS_SSL_DEBUG_MSG(3, ("ticket_age_add: %u",
2911 (unsigned int) session->ticket_age_add));
Jerry Yue67bef42022-07-07 07:29:42 +00002912
2913 /* Write ticket_nonce */
Gilles Peskine449bd832023-01-11 14:50:10 +01002914 p[8] = (unsigned char) ticket_nonce_size;
2915 if (ticket_nonce_size > 0) {
2916 memcpy(p + 9, ticket_nonce, ticket_nonce_size);
Jerry Yue67bef42022-07-07 07:29:42 +00002917 }
2918 p += 9 + ticket_nonce_size;
2919
2920 /* Write ticket */
Gilles Peskine449bd832023-01-11 14:50:10 +01002921 MBEDTLS_PUT_UINT16_BE(ticket_len, p, 0);
Jerry Yue67bef42022-07-07 07:29:42 +00002922 p += 2;
Gilles Peskine449bd832023-01-11 14:50:10 +01002923 MBEDTLS_SSL_DEBUG_BUF(4, "ticket", p, ticket_len);
Jerry Yue67bef42022-07-07 07:29:42 +00002924 p += ticket_len;
2925
2926 /* Ticket Extensions
2927 *
2928 * Note: We currently don't have any extensions.
2929 * Set length to zero.
2930 */
Jerry Yuedab6372022-10-31 14:37:31 +08002931 ssl->handshake->sent_extensions = MBEDTLS_SSL_EXT_MASK_NONE;
2932
Gilles Peskine449bd832023-01-11 14:50:10 +01002933 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 2);
2934 MBEDTLS_PUT_UINT16_BE(0, p, 0);
Jerry Yue67bef42022-07-07 07:29:42 +00002935 p += 2;
2936
2937 *out_len = p - buf;
Gilles Peskine449bd832023-01-11 14:50:10 +01002938 MBEDTLS_SSL_DEBUG_BUF(4, "ticket", buf, *out_len);
2939 MBEDTLS_SSL_DEBUG_MSG(2, ("<= write new session ticket"));
Jerry Yue67bef42022-07-07 07:29:42 +00002940
Jerry Yu7de2ff02022-11-08 21:43:46 +08002941 MBEDTLS_SSL_PRINT_EXTS(
Gilles Peskine449bd832023-01-11 14:50:10 +01002942 3, MBEDTLS_SSL_HS_NEW_SESSION_TICKET, ssl->handshake->sent_extensions);
Jerry Yu4b8f2f72022-10-31 13:31:22 +08002943
Gilles Peskine449bd832023-01-11 14:50:10 +01002944 return 0;
Jerry Yue67bef42022-07-07 07:29:42 +00002945}
2946
2947/*
Jerry Yua8d3c502022-10-30 14:51:23 +08002948 * Handler for MBEDTLS_SSL_TLS1_3_NEW_SESSION_TICKET
Jerry Yue67bef42022-07-07 07:29:42 +00002949 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002950static int ssl_tls13_write_new_session_ticket(mbedtls_ssl_context *ssl)
Jerry Yue67bef42022-07-07 07:29:42 +00002951{
2952 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
2953
Gilles Peskine449bd832023-01-11 14:50:10 +01002954 MBEDTLS_SSL_PROC_CHK_NEG(ssl_tls13_write_new_session_ticket_coordinate(ssl));
Jerry Yue67bef42022-07-07 07:29:42 +00002955
Gilles Peskine449bd832023-01-11 14:50:10 +01002956 if (ret == SSL_NEW_SESSION_TICKET_WRITE) {
Jerry Yue67bef42022-07-07 07:29:42 +00002957 unsigned char ticket_nonce[MBEDTLS_SSL_TLS1_3_TICKET_NONCE_LENGTH];
2958 unsigned char *buf;
2959 size_t buf_len, msg_len;
2960
Gilles Peskine449bd832023-01-11 14:50:10 +01002961 MBEDTLS_SSL_PROC_CHK(ssl_tls13_prepare_new_session_ticket(
2962 ssl, ticket_nonce, sizeof(ticket_nonce)));
Jerry Yue67bef42022-07-07 07:29:42 +00002963
Xiaokang Qian9f1747b2023-03-30 02:50:04 +00002964 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_start_handshake_msg(
2965 ssl, MBEDTLS_SSL_HS_NEW_SESSION_TICKET,
2966 &buf, &buf_len));
Jerry Yue67bef42022-07-07 07:29:42 +00002967
Gilles Peskine449bd832023-01-11 14:50:10 +01002968 MBEDTLS_SSL_PROC_CHK(ssl_tls13_write_new_session_ticket_body(
2969 ssl, buf, buf + buf_len, &msg_len,
2970 ticket_nonce, sizeof(ticket_nonce)));
Jerry Yue67bef42022-07-07 07:29:42 +00002971
Gilles Peskine449bd832023-01-11 14:50:10 +01002972 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_finish_handshake_msg(
2973 ssl, buf_len, msg_len));
Jerry Yufca4d572022-07-21 10:37:48 +08002974
Jerry Yu359e65f2022-09-22 23:47:43 +08002975 /* Limit session tickets count to one when resumption connection.
2976 *
2977 * See document of mbedtls_ssl_conf_new_session_tickets.
2978 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002979 if (ssl->handshake->resume == 1) {
Jerry Yu359e65f2022-09-22 23:47:43 +08002980 ssl->handshake->new_session_tickets_count = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01002981 } else {
Jerry Yu359e65f2022-09-22 23:47:43 +08002982 ssl->handshake->new_session_tickets_count--;
Gilles Peskine449bd832023-01-11 14:50:10 +01002983 }
Jerry Yub7e3fa72022-09-22 11:07:18 +08002984
Jerry Yua8d3c502022-10-30 14:51:23 +08002985 mbedtls_ssl_handshake_set_state(
Gilles Peskine449bd832023-01-11 14:50:10 +01002986 ssl, MBEDTLS_SSL_TLS1_3_NEW_SESSION_TICKET_FLUSH);
2987 } else {
2988 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_HANDSHAKE_OVER);
Jerry Yue67bef42022-07-07 07:29:42 +00002989 }
2990
Jerry Yue67bef42022-07-07 07:29:42 +00002991cleanup:
2992
Gilles Peskine449bd832023-01-11 14:50:10 +01002993 return ret;
Jerry Yue67bef42022-07-07 07:29:42 +00002994}
2995#endif /* MBEDTLS_SSL_SESSION_TICKETS */
2996
2997/*
XiaokangQiane8ff3502022-04-22 02:34:40 +00002998 * TLS 1.3 State Machine -- server side
XiaokangQian7807f9f2022-02-15 10:04:37 +00002999 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003000int mbedtls_ssl_tls13_handshake_server_step(mbedtls_ssl_context *ssl)
Jerry Yub9930e72021-08-06 17:11:51 +08003001{
XiaokangQian08037552022-04-20 07:16:41 +00003002 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
XiaokangQian7807f9f2022-02-15 10:04:37 +00003003
Gilles Peskine449bd832023-01-11 14:50:10 +01003004 if (ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER || ssl->handshake == NULL) {
3005 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
3006 }
XiaokangQian7807f9f2022-02-15 10:04:37 +00003007
Gilles Peskine449bd832023-01-11 14:50:10 +01003008 MBEDTLS_SSL_DEBUG_MSG(2, ("tls13 server state: %s(%d)",
3009 mbedtls_ssl_states_str(ssl->state),
3010 ssl->state));
Jerry Yu6e81b272021-09-27 11:16:17 +08003011
Gilles Peskine449bd832023-01-11 14:50:10 +01003012 switch (ssl->state) {
XiaokangQian7807f9f2022-02-15 10:04:37 +00003013 /* start state */
3014 case MBEDTLS_SSL_HELLO_REQUEST:
Gilles Peskine449bd832023-01-11 14:50:10 +01003015 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_CLIENT_HELLO);
XiaokangQian08037552022-04-20 07:16:41 +00003016 ret = 0;
XiaokangQian7807f9f2022-02-15 10:04:37 +00003017 break;
3018
XiaokangQian7807f9f2022-02-15 10:04:37 +00003019 case MBEDTLS_SSL_CLIENT_HELLO:
Gilles Peskine449bd832023-01-11 14:50:10 +01003020 ret = ssl_tls13_process_client_hello(ssl);
3021 if (ret != 0) {
3022 MBEDTLS_SSL_DEBUG_RET(1, "ssl_tls13_process_client_hello", ret);
3023 }
Jerry Yuf41553b2022-05-09 22:20:30 +08003024 break;
XiaokangQian7807f9f2022-02-15 10:04:37 +00003025
Jerry Yuf41553b2022-05-09 22:20:30 +08003026 case MBEDTLS_SSL_HELLO_RETRY_REQUEST:
Gilles Peskine449bd832023-01-11 14:50:10 +01003027 ret = ssl_tls13_write_hello_retry_request(ssl);
3028 if (ret != 0) {
3029 MBEDTLS_SSL_DEBUG_RET(1, "ssl_tls13_write_hello_retry_request", ret);
3030 return ret;
Jerry Yuf41553b2022-05-09 22:20:30 +08003031 }
XiaokangQian7807f9f2022-02-15 10:04:37 +00003032 break;
3033
Jerry Yu5b64ae92022-03-30 17:15:02 +08003034 case MBEDTLS_SSL_SERVER_HELLO:
Gilles Peskine449bd832023-01-11 14:50:10 +01003035 ret = ssl_tls13_write_server_hello(ssl);
Jerry Yu5b64ae92022-03-30 17:15:02 +08003036 break;
3037
Xiaofei Baicba64af2022-02-15 10:00:56 +00003038 case MBEDTLS_SSL_ENCRYPTED_EXTENSIONS:
Gilles Peskine449bd832023-01-11 14:50:10 +01003039 ret = ssl_tls13_write_encrypted_extensions(ssl);
3040 if (ret != 0) {
3041 MBEDTLS_SSL_DEBUG_RET(1, "ssl_tls13_write_encrypted_extensions", ret);
3042 return ret;
Xiaofei Baicba64af2022-02-15 10:00:56 +00003043 }
Jerry Yu48330562022-05-06 21:35:44 +08003044 break;
Jerry Yu6a2cd9e2022-05-05 11:14:19 +08003045
Ronald Cron928cbd32022-10-04 16:14:26 +02003046#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED)
Xiaofei Bai9ca09d42022-02-14 12:57:18 +00003047 case MBEDTLS_SSL_CERTIFICATE_REQUEST:
Gilles Peskine449bd832023-01-11 14:50:10 +01003048 ret = ssl_tls13_write_certificate_request(ssl);
Xiaofei Bai9ca09d42022-02-14 12:57:18 +00003049 break;
Xiaofei Bai9ca09d42022-02-14 12:57:18 +00003050
Jerry Yu83da34e2022-04-16 13:59:52 +08003051 case MBEDTLS_SSL_SERVER_CERTIFICATE:
Gilles Peskine449bd832023-01-11 14:50:10 +01003052 ret = ssl_tls13_write_server_certificate(ssl);
Jerry Yu83da34e2022-04-16 13:59:52 +08003053 break;
3054
3055 case MBEDTLS_SSL_CERTIFICATE_VERIFY:
Gilles Peskine449bd832023-01-11 14:50:10 +01003056 ret = ssl_tls13_write_certificate_verify(ssl);
Jerry Yu83da34e2022-04-16 13:59:52 +08003057 break;
Ronald Cron928cbd32022-10-04 16:14:26 +02003058#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED */
Jerry Yu83da34e2022-04-16 13:59:52 +08003059
Gilles Peskine449bd832023-01-11 14:50:10 +01003060 /*
3061 * Injection of dummy-CCS's for middlebox compatibility
3062 */
Gabor Mezei7b39bf12022-05-24 16:04:14 +02003063#if defined(MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE)
Gabor Mezeif7044ea2022-06-28 16:01:49 +02003064 case MBEDTLS_SSL_SERVER_CCS_AFTER_HELLO_RETRY_REQUEST:
Gilles Peskine449bd832023-01-11 14:50:10 +01003065 ret = mbedtls_ssl_tls13_write_change_cipher_spec(ssl);
3066 if (ret == 0) {
3067 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_CLIENT_HELLO);
3068 }
Gabor Mezei7b39bf12022-05-24 16:04:14 +02003069 break;
3070
3071 case MBEDTLS_SSL_SERVER_CCS_AFTER_SERVER_HELLO:
Gilles Peskine449bd832023-01-11 14:50:10 +01003072 ret = mbedtls_ssl_tls13_write_change_cipher_spec(ssl);
3073 if (ret == 0) {
3074 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_ENCRYPTED_EXTENSIONS);
3075 }
Gabor Mezei7b39bf12022-05-24 16:04:14 +02003076 break;
3077#endif /* MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE */
3078
Jerry Yu69dd8d42022-04-16 12:51:26 +08003079 case MBEDTLS_SSL_SERVER_FINISHED:
Gilles Peskine449bd832023-01-11 14:50:10 +01003080 ret = ssl_tls13_write_server_finished(ssl);
Jerry Yu69dd8d42022-04-16 12:51:26 +08003081 break;
3082
3083 case MBEDTLS_SSL_CLIENT_FINISHED:
Gilles Peskine449bd832023-01-11 14:50:10 +01003084 ret = ssl_tls13_process_client_finished(ssl);
Jerry Yu69dd8d42022-04-16 12:51:26 +08003085 break;
3086
Jerry Yu69dd8d42022-04-16 12:51:26 +08003087 case MBEDTLS_SSL_HANDSHAKE_WRAPUP:
Gilles Peskine449bd832023-01-11 14:50:10 +01003088 ret = ssl_tls13_handshake_wrapup(ssl);
Jerry Yu69dd8d42022-04-16 12:51:26 +08003089 break;
3090
Ronald Cron766c0cd2022-10-18 12:17:11 +02003091#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED)
XiaokangQian6b916b12022-04-25 07:29:34 +00003092 case MBEDTLS_SSL_CLIENT_CERTIFICATE:
Gilles Peskine449bd832023-01-11 14:50:10 +01003093 ret = mbedtls_ssl_tls13_process_certificate(ssl);
3094 if (ret == 0) {
3095 if (ssl->session_negotiate->peer_cert != NULL) {
Ronald Cron209cae92022-06-07 10:30:19 +02003096 mbedtls_ssl_handshake_set_state(
Gilles Peskine449bd832023-01-11 14:50:10 +01003097 ssl, MBEDTLS_SSL_CLIENT_CERTIFICATE_VERIFY);
3098 } else {
3099 MBEDTLS_SSL_DEBUG_MSG(2, ("skip parse certificate verify"));
Ronald Cron209cae92022-06-07 10:30:19 +02003100 mbedtls_ssl_handshake_set_state(
Gilles Peskine449bd832023-01-11 14:50:10 +01003101 ssl, MBEDTLS_SSL_CLIENT_FINISHED);
Ronald Cron19385882022-06-15 16:26:13 +02003102 }
XiaokangQian6b916b12022-04-25 07:29:34 +00003103 }
3104 break;
3105
3106 case MBEDTLS_SSL_CLIENT_CERTIFICATE_VERIFY:
Gilles Peskine449bd832023-01-11 14:50:10 +01003107 ret = mbedtls_ssl_tls13_process_certificate_verify(ssl);
3108 if (ret == 0) {
XiaokangQian6b916b12022-04-25 07:29:34 +00003109 mbedtls_ssl_handshake_set_state(
Gilles Peskine449bd832023-01-11 14:50:10 +01003110 ssl, MBEDTLS_SSL_CLIENT_FINISHED);
XiaokangQian6b916b12022-04-25 07:29:34 +00003111 }
3112 break;
Ronald Cron766c0cd2022-10-18 12:17:11 +02003113#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED */
XiaokangQian6b916b12022-04-25 07:29:34 +00003114
Jerry Yue67bef42022-07-07 07:29:42 +00003115#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Jerry Yua8d3c502022-10-30 14:51:23 +08003116 case MBEDTLS_SSL_TLS1_3_NEW_SESSION_TICKET:
Gilles Peskine449bd832023-01-11 14:50:10 +01003117 ret = ssl_tls13_write_new_session_ticket(ssl);
3118 if (ret != 0) {
3119 MBEDTLS_SSL_DEBUG_RET(1,
3120 "ssl_tls13_write_new_session_ticket ",
3121 ret);
Jerry Yue67bef42022-07-07 07:29:42 +00003122 }
3123 break;
Jerry Yua8d3c502022-10-30 14:51:23 +08003124 case MBEDTLS_SSL_TLS1_3_NEW_SESSION_TICKET_FLUSH:
Jerry Yue67bef42022-07-07 07:29:42 +00003125 /* This state is necessary to do the flush of the New Session
Jerry Yua8d3c502022-10-30 14:51:23 +08003126 * Ticket message written in MBEDTLS_SSL_TLS1_3_NEW_SESSION_TICKET
Jerry Yue67bef42022-07-07 07:29:42 +00003127 * as part of ssl_prepare_handshake_step.
3128 */
3129 ret = 0;
Jerry Yud4e75002022-08-09 13:33:50 +08003130
Gilles Peskine449bd832023-01-11 14:50:10 +01003131 if (ssl->handshake->new_session_tickets_count == 0) {
3132 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_HANDSHAKE_OVER);
3133 } else {
Xiaokang Qian9f1747b2023-03-30 02:50:04 +00003134 mbedtls_ssl_handshake_set_state(
3135 ssl, MBEDTLS_SSL_TLS1_3_NEW_SESSION_TICKET);
Gilles Peskine449bd832023-01-11 14:50:10 +01003136 }
Jerry Yue67bef42022-07-07 07:29:42 +00003137 break;
3138
3139#endif /* MBEDTLS_SSL_SESSION_TICKETS */
3140
XiaokangQian7807f9f2022-02-15 10:04:37 +00003141 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01003142 MBEDTLS_SSL_DEBUG_MSG(1, ("invalid state %d", ssl->state));
3143 return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
XiaokangQian7807f9f2022-02-15 10:04:37 +00003144 }
3145
Gilles Peskine449bd832023-01-11 14:50:10 +01003146 return ret;
Jerry Yub9930e72021-08-06 17:11:51 +08003147}
Jerry Yu3cc4c2a2021-08-06 16:29:08 +08003148
Jerry Yufb4b6472022-01-27 15:03:26 +08003149#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_PROTO_TLS1_3 */