blob: e58c3e5b8735715609561fe7dc2158a81d905ff6 [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 Kurek00644842023-05-30 05:45:00 -040042/* Define a local translating function to save code size by not using too many
43 * arguments in each translating place. */
44static int local_err_translation(psa_status_t status)
45{
46 return psa_status_to_mbedtls(status, psa_to_ssl_errors,
Andrzej Kurek1e4a0302023-05-30 09:45:17 -040047 ARRAY_LENGTH(psa_to_ssl_errors),
Andrzej Kurek00644842023-05-30 05:45:00 -040048 psa_generic_status_to_mbedtls);
49}
50#define PSA_TO_MBEDTLS_ERR(status) local_err_translation(status)
Andrzej Kurek8a045ce2022-12-23 11:00:06 -050051
Jerry Yufbe3e642022-04-25 19:31:51 +080052const uint8_t mbedtls_ssl_tls13_hello_retry_request_magic[
Gilles Peskine449bd832023-01-11 14:50:10 +010053 MBEDTLS_SERVER_HELLO_RANDOM_LEN] =
54{ 0xCF, 0x21, 0xAD, 0x74, 0xE5, 0x9A, 0x61, 0x11,
55 0xBE, 0x1D, 0x8C, 0x02, 0x1E, 0x65, 0xB8, 0x91,
56 0xC2, 0xA2, 0x11, 0x16, 0x7A, 0xBB, 0x8C, 0x5E,
57 0x07, 0x9E, 0x09, 0xE2, 0xC8, 0xA8, 0x33, 0x9C };
Jerry Yu93a13f22022-04-11 23:00:01 +080058
Gilles Peskine449bd832023-01-11 14:50:10 +010059int mbedtls_ssl_tls13_fetch_handshake_msg(mbedtls_ssl_context *ssl,
60 unsigned hs_type,
61 unsigned char **buf,
62 size_t *buf_len)
XiaokangQian6b226b02021-09-24 07:51:16 +000063{
64 int ret;
65
Gilles Peskine449bd832023-01-11 14:50:10 +010066 if ((ret = mbedtls_ssl_read_record(ssl, 0)) != 0) {
67 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_read_record", ret);
XiaokangQian6b226b02021-09-24 07:51:16 +000068 goto cleanup;
69 }
70
Gilles Peskine449bd832023-01-11 14:50:10 +010071 if (ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE ||
72 ssl->in_msg[0] != hs_type) {
73 MBEDTLS_SSL_DEBUG_MSG(1, ("Receive unexpected handshake message."));
74 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE,
75 MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE);
XiaokangQian6b226b02021-09-24 07:51:16 +000076 ret = MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE;
77 goto cleanup;
78 }
79
XiaokangQian05420b12021-09-29 08:46:37 +000080 /*
81 * Jump handshake header (4 bytes, see Section 4 of RFC 8446).
82 * ...
83 * HandshakeType msg_type;
84 * uint24 length;
85 * ...
86 */
Xiaofei Baieef15042021-11-18 07:29:56 +000087 *buf = ssl->in_msg + 4;
88 *buf_len = ssl->in_hslen - 4;
XiaokangQian6b226b02021-09-24 07:51:16 +000089
XiaokangQian6b226b02021-09-24 07:51:16 +000090cleanup:
91
Gilles Peskine449bd832023-01-11 14:50:10 +010092 return ret;
XiaokangQian6b226b02021-09-24 07:51:16 +000093}
94
Ronald Cron47dce632023-02-08 17:38:29 +010095int mbedtls_ssl_tls13_is_supported_versions_ext_present_in_exts(
96 mbedtls_ssl_context *ssl,
97 const unsigned char *buf, const unsigned char *end,
Ronald Croneff56732023-04-03 17:36:31 +020098 const unsigned char **supported_versions_data,
99 const unsigned char **supported_versions_data_end)
Ronald Cron47dce632023-02-08 17:38:29 +0100100{
101 const unsigned char *p = buf;
102 size_t extensions_len;
103 const unsigned char *extensions_end;
104
Ronald Croneff56732023-04-03 17:36:31 +0200105 *supported_versions_data = NULL;
106 *supported_versions_data_end = NULL;
Ronald Cron47dce632023-02-08 17:38:29 +0100107
108 /* Case of no extension */
109 if (p == end) {
110 return 0;
111 }
112
113 /* ...
114 * Extension extensions<x..2^16-1>;
115 * ...
116 * struct {
117 * ExtensionType extension_type; (2 bytes)
118 * opaque extension_data<0..2^16-1>;
119 * } Extension;
120 */
121 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, 2);
122 extensions_len = MBEDTLS_GET_UINT16_BE(p, 0);
123 p += 2;
124
125 /* Check extensions do not go beyond the buffer of data. */
126 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, extensions_len);
127 extensions_end = p + extensions_len;
128
129 while (p < extensions_end) {
130 unsigned int extension_type;
131 size_t extension_data_len;
132
133 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, extensions_end, 4);
134 extension_type = MBEDTLS_GET_UINT16_BE(p, 0);
135 extension_data_len = MBEDTLS_GET_UINT16_BE(p, 2);
136 p += 4;
137 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, extensions_end, extension_data_len);
138
139 if (extension_type == MBEDTLS_TLS_EXT_SUPPORTED_VERSIONS) {
Ronald Croneff56732023-04-03 17:36:31 +0200140 *supported_versions_data = p;
141 *supported_versions_data_end = p + extension_data_len;
Ronald Cron47dce632023-02-08 17:38:29 +0100142 return 1;
143 }
144 p += extension_data_len;
145 }
146
147 return 0;
148}
149
Ronald Cron928cbd32022-10-04 16:14:26 +0200150#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED)
Jerry Yu30b071c2021-09-12 20:16:03 +0800151/*
Jerry Yu30b071c2021-09-12 20:16:03 +0800152 * STATE HANDLING: Read CertificateVerify
153 */
Jerry Yud0fc5852021-10-29 11:09:06 +0800154/* Macro to express the maximum length of the verify structure.
Jerry Yu30b071c2021-09-12 20:16:03 +0800155 *
156 * The structure is computed per TLS 1.3 specification as:
157 * - 64 bytes of octet 32,
158 * - 33 bytes for the context string
159 * (which is either "TLS 1.3, client CertificateVerify"
160 * or "TLS 1.3, server CertificateVerify"),
Jerry Yud0fc5852021-10-29 11:09:06 +0800161 * - 1 byte for the octet 0x0, which serves as a separator,
Jerry Yu30b071c2021-09-12 20:16:03 +0800162 * - 32 or 48 bytes for the Transcript-Hash(Handshake Context, Certificate)
163 * (depending on the size of the transcript_hash)
164 *
165 * This results in a total size of
166 * - 130 bytes for a SHA256-based transcript hash, or
167 * (64 + 33 + 1 + 32 bytes)
168 * - 146 bytes for a SHA384-based transcript hash.
169 * (64 + 33 + 1 + 48 bytes)
170 *
171 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100172#define SSL_VERIFY_STRUCT_MAX_SIZE (64 + \
173 33 + \
174 1 + \
175 MBEDTLS_TLS1_3_MD_MAX_SIZE \
176 )
Jerry Yu30b071c2021-09-12 20:16:03 +0800177
Jerry Yu0b32c502021-10-28 13:41:59 +0800178/*
179 * The ssl_tls13_create_verify_structure() creates the verify structure.
180 * As input, it requires the transcript hash.
181 *
182 * The caller has to ensure that the buffer has size at least
183 * SSL_VERIFY_STRUCT_MAX_SIZE bytes.
184 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100185static void ssl_tls13_create_verify_structure(const unsigned char *transcript_hash,
186 size_t transcript_hash_len,
187 unsigned char *verify_buffer,
188 size_t *verify_buffer_len,
189 int from)
Jerry Yu0b32c502021-10-28 13:41:59 +0800190{
191 size_t idx;
Jerry Yu30b071c2021-09-12 20:16:03 +0800192
Jerry Yu0b32c502021-10-28 13:41:59 +0800193 /* RFC 8446, Section 4.4.3:
194 *
195 * The digital signature [in the CertificateVerify message] is then
196 * computed over the concatenation of:
197 * - A string that consists of octet 32 (0x20) repeated 64 times
198 * - The context string
199 * - A single 0 byte which serves as the separator
200 * - The content to be signed
201 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100202 memset(verify_buffer, 0x20, 64);
Jerry Yu0b32c502021-10-28 13:41:59 +0800203 idx = 64;
204
Gilles Peskine449bd832023-01-11 14:50:10 +0100205 if (from == MBEDTLS_SSL_IS_CLIENT) {
206 memcpy(verify_buffer + idx, MBEDTLS_SSL_TLS1_3_LBL_WITH_LEN(client_cv));
207 idx += MBEDTLS_SSL_TLS1_3_LBL_LEN(client_cv);
208 } else { /* from == MBEDTLS_SSL_IS_SERVER */
209 memcpy(verify_buffer + idx, MBEDTLS_SSL_TLS1_3_LBL_WITH_LEN(server_cv));
210 idx += MBEDTLS_SSL_TLS1_3_LBL_LEN(server_cv);
Jerry Yu0b32c502021-10-28 13:41:59 +0800211 }
212
213 verify_buffer[idx++] = 0x0;
214
Gilles Peskine449bd832023-01-11 14:50:10 +0100215 memcpy(verify_buffer + idx, transcript_hash, transcript_hash_len);
Jerry Yu0b32c502021-10-28 13:41:59 +0800216 idx += transcript_hash_len;
217
218 *verify_buffer_len = idx;
219}
220
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200221MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100222static int ssl_tls13_parse_certificate_verify(mbedtls_ssl_context *ssl,
223 const unsigned char *buf,
224 const unsigned char *end,
225 const unsigned char *verify_buffer,
226 size_t verify_buffer_len)
Jerry Yu30b071c2021-09-12 20:16:03 +0800227{
228 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
pespaceka1378102022-04-26 15:03:11 +0200229 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Jerry Yu30b071c2021-09-12 20:16:03 +0800230 const unsigned char *p = buf;
231 uint16_t algorithm;
Jerry Yu30b071c2021-09-12 20:16:03 +0800232 size_t signature_len;
233 mbedtls_pk_type_t sig_alg;
234 mbedtls_md_type_t md_alg;
pespaceka1378102022-04-26 15:03:11 +0200235 psa_algorithm_t hash_alg = PSA_ALG_NONE;
236 unsigned char verify_hash[PSA_HASH_MAX_SIZE];
Jerry Yu30b071c2021-09-12 20:16:03 +0800237 size_t verify_hash_len;
238
Xiaofei Baid25fab62021-12-02 06:36:27 +0000239 void const *options = NULL;
XiaokangQian82d34cc2021-11-03 08:51:56 +0000240#if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT)
Xiaofei Baid25fab62021-12-02 06:36:27 +0000241 mbedtls_pk_rsassa_pss_options rsassa_pss_options;
XiaokangQian82d34cc2021-11-03 08:51:56 +0000242#endif /* MBEDTLS_X509_RSASSA_PSS_SUPPORT */
243
Jerry Yu30b071c2021-09-12 20:16:03 +0800244 /*
245 * struct {
246 * SignatureScheme algorithm;
247 * opaque signature<0..2^16-1>;
248 * } CertificateVerify;
249 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100250 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, 2);
251 algorithm = MBEDTLS_GET_UINT16_BE(p, 0);
Jerry Yu30b071c2021-09-12 20:16:03 +0800252 p += 2;
253
254 /* RFC 8446 section 4.4.3
255 *
Xiaokang Qian73437382023-03-29 08:24:12 +0000256 * If the CertificateVerify message is sent by a server, the signature
257 * algorithm MUST be one offered in the client's "signature_algorithms"
258 * extension unless no valid certificate chain can be produced without
259 * unsupported algorithms
Jerry Yu30b071c2021-09-12 20:16:03 +0800260 *
261 * RFC 8446 section 4.4.2.2
262 *
263 * If the client cannot construct an acceptable chain using the provided
Xiaokang Qian73437382023-03-29 08:24:12 +0000264 * certificates and decides to abort the handshake, then it MUST abort the
265 * handshake with an appropriate certificate-related alert
266 * (by default, "unsupported_certificate").
Jerry Yu30b071c2021-09-12 20:16:03 +0800267 *
Jerry Yu6f87f252021-10-29 20:12:51 +0800268 * Check if algorithm is an offered signature algorithm.
Jerry Yu30b071c2021-09-12 20:16:03 +0800269 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100270 if (!mbedtls_ssl_sig_alg_is_offered(ssl, algorithm)) {
Jerry Yu982d9e52021-10-14 15:59:37 +0800271 /* algorithm not in offered signature algorithms list */
Gilles Peskine449bd832023-01-11 14:50:10 +0100272 MBEDTLS_SSL_DEBUG_MSG(1, ("Received signature algorithm(%04x) is not "
273 "offered.",
274 (unsigned int) algorithm));
Jerry Yu6f87f252021-10-29 20:12:51 +0800275 goto error;
Jerry Yu30b071c2021-09-12 20:16:03 +0800276 }
277
Gilles Peskine449bd832023-01-11 14:50:10 +0100278 if (mbedtls_ssl_get_pk_type_and_md_alg_from_sig_alg(
279 algorithm, &sig_alg, &md_alg) != 0) {
Jerry Yu8c338862022-03-23 13:34:04 +0800280 goto error;
Jerry Yu30b071c2021-09-12 20:16:03 +0800281 }
282
Manuel Pégourié-Gonnard2d6d9932023-03-28 11:38:08 +0200283 hash_alg = mbedtls_md_psa_alg_from_type(md_alg);
Gilles Peskine449bd832023-01-11 14:50:10 +0100284 if (hash_alg == 0) {
pespaceka1378102022-04-26 15:03:11 +0200285 goto error;
286 }
287
Gilles Peskine449bd832023-01-11 14:50:10 +0100288 MBEDTLS_SSL_DEBUG_MSG(3, ("Certificate Verify: Signature algorithm ( %04x )",
289 (unsigned int) algorithm));
Jerry Yu30b071c2021-09-12 20:16:03 +0800290
291 /*
292 * Check the certificate's key type matches the signature alg
293 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100294 if (!mbedtls_pk_can_do(&ssl->session_negotiate->peer_cert->pk, sig_alg)) {
295 MBEDTLS_SSL_DEBUG_MSG(1, ("signature algorithm doesn't match cert key"));
Jerry Yu6f87f252021-10-29 20:12:51 +0800296 goto error;
Jerry Yu30b071c2021-09-12 20:16:03 +0800297 }
298
Gilles Peskine449bd832023-01-11 14:50:10 +0100299 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, 2);
300 signature_len = MBEDTLS_GET_UINT16_BE(p, 0);
Jerry Yu30b071c2021-09-12 20:16:03 +0800301 p += 2;
Gilles Peskine449bd832023-01-11 14:50:10 +0100302 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, signature_len);
Jerry Yu30b071c2021-09-12 20:16:03 +0800303
Gilles Peskine449bd832023-01-11 14:50:10 +0100304 status = psa_hash_compute(hash_alg,
305 verify_buffer,
306 verify_buffer_len,
307 verify_hash,
308 sizeof(verify_hash),
309 &verify_hash_len);
310 if (status != PSA_SUCCESS) {
311 MBEDTLS_SSL_DEBUG_RET(1, "hash computation PSA error", status);
Jerry Yu6f87f252021-10-29 20:12:51 +0800312 goto error;
Jerry Yu133690c2021-10-25 14:01:13 +0800313 }
314
Gilles Peskine449bd832023-01-11 14:50:10 +0100315 MBEDTLS_SSL_DEBUG_BUF(3, "verify hash", verify_hash, verify_hash_len);
XiaokangQian82d34cc2021-11-03 08:51:56 +0000316#if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT)
Gilles Peskine449bd832023-01-11 14:50:10 +0100317 if (sig_alg == MBEDTLS_PK_RSASSA_PSS) {
Xiaofei Baid25fab62021-12-02 06:36:27 +0000318 rsassa_pss_options.mgf1_hash_id = md_alg;
Przemek Stekiel6a5e0182022-06-27 11:53:13 +0200319
Gilles Peskine449bd832023-01-11 14:50:10 +0100320 rsassa_pss_options.expected_salt_len = PSA_HASH_LENGTH(hash_alg);
321 options = (const void *) &rsassa_pss_options;
XiaokangQian82d34cc2021-11-03 08:51:56 +0000322 }
323#endif /* MBEDTLS_X509_RSASSA_PSS_SUPPORT */
Jerry Yu30b071c2021-09-12 20:16:03 +0800324
Gilles Peskine449bd832023-01-11 14:50:10 +0100325 if ((ret = mbedtls_pk_verify_ext(sig_alg, options,
326 &ssl->session_negotiate->peer_cert->pk,
327 md_alg, verify_hash, verify_hash_len,
328 p, signature_len)) == 0) {
329 return 0;
Jerry Yu30b071c2021-09-12 20:16:03 +0800330 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100331 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_pk_verify_ext", ret);
Jerry Yu30b071c2021-09-12 20:16:03 +0800332
Jerry Yu6f87f252021-10-29 20:12:51 +0800333error:
334 /* RFC 8446 section 4.4.3
335 *
336 * If the verification fails, the receiver MUST terminate the handshake
337 * with a "decrypt_error" alert.
Gilles Peskine449bd832023-01-11 14:50:10 +0100338 */
339 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_DECRYPT_ERROR,
340 MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE);
341 return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
Jerry Yu6f87f252021-10-29 20:12:51 +0800342
Jerry Yu30b071c2021-09-12 20:16:03 +0800343}
Ronald Cron928cbd32022-10-04 16:14:26 +0200344#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED */
Jerry Yu30b071c2021-09-12 20:16:03 +0800345
Gilles Peskine449bd832023-01-11 14:50:10 +0100346int mbedtls_ssl_tls13_process_certificate_verify(mbedtls_ssl_context *ssl)
Jerry Yu30b071c2021-09-12 20:16:03 +0800347{
Jerry Yu30b071c2021-09-12 20:16:03 +0800348
Ronald Cron928cbd32022-10-04 16:14:26 +0200349#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED)
Jerry Yuda8cdf22021-10-25 15:06:49 +0800350 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
351 unsigned char verify_buffer[SSL_VERIFY_STRUCT_MAX_SIZE];
352 size_t verify_buffer_len;
353 unsigned char transcript[MBEDTLS_TLS1_3_MD_MAX_SIZE];
354 size_t transcript_len;
355 unsigned char *buf;
356 size_t buf_len;
357
Gilles Peskine449bd832023-01-11 14:50:10 +0100358 MBEDTLS_SSL_DEBUG_MSG(2, ("=> parse certificate verify"));
Jerry Yu30b071c2021-09-12 20:16:03 +0800359
Jerry Yuda8cdf22021-10-25 15:06:49 +0800360 MBEDTLS_SSL_PROC_CHK(
Xiaokang Qian73437382023-03-29 08:24:12 +0000361 mbedtls_ssl_tls13_fetch_handshake_msg(
362 ssl, MBEDTLS_SSL_HS_CERTIFICATE_VERIFY, &buf, &buf_len));
Jerry Yu30b071c2021-09-12 20:16:03 +0800363
Jerry Yuda8cdf22021-10-25 15:06:49 +0800364 /* Need to calculate the hash of the transcript first
Jerry Yu0b32c502021-10-28 13:41:59 +0800365 * before reading the message since otherwise it gets
366 * included in the transcript
367 */
Xiaokang Qian73437382023-03-29 08:24:12 +0000368 ret = mbedtls_ssl_get_handshake_transcript(
369 ssl,
370 ssl->handshake->ciphersuite_info->mac,
371 transcript, sizeof(transcript),
372 &transcript_len);
Gilles Peskine449bd832023-01-11 14:50:10 +0100373 if (ret != 0) {
Jerry Yuda8cdf22021-10-25 15:06:49 +0800374 MBEDTLS_SSL_PEND_FATAL_ALERT(
375 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR,
Gilles Peskine449bd832023-01-11 14:50:10 +0100376 MBEDTLS_ERR_SSL_INTERNAL_ERROR);
377 return ret;
Jerry Yu30b071c2021-09-12 20:16:03 +0800378 }
379
Gilles Peskine449bd832023-01-11 14:50:10 +0100380 MBEDTLS_SSL_DEBUG_BUF(3, "handshake hash", transcript, transcript_len);
Jerry Yuda8cdf22021-10-25 15:06:49 +0800381
382 /* Create verify structure */
Gilles Peskine449bd832023-01-11 14:50:10 +0100383 ssl_tls13_create_verify_structure(transcript,
384 transcript_len,
385 verify_buffer,
386 &verify_buffer_len,
387 (ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT) ?
388 MBEDTLS_SSL_IS_SERVER :
389 MBEDTLS_SSL_IS_CLIENT);
Jerry Yuda8cdf22021-10-25 15:06:49 +0800390
391 /* Process the message contents */
Xiaokang Qian73437382023-03-29 08:24:12 +0000392 MBEDTLS_SSL_PROC_CHK(ssl_tls13_parse_certificate_verify(
393 ssl, buf, buf + buf_len,
394 verify_buffer, verify_buffer_len));
Jerry Yuda8cdf22021-10-25 15:06:49 +0800395
Xiaokang Qian73437382023-03-29 08:24:12 +0000396 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_add_hs_msg_to_checksum(
397 ssl, MBEDTLS_SSL_HS_CERTIFICATE_VERIFY,
398 buf, buf_len));
Jerry Yu30b071c2021-09-12 20:16:03 +0800399
400cleanup:
401
Gilles Peskine449bd832023-01-11 14:50:10 +0100402 MBEDTLS_SSL_DEBUG_MSG(2, ("<= parse certificate verify"));
403 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_tls13_process_certificate_verify", ret);
404 return ret;
Jerry Yuda8cdf22021-10-25 15:06:49 +0800405#else
406 ((void) ssl);
Gilles Peskine449bd832023-01-11 14:50:10 +0100407 MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
408 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Ronald Cron928cbd32022-10-04 16:14:26 +0200409#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED */
Jerry Yu30b071c2021-09-12 20:16:03 +0800410}
411
412/*
Xiaofei Bai947571e2021-09-29 09:12:03 +0000413 *
XiaokangQian6b916b12022-04-25 07:29:34 +0000414 * STATE HANDLING: Incoming Certificate.
Xiaofei Bai947571e2021-09-29 09:12:03 +0000415 *
416 */
417
Ronald Cronde08cf32022-10-04 17:15:35 +0200418#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED)
Xiaofei Bai947571e2021-09-29 09:12:03 +0000419#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
420/*
421 * Structure of Certificate message:
422 *
423 * enum {
424 * X509(0),
425 * RawPublicKey(2),
426 * (255)
427 * } CertificateType;
428 *
429 * struct {
430 * select (certificate_type) {
431 * case RawPublicKey:
432 * * From RFC 7250 ASN.1_subjectPublicKeyInfo *
433 * opaque ASN1_subjectPublicKeyInfo<1..2^24-1>;
434 * case X509:
435 * opaque cert_data<1..2^24-1>;
436 * };
437 * Extension extensions<0..2^16-1>;
438 * } CertificateEntry;
439 *
440 * struct {
441 * opaque certificate_request_context<0..2^8-1>;
442 * CertificateEntry certificate_list<0..2^24-1>;
443 * } Certificate;
444 *
445 */
Xiaofei Bai79595ac2021-10-26 07:16:45 +0000446
447/* Parse certificate chain send by the server. */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200448MBEDTLS_CHECK_RETURN_CRITICAL
Ronald Crone3dac4a2022-06-10 17:21:51 +0200449MBEDTLS_STATIC_TESTABLE
Gilles Peskine449bd832023-01-11 14:50:10 +0100450int mbedtls_ssl_tls13_parse_certificate(mbedtls_ssl_context *ssl,
451 const unsigned char *buf,
452 const unsigned char *end)
Xiaofei Bai947571e2021-09-29 09:12:03 +0000453{
454 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
455 size_t certificate_request_context_len = 0;
456 size_t certificate_list_len = 0;
457 const unsigned char *p = buf;
458 const unsigned char *certificate_list_end;
Jerry Yuc4bf5d62022-10-29 09:08:47 +0800459 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
Xiaofei Bai947571e2021-09-29 09:12:03 +0000460
Gilles Peskine449bd832023-01-11 14:50:10 +0100461 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, 4);
Xiaofei Bai947571e2021-09-29 09:12:03 +0000462 certificate_request_context_len = p[0];
Gilles Peskine449bd832023-01-11 14:50:10 +0100463 certificate_list_len = MBEDTLS_GET_UINT24_BE(p, 1);
XiaokangQian63e713e2022-05-15 04:26:57 +0000464 p += 4;
Xiaofei Bai947571e2021-09-29 09:12:03 +0000465
466 /* In theory, the certificate list can be up to 2^24 Bytes, but we don't
467 * support anything beyond 2^16 = 64K.
468 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100469 if ((certificate_request_context_len != 0) ||
470 (certificate_list_len >= 0x10000)) {
471 MBEDTLS_SSL_DEBUG_MSG(1, ("bad certificate message"));
472 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR,
473 MBEDTLS_ERR_SSL_DECODE_ERROR);
474 return MBEDTLS_ERR_SSL_DECODE_ERROR;
Xiaofei Bai947571e2021-09-29 09:12:03 +0000475 }
476
477 /* In case we tried to reuse a session but it failed */
Gilles Peskine449bd832023-01-11 14:50:10 +0100478 if (ssl->session_negotiate->peer_cert != NULL) {
479 mbedtls_x509_crt_free(ssl->session_negotiate->peer_cert);
480 mbedtls_free(ssl->session_negotiate->peer_cert);
Xiaofei Bai947571e2021-09-29 09:12:03 +0000481 }
482
Gilles Peskine449bd832023-01-11 14:50:10 +0100483 if (certificate_list_len == 0) {
XiaokangQianc3017f62022-05-13 05:55:41 +0000484 ssl->session_negotiate->peer_cert = NULL;
485 ret = 0;
486 goto exit;
487 }
488
Gilles Peskine449bd832023-01-11 14:50:10 +0100489 if ((ssl->session_negotiate->peer_cert =
490 mbedtls_calloc(1, sizeof(mbedtls_x509_crt))) == NULL) {
491 MBEDTLS_SSL_DEBUG_MSG(1, ("alloc( %" MBEDTLS_PRINTF_SIZET " bytes ) failed",
492 sizeof(mbedtls_x509_crt)));
493 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR,
494 MBEDTLS_ERR_SSL_ALLOC_FAILED);
495 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
Xiaofei Bai947571e2021-09-29 09:12:03 +0000496 }
497
Gilles Peskine449bd832023-01-11 14:50:10 +0100498 mbedtls_x509_crt_init(ssl->session_negotiate->peer_cert);
Xiaofei Bai947571e2021-09-29 09:12:03 +0000499
Gilles Peskine449bd832023-01-11 14:50:10 +0100500 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, certificate_list_len);
Xiaofei Bai947571e2021-09-29 09:12:03 +0000501 certificate_list_end = p + certificate_list_len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100502 while (p < certificate_list_end) {
Xiaofei Bai947571e2021-09-29 09:12:03 +0000503 size_t cert_data_len, extensions_len;
Jerry Yu2eaa7602022-08-04 17:28:15 +0800504 const unsigned char *extensions_end;
Xiaofei Bai947571e2021-09-29 09:12:03 +0000505
Gilles Peskine449bd832023-01-11 14:50:10 +0100506 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, certificate_list_end, 3);
507 cert_data_len = MBEDTLS_GET_UINT24_BE(p, 0);
Xiaofei Bai947571e2021-09-29 09:12:03 +0000508 p += 3;
509
510 /* In theory, the CRT can be up to 2^24 Bytes, but we don't support
511 * anything beyond 2^16 = 64K. Otherwise as in the TLS 1.2 code,
512 * check that we have a minimum of 128 bytes of data, this is not
513 * clear why we need that though.
514 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100515 if ((cert_data_len < 128) || (cert_data_len >= 0x10000)) {
516 MBEDTLS_SSL_DEBUG_MSG(1, ("bad Certificate message"));
517 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR,
518 MBEDTLS_ERR_SSL_DECODE_ERROR);
519 return MBEDTLS_ERR_SSL_DECODE_ERROR;
Xiaofei Bai947571e2021-09-29 09:12:03 +0000520 }
521
Gilles Peskine449bd832023-01-11 14:50:10 +0100522 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, certificate_list_end, cert_data_len);
523 ret = mbedtls_x509_crt_parse_der(ssl->session_negotiate->peer_cert,
524 p, cert_data_len);
Xiaofei Bai947571e2021-09-29 09:12:03 +0000525
Gilles Peskine449bd832023-01-11 14:50:10 +0100526 switch (ret) {
Xiaofei Bai947571e2021-09-29 09:12:03 +0000527 case 0: /*ok*/
528 break;
529 case MBEDTLS_ERR_X509_UNKNOWN_SIG_ALG + MBEDTLS_ERR_OID_NOT_FOUND:
530 /* Ignore certificate with an unknown algorithm: maybe a
531 prior certificate was already trusted. */
532 break;
533
534 case MBEDTLS_ERR_X509_ALLOC_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100535 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR,
536 MBEDTLS_ERR_X509_ALLOC_FAILED);
537 MBEDTLS_SSL_DEBUG_RET(1, " mbedtls_x509_crt_parse_der", ret);
538 return ret;
Xiaofei Bai947571e2021-09-29 09:12:03 +0000539
540 case MBEDTLS_ERR_X509_UNKNOWN_VERSION:
Gilles Peskine449bd832023-01-11 14:50:10 +0100541 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT,
542 MBEDTLS_ERR_X509_UNKNOWN_VERSION);
543 MBEDTLS_SSL_DEBUG_RET(1, " mbedtls_x509_crt_parse_der", ret);
544 return ret;
Xiaofei Bai947571e2021-09-29 09:12:03 +0000545
546 default:
Gilles Peskine449bd832023-01-11 14:50:10 +0100547 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_BAD_CERT,
548 ret);
549 MBEDTLS_SSL_DEBUG_RET(1, " mbedtls_x509_crt_parse_der", ret);
550 return ret;
Xiaofei Bai947571e2021-09-29 09:12:03 +0000551 }
552
553 p += cert_data_len;
554
555 /* Certificate extensions length */
Gilles Peskine449bd832023-01-11 14:50:10 +0100556 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, certificate_list_end, 2);
557 extensions_len = MBEDTLS_GET_UINT16_BE(p, 0);
Xiaofei Bai947571e2021-09-29 09:12:03 +0000558 p += 2;
Gilles Peskine449bd832023-01-11 14:50:10 +0100559 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, certificate_list_end, extensions_len);
Jerry Yu2eaa7602022-08-04 17:28:15 +0800560
561 extensions_end = p + extensions_len;
Jerry Yu0d5cfb72022-10-31 14:15:48 +0800562 handshake->received_extensions = MBEDTLS_SSL_EXT_MASK_NONE;
Jerry Yu2eaa7602022-08-04 17:28:15 +0800563
Gilles Peskine449bd832023-01-11 14:50:10 +0100564 while (p < extensions_end) {
Jerry Yu2eaa7602022-08-04 17:28:15 +0800565 unsigned int extension_type;
566 size_t extension_data_len;
567
568 /*
Gilles Peskine449bd832023-01-11 14:50:10 +0100569 * struct {
570 * ExtensionType extension_type; (2 bytes)
571 * opaque extension_data<0..2^16-1>;
572 * } Extension;
573 */
574 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, extensions_end, 4);
575 extension_type = MBEDTLS_GET_UINT16_BE(p, 0);
576 extension_data_len = MBEDTLS_GET_UINT16_BE(p, 2);
Jerry Yu2eaa7602022-08-04 17:28:15 +0800577 p += 4;
578
Gilles Peskine449bd832023-01-11 14:50:10 +0100579 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, extensions_end, extension_data_len);
Jerry Yu2eaa7602022-08-04 17:28:15 +0800580
Jerry Yuc4bf5d62022-10-29 09:08:47 +0800581 ret = mbedtls_ssl_tls13_check_received_extension(
Gilles Peskine449bd832023-01-11 14:50:10 +0100582 ssl, MBEDTLS_SSL_HS_CERTIFICATE, extension_type,
583 MBEDTLS_SSL_TLS1_3_ALLOWED_EXTS_OF_CT);
584 if (ret != 0) {
585 return ret;
586 }
Jerry Yu0c354a22022-08-29 15:25:36 +0800587
Gilles Peskine449bd832023-01-11 14:50:10 +0100588 switch (extension_type) {
Jerry Yu2eaa7602022-08-04 17:28:15 +0800589 default:
Jerry Yu79aa7212022-11-08 21:30:21 +0800590 MBEDTLS_SSL_PRINT_EXT(
Jerry Yu0d5cfb72022-10-31 14:15:48 +0800591 3, MBEDTLS_SSL_HS_CERTIFICATE,
Gilles Peskine449bd832023-01-11 14:50:10 +0100592 extension_type, "( ignored )");
Jerry Yu2eaa7602022-08-04 17:28:15 +0800593 break;
594 }
595
596 p += extension_data_len;
597 }
598
Gilles Peskine449bd832023-01-11 14:50:10 +0100599 MBEDTLS_SSL_PRINT_EXTS(3, MBEDTLS_SSL_HS_CERTIFICATE,
600 handshake->received_extensions);
Xiaofei Bai947571e2021-09-29 09:12:03 +0000601 }
602
XiaokangQian63e713e2022-05-15 04:26:57 +0000603exit:
Xiaofei Bai947571e2021-09-29 09:12:03 +0000604 /* Check that all the message is consumed. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100605 if (p != end) {
606 MBEDTLS_SSL_DEBUG_MSG(1, ("bad Certificate message"));
607 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR,
608 MBEDTLS_ERR_SSL_DECODE_ERROR);
609 return MBEDTLS_ERR_SSL_DECODE_ERROR;
Xiaofei Bai947571e2021-09-29 09:12:03 +0000610 }
611
Xiaokang Qian73437382023-03-29 08:24:12 +0000612 MBEDTLS_SSL_DEBUG_CRT(3, "peer certificate",
613 ssl->session_negotiate->peer_cert);
Xiaofei Bai947571e2021-09-29 09:12:03 +0000614
Gilles Peskine449bd832023-01-11 14:50:10 +0100615 return ret;
Xiaofei Bai947571e2021-09-29 09:12:03 +0000616}
617#else
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200618MBEDTLS_CHECK_RETURN_CRITICAL
Ronald Crone3dac4a2022-06-10 17:21:51 +0200619MBEDTLS_STATIC_TESTABLE
Gilles Peskine449bd832023-01-11 14:50:10 +0100620int mbedtls_ssl_tls13_parse_certificate(mbedtls_ssl_context *ssl,
621 const unsigned char *buf,
622 const unsigned char *end)
Xiaofei Bai947571e2021-09-29 09:12:03 +0000623{
624 ((void) ssl);
625 ((void) buf);
626 ((void) end);
Gilles Peskine449bd832023-01-11 14:50:10 +0100627 return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Xiaofei Bai947571e2021-09-29 09:12:03 +0000628}
629#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Ronald Cronde08cf32022-10-04 17:15:35 +0200630#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED */
Xiaofei Bai947571e2021-09-29 09:12:03 +0000631
Ronald Cronde08cf32022-10-04 17:15:35 +0200632#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED)
Xiaofei Bai947571e2021-09-29 09:12:03 +0000633#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Xiaofei Bai79595ac2021-10-26 07:16:45 +0000634/* Validate certificate chain sent by the server. */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200635MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100636static int ssl_tls13_validate_certificate(mbedtls_ssl_context *ssl)
Xiaofei Bai947571e2021-09-29 09:12:03 +0000637{
638 int ret = 0;
XiaokangQian989f06d2022-05-17 01:50:15 +0000639 int authmode = MBEDTLS_SSL_VERIFY_REQUIRED;
Xiaofei Bai947571e2021-09-29 09:12:03 +0000640 mbedtls_x509_crt *ca_chain;
641 mbedtls_x509_crl *ca_crl;
Ronald Cron30c5a252022-06-16 19:31:06 +0200642 const char *ext_oid;
643 size_t ext_len;
Xiaofei Baiff456022021-10-28 06:50:17 +0000644 uint32_t verify_result = 0;
Xiaofei Bai947571e2021-09-29 09:12:03 +0000645
XiaokangQian6b916b12022-04-25 07:29:34 +0000646 /* If SNI was used, overwrite authentication mode
647 * from the configuration. */
XiaokangQian989f06d2022-05-17 01:50:15 +0000648#if defined(MBEDTLS_SSL_SRV_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100649 if (ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER) {
XiaokangQian0557c942022-05-30 08:10:53 +0000650#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Gilles Peskine449bd832023-01-11 14:50:10 +0100651 if (ssl->handshake->sni_authmode != MBEDTLS_SSL_VERIFY_UNSET) {
XiaokangQian0557c942022-05-30 08:10:53 +0000652 authmode = ssl->handshake->sni_authmode;
Gilles Peskine449bd832023-01-11 14:50:10 +0100653 } else
XiaokangQian0557c942022-05-30 08:10:53 +0000654#endif
Gilles Peskine449bd832023-01-11 14:50:10 +0100655 authmode = ssl->conf->authmode;
XiaokangQian0557c942022-05-30 08:10:53 +0000656 }
XiaokangQian6b916b12022-04-25 07:29:34 +0000657#endif
658
659 /*
XiaokangQian989f06d2022-05-17 01:50:15 +0000660 * If the peer hasn't sent a certificate ( i.e. it sent
XiaokangQian6b916b12022-04-25 07:29:34 +0000661 * an empty certificate chain ), this is reflected in the peer CRT
662 * structure being unset.
663 * Check for that and handle it depending on the
XiaokangQian989f06d2022-05-17 01:50:15 +0000664 * authentication mode.
XiaokangQian6b916b12022-04-25 07:29:34 +0000665 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100666 if (ssl->session_negotiate->peer_cert == NULL) {
667 MBEDTLS_SSL_DEBUG_MSG(1, ("peer has no certificate"));
XiaokangQian989f06d2022-05-17 01:50:15 +0000668
XiaokangQian63e713e2022-05-15 04:26:57 +0000669#if defined(MBEDTLS_SSL_SRV_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100670 if (ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER) {
XiaokangQian63e713e2022-05-15 04:26:57 +0000671 /* The client was asked for a certificate but didn't send
672 * one. The client should know what's going on, so we
673 * don't send an alert.
674 */
675 ssl->session_negotiate->verify_result = MBEDTLS_X509_BADCERT_MISSING;
Gilles Peskine449bd832023-01-11 14:50:10 +0100676 if (authmode == MBEDTLS_SSL_VERIFY_OPTIONAL) {
677 return 0;
678 } else {
Xiaokang Qian73437382023-03-29 08:24:12 +0000679 MBEDTLS_SSL_PEND_FATAL_ALERT(
680 MBEDTLS_SSL_ALERT_MSG_NO_CERT,
681 MBEDTLS_ERR_SSL_NO_CLIENT_CERTIFICATE);
Gilles Peskine449bd832023-01-11 14:50:10 +0100682 return MBEDTLS_ERR_SSL_NO_CLIENT_CERTIFICATE;
XiaokangQian989f06d2022-05-17 01:50:15 +0000683 }
XiaokangQian63e713e2022-05-15 04:26:57 +0000684 }
XiaokangQian6b916b12022-04-25 07:29:34 +0000685#endif /* MBEDTLS_SSL_SRV_C */
686
XiaokangQianc3017f62022-05-13 05:55:41 +0000687#if defined(MBEDTLS_SSL_CLI_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100688 if (ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT) {
689 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_NO_CERT,
690 MBEDTLS_ERR_SSL_FATAL_ALERT_MESSAGE);
691 return MBEDTLS_ERR_SSL_FATAL_ALERT_MESSAGE;
XiaokangQian63e713e2022-05-15 04:26:57 +0000692 }
XiaokangQianc3017f62022-05-13 05:55:41 +0000693#endif /* MBEDTLS_SSL_CLI_C */
XiaokangQian63e713e2022-05-15 04:26:57 +0000694 }
XiaokangQian6b916b12022-04-25 07:29:34 +0000695
Xiaofei Bai947571e2021-09-29 09:12:03 +0000696#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Gilles Peskine449bd832023-01-11 14:50:10 +0100697 if (ssl->handshake->sni_ca_chain != NULL) {
Xiaofei Bai947571e2021-09-29 09:12:03 +0000698 ca_chain = ssl->handshake->sni_ca_chain;
699 ca_crl = ssl->handshake->sni_ca_crl;
Gilles Peskine449bd832023-01-11 14:50:10 +0100700 } else
Xiaofei Bai947571e2021-09-29 09:12:03 +0000701#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
702 {
703 ca_chain = ssl->conf->ca_chain;
704 ca_crl = ssl->conf->ca_crl;
705 }
706
707 /*
708 * Main check: verify certificate
709 */
710 ret = mbedtls_x509_crt_verify_with_profile(
711 ssl->session_negotiate->peer_cert,
712 ca_chain, ca_crl,
713 ssl->conf->cert_profile,
714 ssl->hostname,
Xiaofei Baiff456022021-10-28 06:50:17 +0000715 &verify_result,
Gilles Peskine449bd832023-01-11 14:50:10 +0100716 ssl->conf->f_vrfy, ssl->conf->p_vrfy);
Xiaofei Bai947571e2021-09-29 09:12:03 +0000717
Gilles Peskine449bd832023-01-11 14:50:10 +0100718 if (ret != 0) {
719 MBEDTLS_SSL_DEBUG_RET(1, "x509_verify_cert", ret);
Xiaofei Bai947571e2021-09-29 09:12:03 +0000720 }
721
722 /*
723 * Secondary checks: always done, but change 'ret' only if it was 0
724 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100725 if (ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT) {
Ronald Cron30c5a252022-06-16 19:31:06 +0200726 ext_oid = MBEDTLS_OID_SERVER_AUTH;
Gilles Peskine449bd832023-01-11 14:50:10 +0100727 ext_len = MBEDTLS_OID_SIZE(MBEDTLS_OID_SERVER_AUTH);
728 } else {
Ronald Cron30c5a252022-06-16 19:31:06 +0200729 ext_oid = MBEDTLS_OID_CLIENT_AUTH;
Gilles Peskine449bd832023-01-11 14:50:10 +0100730 ext_len = MBEDTLS_OID_SIZE(MBEDTLS_OID_CLIENT_AUTH);
Ronald Cron30c5a252022-06-16 19:31:06 +0200731 }
732
Gilles Peskine449bd832023-01-11 14:50:10 +0100733 if ((mbedtls_x509_crt_check_key_usage(
734 ssl->session_negotiate->peer_cert,
735 MBEDTLS_X509_KU_DIGITAL_SIGNATURE) != 0) ||
736 (mbedtls_x509_crt_check_extended_key_usage(
737 ssl->session_negotiate->peer_cert,
738 ext_oid, ext_len) != 0)) {
739 MBEDTLS_SSL_DEBUG_MSG(1, ("bad certificate (usage extensions)"));
740 if (ret == 0) {
Xiaofei Bai947571e2021-09-29 09:12:03 +0000741 ret = MBEDTLS_ERR_SSL_BAD_CERTIFICATE;
Gilles Peskine449bd832023-01-11 14:50:10 +0100742 }
Xiaofei Bai947571e2021-09-29 09:12:03 +0000743 }
744
XiaokangQian6b916b12022-04-25 07:29:34 +0000745 /* mbedtls_x509_crt_verify_with_profile is supposed to report a
746 * verification failure through MBEDTLS_ERR_X509_CERT_VERIFY_FAILED,
747 * with details encoded in the verification flags. All other kinds
748 * of error codes, including those from the user provided f_vrfy
749 * functions, are treated as fatal and lead to a failure of
Ronald Crone3dac4a2022-06-10 17:21:51 +0200750 * mbedtls_ssl_tls13_parse_certificate even if verification was optional.
751 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100752 if (authmode == MBEDTLS_SSL_VERIFY_OPTIONAL &&
753 (ret == MBEDTLS_ERR_X509_CERT_VERIFY_FAILED ||
754 ret == MBEDTLS_ERR_SSL_BAD_CERTIFICATE)) {
XiaokangQian6b916b12022-04-25 07:29:34 +0000755 ret = 0;
756 }
Xiaofei Bai947571e2021-09-29 09:12:03 +0000757
Gilles Peskine449bd832023-01-11 14:50:10 +0100758 if (ca_chain == NULL && authmode == MBEDTLS_SSL_VERIFY_REQUIRED) {
759 MBEDTLS_SSL_DEBUG_MSG(1, ("got no CA chain"));
Xiaofei Bai947571e2021-09-29 09:12:03 +0000760 ret = MBEDTLS_ERR_SSL_CA_CHAIN_REQUIRED;
761 }
762
Gilles Peskine449bd832023-01-11 14:50:10 +0100763 if (ret != 0) {
Xiaofei Bai947571e2021-09-29 09:12:03 +0000764 /* The certificate may have been rejected for several reasons.
765 Pick one and send the corresponding alert. Which alert to send
766 may be a subject of debate in some cases. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100767 if (verify_result & MBEDTLS_X509_BADCERT_OTHER) {
Xiaokang Qian73437382023-03-29 08:24:12 +0000768 MBEDTLS_SSL_PEND_FATAL_ALERT(
769 MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED, ret);
Gilles Peskine449bd832023-01-11 14:50:10 +0100770 } else if (verify_result & MBEDTLS_X509_BADCERT_CN_MISMATCH) {
771 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_BAD_CERT, ret);
772 } else if (verify_result & (MBEDTLS_X509_BADCERT_KEY_USAGE |
773 MBEDTLS_X509_BADCERT_EXT_KEY_USAGE |
774 MBEDTLS_X509_BADCERT_NS_CERT_TYPE |
775 MBEDTLS_X509_BADCERT_BAD_PK |
776 MBEDTLS_X509_BADCERT_BAD_KEY)) {
Xiaokang Qian73437382023-03-29 08:24:12 +0000777 MBEDTLS_SSL_PEND_FATAL_ALERT(
778 MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT, ret);
Gilles Peskine449bd832023-01-11 14:50:10 +0100779 } else if (verify_result & MBEDTLS_X509_BADCERT_EXPIRED) {
Xiaokang Qian73437382023-03-29 08:24:12 +0000780 MBEDTLS_SSL_PEND_FATAL_ALERT(
781 MBEDTLS_SSL_ALERT_MSG_CERT_EXPIRED, ret);
Gilles Peskine449bd832023-01-11 14:50:10 +0100782 } else if (verify_result & MBEDTLS_X509_BADCERT_REVOKED) {
Xiaokang Qian73437382023-03-29 08:24:12 +0000783 MBEDTLS_SSL_PEND_FATAL_ALERT(
784 MBEDTLS_SSL_ALERT_MSG_CERT_REVOKED, ret);
Gilles Peskine449bd832023-01-11 14:50:10 +0100785 } else if (verify_result & MBEDTLS_X509_BADCERT_NOT_TRUSTED) {
786 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_UNKNOWN_CA, ret);
787 } else {
Xiaokang Qian73437382023-03-29 08:24:12 +0000788 MBEDTLS_SSL_PEND_FATAL_ALERT(
789 MBEDTLS_SSL_ALERT_MSG_CERT_UNKNOWN, ret);
Gilles Peskine449bd832023-01-11 14:50:10 +0100790 }
Xiaofei Bai947571e2021-09-29 09:12:03 +0000791 }
792
793#if defined(MBEDTLS_DEBUG_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100794 if (verify_result != 0) {
795 MBEDTLS_SSL_DEBUG_MSG(3, ("! Certificate verification flags %08x",
796 (unsigned int) verify_result));
797 } else {
798 MBEDTLS_SSL_DEBUG_MSG(3, ("Certificate verification flags clear"));
Xiaofei Bai947571e2021-09-29 09:12:03 +0000799 }
800#endif /* MBEDTLS_DEBUG_C */
801
Xiaofei Baiff456022021-10-28 06:50:17 +0000802 ssl->session_negotiate->verify_result = verify_result;
Gilles Peskine449bd832023-01-11 14:50:10 +0100803 return ret;
Xiaofei Bai947571e2021-09-29 09:12:03 +0000804}
805#else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200806MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100807static int ssl_tls13_validate_certificate(mbedtls_ssl_context *ssl)
Xiaofei Bai947571e2021-09-29 09:12:03 +0000808{
809 ((void) ssl);
Gilles Peskine449bd832023-01-11 14:50:10 +0100810 return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Xiaofei Bai947571e2021-09-29 09:12:03 +0000811}
812#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Ronald Cronde08cf32022-10-04 17:15:35 +0200813#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED */
Xiaofei Bai947571e2021-09-29 09:12:03 +0000814
Gilles Peskine449bd832023-01-11 14:50:10 +0100815int mbedtls_ssl_tls13_process_certificate(mbedtls_ssl_context *ssl)
Xiaofei Bai947571e2021-09-29 09:12:03 +0000816{
Xiaofei Bai79595ac2021-10-26 07:16:45 +0000817 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine449bd832023-01-11 14:50:10 +0100818 MBEDTLS_SSL_DEBUG_MSG(2, ("=> parse certificate"));
Xiaofei Bai79595ac2021-10-26 07:16:45 +0000819
Ronald Cronde08cf32022-10-04 17:15:35 +0200820#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED)
XiaokangQianc3017f62022-05-13 05:55:41 +0000821 unsigned char *buf;
822 size_t buf_len;
Xiaofei Bai79595ac2021-10-26 07:16:45 +0000823
Gilles Peskine449bd832023-01-11 14:50:10 +0100824 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_tls13_fetch_handshake_msg(
825 ssl, MBEDTLS_SSL_HS_CERTIFICATE,
826 &buf, &buf_len));
Xiaofei Bai79595ac2021-10-26 07:16:45 +0000827
XiaokangQianc3017f62022-05-13 05:55:41 +0000828 /* Parse the certificate chain sent by the peer. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100829 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_tls13_parse_certificate(ssl, buf,
830 buf + buf_len));
XiaokangQianc3017f62022-05-13 05:55:41 +0000831 /* Validate the certificate chain and set the verification results. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100832 MBEDTLS_SSL_PROC_CHK(ssl_tls13_validate_certificate(ssl));
Xiaofei Bai79595ac2021-10-26 07:16:45 +0000833
Xiaokang Qian73437382023-03-29 08:24:12 +0000834 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_add_hs_msg_to_checksum(
835 ssl, MBEDTLS_SSL_HS_CERTIFICATE, buf, buf_len));
Xiaofei Bai79595ac2021-10-26 07:16:45 +0000836
Xiaofei Bai79595ac2021-10-26 07:16:45 +0000837cleanup:
Ronald Cronde08cf32022-10-04 17:15:35 +0200838#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED */
Xiaofei Bai79595ac2021-10-26 07:16:45 +0000839
Gilles Peskine449bd832023-01-11 14:50:10 +0100840 MBEDTLS_SSL_DEBUG_MSG(2, ("<= parse certificate"));
841 return ret;
Xiaofei Bai947571e2021-09-29 09:12:03 +0000842}
Ronald Cron928cbd32022-10-04 16:14:26 +0200843#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED)
Jerry Yu7399d0d2022-01-30 17:54:19 +0800844/*
845 * enum {
846 * X509(0),
847 * RawPublicKey(2),
848 * (255)
849 * } CertificateType;
850 *
851 * struct {
852 * select (certificate_type) {
853 * case RawPublicKey:
854 * // From RFC 7250 ASN.1_subjectPublicKeyInfo
855 * opaque ASN1_subjectPublicKeyInfo<1..2^24-1>;
856 *
857 * case X509:
858 * opaque cert_data<1..2^24-1>;
859 * };
860 * Extension extensions<0..2^16-1>;
861 * } CertificateEntry;
862 *
863 * struct {
864 * opaque certificate_request_context<0..2^8-1>;
865 * CertificateEntry certificate_list<0..2^24-1>;
866 * } Certificate;
867 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200868MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100869static int ssl_tls13_write_certificate_body(mbedtls_ssl_context *ssl,
870 unsigned char *buf,
871 unsigned char *end,
872 size_t *out_len)
Jerry Yu5cc35062022-01-28 16:16:08 +0800873{
Gilles Peskine449bd832023-01-11 14:50:10 +0100874 const mbedtls_x509_crt *crt = mbedtls_ssl_own_cert(ssl);
Jerry Yu3e536442022-02-15 11:05:59 +0800875 unsigned char *p = buf;
Jerry Yuc8d8d4e2022-02-18 12:10:03 +0800876 unsigned char *certificate_request_context =
Gilles Peskine449bd832023-01-11 14:50:10 +0100877 ssl->handshake->certificate_request_context;
Jerry Yuc8d8d4e2022-02-18 12:10:03 +0800878 unsigned char certificate_request_context_len =
Gilles Peskine449bd832023-01-11 14:50:10 +0100879 ssl->handshake->certificate_request_context_len;
Jerry Yuc8d8d4e2022-02-18 12:10:03 +0800880 unsigned char *p_certificate_list_len;
Jerry Yu5cc35062022-01-28 16:16:08 +0800881
Jerry Yu5cc35062022-01-28 16:16:08 +0800882
Jerry Yu3391ac02022-02-16 11:21:37 +0800883 /* ...
884 * opaque certificate_request_context<0..2^8-1>;
885 * ...
886 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100887 MBEDTLS_SSL_CHK_BUF_PTR(p, end, certificate_request_context_len + 1);
Jerry Yuc8d8d4e2022-02-18 12:10:03 +0800888 *p++ = certificate_request_context_len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100889 if (certificate_request_context_len > 0) {
890 memcpy(p, certificate_request_context, certificate_request_context_len);
Jerry Yuc8d8d4e2022-02-18 12:10:03 +0800891 p += certificate_request_context_len;
Jerry Yu537530d2022-02-15 14:00:57 +0800892 }
893
Jerry Yu3391ac02022-02-16 11:21:37 +0800894 /* ...
895 * CertificateEntry certificate_list<0..2^24-1>;
896 * ...
897 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100898 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 3);
Jerry Yuc8d8d4e2022-02-18 12:10:03 +0800899 p_certificate_list_len = p;
Jerry Yu3e536442022-02-15 11:05:59 +0800900 p += 3;
901
Gilles Peskine449bd832023-01-11 14:50:10 +0100902 MBEDTLS_SSL_DEBUG_CRT(3, "own certificate", crt);
Jerry Yu5cc35062022-01-28 16:16:08 +0800903
Gilles Peskine449bd832023-01-11 14:50:10 +0100904 while (crt != NULL) {
Jerry Yu7399d0d2022-01-30 17:54:19 +0800905 size_t cert_data_len = crt->raw.len;
Jerry Yu5cc35062022-01-28 16:16:08 +0800906
Gilles Peskine449bd832023-01-11 14:50:10 +0100907 MBEDTLS_SSL_CHK_BUF_PTR(p, end, cert_data_len + 3 + 2);
908 MBEDTLS_PUT_UINT24_BE(cert_data_len, p, 0);
Jerry Yu7399d0d2022-01-30 17:54:19 +0800909 p += 3;
Jerry Yu5cc35062022-01-28 16:16:08 +0800910
Gilles Peskine449bd832023-01-11 14:50:10 +0100911 memcpy(p, crt->raw.p, cert_data_len);
Jerry Yu7399d0d2022-01-30 17:54:19 +0800912 p += cert_data_len;
913 crt = crt->next;
Jerry Yu5cc35062022-01-28 16:16:08 +0800914
915 /* Currently, we don't have any certificate extensions defined.
916 * Hence, we are sending an empty extension with length zero.
917 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100918 MBEDTLS_PUT_UINT16_BE(0, p, 0);
Jerry Yu7399d0d2022-01-30 17:54:19 +0800919 p += 2;
Jerry Yu5cc35062022-01-28 16:16:08 +0800920 }
Jerry Yu5cc35062022-01-28 16:16:08 +0800921
Gilles Peskine449bd832023-01-11 14:50:10 +0100922 MBEDTLS_PUT_UINT24_BE(p - p_certificate_list_len - 3,
923 p_certificate_list_len, 0);
Jerry Yu7399d0d2022-01-30 17:54:19 +0800924
Jerry Yu3e536442022-02-15 11:05:59 +0800925 *out_len = p - buf;
Jerry Yu5cc35062022-01-28 16:16:08 +0800926
Jerry Yu7de2ff02022-11-08 21:43:46 +0800927 MBEDTLS_SSL_PRINT_EXTS(
Gilles Peskine449bd832023-01-11 14:50:10 +0100928 3, MBEDTLS_SSL_HS_CERTIFICATE, ssl->handshake->sent_extensions);
Jerry Yu4b8f2f72022-10-31 13:31:22 +0800929
Gilles Peskine449bd832023-01-11 14:50:10 +0100930 return 0;
Jerry Yu5cc35062022-01-28 16:16:08 +0800931}
Jerry Yu5cc35062022-01-28 16:16:08 +0800932
Gilles Peskine449bd832023-01-11 14:50:10 +0100933int mbedtls_ssl_tls13_write_certificate(mbedtls_ssl_context *ssl)
Jerry Yu5cc35062022-01-28 16:16:08 +0800934{
935 int ret;
Ronald Cron5bb8fc82022-03-09 07:00:13 +0100936 unsigned char *buf;
937 size_t buf_len, msg_len;
938
Gilles Peskine449bd832023-01-11 14:50:10 +0100939 MBEDTLS_SSL_DEBUG_MSG(2, ("=> write certificate"));
Jerry Yu5cc35062022-01-28 16:16:08 +0800940
Xiaokang Qian73437382023-03-29 08:24:12 +0000941 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_start_handshake_msg(
942 ssl, MBEDTLS_SSL_HS_CERTIFICATE, &buf, &buf_len));
Jerry Yu5cc35062022-01-28 16:16:08 +0800943
Gilles Peskine449bd832023-01-11 14:50:10 +0100944 MBEDTLS_SSL_PROC_CHK(ssl_tls13_write_certificate_body(ssl,
945 buf,
946 buf + buf_len,
947 &msg_len));
Jerry Yu5cc35062022-01-28 16:16:08 +0800948
Xiaokang Qian73437382023-03-29 08:24:12 +0000949 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_add_hs_msg_to_checksum(
950 ssl, MBEDTLS_SSL_HS_CERTIFICATE, buf, msg_len));
Jerry Yu5cc35062022-01-28 16:16:08 +0800951
Gilles Peskine449bd832023-01-11 14:50:10 +0100952 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_finish_handshake_msg(
953 ssl, buf_len, msg_len));
Jerry Yu5cc35062022-01-28 16:16:08 +0800954cleanup:
955
Gilles Peskine449bd832023-01-11 14:50:10 +0100956 MBEDTLS_SSL_DEBUG_MSG(2, ("<= write certificate"));
957 return ret;
Jerry Yu5cc35062022-01-28 16:16:08 +0800958}
959
Jerry Yu3e536442022-02-15 11:05:59 +0800960/*
961 * STATE HANDLING: Output Certificate Verify
962 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100963int mbedtls_ssl_tls13_check_sig_alg_cert_key_match(uint16_t sig_alg,
964 mbedtls_pk_context *key)
Jerry Yu0c6be8f2022-06-20 20:42:00 +0800965{
Gilles Peskine449bd832023-01-11 14:50:10 +0100966 mbedtls_pk_type_t pk_type = mbedtls_ssl_sig_from_pk(key);
967 size_t key_size = mbedtls_pk_get_bitlen(key);
Jerry Yu0c6be8f2022-06-20 20:42:00 +0800968
Gilles Peskine449bd832023-01-11 14:50:10 +0100969 switch (pk_type) {
Jerry Yu67eced02022-02-25 13:37:36 +0800970 case MBEDTLS_SSL_SIG_ECDSA:
Gilles Peskine449bd832023-01-11 14:50:10 +0100971 switch (key_size) {
Jerry Yu67eced02022-02-25 13:37:36 +0800972 case 256:
Gilles Peskine449bd832023-01-11 14:50:10 +0100973 return
974 sig_alg == MBEDTLS_TLS1_3_SIG_ECDSA_SECP256R1_SHA256;
Jerry Yu0c6be8f2022-06-20 20:42:00 +0800975
Jerry Yu67eced02022-02-25 13:37:36 +0800976 case 384:
Gilles Peskine449bd832023-01-11 14:50:10 +0100977 return
978 sig_alg == MBEDTLS_TLS1_3_SIG_ECDSA_SECP384R1_SHA384;
Jerry Yu0c6be8f2022-06-20 20:42:00 +0800979
Jerry Yu67eced02022-02-25 13:37:36 +0800980 case 521:
Gilles Peskine449bd832023-01-11 14:50:10 +0100981 return
982 sig_alg == MBEDTLS_TLS1_3_SIG_ECDSA_SECP521R1_SHA512;
Jerry Yu67eced02022-02-25 13:37:36 +0800983 default:
Jerry Yu67eced02022-02-25 13:37:36 +0800984 break;
985 }
986 break;
Jerry Yu67eced02022-02-25 13:37:36 +0800987
Jerry Yu67eced02022-02-25 13:37:36 +0800988 case MBEDTLS_SSL_SIG_RSA:
Gilles Peskine449bd832023-01-11 14:50:10 +0100989 switch (sig_alg) {
Ronald Cron38391bf2022-09-16 11:19:27 +0200990 case MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA256: /* Intentional fallthrough */
991 case MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA384: /* Intentional fallthrough */
Jerry Yu0c6be8f2022-06-20 20:42:00 +0800992 case MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA512:
Gilles Peskine449bd832023-01-11 14:50:10 +0100993 return 1;
Jerry Yuc2e04932022-06-27 22:13:03 +0800994
Jerry Yu0c6be8f2022-06-20 20:42:00 +0800995 default:
996 break;
Jerry Yucef3f332022-03-22 23:00:13 +0800997 }
Jerry Yu67eced02022-02-25 13:37:36 +0800998 break;
Jerry Yu0c6be8f2022-06-20 20:42:00 +0800999
Jerry Yu67eced02022-02-25 13:37:36 +08001000 default:
Jerry Yu67eced02022-02-25 13:37:36 +08001001 break;
1002 }
Jerry Yu0c6be8f2022-06-20 20:42:00 +08001003
Gilles Peskine449bd832023-01-11 14:50:10 +01001004 return 0;
Jerry Yu0c6be8f2022-06-20 20:42:00 +08001005}
1006
Ronald Cronce7d76e2022-07-08 18:56:49 +02001007MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01001008static int ssl_tls13_write_certificate_verify_body(mbedtls_ssl_context *ssl,
1009 unsigned char *buf,
1010 unsigned char *end,
1011 size_t *out_len)
Jerry Yu8511f122022-01-29 10:01:04 +08001012{
Ronald Cron067a1e72022-09-16 13:44:49 +02001013 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yu3e536442022-02-15 11:05:59 +08001014 unsigned char *p = buf;
Jerry Yu8511f122022-01-29 10:01:04 +08001015 mbedtls_pk_context *own_key;
Jerry Yu3e536442022-02-15 11:05:59 +08001016
Gilles Peskine449bd832023-01-11 14:50:10 +01001017 unsigned char handshake_hash[MBEDTLS_TLS1_3_MD_MAX_SIZE];
Jerry Yu8511f122022-01-29 10:01:04 +08001018 size_t handshake_hash_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01001019 unsigned char verify_buffer[SSL_VERIFY_STRUCT_MAX_SIZE];
Jerry Yu3e536442022-02-15 11:05:59 +08001020 size_t verify_buffer_len;
Ronald Cron067a1e72022-09-16 13:44:49 +02001021
1022 uint16_t *sig_alg = ssl->handshake->received_sig_algs;
Jerry Yu3e536442022-02-15 11:05:59 +08001023 size_t signature_len = 0;
Jerry Yu8511f122022-01-29 10:01:04 +08001024
Jerry Yu0b7b1012022-02-23 12:23:05 +08001025 *out_len = 0;
1026
Gilles Peskine449bd832023-01-11 14:50:10 +01001027 own_key = mbedtls_ssl_own_key(ssl);
1028 if (own_key == NULL) {
1029 MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
1030 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Jerry Yu8511f122022-01-29 10:01:04 +08001031 }
1032
Xiaokang Qian73437382023-03-29 08:24:12 +00001033 ret = mbedtls_ssl_get_handshake_transcript(
1034 ssl, ssl->handshake->ciphersuite_info->mac,
1035 handshake_hash, sizeof(handshake_hash), &handshake_hash_len);
Gilles Peskine449bd832023-01-11 14:50:10 +01001036 if (ret != 0) {
1037 return ret;
1038 }
Jerry Yu8511f122022-01-29 10:01:04 +08001039
Gilles Peskine449bd832023-01-11 14:50:10 +01001040 MBEDTLS_SSL_DEBUG_BUF(3, "handshake hash",
1041 handshake_hash,
1042 handshake_hash_len);
Jerry Yu8511f122022-01-29 10:01:04 +08001043
Gilles Peskine449bd832023-01-11 14:50:10 +01001044 ssl_tls13_create_verify_structure(handshake_hash, handshake_hash_len,
1045 verify_buffer, &verify_buffer_len,
1046 ssl->conf->endpoint);
Jerry Yu8511f122022-01-29 10:01:04 +08001047
1048 /*
1049 * struct {
1050 * SignatureScheme algorithm;
1051 * opaque signature<0..2^16-1>;
1052 * } CertificateVerify;
1053 */
Ronald Cron067a1e72022-09-16 13:44:49 +02001054 /* Check there is space for the algorithm identifier (2 bytes) and the
1055 * signature length (2 bytes).
1056 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001057 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 4);
Ronald Cron067a1e72022-09-16 13:44:49 +02001058
Gilles Peskine449bd832023-01-11 14:50:10 +01001059 for (; *sig_alg != MBEDTLS_TLS1_3_SIG_NONE; sig_alg++) {
Ronald Cron067a1e72022-09-16 13:44:49 +02001060 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
1061 mbedtls_pk_type_t pk_type = MBEDTLS_PK_NONE;
1062 mbedtls_md_type_t md_alg = MBEDTLS_MD_NONE;
1063 psa_algorithm_t psa_algorithm = PSA_ALG_NONE;
1064 unsigned char verify_hash[PSA_HASH_MAX_SIZE];
1065 size_t verify_hash_len;
Jerry Yu67eced02022-02-25 13:37:36 +08001066
Gilles Peskine449bd832023-01-11 14:50:10 +01001067 if (!mbedtls_ssl_sig_alg_is_offered(ssl, *sig_alg)) {
Ronald Cron067a1e72022-09-16 13:44:49 +02001068 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +01001069 }
Jerry Yu67eced02022-02-25 13:37:36 +08001070
Gilles Peskine449bd832023-01-11 14:50:10 +01001071 if (!mbedtls_ssl_tls13_sig_alg_for_cert_verify_is_supported(*sig_alg)) {
Ronald Cron067a1e72022-09-16 13:44:49 +02001072 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +01001073 }
Ronald Cron067a1e72022-09-16 13:44:49 +02001074
Gilles Peskine449bd832023-01-11 14:50:10 +01001075 if (!mbedtls_ssl_tls13_check_sig_alg_cert_key_match(*sig_alg, own_key)) {
Ronald Cron067a1e72022-09-16 13:44:49 +02001076 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +01001077 }
Ronald Cron067a1e72022-09-16 13:44:49 +02001078
Gilles Peskine449bd832023-01-11 14:50:10 +01001079 if (mbedtls_ssl_get_pk_type_and_md_alg_from_sig_alg(
1080 *sig_alg, &pk_type, &md_alg) != 0) {
1081 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Ronald Cron067a1e72022-09-16 13:44:49 +02001082 }
1083
1084 /* Hash verify buffer with indicated hash function */
Manuel Pégourié-Gonnard2d6d9932023-03-28 11:38:08 +02001085 psa_algorithm = mbedtls_md_psa_alg_from_type(md_alg);
Gilles Peskine449bd832023-01-11 14:50:10 +01001086 status = psa_hash_compute(psa_algorithm,
1087 verify_buffer,
1088 verify_buffer_len,
1089 verify_hash, sizeof(verify_hash),
1090 &verify_hash_len);
1091 if (status != PSA_SUCCESS) {
Andrzej Kurek8a045ce2022-12-23 11:00:06 -05001092 return PSA_TO_MBEDTLS_ERR(status);
Ronald Cron067a1e72022-09-16 13:44:49 +02001093 }
1094
Gilles Peskine449bd832023-01-11 14:50:10 +01001095 MBEDTLS_SSL_DEBUG_BUF(3, "verify hash", verify_hash, verify_hash_len);
1096
1097 if ((ret = mbedtls_pk_sign_ext(pk_type, own_key,
1098 md_alg, verify_hash, verify_hash_len,
1099 p + 4, (size_t) (end - (p + 4)), &signature_len,
1100 ssl->conf->f_rng, ssl->conf->p_rng)) != 0) {
1101 MBEDTLS_SSL_DEBUG_MSG(2, ("CertificateVerify signature failed with %s",
1102 mbedtls_ssl_sig_alg_to_str(*sig_alg)));
1103 MBEDTLS_SSL_DEBUG_RET(2, "mbedtls_pk_sign_ext", ret);
1104
1105 /* The signature failed. This is possible if the private key
1106 * was not suitable for the signature operation as purposely we
1107 * did not check its suitability completely. Let's try with
1108 * another signature algorithm.
1109 */
1110 continue;
1111 }
1112
1113 MBEDTLS_SSL_DEBUG_MSG(2, ("CertificateVerify signature with %s",
1114 mbedtls_ssl_sig_alg_to_str(*sig_alg)));
Ronald Cron067a1e72022-09-16 13:44:49 +02001115
1116 break;
1117 }
1118
Gilles Peskine449bd832023-01-11 14:50:10 +01001119 if (*sig_alg == MBEDTLS_TLS1_3_SIG_NONE) {
1120 MBEDTLS_SSL_DEBUG_MSG(1, ("no suitable signature algorithm"));
1121 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE,
1122 MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE);
1123 return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
Jerry Yu8511f122022-01-29 10:01:04 +08001124 }
1125
Gilles Peskine449bd832023-01-11 14:50:10 +01001126 MBEDTLS_PUT_UINT16_BE(*sig_alg, p, 0);
1127 MBEDTLS_PUT_UINT16_BE(signature_len, p, 2);
Jerry Yuf3b46b52022-06-19 16:52:27 +08001128
Ronald Cron067a1e72022-09-16 13:44:49 +02001129 *out_len = 4 + signature_len;
Jerry Yu8c338862022-03-23 13:34:04 +08001130
Gilles Peskine449bd832023-01-11 14:50:10 +01001131 return 0;
Jerry Yu8511f122022-01-29 10:01:04 +08001132}
Jerry Yu8511f122022-01-29 10:01:04 +08001133
Gilles Peskine449bd832023-01-11 14:50:10 +01001134int mbedtls_ssl_tls13_write_certificate_verify(mbedtls_ssl_context *ssl)
Jerry Yu8511f122022-01-29 10:01:04 +08001135{
1136 int ret = 0;
Jerry Yuca133a32022-02-15 14:22:05 +08001137 unsigned char *buf;
1138 size_t buf_len, msg_len;
1139
Gilles Peskine449bd832023-01-11 14:50:10 +01001140 MBEDTLS_SSL_DEBUG_MSG(2, ("=> write certificate verify"));
Jerry Yu8511f122022-01-29 10:01:04 +08001141
Xiaokang Qian73437382023-03-29 08:24:12 +00001142 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_start_handshake_msg(
1143 ssl, MBEDTLS_SSL_HS_CERTIFICATE_VERIFY,
1144 &buf, &buf_len));
Jerry Yu8511f122022-01-29 10:01:04 +08001145
Gilles Peskine449bd832023-01-11 14:50:10 +01001146 MBEDTLS_SSL_PROC_CHK(ssl_tls13_write_certificate_verify_body(
1147 ssl, buf, buf + buf_len, &msg_len));
Jerry Yu8511f122022-01-29 10:01:04 +08001148
Xiaokang Qian73437382023-03-29 08:24:12 +00001149 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_add_hs_msg_to_checksum(
1150 ssl, MBEDTLS_SSL_HS_CERTIFICATE_VERIFY,
1151 buf, msg_len));
Jerry Yu8511f122022-01-29 10:01:04 +08001152
Gilles Peskine449bd832023-01-11 14:50:10 +01001153 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_finish_handshake_msg(
1154 ssl, buf_len, msg_len));
Jerry Yu8511f122022-01-29 10:01:04 +08001155
1156cleanup:
1157
Gilles Peskine449bd832023-01-11 14:50:10 +01001158 MBEDTLS_SSL_DEBUG_MSG(2, ("<= write certificate verify"));
1159 return ret;
Jerry Yu8511f122022-01-29 10:01:04 +08001160}
1161
Ronald Cron928cbd32022-10-04 16:14:26 +02001162#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED */
Jerry Yu90f152d2022-01-29 22:12:42 +08001163
Jerry Yu5cc35062022-01-28 16:16:08 +08001164/*
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001165 *
XiaokangQianc5c39d52021-11-09 11:55:10 +00001166 * STATE HANDLING: Incoming Finished message.
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001167 */
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001168/*
1169 * Implementation
1170 */
1171
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001172MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01001173static int ssl_tls13_preprocess_finished_message(mbedtls_ssl_context *ssl)
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001174{
1175 int ret;
1176
Xiaokang Qian73437382023-03-29 08:24:12 +00001177 ret = mbedtls_ssl_tls13_calculate_verify_data(
1178 ssl,
1179 ssl->handshake->state_local.finished_in.digest,
1180 sizeof(ssl->handshake->state_local.finished_in.digest),
1181 &ssl->handshake->state_local.finished_in.digest_len,
1182 ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT ?
1183 MBEDTLS_SSL_IS_SERVER : MBEDTLS_SSL_IS_CLIENT);
Gilles Peskine449bd832023-01-11 14:50:10 +01001184 if (ret != 0) {
1185 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_tls13_calculate_verify_data", ret);
1186 return ret;
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001187 }
1188
Gilles Peskine449bd832023-01-11 14:50:10 +01001189 return 0;
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001190}
1191
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001192MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01001193static int ssl_tls13_parse_finished_message(mbedtls_ssl_context *ssl,
1194 const unsigned char *buf,
1195 const unsigned char *end)
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001196{
XiaokangQian33062842021-11-11 03:37:45 +00001197 /*
1198 * struct {
XiaokangQianc13f9352021-11-11 06:13:22 +00001199 * opaque verify_data[Hash.length];
XiaokangQian33062842021-11-11 03:37:45 +00001200 * } Finished;
1201 */
1202 const unsigned char *expected_verify_data =
1203 ssl->handshake->state_local.finished_in.digest;
1204 size_t expected_verify_data_len =
1205 ssl->handshake->state_local.finished_in.digest_len;
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001206 /* Structural validation */
Gilles Peskine449bd832023-01-11 14:50:10 +01001207 if ((size_t) (end - buf) != expected_verify_data_len) {
1208 MBEDTLS_SSL_DEBUG_MSG(1, ("bad finished message"));
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001209
Gilles Peskine449bd832023-01-11 14:50:10 +01001210 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR,
1211 MBEDTLS_ERR_SSL_DECODE_ERROR);
1212 return MBEDTLS_ERR_SSL_DECODE_ERROR;
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001213 }
1214
Gilles Peskine449bd832023-01-11 14:50:10 +01001215 MBEDTLS_SSL_DEBUG_BUF(4, "verify_data (self-computed):",
1216 expected_verify_data,
1217 expected_verify_data_len);
1218 MBEDTLS_SSL_DEBUG_BUF(4, "verify_data (received message):", buf,
1219 expected_verify_data_len);
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001220
1221 /* Semantic validation */
Gilles Peskine449bd832023-01-11 14:50:10 +01001222 if (mbedtls_ct_memcmp(buf,
1223 expected_verify_data,
1224 expected_verify_data_len) != 0) {
1225 MBEDTLS_SSL_DEBUG_MSG(1, ("bad finished message"));
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001226
Gilles Peskine449bd832023-01-11 14:50:10 +01001227 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_DECRYPT_ERROR,
1228 MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE);
1229 return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001230 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001231 return 0;
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001232}
1233
Gilles Peskine449bd832023-01-11 14:50:10 +01001234int mbedtls_ssl_tls13_process_finished_message(mbedtls_ssl_context *ssl)
XiaokangQianc5c39d52021-11-09 11:55:10 +00001235{
XiaokangQian33062842021-11-11 03:37:45 +00001236 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
XiaokangQianc5c39d52021-11-09 11:55:10 +00001237 unsigned char *buf;
Xiaofei Baieef15042021-11-18 07:29:56 +00001238 size_t buf_len;
XiaokangQianc5c39d52021-11-09 11:55:10 +00001239
Gilles Peskine449bd832023-01-11 14:50:10 +01001240 MBEDTLS_SSL_DEBUG_MSG(2, ("=> parse finished message"));
XiaokangQianc5c39d52021-11-09 11:55:10 +00001241
Xiaokang Qian73437382023-03-29 08:24:12 +00001242 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_tls13_fetch_handshake_msg(
1243 ssl, MBEDTLS_SSL_HS_FINISHED, &buf, &buf_len));
Jerry Yu0a92d6c2022-05-16 16:54:46 +08001244
1245 /* Preprocessing step: Compute handshake digest */
Gilles Peskine449bd832023-01-11 14:50:10 +01001246 MBEDTLS_SSL_PROC_CHK(ssl_tls13_preprocess_finished_message(ssl));
Jerry Yu0a92d6c2022-05-16 16:54:46 +08001247
Xiaokang Qian73437382023-03-29 08:24:12 +00001248 MBEDTLS_SSL_PROC_CHK(ssl_tls13_parse_finished_message(
1249 ssl, buf, buf + buf_len));
Jerry Yu0a92d6c2022-05-16 16:54:46 +08001250
Xiaokang Qian73437382023-03-29 08:24:12 +00001251 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_add_hs_msg_to_checksum(
1252 ssl, MBEDTLS_SSL_HS_FINISHED, buf, buf_len));
XiaokangQianc5c39d52021-11-09 11:55:10 +00001253
1254cleanup:
1255
Gilles Peskine449bd832023-01-11 14:50:10 +01001256 MBEDTLS_SSL_DEBUG_MSG(2, ("<= parse finished message"));
1257 return ret;
XiaokangQianc5c39d52021-11-09 11:55:10 +00001258}
1259
XiaokangQian74af2a82021-09-22 07:40:30 +00001260/*
1261 *
XiaokangQiancc90c942021-11-09 12:30:09 +00001262 * STATE HANDLING: Write and send Finished message.
XiaokangQian74af2a82021-09-22 07:40:30 +00001263 *
1264 */
XiaokangQian74af2a82021-09-22 07:40:30 +00001265/*
XiaokangQian35dc6252021-11-11 08:16:19 +00001266 * Implement
XiaokangQian74af2a82021-09-22 07:40:30 +00001267 */
1268
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001269MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01001270static int ssl_tls13_prepare_finished_message(mbedtls_ssl_context *ssl)
XiaokangQian74af2a82021-09-22 07:40:30 +00001271{
1272 int ret;
1273
1274 /* Compute transcript of handshake up to now. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001275 ret = mbedtls_ssl_tls13_calculate_verify_data(ssl,
1276 ssl->handshake->state_local.finished_out.digest,
1277 sizeof(ssl->handshake->state_local.finished_out.
1278 digest),
1279 &ssl->handshake->state_local.finished_out.digest_len,
1280 ssl->conf->endpoint);
XiaokangQian74af2a82021-09-22 07:40:30 +00001281
Gilles Peskine449bd832023-01-11 14:50:10 +01001282 if (ret != 0) {
1283 MBEDTLS_SSL_DEBUG_RET(1, "calculate_verify_data failed", ret);
1284 return ret;
XiaokangQian74af2a82021-09-22 07:40:30 +00001285 }
1286
Gilles Peskine449bd832023-01-11 14:50:10 +01001287 return 0;
XiaokangQian74af2a82021-09-22 07:40:30 +00001288}
1289
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001290MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01001291static int ssl_tls13_write_finished_message_body(mbedtls_ssl_context *ssl,
1292 unsigned char *buf,
1293 unsigned char *end,
1294 size_t *out_len)
XiaokangQian74af2a82021-09-22 07:40:30 +00001295{
XiaokangQian8773aa02021-11-10 07:33:09 +00001296 size_t verify_data_len = ssl->handshake->state_local.finished_out.digest_len;
XiaokangQian0fa66432021-11-15 03:33:57 +00001297 /*
1298 * struct {
1299 * opaque verify_data[Hash.length];
1300 * } Finished;
1301 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001302 MBEDTLS_SSL_CHK_BUF_PTR(buf, end, verify_data_len);
XiaokangQian74af2a82021-09-22 07:40:30 +00001303
Gilles Peskine449bd832023-01-11 14:50:10 +01001304 memcpy(buf, ssl->handshake->state_local.finished_out.digest,
1305 verify_data_len);
XiaokangQian74af2a82021-09-22 07:40:30 +00001306
Xiaofei Baid25fab62021-12-02 06:36:27 +00001307 *out_len = verify_data_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01001308 return 0;
XiaokangQian74af2a82021-09-22 07:40:30 +00001309}
XiaokangQianc5c39d52021-11-09 11:55:10 +00001310
XiaokangQian35dc6252021-11-11 08:16:19 +00001311/* Main entry point: orchestrates the other functions */
Gilles Peskine449bd832023-01-11 14:50:10 +01001312int mbedtls_ssl_tls13_write_finished_message(mbedtls_ssl_context *ssl)
XiaokangQian35dc6252021-11-11 08:16:19 +00001313{
1314 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1315 unsigned char *buf;
1316 size_t buf_len, msg_len;
1317
Gilles Peskine449bd832023-01-11 14:50:10 +01001318 MBEDTLS_SSL_DEBUG_MSG(2, ("=> write finished message"));
XiaokangQian35dc6252021-11-11 08:16:19 +00001319
Gilles Peskine449bd832023-01-11 14:50:10 +01001320 MBEDTLS_SSL_PROC_CHK(ssl_tls13_prepare_finished_message(ssl));
XiaokangQiandce82242021-11-15 06:01:26 +00001321
Gilles Peskine449bd832023-01-11 14:50:10 +01001322 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_start_handshake_msg(ssl,
1323 MBEDTLS_SSL_HS_FINISHED, &buf, &buf_len));
XiaokangQian35dc6252021-11-11 08:16:19 +00001324
Gilles Peskine449bd832023-01-11 14:50:10 +01001325 MBEDTLS_SSL_PROC_CHK(ssl_tls13_write_finished_message_body(
1326 ssl, buf, buf + buf_len, &msg_len));
XiaokangQian35dc6252021-11-11 08:16:19 +00001327
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +01001328 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_add_hs_msg_to_checksum(ssl,
Manuel Pégourié-Gonnard43cc1272023-02-06 11:48:19 +01001329 MBEDTLS_SSL_HS_FINISHED, buf, msg_len));
XiaokangQian35dc6252021-11-11 08:16:19 +00001330
Gilles Peskine449bd832023-01-11 14:50:10 +01001331 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_finish_handshake_msg(
1332 ssl, buf_len, msg_len));
XiaokangQian35dc6252021-11-11 08:16:19 +00001333cleanup:
1334
Gilles Peskine449bd832023-01-11 14:50:10 +01001335 MBEDTLS_SSL_DEBUG_MSG(2, ("<= write finished message"));
1336 return ret;
XiaokangQian35dc6252021-11-11 08:16:19 +00001337}
1338
Gilles Peskine449bd832023-01-11 14:50:10 +01001339void mbedtls_ssl_tls13_handshake_wrapup(mbedtls_ssl_context *ssl)
Jerry Yu378254d2021-10-30 21:44:47 +08001340{
1341
Gilles Peskine449bd832023-01-11 14:50:10 +01001342 MBEDTLS_SSL_DEBUG_MSG(3, ("=> handshake wrapup"));
Jerry Yu378254d2021-10-30 21:44:47 +08001343
Gilles Peskine449bd832023-01-11 14:50:10 +01001344 MBEDTLS_SSL_DEBUG_MSG(1, ("Switch to application keys for inbound traffic"));
1345 mbedtls_ssl_set_inbound_transform(ssl, ssl->transform_application);
Jerry Yue8c1fca2022-05-18 14:48:56 +08001346
Gilles Peskine449bd832023-01-11 14:50:10 +01001347 MBEDTLS_SSL_DEBUG_MSG(1, ("Switch to application keys for outbound traffic"));
1348 mbedtls_ssl_set_outbound_transform(ssl, ssl->transform_application);
Jerry Yue8c1fca2022-05-18 14:48:56 +08001349
Jerry Yu378254d2021-10-30 21:44:47 +08001350 /*
Jerry Yucfe64f02021-11-15 13:54:06 +08001351 * Free the previous session and switch to the current one.
Jerry Yu378254d2021-10-30 21:44:47 +08001352 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001353 if (ssl->session) {
1354 mbedtls_ssl_session_free(ssl->session);
1355 mbedtls_free(ssl->session);
Jerry Yu378254d2021-10-30 21:44:47 +08001356 }
1357 ssl->session = ssl->session_negotiate;
1358 ssl->session_negotiate = NULL;
1359
Gilles Peskine449bd832023-01-11 14:50:10 +01001360 MBEDTLS_SSL_DEBUG_MSG(3, ("<= handshake wrapup"));
Jerry Yu378254d2021-10-30 21:44:47 +08001361}
1362
Ronald Cron49ad6192021-11-24 16:25:31 +01001363/*
1364 *
1365 * STATE HANDLING: Write ChangeCipherSpec
1366 *
1367 */
1368#if defined(MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001369MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01001370static int ssl_tls13_write_change_cipher_spec_body(mbedtls_ssl_context *ssl,
1371 unsigned char *buf,
1372 unsigned char *end,
1373 size_t *olen)
Ronald Cron49ad6192021-11-24 16:25:31 +01001374{
1375 ((void) ssl);
1376
Gilles Peskine449bd832023-01-11 14:50:10 +01001377 MBEDTLS_SSL_CHK_BUF_PTR(buf, end, 1);
Ronald Cron49ad6192021-11-24 16:25:31 +01001378 buf[0] = 1;
1379 *olen = 1;
1380
Gilles Peskine449bd832023-01-11 14:50:10 +01001381 return 0;
Ronald Cron49ad6192021-11-24 16:25:31 +01001382}
1383
Gilles Peskine449bd832023-01-11 14:50:10 +01001384int mbedtls_ssl_tls13_write_change_cipher_spec(mbedtls_ssl_context *ssl)
Ronald Cron49ad6192021-11-24 16:25:31 +01001385{
1386 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1387
Gilles Peskine449bd832023-01-11 14:50:10 +01001388 MBEDTLS_SSL_DEBUG_MSG(2, ("=> write change cipher spec"));
Ronald Cron49ad6192021-11-24 16:25:31 +01001389
Ronald Cron49ad6192021-11-24 16:25:31 +01001390 /* Write CCS message */
Gilles Peskine449bd832023-01-11 14:50:10 +01001391 MBEDTLS_SSL_PROC_CHK(ssl_tls13_write_change_cipher_spec_body(
1392 ssl, ssl->out_msg,
1393 ssl->out_msg + MBEDTLS_SSL_OUT_CONTENT_LEN,
1394 &ssl->out_msglen));
Ronald Cron49ad6192021-11-24 16:25:31 +01001395
1396 ssl->out_msgtype = MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC;
1397
Ronald Cron49ad6192021-11-24 16:25:31 +01001398 /* Dispatch message */
Gilles Peskine449bd832023-01-11 14:50:10 +01001399 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_write_record(ssl, 0));
Ronald Cron49ad6192021-11-24 16:25:31 +01001400
1401cleanup:
1402
Gilles Peskine449bd832023-01-11 14:50:10 +01001403 MBEDTLS_SSL_DEBUG_MSG(2, ("<= write change cipher spec"));
1404 return ret;
Ronald Cron49ad6192021-11-24 16:25:31 +01001405}
1406
1407#endif /* MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE */
1408
Xiaokang Qianecc29482022-11-02 07:52:47 +00001409/* Early Data Indication Extension
Xiaokang Qian0e97d4d2022-10-24 11:12:51 +00001410 *
Xiaokang Qian0e97d4d2022-10-24 11:12:51 +00001411 * struct {
1412 * select ( Handshake.msg_type ) {
Xiaokang Qianecc29482022-11-02 07:52:47 +00001413 * ...
Xiaokang Qian0e97d4d2022-10-24 11:12:51 +00001414 * case client_hello: Empty;
1415 * case encrypted_extensions: Empty;
1416 * };
1417 * } EarlyDataIndication;
1418 */
1419#if defined(MBEDTLS_SSL_EARLY_DATA)
Gilles Peskine449bd832023-01-11 14:50:10 +01001420int mbedtls_ssl_tls13_write_early_data_ext(mbedtls_ssl_context *ssl,
1421 unsigned char *buf,
1422 const unsigned char *end,
1423 size_t *out_len)
Xiaokang Qian0e97d4d2022-10-24 11:12:51 +00001424{
1425 unsigned char *p = buf;
1426 *out_len = 0;
1427 ((void) ssl);
1428
Gilles Peskine449bd832023-01-11 14:50:10 +01001429 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 4);
Xiaokang Qian0e97d4d2022-10-24 11:12:51 +00001430
Gilles Peskine449bd832023-01-11 14:50:10 +01001431 MBEDTLS_PUT_UINT16_BE(MBEDTLS_TLS_EXT_EARLY_DATA, p, 0);
1432 MBEDTLS_PUT_UINT16_BE(0, p, 2);
Xiaokang Qian0e97d4d2022-10-24 11:12:51 +00001433
1434 *out_len = 4;
Xiaokang Qian2cd5ce02022-11-15 10:33:53 +00001435
Gilles Peskine449bd832023-01-11 14:50:10 +01001436 mbedtls_ssl_tls13_set_hs_sent_ext_mask(ssl, MBEDTLS_TLS_EXT_EARLY_DATA);
Xiaokang Qian2cd5ce02022-11-15 10:33:53 +00001437
Gilles Peskine449bd832023-01-11 14:50:10 +01001438 return 0;
Xiaokang Qian0e97d4d2022-10-24 11:12:51 +00001439}
1440#endif /* MBEDTLS_SSL_EARLY_DATA */
1441
XiaokangQian78b1fa72022-01-19 06:56:30 +00001442/* Reset SSL context and update hash for handling HRR.
1443 *
1444 * Replace Transcript-Hash(X) by
1445 * Transcript-Hash( message_hash ||
1446 * 00 00 Hash.length ||
1447 * X )
1448 * A few states of the handshake are preserved, including:
1449 * - session ID
1450 * - session ticket
1451 * - negotiated ciphersuite
1452 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001453int mbedtls_ssl_reset_transcript_for_hrr(mbedtls_ssl_context *ssl)
XiaokangQian78b1fa72022-01-19 06:56:30 +00001454{
1455 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Przemyslaw Stekielda645252022-09-14 12:50:51 +02001456 unsigned char hash_transcript[PSA_HASH_MAX_SIZE + 4];
XiaokangQian0ece9982022-01-24 08:56:23 +00001457 size_t hash_len;
Xiaokang Qian6b980012023-02-07 03:17:45 +00001458 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
1459 ssl->handshake->ciphersuite_info;
XiaokangQian78b1fa72022-01-19 06:56:30 +00001460
Gilles Peskine449bd832023-01-11 14:50:10 +01001461 MBEDTLS_SSL_DEBUG_MSG(3, ("Reset SSL session for HRR"));
XiaokangQian78b1fa72022-01-19 06:56:30 +00001462
Gilles Peskine449bd832023-01-11 14:50:10 +01001463 ret = mbedtls_ssl_get_handshake_transcript(ssl, ciphersuite_info->mac,
1464 hash_transcript + 4,
1465 PSA_HASH_MAX_SIZE,
1466 &hash_len);
1467 if (ret != 0) {
Manuel Pégourié-Gonnardda7979b2023-02-21 09:31:10 +01001468 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_get_handshake_transcript", ret);
Gilles Peskine449bd832023-01-11 14:50:10 +01001469 return ret;
XiaokangQian0ece9982022-01-24 08:56:23 +00001470 }
1471
1472 hash_transcript[0] = MBEDTLS_SSL_HS_MESSAGE_HASH;
1473 hash_transcript[1] = 0;
1474 hash_transcript[2] = 0;
1475 hash_transcript[3] = (unsigned char) hash_len;
1476
1477 hash_len += 4;
1478
Manuel Pégourié-Gonnardda7979b2023-02-21 09:31:10 +01001479 MBEDTLS_SSL_DEBUG_BUF(4, "Truncated handshake transcript",
1480 hash_transcript, hash_len);
1481
Manuel Pégourié-Gonnardd7a7a232023-02-05 10:26:49 +01001482 /* Reset running hash and replace it with a hash of the transcript */
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +01001483 ret = mbedtls_ssl_reset_checksum(ssl);
1484 if (ret != 0) {
1485 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_reset_checksum", ret);
1486 return ret;
1487 }
1488 ret = ssl->handshake->update_checksum(ssl, hash_transcript, hash_len);
1489 if (ret != 0) {
1490 MBEDTLS_SSL_DEBUG_RET(1, "update_checksum", ret);
1491 return ret;
1492 }
Przemyslaw Stekiel4b3fff42022-02-14 16:39:52 +01001493
Gilles Peskine449bd832023-01-11 14:50:10 +01001494 return ret;
XiaokangQian78b1fa72022-01-19 06:56:30 +00001495}
1496
Valerio Setti080a22b2023-03-20 15:22:47 +01001497#if defined(PSA_WANT_ALG_ECDH)
XiaokangQian7807f9f2022-02-15 10:04:37 +00001498
Gilles Peskine449bd832023-01-11 14:50:10 +01001499int mbedtls_ssl_tls13_read_public_ecdhe_share(mbedtls_ssl_context *ssl,
1500 const unsigned char *buf,
1501 size_t buf_len)
XiaokangQian7807f9f2022-02-15 10:04:37 +00001502{
Gilles Peskine449bd832023-01-11 14:50:10 +01001503 uint8_t *p = (uint8_t *) buf;
XiaokangQiancfd925f2022-04-14 07:10:37 +00001504 const uint8_t *end = buf + buf_len;
XiaokangQian9b5d04b2022-04-10 10:20:43 +00001505 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001506
XiaokangQian9b5d04b2022-04-10 10:20:43 +00001507 /* Get size of the TLS opaque key_exchange field of the KeyShareEntry struct. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001508 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, 2);
1509 uint16_t peerkey_len = MBEDTLS_GET_UINT16_BE(p, 0);
XiaokangQian9b5d04b2022-04-10 10:20:43 +00001510 p += 2;
XiaokangQian3207a322022-02-23 03:15:27 +00001511
XiaokangQian9b5d04b2022-04-10 10:20:43 +00001512 /* Check if key size is consistent with given buffer length. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001513 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, peerkey_len);
XiaokangQian9b5d04b2022-04-10 10:20:43 +00001514
1515 /* Store peer's ECDH public key. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001516 memcpy(handshake->ecdh_psa_peerkey, p, peerkey_len);
XiaokangQian9b5d04b2022-04-10 10:20:43 +00001517 handshake->ecdh_psa_peerkey_len = peerkey_len;
1518
Gilles Peskine449bd832023-01-11 14:50:10 +01001519 return 0;
XiaokangQian3207a322022-02-23 03:15:27 +00001520}
Jerry Yu89e103c2022-03-30 22:43:29 +08001521
1522int mbedtls_ssl_tls13_generate_and_write_ecdh_key_exchange(
Gilles Peskine449bd832023-01-11 14:50:10 +01001523 mbedtls_ssl_context *ssl,
1524 uint16_t named_group,
1525 unsigned char *buf,
1526 unsigned char *end,
1527 size_t *out_len)
Jerry Yu89e103c2022-03-30 22:43:29 +08001528{
1529 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
1530 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
1531 psa_key_attributes_t key_attributes;
1532 size_t own_pubkey_len;
1533 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
Valerio Setti40d9ca92023-01-04 16:08:04 +01001534 psa_ecc_family_t ec_psa_family = 0;
1535 size_t ec_bits = 0;
Jerry Yu89e103c2022-03-30 22:43:29 +08001536
Gilles Peskine449bd832023-01-11 14:50:10 +01001537 MBEDTLS_SSL_DEBUG_MSG(1, ("Perform PSA-based ECDH computation."));
Jerry Yu89e103c2022-03-30 22:43:29 +08001538
Valerio Setti40d9ca92023-01-04 16:08:04 +01001539 /* Convert EC's TLS ID to PSA key type. */
Xiaokang Qian73437382023-03-29 08:24:12 +00001540 if (mbedtls_ssl_get_psa_curve_info_from_tls_id(
1541 named_group, &ec_psa_family, &ec_bits) == PSA_ERROR_NOT_SUPPORTED) {
Gilles Peskine449bd832023-01-11 14:50:10 +01001542 return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
Valerio Setti40d9ca92023-01-04 16:08:04 +01001543 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001544 handshake->ecdh_psa_type = PSA_KEY_TYPE_ECC_KEY_PAIR(ec_psa_family);
Valerio Setti40d9ca92023-01-04 16:08:04 +01001545 ssl->handshake->ecdh_bits = ec_bits;
Jerry Yu89e103c2022-03-30 22:43:29 +08001546
1547 key_attributes = psa_key_attributes_init();
Gilles Peskine449bd832023-01-11 14:50:10 +01001548 psa_set_key_usage_flags(&key_attributes, PSA_KEY_USAGE_DERIVE);
1549 psa_set_key_algorithm(&key_attributes, PSA_ALG_ECDH);
1550 psa_set_key_type(&key_attributes, handshake->ecdh_psa_type);
1551 psa_set_key_bits(&key_attributes, handshake->ecdh_bits);
Jerry Yu89e103c2022-03-30 22:43:29 +08001552
1553 /* Generate ECDH private key. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001554 status = psa_generate_key(&key_attributes,
1555 &handshake->ecdh_psa_privkey);
1556 if (status != PSA_SUCCESS) {
Andrzej Kurek8a045ce2022-12-23 11:00:06 -05001557 ret = PSA_TO_MBEDTLS_ERR(status);
Gilles Peskine449bd832023-01-11 14:50:10 +01001558 MBEDTLS_SSL_DEBUG_RET(1, "psa_generate_key", ret);
1559 return ret;
Jerry Yu89e103c2022-03-30 22:43:29 +08001560
1561 }
1562
1563 /* Export the public part of the ECDH private key from PSA. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001564 status = psa_export_public_key(handshake->ecdh_psa_privkey,
1565 buf, (size_t) (end - buf),
1566 &own_pubkey_len);
1567 if (status != PSA_SUCCESS) {
Andrzej Kurek8a045ce2022-12-23 11:00:06 -05001568 ret = PSA_TO_MBEDTLS_ERR(status);
Gilles Peskine449bd832023-01-11 14:50:10 +01001569 MBEDTLS_SSL_DEBUG_RET(1, "psa_export_public_key", ret);
1570 return ret;
Jerry Yu89e103c2022-03-30 22:43:29 +08001571
1572 }
1573
1574 *out_len = own_pubkey_len;
1575
Gilles Peskine449bd832023-01-11 14:50:10 +01001576 return 0;
Jerry Yu89e103c2022-03-30 22:43:29 +08001577}
Valerio Setti080a22b2023-03-20 15:22:47 +01001578#endif /* PSA_WANT_ALG_ECDH */
XiaokangQian7807f9f2022-02-15 10:04:37 +00001579
Jerry Yu0c354a22022-08-29 15:25:36 +08001580/* RFC 8446 section 4.2
1581 *
1582 * If an implementation receives an extension which it recognizes and which is
1583 * not specified for the message in which it appears, it MUST abort the handshake
1584 * with an "illegal_parameter" alert.
1585 *
1586 */
Jerry Yuc4bf5d62022-10-29 09:08:47 +08001587int mbedtls_ssl_tls13_check_received_extension(
Gilles Peskine449bd832023-01-11 14:50:10 +01001588 mbedtls_ssl_context *ssl,
1589 int hs_msg_type,
1590 unsigned int received_extension_type,
1591 uint32_t hs_msg_allowed_extensions_mask)
Jerry Yu0c354a22022-08-29 15:25:36 +08001592{
Jerry Yudf0ad652022-10-31 13:20:57 +08001593 uint32_t extension_mask = mbedtls_ssl_get_extension_mask(
Gilles Peskine449bd832023-01-11 14:50:10 +01001594 received_extension_type);
Jerry Yu0c354a22022-08-29 15:25:36 +08001595
Jerry Yu79aa7212022-11-08 21:30:21 +08001596 MBEDTLS_SSL_PRINT_EXT(
Gilles Peskine449bd832023-01-11 14:50:10 +01001597 3, hs_msg_type, received_extension_type, "received");
Jerry Yu0c354a22022-08-29 15:25:36 +08001598
Gilles Peskine449bd832023-01-11 14:50:10 +01001599 if ((extension_mask & hs_msg_allowed_extensions_mask) == 0) {
Jerry Yu79aa7212022-11-08 21:30:21 +08001600 MBEDTLS_SSL_PRINT_EXT(
Gilles Peskine449bd832023-01-11 14:50:10 +01001601 3, hs_msg_type, received_extension_type, "is illegal");
Jerry Yu0c354a22022-08-29 15:25:36 +08001602 MBEDTLS_SSL_PEND_FATAL_ALERT(
1603 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER,
Gilles Peskine449bd832023-01-11 14:50:10 +01001604 MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER);
1605 return MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER;
Jerry Yu0c354a22022-08-29 15:25:36 +08001606 }
1607
1608 ssl->handshake->received_extensions |= extension_mask;
Jerry Yuc4bf5d62022-10-29 09:08:47 +08001609 /*
1610 * If it is a message containing extension responses, check that we
1611 * previously sent the extension.
1612 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001613 switch (hs_msg_type) {
Jerry Yu0c354a22022-08-29 15:25:36 +08001614 case MBEDTLS_SSL_HS_SERVER_HELLO:
Jerry Yudf0ad652022-10-31 13:20:57 +08001615 case MBEDTLS_SSL_TLS1_3_HS_HELLO_RETRY_REQUEST:
Jerry Yu0c354a22022-08-29 15:25:36 +08001616 case MBEDTLS_SSL_HS_ENCRYPTED_EXTENSIONS:
1617 case MBEDTLS_SSL_HS_CERTIFICATE:
Jerry Yuc4bf5d62022-10-29 09:08:47 +08001618 /* Check if the received extension is sent by peer message.*/
Gilles Peskine449bd832023-01-11 14:50:10 +01001619 if ((ssl->handshake->sent_extensions & extension_mask) != 0) {
1620 return 0;
1621 }
Jerry Yu0c354a22022-08-29 15:25:36 +08001622 break;
1623 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01001624 return 0;
Jerry Yu0c354a22022-08-29 15:25:36 +08001625 }
1626
Jerry Yu79aa7212022-11-08 21:30:21 +08001627 MBEDTLS_SSL_PRINT_EXT(
Gilles Peskine449bd832023-01-11 14:50:10 +01001628 3, hs_msg_type, received_extension_type, "is unsupported");
Jerry Yu0c354a22022-08-29 15:25:36 +08001629 MBEDTLS_SSL_PEND_FATAL_ALERT(
1630 MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_EXT,
Gilles Peskine449bd832023-01-11 14:50:10 +01001631 MBEDTLS_ERR_SSL_UNSUPPORTED_EXTENSION);
1632 return MBEDTLS_ERR_SSL_UNSUPPORTED_EXTENSION;
Jerry Yu0c354a22022-08-29 15:25:36 +08001633}
1634
Jan Bruckner151f6422023-02-10 12:45:19 +01001635#if defined(MBEDTLS_SSL_RECORD_SIZE_LIMIT)
Jan Bruckner1a38e542023-03-15 14:15:11 +01001636/* RFC 8449, section 4:
1637 *
Jan Bruckner151f6422023-02-10 12:45:19 +01001638 * The ExtensionData of the "record_size_limit" extension is
1639 * RecordSizeLimit:
1640 * uint16 RecordSizeLimit;
1641 */
1642MBEDTLS_CHECK_RETURN_CRITICAL
1643int mbedtls_ssl_tls13_parse_record_size_limit_ext(mbedtls_ssl_context *ssl,
1644 const unsigned char *buf,
1645 const unsigned char *end)
1646{
Jan Bruckner1a38e542023-03-15 14:15:11 +01001647 const unsigned char *p = buf;
1648 uint16_t record_size_limit;
Jan Brucknera0589e72023-03-15 11:04:45 +01001649 const size_t extension_data_len = end - buf;
Jan Bruckner1a38e542023-03-15 14:15:11 +01001650
Xiaokang Qian73437382023-03-29 08:24:12 +00001651 if (extension_data_len !=
1652 MBEDTLS_SSL_RECORD_SIZE_LIMIT_EXTENSION_DATA_LENGTH) {
Jan Bruckner151f6422023-02-10 12:45:19 +01001653 MBEDTLS_SSL_DEBUG_MSG(2,
Jan Bruckner1a38e542023-03-15 14:15:11 +01001654 ("record_size_limit extension has invalid length: %"
1655 MBEDTLS_PRINTF_SIZET " Bytes",
Jan Bruckner151f6422023-02-10 12:45:19 +01001656 extension_data_len));
1657
1658 MBEDTLS_SSL_PEND_FATAL_ALERT(
1659 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER,
1660 MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER);
1661 return MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER;
1662 }
1663
Jan Bruckner151f6422023-02-10 12:45:19 +01001664 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, 2);
1665 record_size_limit = MBEDTLS_GET_UINT16_BE(p, 0);
1666
1667 MBEDTLS_SSL_DEBUG_MSG(2, ("RecordSizeLimit: %u Bytes", record_size_limit));
1668
Jan Bruckner1a38e542023-03-15 14:15:11 +01001669 /* RFC 8449, section 4
Jan Bruckner151f6422023-02-10 12:45:19 +01001670 *
1671 * Endpoints MUST NOT send a "record_size_limit" extension with a value
1672 * smaller than 64. An endpoint MUST treat receipt of a smaller value
1673 * as a fatal error and generate an "illegal_parameter" alert.
1674 */
Jan Brucknera0589e72023-03-15 11:04:45 +01001675 if (record_size_limit < MBEDTLS_SSL_RECORD_SIZE_LIMIT_MIN) {
Jan Bruckner151f6422023-02-10 12:45:19 +01001676 MBEDTLS_SSL_PEND_FATAL_ALERT(
1677 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER,
1678 MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER);
1679 return MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER;
1680 }
1681
Xiaokang Qian73437382023-03-29 08:24:12 +00001682 MBEDTLS_SSL_DEBUG_MSG(
1683 2, ("record_size_limit extension is still in development. Aborting handshake."));
Jan Bruckner151f6422023-02-10 12:45:19 +01001684
1685 MBEDTLS_SSL_PEND_FATAL_ALERT(
1686 MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_EXT,
1687 MBEDTLS_ERR_SSL_UNSUPPORTED_EXTENSION);
1688 return MBEDTLS_ERR_SSL_UNSUPPORTED_EXTENSION;
1689}
1690#endif /* MBEDTLS_SSL_RECORD_SIZE_LIMIT */
1691
Jerry Yufb4b6472022-01-27 15:03:26 +08001692#endif /* MBEDTLS_SSL_TLS_C && MBEDTLS_SSL_PROTO_TLS1_3 */