blob: a7fddf9ca54ed12b80a0c5681334ba8a12ca2a73 [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 *
Xiaokang Qian73437382023-03-29 08:24:12 +0000250 * If the CertificateVerify message is sent by a server, the signature
251 * algorithm MUST be one offered in the client's "signature_algorithms"
252 * extension unless no valid certificate chain can be produced without
253 * unsupported algorithms
Jerry Yu30b071c2021-09-12 20:16:03 +0800254 *
255 * RFC 8446 section 4.4.2.2
256 *
257 * If the client cannot construct an acceptable chain using the provided
Xiaokang Qian73437382023-03-29 08:24:12 +0000258 * certificates and decides to abort the handshake, then it MUST abort the
259 * handshake with an appropriate certificate-related alert
260 * (by default, "unsupported_certificate").
Jerry Yu30b071c2021-09-12 20:16:03 +0800261 *
Jerry Yu6f87f252021-10-29 20:12:51 +0800262 * Check if algorithm is an offered signature algorithm.
Jerry Yu30b071c2021-09-12 20:16:03 +0800263 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100264 if (!mbedtls_ssl_sig_alg_is_offered(ssl, algorithm)) {
Jerry Yu982d9e52021-10-14 15:59:37 +0800265 /* algorithm not in offered signature algorithms list */
Gilles Peskine449bd832023-01-11 14:50:10 +0100266 MBEDTLS_SSL_DEBUG_MSG(1, ("Received signature algorithm(%04x) is not "
267 "offered.",
268 (unsigned int) algorithm));
Jerry Yu6f87f252021-10-29 20:12:51 +0800269 goto error;
Jerry Yu30b071c2021-09-12 20:16:03 +0800270 }
271
Gilles Peskine449bd832023-01-11 14:50:10 +0100272 if (mbedtls_ssl_get_pk_type_and_md_alg_from_sig_alg(
273 algorithm, &sig_alg, &md_alg) != 0) {
Jerry Yu8c338862022-03-23 13:34:04 +0800274 goto error;
Jerry Yu30b071c2021-09-12 20:16:03 +0800275 }
276
Gilles Peskine449bd832023-01-11 14:50:10 +0100277 hash_alg = mbedtls_hash_info_psa_from_md(md_alg);
278 if (hash_alg == 0) {
pespaceka1378102022-04-26 15:03:11 +0200279 goto error;
280 }
281
Gilles Peskine449bd832023-01-11 14:50:10 +0100282 MBEDTLS_SSL_DEBUG_MSG(3, ("Certificate Verify: Signature algorithm ( %04x )",
283 (unsigned int) algorithm));
Jerry Yu30b071c2021-09-12 20:16:03 +0800284
285 /*
286 * Check the certificate's key type matches the signature alg
287 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100288 if (!mbedtls_pk_can_do(&ssl->session_negotiate->peer_cert->pk, sig_alg)) {
289 MBEDTLS_SSL_DEBUG_MSG(1, ("signature algorithm doesn't match cert key"));
Jerry Yu6f87f252021-10-29 20:12:51 +0800290 goto error;
Jerry Yu30b071c2021-09-12 20:16:03 +0800291 }
292
Gilles Peskine449bd832023-01-11 14:50:10 +0100293 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, 2);
294 signature_len = MBEDTLS_GET_UINT16_BE(p, 0);
Jerry Yu30b071c2021-09-12 20:16:03 +0800295 p += 2;
Gilles Peskine449bd832023-01-11 14:50:10 +0100296 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, signature_len);
Jerry Yu30b071c2021-09-12 20:16:03 +0800297
Gilles Peskine449bd832023-01-11 14:50:10 +0100298 status = psa_hash_compute(hash_alg,
299 verify_buffer,
300 verify_buffer_len,
301 verify_hash,
302 sizeof(verify_hash),
303 &verify_hash_len);
304 if (status != PSA_SUCCESS) {
305 MBEDTLS_SSL_DEBUG_RET(1, "hash computation PSA error", status);
Jerry Yu6f87f252021-10-29 20:12:51 +0800306 goto error;
Jerry Yu133690c2021-10-25 14:01:13 +0800307 }
308
Gilles Peskine449bd832023-01-11 14:50:10 +0100309 MBEDTLS_SSL_DEBUG_BUF(3, "verify hash", verify_hash, verify_hash_len);
XiaokangQian82d34cc2021-11-03 08:51:56 +0000310#if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT)
Gilles Peskine449bd832023-01-11 14:50:10 +0100311 if (sig_alg == MBEDTLS_PK_RSASSA_PSS) {
Xiaofei Baid25fab62021-12-02 06:36:27 +0000312 rsassa_pss_options.mgf1_hash_id = md_alg;
Przemek Stekiel6a5e0182022-06-27 11:53:13 +0200313
Gilles Peskine449bd832023-01-11 14:50:10 +0100314 rsassa_pss_options.expected_salt_len = PSA_HASH_LENGTH(hash_alg);
315 options = (const void *) &rsassa_pss_options;
XiaokangQian82d34cc2021-11-03 08:51:56 +0000316 }
317#endif /* MBEDTLS_X509_RSASSA_PSS_SUPPORT */
Jerry Yu30b071c2021-09-12 20:16:03 +0800318
Gilles Peskine449bd832023-01-11 14:50:10 +0100319 if ((ret = mbedtls_pk_verify_ext(sig_alg, options,
320 &ssl->session_negotiate->peer_cert->pk,
321 md_alg, verify_hash, verify_hash_len,
322 p, signature_len)) == 0) {
323 return 0;
Jerry Yu30b071c2021-09-12 20:16:03 +0800324 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100325 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_pk_verify_ext", ret);
Jerry Yu30b071c2021-09-12 20:16:03 +0800326
Jerry Yu6f87f252021-10-29 20:12:51 +0800327error:
328 /* RFC 8446 section 4.4.3
329 *
330 * If the verification fails, the receiver MUST terminate the handshake
331 * with a "decrypt_error" alert.
Gilles Peskine449bd832023-01-11 14:50:10 +0100332 */
333 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_DECRYPT_ERROR,
334 MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE);
335 return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
Jerry Yu6f87f252021-10-29 20:12:51 +0800336
Jerry Yu30b071c2021-09-12 20:16:03 +0800337}
Ronald Cron928cbd32022-10-04 16:14:26 +0200338#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED */
Jerry Yu30b071c2021-09-12 20:16:03 +0800339
Gilles Peskine449bd832023-01-11 14:50:10 +0100340int mbedtls_ssl_tls13_process_certificate_verify(mbedtls_ssl_context *ssl)
Jerry Yu30b071c2021-09-12 20:16:03 +0800341{
Jerry Yu30b071c2021-09-12 20:16:03 +0800342
Ronald Cron928cbd32022-10-04 16:14:26 +0200343#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED)
Jerry Yuda8cdf22021-10-25 15:06:49 +0800344 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
345 unsigned char verify_buffer[SSL_VERIFY_STRUCT_MAX_SIZE];
346 size_t verify_buffer_len;
347 unsigned char transcript[MBEDTLS_TLS1_3_MD_MAX_SIZE];
348 size_t transcript_len;
349 unsigned char *buf;
350 size_t buf_len;
351
Gilles Peskine449bd832023-01-11 14:50:10 +0100352 MBEDTLS_SSL_DEBUG_MSG(2, ("=> parse certificate verify"));
Jerry Yu30b071c2021-09-12 20:16:03 +0800353
Jerry Yuda8cdf22021-10-25 15:06:49 +0800354 MBEDTLS_SSL_PROC_CHK(
Xiaokang Qian73437382023-03-29 08:24:12 +0000355 mbedtls_ssl_tls13_fetch_handshake_msg(
356 ssl, MBEDTLS_SSL_HS_CERTIFICATE_VERIFY, &buf, &buf_len));
Jerry Yu30b071c2021-09-12 20:16:03 +0800357
Jerry Yuda8cdf22021-10-25 15:06:49 +0800358 /* Need to calculate the hash of the transcript first
Jerry Yu0b32c502021-10-28 13:41:59 +0800359 * before reading the message since otherwise it gets
360 * included in the transcript
361 */
Xiaokang Qian73437382023-03-29 08:24:12 +0000362 ret = mbedtls_ssl_get_handshake_transcript(
363 ssl,
364 ssl->handshake->ciphersuite_info->mac,
365 transcript, sizeof(transcript),
366 &transcript_len);
Gilles Peskine449bd832023-01-11 14:50:10 +0100367 if (ret != 0) {
Jerry Yuda8cdf22021-10-25 15:06:49 +0800368 MBEDTLS_SSL_PEND_FATAL_ALERT(
369 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR,
Gilles Peskine449bd832023-01-11 14:50:10 +0100370 MBEDTLS_ERR_SSL_INTERNAL_ERROR);
371 return ret;
Jerry Yu30b071c2021-09-12 20:16:03 +0800372 }
373
Gilles Peskine449bd832023-01-11 14:50:10 +0100374 MBEDTLS_SSL_DEBUG_BUF(3, "handshake hash", transcript, transcript_len);
Jerry Yuda8cdf22021-10-25 15:06:49 +0800375
376 /* Create verify structure */
Gilles Peskine449bd832023-01-11 14:50:10 +0100377 ssl_tls13_create_verify_structure(transcript,
378 transcript_len,
379 verify_buffer,
380 &verify_buffer_len,
381 (ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT) ?
382 MBEDTLS_SSL_IS_SERVER :
383 MBEDTLS_SSL_IS_CLIENT);
Jerry Yuda8cdf22021-10-25 15:06:49 +0800384
385 /* Process the message contents */
Xiaokang Qian73437382023-03-29 08:24:12 +0000386 MBEDTLS_SSL_PROC_CHK(ssl_tls13_parse_certificate_verify(
387 ssl, buf, buf + buf_len,
388 verify_buffer, verify_buffer_len));
Jerry Yuda8cdf22021-10-25 15:06:49 +0800389
Xiaokang Qian73437382023-03-29 08:24:12 +0000390 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_add_hs_msg_to_checksum(
391 ssl, MBEDTLS_SSL_HS_CERTIFICATE_VERIFY,
392 buf, buf_len));
Jerry Yu30b071c2021-09-12 20:16:03 +0800393
394cleanup:
395
Gilles Peskine449bd832023-01-11 14:50:10 +0100396 MBEDTLS_SSL_DEBUG_MSG(2, ("<= parse certificate verify"));
397 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_tls13_process_certificate_verify", ret);
398 return ret;
Jerry Yuda8cdf22021-10-25 15:06:49 +0800399#else
400 ((void) ssl);
Gilles Peskine449bd832023-01-11 14:50:10 +0100401 MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
402 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Ronald Cron928cbd32022-10-04 16:14:26 +0200403#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED */
Jerry Yu30b071c2021-09-12 20:16:03 +0800404}
405
406/*
Xiaofei Bai947571e2021-09-29 09:12:03 +0000407 *
XiaokangQian6b916b12022-04-25 07:29:34 +0000408 * STATE HANDLING: Incoming Certificate.
Xiaofei Bai947571e2021-09-29 09:12:03 +0000409 *
410 */
411
Ronald Cronde08cf32022-10-04 17:15:35 +0200412#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED)
Xiaofei Bai947571e2021-09-29 09:12:03 +0000413#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
414/*
415 * Structure of Certificate message:
416 *
417 * enum {
418 * X509(0),
419 * RawPublicKey(2),
420 * (255)
421 * } CertificateType;
422 *
423 * struct {
424 * select (certificate_type) {
425 * case RawPublicKey:
426 * * From RFC 7250 ASN.1_subjectPublicKeyInfo *
427 * opaque ASN1_subjectPublicKeyInfo<1..2^24-1>;
428 * case X509:
429 * opaque cert_data<1..2^24-1>;
430 * };
431 * Extension extensions<0..2^16-1>;
432 * } CertificateEntry;
433 *
434 * struct {
435 * opaque certificate_request_context<0..2^8-1>;
436 * CertificateEntry certificate_list<0..2^24-1>;
437 * } Certificate;
438 *
439 */
Xiaofei Bai79595ac2021-10-26 07:16:45 +0000440
441/* Parse certificate chain send by the server. */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200442MBEDTLS_CHECK_RETURN_CRITICAL
Ronald Crone3dac4a2022-06-10 17:21:51 +0200443MBEDTLS_STATIC_TESTABLE
Gilles Peskine449bd832023-01-11 14:50:10 +0100444int mbedtls_ssl_tls13_parse_certificate(mbedtls_ssl_context *ssl,
445 const unsigned char *buf,
446 const unsigned char *end)
Xiaofei Bai947571e2021-09-29 09:12:03 +0000447{
448 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
449 size_t certificate_request_context_len = 0;
450 size_t certificate_list_len = 0;
451 const unsigned char *p = buf;
452 const unsigned char *certificate_list_end;
Jerry Yuc4bf5d62022-10-29 09:08:47 +0800453 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
Xiaofei Bai947571e2021-09-29 09:12:03 +0000454
Gilles Peskine449bd832023-01-11 14:50:10 +0100455 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, 4);
Xiaofei Bai947571e2021-09-29 09:12:03 +0000456 certificate_request_context_len = p[0];
Gilles Peskine449bd832023-01-11 14:50:10 +0100457 certificate_list_len = MBEDTLS_GET_UINT24_BE(p, 1);
XiaokangQian63e713e2022-05-15 04:26:57 +0000458 p += 4;
Xiaofei Bai947571e2021-09-29 09:12:03 +0000459
460 /* In theory, the certificate list can be up to 2^24 Bytes, but we don't
461 * support anything beyond 2^16 = 64K.
462 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100463 if ((certificate_request_context_len != 0) ||
464 (certificate_list_len >= 0x10000)) {
465 MBEDTLS_SSL_DEBUG_MSG(1, ("bad certificate message"));
466 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR,
467 MBEDTLS_ERR_SSL_DECODE_ERROR);
468 return MBEDTLS_ERR_SSL_DECODE_ERROR;
Xiaofei Bai947571e2021-09-29 09:12:03 +0000469 }
470
471 /* In case we tried to reuse a session but it failed */
Gilles Peskine449bd832023-01-11 14:50:10 +0100472 if (ssl->session_negotiate->peer_cert != NULL) {
473 mbedtls_x509_crt_free(ssl->session_negotiate->peer_cert);
474 mbedtls_free(ssl->session_negotiate->peer_cert);
Xiaofei Bai947571e2021-09-29 09:12:03 +0000475 }
476
Gilles Peskine449bd832023-01-11 14:50:10 +0100477 if (certificate_list_len == 0) {
XiaokangQianc3017f62022-05-13 05:55:41 +0000478 ssl->session_negotiate->peer_cert = NULL;
479 ret = 0;
480 goto exit;
481 }
482
Gilles Peskine449bd832023-01-11 14:50:10 +0100483 if ((ssl->session_negotiate->peer_cert =
484 mbedtls_calloc(1, sizeof(mbedtls_x509_crt))) == NULL) {
485 MBEDTLS_SSL_DEBUG_MSG(1, ("alloc( %" MBEDTLS_PRINTF_SIZET " bytes ) failed",
486 sizeof(mbedtls_x509_crt)));
487 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR,
488 MBEDTLS_ERR_SSL_ALLOC_FAILED);
489 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
Xiaofei Bai947571e2021-09-29 09:12:03 +0000490 }
491
Gilles Peskine449bd832023-01-11 14:50:10 +0100492 mbedtls_x509_crt_init(ssl->session_negotiate->peer_cert);
Xiaofei Bai947571e2021-09-29 09:12:03 +0000493
Gilles Peskine449bd832023-01-11 14:50:10 +0100494 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, certificate_list_len);
Xiaofei Bai947571e2021-09-29 09:12:03 +0000495 certificate_list_end = p + certificate_list_len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100496 while (p < certificate_list_end) {
Xiaofei Bai947571e2021-09-29 09:12:03 +0000497 size_t cert_data_len, extensions_len;
Jerry Yu2eaa7602022-08-04 17:28:15 +0800498 const unsigned char *extensions_end;
Xiaofei Bai947571e2021-09-29 09:12:03 +0000499
Gilles Peskine449bd832023-01-11 14:50:10 +0100500 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, certificate_list_end, 3);
501 cert_data_len = MBEDTLS_GET_UINT24_BE(p, 0);
Xiaofei Bai947571e2021-09-29 09:12:03 +0000502 p += 3;
503
504 /* In theory, the CRT can be up to 2^24 Bytes, but we don't support
505 * anything beyond 2^16 = 64K. Otherwise as in the TLS 1.2 code,
506 * check that we have a minimum of 128 bytes of data, this is not
507 * clear why we need that though.
508 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100509 if ((cert_data_len < 128) || (cert_data_len >= 0x10000)) {
510 MBEDTLS_SSL_DEBUG_MSG(1, ("bad Certificate message"));
511 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR,
512 MBEDTLS_ERR_SSL_DECODE_ERROR);
513 return MBEDTLS_ERR_SSL_DECODE_ERROR;
Xiaofei Bai947571e2021-09-29 09:12:03 +0000514 }
515
Gilles Peskine449bd832023-01-11 14:50:10 +0100516 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, certificate_list_end, cert_data_len);
517 ret = mbedtls_x509_crt_parse_der(ssl->session_negotiate->peer_cert,
518 p, cert_data_len);
Xiaofei Bai947571e2021-09-29 09:12:03 +0000519
Gilles Peskine449bd832023-01-11 14:50:10 +0100520 switch (ret) {
Xiaofei Bai947571e2021-09-29 09:12:03 +0000521 case 0: /*ok*/
522 break;
523 case MBEDTLS_ERR_X509_UNKNOWN_SIG_ALG + MBEDTLS_ERR_OID_NOT_FOUND:
524 /* Ignore certificate with an unknown algorithm: maybe a
525 prior certificate was already trusted. */
526 break;
527
528 case MBEDTLS_ERR_X509_ALLOC_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100529 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR,
530 MBEDTLS_ERR_X509_ALLOC_FAILED);
531 MBEDTLS_SSL_DEBUG_RET(1, " mbedtls_x509_crt_parse_der", ret);
532 return ret;
Xiaofei Bai947571e2021-09-29 09:12:03 +0000533
534 case MBEDTLS_ERR_X509_UNKNOWN_VERSION:
Gilles Peskine449bd832023-01-11 14:50:10 +0100535 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT,
536 MBEDTLS_ERR_X509_UNKNOWN_VERSION);
537 MBEDTLS_SSL_DEBUG_RET(1, " mbedtls_x509_crt_parse_der", ret);
538 return ret;
Xiaofei Bai947571e2021-09-29 09:12:03 +0000539
540 default:
Gilles Peskine449bd832023-01-11 14:50:10 +0100541 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_BAD_CERT,
542 ret);
543 MBEDTLS_SSL_DEBUG_RET(1, " mbedtls_x509_crt_parse_der", ret);
544 return ret;
Xiaofei Bai947571e2021-09-29 09:12:03 +0000545 }
546
547 p += cert_data_len;
548
549 /* Certificate extensions length */
Gilles Peskine449bd832023-01-11 14:50:10 +0100550 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, certificate_list_end, 2);
551 extensions_len = MBEDTLS_GET_UINT16_BE(p, 0);
Xiaofei Bai947571e2021-09-29 09:12:03 +0000552 p += 2;
Gilles Peskine449bd832023-01-11 14:50:10 +0100553 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, certificate_list_end, extensions_len);
Jerry Yu2eaa7602022-08-04 17:28:15 +0800554
555 extensions_end = p + extensions_len;
Jerry Yu0d5cfb72022-10-31 14:15:48 +0800556 handshake->received_extensions = MBEDTLS_SSL_EXT_MASK_NONE;
Jerry Yu2eaa7602022-08-04 17:28:15 +0800557
Gilles Peskine449bd832023-01-11 14:50:10 +0100558 while (p < extensions_end) {
Jerry Yu2eaa7602022-08-04 17:28:15 +0800559 unsigned int extension_type;
560 size_t extension_data_len;
561
562 /*
Gilles Peskine449bd832023-01-11 14:50:10 +0100563 * struct {
564 * ExtensionType extension_type; (2 bytes)
565 * opaque extension_data<0..2^16-1>;
566 * } Extension;
567 */
568 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, extensions_end, 4);
569 extension_type = MBEDTLS_GET_UINT16_BE(p, 0);
570 extension_data_len = MBEDTLS_GET_UINT16_BE(p, 2);
Jerry Yu2eaa7602022-08-04 17:28:15 +0800571 p += 4;
572
Gilles Peskine449bd832023-01-11 14:50:10 +0100573 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, extensions_end, extension_data_len);
Jerry Yu2eaa7602022-08-04 17:28:15 +0800574
Jerry Yuc4bf5d62022-10-29 09:08:47 +0800575 ret = mbedtls_ssl_tls13_check_received_extension(
Gilles Peskine449bd832023-01-11 14:50:10 +0100576 ssl, MBEDTLS_SSL_HS_CERTIFICATE, extension_type,
577 MBEDTLS_SSL_TLS1_3_ALLOWED_EXTS_OF_CT);
578 if (ret != 0) {
579 return ret;
580 }
Jerry Yu0c354a22022-08-29 15:25:36 +0800581
Gilles Peskine449bd832023-01-11 14:50:10 +0100582 switch (extension_type) {
Jerry Yu2eaa7602022-08-04 17:28:15 +0800583 default:
Jerry Yu79aa7212022-11-08 21:30:21 +0800584 MBEDTLS_SSL_PRINT_EXT(
Jerry Yu0d5cfb72022-10-31 14:15:48 +0800585 3, MBEDTLS_SSL_HS_CERTIFICATE,
Gilles Peskine449bd832023-01-11 14:50:10 +0100586 extension_type, "( ignored )");
Jerry Yu2eaa7602022-08-04 17:28:15 +0800587 break;
588 }
589
590 p += extension_data_len;
591 }
592
Gilles Peskine449bd832023-01-11 14:50:10 +0100593 MBEDTLS_SSL_PRINT_EXTS(3, MBEDTLS_SSL_HS_CERTIFICATE,
594 handshake->received_extensions);
Xiaofei Bai947571e2021-09-29 09:12:03 +0000595 }
596
XiaokangQian63e713e2022-05-15 04:26:57 +0000597exit:
Xiaofei Bai947571e2021-09-29 09:12:03 +0000598 /* Check that all the message is consumed. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100599 if (p != end) {
600 MBEDTLS_SSL_DEBUG_MSG(1, ("bad Certificate message"));
601 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR,
602 MBEDTLS_ERR_SSL_DECODE_ERROR);
603 return MBEDTLS_ERR_SSL_DECODE_ERROR;
Xiaofei Bai947571e2021-09-29 09:12:03 +0000604 }
605
Xiaokang Qian73437382023-03-29 08:24:12 +0000606 MBEDTLS_SSL_DEBUG_CRT(3, "peer certificate",
607 ssl->session_negotiate->peer_cert);
Xiaofei Bai947571e2021-09-29 09:12:03 +0000608
Gilles Peskine449bd832023-01-11 14:50:10 +0100609 return ret;
Xiaofei Bai947571e2021-09-29 09:12:03 +0000610}
611#else
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200612MBEDTLS_CHECK_RETURN_CRITICAL
Ronald Crone3dac4a2022-06-10 17:21:51 +0200613MBEDTLS_STATIC_TESTABLE
Gilles Peskine449bd832023-01-11 14:50:10 +0100614int mbedtls_ssl_tls13_parse_certificate(mbedtls_ssl_context *ssl,
615 const unsigned char *buf,
616 const unsigned char *end)
Xiaofei Bai947571e2021-09-29 09:12:03 +0000617{
618 ((void) ssl);
619 ((void) buf);
620 ((void) end);
Gilles Peskine449bd832023-01-11 14:50:10 +0100621 return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Xiaofei Bai947571e2021-09-29 09:12:03 +0000622}
623#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Ronald Cronde08cf32022-10-04 17:15:35 +0200624#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED */
Xiaofei Bai947571e2021-09-29 09:12:03 +0000625
Ronald Cronde08cf32022-10-04 17:15:35 +0200626#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED)
Xiaofei Bai947571e2021-09-29 09:12:03 +0000627#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Xiaofei Bai79595ac2021-10-26 07:16:45 +0000628/* Validate certificate chain sent by the server. */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200629MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100630static int ssl_tls13_validate_certificate(mbedtls_ssl_context *ssl)
Xiaofei Bai947571e2021-09-29 09:12:03 +0000631{
632 int ret = 0;
XiaokangQian989f06d2022-05-17 01:50:15 +0000633 int authmode = MBEDTLS_SSL_VERIFY_REQUIRED;
Xiaofei Bai947571e2021-09-29 09:12:03 +0000634 mbedtls_x509_crt *ca_chain;
635 mbedtls_x509_crl *ca_crl;
Ronald Cron30c5a252022-06-16 19:31:06 +0200636 const char *ext_oid;
637 size_t ext_len;
Xiaofei Baiff456022021-10-28 06:50:17 +0000638 uint32_t verify_result = 0;
Xiaofei Bai947571e2021-09-29 09:12:03 +0000639
XiaokangQian6b916b12022-04-25 07:29:34 +0000640 /* If SNI was used, overwrite authentication mode
641 * from the configuration. */
XiaokangQian989f06d2022-05-17 01:50:15 +0000642#if defined(MBEDTLS_SSL_SRV_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100643 if (ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER) {
XiaokangQian0557c942022-05-30 08:10:53 +0000644#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Gilles Peskine449bd832023-01-11 14:50:10 +0100645 if (ssl->handshake->sni_authmode != MBEDTLS_SSL_VERIFY_UNSET) {
XiaokangQian0557c942022-05-30 08:10:53 +0000646 authmode = ssl->handshake->sni_authmode;
Gilles Peskine449bd832023-01-11 14:50:10 +0100647 } else
XiaokangQian0557c942022-05-30 08:10:53 +0000648#endif
Gilles Peskine449bd832023-01-11 14:50:10 +0100649 authmode = ssl->conf->authmode;
XiaokangQian0557c942022-05-30 08:10:53 +0000650 }
XiaokangQian6b916b12022-04-25 07:29:34 +0000651#endif
652
653 /*
XiaokangQian989f06d2022-05-17 01:50:15 +0000654 * If the peer hasn't sent a certificate ( i.e. it sent
XiaokangQian6b916b12022-04-25 07:29:34 +0000655 * an empty certificate chain ), this is reflected in the peer CRT
656 * structure being unset.
657 * Check for that and handle it depending on the
XiaokangQian989f06d2022-05-17 01:50:15 +0000658 * authentication mode.
XiaokangQian6b916b12022-04-25 07:29:34 +0000659 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100660 if (ssl->session_negotiate->peer_cert == NULL) {
661 MBEDTLS_SSL_DEBUG_MSG(1, ("peer has no certificate"));
XiaokangQian989f06d2022-05-17 01:50:15 +0000662
XiaokangQian63e713e2022-05-15 04:26:57 +0000663#if defined(MBEDTLS_SSL_SRV_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100664 if (ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER) {
XiaokangQian63e713e2022-05-15 04:26:57 +0000665 /* The client was asked for a certificate but didn't send
666 * one. The client should know what's going on, so we
667 * don't send an alert.
668 */
669 ssl->session_negotiate->verify_result = MBEDTLS_X509_BADCERT_MISSING;
Gilles Peskine449bd832023-01-11 14:50:10 +0100670 if (authmode == MBEDTLS_SSL_VERIFY_OPTIONAL) {
671 return 0;
672 } else {
Xiaokang Qian73437382023-03-29 08:24:12 +0000673 MBEDTLS_SSL_PEND_FATAL_ALERT(
674 MBEDTLS_SSL_ALERT_MSG_NO_CERT,
675 MBEDTLS_ERR_SSL_NO_CLIENT_CERTIFICATE);
Gilles Peskine449bd832023-01-11 14:50:10 +0100676 return MBEDTLS_ERR_SSL_NO_CLIENT_CERTIFICATE;
XiaokangQian989f06d2022-05-17 01:50:15 +0000677 }
XiaokangQian63e713e2022-05-15 04:26:57 +0000678 }
XiaokangQian6b916b12022-04-25 07:29:34 +0000679#endif /* MBEDTLS_SSL_SRV_C */
680
XiaokangQianc3017f62022-05-13 05:55:41 +0000681#if defined(MBEDTLS_SSL_CLI_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100682 if (ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT) {
683 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_NO_CERT,
684 MBEDTLS_ERR_SSL_FATAL_ALERT_MESSAGE);
685 return MBEDTLS_ERR_SSL_FATAL_ALERT_MESSAGE;
XiaokangQian63e713e2022-05-15 04:26:57 +0000686 }
XiaokangQianc3017f62022-05-13 05:55:41 +0000687#endif /* MBEDTLS_SSL_CLI_C */
XiaokangQian63e713e2022-05-15 04:26:57 +0000688 }
XiaokangQian6b916b12022-04-25 07:29:34 +0000689
Xiaofei Bai947571e2021-09-29 09:12:03 +0000690#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Gilles Peskine449bd832023-01-11 14:50:10 +0100691 if (ssl->handshake->sni_ca_chain != NULL) {
Xiaofei Bai947571e2021-09-29 09:12:03 +0000692 ca_chain = ssl->handshake->sni_ca_chain;
693 ca_crl = ssl->handshake->sni_ca_crl;
Gilles Peskine449bd832023-01-11 14:50:10 +0100694 } else
Xiaofei Bai947571e2021-09-29 09:12:03 +0000695#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
696 {
697 ca_chain = ssl->conf->ca_chain;
698 ca_crl = ssl->conf->ca_crl;
699 }
700
701 /*
702 * Main check: verify certificate
703 */
704 ret = mbedtls_x509_crt_verify_with_profile(
705 ssl->session_negotiate->peer_cert,
706 ca_chain, ca_crl,
707 ssl->conf->cert_profile,
708 ssl->hostname,
Xiaofei Baiff456022021-10-28 06:50:17 +0000709 &verify_result,
Gilles Peskine449bd832023-01-11 14:50:10 +0100710 ssl->conf->f_vrfy, ssl->conf->p_vrfy);
Xiaofei Bai947571e2021-09-29 09:12:03 +0000711
Gilles Peskine449bd832023-01-11 14:50:10 +0100712 if (ret != 0) {
713 MBEDTLS_SSL_DEBUG_RET(1, "x509_verify_cert", ret);
Xiaofei Bai947571e2021-09-29 09:12:03 +0000714 }
715
716 /*
717 * Secondary checks: always done, but change 'ret' only if it was 0
718 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100719 if (ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT) {
Ronald Cron30c5a252022-06-16 19:31:06 +0200720 ext_oid = MBEDTLS_OID_SERVER_AUTH;
Gilles Peskine449bd832023-01-11 14:50:10 +0100721 ext_len = MBEDTLS_OID_SIZE(MBEDTLS_OID_SERVER_AUTH);
722 } else {
Ronald Cron30c5a252022-06-16 19:31:06 +0200723 ext_oid = MBEDTLS_OID_CLIENT_AUTH;
Gilles Peskine449bd832023-01-11 14:50:10 +0100724 ext_len = MBEDTLS_OID_SIZE(MBEDTLS_OID_CLIENT_AUTH);
Ronald Cron30c5a252022-06-16 19:31:06 +0200725 }
726
Gilles Peskine449bd832023-01-11 14:50:10 +0100727 if ((mbedtls_x509_crt_check_key_usage(
728 ssl->session_negotiate->peer_cert,
729 MBEDTLS_X509_KU_DIGITAL_SIGNATURE) != 0) ||
730 (mbedtls_x509_crt_check_extended_key_usage(
731 ssl->session_negotiate->peer_cert,
732 ext_oid, ext_len) != 0)) {
733 MBEDTLS_SSL_DEBUG_MSG(1, ("bad certificate (usage extensions)"));
734 if (ret == 0) {
Xiaofei Bai947571e2021-09-29 09:12:03 +0000735 ret = MBEDTLS_ERR_SSL_BAD_CERTIFICATE;
Gilles Peskine449bd832023-01-11 14:50:10 +0100736 }
Xiaofei Bai947571e2021-09-29 09:12:03 +0000737 }
738
XiaokangQian6b916b12022-04-25 07:29:34 +0000739 /* mbedtls_x509_crt_verify_with_profile is supposed to report a
740 * verification failure through MBEDTLS_ERR_X509_CERT_VERIFY_FAILED,
741 * with details encoded in the verification flags. All other kinds
742 * of error codes, including those from the user provided f_vrfy
743 * functions, are treated as fatal and lead to a failure of
Ronald Crone3dac4a2022-06-10 17:21:51 +0200744 * mbedtls_ssl_tls13_parse_certificate even if verification was optional.
745 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100746 if (authmode == MBEDTLS_SSL_VERIFY_OPTIONAL &&
747 (ret == MBEDTLS_ERR_X509_CERT_VERIFY_FAILED ||
748 ret == MBEDTLS_ERR_SSL_BAD_CERTIFICATE)) {
XiaokangQian6b916b12022-04-25 07:29:34 +0000749 ret = 0;
750 }
Xiaofei Bai947571e2021-09-29 09:12:03 +0000751
Gilles Peskine449bd832023-01-11 14:50:10 +0100752 if (ca_chain == NULL && authmode == MBEDTLS_SSL_VERIFY_REQUIRED) {
753 MBEDTLS_SSL_DEBUG_MSG(1, ("got no CA chain"));
Xiaofei Bai947571e2021-09-29 09:12:03 +0000754 ret = MBEDTLS_ERR_SSL_CA_CHAIN_REQUIRED;
755 }
756
Gilles Peskine449bd832023-01-11 14:50:10 +0100757 if (ret != 0) {
Xiaofei Bai947571e2021-09-29 09:12:03 +0000758 /* The certificate may have been rejected for several reasons.
759 Pick one and send the corresponding alert. Which alert to send
760 may be a subject of debate in some cases. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100761 if (verify_result & MBEDTLS_X509_BADCERT_OTHER) {
Xiaokang Qian73437382023-03-29 08:24:12 +0000762 MBEDTLS_SSL_PEND_FATAL_ALERT(
763 MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED, ret);
Gilles Peskine449bd832023-01-11 14:50:10 +0100764 } else if (verify_result & MBEDTLS_X509_BADCERT_CN_MISMATCH) {
765 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_BAD_CERT, ret);
766 } else if (verify_result & (MBEDTLS_X509_BADCERT_KEY_USAGE |
767 MBEDTLS_X509_BADCERT_EXT_KEY_USAGE |
768 MBEDTLS_X509_BADCERT_NS_CERT_TYPE |
769 MBEDTLS_X509_BADCERT_BAD_PK |
770 MBEDTLS_X509_BADCERT_BAD_KEY)) {
Xiaokang Qian73437382023-03-29 08:24:12 +0000771 MBEDTLS_SSL_PEND_FATAL_ALERT(
772 MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT, ret);
Gilles Peskine449bd832023-01-11 14:50:10 +0100773 } else if (verify_result & MBEDTLS_X509_BADCERT_EXPIRED) {
Xiaokang Qian73437382023-03-29 08:24:12 +0000774 MBEDTLS_SSL_PEND_FATAL_ALERT(
775 MBEDTLS_SSL_ALERT_MSG_CERT_EXPIRED, ret);
Gilles Peskine449bd832023-01-11 14:50:10 +0100776 } else if (verify_result & MBEDTLS_X509_BADCERT_REVOKED) {
Xiaokang Qian73437382023-03-29 08:24:12 +0000777 MBEDTLS_SSL_PEND_FATAL_ALERT(
778 MBEDTLS_SSL_ALERT_MSG_CERT_REVOKED, ret);
Gilles Peskine449bd832023-01-11 14:50:10 +0100779 } else if (verify_result & MBEDTLS_X509_BADCERT_NOT_TRUSTED) {
780 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_UNKNOWN_CA, ret);
781 } else {
Xiaokang Qian73437382023-03-29 08:24:12 +0000782 MBEDTLS_SSL_PEND_FATAL_ALERT(
783 MBEDTLS_SSL_ALERT_MSG_CERT_UNKNOWN, ret);
Gilles Peskine449bd832023-01-11 14:50:10 +0100784 }
Xiaofei Bai947571e2021-09-29 09:12:03 +0000785 }
786
787#if defined(MBEDTLS_DEBUG_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100788 if (verify_result != 0) {
789 MBEDTLS_SSL_DEBUG_MSG(3, ("! Certificate verification flags %08x",
790 (unsigned int) verify_result));
791 } else {
792 MBEDTLS_SSL_DEBUG_MSG(3, ("Certificate verification flags clear"));
Xiaofei Bai947571e2021-09-29 09:12:03 +0000793 }
794#endif /* MBEDTLS_DEBUG_C */
795
Xiaofei Baiff456022021-10-28 06:50:17 +0000796 ssl->session_negotiate->verify_result = verify_result;
Gilles Peskine449bd832023-01-11 14:50:10 +0100797 return ret;
Xiaofei Bai947571e2021-09-29 09:12:03 +0000798}
799#else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200800MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100801static int ssl_tls13_validate_certificate(mbedtls_ssl_context *ssl)
Xiaofei Bai947571e2021-09-29 09:12:03 +0000802{
803 ((void) ssl);
Gilles Peskine449bd832023-01-11 14:50:10 +0100804 return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Xiaofei Bai947571e2021-09-29 09:12:03 +0000805}
806#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Ronald Cronde08cf32022-10-04 17:15:35 +0200807#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED */
Xiaofei Bai947571e2021-09-29 09:12:03 +0000808
Gilles Peskine449bd832023-01-11 14:50:10 +0100809int mbedtls_ssl_tls13_process_certificate(mbedtls_ssl_context *ssl)
Xiaofei Bai947571e2021-09-29 09:12:03 +0000810{
Xiaofei Bai79595ac2021-10-26 07:16:45 +0000811 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine449bd832023-01-11 14:50:10 +0100812 MBEDTLS_SSL_DEBUG_MSG(2, ("=> parse certificate"));
Xiaofei Bai79595ac2021-10-26 07:16:45 +0000813
Ronald Cronde08cf32022-10-04 17:15:35 +0200814#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED)
XiaokangQianc3017f62022-05-13 05:55:41 +0000815 unsigned char *buf;
816 size_t buf_len;
Xiaofei Bai79595ac2021-10-26 07:16:45 +0000817
Gilles Peskine449bd832023-01-11 14:50:10 +0100818 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_tls13_fetch_handshake_msg(
819 ssl, MBEDTLS_SSL_HS_CERTIFICATE,
820 &buf, &buf_len));
Xiaofei Bai79595ac2021-10-26 07:16:45 +0000821
XiaokangQianc3017f62022-05-13 05:55:41 +0000822 /* Parse the certificate chain sent by the peer. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100823 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_tls13_parse_certificate(ssl, buf,
824 buf + buf_len));
XiaokangQianc3017f62022-05-13 05:55:41 +0000825 /* Validate the certificate chain and set the verification results. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100826 MBEDTLS_SSL_PROC_CHK(ssl_tls13_validate_certificate(ssl));
Xiaofei Bai79595ac2021-10-26 07:16:45 +0000827
Xiaokang Qian73437382023-03-29 08:24:12 +0000828 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_add_hs_msg_to_checksum(
829 ssl, MBEDTLS_SSL_HS_CERTIFICATE, buf, buf_len));
Xiaofei Bai79595ac2021-10-26 07:16:45 +0000830
Xiaofei Bai79595ac2021-10-26 07:16:45 +0000831cleanup:
Ronald Cronde08cf32022-10-04 17:15:35 +0200832#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED */
Xiaofei Bai79595ac2021-10-26 07:16:45 +0000833
Gilles Peskine449bd832023-01-11 14:50:10 +0100834 MBEDTLS_SSL_DEBUG_MSG(2, ("<= parse certificate"));
835 return ret;
Xiaofei Bai947571e2021-09-29 09:12:03 +0000836}
Ronald Cron928cbd32022-10-04 16:14:26 +0200837#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED)
Jerry Yu7399d0d2022-01-30 17:54:19 +0800838/*
839 * enum {
840 * X509(0),
841 * RawPublicKey(2),
842 * (255)
843 * } CertificateType;
844 *
845 * struct {
846 * select (certificate_type) {
847 * case RawPublicKey:
848 * // From RFC 7250 ASN.1_subjectPublicKeyInfo
849 * opaque ASN1_subjectPublicKeyInfo<1..2^24-1>;
850 *
851 * case X509:
852 * opaque cert_data<1..2^24-1>;
853 * };
854 * Extension extensions<0..2^16-1>;
855 * } CertificateEntry;
856 *
857 * struct {
858 * opaque certificate_request_context<0..2^8-1>;
859 * CertificateEntry certificate_list<0..2^24-1>;
860 * } Certificate;
861 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200862MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100863static int ssl_tls13_write_certificate_body(mbedtls_ssl_context *ssl,
864 unsigned char *buf,
865 unsigned char *end,
866 size_t *out_len)
Jerry Yu5cc35062022-01-28 16:16:08 +0800867{
Gilles Peskine449bd832023-01-11 14:50:10 +0100868 const mbedtls_x509_crt *crt = mbedtls_ssl_own_cert(ssl);
Jerry Yu3e536442022-02-15 11:05:59 +0800869 unsigned char *p = buf;
Jerry Yuc8d8d4e2022-02-18 12:10:03 +0800870 unsigned char *certificate_request_context =
Gilles Peskine449bd832023-01-11 14:50:10 +0100871 ssl->handshake->certificate_request_context;
Jerry Yuc8d8d4e2022-02-18 12:10:03 +0800872 unsigned char certificate_request_context_len =
Gilles Peskine449bd832023-01-11 14:50:10 +0100873 ssl->handshake->certificate_request_context_len;
Jerry Yuc8d8d4e2022-02-18 12:10:03 +0800874 unsigned char *p_certificate_list_len;
Jerry Yu5cc35062022-01-28 16:16:08 +0800875
Jerry Yu5cc35062022-01-28 16:16:08 +0800876
Jerry Yu3391ac02022-02-16 11:21:37 +0800877 /* ...
878 * opaque certificate_request_context<0..2^8-1>;
879 * ...
880 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100881 MBEDTLS_SSL_CHK_BUF_PTR(p, end, certificate_request_context_len + 1);
Jerry Yuc8d8d4e2022-02-18 12:10:03 +0800882 *p++ = certificate_request_context_len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100883 if (certificate_request_context_len > 0) {
884 memcpy(p, certificate_request_context, certificate_request_context_len);
Jerry Yuc8d8d4e2022-02-18 12:10:03 +0800885 p += certificate_request_context_len;
Jerry Yu537530d2022-02-15 14:00:57 +0800886 }
887
Jerry Yu3391ac02022-02-16 11:21:37 +0800888 /* ...
889 * CertificateEntry certificate_list<0..2^24-1>;
890 * ...
891 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100892 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 3);
Jerry Yuc8d8d4e2022-02-18 12:10:03 +0800893 p_certificate_list_len = p;
Jerry Yu3e536442022-02-15 11:05:59 +0800894 p += 3;
895
Gilles Peskine449bd832023-01-11 14:50:10 +0100896 MBEDTLS_SSL_DEBUG_CRT(3, "own certificate", crt);
Jerry Yu5cc35062022-01-28 16:16:08 +0800897
Gilles Peskine449bd832023-01-11 14:50:10 +0100898 while (crt != NULL) {
Jerry Yu7399d0d2022-01-30 17:54:19 +0800899 size_t cert_data_len = crt->raw.len;
Jerry Yu5cc35062022-01-28 16:16:08 +0800900
Gilles Peskine449bd832023-01-11 14:50:10 +0100901 MBEDTLS_SSL_CHK_BUF_PTR(p, end, cert_data_len + 3 + 2);
902 MBEDTLS_PUT_UINT24_BE(cert_data_len, p, 0);
Jerry Yu7399d0d2022-01-30 17:54:19 +0800903 p += 3;
Jerry Yu5cc35062022-01-28 16:16:08 +0800904
Gilles Peskine449bd832023-01-11 14:50:10 +0100905 memcpy(p, crt->raw.p, cert_data_len);
Jerry Yu7399d0d2022-01-30 17:54:19 +0800906 p += cert_data_len;
907 crt = crt->next;
Jerry Yu5cc35062022-01-28 16:16:08 +0800908
909 /* Currently, we don't have any certificate extensions defined.
910 * Hence, we are sending an empty extension with length zero.
911 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100912 MBEDTLS_PUT_UINT16_BE(0, p, 0);
Jerry Yu7399d0d2022-01-30 17:54:19 +0800913 p += 2;
Jerry Yu5cc35062022-01-28 16:16:08 +0800914 }
Jerry Yu5cc35062022-01-28 16:16:08 +0800915
Gilles Peskine449bd832023-01-11 14:50:10 +0100916 MBEDTLS_PUT_UINT24_BE(p - p_certificate_list_len - 3,
917 p_certificate_list_len, 0);
Jerry Yu7399d0d2022-01-30 17:54:19 +0800918
Jerry Yu3e536442022-02-15 11:05:59 +0800919 *out_len = p - buf;
Jerry Yu5cc35062022-01-28 16:16:08 +0800920
Jerry Yu7de2ff02022-11-08 21:43:46 +0800921 MBEDTLS_SSL_PRINT_EXTS(
Gilles Peskine449bd832023-01-11 14:50:10 +0100922 3, MBEDTLS_SSL_HS_CERTIFICATE, ssl->handshake->sent_extensions);
Jerry Yu4b8f2f72022-10-31 13:31:22 +0800923
Gilles Peskine449bd832023-01-11 14:50:10 +0100924 return 0;
Jerry Yu5cc35062022-01-28 16:16:08 +0800925}
Jerry Yu5cc35062022-01-28 16:16:08 +0800926
Gilles Peskine449bd832023-01-11 14:50:10 +0100927int mbedtls_ssl_tls13_write_certificate(mbedtls_ssl_context *ssl)
Jerry Yu5cc35062022-01-28 16:16:08 +0800928{
929 int ret;
Ronald Cron5bb8fc82022-03-09 07:00:13 +0100930 unsigned char *buf;
931 size_t buf_len, msg_len;
932
Gilles Peskine449bd832023-01-11 14:50:10 +0100933 MBEDTLS_SSL_DEBUG_MSG(2, ("=> write certificate"));
Jerry Yu5cc35062022-01-28 16:16:08 +0800934
Xiaokang Qian73437382023-03-29 08:24:12 +0000935 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_start_handshake_msg(
936 ssl, MBEDTLS_SSL_HS_CERTIFICATE, &buf, &buf_len));
Jerry Yu5cc35062022-01-28 16:16:08 +0800937
Gilles Peskine449bd832023-01-11 14:50:10 +0100938 MBEDTLS_SSL_PROC_CHK(ssl_tls13_write_certificate_body(ssl,
939 buf,
940 buf + buf_len,
941 &msg_len));
Jerry Yu5cc35062022-01-28 16:16:08 +0800942
Xiaokang Qian73437382023-03-29 08:24:12 +0000943 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_add_hs_msg_to_checksum(
944 ssl, MBEDTLS_SSL_HS_CERTIFICATE, buf, msg_len));
Jerry Yu5cc35062022-01-28 16:16:08 +0800945
Gilles Peskine449bd832023-01-11 14:50:10 +0100946 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_finish_handshake_msg(
947 ssl, buf_len, msg_len));
Jerry Yu5cc35062022-01-28 16:16:08 +0800948cleanup:
949
Gilles Peskine449bd832023-01-11 14:50:10 +0100950 MBEDTLS_SSL_DEBUG_MSG(2, ("<= write certificate"));
951 return ret;
Jerry Yu5cc35062022-01-28 16:16:08 +0800952}
953
Jerry Yu3e536442022-02-15 11:05:59 +0800954/*
955 * STATE HANDLING: Output Certificate Verify
956 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100957int mbedtls_ssl_tls13_check_sig_alg_cert_key_match(uint16_t sig_alg,
958 mbedtls_pk_context *key)
Jerry Yu0c6be8f2022-06-20 20:42:00 +0800959{
Gilles Peskine449bd832023-01-11 14:50:10 +0100960 mbedtls_pk_type_t pk_type = mbedtls_ssl_sig_from_pk(key);
961 size_t key_size = mbedtls_pk_get_bitlen(key);
Jerry Yu0c6be8f2022-06-20 20:42:00 +0800962
Gilles Peskine449bd832023-01-11 14:50:10 +0100963 switch (pk_type) {
Jerry Yu67eced02022-02-25 13:37:36 +0800964 case MBEDTLS_SSL_SIG_ECDSA:
Gilles Peskine449bd832023-01-11 14:50:10 +0100965 switch (key_size) {
Jerry Yu67eced02022-02-25 13:37:36 +0800966 case 256:
Gilles Peskine449bd832023-01-11 14:50:10 +0100967 return
968 sig_alg == MBEDTLS_TLS1_3_SIG_ECDSA_SECP256R1_SHA256;
Jerry Yu0c6be8f2022-06-20 20:42:00 +0800969
Jerry Yu67eced02022-02-25 13:37:36 +0800970 case 384:
Gilles Peskine449bd832023-01-11 14:50:10 +0100971 return
972 sig_alg == MBEDTLS_TLS1_3_SIG_ECDSA_SECP384R1_SHA384;
Jerry Yu0c6be8f2022-06-20 20:42:00 +0800973
Jerry Yu67eced02022-02-25 13:37:36 +0800974 case 521:
Gilles Peskine449bd832023-01-11 14:50:10 +0100975 return
976 sig_alg == MBEDTLS_TLS1_3_SIG_ECDSA_SECP521R1_SHA512;
Jerry Yu67eced02022-02-25 13:37:36 +0800977 default:
Jerry Yu67eced02022-02-25 13:37:36 +0800978 break;
979 }
980 break;
Jerry Yu67eced02022-02-25 13:37:36 +0800981
Jerry Yu67eced02022-02-25 13:37:36 +0800982 case MBEDTLS_SSL_SIG_RSA:
Gilles Peskine449bd832023-01-11 14:50:10 +0100983 switch (sig_alg) {
Ronald Cron38391bf2022-09-16 11:19:27 +0200984 case MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA256: /* Intentional fallthrough */
985 case MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA384: /* Intentional fallthrough */
Jerry Yu0c6be8f2022-06-20 20:42:00 +0800986 case MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA512:
Gilles Peskine449bd832023-01-11 14:50:10 +0100987 return 1;
Jerry Yuc2e04932022-06-27 22:13:03 +0800988
Jerry Yu0c6be8f2022-06-20 20:42:00 +0800989 default:
990 break;
Jerry Yucef3f332022-03-22 23:00:13 +0800991 }
Jerry Yu67eced02022-02-25 13:37:36 +0800992 break;
Jerry Yu0c6be8f2022-06-20 20:42:00 +0800993
Jerry Yu67eced02022-02-25 13:37:36 +0800994 default:
Jerry Yu67eced02022-02-25 13:37:36 +0800995 break;
996 }
Jerry Yu0c6be8f2022-06-20 20:42:00 +0800997
Gilles Peskine449bd832023-01-11 14:50:10 +0100998 return 0;
Jerry Yu0c6be8f2022-06-20 20:42:00 +0800999}
1000
Ronald Cronce7d76e2022-07-08 18:56:49 +02001001MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01001002static int ssl_tls13_write_certificate_verify_body(mbedtls_ssl_context *ssl,
1003 unsigned char *buf,
1004 unsigned char *end,
1005 size_t *out_len)
Jerry Yu8511f122022-01-29 10:01:04 +08001006{
Ronald Cron067a1e72022-09-16 13:44:49 +02001007 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yu3e536442022-02-15 11:05:59 +08001008 unsigned char *p = buf;
Jerry Yu8511f122022-01-29 10:01:04 +08001009 mbedtls_pk_context *own_key;
Jerry Yu3e536442022-02-15 11:05:59 +08001010
Gilles Peskine449bd832023-01-11 14:50:10 +01001011 unsigned char handshake_hash[MBEDTLS_TLS1_3_MD_MAX_SIZE];
Jerry Yu8511f122022-01-29 10:01:04 +08001012 size_t handshake_hash_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01001013 unsigned char verify_buffer[SSL_VERIFY_STRUCT_MAX_SIZE];
Jerry Yu3e536442022-02-15 11:05:59 +08001014 size_t verify_buffer_len;
Ronald Cron067a1e72022-09-16 13:44:49 +02001015
1016 uint16_t *sig_alg = ssl->handshake->received_sig_algs;
Jerry Yu3e536442022-02-15 11:05:59 +08001017 size_t signature_len = 0;
Jerry Yu8511f122022-01-29 10:01:04 +08001018
Jerry Yu0b7b1012022-02-23 12:23:05 +08001019 *out_len = 0;
1020
Gilles Peskine449bd832023-01-11 14:50:10 +01001021 own_key = mbedtls_ssl_own_key(ssl);
1022 if (own_key == NULL) {
1023 MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
1024 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Jerry Yu8511f122022-01-29 10:01:04 +08001025 }
1026
Xiaokang Qian73437382023-03-29 08:24:12 +00001027 ret = mbedtls_ssl_get_handshake_transcript(
1028 ssl, ssl->handshake->ciphersuite_info->mac,
1029 handshake_hash, sizeof(handshake_hash), &handshake_hash_len);
Gilles Peskine449bd832023-01-11 14:50:10 +01001030 if (ret != 0) {
1031 return ret;
1032 }
Jerry Yu8511f122022-01-29 10:01:04 +08001033
Gilles Peskine449bd832023-01-11 14:50:10 +01001034 MBEDTLS_SSL_DEBUG_BUF(3, "handshake hash",
1035 handshake_hash,
1036 handshake_hash_len);
Jerry Yu8511f122022-01-29 10:01:04 +08001037
Gilles Peskine449bd832023-01-11 14:50:10 +01001038 ssl_tls13_create_verify_structure(handshake_hash, handshake_hash_len,
1039 verify_buffer, &verify_buffer_len,
1040 ssl->conf->endpoint);
Jerry Yu8511f122022-01-29 10:01:04 +08001041
1042 /*
1043 * struct {
1044 * SignatureScheme algorithm;
1045 * opaque signature<0..2^16-1>;
1046 * } CertificateVerify;
1047 */
Ronald Cron067a1e72022-09-16 13:44:49 +02001048 /* Check there is space for the algorithm identifier (2 bytes) and the
1049 * signature length (2 bytes).
1050 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001051 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 4);
Ronald Cron067a1e72022-09-16 13:44:49 +02001052
Gilles Peskine449bd832023-01-11 14:50:10 +01001053 for (; *sig_alg != MBEDTLS_TLS1_3_SIG_NONE; sig_alg++) {
Ronald Cron067a1e72022-09-16 13:44:49 +02001054 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
1055 mbedtls_pk_type_t pk_type = MBEDTLS_PK_NONE;
1056 mbedtls_md_type_t md_alg = MBEDTLS_MD_NONE;
1057 psa_algorithm_t psa_algorithm = PSA_ALG_NONE;
1058 unsigned char verify_hash[PSA_HASH_MAX_SIZE];
1059 size_t verify_hash_len;
Jerry Yu67eced02022-02-25 13:37:36 +08001060
Gilles Peskine449bd832023-01-11 14:50:10 +01001061 if (!mbedtls_ssl_sig_alg_is_offered(ssl, *sig_alg)) {
Ronald Cron067a1e72022-09-16 13:44:49 +02001062 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +01001063 }
Jerry Yu67eced02022-02-25 13:37:36 +08001064
Gilles Peskine449bd832023-01-11 14:50:10 +01001065 if (!mbedtls_ssl_tls13_sig_alg_for_cert_verify_is_supported(*sig_alg)) {
Ronald Cron067a1e72022-09-16 13:44:49 +02001066 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +01001067 }
Ronald Cron067a1e72022-09-16 13:44:49 +02001068
Gilles Peskine449bd832023-01-11 14:50:10 +01001069 if (!mbedtls_ssl_tls13_check_sig_alg_cert_key_match(*sig_alg, own_key)) {
Ronald Cron067a1e72022-09-16 13:44:49 +02001070 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +01001071 }
Ronald Cron067a1e72022-09-16 13:44:49 +02001072
Gilles Peskine449bd832023-01-11 14:50:10 +01001073 if (mbedtls_ssl_get_pk_type_and_md_alg_from_sig_alg(
1074 *sig_alg, &pk_type, &md_alg) != 0) {
1075 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Ronald Cron067a1e72022-09-16 13:44:49 +02001076 }
1077
1078 /* Hash verify buffer with indicated hash function */
Gilles Peskine449bd832023-01-11 14:50:10 +01001079 psa_algorithm = mbedtls_hash_info_psa_from_md(md_alg);
1080 status = psa_hash_compute(psa_algorithm,
1081 verify_buffer,
1082 verify_buffer_len,
1083 verify_hash, sizeof(verify_hash),
1084 &verify_hash_len);
1085 if (status != PSA_SUCCESS) {
Andrzej Kurek8a045ce2022-12-23 11:00:06 -05001086 return PSA_TO_MBEDTLS_ERR(status);
Ronald Cron067a1e72022-09-16 13:44:49 +02001087 }
1088
Gilles Peskine449bd832023-01-11 14:50:10 +01001089 MBEDTLS_SSL_DEBUG_BUF(3, "verify hash", verify_hash, verify_hash_len);
1090
1091 if ((ret = mbedtls_pk_sign_ext(pk_type, own_key,
1092 md_alg, verify_hash, verify_hash_len,
1093 p + 4, (size_t) (end - (p + 4)), &signature_len,
1094 ssl->conf->f_rng, ssl->conf->p_rng)) != 0) {
1095 MBEDTLS_SSL_DEBUG_MSG(2, ("CertificateVerify signature failed with %s",
1096 mbedtls_ssl_sig_alg_to_str(*sig_alg)));
1097 MBEDTLS_SSL_DEBUG_RET(2, "mbedtls_pk_sign_ext", ret);
1098
1099 /* The signature failed. This is possible if the private key
1100 * was not suitable for the signature operation as purposely we
1101 * did not check its suitability completely. Let's try with
1102 * another signature algorithm.
1103 */
1104 continue;
1105 }
1106
1107 MBEDTLS_SSL_DEBUG_MSG(2, ("CertificateVerify signature with %s",
1108 mbedtls_ssl_sig_alg_to_str(*sig_alg)));
Ronald Cron067a1e72022-09-16 13:44:49 +02001109
1110 break;
1111 }
1112
Gilles Peskine449bd832023-01-11 14:50:10 +01001113 if (*sig_alg == MBEDTLS_TLS1_3_SIG_NONE) {
1114 MBEDTLS_SSL_DEBUG_MSG(1, ("no suitable signature algorithm"));
1115 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE,
1116 MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE);
1117 return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
Jerry Yu8511f122022-01-29 10:01:04 +08001118 }
1119
Gilles Peskine449bd832023-01-11 14:50:10 +01001120 MBEDTLS_PUT_UINT16_BE(*sig_alg, p, 0);
1121 MBEDTLS_PUT_UINT16_BE(signature_len, p, 2);
Jerry Yuf3b46b52022-06-19 16:52:27 +08001122
Ronald Cron067a1e72022-09-16 13:44:49 +02001123 *out_len = 4 + signature_len;
Jerry Yu8c338862022-03-23 13:34:04 +08001124
Gilles Peskine449bd832023-01-11 14:50:10 +01001125 return 0;
Jerry Yu8511f122022-01-29 10:01:04 +08001126}
Jerry Yu8511f122022-01-29 10:01:04 +08001127
Gilles Peskine449bd832023-01-11 14:50:10 +01001128int mbedtls_ssl_tls13_write_certificate_verify(mbedtls_ssl_context *ssl)
Jerry Yu8511f122022-01-29 10:01:04 +08001129{
1130 int ret = 0;
Jerry Yuca133a32022-02-15 14:22:05 +08001131 unsigned char *buf;
1132 size_t buf_len, msg_len;
1133
Gilles Peskine449bd832023-01-11 14:50:10 +01001134 MBEDTLS_SSL_DEBUG_MSG(2, ("=> write certificate verify"));
Jerry Yu8511f122022-01-29 10:01:04 +08001135
Xiaokang Qian73437382023-03-29 08:24:12 +00001136 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_start_handshake_msg(
1137 ssl, MBEDTLS_SSL_HS_CERTIFICATE_VERIFY,
1138 &buf, &buf_len));
Jerry Yu8511f122022-01-29 10:01:04 +08001139
Gilles Peskine449bd832023-01-11 14:50:10 +01001140 MBEDTLS_SSL_PROC_CHK(ssl_tls13_write_certificate_verify_body(
1141 ssl, buf, buf + buf_len, &msg_len));
Jerry Yu8511f122022-01-29 10:01:04 +08001142
Xiaokang Qian73437382023-03-29 08:24:12 +00001143 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_add_hs_msg_to_checksum(
1144 ssl, MBEDTLS_SSL_HS_CERTIFICATE_VERIFY,
1145 buf, msg_len));
Jerry Yu8511f122022-01-29 10:01:04 +08001146
Gilles Peskine449bd832023-01-11 14:50:10 +01001147 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_finish_handshake_msg(
1148 ssl, buf_len, msg_len));
Jerry Yu8511f122022-01-29 10:01:04 +08001149
1150cleanup:
1151
Gilles Peskine449bd832023-01-11 14:50:10 +01001152 MBEDTLS_SSL_DEBUG_MSG(2, ("<= write certificate verify"));
1153 return ret;
Jerry Yu8511f122022-01-29 10:01:04 +08001154}
1155
Ronald Cron928cbd32022-10-04 16:14:26 +02001156#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED */
Jerry Yu90f152d2022-01-29 22:12:42 +08001157
Jerry Yu5cc35062022-01-28 16:16:08 +08001158/*
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001159 *
XiaokangQianc5c39d52021-11-09 11:55:10 +00001160 * STATE HANDLING: Incoming Finished message.
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001161 */
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001162/*
1163 * Implementation
1164 */
1165
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001166MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01001167static int ssl_tls13_preprocess_finished_message(mbedtls_ssl_context *ssl)
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001168{
1169 int ret;
1170
Xiaokang Qian73437382023-03-29 08:24:12 +00001171 ret = mbedtls_ssl_tls13_calculate_verify_data(
1172 ssl,
1173 ssl->handshake->state_local.finished_in.digest,
1174 sizeof(ssl->handshake->state_local.finished_in.digest),
1175 &ssl->handshake->state_local.finished_in.digest_len,
1176 ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT ?
1177 MBEDTLS_SSL_IS_SERVER : MBEDTLS_SSL_IS_CLIENT);
Gilles Peskine449bd832023-01-11 14:50:10 +01001178 if (ret != 0) {
1179 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_tls13_calculate_verify_data", ret);
1180 return ret;
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001181 }
1182
Gilles Peskine449bd832023-01-11 14:50:10 +01001183 return 0;
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001184}
1185
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001186MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01001187static int ssl_tls13_parse_finished_message(mbedtls_ssl_context *ssl,
1188 const unsigned char *buf,
1189 const unsigned char *end)
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001190{
XiaokangQian33062842021-11-11 03:37:45 +00001191 /*
1192 * struct {
XiaokangQianc13f9352021-11-11 06:13:22 +00001193 * opaque verify_data[Hash.length];
XiaokangQian33062842021-11-11 03:37:45 +00001194 * } Finished;
1195 */
1196 const unsigned char *expected_verify_data =
1197 ssl->handshake->state_local.finished_in.digest;
1198 size_t expected_verify_data_len =
1199 ssl->handshake->state_local.finished_in.digest_len;
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001200 /* Structural validation */
Gilles Peskine449bd832023-01-11 14:50:10 +01001201 if ((size_t) (end - buf) != expected_verify_data_len) {
1202 MBEDTLS_SSL_DEBUG_MSG(1, ("bad finished message"));
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001203
Gilles Peskine449bd832023-01-11 14:50:10 +01001204 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR,
1205 MBEDTLS_ERR_SSL_DECODE_ERROR);
1206 return MBEDTLS_ERR_SSL_DECODE_ERROR;
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001207 }
1208
Gilles Peskine449bd832023-01-11 14:50:10 +01001209 MBEDTLS_SSL_DEBUG_BUF(4, "verify_data (self-computed):",
1210 expected_verify_data,
1211 expected_verify_data_len);
1212 MBEDTLS_SSL_DEBUG_BUF(4, "verify_data (received message):", buf,
1213 expected_verify_data_len);
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001214
1215 /* Semantic validation */
Gilles Peskine449bd832023-01-11 14:50:10 +01001216 if (mbedtls_ct_memcmp(buf,
1217 expected_verify_data,
1218 expected_verify_data_len) != 0) {
1219 MBEDTLS_SSL_DEBUG_MSG(1, ("bad finished message"));
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001220
Gilles Peskine449bd832023-01-11 14:50:10 +01001221 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_DECRYPT_ERROR,
1222 MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE);
1223 return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001224 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001225 return 0;
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001226}
1227
Gilles Peskine449bd832023-01-11 14:50:10 +01001228int mbedtls_ssl_tls13_process_finished_message(mbedtls_ssl_context *ssl)
XiaokangQianc5c39d52021-11-09 11:55:10 +00001229{
XiaokangQian33062842021-11-11 03:37:45 +00001230 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
XiaokangQianc5c39d52021-11-09 11:55:10 +00001231 unsigned char *buf;
Xiaofei Baieef15042021-11-18 07:29:56 +00001232 size_t buf_len;
XiaokangQianc5c39d52021-11-09 11:55:10 +00001233
Gilles Peskine449bd832023-01-11 14:50:10 +01001234 MBEDTLS_SSL_DEBUG_MSG(2, ("=> parse finished message"));
XiaokangQianc5c39d52021-11-09 11:55:10 +00001235
Xiaokang Qian73437382023-03-29 08:24:12 +00001236 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_tls13_fetch_handshake_msg(
1237 ssl, MBEDTLS_SSL_HS_FINISHED, &buf, &buf_len));
Jerry Yu0a92d6c2022-05-16 16:54:46 +08001238
1239 /* Preprocessing step: Compute handshake digest */
Gilles Peskine449bd832023-01-11 14:50:10 +01001240 MBEDTLS_SSL_PROC_CHK(ssl_tls13_preprocess_finished_message(ssl));
Jerry Yu0a92d6c2022-05-16 16:54:46 +08001241
Xiaokang Qian73437382023-03-29 08:24:12 +00001242 MBEDTLS_SSL_PROC_CHK(ssl_tls13_parse_finished_message(
1243 ssl, buf, buf + buf_len));
Jerry Yu0a92d6c2022-05-16 16:54:46 +08001244
Xiaokang Qian73437382023-03-29 08:24:12 +00001245 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_add_hs_msg_to_checksum(
1246 ssl, MBEDTLS_SSL_HS_FINISHED, buf, buf_len));
XiaokangQianc5c39d52021-11-09 11:55:10 +00001247
1248cleanup:
1249
Gilles Peskine449bd832023-01-11 14:50:10 +01001250 MBEDTLS_SSL_DEBUG_MSG(2, ("<= parse finished message"));
1251 return ret;
XiaokangQianc5c39d52021-11-09 11:55:10 +00001252}
1253
XiaokangQian74af2a82021-09-22 07:40:30 +00001254/*
1255 *
XiaokangQiancc90c942021-11-09 12:30:09 +00001256 * STATE HANDLING: Write and send Finished message.
XiaokangQian74af2a82021-09-22 07:40:30 +00001257 *
1258 */
XiaokangQian74af2a82021-09-22 07:40:30 +00001259/*
XiaokangQian35dc6252021-11-11 08:16:19 +00001260 * Implement
XiaokangQian74af2a82021-09-22 07:40:30 +00001261 */
1262
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001263MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01001264static int ssl_tls13_prepare_finished_message(mbedtls_ssl_context *ssl)
XiaokangQian74af2a82021-09-22 07:40:30 +00001265{
1266 int ret;
1267
1268 /* Compute transcript of handshake up to now. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001269 ret = mbedtls_ssl_tls13_calculate_verify_data(ssl,
1270 ssl->handshake->state_local.finished_out.digest,
1271 sizeof(ssl->handshake->state_local.finished_out.
1272 digest),
1273 &ssl->handshake->state_local.finished_out.digest_len,
1274 ssl->conf->endpoint);
XiaokangQian74af2a82021-09-22 07:40:30 +00001275
Gilles Peskine449bd832023-01-11 14:50:10 +01001276 if (ret != 0) {
1277 MBEDTLS_SSL_DEBUG_RET(1, "calculate_verify_data failed", ret);
1278 return ret;
XiaokangQian74af2a82021-09-22 07:40:30 +00001279 }
1280
Gilles Peskine449bd832023-01-11 14:50:10 +01001281 return 0;
XiaokangQian74af2a82021-09-22 07:40:30 +00001282}
1283
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001284MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01001285static int ssl_tls13_write_finished_message_body(mbedtls_ssl_context *ssl,
1286 unsigned char *buf,
1287 unsigned char *end,
1288 size_t *out_len)
XiaokangQian74af2a82021-09-22 07:40:30 +00001289{
XiaokangQian8773aa02021-11-10 07:33:09 +00001290 size_t verify_data_len = ssl->handshake->state_local.finished_out.digest_len;
XiaokangQian0fa66432021-11-15 03:33:57 +00001291 /*
1292 * struct {
1293 * opaque verify_data[Hash.length];
1294 * } Finished;
1295 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001296 MBEDTLS_SSL_CHK_BUF_PTR(buf, end, verify_data_len);
XiaokangQian74af2a82021-09-22 07:40:30 +00001297
Gilles Peskine449bd832023-01-11 14:50:10 +01001298 memcpy(buf, ssl->handshake->state_local.finished_out.digest,
1299 verify_data_len);
XiaokangQian74af2a82021-09-22 07:40:30 +00001300
Xiaofei Baid25fab62021-12-02 06:36:27 +00001301 *out_len = verify_data_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01001302 return 0;
XiaokangQian74af2a82021-09-22 07:40:30 +00001303}
XiaokangQianc5c39d52021-11-09 11:55:10 +00001304
XiaokangQian35dc6252021-11-11 08:16:19 +00001305/* Main entry point: orchestrates the other functions */
Gilles Peskine449bd832023-01-11 14:50:10 +01001306int mbedtls_ssl_tls13_write_finished_message(mbedtls_ssl_context *ssl)
XiaokangQian35dc6252021-11-11 08:16:19 +00001307{
1308 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1309 unsigned char *buf;
1310 size_t buf_len, msg_len;
1311
Gilles Peskine449bd832023-01-11 14:50:10 +01001312 MBEDTLS_SSL_DEBUG_MSG(2, ("=> write finished message"));
XiaokangQian35dc6252021-11-11 08:16:19 +00001313
Gilles Peskine449bd832023-01-11 14:50:10 +01001314 MBEDTLS_SSL_PROC_CHK(ssl_tls13_prepare_finished_message(ssl));
XiaokangQiandce82242021-11-15 06:01:26 +00001315
Gilles Peskine449bd832023-01-11 14:50:10 +01001316 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_start_handshake_msg(ssl,
1317 MBEDTLS_SSL_HS_FINISHED, &buf, &buf_len));
XiaokangQian35dc6252021-11-11 08:16:19 +00001318
Gilles Peskine449bd832023-01-11 14:50:10 +01001319 MBEDTLS_SSL_PROC_CHK(ssl_tls13_write_finished_message_body(
1320 ssl, buf, buf + buf_len, &msg_len));
XiaokangQian35dc6252021-11-11 08:16:19 +00001321
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +01001322 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_add_hs_msg_to_checksum(ssl,
Manuel Pégourié-Gonnard43cc1272023-02-06 11:48:19 +01001323 MBEDTLS_SSL_HS_FINISHED, buf, msg_len));
XiaokangQian35dc6252021-11-11 08:16:19 +00001324
Gilles Peskine449bd832023-01-11 14:50:10 +01001325 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_finish_handshake_msg(
1326 ssl, buf_len, msg_len));
XiaokangQian35dc6252021-11-11 08:16:19 +00001327cleanup:
1328
Gilles Peskine449bd832023-01-11 14:50:10 +01001329 MBEDTLS_SSL_DEBUG_MSG(2, ("<= write finished message"));
1330 return ret;
XiaokangQian35dc6252021-11-11 08:16:19 +00001331}
1332
Gilles Peskine449bd832023-01-11 14:50:10 +01001333void mbedtls_ssl_tls13_handshake_wrapup(mbedtls_ssl_context *ssl)
Jerry Yu378254d2021-10-30 21:44:47 +08001334{
1335
Gilles Peskine449bd832023-01-11 14:50:10 +01001336 MBEDTLS_SSL_DEBUG_MSG(3, ("=> handshake wrapup"));
Jerry Yu378254d2021-10-30 21:44:47 +08001337
Gilles Peskine449bd832023-01-11 14:50:10 +01001338 MBEDTLS_SSL_DEBUG_MSG(1, ("Switch to application keys for inbound traffic"));
1339 mbedtls_ssl_set_inbound_transform(ssl, ssl->transform_application);
Jerry Yue8c1fca2022-05-18 14:48:56 +08001340
Gilles Peskine449bd832023-01-11 14:50:10 +01001341 MBEDTLS_SSL_DEBUG_MSG(1, ("Switch to application keys for outbound traffic"));
1342 mbedtls_ssl_set_outbound_transform(ssl, ssl->transform_application);
Jerry Yue8c1fca2022-05-18 14:48:56 +08001343
Jerry Yu378254d2021-10-30 21:44:47 +08001344 /*
Jerry Yucfe64f02021-11-15 13:54:06 +08001345 * Free the previous session and switch to the current one.
Jerry Yu378254d2021-10-30 21:44:47 +08001346 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001347 if (ssl->session) {
1348 mbedtls_ssl_session_free(ssl->session);
1349 mbedtls_free(ssl->session);
Jerry Yu378254d2021-10-30 21:44:47 +08001350 }
1351 ssl->session = ssl->session_negotiate;
1352 ssl->session_negotiate = NULL;
1353
Gilles Peskine449bd832023-01-11 14:50:10 +01001354 MBEDTLS_SSL_DEBUG_MSG(3, ("<= handshake wrapup"));
Jerry Yu378254d2021-10-30 21:44:47 +08001355}
1356
Ronald Cron49ad6192021-11-24 16:25:31 +01001357/*
1358 *
1359 * STATE HANDLING: Write ChangeCipherSpec
1360 *
1361 */
1362#if defined(MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001363MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01001364static int ssl_tls13_write_change_cipher_spec_body(mbedtls_ssl_context *ssl,
1365 unsigned char *buf,
1366 unsigned char *end,
1367 size_t *olen)
Ronald Cron49ad6192021-11-24 16:25:31 +01001368{
1369 ((void) ssl);
1370
Gilles Peskine449bd832023-01-11 14:50:10 +01001371 MBEDTLS_SSL_CHK_BUF_PTR(buf, end, 1);
Ronald Cron49ad6192021-11-24 16:25:31 +01001372 buf[0] = 1;
1373 *olen = 1;
1374
Gilles Peskine449bd832023-01-11 14:50:10 +01001375 return 0;
Ronald Cron49ad6192021-11-24 16:25:31 +01001376}
1377
Gilles Peskine449bd832023-01-11 14:50:10 +01001378int mbedtls_ssl_tls13_write_change_cipher_spec(mbedtls_ssl_context *ssl)
Ronald Cron49ad6192021-11-24 16:25:31 +01001379{
1380 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1381
Gilles Peskine449bd832023-01-11 14:50:10 +01001382 MBEDTLS_SSL_DEBUG_MSG(2, ("=> write change cipher spec"));
Ronald Cron49ad6192021-11-24 16:25:31 +01001383
Ronald Cron49ad6192021-11-24 16:25:31 +01001384 /* Write CCS message */
Gilles Peskine449bd832023-01-11 14:50:10 +01001385 MBEDTLS_SSL_PROC_CHK(ssl_tls13_write_change_cipher_spec_body(
1386 ssl, ssl->out_msg,
1387 ssl->out_msg + MBEDTLS_SSL_OUT_CONTENT_LEN,
1388 &ssl->out_msglen));
Ronald Cron49ad6192021-11-24 16:25:31 +01001389
1390 ssl->out_msgtype = MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC;
1391
Ronald Cron49ad6192021-11-24 16:25:31 +01001392 /* Dispatch message */
Gilles Peskine449bd832023-01-11 14:50:10 +01001393 MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_write_record(ssl, 0));
Ronald Cron49ad6192021-11-24 16:25:31 +01001394
1395cleanup:
1396
Gilles Peskine449bd832023-01-11 14:50:10 +01001397 MBEDTLS_SSL_DEBUG_MSG(2, ("<= write change cipher spec"));
1398 return ret;
Ronald Cron49ad6192021-11-24 16:25:31 +01001399}
1400
1401#endif /* MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE */
1402
Xiaokang Qianecc29482022-11-02 07:52:47 +00001403/* Early Data Indication Extension
Xiaokang Qian0e97d4d2022-10-24 11:12:51 +00001404 *
Xiaokang Qian0e97d4d2022-10-24 11:12:51 +00001405 * struct {
1406 * select ( Handshake.msg_type ) {
Xiaokang Qianecc29482022-11-02 07:52:47 +00001407 * ...
Xiaokang Qian0e97d4d2022-10-24 11:12:51 +00001408 * case client_hello: Empty;
1409 * case encrypted_extensions: Empty;
1410 * };
1411 * } EarlyDataIndication;
1412 */
1413#if defined(MBEDTLS_SSL_EARLY_DATA)
Gilles Peskine449bd832023-01-11 14:50:10 +01001414int mbedtls_ssl_tls13_write_early_data_ext(mbedtls_ssl_context *ssl,
1415 unsigned char *buf,
1416 const unsigned char *end,
1417 size_t *out_len)
Xiaokang Qian0e97d4d2022-10-24 11:12:51 +00001418{
1419 unsigned char *p = buf;
1420 *out_len = 0;
1421 ((void) ssl);
1422
Gilles Peskine449bd832023-01-11 14:50:10 +01001423 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 4);
Xiaokang Qian0e97d4d2022-10-24 11:12:51 +00001424
Gilles Peskine449bd832023-01-11 14:50:10 +01001425 MBEDTLS_PUT_UINT16_BE(MBEDTLS_TLS_EXT_EARLY_DATA, p, 0);
1426 MBEDTLS_PUT_UINT16_BE(0, p, 2);
Xiaokang Qian0e97d4d2022-10-24 11:12:51 +00001427
1428 *out_len = 4;
Xiaokang Qian2cd5ce02022-11-15 10:33:53 +00001429
Gilles Peskine449bd832023-01-11 14:50:10 +01001430 mbedtls_ssl_tls13_set_hs_sent_ext_mask(ssl, MBEDTLS_TLS_EXT_EARLY_DATA);
Xiaokang Qian2cd5ce02022-11-15 10:33:53 +00001431
Gilles Peskine449bd832023-01-11 14:50:10 +01001432 return 0;
Xiaokang Qian0e97d4d2022-10-24 11:12:51 +00001433}
1434#endif /* MBEDTLS_SSL_EARLY_DATA */
1435
XiaokangQian78b1fa72022-01-19 06:56:30 +00001436/* Reset SSL context and update hash for handling HRR.
1437 *
1438 * Replace Transcript-Hash(X) by
1439 * Transcript-Hash( message_hash ||
1440 * 00 00 Hash.length ||
1441 * X )
1442 * A few states of the handshake are preserved, including:
1443 * - session ID
1444 * - session ticket
1445 * - negotiated ciphersuite
1446 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001447int mbedtls_ssl_reset_transcript_for_hrr(mbedtls_ssl_context *ssl)
XiaokangQian78b1fa72022-01-19 06:56:30 +00001448{
1449 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Przemyslaw Stekielda645252022-09-14 12:50:51 +02001450 unsigned char hash_transcript[PSA_HASH_MAX_SIZE + 4];
XiaokangQian0ece9982022-01-24 08:56:23 +00001451 size_t hash_len;
Xiaokang Qian6b980012023-02-07 03:17:45 +00001452 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
1453 ssl->handshake->ciphersuite_info;
XiaokangQian78b1fa72022-01-19 06:56:30 +00001454
Gilles Peskine449bd832023-01-11 14:50:10 +01001455 MBEDTLS_SSL_DEBUG_MSG(3, ("Reset SSL session for HRR"));
XiaokangQian78b1fa72022-01-19 06:56:30 +00001456
Gilles Peskine449bd832023-01-11 14:50:10 +01001457 ret = mbedtls_ssl_get_handshake_transcript(ssl, ciphersuite_info->mac,
1458 hash_transcript + 4,
1459 PSA_HASH_MAX_SIZE,
1460 &hash_len);
1461 if (ret != 0) {
Manuel Pégourié-Gonnardda7979b2023-02-21 09:31:10 +01001462 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_get_handshake_transcript", ret);
Gilles Peskine449bd832023-01-11 14:50:10 +01001463 return ret;
XiaokangQian0ece9982022-01-24 08:56:23 +00001464 }
1465
1466 hash_transcript[0] = MBEDTLS_SSL_HS_MESSAGE_HASH;
1467 hash_transcript[1] = 0;
1468 hash_transcript[2] = 0;
1469 hash_transcript[3] = (unsigned char) hash_len;
1470
1471 hash_len += 4;
1472
Manuel Pégourié-Gonnardda7979b2023-02-21 09:31:10 +01001473 MBEDTLS_SSL_DEBUG_BUF(4, "Truncated handshake transcript",
1474 hash_transcript, hash_len);
1475
Manuel Pégourié-Gonnardd7a7a232023-02-05 10:26:49 +01001476 /* Reset running hash and replace it with a hash of the transcript */
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +01001477 ret = mbedtls_ssl_reset_checksum(ssl);
1478 if (ret != 0) {
1479 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_reset_checksum", ret);
1480 return ret;
1481 }
1482 ret = ssl->handshake->update_checksum(ssl, hash_transcript, hash_len);
1483 if (ret != 0) {
1484 MBEDTLS_SSL_DEBUG_RET(1, "update_checksum", ret);
1485 return ret;
1486 }
Przemyslaw Stekiel4b3fff42022-02-14 16:39:52 +01001487
Gilles Peskine449bd832023-01-11 14:50:10 +01001488 return ret;
XiaokangQian78b1fa72022-01-19 06:56:30 +00001489}
1490
Valerio Setti080a22b2023-03-20 15:22:47 +01001491#if defined(PSA_WANT_ALG_ECDH)
XiaokangQian7807f9f2022-02-15 10:04:37 +00001492
Gilles Peskine449bd832023-01-11 14:50:10 +01001493int mbedtls_ssl_tls13_read_public_ecdhe_share(mbedtls_ssl_context *ssl,
1494 const unsigned char *buf,
1495 size_t buf_len)
XiaokangQian7807f9f2022-02-15 10:04:37 +00001496{
Gilles Peskine449bd832023-01-11 14:50:10 +01001497 uint8_t *p = (uint8_t *) buf;
XiaokangQiancfd925f2022-04-14 07:10:37 +00001498 const uint8_t *end = buf + buf_len;
XiaokangQian9b5d04b2022-04-10 10:20:43 +00001499 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001500
XiaokangQian9b5d04b2022-04-10 10:20:43 +00001501 /* Get size of the TLS opaque key_exchange field of the KeyShareEntry struct. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001502 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, 2);
1503 uint16_t peerkey_len = MBEDTLS_GET_UINT16_BE(p, 0);
XiaokangQian9b5d04b2022-04-10 10:20:43 +00001504 p += 2;
XiaokangQian3207a322022-02-23 03:15:27 +00001505
XiaokangQian9b5d04b2022-04-10 10:20:43 +00001506 /* Check if key size is consistent with given buffer length. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001507 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, peerkey_len);
XiaokangQian9b5d04b2022-04-10 10:20:43 +00001508
1509 /* Store peer's ECDH public key. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001510 memcpy(handshake->ecdh_psa_peerkey, p, peerkey_len);
XiaokangQian9b5d04b2022-04-10 10:20:43 +00001511 handshake->ecdh_psa_peerkey_len = peerkey_len;
1512
Gilles Peskine449bd832023-01-11 14:50:10 +01001513 return 0;
XiaokangQian3207a322022-02-23 03:15:27 +00001514}
Jerry Yu89e103c2022-03-30 22:43:29 +08001515
1516int mbedtls_ssl_tls13_generate_and_write_ecdh_key_exchange(
Gilles Peskine449bd832023-01-11 14:50:10 +01001517 mbedtls_ssl_context *ssl,
1518 uint16_t named_group,
1519 unsigned char *buf,
1520 unsigned char *end,
1521 size_t *out_len)
Jerry Yu89e103c2022-03-30 22:43:29 +08001522{
1523 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
1524 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
1525 psa_key_attributes_t key_attributes;
1526 size_t own_pubkey_len;
1527 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
Valerio Setti40d9ca92023-01-04 16:08:04 +01001528 psa_ecc_family_t ec_psa_family = 0;
1529 size_t ec_bits = 0;
Jerry Yu89e103c2022-03-30 22:43:29 +08001530
Gilles Peskine449bd832023-01-11 14:50:10 +01001531 MBEDTLS_SSL_DEBUG_MSG(1, ("Perform PSA-based ECDH computation."));
Jerry Yu89e103c2022-03-30 22:43:29 +08001532
Valerio Setti40d9ca92023-01-04 16:08:04 +01001533 /* Convert EC's TLS ID to PSA key type. */
Xiaokang Qian73437382023-03-29 08:24:12 +00001534 if (mbedtls_ssl_get_psa_curve_info_from_tls_id(
1535 named_group, &ec_psa_family, &ec_bits) == PSA_ERROR_NOT_SUPPORTED) {
Gilles Peskine449bd832023-01-11 14:50:10 +01001536 return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
Valerio Setti40d9ca92023-01-04 16:08:04 +01001537 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001538 handshake->ecdh_psa_type = PSA_KEY_TYPE_ECC_KEY_PAIR(ec_psa_family);
Valerio Setti40d9ca92023-01-04 16:08:04 +01001539 ssl->handshake->ecdh_bits = ec_bits;
Jerry Yu89e103c2022-03-30 22:43:29 +08001540
1541 key_attributes = psa_key_attributes_init();
Gilles Peskine449bd832023-01-11 14:50:10 +01001542 psa_set_key_usage_flags(&key_attributes, PSA_KEY_USAGE_DERIVE);
1543 psa_set_key_algorithm(&key_attributes, PSA_ALG_ECDH);
1544 psa_set_key_type(&key_attributes, handshake->ecdh_psa_type);
1545 psa_set_key_bits(&key_attributes, handshake->ecdh_bits);
Jerry Yu89e103c2022-03-30 22:43:29 +08001546
1547 /* Generate ECDH private key. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001548 status = psa_generate_key(&key_attributes,
1549 &handshake->ecdh_psa_privkey);
1550 if (status != PSA_SUCCESS) {
Andrzej Kurek8a045ce2022-12-23 11:00:06 -05001551 ret = PSA_TO_MBEDTLS_ERR(status);
Gilles Peskine449bd832023-01-11 14:50:10 +01001552 MBEDTLS_SSL_DEBUG_RET(1, "psa_generate_key", ret);
1553 return ret;
Jerry Yu89e103c2022-03-30 22:43:29 +08001554
1555 }
1556
1557 /* Export the public part of the ECDH private key from PSA. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001558 status = psa_export_public_key(handshake->ecdh_psa_privkey,
1559 buf, (size_t) (end - buf),
1560 &own_pubkey_len);
1561 if (status != PSA_SUCCESS) {
Andrzej Kurek8a045ce2022-12-23 11:00:06 -05001562 ret = PSA_TO_MBEDTLS_ERR(status);
Gilles Peskine449bd832023-01-11 14:50:10 +01001563 MBEDTLS_SSL_DEBUG_RET(1, "psa_export_public_key", ret);
1564 return ret;
Jerry Yu89e103c2022-03-30 22:43:29 +08001565
1566 }
1567
1568 *out_len = own_pubkey_len;
1569
Gilles Peskine449bd832023-01-11 14:50:10 +01001570 return 0;
Jerry Yu89e103c2022-03-30 22:43:29 +08001571}
Valerio Setti080a22b2023-03-20 15:22:47 +01001572#endif /* PSA_WANT_ALG_ECDH */
XiaokangQian7807f9f2022-02-15 10:04:37 +00001573
Przemek Stekielc89f3ea2023-05-18 15:45:53 +02001574#if defined(PSA_WANT_ALG_FFDH)
1575int mbedtls_ssl_tls13_generate_and_write_dhe_key_exchange(
1576 mbedtls_ssl_context *ssl,
1577 uint16_t named_group,
1578 unsigned char *buf,
1579 unsigned char *end,
1580 size_t *out_len)
1581{
1582 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
1583 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
1584 psa_key_attributes_t key_attributes;
1585 size_t own_pubkey_len;
1586 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
1587 size_t ffdh_bits = 0;
1588
1589 MBEDTLS_SSL_DEBUG_MSG(1, ("Perform PSA-based DHE computation."));
1590
1591 /* Convert DHE group to PSA key type. */
1592 if ((handshake->ecdh_psa_type =
1593 mbedtls_psa_parse_tls_ffdh_group(named_group, &ffdh_bits)) == 0) {
1594 return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
1595 }
1596
1597 if ((size_t) (end - buf) < PSA_BITS_TO_BYTES(ffdh_bits)) {
1598 ret = MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL;
1599 return ret;
1600 }
1601
1602 ssl->handshake->ecdh_bits = ffdh_bits;
1603
1604 key_attributes = psa_key_attributes_init();
1605 psa_set_key_usage_flags(&key_attributes, PSA_KEY_USAGE_DERIVE);
1606 psa_set_key_algorithm(&key_attributes, PSA_ALG_FFDH);
1607 psa_set_key_type(&key_attributes, handshake->ecdh_psa_type);
1608 psa_set_key_bits(&key_attributes, handshake->ecdh_bits);
1609
1610 /* Generate FFDH private key. */
1611 status = psa_generate_key(&key_attributes,
1612 &handshake->ecdh_psa_privkey);
1613 if (status != PSA_SUCCESS) {
1614 ret = psa_ssl_status_to_mbedtls(status);
1615 MBEDTLS_SSL_DEBUG_RET(1, "psa_generate_key", ret);
1616 return ret;
1617
1618 }
1619
1620 /* Export the public part of the FFDH private key from PSA. */
1621 status = psa_export_public_key(handshake->ecdh_psa_privkey,
1622 buf, PSA_BITS_TO_BYTES(ffdh_bits),
1623 &own_pubkey_len);
1624 if (status != PSA_SUCCESS) {
1625 ret = psa_ssl_status_to_mbedtls(status);
1626 MBEDTLS_SSL_DEBUG_RET(1, "psa_export_public_key", ret);
1627 return ret;
1628 }
1629
1630 *out_len = own_pubkey_len;
1631
1632 return 0;
1633}
1634#endif /* PSA_WANT_ALG_FFDH */
1635
Jerry Yu0c354a22022-08-29 15:25:36 +08001636/* RFC 8446 section 4.2
1637 *
1638 * If an implementation receives an extension which it recognizes and which is
1639 * not specified for the message in which it appears, it MUST abort the handshake
1640 * with an "illegal_parameter" alert.
1641 *
1642 */
Jerry Yuc4bf5d62022-10-29 09:08:47 +08001643int mbedtls_ssl_tls13_check_received_extension(
Gilles Peskine449bd832023-01-11 14:50:10 +01001644 mbedtls_ssl_context *ssl,
1645 int hs_msg_type,
1646 unsigned int received_extension_type,
1647 uint32_t hs_msg_allowed_extensions_mask)
Jerry Yu0c354a22022-08-29 15:25:36 +08001648{
Jerry Yudf0ad652022-10-31 13:20:57 +08001649 uint32_t extension_mask = mbedtls_ssl_get_extension_mask(
Gilles Peskine449bd832023-01-11 14:50:10 +01001650 received_extension_type);
Jerry Yu0c354a22022-08-29 15:25:36 +08001651
Jerry Yu79aa7212022-11-08 21:30:21 +08001652 MBEDTLS_SSL_PRINT_EXT(
Gilles Peskine449bd832023-01-11 14:50:10 +01001653 3, hs_msg_type, received_extension_type, "received");
Jerry Yu0c354a22022-08-29 15:25:36 +08001654
Gilles Peskine449bd832023-01-11 14:50:10 +01001655 if ((extension_mask & hs_msg_allowed_extensions_mask) == 0) {
Jerry Yu79aa7212022-11-08 21:30:21 +08001656 MBEDTLS_SSL_PRINT_EXT(
Gilles Peskine449bd832023-01-11 14:50:10 +01001657 3, hs_msg_type, received_extension_type, "is illegal");
Jerry Yu0c354a22022-08-29 15:25:36 +08001658 MBEDTLS_SSL_PEND_FATAL_ALERT(
1659 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER,
Gilles Peskine449bd832023-01-11 14:50:10 +01001660 MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER);
1661 return MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER;
Jerry Yu0c354a22022-08-29 15:25:36 +08001662 }
1663
1664 ssl->handshake->received_extensions |= extension_mask;
Jerry Yuc4bf5d62022-10-29 09:08:47 +08001665 /*
1666 * If it is a message containing extension responses, check that we
1667 * previously sent the extension.
1668 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001669 switch (hs_msg_type) {
Jerry Yu0c354a22022-08-29 15:25:36 +08001670 case MBEDTLS_SSL_HS_SERVER_HELLO:
Jerry Yudf0ad652022-10-31 13:20:57 +08001671 case MBEDTLS_SSL_TLS1_3_HS_HELLO_RETRY_REQUEST:
Jerry Yu0c354a22022-08-29 15:25:36 +08001672 case MBEDTLS_SSL_HS_ENCRYPTED_EXTENSIONS:
1673 case MBEDTLS_SSL_HS_CERTIFICATE:
Jerry Yuc4bf5d62022-10-29 09:08:47 +08001674 /* Check if the received extension is sent by peer message.*/
Gilles Peskine449bd832023-01-11 14:50:10 +01001675 if ((ssl->handshake->sent_extensions & extension_mask) != 0) {
1676 return 0;
1677 }
Jerry Yu0c354a22022-08-29 15:25:36 +08001678 break;
1679 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01001680 return 0;
Jerry Yu0c354a22022-08-29 15:25:36 +08001681 }
1682
Jerry Yu79aa7212022-11-08 21:30:21 +08001683 MBEDTLS_SSL_PRINT_EXT(
Gilles Peskine449bd832023-01-11 14:50:10 +01001684 3, hs_msg_type, received_extension_type, "is unsupported");
Jerry Yu0c354a22022-08-29 15:25:36 +08001685 MBEDTLS_SSL_PEND_FATAL_ALERT(
1686 MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_EXT,
Gilles Peskine449bd832023-01-11 14:50:10 +01001687 MBEDTLS_ERR_SSL_UNSUPPORTED_EXTENSION);
1688 return MBEDTLS_ERR_SSL_UNSUPPORTED_EXTENSION;
Jerry Yu0c354a22022-08-29 15:25:36 +08001689}
1690
Jan Bruckner151f6422023-02-10 12:45:19 +01001691#if defined(MBEDTLS_SSL_RECORD_SIZE_LIMIT)
Jan Bruckner1a38e542023-03-15 14:15:11 +01001692/* RFC 8449, section 4:
1693 *
Jan Bruckner151f6422023-02-10 12:45:19 +01001694 * The ExtensionData of the "record_size_limit" extension is
1695 * RecordSizeLimit:
1696 * uint16 RecordSizeLimit;
1697 */
1698MBEDTLS_CHECK_RETURN_CRITICAL
1699int mbedtls_ssl_tls13_parse_record_size_limit_ext(mbedtls_ssl_context *ssl,
1700 const unsigned char *buf,
1701 const unsigned char *end)
1702{
Jan Bruckner1a38e542023-03-15 14:15:11 +01001703 const unsigned char *p = buf;
1704 uint16_t record_size_limit;
Jan Brucknera0589e72023-03-15 11:04:45 +01001705 const size_t extension_data_len = end - buf;
Jan Bruckner1a38e542023-03-15 14:15:11 +01001706
Xiaokang Qian73437382023-03-29 08:24:12 +00001707 if (extension_data_len !=
1708 MBEDTLS_SSL_RECORD_SIZE_LIMIT_EXTENSION_DATA_LENGTH) {
Jan Bruckner151f6422023-02-10 12:45:19 +01001709 MBEDTLS_SSL_DEBUG_MSG(2,
Jan Bruckner1a38e542023-03-15 14:15:11 +01001710 ("record_size_limit extension has invalid length: %"
1711 MBEDTLS_PRINTF_SIZET " Bytes",
Jan Bruckner151f6422023-02-10 12:45:19 +01001712 extension_data_len));
1713
1714 MBEDTLS_SSL_PEND_FATAL_ALERT(
1715 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER,
1716 MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER);
1717 return MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER;
1718 }
1719
Jan Bruckner151f6422023-02-10 12:45:19 +01001720 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, 2);
1721 record_size_limit = MBEDTLS_GET_UINT16_BE(p, 0);
1722
1723 MBEDTLS_SSL_DEBUG_MSG(2, ("RecordSizeLimit: %u Bytes", record_size_limit));
1724
Jan Bruckner1a38e542023-03-15 14:15:11 +01001725 /* RFC 8449, section 4
Jan Bruckner151f6422023-02-10 12:45:19 +01001726 *
1727 * Endpoints MUST NOT send a "record_size_limit" extension with a value
1728 * smaller than 64. An endpoint MUST treat receipt of a smaller value
1729 * as a fatal error and generate an "illegal_parameter" alert.
1730 */
Jan Brucknera0589e72023-03-15 11:04:45 +01001731 if (record_size_limit < MBEDTLS_SSL_RECORD_SIZE_LIMIT_MIN) {
Jan Bruckner151f6422023-02-10 12:45:19 +01001732 MBEDTLS_SSL_PEND_FATAL_ALERT(
1733 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER,
1734 MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER);
1735 return MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER;
1736 }
1737
Xiaokang Qian73437382023-03-29 08:24:12 +00001738 MBEDTLS_SSL_DEBUG_MSG(
1739 2, ("record_size_limit extension is still in development. Aborting handshake."));
Jan Bruckner151f6422023-02-10 12:45:19 +01001740
1741 MBEDTLS_SSL_PEND_FATAL_ALERT(
1742 MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_EXT,
1743 MBEDTLS_ERR_SSL_UNSUPPORTED_EXTENSION);
1744 return MBEDTLS_ERR_SSL_UNSUPPORTED_EXTENSION;
1745}
1746#endif /* MBEDTLS_SSL_RECORD_SIZE_LIMIT */
1747
Jerry Yufb4b6472022-01-27 15:03:26 +08001748#endif /* MBEDTLS_SSL_TLS_C && MBEDTLS_SSL_PROTO_TLS1_3 */