scorpiox-bot-plugin-webhook
←ssl_tls13_client.cc
3208 lines · 106 KBHEADBlameDownload
1/*
2 * TLS 1.3 client-side functions
3 *
4 * Copyright The Mbed TLS Contributors
5 * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
6 */
7
8#include "common.h"
9
10#if defined(MBEDTLS_SSL_CLI_C) && defined(MBEDTLS_SSL_PROTO_TLS1_3)
11
12#include <string.h>
13
14#include "debug_internal.h"
15#include "mbedtls/error.h"
16#include "mbedtls/platform.h"
17
18#include "ssl_misc.h"
19#include "ssl_client.h"
20#include "ssl_tls13_keys.h"
21#include "ssl_debug_helpers.h"
22#include "mbedtls/psa_util.h"
23
24#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_EPHEMERAL_ENABLED)
25/* Define a local translating function to save code size by not using too many
26 * arguments in each translating place. */
27static int local_err_translation(psa_status_t status)
28{
29 return psa_status_to_mbedtls(status, psa_to_ssl_errors,
30 ARRAY_LENGTH(psa_to_ssl_errors),
31 psa_generic_status_to_mbedtls);
32}
33#define PSA_TO_MBEDTLS_ERR(status) local_err_translation(status)
34#endif
35
36/* Write extensions */
37
38/*
39 * ssl_tls13_write_supported_versions_ext():
40 *
41 * struct {
42 * ProtocolVersion versions<2..254>;
43 * } SupportedVersions;
44 */
45MBEDTLS_CHECK_RETURN_CRITICAL
46static int ssl_tls13_write_supported_versions_ext(mbedtls_ssl_context *ssl,
47 unsigned char *buf,
48 unsigned char *end,
49 size_t *out_len)
50{
51 unsigned char *p = buf;
52 unsigned char versions_len = (ssl->handshake->min_tls_version <=
53 MBEDTLS_SSL_VERSION_TLS1_2) ? 4 : 2;
54
55 *out_len = 0;
56
57 MBEDTLS_SSL_DEBUG_MSG(3, ("client hello, adding supported versions extension"));
58
59 /* Check if we have space to write the extension:
60 * - extension_type (2 bytes)
61 * - extension_data_length (2 bytes)
62 * - versions_length (1 byte )
63 * - versions (2 or 4 bytes)
64 */
65 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 5 + versions_len);
66
67 MBEDTLS_PUT_UINT16_BE(MBEDTLS_TLS_EXT_SUPPORTED_VERSIONS, p, 0);
68 MBEDTLS_PUT_UINT16_BE(versions_len + 1, p, 2);
69 p += 4;
70
71 /* Length of versions */
72 *p++ = versions_len;
73
74 /* Write values of supported versions.
75 * They are defined by the configuration.
76 * Currently, we advertise only TLS 1.3 or both TLS 1.3 and TLS 1.2.
77 */
78 mbedtls_ssl_write_version(p, MBEDTLS_SSL_TRANSPORT_STREAM,
79 MBEDTLS_SSL_VERSION_TLS1_3);
80 MBEDTLS_SSL_DEBUG_MSG(3, ("supported version: [3:4]"));
81
82
83 if (ssl->handshake->min_tls_version <= MBEDTLS_SSL_VERSION_TLS1_2) {
84 mbedtls_ssl_write_version(p + 2, MBEDTLS_SSL_TRANSPORT_STREAM,
85 MBEDTLS_SSL_VERSION_TLS1_2);
86 MBEDTLS_SSL_DEBUG_MSG(3, ("supported version: [3:3]"));
87 }
88
89 *out_len = 5 + versions_len;
90
91 mbedtls_ssl_tls13_set_hs_sent_ext_mask(
92 ssl, MBEDTLS_TLS_EXT_SUPPORTED_VERSIONS);
93
94 return 0;
95}
96
97MBEDTLS_CHECK_RETURN_CRITICAL
98static int ssl_tls13_parse_supported_versions_ext(mbedtls_ssl_context *ssl,
99 const unsigned char *buf,
100 const unsigned char *end)
101{
102 ((void) ssl);
103
104 MBEDTLS_SSL_CHK_BUF_READ_PTR(buf, end, 2);
105 if (mbedtls_ssl_read_version(buf, ssl->conf->transport) !=
106 MBEDTLS_SSL_VERSION_TLS1_3) {
107 MBEDTLS_SSL_DEBUG_MSG(1, ("unexpected version"));
108
109 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER,
110 MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER);
111 return MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER;
112 }
113
114 if (&buf[2] != end) {
115 MBEDTLS_SSL_DEBUG_MSG(
116 1, ("supported_versions ext data length incorrect"));
117 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR,
118 MBEDTLS_ERR_SSL_DECODE_ERROR);
119 return MBEDTLS_ERR_SSL_DECODE_ERROR;
120 }
121
122 return 0;
123}
124
125#if defined(MBEDTLS_SSL_ALPN)
126MBEDTLS_CHECK_RETURN_CRITICAL
127static int ssl_tls13_parse_alpn_ext(mbedtls_ssl_context *ssl,
128 const unsigned char *buf, size_t len)
129{
130 const unsigned char *p = buf;
131 const unsigned char *end = buf + len;
132 size_t protocol_name_list_len, protocol_name_len;
133 const unsigned char *protocol_name_list_end;
134
135 /* If we didn't send it, the server shouldn't send it */
136 if (ssl->conf->alpn_list == NULL) {
137 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
138 }
139
140 /*
141 * opaque ProtocolName<1..2^8-1>;
142 *
143 * struct {
144 * ProtocolName protocol_name_list<2..2^16-1>
145 * } ProtocolNameList;
146 *
147 * the "ProtocolNameList" MUST contain exactly one "ProtocolName"
148 */
149
150 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, 2);
151 protocol_name_list_len = MBEDTLS_GET_UINT16_BE(p, 0);
152 p += 2;
153
154 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, protocol_name_list_len);
155 protocol_name_list_end = p + protocol_name_list_len;
156
157 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, protocol_name_list_end, 1);
158 protocol_name_len = *p++;
159
160 /* Check that the server chosen protocol was in our list and save it */
161 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, protocol_name_list_end, protocol_name_len);
162 for (const char **alpn = ssl->conf->alpn_list; *alpn != NULL; alpn++) {
163 if (protocol_name_len == strlen(*alpn) &&
164 memcmp(p, *alpn, protocol_name_len) == 0) {
165 ssl->alpn_chosen = *alpn;
166 return 0;
167 }
168 }
169
170 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
171}
172#endif /* MBEDTLS_SSL_ALPN */
173
174MBEDTLS_CHECK_RETURN_CRITICAL
175static int ssl_tls13_reset_key_share(mbedtls_ssl_context *ssl)
176{
177 uint16_t group_id = ssl->handshake->offered_group_id;
178
179 if (group_id == 0) {
180 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
181 }
182
183#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_EPHEMERAL_ENABLED)
184 if (mbedtls_ssl_tls13_named_group_is_ecdhe(group_id) ||
185 mbedtls_ssl_tls13_named_group_is_ffdh(group_id)) {
186 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
187 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
188
189 /* Destroy generated private key. */
190 status = psa_destroy_key(ssl->handshake->xxdh_psa_privkey);
191 if (status != PSA_SUCCESS) {
192 ret = PSA_TO_MBEDTLS_ERR(status);
193 MBEDTLS_SSL_DEBUG_RET(1, "psa_destroy_key", ret);
194 return ret;
195 }
196
197 ssl->handshake->xxdh_psa_privkey = MBEDTLS_SVC_KEY_ID_INIT;
198 return 0;
199 } else
200#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_EPHEMERAL_ENABLED */
201 if (0 /* other KEMs? */) {
202 /* Do something */
203 }
204
205 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
206}
207
208/*
209 * Functions for writing key_share extension.
210 */
211#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_EPHEMERAL_ENABLED)
212MBEDTLS_CHECK_RETURN_CRITICAL
213static int ssl_tls13_get_default_group_id(mbedtls_ssl_context *ssl,
214 uint16_t *group_id)
215{
216 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
217
218
219#if defined(PSA_WANT_ALG_ECDH) || defined(PSA_WANT_ALG_FFDH)
220 const uint16_t *group_list = mbedtls_ssl_get_groups(ssl);
221 /* Pick first available ECDHE group compatible with TLS 1.3 */
222 if (group_list == NULL) {
223 return MBEDTLS_ERR_SSL_BAD_CONFIG;
224 }
225
226 for (; *group_list != 0; group_list++) {
227#if defined(PSA_WANT_ALG_ECDH)
228 if ((mbedtls_ssl_get_psa_curve_info_from_tls_id(
229 *group_list, NULL, NULL) == PSA_SUCCESS) &&
230 mbedtls_ssl_tls13_named_group_is_ecdhe(*group_list)) {
231 *group_id = *group_list;
232 return 0;
233 }
234#endif
235#if defined(PSA_WANT_ALG_FFDH)
236 if (mbedtls_ssl_tls13_named_group_is_ffdh(*group_list)) {
237 *group_id = *group_list;
238 return 0;
239 }
240#endif
241 }
242#else
243 ((void) ssl);
244 ((void) group_id);
245#endif /* PSA_WANT_ALG_ECDH || PSA_WANT_ALG_FFDH */
246
247 return ret;
248}
249
250/*
251 * ssl_tls13_write_key_share_ext
252 *
253 * Structure of key_share extension in ClientHello:
254 *
255 * struct {
256 * NamedGroup group;
257 * opaque key_exchange<1..2^16-1>;
258 * } KeyShareEntry;
259 * struct {
260 * KeyShareEntry client_shares<0..2^16-1>;
261 * } KeyShareClientHello;
262 */
263MBEDTLS_CHECK_RETURN_CRITICAL
264static int ssl_tls13_write_key_share_ext(mbedtls_ssl_context *ssl,
265 unsigned char *buf,
266 unsigned char *end,
267 size_t *out_len)
268{
269 unsigned char *p = buf;
270 unsigned char *client_shares; /* Start of client_shares */
271 size_t client_shares_len; /* Length of client_shares */
272 uint16_t group_id;
273 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
274
275 *out_len = 0;
276
277 /* Check if we have space for header and length fields:
278 * - extension_type (2 bytes)
279 * - extension_data_length (2 bytes)
280 * - client_shares_length (2 bytes)
281 */
282 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 6);
283 p += 6;
284
285 MBEDTLS_SSL_DEBUG_MSG(3, ("client hello: adding key share extension"));
286
287 /* HRR could already have requested something else. */
288 group_id = ssl->handshake->offered_group_id;
289 if (!mbedtls_ssl_tls13_named_group_is_ecdhe(group_id) &&
290 !mbedtls_ssl_tls13_named_group_is_ffdh(group_id)) {
291 MBEDTLS_SSL_PROC_CHK(ssl_tls13_get_default_group_id(ssl,
292 &group_id));
293 }
294
295 /*
296 * Dispatch to type-specific key generation function.
297 *
298 * So far, we're only supporting ECDHE. With the introduction
299 * of PQC KEMs, we'll want to have multiple branches, one per
300 * type of KEM, and dispatch to the corresponding crypto. And
301 * only one key share entry is allowed.
302 */
303 client_shares = p;
304#if defined(PSA_WANT_ALG_ECDH) || defined(PSA_WANT_ALG_FFDH)
305 if (mbedtls_ssl_tls13_named_group_is_ecdhe(group_id) ||
306 mbedtls_ssl_tls13_named_group_is_ffdh(group_id)) {
307 /* Pointer to group */
308 unsigned char *group = p;
309 /* Length of key_exchange */
310 size_t key_exchange_len = 0;
311
312 /* Check there is space for header of KeyShareEntry
313 * - group (2 bytes)
314 * - key_exchange_length (2 bytes)
315 */
316 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 4);
317 p += 4;
318 ret = mbedtls_ssl_tls13_generate_and_write_xxdh_key_exchange(
319 ssl, group_id, p, end, &key_exchange_len);
320 p += key_exchange_len;
321 if (ret != 0) {
322 MBEDTLS_SSL_DEBUG_MSG(1, ("client hello: failed generating xxdh key exchange"));
323 return ret;
324 }
325
326 /* Write group */
327 MBEDTLS_PUT_UINT16_BE(group_id, group, 0);
328 /* Write key_exchange_length */
329 MBEDTLS_PUT_UINT16_BE(key_exchange_len, group, 2);
330 } else
331#endif /* PSA_WANT_ALG_ECDH || PSA_WANT_ALG_FFDH */
332 if (0 /* other KEMs? */) {
333 /* Do something */
334 } else {
335 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
336 }
337
338 /* Length of client_shares */
339 client_shares_len = p - client_shares;
340 if (client_shares_len == 0) {
341 MBEDTLS_SSL_DEBUG_MSG(1, ("No key share defined."));
342 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
343 }
344 /* Write extension_type */
345 MBEDTLS_PUT_UINT16_BE(MBEDTLS_TLS_EXT_KEY_SHARE, buf, 0);
346 /* Write extension_data_length */
347 MBEDTLS_PUT_UINT16_BE(client_shares_len + 2, buf, 2);
348 /* Write client_shares_length */
349 MBEDTLS_PUT_UINT16_BE(client_shares_len, buf, 4);
350
351 /* Update offered_group_id field */
352 ssl->handshake->offered_group_id = group_id;
353
354 /* Output the total length of key_share extension. */
355 *out_len = p - buf;
356
357 MBEDTLS_SSL_DEBUG_BUF(
358 3, "client hello, key_share extension", buf, *out_len);
359
360 mbedtls_ssl_tls13_set_hs_sent_ext_mask(ssl, MBEDTLS_TLS_EXT_KEY_SHARE);
361
362cleanup:
363
364 return ret;
365}
366#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_EPHEMERAL_ENABLED */
367
368/*
369 * ssl_tls13_parse_hrr_key_share_ext()
370 * Parse key_share extension in Hello Retry Request
371 *
372 * struct {
373 * NamedGroup selected_group;
374 * } KeyShareHelloRetryRequest;
375 */
376MBEDTLS_CHECK_RETURN_CRITICAL
377static int ssl_tls13_parse_hrr_key_share_ext(mbedtls_ssl_context *ssl,
378 const unsigned char *buf,
379 const unsigned char *end)
380{
381#if defined(PSA_WANT_ALG_ECDH) || defined(PSA_WANT_ALG_FFDH)
382 const unsigned char *p = buf;
383 int selected_group;
384 int found = 0;
385
386 const uint16_t *group_list = mbedtls_ssl_get_groups(ssl);
387 if (group_list == NULL) {
388 return MBEDTLS_ERR_SSL_BAD_CONFIG;
389 }
390
391 MBEDTLS_SSL_DEBUG_BUF(3, "key_share extension", p, end - buf);
392
393 /* Read selected_group */
394 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, 2);
395 selected_group = MBEDTLS_GET_UINT16_BE(p, 0);
396 MBEDTLS_SSL_DEBUG_MSG(3, ("selected_group ( %d )", selected_group));
397
398 /* Upon receipt of this extension in a HelloRetryRequest, the client
399 * MUST first verify that the selected_group field corresponds to a
400 * group which was provided in the "supported_groups" extension in the
401 * original ClientHello.
402 * The supported_group was based on the info in ssl->conf->group_list.
403 *
404 * If the server provided a key share that was not sent in the ClientHello
405 * then the client MUST abort the handshake with an "illegal_parameter" alert.
406 */
407 for (; *group_list != 0; group_list++) {
408 if (*group_list != selected_group) {
409 continue;
410 }
411#if defined(PSA_WANT_ALG_ECDH)
412 if (mbedtls_ssl_tls13_named_group_is_ecdhe(*group_list)) {
413 if (mbedtls_ssl_get_psa_curve_info_from_tls_id(
414 *group_list, NULL, NULL) == PSA_ERROR_NOT_SUPPORTED) {
415 continue;
416 }
417 /* Found only if psa_curve is supported and group_list == selected_group */
418 found = 1;
419 break;
420 }
421#endif /* PSA_WANT_ALG_ECDH */
422#if defined(PSA_WANT_ALG_FFDH)
423 if (mbedtls_ssl_tls13_named_group_is_ffdh(*group_list)) {
424 found = 1;
425 break;
426 }
427#endif /* PSA_WANT_ALG_FFDH */
428 }
429
430 /* Client MUST verify that the selected_group field does not
431 * correspond to a group which was provided in the "key_share"
432 * extension in the original ClientHello. If the server sent an
433 * HRR message with a key share already provided in the
434 * ClientHello then the client MUST abort the handshake with
435 * an "illegal_parameter" alert.
436 */
437 if (found == 0 || selected_group == ssl->handshake->offered_group_id) {
438 MBEDTLS_SSL_DEBUG_MSG(1, ("Invalid key share in HRR"));
439 MBEDTLS_SSL_PEND_FATAL_ALERT(
440 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER,
441 MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER);
442 return MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER;
443 }
444
445 /* Remember server's preference for next ClientHello */
446 ssl->handshake->offered_group_id = selected_group;
447
448 return 0;
449#else /* PSA_WANT_ALG_ECDH || PSA_WANT_ALG_FFDH */
450 (void) ssl;
451 (void) buf;
452 (void) end;
453 return MBEDTLS_ERR_SSL_BAD_CONFIG;
454#endif /* PSA_WANT_ALG_ECDH || PSA_WANT_ALG_FFDH */
455}
456
457/*
458 * ssl_tls13_parse_key_share_ext()
459 * Parse key_share extension in Server Hello
460 *
461 * struct {
462 * KeyShareEntry server_share;
463 * } KeyShareServerHello;
464 * struct {
465 * NamedGroup group;
466 * opaque key_exchange<1..2^16-1>;
467 * } KeyShareEntry;
468 */
469MBEDTLS_CHECK_RETURN_CRITICAL
470static int ssl_tls13_parse_key_share_ext(mbedtls_ssl_context *ssl,
471 const unsigned char *buf,
472 const unsigned char *end)
473{
474 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
475 const unsigned char *p = buf;
476 uint16_t group, offered_group;
477
478 /* ...
479 * NamedGroup group; (2 bytes)
480 * ...
481 */
482 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, 2);
483 group = MBEDTLS_GET_UINT16_BE(p, 0);
484 p += 2;
485
486 /* Check that the chosen group matches the one we offered. */
487 offered_group = ssl->handshake->offered_group_id;
488 if (offered_group != group) {
489 MBEDTLS_SSL_DEBUG_MSG(
490 1, ("Invalid server key share, our group %u, their group %u",
491 (unsigned) offered_group, (unsigned) group));
492 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE,
493 MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE);
494 return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
495 }
496
497#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_EPHEMERAL_ENABLED)
498 if (mbedtls_ssl_tls13_named_group_is_ecdhe(group) ||
499 mbedtls_ssl_tls13_named_group_is_ffdh(group)) {
500 MBEDTLS_SSL_DEBUG_MSG(2,
501 ("DHE group name: %s", mbedtls_ssl_named_group_to_str(group)));
502 ret = mbedtls_ssl_tls13_read_public_xxdhe_share(ssl, p, end - p);
503 if (ret != 0) {
504 return ret;
505 }
506 } else
507#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_EPHEMERAL_ENABLED */
508 if (0 /* other KEMs? */) {
509 /* Do something */
510 } else {
511 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
512 }
513
514 return ret;
515}
516
517/*
518 * ssl_tls13_parse_cookie_ext()
519 * Parse cookie extension in Hello Retry Request
520 *
521 * struct {
522 * opaque cookie<1..2^16-1>;
523 * } Cookie;
524 *
525 * When sending a HelloRetryRequest, the server MAY provide a "cookie"
526 * extension to the client (this is an exception to the usual rule that
527 * the only extensions that may be sent are those that appear in the
528 * ClientHello). When sending the new ClientHello, the client MUST copy
529 * the contents of the extension received in the HelloRetryRequest into
530 * a "cookie" extension in the new ClientHello. Clients MUST NOT use
531 * cookies in their initial ClientHello in subsequent connections.
532 */
533MBEDTLS_CHECK_RETURN_CRITICAL
534static int ssl_tls13_parse_cookie_ext(mbedtls_ssl_context *ssl,
535 const unsigned char *buf,
536 const unsigned char *end)
537{
538 uint16_t cookie_len;
539 const unsigned char *p = buf;
540 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
541
542 /* Retrieve length field of cookie */
543 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, 2);
544 cookie_len = MBEDTLS_GET_UINT16_BE(p, 0);
545 p += 2;
546
547 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, cookie_len);
548 MBEDTLS_SSL_DEBUG_BUF(3, "cookie extension", p, cookie_len);
549
550 mbedtls_free(handshake->cookie);
551 handshake->cookie_len = 0;
552 handshake->cookie = mbedtls_calloc(1, cookie_len);
553 if (handshake->cookie == NULL) {
554 MBEDTLS_SSL_DEBUG_MSG(1,
555 ("alloc failed ( %ud bytes )",
556 cookie_len));
557 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
558 }
559
560 memcpy(handshake->cookie, p, cookie_len);
561 handshake->cookie_len = cookie_len;
562
563 return 0;
564}
565
566MBEDTLS_CHECK_RETURN_CRITICAL
567static int ssl_tls13_write_cookie_ext(mbedtls_ssl_context *ssl,
568 unsigned char *buf,
569 unsigned char *end,
570 size_t *out_len)
571{
572 unsigned char *p = buf;
573 *out_len = 0;
574 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
575
576 if (handshake->cookie == NULL) {
577 MBEDTLS_SSL_DEBUG_MSG(3, ("no cookie to send; skip extension"));
578 return 0;
579 }
580
581 MBEDTLS_SSL_DEBUG_BUF(3, "client hello, cookie",
582 handshake->cookie,
583 handshake->cookie_len);
584
585 MBEDTLS_SSL_CHK_BUF_PTR(p, end, handshake->cookie_len + 6);
586
587 MBEDTLS_SSL_DEBUG_MSG(3, ("client hello, adding cookie extension"));
588
589 MBEDTLS_PUT_UINT16_BE(MBEDTLS_TLS_EXT_COOKIE, p, 0);
590 MBEDTLS_PUT_UINT16_BE(handshake->cookie_len + 2, p, 2);
591 MBEDTLS_PUT_UINT16_BE(handshake->cookie_len, p, 4);
592 p += 6;
593
594 /* Cookie */
595 memcpy(p, handshake->cookie, handshake->cookie_len);
596
597 *out_len = handshake->cookie_len + 6;
598
599 mbedtls_ssl_tls13_set_hs_sent_ext_mask(ssl, MBEDTLS_TLS_EXT_COOKIE);
600
601 return 0;
602}
603
604#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED)
605/*
606 * ssl_tls13_write_psk_key_exchange_modes_ext() structure:
607 *
608 * enum { psk_ke( 0 ), psk_dhe_ke( 1 ), ( 255 ) } PskKeyExchangeMode;
609 *
610 * struct {
611 * PskKeyExchangeMode ke_modes<1..255>;
612 * } PskKeyExchangeModes;
613 */
614MBEDTLS_CHECK_RETURN_CRITICAL
615static int ssl_tls13_write_psk_key_exchange_modes_ext(mbedtls_ssl_context *ssl,
616 unsigned char *buf,
617 unsigned char *end,
618 size_t *out_len)
619{
620 unsigned char *p = buf;
621 int ke_modes_len = 0;
622
623 ((void) ke_modes_len);
624 *out_len = 0;
625
626 /* Skip writing extension if no PSK key exchange mode
627 * is enabled in the config.
628 */
629 if (!mbedtls_ssl_conf_tls13_is_some_psk_enabled(ssl)) {
630 MBEDTLS_SSL_DEBUG_MSG(3, ("skip psk_key_exchange_modes extension"));
631 return 0;
632 }
633
634 /* Require 7 bytes of data, otherwise fail,
635 * even if extension might be shorter.
636 */
637 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 7);
638 MBEDTLS_SSL_DEBUG_MSG(
639 3, ("client hello, adding psk_key_exchange_modes extension"));
640
641 MBEDTLS_PUT_UINT16_BE(MBEDTLS_TLS_EXT_PSK_KEY_EXCHANGE_MODES, p, 0);
642
643 /* Skip extension length (2 bytes) and
644 * ke_modes length (1 byte) for now.
645 */
646 p += 5;
647
648 if (mbedtls_ssl_conf_tls13_is_psk_ephemeral_enabled(ssl)) {
649 *p++ = MBEDTLS_SSL_TLS1_3_PSK_MODE_ECDHE;
650 ke_modes_len++;
651
652 MBEDTLS_SSL_DEBUG_MSG(4, ("Adding PSK-ECDHE key exchange mode"));
653 }
654
655 if (mbedtls_ssl_conf_tls13_is_psk_enabled(ssl)) {
656 *p++ = MBEDTLS_SSL_TLS1_3_PSK_MODE_PURE;
657 ke_modes_len++;
658
659 MBEDTLS_SSL_DEBUG_MSG(4, ("Adding pure PSK key exchange mode"));
660 }
661
662 /* Now write the extension and ke_modes length */
663 MBEDTLS_PUT_UINT16_BE(ke_modes_len + 1, buf, 2);
664 buf[4] = ke_modes_len;
665
666 *out_len = p - buf;
667
668 mbedtls_ssl_tls13_set_hs_sent_ext_mask(
669 ssl, MBEDTLS_TLS_EXT_PSK_KEY_EXCHANGE_MODES);
670
671 return 0;
672}
673
674#if defined(MBEDTLS_SSL_SESSION_TICKETS)
675static psa_algorithm_t ssl_tls13_get_ciphersuite_hash_alg(int ciphersuite)
676{
677 const mbedtls_ssl_ciphersuite_t *ciphersuite_info = NULL;
678 ciphersuite_info = mbedtls_ssl_ciphersuite_from_id(ciphersuite);
679
680 if (ciphersuite_info != NULL) {
681 return mbedtls_md_psa_alg_from_type((mbedtls_md_type_t) ciphersuite_info->mac);
682 }
683
684 return PSA_ALG_NONE;
685}
686
687static int ssl_tls13_has_configured_ticket(mbedtls_ssl_context *ssl)
688{
689 mbedtls_ssl_session *session = ssl->session_negotiate;
690 return ssl->handshake->resume &&
691 session != NULL && session->ticket != NULL &&
692 mbedtls_ssl_conf_tls13_is_kex_mode_enabled(
693 ssl, mbedtls_ssl_tls13_session_get_ticket_flags(
694 session, MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK_ALL));
695}
696
697#if defined(MBEDTLS_SSL_EARLY_DATA)
698static int ssl_tls13_early_data_has_valid_ticket(mbedtls_ssl_context *ssl)
699{
700 mbedtls_ssl_session *session = ssl->session_negotiate;
701 return ssl->handshake->resume &&
702 session->tls_version == MBEDTLS_SSL_VERSION_TLS1_3 &&
703 mbedtls_ssl_tls13_session_ticket_allow_early_data(session) &&
704 mbedtls_ssl_tls13_cipher_suite_is_offered(ssl, session->ciphersuite);
705}
706#endif
707
708MBEDTLS_CHECK_RETURN_CRITICAL
709static int ssl_tls13_ticket_get_identity(mbedtls_ssl_context *ssl,
710 psa_algorithm_t *hash_alg,
711 const unsigned char **identity,
712 size_t *identity_len)
713{
714 mbedtls_ssl_session *session = ssl->session_negotiate;
715
716 if (!ssl_tls13_has_configured_ticket(ssl)) {
717 return -1;
718 }
719
720 *hash_alg = ssl_tls13_get_ciphersuite_hash_alg(session->ciphersuite);
721 *identity = session->ticket;
722 *identity_len = session->ticket_len;
723 return 0;
724}
725
726MBEDTLS_CHECK_RETURN_CRITICAL
727static int ssl_tls13_ticket_get_psk(mbedtls_ssl_context *ssl,
728 psa_algorithm_t *hash_alg,
729 const unsigned char **psk,
730 size_t *psk_len)
731{
732
733 mbedtls_ssl_session *session = ssl->session_negotiate;
734
735 if (!ssl_tls13_has_configured_ticket(ssl)) {
736 return -1;
737 }
738
739 *hash_alg = ssl_tls13_get_ciphersuite_hash_alg(session->ciphersuite);
740 *psk = session->resumption_key;
741 *psk_len = session->resumption_key_len;
742
743 return 0;
744}
745#endif /* MBEDTLS_SSL_SESSION_TICKETS */
746
747MBEDTLS_CHECK_RETURN_CRITICAL
748static int ssl_tls13_psk_get_identity(mbedtls_ssl_context *ssl,
749 psa_algorithm_t *hash_alg,
750 const unsigned char **identity,
751 size_t *identity_len)
752{
753
754 if (!mbedtls_ssl_conf_has_static_psk(ssl->conf)) {
755 return -1;
756 }
757
758 *hash_alg = PSA_ALG_SHA_256;
759 *identity = ssl->conf->psk_identity;
760 *identity_len = ssl->conf->psk_identity_len;
761 return 0;
762}
763
764MBEDTLS_CHECK_RETURN_CRITICAL
765static int ssl_tls13_psk_get_psk(mbedtls_ssl_context *ssl,
766 psa_algorithm_t *hash_alg,
767 const unsigned char **psk,
768 size_t *psk_len)
769{
770
771 if (!mbedtls_ssl_conf_has_static_psk(ssl->conf)) {
772 return -1;
773 }
774
775 *hash_alg = PSA_ALG_SHA_256;
776 *psk = ssl->conf->psk;
777 *psk_len = ssl->conf->psk_len;
778 return 0;
779}
780
781static int ssl_tls13_get_configured_psk_count(mbedtls_ssl_context *ssl)
782{
783 int configured_psk_count = 0;
784#if defined(MBEDTLS_SSL_SESSION_TICKETS)
785 if (ssl_tls13_has_configured_ticket(ssl)) {
786 MBEDTLS_SSL_DEBUG_MSG(3, ("Ticket is configured"));
787 configured_psk_count++;
788 }
789#endif
790 if (mbedtls_ssl_conf_has_static_psk(ssl->conf)) {
791 MBEDTLS_SSL_DEBUG_MSG(3, ("PSK is configured"));
792 configured_psk_count++;
793 }
794 return configured_psk_count;
795}
796
797MBEDTLS_CHECK_RETURN_CRITICAL
798static int ssl_tls13_write_identity(mbedtls_ssl_context *ssl,
799 unsigned char *buf,
800 unsigned char *end,
801 const unsigned char *identity,
802 size_t identity_len,
803 uint32_t obfuscated_ticket_age,
804 size_t *out_len)
805{
806 ((void) ssl);
807 *out_len = 0;
808
809 /*
810 * - identity_len (2 bytes)
811 * - identity (psk_identity_len bytes)
812 * - obfuscated_ticket_age (4 bytes)
813 */
814 MBEDTLS_SSL_CHK_BUF_PTR(buf, end, 6 + identity_len);
815
816 MBEDTLS_PUT_UINT16_BE(identity_len, buf, 0);
817 memcpy(buf + 2, identity, identity_len);
818 MBEDTLS_PUT_UINT32_BE(obfuscated_ticket_age, buf, 2 + identity_len);
819
820 MBEDTLS_SSL_DEBUG_BUF(4, "write identity", buf, 6 + identity_len);
821
822 *out_len = 6 + identity_len;
823
824 return 0;
825}
826
827MBEDTLS_CHECK_RETURN_CRITICAL
828static int ssl_tls13_write_binder(mbedtls_ssl_context *ssl,
829 unsigned char *buf,
830 unsigned char *end,
831 int psk_type,
832 psa_algorithm_t hash_alg,
833 const unsigned char *psk,
834 size_t psk_len,
835 size_t *out_len)
836{
837 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
838 unsigned char binder_len;
839 unsigned char transcript[MBEDTLS_TLS1_3_MD_MAX_SIZE];
840 size_t transcript_len = 0;
841
842 *out_len = 0;
843
844 binder_len = PSA_HASH_LENGTH(hash_alg);
845
846 /*
847 * - binder_len (1 bytes)
848 * - binder (binder_len bytes)
849 */
850 MBEDTLS_SSL_CHK_BUF_PTR(buf, end, 1 + binder_len);
851
852 buf[0] = binder_len;
853
854 /* Get current state of handshake transcript. */
855 ret = mbedtls_ssl_get_handshake_transcript(
856 ssl, mbedtls_md_type_from_psa_alg(hash_alg),
857 transcript, sizeof(transcript), &transcript_len);
858 if (ret != 0) {
859 return ret;
860 }
861
862 ret = mbedtls_ssl_tls13_create_psk_binder(ssl, hash_alg,
863 psk, psk_len, psk_type,
864 transcript, buf + 1);
865 if (ret != 0) {
866 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_tls13_create_psk_binder", ret);
867 return ret;
868 }
869 MBEDTLS_SSL_DEBUG_BUF(4, "write binder", buf, 1 + binder_len);
870
871 *out_len = 1 + binder_len;
872
873 return 0;
874}
875
876/*
877 * mbedtls_ssl_tls13_write_identities_of_pre_shared_key_ext() structure:
878 *
879 * struct {
880 * opaque identity<1..2^16-1>;
881 * uint32 obfuscated_ticket_age;
882 * } PskIdentity;
883 *
884 * opaque PskBinderEntry<32..255>;
885 *
886 * struct {
887 * PskIdentity identities<7..2^16-1>;
888 * PskBinderEntry binders<33..2^16-1>;
889 * } OfferedPsks;
890 *
891 * struct {
892 * select (Handshake.msg_type) {
893 * case client_hello: OfferedPsks;
894 * ...
895 * };
896 * } PreSharedKeyExtension;
897 *
898 */
899int mbedtls_ssl_tls13_write_identities_of_pre_shared_key_ext(
900 mbedtls_ssl_context *ssl, unsigned char *buf, unsigned char *end,
901 size_t *out_len, size_t *binders_len)
902{
903 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
904 int configured_psk_count = 0;
905 unsigned char *p = buf;
906 psa_algorithm_t hash_alg = PSA_ALG_NONE;
907 const unsigned char *identity;
908 size_t identity_len;
909 size_t l_binders_len = 0;
910 size_t output_len;
911
912 *out_len = 0;
913 *binders_len = 0;
914
915 /* Check if we have any PSKs to offer. If no, skip pre_shared_key */
916 configured_psk_count = ssl_tls13_get_configured_psk_count(ssl);
917 if (configured_psk_count == 0) {
918 MBEDTLS_SSL_DEBUG_MSG(3, ("skip pre_shared_key extensions"));
919 return 0;
920 }
921
922 MBEDTLS_SSL_DEBUG_MSG(4, ("Pre-configured PSK number = %d",
923 configured_psk_count));
924
925 /* Check if we have space to write the extension, binders included.
926 * - extension_type (2 bytes)
927 * - extension_data_len (2 bytes)
928 * - identities_len (2 bytes)
929 */
930 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 6);
931 p += 6;
932
933#if defined(MBEDTLS_SSL_SESSION_TICKETS)
934 if (ssl_tls13_ticket_get_identity(
935 ssl, &hash_alg, &identity, &identity_len) == 0) {
936#if defined(MBEDTLS_HAVE_TIME)
937 mbedtls_ms_time_t now = mbedtls_ms_time();
938 mbedtls_ssl_session *session = ssl->session_negotiate;
939 /* The ticket age has been checked to be smaller than the
940 * `ticket_lifetime` in ssl_prepare_client_hello() which is smaller than
941 * 7 days (enforced in ssl_tls13_parse_new_session_ticket()) . Thus the
942 * cast to `uint32_t` of the ticket age is safe. */
943 uint32_t obfuscated_ticket_age =
944 (uint32_t) (now - session->ticket_reception_time);
945 obfuscated_ticket_age += session->ticket_age_add;
946
947 ret = ssl_tls13_write_identity(ssl, p, end,
948 identity, identity_len,
949 obfuscated_ticket_age,
950 &output_len);
951#else
952 ret = ssl_tls13_write_identity(ssl, p, end, identity, identity_len,
953 0, &output_len);
954#endif /* MBEDTLS_HAVE_TIME */
955 if (ret != 0) {
956 return ret;
957 }
958
959 p += output_len;
960 l_binders_len += 1 + PSA_HASH_LENGTH(hash_alg);
961 }
962#endif /* MBEDTLS_SSL_SESSION_TICKETS */
963
964 if (ssl_tls13_psk_get_identity(
965 ssl, &hash_alg, &identity, &identity_len) == 0) {
966
967 ret = ssl_tls13_write_identity(ssl, p, end, identity, identity_len, 0,
968 &output_len);
969 if (ret != 0) {
970 return ret;
971 }
972
973 p += output_len;
974 l_binders_len += 1 + PSA_HASH_LENGTH(hash_alg);
975 }
976
977 MBEDTLS_SSL_DEBUG_MSG(3,
978 ("client hello, adding pre_shared_key extension, "
979 "omitting PSK binder list"));
980
981 /* Take into account the two bytes for the length of the binders. */
982 l_binders_len += 2;
983 /* Check if there is enough space for binders */
984 MBEDTLS_SSL_CHK_BUF_PTR(p, end, l_binders_len);
985
986 /*
987 * - extension_type (2 bytes)
988 * - extension_data_len (2 bytes)
989 * - identities_len (2 bytes)
990 */
991 MBEDTLS_PUT_UINT16_BE(MBEDTLS_TLS_EXT_PRE_SHARED_KEY, buf, 0);
992 MBEDTLS_PUT_UINT16_BE(p - buf - 4 + l_binders_len, buf, 2);
993 MBEDTLS_PUT_UINT16_BE(p - buf - 6, buf, 4);
994
995 *out_len = (p - buf) + l_binders_len;
996 *binders_len = l_binders_len;
997
998 MBEDTLS_SSL_DEBUG_BUF(3, "pre_shared_key identities", buf, p - buf);
999
1000 return 0;
1001}
1002
1003int mbedtls_ssl_tls13_write_binders_of_pre_shared_key_ext(
1004 mbedtls_ssl_context *ssl, unsigned char *buf, unsigned char *end)
1005{
1006 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1007 unsigned char *p = buf;
1008 psa_algorithm_t hash_alg = PSA_ALG_NONE;
1009 const unsigned char *psk;
1010 size_t psk_len;
1011 size_t output_len;
1012
1013 /* Check if we have space to write binders_len.
1014 * - binders_len (2 bytes)
1015 */
1016 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 2);
1017 p += 2;
1018
1019#if defined(MBEDTLS_SSL_SESSION_TICKETS)
1020 if (ssl_tls13_ticket_get_psk(ssl, &hash_alg, &psk, &psk_len) == 0) {
1021
1022 ret = ssl_tls13_write_binder(ssl, p, end,
1023 MBEDTLS_SSL_TLS1_3_PSK_RESUMPTION,
1024 hash_alg, psk, psk_len,
1025 &output_len);
1026 if (ret != 0) {
1027 return ret;
1028 }
1029 p += output_len;
1030 }
1031#endif /* MBEDTLS_SSL_SESSION_TICKETS */
1032
1033 if (ssl_tls13_psk_get_psk(ssl, &hash_alg, &psk, &psk_len) == 0) {
1034
1035 ret = ssl_tls13_write_binder(ssl, p, end,
1036 MBEDTLS_SSL_TLS1_3_PSK_EXTERNAL,
1037 hash_alg, psk, psk_len,
1038 &output_len);
1039 if (ret != 0) {
1040 return ret;
1041 }
1042 p += output_len;
1043 }
1044
1045 MBEDTLS_SSL_DEBUG_MSG(3, ("client hello, adding PSK binder list."));
1046
1047 /*
1048 * - binders_len (2 bytes)
1049 */
1050 MBEDTLS_PUT_UINT16_BE(p - buf - 2, buf, 0);
1051
1052 MBEDTLS_SSL_DEBUG_BUF(3, "pre_shared_key binders", buf, p - buf);
1053
1054 mbedtls_ssl_tls13_set_hs_sent_ext_mask(
1055 ssl, MBEDTLS_TLS_EXT_PRE_SHARED_KEY);
1056
1057 return 0;
1058}
1059
1060/*
1061 * struct {
1062 * opaque identity<1..2^16-1>;
1063 * uint32 obfuscated_ticket_age;
1064 * } PskIdentity;
1065 *
1066 * opaque PskBinderEntry<32..255>;
1067 *
1068 * struct {
1069 *
1070 * select (Handshake.msg_type) {
1071 * ...
1072 * case server_hello: uint16 selected_identity;
1073 * };
1074 *
1075 * } PreSharedKeyExtension;
1076 *
1077 */
1078MBEDTLS_CHECK_RETURN_CRITICAL
1079static int ssl_tls13_parse_server_pre_shared_key_ext(mbedtls_ssl_context *ssl,
1080 const unsigned char *buf,
1081 const unsigned char *end)
1082{
1083 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1084 int selected_identity;
1085 const unsigned char *psk;
1086 size_t psk_len;
1087 psa_algorithm_t hash_alg;
1088
1089 MBEDTLS_SSL_CHK_BUF_READ_PTR(buf, end, 2);
1090 selected_identity = MBEDTLS_GET_UINT16_BE(buf, 0);
1091 ssl->handshake->selected_identity = (uint16_t) selected_identity;
1092
1093 MBEDTLS_SSL_DEBUG_MSG(3, ("selected_identity = %d", selected_identity));
1094
1095 if (selected_identity >= ssl_tls13_get_configured_psk_count(ssl)) {
1096 MBEDTLS_SSL_DEBUG_MSG(1, ("Invalid PSK identity."));
1097
1098 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER,
1099 MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER);
1100 return MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER;
1101 }
1102
1103#if defined(MBEDTLS_SSL_SESSION_TICKETS)
1104 if (selected_identity == 0 && ssl_tls13_has_configured_ticket(ssl)) {
1105 ret = ssl_tls13_ticket_get_psk(ssl, &hash_alg, &psk, &psk_len);
1106 } else
1107#endif
1108 if (mbedtls_ssl_conf_has_static_psk(ssl->conf)) {
1109 ret = ssl_tls13_psk_get_psk(ssl, &hash_alg, &psk, &psk_len);
1110 } else {
1111 MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
1112 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
1113 }
1114 if (ret != 0) {
1115 return ret;
1116 }
1117
1118 if (mbedtls_md_psa_alg_from_type((mbedtls_md_type_t) ssl->handshake->ciphersuite_info->mac)
1119 != hash_alg) {
1120 MBEDTLS_SSL_DEBUG_MSG(
1121 1, ("Invalid ciphersuite for external psk."));
1122
1123 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER,
1124 MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER);
1125 return MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER;
1126 }
1127
1128 ret = mbedtls_ssl_set_hs_psk(ssl, psk, psk_len);
1129 if (ret != 0) {
1130 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_set_hs_psk", ret);
1131 return ret;
1132 }
1133
1134 return 0;
1135}
1136#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED */
1137
1138int mbedtls_ssl_tls13_write_client_hello_exts(mbedtls_ssl_context *ssl,
1139 unsigned char *buf,
1140 unsigned char *end,
1141 size_t *out_len)
1142{
1143 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1144 unsigned char *p = buf;
1145 size_t ext_len;
1146
1147 *out_len = 0;
1148
1149 ret = mbedtls_ssl_tls13_crypto_init(ssl);
1150 if (ret != 0) {
1151 return ret;
1152 }
1153
1154 /* Write supported_versions extension
1155 *
1156 * Supported Versions Extension is mandatory with TLS 1.3.
1157 */
1158 ret = ssl_tls13_write_supported_versions_ext(ssl, p, end, &ext_len);
1159 if (ret != 0) {
1160 return ret;
1161 }
1162 p += ext_len;
1163
1164 /* Echo the cookie if the server provided one in its preceding
1165 * HelloRetryRequest message.
1166 */
1167 ret = ssl_tls13_write_cookie_ext(ssl, p, end, &ext_len);
1168 if (ret != 0) {
1169 return ret;
1170 }
1171 p += ext_len;
1172
1173#if defined(MBEDTLS_SSL_RECORD_SIZE_LIMIT)
1174 ret = mbedtls_ssl_tls13_write_record_size_limit_ext(
1175 ssl, p, end, &ext_len);
1176 if (ret != 0) {
1177 return ret;
1178 }
1179 p += ext_len;
1180#endif
1181
1182#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_EPHEMERAL_ENABLED)
1183 if (mbedtls_ssl_conf_tls13_is_some_ephemeral_enabled(ssl)) {
1184 ret = ssl_tls13_write_key_share_ext(ssl, p, end, &ext_len);
1185 if (ret != 0) {
1186 return ret;
1187 }
1188 p += ext_len;
1189 }
1190#endif
1191
1192#if defined(MBEDTLS_SSL_EARLY_DATA)
1193 /* In the first ClientHello, write the early data indication extension if
1194 * necessary and update the early data state.
1195 * If an HRR has been received and thus we are currently writing the
1196 * second ClientHello, the second ClientHello must not contain an early
1197 * data extension and the early data state must stay as it is:
1198 * MBEDTLS_SSL_EARLY_DATA_STATE_NO_IND_SENT or
1199 * MBEDTLS_SSL_EARLY_DATA_STATE_REJECTED.
1200 */
1201 if (!ssl->handshake->hello_retry_request_flag) {
1202 if (mbedtls_ssl_conf_tls13_is_some_psk_enabled(ssl) &&
1203 ssl_tls13_early_data_has_valid_ticket(ssl) &&
1204 ssl->conf->early_data_enabled == MBEDTLS_SSL_EARLY_DATA_ENABLED) {
1205 ret = mbedtls_ssl_tls13_write_early_data_ext(
1206 ssl, 0, p, end, &ext_len);
1207 if (ret != 0) {
1208 return ret;
1209 }
1210 p += ext_len;
1211
1212 ssl->early_data_state = MBEDTLS_SSL_EARLY_DATA_STATE_IND_SENT;
1213 } else {
1214 ssl->early_data_state = MBEDTLS_SSL_EARLY_DATA_STATE_NO_IND_SENT;
1215 }
1216 }
1217#endif /* MBEDTLS_SSL_EARLY_DATA */
1218
1219#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED)
1220 /* For PSK-based key exchange we need the pre_shared_key extension
1221 * and the psk_key_exchange_modes extension.
1222 *
1223 * The pre_shared_key extension MUST be the last extension in the
1224 * ClientHello. Servers MUST check that it is the last extension and
1225 * otherwise fail the handshake with an "illegal_parameter" alert.
1226 *
1227 * Add the psk_key_exchange_modes extension.
1228 */
1229 ret = ssl_tls13_write_psk_key_exchange_modes_ext(ssl, p, end, &ext_len);
1230 if (ret != 0) {
1231 return ret;
1232 }
1233 p += ext_len;
1234#endif
1235
1236 *out_len = p - buf;
1237
1238 return 0;
1239}
1240
1241int mbedtls_ssl_tls13_finalize_client_hello(mbedtls_ssl_context *ssl)
1242{
1243 ((void) ssl);
1244
1245#if defined(MBEDTLS_SSL_EARLY_DATA)
1246 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1247 psa_algorithm_t hash_alg = PSA_ALG_NONE;
1248 const unsigned char *psk;
1249 size_t psk_len;
1250 const mbedtls_ssl_ciphersuite_t *ciphersuite_info;
1251
1252 if (ssl->early_data_state == MBEDTLS_SSL_EARLY_DATA_STATE_IND_SENT) {
1253 MBEDTLS_SSL_DEBUG_MSG(
1254 1, ("Set hs psk for early data when writing the first psk"));
1255
1256 ret = ssl_tls13_ticket_get_psk(ssl, &hash_alg, &psk, &psk_len);
1257 if (ret != 0) {
1258 MBEDTLS_SSL_DEBUG_RET(
1259 1, "ssl_tls13_ticket_get_psk", ret);
1260 return ret;
1261 }
1262
1263 ret = mbedtls_ssl_set_hs_psk(ssl, psk, psk_len);
1264 if (ret != 0) {
1265 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_set_hs_psk", ret);
1266 return ret;
1267 }
1268
1269 /*
1270 * Early data are going to be encrypted using the ciphersuite
1271 * associated with the pre-shared key used for the handshake.
1272 * Note that if the server rejects early data, the handshake
1273 * based on the pre-shared key may complete successfully
1274 * with a selected ciphersuite different from the ciphersuite
1275 * associated with the pre-shared key. Only the hashes of the
1276 * two ciphersuites have to be the same. In that case, the
1277 * encrypted handshake data and application data are
1278 * encrypted using a different ciphersuite than the one used for
1279 * the rejected early data.
1280 */
1281 ciphersuite_info = mbedtls_ssl_ciphersuite_from_id(
1282 ssl->session_negotiate->ciphersuite);
1283 ssl->handshake->ciphersuite_info = ciphersuite_info;
1284
1285 /* Enable psk and psk_ephemeral to make stage early happy */
1286 ssl->handshake->key_exchange_mode =
1287 MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK_ALL;
1288
1289 /* Start the TLS 1.3 key schedule:
1290 * Set the PSK and derive early secret.
1291 */
1292 ret = mbedtls_ssl_tls13_key_schedule_stage_early(ssl);
1293 if (ret != 0) {
1294 MBEDTLS_SSL_DEBUG_RET(
1295 1, "mbedtls_ssl_tls13_key_schedule_stage_early", ret);
1296 return ret;
1297 }
1298
1299 /* Derive early data key material */
1300 ret = mbedtls_ssl_tls13_compute_early_transform(ssl);
1301 if (ret != 0) {
1302 MBEDTLS_SSL_DEBUG_RET(
1303 1, "mbedtls_ssl_tls13_compute_early_transform", ret);
1304 return ret;
1305 }
1306
1307#if defined(MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE)
1308 mbedtls_ssl_handshake_set_state(
1309 ssl, MBEDTLS_SSL_CLIENT_CCS_AFTER_CLIENT_HELLO);
1310#else
1311 MBEDTLS_SSL_DEBUG_MSG(
1312 1, ("Switch to early data keys for outbound traffic"));
1313 mbedtls_ssl_set_outbound_transform(
1314 ssl, ssl->handshake->transform_earlydata);
1315 ssl->early_data_state = MBEDTLS_SSL_EARLY_DATA_STATE_CAN_WRITE;
1316#endif
1317 }
1318#endif /* MBEDTLS_SSL_EARLY_DATA */
1319 return 0;
1320}
1321/*
1322 * Functions for parsing and processing Server Hello
1323 */
1324
1325/**
1326 * \brief Detect if the ServerHello contains a supported_versions extension
1327 * or not.
1328 *
1329 * \param[in] ssl SSL context
1330 * \param[in] buf Buffer containing the ServerHello message
1331 * \param[in] end End of the buffer containing the ServerHello message
1332 *
1333 * \return 0 if the ServerHello does not contain a supported_versions extension
1334 * \return 1 if the ServerHello contains a supported_versions extension
1335 * \return A negative value if an error occurred while parsing the ServerHello.
1336 */
1337MBEDTLS_CHECK_RETURN_CRITICAL
1338static int ssl_tls13_is_supported_versions_ext_present(
1339 mbedtls_ssl_context *ssl,
1340 const unsigned char *buf,
1341 const unsigned char *end)
1342{
1343 const unsigned char *p = buf;
1344 size_t legacy_session_id_echo_len;
1345 const unsigned char *supported_versions_data;
1346 const unsigned char *supported_versions_data_end;
1347
1348 /*
1349 * Check there is enough data to access the legacy_session_id_echo vector
1350 * length:
1351 * - legacy_version 2 bytes
1352 * - random MBEDTLS_SERVER_HELLO_RANDOM_LEN bytes
1353 * - legacy_session_id_echo length 1 byte
1354 */
1355 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, MBEDTLS_SERVER_HELLO_RANDOM_LEN + 3);
1356 p += MBEDTLS_SERVER_HELLO_RANDOM_LEN + 2;
1357 legacy_session_id_echo_len = *p;
1358
1359 /*
1360 * Jump to the extensions, jumping over:
1361 * - legacy_session_id_echo (legacy_session_id_echo_len + 1) bytes
1362 * - cipher_suite 2 bytes
1363 * - legacy_compression_method 1 byte
1364 */
1365 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, legacy_session_id_echo_len + 4);
1366 p += legacy_session_id_echo_len + 4;
1367
1368 return mbedtls_ssl_tls13_is_supported_versions_ext_present_in_exts(
1369 ssl, p, end,
1370 &supported_versions_data, &supported_versions_data_end);
1371}
1372
1373/* Returns a negative value on failure, and otherwise
1374 * - 1 if the last eight bytes of the ServerHello random bytes indicate that
1375 * the server is TLS 1.3 capable but negotiating TLS 1.2 or below.
1376 * - 0 otherwise
1377 */
1378MBEDTLS_CHECK_RETURN_CRITICAL
1379static int ssl_tls13_is_downgrade_negotiation(mbedtls_ssl_context *ssl,
1380 const unsigned char *buf,
1381 const unsigned char *end)
1382{
1383 /* First seven bytes of the magic downgrade strings, see RFC 8446 4.1.3 */
1384 static const unsigned char magic_downgrade_string[] =
1385 { 0x44, 0x4F, 0x57, 0x4E, 0x47, 0x52, 0x44 };
1386 const unsigned char *last_eight_bytes_of_random;
1387 unsigned char last_byte_of_random;
1388
1389 MBEDTLS_SSL_CHK_BUF_READ_PTR(buf, end, MBEDTLS_SERVER_HELLO_RANDOM_LEN + 2);
1390 last_eight_bytes_of_random = buf + 2 + MBEDTLS_SERVER_HELLO_RANDOM_LEN - 8;
1391
1392 if (memcmp(last_eight_bytes_of_random,
1393 magic_downgrade_string,
1394 sizeof(magic_downgrade_string)) == 0) {
1395 last_byte_of_random = last_eight_bytes_of_random[7];
1396 return last_byte_of_random == 0 ||
1397 last_byte_of_random == 1;
1398 }
1399
1400 return 0;
1401}
1402
1403/* Returns a negative value on failure, and otherwise
1404 * - SSL_SERVER_HELLO or
1405 * - SSL_SERVER_HELLO_HRR
1406 * to indicate which message is expected and to be parsed next.
1407 */
1408#define SSL_SERVER_HELLO 0
1409#define SSL_SERVER_HELLO_HRR 1
1410MBEDTLS_CHECK_RETURN_CRITICAL
1411static int ssl_server_hello_is_hrr(mbedtls_ssl_context *ssl,
1412 const unsigned char *buf,
1413 const unsigned char *end)
1414{
1415
1416 /* Check whether this message is a HelloRetryRequest ( HRR ) message.
1417 *
1418 * Server Hello and HRR are only distinguished by Random set to the
1419 * special value of the SHA-256 of "HelloRetryRequest".
1420 *
1421 * struct {
1422 * ProtocolVersion legacy_version = 0x0303;
1423 * Random random;
1424 * opaque legacy_session_id_echo<0..32>;
1425 * CipherSuite cipher_suite;
1426 * uint8 legacy_compression_method = 0;
1427 * Extension extensions<6..2^16-1>;
1428 * } ServerHello;
1429 *
1430 */
1431 MBEDTLS_SSL_CHK_BUF_READ_PTR(
1432 buf, end, 2 + sizeof(mbedtls_ssl_tls13_hello_retry_request_magic));
1433
1434 if (memcmp(buf + 2, mbedtls_ssl_tls13_hello_retry_request_magic,
1435 sizeof(mbedtls_ssl_tls13_hello_retry_request_magic)) == 0) {
1436 return SSL_SERVER_HELLO_HRR;
1437 }
1438
1439 return SSL_SERVER_HELLO;
1440}
1441
1442/*
1443 * Returns a negative value on failure, and otherwise
1444 * - SSL_SERVER_HELLO or
1445 * - SSL_SERVER_HELLO_HRR or
1446 * - SSL_SERVER_HELLO_TLS1_2
1447 */
1448#define SSL_SERVER_HELLO_TLS1_2 2
1449MBEDTLS_CHECK_RETURN_CRITICAL
1450static int ssl_tls13_preprocess_server_hello(mbedtls_ssl_context *ssl,
1451 const unsigned char *buf,
1452 const unsigned char *end)
1453{
1454 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1455 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
1456
1457 MBEDTLS_SSL_PROC_CHK_NEG(ssl_tls13_is_supported_versions_ext_present(
1458 ssl, buf, end));
1459
1460 if (ret == 0) {
1461 MBEDTLS_SSL_PROC_CHK_NEG(
1462 ssl_tls13_is_downgrade_negotiation(ssl, buf, end));
1463
1464 /* If the server is negotiating TLS 1.2 or below and:
1465 * . we did not propose TLS 1.2 or
1466 * . the server responded it is TLS 1.3 capable but negotiating a lower
1467 * version of the protocol and thus we are under downgrade attack
1468 * abort the handshake with an "illegal parameter" alert.
1469 */
1470 if (handshake->min_tls_version > MBEDTLS_SSL_VERSION_TLS1_2 || ret) {
1471 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER,
1472 MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER);
1473 return MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER;
1474 }
1475
1476 /*
1477 * Version 1.2 of the protocol has been negotiated, set the
1478 * ssl->keep_current_message flag for the ServerHello to be kept and
1479 * parsed as a TLS 1.2 ServerHello. We also change ssl->tls_version to
1480 * MBEDTLS_SSL_VERSION_TLS1_2 thus from now on mbedtls_ssl_handshake_step()
1481 * will dispatch to the TLS 1.2 state machine.
1482 */
1483 ssl->keep_current_message = 1;
1484 ssl->tls_version = MBEDTLS_SSL_VERSION_TLS1_2;
1485 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_add_hs_msg_to_checksum(
1486 ssl, MBEDTLS_SSL_HS_SERVER_HELLO,
1487 buf, (size_t) (end - buf)));
1488
1489 if (mbedtls_ssl_conf_tls13_is_some_ephemeral_enabled(ssl)) {
1490 ret = ssl_tls13_reset_key_share(ssl);
1491 if (ret != 0) {
1492 return ret;
1493 }
1494 }
1495
1496 return SSL_SERVER_HELLO_TLS1_2;
1497 }
1498
1499 ssl->session_negotiate->tls_version = ssl->tls_version;
1500 ssl->session_negotiate->endpoint = ssl->conf->endpoint;
1501
1502 handshake->received_extensions = MBEDTLS_SSL_EXT_MASK_NONE;
1503
1504 ret = ssl_server_hello_is_hrr(ssl, buf, end);
1505 switch (ret) {
1506 case SSL_SERVER_HELLO:
1507 MBEDTLS_SSL_DEBUG_MSG(2, ("received ServerHello message"));
1508 break;
1509 case SSL_SERVER_HELLO_HRR:
1510 MBEDTLS_SSL_DEBUG_MSG(2, ("received HelloRetryRequest message"));
1511 /* If a client receives a second HelloRetryRequest in the same
1512 * connection (i.e., where the ClientHello was itself in response
1513 * to a HelloRetryRequest), it MUST abort the handshake with an
1514 * "unexpected_message" alert.
1515 */
1516 if (handshake->hello_retry_request_flag) {
1517 MBEDTLS_SSL_DEBUG_MSG(1, ("Multiple HRRs received"));
1518 MBEDTLS_SSL_PEND_FATAL_ALERT(
1519 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE,
1520 MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE);
1521 return MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE;
1522 }
1523 /*
1524 * Clients must abort the handshake with an "illegal_parameter"
1525 * alert if the HelloRetryRequest would not result in any change
1526 * in the ClientHello.
1527 * In a PSK only key exchange that what we expect.
1528 */
1529 if (!mbedtls_ssl_conf_tls13_is_some_ephemeral_enabled(ssl)) {
1530 MBEDTLS_SSL_DEBUG_MSG(1,
1531 ("Unexpected HRR in pure PSK key exchange."));
1532 MBEDTLS_SSL_PEND_FATAL_ALERT(
1533 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER,
1534 MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER);
1535 return MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER;
1536 }
1537
1538 handshake->hello_retry_request_flag = 1;
1539
1540 break;
1541 }
1542
1543cleanup:
1544
1545 return ret;
1546}
1547
1548MBEDTLS_CHECK_RETURN_CRITICAL
1549static int ssl_tls13_check_server_hello_session_id_echo(mbedtls_ssl_context *ssl,
1550 const unsigned char **buf,
1551 const unsigned char *end)
1552{
1553 const unsigned char *p = *buf;
1554 size_t legacy_session_id_echo_len;
1555
1556 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, 1);
1557 legacy_session_id_echo_len = *p++;
1558
1559 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, legacy_session_id_echo_len);
1560
1561 /* legacy_session_id_echo */
1562 if (ssl->session_negotiate->id_len != legacy_session_id_echo_len ||
1563 memcmp(ssl->session_negotiate->id, p, legacy_session_id_echo_len) != 0) {
1564 MBEDTLS_SSL_DEBUG_BUF(3, "Expected Session ID",
1565 ssl->session_negotiate->id,
1566 ssl->session_negotiate->id_len);
1567 MBEDTLS_SSL_DEBUG_BUF(3, "Received Session ID", p,
1568 legacy_session_id_echo_len);
1569
1570 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER,
1571 MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER);
1572
1573 return MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER;
1574 }
1575
1576 p += legacy_session_id_echo_len;
1577 *buf = p;
1578
1579 MBEDTLS_SSL_DEBUG_BUF(3, "Session ID", ssl->session_negotiate->id,
1580 ssl->session_negotiate->id_len);
1581 return 0;
1582}
1583
1584/* Parse ServerHello message and configure context
1585 *
1586 * struct {
1587 * ProtocolVersion legacy_version = 0x0303; // TLS 1.2
1588 * Random random;
1589 * opaque legacy_session_id_echo<0..32>;
1590 * CipherSuite cipher_suite;
1591 * uint8 legacy_compression_method = 0;
1592 * Extension extensions<6..2^16-1>;
1593 * } ServerHello;
1594 */
1595MBEDTLS_CHECK_RETURN_CRITICAL
1596static int ssl_tls13_parse_server_hello(mbedtls_ssl_context *ssl,
1597 const unsigned char *buf,
1598 const unsigned char *end,
1599 int is_hrr)
1600{
1601 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1602 const unsigned char *p = buf;
1603 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
1604 size_t extensions_len;
1605 const unsigned char *extensions_end;
1606 uint16_t cipher_suite;
1607 const mbedtls_ssl_ciphersuite_t *ciphersuite_info;
1608 int fatal_alert = 0;
1609 uint32_t allowed_extensions_mask;
1610 int hs_msg_type = is_hrr ? MBEDTLS_SSL_TLS1_3_HS_HELLO_RETRY_REQUEST :
1611 MBEDTLS_SSL_HS_SERVER_HELLO;
1612
1613 /*
1614 * Check there is space for minimal fields
1615 *
1616 * - legacy_version ( 2 bytes)
1617 * - random (MBEDTLS_SERVER_HELLO_RANDOM_LEN bytes)
1618 * - legacy_session_id_echo ( 1 byte ), minimum size
1619 * - cipher_suite ( 2 bytes)
1620 * - legacy_compression_method ( 1 byte )
1621 */
1622 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, MBEDTLS_SERVER_HELLO_RANDOM_LEN + 6);
1623
1624 MBEDTLS_SSL_DEBUG_BUF(4, "server hello", p, end - p);
1625 MBEDTLS_SSL_DEBUG_BUF(3, "server hello, version", p, 2);
1626
1627 /* ...
1628 * ProtocolVersion legacy_version = 0x0303; // TLS 1.2
1629 * ...
1630 * with ProtocolVersion defined as:
1631 * uint16 ProtocolVersion;
1632 */
1633 if (mbedtls_ssl_read_version(p, ssl->conf->transport) !=
1634 MBEDTLS_SSL_VERSION_TLS1_2) {
1635 MBEDTLS_SSL_DEBUG_MSG(1, ("Unsupported version of TLS."));
1636 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_PROTOCOL_VERSION,
1637 MBEDTLS_ERR_SSL_BAD_PROTOCOL_VERSION);
1638 ret = MBEDTLS_ERR_SSL_BAD_PROTOCOL_VERSION;
1639 goto cleanup;
1640 }
1641 p += 2;
1642
1643 /* ...
1644 * Random random;
1645 * ...
1646 * with Random defined as:
1647 * opaque Random[MBEDTLS_SERVER_HELLO_RANDOM_LEN];
1648 */
1649 if (!is_hrr) {
1650 memcpy(&handshake->randbytes[MBEDTLS_CLIENT_HELLO_RANDOM_LEN], p,
1651 MBEDTLS_SERVER_HELLO_RANDOM_LEN);
1652 MBEDTLS_SSL_DEBUG_BUF(3, "server hello, random bytes",
1653 p, MBEDTLS_SERVER_HELLO_RANDOM_LEN);
1654 }
1655 p += MBEDTLS_SERVER_HELLO_RANDOM_LEN;
1656
1657 /* ...
1658 * opaque legacy_session_id_echo<0..32>;
1659 * ...
1660 */
1661 if (ssl_tls13_check_server_hello_session_id_echo(ssl, &p, end) != 0) {
1662 fatal_alert = MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER;
1663 goto cleanup;
1664 }
1665
1666 /* ...
1667 * CipherSuite cipher_suite;
1668 * ...
1669 * with CipherSuite defined as:
1670 * uint8 CipherSuite[2];
1671 */
1672 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, 2);
1673 cipher_suite = MBEDTLS_GET_UINT16_BE(p, 0);
1674 p += 2;
1675
1676
1677 ciphersuite_info = mbedtls_ssl_ciphersuite_from_id(cipher_suite);
1678 /*
1679 * Check whether this ciphersuite is valid and offered.
1680 */
1681 if ((mbedtls_ssl_validate_ciphersuite(ssl, ciphersuite_info,
1682 ssl->tls_version,
1683 ssl->tls_version) != 0) ||
1684 !mbedtls_ssl_tls13_cipher_suite_is_offered(ssl, cipher_suite)) {
1685 fatal_alert = MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER;
1686 }
1687 /*
1688 * If we received an HRR before and that the proposed selected
1689 * ciphersuite in this server hello is not the same as the one
1690 * proposed in the HRR, we abort the handshake and send an
1691 * "illegal_parameter" alert.
1692 */
1693 else if ((!is_hrr) && handshake->hello_retry_request_flag &&
1694 (cipher_suite != ssl->session_negotiate->ciphersuite)) {
1695 fatal_alert = MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER;
1696 }
1697
1698 if (fatal_alert == MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER) {
1699 MBEDTLS_SSL_DEBUG_MSG(1, ("invalid ciphersuite(%04x) parameter",
1700 cipher_suite));
1701 goto cleanup;
1702 }
1703
1704 /* Configure ciphersuites */
1705 mbedtls_ssl_optimize_checksum(ssl, ciphersuite_info);
1706
1707 handshake->ciphersuite_info = ciphersuite_info;
1708 MBEDTLS_SSL_DEBUG_MSG(3, ("server hello, chosen ciphersuite: ( %04x ) - %s",
1709 cipher_suite, ciphersuite_info->name));
1710
1711#if defined(MBEDTLS_HAVE_TIME)
1712 ssl->session_negotiate->start = mbedtls_time(NULL);
1713#endif /* MBEDTLS_HAVE_TIME */
1714
1715 /* ...
1716 * uint8 legacy_compression_method = 0;
1717 * ...
1718 */
1719 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, 1);
1720 if (p[0] != MBEDTLS_SSL_COMPRESS_NULL) {
1721 MBEDTLS_SSL_DEBUG_MSG(1, ("bad legacy compression method"));
1722 fatal_alert = MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER;
1723 goto cleanup;
1724 }
1725 p++;
1726
1727 /* ...
1728 * Extension extensions<6..2^16-1>;
1729 * ...
1730 * struct {
1731 * ExtensionType extension_type; (2 bytes)
1732 * opaque extension_data<0..2^16-1>;
1733 * } Extension;
1734 */
1735 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, 2);
1736 extensions_len = MBEDTLS_GET_UINT16_BE(p, 0);
1737 p += 2;
1738
1739 /* Check extensions do not go beyond the buffer of data. */
1740 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, extensions_len);
1741 extensions_end = p + extensions_len;
1742
1743 MBEDTLS_SSL_DEBUG_BUF(3, "server hello extensions", p, extensions_len);
1744
1745 handshake->received_extensions = MBEDTLS_SSL_EXT_MASK_NONE;
1746 allowed_extensions_mask = is_hrr ?
1747 MBEDTLS_SSL_TLS1_3_ALLOWED_EXTS_OF_HRR :
1748 MBEDTLS_SSL_TLS1_3_ALLOWED_EXTS_OF_SH;
1749
1750 while (p < extensions_end) {
1751 unsigned int extension_type;
1752 size_t extension_data_len;
1753 const unsigned char *extension_data_end;
1754
1755 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, extensions_end, 4);
1756 extension_type = MBEDTLS_GET_UINT16_BE(p, 0);
1757 extension_data_len = MBEDTLS_GET_UINT16_BE(p, 2);
1758 p += 4;
1759
1760 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, extensions_end, extension_data_len);
1761 extension_data_end = p + extension_data_len;
1762
1763 ret = mbedtls_ssl_tls13_check_received_extension(
1764 ssl, hs_msg_type, extension_type, allowed_extensions_mask);
1765 if (ret != 0) {
1766 return ret;
1767 }
1768
1769 switch (extension_type) {
1770 case MBEDTLS_TLS_EXT_COOKIE:
1771
1772 ret = ssl_tls13_parse_cookie_ext(ssl,
1773 p, extension_data_end);
1774 if (ret != 0) {
1775 MBEDTLS_SSL_DEBUG_RET(1,
1776 "ssl_tls13_parse_cookie_ext",
1777 ret);
1778 goto cleanup;
1779 }
1780 break;
1781
1782 case MBEDTLS_TLS_EXT_SUPPORTED_VERSIONS:
1783 ret = ssl_tls13_parse_supported_versions_ext(ssl,
1784 p,
1785 extension_data_end);
1786 if (ret != 0) {
1787 goto cleanup;
1788 }
1789 break;
1790
1791#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED)
1792 case MBEDTLS_TLS_EXT_PRE_SHARED_KEY:
1793 MBEDTLS_SSL_DEBUG_MSG(3, ("found pre_shared_key extension"));
1794
1795 if ((ret = ssl_tls13_parse_server_pre_shared_key_ext(
1796 ssl, p, extension_data_end)) != 0) {
1797 MBEDTLS_SSL_DEBUG_RET(
1798 1, ("ssl_tls13_parse_server_pre_shared_key_ext"), ret);
1799 return ret;
1800 }
1801 break;
1802#endif
1803
1804 case MBEDTLS_TLS_EXT_KEY_SHARE:
1805 MBEDTLS_SSL_DEBUG_MSG(3, ("found key_shares extension"));
1806 if (!mbedtls_ssl_conf_tls13_is_some_ephemeral_enabled(ssl)) {
1807 fatal_alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_EXT;
1808 goto cleanup;
1809 }
1810
1811 if (is_hrr) {
1812 ret = ssl_tls13_parse_hrr_key_share_ext(ssl,
1813 p, extension_data_end);
1814 } else {
1815 ret = ssl_tls13_parse_key_share_ext(ssl,
1816 p, extension_data_end);
1817 }
1818 if (ret != 0) {
1819 MBEDTLS_SSL_DEBUG_RET(1,
1820 "ssl_tls13_parse_key_share_ext",
1821 ret);
1822 goto cleanup;
1823 }
1824 break;
1825
1826 default:
1827 ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
1828 goto cleanup;
1829 }
1830
1831 p += extension_data_len;
1832 }
1833
1834 MBEDTLS_SSL_PRINT_EXTS(3, hs_msg_type, handshake->received_extensions);
1835
1836cleanup:
1837
1838 if (fatal_alert == MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_EXT) {
1839 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_EXT,
1840 MBEDTLS_ERR_SSL_UNSUPPORTED_EXTENSION);
1841 ret = MBEDTLS_ERR_SSL_UNSUPPORTED_EXTENSION;
1842 } else if (fatal_alert == MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER) {
1843 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER,
1844 MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER);
1845 ret = MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER;
1846 }
1847 return ret;
1848}
1849
1850#if defined(MBEDTLS_DEBUG_C)
1851static const char *ssl_tls13_get_kex_mode_str(int mode)
1852{
1853 switch (mode) {
1854 case MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK:
1855 return "psk";
1856 case MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL:
1857 return "ephemeral";
1858 case MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK_EPHEMERAL:
1859 return "psk_ephemeral";
1860 default:
1861 return "unknown mode";
1862 }
1863}
1864#endif /* MBEDTLS_DEBUG_C */
1865
1866MBEDTLS_CHECK_RETURN_CRITICAL
1867static int ssl_tls13_postprocess_server_hello(mbedtls_ssl_context *ssl)
1868{
1869 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1870 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
1871
1872 /* Determine the key exchange mode:
1873 * 1) If both the pre_shared_key and key_share extensions were received
1874 * then the key exchange mode is PSK with EPHEMERAL.
1875 * 2) If only the pre_shared_key extension was received then the key
1876 * exchange mode is PSK-only.
1877 * 3) If only the key_share extension was received then the key
1878 * exchange mode is EPHEMERAL-only.
1879 */
1880 switch (handshake->received_extensions &
1881 (MBEDTLS_SSL_EXT_MASK(PRE_SHARED_KEY) |
1882 MBEDTLS_SSL_EXT_MASK(KEY_SHARE))) {
1883 /* Only the pre_shared_key extension was received */
1884 case MBEDTLS_SSL_EXT_MASK(PRE_SHARED_KEY):
1885 handshake->key_exchange_mode =
1886 MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK;
1887 break;
1888
1889 /* Only the key_share extension was received */
1890 case MBEDTLS_SSL_EXT_MASK(KEY_SHARE):
1891 handshake->key_exchange_mode =
1892 MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL;
1893 break;
1894
1895 /* Both the pre_shared_key and key_share extensions were received */
1896 case (MBEDTLS_SSL_EXT_MASK(PRE_SHARED_KEY) |
1897 MBEDTLS_SSL_EXT_MASK(KEY_SHARE)):
1898 handshake->key_exchange_mode =
1899 MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK_EPHEMERAL;
1900 break;
1901
1902 /* Neither pre_shared_key nor key_share extension was received */
1903 default:
1904 MBEDTLS_SSL_DEBUG_MSG(1, ("Unknown key exchange."));
1905 ret = MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
1906 goto cleanup;
1907 }
1908
1909 if (!mbedtls_ssl_conf_tls13_is_kex_mode_enabled(
1910 ssl, handshake->key_exchange_mode)) {
1911 ret = MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
1912 MBEDTLS_SSL_DEBUG_MSG(
1913 2, ("Key exchange mode(%s) is not supported.",
1914 ssl_tls13_get_kex_mode_str(handshake->key_exchange_mode)));
1915 goto cleanup;
1916 }
1917
1918 MBEDTLS_SSL_DEBUG_MSG(
1919 3, ("Selected key exchange mode: %s",
1920 ssl_tls13_get_kex_mode_str(handshake->key_exchange_mode)));
1921
1922 /* Start the TLS 1.3 key scheduling if not already done.
1923 *
1924 * If we proposed early data then we have already derived an
1925 * early secret using the selected PSK and its associated hash.
1926 * It means that if the negotiated key exchange mode is psk or
1927 * psk_ephemeral, we have already correctly computed the
1928 * early secret and thus we do not do it again. In all other
1929 * cases we compute it here.
1930 */
1931#if defined(MBEDTLS_SSL_EARLY_DATA)
1932 if (ssl->early_data_state == MBEDTLS_SSL_EARLY_DATA_STATE_NO_IND_SENT ||
1933 handshake->key_exchange_mode ==
1934 MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL)
1935#endif
1936 {
1937 ret = mbedtls_ssl_tls13_key_schedule_stage_early(ssl);
1938 if (ret != 0) {
1939 MBEDTLS_SSL_DEBUG_RET(
1940 1, "mbedtls_ssl_tls13_key_schedule_stage_early", ret);
1941 goto cleanup;
1942 }
1943 }
1944
1945 ret = mbedtls_ssl_tls13_compute_handshake_transform(ssl);
1946 if (ret != 0) {
1947 MBEDTLS_SSL_DEBUG_RET(1,
1948 "mbedtls_ssl_tls13_compute_handshake_transform",
1949 ret);
1950 goto cleanup;
1951 }
1952
1953 mbedtls_ssl_set_inbound_transform(ssl, handshake->transform_handshake);
1954 MBEDTLS_SSL_DEBUG_MSG(1, ("Switch to handshake keys for inbound traffic"));
1955 ssl->session_in = ssl->session_negotiate;
1956
1957cleanup:
1958 if (ret != 0) {
1959 MBEDTLS_SSL_PEND_FATAL_ALERT(
1960 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE,
1961 MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE);
1962 }
1963
1964 return ret;
1965}
1966
1967MBEDTLS_CHECK_RETURN_CRITICAL
1968static int ssl_tls13_postprocess_hrr(mbedtls_ssl_context *ssl)
1969{
1970 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1971
1972 mbedtls_ssl_session_reset_msg_layer(ssl, 0);
1973
1974 /*
1975 * We are going to re-generate a shared secret corresponding to the group
1976 * selected by the server, which is different from the group for which we
1977 * generated a shared secret in the first client hello.
1978 * Thus, reset the shared secret.
1979 */
1980 ret = ssl_tls13_reset_key_share(ssl);
1981 if (ret != 0) {
1982 return ret;
1983 }
1984
1985 ssl->session_negotiate->ciphersuite = ssl->handshake->ciphersuite_info->id;
1986
1987#if defined(MBEDTLS_SSL_EARLY_DATA)
1988 if (ssl->early_data_state != MBEDTLS_SSL_EARLY_DATA_STATE_NO_IND_SENT) {
1989 ssl->early_data_state = MBEDTLS_SSL_EARLY_DATA_STATE_REJECTED;
1990 }
1991#endif
1992
1993 return 0;
1994}
1995
1996/*
1997 * Wait and parse ServerHello handshake message.
1998 * Handler for MBEDTLS_SSL_SERVER_HELLO
1999 */
2000MBEDTLS_CHECK_RETURN_CRITICAL
2001static int ssl_tls13_process_server_hello(mbedtls_ssl_context *ssl)
2002{
2003 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
2004 unsigned char *buf = NULL;
2005 size_t buf_len = 0;
2006 int is_hrr = 0;
2007
2008 MBEDTLS_SSL_DEBUG_MSG(2, ("=> %s", __func__));
2009
2010 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_tls13_fetch_handshake_msg(
2011 ssl, MBEDTLS_SSL_HS_SERVER_HELLO, &buf, &buf_len));
2012
2013 ret = ssl_tls13_preprocess_server_hello(ssl, buf, buf + buf_len);
2014 if (ret < 0) {
2015 goto cleanup;
2016 } else {
2017 is_hrr = (ret == SSL_SERVER_HELLO_HRR);
2018 }
2019
2020 if (ret == SSL_SERVER_HELLO_TLS1_2) {
2021 ret = 0;
2022 goto cleanup;
2023 }
2024
2025 /*
2026 * TLS 1.3 is negotiated. Check that ServerHello/HRR ends at a record
2027 * boundary. For HRR, which does not trigger a key update, this checks
2028 * that HRR terminates the server flight.
2029 */
2030 if (ssl->in_hslen != ssl->in_msglen) {
2031 MBEDTLS_SSL_DEBUG_MSG(1, ("ServerHello end not aligned with a record boundary"));
2032 ret = MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE;
2033 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE,
2034 MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE);
2035 goto cleanup;
2036 }
2037
2038 MBEDTLS_SSL_PROC_CHK(ssl_tls13_parse_server_hello(ssl, buf,
2039 buf + buf_len,
2040 is_hrr));
2041 if (is_hrr) {
2042 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_reset_transcript_for_hrr(ssl));
2043 }
2044
2045 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_add_hs_msg_to_checksum(
2046 ssl, MBEDTLS_SSL_HS_SERVER_HELLO, buf, buf_len));
2047
2048 if (is_hrr) {
2049 MBEDTLS_SSL_PROC_CHK(ssl_tls13_postprocess_hrr(ssl));
2050#if defined(MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE)
2051 /* If not offering early data, the client sends a dummy CCS record
2052 * immediately before its second flight. This may either be before
2053 * its second ClientHello or before its encrypted handshake flight.
2054 */
2055 mbedtls_ssl_handshake_set_state(
2056 ssl, MBEDTLS_SSL_CLIENT_CCS_BEFORE_2ND_CLIENT_HELLO);
2057#else
2058 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_CLIENT_HELLO);
2059#endif /* MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE */
2060 } else {
2061 MBEDTLS_SSL_PROC_CHK(ssl_tls13_postprocess_server_hello(ssl));
2062 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_ENCRYPTED_EXTENSIONS);
2063 }
2064
2065cleanup:
2066 MBEDTLS_SSL_DEBUG_MSG(2, ("<= %s ( %s )", __func__,
2067 is_hrr ? "HelloRetryRequest" : "ServerHello"));
2068 return ret;
2069}
2070
2071/*
2072 *
2073 * Handler for MBEDTLS_SSL_ENCRYPTED_EXTENSIONS
2074 *
2075 * The EncryptedExtensions message contains any extensions which
2076 * should be protected, i.e., any which are not needed to establish
2077 * the cryptographic context.
2078 */
2079
2080/* Parse EncryptedExtensions message
2081 * struct {
2082 * Extension extensions<0..2^16-1>;
2083 * } EncryptedExtensions;
2084 */
2085MBEDTLS_CHECK_RETURN_CRITICAL
2086static int ssl_tls13_parse_encrypted_extensions(mbedtls_ssl_context *ssl,
2087 const unsigned char *buf,
2088 const unsigned char *end)
2089{
2090 int ret = 0;
2091 size_t extensions_len;
2092 const unsigned char *p = buf;
2093 const unsigned char *extensions_end;
2094 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
2095
2096 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, 2);
2097 extensions_len = MBEDTLS_GET_UINT16_BE(p, 0);
2098 p += 2;
2099
2100 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, extensions_len);
2101 extensions_end = p + extensions_len;
2102
2103 MBEDTLS_SSL_DEBUG_BUF(3, "encrypted extensions", p, extensions_len);
2104
2105 handshake->received_extensions = MBEDTLS_SSL_EXT_MASK_NONE;
2106
2107 while (p < extensions_end) {
2108 unsigned int extension_type;
2109 size_t extension_data_len;
2110
2111 /*
2112 * struct {
2113 * ExtensionType extension_type; (2 bytes)
2114 * opaque extension_data<0..2^16-1>;
2115 * } Extension;
2116 */
2117 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, extensions_end, 4);
2118 extension_type = MBEDTLS_GET_UINT16_BE(p, 0);
2119 extension_data_len = MBEDTLS_GET_UINT16_BE(p, 2);
2120 p += 4;
2121
2122 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, extensions_end, extension_data_len);
2123
2124 ret = mbedtls_ssl_tls13_check_received_extension(
2125 ssl, MBEDTLS_SSL_HS_ENCRYPTED_EXTENSIONS, extension_type,
2126 MBEDTLS_SSL_TLS1_3_ALLOWED_EXTS_OF_EE);
2127 if (ret != 0) {
2128 return ret;
2129 }
2130
2131 switch (extension_type) {
2132#if defined(MBEDTLS_SSL_ALPN)
2133 case MBEDTLS_TLS_EXT_ALPN:
2134 MBEDTLS_SSL_DEBUG_MSG(3, ("found alpn extension"));
2135
2136 if ((ret = ssl_tls13_parse_alpn_ext(
2137 ssl, p, (size_t) extension_data_len)) != 0) {
2138 return ret;
2139 }
2140
2141 break;
2142#endif /* MBEDTLS_SSL_ALPN */
2143
2144#if defined(MBEDTLS_SSL_EARLY_DATA)
2145 case MBEDTLS_TLS_EXT_EARLY_DATA:
2146
2147 if (extension_data_len != 0) {
2148 /* The message must be empty. */
2149 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR,
2150 MBEDTLS_ERR_SSL_DECODE_ERROR);
2151 return MBEDTLS_ERR_SSL_DECODE_ERROR;
2152 }
2153
2154 break;
2155#endif /* MBEDTLS_SSL_EARLY_DATA */
2156
2157#if defined(MBEDTLS_SSL_RECORD_SIZE_LIMIT)
2158 case MBEDTLS_TLS_EXT_RECORD_SIZE_LIMIT:
2159 MBEDTLS_SSL_DEBUG_MSG(3, ("found record_size_limit extension"));
2160
2161 ret = mbedtls_ssl_tls13_parse_record_size_limit_ext(
2162 ssl, p, p + extension_data_len);
2163 if (ret != 0) {
2164 MBEDTLS_SSL_DEBUG_RET(
2165 1, ("mbedtls_ssl_tls13_parse_record_size_limit_ext"), ret);
2166 return ret;
2167 }
2168 break;
2169#endif /* MBEDTLS_SSL_RECORD_SIZE_LIMIT */
2170
2171 default:
2172 MBEDTLS_SSL_PRINT_EXT(
2173 3, MBEDTLS_SSL_HS_ENCRYPTED_EXTENSIONS,
2174 extension_type, "( ignored )");
2175 break;
2176 }
2177
2178 p += extension_data_len;
2179 }
2180
2181 if ((handshake->received_extensions & MBEDTLS_SSL_EXT_MASK(RECORD_SIZE_LIMIT)) &&
2182 (handshake->received_extensions & MBEDTLS_SSL_EXT_MASK(MAX_FRAGMENT_LENGTH))) {
2183 MBEDTLS_SSL_DEBUG_MSG(3,
2184 (
2185 "Record size limit extension cannot be used with max fragment length extension"));
2186 MBEDTLS_SSL_PEND_FATAL_ALERT(
2187 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER,
2188 MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER);
2189 return MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER;
2190 }
2191
2192 MBEDTLS_SSL_PRINT_EXTS(3, MBEDTLS_SSL_HS_ENCRYPTED_EXTENSIONS,
2193 handshake->received_extensions);
2194
2195 /* Check that we consumed all the message. */
2196 if (p != end) {
2197 MBEDTLS_SSL_DEBUG_MSG(1, ("EncryptedExtension lengths misaligned"));
2198 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR,
2199 MBEDTLS_ERR_SSL_DECODE_ERROR);
2200 return MBEDTLS_ERR_SSL_DECODE_ERROR;
2201 }
2202
2203 return ret;
2204}
2205
2206MBEDTLS_CHECK_RETURN_CRITICAL
2207static int ssl_tls13_process_encrypted_extensions(mbedtls_ssl_context *ssl)
2208{
2209 int ret;
2210 unsigned char *buf;
2211 size_t buf_len;
2212 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
2213
2214 MBEDTLS_SSL_DEBUG_MSG(2, ("=> parse encrypted extensions"));
2215
2216 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_tls13_fetch_handshake_msg(
2217 ssl, MBEDTLS_SSL_HS_ENCRYPTED_EXTENSIONS,
2218 &buf, &buf_len));
2219
2220 /* Process the message contents */
2221 MBEDTLS_SSL_PROC_CHK(
2222 ssl_tls13_parse_encrypted_extensions(ssl, buf, buf + buf_len));
2223
2224#if defined(MBEDTLS_SSL_EARLY_DATA)
2225 if (handshake->received_extensions & MBEDTLS_SSL_EXT_MASK(EARLY_DATA)) {
2226 /* RFC8446 4.2.11
2227 * If the server supplies an "early_data" extension, the
2228 * client MUST verify that the server's selected_identity
2229 * is 0. If any other value is returned, the client MUST
2230 * abort the handshake with an "illegal_parameter" alert.
2231 *
2232 * RFC 8446 4.2.10
2233 * In order to accept early data, the server MUST have accepted a PSK
2234 * cipher suite and selected the first key offered in the client's
2235 * "pre_shared_key" extension. In addition, it MUST verify that the
2236 * following values are the same as those associated with the
2237 * selected PSK:
2238 * - The TLS version number
2239 * - The selected cipher suite
2240 * - The selected ALPN [RFC7301] protocol, if any
2241 *
2242 * The server has sent an early data extension in its Encrypted
2243 * Extension message thus accepted to receive early data. We
2244 * check here that the additional constraints on the handshake
2245 * parameters, when early data are exchanged, are met,
2246 * namely:
2247 * - a PSK has been selected for the handshake
2248 * - the selected PSK for the handshake was the first one proposed
2249 * by the client.
2250 * - the selected ciphersuite for the handshake is the ciphersuite
2251 * associated with the selected PSK.
2252 */
2253 if ((!mbedtls_ssl_tls13_key_exchange_mode_with_psk(ssl)) ||
2254 handshake->selected_identity != 0 ||
2255 handshake->ciphersuite_info->id !=
2256 ssl->session_negotiate->ciphersuite) {
2257
2258 MBEDTLS_SSL_PEND_FATAL_ALERT(
2259 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER,
2260 MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER);
2261 return MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER;
2262 }
2263
2264 ssl->early_data_state = MBEDTLS_SSL_EARLY_DATA_STATE_ACCEPTED;
2265 } else if (ssl->early_data_state !=
2266 MBEDTLS_SSL_EARLY_DATA_STATE_NO_IND_SENT) {
2267 ssl->early_data_state = MBEDTLS_SSL_EARLY_DATA_STATE_REJECTED;
2268 }
2269#endif
2270
2271 /*
2272 * In case the client has proposed a PSK associated with a ticket,
2273 * `ssl->session_negotiate->ciphersuite` still contains at this point the
2274 * identifier of the ciphersuite associated with the ticket. This is that
2275 * way because, if an exchange of early data is agreed upon, we need
2276 * it to check that the ciphersuite selected for the handshake is the
2277 * ticket ciphersuite (see above). This information is not needed
2278 * anymore thus we can now set it to the identifier of the ciphersuite
2279 * used in this session under negotiation.
2280 */
2281 ssl->session_negotiate->ciphersuite = handshake->ciphersuite_info->id;
2282
2283 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_add_hs_msg_to_checksum(
2284 ssl, MBEDTLS_SSL_HS_ENCRYPTED_EXTENSIONS,
2285 buf, buf_len));
2286
2287#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED)
2288 if (mbedtls_ssl_tls13_key_exchange_mode_with_psk(ssl)) {
2289 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_SERVER_FINISHED);
2290
2291 /* Since we're not using a certificate, set verify_result to success */
2292 ssl->session_negotiate->verify_result = 0;
2293 } else {
2294 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_CERTIFICATE_REQUEST);
2295 }
2296#else
2297 ((void) ssl);
2298 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_SERVER_FINISHED);
2299#endif
2300
2301cleanup:
2302
2303 MBEDTLS_SSL_DEBUG_MSG(2, ("<= parse encrypted extensions"));
2304 return ret;
2305
2306}
2307
2308#if defined(MBEDTLS_SSL_EARLY_DATA)
2309/*
2310 * Handler for MBEDTLS_SSL_END_OF_EARLY_DATA
2311 *
2312 * RFC 8446 section 4.5
2313 *
2314 * struct {} EndOfEarlyData;
2315 *
2316 * If the server sent an "early_data" extension in EncryptedExtensions, the
2317 * client MUST send an EndOfEarlyData message after receiving the server
2318 * Finished. Otherwise, the client MUST NOT send an EndOfEarlyData message.
2319 */
2320
2321MBEDTLS_CHECK_RETURN_CRITICAL
2322static int ssl_tls13_write_end_of_early_data(mbedtls_ssl_context *ssl)
2323{
2324 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
2325 unsigned char *buf = NULL;
2326 size_t buf_len;
2327 MBEDTLS_SSL_DEBUG_MSG(2, ("=> write EndOfEarlyData"));
2328
2329 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_start_handshake_msg(
2330 ssl, MBEDTLS_SSL_HS_END_OF_EARLY_DATA,
2331 &buf, &buf_len));
2332
2333 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_add_hs_hdr_to_checksum(
2334 ssl, MBEDTLS_SSL_HS_END_OF_EARLY_DATA, 0));
2335
2336 MBEDTLS_SSL_PROC_CHK(
2337 mbedtls_ssl_finish_handshake_msg(ssl, buf_len, 0));
2338
2339 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_CLIENT_CERTIFICATE);
2340
2341cleanup:
2342
2343 MBEDTLS_SSL_DEBUG_MSG(2, ("<= write EndOfEarlyData"));
2344 return ret;
2345}
2346
2347int mbedtls_ssl_get_early_data_status(mbedtls_ssl_context *ssl)
2348{
2349 if ((ssl->conf->endpoint != MBEDTLS_SSL_IS_CLIENT) ||
2350 (!mbedtls_ssl_is_handshake_over(ssl))) {
2351 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
2352 }
2353
2354 switch (ssl->early_data_state) {
2355 case MBEDTLS_SSL_EARLY_DATA_STATE_NO_IND_SENT:
2356 return MBEDTLS_SSL_EARLY_DATA_STATUS_NOT_INDICATED;
2357 break;
2358
2359 case MBEDTLS_SSL_EARLY_DATA_STATE_REJECTED:
2360 return MBEDTLS_SSL_EARLY_DATA_STATUS_REJECTED;
2361 break;
2362
2363 case MBEDTLS_SSL_EARLY_DATA_STATE_SERVER_FINISHED_RECEIVED:
2364 return MBEDTLS_SSL_EARLY_DATA_STATUS_ACCEPTED;
2365 break;
2366
2367 default:
2368 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
2369 }
2370}
2371#endif /* MBEDTLS_SSL_EARLY_DATA */
2372
2373#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED)
2374/*
2375 * STATE HANDLING: CertificateRequest
2376 *
2377 */
2378#define SSL_CERTIFICATE_REQUEST_EXPECT_REQUEST 0
2379#define SSL_CERTIFICATE_REQUEST_SKIP 1
2380/* Coordination:
2381 * Deals with the ambiguity of not knowing if a CertificateRequest
2382 * will be sent. Returns a negative code on failure, or
2383 * - SSL_CERTIFICATE_REQUEST_EXPECT_REQUEST
2384 * - SSL_CERTIFICATE_REQUEST_SKIP
2385 * indicating if a Certificate Request is expected or not.
2386 */
2387MBEDTLS_CHECK_RETURN_CRITICAL
2388static int ssl_tls13_certificate_request_coordinate(mbedtls_ssl_context *ssl)
2389{
2390 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
2391
2392 if ((ret = mbedtls_ssl_read_record(ssl, 0)) != 0) {
2393 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_read_record", ret);
2394 return ret;
2395 }
2396 ssl->keep_current_message = 1;
2397
2398 if ((ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE) &&
2399 (ssl->in_msg[0] == MBEDTLS_SSL_HS_CERTIFICATE_REQUEST)) {
2400 MBEDTLS_SSL_DEBUG_MSG(3, ("got a certificate request"));
2401 return SSL_CERTIFICATE_REQUEST_EXPECT_REQUEST;
2402 }
2403
2404 MBEDTLS_SSL_DEBUG_MSG(3, ("got no certificate request"));
2405
2406 return SSL_CERTIFICATE_REQUEST_SKIP;
2407}
2408
2409/*
2410 * ssl_tls13_parse_certificate_request()
2411 * Parse certificate request
2412 * struct {
2413 * opaque certificate_request_context<0..2^8-1>;
2414 * Extension extensions<2..2^16-1>;
2415 * } CertificateRequest;
2416 */
2417MBEDTLS_CHECK_RETURN_CRITICAL
2418static int ssl_tls13_parse_certificate_request(mbedtls_ssl_context *ssl,
2419 const unsigned char *buf,
2420 const unsigned char *end)
2421{
2422 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
2423 const unsigned char *p = buf;
2424 size_t certificate_request_context_len = 0;
2425 size_t extensions_len = 0;
2426 const unsigned char *extensions_end;
2427 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
2428
2429 /* ...
2430 * opaque certificate_request_context<0..2^8-1>
2431 * ...
2432 */
2433 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, 1);
2434 certificate_request_context_len = (size_t) p[0];
2435 p += 1;
2436
2437 if (certificate_request_context_len > 0) {
2438 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, certificate_request_context_len);
2439 MBEDTLS_SSL_DEBUG_BUF(3, "Certificate Request Context",
2440 p, certificate_request_context_len);
2441
2442 handshake->certificate_request_context =
2443 mbedtls_calloc(1, certificate_request_context_len);
2444 if (handshake->certificate_request_context == NULL) {
2445 MBEDTLS_SSL_DEBUG_MSG(1, ("buffer too small"));
2446 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
2447 }
2448 memcpy(handshake->certificate_request_context, p,
2449 certificate_request_context_len);
2450 p += certificate_request_context_len;
2451 }
2452
2453 /* ...
2454 * Extension extensions<2..2^16-1>;
2455 * ...
2456 */
2457 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, 2);
2458 extensions_len = MBEDTLS_GET_UINT16_BE(p, 0);
2459 p += 2;
2460
2461 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, extensions_len);
2462 extensions_end = p + extensions_len;
2463
2464 handshake->received_extensions = MBEDTLS_SSL_EXT_MASK_NONE;
2465
2466 while (p < extensions_end) {
2467 unsigned int extension_type;
2468 size_t extension_data_len;
2469
2470 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, extensions_end, 4);
2471 extension_type = MBEDTLS_GET_UINT16_BE(p, 0);
2472 extension_data_len = MBEDTLS_GET_UINT16_BE(p, 2);
2473 p += 4;
2474
2475 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, extensions_end, extension_data_len);
2476
2477 ret = mbedtls_ssl_tls13_check_received_extension(
2478 ssl, MBEDTLS_SSL_HS_CERTIFICATE_REQUEST, extension_type,
2479 MBEDTLS_SSL_TLS1_3_ALLOWED_EXTS_OF_CR);
2480 if (ret != 0) {
2481 return ret;
2482 }
2483
2484 switch (extension_type) {
2485 case MBEDTLS_TLS_EXT_SIG_ALG:
2486 MBEDTLS_SSL_DEBUG_MSG(3,
2487 ("found signature algorithms extension"));
2488 ret = mbedtls_ssl_parse_sig_alg_ext(ssl, p,
2489 p + extension_data_len);
2490 if (ret != 0) {
2491 return ret;
2492 }
2493
2494 break;
2495
2496 default:
2497 MBEDTLS_SSL_PRINT_EXT(
2498 3, MBEDTLS_SSL_HS_CERTIFICATE_REQUEST,
2499 extension_type, "( ignored )");
2500 break;
2501 }
2502
2503 p += extension_data_len;
2504 }
2505
2506 MBEDTLS_SSL_PRINT_EXTS(3, MBEDTLS_SSL_HS_CERTIFICATE_REQUEST,
2507 handshake->received_extensions);
2508
2509 /* Check that we consumed all the message. */
2510 if (p != end) {
2511 MBEDTLS_SSL_DEBUG_MSG(1,
2512 ("CertificateRequest misaligned"));
2513 goto decode_error;
2514 }
2515
2516 /* RFC 8446 section 4.3.2
2517 *
2518 * The "signature_algorithms" extension MUST be specified
2519 */
2520 if ((handshake->received_extensions & MBEDTLS_SSL_EXT_MASK(SIG_ALG)) == 0) {
2521 MBEDTLS_SSL_DEBUG_MSG(3,
2522 ("no signature algorithms extension found"));
2523 goto decode_error;
2524 }
2525
2526 ssl->handshake->client_auth = 1;
2527 return 0;
2528
2529decode_error:
2530 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR,
2531 MBEDTLS_ERR_SSL_DECODE_ERROR);
2532 return MBEDTLS_ERR_SSL_DECODE_ERROR;
2533}
2534
2535/*
2536 * Handler for MBEDTLS_SSL_CERTIFICATE_REQUEST
2537 */
2538MBEDTLS_CHECK_RETURN_CRITICAL
2539static int ssl_tls13_process_certificate_request(mbedtls_ssl_context *ssl)
2540{
2541 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
2542
2543 MBEDTLS_SSL_DEBUG_MSG(2, ("=> parse certificate request"));
2544
2545 MBEDTLS_SSL_PROC_CHK_NEG(ssl_tls13_certificate_request_coordinate(ssl));
2546
2547 if (ret == SSL_CERTIFICATE_REQUEST_EXPECT_REQUEST) {
2548 unsigned char *buf;
2549 size_t buf_len;
2550
2551 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_tls13_fetch_handshake_msg(
2552 ssl, MBEDTLS_SSL_HS_CERTIFICATE_REQUEST,
2553 &buf, &buf_len));
2554
2555 MBEDTLS_SSL_PROC_CHK(ssl_tls13_parse_certificate_request(
2556 ssl, buf, buf + buf_len));
2557
2558 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_add_hs_msg_to_checksum(
2559 ssl, MBEDTLS_SSL_HS_CERTIFICATE_REQUEST,
2560 buf, buf_len));
2561 } else if (ret == SSL_CERTIFICATE_REQUEST_SKIP) {
2562 ret = 0;
2563 } else {
2564 MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
2565 ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
2566 goto cleanup;
2567 }
2568
2569 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_SERVER_CERTIFICATE);
2570
2571cleanup:
2572
2573 MBEDTLS_SSL_DEBUG_MSG(2, ("<= parse certificate request"));
2574 return ret;
2575}
2576
2577/*
2578 * Handler for MBEDTLS_SSL_SERVER_CERTIFICATE
2579 */
2580MBEDTLS_CHECK_RETURN_CRITICAL
2581static int ssl_tls13_process_server_certificate(mbedtls_ssl_context *ssl)
2582{
2583 int ret;
2584
2585 ret = mbedtls_ssl_tls13_process_certificate(ssl);
2586 if (ret != 0) {
2587 return ret;
2588 }
2589
2590 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_CERTIFICATE_VERIFY);
2591 return 0;
2592}
2593
2594/*
2595 * Handler for MBEDTLS_SSL_CERTIFICATE_VERIFY
2596 */
2597MBEDTLS_CHECK_RETURN_CRITICAL
2598static int ssl_tls13_process_certificate_verify(mbedtls_ssl_context *ssl)
2599{
2600 int ret;
2601
2602 ret = mbedtls_ssl_tls13_process_certificate_verify(ssl);
2603 if (ret != 0) {
2604 return ret;
2605 }
2606
2607 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_SERVER_FINISHED);
2608 return 0;
2609}
2610#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED */
2611
2612/*
2613 * Handler for MBEDTLS_SSL_SERVER_FINISHED
2614 */
2615MBEDTLS_CHECK_RETURN_CRITICAL
2616static int ssl_tls13_process_server_finished(mbedtls_ssl_context *ssl)
2617{
2618 int ret;
2619
2620 ret = mbedtls_ssl_tls13_process_finished_message(ssl);
2621 if (ret != 0) {
2622 return ret;
2623 }
2624
2625 ret = mbedtls_ssl_tls13_compute_application_transform(ssl);
2626 if (ret != 0) {
2627 MBEDTLS_SSL_PEND_FATAL_ALERT(
2628 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE,
2629 MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE);
2630 return ret;
2631 }
2632
2633#if defined(MBEDTLS_SSL_EARLY_DATA)
2634 if (ssl->early_data_state == MBEDTLS_SSL_EARLY_DATA_STATE_ACCEPTED) {
2635 ssl->early_data_state = MBEDTLS_SSL_EARLY_DATA_STATE_SERVER_FINISHED_RECEIVED;
2636 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_END_OF_EARLY_DATA);
2637 } else
2638#endif /* MBEDTLS_SSL_EARLY_DATA */
2639 {
2640#if defined(MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE)
2641 mbedtls_ssl_handshake_set_state(
2642 ssl, MBEDTLS_SSL_CLIENT_CCS_AFTER_SERVER_FINISHED);
2643#else
2644 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_CLIENT_CERTIFICATE);
2645#endif /* MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE */
2646 }
2647
2648 return 0;
2649}
2650
2651/*
2652 * Handler for MBEDTLS_SSL_CLIENT_CERTIFICATE
2653 */
2654MBEDTLS_CHECK_RETURN_CRITICAL
2655static int ssl_tls13_write_client_certificate(mbedtls_ssl_context *ssl)
2656{
2657 int non_empty_certificate_msg = 0;
2658
2659 MBEDTLS_SSL_DEBUG_MSG(1,
2660 ("Switch to handshake traffic keys for outbound traffic"));
2661 mbedtls_ssl_set_outbound_transform(ssl, ssl->handshake->transform_handshake);
2662
2663#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED)
2664 if (ssl->handshake->client_auth) {
2665 int ret = mbedtls_ssl_tls13_write_certificate(ssl);
2666 if (ret != 0) {
2667 return ret;
2668 }
2669
2670 if (mbedtls_ssl_own_cert(ssl) != NULL) {
2671 non_empty_certificate_msg = 1;
2672 }
2673 } else {
2674 MBEDTLS_SSL_DEBUG_MSG(2, ("skip write certificate"));
2675 }
2676#endif
2677
2678 if (non_empty_certificate_msg) {
2679 mbedtls_ssl_handshake_set_state(ssl,
2680 MBEDTLS_SSL_CLIENT_CERTIFICATE_VERIFY);
2681 } else {
2682 MBEDTLS_SSL_DEBUG_MSG(2, ("skip write certificate verify"));
2683 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_CLIENT_FINISHED);
2684 }
2685
2686 return 0;
2687}
2688
2689#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED)
2690/*
2691 * Handler for MBEDTLS_SSL_CLIENT_CERTIFICATE_VERIFY
2692 */
2693MBEDTLS_CHECK_RETURN_CRITICAL
2694static int ssl_tls13_write_client_certificate_verify(mbedtls_ssl_context *ssl)
2695{
2696 int ret = mbedtls_ssl_tls13_write_certificate_verify(ssl);
2697
2698 if (ret == 0) {
2699 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_CLIENT_FINISHED);
2700 }
2701
2702 return ret;
2703}
2704#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED */
2705
2706/*
2707 * Handler for MBEDTLS_SSL_CLIENT_FINISHED
2708 */
2709MBEDTLS_CHECK_RETURN_CRITICAL
2710static int ssl_tls13_write_client_finished(mbedtls_ssl_context *ssl)
2711{
2712 int ret;
2713
2714 ret = mbedtls_ssl_tls13_write_finished_message(ssl);
2715 if (ret != 0) {
2716 return ret;
2717 }
2718
2719 ret = mbedtls_ssl_tls13_compute_resumption_master_secret(ssl);
2720 if (ret != 0) {
2721 MBEDTLS_SSL_DEBUG_RET(
2722 1, "mbedtls_ssl_tls13_compute_resumption_master_secret ", ret);
2723 return ret;
2724 }
2725
2726 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_FLUSH_BUFFERS);
2727 return 0;
2728}
2729
2730/*
2731 * Handler for MBEDTLS_SSL_FLUSH_BUFFERS
2732 */
2733MBEDTLS_CHECK_RETURN_CRITICAL
2734static int ssl_tls13_flush_buffers(mbedtls_ssl_context *ssl)
2735{
2736 MBEDTLS_SSL_DEBUG_MSG(2, ("handshake: done"));
2737 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_HANDSHAKE_WRAPUP);
2738 return 0;
2739}
2740
2741/*
2742 * Handler for MBEDTLS_SSL_HANDSHAKE_WRAPUP
2743 */
2744MBEDTLS_CHECK_RETURN_CRITICAL
2745static int ssl_tls13_handshake_wrapup(mbedtls_ssl_context *ssl)
2746{
2747
2748 mbedtls_ssl_tls13_handshake_wrapup(ssl);
2749
2750 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_HANDSHAKE_OVER);
2751 return 0;
2752}
2753
2754#if defined(MBEDTLS_SSL_SESSION_TICKETS)
2755
2756#if defined(MBEDTLS_SSL_EARLY_DATA)
2757/* From RFC 8446 section 4.2.10
2758 *
2759 * struct {
2760 * select (Handshake.msg_type) {
2761 * case new_session_ticket: uint32 max_early_data_size;
2762 * ...
2763 * };
2764 * } EarlyDataIndication;
2765 */
2766MBEDTLS_CHECK_RETURN_CRITICAL
2767static int ssl_tls13_parse_new_session_ticket_early_data_ext(
2768 mbedtls_ssl_context *ssl,
2769 const unsigned char *buf,
2770 const unsigned char *end)
2771{
2772 mbedtls_ssl_session *session = ssl->session;
2773
2774 MBEDTLS_SSL_CHK_BUF_READ_PTR(buf, end, 4);
2775
2776 session->max_early_data_size = MBEDTLS_GET_UINT32_BE(buf, 0);
2777 mbedtls_ssl_tls13_session_set_ticket_flags(
2778 session, MBEDTLS_SSL_TLS1_3_TICKET_ALLOW_EARLY_DATA);
2779 MBEDTLS_SSL_DEBUG_MSG(
2780 3, ("received max_early_data_size: %u",
2781 (unsigned int) session->max_early_data_size));
2782
2783 return 0;
2784}
2785#endif /* MBEDTLS_SSL_EARLY_DATA */
2786
2787MBEDTLS_CHECK_RETURN_CRITICAL
2788static int ssl_tls13_parse_new_session_ticket_exts(mbedtls_ssl_context *ssl,
2789 const unsigned char *buf,
2790 const unsigned char *end)
2791{
2792 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
2793 const unsigned char *p = buf;
2794
2795
2796 handshake->received_extensions = MBEDTLS_SSL_EXT_MASK_NONE;
2797
2798 while (p < end) {
2799 unsigned int extension_type;
2800 size_t extension_data_len;
2801 int ret;
2802
2803 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, 4);
2804 extension_type = MBEDTLS_GET_UINT16_BE(p, 0);
2805 extension_data_len = MBEDTLS_GET_UINT16_BE(p, 2);
2806 p += 4;
2807
2808 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, extension_data_len);
2809
2810 ret = mbedtls_ssl_tls13_check_received_extension(
2811 ssl, MBEDTLS_SSL_HS_NEW_SESSION_TICKET, extension_type,
2812 MBEDTLS_SSL_TLS1_3_ALLOWED_EXTS_OF_NST);
2813 if (ret != 0) {
2814 return ret;
2815 }
2816
2817 switch (extension_type) {
2818#if defined(MBEDTLS_SSL_EARLY_DATA)
2819 case MBEDTLS_TLS_EXT_EARLY_DATA:
2820 ret = ssl_tls13_parse_new_session_ticket_early_data_ext(
2821 ssl, p, p + extension_data_len);
2822 if (ret != 0) {
2823 MBEDTLS_SSL_DEBUG_RET(
2824 1, "ssl_tls13_parse_new_session_ticket_early_data_ext",
2825 ret);
2826 return ret;
2827 }
2828 break;
2829#endif /* MBEDTLS_SSL_EARLY_DATA */
2830
2831 default:
2832 MBEDTLS_SSL_PRINT_EXT(
2833 3, MBEDTLS_SSL_HS_NEW_SESSION_TICKET,
2834 extension_type, "( ignored )");
2835 break;
2836 }
2837
2838 p += extension_data_len;
2839 }
2840
2841 MBEDTLS_SSL_PRINT_EXTS(3, MBEDTLS_SSL_HS_NEW_SESSION_TICKET,
2842 handshake->received_extensions);
2843
2844 return 0;
2845}
2846
2847/*
2848 * From RFC8446, page 74
2849 *
2850 * struct {
2851 * uint32 ticket_lifetime;
2852 * uint32 ticket_age_add;
2853 * opaque ticket_nonce<0..255>;
2854 * opaque ticket<1..2^16-1>;
2855 * Extension extensions<0..2^16-2>;
2856 * } NewSessionTicket;
2857 *
2858 */
2859MBEDTLS_CHECK_RETURN_CRITICAL
2860static int ssl_tls13_parse_new_session_ticket(mbedtls_ssl_context *ssl,
2861 unsigned char *buf,
2862 unsigned char *end,
2863 unsigned char **ticket_nonce,
2864 size_t *ticket_nonce_len)
2865{
2866 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
2867 unsigned char *p = buf;
2868 mbedtls_ssl_session *session = ssl->session;
2869 size_t ticket_len;
2870 unsigned char *ticket;
2871 size_t extensions_len;
2872
2873 *ticket_nonce = NULL;
2874 *ticket_nonce_len = 0;
2875 /*
2876 * ticket_lifetime 4 bytes
2877 * ticket_age_add 4 bytes
2878 * ticket_nonce_len 1 byte
2879 */
2880 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, 9);
2881
2882 session->ticket_lifetime = MBEDTLS_GET_UINT32_BE(p, 0);
2883 MBEDTLS_SSL_DEBUG_MSG(3,
2884 ("ticket_lifetime: %u",
2885 (unsigned int) session->ticket_lifetime));
2886 if (session->ticket_lifetime >
2887 MBEDTLS_SSL_TLS1_3_MAX_ALLOWED_TICKET_LIFETIME) {
2888 MBEDTLS_SSL_DEBUG_MSG(3, ("ticket_lifetime exceeds 7 days."));
2889 return MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER;
2890 }
2891
2892 session->ticket_age_add = MBEDTLS_GET_UINT32_BE(p, 4);
2893 MBEDTLS_SSL_DEBUG_MSG(3,
2894 ("ticket_age_add: %u",
2895 (unsigned int) session->ticket_age_add));
2896
2897 *ticket_nonce_len = p[8];
2898 p += 9;
2899
2900 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, *ticket_nonce_len);
2901 *ticket_nonce = p;
2902 MBEDTLS_SSL_DEBUG_BUF(3, "ticket_nonce:", *ticket_nonce, *ticket_nonce_len);
2903 p += *ticket_nonce_len;
2904
2905 /* Ticket */
2906 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, 2);
2907 ticket_len = MBEDTLS_GET_UINT16_BE(p, 0);
2908 p += 2;
2909 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, ticket_len);
2910 MBEDTLS_SSL_DEBUG_BUF(3, "received ticket", p, ticket_len);
2911
2912 /* Check if we previously received a ticket already. */
2913 if (session->ticket != NULL || session->ticket_len > 0) {
2914 mbedtls_free(session->ticket);
2915 session->ticket = NULL;
2916 session->ticket_len = 0;
2917 }
2918
2919 if ((ticket = mbedtls_calloc(1, ticket_len)) == NULL) {
2920 MBEDTLS_SSL_DEBUG_MSG(1, ("ticket alloc failed"));
2921 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
2922 }
2923 memcpy(ticket, p, ticket_len);
2924 p += ticket_len;
2925 session->ticket = ticket;
2926 session->ticket_len = ticket_len;
2927
2928 /* Clear all flags in ticket_flags */
2929 mbedtls_ssl_tls13_session_clear_ticket_flags(
2930 session, MBEDTLS_SSL_TLS1_3_TICKET_FLAGS_MASK);
2931
2932 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, 2);
2933 extensions_len = MBEDTLS_GET_UINT16_BE(p, 0);
2934 p += 2;
2935 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, extensions_len);
2936
2937 MBEDTLS_SSL_DEBUG_BUF(3, "ticket extension", p, extensions_len);
2938
2939 ret = ssl_tls13_parse_new_session_ticket_exts(ssl, p, p + extensions_len);
2940 if (ret != 0) {
2941 MBEDTLS_SSL_DEBUG_RET(1,
2942 "ssl_tls13_parse_new_session_ticket_exts",
2943 ret);
2944 return ret;
2945 }
2946
2947 return 0;
2948}
2949
2950/* Non negative return values for ssl_tls13_postprocess_new_session_ticket().
2951 * - POSTPROCESS_NEW_SESSION_TICKET_SIGNAL, all good, we have to signal the
2952 * application that a valid ticket has been received.
2953 * - POSTPROCESS_NEW_SESSION_TICKET_DISCARD, no fatal error, we keep the
2954 * connection alive but we do not signal the ticket to the application.
2955 */
2956#define POSTPROCESS_NEW_SESSION_TICKET_SIGNAL 0
2957#define POSTPROCESS_NEW_SESSION_TICKET_DISCARD 1
2958MBEDTLS_CHECK_RETURN_CRITICAL
2959static int ssl_tls13_postprocess_new_session_ticket(mbedtls_ssl_context *ssl,
2960 unsigned char *ticket_nonce,
2961 size_t ticket_nonce_len)
2962{
2963 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
2964 mbedtls_ssl_session *session = ssl->session;
2965 const mbedtls_ssl_ciphersuite_t *ciphersuite_info;
2966 psa_algorithm_t psa_hash_alg;
2967 int hash_length;
2968
2969 if (session->ticket_lifetime == 0) {
2970 return POSTPROCESS_NEW_SESSION_TICKET_DISCARD;
2971 }
2972
2973#if defined(MBEDTLS_HAVE_TIME)
2974 /* Store ticket creation time */
2975 session->ticket_reception_time = mbedtls_ms_time();
2976#endif
2977
2978 ciphersuite_info = mbedtls_ssl_ciphersuite_from_id(session->ciphersuite);
2979 if (ciphersuite_info == NULL) {
2980 MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
2981 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
2982 }
2983
2984 psa_hash_alg = mbedtls_md_psa_alg_from_type((mbedtls_md_type_t) ciphersuite_info->mac);
2985 hash_length = PSA_HASH_LENGTH(psa_hash_alg);
2986 if (hash_length == -1 ||
2987 (size_t) hash_length > sizeof(session->resumption_key)) {
2988 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
2989 }
2990
2991
2992 MBEDTLS_SSL_DEBUG_BUF(3, "resumption_master_secret",
2993 session->app_secrets.resumption_master_secret,
2994 hash_length);
2995
2996 /* Compute resumption key
2997 *
2998 * HKDF-Expand-Label( resumption_master_secret,
2999 * "resumption", ticket_nonce, Hash.length )
3000 */
3001 ret = mbedtls_ssl_tls13_hkdf_expand_label(
3002 psa_hash_alg,
3003 session->app_secrets.resumption_master_secret,
3004 hash_length,
3005 MBEDTLS_SSL_TLS1_3_LBL_WITH_LEN(resumption),
3006 ticket_nonce,
3007 ticket_nonce_len,
3008 session->resumption_key,
3009 hash_length);
3010
3011 if (ret != 0) {
3012 MBEDTLS_SSL_DEBUG_RET(2,
3013 "Creating the ticket-resumed PSK failed",
3014 ret);
3015 return ret;
3016 }
3017
3018 session->resumption_key_len = hash_length;
3019
3020 MBEDTLS_SSL_DEBUG_BUF(3, "Ticket-resumed PSK",
3021 session->resumption_key,
3022 session->resumption_key_len);
3023
3024 /* Set ticket_flags depends on the selected key exchange modes */
3025 mbedtls_ssl_tls13_session_set_ticket_flags(
3026 session, ssl->conf->tls13_kex_modes);
3027 MBEDTLS_SSL_PRINT_TICKET_FLAGS(4, session->ticket_flags);
3028
3029 return POSTPROCESS_NEW_SESSION_TICKET_SIGNAL;
3030}
3031
3032/*
3033 * Handler for MBEDTLS_SSL_TLS1_3_NEW_SESSION_TICKET
3034 */
3035MBEDTLS_CHECK_RETURN_CRITICAL
3036static int ssl_tls13_process_new_session_ticket(mbedtls_ssl_context *ssl)
3037{
3038 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
3039 unsigned char *buf;
3040 size_t buf_len;
3041 unsigned char *ticket_nonce;
3042 size_t ticket_nonce_len;
3043
3044 MBEDTLS_SSL_DEBUG_MSG(2, ("=> parse new session ticket"));
3045
3046 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_tls13_fetch_handshake_msg(
3047 ssl, MBEDTLS_SSL_HS_NEW_SESSION_TICKET,
3048 &buf, &buf_len));
3049
3050 /*
3051 * We are about to update (maybe only partially) ticket data thus block
3052 * any session export for the time being.
3053 */
3054 ssl->session->exported = 1;
3055
3056 MBEDTLS_SSL_PROC_CHK(ssl_tls13_parse_new_session_ticket(
3057 ssl, buf, buf + buf_len,
3058 &ticket_nonce, &ticket_nonce_len));
3059
3060 MBEDTLS_SSL_PROC_CHK_NEG(ssl_tls13_postprocess_new_session_ticket(
3061 ssl, ticket_nonce, ticket_nonce_len));
3062
3063 switch (ret) {
3064 case POSTPROCESS_NEW_SESSION_TICKET_SIGNAL:
3065 /*
3066 * All good, we have received a new valid ticket, session data can
3067 * be exported now and we signal the ticket to the application.
3068 */
3069 ssl->session->exported = 0;
3070 ret = MBEDTLS_ERR_SSL_RECEIVED_NEW_SESSION_TICKET;
3071 break;
3072
3073 case POSTPROCESS_NEW_SESSION_TICKET_DISCARD:
3074 ret = 0;
3075 MBEDTLS_SSL_DEBUG_MSG(2, ("Discard new session ticket"));
3076 break;
3077
3078 default:
3079 ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
3080 }
3081
3082 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_HANDSHAKE_OVER);
3083
3084cleanup:
3085
3086 MBEDTLS_SSL_DEBUG_MSG(2, ("<= parse new session ticket"));
3087 return ret;
3088}
3089#endif /* MBEDTLS_SSL_SESSION_TICKETS */
3090
3091int mbedtls_ssl_tls13_handshake_client_step(mbedtls_ssl_context *ssl)
3092{
3093 int ret = 0;
3094
3095 switch (ssl->state) {
3096 case MBEDTLS_SSL_HELLO_REQUEST:
3097 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_CLIENT_HELLO);
3098 break;
3099
3100 case MBEDTLS_SSL_CLIENT_HELLO:
3101 ret = mbedtls_ssl_write_client_hello(ssl);
3102 break;
3103
3104 case MBEDTLS_SSL_SERVER_HELLO:
3105 ret = ssl_tls13_process_server_hello(ssl);
3106 break;
3107
3108 case MBEDTLS_SSL_ENCRYPTED_EXTENSIONS:
3109 ret = ssl_tls13_process_encrypted_extensions(ssl);
3110 break;
3111
3112#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED)
3113 case MBEDTLS_SSL_CERTIFICATE_REQUEST:
3114 ret = ssl_tls13_process_certificate_request(ssl);
3115 break;
3116
3117 case MBEDTLS_SSL_SERVER_CERTIFICATE:
3118 ret = ssl_tls13_process_server_certificate(ssl);
3119 break;
3120
3121 case MBEDTLS_SSL_CERTIFICATE_VERIFY:
3122 ret = ssl_tls13_process_certificate_verify(ssl);
3123 break;
3124#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED */
3125
3126 case MBEDTLS_SSL_SERVER_FINISHED:
3127 ret = ssl_tls13_process_server_finished(ssl);
3128 break;
3129
3130#if defined(MBEDTLS_SSL_EARLY_DATA)
3131 case MBEDTLS_SSL_END_OF_EARLY_DATA:
3132 ret = ssl_tls13_write_end_of_early_data(ssl);
3133 break;
3134#endif
3135
3136 case MBEDTLS_SSL_CLIENT_CERTIFICATE:
3137 ret = ssl_tls13_write_client_certificate(ssl);
3138 break;
3139
3140#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED)
3141 case MBEDTLS_SSL_CLIENT_CERTIFICATE_VERIFY:
3142 ret = ssl_tls13_write_client_certificate_verify(ssl);
3143 break;
3144#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED */
3145
3146 case MBEDTLS_SSL_CLIENT_FINISHED:
3147 ret = ssl_tls13_write_client_finished(ssl);
3148 break;
3149
3150 case MBEDTLS_SSL_FLUSH_BUFFERS:
3151 ret = ssl_tls13_flush_buffers(ssl);
3152 break;
3153
3154 case MBEDTLS_SSL_HANDSHAKE_WRAPUP:
3155 ret = ssl_tls13_handshake_wrapup(ssl);
3156 break;
3157
3158 /*
3159 * Injection of dummy-CCS's for middlebox compatibility
3160 */
3161#if defined(MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE)
3162 case MBEDTLS_SSL_CLIENT_CCS_BEFORE_2ND_CLIENT_HELLO:
3163 ret = mbedtls_ssl_tls13_write_change_cipher_spec(ssl);
3164 if (ret != 0) {
3165 break;
3166 }
3167 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_CLIENT_HELLO);
3168 break;
3169
3170 case MBEDTLS_SSL_CLIENT_CCS_AFTER_SERVER_FINISHED:
3171 ret = mbedtls_ssl_tls13_write_change_cipher_spec(ssl);
3172 if (ret != 0) {
3173 break;
3174 }
3175 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_CLIENT_CERTIFICATE);
3176 break;
3177
3178#if defined(MBEDTLS_SSL_EARLY_DATA)
3179 case MBEDTLS_SSL_CLIENT_CCS_AFTER_CLIENT_HELLO:
3180 ret = mbedtls_ssl_tls13_write_change_cipher_spec(ssl);
3181 if (ret == 0) {
3182 mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_SERVER_HELLO);
3183
3184 MBEDTLS_SSL_DEBUG_MSG(
3185 1, ("Switch to early data keys for outbound traffic"));
3186 mbedtls_ssl_set_outbound_transform(
3187 ssl, ssl->handshake->transform_earlydata);
3188 ssl->early_data_state = MBEDTLS_SSL_EARLY_DATA_STATE_CAN_WRITE;
3189 }
3190 break;
3191#endif /* MBEDTLS_SSL_EARLY_DATA */
3192#endif /* MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE */
3193
3194#if defined(MBEDTLS_SSL_SESSION_TICKETS)
3195 case MBEDTLS_SSL_TLS1_3_NEW_SESSION_TICKET:
3196 ret = ssl_tls13_process_new_session_ticket(ssl);
3197 break;
3198#endif /* MBEDTLS_SSL_SESSION_TICKETS */
3199
3200 default:
3201 MBEDTLS_SSL_DEBUG_MSG(1, ("invalid state %d", ssl->state));
3202 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
3203 }
3204
3205 return ret;
3206}
3207
3208#endif /* MBEDTLS_SSL_CLI_C && MBEDTLS_SSL_PROTO_TLS1_3 */