blob: deba2ae1e05a193325ec3c768935a1c966885f93 [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
Harry Ramsey0f6bc412024-10-04 10:36:54 +01008#include "ssl_misc.h"
Jerry Yu65dd2cc2021-08-18 16:38:40 +08009
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
Ronald Crone3dac4a2022-06-10 17:21:51 +020022#include "ssl_tls13_invasive.h"
Jerry Yu30b071c2021-09-12 20:16:03 +080023#include "ssl_tls13_keys.h"
Jerry Yu67eced02022-02-25 13:37:36 +080024#include "ssl_debug_helpers.h"
Jerry Yu65dd2cc2021-08-18 16:38:40 +080025
Andrzej Kurek8a045ce2022-12-23 11:00:06 -050026#include "psa/crypto.h"
Manuel Pégourié-Gonnard2be8c632023-06-07 13:07:21 +020027#include "psa_util_internal.h"
Andrzej Kurek8a045ce2022-12-23 11:00:06 -050028
Valerio Settic9ae8622023-07-25 11:23:50 +020029#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_EPHEMERAL_ENABLED)
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 Kureka6033ac2023-05-30 15:16:34 -040039#endif
Andrzej Kurek8a045ce2022-12-23 11:00:06 -050040
Jerry Yufbe3e642022-04-25 19:31:51 +080041const uint8_t mbedtls_ssl_tls13_hello_retry_request_magic[
Gilles Peskine449bd832023-01-11 14:50:10 +010042 MBEDTLS_SERVER_HELLO_RANDOM_LEN] =
43{ 0xCF, 0x21, 0xAD, 0x74, 0xE5, 0x9A, 0x61, 0x11,
44 0xBE, 0x1D, 0x8C, 0x02, 0x1E, 0x65, 0xB8, 0x91,
45 0xC2, 0xA2, 0x11, 0x16, 0x7A, 0xBB, 0x8C, 0x5E,
46 0x07, 0x9E, 0x09, 0xE2, 0xC8, 0xA8, 0x33, 0x9C };
Jerry Yu93a13f22022-04-11 23:00:01 +080047
Gilles Peskine449bd832023-01-11 14:50:10 +010048int mbedtls_ssl_tls13_fetch_handshake_msg(mbedtls_ssl_context *ssl,
49 unsigned hs_type,
50 unsigned char **buf,
51 size_t *buf_len)
XiaokangQian6b226b02021-09-24 07:51:16 +000052{
53 int ret;
54
Gilles Peskine449bd832023-01-11 14:50:10 +010055 if ((ret = mbedtls_ssl_read_record(ssl, 0)) != 0) {
56 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_read_record", ret);
XiaokangQian6b226b02021-09-24 07:51:16 +000057 goto cleanup;
58 }
59
Gilles Peskine449bd832023-01-11 14:50:10 +010060 if (ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE ||
61 ssl->in_msg[0] != hs_type) {
62 MBEDTLS_SSL_DEBUG_MSG(1, ("Receive unexpected handshake message."));
63 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE,
64 MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE);
XiaokangQian6b226b02021-09-24 07:51:16 +000065 ret = MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE;
66 goto cleanup;
67 }
68
XiaokangQian05420b12021-09-29 08:46:37 +000069 /*
70 * Jump handshake header (4 bytes, see Section 4 of RFC 8446).
71 * ...
72 * HandshakeType msg_type;
73 * uint24 length;
74 * ...
75 */
Xiaofei Baieef15042021-11-18 07:29:56 +000076 *buf = ssl->in_msg + 4;
77 *buf_len = ssl->in_hslen - 4;
XiaokangQian6b226b02021-09-24 07:51:16 +000078
XiaokangQian6b226b02021-09-24 07:51:16 +000079cleanup:
80
Gilles Peskine449bd832023-01-11 14:50:10 +010081 return ret;
XiaokangQian6b226b02021-09-24 07:51:16 +000082}
83
Ronald Cron47dce632023-02-08 17:38:29 +010084int mbedtls_ssl_tls13_is_supported_versions_ext_present_in_exts(
85 mbedtls_ssl_context *ssl,
86 const unsigned char *buf, const unsigned char *end,
Ronald Croneff56732023-04-03 17:36:31 +020087 const unsigned char **supported_versions_data,
88 const unsigned char **supported_versions_data_end)
Ronald Cron47dce632023-02-08 17:38:29 +010089{
90 const unsigned char *p = buf;
91 size_t extensions_len;
92 const unsigned char *extensions_end;
93
Ronald Croneff56732023-04-03 17:36:31 +020094 *supported_versions_data = NULL;
95 *supported_versions_data_end = NULL;
Ronald Cron47dce632023-02-08 17:38:29 +010096
97 /* Case of no extension */
98 if (p == end) {
99 return 0;
100 }
101
102 /* ...
103 * Extension extensions<x..2^16-1>;
104 * ...
105 * struct {
106 * ExtensionType extension_type; (2 bytes)
107 * opaque extension_data<0..2^16-1>;
108 * } Extension;
109 */
110 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, 2);
111 extensions_len = MBEDTLS_GET_UINT16_BE(p, 0);
112 p += 2;
113
114 /* Check extensions do not go beyond the buffer of data. */
115 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, extensions_len);
116 extensions_end = p + extensions_len;
117
118 while (p < extensions_end) {
119 unsigned int extension_type;
120 size_t extension_data_len;
121
122 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, extensions_end, 4);
123 extension_type = MBEDTLS_GET_UINT16_BE(p, 0);
124 extension_data_len = MBEDTLS_GET_UINT16_BE(p, 2);
125 p += 4;
126 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, extensions_end, extension_data_len);
127
128 if (extension_type == MBEDTLS_TLS_EXT_SUPPORTED_VERSIONS) {
Ronald Croneff56732023-04-03 17:36:31 +0200129 *supported_versions_data = p;
130 *supported_versions_data_end = p + extension_data_len;
Ronald Cron47dce632023-02-08 17:38:29 +0100131 return 1;
132 }
133 p += extension_data_len;
134 }
135
136 return 0;
137}
138
Ronald Cron928cbd32022-10-04 16:14:26 +0200139#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED)
Jerry Yu30b071c2021-09-12 20:16:03 +0800140/*
Jerry Yu30b071c2021-09-12 20:16:03 +0800141 * STATE HANDLING: Read CertificateVerify
142 */
Jerry Yud0fc5852021-10-29 11:09:06 +0800143/* Macro to express the maximum length of the verify structure.
Jerry Yu30b071c2021-09-12 20:16:03 +0800144 *
145 * The structure is computed per TLS 1.3 specification as:
146 * - 64 bytes of octet 32,
147 * - 33 bytes for the context string
148 * (which is either "TLS 1.3, client CertificateVerify"
149 * or "TLS 1.3, server CertificateVerify"),
Jerry Yud0fc5852021-10-29 11:09:06 +0800150 * - 1 byte for the octet 0x0, which serves as a separator,
Jerry Yu30b071c2021-09-12 20:16:03 +0800151 * - 32 or 48 bytes for the Transcript-Hash(Handshake Context, Certificate)
152 * (depending on the size of the transcript_hash)
153 *
154 * This results in a total size of
155 * - 130 bytes for a SHA256-based transcript hash, or
156 * (64 + 33 + 1 + 32 bytes)
157 * - 146 bytes for a SHA384-based transcript hash.
158 * (64 + 33 + 1 + 48 bytes)
159 *
160 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100161#define SSL_VERIFY_STRUCT_MAX_SIZE (64 + \
162 33 + \
163 1 + \
164 MBEDTLS_TLS1_3_MD_MAX_SIZE \
165 )
Jerry Yu30b071c2021-09-12 20:16:03 +0800166
Jerry Yu0b32c502021-10-28 13:41:59 +0800167/*
168 * The ssl_tls13_create_verify_structure() creates the verify structure.
169 * As input, it requires the transcript hash.
170 *
171 * The caller has to ensure that the buffer has size at least
172 * SSL_VERIFY_STRUCT_MAX_SIZE bytes.
173 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100174static void ssl_tls13_create_verify_structure(const unsigned char *transcript_hash,
175 size_t transcript_hash_len,
176 unsigned char *verify_buffer,
177 size_t *verify_buffer_len,
178 int from)
Jerry Yu0b32c502021-10-28 13:41:59 +0800179{
180 size_t idx;
Jerry Yu30b071c2021-09-12 20:16:03 +0800181
Jerry Yu0b32c502021-10-28 13:41:59 +0800182 /* RFC 8446, Section 4.4.3:
183 *
184 * The digital signature [in the CertificateVerify message] is then
185 * computed over the concatenation of:
186 * - A string that consists of octet 32 (0x20) repeated 64 times
187 * - The context string
188 * - A single 0 byte which serves as the separator
189 * - The content to be signed
190 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100191 memset(verify_buffer, 0x20, 64);
Jerry Yu0b32c502021-10-28 13:41:59 +0800192 idx = 64;
193
Gilles Peskine449bd832023-01-11 14:50:10 +0100194 if (from == MBEDTLS_SSL_IS_CLIENT) {
Tom Cosgrovea2c45dc2024-04-02 14:26:13 +0100195 memcpy(verify_buffer + idx, mbedtls_ssl_tls13_labels.client_cv,
196 MBEDTLS_SSL_TLS1_3_LBL_LEN(client_cv));
Gilles Peskine449bd832023-01-11 14:50:10 +0100197 idx += MBEDTLS_SSL_TLS1_3_LBL_LEN(client_cv);
198 } else { /* from == MBEDTLS_SSL_IS_SERVER */
Tom Cosgrovea2c45dc2024-04-02 14:26:13 +0100199 memcpy(verify_buffer + idx, mbedtls_ssl_tls13_labels.server_cv,
200 MBEDTLS_SSL_TLS1_3_LBL_LEN(server_cv));
Gilles Peskine449bd832023-01-11 14:50:10 +0100201 idx += MBEDTLS_SSL_TLS1_3_LBL_LEN(server_cv);
Jerry Yu0b32c502021-10-28 13:41:59 +0800202 }
203
204 verify_buffer[idx++] = 0x0;
205
Gilles Peskine449bd832023-01-11 14:50:10 +0100206 memcpy(verify_buffer + idx, transcript_hash, transcript_hash_len);
Jerry Yu0b32c502021-10-28 13:41:59 +0800207 idx += transcript_hash_len;
208
209 *verify_buffer_len = idx;
210}
211
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200212MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100213static int ssl_tls13_parse_certificate_verify(mbedtls_ssl_context *ssl,
214 const unsigned char *buf,
215 const unsigned char *end,
216 const unsigned char *verify_buffer,
217 size_t verify_buffer_len)
Jerry Yu30b071c2021-09-12 20:16:03 +0800218{
219 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
pespaceka1378102022-04-26 15:03:11 +0200220 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Jerry Yu30b071c2021-09-12 20:16:03 +0800221 const unsigned char *p = buf;
222 uint16_t algorithm;
Jerry Yu30b071c2021-09-12 20:16:03 +0800223 size_t signature_len;
224 mbedtls_pk_type_t sig_alg;
225 mbedtls_md_type_t md_alg;
pespaceka1378102022-04-26 15:03:11 +0200226 psa_algorithm_t hash_alg = PSA_ALG_NONE;
227 unsigned char verify_hash[PSA_HASH_MAX_SIZE];
Jerry Yu30b071c2021-09-12 20:16:03 +0800228 size_t verify_hash_len;
229
Xiaofei Baid25fab62021-12-02 06:36:27 +0000230 void const *options = NULL;
XiaokangQian82d34cc2021-11-03 08:51:56 +0000231#if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT)
Xiaofei Baid25fab62021-12-02 06:36:27 +0000232 mbedtls_pk_rsassa_pss_options rsassa_pss_options;
XiaokangQian82d34cc2021-11-03 08:51:56 +0000233#endif /* MBEDTLS_X509_RSASSA_PSS_SUPPORT */
234
Jerry Yu30b071c2021-09-12 20:16:03 +0800235 /*
236 * struct {
237 * SignatureScheme algorithm;
238 * opaque signature<0..2^16-1>;
239 * } CertificateVerify;
240 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100241 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, 2);
242 algorithm = MBEDTLS_GET_UINT16_BE(p, 0);
Jerry Yu30b071c2021-09-12 20:16:03 +0800243 p += 2;
244
245 /* RFC 8446 section 4.4.3
246 *
Xiaokang Qian73437382023-03-29 08:24:12 +0000247 * If the CertificateVerify message is sent by a server, the signature
248 * algorithm MUST be one offered in the client's "signature_algorithms"
249 * extension unless no valid certificate chain can be produced without
250 * unsupported algorithms
Jerry Yu30b071c2021-09-12 20:16:03 +0800251 *
252 * RFC 8446 section 4.4.2.2
253 *
254 * If the client cannot construct an acceptable chain using the provided
Xiaokang Qian73437382023-03-29 08:24:12 +0000255 * certificates and decides to abort the handshake, then it MUST abort the
256 * handshake with an appropriate certificate-related alert
257 * (by default, "unsupported_certificate").
Jerry Yu30b071c2021-09-12 20:16:03 +0800258 *
Jerry Yu6f87f252021-10-29 20:12:51 +0800259 * Check if algorithm is an offered signature algorithm.
Jerry Yu30b071c2021-09-12 20:16:03 +0800260 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100261 if (!mbedtls_ssl_sig_alg_is_offered(ssl, algorithm)) {
Jerry Yu982d9e52021-10-14 15:59:37 +0800262 /* algorithm not in offered signature algorithms list */
Gilles Peskine449bd832023-01-11 14:50:10 +0100263 MBEDTLS_SSL_DEBUG_MSG(1, ("Received signature algorithm(%04x) is not "
264 "offered.",
265 (unsigned int) algorithm));
Jerry Yu6f87f252021-10-29 20:12:51 +0800266 goto error;
Jerry Yu30b071c2021-09-12 20:16:03 +0800267 }
268
Gilles Peskine449bd832023-01-11 14:50:10 +0100269 if (mbedtls_ssl_get_pk_type_and_md_alg_from_sig_alg(
270 algorithm, &sig_alg, &md_alg) != 0) {
Jerry Yu8c338862022-03-23 13:34:04 +0800271 goto error;
Jerry Yu30b071c2021-09-12 20:16:03 +0800272 }
273
Manuel Pégourié-Gonnard2d6d9932023-03-28 11:38:08 +0200274 hash_alg = mbedtls_md_psa_alg_from_type(md_alg);
Gilles Peskine449bd832023-01-11 14:50:10 +0100275 if (hash_alg == 0) {
pespaceka1378102022-04-26 15:03:11 +0200276 goto error;
277 }
278
Gilles Peskine449bd832023-01-11 14:50:10 +0100279 MBEDTLS_SSL_DEBUG_MSG(3, ("Certificate Verify: Signature algorithm ( %04x )",
280 (unsigned int) algorithm));
Jerry Yu30b071c2021-09-12 20:16:03 +0800281
282 /*
283 * Check the certificate's key type matches the signature alg
284 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100285 if (!mbedtls_pk_can_do(&ssl->session_negotiate->peer_cert->pk, sig_alg)) {
286 MBEDTLS_SSL_DEBUG_MSG(1, ("signature algorithm doesn't match cert key"));
Jerry Yu6f87f252021-10-29 20:12:51 +0800287 goto error;
Jerry Yu30b071c2021-09-12 20:16:03 +0800288 }
289
Gilles Peskine449bd832023-01-11 14:50:10 +0100290 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, 2);
291 signature_len = MBEDTLS_GET_UINT16_BE(p, 0);
Jerry Yu30b071c2021-09-12 20:16:03 +0800292 p += 2;
Gilles Peskine449bd832023-01-11 14:50:10 +0100293 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, signature_len);
Jerry Yu30b071c2021-09-12 20:16:03 +0800294
Gilles Peskine449bd832023-01-11 14:50:10 +0100295 status = psa_hash_compute(hash_alg,
296 verify_buffer,
297 verify_buffer_len,
298 verify_hash,
299 sizeof(verify_hash),
300 &verify_hash_len);
301 if (status != PSA_SUCCESS) {
302 MBEDTLS_SSL_DEBUG_RET(1, "hash computation PSA error", status);
Jerry Yu6f87f252021-10-29 20:12:51 +0800303 goto error;
Jerry Yu133690c2021-10-25 14:01:13 +0800304 }
305
Gilles Peskine449bd832023-01-11 14:50:10 +0100306 MBEDTLS_SSL_DEBUG_BUF(3, "verify hash", verify_hash, verify_hash_len);
XiaokangQian82d34cc2021-11-03 08:51:56 +0000307#if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT)
Gilles Peskine449bd832023-01-11 14:50:10 +0100308 if (sig_alg == MBEDTLS_PK_RSASSA_PSS) {
Xiaofei Baid25fab62021-12-02 06:36:27 +0000309 rsassa_pss_options.mgf1_hash_id = md_alg;
Przemek Stekiel6a5e0182022-06-27 11:53:13 +0200310
Gilles Peskine449bd832023-01-11 14:50:10 +0100311 rsassa_pss_options.expected_salt_len = PSA_HASH_LENGTH(hash_alg);
312 options = (const void *) &rsassa_pss_options;
XiaokangQian82d34cc2021-11-03 08:51:56 +0000313 }
314#endif /* MBEDTLS_X509_RSASSA_PSS_SUPPORT */
Jerry Yu30b071c2021-09-12 20:16:03 +0800315
Gilles Peskine449bd832023-01-11 14:50:10 +0100316 if ((ret = mbedtls_pk_verify_ext(sig_alg, options,
317 &ssl->session_negotiate->peer_cert->pk,
318 md_alg, verify_hash, verify_hash_len,
319 p, signature_len)) == 0) {
320 return 0;
Jerry Yu30b071c2021-09-12 20:16:03 +0800321 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100322 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_pk_verify_ext", ret);
Jerry Yu30b071c2021-09-12 20:16:03 +0800323
Jerry Yu6f87f252021-10-29 20:12:51 +0800324error:
325 /* RFC 8446 section 4.4.3
326 *
327 * If the verification fails, the receiver MUST terminate the handshake
328 * with a "decrypt_error" alert.
Gilles Peskine449bd832023-01-11 14:50:10 +0100329 */
330 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_DECRYPT_ERROR,
331 MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE);
332 return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
Jerry Yu6f87f252021-10-29 20:12:51 +0800333
Jerry Yu30b071c2021-09-12 20:16:03 +0800334}
Ronald Cron928cbd32022-10-04 16:14:26 +0200335#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED */
Jerry Yu30b071c2021-09-12 20:16:03 +0800336
Gilles Peskine449bd832023-01-11 14:50:10 +0100337int mbedtls_ssl_tls13_process_certificate_verify(mbedtls_ssl_context *ssl)
Jerry Yu30b071c2021-09-12 20:16:03 +0800338{
Jerry Yu30b071c2021-09-12 20:16:03 +0800339
Ronald Cron928cbd32022-10-04 16:14:26 +0200340#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED)
Jerry Yuda8cdf22021-10-25 15:06:49 +0800341 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
342 unsigned char verify_buffer[SSL_VERIFY_STRUCT_MAX_SIZE];
343 size_t verify_buffer_len;
344 unsigned char transcript[MBEDTLS_TLS1_3_MD_MAX_SIZE];
345 size_t transcript_len;
346 unsigned char *buf;
347 size_t buf_len;
348
Gilles Peskine449bd832023-01-11 14:50:10 +0100349 MBEDTLS_SSL_DEBUG_MSG(2, ("=> parse certificate verify"));
Jerry Yu30b071c2021-09-12 20:16:03 +0800350
Jerry Yuda8cdf22021-10-25 15:06:49 +0800351 MBEDTLS_SSL_PROC_CHK(
Xiaokang Qian73437382023-03-29 08:24:12 +0000352 mbedtls_ssl_tls13_fetch_handshake_msg(
353 ssl, MBEDTLS_SSL_HS_CERTIFICATE_VERIFY, &buf, &buf_len));
Jerry Yu30b071c2021-09-12 20:16:03 +0800354
Jerry Yuda8cdf22021-10-25 15:06:49 +0800355 /* Need to calculate the hash of the transcript first
Jerry Yu0b32c502021-10-28 13:41:59 +0800356 * before reading the message since otherwise it gets
357 * included in the transcript
358 */
Xiaokang Qian73437382023-03-29 08:24:12 +0000359 ret = mbedtls_ssl_get_handshake_transcript(
360 ssl,
Dave Rodgman2eab4622023-10-05 13:30:37 +0100361 (mbedtls_md_type_t) ssl->handshake->ciphersuite_info->mac,
Xiaokang Qian73437382023-03-29 08:24:12 +0000362 transcript, sizeof(transcript),
363 &transcript_len);
Gilles Peskine449bd832023-01-11 14:50:10 +0100364 if (ret != 0) {
Jerry Yuda8cdf22021-10-25 15:06:49 +0800365 MBEDTLS_SSL_PEND_FATAL_ALERT(
366 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR,
Gilles Peskine449bd832023-01-11 14:50:10 +0100367 MBEDTLS_ERR_SSL_INTERNAL_ERROR);
368 return ret;
Jerry Yu30b071c2021-09-12 20:16:03 +0800369 }
370
Gilles Peskine449bd832023-01-11 14:50:10 +0100371 MBEDTLS_SSL_DEBUG_BUF(3, "handshake hash", transcript, transcript_len);
Jerry Yuda8cdf22021-10-25 15:06:49 +0800372
373 /* Create verify structure */
Gilles Peskine449bd832023-01-11 14:50:10 +0100374 ssl_tls13_create_verify_structure(transcript,
375 transcript_len,
376 verify_buffer,
377 &verify_buffer_len,
378 (ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT) ?
379 MBEDTLS_SSL_IS_SERVER :
380 MBEDTLS_SSL_IS_CLIENT);
Jerry Yuda8cdf22021-10-25 15:06:49 +0800381
382 /* Process the message contents */
Xiaokang Qian73437382023-03-29 08:24:12 +0000383 MBEDTLS_SSL_PROC_CHK(ssl_tls13_parse_certificate_verify(
384 ssl, buf, buf + buf_len,
385 verify_buffer, verify_buffer_len));
Jerry Yuda8cdf22021-10-25 15:06:49 +0800386
Xiaokang Qian73437382023-03-29 08:24:12 +0000387 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_add_hs_msg_to_checksum(
388 ssl, MBEDTLS_SSL_HS_CERTIFICATE_VERIFY,
389 buf, buf_len));
Jerry Yu30b071c2021-09-12 20:16:03 +0800390
391cleanup:
392
Gilles Peskine449bd832023-01-11 14:50:10 +0100393 MBEDTLS_SSL_DEBUG_MSG(2, ("<= parse certificate verify"));
394 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_tls13_process_certificate_verify", ret);
395 return ret;
Jerry Yuda8cdf22021-10-25 15:06:49 +0800396#else
397 ((void) ssl);
Gilles Peskine449bd832023-01-11 14:50:10 +0100398 MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
399 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Ronald Cron928cbd32022-10-04 16:14:26 +0200400#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED */
Jerry Yu30b071c2021-09-12 20:16:03 +0800401}
402
403/*
Xiaofei Bai947571e2021-09-29 09:12:03 +0000404 *
XiaokangQian6b916b12022-04-25 07:29:34 +0000405 * STATE HANDLING: Incoming Certificate.
Xiaofei Bai947571e2021-09-29 09:12:03 +0000406 *
407 */
408
Ronald Cronde08cf32022-10-04 17:15:35 +0200409#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED)
Xiaofei Bai947571e2021-09-29 09:12:03 +0000410#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
411/*
412 * Structure of Certificate message:
413 *
414 * enum {
415 * X509(0),
416 * RawPublicKey(2),
417 * (255)
418 * } CertificateType;
419 *
420 * struct {
421 * select (certificate_type) {
422 * case RawPublicKey:
423 * * From RFC 7250 ASN.1_subjectPublicKeyInfo *
424 * opaque ASN1_subjectPublicKeyInfo<1..2^24-1>;
425 * case X509:
426 * opaque cert_data<1..2^24-1>;
427 * };
428 * Extension extensions<0..2^16-1>;
429 * } CertificateEntry;
430 *
431 * struct {
432 * opaque certificate_request_context<0..2^8-1>;
433 * CertificateEntry certificate_list<0..2^24-1>;
434 * } Certificate;
435 *
436 */
Xiaofei Bai79595ac2021-10-26 07:16:45 +0000437
438/* Parse certificate chain send by the server. */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200439MBEDTLS_CHECK_RETURN_CRITICAL
Ronald Crone3dac4a2022-06-10 17:21:51 +0200440MBEDTLS_STATIC_TESTABLE
Gilles Peskine449bd832023-01-11 14:50:10 +0100441int mbedtls_ssl_tls13_parse_certificate(mbedtls_ssl_context *ssl,
442 const unsigned char *buf,
443 const unsigned char *end)
Xiaofei Bai947571e2021-09-29 09:12:03 +0000444{
445 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
446 size_t certificate_request_context_len = 0;
447 size_t certificate_list_len = 0;
448 const unsigned char *p = buf;
449 const unsigned char *certificate_list_end;
Jerry Yuc4bf5d62022-10-29 09:08:47 +0800450 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
Xiaofei Bai947571e2021-09-29 09:12:03 +0000451
Gilles Peskine449bd832023-01-11 14:50:10 +0100452 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, 4);
Xiaofei Bai947571e2021-09-29 09:12:03 +0000453 certificate_request_context_len = p[0];
Gilles Peskine449bd832023-01-11 14:50:10 +0100454 certificate_list_len = MBEDTLS_GET_UINT24_BE(p, 1);
XiaokangQian63e713e2022-05-15 04:26:57 +0000455 p += 4;
Xiaofei Bai947571e2021-09-29 09:12:03 +0000456
457 /* In theory, the certificate list can be up to 2^24 Bytes, but we don't
458 * support anything beyond 2^16 = 64K.
459 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100460 if ((certificate_request_context_len != 0) ||
461 (certificate_list_len >= 0x10000)) {
462 MBEDTLS_SSL_DEBUG_MSG(1, ("bad certificate message"));
463 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR,
464 MBEDTLS_ERR_SSL_DECODE_ERROR);
465 return MBEDTLS_ERR_SSL_DECODE_ERROR;
Xiaofei Bai947571e2021-09-29 09:12:03 +0000466 }
467
468 /* In case we tried to reuse a session but it failed */
Gilles Peskine449bd832023-01-11 14:50:10 +0100469 if (ssl->session_negotiate->peer_cert != NULL) {
470 mbedtls_x509_crt_free(ssl->session_negotiate->peer_cert);
471 mbedtls_free(ssl->session_negotiate->peer_cert);
Xiaofei Bai947571e2021-09-29 09:12:03 +0000472 }
473
Manuel Pégourié-Gonnardaefc5932024-08-12 10:36:40 +0200474 /* This is used by ssl_tls13_validate_certificate() */
Gilles Peskine449bd832023-01-11 14:50:10 +0100475 if (certificate_list_len == 0) {
XiaokangQianc3017f62022-05-13 05:55:41 +0000476 ssl->session_negotiate->peer_cert = NULL;
477 ret = 0;
478 goto exit;
479 }
480
Gilles Peskine449bd832023-01-11 14:50:10 +0100481 if ((ssl->session_negotiate->peer_cert =
482 mbedtls_calloc(1, sizeof(mbedtls_x509_crt))) == NULL) {
483 MBEDTLS_SSL_DEBUG_MSG(1, ("alloc( %" MBEDTLS_PRINTF_SIZET " bytes ) failed",
484 sizeof(mbedtls_x509_crt)));
485 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR,
486 MBEDTLS_ERR_SSL_ALLOC_FAILED);
487 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
Xiaofei Bai947571e2021-09-29 09:12:03 +0000488 }
489
Gilles Peskine449bd832023-01-11 14:50:10 +0100490 mbedtls_x509_crt_init(ssl->session_negotiate->peer_cert);
Xiaofei Bai947571e2021-09-29 09:12:03 +0000491
Gilles Peskine449bd832023-01-11 14:50:10 +0100492 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, certificate_list_len);
Xiaofei Bai947571e2021-09-29 09:12:03 +0000493 certificate_list_end = p + certificate_list_len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100494 while (p < certificate_list_end) {
Xiaofei Bai947571e2021-09-29 09:12:03 +0000495 size_t cert_data_len, extensions_len;
Jerry Yu2eaa7602022-08-04 17:28:15 +0800496 const unsigned char *extensions_end;
Xiaofei Bai947571e2021-09-29 09:12:03 +0000497
Gilles Peskine449bd832023-01-11 14:50:10 +0100498 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, certificate_list_end, 3);
499 cert_data_len = MBEDTLS_GET_UINT24_BE(p, 0);
Xiaofei Bai947571e2021-09-29 09:12:03 +0000500 p += 3;
501
502 /* In theory, the CRT can be up to 2^24 Bytes, but we don't support
503 * anything beyond 2^16 = 64K. Otherwise as in the TLS 1.2 code,
504 * check that we have a minimum of 128 bytes of data, this is not
505 * clear why we need that though.
506 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100507 if ((cert_data_len < 128) || (cert_data_len >= 0x10000)) {
508 MBEDTLS_SSL_DEBUG_MSG(1, ("bad Certificate message"));
509 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR,
510 MBEDTLS_ERR_SSL_DECODE_ERROR);
511 return MBEDTLS_ERR_SSL_DECODE_ERROR;
Xiaofei Bai947571e2021-09-29 09:12:03 +0000512 }
513
Gilles Peskine449bd832023-01-11 14:50:10 +0100514 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, certificate_list_end, cert_data_len);
515 ret = mbedtls_x509_crt_parse_der(ssl->session_negotiate->peer_cert,
516 p, cert_data_len);
Xiaofei Bai947571e2021-09-29 09:12:03 +0000517
Gilles Peskine449bd832023-01-11 14:50:10 +0100518 switch (ret) {
Xiaofei Bai947571e2021-09-29 09:12:03 +0000519 case 0: /*ok*/
520 break;
Gilles Peskinea7e14dc2024-09-16 13:10:11 +0200521 case MBEDTLS_ERR_OID_NOT_FOUND:
Xiaofei Bai947571e2021-09-29 09:12:03 +0000522 /* Ignore certificate with an unknown algorithm: maybe a
523 prior certificate was already trusted. */
524 break;
525
526 case MBEDTLS_ERR_X509_ALLOC_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100527 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR,
528 MBEDTLS_ERR_X509_ALLOC_FAILED);
529 MBEDTLS_SSL_DEBUG_RET(1, " mbedtls_x509_crt_parse_der", ret);
530 return ret;
Xiaofei Bai947571e2021-09-29 09:12:03 +0000531
532 case MBEDTLS_ERR_X509_UNKNOWN_VERSION:
Gilles Peskine449bd832023-01-11 14:50:10 +0100533 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT,
534 MBEDTLS_ERR_X509_UNKNOWN_VERSION);
535 MBEDTLS_SSL_DEBUG_RET(1, " mbedtls_x509_crt_parse_der", ret);
536 return ret;
Xiaofei Bai947571e2021-09-29 09:12:03 +0000537
538 default:
Gilles Peskine449bd832023-01-11 14:50:10 +0100539 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_BAD_CERT,
540 ret);
541 MBEDTLS_SSL_DEBUG_RET(1, " mbedtls_x509_crt_parse_der", ret);
542 return ret;
Xiaofei Bai947571e2021-09-29 09:12:03 +0000543 }
544
545 p += cert_data_len;
546
547 /* Certificate extensions length */
Gilles Peskine449bd832023-01-11 14:50:10 +0100548 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, certificate_list_end, 2);
549 extensions_len = MBEDTLS_GET_UINT16_BE(p, 0);
Xiaofei Bai947571e2021-09-29 09:12:03 +0000550 p += 2;
Gilles Peskine449bd832023-01-11 14:50:10 +0100551 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, certificate_list_end, extensions_len);
Jerry Yu2eaa7602022-08-04 17:28:15 +0800552
553 extensions_end = p + extensions_len;
Jerry Yu0d5cfb72022-10-31 14:15:48 +0800554 handshake->received_extensions = MBEDTLS_SSL_EXT_MASK_NONE;
Jerry Yu2eaa7602022-08-04 17:28:15 +0800555
Gilles Peskine449bd832023-01-11 14:50:10 +0100556 while (p < extensions_end) {
Jerry Yu2eaa7602022-08-04 17:28:15 +0800557 unsigned int extension_type;
558 size_t extension_data_len;
559
560 /*
Gilles Peskine449bd832023-01-11 14:50:10 +0100561 * struct {
562 * ExtensionType extension_type; (2 bytes)
563 * opaque extension_data<0..2^16-1>;
564 * } Extension;
565 */
566 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, extensions_end, 4);
567 extension_type = MBEDTLS_GET_UINT16_BE(p, 0);
568 extension_data_len = MBEDTLS_GET_UINT16_BE(p, 2);
Jerry Yu2eaa7602022-08-04 17:28:15 +0800569 p += 4;
570
Gilles Peskine449bd832023-01-11 14:50:10 +0100571 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, extensions_end, extension_data_len);
Jerry Yu2eaa7602022-08-04 17:28:15 +0800572
Jerry Yuc4bf5d62022-10-29 09:08:47 +0800573 ret = mbedtls_ssl_tls13_check_received_extension(
Gilles Peskine449bd832023-01-11 14:50:10 +0100574 ssl, MBEDTLS_SSL_HS_CERTIFICATE, extension_type,
575 MBEDTLS_SSL_TLS1_3_ALLOWED_EXTS_OF_CT);
576 if (ret != 0) {
577 return ret;
578 }
Jerry Yu0c354a22022-08-29 15:25:36 +0800579
Gilles Peskine449bd832023-01-11 14:50:10 +0100580 switch (extension_type) {
Jerry Yu2eaa7602022-08-04 17:28:15 +0800581 default:
Jerry Yu79aa7212022-11-08 21:30:21 +0800582 MBEDTLS_SSL_PRINT_EXT(
Jerry Yu0d5cfb72022-10-31 14:15:48 +0800583 3, MBEDTLS_SSL_HS_CERTIFICATE,
Gilles Peskine449bd832023-01-11 14:50:10 +0100584 extension_type, "( ignored )");
Jerry Yu2eaa7602022-08-04 17:28:15 +0800585 break;
586 }
587
588 p += extension_data_len;
589 }
590
Gilles Peskine449bd832023-01-11 14:50:10 +0100591 MBEDTLS_SSL_PRINT_EXTS(3, MBEDTLS_SSL_HS_CERTIFICATE,
592 handshake->received_extensions);
Xiaofei Bai947571e2021-09-29 09:12:03 +0000593 }
594
XiaokangQian63e713e2022-05-15 04:26:57 +0000595exit:
Xiaofei Bai947571e2021-09-29 09:12:03 +0000596 /* Check that all the message is consumed. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100597 if (p != end) {
598 MBEDTLS_SSL_DEBUG_MSG(1, ("bad Certificate message"));
599 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR,
600 MBEDTLS_ERR_SSL_DECODE_ERROR);
601 return MBEDTLS_ERR_SSL_DECODE_ERROR;
Xiaofei Bai947571e2021-09-29 09:12:03 +0000602 }
603
Xiaokang Qian73437382023-03-29 08:24:12 +0000604 MBEDTLS_SSL_DEBUG_CRT(3, "peer certificate",
605 ssl->session_negotiate->peer_cert);
Xiaofei Bai947571e2021-09-29 09:12:03 +0000606
Gilles Peskine449bd832023-01-11 14:50:10 +0100607 return ret;
Xiaofei Bai947571e2021-09-29 09:12:03 +0000608}
609#else
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200610MBEDTLS_CHECK_RETURN_CRITICAL
Ronald Crone3dac4a2022-06-10 17:21:51 +0200611MBEDTLS_STATIC_TESTABLE
Gilles Peskine449bd832023-01-11 14:50:10 +0100612int mbedtls_ssl_tls13_parse_certificate(mbedtls_ssl_context *ssl,
613 const unsigned char *buf,
614 const unsigned char *end)
Xiaofei Bai947571e2021-09-29 09:12:03 +0000615{
616 ((void) ssl);
617 ((void) buf);
618 ((void) end);
Gilles Peskine449bd832023-01-11 14:50:10 +0100619 return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Xiaofei Bai947571e2021-09-29 09:12:03 +0000620}
621#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Ronald Cronde08cf32022-10-04 17:15:35 +0200622#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED */
Xiaofei Bai947571e2021-09-29 09:12:03 +0000623
Ronald Cronde08cf32022-10-04 17:15:35 +0200624#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED)
Xiaofei Bai947571e2021-09-29 09:12:03 +0000625#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Xiaofei Bai79595ac2021-10-26 07:16:45 +0000626/* Validate certificate chain sent by the server. */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200627MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100628static int ssl_tls13_validate_certificate(mbedtls_ssl_context *ssl)
Xiaofei Bai947571e2021-09-29 09:12:03 +0000629{
Manuel Pégourié-Gonnard58ab9ba2024-08-14 09:47:38 +0200630 /* Authmode: precedence order is SNI if used else configuration */
631#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
632 const int authmode = ssl->handshake->sni_authmode != MBEDTLS_SSL_VERIFY_UNSET
633 ? ssl->handshake->sni_authmode
634 : ssl->conf->authmode;
635#else
636 const int authmode = ssl->conf->authmode;
XiaokangQian6b916b12022-04-25 07:29:34 +0000637#endif
638
639 /*
XiaokangQian989f06d2022-05-17 01:50:15 +0000640 * If the peer hasn't sent a certificate ( i.e. it sent
XiaokangQian6b916b12022-04-25 07:29:34 +0000641 * an empty certificate chain ), this is reflected in the peer CRT
642 * structure being unset.
643 * Check for that and handle it depending on the
XiaokangQian989f06d2022-05-17 01:50:15 +0000644 * authentication mode.
XiaokangQian6b916b12022-04-25 07:29:34 +0000645 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100646 if (ssl->session_negotiate->peer_cert == NULL) {
647 MBEDTLS_SSL_DEBUG_MSG(1, ("peer has no certificate"));
XiaokangQian989f06d2022-05-17 01:50:15 +0000648
XiaokangQian63e713e2022-05-15 04:26:57 +0000649#if defined(MBEDTLS_SSL_SRV_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100650 if (ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER) {
XiaokangQian63e713e2022-05-15 04:26:57 +0000651 /* The client was asked for a certificate but didn't send
652 * one. The client should know what's going on, so we
653 * don't send an alert.
654 */
655 ssl->session_negotiate->verify_result = MBEDTLS_X509_BADCERT_MISSING;
Gilles Peskine449bd832023-01-11 14:50:10 +0100656 if (authmode == MBEDTLS_SSL_VERIFY_OPTIONAL) {
657 return 0;
658 } else {
Xiaokang Qian73437382023-03-29 08:24:12 +0000659 MBEDTLS_SSL_PEND_FATAL_ALERT(
660 MBEDTLS_SSL_ALERT_MSG_NO_CERT,
661 MBEDTLS_ERR_SSL_NO_CLIENT_CERTIFICATE);
Gilles Peskine449bd832023-01-11 14:50:10 +0100662 return MBEDTLS_ERR_SSL_NO_CLIENT_CERTIFICATE;
XiaokangQian989f06d2022-05-17 01:50:15 +0000663 }
XiaokangQian63e713e2022-05-15 04:26:57 +0000664 }
XiaokangQian6b916b12022-04-25 07:29:34 +0000665#endif /* MBEDTLS_SSL_SRV_C */
666
XiaokangQianc3017f62022-05-13 05:55:41 +0000667#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnardaefc5932024-08-12 10:36:40 +0200668 /* Regardless of authmode, the server is not allowed to send an empty
669 * certificate chain. (Last paragraph before 4.4.2.1 in RFC 8446: "The
670 * server's certificate_list MUST always be non-empty.") With authmode
671 * optional/none, we continue the handshake if we can't validate the
672 * server's cert, but we still break it if no certificate was sent. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100673 if (ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT) {
674 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_NO_CERT,
675 MBEDTLS_ERR_SSL_FATAL_ALERT_MESSAGE);
676 return MBEDTLS_ERR_SSL_FATAL_ALERT_MESSAGE;
XiaokangQian63e713e2022-05-15 04:26:57 +0000677 }
XiaokangQianc3017f62022-05-13 05:55:41 +0000678#endif /* MBEDTLS_SSL_CLI_C */
XiaokangQian63e713e2022-05-15 04:26:57 +0000679 }
XiaokangQian6b916b12022-04-25 07:29:34 +0000680
Manuel Pégourié-Gonnard19dd9f52024-08-16 11:03:42 +0200681 return mbedtls_ssl_verify_certificate(ssl, authmode,
682 ssl->session_negotiate->peer_cert,
683 NULL, NULL);
Xiaofei Bai947571e2021-09-29 09:12:03 +0000684}
685#else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200686MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100687static int ssl_tls13_validate_certificate(mbedtls_ssl_context *ssl)
Xiaofei Bai947571e2021-09-29 09:12:03 +0000688{
689 ((void) ssl);
Gilles Peskine449bd832023-01-11 14:50:10 +0100690 return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Xiaofei Bai947571e2021-09-29 09:12:03 +0000691}
692#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Ronald Cronde08cf32022-10-04 17:15:35 +0200693#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED */
Xiaofei Bai947571e2021-09-29 09:12:03 +0000694
Gilles Peskine449bd832023-01-11 14:50:10 +0100695int mbedtls_ssl_tls13_process_certificate(mbedtls_ssl_context *ssl)
Xiaofei Bai947571e2021-09-29 09:12:03 +0000696{
Xiaofei Bai79595ac2021-10-26 07:16:45 +0000697 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine449bd832023-01-11 14:50:10 +0100698 MBEDTLS_SSL_DEBUG_MSG(2, ("=> parse certificate"));
Xiaofei Bai79595ac2021-10-26 07:16:45 +0000699
Ronald Cronde08cf32022-10-04 17:15:35 +0200700#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED)
XiaokangQianc3017f62022-05-13 05:55:41 +0000701 unsigned char *buf;
702 size_t buf_len;
Xiaofei Bai79595ac2021-10-26 07:16:45 +0000703
Gilles Peskine449bd832023-01-11 14:50:10 +0100704 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_tls13_fetch_handshake_msg(
705 ssl, MBEDTLS_SSL_HS_CERTIFICATE,
706 &buf, &buf_len));
Xiaofei Bai79595ac2021-10-26 07:16:45 +0000707
XiaokangQianc3017f62022-05-13 05:55:41 +0000708 /* Parse the certificate chain sent by the peer. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100709 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_tls13_parse_certificate(ssl, buf,
710 buf + buf_len));
XiaokangQianc3017f62022-05-13 05:55:41 +0000711 /* Validate the certificate chain and set the verification results. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100712 MBEDTLS_SSL_PROC_CHK(ssl_tls13_validate_certificate(ssl));
Xiaofei Bai79595ac2021-10-26 07:16:45 +0000713
Xiaokang Qian73437382023-03-29 08:24:12 +0000714 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_add_hs_msg_to_checksum(
715 ssl, MBEDTLS_SSL_HS_CERTIFICATE, buf, buf_len));
Xiaofei Bai79595ac2021-10-26 07:16:45 +0000716
Xiaofei Bai79595ac2021-10-26 07:16:45 +0000717cleanup:
Gilles Peskineff2558a2023-09-05 21:10:39 +0200718#else /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED */
719 (void) ssl;
Ronald Cronde08cf32022-10-04 17:15:35 +0200720#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED */
Xiaofei Bai79595ac2021-10-26 07:16:45 +0000721
Gilles Peskine449bd832023-01-11 14:50:10 +0100722 MBEDTLS_SSL_DEBUG_MSG(2, ("<= parse certificate"));
723 return ret;
Xiaofei Bai947571e2021-09-29 09:12:03 +0000724}
Ronald Cron928cbd32022-10-04 16:14:26 +0200725#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED)
Jerry Yu7399d0d2022-01-30 17:54:19 +0800726/*
727 * enum {
728 * X509(0),
729 * RawPublicKey(2),
730 * (255)
731 * } CertificateType;
732 *
733 * struct {
734 * select (certificate_type) {
735 * case RawPublicKey:
736 * // From RFC 7250 ASN.1_subjectPublicKeyInfo
737 * opaque ASN1_subjectPublicKeyInfo<1..2^24-1>;
738 *
739 * case X509:
740 * opaque cert_data<1..2^24-1>;
741 * };
742 * Extension extensions<0..2^16-1>;
743 * } CertificateEntry;
744 *
745 * struct {
746 * opaque certificate_request_context<0..2^8-1>;
747 * CertificateEntry certificate_list<0..2^24-1>;
748 * } Certificate;
749 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200750MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100751static int ssl_tls13_write_certificate_body(mbedtls_ssl_context *ssl,
752 unsigned char *buf,
753 unsigned char *end,
754 size_t *out_len)
Jerry Yu5cc35062022-01-28 16:16:08 +0800755{
Gilles Peskine449bd832023-01-11 14:50:10 +0100756 const mbedtls_x509_crt *crt = mbedtls_ssl_own_cert(ssl);
Jerry Yu3e536442022-02-15 11:05:59 +0800757 unsigned char *p = buf;
Jerry Yuc8d8d4e2022-02-18 12:10:03 +0800758 unsigned char *certificate_request_context =
Gilles Peskine449bd832023-01-11 14:50:10 +0100759 ssl->handshake->certificate_request_context;
Jerry Yuc8d8d4e2022-02-18 12:10:03 +0800760 unsigned char certificate_request_context_len =
Gilles Peskine449bd832023-01-11 14:50:10 +0100761 ssl->handshake->certificate_request_context_len;
Jerry Yuc8d8d4e2022-02-18 12:10:03 +0800762 unsigned char *p_certificate_list_len;
Jerry Yu5cc35062022-01-28 16:16:08 +0800763
Jerry Yu5cc35062022-01-28 16:16:08 +0800764
Jerry Yu3391ac02022-02-16 11:21:37 +0800765 /* ...
766 * opaque certificate_request_context<0..2^8-1>;
767 * ...
768 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100769 MBEDTLS_SSL_CHK_BUF_PTR(p, end, certificate_request_context_len + 1);
Jerry Yuc8d8d4e2022-02-18 12:10:03 +0800770 *p++ = certificate_request_context_len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100771 if (certificate_request_context_len > 0) {
772 memcpy(p, certificate_request_context, certificate_request_context_len);
Jerry Yuc8d8d4e2022-02-18 12:10:03 +0800773 p += certificate_request_context_len;
Jerry Yu537530d2022-02-15 14:00:57 +0800774 }
775
Jerry Yu3391ac02022-02-16 11:21:37 +0800776 /* ...
777 * CertificateEntry certificate_list<0..2^24-1>;
778 * ...
779 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100780 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 3);
Jerry Yuc8d8d4e2022-02-18 12:10:03 +0800781 p_certificate_list_len = p;
Jerry Yu3e536442022-02-15 11:05:59 +0800782 p += 3;
783
Gilles Peskine449bd832023-01-11 14:50:10 +0100784 MBEDTLS_SSL_DEBUG_CRT(3, "own certificate", crt);
Jerry Yu5cc35062022-01-28 16:16:08 +0800785
Gilles Peskine449bd832023-01-11 14:50:10 +0100786 while (crt != NULL) {
Jerry Yu7399d0d2022-01-30 17:54:19 +0800787 size_t cert_data_len = crt->raw.len;
Jerry Yu5cc35062022-01-28 16:16:08 +0800788
Gilles Peskine449bd832023-01-11 14:50:10 +0100789 MBEDTLS_SSL_CHK_BUF_PTR(p, end, cert_data_len + 3 + 2);
790 MBEDTLS_PUT_UINT24_BE(cert_data_len, p, 0);
Jerry Yu7399d0d2022-01-30 17:54:19 +0800791 p += 3;
Jerry Yu5cc35062022-01-28 16:16:08 +0800792
Gilles Peskine449bd832023-01-11 14:50:10 +0100793 memcpy(p, crt->raw.p, cert_data_len);
Jerry Yu7399d0d2022-01-30 17:54:19 +0800794 p += cert_data_len;
795 crt = crt->next;
Jerry Yu5cc35062022-01-28 16:16:08 +0800796
797 /* Currently, we don't have any certificate extensions defined.
798 * Hence, we are sending an empty extension with length zero.
799 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100800 MBEDTLS_PUT_UINT16_BE(0, p, 0);
Jerry Yu7399d0d2022-01-30 17:54:19 +0800801 p += 2;
Jerry Yu5cc35062022-01-28 16:16:08 +0800802 }
Jerry Yu5cc35062022-01-28 16:16:08 +0800803
Gilles Peskine449bd832023-01-11 14:50:10 +0100804 MBEDTLS_PUT_UINT24_BE(p - p_certificate_list_len - 3,
805 p_certificate_list_len, 0);
Jerry Yu7399d0d2022-01-30 17:54:19 +0800806
Jerry Yu3e536442022-02-15 11:05:59 +0800807 *out_len = p - buf;
Jerry Yu5cc35062022-01-28 16:16:08 +0800808
Jerry Yu7de2ff02022-11-08 21:43:46 +0800809 MBEDTLS_SSL_PRINT_EXTS(
Gilles Peskine449bd832023-01-11 14:50:10 +0100810 3, MBEDTLS_SSL_HS_CERTIFICATE, ssl->handshake->sent_extensions);
Jerry Yu4b8f2f72022-10-31 13:31:22 +0800811
Gilles Peskine449bd832023-01-11 14:50:10 +0100812 return 0;
Jerry Yu5cc35062022-01-28 16:16:08 +0800813}
Jerry Yu5cc35062022-01-28 16:16:08 +0800814
Gilles Peskine449bd832023-01-11 14:50:10 +0100815int mbedtls_ssl_tls13_write_certificate(mbedtls_ssl_context *ssl)
Jerry Yu5cc35062022-01-28 16:16:08 +0800816{
817 int ret;
Ronald Cron5bb8fc82022-03-09 07:00:13 +0100818 unsigned char *buf;
819 size_t buf_len, msg_len;
820
Gilles Peskine449bd832023-01-11 14:50:10 +0100821 MBEDTLS_SSL_DEBUG_MSG(2, ("=> write certificate"));
Jerry Yu5cc35062022-01-28 16:16:08 +0800822
Xiaokang Qian73437382023-03-29 08:24:12 +0000823 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_start_handshake_msg(
824 ssl, MBEDTLS_SSL_HS_CERTIFICATE, &buf, &buf_len));
Jerry Yu5cc35062022-01-28 16:16:08 +0800825
Gilles Peskine449bd832023-01-11 14:50:10 +0100826 MBEDTLS_SSL_PROC_CHK(ssl_tls13_write_certificate_body(ssl,
827 buf,
828 buf + buf_len,
829 &msg_len));
Jerry Yu5cc35062022-01-28 16:16:08 +0800830
Xiaokang Qian73437382023-03-29 08:24:12 +0000831 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_add_hs_msg_to_checksum(
832 ssl, MBEDTLS_SSL_HS_CERTIFICATE, buf, msg_len));
Jerry Yu5cc35062022-01-28 16:16:08 +0800833
Gilles Peskine449bd832023-01-11 14:50:10 +0100834 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_finish_handshake_msg(
835 ssl, buf_len, msg_len));
Jerry Yu5cc35062022-01-28 16:16:08 +0800836cleanup:
837
Gilles Peskine449bd832023-01-11 14:50:10 +0100838 MBEDTLS_SSL_DEBUG_MSG(2, ("<= write certificate"));
839 return ret;
Jerry Yu5cc35062022-01-28 16:16:08 +0800840}
841
Jerry Yu3e536442022-02-15 11:05:59 +0800842/*
843 * STATE HANDLING: Output Certificate Verify
844 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100845int mbedtls_ssl_tls13_check_sig_alg_cert_key_match(uint16_t sig_alg,
846 mbedtls_pk_context *key)
Jerry Yu0c6be8f2022-06-20 20:42:00 +0800847{
Dave Rodgman2eab4622023-10-05 13:30:37 +0100848 mbedtls_pk_type_t pk_type = (mbedtls_pk_type_t) mbedtls_ssl_sig_from_pk(key);
Gilles Peskine449bd832023-01-11 14:50:10 +0100849 size_t key_size = mbedtls_pk_get_bitlen(key);
Jerry Yu0c6be8f2022-06-20 20:42:00 +0800850
Gilles Peskine449bd832023-01-11 14:50:10 +0100851 switch (pk_type) {
Jerry Yu67eced02022-02-25 13:37:36 +0800852 case MBEDTLS_SSL_SIG_ECDSA:
Gilles Peskine449bd832023-01-11 14:50:10 +0100853 switch (key_size) {
Jerry Yu67eced02022-02-25 13:37:36 +0800854 case 256:
Gilles Peskine449bd832023-01-11 14:50:10 +0100855 return
856 sig_alg == MBEDTLS_TLS1_3_SIG_ECDSA_SECP256R1_SHA256;
Jerry Yu0c6be8f2022-06-20 20:42:00 +0800857
Jerry Yu67eced02022-02-25 13:37:36 +0800858 case 384:
Gilles Peskine449bd832023-01-11 14:50:10 +0100859 return
860 sig_alg == MBEDTLS_TLS1_3_SIG_ECDSA_SECP384R1_SHA384;
Jerry Yu0c6be8f2022-06-20 20:42:00 +0800861
Jerry Yu67eced02022-02-25 13:37:36 +0800862 case 521:
Gilles Peskine449bd832023-01-11 14:50:10 +0100863 return
864 sig_alg == MBEDTLS_TLS1_3_SIG_ECDSA_SECP521R1_SHA512;
Jerry Yu67eced02022-02-25 13:37:36 +0800865 default:
Jerry Yu67eced02022-02-25 13:37:36 +0800866 break;
867 }
868 break;
Jerry Yu67eced02022-02-25 13:37:36 +0800869
Jerry Yu67eced02022-02-25 13:37:36 +0800870 case MBEDTLS_SSL_SIG_RSA:
Gilles Peskine449bd832023-01-11 14:50:10 +0100871 switch (sig_alg) {
Ronald Cron38391bf2022-09-16 11:19:27 +0200872 case MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA256: /* Intentional fallthrough */
873 case MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA384: /* Intentional fallthrough */
Jerry Yu0c6be8f2022-06-20 20:42:00 +0800874 case MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA512:
Gilles Peskine449bd832023-01-11 14:50:10 +0100875 return 1;
Jerry Yuc2e04932022-06-27 22:13:03 +0800876
Jerry Yu0c6be8f2022-06-20 20:42:00 +0800877 default:
878 break;
Jerry Yucef3f332022-03-22 23:00:13 +0800879 }
Jerry Yu67eced02022-02-25 13:37:36 +0800880 break;
Jerry Yu0c6be8f2022-06-20 20:42:00 +0800881
Jerry Yu67eced02022-02-25 13:37:36 +0800882 default:
Jerry Yu67eced02022-02-25 13:37:36 +0800883 break;
884 }
Jerry Yu0c6be8f2022-06-20 20:42:00 +0800885
Gilles Peskine449bd832023-01-11 14:50:10 +0100886 return 0;
Jerry Yu0c6be8f2022-06-20 20:42:00 +0800887}
888
Ronald Cronce7d76e2022-07-08 18:56:49 +0200889MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100890static int ssl_tls13_write_certificate_verify_body(mbedtls_ssl_context *ssl,
891 unsigned char *buf,
892 unsigned char *end,
893 size_t *out_len)
Jerry Yu8511f122022-01-29 10:01:04 +0800894{
Ronald Cron067a1e72022-09-16 13:44:49 +0200895 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yu3e536442022-02-15 11:05:59 +0800896 unsigned char *p = buf;
Jerry Yu8511f122022-01-29 10:01:04 +0800897 mbedtls_pk_context *own_key;
Jerry Yu3e536442022-02-15 11:05:59 +0800898
Gilles Peskine449bd832023-01-11 14:50:10 +0100899 unsigned char handshake_hash[MBEDTLS_TLS1_3_MD_MAX_SIZE];
Jerry Yu8511f122022-01-29 10:01:04 +0800900 size_t handshake_hash_len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100901 unsigned char verify_buffer[SSL_VERIFY_STRUCT_MAX_SIZE];
Jerry Yu3e536442022-02-15 11:05:59 +0800902 size_t verify_buffer_len;
Ronald Cron067a1e72022-09-16 13:44:49 +0200903
904 uint16_t *sig_alg = ssl->handshake->received_sig_algs;
Jerry Yu3e536442022-02-15 11:05:59 +0800905 size_t signature_len = 0;
Jerry Yu8511f122022-01-29 10:01:04 +0800906
Jerry Yu0b7b1012022-02-23 12:23:05 +0800907 *out_len = 0;
908
Gilles Peskine449bd832023-01-11 14:50:10 +0100909 own_key = mbedtls_ssl_own_key(ssl);
910 if (own_key == NULL) {
911 MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
912 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Jerry Yu8511f122022-01-29 10:01:04 +0800913 }
914
Xiaokang Qian73437382023-03-29 08:24:12 +0000915 ret = mbedtls_ssl_get_handshake_transcript(
Dave Rodgman2eab4622023-10-05 13:30:37 +0100916 ssl, (mbedtls_md_type_t) ssl->handshake->ciphersuite_info->mac,
Xiaokang Qian73437382023-03-29 08:24:12 +0000917 handshake_hash, sizeof(handshake_hash), &handshake_hash_len);
Gilles Peskine449bd832023-01-11 14:50:10 +0100918 if (ret != 0) {
919 return ret;
920 }
Jerry Yu8511f122022-01-29 10:01:04 +0800921
Gilles Peskine449bd832023-01-11 14:50:10 +0100922 MBEDTLS_SSL_DEBUG_BUF(3, "handshake hash",
923 handshake_hash,
924 handshake_hash_len);
Jerry Yu8511f122022-01-29 10:01:04 +0800925
Gilles Peskine449bd832023-01-11 14:50:10 +0100926 ssl_tls13_create_verify_structure(handshake_hash, handshake_hash_len,
927 verify_buffer, &verify_buffer_len,
928 ssl->conf->endpoint);
Jerry Yu8511f122022-01-29 10:01:04 +0800929
930 /*
931 * struct {
932 * SignatureScheme algorithm;
933 * opaque signature<0..2^16-1>;
934 * } CertificateVerify;
935 */
Ronald Cron067a1e72022-09-16 13:44:49 +0200936 /* Check there is space for the algorithm identifier (2 bytes) and the
937 * signature length (2 bytes).
938 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100939 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 4);
Ronald Cron067a1e72022-09-16 13:44:49 +0200940
Gilles Peskine449bd832023-01-11 14:50:10 +0100941 for (; *sig_alg != MBEDTLS_TLS1_3_SIG_NONE; sig_alg++) {
Ronald Cron067a1e72022-09-16 13:44:49 +0200942 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
943 mbedtls_pk_type_t pk_type = MBEDTLS_PK_NONE;
944 mbedtls_md_type_t md_alg = MBEDTLS_MD_NONE;
945 psa_algorithm_t psa_algorithm = PSA_ALG_NONE;
946 unsigned char verify_hash[PSA_HASH_MAX_SIZE];
947 size_t verify_hash_len;
Jerry Yu67eced02022-02-25 13:37:36 +0800948
Gilles Peskine449bd832023-01-11 14:50:10 +0100949 if (!mbedtls_ssl_sig_alg_is_offered(ssl, *sig_alg)) {
Ronald Cron067a1e72022-09-16 13:44:49 +0200950 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +0100951 }
Jerry Yu67eced02022-02-25 13:37:36 +0800952
Gilles Peskine449bd832023-01-11 14:50:10 +0100953 if (!mbedtls_ssl_tls13_sig_alg_for_cert_verify_is_supported(*sig_alg)) {
Ronald Cron067a1e72022-09-16 13:44:49 +0200954 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +0100955 }
Ronald Cron067a1e72022-09-16 13:44:49 +0200956
Gilles Peskine449bd832023-01-11 14:50:10 +0100957 if (!mbedtls_ssl_tls13_check_sig_alg_cert_key_match(*sig_alg, own_key)) {
Ronald Cron067a1e72022-09-16 13:44:49 +0200958 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +0100959 }
Ronald Cron067a1e72022-09-16 13:44:49 +0200960
Gilles Peskine449bd832023-01-11 14:50:10 +0100961 if (mbedtls_ssl_get_pk_type_and_md_alg_from_sig_alg(
962 *sig_alg, &pk_type, &md_alg) != 0) {
963 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Ronald Cron067a1e72022-09-16 13:44:49 +0200964 }
965
966 /* Hash verify buffer with indicated hash function */
Manuel Pégourié-Gonnard2d6d9932023-03-28 11:38:08 +0200967 psa_algorithm = mbedtls_md_psa_alg_from_type(md_alg);
Gilles Peskine449bd832023-01-11 14:50:10 +0100968 status = psa_hash_compute(psa_algorithm,
969 verify_buffer,
970 verify_buffer_len,
971 verify_hash, sizeof(verify_hash),
972 &verify_hash_len);
973 if (status != PSA_SUCCESS) {
Andrzej Kurek8a045ce2022-12-23 11:00:06 -0500974 return PSA_TO_MBEDTLS_ERR(status);
Ronald Cron067a1e72022-09-16 13:44:49 +0200975 }
976
Gilles Peskine449bd832023-01-11 14:50:10 +0100977 MBEDTLS_SSL_DEBUG_BUF(3, "verify hash", verify_hash, verify_hash_len);
978
979 if ((ret = mbedtls_pk_sign_ext(pk_type, own_key,
980 md_alg, verify_hash, verify_hash_len,
Ben Taylor440cb2a2025-03-05 09:40:08 +0000981 p + 4, (size_t) (end - (p + 4)), &signature_len)) != 0) {
Gilles Peskine449bd832023-01-11 14:50:10 +0100982 MBEDTLS_SSL_DEBUG_MSG(2, ("CertificateVerify signature failed with %s",
983 mbedtls_ssl_sig_alg_to_str(*sig_alg)));
984 MBEDTLS_SSL_DEBUG_RET(2, "mbedtls_pk_sign_ext", ret);
985
986 /* The signature failed. This is possible if the private key
987 * was not suitable for the signature operation as purposely we
988 * did not check its suitability completely. Let's try with
989 * another signature algorithm.
990 */
991 continue;
992 }
993
994 MBEDTLS_SSL_DEBUG_MSG(2, ("CertificateVerify signature with %s",
995 mbedtls_ssl_sig_alg_to_str(*sig_alg)));
Ronald Cron067a1e72022-09-16 13:44:49 +0200996
997 break;
998 }
999
Gilles Peskine449bd832023-01-11 14:50:10 +01001000 if (*sig_alg == MBEDTLS_TLS1_3_SIG_NONE) {
1001 MBEDTLS_SSL_DEBUG_MSG(1, ("no suitable signature algorithm"));
1002 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE,
1003 MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE);
1004 return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
Jerry Yu8511f122022-01-29 10:01:04 +08001005 }
1006
Gilles Peskine449bd832023-01-11 14:50:10 +01001007 MBEDTLS_PUT_UINT16_BE(*sig_alg, p, 0);
1008 MBEDTLS_PUT_UINT16_BE(signature_len, p, 2);
Jerry Yuf3b46b52022-06-19 16:52:27 +08001009
Ronald Cron067a1e72022-09-16 13:44:49 +02001010 *out_len = 4 + signature_len;
Jerry Yu8c338862022-03-23 13:34:04 +08001011
Gilles Peskine449bd832023-01-11 14:50:10 +01001012 return 0;
Jerry Yu8511f122022-01-29 10:01:04 +08001013}
Jerry Yu8511f122022-01-29 10:01:04 +08001014
Gilles Peskine449bd832023-01-11 14:50:10 +01001015int mbedtls_ssl_tls13_write_certificate_verify(mbedtls_ssl_context *ssl)
Jerry Yu8511f122022-01-29 10:01:04 +08001016{
1017 int ret = 0;
Jerry Yuca133a32022-02-15 14:22:05 +08001018 unsigned char *buf;
1019 size_t buf_len, msg_len;
1020
Gilles Peskine449bd832023-01-11 14:50:10 +01001021 MBEDTLS_SSL_DEBUG_MSG(2, ("=> write certificate verify"));
Jerry Yu8511f122022-01-29 10:01:04 +08001022
Xiaokang Qian73437382023-03-29 08:24:12 +00001023 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_start_handshake_msg(
1024 ssl, MBEDTLS_SSL_HS_CERTIFICATE_VERIFY,
1025 &buf, &buf_len));
Jerry Yu8511f122022-01-29 10:01:04 +08001026
Gilles Peskine449bd832023-01-11 14:50:10 +01001027 MBEDTLS_SSL_PROC_CHK(ssl_tls13_write_certificate_verify_body(
1028 ssl, buf, buf + buf_len, &msg_len));
Jerry Yu8511f122022-01-29 10:01:04 +08001029
Xiaokang Qian73437382023-03-29 08:24:12 +00001030 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_add_hs_msg_to_checksum(
1031 ssl, MBEDTLS_SSL_HS_CERTIFICATE_VERIFY,
1032 buf, msg_len));
Jerry Yu8511f122022-01-29 10:01:04 +08001033
Gilles Peskine449bd832023-01-11 14:50:10 +01001034 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_finish_handshake_msg(
1035 ssl, buf_len, msg_len));
Jerry Yu8511f122022-01-29 10:01:04 +08001036
1037cleanup:
1038
Gilles Peskine449bd832023-01-11 14:50:10 +01001039 MBEDTLS_SSL_DEBUG_MSG(2, ("<= write certificate verify"));
1040 return ret;
Jerry Yu8511f122022-01-29 10:01:04 +08001041}
1042
Ronald Cron928cbd32022-10-04 16:14:26 +02001043#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED */
Jerry Yu90f152d2022-01-29 22:12:42 +08001044
Jerry Yu5cc35062022-01-28 16:16:08 +08001045/*
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001046 *
XiaokangQianc5c39d52021-11-09 11:55:10 +00001047 * STATE HANDLING: Incoming Finished message.
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001048 */
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001049/*
1050 * Implementation
1051 */
1052
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001053MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01001054static int ssl_tls13_preprocess_finished_message(mbedtls_ssl_context *ssl)
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001055{
1056 int ret;
1057
Xiaokang Qian73437382023-03-29 08:24:12 +00001058 ret = mbedtls_ssl_tls13_calculate_verify_data(
1059 ssl,
1060 ssl->handshake->state_local.finished_in.digest,
1061 sizeof(ssl->handshake->state_local.finished_in.digest),
1062 &ssl->handshake->state_local.finished_in.digest_len,
1063 ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT ?
1064 MBEDTLS_SSL_IS_SERVER : MBEDTLS_SSL_IS_CLIENT);
Gilles Peskine449bd832023-01-11 14:50:10 +01001065 if (ret != 0) {
1066 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_tls13_calculate_verify_data", ret);
1067 return ret;
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001068 }
1069
Gilles Peskine449bd832023-01-11 14:50:10 +01001070 return 0;
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001071}
1072
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001073MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01001074static int ssl_tls13_parse_finished_message(mbedtls_ssl_context *ssl,
1075 const unsigned char *buf,
1076 const unsigned char *end)
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001077{
XiaokangQian33062842021-11-11 03:37:45 +00001078 /*
1079 * struct {
XiaokangQianc13f9352021-11-11 06:13:22 +00001080 * opaque verify_data[Hash.length];
XiaokangQian33062842021-11-11 03:37:45 +00001081 * } Finished;
1082 */
1083 const unsigned char *expected_verify_data =
1084 ssl->handshake->state_local.finished_in.digest;
1085 size_t expected_verify_data_len =
1086 ssl->handshake->state_local.finished_in.digest_len;
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001087 /* Structural validation */
Gilles Peskine449bd832023-01-11 14:50:10 +01001088 if ((size_t) (end - buf) != expected_verify_data_len) {
1089 MBEDTLS_SSL_DEBUG_MSG(1, ("bad finished message"));
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001090
Gilles Peskine449bd832023-01-11 14:50:10 +01001091 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR,
1092 MBEDTLS_ERR_SSL_DECODE_ERROR);
1093 return MBEDTLS_ERR_SSL_DECODE_ERROR;
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001094 }
1095
Gilles Peskine449bd832023-01-11 14:50:10 +01001096 MBEDTLS_SSL_DEBUG_BUF(4, "verify_data (self-computed):",
1097 expected_verify_data,
1098 expected_verify_data_len);
1099 MBEDTLS_SSL_DEBUG_BUF(4, "verify_data (received message):", buf,
1100 expected_verify_data_len);
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001101
1102 /* Semantic validation */
Gilles Peskine449bd832023-01-11 14:50:10 +01001103 if (mbedtls_ct_memcmp(buf,
1104 expected_verify_data,
1105 expected_verify_data_len) != 0) {
1106 MBEDTLS_SSL_DEBUG_MSG(1, ("bad finished message"));
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001107
Gilles Peskine449bd832023-01-11 14:50:10 +01001108 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_DECRYPT_ERROR,
1109 MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE);
1110 return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001111 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001112 return 0;
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001113}
1114
Gilles Peskine449bd832023-01-11 14:50:10 +01001115int mbedtls_ssl_tls13_process_finished_message(mbedtls_ssl_context *ssl)
XiaokangQianc5c39d52021-11-09 11:55:10 +00001116{
XiaokangQian33062842021-11-11 03:37:45 +00001117 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
XiaokangQianc5c39d52021-11-09 11:55:10 +00001118 unsigned char *buf;
Xiaofei Baieef15042021-11-18 07:29:56 +00001119 size_t buf_len;
XiaokangQianc5c39d52021-11-09 11:55:10 +00001120
Gilles Peskine449bd832023-01-11 14:50:10 +01001121 MBEDTLS_SSL_DEBUG_MSG(2, ("=> parse finished message"));
XiaokangQianc5c39d52021-11-09 11:55:10 +00001122
Xiaokang Qian73437382023-03-29 08:24:12 +00001123 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_tls13_fetch_handshake_msg(
1124 ssl, MBEDTLS_SSL_HS_FINISHED, &buf, &buf_len));
Jerry Yu0a92d6c2022-05-16 16:54:46 +08001125
1126 /* Preprocessing step: Compute handshake digest */
Gilles Peskine449bd832023-01-11 14:50:10 +01001127 MBEDTLS_SSL_PROC_CHK(ssl_tls13_preprocess_finished_message(ssl));
Jerry Yu0a92d6c2022-05-16 16:54:46 +08001128
Xiaokang Qian73437382023-03-29 08:24:12 +00001129 MBEDTLS_SSL_PROC_CHK(ssl_tls13_parse_finished_message(
1130 ssl, buf, buf + buf_len));
Jerry Yu0a92d6c2022-05-16 16:54:46 +08001131
Xiaokang Qian73437382023-03-29 08:24:12 +00001132 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_add_hs_msg_to_checksum(
1133 ssl, MBEDTLS_SSL_HS_FINISHED, buf, buf_len));
XiaokangQianc5c39d52021-11-09 11:55:10 +00001134
1135cleanup:
1136
Gilles Peskine449bd832023-01-11 14:50:10 +01001137 MBEDTLS_SSL_DEBUG_MSG(2, ("<= parse finished message"));
1138 return ret;
XiaokangQianc5c39d52021-11-09 11:55:10 +00001139}
1140
XiaokangQian74af2a82021-09-22 07:40:30 +00001141/*
1142 *
XiaokangQiancc90c942021-11-09 12:30:09 +00001143 * STATE HANDLING: Write and send Finished message.
XiaokangQian74af2a82021-09-22 07:40:30 +00001144 *
1145 */
XiaokangQian74af2a82021-09-22 07:40:30 +00001146/*
XiaokangQian35dc6252021-11-11 08:16:19 +00001147 * Implement
XiaokangQian74af2a82021-09-22 07:40:30 +00001148 */
1149
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001150MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01001151static int ssl_tls13_prepare_finished_message(mbedtls_ssl_context *ssl)
XiaokangQian74af2a82021-09-22 07:40:30 +00001152{
1153 int ret;
1154
1155 /* Compute transcript of handshake up to now. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001156 ret = mbedtls_ssl_tls13_calculate_verify_data(ssl,
1157 ssl->handshake->state_local.finished_out.digest,
1158 sizeof(ssl->handshake->state_local.finished_out.
1159 digest),
1160 &ssl->handshake->state_local.finished_out.digest_len,
1161 ssl->conf->endpoint);
XiaokangQian74af2a82021-09-22 07:40:30 +00001162
Gilles Peskine449bd832023-01-11 14:50:10 +01001163 if (ret != 0) {
1164 MBEDTLS_SSL_DEBUG_RET(1, "calculate_verify_data failed", ret);
1165 return ret;
XiaokangQian74af2a82021-09-22 07:40:30 +00001166 }
1167
Gilles Peskine449bd832023-01-11 14:50:10 +01001168 return 0;
XiaokangQian74af2a82021-09-22 07:40:30 +00001169}
1170
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001171MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01001172static int ssl_tls13_write_finished_message_body(mbedtls_ssl_context *ssl,
1173 unsigned char *buf,
1174 unsigned char *end,
1175 size_t *out_len)
XiaokangQian74af2a82021-09-22 07:40:30 +00001176{
XiaokangQian8773aa02021-11-10 07:33:09 +00001177 size_t verify_data_len = ssl->handshake->state_local.finished_out.digest_len;
XiaokangQian0fa66432021-11-15 03:33:57 +00001178 /*
1179 * struct {
1180 * opaque verify_data[Hash.length];
1181 * } Finished;
1182 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001183 MBEDTLS_SSL_CHK_BUF_PTR(buf, end, verify_data_len);
XiaokangQian74af2a82021-09-22 07:40:30 +00001184
Gilles Peskine449bd832023-01-11 14:50:10 +01001185 memcpy(buf, ssl->handshake->state_local.finished_out.digest,
1186 verify_data_len);
XiaokangQian74af2a82021-09-22 07:40:30 +00001187
Xiaofei Baid25fab62021-12-02 06:36:27 +00001188 *out_len = verify_data_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01001189 return 0;
XiaokangQian74af2a82021-09-22 07:40:30 +00001190}
XiaokangQianc5c39d52021-11-09 11:55:10 +00001191
XiaokangQian35dc6252021-11-11 08:16:19 +00001192/* Main entry point: orchestrates the other functions */
Gilles Peskine449bd832023-01-11 14:50:10 +01001193int mbedtls_ssl_tls13_write_finished_message(mbedtls_ssl_context *ssl)
XiaokangQian35dc6252021-11-11 08:16:19 +00001194{
1195 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1196 unsigned char *buf;
1197 size_t buf_len, msg_len;
1198
Gilles Peskine449bd832023-01-11 14:50:10 +01001199 MBEDTLS_SSL_DEBUG_MSG(2, ("=> write finished message"));
XiaokangQian35dc6252021-11-11 08:16:19 +00001200
Gilles Peskine449bd832023-01-11 14:50:10 +01001201 MBEDTLS_SSL_PROC_CHK(ssl_tls13_prepare_finished_message(ssl));
XiaokangQiandce82242021-11-15 06:01:26 +00001202
Gilles Peskine449bd832023-01-11 14:50:10 +01001203 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_start_handshake_msg(ssl,
1204 MBEDTLS_SSL_HS_FINISHED, &buf, &buf_len));
XiaokangQian35dc6252021-11-11 08:16:19 +00001205
Gilles Peskine449bd832023-01-11 14:50:10 +01001206 MBEDTLS_SSL_PROC_CHK(ssl_tls13_write_finished_message_body(
1207 ssl, buf, buf + buf_len, &msg_len));
XiaokangQian35dc6252021-11-11 08:16:19 +00001208
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +01001209 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_add_hs_msg_to_checksum(ssl,
Manuel Pégourié-Gonnard43cc1272023-02-06 11:48:19 +01001210 MBEDTLS_SSL_HS_FINISHED, buf, msg_len));
XiaokangQian35dc6252021-11-11 08:16:19 +00001211
Gilles Peskine449bd832023-01-11 14:50:10 +01001212 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_finish_handshake_msg(
1213 ssl, buf_len, msg_len));
XiaokangQian35dc6252021-11-11 08:16:19 +00001214cleanup:
1215
Gilles Peskine449bd832023-01-11 14:50:10 +01001216 MBEDTLS_SSL_DEBUG_MSG(2, ("<= write finished message"));
1217 return ret;
XiaokangQian35dc6252021-11-11 08:16:19 +00001218}
1219
Gilles Peskine449bd832023-01-11 14:50:10 +01001220void mbedtls_ssl_tls13_handshake_wrapup(mbedtls_ssl_context *ssl)
Jerry Yu378254d2021-10-30 21:44:47 +08001221{
1222
Gilles Peskine449bd832023-01-11 14:50:10 +01001223 MBEDTLS_SSL_DEBUG_MSG(3, ("=> handshake wrapup"));
Jerry Yu378254d2021-10-30 21:44:47 +08001224
Gilles Peskine449bd832023-01-11 14:50:10 +01001225 MBEDTLS_SSL_DEBUG_MSG(1, ("Switch to application keys for inbound traffic"));
1226 mbedtls_ssl_set_inbound_transform(ssl, ssl->transform_application);
Jerry Yue8c1fca2022-05-18 14:48:56 +08001227
Gilles Peskine449bd832023-01-11 14:50:10 +01001228 MBEDTLS_SSL_DEBUG_MSG(1, ("Switch to application keys for outbound traffic"));
1229 mbedtls_ssl_set_outbound_transform(ssl, ssl->transform_application);
Jerry Yue8c1fca2022-05-18 14:48:56 +08001230
Jerry Yu378254d2021-10-30 21:44:47 +08001231 /*
Jerry Yucfe64f02021-11-15 13:54:06 +08001232 * Free the previous session and switch to the current one.
Jerry Yu378254d2021-10-30 21:44:47 +08001233 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001234 if (ssl->session) {
1235 mbedtls_ssl_session_free(ssl->session);
1236 mbedtls_free(ssl->session);
Jerry Yu378254d2021-10-30 21:44:47 +08001237 }
1238 ssl->session = ssl->session_negotiate;
1239 ssl->session_negotiate = NULL;
1240
Gilles Peskine449bd832023-01-11 14:50:10 +01001241 MBEDTLS_SSL_DEBUG_MSG(3, ("<= handshake wrapup"));
Jerry Yu378254d2021-10-30 21:44:47 +08001242}
1243
Ronald Cron49ad6192021-11-24 16:25:31 +01001244/*
1245 *
1246 * STATE HANDLING: Write ChangeCipherSpec
1247 *
1248 */
1249#if defined(MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001250MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01001251static int ssl_tls13_write_change_cipher_spec_body(mbedtls_ssl_context *ssl,
1252 unsigned char *buf,
1253 unsigned char *end,
1254 size_t *olen)
Ronald Cron49ad6192021-11-24 16:25:31 +01001255{
1256 ((void) ssl);
1257
Gilles Peskine449bd832023-01-11 14:50:10 +01001258 MBEDTLS_SSL_CHK_BUF_PTR(buf, end, 1);
Ronald Cron49ad6192021-11-24 16:25:31 +01001259 buf[0] = 1;
1260 *olen = 1;
1261
Gilles Peskine449bd832023-01-11 14:50:10 +01001262 return 0;
Ronald Cron49ad6192021-11-24 16:25:31 +01001263}
1264
Gilles Peskine449bd832023-01-11 14:50:10 +01001265int mbedtls_ssl_tls13_write_change_cipher_spec(mbedtls_ssl_context *ssl)
Ronald Cron49ad6192021-11-24 16:25:31 +01001266{
1267 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1268
Gilles Peskine449bd832023-01-11 14:50:10 +01001269 MBEDTLS_SSL_DEBUG_MSG(2, ("=> write change cipher spec"));
Ronald Cron49ad6192021-11-24 16:25:31 +01001270
Ronald Crone273f722024-02-13 18:22:26 +01001271 /* Only one CCS to send. */
Ronald Cron5fbd2702024-02-14 10:03:36 +01001272 if (ssl->handshake->ccs_sent) {
Ronald Crone273f722024-02-13 18:22:26 +01001273 ret = 0;
1274 goto cleanup;
1275 }
1276
Ronald Cron49ad6192021-11-24 16:25:31 +01001277 /* Write CCS message */
Gilles Peskine449bd832023-01-11 14:50:10 +01001278 MBEDTLS_SSL_PROC_CHK(ssl_tls13_write_change_cipher_spec_body(
1279 ssl, ssl->out_msg,
1280 ssl->out_msg + MBEDTLS_SSL_OUT_CONTENT_LEN,
1281 &ssl->out_msglen));
Ronald Cron49ad6192021-11-24 16:25:31 +01001282
1283 ssl->out_msgtype = MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC;
1284
Ronald Cron49ad6192021-11-24 16:25:31 +01001285 /* Dispatch message */
Gilles Peskine449bd832023-01-11 14:50:10 +01001286 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_write_record(ssl, 0));
Ronald Cron49ad6192021-11-24 16:25:31 +01001287
Ronald Cron5fbd2702024-02-14 10:03:36 +01001288 ssl->handshake->ccs_sent = 1;
Ronald Cronfe59ff72024-01-24 14:31:50 +01001289
Ronald Cron49ad6192021-11-24 16:25:31 +01001290cleanup:
1291
Gilles Peskine449bd832023-01-11 14:50:10 +01001292 MBEDTLS_SSL_DEBUG_MSG(2, ("<= write change cipher spec"));
1293 return ret;
Ronald Cron49ad6192021-11-24 16:25:31 +01001294}
1295
1296#endif /* MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE */
1297
Xiaokang Qianecc29482022-11-02 07:52:47 +00001298/* Early Data Indication Extension
Xiaokang Qian0e97d4d2022-10-24 11:12:51 +00001299 *
Xiaokang Qian0e97d4d2022-10-24 11:12:51 +00001300 * struct {
1301 * select ( Handshake.msg_type ) {
Jerry Yu52335392023-11-23 18:06:06 +08001302 * case new_session_ticket: uint32 max_early_data_size;
Xiaokang Qian0e97d4d2022-10-24 11:12:51 +00001303 * case client_hello: Empty;
1304 * case encrypted_extensions: Empty;
1305 * };
1306 * } EarlyDataIndication;
1307 */
1308#if defined(MBEDTLS_SSL_EARLY_DATA)
Gilles Peskine449bd832023-01-11 14:50:10 +01001309int mbedtls_ssl_tls13_write_early_data_ext(mbedtls_ssl_context *ssl,
Jerry Yuc59c5862023-12-05 10:40:49 +08001310 int in_new_session_ticket,
Gilles Peskine449bd832023-01-11 14:50:10 +01001311 unsigned char *buf,
1312 const unsigned char *end,
Jerry Yuc59c5862023-12-05 10:40:49 +08001313 size_t *out_len)
Xiaokang Qian0e97d4d2022-10-24 11:12:51 +00001314{
1315 unsigned char *p = buf;
Xiaokang Qian0e97d4d2022-10-24 11:12:51 +00001316
Jerry Yu52335392023-11-23 18:06:06 +08001317#if defined(MBEDTLS_SSL_SRV_C)
Jerry Yuc59c5862023-12-05 10:40:49 +08001318 const size_t needed = in_new_session_ticket ? 8 : 4;
Jerry Yu52335392023-11-23 18:06:06 +08001319#else
1320 const size_t needed = 4;
Jerry Yuc59c5862023-12-05 10:40:49 +08001321 ((void) in_new_session_ticket);
Jerry Yu52335392023-11-23 18:06:06 +08001322#endif
1323
1324 *out_len = 0;
1325
1326 MBEDTLS_SSL_CHK_BUF_PTR(p, end, needed);
Xiaokang Qian0e97d4d2022-10-24 11:12:51 +00001327
Gilles Peskine449bd832023-01-11 14:50:10 +01001328 MBEDTLS_PUT_UINT16_BE(MBEDTLS_TLS_EXT_EARLY_DATA, p, 0);
Jerry Yu52335392023-11-23 18:06:06 +08001329 MBEDTLS_PUT_UINT16_BE(needed - 4, p, 2);
Xiaokang Qian0e97d4d2022-10-24 11:12:51 +00001330
Jerry Yu52335392023-11-23 18:06:06 +08001331#if defined(MBEDTLS_SSL_SRV_C)
Jerry Yuc59c5862023-12-05 10:40:49 +08001332 if (in_new_session_ticket) {
1333 MBEDTLS_PUT_UINT32_BE(ssl->conf->max_early_data_size, p, 4);
Jerry Yu52335392023-11-23 18:06:06 +08001334 MBEDTLS_SSL_DEBUG_MSG(
1335 4, ("Sent max_early_data_size=%u",
Jerry Yuc59c5862023-12-05 10:40:49 +08001336 (unsigned int) ssl->conf->max_early_data_size));
Jerry Yu52335392023-11-23 18:06:06 +08001337 }
1338#endif
1339
1340 *out_len = needed;
Xiaokang Qian2cd5ce02022-11-15 10:33:53 +00001341
Gilles Peskine449bd832023-01-11 14:50:10 +01001342 mbedtls_ssl_tls13_set_hs_sent_ext_mask(ssl, MBEDTLS_TLS_EXT_EARLY_DATA);
Xiaokang Qian2cd5ce02022-11-15 10:33:53 +00001343
Gilles Peskine449bd832023-01-11 14:50:10 +01001344 return 0;
Xiaokang Qian0e97d4d2022-10-24 11:12:51 +00001345}
Ronald Cron85718042024-02-22 10:22:09 +01001346
1347#if defined(MBEDTLS_SSL_SRV_C)
1348int mbedtls_ssl_tls13_check_early_data_len(mbedtls_ssl_context *ssl,
1349 size_t early_data_len)
1350{
Ronald Cron85718042024-02-22 10:22:09 +01001351 /*
1352 * This function should be called only while an handshake is in progress
1353 * and thus a session under negotiation. Add a sanity check to detect a
1354 * misuse.
1355 */
1356 if (ssl->session_negotiate == NULL) {
1357 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
1358 }
1359
1360 /* RFC 8446 section 4.6.1
1361 *
1362 * A server receiving more than max_early_data_size bytes of 0-RTT data
1363 * SHOULD terminate the connection with an "unexpected_message" alert.
Ronald Cron93795f22024-03-07 09:24:56 +01001364 * Note that if it is still possible to send early_data_len bytes of early
1365 * data, it means that early_data_len is smaller than max_early_data_size
1366 * (type uint32_t) and can fit in an uint32_t. We use this further
1367 * down.
Ronald Cron85718042024-02-22 10:22:09 +01001368 */
Ronald Cron2e7dfd52024-03-05 10:54:33 +01001369 if (early_data_len >
Ronald Cron85718042024-02-22 10:22:09 +01001370 (ssl->session_negotiate->max_early_data_size -
Ronald Cron19bfe0a2024-02-26 16:43:01 +01001371 ssl->total_early_data_size)) {
Ronald Cron85718042024-02-22 10:22:09 +01001372
1373 MBEDTLS_SSL_DEBUG_MSG(
Gilles Peskinea9d4ef02024-06-03 22:16:23 +02001374 2, ("EarlyData: Too much early data received, "
Gilles Peskine69770aa2024-06-04 08:45:58 +02001375 "%lu + %" MBEDTLS_PRINTF_SIZET " > %lu",
1376 (unsigned long) ssl->total_early_data_size,
Gilles Peskinea9d4ef02024-06-03 22:16:23 +02001377 early_data_len,
Gilles Peskine69770aa2024-06-04 08:45:58 +02001378 (unsigned long) ssl->session_negotiate->max_early_data_size));
Ronald Cron85718042024-02-22 10:22:09 +01001379
1380 MBEDTLS_SSL_PEND_FATAL_ALERT(
1381 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE,
1382 MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE);
1383 return MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE;
1384 }
1385
Ronald Cron2e7dfd52024-03-05 10:54:33 +01001386 /*
Ronald Cron93795f22024-03-07 09:24:56 +01001387 * early_data_len has been checked to be less than max_early_data_size
1388 * that is uint32_t. Its cast to an uint32_t below is thus safe. We need
1389 * the cast to appease some compilers.
Ronald Cron2e7dfd52024-03-05 10:54:33 +01001390 */
Ronald Cron2e7dfd52024-03-05 10:54:33 +01001391 ssl->total_early_data_size += (uint32_t) early_data_len;
Ronald Cron85718042024-02-22 10:22:09 +01001392
1393 return 0;
1394}
1395#endif /* MBEDTLS_SSL_SRV_C */
Xiaokang Qian0e97d4d2022-10-24 11:12:51 +00001396#endif /* MBEDTLS_SSL_EARLY_DATA */
1397
XiaokangQian78b1fa72022-01-19 06:56:30 +00001398/* Reset SSL context and update hash for handling HRR.
1399 *
1400 * Replace Transcript-Hash(X) by
1401 * Transcript-Hash( message_hash ||
1402 * 00 00 Hash.length ||
1403 * X )
1404 * A few states of the handshake are preserved, including:
1405 * - session ID
1406 * - session ticket
1407 * - negotiated ciphersuite
1408 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001409int mbedtls_ssl_reset_transcript_for_hrr(mbedtls_ssl_context *ssl)
XiaokangQian78b1fa72022-01-19 06:56:30 +00001410{
1411 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Przemyslaw Stekielda645252022-09-14 12:50:51 +02001412 unsigned char hash_transcript[PSA_HASH_MAX_SIZE + 4];
XiaokangQian0ece9982022-01-24 08:56:23 +00001413 size_t hash_len;
Xiaokang Qian6b980012023-02-07 03:17:45 +00001414 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
1415 ssl->handshake->ciphersuite_info;
XiaokangQian78b1fa72022-01-19 06:56:30 +00001416
Gilles Peskine449bd832023-01-11 14:50:10 +01001417 MBEDTLS_SSL_DEBUG_MSG(3, ("Reset SSL session for HRR"));
XiaokangQian78b1fa72022-01-19 06:56:30 +00001418
Dave Rodgman2eab4622023-10-05 13:30:37 +01001419 ret = mbedtls_ssl_get_handshake_transcript(ssl, (mbedtls_md_type_t) ciphersuite_info->mac,
Gilles Peskine449bd832023-01-11 14:50:10 +01001420 hash_transcript + 4,
1421 PSA_HASH_MAX_SIZE,
1422 &hash_len);
1423 if (ret != 0) {
Manuel Pégourié-Gonnardda7979b2023-02-21 09:31:10 +01001424 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_get_handshake_transcript", ret);
Gilles Peskine449bd832023-01-11 14:50:10 +01001425 return ret;
XiaokangQian0ece9982022-01-24 08:56:23 +00001426 }
1427
1428 hash_transcript[0] = MBEDTLS_SSL_HS_MESSAGE_HASH;
1429 hash_transcript[1] = 0;
1430 hash_transcript[2] = 0;
1431 hash_transcript[3] = (unsigned char) hash_len;
1432
1433 hash_len += 4;
1434
Manuel Pégourié-Gonnardda7979b2023-02-21 09:31:10 +01001435 MBEDTLS_SSL_DEBUG_BUF(4, "Truncated handshake transcript",
1436 hash_transcript, hash_len);
1437
Manuel Pégourié-Gonnardd7a7a232023-02-05 10:26:49 +01001438 /* Reset running hash and replace it with a hash of the transcript */
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +01001439 ret = mbedtls_ssl_reset_checksum(ssl);
1440 if (ret != 0) {
1441 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_reset_checksum", ret);
1442 return ret;
1443 }
1444 ret = ssl->handshake->update_checksum(ssl, hash_transcript, hash_len);
1445 if (ret != 0) {
1446 MBEDTLS_SSL_DEBUG_RET(1, "update_checksum", ret);
1447 return ret;
1448 }
Przemyslaw Stekiel4b3fff42022-02-14 16:39:52 +01001449
Gilles Peskine449bd832023-01-11 14:50:10 +01001450 return ret;
XiaokangQian78b1fa72022-01-19 06:56:30 +00001451}
1452
Valerio Settic9ae8622023-07-25 11:23:50 +02001453#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_EPHEMERAL_ENABLED)
XiaokangQian7807f9f2022-02-15 10:04:37 +00001454
Przemek Stekiel7ac93be2023-07-04 10:02:38 +02001455int mbedtls_ssl_tls13_read_public_xxdhe_share(mbedtls_ssl_context *ssl,
Gilles Peskine449bd832023-01-11 14:50:10 +01001456 const unsigned char *buf,
1457 size_t buf_len)
XiaokangQian7807f9f2022-02-15 10:04:37 +00001458{
Gilles Peskine449bd832023-01-11 14:50:10 +01001459 uint8_t *p = (uint8_t *) buf;
XiaokangQiancfd925f2022-04-14 07:10:37 +00001460 const uint8_t *end = buf + buf_len;
XiaokangQian9b5d04b2022-04-10 10:20:43 +00001461 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001462
XiaokangQian9b5d04b2022-04-10 10:20:43 +00001463 /* Get size of the TLS opaque key_exchange field of the KeyShareEntry struct. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001464 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, 2);
1465 uint16_t peerkey_len = MBEDTLS_GET_UINT16_BE(p, 0);
XiaokangQian9b5d04b2022-04-10 10:20:43 +00001466 p += 2;
XiaokangQian3207a322022-02-23 03:15:27 +00001467
XiaokangQian9b5d04b2022-04-10 10:20:43 +00001468 /* Check if key size is consistent with given buffer length. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001469 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, peerkey_len);
XiaokangQian9b5d04b2022-04-10 10:20:43 +00001470
Gilles Peskine12c5aaa2023-10-02 14:55:45 +02001471 /* Store peer's ECDH/FFDH public key. */
1472 if (peerkey_len > sizeof(handshake->xxdh_psa_peerkey)) {
Gilles Peskine530c4232023-10-02 15:37:23 +02001473 MBEDTLS_SSL_DEBUG_MSG(1, ("Invalid public key length: %u > %" MBEDTLS_PRINTF_SIZET,
1474 (unsigned) peerkey_len,
1475 sizeof(handshake->xxdh_psa_peerkey)));
Gilles Peskine12c5aaa2023-10-02 14:55:45 +02001476 return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
1477 }
Przemek Stekiel7ac93be2023-07-04 10:02:38 +02001478 memcpy(handshake->xxdh_psa_peerkey, p, peerkey_len);
1479 handshake->xxdh_psa_peerkey_len = peerkey_len;
XiaokangQian9b5d04b2022-04-10 10:20:43 +00001480
Gilles Peskine449bd832023-01-11 14:50:10 +01001481 return 0;
XiaokangQian3207a322022-02-23 03:15:27 +00001482}
Jerry Yu89e103c2022-03-30 22:43:29 +08001483
Valerio Setti711f8532023-07-31 11:28:07 +02001484#if defined(PSA_WANT_ALG_FFDH)
Przemek Stekielda4fba62023-06-02 14:52:28 +02001485static psa_status_t mbedtls_ssl_get_psa_ffdh_info_from_tls_id(
Przemek Stekiel75a5a9c2023-06-12 11:21:18 +02001486 uint16_t tls_id, size_t *bits, psa_key_type_t *key_type)
Przemek Stekiel29c219c2023-05-31 15:21:04 +02001487{
Przemek Stekiel75a5a9c2023-06-12 11:21:18 +02001488 switch (tls_id) {
Valerio Settiecaf7c52024-01-17 12:30:30 +01001489#if defined(PSA_WANT_DH_RFC7919_2048)
Przemek Stekiel29c219c2023-05-31 15:21:04 +02001490 case MBEDTLS_SSL_IANA_TLS_GROUP_FFDHE2048:
1491 *bits = 2048;
1492 *key_type = PSA_KEY_TYPE_DH_KEY_PAIR(PSA_DH_FAMILY_RFC7919);
1493 return PSA_SUCCESS;
Valerio Settiecaf7c52024-01-17 12:30:30 +01001494#endif /* PSA_WANT_DH_RFC7919_2048 */
1495#if defined(PSA_WANT_DH_RFC7919_3072)
Przemek Stekiel29c219c2023-05-31 15:21:04 +02001496 case MBEDTLS_SSL_IANA_TLS_GROUP_FFDHE3072:
1497 *bits = 3072;
1498 *key_type = PSA_KEY_TYPE_DH_KEY_PAIR(PSA_DH_FAMILY_RFC7919);
1499 return PSA_SUCCESS;
Valerio Settiecaf7c52024-01-17 12:30:30 +01001500#endif /* PSA_WANT_DH_RFC7919_3072 */
1501#if defined(PSA_WANT_DH_RFC7919_4096)
Przemek Stekiel29c219c2023-05-31 15:21:04 +02001502 case MBEDTLS_SSL_IANA_TLS_GROUP_FFDHE4096:
1503 *bits = 4096;
1504 *key_type = PSA_KEY_TYPE_DH_KEY_PAIR(PSA_DH_FAMILY_RFC7919);
1505 return PSA_SUCCESS;
Valerio Settiecaf7c52024-01-17 12:30:30 +01001506#endif /* PSA_WANT_DH_RFC7919_4096 */
1507#if defined(PSA_WANT_DH_RFC7919_6144)
Przemek Stekiel29c219c2023-05-31 15:21:04 +02001508 case MBEDTLS_SSL_IANA_TLS_GROUP_FFDHE6144:
1509 *bits = 6144;
1510 *key_type = PSA_KEY_TYPE_DH_KEY_PAIR(PSA_DH_FAMILY_RFC7919);
1511 return PSA_SUCCESS;
Valerio Settiecaf7c52024-01-17 12:30:30 +01001512#endif /* PSA_WANT_DH_RFC7919_6144 */
1513#if defined(PSA_WANT_DH_RFC7919_8192)
Przemek Stekiel29c219c2023-05-31 15:21:04 +02001514 case MBEDTLS_SSL_IANA_TLS_GROUP_FFDHE8192:
1515 *bits = 8192;
1516 *key_type = PSA_KEY_TYPE_DH_KEY_PAIR(PSA_DH_FAMILY_RFC7919);
1517 return PSA_SUCCESS;
Valerio Settiecaf7c52024-01-17 12:30:30 +01001518#endif /* PSA_WANT_DH_RFC7919_8192 */
Przemek Stekiel29c219c2023-05-31 15:21:04 +02001519 default:
1520 return PSA_ERROR_NOT_SUPPORTED;
1521 }
1522}
Valerio Setti711f8532023-07-31 11:28:07 +02001523#endif /* PSA_WANT_ALG_FFDH */
Przemek Stekiel29c219c2023-05-31 15:21:04 +02001524
Przemek Stekiel408569f2023-07-06 11:26:44 +02001525int mbedtls_ssl_tls13_generate_and_write_xxdh_key_exchange(
Gilles Peskine449bd832023-01-11 14:50:10 +01001526 mbedtls_ssl_context *ssl,
1527 uint16_t named_group,
1528 unsigned char *buf,
1529 unsigned char *end,
1530 size_t *out_len)
Jerry Yu89e103c2022-03-30 22:43:29 +08001531{
1532 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
1533 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
1534 psa_key_attributes_t key_attributes;
1535 size_t own_pubkey_len;
1536 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
Przemek Stekiel29c219c2023-05-31 15:21:04 +02001537 size_t bits = 0;
Przemek Stekiel75a5a9c2023-06-12 11:21:18 +02001538 psa_key_type_t key_type = PSA_KEY_TYPE_NONE;
1539 psa_algorithm_t alg = PSA_ALG_NONE;
Przemek Stekiel29c219c2023-05-31 15:21:04 +02001540 size_t buf_size = (size_t) (end - buf);
Jerry Yu89e103c2022-03-30 22:43:29 +08001541
Przemek Stekiel29c219c2023-05-31 15:21:04 +02001542 MBEDTLS_SSL_DEBUG_MSG(1, ("Perform PSA-based ECDH/FFDH computation."));
Jerry Yu89e103c2022-03-30 22:43:29 +08001543
Valerio Setti40d9ca92023-01-04 16:08:04 +01001544 /* Convert EC's TLS ID to PSA key type. */
Przemek Stekiel29c219c2023-05-31 15:21:04 +02001545#if defined(PSA_WANT_ALG_ECDH)
Xiaokang Qian73437382023-03-29 08:24:12 +00001546 if (mbedtls_ssl_get_psa_curve_info_from_tls_id(
Przemek Stekielda4fba62023-06-02 14:52:28 +02001547 named_group, &key_type, &bits) == PSA_SUCCESS) {
1548 alg = PSA_ALG_ECDH;
Przemek Stekiel29c219c2023-05-31 15:21:04 +02001549 }
1550#endif
1551#if defined(PSA_WANT_ALG_FFDH)
Przemek Stekiel75a5a9c2023-06-12 11:21:18 +02001552 if (mbedtls_ssl_get_psa_ffdh_info_from_tls_id(named_group, &bits,
1553 &key_type) == PSA_SUCCESS) {
Przemek Stekielda4fba62023-06-02 14:52:28 +02001554 alg = PSA_ALG_FFDH;
Przemek Stekiel29c219c2023-05-31 15:21:04 +02001555 }
1556#endif
1557
Przemek Stekiel75a5a9c2023-06-12 11:21:18 +02001558 if (key_type == PSA_KEY_TYPE_NONE) {
Gilles Peskine449bd832023-01-11 14:50:10 +01001559 return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
Valerio Setti40d9ca92023-01-04 16:08:04 +01001560 }
Przemek Stekiel29c219c2023-05-31 15:21:04 +02001561
Przemek Stekielda4fba62023-06-02 14:52:28 +02001562 if (buf_size < PSA_BITS_TO_BYTES(bits)) {
Przemek Stekielda4fba62023-06-02 14:52:28 +02001563 return MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL;
1564 }
1565
Przemek Stekiel7ac93be2023-07-04 10:02:38 +02001566 handshake->xxdh_psa_type = key_type;
Valerio Settiea59c432023-07-25 11:14:03 +02001567 ssl->handshake->xxdh_psa_bits = bits;
Jerry Yu89e103c2022-03-30 22:43:29 +08001568
1569 key_attributes = psa_key_attributes_init();
Gilles Peskine449bd832023-01-11 14:50:10 +01001570 psa_set_key_usage_flags(&key_attributes, PSA_KEY_USAGE_DERIVE);
Przemek Stekielda4fba62023-06-02 14:52:28 +02001571 psa_set_key_algorithm(&key_attributes, alg);
Przemek Stekiel7ac93be2023-07-04 10:02:38 +02001572 psa_set_key_type(&key_attributes, handshake->xxdh_psa_type);
Valerio Settiea59c432023-07-25 11:14:03 +02001573 psa_set_key_bits(&key_attributes, handshake->xxdh_psa_bits);
Jerry Yu89e103c2022-03-30 22:43:29 +08001574
Przemek Stekiel29c219c2023-05-31 15:21:04 +02001575 /* Generate ECDH/FFDH private key. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001576 status = psa_generate_key(&key_attributes,
Przemek Stekiel7ac93be2023-07-04 10:02:38 +02001577 &handshake->xxdh_psa_privkey);
Gilles Peskine449bd832023-01-11 14:50:10 +01001578 if (status != PSA_SUCCESS) {
Andrzej Kurek8a045ce2022-12-23 11:00:06 -05001579 ret = PSA_TO_MBEDTLS_ERR(status);
Gilles Peskine449bd832023-01-11 14:50:10 +01001580 MBEDTLS_SSL_DEBUG_RET(1, "psa_generate_key", ret);
1581 return ret;
Jerry Yu89e103c2022-03-30 22:43:29 +08001582
1583 }
1584
Przemek Stekiel29c219c2023-05-31 15:21:04 +02001585 /* Export the public part of the ECDH/FFDH private key from PSA. */
Przemek Stekiel7ac93be2023-07-04 10:02:38 +02001586 status = psa_export_public_key(handshake->xxdh_psa_privkey,
Przemek Stekiel29c219c2023-05-31 15:21:04 +02001587 buf, buf_size,
Gilles Peskine449bd832023-01-11 14:50:10 +01001588 &own_pubkey_len);
Przemek Stekiel29c219c2023-05-31 15:21:04 +02001589
Gilles Peskine449bd832023-01-11 14:50:10 +01001590 if (status != PSA_SUCCESS) {
Andrzej Kurek8a045ce2022-12-23 11:00:06 -05001591 ret = PSA_TO_MBEDTLS_ERR(status);
Gilles Peskine449bd832023-01-11 14:50:10 +01001592 MBEDTLS_SSL_DEBUG_RET(1, "psa_export_public_key", ret);
1593 return ret;
Jerry Yu89e103c2022-03-30 22:43:29 +08001594 }
1595
1596 *out_len = own_pubkey_len;
1597
Gilles Peskine449bd832023-01-11 14:50:10 +01001598 return 0;
Jerry Yu89e103c2022-03-30 22:43:29 +08001599}
Valerio Settic9ae8622023-07-25 11:23:50 +02001600#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_EPHEMERAL_ENABLED */
XiaokangQian7807f9f2022-02-15 10:04:37 +00001601
Jerry Yu0c354a22022-08-29 15:25:36 +08001602/* RFC 8446 section 4.2
1603 *
1604 * If an implementation receives an extension which it recognizes and which is
1605 * not specified for the message in which it appears, it MUST abort the handshake
1606 * with an "illegal_parameter" alert.
1607 *
1608 */
Jerry Yuc4bf5d62022-10-29 09:08:47 +08001609int mbedtls_ssl_tls13_check_received_extension(
Gilles Peskine449bd832023-01-11 14:50:10 +01001610 mbedtls_ssl_context *ssl,
1611 int hs_msg_type,
1612 unsigned int received_extension_type,
1613 uint32_t hs_msg_allowed_extensions_mask)
Jerry Yu0c354a22022-08-29 15:25:36 +08001614{
Jerry Yudf0ad652022-10-31 13:20:57 +08001615 uint32_t extension_mask = mbedtls_ssl_get_extension_mask(
Gilles Peskine449bd832023-01-11 14:50:10 +01001616 received_extension_type);
Jerry Yu0c354a22022-08-29 15:25:36 +08001617
Jerry Yu79aa7212022-11-08 21:30:21 +08001618 MBEDTLS_SSL_PRINT_EXT(
Gilles Peskine449bd832023-01-11 14:50:10 +01001619 3, hs_msg_type, received_extension_type, "received");
Jerry Yu0c354a22022-08-29 15:25:36 +08001620
Gilles Peskine449bd832023-01-11 14:50:10 +01001621 if ((extension_mask & hs_msg_allowed_extensions_mask) == 0) {
Jerry Yu79aa7212022-11-08 21:30:21 +08001622 MBEDTLS_SSL_PRINT_EXT(
Gilles Peskine449bd832023-01-11 14:50:10 +01001623 3, hs_msg_type, received_extension_type, "is illegal");
Jerry Yu0c354a22022-08-29 15:25:36 +08001624 MBEDTLS_SSL_PEND_FATAL_ALERT(
1625 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER,
Gilles Peskine449bd832023-01-11 14:50:10 +01001626 MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER);
1627 return MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER;
Jerry Yu0c354a22022-08-29 15:25:36 +08001628 }
1629
1630 ssl->handshake->received_extensions |= extension_mask;
Jerry Yuc4bf5d62022-10-29 09:08:47 +08001631 /*
1632 * If it is a message containing extension responses, check that we
1633 * previously sent the extension.
1634 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001635 switch (hs_msg_type) {
Jerry Yu0c354a22022-08-29 15:25:36 +08001636 case MBEDTLS_SSL_HS_SERVER_HELLO:
Jerry Yudf0ad652022-10-31 13:20:57 +08001637 case MBEDTLS_SSL_TLS1_3_HS_HELLO_RETRY_REQUEST:
Jerry Yu0c354a22022-08-29 15:25:36 +08001638 case MBEDTLS_SSL_HS_ENCRYPTED_EXTENSIONS:
1639 case MBEDTLS_SSL_HS_CERTIFICATE:
Jerry Yuc4bf5d62022-10-29 09:08:47 +08001640 /* Check if the received extension is sent by peer message.*/
Gilles Peskine449bd832023-01-11 14:50:10 +01001641 if ((ssl->handshake->sent_extensions & extension_mask) != 0) {
1642 return 0;
1643 }
Jerry Yu0c354a22022-08-29 15:25:36 +08001644 break;
1645 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01001646 return 0;
Jerry Yu0c354a22022-08-29 15:25:36 +08001647 }
1648
Jerry Yu79aa7212022-11-08 21:30:21 +08001649 MBEDTLS_SSL_PRINT_EXT(
Gilles Peskine449bd832023-01-11 14:50:10 +01001650 3, hs_msg_type, received_extension_type, "is unsupported");
Jerry Yu0c354a22022-08-29 15:25:36 +08001651 MBEDTLS_SSL_PEND_FATAL_ALERT(
1652 MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_EXT,
Gilles Peskine449bd832023-01-11 14:50:10 +01001653 MBEDTLS_ERR_SSL_UNSUPPORTED_EXTENSION);
1654 return MBEDTLS_ERR_SSL_UNSUPPORTED_EXTENSION;
Jerry Yu0c354a22022-08-29 15:25:36 +08001655}
1656
Jan Bruckner151f6422023-02-10 12:45:19 +01001657#if defined(MBEDTLS_SSL_RECORD_SIZE_LIMIT)
Yanray Wangfaf70bd2023-12-07 10:03:32 +08001658
1659/* RFC 8449, section 4:
1660 *
Jan Bruckner151f6422023-02-10 12:45:19 +01001661 * The ExtensionData of the "record_size_limit" extension is
1662 * RecordSizeLimit:
1663 * uint16 RecordSizeLimit;
1664 */
1665MBEDTLS_CHECK_RETURN_CRITICAL
1666int mbedtls_ssl_tls13_parse_record_size_limit_ext(mbedtls_ssl_context *ssl,
1667 const unsigned char *buf,
1668 const unsigned char *end)
1669{
Jan Bruckner1a38e542023-03-15 14:15:11 +01001670 const unsigned char *p = buf;
1671 uint16_t record_size_limit;
Jan Brucknera0589e72023-03-15 11:04:45 +01001672 const size_t extension_data_len = end - buf;
Jan Bruckner1a38e542023-03-15 14:15:11 +01001673
Xiaokang Qian73437382023-03-29 08:24:12 +00001674 if (extension_data_len !=
1675 MBEDTLS_SSL_RECORD_SIZE_LIMIT_EXTENSION_DATA_LENGTH) {
Jan Bruckner151f6422023-02-10 12:45:19 +01001676 MBEDTLS_SSL_DEBUG_MSG(2,
Jan Bruckner1a38e542023-03-15 14:15:11 +01001677 ("record_size_limit extension has invalid length: %"
1678 MBEDTLS_PRINTF_SIZET " Bytes",
Jan Bruckner151f6422023-02-10 12:45:19 +01001679 extension_data_len));
1680
1681 MBEDTLS_SSL_PEND_FATAL_ALERT(
1682 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER,
1683 MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER);
1684 return MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER;
1685 }
1686
Jan Bruckner151f6422023-02-10 12:45:19 +01001687 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, 2);
1688 record_size_limit = MBEDTLS_GET_UINT16_BE(p, 0);
1689
1690 MBEDTLS_SSL_DEBUG_MSG(2, ("RecordSizeLimit: %u Bytes", record_size_limit));
1691
Waleed Elmelegye1ac98d2024-01-05 18:10:12 +00001692 /* RFC 8449, section 4:
1693 *
1694 * Endpoints MUST NOT send a "record_size_limit" extension with a value
1695 * smaller than 64. An endpoint MUST treat receipt of a smaller value
1696 * as a fatal error and generate an "illegal_parameter" alert.
1697 */
1698 if (record_size_limit < MBEDTLS_SSL_RECORD_SIZE_LIMIT_MIN) {
1699 MBEDTLS_SSL_DEBUG_MSG(1, ("Invalid record size limit : %u Bytes",
1700 record_size_limit));
1701 MBEDTLS_SSL_PEND_FATAL_ALERT(
1702 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER,
1703 MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER);
1704 return MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER;
Jan Bruckner151f6422023-02-10 12:45:19 +01001705 }
1706
Jan Brucknerf482dcc2023-03-15 09:09:06 +01001707 ssl->session_negotiate->record_size_limit = record_size_limit;
Jan Bruckner151f6422023-02-10 12:45:19 +01001708
Jan Brucknerf482dcc2023-03-15 09:09:06 +01001709 return 0;
Jan Bruckner151f6422023-02-10 12:45:19 +01001710}
Jan Brucknerf482dcc2023-03-15 09:09:06 +01001711
Yanray Wangfaf70bd2023-12-07 10:03:32 +08001712MBEDTLS_CHECK_RETURN_CRITICAL
1713int mbedtls_ssl_tls13_write_record_size_limit_ext(mbedtls_ssl_context *ssl,
Yanray Wangfaf70bd2023-12-07 10:03:32 +08001714 unsigned char *buf,
1715 const unsigned char *end,
1716 size_t *out_len)
1717{
Yanray Wangfaf70bd2023-12-07 10:03:32 +08001718 unsigned char *p = buf;
1719 *out_len = 0;
1720
Waleed Elmelegy148dfb62024-01-04 18:02:35 +00001721 MBEDTLS_STATIC_ASSERT(MBEDTLS_SSL_IN_CONTENT_LEN >= MBEDTLS_SSL_RECORD_SIZE_LIMIT_MIN,
Waleed Elmelegye1ac98d2024-01-05 18:10:12 +00001722 "MBEDTLS_SSL_IN_CONTENT_LEN is less than the "
1723 "minimum record size limit");
Yanray Wangfaf70bd2023-12-07 10:03:32 +08001724
1725 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 6);
1726
1727 MBEDTLS_PUT_UINT16_BE(MBEDTLS_TLS_EXT_RECORD_SIZE_LIMIT, p, 0);
Waleed Elmelegye1ac98d2024-01-05 18:10:12 +00001728 MBEDTLS_PUT_UINT16_BE(MBEDTLS_SSL_RECORD_SIZE_LIMIT_EXTENSION_DATA_LENGTH,
1729 p, 2);
Waleed Elmelegy148dfb62024-01-04 18:02:35 +00001730 MBEDTLS_PUT_UINT16_BE(MBEDTLS_SSL_IN_CONTENT_LEN, p, 4);
Yanray Wangfaf70bd2023-12-07 10:03:32 +08001731
1732 *out_len = 6;
1733
Waleed Elmelegy3ff47242024-01-10 16:15:52 +00001734 MBEDTLS_SSL_DEBUG_MSG(2, ("Sent RecordSizeLimit: %d Bytes",
Waleed Elmelegye1ac98d2024-01-05 18:10:12 +00001735 MBEDTLS_SSL_IN_CONTENT_LEN));
Yanray Wangfaf70bd2023-12-07 10:03:32 +08001736
1737 mbedtls_ssl_tls13_set_hs_sent_ext_mask(ssl, MBEDTLS_TLS_EXT_RECORD_SIZE_LIMIT);
1738
1739 return 0;
1740}
1741
Jan Bruckner151f6422023-02-10 12:45:19 +01001742#endif /* MBEDTLS_SSL_RECORD_SIZE_LIMIT */
1743
Jerry Yufb4b6472022-01-27 15:03:26 +08001744#endif /* MBEDTLS_SSL_TLS_C && MBEDTLS_SSL_PROTO_TLS1_3 */