blob: f8e078edfb2f56ceba41d4d013887d612a69d6ea [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"
40#include "mbedtls/psa_util.h"
41
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,
372 ssl->handshake->ciphersuite_info->mac,
373 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:
Ronald Cronde08cf32022-10-04 17:15:35 +0200840#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED */
Xiaofei Bai79595ac2021-10-26 07:16:45 +0000841
Gilles Peskine449bd832023-01-11 14:50:10 +0100842 MBEDTLS_SSL_DEBUG_MSG(2, ("<= parse certificate"));
843 return ret;
Xiaofei Bai947571e2021-09-29 09:12:03 +0000844}
Ronald Cron928cbd32022-10-04 16:14:26 +0200845#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED)
Jerry Yu7399d0d2022-01-30 17:54:19 +0800846/*
847 * enum {
848 * X509(0),
849 * RawPublicKey(2),
850 * (255)
851 * } CertificateType;
852 *
853 * struct {
854 * select (certificate_type) {
855 * case RawPublicKey:
856 * // From RFC 7250 ASN.1_subjectPublicKeyInfo
857 * opaque ASN1_subjectPublicKeyInfo<1..2^24-1>;
858 *
859 * case X509:
860 * opaque cert_data<1..2^24-1>;
861 * };
862 * Extension extensions<0..2^16-1>;
863 * } CertificateEntry;
864 *
865 * struct {
866 * opaque certificate_request_context<0..2^8-1>;
867 * CertificateEntry certificate_list<0..2^24-1>;
868 * } Certificate;
869 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200870MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100871static int ssl_tls13_write_certificate_body(mbedtls_ssl_context *ssl,
872 unsigned char *buf,
873 unsigned char *end,
874 size_t *out_len)
Jerry Yu5cc35062022-01-28 16:16:08 +0800875{
Gilles Peskine449bd832023-01-11 14:50:10 +0100876 const mbedtls_x509_crt *crt = mbedtls_ssl_own_cert(ssl);
Jerry Yu3e536442022-02-15 11:05:59 +0800877 unsigned char *p = buf;
Jerry Yuc8d8d4e2022-02-18 12:10:03 +0800878 unsigned char *certificate_request_context =
Gilles Peskine449bd832023-01-11 14:50:10 +0100879 ssl->handshake->certificate_request_context;
Jerry Yuc8d8d4e2022-02-18 12:10:03 +0800880 unsigned char certificate_request_context_len =
Gilles Peskine449bd832023-01-11 14:50:10 +0100881 ssl->handshake->certificate_request_context_len;
Jerry Yuc8d8d4e2022-02-18 12:10:03 +0800882 unsigned char *p_certificate_list_len;
Jerry Yu5cc35062022-01-28 16:16:08 +0800883
Jerry Yu5cc35062022-01-28 16:16:08 +0800884
Jerry Yu3391ac02022-02-16 11:21:37 +0800885 /* ...
886 * opaque certificate_request_context<0..2^8-1>;
887 * ...
888 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100889 MBEDTLS_SSL_CHK_BUF_PTR(p, end, certificate_request_context_len + 1);
Jerry Yuc8d8d4e2022-02-18 12:10:03 +0800890 *p++ = certificate_request_context_len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100891 if (certificate_request_context_len > 0) {
892 memcpy(p, certificate_request_context, certificate_request_context_len);
Jerry Yuc8d8d4e2022-02-18 12:10:03 +0800893 p += certificate_request_context_len;
Jerry Yu537530d2022-02-15 14:00:57 +0800894 }
895
Jerry Yu3391ac02022-02-16 11:21:37 +0800896 /* ...
897 * CertificateEntry certificate_list<0..2^24-1>;
898 * ...
899 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100900 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 3);
Jerry Yuc8d8d4e2022-02-18 12:10:03 +0800901 p_certificate_list_len = p;
Jerry Yu3e536442022-02-15 11:05:59 +0800902 p += 3;
903
Gilles Peskine449bd832023-01-11 14:50:10 +0100904 MBEDTLS_SSL_DEBUG_CRT(3, "own certificate", crt);
Jerry Yu5cc35062022-01-28 16:16:08 +0800905
Gilles Peskine449bd832023-01-11 14:50:10 +0100906 while (crt != NULL) {
Jerry Yu7399d0d2022-01-30 17:54:19 +0800907 size_t cert_data_len = crt->raw.len;
Jerry Yu5cc35062022-01-28 16:16:08 +0800908
Gilles Peskine449bd832023-01-11 14:50:10 +0100909 MBEDTLS_SSL_CHK_BUF_PTR(p, end, cert_data_len + 3 + 2);
910 MBEDTLS_PUT_UINT24_BE(cert_data_len, p, 0);
Jerry Yu7399d0d2022-01-30 17:54:19 +0800911 p += 3;
Jerry Yu5cc35062022-01-28 16:16:08 +0800912
Gilles Peskine449bd832023-01-11 14:50:10 +0100913 memcpy(p, crt->raw.p, cert_data_len);
Jerry Yu7399d0d2022-01-30 17:54:19 +0800914 p += cert_data_len;
915 crt = crt->next;
Jerry Yu5cc35062022-01-28 16:16:08 +0800916
917 /* Currently, we don't have any certificate extensions defined.
918 * Hence, we are sending an empty extension with length zero.
919 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100920 MBEDTLS_PUT_UINT16_BE(0, p, 0);
Jerry Yu7399d0d2022-01-30 17:54:19 +0800921 p += 2;
Jerry Yu5cc35062022-01-28 16:16:08 +0800922 }
Jerry Yu5cc35062022-01-28 16:16:08 +0800923
Gilles Peskine449bd832023-01-11 14:50:10 +0100924 MBEDTLS_PUT_UINT24_BE(p - p_certificate_list_len - 3,
925 p_certificate_list_len, 0);
Jerry Yu7399d0d2022-01-30 17:54:19 +0800926
Jerry Yu3e536442022-02-15 11:05:59 +0800927 *out_len = p - buf;
Jerry Yu5cc35062022-01-28 16:16:08 +0800928
Jerry Yu7de2ff02022-11-08 21:43:46 +0800929 MBEDTLS_SSL_PRINT_EXTS(
Gilles Peskine449bd832023-01-11 14:50:10 +0100930 3, MBEDTLS_SSL_HS_CERTIFICATE, ssl->handshake->sent_extensions);
Jerry Yu4b8f2f72022-10-31 13:31:22 +0800931
Gilles Peskine449bd832023-01-11 14:50:10 +0100932 return 0;
Jerry Yu5cc35062022-01-28 16:16:08 +0800933}
Jerry Yu5cc35062022-01-28 16:16:08 +0800934
Gilles Peskine449bd832023-01-11 14:50:10 +0100935int mbedtls_ssl_tls13_write_certificate(mbedtls_ssl_context *ssl)
Jerry Yu5cc35062022-01-28 16:16:08 +0800936{
937 int ret;
Ronald Cron5bb8fc82022-03-09 07:00:13 +0100938 unsigned char *buf;
939 size_t buf_len, msg_len;
940
Gilles Peskine449bd832023-01-11 14:50:10 +0100941 MBEDTLS_SSL_DEBUG_MSG(2, ("=> write certificate"));
Jerry Yu5cc35062022-01-28 16:16:08 +0800942
Xiaokang Qian73437382023-03-29 08:24:12 +0000943 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_start_handshake_msg(
944 ssl, MBEDTLS_SSL_HS_CERTIFICATE, &buf, &buf_len));
Jerry Yu5cc35062022-01-28 16:16:08 +0800945
Gilles Peskine449bd832023-01-11 14:50:10 +0100946 MBEDTLS_SSL_PROC_CHK(ssl_tls13_write_certificate_body(ssl,
947 buf,
948 buf + buf_len,
949 &msg_len));
Jerry Yu5cc35062022-01-28 16:16:08 +0800950
Xiaokang Qian73437382023-03-29 08:24:12 +0000951 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_add_hs_msg_to_checksum(
952 ssl, MBEDTLS_SSL_HS_CERTIFICATE, buf, msg_len));
Jerry Yu5cc35062022-01-28 16:16:08 +0800953
Gilles Peskine449bd832023-01-11 14:50:10 +0100954 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_finish_handshake_msg(
955 ssl, buf_len, msg_len));
Jerry Yu5cc35062022-01-28 16:16:08 +0800956cleanup:
957
Gilles Peskine449bd832023-01-11 14:50:10 +0100958 MBEDTLS_SSL_DEBUG_MSG(2, ("<= write certificate"));
959 return ret;
Jerry Yu5cc35062022-01-28 16:16:08 +0800960}
961
Jerry Yu3e536442022-02-15 11:05:59 +0800962/*
963 * STATE HANDLING: Output Certificate Verify
964 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100965int mbedtls_ssl_tls13_check_sig_alg_cert_key_match(uint16_t sig_alg,
966 mbedtls_pk_context *key)
Jerry Yu0c6be8f2022-06-20 20:42:00 +0800967{
Gilles Peskine449bd832023-01-11 14:50:10 +0100968 mbedtls_pk_type_t pk_type = mbedtls_ssl_sig_from_pk(key);
969 size_t key_size = mbedtls_pk_get_bitlen(key);
Jerry Yu0c6be8f2022-06-20 20:42:00 +0800970
Gilles Peskine449bd832023-01-11 14:50:10 +0100971 switch (pk_type) {
Jerry Yu67eced02022-02-25 13:37:36 +0800972 case MBEDTLS_SSL_SIG_ECDSA:
Gilles Peskine449bd832023-01-11 14:50:10 +0100973 switch (key_size) {
Jerry Yu67eced02022-02-25 13:37:36 +0800974 case 256:
Gilles Peskine449bd832023-01-11 14:50:10 +0100975 return
976 sig_alg == MBEDTLS_TLS1_3_SIG_ECDSA_SECP256R1_SHA256;
Jerry Yu0c6be8f2022-06-20 20:42:00 +0800977
Jerry Yu67eced02022-02-25 13:37:36 +0800978 case 384:
Gilles Peskine449bd832023-01-11 14:50:10 +0100979 return
980 sig_alg == MBEDTLS_TLS1_3_SIG_ECDSA_SECP384R1_SHA384;
Jerry Yu0c6be8f2022-06-20 20:42:00 +0800981
Jerry Yu67eced02022-02-25 13:37:36 +0800982 case 521:
Gilles Peskine449bd832023-01-11 14:50:10 +0100983 return
984 sig_alg == MBEDTLS_TLS1_3_SIG_ECDSA_SECP521R1_SHA512;
Jerry Yu67eced02022-02-25 13:37:36 +0800985 default:
Jerry Yu67eced02022-02-25 13:37:36 +0800986 break;
987 }
988 break;
Jerry Yu67eced02022-02-25 13:37:36 +0800989
Jerry Yu67eced02022-02-25 13:37:36 +0800990 case MBEDTLS_SSL_SIG_RSA:
Gilles Peskine449bd832023-01-11 14:50:10 +0100991 switch (sig_alg) {
Ronald Cron38391bf2022-09-16 11:19:27 +0200992 case MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA256: /* Intentional fallthrough */
993 case MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA384: /* Intentional fallthrough */
Jerry Yu0c6be8f2022-06-20 20:42:00 +0800994 case MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA512:
Gilles Peskine449bd832023-01-11 14:50:10 +0100995 return 1;
Jerry Yuc2e04932022-06-27 22:13:03 +0800996
Jerry Yu0c6be8f2022-06-20 20:42:00 +0800997 default:
998 break;
Jerry Yucef3f332022-03-22 23:00:13 +0800999 }
Jerry Yu67eced02022-02-25 13:37:36 +08001000 break;
Jerry Yu0c6be8f2022-06-20 20:42:00 +08001001
Jerry Yu67eced02022-02-25 13:37:36 +08001002 default:
Jerry Yu67eced02022-02-25 13:37:36 +08001003 break;
1004 }
Jerry Yu0c6be8f2022-06-20 20:42:00 +08001005
Gilles Peskine449bd832023-01-11 14:50:10 +01001006 return 0;
Jerry Yu0c6be8f2022-06-20 20:42:00 +08001007}
1008
Ronald Cronce7d76e2022-07-08 18:56:49 +02001009MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01001010static int ssl_tls13_write_certificate_verify_body(mbedtls_ssl_context *ssl,
1011 unsigned char *buf,
1012 unsigned char *end,
1013 size_t *out_len)
Jerry Yu8511f122022-01-29 10:01:04 +08001014{
Ronald Cron067a1e72022-09-16 13:44:49 +02001015 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yu3e536442022-02-15 11:05:59 +08001016 unsigned char *p = buf;
Jerry Yu8511f122022-01-29 10:01:04 +08001017 mbedtls_pk_context *own_key;
Jerry Yu3e536442022-02-15 11:05:59 +08001018
Gilles Peskine449bd832023-01-11 14:50:10 +01001019 unsigned char handshake_hash[MBEDTLS_TLS1_3_MD_MAX_SIZE];
Jerry Yu8511f122022-01-29 10:01:04 +08001020 size_t handshake_hash_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01001021 unsigned char verify_buffer[SSL_VERIFY_STRUCT_MAX_SIZE];
Jerry Yu3e536442022-02-15 11:05:59 +08001022 size_t verify_buffer_len;
Ronald Cron067a1e72022-09-16 13:44:49 +02001023
1024 uint16_t *sig_alg = ssl->handshake->received_sig_algs;
Jerry Yu3e536442022-02-15 11:05:59 +08001025 size_t signature_len = 0;
Jerry Yu8511f122022-01-29 10:01:04 +08001026
Jerry Yu0b7b1012022-02-23 12:23:05 +08001027 *out_len = 0;
1028
Gilles Peskine449bd832023-01-11 14:50:10 +01001029 own_key = mbedtls_ssl_own_key(ssl);
1030 if (own_key == NULL) {
1031 MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
1032 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Jerry Yu8511f122022-01-29 10:01:04 +08001033 }
1034
Xiaokang Qian73437382023-03-29 08:24:12 +00001035 ret = mbedtls_ssl_get_handshake_transcript(
1036 ssl, ssl->handshake->ciphersuite_info->mac,
1037 handshake_hash, sizeof(handshake_hash), &handshake_hash_len);
Gilles Peskine449bd832023-01-11 14:50:10 +01001038 if (ret != 0) {
1039 return ret;
1040 }
Jerry Yu8511f122022-01-29 10:01:04 +08001041
Gilles Peskine449bd832023-01-11 14:50:10 +01001042 MBEDTLS_SSL_DEBUG_BUF(3, "handshake hash",
1043 handshake_hash,
1044 handshake_hash_len);
Jerry Yu8511f122022-01-29 10:01:04 +08001045
Gilles Peskine449bd832023-01-11 14:50:10 +01001046 ssl_tls13_create_verify_structure(handshake_hash, handshake_hash_len,
1047 verify_buffer, &verify_buffer_len,
1048 ssl->conf->endpoint);
Jerry Yu8511f122022-01-29 10:01:04 +08001049
1050 /*
1051 * struct {
1052 * SignatureScheme algorithm;
1053 * opaque signature<0..2^16-1>;
1054 * } CertificateVerify;
1055 */
Ronald Cron067a1e72022-09-16 13:44:49 +02001056 /* Check there is space for the algorithm identifier (2 bytes) and the
1057 * signature length (2 bytes).
1058 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001059 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 4);
Ronald Cron067a1e72022-09-16 13:44:49 +02001060
Gilles Peskine449bd832023-01-11 14:50:10 +01001061 for (; *sig_alg != MBEDTLS_TLS1_3_SIG_NONE; sig_alg++) {
Ronald Cron067a1e72022-09-16 13:44:49 +02001062 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
1063 mbedtls_pk_type_t pk_type = MBEDTLS_PK_NONE;
1064 mbedtls_md_type_t md_alg = MBEDTLS_MD_NONE;
1065 psa_algorithm_t psa_algorithm = PSA_ALG_NONE;
1066 unsigned char verify_hash[PSA_HASH_MAX_SIZE];
1067 size_t verify_hash_len;
Jerry Yu67eced02022-02-25 13:37:36 +08001068
Gilles Peskine449bd832023-01-11 14:50:10 +01001069 if (!mbedtls_ssl_sig_alg_is_offered(ssl, *sig_alg)) {
Ronald Cron067a1e72022-09-16 13:44:49 +02001070 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +01001071 }
Jerry Yu67eced02022-02-25 13:37:36 +08001072
Gilles Peskine449bd832023-01-11 14:50:10 +01001073 if (!mbedtls_ssl_tls13_sig_alg_for_cert_verify_is_supported(*sig_alg)) {
Ronald Cron067a1e72022-09-16 13:44:49 +02001074 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +01001075 }
Ronald Cron067a1e72022-09-16 13:44:49 +02001076
Gilles Peskine449bd832023-01-11 14:50:10 +01001077 if (!mbedtls_ssl_tls13_check_sig_alg_cert_key_match(*sig_alg, own_key)) {
Ronald Cron067a1e72022-09-16 13:44:49 +02001078 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +01001079 }
Ronald Cron067a1e72022-09-16 13:44:49 +02001080
Gilles Peskine449bd832023-01-11 14:50:10 +01001081 if (mbedtls_ssl_get_pk_type_and_md_alg_from_sig_alg(
1082 *sig_alg, &pk_type, &md_alg) != 0) {
1083 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Ronald Cron067a1e72022-09-16 13:44:49 +02001084 }
1085
1086 /* Hash verify buffer with indicated hash function */
Manuel Pégourié-Gonnard2d6d9932023-03-28 11:38:08 +02001087 psa_algorithm = mbedtls_md_psa_alg_from_type(md_alg);
Gilles Peskine449bd832023-01-11 14:50:10 +01001088 status = psa_hash_compute(psa_algorithm,
1089 verify_buffer,
1090 verify_buffer_len,
1091 verify_hash, sizeof(verify_hash),
1092 &verify_hash_len);
1093 if (status != PSA_SUCCESS) {
Andrzej Kurek8a045ce2022-12-23 11:00:06 -05001094 return PSA_TO_MBEDTLS_ERR(status);
Ronald Cron067a1e72022-09-16 13:44:49 +02001095 }
1096
Gilles Peskine449bd832023-01-11 14:50:10 +01001097 MBEDTLS_SSL_DEBUG_BUF(3, "verify hash", verify_hash, verify_hash_len);
1098
1099 if ((ret = mbedtls_pk_sign_ext(pk_type, own_key,
1100 md_alg, verify_hash, verify_hash_len,
1101 p + 4, (size_t) (end - (p + 4)), &signature_len,
1102 ssl->conf->f_rng, ssl->conf->p_rng)) != 0) {
1103 MBEDTLS_SSL_DEBUG_MSG(2, ("CertificateVerify signature failed with %s",
1104 mbedtls_ssl_sig_alg_to_str(*sig_alg)));
1105 MBEDTLS_SSL_DEBUG_RET(2, "mbedtls_pk_sign_ext", ret);
1106
1107 /* The signature failed. This is possible if the private key
1108 * was not suitable for the signature operation as purposely we
1109 * did not check its suitability completely. Let's try with
1110 * another signature algorithm.
1111 */
1112 continue;
1113 }
1114
1115 MBEDTLS_SSL_DEBUG_MSG(2, ("CertificateVerify signature with %s",
1116 mbedtls_ssl_sig_alg_to_str(*sig_alg)));
Ronald Cron067a1e72022-09-16 13:44:49 +02001117
1118 break;
1119 }
1120
Gilles Peskine449bd832023-01-11 14:50:10 +01001121 if (*sig_alg == MBEDTLS_TLS1_3_SIG_NONE) {
1122 MBEDTLS_SSL_DEBUG_MSG(1, ("no suitable signature algorithm"));
1123 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE,
1124 MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE);
1125 return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
Jerry Yu8511f122022-01-29 10:01:04 +08001126 }
1127
Gilles Peskine449bd832023-01-11 14:50:10 +01001128 MBEDTLS_PUT_UINT16_BE(*sig_alg, p, 0);
1129 MBEDTLS_PUT_UINT16_BE(signature_len, p, 2);
Jerry Yuf3b46b52022-06-19 16:52:27 +08001130
Ronald Cron067a1e72022-09-16 13:44:49 +02001131 *out_len = 4 + signature_len;
Jerry Yu8c338862022-03-23 13:34:04 +08001132
Gilles Peskine449bd832023-01-11 14:50:10 +01001133 return 0;
Jerry Yu8511f122022-01-29 10:01:04 +08001134}
Jerry Yu8511f122022-01-29 10:01:04 +08001135
Gilles Peskine449bd832023-01-11 14:50:10 +01001136int mbedtls_ssl_tls13_write_certificate_verify(mbedtls_ssl_context *ssl)
Jerry Yu8511f122022-01-29 10:01:04 +08001137{
1138 int ret = 0;
Jerry Yuca133a32022-02-15 14:22:05 +08001139 unsigned char *buf;
1140 size_t buf_len, msg_len;
1141
Gilles Peskine449bd832023-01-11 14:50:10 +01001142 MBEDTLS_SSL_DEBUG_MSG(2, ("=> write certificate verify"));
Jerry Yu8511f122022-01-29 10:01:04 +08001143
Xiaokang Qian73437382023-03-29 08:24:12 +00001144 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_start_handshake_msg(
1145 ssl, MBEDTLS_SSL_HS_CERTIFICATE_VERIFY,
1146 &buf, &buf_len));
Jerry Yu8511f122022-01-29 10:01:04 +08001147
Gilles Peskine449bd832023-01-11 14:50:10 +01001148 MBEDTLS_SSL_PROC_CHK(ssl_tls13_write_certificate_verify_body(
1149 ssl, buf, buf + buf_len, &msg_len));
Jerry Yu8511f122022-01-29 10:01:04 +08001150
Xiaokang Qian73437382023-03-29 08:24:12 +00001151 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_add_hs_msg_to_checksum(
1152 ssl, MBEDTLS_SSL_HS_CERTIFICATE_VERIFY,
1153 buf, msg_len));
Jerry Yu8511f122022-01-29 10:01:04 +08001154
Gilles Peskine449bd832023-01-11 14:50:10 +01001155 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_finish_handshake_msg(
1156 ssl, buf_len, msg_len));
Jerry Yu8511f122022-01-29 10:01:04 +08001157
1158cleanup:
1159
Gilles Peskine449bd832023-01-11 14:50:10 +01001160 MBEDTLS_SSL_DEBUG_MSG(2, ("<= write certificate verify"));
1161 return ret;
Jerry Yu8511f122022-01-29 10:01:04 +08001162}
1163
Ronald Cron928cbd32022-10-04 16:14:26 +02001164#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED */
Jerry Yu90f152d2022-01-29 22:12:42 +08001165
Jerry Yu5cc35062022-01-28 16:16:08 +08001166/*
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001167 *
XiaokangQianc5c39d52021-11-09 11:55:10 +00001168 * STATE HANDLING: Incoming Finished message.
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001169 */
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001170/*
1171 * Implementation
1172 */
1173
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001174MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01001175static int ssl_tls13_preprocess_finished_message(mbedtls_ssl_context *ssl)
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001176{
1177 int ret;
1178
Xiaokang Qian73437382023-03-29 08:24:12 +00001179 ret = mbedtls_ssl_tls13_calculate_verify_data(
1180 ssl,
1181 ssl->handshake->state_local.finished_in.digest,
1182 sizeof(ssl->handshake->state_local.finished_in.digest),
1183 &ssl->handshake->state_local.finished_in.digest_len,
1184 ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT ?
1185 MBEDTLS_SSL_IS_SERVER : MBEDTLS_SSL_IS_CLIENT);
Gilles Peskine449bd832023-01-11 14:50:10 +01001186 if (ret != 0) {
1187 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_tls13_calculate_verify_data", ret);
1188 return ret;
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001189 }
1190
Gilles Peskine449bd832023-01-11 14:50:10 +01001191 return 0;
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001192}
1193
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001194MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01001195static int ssl_tls13_parse_finished_message(mbedtls_ssl_context *ssl,
1196 const unsigned char *buf,
1197 const unsigned char *end)
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001198{
XiaokangQian33062842021-11-11 03:37:45 +00001199 /*
1200 * struct {
XiaokangQianc13f9352021-11-11 06:13:22 +00001201 * opaque verify_data[Hash.length];
XiaokangQian33062842021-11-11 03:37:45 +00001202 * } Finished;
1203 */
1204 const unsigned char *expected_verify_data =
1205 ssl->handshake->state_local.finished_in.digest;
1206 size_t expected_verify_data_len =
1207 ssl->handshake->state_local.finished_in.digest_len;
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001208 /* Structural validation */
Gilles Peskine449bd832023-01-11 14:50:10 +01001209 if ((size_t) (end - buf) != expected_verify_data_len) {
1210 MBEDTLS_SSL_DEBUG_MSG(1, ("bad finished message"));
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001211
Gilles Peskine449bd832023-01-11 14:50:10 +01001212 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR,
1213 MBEDTLS_ERR_SSL_DECODE_ERROR);
1214 return MBEDTLS_ERR_SSL_DECODE_ERROR;
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001215 }
1216
Gilles Peskine449bd832023-01-11 14:50:10 +01001217 MBEDTLS_SSL_DEBUG_BUF(4, "verify_data (self-computed):",
1218 expected_verify_data,
1219 expected_verify_data_len);
1220 MBEDTLS_SSL_DEBUG_BUF(4, "verify_data (received message):", buf,
1221 expected_verify_data_len);
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001222
1223 /* Semantic validation */
Gilles Peskine449bd832023-01-11 14:50:10 +01001224 if (mbedtls_ct_memcmp(buf,
1225 expected_verify_data,
1226 expected_verify_data_len) != 0) {
1227 MBEDTLS_SSL_DEBUG_MSG(1, ("bad finished message"));
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001228
Gilles Peskine449bd832023-01-11 14:50:10 +01001229 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_DECRYPT_ERROR,
1230 MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE);
1231 return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001232 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001233 return 0;
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001234}
1235
Gilles Peskine449bd832023-01-11 14:50:10 +01001236int mbedtls_ssl_tls13_process_finished_message(mbedtls_ssl_context *ssl)
XiaokangQianc5c39d52021-11-09 11:55:10 +00001237{
XiaokangQian33062842021-11-11 03:37:45 +00001238 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
XiaokangQianc5c39d52021-11-09 11:55:10 +00001239 unsigned char *buf;
Xiaofei Baieef15042021-11-18 07:29:56 +00001240 size_t buf_len;
XiaokangQianc5c39d52021-11-09 11:55:10 +00001241
Gilles Peskine449bd832023-01-11 14:50:10 +01001242 MBEDTLS_SSL_DEBUG_MSG(2, ("=> parse finished message"));
XiaokangQianc5c39d52021-11-09 11:55:10 +00001243
Xiaokang Qian73437382023-03-29 08:24:12 +00001244 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_tls13_fetch_handshake_msg(
1245 ssl, MBEDTLS_SSL_HS_FINISHED, &buf, &buf_len));
Jerry Yu0a92d6c2022-05-16 16:54:46 +08001246
1247 /* Preprocessing step: Compute handshake digest */
Gilles Peskine449bd832023-01-11 14:50:10 +01001248 MBEDTLS_SSL_PROC_CHK(ssl_tls13_preprocess_finished_message(ssl));
Jerry Yu0a92d6c2022-05-16 16:54:46 +08001249
Xiaokang Qian73437382023-03-29 08:24:12 +00001250 MBEDTLS_SSL_PROC_CHK(ssl_tls13_parse_finished_message(
1251 ssl, buf, buf + buf_len));
Jerry Yu0a92d6c2022-05-16 16:54:46 +08001252
Xiaokang Qian73437382023-03-29 08:24:12 +00001253 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_add_hs_msg_to_checksum(
1254 ssl, MBEDTLS_SSL_HS_FINISHED, buf, buf_len));
XiaokangQianc5c39d52021-11-09 11:55:10 +00001255
1256cleanup:
1257
Gilles Peskine449bd832023-01-11 14:50:10 +01001258 MBEDTLS_SSL_DEBUG_MSG(2, ("<= parse finished message"));
1259 return ret;
XiaokangQianc5c39d52021-11-09 11:55:10 +00001260}
1261
XiaokangQian74af2a82021-09-22 07:40:30 +00001262/*
1263 *
XiaokangQiancc90c942021-11-09 12:30:09 +00001264 * STATE HANDLING: Write and send Finished message.
XiaokangQian74af2a82021-09-22 07:40:30 +00001265 *
1266 */
XiaokangQian74af2a82021-09-22 07:40:30 +00001267/*
XiaokangQian35dc6252021-11-11 08:16:19 +00001268 * Implement
XiaokangQian74af2a82021-09-22 07:40:30 +00001269 */
1270
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001271MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01001272static int ssl_tls13_prepare_finished_message(mbedtls_ssl_context *ssl)
XiaokangQian74af2a82021-09-22 07:40:30 +00001273{
1274 int ret;
1275
1276 /* Compute transcript of handshake up to now. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001277 ret = mbedtls_ssl_tls13_calculate_verify_data(ssl,
1278 ssl->handshake->state_local.finished_out.digest,
1279 sizeof(ssl->handshake->state_local.finished_out.
1280 digest),
1281 &ssl->handshake->state_local.finished_out.digest_len,
1282 ssl->conf->endpoint);
XiaokangQian74af2a82021-09-22 07:40:30 +00001283
Gilles Peskine449bd832023-01-11 14:50:10 +01001284 if (ret != 0) {
1285 MBEDTLS_SSL_DEBUG_RET(1, "calculate_verify_data failed", ret);
1286 return ret;
XiaokangQian74af2a82021-09-22 07:40:30 +00001287 }
1288
Gilles Peskine449bd832023-01-11 14:50:10 +01001289 return 0;
XiaokangQian74af2a82021-09-22 07:40:30 +00001290}
1291
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001292MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01001293static int ssl_tls13_write_finished_message_body(mbedtls_ssl_context *ssl,
1294 unsigned char *buf,
1295 unsigned char *end,
1296 size_t *out_len)
XiaokangQian74af2a82021-09-22 07:40:30 +00001297{
XiaokangQian8773aa02021-11-10 07:33:09 +00001298 size_t verify_data_len = ssl->handshake->state_local.finished_out.digest_len;
XiaokangQian0fa66432021-11-15 03:33:57 +00001299 /*
1300 * struct {
1301 * opaque verify_data[Hash.length];
1302 * } Finished;
1303 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001304 MBEDTLS_SSL_CHK_BUF_PTR(buf, end, verify_data_len);
XiaokangQian74af2a82021-09-22 07:40:30 +00001305
Gilles Peskine449bd832023-01-11 14:50:10 +01001306 memcpy(buf, ssl->handshake->state_local.finished_out.digest,
1307 verify_data_len);
XiaokangQian74af2a82021-09-22 07:40:30 +00001308
Xiaofei Baid25fab62021-12-02 06:36:27 +00001309 *out_len = verify_data_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01001310 return 0;
XiaokangQian74af2a82021-09-22 07:40:30 +00001311}
XiaokangQianc5c39d52021-11-09 11:55:10 +00001312
XiaokangQian35dc6252021-11-11 08:16:19 +00001313/* Main entry point: orchestrates the other functions */
Gilles Peskine449bd832023-01-11 14:50:10 +01001314int mbedtls_ssl_tls13_write_finished_message(mbedtls_ssl_context *ssl)
XiaokangQian35dc6252021-11-11 08:16:19 +00001315{
1316 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1317 unsigned char *buf;
1318 size_t buf_len, msg_len;
1319
Gilles Peskine449bd832023-01-11 14:50:10 +01001320 MBEDTLS_SSL_DEBUG_MSG(2, ("=> write finished message"));
XiaokangQian35dc6252021-11-11 08:16:19 +00001321
Gilles Peskine449bd832023-01-11 14:50:10 +01001322 MBEDTLS_SSL_PROC_CHK(ssl_tls13_prepare_finished_message(ssl));
XiaokangQiandce82242021-11-15 06:01:26 +00001323
Gilles Peskine449bd832023-01-11 14:50:10 +01001324 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_start_handshake_msg(ssl,
1325 MBEDTLS_SSL_HS_FINISHED, &buf, &buf_len));
XiaokangQian35dc6252021-11-11 08:16:19 +00001326
Gilles Peskine449bd832023-01-11 14:50:10 +01001327 MBEDTLS_SSL_PROC_CHK(ssl_tls13_write_finished_message_body(
1328 ssl, buf, buf + buf_len, &msg_len));
XiaokangQian35dc6252021-11-11 08:16:19 +00001329
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +01001330 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_add_hs_msg_to_checksum(ssl,
Manuel Pégourié-Gonnard43cc1272023-02-06 11:48:19 +01001331 MBEDTLS_SSL_HS_FINISHED, buf, msg_len));
XiaokangQian35dc6252021-11-11 08:16:19 +00001332
Gilles Peskine449bd832023-01-11 14:50:10 +01001333 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_finish_handshake_msg(
1334 ssl, buf_len, msg_len));
XiaokangQian35dc6252021-11-11 08:16:19 +00001335cleanup:
1336
Gilles Peskine449bd832023-01-11 14:50:10 +01001337 MBEDTLS_SSL_DEBUG_MSG(2, ("<= write finished message"));
1338 return ret;
XiaokangQian35dc6252021-11-11 08:16:19 +00001339}
1340
Gilles Peskine449bd832023-01-11 14:50:10 +01001341void mbedtls_ssl_tls13_handshake_wrapup(mbedtls_ssl_context *ssl)
Jerry Yu378254d2021-10-30 21:44:47 +08001342{
1343
Gilles Peskine449bd832023-01-11 14:50:10 +01001344 MBEDTLS_SSL_DEBUG_MSG(3, ("=> handshake wrapup"));
Jerry Yu378254d2021-10-30 21:44:47 +08001345
Gilles Peskine449bd832023-01-11 14:50:10 +01001346 MBEDTLS_SSL_DEBUG_MSG(1, ("Switch to application keys for inbound traffic"));
1347 mbedtls_ssl_set_inbound_transform(ssl, ssl->transform_application);
Jerry Yue8c1fca2022-05-18 14:48:56 +08001348
Gilles Peskine449bd832023-01-11 14:50:10 +01001349 MBEDTLS_SSL_DEBUG_MSG(1, ("Switch to application keys for outbound traffic"));
1350 mbedtls_ssl_set_outbound_transform(ssl, ssl->transform_application);
Jerry Yue8c1fca2022-05-18 14:48:56 +08001351
Jerry Yu378254d2021-10-30 21:44:47 +08001352 /*
Jerry Yucfe64f02021-11-15 13:54:06 +08001353 * Free the previous session and switch to the current one.
Jerry Yu378254d2021-10-30 21:44:47 +08001354 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001355 if (ssl->session) {
1356 mbedtls_ssl_session_free(ssl->session);
1357 mbedtls_free(ssl->session);
Jerry Yu378254d2021-10-30 21:44:47 +08001358 }
1359 ssl->session = ssl->session_negotiate;
1360 ssl->session_negotiate = NULL;
1361
Gilles Peskine449bd832023-01-11 14:50:10 +01001362 MBEDTLS_SSL_DEBUG_MSG(3, ("<= handshake wrapup"));
Jerry Yu378254d2021-10-30 21:44:47 +08001363}
1364
Ronald Cron49ad6192021-11-24 16:25:31 +01001365/*
1366 *
1367 * STATE HANDLING: Write ChangeCipherSpec
1368 *
1369 */
1370#if defined(MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001371MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01001372static int ssl_tls13_write_change_cipher_spec_body(mbedtls_ssl_context *ssl,
1373 unsigned char *buf,
1374 unsigned char *end,
1375 size_t *olen)
Ronald Cron49ad6192021-11-24 16:25:31 +01001376{
1377 ((void) ssl);
1378
Gilles Peskine449bd832023-01-11 14:50:10 +01001379 MBEDTLS_SSL_CHK_BUF_PTR(buf, end, 1);
Ronald Cron49ad6192021-11-24 16:25:31 +01001380 buf[0] = 1;
1381 *olen = 1;
1382
Gilles Peskine449bd832023-01-11 14:50:10 +01001383 return 0;
Ronald Cron49ad6192021-11-24 16:25:31 +01001384}
1385
Gilles Peskine449bd832023-01-11 14:50:10 +01001386int mbedtls_ssl_tls13_write_change_cipher_spec(mbedtls_ssl_context *ssl)
Ronald Cron49ad6192021-11-24 16:25:31 +01001387{
1388 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1389
Gilles Peskine449bd832023-01-11 14:50:10 +01001390 MBEDTLS_SSL_DEBUG_MSG(2, ("=> write change cipher spec"));
Ronald Cron49ad6192021-11-24 16:25:31 +01001391
Ronald Cron49ad6192021-11-24 16:25:31 +01001392 /* Write CCS message */
Gilles Peskine449bd832023-01-11 14:50:10 +01001393 MBEDTLS_SSL_PROC_CHK(ssl_tls13_write_change_cipher_spec_body(
1394 ssl, ssl->out_msg,
1395 ssl->out_msg + MBEDTLS_SSL_OUT_CONTENT_LEN,
1396 &ssl->out_msglen));
Ronald Cron49ad6192021-11-24 16:25:31 +01001397
1398 ssl->out_msgtype = MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC;
1399
Ronald Cron49ad6192021-11-24 16:25:31 +01001400 /* Dispatch message */
Gilles Peskine449bd832023-01-11 14:50:10 +01001401 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_write_record(ssl, 0));
Ronald Cron49ad6192021-11-24 16:25:31 +01001402
1403cleanup:
1404
Gilles Peskine449bd832023-01-11 14:50:10 +01001405 MBEDTLS_SSL_DEBUG_MSG(2, ("<= write change cipher spec"));
1406 return ret;
Ronald Cron49ad6192021-11-24 16:25:31 +01001407}
1408
1409#endif /* MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE */
1410
Xiaokang Qianecc29482022-11-02 07:52:47 +00001411/* Early Data Indication Extension
Xiaokang Qian0e97d4d2022-10-24 11:12:51 +00001412 *
Xiaokang Qian0e97d4d2022-10-24 11:12:51 +00001413 * struct {
1414 * select ( Handshake.msg_type ) {
Xiaokang Qianecc29482022-11-02 07:52:47 +00001415 * ...
Xiaokang Qian0e97d4d2022-10-24 11:12:51 +00001416 * case client_hello: Empty;
1417 * case encrypted_extensions: Empty;
1418 * };
1419 * } EarlyDataIndication;
1420 */
1421#if defined(MBEDTLS_SSL_EARLY_DATA)
Gilles Peskine449bd832023-01-11 14:50:10 +01001422int mbedtls_ssl_tls13_write_early_data_ext(mbedtls_ssl_context *ssl,
1423 unsigned char *buf,
1424 const unsigned char *end,
1425 size_t *out_len)
Xiaokang Qian0e97d4d2022-10-24 11:12:51 +00001426{
1427 unsigned char *p = buf;
1428 *out_len = 0;
1429 ((void) ssl);
1430
Gilles Peskine449bd832023-01-11 14:50:10 +01001431 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 4);
Xiaokang Qian0e97d4d2022-10-24 11:12:51 +00001432
Gilles Peskine449bd832023-01-11 14:50:10 +01001433 MBEDTLS_PUT_UINT16_BE(MBEDTLS_TLS_EXT_EARLY_DATA, p, 0);
1434 MBEDTLS_PUT_UINT16_BE(0, p, 2);
Xiaokang Qian0e97d4d2022-10-24 11:12:51 +00001435
1436 *out_len = 4;
Xiaokang Qian2cd5ce02022-11-15 10:33:53 +00001437
Gilles Peskine449bd832023-01-11 14:50:10 +01001438 mbedtls_ssl_tls13_set_hs_sent_ext_mask(ssl, MBEDTLS_TLS_EXT_EARLY_DATA);
Xiaokang Qian2cd5ce02022-11-15 10:33:53 +00001439
Gilles Peskine449bd832023-01-11 14:50:10 +01001440 return 0;
Xiaokang Qian0e97d4d2022-10-24 11:12:51 +00001441}
1442#endif /* MBEDTLS_SSL_EARLY_DATA */
1443
XiaokangQian78b1fa72022-01-19 06:56:30 +00001444/* Reset SSL context and update hash for handling HRR.
1445 *
1446 * Replace Transcript-Hash(X) by
1447 * Transcript-Hash( message_hash ||
1448 * 00 00 Hash.length ||
1449 * X )
1450 * A few states of the handshake are preserved, including:
1451 * - session ID
1452 * - session ticket
1453 * - negotiated ciphersuite
1454 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001455int mbedtls_ssl_reset_transcript_for_hrr(mbedtls_ssl_context *ssl)
XiaokangQian78b1fa72022-01-19 06:56:30 +00001456{
1457 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Przemyslaw Stekielda645252022-09-14 12:50:51 +02001458 unsigned char hash_transcript[PSA_HASH_MAX_SIZE + 4];
XiaokangQian0ece9982022-01-24 08:56:23 +00001459 size_t hash_len;
Xiaokang Qian6b980012023-02-07 03:17:45 +00001460 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
1461 ssl->handshake->ciphersuite_info;
XiaokangQian78b1fa72022-01-19 06:56:30 +00001462
Gilles Peskine449bd832023-01-11 14:50:10 +01001463 MBEDTLS_SSL_DEBUG_MSG(3, ("Reset SSL session for HRR"));
XiaokangQian78b1fa72022-01-19 06:56:30 +00001464
Gilles Peskine449bd832023-01-11 14:50:10 +01001465 ret = mbedtls_ssl_get_handshake_transcript(ssl, ciphersuite_info->mac,
1466 hash_transcript + 4,
1467 PSA_HASH_MAX_SIZE,
1468 &hash_len);
1469 if (ret != 0) {
Manuel Pégourié-Gonnardda7979b2023-02-21 09:31:10 +01001470 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_get_handshake_transcript", ret);
Gilles Peskine449bd832023-01-11 14:50:10 +01001471 return ret;
XiaokangQian0ece9982022-01-24 08:56:23 +00001472 }
1473
1474 hash_transcript[0] = MBEDTLS_SSL_HS_MESSAGE_HASH;
1475 hash_transcript[1] = 0;
1476 hash_transcript[2] = 0;
1477 hash_transcript[3] = (unsigned char) hash_len;
1478
1479 hash_len += 4;
1480
Manuel Pégourié-Gonnardda7979b2023-02-21 09:31:10 +01001481 MBEDTLS_SSL_DEBUG_BUF(4, "Truncated handshake transcript",
1482 hash_transcript, hash_len);
1483
Manuel Pégourié-Gonnardd7a7a232023-02-05 10:26:49 +01001484 /* Reset running hash and replace it with a hash of the transcript */
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +01001485 ret = mbedtls_ssl_reset_checksum(ssl);
1486 if (ret != 0) {
1487 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_reset_checksum", ret);
1488 return ret;
1489 }
1490 ret = ssl->handshake->update_checksum(ssl, hash_transcript, hash_len);
1491 if (ret != 0) {
1492 MBEDTLS_SSL_DEBUG_RET(1, "update_checksum", ret);
1493 return ret;
1494 }
Przemyslaw Stekiel4b3fff42022-02-14 16:39:52 +01001495
Gilles Peskine449bd832023-01-11 14:50:10 +01001496 return ret;
XiaokangQian78b1fa72022-01-19 06:56:30 +00001497}
1498
Valerio Settic9ae8622023-07-25 11:23:50 +02001499#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_EPHEMERAL_ENABLED)
XiaokangQian7807f9f2022-02-15 10:04:37 +00001500
Przemek Stekiel7ac93be2023-07-04 10:02:38 +02001501int mbedtls_ssl_tls13_read_public_xxdhe_share(mbedtls_ssl_context *ssl,
Gilles Peskine449bd832023-01-11 14:50:10 +01001502 const unsigned char *buf,
1503 size_t buf_len)
XiaokangQian7807f9f2022-02-15 10:04:37 +00001504{
Gilles Peskine449bd832023-01-11 14:50:10 +01001505 uint8_t *p = (uint8_t *) buf;
XiaokangQiancfd925f2022-04-14 07:10:37 +00001506 const uint8_t *end = buf + buf_len;
XiaokangQian9b5d04b2022-04-10 10:20:43 +00001507 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001508
XiaokangQian9b5d04b2022-04-10 10:20:43 +00001509 /* Get size of the TLS opaque key_exchange field of the KeyShareEntry struct. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001510 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, 2);
1511 uint16_t peerkey_len = MBEDTLS_GET_UINT16_BE(p, 0);
XiaokangQian9b5d04b2022-04-10 10:20:43 +00001512 p += 2;
XiaokangQian3207a322022-02-23 03:15:27 +00001513
XiaokangQian9b5d04b2022-04-10 10:20:43 +00001514 /* Check if key size is consistent with given buffer length. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001515 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, peerkey_len);
XiaokangQian9b5d04b2022-04-10 10:20:43 +00001516
1517 /* Store peer's ECDH public key. */
Przemek Stekiel7ac93be2023-07-04 10:02:38 +02001518 memcpy(handshake->xxdh_psa_peerkey, p, peerkey_len);
1519 handshake->xxdh_psa_peerkey_len = peerkey_len;
XiaokangQian9b5d04b2022-04-10 10:20:43 +00001520
Gilles Peskine449bd832023-01-11 14:50:10 +01001521 return 0;
XiaokangQian3207a322022-02-23 03:15:27 +00001522}
Jerry Yu89e103c2022-03-30 22:43:29 +08001523
Przemek Stekielda4fba62023-06-02 14:52:28 +02001524static psa_status_t mbedtls_ssl_get_psa_ffdh_info_from_tls_id(
Przemek Stekiel75a5a9c2023-06-12 11:21:18 +02001525 uint16_t tls_id, size_t *bits, psa_key_type_t *key_type)
Przemek Stekiel29c219c2023-05-31 15:21:04 +02001526{
Przemek Stekiel75a5a9c2023-06-12 11:21:18 +02001527 switch (tls_id) {
Przemek Stekiel29c219c2023-05-31 15:21:04 +02001528 case MBEDTLS_SSL_IANA_TLS_GROUP_FFDHE2048:
1529 *bits = 2048;
1530 *key_type = PSA_KEY_TYPE_DH_KEY_PAIR(PSA_DH_FAMILY_RFC7919);
1531 return PSA_SUCCESS;
1532 case MBEDTLS_SSL_IANA_TLS_GROUP_FFDHE3072:
1533 *bits = 3072;
1534 *key_type = PSA_KEY_TYPE_DH_KEY_PAIR(PSA_DH_FAMILY_RFC7919);
1535 return PSA_SUCCESS;
1536 case MBEDTLS_SSL_IANA_TLS_GROUP_FFDHE4096:
1537 *bits = 4096;
1538 *key_type = PSA_KEY_TYPE_DH_KEY_PAIR(PSA_DH_FAMILY_RFC7919);
1539 return PSA_SUCCESS;
1540 case MBEDTLS_SSL_IANA_TLS_GROUP_FFDHE6144:
1541 *bits = 6144;
1542 *key_type = PSA_KEY_TYPE_DH_KEY_PAIR(PSA_DH_FAMILY_RFC7919);
1543 return PSA_SUCCESS;
1544 case MBEDTLS_SSL_IANA_TLS_GROUP_FFDHE8192:
1545 *bits = 8192;
1546 *key_type = PSA_KEY_TYPE_DH_KEY_PAIR(PSA_DH_FAMILY_RFC7919);
1547 return PSA_SUCCESS;
1548 default:
1549 return PSA_ERROR_NOT_SUPPORTED;
1550 }
1551}
1552
Przemek Stekiel408569f2023-07-06 11:26:44 +02001553int mbedtls_ssl_tls13_generate_and_write_xxdh_key_exchange(
Gilles Peskine449bd832023-01-11 14:50:10 +01001554 mbedtls_ssl_context *ssl,
1555 uint16_t named_group,
1556 unsigned char *buf,
1557 unsigned char *end,
1558 size_t *out_len)
Jerry Yu89e103c2022-03-30 22:43:29 +08001559{
1560 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
1561 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
1562 psa_key_attributes_t key_attributes;
1563 size_t own_pubkey_len;
1564 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
Przemek Stekiel29c219c2023-05-31 15:21:04 +02001565 size_t bits = 0;
Przemek Stekiel75a5a9c2023-06-12 11:21:18 +02001566 psa_key_type_t key_type = PSA_KEY_TYPE_NONE;
1567 psa_algorithm_t alg = PSA_ALG_NONE;
Przemek Stekiel29c219c2023-05-31 15:21:04 +02001568 size_t buf_size = (size_t) (end - buf);
Jerry Yu89e103c2022-03-30 22:43:29 +08001569
Przemek Stekiel29c219c2023-05-31 15:21:04 +02001570 MBEDTLS_SSL_DEBUG_MSG(1, ("Perform PSA-based ECDH/FFDH computation."));
Jerry Yu89e103c2022-03-30 22:43:29 +08001571
Valerio Setti40d9ca92023-01-04 16:08:04 +01001572 /* Convert EC's TLS ID to PSA key type. */
Przemek Stekiel29c219c2023-05-31 15:21:04 +02001573#if defined(PSA_WANT_ALG_ECDH)
Xiaokang Qian73437382023-03-29 08:24:12 +00001574 if (mbedtls_ssl_get_psa_curve_info_from_tls_id(
Przemek Stekielda4fba62023-06-02 14:52:28 +02001575 named_group, &key_type, &bits) == PSA_SUCCESS) {
1576 alg = PSA_ALG_ECDH;
Przemek Stekiel29c219c2023-05-31 15:21:04 +02001577 }
1578#endif
1579#if defined(PSA_WANT_ALG_FFDH)
Przemek Stekiel75a5a9c2023-06-12 11:21:18 +02001580 if (mbedtls_ssl_get_psa_ffdh_info_from_tls_id(named_group, &bits,
1581 &key_type) == PSA_SUCCESS) {
Przemek Stekielda4fba62023-06-02 14:52:28 +02001582 alg = PSA_ALG_FFDH;
Przemek Stekiel29c219c2023-05-31 15:21:04 +02001583 }
1584#endif
1585
Przemek Stekiel75a5a9c2023-06-12 11:21:18 +02001586 if (key_type == PSA_KEY_TYPE_NONE) {
Gilles Peskine449bd832023-01-11 14:50:10 +01001587 return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
Valerio Setti40d9ca92023-01-04 16:08:04 +01001588 }
Przemek Stekiel29c219c2023-05-31 15:21:04 +02001589
Przemek Stekielda4fba62023-06-02 14:52:28 +02001590 if (buf_size < PSA_BITS_TO_BYTES(bits)) {
Przemek Stekielda4fba62023-06-02 14:52:28 +02001591 return MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL;
1592 }
1593
Przemek Stekiel7ac93be2023-07-04 10:02:38 +02001594 handshake->xxdh_psa_type = key_type;
Valerio Settiea59c432023-07-25 11:14:03 +02001595 ssl->handshake->xxdh_psa_bits = bits;
Jerry Yu89e103c2022-03-30 22:43:29 +08001596
1597 key_attributes = psa_key_attributes_init();
Gilles Peskine449bd832023-01-11 14:50:10 +01001598 psa_set_key_usage_flags(&key_attributes, PSA_KEY_USAGE_DERIVE);
Przemek Stekielda4fba62023-06-02 14:52:28 +02001599 psa_set_key_algorithm(&key_attributes, alg);
Przemek Stekiel7ac93be2023-07-04 10:02:38 +02001600 psa_set_key_type(&key_attributes, handshake->xxdh_psa_type);
Valerio Settiea59c432023-07-25 11:14:03 +02001601 psa_set_key_bits(&key_attributes, handshake->xxdh_psa_bits);
Jerry Yu89e103c2022-03-30 22:43:29 +08001602
Przemek Stekiel29c219c2023-05-31 15:21:04 +02001603 /* Generate ECDH/FFDH private key. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001604 status = psa_generate_key(&key_attributes,
Przemek Stekiel7ac93be2023-07-04 10:02:38 +02001605 &handshake->xxdh_psa_privkey);
Gilles Peskine449bd832023-01-11 14:50:10 +01001606 if (status != PSA_SUCCESS) {
Andrzej Kurek8a045ce2022-12-23 11:00:06 -05001607 ret = PSA_TO_MBEDTLS_ERR(status);
Gilles Peskine449bd832023-01-11 14:50:10 +01001608 MBEDTLS_SSL_DEBUG_RET(1, "psa_generate_key", ret);
1609 return ret;
Jerry Yu89e103c2022-03-30 22:43:29 +08001610
1611 }
1612
Przemek Stekiel29c219c2023-05-31 15:21:04 +02001613 /* Export the public part of the ECDH/FFDH private key from PSA. */
Przemek Stekiel7ac93be2023-07-04 10:02:38 +02001614 status = psa_export_public_key(handshake->xxdh_psa_privkey,
Przemek Stekiel29c219c2023-05-31 15:21:04 +02001615 buf, buf_size,
Gilles Peskine449bd832023-01-11 14:50:10 +01001616 &own_pubkey_len);
Przemek Stekiel29c219c2023-05-31 15:21:04 +02001617
Gilles Peskine449bd832023-01-11 14:50:10 +01001618 if (status != PSA_SUCCESS) {
Andrzej Kurek8a045ce2022-12-23 11:00:06 -05001619 ret = PSA_TO_MBEDTLS_ERR(status);
Gilles Peskine449bd832023-01-11 14:50:10 +01001620 MBEDTLS_SSL_DEBUG_RET(1, "psa_export_public_key", ret);
1621 return ret;
Jerry Yu89e103c2022-03-30 22:43:29 +08001622 }
1623
1624 *out_len = own_pubkey_len;
1625
Gilles Peskine449bd832023-01-11 14:50:10 +01001626 return 0;
Jerry Yu89e103c2022-03-30 22:43:29 +08001627}
Valerio Settic9ae8622023-07-25 11:23:50 +02001628#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_EPHEMERAL_ENABLED */
XiaokangQian7807f9f2022-02-15 10:04:37 +00001629
Jerry Yu0c354a22022-08-29 15:25:36 +08001630/* RFC 8446 section 4.2
1631 *
1632 * If an implementation receives an extension which it recognizes and which is
1633 * not specified for the message in which it appears, it MUST abort the handshake
1634 * with an "illegal_parameter" alert.
1635 *
1636 */
Jerry Yuc4bf5d62022-10-29 09:08:47 +08001637int mbedtls_ssl_tls13_check_received_extension(
Gilles Peskine449bd832023-01-11 14:50:10 +01001638 mbedtls_ssl_context *ssl,
1639 int hs_msg_type,
1640 unsigned int received_extension_type,
1641 uint32_t hs_msg_allowed_extensions_mask)
Jerry Yu0c354a22022-08-29 15:25:36 +08001642{
Jerry Yudf0ad652022-10-31 13:20:57 +08001643 uint32_t extension_mask = mbedtls_ssl_get_extension_mask(
Gilles Peskine449bd832023-01-11 14:50:10 +01001644 received_extension_type);
Jerry Yu0c354a22022-08-29 15:25:36 +08001645
Jerry Yu79aa7212022-11-08 21:30:21 +08001646 MBEDTLS_SSL_PRINT_EXT(
Gilles Peskine449bd832023-01-11 14:50:10 +01001647 3, hs_msg_type, received_extension_type, "received");
Jerry Yu0c354a22022-08-29 15:25:36 +08001648
Gilles Peskine449bd832023-01-11 14:50:10 +01001649 if ((extension_mask & hs_msg_allowed_extensions_mask) == 0) {
Jerry Yu79aa7212022-11-08 21:30:21 +08001650 MBEDTLS_SSL_PRINT_EXT(
Gilles Peskine449bd832023-01-11 14:50:10 +01001651 3, hs_msg_type, received_extension_type, "is illegal");
Jerry Yu0c354a22022-08-29 15:25:36 +08001652 MBEDTLS_SSL_PEND_FATAL_ALERT(
1653 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER,
Gilles Peskine449bd832023-01-11 14:50:10 +01001654 MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER);
1655 return MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER;
Jerry Yu0c354a22022-08-29 15:25:36 +08001656 }
1657
1658 ssl->handshake->received_extensions |= extension_mask;
Jerry Yuc4bf5d62022-10-29 09:08:47 +08001659 /*
1660 * If it is a message containing extension responses, check that we
1661 * previously sent the extension.
1662 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001663 switch (hs_msg_type) {
Jerry Yu0c354a22022-08-29 15:25:36 +08001664 case MBEDTLS_SSL_HS_SERVER_HELLO:
Jerry Yudf0ad652022-10-31 13:20:57 +08001665 case MBEDTLS_SSL_TLS1_3_HS_HELLO_RETRY_REQUEST:
Jerry Yu0c354a22022-08-29 15:25:36 +08001666 case MBEDTLS_SSL_HS_ENCRYPTED_EXTENSIONS:
1667 case MBEDTLS_SSL_HS_CERTIFICATE:
Jerry Yuc4bf5d62022-10-29 09:08:47 +08001668 /* Check if the received extension is sent by peer message.*/
Gilles Peskine449bd832023-01-11 14:50:10 +01001669 if ((ssl->handshake->sent_extensions & extension_mask) != 0) {
1670 return 0;
1671 }
Jerry Yu0c354a22022-08-29 15:25:36 +08001672 break;
1673 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01001674 return 0;
Jerry Yu0c354a22022-08-29 15:25:36 +08001675 }
1676
Jerry Yu79aa7212022-11-08 21:30:21 +08001677 MBEDTLS_SSL_PRINT_EXT(
Gilles Peskine449bd832023-01-11 14:50:10 +01001678 3, hs_msg_type, received_extension_type, "is unsupported");
Jerry Yu0c354a22022-08-29 15:25:36 +08001679 MBEDTLS_SSL_PEND_FATAL_ALERT(
1680 MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_EXT,
Gilles Peskine449bd832023-01-11 14:50:10 +01001681 MBEDTLS_ERR_SSL_UNSUPPORTED_EXTENSION);
1682 return MBEDTLS_ERR_SSL_UNSUPPORTED_EXTENSION;
Jerry Yu0c354a22022-08-29 15:25:36 +08001683}
1684
Jan Bruckner151f6422023-02-10 12:45:19 +01001685#if defined(MBEDTLS_SSL_RECORD_SIZE_LIMIT)
Jan Bruckner1a38e542023-03-15 14:15:11 +01001686/* RFC 8449, section 4:
1687 *
Jan Bruckner151f6422023-02-10 12:45:19 +01001688 * The ExtensionData of the "record_size_limit" extension is
1689 * RecordSizeLimit:
1690 * uint16 RecordSizeLimit;
1691 */
1692MBEDTLS_CHECK_RETURN_CRITICAL
1693int mbedtls_ssl_tls13_parse_record_size_limit_ext(mbedtls_ssl_context *ssl,
1694 const unsigned char *buf,
1695 const unsigned char *end)
1696{
Jan Bruckner1a38e542023-03-15 14:15:11 +01001697 const unsigned char *p = buf;
1698 uint16_t record_size_limit;
Jan Brucknera0589e72023-03-15 11:04:45 +01001699 const size_t extension_data_len = end - buf;
Jan Bruckner1a38e542023-03-15 14:15:11 +01001700
Xiaokang Qian73437382023-03-29 08:24:12 +00001701 if (extension_data_len !=
1702 MBEDTLS_SSL_RECORD_SIZE_LIMIT_EXTENSION_DATA_LENGTH) {
Jan Bruckner151f6422023-02-10 12:45:19 +01001703 MBEDTLS_SSL_DEBUG_MSG(2,
Jan Bruckner1a38e542023-03-15 14:15:11 +01001704 ("record_size_limit extension has invalid length: %"
1705 MBEDTLS_PRINTF_SIZET " Bytes",
Jan Bruckner151f6422023-02-10 12:45:19 +01001706 extension_data_len));
1707
1708 MBEDTLS_SSL_PEND_FATAL_ALERT(
1709 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER,
1710 MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER);
1711 return MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER;
1712 }
1713
Jan Bruckner151f6422023-02-10 12:45:19 +01001714 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, 2);
1715 record_size_limit = MBEDTLS_GET_UINT16_BE(p, 0);
1716
1717 MBEDTLS_SSL_DEBUG_MSG(2, ("RecordSizeLimit: %u Bytes", record_size_limit));
1718
Jan Bruckner1a38e542023-03-15 14:15:11 +01001719 /* RFC 8449, section 4
Jan Bruckner151f6422023-02-10 12:45:19 +01001720 *
1721 * Endpoints MUST NOT send a "record_size_limit" extension with a value
1722 * smaller than 64. An endpoint MUST treat receipt of a smaller value
1723 * as a fatal error and generate an "illegal_parameter" alert.
1724 */
Jan Brucknera0589e72023-03-15 11:04:45 +01001725 if (record_size_limit < MBEDTLS_SSL_RECORD_SIZE_LIMIT_MIN) {
Jan Bruckner151f6422023-02-10 12:45:19 +01001726 MBEDTLS_SSL_PEND_FATAL_ALERT(
1727 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER,
1728 MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER);
1729 return MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER;
1730 }
1731
Xiaokang Qian73437382023-03-29 08:24:12 +00001732 MBEDTLS_SSL_DEBUG_MSG(
1733 2, ("record_size_limit extension is still in development. Aborting handshake."));
Jan Bruckner151f6422023-02-10 12:45:19 +01001734
1735 MBEDTLS_SSL_PEND_FATAL_ALERT(
1736 MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_EXT,
1737 MBEDTLS_ERR_SSL_UNSUPPORTED_EXTENSION);
1738 return MBEDTLS_ERR_SSL_UNSUPPORTED_EXTENSION;
1739}
1740#endif /* MBEDTLS_SSL_RECORD_SIZE_LIMIT */
1741
Jerry Yufb4b6472022-01-27 15:03:26 +08001742#endif /* MBEDTLS_SSL_TLS_C && MBEDTLS_SSL_PROTO_TLS1_3 */