blob: cdf42128f8cff01aba4011e85ed42b69430e2438 [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"
Ben Taylor04b03d72025-07-14 09:46:18 +010028#pragma GCC diagnostic warning "-Wenum-conversion"
Andrzej Kurek8a045ce2022-12-23 11:00:06 -050029
Valerio Settic9ae8622023-07-25 11:23:50 +020030#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_EPHEMERAL_ENABLED)
Andrzej Kurek00644842023-05-30 05:45:00 -040031/* Define a local translating function to save code size by not using too many
32 * arguments in each translating place. */
33static int local_err_translation(psa_status_t status)
34{
35 return psa_status_to_mbedtls(status, psa_to_ssl_errors,
Andrzej Kurek1e4a0302023-05-30 09:45:17 -040036 ARRAY_LENGTH(psa_to_ssl_errors),
Andrzej Kurek00644842023-05-30 05:45:00 -040037 psa_generic_status_to_mbedtls);
38}
39#define PSA_TO_MBEDTLS_ERR(status) local_err_translation(status)
Andrzej Kureka6033ac2023-05-30 15:16:34 -040040#endif
Andrzej Kurek8a045ce2022-12-23 11:00:06 -050041
Jerry Yufbe3e642022-04-25 19:31:51 +080042const uint8_t mbedtls_ssl_tls13_hello_retry_request_magic[
Gilles Peskine449bd832023-01-11 14:50:10 +010043 MBEDTLS_SERVER_HELLO_RANDOM_LEN] =
44{ 0xCF, 0x21, 0xAD, 0x74, 0xE5, 0x9A, 0x61, 0x11,
45 0xBE, 0x1D, 0x8C, 0x02, 0x1E, 0x65, 0xB8, 0x91,
46 0xC2, 0xA2, 0x11, 0x16, 0x7A, 0xBB, 0x8C, 0x5E,
47 0x07, 0x9E, 0x09, 0xE2, 0xC8, 0xA8, 0x33, 0x9C };
Jerry Yu93a13f22022-04-11 23:00:01 +080048
Gilles Peskine449bd832023-01-11 14:50:10 +010049int mbedtls_ssl_tls13_fetch_handshake_msg(mbedtls_ssl_context *ssl,
50 unsigned hs_type,
51 unsigned char **buf,
52 size_t *buf_len)
XiaokangQian6b226b02021-09-24 07:51:16 +000053{
54 int ret;
55
Gilles Peskine449bd832023-01-11 14:50:10 +010056 if ((ret = mbedtls_ssl_read_record(ssl, 0)) != 0) {
57 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_read_record", ret);
XiaokangQian6b226b02021-09-24 07:51:16 +000058 goto cleanup;
59 }
60
Gilles Peskine449bd832023-01-11 14:50:10 +010061 if (ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE ||
62 ssl->in_msg[0] != hs_type) {
63 MBEDTLS_SSL_DEBUG_MSG(1, ("Receive unexpected handshake message."));
64 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE,
65 MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE);
XiaokangQian6b226b02021-09-24 07:51:16 +000066 ret = MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE;
67 goto cleanup;
68 }
69
XiaokangQian05420b12021-09-29 08:46:37 +000070 /*
71 * Jump handshake header (4 bytes, see Section 4 of RFC 8446).
72 * ...
73 * HandshakeType msg_type;
74 * uint24 length;
75 * ...
76 */
Xiaofei Baieef15042021-11-18 07:29:56 +000077 *buf = ssl->in_msg + 4;
78 *buf_len = ssl->in_hslen - 4;
XiaokangQian6b226b02021-09-24 07:51:16 +000079
XiaokangQian6b226b02021-09-24 07:51:16 +000080cleanup:
81
Gilles Peskine449bd832023-01-11 14:50:10 +010082 return ret;
XiaokangQian6b226b02021-09-24 07:51:16 +000083}
84
Ronald Cron47dce632023-02-08 17:38:29 +010085int mbedtls_ssl_tls13_is_supported_versions_ext_present_in_exts(
86 mbedtls_ssl_context *ssl,
87 const unsigned char *buf, const unsigned char *end,
Ronald Croneff56732023-04-03 17:36:31 +020088 const unsigned char **supported_versions_data,
89 const unsigned char **supported_versions_data_end)
Ronald Cron47dce632023-02-08 17:38:29 +010090{
91 const unsigned char *p = buf;
92 size_t extensions_len;
93 const unsigned char *extensions_end;
94
Ronald Croneff56732023-04-03 17:36:31 +020095 *supported_versions_data = NULL;
96 *supported_versions_data_end = NULL;
Ronald Cron47dce632023-02-08 17:38:29 +010097
98 /* Case of no extension */
99 if (p == end) {
100 return 0;
101 }
102
103 /* ...
104 * Extension extensions<x..2^16-1>;
105 * ...
106 * struct {
107 * ExtensionType extension_type; (2 bytes)
108 * opaque extension_data<0..2^16-1>;
109 * } Extension;
110 */
111 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, 2);
112 extensions_len = MBEDTLS_GET_UINT16_BE(p, 0);
113 p += 2;
114
115 /* Check extensions do not go beyond the buffer of data. */
116 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, extensions_len);
117 extensions_end = p + extensions_len;
118
119 while (p < extensions_end) {
120 unsigned int extension_type;
121 size_t extension_data_len;
122
123 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, extensions_end, 4);
124 extension_type = MBEDTLS_GET_UINT16_BE(p, 0);
125 extension_data_len = MBEDTLS_GET_UINT16_BE(p, 2);
126 p += 4;
127 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, extensions_end, extension_data_len);
128
129 if (extension_type == MBEDTLS_TLS_EXT_SUPPORTED_VERSIONS) {
Ronald Croneff56732023-04-03 17:36:31 +0200130 *supported_versions_data = p;
131 *supported_versions_data_end = p + extension_data_len;
Ronald Cron47dce632023-02-08 17:38:29 +0100132 return 1;
133 }
134 p += extension_data_len;
135 }
136
137 return 0;
138}
139
Ronald Cron928cbd32022-10-04 16:14:26 +0200140#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED)
Jerry Yu30b071c2021-09-12 20:16:03 +0800141/*
Jerry Yu30b071c2021-09-12 20:16:03 +0800142 * STATE HANDLING: Read CertificateVerify
143 */
Jerry Yud0fc5852021-10-29 11:09:06 +0800144/* Macro to express the maximum length of the verify structure.
Jerry Yu30b071c2021-09-12 20:16:03 +0800145 *
146 * The structure is computed per TLS 1.3 specification as:
147 * - 64 bytes of octet 32,
148 * - 33 bytes for the context string
149 * (which is either "TLS 1.3, client CertificateVerify"
150 * or "TLS 1.3, server CertificateVerify"),
Jerry Yud0fc5852021-10-29 11:09:06 +0800151 * - 1 byte for the octet 0x0, which serves as a separator,
Jerry Yu30b071c2021-09-12 20:16:03 +0800152 * - 32 or 48 bytes for the Transcript-Hash(Handshake Context, Certificate)
153 * (depending on the size of the transcript_hash)
154 *
155 * This results in a total size of
156 * - 130 bytes for a SHA256-based transcript hash, or
157 * (64 + 33 + 1 + 32 bytes)
158 * - 146 bytes for a SHA384-based transcript hash.
159 * (64 + 33 + 1 + 48 bytes)
160 *
161 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100162#define SSL_VERIFY_STRUCT_MAX_SIZE (64 + \
163 33 + \
164 1 + \
165 MBEDTLS_TLS1_3_MD_MAX_SIZE \
166 )
Jerry Yu30b071c2021-09-12 20:16:03 +0800167
Jerry Yu0b32c502021-10-28 13:41:59 +0800168/*
169 * The ssl_tls13_create_verify_structure() creates the verify structure.
170 * As input, it requires the transcript hash.
171 *
172 * The caller has to ensure that the buffer has size at least
173 * SSL_VERIFY_STRUCT_MAX_SIZE bytes.
174 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100175static void ssl_tls13_create_verify_structure(const unsigned char *transcript_hash,
176 size_t transcript_hash_len,
177 unsigned char *verify_buffer,
178 size_t *verify_buffer_len,
179 int from)
Jerry Yu0b32c502021-10-28 13:41:59 +0800180{
181 size_t idx;
Jerry Yu30b071c2021-09-12 20:16:03 +0800182
Jerry Yu0b32c502021-10-28 13:41:59 +0800183 /* RFC 8446, Section 4.4.3:
184 *
185 * The digital signature [in the CertificateVerify message] is then
186 * computed over the concatenation of:
187 * - A string that consists of octet 32 (0x20) repeated 64 times
188 * - The context string
189 * - A single 0 byte which serves as the separator
190 * - The content to be signed
191 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100192 memset(verify_buffer, 0x20, 64);
Jerry Yu0b32c502021-10-28 13:41:59 +0800193 idx = 64;
194
Gilles Peskine449bd832023-01-11 14:50:10 +0100195 if (from == MBEDTLS_SSL_IS_CLIENT) {
Tom Cosgrovea2c45dc2024-04-02 14:26:13 +0100196 memcpy(verify_buffer + idx, mbedtls_ssl_tls13_labels.client_cv,
197 MBEDTLS_SSL_TLS1_3_LBL_LEN(client_cv));
Gilles Peskine449bd832023-01-11 14:50:10 +0100198 idx += MBEDTLS_SSL_TLS1_3_LBL_LEN(client_cv);
199 } else { /* from == MBEDTLS_SSL_IS_SERVER */
Tom Cosgrovea2c45dc2024-04-02 14:26:13 +0100200 memcpy(verify_buffer + idx, mbedtls_ssl_tls13_labels.server_cv,
201 MBEDTLS_SSL_TLS1_3_LBL_LEN(server_cv));
Gilles Peskine449bd832023-01-11 14:50:10 +0100202 idx += MBEDTLS_SSL_TLS1_3_LBL_LEN(server_cv);
Jerry Yu0b32c502021-10-28 13:41:59 +0800203 }
204
205 verify_buffer[idx++] = 0x0;
206
Gilles Peskine449bd832023-01-11 14:50:10 +0100207 memcpy(verify_buffer + idx, transcript_hash, transcript_hash_len);
Jerry Yu0b32c502021-10-28 13:41:59 +0800208 idx += transcript_hash_len;
209
210 *verify_buffer_len = idx;
211}
212
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200213MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100214static int ssl_tls13_parse_certificate_verify(mbedtls_ssl_context *ssl,
215 const unsigned char *buf,
216 const unsigned char *end,
217 const unsigned char *verify_buffer,
218 size_t verify_buffer_len)
Jerry Yu30b071c2021-09-12 20:16:03 +0800219{
220 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
pespaceka1378102022-04-26 15:03:11 +0200221 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Jerry Yu30b071c2021-09-12 20:16:03 +0800222 const unsigned char *p = buf;
223 uint16_t algorithm;
Jerry Yu30b071c2021-09-12 20:16:03 +0800224 size_t signature_len;
225 mbedtls_pk_type_t sig_alg;
226 mbedtls_md_type_t md_alg;
pespaceka1378102022-04-26 15:03:11 +0200227 psa_algorithm_t hash_alg = PSA_ALG_NONE;
228 unsigned char verify_hash[PSA_HASH_MAX_SIZE];
Jerry Yu30b071c2021-09-12 20:16:03 +0800229 size_t verify_hash_len;
230
231 /*
232 * struct {
233 * SignatureScheme algorithm;
234 * opaque signature<0..2^16-1>;
235 * } CertificateVerify;
236 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100237 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, 2);
238 algorithm = MBEDTLS_GET_UINT16_BE(p, 0);
Jerry Yu30b071c2021-09-12 20:16:03 +0800239 p += 2;
240
241 /* RFC 8446 section 4.4.3
242 *
Xiaokang Qian73437382023-03-29 08:24:12 +0000243 * If the CertificateVerify message is sent by a server, the signature
244 * algorithm MUST be one offered in the client's "signature_algorithms"
245 * extension unless no valid certificate chain can be produced without
246 * unsupported algorithms
Jerry Yu30b071c2021-09-12 20:16:03 +0800247 *
248 * RFC 8446 section 4.4.2.2
249 *
250 * If the client cannot construct an acceptable chain using the provided
Xiaokang Qian73437382023-03-29 08:24:12 +0000251 * certificates and decides to abort the handshake, then it MUST abort the
252 * handshake with an appropriate certificate-related alert
253 * (by default, "unsupported_certificate").
Jerry Yu30b071c2021-09-12 20:16:03 +0800254 *
Jerry Yu6f87f252021-10-29 20:12:51 +0800255 * Check if algorithm is an offered signature algorithm.
Jerry Yu30b071c2021-09-12 20:16:03 +0800256 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100257 if (!mbedtls_ssl_sig_alg_is_offered(ssl, algorithm)) {
Jerry Yu982d9e52021-10-14 15:59:37 +0800258 /* algorithm not in offered signature algorithms list */
Gilles Peskine449bd832023-01-11 14:50:10 +0100259 MBEDTLS_SSL_DEBUG_MSG(1, ("Received signature algorithm(%04x) is not "
260 "offered.",
261 (unsigned int) algorithm));
Jerry Yu6f87f252021-10-29 20:12:51 +0800262 goto error;
Jerry Yu30b071c2021-09-12 20:16:03 +0800263 }
264
Gilles Peskine449bd832023-01-11 14:50:10 +0100265 if (mbedtls_ssl_get_pk_type_and_md_alg_from_sig_alg(
266 algorithm, &sig_alg, &md_alg) != 0) {
Jerry Yu8c338862022-03-23 13:34:04 +0800267 goto error;
Jerry Yu30b071c2021-09-12 20:16:03 +0800268 }
269
Manuel Pégourié-Gonnard2d6d9932023-03-28 11:38:08 +0200270 hash_alg = mbedtls_md_psa_alg_from_type(md_alg);
Gilles Peskine449bd832023-01-11 14:50:10 +0100271 if (hash_alg == 0) {
pespaceka1378102022-04-26 15:03:11 +0200272 goto error;
273 }
274
Gilles Peskine449bd832023-01-11 14:50:10 +0100275 MBEDTLS_SSL_DEBUG_MSG(3, ("Certificate Verify: Signature algorithm ( %04x )",
276 (unsigned int) algorithm));
Jerry Yu30b071c2021-09-12 20:16:03 +0800277
278 /*
279 * Check the certificate's key type matches the signature alg
280 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100281 if (!mbedtls_pk_can_do(&ssl->session_negotiate->peer_cert->pk, sig_alg)) {
282 MBEDTLS_SSL_DEBUG_MSG(1, ("signature algorithm doesn't match cert key"));
Jerry Yu6f87f252021-10-29 20:12:51 +0800283 goto error;
Jerry Yu30b071c2021-09-12 20:16:03 +0800284 }
285
Gilles Peskine449bd832023-01-11 14:50:10 +0100286 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, 2);
287 signature_len = MBEDTLS_GET_UINT16_BE(p, 0);
Jerry Yu30b071c2021-09-12 20:16:03 +0800288 p += 2;
Gilles Peskine449bd832023-01-11 14:50:10 +0100289 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, signature_len);
Jerry Yu30b071c2021-09-12 20:16:03 +0800290
Gilles Peskine449bd832023-01-11 14:50:10 +0100291 status = psa_hash_compute(hash_alg,
292 verify_buffer,
293 verify_buffer_len,
294 verify_hash,
295 sizeof(verify_hash),
296 &verify_hash_len);
297 if (status != PSA_SUCCESS) {
298 MBEDTLS_SSL_DEBUG_RET(1, "hash computation PSA error", status);
Jerry Yu6f87f252021-10-29 20:12:51 +0800299 goto error;
Jerry Yu133690c2021-10-25 14:01:13 +0800300 }
301
Gilles Peskine449bd832023-01-11 14:50:10 +0100302 MBEDTLS_SSL_DEBUG_BUF(3, "verify hash", verify_hash, verify_hash_len);
Przemek Stekiel6a5e0182022-06-27 11:53:13 +0200303
Ben Taylor306ffd32025-07-07 09:41:34 +0100304 if ((ret = mbedtls_pk_verify_new(sig_alg,
Gilles Peskine449bd832023-01-11 14:50:10 +0100305 &ssl->session_negotiate->peer_cert->pk,
306 md_alg, verify_hash, verify_hash_len,
307 p, signature_len)) == 0) {
308 return 0;
Jerry Yu30b071c2021-09-12 20:16:03 +0800309 }
Ben Taylor0de87612025-07-14 08:27:01 +0100310 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_pk_verify_new", ret);
Jerry Yu30b071c2021-09-12 20:16:03 +0800311
Jerry Yu6f87f252021-10-29 20:12:51 +0800312error:
313 /* RFC 8446 section 4.4.3
314 *
315 * If the verification fails, the receiver MUST terminate the handshake
316 * with a "decrypt_error" alert.
Gilles Peskine449bd832023-01-11 14:50:10 +0100317 */
318 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_DECRYPT_ERROR,
319 MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE);
320 return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
Jerry Yu6f87f252021-10-29 20:12:51 +0800321
Jerry Yu30b071c2021-09-12 20:16:03 +0800322}
Ronald Cron928cbd32022-10-04 16:14:26 +0200323#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED */
Jerry Yu30b071c2021-09-12 20:16:03 +0800324
Gilles Peskine449bd832023-01-11 14:50:10 +0100325int mbedtls_ssl_tls13_process_certificate_verify(mbedtls_ssl_context *ssl)
Jerry Yu30b071c2021-09-12 20:16:03 +0800326{
Jerry Yu30b071c2021-09-12 20:16:03 +0800327
Ronald Cron928cbd32022-10-04 16:14:26 +0200328#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED)
Jerry Yuda8cdf22021-10-25 15:06:49 +0800329 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
330 unsigned char verify_buffer[SSL_VERIFY_STRUCT_MAX_SIZE];
331 size_t verify_buffer_len;
332 unsigned char transcript[MBEDTLS_TLS1_3_MD_MAX_SIZE];
333 size_t transcript_len;
334 unsigned char *buf;
335 size_t buf_len;
336
Gilles Peskine449bd832023-01-11 14:50:10 +0100337 MBEDTLS_SSL_DEBUG_MSG(2, ("=> parse certificate verify"));
Jerry Yu30b071c2021-09-12 20:16:03 +0800338
Jerry Yuda8cdf22021-10-25 15:06:49 +0800339 MBEDTLS_SSL_PROC_CHK(
Xiaokang Qian73437382023-03-29 08:24:12 +0000340 mbedtls_ssl_tls13_fetch_handshake_msg(
341 ssl, MBEDTLS_SSL_HS_CERTIFICATE_VERIFY, &buf, &buf_len));
Jerry Yu30b071c2021-09-12 20:16:03 +0800342
Jerry Yuda8cdf22021-10-25 15:06:49 +0800343 /* Need to calculate the hash of the transcript first
Jerry Yu0b32c502021-10-28 13:41:59 +0800344 * before reading the message since otherwise it gets
345 * included in the transcript
346 */
Xiaokang Qian73437382023-03-29 08:24:12 +0000347 ret = mbedtls_ssl_get_handshake_transcript(
348 ssl,
Dave Rodgman2eab4622023-10-05 13:30:37 +0100349 (mbedtls_md_type_t) ssl->handshake->ciphersuite_info->mac,
Xiaokang Qian73437382023-03-29 08:24:12 +0000350 transcript, sizeof(transcript),
351 &transcript_len);
Gilles Peskine449bd832023-01-11 14:50:10 +0100352 if (ret != 0) {
Jerry Yuda8cdf22021-10-25 15:06:49 +0800353 MBEDTLS_SSL_PEND_FATAL_ALERT(
354 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR,
Gilles Peskine449bd832023-01-11 14:50:10 +0100355 MBEDTLS_ERR_SSL_INTERNAL_ERROR);
356 return ret;
Jerry Yu30b071c2021-09-12 20:16:03 +0800357 }
358
Gilles Peskine449bd832023-01-11 14:50:10 +0100359 MBEDTLS_SSL_DEBUG_BUF(3, "handshake hash", transcript, transcript_len);
Jerry Yuda8cdf22021-10-25 15:06:49 +0800360
361 /* Create verify structure */
Gilles Peskine449bd832023-01-11 14:50:10 +0100362 ssl_tls13_create_verify_structure(transcript,
363 transcript_len,
364 verify_buffer,
365 &verify_buffer_len,
366 (ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT) ?
367 MBEDTLS_SSL_IS_SERVER :
368 MBEDTLS_SSL_IS_CLIENT);
Jerry Yuda8cdf22021-10-25 15:06:49 +0800369
370 /* Process the message contents */
Xiaokang Qian73437382023-03-29 08:24:12 +0000371 MBEDTLS_SSL_PROC_CHK(ssl_tls13_parse_certificate_verify(
372 ssl, buf, buf + buf_len,
373 verify_buffer, verify_buffer_len));
Jerry Yuda8cdf22021-10-25 15:06:49 +0800374
Xiaokang Qian73437382023-03-29 08:24:12 +0000375 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_add_hs_msg_to_checksum(
376 ssl, MBEDTLS_SSL_HS_CERTIFICATE_VERIFY,
377 buf, buf_len));
Jerry Yu30b071c2021-09-12 20:16:03 +0800378
379cleanup:
380
Gilles Peskine449bd832023-01-11 14:50:10 +0100381 MBEDTLS_SSL_DEBUG_MSG(2, ("<= parse certificate verify"));
382 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_tls13_process_certificate_verify", ret);
383 return ret;
Jerry Yuda8cdf22021-10-25 15:06:49 +0800384#else
385 ((void) ssl);
Gilles Peskine449bd832023-01-11 14:50:10 +0100386 MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
387 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Ronald Cron928cbd32022-10-04 16:14:26 +0200388#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED */
Jerry Yu30b071c2021-09-12 20:16:03 +0800389}
390
391/*
Xiaofei Bai947571e2021-09-29 09:12:03 +0000392 *
XiaokangQian6b916b12022-04-25 07:29:34 +0000393 * STATE HANDLING: Incoming Certificate.
Xiaofei Bai947571e2021-09-29 09:12:03 +0000394 *
395 */
396
Ronald Cronde08cf32022-10-04 17:15:35 +0200397#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED)
Xiaofei Bai947571e2021-09-29 09:12:03 +0000398#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
399/*
400 * Structure of Certificate message:
401 *
402 * enum {
403 * X509(0),
404 * RawPublicKey(2),
405 * (255)
406 * } CertificateType;
407 *
408 * struct {
409 * select (certificate_type) {
410 * case RawPublicKey:
411 * * From RFC 7250 ASN.1_subjectPublicKeyInfo *
412 * opaque ASN1_subjectPublicKeyInfo<1..2^24-1>;
413 * case X509:
414 * opaque cert_data<1..2^24-1>;
415 * };
416 * Extension extensions<0..2^16-1>;
417 * } CertificateEntry;
418 *
419 * struct {
420 * opaque certificate_request_context<0..2^8-1>;
421 * CertificateEntry certificate_list<0..2^24-1>;
422 * } Certificate;
423 *
424 */
Xiaofei Bai79595ac2021-10-26 07:16:45 +0000425
426/* Parse certificate chain send by the server. */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200427MBEDTLS_CHECK_RETURN_CRITICAL
Ronald Crone3dac4a2022-06-10 17:21:51 +0200428MBEDTLS_STATIC_TESTABLE
Gilles Peskine449bd832023-01-11 14:50:10 +0100429int mbedtls_ssl_tls13_parse_certificate(mbedtls_ssl_context *ssl,
430 const unsigned char *buf,
431 const unsigned char *end)
Xiaofei Bai947571e2021-09-29 09:12:03 +0000432{
433 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
434 size_t certificate_request_context_len = 0;
435 size_t certificate_list_len = 0;
436 const unsigned char *p = buf;
437 const unsigned char *certificate_list_end;
Jerry Yuc4bf5d62022-10-29 09:08:47 +0800438 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
Xiaofei Bai947571e2021-09-29 09:12:03 +0000439
Gilles Peskine449bd832023-01-11 14:50:10 +0100440 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, 4);
Xiaofei Bai947571e2021-09-29 09:12:03 +0000441 certificate_request_context_len = p[0];
Gilles Peskine449bd832023-01-11 14:50:10 +0100442 certificate_list_len = MBEDTLS_GET_UINT24_BE(p, 1);
XiaokangQian63e713e2022-05-15 04:26:57 +0000443 p += 4;
Xiaofei Bai947571e2021-09-29 09:12:03 +0000444
445 /* In theory, the certificate list can be up to 2^24 Bytes, but we don't
446 * support anything beyond 2^16 = 64K.
447 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100448 if ((certificate_request_context_len != 0) ||
449 (certificate_list_len >= 0x10000)) {
450 MBEDTLS_SSL_DEBUG_MSG(1, ("bad certificate message"));
451 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR,
452 MBEDTLS_ERR_SSL_DECODE_ERROR);
453 return MBEDTLS_ERR_SSL_DECODE_ERROR;
Xiaofei Bai947571e2021-09-29 09:12:03 +0000454 }
455
456 /* In case we tried to reuse a session but it failed */
Gilles Peskine449bd832023-01-11 14:50:10 +0100457 if (ssl->session_negotiate->peer_cert != NULL) {
458 mbedtls_x509_crt_free(ssl->session_negotiate->peer_cert);
459 mbedtls_free(ssl->session_negotiate->peer_cert);
Xiaofei Bai947571e2021-09-29 09:12:03 +0000460 }
461
Manuel Pégourié-Gonnardaefc5932024-08-12 10:36:40 +0200462 /* This is used by ssl_tls13_validate_certificate() */
Gilles Peskine449bd832023-01-11 14:50:10 +0100463 if (certificate_list_len == 0) {
XiaokangQianc3017f62022-05-13 05:55:41 +0000464 ssl->session_negotiate->peer_cert = NULL;
465 ret = 0;
466 goto exit;
467 }
468
Gilles Peskine449bd832023-01-11 14:50:10 +0100469 if ((ssl->session_negotiate->peer_cert =
470 mbedtls_calloc(1, sizeof(mbedtls_x509_crt))) == NULL) {
471 MBEDTLS_SSL_DEBUG_MSG(1, ("alloc( %" MBEDTLS_PRINTF_SIZET " bytes ) failed",
472 sizeof(mbedtls_x509_crt)));
473 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR,
474 MBEDTLS_ERR_SSL_ALLOC_FAILED);
475 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
Xiaofei Bai947571e2021-09-29 09:12:03 +0000476 }
477
Gilles Peskine449bd832023-01-11 14:50:10 +0100478 mbedtls_x509_crt_init(ssl->session_negotiate->peer_cert);
Xiaofei Bai947571e2021-09-29 09:12:03 +0000479
Gilles Peskine449bd832023-01-11 14:50:10 +0100480 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, certificate_list_len);
Xiaofei Bai947571e2021-09-29 09:12:03 +0000481 certificate_list_end = p + certificate_list_len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100482 while (p < certificate_list_end) {
Xiaofei Bai947571e2021-09-29 09:12:03 +0000483 size_t cert_data_len, extensions_len;
Jerry Yu2eaa7602022-08-04 17:28:15 +0800484 const unsigned char *extensions_end;
Xiaofei Bai947571e2021-09-29 09:12:03 +0000485
Gilles Peskine449bd832023-01-11 14:50:10 +0100486 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, certificate_list_end, 3);
487 cert_data_len = MBEDTLS_GET_UINT24_BE(p, 0);
Xiaofei Bai947571e2021-09-29 09:12:03 +0000488 p += 3;
489
490 /* In theory, the CRT can be up to 2^24 Bytes, but we don't support
491 * anything beyond 2^16 = 64K. Otherwise as in the TLS 1.2 code,
492 * check that we have a minimum of 128 bytes of data, this is not
493 * clear why we need that though.
494 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100495 if ((cert_data_len < 128) || (cert_data_len >= 0x10000)) {
496 MBEDTLS_SSL_DEBUG_MSG(1, ("bad Certificate message"));
497 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR,
498 MBEDTLS_ERR_SSL_DECODE_ERROR);
499 return MBEDTLS_ERR_SSL_DECODE_ERROR;
Xiaofei Bai947571e2021-09-29 09:12:03 +0000500 }
501
Gilles Peskine449bd832023-01-11 14:50:10 +0100502 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, certificate_list_end, cert_data_len);
503 ret = mbedtls_x509_crt_parse_der(ssl->session_negotiate->peer_cert,
504 p, cert_data_len);
Xiaofei Bai947571e2021-09-29 09:12:03 +0000505
Gilles Peskine449bd832023-01-11 14:50:10 +0100506 switch (ret) {
Xiaofei Bai947571e2021-09-29 09:12:03 +0000507 case 0: /*ok*/
508 break;
Gilles Peskine4c832212025-05-07 23:05:12 +0200509 case MBEDTLS_ERR_X509_UNKNOWN_OID:
Xiaofei Bai947571e2021-09-29 09:12:03 +0000510 /* Ignore certificate with an unknown algorithm: maybe a
511 prior certificate was already trusted. */
512 break;
513
514 case MBEDTLS_ERR_X509_ALLOC_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100515 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR,
516 MBEDTLS_ERR_X509_ALLOC_FAILED);
517 MBEDTLS_SSL_DEBUG_RET(1, " mbedtls_x509_crt_parse_der", ret);
518 return ret;
Xiaofei Bai947571e2021-09-29 09:12:03 +0000519
520 case MBEDTLS_ERR_X509_UNKNOWN_VERSION:
Gilles Peskine449bd832023-01-11 14:50:10 +0100521 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT,
522 MBEDTLS_ERR_X509_UNKNOWN_VERSION);
523 MBEDTLS_SSL_DEBUG_RET(1, " mbedtls_x509_crt_parse_der", ret);
524 return ret;
Xiaofei Bai947571e2021-09-29 09:12:03 +0000525
526 default:
Gilles Peskine449bd832023-01-11 14:50:10 +0100527 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_BAD_CERT,
528 ret);
529 MBEDTLS_SSL_DEBUG_RET(1, " mbedtls_x509_crt_parse_der", ret);
530 return ret;
Xiaofei Bai947571e2021-09-29 09:12:03 +0000531 }
532
533 p += cert_data_len;
534
535 /* Certificate extensions length */
Gilles Peskine449bd832023-01-11 14:50:10 +0100536 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, certificate_list_end, 2);
537 extensions_len = MBEDTLS_GET_UINT16_BE(p, 0);
Xiaofei Bai947571e2021-09-29 09:12:03 +0000538 p += 2;
Gilles Peskine449bd832023-01-11 14:50:10 +0100539 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, certificate_list_end, extensions_len);
Jerry Yu2eaa7602022-08-04 17:28:15 +0800540
541 extensions_end = p + extensions_len;
Jerry Yu0d5cfb72022-10-31 14:15:48 +0800542 handshake->received_extensions = MBEDTLS_SSL_EXT_MASK_NONE;
Jerry Yu2eaa7602022-08-04 17:28:15 +0800543
Gilles Peskine449bd832023-01-11 14:50:10 +0100544 while (p < extensions_end) {
Jerry Yu2eaa7602022-08-04 17:28:15 +0800545 unsigned int extension_type;
546 size_t extension_data_len;
547
548 /*
Gilles Peskine449bd832023-01-11 14:50:10 +0100549 * struct {
550 * ExtensionType extension_type; (2 bytes)
551 * opaque extension_data<0..2^16-1>;
552 * } Extension;
553 */
554 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, extensions_end, 4);
555 extension_type = MBEDTLS_GET_UINT16_BE(p, 0);
556 extension_data_len = MBEDTLS_GET_UINT16_BE(p, 2);
Jerry Yu2eaa7602022-08-04 17:28:15 +0800557 p += 4;
558
Gilles Peskine449bd832023-01-11 14:50:10 +0100559 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, extensions_end, extension_data_len);
Jerry Yu2eaa7602022-08-04 17:28:15 +0800560
Jerry Yuc4bf5d62022-10-29 09:08:47 +0800561 ret = mbedtls_ssl_tls13_check_received_extension(
Gilles Peskine449bd832023-01-11 14:50:10 +0100562 ssl, MBEDTLS_SSL_HS_CERTIFICATE, extension_type,
563 MBEDTLS_SSL_TLS1_3_ALLOWED_EXTS_OF_CT);
564 if (ret != 0) {
565 return ret;
566 }
Jerry Yu0c354a22022-08-29 15:25:36 +0800567
Gilles Peskine449bd832023-01-11 14:50:10 +0100568 switch (extension_type) {
Jerry Yu2eaa7602022-08-04 17:28:15 +0800569 default:
Jerry Yu79aa7212022-11-08 21:30:21 +0800570 MBEDTLS_SSL_PRINT_EXT(
Jerry Yu0d5cfb72022-10-31 14:15:48 +0800571 3, MBEDTLS_SSL_HS_CERTIFICATE,
Gilles Peskine449bd832023-01-11 14:50:10 +0100572 extension_type, "( ignored )");
Jerry Yu2eaa7602022-08-04 17:28:15 +0800573 break;
574 }
575
576 p += extension_data_len;
577 }
578
Gilles Peskine449bd832023-01-11 14:50:10 +0100579 MBEDTLS_SSL_PRINT_EXTS(3, MBEDTLS_SSL_HS_CERTIFICATE,
580 handshake->received_extensions);
Xiaofei Bai947571e2021-09-29 09:12:03 +0000581 }
582
XiaokangQian63e713e2022-05-15 04:26:57 +0000583exit:
Xiaofei Bai947571e2021-09-29 09:12:03 +0000584 /* Check that all the message is consumed. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100585 if (p != end) {
586 MBEDTLS_SSL_DEBUG_MSG(1, ("bad Certificate message"));
587 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR,
588 MBEDTLS_ERR_SSL_DECODE_ERROR);
589 return MBEDTLS_ERR_SSL_DECODE_ERROR;
Xiaofei Bai947571e2021-09-29 09:12:03 +0000590 }
591
Xiaokang Qian73437382023-03-29 08:24:12 +0000592 MBEDTLS_SSL_DEBUG_CRT(3, "peer certificate",
593 ssl->session_negotiate->peer_cert);
Xiaofei Bai947571e2021-09-29 09:12:03 +0000594
Gilles Peskine449bd832023-01-11 14:50:10 +0100595 return ret;
Xiaofei Bai947571e2021-09-29 09:12:03 +0000596}
597#else
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200598MBEDTLS_CHECK_RETURN_CRITICAL
Ronald Crone3dac4a2022-06-10 17:21:51 +0200599MBEDTLS_STATIC_TESTABLE
Gilles Peskine449bd832023-01-11 14:50:10 +0100600int mbedtls_ssl_tls13_parse_certificate(mbedtls_ssl_context *ssl,
601 const unsigned char *buf,
602 const unsigned char *end)
Xiaofei Bai947571e2021-09-29 09:12:03 +0000603{
604 ((void) ssl);
605 ((void) buf);
606 ((void) end);
Gilles Peskine449bd832023-01-11 14:50:10 +0100607 return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Xiaofei Bai947571e2021-09-29 09:12:03 +0000608}
609#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Ronald Cronde08cf32022-10-04 17:15:35 +0200610#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED */
Xiaofei Bai947571e2021-09-29 09:12:03 +0000611
Ronald Cronde08cf32022-10-04 17:15:35 +0200612#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED)
Xiaofei Bai947571e2021-09-29 09:12:03 +0000613#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Xiaofei Bai79595ac2021-10-26 07:16:45 +0000614/* Validate certificate chain sent by the server. */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200615MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100616static int ssl_tls13_validate_certificate(mbedtls_ssl_context *ssl)
Xiaofei Bai947571e2021-09-29 09:12:03 +0000617{
Manuel Pégourié-Gonnard58ab9ba2024-08-14 09:47:38 +0200618 /* Authmode: precedence order is SNI if used else configuration */
619#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
620 const int authmode = ssl->handshake->sni_authmode != MBEDTLS_SSL_VERIFY_UNSET
621 ? ssl->handshake->sni_authmode
622 : ssl->conf->authmode;
623#else
624 const int authmode = ssl->conf->authmode;
XiaokangQian6b916b12022-04-25 07:29:34 +0000625#endif
626
627 /*
XiaokangQian989f06d2022-05-17 01:50:15 +0000628 * If the peer hasn't sent a certificate ( i.e. it sent
XiaokangQian6b916b12022-04-25 07:29:34 +0000629 * an empty certificate chain ), this is reflected in the peer CRT
630 * structure being unset.
631 * Check for that and handle it depending on the
XiaokangQian989f06d2022-05-17 01:50:15 +0000632 * authentication mode.
XiaokangQian6b916b12022-04-25 07:29:34 +0000633 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100634 if (ssl->session_negotiate->peer_cert == NULL) {
635 MBEDTLS_SSL_DEBUG_MSG(1, ("peer has no certificate"));
XiaokangQian989f06d2022-05-17 01:50:15 +0000636
XiaokangQian63e713e2022-05-15 04:26:57 +0000637#if defined(MBEDTLS_SSL_SRV_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100638 if (ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER) {
XiaokangQian63e713e2022-05-15 04:26:57 +0000639 /* The client was asked for a certificate but didn't send
640 * one. The client should know what's going on, so we
641 * don't send an alert.
642 */
643 ssl->session_negotiate->verify_result = MBEDTLS_X509_BADCERT_MISSING;
Gilles Peskine449bd832023-01-11 14:50:10 +0100644 if (authmode == MBEDTLS_SSL_VERIFY_OPTIONAL) {
645 return 0;
646 } else {
Xiaokang Qian73437382023-03-29 08:24:12 +0000647 MBEDTLS_SSL_PEND_FATAL_ALERT(
648 MBEDTLS_SSL_ALERT_MSG_NO_CERT,
649 MBEDTLS_ERR_SSL_NO_CLIENT_CERTIFICATE);
Gilles Peskine449bd832023-01-11 14:50:10 +0100650 return MBEDTLS_ERR_SSL_NO_CLIENT_CERTIFICATE;
XiaokangQian989f06d2022-05-17 01:50:15 +0000651 }
XiaokangQian63e713e2022-05-15 04:26:57 +0000652 }
XiaokangQian6b916b12022-04-25 07:29:34 +0000653#endif /* MBEDTLS_SSL_SRV_C */
654
XiaokangQianc3017f62022-05-13 05:55:41 +0000655#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnardaefc5932024-08-12 10:36:40 +0200656 /* Regardless of authmode, the server is not allowed to send an empty
657 * certificate chain. (Last paragraph before 4.4.2.1 in RFC 8446: "The
658 * server's certificate_list MUST always be non-empty.") With authmode
659 * optional/none, we continue the handshake if we can't validate the
660 * server's cert, but we still break it if no certificate was sent. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100661 if (ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT) {
662 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_NO_CERT,
663 MBEDTLS_ERR_SSL_FATAL_ALERT_MESSAGE);
664 return MBEDTLS_ERR_SSL_FATAL_ALERT_MESSAGE;
XiaokangQian63e713e2022-05-15 04:26:57 +0000665 }
XiaokangQianc3017f62022-05-13 05:55:41 +0000666#endif /* MBEDTLS_SSL_CLI_C */
XiaokangQian63e713e2022-05-15 04:26:57 +0000667 }
XiaokangQian6b916b12022-04-25 07:29:34 +0000668
Manuel Pégourié-Gonnard19dd9f52024-08-16 11:03:42 +0200669 return mbedtls_ssl_verify_certificate(ssl, authmode,
670 ssl->session_negotiate->peer_cert,
671 NULL, NULL);
Xiaofei Bai947571e2021-09-29 09:12:03 +0000672}
673#else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200674MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100675static int ssl_tls13_validate_certificate(mbedtls_ssl_context *ssl)
Xiaofei Bai947571e2021-09-29 09:12:03 +0000676{
677 ((void) ssl);
Gilles Peskine449bd832023-01-11 14:50:10 +0100678 return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Xiaofei Bai947571e2021-09-29 09:12:03 +0000679}
680#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Ronald Cronde08cf32022-10-04 17:15:35 +0200681#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED */
Xiaofei Bai947571e2021-09-29 09:12:03 +0000682
Gilles Peskine449bd832023-01-11 14:50:10 +0100683int mbedtls_ssl_tls13_process_certificate(mbedtls_ssl_context *ssl)
Xiaofei Bai947571e2021-09-29 09:12:03 +0000684{
Xiaofei Bai79595ac2021-10-26 07:16:45 +0000685 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine449bd832023-01-11 14:50:10 +0100686 MBEDTLS_SSL_DEBUG_MSG(2, ("=> parse certificate"));
Xiaofei Bai79595ac2021-10-26 07:16:45 +0000687
Ronald Cronde08cf32022-10-04 17:15:35 +0200688#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED)
XiaokangQianc3017f62022-05-13 05:55:41 +0000689 unsigned char *buf;
690 size_t buf_len;
Xiaofei Bai79595ac2021-10-26 07:16:45 +0000691
Gilles Peskine449bd832023-01-11 14:50:10 +0100692 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_tls13_fetch_handshake_msg(
693 ssl, MBEDTLS_SSL_HS_CERTIFICATE,
694 &buf, &buf_len));
Xiaofei Bai79595ac2021-10-26 07:16:45 +0000695
XiaokangQianc3017f62022-05-13 05:55:41 +0000696 /* Parse the certificate chain sent by the peer. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100697 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_tls13_parse_certificate(ssl, buf,
698 buf + buf_len));
XiaokangQianc3017f62022-05-13 05:55:41 +0000699 /* Validate the certificate chain and set the verification results. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100700 MBEDTLS_SSL_PROC_CHK(ssl_tls13_validate_certificate(ssl));
Xiaofei Bai79595ac2021-10-26 07:16:45 +0000701
Xiaokang Qian73437382023-03-29 08:24:12 +0000702 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_add_hs_msg_to_checksum(
703 ssl, MBEDTLS_SSL_HS_CERTIFICATE, buf, buf_len));
Xiaofei Bai79595ac2021-10-26 07:16:45 +0000704
Xiaofei Bai79595ac2021-10-26 07:16:45 +0000705cleanup:
Gilles Peskineff2558a2023-09-05 21:10:39 +0200706#else /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED */
707 (void) ssl;
Ronald Cronde08cf32022-10-04 17:15:35 +0200708#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED */
Xiaofei Bai79595ac2021-10-26 07:16:45 +0000709
Gilles Peskine449bd832023-01-11 14:50:10 +0100710 MBEDTLS_SSL_DEBUG_MSG(2, ("<= parse certificate"));
711 return ret;
Xiaofei Bai947571e2021-09-29 09:12:03 +0000712}
Ronald Cron928cbd32022-10-04 16:14:26 +0200713#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED)
Jerry Yu7399d0d2022-01-30 17:54:19 +0800714/*
715 * enum {
716 * X509(0),
717 * RawPublicKey(2),
718 * (255)
719 * } CertificateType;
720 *
721 * struct {
722 * select (certificate_type) {
723 * case RawPublicKey:
724 * // From RFC 7250 ASN.1_subjectPublicKeyInfo
725 * opaque ASN1_subjectPublicKeyInfo<1..2^24-1>;
726 *
727 * case X509:
728 * opaque cert_data<1..2^24-1>;
729 * };
730 * Extension extensions<0..2^16-1>;
731 * } CertificateEntry;
732 *
733 * struct {
734 * opaque certificate_request_context<0..2^8-1>;
735 * CertificateEntry certificate_list<0..2^24-1>;
736 * } Certificate;
737 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200738MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100739static int ssl_tls13_write_certificate_body(mbedtls_ssl_context *ssl,
740 unsigned char *buf,
741 unsigned char *end,
742 size_t *out_len)
Jerry Yu5cc35062022-01-28 16:16:08 +0800743{
Gilles Peskine449bd832023-01-11 14:50:10 +0100744 const mbedtls_x509_crt *crt = mbedtls_ssl_own_cert(ssl);
Jerry Yu3e536442022-02-15 11:05:59 +0800745 unsigned char *p = buf;
Jerry Yuc8d8d4e2022-02-18 12:10:03 +0800746 unsigned char *certificate_request_context =
Gilles Peskine449bd832023-01-11 14:50:10 +0100747 ssl->handshake->certificate_request_context;
Jerry Yuc8d8d4e2022-02-18 12:10:03 +0800748 unsigned char certificate_request_context_len =
Gilles Peskine449bd832023-01-11 14:50:10 +0100749 ssl->handshake->certificate_request_context_len;
Jerry Yuc8d8d4e2022-02-18 12:10:03 +0800750 unsigned char *p_certificate_list_len;
Jerry Yu5cc35062022-01-28 16:16:08 +0800751
Jerry Yu5cc35062022-01-28 16:16:08 +0800752
Jerry Yu3391ac02022-02-16 11:21:37 +0800753 /* ...
754 * opaque certificate_request_context<0..2^8-1>;
755 * ...
756 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100757 MBEDTLS_SSL_CHK_BUF_PTR(p, end, certificate_request_context_len + 1);
Jerry Yuc8d8d4e2022-02-18 12:10:03 +0800758 *p++ = certificate_request_context_len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100759 if (certificate_request_context_len > 0) {
760 memcpy(p, certificate_request_context, certificate_request_context_len);
Jerry Yuc8d8d4e2022-02-18 12:10:03 +0800761 p += certificate_request_context_len;
Jerry Yu537530d2022-02-15 14:00:57 +0800762 }
763
Jerry Yu3391ac02022-02-16 11:21:37 +0800764 /* ...
765 * CertificateEntry certificate_list<0..2^24-1>;
766 * ...
767 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100768 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 3);
Jerry Yuc8d8d4e2022-02-18 12:10:03 +0800769 p_certificate_list_len = p;
Jerry Yu3e536442022-02-15 11:05:59 +0800770 p += 3;
771
Gilles Peskine449bd832023-01-11 14:50:10 +0100772 MBEDTLS_SSL_DEBUG_CRT(3, "own certificate", crt);
Jerry Yu5cc35062022-01-28 16:16:08 +0800773
Gilles Peskine449bd832023-01-11 14:50:10 +0100774 while (crt != NULL) {
Jerry Yu7399d0d2022-01-30 17:54:19 +0800775 size_t cert_data_len = crt->raw.len;
Jerry Yu5cc35062022-01-28 16:16:08 +0800776
Gilles Peskine449bd832023-01-11 14:50:10 +0100777 MBEDTLS_SSL_CHK_BUF_PTR(p, end, cert_data_len + 3 + 2);
778 MBEDTLS_PUT_UINT24_BE(cert_data_len, p, 0);
Jerry Yu7399d0d2022-01-30 17:54:19 +0800779 p += 3;
Jerry Yu5cc35062022-01-28 16:16:08 +0800780
Gilles Peskine449bd832023-01-11 14:50:10 +0100781 memcpy(p, crt->raw.p, cert_data_len);
Jerry Yu7399d0d2022-01-30 17:54:19 +0800782 p += cert_data_len;
783 crt = crt->next;
Jerry Yu5cc35062022-01-28 16:16:08 +0800784
785 /* Currently, we don't have any certificate extensions defined.
786 * Hence, we are sending an empty extension with length zero.
787 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100788 MBEDTLS_PUT_UINT16_BE(0, p, 0);
Jerry Yu7399d0d2022-01-30 17:54:19 +0800789 p += 2;
Jerry Yu5cc35062022-01-28 16:16:08 +0800790 }
Jerry Yu5cc35062022-01-28 16:16:08 +0800791
Gilles Peskine449bd832023-01-11 14:50:10 +0100792 MBEDTLS_PUT_UINT24_BE(p - p_certificate_list_len - 3,
793 p_certificate_list_len, 0);
Jerry Yu7399d0d2022-01-30 17:54:19 +0800794
Jerry Yu3e536442022-02-15 11:05:59 +0800795 *out_len = p - buf;
Jerry Yu5cc35062022-01-28 16:16:08 +0800796
Jerry Yu7de2ff02022-11-08 21:43:46 +0800797 MBEDTLS_SSL_PRINT_EXTS(
Gilles Peskine449bd832023-01-11 14:50:10 +0100798 3, MBEDTLS_SSL_HS_CERTIFICATE, ssl->handshake->sent_extensions);
Jerry Yu4b8f2f72022-10-31 13:31:22 +0800799
Gilles Peskine449bd832023-01-11 14:50:10 +0100800 return 0;
Jerry Yu5cc35062022-01-28 16:16:08 +0800801}
Jerry Yu5cc35062022-01-28 16:16:08 +0800802
Gilles Peskine449bd832023-01-11 14:50:10 +0100803int mbedtls_ssl_tls13_write_certificate(mbedtls_ssl_context *ssl)
Jerry Yu5cc35062022-01-28 16:16:08 +0800804{
805 int ret;
Ronald Cron5bb8fc82022-03-09 07:00:13 +0100806 unsigned char *buf;
807 size_t buf_len, msg_len;
808
Gilles Peskine449bd832023-01-11 14:50:10 +0100809 MBEDTLS_SSL_DEBUG_MSG(2, ("=> write certificate"));
Jerry Yu5cc35062022-01-28 16:16:08 +0800810
Xiaokang Qian73437382023-03-29 08:24:12 +0000811 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_start_handshake_msg(
812 ssl, MBEDTLS_SSL_HS_CERTIFICATE, &buf, &buf_len));
Jerry Yu5cc35062022-01-28 16:16:08 +0800813
Gilles Peskine449bd832023-01-11 14:50:10 +0100814 MBEDTLS_SSL_PROC_CHK(ssl_tls13_write_certificate_body(ssl,
815 buf,
816 buf + buf_len,
817 &msg_len));
Jerry Yu5cc35062022-01-28 16:16:08 +0800818
Xiaokang Qian73437382023-03-29 08:24:12 +0000819 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_add_hs_msg_to_checksum(
820 ssl, MBEDTLS_SSL_HS_CERTIFICATE, buf, msg_len));
Jerry Yu5cc35062022-01-28 16:16:08 +0800821
Gilles Peskine449bd832023-01-11 14:50:10 +0100822 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_finish_handshake_msg(
823 ssl, buf_len, msg_len));
Jerry Yu5cc35062022-01-28 16:16:08 +0800824cleanup:
825
Gilles Peskine449bd832023-01-11 14:50:10 +0100826 MBEDTLS_SSL_DEBUG_MSG(2, ("<= write certificate"));
827 return ret;
Jerry Yu5cc35062022-01-28 16:16:08 +0800828}
829
Jerry Yu3e536442022-02-15 11:05:59 +0800830/*
831 * STATE HANDLING: Output Certificate Verify
832 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100833int mbedtls_ssl_tls13_check_sig_alg_cert_key_match(uint16_t sig_alg,
834 mbedtls_pk_context *key)
Jerry Yu0c6be8f2022-06-20 20:42:00 +0800835{
Dave Rodgman2eab4622023-10-05 13:30:37 +0100836 mbedtls_pk_type_t pk_type = (mbedtls_pk_type_t) mbedtls_ssl_sig_from_pk(key);
Gilles Peskine449bd832023-01-11 14:50:10 +0100837 size_t key_size = mbedtls_pk_get_bitlen(key);
Jerry Yu0c6be8f2022-06-20 20:42:00 +0800838
Gilles Peskine449bd832023-01-11 14:50:10 +0100839 switch (pk_type) {
Jerry Yu67eced02022-02-25 13:37:36 +0800840 case MBEDTLS_SSL_SIG_ECDSA:
Gilles Peskine449bd832023-01-11 14:50:10 +0100841 switch (key_size) {
Jerry Yu67eced02022-02-25 13:37:36 +0800842 case 256:
Gilles Peskine449bd832023-01-11 14:50:10 +0100843 return
844 sig_alg == MBEDTLS_TLS1_3_SIG_ECDSA_SECP256R1_SHA256;
Jerry Yu0c6be8f2022-06-20 20:42:00 +0800845
Jerry Yu67eced02022-02-25 13:37:36 +0800846 case 384:
Gilles Peskine449bd832023-01-11 14:50:10 +0100847 return
848 sig_alg == MBEDTLS_TLS1_3_SIG_ECDSA_SECP384R1_SHA384;
Jerry Yu0c6be8f2022-06-20 20:42:00 +0800849
Jerry Yu67eced02022-02-25 13:37:36 +0800850 case 521:
Gilles Peskine449bd832023-01-11 14:50:10 +0100851 return
852 sig_alg == MBEDTLS_TLS1_3_SIG_ECDSA_SECP521R1_SHA512;
Jerry Yu67eced02022-02-25 13:37:36 +0800853 default:
Jerry Yu67eced02022-02-25 13:37:36 +0800854 break;
855 }
856 break;
Jerry Yu67eced02022-02-25 13:37:36 +0800857
Jerry Yu67eced02022-02-25 13:37:36 +0800858 case MBEDTLS_SSL_SIG_RSA:
Gilles Peskine449bd832023-01-11 14:50:10 +0100859 switch (sig_alg) {
Ronald Cron38391bf2022-09-16 11:19:27 +0200860 case MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA256: /* Intentional fallthrough */
861 case MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA384: /* Intentional fallthrough */
Jerry Yu0c6be8f2022-06-20 20:42:00 +0800862 case MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA512:
Gilles Peskine449bd832023-01-11 14:50:10 +0100863 return 1;
Jerry Yuc2e04932022-06-27 22:13:03 +0800864
Jerry Yu0c6be8f2022-06-20 20:42:00 +0800865 default:
866 break;
Jerry Yucef3f332022-03-22 23:00:13 +0800867 }
Jerry Yu67eced02022-02-25 13:37:36 +0800868 break;
Jerry Yu0c6be8f2022-06-20 20:42:00 +0800869
Jerry Yu67eced02022-02-25 13:37:36 +0800870 default:
Jerry Yu67eced02022-02-25 13:37:36 +0800871 break;
872 }
Jerry Yu0c6be8f2022-06-20 20:42:00 +0800873
Gilles Peskine449bd832023-01-11 14:50:10 +0100874 return 0;
Jerry Yu0c6be8f2022-06-20 20:42:00 +0800875}
876
Ronald Cronce7d76e2022-07-08 18:56:49 +0200877MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100878static int ssl_tls13_write_certificate_verify_body(mbedtls_ssl_context *ssl,
879 unsigned char *buf,
880 unsigned char *end,
881 size_t *out_len)
Jerry Yu8511f122022-01-29 10:01:04 +0800882{
Ronald Cron067a1e72022-09-16 13:44:49 +0200883 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yu3e536442022-02-15 11:05:59 +0800884 unsigned char *p = buf;
Jerry Yu8511f122022-01-29 10:01:04 +0800885 mbedtls_pk_context *own_key;
Jerry Yu3e536442022-02-15 11:05:59 +0800886
Gilles Peskine449bd832023-01-11 14:50:10 +0100887 unsigned char handshake_hash[MBEDTLS_TLS1_3_MD_MAX_SIZE];
Jerry Yu8511f122022-01-29 10:01:04 +0800888 size_t handshake_hash_len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100889 unsigned char verify_buffer[SSL_VERIFY_STRUCT_MAX_SIZE];
Jerry Yu3e536442022-02-15 11:05:59 +0800890 size_t verify_buffer_len;
Ronald Cron067a1e72022-09-16 13:44:49 +0200891
892 uint16_t *sig_alg = ssl->handshake->received_sig_algs;
Jerry Yu3e536442022-02-15 11:05:59 +0800893 size_t signature_len = 0;
Jerry Yu8511f122022-01-29 10:01:04 +0800894
Jerry Yu0b7b1012022-02-23 12:23:05 +0800895 *out_len = 0;
896
Gilles Peskine449bd832023-01-11 14:50:10 +0100897 own_key = mbedtls_ssl_own_key(ssl);
898 if (own_key == NULL) {
899 MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
900 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Jerry Yu8511f122022-01-29 10:01:04 +0800901 }
902
Xiaokang Qian73437382023-03-29 08:24:12 +0000903 ret = mbedtls_ssl_get_handshake_transcript(
Dave Rodgman2eab4622023-10-05 13:30:37 +0100904 ssl, (mbedtls_md_type_t) ssl->handshake->ciphersuite_info->mac,
Xiaokang Qian73437382023-03-29 08:24:12 +0000905 handshake_hash, sizeof(handshake_hash), &handshake_hash_len);
Gilles Peskine449bd832023-01-11 14:50:10 +0100906 if (ret != 0) {
907 return ret;
908 }
Jerry Yu8511f122022-01-29 10:01:04 +0800909
Gilles Peskine449bd832023-01-11 14:50:10 +0100910 MBEDTLS_SSL_DEBUG_BUF(3, "handshake hash",
911 handshake_hash,
912 handshake_hash_len);
Jerry Yu8511f122022-01-29 10:01:04 +0800913
Gilles Peskine449bd832023-01-11 14:50:10 +0100914 ssl_tls13_create_verify_structure(handshake_hash, handshake_hash_len,
915 verify_buffer, &verify_buffer_len,
916 ssl->conf->endpoint);
Jerry Yu8511f122022-01-29 10:01:04 +0800917
918 /*
919 * struct {
920 * SignatureScheme algorithm;
921 * opaque signature<0..2^16-1>;
922 * } CertificateVerify;
923 */
Ronald Cron067a1e72022-09-16 13:44:49 +0200924 /* Check there is space for the algorithm identifier (2 bytes) and the
925 * signature length (2 bytes).
926 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100927 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 4);
Ronald Cron067a1e72022-09-16 13:44:49 +0200928
Gilles Peskine449bd832023-01-11 14:50:10 +0100929 for (; *sig_alg != MBEDTLS_TLS1_3_SIG_NONE; sig_alg++) {
Ronald Cron067a1e72022-09-16 13:44:49 +0200930 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
931 mbedtls_pk_type_t pk_type = MBEDTLS_PK_NONE;
932 mbedtls_md_type_t md_alg = MBEDTLS_MD_NONE;
933 psa_algorithm_t psa_algorithm = PSA_ALG_NONE;
934 unsigned char verify_hash[PSA_HASH_MAX_SIZE];
935 size_t verify_hash_len;
Jerry Yu67eced02022-02-25 13:37:36 +0800936
Gilles Peskine449bd832023-01-11 14:50:10 +0100937 if (!mbedtls_ssl_sig_alg_is_offered(ssl, *sig_alg)) {
Ronald Cron067a1e72022-09-16 13:44:49 +0200938 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +0100939 }
Jerry Yu67eced02022-02-25 13:37:36 +0800940
Gilles Peskine449bd832023-01-11 14:50:10 +0100941 if (!mbedtls_ssl_tls13_sig_alg_for_cert_verify_is_supported(*sig_alg)) {
Ronald Cron067a1e72022-09-16 13:44:49 +0200942 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +0100943 }
Ronald Cron067a1e72022-09-16 13:44:49 +0200944
Gilles Peskine449bd832023-01-11 14:50:10 +0100945 if (!mbedtls_ssl_tls13_check_sig_alg_cert_key_match(*sig_alg, own_key)) {
Ronald Cron067a1e72022-09-16 13:44:49 +0200946 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +0100947 }
Ronald Cron067a1e72022-09-16 13:44:49 +0200948
Gilles Peskine449bd832023-01-11 14:50:10 +0100949 if (mbedtls_ssl_get_pk_type_and_md_alg_from_sig_alg(
950 *sig_alg, &pk_type, &md_alg) != 0) {
951 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Ronald Cron067a1e72022-09-16 13:44:49 +0200952 }
953
954 /* Hash verify buffer with indicated hash function */
Manuel Pégourié-Gonnard2d6d9932023-03-28 11:38:08 +0200955 psa_algorithm = mbedtls_md_psa_alg_from_type(md_alg);
Gilles Peskine449bd832023-01-11 14:50:10 +0100956 status = psa_hash_compute(psa_algorithm,
957 verify_buffer,
958 verify_buffer_len,
959 verify_hash, sizeof(verify_hash),
960 &verify_hash_len);
961 if (status != PSA_SUCCESS) {
Andrzej Kurek8a045ce2022-12-23 11:00:06 -0500962 return PSA_TO_MBEDTLS_ERR(status);
Ronald Cron067a1e72022-09-16 13:44:49 +0200963 }
964
Gilles Peskine449bd832023-01-11 14:50:10 +0100965 MBEDTLS_SSL_DEBUG_BUF(3, "verify hash", verify_hash, verify_hash_len);
966
967 if ((ret = mbedtls_pk_sign_ext(pk_type, own_key,
968 md_alg, verify_hash, verify_hash_len,
Ben Taylor440cb2a2025-03-05 09:40:08 +0000969 p + 4, (size_t) (end - (p + 4)), &signature_len)) != 0) {
Gilles Peskine449bd832023-01-11 14:50:10 +0100970 MBEDTLS_SSL_DEBUG_MSG(2, ("CertificateVerify signature failed with %s",
971 mbedtls_ssl_sig_alg_to_str(*sig_alg)));
972 MBEDTLS_SSL_DEBUG_RET(2, "mbedtls_pk_sign_ext", ret);
973
974 /* The signature failed. This is possible if the private key
975 * was not suitable for the signature operation as purposely we
976 * did not check its suitability completely. Let's try with
977 * another signature algorithm.
978 */
979 continue;
980 }
981
982 MBEDTLS_SSL_DEBUG_MSG(2, ("CertificateVerify signature with %s",
983 mbedtls_ssl_sig_alg_to_str(*sig_alg)));
Ronald Cron067a1e72022-09-16 13:44:49 +0200984
985 break;
986 }
987
Gilles Peskine449bd832023-01-11 14:50:10 +0100988 if (*sig_alg == MBEDTLS_TLS1_3_SIG_NONE) {
989 MBEDTLS_SSL_DEBUG_MSG(1, ("no suitable signature algorithm"));
990 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE,
991 MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE);
992 return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
Jerry Yu8511f122022-01-29 10:01:04 +0800993 }
994
Gilles Peskine449bd832023-01-11 14:50:10 +0100995 MBEDTLS_PUT_UINT16_BE(*sig_alg, p, 0);
996 MBEDTLS_PUT_UINT16_BE(signature_len, p, 2);
Jerry Yuf3b46b52022-06-19 16:52:27 +0800997
Ronald Cron067a1e72022-09-16 13:44:49 +0200998 *out_len = 4 + signature_len;
Jerry Yu8c338862022-03-23 13:34:04 +0800999
Gilles Peskine449bd832023-01-11 14:50:10 +01001000 return 0;
Jerry Yu8511f122022-01-29 10:01:04 +08001001}
Jerry Yu8511f122022-01-29 10:01:04 +08001002
Gilles Peskine449bd832023-01-11 14:50:10 +01001003int mbedtls_ssl_tls13_write_certificate_verify(mbedtls_ssl_context *ssl)
Jerry Yu8511f122022-01-29 10:01:04 +08001004{
1005 int ret = 0;
Jerry Yuca133a32022-02-15 14:22:05 +08001006 unsigned char *buf;
1007 size_t buf_len, msg_len;
1008
Gilles Peskine449bd832023-01-11 14:50:10 +01001009 MBEDTLS_SSL_DEBUG_MSG(2, ("=> write certificate verify"));
Jerry Yu8511f122022-01-29 10:01:04 +08001010
Xiaokang Qian73437382023-03-29 08:24:12 +00001011 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_start_handshake_msg(
1012 ssl, MBEDTLS_SSL_HS_CERTIFICATE_VERIFY,
1013 &buf, &buf_len));
Jerry Yu8511f122022-01-29 10:01:04 +08001014
Gilles Peskine449bd832023-01-11 14:50:10 +01001015 MBEDTLS_SSL_PROC_CHK(ssl_tls13_write_certificate_verify_body(
1016 ssl, buf, buf + buf_len, &msg_len));
Jerry Yu8511f122022-01-29 10:01:04 +08001017
Xiaokang Qian73437382023-03-29 08:24:12 +00001018 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_add_hs_msg_to_checksum(
1019 ssl, MBEDTLS_SSL_HS_CERTIFICATE_VERIFY,
1020 buf, msg_len));
Jerry Yu8511f122022-01-29 10:01:04 +08001021
Gilles Peskine449bd832023-01-11 14:50:10 +01001022 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_finish_handshake_msg(
1023 ssl, buf_len, msg_len));
Jerry Yu8511f122022-01-29 10:01:04 +08001024
1025cleanup:
1026
Gilles Peskine449bd832023-01-11 14:50:10 +01001027 MBEDTLS_SSL_DEBUG_MSG(2, ("<= write certificate verify"));
1028 return ret;
Jerry Yu8511f122022-01-29 10:01:04 +08001029}
1030
Ronald Cron928cbd32022-10-04 16:14:26 +02001031#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED */
Jerry Yu90f152d2022-01-29 22:12:42 +08001032
Jerry Yu5cc35062022-01-28 16:16:08 +08001033/*
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001034 *
XiaokangQianc5c39d52021-11-09 11:55:10 +00001035 * STATE HANDLING: Incoming Finished message.
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001036 */
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001037/*
1038 * Implementation
1039 */
1040
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001041MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01001042static int ssl_tls13_preprocess_finished_message(mbedtls_ssl_context *ssl)
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001043{
1044 int ret;
1045
Xiaokang Qian73437382023-03-29 08:24:12 +00001046 ret = mbedtls_ssl_tls13_calculate_verify_data(
1047 ssl,
1048 ssl->handshake->state_local.finished_in.digest,
1049 sizeof(ssl->handshake->state_local.finished_in.digest),
1050 &ssl->handshake->state_local.finished_in.digest_len,
1051 ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT ?
1052 MBEDTLS_SSL_IS_SERVER : MBEDTLS_SSL_IS_CLIENT);
Gilles Peskine449bd832023-01-11 14:50:10 +01001053 if (ret != 0) {
1054 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_tls13_calculate_verify_data", ret);
1055 return ret;
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001056 }
1057
Gilles Peskine449bd832023-01-11 14:50:10 +01001058 return 0;
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001059}
1060
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001061MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01001062static int ssl_tls13_parse_finished_message(mbedtls_ssl_context *ssl,
1063 const unsigned char *buf,
1064 const unsigned char *end)
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001065{
XiaokangQian33062842021-11-11 03:37:45 +00001066 /*
1067 * struct {
XiaokangQianc13f9352021-11-11 06:13:22 +00001068 * opaque verify_data[Hash.length];
XiaokangQian33062842021-11-11 03:37:45 +00001069 * } Finished;
1070 */
1071 const unsigned char *expected_verify_data =
1072 ssl->handshake->state_local.finished_in.digest;
1073 size_t expected_verify_data_len =
1074 ssl->handshake->state_local.finished_in.digest_len;
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001075 /* Structural validation */
Gilles Peskine449bd832023-01-11 14:50:10 +01001076 if ((size_t) (end - buf) != expected_verify_data_len) {
1077 MBEDTLS_SSL_DEBUG_MSG(1, ("bad finished message"));
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001078
Gilles Peskine449bd832023-01-11 14:50:10 +01001079 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR,
1080 MBEDTLS_ERR_SSL_DECODE_ERROR);
1081 return MBEDTLS_ERR_SSL_DECODE_ERROR;
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001082 }
1083
Gilles Peskine449bd832023-01-11 14:50:10 +01001084 MBEDTLS_SSL_DEBUG_BUF(4, "verify_data (self-computed):",
1085 expected_verify_data,
1086 expected_verify_data_len);
1087 MBEDTLS_SSL_DEBUG_BUF(4, "verify_data (received message):", buf,
1088 expected_verify_data_len);
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001089
1090 /* Semantic validation */
Gilles Peskine449bd832023-01-11 14:50:10 +01001091 if (mbedtls_ct_memcmp(buf,
1092 expected_verify_data,
1093 expected_verify_data_len) != 0) {
1094 MBEDTLS_SSL_DEBUG_MSG(1, ("bad finished message"));
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001095
Gilles Peskine449bd832023-01-11 14:50:10 +01001096 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_DECRYPT_ERROR,
1097 MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE);
1098 return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001099 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001100 return 0;
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001101}
1102
Gilles Peskine449bd832023-01-11 14:50:10 +01001103int mbedtls_ssl_tls13_process_finished_message(mbedtls_ssl_context *ssl)
XiaokangQianc5c39d52021-11-09 11:55:10 +00001104{
XiaokangQian33062842021-11-11 03:37:45 +00001105 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
XiaokangQianc5c39d52021-11-09 11:55:10 +00001106 unsigned char *buf;
Xiaofei Baieef15042021-11-18 07:29:56 +00001107 size_t buf_len;
XiaokangQianc5c39d52021-11-09 11:55:10 +00001108
Gilles Peskine449bd832023-01-11 14:50:10 +01001109 MBEDTLS_SSL_DEBUG_MSG(2, ("=> parse finished message"));
XiaokangQianc5c39d52021-11-09 11:55:10 +00001110
Xiaokang Qian73437382023-03-29 08:24:12 +00001111 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_tls13_fetch_handshake_msg(
1112 ssl, MBEDTLS_SSL_HS_FINISHED, &buf, &buf_len));
Jerry Yu0a92d6c2022-05-16 16:54:46 +08001113
1114 /* Preprocessing step: Compute handshake digest */
Gilles Peskine449bd832023-01-11 14:50:10 +01001115 MBEDTLS_SSL_PROC_CHK(ssl_tls13_preprocess_finished_message(ssl));
Jerry Yu0a92d6c2022-05-16 16:54:46 +08001116
Xiaokang Qian73437382023-03-29 08:24:12 +00001117 MBEDTLS_SSL_PROC_CHK(ssl_tls13_parse_finished_message(
1118 ssl, buf, buf + buf_len));
Jerry Yu0a92d6c2022-05-16 16:54:46 +08001119
Xiaokang Qian73437382023-03-29 08:24:12 +00001120 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_add_hs_msg_to_checksum(
1121 ssl, MBEDTLS_SSL_HS_FINISHED, buf, buf_len));
XiaokangQianc5c39d52021-11-09 11:55:10 +00001122
1123cleanup:
1124
Gilles Peskine449bd832023-01-11 14:50:10 +01001125 MBEDTLS_SSL_DEBUG_MSG(2, ("<= parse finished message"));
1126 return ret;
XiaokangQianc5c39d52021-11-09 11:55:10 +00001127}
1128
XiaokangQian74af2a82021-09-22 07:40:30 +00001129/*
1130 *
XiaokangQiancc90c942021-11-09 12:30:09 +00001131 * STATE HANDLING: Write and send Finished message.
XiaokangQian74af2a82021-09-22 07:40:30 +00001132 *
1133 */
XiaokangQian74af2a82021-09-22 07:40:30 +00001134/*
XiaokangQian35dc6252021-11-11 08:16:19 +00001135 * Implement
XiaokangQian74af2a82021-09-22 07:40:30 +00001136 */
1137
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001138MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01001139static int ssl_tls13_prepare_finished_message(mbedtls_ssl_context *ssl)
XiaokangQian74af2a82021-09-22 07:40:30 +00001140{
1141 int ret;
1142
1143 /* Compute transcript of handshake up to now. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001144 ret = mbedtls_ssl_tls13_calculate_verify_data(ssl,
1145 ssl->handshake->state_local.finished_out.digest,
1146 sizeof(ssl->handshake->state_local.finished_out.
1147 digest),
1148 &ssl->handshake->state_local.finished_out.digest_len,
1149 ssl->conf->endpoint);
XiaokangQian74af2a82021-09-22 07:40:30 +00001150
Gilles Peskine449bd832023-01-11 14:50:10 +01001151 if (ret != 0) {
1152 MBEDTLS_SSL_DEBUG_RET(1, "calculate_verify_data failed", ret);
1153 return ret;
XiaokangQian74af2a82021-09-22 07:40:30 +00001154 }
1155
Gilles Peskine449bd832023-01-11 14:50:10 +01001156 return 0;
XiaokangQian74af2a82021-09-22 07:40:30 +00001157}
1158
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001159MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01001160static int ssl_tls13_write_finished_message_body(mbedtls_ssl_context *ssl,
1161 unsigned char *buf,
1162 unsigned char *end,
1163 size_t *out_len)
XiaokangQian74af2a82021-09-22 07:40:30 +00001164{
XiaokangQian8773aa02021-11-10 07:33:09 +00001165 size_t verify_data_len = ssl->handshake->state_local.finished_out.digest_len;
XiaokangQian0fa66432021-11-15 03:33:57 +00001166 /*
1167 * struct {
1168 * opaque verify_data[Hash.length];
1169 * } Finished;
1170 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001171 MBEDTLS_SSL_CHK_BUF_PTR(buf, end, verify_data_len);
XiaokangQian74af2a82021-09-22 07:40:30 +00001172
Gilles Peskine449bd832023-01-11 14:50:10 +01001173 memcpy(buf, ssl->handshake->state_local.finished_out.digest,
1174 verify_data_len);
XiaokangQian74af2a82021-09-22 07:40:30 +00001175
Xiaofei Baid25fab62021-12-02 06:36:27 +00001176 *out_len = verify_data_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01001177 return 0;
XiaokangQian74af2a82021-09-22 07:40:30 +00001178}
XiaokangQianc5c39d52021-11-09 11:55:10 +00001179
XiaokangQian35dc6252021-11-11 08:16:19 +00001180/* Main entry point: orchestrates the other functions */
Gilles Peskine449bd832023-01-11 14:50:10 +01001181int mbedtls_ssl_tls13_write_finished_message(mbedtls_ssl_context *ssl)
XiaokangQian35dc6252021-11-11 08:16:19 +00001182{
1183 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1184 unsigned char *buf;
1185 size_t buf_len, msg_len;
1186
Gilles Peskine449bd832023-01-11 14:50:10 +01001187 MBEDTLS_SSL_DEBUG_MSG(2, ("=> write finished message"));
XiaokangQian35dc6252021-11-11 08:16:19 +00001188
Gilles Peskine449bd832023-01-11 14:50:10 +01001189 MBEDTLS_SSL_PROC_CHK(ssl_tls13_prepare_finished_message(ssl));
XiaokangQiandce82242021-11-15 06:01:26 +00001190
Gilles Peskine449bd832023-01-11 14:50:10 +01001191 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_start_handshake_msg(ssl,
1192 MBEDTLS_SSL_HS_FINISHED, &buf, &buf_len));
XiaokangQian35dc6252021-11-11 08:16:19 +00001193
Gilles Peskine449bd832023-01-11 14:50:10 +01001194 MBEDTLS_SSL_PROC_CHK(ssl_tls13_write_finished_message_body(
1195 ssl, buf, buf + buf_len, &msg_len));
XiaokangQian35dc6252021-11-11 08:16:19 +00001196
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +01001197 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_add_hs_msg_to_checksum(ssl,
Manuel Pégourié-Gonnard43cc1272023-02-06 11:48:19 +01001198 MBEDTLS_SSL_HS_FINISHED, buf, msg_len));
XiaokangQian35dc6252021-11-11 08:16:19 +00001199
Gilles Peskine449bd832023-01-11 14:50:10 +01001200 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_finish_handshake_msg(
1201 ssl, buf_len, msg_len));
XiaokangQian35dc6252021-11-11 08:16:19 +00001202cleanup:
1203
Gilles Peskine449bd832023-01-11 14:50:10 +01001204 MBEDTLS_SSL_DEBUG_MSG(2, ("<= write finished message"));
1205 return ret;
XiaokangQian35dc6252021-11-11 08:16:19 +00001206}
1207
Gilles Peskine449bd832023-01-11 14:50:10 +01001208void mbedtls_ssl_tls13_handshake_wrapup(mbedtls_ssl_context *ssl)
Jerry Yu378254d2021-10-30 21:44:47 +08001209{
1210
Gilles Peskine449bd832023-01-11 14:50:10 +01001211 MBEDTLS_SSL_DEBUG_MSG(3, ("=> handshake wrapup"));
Jerry Yu378254d2021-10-30 21:44:47 +08001212
Gilles Peskine449bd832023-01-11 14:50:10 +01001213 MBEDTLS_SSL_DEBUG_MSG(1, ("Switch to application keys for inbound traffic"));
1214 mbedtls_ssl_set_inbound_transform(ssl, ssl->transform_application);
Jerry Yue8c1fca2022-05-18 14:48:56 +08001215
Gilles Peskine449bd832023-01-11 14:50:10 +01001216 MBEDTLS_SSL_DEBUG_MSG(1, ("Switch to application keys for outbound traffic"));
1217 mbedtls_ssl_set_outbound_transform(ssl, ssl->transform_application);
Jerry Yue8c1fca2022-05-18 14:48:56 +08001218
Jerry Yu378254d2021-10-30 21:44:47 +08001219 /*
Jerry Yucfe64f02021-11-15 13:54:06 +08001220 * Free the previous session and switch to the current one.
Jerry Yu378254d2021-10-30 21:44:47 +08001221 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001222 if (ssl->session) {
1223 mbedtls_ssl_session_free(ssl->session);
1224 mbedtls_free(ssl->session);
Jerry Yu378254d2021-10-30 21:44:47 +08001225 }
1226 ssl->session = ssl->session_negotiate;
1227 ssl->session_negotiate = NULL;
1228
Gilles Peskine449bd832023-01-11 14:50:10 +01001229 MBEDTLS_SSL_DEBUG_MSG(3, ("<= handshake wrapup"));
Jerry Yu378254d2021-10-30 21:44:47 +08001230}
1231
Ronald Cron49ad6192021-11-24 16:25:31 +01001232/*
1233 *
1234 * STATE HANDLING: Write ChangeCipherSpec
1235 *
1236 */
1237#if defined(MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001238MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01001239static int ssl_tls13_write_change_cipher_spec_body(mbedtls_ssl_context *ssl,
1240 unsigned char *buf,
1241 unsigned char *end,
1242 size_t *olen)
Ronald Cron49ad6192021-11-24 16:25:31 +01001243{
1244 ((void) ssl);
1245
Gilles Peskine449bd832023-01-11 14:50:10 +01001246 MBEDTLS_SSL_CHK_BUF_PTR(buf, end, 1);
Ronald Cron49ad6192021-11-24 16:25:31 +01001247 buf[0] = 1;
1248 *olen = 1;
1249
Gilles Peskine449bd832023-01-11 14:50:10 +01001250 return 0;
Ronald Cron49ad6192021-11-24 16:25:31 +01001251}
1252
Gilles Peskine449bd832023-01-11 14:50:10 +01001253int mbedtls_ssl_tls13_write_change_cipher_spec(mbedtls_ssl_context *ssl)
Ronald Cron49ad6192021-11-24 16:25:31 +01001254{
1255 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1256
Gilles Peskine449bd832023-01-11 14:50:10 +01001257 MBEDTLS_SSL_DEBUG_MSG(2, ("=> write change cipher spec"));
Ronald Cron49ad6192021-11-24 16:25:31 +01001258
Ronald Crone273f722024-02-13 18:22:26 +01001259 /* Only one CCS to send. */
Ronald Cron5fbd2702024-02-14 10:03:36 +01001260 if (ssl->handshake->ccs_sent) {
Ronald Crone273f722024-02-13 18:22:26 +01001261 ret = 0;
1262 goto cleanup;
1263 }
1264
Ronald Cron49ad6192021-11-24 16:25:31 +01001265 /* Write CCS message */
Gilles Peskine449bd832023-01-11 14:50:10 +01001266 MBEDTLS_SSL_PROC_CHK(ssl_tls13_write_change_cipher_spec_body(
1267 ssl, ssl->out_msg,
1268 ssl->out_msg + MBEDTLS_SSL_OUT_CONTENT_LEN,
1269 &ssl->out_msglen));
Ronald Cron49ad6192021-11-24 16:25:31 +01001270
1271 ssl->out_msgtype = MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC;
1272
Ronald Cron49ad6192021-11-24 16:25:31 +01001273 /* Dispatch message */
Gilles Peskine449bd832023-01-11 14:50:10 +01001274 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_write_record(ssl, 0));
Ronald Cron49ad6192021-11-24 16:25:31 +01001275
Ronald Cron5fbd2702024-02-14 10:03:36 +01001276 ssl->handshake->ccs_sent = 1;
Ronald Cronfe59ff72024-01-24 14:31:50 +01001277
Ronald Cron49ad6192021-11-24 16:25:31 +01001278cleanup:
1279
Gilles Peskine449bd832023-01-11 14:50:10 +01001280 MBEDTLS_SSL_DEBUG_MSG(2, ("<= write change cipher spec"));
1281 return ret;
Ronald Cron49ad6192021-11-24 16:25:31 +01001282}
1283
1284#endif /* MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE */
1285
Xiaokang Qianecc29482022-11-02 07:52:47 +00001286/* Early Data Indication Extension
Xiaokang Qian0e97d4d2022-10-24 11:12:51 +00001287 *
Xiaokang Qian0e97d4d2022-10-24 11:12:51 +00001288 * struct {
1289 * select ( Handshake.msg_type ) {
Jerry Yu52335392023-11-23 18:06:06 +08001290 * case new_session_ticket: uint32 max_early_data_size;
Xiaokang Qian0e97d4d2022-10-24 11:12:51 +00001291 * case client_hello: Empty;
1292 * case encrypted_extensions: Empty;
1293 * };
1294 * } EarlyDataIndication;
1295 */
1296#if defined(MBEDTLS_SSL_EARLY_DATA)
Gilles Peskine449bd832023-01-11 14:50:10 +01001297int mbedtls_ssl_tls13_write_early_data_ext(mbedtls_ssl_context *ssl,
Jerry Yuc59c5862023-12-05 10:40:49 +08001298 int in_new_session_ticket,
Gilles Peskine449bd832023-01-11 14:50:10 +01001299 unsigned char *buf,
1300 const unsigned char *end,
Jerry Yuc59c5862023-12-05 10:40:49 +08001301 size_t *out_len)
Xiaokang Qian0e97d4d2022-10-24 11:12:51 +00001302{
1303 unsigned char *p = buf;
Xiaokang Qian0e97d4d2022-10-24 11:12:51 +00001304
Jerry Yu52335392023-11-23 18:06:06 +08001305#if defined(MBEDTLS_SSL_SRV_C)
Jerry Yuc59c5862023-12-05 10:40:49 +08001306 const size_t needed = in_new_session_ticket ? 8 : 4;
Jerry Yu52335392023-11-23 18:06:06 +08001307#else
1308 const size_t needed = 4;
Jerry Yuc59c5862023-12-05 10:40:49 +08001309 ((void) in_new_session_ticket);
Jerry Yu52335392023-11-23 18:06:06 +08001310#endif
1311
1312 *out_len = 0;
1313
1314 MBEDTLS_SSL_CHK_BUF_PTR(p, end, needed);
Xiaokang Qian0e97d4d2022-10-24 11:12:51 +00001315
Gilles Peskine449bd832023-01-11 14:50:10 +01001316 MBEDTLS_PUT_UINT16_BE(MBEDTLS_TLS_EXT_EARLY_DATA, p, 0);
Jerry Yu52335392023-11-23 18:06:06 +08001317 MBEDTLS_PUT_UINT16_BE(needed - 4, p, 2);
Xiaokang Qian0e97d4d2022-10-24 11:12:51 +00001318
Jerry Yu52335392023-11-23 18:06:06 +08001319#if defined(MBEDTLS_SSL_SRV_C)
Jerry Yuc59c5862023-12-05 10:40:49 +08001320 if (in_new_session_ticket) {
1321 MBEDTLS_PUT_UINT32_BE(ssl->conf->max_early_data_size, p, 4);
Jerry Yu52335392023-11-23 18:06:06 +08001322 MBEDTLS_SSL_DEBUG_MSG(
1323 4, ("Sent max_early_data_size=%u",
Jerry Yuc59c5862023-12-05 10:40:49 +08001324 (unsigned int) ssl->conf->max_early_data_size));
Jerry Yu52335392023-11-23 18:06:06 +08001325 }
1326#endif
1327
1328 *out_len = needed;
Xiaokang Qian2cd5ce02022-11-15 10:33:53 +00001329
Gilles Peskine449bd832023-01-11 14:50:10 +01001330 mbedtls_ssl_tls13_set_hs_sent_ext_mask(ssl, MBEDTLS_TLS_EXT_EARLY_DATA);
Xiaokang Qian2cd5ce02022-11-15 10:33:53 +00001331
Gilles Peskine449bd832023-01-11 14:50:10 +01001332 return 0;
Xiaokang Qian0e97d4d2022-10-24 11:12:51 +00001333}
Ronald Cron85718042024-02-22 10:22:09 +01001334
1335#if defined(MBEDTLS_SSL_SRV_C)
1336int mbedtls_ssl_tls13_check_early_data_len(mbedtls_ssl_context *ssl,
1337 size_t early_data_len)
1338{
Ronald Cron85718042024-02-22 10:22:09 +01001339 /*
1340 * This function should be called only while an handshake is in progress
1341 * and thus a session under negotiation. Add a sanity check to detect a
1342 * misuse.
1343 */
1344 if (ssl->session_negotiate == NULL) {
1345 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
1346 }
1347
1348 /* RFC 8446 section 4.6.1
1349 *
1350 * A server receiving more than max_early_data_size bytes of 0-RTT data
1351 * SHOULD terminate the connection with an "unexpected_message" alert.
Ronald Cron93795f22024-03-07 09:24:56 +01001352 * Note that if it is still possible to send early_data_len bytes of early
1353 * data, it means that early_data_len is smaller than max_early_data_size
1354 * (type uint32_t) and can fit in an uint32_t. We use this further
1355 * down.
Ronald Cron85718042024-02-22 10:22:09 +01001356 */
Ronald Cron2e7dfd52024-03-05 10:54:33 +01001357 if (early_data_len >
Ronald Cron85718042024-02-22 10:22:09 +01001358 (ssl->session_negotiate->max_early_data_size -
Ronald Cron19bfe0a2024-02-26 16:43:01 +01001359 ssl->total_early_data_size)) {
Ronald Cron85718042024-02-22 10:22:09 +01001360
1361 MBEDTLS_SSL_DEBUG_MSG(
Gilles Peskinea9d4ef02024-06-03 22:16:23 +02001362 2, ("EarlyData: Too much early data received, "
Gilles Peskine69770aa2024-06-04 08:45:58 +02001363 "%lu + %" MBEDTLS_PRINTF_SIZET " > %lu",
1364 (unsigned long) ssl->total_early_data_size,
Gilles Peskinea9d4ef02024-06-03 22:16:23 +02001365 early_data_len,
Gilles Peskine69770aa2024-06-04 08:45:58 +02001366 (unsigned long) ssl->session_negotiate->max_early_data_size));
Ronald Cron85718042024-02-22 10:22:09 +01001367
1368 MBEDTLS_SSL_PEND_FATAL_ALERT(
1369 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE,
1370 MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE);
1371 return MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE;
1372 }
1373
Ronald Cron2e7dfd52024-03-05 10:54:33 +01001374 /*
Ronald Cron93795f22024-03-07 09:24:56 +01001375 * early_data_len has been checked to be less than max_early_data_size
1376 * that is uint32_t. Its cast to an uint32_t below is thus safe. We need
1377 * the cast to appease some compilers.
Ronald Cron2e7dfd52024-03-05 10:54:33 +01001378 */
Ronald Cron2e7dfd52024-03-05 10:54:33 +01001379 ssl->total_early_data_size += (uint32_t) early_data_len;
Ronald Cron85718042024-02-22 10:22:09 +01001380
1381 return 0;
1382}
1383#endif /* MBEDTLS_SSL_SRV_C */
Xiaokang Qian0e97d4d2022-10-24 11:12:51 +00001384#endif /* MBEDTLS_SSL_EARLY_DATA */
1385
XiaokangQian78b1fa72022-01-19 06:56:30 +00001386/* Reset SSL context and update hash for handling HRR.
1387 *
1388 * Replace Transcript-Hash(X) by
1389 * Transcript-Hash( message_hash ||
1390 * 00 00 Hash.length ||
1391 * X )
1392 * A few states of the handshake are preserved, including:
1393 * - session ID
1394 * - session ticket
1395 * - negotiated ciphersuite
1396 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001397int mbedtls_ssl_reset_transcript_for_hrr(mbedtls_ssl_context *ssl)
XiaokangQian78b1fa72022-01-19 06:56:30 +00001398{
1399 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Przemyslaw Stekielda645252022-09-14 12:50:51 +02001400 unsigned char hash_transcript[PSA_HASH_MAX_SIZE + 4];
XiaokangQian0ece9982022-01-24 08:56:23 +00001401 size_t hash_len;
Xiaokang Qian6b980012023-02-07 03:17:45 +00001402 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
1403 ssl->handshake->ciphersuite_info;
XiaokangQian78b1fa72022-01-19 06:56:30 +00001404
Gilles Peskine449bd832023-01-11 14:50:10 +01001405 MBEDTLS_SSL_DEBUG_MSG(3, ("Reset SSL session for HRR"));
XiaokangQian78b1fa72022-01-19 06:56:30 +00001406
Dave Rodgman2eab4622023-10-05 13:30:37 +01001407 ret = mbedtls_ssl_get_handshake_transcript(ssl, (mbedtls_md_type_t) ciphersuite_info->mac,
Gilles Peskine449bd832023-01-11 14:50:10 +01001408 hash_transcript + 4,
1409 PSA_HASH_MAX_SIZE,
1410 &hash_len);
1411 if (ret != 0) {
Manuel Pégourié-Gonnardda7979b2023-02-21 09:31:10 +01001412 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_get_handshake_transcript", ret);
Gilles Peskine449bd832023-01-11 14:50:10 +01001413 return ret;
XiaokangQian0ece9982022-01-24 08:56:23 +00001414 }
1415
1416 hash_transcript[0] = MBEDTLS_SSL_HS_MESSAGE_HASH;
1417 hash_transcript[1] = 0;
1418 hash_transcript[2] = 0;
1419 hash_transcript[3] = (unsigned char) hash_len;
1420
1421 hash_len += 4;
1422
Manuel Pégourié-Gonnardda7979b2023-02-21 09:31:10 +01001423 MBEDTLS_SSL_DEBUG_BUF(4, "Truncated handshake transcript",
1424 hash_transcript, hash_len);
1425
Manuel Pégourié-Gonnardd7a7a232023-02-05 10:26:49 +01001426 /* Reset running hash and replace it with a hash of the transcript */
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +01001427 ret = mbedtls_ssl_reset_checksum(ssl);
1428 if (ret != 0) {
1429 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_reset_checksum", ret);
1430 return ret;
1431 }
1432 ret = ssl->handshake->update_checksum(ssl, hash_transcript, hash_len);
1433 if (ret != 0) {
1434 MBEDTLS_SSL_DEBUG_RET(1, "update_checksum", ret);
1435 return ret;
1436 }
Przemyslaw Stekiel4b3fff42022-02-14 16:39:52 +01001437
Gilles Peskine449bd832023-01-11 14:50:10 +01001438 return ret;
XiaokangQian78b1fa72022-01-19 06:56:30 +00001439}
1440
Valerio Settic9ae8622023-07-25 11:23:50 +02001441#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_EPHEMERAL_ENABLED)
XiaokangQian7807f9f2022-02-15 10:04:37 +00001442
Przemek Stekiel7ac93be2023-07-04 10:02:38 +02001443int mbedtls_ssl_tls13_read_public_xxdhe_share(mbedtls_ssl_context *ssl,
Gilles Peskine449bd832023-01-11 14:50:10 +01001444 const unsigned char *buf,
1445 size_t buf_len)
XiaokangQian7807f9f2022-02-15 10:04:37 +00001446{
Gilles Peskine449bd832023-01-11 14:50:10 +01001447 uint8_t *p = (uint8_t *) buf;
XiaokangQiancfd925f2022-04-14 07:10:37 +00001448 const uint8_t *end = buf + buf_len;
XiaokangQian9b5d04b2022-04-10 10:20:43 +00001449 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001450
XiaokangQian9b5d04b2022-04-10 10:20:43 +00001451 /* Get size of the TLS opaque key_exchange field of the KeyShareEntry struct. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001452 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, 2);
1453 uint16_t peerkey_len = MBEDTLS_GET_UINT16_BE(p, 0);
XiaokangQian9b5d04b2022-04-10 10:20:43 +00001454 p += 2;
XiaokangQian3207a322022-02-23 03:15:27 +00001455
XiaokangQian9b5d04b2022-04-10 10:20:43 +00001456 /* Check if key size is consistent with given buffer length. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001457 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, peerkey_len);
XiaokangQian9b5d04b2022-04-10 10:20:43 +00001458
Gilles Peskine12c5aaa2023-10-02 14:55:45 +02001459 /* Store peer's ECDH/FFDH public key. */
1460 if (peerkey_len > sizeof(handshake->xxdh_psa_peerkey)) {
Gilles Peskine530c4232023-10-02 15:37:23 +02001461 MBEDTLS_SSL_DEBUG_MSG(1, ("Invalid public key length: %u > %" MBEDTLS_PRINTF_SIZET,
1462 (unsigned) peerkey_len,
1463 sizeof(handshake->xxdh_psa_peerkey)));
Gilles Peskine12c5aaa2023-10-02 14:55:45 +02001464 return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
1465 }
Przemek Stekiel7ac93be2023-07-04 10:02:38 +02001466 memcpy(handshake->xxdh_psa_peerkey, p, peerkey_len);
1467 handshake->xxdh_psa_peerkey_len = peerkey_len;
XiaokangQian9b5d04b2022-04-10 10:20:43 +00001468
Gilles Peskine449bd832023-01-11 14:50:10 +01001469 return 0;
XiaokangQian3207a322022-02-23 03:15:27 +00001470}
Jerry Yu89e103c2022-03-30 22:43:29 +08001471
Valerio Setti711f8532023-07-31 11:28:07 +02001472#if defined(PSA_WANT_ALG_FFDH)
Przemek Stekielda4fba62023-06-02 14:52:28 +02001473static psa_status_t mbedtls_ssl_get_psa_ffdh_info_from_tls_id(
Przemek Stekiel75a5a9c2023-06-12 11:21:18 +02001474 uint16_t tls_id, size_t *bits, psa_key_type_t *key_type)
Przemek Stekiel29c219c2023-05-31 15:21:04 +02001475{
Przemek Stekiel75a5a9c2023-06-12 11:21:18 +02001476 switch (tls_id) {
Valerio Settiecaf7c52024-01-17 12:30:30 +01001477#if defined(PSA_WANT_DH_RFC7919_2048)
Przemek Stekiel29c219c2023-05-31 15:21:04 +02001478 case MBEDTLS_SSL_IANA_TLS_GROUP_FFDHE2048:
1479 *bits = 2048;
1480 *key_type = PSA_KEY_TYPE_DH_KEY_PAIR(PSA_DH_FAMILY_RFC7919);
1481 return PSA_SUCCESS;
Valerio Settiecaf7c52024-01-17 12:30:30 +01001482#endif /* PSA_WANT_DH_RFC7919_2048 */
1483#if defined(PSA_WANT_DH_RFC7919_3072)
Przemek Stekiel29c219c2023-05-31 15:21:04 +02001484 case MBEDTLS_SSL_IANA_TLS_GROUP_FFDHE3072:
1485 *bits = 3072;
1486 *key_type = PSA_KEY_TYPE_DH_KEY_PAIR(PSA_DH_FAMILY_RFC7919);
1487 return PSA_SUCCESS;
Valerio Settiecaf7c52024-01-17 12:30:30 +01001488#endif /* PSA_WANT_DH_RFC7919_3072 */
1489#if defined(PSA_WANT_DH_RFC7919_4096)
Przemek Stekiel29c219c2023-05-31 15:21:04 +02001490 case MBEDTLS_SSL_IANA_TLS_GROUP_FFDHE4096:
1491 *bits = 4096;
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_4096 */
1495#if defined(PSA_WANT_DH_RFC7919_6144)
Przemek Stekiel29c219c2023-05-31 15:21:04 +02001496 case MBEDTLS_SSL_IANA_TLS_GROUP_FFDHE6144:
1497 *bits = 6144;
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_6144 */
1501#if defined(PSA_WANT_DH_RFC7919_8192)
Przemek Stekiel29c219c2023-05-31 15:21:04 +02001502 case MBEDTLS_SSL_IANA_TLS_GROUP_FFDHE8192:
1503 *bits = 8192;
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_8192 */
Przemek Stekiel29c219c2023-05-31 15:21:04 +02001507 default:
1508 return PSA_ERROR_NOT_SUPPORTED;
1509 }
1510}
Valerio Setti711f8532023-07-31 11:28:07 +02001511#endif /* PSA_WANT_ALG_FFDH */
Przemek Stekiel29c219c2023-05-31 15:21:04 +02001512
Przemek Stekiel408569f2023-07-06 11:26:44 +02001513int mbedtls_ssl_tls13_generate_and_write_xxdh_key_exchange(
Gilles Peskine449bd832023-01-11 14:50:10 +01001514 mbedtls_ssl_context *ssl,
1515 uint16_t named_group,
1516 unsigned char *buf,
1517 unsigned char *end,
1518 size_t *out_len)
Jerry Yu89e103c2022-03-30 22:43:29 +08001519{
1520 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
1521 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
1522 psa_key_attributes_t key_attributes;
1523 size_t own_pubkey_len;
1524 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
Przemek Stekiel29c219c2023-05-31 15:21:04 +02001525 size_t bits = 0;
Przemek Stekiel75a5a9c2023-06-12 11:21:18 +02001526 psa_key_type_t key_type = PSA_KEY_TYPE_NONE;
1527 psa_algorithm_t alg = PSA_ALG_NONE;
Przemek Stekiel29c219c2023-05-31 15:21:04 +02001528 size_t buf_size = (size_t) (end - buf);
Jerry Yu89e103c2022-03-30 22:43:29 +08001529
Przemek Stekiel29c219c2023-05-31 15:21:04 +02001530 MBEDTLS_SSL_DEBUG_MSG(1, ("Perform PSA-based ECDH/FFDH computation."));
Jerry Yu89e103c2022-03-30 22:43:29 +08001531
Valerio Setti40d9ca92023-01-04 16:08:04 +01001532 /* Convert EC's TLS ID to PSA key type. */
Przemek Stekiel29c219c2023-05-31 15:21:04 +02001533#if defined(PSA_WANT_ALG_ECDH)
Xiaokang Qian73437382023-03-29 08:24:12 +00001534 if (mbedtls_ssl_get_psa_curve_info_from_tls_id(
Przemek Stekielda4fba62023-06-02 14:52:28 +02001535 named_group, &key_type, &bits) == PSA_SUCCESS) {
1536 alg = PSA_ALG_ECDH;
Przemek Stekiel29c219c2023-05-31 15:21:04 +02001537 }
1538#endif
1539#if defined(PSA_WANT_ALG_FFDH)
Przemek Stekiel75a5a9c2023-06-12 11:21:18 +02001540 if (mbedtls_ssl_get_psa_ffdh_info_from_tls_id(named_group, &bits,
1541 &key_type) == PSA_SUCCESS) {
Przemek Stekielda4fba62023-06-02 14:52:28 +02001542 alg = PSA_ALG_FFDH;
Przemek Stekiel29c219c2023-05-31 15:21:04 +02001543 }
1544#endif
1545
Przemek Stekiel75a5a9c2023-06-12 11:21:18 +02001546 if (key_type == PSA_KEY_TYPE_NONE) {
Gilles Peskine449bd832023-01-11 14:50:10 +01001547 return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
Valerio Setti40d9ca92023-01-04 16:08:04 +01001548 }
Przemek Stekiel29c219c2023-05-31 15:21:04 +02001549
Przemek Stekielda4fba62023-06-02 14:52:28 +02001550 if (buf_size < PSA_BITS_TO_BYTES(bits)) {
Przemek Stekielda4fba62023-06-02 14:52:28 +02001551 return MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL;
1552 }
1553
Przemek Stekiel7ac93be2023-07-04 10:02:38 +02001554 handshake->xxdh_psa_type = key_type;
Valerio Settiea59c432023-07-25 11:14:03 +02001555 ssl->handshake->xxdh_psa_bits = bits;
Jerry Yu89e103c2022-03-30 22:43:29 +08001556
1557 key_attributes = psa_key_attributes_init();
Gilles Peskine449bd832023-01-11 14:50:10 +01001558 psa_set_key_usage_flags(&key_attributes, PSA_KEY_USAGE_DERIVE);
Przemek Stekielda4fba62023-06-02 14:52:28 +02001559 psa_set_key_algorithm(&key_attributes, alg);
Przemek Stekiel7ac93be2023-07-04 10:02:38 +02001560 psa_set_key_type(&key_attributes, handshake->xxdh_psa_type);
Valerio Settiea59c432023-07-25 11:14:03 +02001561 psa_set_key_bits(&key_attributes, handshake->xxdh_psa_bits);
Jerry Yu89e103c2022-03-30 22:43:29 +08001562
Przemek Stekiel29c219c2023-05-31 15:21:04 +02001563 /* Generate ECDH/FFDH private key. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001564 status = psa_generate_key(&key_attributes,
Przemek Stekiel7ac93be2023-07-04 10:02:38 +02001565 &handshake->xxdh_psa_privkey);
Gilles Peskine449bd832023-01-11 14:50:10 +01001566 if (status != PSA_SUCCESS) {
Andrzej Kurek8a045ce2022-12-23 11:00:06 -05001567 ret = PSA_TO_MBEDTLS_ERR(status);
Gilles Peskine449bd832023-01-11 14:50:10 +01001568 MBEDTLS_SSL_DEBUG_RET(1, "psa_generate_key", ret);
1569 return ret;
Jerry Yu89e103c2022-03-30 22:43:29 +08001570
1571 }
1572
Przemek Stekiel29c219c2023-05-31 15:21:04 +02001573 /* Export the public part of the ECDH/FFDH private key from PSA. */
Przemek Stekiel7ac93be2023-07-04 10:02:38 +02001574 status = psa_export_public_key(handshake->xxdh_psa_privkey,
Przemek Stekiel29c219c2023-05-31 15:21:04 +02001575 buf, buf_size,
Gilles Peskine449bd832023-01-11 14:50:10 +01001576 &own_pubkey_len);
Przemek Stekiel29c219c2023-05-31 15:21:04 +02001577
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_export_public_key", ret);
1581 return ret;
Jerry Yu89e103c2022-03-30 22:43:29 +08001582 }
1583
1584 *out_len = own_pubkey_len;
1585
Gilles Peskine449bd832023-01-11 14:50:10 +01001586 return 0;
Jerry Yu89e103c2022-03-30 22:43:29 +08001587}
Valerio Settic9ae8622023-07-25 11:23:50 +02001588#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_EPHEMERAL_ENABLED */
XiaokangQian7807f9f2022-02-15 10:04:37 +00001589
Jerry Yu0c354a22022-08-29 15:25:36 +08001590/* RFC 8446 section 4.2
1591 *
1592 * If an implementation receives an extension which it recognizes and which is
1593 * not specified for the message in which it appears, it MUST abort the handshake
1594 * with an "illegal_parameter" alert.
1595 *
1596 */
Jerry Yuc4bf5d62022-10-29 09:08:47 +08001597int mbedtls_ssl_tls13_check_received_extension(
Gilles Peskine449bd832023-01-11 14:50:10 +01001598 mbedtls_ssl_context *ssl,
1599 int hs_msg_type,
1600 unsigned int received_extension_type,
1601 uint32_t hs_msg_allowed_extensions_mask)
Jerry Yu0c354a22022-08-29 15:25:36 +08001602{
Jerry Yudf0ad652022-10-31 13:20:57 +08001603 uint32_t extension_mask = mbedtls_ssl_get_extension_mask(
Gilles Peskine449bd832023-01-11 14:50:10 +01001604 received_extension_type);
Jerry Yu0c354a22022-08-29 15:25:36 +08001605
Jerry Yu79aa7212022-11-08 21:30:21 +08001606 MBEDTLS_SSL_PRINT_EXT(
Gilles Peskine449bd832023-01-11 14:50:10 +01001607 3, hs_msg_type, received_extension_type, "received");
Jerry Yu0c354a22022-08-29 15:25:36 +08001608
Gilles Peskine449bd832023-01-11 14:50:10 +01001609 if ((extension_mask & hs_msg_allowed_extensions_mask) == 0) {
Jerry Yu79aa7212022-11-08 21:30:21 +08001610 MBEDTLS_SSL_PRINT_EXT(
Gilles Peskine449bd832023-01-11 14:50:10 +01001611 3, hs_msg_type, received_extension_type, "is illegal");
Jerry Yu0c354a22022-08-29 15:25:36 +08001612 MBEDTLS_SSL_PEND_FATAL_ALERT(
1613 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER,
Gilles Peskine449bd832023-01-11 14:50:10 +01001614 MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER);
1615 return MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER;
Jerry Yu0c354a22022-08-29 15:25:36 +08001616 }
1617
1618 ssl->handshake->received_extensions |= extension_mask;
Jerry Yuc4bf5d62022-10-29 09:08:47 +08001619 /*
1620 * If it is a message containing extension responses, check that we
1621 * previously sent the extension.
1622 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001623 switch (hs_msg_type) {
Jerry Yu0c354a22022-08-29 15:25:36 +08001624 case MBEDTLS_SSL_HS_SERVER_HELLO:
Jerry Yudf0ad652022-10-31 13:20:57 +08001625 case MBEDTLS_SSL_TLS1_3_HS_HELLO_RETRY_REQUEST:
Jerry Yu0c354a22022-08-29 15:25:36 +08001626 case MBEDTLS_SSL_HS_ENCRYPTED_EXTENSIONS:
1627 case MBEDTLS_SSL_HS_CERTIFICATE:
Jerry Yuc4bf5d62022-10-29 09:08:47 +08001628 /* Check if the received extension is sent by peer message.*/
Gilles Peskine449bd832023-01-11 14:50:10 +01001629 if ((ssl->handshake->sent_extensions & extension_mask) != 0) {
1630 return 0;
1631 }
Jerry Yu0c354a22022-08-29 15:25:36 +08001632 break;
1633 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01001634 return 0;
Jerry Yu0c354a22022-08-29 15:25:36 +08001635 }
1636
Jerry Yu79aa7212022-11-08 21:30:21 +08001637 MBEDTLS_SSL_PRINT_EXT(
Gilles Peskine449bd832023-01-11 14:50:10 +01001638 3, hs_msg_type, received_extension_type, "is unsupported");
Jerry Yu0c354a22022-08-29 15:25:36 +08001639 MBEDTLS_SSL_PEND_FATAL_ALERT(
1640 MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_EXT,
Gilles Peskine449bd832023-01-11 14:50:10 +01001641 MBEDTLS_ERR_SSL_UNSUPPORTED_EXTENSION);
1642 return MBEDTLS_ERR_SSL_UNSUPPORTED_EXTENSION;
Jerry Yu0c354a22022-08-29 15:25:36 +08001643}
1644
Jan Bruckner151f6422023-02-10 12:45:19 +01001645#if defined(MBEDTLS_SSL_RECORD_SIZE_LIMIT)
Yanray Wangfaf70bd2023-12-07 10:03:32 +08001646
1647/* RFC 8449, section 4:
1648 *
Jan Bruckner151f6422023-02-10 12:45:19 +01001649 * The ExtensionData of the "record_size_limit" extension is
1650 * RecordSizeLimit:
1651 * uint16 RecordSizeLimit;
1652 */
1653MBEDTLS_CHECK_RETURN_CRITICAL
1654int mbedtls_ssl_tls13_parse_record_size_limit_ext(mbedtls_ssl_context *ssl,
1655 const unsigned char *buf,
1656 const unsigned char *end)
1657{
Jan Bruckner1a38e542023-03-15 14:15:11 +01001658 const unsigned char *p = buf;
1659 uint16_t record_size_limit;
Jan Brucknera0589e72023-03-15 11:04:45 +01001660 const size_t extension_data_len = end - buf;
Jan Bruckner1a38e542023-03-15 14:15:11 +01001661
Xiaokang Qian73437382023-03-29 08:24:12 +00001662 if (extension_data_len !=
1663 MBEDTLS_SSL_RECORD_SIZE_LIMIT_EXTENSION_DATA_LENGTH) {
Jan Bruckner151f6422023-02-10 12:45:19 +01001664 MBEDTLS_SSL_DEBUG_MSG(2,
Jan Bruckner1a38e542023-03-15 14:15:11 +01001665 ("record_size_limit extension has invalid length: %"
1666 MBEDTLS_PRINTF_SIZET " Bytes",
Jan Bruckner151f6422023-02-10 12:45:19 +01001667 extension_data_len));
1668
1669 MBEDTLS_SSL_PEND_FATAL_ALERT(
1670 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER,
1671 MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER);
1672 return MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER;
1673 }
1674
Jan Bruckner151f6422023-02-10 12:45:19 +01001675 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, 2);
1676 record_size_limit = MBEDTLS_GET_UINT16_BE(p, 0);
1677
1678 MBEDTLS_SSL_DEBUG_MSG(2, ("RecordSizeLimit: %u Bytes", record_size_limit));
1679
Waleed Elmelegye1ac98d2024-01-05 18:10:12 +00001680 /* RFC 8449, section 4:
1681 *
1682 * Endpoints MUST NOT send a "record_size_limit" extension with a value
1683 * smaller than 64. An endpoint MUST treat receipt of a smaller value
1684 * as a fatal error and generate an "illegal_parameter" alert.
1685 */
1686 if (record_size_limit < MBEDTLS_SSL_RECORD_SIZE_LIMIT_MIN) {
1687 MBEDTLS_SSL_DEBUG_MSG(1, ("Invalid record size limit : %u Bytes",
1688 record_size_limit));
1689 MBEDTLS_SSL_PEND_FATAL_ALERT(
1690 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER,
1691 MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER);
1692 return MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER;
Jan Bruckner151f6422023-02-10 12:45:19 +01001693 }
1694
Jan Brucknerf482dcc2023-03-15 09:09:06 +01001695 ssl->session_negotiate->record_size_limit = record_size_limit;
Jan Bruckner151f6422023-02-10 12:45:19 +01001696
Jan Brucknerf482dcc2023-03-15 09:09:06 +01001697 return 0;
Jan Bruckner151f6422023-02-10 12:45:19 +01001698}
Jan Brucknerf482dcc2023-03-15 09:09:06 +01001699
Yanray Wangfaf70bd2023-12-07 10:03:32 +08001700MBEDTLS_CHECK_RETURN_CRITICAL
1701int mbedtls_ssl_tls13_write_record_size_limit_ext(mbedtls_ssl_context *ssl,
Yanray Wangfaf70bd2023-12-07 10:03:32 +08001702 unsigned char *buf,
1703 const unsigned char *end,
1704 size_t *out_len)
1705{
Yanray Wangfaf70bd2023-12-07 10:03:32 +08001706 unsigned char *p = buf;
1707 *out_len = 0;
1708
Waleed Elmelegy148dfb62024-01-04 18:02:35 +00001709 MBEDTLS_STATIC_ASSERT(MBEDTLS_SSL_IN_CONTENT_LEN >= MBEDTLS_SSL_RECORD_SIZE_LIMIT_MIN,
Waleed Elmelegye1ac98d2024-01-05 18:10:12 +00001710 "MBEDTLS_SSL_IN_CONTENT_LEN is less than the "
1711 "minimum record size limit");
Yanray Wangfaf70bd2023-12-07 10:03:32 +08001712
1713 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 6);
1714
1715 MBEDTLS_PUT_UINT16_BE(MBEDTLS_TLS_EXT_RECORD_SIZE_LIMIT, p, 0);
Waleed Elmelegye1ac98d2024-01-05 18:10:12 +00001716 MBEDTLS_PUT_UINT16_BE(MBEDTLS_SSL_RECORD_SIZE_LIMIT_EXTENSION_DATA_LENGTH,
1717 p, 2);
Waleed Elmelegy148dfb62024-01-04 18:02:35 +00001718 MBEDTLS_PUT_UINT16_BE(MBEDTLS_SSL_IN_CONTENT_LEN, p, 4);
Yanray Wangfaf70bd2023-12-07 10:03:32 +08001719
1720 *out_len = 6;
1721
Waleed Elmelegy3ff47242024-01-10 16:15:52 +00001722 MBEDTLS_SSL_DEBUG_MSG(2, ("Sent RecordSizeLimit: %d Bytes",
Waleed Elmelegye1ac98d2024-01-05 18:10:12 +00001723 MBEDTLS_SSL_IN_CONTENT_LEN));
Yanray Wangfaf70bd2023-12-07 10:03:32 +08001724
1725 mbedtls_ssl_tls13_set_hs_sent_ext_mask(ssl, MBEDTLS_TLS_EXT_RECORD_SIZE_LIMIT);
1726
1727 return 0;
1728}
1729
Jan Bruckner151f6422023-02-10 12:45:19 +01001730#endif /* MBEDTLS_SSL_RECORD_SIZE_LIMIT */
1731
Jerry Yufb4b6472022-01-27 15:03:26 +08001732#endif /* MBEDTLS_SSL_TLS_C && MBEDTLS_SSL_PROTO_TLS1_3 */