blob: 8ec9f90bb0931ff1c8ed72c568c29aadbfcdffae [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"
XiaokangQian74af2a82021-09-22 07:40:30 +000031#include <string.h>
Jerry Yuc8a392c2021-08-18 16:46:28 +080032
Jerry Yu65dd2cc2021-08-18 16:38:40 +080033#include "ssl_misc.h"
Jerry Yu30b071c2021-09-12 20:16:03 +080034#include "ssl_tls13_keys.h"
Jerry Yu67eced02022-02-25 13:37:36 +080035#include "ssl_debug_helpers.h"
Jerry Yu65dd2cc2021-08-18 16:38:40 +080036
pespaceka1378102022-04-26 15:03:11 +020037#include "psa/crypto.h"
38#include "mbedtls/psa_util.h"
39
Jerry Yufbe3e642022-04-25 19:31:51 +080040const uint8_t mbedtls_ssl_tls13_hello_retry_request_magic[
41 MBEDTLS_SERVER_HELLO_RANDOM_LEN ] =
42 { 0xCF, 0x21, 0xAD, 0x74, 0xE5, 0x9A, 0x61, 0x11,
43 0xBE, 0x1D, 0x8C, 0x02, 0x1E, 0x65, 0xB8, 0x91,
44 0xC2, 0xA2, 0x11, 0x16, 0x7A, 0xBB, 0x8C, 0x5E,
45 0x07, 0x9E, 0x09, 0xE2, 0xC8, 0xA8, 0x33, 0x9C };
Jerry Yu93a13f22022-04-11 23:00:01 +080046
Xiaofei Bai746f9482021-11-12 08:53:56 +000047int mbedtls_ssl_tls13_fetch_handshake_msg( mbedtls_ssl_context *ssl,
48 unsigned hs_type,
49 unsigned char **buf,
Xiaofei Baieef15042021-11-18 07:29:56 +000050 size_t *buf_len )
XiaokangQian6b226b02021-09-24 07:51:16 +000051{
52 int ret;
53
54 if( ( ret = mbedtls_ssl_read_record( ssl, 0 ) ) != 0 )
55 {
56 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
57 goto cleanup;
58 }
59
XiaokangQian16c61aa2021-09-27 09:30:17 +000060 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE ||
XiaokangQian6b226b02021-09-24 07:51:16 +000061 ssl->in_msg[0] != hs_type )
62 {
XiaokangQian16c61aa2021-09-27 09:30:17 +000063 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Receive unexpected handshake message." ) );
XiaokangQian6b226b02021-09-24 07:51:16 +000064 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE,
XiaokangQian05420b12021-09-29 08:46:37 +000065 MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
XiaokangQian6b226b02021-09-24 07:51:16 +000066 ret = MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE;
67 goto cleanup;
68 }
69
XiaokangQian05420b12021-09-29 08:46:37 +000070 /*
71 * Jump handshake header (4 bytes, see Section 4 of RFC 8446).
72 * ...
73 * HandshakeType msg_type;
74 * uint24 length;
75 * ...
76 */
Xiaofei Baieef15042021-11-18 07:29:56 +000077 *buf = ssl->in_msg + 4;
78 *buf_len = ssl->in_hslen - 4;
XiaokangQian6b226b02021-09-24 07:51:16 +000079
XiaokangQian6b226b02021-09-24 07:51:16 +000080cleanup:
81
82 return( ret );
83}
84
Jerry Yubc20bdd2021-08-24 15:59:48 +080085#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Jerry Yu30b071c2021-09-12 20:16:03 +080086/*
Jerry Yu30b071c2021-09-12 20:16:03 +080087 * STATE HANDLING: Read CertificateVerify
88 */
Jerry Yud0fc5852021-10-29 11:09:06 +080089/* Macro to express the maximum length of the verify structure.
Jerry Yu30b071c2021-09-12 20:16:03 +080090 *
91 * The structure is computed per TLS 1.3 specification as:
92 * - 64 bytes of octet 32,
93 * - 33 bytes for the context string
94 * (which is either "TLS 1.3, client CertificateVerify"
95 * or "TLS 1.3, server CertificateVerify"),
Jerry Yud0fc5852021-10-29 11:09:06 +080096 * - 1 byte for the octet 0x0, which serves as a separator,
Jerry Yu30b071c2021-09-12 20:16:03 +080097 * - 32 or 48 bytes for the Transcript-Hash(Handshake Context, Certificate)
98 * (depending on the size of the transcript_hash)
99 *
100 * This results in a total size of
101 * - 130 bytes for a SHA256-based transcript hash, or
102 * (64 + 33 + 1 + 32 bytes)
103 * - 146 bytes for a SHA384-based transcript hash.
104 * (64 + 33 + 1 + 48 bytes)
105 *
106 */
Jerry Yu26c2d112021-10-25 12:42:58 +0800107#define SSL_VERIFY_STRUCT_MAX_SIZE ( 64 + \
108 33 + \
109 1 + \
110 MBEDTLS_TLS1_3_MD_MAX_SIZE \
Jerry Yu30b071c2021-09-12 20:16:03 +0800111 )
Jerry Yu30b071c2021-09-12 20:16:03 +0800112
Jerry Yu0b32c502021-10-28 13:41:59 +0800113/*
114 * The ssl_tls13_create_verify_structure() creates the verify structure.
115 * As input, it requires the transcript hash.
116 *
117 * The caller has to ensure that the buffer has size at least
118 * SSL_VERIFY_STRUCT_MAX_SIZE bytes.
119 */
Jerry Yud0fc5852021-10-29 11:09:06 +0800120static void ssl_tls13_create_verify_structure( const unsigned char *transcript_hash,
Jerry Yu0b32c502021-10-28 13:41:59 +0800121 size_t transcript_hash_len,
122 unsigned char *verify_buffer,
123 size_t *verify_buffer_len,
124 int from )
125{
126 size_t idx;
Jerry Yu30b071c2021-09-12 20:16:03 +0800127
Jerry Yu0b32c502021-10-28 13:41:59 +0800128 /* RFC 8446, Section 4.4.3:
129 *
130 * The digital signature [in the CertificateVerify message] is then
131 * computed over the concatenation of:
132 * - A string that consists of octet 32 (0x20) repeated 64 times
133 * - The context string
134 * - A single 0 byte which serves as the separator
135 * - The content to be signed
136 */
137 memset( verify_buffer, 0x20, 64 );
138 idx = 64;
139
140 if( from == MBEDTLS_SSL_IS_CLIENT )
141 {
142 memcpy( verify_buffer + idx, MBEDTLS_SSL_TLS1_3_LBL_WITH_LEN( client_cv ) );
143 idx += MBEDTLS_SSL_TLS1_3_LBL_LEN( client_cv );
144 }
145 else
146 { /* from == MBEDTLS_SSL_IS_SERVER */
147 memcpy( verify_buffer + idx, MBEDTLS_SSL_TLS1_3_LBL_WITH_LEN( server_cv ) );
148 idx += MBEDTLS_SSL_TLS1_3_LBL_LEN( server_cv );
149 }
150
151 verify_buffer[idx++] = 0x0;
152
153 memcpy( verify_buffer + idx, transcript_hash, transcript_hash_len );
154 idx += transcript_hash_len;
155
156 *verify_buffer_len = idx;
157}
158
Jerry Yu0b32c502021-10-28 13:41:59 +0800159static int ssl_tls13_parse_certificate_verify( mbedtls_ssl_context *ssl,
Jerry Yud0fc5852021-10-29 11:09:06 +0800160 const unsigned char *buf,
161 const unsigned char *end,
162 const unsigned char *verify_buffer,
163 size_t verify_buffer_len )
Jerry Yu30b071c2021-09-12 20:16:03 +0800164{
165 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
pespaceka1378102022-04-26 15:03:11 +0200166 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Jerry Yu30b071c2021-09-12 20:16:03 +0800167 const unsigned char *p = buf;
168 uint16_t algorithm;
Jerry Yu30b071c2021-09-12 20:16:03 +0800169 size_t signature_len;
170 mbedtls_pk_type_t sig_alg;
171 mbedtls_md_type_t md_alg;
pespaceka1378102022-04-26 15:03:11 +0200172 psa_algorithm_t hash_alg = PSA_ALG_NONE;
173 unsigned char verify_hash[PSA_HASH_MAX_SIZE];
Jerry Yu30b071c2021-09-12 20:16:03 +0800174 size_t verify_hash_len;
175
Xiaofei Baid25fab62021-12-02 06:36:27 +0000176 void const *options = NULL;
XiaokangQian82d34cc2021-11-03 08:51:56 +0000177#if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT)
Xiaofei Baid25fab62021-12-02 06:36:27 +0000178 mbedtls_pk_rsassa_pss_options rsassa_pss_options;
XiaokangQian82d34cc2021-11-03 08:51:56 +0000179#endif /* MBEDTLS_X509_RSASSA_PSS_SUPPORT */
180
Jerry Yu30b071c2021-09-12 20:16:03 +0800181 /*
182 * struct {
183 * SignatureScheme algorithm;
184 * opaque signature<0..2^16-1>;
185 * } CertificateVerify;
186 */
187 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 2 );
188 algorithm = MBEDTLS_GET_UINT16_BE( p, 0 );
189 p += 2;
190
191 /* RFC 8446 section 4.4.3
192 *
193 * If the CertificateVerify message is sent by a server, the signature algorithm
194 * MUST be one offered in the client's "signature_algorithms" extension unless
195 * no valid certificate chain can be produced without unsupported algorithms
196 *
197 * RFC 8446 section 4.4.2.2
198 *
199 * If the client cannot construct an acceptable chain using the provided
200 * certificates and decides to abort the handshake, then it MUST abort the handshake
201 * with an appropriate certificate-related alert (by default, "unsupported_certificate").
202 *
Jerry Yu6f87f252021-10-29 20:12:51 +0800203 * Check if algorithm is an offered signature algorithm.
Jerry Yu30b071c2021-09-12 20:16:03 +0800204 */
Jerry Yu24811fb2022-01-19 18:02:15 +0800205 if( ! mbedtls_ssl_sig_alg_is_offered( ssl, algorithm ) )
Jerry Yu30b071c2021-09-12 20:16:03 +0800206 {
Jerry Yu982d9e52021-10-14 15:59:37 +0800207 /* algorithm not in offered signature algorithms list */
208 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Received signature algorithm(%04x) is not "
209 "offered.",
210 ( unsigned int ) algorithm ) );
Jerry Yu6f87f252021-10-29 20:12:51 +0800211 goto error;
Jerry Yu30b071c2021-09-12 20:16:03 +0800212 }
213
Jerry Yu8c338862022-03-23 13:34:04 +0800214 if( mbedtls_ssl_tls13_get_pk_type_and_md_alg_from_sig_alg(
Jerry Yuf8aa9a42022-03-23 20:40:28 +0800215 algorithm, &sig_alg, &md_alg ) != 0 )
Jerry Yu30b071c2021-09-12 20:16:03 +0800216 {
Jerry Yu8c338862022-03-23 13:34:04 +0800217 goto error;
Jerry Yu30b071c2021-09-12 20:16:03 +0800218 }
219
pespaceka1378102022-04-26 15:03:11 +0200220 hash_alg = mbedtls_psa_translate_md( md_alg );
221 if( hash_alg == 0 )
222 {
223 goto error;
224 }
225
Jerry Yu30b071c2021-09-12 20:16:03 +0800226 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Certificate Verify: Signature algorithm ( %04x )",
227 ( unsigned int ) algorithm ) );
228
229 /*
230 * Check the certificate's key type matches the signature alg
231 */
232 if( !mbedtls_pk_can_do( &ssl->session_negotiate->peer_cert->pk, sig_alg ) )
233 {
234 MBEDTLS_SSL_DEBUG_MSG( 1, ( "signature algorithm doesn't match cert key" ) );
Jerry Yu6f87f252021-10-29 20:12:51 +0800235 goto error;
Jerry Yu30b071c2021-09-12 20:16:03 +0800236 }
237
238 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 2 );
239 signature_len = MBEDTLS_GET_UINT16_BE( p, 0 );
240 p += 2;
241 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, signature_len );
242
pespaceka1378102022-04-26 15:03:11 +0200243 status = psa_hash_compute( hash_alg,
244 verify_buffer,
245 verify_buffer_len,
246 verify_hash,
247 sizeof( verify_hash ),
248 &verify_hash_len );
249 if( status != PSA_SUCCESS )
Jerry Yu30b071c2021-09-12 20:16:03 +0800250 {
pespaceka1378102022-04-26 15:03:11 +0200251 MBEDTLS_SSL_DEBUG_RET( 1, "hash computation PSA error", status );
Jerry Yu6f87f252021-10-29 20:12:51 +0800252 goto error;
Jerry Yu133690c2021-10-25 14:01:13 +0800253 }
254
Jerry Yu30b071c2021-09-12 20:16:03 +0800255 MBEDTLS_SSL_DEBUG_BUF( 3, "verify hash", verify_hash, verify_hash_len );
XiaokangQian82d34cc2021-11-03 08:51:56 +0000256#if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT)
257 if( sig_alg == MBEDTLS_PK_RSASSA_PSS )
258 {
Xiaofei Baid25fab62021-12-02 06:36:27 +0000259 rsassa_pss_options.mgf1_hash_id = md_alg;
Przemek Stekiel6a5e0182022-06-27 11:53:13 +0200260
Przemek Stekiel4dc87442022-06-28 11:05:42 +0200261 rsassa_pss_options.expected_salt_len = PSA_HASH_LENGTH( hash_alg );
Xiaofei Baid25fab62021-12-02 06:36:27 +0000262 options = (const void*) &rsassa_pss_options;
XiaokangQian82d34cc2021-11-03 08:51:56 +0000263 }
264#endif /* MBEDTLS_X509_RSASSA_PSS_SUPPORT */
Jerry Yu30b071c2021-09-12 20:16:03 +0800265
Xiaofei Baid25fab62021-12-02 06:36:27 +0000266 if( ( ret = mbedtls_pk_verify_ext( sig_alg, options,
Jerry Yu30b071c2021-09-12 20:16:03 +0800267 &ssl->session_negotiate->peer_cert->pk,
268 md_alg, verify_hash, verify_hash_len,
Jerry Yu6f87f252021-10-29 20:12:51 +0800269 p, signature_len ) ) == 0 )
Jerry Yu30b071c2021-09-12 20:16:03 +0800270 {
Jerry Yu6f87f252021-10-29 20:12:51 +0800271 return( 0 );
Jerry Yu30b071c2021-09-12 20:16:03 +0800272 }
Jerry Yu6f87f252021-10-29 20:12:51 +0800273 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_pk_verify_ext", ret );
Jerry Yu30b071c2021-09-12 20:16:03 +0800274
Jerry Yu6f87f252021-10-29 20:12:51 +0800275error:
276 /* RFC 8446 section 4.4.3
277 *
278 * If the verification fails, the receiver MUST terminate the handshake
279 * with a "decrypt_error" alert.
280 */
281 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_DECRYPT_ERROR,
282 MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
283 return( MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
284
Jerry Yu30b071c2021-09-12 20:16:03 +0800285}
286#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
287
288int mbedtls_ssl_tls13_process_certificate_verify( mbedtls_ssl_context *ssl )
289{
Jerry Yu30b071c2021-09-12 20:16:03 +0800290
Jerry Yuda8cdf22021-10-25 15:06:49 +0800291#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
292 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
293 unsigned char verify_buffer[SSL_VERIFY_STRUCT_MAX_SIZE];
294 size_t verify_buffer_len;
295 unsigned char transcript[MBEDTLS_TLS1_3_MD_MAX_SIZE];
296 size_t transcript_len;
297 unsigned char *buf;
298 size_t buf_len;
299
Jerry Yu30b071c2021-09-12 20:16:03 +0800300 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate verify" ) );
301
Jerry Yuda8cdf22021-10-25 15:06:49 +0800302 MBEDTLS_SSL_PROC_CHK(
Xiaofei Bai746f9482021-11-12 08:53:56 +0000303 mbedtls_ssl_tls13_fetch_handshake_msg( ssl,
Jerry Yuda8cdf22021-10-25 15:06:49 +0800304 MBEDTLS_SSL_HS_CERTIFICATE_VERIFY, &buf, &buf_len ) );
Jerry Yu30b071c2021-09-12 20:16:03 +0800305
Jerry Yuda8cdf22021-10-25 15:06:49 +0800306 /* Need to calculate the hash of the transcript first
Jerry Yu0b32c502021-10-28 13:41:59 +0800307 * before reading the message since otherwise it gets
308 * included in the transcript
309 */
Jerry Yuda8cdf22021-10-25 15:06:49 +0800310 ret = mbedtls_ssl_get_handshake_transcript( ssl,
311 ssl->handshake->ciphersuite_info->mac,
312 transcript, sizeof( transcript ),
313 &transcript_len );
314 if( ret != 0 )
Jerry Yu30b071c2021-09-12 20:16:03 +0800315 {
Jerry Yuda8cdf22021-10-25 15:06:49 +0800316 MBEDTLS_SSL_PEND_FATAL_ALERT(
317 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR,
318 MBEDTLS_ERR_SSL_INTERNAL_ERROR );
319 return( ret );
Jerry Yu30b071c2021-09-12 20:16:03 +0800320 }
321
Jerry Yuda8cdf22021-10-25 15:06:49 +0800322 MBEDTLS_SSL_DEBUG_BUF( 3, "handshake hash", transcript, transcript_len );
323
324 /* Create verify structure */
325 ssl_tls13_create_verify_structure( transcript,
Jerry Yu0b32c502021-10-28 13:41:59 +0800326 transcript_len,
327 verify_buffer,
328 &verify_buffer_len,
329 ( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT ) ?
330 MBEDTLS_SSL_IS_SERVER :
331 MBEDTLS_SSL_IS_CLIENT );
Jerry Yuda8cdf22021-10-25 15:06:49 +0800332
333 /* Process the message contents */
Jerry Yu0b32c502021-10-28 13:41:59 +0800334 MBEDTLS_SSL_PROC_CHK( ssl_tls13_parse_certificate_verify( ssl, buf,
335 buf + buf_len, verify_buffer, verify_buffer_len ) );
Jerry Yuda8cdf22021-10-25 15:06:49 +0800336
Ronald Cron8f6d39a2022-03-10 18:56:50 +0100337 mbedtls_ssl_add_hs_msg_to_checksum( ssl, MBEDTLS_SSL_HS_CERTIFICATE_VERIFY,
338 buf, buf_len );
Jerry Yu30b071c2021-09-12 20:16:03 +0800339
340cleanup:
341
342 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse certificate verify" ) );
Jerry Yu5398c102021-11-05 13:32:38 +0800343 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_tls13_process_certificate_verify", ret );
Jerry Yu30b071c2021-09-12 20:16:03 +0800344 return( ret );
Jerry Yuda8cdf22021-10-25 15:06:49 +0800345#else
346 ((void) ssl);
347 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
348 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
349#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
Jerry Yu30b071c2021-09-12 20:16:03 +0800350}
351
352/*
Xiaofei Bai947571e2021-09-29 09:12:03 +0000353 *
XiaokangQian6b916b12022-04-25 07:29:34 +0000354 * STATE HANDLING: Incoming Certificate.
Xiaofei Bai947571e2021-09-29 09:12:03 +0000355 *
356 */
357
Xiaofei Bai947571e2021-09-29 09:12:03 +0000358#if defined(MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED)
359#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
360/*
361 * Structure of Certificate message:
362 *
363 * enum {
364 * X509(0),
365 * RawPublicKey(2),
366 * (255)
367 * } CertificateType;
368 *
369 * struct {
370 * select (certificate_type) {
371 * case RawPublicKey:
372 * * From RFC 7250 ASN.1_subjectPublicKeyInfo *
373 * opaque ASN1_subjectPublicKeyInfo<1..2^24-1>;
374 * case X509:
375 * opaque cert_data<1..2^24-1>;
376 * };
377 * Extension extensions<0..2^16-1>;
378 * } CertificateEntry;
379 *
380 * struct {
381 * opaque certificate_request_context<0..2^8-1>;
382 * CertificateEntry certificate_list<0..2^24-1>;
383 * } Certificate;
384 *
385 */
Xiaofei Bai79595ac2021-10-26 07:16:45 +0000386
387/* Parse certificate chain send by the server. */
Xiaofei Bai947571e2021-09-29 09:12:03 +0000388static int ssl_tls13_parse_certificate( mbedtls_ssl_context *ssl,
389 const unsigned char *buf,
390 const unsigned char *end )
391{
392 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
393 size_t certificate_request_context_len = 0;
394 size_t certificate_list_len = 0;
395 const unsigned char *p = buf;
396 const unsigned char *certificate_list_end;
397
XiaokangQian63e713e2022-05-15 04:26:57 +0000398 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 4 );
Xiaofei Bai947571e2021-09-29 09:12:03 +0000399 certificate_request_context_len = p[0];
XiaokangQian63e713e2022-05-15 04:26:57 +0000400 certificate_list_len = MBEDTLS_GET_UINT24_BE( p, 1 );
401 p += 4;
Xiaofei Bai947571e2021-09-29 09:12:03 +0000402
403 /* In theory, the certificate list can be up to 2^24 Bytes, but we don't
404 * support anything beyond 2^16 = 64K.
405 */
Xiaofei Bai79595ac2021-10-26 07:16:45 +0000406 if( ( certificate_request_context_len != 0 ) ||
407 ( certificate_list_len >= 0x10000 ) )
Xiaofei Bai947571e2021-09-29 09:12:03 +0000408 {
409 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
410 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR,
411 MBEDTLS_ERR_SSL_DECODE_ERROR );
412 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
413 }
414
415 /* In case we tried to reuse a session but it failed */
416 if( ssl->session_negotiate->peer_cert != NULL )
417 {
418 mbedtls_x509_crt_free( ssl->session_negotiate->peer_cert );
419 mbedtls_free( ssl->session_negotiate->peer_cert );
420 }
421
XiaokangQianc3017f62022-05-13 05:55:41 +0000422 if( certificate_list_len == 0 )
423 {
424 ssl->session_negotiate->peer_cert = NULL;
425 ret = 0;
426 goto exit;
427 }
428
Xiaofei Bai947571e2021-09-29 09:12:03 +0000429 if( ( ssl->session_negotiate->peer_cert =
430 mbedtls_calloc( 1, sizeof( mbedtls_x509_crt ) ) ) == NULL )
431 {
432 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc( %" MBEDTLS_PRINTF_SIZET " bytes ) failed",
433 sizeof( mbedtls_x509_crt ) ) );
434 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR,
435 MBEDTLS_ERR_SSL_ALLOC_FAILED );
436 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
437 }
438
439 mbedtls_x509_crt_init( ssl->session_negotiate->peer_cert );
440
Xiaofei Bai947571e2021-09-29 09:12:03 +0000441 certificate_list_end = p + certificate_list_len;
Xiaofei Bai79595ac2021-10-26 07:16:45 +0000442 while( p < certificate_list_end )
Xiaofei Bai947571e2021-09-29 09:12:03 +0000443 {
444 size_t cert_data_len, extensions_len;
445
Xiaofei Bai79595ac2021-10-26 07:16:45 +0000446 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, certificate_list_end, 3 );
Xiaofei Baif93cbd22021-10-29 02:39:30 +0000447 cert_data_len = MBEDTLS_GET_UINT24_BE( p, 0 );
Xiaofei Bai947571e2021-09-29 09:12:03 +0000448 p += 3;
449
450 /* In theory, the CRT can be up to 2^24 Bytes, but we don't support
451 * anything beyond 2^16 = 64K. Otherwise as in the TLS 1.2 code,
452 * check that we have a minimum of 128 bytes of data, this is not
453 * clear why we need that though.
454 */
455 if( ( cert_data_len < 128 ) || ( cert_data_len >= 0x10000 ) )
Xiaofei Bai79595ac2021-10-26 07:16:45 +0000456 {
Xiaofei Bai947571e2021-09-29 09:12:03 +0000457 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad Certificate message" ) );
458 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR,
459 MBEDTLS_ERR_SSL_DECODE_ERROR );
460 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
461 }
462
Xiaofei Bai79595ac2021-10-26 07:16:45 +0000463 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, certificate_list_end, cert_data_len );
Xiaofei Bai947571e2021-09-29 09:12:03 +0000464 ret = mbedtls_x509_crt_parse_der( ssl->session_negotiate->peer_cert,
465 p, cert_data_len );
466
467 switch( ret )
468 {
469 case 0: /*ok*/
470 break;
471 case MBEDTLS_ERR_X509_UNKNOWN_SIG_ALG + MBEDTLS_ERR_OID_NOT_FOUND:
472 /* Ignore certificate with an unknown algorithm: maybe a
473 prior certificate was already trusted. */
474 break;
475
476 case MBEDTLS_ERR_X509_ALLOC_FAILED:
477 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR,
478 MBEDTLS_ERR_X509_ALLOC_FAILED );
479 MBEDTLS_SSL_DEBUG_RET( 1, " mbedtls_x509_crt_parse_der", ret );
480 return( ret );
481
482 case MBEDTLS_ERR_X509_UNKNOWN_VERSION:
483 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT,
484 MBEDTLS_ERR_X509_UNKNOWN_VERSION );
485 MBEDTLS_SSL_DEBUG_RET( 1, " mbedtls_x509_crt_parse_der", ret );
486 return( ret );
487
488 default:
489 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_BAD_CERT,
490 ret );
491 MBEDTLS_SSL_DEBUG_RET( 1, " mbedtls_x509_crt_parse_der", ret );
492 return( ret );
493 }
494
495 p += cert_data_len;
496
497 /* Certificate extensions length */
498 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, certificate_list_end, 2 );
499 extensions_len = MBEDTLS_GET_UINT16_BE( p, 0 );
500 p += 2;
Xiaofei Bai79595ac2021-10-26 07:16:45 +0000501 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, certificate_list_end, extensions_len );
Xiaofei Bai947571e2021-09-29 09:12:03 +0000502 p += extensions_len;
503 }
504
XiaokangQian63e713e2022-05-15 04:26:57 +0000505exit:
Xiaofei Bai947571e2021-09-29 09:12:03 +0000506 /* Check that all the message is consumed. */
507 if( p != end )
508 {
509 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad Certificate message" ) );
510 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR, \
511 MBEDTLS_ERR_SSL_DECODE_ERROR );
512 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
513 }
514
515 MBEDTLS_SSL_DEBUG_CRT( 3, "peer certificate", ssl->session_negotiate->peer_cert );
516
517 return( ret );
518}
519#else
520static int ssl_tls13_parse_certificate( mbedtls_ssl_context *ssl,
521 const unsigned char *buf,
522 const unsigned char *end )
523{
524 ((void) ssl);
525 ((void) buf);
526 ((void) end);
527 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
528}
529#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
530#endif /* MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED */
531
532#if defined(MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED)
533#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Xiaofei Bai79595ac2021-10-26 07:16:45 +0000534/* Validate certificate chain sent by the server. */
Xiaofei Bai947571e2021-09-29 09:12:03 +0000535static int ssl_tls13_validate_certificate( mbedtls_ssl_context *ssl )
536{
537 int ret = 0;
XiaokangQian989f06d2022-05-17 01:50:15 +0000538 int authmode = MBEDTLS_SSL_VERIFY_REQUIRED;
Xiaofei Bai947571e2021-09-29 09:12:03 +0000539 mbedtls_x509_crt *ca_chain;
540 mbedtls_x509_crl *ca_crl;
Xiaofei Baiff456022021-10-28 06:50:17 +0000541 uint32_t verify_result = 0;
Xiaofei Bai947571e2021-09-29 09:12:03 +0000542
XiaokangQian6b916b12022-04-25 07:29:34 +0000543 /* If SNI was used, overwrite authentication mode
544 * from the configuration. */
XiaokangQian989f06d2022-05-17 01:50:15 +0000545#if defined(MBEDTLS_SSL_SRV_C)
546 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
XiaokangQian0557c942022-05-30 08:10:53 +0000547 {
548#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
549 if( ssl->handshake->sni_authmode != MBEDTLS_SSL_VERIFY_UNSET )
550 authmode = ssl->handshake->sni_authmode;
551 else
552#endif
553 authmode = ssl->conf->authmode;
554 }
XiaokangQian6b916b12022-04-25 07:29:34 +0000555#endif
556
557 /*
XiaokangQian989f06d2022-05-17 01:50:15 +0000558 * If the peer hasn't sent a certificate ( i.e. it sent
XiaokangQian6b916b12022-04-25 07:29:34 +0000559 * an empty certificate chain ), this is reflected in the peer CRT
560 * structure being unset.
561 * Check for that and handle it depending on the
XiaokangQian989f06d2022-05-17 01:50:15 +0000562 * authentication mode.
XiaokangQian6b916b12022-04-25 07:29:34 +0000563 */
XiaokangQian63e713e2022-05-15 04:26:57 +0000564 if( ssl->session_negotiate->peer_cert == NULL )
XiaokangQian6b916b12022-04-25 07:29:34 +0000565 {
Ronald Cron19385882022-06-15 16:26:13 +0200566 MBEDTLS_SSL_DEBUG_MSG( 1, ( "peer has no certificate" ) );
XiaokangQian989f06d2022-05-17 01:50:15 +0000567
XiaokangQian63e713e2022-05-15 04:26:57 +0000568#if defined(MBEDTLS_SSL_SRV_C)
569 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
570 {
XiaokangQian63e713e2022-05-15 04:26:57 +0000571 /* The client was asked for a certificate but didn't send
572 * one. The client should know what's going on, so we
573 * don't send an alert.
574 */
575 ssl->session_negotiate->verify_result = MBEDTLS_X509_BADCERT_MISSING;
576 if( authmode == MBEDTLS_SSL_VERIFY_OPTIONAL )
577 return( 0 );
578 else
XiaokangQian989f06d2022-05-17 01:50:15 +0000579 {
580 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_NO_CERT,
XiaokangQianaca90482022-05-19 07:19:31 +0000581 MBEDTLS_ERR_SSL_NO_CLIENT_CERTIFICATE );
582 return( MBEDTLS_ERR_SSL_NO_CLIENT_CERTIFICATE );
XiaokangQian989f06d2022-05-17 01:50:15 +0000583 }
XiaokangQian63e713e2022-05-15 04:26:57 +0000584 }
XiaokangQian6b916b12022-04-25 07:29:34 +0000585#endif /* MBEDTLS_SSL_SRV_C */
586
XiaokangQianc3017f62022-05-13 05:55:41 +0000587#if defined(MBEDTLS_SSL_CLI_C)
XiaokangQian63e713e2022-05-15 04:26:57 +0000588 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
589 {
590 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_NO_CERT,
591 MBEDTLS_ERR_SSL_FATAL_ALERT_MESSAGE );
592 return( MBEDTLS_ERR_SSL_FATAL_ALERT_MESSAGE );
593 }
XiaokangQianc3017f62022-05-13 05:55:41 +0000594#endif /* MBEDTLS_SSL_CLI_C */
XiaokangQian63e713e2022-05-15 04:26:57 +0000595 }
XiaokangQian6b916b12022-04-25 07:29:34 +0000596
Xiaofei Bai947571e2021-09-29 09:12:03 +0000597#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
598 if( ssl->handshake->sni_ca_chain != NULL )
599 {
600 ca_chain = ssl->handshake->sni_ca_chain;
601 ca_crl = ssl->handshake->sni_ca_crl;
602 }
603 else
604#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
605 {
606 ca_chain = ssl->conf->ca_chain;
607 ca_crl = ssl->conf->ca_crl;
608 }
609
610 /*
611 * Main check: verify certificate
612 */
613 ret = mbedtls_x509_crt_verify_with_profile(
614 ssl->session_negotiate->peer_cert,
615 ca_chain, ca_crl,
616 ssl->conf->cert_profile,
617 ssl->hostname,
Xiaofei Baiff456022021-10-28 06:50:17 +0000618 &verify_result,
Xiaofei Bai947571e2021-09-29 09:12:03 +0000619 ssl->conf->f_vrfy, ssl->conf->p_vrfy );
620
621 if( ret != 0 )
622 {
623 MBEDTLS_SSL_DEBUG_RET( 1, "x509_verify_cert", ret );
624 }
625
626 /*
627 * Secondary checks: always done, but change 'ret' only if it was 0
628 */
Xiaofei Bai947571e2021-09-29 09:12:03 +0000629 if( mbedtls_ssl_check_cert_usage( ssl->session_negotiate->peer_cert,
630 ssl->handshake->ciphersuite_info,
631 !ssl->conf->endpoint,
Xiaofei Baiff456022021-10-28 06:50:17 +0000632 &verify_result ) != 0 )
Xiaofei Bai947571e2021-09-29 09:12:03 +0000633 {
634 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate ( usage extensions )" ) );
635 if( ret == 0 )
636 ret = MBEDTLS_ERR_SSL_BAD_CERTIFICATE;
637 }
638
XiaokangQian6b916b12022-04-25 07:29:34 +0000639 /* mbedtls_x509_crt_verify_with_profile is supposed to report a
640 * verification failure through MBEDTLS_ERR_X509_CERT_VERIFY_FAILED,
641 * with details encoded in the verification flags. All other kinds
642 * of error codes, including those from the user provided f_vrfy
643 * functions, are treated as fatal and lead to a failure of
644 * ssl_tls13_parse_certificate even if verification was optional. */
645 if( authmode == MBEDTLS_SSL_VERIFY_OPTIONAL &&
646 ( ret == MBEDTLS_ERR_X509_CERT_VERIFY_FAILED ||
647 ret == MBEDTLS_ERR_SSL_BAD_CERTIFICATE ) )
648 {
649 ret = 0;
650 }
Xiaofei Bai947571e2021-09-29 09:12:03 +0000651
XiaokangQian6b916b12022-04-25 07:29:34 +0000652 if( ca_chain == NULL && authmode == MBEDTLS_SSL_VERIFY_REQUIRED )
Xiaofei Bai947571e2021-09-29 09:12:03 +0000653 {
654 MBEDTLS_SSL_DEBUG_MSG( 1, ( "got no CA chain" ) );
655 ret = MBEDTLS_ERR_SSL_CA_CHAIN_REQUIRED;
656 }
657
658 if( ret != 0 )
659 {
660 /* The certificate may have been rejected for several reasons.
661 Pick one and send the corresponding alert. Which alert to send
662 may be a subject of debate in some cases. */
Xiaofei Baiff456022021-10-28 06:50:17 +0000663 if( verify_result & MBEDTLS_X509_BADCERT_OTHER )
Xiaofei Bai947571e2021-09-29 09:12:03 +0000664 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED, ret );
Xiaofei Baiff456022021-10-28 06:50:17 +0000665 else if( verify_result & MBEDTLS_X509_BADCERT_CN_MISMATCH )
Xiaofei Bai947571e2021-09-29 09:12:03 +0000666 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_BAD_CERT, ret );
Xiaofei Baif93cbd22021-10-29 02:39:30 +0000667 else if( verify_result & ( MBEDTLS_X509_BADCERT_KEY_USAGE |
668 MBEDTLS_X509_BADCERT_EXT_KEY_USAGE |
669 MBEDTLS_X509_BADCERT_NS_CERT_TYPE |
670 MBEDTLS_X509_BADCERT_BAD_PK |
671 MBEDTLS_X509_BADCERT_BAD_KEY ) )
Xiaofei Bai947571e2021-09-29 09:12:03 +0000672 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT, ret );
Xiaofei Baiff456022021-10-28 06:50:17 +0000673 else if( verify_result & MBEDTLS_X509_BADCERT_EXPIRED )
Xiaofei Bai947571e2021-09-29 09:12:03 +0000674 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_CERT_EXPIRED, ret );
Xiaofei Baiff456022021-10-28 06:50:17 +0000675 else if( verify_result & MBEDTLS_X509_BADCERT_REVOKED )
Xiaofei Bai947571e2021-09-29 09:12:03 +0000676 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_CERT_REVOKED, ret );
Xiaofei Baiff456022021-10-28 06:50:17 +0000677 else if( verify_result & MBEDTLS_X509_BADCERT_NOT_TRUSTED )
Xiaofei Bai947571e2021-09-29 09:12:03 +0000678 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_UNKNOWN_CA, ret );
679 else
680 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_CERT_UNKNOWN, ret );
681 }
682
683#if defined(MBEDTLS_DEBUG_C)
Xiaofei Baiff456022021-10-28 06:50:17 +0000684 if( verify_result != 0 )
Xiaofei Bai947571e2021-09-29 09:12:03 +0000685 {
686 MBEDTLS_SSL_DEBUG_MSG( 3, ( "! Certificate verification flags %08x",
Jerry Yu83bb1312021-10-28 22:16:33 +0800687 (unsigned int) verify_result ) );
Xiaofei Bai947571e2021-09-29 09:12:03 +0000688 }
689 else
690 {
691 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Certificate verification flags clear" ) );
692 }
693#endif /* MBEDTLS_DEBUG_C */
694
Xiaofei Baiff456022021-10-28 06:50:17 +0000695 ssl->session_negotiate->verify_result = verify_result;
Xiaofei Bai947571e2021-09-29 09:12:03 +0000696 return( ret );
697}
698#else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
699static int ssl_tls13_validate_certificate( mbedtls_ssl_context *ssl )
700{
701 ((void) ssl);
702 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
703}
704#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
705#endif /* MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED */
706
Xiaofei Bai79595ac2021-10-26 07:16:45 +0000707int mbedtls_ssl_tls13_process_certificate( mbedtls_ssl_context *ssl )
Xiaofei Bai947571e2021-09-29 09:12:03 +0000708{
Xiaofei Bai79595ac2021-10-26 07:16:45 +0000709 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
710 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) );
711
712#if defined(MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED)
XiaokangQianc3017f62022-05-13 05:55:41 +0000713 unsigned char *buf;
714 size_t buf_len;
Xiaofei Bai79595ac2021-10-26 07:16:45 +0000715
XiaokangQianc3017f62022-05-13 05:55:41 +0000716 MBEDTLS_SSL_PROC_CHK( mbedtls_ssl_tls13_fetch_handshake_msg(
717 ssl, MBEDTLS_SSL_HS_CERTIFICATE,
718 &buf, &buf_len ) );
Xiaofei Bai79595ac2021-10-26 07:16:45 +0000719
XiaokangQianc3017f62022-05-13 05:55:41 +0000720 /* Parse the certificate chain sent by the peer. */
721 MBEDTLS_SSL_PROC_CHK( ssl_tls13_parse_certificate( ssl, buf,
722 buf + buf_len ) );
723 /* Validate the certificate chain and set the verification results. */
724 MBEDTLS_SSL_PROC_CHK( ssl_tls13_validate_certificate( ssl ) );
Xiaofei Bai79595ac2021-10-26 07:16:45 +0000725
XiaokangQianc3017f62022-05-13 05:55:41 +0000726 mbedtls_ssl_add_hs_msg_to_checksum( ssl, MBEDTLS_SSL_HS_CERTIFICATE,
727 buf, buf_len );
Xiaofei Bai79595ac2021-10-26 07:16:45 +0000728
Xiaofei Bai79595ac2021-10-26 07:16:45 +0000729cleanup:
XiaokangQianaca90482022-05-19 07:19:31 +0000730#endif /* MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED */
Xiaofei Bai79595ac2021-10-26 07:16:45 +0000731
732 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse certificate" ) );
733 return( ret );
Xiaofei Bai947571e2021-09-29 09:12:03 +0000734}
Jerry Yu90f152d2022-01-29 22:12:42 +0800735#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Jerry Yu7399d0d2022-01-30 17:54:19 +0800736/*
737 * enum {
738 * X509(0),
739 * RawPublicKey(2),
740 * (255)
741 * } CertificateType;
742 *
743 * struct {
744 * select (certificate_type) {
745 * case RawPublicKey:
746 * // From RFC 7250 ASN.1_subjectPublicKeyInfo
747 * opaque ASN1_subjectPublicKeyInfo<1..2^24-1>;
748 *
749 * case X509:
750 * opaque cert_data<1..2^24-1>;
751 * };
752 * Extension extensions<0..2^16-1>;
753 * } CertificateEntry;
754 *
755 * struct {
756 * opaque certificate_request_context<0..2^8-1>;
757 * CertificateEntry certificate_list<0..2^24-1>;
758 * } Certificate;
759 */
760static int ssl_tls13_write_certificate_body( mbedtls_ssl_context *ssl,
Jerry Yu3e536442022-02-15 11:05:59 +0800761 unsigned char *buf,
Jerry Yu7399d0d2022-01-30 17:54:19 +0800762 unsigned char *end,
Jerry Yu3e536442022-02-15 11:05:59 +0800763 size_t *out_len )
Jerry Yu5cc35062022-01-28 16:16:08 +0800764{
Jerry Yu5cc35062022-01-28 16:16:08 +0800765 const mbedtls_x509_crt *crt = mbedtls_ssl_own_cert( ssl );
Jerry Yu3e536442022-02-15 11:05:59 +0800766 unsigned char *p = buf;
Jerry Yuc8d8d4e2022-02-18 12:10:03 +0800767 unsigned char *certificate_request_context =
768 ssl->handshake->certificate_request_context;
769 unsigned char certificate_request_context_len =
770 ssl->handshake->certificate_request_context_len;
771 unsigned char *p_certificate_list_len;
Jerry Yu5cc35062022-01-28 16:16:08 +0800772
Jerry Yu5cc35062022-01-28 16:16:08 +0800773
Jerry Yu3391ac02022-02-16 11:21:37 +0800774 /* ...
775 * opaque certificate_request_context<0..2^8-1>;
776 * ...
777 */
Jerry Yuc8d8d4e2022-02-18 12:10:03 +0800778 MBEDTLS_SSL_CHK_BUF_PTR( p, end, certificate_request_context_len + 1 );
779 *p++ = certificate_request_context_len;
780 if( certificate_request_context_len > 0 )
Jerry Yu537530d2022-02-15 14:00:57 +0800781 {
Jerry Yuc8d8d4e2022-02-18 12:10:03 +0800782 memcpy( p, certificate_request_context, certificate_request_context_len );
783 p += certificate_request_context_len;
Jerry Yu537530d2022-02-15 14:00:57 +0800784 }
785
Jerry Yu3391ac02022-02-16 11:21:37 +0800786 /* ...
787 * CertificateEntry certificate_list<0..2^24-1>;
788 * ...
789 */
Jerry Yu3e536442022-02-15 11:05:59 +0800790 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 3 );
Jerry Yuc8d8d4e2022-02-18 12:10:03 +0800791 p_certificate_list_len = p;
Jerry Yu3e536442022-02-15 11:05:59 +0800792 p += 3;
793
Jerry Yu7399d0d2022-01-30 17:54:19 +0800794 MBEDTLS_SSL_DEBUG_CRT( 3, "own certificate", crt );
Jerry Yu5cc35062022-01-28 16:16:08 +0800795
Jerry Yuc8d8d4e2022-02-18 12:10:03 +0800796 while( crt != NULL )
Jerry Yu5cc35062022-01-28 16:16:08 +0800797 {
Jerry Yu7399d0d2022-01-30 17:54:19 +0800798 size_t cert_data_len = crt->raw.len;
Jerry Yu5cc35062022-01-28 16:16:08 +0800799
Jerry Yu7399d0d2022-01-30 17:54:19 +0800800 MBEDTLS_SSL_CHK_BUF_PTR( p, end, cert_data_len + 3 + 2 );
801 MBEDTLS_PUT_UINT24_BE( cert_data_len, p, 0 );
802 p += 3;
Jerry Yu5cc35062022-01-28 16:16:08 +0800803
Jerry Yu7399d0d2022-01-30 17:54:19 +0800804 memcpy( p, crt->raw.p, cert_data_len );
805 p += cert_data_len;
806 crt = crt->next;
Jerry Yu5cc35062022-01-28 16:16:08 +0800807
808 /* Currently, we don't have any certificate extensions defined.
809 * Hence, we are sending an empty extension with length zero.
810 */
Ronald Cron11b53322022-06-01 14:58:52 +0200811 MBEDTLS_PUT_UINT16_BE( 0, p, 0 );
Jerry Yu7399d0d2022-01-30 17:54:19 +0800812 p += 2;
Jerry Yu5cc35062022-01-28 16:16:08 +0800813 }
Jerry Yu5cc35062022-01-28 16:16:08 +0800814
Jerry Yuc8d8d4e2022-02-18 12:10:03 +0800815 MBEDTLS_PUT_UINT24_BE( p - p_certificate_list_len - 3,
816 p_certificate_list_len, 0 );
Jerry Yu7399d0d2022-01-30 17:54:19 +0800817
Jerry Yu3e536442022-02-15 11:05:59 +0800818 *out_len = p - buf;
Jerry Yu5cc35062022-01-28 16:16:08 +0800819
820 return( 0 );
821}
Jerry Yu5cc35062022-01-28 16:16:08 +0800822
Jerry Yu3e536442022-02-15 11:05:59 +0800823int mbedtls_ssl_tls13_write_certificate( mbedtls_ssl_context *ssl )
Jerry Yu5cc35062022-01-28 16:16:08 +0800824{
825 int ret;
Ronald Cron5bb8fc82022-03-09 07:00:13 +0100826 unsigned char *buf;
827 size_t buf_len, msg_len;
828
Jerry Yu5cc35062022-01-28 16:16:08 +0800829 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write certificate" ) );
830
Ronald Cron8f6d39a2022-03-10 18:56:50 +0100831 MBEDTLS_SSL_PROC_CHK( mbedtls_ssl_start_handshake_msg( ssl,
Ronald Cron5bb8fc82022-03-09 07:00:13 +0100832 MBEDTLS_SSL_HS_CERTIFICATE, &buf, &buf_len ) );
Jerry Yu5cc35062022-01-28 16:16:08 +0800833
Ronald Cron5bb8fc82022-03-09 07:00:13 +0100834 MBEDTLS_SSL_PROC_CHK( ssl_tls13_write_certificate_body( ssl,
835 buf,
836 buf + buf_len,
837 &msg_len ) );
Jerry Yu5cc35062022-01-28 16:16:08 +0800838
Ronald Cron8f6d39a2022-03-10 18:56:50 +0100839 mbedtls_ssl_add_hs_msg_to_checksum( ssl, MBEDTLS_SSL_HS_CERTIFICATE,
840 buf, msg_len );
Jerry Yu5cc35062022-01-28 16:16:08 +0800841
Ronald Cron8f6d39a2022-03-10 18:56:50 +0100842 MBEDTLS_SSL_PROC_CHK( mbedtls_ssl_finish_handshake_msg(
Ronald Cron5bb8fc82022-03-09 07:00:13 +0100843 ssl, buf_len, msg_len ) );
Jerry Yu5cc35062022-01-28 16:16:08 +0800844cleanup:
845
846 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write certificate" ) );
847 return( ret );
848}
849
Jerry Yu3e536442022-02-15 11:05:59 +0800850/*
851 * STATE HANDLING: Output Certificate Verify
852 */
Jerry Yu67eced02022-02-25 13:37:36 +0800853
Jerry Yuc2e04932022-06-27 22:13:03 +0800854int mbedtls_ssl_tls13_check_sig_alg_cert_key_match( uint16_t sig_alg,
855 mbedtls_pk_context *key )
Jerry Yu0c6be8f2022-06-20 20:42:00 +0800856{
857 mbedtls_pk_type_t pk_type = mbedtls_ssl_sig_from_pk( key );
858 size_t key_size = mbedtls_pk_get_bitlen( key );
859
860 switch( pk_type )
Jerry Yu67eced02022-02-25 13:37:36 +0800861 {
Jerry Yu67eced02022-02-25 13:37:36 +0800862 case MBEDTLS_SSL_SIG_ECDSA:
Jerry Yu0c6be8f2022-06-20 20:42:00 +0800863 switch( key_size )
Jerry Yu67eced02022-02-25 13:37:36 +0800864 {
865 case 256:
Jerry Yu0c6be8f2022-06-20 20:42:00 +0800866 return(
867 sig_alg == MBEDTLS_TLS1_3_SIG_ECDSA_SECP256R1_SHA256 );
Jerry Yu0c6be8f2022-06-20 20:42:00 +0800868
Jerry Yu67eced02022-02-25 13:37:36 +0800869 case 384:
Jerry Yu0c6be8f2022-06-20 20:42:00 +0800870 return(
871 sig_alg == MBEDTLS_TLS1_3_SIG_ECDSA_SECP384R1_SHA384 );
Jerry Yu0c6be8f2022-06-20 20:42:00 +0800872
Jerry Yu67eced02022-02-25 13:37:36 +0800873 case 521:
Jerry Yu0c6be8f2022-06-20 20:42:00 +0800874 return(
875 sig_alg == MBEDTLS_TLS1_3_SIG_ECDSA_SECP521R1_SHA512 );
Jerry Yu67eced02022-02-25 13:37:36 +0800876 default:
Jerry Yu67eced02022-02-25 13:37:36 +0800877 break;
878 }
879 break;
Jerry Yu67eced02022-02-25 13:37:36 +0800880
Jerry Yu67eced02022-02-25 13:37:36 +0800881 case MBEDTLS_SSL_SIG_RSA:
Jerry Yu0c6be8f2022-06-20 20:42:00 +0800882 switch( sig_alg )
Jerry Yu67eced02022-02-25 13:37:36 +0800883 {
Jerry Yu0c6be8f2022-06-20 20:42:00 +0800884 case MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA256:
Jerry Yua1255e62022-06-24 10:10:47 +0800885 return( key_size <= 3072 );
Jerry Yu0c6be8f2022-06-20 20:42:00 +0800886
Jerry Yu0c6be8f2022-06-20 20:42:00 +0800887 case MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA384:
Jerry Yua1255e62022-06-24 10:10:47 +0800888 return( key_size <= 7680 );
Jerry Yu0c6be8f2022-06-20 20:42:00 +0800889
Jerry Yu0c6be8f2022-06-20 20:42:00 +0800890 case MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA512:
Jerry Yua1255e62022-06-24 10:10:47 +0800891 return( 1 );
Jerry Yuc2e04932022-06-27 22:13:03 +0800892
Jerry Yu0c6be8f2022-06-20 20:42:00 +0800893 default:
894 break;
Jerry Yucef3f332022-03-22 23:00:13 +0800895 }
Jerry Yu67eced02022-02-25 13:37:36 +0800896 break;
Jerry Yu0c6be8f2022-06-20 20:42:00 +0800897
Jerry Yu67eced02022-02-25 13:37:36 +0800898 default:
Jerry Yu67eced02022-02-25 13:37:36 +0800899 break;
900 }
Jerry Yu0c6be8f2022-06-20 20:42:00 +0800901
902 return( 0 );
903}
904
Jerry Yuf55886a2022-06-19 11:48:56 +0800905static int ssl_tls13_select_sig_alg_for_certificate_verify(
906 mbedtls_ssl_context *ssl,
Jerry Yuf249ef72022-06-15 17:23:33 +0800907 mbedtls_pk_context *own_key,
908 uint16_t *algorithm )
Jerry Yu67eced02022-02-25 13:37:36 +0800909{
Jerry Yuf249ef72022-06-15 17:23:33 +0800910 uint16_t *sig_alg = ssl->handshake->received_sig_algs;
911
Jerry Yu67eced02022-02-25 13:37:36 +0800912 *algorithm = MBEDTLS_TLS1_3_SIG_NONE;
Jerry Yuf249ef72022-06-15 17:23:33 +0800913 for( ; *sig_alg != MBEDTLS_TLS1_3_SIG_NONE ; sig_alg++ )
Jerry Yu67eced02022-02-25 13:37:36 +0800914 {
Jerry Yu660cb422022-06-28 16:17:58 +0800915 if( mbedtls_ssl_sig_alg_is_offered( ssl, *sig_alg ) &&
Jerry Yu959e5e02022-06-29 09:49:02 +0800916 mbedtls_ssl_tls13_sig_alg_for_cert_verify_is_supported( *sig_alg ) &&
917 mbedtls_ssl_tls13_check_sig_alg_cert_key_match( *sig_alg, own_key ) )
Jerry Yuf249ef72022-06-15 17:23:33 +0800918 {
Jerry Yucc539102022-06-27 16:27:35 +0800919 MBEDTLS_SSL_DEBUG_MSG( 3,
920 ( "select_sig_alg_for_certificate_verify:"
921 "selected signature algorithm %s [%04x]",
922 mbedtls_ssl_sig_alg_to_str( *sig_alg ),
923 *sig_alg ) );
Jerry Yuf249ef72022-06-15 17:23:33 +0800924 *algorithm = *sig_alg;
925 return( 0 );
926 }
Jerry Yu67eced02022-02-25 13:37:36 +0800927 }
Jerry Yucc539102022-06-27 16:27:35 +0800928 MBEDTLS_SSL_DEBUG_MSG( 2,
929 ( "select_sig_alg_for_certificate_verify:"
930 "no suitable signature algorithm found" ) );
Jerry Yue91a51a2022-03-22 21:42:50 +0800931 return( -1 );
Jerry Yu67eced02022-02-25 13:37:36 +0800932}
933
Jerry Yu3e536442022-02-15 11:05:59 +0800934static int ssl_tls13_write_certificate_verify_body( mbedtls_ssl_context *ssl,
935 unsigned char *buf,
936 unsigned char *end,
937 size_t *out_len )
Jerry Yu8511f122022-01-29 10:01:04 +0800938{
939 int ret;
Jerry Yu3e536442022-02-15 11:05:59 +0800940 unsigned char *p = buf;
Jerry Yu8511f122022-01-29 10:01:04 +0800941 mbedtls_pk_context *own_key;
Jerry Yu3e536442022-02-15 11:05:59 +0800942
Jerry Yu8511f122022-01-29 10:01:04 +0800943 unsigned char handshake_hash[ MBEDTLS_TLS1_3_MD_MAX_SIZE ];
944 size_t handshake_hash_len;
Jerry Yu3e536442022-02-15 11:05:59 +0800945 unsigned char verify_buffer[ SSL_VERIFY_STRUCT_MAX_SIZE ];
946 size_t verify_buffer_len;
Jerry Yu67eced02022-02-25 13:37:36 +0800947 mbedtls_pk_type_t pk_type = MBEDTLS_PK_NONE;
Jerry Yu67eced02022-02-25 13:37:36 +0800948 mbedtls_md_type_t md_alg = MBEDTLS_MD_NONE;
pespacek34935872022-05-20 15:43:32 +0200949 psa_algorithm_t psa_algorithm = PSA_ALG_NONE;
Jerry Yu78272072022-02-22 10:28:13 +0800950 uint16_t algorithm = MBEDTLS_TLS1_3_SIG_NONE;
Jerry Yu3e536442022-02-15 11:05:59 +0800951 size_t signature_len = 0;
Jerry Yu3391ac02022-02-16 11:21:37 +0800952 unsigned char verify_hash[ MBEDTLS_MD_MAX_SIZE ];
Jerry Yu3e536442022-02-15 11:05:59 +0800953 size_t verify_hash_len;
pespacekb06acd72022-06-07 13:07:21 +0200954 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Jerry Yu8511f122022-01-29 10:01:04 +0800955
Jerry Yu0b7b1012022-02-23 12:23:05 +0800956 *out_len = 0;
957
Jerry Yu3e536442022-02-15 11:05:59 +0800958 own_key = mbedtls_ssl_own_key( ssl );
959 if( own_key == NULL )
Jerry Yu8511f122022-01-29 10:01:04 +0800960 {
Jerry Yu3e536442022-02-15 11:05:59 +0800961 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
962 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Jerry Yu8511f122022-01-29 10:01:04 +0800963 }
964
Jerry Yu8511f122022-01-29 10:01:04 +0800965 ret = mbedtls_ssl_get_handshake_transcript( ssl,
966 ssl->handshake->ciphersuite_info->mac,
967 handshake_hash,
968 sizeof( handshake_hash ),
969 &handshake_hash_len );
970 if( ret != 0 )
971 return( ret );
972
973 MBEDTLS_SSL_DEBUG_BUF( 3, "handshake hash",
974 handshake_hash,
975 handshake_hash_len);
976
Jerry Yu8511f122022-01-29 10:01:04 +0800977 ssl_tls13_create_verify_structure( handshake_hash, handshake_hash_len,
978 verify_buffer, &verify_buffer_len,
979 ssl->conf->endpoint );
980
981 /*
982 * struct {
983 * SignatureScheme algorithm;
984 * opaque signature<0..2^16-1>;
985 * } CertificateVerify;
986 */
Jerry Yuf55886a2022-06-19 11:48:56 +0800987 ret = ssl_tls13_select_sig_alg_for_certificate_verify( ssl, own_key,
988 &algorithm );
989 if( ret != 0 )
Jerry Yu8511f122022-01-29 10:01:04 +0800990 {
Jerry Yud66409a2022-02-18 16:42:24 +0800991 MBEDTLS_SSL_DEBUG_MSG( 1,
Jerry Yu2124d052022-02-18 21:07:18 +0800992 ( "signature algorithm not in received or offered list." ) );
Jerry Yu67eced02022-02-25 13:37:36 +0800993
994 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Signature algorithm is %s",
995 mbedtls_ssl_sig_alg_to_str( algorithm ) ) );
996
Jerry Yu71f36f12022-02-23 17:34:29 +0800997 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE,
Jerry Yud66409a2022-02-18 16:42:24 +0800998 MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
Jerry Yu3391ac02022-02-16 11:21:37 +0800999 return( MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
Jerry Yu8511f122022-01-29 10:01:04 +08001000 }
1001
Jerry Yuf3b46b52022-06-19 16:52:27 +08001002 MBEDTLS_SSL_DEBUG_MSG( 2, ( "CertificateVerify with %s",
1003 mbedtls_ssl_sig_alg_to_str( algorithm )) );
1004
Jerry Yu6c6f1022022-03-25 11:09:50 +08001005 if( mbedtls_ssl_tls13_get_pk_type_and_md_alg_from_sig_alg(
1006 algorithm, &pk_type, &md_alg ) != 0 )
Jerry Yu8c338862022-03-23 13:34:04 +08001007 {
Jerry Yuf8aa9a42022-03-23 20:40:28 +08001008 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Jerry Yu8c338862022-03-23 13:34:04 +08001009 }
1010
Jerry Yu3391ac02022-02-16 11:21:37 +08001011 /* Check there is space for the algorithm identifier (2 bytes) and the
1012 * signature length (2 bytes).
1013 */
1014 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 4 );
Jerry Yu3e536442022-02-15 11:05:59 +08001015 MBEDTLS_PUT_UINT16_BE( algorithm, p, 0 );
1016 p += 2;
Jerry Yu8511f122022-01-29 10:01:04 +08001017
1018 /* Hash verify buffer with indicated hash function */
pespacek34935872022-05-20 15:43:32 +02001019 psa_algorithm = mbedtls_psa_translate_md( md_alg );
pespacekb06acd72022-06-07 13:07:21 +02001020 status = psa_hash_compute( psa_algorithm,
1021 verify_buffer,
1022 verify_buffer_len,
1023 verify_hash,sizeof( verify_hash ),
1024 &verify_hash_len );
1025 if( status != PSA_SUCCESS )
pespacek670913f2022-06-07 10:53:39 +02001026 return( psa_ssl_status_to_mbedtls( status ) );
Jerry Yu8511f122022-01-29 10:01:04 +08001027
Jerry Yu8511f122022-01-29 10:01:04 +08001028 MBEDTLS_SSL_DEBUG_BUF( 3, "verify hash", verify_hash, verify_hash_len );
1029
Jerry Yu8beb9e12022-03-12 16:23:53 +08001030 if( ( ret = mbedtls_pk_sign_ext( pk_type, own_key,
1031 md_alg, verify_hash, verify_hash_len,
Jerry Yu67eced02022-02-25 13:37:36 +08001032 p + 2, (size_t)( end - ( p + 2 ) ), &signature_len,
1033 ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 )
Jerry Yu8511f122022-01-29 10:01:04 +08001034 {
1035 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_pk_sign", ret );
1036 return( ret );
1037 }
Jerry Yu8511f122022-01-29 10:01:04 +08001038
Jerry Yu3e536442022-02-15 11:05:59 +08001039 MBEDTLS_PUT_UINT16_BE( signature_len, p, 0 );
1040 p += 2 + signature_len;
Jerry Yu8511f122022-01-29 10:01:04 +08001041
Jerry Yu3e536442022-02-15 11:05:59 +08001042 *out_len = (size_t)( p - buf );
1043
Jerry Yu8511f122022-01-29 10:01:04 +08001044 return( ret );
1045}
Jerry Yu8511f122022-01-29 10:01:04 +08001046
Jerry Yu3e536442022-02-15 11:05:59 +08001047int mbedtls_ssl_tls13_write_certificate_verify( mbedtls_ssl_context *ssl )
Jerry Yu8511f122022-01-29 10:01:04 +08001048{
1049 int ret = 0;
Jerry Yuca133a32022-02-15 14:22:05 +08001050 unsigned char *buf;
1051 size_t buf_len, msg_len;
1052
Jerry Yu8511f122022-01-29 10:01:04 +08001053 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write certificate verify" ) );
1054
Ronald Cron8f6d39a2022-03-10 18:56:50 +01001055 MBEDTLS_SSL_PROC_CHK( mbedtls_ssl_start_handshake_msg( ssl,
Jerry Yuca133a32022-02-15 14:22:05 +08001056 MBEDTLS_SSL_HS_CERTIFICATE_VERIFY, &buf, &buf_len ) );
Jerry Yu8511f122022-01-29 10:01:04 +08001057
Jerry Yuca133a32022-02-15 14:22:05 +08001058 MBEDTLS_SSL_PROC_CHK( ssl_tls13_write_certificate_verify_body(
1059 ssl, buf, buf + buf_len, &msg_len ) );
Jerry Yu8511f122022-01-29 10:01:04 +08001060
Ronald Cron8f6d39a2022-03-10 18:56:50 +01001061 mbedtls_ssl_add_hs_msg_to_checksum( ssl, MBEDTLS_SSL_HS_CERTIFICATE_VERIFY,
1062 buf, msg_len );
Jerry Yu8511f122022-01-29 10:01:04 +08001063
Ronald Cron8f6d39a2022-03-10 18:56:50 +01001064 MBEDTLS_SSL_PROC_CHK( mbedtls_ssl_finish_handshake_msg(
Jerry Yuca133a32022-02-15 14:22:05 +08001065 ssl, buf_len, msg_len ) );
Jerry Yu8511f122022-01-29 10:01:04 +08001066
1067cleanup:
1068
1069 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write certificate verify" ) );
1070 return( ret );
1071}
1072
Jerry Yu90f152d2022-01-29 22:12:42 +08001073#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
1074
Jerry Yu5cc35062022-01-28 16:16:08 +08001075/*
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001076 *
XiaokangQianc5c39d52021-11-09 11:55:10 +00001077 * STATE HANDLING: Incoming Finished message.
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001078 */
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001079/*
1080 * Implementation
1081 */
1082
XiaokangQianaaa0e192021-11-10 03:07:04 +00001083static int ssl_tls13_preprocess_finished_message( mbedtls_ssl_context *ssl )
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001084{
1085 int ret;
1086
XiaokangQianc5c39d52021-11-09 11:55:10 +00001087 ret = mbedtls_ssl_tls13_calculate_verify_data( ssl,
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001088 ssl->handshake->state_local.finished_in.digest,
1089 sizeof( ssl->handshake->state_local.finished_in.digest ),
1090 &ssl->handshake->state_local.finished_in.digest_len,
XiaokangQianc5c39d52021-11-09 11:55:10 +00001091 ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT ?
XiaokangQianc13f9352021-11-11 06:13:22 +00001092 MBEDTLS_SSL_IS_SERVER : MBEDTLS_SSL_IS_CLIENT );
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001093 if( ret != 0 )
1094 {
XiaokangQianc5c39d52021-11-09 11:55:10 +00001095 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_tls13_calculate_verify_data", ret );
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001096 return( ret );
1097 }
1098
1099 return( 0 );
1100}
1101
XiaokangQianc5c39d52021-11-09 11:55:10 +00001102static int ssl_tls13_parse_finished_message( mbedtls_ssl_context *ssl,
1103 const unsigned char *buf,
1104 const unsigned char *end )
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001105{
XiaokangQian33062842021-11-11 03:37:45 +00001106 /*
1107 * struct {
XiaokangQianc13f9352021-11-11 06:13:22 +00001108 * opaque verify_data[Hash.length];
XiaokangQian33062842021-11-11 03:37:45 +00001109 * } Finished;
1110 */
1111 const unsigned char *expected_verify_data =
1112 ssl->handshake->state_local.finished_in.digest;
1113 size_t expected_verify_data_len =
1114 ssl->handshake->state_local.finished_in.digest_len;
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001115 /* Structural validation */
XiaokangQian33062842021-11-11 03:37:45 +00001116 if( (size_t)( end - buf ) != expected_verify_data_len )
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001117 {
1118 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
1119
1120 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR,
XiaokangQianc13f9352021-11-11 06:13:22 +00001121 MBEDTLS_ERR_SSL_DECODE_ERROR );
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001122 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
1123 }
1124
XiaokangQianc5c39d52021-11-09 11:55:10 +00001125 MBEDTLS_SSL_DEBUG_BUF( 4, "verify_data (self-computed):",
XiaokangQian33062842021-11-11 03:37:45 +00001126 expected_verify_data,
1127 expected_verify_data_len );
XiaokangQianc5c39d52021-11-09 11:55:10 +00001128 MBEDTLS_SSL_DEBUG_BUF( 4, "verify_data (received message):", buf,
XiaokangQian33062842021-11-11 03:37:45 +00001129 expected_verify_data_len );
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001130
1131 /* Semantic validation */
Gabor Mezei685472b2021-11-24 11:17:36 +01001132 if( mbedtls_ct_memcmp( buf,
1133 expected_verify_data,
1134 expected_verify_data_len ) != 0 )
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001135 {
1136 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
1137
XiaokangQian33062842021-11-11 03:37:45 +00001138 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_DECRYPT_ERROR,
XiaokangQianc13f9352021-11-11 06:13:22 +00001139 MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001140 return( MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
1141 }
1142 return( 0 );
1143}
1144
XiaokangQianc5c39d52021-11-09 11:55:10 +00001145int mbedtls_ssl_tls13_process_finished_message( mbedtls_ssl_context *ssl )
1146{
XiaokangQian33062842021-11-11 03:37:45 +00001147 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
XiaokangQianc5c39d52021-11-09 11:55:10 +00001148 unsigned char *buf;
Xiaofei Baieef15042021-11-18 07:29:56 +00001149 size_t buf_len;
XiaokangQianc5c39d52021-11-09 11:55:10 +00001150
XiaokangQiand0aa3e92021-11-10 06:17:40 +00001151 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse finished message" ) );
XiaokangQianc5c39d52021-11-09 11:55:10 +00001152
Xiaofei Bai746f9482021-11-12 08:53:56 +00001153 MBEDTLS_SSL_PROC_CHK( mbedtls_ssl_tls13_fetch_handshake_msg( ssl,
XiaokangQianc5c39d52021-11-09 11:55:10 +00001154 MBEDTLS_SSL_HS_FINISHED,
Xiaofei Baieef15042021-11-18 07:29:56 +00001155 &buf, &buf_len ) );
Jerry Yu0a92d6c2022-05-16 16:54:46 +08001156
1157 /* Preprocessing step: Compute handshake digest */
1158 MBEDTLS_SSL_PROC_CHK( ssl_tls13_preprocess_finished_message( ssl ) );
1159
Xiaofei Baieef15042021-11-18 07:29:56 +00001160 MBEDTLS_SSL_PROC_CHK( ssl_tls13_parse_finished_message( ssl, buf, buf + buf_len ) );
Jerry Yu0a92d6c2022-05-16 16:54:46 +08001161
Ronald Cron8f6d39a2022-03-10 18:56:50 +01001162 mbedtls_ssl_add_hs_msg_to_checksum( ssl, MBEDTLS_SSL_HS_FINISHED,
1163 buf, buf_len );
XiaokangQianc5c39d52021-11-09 11:55:10 +00001164
1165cleanup:
1166
XiaokangQiand0aa3e92021-11-10 06:17:40 +00001167 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse finished message" ) );
XiaokangQianc5c39d52021-11-09 11:55:10 +00001168 return( ret );
1169}
1170
XiaokangQian74af2a82021-09-22 07:40:30 +00001171/*
1172 *
XiaokangQiancc90c942021-11-09 12:30:09 +00001173 * STATE HANDLING: Write and send Finished message.
XiaokangQian74af2a82021-09-22 07:40:30 +00001174 *
1175 */
XiaokangQian74af2a82021-09-22 07:40:30 +00001176/*
XiaokangQian35dc6252021-11-11 08:16:19 +00001177 * Implement
XiaokangQian74af2a82021-09-22 07:40:30 +00001178 */
1179
XiaokangQian8773aa02021-11-10 07:33:09 +00001180static int ssl_tls13_prepare_finished_message( mbedtls_ssl_context *ssl )
XiaokangQian74af2a82021-09-22 07:40:30 +00001181{
1182 int ret;
1183
1184 /* Compute transcript of handshake up to now. */
XiaokangQiancc90c942021-11-09 12:30:09 +00001185 ret = mbedtls_ssl_tls13_calculate_verify_data( ssl,
XiaokangQian74af2a82021-09-22 07:40:30 +00001186 ssl->handshake->state_local.finished_out.digest,
1187 sizeof( ssl->handshake->state_local.finished_out.digest ),
1188 &ssl->handshake->state_local.finished_out.digest_len,
1189 ssl->conf->endpoint );
1190
1191 if( ret != 0 )
1192 {
Jerry Yu7ca30542021-12-08 15:57:57 +08001193 MBEDTLS_SSL_DEBUG_RET( 1, "calculate_verify_data failed", ret );
XiaokangQian74af2a82021-09-22 07:40:30 +00001194 return( ret );
1195 }
1196
1197 return( 0 );
1198}
1199
XiaokangQian8773aa02021-11-10 07:33:09 +00001200static int ssl_tls13_write_finished_message_body( mbedtls_ssl_context *ssl,
XiaokangQian35dc6252021-11-11 08:16:19 +00001201 unsigned char *buf,
1202 unsigned char *end,
Xiaofei Baid25fab62021-12-02 06:36:27 +00001203 size_t *out_len )
XiaokangQian74af2a82021-09-22 07:40:30 +00001204{
XiaokangQian8773aa02021-11-10 07:33:09 +00001205 size_t verify_data_len = ssl->handshake->state_local.finished_out.digest_len;
XiaokangQian0fa66432021-11-15 03:33:57 +00001206 /*
1207 * struct {
1208 * opaque verify_data[Hash.length];
1209 * } Finished;
1210 */
XiaokangQian8773aa02021-11-10 07:33:09 +00001211 MBEDTLS_SSL_CHK_BUF_PTR( buf, end, verify_data_len );
XiaokangQian74af2a82021-09-22 07:40:30 +00001212
1213 memcpy( buf, ssl->handshake->state_local.finished_out.digest,
XiaokangQian8773aa02021-11-10 07:33:09 +00001214 verify_data_len );
XiaokangQian74af2a82021-09-22 07:40:30 +00001215
Xiaofei Baid25fab62021-12-02 06:36:27 +00001216 *out_len = verify_data_len;
XiaokangQian74af2a82021-09-22 07:40:30 +00001217 return( 0 );
1218}
XiaokangQianc5c39d52021-11-09 11:55:10 +00001219
XiaokangQian35dc6252021-11-11 08:16:19 +00001220/* Main entry point: orchestrates the other functions */
1221int mbedtls_ssl_tls13_write_finished_message( mbedtls_ssl_context *ssl )
1222{
1223 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1224 unsigned char *buf;
1225 size_t buf_len, msg_len;
1226
1227 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write finished message" ) );
1228
XiaokangQiandce82242021-11-15 06:01:26 +00001229 MBEDTLS_SSL_PROC_CHK( ssl_tls13_prepare_finished_message( ssl ) );
1230
Ronald Cron8f6d39a2022-03-10 18:56:50 +01001231 MBEDTLS_SSL_PROC_CHK( mbedtls_ssl_start_handshake_msg( ssl,
XiaokangQian35dc6252021-11-11 08:16:19 +00001232 MBEDTLS_SSL_HS_FINISHED, &buf, &buf_len ) );
1233
1234 MBEDTLS_SSL_PROC_CHK( ssl_tls13_write_finished_message_body(
1235 ssl, buf, buf + buf_len, &msg_len ) );
1236
Ronald Cron8f6d39a2022-03-10 18:56:50 +01001237 mbedtls_ssl_add_hs_msg_to_checksum( ssl, MBEDTLS_SSL_HS_FINISHED,
1238 buf, msg_len );
XiaokangQian35dc6252021-11-11 08:16:19 +00001239
Ronald Cron8f6d39a2022-03-10 18:56:50 +01001240 MBEDTLS_SSL_PROC_CHK( mbedtls_ssl_finish_handshake_msg(
1241 ssl, buf_len, msg_len ) );
XiaokangQian35dc6252021-11-11 08:16:19 +00001242cleanup:
1243
1244 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write finished message" ) );
1245 return( ret );
1246}
1247
Jerry Yu378254d2021-10-30 21:44:47 +08001248void mbedtls_ssl_tls13_handshake_wrapup( mbedtls_ssl_context *ssl )
1249{
1250
1251 MBEDTLS_SSL_DEBUG_MSG( 3, ( "=> handshake wrapup" ) );
1252
Jerry Yue8c1fca2022-05-18 14:48:56 +08001253 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Switch to application keys for inbound traffic" ) );
1254 mbedtls_ssl_set_inbound_transform ( ssl, ssl->transform_application );
1255
1256 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Switch to application keys for outbound traffic" ) );
1257 mbedtls_ssl_set_outbound_transform( ssl, ssl->transform_application );
1258
Jerry Yu378254d2021-10-30 21:44:47 +08001259 /*
Jerry Yucfe64f02021-11-15 13:54:06 +08001260 * Free the previous session and switch to the current one.
Jerry Yu378254d2021-10-30 21:44:47 +08001261 */
1262 if( ssl->session )
1263 {
Jerry Yu378254d2021-10-30 21:44:47 +08001264 mbedtls_ssl_session_free( ssl->session );
1265 mbedtls_free( ssl->session );
1266 }
1267 ssl->session = ssl->session_negotiate;
1268 ssl->session_negotiate = NULL;
1269
1270 MBEDTLS_SSL_DEBUG_MSG( 3, ( "<= handshake wrapup" ) );
1271}
1272
Ronald Cron49ad6192021-11-24 16:25:31 +01001273/*
1274 *
1275 * STATE HANDLING: Write ChangeCipherSpec
1276 *
1277 */
1278#if defined(MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE)
Ronald Cron49ad6192021-11-24 16:25:31 +01001279static int ssl_tls13_write_change_cipher_spec_body( mbedtls_ssl_context *ssl,
1280 unsigned char *buf,
1281 unsigned char *end,
1282 size_t *olen )
1283{
1284 ((void) ssl);
1285
1286 MBEDTLS_SSL_CHK_BUF_PTR( buf, end, 1 );
1287 buf[0] = 1;
1288 *olen = 1;
1289
1290 return( 0 );
1291}
1292
Ronald Cron49ad6192021-11-24 16:25:31 +01001293int mbedtls_ssl_tls13_write_change_cipher_spec( mbedtls_ssl_context *ssl )
1294{
1295 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1296
1297 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write change cipher spec" ) );
1298
Ronald Cron49ad6192021-11-24 16:25:31 +01001299 /* Write CCS message */
1300 MBEDTLS_SSL_PROC_CHK( ssl_tls13_write_change_cipher_spec_body(
1301 ssl, ssl->out_msg,
1302 ssl->out_msg + MBEDTLS_SSL_OUT_CONTENT_LEN,
1303 &ssl->out_msglen ) );
1304
1305 ssl->out_msgtype = MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC;
1306
Ronald Cron49ad6192021-11-24 16:25:31 +01001307 /* Dispatch message */
Ronald Cron66dbf912022-02-02 15:33:46 +01001308 MBEDTLS_SSL_PROC_CHK( mbedtls_ssl_write_record( ssl, 0 ) );
Ronald Cron49ad6192021-11-24 16:25:31 +01001309
1310cleanup:
1311
1312 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write change cipher spec" ) );
1313 return( ret );
1314}
1315
1316#endif /* MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE */
1317
XiaokangQian78b1fa72022-01-19 06:56:30 +00001318/* Reset SSL context and update hash for handling HRR.
1319 *
1320 * Replace Transcript-Hash(X) by
1321 * Transcript-Hash( message_hash ||
1322 * 00 00 Hash.length ||
1323 * X )
1324 * A few states of the handshake are preserved, including:
1325 * - session ID
1326 * - session ticket
1327 * - negotiated ciphersuite
1328 */
1329int mbedtls_ssl_reset_transcript_for_hrr( mbedtls_ssl_context *ssl )
1330{
1331 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1332 unsigned char hash_transcript[ MBEDTLS_MD_MAX_SIZE + 4 ];
XiaokangQian0ece9982022-01-24 08:56:23 +00001333 size_t hash_len;
XiaokangQian78b1fa72022-01-19 06:56:30 +00001334 const mbedtls_ssl_ciphersuite_t *ciphersuite_info;
1335 uint16_t cipher_suite = ssl->session_negotiate->ciphersuite;
1336 ciphersuite_info = mbedtls_ssl_ciphersuite_from_id( cipher_suite );
1337
1338 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Reset SSL session for HRR" ) );
1339
XiaokangQian0ece9982022-01-24 08:56:23 +00001340 ret = mbedtls_ssl_get_handshake_transcript( ssl, ciphersuite_info->mac,
1341 hash_transcript + 4,
1342 MBEDTLS_MD_MAX_SIZE,
1343 &hash_len );
1344 if( ret != 0 )
1345 {
1346 MBEDTLS_SSL_DEBUG_RET( 4, "mbedtls_ssl_get_handshake_transcript", ret );
1347 return( ret );
1348 }
1349
1350 hash_transcript[0] = MBEDTLS_SSL_HS_MESSAGE_HASH;
1351 hash_transcript[1] = 0;
1352 hash_transcript[2] = 0;
1353 hash_transcript[3] = (unsigned char) hash_len;
1354
1355 hash_len += 4;
1356
XiaokangQian78b1fa72022-01-19 06:56:30 +00001357 if( ciphersuite_info->mac == MBEDTLS_MD_SHA256 )
1358 {
1359#if defined(MBEDTLS_SHA256_C)
XiaokangQian78b1fa72022-01-19 06:56:30 +00001360 MBEDTLS_SSL_DEBUG_BUF( 4, "Truncated SHA-256 handshake transcript",
XiaokangQian0ece9982022-01-24 08:56:23 +00001361 hash_transcript, hash_len );
XiaokangQian78b1fa72022-01-19 06:56:30 +00001362
1363#if defined(MBEDTLS_USE_PSA_CRYPTO)
1364 psa_hash_abort( &ssl->handshake->fin_sha256_psa );
1365 psa_hash_setup( &ssl->handshake->fin_sha256_psa, PSA_ALG_SHA_256 );
1366#else
1367 mbedtls_sha256_starts( &ssl->handshake->fin_sha256, 0 );
1368#endif
XiaokangQian78b1fa72022-01-19 06:56:30 +00001369#endif /* MBEDTLS_SHA256_C */
1370 }
1371 else if( ciphersuite_info->mac == MBEDTLS_MD_SHA384 )
1372 {
1373#if defined(MBEDTLS_SHA384_C)
XiaokangQian78b1fa72022-01-19 06:56:30 +00001374 MBEDTLS_SSL_DEBUG_BUF( 4, "Truncated SHA-384 handshake transcript",
XiaokangQian0ece9982022-01-24 08:56:23 +00001375 hash_transcript, hash_len );
XiaokangQian78b1fa72022-01-19 06:56:30 +00001376
1377#if defined(MBEDTLS_USE_PSA_CRYPTO)
1378 psa_hash_abort( &ssl->handshake->fin_sha384_psa );
1379 psa_hash_setup( &ssl->handshake->fin_sha384_psa, PSA_ALG_SHA_384 );
1380#else
1381 mbedtls_sha512_starts( &ssl->handshake->fin_sha512, 1 );
1382#endif
XiaokangQian78b1fa72022-01-19 06:56:30 +00001383#endif /* MBEDTLS_SHA384_C */
1384 }
1385
XiaokangQian0ece9982022-01-24 08:56:23 +00001386#if defined(MBEDTLS_SHA256_C) || defined(MBEDTLS_SHA384_C)
1387 ssl->handshake->update_checksum( ssl, hash_transcript, hash_len );
1388#endif /* MBEDTLS_SHA256_C || MBEDTLS_SHA384_C */
Przemyslaw Stekiel4b3fff42022-02-14 16:39:52 +01001389
XiaokangQian78b1fa72022-01-19 06:56:30 +00001390 return( ret );
1391}
1392
XiaokangQian9b5d04b2022-04-10 10:20:43 +00001393#if defined(MBEDTLS_ECDH_C)
XiaokangQian7807f9f2022-02-15 10:04:37 +00001394
XiaokangQian9b5d04b2022-04-10 10:20:43 +00001395int mbedtls_ssl_tls13_read_public_ecdhe_share( mbedtls_ssl_context *ssl,
1396 const unsigned char *buf,
1397 size_t buf_len )
XiaokangQian7807f9f2022-02-15 10:04:37 +00001398{
XiaokangQian9b5d04b2022-04-10 10:20:43 +00001399 uint8_t *p = (uint8_t*)buf;
XiaokangQiancfd925f2022-04-14 07:10:37 +00001400 const uint8_t *end = buf + buf_len;
XiaokangQian9b5d04b2022-04-10 10:20:43 +00001401 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001402
XiaokangQian9b5d04b2022-04-10 10:20:43 +00001403 /* Get size of the TLS opaque key_exchange field of the KeyShareEntry struct. */
Ronald Cron154d1b62022-06-01 15:33:26 +02001404 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 2 );
XiaokangQian9b5d04b2022-04-10 10:20:43 +00001405 uint16_t peerkey_len = MBEDTLS_GET_UINT16_BE( p, 0 );
1406 p += 2;
XiaokangQian3207a322022-02-23 03:15:27 +00001407
XiaokangQian9b5d04b2022-04-10 10:20:43 +00001408 /* Check if key size is consistent with given buffer length. */
Ronald Cron154d1b62022-06-01 15:33:26 +02001409 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, peerkey_len );
XiaokangQian9b5d04b2022-04-10 10:20:43 +00001410
1411 /* Store peer's ECDH public key. */
1412 memcpy( handshake->ecdh_psa_peerkey, p, peerkey_len );
1413 handshake->ecdh_psa_peerkey_len = peerkey_len;
1414
XiaokangQian3207a322022-02-23 03:15:27 +00001415 return( 0 );
1416}
Jerry Yu89e103c2022-03-30 22:43:29 +08001417
1418int mbedtls_ssl_tls13_generate_and_write_ecdh_key_exchange(
1419 mbedtls_ssl_context *ssl,
1420 uint16_t named_group,
1421 unsigned char *buf,
1422 unsigned char *end,
1423 size_t *out_len )
1424{
1425 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
1426 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
1427 psa_key_attributes_t key_attributes;
1428 size_t own_pubkey_len;
1429 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
1430 size_t ecdh_bits = 0;
1431
1432 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Perform PSA-based ECDH computation." ) );
1433
1434 /* Convert EC group to PSA key type. */
1435 if( ( handshake->ecdh_psa_type =
1436 mbedtls_psa_parse_tls_ecc_group( named_group, &ecdh_bits ) ) == 0 )
1437 return( MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
1438
1439 ssl->handshake->ecdh_bits = ecdh_bits;
1440
1441 key_attributes = psa_key_attributes_init();
1442 psa_set_key_usage_flags( &key_attributes, PSA_KEY_USAGE_DERIVE );
1443 psa_set_key_algorithm( &key_attributes, PSA_ALG_ECDH );
1444 psa_set_key_type( &key_attributes, handshake->ecdh_psa_type );
1445 psa_set_key_bits( &key_attributes, handshake->ecdh_bits );
1446
1447 /* Generate ECDH private key. */
1448 status = psa_generate_key( &key_attributes,
1449 &handshake->ecdh_psa_privkey );
1450 if( status != PSA_SUCCESS )
1451 {
1452 ret = psa_ssl_status_to_mbedtls( status );
1453 MBEDTLS_SSL_DEBUG_RET( 1, "psa_generate_key", ret );
1454 return( ret );
1455
1456 }
1457
1458 /* Export the public part of the ECDH private key from PSA. */
1459 status = psa_export_public_key( handshake->ecdh_psa_privkey,
1460 buf, (size_t)( end - buf ),
1461 &own_pubkey_len );
1462 if( status != PSA_SUCCESS )
1463 {
1464 ret = psa_ssl_status_to_mbedtls( status );
1465 MBEDTLS_SSL_DEBUG_RET( 1, "psa_export_public_key", ret );
1466 return( ret );
1467
1468 }
1469
1470 *out_len = own_pubkey_len;
1471
1472 return( 0 );
1473}
XiaokangQian9b5d04b2022-04-10 10:20:43 +00001474#endif /* MBEDTLS_ECDH_C */
XiaokangQian7807f9f2022-02-15 10:04:37 +00001475
Jerry Yufb4b6472022-01-27 15:03:26 +08001476#endif /* MBEDTLS_SSL_TLS_C && MBEDTLS_SSL_PROTO_TLS1_3 */