blob: ef27644ec66a0d648d298e58a543f92ece03e918 [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
Jerry Yufbe3e642022-04-25 19:31:51 +080037const uint8_t mbedtls_ssl_tls13_hello_retry_request_magic[
38 MBEDTLS_SERVER_HELLO_RANDOM_LEN ] =
39 { 0xCF, 0x21, 0xAD, 0x74, 0xE5, 0x9A, 0x61, 0x11,
40 0xBE, 0x1D, 0x8C, 0x02, 0x1E, 0x65, 0xB8, 0x91,
41 0xC2, 0xA2, 0x11, 0x16, 0x7A, 0xBB, 0x8C, 0x5E,
42 0x07, 0x9E, 0x09, 0xE2, 0xC8, 0xA8, 0x33, 0x9C };
Jerry Yu93a13f22022-04-11 23:00:01 +080043
Xiaofei Bai746f9482021-11-12 08:53:56 +000044int mbedtls_ssl_tls13_fetch_handshake_msg( mbedtls_ssl_context *ssl,
45 unsigned hs_type,
46 unsigned char **buf,
Xiaofei Baieef15042021-11-18 07:29:56 +000047 size_t *buf_len )
XiaokangQian6b226b02021-09-24 07:51:16 +000048{
49 int ret;
50
51 if( ( ret = mbedtls_ssl_read_record( ssl, 0 ) ) != 0 )
52 {
53 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
54 goto cleanup;
55 }
56
XiaokangQian16c61aa2021-09-27 09:30:17 +000057 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE ||
XiaokangQian6b226b02021-09-24 07:51:16 +000058 ssl->in_msg[0] != hs_type )
59 {
XiaokangQian16c61aa2021-09-27 09:30:17 +000060 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Receive unexpected handshake message." ) );
XiaokangQian6b226b02021-09-24 07:51:16 +000061 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE,
XiaokangQian05420b12021-09-29 08:46:37 +000062 MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
XiaokangQian6b226b02021-09-24 07:51:16 +000063 ret = MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE;
64 goto cleanup;
65 }
66
XiaokangQian05420b12021-09-29 08:46:37 +000067 /*
68 * Jump handshake header (4 bytes, see Section 4 of RFC 8446).
69 * ...
70 * HandshakeType msg_type;
71 * uint24 length;
72 * ...
73 */
Xiaofei Baieef15042021-11-18 07:29:56 +000074 *buf = ssl->in_msg + 4;
75 *buf_len = ssl->in_hslen - 4;
XiaokangQian6b226b02021-09-24 07:51:16 +000076
XiaokangQian6b226b02021-09-24 07:51:16 +000077cleanup:
78
79 return( ret );
80}
81
Jerry Yubc20bdd2021-08-24 15:59:48 +080082#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Jerry Yu30b071c2021-09-12 20:16:03 +080083/*
Jerry Yu30b071c2021-09-12 20:16:03 +080084 * STATE HANDLING: Read CertificateVerify
85 */
Jerry Yud0fc5852021-10-29 11:09:06 +080086/* Macro to express the maximum length of the verify structure.
Jerry Yu30b071c2021-09-12 20:16:03 +080087 *
88 * The structure is computed per TLS 1.3 specification as:
89 * - 64 bytes of octet 32,
90 * - 33 bytes for the context string
91 * (which is either "TLS 1.3, client CertificateVerify"
92 * or "TLS 1.3, server CertificateVerify"),
Jerry Yud0fc5852021-10-29 11:09:06 +080093 * - 1 byte for the octet 0x0, which serves as a separator,
Jerry Yu30b071c2021-09-12 20:16:03 +080094 * - 32 or 48 bytes for the Transcript-Hash(Handshake Context, Certificate)
95 * (depending on the size of the transcript_hash)
96 *
97 * This results in a total size of
98 * - 130 bytes for a SHA256-based transcript hash, or
99 * (64 + 33 + 1 + 32 bytes)
100 * - 146 bytes for a SHA384-based transcript hash.
101 * (64 + 33 + 1 + 48 bytes)
102 *
103 */
Jerry Yu26c2d112021-10-25 12:42:58 +0800104#define SSL_VERIFY_STRUCT_MAX_SIZE ( 64 + \
105 33 + \
106 1 + \
107 MBEDTLS_TLS1_3_MD_MAX_SIZE \
Jerry Yu30b071c2021-09-12 20:16:03 +0800108 )
Jerry Yu30b071c2021-09-12 20:16:03 +0800109
Jerry Yu0b32c502021-10-28 13:41:59 +0800110/*
111 * The ssl_tls13_create_verify_structure() creates the verify structure.
112 * As input, it requires the transcript hash.
113 *
114 * The caller has to ensure that the buffer has size at least
115 * SSL_VERIFY_STRUCT_MAX_SIZE bytes.
116 */
Jerry Yud0fc5852021-10-29 11:09:06 +0800117static void ssl_tls13_create_verify_structure( const unsigned char *transcript_hash,
Jerry Yu0b32c502021-10-28 13:41:59 +0800118 size_t transcript_hash_len,
119 unsigned char *verify_buffer,
120 size_t *verify_buffer_len,
121 int from )
122{
123 size_t idx;
Jerry Yu30b071c2021-09-12 20:16:03 +0800124
Jerry Yu0b32c502021-10-28 13:41:59 +0800125 /* RFC 8446, Section 4.4.3:
126 *
127 * The digital signature [in the CertificateVerify message] is then
128 * computed over the concatenation of:
129 * - A string that consists of octet 32 (0x20) repeated 64 times
130 * - The context string
131 * - A single 0 byte which serves as the separator
132 * - The content to be signed
133 */
134 memset( verify_buffer, 0x20, 64 );
135 idx = 64;
136
137 if( from == MBEDTLS_SSL_IS_CLIENT )
138 {
139 memcpy( verify_buffer + idx, MBEDTLS_SSL_TLS1_3_LBL_WITH_LEN( client_cv ) );
140 idx += MBEDTLS_SSL_TLS1_3_LBL_LEN( client_cv );
141 }
142 else
143 { /* from == MBEDTLS_SSL_IS_SERVER */
144 memcpy( verify_buffer + idx, MBEDTLS_SSL_TLS1_3_LBL_WITH_LEN( server_cv ) );
145 idx += MBEDTLS_SSL_TLS1_3_LBL_LEN( server_cv );
146 }
147
148 verify_buffer[idx++] = 0x0;
149
150 memcpy( verify_buffer + idx, transcript_hash, transcript_hash_len );
151 idx += transcript_hash_len;
152
153 *verify_buffer_len = idx;
154}
155
Jerry Yu0b32c502021-10-28 13:41:59 +0800156static int ssl_tls13_parse_certificate_verify( mbedtls_ssl_context *ssl,
Jerry Yud0fc5852021-10-29 11:09:06 +0800157 const unsigned char *buf,
158 const unsigned char *end,
159 const unsigned char *verify_buffer,
160 size_t verify_buffer_len )
Jerry Yu30b071c2021-09-12 20:16:03 +0800161{
162 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
163 const unsigned char *p = buf;
164 uint16_t algorithm;
Jerry Yu30b071c2021-09-12 20:16:03 +0800165 size_t signature_len;
166 mbedtls_pk_type_t sig_alg;
167 mbedtls_md_type_t md_alg;
Jerry Yud0fc5852021-10-29 11:09:06 +0800168 unsigned char verify_hash[MBEDTLS_MD_MAX_SIZE];
Jerry Yu30b071c2021-09-12 20:16:03 +0800169 size_t verify_hash_len;
170
Xiaofei Baid25fab62021-12-02 06:36:27 +0000171 void const *options = NULL;
XiaokangQian82d34cc2021-11-03 08:51:56 +0000172#if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT)
Xiaofei Baid25fab62021-12-02 06:36:27 +0000173 mbedtls_pk_rsassa_pss_options rsassa_pss_options;
XiaokangQian82d34cc2021-11-03 08:51:56 +0000174#endif /* MBEDTLS_X509_RSASSA_PSS_SUPPORT */
175
Jerry Yu30b071c2021-09-12 20:16:03 +0800176 /*
177 * struct {
178 * SignatureScheme algorithm;
179 * opaque signature<0..2^16-1>;
180 * } CertificateVerify;
181 */
182 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 2 );
183 algorithm = MBEDTLS_GET_UINT16_BE( p, 0 );
184 p += 2;
185
186 /* RFC 8446 section 4.4.3
187 *
188 * If the CertificateVerify message is sent by a server, the signature algorithm
189 * MUST be one offered in the client's "signature_algorithms" extension unless
190 * no valid certificate chain can be produced without unsupported algorithms
191 *
192 * RFC 8446 section 4.4.2.2
193 *
194 * If the client cannot construct an acceptable chain using the provided
195 * certificates and decides to abort the handshake, then it MUST abort the handshake
196 * with an appropriate certificate-related alert (by default, "unsupported_certificate").
197 *
Jerry Yu6f87f252021-10-29 20:12:51 +0800198 * Check if algorithm is an offered signature algorithm.
Jerry Yu30b071c2021-09-12 20:16:03 +0800199 */
Jerry Yu24811fb2022-01-19 18:02:15 +0800200 if( ! mbedtls_ssl_sig_alg_is_offered( ssl, algorithm ) )
Jerry Yu30b071c2021-09-12 20:16:03 +0800201 {
Jerry Yu982d9e52021-10-14 15:59:37 +0800202 /* algorithm not in offered signature algorithms list */
203 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Received signature algorithm(%04x) is not "
204 "offered.",
205 ( unsigned int ) algorithm ) );
Jerry Yu6f87f252021-10-29 20:12:51 +0800206 goto error;
Jerry Yu30b071c2021-09-12 20:16:03 +0800207 }
208
Jerry Yu8c338862022-03-23 13:34:04 +0800209 if( mbedtls_ssl_tls13_get_pk_type_and_md_alg_from_sig_alg(
Jerry Yuf8aa9a42022-03-23 20:40:28 +0800210 algorithm, &sig_alg, &md_alg ) != 0 )
Jerry Yu30b071c2021-09-12 20:16:03 +0800211 {
Jerry Yu8c338862022-03-23 13:34:04 +0800212 goto error;
Jerry Yu30b071c2021-09-12 20:16:03 +0800213 }
214
215 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Certificate Verify: Signature algorithm ( %04x )",
216 ( unsigned int ) algorithm ) );
217
218 /*
219 * Check the certificate's key type matches the signature alg
220 */
221 if( !mbedtls_pk_can_do( &ssl->session_negotiate->peer_cert->pk, sig_alg ) )
222 {
223 MBEDTLS_SSL_DEBUG_MSG( 1, ( "signature algorithm doesn't match cert key" ) );
Jerry Yu6f87f252021-10-29 20:12:51 +0800224 goto error;
Jerry Yu30b071c2021-09-12 20:16:03 +0800225 }
226
227 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 2 );
228 signature_len = MBEDTLS_GET_UINT16_BE( p, 0 );
229 p += 2;
230 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, signature_len );
231
232 /* Hash verify buffer with indicated hash function */
233 switch( md_alg )
234 {
235#if defined(MBEDTLS_SHA256_C)
236 case MBEDTLS_MD_SHA256:
237 verify_hash_len = 32;
Jerry Yu133690c2021-10-25 14:01:13 +0800238 ret = mbedtls_sha256( verify_buffer, verify_buffer_len, verify_hash, 0 );
Jerry Yu30b071c2021-09-12 20:16:03 +0800239 break;
240#endif /* MBEDTLS_SHA256_C */
241
242#if defined(MBEDTLS_SHA384_C)
243 case MBEDTLS_MD_SHA384:
244 verify_hash_len = 48;
Jerry Yu133690c2021-10-25 14:01:13 +0800245 ret = mbedtls_sha512( verify_buffer, verify_buffer_len, verify_hash, 1 );
Jerry Yu30b071c2021-09-12 20:16:03 +0800246 break;
247#endif /* MBEDTLS_SHA384_C */
248
249#if defined(MBEDTLS_SHA512_C)
250 case MBEDTLS_MD_SHA512:
251 verify_hash_len = 64;
Jerry Yu133690c2021-10-25 14:01:13 +0800252 ret = mbedtls_sha512( verify_buffer, verify_buffer_len, verify_hash, 0 );
Jerry Yu30b071c2021-09-12 20:16:03 +0800253 break;
254#endif /* MBEDTLS_SHA512_C */
255
Jerry Yu0b32c502021-10-28 13:41:59 +0800256 default:
257 ret = MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
258 break;
Jerry Yu30b071c2021-09-12 20:16:03 +0800259 }
260
Jerry Yu133690c2021-10-25 14:01:13 +0800261 if( ret != 0 )
262 {
263 MBEDTLS_SSL_DEBUG_RET( 1, "hash computation error", ret );
Jerry Yu6f87f252021-10-29 20:12:51 +0800264 goto error;
Jerry Yu133690c2021-10-25 14:01:13 +0800265 }
266
Jerry Yu30b071c2021-09-12 20:16:03 +0800267 MBEDTLS_SSL_DEBUG_BUF( 3, "verify hash", verify_hash, verify_hash_len );
XiaokangQian82d34cc2021-11-03 08:51:56 +0000268#if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT)
269 if( sig_alg == MBEDTLS_PK_RSASSA_PSS )
270 {
271 const mbedtls_md_info_t* md_info;
Xiaofei Baid25fab62021-12-02 06:36:27 +0000272 rsassa_pss_options.mgf1_hash_id = md_alg;
XiaokangQian82d34cc2021-11-03 08:51:56 +0000273 if( ( md_info = mbedtls_md_info_from_type( md_alg ) ) == NULL )
274 {
275 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
276 }
Xiaofei Baid25fab62021-12-02 06:36:27 +0000277 rsassa_pss_options.expected_salt_len = mbedtls_md_get_size( md_info );
278 options = (const void*) &rsassa_pss_options;
XiaokangQian82d34cc2021-11-03 08:51:56 +0000279 }
280#endif /* MBEDTLS_X509_RSASSA_PSS_SUPPORT */
Jerry Yu30b071c2021-09-12 20:16:03 +0800281
Xiaofei Baid25fab62021-12-02 06:36:27 +0000282 if( ( ret = mbedtls_pk_verify_ext( sig_alg, options,
Jerry Yu30b071c2021-09-12 20:16:03 +0800283 &ssl->session_negotiate->peer_cert->pk,
284 md_alg, verify_hash, verify_hash_len,
Jerry Yu6f87f252021-10-29 20:12:51 +0800285 p, signature_len ) ) == 0 )
Jerry Yu30b071c2021-09-12 20:16:03 +0800286 {
Jerry Yu6f87f252021-10-29 20:12:51 +0800287 return( 0 );
Jerry Yu30b071c2021-09-12 20:16:03 +0800288 }
Jerry Yu6f87f252021-10-29 20:12:51 +0800289 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_pk_verify_ext", ret );
Jerry Yu30b071c2021-09-12 20:16:03 +0800290
Jerry Yu6f87f252021-10-29 20:12:51 +0800291error:
292 /* RFC 8446 section 4.4.3
293 *
294 * If the verification fails, the receiver MUST terminate the handshake
295 * with a "decrypt_error" alert.
296 */
297 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_DECRYPT_ERROR,
298 MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
299 return( MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
300
Jerry Yu30b071c2021-09-12 20:16:03 +0800301}
302#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
303
304int mbedtls_ssl_tls13_process_certificate_verify( mbedtls_ssl_context *ssl )
305{
Jerry Yu30b071c2021-09-12 20:16:03 +0800306
Jerry Yuda8cdf22021-10-25 15:06:49 +0800307#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
308 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
309 unsigned char verify_buffer[SSL_VERIFY_STRUCT_MAX_SIZE];
310 size_t verify_buffer_len;
311 unsigned char transcript[MBEDTLS_TLS1_3_MD_MAX_SIZE];
312 size_t transcript_len;
313 unsigned char *buf;
314 size_t buf_len;
315
Jerry Yu30b071c2021-09-12 20:16:03 +0800316 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate verify" ) );
317
Jerry Yuda8cdf22021-10-25 15:06:49 +0800318 MBEDTLS_SSL_PROC_CHK(
Xiaofei Bai746f9482021-11-12 08:53:56 +0000319 mbedtls_ssl_tls13_fetch_handshake_msg( ssl,
Jerry Yuda8cdf22021-10-25 15:06:49 +0800320 MBEDTLS_SSL_HS_CERTIFICATE_VERIFY, &buf, &buf_len ) );
Jerry Yu30b071c2021-09-12 20:16:03 +0800321
Jerry Yuda8cdf22021-10-25 15:06:49 +0800322 /* Need to calculate the hash of the transcript first
Jerry Yu0b32c502021-10-28 13:41:59 +0800323 * before reading the message since otherwise it gets
324 * included in the transcript
325 */
Jerry Yuda8cdf22021-10-25 15:06:49 +0800326 ret = mbedtls_ssl_get_handshake_transcript( ssl,
327 ssl->handshake->ciphersuite_info->mac,
328 transcript, sizeof( transcript ),
329 &transcript_len );
330 if( ret != 0 )
Jerry Yu30b071c2021-09-12 20:16:03 +0800331 {
Jerry Yuda8cdf22021-10-25 15:06:49 +0800332 MBEDTLS_SSL_PEND_FATAL_ALERT(
333 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR,
334 MBEDTLS_ERR_SSL_INTERNAL_ERROR );
335 return( ret );
Jerry Yu30b071c2021-09-12 20:16:03 +0800336 }
337
Jerry Yuda8cdf22021-10-25 15:06:49 +0800338 MBEDTLS_SSL_DEBUG_BUF( 3, "handshake hash", transcript, transcript_len );
339
340 /* Create verify structure */
341 ssl_tls13_create_verify_structure( transcript,
Jerry Yu0b32c502021-10-28 13:41:59 +0800342 transcript_len,
343 verify_buffer,
344 &verify_buffer_len,
345 ( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT ) ?
346 MBEDTLS_SSL_IS_SERVER :
347 MBEDTLS_SSL_IS_CLIENT );
Jerry Yuda8cdf22021-10-25 15:06:49 +0800348
349 /* Process the message contents */
Jerry Yu0b32c502021-10-28 13:41:59 +0800350 MBEDTLS_SSL_PROC_CHK( ssl_tls13_parse_certificate_verify( ssl, buf,
351 buf + buf_len, verify_buffer, verify_buffer_len ) );
Jerry Yuda8cdf22021-10-25 15:06:49 +0800352
Ronald Cron8f6d39a2022-03-10 18:56:50 +0100353 mbedtls_ssl_add_hs_msg_to_checksum( ssl, MBEDTLS_SSL_HS_CERTIFICATE_VERIFY,
354 buf, buf_len );
Jerry Yu30b071c2021-09-12 20:16:03 +0800355
356cleanup:
357
358 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse certificate verify" ) );
Jerry Yu5398c102021-11-05 13:32:38 +0800359 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_tls13_process_certificate_verify", ret );
Jerry Yu30b071c2021-09-12 20:16:03 +0800360 return( ret );
Jerry Yuda8cdf22021-10-25 15:06:49 +0800361#else
362 ((void) ssl);
363 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
364 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
365#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
Jerry Yu30b071c2021-09-12 20:16:03 +0800366}
367
368/*
Xiaofei Bai947571e2021-09-29 09:12:03 +0000369 *
XiaokangQian6b916b12022-04-25 07:29:34 +0000370 * STATE HANDLING: Incoming Certificate.
Xiaofei Bai947571e2021-09-29 09:12:03 +0000371 *
372 */
373
Xiaofei Bai947571e2021-09-29 09:12:03 +0000374#if defined(MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED)
375#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
376/*
377 * Structure of Certificate message:
378 *
379 * enum {
380 * X509(0),
381 * RawPublicKey(2),
382 * (255)
383 * } CertificateType;
384 *
385 * struct {
386 * select (certificate_type) {
387 * case RawPublicKey:
388 * * From RFC 7250 ASN.1_subjectPublicKeyInfo *
389 * opaque ASN1_subjectPublicKeyInfo<1..2^24-1>;
390 * case X509:
391 * opaque cert_data<1..2^24-1>;
392 * };
393 * Extension extensions<0..2^16-1>;
394 * } CertificateEntry;
395 *
396 * struct {
397 * opaque certificate_request_context<0..2^8-1>;
398 * CertificateEntry certificate_list<0..2^24-1>;
399 * } Certificate;
400 *
401 */
Xiaofei Bai79595ac2021-10-26 07:16:45 +0000402
403/* Parse certificate chain send by the server. */
Xiaofei Bai947571e2021-09-29 09:12:03 +0000404static int ssl_tls13_parse_certificate( mbedtls_ssl_context *ssl,
405 const unsigned char *buf,
406 const unsigned char *end )
407{
408 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
409 size_t certificate_request_context_len = 0;
410 size_t certificate_list_len = 0;
411 const unsigned char *p = buf;
412 const unsigned char *certificate_list_end;
413
XiaokangQian63e713e2022-05-15 04:26:57 +0000414 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 4 );
Xiaofei Bai947571e2021-09-29 09:12:03 +0000415 certificate_request_context_len = p[0];
XiaokangQian63e713e2022-05-15 04:26:57 +0000416 certificate_list_len = MBEDTLS_GET_UINT24_BE( p, 1 );
417 p += 4;
Xiaofei Bai947571e2021-09-29 09:12:03 +0000418
419 /* In theory, the certificate list can be up to 2^24 Bytes, but we don't
420 * support anything beyond 2^16 = 64K.
421 */
Xiaofei Bai79595ac2021-10-26 07:16:45 +0000422 if( ( certificate_request_context_len != 0 ) ||
423 ( certificate_list_len >= 0x10000 ) )
Xiaofei Bai947571e2021-09-29 09:12:03 +0000424 {
425 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
426 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR,
427 MBEDTLS_ERR_SSL_DECODE_ERROR );
428 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
429 }
430
431 /* In case we tried to reuse a session but it failed */
432 if( ssl->session_negotiate->peer_cert != NULL )
433 {
434 mbedtls_x509_crt_free( ssl->session_negotiate->peer_cert );
435 mbedtls_free( ssl->session_negotiate->peer_cert );
436 }
437
XiaokangQianc3017f62022-05-13 05:55:41 +0000438 if( certificate_list_len == 0 )
439 {
440 ssl->session_negotiate->peer_cert = NULL;
441 ret = 0;
442 goto exit;
443 }
444
Xiaofei Bai947571e2021-09-29 09:12:03 +0000445 if( ( ssl->session_negotiate->peer_cert =
446 mbedtls_calloc( 1, sizeof( mbedtls_x509_crt ) ) ) == NULL )
447 {
448 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc( %" MBEDTLS_PRINTF_SIZET " bytes ) failed",
449 sizeof( mbedtls_x509_crt ) ) );
450 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR,
451 MBEDTLS_ERR_SSL_ALLOC_FAILED );
452 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
453 }
454
455 mbedtls_x509_crt_init( ssl->session_negotiate->peer_cert );
456
Xiaofei Bai947571e2021-09-29 09:12:03 +0000457 certificate_list_end = p + certificate_list_len;
Xiaofei Bai79595ac2021-10-26 07:16:45 +0000458 while( p < certificate_list_end )
Xiaofei Bai947571e2021-09-29 09:12:03 +0000459 {
460 size_t cert_data_len, extensions_len;
461
Xiaofei Bai79595ac2021-10-26 07:16:45 +0000462 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, certificate_list_end, 3 );
Xiaofei Baif93cbd22021-10-29 02:39:30 +0000463 cert_data_len = MBEDTLS_GET_UINT24_BE( p, 0 );
Xiaofei Bai947571e2021-09-29 09:12:03 +0000464 p += 3;
465
466 /* In theory, the CRT can be up to 2^24 Bytes, but we don't support
467 * anything beyond 2^16 = 64K. Otherwise as in the TLS 1.2 code,
468 * check that we have a minimum of 128 bytes of data, this is not
469 * clear why we need that though.
470 */
471 if( ( cert_data_len < 128 ) || ( cert_data_len >= 0x10000 ) )
Xiaofei Bai79595ac2021-10-26 07:16:45 +0000472 {
Xiaofei Bai947571e2021-09-29 09:12:03 +0000473 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad Certificate message" ) );
474 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR,
475 MBEDTLS_ERR_SSL_DECODE_ERROR );
476 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
477 }
478
Xiaofei Bai79595ac2021-10-26 07:16:45 +0000479 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, certificate_list_end, cert_data_len );
Xiaofei Bai947571e2021-09-29 09:12:03 +0000480 ret = mbedtls_x509_crt_parse_der( ssl->session_negotiate->peer_cert,
481 p, cert_data_len );
482
483 switch( ret )
484 {
485 case 0: /*ok*/
486 break;
487 case MBEDTLS_ERR_X509_UNKNOWN_SIG_ALG + MBEDTLS_ERR_OID_NOT_FOUND:
488 /* Ignore certificate with an unknown algorithm: maybe a
489 prior certificate was already trusted. */
490 break;
491
492 case MBEDTLS_ERR_X509_ALLOC_FAILED:
493 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR,
494 MBEDTLS_ERR_X509_ALLOC_FAILED );
495 MBEDTLS_SSL_DEBUG_RET( 1, " mbedtls_x509_crt_parse_der", ret );
496 return( ret );
497
498 case MBEDTLS_ERR_X509_UNKNOWN_VERSION:
499 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT,
500 MBEDTLS_ERR_X509_UNKNOWN_VERSION );
501 MBEDTLS_SSL_DEBUG_RET( 1, " mbedtls_x509_crt_parse_der", ret );
502 return( ret );
503
504 default:
505 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_BAD_CERT,
506 ret );
507 MBEDTLS_SSL_DEBUG_RET( 1, " mbedtls_x509_crt_parse_der", ret );
508 return( ret );
509 }
510
511 p += cert_data_len;
512
513 /* Certificate extensions length */
514 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, certificate_list_end, 2 );
515 extensions_len = MBEDTLS_GET_UINT16_BE( p, 0 );
516 p += 2;
Xiaofei Bai79595ac2021-10-26 07:16:45 +0000517 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, certificate_list_end, extensions_len );
Xiaofei Bai947571e2021-09-29 09:12:03 +0000518 p += extensions_len;
519 }
520
XiaokangQian63e713e2022-05-15 04:26:57 +0000521exit:
Xiaofei Bai947571e2021-09-29 09:12:03 +0000522 /* Check that all the message is consumed. */
523 if( p != end )
524 {
525 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad Certificate message" ) );
526 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR, \
527 MBEDTLS_ERR_SSL_DECODE_ERROR );
528 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
529 }
530
531 MBEDTLS_SSL_DEBUG_CRT( 3, "peer certificate", ssl->session_negotiate->peer_cert );
532
533 return( ret );
534}
535#else
536static int ssl_tls13_parse_certificate( mbedtls_ssl_context *ssl,
537 const unsigned char *buf,
538 const unsigned char *end )
539{
540 ((void) ssl);
541 ((void) buf);
542 ((void) end);
543 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
544}
545#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
546#endif /* MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED */
547
548#if defined(MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED)
549#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Xiaofei Bai79595ac2021-10-26 07:16:45 +0000550/* Validate certificate chain sent by the server. */
Xiaofei Bai947571e2021-09-29 09:12:03 +0000551static int ssl_tls13_validate_certificate( mbedtls_ssl_context *ssl )
552{
553 int ret = 0;
XiaokangQian6b916b12022-04-25 07:29:34 +0000554 int authmode = ssl->conf->authmode;
Xiaofei Bai947571e2021-09-29 09:12:03 +0000555 mbedtls_x509_crt *ca_chain;
556 mbedtls_x509_crl *ca_crl;
Xiaofei Baiff456022021-10-28 06:50:17 +0000557 uint32_t verify_result = 0;
Xiaofei Bai947571e2021-09-29 09:12:03 +0000558
XiaokangQian6b916b12022-04-25 07:29:34 +0000559 /* If SNI was used, overwrite authentication mode
560 * from the configuration. */
561#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
562 if( ssl->handshake->sni_authmode != MBEDTLS_SSL_VERIFY_UNSET )
563 authmode = ssl->handshake->sni_authmode;
564#endif
565
566 /*
567 * If the client hasn't sent a certificate ( i.e. it sent
568 * an empty certificate chain ), this is reflected in the peer CRT
569 * structure being unset.
570 * Check for that and handle it depending on the
571 * server's authentication mode.
572 */
XiaokangQian63e713e2022-05-15 04:26:57 +0000573 if( ssl->session_negotiate->peer_cert == NULL )
XiaokangQian6b916b12022-04-25 07:29:34 +0000574 {
XiaokangQian63e713e2022-05-15 04:26:57 +0000575#if defined(MBEDTLS_SSL_SRV_C)
576 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
577 {
578 MBEDTLS_SSL_DEBUG_MSG( 1, ( "client has no certificate" ) );
XiaokangQian6b916b12022-04-25 07:29:34 +0000579
XiaokangQian63e713e2022-05-15 04:26:57 +0000580 /* The client was asked for a certificate but didn't send
581 * one. The client should know what's going on, so we
582 * don't send an alert.
583 */
584 ssl->session_negotiate->verify_result = MBEDTLS_X509_BADCERT_MISSING;
585 if( authmode == MBEDTLS_SSL_VERIFY_OPTIONAL )
586 return( 0 );
587 else
588 return( MBEDTLS_ERR_SSL_NO_CLIENT_CERTIFICATE );
589 }
XiaokangQian6b916b12022-04-25 07:29:34 +0000590#endif /* MBEDTLS_SSL_SRV_C */
591
XiaokangQianc3017f62022-05-13 05:55:41 +0000592#if defined(MBEDTLS_SSL_CLI_C)
XiaokangQian63e713e2022-05-15 04:26:57 +0000593 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
594 {
595 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_NO_CERT,
596 MBEDTLS_ERR_SSL_FATAL_ALERT_MESSAGE );
597 return( MBEDTLS_ERR_SSL_FATAL_ALERT_MESSAGE );
598 }
XiaokangQianc3017f62022-05-13 05:55:41 +0000599#endif /* MBEDTLS_SSL_CLI_C */
XiaokangQian63e713e2022-05-15 04:26:57 +0000600 }
XiaokangQian6b916b12022-04-25 07:29:34 +0000601
Xiaofei Bai947571e2021-09-29 09:12:03 +0000602#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
603 if( ssl->handshake->sni_ca_chain != NULL )
604 {
605 ca_chain = ssl->handshake->sni_ca_chain;
606 ca_crl = ssl->handshake->sni_ca_crl;
607 }
608 else
609#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
610 {
611 ca_chain = ssl->conf->ca_chain;
612 ca_crl = ssl->conf->ca_crl;
613 }
614
615 /*
616 * Main check: verify certificate
617 */
618 ret = mbedtls_x509_crt_verify_with_profile(
619 ssl->session_negotiate->peer_cert,
620 ca_chain, ca_crl,
621 ssl->conf->cert_profile,
622 ssl->hostname,
Xiaofei Baiff456022021-10-28 06:50:17 +0000623 &verify_result,
Xiaofei Bai947571e2021-09-29 09:12:03 +0000624 ssl->conf->f_vrfy, ssl->conf->p_vrfy );
625
626 if( ret != 0 )
627 {
628 MBEDTLS_SSL_DEBUG_RET( 1, "x509_verify_cert", ret );
629 }
630
631 /*
632 * Secondary checks: always done, but change 'ret' only if it was 0
633 */
Xiaofei Bai947571e2021-09-29 09:12:03 +0000634 if( mbedtls_ssl_check_cert_usage( ssl->session_negotiate->peer_cert,
635 ssl->handshake->ciphersuite_info,
636 !ssl->conf->endpoint,
Xiaofei Baiff456022021-10-28 06:50:17 +0000637 &verify_result ) != 0 )
Xiaofei Bai947571e2021-09-29 09:12:03 +0000638 {
639 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate ( usage extensions )" ) );
640 if( ret == 0 )
641 ret = MBEDTLS_ERR_SSL_BAD_CERTIFICATE;
642 }
643
XiaokangQian6b916b12022-04-25 07:29:34 +0000644 /* mbedtls_x509_crt_verify_with_profile is supposed to report a
645 * verification failure through MBEDTLS_ERR_X509_CERT_VERIFY_FAILED,
646 * with details encoded in the verification flags. All other kinds
647 * of error codes, including those from the user provided f_vrfy
648 * functions, are treated as fatal and lead to a failure of
649 * ssl_tls13_parse_certificate even if verification was optional. */
650 if( authmode == MBEDTLS_SSL_VERIFY_OPTIONAL &&
651 ( ret == MBEDTLS_ERR_X509_CERT_VERIFY_FAILED ||
652 ret == MBEDTLS_ERR_SSL_BAD_CERTIFICATE ) )
653 {
654 ret = 0;
655 }
Xiaofei Bai947571e2021-09-29 09:12:03 +0000656
XiaokangQian6b916b12022-04-25 07:29:34 +0000657
658 if( ca_chain == NULL && authmode == MBEDTLS_SSL_VERIFY_REQUIRED )
Xiaofei Bai947571e2021-09-29 09:12:03 +0000659 {
660 MBEDTLS_SSL_DEBUG_MSG( 1, ( "got no CA chain" ) );
661 ret = MBEDTLS_ERR_SSL_CA_CHAIN_REQUIRED;
662 }
663
664 if( ret != 0 )
665 {
666 /* The certificate may have been rejected for several reasons.
667 Pick one and send the corresponding alert. Which alert to send
668 may be a subject of debate in some cases. */
Xiaofei Baiff456022021-10-28 06:50:17 +0000669 if( verify_result & MBEDTLS_X509_BADCERT_OTHER )
Xiaofei Bai947571e2021-09-29 09:12:03 +0000670 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED, ret );
Xiaofei Baiff456022021-10-28 06:50:17 +0000671 else if( verify_result & MBEDTLS_X509_BADCERT_CN_MISMATCH )
Xiaofei Bai947571e2021-09-29 09:12:03 +0000672 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_BAD_CERT, ret );
Xiaofei Baif93cbd22021-10-29 02:39:30 +0000673 else if( verify_result & ( MBEDTLS_X509_BADCERT_KEY_USAGE |
674 MBEDTLS_X509_BADCERT_EXT_KEY_USAGE |
675 MBEDTLS_X509_BADCERT_NS_CERT_TYPE |
676 MBEDTLS_X509_BADCERT_BAD_PK |
677 MBEDTLS_X509_BADCERT_BAD_KEY ) )
Xiaofei Bai947571e2021-09-29 09:12:03 +0000678 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT, ret );
Xiaofei Baiff456022021-10-28 06:50:17 +0000679 else if( verify_result & MBEDTLS_X509_BADCERT_EXPIRED )
Xiaofei Bai947571e2021-09-29 09:12:03 +0000680 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_CERT_EXPIRED, ret );
Xiaofei Baiff456022021-10-28 06:50:17 +0000681 else if( verify_result & MBEDTLS_X509_BADCERT_REVOKED )
Xiaofei Bai947571e2021-09-29 09:12:03 +0000682 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_CERT_REVOKED, ret );
Xiaofei Baiff456022021-10-28 06:50:17 +0000683 else if( verify_result & MBEDTLS_X509_BADCERT_NOT_TRUSTED )
Xiaofei Bai947571e2021-09-29 09:12:03 +0000684 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_UNKNOWN_CA, ret );
685 else
686 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_CERT_UNKNOWN, ret );
687 }
688
689#if defined(MBEDTLS_DEBUG_C)
Xiaofei Baiff456022021-10-28 06:50:17 +0000690 if( verify_result != 0 )
Xiaofei Bai947571e2021-09-29 09:12:03 +0000691 {
692 MBEDTLS_SSL_DEBUG_MSG( 3, ( "! Certificate verification flags %08x",
Jerry Yu83bb1312021-10-28 22:16:33 +0800693 (unsigned int) verify_result ) );
Xiaofei Bai947571e2021-09-29 09:12:03 +0000694 }
695 else
696 {
697 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Certificate verification flags clear" ) );
698 }
699#endif /* MBEDTLS_DEBUG_C */
700
Xiaofei Baiff456022021-10-28 06:50:17 +0000701 ssl->session_negotiate->verify_result = verify_result;
Xiaofei Bai947571e2021-09-29 09:12:03 +0000702 return( ret );
703}
704#else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
705static int ssl_tls13_validate_certificate( mbedtls_ssl_context *ssl )
706{
707 ((void) ssl);
708 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
709}
710#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
711#endif /* MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED */
712
Xiaofei Bai79595ac2021-10-26 07:16:45 +0000713int mbedtls_ssl_tls13_process_certificate( mbedtls_ssl_context *ssl )
Xiaofei Bai947571e2021-09-29 09:12:03 +0000714{
Xiaofei Bai79595ac2021-10-26 07:16:45 +0000715 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
716 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) );
717
718#if defined(MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED)
XiaokangQianc3017f62022-05-13 05:55:41 +0000719 unsigned char *buf;
720 size_t buf_len;
Xiaofei Bai79595ac2021-10-26 07:16:45 +0000721
XiaokangQianc3017f62022-05-13 05:55:41 +0000722 MBEDTLS_SSL_PROC_CHK( mbedtls_ssl_tls13_fetch_handshake_msg(
723 ssl, MBEDTLS_SSL_HS_CERTIFICATE,
724 &buf, &buf_len ) );
Xiaofei Bai79595ac2021-10-26 07:16:45 +0000725
XiaokangQianc3017f62022-05-13 05:55:41 +0000726 /* Parse the certificate chain sent by the peer. */
727 MBEDTLS_SSL_PROC_CHK( ssl_tls13_parse_certificate( ssl, buf,
728 buf + buf_len ) );
729 /* Validate the certificate chain and set the verification results. */
730 MBEDTLS_SSL_PROC_CHK( ssl_tls13_validate_certificate( ssl ) );
Xiaofei Bai79595ac2021-10-26 07:16:45 +0000731
XiaokangQianc3017f62022-05-13 05:55:41 +0000732 mbedtls_ssl_add_hs_msg_to_checksum( ssl, MBEDTLS_SSL_HS_CERTIFICATE,
733 buf, buf_len );
XiaokangQian6b916b12022-04-25 07:29:34 +0000734#endif /* MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED */
Xiaofei Bai79595ac2021-10-26 07:16:45 +0000735
Xiaofei Bai79595ac2021-10-26 07:16:45 +0000736cleanup:
737
738 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse certificate" ) );
739 return( ret );
Xiaofei Bai947571e2021-09-29 09:12:03 +0000740}
Jerry Yu90f152d2022-01-29 22:12:42 +0800741#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Jerry Yu7399d0d2022-01-30 17:54:19 +0800742/*
743 * enum {
744 * X509(0),
745 * RawPublicKey(2),
746 * (255)
747 * } CertificateType;
748 *
749 * struct {
750 * select (certificate_type) {
751 * case RawPublicKey:
752 * // From RFC 7250 ASN.1_subjectPublicKeyInfo
753 * opaque ASN1_subjectPublicKeyInfo<1..2^24-1>;
754 *
755 * case X509:
756 * opaque cert_data<1..2^24-1>;
757 * };
758 * Extension extensions<0..2^16-1>;
759 * } CertificateEntry;
760 *
761 * struct {
762 * opaque certificate_request_context<0..2^8-1>;
763 * CertificateEntry certificate_list<0..2^24-1>;
764 * } Certificate;
765 */
766static int ssl_tls13_write_certificate_body( mbedtls_ssl_context *ssl,
Jerry Yu3e536442022-02-15 11:05:59 +0800767 unsigned char *buf,
Jerry Yu7399d0d2022-01-30 17:54:19 +0800768 unsigned char *end,
Jerry Yu3e536442022-02-15 11:05:59 +0800769 size_t *out_len )
Jerry Yu5cc35062022-01-28 16:16:08 +0800770{
Jerry Yu5cc35062022-01-28 16:16:08 +0800771 const mbedtls_x509_crt *crt = mbedtls_ssl_own_cert( ssl );
Jerry Yu3e536442022-02-15 11:05:59 +0800772 unsigned char *p = buf;
Jerry Yuc8d8d4e2022-02-18 12:10:03 +0800773 unsigned char *certificate_request_context =
774 ssl->handshake->certificate_request_context;
775 unsigned char certificate_request_context_len =
776 ssl->handshake->certificate_request_context_len;
777 unsigned char *p_certificate_list_len;
Jerry Yu5cc35062022-01-28 16:16:08 +0800778
Jerry Yu5cc35062022-01-28 16:16:08 +0800779
Jerry Yu3391ac02022-02-16 11:21:37 +0800780 /* ...
781 * opaque certificate_request_context<0..2^8-1>;
782 * ...
783 */
Jerry Yuc8d8d4e2022-02-18 12:10:03 +0800784 MBEDTLS_SSL_CHK_BUF_PTR( p, end, certificate_request_context_len + 1 );
785 *p++ = certificate_request_context_len;
786 if( certificate_request_context_len > 0 )
Jerry Yu537530d2022-02-15 14:00:57 +0800787 {
Jerry Yuc8d8d4e2022-02-18 12:10:03 +0800788 memcpy( p, certificate_request_context, certificate_request_context_len );
789 p += certificate_request_context_len;
Jerry Yu537530d2022-02-15 14:00:57 +0800790 }
791
Jerry Yu3391ac02022-02-16 11:21:37 +0800792 /* ...
793 * CertificateEntry certificate_list<0..2^24-1>;
794 * ...
795 */
Jerry Yu3e536442022-02-15 11:05:59 +0800796 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 3 );
Jerry Yuc8d8d4e2022-02-18 12:10:03 +0800797 p_certificate_list_len = p;
Jerry Yu3e536442022-02-15 11:05:59 +0800798 p += 3;
799
Jerry Yu7399d0d2022-01-30 17:54:19 +0800800 MBEDTLS_SSL_DEBUG_CRT( 3, "own certificate", crt );
Jerry Yu5cc35062022-01-28 16:16:08 +0800801
Jerry Yuc8d8d4e2022-02-18 12:10:03 +0800802 while( crt != NULL )
Jerry Yu5cc35062022-01-28 16:16:08 +0800803 {
Jerry Yu7399d0d2022-01-30 17:54:19 +0800804 size_t cert_data_len = crt->raw.len;
Jerry Yu5cc35062022-01-28 16:16:08 +0800805
Jerry Yu7399d0d2022-01-30 17:54:19 +0800806 MBEDTLS_SSL_CHK_BUF_PTR( p, end, cert_data_len + 3 + 2 );
807 MBEDTLS_PUT_UINT24_BE( cert_data_len, p, 0 );
808 p += 3;
Jerry Yu5cc35062022-01-28 16:16:08 +0800809
Jerry Yu7399d0d2022-01-30 17:54:19 +0800810 memcpy( p, crt->raw.p, cert_data_len );
811 p += cert_data_len;
812 crt = crt->next;
Jerry Yu5cc35062022-01-28 16:16:08 +0800813
814 /* Currently, we don't have any certificate extensions defined.
815 * Hence, we are sending an empty extension with length zero.
816 */
Jerry Yu7399d0d2022-01-30 17:54:19 +0800817 MBEDTLS_PUT_UINT24_BE( 0, p, 0 );
818 p += 2;
Jerry Yu5cc35062022-01-28 16:16:08 +0800819 }
Jerry Yu5cc35062022-01-28 16:16:08 +0800820
Jerry Yuc8d8d4e2022-02-18 12:10:03 +0800821 MBEDTLS_PUT_UINT24_BE( p - p_certificate_list_len - 3,
822 p_certificate_list_len, 0 );
Jerry Yu7399d0d2022-01-30 17:54:19 +0800823
Jerry Yu3e536442022-02-15 11:05:59 +0800824 *out_len = p - buf;
Jerry Yu5cc35062022-01-28 16:16:08 +0800825
826 return( 0 );
827}
Jerry Yu5cc35062022-01-28 16:16:08 +0800828
Jerry Yu3e536442022-02-15 11:05:59 +0800829int mbedtls_ssl_tls13_write_certificate( mbedtls_ssl_context *ssl )
Jerry Yu5cc35062022-01-28 16:16:08 +0800830{
831 int ret;
Ronald Cron5bb8fc82022-03-09 07:00:13 +0100832 unsigned char *buf;
833 size_t buf_len, msg_len;
834
Jerry Yu5cc35062022-01-28 16:16:08 +0800835 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write certificate" ) );
836
Ronald Cron8f6d39a2022-03-10 18:56:50 +0100837 MBEDTLS_SSL_PROC_CHK( mbedtls_ssl_start_handshake_msg( ssl,
Ronald Cron5bb8fc82022-03-09 07:00:13 +0100838 MBEDTLS_SSL_HS_CERTIFICATE, &buf, &buf_len ) );
Jerry Yu5cc35062022-01-28 16:16:08 +0800839
Ronald Cron5bb8fc82022-03-09 07:00:13 +0100840 MBEDTLS_SSL_PROC_CHK( ssl_tls13_write_certificate_body( ssl,
841 buf,
842 buf + buf_len,
843 &msg_len ) );
Jerry Yu5cc35062022-01-28 16:16:08 +0800844
Ronald Cron8f6d39a2022-03-10 18:56:50 +0100845 mbedtls_ssl_add_hs_msg_to_checksum( ssl, MBEDTLS_SSL_HS_CERTIFICATE,
846 buf, msg_len );
Jerry Yu5cc35062022-01-28 16:16:08 +0800847
Ronald Cron8f6d39a2022-03-10 18:56:50 +0100848 MBEDTLS_SSL_PROC_CHK( mbedtls_ssl_finish_handshake_msg(
Ronald Cron5bb8fc82022-03-09 07:00:13 +0100849 ssl, buf_len, msg_len ) );
Jerry Yu5cc35062022-01-28 16:16:08 +0800850cleanup:
851
852 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write certificate" ) );
853 return( ret );
854}
855
Jerry Yu3e536442022-02-15 11:05:59 +0800856/*
857 * STATE HANDLING: Output Certificate Verify
858 */
Jerry Yue91a51a2022-03-22 21:42:50 +0800859static int ssl_tls13_get_sig_alg_from_pk( mbedtls_ssl_context *ssl,
860 mbedtls_pk_context *own_key,
Jerry Yu8c338862022-03-23 13:34:04 +0800861 uint16_t *algorithm )
Jerry Yu67eced02022-02-25 13:37:36 +0800862{
863 mbedtls_pk_type_t sig = mbedtls_ssl_sig_from_pk( own_key );
864 /* Determine the size of the key */
865 size_t own_key_size = mbedtls_pk_get_bitlen( own_key );
Jerry Yue91a51a2022-03-22 21:42:50 +0800866 *algorithm = MBEDTLS_TLS1_3_SIG_NONE;
Jerry Yu67eced02022-02-25 13:37:36 +0800867 ((void) own_key_size);
868
869 switch( sig )
870 {
871#if defined(MBEDTLS_ECDSA_C)
872 case MBEDTLS_SSL_SIG_ECDSA:
873 switch( own_key_size )
874 {
875 case 256:
Jerry Yue91a51a2022-03-22 21:42:50 +0800876 *algorithm = MBEDTLS_TLS1_3_SIG_ECDSA_SECP256R1_SHA256;
Jerry Yue91a51a2022-03-22 21:42:50 +0800877 return( 0 );
Jerry Yu67eced02022-02-25 13:37:36 +0800878 case 384:
Jerry Yue91a51a2022-03-22 21:42:50 +0800879 *algorithm = MBEDTLS_TLS1_3_SIG_ECDSA_SECP384R1_SHA384;
Jerry Yue91a51a2022-03-22 21:42:50 +0800880 return( 0 );
Jerry Yu67eced02022-02-25 13:37:36 +0800881 case 521:
Jerry Yue91a51a2022-03-22 21:42:50 +0800882 *algorithm = MBEDTLS_TLS1_3_SIG_ECDSA_SECP521R1_SHA512;
Jerry Yue91a51a2022-03-22 21:42:50 +0800883 return( 0 );
Jerry Yu67eced02022-02-25 13:37:36 +0800884 default:
885 MBEDTLS_SSL_DEBUG_MSG( 3,
886 ( "unknown key size: %"
887 MBEDTLS_PRINTF_SIZET " bits",
888 own_key_size ) );
889 break;
890 }
891 break;
892#endif /* MBEDTLS_ECDSA_C */
893
Jerry Yucef3f332022-03-22 23:00:13 +0800894#if defined(MBEDTLS_RSA_C)
Jerry Yu67eced02022-02-25 13:37:36 +0800895 case MBEDTLS_SSL_SIG_RSA:
Jerry Yucef3f332022-03-22 23:00:13 +0800896#if defined(MBEDTLS_PKCS1_V21)
897#if defined(MBEDTLS_SHA256_C)
Jerry Yu67eced02022-02-25 13:37:36 +0800898 if( own_key_size <= 2048 &&
899 mbedtls_ssl_sig_alg_is_received( ssl,
900 MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA256 ) )
901 {
Jerry Yue91a51a2022-03-22 21:42:50 +0800902 *algorithm = MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA256;
Jerry Yue91a51a2022-03-22 21:42:50 +0800903 return( 0 );
Jerry Yu67eced02022-02-25 13:37:36 +0800904 }
Jerry Yucef3f332022-03-22 23:00:13 +0800905 else
906#endif /* MBEDTLS_SHA256_C */
907#if defined(MBEDTLS_SHA384_C)
908 if( own_key_size <= 3072 &&
909 mbedtls_ssl_sig_alg_is_received( ssl,
Jerry Yu67eced02022-02-25 13:37:36 +0800910 MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA384 ) )
911 {
Jerry Yue91a51a2022-03-22 21:42:50 +0800912 *algorithm = MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA384;
Jerry Yue91a51a2022-03-22 21:42:50 +0800913 return( 0 );
Jerry Yu67eced02022-02-25 13:37:36 +0800914 }
Jerry Yucef3f332022-03-22 23:00:13 +0800915 else
916#endif /* MBEDTLS_SHA384_C */
917#if defined(MBEDTLS_SHA512_C)
918 if( own_key_size <= 4096 &&
919 mbedtls_ssl_sig_alg_is_received( ssl,
Jerry Yu67eced02022-02-25 13:37:36 +0800920 MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA512 ) )
921 {
Jerry Yue91a51a2022-03-22 21:42:50 +0800922 *algorithm = MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA512;
Jerry Yue91a51a2022-03-22 21:42:50 +0800923 return( 0 );
Jerry Yu67eced02022-02-25 13:37:36 +0800924 }
Jerry Yucef3f332022-03-22 23:00:13 +0800925 else
926#endif /* MBEDTLS_SHA512_C */
927#endif /* MBEDTLS_PKCS1_V21 */
928#if defined(MBEDTLS_PKCS1_V15)
929#if defined(MBEDTLS_SHA256_C)
930 if( own_key_size <= 2048 &&
931 mbedtls_ssl_sig_alg_is_received( ssl,
932 MBEDTLS_TLS1_3_SIG_RSA_PKCS1_SHA256 ) )
933 {
934 *algorithm = MBEDTLS_TLS1_3_SIG_RSA_PKCS1_SHA256;
Jerry Yucef3f332022-03-22 23:00:13 +0800935 return( 0 );
936 }
937 else
938#endif /* MBEDTLS_SHA256_C */
939#if defined(MBEDTLS_SHA384_C)
940 if( own_key_size <= 3072 &&
941 mbedtls_ssl_sig_alg_is_received( ssl,
942 MBEDTLS_TLS1_3_SIG_RSA_PKCS1_SHA384 ) )
943 {
944 *algorithm = MBEDTLS_TLS1_3_SIG_RSA_PKCS1_SHA384;
Jerry Yucef3f332022-03-22 23:00:13 +0800945 return( 0 );
946 }
947 else
948#endif /* MBEDTLS_SHA384_C */
949#if defined(MBEDTLS_SHA512_C)
950 if( own_key_size <= 4096 &&
951 mbedtls_ssl_sig_alg_is_received( ssl,
952 MBEDTLS_TLS1_3_SIG_RSA_PKCS1_SHA512 ) )
953 {
954 *algorithm = MBEDTLS_TLS1_3_SIG_RSA_PKCS1_SHA512;
Jerry Yucef3f332022-03-22 23:00:13 +0800955 return( 0 );
956 }
957 else
958#endif /* MBEDTLS_SHA512_C */
959#endif /* MBEDTLS_PKCS1_V15 */
960 {
961 MBEDTLS_SSL_DEBUG_MSG( 3,
962 ( "unknown key size: %"
963 MBEDTLS_PRINTF_SIZET " bits",
964 own_key_size ) );
965 }
Jerry Yu67eced02022-02-25 13:37:36 +0800966 break;
Jerry Yucef3f332022-03-22 23:00:13 +0800967#endif /* MBEDTLS_RSA_C */
Jerry Yu67eced02022-02-25 13:37:36 +0800968 default:
969 MBEDTLS_SSL_DEBUG_MSG( 1,
Andrzej Kurek5c65c572022-04-13 14:28:52 -0400970 ( "unknown signature type : %u", sig ) );
Jerry Yu67eced02022-02-25 13:37:36 +0800971 break;
972 }
Jerry Yue91a51a2022-03-22 21:42:50 +0800973 return( -1 );
Jerry Yu67eced02022-02-25 13:37:36 +0800974}
975
Jerry Yu3e536442022-02-15 11:05:59 +0800976static int ssl_tls13_write_certificate_verify_body( mbedtls_ssl_context *ssl,
977 unsigned char *buf,
978 unsigned char *end,
979 size_t *out_len )
Jerry Yu8511f122022-01-29 10:01:04 +0800980{
981 int ret;
Jerry Yu3e536442022-02-15 11:05:59 +0800982 unsigned char *p = buf;
Jerry Yu8511f122022-01-29 10:01:04 +0800983 mbedtls_pk_context *own_key;
Jerry Yu3e536442022-02-15 11:05:59 +0800984
Jerry Yu8511f122022-01-29 10:01:04 +0800985 unsigned char handshake_hash[ MBEDTLS_TLS1_3_MD_MAX_SIZE ];
986 size_t handshake_hash_len;
Jerry Yu3e536442022-02-15 11:05:59 +0800987 unsigned char verify_buffer[ SSL_VERIFY_STRUCT_MAX_SIZE ];
988 size_t verify_buffer_len;
Jerry Yu67eced02022-02-25 13:37:36 +0800989 mbedtls_pk_type_t pk_type = MBEDTLS_PK_NONE;
Jerry Yu67eced02022-02-25 13:37:36 +0800990 mbedtls_md_type_t md_alg = MBEDTLS_MD_NONE;
Jerry Yu78272072022-02-22 10:28:13 +0800991 uint16_t algorithm = MBEDTLS_TLS1_3_SIG_NONE;
Jerry Yu3e536442022-02-15 11:05:59 +0800992 size_t signature_len = 0;
Jerry Yu8511f122022-01-29 10:01:04 +0800993 const mbedtls_md_info_t *md_info;
Jerry Yu3391ac02022-02-16 11:21:37 +0800994 unsigned char verify_hash[ MBEDTLS_MD_MAX_SIZE ];
Jerry Yu3e536442022-02-15 11:05:59 +0800995 size_t verify_hash_len;
Jerry Yu8511f122022-01-29 10:01:04 +0800996
Jerry Yu0b7b1012022-02-23 12:23:05 +0800997 *out_len = 0;
998
Jerry Yu3e536442022-02-15 11:05:59 +0800999 own_key = mbedtls_ssl_own_key( ssl );
1000 if( own_key == NULL )
Jerry Yu8511f122022-01-29 10:01:04 +08001001 {
Jerry Yu3e536442022-02-15 11:05:59 +08001002 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1003 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Jerry Yu8511f122022-01-29 10:01:04 +08001004 }
1005
Jerry Yu8511f122022-01-29 10:01:04 +08001006 ret = mbedtls_ssl_get_handshake_transcript( ssl,
1007 ssl->handshake->ciphersuite_info->mac,
1008 handshake_hash,
1009 sizeof( handshake_hash ),
1010 &handshake_hash_len );
1011 if( ret != 0 )
1012 return( ret );
1013
1014 MBEDTLS_SSL_DEBUG_BUF( 3, "handshake hash",
1015 handshake_hash,
1016 handshake_hash_len);
1017
Jerry Yu8511f122022-01-29 10:01:04 +08001018 ssl_tls13_create_verify_structure( handshake_hash, handshake_hash_len,
1019 verify_buffer, &verify_buffer_len,
1020 ssl->conf->endpoint );
1021
1022 /*
1023 * struct {
1024 * SignatureScheme algorithm;
1025 * opaque signature<0..2^16-1>;
1026 * } CertificateVerify;
1027 */
Jerry Yu8c338862022-03-23 13:34:04 +08001028 ret = ssl_tls13_get_sig_alg_from_pk( ssl, own_key, &algorithm );
Jerry Yue91a51a2022-03-22 21:42:50 +08001029 if( ret != 0 || ! mbedtls_ssl_sig_alg_is_received( ssl, algorithm ) )
Jerry Yu8511f122022-01-29 10:01:04 +08001030 {
Jerry Yud66409a2022-02-18 16:42:24 +08001031 MBEDTLS_SSL_DEBUG_MSG( 1,
Jerry Yu2124d052022-02-18 21:07:18 +08001032 ( "signature algorithm not in received or offered list." ) );
Jerry Yu67eced02022-02-25 13:37:36 +08001033
1034 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Signature algorithm is %s",
1035 mbedtls_ssl_sig_alg_to_str( algorithm ) ) );
1036
Jerry Yu71f36f12022-02-23 17:34:29 +08001037 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE,
Jerry Yud66409a2022-02-18 16:42:24 +08001038 MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
Jerry Yu3391ac02022-02-16 11:21:37 +08001039 return( MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
Jerry Yu8511f122022-01-29 10:01:04 +08001040 }
1041
Jerry Yu6c6f1022022-03-25 11:09:50 +08001042 if( mbedtls_ssl_tls13_get_pk_type_and_md_alg_from_sig_alg(
1043 algorithm, &pk_type, &md_alg ) != 0 )
Jerry Yu8c338862022-03-23 13:34:04 +08001044 {
Jerry Yuf8aa9a42022-03-23 20:40:28 +08001045 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Jerry Yu8c338862022-03-23 13:34:04 +08001046 }
1047
Jerry Yu3391ac02022-02-16 11:21:37 +08001048 /* Check there is space for the algorithm identifier (2 bytes) and the
1049 * signature length (2 bytes).
1050 */
1051 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 4 );
Jerry Yu3e536442022-02-15 11:05:59 +08001052 MBEDTLS_PUT_UINT16_BE( algorithm, p, 0 );
1053 p += 2;
Jerry Yu8511f122022-01-29 10:01:04 +08001054
1055 /* Hash verify buffer with indicated hash function */
1056 md_info = mbedtls_md_info_from_type( md_alg );
1057 if( md_info == NULL )
1058 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
1059
1060 ret = mbedtls_md( md_info, verify_buffer, verify_buffer_len, verify_hash );
1061 if( ret != 0 )
1062 return( ret );
1063
1064 verify_hash_len = mbedtls_md_get_size( md_info );
1065 MBEDTLS_SSL_DEBUG_BUF( 3, "verify hash", verify_hash, verify_hash_len );
1066
Jerry Yu8beb9e12022-03-12 16:23:53 +08001067 if( ( ret = mbedtls_pk_sign_ext( pk_type, own_key,
1068 md_alg, verify_hash, verify_hash_len,
Jerry Yu67eced02022-02-25 13:37:36 +08001069 p + 2, (size_t)( end - ( p + 2 ) ), &signature_len,
1070 ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 )
Jerry Yu8511f122022-01-29 10:01:04 +08001071 {
1072 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_pk_sign", ret );
1073 return( ret );
1074 }
Jerry Yu8511f122022-01-29 10:01:04 +08001075
Jerry Yu3e536442022-02-15 11:05:59 +08001076 MBEDTLS_PUT_UINT16_BE( signature_len, p, 0 );
1077 p += 2 + signature_len;
Jerry Yu8511f122022-01-29 10:01:04 +08001078
Jerry Yu3e536442022-02-15 11:05:59 +08001079 *out_len = (size_t)( p - buf );
1080
Jerry Yu8511f122022-01-29 10:01:04 +08001081 return( ret );
1082}
Jerry Yu8511f122022-01-29 10:01:04 +08001083
Jerry Yu3e536442022-02-15 11:05:59 +08001084int mbedtls_ssl_tls13_write_certificate_verify( mbedtls_ssl_context *ssl )
Jerry Yu8511f122022-01-29 10:01:04 +08001085{
1086 int ret = 0;
Jerry Yuca133a32022-02-15 14:22:05 +08001087 unsigned char *buf;
1088 size_t buf_len, msg_len;
1089
Jerry Yu8511f122022-01-29 10:01:04 +08001090 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write certificate verify" ) );
1091
Ronald Cron8f6d39a2022-03-10 18:56:50 +01001092 MBEDTLS_SSL_PROC_CHK( mbedtls_ssl_start_handshake_msg( ssl,
Jerry Yuca133a32022-02-15 14:22:05 +08001093 MBEDTLS_SSL_HS_CERTIFICATE_VERIFY, &buf, &buf_len ) );
Jerry Yu8511f122022-01-29 10:01:04 +08001094
Jerry Yuca133a32022-02-15 14:22:05 +08001095 MBEDTLS_SSL_PROC_CHK( ssl_tls13_write_certificate_verify_body(
1096 ssl, buf, buf + buf_len, &msg_len ) );
Jerry Yu8511f122022-01-29 10:01:04 +08001097
Ronald Cron8f6d39a2022-03-10 18:56:50 +01001098 mbedtls_ssl_add_hs_msg_to_checksum( ssl, MBEDTLS_SSL_HS_CERTIFICATE_VERIFY,
1099 buf, msg_len );
Jerry Yu8511f122022-01-29 10:01:04 +08001100
Ronald Cron8f6d39a2022-03-10 18:56:50 +01001101 MBEDTLS_SSL_PROC_CHK( mbedtls_ssl_finish_handshake_msg(
Jerry Yuca133a32022-02-15 14:22:05 +08001102 ssl, buf_len, msg_len ) );
Jerry Yu8511f122022-01-29 10:01:04 +08001103
1104cleanup:
1105
1106 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write certificate verify" ) );
1107 return( ret );
1108}
1109
Jerry Yu90f152d2022-01-29 22:12:42 +08001110#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
1111
Jerry Yu5cc35062022-01-28 16:16:08 +08001112/*
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001113 *
XiaokangQianc5c39d52021-11-09 11:55:10 +00001114 * STATE HANDLING: Incoming Finished message.
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001115 */
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001116/*
1117 * Implementation
1118 */
1119
XiaokangQianaaa0e192021-11-10 03:07:04 +00001120static int ssl_tls13_preprocess_finished_message( mbedtls_ssl_context *ssl )
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001121{
1122 int ret;
1123
XiaokangQianc5c39d52021-11-09 11:55:10 +00001124 ret = mbedtls_ssl_tls13_calculate_verify_data( ssl,
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001125 ssl->handshake->state_local.finished_in.digest,
1126 sizeof( ssl->handshake->state_local.finished_in.digest ),
1127 &ssl->handshake->state_local.finished_in.digest_len,
XiaokangQianc5c39d52021-11-09 11:55:10 +00001128 ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT ?
XiaokangQianc13f9352021-11-11 06:13:22 +00001129 MBEDTLS_SSL_IS_SERVER : MBEDTLS_SSL_IS_CLIENT );
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001130 if( ret != 0 )
1131 {
XiaokangQianc5c39d52021-11-09 11:55:10 +00001132 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_tls13_calculate_verify_data", ret );
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001133 return( ret );
1134 }
1135
1136 return( 0 );
1137}
1138
XiaokangQianc5c39d52021-11-09 11:55:10 +00001139static int ssl_tls13_parse_finished_message( mbedtls_ssl_context *ssl,
1140 const unsigned char *buf,
1141 const unsigned char *end )
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001142{
XiaokangQian33062842021-11-11 03:37:45 +00001143 /*
1144 * struct {
XiaokangQianc13f9352021-11-11 06:13:22 +00001145 * opaque verify_data[Hash.length];
XiaokangQian33062842021-11-11 03:37:45 +00001146 * } Finished;
1147 */
1148 const unsigned char *expected_verify_data =
1149 ssl->handshake->state_local.finished_in.digest;
1150 size_t expected_verify_data_len =
1151 ssl->handshake->state_local.finished_in.digest_len;
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001152 /* Structural validation */
XiaokangQian33062842021-11-11 03:37:45 +00001153 if( (size_t)( end - buf ) != expected_verify_data_len )
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001154 {
1155 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
1156
1157 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR,
XiaokangQianc13f9352021-11-11 06:13:22 +00001158 MBEDTLS_ERR_SSL_DECODE_ERROR );
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001159 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
1160 }
1161
XiaokangQianc5c39d52021-11-09 11:55:10 +00001162 MBEDTLS_SSL_DEBUG_BUF( 4, "verify_data (self-computed):",
XiaokangQian33062842021-11-11 03:37:45 +00001163 expected_verify_data,
1164 expected_verify_data_len );
XiaokangQianc5c39d52021-11-09 11:55:10 +00001165 MBEDTLS_SSL_DEBUG_BUF( 4, "verify_data (received message):", buf,
XiaokangQian33062842021-11-11 03:37:45 +00001166 expected_verify_data_len );
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001167
1168 /* Semantic validation */
Gabor Mezei685472b2021-11-24 11:17:36 +01001169 if( mbedtls_ct_memcmp( buf,
1170 expected_verify_data,
1171 expected_verify_data_len ) != 0 )
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001172 {
1173 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
1174
XiaokangQian33062842021-11-11 03:37:45 +00001175 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_DECRYPT_ERROR,
XiaokangQianc13f9352021-11-11 06:13:22 +00001176 MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001177 return( MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
1178 }
1179 return( 0 );
1180}
1181
XiaokangQianc5c39d52021-11-09 11:55:10 +00001182int mbedtls_ssl_tls13_process_finished_message( mbedtls_ssl_context *ssl )
1183{
XiaokangQian33062842021-11-11 03:37:45 +00001184 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
XiaokangQianc5c39d52021-11-09 11:55:10 +00001185 unsigned char *buf;
Xiaofei Baieef15042021-11-18 07:29:56 +00001186 size_t buf_len;
XiaokangQianc5c39d52021-11-09 11:55:10 +00001187
XiaokangQiand0aa3e92021-11-10 06:17:40 +00001188 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse finished message" ) );
XiaokangQianc5c39d52021-11-09 11:55:10 +00001189
1190 /* Preprocessing step: Compute handshake digest */
XiaokangQianaaa0e192021-11-10 03:07:04 +00001191 MBEDTLS_SSL_PROC_CHK( ssl_tls13_preprocess_finished_message( ssl ) );
XiaokangQianc5c39d52021-11-09 11:55:10 +00001192
Xiaofei Bai746f9482021-11-12 08:53:56 +00001193 MBEDTLS_SSL_PROC_CHK( mbedtls_ssl_tls13_fetch_handshake_msg( ssl,
XiaokangQianc5c39d52021-11-09 11:55:10 +00001194 MBEDTLS_SSL_HS_FINISHED,
Xiaofei Baieef15042021-11-18 07:29:56 +00001195 &buf, &buf_len ) );
1196 MBEDTLS_SSL_PROC_CHK( ssl_tls13_parse_finished_message( ssl, buf, buf + buf_len ) );
Ronald Cron8f6d39a2022-03-10 18:56:50 +01001197 mbedtls_ssl_add_hs_msg_to_checksum( ssl, MBEDTLS_SSL_HS_FINISHED,
1198 buf, buf_len );
XiaokangQianc5c39d52021-11-09 11:55:10 +00001199
1200cleanup:
1201
XiaokangQiand0aa3e92021-11-10 06:17:40 +00001202 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse finished message" ) );
XiaokangQianc5c39d52021-11-09 11:55:10 +00001203 return( ret );
1204}
1205
XiaokangQian74af2a82021-09-22 07:40:30 +00001206/*
1207 *
XiaokangQiancc90c942021-11-09 12:30:09 +00001208 * STATE HANDLING: Write and send Finished message.
XiaokangQian74af2a82021-09-22 07:40:30 +00001209 *
1210 */
XiaokangQian74af2a82021-09-22 07:40:30 +00001211/*
XiaokangQian35dc6252021-11-11 08:16:19 +00001212 * Implement
XiaokangQian74af2a82021-09-22 07:40:30 +00001213 */
1214
XiaokangQian8773aa02021-11-10 07:33:09 +00001215static int ssl_tls13_prepare_finished_message( mbedtls_ssl_context *ssl )
XiaokangQian74af2a82021-09-22 07:40:30 +00001216{
1217 int ret;
1218
1219 /* Compute transcript of handshake up to now. */
XiaokangQiancc90c942021-11-09 12:30:09 +00001220 ret = mbedtls_ssl_tls13_calculate_verify_data( ssl,
XiaokangQian74af2a82021-09-22 07:40:30 +00001221 ssl->handshake->state_local.finished_out.digest,
1222 sizeof( ssl->handshake->state_local.finished_out.digest ),
1223 &ssl->handshake->state_local.finished_out.digest_len,
1224 ssl->conf->endpoint );
1225
1226 if( ret != 0 )
1227 {
Jerry Yu7ca30542021-12-08 15:57:57 +08001228 MBEDTLS_SSL_DEBUG_RET( 1, "calculate_verify_data failed", ret );
XiaokangQian74af2a82021-09-22 07:40:30 +00001229 return( ret );
1230 }
1231
1232 return( 0 );
1233}
1234
XiaokangQian8773aa02021-11-10 07:33:09 +00001235static int ssl_tls13_write_finished_message_body( mbedtls_ssl_context *ssl,
XiaokangQian35dc6252021-11-11 08:16:19 +00001236 unsigned char *buf,
1237 unsigned char *end,
Xiaofei Baid25fab62021-12-02 06:36:27 +00001238 size_t *out_len )
XiaokangQian74af2a82021-09-22 07:40:30 +00001239{
XiaokangQian8773aa02021-11-10 07:33:09 +00001240 size_t verify_data_len = ssl->handshake->state_local.finished_out.digest_len;
XiaokangQian0fa66432021-11-15 03:33:57 +00001241 /*
1242 * struct {
1243 * opaque verify_data[Hash.length];
1244 * } Finished;
1245 */
XiaokangQian8773aa02021-11-10 07:33:09 +00001246 MBEDTLS_SSL_CHK_BUF_PTR( buf, end, verify_data_len );
XiaokangQian74af2a82021-09-22 07:40:30 +00001247
1248 memcpy( buf, ssl->handshake->state_local.finished_out.digest,
XiaokangQian8773aa02021-11-10 07:33:09 +00001249 verify_data_len );
XiaokangQian74af2a82021-09-22 07:40:30 +00001250
Xiaofei Baid25fab62021-12-02 06:36:27 +00001251 *out_len = verify_data_len;
XiaokangQian74af2a82021-09-22 07:40:30 +00001252 return( 0 );
1253}
XiaokangQianc5c39d52021-11-09 11:55:10 +00001254
XiaokangQian35dc6252021-11-11 08:16:19 +00001255/* Main entry point: orchestrates the other functions */
1256int mbedtls_ssl_tls13_write_finished_message( mbedtls_ssl_context *ssl )
1257{
1258 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1259 unsigned char *buf;
1260 size_t buf_len, msg_len;
1261
1262 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write finished message" ) );
1263
XiaokangQiandce82242021-11-15 06:01:26 +00001264 MBEDTLS_SSL_PROC_CHK( ssl_tls13_prepare_finished_message( ssl ) );
1265
Ronald Cron8f6d39a2022-03-10 18:56:50 +01001266 MBEDTLS_SSL_PROC_CHK( mbedtls_ssl_start_handshake_msg( ssl,
XiaokangQian35dc6252021-11-11 08:16:19 +00001267 MBEDTLS_SSL_HS_FINISHED, &buf, &buf_len ) );
1268
1269 MBEDTLS_SSL_PROC_CHK( ssl_tls13_write_finished_message_body(
1270 ssl, buf, buf + buf_len, &msg_len ) );
1271
Ronald Cron8f6d39a2022-03-10 18:56:50 +01001272 mbedtls_ssl_add_hs_msg_to_checksum( ssl, MBEDTLS_SSL_HS_FINISHED,
1273 buf, msg_len );
XiaokangQian35dc6252021-11-11 08:16:19 +00001274
Ronald Cron8f6d39a2022-03-10 18:56:50 +01001275 MBEDTLS_SSL_PROC_CHK( mbedtls_ssl_finish_handshake_msg(
1276 ssl, buf_len, msg_len ) );
XiaokangQian35dc6252021-11-11 08:16:19 +00001277cleanup:
1278
1279 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write finished message" ) );
1280 return( ret );
1281}
1282
Jerry Yu378254d2021-10-30 21:44:47 +08001283void mbedtls_ssl_tls13_handshake_wrapup( mbedtls_ssl_context *ssl )
1284{
1285
1286 MBEDTLS_SSL_DEBUG_MSG( 3, ( "=> handshake wrapup" ) );
1287
Jerry Yue8c1fca2022-05-18 14:48:56 +08001288 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Switch to application keys for inbound traffic" ) );
1289 mbedtls_ssl_set_inbound_transform ( ssl, ssl->transform_application );
1290
1291 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Switch to application keys for outbound traffic" ) );
1292 mbedtls_ssl_set_outbound_transform( ssl, ssl->transform_application );
1293
Jerry Yu378254d2021-10-30 21:44:47 +08001294 /*
Jerry Yucfe64f02021-11-15 13:54:06 +08001295 * Free the previous session and switch to the current one.
Jerry Yu378254d2021-10-30 21:44:47 +08001296 */
1297 if( ssl->session )
1298 {
Jerry Yu378254d2021-10-30 21:44:47 +08001299 mbedtls_ssl_session_free( ssl->session );
1300 mbedtls_free( ssl->session );
1301 }
1302 ssl->session = ssl->session_negotiate;
1303 ssl->session_negotiate = NULL;
1304
1305 MBEDTLS_SSL_DEBUG_MSG( 3, ( "<= handshake wrapup" ) );
1306}
1307
Ronald Cron49ad6192021-11-24 16:25:31 +01001308/*
1309 *
1310 * STATE HANDLING: Write ChangeCipherSpec
1311 *
1312 */
1313#if defined(MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE)
Ronald Cron49ad6192021-11-24 16:25:31 +01001314static int ssl_tls13_write_change_cipher_spec_body( mbedtls_ssl_context *ssl,
1315 unsigned char *buf,
1316 unsigned char *end,
1317 size_t *olen )
1318{
1319 ((void) ssl);
1320
1321 MBEDTLS_SSL_CHK_BUF_PTR( buf, end, 1 );
1322 buf[0] = 1;
1323 *olen = 1;
1324
1325 return( 0 );
1326}
1327
Ronald Cron49ad6192021-11-24 16:25:31 +01001328int mbedtls_ssl_tls13_write_change_cipher_spec( mbedtls_ssl_context *ssl )
1329{
1330 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1331
1332 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write change cipher spec" ) );
1333
Ronald Cron49ad6192021-11-24 16:25:31 +01001334 /* Write CCS message */
1335 MBEDTLS_SSL_PROC_CHK( ssl_tls13_write_change_cipher_spec_body(
1336 ssl, ssl->out_msg,
1337 ssl->out_msg + MBEDTLS_SSL_OUT_CONTENT_LEN,
1338 &ssl->out_msglen ) );
1339
1340 ssl->out_msgtype = MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC;
1341
Ronald Cron49ad6192021-11-24 16:25:31 +01001342 /* Dispatch message */
Ronald Cron66dbf912022-02-02 15:33:46 +01001343 MBEDTLS_SSL_PROC_CHK( mbedtls_ssl_write_record( ssl, 0 ) );
Ronald Cron49ad6192021-11-24 16:25:31 +01001344
1345cleanup:
1346
1347 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write change cipher spec" ) );
1348 return( ret );
1349}
1350
1351#endif /* MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE */
1352
XiaokangQian78b1fa72022-01-19 06:56:30 +00001353/* Reset SSL context and update hash for handling HRR.
1354 *
1355 * Replace Transcript-Hash(X) by
1356 * Transcript-Hash( message_hash ||
1357 * 00 00 Hash.length ||
1358 * X )
1359 * A few states of the handshake are preserved, including:
1360 * - session ID
1361 * - session ticket
1362 * - negotiated ciphersuite
1363 */
1364int mbedtls_ssl_reset_transcript_for_hrr( mbedtls_ssl_context *ssl )
1365{
1366 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1367 unsigned char hash_transcript[ MBEDTLS_MD_MAX_SIZE + 4 ];
XiaokangQian0ece9982022-01-24 08:56:23 +00001368 size_t hash_len;
XiaokangQian78b1fa72022-01-19 06:56:30 +00001369 const mbedtls_ssl_ciphersuite_t *ciphersuite_info;
1370 uint16_t cipher_suite = ssl->session_negotiate->ciphersuite;
1371 ciphersuite_info = mbedtls_ssl_ciphersuite_from_id( cipher_suite );
1372
1373 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Reset SSL session for HRR" ) );
1374
XiaokangQian0ece9982022-01-24 08:56:23 +00001375 ret = mbedtls_ssl_get_handshake_transcript( ssl, ciphersuite_info->mac,
1376 hash_transcript + 4,
1377 MBEDTLS_MD_MAX_SIZE,
1378 &hash_len );
1379 if( ret != 0 )
1380 {
1381 MBEDTLS_SSL_DEBUG_RET( 4, "mbedtls_ssl_get_handshake_transcript", ret );
1382 return( ret );
1383 }
1384
1385 hash_transcript[0] = MBEDTLS_SSL_HS_MESSAGE_HASH;
1386 hash_transcript[1] = 0;
1387 hash_transcript[2] = 0;
1388 hash_transcript[3] = (unsigned char) hash_len;
1389
1390 hash_len += 4;
1391
XiaokangQian78b1fa72022-01-19 06:56:30 +00001392 if( ciphersuite_info->mac == MBEDTLS_MD_SHA256 )
1393 {
1394#if defined(MBEDTLS_SHA256_C)
XiaokangQian78b1fa72022-01-19 06:56:30 +00001395 MBEDTLS_SSL_DEBUG_BUF( 4, "Truncated SHA-256 handshake transcript",
XiaokangQian0ece9982022-01-24 08:56:23 +00001396 hash_transcript, hash_len );
XiaokangQian78b1fa72022-01-19 06:56:30 +00001397
1398#if defined(MBEDTLS_USE_PSA_CRYPTO)
1399 psa_hash_abort( &ssl->handshake->fin_sha256_psa );
1400 psa_hash_setup( &ssl->handshake->fin_sha256_psa, PSA_ALG_SHA_256 );
1401#else
1402 mbedtls_sha256_starts( &ssl->handshake->fin_sha256, 0 );
1403#endif
XiaokangQian78b1fa72022-01-19 06:56:30 +00001404#endif /* MBEDTLS_SHA256_C */
1405 }
1406 else if( ciphersuite_info->mac == MBEDTLS_MD_SHA384 )
1407 {
1408#if defined(MBEDTLS_SHA384_C)
XiaokangQian78b1fa72022-01-19 06:56:30 +00001409 MBEDTLS_SSL_DEBUG_BUF( 4, "Truncated SHA-384 handshake transcript",
XiaokangQian0ece9982022-01-24 08:56:23 +00001410 hash_transcript, hash_len );
XiaokangQian78b1fa72022-01-19 06:56:30 +00001411
1412#if defined(MBEDTLS_USE_PSA_CRYPTO)
1413 psa_hash_abort( &ssl->handshake->fin_sha384_psa );
1414 psa_hash_setup( &ssl->handshake->fin_sha384_psa, PSA_ALG_SHA_384 );
1415#else
1416 mbedtls_sha512_starts( &ssl->handshake->fin_sha512, 1 );
1417#endif
XiaokangQian78b1fa72022-01-19 06:56:30 +00001418#endif /* MBEDTLS_SHA384_C */
1419 }
1420
XiaokangQian0ece9982022-01-24 08:56:23 +00001421#if defined(MBEDTLS_SHA256_C) || defined(MBEDTLS_SHA384_C)
1422 ssl->handshake->update_checksum( ssl, hash_transcript, hash_len );
1423#endif /* MBEDTLS_SHA256_C || MBEDTLS_SHA384_C */
Przemyslaw Stekiel4b3fff42022-02-14 16:39:52 +01001424
XiaokangQian78b1fa72022-01-19 06:56:30 +00001425 return( ret );
1426}
1427
XiaokangQian9b5d04b2022-04-10 10:20:43 +00001428#if defined(MBEDTLS_ECDH_C)
XiaokangQian7807f9f2022-02-15 10:04:37 +00001429
XiaokangQian9b5d04b2022-04-10 10:20:43 +00001430int mbedtls_ssl_tls13_read_public_ecdhe_share( mbedtls_ssl_context *ssl,
1431 const unsigned char *buf,
1432 size_t buf_len )
XiaokangQian7807f9f2022-02-15 10:04:37 +00001433{
XiaokangQian9b5d04b2022-04-10 10:20:43 +00001434 uint8_t *p = (uint8_t*)buf;
XiaokangQiancfd925f2022-04-14 07:10:37 +00001435 const uint8_t *end = buf + buf_len;
XiaokangQian9b5d04b2022-04-10 10:20:43 +00001436 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001437
XiaokangQian9b5d04b2022-04-10 10:20:43 +00001438 /* Get size of the TLS opaque key_exchange field of the KeyShareEntry struct. */
XiaokangQiancfd925f2022-04-14 07:10:37 +00001439 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 2 );
XiaokangQian9b5d04b2022-04-10 10:20:43 +00001440 uint16_t peerkey_len = MBEDTLS_GET_UINT16_BE( p, 0 );
1441 p += 2;
XiaokangQian3207a322022-02-23 03:15:27 +00001442
XiaokangQian9b5d04b2022-04-10 10:20:43 +00001443 /* Check if key size is consistent with given buffer length. */
XiaokangQiancfd925f2022-04-14 07:10:37 +00001444 MBEDTLS_SSL_CHK_BUF_PTR( p, end, peerkey_len );
XiaokangQian9b5d04b2022-04-10 10:20:43 +00001445
1446 /* Store peer's ECDH public key. */
1447 memcpy( handshake->ecdh_psa_peerkey, p, peerkey_len );
1448 handshake->ecdh_psa_peerkey_len = peerkey_len;
1449
XiaokangQian3207a322022-02-23 03:15:27 +00001450 return( 0 );
1451}
Jerry Yu89e103c2022-03-30 22:43:29 +08001452
1453int mbedtls_ssl_tls13_generate_and_write_ecdh_key_exchange(
1454 mbedtls_ssl_context *ssl,
1455 uint16_t named_group,
1456 unsigned char *buf,
1457 unsigned char *end,
1458 size_t *out_len )
1459{
1460 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
1461 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
1462 psa_key_attributes_t key_attributes;
1463 size_t own_pubkey_len;
1464 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
1465 size_t ecdh_bits = 0;
1466
1467 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Perform PSA-based ECDH computation." ) );
1468
1469 /* Convert EC group to PSA key type. */
1470 if( ( handshake->ecdh_psa_type =
1471 mbedtls_psa_parse_tls_ecc_group( named_group, &ecdh_bits ) ) == 0 )
1472 return( MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
1473
1474 ssl->handshake->ecdh_bits = ecdh_bits;
1475
1476 key_attributes = psa_key_attributes_init();
1477 psa_set_key_usage_flags( &key_attributes, PSA_KEY_USAGE_DERIVE );
1478 psa_set_key_algorithm( &key_attributes, PSA_ALG_ECDH );
1479 psa_set_key_type( &key_attributes, handshake->ecdh_psa_type );
1480 psa_set_key_bits( &key_attributes, handshake->ecdh_bits );
1481
1482 /* Generate ECDH private key. */
1483 status = psa_generate_key( &key_attributes,
1484 &handshake->ecdh_psa_privkey );
1485 if( status != PSA_SUCCESS )
1486 {
1487 ret = psa_ssl_status_to_mbedtls( status );
1488 MBEDTLS_SSL_DEBUG_RET( 1, "psa_generate_key", ret );
1489 return( ret );
1490
1491 }
1492
1493 /* Export the public part of the ECDH private key from PSA. */
1494 status = psa_export_public_key( handshake->ecdh_psa_privkey,
1495 buf, (size_t)( end - buf ),
1496 &own_pubkey_len );
1497 if( status != PSA_SUCCESS )
1498 {
1499 ret = psa_ssl_status_to_mbedtls( status );
1500 MBEDTLS_SSL_DEBUG_RET( 1, "psa_export_public_key", ret );
1501 return( ret );
1502
1503 }
1504
1505 *out_len = own_pubkey_len;
1506
1507 return( 0 );
1508}
XiaokangQian9b5d04b2022-04-10 10:20:43 +00001509#endif /* MBEDTLS_ECDH_C */
XiaokangQian7807f9f2022-02-15 10:04:37 +00001510
Jerry Yufb4b6472022-01-27 15:03:26 +08001511#endif /* MBEDTLS_SSL_TLS_C && MBEDTLS_SSL_PROTO_TLS1_3 */