blob: 1b58dceb207ff1bcfe56bf60923d3cb0a7844ef4 [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
Andrzej Kureka6033ac2023-05-30 15:16:34 -040042#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED) || \
Valerio Settidbd01cb2023-07-04 09:11:39 +020043 defined(PSA_WANT_ALG_ECDH) || defined(PSA_WANT_ALG_FFDH)
Andrzej Kurek00644842023-05-30 05:45:00 -040044/* Define a local translating function to save code size by not using too many
45 * arguments in each translating place. */
46static int local_err_translation(psa_status_t status)
47{
48 return psa_status_to_mbedtls(status, psa_to_ssl_errors,
Andrzej Kurek1e4a0302023-05-30 09:45:17 -040049 ARRAY_LENGTH(psa_to_ssl_errors),
Andrzej Kurek00644842023-05-30 05:45:00 -040050 psa_generic_status_to_mbedtls);
51}
52#define PSA_TO_MBEDTLS_ERR(status) local_err_translation(status)
Andrzej Kureka6033ac2023-05-30 15:16:34 -040053#endif
Andrzej Kurek8a045ce2022-12-23 11:00:06 -050054
Jerry Yufbe3e642022-04-25 19:31:51 +080055const uint8_t mbedtls_ssl_tls13_hello_retry_request_magic[
Gilles Peskine449bd832023-01-11 14:50:10 +010056 MBEDTLS_SERVER_HELLO_RANDOM_LEN] =
57{ 0xCF, 0x21, 0xAD, 0x74, 0xE5, 0x9A, 0x61, 0x11,
58 0xBE, 0x1D, 0x8C, 0x02, 0x1E, 0x65, 0xB8, 0x91,
59 0xC2, 0xA2, 0x11, 0x16, 0x7A, 0xBB, 0x8C, 0x5E,
60 0x07, 0x9E, 0x09, 0xE2, 0xC8, 0xA8, 0x33, 0x9C };
Jerry Yu93a13f22022-04-11 23:00:01 +080061
Gilles Peskine449bd832023-01-11 14:50:10 +010062int mbedtls_ssl_tls13_fetch_handshake_msg(mbedtls_ssl_context *ssl,
63 unsigned hs_type,
64 unsigned char **buf,
65 size_t *buf_len)
XiaokangQian6b226b02021-09-24 07:51:16 +000066{
67 int ret;
68
Gilles Peskine449bd832023-01-11 14:50:10 +010069 if ((ret = mbedtls_ssl_read_record(ssl, 0)) != 0) {
70 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_read_record", ret);
XiaokangQian6b226b02021-09-24 07:51:16 +000071 goto cleanup;
72 }
73
Gilles Peskine449bd832023-01-11 14:50:10 +010074 if (ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE ||
75 ssl->in_msg[0] != hs_type) {
76 MBEDTLS_SSL_DEBUG_MSG(1, ("Receive unexpected handshake message."));
77 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE,
78 MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE);
XiaokangQian6b226b02021-09-24 07:51:16 +000079 ret = MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE;
80 goto cleanup;
81 }
82
XiaokangQian05420b12021-09-29 08:46:37 +000083 /*
84 * Jump handshake header (4 bytes, see Section 4 of RFC 8446).
85 * ...
86 * HandshakeType msg_type;
87 * uint24 length;
88 * ...
89 */
Xiaofei Baieef15042021-11-18 07:29:56 +000090 *buf = ssl->in_msg + 4;
91 *buf_len = ssl->in_hslen - 4;
XiaokangQian6b226b02021-09-24 07:51:16 +000092
XiaokangQian6b226b02021-09-24 07:51:16 +000093cleanup:
94
Gilles Peskine449bd832023-01-11 14:50:10 +010095 return ret;
XiaokangQian6b226b02021-09-24 07:51:16 +000096}
97
Ronald Cron47dce632023-02-08 17:38:29 +010098int mbedtls_ssl_tls13_is_supported_versions_ext_present_in_exts(
99 mbedtls_ssl_context *ssl,
100 const unsigned char *buf, const unsigned char *end,
Ronald Croneff56732023-04-03 17:36:31 +0200101 const unsigned char **supported_versions_data,
102 const unsigned char **supported_versions_data_end)
Ronald Cron47dce632023-02-08 17:38:29 +0100103{
104 const unsigned char *p = buf;
105 size_t extensions_len;
106 const unsigned char *extensions_end;
107
Ronald Croneff56732023-04-03 17:36:31 +0200108 *supported_versions_data = NULL;
109 *supported_versions_data_end = NULL;
Ronald Cron47dce632023-02-08 17:38:29 +0100110
111 /* Case of no extension */
112 if (p == end) {
113 return 0;
114 }
115
116 /* ...
117 * Extension extensions<x..2^16-1>;
118 * ...
119 * struct {
120 * ExtensionType extension_type; (2 bytes)
121 * opaque extension_data<0..2^16-1>;
122 * } Extension;
123 */
124 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, 2);
125 extensions_len = MBEDTLS_GET_UINT16_BE(p, 0);
126 p += 2;
127
128 /* Check extensions do not go beyond the buffer of data. */
129 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, extensions_len);
130 extensions_end = p + extensions_len;
131
132 while (p < extensions_end) {
133 unsigned int extension_type;
134 size_t extension_data_len;
135
136 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, extensions_end, 4);
137 extension_type = MBEDTLS_GET_UINT16_BE(p, 0);
138 extension_data_len = MBEDTLS_GET_UINT16_BE(p, 2);
139 p += 4;
140 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, extensions_end, extension_data_len);
141
142 if (extension_type == MBEDTLS_TLS_EXT_SUPPORTED_VERSIONS) {
Ronald Croneff56732023-04-03 17:36:31 +0200143 *supported_versions_data = p;
144 *supported_versions_data_end = p + extension_data_len;
Ronald Cron47dce632023-02-08 17:38:29 +0100145 return 1;
146 }
147 p += extension_data_len;
148 }
149
150 return 0;
151}
152
Ronald Cron928cbd32022-10-04 16:14:26 +0200153#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED)
Jerry Yu30b071c2021-09-12 20:16:03 +0800154/*
Jerry Yu30b071c2021-09-12 20:16:03 +0800155 * STATE HANDLING: Read CertificateVerify
156 */
Jerry Yud0fc5852021-10-29 11:09:06 +0800157/* Macro to express the maximum length of the verify structure.
Jerry Yu30b071c2021-09-12 20:16:03 +0800158 *
159 * The structure is computed per TLS 1.3 specification as:
160 * - 64 bytes of octet 32,
161 * - 33 bytes for the context string
162 * (which is either "TLS 1.3, client CertificateVerify"
163 * or "TLS 1.3, server CertificateVerify"),
Jerry Yud0fc5852021-10-29 11:09:06 +0800164 * - 1 byte for the octet 0x0, which serves as a separator,
Jerry Yu30b071c2021-09-12 20:16:03 +0800165 * - 32 or 48 bytes for the Transcript-Hash(Handshake Context, Certificate)
166 * (depending on the size of the transcript_hash)
167 *
168 * This results in a total size of
169 * - 130 bytes for a SHA256-based transcript hash, or
170 * (64 + 33 + 1 + 32 bytes)
171 * - 146 bytes for a SHA384-based transcript hash.
172 * (64 + 33 + 1 + 48 bytes)
173 *
174 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100175#define SSL_VERIFY_STRUCT_MAX_SIZE (64 + \
176 33 + \
177 1 + \
178 MBEDTLS_TLS1_3_MD_MAX_SIZE \
179 )
Jerry Yu30b071c2021-09-12 20:16:03 +0800180
Jerry Yu0b32c502021-10-28 13:41:59 +0800181/*
182 * The ssl_tls13_create_verify_structure() creates the verify structure.
183 * As input, it requires the transcript hash.
184 *
185 * The caller has to ensure that the buffer has size at least
186 * SSL_VERIFY_STRUCT_MAX_SIZE bytes.
187 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100188static void ssl_tls13_create_verify_structure(const unsigned char *transcript_hash,
189 size_t transcript_hash_len,
190 unsigned char *verify_buffer,
191 size_t *verify_buffer_len,
192 int from)
Jerry Yu0b32c502021-10-28 13:41:59 +0800193{
194 size_t idx;
Jerry Yu30b071c2021-09-12 20:16:03 +0800195
Jerry Yu0b32c502021-10-28 13:41:59 +0800196 /* RFC 8446, Section 4.4.3:
197 *
198 * The digital signature [in the CertificateVerify message] is then
199 * computed over the concatenation of:
200 * - A string that consists of octet 32 (0x20) repeated 64 times
201 * - The context string
202 * - A single 0 byte which serves as the separator
203 * - The content to be signed
204 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100205 memset(verify_buffer, 0x20, 64);
Jerry Yu0b32c502021-10-28 13:41:59 +0800206 idx = 64;
207
Gilles Peskine449bd832023-01-11 14:50:10 +0100208 if (from == MBEDTLS_SSL_IS_CLIENT) {
209 memcpy(verify_buffer + idx, MBEDTLS_SSL_TLS1_3_LBL_WITH_LEN(client_cv));
210 idx += MBEDTLS_SSL_TLS1_3_LBL_LEN(client_cv);
211 } else { /* from == MBEDTLS_SSL_IS_SERVER */
212 memcpy(verify_buffer + idx, MBEDTLS_SSL_TLS1_3_LBL_WITH_LEN(server_cv));
213 idx += MBEDTLS_SSL_TLS1_3_LBL_LEN(server_cv);
Jerry Yu0b32c502021-10-28 13:41:59 +0800214 }
215
216 verify_buffer[idx++] = 0x0;
217
Gilles Peskine449bd832023-01-11 14:50:10 +0100218 memcpy(verify_buffer + idx, transcript_hash, transcript_hash_len);
Jerry Yu0b32c502021-10-28 13:41:59 +0800219 idx += transcript_hash_len;
220
221 *verify_buffer_len = idx;
222}
223
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200224MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100225static int ssl_tls13_parse_certificate_verify(mbedtls_ssl_context *ssl,
226 const unsigned char *buf,
227 const unsigned char *end,
228 const unsigned char *verify_buffer,
229 size_t verify_buffer_len)
Jerry Yu30b071c2021-09-12 20:16:03 +0800230{
231 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
pespaceka1378102022-04-26 15:03:11 +0200232 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Jerry Yu30b071c2021-09-12 20:16:03 +0800233 const unsigned char *p = buf;
234 uint16_t algorithm;
Jerry Yu30b071c2021-09-12 20:16:03 +0800235 size_t signature_len;
236 mbedtls_pk_type_t sig_alg;
237 mbedtls_md_type_t md_alg;
pespaceka1378102022-04-26 15:03:11 +0200238 psa_algorithm_t hash_alg = PSA_ALG_NONE;
239 unsigned char verify_hash[PSA_HASH_MAX_SIZE];
Jerry Yu30b071c2021-09-12 20:16:03 +0800240 size_t verify_hash_len;
241
Xiaofei Baid25fab62021-12-02 06:36:27 +0000242 void const *options = NULL;
XiaokangQian82d34cc2021-11-03 08:51:56 +0000243#if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT)
Xiaofei Baid25fab62021-12-02 06:36:27 +0000244 mbedtls_pk_rsassa_pss_options rsassa_pss_options;
XiaokangQian82d34cc2021-11-03 08:51:56 +0000245#endif /* MBEDTLS_X509_RSASSA_PSS_SUPPORT */
246
Jerry Yu30b071c2021-09-12 20:16:03 +0800247 /*
248 * struct {
249 * SignatureScheme algorithm;
250 * opaque signature<0..2^16-1>;
251 * } CertificateVerify;
252 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100253 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, 2);
254 algorithm = MBEDTLS_GET_UINT16_BE(p, 0);
Jerry Yu30b071c2021-09-12 20:16:03 +0800255 p += 2;
256
257 /* RFC 8446 section 4.4.3
258 *
Xiaokang Qian73437382023-03-29 08:24:12 +0000259 * If the CertificateVerify message is sent by a server, the signature
260 * algorithm MUST be one offered in the client's "signature_algorithms"
261 * extension unless no valid certificate chain can be produced without
262 * unsupported algorithms
Jerry Yu30b071c2021-09-12 20:16:03 +0800263 *
264 * RFC 8446 section 4.4.2.2
265 *
266 * If the client cannot construct an acceptable chain using the provided
Xiaokang Qian73437382023-03-29 08:24:12 +0000267 * certificates and decides to abort the handshake, then it MUST abort the
268 * handshake with an appropriate certificate-related alert
269 * (by default, "unsupported_certificate").
Jerry Yu30b071c2021-09-12 20:16:03 +0800270 *
Jerry Yu6f87f252021-10-29 20:12:51 +0800271 * Check if algorithm is an offered signature algorithm.
Jerry Yu30b071c2021-09-12 20:16:03 +0800272 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100273 if (!mbedtls_ssl_sig_alg_is_offered(ssl, algorithm)) {
Jerry Yu982d9e52021-10-14 15:59:37 +0800274 /* algorithm not in offered signature algorithms list */
Gilles Peskine449bd832023-01-11 14:50:10 +0100275 MBEDTLS_SSL_DEBUG_MSG(1, ("Received signature algorithm(%04x) is not "
276 "offered.",
277 (unsigned int) algorithm));
Jerry Yu6f87f252021-10-29 20:12:51 +0800278 goto error;
Jerry Yu30b071c2021-09-12 20:16:03 +0800279 }
280
Gilles Peskine449bd832023-01-11 14:50:10 +0100281 if (mbedtls_ssl_get_pk_type_and_md_alg_from_sig_alg(
282 algorithm, &sig_alg, &md_alg) != 0) {
Jerry Yu8c338862022-03-23 13:34:04 +0800283 goto error;
Jerry Yu30b071c2021-09-12 20:16:03 +0800284 }
285
Manuel Pégourié-Gonnard2d6d9932023-03-28 11:38:08 +0200286 hash_alg = mbedtls_md_psa_alg_from_type(md_alg);
Gilles Peskine449bd832023-01-11 14:50:10 +0100287 if (hash_alg == 0) {
pespaceka1378102022-04-26 15:03:11 +0200288 goto error;
289 }
290
Gilles Peskine449bd832023-01-11 14:50:10 +0100291 MBEDTLS_SSL_DEBUG_MSG(3, ("Certificate Verify: Signature algorithm ( %04x )",
292 (unsigned int) algorithm));
Jerry Yu30b071c2021-09-12 20:16:03 +0800293
294 /*
295 * Check the certificate's key type matches the signature alg
296 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100297 if (!mbedtls_pk_can_do(&ssl->session_negotiate->peer_cert->pk, sig_alg)) {
298 MBEDTLS_SSL_DEBUG_MSG(1, ("signature algorithm doesn't match cert key"));
Jerry Yu6f87f252021-10-29 20:12:51 +0800299 goto error;
Jerry Yu30b071c2021-09-12 20:16:03 +0800300 }
301
Gilles Peskine449bd832023-01-11 14:50:10 +0100302 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, 2);
303 signature_len = MBEDTLS_GET_UINT16_BE(p, 0);
Jerry Yu30b071c2021-09-12 20:16:03 +0800304 p += 2;
Gilles Peskine449bd832023-01-11 14:50:10 +0100305 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, signature_len);
Jerry Yu30b071c2021-09-12 20:16:03 +0800306
Gilles Peskine449bd832023-01-11 14:50:10 +0100307 status = psa_hash_compute(hash_alg,
308 verify_buffer,
309 verify_buffer_len,
310 verify_hash,
311 sizeof(verify_hash),
312 &verify_hash_len);
313 if (status != PSA_SUCCESS) {
314 MBEDTLS_SSL_DEBUG_RET(1, "hash computation PSA error", status);
Jerry Yu6f87f252021-10-29 20:12:51 +0800315 goto error;
Jerry Yu133690c2021-10-25 14:01:13 +0800316 }
317
Gilles Peskine449bd832023-01-11 14:50:10 +0100318 MBEDTLS_SSL_DEBUG_BUF(3, "verify hash", verify_hash, verify_hash_len);
XiaokangQian82d34cc2021-11-03 08:51:56 +0000319#if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT)
Gilles Peskine449bd832023-01-11 14:50:10 +0100320 if (sig_alg == MBEDTLS_PK_RSASSA_PSS) {
Xiaofei Baid25fab62021-12-02 06:36:27 +0000321 rsassa_pss_options.mgf1_hash_id = md_alg;
Przemek Stekiel6a5e0182022-06-27 11:53:13 +0200322
Gilles Peskine449bd832023-01-11 14:50:10 +0100323 rsassa_pss_options.expected_salt_len = PSA_HASH_LENGTH(hash_alg);
324 options = (const void *) &rsassa_pss_options;
XiaokangQian82d34cc2021-11-03 08:51:56 +0000325 }
326#endif /* MBEDTLS_X509_RSASSA_PSS_SUPPORT */
Jerry Yu30b071c2021-09-12 20:16:03 +0800327
Gilles Peskine449bd832023-01-11 14:50:10 +0100328 if ((ret = mbedtls_pk_verify_ext(sig_alg, options,
329 &ssl->session_negotiate->peer_cert->pk,
330 md_alg, verify_hash, verify_hash_len,
331 p, signature_len)) == 0) {
332 return 0;
Jerry Yu30b071c2021-09-12 20:16:03 +0800333 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100334 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_pk_verify_ext", ret);
Jerry Yu30b071c2021-09-12 20:16:03 +0800335
Jerry Yu6f87f252021-10-29 20:12:51 +0800336error:
337 /* RFC 8446 section 4.4.3
338 *
339 * If the verification fails, the receiver MUST terminate the handshake
340 * with a "decrypt_error" alert.
Gilles Peskine449bd832023-01-11 14:50:10 +0100341 */
342 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_DECRYPT_ERROR,
343 MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE);
344 return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
Jerry Yu6f87f252021-10-29 20:12:51 +0800345
Jerry Yu30b071c2021-09-12 20:16:03 +0800346}
Ronald Cron928cbd32022-10-04 16:14:26 +0200347#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED */
Jerry Yu30b071c2021-09-12 20:16:03 +0800348
Gilles Peskine449bd832023-01-11 14:50:10 +0100349int mbedtls_ssl_tls13_process_certificate_verify(mbedtls_ssl_context *ssl)
Jerry Yu30b071c2021-09-12 20:16:03 +0800350{
Jerry Yu30b071c2021-09-12 20:16:03 +0800351
Ronald Cron928cbd32022-10-04 16:14:26 +0200352#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED)
Jerry Yuda8cdf22021-10-25 15:06:49 +0800353 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
354 unsigned char verify_buffer[SSL_VERIFY_STRUCT_MAX_SIZE];
355 size_t verify_buffer_len;
356 unsigned char transcript[MBEDTLS_TLS1_3_MD_MAX_SIZE];
357 size_t transcript_len;
358 unsigned char *buf;
359 size_t buf_len;
360
Gilles Peskine449bd832023-01-11 14:50:10 +0100361 MBEDTLS_SSL_DEBUG_MSG(2, ("=> parse certificate verify"));
Jerry Yu30b071c2021-09-12 20:16:03 +0800362
Jerry Yuda8cdf22021-10-25 15:06:49 +0800363 MBEDTLS_SSL_PROC_CHK(
Xiaokang Qian73437382023-03-29 08:24:12 +0000364 mbedtls_ssl_tls13_fetch_handshake_msg(
365 ssl, MBEDTLS_SSL_HS_CERTIFICATE_VERIFY, &buf, &buf_len));
Jerry Yu30b071c2021-09-12 20:16:03 +0800366
Jerry Yuda8cdf22021-10-25 15:06:49 +0800367 /* Need to calculate the hash of the transcript first
Jerry Yu0b32c502021-10-28 13:41:59 +0800368 * before reading the message since otherwise it gets
369 * included in the transcript
370 */
Xiaokang Qian73437382023-03-29 08:24:12 +0000371 ret = mbedtls_ssl_get_handshake_transcript(
372 ssl,
373 ssl->handshake->ciphersuite_info->mac,
374 transcript, sizeof(transcript),
375 &transcript_len);
Gilles Peskine449bd832023-01-11 14:50:10 +0100376 if (ret != 0) {
Jerry Yuda8cdf22021-10-25 15:06:49 +0800377 MBEDTLS_SSL_PEND_FATAL_ALERT(
378 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR,
Gilles Peskine449bd832023-01-11 14:50:10 +0100379 MBEDTLS_ERR_SSL_INTERNAL_ERROR);
380 return ret;
Jerry Yu30b071c2021-09-12 20:16:03 +0800381 }
382
Gilles Peskine449bd832023-01-11 14:50:10 +0100383 MBEDTLS_SSL_DEBUG_BUF(3, "handshake hash", transcript, transcript_len);
Jerry Yuda8cdf22021-10-25 15:06:49 +0800384
385 /* Create verify structure */
Gilles Peskine449bd832023-01-11 14:50:10 +0100386 ssl_tls13_create_verify_structure(transcript,
387 transcript_len,
388 verify_buffer,
389 &verify_buffer_len,
390 (ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT) ?
391 MBEDTLS_SSL_IS_SERVER :
392 MBEDTLS_SSL_IS_CLIENT);
Jerry Yuda8cdf22021-10-25 15:06:49 +0800393
394 /* Process the message contents */
Xiaokang Qian73437382023-03-29 08:24:12 +0000395 MBEDTLS_SSL_PROC_CHK(ssl_tls13_parse_certificate_verify(
396 ssl, buf, buf + buf_len,
397 verify_buffer, verify_buffer_len));
Jerry Yuda8cdf22021-10-25 15:06:49 +0800398
Xiaokang Qian73437382023-03-29 08:24:12 +0000399 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_add_hs_msg_to_checksum(
400 ssl, MBEDTLS_SSL_HS_CERTIFICATE_VERIFY,
401 buf, buf_len));
Jerry Yu30b071c2021-09-12 20:16:03 +0800402
403cleanup:
404
Gilles Peskine449bd832023-01-11 14:50:10 +0100405 MBEDTLS_SSL_DEBUG_MSG(2, ("<= parse certificate verify"));
406 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_tls13_process_certificate_verify", ret);
407 return ret;
Jerry Yuda8cdf22021-10-25 15:06:49 +0800408#else
409 ((void) ssl);
Gilles Peskine449bd832023-01-11 14:50:10 +0100410 MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
411 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Ronald Cron928cbd32022-10-04 16:14:26 +0200412#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED */
Jerry Yu30b071c2021-09-12 20:16:03 +0800413}
414
415/*
Xiaofei Bai947571e2021-09-29 09:12:03 +0000416 *
XiaokangQian6b916b12022-04-25 07:29:34 +0000417 * STATE HANDLING: Incoming Certificate.
Xiaofei Bai947571e2021-09-29 09:12:03 +0000418 *
419 */
420
Ronald Cronde08cf32022-10-04 17:15:35 +0200421#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED)
Xiaofei Bai947571e2021-09-29 09:12:03 +0000422#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
423/*
424 * Structure of Certificate message:
425 *
426 * enum {
427 * X509(0),
428 * RawPublicKey(2),
429 * (255)
430 * } CertificateType;
431 *
432 * struct {
433 * select (certificate_type) {
434 * case RawPublicKey:
435 * * From RFC 7250 ASN.1_subjectPublicKeyInfo *
436 * opaque ASN1_subjectPublicKeyInfo<1..2^24-1>;
437 * case X509:
438 * opaque cert_data<1..2^24-1>;
439 * };
440 * Extension extensions<0..2^16-1>;
441 * } CertificateEntry;
442 *
443 * struct {
444 * opaque certificate_request_context<0..2^8-1>;
445 * CertificateEntry certificate_list<0..2^24-1>;
446 * } Certificate;
447 *
448 */
Xiaofei Bai79595ac2021-10-26 07:16:45 +0000449
450/* Parse certificate chain send by the server. */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200451MBEDTLS_CHECK_RETURN_CRITICAL
Ronald Crone3dac4a2022-06-10 17:21:51 +0200452MBEDTLS_STATIC_TESTABLE
Gilles Peskine449bd832023-01-11 14:50:10 +0100453int mbedtls_ssl_tls13_parse_certificate(mbedtls_ssl_context *ssl,
454 const unsigned char *buf,
455 const unsigned char *end)
Xiaofei Bai947571e2021-09-29 09:12:03 +0000456{
457 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
458 size_t certificate_request_context_len = 0;
459 size_t certificate_list_len = 0;
460 const unsigned char *p = buf;
461 const unsigned char *certificate_list_end;
Jerry Yuc4bf5d62022-10-29 09:08:47 +0800462 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
Xiaofei Bai947571e2021-09-29 09:12:03 +0000463
Gilles Peskine449bd832023-01-11 14:50:10 +0100464 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, 4);
Xiaofei Bai947571e2021-09-29 09:12:03 +0000465 certificate_request_context_len = p[0];
Gilles Peskine449bd832023-01-11 14:50:10 +0100466 certificate_list_len = MBEDTLS_GET_UINT24_BE(p, 1);
XiaokangQian63e713e2022-05-15 04:26:57 +0000467 p += 4;
Xiaofei Bai947571e2021-09-29 09:12:03 +0000468
469 /* In theory, the certificate list can be up to 2^24 Bytes, but we don't
470 * support anything beyond 2^16 = 64K.
471 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100472 if ((certificate_request_context_len != 0) ||
473 (certificate_list_len >= 0x10000)) {
474 MBEDTLS_SSL_DEBUG_MSG(1, ("bad certificate message"));
475 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR,
476 MBEDTLS_ERR_SSL_DECODE_ERROR);
477 return MBEDTLS_ERR_SSL_DECODE_ERROR;
Xiaofei Bai947571e2021-09-29 09:12:03 +0000478 }
479
480 /* In case we tried to reuse a session but it failed */
Gilles Peskine449bd832023-01-11 14:50:10 +0100481 if (ssl->session_negotiate->peer_cert != NULL) {
482 mbedtls_x509_crt_free(ssl->session_negotiate->peer_cert);
483 mbedtls_free(ssl->session_negotiate->peer_cert);
Xiaofei Bai947571e2021-09-29 09:12:03 +0000484 }
485
Gilles Peskine449bd832023-01-11 14:50:10 +0100486 if (certificate_list_len == 0) {
XiaokangQianc3017f62022-05-13 05:55:41 +0000487 ssl->session_negotiate->peer_cert = NULL;
488 ret = 0;
489 goto exit;
490 }
491
Gilles Peskine449bd832023-01-11 14:50:10 +0100492 if ((ssl->session_negotiate->peer_cert =
493 mbedtls_calloc(1, sizeof(mbedtls_x509_crt))) == NULL) {
494 MBEDTLS_SSL_DEBUG_MSG(1, ("alloc( %" MBEDTLS_PRINTF_SIZET " bytes ) failed",
495 sizeof(mbedtls_x509_crt)));
496 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR,
497 MBEDTLS_ERR_SSL_ALLOC_FAILED);
498 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
Xiaofei Bai947571e2021-09-29 09:12:03 +0000499 }
500
Gilles Peskine449bd832023-01-11 14:50:10 +0100501 mbedtls_x509_crt_init(ssl->session_negotiate->peer_cert);
Xiaofei Bai947571e2021-09-29 09:12:03 +0000502
Gilles Peskine449bd832023-01-11 14:50:10 +0100503 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, certificate_list_len);
Xiaofei Bai947571e2021-09-29 09:12:03 +0000504 certificate_list_end = p + certificate_list_len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100505 while (p < certificate_list_end) {
Xiaofei Bai947571e2021-09-29 09:12:03 +0000506 size_t cert_data_len, extensions_len;
Jerry Yu2eaa7602022-08-04 17:28:15 +0800507 const unsigned char *extensions_end;
Xiaofei Bai947571e2021-09-29 09:12:03 +0000508
Gilles Peskine449bd832023-01-11 14:50:10 +0100509 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, certificate_list_end, 3);
510 cert_data_len = MBEDTLS_GET_UINT24_BE(p, 0);
Xiaofei Bai947571e2021-09-29 09:12:03 +0000511 p += 3;
512
513 /* In theory, the CRT can be up to 2^24 Bytes, but we don't support
514 * anything beyond 2^16 = 64K. Otherwise as in the TLS 1.2 code,
515 * check that we have a minimum of 128 bytes of data, this is not
516 * clear why we need that though.
517 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100518 if ((cert_data_len < 128) || (cert_data_len >= 0x10000)) {
519 MBEDTLS_SSL_DEBUG_MSG(1, ("bad Certificate message"));
520 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR,
521 MBEDTLS_ERR_SSL_DECODE_ERROR);
522 return MBEDTLS_ERR_SSL_DECODE_ERROR;
Xiaofei Bai947571e2021-09-29 09:12:03 +0000523 }
524
Gilles Peskine449bd832023-01-11 14:50:10 +0100525 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, certificate_list_end, cert_data_len);
526 ret = mbedtls_x509_crt_parse_der(ssl->session_negotiate->peer_cert,
527 p, cert_data_len);
Xiaofei Bai947571e2021-09-29 09:12:03 +0000528
Gilles Peskine449bd832023-01-11 14:50:10 +0100529 switch (ret) {
Xiaofei Bai947571e2021-09-29 09:12:03 +0000530 case 0: /*ok*/
531 break;
532 case MBEDTLS_ERR_X509_UNKNOWN_SIG_ALG + MBEDTLS_ERR_OID_NOT_FOUND:
533 /* Ignore certificate with an unknown algorithm: maybe a
534 prior certificate was already trusted. */
535 break;
536
537 case MBEDTLS_ERR_X509_ALLOC_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100538 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR,
539 MBEDTLS_ERR_X509_ALLOC_FAILED);
540 MBEDTLS_SSL_DEBUG_RET(1, " mbedtls_x509_crt_parse_der", ret);
541 return ret;
Xiaofei Bai947571e2021-09-29 09:12:03 +0000542
543 case MBEDTLS_ERR_X509_UNKNOWN_VERSION:
Gilles Peskine449bd832023-01-11 14:50:10 +0100544 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT,
545 MBEDTLS_ERR_X509_UNKNOWN_VERSION);
546 MBEDTLS_SSL_DEBUG_RET(1, " mbedtls_x509_crt_parse_der", ret);
547 return ret;
Xiaofei Bai947571e2021-09-29 09:12:03 +0000548
549 default:
Gilles Peskine449bd832023-01-11 14:50:10 +0100550 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_BAD_CERT,
551 ret);
552 MBEDTLS_SSL_DEBUG_RET(1, " mbedtls_x509_crt_parse_der", ret);
553 return ret;
Xiaofei Bai947571e2021-09-29 09:12:03 +0000554 }
555
556 p += cert_data_len;
557
558 /* Certificate extensions length */
Gilles Peskine449bd832023-01-11 14:50:10 +0100559 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, certificate_list_end, 2);
560 extensions_len = MBEDTLS_GET_UINT16_BE(p, 0);
Xiaofei Bai947571e2021-09-29 09:12:03 +0000561 p += 2;
Gilles Peskine449bd832023-01-11 14:50:10 +0100562 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, certificate_list_end, extensions_len);
Jerry Yu2eaa7602022-08-04 17:28:15 +0800563
564 extensions_end = p + extensions_len;
Jerry Yu0d5cfb72022-10-31 14:15:48 +0800565 handshake->received_extensions = MBEDTLS_SSL_EXT_MASK_NONE;
Jerry Yu2eaa7602022-08-04 17:28:15 +0800566
Gilles Peskine449bd832023-01-11 14:50:10 +0100567 while (p < extensions_end) {
Jerry Yu2eaa7602022-08-04 17:28:15 +0800568 unsigned int extension_type;
569 size_t extension_data_len;
570
571 /*
Gilles Peskine449bd832023-01-11 14:50:10 +0100572 * struct {
573 * ExtensionType extension_type; (2 bytes)
574 * opaque extension_data<0..2^16-1>;
575 * } Extension;
576 */
577 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, extensions_end, 4);
578 extension_type = MBEDTLS_GET_UINT16_BE(p, 0);
579 extension_data_len = MBEDTLS_GET_UINT16_BE(p, 2);
Jerry Yu2eaa7602022-08-04 17:28:15 +0800580 p += 4;
581
Gilles Peskine449bd832023-01-11 14:50:10 +0100582 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, extensions_end, extension_data_len);
Jerry Yu2eaa7602022-08-04 17:28:15 +0800583
Jerry Yuc4bf5d62022-10-29 09:08:47 +0800584 ret = mbedtls_ssl_tls13_check_received_extension(
Gilles Peskine449bd832023-01-11 14:50:10 +0100585 ssl, MBEDTLS_SSL_HS_CERTIFICATE, extension_type,
586 MBEDTLS_SSL_TLS1_3_ALLOWED_EXTS_OF_CT);
587 if (ret != 0) {
588 return ret;
589 }
Jerry Yu0c354a22022-08-29 15:25:36 +0800590
Gilles Peskine449bd832023-01-11 14:50:10 +0100591 switch (extension_type) {
Jerry Yu2eaa7602022-08-04 17:28:15 +0800592 default:
Jerry Yu79aa7212022-11-08 21:30:21 +0800593 MBEDTLS_SSL_PRINT_EXT(
Jerry Yu0d5cfb72022-10-31 14:15:48 +0800594 3, MBEDTLS_SSL_HS_CERTIFICATE,
Gilles Peskine449bd832023-01-11 14:50:10 +0100595 extension_type, "( ignored )");
Jerry Yu2eaa7602022-08-04 17:28:15 +0800596 break;
597 }
598
599 p += extension_data_len;
600 }
601
Gilles Peskine449bd832023-01-11 14:50:10 +0100602 MBEDTLS_SSL_PRINT_EXTS(3, MBEDTLS_SSL_HS_CERTIFICATE,
603 handshake->received_extensions);
Xiaofei Bai947571e2021-09-29 09:12:03 +0000604 }
605
XiaokangQian63e713e2022-05-15 04:26:57 +0000606exit:
Xiaofei Bai947571e2021-09-29 09:12:03 +0000607 /* Check that all the message is consumed. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100608 if (p != end) {
609 MBEDTLS_SSL_DEBUG_MSG(1, ("bad Certificate message"));
610 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR,
611 MBEDTLS_ERR_SSL_DECODE_ERROR);
612 return MBEDTLS_ERR_SSL_DECODE_ERROR;
Xiaofei Bai947571e2021-09-29 09:12:03 +0000613 }
614
Xiaokang Qian73437382023-03-29 08:24:12 +0000615 MBEDTLS_SSL_DEBUG_CRT(3, "peer certificate",
616 ssl->session_negotiate->peer_cert);
Xiaofei Bai947571e2021-09-29 09:12:03 +0000617
Gilles Peskine449bd832023-01-11 14:50:10 +0100618 return ret;
Xiaofei Bai947571e2021-09-29 09:12:03 +0000619}
620#else
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200621MBEDTLS_CHECK_RETURN_CRITICAL
Ronald Crone3dac4a2022-06-10 17:21:51 +0200622MBEDTLS_STATIC_TESTABLE
Gilles Peskine449bd832023-01-11 14:50:10 +0100623int mbedtls_ssl_tls13_parse_certificate(mbedtls_ssl_context *ssl,
624 const unsigned char *buf,
625 const unsigned char *end)
Xiaofei Bai947571e2021-09-29 09:12:03 +0000626{
627 ((void) ssl);
628 ((void) buf);
629 ((void) end);
Gilles Peskine449bd832023-01-11 14:50:10 +0100630 return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Xiaofei Bai947571e2021-09-29 09:12:03 +0000631}
632#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Ronald Cronde08cf32022-10-04 17:15:35 +0200633#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED */
Xiaofei Bai947571e2021-09-29 09:12:03 +0000634
Ronald Cronde08cf32022-10-04 17:15:35 +0200635#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED)
Xiaofei Bai947571e2021-09-29 09:12:03 +0000636#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Xiaofei Bai79595ac2021-10-26 07:16:45 +0000637/* Validate certificate chain sent by the server. */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200638MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100639static int ssl_tls13_validate_certificate(mbedtls_ssl_context *ssl)
Xiaofei Bai947571e2021-09-29 09:12:03 +0000640{
641 int ret = 0;
XiaokangQian989f06d2022-05-17 01:50:15 +0000642 int authmode = MBEDTLS_SSL_VERIFY_REQUIRED;
Xiaofei Bai947571e2021-09-29 09:12:03 +0000643 mbedtls_x509_crt *ca_chain;
644 mbedtls_x509_crl *ca_crl;
Ronald Cron30c5a252022-06-16 19:31:06 +0200645 const char *ext_oid;
646 size_t ext_len;
Xiaofei Baiff456022021-10-28 06:50:17 +0000647 uint32_t verify_result = 0;
Xiaofei Bai947571e2021-09-29 09:12:03 +0000648
XiaokangQian6b916b12022-04-25 07:29:34 +0000649 /* If SNI was used, overwrite authentication mode
650 * from the configuration. */
XiaokangQian989f06d2022-05-17 01:50:15 +0000651#if defined(MBEDTLS_SSL_SRV_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100652 if (ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER) {
XiaokangQian0557c942022-05-30 08:10:53 +0000653#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Gilles Peskine449bd832023-01-11 14:50:10 +0100654 if (ssl->handshake->sni_authmode != MBEDTLS_SSL_VERIFY_UNSET) {
XiaokangQian0557c942022-05-30 08:10:53 +0000655 authmode = ssl->handshake->sni_authmode;
Gilles Peskine449bd832023-01-11 14:50:10 +0100656 } else
XiaokangQian0557c942022-05-30 08:10:53 +0000657#endif
Gilles Peskine449bd832023-01-11 14:50:10 +0100658 authmode = ssl->conf->authmode;
XiaokangQian0557c942022-05-30 08:10:53 +0000659 }
XiaokangQian6b916b12022-04-25 07:29:34 +0000660#endif
661
662 /*
XiaokangQian989f06d2022-05-17 01:50:15 +0000663 * If the peer hasn't sent a certificate ( i.e. it sent
XiaokangQian6b916b12022-04-25 07:29:34 +0000664 * an empty certificate chain ), this is reflected in the peer CRT
665 * structure being unset.
666 * Check for that and handle it depending on the
XiaokangQian989f06d2022-05-17 01:50:15 +0000667 * authentication mode.
XiaokangQian6b916b12022-04-25 07:29:34 +0000668 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100669 if (ssl->session_negotiate->peer_cert == NULL) {
670 MBEDTLS_SSL_DEBUG_MSG(1, ("peer has no certificate"));
XiaokangQian989f06d2022-05-17 01:50:15 +0000671
XiaokangQian63e713e2022-05-15 04:26:57 +0000672#if defined(MBEDTLS_SSL_SRV_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100673 if (ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER) {
XiaokangQian63e713e2022-05-15 04:26:57 +0000674 /* The client was asked for a certificate but didn't send
675 * one. The client should know what's going on, so we
676 * don't send an alert.
677 */
678 ssl->session_negotiate->verify_result = MBEDTLS_X509_BADCERT_MISSING;
Gilles Peskine449bd832023-01-11 14:50:10 +0100679 if (authmode == MBEDTLS_SSL_VERIFY_OPTIONAL) {
680 return 0;
681 } else {
Xiaokang Qian73437382023-03-29 08:24:12 +0000682 MBEDTLS_SSL_PEND_FATAL_ALERT(
683 MBEDTLS_SSL_ALERT_MSG_NO_CERT,
684 MBEDTLS_ERR_SSL_NO_CLIENT_CERTIFICATE);
Gilles Peskine449bd832023-01-11 14:50:10 +0100685 return MBEDTLS_ERR_SSL_NO_CLIENT_CERTIFICATE;
XiaokangQian989f06d2022-05-17 01:50:15 +0000686 }
XiaokangQian63e713e2022-05-15 04:26:57 +0000687 }
XiaokangQian6b916b12022-04-25 07:29:34 +0000688#endif /* MBEDTLS_SSL_SRV_C */
689
XiaokangQianc3017f62022-05-13 05:55:41 +0000690#if defined(MBEDTLS_SSL_CLI_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100691 if (ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT) {
692 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_NO_CERT,
693 MBEDTLS_ERR_SSL_FATAL_ALERT_MESSAGE);
694 return MBEDTLS_ERR_SSL_FATAL_ALERT_MESSAGE;
XiaokangQian63e713e2022-05-15 04:26:57 +0000695 }
XiaokangQianc3017f62022-05-13 05:55:41 +0000696#endif /* MBEDTLS_SSL_CLI_C */
XiaokangQian63e713e2022-05-15 04:26:57 +0000697 }
XiaokangQian6b916b12022-04-25 07:29:34 +0000698
Xiaofei Bai947571e2021-09-29 09:12:03 +0000699#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Gilles Peskine449bd832023-01-11 14:50:10 +0100700 if (ssl->handshake->sni_ca_chain != NULL) {
Xiaofei Bai947571e2021-09-29 09:12:03 +0000701 ca_chain = ssl->handshake->sni_ca_chain;
702 ca_crl = ssl->handshake->sni_ca_crl;
Gilles Peskine449bd832023-01-11 14:50:10 +0100703 } else
Xiaofei Bai947571e2021-09-29 09:12:03 +0000704#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
705 {
706 ca_chain = ssl->conf->ca_chain;
707 ca_crl = ssl->conf->ca_crl;
708 }
709
710 /*
711 * Main check: verify certificate
712 */
713 ret = mbedtls_x509_crt_verify_with_profile(
714 ssl->session_negotiate->peer_cert,
715 ca_chain, ca_crl,
716 ssl->conf->cert_profile,
717 ssl->hostname,
Xiaofei Baiff456022021-10-28 06:50:17 +0000718 &verify_result,
Gilles Peskine449bd832023-01-11 14:50:10 +0100719 ssl->conf->f_vrfy, ssl->conf->p_vrfy);
Xiaofei Bai947571e2021-09-29 09:12:03 +0000720
Gilles Peskine449bd832023-01-11 14:50:10 +0100721 if (ret != 0) {
722 MBEDTLS_SSL_DEBUG_RET(1, "x509_verify_cert", ret);
Xiaofei Bai947571e2021-09-29 09:12:03 +0000723 }
724
725 /*
726 * Secondary checks: always done, but change 'ret' only if it was 0
727 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100728 if (ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT) {
Ronald Cron30c5a252022-06-16 19:31:06 +0200729 ext_oid = MBEDTLS_OID_SERVER_AUTH;
Gilles Peskine449bd832023-01-11 14:50:10 +0100730 ext_len = MBEDTLS_OID_SIZE(MBEDTLS_OID_SERVER_AUTH);
731 } else {
Ronald Cron30c5a252022-06-16 19:31:06 +0200732 ext_oid = MBEDTLS_OID_CLIENT_AUTH;
Gilles Peskine449bd832023-01-11 14:50:10 +0100733 ext_len = MBEDTLS_OID_SIZE(MBEDTLS_OID_CLIENT_AUTH);
Ronald Cron30c5a252022-06-16 19:31:06 +0200734 }
735
Gilles Peskine449bd832023-01-11 14:50:10 +0100736 if ((mbedtls_x509_crt_check_key_usage(
737 ssl->session_negotiate->peer_cert,
738 MBEDTLS_X509_KU_DIGITAL_SIGNATURE) != 0) ||
739 (mbedtls_x509_crt_check_extended_key_usage(
740 ssl->session_negotiate->peer_cert,
741 ext_oid, ext_len) != 0)) {
742 MBEDTLS_SSL_DEBUG_MSG(1, ("bad certificate (usage extensions)"));
743 if (ret == 0) {
Xiaofei Bai947571e2021-09-29 09:12:03 +0000744 ret = MBEDTLS_ERR_SSL_BAD_CERTIFICATE;
Gilles Peskine449bd832023-01-11 14:50:10 +0100745 }
Xiaofei Bai947571e2021-09-29 09:12:03 +0000746 }
747
XiaokangQian6b916b12022-04-25 07:29:34 +0000748 /* mbedtls_x509_crt_verify_with_profile is supposed to report a
749 * verification failure through MBEDTLS_ERR_X509_CERT_VERIFY_FAILED,
750 * with details encoded in the verification flags. All other kinds
751 * of error codes, including those from the user provided f_vrfy
752 * functions, are treated as fatal and lead to a failure of
Ronald Crone3dac4a2022-06-10 17:21:51 +0200753 * mbedtls_ssl_tls13_parse_certificate even if verification was optional.
754 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100755 if (authmode == MBEDTLS_SSL_VERIFY_OPTIONAL &&
756 (ret == MBEDTLS_ERR_X509_CERT_VERIFY_FAILED ||
757 ret == MBEDTLS_ERR_SSL_BAD_CERTIFICATE)) {
XiaokangQian6b916b12022-04-25 07:29:34 +0000758 ret = 0;
759 }
Xiaofei Bai947571e2021-09-29 09:12:03 +0000760
Gilles Peskine449bd832023-01-11 14:50:10 +0100761 if (ca_chain == NULL && authmode == MBEDTLS_SSL_VERIFY_REQUIRED) {
762 MBEDTLS_SSL_DEBUG_MSG(1, ("got no CA chain"));
Xiaofei Bai947571e2021-09-29 09:12:03 +0000763 ret = MBEDTLS_ERR_SSL_CA_CHAIN_REQUIRED;
764 }
765
Gilles Peskine449bd832023-01-11 14:50:10 +0100766 if (ret != 0) {
Xiaofei Bai947571e2021-09-29 09:12:03 +0000767 /* The certificate may have been rejected for several reasons.
768 Pick one and send the corresponding alert. Which alert to send
769 may be a subject of debate in some cases. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100770 if (verify_result & MBEDTLS_X509_BADCERT_OTHER) {
Xiaokang Qian73437382023-03-29 08:24:12 +0000771 MBEDTLS_SSL_PEND_FATAL_ALERT(
772 MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED, ret);
Gilles Peskine449bd832023-01-11 14:50:10 +0100773 } else if (verify_result & MBEDTLS_X509_BADCERT_CN_MISMATCH) {
774 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_BAD_CERT, ret);
775 } else if (verify_result & (MBEDTLS_X509_BADCERT_KEY_USAGE |
776 MBEDTLS_X509_BADCERT_EXT_KEY_USAGE |
777 MBEDTLS_X509_BADCERT_NS_CERT_TYPE |
778 MBEDTLS_X509_BADCERT_BAD_PK |
779 MBEDTLS_X509_BADCERT_BAD_KEY)) {
Xiaokang Qian73437382023-03-29 08:24:12 +0000780 MBEDTLS_SSL_PEND_FATAL_ALERT(
781 MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT, ret);
Gilles Peskine449bd832023-01-11 14:50:10 +0100782 } else if (verify_result & MBEDTLS_X509_BADCERT_EXPIRED) {
Xiaokang Qian73437382023-03-29 08:24:12 +0000783 MBEDTLS_SSL_PEND_FATAL_ALERT(
784 MBEDTLS_SSL_ALERT_MSG_CERT_EXPIRED, ret);
Gilles Peskine449bd832023-01-11 14:50:10 +0100785 } else if (verify_result & MBEDTLS_X509_BADCERT_REVOKED) {
Xiaokang Qian73437382023-03-29 08:24:12 +0000786 MBEDTLS_SSL_PEND_FATAL_ALERT(
787 MBEDTLS_SSL_ALERT_MSG_CERT_REVOKED, ret);
Gilles Peskine449bd832023-01-11 14:50:10 +0100788 } else if (verify_result & MBEDTLS_X509_BADCERT_NOT_TRUSTED) {
789 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_UNKNOWN_CA, ret);
790 } else {
Xiaokang Qian73437382023-03-29 08:24:12 +0000791 MBEDTLS_SSL_PEND_FATAL_ALERT(
792 MBEDTLS_SSL_ALERT_MSG_CERT_UNKNOWN, ret);
Gilles Peskine449bd832023-01-11 14:50:10 +0100793 }
Xiaofei Bai947571e2021-09-29 09:12:03 +0000794 }
795
796#if defined(MBEDTLS_DEBUG_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100797 if (verify_result != 0) {
798 MBEDTLS_SSL_DEBUG_MSG(3, ("! Certificate verification flags %08x",
799 (unsigned int) verify_result));
800 } else {
801 MBEDTLS_SSL_DEBUG_MSG(3, ("Certificate verification flags clear"));
Xiaofei Bai947571e2021-09-29 09:12:03 +0000802 }
803#endif /* MBEDTLS_DEBUG_C */
804
Xiaofei Baiff456022021-10-28 06:50:17 +0000805 ssl->session_negotiate->verify_result = verify_result;
Gilles Peskine449bd832023-01-11 14:50:10 +0100806 return ret;
Xiaofei Bai947571e2021-09-29 09:12:03 +0000807}
808#else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200809MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100810static int ssl_tls13_validate_certificate(mbedtls_ssl_context *ssl)
Xiaofei Bai947571e2021-09-29 09:12:03 +0000811{
812 ((void) ssl);
Gilles Peskine449bd832023-01-11 14:50:10 +0100813 return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Xiaofei Bai947571e2021-09-29 09:12:03 +0000814}
815#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Ronald Cronde08cf32022-10-04 17:15:35 +0200816#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED */
Xiaofei Bai947571e2021-09-29 09:12:03 +0000817
Gilles Peskine449bd832023-01-11 14:50:10 +0100818int mbedtls_ssl_tls13_process_certificate(mbedtls_ssl_context *ssl)
Xiaofei Bai947571e2021-09-29 09:12:03 +0000819{
Xiaofei Bai79595ac2021-10-26 07:16:45 +0000820 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine449bd832023-01-11 14:50:10 +0100821 MBEDTLS_SSL_DEBUG_MSG(2, ("=> parse certificate"));
Xiaofei Bai79595ac2021-10-26 07:16:45 +0000822
Ronald Cronde08cf32022-10-04 17:15:35 +0200823#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED)
XiaokangQianc3017f62022-05-13 05:55:41 +0000824 unsigned char *buf;
825 size_t buf_len;
Xiaofei Bai79595ac2021-10-26 07:16:45 +0000826
Gilles Peskine449bd832023-01-11 14:50:10 +0100827 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_tls13_fetch_handshake_msg(
828 ssl, MBEDTLS_SSL_HS_CERTIFICATE,
829 &buf, &buf_len));
Xiaofei Bai79595ac2021-10-26 07:16:45 +0000830
XiaokangQianc3017f62022-05-13 05:55:41 +0000831 /* Parse the certificate chain sent by the peer. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100832 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_tls13_parse_certificate(ssl, buf,
833 buf + buf_len));
XiaokangQianc3017f62022-05-13 05:55:41 +0000834 /* Validate the certificate chain and set the verification results. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100835 MBEDTLS_SSL_PROC_CHK(ssl_tls13_validate_certificate(ssl));
Xiaofei Bai79595ac2021-10-26 07:16:45 +0000836
Xiaokang Qian73437382023-03-29 08:24:12 +0000837 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_add_hs_msg_to_checksum(
838 ssl, MBEDTLS_SSL_HS_CERTIFICATE, buf, buf_len));
Xiaofei Bai79595ac2021-10-26 07:16:45 +0000839
Xiaofei Bai79595ac2021-10-26 07:16:45 +0000840cleanup:
Ronald Cronde08cf32022-10-04 17:15:35 +0200841#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED */
Xiaofei Bai79595ac2021-10-26 07:16:45 +0000842
Gilles Peskine449bd832023-01-11 14:50:10 +0100843 MBEDTLS_SSL_DEBUG_MSG(2, ("<= parse certificate"));
844 return ret;
Xiaofei Bai947571e2021-09-29 09:12:03 +0000845}
Ronald Cron928cbd32022-10-04 16:14:26 +0200846#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED)
Jerry Yu7399d0d2022-01-30 17:54:19 +0800847/*
848 * enum {
849 * X509(0),
850 * RawPublicKey(2),
851 * (255)
852 * } CertificateType;
853 *
854 * struct {
855 * select (certificate_type) {
856 * case RawPublicKey:
857 * // From RFC 7250 ASN.1_subjectPublicKeyInfo
858 * opaque ASN1_subjectPublicKeyInfo<1..2^24-1>;
859 *
860 * case X509:
861 * opaque cert_data<1..2^24-1>;
862 * };
863 * Extension extensions<0..2^16-1>;
864 * } CertificateEntry;
865 *
866 * struct {
867 * opaque certificate_request_context<0..2^8-1>;
868 * CertificateEntry certificate_list<0..2^24-1>;
869 * } Certificate;
870 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200871MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100872static int ssl_tls13_write_certificate_body(mbedtls_ssl_context *ssl,
873 unsigned char *buf,
874 unsigned char *end,
875 size_t *out_len)
Jerry Yu5cc35062022-01-28 16:16:08 +0800876{
Gilles Peskine449bd832023-01-11 14:50:10 +0100877 const mbedtls_x509_crt *crt = mbedtls_ssl_own_cert(ssl);
Jerry Yu3e536442022-02-15 11:05:59 +0800878 unsigned char *p = buf;
Jerry Yuc8d8d4e2022-02-18 12:10:03 +0800879 unsigned char *certificate_request_context =
Gilles Peskine449bd832023-01-11 14:50:10 +0100880 ssl->handshake->certificate_request_context;
Jerry Yuc8d8d4e2022-02-18 12:10:03 +0800881 unsigned char certificate_request_context_len =
Gilles Peskine449bd832023-01-11 14:50:10 +0100882 ssl->handshake->certificate_request_context_len;
Jerry Yuc8d8d4e2022-02-18 12:10:03 +0800883 unsigned char *p_certificate_list_len;
Jerry Yu5cc35062022-01-28 16:16:08 +0800884
Jerry Yu5cc35062022-01-28 16:16:08 +0800885
Jerry Yu3391ac02022-02-16 11:21:37 +0800886 /* ...
887 * opaque certificate_request_context<0..2^8-1>;
888 * ...
889 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100890 MBEDTLS_SSL_CHK_BUF_PTR(p, end, certificate_request_context_len + 1);
Jerry Yuc8d8d4e2022-02-18 12:10:03 +0800891 *p++ = certificate_request_context_len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100892 if (certificate_request_context_len > 0) {
893 memcpy(p, certificate_request_context, certificate_request_context_len);
Jerry Yuc8d8d4e2022-02-18 12:10:03 +0800894 p += certificate_request_context_len;
Jerry Yu537530d2022-02-15 14:00:57 +0800895 }
896
Jerry Yu3391ac02022-02-16 11:21:37 +0800897 /* ...
898 * CertificateEntry certificate_list<0..2^24-1>;
899 * ...
900 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100901 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 3);
Jerry Yuc8d8d4e2022-02-18 12:10:03 +0800902 p_certificate_list_len = p;
Jerry Yu3e536442022-02-15 11:05:59 +0800903 p += 3;
904
Gilles Peskine449bd832023-01-11 14:50:10 +0100905 MBEDTLS_SSL_DEBUG_CRT(3, "own certificate", crt);
Jerry Yu5cc35062022-01-28 16:16:08 +0800906
Gilles Peskine449bd832023-01-11 14:50:10 +0100907 while (crt != NULL) {
Jerry Yu7399d0d2022-01-30 17:54:19 +0800908 size_t cert_data_len = crt->raw.len;
Jerry Yu5cc35062022-01-28 16:16:08 +0800909
Gilles Peskine449bd832023-01-11 14:50:10 +0100910 MBEDTLS_SSL_CHK_BUF_PTR(p, end, cert_data_len + 3 + 2);
911 MBEDTLS_PUT_UINT24_BE(cert_data_len, p, 0);
Jerry Yu7399d0d2022-01-30 17:54:19 +0800912 p += 3;
Jerry Yu5cc35062022-01-28 16:16:08 +0800913
Gilles Peskine449bd832023-01-11 14:50:10 +0100914 memcpy(p, crt->raw.p, cert_data_len);
Jerry Yu7399d0d2022-01-30 17:54:19 +0800915 p += cert_data_len;
916 crt = crt->next;
Jerry Yu5cc35062022-01-28 16:16:08 +0800917
918 /* Currently, we don't have any certificate extensions defined.
919 * Hence, we are sending an empty extension with length zero.
920 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100921 MBEDTLS_PUT_UINT16_BE(0, p, 0);
Jerry Yu7399d0d2022-01-30 17:54:19 +0800922 p += 2;
Jerry Yu5cc35062022-01-28 16:16:08 +0800923 }
Jerry Yu5cc35062022-01-28 16:16:08 +0800924
Gilles Peskine449bd832023-01-11 14:50:10 +0100925 MBEDTLS_PUT_UINT24_BE(p - p_certificate_list_len - 3,
926 p_certificate_list_len, 0);
Jerry Yu7399d0d2022-01-30 17:54:19 +0800927
Jerry Yu3e536442022-02-15 11:05:59 +0800928 *out_len = p - buf;
Jerry Yu5cc35062022-01-28 16:16:08 +0800929
Jerry Yu7de2ff02022-11-08 21:43:46 +0800930 MBEDTLS_SSL_PRINT_EXTS(
Gilles Peskine449bd832023-01-11 14:50:10 +0100931 3, MBEDTLS_SSL_HS_CERTIFICATE, ssl->handshake->sent_extensions);
Jerry Yu4b8f2f72022-10-31 13:31:22 +0800932
Gilles Peskine449bd832023-01-11 14:50:10 +0100933 return 0;
Jerry Yu5cc35062022-01-28 16:16:08 +0800934}
Jerry Yu5cc35062022-01-28 16:16:08 +0800935
Gilles Peskine449bd832023-01-11 14:50:10 +0100936int mbedtls_ssl_tls13_write_certificate(mbedtls_ssl_context *ssl)
Jerry Yu5cc35062022-01-28 16:16:08 +0800937{
938 int ret;
Ronald Cron5bb8fc82022-03-09 07:00:13 +0100939 unsigned char *buf;
940 size_t buf_len, msg_len;
941
Gilles Peskine449bd832023-01-11 14:50:10 +0100942 MBEDTLS_SSL_DEBUG_MSG(2, ("=> write certificate"));
Jerry Yu5cc35062022-01-28 16:16:08 +0800943
Xiaokang Qian73437382023-03-29 08:24:12 +0000944 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_start_handshake_msg(
945 ssl, MBEDTLS_SSL_HS_CERTIFICATE, &buf, &buf_len));
Jerry Yu5cc35062022-01-28 16:16:08 +0800946
Gilles Peskine449bd832023-01-11 14:50:10 +0100947 MBEDTLS_SSL_PROC_CHK(ssl_tls13_write_certificate_body(ssl,
948 buf,
949 buf + buf_len,
950 &msg_len));
Jerry Yu5cc35062022-01-28 16:16:08 +0800951
Xiaokang Qian73437382023-03-29 08:24:12 +0000952 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_add_hs_msg_to_checksum(
953 ssl, MBEDTLS_SSL_HS_CERTIFICATE, buf, msg_len));
Jerry Yu5cc35062022-01-28 16:16:08 +0800954
Gilles Peskine449bd832023-01-11 14:50:10 +0100955 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_finish_handshake_msg(
956 ssl, buf_len, msg_len));
Jerry Yu5cc35062022-01-28 16:16:08 +0800957cleanup:
958
Gilles Peskine449bd832023-01-11 14:50:10 +0100959 MBEDTLS_SSL_DEBUG_MSG(2, ("<= write certificate"));
960 return ret;
Jerry Yu5cc35062022-01-28 16:16:08 +0800961}
962
Jerry Yu3e536442022-02-15 11:05:59 +0800963/*
964 * STATE HANDLING: Output Certificate Verify
965 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100966int mbedtls_ssl_tls13_check_sig_alg_cert_key_match(uint16_t sig_alg,
967 mbedtls_pk_context *key)
Jerry Yu0c6be8f2022-06-20 20:42:00 +0800968{
Gilles Peskine449bd832023-01-11 14:50:10 +0100969 mbedtls_pk_type_t pk_type = mbedtls_ssl_sig_from_pk(key);
970 size_t key_size = mbedtls_pk_get_bitlen(key);
Jerry Yu0c6be8f2022-06-20 20:42:00 +0800971
Gilles Peskine449bd832023-01-11 14:50:10 +0100972 switch (pk_type) {
Jerry Yu67eced02022-02-25 13:37:36 +0800973 case MBEDTLS_SSL_SIG_ECDSA:
Gilles Peskine449bd832023-01-11 14:50:10 +0100974 switch (key_size) {
Jerry Yu67eced02022-02-25 13:37:36 +0800975 case 256:
Gilles Peskine449bd832023-01-11 14:50:10 +0100976 return
977 sig_alg == MBEDTLS_TLS1_3_SIG_ECDSA_SECP256R1_SHA256;
Jerry Yu0c6be8f2022-06-20 20:42:00 +0800978
Jerry Yu67eced02022-02-25 13:37:36 +0800979 case 384:
Gilles Peskine449bd832023-01-11 14:50:10 +0100980 return
981 sig_alg == MBEDTLS_TLS1_3_SIG_ECDSA_SECP384R1_SHA384;
Jerry Yu0c6be8f2022-06-20 20:42:00 +0800982
Jerry Yu67eced02022-02-25 13:37:36 +0800983 case 521:
Gilles Peskine449bd832023-01-11 14:50:10 +0100984 return
985 sig_alg == MBEDTLS_TLS1_3_SIG_ECDSA_SECP521R1_SHA512;
Jerry Yu67eced02022-02-25 13:37:36 +0800986 default:
Jerry Yu67eced02022-02-25 13:37:36 +0800987 break;
988 }
989 break;
Jerry Yu67eced02022-02-25 13:37:36 +0800990
Jerry Yu67eced02022-02-25 13:37:36 +0800991 case MBEDTLS_SSL_SIG_RSA:
Gilles Peskine449bd832023-01-11 14:50:10 +0100992 switch (sig_alg) {
Ronald Cron38391bf2022-09-16 11:19:27 +0200993 case MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA256: /* Intentional fallthrough */
994 case MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA384: /* Intentional fallthrough */
Jerry Yu0c6be8f2022-06-20 20:42:00 +0800995 case MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA512:
Gilles Peskine449bd832023-01-11 14:50:10 +0100996 return 1;
Jerry Yuc2e04932022-06-27 22:13:03 +0800997
Jerry Yu0c6be8f2022-06-20 20:42:00 +0800998 default:
999 break;
Jerry Yucef3f332022-03-22 23:00:13 +08001000 }
Jerry Yu67eced02022-02-25 13:37:36 +08001001 break;
Jerry Yu0c6be8f2022-06-20 20:42:00 +08001002
Jerry Yu67eced02022-02-25 13:37:36 +08001003 default:
Jerry Yu67eced02022-02-25 13:37:36 +08001004 break;
1005 }
Jerry Yu0c6be8f2022-06-20 20:42:00 +08001006
Gilles Peskine449bd832023-01-11 14:50:10 +01001007 return 0;
Jerry Yu0c6be8f2022-06-20 20:42:00 +08001008}
1009
Ronald Cronce7d76e2022-07-08 18:56:49 +02001010MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01001011static int ssl_tls13_write_certificate_verify_body(mbedtls_ssl_context *ssl,
1012 unsigned char *buf,
1013 unsigned char *end,
1014 size_t *out_len)
Jerry Yu8511f122022-01-29 10:01:04 +08001015{
Ronald Cron067a1e72022-09-16 13:44:49 +02001016 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yu3e536442022-02-15 11:05:59 +08001017 unsigned char *p = buf;
Jerry Yu8511f122022-01-29 10:01:04 +08001018 mbedtls_pk_context *own_key;
Jerry Yu3e536442022-02-15 11:05:59 +08001019
Gilles Peskine449bd832023-01-11 14:50:10 +01001020 unsigned char handshake_hash[MBEDTLS_TLS1_3_MD_MAX_SIZE];
Jerry Yu8511f122022-01-29 10:01:04 +08001021 size_t handshake_hash_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01001022 unsigned char verify_buffer[SSL_VERIFY_STRUCT_MAX_SIZE];
Jerry Yu3e536442022-02-15 11:05:59 +08001023 size_t verify_buffer_len;
Ronald Cron067a1e72022-09-16 13:44:49 +02001024
1025 uint16_t *sig_alg = ssl->handshake->received_sig_algs;
Jerry Yu3e536442022-02-15 11:05:59 +08001026 size_t signature_len = 0;
Jerry Yu8511f122022-01-29 10:01:04 +08001027
Jerry Yu0b7b1012022-02-23 12:23:05 +08001028 *out_len = 0;
1029
Gilles Peskine449bd832023-01-11 14:50:10 +01001030 own_key = mbedtls_ssl_own_key(ssl);
1031 if (own_key == NULL) {
1032 MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
1033 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Jerry Yu8511f122022-01-29 10:01:04 +08001034 }
1035
Xiaokang Qian73437382023-03-29 08:24:12 +00001036 ret = mbedtls_ssl_get_handshake_transcript(
1037 ssl, ssl->handshake->ciphersuite_info->mac,
1038 handshake_hash, sizeof(handshake_hash), &handshake_hash_len);
Gilles Peskine449bd832023-01-11 14:50:10 +01001039 if (ret != 0) {
1040 return ret;
1041 }
Jerry Yu8511f122022-01-29 10:01:04 +08001042
Gilles Peskine449bd832023-01-11 14:50:10 +01001043 MBEDTLS_SSL_DEBUG_BUF(3, "handshake hash",
1044 handshake_hash,
1045 handshake_hash_len);
Jerry Yu8511f122022-01-29 10:01:04 +08001046
Gilles Peskine449bd832023-01-11 14:50:10 +01001047 ssl_tls13_create_verify_structure(handshake_hash, handshake_hash_len,
1048 verify_buffer, &verify_buffer_len,
1049 ssl->conf->endpoint);
Jerry Yu8511f122022-01-29 10:01:04 +08001050
1051 /*
1052 * struct {
1053 * SignatureScheme algorithm;
1054 * opaque signature<0..2^16-1>;
1055 * } CertificateVerify;
1056 */
Ronald Cron067a1e72022-09-16 13:44:49 +02001057 /* Check there is space for the algorithm identifier (2 bytes) and the
1058 * signature length (2 bytes).
1059 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001060 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 4);
Ronald Cron067a1e72022-09-16 13:44:49 +02001061
Gilles Peskine449bd832023-01-11 14:50:10 +01001062 for (; *sig_alg != MBEDTLS_TLS1_3_SIG_NONE; sig_alg++) {
Ronald Cron067a1e72022-09-16 13:44:49 +02001063 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
1064 mbedtls_pk_type_t pk_type = MBEDTLS_PK_NONE;
1065 mbedtls_md_type_t md_alg = MBEDTLS_MD_NONE;
1066 psa_algorithm_t psa_algorithm = PSA_ALG_NONE;
1067 unsigned char verify_hash[PSA_HASH_MAX_SIZE];
1068 size_t verify_hash_len;
Jerry Yu67eced02022-02-25 13:37:36 +08001069
Gilles Peskine449bd832023-01-11 14:50:10 +01001070 if (!mbedtls_ssl_sig_alg_is_offered(ssl, *sig_alg)) {
Ronald Cron067a1e72022-09-16 13:44:49 +02001071 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +01001072 }
Jerry Yu67eced02022-02-25 13:37:36 +08001073
Gilles Peskine449bd832023-01-11 14:50:10 +01001074 if (!mbedtls_ssl_tls13_sig_alg_for_cert_verify_is_supported(*sig_alg)) {
Ronald Cron067a1e72022-09-16 13:44:49 +02001075 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +01001076 }
Ronald Cron067a1e72022-09-16 13:44:49 +02001077
Gilles Peskine449bd832023-01-11 14:50:10 +01001078 if (!mbedtls_ssl_tls13_check_sig_alg_cert_key_match(*sig_alg, own_key)) {
Ronald Cron067a1e72022-09-16 13:44:49 +02001079 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +01001080 }
Ronald Cron067a1e72022-09-16 13:44:49 +02001081
Gilles Peskine449bd832023-01-11 14:50:10 +01001082 if (mbedtls_ssl_get_pk_type_and_md_alg_from_sig_alg(
1083 *sig_alg, &pk_type, &md_alg) != 0) {
1084 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Ronald Cron067a1e72022-09-16 13:44:49 +02001085 }
1086
1087 /* Hash verify buffer with indicated hash function */
Manuel Pégourié-Gonnard2d6d9932023-03-28 11:38:08 +02001088 psa_algorithm = mbedtls_md_psa_alg_from_type(md_alg);
Gilles Peskine449bd832023-01-11 14:50:10 +01001089 status = psa_hash_compute(psa_algorithm,
1090 verify_buffer,
1091 verify_buffer_len,
1092 verify_hash, sizeof(verify_hash),
1093 &verify_hash_len);
1094 if (status != PSA_SUCCESS) {
Andrzej Kurek8a045ce2022-12-23 11:00:06 -05001095 return PSA_TO_MBEDTLS_ERR(status);
Ronald Cron067a1e72022-09-16 13:44:49 +02001096 }
1097
Gilles Peskine449bd832023-01-11 14:50:10 +01001098 MBEDTLS_SSL_DEBUG_BUF(3, "verify hash", verify_hash, verify_hash_len);
1099
1100 if ((ret = mbedtls_pk_sign_ext(pk_type, own_key,
1101 md_alg, verify_hash, verify_hash_len,
1102 p + 4, (size_t) (end - (p + 4)), &signature_len,
1103 ssl->conf->f_rng, ssl->conf->p_rng)) != 0) {
1104 MBEDTLS_SSL_DEBUG_MSG(2, ("CertificateVerify signature failed with %s",
1105 mbedtls_ssl_sig_alg_to_str(*sig_alg)));
1106 MBEDTLS_SSL_DEBUG_RET(2, "mbedtls_pk_sign_ext", ret);
1107
1108 /* The signature failed. This is possible if the private key
1109 * was not suitable for the signature operation as purposely we
1110 * did not check its suitability completely. Let's try with
1111 * another signature algorithm.
1112 */
1113 continue;
1114 }
1115
1116 MBEDTLS_SSL_DEBUG_MSG(2, ("CertificateVerify signature with %s",
1117 mbedtls_ssl_sig_alg_to_str(*sig_alg)));
Ronald Cron067a1e72022-09-16 13:44:49 +02001118
1119 break;
1120 }
1121
Gilles Peskine449bd832023-01-11 14:50:10 +01001122 if (*sig_alg == MBEDTLS_TLS1_3_SIG_NONE) {
1123 MBEDTLS_SSL_DEBUG_MSG(1, ("no suitable signature algorithm"));
1124 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE,
1125 MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE);
1126 return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
Jerry Yu8511f122022-01-29 10:01:04 +08001127 }
1128
Gilles Peskine449bd832023-01-11 14:50:10 +01001129 MBEDTLS_PUT_UINT16_BE(*sig_alg, p, 0);
1130 MBEDTLS_PUT_UINT16_BE(signature_len, p, 2);
Jerry Yuf3b46b52022-06-19 16:52:27 +08001131
Ronald Cron067a1e72022-09-16 13:44:49 +02001132 *out_len = 4 + signature_len;
Jerry Yu8c338862022-03-23 13:34:04 +08001133
Gilles Peskine449bd832023-01-11 14:50:10 +01001134 return 0;
Jerry Yu8511f122022-01-29 10:01:04 +08001135}
Jerry Yu8511f122022-01-29 10:01:04 +08001136
Gilles Peskine449bd832023-01-11 14:50:10 +01001137int mbedtls_ssl_tls13_write_certificate_verify(mbedtls_ssl_context *ssl)
Jerry Yu8511f122022-01-29 10:01:04 +08001138{
1139 int ret = 0;
Jerry Yuca133a32022-02-15 14:22:05 +08001140 unsigned char *buf;
1141 size_t buf_len, msg_len;
1142
Gilles Peskine449bd832023-01-11 14:50:10 +01001143 MBEDTLS_SSL_DEBUG_MSG(2, ("=> write certificate verify"));
Jerry Yu8511f122022-01-29 10:01:04 +08001144
Xiaokang Qian73437382023-03-29 08:24:12 +00001145 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_start_handshake_msg(
1146 ssl, MBEDTLS_SSL_HS_CERTIFICATE_VERIFY,
1147 &buf, &buf_len));
Jerry Yu8511f122022-01-29 10:01:04 +08001148
Gilles Peskine449bd832023-01-11 14:50:10 +01001149 MBEDTLS_SSL_PROC_CHK(ssl_tls13_write_certificate_verify_body(
1150 ssl, buf, buf + buf_len, &msg_len));
Jerry Yu8511f122022-01-29 10:01:04 +08001151
Xiaokang Qian73437382023-03-29 08:24:12 +00001152 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_add_hs_msg_to_checksum(
1153 ssl, MBEDTLS_SSL_HS_CERTIFICATE_VERIFY,
1154 buf, msg_len));
Jerry Yu8511f122022-01-29 10:01:04 +08001155
Gilles Peskine449bd832023-01-11 14:50:10 +01001156 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_finish_handshake_msg(
1157 ssl, buf_len, msg_len));
Jerry Yu8511f122022-01-29 10:01:04 +08001158
1159cleanup:
1160
Gilles Peskine449bd832023-01-11 14:50:10 +01001161 MBEDTLS_SSL_DEBUG_MSG(2, ("<= write certificate verify"));
1162 return ret;
Jerry Yu8511f122022-01-29 10:01:04 +08001163}
1164
Ronald Cron928cbd32022-10-04 16:14:26 +02001165#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED */
Jerry Yu90f152d2022-01-29 22:12:42 +08001166
Jerry Yu5cc35062022-01-28 16:16:08 +08001167/*
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001168 *
XiaokangQianc5c39d52021-11-09 11:55:10 +00001169 * STATE HANDLING: Incoming Finished message.
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001170 */
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001171/*
1172 * Implementation
1173 */
1174
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001175MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01001176static int ssl_tls13_preprocess_finished_message(mbedtls_ssl_context *ssl)
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001177{
1178 int ret;
1179
Xiaokang Qian73437382023-03-29 08:24:12 +00001180 ret = mbedtls_ssl_tls13_calculate_verify_data(
1181 ssl,
1182 ssl->handshake->state_local.finished_in.digest,
1183 sizeof(ssl->handshake->state_local.finished_in.digest),
1184 &ssl->handshake->state_local.finished_in.digest_len,
1185 ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT ?
1186 MBEDTLS_SSL_IS_SERVER : MBEDTLS_SSL_IS_CLIENT);
Gilles Peskine449bd832023-01-11 14:50:10 +01001187 if (ret != 0) {
1188 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_tls13_calculate_verify_data", ret);
1189 return ret;
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001190 }
1191
Gilles Peskine449bd832023-01-11 14:50:10 +01001192 return 0;
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001193}
1194
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001195MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01001196static int ssl_tls13_parse_finished_message(mbedtls_ssl_context *ssl,
1197 const unsigned char *buf,
1198 const unsigned char *end)
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001199{
XiaokangQian33062842021-11-11 03:37:45 +00001200 /*
1201 * struct {
XiaokangQianc13f9352021-11-11 06:13:22 +00001202 * opaque verify_data[Hash.length];
XiaokangQian33062842021-11-11 03:37:45 +00001203 * } Finished;
1204 */
1205 const unsigned char *expected_verify_data =
1206 ssl->handshake->state_local.finished_in.digest;
1207 size_t expected_verify_data_len =
1208 ssl->handshake->state_local.finished_in.digest_len;
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001209 /* Structural validation */
Gilles Peskine449bd832023-01-11 14:50:10 +01001210 if ((size_t) (end - buf) != expected_verify_data_len) {
1211 MBEDTLS_SSL_DEBUG_MSG(1, ("bad finished message"));
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001212
Gilles Peskine449bd832023-01-11 14:50:10 +01001213 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR,
1214 MBEDTLS_ERR_SSL_DECODE_ERROR);
1215 return MBEDTLS_ERR_SSL_DECODE_ERROR;
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001216 }
1217
Gilles Peskine449bd832023-01-11 14:50:10 +01001218 MBEDTLS_SSL_DEBUG_BUF(4, "verify_data (self-computed):",
1219 expected_verify_data,
1220 expected_verify_data_len);
1221 MBEDTLS_SSL_DEBUG_BUF(4, "verify_data (received message):", buf,
1222 expected_verify_data_len);
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001223
1224 /* Semantic validation */
Gilles Peskine449bd832023-01-11 14:50:10 +01001225 if (mbedtls_ct_memcmp(buf,
1226 expected_verify_data,
1227 expected_verify_data_len) != 0) {
1228 MBEDTLS_SSL_DEBUG_MSG(1, ("bad finished message"));
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001229
Gilles Peskine449bd832023-01-11 14:50:10 +01001230 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_DECRYPT_ERROR,
1231 MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE);
1232 return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001233 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001234 return 0;
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001235}
1236
Gilles Peskine449bd832023-01-11 14:50:10 +01001237int mbedtls_ssl_tls13_process_finished_message(mbedtls_ssl_context *ssl)
XiaokangQianc5c39d52021-11-09 11:55:10 +00001238{
XiaokangQian33062842021-11-11 03:37:45 +00001239 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
XiaokangQianc5c39d52021-11-09 11:55:10 +00001240 unsigned char *buf;
Xiaofei Baieef15042021-11-18 07:29:56 +00001241 size_t buf_len;
XiaokangQianc5c39d52021-11-09 11:55:10 +00001242
Gilles Peskine449bd832023-01-11 14:50:10 +01001243 MBEDTLS_SSL_DEBUG_MSG(2, ("=> parse finished message"));
XiaokangQianc5c39d52021-11-09 11:55:10 +00001244
Xiaokang Qian73437382023-03-29 08:24:12 +00001245 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_tls13_fetch_handshake_msg(
1246 ssl, MBEDTLS_SSL_HS_FINISHED, &buf, &buf_len));
Jerry Yu0a92d6c2022-05-16 16:54:46 +08001247
1248 /* Preprocessing step: Compute handshake digest */
Gilles Peskine449bd832023-01-11 14:50:10 +01001249 MBEDTLS_SSL_PROC_CHK(ssl_tls13_preprocess_finished_message(ssl));
Jerry Yu0a92d6c2022-05-16 16:54:46 +08001250
Xiaokang Qian73437382023-03-29 08:24:12 +00001251 MBEDTLS_SSL_PROC_CHK(ssl_tls13_parse_finished_message(
1252 ssl, buf, buf + buf_len));
Jerry Yu0a92d6c2022-05-16 16:54:46 +08001253
Xiaokang Qian73437382023-03-29 08:24:12 +00001254 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_add_hs_msg_to_checksum(
1255 ssl, MBEDTLS_SSL_HS_FINISHED, buf, buf_len));
XiaokangQianc5c39d52021-11-09 11:55:10 +00001256
1257cleanup:
1258
Gilles Peskine449bd832023-01-11 14:50:10 +01001259 MBEDTLS_SSL_DEBUG_MSG(2, ("<= parse finished message"));
1260 return ret;
XiaokangQianc5c39d52021-11-09 11:55:10 +00001261}
1262
XiaokangQian74af2a82021-09-22 07:40:30 +00001263/*
1264 *
XiaokangQiancc90c942021-11-09 12:30:09 +00001265 * STATE HANDLING: Write and send Finished message.
XiaokangQian74af2a82021-09-22 07:40:30 +00001266 *
1267 */
XiaokangQian74af2a82021-09-22 07:40:30 +00001268/*
XiaokangQian35dc6252021-11-11 08:16:19 +00001269 * Implement
XiaokangQian74af2a82021-09-22 07:40:30 +00001270 */
1271
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001272MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01001273static int ssl_tls13_prepare_finished_message(mbedtls_ssl_context *ssl)
XiaokangQian74af2a82021-09-22 07:40:30 +00001274{
1275 int ret;
1276
1277 /* Compute transcript of handshake up to now. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001278 ret = mbedtls_ssl_tls13_calculate_verify_data(ssl,
1279 ssl->handshake->state_local.finished_out.digest,
1280 sizeof(ssl->handshake->state_local.finished_out.
1281 digest),
1282 &ssl->handshake->state_local.finished_out.digest_len,
1283 ssl->conf->endpoint);
XiaokangQian74af2a82021-09-22 07:40:30 +00001284
Gilles Peskine449bd832023-01-11 14:50:10 +01001285 if (ret != 0) {
1286 MBEDTLS_SSL_DEBUG_RET(1, "calculate_verify_data failed", ret);
1287 return ret;
XiaokangQian74af2a82021-09-22 07:40:30 +00001288 }
1289
Gilles Peskine449bd832023-01-11 14:50:10 +01001290 return 0;
XiaokangQian74af2a82021-09-22 07:40:30 +00001291}
1292
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001293MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01001294static int ssl_tls13_write_finished_message_body(mbedtls_ssl_context *ssl,
1295 unsigned char *buf,
1296 unsigned char *end,
1297 size_t *out_len)
XiaokangQian74af2a82021-09-22 07:40:30 +00001298{
XiaokangQian8773aa02021-11-10 07:33:09 +00001299 size_t verify_data_len = ssl->handshake->state_local.finished_out.digest_len;
XiaokangQian0fa66432021-11-15 03:33:57 +00001300 /*
1301 * struct {
1302 * opaque verify_data[Hash.length];
1303 * } Finished;
1304 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001305 MBEDTLS_SSL_CHK_BUF_PTR(buf, end, verify_data_len);
XiaokangQian74af2a82021-09-22 07:40:30 +00001306
Gilles Peskine449bd832023-01-11 14:50:10 +01001307 memcpy(buf, ssl->handshake->state_local.finished_out.digest,
1308 verify_data_len);
XiaokangQian74af2a82021-09-22 07:40:30 +00001309
Xiaofei Baid25fab62021-12-02 06:36:27 +00001310 *out_len = verify_data_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01001311 return 0;
XiaokangQian74af2a82021-09-22 07:40:30 +00001312}
XiaokangQianc5c39d52021-11-09 11:55:10 +00001313
XiaokangQian35dc6252021-11-11 08:16:19 +00001314/* Main entry point: orchestrates the other functions */
Gilles Peskine449bd832023-01-11 14:50:10 +01001315int mbedtls_ssl_tls13_write_finished_message(mbedtls_ssl_context *ssl)
XiaokangQian35dc6252021-11-11 08:16:19 +00001316{
1317 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1318 unsigned char *buf;
1319 size_t buf_len, msg_len;
1320
Gilles Peskine449bd832023-01-11 14:50:10 +01001321 MBEDTLS_SSL_DEBUG_MSG(2, ("=> write finished message"));
XiaokangQian35dc6252021-11-11 08:16:19 +00001322
Gilles Peskine449bd832023-01-11 14:50:10 +01001323 MBEDTLS_SSL_PROC_CHK(ssl_tls13_prepare_finished_message(ssl));
XiaokangQiandce82242021-11-15 06:01:26 +00001324
Gilles Peskine449bd832023-01-11 14:50:10 +01001325 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_start_handshake_msg(ssl,
1326 MBEDTLS_SSL_HS_FINISHED, &buf, &buf_len));
XiaokangQian35dc6252021-11-11 08:16:19 +00001327
Gilles Peskine449bd832023-01-11 14:50:10 +01001328 MBEDTLS_SSL_PROC_CHK(ssl_tls13_write_finished_message_body(
1329 ssl, buf, buf + buf_len, &msg_len));
XiaokangQian35dc6252021-11-11 08:16:19 +00001330
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +01001331 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_add_hs_msg_to_checksum(ssl,
Manuel Pégourié-Gonnard43cc1272023-02-06 11:48:19 +01001332 MBEDTLS_SSL_HS_FINISHED, buf, msg_len));
XiaokangQian35dc6252021-11-11 08:16:19 +00001333
Gilles Peskine449bd832023-01-11 14:50:10 +01001334 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_finish_handshake_msg(
1335 ssl, buf_len, msg_len));
XiaokangQian35dc6252021-11-11 08:16:19 +00001336cleanup:
1337
Gilles Peskine449bd832023-01-11 14:50:10 +01001338 MBEDTLS_SSL_DEBUG_MSG(2, ("<= write finished message"));
1339 return ret;
XiaokangQian35dc6252021-11-11 08:16:19 +00001340}
1341
Gilles Peskine449bd832023-01-11 14:50:10 +01001342void mbedtls_ssl_tls13_handshake_wrapup(mbedtls_ssl_context *ssl)
Jerry Yu378254d2021-10-30 21:44:47 +08001343{
1344
Gilles Peskine449bd832023-01-11 14:50:10 +01001345 MBEDTLS_SSL_DEBUG_MSG(3, ("=> handshake wrapup"));
Jerry Yu378254d2021-10-30 21:44:47 +08001346
Gilles Peskine449bd832023-01-11 14:50:10 +01001347 MBEDTLS_SSL_DEBUG_MSG(1, ("Switch to application keys for inbound traffic"));
1348 mbedtls_ssl_set_inbound_transform(ssl, ssl->transform_application);
Jerry Yue8c1fca2022-05-18 14:48:56 +08001349
Gilles Peskine449bd832023-01-11 14:50:10 +01001350 MBEDTLS_SSL_DEBUG_MSG(1, ("Switch to application keys for outbound traffic"));
1351 mbedtls_ssl_set_outbound_transform(ssl, ssl->transform_application);
Jerry Yue8c1fca2022-05-18 14:48:56 +08001352
Jerry Yu378254d2021-10-30 21:44:47 +08001353 /*
Jerry Yucfe64f02021-11-15 13:54:06 +08001354 * Free the previous session and switch to the current one.
Jerry Yu378254d2021-10-30 21:44:47 +08001355 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001356 if (ssl->session) {
1357 mbedtls_ssl_session_free(ssl->session);
1358 mbedtls_free(ssl->session);
Jerry Yu378254d2021-10-30 21:44:47 +08001359 }
1360 ssl->session = ssl->session_negotiate;
1361 ssl->session_negotiate = NULL;
1362
Gilles Peskine449bd832023-01-11 14:50:10 +01001363 MBEDTLS_SSL_DEBUG_MSG(3, ("<= handshake wrapup"));
Jerry Yu378254d2021-10-30 21:44:47 +08001364}
1365
Ronald Cron49ad6192021-11-24 16:25:31 +01001366/*
1367 *
1368 * STATE HANDLING: Write ChangeCipherSpec
1369 *
1370 */
1371#if defined(MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001372MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01001373static int ssl_tls13_write_change_cipher_spec_body(mbedtls_ssl_context *ssl,
1374 unsigned char *buf,
1375 unsigned char *end,
1376 size_t *olen)
Ronald Cron49ad6192021-11-24 16:25:31 +01001377{
1378 ((void) ssl);
1379
Gilles Peskine449bd832023-01-11 14:50:10 +01001380 MBEDTLS_SSL_CHK_BUF_PTR(buf, end, 1);
Ronald Cron49ad6192021-11-24 16:25:31 +01001381 buf[0] = 1;
1382 *olen = 1;
1383
Gilles Peskine449bd832023-01-11 14:50:10 +01001384 return 0;
Ronald Cron49ad6192021-11-24 16:25:31 +01001385}
1386
Gilles Peskine449bd832023-01-11 14:50:10 +01001387int mbedtls_ssl_tls13_write_change_cipher_spec(mbedtls_ssl_context *ssl)
Ronald Cron49ad6192021-11-24 16:25:31 +01001388{
1389 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1390
Gilles Peskine449bd832023-01-11 14:50:10 +01001391 MBEDTLS_SSL_DEBUG_MSG(2, ("=> write change cipher spec"));
Ronald Cron49ad6192021-11-24 16:25:31 +01001392
Ronald Cron49ad6192021-11-24 16:25:31 +01001393 /* Write CCS message */
Gilles Peskine449bd832023-01-11 14:50:10 +01001394 MBEDTLS_SSL_PROC_CHK(ssl_tls13_write_change_cipher_spec_body(
1395 ssl, ssl->out_msg,
1396 ssl->out_msg + MBEDTLS_SSL_OUT_CONTENT_LEN,
1397 &ssl->out_msglen));
Ronald Cron49ad6192021-11-24 16:25:31 +01001398
1399 ssl->out_msgtype = MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC;
1400
Ronald Cron49ad6192021-11-24 16:25:31 +01001401 /* Dispatch message */
Gilles Peskine449bd832023-01-11 14:50:10 +01001402 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_write_record(ssl, 0));
Ronald Cron49ad6192021-11-24 16:25:31 +01001403
1404cleanup:
1405
Gilles Peskine449bd832023-01-11 14:50:10 +01001406 MBEDTLS_SSL_DEBUG_MSG(2, ("<= write change cipher spec"));
1407 return ret;
Ronald Cron49ad6192021-11-24 16:25:31 +01001408}
1409
1410#endif /* MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE */
1411
Xiaokang Qianecc29482022-11-02 07:52:47 +00001412/* Early Data Indication Extension
Xiaokang Qian0e97d4d2022-10-24 11:12:51 +00001413 *
Xiaokang Qian0e97d4d2022-10-24 11:12:51 +00001414 * struct {
1415 * select ( Handshake.msg_type ) {
Xiaokang Qianecc29482022-11-02 07:52:47 +00001416 * ...
Xiaokang Qian0e97d4d2022-10-24 11:12:51 +00001417 * case client_hello: Empty;
1418 * case encrypted_extensions: Empty;
1419 * };
1420 * } EarlyDataIndication;
1421 */
1422#if defined(MBEDTLS_SSL_EARLY_DATA)
Gilles Peskine449bd832023-01-11 14:50:10 +01001423int mbedtls_ssl_tls13_write_early_data_ext(mbedtls_ssl_context *ssl,
1424 unsigned char *buf,
1425 const unsigned char *end,
1426 size_t *out_len)
Xiaokang Qian0e97d4d2022-10-24 11:12:51 +00001427{
1428 unsigned char *p = buf;
1429 *out_len = 0;
1430 ((void) ssl);
1431
Gilles Peskine449bd832023-01-11 14:50:10 +01001432 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 4);
Xiaokang Qian0e97d4d2022-10-24 11:12:51 +00001433
Gilles Peskine449bd832023-01-11 14:50:10 +01001434 MBEDTLS_PUT_UINT16_BE(MBEDTLS_TLS_EXT_EARLY_DATA, p, 0);
1435 MBEDTLS_PUT_UINT16_BE(0, p, 2);
Xiaokang Qian0e97d4d2022-10-24 11:12:51 +00001436
1437 *out_len = 4;
Xiaokang Qian2cd5ce02022-11-15 10:33:53 +00001438
Gilles Peskine449bd832023-01-11 14:50:10 +01001439 mbedtls_ssl_tls13_set_hs_sent_ext_mask(ssl, MBEDTLS_TLS_EXT_EARLY_DATA);
Xiaokang Qian2cd5ce02022-11-15 10:33:53 +00001440
Gilles Peskine449bd832023-01-11 14:50:10 +01001441 return 0;
Xiaokang Qian0e97d4d2022-10-24 11:12:51 +00001442}
1443#endif /* MBEDTLS_SSL_EARLY_DATA */
1444
XiaokangQian78b1fa72022-01-19 06:56:30 +00001445/* Reset SSL context and update hash for handling HRR.
1446 *
1447 * Replace Transcript-Hash(X) by
1448 * Transcript-Hash( message_hash ||
1449 * 00 00 Hash.length ||
1450 * X )
1451 * A few states of the handshake are preserved, including:
1452 * - session ID
1453 * - session ticket
1454 * - negotiated ciphersuite
1455 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001456int mbedtls_ssl_reset_transcript_for_hrr(mbedtls_ssl_context *ssl)
XiaokangQian78b1fa72022-01-19 06:56:30 +00001457{
1458 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Przemyslaw Stekielda645252022-09-14 12:50:51 +02001459 unsigned char hash_transcript[PSA_HASH_MAX_SIZE + 4];
XiaokangQian0ece9982022-01-24 08:56:23 +00001460 size_t hash_len;
Xiaokang Qian6b980012023-02-07 03:17:45 +00001461 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
1462 ssl->handshake->ciphersuite_info;
XiaokangQian78b1fa72022-01-19 06:56:30 +00001463
Gilles Peskine449bd832023-01-11 14:50:10 +01001464 MBEDTLS_SSL_DEBUG_MSG(3, ("Reset SSL session for HRR"));
XiaokangQian78b1fa72022-01-19 06:56:30 +00001465
Gilles Peskine449bd832023-01-11 14:50:10 +01001466 ret = mbedtls_ssl_get_handshake_transcript(ssl, ciphersuite_info->mac,
1467 hash_transcript + 4,
1468 PSA_HASH_MAX_SIZE,
1469 &hash_len);
1470 if (ret != 0) {
Manuel Pégourié-Gonnardda7979b2023-02-21 09:31:10 +01001471 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_get_handshake_transcript", ret);
Gilles Peskine449bd832023-01-11 14:50:10 +01001472 return ret;
XiaokangQian0ece9982022-01-24 08:56:23 +00001473 }
1474
1475 hash_transcript[0] = MBEDTLS_SSL_HS_MESSAGE_HASH;
1476 hash_transcript[1] = 0;
1477 hash_transcript[2] = 0;
1478 hash_transcript[3] = (unsigned char) hash_len;
1479
1480 hash_len += 4;
1481
Manuel Pégourié-Gonnardda7979b2023-02-21 09:31:10 +01001482 MBEDTLS_SSL_DEBUG_BUF(4, "Truncated handshake transcript",
1483 hash_transcript, hash_len);
1484
Manuel Pégourié-Gonnardd7a7a232023-02-05 10:26:49 +01001485 /* Reset running hash and replace it with a hash of the transcript */
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +01001486 ret = mbedtls_ssl_reset_checksum(ssl);
1487 if (ret != 0) {
1488 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_reset_checksum", ret);
1489 return ret;
1490 }
1491 ret = ssl->handshake->update_checksum(ssl, hash_transcript, hash_len);
1492 if (ret != 0) {
1493 MBEDTLS_SSL_DEBUG_RET(1, "update_checksum", ret);
1494 return ret;
1495 }
Przemyslaw Stekiel4b3fff42022-02-14 16:39:52 +01001496
Gilles Peskine449bd832023-01-11 14:50:10 +01001497 return ret;
XiaokangQian78b1fa72022-01-19 06:56:30 +00001498}
1499
Przemek Stekiel63706622023-05-23 16:31:56 +02001500#if defined(PSA_WANT_ALG_ECDH) || defined(PSA_WANT_ALG_FFDH)
XiaokangQian7807f9f2022-02-15 10:04:37 +00001501
Przemek Stekield5f79e72023-06-29 09:08:43 +02001502int mbedtls_ssl_tls13_read_public_dhe_share(mbedtls_ssl_context *ssl,
Gilles Peskine449bd832023-01-11 14:50:10 +01001503 const unsigned char *buf,
1504 size_t buf_len)
XiaokangQian7807f9f2022-02-15 10:04:37 +00001505{
Gilles Peskine449bd832023-01-11 14:50:10 +01001506 uint8_t *p = (uint8_t *) buf;
XiaokangQiancfd925f2022-04-14 07:10:37 +00001507 const uint8_t *end = buf + buf_len;
XiaokangQian9b5d04b2022-04-10 10:20:43 +00001508 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001509
XiaokangQian9b5d04b2022-04-10 10:20:43 +00001510 /* Get size of the TLS opaque key_exchange field of the KeyShareEntry struct. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001511 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, 2);
1512 uint16_t peerkey_len = MBEDTLS_GET_UINT16_BE(p, 0);
XiaokangQian9b5d04b2022-04-10 10:20:43 +00001513 p += 2;
XiaokangQian3207a322022-02-23 03:15:27 +00001514
XiaokangQian9b5d04b2022-04-10 10:20:43 +00001515 /* Check if key size is consistent with given buffer length. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001516 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, peerkey_len);
XiaokangQian9b5d04b2022-04-10 10:20:43 +00001517
1518 /* Store peer's ECDH public key. */
Przemek Stekiel6f199852023-06-29 08:59:26 +02001519 memcpy(handshake->dh_psa_peerkey, p, peerkey_len);
1520 handshake->dh_psa_peerkey_len = peerkey_len;
XiaokangQian9b5d04b2022-04-10 10:20:43 +00001521
Gilles Peskine449bd832023-01-11 14:50:10 +01001522 return 0;
XiaokangQian3207a322022-02-23 03:15:27 +00001523}
Jerry Yu89e103c2022-03-30 22:43:29 +08001524
Przemek Stekielda4fba62023-06-02 14:52:28 +02001525static psa_status_t mbedtls_ssl_get_psa_ffdh_info_from_tls_id(
Przemek Stekiel75a5a9c2023-06-12 11:21:18 +02001526 uint16_t tls_id, size_t *bits, psa_key_type_t *key_type)
Przemek Stekiel29c219c2023-05-31 15:21:04 +02001527{
Przemek Stekiel75a5a9c2023-06-12 11:21:18 +02001528 switch (tls_id) {
Przemek Stekiel29c219c2023-05-31 15:21:04 +02001529 case MBEDTLS_SSL_IANA_TLS_GROUP_FFDHE2048:
1530 *bits = 2048;
1531 *key_type = PSA_KEY_TYPE_DH_KEY_PAIR(PSA_DH_FAMILY_RFC7919);
1532 return PSA_SUCCESS;
1533 case MBEDTLS_SSL_IANA_TLS_GROUP_FFDHE3072:
1534 *bits = 3072;
1535 *key_type = PSA_KEY_TYPE_DH_KEY_PAIR(PSA_DH_FAMILY_RFC7919);
1536 return PSA_SUCCESS;
1537 case MBEDTLS_SSL_IANA_TLS_GROUP_FFDHE4096:
1538 *bits = 4096;
1539 *key_type = PSA_KEY_TYPE_DH_KEY_PAIR(PSA_DH_FAMILY_RFC7919);
1540 return PSA_SUCCESS;
1541 case MBEDTLS_SSL_IANA_TLS_GROUP_FFDHE6144:
1542 *bits = 6144;
1543 *key_type = PSA_KEY_TYPE_DH_KEY_PAIR(PSA_DH_FAMILY_RFC7919);
1544 return PSA_SUCCESS;
1545 case MBEDTLS_SSL_IANA_TLS_GROUP_FFDHE8192:
1546 *bits = 8192;
1547 *key_type = PSA_KEY_TYPE_DH_KEY_PAIR(PSA_DH_FAMILY_RFC7919);
1548 return PSA_SUCCESS;
1549 default:
1550 return PSA_ERROR_NOT_SUPPORTED;
1551 }
1552}
1553
1554int mbedtls_ssl_tls13_generate_and_write_dh_key_exchange(
Gilles Peskine449bd832023-01-11 14:50:10 +01001555 mbedtls_ssl_context *ssl,
1556 uint16_t named_group,
1557 unsigned char *buf,
1558 unsigned char *end,
1559 size_t *out_len)
Jerry Yu89e103c2022-03-30 22:43:29 +08001560{
1561 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
1562 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
1563 psa_key_attributes_t key_attributes;
1564 size_t own_pubkey_len;
1565 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
Przemek Stekiel29c219c2023-05-31 15:21:04 +02001566 size_t bits = 0;
Przemek Stekiel75a5a9c2023-06-12 11:21:18 +02001567 psa_key_type_t key_type = PSA_KEY_TYPE_NONE;
1568 psa_algorithm_t alg = PSA_ALG_NONE;
Przemek Stekiel29c219c2023-05-31 15:21:04 +02001569 size_t buf_size = (size_t) (end - buf);
Jerry Yu89e103c2022-03-30 22:43:29 +08001570
Przemek Stekiel29c219c2023-05-31 15:21:04 +02001571 MBEDTLS_SSL_DEBUG_MSG(1, ("Perform PSA-based ECDH/FFDH computation."));
Jerry Yu89e103c2022-03-30 22:43:29 +08001572
Valerio Setti40d9ca92023-01-04 16:08:04 +01001573 /* Convert EC's TLS ID to PSA key type. */
Przemek Stekiel29c219c2023-05-31 15:21:04 +02001574#if defined(PSA_WANT_ALG_ECDH)
Xiaokang Qian73437382023-03-29 08:24:12 +00001575 if (mbedtls_ssl_get_psa_curve_info_from_tls_id(
Przemek Stekielda4fba62023-06-02 14:52:28 +02001576 named_group, &key_type, &bits) == PSA_SUCCESS) {
1577 alg = PSA_ALG_ECDH;
Przemek Stekiel29c219c2023-05-31 15:21:04 +02001578 }
1579#endif
1580#if defined(PSA_WANT_ALG_FFDH)
Przemek Stekiel75a5a9c2023-06-12 11:21:18 +02001581 if (mbedtls_ssl_get_psa_ffdh_info_from_tls_id(named_group, &bits,
1582 &key_type) == PSA_SUCCESS) {
Przemek Stekielda4fba62023-06-02 14:52:28 +02001583 alg = PSA_ALG_FFDH;
Przemek Stekiel29c219c2023-05-31 15:21:04 +02001584 }
1585#endif
1586
Przemek Stekiel75a5a9c2023-06-12 11:21:18 +02001587 if (key_type == PSA_KEY_TYPE_NONE) {
Gilles Peskine449bd832023-01-11 14:50:10 +01001588 return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
Valerio Setti40d9ca92023-01-04 16:08:04 +01001589 }
Przemek Stekiel29c219c2023-05-31 15:21:04 +02001590
Przemek Stekielda4fba62023-06-02 14:52:28 +02001591 if (buf_size < PSA_BITS_TO_BYTES(bits)) {
Przemek Stekielda4fba62023-06-02 14:52:28 +02001592 return MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL;
1593 }
1594
Przemek Stekiel6f199852023-06-29 08:59:26 +02001595 handshake->dh_psa_type = key_type;
1596 ssl->handshake->dh_bits = bits;
Jerry Yu89e103c2022-03-30 22:43:29 +08001597
1598 key_attributes = psa_key_attributes_init();
Gilles Peskine449bd832023-01-11 14:50:10 +01001599 psa_set_key_usage_flags(&key_attributes, PSA_KEY_USAGE_DERIVE);
Przemek Stekielda4fba62023-06-02 14:52:28 +02001600 psa_set_key_algorithm(&key_attributes, alg);
Przemek Stekiel6f199852023-06-29 08:59:26 +02001601 psa_set_key_type(&key_attributes, handshake->dh_psa_type);
1602 psa_set_key_bits(&key_attributes, handshake->dh_bits);
Jerry Yu89e103c2022-03-30 22:43:29 +08001603
Przemek Stekiel29c219c2023-05-31 15:21:04 +02001604 /* Generate ECDH/FFDH private key. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001605 status = psa_generate_key(&key_attributes,
Przemek Stekiel6f199852023-06-29 08:59:26 +02001606 &handshake->dh_psa_privkey);
Gilles Peskine449bd832023-01-11 14:50:10 +01001607 if (status != PSA_SUCCESS) {
Andrzej Kurek8a045ce2022-12-23 11:00:06 -05001608 ret = PSA_TO_MBEDTLS_ERR(status);
Gilles Peskine449bd832023-01-11 14:50:10 +01001609 MBEDTLS_SSL_DEBUG_RET(1, "psa_generate_key", ret);
1610 return ret;
Jerry Yu89e103c2022-03-30 22:43:29 +08001611
1612 }
1613
Przemek Stekiel29c219c2023-05-31 15:21:04 +02001614 /* Export the public part of the ECDH/FFDH private key from PSA. */
Przemek Stekiel6f199852023-06-29 08:59:26 +02001615 status = psa_export_public_key(handshake->dh_psa_privkey,
Przemek Stekiel29c219c2023-05-31 15:21:04 +02001616 buf, buf_size,
Gilles Peskine449bd832023-01-11 14:50:10 +01001617 &own_pubkey_len);
Przemek Stekiel29c219c2023-05-31 15:21:04 +02001618
Gilles Peskine449bd832023-01-11 14:50:10 +01001619 if (status != PSA_SUCCESS) {
Andrzej Kurek8a045ce2022-12-23 11:00:06 -05001620 ret = PSA_TO_MBEDTLS_ERR(status);
Gilles Peskine449bd832023-01-11 14:50:10 +01001621 MBEDTLS_SSL_DEBUG_RET(1, "psa_export_public_key", ret);
1622 return ret;
Jerry Yu89e103c2022-03-30 22:43:29 +08001623 }
1624
1625 *out_len = own_pubkey_len;
1626
Gilles Peskine449bd832023-01-11 14:50:10 +01001627 return 0;
Jerry Yu89e103c2022-03-30 22:43:29 +08001628}
Przemek Stekiel63706622023-05-23 16:31:56 +02001629#endif /* PSA_WANT_ALG_ECDH || PSA_WANT_ALG_FFDH */
XiaokangQian7807f9f2022-02-15 10:04:37 +00001630
Jerry Yu0c354a22022-08-29 15:25:36 +08001631/* RFC 8446 section 4.2
1632 *
1633 * If an implementation receives an extension which it recognizes and which is
1634 * not specified for the message in which it appears, it MUST abort the handshake
1635 * with an "illegal_parameter" alert.
1636 *
1637 */
Jerry Yuc4bf5d62022-10-29 09:08:47 +08001638int mbedtls_ssl_tls13_check_received_extension(
Gilles Peskine449bd832023-01-11 14:50:10 +01001639 mbedtls_ssl_context *ssl,
1640 int hs_msg_type,
1641 unsigned int received_extension_type,
1642 uint32_t hs_msg_allowed_extensions_mask)
Jerry Yu0c354a22022-08-29 15:25:36 +08001643{
Jerry Yudf0ad652022-10-31 13:20:57 +08001644 uint32_t extension_mask = mbedtls_ssl_get_extension_mask(
Gilles Peskine449bd832023-01-11 14:50:10 +01001645 received_extension_type);
Jerry Yu0c354a22022-08-29 15:25:36 +08001646
Jerry Yu79aa7212022-11-08 21:30:21 +08001647 MBEDTLS_SSL_PRINT_EXT(
Gilles Peskine449bd832023-01-11 14:50:10 +01001648 3, hs_msg_type, received_extension_type, "received");
Jerry Yu0c354a22022-08-29 15:25:36 +08001649
Gilles Peskine449bd832023-01-11 14:50:10 +01001650 if ((extension_mask & hs_msg_allowed_extensions_mask) == 0) {
Jerry Yu79aa7212022-11-08 21:30:21 +08001651 MBEDTLS_SSL_PRINT_EXT(
Gilles Peskine449bd832023-01-11 14:50:10 +01001652 3, hs_msg_type, received_extension_type, "is illegal");
Jerry Yu0c354a22022-08-29 15:25:36 +08001653 MBEDTLS_SSL_PEND_FATAL_ALERT(
1654 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER,
Gilles Peskine449bd832023-01-11 14:50:10 +01001655 MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER);
1656 return MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER;
Jerry Yu0c354a22022-08-29 15:25:36 +08001657 }
1658
1659 ssl->handshake->received_extensions |= extension_mask;
Jerry Yuc4bf5d62022-10-29 09:08:47 +08001660 /*
1661 * If it is a message containing extension responses, check that we
1662 * previously sent the extension.
1663 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001664 switch (hs_msg_type) {
Jerry Yu0c354a22022-08-29 15:25:36 +08001665 case MBEDTLS_SSL_HS_SERVER_HELLO:
Jerry Yudf0ad652022-10-31 13:20:57 +08001666 case MBEDTLS_SSL_TLS1_3_HS_HELLO_RETRY_REQUEST:
Jerry Yu0c354a22022-08-29 15:25:36 +08001667 case MBEDTLS_SSL_HS_ENCRYPTED_EXTENSIONS:
1668 case MBEDTLS_SSL_HS_CERTIFICATE:
Jerry Yuc4bf5d62022-10-29 09:08:47 +08001669 /* Check if the received extension is sent by peer message.*/
Gilles Peskine449bd832023-01-11 14:50:10 +01001670 if ((ssl->handshake->sent_extensions & extension_mask) != 0) {
1671 return 0;
1672 }
Jerry Yu0c354a22022-08-29 15:25:36 +08001673 break;
1674 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01001675 return 0;
Jerry Yu0c354a22022-08-29 15:25:36 +08001676 }
1677
Jerry Yu79aa7212022-11-08 21:30:21 +08001678 MBEDTLS_SSL_PRINT_EXT(
Gilles Peskine449bd832023-01-11 14:50:10 +01001679 3, hs_msg_type, received_extension_type, "is unsupported");
Jerry Yu0c354a22022-08-29 15:25:36 +08001680 MBEDTLS_SSL_PEND_FATAL_ALERT(
1681 MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_EXT,
Gilles Peskine449bd832023-01-11 14:50:10 +01001682 MBEDTLS_ERR_SSL_UNSUPPORTED_EXTENSION);
1683 return MBEDTLS_ERR_SSL_UNSUPPORTED_EXTENSION;
Jerry Yu0c354a22022-08-29 15:25:36 +08001684}
1685
Jan Bruckner151f6422023-02-10 12:45:19 +01001686#if defined(MBEDTLS_SSL_RECORD_SIZE_LIMIT)
Jan Bruckner1a38e542023-03-15 14:15:11 +01001687/* RFC 8449, section 4:
1688 *
Jan Bruckner151f6422023-02-10 12:45:19 +01001689 * The ExtensionData of the "record_size_limit" extension is
1690 * RecordSizeLimit:
1691 * uint16 RecordSizeLimit;
1692 */
1693MBEDTLS_CHECK_RETURN_CRITICAL
1694int mbedtls_ssl_tls13_parse_record_size_limit_ext(mbedtls_ssl_context *ssl,
1695 const unsigned char *buf,
1696 const unsigned char *end)
1697{
Jan Bruckner1a38e542023-03-15 14:15:11 +01001698 const unsigned char *p = buf;
1699 uint16_t record_size_limit;
Jan Brucknera0589e72023-03-15 11:04:45 +01001700 const size_t extension_data_len = end - buf;
Jan Bruckner1a38e542023-03-15 14:15:11 +01001701
Xiaokang Qian73437382023-03-29 08:24:12 +00001702 if (extension_data_len !=
1703 MBEDTLS_SSL_RECORD_SIZE_LIMIT_EXTENSION_DATA_LENGTH) {
Jan Bruckner151f6422023-02-10 12:45:19 +01001704 MBEDTLS_SSL_DEBUG_MSG(2,
Jan Bruckner1a38e542023-03-15 14:15:11 +01001705 ("record_size_limit extension has invalid length: %"
1706 MBEDTLS_PRINTF_SIZET " Bytes",
Jan Bruckner151f6422023-02-10 12:45:19 +01001707 extension_data_len));
1708
1709 MBEDTLS_SSL_PEND_FATAL_ALERT(
1710 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER,
1711 MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER);
1712 return MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER;
1713 }
1714
Jan Bruckner151f6422023-02-10 12:45:19 +01001715 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, 2);
1716 record_size_limit = MBEDTLS_GET_UINT16_BE(p, 0);
1717
1718 MBEDTLS_SSL_DEBUG_MSG(2, ("RecordSizeLimit: %u Bytes", record_size_limit));
1719
Jan Bruckner1a38e542023-03-15 14:15:11 +01001720 /* RFC 8449, section 4
Jan Bruckner151f6422023-02-10 12:45:19 +01001721 *
1722 * Endpoints MUST NOT send a "record_size_limit" extension with a value
1723 * smaller than 64. An endpoint MUST treat receipt of a smaller value
1724 * as a fatal error and generate an "illegal_parameter" alert.
1725 */
Jan Brucknera0589e72023-03-15 11:04:45 +01001726 if (record_size_limit < MBEDTLS_SSL_RECORD_SIZE_LIMIT_MIN) {
Jan Bruckner151f6422023-02-10 12:45:19 +01001727 MBEDTLS_SSL_PEND_FATAL_ALERT(
1728 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER,
1729 MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER);
1730 return MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER;
1731 }
1732
Xiaokang Qian73437382023-03-29 08:24:12 +00001733 MBEDTLS_SSL_DEBUG_MSG(
1734 2, ("record_size_limit extension is still in development. Aborting handshake."));
Jan Bruckner151f6422023-02-10 12:45:19 +01001735
1736 MBEDTLS_SSL_PEND_FATAL_ALERT(
1737 MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_EXT,
1738 MBEDTLS_ERR_SSL_UNSUPPORTED_EXTENSION);
1739 return MBEDTLS_ERR_SSL_UNSUPPORTED_EXTENSION;
1740}
1741#endif /* MBEDTLS_SSL_RECORD_SIZE_LIMIT */
1742
Jerry Yufb4b6472022-01-27 15:03:26 +08001743#endif /* MBEDTLS_SSL_TLS_C && MBEDTLS_SSL_PROTO_TLS1_3 */