blob: d2082a7f1e3f61b9a82c6458a4efdbd7e5248f0e [file] [log] [blame]
Jerry Yu65dd2cc2021-08-18 16:38:40 +08001/*
2 * TLS 1.3 functionality shared between client and server
3 *
4 * Copyright The Mbed TLS Contributors
5 * SPDX-License-Identifier: Apache-2.0
6 *
7 * Licensed under the Apache License, Version 2.0 (the "License"); you may
8 * not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
15 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 */
19
20#include "common.h"
21
Jerry Yufb4b6472022-01-27 15:03:26 +080022#if defined(MBEDTLS_SSL_TLS_C) && defined(MBEDTLS_SSL_PROTO_TLS1_3)
Jerry Yu65dd2cc2021-08-18 16:38:40 +080023
Jerry Yu30b071c2021-09-12 20:16:03 +080024#include <string.h>
25
Jerry Yuc8a392c2021-08-18 16:46:28 +080026#include "mbedtls/error.h"
Jerry Yu75336352021-09-01 15:59:36 +080027#include "mbedtls/debug.h"
Jerry Yu30b071c2021-09-12 20:16:03 +080028#include "mbedtls/oid.h"
29#include "mbedtls/platform.h"
Gabor Mezei685472b2021-11-24 11:17:36 +010030#include "mbedtls/constant_time.h"
Jerry Yu141bbe72022-12-01 20:30:41 +080031#include "psa/crypto.h"
32#include "mbedtls/psa_util.h"
Jerry Yuc8a392c2021-08-18 16:46:28 +080033
Jerry Yu65dd2cc2021-08-18 16:38:40 +080034#include "ssl_misc.h"
Ronald Crone3dac4a2022-06-10 17:21:51 +020035#include "ssl_tls13_invasive.h"
Jerry Yu30b071c2021-09-12 20:16:03 +080036#include "ssl_tls13_keys.h"
Jerry Yu67eced02022-02-25 13:37:36 +080037#include "ssl_debug_helpers.h"
Jerry Yu65dd2cc2021-08-18 16:38:40 +080038
Andrzej Kurek8a045ce2022-12-23 11:00:06 -050039#include "psa/crypto.h"
40#include "mbedtls/psa_util.h"
41
42#define PSA_TO_MBEDTLS_ERR(status) PSA_TO_MBEDTLS_ERR_LIST(status, \
43 psa_to_ssl_errors, \
44 psa_generic_status_to_mbedtls)
45
Jerry Yufbe3e642022-04-25 19:31:51 +080046const uint8_t mbedtls_ssl_tls13_hello_retry_request_magic[
Gilles Peskine449bd832023-01-11 14:50:10 +010047 MBEDTLS_SERVER_HELLO_RANDOM_LEN] =
48{ 0xCF, 0x21, 0xAD, 0x74, 0xE5, 0x9A, 0x61, 0x11,
49 0xBE, 0x1D, 0x8C, 0x02, 0x1E, 0x65, 0xB8, 0x91,
50 0xC2, 0xA2, 0x11, 0x16, 0x7A, 0xBB, 0x8C, 0x5E,
51 0x07, 0x9E, 0x09, 0xE2, 0xC8, 0xA8, 0x33, 0x9C };
Jerry Yu93a13f22022-04-11 23:00:01 +080052
Gilles Peskine449bd832023-01-11 14:50:10 +010053int mbedtls_ssl_tls13_fetch_handshake_msg(mbedtls_ssl_context *ssl,
54 unsigned hs_type,
55 unsigned char **buf,
56 size_t *buf_len)
XiaokangQian6b226b02021-09-24 07:51:16 +000057{
58 int ret;
59
Gilles Peskine449bd832023-01-11 14:50:10 +010060 if ((ret = mbedtls_ssl_read_record(ssl, 0)) != 0) {
61 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_read_record", ret);
XiaokangQian6b226b02021-09-24 07:51:16 +000062 goto cleanup;
63 }
64
Gilles Peskine449bd832023-01-11 14:50:10 +010065 if (ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE ||
66 ssl->in_msg[0] != hs_type) {
67 MBEDTLS_SSL_DEBUG_MSG(1, ("Receive unexpected handshake message."));
68 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE,
69 MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE);
XiaokangQian6b226b02021-09-24 07:51:16 +000070 ret = MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE;
71 goto cleanup;
72 }
73
XiaokangQian05420b12021-09-29 08:46:37 +000074 /*
75 * Jump handshake header (4 bytes, see Section 4 of RFC 8446).
76 * ...
77 * HandshakeType msg_type;
78 * uint24 length;
79 * ...
80 */
Xiaofei Baieef15042021-11-18 07:29:56 +000081 *buf = ssl->in_msg + 4;
82 *buf_len = ssl->in_hslen - 4;
XiaokangQian6b226b02021-09-24 07:51:16 +000083
XiaokangQian6b226b02021-09-24 07:51:16 +000084cleanup:
85
Gilles Peskine449bd832023-01-11 14:50:10 +010086 return ret;
XiaokangQian6b226b02021-09-24 07:51:16 +000087}
88
Ronald Cron47dce632023-02-08 17:38:29 +010089int mbedtls_ssl_tls13_is_supported_versions_ext_present_in_exts(
90 mbedtls_ssl_context *ssl,
91 const unsigned char *buf, const unsigned char *end,
Ronald Croneff56732023-04-03 17:36:31 +020092 const unsigned char **supported_versions_data,
93 const unsigned char **supported_versions_data_end)
Ronald Cron47dce632023-02-08 17:38:29 +010094{
95 const unsigned char *p = buf;
96 size_t extensions_len;
97 const unsigned char *extensions_end;
98
Ronald Croneff56732023-04-03 17:36:31 +020099 *supported_versions_data = NULL;
100 *supported_versions_data_end = NULL;
Ronald Cron47dce632023-02-08 17:38:29 +0100101
102 /* Case of no extension */
103 if (p == end) {
104 return 0;
105 }
106
107 /* ...
108 * Extension extensions<x..2^16-1>;
109 * ...
110 * struct {
111 * ExtensionType extension_type; (2 bytes)
112 * opaque extension_data<0..2^16-1>;
113 * } Extension;
114 */
115 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, 2);
116 extensions_len = MBEDTLS_GET_UINT16_BE(p, 0);
117 p += 2;
118
119 /* Check extensions do not go beyond the buffer of data. */
120 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, extensions_len);
121 extensions_end = p + extensions_len;
122
123 while (p < extensions_end) {
124 unsigned int extension_type;
125 size_t extension_data_len;
126
127 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, extensions_end, 4);
128 extension_type = MBEDTLS_GET_UINT16_BE(p, 0);
129 extension_data_len = MBEDTLS_GET_UINT16_BE(p, 2);
130 p += 4;
131 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, extensions_end, extension_data_len);
132
133 if (extension_type == MBEDTLS_TLS_EXT_SUPPORTED_VERSIONS) {
Ronald Croneff56732023-04-03 17:36:31 +0200134 *supported_versions_data = p;
135 *supported_versions_data_end = p + extension_data_len;
Ronald Cron47dce632023-02-08 17:38:29 +0100136 return 1;
137 }
138 p += extension_data_len;
139 }
140
141 return 0;
142}
143
Ronald Cron928cbd32022-10-04 16:14:26 +0200144#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED)
Jerry Yu30b071c2021-09-12 20:16:03 +0800145/*
Jerry Yu30b071c2021-09-12 20:16:03 +0800146 * STATE HANDLING: Read CertificateVerify
147 */
Jerry Yud0fc5852021-10-29 11:09:06 +0800148/* Macro to express the maximum length of the verify structure.
Jerry Yu30b071c2021-09-12 20:16:03 +0800149 *
150 * The structure is computed per TLS 1.3 specification as:
151 * - 64 bytes of octet 32,
152 * - 33 bytes for the context string
153 * (which is either "TLS 1.3, client CertificateVerify"
154 * or "TLS 1.3, server CertificateVerify"),
Jerry Yud0fc5852021-10-29 11:09:06 +0800155 * - 1 byte for the octet 0x0, which serves as a separator,
Jerry Yu30b071c2021-09-12 20:16:03 +0800156 * - 32 or 48 bytes for the Transcript-Hash(Handshake Context, Certificate)
157 * (depending on the size of the transcript_hash)
158 *
159 * This results in a total size of
160 * - 130 bytes for a SHA256-based transcript hash, or
161 * (64 + 33 + 1 + 32 bytes)
162 * - 146 bytes for a SHA384-based transcript hash.
163 * (64 + 33 + 1 + 48 bytes)
164 *
165 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100166#define SSL_VERIFY_STRUCT_MAX_SIZE (64 + \
167 33 + \
168 1 + \
169 MBEDTLS_TLS1_3_MD_MAX_SIZE \
170 )
Jerry Yu30b071c2021-09-12 20:16:03 +0800171
Jerry Yu0b32c502021-10-28 13:41:59 +0800172/*
173 * The ssl_tls13_create_verify_structure() creates the verify structure.
174 * As input, it requires the transcript hash.
175 *
176 * The caller has to ensure that the buffer has size at least
177 * SSL_VERIFY_STRUCT_MAX_SIZE bytes.
178 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100179static void ssl_tls13_create_verify_structure(const unsigned char *transcript_hash,
180 size_t transcript_hash_len,
181 unsigned char *verify_buffer,
182 size_t *verify_buffer_len,
183 int from)
Jerry Yu0b32c502021-10-28 13:41:59 +0800184{
185 size_t idx;
Jerry Yu30b071c2021-09-12 20:16:03 +0800186
Jerry Yu0b32c502021-10-28 13:41:59 +0800187 /* RFC 8446, Section 4.4.3:
188 *
189 * The digital signature [in the CertificateVerify message] is then
190 * computed over the concatenation of:
191 * - A string that consists of octet 32 (0x20) repeated 64 times
192 * - The context string
193 * - A single 0 byte which serves as the separator
194 * - The content to be signed
195 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100196 memset(verify_buffer, 0x20, 64);
Jerry Yu0b32c502021-10-28 13:41:59 +0800197 idx = 64;
198
Gilles Peskine449bd832023-01-11 14:50:10 +0100199 if (from == MBEDTLS_SSL_IS_CLIENT) {
200 memcpy(verify_buffer + idx, MBEDTLS_SSL_TLS1_3_LBL_WITH_LEN(client_cv));
201 idx += MBEDTLS_SSL_TLS1_3_LBL_LEN(client_cv);
202 } else { /* from == MBEDTLS_SSL_IS_SERVER */
203 memcpy(verify_buffer + idx, MBEDTLS_SSL_TLS1_3_LBL_WITH_LEN(server_cv));
204 idx += MBEDTLS_SSL_TLS1_3_LBL_LEN(server_cv);
Jerry Yu0b32c502021-10-28 13:41:59 +0800205 }
206
207 verify_buffer[idx++] = 0x0;
208
Gilles Peskine449bd832023-01-11 14:50:10 +0100209 memcpy(verify_buffer + idx, transcript_hash, transcript_hash_len);
Jerry Yu0b32c502021-10-28 13:41:59 +0800210 idx += transcript_hash_len;
211
212 *verify_buffer_len = idx;
213}
214
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200215MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100216static int ssl_tls13_parse_certificate_verify(mbedtls_ssl_context *ssl,
217 const unsigned char *buf,
218 const unsigned char *end,
219 const unsigned char *verify_buffer,
220 size_t verify_buffer_len)
Jerry Yu30b071c2021-09-12 20:16:03 +0800221{
222 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
pespaceka1378102022-04-26 15:03:11 +0200223 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Jerry Yu30b071c2021-09-12 20:16:03 +0800224 const unsigned char *p = buf;
225 uint16_t algorithm;
Jerry Yu30b071c2021-09-12 20:16:03 +0800226 size_t signature_len;
227 mbedtls_pk_type_t sig_alg;
228 mbedtls_md_type_t md_alg;
pespaceka1378102022-04-26 15:03:11 +0200229 psa_algorithm_t hash_alg = PSA_ALG_NONE;
230 unsigned char verify_hash[PSA_HASH_MAX_SIZE];
Jerry Yu30b071c2021-09-12 20:16:03 +0800231 size_t verify_hash_len;
232
Xiaofei Baid25fab62021-12-02 06:36:27 +0000233 void const *options = NULL;
XiaokangQian82d34cc2021-11-03 08:51:56 +0000234#if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT)
Xiaofei Baid25fab62021-12-02 06:36:27 +0000235 mbedtls_pk_rsassa_pss_options rsassa_pss_options;
XiaokangQian82d34cc2021-11-03 08:51:56 +0000236#endif /* MBEDTLS_X509_RSASSA_PSS_SUPPORT */
237
Jerry Yu30b071c2021-09-12 20:16:03 +0800238 /*
239 * struct {
240 * SignatureScheme algorithm;
241 * opaque signature<0..2^16-1>;
242 * } CertificateVerify;
243 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100244 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, 2);
245 algorithm = MBEDTLS_GET_UINT16_BE(p, 0);
Jerry Yu30b071c2021-09-12 20:16:03 +0800246 p += 2;
247
248 /* RFC 8446 section 4.4.3
249 *
250 * If the CertificateVerify message is sent by a server, the signature algorithm
251 * MUST be one offered in the client's "signature_algorithms" extension unless
252 * no valid certificate chain can be produced without unsupported algorithms
253 *
254 * RFC 8446 section 4.4.2.2
255 *
256 * If the client cannot construct an acceptable chain using the provided
257 * certificates and decides to abort the handshake, then it MUST abort the handshake
258 * with an appropriate certificate-related alert (by default, "unsupported_certificate").
259 *
Jerry Yu6f87f252021-10-29 20:12:51 +0800260 * Check if algorithm is an offered signature algorithm.
Jerry Yu30b071c2021-09-12 20:16:03 +0800261 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100262 if (!mbedtls_ssl_sig_alg_is_offered(ssl, algorithm)) {
Jerry Yu982d9e52021-10-14 15:59:37 +0800263 /* algorithm not in offered signature algorithms list */
Gilles Peskine449bd832023-01-11 14:50:10 +0100264 MBEDTLS_SSL_DEBUG_MSG(1, ("Received signature algorithm(%04x) is not "
265 "offered.",
266 (unsigned int) algorithm));
Jerry Yu6f87f252021-10-29 20:12:51 +0800267 goto error;
Jerry Yu30b071c2021-09-12 20:16:03 +0800268 }
269
Gilles Peskine449bd832023-01-11 14:50:10 +0100270 if (mbedtls_ssl_get_pk_type_and_md_alg_from_sig_alg(
271 algorithm, &sig_alg, &md_alg) != 0) {
Jerry Yu8c338862022-03-23 13:34:04 +0800272 goto error;
Jerry Yu30b071c2021-09-12 20:16:03 +0800273 }
274
Gilles Peskine449bd832023-01-11 14:50:10 +0100275 hash_alg = mbedtls_hash_info_psa_from_md(md_alg);
276 if (hash_alg == 0) {
pespaceka1378102022-04-26 15:03:11 +0200277 goto error;
278 }
279
Gilles Peskine449bd832023-01-11 14:50:10 +0100280 MBEDTLS_SSL_DEBUG_MSG(3, ("Certificate Verify: Signature algorithm ( %04x )",
281 (unsigned int) algorithm));
Jerry Yu30b071c2021-09-12 20:16:03 +0800282
283 /*
284 * Check the certificate's key type matches the signature alg
285 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100286 if (!mbedtls_pk_can_do(&ssl->session_negotiate->peer_cert->pk, sig_alg)) {
287 MBEDTLS_SSL_DEBUG_MSG(1, ("signature algorithm doesn't match cert key"));
Jerry Yu6f87f252021-10-29 20:12:51 +0800288 goto error;
Jerry Yu30b071c2021-09-12 20:16:03 +0800289 }
290
Gilles Peskine449bd832023-01-11 14:50:10 +0100291 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, 2);
292 signature_len = MBEDTLS_GET_UINT16_BE(p, 0);
Jerry Yu30b071c2021-09-12 20:16:03 +0800293 p += 2;
Gilles Peskine449bd832023-01-11 14:50:10 +0100294 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, signature_len);
Jerry Yu30b071c2021-09-12 20:16:03 +0800295
Gilles Peskine449bd832023-01-11 14:50:10 +0100296 status = psa_hash_compute(hash_alg,
297 verify_buffer,
298 verify_buffer_len,
299 verify_hash,
300 sizeof(verify_hash),
301 &verify_hash_len);
302 if (status != PSA_SUCCESS) {
303 MBEDTLS_SSL_DEBUG_RET(1, "hash computation PSA error", status);
Jerry Yu6f87f252021-10-29 20:12:51 +0800304 goto error;
Jerry Yu133690c2021-10-25 14:01:13 +0800305 }
306
Gilles Peskine449bd832023-01-11 14:50:10 +0100307 MBEDTLS_SSL_DEBUG_BUF(3, "verify hash", verify_hash, verify_hash_len);
XiaokangQian82d34cc2021-11-03 08:51:56 +0000308#if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT)
Gilles Peskine449bd832023-01-11 14:50:10 +0100309 if (sig_alg == MBEDTLS_PK_RSASSA_PSS) {
Xiaofei Baid25fab62021-12-02 06:36:27 +0000310 rsassa_pss_options.mgf1_hash_id = md_alg;
Przemek Stekiel6a5e0182022-06-27 11:53:13 +0200311
Gilles Peskine449bd832023-01-11 14:50:10 +0100312 rsassa_pss_options.expected_salt_len = PSA_HASH_LENGTH(hash_alg);
313 options = (const void *) &rsassa_pss_options;
XiaokangQian82d34cc2021-11-03 08:51:56 +0000314 }
315#endif /* MBEDTLS_X509_RSASSA_PSS_SUPPORT */
Jerry Yu30b071c2021-09-12 20:16:03 +0800316
Gilles Peskine449bd832023-01-11 14:50:10 +0100317 if ((ret = mbedtls_pk_verify_ext(sig_alg, options,
318 &ssl->session_negotiate->peer_cert->pk,
319 md_alg, verify_hash, verify_hash_len,
320 p, signature_len)) == 0) {
321 return 0;
Jerry Yu30b071c2021-09-12 20:16:03 +0800322 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100323 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_pk_verify_ext", ret);
Jerry Yu30b071c2021-09-12 20:16:03 +0800324
Jerry Yu6f87f252021-10-29 20:12:51 +0800325error:
326 /* RFC 8446 section 4.4.3
327 *
328 * If the verification fails, the receiver MUST terminate the handshake
329 * with a "decrypt_error" alert.
Gilles Peskine449bd832023-01-11 14:50:10 +0100330 */
331 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_DECRYPT_ERROR,
332 MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE);
333 return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
Jerry Yu6f87f252021-10-29 20:12:51 +0800334
Jerry Yu30b071c2021-09-12 20:16:03 +0800335}
Ronald Cron928cbd32022-10-04 16:14:26 +0200336#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED */
Jerry Yu30b071c2021-09-12 20:16:03 +0800337
Gilles Peskine449bd832023-01-11 14:50:10 +0100338int mbedtls_ssl_tls13_process_certificate_verify(mbedtls_ssl_context *ssl)
Jerry Yu30b071c2021-09-12 20:16:03 +0800339{
Jerry Yu30b071c2021-09-12 20:16:03 +0800340
Ronald Cron928cbd32022-10-04 16:14:26 +0200341#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED)
Jerry Yuda8cdf22021-10-25 15:06:49 +0800342 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
343 unsigned char verify_buffer[SSL_VERIFY_STRUCT_MAX_SIZE];
344 size_t verify_buffer_len;
345 unsigned char transcript[MBEDTLS_TLS1_3_MD_MAX_SIZE];
346 size_t transcript_len;
347 unsigned char *buf;
348 size_t buf_len;
349
Gilles Peskine449bd832023-01-11 14:50:10 +0100350 MBEDTLS_SSL_DEBUG_MSG(2, ("=> parse certificate verify"));
Jerry Yu30b071c2021-09-12 20:16:03 +0800351
Jerry Yuda8cdf22021-10-25 15:06:49 +0800352 MBEDTLS_SSL_PROC_CHK(
Gilles Peskine449bd832023-01-11 14:50:10 +0100353 mbedtls_ssl_tls13_fetch_handshake_msg(ssl,
354 MBEDTLS_SSL_HS_CERTIFICATE_VERIFY, &buf, &buf_len));
Jerry Yu30b071c2021-09-12 20:16:03 +0800355
Jerry Yuda8cdf22021-10-25 15:06:49 +0800356 /* Need to calculate the hash of the transcript first
Jerry Yu0b32c502021-10-28 13:41:59 +0800357 * before reading the message since otherwise it gets
358 * included in the transcript
359 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100360 ret = mbedtls_ssl_get_handshake_transcript(ssl,
361 ssl->handshake->ciphersuite_info->mac,
362 transcript, sizeof(transcript),
363 &transcript_len);
364 if (ret != 0) {
Jerry Yuda8cdf22021-10-25 15:06:49 +0800365 MBEDTLS_SSL_PEND_FATAL_ALERT(
366 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR,
Gilles Peskine449bd832023-01-11 14:50:10 +0100367 MBEDTLS_ERR_SSL_INTERNAL_ERROR);
368 return ret;
Jerry Yu30b071c2021-09-12 20:16:03 +0800369 }
370
Gilles Peskine449bd832023-01-11 14:50:10 +0100371 MBEDTLS_SSL_DEBUG_BUF(3, "handshake hash", transcript, transcript_len);
Jerry Yuda8cdf22021-10-25 15:06:49 +0800372
373 /* Create verify structure */
Gilles Peskine449bd832023-01-11 14:50:10 +0100374 ssl_tls13_create_verify_structure(transcript,
375 transcript_len,
376 verify_buffer,
377 &verify_buffer_len,
378 (ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT) ?
379 MBEDTLS_SSL_IS_SERVER :
380 MBEDTLS_SSL_IS_CLIENT);
Jerry Yuda8cdf22021-10-25 15:06:49 +0800381
382 /* Process the message contents */
Gilles Peskine449bd832023-01-11 14:50:10 +0100383 MBEDTLS_SSL_PROC_CHK(ssl_tls13_parse_certificate_verify(ssl, buf,
384 buf + buf_len, verify_buffer,
385 verify_buffer_len));
Jerry Yuda8cdf22021-10-25 15:06:49 +0800386
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +0100387 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_add_hs_msg_to_checksum(ssl,
Manuel Pégourié-Gonnard43cc1272023-02-06 11:48:19 +0100388 MBEDTLS_SSL_HS_CERTIFICATE_VERIFY,
389 buf, buf_len));
Jerry Yu30b071c2021-09-12 20:16:03 +0800390
391cleanup:
392
Gilles Peskine449bd832023-01-11 14:50:10 +0100393 MBEDTLS_SSL_DEBUG_MSG(2, ("<= parse certificate verify"));
394 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_tls13_process_certificate_verify", ret);
395 return ret;
Jerry Yuda8cdf22021-10-25 15:06:49 +0800396#else
397 ((void) ssl);
Gilles Peskine449bd832023-01-11 14:50:10 +0100398 MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
399 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Ronald Cron928cbd32022-10-04 16:14:26 +0200400#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED */
Jerry Yu30b071c2021-09-12 20:16:03 +0800401}
402
403/*
Xiaofei Bai947571e2021-09-29 09:12:03 +0000404 *
XiaokangQian6b916b12022-04-25 07:29:34 +0000405 * STATE HANDLING: Incoming Certificate.
Xiaofei Bai947571e2021-09-29 09:12:03 +0000406 *
407 */
408
Ronald Cronde08cf32022-10-04 17:15:35 +0200409#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED)
Xiaofei Bai947571e2021-09-29 09:12:03 +0000410#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
411/*
412 * Structure of Certificate message:
413 *
414 * enum {
415 * X509(0),
416 * RawPublicKey(2),
417 * (255)
418 * } CertificateType;
419 *
420 * struct {
421 * select (certificate_type) {
422 * case RawPublicKey:
423 * * From RFC 7250 ASN.1_subjectPublicKeyInfo *
424 * opaque ASN1_subjectPublicKeyInfo<1..2^24-1>;
425 * case X509:
426 * opaque cert_data<1..2^24-1>;
427 * };
428 * Extension extensions<0..2^16-1>;
429 * } CertificateEntry;
430 *
431 * struct {
432 * opaque certificate_request_context<0..2^8-1>;
433 * CertificateEntry certificate_list<0..2^24-1>;
434 * } Certificate;
435 *
436 */
Xiaofei Bai79595ac2021-10-26 07:16:45 +0000437
438/* Parse certificate chain send by the server. */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200439MBEDTLS_CHECK_RETURN_CRITICAL
Ronald Crone3dac4a2022-06-10 17:21:51 +0200440MBEDTLS_STATIC_TESTABLE
Gilles Peskine449bd832023-01-11 14:50:10 +0100441int mbedtls_ssl_tls13_parse_certificate(mbedtls_ssl_context *ssl,
442 const unsigned char *buf,
443 const unsigned char *end)
Xiaofei Bai947571e2021-09-29 09:12:03 +0000444{
445 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
446 size_t certificate_request_context_len = 0;
447 size_t certificate_list_len = 0;
448 const unsigned char *p = buf;
449 const unsigned char *certificate_list_end;
Jerry Yuc4bf5d62022-10-29 09:08:47 +0800450 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
Xiaofei Bai947571e2021-09-29 09:12:03 +0000451
Gilles Peskine449bd832023-01-11 14:50:10 +0100452 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, 4);
Xiaofei Bai947571e2021-09-29 09:12:03 +0000453 certificate_request_context_len = p[0];
Gilles Peskine449bd832023-01-11 14:50:10 +0100454 certificate_list_len = MBEDTLS_GET_UINT24_BE(p, 1);
XiaokangQian63e713e2022-05-15 04:26:57 +0000455 p += 4;
Xiaofei Bai947571e2021-09-29 09:12:03 +0000456
457 /* In theory, the certificate list can be up to 2^24 Bytes, but we don't
458 * support anything beyond 2^16 = 64K.
459 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100460 if ((certificate_request_context_len != 0) ||
461 (certificate_list_len >= 0x10000)) {
462 MBEDTLS_SSL_DEBUG_MSG(1, ("bad certificate message"));
463 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR,
464 MBEDTLS_ERR_SSL_DECODE_ERROR);
465 return MBEDTLS_ERR_SSL_DECODE_ERROR;
Xiaofei Bai947571e2021-09-29 09:12:03 +0000466 }
467
468 /* In case we tried to reuse a session but it failed */
Gilles Peskine449bd832023-01-11 14:50:10 +0100469 if (ssl->session_negotiate->peer_cert != NULL) {
470 mbedtls_x509_crt_free(ssl->session_negotiate->peer_cert);
471 mbedtls_free(ssl->session_negotiate->peer_cert);
Xiaofei Bai947571e2021-09-29 09:12:03 +0000472 }
473
Gilles Peskine449bd832023-01-11 14:50:10 +0100474 if (certificate_list_len == 0) {
XiaokangQianc3017f62022-05-13 05:55:41 +0000475 ssl->session_negotiate->peer_cert = NULL;
476 ret = 0;
477 goto exit;
478 }
479
Gilles Peskine449bd832023-01-11 14:50:10 +0100480 if ((ssl->session_negotiate->peer_cert =
481 mbedtls_calloc(1, sizeof(mbedtls_x509_crt))) == NULL) {
482 MBEDTLS_SSL_DEBUG_MSG(1, ("alloc( %" MBEDTLS_PRINTF_SIZET " bytes ) failed",
483 sizeof(mbedtls_x509_crt)));
484 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR,
485 MBEDTLS_ERR_SSL_ALLOC_FAILED);
486 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
Xiaofei Bai947571e2021-09-29 09:12:03 +0000487 }
488
Gilles Peskine449bd832023-01-11 14:50:10 +0100489 mbedtls_x509_crt_init(ssl->session_negotiate->peer_cert);
Xiaofei Bai947571e2021-09-29 09:12:03 +0000490
Gilles Peskine449bd832023-01-11 14:50:10 +0100491 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, certificate_list_len);
Xiaofei Bai947571e2021-09-29 09:12:03 +0000492 certificate_list_end = p + certificate_list_len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100493 while (p < certificate_list_end) {
Xiaofei Bai947571e2021-09-29 09:12:03 +0000494 size_t cert_data_len, extensions_len;
Jerry Yu2eaa7602022-08-04 17:28:15 +0800495 const unsigned char *extensions_end;
Xiaofei Bai947571e2021-09-29 09:12:03 +0000496
Gilles Peskine449bd832023-01-11 14:50:10 +0100497 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, certificate_list_end, 3);
498 cert_data_len = MBEDTLS_GET_UINT24_BE(p, 0);
Xiaofei Bai947571e2021-09-29 09:12:03 +0000499 p += 3;
500
501 /* In theory, the CRT can be up to 2^24 Bytes, but we don't support
502 * anything beyond 2^16 = 64K. Otherwise as in the TLS 1.2 code,
503 * check that we have a minimum of 128 bytes of data, this is not
504 * clear why we need that though.
505 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100506 if ((cert_data_len < 128) || (cert_data_len >= 0x10000)) {
507 MBEDTLS_SSL_DEBUG_MSG(1, ("bad Certificate message"));
508 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR,
509 MBEDTLS_ERR_SSL_DECODE_ERROR);
510 return MBEDTLS_ERR_SSL_DECODE_ERROR;
Xiaofei Bai947571e2021-09-29 09:12:03 +0000511 }
512
Gilles Peskine449bd832023-01-11 14:50:10 +0100513 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, certificate_list_end, cert_data_len);
514 ret = mbedtls_x509_crt_parse_der(ssl->session_negotiate->peer_cert,
515 p, cert_data_len);
Xiaofei Bai947571e2021-09-29 09:12:03 +0000516
Gilles Peskine449bd832023-01-11 14:50:10 +0100517 switch (ret) {
Xiaofei Bai947571e2021-09-29 09:12:03 +0000518 case 0: /*ok*/
519 break;
520 case MBEDTLS_ERR_X509_UNKNOWN_SIG_ALG + MBEDTLS_ERR_OID_NOT_FOUND:
521 /* Ignore certificate with an unknown algorithm: maybe a
522 prior certificate was already trusted. */
523 break;
524
525 case MBEDTLS_ERR_X509_ALLOC_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100526 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR,
527 MBEDTLS_ERR_X509_ALLOC_FAILED);
528 MBEDTLS_SSL_DEBUG_RET(1, " mbedtls_x509_crt_parse_der", ret);
529 return ret;
Xiaofei Bai947571e2021-09-29 09:12:03 +0000530
531 case MBEDTLS_ERR_X509_UNKNOWN_VERSION:
Gilles Peskine449bd832023-01-11 14:50:10 +0100532 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT,
533 MBEDTLS_ERR_X509_UNKNOWN_VERSION);
534 MBEDTLS_SSL_DEBUG_RET(1, " mbedtls_x509_crt_parse_der", ret);
535 return ret;
Xiaofei Bai947571e2021-09-29 09:12:03 +0000536
537 default:
Gilles Peskine449bd832023-01-11 14:50:10 +0100538 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_BAD_CERT,
539 ret);
540 MBEDTLS_SSL_DEBUG_RET(1, " mbedtls_x509_crt_parse_der", ret);
541 return ret;
Xiaofei Bai947571e2021-09-29 09:12:03 +0000542 }
543
544 p += cert_data_len;
545
546 /* Certificate extensions length */
Gilles Peskine449bd832023-01-11 14:50:10 +0100547 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, certificate_list_end, 2);
548 extensions_len = MBEDTLS_GET_UINT16_BE(p, 0);
Xiaofei Bai947571e2021-09-29 09:12:03 +0000549 p += 2;
Gilles Peskine449bd832023-01-11 14:50:10 +0100550 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, certificate_list_end, extensions_len);
Jerry Yu2eaa7602022-08-04 17:28:15 +0800551
552 extensions_end = p + extensions_len;
Jerry Yu0d5cfb72022-10-31 14:15:48 +0800553 handshake->received_extensions = MBEDTLS_SSL_EXT_MASK_NONE;
Jerry Yu2eaa7602022-08-04 17:28:15 +0800554
Gilles Peskine449bd832023-01-11 14:50:10 +0100555 while (p < extensions_end) {
Jerry Yu2eaa7602022-08-04 17:28:15 +0800556 unsigned int extension_type;
557 size_t extension_data_len;
558
559 /*
Gilles Peskine449bd832023-01-11 14:50:10 +0100560 * struct {
561 * ExtensionType extension_type; (2 bytes)
562 * opaque extension_data<0..2^16-1>;
563 * } Extension;
564 */
565 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, extensions_end, 4);
566 extension_type = MBEDTLS_GET_UINT16_BE(p, 0);
567 extension_data_len = MBEDTLS_GET_UINT16_BE(p, 2);
Jerry Yu2eaa7602022-08-04 17:28:15 +0800568 p += 4;
569
Gilles Peskine449bd832023-01-11 14:50:10 +0100570 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, extensions_end, extension_data_len);
Jerry Yu2eaa7602022-08-04 17:28:15 +0800571
Jerry Yuc4bf5d62022-10-29 09:08:47 +0800572 ret = mbedtls_ssl_tls13_check_received_extension(
Gilles Peskine449bd832023-01-11 14:50:10 +0100573 ssl, MBEDTLS_SSL_HS_CERTIFICATE, extension_type,
574 MBEDTLS_SSL_TLS1_3_ALLOWED_EXTS_OF_CT);
575 if (ret != 0) {
576 return ret;
577 }
Jerry Yu0c354a22022-08-29 15:25:36 +0800578
Gilles Peskine449bd832023-01-11 14:50:10 +0100579 switch (extension_type) {
Jerry Yu2eaa7602022-08-04 17:28:15 +0800580 default:
Jerry Yu79aa7212022-11-08 21:30:21 +0800581 MBEDTLS_SSL_PRINT_EXT(
Jerry Yu0d5cfb72022-10-31 14:15:48 +0800582 3, MBEDTLS_SSL_HS_CERTIFICATE,
Gilles Peskine449bd832023-01-11 14:50:10 +0100583 extension_type, "( ignored )");
Jerry Yu2eaa7602022-08-04 17:28:15 +0800584 break;
585 }
586
587 p += extension_data_len;
588 }
589
Gilles Peskine449bd832023-01-11 14:50:10 +0100590 MBEDTLS_SSL_PRINT_EXTS(3, MBEDTLS_SSL_HS_CERTIFICATE,
591 handshake->received_extensions);
Xiaofei Bai947571e2021-09-29 09:12:03 +0000592 }
593
XiaokangQian63e713e2022-05-15 04:26:57 +0000594exit:
Xiaofei Bai947571e2021-09-29 09:12:03 +0000595 /* Check that all the message is consumed. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100596 if (p != end) {
597 MBEDTLS_SSL_DEBUG_MSG(1, ("bad Certificate message"));
598 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR,
599 MBEDTLS_ERR_SSL_DECODE_ERROR);
600 return MBEDTLS_ERR_SSL_DECODE_ERROR;
Xiaofei Bai947571e2021-09-29 09:12:03 +0000601 }
602
Gilles Peskine449bd832023-01-11 14:50:10 +0100603 MBEDTLS_SSL_DEBUG_CRT(3, "peer certificate", ssl->session_negotiate->peer_cert);
Xiaofei Bai947571e2021-09-29 09:12:03 +0000604
Gilles Peskine449bd832023-01-11 14:50:10 +0100605 return ret;
Xiaofei Bai947571e2021-09-29 09:12:03 +0000606}
607#else
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200608MBEDTLS_CHECK_RETURN_CRITICAL
Ronald Crone3dac4a2022-06-10 17:21:51 +0200609MBEDTLS_STATIC_TESTABLE
Gilles Peskine449bd832023-01-11 14:50:10 +0100610int mbedtls_ssl_tls13_parse_certificate(mbedtls_ssl_context *ssl,
611 const unsigned char *buf,
612 const unsigned char *end)
Xiaofei Bai947571e2021-09-29 09:12:03 +0000613{
614 ((void) ssl);
615 ((void) buf);
616 ((void) end);
Gilles Peskine449bd832023-01-11 14:50:10 +0100617 return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Xiaofei Bai947571e2021-09-29 09:12:03 +0000618}
619#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Ronald Cronde08cf32022-10-04 17:15:35 +0200620#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED */
Xiaofei Bai947571e2021-09-29 09:12:03 +0000621
Ronald Cronde08cf32022-10-04 17:15:35 +0200622#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED)
Xiaofei Bai947571e2021-09-29 09:12:03 +0000623#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Xiaofei Bai79595ac2021-10-26 07:16:45 +0000624/* Validate certificate chain sent by the server. */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200625MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100626static int ssl_tls13_validate_certificate(mbedtls_ssl_context *ssl)
Xiaofei Bai947571e2021-09-29 09:12:03 +0000627{
628 int ret = 0;
XiaokangQian989f06d2022-05-17 01:50:15 +0000629 int authmode = MBEDTLS_SSL_VERIFY_REQUIRED;
Xiaofei Bai947571e2021-09-29 09:12:03 +0000630 mbedtls_x509_crt *ca_chain;
631 mbedtls_x509_crl *ca_crl;
Ronald Cron30c5a252022-06-16 19:31:06 +0200632 const char *ext_oid;
633 size_t ext_len;
Xiaofei Baiff456022021-10-28 06:50:17 +0000634 uint32_t verify_result = 0;
Xiaofei Bai947571e2021-09-29 09:12:03 +0000635
XiaokangQian6b916b12022-04-25 07:29:34 +0000636 /* If SNI was used, overwrite authentication mode
637 * from the configuration. */
XiaokangQian989f06d2022-05-17 01:50:15 +0000638#if defined(MBEDTLS_SSL_SRV_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100639 if (ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER) {
XiaokangQian0557c942022-05-30 08:10:53 +0000640#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Gilles Peskine449bd832023-01-11 14:50:10 +0100641 if (ssl->handshake->sni_authmode != MBEDTLS_SSL_VERIFY_UNSET) {
XiaokangQian0557c942022-05-30 08:10:53 +0000642 authmode = ssl->handshake->sni_authmode;
Gilles Peskine449bd832023-01-11 14:50:10 +0100643 } else
XiaokangQian0557c942022-05-30 08:10:53 +0000644#endif
Gilles Peskine449bd832023-01-11 14:50:10 +0100645 authmode = ssl->conf->authmode;
XiaokangQian0557c942022-05-30 08:10:53 +0000646 }
XiaokangQian6b916b12022-04-25 07:29:34 +0000647#endif
648
649 /*
XiaokangQian989f06d2022-05-17 01:50:15 +0000650 * If the peer hasn't sent a certificate ( i.e. it sent
XiaokangQian6b916b12022-04-25 07:29:34 +0000651 * an empty certificate chain ), this is reflected in the peer CRT
652 * structure being unset.
653 * Check for that and handle it depending on the
XiaokangQian989f06d2022-05-17 01:50:15 +0000654 * authentication mode.
XiaokangQian6b916b12022-04-25 07:29:34 +0000655 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100656 if (ssl->session_negotiate->peer_cert == NULL) {
657 MBEDTLS_SSL_DEBUG_MSG(1, ("peer has no certificate"));
XiaokangQian989f06d2022-05-17 01:50:15 +0000658
XiaokangQian63e713e2022-05-15 04:26:57 +0000659#if defined(MBEDTLS_SSL_SRV_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100660 if (ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER) {
XiaokangQian63e713e2022-05-15 04:26:57 +0000661 /* The client was asked for a certificate but didn't send
662 * one. The client should know what's going on, so we
663 * don't send an alert.
664 */
665 ssl->session_negotiate->verify_result = MBEDTLS_X509_BADCERT_MISSING;
Gilles Peskine449bd832023-01-11 14:50:10 +0100666 if (authmode == MBEDTLS_SSL_VERIFY_OPTIONAL) {
667 return 0;
668 } else {
669 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_NO_CERT,
670 MBEDTLS_ERR_SSL_NO_CLIENT_CERTIFICATE);
671 return MBEDTLS_ERR_SSL_NO_CLIENT_CERTIFICATE;
XiaokangQian989f06d2022-05-17 01:50:15 +0000672 }
XiaokangQian63e713e2022-05-15 04:26:57 +0000673 }
XiaokangQian6b916b12022-04-25 07:29:34 +0000674#endif /* MBEDTLS_SSL_SRV_C */
675
XiaokangQianc3017f62022-05-13 05:55:41 +0000676#if defined(MBEDTLS_SSL_CLI_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100677 if (ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT) {
678 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_NO_CERT,
679 MBEDTLS_ERR_SSL_FATAL_ALERT_MESSAGE);
680 return MBEDTLS_ERR_SSL_FATAL_ALERT_MESSAGE;
XiaokangQian63e713e2022-05-15 04:26:57 +0000681 }
XiaokangQianc3017f62022-05-13 05:55:41 +0000682#endif /* MBEDTLS_SSL_CLI_C */
XiaokangQian63e713e2022-05-15 04:26:57 +0000683 }
XiaokangQian6b916b12022-04-25 07:29:34 +0000684
Xiaofei Bai947571e2021-09-29 09:12:03 +0000685#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Gilles Peskine449bd832023-01-11 14:50:10 +0100686 if (ssl->handshake->sni_ca_chain != NULL) {
Xiaofei Bai947571e2021-09-29 09:12:03 +0000687 ca_chain = ssl->handshake->sni_ca_chain;
688 ca_crl = ssl->handshake->sni_ca_crl;
Gilles Peskine449bd832023-01-11 14:50:10 +0100689 } else
Xiaofei Bai947571e2021-09-29 09:12:03 +0000690#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
691 {
692 ca_chain = ssl->conf->ca_chain;
693 ca_crl = ssl->conf->ca_crl;
694 }
695
696 /*
697 * Main check: verify certificate
698 */
699 ret = mbedtls_x509_crt_verify_with_profile(
700 ssl->session_negotiate->peer_cert,
701 ca_chain, ca_crl,
702 ssl->conf->cert_profile,
703 ssl->hostname,
Xiaofei Baiff456022021-10-28 06:50:17 +0000704 &verify_result,
Gilles Peskine449bd832023-01-11 14:50:10 +0100705 ssl->conf->f_vrfy, ssl->conf->p_vrfy);
Xiaofei Bai947571e2021-09-29 09:12:03 +0000706
Gilles Peskine449bd832023-01-11 14:50:10 +0100707 if (ret != 0) {
708 MBEDTLS_SSL_DEBUG_RET(1, "x509_verify_cert", ret);
Xiaofei Bai947571e2021-09-29 09:12:03 +0000709 }
710
711 /*
712 * Secondary checks: always done, but change 'ret' only if it was 0
713 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100714 if (ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT) {
Ronald Cron30c5a252022-06-16 19:31:06 +0200715 ext_oid = MBEDTLS_OID_SERVER_AUTH;
Gilles Peskine449bd832023-01-11 14:50:10 +0100716 ext_len = MBEDTLS_OID_SIZE(MBEDTLS_OID_SERVER_AUTH);
717 } else {
Ronald Cron30c5a252022-06-16 19:31:06 +0200718 ext_oid = MBEDTLS_OID_CLIENT_AUTH;
Gilles Peskine449bd832023-01-11 14:50:10 +0100719 ext_len = MBEDTLS_OID_SIZE(MBEDTLS_OID_CLIENT_AUTH);
Ronald Cron30c5a252022-06-16 19:31:06 +0200720 }
721
Gilles Peskine449bd832023-01-11 14:50:10 +0100722 if ((mbedtls_x509_crt_check_key_usage(
723 ssl->session_negotiate->peer_cert,
724 MBEDTLS_X509_KU_DIGITAL_SIGNATURE) != 0) ||
725 (mbedtls_x509_crt_check_extended_key_usage(
726 ssl->session_negotiate->peer_cert,
727 ext_oid, ext_len) != 0)) {
728 MBEDTLS_SSL_DEBUG_MSG(1, ("bad certificate (usage extensions)"));
729 if (ret == 0) {
Xiaofei Bai947571e2021-09-29 09:12:03 +0000730 ret = MBEDTLS_ERR_SSL_BAD_CERTIFICATE;
Gilles Peskine449bd832023-01-11 14:50:10 +0100731 }
Xiaofei Bai947571e2021-09-29 09:12:03 +0000732 }
733
XiaokangQian6b916b12022-04-25 07:29:34 +0000734 /* mbedtls_x509_crt_verify_with_profile is supposed to report a
735 * verification failure through MBEDTLS_ERR_X509_CERT_VERIFY_FAILED,
736 * with details encoded in the verification flags. All other kinds
737 * of error codes, including those from the user provided f_vrfy
738 * functions, are treated as fatal and lead to a failure of
Ronald Crone3dac4a2022-06-10 17:21:51 +0200739 * mbedtls_ssl_tls13_parse_certificate even if verification was optional.
740 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100741 if (authmode == MBEDTLS_SSL_VERIFY_OPTIONAL &&
742 (ret == MBEDTLS_ERR_X509_CERT_VERIFY_FAILED ||
743 ret == MBEDTLS_ERR_SSL_BAD_CERTIFICATE)) {
XiaokangQian6b916b12022-04-25 07:29:34 +0000744 ret = 0;
745 }
Xiaofei Bai947571e2021-09-29 09:12:03 +0000746
Gilles Peskine449bd832023-01-11 14:50:10 +0100747 if (ca_chain == NULL && authmode == MBEDTLS_SSL_VERIFY_REQUIRED) {
748 MBEDTLS_SSL_DEBUG_MSG(1, ("got no CA chain"));
Xiaofei Bai947571e2021-09-29 09:12:03 +0000749 ret = MBEDTLS_ERR_SSL_CA_CHAIN_REQUIRED;
750 }
751
Gilles Peskine449bd832023-01-11 14:50:10 +0100752 if (ret != 0) {
Xiaofei Bai947571e2021-09-29 09:12:03 +0000753 /* The certificate may have been rejected for several reasons.
754 Pick one and send the corresponding alert. Which alert to send
755 may be a subject of debate in some cases. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100756 if (verify_result & MBEDTLS_X509_BADCERT_OTHER) {
757 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED, ret);
758 } else if (verify_result & MBEDTLS_X509_BADCERT_CN_MISMATCH) {
759 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_BAD_CERT, ret);
760 } else if (verify_result & (MBEDTLS_X509_BADCERT_KEY_USAGE |
761 MBEDTLS_X509_BADCERT_EXT_KEY_USAGE |
762 MBEDTLS_X509_BADCERT_NS_CERT_TYPE |
763 MBEDTLS_X509_BADCERT_BAD_PK |
764 MBEDTLS_X509_BADCERT_BAD_KEY)) {
765 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT, ret);
766 } else if (verify_result & MBEDTLS_X509_BADCERT_EXPIRED) {
767 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_CERT_EXPIRED, ret);
768 } else if (verify_result & MBEDTLS_X509_BADCERT_REVOKED) {
769 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_CERT_REVOKED, ret);
770 } else if (verify_result & MBEDTLS_X509_BADCERT_NOT_TRUSTED) {
771 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_UNKNOWN_CA, ret);
772 } else {
773 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_CERT_UNKNOWN, ret);
774 }
Xiaofei Bai947571e2021-09-29 09:12:03 +0000775 }
776
777#if defined(MBEDTLS_DEBUG_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100778 if (verify_result != 0) {
779 MBEDTLS_SSL_DEBUG_MSG(3, ("! Certificate verification flags %08x",
780 (unsigned int) verify_result));
781 } else {
782 MBEDTLS_SSL_DEBUG_MSG(3, ("Certificate verification flags clear"));
Xiaofei Bai947571e2021-09-29 09:12:03 +0000783 }
784#endif /* MBEDTLS_DEBUG_C */
785
Xiaofei Baiff456022021-10-28 06:50:17 +0000786 ssl->session_negotiate->verify_result = verify_result;
Gilles Peskine449bd832023-01-11 14:50:10 +0100787 return ret;
Xiaofei Bai947571e2021-09-29 09:12:03 +0000788}
789#else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200790MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100791static int ssl_tls13_validate_certificate(mbedtls_ssl_context *ssl)
Xiaofei Bai947571e2021-09-29 09:12:03 +0000792{
793 ((void) ssl);
Gilles Peskine449bd832023-01-11 14:50:10 +0100794 return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Xiaofei Bai947571e2021-09-29 09:12:03 +0000795}
796#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Ronald Cronde08cf32022-10-04 17:15:35 +0200797#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED */
Xiaofei Bai947571e2021-09-29 09:12:03 +0000798
Gilles Peskine449bd832023-01-11 14:50:10 +0100799int mbedtls_ssl_tls13_process_certificate(mbedtls_ssl_context *ssl)
Xiaofei Bai947571e2021-09-29 09:12:03 +0000800{
Xiaofei Bai79595ac2021-10-26 07:16:45 +0000801 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine449bd832023-01-11 14:50:10 +0100802 MBEDTLS_SSL_DEBUG_MSG(2, ("=> parse certificate"));
Xiaofei Bai79595ac2021-10-26 07:16:45 +0000803
Ronald Cronde08cf32022-10-04 17:15:35 +0200804#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED)
XiaokangQianc3017f62022-05-13 05:55:41 +0000805 unsigned char *buf;
806 size_t buf_len;
Xiaofei Bai79595ac2021-10-26 07:16:45 +0000807
Gilles Peskine449bd832023-01-11 14:50:10 +0100808 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_tls13_fetch_handshake_msg(
809 ssl, MBEDTLS_SSL_HS_CERTIFICATE,
810 &buf, &buf_len));
Xiaofei Bai79595ac2021-10-26 07:16:45 +0000811
XiaokangQianc3017f62022-05-13 05:55:41 +0000812 /* Parse the certificate chain sent by the peer. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100813 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_tls13_parse_certificate(ssl, buf,
814 buf + buf_len));
XiaokangQianc3017f62022-05-13 05:55:41 +0000815 /* Validate the certificate chain and set the verification results. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100816 MBEDTLS_SSL_PROC_CHK(ssl_tls13_validate_certificate(ssl));
Xiaofei Bai79595ac2021-10-26 07:16:45 +0000817
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +0100818 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_add_hs_msg_to_checksum(ssl,
Manuel Pégourié-Gonnard43cc1272023-02-06 11:48:19 +0100819 MBEDTLS_SSL_HS_CERTIFICATE, buf,
820 buf_len));
Xiaofei Bai79595ac2021-10-26 07:16:45 +0000821
Xiaofei Bai79595ac2021-10-26 07:16:45 +0000822cleanup:
Ronald Cronde08cf32022-10-04 17:15:35 +0200823#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED */
Xiaofei Bai79595ac2021-10-26 07:16:45 +0000824
Gilles Peskine449bd832023-01-11 14:50:10 +0100825 MBEDTLS_SSL_DEBUG_MSG(2, ("<= parse certificate"));
826 return ret;
Xiaofei Bai947571e2021-09-29 09:12:03 +0000827}
Ronald Cron928cbd32022-10-04 16:14:26 +0200828#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED)
Jerry Yu7399d0d2022-01-30 17:54:19 +0800829/*
830 * enum {
831 * X509(0),
832 * RawPublicKey(2),
833 * (255)
834 * } CertificateType;
835 *
836 * struct {
837 * select (certificate_type) {
838 * case RawPublicKey:
839 * // From RFC 7250 ASN.1_subjectPublicKeyInfo
840 * opaque ASN1_subjectPublicKeyInfo<1..2^24-1>;
841 *
842 * case X509:
843 * opaque cert_data<1..2^24-1>;
844 * };
845 * Extension extensions<0..2^16-1>;
846 * } CertificateEntry;
847 *
848 * struct {
849 * opaque certificate_request_context<0..2^8-1>;
850 * CertificateEntry certificate_list<0..2^24-1>;
851 * } Certificate;
852 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200853MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100854static int ssl_tls13_write_certificate_body(mbedtls_ssl_context *ssl,
855 unsigned char *buf,
856 unsigned char *end,
857 size_t *out_len)
Jerry Yu5cc35062022-01-28 16:16:08 +0800858{
Gilles Peskine449bd832023-01-11 14:50:10 +0100859 const mbedtls_x509_crt *crt = mbedtls_ssl_own_cert(ssl);
Jerry Yu3e536442022-02-15 11:05:59 +0800860 unsigned char *p = buf;
Jerry Yuc8d8d4e2022-02-18 12:10:03 +0800861 unsigned char *certificate_request_context =
Gilles Peskine449bd832023-01-11 14:50:10 +0100862 ssl->handshake->certificate_request_context;
Jerry Yuc8d8d4e2022-02-18 12:10:03 +0800863 unsigned char certificate_request_context_len =
Gilles Peskine449bd832023-01-11 14:50:10 +0100864 ssl->handshake->certificate_request_context_len;
Jerry Yuc8d8d4e2022-02-18 12:10:03 +0800865 unsigned char *p_certificate_list_len;
Jerry Yu5cc35062022-01-28 16:16:08 +0800866
Jerry Yu5cc35062022-01-28 16:16:08 +0800867
Jerry Yu3391ac02022-02-16 11:21:37 +0800868 /* ...
869 * opaque certificate_request_context<0..2^8-1>;
870 * ...
871 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100872 MBEDTLS_SSL_CHK_BUF_PTR(p, end, certificate_request_context_len + 1);
Jerry Yuc8d8d4e2022-02-18 12:10:03 +0800873 *p++ = certificate_request_context_len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100874 if (certificate_request_context_len > 0) {
875 memcpy(p, certificate_request_context, certificate_request_context_len);
Jerry Yuc8d8d4e2022-02-18 12:10:03 +0800876 p += certificate_request_context_len;
Jerry Yu537530d2022-02-15 14:00:57 +0800877 }
878
Jerry Yu3391ac02022-02-16 11:21:37 +0800879 /* ...
880 * CertificateEntry certificate_list<0..2^24-1>;
881 * ...
882 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100883 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 3);
Jerry Yuc8d8d4e2022-02-18 12:10:03 +0800884 p_certificate_list_len = p;
Jerry Yu3e536442022-02-15 11:05:59 +0800885 p += 3;
886
Gilles Peskine449bd832023-01-11 14:50:10 +0100887 MBEDTLS_SSL_DEBUG_CRT(3, "own certificate", crt);
Jerry Yu5cc35062022-01-28 16:16:08 +0800888
Gilles Peskine449bd832023-01-11 14:50:10 +0100889 while (crt != NULL) {
Jerry Yu7399d0d2022-01-30 17:54:19 +0800890 size_t cert_data_len = crt->raw.len;
Jerry Yu5cc35062022-01-28 16:16:08 +0800891
Gilles Peskine449bd832023-01-11 14:50:10 +0100892 MBEDTLS_SSL_CHK_BUF_PTR(p, end, cert_data_len + 3 + 2);
893 MBEDTLS_PUT_UINT24_BE(cert_data_len, p, 0);
Jerry Yu7399d0d2022-01-30 17:54:19 +0800894 p += 3;
Jerry Yu5cc35062022-01-28 16:16:08 +0800895
Gilles Peskine449bd832023-01-11 14:50:10 +0100896 memcpy(p, crt->raw.p, cert_data_len);
Jerry Yu7399d0d2022-01-30 17:54:19 +0800897 p += cert_data_len;
898 crt = crt->next;
Jerry Yu5cc35062022-01-28 16:16:08 +0800899
900 /* Currently, we don't have any certificate extensions defined.
901 * Hence, we are sending an empty extension with length zero.
902 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100903 MBEDTLS_PUT_UINT16_BE(0, p, 0);
Jerry Yu7399d0d2022-01-30 17:54:19 +0800904 p += 2;
Jerry Yu5cc35062022-01-28 16:16:08 +0800905 }
Jerry Yu5cc35062022-01-28 16:16:08 +0800906
Gilles Peskine449bd832023-01-11 14:50:10 +0100907 MBEDTLS_PUT_UINT24_BE(p - p_certificate_list_len - 3,
908 p_certificate_list_len, 0);
Jerry Yu7399d0d2022-01-30 17:54:19 +0800909
Jerry Yu3e536442022-02-15 11:05:59 +0800910 *out_len = p - buf;
Jerry Yu5cc35062022-01-28 16:16:08 +0800911
Jerry Yu7de2ff02022-11-08 21:43:46 +0800912 MBEDTLS_SSL_PRINT_EXTS(
Gilles Peskine449bd832023-01-11 14:50:10 +0100913 3, MBEDTLS_SSL_HS_CERTIFICATE, ssl->handshake->sent_extensions);
Jerry Yu4b8f2f72022-10-31 13:31:22 +0800914
Gilles Peskine449bd832023-01-11 14:50:10 +0100915 return 0;
Jerry Yu5cc35062022-01-28 16:16:08 +0800916}
Jerry Yu5cc35062022-01-28 16:16:08 +0800917
Gilles Peskine449bd832023-01-11 14:50:10 +0100918int mbedtls_ssl_tls13_write_certificate(mbedtls_ssl_context *ssl)
Jerry Yu5cc35062022-01-28 16:16:08 +0800919{
920 int ret;
Ronald Cron5bb8fc82022-03-09 07:00:13 +0100921 unsigned char *buf;
922 size_t buf_len, msg_len;
923
Gilles Peskine449bd832023-01-11 14:50:10 +0100924 MBEDTLS_SSL_DEBUG_MSG(2, ("=> write certificate"));
Jerry Yu5cc35062022-01-28 16:16:08 +0800925
Gilles Peskine449bd832023-01-11 14:50:10 +0100926 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_start_handshake_msg(ssl,
927 MBEDTLS_SSL_HS_CERTIFICATE, &buf,
928 &buf_len));
Jerry Yu5cc35062022-01-28 16:16:08 +0800929
Gilles Peskine449bd832023-01-11 14:50:10 +0100930 MBEDTLS_SSL_PROC_CHK(ssl_tls13_write_certificate_body(ssl,
931 buf,
932 buf + buf_len,
933 &msg_len));
Jerry Yu5cc35062022-01-28 16:16:08 +0800934
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +0100935 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_add_hs_msg_to_checksum(ssl,
Manuel Pégourié-Gonnard43cc1272023-02-06 11:48:19 +0100936 MBEDTLS_SSL_HS_CERTIFICATE, buf,
937 msg_len));
Jerry Yu5cc35062022-01-28 16:16:08 +0800938
Gilles Peskine449bd832023-01-11 14:50:10 +0100939 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_finish_handshake_msg(
940 ssl, buf_len, msg_len));
Jerry Yu5cc35062022-01-28 16:16:08 +0800941cleanup:
942
Gilles Peskine449bd832023-01-11 14:50:10 +0100943 MBEDTLS_SSL_DEBUG_MSG(2, ("<= write certificate"));
944 return ret;
Jerry Yu5cc35062022-01-28 16:16:08 +0800945}
946
Jerry Yu3e536442022-02-15 11:05:59 +0800947/*
948 * STATE HANDLING: Output Certificate Verify
949 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100950int mbedtls_ssl_tls13_check_sig_alg_cert_key_match(uint16_t sig_alg,
951 mbedtls_pk_context *key)
Jerry Yu0c6be8f2022-06-20 20:42:00 +0800952{
Gilles Peskine449bd832023-01-11 14:50:10 +0100953 mbedtls_pk_type_t pk_type = mbedtls_ssl_sig_from_pk(key);
954 size_t key_size = mbedtls_pk_get_bitlen(key);
Jerry Yu0c6be8f2022-06-20 20:42:00 +0800955
Gilles Peskine449bd832023-01-11 14:50:10 +0100956 switch (pk_type) {
Jerry Yu67eced02022-02-25 13:37:36 +0800957 case MBEDTLS_SSL_SIG_ECDSA:
Gilles Peskine449bd832023-01-11 14:50:10 +0100958 switch (key_size) {
Jerry Yu67eced02022-02-25 13:37:36 +0800959 case 256:
Gilles Peskine449bd832023-01-11 14:50:10 +0100960 return
961 sig_alg == MBEDTLS_TLS1_3_SIG_ECDSA_SECP256R1_SHA256;
Jerry Yu0c6be8f2022-06-20 20:42:00 +0800962
Jerry Yu67eced02022-02-25 13:37:36 +0800963 case 384:
Gilles Peskine449bd832023-01-11 14:50:10 +0100964 return
965 sig_alg == MBEDTLS_TLS1_3_SIG_ECDSA_SECP384R1_SHA384;
Jerry Yu0c6be8f2022-06-20 20:42:00 +0800966
Jerry Yu67eced02022-02-25 13:37:36 +0800967 case 521:
Gilles Peskine449bd832023-01-11 14:50:10 +0100968 return
969 sig_alg == MBEDTLS_TLS1_3_SIG_ECDSA_SECP521R1_SHA512;
Jerry Yu67eced02022-02-25 13:37:36 +0800970 default:
Jerry Yu67eced02022-02-25 13:37:36 +0800971 break;
972 }
973 break;
Jerry Yu67eced02022-02-25 13:37:36 +0800974
Jerry Yu67eced02022-02-25 13:37:36 +0800975 case MBEDTLS_SSL_SIG_RSA:
Gilles Peskine449bd832023-01-11 14:50:10 +0100976 switch (sig_alg) {
Ronald Cron38391bf2022-09-16 11:19:27 +0200977 case MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA256: /* Intentional fallthrough */
978 case MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA384: /* Intentional fallthrough */
Jerry Yu0c6be8f2022-06-20 20:42:00 +0800979 case MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA512:
Gilles Peskine449bd832023-01-11 14:50:10 +0100980 return 1;
Jerry Yuc2e04932022-06-27 22:13:03 +0800981
Jerry Yu0c6be8f2022-06-20 20:42:00 +0800982 default:
983 break;
Jerry Yucef3f332022-03-22 23:00:13 +0800984 }
Jerry Yu67eced02022-02-25 13:37:36 +0800985 break;
Jerry Yu0c6be8f2022-06-20 20:42:00 +0800986
Jerry Yu67eced02022-02-25 13:37:36 +0800987 default:
Jerry Yu67eced02022-02-25 13:37:36 +0800988 break;
989 }
Jerry Yu0c6be8f2022-06-20 20:42:00 +0800990
Gilles Peskine449bd832023-01-11 14:50:10 +0100991 return 0;
Jerry Yu0c6be8f2022-06-20 20:42:00 +0800992}
993
Ronald Cronce7d76e2022-07-08 18:56:49 +0200994MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100995static int ssl_tls13_write_certificate_verify_body(mbedtls_ssl_context *ssl,
996 unsigned char *buf,
997 unsigned char *end,
998 size_t *out_len)
Jerry Yu8511f122022-01-29 10:01:04 +0800999{
Ronald Cron067a1e72022-09-16 13:44:49 +02001000 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yu3e536442022-02-15 11:05:59 +08001001 unsigned char *p = buf;
Jerry Yu8511f122022-01-29 10:01:04 +08001002 mbedtls_pk_context *own_key;
Jerry Yu3e536442022-02-15 11:05:59 +08001003
Gilles Peskine449bd832023-01-11 14:50:10 +01001004 unsigned char handshake_hash[MBEDTLS_TLS1_3_MD_MAX_SIZE];
Jerry Yu8511f122022-01-29 10:01:04 +08001005 size_t handshake_hash_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01001006 unsigned char verify_buffer[SSL_VERIFY_STRUCT_MAX_SIZE];
Jerry Yu3e536442022-02-15 11:05:59 +08001007 size_t verify_buffer_len;
Ronald Cron067a1e72022-09-16 13:44:49 +02001008
1009 uint16_t *sig_alg = ssl->handshake->received_sig_algs;
Jerry Yu3e536442022-02-15 11:05:59 +08001010 size_t signature_len = 0;
Jerry Yu8511f122022-01-29 10:01:04 +08001011
Jerry Yu0b7b1012022-02-23 12:23:05 +08001012 *out_len = 0;
1013
Gilles Peskine449bd832023-01-11 14:50:10 +01001014 own_key = mbedtls_ssl_own_key(ssl);
1015 if (own_key == NULL) {
1016 MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
1017 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Jerry Yu8511f122022-01-29 10:01:04 +08001018 }
1019
Gilles Peskine449bd832023-01-11 14:50:10 +01001020 ret = mbedtls_ssl_get_handshake_transcript(ssl,
1021 ssl->handshake->ciphersuite_info->mac,
1022 handshake_hash,
1023 sizeof(handshake_hash),
1024 &handshake_hash_len);
1025 if (ret != 0) {
1026 return ret;
1027 }
Jerry Yu8511f122022-01-29 10:01:04 +08001028
Gilles Peskine449bd832023-01-11 14:50:10 +01001029 MBEDTLS_SSL_DEBUG_BUF(3, "handshake hash",
1030 handshake_hash,
1031 handshake_hash_len);
Jerry Yu8511f122022-01-29 10:01:04 +08001032
Gilles Peskine449bd832023-01-11 14:50:10 +01001033 ssl_tls13_create_verify_structure(handshake_hash, handshake_hash_len,
1034 verify_buffer, &verify_buffer_len,
1035 ssl->conf->endpoint);
Jerry Yu8511f122022-01-29 10:01:04 +08001036
1037 /*
1038 * struct {
1039 * SignatureScheme algorithm;
1040 * opaque signature<0..2^16-1>;
1041 * } CertificateVerify;
1042 */
Ronald Cron067a1e72022-09-16 13:44:49 +02001043 /* Check there is space for the algorithm identifier (2 bytes) and the
1044 * signature length (2 bytes).
1045 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001046 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 4);
Ronald Cron067a1e72022-09-16 13:44:49 +02001047
Gilles Peskine449bd832023-01-11 14:50:10 +01001048 for (; *sig_alg != MBEDTLS_TLS1_3_SIG_NONE; sig_alg++) {
Ronald Cron067a1e72022-09-16 13:44:49 +02001049 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
1050 mbedtls_pk_type_t pk_type = MBEDTLS_PK_NONE;
1051 mbedtls_md_type_t md_alg = MBEDTLS_MD_NONE;
1052 psa_algorithm_t psa_algorithm = PSA_ALG_NONE;
1053 unsigned char verify_hash[PSA_HASH_MAX_SIZE];
1054 size_t verify_hash_len;
Jerry Yu67eced02022-02-25 13:37:36 +08001055
Gilles Peskine449bd832023-01-11 14:50:10 +01001056 if (!mbedtls_ssl_sig_alg_is_offered(ssl, *sig_alg)) {
Ronald Cron067a1e72022-09-16 13:44:49 +02001057 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +01001058 }
Jerry Yu67eced02022-02-25 13:37:36 +08001059
Gilles Peskine449bd832023-01-11 14:50:10 +01001060 if (!mbedtls_ssl_tls13_sig_alg_for_cert_verify_is_supported(*sig_alg)) {
Ronald Cron067a1e72022-09-16 13:44:49 +02001061 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +01001062 }
Ronald Cron067a1e72022-09-16 13:44:49 +02001063
Gilles Peskine449bd832023-01-11 14:50:10 +01001064 if (!mbedtls_ssl_tls13_check_sig_alg_cert_key_match(*sig_alg, own_key)) {
Ronald Cron067a1e72022-09-16 13:44:49 +02001065 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +01001066 }
Ronald Cron067a1e72022-09-16 13:44:49 +02001067
Gilles Peskine449bd832023-01-11 14:50:10 +01001068 if (mbedtls_ssl_get_pk_type_and_md_alg_from_sig_alg(
1069 *sig_alg, &pk_type, &md_alg) != 0) {
1070 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Ronald Cron067a1e72022-09-16 13:44:49 +02001071 }
1072
1073 /* Hash verify buffer with indicated hash function */
Gilles Peskine449bd832023-01-11 14:50:10 +01001074 psa_algorithm = mbedtls_hash_info_psa_from_md(md_alg);
1075 status = psa_hash_compute(psa_algorithm,
1076 verify_buffer,
1077 verify_buffer_len,
1078 verify_hash, sizeof(verify_hash),
1079 &verify_hash_len);
1080 if (status != PSA_SUCCESS) {
Andrzej Kurek8a045ce2022-12-23 11:00:06 -05001081 return PSA_TO_MBEDTLS_ERR(status);
Ronald Cron067a1e72022-09-16 13:44:49 +02001082 }
1083
Gilles Peskine449bd832023-01-11 14:50:10 +01001084 MBEDTLS_SSL_DEBUG_BUF(3, "verify hash", verify_hash, verify_hash_len);
1085
1086 if ((ret = mbedtls_pk_sign_ext(pk_type, own_key,
1087 md_alg, verify_hash, verify_hash_len,
1088 p + 4, (size_t) (end - (p + 4)), &signature_len,
1089 ssl->conf->f_rng, ssl->conf->p_rng)) != 0) {
1090 MBEDTLS_SSL_DEBUG_MSG(2, ("CertificateVerify signature failed with %s",
1091 mbedtls_ssl_sig_alg_to_str(*sig_alg)));
1092 MBEDTLS_SSL_DEBUG_RET(2, "mbedtls_pk_sign_ext", ret);
1093
1094 /* The signature failed. This is possible if the private key
1095 * was not suitable for the signature operation as purposely we
1096 * did not check its suitability completely. Let's try with
1097 * another signature algorithm.
1098 */
1099 continue;
1100 }
1101
1102 MBEDTLS_SSL_DEBUG_MSG(2, ("CertificateVerify signature with %s",
1103 mbedtls_ssl_sig_alg_to_str(*sig_alg)));
Ronald Cron067a1e72022-09-16 13:44:49 +02001104
1105 break;
1106 }
1107
Gilles Peskine449bd832023-01-11 14:50:10 +01001108 if (*sig_alg == MBEDTLS_TLS1_3_SIG_NONE) {
1109 MBEDTLS_SSL_DEBUG_MSG(1, ("no suitable signature algorithm"));
1110 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE,
1111 MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE);
1112 return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
Jerry Yu8511f122022-01-29 10:01:04 +08001113 }
1114
Gilles Peskine449bd832023-01-11 14:50:10 +01001115 MBEDTLS_PUT_UINT16_BE(*sig_alg, p, 0);
1116 MBEDTLS_PUT_UINT16_BE(signature_len, p, 2);
Jerry Yuf3b46b52022-06-19 16:52:27 +08001117
Ronald Cron067a1e72022-09-16 13:44:49 +02001118 *out_len = 4 + signature_len;
Jerry Yu8c338862022-03-23 13:34:04 +08001119
Gilles Peskine449bd832023-01-11 14:50:10 +01001120 return 0;
Jerry Yu8511f122022-01-29 10:01:04 +08001121}
Jerry Yu8511f122022-01-29 10:01:04 +08001122
Gilles Peskine449bd832023-01-11 14:50:10 +01001123int mbedtls_ssl_tls13_write_certificate_verify(mbedtls_ssl_context *ssl)
Jerry Yu8511f122022-01-29 10:01:04 +08001124{
1125 int ret = 0;
Jerry Yuca133a32022-02-15 14:22:05 +08001126 unsigned char *buf;
1127 size_t buf_len, msg_len;
1128
Gilles Peskine449bd832023-01-11 14:50:10 +01001129 MBEDTLS_SSL_DEBUG_MSG(2, ("=> write certificate verify"));
Jerry Yu8511f122022-01-29 10:01:04 +08001130
Gilles Peskine449bd832023-01-11 14:50:10 +01001131 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_start_handshake_msg(ssl,
1132 MBEDTLS_SSL_HS_CERTIFICATE_VERIFY, &buf,
1133 &buf_len));
Jerry Yu8511f122022-01-29 10:01:04 +08001134
Gilles Peskine449bd832023-01-11 14:50:10 +01001135 MBEDTLS_SSL_PROC_CHK(ssl_tls13_write_certificate_verify_body(
1136 ssl, buf, buf + buf_len, &msg_len));
Jerry Yu8511f122022-01-29 10:01:04 +08001137
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +01001138 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_add_hs_msg_to_checksum(ssl,
Manuel Pégourié-Gonnard43cc1272023-02-06 11:48:19 +01001139 MBEDTLS_SSL_HS_CERTIFICATE_VERIFY, buf,
1140 msg_len));
Jerry Yu8511f122022-01-29 10:01:04 +08001141
Gilles Peskine449bd832023-01-11 14:50:10 +01001142 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_finish_handshake_msg(
1143 ssl, buf_len, msg_len));
Jerry Yu8511f122022-01-29 10:01:04 +08001144
1145cleanup:
1146
Gilles Peskine449bd832023-01-11 14:50:10 +01001147 MBEDTLS_SSL_DEBUG_MSG(2, ("<= write certificate verify"));
1148 return ret;
Jerry Yu8511f122022-01-29 10:01:04 +08001149}
1150
Ronald Cron928cbd32022-10-04 16:14:26 +02001151#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED */
Jerry Yu90f152d2022-01-29 22:12:42 +08001152
Jerry Yu5cc35062022-01-28 16:16:08 +08001153/*
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001154 *
XiaokangQianc5c39d52021-11-09 11:55:10 +00001155 * STATE HANDLING: Incoming Finished message.
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001156 */
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001157/*
1158 * Implementation
1159 */
1160
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001161MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01001162static int ssl_tls13_preprocess_finished_message(mbedtls_ssl_context *ssl)
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001163{
1164 int ret;
1165
Gilles Peskine449bd832023-01-11 14:50:10 +01001166 ret = mbedtls_ssl_tls13_calculate_verify_data(ssl,
1167 ssl->handshake->state_local.finished_in.digest,
1168 sizeof(ssl->handshake->state_local.finished_in.
1169 digest),
1170 &ssl->handshake->state_local.finished_in.digest_len,
1171 ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT ?
1172 MBEDTLS_SSL_IS_SERVER : MBEDTLS_SSL_IS_CLIENT);
1173 if (ret != 0) {
1174 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_tls13_calculate_verify_data", ret);
1175 return ret;
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001176 }
1177
Gilles Peskine449bd832023-01-11 14:50:10 +01001178 return 0;
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001179}
1180
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001181MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01001182static int ssl_tls13_parse_finished_message(mbedtls_ssl_context *ssl,
1183 const unsigned char *buf,
1184 const unsigned char *end)
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001185{
XiaokangQian33062842021-11-11 03:37:45 +00001186 /*
1187 * struct {
XiaokangQianc13f9352021-11-11 06:13:22 +00001188 * opaque verify_data[Hash.length];
XiaokangQian33062842021-11-11 03:37:45 +00001189 * } Finished;
1190 */
1191 const unsigned char *expected_verify_data =
1192 ssl->handshake->state_local.finished_in.digest;
1193 size_t expected_verify_data_len =
1194 ssl->handshake->state_local.finished_in.digest_len;
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001195 /* Structural validation */
Gilles Peskine449bd832023-01-11 14:50:10 +01001196 if ((size_t) (end - buf) != expected_verify_data_len) {
1197 MBEDTLS_SSL_DEBUG_MSG(1, ("bad finished message"));
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001198
Gilles Peskine449bd832023-01-11 14:50:10 +01001199 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR,
1200 MBEDTLS_ERR_SSL_DECODE_ERROR);
1201 return MBEDTLS_ERR_SSL_DECODE_ERROR;
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001202 }
1203
Gilles Peskine449bd832023-01-11 14:50:10 +01001204 MBEDTLS_SSL_DEBUG_BUF(4, "verify_data (self-computed):",
1205 expected_verify_data,
1206 expected_verify_data_len);
1207 MBEDTLS_SSL_DEBUG_BUF(4, "verify_data (received message):", buf,
1208 expected_verify_data_len);
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001209
1210 /* Semantic validation */
Gilles Peskine449bd832023-01-11 14:50:10 +01001211 if (mbedtls_ct_memcmp(buf,
1212 expected_verify_data,
1213 expected_verify_data_len) != 0) {
1214 MBEDTLS_SSL_DEBUG_MSG(1, ("bad finished message"));
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001215
Gilles Peskine449bd832023-01-11 14:50:10 +01001216 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_DECRYPT_ERROR,
1217 MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE);
1218 return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001219 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001220 return 0;
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001221}
1222
Gilles Peskine449bd832023-01-11 14:50:10 +01001223int mbedtls_ssl_tls13_process_finished_message(mbedtls_ssl_context *ssl)
XiaokangQianc5c39d52021-11-09 11:55:10 +00001224{
XiaokangQian33062842021-11-11 03:37:45 +00001225 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
XiaokangQianc5c39d52021-11-09 11:55:10 +00001226 unsigned char *buf;
Xiaofei Baieef15042021-11-18 07:29:56 +00001227 size_t buf_len;
XiaokangQianc5c39d52021-11-09 11:55:10 +00001228
Gilles Peskine449bd832023-01-11 14:50:10 +01001229 MBEDTLS_SSL_DEBUG_MSG(2, ("=> parse finished message"));
XiaokangQianc5c39d52021-11-09 11:55:10 +00001230
Gilles Peskine449bd832023-01-11 14:50:10 +01001231 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_tls13_fetch_handshake_msg(ssl,
1232 MBEDTLS_SSL_HS_FINISHED,
1233 &buf, &buf_len));
Jerry Yu0a92d6c2022-05-16 16:54:46 +08001234
1235 /* Preprocessing step: Compute handshake digest */
Gilles Peskine449bd832023-01-11 14:50:10 +01001236 MBEDTLS_SSL_PROC_CHK(ssl_tls13_preprocess_finished_message(ssl));
Jerry Yu0a92d6c2022-05-16 16:54:46 +08001237
Gilles Peskine449bd832023-01-11 14:50:10 +01001238 MBEDTLS_SSL_PROC_CHK(ssl_tls13_parse_finished_message(ssl, buf, buf + buf_len));
Jerry Yu0a92d6c2022-05-16 16:54:46 +08001239
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +01001240 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_add_hs_msg_to_checksum(ssl,
Manuel Pégourié-Gonnard43cc1272023-02-06 11:48:19 +01001241 MBEDTLS_SSL_HS_FINISHED, buf, buf_len));
XiaokangQianc5c39d52021-11-09 11:55:10 +00001242
1243cleanup:
1244
Gilles Peskine449bd832023-01-11 14:50:10 +01001245 MBEDTLS_SSL_DEBUG_MSG(2, ("<= parse finished message"));
1246 return ret;
XiaokangQianc5c39d52021-11-09 11:55:10 +00001247}
1248
XiaokangQian74af2a82021-09-22 07:40:30 +00001249/*
1250 *
XiaokangQiancc90c942021-11-09 12:30:09 +00001251 * STATE HANDLING: Write and send Finished message.
XiaokangQian74af2a82021-09-22 07:40:30 +00001252 *
1253 */
XiaokangQian74af2a82021-09-22 07:40:30 +00001254/*
XiaokangQian35dc6252021-11-11 08:16:19 +00001255 * Implement
XiaokangQian74af2a82021-09-22 07:40:30 +00001256 */
1257
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001258MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01001259static int ssl_tls13_prepare_finished_message(mbedtls_ssl_context *ssl)
XiaokangQian74af2a82021-09-22 07:40:30 +00001260{
1261 int ret;
1262
1263 /* Compute transcript of handshake up to now. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001264 ret = mbedtls_ssl_tls13_calculate_verify_data(ssl,
1265 ssl->handshake->state_local.finished_out.digest,
1266 sizeof(ssl->handshake->state_local.finished_out.
1267 digest),
1268 &ssl->handshake->state_local.finished_out.digest_len,
1269 ssl->conf->endpoint);
XiaokangQian74af2a82021-09-22 07:40:30 +00001270
Gilles Peskine449bd832023-01-11 14:50:10 +01001271 if (ret != 0) {
1272 MBEDTLS_SSL_DEBUG_RET(1, "calculate_verify_data failed", ret);
1273 return ret;
XiaokangQian74af2a82021-09-22 07:40:30 +00001274 }
1275
Gilles Peskine449bd832023-01-11 14:50:10 +01001276 return 0;
XiaokangQian74af2a82021-09-22 07:40:30 +00001277}
1278
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001279MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01001280static int ssl_tls13_write_finished_message_body(mbedtls_ssl_context *ssl,
1281 unsigned char *buf,
1282 unsigned char *end,
1283 size_t *out_len)
XiaokangQian74af2a82021-09-22 07:40:30 +00001284{
XiaokangQian8773aa02021-11-10 07:33:09 +00001285 size_t verify_data_len = ssl->handshake->state_local.finished_out.digest_len;
XiaokangQian0fa66432021-11-15 03:33:57 +00001286 /*
1287 * struct {
1288 * opaque verify_data[Hash.length];
1289 * } Finished;
1290 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001291 MBEDTLS_SSL_CHK_BUF_PTR(buf, end, verify_data_len);
XiaokangQian74af2a82021-09-22 07:40:30 +00001292
Gilles Peskine449bd832023-01-11 14:50:10 +01001293 memcpy(buf, ssl->handshake->state_local.finished_out.digest,
1294 verify_data_len);
XiaokangQian74af2a82021-09-22 07:40:30 +00001295
Xiaofei Baid25fab62021-12-02 06:36:27 +00001296 *out_len = verify_data_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01001297 return 0;
XiaokangQian74af2a82021-09-22 07:40:30 +00001298}
XiaokangQianc5c39d52021-11-09 11:55:10 +00001299
XiaokangQian35dc6252021-11-11 08:16:19 +00001300/* Main entry point: orchestrates the other functions */
Gilles Peskine449bd832023-01-11 14:50:10 +01001301int mbedtls_ssl_tls13_write_finished_message(mbedtls_ssl_context *ssl)
XiaokangQian35dc6252021-11-11 08:16:19 +00001302{
1303 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1304 unsigned char *buf;
1305 size_t buf_len, msg_len;
1306
Gilles Peskine449bd832023-01-11 14:50:10 +01001307 MBEDTLS_SSL_DEBUG_MSG(2, ("=> write finished message"));
XiaokangQian35dc6252021-11-11 08:16:19 +00001308
Gilles Peskine449bd832023-01-11 14:50:10 +01001309 MBEDTLS_SSL_PROC_CHK(ssl_tls13_prepare_finished_message(ssl));
XiaokangQiandce82242021-11-15 06:01:26 +00001310
Gilles Peskine449bd832023-01-11 14:50:10 +01001311 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_start_handshake_msg(ssl,
1312 MBEDTLS_SSL_HS_FINISHED, &buf, &buf_len));
XiaokangQian35dc6252021-11-11 08:16:19 +00001313
Gilles Peskine449bd832023-01-11 14:50:10 +01001314 MBEDTLS_SSL_PROC_CHK(ssl_tls13_write_finished_message_body(
1315 ssl, buf, buf + buf_len, &msg_len));
XiaokangQian35dc6252021-11-11 08:16:19 +00001316
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +01001317 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_add_hs_msg_to_checksum(ssl,
Manuel Pégourié-Gonnard43cc1272023-02-06 11:48:19 +01001318 MBEDTLS_SSL_HS_FINISHED, buf, msg_len));
XiaokangQian35dc6252021-11-11 08:16:19 +00001319
Gilles Peskine449bd832023-01-11 14:50:10 +01001320 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_finish_handshake_msg(
1321 ssl, buf_len, msg_len));
XiaokangQian35dc6252021-11-11 08:16:19 +00001322cleanup:
1323
Gilles Peskine449bd832023-01-11 14:50:10 +01001324 MBEDTLS_SSL_DEBUG_MSG(2, ("<= write finished message"));
1325 return ret;
XiaokangQian35dc6252021-11-11 08:16:19 +00001326}
1327
Gilles Peskine449bd832023-01-11 14:50:10 +01001328void mbedtls_ssl_tls13_handshake_wrapup(mbedtls_ssl_context *ssl)
Jerry Yu378254d2021-10-30 21:44:47 +08001329{
1330
Gilles Peskine449bd832023-01-11 14:50:10 +01001331 MBEDTLS_SSL_DEBUG_MSG(3, ("=> handshake wrapup"));
Jerry Yu378254d2021-10-30 21:44:47 +08001332
Gilles Peskine449bd832023-01-11 14:50:10 +01001333 MBEDTLS_SSL_DEBUG_MSG(1, ("Switch to application keys for inbound traffic"));
1334 mbedtls_ssl_set_inbound_transform(ssl, ssl->transform_application);
Jerry Yue8c1fca2022-05-18 14:48:56 +08001335
Gilles Peskine449bd832023-01-11 14:50:10 +01001336 MBEDTLS_SSL_DEBUG_MSG(1, ("Switch to application keys for outbound traffic"));
1337 mbedtls_ssl_set_outbound_transform(ssl, ssl->transform_application);
Jerry Yue8c1fca2022-05-18 14:48:56 +08001338
Jerry Yu378254d2021-10-30 21:44:47 +08001339 /*
Jerry Yucfe64f02021-11-15 13:54:06 +08001340 * Free the previous session and switch to the current one.
Jerry Yu378254d2021-10-30 21:44:47 +08001341 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001342 if (ssl->session) {
1343 mbedtls_ssl_session_free(ssl->session);
1344 mbedtls_free(ssl->session);
Jerry Yu378254d2021-10-30 21:44:47 +08001345 }
1346 ssl->session = ssl->session_negotiate;
1347 ssl->session_negotiate = NULL;
1348
Gilles Peskine449bd832023-01-11 14:50:10 +01001349 MBEDTLS_SSL_DEBUG_MSG(3, ("<= handshake wrapup"));
Jerry Yu378254d2021-10-30 21:44:47 +08001350}
1351
Ronald Cron49ad6192021-11-24 16:25:31 +01001352/*
1353 *
1354 * STATE HANDLING: Write ChangeCipherSpec
1355 *
1356 */
1357#if defined(MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001358MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01001359static int ssl_tls13_write_change_cipher_spec_body(mbedtls_ssl_context *ssl,
1360 unsigned char *buf,
1361 unsigned char *end,
1362 size_t *olen)
Ronald Cron49ad6192021-11-24 16:25:31 +01001363{
1364 ((void) ssl);
1365
Gilles Peskine449bd832023-01-11 14:50:10 +01001366 MBEDTLS_SSL_CHK_BUF_PTR(buf, end, 1);
Ronald Cron49ad6192021-11-24 16:25:31 +01001367 buf[0] = 1;
1368 *olen = 1;
1369
Gilles Peskine449bd832023-01-11 14:50:10 +01001370 return 0;
Ronald Cron49ad6192021-11-24 16:25:31 +01001371}
1372
Gilles Peskine449bd832023-01-11 14:50:10 +01001373int mbedtls_ssl_tls13_write_change_cipher_spec(mbedtls_ssl_context *ssl)
Ronald Cron49ad6192021-11-24 16:25:31 +01001374{
1375 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1376
Gilles Peskine449bd832023-01-11 14:50:10 +01001377 MBEDTLS_SSL_DEBUG_MSG(2, ("=> write change cipher spec"));
Ronald Cron49ad6192021-11-24 16:25:31 +01001378
Ronald Cron49ad6192021-11-24 16:25:31 +01001379 /* Write CCS message */
Gilles Peskine449bd832023-01-11 14:50:10 +01001380 MBEDTLS_SSL_PROC_CHK(ssl_tls13_write_change_cipher_spec_body(
1381 ssl, ssl->out_msg,
1382 ssl->out_msg + MBEDTLS_SSL_OUT_CONTENT_LEN,
1383 &ssl->out_msglen));
Ronald Cron49ad6192021-11-24 16:25:31 +01001384
1385 ssl->out_msgtype = MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC;
1386
Ronald Cron49ad6192021-11-24 16:25:31 +01001387 /* Dispatch message */
Gilles Peskine449bd832023-01-11 14:50:10 +01001388 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_write_record(ssl, 0));
Ronald Cron49ad6192021-11-24 16:25:31 +01001389
1390cleanup:
1391
Gilles Peskine449bd832023-01-11 14:50:10 +01001392 MBEDTLS_SSL_DEBUG_MSG(2, ("<= write change cipher spec"));
1393 return ret;
Ronald Cron49ad6192021-11-24 16:25:31 +01001394}
1395
1396#endif /* MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE */
1397
Xiaokang Qianecc29482022-11-02 07:52:47 +00001398/* Early Data Indication Extension
Xiaokang Qian0e97d4d2022-10-24 11:12:51 +00001399 *
Xiaokang Qian0e97d4d2022-10-24 11:12:51 +00001400 * struct {
1401 * select ( Handshake.msg_type ) {
Xiaokang Qianecc29482022-11-02 07:52:47 +00001402 * ...
Xiaokang Qian0e97d4d2022-10-24 11:12:51 +00001403 * case client_hello: Empty;
1404 * case encrypted_extensions: Empty;
1405 * };
1406 * } EarlyDataIndication;
1407 */
1408#if defined(MBEDTLS_SSL_EARLY_DATA)
Gilles Peskine449bd832023-01-11 14:50:10 +01001409int mbedtls_ssl_tls13_write_early_data_ext(mbedtls_ssl_context *ssl,
1410 unsigned char *buf,
1411 const unsigned char *end,
1412 size_t *out_len)
Xiaokang Qian0e97d4d2022-10-24 11:12:51 +00001413{
1414 unsigned char *p = buf;
1415 *out_len = 0;
1416 ((void) ssl);
1417
Gilles Peskine449bd832023-01-11 14:50:10 +01001418 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 4);
Xiaokang Qian0e97d4d2022-10-24 11:12:51 +00001419
Gilles Peskine449bd832023-01-11 14:50:10 +01001420 MBEDTLS_PUT_UINT16_BE(MBEDTLS_TLS_EXT_EARLY_DATA, p, 0);
1421 MBEDTLS_PUT_UINT16_BE(0, p, 2);
Xiaokang Qian0e97d4d2022-10-24 11:12:51 +00001422
1423 *out_len = 4;
Xiaokang Qian2cd5ce02022-11-15 10:33:53 +00001424
Gilles Peskine449bd832023-01-11 14:50:10 +01001425 mbedtls_ssl_tls13_set_hs_sent_ext_mask(ssl, MBEDTLS_TLS_EXT_EARLY_DATA);
Xiaokang Qian2cd5ce02022-11-15 10:33:53 +00001426
Gilles Peskine449bd832023-01-11 14:50:10 +01001427 return 0;
Xiaokang Qian0e97d4d2022-10-24 11:12:51 +00001428}
1429#endif /* MBEDTLS_SSL_EARLY_DATA */
1430
XiaokangQian78b1fa72022-01-19 06:56:30 +00001431/* Reset SSL context and update hash for handling HRR.
1432 *
1433 * Replace Transcript-Hash(X) by
1434 * Transcript-Hash( message_hash ||
1435 * 00 00 Hash.length ||
1436 * X )
1437 * A few states of the handshake are preserved, including:
1438 * - session ID
1439 * - session ticket
1440 * - negotiated ciphersuite
1441 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001442int mbedtls_ssl_reset_transcript_for_hrr(mbedtls_ssl_context *ssl)
XiaokangQian78b1fa72022-01-19 06:56:30 +00001443{
1444 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Przemyslaw Stekielda645252022-09-14 12:50:51 +02001445 unsigned char hash_transcript[PSA_HASH_MAX_SIZE + 4];
XiaokangQian0ece9982022-01-24 08:56:23 +00001446 size_t hash_len;
Xiaokang Qian6b980012023-02-07 03:17:45 +00001447 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
1448 ssl->handshake->ciphersuite_info;
XiaokangQian78b1fa72022-01-19 06:56:30 +00001449
Gilles Peskine449bd832023-01-11 14:50:10 +01001450 MBEDTLS_SSL_DEBUG_MSG(3, ("Reset SSL session for HRR"));
XiaokangQian78b1fa72022-01-19 06:56:30 +00001451
Gilles Peskine449bd832023-01-11 14:50:10 +01001452 ret = mbedtls_ssl_get_handshake_transcript(ssl, ciphersuite_info->mac,
1453 hash_transcript + 4,
1454 PSA_HASH_MAX_SIZE,
1455 &hash_len);
1456 if (ret != 0) {
Manuel Pégourié-Gonnardda7979b2023-02-21 09:31:10 +01001457 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_get_handshake_transcript", ret);
Gilles Peskine449bd832023-01-11 14:50:10 +01001458 return ret;
XiaokangQian0ece9982022-01-24 08:56:23 +00001459 }
1460
1461 hash_transcript[0] = MBEDTLS_SSL_HS_MESSAGE_HASH;
1462 hash_transcript[1] = 0;
1463 hash_transcript[2] = 0;
1464 hash_transcript[3] = (unsigned char) hash_len;
1465
1466 hash_len += 4;
1467
Manuel Pégourié-Gonnardda7979b2023-02-21 09:31:10 +01001468 MBEDTLS_SSL_DEBUG_BUF(4, "Truncated handshake transcript",
1469 hash_transcript, hash_len);
1470
Manuel Pégourié-Gonnardd7a7a232023-02-05 10:26:49 +01001471 /* Reset running hash and replace it with a hash of the transcript */
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +01001472 ret = mbedtls_ssl_reset_checksum(ssl);
1473 if (ret != 0) {
1474 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_reset_checksum", ret);
1475 return ret;
1476 }
1477 ret = ssl->handshake->update_checksum(ssl, hash_transcript, hash_len);
1478 if (ret != 0) {
1479 MBEDTLS_SSL_DEBUG_RET(1, "update_checksum", ret);
1480 return ret;
1481 }
Przemyslaw Stekiel4b3fff42022-02-14 16:39:52 +01001482
Gilles Peskine449bd832023-01-11 14:50:10 +01001483 return ret;
XiaokangQian78b1fa72022-01-19 06:56:30 +00001484}
1485
Valerio Setti080a22b2023-03-20 15:22:47 +01001486#if defined(PSA_WANT_ALG_ECDH)
XiaokangQian7807f9f2022-02-15 10:04:37 +00001487
Gilles Peskine449bd832023-01-11 14:50:10 +01001488int mbedtls_ssl_tls13_read_public_ecdhe_share(mbedtls_ssl_context *ssl,
1489 const unsigned char *buf,
1490 size_t buf_len)
XiaokangQian7807f9f2022-02-15 10:04:37 +00001491{
Gilles Peskine449bd832023-01-11 14:50:10 +01001492 uint8_t *p = (uint8_t *) buf;
XiaokangQiancfd925f2022-04-14 07:10:37 +00001493 const uint8_t *end = buf + buf_len;
XiaokangQian9b5d04b2022-04-10 10:20:43 +00001494 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001495
XiaokangQian9b5d04b2022-04-10 10:20:43 +00001496 /* Get size of the TLS opaque key_exchange field of the KeyShareEntry struct. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001497 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, 2);
1498 uint16_t peerkey_len = MBEDTLS_GET_UINT16_BE(p, 0);
XiaokangQian9b5d04b2022-04-10 10:20:43 +00001499 p += 2;
XiaokangQian3207a322022-02-23 03:15:27 +00001500
XiaokangQian9b5d04b2022-04-10 10:20:43 +00001501 /* Check if key size is consistent with given buffer length. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001502 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, peerkey_len);
XiaokangQian9b5d04b2022-04-10 10:20:43 +00001503
1504 /* Store peer's ECDH public key. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001505 memcpy(handshake->ecdh_psa_peerkey, p, peerkey_len);
XiaokangQian9b5d04b2022-04-10 10:20:43 +00001506 handshake->ecdh_psa_peerkey_len = peerkey_len;
1507
Gilles Peskine449bd832023-01-11 14:50:10 +01001508 return 0;
XiaokangQian3207a322022-02-23 03:15:27 +00001509}
Jerry Yu89e103c2022-03-30 22:43:29 +08001510
1511int mbedtls_ssl_tls13_generate_and_write_ecdh_key_exchange(
Gilles Peskine449bd832023-01-11 14:50:10 +01001512 mbedtls_ssl_context *ssl,
1513 uint16_t named_group,
1514 unsigned char *buf,
1515 unsigned char *end,
1516 size_t *out_len)
Jerry Yu89e103c2022-03-30 22:43:29 +08001517{
1518 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
1519 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
1520 psa_key_attributes_t key_attributes;
1521 size_t own_pubkey_len;
1522 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
Valerio Setti40d9ca92023-01-04 16:08:04 +01001523 psa_ecc_family_t ec_psa_family = 0;
1524 size_t ec_bits = 0;
Jerry Yu89e103c2022-03-30 22:43:29 +08001525
Gilles Peskine449bd832023-01-11 14:50:10 +01001526 MBEDTLS_SSL_DEBUG_MSG(1, ("Perform PSA-based ECDH computation."));
Jerry Yu89e103c2022-03-30 22:43:29 +08001527
Valerio Setti40d9ca92023-01-04 16:08:04 +01001528 /* Convert EC's TLS ID to PSA key type. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001529 if (mbedtls_ssl_get_psa_curve_info_from_tls_id(named_group,
1530 &ec_psa_family,
1531 &ec_bits) == PSA_ERROR_NOT_SUPPORTED) {
1532 return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
Valerio Setti40d9ca92023-01-04 16:08:04 +01001533 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001534 handshake->ecdh_psa_type = PSA_KEY_TYPE_ECC_KEY_PAIR(ec_psa_family);
Valerio Setti40d9ca92023-01-04 16:08:04 +01001535 ssl->handshake->ecdh_bits = ec_bits;
Jerry Yu89e103c2022-03-30 22:43:29 +08001536
1537 key_attributes = psa_key_attributes_init();
Gilles Peskine449bd832023-01-11 14:50:10 +01001538 psa_set_key_usage_flags(&key_attributes, PSA_KEY_USAGE_DERIVE);
1539 psa_set_key_algorithm(&key_attributes, PSA_ALG_ECDH);
1540 psa_set_key_type(&key_attributes, handshake->ecdh_psa_type);
1541 psa_set_key_bits(&key_attributes, handshake->ecdh_bits);
Jerry Yu89e103c2022-03-30 22:43:29 +08001542
1543 /* Generate ECDH private key. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001544 status = psa_generate_key(&key_attributes,
1545 &handshake->ecdh_psa_privkey);
1546 if (status != PSA_SUCCESS) {
Andrzej Kurek8a045ce2022-12-23 11:00:06 -05001547 ret = PSA_TO_MBEDTLS_ERR(status);
Gilles Peskine449bd832023-01-11 14:50:10 +01001548 MBEDTLS_SSL_DEBUG_RET(1, "psa_generate_key", ret);
1549 return ret;
Jerry Yu89e103c2022-03-30 22:43:29 +08001550
1551 }
1552
1553 /* Export the public part of the ECDH private key from PSA. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001554 status = psa_export_public_key(handshake->ecdh_psa_privkey,
1555 buf, (size_t) (end - buf),
1556 &own_pubkey_len);
1557 if (status != PSA_SUCCESS) {
Andrzej Kurek8a045ce2022-12-23 11:00:06 -05001558 ret = PSA_TO_MBEDTLS_ERR(status);
Gilles Peskine449bd832023-01-11 14:50:10 +01001559 MBEDTLS_SSL_DEBUG_RET(1, "psa_export_public_key", ret);
1560 return ret;
Jerry Yu89e103c2022-03-30 22:43:29 +08001561
1562 }
1563
1564 *out_len = own_pubkey_len;
1565
Gilles Peskine449bd832023-01-11 14:50:10 +01001566 return 0;
Jerry Yu89e103c2022-03-30 22:43:29 +08001567}
Valerio Setti080a22b2023-03-20 15:22:47 +01001568#endif /* PSA_WANT_ALG_ECDH */
XiaokangQian7807f9f2022-02-15 10:04:37 +00001569
Jerry Yu0c354a22022-08-29 15:25:36 +08001570/* RFC 8446 section 4.2
1571 *
1572 * If an implementation receives an extension which it recognizes and which is
1573 * not specified for the message in which it appears, it MUST abort the handshake
1574 * with an "illegal_parameter" alert.
1575 *
1576 */
Jerry Yuc4bf5d62022-10-29 09:08:47 +08001577int mbedtls_ssl_tls13_check_received_extension(
Gilles Peskine449bd832023-01-11 14:50:10 +01001578 mbedtls_ssl_context *ssl,
1579 int hs_msg_type,
1580 unsigned int received_extension_type,
1581 uint32_t hs_msg_allowed_extensions_mask)
Jerry Yu0c354a22022-08-29 15:25:36 +08001582{
Jerry Yudf0ad652022-10-31 13:20:57 +08001583 uint32_t extension_mask = mbedtls_ssl_get_extension_mask(
Gilles Peskine449bd832023-01-11 14:50:10 +01001584 received_extension_type);
Jerry Yu0c354a22022-08-29 15:25:36 +08001585
Jerry Yu79aa7212022-11-08 21:30:21 +08001586 MBEDTLS_SSL_PRINT_EXT(
Gilles Peskine449bd832023-01-11 14:50:10 +01001587 3, hs_msg_type, received_extension_type, "received");
Jerry Yu0c354a22022-08-29 15:25:36 +08001588
Gilles Peskine449bd832023-01-11 14:50:10 +01001589 if ((extension_mask & hs_msg_allowed_extensions_mask) == 0) {
Jerry Yu79aa7212022-11-08 21:30:21 +08001590 MBEDTLS_SSL_PRINT_EXT(
Gilles Peskine449bd832023-01-11 14:50:10 +01001591 3, hs_msg_type, received_extension_type, "is illegal");
Jerry Yu0c354a22022-08-29 15:25:36 +08001592 MBEDTLS_SSL_PEND_FATAL_ALERT(
1593 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER,
Gilles Peskine449bd832023-01-11 14:50:10 +01001594 MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER);
1595 return MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER;
Jerry Yu0c354a22022-08-29 15:25:36 +08001596 }
1597
1598 ssl->handshake->received_extensions |= extension_mask;
Jerry Yuc4bf5d62022-10-29 09:08:47 +08001599 /*
1600 * If it is a message containing extension responses, check that we
1601 * previously sent the extension.
1602 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001603 switch (hs_msg_type) {
Jerry Yu0c354a22022-08-29 15:25:36 +08001604 case MBEDTLS_SSL_HS_SERVER_HELLO:
Jerry Yudf0ad652022-10-31 13:20:57 +08001605 case MBEDTLS_SSL_TLS1_3_HS_HELLO_RETRY_REQUEST:
Jerry Yu0c354a22022-08-29 15:25:36 +08001606 case MBEDTLS_SSL_HS_ENCRYPTED_EXTENSIONS:
1607 case MBEDTLS_SSL_HS_CERTIFICATE:
Jerry Yuc4bf5d62022-10-29 09:08:47 +08001608 /* Check if the received extension is sent by peer message.*/
Gilles Peskine449bd832023-01-11 14:50:10 +01001609 if ((ssl->handshake->sent_extensions & extension_mask) != 0) {
1610 return 0;
1611 }
Jerry Yu0c354a22022-08-29 15:25:36 +08001612 break;
1613 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01001614 return 0;
Jerry Yu0c354a22022-08-29 15:25:36 +08001615 }
1616
Jerry Yu79aa7212022-11-08 21:30:21 +08001617 MBEDTLS_SSL_PRINT_EXT(
Gilles Peskine449bd832023-01-11 14:50:10 +01001618 3, hs_msg_type, received_extension_type, "is unsupported");
Jerry Yu0c354a22022-08-29 15:25:36 +08001619 MBEDTLS_SSL_PEND_FATAL_ALERT(
1620 MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_EXT,
Gilles Peskine449bd832023-01-11 14:50:10 +01001621 MBEDTLS_ERR_SSL_UNSUPPORTED_EXTENSION);
1622 return MBEDTLS_ERR_SSL_UNSUPPORTED_EXTENSION;
Jerry Yu0c354a22022-08-29 15:25:36 +08001623}
1624
Jan Bruckner151f6422023-02-10 12:45:19 +01001625#if defined(MBEDTLS_SSL_RECORD_SIZE_LIMIT)
Jan Bruckner1a38e542023-03-15 14:15:11 +01001626/* RFC 8449, section 4:
1627 *
Jan Bruckner151f6422023-02-10 12:45:19 +01001628 * The ExtensionData of the "record_size_limit" extension is
1629 * RecordSizeLimit:
1630 * uint16 RecordSizeLimit;
1631 */
1632MBEDTLS_CHECK_RETURN_CRITICAL
1633int mbedtls_ssl_tls13_parse_record_size_limit_ext(mbedtls_ssl_context *ssl,
1634 const unsigned char *buf,
1635 const unsigned char *end)
1636{
Jan Bruckner1a38e542023-03-15 14:15:11 +01001637 const unsigned char *p = buf;
1638 uint16_t record_size_limit;
Jan Brucknera0589e72023-03-15 11:04:45 +01001639 const size_t extension_data_len = end - buf;
Jan Bruckner1a38e542023-03-15 14:15:11 +01001640
Jan Brucknera0589e72023-03-15 11:04:45 +01001641 if (extension_data_len != MBEDTLS_SSL_RECORD_SIZE_LIMIT_EXTENSION_DATA_LENGTH) {
Jan Bruckner151f6422023-02-10 12:45:19 +01001642 MBEDTLS_SSL_DEBUG_MSG(2,
Jan Bruckner1a38e542023-03-15 14:15:11 +01001643 ("record_size_limit extension has invalid length: %"
1644 MBEDTLS_PRINTF_SIZET " Bytes",
Jan Bruckner151f6422023-02-10 12:45:19 +01001645 extension_data_len));
1646
1647 MBEDTLS_SSL_PEND_FATAL_ALERT(
1648 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER,
1649 MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER);
1650 return MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER;
1651 }
1652
Jan Bruckner151f6422023-02-10 12:45:19 +01001653 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, 2);
1654 record_size_limit = MBEDTLS_GET_UINT16_BE(p, 0);
1655
1656 MBEDTLS_SSL_DEBUG_MSG(2, ("RecordSizeLimit: %u Bytes", record_size_limit));
1657
Jan Bruckner1a38e542023-03-15 14:15:11 +01001658 /* RFC 8449, section 4
Jan Bruckner151f6422023-02-10 12:45:19 +01001659 *
1660 * Endpoints MUST NOT send a "record_size_limit" extension with a value
1661 * smaller than 64. An endpoint MUST treat receipt of a smaller value
1662 * as a fatal error and generate an "illegal_parameter" alert.
1663 */
Jan Brucknera0589e72023-03-15 11:04:45 +01001664 if (record_size_limit < MBEDTLS_SSL_RECORD_SIZE_LIMIT_MIN) {
Jan Bruckner151f6422023-02-10 12:45:19 +01001665 MBEDTLS_SSL_PEND_FATAL_ALERT(
1666 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER,
1667 MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER);
1668 return MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER;
1669 }
1670
1671 MBEDTLS_SSL_DEBUG_MSG(2,
1672 (
1673 "record_size_limit extension is still in development. Aborting handshake."));
1674
1675 MBEDTLS_SSL_PEND_FATAL_ALERT(
1676 MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_EXT,
1677 MBEDTLS_ERR_SSL_UNSUPPORTED_EXTENSION);
1678 return MBEDTLS_ERR_SSL_UNSUPPORTED_EXTENSION;
1679}
1680#endif /* MBEDTLS_SSL_RECORD_SIZE_LIMIT */
1681
Jerry Yufb4b6472022-01-27 15:03:26 +08001682#endif /* MBEDTLS_SSL_TLS_C && MBEDTLS_SSL_PROTO_TLS1_3 */