scorpiox-bot-plugin-webhook
←ssl_tls13_generic.cc
1793 lines · 61 KBHEADBlameDownload
1/*
2 * TLS 1.3 functionality shared between client and server
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_TLS_C) && defined(MBEDTLS_SSL_PROTO_TLS1_3)
11
12#include <string.h>
13
14#include "mbedtls/error.h"
15#include "debug_internal.h"
16#include "mbedtls/oid.h"
17#include "mbedtls/platform.h"
18#include "mbedtls/constant_time.h"
19#include "psa/crypto.h"
20#include "mbedtls/psa_util.h"
21
22#include "ssl_misc.h"
23#include "ssl_tls13_invasive.h"
24#include "ssl_tls13_keys.h"
25#include "ssl_debug_helpers.h"
26
27#include "psa/crypto.h"
28#include "psa_util_internal.h"
29
30/* Define a local translating function to save code size by not using too many
31 * arguments in each translating place. */
32static int local_err_translation(psa_status_t status)
33{
34 return psa_status_to_mbedtls(status, psa_to_ssl_errors,
35 ARRAY_LENGTH(psa_to_ssl_errors),
36 psa_generic_status_to_mbedtls);
37}
38#define PSA_TO_MBEDTLS_ERR(status) local_err_translation(status)
39
40int mbedtls_ssl_tls13_crypto_init(mbedtls_ssl_context *ssl)
41{
42 psa_status_t status = psa_crypto_init();
43 if (status != PSA_SUCCESS) {
44 (void) ssl; // unused when debugging is disabled
45 MBEDTLS_SSL_DEBUG_RET(1, "psa_crypto_init", status);
46 }
47 return PSA_TO_MBEDTLS_ERR(status);
48}
49
50const uint8_t mbedtls_ssl_tls13_hello_retry_request_magic[
51 MBEDTLS_SERVER_HELLO_RANDOM_LEN] =
52{ 0xCF, 0x21, 0xAD, 0x74, 0xE5, 0x9A, 0x61, 0x11,
53 0xBE, 0x1D, 0x8C, 0x02, 0x1E, 0x65, 0xB8, 0x91,
54 0xC2, 0xA2, 0x11, 0x16, 0x7A, 0xBB, 0x8C, 0x5E,
55 0x07, 0x9E, 0x09, 0xE2, 0xC8, 0xA8, 0x33, 0x9C };
56
57int mbedtls_ssl_tls13_fetch_handshake_msg(mbedtls_ssl_context *ssl,
58 unsigned hs_type,
59 unsigned char **buf,
60 size_t *buf_len)
61{
62 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
63 int require_record_boundary;
64
65 /* Require record-boundary alignment by default. The exceptions below are
66 * handshake messages that may legitimately be followed by additional
67 * handshake messages in the same record. Using the default behaviour for a
68 * message that should be exempt is an interoperability bug; exempting a
69 * message that should not be exempt may have security implications.
70 */
71 switch (hs_type) {
72 case MBEDTLS_SSL_HS_NEW_SESSION_TICKET:
73 case MBEDTLS_SSL_HS_ENCRYPTED_EXTENSIONS:
74 case MBEDTLS_SSL_HS_CERTIFICATE:
75 case MBEDTLS_SSL_HS_CERTIFICATE_REQUEST:
76 case MBEDTLS_SSL_HS_CERTIFICATE_VERIFY:
77 /*
78 * For TLS 1.3, ServerHello (including HelloRetryRequest) must end at a
79 * record boundary, but not for TLS 1.2. At this point the ServerHello
80 * has not yet been processed, so we do not know whether it negotiates
81 * TLS 1.3 or TLS 1.2. Defer the boundary check until the protocol
82 * version is known to be TLS 1.3.
83 */
84 case MBEDTLS_SSL_HS_SERVER_HELLO:
85 require_record_boundary = 0;
86 break;
87
88 default:
89 /* ClientHello, EndOfEarlyData, Finished, and KeyUpdate.
90 */
91 require_record_boundary = 1;
92 }
93
94 ret = mbedtls_ssl_read_record(ssl, 0);
95 if (ret != 0) {
96 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_read_record", ret);
97 goto error;
98 }
99
100 ret = MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE;
101 if (ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE ||
102 ssl->in_msg[0] != hs_type) {
103 MBEDTLS_SSL_DEBUG_MSG(1, ("Receive unexpected handshake message."));
104 goto error;
105 }
106
107 if (require_record_boundary && (ssl->in_hslen != ssl->in_msglen)) {
108 MBEDTLS_SSL_DEBUG_MSG(1, ("Handshake message %s end not aligned with a record boundary",
109 mbedtls_ssl_get_hs_msg_name(hs_type)));
110 goto error;
111 }
112
113 /*
114 * Jump handshake header (4 bytes, see Section 4 of RFC 8446).
115 * ...
116 * HandshakeType msg_type;
117 * uint24 length;
118 * ...
119 */
120 *buf = ssl->in_msg + 4;
121 *buf_len = ssl->in_hslen - 4;
122 return 0;
123
124error:
125 if (ret == MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE) {
126 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE,
127 MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE);
128 }
129 return ret;
130}
131
132int mbedtls_ssl_tls13_is_supported_versions_ext_present_in_exts(
133 mbedtls_ssl_context *ssl,
134 const unsigned char *buf, const unsigned char *end,
135 const unsigned char **supported_versions_data,
136 const unsigned char **supported_versions_data_end)
137{
138 const unsigned char *p = buf;
139 size_t extensions_len;
140 const unsigned char *extensions_end;
141
142 *supported_versions_data = NULL;
143 *supported_versions_data_end = NULL;
144
145 /* Case of no extension */
146 if (p == end) {
147 return 0;
148 }
149
150 /* ...
151 * Extension extensions<x..2^16-1>;
152 * ...
153 * struct {
154 * ExtensionType extension_type; (2 bytes)
155 * opaque extension_data<0..2^16-1>;
156 * } Extension;
157 */
158 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, 2);
159 extensions_len = MBEDTLS_GET_UINT16_BE(p, 0);
160 p += 2;
161
162 /* Check extensions do not go beyond the buffer of data. */
163 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, extensions_len);
164 extensions_end = p + extensions_len;
165
166 while (p < extensions_end) {
167 unsigned int extension_type;
168 size_t extension_data_len;
169
170 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, extensions_end, 4);
171 extension_type = MBEDTLS_GET_UINT16_BE(p, 0);
172 extension_data_len = MBEDTLS_GET_UINT16_BE(p, 2);
173 p += 4;
174 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, extensions_end, extension_data_len);
175
176 if (extension_type == MBEDTLS_TLS_EXT_SUPPORTED_VERSIONS) {
177 *supported_versions_data = p;
178 *supported_versions_data_end = p + extension_data_len;
179 return 1;
180 }
181 p += extension_data_len;
182 }
183
184 return 0;
185}
186
187#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED)
188/*
189 * STATE HANDLING: Read CertificateVerify
190 */
191/* Macro to express the maximum length of the verify structure.
192 *
193 * The structure is computed per TLS 1.3 specification as:
194 * - 64 bytes of octet 32,
195 * - 33 bytes for the context string
196 * (which is either "TLS 1.3, client CertificateVerify"
197 * or "TLS 1.3, server CertificateVerify"),
198 * - 1 byte for the octet 0x0, which serves as a separator,
199 * - 32 or 48 bytes for the Transcript-Hash(Handshake Context, Certificate)
200 * (depending on the size of the transcript_hash)
201 *
202 * This results in a total size of
203 * - 130 bytes for a SHA256-based transcript hash, or
204 * (64 + 33 + 1 + 32 bytes)
205 * - 146 bytes for a SHA384-based transcript hash.
206 * (64 + 33 + 1 + 48 bytes)
207 *
208 */
209#define SSL_VERIFY_STRUCT_MAX_SIZE (64 + \
210 33 + \
211 1 + \
212 MBEDTLS_TLS1_3_MD_MAX_SIZE \
213 )
214
215/*
216 * The ssl_tls13_create_verify_structure() creates the verify structure.
217 * As input, it requires the transcript hash.
218 *
219 * The caller has to ensure that the buffer has size at least
220 * SSL_VERIFY_STRUCT_MAX_SIZE bytes.
221 */
222static void ssl_tls13_create_verify_structure(const unsigned char *transcript_hash,
223 size_t transcript_hash_len,
224 unsigned char *verify_buffer,
225 size_t *verify_buffer_len,
226 int from)
227{
228 size_t idx;
229
230 /* RFC 8446, Section 4.4.3:
231 *
232 * The digital signature [in the CertificateVerify message] is then
233 * computed over the concatenation of:
234 * - A string that consists of octet 32 (0x20) repeated 64 times
235 * - The context string
236 * - A single 0 byte which serves as the separator
237 * - The content to be signed
238 */
239 memset(verify_buffer, 0x20, 64);
240 idx = 64;
241
242 if (from == MBEDTLS_SSL_IS_CLIENT) {
243 memcpy(verify_buffer + idx, mbedtls_ssl_tls13_labels.client_cv,
244 MBEDTLS_SSL_TLS1_3_LBL_LEN(client_cv));
245 idx += MBEDTLS_SSL_TLS1_3_LBL_LEN(client_cv);
246 } else { /* from == MBEDTLS_SSL_IS_SERVER */
247 memcpy(verify_buffer + idx, mbedtls_ssl_tls13_labels.server_cv,
248 MBEDTLS_SSL_TLS1_3_LBL_LEN(server_cv));
249 idx += MBEDTLS_SSL_TLS1_3_LBL_LEN(server_cv);
250 }
251
252 verify_buffer[idx++] = 0x0;
253
254 memcpy(verify_buffer + idx, transcript_hash, transcript_hash_len);
255 idx += transcript_hash_len;
256
257 *verify_buffer_len = idx;
258}
259
260MBEDTLS_CHECK_RETURN_CRITICAL
261static int ssl_tls13_parse_certificate_verify(mbedtls_ssl_context *ssl,
262 const unsigned char *buf,
263 const unsigned char *end,
264 const unsigned char *verify_buffer,
265 size_t verify_buffer_len)
266{
267 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
268 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
269 const unsigned char *p = buf;
270 uint16_t algorithm;
271 size_t signature_len;
272 mbedtls_pk_type_t sig_alg;
273 mbedtls_md_type_t md_alg;
274 psa_algorithm_t hash_alg = PSA_ALG_NONE;
275 unsigned char verify_hash[PSA_HASH_MAX_SIZE];
276 size_t verify_hash_len;
277
278 void const *options = NULL;
279#if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT)
280 mbedtls_pk_rsassa_pss_options rsassa_pss_options;
281#endif /* MBEDTLS_X509_RSASSA_PSS_SUPPORT */
282
283 /*
284 * struct {
285 * SignatureScheme algorithm;
286 * opaque signature<0..2^16-1>;
287 * } CertificateVerify;
288 */
289 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, 2);
290 algorithm = MBEDTLS_GET_UINT16_BE(p, 0);
291 p += 2;
292
293 /* RFC 8446 section 4.4.3
294 *
295 * If the CertificateVerify message is sent by a server, the signature
296 * algorithm MUST be one offered in the client's "signature_algorithms"
297 * extension unless no valid certificate chain can be produced without
298 * unsupported algorithms
299 *
300 * RFC 8446 section 4.4.2.2
301 *
302 * If the client cannot construct an acceptable chain using the provided
303 * certificates and decides to abort the handshake, then it MUST abort the
304 * handshake with an appropriate certificate-related alert
305 * (by default, "unsupported_certificate").
306 *
307 * Check if algorithm is an offered signature algorithm.
308 */
309 if (!mbedtls_ssl_sig_alg_is_offered(ssl, algorithm)) {
310 /* algorithm not in offered signature algorithms list */
311 MBEDTLS_SSL_DEBUG_MSG(1, ("Received signature algorithm(%04x) is not "
312 "offered.",
313 (unsigned int) algorithm));
314 goto error;
315 }
316
317 if (mbedtls_ssl_get_pk_type_and_md_alg_from_sig_alg(
318 algorithm, &sig_alg, &md_alg) != 0) {
319 goto error;
320 }
321
322 hash_alg = mbedtls_md_psa_alg_from_type(md_alg);
323 if (hash_alg == 0) {
324 goto error;
325 }
326
327 MBEDTLS_SSL_DEBUG_MSG(3, ("Certificate Verify: Signature algorithm ( %04x )",
328 (unsigned int) algorithm));
329
330 /*
331 * Check the certificate's key type matches the signature alg
332 */
333 if (!mbedtls_pk_can_do(&ssl->session_negotiate->peer_cert->pk, sig_alg)) {
334 MBEDTLS_SSL_DEBUG_MSG(1, ("signature algorithm doesn't match cert key"));
335 goto error;
336 }
337
338 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, 2);
339 signature_len = MBEDTLS_GET_UINT16_BE(p, 0);
340 p += 2;
341 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, signature_len);
342
343 status = psa_hash_compute(hash_alg,
344 verify_buffer,
345 verify_buffer_len,
346 verify_hash,
347 sizeof(verify_hash),
348 &verify_hash_len);
349 if (status != PSA_SUCCESS) {
350 MBEDTLS_SSL_DEBUG_RET(1, "hash computation PSA error", status);
351 goto error;
352 }
353
354 MBEDTLS_SSL_DEBUG_BUF(3, "verify hash", verify_hash, verify_hash_len);
355#if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT)
356 if (sig_alg == MBEDTLS_PK_RSASSA_PSS) {
357 rsassa_pss_options.mgf1_hash_id = md_alg;
358
359 rsassa_pss_options.expected_salt_len = PSA_HASH_LENGTH(hash_alg);
360 options = (const void *) &rsassa_pss_options;
361 }
362#endif /* MBEDTLS_X509_RSASSA_PSS_SUPPORT */
363
364 if ((ret = mbedtls_pk_verify_ext(sig_alg, options,
365 &ssl->session_negotiate->peer_cert->pk,
366 md_alg, verify_hash, verify_hash_len,
367 p, signature_len)) == 0) {
368 return 0;
369 }
370 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_pk_verify_ext", ret);
371
372error:
373 /* RFC 8446 section 4.4.3
374 *
375 * If the verification fails, the receiver MUST terminate the handshake
376 * with a "decrypt_error" alert.
377 */
378 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_DECRYPT_ERROR,
379 MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE);
380 return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
381
382}
383#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED */
384
385int mbedtls_ssl_tls13_process_certificate_verify(mbedtls_ssl_context *ssl)
386{
387
388#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED)
389 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
390 unsigned char verify_buffer[SSL_VERIFY_STRUCT_MAX_SIZE];
391 size_t verify_buffer_len;
392 unsigned char transcript[MBEDTLS_TLS1_3_MD_MAX_SIZE];
393 size_t transcript_len;
394 unsigned char *buf;
395 size_t buf_len;
396
397 MBEDTLS_SSL_DEBUG_MSG(2, ("=> parse certificate verify"));
398
399 MBEDTLS_SSL_PROC_CHK(
400 mbedtls_ssl_tls13_fetch_handshake_msg(
401 ssl, MBEDTLS_SSL_HS_CERTIFICATE_VERIFY, &buf, &buf_len));
402
403 /* Need to calculate the hash of the transcript first
404 * before reading the message since otherwise it gets
405 * included in the transcript
406 */
407 ret = mbedtls_ssl_get_handshake_transcript(
408 ssl,
409 (mbedtls_md_type_t) ssl->handshake->ciphersuite_info->mac,
410 transcript, sizeof(transcript),
411 &transcript_len);
412 if (ret != 0) {
413 MBEDTLS_SSL_PEND_FATAL_ALERT(
414 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR,
415 MBEDTLS_ERR_SSL_INTERNAL_ERROR);
416 return ret;
417 }
418
419 MBEDTLS_SSL_DEBUG_BUF(3, "handshake hash", transcript, transcript_len);
420
421 /* Create verify structure */
422 ssl_tls13_create_verify_structure(transcript,
423 transcript_len,
424 verify_buffer,
425 &verify_buffer_len,
426 (ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT) ?
427 MBEDTLS_SSL_IS_SERVER :
428 MBEDTLS_SSL_IS_CLIENT);
429
430 /* Process the message contents */
431 MBEDTLS_SSL_PROC_CHK(ssl_tls13_parse_certificate_verify(
432 ssl, buf, buf + buf_len,
433 verify_buffer, verify_buffer_len));
434
435 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_add_hs_msg_to_checksum(
436 ssl, MBEDTLS_SSL_HS_CERTIFICATE_VERIFY,
437 buf, buf_len));
438
439cleanup:
440
441 MBEDTLS_SSL_DEBUG_MSG(2, ("<= parse certificate verify"));
442 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_tls13_process_certificate_verify", ret);
443 return ret;
444#else
445 ((void) ssl);
446 MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
447 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
448#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED */
449}
450
451/*
452 *
453 * STATE HANDLING: Incoming Certificate.
454 *
455 */
456
457#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED)
458#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
459/*
460 * Structure of Certificate message:
461 *
462 * enum {
463 * X509(0),
464 * RawPublicKey(2),
465 * (255)
466 * } CertificateType;
467 *
468 * struct {
469 * select (certificate_type) {
470 * case RawPublicKey:
471 * * From RFC 7250 ASN.1_subjectPublicKeyInfo *
472 * opaque ASN1_subjectPublicKeyInfo<1..2^24-1>;
473 * case X509:
474 * opaque cert_data<1..2^24-1>;
475 * };
476 * Extension extensions<0..2^16-1>;
477 * } CertificateEntry;
478 *
479 * struct {
480 * opaque certificate_request_context<0..2^8-1>;
481 * CertificateEntry certificate_list<0..2^24-1>;
482 * } Certificate;
483 *
484 */
485
486/* Parse certificate chain send by the server. */
487MBEDTLS_CHECK_RETURN_CRITICAL
488MBEDTLS_STATIC_TESTABLE
489int mbedtls_ssl_tls13_parse_certificate(mbedtls_ssl_context *ssl,
490 const unsigned char *buf,
491 const unsigned char *end)
492{
493 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
494 size_t certificate_request_context_len = 0;
495 size_t certificate_list_len = 0;
496 const unsigned char *p = buf;
497 const unsigned char *certificate_list_end;
498 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
499
500 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, 4);
501 certificate_request_context_len = p[0];
502 certificate_list_len = MBEDTLS_GET_UINT24_BE(p, 1);
503 p += 4;
504
505 /* In theory, the certificate list can be up to 2^24 Bytes, but we don't
506 * support anything beyond 2^16 = 64K.
507 */
508 if ((certificate_request_context_len != 0) ||
509 (certificate_list_len >= 0x10000)) {
510 MBEDTLS_SSL_DEBUG_MSG(1, ("bad certificate message"));
511 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR,
512 MBEDTLS_ERR_SSL_DECODE_ERROR);
513 return MBEDTLS_ERR_SSL_DECODE_ERROR;
514 }
515
516 /* In case we tried to reuse a session but it failed */
517 if (ssl->session_negotiate->peer_cert != NULL) {
518 mbedtls_x509_crt_free(ssl->session_negotiate->peer_cert);
519 mbedtls_free(ssl->session_negotiate->peer_cert);
520 }
521
522 /* This is used by ssl_tls13_validate_certificate() */
523 if (certificate_list_len == 0) {
524 ssl->session_negotiate->peer_cert = NULL;
525 ret = 0;
526 goto exit;
527 }
528
529 if ((ssl->session_negotiate->peer_cert =
530 mbedtls_calloc(1, sizeof(mbedtls_x509_crt))) == NULL) {
531 MBEDTLS_SSL_DEBUG_MSG(1, ("alloc( %" MBEDTLS_PRINTF_SIZET " bytes ) failed",
532 sizeof(mbedtls_x509_crt)));
533 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR,
534 MBEDTLS_ERR_SSL_ALLOC_FAILED);
535 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
536 }
537
538 mbedtls_x509_crt_init(ssl->session_negotiate->peer_cert);
539
540 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, certificate_list_len);
541 certificate_list_end = p + certificate_list_len;
542 while (p < certificate_list_end) {
543 size_t cert_data_len, extensions_len;
544 const unsigned char *extensions_end;
545
546 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, certificate_list_end, 3);
547 cert_data_len = MBEDTLS_GET_UINT24_BE(p, 0);
548 p += 3;
549
550 /* In theory, the CRT can be up to 2^24 Bytes, but we don't support
551 * anything beyond 2^16 = 64K. Otherwise as in the TLS 1.2 code,
552 * check that we have a minimum of 128 bytes of data, this is not
553 * clear why we need that though.
554 */
555 if ((cert_data_len < 128) || (cert_data_len >= 0x10000)) {
556 MBEDTLS_SSL_DEBUG_MSG(1, ("bad Certificate message"));
557 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR,
558 MBEDTLS_ERR_SSL_DECODE_ERROR);
559 return MBEDTLS_ERR_SSL_DECODE_ERROR;
560 }
561
562 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, certificate_list_end, cert_data_len);
563 ret = mbedtls_x509_crt_parse_der(ssl->session_negotiate->peer_cert,
564 p, cert_data_len);
565
566 switch (ret) {
567 case 0: /*ok*/
568 break;
569 case MBEDTLS_ERR_X509_UNKNOWN_SIG_ALG + MBEDTLS_ERR_OID_NOT_FOUND:
570 /* Ignore certificate with an unknown algorithm: maybe a
571 prior certificate was already trusted. */
572 break;
573
574 case MBEDTLS_ERR_X509_ALLOC_FAILED:
575 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR,
576 MBEDTLS_ERR_X509_ALLOC_FAILED);
577 MBEDTLS_SSL_DEBUG_RET(1, " mbedtls_x509_crt_parse_der", ret);
578 return ret;
579
580 case MBEDTLS_ERR_X509_UNKNOWN_VERSION:
581 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT,
582 MBEDTLS_ERR_X509_UNKNOWN_VERSION);
583 MBEDTLS_SSL_DEBUG_RET(1, " mbedtls_x509_crt_parse_der", ret);
584 return ret;
585
586 default:
587 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_BAD_CERT,
588 ret);
589 MBEDTLS_SSL_DEBUG_RET(1, " mbedtls_x509_crt_parse_der", ret);
590 return ret;
591 }
592
593 p += cert_data_len;
594
595 /* Certificate extensions length */
596 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, certificate_list_end, 2);
597 extensions_len = MBEDTLS_GET_UINT16_BE(p, 0);
598 p += 2;
599 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, certificate_list_end, extensions_len);
600
601 extensions_end = p + extensions_len;
602 handshake->received_extensions = MBEDTLS_SSL_EXT_MASK_NONE;
603
604 while (p < extensions_end) {
605 unsigned int extension_type;
606 size_t extension_data_len;
607
608 /*
609 * struct {
610 * ExtensionType extension_type; (2 bytes)
611 * opaque extension_data<0..2^16-1>;
612 * } Extension;
613 */
614 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, extensions_end, 4);
615 extension_type = MBEDTLS_GET_UINT16_BE(p, 0);
616 extension_data_len = MBEDTLS_GET_UINT16_BE(p, 2);
617 p += 4;
618
619 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, extensions_end, extension_data_len);
620
621 ret = mbedtls_ssl_tls13_check_received_extension(
622 ssl, MBEDTLS_SSL_HS_CERTIFICATE, extension_type,
623 MBEDTLS_SSL_TLS1_3_ALLOWED_EXTS_OF_CT);
624 if (ret != 0) {
625 return ret;
626 }
627
628 switch (extension_type) {
629 default:
630 MBEDTLS_SSL_PRINT_EXT(
631 3, MBEDTLS_SSL_HS_CERTIFICATE,
632 extension_type, "( ignored )");
633 break;
634 }
635
636 p += extension_data_len;
637 }
638
639 MBEDTLS_SSL_PRINT_EXTS(3, MBEDTLS_SSL_HS_CERTIFICATE,
640 handshake->received_extensions);
641 }
642
643exit:
644 /* Check that all the message is consumed. */
645 if (p != end) {
646 MBEDTLS_SSL_DEBUG_MSG(1, ("bad Certificate message"));
647 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR,
648 MBEDTLS_ERR_SSL_DECODE_ERROR);
649 return MBEDTLS_ERR_SSL_DECODE_ERROR;
650 }
651
652 MBEDTLS_SSL_DEBUG_CRT(3, "peer certificate",
653 ssl->session_negotiate->peer_cert);
654
655 return ret;
656}
657#else
658MBEDTLS_CHECK_RETURN_CRITICAL
659MBEDTLS_STATIC_TESTABLE
660int mbedtls_ssl_tls13_parse_certificate(mbedtls_ssl_context *ssl,
661 const unsigned char *buf,
662 const unsigned char *end)
663{
664 ((void) ssl);
665 ((void) buf);
666 ((void) end);
667 return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
668}
669#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
670#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED */
671
672#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED)
673#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
674/* Validate certificate chain sent by the server. */
675MBEDTLS_CHECK_RETURN_CRITICAL
676static int ssl_tls13_validate_certificate(mbedtls_ssl_context *ssl)
677{
678 /* Authmode: precedence order is SNI if used else configuration */
679#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
680 const int authmode = ssl->handshake->sni_authmode != MBEDTLS_SSL_VERIFY_UNSET
681 ? ssl->handshake->sni_authmode
682 : ssl->conf->authmode;
683#else
684 const int authmode = ssl->conf->authmode;
685#endif
686
687 /*
688 * If the peer hasn't sent a certificate ( i.e. it sent
689 * an empty certificate chain ), this is reflected in the peer CRT
690 * structure being unset.
691 * Check for that and handle it depending on the
692 * authentication mode.
693 */
694 if (ssl->session_negotiate->peer_cert == NULL) {
695 MBEDTLS_SSL_DEBUG_MSG(1, ("peer has no certificate"));
696
697#if defined(MBEDTLS_SSL_SRV_C)
698 if (ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER) {
699 /* The client was asked for a certificate but didn't send
700 * one. The client should know what's going on, so we
701 * don't send an alert.
702 */
703 ssl->session_negotiate->verify_result = MBEDTLS_X509_BADCERT_MISSING;
704 if (authmode == MBEDTLS_SSL_VERIFY_OPTIONAL) {
705 return 0;
706 } else {
707 MBEDTLS_SSL_PEND_FATAL_ALERT(
708 MBEDTLS_SSL_ALERT_MSG_NO_CERT,
709 MBEDTLS_ERR_SSL_NO_CLIENT_CERTIFICATE);
710 return MBEDTLS_ERR_SSL_NO_CLIENT_CERTIFICATE;
711 }
712 }
713#endif /* MBEDTLS_SSL_SRV_C */
714
715#if defined(MBEDTLS_SSL_CLI_C)
716 /* Regardless of authmode, the server is not allowed to send an empty
717 * certificate chain. (Last paragraph before 4.4.2.1 in RFC 8446: "The
718 * server's certificate_list MUST always be non-empty.") With authmode
719 * optional/none, we continue the handshake if we can't validate the
720 * server's cert, but we still break it if no certificate was sent. */
721 if (ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT) {
722 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_NO_CERT,
723 MBEDTLS_ERR_SSL_FATAL_ALERT_MESSAGE);
724 return MBEDTLS_ERR_SSL_FATAL_ALERT_MESSAGE;
725 }
726#endif /* MBEDTLS_SSL_CLI_C */
727 }
728
729 return mbedtls_ssl_verify_certificate(ssl, authmode,
730 ssl->session_negotiate->peer_cert,
731 NULL, NULL);
732}
733#else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
734MBEDTLS_CHECK_RETURN_CRITICAL
735static int ssl_tls13_validate_certificate(mbedtls_ssl_context *ssl)
736{
737 ((void) ssl);
738 return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
739}
740#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
741#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED */
742
743int mbedtls_ssl_tls13_process_certificate(mbedtls_ssl_context *ssl)
744{
745 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
746 MBEDTLS_SSL_DEBUG_MSG(2, ("=> parse certificate"));
747
748#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED)
749 unsigned char *buf;
750 size_t buf_len;
751
752 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_tls13_fetch_handshake_msg(
753 ssl, MBEDTLS_SSL_HS_CERTIFICATE,
754 &buf, &buf_len));
755
756 /* Parse the certificate chain sent by the peer. */
757 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_tls13_parse_certificate(ssl, buf,
758 buf + buf_len));
759 /* Validate the certificate chain and set the verification results. */
760 MBEDTLS_SSL_PROC_CHK(ssl_tls13_validate_certificate(ssl));
761
762 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_add_hs_msg_to_checksum(
763 ssl, MBEDTLS_SSL_HS_CERTIFICATE, buf, buf_len));
764
765cleanup:
766#else /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED */
767 (void) ssl;
768#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED */
769
770 MBEDTLS_SSL_DEBUG_MSG(2, ("<= parse certificate"));
771 return ret;
772}
773#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED)
774/*
775 * enum {
776 * X509(0),
777 * RawPublicKey(2),
778 * (255)
779 * } CertificateType;
780 *
781 * struct {
782 * select (certificate_type) {
783 * case RawPublicKey:
784 * // From RFC 7250 ASN.1_subjectPublicKeyInfo
785 * opaque ASN1_subjectPublicKeyInfo<1..2^24-1>;
786 *
787 * case X509:
788 * opaque cert_data<1..2^24-1>;
789 * };
790 * Extension extensions<0..2^16-1>;
791 * } CertificateEntry;
792 *
793 * struct {
794 * opaque certificate_request_context<0..2^8-1>;
795 * CertificateEntry certificate_list<0..2^24-1>;
796 * } Certificate;
797 */
798MBEDTLS_CHECK_RETURN_CRITICAL
799static int ssl_tls13_write_certificate_body(mbedtls_ssl_context *ssl,
800 unsigned char *buf,
801 unsigned char *end,
802 size_t *out_len)
803{
804 const mbedtls_x509_crt *crt = mbedtls_ssl_own_cert(ssl);
805 unsigned char *p = buf;
806 unsigned char *certificate_request_context =
807 ssl->handshake->certificate_request_context;
808 unsigned char certificate_request_context_len =
809 ssl->handshake->certificate_request_context_len;
810 unsigned char *p_certificate_list_len;
811
812
813 /* ...
814 * opaque certificate_request_context<0..2^8-1>;
815 * ...
816 */
817 MBEDTLS_SSL_CHK_BUF_PTR(p, end, certificate_request_context_len + 1);
818 *p++ = certificate_request_context_len;
819 if (certificate_request_context_len > 0) {
820 memcpy(p, certificate_request_context, certificate_request_context_len);
821 p += certificate_request_context_len;
822 }
823
824 /* ...
825 * CertificateEntry certificate_list<0..2^24-1>;
826 * ...
827 */
828 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 3);
829 p_certificate_list_len = p;
830 p += 3;
831
832 MBEDTLS_SSL_DEBUG_CRT(3, "own certificate", crt);
833
834 while (crt != NULL) {
835 size_t cert_data_len = crt->raw.len;
836
837 MBEDTLS_SSL_CHK_BUF_PTR(p, end, cert_data_len + 3 + 2);
838 MBEDTLS_PUT_UINT24_BE(cert_data_len, p, 0);
839 p += 3;
840
841 memcpy(p, crt->raw.p, cert_data_len);
842 p += cert_data_len;
843 crt = crt->next;
844
845 /* Currently, we don't have any certificate extensions defined.
846 * Hence, we are sending an empty extension with length zero.
847 */
848 MBEDTLS_PUT_UINT16_BE(0, p, 0);
849 p += 2;
850 }
851
852 MBEDTLS_PUT_UINT24_BE(p - p_certificate_list_len - 3,
853 p_certificate_list_len, 0);
854
855 *out_len = p - buf;
856
857 MBEDTLS_SSL_PRINT_EXTS(
858 3, MBEDTLS_SSL_HS_CERTIFICATE, ssl->handshake->sent_extensions);
859
860 return 0;
861}
862
863int mbedtls_ssl_tls13_write_certificate(mbedtls_ssl_context *ssl)
864{
865 int ret;
866 unsigned char *buf;
867 size_t buf_len, msg_len;
868
869 MBEDTLS_SSL_DEBUG_MSG(2, ("=> write certificate"));
870
871 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_start_handshake_msg(
872 ssl, MBEDTLS_SSL_HS_CERTIFICATE, &buf, &buf_len));
873
874 MBEDTLS_SSL_PROC_CHK(ssl_tls13_write_certificate_body(ssl,
875 buf,
876 buf + buf_len,
877 &msg_len));
878
879 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_add_hs_msg_to_checksum(
880 ssl, MBEDTLS_SSL_HS_CERTIFICATE, buf, msg_len));
881
882 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_finish_handshake_msg(
883 ssl, buf_len, msg_len));
884cleanup:
885
886 MBEDTLS_SSL_DEBUG_MSG(2, ("<= write certificate"));
887 return ret;
888}
889
890/*
891 * STATE HANDLING: Output Certificate Verify
892 */
893int mbedtls_ssl_tls13_check_sig_alg_cert_key_match(uint16_t sig_alg,
894 mbedtls_pk_context *key)
895{
896 mbedtls_pk_type_t pk_type = (mbedtls_pk_type_t) mbedtls_ssl_sig_from_pk(key);
897 size_t key_size = mbedtls_pk_get_bitlen(key);
898
899 switch (pk_type) {
900 case MBEDTLS_SSL_SIG_ECDSA:
901 switch (key_size) {
902 case 256:
903 return
904 sig_alg == MBEDTLS_TLS1_3_SIG_ECDSA_SECP256R1_SHA256;
905
906 case 384:
907 return
908 sig_alg == MBEDTLS_TLS1_3_SIG_ECDSA_SECP384R1_SHA384;
909
910 case 521:
911 return
912 sig_alg == MBEDTLS_TLS1_3_SIG_ECDSA_SECP521R1_SHA512;
913 default:
914 break;
915 }
916 break;
917
918 case MBEDTLS_SSL_SIG_RSA:
919 switch (sig_alg) {
920 case MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA256: /* Intentional fallthrough */
921 case MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA384: /* Intentional fallthrough */
922 case MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA512:
923 return 1;
924
925 default:
926 break;
927 }
928 break;
929
930 default:
931 break;
932 }
933
934 return 0;
935}
936
937MBEDTLS_CHECK_RETURN_CRITICAL
938static int ssl_tls13_write_certificate_verify_body(mbedtls_ssl_context *ssl,
939 unsigned char *buf,
940 unsigned char *end,
941 size_t *out_len)
942{
943 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
944 unsigned char *p = buf;
945 mbedtls_pk_context *own_key;
946
947 unsigned char handshake_hash[MBEDTLS_TLS1_3_MD_MAX_SIZE];
948 size_t handshake_hash_len;
949 unsigned char verify_buffer[SSL_VERIFY_STRUCT_MAX_SIZE];
950 size_t verify_buffer_len;
951
952 uint16_t *sig_alg = ssl->handshake->received_sig_algs;
953 size_t signature_len = 0;
954
955 *out_len = 0;
956
957 own_key = mbedtls_ssl_own_key(ssl);
958 if (own_key == NULL) {
959 MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
960 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
961 }
962
963 ret = mbedtls_ssl_get_handshake_transcript(
964 ssl, (mbedtls_md_type_t) ssl->handshake->ciphersuite_info->mac,
965 handshake_hash, sizeof(handshake_hash), &handshake_hash_len);
966 if (ret != 0) {
967 return ret;
968 }
969
970 MBEDTLS_SSL_DEBUG_BUF(3, "handshake hash",
971 handshake_hash,
972 handshake_hash_len);
973
974 ssl_tls13_create_verify_structure(handshake_hash, handshake_hash_len,
975 verify_buffer, &verify_buffer_len,
976 ssl->conf->endpoint);
977
978 /*
979 * struct {
980 * SignatureScheme algorithm;
981 * opaque signature<0..2^16-1>;
982 * } CertificateVerify;
983 */
984 /* Check there is space for the algorithm identifier (2 bytes) and the
985 * signature length (2 bytes).
986 */
987 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 4);
988
989 for (; *sig_alg != MBEDTLS_TLS1_3_SIG_NONE; sig_alg++) {
990 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
991 mbedtls_pk_type_t pk_type = MBEDTLS_PK_NONE;
992 mbedtls_md_type_t md_alg = MBEDTLS_MD_NONE;
993 psa_algorithm_t psa_algorithm = PSA_ALG_NONE;
994 unsigned char verify_hash[PSA_HASH_MAX_SIZE];
995 size_t verify_hash_len;
996
997 if (!mbedtls_ssl_sig_alg_is_offered(ssl, *sig_alg)) {
998 continue;
999 }
1000
1001 if (!mbedtls_ssl_tls13_sig_alg_for_cert_verify_is_supported(*sig_alg)) {
1002 continue;
1003 }
1004
1005 if (!mbedtls_ssl_tls13_check_sig_alg_cert_key_match(*sig_alg, own_key)) {
1006 continue;
1007 }
1008
1009 if (mbedtls_ssl_get_pk_type_and_md_alg_from_sig_alg(
1010 *sig_alg, &pk_type, &md_alg) != 0) {
1011 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
1012 }
1013
1014 /* Hash verify buffer with indicated hash function */
1015 psa_algorithm = mbedtls_md_psa_alg_from_type(md_alg);
1016 status = psa_hash_compute(psa_algorithm,
1017 verify_buffer,
1018 verify_buffer_len,
1019 verify_hash, sizeof(verify_hash),
1020 &verify_hash_len);
1021 if (status != PSA_SUCCESS) {
1022 return PSA_TO_MBEDTLS_ERR(status);
1023 }
1024
1025 MBEDTLS_SSL_DEBUG_BUF(3, "verify hash", verify_hash, verify_hash_len);
1026
1027 if ((ret = mbedtls_pk_sign_ext(pk_type, own_key,
1028 md_alg, verify_hash, verify_hash_len,
1029 p + 4, (size_t) (end - (p + 4)), &signature_len,
1030 ssl->conf->f_rng, ssl->conf->p_rng)) != 0) {
1031 MBEDTLS_SSL_DEBUG_MSG(2, ("CertificateVerify signature failed with %s",
1032 mbedtls_ssl_sig_alg_to_str(*sig_alg)));
1033 MBEDTLS_SSL_DEBUG_RET(2, "mbedtls_pk_sign_ext", ret);
1034
1035 /* The signature failed. This is possible if the private key
1036 * was not suitable for the signature operation as purposely we
1037 * did not check its suitability completely. Let's try with
1038 * another signature algorithm.
1039 */
1040 continue;
1041 }
1042
1043 MBEDTLS_SSL_DEBUG_MSG(2, ("CertificateVerify signature with %s",
1044 mbedtls_ssl_sig_alg_to_str(*sig_alg)));
1045
1046 break;
1047 }
1048
1049 if (*sig_alg == MBEDTLS_TLS1_3_SIG_NONE) {
1050 MBEDTLS_SSL_DEBUG_MSG(1, ("no suitable signature algorithm"));
1051 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE,
1052 MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE);
1053 return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
1054 }
1055
1056 MBEDTLS_PUT_UINT16_BE(*sig_alg, p, 0);
1057 MBEDTLS_PUT_UINT16_BE(signature_len, p, 2);
1058
1059 *out_len = 4 + signature_len;
1060
1061 return 0;
1062}
1063
1064int mbedtls_ssl_tls13_write_certificate_verify(mbedtls_ssl_context *ssl)
1065{
1066 int ret = 0;
1067 unsigned char *buf;
1068 size_t buf_len, msg_len;
1069
1070 MBEDTLS_SSL_DEBUG_MSG(2, ("=> write certificate verify"));
1071
1072 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_start_handshake_msg(
1073 ssl, MBEDTLS_SSL_HS_CERTIFICATE_VERIFY,
1074 &buf, &buf_len));
1075
1076 MBEDTLS_SSL_PROC_CHK(ssl_tls13_write_certificate_verify_body(
1077 ssl, buf, buf + buf_len, &msg_len));
1078
1079 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_add_hs_msg_to_checksum(
1080 ssl, MBEDTLS_SSL_HS_CERTIFICATE_VERIFY,
1081 buf, msg_len));
1082
1083 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_finish_handshake_msg(
1084 ssl, buf_len, msg_len));
1085
1086cleanup:
1087
1088 MBEDTLS_SSL_DEBUG_MSG(2, ("<= write certificate verify"));
1089 return ret;
1090}
1091
1092#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED */
1093
1094/*
1095 *
1096 * STATE HANDLING: Incoming Finished message.
1097 */
1098/*
1099 * Implementation
1100 */
1101
1102MBEDTLS_CHECK_RETURN_CRITICAL
1103static int ssl_tls13_preprocess_finished_message(mbedtls_ssl_context *ssl)
1104{
1105 int ret;
1106
1107 ret = mbedtls_ssl_tls13_calculate_verify_data(
1108 ssl,
1109 ssl->handshake->state_local.finished_in.digest,
1110 sizeof(ssl->handshake->state_local.finished_in.digest),
1111 &ssl->handshake->state_local.finished_in.digest_len,
1112 ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT ?
1113 MBEDTLS_SSL_IS_SERVER : MBEDTLS_SSL_IS_CLIENT);
1114 if (ret != 0) {
1115 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_tls13_calculate_verify_data", ret);
1116 return ret;
1117 }
1118
1119 return 0;
1120}
1121
1122MBEDTLS_CHECK_RETURN_CRITICAL
1123static int ssl_tls13_parse_finished_message(mbedtls_ssl_context *ssl,
1124 const unsigned char *buf,
1125 const unsigned char *end)
1126{
1127 /*
1128 * struct {
1129 * opaque verify_data[Hash.length];
1130 * } Finished;
1131 */
1132 const unsigned char *expected_verify_data =
1133 ssl->handshake->state_local.finished_in.digest;
1134 size_t expected_verify_data_len =
1135 ssl->handshake->state_local.finished_in.digest_len;
1136 /* Structural validation */
1137 if ((size_t) (end - buf) != expected_verify_data_len) {
1138 MBEDTLS_SSL_DEBUG_MSG(1, ("bad finished message"));
1139
1140 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR,
1141 MBEDTLS_ERR_SSL_DECODE_ERROR);
1142 return MBEDTLS_ERR_SSL_DECODE_ERROR;
1143 }
1144
1145 MBEDTLS_SSL_DEBUG_BUF(4, "verify_data (self-computed):",
1146 expected_verify_data,
1147 expected_verify_data_len);
1148 MBEDTLS_SSL_DEBUG_BUF(4, "verify_data (received message):", buf,
1149 expected_verify_data_len);
1150
1151 /* Semantic validation */
1152 if (mbedtls_ct_memcmp(buf,
1153 expected_verify_data,
1154 expected_verify_data_len) != 0) {
1155 MBEDTLS_SSL_DEBUG_MSG(1, ("bad finished message"));
1156
1157 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_DECRYPT_ERROR,
1158 MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE);
1159 return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
1160 }
1161 return 0;
1162}
1163
1164int mbedtls_ssl_tls13_process_finished_message(mbedtls_ssl_context *ssl)
1165{
1166 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1167 unsigned char *buf;
1168 size_t buf_len;
1169
1170 MBEDTLS_SSL_DEBUG_MSG(2, ("=> parse finished message"));
1171
1172 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_tls13_fetch_handshake_msg(
1173 ssl, MBEDTLS_SSL_HS_FINISHED, &buf, &buf_len));
1174
1175 /* Preprocessing step: Compute handshake digest */
1176 MBEDTLS_SSL_PROC_CHK(ssl_tls13_preprocess_finished_message(ssl));
1177
1178 MBEDTLS_SSL_PROC_CHK(ssl_tls13_parse_finished_message(
1179 ssl, buf, buf + buf_len));
1180
1181 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_add_hs_msg_to_checksum(
1182 ssl, MBEDTLS_SSL_HS_FINISHED, buf, buf_len));
1183
1184cleanup:
1185
1186 MBEDTLS_SSL_DEBUG_MSG(2, ("<= parse finished message"));
1187 return ret;
1188}
1189
1190/*
1191 *
1192 * STATE HANDLING: Write and send Finished message.
1193 *
1194 */
1195/*
1196 * Implement
1197 */
1198
1199MBEDTLS_CHECK_RETURN_CRITICAL
1200static int ssl_tls13_prepare_finished_message(mbedtls_ssl_context *ssl)
1201{
1202 int ret;
1203
1204 /* Compute transcript of handshake up to now. */
1205 ret = mbedtls_ssl_tls13_calculate_verify_data(ssl,
1206 ssl->handshake->state_local.finished_out.digest,
1207 sizeof(ssl->handshake->state_local.finished_out.
1208 digest),
1209 &ssl->handshake->state_local.finished_out.digest_len,
1210 ssl->conf->endpoint);
1211
1212 if (ret != 0) {
1213 MBEDTLS_SSL_DEBUG_RET(1, "calculate_verify_data failed", ret);
1214 return ret;
1215 }
1216
1217 return 0;
1218}
1219
1220MBEDTLS_CHECK_RETURN_CRITICAL
1221static int ssl_tls13_write_finished_message_body(mbedtls_ssl_context *ssl,
1222 unsigned char *buf,
1223 unsigned char *end,
1224 size_t *out_len)
1225{
1226 size_t verify_data_len = ssl->handshake->state_local.finished_out.digest_len;
1227 /*
1228 * struct {
1229 * opaque verify_data[Hash.length];
1230 * } Finished;
1231 */
1232 MBEDTLS_SSL_CHK_BUF_PTR(buf, end, verify_data_len);
1233
1234 memcpy(buf, ssl->handshake->state_local.finished_out.digest,
1235 verify_data_len);
1236
1237 *out_len = verify_data_len;
1238 return 0;
1239}
1240
1241/* Main entry point: orchestrates the other functions */
1242int mbedtls_ssl_tls13_write_finished_message(mbedtls_ssl_context *ssl)
1243{
1244 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1245 unsigned char *buf;
1246 size_t buf_len, msg_len;
1247
1248 MBEDTLS_SSL_DEBUG_MSG(2, ("=> write finished message"));
1249
1250 MBEDTLS_SSL_PROC_CHK(ssl_tls13_prepare_finished_message(ssl));
1251
1252 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_start_handshake_msg(ssl,
1253 MBEDTLS_SSL_HS_FINISHED, &buf, &buf_len));
1254
1255 MBEDTLS_SSL_PROC_CHK(ssl_tls13_write_finished_message_body(
1256 ssl, buf, buf + buf_len, &msg_len));
1257
1258 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_add_hs_msg_to_checksum(ssl,
1259 MBEDTLS_SSL_HS_FINISHED, buf, msg_len));
1260
1261 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_finish_handshake_msg(
1262 ssl, buf_len, msg_len));
1263cleanup:
1264
1265 MBEDTLS_SSL_DEBUG_MSG(2, ("<= write finished message"));
1266 return ret;
1267}
1268
1269void mbedtls_ssl_tls13_handshake_wrapup(mbedtls_ssl_context *ssl)
1270{
1271
1272 MBEDTLS_SSL_DEBUG_MSG(3, ("=> handshake wrapup"));
1273
1274 MBEDTLS_SSL_DEBUG_MSG(1, ("Switch to application keys for inbound traffic"));
1275 mbedtls_ssl_set_inbound_transform(ssl, ssl->transform_application);
1276
1277 MBEDTLS_SSL_DEBUG_MSG(1, ("Switch to application keys for outbound traffic"));
1278 mbedtls_ssl_set_outbound_transform(ssl, ssl->transform_application);
1279
1280 /*
1281 * Free the previous session and switch to the current one.
1282 */
1283 if (ssl->session) {
1284 mbedtls_ssl_session_free(ssl->session);
1285 mbedtls_free(ssl->session);
1286 }
1287 ssl->session = ssl->session_negotiate;
1288 ssl->session_negotiate = NULL;
1289
1290 MBEDTLS_SSL_DEBUG_MSG(3, ("<= handshake wrapup"));
1291}
1292
1293/*
1294 *
1295 * STATE HANDLING: Write ChangeCipherSpec
1296 *
1297 */
1298#if defined(MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE)
1299MBEDTLS_CHECK_RETURN_CRITICAL
1300static int ssl_tls13_write_change_cipher_spec_body(mbedtls_ssl_context *ssl,
1301 unsigned char *buf,
1302 unsigned char *end,
1303 size_t *olen)
1304{
1305 ((void) ssl);
1306
1307 MBEDTLS_SSL_CHK_BUF_PTR(buf, end, 1);
1308 buf[0] = 1;
1309 *olen = 1;
1310
1311 return 0;
1312}
1313
1314int mbedtls_ssl_tls13_write_change_cipher_spec(mbedtls_ssl_context *ssl)
1315{
1316 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1317
1318 MBEDTLS_SSL_DEBUG_MSG(2, ("=> write change cipher spec"));
1319
1320 /* Only one CCS to send. */
1321 if (ssl->handshake->ccs_sent) {
1322 ret = 0;
1323 goto cleanup;
1324 }
1325
1326 /* Write CCS message */
1327 MBEDTLS_SSL_PROC_CHK(ssl_tls13_write_change_cipher_spec_body(
1328 ssl, ssl->out_msg,
1329 ssl->out_msg + MBEDTLS_SSL_OUT_CONTENT_LEN,
1330 &ssl->out_msglen));
1331
1332 ssl->out_msgtype = MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC;
1333
1334 /* Dispatch message */
1335 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_write_record(ssl, 0));
1336
1337 ssl->handshake->ccs_sent = 1;
1338
1339cleanup:
1340
1341 MBEDTLS_SSL_DEBUG_MSG(2, ("<= write change cipher spec"));
1342 return ret;
1343}
1344
1345#endif /* MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE */
1346
1347/* Early Data Indication Extension
1348 *
1349 * struct {
1350 * select ( Handshake.msg_type ) {
1351 * case new_session_ticket: uint32 max_early_data_size;
1352 * case client_hello: Empty;
1353 * case encrypted_extensions: Empty;
1354 * };
1355 * } EarlyDataIndication;
1356 */
1357#if defined(MBEDTLS_SSL_EARLY_DATA)
1358int mbedtls_ssl_tls13_write_early_data_ext(mbedtls_ssl_context *ssl,
1359 int in_new_session_ticket,
1360 unsigned char *buf,
1361 const unsigned char *end,
1362 size_t *out_len)
1363{
1364 unsigned char *p = buf;
1365
1366#if defined(MBEDTLS_SSL_SRV_C)
1367 const size_t needed = in_new_session_ticket ? 8 : 4;
1368#else
1369 const size_t needed = 4;
1370 ((void) in_new_session_ticket);
1371#endif
1372
1373 *out_len = 0;
1374
1375 MBEDTLS_SSL_CHK_BUF_PTR(p, end, needed);
1376
1377 MBEDTLS_PUT_UINT16_BE(MBEDTLS_TLS_EXT_EARLY_DATA, p, 0);
1378 MBEDTLS_PUT_UINT16_BE(needed - 4, p, 2);
1379
1380#if defined(MBEDTLS_SSL_SRV_C)
1381 if (in_new_session_ticket) {
1382 MBEDTLS_PUT_UINT32_BE(ssl->conf->max_early_data_size, p, 4);
1383 MBEDTLS_SSL_DEBUG_MSG(
1384 4, ("Sent max_early_data_size=%u",
1385 (unsigned int) ssl->conf->max_early_data_size));
1386 }
1387#endif
1388
1389 *out_len = needed;
1390
1391 mbedtls_ssl_tls13_set_hs_sent_ext_mask(ssl, MBEDTLS_TLS_EXT_EARLY_DATA);
1392
1393 return 0;
1394}
1395
1396#if defined(MBEDTLS_SSL_SRV_C)
1397int mbedtls_ssl_tls13_check_early_data_len(mbedtls_ssl_context *ssl,
1398 size_t early_data_len)
1399{
1400 /*
1401 * This function should be called only while an handshake is in progress
1402 * and thus a session under negotiation. Add a sanity check to detect a
1403 * misuse.
1404 */
1405 if (ssl->session_negotiate == NULL) {
1406 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
1407 }
1408
1409 /* RFC 8446 section 4.6.1
1410 *
1411 * A server receiving more than max_early_data_size bytes of 0-RTT data
1412 * SHOULD terminate the connection with an "unexpected_message" alert.
1413 * Note that if it is still possible to send early_data_len bytes of early
1414 * data, it means that early_data_len is smaller than max_early_data_size
1415 * (type uint32_t) and can fit in an uint32_t. We use this further
1416 * down.
1417 */
1418 if (early_data_len >
1419 (ssl->session_negotiate->max_early_data_size -
1420 ssl->total_early_data_size)) {
1421
1422 MBEDTLS_SSL_DEBUG_MSG(
1423 2, ("EarlyData: Too much early data received, "
1424 "%lu + %" MBEDTLS_PRINTF_SIZET " > %lu",
1425 (unsigned long) ssl->total_early_data_size,
1426 early_data_len,
1427 (unsigned long) ssl->session_negotiate->max_early_data_size));
1428
1429 MBEDTLS_SSL_PEND_FATAL_ALERT(
1430 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE,
1431 MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE);
1432 return MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE;
1433 }
1434
1435 /*
1436 * early_data_len has been checked to be less than max_early_data_size
1437 * that is uint32_t. Its cast to an uint32_t below is thus safe. We need
1438 * the cast to appease some compilers.
1439 */
1440 ssl->total_early_data_size += (uint32_t) early_data_len;
1441
1442 return 0;
1443}
1444#endif /* MBEDTLS_SSL_SRV_C */
1445#endif /* MBEDTLS_SSL_EARLY_DATA */
1446
1447/* Reset SSL context and update hash for handling HRR.
1448 *
1449 * Replace Transcript-Hash(X) by
1450 * Transcript-Hash( message_hash ||
1451 * 00 00 Hash.length ||
1452 * X )
1453 * A few states of the handshake are preserved, including:
1454 * - session ID
1455 * - session ticket
1456 * - negotiated ciphersuite
1457 */
1458int mbedtls_ssl_reset_transcript_for_hrr(mbedtls_ssl_context *ssl)
1459{
1460 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1461 unsigned char hash_transcript[PSA_HASH_MAX_SIZE + 4];
1462 size_t hash_len;
1463 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
1464 ssl->handshake->ciphersuite_info;
1465
1466 MBEDTLS_SSL_DEBUG_MSG(3, ("Reset SSL session for HRR"));
1467
1468 ret = mbedtls_ssl_get_handshake_transcript(ssl, (mbedtls_md_type_t) ciphersuite_info->mac,
1469 hash_transcript + 4,
1470 PSA_HASH_MAX_SIZE,
1471 &hash_len);
1472 if (ret != 0) {
1473 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_get_handshake_transcript", ret);
1474 return ret;
1475 }
1476
1477 hash_transcript[0] = MBEDTLS_SSL_HS_MESSAGE_HASH;
1478 hash_transcript[1] = 0;
1479 hash_transcript[2] = 0;
1480 hash_transcript[3] = (unsigned char) hash_len;
1481
1482 hash_len += 4;
1483
1484 MBEDTLS_SSL_DEBUG_BUF(4, "Truncated handshake transcript",
1485 hash_transcript, hash_len);
1486
1487 /* Reset running hash and replace it with a hash of the transcript */
1488 ret = mbedtls_ssl_reset_checksum(ssl);
1489 if (ret != 0) {
1490 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_reset_checksum", ret);
1491 return ret;
1492 }
1493 ret = ssl->handshake->update_checksum(ssl, hash_transcript, hash_len);
1494 if (ret != 0) {
1495 MBEDTLS_SSL_DEBUG_RET(1, "update_checksum", ret);
1496 return ret;
1497 }
1498
1499 return ret;
1500}
1501
1502#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_EPHEMERAL_ENABLED)
1503
1504int mbedtls_ssl_tls13_read_public_xxdhe_share(mbedtls_ssl_context *ssl,
1505 const unsigned char *buf,
1506 size_t buf_len)
1507{
1508 uint8_t *p = (uint8_t *) buf;
1509 const uint8_t *end = buf + buf_len;
1510 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
1511
1512 /* Get size of the TLS opaque key_exchange field of the KeyShareEntry struct. */
1513 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, 2);
1514 uint16_t peerkey_len = MBEDTLS_GET_UINT16_BE(p, 0);
1515 p += 2;
1516
1517 /* Check if key size is consistent with given buffer length. */
1518 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, peerkey_len);
1519
1520 /* Store peer's ECDH/FFDH public key. */
1521 if (peerkey_len > sizeof(handshake->xxdh_psa_peerkey)) {
1522 MBEDTLS_SSL_DEBUG_MSG(1, ("Invalid public key length: %u > %" MBEDTLS_PRINTF_SIZET,
1523 (unsigned) peerkey_len,
1524 sizeof(handshake->xxdh_psa_peerkey)));
1525 return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
1526 }
1527 memcpy(handshake->xxdh_psa_peerkey, p, peerkey_len);
1528 handshake->xxdh_psa_peerkey_len = peerkey_len;
1529
1530 return 0;
1531}
1532
1533#if defined(PSA_WANT_ALG_FFDH)
1534static psa_status_t mbedtls_ssl_get_psa_ffdh_info_from_tls_id(
1535 uint16_t tls_id, size_t *bits, psa_key_type_t *key_type)
1536{
1537 switch (tls_id) {
1538#if defined(PSA_WANT_DH_RFC7919_2048)
1539 case MBEDTLS_SSL_IANA_TLS_GROUP_FFDHE2048:
1540 *bits = 2048;
1541 *key_type = PSA_KEY_TYPE_DH_KEY_PAIR(PSA_DH_FAMILY_RFC7919);
1542 return PSA_SUCCESS;
1543#endif /* PSA_WANT_DH_RFC7919_2048 */
1544#if defined(PSA_WANT_DH_RFC7919_3072)
1545 case MBEDTLS_SSL_IANA_TLS_GROUP_FFDHE3072:
1546 *bits = 3072;
1547 *key_type = PSA_KEY_TYPE_DH_KEY_PAIR(PSA_DH_FAMILY_RFC7919);
1548 return PSA_SUCCESS;
1549#endif /* PSA_WANT_DH_RFC7919_3072 */
1550#if defined(PSA_WANT_DH_RFC7919_4096)
1551 case MBEDTLS_SSL_IANA_TLS_GROUP_FFDHE4096:
1552 *bits = 4096;
1553 *key_type = PSA_KEY_TYPE_DH_KEY_PAIR(PSA_DH_FAMILY_RFC7919);
1554 return PSA_SUCCESS;
1555#endif /* PSA_WANT_DH_RFC7919_4096 */
1556#if defined(PSA_WANT_DH_RFC7919_6144)
1557 case MBEDTLS_SSL_IANA_TLS_GROUP_FFDHE6144:
1558 *bits = 6144;
1559 *key_type = PSA_KEY_TYPE_DH_KEY_PAIR(PSA_DH_FAMILY_RFC7919);
1560 return PSA_SUCCESS;
1561#endif /* PSA_WANT_DH_RFC7919_6144 */
1562#if defined(PSA_WANT_DH_RFC7919_8192)
1563 case MBEDTLS_SSL_IANA_TLS_GROUP_FFDHE8192:
1564 *bits = 8192;
1565 *key_type = PSA_KEY_TYPE_DH_KEY_PAIR(PSA_DH_FAMILY_RFC7919);
1566 return PSA_SUCCESS;
1567#endif /* PSA_WANT_DH_RFC7919_8192 */
1568 default:
1569 return PSA_ERROR_NOT_SUPPORTED;
1570 }
1571}
1572#endif /* PSA_WANT_ALG_FFDH */
1573
1574int mbedtls_ssl_tls13_generate_and_write_xxdh_key_exchange(
1575 mbedtls_ssl_context *ssl,
1576 uint16_t named_group,
1577 unsigned char *buf,
1578 unsigned char *end,
1579 size_t *out_len)
1580{
1581 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
1582 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
1583 psa_key_attributes_t key_attributes;
1584 size_t own_pubkey_len;
1585 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
1586 size_t bits = 0;
1587 psa_key_type_t key_type = PSA_KEY_TYPE_NONE;
1588 psa_algorithm_t alg = PSA_ALG_NONE;
1589 size_t buf_size = (size_t) (end - buf);
1590
1591 MBEDTLS_SSL_DEBUG_MSG(1, ("Perform PSA-based ECDH/FFDH computation."));
1592
1593 /* Convert EC's TLS ID to PSA key type. */
1594#if defined(PSA_WANT_ALG_ECDH)
1595 if (mbedtls_ssl_get_psa_curve_info_from_tls_id(
1596 named_group, &key_type, &bits) == PSA_SUCCESS) {
1597 alg = PSA_ALG_ECDH;
1598 }
1599#endif
1600#if defined(PSA_WANT_ALG_FFDH)
1601 if (mbedtls_ssl_get_psa_ffdh_info_from_tls_id(named_group, &bits,
1602 &key_type) == PSA_SUCCESS) {
1603 alg = PSA_ALG_FFDH;
1604 }
1605#endif
1606
1607 if (key_type == PSA_KEY_TYPE_NONE) {
1608 return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
1609 }
1610
1611 if (buf_size < PSA_BITS_TO_BYTES(bits)) {
1612 return MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL;
1613 }
1614
1615 handshake->xxdh_psa_type = key_type;
1616 ssl->handshake->xxdh_psa_bits = bits;
1617
1618 key_attributes = psa_key_attributes_init();
1619 psa_set_key_usage_flags(&key_attributes, PSA_KEY_USAGE_DERIVE);
1620 psa_set_key_algorithm(&key_attributes, alg);
1621 psa_set_key_type(&key_attributes, handshake->xxdh_psa_type);
1622 psa_set_key_bits(&key_attributes, handshake->xxdh_psa_bits);
1623
1624 /* Generate ECDH/FFDH private key. */
1625 status = psa_generate_key(&key_attributes,
1626 &handshake->xxdh_psa_privkey);
1627 if (status != PSA_SUCCESS) {
1628 ret = PSA_TO_MBEDTLS_ERR(status);
1629 MBEDTLS_SSL_DEBUG_RET(1, "psa_generate_key", ret);
1630 return ret;
1631
1632 }
1633
1634 /* Export the public part of the ECDH/FFDH private key from PSA. */
1635 status = psa_export_public_key(handshake->xxdh_psa_privkey,
1636 buf, buf_size,
1637 &own_pubkey_len);
1638
1639 if (status != PSA_SUCCESS) {
1640 ret = PSA_TO_MBEDTLS_ERR(status);
1641 MBEDTLS_SSL_DEBUG_RET(1, "psa_export_public_key", ret);
1642 return ret;
1643 }
1644
1645 *out_len = own_pubkey_len;
1646
1647 return 0;
1648}
1649#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_EPHEMERAL_ENABLED */
1650
1651/* RFC 8446 section 4.2
1652 *
1653 * If an implementation receives an extension which it recognizes and which is
1654 * not specified for the message in which it appears, it MUST abort the handshake
1655 * with an "illegal_parameter" alert.
1656 *
1657 */
1658int mbedtls_ssl_tls13_check_received_extension(
1659 mbedtls_ssl_context *ssl,
1660 int hs_msg_type,
1661 unsigned int received_extension_type,
1662 uint32_t hs_msg_allowed_extensions_mask)
1663{
1664 uint32_t extension_mask = mbedtls_ssl_get_extension_mask(
1665 received_extension_type);
1666
1667 MBEDTLS_SSL_PRINT_EXT(
1668 3, hs_msg_type, received_extension_type, "received");
1669
1670 if ((extension_mask & hs_msg_allowed_extensions_mask) == 0) {
1671 MBEDTLS_SSL_PRINT_EXT(
1672 3, hs_msg_type, received_extension_type, "is illegal");
1673 MBEDTLS_SSL_PEND_FATAL_ALERT(
1674 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER,
1675 MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER);
1676 return MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER;
1677 }
1678
1679 ssl->handshake->received_extensions |= extension_mask;
1680 /*
1681 * If it is a message containing extension responses, check that we
1682 * previously sent the extension.
1683 */
1684 switch (hs_msg_type) {
1685 case MBEDTLS_SSL_HS_SERVER_HELLO:
1686 case MBEDTLS_SSL_TLS1_3_HS_HELLO_RETRY_REQUEST:
1687 case MBEDTLS_SSL_HS_ENCRYPTED_EXTENSIONS:
1688 case MBEDTLS_SSL_HS_CERTIFICATE:
1689 /* Check if the received extension is sent by peer message.*/
1690 if ((ssl->handshake->sent_extensions & extension_mask) != 0) {
1691 return 0;
1692 }
1693 break;
1694 default:
1695 return 0;
1696 }
1697
1698 MBEDTLS_SSL_PRINT_EXT(
1699 3, hs_msg_type, received_extension_type, "is unsupported");
1700 MBEDTLS_SSL_PEND_FATAL_ALERT(
1701 MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_EXT,
1702 MBEDTLS_ERR_SSL_UNSUPPORTED_EXTENSION);
1703 return MBEDTLS_ERR_SSL_UNSUPPORTED_EXTENSION;
1704}
1705
1706#if defined(MBEDTLS_SSL_RECORD_SIZE_LIMIT)
1707
1708/* RFC 8449, section 4:
1709 *
1710 * The ExtensionData of the "record_size_limit" extension is
1711 * RecordSizeLimit:
1712 * uint16 RecordSizeLimit;
1713 */
1714MBEDTLS_CHECK_RETURN_CRITICAL
1715int mbedtls_ssl_tls13_parse_record_size_limit_ext(mbedtls_ssl_context *ssl,
1716 const unsigned char *buf,
1717 const unsigned char *end)
1718{
1719 const unsigned char *p = buf;
1720 uint16_t record_size_limit;
1721 const size_t extension_data_len = end - buf;
1722
1723 if (extension_data_len !=
1724 MBEDTLS_SSL_RECORD_SIZE_LIMIT_EXTENSION_DATA_LENGTH) {
1725 MBEDTLS_SSL_DEBUG_MSG(2,
1726 ("record_size_limit extension has invalid length: %"
1727 MBEDTLS_PRINTF_SIZET " Bytes",
1728 extension_data_len));
1729
1730 MBEDTLS_SSL_PEND_FATAL_ALERT(
1731 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER,
1732 MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER);
1733 return MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER;
1734 }
1735
1736 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, 2);
1737 record_size_limit = MBEDTLS_GET_UINT16_BE(p, 0);
1738
1739 MBEDTLS_SSL_DEBUG_MSG(2, ("RecordSizeLimit: %u Bytes", record_size_limit));
1740
1741 /* RFC 8449, section 4:
1742 *
1743 * Endpoints MUST NOT send a "record_size_limit" extension with a value
1744 * smaller than 64. An endpoint MUST treat receipt of a smaller value
1745 * as a fatal error and generate an "illegal_parameter" alert.
1746 */
1747 if (record_size_limit < MBEDTLS_SSL_RECORD_SIZE_LIMIT_MIN) {
1748 MBEDTLS_SSL_DEBUG_MSG(1, ("Invalid record size limit : %u Bytes",
1749 record_size_limit));
1750 MBEDTLS_SSL_PEND_FATAL_ALERT(
1751 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER,
1752 MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER);
1753 return MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER;
1754 }
1755
1756 ssl->session_negotiate->record_size_limit = record_size_limit;
1757
1758 return 0;
1759}
1760
1761MBEDTLS_CHECK_RETURN_CRITICAL
1762int mbedtls_ssl_tls13_write_record_size_limit_ext(mbedtls_ssl_context *ssl,
1763 unsigned char *buf,
1764 const unsigned char *end,
1765 size_t *out_len)
1766{
1767 unsigned char *p = buf;
1768 *out_len = 0;
1769
1770 MBEDTLS_STATIC_ASSERT(MBEDTLS_SSL_IN_CONTENT_LEN >= MBEDTLS_SSL_RECORD_SIZE_LIMIT_MIN,
1771 "MBEDTLS_SSL_IN_CONTENT_LEN is less than the "
1772 "minimum record size limit");
1773
1774 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 6);
1775
1776 MBEDTLS_PUT_UINT16_BE(MBEDTLS_TLS_EXT_RECORD_SIZE_LIMIT, p, 0);
1777 MBEDTLS_PUT_UINT16_BE(MBEDTLS_SSL_RECORD_SIZE_LIMIT_EXTENSION_DATA_LENGTH,
1778 p, 2);
1779 MBEDTLS_PUT_UINT16_BE(MBEDTLS_SSL_IN_CONTENT_LEN, p, 4);
1780
1781 *out_len = 6;
1782
1783 MBEDTLS_SSL_DEBUG_MSG(2, ("Sent RecordSizeLimit: %d Bytes",
1784 MBEDTLS_SSL_IN_CONTENT_LEN));
1785
1786 mbedtls_ssl_tls13_set_hs_sent_ext_mask(ssl, MBEDTLS_TLS_EXT_RECORD_SIZE_LIMIT);
1787
1788 return 0;
1789}
1790
1791#endif /* MBEDTLS_SSL_RECORD_SIZE_LIMIT */
1792
1793#endif /* MBEDTLS_SSL_TLS_C && MBEDTLS_SSL_PROTO_TLS1_3 */