blob: 954ddffb2da333a4fe320f72c32f52128a0c898c [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
Dave Rodgman16799db2023-11-02 19:47:20 +00005 * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
Gilles Peskine449bd832023-01-11 14:50:10 +01006 */
Jerry Yu3cc4c2a2021-08-06 16:29:08 +08007
8#include "common.h"
9
Jerry Yufb4b6472022-01-27 15:03:26 +080010#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_PROTO_TLS1_3)
Jerry Yu3cc4c2a2021-08-06 16:29:08 +080011
Jerry Yu687101b2021-09-14 16:03:56 +080012#include "mbedtls/debug.h"
Xiaofei Baicba64af2022-02-15 10:00:56 +000013#include "mbedtls/error.h"
14#include "mbedtls/platform.h"
Jerry Yu1c105562022-07-10 06:32:38 +000015#include "mbedtls/constant_time.h"
Manuel Pégourié-Gonnardd55d66f2023-06-20 10:14:58 +020016#include "mbedtls/oid.h"
Manuel Pégourié-Gonnard02b10d82023-03-28 12:33:20 +020017#include "md_psa.h"
Jerry Yu3bf2c642022-03-30 22:02:12 +080018
Xiaofei Bai9ca09d42022-02-14 12:57:18 +000019#include "ssl_misc.h"
20#include "ssl_tls13_keys.h"
21#include "ssl_debug_helpers.h"
22
Jerry Yuf35ba382022-08-23 17:58:26 +080023
Jerry Yuc5a23a02022-08-25 10:51:44 +080024static const mbedtls_ssl_ciphersuite_t *ssl_tls13_validate_peer_ciphersuite(
Gilles Peskine449bd832023-01-11 14:50:10 +010025 mbedtls_ssl_context *ssl,
26 unsigned int cipher_suite)
Jerry Yuf35ba382022-08-23 17:58:26 +080027{
28 const mbedtls_ssl_ciphersuite_t *ciphersuite_info;
Gilles Peskine449bd832023-01-11 14:50:10 +010029 if (!mbedtls_ssl_tls13_cipher_suite_is_offered(ssl, cipher_suite)) {
30 return NULL;
Jerry Yuf35ba382022-08-23 17:58:26 +080031 }
Gilles Peskine449bd832023-01-11 14:50:10 +010032
33 ciphersuite_info = mbedtls_ssl_ciphersuite_from_id(cipher_suite);
34 if ((mbedtls_ssl_validate_ciphersuite(ssl, ciphersuite_info,
35 ssl->tls_version,
36 ssl->tls_version) != 0)) {
37 return NULL;
38 }
39 return ciphersuite_info;
Jerry Yuf35ba382022-08-23 17:58:26 +080040}
41
Ronald Cron41a443a2022-10-04 16:38:25 +020042#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED)
Jerry Yue19e3b92022-07-08 12:04:51 +000043/* From RFC 8446:
44 *
45 * enum { psk_ke(0), psk_dhe_ke(1), (255) } PskKeyExchangeMode;
46 * struct {
47 * PskKeyExchangeMode ke_modes<1..255>;
48 * } PskKeyExchangeModes;
49 */
Jerry Yu6e74a7e2022-07-20 20:49:32 +080050MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +010051static int ssl_tls13_parse_key_exchange_modes_ext(mbedtls_ssl_context *ssl,
52 const unsigned char *buf,
53 const unsigned char *end)
Jerry Yue19e3b92022-07-08 12:04:51 +000054{
Jerry Yu299e31f2022-07-13 23:06:36 +080055 const unsigned char *p = buf;
Jerry Yue19e3b92022-07-08 12:04:51 +000056 size_t ke_modes_len;
57 int ke_modes = 0;
58
Jerry Yu854dd9e2022-07-15 14:28:27 +080059 /* Read ke_modes length (1 Byte) */
Gilles Peskine449bd832023-01-11 14:50:10 +010060 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, 1);
Jerry Yu299e31f2022-07-13 23:06:36 +080061 ke_modes_len = *p++;
Jerry Yue19e3b92022-07-08 12:04:51 +000062 /* Currently, there are only two PSK modes, so even without looking
63 * at the content, something's wrong if the list has more than 2 items. */
Gilles Peskine449bd832023-01-11 14:50:10 +010064 if (ke_modes_len > 2) {
65 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER,
66 MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER);
67 return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
Jerry Yu299e31f2022-07-13 23:06:36 +080068 }
Jerry Yue19e3b92022-07-08 12:04:51 +000069
Gilles Peskine449bd832023-01-11 14:50:10 +010070 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, ke_modes_len);
Jerry Yue19e3b92022-07-08 12:04:51 +000071
Gilles Peskine449bd832023-01-11 14:50:10 +010072 while (ke_modes_len-- != 0) {
73 switch (*p++) {
74 case MBEDTLS_SSL_TLS1_3_PSK_MODE_PURE:
75 ke_modes |= MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK;
76 MBEDTLS_SSL_DEBUG_MSG(3, ("Found PSK KEX MODE"));
77 break;
78 case MBEDTLS_SSL_TLS1_3_PSK_MODE_ECDHE:
79 ke_modes |= MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK_EPHEMERAL;
80 MBEDTLS_SSL_DEBUG_MSG(3, ("Found PSK_EPHEMERAL KEX MODE"));
81 break;
82 default:
83 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER,
84 MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER);
85 return MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER;
Jerry Yue19e3b92022-07-08 12:04:51 +000086 }
87 }
88
89 ssl->handshake->tls13_kex_modes = ke_modes;
Gilles Peskine449bd832023-01-11 14:50:10 +010090 return 0;
Jerry Yue19e3b92022-07-08 12:04:51 +000091}
Jerry Yu1c105562022-07-10 06:32:38 +000092
Jerry Yue9d4fc02022-08-20 19:21:15 +080093#define SSL_TLS1_3_OFFERED_PSK_NOT_MATCH 1
94#define SSL_TLS1_3_OFFERED_PSK_MATCH 0
Jerry Yu95699e72022-08-21 19:22:23 +080095
96#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Pengyu Lv29daf4a2023-10-30 17:13:30 +080097MBEDTLS_CHECK_RETURN_CRITICAL
98static int ssl_tls13_check_psk_key_exchange(mbedtls_ssl_context *ssl);
99MBEDTLS_CHECK_RETURN_CRITICAL
100static int ssl_tls13_check_psk_ephemeral_key_exchange(mbedtls_ssl_context *ssl);
Jerry Yu95699e72022-08-21 19:22:23 +0800101
102MBEDTLS_CHECK_RETURN_CRITICAL
103static int ssl_tls13_offered_psks_check_identity_match_ticket(
Gilles Peskine449bd832023-01-11 14:50:10 +0100104 mbedtls_ssl_context *ssl,
105 const unsigned char *identity,
106 size_t identity_len,
107 uint32_t obfuscated_ticket_age,
108 mbedtls_ssl_session *session)
Jerry Yu95699e72022-08-21 19:22:23 +0800109{
Jerry Yufd310eb2022-09-06 09:16:35 +0800110 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yu95699e72022-08-21 19:22:23 +0800111 unsigned char *ticket_buffer;
Pengyu Lv29daf4a2023-10-30 17:13:30 +0800112 unsigned int key_exchanges;
Jerry Yu8d4bbba2022-09-13 14:15:48 +0800113#if defined(MBEDTLS_HAVE_TIME)
114 mbedtls_time_t now;
Jerry Yuacff8232022-09-14 14:35:11 +0800115 uint64_t age_in_s;
Jerry Yuf7dad3c2022-09-14 22:31:39 +0800116 int64_t age_diff_in_ms;
Jerry Yu8d4bbba2022-09-13 14:15:48 +0800117#endif
Jerry Yu95699e72022-08-21 19:22:23 +0800118
119 ((void) obfuscated_ticket_age);
120
Gilles Peskine449bd832023-01-11 14:50:10 +0100121 MBEDTLS_SSL_DEBUG_MSG(2, ("=> check_identity_match_ticket"));
Jerry Yu95699e72022-08-21 19:22:23 +0800122
Jerry Yufd310eb2022-09-06 09:16:35 +0800123 /* Ticket parser is not configured, Skip */
Gilles Peskine449bd832023-01-11 14:50:10 +0100124 if (ssl->conf->f_ticket_parse == NULL || identity_len == 0) {
125 return 0;
126 }
Jerry Yu95699e72022-08-21 19:22:23 +0800127
Jerry Yu4746b102022-09-13 11:11:48 +0800128 /* We create a copy of the encrypted ticket since the ticket parsing
129 * function is allowed to use its input buffer as an output buffer
130 * (in-place decryption). We do, however, need the original buffer for
131 * computing the PSK binder value.
Jerry Yu95699e72022-08-21 19:22:23 +0800132 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100133 ticket_buffer = mbedtls_calloc(1, identity_len);
134 if (ticket_buffer == NULL) {
135 MBEDTLS_SSL_DEBUG_MSG(1, ("buffer too small"));
136 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
Jerry Yu95699e72022-08-21 19:22:23 +0800137 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100138 memcpy(ticket_buffer, identity, identity_len);
Jerry Yu95699e72022-08-21 19:22:23 +0800139
Gilles Peskine449bd832023-01-11 14:50:10 +0100140 if ((ret = ssl->conf->f_ticket_parse(ssl->conf->p_ticket,
141 session,
142 ticket_buffer, identity_len)) != 0) {
143 if (ret == MBEDTLS_ERR_SSL_INVALID_MAC) {
144 MBEDTLS_SSL_DEBUG_MSG(3, ("ticket is not authentic"));
145 } else if (ret == MBEDTLS_ERR_SSL_SESSION_TICKET_EXPIRED) {
146 MBEDTLS_SSL_DEBUG_MSG(3, ("ticket is expired"));
147 } else {
148 MBEDTLS_SSL_DEBUG_RET(1, "ticket_parse", ret);
149 }
Jerry Yu95699e72022-08-21 19:22:23 +0800150 }
151
152 /* We delete the temporary buffer */
Gilles Peskine449bd832023-01-11 14:50:10 +0100153 mbedtls_free(ticket_buffer);
Jerry Yu95699e72022-08-21 19:22:23 +0800154
Gilles Peskine449bd832023-01-11 14:50:10 +0100155 if (ret != 0) {
Jerry Yu8d4bbba2022-09-13 14:15:48 +0800156 goto exit;
157 }
Jerry Yu95699e72022-08-21 19:22:23 +0800158
Pengyu Lv3eb49be2022-12-05 16:35:12 +0800159 /* RFC 8446 section 4.2.9
160 *
161 * Servers SHOULD NOT send NewSessionTicket with tickets that are not
162 * compatible with the advertised modes; however, if a server does so,
163 * the impact will just be that the client's attempts at resumption fail.
164 *
165 * We regard the ticket with incompatible key exchange modes as not match.
166 */
Pengyu Lv18946532023-01-12 12:28:09 +0800167 ret = MBEDTLS_ERR_ERROR_GENERIC_ERROR;
Pengyu Lv29daf4a2023-10-30 17:13:30 +0800168 MBEDTLS_SSL_PRINT_TICKET_FLAGS(4, session->ticket_flags);
Pengyu Lv29daf4a2023-10-30 17:13:30 +0800169
170 key_exchanges = 0;
Pengyu Lvdbd1e0d2023-10-31 10:08:10 +0800171 if (mbedtls_ssl_session_ticket_allow_psk_ephemeral(session) &&
Pengyu Lv29daf4a2023-10-30 17:13:30 +0800172 ssl_tls13_check_psk_ephemeral_key_exchange(ssl)) {
173 key_exchanges |= MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK_EPHEMERAL;
174 }
Pengyu Lvdbd1e0d2023-10-31 10:08:10 +0800175 if (mbedtls_ssl_session_ticket_allow_psk(session) &&
Pengyu Lv29daf4a2023-10-30 17:13:30 +0800176 ssl_tls13_check_psk_key_exchange(ssl)) {
177 key_exchanges |= MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK;
178 }
179
180 if (key_exchanges == 0) {
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 */
Dave Rodgman2eab4622023-10-05 13:30:37 +0100410 if (psk_hash_alg ==
411 mbedtls_md_psa_alg_from_type((mbedtls_md_type_t) ciphersuite_info->mac)) {
Jerry Yuf35ba382022-08-23 17:58:26 +0800412 *selected_ciphersuite = cipher_suite;
413 *selected_ciphersuite_info = ciphersuite_info;
Gilles Peskine449bd832023-01-11 14:50:10 +0100414 return 0;
Jerry Yuf35ba382022-08-23 17:58:26 +0800415 }
416 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100417 MBEDTLS_SSL_DEBUG_MSG(2, ("No matched ciphersuite"));
418 return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
Jerry Yuf35ba382022-08-23 17:58:26 +0800419}
Jerry Yu5725f1c2022-08-21 17:27:16 +0800420
Jerry Yu82534862022-08-30 10:42:33 +0800421#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Jerry Yuf35ba382022-08-23 17:58:26 +0800422MBEDTLS_CHECK_RETURN_CRITICAL
423static int ssl_tls13_select_ciphersuite_for_resumption(
Gilles Peskine449bd832023-01-11 14:50:10 +0100424 mbedtls_ssl_context *ssl,
425 const unsigned char *cipher_suites,
426 const unsigned char *cipher_suites_end,
427 mbedtls_ssl_session *session,
428 uint16_t *selected_ciphersuite,
429 const mbedtls_ssl_ciphersuite_t **selected_ciphersuite_info)
Jerry Yuf35ba382022-08-23 17:58:26 +0800430{
Jerry Yu82534862022-08-30 10:42:33 +0800431
Jerry Yuf35ba382022-08-23 17:58:26 +0800432 *selected_ciphersuite = 0;
433 *selected_ciphersuite_info = NULL;
Gilles Peskine449bd832023-01-11 14:50:10 +0100434 for (const unsigned char *p = cipher_suites; p < cipher_suites_end; p += 2) {
435 uint16_t cipher_suite = MBEDTLS_GET_UINT16_BE(p, 0);
Jerry Yu82534862022-08-30 10:42:33 +0800436 const mbedtls_ssl_ciphersuite_t *ciphersuite_info;
437
Gilles Peskine449bd832023-01-11 14:50:10 +0100438 if (cipher_suite != session->ciphersuite) {
Jerry Yu82534862022-08-30 10:42:33 +0800439 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +0100440 }
Jerry Yu82534862022-08-30 10:42:33 +0800441
Gilles Peskine449bd832023-01-11 14:50:10 +0100442 ciphersuite_info = ssl_tls13_validate_peer_ciphersuite(ssl,
443 cipher_suite);
444 if (ciphersuite_info == NULL) {
Jerry Yu82534862022-08-30 10:42:33 +0800445 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +0100446 }
Jerry Yu82534862022-08-30 10:42:33 +0800447
Jerry Yu4746b102022-09-13 11:11:48 +0800448 *selected_ciphersuite = cipher_suite;
Jerry Yu82534862022-08-30 10:42:33 +0800449 *selected_ciphersuite_info = ciphersuite_info;
450
Gilles Peskine449bd832023-01-11 14:50:10 +0100451 return 0;
Jerry Yu82534862022-08-30 10:42:33 +0800452 }
453
Gilles Peskine449bd832023-01-11 14:50:10 +0100454 return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
Jerry Yu5725f1c2022-08-21 17:27:16 +0800455}
Jerry Yuf35ba382022-08-23 17:58:26 +0800456
Jerry Yu82534862022-08-30 10:42:33 +0800457MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100458static int ssl_tls13_session_copy_ticket(mbedtls_ssl_session *dst,
459 const mbedtls_ssl_session *src)
Jerry Yu82534862022-08-30 10:42:33 +0800460{
Jerry Yu82534862022-08-30 10:42:33 +0800461 dst->ticket_age_add = src->ticket_age_add;
462 dst->ticket_flags = src->ticket_flags;
463 dst->resumption_key_len = src->resumption_key_len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100464 if (src->resumption_key_len == 0) {
465 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
466 }
467 memcpy(dst->resumption_key, src->resumption_key, src->resumption_key_len);
Jerry Yu4746b102022-09-13 11:11:48 +0800468
Gilles Peskine449bd832023-01-11 14:50:10 +0100469 return 0;
Jerry Yu82534862022-08-30 10:42:33 +0800470}
471#endif /* MBEDTLS_SSL_SESSION_TICKETS */
472
Jerry Yu1c105562022-07-10 06:32:38 +0000473/* Parser for pre_shared_key extension in client hello
474 * struct {
475 * opaque identity<1..2^16-1>;
476 * uint32 obfuscated_ticket_age;
477 * } PskIdentity;
478 *
479 * opaque PskBinderEntry<32..255>;
480 *
481 * struct {
482 * PskIdentity identities<7..2^16-1>;
483 * PskBinderEntry binders<33..2^16-1>;
484 * } OfferedPsks;
485 *
486 * struct {
487 * select (Handshake.msg_type) {
488 * case client_hello: OfferedPsks;
489 * ....
490 * };
491 * } PreSharedKeyExtension;
492 */
Jerry Yu6e74a7e2022-07-20 20:49:32 +0800493MBEDTLS_CHECK_RETURN_CRITICAL
Xiaokang Qian9f1747b2023-03-30 02:50:04 +0000494static int ssl_tls13_parse_pre_shared_key_ext(
495 mbedtls_ssl_context *ssl,
496 const unsigned char *pre_shared_key_ext,
497 const unsigned char *pre_shared_key_ext_end,
498 const unsigned char *ciphersuites,
499 const unsigned char *ciphersuites_end)
Jerry Yu1c105562022-07-10 06:32:38 +0000500{
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +0100501 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yu29d9faa2022-08-23 17:52:45 +0800502 const unsigned char *identities = pre_shared_key_ext;
Jerry Yu568ec252022-07-22 21:27:34 +0800503 const unsigned char *p_identity_len;
504 size_t identities_len;
Jerry Yu1c105562022-07-10 06:32:38 +0000505 const unsigned char *identities_end;
Jerry Yu568ec252022-07-22 21:27:34 +0800506 const unsigned char *binders;
507 const unsigned char *p_binder_len;
508 size_t binders_len;
Jerry Yu1c105562022-07-10 06:32:38 +0000509 const unsigned char *binders_end;
Jerry Yu96a2e362022-07-21 15:11:34 +0800510 int matched_identity = -1;
511 int identity_id = -1;
Jerry Yu1c105562022-07-10 06:32:38 +0000512
Gilles Peskine449bd832023-01-11 14:50:10 +0100513 MBEDTLS_SSL_DEBUG_BUF(3, "pre_shared_key extension",
514 pre_shared_key_ext,
515 pre_shared_key_ext_end - pre_shared_key_ext);
Jerry Yu1c105562022-07-10 06:32:38 +0000516
Jerry Yu96a2e362022-07-21 15:11:34 +0800517 /* identities_len 2 bytes
518 * identities_data >= 7 bytes
519 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100520 MBEDTLS_SSL_CHK_BUF_READ_PTR(identities, pre_shared_key_ext_end, 7 + 2);
521 identities_len = MBEDTLS_GET_UINT16_BE(identities, 0);
Jerry Yu568ec252022-07-22 21:27:34 +0800522 p_identity_len = identities + 2;
Gilles Peskine449bd832023-01-11 14:50:10 +0100523 MBEDTLS_SSL_CHK_BUF_READ_PTR(p_identity_len, pre_shared_key_ext_end,
524 identities_len);
Jerry Yu568ec252022-07-22 21:27:34 +0800525 identities_end = p_identity_len + identities_len;
Jerry Yu1c105562022-07-10 06:32:38 +0000526
Jerry Yu96a2e362022-07-21 15:11:34 +0800527 /* binders_len 2 bytes
528 * binders >= 33 bytes
529 */
Jerry Yu568ec252022-07-22 21:27:34 +0800530 binders = identities_end;
Gilles Peskine449bd832023-01-11 14:50:10 +0100531 MBEDTLS_SSL_CHK_BUF_READ_PTR(binders, pre_shared_key_ext_end, 33 + 2);
532 binders_len = MBEDTLS_GET_UINT16_BE(binders, 0);
Jerry Yu568ec252022-07-22 21:27:34 +0800533 p_binder_len = binders + 2;
Gilles Peskine449bd832023-01-11 14:50:10 +0100534 MBEDTLS_SSL_CHK_BUF_READ_PTR(p_binder_len, pre_shared_key_ext_end, binders_len);
Jerry Yu568ec252022-07-22 21:27:34 +0800535 binders_end = p_binder_len + binders_len;
Jerry Yu1c105562022-07-10 06:32:38 +0000536
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +0100537 ret = ssl->handshake->update_checksum(ssl, pre_shared_key_ext,
538 identities_end - pre_shared_key_ext);
539 if (0 != ret) {
540 MBEDTLS_SSL_DEBUG_RET(1, ("update_checksum"), ret);
541 return ret;
542 }
Jerry Yu96a2e362022-07-21 15:11:34 +0800543
Gilles Peskine449bd832023-01-11 14:50:10 +0100544 while (p_identity_len < identities_end && p_binder_len < binders_end) {
Jerry Yu1c105562022-07-10 06:32:38 +0000545 const unsigned char *identity;
Jerry Yu568ec252022-07-22 21:27:34 +0800546 size_t identity_len;
Jerry Yu82534862022-08-30 10:42:33 +0800547 uint32_t obfuscated_ticket_age;
Jerry Yu96a2e362022-07-21 15:11:34 +0800548 const unsigned char *binder;
Jerry Yu568ec252022-07-22 21:27:34 +0800549 size_t binder_len;
Jerry Yu5725f1c2022-08-21 17:27:16 +0800550 int psk_type;
551 uint16_t cipher_suite;
Jerry Yu29d9faa2022-08-23 17:52:45 +0800552 const mbedtls_ssl_ciphersuite_t *ciphersuite_info;
Jerry Yu82534862022-08-30 10:42:33 +0800553#if defined(MBEDTLS_SSL_SESSION_TICKETS)
554 mbedtls_ssl_session session;
Gilles Peskine449bd832023-01-11 14:50:10 +0100555 mbedtls_ssl_session_init(&session);
Jerry Yu82534862022-08-30 10:42:33 +0800556#endif
Jerry Yubb852022022-07-20 21:10:44 +0800557
Gilles Peskine449bd832023-01-11 14:50:10 +0100558 MBEDTLS_SSL_CHK_BUF_READ_PTR(p_identity_len, identities_end, 2 + 1 + 4);
559 identity_len = MBEDTLS_GET_UINT16_BE(p_identity_len, 0);
Jerry Yu568ec252022-07-22 21:27:34 +0800560 identity = p_identity_len + 2;
Gilles Peskine449bd832023-01-11 14:50:10 +0100561 MBEDTLS_SSL_CHK_BUF_READ_PTR(identity, identities_end, identity_len + 4);
562 obfuscated_ticket_age = MBEDTLS_GET_UINT32_BE(identity, identity_len);
Jerry Yu568ec252022-07-22 21:27:34 +0800563 p_identity_len += identity_len + 6;
Jerry Yu1c105562022-07-10 06:32:38 +0000564
Gilles Peskine449bd832023-01-11 14:50:10 +0100565 MBEDTLS_SSL_CHK_BUF_READ_PTR(p_binder_len, binders_end, 1 + 32);
Jerry Yu568ec252022-07-22 21:27:34 +0800566 binder_len = *p_binder_len;
567 binder = p_binder_len + 1;
Gilles Peskine449bd832023-01-11 14:50:10 +0100568 MBEDTLS_SSL_CHK_BUF_READ_PTR(binder, binders_end, binder_len);
Jerry Yu568ec252022-07-22 21:27:34 +0800569 p_binder_len += binder_len + 1;
Jerry Yu96a2e362022-07-21 15:11:34 +0800570
Jerry Yu96a2e362022-07-21 15:11:34 +0800571 identity_id++;
Gilles Peskine449bd832023-01-11 14:50:10 +0100572 if (matched_identity != -1) {
Jerry Yu1c105562022-07-10 06:32:38 +0000573 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +0100574 }
Jerry Yu1c105562022-07-10 06:32:38 +0000575
Jerry Yu96a2e362022-07-21 15:11:34 +0800576 ret = ssl_tls13_offered_psks_check_identity_match(
Gilles Peskine449bd832023-01-11 14:50:10 +0100577 ssl, identity, identity_len, obfuscated_ticket_age,
578 &psk_type, &session);
579 if (ret != SSL_TLS1_3_OFFERED_PSK_MATCH) {
Jerry Yu96a2e362022-07-21 15:11:34 +0800580 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +0100581 }
Jerry Yu96a2e362022-07-21 15:11:34 +0800582
Gilles Peskine449bd832023-01-11 14:50:10 +0100583 MBEDTLS_SSL_DEBUG_MSG(4, ("found matched identity"));
584 switch (psk_type) {
Jerry Yu0baf9072022-08-25 11:21:04 +0800585 case MBEDTLS_SSL_TLS1_3_PSK_EXTERNAL:
586 ret = ssl_tls13_select_ciphersuite_for_psk(
Gilles Peskine449bd832023-01-11 14:50:10 +0100587 ssl, ciphersuites, ciphersuites_end,
588 &cipher_suite, &ciphersuite_info);
Jerry Yu0baf9072022-08-25 11:21:04 +0800589 break;
590 case MBEDTLS_SSL_TLS1_3_PSK_RESUMPTION:
Jerry Yu82534862022-08-30 10:42:33 +0800591#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Jerry Yu0baf9072022-08-25 11:21:04 +0800592 ret = ssl_tls13_select_ciphersuite_for_resumption(
Gilles Peskine449bd832023-01-11 14:50:10 +0100593 ssl, ciphersuites, ciphersuites_end, &session,
594 &cipher_suite, &ciphersuite_info);
595 if (ret != 0) {
596 mbedtls_ssl_session_free(&session);
597 }
Jerry Yu82534862022-08-30 10:42:33 +0800598#else
599 ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
600#endif
Jerry Yu0baf9072022-08-25 11:21:04 +0800601 break;
602 default:
Gilles Peskine449bd832023-01-11 14:50:10 +0100603 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Jerry Yu0baf9072022-08-25 11:21:04 +0800604 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100605 if (ret != 0) {
Jerry Yuf35ba382022-08-23 17:58:26 +0800606 /* See below, no cipher_suite available, abort handshake */
607 MBEDTLS_SSL_PEND_FATAL_ALERT(
608 MBEDTLS_SSL_ALERT_MSG_DECRYPT_ERROR,
Gilles Peskine449bd832023-01-11 14:50:10 +0100609 MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE);
Jerry Yuf35ba382022-08-23 17:58:26 +0800610 MBEDTLS_SSL_DEBUG_RET(
Gilles Peskine449bd832023-01-11 14:50:10 +0100611 2, "ssl_tls13_select_ciphersuite", ret);
612 return ret;
Jerry Yu5725f1c2022-08-21 17:27:16 +0800613 }
614
Jerry Yu96a2e362022-07-21 15:11:34 +0800615 ret = ssl_tls13_offered_psks_check_binder_match(
Gilles Peskine449bd832023-01-11 14:50:10 +0100616 ssl, binder, binder_len, psk_type,
Dave Rodgman2eab4622023-10-05 13:30:37 +0100617 mbedtls_md_psa_alg_from_type((mbedtls_md_type_t) ciphersuite_info->mac));
Gilles Peskine449bd832023-01-11 14:50:10 +0100618 if (ret != SSL_TLS1_3_OFFERED_PSK_MATCH) {
Jerry Yuc5a23a02022-08-25 10:51:44 +0800619 /* For security reasons, the handshake should be aborted when we
620 * fail to validate a binder value. See RFC 8446 section 4.2.11.2
621 * and appendix E.6. */
Jerry Yu82534862022-08-30 10:42:33 +0800622#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Gilles Peskine449bd832023-01-11 14:50:10 +0100623 mbedtls_ssl_session_free(&session);
Jerry Yu82534862022-08-30 10:42:33 +0800624#endif
Gilles Peskine449bd832023-01-11 14:50:10 +0100625 MBEDTLS_SSL_DEBUG_MSG(3, ("Invalid binder."));
Xiaokang Qian9f1747b2023-03-30 02:50:04 +0000626 MBEDTLS_SSL_DEBUG_RET(
627 1, "ssl_tls13_offered_psks_check_binder_match", ret);
Jerry Yu96a2e362022-07-21 15:11:34 +0800628 MBEDTLS_SSL_PEND_FATAL_ALERT(
629 MBEDTLS_SSL_ALERT_MSG_DECRYPT_ERROR,
Gilles Peskine449bd832023-01-11 14:50:10 +0100630 MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE);
631 return ret;
Jerry Yu96a2e362022-07-21 15:11:34 +0800632 }
Jerry Yu96a2e362022-07-21 15:11:34 +0800633
634 matched_identity = identity_id;
Jerry Yu5725f1c2022-08-21 17:27:16 +0800635
636 /* Update handshake parameters */
Jerry Yue5834fd2022-08-29 20:16:09 +0800637 ssl->handshake->ciphersuite_info = ciphersuite_info;
Jerry Yu4746b102022-09-13 11:11:48 +0800638 ssl->session_negotiate->ciphersuite = cipher_suite;
Gilles Peskine449bd832023-01-11 14:50:10 +0100639 MBEDTLS_SSL_DEBUG_MSG(2, ("overwrite ciphersuite: %04x - %s",
640 cipher_suite, ciphersuite_info->name));
Jerry Yu82534862022-08-30 10:42:33 +0800641#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Gilles Peskine449bd832023-01-11 14:50:10 +0100642 if (psk_type == MBEDTLS_SSL_TLS1_3_PSK_RESUMPTION) {
643 ret = ssl_tls13_session_copy_ticket(ssl->session_negotiate,
644 &session);
645 mbedtls_ssl_session_free(&session);
646 if (ret != 0) {
647 return ret;
648 }
Jerry Yu82534862022-08-30 10:42:33 +0800649 }
650#endif /* MBEDTLS_SSL_SESSION_TICKETS */
Jerry Yu1c105562022-07-10 06:32:38 +0000651 }
652
Gilles Peskine449bd832023-01-11 14:50:10 +0100653 if (p_identity_len != identities_end || p_binder_len != binders_end) {
654 MBEDTLS_SSL_DEBUG_MSG(3, ("pre_shared_key extension decode error"));
655 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR,
656 MBEDTLS_ERR_SSL_DECODE_ERROR);
657 return MBEDTLS_ERR_SSL_DECODE_ERROR;
Jerry Yu1c105562022-07-10 06:32:38 +0000658 }
659
Jerry Yu6f1db3f2022-07-22 23:05:59 +0800660 /* Update the handshake transcript with the binder list. */
Xiaokang Qian9f1747b2023-03-30 02:50:04 +0000661 ret = ssl->handshake->update_checksum(
662 ssl, identities_end, (size_t) (binders_end - identities_end));
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +0100663 if (0 != ret) {
664 MBEDTLS_SSL_DEBUG_RET(1, ("update_checksum"), ret);
665 return ret;
666 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100667 if (matched_identity == -1) {
668 MBEDTLS_SSL_DEBUG_MSG(3, ("No matched PSK or ticket."));
669 return MBEDTLS_ERR_SSL_UNKNOWN_IDENTITY;
Jerry Yu1c105562022-07-10 06:32:38 +0000670 }
671
Gilles Peskine449bd832023-01-11 14:50:10 +0100672 ssl->handshake->selected_identity = (uint16_t) matched_identity;
673 MBEDTLS_SSL_DEBUG_MSG(3, ("Pre shared key found"));
Jerry Yu1c105562022-07-10 06:32:38 +0000674
Gilles Peskine449bd832023-01-11 14:50:10 +0100675 return 0;
Jerry Yu1c105562022-07-10 06:32:38 +0000676}
Jerry Yu032b15ce2022-07-11 06:10:03 +0000677
678/*
679 * struct {
680 * select ( Handshake.msg_type ) {
681 * ....
682 * case server_hello:
683 * uint16 selected_identity;
684 * }
685 * } PreSharedKeyExtension;
686 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100687static int ssl_tls13_write_server_pre_shared_key_ext(mbedtls_ssl_context *ssl,
688 unsigned char *buf,
689 unsigned char *end,
690 size_t *olen)
Jerry Yu032b15ce2022-07-11 06:10:03 +0000691{
Gilles Peskine449bd832023-01-11 14:50:10 +0100692 unsigned char *p = (unsigned char *) buf;
Jerry Yu032b15ce2022-07-11 06:10:03 +0000693
694 *olen = 0;
695
David Horstmann21b89762022-10-06 18:34:28 +0100696 int not_using_psk = 0;
Jerry Yu032b15ce2022-07-11 06:10:03 +0000697#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +0100698 not_using_psk = (mbedtls_svc_key_id_is_null(ssl->handshake->psk_opaque));
Jerry Yu032b15ce2022-07-11 06:10:03 +0000699#else
Gilles Peskine449bd832023-01-11 14:50:10 +0100700 not_using_psk = (ssl->handshake->psk == NULL);
Jerry Yu032b15ce2022-07-11 06:10:03 +0000701#endif
Gilles Peskine449bd832023-01-11 14:50:10 +0100702 if (not_using_psk) {
Jerry Yu032b15ce2022-07-11 06:10:03 +0000703 /* We shouldn't have called this extension writer unless we've
704 * chosen to use a PSK. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100705 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Jerry Yu032b15ce2022-07-11 06:10:03 +0000706 }
707
Gilles Peskine449bd832023-01-11 14:50:10 +0100708 MBEDTLS_SSL_DEBUG_MSG(3, ("server hello, adding pre_shared_key extension"));
709 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 6);
Jerry Yu032b15ce2022-07-11 06:10:03 +0000710
Gilles Peskine449bd832023-01-11 14:50:10 +0100711 MBEDTLS_PUT_UINT16_BE(MBEDTLS_TLS_EXT_PRE_SHARED_KEY, p, 0);
712 MBEDTLS_PUT_UINT16_BE(2, p, 2);
Jerry Yu032b15ce2022-07-11 06:10:03 +0000713
Gilles Peskine449bd832023-01-11 14:50:10 +0100714 MBEDTLS_PUT_UINT16_BE(ssl->handshake->selected_identity, p, 4);
Jerry Yu032b15ce2022-07-11 06:10:03 +0000715
716 *olen = 6;
717
Gilles Peskine449bd832023-01-11 14:50:10 +0100718 MBEDTLS_SSL_DEBUG_MSG(4, ("sent selected_identity: %u",
719 ssl->handshake->selected_identity));
Jerry Yu032b15ce2022-07-11 06:10:03 +0000720
Gilles Peskine449bd832023-01-11 14:50:10 +0100721 mbedtls_ssl_tls13_set_hs_sent_ext_mask(ssl, MBEDTLS_TLS_EXT_PRE_SHARED_KEY);
Jerry Yub95dd362022-11-08 21:19:34 +0800722
Gilles Peskine449bd832023-01-11 14:50:10 +0100723 return 0;
Jerry Yu032b15ce2022-07-11 06:10:03 +0000724}
725
Ronald Cron41a443a2022-10-04 16:38:25 +0200726#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED */
Jerry Yue19e3b92022-07-08 12:04:51 +0000727
XiaokangQian7807f9f2022-02-15 10:04:37 +0000728/* From RFC 8446:
729 * struct {
XiaokangQiancfd925f2022-04-14 07:10:37 +0000730 * ProtocolVersion versions<2..254>;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000731 * } SupportedVersions;
732 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200733MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100734static int ssl_tls13_parse_supported_versions_ext(mbedtls_ssl_context *ssl,
735 const unsigned char *buf,
736 const unsigned char *end)
XiaokangQian7807f9f2022-02-15 10:04:37 +0000737{
XiaokangQian7807f9f2022-02-15 10:04:37 +0000738 const unsigned char *p = buf;
XiaokangQian8f9dfe42022-04-15 02:52:39 +0000739 size_t versions_len;
XiaokangQian4080a7f2022-04-11 09:55:18 +0000740 const unsigned char *versions_end;
XiaokangQian0a1b54e2022-04-21 03:01:38 +0000741 uint16_t tls_version;
Ronald Cron5af4c7f2023-03-07 20:46:59 +0100742 int found_supported_version = 0;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000743
Gilles Peskine449bd832023-01-11 14:50:10 +0100744 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, 1);
XiaokangQian4080a7f2022-04-11 09:55:18 +0000745 versions_len = p[0];
XiaokangQian7807f9f2022-02-15 10:04:37 +0000746 p += 1;
747
Gilles Peskine449bd832023-01-11 14:50:10 +0100748 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, versions_len);
XiaokangQian4080a7f2022-04-11 09:55:18 +0000749 versions_end = p + versions_len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100750 while (p < versions_end) {
751 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, versions_end, 2);
752 tls_version = mbedtls_ssl_read_version(p, ssl->conf->transport);
XiaokangQianb67384d2022-04-19 00:02:38 +0000753 p += 2;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000754
Ronald Cron3bd2b022023-04-03 16:45:39 +0200755 if (MBEDTLS_SSL_VERSION_TLS1_3 == tls_version) {
Ronald Cron5af4c7f2023-03-07 20:46:59 +0100756 found_supported_version = 1;
757 break;
758 }
759
Ronald Cron3bd2b022023-04-03 16:45:39 +0200760 if ((MBEDTLS_SSL_VERSION_TLS1_2 == tls_version) &&
761 mbedtls_ssl_conf_is_tls12_enabled(ssl->conf)) {
Ronald Cron5af4c7f2023-03-07 20:46:59 +0100762 found_supported_version = 1;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000763 break;
764 }
XiaokangQian7807f9f2022-02-15 10:04:37 +0000765 }
766
Ronald Cron5af4c7f2023-03-07 20:46:59 +0100767 if (!found_supported_version) {
768 MBEDTLS_SSL_DEBUG_MSG(1, ("No supported version found."));
XiaokangQian7807f9f2022-02-15 10:04:37 +0000769
Gilles Peskine449bd832023-01-11 14:50:10 +0100770 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_PROTOCOL_VERSION,
771 MBEDTLS_ERR_SSL_BAD_PROTOCOL_VERSION);
772 return MBEDTLS_ERR_SSL_BAD_PROTOCOL_VERSION;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000773 }
774
Ronald Cron5af4c7f2023-03-07 20:46:59 +0100775 MBEDTLS_SSL_DEBUG_MSG(1, ("Negotiated version: [%04x]",
Gilles Peskine449bd832023-01-11 14:50:10 +0100776 (unsigned int) tls_version));
XiaokangQian7807f9f2022-02-15 10:04:37 +0000777
Ronald Cron5af4c7f2023-03-07 20:46:59 +0100778 return (int) tls_version;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000779}
780
Przemek Stekiel8c0a9532023-06-15 16:48:19 +0200781#if defined(PSA_WANT_ALG_ECDH) || defined(PSA_WANT_ALG_FFDH)
XiaokangQiane8ff3502022-04-22 02:34:40 +0000782/*
XiaokangQian7807f9f2022-02-15 10:04:37 +0000783 *
784 * From RFC 8446:
785 * enum {
786 * ... (0xFFFF)
787 * } NamedGroup;
788 * struct {
789 * NamedGroup named_group_list<2..2^16-1>;
790 * } NamedGroupList;
791 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200792MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100793static int ssl_tls13_parse_supported_groups_ext(mbedtls_ssl_context *ssl,
794 const unsigned char *buf,
795 const unsigned char *end)
XiaokangQian7807f9f2022-02-15 10:04:37 +0000796{
XiaokangQian7807f9f2022-02-15 10:04:37 +0000797 const unsigned char *p = buf;
XiaokangQian84823772022-04-19 07:57:30 +0000798 size_t named_group_list_len;
XiaokangQian8f9dfe42022-04-15 02:52:39 +0000799 const unsigned char *named_group_list_end;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000800
Gilles Peskine449bd832023-01-11 14:50:10 +0100801 MBEDTLS_SSL_DEBUG_BUF(3, "supported_groups extension", p, end - buf);
802 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, 2);
803 named_group_list_len = MBEDTLS_GET_UINT16_BE(p, 0);
XiaokangQian7807f9f2022-02-15 10:04:37 +0000804 p += 2;
Gilles Peskine449bd832023-01-11 14:50:10 +0100805 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, named_group_list_len);
XiaokangQian8f9dfe42022-04-15 02:52:39 +0000806 named_group_list_end = p + named_group_list_len;
XiaokangQian4e8cd7b2022-04-21 09:48:09 +0000807 ssl->handshake->hrr_selected_group = 0;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000808
Gilles Peskine449bd832023-01-11 14:50:10 +0100809 while (p < named_group_list_end) {
XiaokangQian08037552022-04-20 07:16:41 +0000810 uint16_t named_group;
Gilles Peskine449bd832023-01-11 14:50:10 +0100811 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, named_group_list_end, 2);
812 named_group = MBEDTLS_GET_UINT16_BE(p, 0);
XiaokangQian08037552022-04-20 07:16:41 +0000813 p += 2;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000814
Gilles Peskine449bd832023-01-11 14:50:10 +0100815 MBEDTLS_SSL_DEBUG_MSG(2,
816 ("got named group: %s(%04x)",
817 mbedtls_ssl_named_group_to_str(named_group),
818 named_group));
XiaokangQian08037552022-04-20 07:16:41 +0000819
Gilles Peskine449bd832023-01-11 14:50:10 +0100820 if (!mbedtls_ssl_named_group_is_offered(ssl, named_group) ||
821 !mbedtls_ssl_named_group_is_supported(named_group) ||
822 ssl->handshake->hrr_selected_group != 0) {
XiaokangQian08037552022-04-20 07:16:41 +0000823 continue;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000824 }
825
Gilles Peskine449bd832023-01-11 14:50:10 +0100826 MBEDTLS_SSL_DEBUG_MSG(2,
827 ("add named group %s(%04x) into received list.",
828 mbedtls_ssl_named_group_to_str(named_group),
829 named_group));
Jerry Yuc1be19f2022-04-23 16:11:39 +0800830
XiaokangQian4e8cd7b2022-04-21 09:48:09 +0000831 ssl->handshake->hrr_selected_group = named_group;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000832 }
833
Gilles Peskine449bd832023-01-11 14:50:10 +0100834 return 0;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000835
836}
Przemek Stekiel8c0a9532023-06-15 16:48:19 +0200837#endif /* PSA_WANT_ALG_ECDH || PSA_WANT_ALG_FFDH */
XiaokangQian7807f9f2022-02-15 10:04:37 +0000838
XiaokangQian08037552022-04-20 07:16:41 +0000839#define SSL_TLS1_3_PARSE_KEY_SHARES_EXT_NO_MATCH 1
840
Valerio Settic9ae8622023-07-25 11:23:50 +0200841#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_EPHEMERAL_ENABLED)
XiaokangQian7807f9f2022-02-15 10:04:37 +0000842/*
843 * ssl_tls13_parse_key_shares_ext() verifies whether the information in the
Xiaokang Qian9f1747b2023-03-30 02:50:04 +0000844 * extension is correct and stores the first acceptable key share and its
845 * associated group.
XiaokangQian7807f9f2022-02-15 10:04:37 +0000846 *
847 * Possible return values are:
848 * - 0: Successful processing of the client provided key share extension.
Xiaokang Qian9f1747b2023-03-30 02:50:04 +0000849 * - SSL_TLS1_3_PARSE_KEY_SHARES_EXT_NO_MATCH: The key shares provided by
850 * the client does not match a group supported by the server. A
851 * HelloRetryRequest will be needed.
XiaokangQiane8ff3502022-04-22 02:34:40 +0000852 * - A negative value for fatal errors.
Jerry Yuc1be19f2022-04-23 16:11:39 +0800853 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200854MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100855static int ssl_tls13_parse_key_shares_ext(mbedtls_ssl_context *ssl,
856 const unsigned char *buf,
857 const unsigned char *end)
XiaokangQian7807f9f2022-02-15 10:04:37 +0000858{
XiaokangQianb67384d2022-04-19 00:02:38 +0000859 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000860 unsigned char const *p = buf;
XiaokangQian8f9dfe42022-04-15 02:52:39 +0000861 unsigned char const *client_shares_end;
Jerry Yuc1be19f2022-04-23 16:11:39 +0800862 size_t client_shares_len;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000863
864 /* From RFC 8446:
865 *
866 * struct {
867 * KeyShareEntry client_shares<0..2^16-1>;
868 * } KeyShareClientHello;
869 *
870 */
871
Gilles Peskine449bd832023-01-11 14:50:10 +0100872 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, 2);
873 client_shares_len = MBEDTLS_GET_UINT16_BE(p, 0);
XiaokangQian7807f9f2022-02-15 10:04:37 +0000874 p += 2;
Gilles Peskine449bd832023-01-11 14:50:10 +0100875 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, client_shares_len);
XiaokangQian7807f9f2022-02-15 10:04:37 +0000876
877 ssl->handshake->offered_group_id = 0;
XiaokangQian8f9dfe42022-04-15 02:52:39 +0000878 client_shares_end = p + client_shares_len;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000879
880 /* We try to find a suitable key share entry and copy it to the
881 * handshake context. Later, we have to find out whether we can do
882 * something with the provided key share or whether we have to
XiaokangQianc5763b52022-04-02 03:34:37 +0000883 * dismiss it and send a HelloRetryRequest message.
884 */
XiaokangQian7807f9f2022-02-15 10:04:37 +0000885
Gilles Peskine449bd832023-01-11 14:50:10 +0100886 while (p < client_shares_end) {
XiaokangQian9b5d04b2022-04-10 10:20:43 +0000887 uint16_t group;
Jerry Yuc1be19f2022-04-23 16:11:39 +0800888 size_t key_exchange_len;
Jerry Yu086edc22022-05-05 10:50:38 +0800889 const unsigned char *key_exchange;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000890
891 /*
892 * struct {
893 * NamedGroup group;
894 * opaque key_exchange<1..2^16-1>;
895 * } KeyShareEntry;
896 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100897 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, client_shares_end, 4);
898 group = MBEDTLS_GET_UINT16_BE(p, 0);
899 key_exchange_len = MBEDTLS_GET_UINT16_BE(p, 2);
Jerry Yuc1be19f2022-04-23 16:11:39 +0800900 p += 4;
Jerry Yu086edc22022-05-05 10:50:38 +0800901 key_exchange = p;
Gilles Peskine449bd832023-01-11 14:50:10 +0100902 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, client_shares_end, key_exchange_len);
Jerry Yu086edc22022-05-05 10:50:38 +0800903 p += key_exchange_len;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000904
905 /* Continue parsing even if we have already found a match,
XiaokangQianc5763b52022-04-02 03:34:37 +0000906 * for input validation purposes.
907 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100908 if (!mbedtls_ssl_named_group_is_offered(ssl, group) ||
909 !mbedtls_ssl_named_group_is_supported(group) ||
910 ssl->handshake->offered_group_id != 0) {
XiaokangQian060d8672022-04-21 09:24:56 +0000911 continue;
912 }
XiaokangQian060d8672022-04-21 09:24:56 +0000913
XiaokangQian7807f9f2022-02-15 10:04:37 +0000914 /*
Przemek Stekielc89f3ea2023-05-18 15:45:53 +0200915 * ECDHE and FFDHE groups are supported
XiaokangQian7807f9f2022-02-15 10:04:37 +0000916 */
Przemek Stekielc89f3ea2023-05-18 15:45:53 +0200917 if (mbedtls_ssl_tls13_named_group_is_ecdhe(group) ||
Przemek Stekield5f79e72023-06-29 09:08:43 +0200918 mbedtls_ssl_tls13_named_group_is_ffdh(group)) {
Przemek Stekielc89f3ea2023-05-18 15:45:53 +0200919 MBEDTLS_SSL_DEBUG_MSG(2, ("ECDH/FFDH group: %s (%04x)",
Gilles Peskine449bd832023-01-11 14:50:10 +0100920 mbedtls_ssl_named_group_to_str(group),
921 group));
Przemek Stekiel7ac93be2023-07-04 10:02:38 +0200922 ret = mbedtls_ssl_tls13_read_public_xxdhe_share(
Gilles Peskine449bd832023-01-11 14:50:10 +0100923 ssl, key_exchange - 2, key_exchange_len + 2);
924 if (ret != 0) {
925 return ret;
926 }
XiaokangQian4e8cd7b2022-04-21 09:48:09 +0000927
Gilles Peskine449bd832023-01-11 14:50:10 +0100928 } else {
929 MBEDTLS_SSL_DEBUG_MSG(4, ("Unrecognized NamedGroup %u",
930 (unsigned) group));
XiaokangQian7807f9f2022-02-15 10:04:37 +0000931 continue;
932 }
933
XiaokangQian9b5d04b2022-04-10 10:20:43 +0000934 ssl->handshake->offered_group_id = group;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000935 }
936
Jerry Yuc1be19f2022-04-23 16:11:39 +0800937
Gilles Peskine449bd832023-01-11 14:50:10 +0100938 if (ssl->handshake->offered_group_id == 0) {
939 MBEDTLS_SSL_DEBUG_MSG(1, ("no matching key share"));
940 return SSL_TLS1_3_PARSE_KEY_SHARES_EXT_NO_MATCH;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000941 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100942 return 0;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000943}
Valerio Settic9ae8622023-07-25 11:23:50 +0200944#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_EPHEMERAL_ENABLED */
XiaokangQian7807f9f2022-02-15 10:04:37 +0000945
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200946MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100947static int ssl_tls13_client_hello_has_exts(mbedtls_ssl_context *ssl,
948 int exts_mask)
XiaokangQian7807f9f2022-02-15 10:04:37 +0000949{
Jerry Yu0c354a22022-08-29 15:25:36 +0800950 int masked = ssl->handshake->received_extensions & exts_mask;
Gilles Peskine449bd832023-01-11 14:50:10 +0100951 return masked == exts_mask;
XiaokangQian7807f9f2022-02-15 10:04:37 +0000952}
953
Jerry Yue5991322022-11-07 14:03:44 +0800954#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200955MBEDTLS_CHECK_RETURN_CRITICAL
XiaokangQianb67384d2022-04-19 00:02:38 +0000956static int ssl_tls13_client_hello_has_exts_for_ephemeral_key_exchange(
Gilles Peskine449bd832023-01-11 14:50:10 +0100957 mbedtls_ssl_context *ssl)
XiaokangQian7807f9f2022-02-15 10:04:37 +0000958{
Gilles Peskine449bd832023-01-11 14:50:10 +0100959 return ssl_tls13_client_hello_has_exts(
960 ssl,
961 MBEDTLS_SSL_EXT_MASK(SUPPORTED_GROUPS) |
962 MBEDTLS_SSL_EXT_MASK(KEY_SHARE) |
963 MBEDTLS_SSL_EXT_MASK(SIG_ALG));
XiaokangQian7807f9f2022-02-15 10:04:37 +0000964}
Jerry Yue5991322022-11-07 14:03:44 +0800965#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED */
XiaokangQian7807f9f2022-02-15 10:04:37 +0000966
Jerry Yue5991322022-11-07 14:03:44 +0800967#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK_ENABLED)
Jerry Yu77f01482022-07-11 07:03:24 +0000968MBEDTLS_CHECK_RETURN_CRITICAL
969static int ssl_tls13_client_hello_has_exts_for_psk_key_exchange(
Gilles Peskine449bd832023-01-11 14:50:10 +0100970 mbedtls_ssl_context *ssl)
Jerry Yu77f01482022-07-11 07:03:24 +0000971{
Gilles Peskine449bd832023-01-11 14:50:10 +0100972 return ssl_tls13_client_hello_has_exts(
973 ssl,
974 MBEDTLS_SSL_EXT_MASK(PRE_SHARED_KEY) |
975 MBEDTLS_SSL_EXT_MASK(PSK_KEY_EXCHANGE_MODES));
Jerry Yu77f01482022-07-11 07:03:24 +0000976}
Jerry Yue5991322022-11-07 14:03:44 +0800977#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK_ENABLED */
Jerry Yu77f01482022-07-11 07:03:24 +0000978
Jerry Yue5991322022-11-07 14:03:44 +0800979#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK_EPHEMERAL_ENABLED)
Jerry Yu77f01482022-07-11 07:03:24 +0000980MBEDTLS_CHECK_RETURN_CRITICAL
981static int ssl_tls13_client_hello_has_exts_for_psk_ephemeral_key_exchange(
Gilles Peskine449bd832023-01-11 14:50:10 +0100982 mbedtls_ssl_context *ssl)
Jerry Yu77f01482022-07-11 07:03:24 +0000983{
Gilles Peskine449bd832023-01-11 14:50:10 +0100984 return ssl_tls13_client_hello_has_exts(
985 ssl,
986 MBEDTLS_SSL_EXT_MASK(SUPPORTED_GROUPS) |
987 MBEDTLS_SSL_EXT_MASK(KEY_SHARE) |
988 MBEDTLS_SSL_EXT_MASK(PRE_SHARED_KEY) |
989 MBEDTLS_SSL_EXT_MASK(PSK_KEY_EXCHANGE_MODES));
Jerry Yu77f01482022-07-11 07:03:24 +0000990}
Jerry Yue5991322022-11-07 14:03:44 +0800991#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK_EPHEMERAL_ENABLED */
Jerry Yu77f01482022-07-11 07:03:24 +0000992
Pengyu Lv306a01d2023-02-02 16:35:47 +0800993#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED)
994MBEDTLS_CHECK_RETURN_CRITICAL
Pengyu Lv52ad3332023-02-14 14:32:37 +0800995static int ssl_tls13_ticket_permission_check(mbedtls_ssl_context *ssl,
996 unsigned int kex_mode)
Pengyu Lv306a01d2023-02-02 16:35:47 +0800997{
998#if defined(MBEDTLS_SSL_SESSION_TICKETS)
999 if (ssl->handshake->resume) {
Pengyu Lv7b711712023-10-24 17:07:14 +08001000 if (mbedtls_ssl_session_check_ticket_flags(
Pengyu Lv306a01d2023-02-02 16:35:47 +08001001 ssl->session_negotiate, kex_mode)) {
1002 return 0;
1003 }
1004 }
1005#else
1006 ((void) ssl);
1007 ((void) kex_mode);
1008#endif
1009 return 1;
1010}
1011#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED */
1012
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001013MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01001014static int ssl_tls13_check_ephemeral_key_exchange(mbedtls_ssl_context *ssl)
XiaokangQian7807f9f2022-02-15 10:04:37 +00001015{
Jerry Yue5991322022-11-07 14:03:44 +08001016#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01001017 return mbedtls_ssl_conf_tls13_ephemeral_enabled(ssl) &&
1018 ssl_tls13_client_hello_has_exts_for_ephemeral_key_exchange(ssl);
Jerry Yue5991322022-11-07 14:03:44 +08001019#else
1020 ((void) ssl);
Gilles Peskine449bd832023-01-11 14:50:10 +01001021 return 0;
Jerry Yue5991322022-11-07 14:03:44 +08001022#endif
Jerry Yu77f01482022-07-11 07:03:24 +00001023}
XiaokangQian7807f9f2022-02-15 10:04:37 +00001024
Jerry Yu77f01482022-07-11 07:03:24 +00001025MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01001026static int ssl_tls13_check_psk_key_exchange(mbedtls_ssl_context *ssl)
Jerry Yu77f01482022-07-11 07:03:24 +00001027{
Jerry Yue5991322022-11-07 14:03:44 +08001028#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK_ENABLED)
Pengyu Lv52ad3332023-02-14 14:32:37 +08001029 return ssl_tls13_ticket_permission_check(
Pengyu Lv306a01d2023-02-02 16:35:47 +08001030 ssl, MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK) &&
1031 mbedtls_ssl_conf_tls13_psk_enabled(ssl) &&
Gilles Peskine449bd832023-01-11 14:50:10 +01001032 mbedtls_ssl_tls13_psk_enabled(ssl) &&
1033 ssl_tls13_client_hello_has_exts_for_psk_key_exchange(ssl);
Jerry Yu77f01482022-07-11 07:03:24 +00001034#else
1035 ((void) ssl);
Gilles Peskine449bd832023-01-11 14:50:10 +01001036 return 0;
Jerry Yu77f01482022-07-11 07:03:24 +00001037#endif
1038}
XiaokangQian7807f9f2022-02-15 10:04:37 +00001039
Jerry Yu77f01482022-07-11 07:03:24 +00001040MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01001041static int ssl_tls13_check_psk_ephemeral_key_exchange(mbedtls_ssl_context *ssl)
Jerry Yu77f01482022-07-11 07:03:24 +00001042{
Jerry Yue5991322022-11-07 14:03:44 +08001043#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK_EPHEMERAL_ENABLED)
Pengyu Lv52ad3332023-02-14 14:32:37 +08001044 return ssl_tls13_ticket_permission_check(
Pengyu Lv306a01d2023-02-02 16:35:47 +08001045 ssl, MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK_EPHEMERAL) &&
1046 mbedtls_ssl_conf_tls13_psk_ephemeral_enabled(ssl) &&
Gilles Peskine449bd832023-01-11 14:50:10 +01001047 mbedtls_ssl_tls13_psk_ephemeral_enabled(ssl) &&
1048 ssl_tls13_client_hello_has_exts_for_psk_ephemeral_key_exchange(ssl);
Jerry Yu77f01482022-07-11 07:03:24 +00001049#else
1050 ((void) ssl);
Gilles Peskine449bd832023-01-11 14:50:10 +01001051 return 0;
Jerry Yu77f01482022-07-11 07:03:24 +00001052#endif
1053}
1054
Gilles Peskine449bd832023-01-11 14:50:10 +01001055static int ssl_tls13_determine_key_exchange_mode(mbedtls_ssl_context *ssl)
Jerry Yu77f01482022-07-11 07:03:24 +00001056{
1057 /*
1058 * Determine the key exchange algorithm to use.
1059 * There are three types of key exchanges supported in TLS 1.3:
1060 * - (EC)DH with ECDSA,
1061 * - (EC)DH with PSK,
1062 * - plain PSK.
1063 *
1064 * The PSK-based key exchanges may additionally be used with 0-RTT.
1065 *
1066 * Our built-in order of preference is
Jerry Yuce6ed702022-07-22 21:49:53 +08001067 * 1 ) (EC)DHE-PSK Mode ( psk_ephemeral )
1068 * 2 ) Certificate Mode ( ephemeral )
1069 * 3 ) Plain PSK Mode ( psk )
Jerry Yu77f01482022-07-11 07:03:24 +00001070 */
1071
Xiaokang Qian9f1747b2023-03-30 02:50:04 +00001072 ssl->handshake->key_exchange_mode =
1073 MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_NONE;
Jerry Yu77f01482022-07-11 07:03:24 +00001074
Gilles Peskine449bd832023-01-11 14:50:10 +01001075 if (ssl_tls13_check_psk_ephemeral_key_exchange(ssl)) {
Jerry Yu77f01482022-07-11 07:03:24 +00001076 ssl->handshake->key_exchange_mode =
1077 MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK_EPHEMERAL;
Gilles Peskine449bd832023-01-11 14:50:10 +01001078 MBEDTLS_SSL_DEBUG_MSG(2, ("key exchange mode: psk_ephemeral"));
1079 } else
1080 if (ssl_tls13_check_ephemeral_key_exchange(ssl)) {
Jerry Yu77f01482022-07-11 07:03:24 +00001081 ssl->handshake->key_exchange_mode =
1082 MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL;
Gilles Peskine449bd832023-01-11 14:50:10 +01001083 MBEDTLS_SSL_DEBUG_MSG(2, ("key exchange mode: ephemeral"));
1084 } else
1085 if (ssl_tls13_check_psk_key_exchange(ssl)) {
Jerry Yuce6ed702022-07-22 21:49:53 +08001086 ssl->handshake->key_exchange_mode =
1087 MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK;
Gilles Peskine449bd832023-01-11 14:50:10 +01001088 MBEDTLS_SSL_DEBUG_MSG(2, ("key exchange mode: psk"));
1089 } else {
Jerry Yu77f01482022-07-11 07:03:24 +00001090 MBEDTLS_SSL_DEBUG_MSG(
Gilles Peskine449bd832023-01-11 14:50:10 +01001091 1,
1092 ("ClientHello message misses mandatory extensions."));
1093 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_MISSING_EXTENSION,
1094 MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER);
1095 return MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER;
Jerry Yu77f01482022-07-11 07:03:24 +00001096 }
1097
Gilles Peskine449bd832023-01-11 14:50:10 +01001098 return 0;
Jerry Yu77f01482022-07-11 07:03:24 +00001099
XiaokangQian7807f9f2022-02-15 10:04:37 +00001100}
1101
XiaokangQian81802f42022-06-10 13:25:22 +00001102#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
Ronald Cron928cbd32022-10-04 16:14:26 +02001103 defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED)
Ronald Cron67ea2542022-09-15 17:34:42 +02001104
1105#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01001106static psa_algorithm_t ssl_tls13_iana_sig_alg_to_psa_alg(uint16_t sig_alg)
Ronald Cron67ea2542022-09-15 17:34:42 +02001107{
Gilles Peskine449bd832023-01-11 14:50:10 +01001108 switch (sig_alg) {
Ronald Cron67ea2542022-09-15 17:34:42 +02001109 case MBEDTLS_TLS1_3_SIG_ECDSA_SECP256R1_SHA256:
Gilles Peskine449bd832023-01-11 14:50:10 +01001110 return PSA_ALG_ECDSA(PSA_ALG_SHA_256);
Ronald Cron67ea2542022-09-15 17:34:42 +02001111 case MBEDTLS_TLS1_3_SIG_ECDSA_SECP384R1_SHA384:
Gilles Peskine449bd832023-01-11 14:50:10 +01001112 return PSA_ALG_ECDSA(PSA_ALG_SHA_384);
Ronald Cron67ea2542022-09-15 17:34:42 +02001113 case MBEDTLS_TLS1_3_SIG_ECDSA_SECP521R1_SHA512:
Gilles Peskine449bd832023-01-11 14:50:10 +01001114 return PSA_ALG_ECDSA(PSA_ALG_SHA_512);
Ronald Cron67ea2542022-09-15 17:34:42 +02001115 case MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA256:
Gilles Peskine449bd832023-01-11 14:50:10 +01001116 return PSA_ALG_RSA_PSS(PSA_ALG_SHA_256);
Ronald Cron67ea2542022-09-15 17:34:42 +02001117 case MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA384:
Gilles Peskine449bd832023-01-11 14:50:10 +01001118 return PSA_ALG_RSA_PSS(PSA_ALG_SHA_384);
Ronald Cron67ea2542022-09-15 17:34:42 +02001119 case MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA512:
Gilles Peskine449bd832023-01-11 14:50:10 +01001120 return PSA_ALG_RSA_PSS(PSA_ALG_SHA_512);
Ronald Cron67ea2542022-09-15 17:34:42 +02001121 case MBEDTLS_TLS1_3_SIG_RSA_PKCS1_SHA256:
Gilles Peskine449bd832023-01-11 14:50:10 +01001122 return PSA_ALG_RSA_PKCS1V15_SIGN(PSA_ALG_SHA_256);
Ronald Cron67ea2542022-09-15 17:34:42 +02001123 case MBEDTLS_TLS1_3_SIG_RSA_PKCS1_SHA384:
Gilles Peskine449bd832023-01-11 14:50:10 +01001124 return PSA_ALG_RSA_PKCS1V15_SIGN(PSA_ALG_SHA_384);
Ronald Cron67ea2542022-09-15 17:34:42 +02001125 case MBEDTLS_TLS1_3_SIG_RSA_PKCS1_SHA512:
Gilles Peskine449bd832023-01-11 14:50:10 +01001126 return PSA_ALG_RSA_PKCS1V15_SIGN(PSA_ALG_SHA_512);
Ronald Cron67ea2542022-09-15 17:34:42 +02001127 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01001128 return PSA_ALG_NONE;
Ronald Cron67ea2542022-09-15 17:34:42 +02001129 }
1130}
1131#endif /* MBEDTLS_USE_PSA_CRYPTO */
1132
XiaokangQian23c5be62022-06-07 02:04:34 +00001133/*
XiaokangQianfb665a82022-06-15 03:57:21 +00001134 * Pick best ( private key, certificate chain ) pair based on the signature
1135 * algorithms supported by the client.
XiaokangQian23c5be62022-06-07 02:04:34 +00001136 */
Ronald Cronce7d76e2022-07-08 18:56:49 +02001137MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01001138static int ssl_tls13_pick_key_cert(mbedtls_ssl_context *ssl)
XiaokangQian23c5be62022-06-07 02:04:34 +00001139{
XiaokangQianfb665a82022-06-15 03:57:21 +00001140 mbedtls_ssl_key_cert *key_cert, *key_cert_list;
XiaokangQian81802f42022-06-10 13:25:22 +00001141 const uint16_t *sig_alg = ssl->handshake->received_sig_algs;
XiaokangQian23c5be62022-06-07 02:04:34 +00001142
1143#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Gilles Peskine449bd832023-01-11 14:50:10 +01001144 if (ssl->handshake->sni_key_cert != NULL) {
XiaokangQianfb665a82022-06-15 03:57:21 +00001145 key_cert_list = ssl->handshake->sni_key_cert;
Gilles Peskine449bd832023-01-11 14:50:10 +01001146 } else
XiaokangQian23c5be62022-06-07 02:04:34 +00001147#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
Gilles Peskine449bd832023-01-11 14:50:10 +01001148 key_cert_list = ssl->conf->key_cert;
XiaokangQian23c5be62022-06-07 02:04:34 +00001149
Gilles Peskine449bd832023-01-11 14:50:10 +01001150 if (key_cert_list == NULL) {
1151 MBEDTLS_SSL_DEBUG_MSG(3, ("server has no certificate"));
1152 return -1;
XiaokangQian23c5be62022-06-07 02:04:34 +00001153 }
1154
Gilles Peskine449bd832023-01-11 14:50:10 +01001155 for (; *sig_alg != MBEDTLS_TLS1_3_SIG_NONE; sig_alg++) {
1156 if (!mbedtls_ssl_sig_alg_is_offered(ssl, *sig_alg)) {
Ronald Cron67ea2542022-09-15 17:34:42 +02001157 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +01001158 }
Ronald Cron67ea2542022-09-15 17:34:42 +02001159
Gilles Peskine449bd832023-01-11 14:50:10 +01001160 if (!mbedtls_ssl_tls13_sig_alg_for_cert_verify_is_supported(*sig_alg)) {
Ronald Cron67ea2542022-09-15 17:34:42 +02001161 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +01001162 }
Ronald Cron67ea2542022-09-15 17:34:42 +02001163
Gilles Peskine449bd832023-01-11 14:50:10 +01001164 for (key_cert = key_cert_list; key_cert != NULL;
1165 key_cert = key_cert->next) {
Ronald Cron67ea2542022-09-15 17:34:42 +02001166#if defined(MBEDTLS_USE_PSA_CRYPTO)
1167 psa_algorithm_t psa_alg = PSA_ALG_NONE;
1168#endif /* MBEDTLS_USE_PSA_CRYPTO */
1169
Gilles Peskine449bd832023-01-11 14:50:10 +01001170 MBEDTLS_SSL_DEBUG_CRT(3, "certificate (chain) candidate",
1171 key_cert->cert);
XiaokangQian81802f42022-06-10 13:25:22 +00001172
1173 /*
Gilles Peskine449bd832023-01-11 14:50:10 +01001174 * This avoids sending the client a cert it'll reject based on
1175 * keyUsage or other extensions.
1176 */
1177 if (mbedtls_x509_crt_check_key_usage(
1178 key_cert->cert, MBEDTLS_X509_KU_DIGITAL_SIGNATURE) != 0 ||
XiaokangQian81802f42022-06-10 13:25:22 +00001179 mbedtls_x509_crt_check_extended_key_usage(
XiaokangQianfb665a82022-06-15 03:57:21 +00001180 key_cert->cert, MBEDTLS_OID_SERVER_AUTH,
Gilles Peskine449bd832023-01-11 14:50:10 +01001181 MBEDTLS_OID_SIZE(MBEDTLS_OID_SERVER_AUTH)) != 0) {
1182 MBEDTLS_SSL_DEBUG_MSG(3, ("certificate mismatch: "
1183 "(extended) key usage extension"));
XiaokangQian81802f42022-06-10 13:25:22 +00001184 continue;
1185 }
XiaokangQian23c5be62022-06-07 02:04:34 +00001186
Gilles Peskine449bd832023-01-11 14:50:10 +01001187 MBEDTLS_SSL_DEBUG_MSG(3,
1188 ("ssl_tls13_pick_key_cert:"
1189 "check signature algorithm %s [%04x]",
1190 mbedtls_ssl_sig_alg_to_str(*sig_alg),
1191 *sig_alg));
Ronald Cron67ea2542022-09-15 17:34:42 +02001192#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01001193 psa_alg = ssl_tls13_iana_sig_alg_to_psa_alg(*sig_alg);
Ronald Cron67ea2542022-09-15 17:34:42 +02001194#endif /* MBEDTLS_USE_PSA_CRYPTO */
1195
Gilles Peskine449bd832023-01-11 14:50:10 +01001196 if (mbedtls_ssl_tls13_check_sig_alg_cert_key_match(
1197 *sig_alg, &key_cert->cert->pk)
Ronald Cron67ea2542022-09-15 17:34:42 +02001198#if defined(MBEDTLS_USE_PSA_CRYPTO)
1199 && psa_alg != PSA_ALG_NONE &&
Gilles Peskine449bd832023-01-11 14:50:10 +01001200 mbedtls_pk_can_do_ext(&key_cert->cert->pk, psa_alg,
1201 PSA_KEY_USAGE_SIGN_HASH) == 1
Ronald Cron67ea2542022-09-15 17:34:42 +02001202#endif /* MBEDTLS_USE_PSA_CRYPTO */
Gilles Peskine449bd832023-01-11 14:50:10 +01001203 ) {
XiaokangQianfb665a82022-06-15 03:57:21 +00001204 ssl->handshake->key_cert = key_cert;
Gilles Peskine449bd832023-01-11 14:50:10 +01001205 MBEDTLS_SSL_DEBUG_MSG(3,
1206 ("ssl_tls13_pick_key_cert:"
1207 "selected signature algorithm"
1208 " %s [%04x]",
1209 mbedtls_ssl_sig_alg_to_str(*sig_alg),
1210 *sig_alg));
XiaokangQianfb665a82022-06-15 03:57:21 +00001211 MBEDTLS_SSL_DEBUG_CRT(
Gilles Peskine449bd832023-01-11 14:50:10 +01001212 3, "selected certificate (chain)",
1213 ssl->handshake->key_cert->cert);
1214 return 0;
XiaokangQian81802f42022-06-10 13:25:22 +00001215 }
XiaokangQian81802f42022-06-10 13:25:22 +00001216 }
XiaokangQian23c5be62022-06-07 02:04:34 +00001217 }
1218
Gilles Peskine449bd832023-01-11 14:50:10 +01001219 MBEDTLS_SSL_DEBUG_MSG(2, ("ssl_tls13_pick_key_cert:"
1220 "no suitable certificate found"));
1221 return -1;
XiaokangQian23c5be62022-06-07 02:04:34 +00001222}
XiaokangQian81802f42022-06-10 13:25:22 +00001223#endif /* MBEDTLS_X509_CRT_PARSE_C &&
Ronald Cron928cbd32022-10-04 16:14:26 +02001224 MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED */
XiaokangQian23c5be62022-06-07 02:04:34 +00001225
XiaokangQian4080a7f2022-04-11 09:55:18 +00001226/*
XiaokangQianed582dd2022-04-13 08:21:05 +00001227 *
1228 * STATE HANDLING: ClientHello
1229 *
XiaokangQianb67384d2022-04-19 00:02:38 +00001230 * There are three possible classes of outcomes when parsing the ClientHello:
XiaokangQianed582dd2022-04-13 08:21:05 +00001231 *
XiaokangQianb67384d2022-04-19 00:02:38 +00001232 * 1) The ClientHello was well-formed and matched the server's configuration.
XiaokangQianed582dd2022-04-13 08:21:05 +00001233 *
1234 * In this case, the server progresses to sending its ServerHello.
1235 *
XiaokangQianb67384d2022-04-19 00:02:38 +00001236 * 2) The ClientHello was well-formed but didn't match the server's
1237 * configuration.
XiaokangQianed582dd2022-04-13 08:21:05 +00001238 *
1239 * For example, the client might not have offered a key share which
1240 * the server supports, or the server might require a cookie.
1241 *
1242 * In this case, the server sends a HelloRetryRequest.
1243 *
XiaokangQianb67384d2022-04-19 00:02:38 +00001244 * 3) The ClientHello was ill-formed
XiaokangQianed582dd2022-04-13 08:21:05 +00001245 *
1246 * In this case, we abort the handshake.
1247 *
1248 */
1249
1250/*
XiaokangQian4080a7f2022-04-11 09:55:18 +00001251 * Structure of this message:
1252 *
XiaokangQiane8ff3502022-04-22 02:34:40 +00001253 * uint16 ProtocolVersion;
1254 * opaque Random[32];
1255 * uint8 CipherSuite[2]; // Cryptographic suite selector
XiaokangQian4080a7f2022-04-11 09:55:18 +00001256 *
XiaokangQiane8ff3502022-04-22 02:34:40 +00001257 * struct {
1258 * ProtocolVersion legacy_version = 0x0303; // TLS v1.2
1259 * Random random;
1260 * opaque legacy_session_id<0..32>;
1261 * CipherSuite cipher_suites<2..2^16-2>;
1262 * opaque legacy_compression_methods<1..2^8-1>;
1263 * Extension extensions<8..2^16-1>;
1264 * } ClientHello;
XiaokangQian4080a7f2022-04-11 09:55:18 +00001265 */
XiaokangQianed582dd2022-04-13 08:21:05 +00001266
1267#define SSL_CLIENT_HELLO_OK 0
1268#define SSL_CLIENT_HELLO_HRR_REQUIRED 1
Ronald Cron5af4c7f2023-03-07 20:46:59 +01001269#define SSL_CLIENT_HELLO_TLS1_2 2
XiaokangQianed582dd2022-04-13 08:21:05 +00001270
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001271MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01001272static int ssl_tls13_parse_client_hello(mbedtls_ssl_context *ssl,
1273 const unsigned char *buf,
1274 const unsigned char *end)
XiaokangQian7807f9f2022-02-15 10:04:37 +00001275{
XiaokangQianb67384d2022-04-19 00:02:38 +00001276 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1277 const unsigned char *p = buf;
Ronald Crond540d992023-03-07 09:41:48 +01001278 const unsigned char *random;
XiaokangQian4080a7f2022-04-11 09:55:18 +00001279 size_t legacy_session_id_len;
Ronald Croncada4102023-03-07 09:51:39 +01001280 const unsigned char *legacy_session_id;
XiaokangQian318dc762022-04-20 09:43:51 +00001281 size_t cipher_suites_len;
Ronald Cron2f16b4e2023-03-07 10:07:32 +01001282 const unsigned char *cipher_suites;
XiaokangQian060d8672022-04-21 09:24:56 +00001283 const unsigned char *cipher_suites_end;
XiaokangQianb67384d2022-04-19 00:02:38 +00001284 size_t extensions_len;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001285 const unsigned char *extensions_end;
Ronald Croneff56732023-04-03 17:36:31 +02001286 const unsigned char *supported_versions_data;
1287 const unsigned char *supported_versions_data_end;
Jerry Yuc4bf5d62022-10-29 09:08:47 +08001288 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
Jerry Yu49ca9282022-05-05 11:05:22 +08001289 int hrr_required = 0;
Ronald Cron8a74f072023-06-14 17:59:29 +02001290 int no_usable_share_for_key_agreement = 0;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001291
Ronald Cron41a443a2022-10-04 16:38:25 +02001292#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED)
Jerry Yu29d9faa2022-08-23 17:52:45 +08001293 const unsigned char *pre_shared_key_ext = NULL;
Jerry Yu1c105562022-07-10 06:32:38 +00001294 const unsigned char *pre_shared_key_ext_end = NULL;
Ronald Cron41a443a2022-10-04 16:38:25 +02001295#endif
XiaokangQian7807f9f2022-02-15 10:04:37 +00001296
XiaokangQian7807f9f2022-02-15 10:04:37 +00001297 /*
XiaokangQianb67384d2022-04-19 00:02:38 +00001298 * ClientHello layout:
XiaokangQian7807f9f2022-02-15 10:04:37 +00001299 * 0 . 1 protocol version
XiaokangQiane8ff3502022-04-22 02:34:40 +00001300 * 2 . 33 random bytes
XiaokangQianc5763b52022-04-02 03:34:37 +00001301 * 34 . 34 session id length ( 1 byte )
XiaokangQian7807f9f2022-02-15 10:04:37 +00001302 * 35 . 34+x session id
XiaokangQian7807f9f2022-02-15 10:04:37 +00001303 * .. . .. ciphersuite list length ( 2 bytes )
1304 * .. . .. ciphersuite list
1305 * .. . .. compression alg. list length ( 1 byte )
1306 * .. . .. compression alg. list
1307 * .. . .. extensions length ( 2 bytes, optional )
1308 * .. . .. extensions ( optional )
1309 */
1310
XiaokangQianb67384d2022-04-19 00:02:38 +00001311 /*
bootstrap-prime6dbbf442022-05-17 19:30:44 -04001312 * Minimal length ( with everything empty and extensions omitted ) is
XiaokangQian7807f9f2022-02-15 10:04:37 +00001313 * 2 + 32 + 1 + 2 + 1 = 38 bytes. Check that first, so that we can
1314 * read at least up to session id length without worrying.
1315 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001316 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, 38);
XiaokangQian7807f9f2022-02-15 10:04:37 +00001317
1318 /* ...
1319 * ProtocolVersion legacy_version = 0x0303; // TLS 1.2
1320 * ...
1321 * with ProtocolVersion defined as:
1322 * uint16 ProtocolVersion;
1323 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001324 if (mbedtls_ssl_read_version(p, ssl->conf->transport) !=
1325 MBEDTLS_SSL_VERSION_TLS1_2) {
1326 MBEDTLS_SSL_DEBUG_MSG(1, ("Unsupported version of TLS."));
1327 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_PROTOCOL_VERSION,
1328 MBEDTLS_ERR_SSL_BAD_PROTOCOL_VERSION);
1329 return MBEDTLS_ERR_SSL_BAD_PROTOCOL_VERSION;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001330 }
1331 p += 2;
1332
Jerry Yuc1be19f2022-04-23 16:11:39 +08001333 /* ...
1334 * Random random;
1335 * ...
XiaokangQianb67384d2022-04-19 00:02:38 +00001336 * with Random defined as:
1337 * opaque Random[32];
XiaokangQian7807f9f2022-02-15 10:04:37 +00001338 */
Ronald Crond540d992023-03-07 09:41:48 +01001339 random = p;
XiaokangQian08037552022-04-20 07:16:41 +00001340 p += MBEDTLS_CLIENT_HELLO_RANDOM_LEN;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001341
Jerry Yuc1be19f2022-04-23 16:11:39 +08001342 /* ...
XiaokangQianb67384d2022-04-19 00:02:38 +00001343 * opaque legacy_session_id<0..32>;
Jerry Yuc1be19f2022-04-23 16:11:39 +08001344 * ...
XiaokangQian7807f9f2022-02-15 10:04:37 +00001345 */
Ronald Croncada4102023-03-07 09:51:39 +01001346 legacy_session_id_len = *(p++);
1347 legacy_session_id = p;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001348
XiaokangQianb67384d2022-04-19 00:02:38 +00001349 /*
1350 * Check we have enough data for the legacy session identifier
Jerry Yue95c8af2022-07-26 15:48:20 +08001351 * and the ciphersuite list length.
XiaokangQianb67384d2022-04-19 00:02:38 +00001352 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001353 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, legacy_session_id_len + 2);
XiaokangQian4080a7f2022-04-11 09:55:18 +00001354 p += legacy_session_id_len;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001355
Ronald Cron2f16b4e2023-03-07 10:07:32 +01001356 /* ...
1357 * CipherSuite cipher_suites<2..2^16-2>;
1358 * ...
1359 * with CipherSuite defined as:
1360 * uint8 CipherSuite[2];
1361 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001362 cipher_suites_len = MBEDTLS_GET_UINT16_BE(p, 0);
XiaokangQian7807f9f2022-02-15 10:04:37 +00001363 p += 2;
Ronald Cron2f16b4e2023-03-07 10:07:32 +01001364 cipher_suites = p;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001365
Ronald Cronfc7ae872023-02-16 15:32:19 +01001366 /*
1367 * The length of the ciphersuite list has to be even.
1368 */
1369 if (cipher_suites_len & 1) {
1370 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR,
1371 MBEDTLS_ERR_SSL_DECODE_ERROR);
1372 return MBEDTLS_ERR_SSL_DECODE_ERROR;
1373 }
1374
XiaokangQianb67384d2022-04-19 00:02:38 +00001375 /* Check we have enough data for the ciphersuite list, the legacy
1376 * compression methods and the length of the extensions.
Jerry Yue95c8af2022-07-26 15:48:20 +08001377 *
1378 * cipher_suites cipher_suites_len bytes
1379 * legacy_compression_methods 2 bytes
1380 * extensions_len 2 bytes
XiaokangQianb67384d2022-04-19 00:02:38 +00001381 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001382 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, cipher_suites_len + 2 + 2);
Ronald Cron8c527d02023-03-07 15:47:47 +01001383 p += cipher_suites_len;
1384 cipher_suites_end = p;
Jerry Yu5725f1c2022-08-21 17:27:16 +08001385
1386 /*
Ronald Cron8c527d02023-03-07 15:47:47 +01001387 * Search for the supported versions extension and parse it to determine
1388 * if the client supports TLS 1.3.
1389 */
1390 ret = mbedtls_ssl_tls13_is_supported_versions_ext_present_in_exts(
1391 ssl, p + 2, end,
Ronald Croneff56732023-04-03 17:36:31 +02001392 &supported_versions_data, &supported_versions_data_end);
Ronald Cron8c527d02023-03-07 15:47:47 +01001393 if (ret < 0) {
1394 MBEDTLS_SSL_DEBUG_RET(1,
1395 ("mbedtls_ssl_tls13_is_supported_versions_ext_present_in_exts"), ret);
1396 return ret;
1397 }
1398
1399 if (ret == 0) {
Ronald Cron5af4c7f2023-03-07 20:46:59 +01001400 return SSL_CLIENT_HELLO_TLS1_2;
Ronald Cron8c527d02023-03-07 15:47:47 +01001401 }
1402
Ronald Cron5af4c7f2023-03-07 20:46:59 +01001403 if (ret == 1) {
1404 ret = ssl_tls13_parse_supported_versions_ext(ssl,
Ronald Croneff56732023-04-03 17:36:31 +02001405 supported_versions_data,
1406 supported_versions_data_end);
Ronald Cron5af4c7f2023-03-07 20:46:59 +01001407 if (ret < 0) {
1408 MBEDTLS_SSL_DEBUG_RET(1,
1409 ("ssl_tls13_parse_supported_versions_ext"), ret);
1410 return ret;
1411 }
1412
Ronald Cronb828c7d2023-04-03 16:37:22 +02001413 /*
1414 * The supported versions extension was parsed successfully as the
1415 * value returned by ssl_tls13_parse_supported_versions_ext() is
1416 * positive. The return value is then equal to
1417 * MBEDTLS_SSL_VERSION_TLS1_2 or MBEDTLS_SSL_VERSION_TLS1_3, defining
1418 * the TLS version to negotiate.
1419 */
Ronald Cron5af4c7f2023-03-07 20:46:59 +01001420 if (MBEDTLS_SSL_VERSION_TLS1_2 == ret) {
1421 return SSL_CLIENT_HELLO_TLS1_2;
1422 }
Ronald Cron8c527d02023-03-07 15:47:47 +01001423 }
1424
1425 /*
1426 * We negotiate TLS 1.3.
Ronald Cron64582392023-03-07 09:21:40 +01001427 */
1428 ssl->tls_version = MBEDTLS_SSL_VERSION_TLS1_3;
1429
1430#if defined(MBEDTLS_SSL_SESSION_TICKETS)
1431 /* Store minor version for later use with ticket serialization. */
1432 ssl->session_negotiate->tls_version = MBEDTLS_SSL_VERSION_TLS1_3;
1433 ssl->session_negotiate->endpoint = ssl->conf->endpoint;
1434#endif
1435
1436 /*
Ronald Crondad02b22023-04-06 09:57:52 +02001437 * We are negotiating the version 1.3 of the protocol. Do what we have
Ronald Croncada4102023-03-07 09:51:39 +01001438 * postponed: copy of the client random bytes, copy of the legacy session
Ronald Cron2f16b4e2023-03-07 10:07:32 +01001439 * identifier and selection of the TLS 1.3 cipher suite.
Ronald Crond540d992023-03-07 09:41:48 +01001440 */
1441 MBEDTLS_SSL_DEBUG_BUF(3, "client hello, random bytes",
1442 random, MBEDTLS_CLIENT_HELLO_RANDOM_LEN);
1443 memcpy(&handshake->randbytes[0], random, MBEDTLS_CLIENT_HELLO_RANDOM_LEN);
1444
Ronald Croncada4102023-03-07 09:51:39 +01001445 if (legacy_session_id_len > sizeof(ssl->session_negotiate->id)) {
1446 MBEDTLS_SSL_DEBUG_MSG(1, ("bad client hello message"));
1447 return MBEDTLS_ERR_SSL_DECODE_ERROR;
1448 }
1449 ssl->session_negotiate->id_len = legacy_session_id_len;
1450 MBEDTLS_SSL_DEBUG_BUF(3, "client hello, session id",
1451 legacy_session_id, legacy_session_id_len);
1452 memcpy(&ssl->session_negotiate->id[0],
1453 legacy_session_id, legacy_session_id_len);
1454
Ronald Crond540d992023-03-07 09:41:48 +01001455 /*
Jerry Yu5725f1c2022-08-21 17:27:16 +08001456 * Search for a matching ciphersuite
1457 */
Ronald Cron2f16b4e2023-03-07 10:07:32 +01001458 MBEDTLS_SSL_DEBUG_BUF(3, "client hello, list of cipher suites",
1459 cipher_suites, cipher_suites_len);
Ronald Crone45afd72023-04-04 15:10:06 +02001460 for (const unsigned char *cipher_suites_p = cipher_suites;
1461 cipher_suites_p < cipher_suites_end; cipher_suites_p += 2) {
XiaokangQiane8ff3502022-04-22 02:34:40 +00001462 uint16_t cipher_suite;
Gilles Peskine449bd832023-01-11 14:50:10 +01001463 const mbedtls_ssl_ciphersuite_t *ciphersuite_info;
Jerry Yu5725f1c2022-08-21 17:27:16 +08001464
Ronald Crond89360b2023-02-21 08:53:33 +01001465 /*
Ronald Crone45afd72023-04-04 15:10:06 +02001466 * "cipher_suites_end - cipher_suites_p is even" is an invariant of the
1467 * loop. As cipher_suites_end - cipher_suites_p > 0, we have
1468 * cipher_suites_end - cipher_suites_p >= 2 and it is thus safe to read
1469 * two bytes.
Ronald Crond89360b2023-02-21 08:53:33 +01001470 */
Ronald Crone45afd72023-04-04 15:10:06 +02001471 cipher_suite = MBEDTLS_GET_UINT16_BE(cipher_suites_p, 0);
Jerry Yuc5a23a02022-08-25 10:51:44 +08001472 ciphersuite_info = ssl_tls13_validate_peer_ciphersuite(
Gilles Peskine449bd832023-01-11 14:50:10 +01001473 ssl, cipher_suite);
1474 if (ciphersuite_info == NULL) {
Jerry Yu5725f1c2022-08-21 17:27:16 +08001475 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +01001476 }
Jerry Yu5725f1c2022-08-21 17:27:16 +08001477
Jerry Yu5725f1c2022-08-21 17:27:16 +08001478 ssl->session_negotiate->ciphersuite = cipher_suite;
Jerry Yuc4bf5d62022-10-29 09:08:47 +08001479 handshake->ciphersuite_info = ciphersuite_info;
Gilles Peskine449bd832023-01-11 14:50:10 +01001480 MBEDTLS_SSL_DEBUG_MSG(2, ("selected ciphersuite: %04x - %s",
1481 cipher_suite,
1482 ciphersuite_info->name));
Ronald Cron25e9ec62023-02-16 15:35:16 +01001483 break;
XiaokangQian17f974c2022-04-19 09:57:41 +00001484 }
Jerry Yu5725f1c2022-08-21 17:27:16 +08001485
Gilles Peskine449bd832023-01-11 14:50:10 +01001486 if (handshake->ciphersuite_info == NULL) {
1487 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE,
1488 MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE);
1489 return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
Jerry Yu5725f1c2022-08-21 17:27:16 +08001490 }
Jerry Yuc1be19f2022-04-23 16:11:39 +08001491
XiaokangQian4080a7f2022-04-11 09:55:18 +00001492 /* ...
XiaokangQianb67384d2022-04-19 00:02:38 +00001493 * opaque legacy_compression_methods<1..2^8-1>;
XiaokangQian4080a7f2022-04-11 09:55:18 +00001494 * ...
XiaokangQian7807f9f2022-02-15 10:04:37 +00001495 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001496 if (p[0] != 1 || p[1] != MBEDTLS_SSL_COMPRESS_NULL) {
1497 MBEDTLS_SSL_DEBUG_MSG(1, ("bad legacy compression method"));
1498 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER,
1499 MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER);
1500 return MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001501 }
XiaokangQianb67384d2022-04-19 00:02:38 +00001502 p += 2;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001503
Jerry Yuc1be19f2022-04-23 16:11:39 +08001504 /* ...
XiaokangQianb67384d2022-04-19 00:02:38 +00001505 * Extension extensions<8..2^16-1>;
Jerry Yuc1be19f2022-04-23 16:11:39 +08001506 * ...
XiaokangQianb67384d2022-04-19 00:02:38 +00001507 * with Extension defined as:
1508 * struct {
1509 * ExtensionType extension_type;
1510 * opaque extension_data<0..2^16-1>;
1511 * } Extension;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001512 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001513 extensions_len = MBEDTLS_GET_UINT16_BE(p, 0);
XiaokangQian7807f9f2022-02-15 10:04:37 +00001514 p += 2;
Gilles Peskine449bd832023-01-11 14:50:10 +01001515 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, extensions_len);
XiaokangQiane8ff3502022-04-22 02:34:40 +00001516 extensions_end = p + extensions_len;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001517
Gilles Peskine449bd832023-01-11 14:50:10 +01001518 MBEDTLS_SSL_DEBUG_BUF(3, "client hello extensions", p, extensions_len);
Jerry Yu63a459c2022-10-31 13:38:40 +08001519 handshake->received_extensions = MBEDTLS_SSL_EXT_MASK_NONE;
Jerry Yu0c354a22022-08-29 15:25:36 +08001520
Gilles Peskine449bd832023-01-11 14:50:10 +01001521 while (p < extensions_end) {
XiaokangQian7807f9f2022-02-15 10:04:37 +00001522 unsigned int extension_type;
1523 size_t extension_data_len;
1524 const unsigned char *extension_data_end;
1525
Jerry Yu0c354a22022-08-29 15:25:36 +08001526 /* RFC 8446, section 4.2.11
Jerry Yu1c9247c2022-07-21 12:37:39 +08001527 *
1528 * The "pre_shared_key" extension MUST be the last extension in the
1529 * ClientHello (this facilitates implementation as described below).
1530 * Servers MUST check that it is the last extension and otherwise fail
1531 * the handshake with an "illegal_parameter" alert.
1532 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001533 if (handshake->received_extensions & MBEDTLS_SSL_EXT_MASK(PRE_SHARED_KEY)) {
Jerry Yu1c9247c2022-07-21 12:37:39 +08001534 MBEDTLS_SSL_DEBUG_MSG(
Gilles Peskine449bd832023-01-11 14:50:10 +01001535 3, ("pre_shared_key is not last extension."));
Jerry Yu1c9247c2022-07-21 12:37:39 +08001536 MBEDTLS_SSL_PEND_FATAL_ALERT(
1537 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER,
Gilles Peskine449bd832023-01-11 14:50:10 +01001538 MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER);
1539 return MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER;
Jerry Yu1c9247c2022-07-21 12:37:39 +08001540 }
1541
Gilles Peskine449bd832023-01-11 14:50:10 +01001542 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, extensions_end, 4);
1543 extension_type = MBEDTLS_GET_UINT16_BE(p, 0);
1544 extension_data_len = MBEDTLS_GET_UINT16_BE(p, 2);
XiaokangQian7807f9f2022-02-15 10:04:37 +00001545 p += 4;
1546
Gilles Peskine449bd832023-01-11 14:50:10 +01001547 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, extensions_end, extension_data_len);
XiaokangQian7807f9f2022-02-15 10:04:37 +00001548 extension_data_end = p + extension_data_len;
1549
Jerry Yuc4bf5d62022-10-29 09:08:47 +08001550 ret = mbedtls_ssl_tls13_check_received_extension(
Gilles Peskine449bd832023-01-11 14:50:10 +01001551 ssl, MBEDTLS_SSL_HS_CLIENT_HELLO, extension_type,
1552 MBEDTLS_SSL_TLS1_3_ALLOWED_EXTS_OF_CH);
1553 if (ret != 0) {
1554 return ret;
1555 }
Jerry Yue18dc7e2022-08-04 16:29:22 +08001556
Gilles Peskine449bd832023-01-11 14:50:10 +01001557 switch (extension_type) {
XiaokangQian40a35232022-05-07 09:02:40 +00001558#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
1559 case MBEDTLS_TLS_EXT_SERVERNAME:
Gilles Peskine449bd832023-01-11 14:50:10 +01001560 MBEDTLS_SSL_DEBUG_MSG(3, ("found ServerName extension"));
1561 ret = mbedtls_ssl_parse_server_name_ext(ssl, p,
1562 extension_data_end);
1563 if (ret != 0) {
XiaokangQian40a35232022-05-07 09:02:40 +00001564 MBEDTLS_SSL_DEBUG_RET(
Gilles Peskine449bd832023-01-11 14:50:10 +01001565 1, "mbedtls_ssl_parse_servername_ext", ret);
1566 return ret;
XiaokangQian40a35232022-05-07 09:02:40 +00001567 }
XiaokangQian40a35232022-05-07 09:02:40 +00001568 break;
1569#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
1570
Przemek Stekiel8c0a9532023-06-15 16:48:19 +02001571#if defined(PSA_WANT_ALG_ECDH) || defined(PSA_WANT_ALG_FFDH)
XiaokangQian7807f9f2022-02-15 10:04:37 +00001572 case MBEDTLS_TLS_EXT_SUPPORTED_GROUPS:
Gilles Peskine449bd832023-01-11 14:50:10 +01001573 MBEDTLS_SSL_DEBUG_MSG(3, ("found supported group extension"));
XiaokangQian7807f9f2022-02-15 10:04:37 +00001574
1575 /* Supported Groups Extension
1576 *
1577 * When sent by the client, the "supported_groups" extension
1578 * indicates the named groups which the client supports,
1579 * ordered from most preferred to least preferred.
1580 */
Jerry Yuc1be19f2022-04-23 16:11:39 +08001581 ret = ssl_tls13_parse_supported_groups_ext(
Gilles Peskine449bd832023-01-11 14:50:10 +01001582 ssl, p, extension_data_end);
1583 if (ret != 0) {
Xiaokang Qian9f1747b2023-03-30 02:50:04 +00001584 MBEDTLS_SSL_DEBUG_RET(
Xiaokang Qian8bce0e62023-04-04 10:15:48 +00001585 1, "ssl_tls13_parse_supported_groups_ext", ret);
Gilles Peskine449bd832023-01-11 14:50:10 +01001586 return ret;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001587 }
1588
XiaokangQian7807f9f2022-02-15 10:04:37 +00001589 break;
Przemek Stekiel8c0a9532023-06-15 16:48:19 +02001590#endif /* PSA_WANT_ALG_ECDH || PSA_WANT_ALG_FFDH*/
XiaokangQian7807f9f2022-02-15 10:04:37 +00001591
Valerio Settic9ae8622023-07-25 11:23:50 +02001592#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_EPHEMERAL_ENABLED)
XiaokangQian7807f9f2022-02-15 10:04:37 +00001593 case MBEDTLS_TLS_EXT_KEY_SHARE:
Gilles Peskine449bd832023-01-11 14:50:10 +01001594 MBEDTLS_SSL_DEBUG_MSG(3, ("found key share extension"));
XiaokangQian7807f9f2022-02-15 10:04:37 +00001595
1596 /*
1597 * Key Share Extension
1598 *
1599 * When sent by the client, the "key_share" extension
1600 * contains the endpoint's cryptographic parameters for
1601 * ECDHE/DHE key establishment methods.
1602 */
Jerry Yuc1be19f2022-04-23 16:11:39 +08001603 ret = ssl_tls13_parse_key_shares_ext(
Gilles Peskine449bd832023-01-11 14:50:10 +01001604 ssl, p, extension_data_end);
1605 if (ret == SSL_TLS1_3_PARSE_KEY_SHARES_EXT_NO_MATCH) {
Ronald Cron8a74f072023-06-14 17:59:29 +02001606 MBEDTLS_SSL_DEBUG_MSG(2, ("No usable share for key agreement."));
1607 no_usable_share_for_key_agreement = 1;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001608 }
1609
Gilles Peskine449bd832023-01-11 14:50:10 +01001610 if (ret < 0) {
Jerry Yu582dd062022-04-22 21:59:01 +08001611 MBEDTLS_SSL_DEBUG_RET(
Gilles Peskine449bd832023-01-11 14:50:10 +01001612 1, "ssl_tls13_parse_key_shares_ext", ret);
1613 return ret;
Jerry Yu582dd062022-04-22 21:59:01 +08001614 }
XiaokangQian7807f9f2022-02-15 10:04:37 +00001615
XiaokangQian7807f9f2022-02-15 10:04:37 +00001616 break;
Valerio Settic9ae8622023-07-25 11:23:50 +02001617#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_EPHEMERAL_ENABLED */
XiaokangQian7807f9f2022-02-15 10:04:37 +00001618
1619 case MBEDTLS_TLS_EXT_SUPPORTED_VERSIONS:
Ronald Cron8c527d02023-03-07 15:47:47 +01001620 /* Already parsed */
XiaokangQian7807f9f2022-02-15 10:04:37 +00001621 break;
1622
Ronald Cron41a443a2022-10-04 16:38:25 +02001623#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED)
Jerry Yue19e3b92022-07-08 12:04:51 +00001624 case MBEDTLS_TLS_EXT_PSK_KEY_EXCHANGE_MODES:
Xiaokang Qian9f1747b2023-03-30 02:50:04 +00001625 MBEDTLS_SSL_DEBUG_MSG(
1626 3, ("found psk key exchange modes extension"));
Jerry Yue19e3b92022-07-08 12:04:51 +00001627
1628 ret = ssl_tls13_parse_key_exchange_modes_ext(
Gilles Peskine449bd832023-01-11 14:50:10 +01001629 ssl, p, extension_data_end);
1630 if (ret != 0) {
Jerry Yue19e3b92022-07-08 12:04:51 +00001631 MBEDTLS_SSL_DEBUG_RET(
Gilles Peskine449bd832023-01-11 14:50:10 +01001632 1, "ssl_tls13_parse_key_exchange_modes_ext", ret);
1633 return ret;
Jerry Yue19e3b92022-07-08 12:04:51 +00001634 }
1635
Jerry Yue19e3b92022-07-08 12:04:51 +00001636 break;
Ronald Cron41a443a2022-10-04 16:38:25 +02001637#endif
Jerry Yue19e3b92022-07-08 12:04:51 +00001638
Jerry Yu1c105562022-07-10 06:32:38 +00001639 case MBEDTLS_TLS_EXT_PRE_SHARED_KEY:
Gilles Peskine449bd832023-01-11 14:50:10 +01001640 MBEDTLS_SSL_DEBUG_MSG(3, ("found pre_shared_key extension"));
1641 if ((handshake->received_extensions &
1642 MBEDTLS_SSL_EXT_MASK(PSK_KEY_EXCHANGE_MODES)) == 0) {
Jerry Yu13ab81d2022-07-22 23:17:11 +08001643 MBEDTLS_SSL_PEND_FATAL_ALERT(
1644 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER,
Gilles Peskine449bd832023-01-11 14:50:10 +01001645 MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER);
1646 return MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER;
Jerry Yu13ab81d2022-07-22 23:17:11 +08001647 }
Ronald Cron41a443a2022-10-04 16:38:25 +02001648#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED)
Jerry Yu1c105562022-07-10 06:32:38 +00001649 /* Delay processing of the PSK identity once we have
1650 * found out which algorithms to use. We keep a pointer
1651 * to the buffer and the size for later processing.
1652 */
Jerry Yu29d9faa2022-08-23 17:52:45 +08001653 pre_shared_key_ext = p;
Jerry Yu1c105562022-07-10 06:32:38 +00001654 pre_shared_key_ext_end = extension_data_end;
Jerry Yue18dc7e2022-08-04 16:29:22 +08001655#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED */
Jerry Yu1c105562022-07-10 06:32:38 +00001656 break;
Jerry Yu1c105562022-07-10 06:32:38 +00001657
XiaokangQianacb39922022-06-17 10:18:48 +00001658#if defined(MBEDTLS_SSL_ALPN)
1659 case MBEDTLS_TLS_EXT_ALPN:
Gilles Peskine449bd832023-01-11 14:50:10 +01001660 MBEDTLS_SSL_DEBUG_MSG(3, ("found alpn extension"));
XiaokangQianacb39922022-06-17 10:18:48 +00001661
Gilles Peskine449bd832023-01-11 14:50:10 +01001662 ret = mbedtls_ssl_parse_alpn_ext(ssl, p, extension_data_end);
1663 if (ret != 0) {
XiaokangQianacb39922022-06-17 10:18:48 +00001664 MBEDTLS_SSL_DEBUG_RET(
Gilles Peskine449bd832023-01-11 14:50:10 +01001665 1, ("mbedtls_ssl_parse_alpn_ext"), ret);
1666 return ret;
XiaokangQianacb39922022-06-17 10:18:48 +00001667 }
XiaokangQianacb39922022-06-17 10:18:48 +00001668 break;
1669#endif /* MBEDTLS_SSL_ALPN */
1670
Ronald Cron928cbd32022-10-04 16:14:26 +02001671#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED)
XiaokangQian7807f9f2022-02-15 10:04:37 +00001672 case MBEDTLS_TLS_EXT_SIG_ALG:
Gilles Peskine449bd832023-01-11 14:50:10 +01001673 MBEDTLS_SSL_DEBUG_MSG(3, ("found signature_algorithms extension"));
XiaokangQian7807f9f2022-02-15 10:04:37 +00001674
Gabor Mezei078e8032022-04-27 21:17:56 +02001675 ret = mbedtls_ssl_parse_sig_alg_ext(
Gilles Peskine449bd832023-01-11 14:50:10 +01001676 ssl, p, extension_data_end);
1677 if (ret != 0) {
Xiaokang Qian49f39c12023-04-06 02:31:02 +00001678 MBEDTLS_SSL_DEBUG_RET(
1679 1, "mbedtls_ssl_parse_sig_alg_ext", ret);
Gilles Peskine449bd832023-01-11 14:50:10 +01001680 return ret;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001681 }
XiaokangQian7807f9f2022-02-15 10:04:37 +00001682 break;
Ronald Cron928cbd32022-10-04 16:14:26 +02001683#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED */
XiaokangQian7807f9f2022-02-15 10:04:37 +00001684
Jan Bruckner151f6422023-02-10 12:45:19 +01001685#if defined(MBEDTLS_SSL_RECORD_SIZE_LIMIT)
1686 case MBEDTLS_TLS_EXT_RECORD_SIZE_LIMIT:
1687 MBEDTLS_SSL_DEBUG_MSG(3, ("found record_size_limit extension"));
1688
Xiaokang Qian9f1747b2023-03-30 02:50:04 +00001689 ret = mbedtls_ssl_tls13_parse_record_size_limit_ext(
1690 ssl, p, extension_data_end);
Jan Bruckner151f6422023-02-10 12:45:19 +01001691
Xiaokang Qian09c3ccc2023-04-04 10:18:38 +00001692 /*
1693 * TODO: Return unconditionally here until we handle the record
1694 * size limit correctly.
1695 * Once handled correctly, only return in case of errors.
1696 */
Jan Bruckner151f6422023-02-10 12:45:19 +01001697 return ret;
1698
1699 break;
1700#endif /* MBEDTLS_SSL_RECORD_SIZE_LIMIT */
1701
XiaokangQian7807f9f2022-02-15 10:04:37 +00001702 default:
Jerry Yu79aa7212022-11-08 21:30:21 +08001703 MBEDTLS_SSL_PRINT_EXT(
Jerry Yu63a459c2022-10-31 13:38:40 +08001704 3, MBEDTLS_SSL_HS_CLIENT_HELLO,
Gilles Peskine449bd832023-01-11 14:50:10 +01001705 extension_type, "( ignored )");
Jerry Yu0c354a22022-08-29 15:25:36 +08001706 break;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001707 }
1708
1709 p += extension_data_len;
1710 }
1711
Gilles Peskine449bd832023-01-11 14:50:10 +01001712 MBEDTLS_SSL_PRINT_EXTS(3, MBEDTLS_SSL_HS_CLIENT_HELLO,
1713 handshake->received_extensions);
Jerry Yue18dc7e2022-08-04 16:29:22 +08001714
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +01001715 ret = mbedtls_ssl_add_hs_hdr_to_checksum(ssl,
1716 MBEDTLS_SSL_HS_CLIENT_HELLO,
1717 p - buf);
1718 if (0 != ret) {
1719 MBEDTLS_SSL_DEBUG_RET(1, ("mbedtls_ssl_add_hs_hdr_to_checksum"), ret);
1720 return ret;
1721 }
Jerry Yu032b15ce2022-07-11 06:10:03 +00001722
Ronald Cron41a443a2022-10-04 16:38:25 +02001723#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED)
XiaokangQian7807f9f2022-02-15 10:04:37 +00001724 /* Update checksum with either
1725 * - The entire content of the CH message, if no PSK extension is present
1726 * - The content up to but excluding the PSK extension, if present.
1727 */
Jerry Yu1c105562022-07-10 06:32:38 +00001728 /* If we've settled on a PSK-based exchange, parse PSK identity ext */
Pengyu Lvcfb23b82023-10-30 15:26:26 +08001729 if (ssl_tls13_check_psk_key_exchange(ssl) ||
1730 ssl_tls13_check_psk_ephemeral_key_exchange(ssl)) {
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +01001731 ret = handshake->update_checksum(ssl, buf,
1732 pre_shared_key_ext - buf);
1733 if (0 != ret) {
1734 MBEDTLS_SSL_DEBUG_RET(1, ("update_checksum"), ret);
1735 return ret;
1736 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001737 ret = ssl_tls13_parse_pre_shared_key_ext(ssl,
1738 pre_shared_key_ext,
1739 pre_shared_key_ext_end,
1740 cipher_suites,
1741 cipher_suites_end);
1742 if (ret == MBEDTLS_ERR_SSL_UNKNOWN_IDENTITY) {
1743 handshake->received_extensions &= ~MBEDTLS_SSL_EXT_MASK(PRE_SHARED_KEY);
1744 } else if (ret != 0) {
Jerry Yu63a459c2022-10-31 13:38:40 +08001745 MBEDTLS_SSL_DEBUG_RET(
Gilles Peskine449bd832023-01-11 14:50:10 +01001746 1, "ssl_tls13_parse_pre_shared_key_ext", ret);
1747 return ret;
Jerry Yu1c105562022-07-10 06:32:38 +00001748 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001749 } else
Ronald Cron41a443a2022-10-04 16:38:25 +02001750#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED */
Jerry Yu1c105562022-07-10 06:32:38 +00001751 {
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +01001752 ret = handshake->update_checksum(ssl, buf, p - buf);
1753 if (0 != ret) {
1754 MBEDTLS_SSL_DEBUG_RET(1, ("update_checksum"), ret);
1755 return ret;
1756 }
Jerry Yu1c105562022-07-10 06:32:38 +00001757 }
XiaokangQian7807f9f2022-02-15 10:04:37 +00001758
Gilles Peskine449bd832023-01-11 14:50:10 +01001759 ret = ssl_tls13_determine_key_exchange_mode(ssl);
1760 if (ret < 0) {
1761 return ret;
1762 }
XiaokangQian7807f9f2022-02-15 10:04:37 +00001763
Ronald Cron8a74f072023-06-14 17:59:29 +02001764 if (ssl->handshake->key_exchange_mode !=
1765 MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK) {
1766 hrr_required = (no_usable_share_for_key_agreement != 0);
1767 }
1768
Gilles Peskine449bd832023-01-11 14:50:10 +01001769 mbedtls_ssl_optimize_checksum(ssl, handshake->ciphersuite_info);
Jerry Yue5834fd2022-08-29 20:16:09 +08001770
Gilles Peskine449bd832023-01-11 14:50:10 +01001771 return hrr_required ? SSL_CLIENT_HELLO_HRR_REQUIRED : SSL_CLIENT_HELLO_OK;
XiaokangQian75fe8c72022-06-15 09:42:45 +00001772}
1773
Jerry Yuab0da372023-02-08 13:55:24 +08001774#if defined(MBEDTLS_SSL_EARLY_DATA)
1775static void ssl_tls13_update_early_data_status(mbedtls_ssl_context *ssl)
1776{
1777 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
1778
1779 if ((handshake->received_extensions &
1780 MBEDTLS_SSL_EXT_MASK(EARLY_DATA)) == 0) {
1781 MBEDTLS_SSL_DEBUG_MSG(
Jerry Yub47b2992023-10-18 15:50:35 +08001782 1, ("EarlyData: no early data extension received."));
Jerry Yuab0da372023-02-08 13:55:24 +08001783 ssl->early_data_status = MBEDTLS_SSL_EARLY_DATA_STATUS_NOT_RECEIVED;
1784 return;
1785 }
1786
Jerry Yub47b2992023-10-18 15:50:35 +08001787 /* We do not accept early data for the time being */
Jerry Yuab0da372023-02-08 13:55:24 +08001788 ssl->early_data_status = MBEDTLS_SSL_EARLY_DATA_STATUS_REJECTED;
1789
Jerry Yuab0da372023-02-08 13:55:24 +08001790}
1791#endif /* MBEDTLS_SSL_EARLY_DATA */
1792
XiaokangQian75fe8c72022-06-15 09:42:45 +00001793/* Update the handshake state machine */
1794
Ronald Cronce7d76e2022-07-08 18:56:49 +02001795MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01001796static int ssl_tls13_postprocess_client_hello(mbedtls_ssl_context *ssl)
XiaokangQian75fe8c72022-06-15 09:42:45 +00001797{
1798 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1799
XiaokangQian7807f9f2022-02-15 10:04:37 +00001800 /*
XiaokangQianfb665a82022-06-15 03:57:21 +00001801 * Server certificate selection
1802 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001803 if (ssl->conf->f_cert_cb && (ret = ssl->conf->f_cert_cb(ssl)) != 0) {
1804 MBEDTLS_SSL_DEBUG_RET(1, "f_cert_cb", ret);
1805 return ret;
XiaokangQianfb665a82022-06-15 03:57:21 +00001806 }
1807#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
1808 ssl->handshake->sni_name = NULL;
1809 ssl->handshake->sni_name_len = 0;
1810#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
XiaokangQian7807f9f2022-02-15 10:04:37 +00001811
Gilles Peskine449bd832023-01-11 14:50:10 +01001812 ret = mbedtls_ssl_tls13_key_schedule_stage_early(ssl);
1813 if (ret != 0) {
1814 MBEDTLS_SSL_DEBUG_RET(1,
1815 "mbedtls_ssl_tls1_3_key_schedule_stage_early", ret);
1816 return ret;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001817 }
1818
Jerry Yuab0da372023-02-08 13:55:24 +08001819#if defined(MBEDTLS_SSL_EARLY_DATA)
1820 /* There is enough information, update early data state. */
1821 ssl_tls13_update_early_data_status(ssl);
Jerry Yuab0da372023-02-08 13:55:24 +08001822#endif /* MBEDTLS_SSL_EARLY_DATA */
1823
Gilles Peskine449bd832023-01-11 14:50:10 +01001824 return 0;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001825
1826}
1827
1828/*
XiaokangQianed582dd2022-04-13 08:21:05 +00001829 * Main entry point from the state machine; orchestrates the otherfunctions.
1830 */
1831
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001832MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01001833static int ssl_tls13_process_client_hello(mbedtls_ssl_context *ssl)
XiaokangQianed582dd2022-04-13 08:21:05 +00001834{
1835
XiaokangQian08037552022-04-20 07:16:41 +00001836 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine449bd832023-01-11 14:50:10 +01001837 unsigned char *buf = NULL;
XiaokangQianed582dd2022-04-13 08:21:05 +00001838 size_t buflen = 0;
Jerry Yu4ca91402022-05-09 15:50:57 +08001839 int parse_client_hello_ret;
1840
Gilles Peskine449bd832023-01-11 14:50:10 +01001841 MBEDTLS_SSL_DEBUG_MSG(2, ("=> parse client hello"));
XiaokangQianed582dd2022-04-13 08:21:05 +00001842
Gilles Peskine449bd832023-01-11 14:50:10 +01001843 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_tls13_fetch_handshake_msg(
1844 ssl, MBEDTLS_SSL_HS_CLIENT_HELLO,
1845 &buf, &buflen));
XiaokangQianed582dd2022-04-13 08:21:05 +00001846
Gilles Peskine449bd832023-01-11 14:50:10 +01001847 MBEDTLS_SSL_PROC_CHK_NEG(ssl_tls13_parse_client_hello(ssl, buf,
1848 buf + buflen));
Ronald Cron5af4c7f2023-03-07 20:46:59 +01001849 parse_client_hello_ret = ret; /* Store positive return value of
1850 * parse_client_hello,
1851 * as negative error codes are handled
Jerry Yuf41553b2022-05-09 22:20:30 +08001852 * by MBEDTLS_SSL_PROC_CHK_NEG. */
Jerry Yu4ca91402022-05-09 15:50:57 +08001853
Ronald Cronb828c7d2023-04-03 16:37:22 +02001854 /*
1855 * Version 1.2 of the protocol has been chosen, set the
1856 * ssl->keep_current_message flag for the ClientHello to be kept and parsed
1857 * as a TLS 1.2 ClientHello. We also change ssl->tls_version to
1858 * MBEDTLS_SSL_VERSION_TLS1_2 thus from now on mbedtls_ssl_handshake_step()
1859 * will dispatch to the TLS 1.2 state machine.
1860 */
Ronald Cron5af4c7f2023-03-07 20:46:59 +01001861 if (SSL_CLIENT_HELLO_TLS1_2 == parse_client_hello_ret) {
1862 ssl->keep_current_message = 1;
1863 ssl->tls_version = MBEDTLS_SSL_VERSION_TLS1_2;
1864 return 0;
1865 }
1866
Gilles Peskine449bd832023-01-11 14:50:10 +01001867 MBEDTLS_SSL_PROC_CHK(ssl_tls13_postprocess_client_hello(ssl));
Jerry Yu582dd062022-04-22 21:59:01 +08001868
Ronald Cron5af4c7f2023-03-07 20:46:59 +01001869 if (SSL_CLIENT_HELLO_OK == parse_client_hello_ret) {
Gilles Peskine449bd832023-01-11 14:50:10 +01001870 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_SERVER_HELLO);
1871 } else {
1872 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_HELLO_RETRY_REQUEST);
1873 }
XiaokangQianed582dd2022-04-13 08:21:05 +00001874
1875cleanup:
1876
Gilles Peskine449bd832023-01-11 14:50:10 +01001877 MBEDTLS_SSL_DEBUG_MSG(2, ("<= parse client hello"));
1878 return ret;
XiaokangQianed582dd2022-04-13 08:21:05 +00001879}
1880
1881/*
Jerry Yu1c3e6882022-04-20 21:23:40 +08001882 * Handler for MBEDTLS_SSL_SERVER_HELLO
Jerry Yu5b64ae92022-03-30 17:15:02 +08001883 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001884MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01001885static int ssl_tls13_prepare_server_hello(mbedtls_ssl_context *ssl)
Jerry Yuf4b27e42022-03-30 17:32:21 +08001886{
Jerry Yu637a3f12022-04-20 21:37:58 +08001887 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1888 unsigned char *server_randbytes =
Gilles Peskine449bd832023-01-11 14:50:10 +01001889 ssl->handshake->randbytes + MBEDTLS_CLIENT_HELLO_RANDOM_LEN;
1890 if (ssl->conf->f_rng == NULL) {
1891 MBEDTLS_SSL_DEBUG_MSG(1, ("no RNG provided"));
1892 return MBEDTLS_ERR_SSL_NO_RNG;
Jerry Yuf4b27e42022-03-30 17:32:21 +08001893 }
1894
Gilles Peskine449bd832023-01-11 14:50:10 +01001895 if ((ret = ssl->conf->f_rng(ssl->conf->p_rng, server_randbytes,
1896 MBEDTLS_SERVER_HELLO_RANDOM_LEN)) != 0) {
1897 MBEDTLS_SSL_DEBUG_RET(1, "f_rng", ret);
1898 return ret;
Jerry Yuf4b27e42022-03-30 17:32:21 +08001899 }
1900
Gilles Peskine449bd832023-01-11 14:50:10 +01001901 MBEDTLS_SSL_DEBUG_BUF(3, "server hello, random bytes", server_randbytes,
1902 MBEDTLS_SERVER_HELLO_RANDOM_LEN);
Jerry Yuf4b27e42022-03-30 17:32:21 +08001903
1904#if defined(MBEDTLS_HAVE_TIME)
YxCda609132023-05-22 12:08:12 -07001905 ssl->session_negotiate->start = mbedtls_time(NULL);
Jerry Yuf4b27e42022-03-30 17:32:21 +08001906#endif /* MBEDTLS_HAVE_TIME */
1907
Gilles Peskine449bd832023-01-11 14:50:10 +01001908 return ret;
Jerry Yuf4b27e42022-03-30 17:32:21 +08001909}
1910
Jerry Yu3bf2c642022-03-30 22:02:12 +08001911/*
Jerry Yue74e04a2022-04-21 09:23:16 +08001912 * ssl_tls13_write_server_hello_supported_versions_ext ():
Jerry Yu3bf2c642022-03-30 22:02:12 +08001913 *
1914 * struct {
Jerry Yufb9f54d2022-04-06 10:08:34 +08001915 * ProtocolVersion selected_version;
Jerry Yu3bf2c642022-03-30 22:02:12 +08001916 * } SupportedVersions;
1917 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001918MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yue74e04a2022-04-21 09:23:16 +08001919static int ssl_tls13_write_server_hello_supported_versions_ext(
Gilles Peskine449bd832023-01-11 14:50:10 +01001920 mbedtls_ssl_context *ssl,
1921 unsigned char *buf,
1922 unsigned char *end,
1923 size_t *out_len)
Jerry Yu3bf2c642022-03-30 22:02:12 +08001924{
Jerry Yu3bf2c642022-03-30 22:02:12 +08001925 *out_len = 0;
1926
Gilles Peskine449bd832023-01-11 14:50:10 +01001927 MBEDTLS_SSL_DEBUG_MSG(3, ("server hello, write selected version"));
Jerry Yu3bf2c642022-03-30 22:02:12 +08001928
1929 /* Check if we have space to write the extension:
1930 * - extension_type (2 bytes)
1931 * - extension_data_length (2 bytes)
Jerry Yu349a6132022-04-14 20:52:56 +08001932 * - selected_version (2 bytes)
Jerry Yu3bf2c642022-03-30 22:02:12 +08001933 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001934 MBEDTLS_SSL_CHK_BUF_PTR(buf, end, 6);
Jerry Yu3bf2c642022-03-30 22:02:12 +08001935
Gilles Peskine449bd832023-01-11 14:50:10 +01001936 MBEDTLS_PUT_UINT16_BE(MBEDTLS_TLS_EXT_SUPPORTED_VERSIONS, buf, 0);
Jerry Yu3bf2c642022-03-30 22:02:12 +08001937
Gilles Peskine449bd832023-01-11 14:50:10 +01001938 MBEDTLS_PUT_UINT16_BE(2, buf, 2);
Jerry Yu3bf2c642022-03-30 22:02:12 +08001939
Gilles Peskine449bd832023-01-11 14:50:10 +01001940 mbedtls_ssl_write_version(buf + 4,
1941 ssl->conf->transport,
1942 ssl->tls_version);
Jerry Yu3bf2c642022-03-30 22:02:12 +08001943
Gilles Peskine449bd832023-01-11 14:50:10 +01001944 MBEDTLS_SSL_DEBUG_MSG(3, ("supported version: [%04x]",
1945 ssl->tls_version));
Jerry Yu3bf2c642022-03-30 22:02:12 +08001946
Jerry Yu349a6132022-04-14 20:52:56 +08001947 *out_len = 6;
Jerry Yu3bf2c642022-03-30 22:02:12 +08001948
Jerry Yub95dd362022-11-08 21:19:34 +08001949 mbedtls_ssl_tls13_set_hs_sent_ext_mask(
Gilles Peskine449bd832023-01-11 14:50:10 +01001950 ssl, MBEDTLS_TLS_EXT_SUPPORTED_VERSIONS);
Jerry Yub95dd362022-11-08 21:19:34 +08001951
Gilles Peskine449bd832023-01-11 14:50:10 +01001952 return 0;
Jerry Yu3bf2c642022-03-30 22:02:12 +08001953}
1954
Jerry Yud9436a12022-04-20 22:28:09 +08001955
Jerry Yu3bf2c642022-03-30 22:02:12 +08001956
1957/* Generate and export a single key share. For hybrid KEMs, this can
1958 * be called multiple times with the different components of the hybrid. */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001959MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01001960static int ssl_tls13_generate_and_write_key_share(mbedtls_ssl_context *ssl,
1961 uint16_t named_group,
1962 unsigned char *buf,
1963 unsigned char *end,
1964 size_t *out_len)
Jerry Yu3bf2c642022-03-30 22:02:12 +08001965{
1966 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yu955ddd72022-04-22 22:27:33 +08001967
1968 *out_len = 0;
1969
Valerio Settic9ae8622023-07-25 11:23:50 +02001970#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_EPHEMERAL_ENABLED)
Przemek Stekiel29c219c2023-05-31 15:21:04 +02001971 if (mbedtls_ssl_tls13_named_group_is_ecdhe(named_group) ||
Przemek Stekield5f79e72023-06-29 09:08:43 +02001972 mbedtls_ssl_tls13_named_group_is_ffdh(named_group)) {
Przemek Stekiel408569f2023-07-06 11:26:44 +02001973 ret = mbedtls_ssl_tls13_generate_and_write_xxdh_key_exchange(
Gilles Peskine449bd832023-01-11 14:50:10 +01001974 ssl, named_group, buf, end, out_len);
1975 if (ret != 0) {
Jerry Yu89e103c2022-03-30 22:43:29 +08001976 MBEDTLS_SSL_DEBUG_RET(
Przemek Stekiel408569f2023-07-06 11:26:44 +02001977 1, "mbedtls_ssl_tls13_generate_and_write_xxdh_key_exchange",
Gilles Peskine449bd832023-01-11 14:50:10 +01001978 ret);
1979 return ret;
Jerry Yu3bf2c642022-03-30 22:02:12 +08001980 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001981 } else
Valerio Settic9ae8622023-07-25 11:23:50 +02001982#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_EPHEMERAL_ENABLED */
Gilles Peskine449bd832023-01-11 14:50:10 +01001983 if (0 /* Other kinds of KEMs */) {
1984 } else {
Jerry Yu955ddd72022-04-22 22:27:33 +08001985 ((void) ssl);
1986 ((void) named_group);
1987 ((void) buf);
1988 ((void) end);
Jerry Yu3bf2c642022-03-30 22:02:12 +08001989 ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
1990 }
1991
Gilles Peskine449bd832023-01-11 14:50:10 +01001992 return ret;
Jerry Yu3bf2c642022-03-30 22:02:12 +08001993}
1994
1995/*
1996 * ssl_tls13_write_key_share_ext
1997 *
1998 * Structure of key_share extension in ServerHello:
1999 *
Jerry Yu1c3e6882022-04-20 21:23:40 +08002000 * struct {
2001 * NamedGroup group;
2002 * opaque key_exchange<1..2^16-1>;
2003 * } KeyShareEntry;
2004 * struct {
2005 * KeyShareEntry server_share;
2006 * } KeyShareServerHello;
Jerry Yu3bf2c642022-03-30 22:02:12 +08002007 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002008MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002009static int ssl_tls13_write_key_share_ext(mbedtls_ssl_context *ssl,
2010 unsigned char *buf,
2011 unsigned char *end,
2012 size_t *out_len)
Jerry Yu3bf2c642022-03-30 22:02:12 +08002013{
Jerry Yu955ddd72022-04-22 22:27:33 +08002014 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yu3bf2c642022-03-30 22:02:12 +08002015 unsigned char *p = buf;
Jerry Yu57d48412022-04-20 21:50:42 +08002016 uint16_t group = ssl->handshake->offered_group_id;
Jerry Yu3bf2c642022-03-30 22:02:12 +08002017 unsigned char *server_share = buf + 4;
Jerry Yu3bf2c642022-03-30 22:02:12 +08002018 size_t key_exchange_length;
Jerry Yu3bf2c642022-03-30 22:02:12 +08002019
2020 *out_len = 0;
2021
Gilles Peskine449bd832023-01-11 14:50:10 +01002022 MBEDTLS_SSL_DEBUG_MSG(3, ("server hello, adding key share extension"));
Jerry Yu3bf2c642022-03-30 22:02:12 +08002023
Gilles Peskine449bd832023-01-11 14:50:10 +01002024 MBEDTLS_SSL_DEBUG_MSG(2, ("server hello, write selected_group: %s (%04x)",
2025 mbedtls_ssl_named_group_to_str(group),
2026 group));
Jerry Yu58af2332022-09-06 11:19:31 +08002027
Jerry Yu3bf2c642022-03-30 22:02:12 +08002028 /* Check if we have space for header and length fields:
2029 * - extension_type (2 bytes)
2030 * - extension_data_length (2 bytes)
2031 * - group (2 bytes)
2032 * - key_exchange_length (2 bytes)
2033 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002034 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 8);
2035 MBEDTLS_PUT_UINT16_BE(MBEDTLS_TLS_EXT_KEY_SHARE, p, 0);
2036 MBEDTLS_PUT_UINT16_BE(group, server_share, 0);
Jerry Yu3bf2c642022-03-30 22:02:12 +08002037 p += 8;
Jerry Yu57d48412022-04-20 21:50:42 +08002038
Jerry Yu3bf2c642022-03-30 22:02:12 +08002039 /* When we introduce PQC-ECDHE hybrids, we'll want to call this
2040 * function multiple times. */
Jerry Yu955ddd72022-04-22 22:27:33 +08002041 ret = ssl_tls13_generate_and_write_key_share(
Gilles Peskine449bd832023-01-11 14:50:10 +01002042 ssl, group, server_share + 4, end, &key_exchange_length);
2043 if (ret != 0) {
2044 return ret;
2045 }
Jerry Yu3bf2c642022-03-30 22:02:12 +08002046 p += key_exchange_length;
Jerry Yuc1be19f2022-04-23 16:11:39 +08002047
Gilles Peskine449bd832023-01-11 14:50:10 +01002048 MBEDTLS_PUT_UINT16_BE(key_exchange_length, server_share + 2, 0);
Jerry Yu3bf2c642022-03-30 22:02:12 +08002049
Gilles Peskine449bd832023-01-11 14:50:10 +01002050 MBEDTLS_PUT_UINT16_BE(p - server_share, buf, 2);
Jerry Yu3bf2c642022-03-30 22:02:12 +08002051
Jerry Yu57d48412022-04-20 21:50:42 +08002052 *out_len = p - buf;
Jerry Yu955ddd72022-04-22 22:27:33 +08002053
Gilles Peskine449bd832023-01-11 14:50:10 +01002054 mbedtls_ssl_tls13_set_hs_sent_ext_mask(ssl, MBEDTLS_TLS_EXT_KEY_SHARE);
Jerry Yub95dd362022-11-08 21:19:34 +08002055
Gilles Peskine449bd832023-01-11 14:50:10 +01002056 return 0;
Jerry Yu3bf2c642022-03-30 22:02:12 +08002057}
Jerry Yud9436a12022-04-20 22:28:09 +08002058
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002059MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002060static int ssl_tls13_write_hrr_key_share_ext(mbedtls_ssl_context *ssl,
2061 unsigned char *buf,
2062 unsigned char *end,
2063 size_t *out_len)
Jerry Yu23f7a6f2022-04-23 15:16:45 +08002064{
2065 uint16_t selected_group = ssl->handshake->hrr_selected_group;
2066 /* key_share Extension
2067 *
2068 * struct {
2069 * select (Handshake.msg_type) {
2070 * ...
2071 * case hello_retry_request:
2072 * NamedGroup selected_group;
2073 * ...
2074 * };
2075 * } KeyShare;
2076 */
2077
2078 *out_len = 0;
2079
Jerry Yub0ac10b2022-05-05 11:10:08 +08002080 /*
2081 * For a pure PSK key exchange, there is no group to agree upon. The purpose
2082 * of the HRR is then to transmit a cookie to force the client to demonstrate
2083 * reachability at their apparent network address (primarily useful for DTLS).
2084 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002085 if (!mbedtls_ssl_tls13_key_exchange_mode_with_ephemeral(ssl)) {
2086 return 0;
2087 }
Jerry Yu23f7a6f2022-04-23 15:16:45 +08002088
2089 /* We should only send the key_share extension if the client's initial
2090 * key share was not acceptable. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002091 if (ssl->handshake->offered_group_id != 0) {
2092 MBEDTLS_SSL_DEBUG_MSG(4, ("Skip key_share extension in HRR"));
2093 return 0;
Jerry Yu23f7a6f2022-04-23 15:16:45 +08002094 }
2095
Gilles Peskine449bd832023-01-11 14:50:10 +01002096 if (selected_group == 0) {
2097 MBEDTLS_SSL_DEBUG_MSG(1, ("no matching named group found"));
2098 return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
Jerry Yu23f7a6f2022-04-23 15:16:45 +08002099 }
2100
Jerry Yub0ac10b2022-05-05 11:10:08 +08002101 /* Check if we have enough space:
2102 * - extension_type (2 bytes)
2103 * - extension_data_length (2 bytes)
2104 * - selected_group (2 bytes)
2105 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002106 MBEDTLS_SSL_CHK_BUF_PTR(buf, end, 6);
Jerry Yu23f7a6f2022-04-23 15:16:45 +08002107
Gilles Peskine449bd832023-01-11 14:50:10 +01002108 MBEDTLS_PUT_UINT16_BE(MBEDTLS_TLS_EXT_KEY_SHARE, buf, 0);
2109 MBEDTLS_PUT_UINT16_BE(2, buf, 2);
2110 MBEDTLS_PUT_UINT16_BE(selected_group, buf, 4);
Jerry Yu23f7a6f2022-04-23 15:16:45 +08002111
Gilles Peskine449bd832023-01-11 14:50:10 +01002112 MBEDTLS_SSL_DEBUG_MSG(3,
2113 ("HRR selected_group: %s (%x)",
2114 mbedtls_ssl_named_group_to_str(selected_group),
2115 selected_group));
Jerry Yu23f7a6f2022-04-23 15:16:45 +08002116
2117 *out_len = 6;
Jerry Yu23f7a6f2022-04-23 15:16:45 +08002118
Gilles Peskine449bd832023-01-11 14:50:10 +01002119 mbedtls_ssl_tls13_set_hs_sent_ext_mask(ssl, MBEDTLS_TLS_EXT_KEY_SHARE);
Jerry Yub95dd362022-11-08 21:19:34 +08002120
Gilles Peskine449bd832023-01-11 14:50:10 +01002121 return 0;
Jerry Yu23f7a6f2022-04-23 15:16:45 +08002122}
Jerry Yu3bf2c642022-03-30 22:02:12 +08002123
2124/*
2125 * Structure of ServerHello message:
2126 *
2127 * struct {
2128 * ProtocolVersion legacy_version = 0x0303; // TLS v1.2
2129 * Random random;
2130 * opaque legacy_session_id_echo<0..32>;
2131 * CipherSuite cipher_suite;
2132 * uint8 legacy_compression_method = 0;
2133 * Extension extensions<6..2^16-1>;
2134 * } ServerHello;
2135 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002136MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002137static int ssl_tls13_write_server_hello_body(mbedtls_ssl_context *ssl,
2138 unsigned char *buf,
2139 unsigned char *end,
2140 size_t *out_len,
2141 int is_hrr)
Jerry Yu56404d72022-03-30 17:36:13 +08002142{
Jerry Yu955ddd72022-04-22 22:27:33 +08002143 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yu3bf2c642022-03-30 22:02:12 +08002144 unsigned char *p = buf;
Jerry Yud9436a12022-04-20 22:28:09 +08002145 unsigned char *p_extensions_len;
Jerry Yufbe3e642022-04-25 19:31:51 +08002146 size_t output_len;
Jerry Yu3bf2c642022-03-30 22:02:12 +08002147
2148 *out_len = 0;
Jerry Yu50e00e32022-10-31 14:45:01 +08002149 ssl->handshake->sent_extensions = MBEDTLS_SSL_EXT_MASK_NONE;
Jerry Yu3bf2c642022-03-30 22:02:12 +08002150
Jerry Yucfc04b32022-04-21 09:31:58 +08002151 /* ...
2152 * ProtocolVersion legacy_version = 0x0303; // TLS 1.2
2153 * ...
2154 * with ProtocolVersion defined as:
2155 * uint16 ProtocolVersion;
Jerry Yu3bf2c642022-03-30 22:02:12 +08002156 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002157 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 2);
2158 MBEDTLS_PUT_UINT16_BE(0x0303, p, 0);
Jerry Yu3bf2c642022-03-30 22:02:12 +08002159 p += 2;
2160
Jerry Yu1c3e6882022-04-20 21:23:40 +08002161 /* ...
2162 * Random random;
2163 * ...
Jerry Yucfc04b32022-04-21 09:31:58 +08002164 * with Random defined as:
2165 * opaque Random[MBEDTLS_SERVER_HELLO_RANDOM_LEN];
Jerry Yu1c3e6882022-04-20 21:23:40 +08002166 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002167 MBEDTLS_SSL_CHK_BUF_PTR(p, end, MBEDTLS_SERVER_HELLO_RANDOM_LEN);
2168 if (is_hrr) {
2169 memcpy(p, mbedtls_ssl_tls13_hello_retry_request_magic,
2170 MBEDTLS_SERVER_HELLO_RANDOM_LEN);
2171 } else {
2172 memcpy(p, &ssl->handshake->randbytes[MBEDTLS_CLIENT_HELLO_RANDOM_LEN],
2173 MBEDTLS_SERVER_HELLO_RANDOM_LEN);
Jerry Yu23f7a6f2022-04-23 15:16:45 +08002174 }
Gilles Peskine449bd832023-01-11 14:50:10 +01002175 MBEDTLS_SSL_DEBUG_BUF(3, "server hello, random bytes",
2176 p, MBEDTLS_SERVER_HELLO_RANDOM_LEN);
Jerry Yu3bf2c642022-03-30 22:02:12 +08002177 p += MBEDTLS_SERVER_HELLO_RANDOM_LEN;
2178
Jerry Yucfc04b32022-04-21 09:31:58 +08002179 /* ...
2180 * opaque legacy_session_id_echo<0..32>;
2181 * ...
Jerry Yu3bf2c642022-03-30 22:02:12 +08002182 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002183 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 1 + ssl->session_negotiate->id_len);
2184 *p++ = (unsigned char) ssl->session_negotiate->id_len;
2185 if (ssl->session_negotiate->id_len > 0) {
2186 memcpy(p, &ssl->session_negotiate->id[0],
2187 ssl->session_negotiate->id_len);
Jerry Yu3bf2c642022-03-30 22:02:12 +08002188 p += ssl->session_negotiate->id_len;
Jerry Yu955ddd72022-04-22 22:27:33 +08002189
Gilles Peskine449bd832023-01-11 14:50:10 +01002190 MBEDTLS_SSL_DEBUG_BUF(3, "session id", ssl->session_negotiate->id,
2191 ssl->session_negotiate->id_len);
Jerry Yu3bf2c642022-03-30 22:02:12 +08002192 }
2193
Jerry Yucfc04b32022-04-21 09:31:58 +08002194 /* ...
2195 * CipherSuite cipher_suite;
2196 * ...
2197 * with CipherSuite defined as:
2198 * uint8 CipherSuite[2];
Jerry Yu3bf2c642022-03-30 22:02:12 +08002199 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002200 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 2);
2201 MBEDTLS_PUT_UINT16_BE(ssl->session_negotiate->ciphersuite, p, 0);
Jerry Yu3bf2c642022-03-30 22:02:12 +08002202 p += 2;
Gilles Peskine449bd832023-01-11 14:50:10 +01002203 MBEDTLS_SSL_DEBUG_MSG(3,
2204 ("server hello, chosen ciphersuite: %s ( id=%d )",
2205 mbedtls_ssl_get_ciphersuite_name(
2206 ssl->session_negotiate->ciphersuite),
2207 ssl->session_negotiate->ciphersuite));
Jerry Yu3bf2c642022-03-30 22:02:12 +08002208
Jerry Yucfc04b32022-04-21 09:31:58 +08002209 /* ...
2210 * uint8 legacy_compression_method = 0;
2211 * ...
2212 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002213 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 1);
Thomas Daubney31e03a82022-07-25 15:59:25 +01002214 *p++ = MBEDTLS_SSL_COMPRESS_NULL;
Jerry Yu3bf2c642022-03-30 22:02:12 +08002215
Jerry Yucfc04b32022-04-21 09:31:58 +08002216 /* ...
2217 * Extension extensions<6..2^16-1>;
2218 * ...
2219 * struct {
2220 * ExtensionType extension_type; (2 bytes)
2221 * opaque extension_data<0..2^16-1>;
2222 * } Extension;
2223 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002224 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 2);
Jerry Yud9436a12022-04-20 22:28:09 +08002225 p_extensions_len = p;
Jerry Yu3bf2c642022-03-30 22:02:12 +08002226 p += 2;
2227
Gilles Peskine449bd832023-01-11 14:50:10 +01002228 if ((ret = ssl_tls13_write_server_hello_supported_versions_ext(
2229 ssl, p, end, &output_len)) != 0) {
Jerry Yu955ddd72022-04-22 22:27:33 +08002230 MBEDTLS_SSL_DEBUG_RET(
Gilles Peskine449bd832023-01-11 14:50:10 +01002231 1, "ssl_tls13_write_server_hello_supported_versions_ext", ret);
2232 return ret;
Jerry Yu3bf2c642022-03-30 22:02:12 +08002233 }
2234 p += output_len;
2235
Gilles Peskine449bd832023-01-11 14:50:10 +01002236 if (mbedtls_ssl_tls13_key_exchange_mode_with_ephemeral(ssl)) {
2237 if (is_hrr) {
2238 ret = ssl_tls13_write_hrr_key_share_ext(ssl, p, end, &output_len);
2239 } else {
2240 ret = ssl_tls13_write_key_share_ext(ssl, p, end, &output_len);
2241 }
2242 if (ret != 0) {
2243 return ret;
2244 }
Jerry Yu3bf2c642022-03-30 22:02:12 +08002245 p += output_len;
2246 }
Jerry Yu3bf2c642022-03-30 22:02:12 +08002247
Ronald Cron41a443a2022-10-04 16:38:25 +02002248#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01002249 if (!is_hrr && mbedtls_ssl_tls13_key_exchange_mode_with_psk(ssl)) {
2250 ret = ssl_tls13_write_server_pre_shared_key_ext(ssl, p, end, &output_len);
2251 if (ret != 0) {
2252 MBEDTLS_SSL_DEBUG_RET(1, "ssl_tls13_write_server_pre_shared_key_ext",
2253 ret);
2254 return ret;
Jerry Yu032b15ce2022-07-11 06:10:03 +00002255 }
2256 p += output_len;
2257 }
Ronald Cron41a443a2022-10-04 16:38:25 +02002258#endif
Jerry Yu032b15ce2022-07-11 06:10:03 +00002259
Gilles Peskine449bd832023-01-11 14:50:10 +01002260 MBEDTLS_PUT_UINT16_BE(p - p_extensions_len - 2, p_extensions_len, 0);
Jerry Yu3bf2c642022-03-30 22:02:12 +08002261
Gilles Peskine449bd832023-01-11 14:50:10 +01002262 MBEDTLS_SSL_DEBUG_BUF(4, "server hello extensions",
2263 p_extensions_len, p - p_extensions_len);
Jerry Yu3bf2c642022-03-30 22:02:12 +08002264
Jerry Yud9436a12022-04-20 22:28:09 +08002265 *out_len = p - buf;
Jerry Yu3bf2c642022-03-30 22:02:12 +08002266
Gilles Peskine449bd832023-01-11 14:50:10 +01002267 MBEDTLS_SSL_DEBUG_BUF(3, "server hello", buf, *out_len);
Jerry Yu3bf2c642022-03-30 22:02:12 +08002268
Jerry Yu7de2ff02022-11-08 21:43:46 +08002269 MBEDTLS_SSL_PRINT_EXTS(
Jerry Yu4b8f2f72022-10-31 13:31:22 +08002270 3, is_hrr ? MBEDTLS_SSL_TLS1_3_HS_HELLO_RETRY_REQUEST :
Gilles Peskine449bd832023-01-11 14:50:10 +01002271 MBEDTLS_SSL_HS_SERVER_HELLO,
2272 ssl->handshake->sent_extensions);
Jerry Yu4b8f2f72022-10-31 13:31:22 +08002273
Gilles Peskine449bd832023-01-11 14:50:10 +01002274 return ret;
Jerry Yu56404d72022-03-30 17:36:13 +08002275}
2276
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002277MBEDTLS_CHECK_RETURN_CRITICAL
Xiaokang Qian934ce6f2023-02-06 10:23:04 +00002278static int ssl_tls13_finalize_server_hello(mbedtls_ssl_context *ssl)
Jerry Yue110d252022-05-05 10:19:22 +08002279{
2280 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine449bd832023-01-11 14:50:10 +01002281 ret = mbedtls_ssl_tls13_compute_handshake_transform(ssl);
2282 if (ret != 0) {
2283 MBEDTLS_SSL_DEBUG_RET(1,
2284 "mbedtls_ssl_tls13_compute_handshake_transform",
2285 ret);
2286 return ret;
Jerry Yue110d252022-05-05 10:19:22 +08002287 }
2288
Gilles Peskine449bd832023-01-11 14:50:10 +01002289 return ret;
Jerry Yue110d252022-05-05 10:19:22 +08002290}
2291
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002292MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002293static int ssl_tls13_write_server_hello(mbedtls_ssl_context *ssl)
Jerry Yu5b64ae92022-03-30 17:15:02 +08002294{
Jerry Yu637a3f12022-04-20 21:37:58 +08002295 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yuf4b27e42022-03-30 17:32:21 +08002296 unsigned char *buf;
2297 size_t buf_len, msg_len;
2298
Gilles Peskine449bd832023-01-11 14:50:10 +01002299 MBEDTLS_SSL_DEBUG_MSG(2, ("=> write server hello"));
Jerry Yuf4b27e42022-03-30 17:32:21 +08002300
Gilles Peskine449bd832023-01-11 14:50:10 +01002301 MBEDTLS_SSL_PROC_CHK(ssl_tls13_prepare_server_hello(ssl));
Jerry Yuf4b27e42022-03-30 17:32:21 +08002302
Xiaokang Qian9f1747b2023-03-30 02:50:04 +00002303 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_start_handshake_msg(
2304 ssl, MBEDTLS_SSL_HS_SERVER_HELLO, &buf, &buf_len));
Jerry Yuf4b27e42022-03-30 17:32:21 +08002305
Gilles Peskine449bd832023-01-11 14:50:10 +01002306 MBEDTLS_SSL_PROC_CHK(ssl_tls13_write_server_hello_body(ssl, buf,
2307 buf + buf_len,
2308 &msg_len,
2309 0));
Jerry Yuf4b27e42022-03-30 17:32:21 +08002310
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +01002311 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_add_hs_msg_to_checksum(
Manuel Pégourié-Gonnard43cc1272023-02-06 11:48:19 +01002312 ssl, MBEDTLS_SSL_HS_SERVER_HELLO, buf, msg_len));
Jerry Yuf4b27e42022-03-30 17:32:21 +08002313
Gilles Peskine449bd832023-01-11 14:50:10 +01002314 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_finish_handshake_msg(
2315 ssl, buf_len, msg_len));
Jerry Yu637a3f12022-04-20 21:37:58 +08002316
Xiaokang Qian934ce6f2023-02-06 10:23:04 +00002317 MBEDTLS_SSL_PROC_CHK(ssl_tls13_finalize_server_hello(ssl));
Jerry Yue110d252022-05-05 10:19:22 +08002318
Gabor Mezei7b39bf12022-05-24 16:04:14 +02002319#if defined(MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE)
2320 /* The server sends a dummy change_cipher_spec record immediately
2321 * after its first handshake message. This may either be after
2322 * a ServerHello or a HelloRetryRequest.
2323 */
Gabor Mezei96ae9262022-06-28 11:45:18 +02002324 mbedtls_ssl_handshake_set_state(
Gilles Peskine449bd832023-01-11 14:50:10 +01002325 ssl, MBEDTLS_SSL_SERVER_CCS_AFTER_SERVER_HELLO);
Gabor Mezei7b39bf12022-05-24 16:04:14 +02002326#else
Gilles Peskine449bd832023-01-11 14:50:10 +01002327 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_ENCRYPTED_EXTENSIONS);
Gabor Mezei7b39bf12022-05-24 16:04:14 +02002328#endif /* MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE */
Jerry Yue110d252022-05-05 10:19:22 +08002329
Jerry Yuf4b27e42022-03-30 17:32:21 +08002330cleanup:
2331
Gilles Peskine449bd832023-01-11 14:50:10 +01002332 MBEDTLS_SSL_DEBUG_MSG(2, ("<= write server hello"));
2333 return ret;
Jerry Yu5b64ae92022-03-30 17:15:02 +08002334}
2335
Jerry Yu23d1a252022-05-12 18:08:59 +08002336
2337/*
2338 * Handler for MBEDTLS_SSL_HELLO_RETRY_REQUEST
2339 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002340MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002341static int ssl_tls13_prepare_hello_retry_request(mbedtls_ssl_context *ssl)
Jerry Yu23d1a252022-05-12 18:08:59 +08002342{
2343 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine449bd832023-01-11 14:50:10 +01002344 if (ssl->handshake->hello_retry_request_count > 0) {
2345 MBEDTLS_SSL_DEBUG_MSG(1, ("Too many HRRs"));
2346 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE,
2347 MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE);
2348 return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
Jerry Yu23d1a252022-05-12 18:08:59 +08002349 }
2350
2351 /*
2352 * Create stateless transcript hash for HRR
2353 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002354 MBEDTLS_SSL_DEBUG_MSG(4, ("Reset transcript for HRR"));
2355 ret = mbedtls_ssl_reset_transcript_for_hrr(ssl);
2356 if (ret != 0) {
2357 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_reset_transcript_for_hrr", ret);
2358 return ret;
Jerry Yu23d1a252022-05-12 18:08:59 +08002359 }
Gilles Peskine449bd832023-01-11 14:50:10 +01002360 mbedtls_ssl_session_reset_msg_layer(ssl, 0);
Jerry Yu23d1a252022-05-12 18:08:59 +08002361
Gilles Peskine449bd832023-01-11 14:50:10 +01002362 return 0;
Jerry Yu23d1a252022-05-12 18:08:59 +08002363}
2364
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002365MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002366static int ssl_tls13_write_hello_retry_request(mbedtls_ssl_context *ssl)
Jerry Yu23d1a252022-05-12 18:08:59 +08002367{
2368 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
2369 unsigned char *buf;
2370 size_t buf_len, msg_len;
2371
Gilles Peskine449bd832023-01-11 14:50:10 +01002372 MBEDTLS_SSL_DEBUG_MSG(2, ("=> write hello retry request"));
Jerry Yu23d1a252022-05-12 18:08:59 +08002373
Gilles Peskine449bd832023-01-11 14:50:10 +01002374 MBEDTLS_SSL_PROC_CHK(ssl_tls13_prepare_hello_retry_request(ssl));
Jerry Yu23d1a252022-05-12 18:08:59 +08002375
Gilles Peskine449bd832023-01-11 14:50:10 +01002376 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_start_handshake_msg(
2377 ssl, MBEDTLS_SSL_HS_SERVER_HELLO,
2378 &buf, &buf_len));
Jerry Yu23d1a252022-05-12 18:08:59 +08002379
Gilles Peskine449bd832023-01-11 14:50:10 +01002380 MBEDTLS_SSL_PROC_CHK(ssl_tls13_write_server_hello_body(ssl, buf,
2381 buf + buf_len,
2382 &msg_len,
2383 1));
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +01002384 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_add_hs_msg_to_checksum(
Manuel Pégourié-Gonnard43cc1272023-02-06 11:48:19 +01002385 ssl, MBEDTLS_SSL_HS_SERVER_HELLO, buf, msg_len));
Jerry Yu23d1a252022-05-12 18:08:59 +08002386
2387
Gilles Peskine449bd832023-01-11 14:50:10 +01002388 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_finish_handshake_msg(ssl, buf_len,
2389 msg_len));
Jerry Yu23d1a252022-05-12 18:08:59 +08002390
2391 ssl->handshake->hello_retry_request_count++;
2392
Gabor Mezei7b39bf12022-05-24 16:04:14 +02002393#if defined(MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE)
2394 /* The server sends a dummy change_cipher_spec record immediately
2395 * after its first handshake message. This may either be after
2396 * a ServerHello or a HelloRetryRequest.
2397 */
Gabor Mezei96ae9262022-06-28 11:45:18 +02002398 mbedtls_ssl_handshake_set_state(
Gilles Peskine449bd832023-01-11 14:50:10 +01002399 ssl, MBEDTLS_SSL_SERVER_CCS_AFTER_HELLO_RETRY_REQUEST);
Gabor Mezei7b39bf12022-05-24 16:04:14 +02002400#else
Gilles Peskine449bd832023-01-11 14:50:10 +01002401 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_CLIENT_HELLO);
Gabor Mezei7b39bf12022-05-24 16:04:14 +02002402#endif /* MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE */
Jerry Yu23d1a252022-05-12 18:08:59 +08002403
2404cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +01002405 MBEDTLS_SSL_DEBUG_MSG(2, ("<= write hello retry request"));
2406 return ret;
Jerry Yu23d1a252022-05-12 18:08:59 +08002407}
2408
Jerry Yu5b64ae92022-03-30 17:15:02 +08002409/*
Jerry Yu4d3841a2022-04-16 12:37:19 +08002410 * Handler for MBEDTLS_SSL_ENCRYPTED_EXTENSIONS
2411 */
Jerry Yu4d3841a2022-04-16 12:37:19 +08002412
2413/*
2414 * struct {
2415 * Extension extensions<0..2 ^ 16 - 1>;
2416 * } EncryptedExtensions;
2417 *
2418 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002419MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002420static int ssl_tls13_write_encrypted_extensions_body(mbedtls_ssl_context *ssl,
2421 unsigned char *buf,
2422 unsigned char *end,
2423 size_t *out_len)
Jerry Yu4d3841a2022-04-16 12:37:19 +08002424{
XiaokangQianacb39922022-06-17 10:18:48 +00002425 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yu4d3841a2022-04-16 12:37:19 +08002426 unsigned char *p = buf;
2427 size_t extensions_len = 0;
Jerry Yu8937eb42022-05-03 12:12:14 +08002428 unsigned char *p_extensions_len;
XiaokangQianacb39922022-06-17 10:18:48 +00002429 size_t output_len;
Jerry Yu9da5e5a2022-05-03 15:46:09 +08002430
Jerry Yu4d3841a2022-04-16 12:37:19 +08002431 *out_len = 0;
2432
Gilles Peskine449bd832023-01-11 14:50:10 +01002433 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 2);
Jerry Yu8937eb42022-05-03 12:12:14 +08002434 p_extensions_len = p;
Jerry Yu4d3841a2022-04-16 12:37:19 +08002435 p += 2;
2436
Jerry Yu8937eb42022-05-03 12:12:14 +08002437 ((void) ssl);
XiaokangQianacb39922022-06-17 10:18:48 +00002438 ((void) ret);
2439 ((void) output_len);
2440
2441#if defined(MBEDTLS_SSL_ALPN)
Gilles Peskine449bd832023-01-11 14:50:10 +01002442 ret = mbedtls_ssl_write_alpn_ext(ssl, p, end, &output_len);
2443 if (ret != 0) {
2444 return ret;
2445 }
XiaokangQian95d5f542022-06-24 02:29:26 +00002446 p += output_len;
XiaokangQianacb39922022-06-17 10:18:48 +00002447#endif /* MBEDTLS_SSL_ALPN */
Jerry Yu4d3841a2022-04-16 12:37:19 +08002448
Gilles Peskine449bd832023-01-11 14:50:10 +01002449 extensions_len = (p - p_extensions_len) - 2;
2450 MBEDTLS_PUT_UINT16_BE(extensions_len, p_extensions_len, 0);
Jerry Yu8937eb42022-05-03 12:12:14 +08002451
2452 *out_len = p - buf;
Jerry Yu4d3841a2022-04-16 12:37:19 +08002453
Gilles Peskine449bd832023-01-11 14:50:10 +01002454 MBEDTLS_SSL_DEBUG_BUF(4, "encrypted extensions", buf, *out_len);
Jerry Yu4d3841a2022-04-16 12:37:19 +08002455
Jerry Yu7de2ff02022-11-08 21:43:46 +08002456 MBEDTLS_SSL_PRINT_EXTS(
Gilles Peskine449bd832023-01-11 14:50:10 +01002457 3, MBEDTLS_SSL_HS_ENCRYPTED_EXTENSIONS, ssl->handshake->sent_extensions);
Jerry Yu4b8f2f72022-10-31 13:31:22 +08002458
Gilles Peskine449bd832023-01-11 14:50:10 +01002459 return 0;
Jerry Yu4d3841a2022-04-16 12:37:19 +08002460}
2461
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002462MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002463static int ssl_tls13_write_encrypted_extensions(mbedtls_ssl_context *ssl)
Jerry Yu4d3841a2022-04-16 12:37:19 +08002464{
2465 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
2466 unsigned char *buf;
Jerry Yu39730a72022-05-03 12:14:04 +08002467 size_t buf_len, msg_len;
Jerry Yu4d3841a2022-04-16 12:37:19 +08002468
Gilles Peskine449bd832023-01-11 14:50:10 +01002469 mbedtls_ssl_set_outbound_transform(ssl,
2470 ssl->handshake->transform_handshake);
Gabor Mezei54719122022-06-28 11:34:56 +02002471 MBEDTLS_SSL_DEBUG_MSG(
Gilles Peskine449bd832023-01-11 14:50:10 +01002472 3, ("switching to handshake transform for outbound data"));
Gabor Mezei54719122022-06-28 11:34:56 +02002473
Gilles Peskine449bd832023-01-11 14:50:10 +01002474 MBEDTLS_SSL_DEBUG_MSG(2, ("=> write encrypted extensions"));
Jerry Yu4d3841a2022-04-16 12:37:19 +08002475
Xiaokang Qian9f1747b2023-03-30 02:50:04 +00002476 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_start_handshake_msg(
2477 ssl, MBEDTLS_SSL_HS_ENCRYPTED_EXTENSIONS,
2478 &buf, &buf_len));
Jerry Yu4d3841a2022-04-16 12:37:19 +08002479
Gilles Peskine449bd832023-01-11 14:50:10 +01002480 MBEDTLS_SSL_PROC_CHK(ssl_tls13_write_encrypted_extensions_body(
2481 ssl, buf, buf + buf_len, &msg_len));
Jerry Yu4d3841a2022-04-16 12:37:19 +08002482
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +01002483 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_add_hs_msg_to_checksum(
Xiaokang Qian9f1747b2023-03-30 02:50:04 +00002484 ssl, MBEDTLS_SSL_HS_ENCRYPTED_EXTENSIONS,
2485 buf, msg_len));
Jerry Yu4d3841a2022-04-16 12:37:19 +08002486
Gilles Peskine449bd832023-01-11 14:50:10 +01002487 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_finish_handshake_msg(
2488 ssl, buf_len, msg_len));
Jerry Yu4d3841a2022-04-16 12:37:19 +08002489
Ronald Cron928cbd32022-10-04 16:14:26 +02002490#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01002491 if (mbedtls_ssl_tls13_key_exchange_mode_with_psk(ssl)) {
2492 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_SERVER_FINISHED);
2493 } else {
2494 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_CERTIFICATE_REQUEST);
2495 }
Jerry Yu8937eb42022-05-03 12:12:14 +08002496#else
Gilles Peskine449bd832023-01-11 14:50:10 +01002497 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_SERVER_FINISHED);
Jerry Yu8937eb42022-05-03 12:12:14 +08002498#endif
2499
Jerry Yu4d3841a2022-04-16 12:37:19 +08002500cleanup:
2501
Gilles Peskine449bd832023-01-11 14:50:10 +01002502 MBEDTLS_SSL_DEBUG_MSG(2, ("<= write encrypted extensions"));
2503 return ret;
Jerry Yu4d3841a2022-04-16 12:37:19 +08002504}
2505
Ronald Cron928cbd32022-10-04 16:14:26 +02002506#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED)
XiaokangQiana987e1d2022-05-07 01:25:58 +00002507#define SSL_CERTIFICATE_REQUEST_SEND_REQUEST 0
2508#define SSL_CERTIFICATE_REQUEST_SKIP 1
2509/* Coordination:
2510 * Check whether a CertificateRequest message should be written.
2511 * Returns a negative code on failure, or
2512 * - SSL_CERTIFICATE_REQUEST_SEND_REQUEST
2513 * - SSL_CERTIFICATE_REQUEST_SKIP
2514 * indicating if the writing of the CertificateRequest
2515 * should be skipped or not.
2516 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002517MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002518static int ssl_tls13_certificate_request_coordinate(mbedtls_ssl_context *ssl)
XiaokangQiana987e1d2022-05-07 01:25:58 +00002519{
2520 int authmode;
2521
XiaokangQian40a35232022-05-07 09:02:40 +00002522#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Gilles Peskine449bd832023-01-11 14:50:10 +01002523 if (ssl->handshake->sni_authmode != MBEDTLS_SSL_VERIFY_UNSET) {
XiaokangQian40a35232022-05-07 09:02:40 +00002524 authmode = ssl->handshake->sni_authmode;
Gilles Peskine449bd832023-01-11 14:50:10 +01002525 } else
XiaokangQian40a35232022-05-07 09:02:40 +00002526#endif
XiaokangQiana987e1d2022-05-07 01:25:58 +00002527 authmode = ssl->conf->authmode;
2528
Gilles Peskine449bd832023-01-11 14:50:10 +01002529 if (authmode == MBEDTLS_SSL_VERIFY_NONE) {
Ronald Croneac00ad2022-09-13 10:16:31 +02002530 ssl->session_negotiate->verify_result = MBEDTLS_X509_BADCERT_SKIP_VERIFY;
Gilles Peskine449bd832023-01-11 14:50:10 +01002531 return SSL_CERTIFICATE_REQUEST_SKIP;
Ronald Croneac00ad2022-09-13 10:16:31 +02002532 }
XiaokangQiana987e1d2022-05-07 01:25:58 +00002533
XiaokangQianc3017f62022-05-13 05:55:41 +00002534 ssl->handshake->certificate_request_sent = 1;
XiaokangQian189ded22022-05-10 08:12:17 +00002535
Gilles Peskine449bd832023-01-11 14:50:10 +01002536 return SSL_CERTIFICATE_REQUEST_SEND_REQUEST;
XiaokangQiana987e1d2022-05-07 01:25:58 +00002537}
2538
XiaokangQiancec9ae62022-05-06 07:28:50 +00002539/*
2540 * struct {
2541 * opaque certificate_request_context<0..2^8-1>;
2542 * Extension extensions<2..2^16-1>;
2543 * } CertificateRequest;
2544 *
2545 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002546MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002547static int ssl_tls13_write_certificate_request_body(mbedtls_ssl_context *ssl,
2548 unsigned char *buf,
2549 const unsigned char *end,
2550 size_t *out_len)
XiaokangQiancec9ae62022-05-06 07:28:50 +00002551{
2552 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
2553 unsigned char *p = buf;
XiaokangQianec6efb92022-05-06 09:53:10 +00002554 size_t output_len = 0;
XiaokangQiancec9ae62022-05-06 07:28:50 +00002555 unsigned char *p_extensions_len;
2556
2557 *out_len = 0;
2558
2559 /* Check if we have enough space:
2560 * - certificate_request_context (1 byte)
2561 * - extensions length (2 bytes)
2562 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002563 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 3);
XiaokangQiancec9ae62022-05-06 07:28:50 +00002564
2565 /*
2566 * Write certificate_request_context
2567 */
2568 /*
2569 * We use a zero length context for the normal handshake
2570 * messages. For post-authentication handshake messages
2571 * this request context would be set to a non-zero value.
2572 */
2573 *p++ = 0x0;
2574
2575 /*
2576 * Write extensions
2577 */
2578 /* The extensions must contain the signature_algorithms. */
2579 p_extensions_len = p;
2580 p += 2;
Gilles Peskine449bd832023-01-11 14:50:10 +01002581 ret = mbedtls_ssl_write_sig_alg_ext(ssl, p, end, &output_len);
2582 if (ret != 0) {
2583 return ret;
2584 }
XiaokangQiancec9ae62022-05-06 07:28:50 +00002585
XiaokangQianec6efb92022-05-06 09:53:10 +00002586 p += output_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01002587 MBEDTLS_PUT_UINT16_BE(p - p_extensions_len - 2, p_extensions_len, 0);
XiaokangQiancec9ae62022-05-06 07:28:50 +00002588
2589 *out_len = p - buf;
2590
Jerry Yu7de2ff02022-11-08 21:43:46 +08002591 MBEDTLS_SSL_PRINT_EXTS(
Gilles Peskine449bd832023-01-11 14:50:10 +01002592 3, MBEDTLS_SSL_HS_CERTIFICATE_REQUEST, ssl->handshake->sent_extensions);
Jerry Yu4b8f2f72022-10-31 13:31:22 +08002593
Gilles Peskine449bd832023-01-11 14:50:10 +01002594 return 0;
XiaokangQiancec9ae62022-05-06 07:28:50 +00002595}
2596
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002597MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002598static int ssl_tls13_write_certificate_request(mbedtls_ssl_context *ssl)
XiaokangQiancec9ae62022-05-06 07:28:50 +00002599{
2600 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
2601
Gilles Peskine449bd832023-01-11 14:50:10 +01002602 MBEDTLS_SSL_DEBUG_MSG(2, ("=> write certificate request"));
XiaokangQiancec9ae62022-05-06 07:28:50 +00002603
Gilles Peskine449bd832023-01-11 14:50:10 +01002604 MBEDTLS_SSL_PROC_CHK_NEG(ssl_tls13_certificate_request_coordinate(ssl));
XiaokangQiancec9ae62022-05-06 07:28:50 +00002605
Gilles Peskine449bd832023-01-11 14:50:10 +01002606 if (ret == SSL_CERTIFICATE_REQUEST_SEND_REQUEST) {
XiaokangQiancec9ae62022-05-06 07:28:50 +00002607 unsigned char *buf;
2608 size_t buf_len, msg_len;
2609
Xiaokang Qian9f1747b2023-03-30 02:50:04 +00002610 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_start_handshake_msg(
2611 ssl, MBEDTLS_SSL_HS_CERTIFICATE_REQUEST,
2612 &buf, &buf_len));
XiaokangQiancec9ae62022-05-06 07:28:50 +00002613
Gilles Peskine449bd832023-01-11 14:50:10 +01002614 MBEDTLS_SSL_PROC_CHK(ssl_tls13_write_certificate_request_body(
2615 ssl, buf, buf + buf_len, &msg_len));
XiaokangQiancec9ae62022-05-06 07:28:50 +00002616
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +01002617 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_add_hs_msg_to_checksum(
Xiaokang Qian9f1747b2023-03-30 02:50:04 +00002618 ssl, MBEDTLS_SSL_HS_CERTIFICATE_REQUEST,
2619 buf, msg_len));
XiaokangQiancec9ae62022-05-06 07:28:50 +00002620
Gilles Peskine449bd832023-01-11 14:50:10 +01002621 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_finish_handshake_msg(
2622 ssl, buf_len, msg_len));
2623 } else if (ret == SSL_CERTIFICATE_REQUEST_SKIP) {
2624 MBEDTLS_SSL_DEBUG_MSG(2, ("<= skip write certificate request"));
XiaokangQiancec9ae62022-05-06 07:28:50 +00002625 ret = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01002626 } else {
2627 MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
XiaokangQiancec9ae62022-05-06 07:28:50 +00002628 ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
2629 goto cleanup;
2630 }
2631
Gilles Peskine449bd832023-01-11 14:50:10 +01002632 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_SERVER_CERTIFICATE);
XiaokangQiancec9ae62022-05-06 07:28:50 +00002633cleanup:
2634
Gilles Peskine449bd832023-01-11 14:50:10 +01002635 MBEDTLS_SSL_DEBUG_MSG(2, ("<= write certificate request"));
2636 return ret;
XiaokangQiancec9ae62022-05-06 07:28:50 +00002637}
XiaokangQiancec9ae62022-05-06 07:28:50 +00002638
Jerry Yu4d3841a2022-04-16 12:37:19 +08002639/*
Jerry Yuc6e6dbf2022-04-16 19:42:57 +08002640 * Handler for MBEDTLS_SSL_SERVER_CERTIFICATE
Jerry Yu83da34e2022-04-16 13:59:52 +08002641 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002642MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002643static int ssl_tls13_write_server_certificate(mbedtls_ssl_context *ssl)
Jerry Yu83da34e2022-04-16 13:59:52 +08002644{
Jerry Yu5a26f302022-05-10 20:46:40 +08002645 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
XiaokangQianfb665a82022-06-15 03:57:21 +00002646
2647#if defined(MBEDTLS_X509_CRT_PARSE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01002648 if ((ssl_tls13_pick_key_cert(ssl) != 0) ||
2649 mbedtls_ssl_own_cert(ssl) == NULL) {
2650 MBEDTLS_SSL_DEBUG_MSG(2, ("No certificate available."));
2651 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE,
2652 MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE);
2653 return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
Jerry Yu5a26f302022-05-10 20:46:40 +08002654 }
XiaokangQianfb665a82022-06-15 03:57:21 +00002655#endif /* MBEDTLS_X509_CRT_PARSE_C */
Jerry Yu5a26f302022-05-10 20:46:40 +08002656
Gilles Peskine449bd832023-01-11 14:50:10 +01002657 ret = mbedtls_ssl_tls13_write_certificate(ssl);
2658 if (ret != 0) {
2659 return ret;
2660 }
2661 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_CERTIFICATE_VERIFY);
2662 return 0;
Jerry Yu83da34e2022-04-16 13:59:52 +08002663}
2664
2665/*
Jerry Yuc6e6dbf2022-04-16 19:42:57 +08002666 * Handler for MBEDTLS_SSL_CERTIFICATE_VERIFY
Jerry Yu83da34e2022-04-16 13:59:52 +08002667 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002668MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002669static int ssl_tls13_write_certificate_verify(mbedtls_ssl_context *ssl)
Jerry Yu83da34e2022-04-16 13:59:52 +08002670{
Gilles Peskine449bd832023-01-11 14:50:10 +01002671 int ret = mbedtls_ssl_tls13_write_certificate_verify(ssl);
2672 if (ret != 0) {
2673 return ret;
2674 }
2675 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_SERVER_FINISHED);
2676 return 0;
Jerry Yu83da34e2022-04-16 13:59:52 +08002677}
Ronald Cron928cbd32022-10-04 16:14:26 +02002678#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED */
Jerry Yu83da34e2022-04-16 13:59:52 +08002679
2680/*
Jerry Yud6e253d2022-05-18 13:59:24 +08002681 * Handler for MBEDTLS_SSL_SERVER_FINISHED
Jerry Yu69dd8d42022-04-16 12:51:26 +08002682 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002683MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002684static int ssl_tls13_write_server_finished(mbedtls_ssl_context *ssl)
Jerry Yu69dd8d42022-04-16 12:51:26 +08002685{
Jerry Yud6e253d2022-05-18 13:59:24 +08002686 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yu27bdc7c2022-04-16 13:33:27 +08002687
Gilles Peskine449bd832023-01-11 14:50:10 +01002688 ret = mbedtls_ssl_tls13_write_finished_message(ssl);
2689 if (ret != 0) {
2690 return ret;
2691 }
Jerry Yu27bdc7c2022-04-16 13:33:27 +08002692
Gilles Peskine449bd832023-01-11 14:50:10 +01002693 ret = mbedtls_ssl_tls13_compute_application_transform(ssl);
2694 if (ret != 0) {
Jerry Yue3d67cb2022-05-19 15:33:10 +08002695 MBEDTLS_SSL_PEND_FATAL_ALERT(
Gilles Peskine449bd832023-01-11 14:50:10 +01002696 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE,
2697 MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE);
2698 return ret;
Jerry Yue3d67cb2022-05-19 15:33:10 +08002699 }
XiaokangQianc3017f62022-05-13 05:55:41 +00002700
Gilles Peskine449bd832023-01-11 14:50:10 +01002701 MBEDTLS_SSL_DEBUG_MSG(1, ("Switch to handshake keys for inbound traffic"));
2702 mbedtls_ssl_set_inbound_transform(ssl, ssl->handshake->transform_handshake);
XiaokangQian189ded22022-05-10 08:12:17 +00002703
Gilles Peskine449bd832023-01-11 14:50:10 +01002704 if (ssl->handshake->certificate_request_sent) {
2705 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_CLIENT_CERTIFICATE);
2706 } else {
2707 MBEDTLS_SSL_DEBUG_MSG(2, ("skip parse certificate"));
2708 MBEDTLS_SSL_DEBUG_MSG(2, ("skip parse certificate verify"));
2709 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_CLIENT_FINISHED);
Ronald Cron19385882022-06-15 16:26:13 +02002710 }
Ronald Cron63dc4632022-05-31 14:41:53 +02002711
Gilles Peskine449bd832023-01-11 14:50:10 +01002712 return 0;
Jerry Yu69dd8d42022-04-16 12:51:26 +08002713}
2714
2715/*
Jerry Yud6e253d2022-05-18 13:59:24 +08002716 * Handler for MBEDTLS_SSL_CLIENT_FINISHED
Jerry Yu69dd8d42022-04-16 12:51:26 +08002717 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002718MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002719static int ssl_tls13_process_client_finished(mbedtls_ssl_context *ssl)
Jerry Yu69dd8d42022-04-16 12:51:26 +08002720{
Jerry Yud6e253d2022-05-18 13:59:24 +08002721 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
2722
Gilles Peskine449bd832023-01-11 14:50:10 +01002723 ret = mbedtls_ssl_tls13_process_finished_message(ssl);
2724 if (ret != 0) {
2725 return ret;
Jerry Yue3d67cb2022-05-19 15:33:10 +08002726 }
2727
Gilles Peskine449bd832023-01-11 14:50:10 +01002728 ret = mbedtls_ssl_tls13_compute_resumption_master_secret(ssl);
2729 if (ret != 0) {
Xiaokang Qian9f1747b2023-03-30 02:50:04 +00002730 MBEDTLS_SSL_DEBUG_RET(
2731 1, "mbedtls_ssl_tls13_compute_resumption_master_secret", ret);
Gilles Peskine449bd832023-01-11 14:50:10 +01002732 }
2733
2734 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_HANDSHAKE_WRAPUP);
2735 return 0;
Jerry Yu69dd8d42022-04-16 12:51:26 +08002736}
2737
2738/*
Jerry Yud6e253d2022-05-18 13:59:24 +08002739 * Handler for MBEDTLS_SSL_HANDSHAKE_WRAPUP
Jerry Yu69dd8d42022-04-16 12:51:26 +08002740 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002741MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002742static int ssl_tls13_handshake_wrapup(mbedtls_ssl_context *ssl)
Jerry Yu69dd8d42022-04-16 12:51:26 +08002743{
Gilles Peskine449bd832023-01-11 14:50:10 +01002744 MBEDTLS_SSL_DEBUG_MSG(2, ("handshake: done"));
Jerry Yu03ed50b2022-04-16 17:13:30 +08002745
Gilles Peskine449bd832023-01-11 14:50:10 +01002746 mbedtls_ssl_tls13_handshake_wrapup(ssl);
Jerry Yue67bef42022-07-07 07:29:42 +00002747
Pengyu Lv3643fdb2023-01-12 16:46:28 +08002748#if defined(MBEDTLS_SSL_SESSION_TICKETS) && \
2749 defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED)
Pengyu Lva1aa31b2022-12-13 13:49:59 +08002750/* TODO: Remove the check of SOME_PSK_ENABLED since SESSION_TICKETS requires
2751 * SOME_PSK_ENABLED to be enabled. Here is just to make CI happy. It is
2752 * expected to be resolved with issue#6395.
2753 */
Pengyu Lvc7af2c42022-12-01 16:33:00 +08002754 /* Sent NewSessionTicket message only when client supports PSK */
Pengyu Lv3643fdb2023-01-12 16:46:28 +08002755 if (mbedtls_ssl_tls13_some_psk_enabled(ssl)) {
Xiaokang Qian9f1747b2023-03-30 02:50:04 +00002756 mbedtls_ssl_handshake_set_state(
2757 ssl, MBEDTLS_SSL_TLS1_3_NEW_SESSION_TICKET);
Pengyu Lvc7af2c42022-12-01 16:33:00 +08002758 } else
Jerry Yufca4d572022-07-21 10:37:48 +08002759#endif
Pengyu Lv3643fdb2023-01-12 16:46:28 +08002760 {
2761 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_HANDSHAKE_OVER);
2762 }
Gilles Peskine449bd832023-01-11 14:50:10 +01002763 return 0;
Jerry Yu69dd8d42022-04-16 12:51:26 +08002764}
2765
2766/*
Jerry Yua8d3c502022-10-30 14:51:23 +08002767 * Handler for MBEDTLS_SSL_TLS1_3_NEW_SESSION_TICKET
Jerry Yue67bef42022-07-07 07:29:42 +00002768 */
2769#define SSL_NEW_SESSION_TICKET_SKIP 0
2770#define SSL_NEW_SESSION_TICKET_WRITE 1
2771MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002772static int ssl_tls13_write_new_session_ticket_coordinate(mbedtls_ssl_context *ssl)
Jerry Yue67bef42022-07-07 07:29:42 +00002773{
2774 /* Check whether the use of session tickets is enabled */
Gilles Peskine449bd832023-01-11 14:50:10 +01002775 if (ssl->conf->f_ticket_write == NULL) {
2776 MBEDTLS_SSL_DEBUG_MSG(2, ("NewSessionTicket: disabled,"
2777 " callback is not set"));
2778 return SSL_NEW_SESSION_TICKET_SKIP;
Jerry Yud0766ec2022-09-22 10:46:57 +08002779 }
Gilles Peskine449bd832023-01-11 14:50:10 +01002780 if (ssl->conf->new_session_tickets_count == 0) {
2781 MBEDTLS_SSL_DEBUG_MSG(2, ("NewSessionTicket: disabled,"
2782 " configured count is zero"));
2783 return SSL_NEW_SESSION_TICKET_SKIP;
Jerry Yud0766ec2022-09-22 10:46:57 +08002784 }
2785
Gilles Peskine449bd832023-01-11 14:50:10 +01002786 if (ssl->handshake->new_session_tickets_count == 0) {
2787 MBEDTLS_SSL_DEBUG_MSG(2, ("NewSessionTicket: all tickets have "
2788 "been sent."));
2789 return SSL_NEW_SESSION_TICKET_SKIP;
Jerry Yue67bef42022-07-07 07:29:42 +00002790 }
2791
Gilles Peskine449bd832023-01-11 14:50:10 +01002792 return SSL_NEW_SESSION_TICKET_WRITE;
Jerry Yue67bef42022-07-07 07:29:42 +00002793}
2794
2795#if defined(MBEDTLS_SSL_SESSION_TICKETS)
2796MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002797static int ssl_tls13_prepare_new_session_ticket(mbedtls_ssl_context *ssl,
2798 unsigned char *ticket_nonce,
2799 size_t ticket_nonce_size)
Jerry Yue67bef42022-07-07 07:29:42 +00002800{
2801 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
2802 mbedtls_ssl_session *session = ssl->session;
2803 mbedtls_ssl_ciphersuite_t *ciphersuite_info;
2804 psa_algorithm_t psa_hash_alg;
2805 int hash_length;
2806
Gilles Peskine449bd832023-01-11 14:50:10 +01002807 MBEDTLS_SSL_DEBUG_MSG(2, ("=> prepare NewSessionTicket msg"));
Jerry Yue67bef42022-07-07 07:29:42 +00002808
2809#if defined(MBEDTLS_HAVE_TIME)
Gilles Peskine449bd832023-01-11 14:50:10 +01002810 session->start = mbedtls_time(NULL);
Jerry Yue67bef42022-07-07 07:29:42 +00002811#endif
2812
Pengyu Lv9f926952022-11-17 15:22:33 +08002813 /* Set ticket_flags depends on the advertised psk key exchange mode */
Pengyu Lv80270b22023-01-12 11:54:04 +08002814 mbedtls_ssl_session_clear_ticket_flags(
Pengyu Lv9eacb442022-12-09 14:39:19 +08002815 session, MBEDTLS_SSL_TLS1_3_TICKET_FLAGS_MASK);
Pengyu Lve6487fe2022-12-06 09:30:29 +08002816#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED)
Pengyu Lv80270b22023-01-12 11:54:04 +08002817 mbedtls_ssl_session_set_ticket_flags(
Pengyu Lv9eacb442022-12-09 14:39:19 +08002818 session, ssl->handshake->tls13_kex_modes);
Pengyu Lve6487fe2022-12-06 09:30:29 +08002819#endif
Pengyu Lv4938a562023-01-16 11:28:49 +08002820 MBEDTLS_SSL_PRINT_TICKET_FLAGS(4, session->ticket_flags);
Pengyu Lv9f926952022-11-17 15:22:33 +08002821
Jerry Yue67bef42022-07-07 07:29:42 +00002822 /* Generate ticket_age_add */
Gilles Peskine449bd832023-01-11 14:50:10 +01002823 if ((ret = ssl->conf->f_rng(ssl->conf->p_rng,
2824 (unsigned char *) &session->ticket_age_add,
2825 sizeof(session->ticket_age_add)) != 0)) {
2826 MBEDTLS_SSL_DEBUG_RET(1, "generate_ticket_age_add", ret);
2827 return ret;
Jerry Yue67bef42022-07-07 07:29:42 +00002828 }
Gilles Peskine449bd832023-01-11 14:50:10 +01002829 MBEDTLS_SSL_DEBUG_MSG(3, ("ticket_age_add: %u",
2830 (unsigned int) session->ticket_age_add));
Jerry Yue67bef42022-07-07 07:29:42 +00002831
2832 /* Generate ticket_nonce */
Gilles Peskine449bd832023-01-11 14:50:10 +01002833 ret = ssl->conf->f_rng(ssl->conf->p_rng, ticket_nonce, ticket_nonce_size);
2834 if (ret != 0) {
2835 MBEDTLS_SSL_DEBUG_RET(1, "generate_ticket_nonce", ret);
2836 return ret;
Jerry Yue67bef42022-07-07 07:29:42 +00002837 }
Gilles Peskine449bd832023-01-11 14:50:10 +01002838 MBEDTLS_SSL_DEBUG_BUF(3, "ticket_nonce:",
2839 ticket_nonce, ticket_nonce_size);
Jerry Yue67bef42022-07-07 07:29:42 +00002840
2841 ciphersuite_info =
Gilles Peskine449bd832023-01-11 14:50:10 +01002842 (mbedtls_ssl_ciphersuite_t *) ssl->handshake->ciphersuite_info;
Dave Rodgman2eab4622023-10-05 13:30:37 +01002843 psa_hash_alg = mbedtls_md_psa_alg_from_type((mbedtls_md_type_t) ciphersuite_info->mac);
Gilles Peskine449bd832023-01-11 14:50:10 +01002844 hash_length = PSA_HASH_LENGTH(psa_hash_alg);
2845 if (hash_length == -1 ||
2846 (size_t) hash_length > sizeof(session->resumption_key)) {
2847 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Jerry Yufca4d572022-07-21 10:37:48 +08002848 }
2849
Jerry Yue67bef42022-07-07 07:29:42 +00002850 /* In this code the psk key length equals the length of the hash */
2851 session->resumption_key_len = hash_length;
2852 session->ciphersuite = ciphersuite_info->id;
2853
2854 /* Compute resumption key
2855 *
2856 * HKDF-Expand-Label( resumption_master_secret,
2857 * "resumption", ticket_nonce, Hash.length )
2858 */
2859 ret = mbedtls_ssl_tls13_hkdf_expand_label(
Gilles Peskine449bd832023-01-11 14:50:10 +01002860 psa_hash_alg,
2861 session->app_secrets.resumption_master_secret,
2862 hash_length,
2863 MBEDTLS_SSL_TLS1_3_LBL_WITH_LEN(resumption),
2864 ticket_nonce,
2865 ticket_nonce_size,
2866 session->resumption_key,
2867 hash_length);
Jerry Yue67bef42022-07-07 07:29:42 +00002868
Gilles Peskine449bd832023-01-11 14:50:10 +01002869 if (ret != 0) {
2870 MBEDTLS_SSL_DEBUG_RET(2,
2871 "Creating the ticket-resumed PSK failed",
2872 ret);
2873 return ret;
Jerry Yue67bef42022-07-07 07:29:42 +00002874 }
Gilles Peskine449bd832023-01-11 14:50:10 +01002875 MBEDTLS_SSL_DEBUG_BUF(3, "Ticket-resumed PSK",
2876 session->resumption_key,
2877 session->resumption_key_len);
Jerry Yue67bef42022-07-07 07:29:42 +00002878
Gilles Peskine449bd832023-01-11 14:50:10 +01002879 MBEDTLS_SSL_DEBUG_BUF(3, "resumption_master_secret",
2880 session->app_secrets.resumption_master_secret,
2881 hash_length);
Jerry Yue67bef42022-07-07 07:29:42 +00002882
Gilles Peskine449bd832023-01-11 14:50:10 +01002883 return 0;
Jerry Yue67bef42022-07-07 07:29:42 +00002884}
2885
2886/* This function creates a NewSessionTicket message in the following format:
2887 *
2888 * struct {
2889 * uint32 ticket_lifetime;
2890 * uint32 ticket_age_add;
2891 * opaque ticket_nonce<0..255>;
2892 * opaque ticket<1..2^16-1>;
2893 * Extension extensions<0..2^16-2>;
2894 * } NewSessionTicket;
2895 *
2896 * The ticket inside the NewSessionTicket message is an encrypted container
2897 * carrying the necessary information so that the server is later able to
2898 * re-start the communication.
2899 *
2900 * The following fields are placed inside the ticket by the
2901 * f_ticket_write() function:
2902 *
2903 * - creation time (start)
2904 * - flags (flags)
2905 * - age add (ticket_age_add)
2906 * - key (key)
2907 * - key length (key_len)
2908 * - ciphersuite (ciphersuite)
2909 */
2910MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002911static int ssl_tls13_write_new_session_ticket_body(mbedtls_ssl_context *ssl,
2912 unsigned char *buf,
2913 unsigned char *end,
2914 size_t *out_len,
2915 unsigned char *ticket_nonce,
2916 size_t ticket_nonce_size)
Jerry Yue67bef42022-07-07 07:29:42 +00002917{
2918 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
2919 unsigned char *p = buf;
2920 mbedtls_ssl_session *session = ssl->session;
2921 size_t ticket_len;
2922 uint32_t ticket_lifetime;
2923
2924 *out_len = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01002925 MBEDTLS_SSL_DEBUG_MSG(2, ("=> write NewSessionTicket msg"));
Jerry Yue67bef42022-07-07 07:29:42 +00002926
2927 /*
2928 * ticket_lifetime 4 bytes
2929 * ticket_age_add 4 bytes
2930 * ticket_nonce 1 + ticket_nonce_size bytes
2931 * ticket >=2 bytes
2932 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002933 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 4 + 4 + 1 + ticket_nonce_size + 2);
Jerry Yue67bef42022-07-07 07:29:42 +00002934
2935 /* Generate ticket and ticket_lifetime */
Gilles Peskine449bd832023-01-11 14:50:10 +01002936 ret = ssl->conf->f_ticket_write(ssl->conf->p_ticket,
2937 session,
2938 p + 9 + ticket_nonce_size + 2,
2939 end,
2940 &ticket_len,
2941 &ticket_lifetime);
2942 if (ret != 0) {
2943 MBEDTLS_SSL_DEBUG_RET(1, "write_ticket", ret);
2944 return ret;
Jerry Yue67bef42022-07-07 07:29:42 +00002945 }
2946 /* RFC 8446 4.6.1
2947 * ticket_lifetime: Indicates the lifetime in seconds as a 32-bit
2948 * unsigned integer in network byte order from the time of ticket
2949 * issuance. Servers MUST NOT use any value greater than
2950 * 604800 seconds (7 days). The value of zero indicates that the
2951 * ticket should be discarded immediately. Clients MUST NOT cache
2952 * tickets for longer than 7 days, regardless of the ticket_lifetime,
2953 * and MAY delete tickets earlier based on local policy. A server
2954 * MAY treat a ticket as valid for a shorter period of time than what
2955 * is stated in the ticket_lifetime.
2956 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002957 if (ticket_lifetime > 604800) {
Xiaokang Qian28af5012022-10-13 08:18:19 +00002958 ticket_lifetime = 604800;
Gilles Peskine449bd832023-01-11 14:50:10 +01002959 }
2960 MBEDTLS_PUT_UINT32_BE(ticket_lifetime, p, 0);
2961 MBEDTLS_SSL_DEBUG_MSG(3, ("ticket_lifetime: %u",
2962 (unsigned int) ticket_lifetime));
Jerry Yue67bef42022-07-07 07:29:42 +00002963
2964 /* Write ticket_age_add */
Gilles Peskine449bd832023-01-11 14:50:10 +01002965 MBEDTLS_PUT_UINT32_BE(session->ticket_age_add, p, 4);
2966 MBEDTLS_SSL_DEBUG_MSG(3, ("ticket_age_add: %u",
2967 (unsigned int) session->ticket_age_add));
Jerry Yue67bef42022-07-07 07:29:42 +00002968
2969 /* Write ticket_nonce */
Gilles Peskine449bd832023-01-11 14:50:10 +01002970 p[8] = (unsigned char) ticket_nonce_size;
2971 if (ticket_nonce_size > 0) {
2972 memcpy(p + 9, ticket_nonce, ticket_nonce_size);
Jerry Yue67bef42022-07-07 07:29:42 +00002973 }
2974 p += 9 + ticket_nonce_size;
2975
2976 /* Write ticket */
Gilles Peskine449bd832023-01-11 14:50:10 +01002977 MBEDTLS_PUT_UINT16_BE(ticket_len, p, 0);
Jerry Yue67bef42022-07-07 07:29:42 +00002978 p += 2;
Gilles Peskine449bd832023-01-11 14:50:10 +01002979 MBEDTLS_SSL_DEBUG_BUF(4, "ticket", p, ticket_len);
Jerry Yue67bef42022-07-07 07:29:42 +00002980 p += ticket_len;
2981
2982 /* Ticket Extensions
2983 *
2984 * Note: We currently don't have any extensions.
2985 * Set length to zero.
2986 */
Jerry Yuedab6372022-10-31 14:37:31 +08002987 ssl->handshake->sent_extensions = MBEDTLS_SSL_EXT_MASK_NONE;
2988
Gilles Peskine449bd832023-01-11 14:50:10 +01002989 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 2);
2990 MBEDTLS_PUT_UINT16_BE(0, p, 0);
Jerry Yue67bef42022-07-07 07:29:42 +00002991 p += 2;
2992
2993 *out_len = p - buf;
Gilles Peskine449bd832023-01-11 14:50:10 +01002994 MBEDTLS_SSL_DEBUG_BUF(4, "ticket", buf, *out_len);
2995 MBEDTLS_SSL_DEBUG_MSG(2, ("<= write new session ticket"));
Jerry Yue67bef42022-07-07 07:29:42 +00002996
Jerry Yu7de2ff02022-11-08 21:43:46 +08002997 MBEDTLS_SSL_PRINT_EXTS(
Gilles Peskine449bd832023-01-11 14:50:10 +01002998 3, MBEDTLS_SSL_HS_NEW_SESSION_TICKET, ssl->handshake->sent_extensions);
Jerry Yu4b8f2f72022-10-31 13:31:22 +08002999
Gilles Peskine449bd832023-01-11 14:50:10 +01003000 return 0;
Jerry Yue67bef42022-07-07 07:29:42 +00003001}
3002
3003/*
Jerry Yua8d3c502022-10-30 14:51:23 +08003004 * Handler for MBEDTLS_SSL_TLS1_3_NEW_SESSION_TICKET
Jerry Yue67bef42022-07-07 07:29:42 +00003005 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003006static int ssl_tls13_write_new_session_ticket(mbedtls_ssl_context *ssl)
Jerry Yue67bef42022-07-07 07:29:42 +00003007{
3008 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
3009
Gilles Peskine449bd832023-01-11 14:50:10 +01003010 MBEDTLS_SSL_PROC_CHK_NEG(ssl_tls13_write_new_session_ticket_coordinate(ssl));
Jerry Yue67bef42022-07-07 07:29:42 +00003011
Gilles Peskine449bd832023-01-11 14:50:10 +01003012 if (ret == SSL_NEW_SESSION_TICKET_WRITE) {
Jerry Yue67bef42022-07-07 07:29:42 +00003013 unsigned char ticket_nonce[MBEDTLS_SSL_TLS1_3_TICKET_NONCE_LENGTH];
3014 unsigned char *buf;
3015 size_t buf_len, msg_len;
3016
Gilles Peskine449bd832023-01-11 14:50:10 +01003017 MBEDTLS_SSL_PROC_CHK(ssl_tls13_prepare_new_session_ticket(
3018 ssl, ticket_nonce, sizeof(ticket_nonce)));
Jerry Yue67bef42022-07-07 07:29:42 +00003019
Xiaokang Qian9f1747b2023-03-30 02:50:04 +00003020 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_start_handshake_msg(
3021 ssl, MBEDTLS_SSL_HS_NEW_SESSION_TICKET,
3022 &buf, &buf_len));
Jerry Yue67bef42022-07-07 07:29:42 +00003023
Gilles Peskine449bd832023-01-11 14:50:10 +01003024 MBEDTLS_SSL_PROC_CHK(ssl_tls13_write_new_session_ticket_body(
3025 ssl, buf, buf + buf_len, &msg_len,
3026 ticket_nonce, sizeof(ticket_nonce)));
Jerry Yue67bef42022-07-07 07:29:42 +00003027
Gilles Peskine449bd832023-01-11 14:50:10 +01003028 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_finish_handshake_msg(
3029 ssl, buf_len, msg_len));
Jerry Yufca4d572022-07-21 10:37:48 +08003030
Jerry Yu359e65f2022-09-22 23:47:43 +08003031 /* Limit session tickets count to one when resumption connection.
3032 *
3033 * See document of mbedtls_ssl_conf_new_session_tickets.
3034 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003035 if (ssl->handshake->resume == 1) {
Jerry Yu359e65f2022-09-22 23:47:43 +08003036 ssl->handshake->new_session_tickets_count = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01003037 } else {
Jerry Yu359e65f2022-09-22 23:47:43 +08003038 ssl->handshake->new_session_tickets_count--;
Gilles Peskine449bd832023-01-11 14:50:10 +01003039 }
Jerry Yub7e3fa72022-09-22 11:07:18 +08003040
Jerry Yua8d3c502022-10-30 14:51:23 +08003041 mbedtls_ssl_handshake_set_state(
Gilles Peskine449bd832023-01-11 14:50:10 +01003042 ssl, MBEDTLS_SSL_TLS1_3_NEW_SESSION_TICKET_FLUSH);
3043 } else {
3044 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_HANDSHAKE_OVER);
Jerry Yue67bef42022-07-07 07:29:42 +00003045 }
3046
Jerry Yue67bef42022-07-07 07:29:42 +00003047cleanup:
3048
Gilles Peskine449bd832023-01-11 14:50:10 +01003049 return ret;
Jerry Yue67bef42022-07-07 07:29:42 +00003050}
3051#endif /* MBEDTLS_SSL_SESSION_TICKETS */
3052
3053/*
XiaokangQiane8ff3502022-04-22 02:34:40 +00003054 * TLS 1.3 State Machine -- server side
XiaokangQian7807f9f2022-02-15 10:04:37 +00003055 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003056int mbedtls_ssl_tls13_handshake_server_step(mbedtls_ssl_context *ssl)
Jerry Yub9930e72021-08-06 17:11:51 +08003057{
XiaokangQian08037552022-04-20 07:16:41 +00003058 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
XiaokangQian7807f9f2022-02-15 10:04:37 +00003059
Gilles Peskine449bd832023-01-11 14:50:10 +01003060 if (ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER || ssl->handshake == NULL) {
3061 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
3062 }
XiaokangQian7807f9f2022-02-15 10:04:37 +00003063
Gilles Peskine449bd832023-01-11 14:50:10 +01003064 MBEDTLS_SSL_DEBUG_MSG(2, ("tls13 server state: %s(%d)",
Dave Rodgman2eab4622023-10-05 13:30:37 +01003065 mbedtls_ssl_states_str((mbedtls_ssl_states) ssl->state),
Gilles Peskine449bd832023-01-11 14:50:10 +01003066 ssl->state));
Jerry Yu6e81b272021-09-27 11:16:17 +08003067
Gilles Peskine449bd832023-01-11 14:50:10 +01003068 switch (ssl->state) {
XiaokangQian7807f9f2022-02-15 10:04:37 +00003069 /* start state */
3070 case MBEDTLS_SSL_HELLO_REQUEST:
Gilles Peskine449bd832023-01-11 14:50:10 +01003071 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_CLIENT_HELLO);
XiaokangQian08037552022-04-20 07:16:41 +00003072 ret = 0;
XiaokangQian7807f9f2022-02-15 10:04:37 +00003073 break;
3074
XiaokangQian7807f9f2022-02-15 10:04:37 +00003075 case MBEDTLS_SSL_CLIENT_HELLO:
Gilles Peskine449bd832023-01-11 14:50:10 +01003076 ret = ssl_tls13_process_client_hello(ssl);
3077 if (ret != 0) {
3078 MBEDTLS_SSL_DEBUG_RET(1, "ssl_tls13_process_client_hello", ret);
3079 }
Jerry Yuf41553b2022-05-09 22:20:30 +08003080 break;
XiaokangQian7807f9f2022-02-15 10:04:37 +00003081
Jerry Yuf41553b2022-05-09 22:20:30 +08003082 case MBEDTLS_SSL_HELLO_RETRY_REQUEST:
Gilles Peskine449bd832023-01-11 14:50:10 +01003083 ret = ssl_tls13_write_hello_retry_request(ssl);
3084 if (ret != 0) {
3085 MBEDTLS_SSL_DEBUG_RET(1, "ssl_tls13_write_hello_retry_request", ret);
3086 return ret;
Jerry Yuf41553b2022-05-09 22:20:30 +08003087 }
XiaokangQian7807f9f2022-02-15 10:04:37 +00003088 break;
3089
Jerry Yu5b64ae92022-03-30 17:15:02 +08003090 case MBEDTLS_SSL_SERVER_HELLO:
Gilles Peskine449bd832023-01-11 14:50:10 +01003091 ret = ssl_tls13_write_server_hello(ssl);
Jerry Yu5b64ae92022-03-30 17:15:02 +08003092 break;
3093
Xiaofei Baicba64af2022-02-15 10:00:56 +00003094 case MBEDTLS_SSL_ENCRYPTED_EXTENSIONS:
Gilles Peskine449bd832023-01-11 14:50:10 +01003095 ret = ssl_tls13_write_encrypted_extensions(ssl);
3096 if (ret != 0) {
3097 MBEDTLS_SSL_DEBUG_RET(1, "ssl_tls13_write_encrypted_extensions", ret);
3098 return ret;
Xiaofei Baicba64af2022-02-15 10:00:56 +00003099 }
Jerry Yu48330562022-05-06 21:35:44 +08003100 break;
Jerry Yu6a2cd9e2022-05-05 11:14:19 +08003101
Ronald Cron928cbd32022-10-04 16:14:26 +02003102#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED)
Xiaofei Bai9ca09d42022-02-14 12:57:18 +00003103 case MBEDTLS_SSL_CERTIFICATE_REQUEST:
Gilles Peskine449bd832023-01-11 14:50:10 +01003104 ret = ssl_tls13_write_certificate_request(ssl);
Xiaofei Bai9ca09d42022-02-14 12:57:18 +00003105 break;
Xiaofei Bai9ca09d42022-02-14 12:57:18 +00003106
Jerry Yu83da34e2022-04-16 13:59:52 +08003107 case MBEDTLS_SSL_SERVER_CERTIFICATE:
Gilles Peskine449bd832023-01-11 14:50:10 +01003108 ret = ssl_tls13_write_server_certificate(ssl);
Jerry Yu83da34e2022-04-16 13:59:52 +08003109 break;
3110
3111 case MBEDTLS_SSL_CERTIFICATE_VERIFY:
Gilles Peskine449bd832023-01-11 14:50:10 +01003112 ret = ssl_tls13_write_certificate_verify(ssl);
Jerry Yu83da34e2022-04-16 13:59:52 +08003113 break;
Ronald Cron928cbd32022-10-04 16:14:26 +02003114#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED */
Jerry Yu83da34e2022-04-16 13:59:52 +08003115
Gilles Peskine449bd832023-01-11 14:50:10 +01003116 /*
3117 * Injection of dummy-CCS's for middlebox compatibility
3118 */
Gabor Mezei7b39bf12022-05-24 16:04:14 +02003119#if defined(MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE)
Gabor Mezeif7044ea2022-06-28 16:01:49 +02003120 case MBEDTLS_SSL_SERVER_CCS_AFTER_HELLO_RETRY_REQUEST:
Gilles Peskine449bd832023-01-11 14:50:10 +01003121 ret = mbedtls_ssl_tls13_write_change_cipher_spec(ssl);
3122 if (ret == 0) {
3123 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_CLIENT_HELLO);
3124 }
Gabor Mezei7b39bf12022-05-24 16:04:14 +02003125 break;
3126
3127 case MBEDTLS_SSL_SERVER_CCS_AFTER_SERVER_HELLO:
Gilles Peskine449bd832023-01-11 14:50:10 +01003128 ret = mbedtls_ssl_tls13_write_change_cipher_spec(ssl);
3129 if (ret == 0) {
3130 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_ENCRYPTED_EXTENSIONS);
3131 }
Gabor Mezei7b39bf12022-05-24 16:04:14 +02003132 break;
3133#endif /* MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE */
3134
Jerry Yu69dd8d42022-04-16 12:51:26 +08003135 case MBEDTLS_SSL_SERVER_FINISHED:
Gilles Peskine449bd832023-01-11 14:50:10 +01003136 ret = ssl_tls13_write_server_finished(ssl);
Jerry Yu69dd8d42022-04-16 12:51:26 +08003137 break;
3138
3139 case MBEDTLS_SSL_CLIENT_FINISHED:
Gilles Peskine449bd832023-01-11 14:50:10 +01003140 ret = ssl_tls13_process_client_finished(ssl);
Jerry Yu69dd8d42022-04-16 12:51:26 +08003141 break;
3142
Jerry Yu69dd8d42022-04-16 12:51:26 +08003143 case MBEDTLS_SSL_HANDSHAKE_WRAPUP:
Gilles Peskine449bd832023-01-11 14:50:10 +01003144 ret = ssl_tls13_handshake_wrapup(ssl);
Jerry Yu69dd8d42022-04-16 12:51:26 +08003145 break;
3146
Ronald Cron766c0cd2022-10-18 12:17:11 +02003147#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED)
XiaokangQian6b916b12022-04-25 07:29:34 +00003148 case MBEDTLS_SSL_CLIENT_CERTIFICATE:
Gilles Peskine449bd832023-01-11 14:50:10 +01003149 ret = mbedtls_ssl_tls13_process_certificate(ssl);
3150 if (ret == 0) {
3151 if (ssl->session_negotiate->peer_cert != NULL) {
Ronald Cron209cae92022-06-07 10:30:19 +02003152 mbedtls_ssl_handshake_set_state(
Gilles Peskine449bd832023-01-11 14:50:10 +01003153 ssl, MBEDTLS_SSL_CLIENT_CERTIFICATE_VERIFY);
3154 } else {
3155 MBEDTLS_SSL_DEBUG_MSG(2, ("skip parse certificate verify"));
Ronald Cron209cae92022-06-07 10:30:19 +02003156 mbedtls_ssl_handshake_set_state(
Gilles Peskine449bd832023-01-11 14:50:10 +01003157 ssl, MBEDTLS_SSL_CLIENT_FINISHED);
Ronald Cron19385882022-06-15 16:26:13 +02003158 }
XiaokangQian6b916b12022-04-25 07:29:34 +00003159 }
3160 break;
3161
3162 case MBEDTLS_SSL_CLIENT_CERTIFICATE_VERIFY:
Gilles Peskine449bd832023-01-11 14:50:10 +01003163 ret = mbedtls_ssl_tls13_process_certificate_verify(ssl);
3164 if (ret == 0) {
XiaokangQian6b916b12022-04-25 07:29:34 +00003165 mbedtls_ssl_handshake_set_state(
Gilles Peskine449bd832023-01-11 14:50:10 +01003166 ssl, MBEDTLS_SSL_CLIENT_FINISHED);
XiaokangQian6b916b12022-04-25 07:29:34 +00003167 }
3168 break;
Ronald Cron766c0cd2022-10-18 12:17:11 +02003169#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED */
XiaokangQian6b916b12022-04-25 07:29:34 +00003170
Jerry Yue67bef42022-07-07 07:29:42 +00003171#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Jerry Yua8d3c502022-10-30 14:51:23 +08003172 case MBEDTLS_SSL_TLS1_3_NEW_SESSION_TICKET:
Gilles Peskine449bd832023-01-11 14:50:10 +01003173 ret = ssl_tls13_write_new_session_ticket(ssl);
3174 if (ret != 0) {
3175 MBEDTLS_SSL_DEBUG_RET(1,
3176 "ssl_tls13_write_new_session_ticket ",
3177 ret);
Jerry Yue67bef42022-07-07 07:29:42 +00003178 }
3179 break;
Jerry Yua8d3c502022-10-30 14:51:23 +08003180 case MBEDTLS_SSL_TLS1_3_NEW_SESSION_TICKET_FLUSH:
Jerry Yue67bef42022-07-07 07:29:42 +00003181 /* This state is necessary to do the flush of the New Session
Jerry Yua8d3c502022-10-30 14:51:23 +08003182 * Ticket message written in MBEDTLS_SSL_TLS1_3_NEW_SESSION_TICKET
Jerry Yue67bef42022-07-07 07:29:42 +00003183 * as part of ssl_prepare_handshake_step.
3184 */
3185 ret = 0;
Jerry Yud4e75002022-08-09 13:33:50 +08003186
Gilles Peskine449bd832023-01-11 14:50:10 +01003187 if (ssl->handshake->new_session_tickets_count == 0) {
3188 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_HANDSHAKE_OVER);
3189 } else {
Xiaokang Qian9f1747b2023-03-30 02:50:04 +00003190 mbedtls_ssl_handshake_set_state(
3191 ssl, MBEDTLS_SSL_TLS1_3_NEW_SESSION_TICKET);
Gilles Peskine449bd832023-01-11 14:50:10 +01003192 }
Jerry Yue67bef42022-07-07 07:29:42 +00003193 break;
3194
3195#endif /* MBEDTLS_SSL_SESSION_TICKETS */
3196
XiaokangQian7807f9f2022-02-15 10:04:37 +00003197 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01003198 MBEDTLS_SSL_DEBUG_MSG(1, ("invalid state %d", ssl->state));
3199 return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
XiaokangQian7807f9f2022-02-15 10:04:37 +00003200 }
3201
Gilles Peskine449bd832023-01-11 14:50:10 +01003202 return ret;
Jerry Yub9930e72021-08-06 17:11:51 +08003203}
Jerry Yu3cc4c2a2021-08-06 16:29:08 +08003204
Jerry Yufb4b6472022-01-27 15:03:26 +08003205#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_PROTO_TLS1_3 */