blob: fb57aa4a7573ec5e05b0741be72939062f6ca1a3 [file] [log] [blame]
Jerry Yu65dd2cc2021-08-18 16:38:40 +08001/*
2 * TLS 1.3 functionality shared between client and server
3 *
4 * Copyright The Mbed TLS Contributors
Dave Rodgman16799db2023-11-02 19:47:20 +00005 * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
Jerry Yu65dd2cc2021-08-18 16:38:40 +08006 */
7
8#include "common.h"
9
Jerry Yufb4b6472022-01-27 15:03:26 +080010#if defined(MBEDTLS_SSL_TLS_C) && defined(MBEDTLS_SSL_PROTO_TLS1_3)
Jerry Yu65dd2cc2021-08-18 16:38:40 +080011
Jerry Yu30b071c2021-09-12 20:16:03 +080012#include <string.h>
13
Jerry Yuc8a392c2021-08-18 16:46:28 +080014#include "mbedtls/error.h"
Valerio Settib4f50762024-01-17 10:24:52 +010015#include "debug_internal.h"
Jerry Yu30b071c2021-09-12 20:16:03 +080016#include "mbedtls/oid.h"
17#include "mbedtls/platform.h"
Gabor Mezei685472b2021-11-24 11:17:36 +010018#include "mbedtls/constant_time.h"
Jerry Yu141bbe72022-12-01 20:30:41 +080019#include "psa/crypto.h"
Valerio Setti384fbde2024-01-02 13:26:40 +010020#include "mbedtls/psa_util.h"
Jerry Yuc8a392c2021-08-18 16:46:28 +080021
Jerry Yu65dd2cc2021-08-18 16:38:40 +080022#include "ssl_misc.h"
Ronald Crone3dac4a2022-06-10 17:21:51 +020023#include "ssl_tls13_invasive.h"
Jerry Yu30b071c2021-09-12 20:16:03 +080024#include "ssl_tls13_keys.h"
Jerry Yu67eced02022-02-25 13:37:36 +080025#include "ssl_debug_helpers.h"
Jerry Yu65dd2cc2021-08-18 16:38:40 +080026
Andrzej Kurek8a045ce2022-12-23 11:00:06 -050027#include "psa/crypto.h"
Manuel Pégourié-Gonnard2be8c632023-06-07 13:07:21 +020028#include "psa_util_internal.h"
Andrzej Kurek8a045ce2022-12-23 11:00:06 -050029
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 Cosgroveb32d7ae2024-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 Cosgroveb32d7ae2024-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
Xiaofei Baid25fab62021-12-02 06:36:27 +0000231 void const *options = NULL;
XiaokangQian82d34cc2021-11-03 08:51:56 +0000232#if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT)
Xiaofei Baid25fab62021-12-02 06:36:27 +0000233 mbedtls_pk_rsassa_pss_options rsassa_pss_options;
XiaokangQian82d34cc2021-11-03 08:51:56 +0000234#endif /* MBEDTLS_X509_RSASSA_PSS_SUPPORT */
235
Jerry Yu30b071c2021-09-12 20:16:03 +0800236 /*
237 * struct {
238 * SignatureScheme algorithm;
239 * opaque signature<0..2^16-1>;
240 * } CertificateVerify;
241 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100242 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, 2);
243 algorithm = MBEDTLS_GET_UINT16_BE(p, 0);
Jerry Yu30b071c2021-09-12 20:16:03 +0800244 p += 2;
245
246 /* RFC 8446 section 4.4.3
247 *
Xiaokang Qian73437382023-03-29 08:24:12 +0000248 * If the CertificateVerify message is sent by a server, the signature
249 * algorithm MUST be one offered in the client's "signature_algorithms"
250 * extension unless no valid certificate chain can be produced without
251 * unsupported algorithms
Jerry Yu30b071c2021-09-12 20:16:03 +0800252 *
253 * RFC 8446 section 4.4.2.2
254 *
255 * If the client cannot construct an acceptable chain using the provided
Xiaokang Qian73437382023-03-29 08:24:12 +0000256 * certificates and decides to abort the handshake, then it MUST abort the
257 * handshake with an appropriate certificate-related alert
258 * (by default, "unsupported_certificate").
Jerry Yu30b071c2021-09-12 20:16:03 +0800259 *
Jerry Yu6f87f252021-10-29 20:12:51 +0800260 * Check if algorithm is an offered signature algorithm.
Jerry Yu30b071c2021-09-12 20:16:03 +0800261 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100262 if (!mbedtls_ssl_sig_alg_is_offered(ssl, algorithm)) {
Jerry Yu982d9e52021-10-14 15:59:37 +0800263 /* algorithm not in offered signature algorithms list */
Gilles Peskine449bd832023-01-11 14:50:10 +0100264 MBEDTLS_SSL_DEBUG_MSG(1, ("Received signature algorithm(%04x) is not "
265 "offered.",
266 (unsigned int) algorithm));
Jerry Yu6f87f252021-10-29 20:12:51 +0800267 goto error;
Jerry Yu30b071c2021-09-12 20:16:03 +0800268 }
269
Gilles Peskine449bd832023-01-11 14:50:10 +0100270 if (mbedtls_ssl_get_pk_type_and_md_alg_from_sig_alg(
271 algorithm, &sig_alg, &md_alg) != 0) {
Jerry Yu8c338862022-03-23 13:34:04 +0800272 goto error;
Jerry Yu30b071c2021-09-12 20:16:03 +0800273 }
274
Manuel Pégourié-Gonnard2d6d9932023-03-28 11:38:08 +0200275 hash_alg = mbedtls_md_psa_alg_from_type(md_alg);
Gilles Peskine449bd832023-01-11 14:50:10 +0100276 if (hash_alg == 0) {
pespaceka1378102022-04-26 15:03:11 +0200277 goto error;
278 }
279
Gilles Peskine449bd832023-01-11 14:50:10 +0100280 MBEDTLS_SSL_DEBUG_MSG(3, ("Certificate Verify: Signature algorithm ( %04x )",
281 (unsigned int) algorithm));
Jerry Yu30b071c2021-09-12 20:16:03 +0800282
283 /*
284 * Check the certificate's key type matches the signature alg
285 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100286 if (!mbedtls_pk_can_do(&ssl->session_negotiate->peer_cert->pk, sig_alg)) {
287 MBEDTLS_SSL_DEBUG_MSG(1, ("signature algorithm doesn't match cert key"));
Jerry Yu6f87f252021-10-29 20:12:51 +0800288 goto error;
Jerry Yu30b071c2021-09-12 20:16:03 +0800289 }
290
Gilles Peskine449bd832023-01-11 14:50:10 +0100291 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, 2);
292 signature_len = MBEDTLS_GET_UINT16_BE(p, 0);
Jerry Yu30b071c2021-09-12 20:16:03 +0800293 p += 2;
Gilles Peskine449bd832023-01-11 14:50:10 +0100294 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, signature_len);
Jerry Yu30b071c2021-09-12 20:16:03 +0800295
Gilles Peskine449bd832023-01-11 14:50:10 +0100296 status = psa_hash_compute(hash_alg,
297 verify_buffer,
298 verify_buffer_len,
299 verify_hash,
300 sizeof(verify_hash),
301 &verify_hash_len);
302 if (status != PSA_SUCCESS) {
303 MBEDTLS_SSL_DEBUG_RET(1, "hash computation PSA error", status);
Jerry Yu6f87f252021-10-29 20:12:51 +0800304 goto error;
Jerry Yu133690c2021-10-25 14:01:13 +0800305 }
306
Gilles Peskine449bd832023-01-11 14:50:10 +0100307 MBEDTLS_SSL_DEBUG_BUF(3, "verify hash", verify_hash, verify_hash_len);
XiaokangQian82d34cc2021-11-03 08:51:56 +0000308#if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT)
Gilles Peskine449bd832023-01-11 14:50:10 +0100309 if (sig_alg == MBEDTLS_PK_RSASSA_PSS) {
Xiaofei Baid25fab62021-12-02 06:36:27 +0000310 rsassa_pss_options.mgf1_hash_id = md_alg;
Przemek Stekiel6a5e0182022-06-27 11:53:13 +0200311
Gilles Peskine449bd832023-01-11 14:50:10 +0100312 rsassa_pss_options.expected_salt_len = PSA_HASH_LENGTH(hash_alg);
313 options = (const void *) &rsassa_pss_options;
XiaokangQian82d34cc2021-11-03 08:51:56 +0000314 }
315#endif /* MBEDTLS_X509_RSASSA_PSS_SUPPORT */
Jerry Yu30b071c2021-09-12 20:16:03 +0800316
Gilles Peskine449bd832023-01-11 14:50:10 +0100317 if ((ret = mbedtls_pk_verify_ext(sig_alg, options,
318 &ssl->session_negotiate->peer_cert->pk,
319 md_alg, verify_hash, verify_hash_len,
320 p, signature_len)) == 0) {
321 return 0;
Jerry Yu30b071c2021-09-12 20:16:03 +0800322 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100323 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_pk_verify_ext", ret);
Jerry Yu30b071c2021-09-12 20:16:03 +0800324
Jerry Yu6f87f252021-10-29 20:12:51 +0800325error:
326 /* RFC 8446 section 4.4.3
327 *
328 * If the verification fails, the receiver MUST terminate the handshake
329 * with a "decrypt_error" alert.
Gilles Peskine449bd832023-01-11 14:50:10 +0100330 */
331 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_DECRYPT_ERROR,
332 MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE);
333 return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
Jerry Yu6f87f252021-10-29 20:12:51 +0800334
Jerry Yu30b071c2021-09-12 20:16:03 +0800335}
Ronald Cron928cbd32022-10-04 16:14:26 +0200336#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED */
Jerry Yu30b071c2021-09-12 20:16:03 +0800337
Gilles Peskine449bd832023-01-11 14:50:10 +0100338int mbedtls_ssl_tls13_process_certificate_verify(mbedtls_ssl_context *ssl)
Jerry Yu30b071c2021-09-12 20:16:03 +0800339{
Jerry Yu30b071c2021-09-12 20:16:03 +0800340
Ronald Cron928cbd32022-10-04 16:14:26 +0200341#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED)
Jerry Yuda8cdf22021-10-25 15:06:49 +0800342 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
343 unsigned char verify_buffer[SSL_VERIFY_STRUCT_MAX_SIZE];
344 size_t verify_buffer_len;
345 unsigned char transcript[MBEDTLS_TLS1_3_MD_MAX_SIZE];
346 size_t transcript_len;
347 unsigned char *buf;
348 size_t buf_len;
349
Gilles Peskine449bd832023-01-11 14:50:10 +0100350 MBEDTLS_SSL_DEBUG_MSG(2, ("=> parse certificate verify"));
Jerry Yu30b071c2021-09-12 20:16:03 +0800351
Jerry Yuda8cdf22021-10-25 15:06:49 +0800352 MBEDTLS_SSL_PROC_CHK(
Xiaokang Qian73437382023-03-29 08:24:12 +0000353 mbedtls_ssl_tls13_fetch_handshake_msg(
354 ssl, MBEDTLS_SSL_HS_CERTIFICATE_VERIFY, &buf, &buf_len));
Jerry Yu30b071c2021-09-12 20:16:03 +0800355
Jerry Yuda8cdf22021-10-25 15:06:49 +0800356 /* Need to calculate the hash of the transcript first
Jerry Yu0b32c502021-10-28 13:41:59 +0800357 * before reading the message since otherwise it gets
358 * included in the transcript
359 */
Xiaokang Qian73437382023-03-29 08:24:12 +0000360 ret = mbedtls_ssl_get_handshake_transcript(
361 ssl,
Dave Rodgman2eab4622023-10-05 13:30:37 +0100362 (mbedtls_md_type_t) ssl->handshake->ciphersuite_info->mac,
Xiaokang Qian73437382023-03-29 08:24:12 +0000363 transcript, sizeof(transcript),
364 &transcript_len);
Gilles Peskine449bd832023-01-11 14:50:10 +0100365 if (ret != 0) {
Jerry Yuda8cdf22021-10-25 15:06:49 +0800366 MBEDTLS_SSL_PEND_FATAL_ALERT(
367 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR,
Gilles Peskine449bd832023-01-11 14:50:10 +0100368 MBEDTLS_ERR_SSL_INTERNAL_ERROR);
369 return ret;
Jerry Yu30b071c2021-09-12 20:16:03 +0800370 }
371
Gilles Peskine449bd832023-01-11 14:50:10 +0100372 MBEDTLS_SSL_DEBUG_BUF(3, "handshake hash", transcript, transcript_len);
Jerry Yuda8cdf22021-10-25 15:06:49 +0800373
374 /* Create verify structure */
Gilles Peskine449bd832023-01-11 14:50:10 +0100375 ssl_tls13_create_verify_structure(transcript,
376 transcript_len,
377 verify_buffer,
378 &verify_buffer_len,
379 (ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT) ?
380 MBEDTLS_SSL_IS_SERVER :
381 MBEDTLS_SSL_IS_CLIENT);
Jerry Yuda8cdf22021-10-25 15:06:49 +0800382
383 /* Process the message contents */
Xiaokang Qian73437382023-03-29 08:24:12 +0000384 MBEDTLS_SSL_PROC_CHK(ssl_tls13_parse_certificate_verify(
385 ssl, buf, buf + buf_len,
386 verify_buffer, verify_buffer_len));
Jerry Yuda8cdf22021-10-25 15:06:49 +0800387
Xiaokang Qian73437382023-03-29 08:24:12 +0000388 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_add_hs_msg_to_checksum(
389 ssl, MBEDTLS_SSL_HS_CERTIFICATE_VERIFY,
390 buf, buf_len));
Jerry Yu30b071c2021-09-12 20:16:03 +0800391
392cleanup:
393
Gilles Peskine449bd832023-01-11 14:50:10 +0100394 MBEDTLS_SSL_DEBUG_MSG(2, ("<= parse certificate verify"));
395 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_tls13_process_certificate_verify", ret);
396 return ret;
Jerry Yuda8cdf22021-10-25 15:06:49 +0800397#else
398 ((void) ssl);
Gilles Peskine449bd832023-01-11 14:50:10 +0100399 MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
400 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Ronald Cron928cbd32022-10-04 16:14:26 +0200401#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED */
Jerry Yu30b071c2021-09-12 20:16:03 +0800402}
403
404/*
Xiaofei Bai947571e2021-09-29 09:12:03 +0000405 *
XiaokangQian6b916b12022-04-25 07:29:34 +0000406 * STATE HANDLING: Incoming Certificate.
Xiaofei Bai947571e2021-09-29 09:12:03 +0000407 *
408 */
409
Ronald Cronde08cf32022-10-04 17:15:35 +0200410#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED)
Xiaofei Bai947571e2021-09-29 09:12:03 +0000411#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
412/*
413 * Structure of Certificate message:
414 *
415 * enum {
416 * X509(0),
417 * RawPublicKey(2),
418 * (255)
419 * } CertificateType;
420 *
421 * struct {
422 * select (certificate_type) {
423 * case RawPublicKey:
424 * * From RFC 7250 ASN.1_subjectPublicKeyInfo *
425 * opaque ASN1_subjectPublicKeyInfo<1..2^24-1>;
426 * case X509:
427 * opaque cert_data<1..2^24-1>;
428 * };
429 * Extension extensions<0..2^16-1>;
430 * } CertificateEntry;
431 *
432 * struct {
433 * opaque certificate_request_context<0..2^8-1>;
434 * CertificateEntry certificate_list<0..2^24-1>;
435 * } Certificate;
436 *
437 */
Xiaofei Bai79595ac2021-10-26 07:16:45 +0000438
439/* Parse certificate chain send by the server. */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200440MBEDTLS_CHECK_RETURN_CRITICAL
Ronald Crone3dac4a2022-06-10 17:21:51 +0200441MBEDTLS_STATIC_TESTABLE
Gilles Peskine449bd832023-01-11 14:50:10 +0100442int mbedtls_ssl_tls13_parse_certificate(mbedtls_ssl_context *ssl,
443 const unsigned char *buf,
444 const unsigned char *end)
Xiaofei Bai947571e2021-09-29 09:12:03 +0000445{
446 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
447 size_t certificate_request_context_len = 0;
448 size_t certificate_list_len = 0;
449 const unsigned char *p = buf;
450 const unsigned char *certificate_list_end;
Jerry Yuc4bf5d62022-10-29 09:08:47 +0800451 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
Xiaofei Bai947571e2021-09-29 09:12:03 +0000452
Gilles Peskine449bd832023-01-11 14:50:10 +0100453 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, 4);
Xiaofei Bai947571e2021-09-29 09:12:03 +0000454 certificate_request_context_len = p[0];
Gilles Peskine449bd832023-01-11 14:50:10 +0100455 certificate_list_len = MBEDTLS_GET_UINT24_BE(p, 1);
XiaokangQian63e713e2022-05-15 04:26:57 +0000456 p += 4;
Xiaofei Bai947571e2021-09-29 09:12:03 +0000457
458 /* In theory, the certificate list can be up to 2^24 Bytes, but we don't
459 * support anything beyond 2^16 = 64K.
460 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100461 if ((certificate_request_context_len != 0) ||
462 (certificate_list_len >= 0x10000)) {
463 MBEDTLS_SSL_DEBUG_MSG(1, ("bad certificate message"));
464 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR,
465 MBEDTLS_ERR_SSL_DECODE_ERROR);
466 return MBEDTLS_ERR_SSL_DECODE_ERROR;
Xiaofei Bai947571e2021-09-29 09:12:03 +0000467 }
468
469 /* In case we tried to reuse a session but it failed */
Gilles Peskine449bd832023-01-11 14:50:10 +0100470 if (ssl->session_negotiate->peer_cert != NULL) {
471 mbedtls_x509_crt_free(ssl->session_negotiate->peer_cert);
472 mbedtls_free(ssl->session_negotiate->peer_cert);
Xiaofei Bai947571e2021-09-29 09:12:03 +0000473 }
474
Manuel Pégourié-Gonnard4d4c0c72024-08-12 10:36:40 +0200475 /* This is used by ssl_tls13_validate_certificate() */
Gilles Peskine449bd832023-01-11 14:50:10 +0100476 if (certificate_list_len == 0) {
XiaokangQianc3017f62022-05-13 05:55:41 +0000477 ssl->session_negotiate->peer_cert = NULL;
478 ret = 0;
479 goto exit;
480 }
481
Gilles Peskine449bd832023-01-11 14:50:10 +0100482 if ((ssl->session_negotiate->peer_cert =
483 mbedtls_calloc(1, sizeof(mbedtls_x509_crt))) == NULL) {
484 MBEDTLS_SSL_DEBUG_MSG(1, ("alloc( %" MBEDTLS_PRINTF_SIZET " bytes ) failed",
485 sizeof(mbedtls_x509_crt)));
486 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR,
487 MBEDTLS_ERR_SSL_ALLOC_FAILED);
488 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
Xiaofei Bai947571e2021-09-29 09:12:03 +0000489 }
490
Gilles Peskine449bd832023-01-11 14:50:10 +0100491 mbedtls_x509_crt_init(ssl->session_negotiate->peer_cert);
Xiaofei Bai947571e2021-09-29 09:12:03 +0000492
Gilles Peskine449bd832023-01-11 14:50:10 +0100493 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, certificate_list_len);
Xiaofei Bai947571e2021-09-29 09:12:03 +0000494 certificate_list_end = p + certificate_list_len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100495 while (p < certificate_list_end) {
Xiaofei Bai947571e2021-09-29 09:12:03 +0000496 size_t cert_data_len, extensions_len;
Jerry Yu2eaa7602022-08-04 17:28:15 +0800497 const unsigned char *extensions_end;
Xiaofei Bai947571e2021-09-29 09:12:03 +0000498
Gilles Peskine449bd832023-01-11 14:50:10 +0100499 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, certificate_list_end, 3);
500 cert_data_len = MBEDTLS_GET_UINT24_BE(p, 0);
Xiaofei Bai947571e2021-09-29 09:12:03 +0000501 p += 3;
502
503 /* In theory, the CRT can be up to 2^24 Bytes, but we don't support
504 * anything beyond 2^16 = 64K. Otherwise as in the TLS 1.2 code,
505 * check that we have a minimum of 128 bytes of data, this is not
506 * clear why we need that though.
507 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100508 if ((cert_data_len < 128) || (cert_data_len >= 0x10000)) {
509 MBEDTLS_SSL_DEBUG_MSG(1, ("bad Certificate message"));
510 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR,
511 MBEDTLS_ERR_SSL_DECODE_ERROR);
512 return MBEDTLS_ERR_SSL_DECODE_ERROR;
Xiaofei Bai947571e2021-09-29 09:12:03 +0000513 }
514
Gilles Peskine449bd832023-01-11 14:50:10 +0100515 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, certificate_list_end, cert_data_len);
516 ret = mbedtls_x509_crt_parse_der(ssl->session_negotiate->peer_cert,
517 p, cert_data_len);
Xiaofei Bai947571e2021-09-29 09:12:03 +0000518
Gilles Peskine449bd832023-01-11 14:50:10 +0100519 switch (ret) {
Xiaofei Bai947571e2021-09-29 09:12:03 +0000520 case 0: /*ok*/
521 break;
522 case MBEDTLS_ERR_X509_UNKNOWN_SIG_ALG + MBEDTLS_ERR_OID_NOT_FOUND:
523 /* Ignore certificate with an unknown algorithm: maybe a
524 prior certificate was already trusted. */
525 break;
526
527 case MBEDTLS_ERR_X509_ALLOC_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100528 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR,
529 MBEDTLS_ERR_X509_ALLOC_FAILED);
530 MBEDTLS_SSL_DEBUG_RET(1, " mbedtls_x509_crt_parse_der", ret);
531 return ret;
Xiaofei Bai947571e2021-09-29 09:12:03 +0000532
533 case MBEDTLS_ERR_X509_UNKNOWN_VERSION:
Gilles Peskine449bd832023-01-11 14:50:10 +0100534 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT,
535 MBEDTLS_ERR_X509_UNKNOWN_VERSION);
536 MBEDTLS_SSL_DEBUG_RET(1, " mbedtls_x509_crt_parse_der", ret);
537 return ret;
Xiaofei Bai947571e2021-09-29 09:12:03 +0000538
539 default:
Gilles Peskine449bd832023-01-11 14:50:10 +0100540 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_BAD_CERT,
541 ret);
542 MBEDTLS_SSL_DEBUG_RET(1, " mbedtls_x509_crt_parse_der", ret);
543 return ret;
Xiaofei Bai947571e2021-09-29 09:12:03 +0000544 }
545
546 p += cert_data_len;
547
548 /* Certificate extensions length */
Gilles Peskine449bd832023-01-11 14:50:10 +0100549 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, certificate_list_end, 2);
550 extensions_len = MBEDTLS_GET_UINT16_BE(p, 0);
Xiaofei Bai947571e2021-09-29 09:12:03 +0000551 p += 2;
Gilles Peskine449bd832023-01-11 14:50:10 +0100552 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, certificate_list_end, extensions_len);
Jerry Yu2eaa7602022-08-04 17:28:15 +0800553
554 extensions_end = p + extensions_len;
Jerry Yu0d5cfb72022-10-31 14:15:48 +0800555 handshake->received_extensions = MBEDTLS_SSL_EXT_MASK_NONE;
Jerry Yu2eaa7602022-08-04 17:28:15 +0800556
Gilles Peskine449bd832023-01-11 14:50:10 +0100557 while (p < extensions_end) {
Jerry Yu2eaa7602022-08-04 17:28:15 +0800558 unsigned int extension_type;
559 size_t extension_data_len;
560
561 /*
Gilles Peskine449bd832023-01-11 14:50:10 +0100562 * struct {
563 * ExtensionType extension_type; (2 bytes)
564 * opaque extension_data<0..2^16-1>;
565 * } Extension;
566 */
567 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, extensions_end, 4);
568 extension_type = MBEDTLS_GET_UINT16_BE(p, 0);
569 extension_data_len = MBEDTLS_GET_UINT16_BE(p, 2);
Jerry Yu2eaa7602022-08-04 17:28:15 +0800570 p += 4;
571
Gilles Peskine449bd832023-01-11 14:50:10 +0100572 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, extensions_end, extension_data_len);
Jerry Yu2eaa7602022-08-04 17:28:15 +0800573
Jerry Yuc4bf5d62022-10-29 09:08:47 +0800574 ret = mbedtls_ssl_tls13_check_received_extension(
Gilles Peskine449bd832023-01-11 14:50:10 +0100575 ssl, MBEDTLS_SSL_HS_CERTIFICATE, extension_type,
576 MBEDTLS_SSL_TLS1_3_ALLOWED_EXTS_OF_CT);
577 if (ret != 0) {
578 return ret;
579 }
Jerry Yu0c354a22022-08-29 15:25:36 +0800580
Gilles Peskine449bd832023-01-11 14:50:10 +0100581 switch (extension_type) {
Jerry Yu2eaa7602022-08-04 17:28:15 +0800582 default:
Jerry Yu79aa7212022-11-08 21:30:21 +0800583 MBEDTLS_SSL_PRINT_EXT(
Jerry Yu0d5cfb72022-10-31 14:15:48 +0800584 3, MBEDTLS_SSL_HS_CERTIFICATE,
Gilles Peskine449bd832023-01-11 14:50:10 +0100585 extension_type, "( ignored )");
Jerry Yu2eaa7602022-08-04 17:28:15 +0800586 break;
587 }
588
589 p += extension_data_len;
590 }
591
Gilles Peskine449bd832023-01-11 14:50:10 +0100592 MBEDTLS_SSL_PRINT_EXTS(3, MBEDTLS_SSL_HS_CERTIFICATE,
593 handshake->received_extensions);
Xiaofei Bai947571e2021-09-29 09:12:03 +0000594 }
595
XiaokangQian63e713e2022-05-15 04:26:57 +0000596exit:
Xiaofei Bai947571e2021-09-29 09:12:03 +0000597 /* Check that all the message is consumed. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100598 if (p != end) {
599 MBEDTLS_SSL_DEBUG_MSG(1, ("bad Certificate message"));
600 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR,
601 MBEDTLS_ERR_SSL_DECODE_ERROR);
602 return MBEDTLS_ERR_SSL_DECODE_ERROR;
Xiaofei Bai947571e2021-09-29 09:12:03 +0000603 }
604
Xiaokang Qian73437382023-03-29 08:24:12 +0000605 MBEDTLS_SSL_DEBUG_CRT(3, "peer certificate",
606 ssl->session_negotiate->peer_cert);
Xiaofei Bai947571e2021-09-29 09:12:03 +0000607
Gilles Peskine449bd832023-01-11 14:50:10 +0100608 return ret;
Xiaofei Bai947571e2021-09-29 09:12:03 +0000609}
610#else
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200611MBEDTLS_CHECK_RETURN_CRITICAL
Ronald Crone3dac4a2022-06-10 17:21:51 +0200612MBEDTLS_STATIC_TESTABLE
Gilles Peskine449bd832023-01-11 14:50:10 +0100613int mbedtls_ssl_tls13_parse_certificate(mbedtls_ssl_context *ssl,
614 const unsigned char *buf,
615 const unsigned char *end)
Xiaofei Bai947571e2021-09-29 09:12:03 +0000616{
617 ((void) ssl);
618 ((void) buf);
619 ((void) end);
Gilles Peskine449bd832023-01-11 14:50:10 +0100620 return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Xiaofei Bai947571e2021-09-29 09:12:03 +0000621}
622#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Ronald Cronde08cf32022-10-04 17:15:35 +0200623#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED */
Xiaofei Bai947571e2021-09-29 09:12:03 +0000624
Ronald Cronde08cf32022-10-04 17:15:35 +0200625#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED)
Xiaofei Bai947571e2021-09-29 09:12:03 +0000626#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Xiaofei Bai79595ac2021-10-26 07:16:45 +0000627/* Validate certificate chain sent by the server. */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200628MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100629static int ssl_tls13_validate_certificate(mbedtls_ssl_context *ssl)
Xiaofei Bai947571e2021-09-29 09:12:03 +0000630{
631 int ret = 0;
Manuel Pégourié-Gonnarde910ac82024-08-14 12:52:59 +0200632 int have_ca_chain_or_callback = 0;
Xiaofei Baiff456022021-10-28 06:50:17 +0000633 uint32_t verify_result = 0;
Xiaofei Bai947571e2021-09-29 09:12:03 +0000634
Manuel Pégourié-Gonnarde1cc9262024-08-14 09:47:38 +0200635 /* Authmode: precedence order is SNI if used else configuration */
636#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
637 const int authmode = ssl->handshake->sni_authmode != MBEDTLS_SSL_VERIFY_UNSET
638 ? ssl->handshake->sni_authmode
639 : ssl->conf->authmode;
640#else
641 const int authmode = ssl->conf->authmode;
XiaokangQian6b916b12022-04-25 07:29:34 +0000642#endif
643
644 /*
XiaokangQian989f06d2022-05-17 01:50:15 +0000645 * If the peer hasn't sent a certificate ( i.e. it sent
XiaokangQian6b916b12022-04-25 07:29:34 +0000646 * an empty certificate chain ), this is reflected in the peer CRT
647 * structure being unset.
648 * Check for that and handle it depending on the
XiaokangQian989f06d2022-05-17 01:50:15 +0000649 * authentication mode.
XiaokangQian6b916b12022-04-25 07:29:34 +0000650 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100651 if (ssl->session_negotiate->peer_cert == NULL) {
652 MBEDTLS_SSL_DEBUG_MSG(1, ("peer has no certificate"));
XiaokangQian989f06d2022-05-17 01:50:15 +0000653
XiaokangQian63e713e2022-05-15 04:26:57 +0000654#if defined(MBEDTLS_SSL_SRV_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100655 if (ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER) {
XiaokangQian63e713e2022-05-15 04:26:57 +0000656 /* The client was asked for a certificate but didn't send
657 * one. The client should know what's going on, so we
658 * don't send an alert.
659 */
660 ssl->session_negotiate->verify_result = MBEDTLS_X509_BADCERT_MISSING;
Gilles Peskine449bd832023-01-11 14:50:10 +0100661 if (authmode == MBEDTLS_SSL_VERIFY_OPTIONAL) {
662 return 0;
663 } else {
Xiaokang Qian73437382023-03-29 08:24:12 +0000664 MBEDTLS_SSL_PEND_FATAL_ALERT(
665 MBEDTLS_SSL_ALERT_MSG_NO_CERT,
666 MBEDTLS_ERR_SSL_NO_CLIENT_CERTIFICATE);
Gilles Peskine449bd832023-01-11 14:50:10 +0100667 return MBEDTLS_ERR_SSL_NO_CLIENT_CERTIFICATE;
XiaokangQian989f06d2022-05-17 01:50:15 +0000668 }
XiaokangQian63e713e2022-05-15 04:26:57 +0000669 }
XiaokangQian6b916b12022-04-25 07:29:34 +0000670#endif /* MBEDTLS_SSL_SRV_C */
671
XiaokangQianc3017f62022-05-13 05:55:41 +0000672#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard4d4c0c72024-08-12 10:36:40 +0200673 /* Regardless of authmode, the server is not allowed to send an empty
674 * certificate chain. (Last paragraph before 4.4.2.1 in RFC 8446: "The
675 * server's certificate_list MUST always be non-empty.") With authmode
676 * optional/none, we continue the handshake if we can't validate the
677 * server's cert, but we still break it if no certificate was sent. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100678 if (ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT) {
679 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_NO_CERT,
680 MBEDTLS_ERR_SSL_FATAL_ALERT_MESSAGE);
681 return MBEDTLS_ERR_SSL_FATAL_ALERT_MESSAGE;
XiaokangQian63e713e2022-05-15 04:26:57 +0000682 }
XiaokangQianc3017f62022-05-13 05:55:41 +0000683#endif /* MBEDTLS_SSL_CLI_C */
XiaokangQian63e713e2022-05-15 04:26:57 +0000684 }
XiaokangQian6b916b12022-04-25 07:29:34 +0000685
Manuel Pégourié-Gonnard2b98a4e2024-08-14 10:44:02 +0200686 /*
687 * NONE means we skip all checks
688 *
689 * Note: we still check above that the server did send a certificate,
690 * because only a non-compliant server would fail to do so. NONE means we
691 * don't care about the server certificate being valid, but we still care
692 * about the server otherwise following the TLS standard.
693 */
694 if (authmode == MBEDTLS_SSL_VERIFY_NONE) {
695 return 0;
696 }
697
Manuel Pégourié-Gonnarddee6ffa2024-08-16 09:53:41 +0200698 /* Verify callback: precedence order is SSL context, else conf struct. */
699 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *);
700 void *p_vrfy;
701 if (ssl->f_vrfy != NULL) {
702 MBEDTLS_SSL_DEBUG_MSG(3, ("Use context-specific verification callback"));
703 f_vrfy = ssl->f_vrfy;
704 p_vrfy = ssl->p_vrfy;
705 } else {
706 MBEDTLS_SSL_DEBUG_MSG(3, ("Use configuration-specific verification callback"));
707 f_vrfy = ssl->conf->f_vrfy;
708 p_vrfy = ssl->conf->p_vrfy;
709 }
710
Xiaofei Bai947571e2021-09-29 09:12:03 +0000711 /*
712 * Main check: verify certificate
713 */
Ronald Croncb7f6322024-04-03 09:07:22 +0200714#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
715 if (ssl->conf->f_ca_cb != NULL) {
Manuel Pégourié-Gonnarde910ac82024-08-14 12:52:59 +0200716 have_ca_chain_or_callback = 1;
Ronald Croncb7f6322024-04-03 09:07:22 +0200717
718 MBEDTLS_SSL_DEBUG_MSG(3, ("use CA callback for X.509 CRT verification"));
719 ret = mbedtls_x509_crt_verify_with_ca_cb(
720 ssl->session_negotiate->peer_cert,
721 ssl->conf->f_ca_cb,
722 ssl->conf->p_ca_cb,
723 ssl->conf->cert_profile,
724 ssl->hostname,
725 &verify_result,
Manuel Pégourié-Gonnarddee6ffa2024-08-16 09:53:41 +0200726 f_vrfy, p_vrfy);
Ronald Croncb7f6322024-04-03 09:07:22 +0200727 } else
728#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
729 {
Manuel Pégourié-Gonnard523a7e42024-08-14 12:51:00 +0200730 mbedtls_x509_crt *ca_chain;
731 mbedtls_x509_crl *ca_crl;
Ronald Croncb7f6322024-04-03 09:07:22 +0200732#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
733 if (ssl->handshake->sni_ca_chain != NULL) {
734 ca_chain = ssl->handshake->sni_ca_chain;
735 ca_crl = ssl->handshake->sni_ca_crl;
736 } else
737#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
738 {
739 ca_chain = ssl->conf->ca_chain;
740 ca_crl = ssl->conf->ca_crl;
741 }
742
743 if (ca_chain != NULL) {
Manuel Pégourié-Gonnarde910ac82024-08-14 12:52:59 +0200744 have_ca_chain_or_callback = 1;
Ronald Croncb7f6322024-04-03 09:07:22 +0200745 }
746
747 ret = mbedtls_x509_crt_verify_with_profile(
748 ssl->session_negotiate->peer_cert,
749 ca_chain, ca_crl,
750 ssl->conf->cert_profile,
751 ssl->hostname,
752 &verify_result,
Manuel Pégourié-Gonnarddee6ffa2024-08-16 09:53:41 +0200753 f_vrfy, p_vrfy);
Ronald Croncb7f6322024-04-03 09:07:22 +0200754 }
Xiaofei Bai947571e2021-09-29 09:12:03 +0000755
Gilles Peskine449bd832023-01-11 14:50:10 +0100756 if (ret != 0) {
757 MBEDTLS_SSL_DEBUG_RET(1, "x509_verify_cert", ret);
Xiaofei Bai947571e2021-09-29 09:12:03 +0000758 }
759
760 /*
761 * Secondary checks: always done, but change 'ret' only if it was 0
762 */
Manuel Pégourié-Gonnard4938b692024-08-09 11:49:12 +0200763 if (mbedtls_ssl_check_cert_usage(ssl->session_negotiate->peer_cert,
764 NULL,
765 ssl->conf->endpoint,
766 MBEDTLS_SSL_VERSION_TLS1_3,
767 &verify_result) != 0) {
Manuel Pégourié-Gonnardef41d8c2024-08-08 10:28:56 +0200768 MBEDTLS_SSL_DEBUG_MSG(1, ("bad certificate (usage extensions)"));
769 if (ret == 0) {
770 ret = MBEDTLS_ERR_SSL_BAD_CERTIFICATE;
771 }
Xiaofei Bai947571e2021-09-29 09:12:03 +0000772 }
773
XiaokangQian6b916b12022-04-25 07:29:34 +0000774 /* mbedtls_x509_crt_verify_with_profile is supposed to report a
775 * verification failure through MBEDTLS_ERR_X509_CERT_VERIFY_FAILED,
776 * with details encoded in the verification flags. All other kinds
777 * of error codes, including those from the user provided f_vrfy
778 * functions, are treated as fatal and lead to a failure of
Ronald Crone3dac4a2022-06-10 17:21:51 +0200779 * mbedtls_ssl_tls13_parse_certificate even if verification was optional.
780 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100781 if (authmode == MBEDTLS_SSL_VERIFY_OPTIONAL &&
782 (ret == MBEDTLS_ERR_X509_CERT_VERIFY_FAILED ||
783 ret == MBEDTLS_ERR_SSL_BAD_CERTIFICATE)) {
XiaokangQian6b916b12022-04-25 07:29:34 +0000784 ret = 0;
785 }
Xiaofei Bai947571e2021-09-29 09:12:03 +0000786
Manuel Pégourié-Gonnarde910ac82024-08-14 12:52:59 +0200787 if (!have_ca_chain_or_callback && authmode == MBEDTLS_SSL_VERIFY_REQUIRED) {
Gilles Peskine449bd832023-01-11 14:50:10 +0100788 MBEDTLS_SSL_DEBUG_MSG(1, ("got no CA chain"));
Xiaofei Bai947571e2021-09-29 09:12:03 +0000789 ret = MBEDTLS_ERR_SSL_CA_CHAIN_REQUIRED;
790 }
791
Gilles Peskine449bd832023-01-11 14:50:10 +0100792 if (ret != 0) {
Xiaofei Bai947571e2021-09-29 09:12:03 +0000793 /* The certificate may have been rejected for several reasons.
794 Pick one and send the corresponding alert. Which alert to send
795 may be a subject of debate in some cases. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100796 if (verify_result & MBEDTLS_X509_BADCERT_OTHER) {
Xiaokang Qian73437382023-03-29 08:24:12 +0000797 MBEDTLS_SSL_PEND_FATAL_ALERT(
798 MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED, ret);
Gilles Peskine449bd832023-01-11 14:50:10 +0100799 } else if (verify_result & MBEDTLS_X509_BADCERT_CN_MISMATCH) {
800 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_BAD_CERT, ret);
801 } else if (verify_result & (MBEDTLS_X509_BADCERT_KEY_USAGE |
802 MBEDTLS_X509_BADCERT_EXT_KEY_USAGE |
Gilles Peskine449bd832023-01-11 14:50:10 +0100803 MBEDTLS_X509_BADCERT_BAD_PK |
804 MBEDTLS_X509_BADCERT_BAD_KEY)) {
Xiaokang Qian73437382023-03-29 08:24:12 +0000805 MBEDTLS_SSL_PEND_FATAL_ALERT(
806 MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT, ret);
Gilles Peskine449bd832023-01-11 14:50:10 +0100807 } else if (verify_result & MBEDTLS_X509_BADCERT_EXPIRED) {
Xiaokang Qian73437382023-03-29 08:24:12 +0000808 MBEDTLS_SSL_PEND_FATAL_ALERT(
809 MBEDTLS_SSL_ALERT_MSG_CERT_EXPIRED, ret);
Gilles Peskine449bd832023-01-11 14:50:10 +0100810 } else if (verify_result & MBEDTLS_X509_BADCERT_REVOKED) {
Xiaokang Qian73437382023-03-29 08:24:12 +0000811 MBEDTLS_SSL_PEND_FATAL_ALERT(
812 MBEDTLS_SSL_ALERT_MSG_CERT_REVOKED, ret);
Gilles Peskine449bd832023-01-11 14:50:10 +0100813 } else if (verify_result & MBEDTLS_X509_BADCERT_NOT_TRUSTED) {
814 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_UNKNOWN_CA, ret);
815 } else {
Xiaokang Qian73437382023-03-29 08:24:12 +0000816 MBEDTLS_SSL_PEND_FATAL_ALERT(
817 MBEDTLS_SSL_ALERT_MSG_CERT_UNKNOWN, ret);
Gilles Peskine449bd832023-01-11 14:50:10 +0100818 }
Xiaofei Bai947571e2021-09-29 09:12:03 +0000819 }
820
821#if defined(MBEDTLS_DEBUG_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100822 if (verify_result != 0) {
823 MBEDTLS_SSL_DEBUG_MSG(3, ("! Certificate verification flags %08x",
824 (unsigned int) verify_result));
825 } else {
826 MBEDTLS_SSL_DEBUG_MSG(3, ("Certificate verification flags clear"));
Xiaofei Bai947571e2021-09-29 09:12:03 +0000827 }
828#endif /* MBEDTLS_DEBUG_C */
829
Xiaofei Baiff456022021-10-28 06:50:17 +0000830 ssl->session_negotiate->verify_result = verify_result;
Gilles Peskine449bd832023-01-11 14:50:10 +0100831 return ret;
Xiaofei Bai947571e2021-09-29 09:12:03 +0000832}
833#else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200834MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100835static int ssl_tls13_validate_certificate(mbedtls_ssl_context *ssl)
Xiaofei Bai947571e2021-09-29 09:12:03 +0000836{
837 ((void) ssl);
Gilles Peskine449bd832023-01-11 14:50:10 +0100838 return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Xiaofei Bai947571e2021-09-29 09:12:03 +0000839}
840#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Ronald Cronde08cf32022-10-04 17:15:35 +0200841#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED */
Xiaofei Bai947571e2021-09-29 09:12:03 +0000842
Gilles Peskine449bd832023-01-11 14:50:10 +0100843int mbedtls_ssl_tls13_process_certificate(mbedtls_ssl_context *ssl)
Xiaofei Bai947571e2021-09-29 09:12:03 +0000844{
Xiaofei Bai79595ac2021-10-26 07:16:45 +0000845 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine449bd832023-01-11 14:50:10 +0100846 MBEDTLS_SSL_DEBUG_MSG(2, ("=> parse certificate"));
Xiaofei Bai79595ac2021-10-26 07:16:45 +0000847
Ronald Cronde08cf32022-10-04 17:15:35 +0200848#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED)
XiaokangQianc3017f62022-05-13 05:55:41 +0000849 unsigned char *buf;
850 size_t buf_len;
Xiaofei Bai79595ac2021-10-26 07:16:45 +0000851
Gilles Peskine449bd832023-01-11 14:50:10 +0100852 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_tls13_fetch_handshake_msg(
853 ssl, MBEDTLS_SSL_HS_CERTIFICATE,
854 &buf, &buf_len));
Xiaofei Bai79595ac2021-10-26 07:16:45 +0000855
XiaokangQianc3017f62022-05-13 05:55:41 +0000856 /* Parse the certificate chain sent by the peer. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100857 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_tls13_parse_certificate(ssl, buf,
858 buf + buf_len));
XiaokangQianc3017f62022-05-13 05:55:41 +0000859 /* Validate the certificate chain and set the verification results. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100860 MBEDTLS_SSL_PROC_CHK(ssl_tls13_validate_certificate(ssl));
Xiaofei Bai79595ac2021-10-26 07:16:45 +0000861
Xiaokang Qian73437382023-03-29 08:24:12 +0000862 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_add_hs_msg_to_checksum(
863 ssl, MBEDTLS_SSL_HS_CERTIFICATE, buf, buf_len));
Xiaofei Bai79595ac2021-10-26 07:16:45 +0000864
Xiaofei Bai79595ac2021-10-26 07:16:45 +0000865cleanup:
Gilles Peskineff2558a2023-09-05 21:10:39 +0200866#else /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED */
867 (void) ssl;
Ronald Cronde08cf32022-10-04 17:15:35 +0200868#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED */
Xiaofei Bai79595ac2021-10-26 07:16:45 +0000869
Gilles Peskine449bd832023-01-11 14:50:10 +0100870 MBEDTLS_SSL_DEBUG_MSG(2, ("<= parse certificate"));
871 return ret;
Xiaofei Bai947571e2021-09-29 09:12:03 +0000872}
Ronald Cron928cbd32022-10-04 16:14:26 +0200873#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED)
Jerry Yu7399d0d2022-01-30 17:54:19 +0800874/*
875 * enum {
876 * X509(0),
877 * RawPublicKey(2),
878 * (255)
879 * } CertificateType;
880 *
881 * struct {
882 * select (certificate_type) {
883 * case RawPublicKey:
884 * // From RFC 7250 ASN.1_subjectPublicKeyInfo
885 * opaque ASN1_subjectPublicKeyInfo<1..2^24-1>;
886 *
887 * case X509:
888 * opaque cert_data<1..2^24-1>;
889 * };
890 * Extension extensions<0..2^16-1>;
891 * } CertificateEntry;
892 *
893 * struct {
894 * opaque certificate_request_context<0..2^8-1>;
895 * CertificateEntry certificate_list<0..2^24-1>;
896 * } Certificate;
897 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200898MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100899static int ssl_tls13_write_certificate_body(mbedtls_ssl_context *ssl,
900 unsigned char *buf,
901 unsigned char *end,
902 size_t *out_len)
Jerry Yu5cc35062022-01-28 16:16:08 +0800903{
Gilles Peskine449bd832023-01-11 14:50:10 +0100904 const mbedtls_x509_crt *crt = mbedtls_ssl_own_cert(ssl);
Jerry Yu3e536442022-02-15 11:05:59 +0800905 unsigned char *p = buf;
Jerry Yuc8d8d4e2022-02-18 12:10:03 +0800906 unsigned char *certificate_request_context =
Gilles Peskine449bd832023-01-11 14:50:10 +0100907 ssl->handshake->certificate_request_context;
Jerry Yuc8d8d4e2022-02-18 12:10:03 +0800908 unsigned char certificate_request_context_len =
Gilles Peskine449bd832023-01-11 14:50:10 +0100909 ssl->handshake->certificate_request_context_len;
Jerry Yuc8d8d4e2022-02-18 12:10:03 +0800910 unsigned char *p_certificate_list_len;
Jerry Yu5cc35062022-01-28 16:16:08 +0800911
Jerry Yu5cc35062022-01-28 16:16:08 +0800912
Jerry Yu3391ac02022-02-16 11:21:37 +0800913 /* ...
914 * opaque certificate_request_context<0..2^8-1>;
915 * ...
916 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100917 MBEDTLS_SSL_CHK_BUF_PTR(p, end, certificate_request_context_len + 1);
Jerry Yuc8d8d4e2022-02-18 12:10:03 +0800918 *p++ = certificate_request_context_len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100919 if (certificate_request_context_len > 0) {
920 memcpy(p, certificate_request_context, certificate_request_context_len);
Jerry Yuc8d8d4e2022-02-18 12:10:03 +0800921 p += certificate_request_context_len;
Jerry Yu537530d2022-02-15 14:00:57 +0800922 }
923
Jerry Yu3391ac02022-02-16 11:21:37 +0800924 /* ...
925 * CertificateEntry certificate_list<0..2^24-1>;
926 * ...
927 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100928 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 3);
Jerry Yuc8d8d4e2022-02-18 12:10:03 +0800929 p_certificate_list_len = p;
Jerry Yu3e536442022-02-15 11:05:59 +0800930 p += 3;
931
Gilles Peskine449bd832023-01-11 14:50:10 +0100932 MBEDTLS_SSL_DEBUG_CRT(3, "own certificate", crt);
Jerry Yu5cc35062022-01-28 16:16:08 +0800933
Gilles Peskine449bd832023-01-11 14:50:10 +0100934 while (crt != NULL) {
Jerry Yu7399d0d2022-01-30 17:54:19 +0800935 size_t cert_data_len = crt->raw.len;
Jerry Yu5cc35062022-01-28 16:16:08 +0800936
Gilles Peskine449bd832023-01-11 14:50:10 +0100937 MBEDTLS_SSL_CHK_BUF_PTR(p, end, cert_data_len + 3 + 2);
938 MBEDTLS_PUT_UINT24_BE(cert_data_len, p, 0);
Jerry Yu7399d0d2022-01-30 17:54:19 +0800939 p += 3;
Jerry Yu5cc35062022-01-28 16:16:08 +0800940
Gilles Peskine449bd832023-01-11 14:50:10 +0100941 memcpy(p, crt->raw.p, cert_data_len);
Jerry Yu7399d0d2022-01-30 17:54:19 +0800942 p += cert_data_len;
943 crt = crt->next;
Jerry Yu5cc35062022-01-28 16:16:08 +0800944
945 /* Currently, we don't have any certificate extensions defined.
946 * Hence, we are sending an empty extension with length zero.
947 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100948 MBEDTLS_PUT_UINT16_BE(0, p, 0);
Jerry Yu7399d0d2022-01-30 17:54:19 +0800949 p += 2;
Jerry Yu5cc35062022-01-28 16:16:08 +0800950 }
Jerry Yu5cc35062022-01-28 16:16:08 +0800951
Gilles Peskine449bd832023-01-11 14:50:10 +0100952 MBEDTLS_PUT_UINT24_BE(p - p_certificate_list_len - 3,
953 p_certificate_list_len, 0);
Jerry Yu7399d0d2022-01-30 17:54:19 +0800954
Jerry Yu3e536442022-02-15 11:05:59 +0800955 *out_len = p - buf;
Jerry Yu5cc35062022-01-28 16:16:08 +0800956
Jerry Yu7de2ff02022-11-08 21:43:46 +0800957 MBEDTLS_SSL_PRINT_EXTS(
Gilles Peskine449bd832023-01-11 14:50:10 +0100958 3, MBEDTLS_SSL_HS_CERTIFICATE, ssl->handshake->sent_extensions);
Jerry Yu4b8f2f72022-10-31 13:31:22 +0800959
Gilles Peskine449bd832023-01-11 14:50:10 +0100960 return 0;
Jerry Yu5cc35062022-01-28 16:16:08 +0800961}
Jerry Yu5cc35062022-01-28 16:16:08 +0800962
Gilles Peskine449bd832023-01-11 14:50:10 +0100963int mbedtls_ssl_tls13_write_certificate(mbedtls_ssl_context *ssl)
Jerry Yu5cc35062022-01-28 16:16:08 +0800964{
965 int ret;
Ronald Cron5bb8fc82022-03-09 07:00:13 +0100966 unsigned char *buf;
967 size_t buf_len, msg_len;
968
Gilles Peskine449bd832023-01-11 14:50:10 +0100969 MBEDTLS_SSL_DEBUG_MSG(2, ("=> write certificate"));
Jerry Yu5cc35062022-01-28 16:16:08 +0800970
Xiaokang Qian73437382023-03-29 08:24:12 +0000971 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_start_handshake_msg(
972 ssl, MBEDTLS_SSL_HS_CERTIFICATE, &buf, &buf_len));
Jerry Yu5cc35062022-01-28 16:16:08 +0800973
Gilles Peskine449bd832023-01-11 14:50:10 +0100974 MBEDTLS_SSL_PROC_CHK(ssl_tls13_write_certificate_body(ssl,
975 buf,
976 buf + buf_len,
977 &msg_len));
Jerry Yu5cc35062022-01-28 16:16:08 +0800978
Xiaokang Qian73437382023-03-29 08:24:12 +0000979 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_add_hs_msg_to_checksum(
980 ssl, MBEDTLS_SSL_HS_CERTIFICATE, buf, msg_len));
Jerry Yu5cc35062022-01-28 16:16:08 +0800981
Gilles Peskine449bd832023-01-11 14:50:10 +0100982 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_finish_handshake_msg(
983 ssl, buf_len, msg_len));
Jerry Yu5cc35062022-01-28 16:16:08 +0800984cleanup:
985
Gilles Peskine449bd832023-01-11 14:50:10 +0100986 MBEDTLS_SSL_DEBUG_MSG(2, ("<= write certificate"));
987 return ret;
Jerry Yu5cc35062022-01-28 16:16:08 +0800988}
989
Jerry Yu3e536442022-02-15 11:05:59 +0800990/*
991 * STATE HANDLING: Output Certificate Verify
992 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100993int mbedtls_ssl_tls13_check_sig_alg_cert_key_match(uint16_t sig_alg,
994 mbedtls_pk_context *key)
Jerry Yu0c6be8f2022-06-20 20:42:00 +0800995{
Dave Rodgman2eab4622023-10-05 13:30:37 +0100996 mbedtls_pk_type_t pk_type = (mbedtls_pk_type_t) mbedtls_ssl_sig_from_pk(key);
Gilles Peskine449bd832023-01-11 14:50:10 +0100997 size_t key_size = mbedtls_pk_get_bitlen(key);
Jerry Yu0c6be8f2022-06-20 20:42:00 +0800998
Gilles Peskine449bd832023-01-11 14:50:10 +0100999 switch (pk_type) {
Jerry Yu67eced02022-02-25 13:37:36 +08001000 case MBEDTLS_SSL_SIG_ECDSA:
Gilles Peskine449bd832023-01-11 14:50:10 +01001001 switch (key_size) {
Jerry Yu67eced02022-02-25 13:37:36 +08001002 case 256:
Gilles Peskine449bd832023-01-11 14:50:10 +01001003 return
1004 sig_alg == MBEDTLS_TLS1_3_SIG_ECDSA_SECP256R1_SHA256;
Jerry Yu0c6be8f2022-06-20 20:42:00 +08001005
Jerry Yu67eced02022-02-25 13:37:36 +08001006 case 384:
Gilles Peskine449bd832023-01-11 14:50:10 +01001007 return
1008 sig_alg == MBEDTLS_TLS1_3_SIG_ECDSA_SECP384R1_SHA384;
Jerry Yu0c6be8f2022-06-20 20:42:00 +08001009
Jerry Yu67eced02022-02-25 13:37:36 +08001010 case 521:
Gilles Peskine449bd832023-01-11 14:50:10 +01001011 return
1012 sig_alg == MBEDTLS_TLS1_3_SIG_ECDSA_SECP521R1_SHA512;
Jerry Yu67eced02022-02-25 13:37:36 +08001013 default:
Jerry Yu67eced02022-02-25 13:37:36 +08001014 break;
1015 }
1016 break;
Jerry Yu67eced02022-02-25 13:37:36 +08001017
Jerry Yu67eced02022-02-25 13:37:36 +08001018 case MBEDTLS_SSL_SIG_RSA:
Gilles Peskine449bd832023-01-11 14:50:10 +01001019 switch (sig_alg) {
Ronald Cron38391bf2022-09-16 11:19:27 +02001020 case MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA256: /* Intentional fallthrough */
1021 case MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA384: /* Intentional fallthrough */
Jerry Yu0c6be8f2022-06-20 20:42:00 +08001022 case MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA512:
Gilles Peskine449bd832023-01-11 14:50:10 +01001023 return 1;
Jerry Yuc2e04932022-06-27 22:13:03 +08001024
Jerry Yu0c6be8f2022-06-20 20:42:00 +08001025 default:
1026 break;
Jerry Yucef3f332022-03-22 23:00:13 +08001027 }
Jerry Yu67eced02022-02-25 13:37:36 +08001028 break;
Jerry Yu0c6be8f2022-06-20 20:42:00 +08001029
Jerry Yu67eced02022-02-25 13:37:36 +08001030 default:
Jerry Yu67eced02022-02-25 13:37:36 +08001031 break;
1032 }
Jerry Yu0c6be8f2022-06-20 20:42:00 +08001033
Gilles Peskine449bd832023-01-11 14:50:10 +01001034 return 0;
Jerry Yu0c6be8f2022-06-20 20:42:00 +08001035}
1036
Ronald Cronce7d76e2022-07-08 18:56:49 +02001037MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01001038static int ssl_tls13_write_certificate_verify_body(mbedtls_ssl_context *ssl,
1039 unsigned char *buf,
1040 unsigned char *end,
1041 size_t *out_len)
Jerry Yu8511f122022-01-29 10:01:04 +08001042{
Ronald Cron067a1e72022-09-16 13:44:49 +02001043 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yu3e536442022-02-15 11:05:59 +08001044 unsigned char *p = buf;
Jerry Yu8511f122022-01-29 10:01:04 +08001045 mbedtls_pk_context *own_key;
Jerry Yu3e536442022-02-15 11:05:59 +08001046
Gilles Peskine449bd832023-01-11 14:50:10 +01001047 unsigned char handshake_hash[MBEDTLS_TLS1_3_MD_MAX_SIZE];
Jerry Yu8511f122022-01-29 10:01:04 +08001048 size_t handshake_hash_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01001049 unsigned char verify_buffer[SSL_VERIFY_STRUCT_MAX_SIZE];
Jerry Yu3e536442022-02-15 11:05:59 +08001050 size_t verify_buffer_len;
Ronald Cron067a1e72022-09-16 13:44:49 +02001051
1052 uint16_t *sig_alg = ssl->handshake->received_sig_algs;
Jerry Yu3e536442022-02-15 11:05:59 +08001053 size_t signature_len = 0;
Jerry Yu8511f122022-01-29 10:01:04 +08001054
Jerry Yu0b7b1012022-02-23 12:23:05 +08001055 *out_len = 0;
1056
Gilles Peskine449bd832023-01-11 14:50:10 +01001057 own_key = mbedtls_ssl_own_key(ssl);
1058 if (own_key == NULL) {
1059 MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
1060 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Jerry Yu8511f122022-01-29 10:01:04 +08001061 }
1062
Xiaokang Qian73437382023-03-29 08:24:12 +00001063 ret = mbedtls_ssl_get_handshake_transcript(
Dave Rodgman2eab4622023-10-05 13:30:37 +01001064 ssl, (mbedtls_md_type_t) ssl->handshake->ciphersuite_info->mac,
Xiaokang Qian73437382023-03-29 08:24:12 +00001065 handshake_hash, sizeof(handshake_hash), &handshake_hash_len);
Gilles Peskine449bd832023-01-11 14:50:10 +01001066 if (ret != 0) {
1067 return ret;
1068 }
Jerry Yu8511f122022-01-29 10:01:04 +08001069
Gilles Peskine449bd832023-01-11 14:50:10 +01001070 MBEDTLS_SSL_DEBUG_BUF(3, "handshake hash",
1071 handshake_hash,
1072 handshake_hash_len);
Jerry Yu8511f122022-01-29 10:01:04 +08001073
Gilles Peskine449bd832023-01-11 14:50:10 +01001074 ssl_tls13_create_verify_structure(handshake_hash, handshake_hash_len,
1075 verify_buffer, &verify_buffer_len,
1076 ssl->conf->endpoint);
Jerry Yu8511f122022-01-29 10:01:04 +08001077
1078 /*
1079 * struct {
1080 * SignatureScheme algorithm;
1081 * opaque signature<0..2^16-1>;
1082 * } CertificateVerify;
1083 */
Ronald Cron067a1e72022-09-16 13:44:49 +02001084 /* Check there is space for the algorithm identifier (2 bytes) and the
1085 * signature length (2 bytes).
1086 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001087 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 4);
Ronald Cron067a1e72022-09-16 13:44:49 +02001088
Gilles Peskine449bd832023-01-11 14:50:10 +01001089 for (; *sig_alg != MBEDTLS_TLS1_3_SIG_NONE; sig_alg++) {
Ronald Cron067a1e72022-09-16 13:44:49 +02001090 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
1091 mbedtls_pk_type_t pk_type = MBEDTLS_PK_NONE;
1092 mbedtls_md_type_t md_alg = MBEDTLS_MD_NONE;
1093 psa_algorithm_t psa_algorithm = PSA_ALG_NONE;
1094 unsigned char verify_hash[PSA_HASH_MAX_SIZE];
1095 size_t verify_hash_len;
Jerry Yu67eced02022-02-25 13:37:36 +08001096
Gilles Peskine449bd832023-01-11 14:50:10 +01001097 if (!mbedtls_ssl_sig_alg_is_offered(ssl, *sig_alg)) {
Ronald Cron067a1e72022-09-16 13:44:49 +02001098 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +01001099 }
Jerry Yu67eced02022-02-25 13:37:36 +08001100
Gilles Peskine449bd832023-01-11 14:50:10 +01001101 if (!mbedtls_ssl_tls13_sig_alg_for_cert_verify_is_supported(*sig_alg)) {
Ronald Cron067a1e72022-09-16 13:44:49 +02001102 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +01001103 }
Ronald Cron067a1e72022-09-16 13:44:49 +02001104
Gilles Peskine449bd832023-01-11 14:50:10 +01001105 if (!mbedtls_ssl_tls13_check_sig_alg_cert_key_match(*sig_alg, own_key)) {
Ronald Cron067a1e72022-09-16 13:44:49 +02001106 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +01001107 }
Ronald Cron067a1e72022-09-16 13:44:49 +02001108
Gilles Peskine449bd832023-01-11 14:50:10 +01001109 if (mbedtls_ssl_get_pk_type_and_md_alg_from_sig_alg(
1110 *sig_alg, &pk_type, &md_alg) != 0) {
1111 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Ronald Cron067a1e72022-09-16 13:44:49 +02001112 }
1113
1114 /* Hash verify buffer with indicated hash function */
Manuel Pégourié-Gonnard2d6d9932023-03-28 11:38:08 +02001115 psa_algorithm = mbedtls_md_psa_alg_from_type(md_alg);
Gilles Peskine449bd832023-01-11 14:50:10 +01001116 status = psa_hash_compute(psa_algorithm,
1117 verify_buffer,
1118 verify_buffer_len,
1119 verify_hash, sizeof(verify_hash),
1120 &verify_hash_len);
1121 if (status != PSA_SUCCESS) {
Andrzej Kurek8a045ce2022-12-23 11:00:06 -05001122 return PSA_TO_MBEDTLS_ERR(status);
Ronald Cron067a1e72022-09-16 13:44:49 +02001123 }
1124
Gilles Peskine449bd832023-01-11 14:50:10 +01001125 MBEDTLS_SSL_DEBUG_BUF(3, "verify hash", verify_hash, verify_hash_len);
1126
1127 if ((ret = mbedtls_pk_sign_ext(pk_type, own_key,
1128 md_alg, verify_hash, verify_hash_len,
1129 p + 4, (size_t) (end - (p + 4)), &signature_len,
1130 ssl->conf->f_rng, ssl->conf->p_rng)) != 0) {
1131 MBEDTLS_SSL_DEBUG_MSG(2, ("CertificateVerify signature failed with %s",
1132 mbedtls_ssl_sig_alg_to_str(*sig_alg)));
1133 MBEDTLS_SSL_DEBUG_RET(2, "mbedtls_pk_sign_ext", ret);
1134
1135 /* The signature failed. This is possible if the private key
1136 * was not suitable for the signature operation as purposely we
1137 * did not check its suitability completely. Let's try with
1138 * another signature algorithm.
1139 */
1140 continue;
1141 }
1142
1143 MBEDTLS_SSL_DEBUG_MSG(2, ("CertificateVerify signature with %s",
1144 mbedtls_ssl_sig_alg_to_str(*sig_alg)));
Ronald Cron067a1e72022-09-16 13:44:49 +02001145
1146 break;
1147 }
1148
Gilles Peskine449bd832023-01-11 14:50:10 +01001149 if (*sig_alg == MBEDTLS_TLS1_3_SIG_NONE) {
1150 MBEDTLS_SSL_DEBUG_MSG(1, ("no suitable signature algorithm"));
1151 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE,
1152 MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE);
1153 return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
Jerry Yu8511f122022-01-29 10:01:04 +08001154 }
1155
Gilles Peskine449bd832023-01-11 14:50:10 +01001156 MBEDTLS_PUT_UINT16_BE(*sig_alg, p, 0);
1157 MBEDTLS_PUT_UINT16_BE(signature_len, p, 2);
Jerry Yuf3b46b52022-06-19 16:52:27 +08001158
Ronald Cron067a1e72022-09-16 13:44:49 +02001159 *out_len = 4 + signature_len;
Jerry Yu8c338862022-03-23 13:34:04 +08001160
Gilles Peskine449bd832023-01-11 14:50:10 +01001161 return 0;
Jerry Yu8511f122022-01-29 10:01:04 +08001162}
Jerry Yu8511f122022-01-29 10:01:04 +08001163
Gilles Peskine449bd832023-01-11 14:50:10 +01001164int mbedtls_ssl_tls13_write_certificate_verify(mbedtls_ssl_context *ssl)
Jerry Yu8511f122022-01-29 10:01:04 +08001165{
1166 int ret = 0;
Jerry Yuca133a32022-02-15 14:22:05 +08001167 unsigned char *buf;
1168 size_t buf_len, msg_len;
1169
Gilles Peskine449bd832023-01-11 14:50:10 +01001170 MBEDTLS_SSL_DEBUG_MSG(2, ("=> write certificate verify"));
Jerry Yu8511f122022-01-29 10:01:04 +08001171
Xiaokang Qian73437382023-03-29 08:24:12 +00001172 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_start_handshake_msg(
1173 ssl, MBEDTLS_SSL_HS_CERTIFICATE_VERIFY,
1174 &buf, &buf_len));
Jerry Yu8511f122022-01-29 10:01:04 +08001175
Gilles Peskine449bd832023-01-11 14:50:10 +01001176 MBEDTLS_SSL_PROC_CHK(ssl_tls13_write_certificate_verify_body(
1177 ssl, buf, buf + buf_len, &msg_len));
Jerry Yu8511f122022-01-29 10:01:04 +08001178
Xiaokang Qian73437382023-03-29 08:24:12 +00001179 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_add_hs_msg_to_checksum(
1180 ssl, MBEDTLS_SSL_HS_CERTIFICATE_VERIFY,
1181 buf, msg_len));
Jerry Yu8511f122022-01-29 10:01:04 +08001182
Gilles Peskine449bd832023-01-11 14:50:10 +01001183 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_finish_handshake_msg(
1184 ssl, buf_len, msg_len));
Jerry Yu8511f122022-01-29 10:01:04 +08001185
1186cleanup:
1187
Gilles Peskine449bd832023-01-11 14:50:10 +01001188 MBEDTLS_SSL_DEBUG_MSG(2, ("<= write certificate verify"));
1189 return ret;
Jerry Yu8511f122022-01-29 10:01:04 +08001190}
1191
Ronald Cron928cbd32022-10-04 16:14:26 +02001192#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED */
Jerry Yu90f152d2022-01-29 22:12:42 +08001193
Jerry Yu5cc35062022-01-28 16:16:08 +08001194/*
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001195 *
XiaokangQianc5c39d52021-11-09 11:55:10 +00001196 * STATE HANDLING: Incoming Finished message.
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001197 */
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001198/*
1199 * Implementation
1200 */
1201
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001202MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01001203static int ssl_tls13_preprocess_finished_message(mbedtls_ssl_context *ssl)
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001204{
1205 int ret;
1206
Xiaokang Qian73437382023-03-29 08:24:12 +00001207 ret = mbedtls_ssl_tls13_calculate_verify_data(
1208 ssl,
1209 ssl->handshake->state_local.finished_in.digest,
1210 sizeof(ssl->handshake->state_local.finished_in.digest),
1211 &ssl->handshake->state_local.finished_in.digest_len,
1212 ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT ?
1213 MBEDTLS_SSL_IS_SERVER : MBEDTLS_SSL_IS_CLIENT);
Gilles Peskine449bd832023-01-11 14:50:10 +01001214 if (ret != 0) {
1215 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_tls13_calculate_verify_data", ret);
1216 return ret;
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001217 }
1218
Gilles Peskine449bd832023-01-11 14:50:10 +01001219 return 0;
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001220}
1221
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001222MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01001223static int ssl_tls13_parse_finished_message(mbedtls_ssl_context *ssl,
1224 const unsigned char *buf,
1225 const unsigned char *end)
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001226{
XiaokangQian33062842021-11-11 03:37:45 +00001227 /*
1228 * struct {
XiaokangQianc13f9352021-11-11 06:13:22 +00001229 * opaque verify_data[Hash.length];
XiaokangQian33062842021-11-11 03:37:45 +00001230 * } Finished;
1231 */
1232 const unsigned char *expected_verify_data =
1233 ssl->handshake->state_local.finished_in.digest;
1234 size_t expected_verify_data_len =
1235 ssl->handshake->state_local.finished_in.digest_len;
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001236 /* Structural validation */
Gilles Peskine449bd832023-01-11 14:50:10 +01001237 if ((size_t) (end - buf) != expected_verify_data_len) {
1238 MBEDTLS_SSL_DEBUG_MSG(1, ("bad finished message"));
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001239
Gilles Peskine449bd832023-01-11 14:50:10 +01001240 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR,
1241 MBEDTLS_ERR_SSL_DECODE_ERROR);
1242 return MBEDTLS_ERR_SSL_DECODE_ERROR;
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001243 }
1244
Gilles Peskine449bd832023-01-11 14:50:10 +01001245 MBEDTLS_SSL_DEBUG_BUF(4, "verify_data (self-computed):",
1246 expected_verify_data,
1247 expected_verify_data_len);
1248 MBEDTLS_SSL_DEBUG_BUF(4, "verify_data (received message):", buf,
1249 expected_verify_data_len);
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001250
1251 /* Semantic validation */
Gilles Peskine449bd832023-01-11 14:50:10 +01001252 if (mbedtls_ct_memcmp(buf,
1253 expected_verify_data,
1254 expected_verify_data_len) != 0) {
1255 MBEDTLS_SSL_DEBUG_MSG(1, ("bad finished message"));
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001256
Gilles Peskine449bd832023-01-11 14:50:10 +01001257 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_DECRYPT_ERROR,
1258 MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE);
1259 return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001260 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001261 return 0;
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001262}
1263
Gilles Peskine449bd832023-01-11 14:50:10 +01001264int mbedtls_ssl_tls13_process_finished_message(mbedtls_ssl_context *ssl)
XiaokangQianc5c39d52021-11-09 11:55:10 +00001265{
XiaokangQian33062842021-11-11 03:37:45 +00001266 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
XiaokangQianc5c39d52021-11-09 11:55:10 +00001267 unsigned char *buf;
Xiaofei Baieef15042021-11-18 07:29:56 +00001268 size_t buf_len;
XiaokangQianc5c39d52021-11-09 11:55:10 +00001269
Gilles Peskine449bd832023-01-11 14:50:10 +01001270 MBEDTLS_SSL_DEBUG_MSG(2, ("=> parse finished message"));
XiaokangQianc5c39d52021-11-09 11:55:10 +00001271
Xiaokang Qian73437382023-03-29 08:24:12 +00001272 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_tls13_fetch_handshake_msg(
1273 ssl, MBEDTLS_SSL_HS_FINISHED, &buf, &buf_len));
Jerry Yu0a92d6c2022-05-16 16:54:46 +08001274
1275 /* Preprocessing step: Compute handshake digest */
Gilles Peskine449bd832023-01-11 14:50:10 +01001276 MBEDTLS_SSL_PROC_CHK(ssl_tls13_preprocess_finished_message(ssl));
Jerry Yu0a92d6c2022-05-16 16:54:46 +08001277
Xiaokang Qian73437382023-03-29 08:24:12 +00001278 MBEDTLS_SSL_PROC_CHK(ssl_tls13_parse_finished_message(
1279 ssl, buf, buf + buf_len));
Jerry Yu0a92d6c2022-05-16 16:54:46 +08001280
Xiaokang Qian73437382023-03-29 08:24:12 +00001281 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_add_hs_msg_to_checksum(
1282 ssl, MBEDTLS_SSL_HS_FINISHED, buf, buf_len));
XiaokangQianc5c39d52021-11-09 11:55:10 +00001283
1284cleanup:
1285
Gilles Peskine449bd832023-01-11 14:50:10 +01001286 MBEDTLS_SSL_DEBUG_MSG(2, ("<= parse finished message"));
1287 return ret;
XiaokangQianc5c39d52021-11-09 11:55:10 +00001288}
1289
XiaokangQian74af2a82021-09-22 07:40:30 +00001290/*
1291 *
XiaokangQiancc90c942021-11-09 12:30:09 +00001292 * STATE HANDLING: Write and send Finished message.
XiaokangQian74af2a82021-09-22 07:40:30 +00001293 *
1294 */
XiaokangQian74af2a82021-09-22 07:40:30 +00001295/*
XiaokangQian35dc6252021-11-11 08:16:19 +00001296 * Implement
XiaokangQian74af2a82021-09-22 07:40:30 +00001297 */
1298
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001299MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01001300static int ssl_tls13_prepare_finished_message(mbedtls_ssl_context *ssl)
XiaokangQian74af2a82021-09-22 07:40:30 +00001301{
1302 int ret;
1303
1304 /* Compute transcript of handshake up to now. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001305 ret = mbedtls_ssl_tls13_calculate_verify_data(ssl,
1306 ssl->handshake->state_local.finished_out.digest,
1307 sizeof(ssl->handshake->state_local.finished_out.
1308 digest),
1309 &ssl->handshake->state_local.finished_out.digest_len,
1310 ssl->conf->endpoint);
XiaokangQian74af2a82021-09-22 07:40:30 +00001311
Gilles Peskine449bd832023-01-11 14:50:10 +01001312 if (ret != 0) {
1313 MBEDTLS_SSL_DEBUG_RET(1, "calculate_verify_data failed", ret);
1314 return ret;
XiaokangQian74af2a82021-09-22 07:40:30 +00001315 }
1316
Gilles Peskine449bd832023-01-11 14:50:10 +01001317 return 0;
XiaokangQian74af2a82021-09-22 07:40:30 +00001318}
1319
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001320MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01001321static int ssl_tls13_write_finished_message_body(mbedtls_ssl_context *ssl,
1322 unsigned char *buf,
1323 unsigned char *end,
1324 size_t *out_len)
XiaokangQian74af2a82021-09-22 07:40:30 +00001325{
XiaokangQian8773aa02021-11-10 07:33:09 +00001326 size_t verify_data_len = ssl->handshake->state_local.finished_out.digest_len;
XiaokangQian0fa66432021-11-15 03:33:57 +00001327 /*
1328 * struct {
1329 * opaque verify_data[Hash.length];
1330 * } Finished;
1331 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001332 MBEDTLS_SSL_CHK_BUF_PTR(buf, end, verify_data_len);
XiaokangQian74af2a82021-09-22 07:40:30 +00001333
Gilles Peskine449bd832023-01-11 14:50:10 +01001334 memcpy(buf, ssl->handshake->state_local.finished_out.digest,
1335 verify_data_len);
XiaokangQian74af2a82021-09-22 07:40:30 +00001336
Xiaofei Baid25fab62021-12-02 06:36:27 +00001337 *out_len = verify_data_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01001338 return 0;
XiaokangQian74af2a82021-09-22 07:40:30 +00001339}
XiaokangQianc5c39d52021-11-09 11:55:10 +00001340
XiaokangQian35dc6252021-11-11 08:16:19 +00001341/* Main entry point: orchestrates the other functions */
Gilles Peskine449bd832023-01-11 14:50:10 +01001342int mbedtls_ssl_tls13_write_finished_message(mbedtls_ssl_context *ssl)
XiaokangQian35dc6252021-11-11 08:16:19 +00001343{
1344 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1345 unsigned char *buf;
1346 size_t buf_len, msg_len;
1347
Gilles Peskine449bd832023-01-11 14:50:10 +01001348 MBEDTLS_SSL_DEBUG_MSG(2, ("=> write finished message"));
XiaokangQian35dc6252021-11-11 08:16:19 +00001349
Gilles Peskine449bd832023-01-11 14:50:10 +01001350 MBEDTLS_SSL_PROC_CHK(ssl_tls13_prepare_finished_message(ssl));
XiaokangQiandce82242021-11-15 06:01:26 +00001351
Gilles Peskine449bd832023-01-11 14:50:10 +01001352 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_start_handshake_msg(ssl,
1353 MBEDTLS_SSL_HS_FINISHED, &buf, &buf_len));
XiaokangQian35dc6252021-11-11 08:16:19 +00001354
Gilles Peskine449bd832023-01-11 14:50:10 +01001355 MBEDTLS_SSL_PROC_CHK(ssl_tls13_write_finished_message_body(
1356 ssl, buf, buf + buf_len, &msg_len));
XiaokangQian35dc6252021-11-11 08:16:19 +00001357
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +01001358 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_add_hs_msg_to_checksum(ssl,
Manuel Pégourié-Gonnard43cc1272023-02-06 11:48:19 +01001359 MBEDTLS_SSL_HS_FINISHED, buf, msg_len));
XiaokangQian35dc6252021-11-11 08:16:19 +00001360
Gilles Peskine449bd832023-01-11 14:50:10 +01001361 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_finish_handshake_msg(
1362 ssl, buf_len, msg_len));
XiaokangQian35dc6252021-11-11 08:16:19 +00001363cleanup:
1364
Gilles Peskine449bd832023-01-11 14:50:10 +01001365 MBEDTLS_SSL_DEBUG_MSG(2, ("<= write finished message"));
1366 return ret;
XiaokangQian35dc6252021-11-11 08:16:19 +00001367}
1368
Gilles Peskine449bd832023-01-11 14:50:10 +01001369void mbedtls_ssl_tls13_handshake_wrapup(mbedtls_ssl_context *ssl)
Jerry Yu378254d2021-10-30 21:44:47 +08001370{
1371
Gilles Peskine449bd832023-01-11 14:50:10 +01001372 MBEDTLS_SSL_DEBUG_MSG(3, ("=> handshake wrapup"));
Jerry Yu378254d2021-10-30 21:44:47 +08001373
Gilles Peskine449bd832023-01-11 14:50:10 +01001374 MBEDTLS_SSL_DEBUG_MSG(1, ("Switch to application keys for inbound traffic"));
1375 mbedtls_ssl_set_inbound_transform(ssl, ssl->transform_application);
Jerry Yue8c1fca2022-05-18 14:48:56 +08001376
Gilles Peskine449bd832023-01-11 14:50:10 +01001377 MBEDTLS_SSL_DEBUG_MSG(1, ("Switch to application keys for outbound traffic"));
1378 mbedtls_ssl_set_outbound_transform(ssl, ssl->transform_application);
Jerry Yue8c1fca2022-05-18 14:48:56 +08001379
Jerry Yu378254d2021-10-30 21:44:47 +08001380 /*
Jerry Yucfe64f02021-11-15 13:54:06 +08001381 * Free the previous session and switch to the current one.
Jerry Yu378254d2021-10-30 21:44:47 +08001382 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001383 if (ssl->session) {
1384 mbedtls_ssl_session_free(ssl->session);
1385 mbedtls_free(ssl->session);
Jerry Yu378254d2021-10-30 21:44:47 +08001386 }
1387 ssl->session = ssl->session_negotiate;
1388 ssl->session_negotiate = NULL;
1389
Gilles Peskine449bd832023-01-11 14:50:10 +01001390 MBEDTLS_SSL_DEBUG_MSG(3, ("<= handshake wrapup"));
Jerry Yu378254d2021-10-30 21:44:47 +08001391}
1392
Ronald Cron49ad6192021-11-24 16:25:31 +01001393/*
1394 *
1395 * STATE HANDLING: Write ChangeCipherSpec
1396 *
1397 */
1398#if defined(MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001399MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01001400static int ssl_tls13_write_change_cipher_spec_body(mbedtls_ssl_context *ssl,
1401 unsigned char *buf,
1402 unsigned char *end,
1403 size_t *olen)
Ronald Cron49ad6192021-11-24 16:25:31 +01001404{
1405 ((void) ssl);
1406
Gilles Peskine449bd832023-01-11 14:50:10 +01001407 MBEDTLS_SSL_CHK_BUF_PTR(buf, end, 1);
Ronald Cron49ad6192021-11-24 16:25:31 +01001408 buf[0] = 1;
1409 *olen = 1;
1410
Gilles Peskine449bd832023-01-11 14:50:10 +01001411 return 0;
Ronald Cron49ad6192021-11-24 16:25:31 +01001412}
1413
Gilles Peskine449bd832023-01-11 14:50:10 +01001414int mbedtls_ssl_tls13_write_change_cipher_spec(mbedtls_ssl_context *ssl)
Ronald Cron49ad6192021-11-24 16:25:31 +01001415{
1416 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1417
Gilles Peskine449bd832023-01-11 14:50:10 +01001418 MBEDTLS_SSL_DEBUG_MSG(2, ("=> write change cipher spec"));
Ronald Cron49ad6192021-11-24 16:25:31 +01001419
Ronald Crone273f722024-02-13 18:22:26 +01001420 /* Only one CCS to send. */
Ronald Cron5fbd2702024-02-14 10:03:36 +01001421 if (ssl->handshake->ccs_sent) {
Ronald Crone273f722024-02-13 18:22:26 +01001422 ret = 0;
1423 goto cleanup;
1424 }
1425
Ronald Cron49ad6192021-11-24 16:25:31 +01001426 /* Write CCS message */
Gilles Peskine449bd832023-01-11 14:50:10 +01001427 MBEDTLS_SSL_PROC_CHK(ssl_tls13_write_change_cipher_spec_body(
1428 ssl, ssl->out_msg,
1429 ssl->out_msg + MBEDTLS_SSL_OUT_CONTENT_LEN,
1430 &ssl->out_msglen));
Ronald Cron49ad6192021-11-24 16:25:31 +01001431
1432 ssl->out_msgtype = MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC;
1433
Ronald Cron49ad6192021-11-24 16:25:31 +01001434 /* Dispatch message */
Gilles Peskine449bd832023-01-11 14:50:10 +01001435 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_write_record(ssl, 0));
Ronald Cron49ad6192021-11-24 16:25:31 +01001436
Ronald Cron5fbd2702024-02-14 10:03:36 +01001437 ssl->handshake->ccs_sent = 1;
Ronald Cronfe59ff72024-01-24 14:31:50 +01001438
Ronald Cron49ad6192021-11-24 16:25:31 +01001439cleanup:
1440
Gilles Peskine449bd832023-01-11 14:50:10 +01001441 MBEDTLS_SSL_DEBUG_MSG(2, ("<= write change cipher spec"));
1442 return ret;
Ronald Cron49ad6192021-11-24 16:25:31 +01001443}
1444
1445#endif /* MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE */
1446
Xiaokang Qianecc29482022-11-02 07:52:47 +00001447/* Early Data Indication Extension
Xiaokang Qian0e97d4d2022-10-24 11:12:51 +00001448 *
Xiaokang Qian0e97d4d2022-10-24 11:12:51 +00001449 * struct {
1450 * select ( Handshake.msg_type ) {
Jerry Yu52335392023-11-23 18:06:06 +08001451 * case new_session_ticket: uint32 max_early_data_size;
Xiaokang Qian0e97d4d2022-10-24 11:12:51 +00001452 * case client_hello: Empty;
1453 * case encrypted_extensions: Empty;
1454 * };
1455 * } EarlyDataIndication;
1456 */
1457#if defined(MBEDTLS_SSL_EARLY_DATA)
Gilles Peskine449bd832023-01-11 14:50:10 +01001458int mbedtls_ssl_tls13_write_early_data_ext(mbedtls_ssl_context *ssl,
Jerry Yuc59c5862023-12-05 10:40:49 +08001459 int in_new_session_ticket,
Gilles Peskine449bd832023-01-11 14:50:10 +01001460 unsigned char *buf,
1461 const unsigned char *end,
Jerry Yuc59c5862023-12-05 10:40:49 +08001462 size_t *out_len)
Xiaokang Qian0e97d4d2022-10-24 11:12:51 +00001463{
1464 unsigned char *p = buf;
Xiaokang Qian0e97d4d2022-10-24 11:12:51 +00001465
Jerry Yu52335392023-11-23 18:06:06 +08001466#if defined(MBEDTLS_SSL_SRV_C)
Jerry Yuc59c5862023-12-05 10:40:49 +08001467 const size_t needed = in_new_session_ticket ? 8 : 4;
Jerry Yu52335392023-11-23 18:06:06 +08001468#else
1469 const size_t needed = 4;
Jerry Yuc59c5862023-12-05 10:40:49 +08001470 ((void) in_new_session_ticket);
Jerry Yu52335392023-11-23 18:06:06 +08001471#endif
1472
1473 *out_len = 0;
1474
1475 MBEDTLS_SSL_CHK_BUF_PTR(p, end, needed);
Xiaokang Qian0e97d4d2022-10-24 11:12:51 +00001476
Gilles Peskine449bd832023-01-11 14:50:10 +01001477 MBEDTLS_PUT_UINT16_BE(MBEDTLS_TLS_EXT_EARLY_DATA, p, 0);
Jerry Yu52335392023-11-23 18:06:06 +08001478 MBEDTLS_PUT_UINT16_BE(needed - 4, p, 2);
Xiaokang Qian0e97d4d2022-10-24 11:12:51 +00001479
Jerry Yu52335392023-11-23 18:06:06 +08001480#if defined(MBEDTLS_SSL_SRV_C)
Jerry Yuc59c5862023-12-05 10:40:49 +08001481 if (in_new_session_ticket) {
1482 MBEDTLS_PUT_UINT32_BE(ssl->conf->max_early_data_size, p, 4);
Jerry Yu52335392023-11-23 18:06:06 +08001483 MBEDTLS_SSL_DEBUG_MSG(
1484 4, ("Sent max_early_data_size=%u",
Jerry Yuc59c5862023-12-05 10:40:49 +08001485 (unsigned int) ssl->conf->max_early_data_size));
Jerry Yu52335392023-11-23 18:06:06 +08001486 }
1487#endif
1488
1489 *out_len = needed;
Xiaokang Qian2cd5ce02022-11-15 10:33:53 +00001490
Gilles Peskine449bd832023-01-11 14:50:10 +01001491 mbedtls_ssl_tls13_set_hs_sent_ext_mask(ssl, MBEDTLS_TLS_EXT_EARLY_DATA);
Xiaokang Qian2cd5ce02022-11-15 10:33:53 +00001492
Gilles Peskine449bd832023-01-11 14:50:10 +01001493 return 0;
Xiaokang Qian0e97d4d2022-10-24 11:12:51 +00001494}
Ronald Cron85718042024-02-22 10:22:09 +01001495
1496#if defined(MBEDTLS_SSL_SRV_C)
1497int mbedtls_ssl_tls13_check_early_data_len(mbedtls_ssl_context *ssl,
1498 size_t early_data_len)
1499{
Ronald Cron85718042024-02-22 10:22:09 +01001500 /*
1501 * This function should be called only while an handshake is in progress
1502 * and thus a session under negotiation. Add a sanity check to detect a
1503 * misuse.
1504 */
1505 if (ssl->session_negotiate == NULL) {
1506 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
1507 }
1508
1509 /* RFC 8446 section 4.6.1
1510 *
1511 * A server receiving more than max_early_data_size bytes of 0-RTT data
1512 * SHOULD terminate the connection with an "unexpected_message" alert.
Ronald Cron93795f22024-03-07 09:24:56 +01001513 * Note that if it is still possible to send early_data_len bytes of early
1514 * data, it means that early_data_len is smaller than max_early_data_size
1515 * (type uint32_t) and can fit in an uint32_t. We use this further
1516 * down.
Ronald Cron85718042024-02-22 10:22:09 +01001517 */
Ronald Cron2e7dfd52024-03-05 10:54:33 +01001518 if (early_data_len >
Ronald Cron85718042024-02-22 10:22:09 +01001519 (ssl->session_negotiate->max_early_data_size -
Ronald Cron19bfe0a2024-02-26 16:43:01 +01001520 ssl->total_early_data_size)) {
Ronald Cron85718042024-02-22 10:22:09 +01001521
1522 MBEDTLS_SSL_DEBUG_MSG(
Gilles Peskineeeb4ff52024-06-03 22:16:23 +02001523 2, ("EarlyData: Too much early data received, "
Gilles Peskine400659b2024-06-04 08:45:58 +02001524 "%lu + %" MBEDTLS_PRINTF_SIZET " > %lu",
1525 (unsigned long) ssl->total_early_data_size,
Gilles Peskineeeb4ff52024-06-03 22:16:23 +02001526 early_data_len,
Gilles Peskine400659b2024-06-04 08:45:58 +02001527 (unsigned long) ssl->session_negotiate->max_early_data_size));
Ronald Cron85718042024-02-22 10:22:09 +01001528
1529 MBEDTLS_SSL_PEND_FATAL_ALERT(
1530 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE,
1531 MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE);
1532 return MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE;
1533 }
1534
Ronald Cron2e7dfd52024-03-05 10:54:33 +01001535 /*
Ronald Cron93795f22024-03-07 09:24:56 +01001536 * early_data_len has been checked to be less than max_early_data_size
1537 * that is uint32_t. Its cast to an uint32_t below is thus safe. We need
1538 * the cast to appease some compilers.
Ronald Cron2e7dfd52024-03-05 10:54:33 +01001539 */
Ronald Cron2e7dfd52024-03-05 10:54:33 +01001540 ssl->total_early_data_size += (uint32_t) early_data_len;
Ronald Cron85718042024-02-22 10:22:09 +01001541
1542 return 0;
1543}
1544#endif /* MBEDTLS_SSL_SRV_C */
Xiaokang Qian0e97d4d2022-10-24 11:12:51 +00001545#endif /* MBEDTLS_SSL_EARLY_DATA */
1546
XiaokangQian78b1fa72022-01-19 06:56:30 +00001547/* Reset SSL context and update hash for handling HRR.
1548 *
1549 * Replace Transcript-Hash(X) by
1550 * Transcript-Hash( message_hash ||
1551 * 00 00 Hash.length ||
1552 * X )
1553 * A few states of the handshake are preserved, including:
1554 * - session ID
1555 * - session ticket
1556 * - negotiated ciphersuite
1557 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001558int mbedtls_ssl_reset_transcript_for_hrr(mbedtls_ssl_context *ssl)
XiaokangQian78b1fa72022-01-19 06:56:30 +00001559{
1560 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Przemyslaw Stekielda645252022-09-14 12:50:51 +02001561 unsigned char hash_transcript[PSA_HASH_MAX_SIZE + 4];
XiaokangQian0ece9982022-01-24 08:56:23 +00001562 size_t hash_len;
Xiaokang Qian6b980012023-02-07 03:17:45 +00001563 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
1564 ssl->handshake->ciphersuite_info;
XiaokangQian78b1fa72022-01-19 06:56:30 +00001565
Gilles Peskine449bd832023-01-11 14:50:10 +01001566 MBEDTLS_SSL_DEBUG_MSG(3, ("Reset SSL session for HRR"));
XiaokangQian78b1fa72022-01-19 06:56:30 +00001567
Dave Rodgman2eab4622023-10-05 13:30:37 +01001568 ret = mbedtls_ssl_get_handshake_transcript(ssl, (mbedtls_md_type_t) ciphersuite_info->mac,
Gilles Peskine449bd832023-01-11 14:50:10 +01001569 hash_transcript + 4,
1570 PSA_HASH_MAX_SIZE,
1571 &hash_len);
1572 if (ret != 0) {
Manuel Pégourié-Gonnardda7979b2023-02-21 09:31:10 +01001573 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_get_handshake_transcript", ret);
Gilles Peskine449bd832023-01-11 14:50:10 +01001574 return ret;
XiaokangQian0ece9982022-01-24 08:56:23 +00001575 }
1576
1577 hash_transcript[0] = MBEDTLS_SSL_HS_MESSAGE_HASH;
1578 hash_transcript[1] = 0;
1579 hash_transcript[2] = 0;
1580 hash_transcript[3] = (unsigned char) hash_len;
1581
1582 hash_len += 4;
1583
Manuel Pégourié-Gonnardda7979b2023-02-21 09:31:10 +01001584 MBEDTLS_SSL_DEBUG_BUF(4, "Truncated handshake transcript",
1585 hash_transcript, hash_len);
1586
Manuel Pégourié-Gonnardd7a7a232023-02-05 10:26:49 +01001587 /* Reset running hash and replace it with a hash of the transcript */
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +01001588 ret = mbedtls_ssl_reset_checksum(ssl);
1589 if (ret != 0) {
1590 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_reset_checksum", ret);
1591 return ret;
1592 }
1593 ret = ssl->handshake->update_checksum(ssl, hash_transcript, hash_len);
1594 if (ret != 0) {
1595 MBEDTLS_SSL_DEBUG_RET(1, "update_checksum", ret);
1596 return ret;
1597 }
Przemyslaw Stekiel4b3fff42022-02-14 16:39:52 +01001598
Gilles Peskine449bd832023-01-11 14:50:10 +01001599 return ret;
XiaokangQian78b1fa72022-01-19 06:56:30 +00001600}
1601
Valerio Settic9ae8622023-07-25 11:23:50 +02001602#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_EPHEMERAL_ENABLED)
XiaokangQian7807f9f2022-02-15 10:04:37 +00001603
Przemek Stekiel7ac93be2023-07-04 10:02:38 +02001604int mbedtls_ssl_tls13_read_public_xxdhe_share(mbedtls_ssl_context *ssl,
Gilles Peskine449bd832023-01-11 14:50:10 +01001605 const unsigned char *buf,
1606 size_t buf_len)
XiaokangQian7807f9f2022-02-15 10:04:37 +00001607{
Gilles Peskine449bd832023-01-11 14:50:10 +01001608 uint8_t *p = (uint8_t *) buf;
XiaokangQiancfd925f2022-04-14 07:10:37 +00001609 const uint8_t *end = buf + buf_len;
XiaokangQian9b5d04b2022-04-10 10:20:43 +00001610 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001611
XiaokangQian9b5d04b2022-04-10 10:20:43 +00001612 /* Get size of the TLS opaque key_exchange field of the KeyShareEntry struct. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001613 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, 2);
1614 uint16_t peerkey_len = MBEDTLS_GET_UINT16_BE(p, 0);
XiaokangQian9b5d04b2022-04-10 10:20:43 +00001615 p += 2;
XiaokangQian3207a322022-02-23 03:15:27 +00001616
XiaokangQian9b5d04b2022-04-10 10:20:43 +00001617 /* Check if key size is consistent with given buffer length. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001618 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, peerkey_len);
XiaokangQian9b5d04b2022-04-10 10:20:43 +00001619
Gilles Peskine12c5aaa2023-10-02 14:55:45 +02001620 /* Store peer's ECDH/FFDH public key. */
1621 if (peerkey_len > sizeof(handshake->xxdh_psa_peerkey)) {
Gilles Peskine530c4232023-10-02 15:37:23 +02001622 MBEDTLS_SSL_DEBUG_MSG(1, ("Invalid public key length: %u > %" MBEDTLS_PRINTF_SIZET,
1623 (unsigned) peerkey_len,
1624 sizeof(handshake->xxdh_psa_peerkey)));
Gilles Peskine12c5aaa2023-10-02 14:55:45 +02001625 return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
1626 }
Przemek Stekiel7ac93be2023-07-04 10:02:38 +02001627 memcpy(handshake->xxdh_psa_peerkey, p, peerkey_len);
1628 handshake->xxdh_psa_peerkey_len = peerkey_len;
XiaokangQian9b5d04b2022-04-10 10:20:43 +00001629
Gilles Peskine449bd832023-01-11 14:50:10 +01001630 return 0;
XiaokangQian3207a322022-02-23 03:15:27 +00001631}
Jerry Yu89e103c2022-03-30 22:43:29 +08001632
Valerio Setti711f8532023-07-31 11:28:07 +02001633#if defined(PSA_WANT_ALG_FFDH)
Przemek Stekielda4fba62023-06-02 14:52:28 +02001634static psa_status_t mbedtls_ssl_get_psa_ffdh_info_from_tls_id(
Przemek Stekiel75a5a9c2023-06-12 11:21:18 +02001635 uint16_t tls_id, size_t *bits, psa_key_type_t *key_type)
Przemek Stekiel29c219c2023-05-31 15:21:04 +02001636{
Przemek Stekiel75a5a9c2023-06-12 11:21:18 +02001637 switch (tls_id) {
Valerio Settiecaf7c52024-01-17 12:30:30 +01001638#if defined(PSA_WANT_DH_RFC7919_2048)
Przemek Stekiel29c219c2023-05-31 15:21:04 +02001639 case MBEDTLS_SSL_IANA_TLS_GROUP_FFDHE2048:
1640 *bits = 2048;
1641 *key_type = PSA_KEY_TYPE_DH_KEY_PAIR(PSA_DH_FAMILY_RFC7919);
1642 return PSA_SUCCESS;
Valerio Settiecaf7c52024-01-17 12:30:30 +01001643#endif /* PSA_WANT_DH_RFC7919_2048 */
1644#if defined(PSA_WANT_DH_RFC7919_3072)
Przemek Stekiel29c219c2023-05-31 15:21:04 +02001645 case MBEDTLS_SSL_IANA_TLS_GROUP_FFDHE3072:
1646 *bits = 3072;
1647 *key_type = PSA_KEY_TYPE_DH_KEY_PAIR(PSA_DH_FAMILY_RFC7919);
1648 return PSA_SUCCESS;
Valerio Settiecaf7c52024-01-17 12:30:30 +01001649#endif /* PSA_WANT_DH_RFC7919_3072 */
1650#if defined(PSA_WANT_DH_RFC7919_4096)
Przemek Stekiel29c219c2023-05-31 15:21:04 +02001651 case MBEDTLS_SSL_IANA_TLS_GROUP_FFDHE4096:
1652 *bits = 4096;
1653 *key_type = PSA_KEY_TYPE_DH_KEY_PAIR(PSA_DH_FAMILY_RFC7919);
1654 return PSA_SUCCESS;
Valerio Settiecaf7c52024-01-17 12:30:30 +01001655#endif /* PSA_WANT_DH_RFC7919_4096 */
1656#if defined(PSA_WANT_DH_RFC7919_6144)
Przemek Stekiel29c219c2023-05-31 15:21:04 +02001657 case MBEDTLS_SSL_IANA_TLS_GROUP_FFDHE6144:
1658 *bits = 6144;
1659 *key_type = PSA_KEY_TYPE_DH_KEY_PAIR(PSA_DH_FAMILY_RFC7919);
1660 return PSA_SUCCESS;
Valerio Settiecaf7c52024-01-17 12:30:30 +01001661#endif /* PSA_WANT_DH_RFC7919_6144 */
1662#if defined(PSA_WANT_DH_RFC7919_8192)
Przemek Stekiel29c219c2023-05-31 15:21:04 +02001663 case MBEDTLS_SSL_IANA_TLS_GROUP_FFDHE8192:
1664 *bits = 8192;
1665 *key_type = PSA_KEY_TYPE_DH_KEY_PAIR(PSA_DH_FAMILY_RFC7919);
1666 return PSA_SUCCESS;
Valerio Settiecaf7c52024-01-17 12:30:30 +01001667#endif /* PSA_WANT_DH_RFC7919_8192 */
Przemek Stekiel29c219c2023-05-31 15:21:04 +02001668 default:
1669 return PSA_ERROR_NOT_SUPPORTED;
1670 }
1671}
Valerio Setti711f8532023-07-31 11:28:07 +02001672#endif /* PSA_WANT_ALG_FFDH */
Przemek Stekiel29c219c2023-05-31 15:21:04 +02001673
Przemek Stekiel408569f2023-07-06 11:26:44 +02001674int mbedtls_ssl_tls13_generate_and_write_xxdh_key_exchange(
Gilles Peskine449bd832023-01-11 14:50:10 +01001675 mbedtls_ssl_context *ssl,
1676 uint16_t named_group,
1677 unsigned char *buf,
1678 unsigned char *end,
1679 size_t *out_len)
Jerry Yu89e103c2022-03-30 22:43:29 +08001680{
1681 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
1682 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
1683 psa_key_attributes_t key_attributes;
1684 size_t own_pubkey_len;
1685 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
Przemek Stekiel29c219c2023-05-31 15:21:04 +02001686 size_t bits = 0;
Przemek Stekiel75a5a9c2023-06-12 11:21:18 +02001687 psa_key_type_t key_type = PSA_KEY_TYPE_NONE;
1688 psa_algorithm_t alg = PSA_ALG_NONE;
Przemek Stekiel29c219c2023-05-31 15:21:04 +02001689 size_t buf_size = (size_t) (end - buf);
Jerry Yu89e103c2022-03-30 22:43:29 +08001690
Przemek Stekiel29c219c2023-05-31 15:21:04 +02001691 MBEDTLS_SSL_DEBUG_MSG(1, ("Perform PSA-based ECDH/FFDH computation."));
Jerry Yu89e103c2022-03-30 22:43:29 +08001692
Valerio Setti40d9ca92023-01-04 16:08:04 +01001693 /* Convert EC's TLS ID to PSA key type. */
Przemek Stekiel29c219c2023-05-31 15:21:04 +02001694#if defined(PSA_WANT_ALG_ECDH)
Xiaokang Qian73437382023-03-29 08:24:12 +00001695 if (mbedtls_ssl_get_psa_curve_info_from_tls_id(
Przemek Stekielda4fba62023-06-02 14:52:28 +02001696 named_group, &key_type, &bits) == PSA_SUCCESS) {
1697 alg = PSA_ALG_ECDH;
Przemek Stekiel29c219c2023-05-31 15:21:04 +02001698 }
1699#endif
1700#if defined(PSA_WANT_ALG_FFDH)
Przemek Stekiel75a5a9c2023-06-12 11:21:18 +02001701 if (mbedtls_ssl_get_psa_ffdh_info_from_tls_id(named_group, &bits,
1702 &key_type) == PSA_SUCCESS) {
Przemek Stekielda4fba62023-06-02 14:52:28 +02001703 alg = PSA_ALG_FFDH;
Przemek Stekiel29c219c2023-05-31 15:21:04 +02001704 }
1705#endif
1706
Przemek Stekiel75a5a9c2023-06-12 11:21:18 +02001707 if (key_type == PSA_KEY_TYPE_NONE) {
Gilles Peskine449bd832023-01-11 14:50:10 +01001708 return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
Valerio Setti40d9ca92023-01-04 16:08:04 +01001709 }
Przemek Stekiel29c219c2023-05-31 15:21:04 +02001710
Przemek Stekielda4fba62023-06-02 14:52:28 +02001711 if (buf_size < PSA_BITS_TO_BYTES(bits)) {
Przemek Stekielda4fba62023-06-02 14:52:28 +02001712 return MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL;
1713 }
1714
Przemek Stekiel7ac93be2023-07-04 10:02:38 +02001715 handshake->xxdh_psa_type = key_type;
Valerio Settiea59c432023-07-25 11:14:03 +02001716 ssl->handshake->xxdh_psa_bits = bits;
Jerry Yu89e103c2022-03-30 22:43:29 +08001717
1718 key_attributes = psa_key_attributes_init();
Gilles Peskine449bd832023-01-11 14:50:10 +01001719 psa_set_key_usage_flags(&key_attributes, PSA_KEY_USAGE_DERIVE);
Przemek Stekielda4fba62023-06-02 14:52:28 +02001720 psa_set_key_algorithm(&key_attributes, alg);
Przemek Stekiel7ac93be2023-07-04 10:02:38 +02001721 psa_set_key_type(&key_attributes, handshake->xxdh_psa_type);
Valerio Settiea59c432023-07-25 11:14:03 +02001722 psa_set_key_bits(&key_attributes, handshake->xxdh_psa_bits);
Jerry Yu89e103c2022-03-30 22:43:29 +08001723
Przemek Stekiel29c219c2023-05-31 15:21:04 +02001724 /* Generate ECDH/FFDH private key. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001725 status = psa_generate_key(&key_attributes,
Przemek Stekiel7ac93be2023-07-04 10:02:38 +02001726 &handshake->xxdh_psa_privkey);
Gilles Peskine449bd832023-01-11 14:50:10 +01001727 if (status != PSA_SUCCESS) {
Andrzej Kurek8a045ce2022-12-23 11:00:06 -05001728 ret = PSA_TO_MBEDTLS_ERR(status);
Gilles Peskine449bd832023-01-11 14:50:10 +01001729 MBEDTLS_SSL_DEBUG_RET(1, "psa_generate_key", ret);
1730 return ret;
Jerry Yu89e103c2022-03-30 22:43:29 +08001731
1732 }
1733
Przemek Stekiel29c219c2023-05-31 15:21:04 +02001734 /* Export the public part of the ECDH/FFDH private key from PSA. */
Przemek Stekiel7ac93be2023-07-04 10:02:38 +02001735 status = psa_export_public_key(handshake->xxdh_psa_privkey,
Przemek Stekiel29c219c2023-05-31 15:21:04 +02001736 buf, buf_size,
Gilles Peskine449bd832023-01-11 14:50:10 +01001737 &own_pubkey_len);
Przemek Stekiel29c219c2023-05-31 15:21:04 +02001738
Gilles Peskine449bd832023-01-11 14:50:10 +01001739 if (status != PSA_SUCCESS) {
Andrzej Kurek8a045ce2022-12-23 11:00:06 -05001740 ret = PSA_TO_MBEDTLS_ERR(status);
Gilles Peskine449bd832023-01-11 14:50:10 +01001741 MBEDTLS_SSL_DEBUG_RET(1, "psa_export_public_key", ret);
1742 return ret;
Jerry Yu89e103c2022-03-30 22:43:29 +08001743 }
1744
1745 *out_len = own_pubkey_len;
1746
Gilles Peskine449bd832023-01-11 14:50:10 +01001747 return 0;
Jerry Yu89e103c2022-03-30 22:43:29 +08001748}
Valerio Settic9ae8622023-07-25 11:23:50 +02001749#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_EPHEMERAL_ENABLED */
XiaokangQian7807f9f2022-02-15 10:04:37 +00001750
Jerry Yu0c354a22022-08-29 15:25:36 +08001751/* RFC 8446 section 4.2
1752 *
1753 * If an implementation receives an extension which it recognizes and which is
1754 * not specified for the message in which it appears, it MUST abort the handshake
1755 * with an "illegal_parameter" alert.
1756 *
1757 */
Jerry Yuc4bf5d62022-10-29 09:08:47 +08001758int mbedtls_ssl_tls13_check_received_extension(
Gilles Peskine449bd832023-01-11 14:50:10 +01001759 mbedtls_ssl_context *ssl,
1760 int hs_msg_type,
1761 unsigned int received_extension_type,
1762 uint32_t hs_msg_allowed_extensions_mask)
Jerry Yu0c354a22022-08-29 15:25:36 +08001763{
Jerry Yudf0ad652022-10-31 13:20:57 +08001764 uint32_t extension_mask = mbedtls_ssl_get_extension_mask(
Gilles Peskine449bd832023-01-11 14:50:10 +01001765 received_extension_type);
Jerry Yu0c354a22022-08-29 15:25:36 +08001766
Jerry Yu79aa7212022-11-08 21:30:21 +08001767 MBEDTLS_SSL_PRINT_EXT(
Gilles Peskine449bd832023-01-11 14:50:10 +01001768 3, hs_msg_type, received_extension_type, "received");
Jerry Yu0c354a22022-08-29 15:25:36 +08001769
Gilles Peskine449bd832023-01-11 14:50:10 +01001770 if ((extension_mask & hs_msg_allowed_extensions_mask) == 0) {
Jerry Yu79aa7212022-11-08 21:30:21 +08001771 MBEDTLS_SSL_PRINT_EXT(
Gilles Peskine449bd832023-01-11 14:50:10 +01001772 3, hs_msg_type, received_extension_type, "is illegal");
Jerry Yu0c354a22022-08-29 15:25:36 +08001773 MBEDTLS_SSL_PEND_FATAL_ALERT(
1774 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER,
Gilles Peskine449bd832023-01-11 14:50:10 +01001775 MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER);
1776 return MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER;
Jerry Yu0c354a22022-08-29 15:25:36 +08001777 }
1778
1779 ssl->handshake->received_extensions |= extension_mask;
Jerry Yuc4bf5d62022-10-29 09:08:47 +08001780 /*
1781 * If it is a message containing extension responses, check that we
1782 * previously sent the extension.
1783 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001784 switch (hs_msg_type) {
Jerry Yu0c354a22022-08-29 15:25:36 +08001785 case MBEDTLS_SSL_HS_SERVER_HELLO:
Jerry Yudf0ad652022-10-31 13:20:57 +08001786 case MBEDTLS_SSL_TLS1_3_HS_HELLO_RETRY_REQUEST:
Jerry Yu0c354a22022-08-29 15:25:36 +08001787 case MBEDTLS_SSL_HS_ENCRYPTED_EXTENSIONS:
1788 case MBEDTLS_SSL_HS_CERTIFICATE:
Jerry Yuc4bf5d62022-10-29 09:08:47 +08001789 /* Check if the received extension is sent by peer message.*/
Gilles Peskine449bd832023-01-11 14:50:10 +01001790 if ((ssl->handshake->sent_extensions & extension_mask) != 0) {
1791 return 0;
1792 }
Jerry Yu0c354a22022-08-29 15:25:36 +08001793 break;
1794 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01001795 return 0;
Jerry Yu0c354a22022-08-29 15:25:36 +08001796 }
1797
Jerry Yu79aa7212022-11-08 21:30:21 +08001798 MBEDTLS_SSL_PRINT_EXT(
Gilles Peskine449bd832023-01-11 14:50:10 +01001799 3, hs_msg_type, received_extension_type, "is unsupported");
Jerry Yu0c354a22022-08-29 15:25:36 +08001800 MBEDTLS_SSL_PEND_FATAL_ALERT(
1801 MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_EXT,
Gilles Peskine449bd832023-01-11 14:50:10 +01001802 MBEDTLS_ERR_SSL_UNSUPPORTED_EXTENSION);
1803 return MBEDTLS_ERR_SSL_UNSUPPORTED_EXTENSION;
Jerry Yu0c354a22022-08-29 15:25:36 +08001804}
1805
Jan Bruckner151f6422023-02-10 12:45:19 +01001806#if defined(MBEDTLS_SSL_RECORD_SIZE_LIMIT)
Yanray Wangfaf70bd2023-12-07 10:03:32 +08001807
1808/* RFC 8449, section 4:
1809 *
Jan Bruckner151f6422023-02-10 12:45:19 +01001810 * The ExtensionData of the "record_size_limit" extension is
1811 * RecordSizeLimit:
1812 * uint16 RecordSizeLimit;
1813 */
1814MBEDTLS_CHECK_RETURN_CRITICAL
1815int mbedtls_ssl_tls13_parse_record_size_limit_ext(mbedtls_ssl_context *ssl,
1816 const unsigned char *buf,
1817 const unsigned char *end)
1818{
Jan Bruckner1a38e542023-03-15 14:15:11 +01001819 const unsigned char *p = buf;
1820 uint16_t record_size_limit;
Jan Brucknera0589e72023-03-15 11:04:45 +01001821 const size_t extension_data_len = end - buf;
Jan Bruckner1a38e542023-03-15 14:15:11 +01001822
Xiaokang Qian73437382023-03-29 08:24:12 +00001823 if (extension_data_len !=
1824 MBEDTLS_SSL_RECORD_SIZE_LIMIT_EXTENSION_DATA_LENGTH) {
Jan Bruckner151f6422023-02-10 12:45:19 +01001825 MBEDTLS_SSL_DEBUG_MSG(2,
Jan Bruckner1a38e542023-03-15 14:15:11 +01001826 ("record_size_limit extension has invalid length: %"
1827 MBEDTLS_PRINTF_SIZET " Bytes",
Jan Bruckner151f6422023-02-10 12:45:19 +01001828 extension_data_len));
1829
1830 MBEDTLS_SSL_PEND_FATAL_ALERT(
1831 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER,
1832 MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER);
1833 return MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER;
1834 }
1835
Jan Bruckner151f6422023-02-10 12:45:19 +01001836 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, 2);
1837 record_size_limit = MBEDTLS_GET_UINT16_BE(p, 0);
1838
1839 MBEDTLS_SSL_DEBUG_MSG(2, ("RecordSizeLimit: %u Bytes", record_size_limit));
1840
Waleed Elmelegye1ac98d2024-01-05 18:10:12 +00001841 /* RFC 8449, section 4:
1842 *
1843 * Endpoints MUST NOT send a "record_size_limit" extension with a value
1844 * smaller than 64. An endpoint MUST treat receipt of a smaller value
1845 * as a fatal error and generate an "illegal_parameter" alert.
1846 */
1847 if (record_size_limit < MBEDTLS_SSL_RECORD_SIZE_LIMIT_MIN) {
1848 MBEDTLS_SSL_DEBUG_MSG(1, ("Invalid record size limit : %u Bytes",
1849 record_size_limit));
1850 MBEDTLS_SSL_PEND_FATAL_ALERT(
1851 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER,
1852 MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER);
1853 return MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER;
Jan Bruckner151f6422023-02-10 12:45:19 +01001854 }
1855
Jan Brucknerf482dcc2023-03-15 09:09:06 +01001856 ssl->session_negotiate->record_size_limit = record_size_limit;
Jan Bruckner151f6422023-02-10 12:45:19 +01001857
Jan Brucknerf482dcc2023-03-15 09:09:06 +01001858 return 0;
Jan Bruckner151f6422023-02-10 12:45:19 +01001859}
Jan Brucknerf482dcc2023-03-15 09:09:06 +01001860
Yanray Wangfaf70bd2023-12-07 10:03:32 +08001861MBEDTLS_CHECK_RETURN_CRITICAL
1862int mbedtls_ssl_tls13_write_record_size_limit_ext(mbedtls_ssl_context *ssl,
Yanray Wangfaf70bd2023-12-07 10:03:32 +08001863 unsigned char *buf,
1864 const unsigned char *end,
1865 size_t *out_len)
1866{
Yanray Wangfaf70bd2023-12-07 10:03:32 +08001867 unsigned char *p = buf;
1868 *out_len = 0;
1869
Waleed Elmelegy148dfb62024-01-04 18:02:35 +00001870 MBEDTLS_STATIC_ASSERT(MBEDTLS_SSL_IN_CONTENT_LEN >= MBEDTLS_SSL_RECORD_SIZE_LIMIT_MIN,
Waleed Elmelegye1ac98d2024-01-05 18:10:12 +00001871 "MBEDTLS_SSL_IN_CONTENT_LEN is less than the "
1872 "minimum record size limit");
Yanray Wangfaf70bd2023-12-07 10:03:32 +08001873
1874 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 6);
1875
1876 MBEDTLS_PUT_UINT16_BE(MBEDTLS_TLS_EXT_RECORD_SIZE_LIMIT, p, 0);
Waleed Elmelegye1ac98d2024-01-05 18:10:12 +00001877 MBEDTLS_PUT_UINT16_BE(MBEDTLS_SSL_RECORD_SIZE_LIMIT_EXTENSION_DATA_LENGTH,
1878 p, 2);
Waleed Elmelegy148dfb62024-01-04 18:02:35 +00001879 MBEDTLS_PUT_UINT16_BE(MBEDTLS_SSL_IN_CONTENT_LEN, p, 4);
Yanray Wangfaf70bd2023-12-07 10:03:32 +08001880
1881 *out_len = 6;
1882
Waleed Elmelegy3ff47242024-01-10 16:15:52 +00001883 MBEDTLS_SSL_DEBUG_MSG(2, ("Sent RecordSizeLimit: %d Bytes",
Waleed Elmelegye1ac98d2024-01-05 18:10:12 +00001884 MBEDTLS_SSL_IN_CONTENT_LEN));
Yanray Wangfaf70bd2023-12-07 10:03:32 +08001885
1886 mbedtls_ssl_tls13_set_hs_sent_ext_mask(ssl, MBEDTLS_TLS_EXT_RECORD_SIZE_LIMIT);
1887
1888 return 0;
1889}
1890
Jan Bruckner151f6422023-02-10 12:45:19 +01001891#endif /* MBEDTLS_SSL_RECORD_SIZE_LIMIT */
1892
Jerry Yufb4b6472022-01-27 15:03:26 +08001893#endif /* MBEDTLS_SSL_TLS_C && MBEDTLS_SSL_PROTO_TLS1_3 */