blob: 0e7aa3a74e29c0ff200bcfaf3a26a5fa7883e782 [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
5 * SPDX-License-Identifier: Apache-2.0
6 *
7 * Licensed under the Apache License, Version 2.0 (the "License"); you may
8 * not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
15 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 */
19
20#include "common.h"
21
Jerry Yufb4b6472022-01-27 15:03:26 +080022#if defined(MBEDTLS_SSL_TLS_C) && defined(MBEDTLS_SSL_PROTO_TLS1_3)
Jerry Yu65dd2cc2021-08-18 16:38:40 +080023
Jerry Yu30b071c2021-09-12 20:16:03 +080024#include <string.h>
25
Jerry Yuc8a392c2021-08-18 16:46:28 +080026#include "mbedtls/error.h"
Jerry Yu75336352021-09-01 15:59:36 +080027#include "mbedtls/debug.h"
Jerry Yu30b071c2021-09-12 20:16:03 +080028#include "mbedtls/oid.h"
29#include "mbedtls/platform.h"
Gabor Mezei685472b2021-11-24 11:17:36 +010030#include "mbedtls/constant_time.h"
Jerry Yu141bbe72022-12-01 20:30:41 +080031#include "psa/crypto.h"
32#include "mbedtls/psa_util.h"
Jerry Yuc8a392c2021-08-18 16:46:28 +080033
Jerry Yu65dd2cc2021-08-18 16:38:40 +080034#include "ssl_misc.h"
Ronald Crone3dac4a2022-06-10 17:21:51 +020035#include "ssl_tls13_invasive.h"
Jerry Yu30b071c2021-09-12 20:16:03 +080036#include "ssl_tls13_keys.h"
Jerry Yu67eced02022-02-25 13:37:36 +080037#include "ssl_debug_helpers.h"
Jerry Yu65dd2cc2021-08-18 16:38:40 +080038
Andrzej Kurek8a045ce2022-12-23 11:00:06 -050039#include "psa/crypto.h"
40#include "mbedtls/psa_util.h"
41
42#define PSA_TO_MBEDTLS_ERR(status) PSA_TO_MBEDTLS_ERR_LIST(status, \
43 psa_to_ssl_errors, \
44 psa_generic_status_to_mbedtls)
45
Jerry Yufbe3e642022-04-25 19:31:51 +080046const uint8_t mbedtls_ssl_tls13_hello_retry_request_magic[
Gilles Peskine449bd832023-01-11 14:50:10 +010047 MBEDTLS_SERVER_HELLO_RANDOM_LEN] =
48{ 0xCF, 0x21, 0xAD, 0x74, 0xE5, 0x9A, 0x61, 0x11,
49 0xBE, 0x1D, 0x8C, 0x02, 0x1E, 0x65, 0xB8, 0x91,
50 0xC2, 0xA2, 0x11, 0x16, 0x7A, 0xBB, 0x8C, 0x5E,
51 0x07, 0x9E, 0x09, 0xE2, 0xC8, 0xA8, 0x33, 0x9C };
Jerry Yu93a13f22022-04-11 23:00:01 +080052
Gilles Peskine449bd832023-01-11 14:50:10 +010053int mbedtls_ssl_tls13_fetch_handshake_msg(mbedtls_ssl_context *ssl,
54 unsigned hs_type,
55 unsigned char **buf,
56 size_t *buf_len)
XiaokangQian6b226b02021-09-24 07:51:16 +000057{
58 int ret;
59
Gilles Peskine449bd832023-01-11 14:50:10 +010060 if ((ret = mbedtls_ssl_read_record(ssl, 0)) != 0) {
61 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_read_record", ret);
XiaokangQian6b226b02021-09-24 07:51:16 +000062 goto cleanup;
63 }
64
Gilles Peskine449bd832023-01-11 14:50:10 +010065 if (ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE ||
66 ssl->in_msg[0] != hs_type) {
67 MBEDTLS_SSL_DEBUG_MSG(1, ("Receive unexpected handshake message."));
68 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE,
69 MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE);
XiaokangQian6b226b02021-09-24 07:51:16 +000070 ret = MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE;
71 goto cleanup;
72 }
73
XiaokangQian05420b12021-09-29 08:46:37 +000074 /*
75 * Jump handshake header (4 bytes, see Section 4 of RFC 8446).
76 * ...
77 * HandshakeType msg_type;
78 * uint24 length;
79 * ...
80 */
Xiaofei Baieef15042021-11-18 07:29:56 +000081 *buf = ssl->in_msg + 4;
82 *buf_len = ssl->in_hslen - 4;
XiaokangQian6b226b02021-09-24 07:51:16 +000083
XiaokangQian6b226b02021-09-24 07:51:16 +000084cleanup:
85
Gilles Peskine449bd832023-01-11 14:50:10 +010086 return ret;
XiaokangQian6b226b02021-09-24 07:51:16 +000087}
88
Ronald Cron928cbd32022-10-04 16:14:26 +020089#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED)
Jerry Yu30b071c2021-09-12 20:16:03 +080090/*
Jerry Yu30b071c2021-09-12 20:16:03 +080091 * STATE HANDLING: Read CertificateVerify
92 */
Jerry Yud0fc5852021-10-29 11:09:06 +080093/* Macro to express the maximum length of the verify structure.
Jerry Yu30b071c2021-09-12 20:16:03 +080094 *
95 * The structure is computed per TLS 1.3 specification as:
96 * - 64 bytes of octet 32,
97 * - 33 bytes for the context string
98 * (which is either "TLS 1.3, client CertificateVerify"
99 * or "TLS 1.3, server CertificateVerify"),
Jerry Yud0fc5852021-10-29 11:09:06 +0800100 * - 1 byte for the octet 0x0, which serves as a separator,
Jerry Yu30b071c2021-09-12 20:16:03 +0800101 * - 32 or 48 bytes for the Transcript-Hash(Handshake Context, Certificate)
102 * (depending on the size of the transcript_hash)
103 *
104 * This results in a total size of
105 * - 130 bytes for a SHA256-based transcript hash, or
106 * (64 + 33 + 1 + 32 bytes)
107 * - 146 bytes for a SHA384-based transcript hash.
108 * (64 + 33 + 1 + 48 bytes)
109 *
110 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100111#define SSL_VERIFY_STRUCT_MAX_SIZE (64 + \
112 33 + \
113 1 + \
114 MBEDTLS_TLS1_3_MD_MAX_SIZE \
115 )
Jerry Yu30b071c2021-09-12 20:16:03 +0800116
Jerry Yu0b32c502021-10-28 13:41:59 +0800117/*
118 * The ssl_tls13_create_verify_structure() creates the verify structure.
119 * As input, it requires the transcript hash.
120 *
121 * The caller has to ensure that the buffer has size at least
122 * SSL_VERIFY_STRUCT_MAX_SIZE bytes.
123 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100124static void ssl_tls13_create_verify_structure(const unsigned char *transcript_hash,
125 size_t transcript_hash_len,
126 unsigned char *verify_buffer,
127 size_t *verify_buffer_len,
128 int from)
Jerry Yu0b32c502021-10-28 13:41:59 +0800129{
130 size_t idx;
Jerry Yu30b071c2021-09-12 20:16:03 +0800131
Jerry Yu0b32c502021-10-28 13:41:59 +0800132 /* RFC 8446, Section 4.4.3:
133 *
134 * The digital signature [in the CertificateVerify message] is then
135 * computed over the concatenation of:
136 * - A string that consists of octet 32 (0x20) repeated 64 times
137 * - The context string
138 * - A single 0 byte which serves as the separator
139 * - The content to be signed
140 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100141 memset(verify_buffer, 0x20, 64);
Jerry Yu0b32c502021-10-28 13:41:59 +0800142 idx = 64;
143
Gilles Peskine449bd832023-01-11 14:50:10 +0100144 if (from == MBEDTLS_SSL_IS_CLIENT) {
145 memcpy(verify_buffer + idx, MBEDTLS_SSL_TLS1_3_LBL_WITH_LEN(client_cv));
146 idx += MBEDTLS_SSL_TLS1_3_LBL_LEN(client_cv);
147 } else { /* from == MBEDTLS_SSL_IS_SERVER */
148 memcpy(verify_buffer + idx, MBEDTLS_SSL_TLS1_3_LBL_WITH_LEN(server_cv));
149 idx += MBEDTLS_SSL_TLS1_3_LBL_LEN(server_cv);
Jerry Yu0b32c502021-10-28 13:41:59 +0800150 }
151
152 verify_buffer[idx++] = 0x0;
153
Gilles Peskine449bd832023-01-11 14:50:10 +0100154 memcpy(verify_buffer + idx, transcript_hash, transcript_hash_len);
Jerry Yu0b32c502021-10-28 13:41:59 +0800155 idx += transcript_hash_len;
156
157 *verify_buffer_len = idx;
158}
159
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200160MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100161static int ssl_tls13_parse_certificate_verify(mbedtls_ssl_context *ssl,
162 const unsigned char *buf,
163 const unsigned char *end,
164 const unsigned char *verify_buffer,
165 size_t verify_buffer_len)
Jerry Yu30b071c2021-09-12 20:16:03 +0800166{
167 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
pespaceka1378102022-04-26 15:03:11 +0200168 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Jerry Yu30b071c2021-09-12 20:16:03 +0800169 const unsigned char *p = buf;
170 uint16_t algorithm;
Jerry Yu30b071c2021-09-12 20:16:03 +0800171 size_t signature_len;
172 mbedtls_pk_type_t sig_alg;
173 mbedtls_md_type_t md_alg;
pespaceka1378102022-04-26 15:03:11 +0200174 psa_algorithm_t hash_alg = PSA_ALG_NONE;
175 unsigned char verify_hash[PSA_HASH_MAX_SIZE];
Jerry Yu30b071c2021-09-12 20:16:03 +0800176 size_t verify_hash_len;
177
Xiaofei Baid25fab62021-12-02 06:36:27 +0000178 void const *options = NULL;
XiaokangQian82d34cc2021-11-03 08:51:56 +0000179#if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT)
Xiaofei Baid25fab62021-12-02 06:36:27 +0000180 mbedtls_pk_rsassa_pss_options rsassa_pss_options;
XiaokangQian82d34cc2021-11-03 08:51:56 +0000181#endif /* MBEDTLS_X509_RSASSA_PSS_SUPPORT */
182
Jerry Yu30b071c2021-09-12 20:16:03 +0800183 /*
184 * struct {
185 * SignatureScheme algorithm;
186 * opaque signature<0..2^16-1>;
187 * } CertificateVerify;
188 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100189 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, 2);
190 algorithm = MBEDTLS_GET_UINT16_BE(p, 0);
Jerry Yu30b071c2021-09-12 20:16:03 +0800191 p += 2;
192
193 /* RFC 8446 section 4.4.3
194 *
195 * If the CertificateVerify message is sent by a server, the signature algorithm
196 * MUST be one offered in the client's "signature_algorithms" extension unless
197 * no valid certificate chain can be produced without unsupported algorithms
198 *
199 * RFC 8446 section 4.4.2.2
200 *
201 * If the client cannot construct an acceptable chain using the provided
202 * certificates and decides to abort the handshake, then it MUST abort the handshake
203 * with an appropriate certificate-related alert (by default, "unsupported_certificate").
204 *
Jerry Yu6f87f252021-10-29 20:12:51 +0800205 * Check if algorithm is an offered signature algorithm.
Jerry Yu30b071c2021-09-12 20:16:03 +0800206 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100207 if (!mbedtls_ssl_sig_alg_is_offered(ssl, algorithm)) {
Jerry Yu982d9e52021-10-14 15:59:37 +0800208 /* algorithm not in offered signature algorithms list */
Gilles Peskine449bd832023-01-11 14:50:10 +0100209 MBEDTLS_SSL_DEBUG_MSG(1, ("Received signature algorithm(%04x) is not "
210 "offered.",
211 (unsigned int) algorithm));
Jerry Yu6f87f252021-10-29 20:12:51 +0800212 goto error;
Jerry Yu30b071c2021-09-12 20:16:03 +0800213 }
214
Gilles Peskine449bd832023-01-11 14:50:10 +0100215 if (mbedtls_ssl_get_pk_type_and_md_alg_from_sig_alg(
216 algorithm, &sig_alg, &md_alg) != 0) {
Jerry Yu8c338862022-03-23 13:34:04 +0800217 goto error;
Jerry Yu30b071c2021-09-12 20:16:03 +0800218 }
219
Gilles Peskine449bd832023-01-11 14:50:10 +0100220 hash_alg = mbedtls_hash_info_psa_from_md(md_alg);
221 if (hash_alg == 0) {
pespaceka1378102022-04-26 15:03:11 +0200222 goto error;
223 }
224
Gilles Peskine449bd832023-01-11 14:50:10 +0100225 MBEDTLS_SSL_DEBUG_MSG(3, ("Certificate Verify: Signature algorithm ( %04x )",
226 (unsigned int) algorithm));
Jerry Yu30b071c2021-09-12 20:16:03 +0800227
228 /*
229 * Check the certificate's key type matches the signature alg
230 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100231 if (!mbedtls_pk_can_do(&ssl->session_negotiate->peer_cert->pk, sig_alg)) {
232 MBEDTLS_SSL_DEBUG_MSG(1, ("signature algorithm doesn't match cert key"));
Jerry Yu6f87f252021-10-29 20:12:51 +0800233 goto error;
Jerry Yu30b071c2021-09-12 20:16:03 +0800234 }
235
Gilles Peskine449bd832023-01-11 14:50:10 +0100236 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, 2);
237 signature_len = MBEDTLS_GET_UINT16_BE(p, 0);
Jerry Yu30b071c2021-09-12 20:16:03 +0800238 p += 2;
Gilles Peskine449bd832023-01-11 14:50:10 +0100239 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, signature_len);
Jerry Yu30b071c2021-09-12 20:16:03 +0800240
Gilles Peskine449bd832023-01-11 14:50:10 +0100241 status = psa_hash_compute(hash_alg,
242 verify_buffer,
243 verify_buffer_len,
244 verify_hash,
245 sizeof(verify_hash),
246 &verify_hash_len);
247 if (status != PSA_SUCCESS) {
248 MBEDTLS_SSL_DEBUG_RET(1, "hash computation PSA error", status);
Jerry Yu6f87f252021-10-29 20:12:51 +0800249 goto error;
Jerry Yu133690c2021-10-25 14:01:13 +0800250 }
251
Gilles Peskine449bd832023-01-11 14:50:10 +0100252 MBEDTLS_SSL_DEBUG_BUF(3, "verify hash", verify_hash, verify_hash_len);
XiaokangQian82d34cc2021-11-03 08:51:56 +0000253#if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT)
Gilles Peskine449bd832023-01-11 14:50:10 +0100254 if (sig_alg == MBEDTLS_PK_RSASSA_PSS) {
Xiaofei Baid25fab62021-12-02 06:36:27 +0000255 rsassa_pss_options.mgf1_hash_id = md_alg;
Przemek Stekiel6a5e0182022-06-27 11:53:13 +0200256
Gilles Peskine449bd832023-01-11 14:50:10 +0100257 rsassa_pss_options.expected_salt_len = PSA_HASH_LENGTH(hash_alg);
258 options = (const void *) &rsassa_pss_options;
XiaokangQian82d34cc2021-11-03 08:51:56 +0000259 }
260#endif /* MBEDTLS_X509_RSASSA_PSS_SUPPORT */
Jerry Yu30b071c2021-09-12 20:16:03 +0800261
Gilles Peskine449bd832023-01-11 14:50:10 +0100262 if ((ret = mbedtls_pk_verify_ext(sig_alg, options,
263 &ssl->session_negotiate->peer_cert->pk,
264 md_alg, verify_hash, verify_hash_len,
265 p, signature_len)) == 0) {
266 return 0;
Jerry Yu30b071c2021-09-12 20:16:03 +0800267 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100268 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_pk_verify_ext", ret);
Jerry Yu30b071c2021-09-12 20:16:03 +0800269
Jerry Yu6f87f252021-10-29 20:12:51 +0800270error:
271 /* RFC 8446 section 4.4.3
272 *
273 * If the verification fails, the receiver MUST terminate the handshake
274 * with a "decrypt_error" alert.
Gilles Peskine449bd832023-01-11 14:50:10 +0100275 */
276 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_DECRYPT_ERROR,
277 MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE);
278 return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
Jerry Yu6f87f252021-10-29 20:12:51 +0800279
Jerry Yu30b071c2021-09-12 20:16:03 +0800280}
Ronald Cron928cbd32022-10-04 16:14:26 +0200281#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED */
Jerry Yu30b071c2021-09-12 20:16:03 +0800282
Gilles Peskine449bd832023-01-11 14:50:10 +0100283int mbedtls_ssl_tls13_process_certificate_verify(mbedtls_ssl_context *ssl)
Jerry Yu30b071c2021-09-12 20:16:03 +0800284{
Jerry Yu30b071c2021-09-12 20:16:03 +0800285
Ronald Cron928cbd32022-10-04 16:14:26 +0200286#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED)
Jerry Yuda8cdf22021-10-25 15:06:49 +0800287 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
288 unsigned char verify_buffer[SSL_VERIFY_STRUCT_MAX_SIZE];
289 size_t verify_buffer_len;
290 unsigned char transcript[MBEDTLS_TLS1_3_MD_MAX_SIZE];
291 size_t transcript_len;
292 unsigned char *buf;
293 size_t buf_len;
294
Gilles Peskine449bd832023-01-11 14:50:10 +0100295 MBEDTLS_SSL_DEBUG_MSG(2, ("=> parse certificate verify"));
Jerry Yu30b071c2021-09-12 20:16:03 +0800296
Jerry Yuda8cdf22021-10-25 15:06:49 +0800297 MBEDTLS_SSL_PROC_CHK(
Gilles Peskine449bd832023-01-11 14:50:10 +0100298 mbedtls_ssl_tls13_fetch_handshake_msg(ssl,
299 MBEDTLS_SSL_HS_CERTIFICATE_VERIFY, &buf, &buf_len));
Jerry Yu30b071c2021-09-12 20:16:03 +0800300
Jerry Yuda8cdf22021-10-25 15:06:49 +0800301 /* Need to calculate the hash of the transcript first
Jerry Yu0b32c502021-10-28 13:41:59 +0800302 * before reading the message since otherwise it gets
303 * included in the transcript
304 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100305 ret = mbedtls_ssl_get_handshake_transcript(ssl,
306 ssl->handshake->ciphersuite_info->mac,
307 transcript, sizeof(transcript),
308 &transcript_len);
309 if (ret != 0) {
Jerry Yuda8cdf22021-10-25 15:06:49 +0800310 MBEDTLS_SSL_PEND_FATAL_ALERT(
311 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR,
Gilles Peskine449bd832023-01-11 14:50:10 +0100312 MBEDTLS_ERR_SSL_INTERNAL_ERROR);
313 return ret;
Jerry Yu30b071c2021-09-12 20:16:03 +0800314 }
315
Gilles Peskine449bd832023-01-11 14:50:10 +0100316 MBEDTLS_SSL_DEBUG_BUF(3, "handshake hash", transcript, transcript_len);
Jerry Yuda8cdf22021-10-25 15:06:49 +0800317
318 /* Create verify structure */
Gilles Peskine449bd832023-01-11 14:50:10 +0100319 ssl_tls13_create_verify_structure(transcript,
320 transcript_len,
321 verify_buffer,
322 &verify_buffer_len,
323 (ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT) ?
324 MBEDTLS_SSL_IS_SERVER :
325 MBEDTLS_SSL_IS_CLIENT);
Jerry Yuda8cdf22021-10-25 15:06:49 +0800326
327 /* Process the message contents */
Gilles Peskine449bd832023-01-11 14:50:10 +0100328 MBEDTLS_SSL_PROC_CHK(ssl_tls13_parse_certificate_verify(ssl, buf,
329 buf + buf_len, verify_buffer,
330 verify_buffer_len));
Jerry Yuda8cdf22021-10-25 15:06:49 +0800331
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +0100332 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_add_hs_msg_to_checksum(ssl,
Manuel Pégourié-Gonnard43cc1272023-02-06 11:48:19 +0100333 MBEDTLS_SSL_HS_CERTIFICATE_VERIFY,
334 buf, buf_len));
Jerry Yu30b071c2021-09-12 20:16:03 +0800335
336cleanup:
337
Gilles Peskine449bd832023-01-11 14:50:10 +0100338 MBEDTLS_SSL_DEBUG_MSG(2, ("<= parse certificate verify"));
339 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_tls13_process_certificate_verify", ret);
340 return ret;
Jerry Yuda8cdf22021-10-25 15:06:49 +0800341#else
342 ((void) ssl);
Gilles Peskine449bd832023-01-11 14:50:10 +0100343 MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
344 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Ronald Cron928cbd32022-10-04 16:14:26 +0200345#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED */
Jerry Yu30b071c2021-09-12 20:16:03 +0800346}
347
348/*
Xiaofei Bai947571e2021-09-29 09:12:03 +0000349 *
XiaokangQian6b916b12022-04-25 07:29:34 +0000350 * STATE HANDLING: Incoming Certificate.
Xiaofei Bai947571e2021-09-29 09:12:03 +0000351 *
352 */
353
Ronald Cronde08cf32022-10-04 17:15:35 +0200354#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED)
Xiaofei Bai947571e2021-09-29 09:12:03 +0000355#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
356/*
357 * Structure of Certificate message:
358 *
359 * enum {
360 * X509(0),
361 * RawPublicKey(2),
362 * (255)
363 * } CertificateType;
364 *
365 * struct {
366 * select (certificate_type) {
367 * case RawPublicKey:
368 * * From RFC 7250 ASN.1_subjectPublicKeyInfo *
369 * opaque ASN1_subjectPublicKeyInfo<1..2^24-1>;
370 * case X509:
371 * opaque cert_data<1..2^24-1>;
372 * };
373 * Extension extensions<0..2^16-1>;
374 * } CertificateEntry;
375 *
376 * struct {
377 * opaque certificate_request_context<0..2^8-1>;
378 * CertificateEntry certificate_list<0..2^24-1>;
379 * } Certificate;
380 *
381 */
Xiaofei Bai79595ac2021-10-26 07:16:45 +0000382
383/* Parse certificate chain send by the server. */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200384MBEDTLS_CHECK_RETURN_CRITICAL
Ronald Crone3dac4a2022-06-10 17:21:51 +0200385MBEDTLS_STATIC_TESTABLE
Gilles Peskine449bd832023-01-11 14:50:10 +0100386int mbedtls_ssl_tls13_parse_certificate(mbedtls_ssl_context *ssl,
387 const unsigned char *buf,
388 const unsigned char *end)
Xiaofei Bai947571e2021-09-29 09:12:03 +0000389{
390 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
391 size_t certificate_request_context_len = 0;
392 size_t certificate_list_len = 0;
393 const unsigned char *p = buf;
394 const unsigned char *certificate_list_end;
Jerry Yuc4bf5d62022-10-29 09:08:47 +0800395 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
Xiaofei Bai947571e2021-09-29 09:12:03 +0000396
Gilles Peskine449bd832023-01-11 14:50:10 +0100397 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, 4);
Xiaofei Bai947571e2021-09-29 09:12:03 +0000398 certificate_request_context_len = p[0];
Gilles Peskine449bd832023-01-11 14:50:10 +0100399 certificate_list_len = MBEDTLS_GET_UINT24_BE(p, 1);
XiaokangQian63e713e2022-05-15 04:26:57 +0000400 p += 4;
Xiaofei Bai947571e2021-09-29 09:12:03 +0000401
402 /* In theory, the certificate list can be up to 2^24 Bytes, but we don't
403 * support anything beyond 2^16 = 64K.
404 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100405 if ((certificate_request_context_len != 0) ||
406 (certificate_list_len >= 0x10000)) {
407 MBEDTLS_SSL_DEBUG_MSG(1, ("bad certificate message"));
408 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR,
409 MBEDTLS_ERR_SSL_DECODE_ERROR);
410 return MBEDTLS_ERR_SSL_DECODE_ERROR;
Xiaofei Bai947571e2021-09-29 09:12:03 +0000411 }
412
413 /* In case we tried to reuse a session but it failed */
Gilles Peskine449bd832023-01-11 14:50:10 +0100414 if (ssl->session_negotiate->peer_cert != NULL) {
415 mbedtls_x509_crt_free(ssl->session_negotiate->peer_cert);
416 mbedtls_free(ssl->session_negotiate->peer_cert);
Xiaofei Bai947571e2021-09-29 09:12:03 +0000417 }
418
Gilles Peskine449bd832023-01-11 14:50:10 +0100419 if (certificate_list_len == 0) {
XiaokangQianc3017f62022-05-13 05:55:41 +0000420 ssl->session_negotiate->peer_cert = NULL;
421 ret = 0;
422 goto exit;
423 }
424
Gilles Peskine449bd832023-01-11 14:50:10 +0100425 if ((ssl->session_negotiate->peer_cert =
426 mbedtls_calloc(1, sizeof(mbedtls_x509_crt))) == NULL) {
427 MBEDTLS_SSL_DEBUG_MSG(1, ("alloc( %" MBEDTLS_PRINTF_SIZET " bytes ) failed",
428 sizeof(mbedtls_x509_crt)));
429 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR,
430 MBEDTLS_ERR_SSL_ALLOC_FAILED);
431 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
Xiaofei Bai947571e2021-09-29 09:12:03 +0000432 }
433
Gilles Peskine449bd832023-01-11 14:50:10 +0100434 mbedtls_x509_crt_init(ssl->session_negotiate->peer_cert);
Xiaofei Bai947571e2021-09-29 09:12:03 +0000435
Gilles Peskine449bd832023-01-11 14:50:10 +0100436 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, certificate_list_len);
Xiaofei Bai947571e2021-09-29 09:12:03 +0000437 certificate_list_end = p + certificate_list_len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100438 while (p < certificate_list_end) {
Xiaofei Bai947571e2021-09-29 09:12:03 +0000439 size_t cert_data_len, extensions_len;
Jerry Yu2eaa7602022-08-04 17:28:15 +0800440 const unsigned char *extensions_end;
Xiaofei Bai947571e2021-09-29 09:12:03 +0000441
Gilles Peskine449bd832023-01-11 14:50:10 +0100442 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, certificate_list_end, 3);
443 cert_data_len = MBEDTLS_GET_UINT24_BE(p, 0);
Xiaofei Bai947571e2021-09-29 09:12:03 +0000444 p += 3;
445
446 /* In theory, the CRT can be up to 2^24 Bytes, but we don't support
447 * anything beyond 2^16 = 64K. Otherwise as in the TLS 1.2 code,
448 * check that we have a minimum of 128 bytes of data, this is not
449 * clear why we need that though.
450 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100451 if ((cert_data_len < 128) || (cert_data_len >= 0x10000)) {
452 MBEDTLS_SSL_DEBUG_MSG(1, ("bad Certificate message"));
453 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR,
454 MBEDTLS_ERR_SSL_DECODE_ERROR);
455 return MBEDTLS_ERR_SSL_DECODE_ERROR;
Xiaofei Bai947571e2021-09-29 09:12:03 +0000456 }
457
Gilles Peskine449bd832023-01-11 14:50:10 +0100458 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, certificate_list_end, cert_data_len);
459 ret = mbedtls_x509_crt_parse_der(ssl->session_negotiate->peer_cert,
460 p, cert_data_len);
Xiaofei Bai947571e2021-09-29 09:12:03 +0000461
Gilles Peskine449bd832023-01-11 14:50:10 +0100462 switch (ret) {
Xiaofei Bai947571e2021-09-29 09:12:03 +0000463 case 0: /*ok*/
464 break;
465 case MBEDTLS_ERR_X509_UNKNOWN_SIG_ALG + MBEDTLS_ERR_OID_NOT_FOUND:
466 /* Ignore certificate with an unknown algorithm: maybe a
467 prior certificate was already trusted. */
468 break;
469
470 case MBEDTLS_ERR_X509_ALLOC_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100471 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR,
472 MBEDTLS_ERR_X509_ALLOC_FAILED);
473 MBEDTLS_SSL_DEBUG_RET(1, " mbedtls_x509_crt_parse_der", ret);
474 return ret;
Xiaofei Bai947571e2021-09-29 09:12:03 +0000475
476 case MBEDTLS_ERR_X509_UNKNOWN_VERSION:
Gilles Peskine449bd832023-01-11 14:50:10 +0100477 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT,
478 MBEDTLS_ERR_X509_UNKNOWN_VERSION);
479 MBEDTLS_SSL_DEBUG_RET(1, " mbedtls_x509_crt_parse_der", ret);
480 return ret;
Xiaofei Bai947571e2021-09-29 09:12:03 +0000481
482 default:
Gilles Peskine449bd832023-01-11 14:50:10 +0100483 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_BAD_CERT,
484 ret);
485 MBEDTLS_SSL_DEBUG_RET(1, " mbedtls_x509_crt_parse_der", ret);
486 return ret;
Xiaofei Bai947571e2021-09-29 09:12:03 +0000487 }
488
489 p += cert_data_len;
490
491 /* Certificate extensions length */
Gilles Peskine449bd832023-01-11 14:50:10 +0100492 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, certificate_list_end, 2);
493 extensions_len = MBEDTLS_GET_UINT16_BE(p, 0);
Xiaofei Bai947571e2021-09-29 09:12:03 +0000494 p += 2;
Gilles Peskine449bd832023-01-11 14:50:10 +0100495 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, certificate_list_end, extensions_len);
Jerry Yu2eaa7602022-08-04 17:28:15 +0800496
497 extensions_end = p + extensions_len;
Jerry Yu0d5cfb72022-10-31 14:15:48 +0800498 handshake->received_extensions = MBEDTLS_SSL_EXT_MASK_NONE;
Jerry Yu2eaa7602022-08-04 17:28:15 +0800499
Gilles Peskine449bd832023-01-11 14:50:10 +0100500 while (p < extensions_end) {
Jerry Yu2eaa7602022-08-04 17:28:15 +0800501 unsigned int extension_type;
502 size_t extension_data_len;
503
504 /*
Gilles Peskine449bd832023-01-11 14:50:10 +0100505 * struct {
506 * ExtensionType extension_type; (2 bytes)
507 * opaque extension_data<0..2^16-1>;
508 * } Extension;
509 */
510 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, extensions_end, 4);
511 extension_type = MBEDTLS_GET_UINT16_BE(p, 0);
512 extension_data_len = MBEDTLS_GET_UINT16_BE(p, 2);
Jerry Yu2eaa7602022-08-04 17:28:15 +0800513 p += 4;
514
Gilles Peskine449bd832023-01-11 14:50:10 +0100515 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, extensions_end, extension_data_len);
Jerry Yu2eaa7602022-08-04 17:28:15 +0800516
Jerry Yuc4bf5d62022-10-29 09:08:47 +0800517 ret = mbedtls_ssl_tls13_check_received_extension(
Gilles Peskine449bd832023-01-11 14:50:10 +0100518 ssl, MBEDTLS_SSL_HS_CERTIFICATE, extension_type,
519 MBEDTLS_SSL_TLS1_3_ALLOWED_EXTS_OF_CT);
520 if (ret != 0) {
521 return ret;
522 }
Jerry Yu0c354a22022-08-29 15:25:36 +0800523
Gilles Peskine449bd832023-01-11 14:50:10 +0100524 switch (extension_type) {
Jerry Yu2eaa7602022-08-04 17:28:15 +0800525 default:
Jerry Yu79aa7212022-11-08 21:30:21 +0800526 MBEDTLS_SSL_PRINT_EXT(
Jerry Yu0d5cfb72022-10-31 14:15:48 +0800527 3, MBEDTLS_SSL_HS_CERTIFICATE,
Gilles Peskine449bd832023-01-11 14:50:10 +0100528 extension_type, "( ignored )");
Jerry Yu2eaa7602022-08-04 17:28:15 +0800529 break;
530 }
531
532 p += extension_data_len;
533 }
534
Gilles Peskine449bd832023-01-11 14:50:10 +0100535 MBEDTLS_SSL_PRINT_EXTS(3, MBEDTLS_SSL_HS_CERTIFICATE,
536 handshake->received_extensions);
Xiaofei Bai947571e2021-09-29 09:12:03 +0000537 }
538
XiaokangQian63e713e2022-05-15 04:26:57 +0000539exit:
Xiaofei Bai947571e2021-09-29 09:12:03 +0000540 /* Check that all the message is consumed. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100541 if (p != end) {
542 MBEDTLS_SSL_DEBUG_MSG(1, ("bad Certificate message"));
543 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR,
544 MBEDTLS_ERR_SSL_DECODE_ERROR);
545 return MBEDTLS_ERR_SSL_DECODE_ERROR;
Xiaofei Bai947571e2021-09-29 09:12:03 +0000546 }
547
Gilles Peskine449bd832023-01-11 14:50:10 +0100548 MBEDTLS_SSL_DEBUG_CRT(3, "peer certificate", ssl->session_negotiate->peer_cert);
Xiaofei Bai947571e2021-09-29 09:12:03 +0000549
Gilles Peskine449bd832023-01-11 14:50:10 +0100550 return ret;
Xiaofei Bai947571e2021-09-29 09:12:03 +0000551}
552#else
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200553MBEDTLS_CHECK_RETURN_CRITICAL
Ronald Crone3dac4a2022-06-10 17:21:51 +0200554MBEDTLS_STATIC_TESTABLE
Gilles Peskine449bd832023-01-11 14:50:10 +0100555int mbedtls_ssl_tls13_parse_certificate(mbedtls_ssl_context *ssl,
556 const unsigned char *buf,
557 const unsigned char *end)
Xiaofei Bai947571e2021-09-29 09:12:03 +0000558{
559 ((void) ssl);
560 ((void) buf);
561 ((void) end);
Gilles Peskine449bd832023-01-11 14:50:10 +0100562 return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Xiaofei Bai947571e2021-09-29 09:12:03 +0000563}
564#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Ronald Cronde08cf32022-10-04 17:15:35 +0200565#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED */
Xiaofei Bai947571e2021-09-29 09:12:03 +0000566
Ronald Cronde08cf32022-10-04 17:15:35 +0200567#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED)
Xiaofei Bai947571e2021-09-29 09:12:03 +0000568#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Xiaofei Bai79595ac2021-10-26 07:16:45 +0000569/* Validate certificate chain sent by the server. */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200570MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100571static int ssl_tls13_validate_certificate(mbedtls_ssl_context *ssl)
Xiaofei Bai947571e2021-09-29 09:12:03 +0000572{
573 int ret = 0;
XiaokangQian989f06d2022-05-17 01:50:15 +0000574 int authmode = MBEDTLS_SSL_VERIFY_REQUIRED;
Xiaofei Bai947571e2021-09-29 09:12:03 +0000575 mbedtls_x509_crt *ca_chain;
576 mbedtls_x509_crl *ca_crl;
Ronald Cron30c5a252022-06-16 19:31:06 +0200577 const char *ext_oid;
578 size_t ext_len;
Xiaofei Baiff456022021-10-28 06:50:17 +0000579 uint32_t verify_result = 0;
Xiaofei Bai947571e2021-09-29 09:12:03 +0000580
XiaokangQian6b916b12022-04-25 07:29:34 +0000581 /* If SNI was used, overwrite authentication mode
582 * from the configuration. */
XiaokangQian989f06d2022-05-17 01:50:15 +0000583#if defined(MBEDTLS_SSL_SRV_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100584 if (ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER) {
XiaokangQian0557c942022-05-30 08:10:53 +0000585#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Gilles Peskine449bd832023-01-11 14:50:10 +0100586 if (ssl->handshake->sni_authmode != MBEDTLS_SSL_VERIFY_UNSET) {
XiaokangQian0557c942022-05-30 08:10:53 +0000587 authmode = ssl->handshake->sni_authmode;
Gilles Peskine449bd832023-01-11 14:50:10 +0100588 } else
XiaokangQian0557c942022-05-30 08:10:53 +0000589#endif
Gilles Peskine449bd832023-01-11 14:50:10 +0100590 authmode = ssl->conf->authmode;
XiaokangQian0557c942022-05-30 08:10:53 +0000591 }
XiaokangQian6b916b12022-04-25 07:29:34 +0000592#endif
593
594 /*
XiaokangQian989f06d2022-05-17 01:50:15 +0000595 * If the peer hasn't sent a certificate ( i.e. it sent
XiaokangQian6b916b12022-04-25 07:29:34 +0000596 * an empty certificate chain ), this is reflected in the peer CRT
597 * structure being unset.
598 * Check for that and handle it depending on the
XiaokangQian989f06d2022-05-17 01:50:15 +0000599 * authentication mode.
XiaokangQian6b916b12022-04-25 07:29:34 +0000600 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100601 if (ssl->session_negotiate->peer_cert == NULL) {
602 MBEDTLS_SSL_DEBUG_MSG(1, ("peer has no certificate"));
XiaokangQian989f06d2022-05-17 01:50:15 +0000603
XiaokangQian63e713e2022-05-15 04:26:57 +0000604#if defined(MBEDTLS_SSL_SRV_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100605 if (ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER) {
XiaokangQian63e713e2022-05-15 04:26:57 +0000606 /* The client was asked for a certificate but didn't send
607 * one. The client should know what's going on, so we
608 * don't send an alert.
609 */
610 ssl->session_negotiate->verify_result = MBEDTLS_X509_BADCERT_MISSING;
Gilles Peskine449bd832023-01-11 14:50:10 +0100611 if (authmode == MBEDTLS_SSL_VERIFY_OPTIONAL) {
612 return 0;
613 } else {
614 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_NO_CERT,
615 MBEDTLS_ERR_SSL_NO_CLIENT_CERTIFICATE);
616 return MBEDTLS_ERR_SSL_NO_CLIENT_CERTIFICATE;
XiaokangQian989f06d2022-05-17 01:50:15 +0000617 }
XiaokangQian63e713e2022-05-15 04:26:57 +0000618 }
XiaokangQian6b916b12022-04-25 07:29:34 +0000619#endif /* MBEDTLS_SSL_SRV_C */
620
XiaokangQianc3017f62022-05-13 05:55:41 +0000621#if defined(MBEDTLS_SSL_CLI_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100622 if (ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT) {
623 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_NO_CERT,
624 MBEDTLS_ERR_SSL_FATAL_ALERT_MESSAGE);
625 return MBEDTLS_ERR_SSL_FATAL_ALERT_MESSAGE;
XiaokangQian63e713e2022-05-15 04:26:57 +0000626 }
XiaokangQianc3017f62022-05-13 05:55:41 +0000627#endif /* MBEDTLS_SSL_CLI_C */
XiaokangQian63e713e2022-05-15 04:26:57 +0000628 }
XiaokangQian6b916b12022-04-25 07:29:34 +0000629
Xiaofei Bai947571e2021-09-29 09:12:03 +0000630#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Gilles Peskine449bd832023-01-11 14:50:10 +0100631 if (ssl->handshake->sni_ca_chain != NULL) {
Xiaofei Bai947571e2021-09-29 09:12:03 +0000632 ca_chain = ssl->handshake->sni_ca_chain;
633 ca_crl = ssl->handshake->sni_ca_crl;
Gilles Peskine449bd832023-01-11 14:50:10 +0100634 } else
Xiaofei Bai947571e2021-09-29 09:12:03 +0000635#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
636 {
637 ca_chain = ssl->conf->ca_chain;
638 ca_crl = ssl->conf->ca_crl;
639 }
640
641 /*
642 * Main check: verify certificate
643 */
644 ret = mbedtls_x509_crt_verify_with_profile(
645 ssl->session_negotiate->peer_cert,
646 ca_chain, ca_crl,
647 ssl->conf->cert_profile,
648 ssl->hostname,
Xiaofei Baiff456022021-10-28 06:50:17 +0000649 &verify_result,
Gilles Peskine449bd832023-01-11 14:50:10 +0100650 ssl->conf->f_vrfy, ssl->conf->p_vrfy);
Xiaofei Bai947571e2021-09-29 09:12:03 +0000651
Gilles Peskine449bd832023-01-11 14:50:10 +0100652 if (ret != 0) {
653 MBEDTLS_SSL_DEBUG_RET(1, "x509_verify_cert", ret);
Xiaofei Bai947571e2021-09-29 09:12:03 +0000654 }
655
656 /*
657 * Secondary checks: always done, but change 'ret' only if it was 0
658 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100659 if (ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT) {
Ronald Cron30c5a252022-06-16 19:31:06 +0200660 ext_oid = MBEDTLS_OID_SERVER_AUTH;
Gilles Peskine449bd832023-01-11 14:50:10 +0100661 ext_len = MBEDTLS_OID_SIZE(MBEDTLS_OID_SERVER_AUTH);
662 } else {
Ronald Cron30c5a252022-06-16 19:31:06 +0200663 ext_oid = MBEDTLS_OID_CLIENT_AUTH;
Gilles Peskine449bd832023-01-11 14:50:10 +0100664 ext_len = MBEDTLS_OID_SIZE(MBEDTLS_OID_CLIENT_AUTH);
Ronald Cron30c5a252022-06-16 19:31:06 +0200665 }
666
Gilles Peskine449bd832023-01-11 14:50:10 +0100667 if ((mbedtls_x509_crt_check_key_usage(
668 ssl->session_negotiate->peer_cert,
669 MBEDTLS_X509_KU_DIGITAL_SIGNATURE) != 0) ||
670 (mbedtls_x509_crt_check_extended_key_usage(
671 ssl->session_negotiate->peer_cert,
672 ext_oid, ext_len) != 0)) {
673 MBEDTLS_SSL_DEBUG_MSG(1, ("bad certificate (usage extensions)"));
674 if (ret == 0) {
Xiaofei Bai947571e2021-09-29 09:12:03 +0000675 ret = MBEDTLS_ERR_SSL_BAD_CERTIFICATE;
Gilles Peskine449bd832023-01-11 14:50:10 +0100676 }
Xiaofei Bai947571e2021-09-29 09:12:03 +0000677 }
678
XiaokangQian6b916b12022-04-25 07:29:34 +0000679 /* mbedtls_x509_crt_verify_with_profile is supposed to report a
680 * verification failure through MBEDTLS_ERR_X509_CERT_VERIFY_FAILED,
681 * with details encoded in the verification flags. All other kinds
682 * of error codes, including those from the user provided f_vrfy
683 * functions, are treated as fatal and lead to a failure of
Ronald Crone3dac4a2022-06-10 17:21:51 +0200684 * mbedtls_ssl_tls13_parse_certificate even if verification was optional.
685 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100686 if (authmode == MBEDTLS_SSL_VERIFY_OPTIONAL &&
687 (ret == MBEDTLS_ERR_X509_CERT_VERIFY_FAILED ||
688 ret == MBEDTLS_ERR_SSL_BAD_CERTIFICATE)) {
XiaokangQian6b916b12022-04-25 07:29:34 +0000689 ret = 0;
690 }
Xiaofei Bai947571e2021-09-29 09:12:03 +0000691
Gilles Peskine449bd832023-01-11 14:50:10 +0100692 if (ca_chain == NULL && authmode == MBEDTLS_SSL_VERIFY_REQUIRED) {
693 MBEDTLS_SSL_DEBUG_MSG(1, ("got no CA chain"));
Xiaofei Bai947571e2021-09-29 09:12:03 +0000694 ret = MBEDTLS_ERR_SSL_CA_CHAIN_REQUIRED;
695 }
696
Gilles Peskine449bd832023-01-11 14:50:10 +0100697 if (ret != 0) {
Xiaofei Bai947571e2021-09-29 09:12:03 +0000698 /* The certificate may have been rejected for several reasons.
699 Pick one and send the corresponding alert. Which alert to send
700 may be a subject of debate in some cases. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100701 if (verify_result & MBEDTLS_X509_BADCERT_OTHER) {
702 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED, ret);
703 } else if (verify_result & MBEDTLS_X509_BADCERT_CN_MISMATCH) {
704 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_BAD_CERT, ret);
705 } else if (verify_result & (MBEDTLS_X509_BADCERT_KEY_USAGE |
706 MBEDTLS_X509_BADCERT_EXT_KEY_USAGE |
707 MBEDTLS_X509_BADCERT_NS_CERT_TYPE |
708 MBEDTLS_X509_BADCERT_BAD_PK |
709 MBEDTLS_X509_BADCERT_BAD_KEY)) {
710 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT, ret);
711 } else if (verify_result & MBEDTLS_X509_BADCERT_EXPIRED) {
712 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_CERT_EXPIRED, ret);
713 } else if (verify_result & MBEDTLS_X509_BADCERT_REVOKED) {
714 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_CERT_REVOKED, ret);
715 } else if (verify_result & MBEDTLS_X509_BADCERT_NOT_TRUSTED) {
716 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_UNKNOWN_CA, ret);
717 } else {
718 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_CERT_UNKNOWN, ret);
719 }
Xiaofei Bai947571e2021-09-29 09:12:03 +0000720 }
721
722#if defined(MBEDTLS_DEBUG_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100723 if (verify_result != 0) {
724 MBEDTLS_SSL_DEBUG_MSG(3, ("! Certificate verification flags %08x",
725 (unsigned int) verify_result));
726 } else {
727 MBEDTLS_SSL_DEBUG_MSG(3, ("Certificate verification flags clear"));
Xiaofei Bai947571e2021-09-29 09:12:03 +0000728 }
729#endif /* MBEDTLS_DEBUG_C */
730
Xiaofei Baiff456022021-10-28 06:50:17 +0000731 ssl->session_negotiate->verify_result = verify_result;
Gilles Peskine449bd832023-01-11 14:50:10 +0100732 return ret;
Xiaofei Bai947571e2021-09-29 09:12:03 +0000733}
734#else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200735MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100736static int ssl_tls13_validate_certificate(mbedtls_ssl_context *ssl)
Xiaofei Bai947571e2021-09-29 09:12:03 +0000737{
738 ((void) ssl);
Gilles Peskine449bd832023-01-11 14:50:10 +0100739 return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Xiaofei Bai947571e2021-09-29 09:12:03 +0000740}
741#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Ronald Cronde08cf32022-10-04 17:15:35 +0200742#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED */
Xiaofei Bai947571e2021-09-29 09:12:03 +0000743
Gilles Peskine449bd832023-01-11 14:50:10 +0100744int mbedtls_ssl_tls13_process_certificate(mbedtls_ssl_context *ssl)
Xiaofei Bai947571e2021-09-29 09:12:03 +0000745{
Xiaofei Bai79595ac2021-10-26 07:16:45 +0000746 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine449bd832023-01-11 14:50:10 +0100747 MBEDTLS_SSL_DEBUG_MSG(2, ("=> parse certificate"));
Xiaofei Bai79595ac2021-10-26 07:16:45 +0000748
Ronald Cronde08cf32022-10-04 17:15:35 +0200749#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED)
XiaokangQianc3017f62022-05-13 05:55:41 +0000750 unsigned char *buf;
751 size_t buf_len;
Xiaofei Bai79595ac2021-10-26 07:16:45 +0000752
Gilles Peskine449bd832023-01-11 14:50:10 +0100753 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_tls13_fetch_handshake_msg(
754 ssl, MBEDTLS_SSL_HS_CERTIFICATE,
755 &buf, &buf_len));
Xiaofei Bai79595ac2021-10-26 07:16:45 +0000756
XiaokangQianc3017f62022-05-13 05:55:41 +0000757 /* Parse the certificate chain sent by the peer. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100758 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_tls13_parse_certificate(ssl, buf,
759 buf + buf_len));
XiaokangQianc3017f62022-05-13 05:55:41 +0000760 /* Validate the certificate chain and set the verification results. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100761 MBEDTLS_SSL_PROC_CHK(ssl_tls13_validate_certificate(ssl));
Xiaofei Bai79595ac2021-10-26 07:16:45 +0000762
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +0100763 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_add_hs_msg_to_checksum(ssl,
Manuel Pégourié-Gonnard43cc1272023-02-06 11:48:19 +0100764 MBEDTLS_SSL_HS_CERTIFICATE, buf,
765 buf_len));
Xiaofei Bai79595ac2021-10-26 07:16:45 +0000766
Xiaofei Bai79595ac2021-10-26 07:16:45 +0000767cleanup:
Ronald Cronde08cf32022-10-04 17:15:35 +0200768#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED */
Xiaofei Bai79595ac2021-10-26 07:16:45 +0000769
Gilles Peskine449bd832023-01-11 14:50:10 +0100770 MBEDTLS_SSL_DEBUG_MSG(2, ("<= parse certificate"));
771 return ret;
Xiaofei Bai947571e2021-09-29 09:12:03 +0000772}
Ronald Cron928cbd32022-10-04 16:14:26 +0200773#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED)
Jerry Yu7399d0d2022-01-30 17:54:19 +0800774/*
775 * enum {
776 * X509(0),
777 * RawPublicKey(2),
778 * (255)
779 * } CertificateType;
780 *
781 * struct {
782 * select (certificate_type) {
783 * case RawPublicKey:
784 * // From RFC 7250 ASN.1_subjectPublicKeyInfo
785 * opaque ASN1_subjectPublicKeyInfo<1..2^24-1>;
786 *
787 * case X509:
788 * opaque cert_data<1..2^24-1>;
789 * };
790 * Extension extensions<0..2^16-1>;
791 * } CertificateEntry;
792 *
793 * struct {
794 * opaque certificate_request_context<0..2^8-1>;
795 * CertificateEntry certificate_list<0..2^24-1>;
796 * } Certificate;
797 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200798MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100799static int ssl_tls13_write_certificate_body(mbedtls_ssl_context *ssl,
800 unsigned char *buf,
801 unsigned char *end,
802 size_t *out_len)
Jerry Yu5cc35062022-01-28 16:16:08 +0800803{
Gilles Peskine449bd832023-01-11 14:50:10 +0100804 const mbedtls_x509_crt *crt = mbedtls_ssl_own_cert(ssl);
Jerry Yu3e536442022-02-15 11:05:59 +0800805 unsigned char *p = buf;
Jerry Yuc8d8d4e2022-02-18 12:10:03 +0800806 unsigned char *certificate_request_context =
Gilles Peskine449bd832023-01-11 14:50:10 +0100807 ssl->handshake->certificate_request_context;
Jerry Yuc8d8d4e2022-02-18 12:10:03 +0800808 unsigned char certificate_request_context_len =
Gilles Peskine449bd832023-01-11 14:50:10 +0100809 ssl->handshake->certificate_request_context_len;
Jerry Yuc8d8d4e2022-02-18 12:10:03 +0800810 unsigned char *p_certificate_list_len;
Jerry Yu5cc35062022-01-28 16:16:08 +0800811
Jerry Yu5cc35062022-01-28 16:16:08 +0800812
Jerry Yu3391ac02022-02-16 11:21:37 +0800813 /* ...
814 * opaque certificate_request_context<0..2^8-1>;
815 * ...
816 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100817 MBEDTLS_SSL_CHK_BUF_PTR(p, end, certificate_request_context_len + 1);
Jerry Yuc8d8d4e2022-02-18 12:10:03 +0800818 *p++ = certificate_request_context_len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100819 if (certificate_request_context_len > 0) {
820 memcpy(p, certificate_request_context, certificate_request_context_len);
Jerry Yuc8d8d4e2022-02-18 12:10:03 +0800821 p += certificate_request_context_len;
Jerry Yu537530d2022-02-15 14:00:57 +0800822 }
823
Jerry Yu3391ac02022-02-16 11:21:37 +0800824 /* ...
825 * CertificateEntry certificate_list<0..2^24-1>;
826 * ...
827 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100828 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 3);
Jerry Yuc8d8d4e2022-02-18 12:10:03 +0800829 p_certificate_list_len = p;
Jerry Yu3e536442022-02-15 11:05:59 +0800830 p += 3;
831
Gilles Peskine449bd832023-01-11 14:50:10 +0100832 MBEDTLS_SSL_DEBUG_CRT(3, "own certificate", crt);
Jerry Yu5cc35062022-01-28 16:16:08 +0800833
Gilles Peskine449bd832023-01-11 14:50:10 +0100834 while (crt != NULL) {
Jerry Yu7399d0d2022-01-30 17:54:19 +0800835 size_t cert_data_len = crt->raw.len;
Jerry Yu5cc35062022-01-28 16:16:08 +0800836
Gilles Peskine449bd832023-01-11 14:50:10 +0100837 MBEDTLS_SSL_CHK_BUF_PTR(p, end, cert_data_len + 3 + 2);
838 MBEDTLS_PUT_UINT24_BE(cert_data_len, p, 0);
Jerry Yu7399d0d2022-01-30 17:54:19 +0800839 p += 3;
Jerry Yu5cc35062022-01-28 16:16:08 +0800840
Gilles Peskine449bd832023-01-11 14:50:10 +0100841 memcpy(p, crt->raw.p, cert_data_len);
Jerry Yu7399d0d2022-01-30 17:54:19 +0800842 p += cert_data_len;
843 crt = crt->next;
Jerry Yu5cc35062022-01-28 16:16:08 +0800844
845 /* Currently, we don't have any certificate extensions defined.
846 * Hence, we are sending an empty extension with length zero.
847 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100848 MBEDTLS_PUT_UINT16_BE(0, p, 0);
Jerry Yu7399d0d2022-01-30 17:54:19 +0800849 p += 2;
Jerry Yu5cc35062022-01-28 16:16:08 +0800850 }
Jerry Yu5cc35062022-01-28 16:16:08 +0800851
Gilles Peskine449bd832023-01-11 14:50:10 +0100852 MBEDTLS_PUT_UINT24_BE(p - p_certificate_list_len - 3,
853 p_certificate_list_len, 0);
Jerry Yu7399d0d2022-01-30 17:54:19 +0800854
Jerry Yu3e536442022-02-15 11:05:59 +0800855 *out_len = p - buf;
Jerry Yu5cc35062022-01-28 16:16:08 +0800856
Jerry Yu7de2ff02022-11-08 21:43:46 +0800857 MBEDTLS_SSL_PRINT_EXTS(
Gilles Peskine449bd832023-01-11 14:50:10 +0100858 3, MBEDTLS_SSL_HS_CERTIFICATE, ssl->handshake->sent_extensions);
Jerry Yu4b8f2f72022-10-31 13:31:22 +0800859
Gilles Peskine449bd832023-01-11 14:50:10 +0100860 return 0;
Jerry Yu5cc35062022-01-28 16:16:08 +0800861}
Jerry Yu5cc35062022-01-28 16:16:08 +0800862
Gilles Peskine449bd832023-01-11 14:50:10 +0100863int mbedtls_ssl_tls13_write_certificate(mbedtls_ssl_context *ssl)
Jerry Yu5cc35062022-01-28 16:16:08 +0800864{
865 int ret;
Ronald Cron5bb8fc82022-03-09 07:00:13 +0100866 unsigned char *buf;
867 size_t buf_len, msg_len;
868
Gilles Peskine449bd832023-01-11 14:50:10 +0100869 MBEDTLS_SSL_DEBUG_MSG(2, ("=> write certificate"));
Jerry Yu5cc35062022-01-28 16:16:08 +0800870
Gilles Peskine449bd832023-01-11 14:50:10 +0100871 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_start_handshake_msg(ssl,
872 MBEDTLS_SSL_HS_CERTIFICATE, &buf,
873 &buf_len));
Jerry Yu5cc35062022-01-28 16:16:08 +0800874
Gilles Peskine449bd832023-01-11 14:50:10 +0100875 MBEDTLS_SSL_PROC_CHK(ssl_tls13_write_certificate_body(ssl,
876 buf,
877 buf + buf_len,
878 &msg_len));
Jerry Yu5cc35062022-01-28 16:16:08 +0800879
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +0100880 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_add_hs_msg_to_checksum(ssl,
Manuel Pégourié-Gonnard43cc1272023-02-06 11:48:19 +0100881 MBEDTLS_SSL_HS_CERTIFICATE, buf,
882 msg_len));
Jerry Yu5cc35062022-01-28 16:16:08 +0800883
Gilles Peskine449bd832023-01-11 14:50:10 +0100884 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_finish_handshake_msg(
885 ssl, buf_len, msg_len));
Jerry Yu5cc35062022-01-28 16:16:08 +0800886cleanup:
887
Gilles Peskine449bd832023-01-11 14:50:10 +0100888 MBEDTLS_SSL_DEBUG_MSG(2, ("<= write certificate"));
889 return ret;
Jerry Yu5cc35062022-01-28 16:16:08 +0800890}
891
Jerry Yu3e536442022-02-15 11:05:59 +0800892/*
893 * STATE HANDLING: Output Certificate Verify
894 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100895int mbedtls_ssl_tls13_check_sig_alg_cert_key_match(uint16_t sig_alg,
896 mbedtls_pk_context *key)
Jerry Yu0c6be8f2022-06-20 20:42:00 +0800897{
Gilles Peskine449bd832023-01-11 14:50:10 +0100898 mbedtls_pk_type_t pk_type = mbedtls_ssl_sig_from_pk(key);
899 size_t key_size = mbedtls_pk_get_bitlen(key);
Jerry Yu0c6be8f2022-06-20 20:42:00 +0800900
Gilles Peskine449bd832023-01-11 14:50:10 +0100901 switch (pk_type) {
Jerry Yu67eced02022-02-25 13:37:36 +0800902 case MBEDTLS_SSL_SIG_ECDSA:
Gilles Peskine449bd832023-01-11 14:50:10 +0100903 switch (key_size) {
Jerry Yu67eced02022-02-25 13:37:36 +0800904 case 256:
Gilles Peskine449bd832023-01-11 14:50:10 +0100905 return
906 sig_alg == MBEDTLS_TLS1_3_SIG_ECDSA_SECP256R1_SHA256;
Jerry Yu0c6be8f2022-06-20 20:42:00 +0800907
Jerry Yu67eced02022-02-25 13:37:36 +0800908 case 384:
Gilles Peskine449bd832023-01-11 14:50:10 +0100909 return
910 sig_alg == MBEDTLS_TLS1_3_SIG_ECDSA_SECP384R1_SHA384;
Jerry Yu0c6be8f2022-06-20 20:42:00 +0800911
Jerry Yu67eced02022-02-25 13:37:36 +0800912 case 521:
Gilles Peskine449bd832023-01-11 14:50:10 +0100913 return
914 sig_alg == MBEDTLS_TLS1_3_SIG_ECDSA_SECP521R1_SHA512;
Jerry Yu67eced02022-02-25 13:37:36 +0800915 default:
Jerry Yu67eced02022-02-25 13:37:36 +0800916 break;
917 }
918 break;
Jerry Yu67eced02022-02-25 13:37:36 +0800919
Jerry Yu67eced02022-02-25 13:37:36 +0800920 case MBEDTLS_SSL_SIG_RSA:
Gilles Peskine449bd832023-01-11 14:50:10 +0100921 switch (sig_alg) {
Ronald Cron38391bf2022-09-16 11:19:27 +0200922 case MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA256: /* Intentional fallthrough */
923 case MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA384: /* Intentional fallthrough */
Jerry Yu0c6be8f2022-06-20 20:42:00 +0800924 case MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA512:
Gilles Peskine449bd832023-01-11 14:50:10 +0100925 return 1;
Jerry Yuc2e04932022-06-27 22:13:03 +0800926
Jerry Yu0c6be8f2022-06-20 20:42:00 +0800927 default:
928 break;
Jerry Yucef3f332022-03-22 23:00:13 +0800929 }
Jerry Yu67eced02022-02-25 13:37:36 +0800930 break;
Jerry Yu0c6be8f2022-06-20 20:42:00 +0800931
Jerry Yu67eced02022-02-25 13:37:36 +0800932 default:
Jerry Yu67eced02022-02-25 13:37:36 +0800933 break;
934 }
Jerry Yu0c6be8f2022-06-20 20:42:00 +0800935
Gilles Peskine449bd832023-01-11 14:50:10 +0100936 return 0;
Jerry Yu0c6be8f2022-06-20 20:42:00 +0800937}
938
Ronald Cronce7d76e2022-07-08 18:56:49 +0200939MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100940static int ssl_tls13_write_certificate_verify_body(mbedtls_ssl_context *ssl,
941 unsigned char *buf,
942 unsigned char *end,
943 size_t *out_len)
Jerry Yu8511f122022-01-29 10:01:04 +0800944{
Ronald Cron067a1e72022-09-16 13:44:49 +0200945 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yu3e536442022-02-15 11:05:59 +0800946 unsigned char *p = buf;
Jerry Yu8511f122022-01-29 10:01:04 +0800947 mbedtls_pk_context *own_key;
Jerry Yu3e536442022-02-15 11:05:59 +0800948
Gilles Peskine449bd832023-01-11 14:50:10 +0100949 unsigned char handshake_hash[MBEDTLS_TLS1_3_MD_MAX_SIZE];
Jerry Yu8511f122022-01-29 10:01:04 +0800950 size_t handshake_hash_len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100951 unsigned char verify_buffer[SSL_VERIFY_STRUCT_MAX_SIZE];
Jerry Yu3e536442022-02-15 11:05:59 +0800952 size_t verify_buffer_len;
Ronald Cron067a1e72022-09-16 13:44:49 +0200953
954 uint16_t *sig_alg = ssl->handshake->received_sig_algs;
Jerry Yu3e536442022-02-15 11:05:59 +0800955 size_t signature_len = 0;
Jerry Yu8511f122022-01-29 10:01:04 +0800956
Jerry Yu0b7b1012022-02-23 12:23:05 +0800957 *out_len = 0;
958
Gilles Peskine449bd832023-01-11 14:50:10 +0100959 own_key = mbedtls_ssl_own_key(ssl);
960 if (own_key == NULL) {
961 MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
962 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Jerry Yu8511f122022-01-29 10:01:04 +0800963 }
964
Gilles Peskine449bd832023-01-11 14:50:10 +0100965 ret = mbedtls_ssl_get_handshake_transcript(ssl,
966 ssl->handshake->ciphersuite_info->mac,
967 handshake_hash,
968 sizeof(handshake_hash),
969 &handshake_hash_len);
970 if (ret != 0) {
971 return ret;
972 }
Jerry Yu8511f122022-01-29 10:01:04 +0800973
Gilles Peskine449bd832023-01-11 14:50:10 +0100974 MBEDTLS_SSL_DEBUG_BUF(3, "handshake hash",
975 handshake_hash,
976 handshake_hash_len);
Jerry Yu8511f122022-01-29 10:01:04 +0800977
Gilles Peskine449bd832023-01-11 14:50:10 +0100978 ssl_tls13_create_verify_structure(handshake_hash, handshake_hash_len,
979 verify_buffer, &verify_buffer_len,
980 ssl->conf->endpoint);
Jerry Yu8511f122022-01-29 10:01:04 +0800981
982 /*
983 * struct {
984 * SignatureScheme algorithm;
985 * opaque signature<0..2^16-1>;
986 * } CertificateVerify;
987 */
Ronald Cron067a1e72022-09-16 13:44:49 +0200988 /* Check there is space for the algorithm identifier (2 bytes) and the
989 * signature length (2 bytes).
990 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100991 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 4);
Ronald Cron067a1e72022-09-16 13:44:49 +0200992
Gilles Peskine449bd832023-01-11 14:50:10 +0100993 for (; *sig_alg != MBEDTLS_TLS1_3_SIG_NONE; sig_alg++) {
Ronald Cron067a1e72022-09-16 13:44:49 +0200994 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
995 mbedtls_pk_type_t pk_type = MBEDTLS_PK_NONE;
996 mbedtls_md_type_t md_alg = MBEDTLS_MD_NONE;
997 psa_algorithm_t psa_algorithm = PSA_ALG_NONE;
998 unsigned char verify_hash[PSA_HASH_MAX_SIZE];
999 size_t verify_hash_len;
Jerry Yu67eced02022-02-25 13:37:36 +08001000
Gilles Peskine449bd832023-01-11 14:50:10 +01001001 if (!mbedtls_ssl_sig_alg_is_offered(ssl, *sig_alg)) {
Ronald Cron067a1e72022-09-16 13:44:49 +02001002 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +01001003 }
Jerry Yu67eced02022-02-25 13:37:36 +08001004
Gilles Peskine449bd832023-01-11 14:50:10 +01001005 if (!mbedtls_ssl_tls13_sig_alg_for_cert_verify_is_supported(*sig_alg)) {
Ronald Cron067a1e72022-09-16 13:44:49 +02001006 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +01001007 }
Ronald Cron067a1e72022-09-16 13:44:49 +02001008
Gilles Peskine449bd832023-01-11 14:50:10 +01001009 if (!mbedtls_ssl_tls13_check_sig_alg_cert_key_match(*sig_alg, own_key)) {
Ronald Cron067a1e72022-09-16 13:44:49 +02001010 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +01001011 }
Ronald Cron067a1e72022-09-16 13:44:49 +02001012
Gilles Peskine449bd832023-01-11 14:50:10 +01001013 if (mbedtls_ssl_get_pk_type_and_md_alg_from_sig_alg(
1014 *sig_alg, &pk_type, &md_alg) != 0) {
1015 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Ronald Cron067a1e72022-09-16 13:44:49 +02001016 }
1017
1018 /* Hash verify buffer with indicated hash function */
Gilles Peskine449bd832023-01-11 14:50:10 +01001019 psa_algorithm = mbedtls_hash_info_psa_from_md(md_alg);
1020 status = psa_hash_compute(psa_algorithm,
1021 verify_buffer,
1022 verify_buffer_len,
1023 verify_hash, sizeof(verify_hash),
1024 &verify_hash_len);
1025 if (status != PSA_SUCCESS) {
Andrzej Kurek8a045ce2022-12-23 11:00:06 -05001026 return PSA_TO_MBEDTLS_ERR(status);
Ronald Cron067a1e72022-09-16 13:44:49 +02001027 }
1028
Gilles Peskine449bd832023-01-11 14:50:10 +01001029 MBEDTLS_SSL_DEBUG_BUF(3, "verify hash", verify_hash, verify_hash_len);
1030
1031 if ((ret = mbedtls_pk_sign_ext(pk_type, own_key,
1032 md_alg, verify_hash, verify_hash_len,
1033 p + 4, (size_t) (end - (p + 4)), &signature_len,
1034 ssl->conf->f_rng, ssl->conf->p_rng)) != 0) {
1035 MBEDTLS_SSL_DEBUG_MSG(2, ("CertificateVerify signature failed with %s",
1036 mbedtls_ssl_sig_alg_to_str(*sig_alg)));
1037 MBEDTLS_SSL_DEBUG_RET(2, "mbedtls_pk_sign_ext", ret);
1038
1039 /* The signature failed. This is possible if the private key
1040 * was not suitable for the signature operation as purposely we
1041 * did not check its suitability completely. Let's try with
1042 * another signature algorithm.
1043 */
1044 continue;
1045 }
1046
1047 MBEDTLS_SSL_DEBUG_MSG(2, ("CertificateVerify signature with %s",
1048 mbedtls_ssl_sig_alg_to_str(*sig_alg)));
Ronald Cron067a1e72022-09-16 13:44:49 +02001049
1050 break;
1051 }
1052
Gilles Peskine449bd832023-01-11 14:50:10 +01001053 if (*sig_alg == MBEDTLS_TLS1_3_SIG_NONE) {
1054 MBEDTLS_SSL_DEBUG_MSG(1, ("no suitable signature algorithm"));
1055 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE,
1056 MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE);
1057 return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
Jerry Yu8511f122022-01-29 10:01:04 +08001058 }
1059
Gilles Peskine449bd832023-01-11 14:50:10 +01001060 MBEDTLS_PUT_UINT16_BE(*sig_alg, p, 0);
1061 MBEDTLS_PUT_UINT16_BE(signature_len, p, 2);
Jerry Yuf3b46b52022-06-19 16:52:27 +08001062
Ronald Cron067a1e72022-09-16 13:44:49 +02001063 *out_len = 4 + signature_len;
Jerry Yu8c338862022-03-23 13:34:04 +08001064
Gilles Peskine449bd832023-01-11 14:50:10 +01001065 return 0;
Jerry Yu8511f122022-01-29 10:01:04 +08001066}
Jerry Yu8511f122022-01-29 10:01:04 +08001067
Gilles Peskine449bd832023-01-11 14:50:10 +01001068int mbedtls_ssl_tls13_write_certificate_verify(mbedtls_ssl_context *ssl)
Jerry Yu8511f122022-01-29 10:01:04 +08001069{
1070 int ret = 0;
Jerry Yuca133a32022-02-15 14:22:05 +08001071 unsigned char *buf;
1072 size_t buf_len, msg_len;
1073
Gilles Peskine449bd832023-01-11 14:50:10 +01001074 MBEDTLS_SSL_DEBUG_MSG(2, ("=> write certificate verify"));
Jerry Yu8511f122022-01-29 10:01:04 +08001075
Gilles Peskine449bd832023-01-11 14:50:10 +01001076 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_start_handshake_msg(ssl,
1077 MBEDTLS_SSL_HS_CERTIFICATE_VERIFY, &buf,
1078 &buf_len));
Jerry Yu8511f122022-01-29 10:01:04 +08001079
Gilles Peskine449bd832023-01-11 14:50:10 +01001080 MBEDTLS_SSL_PROC_CHK(ssl_tls13_write_certificate_verify_body(
1081 ssl, buf, buf + buf_len, &msg_len));
Jerry Yu8511f122022-01-29 10:01:04 +08001082
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +01001083 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_add_hs_msg_to_checksum(ssl,
Manuel Pégourié-Gonnard43cc1272023-02-06 11:48:19 +01001084 MBEDTLS_SSL_HS_CERTIFICATE_VERIFY, buf,
1085 msg_len));
Jerry Yu8511f122022-01-29 10:01:04 +08001086
Gilles Peskine449bd832023-01-11 14:50:10 +01001087 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_finish_handshake_msg(
1088 ssl, buf_len, msg_len));
Jerry Yu8511f122022-01-29 10:01:04 +08001089
1090cleanup:
1091
Gilles Peskine449bd832023-01-11 14:50:10 +01001092 MBEDTLS_SSL_DEBUG_MSG(2, ("<= write certificate verify"));
1093 return ret;
Jerry Yu8511f122022-01-29 10:01:04 +08001094}
1095
Ronald Cron928cbd32022-10-04 16:14:26 +02001096#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED */
Jerry Yu90f152d2022-01-29 22:12:42 +08001097
Jerry Yu5cc35062022-01-28 16:16:08 +08001098/*
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001099 *
XiaokangQianc5c39d52021-11-09 11:55:10 +00001100 * STATE HANDLING: Incoming Finished message.
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001101 */
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001102/*
1103 * Implementation
1104 */
1105
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001106MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01001107static int ssl_tls13_preprocess_finished_message(mbedtls_ssl_context *ssl)
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001108{
1109 int ret;
1110
Gilles Peskine449bd832023-01-11 14:50:10 +01001111 ret = mbedtls_ssl_tls13_calculate_verify_data(ssl,
1112 ssl->handshake->state_local.finished_in.digest,
1113 sizeof(ssl->handshake->state_local.finished_in.
1114 digest),
1115 &ssl->handshake->state_local.finished_in.digest_len,
1116 ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT ?
1117 MBEDTLS_SSL_IS_SERVER : MBEDTLS_SSL_IS_CLIENT);
1118 if (ret != 0) {
1119 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_tls13_calculate_verify_data", ret);
1120 return ret;
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001121 }
1122
Gilles Peskine449bd832023-01-11 14:50:10 +01001123 return 0;
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001124}
1125
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001126MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01001127static int ssl_tls13_parse_finished_message(mbedtls_ssl_context *ssl,
1128 const unsigned char *buf,
1129 const unsigned char *end)
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001130{
XiaokangQian33062842021-11-11 03:37:45 +00001131 /*
1132 * struct {
XiaokangQianc13f9352021-11-11 06:13:22 +00001133 * opaque verify_data[Hash.length];
XiaokangQian33062842021-11-11 03:37:45 +00001134 * } Finished;
1135 */
1136 const unsigned char *expected_verify_data =
1137 ssl->handshake->state_local.finished_in.digest;
1138 size_t expected_verify_data_len =
1139 ssl->handshake->state_local.finished_in.digest_len;
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001140 /* Structural validation */
Gilles Peskine449bd832023-01-11 14:50:10 +01001141 if ((size_t) (end - buf) != expected_verify_data_len) {
1142 MBEDTLS_SSL_DEBUG_MSG(1, ("bad finished message"));
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001143
Gilles Peskine449bd832023-01-11 14:50:10 +01001144 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR,
1145 MBEDTLS_ERR_SSL_DECODE_ERROR);
1146 return MBEDTLS_ERR_SSL_DECODE_ERROR;
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001147 }
1148
Gilles Peskine449bd832023-01-11 14:50:10 +01001149 MBEDTLS_SSL_DEBUG_BUF(4, "verify_data (self-computed):",
1150 expected_verify_data,
1151 expected_verify_data_len);
1152 MBEDTLS_SSL_DEBUG_BUF(4, "verify_data (received message):", buf,
1153 expected_verify_data_len);
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001154
1155 /* Semantic validation */
Gilles Peskine449bd832023-01-11 14:50:10 +01001156 if (mbedtls_ct_memcmp(buf,
1157 expected_verify_data,
1158 expected_verify_data_len) != 0) {
1159 MBEDTLS_SSL_DEBUG_MSG(1, ("bad finished message"));
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001160
Gilles Peskine449bd832023-01-11 14:50:10 +01001161 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_DECRYPT_ERROR,
1162 MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE);
1163 return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001164 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001165 return 0;
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001166}
1167
Gilles Peskine449bd832023-01-11 14:50:10 +01001168int mbedtls_ssl_tls13_process_finished_message(mbedtls_ssl_context *ssl)
XiaokangQianc5c39d52021-11-09 11:55:10 +00001169{
XiaokangQian33062842021-11-11 03:37:45 +00001170 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
XiaokangQianc5c39d52021-11-09 11:55:10 +00001171 unsigned char *buf;
Xiaofei Baieef15042021-11-18 07:29:56 +00001172 size_t buf_len;
XiaokangQianc5c39d52021-11-09 11:55:10 +00001173
Gilles Peskine449bd832023-01-11 14:50:10 +01001174 MBEDTLS_SSL_DEBUG_MSG(2, ("=> parse finished message"));
XiaokangQianc5c39d52021-11-09 11:55:10 +00001175
Gilles Peskine449bd832023-01-11 14:50:10 +01001176 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_tls13_fetch_handshake_msg(ssl,
1177 MBEDTLS_SSL_HS_FINISHED,
1178 &buf, &buf_len));
Jerry Yu0a92d6c2022-05-16 16:54:46 +08001179
1180 /* Preprocessing step: Compute handshake digest */
Gilles Peskine449bd832023-01-11 14:50:10 +01001181 MBEDTLS_SSL_PROC_CHK(ssl_tls13_preprocess_finished_message(ssl));
Jerry Yu0a92d6c2022-05-16 16:54:46 +08001182
Gilles Peskine449bd832023-01-11 14:50:10 +01001183 MBEDTLS_SSL_PROC_CHK(ssl_tls13_parse_finished_message(ssl, buf, buf + buf_len));
Jerry Yu0a92d6c2022-05-16 16:54:46 +08001184
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +01001185 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_add_hs_msg_to_checksum(ssl,
Manuel Pégourié-Gonnard43cc1272023-02-06 11:48:19 +01001186 MBEDTLS_SSL_HS_FINISHED, buf, buf_len));
XiaokangQianc5c39d52021-11-09 11:55:10 +00001187
1188cleanup:
1189
Gilles Peskine449bd832023-01-11 14:50:10 +01001190 MBEDTLS_SSL_DEBUG_MSG(2, ("<= parse finished message"));
1191 return ret;
XiaokangQianc5c39d52021-11-09 11:55:10 +00001192}
1193
XiaokangQian74af2a82021-09-22 07:40:30 +00001194/*
1195 *
XiaokangQiancc90c942021-11-09 12:30:09 +00001196 * STATE HANDLING: Write and send Finished message.
XiaokangQian74af2a82021-09-22 07:40:30 +00001197 *
1198 */
XiaokangQian74af2a82021-09-22 07:40:30 +00001199/*
XiaokangQian35dc6252021-11-11 08:16:19 +00001200 * Implement
XiaokangQian74af2a82021-09-22 07:40:30 +00001201 */
1202
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001203MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01001204static int ssl_tls13_prepare_finished_message(mbedtls_ssl_context *ssl)
XiaokangQian74af2a82021-09-22 07:40:30 +00001205{
1206 int ret;
1207
1208 /* Compute transcript of handshake up to now. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001209 ret = mbedtls_ssl_tls13_calculate_verify_data(ssl,
1210 ssl->handshake->state_local.finished_out.digest,
1211 sizeof(ssl->handshake->state_local.finished_out.
1212 digest),
1213 &ssl->handshake->state_local.finished_out.digest_len,
1214 ssl->conf->endpoint);
XiaokangQian74af2a82021-09-22 07:40:30 +00001215
Gilles Peskine449bd832023-01-11 14:50:10 +01001216 if (ret != 0) {
1217 MBEDTLS_SSL_DEBUG_RET(1, "calculate_verify_data failed", ret);
1218 return ret;
XiaokangQian74af2a82021-09-22 07:40:30 +00001219 }
1220
Gilles Peskine449bd832023-01-11 14:50:10 +01001221 return 0;
XiaokangQian74af2a82021-09-22 07:40:30 +00001222}
1223
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001224MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01001225static int ssl_tls13_write_finished_message_body(mbedtls_ssl_context *ssl,
1226 unsigned char *buf,
1227 unsigned char *end,
1228 size_t *out_len)
XiaokangQian74af2a82021-09-22 07:40:30 +00001229{
XiaokangQian8773aa02021-11-10 07:33:09 +00001230 size_t verify_data_len = ssl->handshake->state_local.finished_out.digest_len;
XiaokangQian0fa66432021-11-15 03:33:57 +00001231 /*
1232 * struct {
1233 * opaque verify_data[Hash.length];
1234 * } Finished;
1235 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001236 MBEDTLS_SSL_CHK_BUF_PTR(buf, end, verify_data_len);
XiaokangQian74af2a82021-09-22 07:40:30 +00001237
Gilles Peskine449bd832023-01-11 14:50:10 +01001238 memcpy(buf, ssl->handshake->state_local.finished_out.digest,
1239 verify_data_len);
XiaokangQian74af2a82021-09-22 07:40:30 +00001240
Xiaofei Baid25fab62021-12-02 06:36:27 +00001241 *out_len = verify_data_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01001242 return 0;
XiaokangQian74af2a82021-09-22 07:40:30 +00001243}
XiaokangQianc5c39d52021-11-09 11:55:10 +00001244
XiaokangQian35dc6252021-11-11 08:16:19 +00001245/* Main entry point: orchestrates the other functions */
Gilles Peskine449bd832023-01-11 14:50:10 +01001246int mbedtls_ssl_tls13_write_finished_message(mbedtls_ssl_context *ssl)
XiaokangQian35dc6252021-11-11 08:16:19 +00001247{
1248 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1249 unsigned char *buf;
1250 size_t buf_len, msg_len;
1251
Gilles Peskine449bd832023-01-11 14:50:10 +01001252 MBEDTLS_SSL_DEBUG_MSG(2, ("=> write finished message"));
XiaokangQian35dc6252021-11-11 08:16:19 +00001253
Gilles Peskine449bd832023-01-11 14:50:10 +01001254 MBEDTLS_SSL_PROC_CHK(ssl_tls13_prepare_finished_message(ssl));
XiaokangQiandce82242021-11-15 06:01:26 +00001255
Gilles Peskine449bd832023-01-11 14:50:10 +01001256 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_start_handshake_msg(ssl,
1257 MBEDTLS_SSL_HS_FINISHED, &buf, &buf_len));
XiaokangQian35dc6252021-11-11 08:16:19 +00001258
Gilles Peskine449bd832023-01-11 14:50:10 +01001259 MBEDTLS_SSL_PROC_CHK(ssl_tls13_write_finished_message_body(
1260 ssl, buf, buf + buf_len, &msg_len));
XiaokangQian35dc6252021-11-11 08:16:19 +00001261
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +01001262 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_add_hs_msg_to_checksum(ssl,
Manuel Pégourié-Gonnard43cc1272023-02-06 11:48:19 +01001263 MBEDTLS_SSL_HS_FINISHED, buf, msg_len));
XiaokangQian35dc6252021-11-11 08:16:19 +00001264
Gilles Peskine449bd832023-01-11 14:50:10 +01001265 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_finish_handshake_msg(
1266 ssl, buf_len, msg_len));
XiaokangQian35dc6252021-11-11 08:16:19 +00001267cleanup:
1268
Gilles Peskine449bd832023-01-11 14:50:10 +01001269 MBEDTLS_SSL_DEBUG_MSG(2, ("<= write finished message"));
1270 return ret;
XiaokangQian35dc6252021-11-11 08:16:19 +00001271}
1272
Gilles Peskine449bd832023-01-11 14:50:10 +01001273void mbedtls_ssl_tls13_handshake_wrapup(mbedtls_ssl_context *ssl)
Jerry Yu378254d2021-10-30 21:44:47 +08001274{
1275
Gilles Peskine449bd832023-01-11 14:50:10 +01001276 MBEDTLS_SSL_DEBUG_MSG(3, ("=> handshake wrapup"));
Jerry Yu378254d2021-10-30 21:44:47 +08001277
Gilles Peskine449bd832023-01-11 14:50:10 +01001278 MBEDTLS_SSL_DEBUG_MSG(1, ("Switch to application keys for inbound traffic"));
1279 mbedtls_ssl_set_inbound_transform(ssl, ssl->transform_application);
Jerry Yue8c1fca2022-05-18 14:48:56 +08001280
Gilles Peskine449bd832023-01-11 14:50:10 +01001281 MBEDTLS_SSL_DEBUG_MSG(1, ("Switch to application keys for outbound traffic"));
1282 mbedtls_ssl_set_outbound_transform(ssl, ssl->transform_application);
Jerry Yue8c1fca2022-05-18 14:48:56 +08001283
Jerry Yu378254d2021-10-30 21:44:47 +08001284 /*
Jerry Yucfe64f02021-11-15 13:54:06 +08001285 * Free the previous session and switch to the current one.
Jerry Yu378254d2021-10-30 21:44:47 +08001286 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001287 if (ssl->session) {
1288 mbedtls_ssl_session_free(ssl->session);
1289 mbedtls_free(ssl->session);
Jerry Yu378254d2021-10-30 21:44:47 +08001290 }
1291 ssl->session = ssl->session_negotiate;
1292 ssl->session_negotiate = NULL;
1293
Gilles Peskine449bd832023-01-11 14:50:10 +01001294 MBEDTLS_SSL_DEBUG_MSG(3, ("<= handshake wrapup"));
Jerry Yu378254d2021-10-30 21:44:47 +08001295}
1296
Ronald Cron49ad6192021-11-24 16:25:31 +01001297/*
1298 *
1299 * STATE HANDLING: Write ChangeCipherSpec
1300 *
1301 */
1302#if defined(MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001303MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01001304static int ssl_tls13_write_change_cipher_spec_body(mbedtls_ssl_context *ssl,
1305 unsigned char *buf,
1306 unsigned char *end,
1307 size_t *olen)
Ronald Cron49ad6192021-11-24 16:25:31 +01001308{
1309 ((void) ssl);
1310
Gilles Peskine449bd832023-01-11 14:50:10 +01001311 MBEDTLS_SSL_CHK_BUF_PTR(buf, end, 1);
Ronald Cron49ad6192021-11-24 16:25:31 +01001312 buf[0] = 1;
1313 *olen = 1;
1314
Gilles Peskine449bd832023-01-11 14:50:10 +01001315 return 0;
Ronald Cron49ad6192021-11-24 16:25:31 +01001316}
1317
Gilles Peskine449bd832023-01-11 14:50:10 +01001318int mbedtls_ssl_tls13_write_change_cipher_spec(mbedtls_ssl_context *ssl)
Ronald Cron49ad6192021-11-24 16:25:31 +01001319{
1320 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1321
Gilles Peskine449bd832023-01-11 14:50:10 +01001322 MBEDTLS_SSL_DEBUG_MSG(2, ("=> write change cipher spec"));
Ronald Cron49ad6192021-11-24 16:25:31 +01001323
Ronald Cron49ad6192021-11-24 16:25:31 +01001324 /* Write CCS message */
Gilles Peskine449bd832023-01-11 14:50:10 +01001325 MBEDTLS_SSL_PROC_CHK(ssl_tls13_write_change_cipher_spec_body(
1326 ssl, ssl->out_msg,
1327 ssl->out_msg + MBEDTLS_SSL_OUT_CONTENT_LEN,
1328 &ssl->out_msglen));
Ronald Cron49ad6192021-11-24 16:25:31 +01001329
1330 ssl->out_msgtype = MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC;
1331
Ronald Cron49ad6192021-11-24 16:25:31 +01001332 /* Dispatch message */
Gilles Peskine449bd832023-01-11 14:50:10 +01001333 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_write_record(ssl, 0));
Ronald Cron49ad6192021-11-24 16:25:31 +01001334
1335cleanup:
1336
Gilles Peskine449bd832023-01-11 14:50:10 +01001337 MBEDTLS_SSL_DEBUG_MSG(2, ("<= write change cipher spec"));
1338 return ret;
Ronald Cron49ad6192021-11-24 16:25:31 +01001339}
1340
1341#endif /* MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE */
1342
Xiaokang Qianecc29482022-11-02 07:52:47 +00001343/* Early Data Indication Extension
Xiaokang Qian0e97d4d2022-10-24 11:12:51 +00001344 *
Xiaokang Qian0e97d4d2022-10-24 11:12:51 +00001345 * struct {
1346 * select ( Handshake.msg_type ) {
Xiaokang Qianecc29482022-11-02 07:52:47 +00001347 * ...
Xiaokang Qian0e97d4d2022-10-24 11:12:51 +00001348 * case client_hello: Empty;
1349 * case encrypted_extensions: Empty;
1350 * };
1351 * } EarlyDataIndication;
1352 */
1353#if defined(MBEDTLS_SSL_EARLY_DATA)
Gilles Peskine449bd832023-01-11 14:50:10 +01001354int mbedtls_ssl_tls13_write_early_data_ext(mbedtls_ssl_context *ssl,
1355 unsigned char *buf,
1356 const unsigned char *end,
1357 size_t *out_len)
Xiaokang Qian0e97d4d2022-10-24 11:12:51 +00001358{
1359 unsigned char *p = buf;
1360 *out_len = 0;
1361 ((void) ssl);
1362
Gilles Peskine449bd832023-01-11 14:50:10 +01001363 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 4);
Xiaokang Qian0e97d4d2022-10-24 11:12:51 +00001364
Gilles Peskine449bd832023-01-11 14:50:10 +01001365 MBEDTLS_PUT_UINT16_BE(MBEDTLS_TLS_EXT_EARLY_DATA, p, 0);
1366 MBEDTLS_PUT_UINT16_BE(0, p, 2);
Xiaokang Qian0e97d4d2022-10-24 11:12:51 +00001367
1368 *out_len = 4;
Xiaokang Qian2cd5ce02022-11-15 10:33:53 +00001369
Gilles Peskine449bd832023-01-11 14:50:10 +01001370 mbedtls_ssl_tls13_set_hs_sent_ext_mask(ssl, MBEDTLS_TLS_EXT_EARLY_DATA);
Xiaokang Qian2cd5ce02022-11-15 10:33:53 +00001371
Gilles Peskine449bd832023-01-11 14:50:10 +01001372 return 0;
Xiaokang Qian0e97d4d2022-10-24 11:12:51 +00001373}
1374#endif /* MBEDTLS_SSL_EARLY_DATA */
1375
XiaokangQian78b1fa72022-01-19 06:56:30 +00001376/* Reset SSL context and update hash for handling HRR.
1377 *
1378 * Replace Transcript-Hash(X) by
1379 * Transcript-Hash( message_hash ||
1380 * 00 00 Hash.length ||
1381 * X )
1382 * A few states of the handshake are preserved, including:
1383 * - session ID
1384 * - session ticket
1385 * - negotiated ciphersuite
1386 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001387int mbedtls_ssl_reset_transcript_for_hrr(mbedtls_ssl_context *ssl)
XiaokangQian78b1fa72022-01-19 06:56:30 +00001388{
1389 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Przemyslaw Stekielda645252022-09-14 12:50:51 +02001390 unsigned char hash_transcript[PSA_HASH_MAX_SIZE + 4];
XiaokangQian0ece9982022-01-24 08:56:23 +00001391 size_t hash_len;
Xiaokang Qian6b980012023-02-07 03:17:45 +00001392 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
1393 ssl->handshake->ciphersuite_info;
XiaokangQian78b1fa72022-01-19 06:56:30 +00001394
Gilles Peskine449bd832023-01-11 14:50:10 +01001395 MBEDTLS_SSL_DEBUG_MSG(3, ("Reset SSL session for HRR"));
XiaokangQian78b1fa72022-01-19 06:56:30 +00001396
Gilles Peskine449bd832023-01-11 14:50:10 +01001397 ret = mbedtls_ssl_get_handshake_transcript(ssl, ciphersuite_info->mac,
1398 hash_transcript + 4,
1399 PSA_HASH_MAX_SIZE,
1400 &hash_len);
1401 if (ret != 0) {
Manuel Pégourié-Gonnardda7979b2023-02-21 09:31:10 +01001402 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_get_handshake_transcript", ret);
Gilles Peskine449bd832023-01-11 14:50:10 +01001403 return ret;
XiaokangQian0ece9982022-01-24 08:56:23 +00001404 }
1405
1406 hash_transcript[0] = MBEDTLS_SSL_HS_MESSAGE_HASH;
1407 hash_transcript[1] = 0;
1408 hash_transcript[2] = 0;
1409 hash_transcript[3] = (unsigned char) hash_len;
1410
1411 hash_len += 4;
1412
Manuel Pégourié-Gonnardda7979b2023-02-21 09:31:10 +01001413 MBEDTLS_SSL_DEBUG_BUF(4, "Truncated handshake transcript",
1414 hash_transcript, hash_len);
1415
Manuel Pégourié-Gonnardd7a7a232023-02-05 10:26:49 +01001416 /* Reset running hash and replace it with a hash of the transcript */
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +01001417 ret = mbedtls_ssl_reset_checksum(ssl);
1418 if (ret != 0) {
1419 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_reset_checksum", ret);
1420 return ret;
1421 }
1422 ret = ssl->handshake->update_checksum(ssl, hash_transcript, hash_len);
1423 if (ret != 0) {
1424 MBEDTLS_SSL_DEBUG_RET(1, "update_checksum", ret);
1425 return ret;
1426 }
Przemyslaw Stekiel4b3fff42022-02-14 16:39:52 +01001427
Gilles Peskine449bd832023-01-11 14:50:10 +01001428 return ret;
XiaokangQian78b1fa72022-01-19 06:56:30 +00001429}
1430
XiaokangQian9b5d04b2022-04-10 10:20:43 +00001431#if defined(MBEDTLS_ECDH_C)
XiaokangQian7807f9f2022-02-15 10:04:37 +00001432
Gilles Peskine449bd832023-01-11 14:50:10 +01001433int mbedtls_ssl_tls13_read_public_ecdhe_share(mbedtls_ssl_context *ssl,
1434 const unsigned char *buf,
1435 size_t buf_len)
XiaokangQian7807f9f2022-02-15 10:04:37 +00001436{
Gilles Peskine449bd832023-01-11 14:50:10 +01001437 uint8_t *p = (uint8_t *) buf;
XiaokangQiancfd925f2022-04-14 07:10:37 +00001438 const uint8_t *end = buf + buf_len;
XiaokangQian9b5d04b2022-04-10 10:20:43 +00001439 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001440
XiaokangQian9b5d04b2022-04-10 10:20:43 +00001441 /* Get size of the TLS opaque key_exchange field of the KeyShareEntry struct. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001442 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, 2);
1443 uint16_t peerkey_len = MBEDTLS_GET_UINT16_BE(p, 0);
XiaokangQian9b5d04b2022-04-10 10:20:43 +00001444 p += 2;
XiaokangQian3207a322022-02-23 03:15:27 +00001445
XiaokangQian9b5d04b2022-04-10 10:20:43 +00001446 /* Check if key size is consistent with given buffer length. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001447 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, peerkey_len);
XiaokangQian9b5d04b2022-04-10 10:20:43 +00001448
1449 /* Store peer's ECDH public key. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001450 memcpy(handshake->ecdh_psa_peerkey, p, peerkey_len);
XiaokangQian9b5d04b2022-04-10 10:20:43 +00001451 handshake->ecdh_psa_peerkey_len = peerkey_len;
1452
Gilles Peskine449bd832023-01-11 14:50:10 +01001453 return 0;
XiaokangQian3207a322022-02-23 03:15:27 +00001454}
Jerry Yu89e103c2022-03-30 22:43:29 +08001455
1456int mbedtls_ssl_tls13_generate_and_write_ecdh_key_exchange(
Gilles Peskine449bd832023-01-11 14:50:10 +01001457 mbedtls_ssl_context *ssl,
1458 uint16_t named_group,
1459 unsigned char *buf,
1460 unsigned char *end,
1461 size_t *out_len)
Jerry Yu89e103c2022-03-30 22:43:29 +08001462{
1463 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
1464 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
1465 psa_key_attributes_t key_attributes;
1466 size_t own_pubkey_len;
1467 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
Valerio Setti40d9ca92023-01-04 16:08:04 +01001468 psa_ecc_family_t ec_psa_family = 0;
1469 size_t ec_bits = 0;
Jerry Yu89e103c2022-03-30 22:43:29 +08001470
Gilles Peskine449bd832023-01-11 14:50:10 +01001471 MBEDTLS_SSL_DEBUG_MSG(1, ("Perform PSA-based ECDH computation."));
Jerry Yu89e103c2022-03-30 22:43:29 +08001472
Valerio Setti40d9ca92023-01-04 16:08:04 +01001473 /* Convert EC's TLS ID to PSA key type. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001474 if (mbedtls_ssl_get_psa_curve_info_from_tls_id(named_group,
1475 &ec_psa_family,
1476 &ec_bits) == PSA_ERROR_NOT_SUPPORTED) {
1477 return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
Valerio Setti40d9ca92023-01-04 16:08:04 +01001478 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001479 handshake->ecdh_psa_type = PSA_KEY_TYPE_ECC_KEY_PAIR(ec_psa_family);
Valerio Setti40d9ca92023-01-04 16:08:04 +01001480 ssl->handshake->ecdh_bits = ec_bits;
Jerry Yu89e103c2022-03-30 22:43:29 +08001481
1482 key_attributes = psa_key_attributes_init();
Gilles Peskine449bd832023-01-11 14:50:10 +01001483 psa_set_key_usage_flags(&key_attributes, PSA_KEY_USAGE_DERIVE);
1484 psa_set_key_algorithm(&key_attributes, PSA_ALG_ECDH);
1485 psa_set_key_type(&key_attributes, handshake->ecdh_psa_type);
1486 psa_set_key_bits(&key_attributes, handshake->ecdh_bits);
Jerry Yu89e103c2022-03-30 22:43:29 +08001487
1488 /* Generate ECDH private key. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001489 status = psa_generate_key(&key_attributes,
1490 &handshake->ecdh_psa_privkey);
1491 if (status != PSA_SUCCESS) {
Andrzej Kurek8a045ce2022-12-23 11:00:06 -05001492 ret = PSA_TO_MBEDTLS_ERR(status);
Gilles Peskine449bd832023-01-11 14:50:10 +01001493 MBEDTLS_SSL_DEBUG_RET(1, "psa_generate_key", ret);
1494 return ret;
Jerry Yu89e103c2022-03-30 22:43:29 +08001495
1496 }
1497
1498 /* Export the public part of the ECDH private key from PSA. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001499 status = psa_export_public_key(handshake->ecdh_psa_privkey,
1500 buf, (size_t) (end - buf),
1501 &own_pubkey_len);
1502 if (status != PSA_SUCCESS) {
Andrzej Kurek8a045ce2022-12-23 11:00:06 -05001503 ret = PSA_TO_MBEDTLS_ERR(status);
Gilles Peskine449bd832023-01-11 14:50:10 +01001504 MBEDTLS_SSL_DEBUG_RET(1, "psa_export_public_key", ret);
1505 return ret;
Jerry Yu89e103c2022-03-30 22:43:29 +08001506
1507 }
1508
1509 *out_len = own_pubkey_len;
1510
Gilles Peskine449bd832023-01-11 14:50:10 +01001511 return 0;
Jerry Yu89e103c2022-03-30 22:43:29 +08001512}
XiaokangQian9b5d04b2022-04-10 10:20:43 +00001513#endif /* MBEDTLS_ECDH_C */
XiaokangQian7807f9f2022-02-15 10:04:37 +00001514
Jerry Yu0c354a22022-08-29 15:25:36 +08001515/* RFC 8446 section 4.2
1516 *
1517 * If an implementation receives an extension which it recognizes and which is
1518 * not specified for the message in which it appears, it MUST abort the handshake
1519 * with an "illegal_parameter" alert.
1520 *
1521 */
Jerry Yuc4bf5d62022-10-29 09:08:47 +08001522int mbedtls_ssl_tls13_check_received_extension(
Gilles Peskine449bd832023-01-11 14:50:10 +01001523 mbedtls_ssl_context *ssl,
1524 int hs_msg_type,
1525 unsigned int received_extension_type,
1526 uint32_t hs_msg_allowed_extensions_mask)
Jerry Yu0c354a22022-08-29 15:25:36 +08001527{
Jerry Yudf0ad652022-10-31 13:20:57 +08001528 uint32_t extension_mask = mbedtls_ssl_get_extension_mask(
Gilles Peskine449bd832023-01-11 14:50:10 +01001529 received_extension_type);
Jerry Yu0c354a22022-08-29 15:25:36 +08001530
Jerry Yu79aa7212022-11-08 21:30:21 +08001531 MBEDTLS_SSL_PRINT_EXT(
Gilles Peskine449bd832023-01-11 14:50:10 +01001532 3, hs_msg_type, received_extension_type, "received");
Jerry Yu0c354a22022-08-29 15:25:36 +08001533
Gilles Peskine449bd832023-01-11 14:50:10 +01001534 if ((extension_mask & hs_msg_allowed_extensions_mask) == 0) {
Jerry Yu79aa7212022-11-08 21:30:21 +08001535 MBEDTLS_SSL_PRINT_EXT(
Gilles Peskine449bd832023-01-11 14:50:10 +01001536 3, hs_msg_type, received_extension_type, "is illegal");
Jerry Yu0c354a22022-08-29 15:25:36 +08001537 MBEDTLS_SSL_PEND_FATAL_ALERT(
1538 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER,
Gilles Peskine449bd832023-01-11 14:50:10 +01001539 MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER);
1540 return MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER;
Jerry Yu0c354a22022-08-29 15:25:36 +08001541 }
1542
1543 ssl->handshake->received_extensions |= extension_mask;
Jerry Yuc4bf5d62022-10-29 09:08:47 +08001544 /*
1545 * If it is a message containing extension responses, check that we
1546 * previously sent the extension.
1547 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001548 switch (hs_msg_type) {
Jerry Yu0c354a22022-08-29 15:25:36 +08001549 case MBEDTLS_SSL_HS_SERVER_HELLO:
Jerry Yudf0ad652022-10-31 13:20:57 +08001550 case MBEDTLS_SSL_TLS1_3_HS_HELLO_RETRY_REQUEST:
Jerry Yu0c354a22022-08-29 15:25:36 +08001551 case MBEDTLS_SSL_HS_ENCRYPTED_EXTENSIONS:
1552 case MBEDTLS_SSL_HS_CERTIFICATE:
Jerry Yuc4bf5d62022-10-29 09:08:47 +08001553 /* Check if the received extension is sent by peer message.*/
Gilles Peskine449bd832023-01-11 14:50:10 +01001554 if ((ssl->handshake->sent_extensions & extension_mask) != 0) {
1555 return 0;
1556 }
Jerry Yu0c354a22022-08-29 15:25:36 +08001557 break;
1558 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01001559 return 0;
Jerry Yu0c354a22022-08-29 15:25:36 +08001560 }
1561
Jerry Yu79aa7212022-11-08 21:30:21 +08001562 MBEDTLS_SSL_PRINT_EXT(
Gilles Peskine449bd832023-01-11 14:50:10 +01001563 3, hs_msg_type, received_extension_type, "is unsupported");
Jerry Yu0c354a22022-08-29 15:25:36 +08001564 MBEDTLS_SSL_PEND_FATAL_ALERT(
1565 MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_EXT,
Gilles Peskine449bd832023-01-11 14:50:10 +01001566 MBEDTLS_ERR_SSL_UNSUPPORTED_EXTENSION);
1567 return MBEDTLS_ERR_SSL_UNSUPPORTED_EXTENSION;
Jerry Yu0c354a22022-08-29 15:25:36 +08001568}
1569
Jan Bruckner151f6422023-02-10 12:45:19 +01001570#if defined(MBEDTLS_SSL_RECORD_SIZE_LIMIT)
1571/* From RFC 8449:
1572 * The ExtensionData of the "record_size_limit" extension is
1573 * RecordSizeLimit:
1574 * uint16 RecordSizeLimit;
1575 */
1576MBEDTLS_CHECK_RETURN_CRITICAL
1577int mbedtls_ssl_tls13_parse_record_size_limit_ext(mbedtls_ssl_context *ssl,
1578 const unsigned char *buf,
1579 const unsigned char *end)
1580{
1581 const ptrdiff_t extension_data_len = end - buf;
1582 if (extension_data_len != 2) {
1583 MBEDTLS_SSL_DEBUG_MSG(2,
1584 ("record_size_limit extension has invalid length: %td Bytes",
1585 extension_data_len));
1586
1587 MBEDTLS_SSL_PEND_FATAL_ALERT(
1588 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER,
1589 MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER);
1590 return MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER;
1591 }
1592
1593 const unsigned char *p = buf;
1594 uint16_t record_size_limit;
1595
1596 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, 2);
1597 record_size_limit = MBEDTLS_GET_UINT16_BE(p, 0);
1598
1599 MBEDTLS_SSL_DEBUG_MSG(2, ("RecordSizeLimit: %u Bytes", record_size_limit));
1600
1601 /* RFC 8449 section 4
1602 *
1603 * Endpoints MUST NOT send a "record_size_limit" extension with a value
1604 * smaller than 64. An endpoint MUST treat receipt of a smaller value
1605 * as a fatal error and generate an "illegal_parameter" alert.
1606 */
1607 if (record_size_limit < 64) {
1608 MBEDTLS_SSL_PEND_FATAL_ALERT(
1609 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER,
1610 MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER);
1611 return MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER;
1612 }
1613
1614 MBEDTLS_SSL_DEBUG_MSG(2,
1615 (
1616 "record_size_limit extension is still in development. Aborting handshake."));
1617
1618 MBEDTLS_SSL_PEND_FATAL_ALERT(
1619 MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_EXT,
1620 MBEDTLS_ERR_SSL_UNSUPPORTED_EXTENSION);
1621 return MBEDTLS_ERR_SSL_UNSUPPORTED_EXTENSION;
1622}
1623#endif /* MBEDTLS_SSL_RECORD_SIZE_LIMIT */
1624
Jerry Yufb4b6472022-01-27 15:03:26 +08001625#endif /* MBEDTLS_SSL_TLS_C && MBEDTLS_SSL_PROTO_TLS1_3 */