blob: f607e364cc243b42f43ec9eae12433555e956755 [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
Jerry Yufbe3e642022-04-25 19:31:51 +080039const uint8_t mbedtls_ssl_tls13_hello_retry_request_magic[
Gilles Peskine449bd832023-01-11 14:50:10 +010040 MBEDTLS_SERVER_HELLO_RANDOM_LEN] =
41{ 0xCF, 0x21, 0xAD, 0x74, 0xE5, 0x9A, 0x61, 0x11,
42 0xBE, 0x1D, 0x8C, 0x02, 0x1E, 0x65, 0xB8, 0x91,
43 0xC2, 0xA2, 0x11, 0x16, 0x7A, 0xBB, 0x8C, 0x5E,
44 0x07, 0x9E, 0x09, 0xE2, 0xC8, 0xA8, 0x33, 0x9C };
Jerry Yu93a13f22022-04-11 23:00:01 +080045
Gilles Peskine449bd832023-01-11 14:50:10 +010046int mbedtls_ssl_tls13_fetch_handshake_msg(mbedtls_ssl_context *ssl,
47 unsigned hs_type,
48 unsigned char **buf,
49 size_t *buf_len)
XiaokangQian6b226b02021-09-24 07:51:16 +000050{
51 int ret;
52
Gilles Peskine449bd832023-01-11 14:50:10 +010053 if ((ret = mbedtls_ssl_read_record(ssl, 0)) != 0) {
54 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_read_record", ret);
XiaokangQian6b226b02021-09-24 07:51:16 +000055 goto cleanup;
56 }
57
Gilles Peskine449bd832023-01-11 14:50:10 +010058 if (ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE ||
59 ssl->in_msg[0] != hs_type) {
60 MBEDTLS_SSL_DEBUG_MSG(1, ("Receive unexpected handshake message."));
61 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE,
62 MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE);
XiaokangQian6b226b02021-09-24 07:51:16 +000063 ret = MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE;
64 goto cleanup;
65 }
66
XiaokangQian05420b12021-09-29 08:46:37 +000067 /*
68 * Jump handshake header (4 bytes, see Section 4 of RFC 8446).
69 * ...
70 * HandshakeType msg_type;
71 * uint24 length;
72 * ...
73 */
Xiaofei Baieef15042021-11-18 07:29:56 +000074 *buf = ssl->in_msg + 4;
75 *buf_len = ssl->in_hslen - 4;
XiaokangQian6b226b02021-09-24 07:51:16 +000076
XiaokangQian6b226b02021-09-24 07:51:16 +000077cleanup:
78
Gilles Peskine449bd832023-01-11 14:50:10 +010079 return ret;
XiaokangQian6b226b02021-09-24 07:51:16 +000080}
81
Ronald Cron928cbd32022-10-04 16:14:26 +020082#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED)
Jerry Yu30b071c2021-09-12 20:16:03 +080083/*
Jerry Yu30b071c2021-09-12 20:16:03 +080084 * STATE HANDLING: Read CertificateVerify
85 */
Jerry Yud0fc5852021-10-29 11:09:06 +080086/* Macro to express the maximum length of the verify structure.
Jerry Yu30b071c2021-09-12 20:16:03 +080087 *
88 * The structure is computed per TLS 1.3 specification as:
89 * - 64 bytes of octet 32,
90 * - 33 bytes for the context string
91 * (which is either "TLS 1.3, client CertificateVerify"
92 * or "TLS 1.3, server CertificateVerify"),
Jerry Yud0fc5852021-10-29 11:09:06 +080093 * - 1 byte for the octet 0x0, which serves as a separator,
Jerry Yu30b071c2021-09-12 20:16:03 +080094 * - 32 or 48 bytes for the Transcript-Hash(Handshake Context, Certificate)
95 * (depending on the size of the transcript_hash)
96 *
97 * This results in a total size of
98 * - 130 bytes for a SHA256-based transcript hash, or
99 * (64 + 33 + 1 + 32 bytes)
100 * - 146 bytes for a SHA384-based transcript hash.
101 * (64 + 33 + 1 + 48 bytes)
102 *
103 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100104#define SSL_VERIFY_STRUCT_MAX_SIZE (64 + \
105 33 + \
106 1 + \
107 MBEDTLS_TLS1_3_MD_MAX_SIZE \
108 )
Jerry Yu30b071c2021-09-12 20:16:03 +0800109
Jerry Yu0b32c502021-10-28 13:41:59 +0800110/*
111 * The ssl_tls13_create_verify_structure() creates the verify structure.
112 * As input, it requires the transcript hash.
113 *
114 * The caller has to ensure that the buffer has size at least
115 * SSL_VERIFY_STRUCT_MAX_SIZE bytes.
116 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100117static void ssl_tls13_create_verify_structure(const unsigned char *transcript_hash,
118 size_t transcript_hash_len,
119 unsigned char *verify_buffer,
120 size_t *verify_buffer_len,
121 int from)
Jerry Yu0b32c502021-10-28 13:41:59 +0800122{
123 size_t idx;
Jerry Yu30b071c2021-09-12 20:16:03 +0800124
Jerry Yu0b32c502021-10-28 13:41:59 +0800125 /* RFC 8446, Section 4.4.3:
126 *
127 * The digital signature [in the CertificateVerify message] is then
128 * computed over the concatenation of:
129 * - A string that consists of octet 32 (0x20) repeated 64 times
130 * - The context string
131 * - A single 0 byte which serves as the separator
132 * - The content to be signed
133 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100134 memset(verify_buffer, 0x20, 64);
Jerry Yu0b32c502021-10-28 13:41:59 +0800135 idx = 64;
136
Gilles Peskine449bd832023-01-11 14:50:10 +0100137 if (from == MBEDTLS_SSL_IS_CLIENT) {
138 memcpy(verify_buffer + idx, MBEDTLS_SSL_TLS1_3_LBL_WITH_LEN(client_cv));
139 idx += MBEDTLS_SSL_TLS1_3_LBL_LEN(client_cv);
140 } else { /* from == MBEDTLS_SSL_IS_SERVER */
141 memcpy(verify_buffer + idx, MBEDTLS_SSL_TLS1_3_LBL_WITH_LEN(server_cv));
142 idx += MBEDTLS_SSL_TLS1_3_LBL_LEN(server_cv);
Jerry Yu0b32c502021-10-28 13:41:59 +0800143 }
144
145 verify_buffer[idx++] = 0x0;
146
Gilles Peskine449bd832023-01-11 14:50:10 +0100147 memcpy(verify_buffer + idx, transcript_hash, transcript_hash_len);
Jerry Yu0b32c502021-10-28 13:41:59 +0800148 idx += transcript_hash_len;
149
150 *verify_buffer_len = idx;
151}
152
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200153MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100154static int ssl_tls13_parse_certificate_verify(mbedtls_ssl_context *ssl,
155 const unsigned char *buf,
156 const unsigned char *end,
157 const unsigned char *verify_buffer,
158 size_t verify_buffer_len)
Jerry Yu30b071c2021-09-12 20:16:03 +0800159{
160 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
pespaceka1378102022-04-26 15:03:11 +0200161 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Jerry Yu30b071c2021-09-12 20:16:03 +0800162 const unsigned char *p = buf;
163 uint16_t algorithm;
Jerry Yu30b071c2021-09-12 20:16:03 +0800164 size_t signature_len;
165 mbedtls_pk_type_t sig_alg;
166 mbedtls_md_type_t md_alg;
pespaceka1378102022-04-26 15:03:11 +0200167 psa_algorithm_t hash_alg = PSA_ALG_NONE;
168 unsigned char verify_hash[PSA_HASH_MAX_SIZE];
Jerry Yu30b071c2021-09-12 20:16:03 +0800169 size_t verify_hash_len;
170
Xiaofei Baid25fab62021-12-02 06:36:27 +0000171 void const *options = NULL;
XiaokangQian82d34cc2021-11-03 08:51:56 +0000172#if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT)
Xiaofei Baid25fab62021-12-02 06:36:27 +0000173 mbedtls_pk_rsassa_pss_options rsassa_pss_options;
XiaokangQian82d34cc2021-11-03 08:51:56 +0000174#endif /* MBEDTLS_X509_RSASSA_PSS_SUPPORT */
175
Jerry Yu30b071c2021-09-12 20:16:03 +0800176 /*
177 * struct {
178 * SignatureScheme algorithm;
179 * opaque signature<0..2^16-1>;
180 * } CertificateVerify;
181 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100182 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, 2);
183 algorithm = MBEDTLS_GET_UINT16_BE(p, 0);
Jerry Yu30b071c2021-09-12 20:16:03 +0800184 p += 2;
185
186 /* RFC 8446 section 4.4.3
187 *
188 * If the CertificateVerify message is sent by a server, the signature algorithm
189 * MUST be one offered in the client's "signature_algorithms" extension unless
190 * no valid certificate chain can be produced without unsupported algorithms
191 *
192 * RFC 8446 section 4.4.2.2
193 *
194 * If the client cannot construct an acceptable chain using the provided
195 * certificates and decides to abort the handshake, then it MUST abort the handshake
196 * with an appropriate certificate-related alert (by default, "unsupported_certificate").
197 *
Jerry Yu6f87f252021-10-29 20:12:51 +0800198 * Check if algorithm is an offered signature algorithm.
Jerry Yu30b071c2021-09-12 20:16:03 +0800199 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100200 if (!mbedtls_ssl_sig_alg_is_offered(ssl, algorithm)) {
Jerry Yu982d9e52021-10-14 15:59:37 +0800201 /* algorithm not in offered signature algorithms list */
Gilles Peskine449bd832023-01-11 14:50:10 +0100202 MBEDTLS_SSL_DEBUG_MSG(1, ("Received signature algorithm(%04x) is not "
203 "offered.",
204 (unsigned int) algorithm));
Jerry Yu6f87f252021-10-29 20:12:51 +0800205 goto error;
Jerry Yu30b071c2021-09-12 20:16:03 +0800206 }
207
Gilles Peskine449bd832023-01-11 14:50:10 +0100208 if (mbedtls_ssl_get_pk_type_and_md_alg_from_sig_alg(
209 algorithm, &sig_alg, &md_alg) != 0) {
Jerry Yu8c338862022-03-23 13:34:04 +0800210 goto error;
Jerry Yu30b071c2021-09-12 20:16:03 +0800211 }
212
Gilles Peskine449bd832023-01-11 14:50:10 +0100213 hash_alg = mbedtls_hash_info_psa_from_md(md_alg);
214 if (hash_alg == 0) {
pespaceka1378102022-04-26 15:03:11 +0200215 goto error;
216 }
217
Gilles Peskine449bd832023-01-11 14:50:10 +0100218 MBEDTLS_SSL_DEBUG_MSG(3, ("Certificate Verify: Signature algorithm ( %04x )",
219 (unsigned int) algorithm));
Jerry Yu30b071c2021-09-12 20:16:03 +0800220
221 /*
222 * Check the certificate's key type matches the signature alg
223 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100224 if (!mbedtls_pk_can_do(&ssl->session_negotiate->peer_cert->pk, sig_alg)) {
225 MBEDTLS_SSL_DEBUG_MSG(1, ("signature algorithm doesn't match cert key"));
Jerry Yu6f87f252021-10-29 20:12:51 +0800226 goto error;
Jerry Yu30b071c2021-09-12 20:16:03 +0800227 }
228
Gilles Peskine449bd832023-01-11 14:50:10 +0100229 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, 2);
230 signature_len = MBEDTLS_GET_UINT16_BE(p, 0);
Jerry Yu30b071c2021-09-12 20:16:03 +0800231 p += 2;
Gilles Peskine449bd832023-01-11 14:50:10 +0100232 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, signature_len);
Jerry Yu30b071c2021-09-12 20:16:03 +0800233
Gilles Peskine449bd832023-01-11 14:50:10 +0100234 status = psa_hash_compute(hash_alg,
235 verify_buffer,
236 verify_buffer_len,
237 verify_hash,
238 sizeof(verify_hash),
239 &verify_hash_len);
240 if (status != PSA_SUCCESS) {
241 MBEDTLS_SSL_DEBUG_RET(1, "hash computation PSA error", status);
Jerry Yu6f87f252021-10-29 20:12:51 +0800242 goto error;
Jerry Yu133690c2021-10-25 14:01:13 +0800243 }
244
Gilles Peskine449bd832023-01-11 14:50:10 +0100245 MBEDTLS_SSL_DEBUG_BUF(3, "verify hash", verify_hash, verify_hash_len);
XiaokangQian82d34cc2021-11-03 08:51:56 +0000246#if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT)
Gilles Peskine449bd832023-01-11 14:50:10 +0100247 if (sig_alg == MBEDTLS_PK_RSASSA_PSS) {
Xiaofei Baid25fab62021-12-02 06:36:27 +0000248 rsassa_pss_options.mgf1_hash_id = md_alg;
Przemek Stekiel6a5e0182022-06-27 11:53:13 +0200249
Gilles Peskine449bd832023-01-11 14:50:10 +0100250 rsassa_pss_options.expected_salt_len = PSA_HASH_LENGTH(hash_alg);
251 options = (const void *) &rsassa_pss_options;
XiaokangQian82d34cc2021-11-03 08:51:56 +0000252 }
253#endif /* MBEDTLS_X509_RSASSA_PSS_SUPPORT */
Jerry Yu30b071c2021-09-12 20:16:03 +0800254
Gilles Peskine449bd832023-01-11 14:50:10 +0100255 if ((ret = mbedtls_pk_verify_ext(sig_alg, options,
256 &ssl->session_negotiate->peer_cert->pk,
257 md_alg, verify_hash, verify_hash_len,
258 p, signature_len)) == 0) {
259 return 0;
Jerry Yu30b071c2021-09-12 20:16:03 +0800260 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100261 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_pk_verify_ext", ret);
Jerry Yu30b071c2021-09-12 20:16:03 +0800262
Jerry Yu6f87f252021-10-29 20:12:51 +0800263error:
264 /* RFC 8446 section 4.4.3
265 *
266 * If the verification fails, the receiver MUST terminate the handshake
267 * with a "decrypt_error" alert.
Gilles Peskine449bd832023-01-11 14:50:10 +0100268 */
269 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_DECRYPT_ERROR,
270 MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE);
271 return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
Jerry Yu6f87f252021-10-29 20:12:51 +0800272
Jerry Yu30b071c2021-09-12 20:16:03 +0800273}
Ronald Cron928cbd32022-10-04 16:14:26 +0200274#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED */
Jerry Yu30b071c2021-09-12 20:16:03 +0800275
Gilles Peskine449bd832023-01-11 14:50:10 +0100276int mbedtls_ssl_tls13_process_certificate_verify(mbedtls_ssl_context *ssl)
Jerry Yu30b071c2021-09-12 20:16:03 +0800277{
Jerry Yu30b071c2021-09-12 20:16:03 +0800278
Ronald Cron928cbd32022-10-04 16:14:26 +0200279#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED)
Jerry Yuda8cdf22021-10-25 15:06:49 +0800280 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
281 unsigned char verify_buffer[SSL_VERIFY_STRUCT_MAX_SIZE];
282 size_t verify_buffer_len;
283 unsigned char transcript[MBEDTLS_TLS1_3_MD_MAX_SIZE];
284 size_t transcript_len;
285 unsigned char *buf;
286 size_t buf_len;
287
Gilles Peskine449bd832023-01-11 14:50:10 +0100288 MBEDTLS_SSL_DEBUG_MSG(2, ("=> parse certificate verify"));
Jerry Yu30b071c2021-09-12 20:16:03 +0800289
Jerry Yuda8cdf22021-10-25 15:06:49 +0800290 MBEDTLS_SSL_PROC_CHK(
Gilles Peskine449bd832023-01-11 14:50:10 +0100291 mbedtls_ssl_tls13_fetch_handshake_msg(ssl,
292 MBEDTLS_SSL_HS_CERTIFICATE_VERIFY, &buf, &buf_len));
Jerry Yu30b071c2021-09-12 20:16:03 +0800293
Jerry Yuda8cdf22021-10-25 15:06:49 +0800294 /* Need to calculate the hash of the transcript first
Jerry Yu0b32c502021-10-28 13:41:59 +0800295 * before reading the message since otherwise it gets
296 * included in the transcript
297 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100298 ret = mbedtls_ssl_get_handshake_transcript(ssl,
299 ssl->handshake->ciphersuite_info->mac,
300 transcript, sizeof(transcript),
301 &transcript_len);
302 if (ret != 0) {
Jerry Yuda8cdf22021-10-25 15:06:49 +0800303 MBEDTLS_SSL_PEND_FATAL_ALERT(
304 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR,
Gilles Peskine449bd832023-01-11 14:50:10 +0100305 MBEDTLS_ERR_SSL_INTERNAL_ERROR);
306 return ret;
Jerry Yu30b071c2021-09-12 20:16:03 +0800307 }
308
Gilles Peskine449bd832023-01-11 14:50:10 +0100309 MBEDTLS_SSL_DEBUG_BUF(3, "handshake hash", transcript, transcript_len);
Jerry Yuda8cdf22021-10-25 15:06:49 +0800310
311 /* Create verify structure */
Gilles Peskine449bd832023-01-11 14:50:10 +0100312 ssl_tls13_create_verify_structure(transcript,
313 transcript_len,
314 verify_buffer,
315 &verify_buffer_len,
316 (ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT) ?
317 MBEDTLS_SSL_IS_SERVER :
318 MBEDTLS_SSL_IS_CLIENT);
Jerry Yuda8cdf22021-10-25 15:06:49 +0800319
320 /* Process the message contents */
Gilles Peskine449bd832023-01-11 14:50:10 +0100321 MBEDTLS_SSL_PROC_CHK(ssl_tls13_parse_certificate_verify(ssl, buf,
322 buf + buf_len, verify_buffer,
323 verify_buffer_len));
Jerry Yuda8cdf22021-10-25 15:06:49 +0800324
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +0100325 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_add_hs_msg_to_checksum(ssl,
Manuel Pégourié-Gonnard43cc1272023-02-06 11:48:19 +0100326 MBEDTLS_SSL_HS_CERTIFICATE_VERIFY,
327 buf, buf_len));
Jerry Yu30b071c2021-09-12 20:16:03 +0800328
329cleanup:
330
Gilles Peskine449bd832023-01-11 14:50:10 +0100331 MBEDTLS_SSL_DEBUG_MSG(2, ("<= parse certificate verify"));
332 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_tls13_process_certificate_verify", ret);
333 return ret;
Jerry Yuda8cdf22021-10-25 15:06:49 +0800334#else
335 ((void) ssl);
Gilles Peskine449bd832023-01-11 14:50:10 +0100336 MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
337 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Ronald Cron928cbd32022-10-04 16:14:26 +0200338#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED */
Jerry Yu30b071c2021-09-12 20:16:03 +0800339}
340
341/*
Xiaofei Bai947571e2021-09-29 09:12:03 +0000342 *
XiaokangQian6b916b12022-04-25 07:29:34 +0000343 * STATE HANDLING: Incoming Certificate.
Xiaofei Bai947571e2021-09-29 09:12:03 +0000344 *
345 */
346
Ronald Cronde08cf32022-10-04 17:15:35 +0200347#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED)
Xiaofei Bai947571e2021-09-29 09:12:03 +0000348#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
349/*
350 * Structure of Certificate message:
351 *
352 * enum {
353 * X509(0),
354 * RawPublicKey(2),
355 * (255)
356 * } CertificateType;
357 *
358 * struct {
359 * select (certificate_type) {
360 * case RawPublicKey:
361 * * From RFC 7250 ASN.1_subjectPublicKeyInfo *
362 * opaque ASN1_subjectPublicKeyInfo<1..2^24-1>;
363 * case X509:
364 * opaque cert_data<1..2^24-1>;
365 * };
366 * Extension extensions<0..2^16-1>;
367 * } CertificateEntry;
368 *
369 * struct {
370 * opaque certificate_request_context<0..2^8-1>;
371 * CertificateEntry certificate_list<0..2^24-1>;
372 * } Certificate;
373 *
374 */
Xiaofei Bai79595ac2021-10-26 07:16:45 +0000375
376/* Parse certificate chain send by the server. */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200377MBEDTLS_CHECK_RETURN_CRITICAL
Ronald Crone3dac4a2022-06-10 17:21:51 +0200378MBEDTLS_STATIC_TESTABLE
Gilles Peskine449bd832023-01-11 14:50:10 +0100379int mbedtls_ssl_tls13_parse_certificate(mbedtls_ssl_context *ssl,
380 const unsigned char *buf,
381 const unsigned char *end)
Xiaofei Bai947571e2021-09-29 09:12:03 +0000382{
383 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
384 size_t certificate_request_context_len = 0;
385 size_t certificate_list_len = 0;
386 const unsigned char *p = buf;
387 const unsigned char *certificate_list_end;
Jerry Yuc4bf5d62022-10-29 09:08:47 +0800388 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
Xiaofei Bai947571e2021-09-29 09:12:03 +0000389
Gilles Peskine449bd832023-01-11 14:50:10 +0100390 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, 4);
Xiaofei Bai947571e2021-09-29 09:12:03 +0000391 certificate_request_context_len = p[0];
Gilles Peskine449bd832023-01-11 14:50:10 +0100392 certificate_list_len = MBEDTLS_GET_UINT24_BE(p, 1);
XiaokangQian63e713e2022-05-15 04:26:57 +0000393 p += 4;
Xiaofei Bai947571e2021-09-29 09:12:03 +0000394
395 /* In theory, the certificate list can be up to 2^24 Bytes, but we don't
396 * support anything beyond 2^16 = 64K.
397 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100398 if ((certificate_request_context_len != 0) ||
399 (certificate_list_len >= 0x10000)) {
400 MBEDTLS_SSL_DEBUG_MSG(1, ("bad certificate message"));
401 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR,
402 MBEDTLS_ERR_SSL_DECODE_ERROR);
403 return MBEDTLS_ERR_SSL_DECODE_ERROR;
Xiaofei Bai947571e2021-09-29 09:12:03 +0000404 }
405
406 /* In case we tried to reuse a session but it failed */
Gilles Peskine449bd832023-01-11 14:50:10 +0100407 if (ssl->session_negotiate->peer_cert != NULL) {
408 mbedtls_x509_crt_free(ssl->session_negotiate->peer_cert);
409 mbedtls_free(ssl->session_negotiate->peer_cert);
Xiaofei Bai947571e2021-09-29 09:12:03 +0000410 }
411
Gilles Peskine449bd832023-01-11 14:50:10 +0100412 if (certificate_list_len == 0) {
XiaokangQianc3017f62022-05-13 05:55:41 +0000413 ssl->session_negotiate->peer_cert = NULL;
414 ret = 0;
415 goto exit;
416 }
417
Gilles Peskine449bd832023-01-11 14:50:10 +0100418 if ((ssl->session_negotiate->peer_cert =
419 mbedtls_calloc(1, sizeof(mbedtls_x509_crt))) == NULL) {
420 MBEDTLS_SSL_DEBUG_MSG(1, ("alloc( %" MBEDTLS_PRINTF_SIZET " bytes ) failed",
421 sizeof(mbedtls_x509_crt)));
422 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR,
423 MBEDTLS_ERR_SSL_ALLOC_FAILED);
424 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
Xiaofei Bai947571e2021-09-29 09:12:03 +0000425 }
426
Gilles Peskine449bd832023-01-11 14:50:10 +0100427 mbedtls_x509_crt_init(ssl->session_negotiate->peer_cert);
Xiaofei Bai947571e2021-09-29 09:12:03 +0000428
Gilles Peskine449bd832023-01-11 14:50:10 +0100429 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, certificate_list_len);
Xiaofei Bai947571e2021-09-29 09:12:03 +0000430 certificate_list_end = p + certificate_list_len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100431 while (p < certificate_list_end) {
Xiaofei Bai947571e2021-09-29 09:12:03 +0000432 size_t cert_data_len, extensions_len;
Jerry Yu2eaa7602022-08-04 17:28:15 +0800433 const unsigned char *extensions_end;
Xiaofei Bai947571e2021-09-29 09:12:03 +0000434
Gilles Peskine449bd832023-01-11 14:50:10 +0100435 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, certificate_list_end, 3);
436 cert_data_len = MBEDTLS_GET_UINT24_BE(p, 0);
Xiaofei Bai947571e2021-09-29 09:12:03 +0000437 p += 3;
438
439 /* In theory, the CRT can be up to 2^24 Bytes, but we don't support
440 * anything beyond 2^16 = 64K. Otherwise as in the TLS 1.2 code,
441 * check that we have a minimum of 128 bytes of data, this is not
442 * clear why we need that though.
443 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100444 if ((cert_data_len < 128) || (cert_data_len >= 0x10000)) {
445 MBEDTLS_SSL_DEBUG_MSG(1, ("bad Certificate message"));
446 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR,
447 MBEDTLS_ERR_SSL_DECODE_ERROR);
448 return MBEDTLS_ERR_SSL_DECODE_ERROR;
Xiaofei Bai947571e2021-09-29 09:12:03 +0000449 }
450
Gilles Peskine449bd832023-01-11 14:50:10 +0100451 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, certificate_list_end, cert_data_len);
452 ret = mbedtls_x509_crt_parse_der(ssl->session_negotiate->peer_cert,
453 p, cert_data_len);
Xiaofei Bai947571e2021-09-29 09:12:03 +0000454
Gilles Peskine449bd832023-01-11 14:50:10 +0100455 switch (ret) {
Xiaofei Bai947571e2021-09-29 09:12:03 +0000456 case 0: /*ok*/
457 break;
458 case MBEDTLS_ERR_X509_UNKNOWN_SIG_ALG + MBEDTLS_ERR_OID_NOT_FOUND:
459 /* Ignore certificate with an unknown algorithm: maybe a
460 prior certificate was already trusted. */
461 break;
462
463 case MBEDTLS_ERR_X509_ALLOC_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100464 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR,
465 MBEDTLS_ERR_X509_ALLOC_FAILED);
466 MBEDTLS_SSL_DEBUG_RET(1, " mbedtls_x509_crt_parse_der", ret);
467 return ret;
Xiaofei Bai947571e2021-09-29 09:12:03 +0000468
469 case MBEDTLS_ERR_X509_UNKNOWN_VERSION:
Gilles Peskine449bd832023-01-11 14:50:10 +0100470 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT,
471 MBEDTLS_ERR_X509_UNKNOWN_VERSION);
472 MBEDTLS_SSL_DEBUG_RET(1, " mbedtls_x509_crt_parse_der", ret);
473 return ret;
Xiaofei Bai947571e2021-09-29 09:12:03 +0000474
475 default:
Gilles Peskine449bd832023-01-11 14:50:10 +0100476 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_BAD_CERT,
477 ret);
478 MBEDTLS_SSL_DEBUG_RET(1, " mbedtls_x509_crt_parse_der", ret);
479 return ret;
Xiaofei Bai947571e2021-09-29 09:12:03 +0000480 }
481
482 p += cert_data_len;
483
484 /* Certificate extensions length */
Gilles Peskine449bd832023-01-11 14:50:10 +0100485 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, certificate_list_end, 2);
486 extensions_len = MBEDTLS_GET_UINT16_BE(p, 0);
Xiaofei Bai947571e2021-09-29 09:12:03 +0000487 p += 2;
Gilles Peskine449bd832023-01-11 14:50:10 +0100488 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, certificate_list_end, extensions_len);
Jerry Yu2eaa7602022-08-04 17:28:15 +0800489
490 extensions_end = p + extensions_len;
Jerry Yu0d5cfb72022-10-31 14:15:48 +0800491 handshake->received_extensions = MBEDTLS_SSL_EXT_MASK_NONE;
Jerry Yu2eaa7602022-08-04 17:28:15 +0800492
Gilles Peskine449bd832023-01-11 14:50:10 +0100493 while (p < extensions_end) {
Jerry Yu2eaa7602022-08-04 17:28:15 +0800494 unsigned int extension_type;
495 size_t extension_data_len;
496
497 /*
Gilles Peskine449bd832023-01-11 14:50:10 +0100498 * struct {
499 * ExtensionType extension_type; (2 bytes)
500 * opaque extension_data<0..2^16-1>;
501 * } Extension;
502 */
503 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, extensions_end, 4);
504 extension_type = MBEDTLS_GET_UINT16_BE(p, 0);
505 extension_data_len = MBEDTLS_GET_UINT16_BE(p, 2);
Jerry Yu2eaa7602022-08-04 17:28:15 +0800506 p += 4;
507
Gilles Peskine449bd832023-01-11 14:50:10 +0100508 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, extensions_end, extension_data_len);
Jerry Yu2eaa7602022-08-04 17:28:15 +0800509
Jerry Yuc4bf5d62022-10-29 09:08:47 +0800510 ret = mbedtls_ssl_tls13_check_received_extension(
Gilles Peskine449bd832023-01-11 14:50:10 +0100511 ssl, MBEDTLS_SSL_HS_CERTIFICATE, extension_type,
512 MBEDTLS_SSL_TLS1_3_ALLOWED_EXTS_OF_CT);
513 if (ret != 0) {
514 return ret;
515 }
Jerry Yu0c354a22022-08-29 15:25:36 +0800516
Gilles Peskine449bd832023-01-11 14:50:10 +0100517 switch (extension_type) {
Jerry Yu2eaa7602022-08-04 17:28:15 +0800518 default:
Jerry Yu79aa7212022-11-08 21:30:21 +0800519 MBEDTLS_SSL_PRINT_EXT(
Jerry Yu0d5cfb72022-10-31 14:15:48 +0800520 3, MBEDTLS_SSL_HS_CERTIFICATE,
Gilles Peskine449bd832023-01-11 14:50:10 +0100521 extension_type, "( ignored )");
Jerry Yu2eaa7602022-08-04 17:28:15 +0800522 break;
523 }
524
525 p += extension_data_len;
526 }
527
Gilles Peskine449bd832023-01-11 14:50:10 +0100528 MBEDTLS_SSL_PRINT_EXTS(3, MBEDTLS_SSL_HS_CERTIFICATE,
529 handshake->received_extensions);
Xiaofei Bai947571e2021-09-29 09:12:03 +0000530 }
531
XiaokangQian63e713e2022-05-15 04:26:57 +0000532exit:
Xiaofei Bai947571e2021-09-29 09:12:03 +0000533 /* Check that all the message is consumed. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100534 if (p != end) {
535 MBEDTLS_SSL_DEBUG_MSG(1, ("bad Certificate message"));
536 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR,
537 MBEDTLS_ERR_SSL_DECODE_ERROR);
538 return MBEDTLS_ERR_SSL_DECODE_ERROR;
Xiaofei Bai947571e2021-09-29 09:12:03 +0000539 }
540
Gilles Peskine449bd832023-01-11 14:50:10 +0100541 MBEDTLS_SSL_DEBUG_CRT(3, "peer certificate", ssl->session_negotiate->peer_cert);
Xiaofei Bai947571e2021-09-29 09:12:03 +0000542
Gilles Peskine449bd832023-01-11 14:50:10 +0100543 return ret;
Xiaofei Bai947571e2021-09-29 09:12:03 +0000544}
545#else
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200546MBEDTLS_CHECK_RETURN_CRITICAL
Ronald Crone3dac4a2022-06-10 17:21:51 +0200547MBEDTLS_STATIC_TESTABLE
Gilles Peskine449bd832023-01-11 14:50:10 +0100548int mbedtls_ssl_tls13_parse_certificate(mbedtls_ssl_context *ssl,
549 const unsigned char *buf,
550 const unsigned char *end)
Xiaofei Bai947571e2021-09-29 09:12:03 +0000551{
552 ((void) ssl);
553 ((void) buf);
554 ((void) end);
Gilles Peskine449bd832023-01-11 14:50:10 +0100555 return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Xiaofei Bai947571e2021-09-29 09:12:03 +0000556}
557#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Ronald Cronde08cf32022-10-04 17:15:35 +0200558#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED */
Xiaofei Bai947571e2021-09-29 09:12:03 +0000559
Ronald Cronde08cf32022-10-04 17:15:35 +0200560#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED)
Xiaofei Bai947571e2021-09-29 09:12:03 +0000561#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Xiaofei Bai79595ac2021-10-26 07:16:45 +0000562/* Validate certificate chain sent by the server. */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200563MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100564static int ssl_tls13_validate_certificate(mbedtls_ssl_context *ssl)
Xiaofei Bai947571e2021-09-29 09:12:03 +0000565{
566 int ret = 0;
XiaokangQian989f06d2022-05-17 01:50:15 +0000567 int authmode = MBEDTLS_SSL_VERIFY_REQUIRED;
Xiaofei Bai947571e2021-09-29 09:12:03 +0000568 mbedtls_x509_crt *ca_chain;
569 mbedtls_x509_crl *ca_crl;
Ronald Cron30c5a252022-06-16 19:31:06 +0200570 const char *ext_oid;
571 size_t ext_len;
Xiaofei Baiff456022021-10-28 06:50:17 +0000572 uint32_t verify_result = 0;
Xiaofei Bai947571e2021-09-29 09:12:03 +0000573
XiaokangQian6b916b12022-04-25 07:29:34 +0000574 /* If SNI was used, overwrite authentication mode
575 * from the configuration. */
XiaokangQian989f06d2022-05-17 01:50:15 +0000576#if defined(MBEDTLS_SSL_SRV_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100577 if (ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER) {
XiaokangQian0557c942022-05-30 08:10:53 +0000578#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Gilles Peskine449bd832023-01-11 14:50:10 +0100579 if (ssl->handshake->sni_authmode != MBEDTLS_SSL_VERIFY_UNSET) {
XiaokangQian0557c942022-05-30 08:10:53 +0000580 authmode = ssl->handshake->sni_authmode;
Gilles Peskine449bd832023-01-11 14:50:10 +0100581 } else
XiaokangQian0557c942022-05-30 08:10:53 +0000582#endif
Gilles Peskine449bd832023-01-11 14:50:10 +0100583 authmode = ssl->conf->authmode;
XiaokangQian0557c942022-05-30 08:10:53 +0000584 }
XiaokangQian6b916b12022-04-25 07:29:34 +0000585#endif
586
587 /*
XiaokangQian989f06d2022-05-17 01:50:15 +0000588 * If the peer hasn't sent a certificate ( i.e. it sent
XiaokangQian6b916b12022-04-25 07:29:34 +0000589 * an empty certificate chain ), this is reflected in the peer CRT
590 * structure being unset.
591 * Check for that and handle it depending on the
XiaokangQian989f06d2022-05-17 01:50:15 +0000592 * authentication mode.
XiaokangQian6b916b12022-04-25 07:29:34 +0000593 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100594 if (ssl->session_negotiate->peer_cert == NULL) {
595 MBEDTLS_SSL_DEBUG_MSG(1, ("peer has no certificate"));
XiaokangQian989f06d2022-05-17 01:50:15 +0000596
XiaokangQian63e713e2022-05-15 04:26:57 +0000597#if defined(MBEDTLS_SSL_SRV_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100598 if (ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER) {
XiaokangQian63e713e2022-05-15 04:26:57 +0000599 /* The client was asked for a certificate but didn't send
600 * one. The client should know what's going on, so we
601 * don't send an alert.
602 */
603 ssl->session_negotiate->verify_result = MBEDTLS_X509_BADCERT_MISSING;
Gilles Peskine449bd832023-01-11 14:50:10 +0100604 if (authmode == MBEDTLS_SSL_VERIFY_OPTIONAL) {
605 return 0;
606 } else {
607 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_NO_CERT,
608 MBEDTLS_ERR_SSL_NO_CLIENT_CERTIFICATE);
609 return MBEDTLS_ERR_SSL_NO_CLIENT_CERTIFICATE;
XiaokangQian989f06d2022-05-17 01:50:15 +0000610 }
XiaokangQian63e713e2022-05-15 04:26:57 +0000611 }
XiaokangQian6b916b12022-04-25 07:29:34 +0000612#endif /* MBEDTLS_SSL_SRV_C */
613
XiaokangQianc3017f62022-05-13 05:55:41 +0000614#if defined(MBEDTLS_SSL_CLI_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100615 if (ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT) {
616 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_NO_CERT,
617 MBEDTLS_ERR_SSL_FATAL_ALERT_MESSAGE);
618 return MBEDTLS_ERR_SSL_FATAL_ALERT_MESSAGE;
XiaokangQian63e713e2022-05-15 04:26:57 +0000619 }
XiaokangQianc3017f62022-05-13 05:55:41 +0000620#endif /* MBEDTLS_SSL_CLI_C */
XiaokangQian63e713e2022-05-15 04:26:57 +0000621 }
XiaokangQian6b916b12022-04-25 07:29:34 +0000622
Xiaofei Bai947571e2021-09-29 09:12:03 +0000623#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Gilles Peskine449bd832023-01-11 14:50:10 +0100624 if (ssl->handshake->sni_ca_chain != NULL) {
Xiaofei Bai947571e2021-09-29 09:12:03 +0000625 ca_chain = ssl->handshake->sni_ca_chain;
626 ca_crl = ssl->handshake->sni_ca_crl;
Gilles Peskine449bd832023-01-11 14:50:10 +0100627 } else
Xiaofei Bai947571e2021-09-29 09:12:03 +0000628#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
629 {
630 ca_chain = ssl->conf->ca_chain;
631 ca_crl = ssl->conf->ca_crl;
632 }
633
634 /*
635 * Main check: verify certificate
636 */
637 ret = mbedtls_x509_crt_verify_with_profile(
638 ssl->session_negotiate->peer_cert,
639 ca_chain, ca_crl,
640 ssl->conf->cert_profile,
641 ssl->hostname,
Xiaofei Baiff456022021-10-28 06:50:17 +0000642 &verify_result,
Gilles Peskine449bd832023-01-11 14:50:10 +0100643 ssl->conf->f_vrfy, ssl->conf->p_vrfy);
Xiaofei Bai947571e2021-09-29 09:12:03 +0000644
Gilles Peskine449bd832023-01-11 14:50:10 +0100645 if (ret != 0) {
646 MBEDTLS_SSL_DEBUG_RET(1, "x509_verify_cert", ret);
Xiaofei Bai947571e2021-09-29 09:12:03 +0000647 }
648
649 /*
650 * Secondary checks: always done, but change 'ret' only if it was 0
651 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100652 if (ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT) {
Ronald Cron30c5a252022-06-16 19:31:06 +0200653 ext_oid = MBEDTLS_OID_SERVER_AUTH;
Gilles Peskine449bd832023-01-11 14:50:10 +0100654 ext_len = MBEDTLS_OID_SIZE(MBEDTLS_OID_SERVER_AUTH);
655 } else {
Ronald Cron30c5a252022-06-16 19:31:06 +0200656 ext_oid = MBEDTLS_OID_CLIENT_AUTH;
Gilles Peskine449bd832023-01-11 14:50:10 +0100657 ext_len = MBEDTLS_OID_SIZE(MBEDTLS_OID_CLIENT_AUTH);
Ronald Cron30c5a252022-06-16 19:31:06 +0200658 }
659
Gilles Peskine449bd832023-01-11 14:50:10 +0100660 if ((mbedtls_x509_crt_check_key_usage(
661 ssl->session_negotiate->peer_cert,
662 MBEDTLS_X509_KU_DIGITAL_SIGNATURE) != 0) ||
663 (mbedtls_x509_crt_check_extended_key_usage(
664 ssl->session_negotiate->peer_cert,
665 ext_oid, ext_len) != 0)) {
666 MBEDTLS_SSL_DEBUG_MSG(1, ("bad certificate (usage extensions)"));
667 if (ret == 0) {
Xiaofei Bai947571e2021-09-29 09:12:03 +0000668 ret = MBEDTLS_ERR_SSL_BAD_CERTIFICATE;
Gilles Peskine449bd832023-01-11 14:50:10 +0100669 }
Xiaofei Bai947571e2021-09-29 09:12:03 +0000670 }
671
XiaokangQian6b916b12022-04-25 07:29:34 +0000672 /* mbedtls_x509_crt_verify_with_profile is supposed to report a
673 * verification failure through MBEDTLS_ERR_X509_CERT_VERIFY_FAILED,
674 * with details encoded in the verification flags. All other kinds
675 * of error codes, including those from the user provided f_vrfy
676 * functions, are treated as fatal and lead to a failure of
Ronald Crone3dac4a2022-06-10 17:21:51 +0200677 * mbedtls_ssl_tls13_parse_certificate even if verification was optional.
678 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100679 if (authmode == MBEDTLS_SSL_VERIFY_OPTIONAL &&
680 (ret == MBEDTLS_ERR_X509_CERT_VERIFY_FAILED ||
681 ret == MBEDTLS_ERR_SSL_BAD_CERTIFICATE)) {
XiaokangQian6b916b12022-04-25 07:29:34 +0000682 ret = 0;
683 }
Xiaofei Bai947571e2021-09-29 09:12:03 +0000684
Gilles Peskine449bd832023-01-11 14:50:10 +0100685 if (ca_chain == NULL && authmode == MBEDTLS_SSL_VERIFY_REQUIRED) {
686 MBEDTLS_SSL_DEBUG_MSG(1, ("got no CA chain"));
Xiaofei Bai947571e2021-09-29 09:12:03 +0000687 ret = MBEDTLS_ERR_SSL_CA_CHAIN_REQUIRED;
688 }
689
Gilles Peskine449bd832023-01-11 14:50:10 +0100690 if (ret != 0) {
Xiaofei Bai947571e2021-09-29 09:12:03 +0000691 /* The certificate may have been rejected for several reasons.
692 Pick one and send the corresponding alert. Which alert to send
693 may be a subject of debate in some cases. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100694 if (verify_result & MBEDTLS_X509_BADCERT_OTHER) {
695 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED, ret);
696 } else if (verify_result & MBEDTLS_X509_BADCERT_CN_MISMATCH) {
697 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_BAD_CERT, ret);
698 } else if (verify_result & (MBEDTLS_X509_BADCERT_KEY_USAGE |
699 MBEDTLS_X509_BADCERT_EXT_KEY_USAGE |
700 MBEDTLS_X509_BADCERT_NS_CERT_TYPE |
701 MBEDTLS_X509_BADCERT_BAD_PK |
702 MBEDTLS_X509_BADCERT_BAD_KEY)) {
703 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT, ret);
704 } else if (verify_result & MBEDTLS_X509_BADCERT_EXPIRED) {
705 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_CERT_EXPIRED, ret);
706 } else if (verify_result & MBEDTLS_X509_BADCERT_REVOKED) {
707 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_CERT_REVOKED, ret);
708 } else if (verify_result & MBEDTLS_X509_BADCERT_NOT_TRUSTED) {
709 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_UNKNOWN_CA, ret);
710 } else {
711 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_CERT_UNKNOWN, ret);
712 }
Xiaofei Bai947571e2021-09-29 09:12:03 +0000713 }
714
715#if defined(MBEDTLS_DEBUG_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100716 if (verify_result != 0) {
717 MBEDTLS_SSL_DEBUG_MSG(3, ("! Certificate verification flags %08x",
718 (unsigned int) verify_result));
719 } else {
720 MBEDTLS_SSL_DEBUG_MSG(3, ("Certificate verification flags clear"));
Xiaofei Bai947571e2021-09-29 09:12:03 +0000721 }
722#endif /* MBEDTLS_DEBUG_C */
723
Xiaofei Baiff456022021-10-28 06:50:17 +0000724 ssl->session_negotiate->verify_result = verify_result;
Gilles Peskine449bd832023-01-11 14:50:10 +0100725 return ret;
Xiaofei Bai947571e2021-09-29 09:12:03 +0000726}
727#else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200728MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100729static int ssl_tls13_validate_certificate(mbedtls_ssl_context *ssl)
Xiaofei Bai947571e2021-09-29 09:12:03 +0000730{
731 ((void) ssl);
Gilles Peskine449bd832023-01-11 14:50:10 +0100732 return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Xiaofei Bai947571e2021-09-29 09:12:03 +0000733}
734#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Ronald Cronde08cf32022-10-04 17:15:35 +0200735#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED */
Xiaofei Bai947571e2021-09-29 09:12:03 +0000736
Gilles Peskine449bd832023-01-11 14:50:10 +0100737int mbedtls_ssl_tls13_process_certificate(mbedtls_ssl_context *ssl)
Xiaofei Bai947571e2021-09-29 09:12:03 +0000738{
Xiaofei Bai79595ac2021-10-26 07:16:45 +0000739 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine449bd832023-01-11 14:50:10 +0100740 MBEDTLS_SSL_DEBUG_MSG(2, ("=> parse certificate"));
Xiaofei Bai79595ac2021-10-26 07:16:45 +0000741
Ronald Cronde08cf32022-10-04 17:15:35 +0200742#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED)
XiaokangQianc3017f62022-05-13 05:55:41 +0000743 unsigned char *buf;
744 size_t buf_len;
Xiaofei Bai79595ac2021-10-26 07:16:45 +0000745
Gilles Peskine449bd832023-01-11 14:50:10 +0100746 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_tls13_fetch_handshake_msg(
747 ssl, MBEDTLS_SSL_HS_CERTIFICATE,
748 &buf, &buf_len));
Xiaofei Bai79595ac2021-10-26 07:16:45 +0000749
XiaokangQianc3017f62022-05-13 05:55:41 +0000750 /* Parse the certificate chain sent by the peer. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100751 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_tls13_parse_certificate(ssl, buf,
752 buf + buf_len));
XiaokangQianc3017f62022-05-13 05:55:41 +0000753 /* Validate the certificate chain and set the verification results. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100754 MBEDTLS_SSL_PROC_CHK(ssl_tls13_validate_certificate(ssl));
Xiaofei Bai79595ac2021-10-26 07:16:45 +0000755
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +0100756 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_add_hs_msg_to_checksum(ssl,
Manuel Pégourié-Gonnard43cc1272023-02-06 11:48:19 +0100757 MBEDTLS_SSL_HS_CERTIFICATE, buf,
758 buf_len));
Xiaofei Bai79595ac2021-10-26 07:16:45 +0000759
Xiaofei Bai79595ac2021-10-26 07:16:45 +0000760cleanup:
Ronald Cronde08cf32022-10-04 17:15:35 +0200761#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED */
Xiaofei Bai79595ac2021-10-26 07:16:45 +0000762
Gilles Peskine449bd832023-01-11 14:50:10 +0100763 MBEDTLS_SSL_DEBUG_MSG(2, ("<= parse certificate"));
764 return ret;
Xiaofei Bai947571e2021-09-29 09:12:03 +0000765}
Ronald Cron928cbd32022-10-04 16:14:26 +0200766#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED)
Jerry Yu7399d0d2022-01-30 17:54:19 +0800767/*
768 * enum {
769 * X509(0),
770 * RawPublicKey(2),
771 * (255)
772 * } CertificateType;
773 *
774 * struct {
775 * select (certificate_type) {
776 * case RawPublicKey:
777 * // From RFC 7250 ASN.1_subjectPublicKeyInfo
778 * opaque ASN1_subjectPublicKeyInfo<1..2^24-1>;
779 *
780 * case X509:
781 * opaque cert_data<1..2^24-1>;
782 * };
783 * Extension extensions<0..2^16-1>;
784 * } CertificateEntry;
785 *
786 * struct {
787 * opaque certificate_request_context<0..2^8-1>;
788 * CertificateEntry certificate_list<0..2^24-1>;
789 * } Certificate;
790 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200791MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100792static int ssl_tls13_write_certificate_body(mbedtls_ssl_context *ssl,
793 unsigned char *buf,
794 unsigned char *end,
795 size_t *out_len)
Jerry Yu5cc35062022-01-28 16:16:08 +0800796{
Gilles Peskine449bd832023-01-11 14:50:10 +0100797 const mbedtls_x509_crt *crt = mbedtls_ssl_own_cert(ssl);
Jerry Yu3e536442022-02-15 11:05:59 +0800798 unsigned char *p = buf;
Jerry Yuc8d8d4e2022-02-18 12:10:03 +0800799 unsigned char *certificate_request_context =
Gilles Peskine449bd832023-01-11 14:50:10 +0100800 ssl->handshake->certificate_request_context;
Jerry Yuc8d8d4e2022-02-18 12:10:03 +0800801 unsigned char certificate_request_context_len =
Gilles Peskine449bd832023-01-11 14:50:10 +0100802 ssl->handshake->certificate_request_context_len;
Jerry Yuc8d8d4e2022-02-18 12:10:03 +0800803 unsigned char *p_certificate_list_len;
Jerry Yu5cc35062022-01-28 16:16:08 +0800804
Jerry Yu5cc35062022-01-28 16:16:08 +0800805
Jerry Yu3391ac02022-02-16 11:21:37 +0800806 /* ...
807 * opaque certificate_request_context<0..2^8-1>;
808 * ...
809 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100810 MBEDTLS_SSL_CHK_BUF_PTR(p, end, certificate_request_context_len + 1);
Jerry Yuc8d8d4e2022-02-18 12:10:03 +0800811 *p++ = certificate_request_context_len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100812 if (certificate_request_context_len > 0) {
813 memcpy(p, certificate_request_context, certificate_request_context_len);
Jerry Yuc8d8d4e2022-02-18 12:10:03 +0800814 p += certificate_request_context_len;
Jerry Yu537530d2022-02-15 14:00:57 +0800815 }
816
Jerry Yu3391ac02022-02-16 11:21:37 +0800817 /* ...
818 * CertificateEntry certificate_list<0..2^24-1>;
819 * ...
820 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100821 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 3);
Jerry Yuc8d8d4e2022-02-18 12:10:03 +0800822 p_certificate_list_len = p;
Jerry Yu3e536442022-02-15 11:05:59 +0800823 p += 3;
824
Gilles Peskine449bd832023-01-11 14:50:10 +0100825 MBEDTLS_SSL_DEBUG_CRT(3, "own certificate", crt);
Jerry Yu5cc35062022-01-28 16:16:08 +0800826
Gilles Peskine449bd832023-01-11 14:50:10 +0100827 while (crt != NULL) {
Jerry Yu7399d0d2022-01-30 17:54:19 +0800828 size_t cert_data_len = crt->raw.len;
Jerry Yu5cc35062022-01-28 16:16:08 +0800829
Gilles Peskine449bd832023-01-11 14:50:10 +0100830 MBEDTLS_SSL_CHK_BUF_PTR(p, end, cert_data_len + 3 + 2);
831 MBEDTLS_PUT_UINT24_BE(cert_data_len, p, 0);
Jerry Yu7399d0d2022-01-30 17:54:19 +0800832 p += 3;
Jerry Yu5cc35062022-01-28 16:16:08 +0800833
Gilles Peskine449bd832023-01-11 14:50:10 +0100834 memcpy(p, crt->raw.p, cert_data_len);
Jerry Yu7399d0d2022-01-30 17:54:19 +0800835 p += cert_data_len;
836 crt = crt->next;
Jerry Yu5cc35062022-01-28 16:16:08 +0800837
838 /* Currently, we don't have any certificate extensions defined.
839 * Hence, we are sending an empty extension with length zero.
840 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100841 MBEDTLS_PUT_UINT16_BE(0, p, 0);
Jerry Yu7399d0d2022-01-30 17:54:19 +0800842 p += 2;
Jerry Yu5cc35062022-01-28 16:16:08 +0800843 }
Jerry Yu5cc35062022-01-28 16:16:08 +0800844
Gilles Peskine449bd832023-01-11 14:50:10 +0100845 MBEDTLS_PUT_UINT24_BE(p - p_certificate_list_len - 3,
846 p_certificate_list_len, 0);
Jerry Yu7399d0d2022-01-30 17:54:19 +0800847
Jerry Yu3e536442022-02-15 11:05:59 +0800848 *out_len = p - buf;
Jerry Yu5cc35062022-01-28 16:16:08 +0800849
Jerry Yu7de2ff02022-11-08 21:43:46 +0800850 MBEDTLS_SSL_PRINT_EXTS(
Gilles Peskine449bd832023-01-11 14:50:10 +0100851 3, MBEDTLS_SSL_HS_CERTIFICATE, ssl->handshake->sent_extensions);
Jerry Yu4b8f2f72022-10-31 13:31:22 +0800852
Gilles Peskine449bd832023-01-11 14:50:10 +0100853 return 0;
Jerry Yu5cc35062022-01-28 16:16:08 +0800854}
Jerry Yu5cc35062022-01-28 16:16:08 +0800855
Gilles Peskine449bd832023-01-11 14:50:10 +0100856int mbedtls_ssl_tls13_write_certificate(mbedtls_ssl_context *ssl)
Jerry Yu5cc35062022-01-28 16:16:08 +0800857{
858 int ret;
Ronald Cron5bb8fc82022-03-09 07:00:13 +0100859 unsigned char *buf;
860 size_t buf_len, msg_len;
861
Gilles Peskine449bd832023-01-11 14:50:10 +0100862 MBEDTLS_SSL_DEBUG_MSG(2, ("=> write certificate"));
Jerry Yu5cc35062022-01-28 16:16:08 +0800863
Gilles Peskine449bd832023-01-11 14:50:10 +0100864 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_start_handshake_msg(ssl,
865 MBEDTLS_SSL_HS_CERTIFICATE, &buf,
866 &buf_len));
Jerry Yu5cc35062022-01-28 16:16:08 +0800867
Gilles Peskine449bd832023-01-11 14:50:10 +0100868 MBEDTLS_SSL_PROC_CHK(ssl_tls13_write_certificate_body(ssl,
869 buf,
870 buf + buf_len,
871 &msg_len));
Jerry Yu5cc35062022-01-28 16:16:08 +0800872
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +0100873 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_add_hs_msg_to_checksum(ssl,
Manuel Pégourié-Gonnard43cc1272023-02-06 11:48:19 +0100874 MBEDTLS_SSL_HS_CERTIFICATE, buf,
875 msg_len));
Jerry Yu5cc35062022-01-28 16:16:08 +0800876
Gilles Peskine449bd832023-01-11 14:50:10 +0100877 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_finish_handshake_msg(
878 ssl, buf_len, msg_len));
Jerry Yu5cc35062022-01-28 16:16:08 +0800879cleanup:
880
Gilles Peskine449bd832023-01-11 14:50:10 +0100881 MBEDTLS_SSL_DEBUG_MSG(2, ("<= write certificate"));
882 return ret;
Jerry Yu5cc35062022-01-28 16:16:08 +0800883}
884
Jerry Yu3e536442022-02-15 11:05:59 +0800885/*
886 * STATE HANDLING: Output Certificate Verify
887 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100888int mbedtls_ssl_tls13_check_sig_alg_cert_key_match(uint16_t sig_alg,
889 mbedtls_pk_context *key)
Jerry Yu0c6be8f2022-06-20 20:42:00 +0800890{
Gilles Peskine449bd832023-01-11 14:50:10 +0100891 mbedtls_pk_type_t pk_type = mbedtls_ssl_sig_from_pk(key);
892 size_t key_size = mbedtls_pk_get_bitlen(key);
Jerry Yu0c6be8f2022-06-20 20:42:00 +0800893
Gilles Peskine449bd832023-01-11 14:50:10 +0100894 switch (pk_type) {
Jerry Yu67eced02022-02-25 13:37:36 +0800895 case MBEDTLS_SSL_SIG_ECDSA:
Gilles Peskine449bd832023-01-11 14:50:10 +0100896 switch (key_size) {
Jerry Yu67eced02022-02-25 13:37:36 +0800897 case 256:
Gilles Peskine449bd832023-01-11 14:50:10 +0100898 return
899 sig_alg == MBEDTLS_TLS1_3_SIG_ECDSA_SECP256R1_SHA256;
Jerry Yu0c6be8f2022-06-20 20:42:00 +0800900
Jerry Yu67eced02022-02-25 13:37:36 +0800901 case 384:
Gilles Peskine449bd832023-01-11 14:50:10 +0100902 return
903 sig_alg == MBEDTLS_TLS1_3_SIG_ECDSA_SECP384R1_SHA384;
Jerry Yu0c6be8f2022-06-20 20:42:00 +0800904
Jerry Yu67eced02022-02-25 13:37:36 +0800905 case 521:
Gilles Peskine449bd832023-01-11 14:50:10 +0100906 return
907 sig_alg == MBEDTLS_TLS1_3_SIG_ECDSA_SECP521R1_SHA512;
Jerry Yu67eced02022-02-25 13:37:36 +0800908 default:
Jerry Yu67eced02022-02-25 13:37:36 +0800909 break;
910 }
911 break;
Jerry Yu67eced02022-02-25 13:37:36 +0800912
Jerry Yu67eced02022-02-25 13:37:36 +0800913 case MBEDTLS_SSL_SIG_RSA:
Gilles Peskine449bd832023-01-11 14:50:10 +0100914 switch (sig_alg) {
Ronald Cron38391bf2022-09-16 11:19:27 +0200915 case MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA256: /* Intentional fallthrough */
916 case MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA384: /* Intentional fallthrough */
Jerry Yu0c6be8f2022-06-20 20:42:00 +0800917 case MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA512:
Gilles Peskine449bd832023-01-11 14:50:10 +0100918 return 1;
Jerry Yuc2e04932022-06-27 22:13:03 +0800919
Jerry Yu0c6be8f2022-06-20 20:42:00 +0800920 default:
921 break;
Jerry Yucef3f332022-03-22 23:00:13 +0800922 }
Jerry Yu67eced02022-02-25 13:37:36 +0800923 break;
Jerry Yu0c6be8f2022-06-20 20:42:00 +0800924
Jerry Yu67eced02022-02-25 13:37:36 +0800925 default:
Jerry Yu67eced02022-02-25 13:37:36 +0800926 break;
927 }
Jerry Yu0c6be8f2022-06-20 20:42:00 +0800928
Gilles Peskine449bd832023-01-11 14:50:10 +0100929 return 0;
Jerry Yu0c6be8f2022-06-20 20:42:00 +0800930}
931
Ronald Cronce7d76e2022-07-08 18:56:49 +0200932MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100933static int ssl_tls13_write_certificate_verify_body(mbedtls_ssl_context *ssl,
934 unsigned char *buf,
935 unsigned char *end,
936 size_t *out_len)
Jerry Yu8511f122022-01-29 10:01:04 +0800937{
Ronald Cron067a1e72022-09-16 13:44:49 +0200938 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yu3e536442022-02-15 11:05:59 +0800939 unsigned char *p = buf;
Jerry Yu8511f122022-01-29 10:01:04 +0800940 mbedtls_pk_context *own_key;
Jerry Yu3e536442022-02-15 11:05:59 +0800941
Gilles Peskine449bd832023-01-11 14:50:10 +0100942 unsigned char handshake_hash[MBEDTLS_TLS1_3_MD_MAX_SIZE];
Jerry Yu8511f122022-01-29 10:01:04 +0800943 size_t handshake_hash_len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100944 unsigned char verify_buffer[SSL_VERIFY_STRUCT_MAX_SIZE];
Jerry Yu3e536442022-02-15 11:05:59 +0800945 size_t verify_buffer_len;
Ronald Cron067a1e72022-09-16 13:44:49 +0200946
947 uint16_t *sig_alg = ssl->handshake->received_sig_algs;
Jerry Yu3e536442022-02-15 11:05:59 +0800948 size_t signature_len = 0;
Jerry Yu8511f122022-01-29 10:01:04 +0800949
Jerry Yu0b7b1012022-02-23 12:23:05 +0800950 *out_len = 0;
951
Gilles Peskine449bd832023-01-11 14:50:10 +0100952 own_key = mbedtls_ssl_own_key(ssl);
953 if (own_key == NULL) {
954 MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
955 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Jerry Yu8511f122022-01-29 10:01:04 +0800956 }
957
Gilles Peskine449bd832023-01-11 14:50:10 +0100958 ret = mbedtls_ssl_get_handshake_transcript(ssl,
959 ssl->handshake->ciphersuite_info->mac,
960 handshake_hash,
961 sizeof(handshake_hash),
962 &handshake_hash_len);
963 if (ret != 0) {
964 return ret;
965 }
Jerry Yu8511f122022-01-29 10:01:04 +0800966
Gilles Peskine449bd832023-01-11 14:50:10 +0100967 MBEDTLS_SSL_DEBUG_BUF(3, "handshake hash",
968 handshake_hash,
969 handshake_hash_len);
Jerry Yu8511f122022-01-29 10:01:04 +0800970
Gilles Peskine449bd832023-01-11 14:50:10 +0100971 ssl_tls13_create_verify_structure(handshake_hash, handshake_hash_len,
972 verify_buffer, &verify_buffer_len,
973 ssl->conf->endpoint);
Jerry Yu8511f122022-01-29 10:01:04 +0800974
975 /*
976 * struct {
977 * SignatureScheme algorithm;
978 * opaque signature<0..2^16-1>;
979 * } CertificateVerify;
980 */
Ronald Cron067a1e72022-09-16 13:44:49 +0200981 /* Check there is space for the algorithm identifier (2 bytes) and the
982 * signature length (2 bytes).
983 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100984 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 4);
Ronald Cron067a1e72022-09-16 13:44:49 +0200985
Gilles Peskine449bd832023-01-11 14:50:10 +0100986 for (; *sig_alg != MBEDTLS_TLS1_3_SIG_NONE; sig_alg++) {
Ronald Cron067a1e72022-09-16 13:44:49 +0200987 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
988 mbedtls_pk_type_t pk_type = MBEDTLS_PK_NONE;
989 mbedtls_md_type_t md_alg = MBEDTLS_MD_NONE;
990 psa_algorithm_t psa_algorithm = PSA_ALG_NONE;
991 unsigned char verify_hash[PSA_HASH_MAX_SIZE];
992 size_t verify_hash_len;
Jerry Yu67eced02022-02-25 13:37:36 +0800993
Gilles Peskine449bd832023-01-11 14:50:10 +0100994 if (!mbedtls_ssl_sig_alg_is_offered(ssl, *sig_alg)) {
Ronald Cron067a1e72022-09-16 13:44:49 +0200995 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +0100996 }
Jerry Yu67eced02022-02-25 13:37:36 +0800997
Gilles Peskine449bd832023-01-11 14:50:10 +0100998 if (!mbedtls_ssl_tls13_sig_alg_for_cert_verify_is_supported(*sig_alg)) {
Ronald Cron067a1e72022-09-16 13:44:49 +0200999 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +01001000 }
Ronald Cron067a1e72022-09-16 13:44:49 +02001001
Gilles Peskine449bd832023-01-11 14:50:10 +01001002 if (!mbedtls_ssl_tls13_check_sig_alg_cert_key_match(*sig_alg, own_key)) {
Ronald Cron067a1e72022-09-16 13:44:49 +02001003 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +01001004 }
Ronald Cron067a1e72022-09-16 13:44:49 +02001005
Gilles Peskine449bd832023-01-11 14:50:10 +01001006 if (mbedtls_ssl_get_pk_type_and_md_alg_from_sig_alg(
1007 *sig_alg, &pk_type, &md_alg) != 0) {
1008 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Ronald Cron067a1e72022-09-16 13:44:49 +02001009 }
1010
1011 /* Hash verify buffer with indicated hash function */
Gilles Peskine449bd832023-01-11 14:50:10 +01001012 psa_algorithm = mbedtls_hash_info_psa_from_md(md_alg);
1013 status = psa_hash_compute(psa_algorithm,
1014 verify_buffer,
1015 verify_buffer_len,
1016 verify_hash, sizeof(verify_hash),
1017 &verify_hash_len);
1018 if (status != PSA_SUCCESS) {
1019 return psa_ssl_status_to_mbedtls(status);
Ronald Cron067a1e72022-09-16 13:44:49 +02001020 }
1021
Gilles Peskine449bd832023-01-11 14:50:10 +01001022 MBEDTLS_SSL_DEBUG_BUF(3, "verify hash", verify_hash, verify_hash_len);
1023
1024 if ((ret = mbedtls_pk_sign_ext(pk_type, own_key,
1025 md_alg, verify_hash, verify_hash_len,
1026 p + 4, (size_t) (end - (p + 4)), &signature_len,
1027 ssl->conf->f_rng, ssl->conf->p_rng)) != 0) {
1028 MBEDTLS_SSL_DEBUG_MSG(2, ("CertificateVerify signature failed with %s",
1029 mbedtls_ssl_sig_alg_to_str(*sig_alg)));
1030 MBEDTLS_SSL_DEBUG_RET(2, "mbedtls_pk_sign_ext", ret);
1031
1032 /* The signature failed. This is possible if the private key
1033 * was not suitable for the signature operation as purposely we
1034 * did not check its suitability completely. Let's try with
1035 * another signature algorithm.
1036 */
1037 continue;
1038 }
1039
1040 MBEDTLS_SSL_DEBUG_MSG(2, ("CertificateVerify signature with %s",
1041 mbedtls_ssl_sig_alg_to_str(*sig_alg)));
Ronald Cron067a1e72022-09-16 13:44:49 +02001042
1043 break;
1044 }
1045
Gilles Peskine449bd832023-01-11 14:50:10 +01001046 if (*sig_alg == MBEDTLS_TLS1_3_SIG_NONE) {
1047 MBEDTLS_SSL_DEBUG_MSG(1, ("no suitable signature algorithm"));
1048 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE,
1049 MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE);
1050 return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
Jerry Yu8511f122022-01-29 10:01:04 +08001051 }
1052
Gilles Peskine449bd832023-01-11 14:50:10 +01001053 MBEDTLS_PUT_UINT16_BE(*sig_alg, p, 0);
1054 MBEDTLS_PUT_UINT16_BE(signature_len, p, 2);
Jerry Yuf3b46b52022-06-19 16:52:27 +08001055
Ronald Cron067a1e72022-09-16 13:44:49 +02001056 *out_len = 4 + signature_len;
Jerry Yu8c338862022-03-23 13:34:04 +08001057
Gilles Peskine449bd832023-01-11 14:50:10 +01001058 return 0;
Jerry Yu8511f122022-01-29 10:01:04 +08001059}
Jerry Yu8511f122022-01-29 10:01:04 +08001060
Gilles Peskine449bd832023-01-11 14:50:10 +01001061int mbedtls_ssl_tls13_write_certificate_verify(mbedtls_ssl_context *ssl)
Jerry Yu8511f122022-01-29 10:01:04 +08001062{
1063 int ret = 0;
Jerry Yuca133a32022-02-15 14:22:05 +08001064 unsigned char *buf;
1065 size_t buf_len, msg_len;
1066
Gilles Peskine449bd832023-01-11 14:50:10 +01001067 MBEDTLS_SSL_DEBUG_MSG(2, ("=> write certificate verify"));
Jerry Yu8511f122022-01-29 10:01:04 +08001068
Gilles Peskine449bd832023-01-11 14:50:10 +01001069 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_start_handshake_msg(ssl,
1070 MBEDTLS_SSL_HS_CERTIFICATE_VERIFY, &buf,
1071 &buf_len));
Jerry Yu8511f122022-01-29 10:01:04 +08001072
Gilles Peskine449bd832023-01-11 14:50:10 +01001073 MBEDTLS_SSL_PROC_CHK(ssl_tls13_write_certificate_verify_body(
1074 ssl, buf, buf + buf_len, &msg_len));
Jerry Yu8511f122022-01-29 10:01:04 +08001075
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +01001076 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_add_hs_msg_to_checksum(ssl,
Manuel Pégourié-Gonnard43cc1272023-02-06 11:48:19 +01001077 MBEDTLS_SSL_HS_CERTIFICATE_VERIFY, buf,
1078 msg_len));
Jerry Yu8511f122022-01-29 10:01:04 +08001079
Gilles Peskine449bd832023-01-11 14:50:10 +01001080 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_finish_handshake_msg(
1081 ssl, buf_len, msg_len));
Jerry Yu8511f122022-01-29 10:01:04 +08001082
1083cleanup:
1084
Gilles Peskine449bd832023-01-11 14:50:10 +01001085 MBEDTLS_SSL_DEBUG_MSG(2, ("<= write certificate verify"));
1086 return ret;
Jerry Yu8511f122022-01-29 10:01:04 +08001087}
1088
Ronald Cron928cbd32022-10-04 16:14:26 +02001089#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED */
Jerry Yu90f152d2022-01-29 22:12:42 +08001090
Jerry Yu5cc35062022-01-28 16:16:08 +08001091/*
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001092 *
XiaokangQianc5c39d52021-11-09 11:55:10 +00001093 * STATE HANDLING: Incoming Finished message.
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001094 */
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001095/*
1096 * Implementation
1097 */
1098
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001099MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01001100static int ssl_tls13_preprocess_finished_message(mbedtls_ssl_context *ssl)
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001101{
1102 int ret;
1103
Gilles Peskine449bd832023-01-11 14:50:10 +01001104 ret = mbedtls_ssl_tls13_calculate_verify_data(ssl,
1105 ssl->handshake->state_local.finished_in.digest,
1106 sizeof(ssl->handshake->state_local.finished_in.
1107 digest),
1108 &ssl->handshake->state_local.finished_in.digest_len,
1109 ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT ?
1110 MBEDTLS_SSL_IS_SERVER : MBEDTLS_SSL_IS_CLIENT);
1111 if (ret != 0) {
1112 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_tls13_calculate_verify_data", ret);
1113 return ret;
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001114 }
1115
Gilles Peskine449bd832023-01-11 14:50:10 +01001116 return 0;
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001117}
1118
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001119MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01001120static int ssl_tls13_parse_finished_message(mbedtls_ssl_context *ssl,
1121 const unsigned char *buf,
1122 const unsigned char *end)
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001123{
XiaokangQian33062842021-11-11 03:37:45 +00001124 /*
1125 * struct {
XiaokangQianc13f9352021-11-11 06:13:22 +00001126 * opaque verify_data[Hash.length];
XiaokangQian33062842021-11-11 03:37:45 +00001127 * } Finished;
1128 */
1129 const unsigned char *expected_verify_data =
1130 ssl->handshake->state_local.finished_in.digest;
1131 size_t expected_verify_data_len =
1132 ssl->handshake->state_local.finished_in.digest_len;
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001133 /* Structural validation */
Gilles Peskine449bd832023-01-11 14:50:10 +01001134 if ((size_t) (end - buf) != expected_verify_data_len) {
1135 MBEDTLS_SSL_DEBUG_MSG(1, ("bad finished message"));
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001136
Gilles Peskine449bd832023-01-11 14:50:10 +01001137 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR,
1138 MBEDTLS_ERR_SSL_DECODE_ERROR);
1139 return MBEDTLS_ERR_SSL_DECODE_ERROR;
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001140 }
1141
Gilles Peskine449bd832023-01-11 14:50:10 +01001142 MBEDTLS_SSL_DEBUG_BUF(4, "verify_data (self-computed):",
1143 expected_verify_data,
1144 expected_verify_data_len);
1145 MBEDTLS_SSL_DEBUG_BUF(4, "verify_data (received message):", buf,
1146 expected_verify_data_len);
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001147
1148 /* Semantic validation */
Gilles Peskine449bd832023-01-11 14:50:10 +01001149 if (mbedtls_ct_memcmp(buf,
1150 expected_verify_data,
1151 expected_verify_data_len) != 0) {
1152 MBEDTLS_SSL_DEBUG_MSG(1, ("bad finished message"));
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001153
Gilles Peskine449bd832023-01-11 14:50:10 +01001154 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_DECRYPT_ERROR,
1155 MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE);
1156 return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001157 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001158 return 0;
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001159}
1160
Gilles Peskine449bd832023-01-11 14:50:10 +01001161int mbedtls_ssl_tls13_process_finished_message(mbedtls_ssl_context *ssl)
XiaokangQianc5c39d52021-11-09 11:55:10 +00001162{
XiaokangQian33062842021-11-11 03:37:45 +00001163 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
XiaokangQianc5c39d52021-11-09 11:55:10 +00001164 unsigned char *buf;
Xiaofei Baieef15042021-11-18 07:29:56 +00001165 size_t buf_len;
XiaokangQianc5c39d52021-11-09 11:55:10 +00001166
Gilles Peskine449bd832023-01-11 14:50:10 +01001167 MBEDTLS_SSL_DEBUG_MSG(2, ("=> parse finished message"));
XiaokangQianc5c39d52021-11-09 11:55:10 +00001168
Gilles Peskine449bd832023-01-11 14:50:10 +01001169 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_tls13_fetch_handshake_msg(ssl,
1170 MBEDTLS_SSL_HS_FINISHED,
1171 &buf, &buf_len));
Jerry Yu0a92d6c2022-05-16 16:54:46 +08001172
1173 /* Preprocessing step: Compute handshake digest */
Gilles Peskine449bd832023-01-11 14:50:10 +01001174 MBEDTLS_SSL_PROC_CHK(ssl_tls13_preprocess_finished_message(ssl));
Jerry Yu0a92d6c2022-05-16 16:54:46 +08001175
Gilles Peskine449bd832023-01-11 14:50:10 +01001176 MBEDTLS_SSL_PROC_CHK(ssl_tls13_parse_finished_message(ssl, buf, buf + buf_len));
Jerry Yu0a92d6c2022-05-16 16:54:46 +08001177
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +01001178 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_add_hs_msg_to_checksum(ssl,
Manuel Pégourié-Gonnard43cc1272023-02-06 11:48:19 +01001179 MBEDTLS_SSL_HS_FINISHED, buf, buf_len));
XiaokangQianc5c39d52021-11-09 11:55:10 +00001180
1181cleanup:
1182
Gilles Peskine449bd832023-01-11 14:50:10 +01001183 MBEDTLS_SSL_DEBUG_MSG(2, ("<= parse finished message"));
1184 return ret;
XiaokangQianc5c39d52021-11-09 11:55:10 +00001185}
1186
XiaokangQian74af2a82021-09-22 07:40:30 +00001187/*
1188 *
XiaokangQiancc90c942021-11-09 12:30:09 +00001189 * STATE HANDLING: Write and send Finished message.
XiaokangQian74af2a82021-09-22 07:40:30 +00001190 *
1191 */
XiaokangQian74af2a82021-09-22 07:40:30 +00001192/*
XiaokangQian35dc6252021-11-11 08:16:19 +00001193 * Implement
XiaokangQian74af2a82021-09-22 07:40:30 +00001194 */
1195
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001196MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01001197static int ssl_tls13_prepare_finished_message(mbedtls_ssl_context *ssl)
XiaokangQian74af2a82021-09-22 07:40:30 +00001198{
1199 int ret;
1200
1201 /* Compute transcript of handshake up to now. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001202 ret = mbedtls_ssl_tls13_calculate_verify_data(ssl,
1203 ssl->handshake->state_local.finished_out.digest,
1204 sizeof(ssl->handshake->state_local.finished_out.
1205 digest),
1206 &ssl->handshake->state_local.finished_out.digest_len,
1207 ssl->conf->endpoint);
XiaokangQian74af2a82021-09-22 07:40:30 +00001208
Gilles Peskine449bd832023-01-11 14:50:10 +01001209 if (ret != 0) {
1210 MBEDTLS_SSL_DEBUG_RET(1, "calculate_verify_data failed", ret);
1211 return ret;
XiaokangQian74af2a82021-09-22 07:40:30 +00001212 }
1213
Gilles Peskine449bd832023-01-11 14:50:10 +01001214 return 0;
XiaokangQian74af2a82021-09-22 07:40:30 +00001215}
1216
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001217MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01001218static int ssl_tls13_write_finished_message_body(mbedtls_ssl_context *ssl,
1219 unsigned char *buf,
1220 unsigned char *end,
1221 size_t *out_len)
XiaokangQian74af2a82021-09-22 07:40:30 +00001222{
XiaokangQian8773aa02021-11-10 07:33:09 +00001223 size_t verify_data_len = ssl->handshake->state_local.finished_out.digest_len;
XiaokangQian0fa66432021-11-15 03:33:57 +00001224 /*
1225 * struct {
1226 * opaque verify_data[Hash.length];
1227 * } Finished;
1228 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001229 MBEDTLS_SSL_CHK_BUF_PTR(buf, end, verify_data_len);
XiaokangQian74af2a82021-09-22 07:40:30 +00001230
Gilles Peskine449bd832023-01-11 14:50:10 +01001231 memcpy(buf, ssl->handshake->state_local.finished_out.digest,
1232 verify_data_len);
XiaokangQian74af2a82021-09-22 07:40:30 +00001233
Xiaofei Baid25fab62021-12-02 06:36:27 +00001234 *out_len = verify_data_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01001235 return 0;
XiaokangQian74af2a82021-09-22 07:40:30 +00001236}
XiaokangQianc5c39d52021-11-09 11:55:10 +00001237
XiaokangQian35dc6252021-11-11 08:16:19 +00001238/* Main entry point: orchestrates the other functions */
Gilles Peskine449bd832023-01-11 14:50:10 +01001239int mbedtls_ssl_tls13_write_finished_message(mbedtls_ssl_context *ssl)
XiaokangQian35dc6252021-11-11 08:16:19 +00001240{
1241 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1242 unsigned char *buf;
1243 size_t buf_len, msg_len;
1244
Gilles Peskine449bd832023-01-11 14:50:10 +01001245 MBEDTLS_SSL_DEBUG_MSG(2, ("=> write finished message"));
XiaokangQian35dc6252021-11-11 08:16:19 +00001246
Gilles Peskine449bd832023-01-11 14:50:10 +01001247 MBEDTLS_SSL_PROC_CHK(ssl_tls13_prepare_finished_message(ssl));
XiaokangQiandce82242021-11-15 06:01:26 +00001248
Gilles Peskine449bd832023-01-11 14:50:10 +01001249 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_start_handshake_msg(ssl,
1250 MBEDTLS_SSL_HS_FINISHED, &buf, &buf_len));
XiaokangQian35dc6252021-11-11 08:16:19 +00001251
Gilles Peskine449bd832023-01-11 14:50:10 +01001252 MBEDTLS_SSL_PROC_CHK(ssl_tls13_write_finished_message_body(
1253 ssl, buf, buf + buf_len, &msg_len));
XiaokangQian35dc6252021-11-11 08:16:19 +00001254
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +01001255 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_add_hs_msg_to_checksum(ssl,
Manuel Pégourié-Gonnard43cc1272023-02-06 11:48:19 +01001256 MBEDTLS_SSL_HS_FINISHED, buf, msg_len));
XiaokangQian35dc6252021-11-11 08:16:19 +00001257
Gilles Peskine449bd832023-01-11 14:50:10 +01001258 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_finish_handshake_msg(
1259 ssl, buf_len, msg_len));
XiaokangQian35dc6252021-11-11 08:16:19 +00001260cleanup:
1261
Gilles Peskine449bd832023-01-11 14:50:10 +01001262 MBEDTLS_SSL_DEBUG_MSG(2, ("<= write finished message"));
1263 return ret;
XiaokangQian35dc6252021-11-11 08:16:19 +00001264}
1265
Gilles Peskine449bd832023-01-11 14:50:10 +01001266void mbedtls_ssl_tls13_handshake_wrapup(mbedtls_ssl_context *ssl)
Jerry Yu378254d2021-10-30 21:44:47 +08001267{
1268
Gilles Peskine449bd832023-01-11 14:50:10 +01001269 MBEDTLS_SSL_DEBUG_MSG(3, ("=> handshake wrapup"));
Jerry Yu378254d2021-10-30 21:44:47 +08001270
Gilles Peskine449bd832023-01-11 14:50:10 +01001271 MBEDTLS_SSL_DEBUG_MSG(1, ("Switch to application keys for inbound traffic"));
1272 mbedtls_ssl_set_inbound_transform(ssl, ssl->transform_application);
Jerry Yue8c1fca2022-05-18 14:48:56 +08001273
Gilles Peskine449bd832023-01-11 14:50:10 +01001274 MBEDTLS_SSL_DEBUG_MSG(1, ("Switch to application keys for outbound traffic"));
1275 mbedtls_ssl_set_outbound_transform(ssl, ssl->transform_application);
Jerry Yue8c1fca2022-05-18 14:48:56 +08001276
Jerry Yu378254d2021-10-30 21:44:47 +08001277 /*
Jerry Yucfe64f02021-11-15 13:54:06 +08001278 * Free the previous session and switch to the current one.
Jerry Yu378254d2021-10-30 21:44:47 +08001279 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001280 if (ssl->session) {
1281 mbedtls_ssl_session_free(ssl->session);
1282 mbedtls_free(ssl->session);
Jerry Yu378254d2021-10-30 21:44:47 +08001283 }
1284 ssl->session = ssl->session_negotiate;
1285 ssl->session_negotiate = NULL;
1286
Gilles Peskine449bd832023-01-11 14:50:10 +01001287 MBEDTLS_SSL_DEBUG_MSG(3, ("<= handshake wrapup"));
Jerry Yu378254d2021-10-30 21:44:47 +08001288}
1289
Ronald Cron49ad6192021-11-24 16:25:31 +01001290/*
1291 *
1292 * STATE HANDLING: Write ChangeCipherSpec
1293 *
1294 */
1295#if defined(MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001296MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01001297static int ssl_tls13_write_change_cipher_spec_body(mbedtls_ssl_context *ssl,
1298 unsigned char *buf,
1299 unsigned char *end,
1300 size_t *olen)
Ronald Cron49ad6192021-11-24 16:25:31 +01001301{
1302 ((void) ssl);
1303
Gilles Peskine449bd832023-01-11 14:50:10 +01001304 MBEDTLS_SSL_CHK_BUF_PTR(buf, end, 1);
Ronald Cron49ad6192021-11-24 16:25:31 +01001305 buf[0] = 1;
1306 *olen = 1;
1307
Gilles Peskine449bd832023-01-11 14:50:10 +01001308 return 0;
Ronald Cron49ad6192021-11-24 16:25:31 +01001309}
1310
Gilles Peskine449bd832023-01-11 14:50:10 +01001311int mbedtls_ssl_tls13_write_change_cipher_spec(mbedtls_ssl_context *ssl)
Ronald Cron49ad6192021-11-24 16:25:31 +01001312{
1313 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1314
Gilles Peskine449bd832023-01-11 14:50:10 +01001315 MBEDTLS_SSL_DEBUG_MSG(2, ("=> write change cipher spec"));
Ronald Cron49ad6192021-11-24 16:25:31 +01001316
Ronald Cron49ad6192021-11-24 16:25:31 +01001317 /* Write CCS message */
Gilles Peskine449bd832023-01-11 14:50:10 +01001318 MBEDTLS_SSL_PROC_CHK(ssl_tls13_write_change_cipher_spec_body(
1319 ssl, ssl->out_msg,
1320 ssl->out_msg + MBEDTLS_SSL_OUT_CONTENT_LEN,
1321 &ssl->out_msglen));
Ronald Cron49ad6192021-11-24 16:25:31 +01001322
1323 ssl->out_msgtype = MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC;
1324
Ronald Cron49ad6192021-11-24 16:25:31 +01001325 /* Dispatch message */
Gilles Peskine449bd832023-01-11 14:50:10 +01001326 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_write_record(ssl, 0));
Ronald Cron49ad6192021-11-24 16:25:31 +01001327
1328cleanup:
1329
Gilles Peskine449bd832023-01-11 14:50:10 +01001330 MBEDTLS_SSL_DEBUG_MSG(2, ("<= write change cipher spec"));
1331 return ret;
Ronald Cron49ad6192021-11-24 16:25:31 +01001332}
1333
1334#endif /* MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE */
1335
Xiaokang Qianecc29482022-11-02 07:52:47 +00001336/* Early Data Indication Extension
Xiaokang Qian0e97d4d2022-10-24 11:12:51 +00001337 *
Xiaokang Qian0e97d4d2022-10-24 11:12:51 +00001338 * struct {
1339 * select ( Handshake.msg_type ) {
Xiaokang Qianecc29482022-11-02 07:52:47 +00001340 * ...
Xiaokang Qian0e97d4d2022-10-24 11:12:51 +00001341 * case client_hello: Empty;
1342 * case encrypted_extensions: Empty;
1343 * };
1344 * } EarlyDataIndication;
1345 */
1346#if defined(MBEDTLS_SSL_EARLY_DATA)
Gilles Peskine449bd832023-01-11 14:50:10 +01001347int mbedtls_ssl_tls13_write_early_data_ext(mbedtls_ssl_context *ssl,
1348 unsigned char *buf,
1349 const unsigned char *end,
1350 size_t *out_len)
Xiaokang Qian0e97d4d2022-10-24 11:12:51 +00001351{
1352 unsigned char *p = buf;
1353 *out_len = 0;
1354 ((void) ssl);
1355
Gilles Peskine449bd832023-01-11 14:50:10 +01001356 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 4);
Xiaokang Qian0e97d4d2022-10-24 11:12:51 +00001357
Gilles Peskine449bd832023-01-11 14:50:10 +01001358 MBEDTLS_PUT_UINT16_BE(MBEDTLS_TLS_EXT_EARLY_DATA, p, 0);
1359 MBEDTLS_PUT_UINT16_BE(0, p, 2);
Xiaokang Qian0e97d4d2022-10-24 11:12:51 +00001360
1361 *out_len = 4;
Xiaokang Qian2cd5ce02022-11-15 10:33:53 +00001362
Gilles Peskine449bd832023-01-11 14:50:10 +01001363 mbedtls_ssl_tls13_set_hs_sent_ext_mask(ssl, MBEDTLS_TLS_EXT_EARLY_DATA);
Xiaokang Qian2cd5ce02022-11-15 10:33:53 +00001364
Gilles Peskine449bd832023-01-11 14:50:10 +01001365 return 0;
Xiaokang Qian0e97d4d2022-10-24 11:12:51 +00001366}
1367#endif /* MBEDTLS_SSL_EARLY_DATA */
1368
XiaokangQian78b1fa72022-01-19 06:56:30 +00001369/* Reset SSL context and update hash for handling HRR.
1370 *
1371 * Replace Transcript-Hash(X) by
1372 * Transcript-Hash( message_hash ||
1373 * 00 00 Hash.length ||
1374 * X )
1375 * A few states of the handshake are preserved, including:
1376 * - session ID
1377 * - session ticket
1378 * - negotiated ciphersuite
1379 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001380int mbedtls_ssl_reset_transcript_for_hrr(mbedtls_ssl_context *ssl)
XiaokangQian78b1fa72022-01-19 06:56:30 +00001381{
1382 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Przemyslaw Stekielda645252022-09-14 12:50:51 +02001383 unsigned char hash_transcript[PSA_HASH_MAX_SIZE + 4];
XiaokangQian0ece9982022-01-24 08:56:23 +00001384 size_t hash_len;
Xiaokang Qian6b980012023-02-07 03:17:45 +00001385 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
1386 ssl->handshake->ciphersuite_info;
XiaokangQian78b1fa72022-01-19 06:56:30 +00001387
Gilles Peskine449bd832023-01-11 14:50:10 +01001388 MBEDTLS_SSL_DEBUG_MSG(3, ("Reset SSL session for HRR"));
XiaokangQian78b1fa72022-01-19 06:56:30 +00001389
Gilles Peskine449bd832023-01-11 14:50:10 +01001390 ret = mbedtls_ssl_get_handshake_transcript(ssl, ciphersuite_info->mac,
1391 hash_transcript + 4,
1392 PSA_HASH_MAX_SIZE,
1393 &hash_len);
1394 if (ret != 0) {
Manuel Pégourié-Gonnardda7979b2023-02-21 09:31:10 +01001395 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_get_handshake_transcript", ret);
Gilles Peskine449bd832023-01-11 14:50:10 +01001396 return ret;
XiaokangQian0ece9982022-01-24 08:56:23 +00001397 }
1398
1399 hash_transcript[0] = MBEDTLS_SSL_HS_MESSAGE_HASH;
1400 hash_transcript[1] = 0;
1401 hash_transcript[2] = 0;
1402 hash_transcript[3] = (unsigned char) hash_len;
1403
1404 hash_len += 4;
1405
Manuel Pégourié-Gonnardda7979b2023-02-21 09:31:10 +01001406 MBEDTLS_SSL_DEBUG_BUF(4, "Truncated handshake transcript",
1407 hash_transcript, hash_len);
1408
Manuel Pégourié-Gonnardd7a7a232023-02-05 10:26:49 +01001409 /* Reset running hash and replace it with a hash of the transcript */
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +01001410 ret = mbedtls_ssl_reset_checksum(ssl);
1411 if (ret != 0) {
1412 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_reset_checksum", ret);
1413 return ret;
1414 }
1415 ret = ssl->handshake->update_checksum(ssl, hash_transcript, hash_len);
1416 if (ret != 0) {
1417 MBEDTLS_SSL_DEBUG_RET(1, "update_checksum", ret);
1418 return ret;
1419 }
Przemyslaw Stekiel4b3fff42022-02-14 16:39:52 +01001420
Gilles Peskine449bd832023-01-11 14:50:10 +01001421 return ret;
XiaokangQian78b1fa72022-01-19 06:56:30 +00001422}
1423
XiaokangQian9b5d04b2022-04-10 10:20:43 +00001424#if defined(MBEDTLS_ECDH_C)
XiaokangQian7807f9f2022-02-15 10:04:37 +00001425
Gilles Peskine449bd832023-01-11 14:50:10 +01001426int mbedtls_ssl_tls13_read_public_ecdhe_share(mbedtls_ssl_context *ssl,
1427 const unsigned char *buf,
1428 size_t buf_len)
XiaokangQian7807f9f2022-02-15 10:04:37 +00001429{
Gilles Peskine449bd832023-01-11 14:50:10 +01001430 uint8_t *p = (uint8_t *) buf;
XiaokangQiancfd925f2022-04-14 07:10:37 +00001431 const uint8_t *end = buf + buf_len;
XiaokangQian9b5d04b2022-04-10 10:20:43 +00001432 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001433
XiaokangQian9b5d04b2022-04-10 10:20:43 +00001434 /* Get size of the TLS opaque key_exchange field of the KeyShareEntry struct. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001435 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, 2);
1436 uint16_t peerkey_len = MBEDTLS_GET_UINT16_BE(p, 0);
XiaokangQian9b5d04b2022-04-10 10:20:43 +00001437 p += 2;
XiaokangQian3207a322022-02-23 03:15:27 +00001438
XiaokangQian9b5d04b2022-04-10 10:20:43 +00001439 /* Check if key size is consistent with given buffer length. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001440 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, peerkey_len);
XiaokangQian9b5d04b2022-04-10 10:20:43 +00001441
1442 /* Store peer's ECDH public key. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001443 memcpy(handshake->ecdh_psa_peerkey, p, peerkey_len);
XiaokangQian9b5d04b2022-04-10 10:20:43 +00001444 handshake->ecdh_psa_peerkey_len = peerkey_len;
1445
Gilles Peskine449bd832023-01-11 14:50:10 +01001446 return 0;
XiaokangQian3207a322022-02-23 03:15:27 +00001447}
Jerry Yu89e103c2022-03-30 22:43:29 +08001448
1449int mbedtls_ssl_tls13_generate_and_write_ecdh_key_exchange(
Gilles Peskine449bd832023-01-11 14:50:10 +01001450 mbedtls_ssl_context *ssl,
1451 uint16_t named_group,
1452 unsigned char *buf,
1453 unsigned char *end,
1454 size_t *out_len)
Jerry Yu89e103c2022-03-30 22:43:29 +08001455{
1456 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
1457 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
1458 psa_key_attributes_t key_attributes;
1459 size_t own_pubkey_len;
1460 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
Valerio Setti40d9ca92023-01-04 16:08:04 +01001461 psa_ecc_family_t ec_psa_family = 0;
1462 size_t ec_bits = 0;
Jerry Yu89e103c2022-03-30 22:43:29 +08001463
Gilles Peskine449bd832023-01-11 14:50:10 +01001464 MBEDTLS_SSL_DEBUG_MSG(1, ("Perform PSA-based ECDH computation."));
Jerry Yu89e103c2022-03-30 22:43:29 +08001465
Valerio Setti40d9ca92023-01-04 16:08:04 +01001466 /* Convert EC's TLS ID to PSA key type. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001467 if (mbedtls_ssl_get_psa_curve_info_from_tls_id(named_group,
1468 &ec_psa_family,
1469 &ec_bits) == PSA_ERROR_NOT_SUPPORTED) {
1470 return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
Valerio Setti40d9ca92023-01-04 16:08:04 +01001471 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001472 handshake->ecdh_psa_type = PSA_KEY_TYPE_ECC_KEY_PAIR(ec_psa_family);
Valerio Setti40d9ca92023-01-04 16:08:04 +01001473 ssl->handshake->ecdh_bits = ec_bits;
Jerry Yu89e103c2022-03-30 22:43:29 +08001474
1475 key_attributes = psa_key_attributes_init();
Gilles Peskine449bd832023-01-11 14:50:10 +01001476 psa_set_key_usage_flags(&key_attributes, PSA_KEY_USAGE_DERIVE);
1477 psa_set_key_algorithm(&key_attributes, PSA_ALG_ECDH);
1478 psa_set_key_type(&key_attributes, handshake->ecdh_psa_type);
1479 psa_set_key_bits(&key_attributes, handshake->ecdh_bits);
Jerry Yu89e103c2022-03-30 22:43:29 +08001480
1481 /* Generate ECDH private key. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001482 status = psa_generate_key(&key_attributes,
1483 &handshake->ecdh_psa_privkey);
1484 if (status != PSA_SUCCESS) {
1485 ret = psa_ssl_status_to_mbedtls(status);
1486 MBEDTLS_SSL_DEBUG_RET(1, "psa_generate_key", ret);
1487 return ret;
Jerry Yu89e103c2022-03-30 22:43:29 +08001488
1489 }
1490
1491 /* Export the public part of the ECDH private key from PSA. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001492 status = psa_export_public_key(handshake->ecdh_psa_privkey,
1493 buf, (size_t) (end - buf),
1494 &own_pubkey_len);
1495 if (status != PSA_SUCCESS) {
1496 ret = psa_ssl_status_to_mbedtls(status);
1497 MBEDTLS_SSL_DEBUG_RET(1, "psa_export_public_key", ret);
1498 return ret;
Jerry Yu89e103c2022-03-30 22:43:29 +08001499
1500 }
1501
1502 *out_len = own_pubkey_len;
1503
Gilles Peskine449bd832023-01-11 14:50:10 +01001504 return 0;
Jerry Yu89e103c2022-03-30 22:43:29 +08001505}
XiaokangQian9b5d04b2022-04-10 10:20:43 +00001506#endif /* MBEDTLS_ECDH_C */
XiaokangQian7807f9f2022-02-15 10:04:37 +00001507
Jerry Yu0c354a22022-08-29 15:25:36 +08001508/* RFC 8446 section 4.2
1509 *
1510 * If an implementation receives an extension which it recognizes and which is
1511 * not specified for the message in which it appears, it MUST abort the handshake
1512 * with an "illegal_parameter" alert.
1513 *
1514 */
Jerry Yuc4bf5d62022-10-29 09:08:47 +08001515int mbedtls_ssl_tls13_check_received_extension(
Gilles Peskine449bd832023-01-11 14:50:10 +01001516 mbedtls_ssl_context *ssl,
1517 int hs_msg_type,
1518 unsigned int received_extension_type,
1519 uint32_t hs_msg_allowed_extensions_mask)
Jerry Yu0c354a22022-08-29 15:25:36 +08001520{
Jerry Yudf0ad652022-10-31 13:20:57 +08001521 uint32_t extension_mask = mbedtls_ssl_get_extension_mask(
Gilles Peskine449bd832023-01-11 14:50:10 +01001522 received_extension_type);
Jerry Yu0c354a22022-08-29 15:25:36 +08001523
Jerry Yu79aa7212022-11-08 21:30:21 +08001524 MBEDTLS_SSL_PRINT_EXT(
Gilles Peskine449bd832023-01-11 14:50:10 +01001525 3, hs_msg_type, received_extension_type, "received");
Jerry Yu0c354a22022-08-29 15:25:36 +08001526
Gilles Peskine449bd832023-01-11 14:50:10 +01001527 if ((extension_mask & hs_msg_allowed_extensions_mask) == 0) {
Jerry Yu79aa7212022-11-08 21:30:21 +08001528 MBEDTLS_SSL_PRINT_EXT(
Gilles Peskine449bd832023-01-11 14:50:10 +01001529 3, hs_msg_type, received_extension_type, "is illegal");
Jerry Yu0c354a22022-08-29 15:25:36 +08001530 MBEDTLS_SSL_PEND_FATAL_ALERT(
1531 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER,
Gilles Peskine449bd832023-01-11 14:50:10 +01001532 MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER);
1533 return MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER;
Jerry Yu0c354a22022-08-29 15:25:36 +08001534 }
1535
1536 ssl->handshake->received_extensions |= extension_mask;
Jerry Yuc4bf5d62022-10-29 09:08:47 +08001537 /*
1538 * If it is a message containing extension responses, check that we
1539 * previously sent the extension.
1540 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001541 switch (hs_msg_type) {
Jerry Yu0c354a22022-08-29 15:25:36 +08001542 case MBEDTLS_SSL_HS_SERVER_HELLO:
Jerry Yudf0ad652022-10-31 13:20:57 +08001543 case MBEDTLS_SSL_TLS1_3_HS_HELLO_RETRY_REQUEST:
Jerry Yu0c354a22022-08-29 15:25:36 +08001544 case MBEDTLS_SSL_HS_ENCRYPTED_EXTENSIONS:
1545 case MBEDTLS_SSL_HS_CERTIFICATE:
Jerry Yuc4bf5d62022-10-29 09:08:47 +08001546 /* Check if the received extension is sent by peer message.*/
Gilles Peskine449bd832023-01-11 14:50:10 +01001547 if ((ssl->handshake->sent_extensions & extension_mask) != 0) {
1548 return 0;
1549 }
Jerry Yu0c354a22022-08-29 15:25:36 +08001550 break;
1551 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01001552 return 0;
Jerry Yu0c354a22022-08-29 15:25:36 +08001553 }
1554
Jerry Yu79aa7212022-11-08 21:30:21 +08001555 MBEDTLS_SSL_PRINT_EXT(
Gilles Peskine449bd832023-01-11 14:50:10 +01001556 3, hs_msg_type, received_extension_type, "is unsupported");
Jerry Yu0c354a22022-08-29 15:25:36 +08001557 MBEDTLS_SSL_PEND_FATAL_ALERT(
1558 MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_EXT,
Gilles Peskine449bd832023-01-11 14:50:10 +01001559 MBEDTLS_ERR_SSL_UNSUPPORTED_EXTENSION);
1560 return MBEDTLS_ERR_SSL_UNSUPPORTED_EXTENSION;
Jerry Yu0c354a22022-08-29 15:25:36 +08001561}
1562
Jerry Yufb4b6472022-01-27 15:03:26 +08001563#endif /* MBEDTLS_SSL_TLS_C && MBEDTLS_SSL_PROTO_TLS1_3 */