blob: 3c8d448c6fe14d71189e26150882735c2a9bdf20 [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"
Manuel Pégourié-Gonnard02b10d82023-03-28 12:33:20 +020032#include "md_psa.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"
Manuel Pégourié-Gonnard2be8c632023-06-07 13:07:21 +020040#include "psa_util_internal.h"
Andrzej Kurek8a045ce2022-12-23 11:00:06 -050041
Valerio Settic9ae8622023-07-25 11:23:50 +020042#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_EPHEMERAL_ENABLED)
Andrzej Kurek00644842023-05-30 05:45:00 -040043/* Define a local translating function to save code size by not using too many
44 * arguments in each translating place. */
45static int local_err_translation(psa_status_t status)
46{
47 return psa_status_to_mbedtls(status, psa_to_ssl_errors,
Andrzej Kurek1e4a0302023-05-30 09:45:17 -040048 ARRAY_LENGTH(psa_to_ssl_errors),
Andrzej Kurek00644842023-05-30 05:45:00 -040049 psa_generic_status_to_mbedtls);
50}
51#define PSA_TO_MBEDTLS_ERR(status) local_err_translation(status)
Andrzej Kureka6033ac2023-05-30 15:16:34 -040052#endif
Andrzej Kurek8a045ce2022-12-23 11:00:06 -050053
Jerry Yufbe3e642022-04-25 19:31:51 +080054const uint8_t mbedtls_ssl_tls13_hello_retry_request_magic[
Gilles Peskine449bd832023-01-11 14:50:10 +010055 MBEDTLS_SERVER_HELLO_RANDOM_LEN] =
56{ 0xCF, 0x21, 0xAD, 0x74, 0xE5, 0x9A, 0x61, 0x11,
57 0xBE, 0x1D, 0x8C, 0x02, 0x1E, 0x65, 0xB8, 0x91,
58 0xC2, 0xA2, 0x11, 0x16, 0x7A, 0xBB, 0x8C, 0x5E,
59 0x07, 0x9E, 0x09, 0xE2, 0xC8, 0xA8, 0x33, 0x9C };
Jerry Yu93a13f22022-04-11 23:00:01 +080060
Gilles Peskine449bd832023-01-11 14:50:10 +010061int mbedtls_ssl_tls13_fetch_handshake_msg(mbedtls_ssl_context *ssl,
62 unsigned hs_type,
63 unsigned char **buf,
64 size_t *buf_len)
XiaokangQian6b226b02021-09-24 07:51:16 +000065{
66 int ret;
67
Gilles Peskine449bd832023-01-11 14:50:10 +010068 if ((ret = mbedtls_ssl_read_record(ssl, 0)) != 0) {
69 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_read_record", ret);
XiaokangQian6b226b02021-09-24 07:51:16 +000070 goto cleanup;
71 }
72
Gilles Peskine449bd832023-01-11 14:50:10 +010073 if (ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE ||
74 ssl->in_msg[0] != hs_type) {
75 MBEDTLS_SSL_DEBUG_MSG(1, ("Receive unexpected handshake message."));
76 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE,
77 MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE);
XiaokangQian6b226b02021-09-24 07:51:16 +000078 ret = MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE;
79 goto cleanup;
80 }
81
XiaokangQian05420b12021-09-29 08:46:37 +000082 /*
83 * Jump handshake header (4 bytes, see Section 4 of RFC 8446).
84 * ...
85 * HandshakeType msg_type;
86 * uint24 length;
87 * ...
88 */
Xiaofei Baieef15042021-11-18 07:29:56 +000089 *buf = ssl->in_msg + 4;
90 *buf_len = ssl->in_hslen - 4;
XiaokangQian6b226b02021-09-24 07:51:16 +000091
XiaokangQian6b226b02021-09-24 07:51:16 +000092cleanup:
93
Gilles Peskine449bd832023-01-11 14:50:10 +010094 return ret;
XiaokangQian6b226b02021-09-24 07:51:16 +000095}
96
Ronald Cron47dce632023-02-08 17:38:29 +010097int mbedtls_ssl_tls13_is_supported_versions_ext_present_in_exts(
98 mbedtls_ssl_context *ssl,
99 const unsigned char *buf, const unsigned char *end,
Ronald Croneff56732023-04-03 17:36:31 +0200100 const unsigned char **supported_versions_data,
101 const unsigned char **supported_versions_data_end)
Ronald Cron47dce632023-02-08 17:38:29 +0100102{
103 const unsigned char *p = buf;
104 size_t extensions_len;
105 const unsigned char *extensions_end;
106
Ronald Croneff56732023-04-03 17:36:31 +0200107 *supported_versions_data = NULL;
108 *supported_versions_data_end = NULL;
Ronald Cron47dce632023-02-08 17:38:29 +0100109
110 /* Case of no extension */
111 if (p == end) {
112 return 0;
113 }
114
115 /* ...
116 * Extension extensions<x..2^16-1>;
117 * ...
118 * struct {
119 * ExtensionType extension_type; (2 bytes)
120 * opaque extension_data<0..2^16-1>;
121 * } Extension;
122 */
123 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, 2);
124 extensions_len = MBEDTLS_GET_UINT16_BE(p, 0);
125 p += 2;
126
127 /* Check extensions do not go beyond the buffer of data. */
128 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, extensions_len);
129 extensions_end = p + extensions_len;
130
131 while (p < extensions_end) {
132 unsigned int extension_type;
133 size_t extension_data_len;
134
135 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, extensions_end, 4);
136 extension_type = MBEDTLS_GET_UINT16_BE(p, 0);
137 extension_data_len = MBEDTLS_GET_UINT16_BE(p, 2);
138 p += 4;
139 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, extensions_end, extension_data_len);
140
141 if (extension_type == MBEDTLS_TLS_EXT_SUPPORTED_VERSIONS) {
Ronald Croneff56732023-04-03 17:36:31 +0200142 *supported_versions_data = p;
143 *supported_versions_data_end = p + extension_data_len;
Ronald Cron47dce632023-02-08 17:38:29 +0100144 return 1;
145 }
146 p += extension_data_len;
147 }
148
149 return 0;
150}
151
Ronald Cron928cbd32022-10-04 16:14:26 +0200152#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED)
Jerry Yu30b071c2021-09-12 20:16:03 +0800153/*
Jerry Yu30b071c2021-09-12 20:16:03 +0800154 * STATE HANDLING: Read CertificateVerify
155 */
Jerry Yud0fc5852021-10-29 11:09:06 +0800156/* Macro to express the maximum length of the verify structure.
Jerry Yu30b071c2021-09-12 20:16:03 +0800157 *
158 * The structure is computed per TLS 1.3 specification as:
159 * - 64 bytes of octet 32,
160 * - 33 bytes for the context string
161 * (which is either "TLS 1.3, client CertificateVerify"
162 * or "TLS 1.3, server CertificateVerify"),
Jerry Yud0fc5852021-10-29 11:09:06 +0800163 * - 1 byte for the octet 0x0, which serves as a separator,
Jerry Yu30b071c2021-09-12 20:16:03 +0800164 * - 32 or 48 bytes for the Transcript-Hash(Handshake Context, Certificate)
165 * (depending on the size of the transcript_hash)
166 *
167 * This results in a total size of
168 * - 130 bytes for a SHA256-based transcript hash, or
169 * (64 + 33 + 1 + 32 bytes)
170 * - 146 bytes for a SHA384-based transcript hash.
171 * (64 + 33 + 1 + 48 bytes)
172 *
173 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100174#define SSL_VERIFY_STRUCT_MAX_SIZE (64 + \
175 33 + \
176 1 + \
177 MBEDTLS_TLS1_3_MD_MAX_SIZE \
178 )
Jerry Yu30b071c2021-09-12 20:16:03 +0800179
Jerry Yu0b32c502021-10-28 13:41:59 +0800180/*
181 * The ssl_tls13_create_verify_structure() creates the verify structure.
182 * As input, it requires the transcript hash.
183 *
184 * The caller has to ensure that the buffer has size at least
185 * SSL_VERIFY_STRUCT_MAX_SIZE bytes.
186 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100187static void ssl_tls13_create_verify_structure(const unsigned char *transcript_hash,
188 size_t transcript_hash_len,
189 unsigned char *verify_buffer,
190 size_t *verify_buffer_len,
191 int from)
Jerry Yu0b32c502021-10-28 13:41:59 +0800192{
193 size_t idx;
Jerry Yu30b071c2021-09-12 20:16:03 +0800194
Jerry Yu0b32c502021-10-28 13:41:59 +0800195 /* RFC 8446, Section 4.4.3:
196 *
197 * The digital signature [in the CertificateVerify message] is then
198 * computed over the concatenation of:
199 * - A string that consists of octet 32 (0x20) repeated 64 times
200 * - The context string
201 * - A single 0 byte which serves as the separator
202 * - The content to be signed
203 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100204 memset(verify_buffer, 0x20, 64);
Jerry Yu0b32c502021-10-28 13:41:59 +0800205 idx = 64;
206
Gilles Peskine449bd832023-01-11 14:50:10 +0100207 if (from == MBEDTLS_SSL_IS_CLIENT) {
208 memcpy(verify_buffer + idx, MBEDTLS_SSL_TLS1_3_LBL_WITH_LEN(client_cv));
209 idx += MBEDTLS_SSL_TLS1_3_LBL_LEN(client_cv);
210 } else { /* from == MBEDTLS_SSL_IS_SERVER */
211 memcpy(verify_buffer + idx, MBEDTLS_SSL_TLS1_3_LBL_WITH_LEN(server_cv));
212 idx += MBEDTLS_SSL_TLS1_3_LBL_LEN(server_cv);
Jerry Yu0b32c502021-10-28 13:41:59 +0800213 }
214
215 verify_buffer[idx++] = 0x0;
216
Gilles Peskine449bd832023-01-11 14:50:10 +0100217 memcpy(verify_buffer + idx, transcript_hash, transcript_hash_len);
Jerry Yu0b32c502021-10-28 13:41:59 +0800218 idx += transcript_hash_len;
219
220 *verify_buffer_len = idx;
221}
222
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200223MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100224static int ssl_tls13_parse_certificate_verify(mbedtls_ssl_context *ssl,
225 const unsigned char *buf,
226 const unsigned char *end,
227 const unsigned char *verify_buffer,
228 size_t verify_buffer_len)
Jerry Yu30b071c2021-09-12 20:16:03 +0800229{
230 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
pespaceka1378102022-04-26 15:03:11 +0200231 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Jerry Yu30b071c2021-09-12 20:16:03 +0800232 const unsigned char *p = buf;
233 uint16_t algorithm;
Jerry Yu30b071c2021-09-12 20:16:03 +0800234 size_t signature_len;
235 mbedtls_pk_type_t sig_alg;
236 mbedtls_md_type_t md_alg;
pespaceka1378102022-04-26 15:03:11 +0200237 psa_algorithm_t hash_alg = PSA_ALG_NONE;
238 unsigned char verify_hash[PSA_HASH_MAX_SIZE];
Jerry Yu30b071c2021-09-12 20:16:03 +0800239 size_t verify_hash_len;
240
Xiaofei Baid25fab62021-12-02 06:36:27 +0000241 void const *options = NULL;
XiaokangQian82d34cc2021-11-03 08:51:56 +0000242#if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT)
Xiaofei Baid25fab62021-12-02 06:36:27 +0000243 mbedtls_pk_rsassa_pss_options rsassa_pss_options;
XiaokangQian82d34cc2021-11-03 08:51:56 +0000244#endif /* MBEDTLS_X509_RSASSA_PSS_SUPPORT */
245
Jerry Yu30b071c2021-09-12 20:16:03 +0800246 /*
247 * struct {
248 * SignatureScheme algorithm;
249 * opaque signature<0..2^16-1>;
250 * } CertificateVerify;
251 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100252 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, 2);
253 algorithm = MBEDTLS_GET_UINT16_BE(p, 0);
Jerry Yu30b071c2021-09-12 20:16:03 +0800254 p += 2;
255
256 /* RFC 8446 section 4.4.3
257 *
Xiaokang Qian73437382023-03-29 08:24:12 +0000258 * If the CertificateVerify message is sent by a server, the signature
259 * algorithm MUST be one offered in the client's "signature_algorithms"
260 * extension unless no valid certificate chain can be produced without
261 * unsupported algorithms
Jerry Yu30b071c2021-09-12 20:16:03 +0800262 *
263 * RFC 8446 section 4.4.2.2
264 *
265 * If the client cannot construct an acceptable chain using the provided
Xiaokang Qian73437382023-03-29 08:24:12 +0000266 * certificates and decides to abort the handshake, then it MUST abort the
267 * handshake with an appropriate certificate-related alert
268 * (by default, "unsupported_certificate").
Jerry Yu30b071c2021-09-12 20:16:03 +0800269 *
Jerry Yu6f87f252021-10-29 20:12:51 +0800270 * Check if algorithm is an offered signature algorithm.
Jerry Yu30b071c2021-09-12 20:16:03 +0800271 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100272 if (!mbedtls_ssl_sig_alg_is_offered(ssl, algorithm)) {
Jerry Yu982d9e52021-10-14 15:59:37 +0800273 /* algorithm not in offered signature algorithms list */
Gilles Peskine449bd832023-01-11 14:50:10 +0100274 MBEDTLS_SSL_DEBUG_MSG(1, ("Received signature algorithm(%04x) is not "
275 "offered.",
276 (unsigned int) algorithm));
Jerry Yu6f87f252021-10-29 20:12:51 +0800277 goto error;
Jerry Yu30b071c2021-09-12 20:16:03 +0800278 }
279
Gilles Peskine449bd832023-01-11 14:50:10 +0100280 if (mbedtls_ssl_get_pk_type_and_md_alg_from_sig_alg(
281 algorithm, &sig_alg, &md_alg) != 0) {
Jerry Yu8c338862022-03-23 13:34:04 +0800282 goto error;
Jerry Yu30b071c2021-09-12 20:16:03 +0800283 }
284
Manuel Pégourié-Gonnard2d6d9932023-03-28 11:38:08 +0200285 hash_alg = mbedtls_md_psa_alg_from_type(md_alg);
Gilles Peskine449bd832023-01-11 14:50:10 +0100286 if (hash_alg == 0) {
pespaceka1378102022-04-26 15:03:11 +0200287 goto error;
288 }
289
Gilles Peskine449bd832023-01-11 14:50:10 +0100290 MBEDTLS_SSL_DEBUG_MSG(3, ("Certificate Verify: Signature algorithm ( %04x )",
291 (unsigned int) algorithm));
Jerry Yu30b071c2021-09-12 20:16:03 +0800292
293 /*
294 * Check the certificate's key type matches the signature alg
295 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100296 if (!mbedtls_pk_can_do(&ssl->session_negotiate->peer_cert->pk, sig_alg)) {
297 MBEDTLS_SSL_DEBUG_MSG(1, ("signature algorithm doesn't match cert key"));
Jerry Yu6f87f252021-10-29 20:12:51 +0800298 goto error;
Jerry Yu30b071c2021-09-12 20:16:03 +0800299 }
300
Gilles Peskine449bd832023-01-11 14:50:10 +0100301 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, 2);
302 signature_len = MBEDTLS_GET_UINT16_BE(p, 0);
Jerry Yu30b071c2021-09-12 20:16:03 +0800303 p += 2;
Gilles Peskine449bd832023-01-11 14:50:10 +0100304 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, signature_len);
Jerry Yu30b071c2021-09-12 20:16:03 +0800305
Gilles Peskine449bd832023-01-11 14:50:10 +0100306 status = psa_hash_compute(hash_alg,
307 verify_buffer,
308 verify_buffer_len,
309 verify_hash,
310 sizeof(verify_hash),
311 &verify_hash_len);
312 if (status != PSA_SUCCESS) {
313 MBEDTLS_SSL_DEBUG_RET(1, "hash computation PSA error", status);
Jerry Yu6f87f252021-10-29 20:12:51 +0800314 goto error;
Jerry Yu133690c2021-10-25 14:01:13 +0800315 }
316
Gilles Peskine449bd832023-01-11 14:50:10 +0100317 MBEDTLS_SSL_DEBUG_BUF(3, "verify hash", verify_hash, verify_hash_len);
XiaokangQian82d34cc2021-11-03 08:51:56 +0000318#if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT)
Gilles Peskine449bd832023-01-11 14:50:10 +0100319 if (sig_alg == MBEDTLS_PK_RSASSA_PSS) {
Xiaofei Baid25fab62021-12-02 06:36:27 +0000320 rsassa_pss_options.mgf1_hash_id = md_alg;
Przemek Stekiel6a5e0182022-06-27 11:53:13 +0200321
Gilles Peskine449bd832023-01-11 14:50:10 +0100322 rsassa_pss_options.expected_salt_len = PSA_HASH_LENGTH(hash_alg);
323 options = (const void *) &rsassa_pss_options;
XiaokangQian82d34cc2021-11-03 08:51:56 +0000324 }
325#endif /* MBEDTLS_X509_RSASSA_PSS_SUPPORT */
Jerry Yu30b071c2021-09-12 20:16:03 +0800326
Gilles Peskine449bd832023-01-11 14:50:10 +0100327 if ((ret = mbedtls_pk_verify_ext(sig_alg, options,
328 &ssl->session_negotiate->peer_cert->pk,
329 md_alg, verify_hash, verify_hash_len,
330 p, signature_len)) == 0) {
331 return 0;
Jerry Yu30b071c2021-09-12 20:16:03 +0800332 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100333 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_pk_verify_ext", ret);
Jerry Yu30b071c2021-09-12 20:16:03 +0800334
Jerry Yu6f87f252021-10-29 20:12:51 +0800335error:
336 /* RFC 8446 section 4.4.3
337 *
338 * If the verification fails, the receiver MUST terminate the handshake
339 * with a "decrypt_error" alert.
Gilles Peskine449bd832023-01-11 14:50:10 +0100340 */
341 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_DECRYPT_ERROR,
342 MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE);
343 return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
Jerry Yu6f87f252021-10-29 20:12:51 +0800344
Jerry Yu30b071c2021-09-12 20:16:03 +0800345}
Ronald Cron928cbd32022-10-04 16:14:26 +0200346#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED */
Jerry Yu30b071c2021-09-12 20:16:03 +0800347
Gilles Peskine449bd832023-01-11 14:50:10 +0100348int mbedtls_ssl_tls13_process_certificate_verify(mbedtls_ssl_context *ssl)
Jerry Yu30b071c2021-09-12 20:16:03 +0800349{
Jerry Yu30b071c2021-09-12 20:16:03 +0800350
Ronald Cron928cbd32022-10-04 16:14:26 +0200351#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED)
Jerry Yuda8cdf22021-10-25 15:06:49 +0800352 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
353 unsigned char verify_buffer[SSL_VERIFY_STRUCT_MAX_SIZE];
354 size_t verify_buffer_len;
355 unsigned char transcript[MBEDTLS_TLS1_3_MD_MAX_SIZE];
356 size_t transcript_len;
357 unsigned char *buf;
358 size_t buf_len;
359
Gilles Peskine449bd832023-01-11 14:50:10 +0100360 MBEDTLS_SSL_DEBUG_MSG(2, ("=> parse certificate verify"));
Jerry Yu30b071c2021-09-12 20:16:03 +0800361
Jerry Yuda8cdf22021-10-25 15:06:49 +0800362 MBEDTLS_SSL_PROC_CHK(
Xiaokang Qian73437382023-03-29 08:24:12 +0000363 mbedtls_ssl_tls13_fetch_handshake_msg(
364 ssl, MBEDTLS_SSL_HS_CERTIFICATE_VERIFY, &buf, &buf_len));
Jerry Yu30b071c2021-09-12 20:16:03 +0800365
Jerry Yuda8cdf22021-10-25 15:06:49 +0800366 /* Need to calculate the hash of the transcript first
Jerry Yu0b32c502021-10-28 13:41:59 +0800367 * before reading the message since otherwise it gets
368 * included in the transcript
369 */
Xiaokang Qian73437382023-03-29 08:24:12 +0000370 ret = mbedtls_ssl_get_handshake_transcript(
371 ssl,
Dave Rodgman2eab4622023-10-05 13:30:37 +0100372 (mbedtls_md_type_t) ssl->handshake->ciphersuite_info->mac,
Xiaokang Qian73437382023-03-29 08:24:12 +0000373 transcript, sizeof(transcript),
374 &transcript_len);
Gilles Peskine449bd832023-01-11 14:50:10 +0100375 if (ret != 0) {
Jerry Yuda8cdf22021-10-25 15:06:49 +0800376 MBEDTLS_SSL_PEND_FATAL_ALERT(
377 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR,
Gilles Peskine449bd832023-01-11 14:50:10 +0100378 MBEDTLS_ERR_SSL_INTERNAL_ERROR);
379 return ret;
Jerry Yu30b071c2021-09-12 20:16:03 +0800380 }
381
Gilles Peskine449bd832023-01-11 14:50:10 +0100382 MBEDTLS_SSL_DEBUG_BUF(3, "handshake hash", transcript, transcript_len);
Jerry Yuda8cdf22021-10-25 15:06:49 +0800383
384 /* Create verify structure */
Gilles Peskine449bd832023-01-11 14:50:10 +0100385 ssl_tls13_create_verify_structure(transcript,
386 transcript_len,
387 verify_buffer,
388 &verify_buffer_len,
389 (ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT) ?
390 MBEDTLS_SSL_IS_SERVER :
391 MBEDTLS_SSL_IS_CLIENT);
Jerry Yuda8cdf22021-10-25 15:06:49 +0800392
393 /* Process the message contents */
Xiaokang Qian73437382023-03-29 08:24:12 +0000394 MBEDTLS_SSL_PROC_CHK(ssl_tls13_parse_certificate_verify(
395 ssl, buf, buf + buf_len,
396 verify_buffer, verify_buffer_len));
Jerry Yuda8cdf22021-10-25 15:06:49 +0800397
Xiaokang Qian73437382023-03-29 08:24:12 +0000398 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_add_hs_msg_to_checksum(
399 ssl, MBEDTLS_SSL_HS_CERTIFICATE_VERIFY,
400 buf, buf_len));
Jerry Yu30b071c2021-09-12 20:16:03 +0800401
402cleanup:
403
Gilles Peskine449bd832023-01-11 14:50:10 +0100404 MBEDTLS_SSL_DEBUG_MSG(2, ("<= parse certificate verify"));
405 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_tls13_process_certificate_verify", ret);
406 return ret;
Jerry Yuda8cdf22021-10-25 15:06:49 +0800407#else
408 ((void) ssl);
Gilles Peskine449bd832023-01-11 14:50:10 +0100409 MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
410 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Ronald Cron928cbd32022-10-04 16:14:26 +0200411#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED */
Jerry Yu30b071c2021-09-12 20:16:03 +0800412}
413
414/*
Xiaofei Bai947571e2021-09-29 09:12:03 +0000415 *
XiaokangQian6b916b12022-04-25 07:29:34 +0000416 * STATE HANDLING: Incoming Certificate.
Xiaofei Bai947571e2021-09-29 09:12:03 +0000417 *
418 */
419
Ronald Cronde08cf32022-10-04 17:15:35 +0200420#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED)
Xiaofei Bai947571e2021-09-29 09:12:03 +0000421#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
422/*
423 * Structure of Certificate message:
424 *
425 * enum {
426 * X509(0),
427 * RawPublicKey(2),
428 * (255)
429 * } CertificateType;
430 *
431 * struct {
432 * select (certificate_type) {
433 * case RawPublicKey:
434 * * From RFC 7250 ASN.1_subjectPublicKeyInfo *
435 * opaque ASN1_subjectPublicKeyInfo<1..2^24-1>;
436 * case X509:
437 * opaque cert_data<1..2^24-1>;
438 * };
439 * Extension extensions<0..2^16-1>;
440 * } CertificateEntry;
441 *
442 * struct {
443 * opaque certificate_request_context<0..2^8-1>;
444 * CertificateEntry certificate_list<0..2^24-1>;
445 * } Certificate;
446 *
447 */
Xiaofei Bai79595ac2021-10-26 07:16:45 +0000448
449/* Parse certificate chain send by the server. */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200450MBEDTLS_CHECK_RETURN_CRITICAL
Ronald Crone3dac4a2022-06-10 17:21:51 +0200451MBEDTLS_STATIC_TESTABLE
Gilles Peskine449bd832023-01-11 14:50:10 +0100452int mbedtls_ssl_tls13_parse_certificate(mbedtls_ssl_context *ssl,
453 const unsigned char *buf,
454 const unsigned char *end)
Xiaofei Bai947571e2021-09-29 09:12:03 +0000455{
456 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
457 size_t certificate_request_context_len = 0;
458 size_t certificate_list_len = 0;
459 const unsigned char *p = buf;
460 const unsigned char *certificate_list_end;
Jerry Yuc4bf5d62022-10-29 09:08:47 +0800461 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
Xiaofei Bai947571e2021-09-29 09:12:03 +0000462
Gilles Peskine449bd832023-01-11 14:50:10 +0100463 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, 4);
Xiaofei Bai947571e2021-09-29 09:12:03 +0000464 certificate_request_context_len = p[0];
Gilles Peskine449bd832023-01-11 14:50:10 +0100465 certificate_list_len = MBEDTLS_GET_UINT24_BE(p, 1);
XiaokangQian63e713e2022-05-15 04:26:57 +0000466 p += 4;
Xiaofei Bai947571e2021-09-29 09:12:03 +0000467
468 /* In theory, the certificate list can be up to 2^24 Bytes, but we don't
469 * support anything beyond 2^16 = 64K.
470 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100471 if ((certificate_request_context_len != 0) ||
472 (certificate_list_len >= 0x10000)) {
473 MBEDTLS_SSL_DEBUG_MSG(1, ("bad certificate message"));
474 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR,
475 MBEDTLS_ERR_SSL_DECODE_ERROR);
476 return MBEDTLS_ERR_SSL_DECODE_ERROR;
Xiaofei Bai947571e2021-09-29 09:12:03 +0000477 }
478
479 /* In case we tried to reuse a session but it failed */
Gilles Peskine449bd832023-01-11 14:50:10 +0100480 if (ssl->session_negotiate->peer_cert != NULL) {
481 mbedtls_x509_crt_free(ssl->session_negotiate->peer_cert);
482 mbedtls_free(ssl->session_negotiate->peer_cert);
Xiaofei Bai947571e2021-09-29 09:12:03 +0000483 }
484
Gilles Peskine449bd832023-01-11 14:50:10 +0100485 if (certificate_list_len == 0) {
XiaokangQianc3017f62022-05-13 05:55:41 +0000486 ssl->session_negotiate->peer_cert = NULL;
487 ret = 0;
488 goto exit;
489 }
490
Gilles Peskine449bd832023-01-11 14:50:10 +0100491 if ((ssl->session_negotiate->peer_cert =
492 mbedtls_calloc(1, sizeof(mbedtls_x509_crt))) == NULL) {
493 MBEDTLS_SSL_DEBUG_MSG(1, ("alloc( %" MBEDTLS_PRINTF_SIZET " bytes ) failed",
494 sizeof(mbedtls_x509_crt)));
495 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR,
496 MBEDTLS_ERR_SSL_ALLOC_FAILED);
497 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
Xiaofei Bai947571e2021-09-29 09:12:03 +0000498 }
499
Gilles Peskine449bd832023-01-11 14:50:10 +0100500 mbedtls_x509_crt_init(ssl->session_negotiate->peer_cert);
Xiaofei Bai947571e2021-09-29 09:12:03 +0000501
Gilles Peskine449bd832023-01-11 14:50:10 +0100502 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, certificate_list_len);
Xiaofei Bai947571e2021-09-29 09:12:03 +0000503 certificate_list_end = p + certificate_list_len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100504 while (p < certificate_list_end) {
Xiaofei Bai947571e2021-09-29 09:12:03 +0000505 size_t cert_data_len, extensions_len;
Jerry Yu2eaa7602022-08-04 17:28:15 +0800506 const unsigned char *extensions_end;
Xiaofei Bai947571e2021-09-29 09:12:03 +0000507
Gilles Peskine449bd832023-01-11 14:50:10 +0100508 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, certificate_list_end, 3);
509 cert_data_len = MBEDTLS_GET_UINT24_BE(p, 0);
Xiaofei Bai947571e2021-09-29 09:12:03 +0000510 p += 3;
511
512 /* In theory, the CRT can be up to 2^24 Bytes, but we don't support
513 * anything beyond 2^16 = 64K. Otherwise as in the TLS 1.2 code,
514 * check that we have a minimum of 128 bytes of data, this is not
515 * clear why we need that though.
516 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100517 if ((cert_data_len < 128) || (cert_data_len >= 0x10000)) {
518 MBEDTLS_SSL_DEBUG_MSG(1, ("bad Certificate message"));
519 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR,
520 MBEDTLS_ERR_SSL_DECODE_ERROR);
521 return MBEDTLS_ERR_SSL_DECODE_ERROR;
Xiaofei Bai947571e2021-09-29 09:12:03 +0000522 }
523
Gilles Peskine449bd832023-01-11 14:50:10 +0100524 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, certificate_list_end, cert_data_len);
525 ret = mbedtls_x509_crt_parse_der(ssl->session_negotiate->peer_cert,
526 p, cert_data_len);
Xiaofei Bai947571e2021-09-29 09:12:03 +0000527
Gilles Peskine449bd832023-01-11 14:50:10 +0100528 switch (ret) {
Xiaofei Bai947571e2021-09-29 09:12:03 +0000529 case 0: /*ok*/
530 break;
531 case MBEDTLS_ERR_X509_UNKNOWN_SIG_ALG + MBEDTLS_ERR_OID_NOT_FOUND:
532 /* Ignore certificate with an unknown algorithm: maybe a
533 prior certificate was already trusted. */
534 break;
535
536 case MBEDTLS_ERR_X509_ALLOC_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100537 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR,
538 MBEDTLS_ERR_X509_ALLOC_FAILED);
539 MBEDTLS_SSL_DEBUG_RET(1, " mbedtls_x509_crt_parse_der", ret);
540 return ret;
Xiaofei Bai947571e2021-09-29 09:12:03 +0000541
542 case MBEDTLS_ERR_X509_UNKNOWN_VERSION:
Gilles Peskine449bd832023-01-11 14:50:10 +0100543 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT,
544 MBEDTLS_ERR_X509_UNKNOWN_VERSION);
545 MBEDTLS_SSL_DEBUG_RET(1, " mbedtls_x509_crt_parse_der", ret);
546 return ret;
Xiaofei Bai947571e2021-09-29 09:12:03 +0000547
548 default:
Gilles Peskine449bd832023-01-11 14:50:10 +0100549 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_BAD_CERT,
550 ret);
551 MBEDTLS_SSL_DEBUG_RET(1, " mbedtls_x509_crt_parse_der", ret);
552 return ret;
Xiaofei Bai947571e2021-09-29 09:12:03 +0000553 }
554
555 p += cert_data_len;
556
557 /* Certificate extensions length */
Gilles Peskine449bd832023-01-11 14:50:10 +0100558 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, certificate_list_end, 2);
559 extensions_len = MBEDTLS_GET_UINT16_BE(p, 0);
Xiaofei Bai947571e2021-09-29 09:12:03 +0000560 p += 2;
Gilles Peskine449bd832023-01-11 14:50:10 +0100561 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, certificate_list_end, extensions_len);
Jerry Yu2eaa7602022-08-04 17:28:15 +0800562
563 extensions_end = p + extensions_len;
Jerry Yu0d5cfb72022-10-31 14:15:48 +0800564 handshake->received_extensions = MBEDTLS_SSL_EXT_MASK_NONE;
Jerry Yu2eaa7602022-08-04 17:28:15 +0800565
Gilles Peskine449bd832023-01-11 14:50:10 +0100566 while (p < extensions_end) {
Jerry Yu2eaa7602022-08-04 17:28:15 +0800567 unsigned int extension_type;
568 size_t extension_data_len;
569
570 /*
Gilles Peskine449bd832023-01-11 14:50:10 +0100571 * struct {
572 * ExtensionType extension_type; (2 bytes)
573 * opaque extension_data<0..2^16-1>;
574 * } Extension;
575 */
576 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, extensions_end, 4);
577 extension_type = MBEDTLS_GET_UINT16_BE(p, 0);
578 extension_data_len = MBEDTLS_GET_UINT16_BE(p, 2);
Jerry Yu2eaa7602022-08-04 17:28:15 +0800579 p += 4;
580
Gilles Peskine449bd832023-01-11 14:50:10 +0100581 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, extensions_end, extension_data_len);
Jerry Yu2eaa7602022-08-04 17:28:15 +0800582
Jerry Yuc4bf5d62022-10-29 09:08:47 +0800583 ret = mbedtls_ssl_tls13_check_received_extension(
Gilles Peskine449bd832023-01-11 14:50:10 +0100584 ssl, MBEDTLS_SSL_HS_CERTIFICATE, extension_type,
585 MBEDTLS_SSL_TLS1_3_ALLOWED_EXTS_OF_CT);
586 if (ret != 0) {
587 return ret;
588 }
Jerry Yu0c354a22022-08-29 15:25:36 +0800589
Gilles Peskine449bd832023-01-11 14:50:10 +0100590 switch (extension_type) {
Jerry Yu2eaa7602022-08-04 17:28:15 +0800591 default:
Jerry Yu79aa7212022-11-08 21:30:21 +0800592 MBEDTLS_SSL_PRINT_EXT(
Jerry Yu0d5cfb72022-10-31 14:15:48 +0800593 3, MBEDTLS_SSL_HS_CERTIFICATE,
Gilles Peskine449bd832023-01-11 14:50:10 +0100594 extension_type, "( ignored )");
Jerry Yu2eaa7602022-08-04 17:28:15 +0800595 break;
596 }
597
598 p += extension_data_len;
599 }
600
Gilles Peskine449bd832023-01-11 14:50:10 +0100601 MBEDTLS_SSL_PRINT_EXTS(3, MBEDTLS_SSL_HS_CERTIFICATE,
602 handshake->received_extensions);
Xiaofei Bai947571e2021-09-29 09:12:03 +0000603 }
604
XiaokangQian63e713e2022-05-15 04:26:57 +0000605exit:
Xiaofei Bai947571e2021-09-29 09:12:03 +0000606 /* Check that all the message is consumed. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100607 if (p != end) {
608 MBEDTLS_SSL_DEBUG_MSG(1, ("bad Certificate message"));
609 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR,
610 MBEDTLS_ERR_SSL_DECODE_ERROR);
611 return MBEDTLS_ERR_SSL_DECODE_ERROR;
Xiaofei Bai947571e2021-09-29 09:12:03 +0000612 }
613
Xiaokang Qian73437382023-03-29 08:24:12 +0000614 MBEDTLS_SSL_DEBUG_CRT(3, "peer certificate",
615 ssl->session_negotiate->peer_cert);
Xiaofei Bai947571e2021-09-29 09:12:03 +0000616
Gilles Peskine449bd832023-01-11 14:50:10 +0100617 return ret;
Xiaofei Bai947571e2021-09-29 09:12:03 +0000618}
619#else
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200620MBEDTLS_CHECK_RETURN_CRITICAL
Ronald Crone3dac4a2022-06-10 17:21:51 +0200621MBEDTLS_STATIC_TESTABLE
Gilles Peskine449bd832023-01-11 14:50:10 +0100622int mbedtls_ssl_tls13_parse_certificate(mbedtls_ssl_context *ssl,
623 const unsigned char *buf,
624 const unsigned char *end)
Xiaofei Bai947571e2021-09-29 09:12:03 +0000625{
626 ((void) ssl);
627 ((void) buf);
628 ((void) end);
Gilles Peskine449bd832023-01-11 14:50:10 +0100629 return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Xiaofei Bai947571e2021-09-29 09:12:03 +0000630}
631#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Ronald Cronde08cf32022-10-04 17:15:35 +0200632#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED */
Xiaofei Bai947571e2021-09-29 09:12:03 +0000633
Ronald Cronde08cf32022-10-04 17:15:35 +0200634#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED)
Xiaofei Bai947571e2021-09-29 09:12:03 +0000635#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Xiaofei Bai79595ac2021-10-26 07:16:45 +0000636/* Validate certificate chain sent by the server. */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200637MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100638static int ssl_tls13_validate_certificate(mbedtls_ssl_context *ssl)
Xiaofei Bai947571e2021-09-29 09:12:03 +0000639{
640 int ret = 0;
XiaokangQian989f06d2022-05-17 01:50:15 +0000641 int authmode = MBEDTLS_SSL_VERIFY_REQUIRED;
Xiaofei Bai947571e2021-09-29 09:12:03 +0000642 mbedtls_x509_crt *ca_chain;
643 mbedtls_x509_crl *ca_crl;
Ronald Cron30c5a252022-06-16 19:31:06 +0200644 const char *ext_oid;
645 size_t ext_len;
Xiaofei Baiff456022021-10-28 06:50:17 +0000646 uint32_t verify_result = 0;
Xiaofei Bai947571e2021-09-29 09:12:03 +0000647
XiaokangQian6b916b12022-04-25 07:29:34 +0000648 /* If SNI was used, overwrite authentication mode
649 * from the configuration. */
XiaokangQian989f06d2022-05-17 01:50:15 +0000650#if defined(MBEDTLS_SSL_SRV_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100651 if (ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER) {
XiaokangQian0557c942022-05-30 08:10:53 +0000652#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Gilles Peskine449bd832023-01-11 14:50:10 +0100653 if (ssl->handshake->sni_authmode != MBEDTLS_SSL_VERIFY_UNSET) {
XiaokangQian0557c942022-05-30 08:10:53 +0000654 authmode = ssl->handshake->sni_authmode;
Gilles Peskine449bd832023-01-11 14:50:10 +0100655 } else
XiaokangQian0557c942022-05-30 08:10:53 +0000656#endif
Gilles Peskine449bd832023-01-11 14:50:10 +0100657 authmode = ssl->conf->authmode;
XiaokangQian0557c942022-05-30 08:10:53 +0000658 }
XiaokangQian6b916b12022-04-25 07:29:34 +0000659#endif
660
661 /*
XiaokangQian989f06d2022-05-17 01:50:15 +0000662 * If the peer hasn't sent a certificate ( i.e. it sent
XiaokangQian6b916b12022-04-25 07:29:34 +0000663 * an empty certificate chain ), this is reflected in the peer CRT
664 * structure being unset.
665 * Check for that and handle it depending on the
XiaokangQian989f06d2022-05-17 01:50:15 +0000666 * authentication mode.
XiaokangQian6b916b12022-04-25 07:29:34 +0000667 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100668 if (ssl->session_negotiate->peer_cert == NULL) {
669 MBEDTLS_SSL_DEBUG_MSG(1, ("peer has no certificate"));
XiaokangQian989f06d2022-05-17 01:50:15 +0000670
XiaokangQian63e713e2022-05-15 04:26:57 +0000671#if defined(MBEDTLS_SSL_SRV_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100672 if (ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER) {
XiaokangQian63e713e2022-05-15 04:26:57 +0000673 /* The client was asked for a certificate but didn't send
674 * one. The client should know what's going on, so we
675 * don't send an alert.
676 */
677 ssl->session_negotiate->verify_result = MBEDTLS_X509_BADCERT_MISSING;
Gilles Peskine449bd832023-01-11 14:50:10 +0100678 if (authmode == MBEDTLS_SSL_VERIFY_OPTIONAL) {
679 return 0;
680 } else {
Xiaokang Qian73437382023-03-29 08:24:12 +0000681 MBEDTLS_SSL_PEND_FATAL_ALERT(
682 MBEDTLS_SSL_ALERT_MSG_NO_CERT,
683 MBEDTLS_ERR_SSL_NO_CLIENT_CERTIFICATE);
Gilles Peskine449bd832023-01-11 14:50:10 +0100684 return MBEDTLS_ERR_SSL_NO_CLIENT_CERTIFICATE;
XiaokangQian989f06d2022-05-17 01:50:15 +0000685 }
XiaokangQian63e713e2022-05-15 04:26:57 +0000686 }
XiaokangQian6b916b12022-04-25 07:29:34 +0000687#endif /* MBEDTLS_SSL_SRV_C */
688
XiaokangQianc3017f62022-05-13 05:55:41 +0000689#if defined(MBEDTLS_SSL_CLI_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100690 if (ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT) {
691 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_NO_CERT,
692 MBEDTLS_ERR_SSL_FATAL_ALERT_MESSAGE);
693 return MBEDTLS_ERR_SSL_FATAL_ALERT_MESSAGE;
XiaokangQian63e713e2022-05-15 04:26:57 +0000694 }
XiaokangQianc3017f62022-05-13 05:55:41 +0000695#endif /* MBEDTLS_SSL_CLI_C */
XiaokangQian63e713e2022-05-15 04:26:57 +0000696 }
XiaokangQian6b916b12022-04-25 07:29:34 +0000697
Xiaofei Bai947571e2021-09-29 09:12:03 +0000698#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Gilles Peskine449bd832023-01-11 14:50:10 +0100699 if (ssl->handshake->sni_ca_chain != NULL) {
Xiaofei Bai947571e2021-09-29 09:12:03 +0000700 ca_chain = ssl->handshake->sni_ca_chain;
701 ca_crl = ssl->handshake->sni_ca_crl;
Gilles Peskine449bd832023-01-11 14:50:10 +0100702 } else
Xiaofei Bai947571e2021-09-29 09:12:03 +0000703#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
704 {
705 ca_chain = ssl->conf->ca_chain;
706 ca_crl = ssl->conf->ca_crl;
707 }
708
709 /*
710 * Main check: verify certificate
711 */
712 ret = mbedtls_x509_crt_verify_with_profile(
713 ssl->session_negotiate->peer_cert,
714 ca_chain, ca_crl,
715 ssl->conf->cert_profile,
716 ssl->hostname,
Xiaofei Baiff456022021-10-28 06:50:17 +0000717 &verify_result,
Gilles Peskine449bd832023-01-11 14:50:10 +0100718 ssl->conf->f_vrfy, ssl->conf->p_vrfy);
Xiaofei Bai947571e2021-09-29 09:12:03 +0000719
Gilles Peskine449bd832023-01-11 14:50:10 +0100720 if (ret != 0) {
721 MBEDTLS_SSL_DEBUG_RET(1, "x509_verify_cert", ret);
Xiaofei Bai947571e2021-09-29 09:12:03 +0000722 }
723
724 /*
725 * Secondary checks: always done, but change 'ret' only if it was 0
726 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100727 if (ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT) {
Ronald Cron30c5a252022-06-16 19:31:06 +0200728 ext_oid = MBEDTLS_OID_SERVER_AUTH;
Gilles Peskine449bd832023-01-11 14:50:10 +0100729 ext_len = MBEDTLS_OID_SIZE(MBEDTLS_OID_SERVER_AUTH);
730 } else {
Ronald Cron30c5a252022-06-16 19:31:06 +0200731 ext_oid = MBEDTLS_OID_CLIENT_AUTH;
Gilles Peskine449bd832023-01-11 14:50:10 +0100732 ext_len = MBEDTLS_OID_SIZE(MBEDTLS_OID_CLIENT_AUTH);
Ronald Cron30c5a252022-06-16 19:31:06 +0200733 }
734
Gilles Peskine449bd832023-01-11 14:50:10 +0100735 if ((mbedtls_x509_crt_check_key_usage(
736 ssl->session_negotiate->peer_cert,
737 MBEDTLS_X509_KU_DIGITAL_SIGNATURE) != 0) ||
738 (mbedtls_x509_crt_check_extended_key_usage(
739 ssl->session_negotiate->peer_cert,
740 ext_oid, ext_len) != 0)) {
741 MBEDTLS_SSL_DEBUG_MSG(1, ("bad certificate (usage extensions)"));
742 if (ret == 0) {
Xiaofei Bai947571e2021-09-29 09:12:03 +0000743 ret = MBEDTLS_ERR_SSL_BAD_CERTIFICATE;
Gilles Peskine449bd832023-01-11 14:50:10 +0100744 }
Xiaofei Bai947571e2021-09-29 09:12:03 +0000745 }
746
XiaokangQian6b916b12022-04-25 07:29:34 +0000747 /* mbedtls_x509_crt_verify_with_profile is supposed to report a
748 * verification failure through MBEDTLS_ERR_X509_CERT_VERIFY_FAILED,
749 * with details encoded in the verification flags. All other kinds
750 * of error codes, including those from the user provided f_vrfy
751 * functions, are treated as fatal and lead to a failure of
Ronald Crone3dac4a2022-06-10 17:21:51 +0200752 * mbedtls_ssl_tls13_parse_certificate even if verification was optional.
753 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100754 if (authmode == MBEDTLS_SSL_VERIFY_OPTIONAL &&
755 (ret == MBEDTLS_ERR_X509_CERT_VERIFY_FAILED ||
756 ret == MBEDTLS_ERR_SSL_BAD_CERTIFICATE)) {
XiaokangQian6b916b12022-04-25 07:29:34 +0000757 ret = 0;
758 }
Xiaofei Bai947571e2021-09-29 09:12:03 +0000759
Gilles Peskine449bd832023-01-11 14:50:10 +0100760 if (ca_chain == NULL && authmode == MBEDTLS_SSL_VERIFY_REQUIRED) {
761 MBEDTLS_SSL_DEBUG_MSG(1, ("got no CA chain"));
Xiaofei Bai947571e2021-09-29 09:12:03 +0000762 ret = MBEDTLS_ERR_SSL_CA_CHAIN_REQUIRED;
763 }
764
Gilles Peskine449bd832023-01-11 14:50:10 +0100765 if (ret != 0) {
Xiaofei Bai947571e2021-09-29 09:12:03 +0000766 /* The certificate may have been rejected for several reasons.
767 Pick one and send the corresponding alert. Which alert to send
768 may be a subject of debate in some cases. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100769 if (verify_result & MBEDTLS_X509_BADCERT_OTHER) {
Xiaokang Qian73437382023-03-29 08:24:12 +0000770 MBEDTLS_SSL_PEND_FATAL_ALERT(
771 MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED, ret);
Gilles Peskine449bd832023-01-11 14:50:10 +0100772 } else if (verify_result & MBEDTLS_X509_BADCERT_CN_MISMATCH) {
773 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_BAD_CERT, ret);
774 } else if (verify_result & (MBEDTLS_X509_BADCERT_KEY_USAGE |
775 MBEDTLS_X509_BADCERT_EXT_KEY_USAGE |
776 MBEDTLS_X509_BADCERT_NS_CERT_TYPE |
777 MBEDTLS_X509_BADCERT_BAD_PK |
778 MBEDTLS_X509_BADCERT_BAD_KEY)) {
Xiaokang Qian73437382023-03-29 08:24:12 +0000779 MBEDTLS_SSL_PEND_FATAL_ALERT(
780 MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT, ret);
Gilles Peskine449bd832023-01-11 14:50:10 +0100781 } else if (verify_result & MBEDTLS_X509_BADCERT_EXPIRED) {
Xiaokang Qian73437382023-03-29 08:24:12 +0000782 MBEDTLS_SSL_PEND_FATAL_ALERT(
783 MBEDTLS_SSL_ALERT_MSG_CERT_EXPIRED, ret);
Gilles Peskine449bd832023-01-11 14:50:10 +0100784 } else if (verify_result & MBEDTLS_X509_BADCERT_REVOKED) {
Xiaokang Qian73437382023-03-29 08:24:12 +0000785 MBEDTLS_SSL_PEND_FATAL_ALERT(
786 MBEDTLS_SSL_ALERT_MSG_CERT_REVOKED, ret);
Gilles Peskine449bd832023-01-11 14:50:10 +0100787 } else if (verify_result & MBEDTLS_X509_BADCERT_NOT_TRUSTED) {
788 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_UNKNOWN_CA, ret);
789 } else {
Xiaokang Qian73437382023-03-29 08:24:12 +0000790 MBEDTLS_SSL_PEND_FATAL_ALERT(
791 MBEDTLS_SSL_ALERT_MSG_CERT_UNKNOWN, ret);
Gilles Peskine449bd832023-01-11 14:50:10 +0100792 }
Xiaofei Bai947571e2021-09-29 09:12:03 +0000793 }
794
795#if defined(MBEDTLS_DEBUG_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100796 if (verify_result != 0) {
797 MBEDTLS_SSL_DEBUG_MSG(3, ("! Certificate verification flags %08x",
798 (unsigned int) verify_result));
799 } else {
800 MBEDTLS_SSL_DEBUG_MSG(3, ("Certificate verification flags clear"));
Xiaofei Bai947571e2021-09-29 09:12:03 +0000801 }
802#endif /* MBEDTLS_DEBUG_C */
803
Xiaofei Baiff456022021-10-28 06:50:17 +0000804 ssl->session_negotiate->verify_result = verify_result;
Gilles Peskine449bd832023-01-11 14:50:10 +0100805 return ret;
Xiaofei Bai947571e2021-09-29 09:12:03 +0000806}
807#else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200808MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100809static int ssl_tls13_validate_certificate(mbedtls_ssl_context *ssl)
Xiaofei Bai947571e2021-09-29 09:12:03 +0000810{
811 ((void) ssl);
Gilles Peskine449bd832023-01-11 14:50:10 +0100812 return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Xiaofei Bai947571e2021-09-29 09:12:03 +0000813}
814#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Ronald Cronde08cf32022-10-04 17:15:35 +0200815#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED */
Xiaofei Bai947571e2021-09-29 09:12:03 +0000816
Gilles Peskine449bd832023-01-11 14:50:10 +0100817int mbedtls_ssl_tls13_process_certificate(mbedtls_ssl_context *ssl)
Xiaofei Bai947571e2021-09-29 09:12:03 +0000818{
Xiaofei Bai79595ac2021-10-26 07:16:45 +0000819 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine449bd832023-01-11 14:50:10 +0100820 MBEDTLS_SSL_DEBUG_MSG(2, ("=> parse certificate"));
Xiaofei Bai79595ac2021-10-26 07:16:45 +0000821
Ronald Cronde08cf32022-10-04 17:15:35 +0200822#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED)
XiaokangQianc3017f62022-05-13 05:55:41 +0000823 unsigned char *buf;
824 size_t buf_len;
Xiaofei Bai79595ac2021-10-26 07:16:45 +0000825
Gilles Peskine449bd832023-01-11 14:50:10 +0100826 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_tls13_fetch_handshake_msg(
827 ssl, MBEDTLS_SSL_HS_CERTIFICATE,
828 &buf, &buf_len));
Xiaofei Bai79595ac2021-10-26 07:16:45 +0000829
XiaokangQianc3017f62022-05-13 05:55:41 +0000830 /* Parse the certificate chain sent by the peer. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100831 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_tls13_parse_certificate(ssl, buf,
832 buf + buf_len));
XiaokangQianc3017f62022-05-13 05:55:41 +0000833 /* Validate the certificate chain and set the verification results. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100834 MBEDTLS_SSL_PROC_CHK(ssl_tls13_validate_certificate(ssl));
Xiaofei Bai79595ac2021-10-26 07:16:45 +0000835
Xiaokang Qian73437382023-03-29 08:24:12 +0000836 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_add_hs_msg_to_checksum(
837 ssl, MBEDTLS_SSL_HS_CERTIFICATE, buf, buf_len));
Xiaofei Bai79595ac2021-10-26 07:16:45 +0000838
Xiaofei Bai79595ac2021-10-26 07:16:45 +0000839cleanup:
Gilles Peskineff2558a2023-09-05 21:10:39 +0200840#else /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED */
841 (void) ssl;
Ronald Cronde08cf32022-10-04 17:15:35 +0200842#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED */
Xiaofei Bai79595ac2021-10-26 07:16:45 +0000843
Gilles Peskine449bd832023-01-11 14:50:10 +0100844 MBEDTLS_SSL_DEBUG_MSG(2, ("<= parse certificate"));
845 return ret;
Xiaofei Bai947571e2021-09-29 09:12:03 +0000846}
Ronald Cron928cbd32022-10-04 16:14:26 +0200847#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED)
Jerry Yu7399d0d2022-01-30 17:54:19 +0800848/*
849 * enum {
850 * X509(0),
851 * RawPublicKey(2),
852 * (255)
853 * } CertificateType;
854 *
855 * struct {
856 * select (certificate_type) {
857 * case RawPublicKey:
858 * // From RFC 7250 ASN.1_subjectPublicKeyInfo
859 * opaque ASN1_subjectPublicKeyInfo<1..2^24-1>;
860 *
861 * case X509:
862 * opaque cert_data<1..2^24-1>;
863 * };
864 * Extension extensions<0..2^16-1>;
865 * } CertificateEntry;
866 *
867 * struct {
868 * opaque certificate_request_context<0..2^8-1>;
869 * CertificateEntry certificate_list<0..2^24-1>;
870 * } Certificate;
871 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200872MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100873static int ssl_tls13_write_certificate_body(mbedtls_ssl_context *ssl,
874 unsigned char *buf,
875 unsigned char *end,
876 size_t *out_len)
Jerry Yu5cc35062022-01-28 16:16:08 +0800877{
Gilles Peskine449bd832023-01-11 14:50:10 +0100878 const mbedtls_x509_crt *crt = mbedtls_ssl_own_cert(ssl);
Jerry Yu3e536442022-02-15 11:05:59 +0800879 unsigned char *p = buf;
Jerry Yuc8d8d4e2022-02-18 12:10:03 +0800880 unsigned char *certificate_request_context =
Gilles Peskine449bd832023-01-11 14:50:10 +0100881 ssl->handshake->certificate_request_context;
Jerry Yuc8d8d4e2022-02-18 12:10:03 +0800882 unsigned char certificate_request_context_len =
Gilles Peskine449bd832023-01-11 14:50:10 +0100883 ssl->handshake->certificate_request_context_len;
Jerry Yuc8d8d4e2022-02-18 12:10:03 +0800884 unsigned char *p_certificate_list_len;
Jerry Yu5cc35062022-01-28 16:16:08 +0800885
Jerry Yu5cc35062022-01-28 16:16:08 +0800886
Jerry Yu3391ac02022-02-16 11:21:37 +0800887 /* ...
888 * opaque certificate_request_context<0..2^8-1>;
889 * ...
890 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100891 MBEDTLS_SSL_CHK_BUF_PTR(p, end, certificate_request_context_len + 1);
Jerry Yuc8d8d4e2022-02-18 12:10:03 +0800892 *p++ = certificate_request_context_len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100893 if (certificate_request_context_len > 0) {
894 memcpy(p, certificate_request_context, certificate_request_context_len);
Jerry Yuc8d8d4e2022-02-18 12:10:03 +0800895 p += certificate_request_context_len;
Jerry Yu537530d2022-02-15 14:00:57 +0800896 }
897
Jerry Yu3391ac02022-02-16 11:21:37 +0800898 /* ...
899 * CertificateEntry certificate_list<0..2^24-1>;
900 * ...
901 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100902 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 3);
Jerry Yuc8d8d4e2022-02-18 12:10:03 +0800903 p_certificate_list_len = p;
Jerry Yu3e536442022-02-15 11:05:59 +0800904 p += 3;
905
Gilles Peskine449bd832023-01-11 14:50:10 +0100906 MBEDTLS_SSL_DEBUG_CRT(3, "own certificate", crt);
Jerry Yu5cc35062022-01-28 16:16:08 +0800907
Gilles Peskine449bd832023-01-11 14:50:10 +0100908 while (crt != NULL) {
Jerry Yu7399d0d2022-01-30 17:54:19 +0800909 size_t cert_data_len = crt->raw.len;
Jerry Yu5cc35062022-01-28 16:16:08 +0800910
Gilles Peskine449bd832023-01-11 14:50:10 +0100911 MBEDTLS_SSL_CHK_BUF_PTR(p, end, cert_data_len + 3 + 2);
912 MBEDTLS_PUT_UINT24_BE(cert_data_len, p, 0);
Jerry Yu7399d0d2022-01-30 17:54:19 +0800913 p += 3;
Jerry Yu5cc35062022-01-28 16:16:08 +0800914
Gilles Peskine449bd832023-01-11 14:50:10 +0100915 memcpy(p, crt->raw.p, cert_data_len);
Jerry Yu7399d0d2022-01-30 17:54:19 +0800916 p += cert_data_len;
917 crt = crt->next;
Jerry Yu5cc35062022-01-28 16:16:08 +0800918
919 /* Currently, we don't have any certificate extensions defined.
920 * Hence, we are sending an empty extension with length zero.
921 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100922 MBEDTLS_PUT_UINT16_BE(0, p, 0);
Jerry Yu7399d0d2022-01-30 17:54:19 +0800923 p += 2;
Jerry Yu5cc35062022-01-28 16:16:08 +0800924 }
Jerry Yu5cc35062022-01-28 16:16:08 +0800925
Gilles Peskine449bd832023-01-11 14:50:10 +0100926 MBEDTLS_PUT_UINT24_BE(p - p_certificate_list_len - 3,
927 p_certificate_list_len, 0);
Jerry Yu7399d0d2022-01-30 17:54:19 +0800928
Jerry Yu3e536442022-02-15 11:05:59 +0800929 *out_len = p - buf;
Jerry Yu5cc35062022-01-28 16:16:08 +0800930
Jerry Yu7de2ff02022-11-08 21:43:46 +0800931 MBEDTLS_SSL_PRINT_EXTS(
Gilles Peskine449bd832023-01-11 14:50:10 +0100932 3, MBEDTLS_SSL_HS_CERTIFICATE, ssl->handshake->sent_extensions);
Jerry Yu4b8f2f72022-10-31 13:31:22 +0800933
Gilles Peskine449bd832023-01-11 14:50:10 +0100934 return 0;
Jerry Yu5cc35062022-01-28 16:16:08 +0800935}
Jerry Yu5cc35062022-01-28 16:16:08 +0800936
Gilles Peskine449bd832023-01-11 14:50:10 +0100937int mbedtls_ssl_tls13_write_certificate(mbedtls_ssl_context *ssl)
Jerry Yu5cc35062022-01-28 16:16:08 +0800938{
939 int ret;
Ronald Cron5bb8fc82022-03-09 07:00:13 +0100940 unsigned char *buf;
941 size_t buf_len, msg_len;
942
Gilles Peskine449bd832023-01-11 14:50:10 +0100943 MBEDTLS_SSL_DEBUG_MSG(2, ("=> write certificate"));
Jerry Yu5cc35062022-01-28 16:16:08 +0800944
Xiaokang Qian73437382023-03-29 08:24:12 +0000945 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_start_handshake_msg(
946 ssl, MBEDTLS_SSL_HS_CERTIFICATE, &buf, &buf_len));
Jerry Yu5cc35062022-01-28 16:16:08 +0800947
Gilles Peskine449bd832023-01-11 14:50:10 +0100948 MBEDTLS_SSL_PROC_CHK(ssl_tls13_write_certificate_body(ssl,
949 buf,
950 buf + buf_len,
951 &msg_len));
Jerry Yu5cc35062022-01-28 16:16:08 +0800952
Xiaokang Qian73437382023-03-29 08:24:12 +0000953 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_add_hs_msg_to_checksum(
954 ssl, MBEDTLS_SSL_HS_CERTIFICATE, buf, msg_len));
Jerry Yu5cc35062022-01-28 16:16:08 +0800955
Gilles Peskine449bd832023-01-11 14:50:10 +0100956 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_finish_handshake_msg(
957 ssl, buf_len, msg_len));
Jerry Yu5cc35062022-01-28 16:16:08 +0800958cleanup:
959
Gilles Peskine449bd832023-01-11 14:50:10 +0100960 MBEDTLS_SSL_DEBUG_MSG(2, ("<= write certificate"));
961 return ret;
Jerry Yu5cc35062022-01-28 16:16:08 +0800962}
963
Jerry Yu3e536442022-02-15 11:05:59 +0800964/*
965 * STATE HANDLING: Output Certificate Verify
966 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100967int mbedtls_ssl_tls13_check_sig_alg_cert_key_match(uint16_t sig_alg,
968 mbedtls_pk_context *key)
Jerry Yu0c6be8f2022-06-20 20:42:00 +0800969{
Dave Rodgman2eab4622023-10-05 13:30:37 +0100970 mbedtls_pk_type_t pk_type = (mbedtls_pk_type_t) mbedtls_ssl_sig_from_pk(key);
Gilles Peskine449bd832023-01-11 14:50:10 +0100971 size_t key_size = mbedtls_pk_get_bitlen(key);
Jerry Yu0c6be8f2022-06-20 20:42:00 +0800972
Gilles Peskine449bd832023-01-11 14:50:10 +0100973 switch (pk_type) {
Jerry Yu67eced02022-02-25 13:37:36 +0800974 case MBEDTLS_SSL_SIG_ECDSA:
Gilles Peskine449bd832023-01-11 14:50:10 +0100975 switch (key_size) {
Jerry Yu67eced02022-02-25 13:37:36 +0800976 case 256:
Gilles Peskine449bd832023-01-11 14:50:10 +0100977 return
978 sig_alg == MBEDTLS_TLS1_3_SIG_ECDSA_SECP256R1_SHA256;
Jerry Yu0c6be8f2022-06-20 20:42:00 +0800979
Jerry Yu67eced02022-02-25 13:37:36 +0800980 case 384:
Gilles Peskine449bd832023-01-11 14:50:10 +0100981 return
982 sig_alg == MBEDTLS_TLS1_3_SIG_ECDSA_SECP384R1_SHA384;
Jerry Yu0c6be8f2022-06-20 20:42:00 +0800983
Jerry Yu67eced02022-02-25 13:37:36 +0800984 case 521:
Gilles Peskine449bd832023-01-11 14:50:10 +0100985 return
986 sig_alg == MBEDTLS_TLS1_3_SIG_ECDSA_SECP521R1_SHA512;
Jerry Yu67eced02022-02-25 13:37:36 +0800987 default:
Jerry Yu67eced02022-02-25 13:37:36 +0800988 break;
989 }
990 break;
Jerry Yu67eced02022-02-25 13:37:36 +0800991
Jerry Yu67eced02022-02-25 13:37:36 +0800992 case MBEDTLS_SSL_SIG_RSA:
Gilles Peskine449bd832023-01-11 14:50:10 +0100993 switch (sig_alg) {
Ronald Cron38391bf2022-09-16 11:19:27 +0200994 case MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA256: /* Intentional fallthrough */
995 case MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA384: /* Intentional fallthrough */
Jerry Yu0c6be8f2022-06-20 20:42:00 +0800996 case MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA512:
Gilles Peskine449bd832023-01-11 14:50:10 +0100997 return 1;
Jerry Yuc2e04932022-06-27 22:13:03 +0800998
Jerry Yu0c6be8f2022-06-20 20:42:00 +0800999 default:
1000 break;
Jerry Yucef3f332022-03-22 23:00:13 +08001001 }
Jerry Yu67eced02022-02-25 13:37:36 +08001002 break;
Jerry Yu0c6be8f2022-06-20 20:42:00 +08001003
Jerry Yu67eced02022-02-25 13:37:36 +08001004 default:
Jerry Yu67eced02022-02-25 13:37:36 +08001005 break;
1006 }
Jerry Yu0c6be8f2022-06-20 20:42:00 +08001007
Gilles Peskine449bd832023-01-11 14:50:10 +01001008 return 0;
Jerry Yu0c6be8f2022-06-20 20:42:00 +08001009}
1010
Ronald Cronce7d76e2022-07-08 18:56:49 +02001011MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01001012static int ssl_tls13_write_certificate_verify_body(mbedtls_ssl_context *ssl,
1013 unsigned char *buf,
1014 unsigned char *end,
1015 size_t *out_len)
Jerry Yu8511f122022-01-29 10:01:04 +08001016{
Ronald Cron067a1e72022-09-16 13:44:49 +02001017 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yu3e536442022-02-15 11:05:59 +08001018 unsigned char *p = buf;
Jerry Yu8511f122022-01-29 10:01:04 +08001019 mbedtls_pk_context *own_key;
Jerry Yu3e536442022-02-15 11:05:59 +08001020
Gilles Peskine449bd832023-01-11 14:50:10 +01001021 unsigned char handshake_hash[MBEDTLS_TLS1_3_MD_MAX_SIZE];
Jerry Yu8511f122022-01-29 10:01:04 +08001022 size_t handshake_hash_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01001023 unsigned char verify_buffer[SSL_VERIFY_STRUCT_MAX_SIZE];
Jerry Yu3e536442022-02-15 11:05:59 +08001024 size_t verify_buffer_len;
Ronald Cron067a1e72022-09-16 13:44:49 +02001025
1026 uint16_t *sig_alg = ssl->handshake->received_sig_algs;
Jerry Yu3e536442022-02-15 11:05:59 +08001027 size_t signature_len = 0;
Jerry Yu8511f122022-01-29 10:01:04 +08001028
Jerry Yu0b7b1012022-02-23 12:23:05 +08001029 *out_len = 0;
1030
Gilles Peskine449bd832023-01-11 14:50:10 +01001031 own_key = mbedtls_ssl_own_key(ssl);
1032 if (own_key == NULL) {
1033 MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
1034 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Jerry Yu8511f122022-01-29 10:01:04 +08001035 }
1036
Xiaokang Qian73437382023-03-29 08:24:12 +00001037 ret = mbedtls_ssl_get_handshake_transcript(
Dave Rodgman2eab4622023-10-05 13:30:37 +01001038 ssl, (mbedtls_md_type_t) ssl->handshake->ciphersuite_info->mac,
Xiaokang Qian73437382023-03-29 08:24:12 +00001039 handshake_hash, sizeof(handshake_hash), &handshake_hash_len);
Gilles Peskine449bd832023-01-11 14:50:10 +01001040 if (ret != 0) {
1041 return ret;
1042 }
Jerry Yu8511f122022-01-29 10:01:04 +08001043
Gilles Peskine449bd832023-01-11 14:50:10 +01001044 MBEDTLS_SSL_DEBUG_BUF(3, "handshake hash",
1045 handshake_hash,
1046 handshake_hash_len);
Jerry Yu8511f122022-01-29 10:01:04 +08001047
Gilles Peskine449bd832023-01-11 14:50:10 +01001048 ssl_tls13_create_verify_structure(handshake_hash, handshake_hash_len,
1049 verify_buffer, &verify_buffer_len,
1050 ssl->conf->endpoint);
Jerry Yu8511f122022-01-29 10:01:04 +08001051
1052 /*
1053 * struct {
1054 * SignatureScheme algorithm;
1055 * opaque signature<0..2^16-1>;
1056 * } CertificateVerify;
1057 */
Ronald Cron067a1e72022-09-16 13:44:49 +02001058 /* Check there is space for the algorithm identifier (2 bytes) and the
1059 * signature length (2 bytes).
1060 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001061 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 4);
Ronald Cron067a1e72022-09-16 13:44:49 +02001062
Gilles Peskine449bd832023-01-11 14:50:10 +01001063 for (; *sig_alg != MBEDTLS_TLS1_3_SIG_NONE; sig_alg++) {
Ronald Cron067a1e72022-09-16 13:44:49 +02001064 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
1065 mbedtls_pk_type_t pk_type = MBEDTLS_PK_NONE;
1066 mbedtls_md_type_t md_alg = MBEDTLS_MD_NONE;
1067 psa_algorithm_t psa_algorithm = PSA_ALG_NONE;
1068 unsigned char verify_hash[PSA_HASH_MAX_SIZE];
1069 size_t verify_hash_len;
Jerry Yu67eced02022-02-25 13:37:36 +08001070
Gilles Peskine449bd832023-01-11 14:50:10 +01001071 if (!mbedtls_ssl_sig_alg_is_offered(ssl, *sig_alg)) {
Ronald Cron067a1e72022-09-16 13:44:49 +02001072 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +01001073 }
Jerry Yu67eced02022-02-25 13:37:36 +08001074
Gilles Peskine449bd832023-01-11 14:50:10 +01001075 if (!mbedtls_ssl_tls13_sig_alg_for_cert_verify_is_supported(*sig_alg)) {
Ronald Cron067a1e72022-09-16 13:44:49 +02001076 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +01001077 }
Ronald Cron067a1e72022-09-16 13:44:49 +02001078
Gilles Peskine449bd832023-01-11 14:50:10 +01001079 if (!mbedtls_ssl_tls13_check_sig_alg_cert_key_match(*sig_alg, own_key)) {
Ronald Cron067a1e72022-09-16 13:44:49 +02001080 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +01001081 }
Ronald Cron067a1e72022-09-16 13:44:49 +02001082
Gilles Peskine449bd832023-01-11 14:50:10 +01001083 if (mbedtls_ssl_get_pk_type_and_md_alg_from_sig_alg(
1084 *sig_alg, &pk_type, &md_alg) != 0) {
1085 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Ronald Cron067a1e72022-09-16 13:44:49 +02001086 }
1087
1088 /* Hash verify buffer with indicated hash function */
Manuel Pégourié-Gonnard2d6d9932023-03-28 11:38:08 +02001089 psa_algorithm = mbedtls_md_psa_alg_from_type(md_alg);
Gilles Peskine449bd832023-01-11 14:50:10 +01001090 status = psa_hash_compute(psa_algorithm,
1091 verify_buffer,
1092 verify_buffer_len,
1093 verify_hash, sizeof(verify_hash),
1094 &verify_hash_len);
1095 if (status != PSA_SUCCESS) {
Andrzej Kurek8a045ce2022-12-23 11:00:06 -05001096 return PSA_TO_MBEDTLS_ERR(status);
Ronald Cron067a1e72022-09-16 13:44:49 +02001097 }
1098
Gilles Peskine449bd832023-01-11 14:50:10 +01001099 MBEDTLS_SSL_DEBUG_BUF(3, "verify hash", verify_hash, verify_hash_len);
1100
1101 if ((ret = mbedtls_pk_sign_ext(pk_type, own_key,
1102 md_alg, verify_hash, verify_hash_len,
1103 p + 4, (size_t) (end - (p + 4)), &signature_len,
1104 ssl->conf->f_rng, ssl->conf->p_rng)) != 0) {
1105 MBEDTLS_SSL_DEBUG_MSG(2, ("CertificateVerify signature failed with %s",
1106 mbedtls_ssl_sig_alg_to_str(*sig_alg)));
1107 MBEDTLS_SSL_DEBUG_RET(2, "mbedtls_pk_sign_ext", ret);
1108
1109 /* The signature failed. This is possible if the private key
1110 * was not suitable for the signature operation as purposely we
1111 * did not check its suitability completely. Let's try with
1112 * another signature algorithm.
1113 */
1114 continue;
1115 }
1116
1117 MBEDTLS_SSL_DEBUG_MSG(2, ("CertificateVerify signature with %s",
1118 mbedtls_ssl_sig_alg_to_str(*sig_alg)));
Ronald Cron067a1e72022-09-16 13:44:49 +02001119
1120 break;
1121 }
1122
Gilles Peskine449bd832023-01-11 14:50:10 +01001123 if (*sig_alg == MBEDTLS_TLS1_3_SIG_NONE) {
1124 MBEDTLS_SSL_DEBUG_MSG(1, ("no suitable signature algorithm"));
1125 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE,
1126 MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE);
1127 return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
Jerry Yu8511f122022-01-29 10:01:04 +08001128 }
1129
Gilles Peskine449bd832023-01-11 14:50:10 +01001130 MBEDTLS_PUT_UINT16_BE(*sig_alg, p, 0);
1131 MBEDTLS_PUT_UINT16_BE(signature_len, p, 2);
Jerry Yuf3b46b52022-06-19 16:52:27 +08001132
Ronald Cron067a1e72022-09-16 13:44:49 +02001133 *out_len = 4 + signature_len;
Jerry Yu8c338862022-03-23 13:34:04 +08001134
Gilles Peskine449bd832023-01-11 14:50:10 +01001135 return 0;
Jerry Yu8511f122022-01-29 10:01:04 +08001136}
Jerry Yu8511f122022-01-29 10:01:04 +08001137
Gilles Peskine449bd832023-01-11 14:50:10 +01001138int mbedtls_ssl_tls13_write_certificate_verify(mbedtls_ssl_context *ssl)
Jerry Yu8511f122022-01-29 10:01:04 +08001139{
1140 int ret = 0;
Jerry Yuca133a32022-02-15 14:22:05 +08001141 unsigned char *buf;
1142 size_t buf_len, msg_len;
1143
Gilles Peskine449bd832023-01-11 14:50:10 +01001144 MBEDTLS_SSL_DEBUG_MSG(2, ("=> write certificate verify"));
Jerry Yu8511f122022-01-29 10:01:04 +08001145
Xiaokang Qian73437382023-03-29 08:24:12 +00001146 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_start_handshake_msg(
1147 ssl, MBEDTLS_SSL_HS_CERTIFICATE_VERIFY,
1148 &buf, &buf_len));
Jerry Yu8511f122022-01-29 10:01:04 +08001149
Gilles Peskine449bd832023-01-11 14:50:10 +01001150 MBEDTLS_SSL_PROC_CHK(ssl_tls13_write_certificate_verify_body(
1151 ssl, buf, buf + buf_len, &msg_len));
Jerry Yu8511f122022-01-29 10:01:04 +08001152
Xiaokang Qian73437382023-03-29 08:24:12 +00001153 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_add_hs_msg_to_checksum(
1154 ssl, MBEDTLS_SSL_HS_CERTIFICATE_VERIFY,
1155 buf, msg_len));
Jerry Yu8511f122022-01-29 10:01:04 +08001156
Gilles Peskine449bd832023-01-11 14:50:10 +01001157 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_finish_handshake_msg(
1158 ssl, buf_len, msg_len));
Jerry Yu8511f122022-01-29 10:01:04 +08001159
1160cleanup:
1161
Gilles Peskine449bd832023-01-11 14:50:10 +01001162 MBEDTLS_SSL_DEBUG_MSG(2, ("<= write certificate verify"));
1163 return ret;
Jerry Yu8511f122022-01-29 10:01:04 +08001164}
1165
Ronald Cron928cbd32022-10-04 16:14:26 +02001166#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED */
Jerry Yu90f152d2022-01-29 22:12:42 +08001167
Jerry Yu5cc35062022-01-28 16:16:08 +08001168/*
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001169 *
XiaokangQianc5c39d52021-11-09 11:55:10 +00001170 * STATE HANDLING: Incoming Finished message.
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001171 */
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001172/*
1173 * Implementation
1174 */
1175
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001176MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01001177static int ssl_tls13_preprocess_finished_message(mbedtls_ssl_context *ssl)
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001178{
1179 int ret;
1180
Xiaokang Qian73437382023-03-29 08:24:12 +00001181 ret = mbedtls_ssl_tls13_calculate_verify_data(
1182 ssl,
1183 ssl->handshake->state_local.finished_in.digest,
1184 sizeof(ssl->handshake->state_local.finished_in.digest),
1185 &ssl->handshake->state_local.finished_in.digest_len,
1186 ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT ?
1187 MBEDTLS_SSL_IS_SERVER : MBEDTLS_SSL_IS_CLIENT);
Gilles Peskine449bd832023-01-11 14:50:10 +01001188 if (ret != 0) {
1189 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_tls13_calculate_verify_data", ret);
1190 return ret;
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001191 }
1192
Gilles Peskine449bd832023-01-11 14:50:10 +01001193 return 0;
XiaokangQianaa5f5c12021-09-18 06:20:25 +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_parse_finished_message(mbedtls_ssl_context *ssl,
1198 const unsigned char *buf,
1199 const unsigned char *end)
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001200{
XiaokangQian33062842021-11-11 03:37:45 +00001201 /*
1202 * struct {
XiaokangQianc13f9352021-11-11 06:13:22 +00001203 * opaque verify_data[Hash.length];
XiaokangQian33062842021-11-11 03:37:45 +00001204 * } Finished;
1205 */
1206 const unsigned char *expected_verify_data =
1207 ssl->handshake->state_local.finished_in.digest;
1208 size_t expected_verify_data_len =
1209 ssl->handshake->state_local.finished_in.digest_len;
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001210 /* Structural validation */
Gilles Peskine449bd832023-01-11 14:50:10 +01001211 if ((size_t) (end - buf) != expected_verify_data_len) {
1212 MBEDTLS_SSL_DEBUG_MSG(1, ("bad finished message"));
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001213
Gilles Peskine449bd832023-01-11 14:50:10 +01001214 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR,
1215 MBEDTLS_ERR_SSL_DECODE_ERROR);
1216 return MBEDTLS_ERR_SSL_DECODE_ERROR;
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001217 }
1218
Gilles Peskine449bd832023-01-11 14:50:10 +01001219 MBEDTLS_SSL_DEBUG_BUF(4, "verify_data (self-computed):",
1220 expected_verify_data,
1221 expected_verify_data_len);
1222 MBEDTLS_SSL_DEBUG_BUF(4, "verify_data (received message):", buf,
1223 expected_verify_data_len);
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001224
1225 /* Semantic validation */
Gilles Peskine449bd832023-01-11 14:50:10 +01001226 if (mbedtls_ct_memcmp(buf,
1227 expected_verify_data,
1228 expected_verify_data_len) != 0) {
1229 MBEDTLS_SSL_DEBUG_MSG(1, ("bad finished message"));
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001230
Gilles Peskine449bd832023-01-11 14:50:10 +01001231 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_DECRYPT_ERROR,
1232 MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE);
1233 return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001234 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001235 return 0;
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001236}
1237
Gilles Peskine449bd832023-01-11 14:50:10 +01001238int mbedtls_ssl_tls13_process_finished_message(mbedtls_ssl_context *ssl)
XiaokangQianc5c39d52021-11-09 11:55:10 +00001239{
XiaokangQian33062842021-11-11 03:37:45 +00001240 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
XiaokangQianc5c39d52021-11-09 11:55:10 +00001241 unsigned char *buf;
Xiaofei Baieef15042021-11-18 07:29:56 +00001242 size_t buf_len;
XiaokangQianc5c39d52021-11-09 11:55:10 +00001243
Gilles Peskine449bd832023-01-11 14:50:10 +01001244 MBEDTLS_SSL_DEBUG_MSG(2, ("=> parse finished message"));
XiaokangQianc5c39d52021-11-09 11:55:10 +00001245
Xiaokang Qian73437382023-03-29 08:24:12 +00001246 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_tls13_fetch_handshake_msg(
1247 ssl, MBEDTLS_SSL_HS_FINISHED, &buf, &buf_len));
Jerry Yu0a92d6c2022-05-16 16:54:46 +08001248
1249 /* Preprocessing step: Compute handshake digest */
Gilles Peskine449bd832023-01-11 14:50:10 +01001250 MBEDTLS_SSL_PROC_CHK(ssl_tls13_preprocess_finished_message(ssl));
Jerry Yu0a92d6c2022-05-16 16:54:46 +08001251
Xiaokang Qian73437382023-03-29 08:24:12 +00001252 MBEDTLS_SSL_PROC_CHK(ssl_tls13_parse_finished_message(
1253 ssl, buf, buf + buf_len));
Jerry Yu0a92d6c2022-05-16 16:54:46 +08001254
Xiaokang Qian73437382023-03-29 08:24:12 +00001255 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_add_hs_msg_to_checksum(
1256 ssl, MBEDTLS_SSL_HS_FINISHED, buf, buf_len));
XiaokangQianc5c39d52021-11-09 11:55:10 +00001257
1258cleanup:
1259
Gilles Peskine449bd832023-01-11 14:50:10 +01001260 MBEDTLS_SSL_DEBUG_MSG(2, ("<= parse finished message"));
1261 return ret;
XiaokangQianc5c39d52021-11-09 11:55:10 +00001262}
1263
XiaokangQian74af2a82021-09-22 07:40:30 +00001264/*
1265 *
XiaokangQiancc90c942021-11-09 12:30:09 +00001266 * STATE HANDLING: Write and send Finished message.
XiaokangQian74af2a82021-09-22 07:40:30 +00001267 *
1268 */
XiaokangQian74af2a82021-09-22 07:40:30 +00001269/*
XiaokangQian35dc6252021-11-11 08:16:19 +00001270 * Implement
XiaokangQian74af2a82021-09-22 07:40:30 +00001271 */
1272
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001273MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01001274static int ssl_tls13_prepare_finished_message(mbedtls_ssl_context *ssl)
XiaokangQian74af2a82021-09-22 07:40:30 +00001275{
1276 int ret;
1277
1278 /* Compute transcript of handshake up to now. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001279 ret = mbedtls_ssl_tls13_calculate_verify_data(ssl,
1280 ssl->handshake->state_local.finished_out.digest,
1281 sizeof(ssl->handshake->state_local.finished_out.
1282 digest),
1283 &ssl->handshake->state_local.finished_out.digest_len,
1284 ssl->conf->endpoint);
XiaokangQian74af2a82021-09-22 07:40:30 +00001285
Gilles Peskine449bd832023-01-11 14:50:10 +01001286 if (ret != 0) {
1287 MBEDTLS_SSL_DEBUG_RET(1, "calculate_verify_data failed", ret);
1288 return ret;
XiaokangQian74af2a82021-09-22 07:40:30 +00001289 }
1290
Gilles Peskine449bd832023-01-11 14:50:10 +01001291 return 0;
XiaokangQian74af2a82021-09-22 07:40:30 +00001292}
1293
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001294MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01001295static int ssl_tls13_write_finished_message_body(mbedtls_ssl_context *ssl,
1296 unsigned char *buf,
1297 unsigned char *end,
1298 size_t *out_len)
XiaokangQian74af2a82021-09-22 07:40:30 +00001299{
XiaokangQian8773aa02021-11-10 07:33:09 +00001300 size_t verify_data_len = ssl->handshake->state_local.finished_out.digest_len;
XiaokangQian0fa66432021-11-15 03:33:57 +00001301 /*
1302 * struct {
1303 * opaque verify_data[Hash.length];
1304 * } Finished;
1305 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001306 MBEDTLS_SSL_CHK_BUF_PTR(buf, end, verify_data_len);
XiaokangQian74af2a82021-09-22 07:40:30 +00001307
Gilles Peskine449bd832023-01-11 14:50:10 +01001308 memcpy(buf, ssl->handshake->state_local.finished_out.digest,
1309 verify_data_len);
XiaokangQian74af2a82021-09-22 07:40:30 +00001310
Xiaofei Baid25fab62021-12-02 06:36:27 +00001311 *out_len = verify_data_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01001312 return 0;
XiaokangQian74af2a82021-09-22 07:40:30 +00001313}
XiaokangQianc5c39d52021-11-09 11:55:10 +00001314
XiaokangQian35dc6252021-11-11 08:16:19 +00001315/* Main entry point: orchestrates the other functions */
Gilles Peskine449bd832023-01-11 14:50:10 +01001316int mbedtls_ssl_tls13_write_finished_message(mbedtls_ssl_context *ssl)
XiaokangQian35dc6252021-11-11 08:16:19 +00001317{
1318 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1319 unsigned char *buf;
1320 size_t buf_len, msg_len;
1321
Gilles Peskine449bd832023-01-11 14:50:10 +01001322 MBEDTLS_SSL_DEBUG_MSG(2, ("=> write finished message"));
XiaokangQian35dc6252021-11-11 08:16:19 +00001323
Gilles Peskine449bd832023-01-11 14:50:10 +01001324 MBEDTLS_SSL_PROC_CHK(ssl_tls13_prepare_finished_message(ssl));
XiaokangQiandce82242021-11-15 06:01:26 +00001325
Gilles Peskine449bd832023-01-11 14:50:10 +01001326 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_start_handshake_msg(ssl,
1327 MBEDTLS_SSL_HS_FINISHED, &buf, &buf_len));
XiaokangQian35dc6252021-11-11 08:16:19 +00001328
Gilles Peskine449bd832023-01-11 14:50:10 +01001329 MBEDTLS_SSL_PROC_CHK(ssl_tls13_write_finished_message_body(
1330 ssl, buf, buf + buf_len, &msg_len));
XiaokangQian35dc6252021-11-11 08:16:19 +00001331
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +01001332 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_add_hs_msg_to_checksum(ssl,
Manuel Pégourié-Gonnard43cc1272023-02-06 11:48:19 +01001333 MBEDTLS_SSL_HS_FINISHED, buf, msg_len));
XiaokangQian35dc6252021-11-11 08:16:19 +00001334
Gilles Peskine449bd832023-01-11 14:50:10 +01001335 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_finish_handshake_msg(
1336 ssl, buf_len, msg_len));
XiaokangQian35dc6252021-11-11 08:16:19 +00001337cleanup:
1338
Gilles Peskine449bd832023-01-11 14:50:10 +01001339 MBEDTLS_SSL_DEBUG_MSG(2, ("<= write finished message"));
1340 return ret;
XiaokangQian35dc6252021-11-11 08:16:19 +00001341}
1342
Gilles Peskine449bd832023-01-11 14:50:10 +01001343void mbedtls_ssl_tls13_handshake_wrapup(mbedtls_ssl_context *ssl)
Jerry Yu378254d2021-10-30 21:44:47 +08001344{
1345
Gilles Peskine449bd832023-01-11 14:50:10 +01001346 MBEDTLS_SSL_DEBUG_MSG(3, ("=> handshake wrapup"));
Jerry Yu378254d2021-10-30 21:44:47 +08001347
Gilles Peskine449bd832023-01-11 14:50:10 +01001348 MBEDTLS_SSL_DEBUG_MSG(1, ("Switch to application keys for inbound traffic"));
1349 mbedtls_ssl_set_inbound_transform(ssl, ssl->transform_application);
Jerry Yue8c1fca2022-05-18 14:48:56 +08001350
Gilles Peskine449bd832023-01-11 14:50:10 +01001351 MBEDTLS_SSL_DEBUG_MSG(1, ("Switch to application keys for outbound traffic"));
1352 mbedtls_ssl_set_outbound_transform(ssl, ssl->transform_application);
Jerry Yue8c1fca2022-05-18 14:48:56 +08001353
Jerry Yu378254d2021-10-30 21:44:47 +08001354 /*
Jerry Yucfe64f02021-11-15 13:54:06 +08001355 * Free the previous session and switch to the current one.
Jerry Yu378254d2021-10-30 21:44:47 +08001356 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001357 if (ssl->session) {
1358 mbedtls_ssl_session_free(ssl->session);
1359 mbedtls_free(ssl->session);
Jerry Yu378254d2021-10-30 21:44:47 +08001360 }
1361 ssl->session = ssl->session_negotiate;
1362 ssl->session_negotiate = NULL;
1363
Gilles Peskine449bd832023-01-11 14:50:10 +01001364 MBEDTLS_SSL_DEBUG_MSG(3, ("<= handshake wrapup"));
Jerry Yu378254d2021-10-30 21:44:47 +08001365}
1366
Ronald Cron49ad6192021-11-24 16:25:31 +01001367/*
1368 *
1369 * STATE HANDLING: Write ChangeCipherSpec
1370 *
1371 */
1372#if defined(MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001373MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01001374static int ssl_tls13_write_change_cipher_spec_body(mbedtls_ssl_context *ssl,
1375 unsigned char *buf,
1376 unsigned char *end,
1377 size_t *olen)
Ronald Cron49ad6192021-11-24 16:25:31 +01001378{
1379 ((void) ssl);
1380
Gilles Peskine449bd832023-01-11 14:50:10 +01001381 MBEDTLS_SSL_CHK_BUF_PTR(buf, end, 1);
Ronald Cron49ad6192021-11-24 16:25:31 +01001382 buf[0] = 1;
1383 *olen = 1;
1384
Gilles Peskine449bd832023-01-11 14:50:10 +01001385 return 0;
Ronald Cron49ad6192021-11-24 16:25:31 +01001386}
1387
Gilles Peskine449bd832023-01-11 14:50:10 +01001388int mbedtls_ssl_tls13_write_change_cipher_spec(mbedtls_ssl_context *ssl)
Ronald Cron49ad6192021-11-24 16:25:31 +01001389{
1390 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1391
Gilles Peskine449bd832023-01-11 14:50:10 +01001392 MBEDTLS_SSL_DEBUG_MSG(2, ("=> write change cipher spec"));
Ronald Cron49ad6192021-11-24 16:25:31 +01001393
Ronald Cron49ad6192021-11-24 16:25:31 +01001394 /* Write CCS message */
Gilles Peskine449bd832023-01-11 14:50:10 +01001395 MBEDTLS_SSL_PROC_CHK(ssl_tls13_write_change_cipher_spec_body(
1396 ssl, ssl->out_msg,
1397 ssl->out_msg + MBEDTLS_SSL_OUT_CONTENT_LEN,
1398 &ssl->out_msglen));
Ronald Cron49ad6192021-11-24 16:25:31 +01001399
1400 ssl->out_msgtype = MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC;
1401
Ronald Cron49ad6192021-11-24 16:25:31 +01001402 /* Dispatch message */
Gilles Peskine449bd832023-01-11 14:50:10 +01001403 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_write_record(ssl, 0));
Ronald Cron49ad6192021-11-24 16:25:31 +01001404
1405cleanup:
1406
Gilles Peskine449bd832023-01-11 14:50:10 +01001407 MBEDTLS_SSL_DEBUG_MSG(2, ("<= write change cipher spec"));
1408 return ret;
Ronald Cron49ad6192021-11-24 16:25:31 +01001409}
1410
1411#endif /* MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE */
1412
Xiaokang Qianecc29482022-11-02 07:52:47 +00001413/* Early Data Indication Extension
Xiaokang Qian0e97d4d2022-10-24 11:12:51 +00001414 *
Xiaokang Qian0e97d4d2022-10-24 11:12:51 +00001415 * struct {
1416 * select ( Handshake.msg_type ) {
Xiaokang Qianecc29482022-11-02 07:52:47 +00001417 * ...
Xiaokang Qian0e97d4d2022-10-24 11:12:51 +00001418 * case client_hello: Empty;
1419 * case encrypted_extensions: Empty;
1420 * };
1421 * } EarlyDataIndication;
1422 */
1423#if defined(MBEDTLS_SSL_EARLY_DATA)
Gilles Peskine449bd832023-01-11 14:50:10 +01001424int mbedtls_ssl_tls13_write_early_data_ext(mbedtls_ssl_context *ssl,
1425 unsigned char *buf,
1426 const unsigned char *end,
1427 size_t *out_len)
Xiaokang Qian0e97d4d2022-10-24 11:12:51 +00001428{
1429 unsigned char *p = buf;
1430 *out_len = 0;
1431 ((void) ssl);
1432
Gilles Peskine449bd832023-01-11 14:50:10 +01001433 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 4);
Xiaokang Qian0e97d4d2022-10-24 11:12:51 +00001434
Gilles Peskine449bd832023-01-11 14:50:10 +01001435 MBEDTLS_PUT_UINT16_BE(MBEDTLS_TLS_EXT_EARLY_DATA, p, 0);
1436 MBEDTLS_PUT_UINT16_BE(0, p, 2);
Xiaokang Qian0e97d4d2022-10-24 11:12:51 +00001437
1438 *out_len = 4;
Xiaokang Qian2cd5ce02022-11-15 10:33:53 +00001439
Gilles Peskine449bd832023-01-11 14:50:10 +01001440 mbedtls_ssl_tls13_set_hs_sent_ext_mask(ssl, MBEDTLS_TLS_EXT_EARLY_DATA);
Xiaokang Qian2cd5ce02022-11-15 10:33:53 +00001441
Gilles Peskine449bd832023-01-11 14:50:10 +01001442 return 0;
Xiaokang Qian0e97d4d2022-10-24 11:12:51 +00001443}
1444#endif /* MBEDTLS_SSL_EARLY_DATA */
1445
XiaokangQian78b1fa72022-01-19 06:56:30 +00001446/* Reset SSL context and update hash for handling HRR.
1447 *
1448 * Replace Transcript-Hash(X) by
1449 * Transcript-Hash( message_hash ||
1450 * 00 00 Hash.length ||
1451 * X )
1452 * A few states of the handshake are preserved, including:
1453 * - session ID
1454 * - session ticket
1455 * - negotiated ciphersuite
1456 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001457int mbedtls_ssl_reset_transcript_for_hrr(mbedtls_ssl_context *ssl)
XiaokangQian78b1fa72022-01-19 06:56:30 +00001458{
1459 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Przemyslaw Stekielda645252022-09-14 12:50:51 +02001460 unsigned char hash_transcript[PSA_HASH_MAX_SIZE + 4];
XiaokangQian0ece9982022-01-24 08:56:23 +00001461 size_t hash_len;
Xiaokang Qian6b980012023-02-07 03:17:45 +00001462 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
1463 ssl->handshake->ciphersuite_info;
XiaokangQian78b1fa72022-01-19 06:56:30 +00001464
Gilles Peskine449bd832023-01-11 14:50:10 +01001465 MBEDTLS_SSL_DEBUG_MSG(3, ("Reset SSL session for HRR"));
XiaokangQian78b1fa72022-01-19 06:56:30 +00001466
Dave Rodgman2eab4622023-10-05 13:30:37 +01001467 ret = mbedtls_ssl_get_handshake_transcript(ssl, (mbedtls_md_type_t) ciphersuite_info->mac,
Gilles Peskine449bd832023-01-11 14:50:10 +01001468 hash_transcript + 4,
1469 PSA_HASH_MAX_SIZE,
1470 &hash_len);
1471 if (ret != 0) {
Manuel Pégourié-Gonnardda7979b2023-02-21 09:31:10 +01001472 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_get_handshake_transcript", ret);
Gilles Peskine449bd832023-01-11 14:50:10 +01001473 return ret;
XiaokangQian0ece9982022-01-24 08:56:23 +00001474 }
1475
1476 hash_transcript[0] = MBEDTLS_SSL_HS_MESSAGE_HASH;
1477 hash_transcript[1] = 0;
1478 hash_transcript[2] = 0;
1479 hash_transcript[3] = (unsigned char) hash_len;
1480
1481 hash_len += 4;
1482
Manuel Pégourié-Gonnardda7979b2023-02-21 09:31:10 +01001483 MBEDTLS_SSL_DEBUG_BUF(4, "Truncated handshake transcript",
1484 hash_transcript, hash_len);
1485
Manuel Pégourié-Gonnardd7a7a232023-02-05 10:26:49 +01001486 /* Reset running hash and replace it with a hash of the transcript */
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +01001487 ret = mbedtls_ssl_reset_checksum(ssl);
1488 if (ret != 0) {
1489 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_reset_checksum", ret);
1490 return ret;
1491 }
1492 ret = ssl->handshake->update_checksum(ssl, hash_transcript, hash_len);
1493 if (ret != 0) {
1494 MBEDTLS_SSL_DEBUG_RET(1, "update_checksum", ret);
1495 return ret;
1496 }
Przemyslaw Stekiel4b3fff42022-02-14 16:39:52 +01001497
Gilles Peskine449bd832023-01-11 14:50:10 +01001498 return ret;
XiaokangQian78b1fa72022-01-19 06:56:30 +00001499}
1500
Valerio Settic9ae8622023-07-25 11:23:50 +02001501#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_EPHEMERAL_ENABLED)
XiaokangQian7807f9f2022-02-15 10:04:37 +00001502
Przemek Stekiel7ac93be2023-07-04 10:02:38 +02001503int mbedtls_ssl_tls13_read_public_xxdhe_share(mbedtls_ssl_context *ssl,
Gilles Peskine449bd832023-01-11 14:50:10 +01001504 const unsigned char *buf,
1505 size_t buf_len)
XiaokangQian7807f9f2022-02-15 10:04:37 +00001506{
Gilles Peskine449bd832023-01-11 14:50:10 +01001507 uint8_t *p = (uint8_t *) buf;
XiaokangQiancfd925f2022-04-14 07:10:37 +00001508 const uint8_t *end = buf + buf_len;
XiaokangQian9b5d04b2022-04-10 10:20:43 +00001509 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001510
XiaokangQian9b5d04b2022-04-10 10:20:43 +00001511 /* Get size of the TLS opaque key_exchange field of the KeyShareEntry struct. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001512 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, 2);
1513 uint16_t peerkey_len = MBEDTLS_GET_UINT16_BE(p, 0);
XiaokangQian9b5d04b2022-04-10 10:20:43 +00001514 p += 2;
XiaokangQian3207a322022-02-23 03:15:27 +00001515
XiaokangQian9b5d04b2022-04-10 10:20:43 +00001516 /* Check if key size is consistent with given buffer length. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001517 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, peerkey_len);
XiaokangQian9b5d04b2022-04-10 10:20:43 +00001518
Gilles Peskine12c5aaa2023-10-02 14:55:45 +02001519 /* Store peer's ECDH/FFDH public key. */
1520 if (peerkey_len > sizeof(handshake->xxdh_psa_peerkey)) {
Gilles Peskine530c4232023-10-02 15:37:23 +02001521 MBEDTLS_SSL_DEBUG_MSG(1, ("Invalid public key length: %u > %" MBEDTLS_PRINTF_SIZET,
1522 (unsigned) peerkey_len,
1523 sizeof(handshake->xxdh_psa_peerkey)));
Gilles Peskine12c5aaa2023-10-02 14:55:45 +02001524 return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
1525 }
Przemek Stekiel7ac93be2023-07-04 10:02:38 +02001526 memcpy(handshake->xxdh_psa_peerkey, p, peerkey_len);
1527 handshake->xxdh_psa_peerkey_len = peerkey_len;
XiaokangQian9b5d04b2022-04-10 10:20:43 +00001528
Gilles Peskine449bd832023-01-11 14:50:10 +01001529 return 0;
XiaokangQian3207a322022-02-23 03:15:27 +00001530}
Jerry Yu89e103c2022-03-30 22:43:29 +08001531
Valerio Setti711f8532023-07-31 11:28:07 +02001532#if defined(PSA_WANT_ALG_FFDH)
Przemek Stekielda4fba62023-06-02 14:52:28 +02001533static psa_status_t mbedtls_ssl_get_psa_ffdh_info_from_tls_id(
Przemek Stekiel75a5a9c2023-06-12 11:21:18 +02001534 uint16_t tls_id, size_t *bits, psa_key_type_t *key_type)
Przemek Stekiel29c219c2023-05-31 15:21:04 +02001535{
Przemek Stekiel75a5a9c2023-06-12 11:21:18 +02001536 switch (tls_id) {
Przemek Stekiel29c219c2023-05-31 15:21:04 +02001537 case MBEDTLS_SSL_IANA_TLS_GROUP_FFDHE2048:
1538 *bits = 2048;
1539 *key_type = PSA_KEY_TYPE_DH_KEY_PAIR(PSA_DH_FAMILY_RFC7919);
1540 return PSA_SUCCESS;
1541 case MBEDTLS_SSL_IANA_TLS_GROUP_FFDHE3072:
1542 *bits = 3072;
1543 *key_type = PSA_KEY_TYPE_DH_KEY_PAIR(PSA_DH_FAMILY_RFC7919);
1544 return PSA_SUCCESS;
1545 case MBEDTLS_SSL_IANA_TLS_GROUP_FFDHE4096:
1546 *bits = 4096;
1547 *key_type = PSA_KEY_TYPE_DH_KEY_PAIR(PSA_DH_FAMILY_RFC7919);
1548 return PSA_SUCCESS;
1549 case MBEDTLS_SSL_IANA_TLS_GROUP_FFDHE6144:
1550 *bits = 6144;
1551 *key_type = PSA_KEY_TYPE_DH_KEY_PAIR(PSA_DH_FAMILY_RFC7919);
1552 return PSA_SUCCESS;
1553 case MBEDTLS_SSL_IANA_TLS_GROUP_FFDHE8192:
1554 *bits = 8192;
1555 *key_type = PSA_KEY_TYPE_DH_KEY_PAIR(PSA_DH_FAMILY_RFC7919);
1556 return PSA_SUCCESS;
1557 default:
1558 return PSA_ERROR_NOT_SUPPORTED;
1559 }
1560}
Valerio Setti711f8532023-07-31 11:28:07 +02001561#endif /* PSA_WANT_ALG_FFDH */
Przemek Stekiel29c219c2023-05-31 15:21:04 +02001562
Przemek Stekiel408569f2023-07-06 11:26:44 +02001563int mbedtls_ssl_tls13_generate_and_write_xxdh_key_exchange(
Gilles Peskine449bd832023-01-11 14:50:10 +01001564 mbedtls_ssl_context *ssl,
1565 uint16_t named_group,
1566 unsigned char *buf,
1567 unsigned char *end,
1568 size_t *out_len)
Jerry Yu89e103c2022-03-30 22:43:29 +08001569{
1570 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
1571 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
1572 psa_key_attributes_t key_attributes;
1573 size_t own_pubkey_len;
1574 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
Przemek Stekiel29c219c2023-05-31 15:21:04 +02001575 size_t bits = 0;
Przemek Stekiel75a5a9c2023-06-12 11:21:18 +02001576 psa_key_type_t key_type = PSA_KEY_TYPE_NONE;
1577 psa_algorithm_t alg = PSA_ALG_NONE;
Przemek Stekiel29c219c2023-05-31 15:21:04 +02001578 size_t buf_size = (size_t) (end - buf);
Jerry Yu89e103c2022-03-30 22:43:29 +08001579
Przemek Stekiel29c219c2023-05-31 15:21:04 +02001580 MBEDTLS_SSL_DEBUG_MSG(1, ("Perform PSA-based ECDH/FFDH computation."));
Jerry Yu89e103c2022-03-30 22:43:29 +08001581
Valerio Setti40d9ca92023-01-04 16:08:04 +01001582 /* Convert EC's TLS ID to PSA key type. */
Przemek Stekiel29c219c2023-05-31 15:21:04 +02001583#if defined(PSA_WANT_ALG_ECDH)
Xiaokang Qian73437382023-03-29 08:24:12 +00001584 if (mbedtls_ssl_get_psa_curve_info_from_tls_id(
Przemek Stekielda4fba62023-06-02 14:52:28 +02001585 named_group, &key_type, &bits) == PSA_SUCCESS) {
1586 alg = PSA_ALG_ECDH;
Przemek Stekiel29c219c2023-05-31 15:21:04 +02001587 }
1588#endif
1589#if defined(PSA_WANT_ALG_FFDH)
Przemek Stekiel75a5a9c2023-06-12 11:21:18 +02001590 if (mbedtls_ssl_get_psa_ffdh_info_from_tls_id(named_group, &bits,
1591 &key_type) == PSA_SUCCESS) {
Przemek Stekielda4fba62023-06-02 14:52:28 +02001592 alg = PSA_ALG_FFDH;
Przemek Stekiel29c219c2023-05-31 15:21:04 +02001593 }
1594#endif
1595
Przemek Stekiel75a5a9c2023-06-12 11:21:18 +02001596 if (key_type == PSA_KEY_TYPE_NONE) {
Gilles Peskine449bd832023-01-11 14:50:10 +01001597 return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
Valerio Setti40d9ca92023-01-04 16:08:04 +01001598 }
Przemek Stekiel29c219c2023-05-31 15:21:04 +02001599
Przemek Stekielda4fba62023-06-02 14:52:28 +02001600 if (buf_size < PSA_BITS_TO_BYTES(bits)) {
Przemek Stekielda4fba62023-06-02 14:52:28 +02001601 return MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL;
1602 }
1603
Przemek Stekiel7ac93be2023-07-04 10:02:38 +02001604 handshake->xxdh_psa_type = key_type;
Valerio Settiea59c432023-07-25 11:14:03 +02001605 ssl->handshake->xxdh_psa_bits = bits;
Jerry Yu89e103c2022-03-30 22:43:29 +08001606
1607 key_attributes = psa_key_attributes_init();
Gilles Peskine449bd832023-01-11 14:50:10 +01001608 psa_set_key_usage_flags(&key_attributes, PSA_KEY_USAGE_DERIVE);
Przemek Stekielda4fba62023-06-02 14:52:28 +02001609 psa_set_key_algorithm(&key_attributes, alg);
Przemek Stekiel7ac93be2023-07-04 10:02:38 +02001610 psa_set_key_type(&key_attributes, handshake->xxdh_psa_type);
Valerio Settiea59c432023-07-25 11:14:03 +02001611 psa_set_key_bits(&key_attributes, handshake->xxdh_psa_bits);
Jerry Yu89e103c2022-03-30 22:43:29 +08001612
Przemek Stekiel29c219c2023-05-31 15:21:04 +02001613 /* Generate ECDH/FFDH private key. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001614 status = psa_generate_key(&key_attributes,
Przemek Stekiel7ac93be2023-07-04 10:02:38 +02001615 &handshake->xxdh_psa_privkey);
Gilles Peskine449bd832023-01-11 14:50:10 +01001616 if (status != PSA_SUCCESS) {
Andrzej Kurek8a045ce2022-12-23 11:00:06 -05001617 ret = PSA_TO_MBEDTLS_ERR(status);
Gilles Peskine449bd832023-01-11 14:50:10 +01001618 MBEDTLS_SSL_DEBUG_RET(1, "psa_generate_key", ret);
1619 return ret;
Jerry Yu89e103c2022-03-30 22:43:29 +08001620
1621 }
1622
Przemek Stekiel29c219c2023-05-31 15:21:04 +02001623 /* Export the public part of the ECDH/FFDH private key from PSA. */
Przemek Stekiel7ac93be2023-07-04 10:02:38 +02001624 status = psa_export_public_key(handshake->xxdh_psa_privkey,
Przemek Stekiel29c219c2023-05-31 15:21:04 +02001625 buf, buf_size,
Gilles Peskine449bd832023-01-11 14:50:10 +01001626 &own_pubkey_len);
Przemek Stekiel29c219c2023-05-31 15:21:04 +02001627
Gilles Peskine449bd832023-01-11 14:50:10 +01001628 if (status != PSA_SUCCESS) {
Andrzej Kurek8a045ce2022-12-23 11:00:06 -05001629 ret = PSA_TO_MBEDTLS_ERR(status);
Gilles Peskine449bd832023-01-11 14:50:10 +01001630 MBEDTLS_SSL_DEBUG_RET(1, "psa_export_public_key", ret);
1631 return ret;
Jerry Yu89e103c2022-03-30 22:43:29 +08001632 }
1633
1634 *out_len = own_pubkey_len;
1635
Gilles Peskine449bd832023-01-11 14:50:10 +01001636 return 0;
Jerry Yu89e103c2022-03-30 22:43:29 +08001637}
Valerio Settic9ae8622023-07-25 11:23:50 +02001638#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_EPHEMERAL_ENABLED */
XiaokangQian7807f9f2022-02-15 10:04:37 +00001639
Jerry Yu0c354a22022-08-29 15:25:36 +08001640/* RFC 8446 section 4.2
1641 *
1642 * If an implementation receives an extension which it recognizes and which is
1643 * not specified for the message in which it appears, it MUST abort the handshake
1644 * with an "illegal_parameter" alert.
1645 *
1646 */
Jerry Yuc4bf5d62022-10-29 09:08:47 +08001647int mbedtls_ssl_tls13_check_received_extension(
Gilles Peskine449bd832023-01-11 14:50:10 +01001648 mbedtls_ssl_context *ssl,
1649 int hs_msg_type,
1650 unsigned int received_extension_type,
1651 uint32_t hs_msg_allowed_extensions_mask)
Jerry Yu0c354a22022-08-29 15:25:36 +08001652{
Jerry Yudf0ad652022-10-31 13:20:57 +08001653 uint32_t extension_mask = mbedtls_ssl_get_extension_mask(
Gilles Peskine449bd832023-01-11 14:50:10 +01001654 received_extension_type);
Jerry Yu0c354a22022-08-29 15:25:36 +08001655
Jerry Yu79aa7212022-11-08 21:30:21 +08001656 MBEDTLS_SSL_PRINT_EXT(
Gilles Peskine449bd832023-01-11 14:50:10 +01001657 3, hs_msg_type, received_extension_type, "received");
Jerry Yu0c354a22022-08-29 15:25:36 +08001658
Gilles Peskine449bd832023-01-11 14:50:10 +01001659 if ((extension_mask & hs_msg_allowed_extensions_mask) == 0) {
Jerry Yu79aa7212022-11-08 21:30:21 +08001660 MBEDTLS_SSL_PRINT_EXT(
Gilles Peskine449bd832023-01-11 14:50:10 +01001661 3, hs_msg_type, received_extension_type, "is illegal");
Jerry Yu0c354a22022-08-29 15:25:36 +08001662 MBEDTLS_SSL_PEND_FATAL_ALERT(
1663 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER,
Gilles Peskine449bd832023-01-11 14:50:10 +01001664 MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER);
1665 return MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER;
Jerry Yu0c354a22022-08-29 15:25:36 +08001666 }
1667
1668 ssl->handshake->received_extensions |= extension_mask;
Jerry Yuc4bf5d62022-10-29 09:08:47 +08001669 /*
1670 * If it is a message containing extension responses, check that we
1671 * previously sent the extension.
1672 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001673 switch (hs_msg_type) {
Jerry Yu0c354a22022-08-29 15:25:36 +08001674 case MBEDTLS_SSL_HS_SERVER_HELLO:
Jerry Yudf0ad652022-10-31 13:20:57 +08001675 case MBEDTLS_SSL_TLS1_3_HS_HELLO_RETRY_REQUEST:
Jerry Yu0c354a22022-08-29 15:25:36 +08001676 case MBEDTLS_SSL_HS_ENCRYPTED_EXTENSIONS:
1677 case MBEDTLS_SSL_HS_CERTIFICATE:
Jerry Yuc4bf5d62022-10-29 09:08:47 +08001678 /* Check if the received extension is sent by peer message.*/
Gilles Peskine449bd832023-01-11 14:50:10 +01001679 if ((ssl->handshake->sent_extensions & extension_mask) != 0) {
1680 return 0;
1681 }
Jerry Yu0c354a22022-08-29 15:25:36 +08001682 break;
1683 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01001684 return 0;
Jerry Yu0c354a22022-08-29 15:25:36 +08001685 }
1686
Jerry Yu79aa7212022-11-08 21:30:21 +08001687 MBEDTLS_SSL_PRINT_EXT(
Gilles Peskine449bd832023-01-11 14:50:10 +01001688 3, hs_msg_type, received_extension_type, "is unsupported");
Jerry Yu0c354a22022-08-29 15:25:36 +08001689 MBEDTLS_SSL_PEND_FATAL_ALERT(
1690 MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_EXT,
Gilles Peskine449bd832023-01-11 14:50:10 +01001691 MBEDTLS_ERR_SSL_UNSUPPORTED_EXTENSION);
1692 return MBEDTLS_ERR_SSL_UNSUPPORTED_EXTENSION;
Jerry Yu0c354a22022-08-29 15:25:36 +08001693}
1694
Jan Bruckner151f6422023-02-10 12:45:19 +01001695#if defined(MBEDTLS_SSL_RECORD_SIZE_LIMIT)
Jan Bruckner1a38e542023-03-15 14:15:11 +01001696/* RFC 8449, section 4:
1697 *
Jan Bruckner151f6422023-02-10 12:45:19 +01001698 * The ExtensionData of the "record_size_limit" extension is
1699 * RecordSizeLimit:
1700 * uint16 RecordSizeLimit;
1701 */
1702MBEDTLS_CHECK_RETURN_CRITICAL
1703int mbedtls_ssl_tls13_parse_record_size_limit_ext(mbedtls_ssl_context *ssl,
1704 const unsigned char *buf,
1705 const unsigned char *end)
1706{
Jan Bruckner1a38e542023-03-15 14:15:11 +01001707 const unsigned char *p = buf;
1708 uint16_t record_size_limit;
Jan Brucknera0589e72023-03-15 11:04:45 +01001709 const size_t extension_data_len = end - buf;
Jan Bruckner1a38e542023-03-15 14:15:11 +01001710
Xiaokang Qian73437382023-03-29 08:24:12 +00001711 if (extension_data_len !=
1712 MBEDTLS_SSL_RECORD_SIZE_LIMIT_EXTENSION_DATA_LENGTH) {
Jan Bruckner151f6422023-02-10 12:45:19 +01001713 MBEDTLS_SSL_DEBUG_MSG(2,
Jan Bruckner1a38e542023-03-15 14:15:11 +01001714 ("record_size_limit extension has invalid length: %"
1715 MBEDTLS_PRINTF_SIZET " Bytes",
Jan Bruckner151f6422023-02-10 12:45:19 +01001716 extension_data_len));
1717
1718 MBEDTLS_SSL_PEND_FATAL_ALERT(
1719 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER,
1720 MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER);
1721 return MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER;
1722 }
1723
Jan Bruckner151f6422023-02-10 12:45:19 +01001724 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, 2);
1725 record_size_limit = MBEDTLS_GET_UINT16_BE(p, 0);
1726
1727 MBEDTLS_SSL_DEBUG_MSG(2, ("RecordSizeLimit: %u Bytes", record_size_limit));
1728
Jan Bruckner1a38e542023-03-15 14:15:11 +01001729 /* RFC 8449, section 4
Jan Bruckner151f6422023-02-10 12:45:19 +01001730 *
1731 * Endpoints MUST NOT send a "record_size_limit" extension with a value
1732 * smaller than 64. An endpoint MUST treat receipt of a smaller value
1733 * as a fatal error and generate an "illegal_parameter" alert.
1734 */
Jan Brucknera0589e72023-03-15 11:04:45 +01001735 if (record_size_limit < MBEDTLS_SSL_RECORD_SIZE_LIMIT_MIN) {
Jan Bruckner151f6422023-02-10 12:45:19 +01001736 MBEDTLS_SSL_PEND_FATAL_ALERT(
1737 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER,
1738 MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER);
1739 return MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER;
1740 }
1741
Xiaokang Qian73437382023-03-29 08:24:12 +00001742 MBEDTLS_SSL_DEBUG_MSG(
1743 2, ("record_size_limit extension is still in development. Aborting handshake."));
Jan Bruckner151f6422023-02-10 12:45:19 +01001744
1745 MBEDTLS_SSL_PEND_FATAL_ALERT(
1746 MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_EXT,
1747 MBEDTLS_ERR_SSL_UNSUPPORTED_EXTENSION);
1748 return MBEDTLS_ERR_SSL_UNSUPPORTED_EXTENSION;
1749}
1750#endif /* MBEDTLS_SSL_RECORD_SIZE_LIMIT */
1751
Jerry Yufb4b6472022-01-27 15:03:26 +08001752#endif /* MBEDTLS_SSL_TLS_C && MBEDTLS_SSL_PROTO_TLS1_3 */