blob: b6d09788ba05f48e4a3b4718b36dac49587d746b [file] [log] [blame]
Jerry Yu65dd2cc2021-08-18 16:38:40 +08001/*
2 * TLS 1.3 functionality shared between client and server
3 *
4 * Copyright The Mbed TLS Contributors
Dave Rodgman16799db2023-11-02 19:47:20 +00005 * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
Jerry Yu65dd2cc2021-08-18 16:38:40 +08006 */
7
8#include "common.h"
9
Jerry Yufb4b6472022-01-27 15:03:26 +080010#if defined(MBEDTLS_SSL_TLS_C) && defined(MBEDTLS_SSL_PROTO_TLS1_3)
Jerry Yu65dd2cc2021-08-18 16:38:40 +080011
Jerry Yu30b071c2021-09-12 20:16:03 +080012#include <string.h>
13
Jerry Yuc8a392c2021-08-18 16:46:28 +080014#include "mbedtls/error.h"
Valerio Settib4f50762024-01-17 10:24:52 +010015#include "debug_internal.h"
Jerry Yu30b071c2021-09-12 20:16:03 +080016#include "mbedtls/oid.h"
17#include "mbedtls/platform.h"
Gabor Mezei685472b2021-11-24 11:17:36 +010018#include "mbedtls/constant_time.h"
Jerry Yu141bbe72022-12-01 20:30:41 +080019#include "psa/crypto.h"
Valerio Setti384fbde2024-01-02 13:26:40 +010020#include "mbedtls/psa_util.h"
Jerry Yuc8a392c2021-08-18 16:46:28 +080021
Jerry Yu65dd2cc2021-08-18 16:38:40 +080022#include "ssl_misc.h"
Ronald Crone3dac4a2022-06-10 17:21:51 +020023#include "ssl_tls13_invasive.h"
Jerry Yu30b071c2021-09-12 20:16:03 +080024#include "ssl_tls13_keys.h"
Jerry Yu67eced02022-02-25 13:37:36 +080025#include "ssl_debug_helpers.h"
Jerry Yu65dd2cc2021-08-18 16:38:40 +080026
Andrzej Kurek8a045ce2022-12-23 11:00:06 -050027#include "psa/crypto.h"
Manuel Pégourié-Gonnard2be8c632023-06-07 13:07:21 +020028#include "psa_util_internal.h"
Andrzej Kurek8a045ce2022-12-23 11:00:06 -050029
Andrzej Kurek00644842023-05-30 05:45:00 -040030/* 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,
Andrzej Kurek1e4a0302023-05-30 09:45:17 -040035 ARRAY_LENGTH(psa_to_ssl_errors),
Andrzej Kurek00644842023-05-30 05:45:00 -040036 psa_generic_status_to_mbedtls);
37}
38#define PSA_TO_MBEDTLS_ERR(status) local_err_translation(status)
Andrzej Kurek8a045ce2022-12-23 11:00:06 -050039
Gilles Peskine069bccd2024-08-23 21:55:24 +020040int 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
Jerry Yufbe3e642022-04-25 19:31:51 +080050const uint8_t mbedtls_ssl_tls13_hello_retry_request_magic[
Gilles Peskine449bd832023-01-11 14:50:10 +010051 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 };
Jerry Yu93a13f22022-04-11 23:00:01 +080056
Gilles Peskine449bd832023-01-11 14:50:10 +010057int mbedtls_ssl_tls13_fetch_handshake_msg(mbedtls_ssl_context *ssl,
58 unsigned hs_type,
59 unsigned char **buf,
60 size_t *buf_len)
XiaokangQian6b226b02021-09-24 07:51:16 +000061{
62 int ret;
63
Gilles Peskine449bd832023-01-11 14:50:10 +010064 if ((ret = mbedtls_ssl_read_record(ssl, 0)) != 0) {
65 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_read_record", ret);
XiaokangQian6b226b02021-09-24 07:51:16 +000066 goto cleanup;
67 }
68
Gilles Peskine449bd832023-01-11 14:50:10 +010069 if (ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE ||
70 ssl->in_msg[0] != hs_type) {
71 MBEDTLS_SSL_DEBUG_MSG(1, ("Receive unexpected handshake message."));
72 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE,
73 MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE);
XiaokangQian6b226b02021-09-24 07:51:16 +000074 ret = MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE;
75 goto cleanup;
76 }
77
XiaokangQian05420b12021-09-29 08:46:37 +000078 /*
79 * Jump handshake header (4 bytes, see Section 4 of RFC 8446).
80 * ...
81 * HandshakeType msg_type;
82 * uint24 length;
83 * ...
84 */
Xiaofei Baieef15042021-11-18 07:29:56 +000085 *buf = ssl->in_msg + 4;
86 *buf_len = ssl->in_hslen - 4;
XiaokangQian6b226b02021-09-24 07:51:16 +000087
XiaokangQian6b226b02021-09-24 07:51:16 +000088cleanup:
89
Gilles Peskine449bd832023-01-11 14:50:10 +010090 return ret;
XiaokangQian6b226b02021-09-24 07:51:16 +000091}
92
Ronald Cron47dce632023-02-08 17:38:29 +010093int mbedtls_ssl_tls13_is_supported_versions_ext_present_in_exts(
94 mbedtls_ssl_context *ssl,
95 const unsigned char *buf, const unsigned char *end,
Ronald Croneff56732023-04-03 17:36:31 +020096 const unsigned char **supported_versions_data,
97 const unsigned char **supported_versions_data_end)
Ronald Cron47dce632023-02-08 17:38:29 +010098{
99 const unsigned char *p = buf;
100 size_t extensions_len;
101 const unsigned char *extensions_end;
102
Ronald Croneff56732023-04-03 17:36:31 +0200103 *supported_versions_data = NULL;
104 *supported_versions_data_end = NULL;
Ronald Cron47dce632023-02-08 17:38:29 +0100105
106 /* Case of no extension */
107 if (p == end) {
108 return 0;
109 }
110
111 /* ...
112 * Extension extensions<x..2^16-1>;
113 * ...
114 * struct {
115 * ExtensionType extension_type; (2 bytes)
116 * opaque extension_data<0..2^16-1>;
117 * } Extension;
118 */
119 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, 2);
120 extensions_len = MBEDTLS_GET_UINT16_BE(p, 0);
121 p += 2;
122
123 /* Check extensions do not go beyond the buffer of data. */
124 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, extensions_len);
125 extensions_end = p + extensions_len;
126
127 while (p < extensions_end) {
128 unsigned int extension_type;
129 size_t extension_data_len;
130
131 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, extensions_end, 4);
132 extension_type = MBEDTLS_GET_UINT16_BE(p, 0);
133 extension_data_len = MBEDTLS_GET_UINT16_BE(p, 2);
134 p += 4;
135 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, extensions_end, extension_data_len);
136
137 if (extension_type == MBEDTLS_TLS_EXT_SUPPORTED_VERSIONS) {
Ronald Croneff56732023-04-03 17:36:31 +0200138 *supported_versions_data = p;
139 *supported_versions_data_end = p + extension_data_len;
Ronald Cron47dce632023-02-08 17:38:29 +0100140 return 1;
141 }
142 p += extension_data_len;
143 }
144
145 return 0;
146}
147
Ronald Cron928cbd32022-10-04 16:14:26 +0200148#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED)
Jerry Yu30b071c2021-09-12 20:16:03 +0800149/*
Jerry Yu30b071c2021-09-12 20:16:03 +0800150 * STATE HANDLING: Read CertificateVerify
151 */
Jerry Yud0fc5852021-10-29 11:09:06 +0800152/* Macro to express the maximum length of the verify structure.
Jerry Yu30b071c2021-09-12 20:16:03 +0800153 *
154 * The structure is computed per TLS 1.3 specification as:
155 * - 64 bytes of octet 32,
156 * - 33 bytes for the context string
157 * (which is either "TLS 1.3, client CertificateVerify"
158 * or "TLS 1.3, server CertificateVerify"),
Jerry Yud0fc5852021-10-29 11:09:06 +0800159 * - 1 byte for the octet 0x0, which serves as a separator,
Jerry Yu30b071c2021-09-12 20:16:03 +0800160 * - 32 or 48 bytes for the Transcript-Hash(Handshake Context, Certificate)
161 * (depending on the size of the transcript_hash)
162 *
163 * This results in a total size of
164 * - 130 bytes for a SHA256-based transcript hash, or
165 * (64 + 33 + 1 + 32 bytes)
166 * - 146 bytes for a SHA384-based transcript hash.
167 * (64 + 33 + 1 + 48 bytes)
168 *
169 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100170#define SSL_VERIFY_STRUCT_MAX_SIZE (64 + \
171 33 + \
172 1 + \
173 MBEDTLS_TLS1_3_MD_MAX_SIZE \
174 )
Jerry Yu30b071c2021-09-12 20:16:03 +0800175
Jerry Yu0b32c502021-10-28 13:41:59 +0800176/*
177 * The ssl_tls13_create_verify_structure() creates the verify structure.
178 * As input, it requires the transcript hash.
179 *
180 * The caller has to ensure that the buffer has size at least
181 * SSL_VERIFY_STRUCT_MAX_SIZE bytes.
182 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100183static void ssl_tls13_create_verify_structure(const unsigned char *transcript_hash,
184 size_t transcript_hash_len,
185 unsigned char *verify_buffer,
186 size_t *verify_buffer_len,
187 int from)
Jerry Yu0b32c502021-10-28 13:41:59 +0800188{
189 size_t idx;
Jerry Yu30b071c2021-09-12 20:16:03 +0800190
Jerry Yu0b32c502021-10-28 13:41:59 +0800191 /* RFC 8446, Section 4.4.3:
192 *
193 * The digital signature [in the CertificateVerify message] is then
194 * computed over the concatenation of:
195 * - A string that consists of octet 32 (0x20) repeated 64 times
196 * - The context string
197 * - A single 0 byte which serves as the separator
198 * - The content to be signed
199 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100200 memset(verify_buffer, 0x20, 64);
Jerry Yu0b32c502021-10-28 13:41:59 +0800201 idx = 64;
202
Gilles Peskine449bd832023-01-11 14:50:10 +0100203 if (from == MBEDTLS_SSL_IS_CLIENT) {
Tom Cosgroveb32d7ae2024-04-02 14:26:13 +0100204 memcpy(verify_buffer + idx, mbedtls_ssl_tls13_labels.client_cv,
205 MBEDTLS_SSL_TLS1_3_LBL_LEN(client_cv));
Gilles Peskine449bd832023-01-11 14:50:10 +0100206 idx += MBEDTLS_SSL_TLS1_3_LBL_LEN(client_cv);
207 } else { /* from == MBEDTLS_SSL_IS_SERVER */
Tom Cosgroveb32d7ae2024-04-02 14:26:13 +0100208 memcpy(verify_buffer + idx, mbedtls_ssl_tls13_labels.server_cv,
209 MBEDTLS_SSL_TLS1_3_LBL_LEN(server_cv));
Gilles Peskine449bd832023-01-11 14:50:10 +0100210 idx += MBEDTLS_SSL_TLS1_3_LBL_LEN(server_cv);
Jerry Yu0b32c502021-10-28 13:41:59 +0800211 }
212
213 verify_buffer[idx++] = 0x0;
214
Gilles Peskine449bd832023-01-11 14:50:10 +0100215 memcpy(verify_buffer + idx, transcript_hash, transcript_hash_len);
Jerry Yu0b32c502021-10-28 13:41:59 +0800216 idx += transcript_hash_len;
217
218 *verify_buffer_len = idx;
219}
220
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200221MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100222static int ssl_tls13_parse_certificate_verify(mbedtls_ssl_context *ssl,
223 const unsigned char *buf,
224 const unsigned char *end,
225 const unsigned char *verify_buffer,
226 size_t verify_buffer_len)
Jerry Yu30b071c2021-09-12 20:16:03 +0800227{
228 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
pespaceka1378102022-04-26 15:03:11 +0200229 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Jerry Yu30b071c2021-09-12 20:16:03 +0800230 const unsigned char *p = buf;
231 uint16_t algorithm;
Jerry Yu30b071c2021-09-12 20:16:03 +0800232 size_t signature_len;
233 mbedtls_pk_type_t sig_alg;
234 mbedtls_md_type_t md_alg;
pespaceka1378102022-04-26 15:03:11 +0200235 psa_algorithm_t hash_alg = PSA_ALG_NONE;
236 unsigned char verify_hash[PSA_HASH_MAX_SIZE];
Jerry Yu30b071c2021-09-12 20:16:03 +0800237 size_t verify_hash_len;
238
Xiaofei Baid25fab62021-12-02 06:36:27 +0000239 void const *options = NULL;
XiaokangQian82d34cc2021-11-03 08:51:56 +0000240#if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT)
Xiaofei Baid25fab62021-12-02 06:36:27 +0000241 mbedtls_pk_rsassa_pss_options rsassa_pss_options;
XiaokangQian82d34cc2021-11-03 08:51:56 +0000242#endif /* MBEDTLS_X509_RSASSA_PSS_SUPPORT */
243
Jerry Yu30b071c2021-09-12 20:16:03 +0800244 /*
245 * struct {
246 * SignatureScheme algorithm;
247 * opaque signature<0..2^16-1>;
248 * } CertificateVerify;
249 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100250 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, 2);
251 algorithm = MBEDTLS_GET_UINT16_BE(p, 0);
Jerry Yu30b071c2021-09-12 20:16:03 +0800252 p += 2;
253
254 /* RFC 8446 section 4.4.3
255 *
Xiaokang Qian73437382023-03-29 08:24:12 +0000256 * If the CertificateVerify message is sent by a server, the signature
257 * algorithm MUST be one offered in the client's "signature_algorithms"
258 * extension unless no valid certificate chain can be produced without
259 * unsupported algorithms
Jerry Yu30b071c2021-09-12 20:16:03 +0800260 *
261 * RFC 8446 section 4.4.2.2
262 *
263 * If the client cannot construct an acceptable chain using the provided
Xiaokang Qian73437382023-03-29 08:24:12 +0000264 * certificates and decides to abort the handshake, then it MUST abort the
265 * handshake with an appropriate certificate-related alert
266 * (by default, "unsupported_certificate").
Jerry Yu30b071c2021-09-12 20:16:03 +0800267 *
Jerry Yu6f87f252021-10-29 20:12:51 +0800268 * Check if algorithm is an offered signature algorithm.
Jerry Yu30b071c2021-09-12 20:16:03 +0800269 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100270 if (!mbedtls_ssl_sig_alg_is_offered(ssl, algorithm)) {
Jerry Yu982d9e52021-10-14 15:59:37 +0800271 /* algorithm not in offered signature algorithms list */
Gilles Peskine449bd832023-01-11 14:50:10 +0100272 MBEDTLS_SSL_DEBUG_MSG(1, ("Received signature algorithm(%04x) is not "
273 "offered.",
274 (unsigned int) algorithm));
Jerry Yu6f87f252021-10-29 20:12:51 +0800275 goto error;
Jerry Yu30b071c2021-09-12 20:16:03 +0800276 }
277
Gilles Peskine449bd832023-01-11 14:50:10 +0100278 if (mbedtls_ssl_get_pk_type_and_md_alg_from_sig_alg(
279 algorithm, &sig_alg, &md_alg) != 0) {
Jerry Yu8c338862022-03-23 13:34:04 +0800280 goto error;
Jerry Yu30b071c2021-09-12 20:16:03 +0800281 }
282
Manuel Pégourié-Gonnard2d6d9932023-03-28 11:38:08 +0200283 hash_alg = mbedtls_md_psa_alg_from_type(md_alg);
Gilles Peskine449bd832023-01-11 14:50:10 +0100284 if (hash_alg == 0) {
pespaceka1378102022-04-26 15:03:11 +0200285 goto error;
286 }
287
Gilles Peskine449bd832023-01-11 14:50:10 +0100288 MBEDTLS_SSL_DEBUG_MSG(3, ("Certificate Verify: Signature algorithm ( %04x )",
289 (unsigned int) algorithm));
Jerry Yu30b071c2021-09-12 20:16:03 +0800290
291 /*
292 * Check the certificate's key type matches the signature alg
293 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100294 if (!mbedtls_pk_can_do(&ssl->session_negotiate->peer_cert->pk, sig_alg)) {
295 MBEDTLS_SSL_DEBUG_MSG(1, ("signature algorithm doesn't match cert key"));
Jerry Yu6f87f252021-10-29 20:12:51 +0800296 goto error;
Jerry Yu30b071c2021-09-12 20:16:03 +0800297 }
298
Gilles Peskine449bd832023-01-11 14:50:10 +0100299 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, 2);
300 signature_len = MBEDTLS_GET_UINT16_BE(p, 0);
Jerry Yu30b071c2021-09-12 20:16:03 +0800301 p += 2;
Gilles Peskine449bd832023-01-11 14:50:10 +0100302 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, signature_len);
Jerry Yu30b071c2021-09-12 20:16:03 +0800303
Gilles Peskine449bd832023-01-11 14:50:10 +0100304 status = psa_hash_compute(hash_alg,
305 verify_buffer,
306 verify_buffer_len,
307 verify_hash,
308 sizeof(verify_hash),
309 &verify_hash_len);
310 if (status != PSA_SUCCESS) {
311 MBEDTLS_SSL_DEBUG_RET(1, "hash computation PSA error", status);
Jerry Yu6f87f252021-10-29 20:12:51 +0800312 goto error;
Jerry Yu133690c2021-10-25 14:01:13 +0800313 }
314
Gilles Peskine449bd832023-01-11 14:50:10 +0100315 MBEDTLS_SSL_DEBUG_BUF(3, "verify hash", verify_hash, verify_hash_len);
XiaokangQian82d34cc2021-11-03 08:51:56 +0000316#if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT)
Gilles Peskine449bd832023-01-11 14:50:10 +0100317 if (sig_alg == MBEDTLS_PK_RSASSA_PSS) {
Xiaofei Baid25fab62021-12-02 06:36:27 +0000318 rsassa_pss_options.mgf1_hash_id = md_alg;
Przemek Stekiel6a5e0182022-06-27 11:53:13 +0200319
Gilles Peskine449bd832023-01-11 14:50:10 +0100320 rsassa_pss_options.expected_salt_len = PSA_HASH_LENGTH(hash_alg);
321 options = (const void *) &rsassa_pss_options;
XiaokangQian82d34cc2021-11-03 08:51:56 +0000322 }
323#endif /* MBEDTLS_X509_RSASSA_PSS_SUPPORT */
Jerry Yu30b071c2021-09-12 20:16:03 +0800324
Gilles Peskine449bd832023-01-11 14:50:10 +0100325 if ((ret = mbedtls_pk_verify_ext(sig_alg, options,
326 &ssl->session_negotiate->peer_cert->pk,
327 md_alg, verify_hash, verify_hash_len,
328 p, signature_len)) == 0) {
329 return 0;
Jerry Yu30b071c2021-09-12 20:16:03 +0800330 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100331 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_pk_verify_ext", ret);
Jerry Yu30b071c2021-09-12 20:16:03 +0800332
Jerry Yu6f87f252021-10-29 20:12:51 +0800333error:
334 /* RFC 8446 section 4.4.3
335 *
336 * If the verification fails, the receiver MUST terminate the handshake
337 * with a "decrypt_error" alert.
Gilles Peskine449bd832023-01-11 14:50:10 +0100338 */
339 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_DECRYPT_ERROR,
340 MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE);
341 return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
Jerry Yu6f87f252021-10-29 20:12:51 +0800342
Jerry Yu30b071c2021-09-12 20:16:03 +0800343}
Ronald Cron928cbd32022-10-04 16:14:26 +0200344#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED */
Jerry Yu30b071c2021-09-12 20:16:03 +0800345
Gilles Peskine449bd832023-01-11 14:50:10 +0100346int mbedtls_ssl_tls13_process_certificate_verify(mbedtls_ssl_context *ssl)
Jerry Yu30b071c2021-09-12 20:16:03 +0800347{
Jerry Yu30b071c2021-09-12 20:16:03 +0800348
Ronald Cron928cbd32022-10-04 16:14:26 +0200349#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED)
Jerry Yuda8cdf22021-10-25 15:06:49 +0800350 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
351 unsigned char verify_buffer[SSL_VERIFY_STRUCT_MAX_SIZE];
352 size_t verify_buffer_len;
353 unsigned char transcript[MBEDTLS_TLS1_3_MD_MAX_SIZE];
354 size_t transcript_len;
355 unsigned char *buf;
356 size_t buf_len;
357
Gilles Peskine449bd832023-01-11 14:50:10 +0100358 MBEDTLS_SSL_DEBUG_MSG(2, ("=> parse certificate verify"));
Jerry Yu30b071c2021-09-12 20:16:03 +0800359
Jerry Yuda8cdf22021-10-25 15:06:49 +0800360 MBEDTLS_SSL_PROC_CHK(
Xiaokang Qian73437382023-03-29 08:24:12 +0000361 mbedtls_ssl_tls13_fetch_handshake_msg(
362 ssl, MBEDTLS_SSL_HS_CERTIFICATE_VERIFY, &buf, &buf_len));
Jerry Yu30b071c2021-09-12 20:16:03 +0800363
Jerry Yuda8cdf22021-10-25 15:06:49 +0800364 /* Need to calculate the hash of the transcript first
Jerry Yu0b32c502021-10-28 13:41:59 +0800365 * before reading the message since otherwise it gets
366 * included in the transcript
367 */
Xiaokang Qian73437382023-03-29 08:24:12 +0000368 ret = mbedtls_ssl_get_handshake_transcript(
369 ssl,
Dave Rodgman2eab4622023-10-05 13:30:37 +0100370 (mbedtls_md_type_t) ssl->handshake->ciphersuite_info->mac,
Xiaokang Qian73437382023-03-29 08:24:12 +0000371 transcript, sizeof(transcript),
372 &transcript_len);
Gilles Peskine449bd832023-01-11 14:50:10 +0100373 if (ret != 0) {
Jerry Yuda8cdf22021-10-25 15:06:49 +0800374 MBEDTLS_SSL_PEND_FATAL_ALERT(
375 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR,
Gilles Peskine449bd832023-01-11 14:50:10 +0100376 MBEDTLS_ERR_SSL_INTERNAL_ERROR);
377 return ret;
Jerry Yu30b071c2021-09-12 20:16:03 +0800378 }
379
Gilles Peskine449bd832023-01-11 14:50:10 +0100380 MBEDTLS_SSL_DEBUG_BUF(3, "handshake hash", transcript, transcript_len);
Jerry Yuda8cdf22021-10-25 15:06:49 +0800381
382 /* Create verify structure */
Gilles Peskine449bd832023-01-11 14:50:10 +0100383 ssl_tls13_create_verify_structure(transcript,
384 transcript_len,
385 verify_buffer,
386 &verify_buffer_len,
387 (ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT) ?
388 MBEDTLS_SSL_IS_SERVER :
389 MBEDTLS_SSL_IS_CLIENT);
Jerry Yuda8cdf22021-10-25 15:06:49 +0800390
391 /* Process the message contents */
Xiaokang Qian73437382023-03-29 08:24:12 +0000392 MBEDTLS_SSL_PROC_CHK(ssl_tls13_parse_certificate_verify(
393 ssl, buf, buf + buf_len,
394 verify_buffer, verify_buffer_len));
Jerry Yuda8cdf22021-10-25 15:06:49 +0800395
Xiaokang Qian73437382023-03-29 08:24:12 +0000396 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_add_hs_msg_to_checksum(
397 ssl, MBEDTLS_SSL_HS_CERTIFICATE_VERIFY,
398 buf, buf_len));
Jerry Yu30b071c2021-09-12 20:16:03 +0800399
400cleanup:
401
Gilles Peskine449bd832023-01-11 14:50:10 +0100402 MBEDTLS_SSL_DEBUG_MSG(2, ("<= parse certificate verify"));
403 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_tls13_process_certificate_verify", ret);
404 return ret;
Jerry Yuda8cdf22021-10-25 15:06:49 +0800405#else
406 ((void) ssl);
Gilles Peskine449bd832023-01-11 14:50:10 +0100407 MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
408 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Ronald Cron928cbd32022-10-04 16:14:26 +0200409#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED */
Jerry Yu30b071c2021-09-12 20:16:03 +0800410}
411
412/*
Xiaofei Bai947571e2021-09-29 09:12:03 +0000413 *
XiaokangQian6b916b12022-04-25 07:29:34 +0000414 * STATE HANDLING: Incoming Certificate.
Xiaofei Bai947571e2021-09-29 09:12:03 +0000415 *
416 */
417
Ronald Cronde08cf32022-10-04 17:15:35 +0200418#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED)
Xiaofei Bai947571e2021-09-29 09:12:03 +0000419#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
420/*
421 * Structure of Certificate message:
422 *
423 * enum {
424 * X509(0),
425 * RawPublicKey(2),
426 * (255)
427 * } CertificateType;
428 *
429 * struct {
430 * select (certificate_type) {
431 * case RawPublicKey:
432 * * From RFC 7250 ASN.1_subjectPublicKeyInfo *
433 * opaque ASN1_subjectPublicKeyInfo<1..2^24-1>;
434 * case X509:
435 * opaque cert_data<1..2^24-1>;
436 * };
437 * Extension extensions<0..2^16-1>;
438 * } CertificateEntry;
439 *
440 * struct {
441 * opaque certificate_request_context<0..2^8-1>;
442 * CertificateEntry certificate_list<0..2^24-1>;
443 * } Certificate;
444 *
445 */
Xiaofei Bai79595ac2021-10-26 07:16:45 +0000446
447/* Parse certificate chain send by the server. */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200448MBEDTLS_CHECK_RETURN_CRITICAL
Ronald Crone3dac4a2022-06-10 17:21:51 +0200449MBEDTLS_STATIC_TESTABLE
Gilles Peskine449bd832023-01-11 14:50:10 +0100450int mbedtls_ssl_tls13_parse_certificate(mbedtls_ssl_context *ssl,
451 const unsigned char *buf,
452 const unsigned char *end)
Xiaofei Bai947571e2021-09-29 09:12:03 +0000453{
454 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
455 size_t certificate_request_context_len = 0;
456 size_t certificate_list_len = 0;
457 const unsigned char *p = buf;
458 const unsigned char *certificate_list_end;
Jerry Yuc4bf5d62022-10-29 09:08:47 +0800459 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
Xiaofei Bai947571e2021-09-29 09:12:03 +0000460
Gilles Peskine449bd832023-01-11 14:50:10 +0100461 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, 4);
Xiaofei Bai947571e2021-09-29 09:12:03 +0000462 certificate_request_context_len = p[0];
Gilles Peskine449bd832023-01-11 14:50:10 +0100463 certificate_list_len = MBEDTLS_GET_UINT24_BE(p, 1);
XiaokangQian63e713e2022-05-15 04:26:57 +0000464 p += 4;
Xiaofei Bai947571e2021-09-29 09:12:03 +0000465
466 /* In theory, the certificate list can be up to 2^24 Bytes, but we don't
467 * support anything beyond 2^16 = 64K.
468 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100469 if ((certificate_request_context_len != 0) ||
470 (certificate_list_len >= 0x10000)) {
471 MBEDTLS_SSL_DEBUG_MSG(1, ("bad certificate message"));
472 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR,
473 MBEDTLS_ERR_SSL_DECODE_ERROR);
474 return MBEDTLS_ERR_SSL_DECODE_ERROR;
Xiaofei Bai947571e2021-09-29 09:12:03 +0000475 }
476
477 /* In case we tried to reuse a session but it failed */
Gilles Peskine449bd832023-01-11 14:50:10 +0100478 if (ssl->session_negotiate->peer_cert != NULL) {
479 mbedtls_x509_crt_free(ssl->session_negotiate->peer_cert);
480 mbedtls_free(ssl->session_negotiate->peer_cert);
Xiaofei Bai947571e2021-09-29 09:12:03 +0000481 }
482
Manuel Pégourié-Gonnard4d4c0c72024-08-12 10:36:40 +0200483 /* This is used by ssl_tls13_validate_certificate() */
Gilles Peskine449bd832023-01-11 14:50:10 +0100484 if (certificate_list_len == 0) {
XiaokangQianc3017f62022-05-13 05:55:41 +0000485 ssl->session_negotiate->peer_cert = NULL;
486 ret = 0;
487 goto exit;
488 }
489
Gilles Peskine449bd832023-01-11 14:50:10 +0100490 if ((ssl->session_negotiate->peer_cert =
491 mbedtls_calloc(1, sizeof(mbedtls_x509_crt))) == NULL) {
492 MBEDTLS_SSL_DEBUG_MSG(1, ("alloc( %" MBEDTLS_PRINTF_SIZET " bytes ) failed",
493 sizeof(mbedtls_x509_crt)));
494 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR,
495 MBEDTLS_ERR_SSL_ALLOC_FAILED);
496 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
Xiaofei Bai947571e2021-09-29 09:12:03 +0000497 }
498
Gilles Peskine449bd832023-01-11 14:50:10 +0100499 mbedtls_x509_crt_init(ssl->session_negotiate->peer_cert);
Xiaofei Bai947571e2021-09-29 09:12:03 +0000500
Gilles Peskine449bd832023-01-11 14:50:10 +0100501 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, certificate_list_len);
Xiaofei Bai947571e2021-09-29 09:12:03 +0000502 certificate_list_end = p + certificate_list_len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100503 while (p < certificate_list_end) {
Xiaofei Bai947571e2021-09-29 09:12:03 +0000504 size_t cert_data_len, extensions_len;
Jerry Yu2eaa7602022-08-04 17:28:15 +0800505 const unsigned char *extensions_end;
Xiaofei Bai947571e2021-09-29 09:12:03 +0000506
Gilles Peskine449bd832023-01-11 14:50:10 +0100507 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, certificate_list_end, 3);
508 cert_data_len = MBEDTLS_GET_UINT24_BE(p, 0);
Xiaofei Bai947571e2021-09-29 09:12:03 +0000509 p += 3;
510
511 /* In theory, the CRT can be up to 2^24 Bytes, but we don't support
512 * anything beyond 2^16 = 64K. Otherwise as in the TLS 1.2 code,
513 * check that we have a minimum of 128 bytes of data, this is not
514 * clear why we need that though.
515 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100516 if ((cert_data_len < 128) || (cert_data_len >= 0x10000)) {
517 MBEDTLS_SSL_DEBUG_MSG(1, ("bad Certificate message"));
518 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR,
519 MBEDTLS_ERR_SSL_DECODE_ERROR);
520 return MBEDTLS_ERR_SSL_DECODE_ERROR;
Xiaofei Bai947571e2021-09-29 09:12:03 +0000521 }
522
Gilles Peskine449bd832023-01-11 14:50:10 +0100523 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, certificate_list_end, cert_data_len);
524 ret = mbedtls_x509_crt_parse_der(ssl->session_negotiate->peer_cert,
525 p, cert_data_len);
Xiaofei Bai947571e2021-09-29 09:12:03 +0000526
Gilles Peskine449bd832023-01-11 14:50:10 +0100527 switch (ret) {
Xiaofei Bai947571e2021-09-29 09:12:03 +0000528 case 0: /*ok*/
529 break;
530 case MBEDTLS_ERR_X509_UNKNOWN_SIG_ALG + MBEDTLS_ERR_OID_NOT_FOUND:
531 /* Ignore certificate with an unknown algorithm: maybe a
532 prior certificate was already trusted. */
533 break;
534
535 case MBEDTLS_ERR_X509_ALLOC_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100536 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR,
537 MBEDTLS_ERR_X509_ALLOC_FAILED);
538 MBEDTLS_SSL_DEBUG_RET(1, " mbedtls_x509_crt_parse_der", ret);
539 return ret;
Xiaofei Bai947571e2021-09-29 09:12:03 +0000540
541 case MBEDTLS_ERR_X509_UNKNOWN_VERSION:
Gilles Peskine449bd832023-01-11 14:50:10 +0100542 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT,
543 MBEDTLS_ERR_X509_UNKNOWN_VERSION);
544 MBEDTLS_SSL_DEBUG_RET(1, " mbedtls_x509_crt_parse_der", ret);
545 return ret;
Xiaofei Bai947571e2021-09-29 09:12:03 +0000546
547 default:
Gilles Peskine449bd832023-01-11 14:50:10 +0100548 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_BAD_CERT,
549 ret);
550 MBEDTLS_SSL_DEBUG_RET(1, " mbedtls_x509_crt_parse_der", ret);
551 return ret;
Xiaofei Bai947571e2021-09-29 09:12:03 +0000552 }
553
554 p += cert_data_len;
555
556 /* Certificate extensions length */
Gilles Peskine449bd832023-01-11 14:50:10 +0100557 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, certificate_list_end, 2);
558 extensions_len = MBEDTLS_GET_UINT16_BE(p, 0);
Xiaofei Bai947571e2021-09-29 09:12:03 +0000559 p += 2;
Gilles Peskine449bd832023-01-11 14:50:10 +0100560 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, certificate_list_end, extensions_len);
Jerry Yu2eaa7602022-08-04 17:28:15 +0800561
562 extensions_end = p + extensions_len;
Jerry Yu0d5cfb72022-10-31 14:15:48 +0800563 handshake->received_extensions = MBEDTLS_SSL_EXT_MASK_NONE;
Jerry Yu2eaa7602022-08-04 17:28:15 +0800564
Gilles Peskine449bd832023-01-11 14:50:10 +0100565 while (p < extensions_end) {
Jerry Yu2eaa7602022-08-04 17:28:15 +0800566 unsigned int extension_type;
567 size_t extension_data_len;
568
569 /*
Gilles Peskine449bd832023-01-11 14:50:10 +0100570 * struct {
571 * ExtensionType extension_type; (2 bytes)
572 * opaque extension_data<0..2^16-1>;
573 * } Extension;
574 */
575 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, extensions_end, 4);
576 extension_type = MBEDTLS_GET_UINT16_BE(p, 0);
577 extension_data_len = MBEDTLS_GET_UINT16_BE(p, 2);
Jerry Yu2eaa7602022-08-04 17:28:15 +0800578 p += 4;
579
Gilles Peskine449bd832023-01-11 14:50:10 +0100580 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, extensions_end, extension_data_len);
Jerry Yu2eaa7602022-08-04 17:28:15 +0800581
Jerry Yuc4bf5d62022-10-29 09:08:47 +0800582 ret = mbedtls_ssl_tls13_check_received_extension(
Gilles Peskine449bd832023-01-11 14:50:10 +0100583 ssl, MBEDTLS_SSL_HS_CERTIFICATE, extension_type,
584 MBEDTLS_SSL_TLS1_3_ALLOWED_EXTS_OF_CT);
585 if (ret != 0) {
586 return ret;
587 }
Jerry Yu0c354a22022-08-29 15:25:36 +0800588
Gilles Peskine449bd832023-01-11 14:50:10 +0100589 switch (extension_type) {
Jerry Yu2eaa7602022-08-04 17:28:15 +0800590 default:
Jerry Yu79aa7212022-11-08 21:30:21 +0800591 MBEDTLS_SSL_PRINT_EXT(
Jerry Yu0d5cfb72022-10-31 14:15:48 +0800592 3, MBEDTLS_SSL_HS_CERTIFICATE,
Gilles Peskine449bd832023-01-11 14:50:10 +0100593 extension_type, "( ignored )");
Jerry Yu2eaa7602022-08-04 17:28:15 +0800594 break;
595 }
596
597 p += extension_data_len;
598 }
599
Gilles Peskine449bd832023-01-11 14:50:10 +0100600 MBEDTLS_SSL_PRINT_EXTS(3, MBEDTLS_SSL_HS_CERTIFICATE,
601 handshake->received_extensions);
Xiaofei Bai947571e2021-09-29 09:12:03 +0000602 }
603
XiaokangQian63e713e2022-05-15 04:26:57 +0000604exit:
Xiaofei Bai947571e2021-09-29 09:12:03 +0000605 /* Check that all the message is consumed. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100606 if (p != end) {
607 MBEDTLS_SSL_DEBUG_MSG(1, ("bad Certificate message"));
608 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR,
609 MBEDTLS_ERR_SSL_DECODE_ERROR);
610 return MBEDTLS_ERR_SSL_DECODE_ERROR;
Xiaofei Bai947571e2021-09-29 09:12:03 +0000611 }
612
Xiaokang Qian73437382023-03-29 08:24:12 +0000613 MBEDTLS_SSL_DEBUG_CRT(3, "peer certificate",
614 ssl->session_negotiate->peer_cert);
Xiaofei Bai947571e2021-09-29 09:12:03 +0000615
Gilles Peskine449bd832023-01-11 14:50:10 +0100616 return ret;
Xiaofei Bai947571e2021-09-29 09:12:03 +0000617}
618#else
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200619MBEDTLS_CHECK_RETURN_CRITICAL
Ronald Crone3dac4a2022-06-10 17:21:51 +0200620MBEDTLS_STATIC_TESTABLE
Gilles Peskine449bd832023-01-11 14:50:10 +0100621int mbedtls_ssl_tls13_parse_certificate(mbedtls_ssl_context *ssl,
622 const unsigned char *buf,
623 const unsigned char *end)
Xiaofei Bai947571e2021-09-29 09:12:03 +0000624{
625 ((void) ssl);
626 ((void) buf);
627 ((void) end);
Gilles Peskine449bd832023-01-11 14:50:10 +0100628 return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Xiaofei Bai947571e2021-09-29 09:12:03 +0000629}
630#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Ronald Cronde08cf32022-10-04 17:15:35 +0200631#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED */
Xiaofei Bai947571e2021-09-29 09:12:03 +0000632
Ronald Cronde08cf32022-10-04 17:15:35 +0200633#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED)
Xiaofei Bai947571e2021-09-29 09:12:03 +0000634#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Xiaofei Bai79595ac2021-10-26 07:16:45 +0000635/* Validate certificate chain sent by the server. */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200636MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100637static int ssl_tls13_validate_certificate(mbedtls_ssl_context *ssl)
Xiaofei Bai947571e2021-09-29 09:12:03 +0000638{
Manuel Pégourié-Gonnarde1cc9262024-08-14 09:47:38 +0200639 /* Authmode: precedence order is SNI if used else configuration */
640#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
641 const int authmode = ssl->handshake->sni_authmode != MBEDTLS_SSL_VERIFY_UNSET
642 ? ssl->handshake->sni_authmode
643 : ssl->conf->authmode;
644#else
645 const int authmode = ssl->conf->authmode;
XiaokangQian6b916b12022-04-25 07:29:34 +0000646#endif
647
648 /*
XiaokangQian989f06d2022-05-17 01:50:15 +0000649 * If the peer hasn't sent a certificate ( i.e. it sent
XiaokangQian6b916b12022-04-25 07:29:34 +0000650 * an empty certificate chain ), this is reflected in the peer CRT
651 * structure being unset.
652 * Check for that and handle it depending on the
XiaokangQian989f06d2022-05-17 01:50:15 +0000653 * authentication mode.
XiaokangQian6b916b12022-04-25 07:29:34 +0000654 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100655 if (ssl->session_negotiate->peer_cert == NULL) {
656 MBEDTLS_SSL_DEBUG_MSG(1, ("peer has no certificate"));
XiaokangQian989f06d2022-05-17 01:50:15 +0000657
XiaokangQian63e713e2022-05-15 04:26:57 +0000658#if defined(MBEDTLS_SSL_SRV_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100659 if (ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER) {
XiaokangQian63e713e2022-05-15 04:26:57 +0000660 /* The client was asked for a certificate but didn't send
661 * one. The client should know what's going on, so we
662 * don't send an alert.
663 */
664 ssl->session_negotiate->verify_result = MBEDTLS_X509_BADCERT_MISSING;
Gilles Peskine449bd832023-01-11 14:50:10 +0100665 if (authmode == MBEDTLS_SSL_VERIFY_OPTIONAL) {
666 return 0;
667 } else {
Xiaokang Qian73437382023-03-29 08:24:12 +0000668 MBEDTLS_SSL_PEND_FATAL_ALERT(
669 MBEDTLS_SSL_ALERT_MSG_NO_CERT,
670 MBEDTLS_ERR_SSL_NO_CLIENT_CERTIFICATE);
Gilles Peskine449bd832023-01-11 14:50:10 +0100671 return MBEDTLS_ERR_SSL_NO_CLIENT_CERTIFICATE;
XiaokangQian989f06d2022-05-17 01:50:15 +0000672 }
XiaokangQian63e713e2022-05-15 04:26:57 +0000673 }
XiaokangQian6b916b12022-04-25 07:29:34 +0000674#endif /* MBEDTLS_SSL_SRV_C */
675
XiaokangQianc3017f62022-05-13 05:55:41 +0000676#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard4d4c0c72024-08-12 10:36:40 +0200677 /* Regardless of authmode, the server is not allowed to send an empty
678 * certificate chain. (Last paragraph before 4.4.2.1 in RFC 8446: "The
679 * server's certificate_list MUST always be non-empty.") With authmode
680 * optional/none, we continue the handshake if we can't validate the
681 * server's cert, but we still break it if no certificate was sent. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100682 if (ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT) {
683 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_NO_CERT,
684 MBEDTLS_ERR_SSL_FATAL_ALERT_MESSAGE);
685 return MBEDTLS_ERR_SSL_FATAL_ALERT_MESSAGE;
XiaokangQian63e713e2022-05-15 04:26:57 +0000686 }
XiaokangQianc3017f62022-05-13 05:55:41 +0000687#endif /* MBEDTLS_SSL_CLI_C */
XiaokangQian63e713e2022-05-15 04:26:57 +0000688 }
XiaokangQian6b916b12022-04-25 07:29:34 +0000689
Manuel Pégourié-Gonnardce603302024-08-16 11:03:42 +0200690 return mbedtls_ssl_verify_certificate(ssl, authmode,
691 ssl->session_negotiate->peer_cert,
692 NULL, NULL);
Xiaofei Bai947571e2021-09-29 09:12:03 +0000693}
694#else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200695MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100696static int ssl_tls13_validate_certificate(mbedtls_ssl_context *ssl)
Xiaofei Bai947571e2021-09-29 09:12:03 +0000697{
698 ((void) ssl);
Gilles Peskine449bd832023-01-11 14:50:10 +0100699 return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Xiaofei Bai947571e2021-09-29 09:12:03 +0000700}
701#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Ronald Cronde08cf32022-10-04 17:15:35 +0200702#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED */
Xiaofei Bai947571e2021-09-29 09:12:03 +0000703
Gilles Peskine449bd832023-01-11 14:50:10 +0100704int mbedtls_ssl_tls13_process_certificate(mbedtls_ssl_context *ssl)
Xiaofei Bai947571e2021-09-29 09:12:03 +0000705{
Xiaofei Bai79595ac2021-10-26 07:16:45 +0000706 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine449bd832023-01-11 14:50:10 +0100707 MBEDTLS_SSL_DEBUG_MSG(2, ("=> parse certificate"));
Xiaofei Bai79595ac2021-10-26 07:16:45 +0000708
Ronald Cronde08cf32022-10-04 17:15:35 +0200709#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED)
XiaokangQianc3017f62022-05-13 05:55:41 +0000710 unsigned char *buf;
711 size_t buf_len;
Xiaofei Bai79595ac2021-10-26 07:16:45 +0000712
Gilles Peskine449bd832023-01-11 14:50:10 +0100713 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_tls13_fetch_handshake_msg(
714 ssl, MBEDTLS_SSL_HS_CERTIFICATE,
715 &buf, &buf_len));
Xiaofei Bai79595ac2021-10-26 07:16:45 +0000716
XiaokangQianc3017f62022-05-13 05:55:41 +0000717 /* Parse the certificate chain sent by the peer. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100718 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_tls13_parse_certificate(ssl, buf,
719 buf + buf_len));
XiaokangQianc3017f62022-05-13 05:55:41 +0000720 /* Validate the certificate chain and set the verification results. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100721 MBEDTLS_SSL_PROC_CHK(ssl_tls13_validate_certificate(ssl));
Xiaofei Bai79595ac2021-10-26 07:16:45 +0000722
Xiaokang Qian73437382023-03-29 08:24:12 +0000723 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_add_hs_msg_to_checksum(
724 ssl, MBEDTLS_SSL_HS_CERTIFICATE, buf, buf_len));
Xiaofei Bai79595ac2021-10-26 07:16:45 +0000725
Xiaofei Bai79595ac2021-10-26 07:16:45 +0000726cleanup:
Gilles Peskineff2558a2023-09-05 21:10:39 +0200727#else /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED */
728 (void) ssl;
Ronald Cronde08cf32022-10-04 17:15:35 +0200729#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED */
Xiaofei Bai79595ac2021-10-26 07:16:45 +0000730
Gilles Peskine449bd832023-01-11 14:50:10 +0100731 MBEDTLS_SSL_DEBUG_MSG(2, ("<= parse certificate"));
732 return ret;
Xiaofei Bai947571e2021-09-29 09:12:03 +0000733}
Ronald Cron928cbd32022-10-04 16:14:26 +0200734#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED)
Jerry Yu7399d0d2022-01-30 17:54:19 +0800735/*
736 * enum {
737 * X509(0),
738 * RawPublicKey(2),
739 * (255)
740 * } CertificateType;
741 *
742 * struct {
743 * select (certificate_type) {
744 * case RawPublicKey:
745 * // From RFC 7250 ASN.1_subjectPublicKeyInfo
746 * opaque ASN1_subjectPublicKeyInfo<1..2^24-1>;
747 *
748 * case X509:
749 * opaque cert_data<1..2^24-1>;
750 * };
751 * Extension extensions<0..2^16-1>;
752 * } CertificateEntry;
753 *
754 * struct {
755 * opaque certificate_request_context<0..2^8-1>;
756 * CertificateEntry certificate_list<0..2^24-1>;
757 * } Certificate;
758 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200759MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100760static int ssl_tls13_write_certificate_body(mbedtls_ssl_context *ssl,
761 unsigned char *buf,
762 unsigned char *end,
763 size_t *out_len)
Jerry Yu5cc35062022-01-28 16:16:08 +0800764{
Gilles Peskine449bd832023-01-11 14:50:10 +0100765 const mbedtls_x509_crt *crt = mbedtls_ssl_own_cert(ssl);
Jerry Yu3e536442022-02-15 11:05:59 +0800766 unsigned char *p = buf;
Jerry Yuc8d8d4e2022-02-18 12:10:03 +0800767 unsigned char *certificate_request_context =
Gilles Peskine449bd832023-01-11 14:50:10 +0100768 ssl->handshake->certificate_request_context;
Jerry Yuc8d8d4e2022-02-18 12:10:03 +0800769 unsigned char certificate_request_context_len =
Gilles Peskine449bd832023-01-11 14:50:10 +0100770 ssl->handshake->certificate_request_context_len;
Jerry Yuc8d8d4e2022-02-18 12:10:03 +0800771 unsigned char *p_certificate_list_len;
Jerry Yu5cc35062022-01-28 16:16:08 +0800772
Jerry Yu5cc35062022-01-28 16:16:08 +0800773
Jerry Yu3391ac02022-02-16 11:21:37 +0800774 /* ...
775 * opaque certificate_request_context<0..2^8-1>;
776 * ...
777 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100778 MBEDTLS_SSL_CHK_BUF_PTR(p, end, certificate_request_context_len + 1);
Jerry Yuc8d8d4e2022-02-18 12:10:03 +0800779 *p++ = certificate_request_context_len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100780 if (certificate_request_context_len > 0) {
781 memcpy(p, certificate_request_context, certificate_request_context_len);
Jerry Yuc8d8d4e2022-02-18 12:10:03 +0800782 p += certificate_request_context_len;
Jerry Yu537530d2022-02-15 14:00:57 +0800783 }
784
Jerry Yu3391ac02022-02-16 11:21:37 +0800785 /* ...
786 * CertificateEntry certificate_list<0..2^24-1>;
787 * ...
788 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100789 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 3);
Jerry Yuc8d8d4e2022-02-18 12:10:03 +0800790 p_certificate_list_len = p;
Jerry Yu3e536442022-02-15 11:05:59 +0800791 p += 3;
792
Gilles Peskine449bd832023-01-11 14:50:10 +0100793 MBEDTLS_SSL_DEBUG_CRT(3, "own certificate", crt);
Jerry Yu5cc35062022-01-28 16:16:08 +0800794
Gilles Peskine449bd832023-01-11 14:50:10 +0100795 while (crt != NULL) {
Jerry Yu7399d0d2022-01-30 17:54:19 +0800796 size_t cert_data_len = crt->raw.len;
Jerry Yu5cc35062022-01-28 16:16:08 +0800797
Gilles Peskine449bd832023-01-11 14:50:10 +0100798 MBEDTLS_SSL_CHK_BUF_PTR(p, end, cert_data_len + 3 + 2);
799 MBEDTLS_PUT_UINT24_BE(cert_data_len, p, 0);
Jerry Yu7399d0d2022-01-30 17:54:19 +0800800 p += 3;
Jerry Yu5cc35062022-01-28 16:16:08 +0800801
Gilles Peskine449bd832023-01-11 14:50:10 +0100802 memcpy(p, crt->raw.p, cert_data_len);
Jerry Yu7399d0d2022-01-30 17:54:19 +0800803 p += cert_data_len;
804 crt = crt->next;
Jerry Yu5cc35062022-01-28 16:16:08 +0800805
806 /* Currently, we don't have any certificate extensions defined.
807 * Hence, we are sending an empty extension with length zero.
808 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100809 MBEDTLS_PUT_UINT16_BE(0, p, 0);
Jerry Yu7399d0d2022-01-30 17:54:19 +0800810 p += 2;
Jerry Yu5cc35062022-01-28 16:16:08 +0800811 }
Jerry Yu5cc35062022-01-28 16:16:08 +0800812
Gilles Peskine449bd832023-01-11 14:50:10 +0100813 MBEDTLS_PUT_UINT24_BE(p - p_certificate_list_len - 3,
814 p_certificate_list_len, 0);
Jerry Yu7399d0d2022-01-30 17:54:19 +0800815
Jerry Yu3e536442022-02-15 11:05:59 +0800816 *out_len = p - buf;
Jerry Yu5cc35062022-01-28 16:16:08 +0800817
Jerry Yu7de2ff02022-11-08 21:43:46 +0800818 MBEDTLS_SSL_PRINT_EXTS(
Gilles Peskine449bd832023-01-11 14:50:10 +0100819 3, MBEDTLS_SSL_HS_CERTIFICATE, ssl->handshake->sent_extensions);
Jerry Yu4b8f2f72022-10-31 13:31:22 +0800820
Gilles Peskine449bd832023-01-11 14:50:10 +0100821 return 0;
Jerry Yu5cc35062022-01-28 16:16:08 +0800822}
Jerry Yu5cc35062022-01-28 16:16:08 +0800823
Gilles Peskine449bd832023-01-11 14:50:10 +0100824int mbedtls_ssl_tls13_write_certificate(mbedtls_ssl_context *ssl)
Jerry Yu5cc35062022-01-28 16:16:08 +0800825{
826 int ret;
Ronald Cron5bb8fc82022-03-09 07:00:13 +0100827 unsigned char *buf;
828 size_t buf_len, msg_len;
829
Gilles Peskine449bd832023-01-11 14:50:10 +0100830 MBEDTLS_SSL_DEBUG_MSG(2, ("=> write certificate"));
Jerry Yu5cc35062022-01-28 16:16:08 +0800831
Xiaokang Qian73437382023-03-29 08:24:12 +0000832 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_start_handshake_msg(
833 ssl, MBEDTLS_SSL_HS_CERTIFICATE, &buf, &buf_len));
Jerry Yu5cc35062022-01-28 16:16:08 +0800834
Gilles Peskine449bd832023-01-11 14:50:10 +0100835 MBEDTLS_SSL_PROC_CHK(ssl_tls13_write_certificate_body(ssl,
836 buf,
837 buf + buf_len,
838 &msg_len));
Jerry Yu5cc35062022-01-28 16:16:08 +0800839
Xiaokang Qian73437382023-03-29 08:24:12 +0000840 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_add_hs_msg_to_checksum(
841 ssl, MBEDTLS_SSL_HS_CERTIFICATE, buf, msg_len));
Jerry Yu5cc35062022-01-28 16:16:08 +0800842
Gilles Peskine449bd832023-01-11 14:50:10 +0100843 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_finish_handshake_msg(
844 ssl, buf_len, msg_len));
Jerry Yu5cc35062022-01-28 16:16:08 +0800845cleanup:
846
Gilles Peskine449bd832023-01-11 14:50:10 +0100847 MBEDTLS_SSL_DEBUG_MSG(2, ("<= write certificate"));
848 return ret;
Jerry Yu5cc35062022-01-28 16:16:08 +0800849}
850
Jerry Yu3e536442022-02-15 11:05:59 +0800851/*
852 * STATE HANDLING: Output Certificate Verify
853 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100854int mbedtls_ssl_tls13_check_sig_alg_cert_key_match(uint16_t sig_alg,
855 mbedtls_pk_context *key)
Jerry Yu0c6be8f2022-06-20 20:42:00 +0800856{
Dave Rodgman2eab4622023-10-05 13:30:37 +0100857 mbedtls_pk_type_t pk_type = (mbedtls_pk_type_t) mbedtls_ssl_sig_from_pk(key);
Gilles Peskine449bd832023-01-11 14:50:10 +0100858 size_t key_size = mbedtls_pk_get_bitlen(key);
Jerry Yu0c6be8f2022-06-20 20:42:00 +0800859
Gilles Peskine449bd832023-01-11 14:50:10 +0100860 switch (pk_type) {
Jerry Yu67eced02022-02-25 13:37:36 +0800861 case MBEDTLS_SSL_SIG_ECDSA:
Gilles Peskine449bd832023-01-11 14:50:10 +0100862 switch (key_size) {
Jerry Yu67eced02022-02-25 13:37:36 +0800863 case 256:
Gilles Peskine449bd832023-01-11 14:50:10 +0100864 return
865 sig_alg == MBEDTLS_TLS1_3_SIG_ECDSA_SECP256R1_SHA256;
Jerry Yu0c6be8f2022-06-20 20:42:00 +0800866
Jerry Yu67eced02022-02-25 13:37:36 +0800867 case 384:
Gilles Peskine449bd832023-01-11 14:50:10 +0100868 return
869 sig_alg == MBEDTLS_TLS1_3_SIG_ECDSA_SECP384R1_SHA384;
Jerry Yu0c6be8f2022-06-20 20:42:00 +0800870
Jerry Yu67eced02022-02-25 13:37:36 +0800871 case 521:
Gilles Peskine449bd832023-01-11 14:50:10 +0100872 return
873 sig_alg == MBEDTLS_TLS1_3_SIG_ECDSA_SECP521R1_SHA512;
Jerry Yu67eced02022-02-25 13:37:36 +0800874 default:
Jerry Yu67eced02022-02-25 13:37:36 +0800875 break;
876 }
877 break;
Jerry Yu67eced02022-02-25 13:37:36 +0800878
Jerry Yu67eced02022-02-25 13:37:36 +0800879 case MBEDTLS_SSL_SIG_RSA:
Gilles Peskine449bd832023-01-11 14:50:10 +0100880 switch (sig_alg) {
Ronald Cron38391bf2022-09-16 11:19:27 +0200881 case MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA256: /* Intentional fallthrough */
882 case MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA384: /* Intentional fallthrough */
Jerry Yu0c6be8f2022-06-20 20:42:00 +0800883 case MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA512:
Gilles Peskine449bd832023-01-11 14:50:10 +0100884 return 1;
Jerry Yuc2e04932022-06-27 22:13:03 +0800885
Jerry Yu0c6be8f2022-06-20 20:42:00 +0800886 default:
887 break;
Jerry Yucef3f332022-03-22 23:00:13 +0800888 }
Jerry Yu67eced02022-02-25 13:37:36 +0800889 break;
Jerry Yu0c6be8f2022-06-20 20:42:00 +0800890
Jerry Yu67eced02022-02-25 13:37:36 +0800891 default:
Jerry Yu67eced02022-02-25 13:37:36 +0800892 break;
893 }
Jerry Yu0c6be8f2022-06-20 20:42:00 +0800894
Gilles Peskine449bd832023-01-11 14:50:10 +0100895 return 0;
Jerry Yu0c6be8f2022-06-20 20:42:00 +0800896}
897
Ronald Cronce7d76e2022-07-08 18:56:49 +0200898MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100899static int ssl_tls13_write_certificate_verify_body(mbedtls_ssl_context *ssl,
900 unsigned char *buf,
901 unsigned char *end,
902 size_t *out_len)
Jerry Yu8511f122022-01-29 10:01:04 +0800903{
Ronald Cron067a1e72022-09-16 13:44:49 +0200904 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yu3e536442022-02-15 11:05:59 +0800905 unsigned char *p = buf;
Jerry Yu8511f122022-01-29 10:01:04 +0800906 mbedtls_pk_context *own_key;
Jerry Yu3e536442022-02-15 11:05:59 +0800907
Gilles Peskine449bd832023-01-11 14:50:10 +0100908 unsigned char handshake_hash[MBEDTLS_TLS1_3_MD_MAX_SIZE];
Jerry Yu8511f122022-01-29 10:01:04 +0800909 size_t handshake_hash_len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100910 unsigned char verify_buffer[SSL_VERIFY_STRUCT_MAX_SIZE];
Jerry Yu3e536442022-02-15 11:05:59 +0800911 size_t verify_buffer_len;
Ronald Cron067a1e72022-09-16 13:44:49 +0200912
913 uint16_t *sig_alg = ssl->handshake->received_sig_algs;
Jerry Yu3e536442022-02-15 11:05:59 +0800914 size_t signature_len = 0;
Jerry Yu8511f122022-01-29 10:01:04 +0800915
Jerry Yu0b7b1012022-02-23 12:23:05 +0800916 *out_len = 0;
917
Gilles Peskine449bd832023-01-11 14:50:10 +0100918 own_key = mbedtls_ssl_own_key(ssl);
919 if (own_key == NULL) {
920 MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
921 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Jerry Yu8511f122022-01-29 10:01:04 +0800922 }
923
Xiaokang Qian73437382023-03-29 08:24:12 +0000924 ret = mbedtls_ssl_get_handshake_transcript(
Dave Rodgman2eab4622023-10-05 13:30:37 +0100925 ssl, (mbedtls_md_type_t) ssl->handshake->ciphersuite_info->mac,
Xiaokang Qian73437382023-03-29 08:24:12 +0000926 handshake_hash, sizeof(handshake_hash), &handshake_hash_len);
Gilles Peskine449bd832023-01-11 14:50:10 +0100927 if (ret != 0) {
928 return ret;
929 }
Jerry Yu8511f122022-01-29 10:01:04 +0800930
Gilles Peskine449bd832023-01-11 14:50:10 +0100931 MBEDTLS_SSL_DEBUG_BUF(3, "handshake hash",
932 handshake_hash,
933 handshake_hash_len);
Jerry Yu8511f122022-01-29 10:01:04 +0800934
Gilles Peskine449bd832023-01-11 14:50:10 +0100935 ssl_tls13_create_verify_structure(handshake_hash, handshake_hash_len,
936 verify_buffer, &verify_buffer_len,
937 ssl->conf->endpoint);
Jerry Yu8511f122022-01-29 10:01:04 +0800938
939 /*
940 * struct {
941 * SignatureScheme algorithm;
942 * opaque signature<0..2^16-1>;
943 * } CertificateVerify;
944 */
Ronald Cron067a1e72022-09-16 13:44:49 +0200945 /* Check there is space for the algorithm identifier (2 bytes) and the
946 * signature length (2 bytes).
947 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100948 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 4);
Ronald Cron067a1e72022-09-16 13:44:49 +0200949
Gilles Peskine449bd832023-01-11 14:50:10 +0100950 for (; *sig_alg != MBEDTLS_TLS1_3_SIG_NONE; sig_alg++) {
Ronald Cron067a1e72022-09-16 13:44:49 +0200951 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
952 mbedtls_pk_type_t pk_type = MBEDTLS_PK_NONE;
953 mbedtls_md_type_t md_alg = MBEDTLS_MD_NONE;
954 psa_algorithm_t psa_algorithm = PSA_ALG_NONE;
955 unsigned char verify_hash[PSA_HASH_MAX_SIZE];
956 size_t verify_hash_len;
Jerry Yu67eced02022-02-25 13:37:36 +0800957
Gilles Peskine449bd832023-01-11 14:50:10 +0100958 if (!mbedtls_ssl_sig_alg_is_offered(ssl, *sig_alg)) {
Ronald Cron067a1e72022-09-16 13:44:49 +0200959 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +0100960 }
Jerry Yu67eced02022-02-25 13:37:36 +0800961
Gilles Peskine449bd832023-01-11 14:50:10 +0100962 if (!mbedtls_ssl_tls13_sig_alg_for_cert_verify_is_supported(*sig_alg)) {
Ronald Cron067a1e72022-09-16 13:44:49 +0200963 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +0100964 }
Ronald Cron067a1e72022-09-16 13:44:49 +0200965
Gilles Peskine449bd832023-01-11 14:50:10 +0100966 if (!mbedtls_ssl_tls13_check_sig_alg_cert_key_match(*sig_alg, own_key)) {
Ronald Cron067a1e72022-09-16 13:44:49 +0200967 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +0100968 }
Ronald Cron067a1e72022-09-16 13:44:49 +0200969
Gilles Peskine449bd832023-01-11 14:50:10 +0100970 if (mbedtls_ssl_get_pk_type_and_md_alg_from_sig_alg(
971 *sig_alg, &pk_type, &md_alg) != 0) {
972 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Ronald Cron067a1e72022-09-16 13:44:49 +0200973 }
974
975 /* Hash verify buffer with indicated hash function */
Manuel Pégourié-Gonnard2d6d9932023-03-28 11:38:08 +0200976 psa_algorithm = mbedtls_md_psa_alg_from_type(md_alg);
Gilles Peskine449bd832023-01-11 14:50:10 +0100977 status = psa_hash_compute(psa_algorithm,
978 verify_buffer,
979 verify_buffer_len,
980 verify_hash, sizeof(verify_hash),
981 &verify_hash_len);
982 if (status != PSA_SUCCESS) {
Andrzej Kurek8a045ce2022-12-23 11:00:06 -0500983 return PSA_TO_MBEDTLS_ERR(status);
Ronald Cron067a1e72022-09-16 13:44:49 +0200984 }
985
Gilles Peskine449bd832023-01-11 14:50:10 +0100986 MBEDTLS_SSL_DEBUG_BUF(3, "verify hash", verify_hash, verify_hash_len);
987
988 if ((ret = mbedtls_pk_sign_ext(pk_type, own_key,
989 md_alg, verify_hash, verify_hash_len,
990 p + 4, (size_t) (end - (p + 4)), &signature_len,
991 ssl->conf->f_rng, ssl->conf->p_rng)) != 0) {
992 MBEDTLS_SSL_DEBUG_MSG(2, ("CertificateVerify signature failed with %s",
993 mbedtls_ssl_sig_alg_to_str(*sig_alg)));
994 MBEDTLS_SSL_DEBUG_RET(2, "mbedtls_pk_sign_ext", ret);
995
996 /* The signature failed. This is possible if the private key
997 * was not suitable for the signature operation as purposely we
998 * did not check its suitability completely. Let's try with
999 * another signature algorithm.
1000 */
1001 continue;
1002 }
1003
1004 MBEDTLS_SSL_DEBUG_MSG(2, ("CertificateVerify signature with %s",
1005 mbedtls_ssl_sig_alg_to_str(*sig_alg)));
Ronald Cron067a1e72022-09-16 13:44:49 +02001006
1007 break;
1008 }
1009
Gilles Peskine449bd832023-01-11 14:50:10 +01001010 if (*sig_alg == MBEDTLS_TLS1_3_SIG_NONE) {
1011 MBEDTLS_SSL_DEBUG_MSG(1, ("no suitable signature algorithm"));
1012 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE,
1013 MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE);
1014 return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
Jerry Yu8511f122022-01-29 10:01:04 +08001015 }
1016
Gilles Peskine449bd832023-01-11 14:50:10 +01001017 MBEDTLS_PUT_UINT16_BE(*sig_alg, p, 0);
1018 MBEDTLS_PUT_UINT16_BE(signature_len, p, 2);
Jerry Yuf3b46b52022-06-19 16:52:27 +08001019
Ronald Cron067a1e72022-09-16 13:44:49 +02001020 *out_len = 4 + signature_len;
Jerry Yu8c338862022-03-23 13:34:04 +08001021
Gilles Peskine449bd832023-01-11 14:50:10 +01001022 return 0;
Jerry Yu8511f122022-01-29 10:01:04 +08001023}
Jerry Yu8511f122022-01-29 10:01:04 +08001024
Gilles Peskine449bd832023-01-11 14:50:10 +01001025int mbedtls_ssl_tls13_write_certificate_verify(mbedtls_ssl_context *ssl)
Jerry Yu8511f122022-01-29 10:01:04 +08001026{
1027 int ret = 0;
Jerry Yuca133a32022-02-15 14:22:05 +08001028 unsigned char *buf;
1029 size_t buf_len, msg_len;
1030
Gilles Peskine449bd832023-01-11 14:50:10 +01001031 MBEDTLS_SSL_DEBUG_MSG(2, ("=> write certificate verify"));
Jerry Yu8511f122022-01-29 10:01:04 +08001032
Xiaokang Qian73437382023-03-29 08:24:12 +00001033 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_start_handshake_msg(
1034 ssl, MBEDTLS_SSL_HS_CERTIFICATE_VERIFY,
1035 &buf, &buf_len));
Jerry Yu8511f122022-01-29 10:01:04 +08001036
Gilles Peskine449bd832023-01-11 14:50:10 +01001037 MBEDTLS_SSL_PROC_CHK(ssl_tls13_write_certificate_verify_body(
1038 ssl, buf, buf + buf_len, &msg_len));
Jerry Yu8511f122022-01-29 10:01:04 +08001039
Xiaokang Qian73437382023-03-29 08:24:12 +00001040 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_add_hs_msg_to_checksum(
1041 ssl, MBEDTLS_SSL_HS_CERTIFICATE_VERIFY,
1042 buf, msg_len));
Jerry Yu8511f122022-01-29 10:01:04 +08001043
Gilles Peskine449bd832023-01-11 14:50:10 +01001044 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_finish_handshake_msg(
1045 ssl, buf_len, msg_len));
Jerry Yu8511f122022-01-29 10:01:04 +08001046
1047cleanup:
1048
Gilles Peskine449bd832023-01-11 14:50:10 +01001049 MBEDTLS_SSL_DEBUG_MSG(2, ("<= write certificate verify"));
1050 return ret;
Jerry Yu8511f122022-01-29 10:01:04 +08001051}
1052
Ronald Cron928cbd32022-10-04 16:14:26 +02001053#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED */
Jerry Yu90f152d2022-01-29 22:12:42 +08001054
Jerry Yu5cc35062022-01-28 16:16:08 +08001055/*
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001056 *
XiaokangQianc5c39d52021-11-09 11:55:10 +00001057 * STATE HANDLING: Incoming Finished message.
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001058 */
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001059/*
1060 * Implementation
1061 */
1062
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001063MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01001064static int ssl_tls13_preprocess_finished_message(mbedtls_ssl_context *ssl)
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001065{
1066 int ret;
1067
Xiaokang Qian73437382023-03-29 08:24:12 +00001068 ret = mbedtls_ssl_tls13_calculate_verify_data(
1069 ssl,
1070 ssl->handshake->state_local.finished_in.digest,
1071 sizeof(ssl->handshake->state_local.finished_in.digest),
1072 &ssl->handshake->state_local.finished_in.digest_len,
1073 ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT ?
1074 MBEDTLS_SSL_IS_SERVER : MBEDTLS_SSL_IS_CLIENT);
Gilles Peskine449bd832023-01-11 14:50:10 +01001075 if (ret != 0) {
1076 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_tls13_calculate_verify_data", ret);
1077 return ret;
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001078 }
1079
Gilles Peskine449bd832023-01-11 14:50:10 +01001080 return 0;
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001081}
1082
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001083MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01001084static int ssl_tls13_parse_finished_message(mbedtls_ssl_context *ssl,
1085 const unsigned char *buf,
1086 const unsigned char *end)
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001087{
XiaokangQian33062842021-11-11 03:37:45 +00001088 /*
1089 * struct {
XiaokangQianc13f9352021-11-11 06:13:22 +00001090 * opaque verify_data[Hash.length];
XiaokangQian33062842021-11-11 03:37:45 +00001091 * } Finished;
1092 */
1093 const unsigned char *expected_verify_data =
1094 ssl->handshake->state_local.finished_in.digest;
1095 size_t expected_verify_data_len =
1096 ssl->handshake->state_local.finished_in.digest_len;
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001097 /* Structural validation */
Gilles Peskine449bd832023-01-11 14:50:10 +01001098 if ((size_t) (end - buf) != expected_verify_data_len) {
1099 MBEDTLS_SSL_DEBUG_MSG(1, ("bad finished message"));
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001100
Gilles Peskine449bd832023-01-11 14:50:10 +01001101 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR,
1102 MBEDTLS_ERR_SSL_DECODE_ERROR);
1103 return MBEDTLS_ERR_SSL_DECODE_ERROR;
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001104 }
1105
Gilles Peskine449bd832023-01-11 14:50:10 +01001106 MBEDTLS_SSL_DEBUG_BUF(4, "verify_data (self-computed):",
1107 expected_verify_data,
1108 expected_verify_data_len);
1109 MBEDTLS_SSL_DEBUG_BUF(4, "verify_data (received message):", buf,
1110 expected_verify_data_len);
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001111
1112 /* Semantic validation */
Gilles Peskine449bd832023-01-11 14:50:10 +01001113 if (mbedtls_ct_memcmp(buf,
1114 expected_verify_data,
1115 expected_verify_data_len) != 0) {
1116 MBEDTLS_SSL_DEBUG_MSG(1, ("bad finished message"));
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001117
Gilles Peskine449bd832023-01-11 14:50:10 +01001118 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_DECRYPT_ERROR,
1119 MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE);
1120 return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001121 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001122 return 0;
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001123}
1124
Gilles Peskine449bd832023-01-11 14:50:10 +01001125int mbedtls_ssl_tls13_process_finished_message(mbedtls_ssl_context *ssl)
XiaokangQianc5c39d52021-11-09 11:55:10 +00001126{
XiaokangQian33062842021-11-11 03:37:45 +00001127 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
XiaokangQianc5c39d52021-11-09 11:55:10 +00001128 unsigned char *buf;
Xiaofei Baieef15042021-11-18 07:29:56 +00001129 size_t buf_len;
XiaokangQianc5c39d52021-11-09 11:55:10 +00001130
Gilles Peskine449bd832023-01-11 14:50:10 +01001131 MBEDTLS_SSL_DEBUG_MSG(2, ("=> parse finished message"));
XiaokangQianc5c39d52021-11-09 11:55:10 +00001132
Xiaokang Qian73437382023-03-29 08:24:12 +00001133 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_tls13_fetch_handshake_msg(
1134 ssl, MBEDTLS_SSL_HS_FINISHED, &buf, &buf_len));
Jerry Yu0a92d6c2022-05-16 16:54:46 +08001135
1136 /* Preprocessing step: Compute handshake digest */
Gilles Peskine449bd832023-01-11 14:50:10 +01001137 MBEDTLS_SSL_PROC_CHK(ssl_tls13_preprocess_finished_message(ssl));
Jerry Yu0a92d6c2022-05-16 16:54:46 +08001138
Xiaokang Qian73437382023-03-29 08:24:12 +00001139 MBEDTLS_SSL_PROC_CHK(ssl_tls13_parse_finished_message(
1140 ssl, buf, buf + buf_len));
Jerry Yu0a92d6c2022-05-16 16:54:46 +08001141
Xiaokang Qian73437382023-03-29 08:24:12 +00001142 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_add_hs_msg_to_checksum(
1143 ssl, MBEDTLS_SSL_HS_FINISHED, buf, buf_len));
XiaokangQianc5c39d52021-11-09 11:55:10 +00001144
1145cleanup:
1146
Gilles Peskine449bd832023-01-11 14:50:10 +01001147 MBEDTLS_SSL_DEBUG_MSG(2, ("<= parse finished message"));
1148 return ret;
XiaokangQianc5c39d52021-11-09 11:55:10 +00001149}
1150
XiaokangQian74af2a82021-09-22 07:40:30 +00001151/*
1152 *
XiaokangQiancc90c942021-11-09 12:30:09 +00001153 * STATE HANDLING: Write and send Finished message.
XiaokangQian74af2a82021-09-22 07:40:30 +00001154 *
1155 */
XiaokangQian74af2a82021-09-22 07:40:30 +00001156/*
XiaokangQian35dc6252021-11-11 08:16:19 +00001157 * Implement
XiaokangQian74af2a82021-09-22 07:40:30 +00001158 */
1159
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001160MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01001161static int ssl_tls13_prepare_finished_message(mbedtls_ssl_context *ssl)
XiaokangQian74af2a82021-09-22 07:40:30 +00001162{
1163 int ret;
1164
1165 /* Compute transcript of handshake up to now. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001166 ret = mbedtls_ssl_tls13_calculate_verify_data(ssl,
1167 ssl->handshake->state_local.finished_out.digest,
1168 sizeof(ssl->handshake->state_local.finished_out.
1169 digest),
1170 &ssl->handshake->state_local.finished_out.digest_len,
1171 ssl->conf->endpoint);
XiaokangQian74af2a82021-09-22 07:40:30 +00001172
Gilles Peskine449bd832023-01-11 14:50:10 +01001173 if (ret != 0) {
1174 MBEDTLS_SSL_DEBUG_RET(1, "calculate_verify_data failed", ret);
1175 return ret;
XiaokangQian74af2a82021-09-22 07:40:30 +00001176 }
1177
Gilles Peskine449bd832023-01-11 14:50:10 +01001178 return 0;
XiaokangQian74af2a82021-09-22 07:40:30 +00001179}
1180
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001181MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01001182static int ssl_tls13_write_finished_message_body(mbedtls_ssl_context *ssl,
1183 unsigned char *buf,
1184 unsigned char *end,
1185 size_t *out_len)
XiaokangQian74af2a82021-09-22 07:40:30 +00001186{
XiaokangQian8773aa02021-11-10 07:33:09 +00001187 size_t verify_data_len = ssl->handshake->state_local.finished_out.digest_len;
XiaokangQian0fa66432021-11-15 03:33:57 +00001188 /*
1189 * struct {
1190 * opaque verify_data[Hash.length];
1191 * } Finished;
1192 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001193 MBEDTLS_SSL_CHK_BUF_PTR(buf, end, verify_data_len);
XiaokangQian74af2a82021-09-22 07:40:30 +00001194
Gilles Peskine449bd832023-01-11 14:50:10 +01001195 memcpy(buf, ssl->handshake->state_local.finished_out.digest,
1196 verify_data_len);
XiaokangQian74af2a82021-09-22 07:40:30 +00001197
Xiaofei Baid25fab62021-12-02 06:36:27 +00001198 *out_len = verify_data_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01001199 return 0;
XiaokangQian74af2a82021-09-22 07:40:30 +00001200}
XiaokangQianc5c39d52021-11-09 11:55:10 +00001201
XiaokangQian35dc6252021-11-11 08:16:19 +00001202/* Main entry point: orchestrates the other functions */
Gilles Peskine449bd832023-01-11 14:50:10 +01001203int mbedtls_ssl_tls13_write_finished_message(mbedtls_ssl_context *ssl)
XiaokangQian35dc6252021-11-11 08:16:19 +00001204{
1205 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1206 unsigned char *buf;
1207 size_t buf_len, msg_len;
1208
Gilles Peskine449bd832023-01-11 14:50:10 +01001209 MBEDTLS_SSL_DEBUG_MSG(2, ("=> write finished message"));
XiaokangQian35dc6252021-11-11 08:16:19 +00001210
Gilles Peskine449bd832023-01-11 14:50:10 +01001211 MBEDTLS_SSL_PROC_CHK(ssl_tls13_prepare_finished_message(ssl));
XiaokangQiandce82242021-11-15 06:01:26 +00001212
Gilles Peskine449bd832023-01-11 14:50:10 +01001213 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_start_handshake_msg(ssl,
1214 MBEDTLS_SSL_HS_FINISHED, &buf, &buf_len));
XiaokangQian35dc6252021-11-11 08:16:19 +00001215
Gilles Peskine449bd832023-01-11 14:50:10 +01001216 MBEDTLS_SSL_PROC_CHK(ssl_tls13_write_finished_message_body(
1217 ssl, buf, buf + buf_len, &msg_len));
XiaokangQian35dc6252021-11-11 08:16:19 +00001218
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +01001219 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_add_hs_msg_to_checksum(ssl,
Manuel Pégourié-Gonnard43cc1272023-02-06 11:48:19 +01001220 MBEDTLS_SSL_HS_FINISHED, buf, msg_len));
XiaokangQian35dc6252021-11-11 08:16:19 +00001221
Gilles Peskine449bd832023-01-11 14:50:10 +01001222 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_finish_handshake_msg(
1223 ssl, buf_len, msg_len));
XiaokangQian35dc6252021-11-11 08:16:19 +00001224cleanup:
1225
Gilles Peskine449bd832023-01-11 14:50:10 +01001226 MBEDTLS_SSL_DEBUG_MSG(2, ("<= write finished message"));
1227 return ret;
XiaokangQian35dc6252021-11-11 08:16:19 +00001228}
1229
Gilles Peskine449bd832023-01-11 14:50:10 +01001230void mbedtls_ssl_tls13_handshake_wrapup(mbedtls_ssl_context *ssl)
Jerry Yu378254d2021-10-30 21:44:47 +08001231{
1232
Gilles Peskine449bd832023-01-11 14:50:10 +01001233 MBEDTLS_SSL_DEBUG_MSG(3, ("=> handshake wrapup"));
Jerry Yu378254d2021-10-30 21:44:47 +08001234
Gilles Peskine449bd832023-01-11 14:50:10 +01001235 MBEDTLS_SSL_DEBUG_MSG(1, ("Switch to application keys for inbound traffic"));
1236 mbedtls_ssl_set_inbound_transform(ssl, ssl->transform_application);
Jerry Yue8c1fca2022-05-18 14:48:56 +08001237
Gilles Peskine449bd832023-01-11 14:50:10 +01001238 MBEDTLS_SSL_DEBUG_MSG(1, ("Switch to application keys for outbound traffic"));
1239 mbedtls_ssl_set_outbound_transform(ssl, ssl->transform_application);
Jerry Yue8c1fca2022-05-18 14:48:56 +08001240
Jerry Yu378254d2021-10-30 21:44:47 +08001241 /*
Jerry Yucfe64f02021-11-15 13:54:06 +08001242 * Free the previous session and switch to the current one.
Jerry Yu378254d2021-10-30 21:44:47 +08001243 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001244 if (ssl->session) {
1245 mbedtls_ssl_session_free(ssl->session);
1246 mbedtls_free(ssl->session);
Jerry Yu378254d2021-10-30 21:44:47 +08001247 }
1248 ssl->session = ssl->session_negotiate;
1249 ssl->session_negotiate = NULL;
1250
Gilles Peskine449bd832023-01-11 14:50:10 +01001251 MBEDTLS_SSL_DEBUG_MSG(3, ("<= handshake wrapup"));
Jerry Yu378254d2021-10-30 21:44:47 +08001252}
1253
Ronald Cron49ad6192021-11-24 16:25:31 +01001254/*
1255 *
1256 * STATE HANDLING: Write ChangeCipherSpec
1257 *
1258 */
1259#if defined(MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001260MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01001261static int ssl_tls13_write_change_cipher_spec_body(mbedtls_ssl_context *ssl,
1262 unsigned char *buf,
1263 unsigned char *end,
1264 size_t *olen)
Ronald Cron49ad6192021-11-24 16:25:31 +01001265{
1266 ((void) ssl);
1267
Gilles Peskine449bd832023-01-11 14:50:10 +01001268 MBEDTLS_SSL_CHK_BUF_PTR(buf, end, 1);
Ronald Cron49ad6192021-11-24 16:25:31 +01001269 buf[0] = 1;
1270 *olen = 1;
1271
Gilles Peskine449bd832023-01-11 14:50:10 +01001272 return 0;
Ronald Cron49ad6192021-11-24 16:25:31 +01001273}
1274
Gilles Peskine449bd832023-01-11 14:50:10 +01001275int mbedtls_ssl_tls13_write_change_cipher_spec(mbedtls_ssl_context *ssl)
Ronald Cron49ad6192021-11-24 16:25:31 +01001276{
1277 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1278
Gilles Peskine449bd832023-01-11 14:50:10 +01001279 MBEDTLS_SSL_DEBUG_MSG(2, ("=> write change cipher spec"));
Ronald Cron49ad6192021-11-24 16:25:31 +01001280
Ronald Crone273f722024-02-13 18:22:26 +01001281 /* Only one CCS to send. */
Ronald Cron5fbd2702024-02-14 10:03:36 +01001282 if (ssl->handshake->ccs_sent) {
Ronald Crone273f722024-02-13 18:22:26 +01001283 ret = 0;
1284 goto cleanup;
1285 }
1286
Ronald Cron49ad6192021-11-24 16:25:31 +01001287 /* Write CCS message */
Gilles Peskine449bd832023-01-11 14:50:10 +01001288 MBEDTLS_SSL_PROC_CHK(ssl_tls13_write_change_cipher_spec_body(
1289 ssl, ssl->out_msg,
1290 ssl->out_msg + MBEDTLS_SSL_OUT_CONTENT_LEN,
1291 &ssl->out_msglen));
Ronald Cron49ad6192021-11-24 16:25:31 +01001292
1293 ssl->out_msgtype = MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC;
1294
Ronald Cron49ad6192021-11-24 16:25:31 +01001295 /* Dispatch message */
Gilles Peskine449bd832023-01-11 14:50:10 +01001296 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_write_record(ssl, 0));
Ronald Cron49ad6192021-11-24 16:25:31 +01001297
Ronald Cron5fbd2702024-02-14 10:03:36 +01001298 ssl->handshake->ccs_sent = 1;
Ronald Cronfe59ff72024-01-24 14:31:50 +01001299
Ronald Cron49ad6192021-11-24 16:25:31 +01001300cleanup:
1301
Gilles Peskine449bd832023-01-11 14:50:10 +01001302 MBEDTLS_SSL_DEBUG_MSG(2, ("<= write change cipher spec"));
1303 return ret;
Ronald Cron49ad6192021-11-24 16:25:31 +01001304}
1305
1306#endif /* MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE */
1307
Xiaokang Qianecc29482022-11-02 07:52:47 +00001308/* Early Data Indication Extension
Xiaokang Qian0e97d4d2022-10-24 11:12:51 +00001309 *
Xiaokang Qian0e97d4d2022-10-24 11:12:51 +00001310 * struct {
1311 * select ( Handshake.msg_type ) {
Jerry Yu52335392023-11-23 18:06:06 +08001312 * case new_session_ticket: uint32 max_early_data_size;
Xiaokang Qian0e97d4d2022-10-24 11:12:51 +00001313 * case client_hello: Empty;
1314 * case encrypted_extensions: Empty;
1315 * };
1316 * } EarlyDataIndication;
1317 */
1318#if defined(MBEDTLS_SSL_EARLY_DATA)
Gilles Peskine449bd832023-01-11 14:50:10 +01001319int mbedtls_ssl_tls13_write_early_data_ext(mbedtls_ssl_context *ssl,
Jerry Yuc59c5862023-12-05 10:40:49 +08001320 int in_new_session_ticket,
Gilles Peskine449bd832023-01-11 14:50:10 +01001321 unsigned char *buf,
1322 const unsigned char *end,
Jerry Yuc59c5862023-12-05 10:40:49 +08001323 size_t *out_len)
Xiaokang Qian0e97d4d2022-10-24 11:12:51 +00001324{
1325 unsigned char *p = buf;
Xiaokang Qian0e97d4d2022-10-24 11:12:51 +00001326
Jerry Yu52335392023-11-23 18:06:06 +08001327#if defined(MBEDTLS_SSL_SRV_C)
Jerry Yuc59c5862023-12-05 10:40:49 +08001328 const size_t needed = in_new_session_ticket ? 8 : 4;
Jerry Yu52335392023-11-23 18:06:06 +08001329#else
1330 const size_t needed = 4;
Jerry Yuc59c5862023-12-05 10:40:49 +08001331 ((void) in_new_session_ticket);
Jerry Yu52335392023-11-23 18:06:06 +08001332#endif
1333
1334 *out_len = 0;
1335
1336 MBEDTLS_SSL_CHK_BUF_PTR(p, end, needed);
Xiaokang Qian0e97d4d2022-10-24 11:12:51 +00001337
Gilles Peskine449bd832023-01-11 14:50:10 +01001338 MBEDTLS_PUT_UINT16_BE(MBEDTLS_TLS_EXT_EARLY_DATA, p, 0);
Jerry Yu52335392023-11-23 18:06:06 +08001339 MBEDTLS_PUT_UINT16_BE(needed - 4, p, 2);
Xiaokang Qian0e97d4d2022-10-24 11:12:51 +00001340
Jerry Yu52335392023-11-23 18:06:06 +08001341#if defined(MBEDTLS_SSL_SRV_C)
Jerry Yuc59c5862023-12-05 10:40:49 +08001342 if (in_new_session_ticket) {
1343 MBEDTLS_PUT_UINT32_BE(ssl->conf->max_early_data_size, p, 4);
Jerry Yu52335392023-11-23 18:06:06 +08001344 MBEDTLS_SSL_DEBUG_MSG(
1345 4, ("Sent max_early_data_size=%u",
Jerry Yuc59c5862023-12-05 10:40:49 +08001346 (unsigned int) ssl->conf->max_early_data_size));
Jerry Yu52335392023-11-23 18:06:06 +08001347 }
1348#endif
1349
1350 *out_len = needed;
Xiaokang Qian2cd5ce02022-11-15 10:33:53 +00001351
Gilles Peskine449bd832023-01-11 14:50:10 +01001352 mbedtls_ssl_tls13_set_hs_sent_ext_mask(ssl, MBEDTLS_TLS_EXT_EARLY_DATA);
Xiaokang Qian2cd5ce02022-11-15 10:33:53 +00001353
Gilles Peskine449bd832023-01-11 14:50:10 +01001354 return 0;
Xiaokang Qian0e97d4d2022-10-24 11:12:51 +00001355}
Ronald Cron85718042024-02-22 10:22:09 +01001356
1357#if defined(MBEDTLS_SSL_SRV_C)
1358int mbedtls_ssl_tls13_check_early_data_len(mbedtls_ssl_context *ssl,
1359 size_t early_data_len)
1360{
Ronald Cron85718042024-02-22 10:22:09 +01001361 /*
1362 * This function should be called only while an handshake is in progress
1363 * and thus a session under negotiation. Add a sanity check to detect a
1364 * misuse.
1365 */
1366 if (ssl->session_negotiate == NULL) {
1367 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
1368 }
1369
1370 /* RFC 8446 section 4.6.1
1371 *
1372 * A server receiving more than max_early_data_size bytes of 0-RTT data
1373 * SHOULD terminate the connection with an "unexpected_message" alert.
Ronald Cron93795f22024-03-07 09:24:56 +01001374 * Note that if it is still possible to send early_data_len bytes of early
1375 * data, it means that early_data_len is smaller than max_early_data_size
1376 * (type uint32_t) and can fit in an uint32_t. We use this further
1377 * down.
Ronald Cron85718042024-02-22 10:22:09 +01001378 */
Ronald Cron2e7dfd52024-03-05 10:54:33 +01001379 if (early_data_len >
Ronald Cron85718042024-02-22 10:22:09 +01001380 (ssl->session_negotiate->max_early_data_size -
Ronald Cron19bfe0a2024-02-26 16:43:01 +01001381 ssl->total_early_data_size)) {
Ronald Cron85718042024-02-22 10:22:09 +01001382
1383 MBEDTLS_SSL_DEBUG_MSG(
Gilles Peskineeeb4ff52024-06-03 22:16:23 +02001384 2, ("EarlyData: Too much early data received, "
Gilles Peskine400659b2024-06-04 08:45:58 +02001385 "%lu + %" MBEDTLS_PRINTF_SIZET " > %lu",
1386 (unsigned long) ssl->total_early_data_size,
Gilles Peskineeeb4ff52024-06-03 22:16:23 +02001387 early_data_len,
Gilles Peskine400659b2024-06-04 08:45:58 +02001388 (unsigned long) ssl->session_negotiate->max_early_data_size));
Ronald Cron85718042024-02-22 10:22:09 +01001389
1390 MBEDTLS_SSL_PEND_FATAL_ALERT(
1391 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE,
1392 MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE);
1393 return MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE;
1394 }
1395
Ronald Cron2e7dfd52024-03-05 10:54:33 +01001396 /*
Ronald Cron93795f22024-03-07 09:24:56 +01001397 * early_data_len has been checked to be less than max_early_data_size
1398 * that is uint32_t. Its cast to an uint32_t below is thus safe. We need
1399 * the cast to appease some compilers.
Ronald Cron2e7dfd52024-03-05 10:54:33 +01001400 */
Ronald Cron2e7dfd52024-03-05 10:54:33 +01001401 ssl->total_early_data_size += (uint32_t) early_data_len;
Ronald Cron85718042024-02-22 10:22:09 +01001402
1403 return 0;
1404}
1405#endif /* MBEDTLS_SSL_SRV_C */
Xiaokang Qian0e97d4d2022-10-24 11:12:51 +00001406#endif /* MBEDTLS_SSL_EARLY_DATA */
1407
XiaokangQian78b1fa72022-01-19 06:56:30 +00001408/* Reset SSL context and update hash for handling HRR.
1409 *
1410 * Replace Transcript-Hash(X) by
1411 * Transcript-Hash( message_hash ||
1412 * 00 00 Hash.length ||
1413 * X )
1414 * A few states of the handshake are preserved, including:
1415 * - session ID
1416 * - session ticket
1417 * - negotiated ciphersuite
1418 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001419int mbedtls_ssl_reset_transcript_for_hrr(mbedtls_ssl_context *ssl)
XiaokangQian78b1fa72022-01-19 06:56:30 +00001420{
1421 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Przemyslaw Stekielda645252022-09-14 12:50:51 +02001422 unsigned char hash_transcript[PSA_HASH_MAX_SIZE + 4];
XiaokangQian0ece9982022-01-24 08:56:23 +00001423 size_t hash_len;
Xiaokang Qian6b980012023-02-07 03:17:45 +00001424 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
1425 ssl->handshake->ciphersuite_info;
XiaokangQian78b1fa72022-01-19 06:56:30 +00001426
Gilles Peskine449bd832023-01-11 14:50:10 +01001427 MBEDTLS_SSL_DEBUG_MSG(3, ("Reset SSL session for HRR"));
XiaokangQian78b1fa72022-01-19 06:56:30 +00001428
Dave Rodgman2eab4622023-10-05 13:30:37 +01001429 ret = mbedtls_ssl_get_handshake_transcript(ssl, (mbedtls_md_type_t) ciphersuite_info->mac,
Gilles Peskine449bd832023-01-11 14:50:10 +01001430 hash_transcript + 4,
1431 PSA_HASH_MAX_SIZE,
1432 &hash_len);
1433 if (ret != 0) {
Manuel Pégourié-Gonnardda7979b2023-02-21 09:31:10 +01001434 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_get_handshake_transcript", ret);
Gilles Peskine449bd832023-01-11 14:50:10 +01001435 return ret;
XiaokangQian0ece9982022-01-24 08:56:23 +00001436 }
1437
1438 hash_transcript[0] = MBEDTLS_SSL_HS_MESSAGE_HASH;
1439 hash_transcript[1] = 0;
1440 hash_transcript[2] = 0;
1441 hash_transcript[3] = (unsigned char) hash_len;
1442
1443 hash_len += 4;
1444
Manuel Pégourié-Gonnardda7979b2023-02-21 09:31:10 +01001445 MBEDTLS_SSL_DEBUG_BUF(4, "Truncated handshake transcript",
1446 hash_transcript, hash_len);
1447
Manuel Pégourié-Gonnardd7a7a232023-02-05 10:26:49 +01001448 /* Reset running hash and replace it with a hash of the transcript */
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +01001449 ret = mbedtls_ssl_reset_checksum(ssl);
1450 if (ret != 0) {
1451 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_reset_checksum", ret);
1452 return ret;
1453 }
1454 ret = ssl->handshake->update_checksum(ssl, hash_transcript, hash_len);
1455 if (ret != 0) {
1456 MBEDTLS_SSL_DEBUG_RET(1, "update_checksum", ret);
1457 return ret;
1458 }
Przemyslaw Stekiel4b3fff42022-02-14 16:39:52 +01001459
Gilles Peskine449bd832023-01-11 14:50:10 +01001460 return ret;
XiaokangQian78b1fa72022-01-19 06:56:30 +00001461}
1462
Valerio Settic9ae8622023-07-25 11:23:50 +02001463#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_EPHEMERAL_ENABLED)
XiaokangQian7807f9f2022-02-15 10:04:37 +00001464
Przemek Stekiel7ac93be2023-07-04 10:02:38 +02001465int mbedtls_ssl_tls13_read_public_xxdhe_share(mbedtls_ssl_context *ssl,
Gilles Peskine449bd832023-01-11 14:50:10 +01001466 const unsigned char *buf,
1467 size_t buf_len)
XiaokangQian7807f9f2022-02-15 10:04:37 +00001468{
Gilles Peskine449bd832023-01-11 14:50:10 +01001469 uint8_t *p = (uint8_t *) buf;
XiaokangQiancfd925f2022-04-14 07:10:37 +00001470 const uint8_t *end = buf + buf_len;
XiaokangQian9b5d04b2022-04-10 10:20:43 +00001471 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001472
XiaokangQian9b5d04b2022-04-10 10:20:43 +00001473 /* Get size of the TLS opaque key_exchange field of the KeyShareEntry struct. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001474 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, 2);
1475 uint16_t peerkey_len = MBEDTLS_GET_UINT16_BE(p, 0);
XiaokangQian9b5d04b2022-04-10 10:20:43 +00001476 p += 2;
XiaokangQian3207a322022-02-23 03:15:27 +00001477
XiaokangQian9b5d04b2022-04-10 10:20:43 +00001478 /* Check if key size is consistent with given buffer length. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001479 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, peerkey_len);
XiaokangQian9b5d04b2022-04-10 10:20:43 +00001480
Gilles Peskine12c5aaa2023-10-02 14:55:45 +02001481 /* Store peer's ECDH/FFDH public key. */
1482 if (peerkey_len > sizeof(handshake->xxdh_psa_peerkey)) {
Gilles Peskine530c4232023-10-02 15:37:23 +02001483 MBEDTLS_SSL_DEBUG_MSG(1, ("Invalid public key length: %u > %" MBEDTLS_PRINTF_SIZET,
1484 (unsigned) peerkey_len,
1485 sizeof(handshake->xxdh_psa_peerkey)));
Gilles Peskine12c5aaa2023-10-02 14:55:45 +02001486 return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
1487 }
Przemek Stekiel7ac93be2023-07-04 10:02:38 +02001488 memcpy(handshake->xxdh_psa_peerkey, p, peerkey_len);
1489 handshake->xxdh_psa_peerkey_len = peerkey_len;
XiaokangQian9b5d04b2022-04-10 10:20:43 +00001490
Gilles Peskine449bd832023-01-11 14:50:10 +01001491 return 0;
XiaokangQian3207a322022-02-23 03:15:27 +00001492}
Jerry Yu89e103c2022-03-30 22:43:29 +08001493
Valerio Setti711f8532023-07-31 11:28:07 +02001494#if defined(PSA_WANT_ALG_FFDH)
Przemek Stekielda4fba62023-06-02 14:52:28 +02001495static psa_status_t mbedtls_ssl_get_psa_ffdh_info_from_tls_id(
Przemek Stekiel75a5a9c2023-06-12 11:21:18 +02001496 uint16_t tls_id, size_t *bits, psa_key_type_t *key_type)
Przemek Stekiel29c219c2023-05-31 15:21:04 +02001497{
Przemek Stekiel75a5a9c2023-06-12 11:21:18 +02001498 switch (tls_id) {
Valerio Settiecaf7c52024-01-17 12:30:30 +01001499#if defined(PSA_WANT_DH_RFC7919_2048)
Przemek Stekiel29c219c2023-05-31 15:21:04 +02001500 case MBEDTLS_SSL_IANA_TLS_GROUP_FFDHE2048:
1501 *bits = 2048;
1502 *key_type = PSA_KEY_TYPE_DH_KEY_PAIR(PSA_DH_FAMILY_RFC7919);
1503 return PSA_SUCCESS;
Valerio Settiecaf7c52024-01-17 12:30:30 +01001504#endif /* PSA_WANT_DH_RFC7919_2048 */
1505#if defined(PSA_WANT_DH_RFC7919_3072)
Przemek Stekiel29c219c2023-05-31 15:21:04 +02001506 case MBEDTLS_SSL_IANA_TLS_GROUP_FFDHE3072:
1507 *bits = 3072;
1508 *key_type = PSA_KEY_TYPE_DH_KEY_PAIR(PSA_DH_FAMILY_RFC7919);
1509 return PSA_SUCCESS;
Valerio Settiecaf7c52024-01-17 12:30:30 +01001510#endif /* PSA_WANT_DH_RFC7919_3072 */
1511#if defined(PSA_WANT_DH_RFC7919_4096)
Przemek Stekiel29c219c2023-05-31 15:21:04 +02001512 case MBEDTLS_SSL_IANA_TLS_GROUP_FFDHE4096:
1513 *bits = 4096;
1514 *key_type = PSA_KEY_TYPE_DH_KEY_PAIR(PSA_DH_FAMILY_RFC7919);
1515 return PSA_SUCCESS;
Valerio Settiecaf7c52024-01-17 12:30:30 +01001516#endif /* PSA_WANT_DH_RFC7919_4096 */
1517#if defined(PSA_WANT_DH_RFC7919_6144)
Przemek Stekiel29c219c2023-05-31 15:21:04 +02001518 case MBEDTLS_SSL_IANA_TLS_GROUP_FFDHE6144:
1519 *bits = 6144;
1520 *key_type = PSA_KEY_TYPE_DH_KEY_PAIR(PSA_DH_FAMILY_RFC7919);
1521 return PSA_SUCCESS;
Valerio Settiecaf7c52024-01-17 12:30:30 +01001522#endif /* PSA_WANT_DH_RFC7919_6144 */
1523#if defined(PSA_WANT_DH_RFC7919_8192)
Przemek Stekiel29c219c2023-05-31 15:21:04 +02001524 case MBEDTLS_SSL_IANA_TLS_GROUP_FFDHE8192:
1525 *bits = 8192;
1526 *key_type = PSA_KEY_TYPE_DH_KEY_PAIR(PSA_DH_FAMILY_RFC7919);
1527 return PSA_SUCCESS;
Valerio Settiecaf7c52024-01-17 12:30:30 +01001528#endif /* PSA_WANT_DH_RFC7919_8192 */
Przemek Stekiel29c219c2023-05-31 15:21:04 +02001529 default:
1530 return PSA_ERROR_NOT_SUPPORTED;
1531 }
1532}
Valerio Setti711f8532023-07-31 11:28:07 +02001533#endif /* PSA_WANT_ALG_FFDH */
Przemek Stekiel29c219c2023-05-31 15:21:04 +02001534
Przemek Stekiel408569f2023-07-06 11:26:44 +02001535int mbedtls_ssl_tls13_generate_and_write_xxdh_key_exchange(
Gilles Peskine449bd832023-01-11 14:50:10 +01001536 mbedtls_ssl_context *ssl,
1537 uint16_t named_group,
1538 unsigned char *buf,
1539 unsigned char *end,
1540 size_t *out_len)
Jerry Yu89e103c2022-03-30 22:43:29 +08001541{
1542 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
1543 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
1544 psa_key_attributes_t key_attributes;
1545 size_t own_pubkey_len;
1546 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
Przemek Stekiel29c219c2023-05-31 15:21:04 +02001547 size_t bits = 0;
Przemek Stekiel75a5a9c2023-06-12 11:21:18 +02001548 psa_key_type_t key_type = PSA_KEY_TYPE_NONE;
1549 psa_algorithm_t alg = PSA_ALG_NONE;
Przemek Stekiel29c219c2023-05-31 15:21:04 +02001550 size_t buf_size = (size_t) (end - buf);
Jerry Yu89e103c2022-03-30 22:43:29 +08001551
Przemek Stekiel29c219c2023-05-31 15:21:04 +02001552 MBEDTLS_SSL_DEBUG_MSG(1, ("Perform PSA-based ECDH/FFDH computation."));
Jerry Yu89e103c2022-03-30 22:43:29 +08001553
Valerio Setti40d9ca92023-01-04 16:08:04 +01001554 /* Convert EC's TLS ID to PSA key type. */
Przemek Stekiel29c219c2023-05-31 15:21:04 +02001555#if defined(PSA_WANT_ALG_ECDH)
Xiaokang Qian73437382023-03-29 08:24:12 +00001556 if (mbedtls_ssl_get_psa_curve_info_from_tls_id(
Przemek Stekielda4fba62023-06-02 14:52:28 +02001557 named_group, &key_type, &bits) == PSA_SUCCESS) {
1558 alg = PSA_ALG_ECDH;
Przemek Stekiel29c219c2023-05-31 15:21:04 +02001559 }
1560#endif
1561#if defined(PSA_WANT_ALG_FFDH)
Przemek Stekiel75a5a9c2023-06-12 11:21:18 +02001562 if (mbedtls_ssl_get_psa_ffdh_info_from_tls_id(named_group, &bits,
1563 &key_type) == PSA_SUCCESS) {
Przemek Stekielda4fba62023-06-02 14:52:28 +02001564 alg = PSA_ALG_FFDH;
Przemek Stekiel29c219c2023-05-31 15:21:04 +02001565 }
1566#endif
1567
Przemek Stekiel75a5a9c2023-06-12 11:21:18 +02001568 if (key_type == PSA_KEY_TYPE_NONE) {
Gilles Peskine449bd832023-01-11 14:50:10 +01001569 return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
Valerio Setti40d9ca92023-01-04 16:08:04 +01001570 }
Przemek Stekiel29c219c2023-05-31 15:21:04 +02001571
Przemek Stekielda4fba62023-06-02 14:52:28 +02001572 if (buf_size < PSA_BITS_TO_BYTES(bits)) {
Przemek Stekielda4fba62023-06-02 14:52:28 +02001573 return MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL;
1574 }
1575
Przemek Stekiel7ac93be2023-07-04 10:02:38 +02001576 handshake->xxdh_psa_type = key_type;
Valerio Settiea59c432023-07-25 11:14:03 +02001577 ssl->handshake->xxdh_psa_bits = bits;
Jerry Yu89e103c2022-03-30 22:43:29 +08001578
1579 key_attributes = psa_key_attributes_init();
Gilles Peskine449bd832023-01-11 14:50:10 +01001580 psa_set_key_usage_flags(&key_attributes, PSA_KEY_USAGE_DERIVE);
Przemek Stekielda4fba62023-06-02 14:52:28 +02001581 psa_set_key_algorithm(&key_attributes, alg);
Przemek Stekiel7ac93be2023-07-04 10:02:38 +02001582 psa_set_key_type(&key_attributes, handshake->xxdh_psa_type);
Valerio Settiea59c432023-07-25 11:14:03 +02001583 psa_set_key_bits(&key_attributes, handshake->xxdh_psa_bits);
Jerry Yu89e103c2022-03-30 22:43:29 +08001584
Przemek Stekiel29c219c2023-05-31 15:21:04 +02001585 /* Generate ECDH/FFDH private key. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001586 status = psa_generate_key(&key_attributes,
Przemek Stekiel7ac93be2023-07-04 10:02:38 +02001587 &handshake->xxdh_psa_privkey);
Gilles Peskine449bd832023-01-11 14:50:10 +01001588 if (status != PSA_SUCCESS) {
Andrzej Kurek8a045ce2022-12-23 11:00:06 -05001589 ret = PSA_TO_MBEDTLS_ERR(status);
Gilles Peskine449bd832023-01-11 14:50:10 +01001590 MBEDTLS_SSL_DEBUG_RET(1, "psa_generate_key", ret);
1591 return ret;
Jerry Yu89e103c2022-03-30 22:43:29 +08001592
1593 }
1594
Przemek Stekiel29c219c2023-05-31 15:21:04 +02001595 /* Export the public part of the ECDH/FFDH private key from PSA. */
Przemek Stekiel7ac93be2023-07-04 10:02:38 +02001596 status = psa_export_public_key(handshake->xxdh_psa_privkey,
Przemek Stekiel29c219c2023-05-31 15:21:04 +02001597 buf, buf_size,
Gilles Peskine449bd832023-01-11 14:50:10 +01001598 &own_pubkey_len);
Przemek Stekiel29c219c2023-05-31 15:21:04 +02001599
Gilles Peskine449bd832023-01-11 14:50:10 +01001600 if (status != PSA_SUCCESS) {
Andrzej Kurek8a045ce2022-12-23 11:00:06 -05001601 ret = PSA_TO_MBEDTLS_ERR(status);
Gilles Peskine449bd832023-01-11 14:50:10 +01001602 MBEDTLS_SSL_DEBUG_RET(1, "psa_export_public_key", ret);
1603 return ret;
Jerry Yu89e103c2022-03-30 22:43:29 +08001604 }
1605
1606 *out_len = own_pubkey_len;
1607
Gilles Peskine449bd832023-01-11 14:50:10 +01001608 return 0;
Jerry Yu89e103c2022-03-30 22:43:29 +08001609}
Valerio Settic9ae8622023-07-25 11:23:50 +02001610#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_EPHEMERAL_ENABLED */
XiaokangQian7807f9f2022-02-15 10:04:37 +00001611
Jerry Yu0c354a22022-08-29 15:25:36 +08001612/* RFC 8446 section 4.2
1613 *
1614 * If an implementation receives an extension which it recognizes and which is
1615 * not specified for the message in which it appears, it MUST abort the handshake
1616 * with an "illegal_parameter" alert.
1617 *
1618 */
Jerry Yuc4bf5d62022-10-29 09:08:47 +08001619int mbedtls_ssl_tls13_check_received_extension(
Gilles Peskine449bd832023-01-11 14:50:10 +01001620 mbedtls_ssl_context *ssl,
1621 int hs_msg_type,
1622 unsigned int received_extension_type,
1623 uint32_t hs_msg_allowed_extensions_mask)
Jerry Yu0c354a22022-08-29 15:25:36 +08001624{
Jerry Yudf0ad652022-10-31 13:20:57 +08001625 uint32_t extension_mask = mbedtls_ssl_get_extension_mask(
Gilles Peskine449bd832023-01-11 14:50:10 +01001626 received_extension_type);
Jerry Yu0c354a22022-08-29 15:25:36 +08001627
Jerry Yu79aa7212022-11-08 21:30:21 +08001628 MBEDTLS_SSL_PRINT_EXT(
Gilles Peskine449bd832023-01-11 14:50:10 +01001629 3, hs_msg_type, received_extension_type, "received");
Jerry Yu0c354a22022-08-29 15:25:36 +08001630
Gilles Peskine449bd832023-01-11 14:50:10 +01001631 if ((extension_mask & hs_msg_allowed_extensions_mask) == 0) {
Jerry Yu79aa7212022-11-08 21:30:21 +08001632 MBEDTLS_SSL_PRINT_EXT(
Gilles Peskine449bd832023-01-11 14:50:10 +01001633 3, hs_msg_type, received_extension_type, "is illegal");
Jerry Yu0c354a22022-08-29 15:25:36 +08001634 MBEDTLS_SSL_PEND_FATAL_ALERT(
1635 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER,
Gilles Peskine449bd832023-01-11 14:50:10 +01001636 MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER);
1637 return MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER;
Jerry Yu0c354a22022-08-29 15:25:36 +08001638 }
1639
1640 ssl->handshake->received_extensions |= extension_mask;
Jerry Yuc4bf5d62022-10-29 09:08:47 +08001641 /*
1642 * If it is a message containing extension responses, check that we
1643 * previously sent the extension.
1644 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001645 switch (hs_msg_type) {
Jerry Yu0c354a22022-08-29 15:25:36 +08001646 case MBEDTLS_SSL_HS_SERVER_HELLO:
Jerry Yudf0ad652022-10-31 13:20:57 +08001647 case MBEDTLS_SSL_TLS1_3_HS_HELLO_RETRY_REQUEST:
Jerry Yu0c354a22022-08-29 15:25:36 +08001648 case MBEDTLS_SSL_HS_ENCRYPTED_EXTENSIONS:
1649 case MBEDTLS_SSL_HS_CERTIFICATE:
Jerry Yuc4bf5d62022-10-29 09:08:47 +08001650 /* Check if the received extension is sent by peer message.*/
Gilles Peskine449bd832023-01-11 14:50:10 +01001651 if ((ssl->handshake->sent_extensions & extension_mask) != 0) {
1652 return 0;
1653 }
Jerry Yu0c354a22022-08-29 15:25:36 +08001654 break;
1655 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01001656 return 0;
Jerry Yu0c354a22022-08-29 15:25:36 +08001657 }
1658
Jerry Yu79aa7212022-11-08 21:30:21 +08001659 MBEDTLS_SSL_PRINT_EXT(
Gilles Peskine449bd832023-01-11 14:50:10 +01001660 3, hs_msg_type, received_extension_type, "is unsupported");
Jerry Yu0c354a22022-08-29 15:25:36 +08001661 MBEDTLS_SSL_PEND_FATAL_ALERT(
1662 MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_EXT,
Gilles Peskine449bd832023-01-11 14:50:10 +01001663 MBEDTLS_ERR_SSL_UNSUPPORTED_EXTENSION);
1664 return MBEDTLS_ERR_SSL_UNSUPPORTED_EXTENSION;
Jerry Yu0c354a22022-08-29 15:25:36 +08001665}
1666
Jan Bruckner151f6422023-02-10 12:45:19 +01001667#if defined(MBEDTLS_SSL_RECORD_SIZE_LIMIT)
Yanray Wangfaf70bd2023-12-07 10:03:32 +08001668
1669/* RFC 8449, section 4:
1670 *
Jan Bruckner151f6422023-02-10 12:45:19 +01001671 * The ExtensionData of the "record_size_limit" extension is
1672 * RecordSizeLimit:
1673 * uint16 RecordSizeLimit;
1674 */
1675MBEDTLS_CHECK_RETURN_CRITICAL
1676int mbedtls_ssl_tls13_parse_record_size_limit_ext(mbedtls_ssl_context *ssl,
1677 const unsigned char *buf,
1678 const unsigned char *end)
1679{
Jan Bruckner1a38e542023-03-15 14:15:11 +01001680 const unsigned char *p = buf;
1681 uint16_t record_size_limit;
Jan Brucknera0589e72023-03-15 11:04:45 +01001682 const size_t extension_data_len = end - buf;
Jan Bruckner1a38e542023-03-15 14:15:11 +01001683
Xiaokang Qian73437382023-03-29 08:24:12 +00001684 if (extension_data_len !=
1685 MBEDTLS_SSL_RECORD_SIZE_LIMIT_EXTENSION_DATA_LENGTH) {
Jan Bruckner151f6422023-02-10 12:45:19 +01001686 MBEDTLS_SSL_DEBUG_MSG(2,
Jan Bruckner1a38e542023-03-15 14:15:11 +01001687 ("record_size_limit extension has invalid length: %"
1688 MBEDTLS_PRINTF_SIZET " Bytes",
Jan Bruckner151f6422023-02-10 12:45:19 +01001689 extension_data_len));
1690
1691 MBEDTLS_SSL_PEND_FATAL_ALERT(
1692 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER,
1693 MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER);
1694 return MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER;
1695 }
1696
Jan Bruckner151f6422023-02-10 12:45:19 +01001697 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, 2);
1698 record_size_limit = MBEDTLS_GET_UINT16_BE(p, 0);
1699
1700 MBEDTLS_SSL_DEBUG_MSG(2, ("RecordSizeLimit: %u Bytes", record_size_limit));
1701
Waleed Elmelegye1ac98d2024-01-05 18:10:12 +00001702 /* RFC 8449, section 4:
1703 *
1704 * Endpoints MUST NOT send a "record_size_limit" extension with a value
1705 * smaller than 64. An endpoint MUST treat receipt of a smaller value
1706 * as a fatal error and generate an "illegal_parameter" alert.
1707 */
1708 if (record_size_limit < MBEDTLS_SSL_RECORD_SIZE_LIMIT_MIN) {
1709 MBEDTLS_SSL_DEBUG_MSG(1, ("Invalid record size limit : %u Bytes",
1710 record_size_limit));
1711 MBEDTLS_SSL_PEND_FATAL_ALERT(
1712 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER,
1713 MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER);
1714 return MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER;
Jan Bruckner151f6422023-02-10 12:45:19 +01001715 }
1716
Jan Brucknerf482dcc2023-03-15 09:09:06 +01001717 ssl->session_negotiate->record_size_limit = record_size_limit;
Jan Bruckner151f6422023-02-10 12:45:19 +01001718
Jan Brucknerf482dcc2023-03-15 09:09:06 +01001719 return 0;
Jan Bruckner151f6422023-02-10 12:45:19 +01001720}
Jan Brucknerf482dcc2023-03-15 09:09:06 +01001721
Yanray Wangfaf70bd2023-12-07 10:03:32 +08001722MBEDTLS_CHECK_RETURN_CRITICAL
1723int mbedtls_ssl_tls13_write_record_size_limit_ext(mbedtls_ssl_context *ssl,
Yanray Wangfaf70bd2023-12-07 10:03:32 +08001724 unsigned char *buf,
1725 const unsigned char *end,
1726 size_t *out_len)
1727{
Yanray Wangfaf70bd2023-12-07 10:03:32 +08001728 unsigned char *p = buf;
1729 *out_len = 0;
1730
Waleed Elmelegy148dfb62024-01-04 18:02:35 +00001731 MBEDTLS_STATIC_ASSERT(MBEDTLS_SSL_IN_CONTENT_LEN >= MBEDTLS_SSL_RECORD_SIZE_LIMIT_MIN,
Waleed Elmelegye1ac98d2024-01-05 18:10:12 +00001732 "MBEDTLS_SSL_IN_CONTENT_LEN is less than the "
1733 "minimum record size limit");
Yanray Wangfaf70bd2023-12-07 10:03:32 +08001734
1735 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 6);
1736
1737 MBEDTLS_PUT_UINT16_BE(MBEDTLS_TLS_EXT_RECORD_SIZE_LIMIT, p, 0);
Waleed Elmelegye1ac98d2024-01-05 18:10:12 +00001738 MBEDTLS_PUT_UINT16_BE(MBEDTLS_SSL_RECORD_SIZE_LIMIT_EXTENSION_DATA_LENGTH,
1739 p, 2);
Waleed Elmelegy148dfb62024-01-04 18:02:35 +00001740 MBEDTLS_PUT_UINT16_BE(MBEDTLS_SSL_IN_CONTENT_LEN, p, 4);
Yanray Wangfaf70bd2023-12-07 10:03:32 +08001741
1742 *out_len = 6;
1743
Waleed Elmelegy3ff47242024-01-10 16:15:52 +00001744 MBEDTLS_SSL_DEBUG_MSG(2, ("Sent RecordSizeLimit: %d Bytes",
Waleed Elmelegye1ac98d2024-01-05 18:10:12 +00001745 MBEDTLS_SSL_IN_CONTENT_LEN));
Yanray Wangfaf70bd2023-12-07 10:03:32 +08001746
1747 mbedtls_ssl_tls13_set_hs_sent_ext_mask(ssl, MBEDTLS_TLS_EXT_RECORD_SIZE_LIMIT);
1748
1749 return 0;
1750}
1751
Jan Bruckner151f6422023-02-10 12:45:19 +01001752#endif /* MBEDTLS_SSL_RECORD_SIZE_LIMIT */
1753
Jerry Yufb4b6472022-01-27 15:03:26 +08001754#endif /* MBEDTLS_SSL_TLS_C && MBEDTLS_SSL_PROTO_TLS1_3 */