blob: 3b49ec5f715331088429c252c6adaa7a1d5587b2 [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 {
259 const mbedtls_md_info_t* md_info;
Xiaofei Baid25fab62021-12-02 06:36:27 +0000260 rsassa_pss_options.mgf1_hash_id = md_alg;
XiaokangQian82d34cc2021-11-03 08:51:56 +0000261 if( ( md_info = mbedtls_md_info_from_type( md_alg ) ) == NULL )
262 {
263 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
264 }
Xiaofei Baid25fab62021-12-02 06:36:27 +0000265 rsassa_pss_options.expected_salt_len = mbedtls_md_get_size( md_info );
266 options = (const void*) &rsassa_pss_options;
XiaokangQian82d34cc2021-11-03 08:51:56 +0000267 }
268#endif /* MBEDTLS_X509_RSASSA_PSS_SUPPORT */
Jerry Yu30b071c2021-09-12 20:16:03 +0800269
Xiaofei Baid25fab62021-12-02 06:36:27 +0000270 if( ( ret = mbedtls_pk_verify_ext( sig_alg, options,
Jerry Yu30b071c2021-09-12 20:16:03 +0800271 &ssl->session_negotiate->peer_cert->pk,
272 md_alg, verify_hash, verify_hash_len,
Jerry Yu6f87f252021-10-29 20:12:51 +0800273 p, signature_len ) ) == 0 )
Jerry Yu30b071c2021-09-12 20:16:03 +0800274 {
Jerry Yu6f87f252021-10-29 20:12:51 +0800275 return( 0 );
Jerry Yu30b071c2021-09-12 20:16:03 +0800276 }
Jerry Yu6f87f252021-10-29 20:12:51 +0800277 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_pk_verify_ext", ret );
Jerry Yu30b071c2021-09-12 20:16:03 +0800278
Jerry Yu6f87f252021-10-29 20:12:51 +0800279error:
280 /* RFC 8446 section 4.4.3
281 *
282 * If the verification fails, the receiver MUST terminate the handshake
283 * with a "decrypt_error" alert.
284 */
285 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_DECRYPT_ERROR,
286 MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
287 return( MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
288
Jerry Yu30b071c2021-09-12 20:16:03 +0800289}
290#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
291
292int mbedtls_ssl_tls13_process_certificate_verify( mbedtls_ssl_context *ssl )
293{
Jerry Yu30b071c2021-09-12 20:16:03 +0800294
Jerry Yuda8cdf22021-10-25 15:06:49 +0800295#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
296 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
297 unsigned char verify_buffer[SSL_VERIFY_STRUCT_MAX_SIZE];
298 size_t verify_buffer_len;
299 unsigned char transcript[MBEDTLS_TLS1_3_MD_MAX_SIZE];
300 size_t transcript_len;
301 unsigned char *buf;
302 size_t buf_len;
303
Jerry Yu30b071c2021-09-12 20:16:03 +0800304 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate verify" ) );
305
Jerry Yuda8cdf22021-10-25 15:06:49 +0800306 MBEDTLS_SSL_PROC_CHK(
Xiaofei Bai746f9482021-11-12 08:53:56 +0000307 mbedtls_ssl_tls13_fetch_handshake_msg( ssl,
Jerry Yuda8cdf22021-10-25 15:06:49 +0800308 MBEDTLS_SSL_HS_CERTIFICATE_VERIFY, &buf, &buf_len ) );
Jerry Yu30b071c2021-09-12 20:16:03 +0800309
Jerry Yuda8cdf22021-10-25 15:06:49 +0800310 /* Need to calculate the hash of the transcript first
Jerry Yu0b32c502021-10-28 13:41:59 +0800311 * before reading the message since otherwise it gets
312 * included in the transcript
313 */
Jerry Yuda8cdf22021-10-25 15:06:49 +0800314 ret = mbedtls_ssl_get_handshake_transcript( ssl,
315 ssl->handshake->ciphersuite_info->mac,
316 transcript, sizeof( transcript ),
317 &transcript_len );
318 if( ret != 0 )
Jerry Yu30b071c2021-09-12 20:16:03 +0800319 {
Jerry Yuda8cdf22021-10-25 15:06:49 +0800320 MBEDTLS_SSL_PEND_FATAL_ALERT(
321 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR,
322 MBEDTLS_ERR_SSL_INTERNAL_ERROR );
323 return( ret );
Jerry Yu30b071c2021-09-12 20:16:03 +0800324 }
325
Jerry Yuda8cdf22021-10-25 15:06:49 +0800326 MBEDTLS_SSL_DEBUG_BUF( 3, "handshake hash", transcript, transcript_len );
327
328 /* Create verify structure */
329 ssl_tls13_create_verify_structure( transcript,
Jerry Yu0b32c502021-10-28 13:41:59 +0800330 transcript_len,
331 verify_buffer,
332 &verify_buffer_len,
333 ( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT ) ?
334 MBEDTLS_SSL_IS_SERVER :
335 MBEDTLS_SSL_IS_CLIENT );
Jerry Yuda8cdf22021-10-25 15:06:49 +0800336
337 /* Process the message contents */
Jerry Yu0b32c502021-10-28 13:41:59 +0800338 MBEDTLS_SSL_PROC_CHK( ssl_tls13_parse_certificate_verify( ssl, buf,
339 buf + buf_len, verify_buffer, verify_buffer_len ) );
Jerry Yuda8cdf22021-10-25 15:06:49 +0800340
Ronald Cron8f6d39a2022-03-10 18:56:50 +0100341 mbedtls_ssl_add_hs_msg_to_checksum( ssl, MBEDTLS_SSL_HS_CERTIFICATE_VERIFY,
342 buf, buf_len );
Jerry Yu30b071c2021-09-12 20:16:03 +0800343
344cleanup:
345
346 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse certificate verify" ) );
Jerry Yu5398c102021-11-05 13:32:38 +0800347 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_tls13_process_certificate_verify", ret );
Jerry Yu30b071c2021-09-12 20:16:03 +0800348 return( ret );
Jerry Yuda8cdf22021-10-25 15:06:49 +0800349#else
350 ((void) ssl);
351 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
352 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
353#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
Jerry Yu30b071c2021-09-12 20:16:03 +0800354}
355
356/*
Xiaofei Bai947571e2021-09-29 09:12:03 +0000357 *
XiaokangQian6b916b12022-04-25 07:29:34 +0000358 * STATE HANDLING: Incoming Certificate.
Xiaofei Bai947571e2021-09-29 09:12:03 +0000359 *
360 */
361
Xiaofei Bai947571e2021-09-29 09:12:03 +0000362#if defined(MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED)
363#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
364/*
365 * Structure of Certificate message:
366 *
367 * enum {
368 * X509(0),
369 * RawPublicKey(2),
370 * (255)
371 * } CertificateType;
372 *
373 * struct {
374 * select (certificate_type) {
375 * case RawPublicKey:
376 * * From RFC 7250 ASN.1_subjectPublicKeyInfo *
377 * opaque ASN1_subjectPublicKeyInfo<1..2^24-1>;
378 * case X509:
379 * opaque cert_data<1..2^24-1>;
380 * };
381 * Extension extensions<0..2^16-1>;
382 * } CertificateEntry;
383 *
384 * struct {
385 * opaque certificate_request_context<0..2^8-1>;
386 * CertificateEntry certificate_list<0..2^24-1>;
387 * } Certificate;
388 *
389 */
Xiaofei Bai79595ac2021-10-26 07:16:45 +0000390
391/* Parse certificate chain send by the server. */
Xiaofei Bai947571e2021-09-29 09:12:03 +0000392static int ssl_tls13_parse_certificate( mbedtls_ssl_context *ssl,
393 const unsigned char *buf,
394 const unsigned char *end )
395{
396 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
397 size_t certificate_request_context_len = 0;
398 size_t certificate_list_len = 0;
399 const unsigned char *p = buf;
400 const unsigned char *certificate_list_end;
401
XiaokangQian63e713e2022-05-15 04:26:57 +0000402 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 4 );
Xiaofei Bai947571e2021-09-29 09:12:03 +0000403 certificate_request_context_len = p[0];
XiaokangQian63e713e2022-05-15 04:26:57 +0000404 certificate_list_len = MBEDTLS_GET_UINT24_BE( p, 1 );
405 p += 4;
Xiaofei Bai947571e2021-09-29 09:12:03 +0000406
407 /* In theory, the certificate list can be up to 2^24 Bytes, but we don't
408 * support anything beyond 2^16 = 64K.
409 */
Xiaofei Bai79595ac2021-10-26 07:16:45 +0000410 if( ( certificate_request_context_len != 0 ) ||
411 ( certificate_list_len >= 0x10000 ) )
Xiaofei Bai947571e2021-09-29 09:12:03 +0000412 {
413 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
414 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR,
415 MBEDTLS_ERR_SSL_DECODE_ERROR );
416 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
417 }
418
419 /* In case we tried to reuse a session but it failed */
420 if( ssl->session_negotiate->peer_cert != NULL )
421 {
422 mbedtls_x509_crt_free( ssl->session_negotiate->peer_cert );
423 mbedtls_free( ssl->session_negotiate->peer_cert );
424 }
425
XiaokangQianc3017f62022-05-13 05:55:41 +0000426 if( certificate_list_len == 0 )
427 {
428 ssl->session_negotiate->peer_cert = NULL;
429 ret = 0;
430 goto exit;
431 }
432
Xiaofei Bai947571e2021-09-29 09:12:03 +0000433 if( ( ssl->session_negotiate->peer_cert =
434 mbedtls_calloc( 1, sizeof( mbedtls_x509_crt ) ) ) == NULL )
435 {
436 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc( %" MBEDTLS_PRINTF_SIZET " bytes ) failed",
437 sizeof( mbedtls_x509_crt ) ) );
438 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR,
439 MBEDTLS_ERR_SSL_ALLOC_FAILED );
440 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
441 }
442
443 mbedtls_x509_crt_init( ssl->session_negotiate->peer_cert );
444
Xiaofei Bai947571e2021-09-29 09:12:03 +0000445 certificate_list_end = p + certificate_list_len;
Xiaofei Bai79595ac2021-10-26 07:16:45 +0000446 while( p < certificate_list_end )
Xiaofei Bai947571e2021-09-29 09:12:03 +0000447 {
448 size_t cert_data_len, extensions_len;
449
Xiaofei Bai79595ac2021-10-26 07:16:45 +0000450 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, certificate_list_end, 3 );
Xiaofei Baif93cbd22021-10-29 02:39:30 +0000451 cert_data_len = MBEDTLS_GET_UINT24_BE( p, 0 );
Xiaofei Bai947571e2021-09-29 09:12:03 +0000452 p += 3;
453
454 /* In theory, the CRT can be up to 2^24 Bytes, but we don't support
455 * anything beyond 2^16 = 64K. Otherwise as in the TLS 1.2 code,
456 * check that we have a minimum of 128 bytes of data, this is not
457 * clear why we need that though.
458 */
459 if( ( cert_data_len < 128 ) || ( cert_data_len >= 0x10000 ) )
Xiaofei Bai79595ac2021-10-26 07:16:45 +0000460 {
Xiaofei Bai947571e2021-09-29 09:12:03 +0000461 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad Certificate message" ) );
462 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR,
463 MBEDTLS_ERR_SSL_DECODE_ERROR );
464 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
465 }
466
Xiaofei Bai79595ac2021-10-26 07:16:45 +0000467 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, certificate_list_end, cert_data_len );
Xiaofei Bai947571e2021-09-29 09:12:03 +0000468 ret = mbedtls_x509_crt_parse_der( ssl->session_negotiate->peer_cert,
469 p, cert_data_len );
470
471 switch( ret )
472 {
473 case 0: /*ok*/
474 break;
475 case MBEDTLS_ERR_X509_UNKNOWN_SIG_ALG + MBEDTLS_ERR_OID_NOT_FOUND:
476 /* Ignore certificate with an unknown algorithm: maybe a
477 prior certificate was already trusted. */
478 break;
479
480 case MBEDTLS_ERR_X509_ALLOC_FAILED:
481 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR,
482 MBEDTLS_ERR_X509_ALLOC_FAILED );
483 MBEDTLS_SSL_DEBUG_RET( 1, " mbedtls_x509_crt_parse_der", ret );
484 return( ret );
485
486 case MBEDTLS_ERR_X509_UNKNOWN_VERSION:
487 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT,
488 MBEDTLS_ERR_X509_UNKNOWN_VERSION );
489 MBEDTLS_SSL_DEBUG_RET( 1, " mbedtls_x509_crt_parse_der", ret );
490 return( ret );
491
492 default:
493 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_BAD_CERT,
494 ret );
495 MBEDTLS_SSL_DEBUG_RET( 1, " mbedtls_x509_crt_parse_der", ret );
496 return( ret );
497 }
498
499 p += cert_data_len;
500
501 /* Certificate extensions length */
502 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, certificate_list_end, 2 );
503 extensions_len = MBEDTLS_GET_UINT16_BE( p, 0 );
504 p += 2;
Xiaofei Bai79595ac2021-10-26 07:16:45 +0000505 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, certificate_list_end, extensions_len );
Xiaofei Bai947571e2021-09-29 09:12:03 +0000506 p += extensions_len;
507 }
508
XiaokangQian63e713e2022-05-15 04:26:57 +0000509exit:
Xiaofei Bai947571e2021-09-29 09:12:03 +0000510 /* Check that all the message is consumed. */
511 if( p != end )
512 {
513 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad Certificate message" ) );
514 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR, \
515 MBEDTLS_ERR_SSL_DECODE_ERROR );
516 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
517 }
518
519 MBEDTLS_SSL_DEBUG_CRT( 3, "peer certificate", ssl->session_negotiate->peer_cert );
520
521 return( ret );
522}
523#else
524static int ssl_tls13_parse_certificate( mbedtls_ssl_context *ssl,
525 const unsigned char *buf,
526 const unsigned char *end )
527{
528 ((void) ssl);
529 ((void) buf);
530 ((void) end);
531 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
532}
533#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
534#endif /* MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED */
535
536#if defined(MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED)
537#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Xiaofei Bai79595ac2021-10-26 07:16:45 +0000538/* Validate certificate chain sent by the server. */
Xiaofei Bai947571e2021-09-29 09:12:03 +0000539static int ssl_tls13_validate_certificate( mbedtls_ssl_context *ssl )
540{
541 int ret = 0;
XiaokangQian989f06d2022-05-17 01:50:15 +0000542 int authmode = MBEDTLS_SSL_VERIFY_REQUIRED;
Xiaofei Bai947571e2021-09-29 09:12:03 +0000543 mbedtls_x509_crt *ca_chain;
544 mbedtls_x509_crl *ca_crl;
Xiaofei Baiff456022021-10-28 06:50:17 +0000545 uint32_t verify_result = 0;
Xiaofei Bai947571e2021-09-29 09:12:03 +0000546
XiaokangQian6b916b12022-04-25 07:29:34 +0000547 /* If SNI was used, overwrite authentication mode
548 * from the configuration. */
XiaokangQian989f06d2022-05-17 01:50:15 +0000549#if defined(MBEDTLS_SSL_SRV_C)
550 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
XiaokangQiane7a5da52022-05-30 00:59:29 +0000551 authmode = ssl->conf->authmode;
XiaokangQian6b916b12022-04-25 07:29:34 +0000552#endif
553
554 /*
XiaokangQian989f06d2022-05-17 01:50:15 +0000555 * If the peer hasn't sent a certificate ( i.e. it sent
XiaokangQian6b916b12022-04-25 07:29:34 +0000556 * an empty certificate chain ), this is reflected in the peer CRT
557 * structure being unset.
558 * Check for that and handle it depending on the
XiaokangQian989f06d2022-05-17 01:50:15 +0000559 * authentication mode.
XiaokangQian6b916b12022-04-25 07:29:34 +0000560 */
XiaokangQian63e713e2022-05-15 04:26:57 +0000561 if( ssl->session_negotiate->peer_cert == NULL )
XiaokangQian6b916b12022-04-25 07:29:34 +0000562 {
XiaokangQian989f06d2022-05-17 01:50:15 +0000563 MBEDTLS_SSL_DEBUG_MSG( 1, ( "peer has not sent a certificate" ) );
564
XiaokangQian63e713e2022-05-15 04:26:57 +0000565#if defined(MBEDTLS_SSL_SRV_C)
566 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
567 {
XiaokangQian63e713e2022-05-15 04:26:57 +0000568 /* The client was asked for a certificate but didn't send
569 * one. The client should know what's going on, so we
570 * don't send an alert.
571 */
572 ssl->session_negotiate->verify_result = MBEDTLS_X509_BADCERT_MISSING;
573 if( authmode == MBEDTLS_SSL_VERIFY_OPTIONAL )
574 return( 0 );
575 else
XiaokangQian989f06d2022-05-17 01:50:15 +0000576 {
577 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_NO_CERT,
XiaokangQianaca90482022-05-19 07:19:31 +0000578 MBEDTLS_ERR_SSL_NO_CLIENT_CERTIFICATE );
579 return( MBEDTLS_ERR_SSL_NO_CLIENT_CERTIFICATE );
XiaokangQian989f06d2022-05-17 01:50:15 +0000580 }
XiaokangQian63e713e2022-05-15 04:26:57 +0000581 }
XiaokangQian6b916b12022-04-25 07:29:34 +0000582#endif /* MBEDTLS_SSL_SRV_C */
583
XiaokangQianc3017f62022-05-13 05:55:41 +0000584#if defined(MBEDTLS_SSL_CLI_C)
XiaokangQian63e713e2022-05-15 04:26:57 +0000585 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
586 {
587 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_NO_CERT,
588 MBEDTLS_ERR_SSL_FATAL_ALERT_MESSAGE );
589 return( MBEDTLS_ERR_SSL_FATAL_ALERT_MESSAGE );
590 }
XiaokangQianc3017f62022-05-13 05:55:41 +0000591#endif /* MBEDTLS_SSL_CLI_C */
XiaokangQian63e713e2022-05-15 04:26:57 +0000592 }
XiaokangQian6b916b12022-04-25 07:29:34 +0000593
Xiaofei Bai947571e2021-09-29 09:12:03 +0000594#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
595 if( ssl->handshake->sni_ca_chain != NULL )
596 {
597 ca_chain = ssl->handshake->sni_ca_chain;
598 ca_crl = ssl->handshake->sni_ca_crl;
599 }
600 else
601#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
602 {
603 ca_chain = ssl->conf->ca_chain;
604 ca_crl = ssl->conf->ca_crl;
605 }
606
607 /*
608 * Main check: verify certificate
609 */
610 ret = mbedtls_x509_crt_verify_with_profile(
611 ssl->session_negotiate->peer_cert,
612 ca_chain, ca_crl,
613 ssl->conf->cert_profile,
614 ssl->hostname,
Xiaofei Baiff456022021-10-28 06:50:17 +0000615 &verify_result,
Xiaofei Bai947571e2021-09-29 09:12:03 +0000616 ssl->conf->f_vrfy, ssl->conf->p_vrfy );
617
618 if( ret != 0 )
619 {
620 MBEDTLS_SSL_DEBUG_RET( 1, "x509_verify_cert", ret );
621 }
622
623 /*
624 * Secondary checks: always done, but change 'ret' only if it was 0
625 */
Xiaofei Bai947571e2021-09-29 09:12:03 +0000626 if( mbedtls_ssl_check_cert_usage( ssl->session_negotiate->peer_cert,
627 ssl->handshake->ciphersuite_info,
628 !ssl->conf->endpoint,
Xiaofei Baiff456022021-10-28 06:50:17 +0000629 &verify_result ) != 0 )
Xiaofei Bai947571e2021-09-29 09:12:03 +0000630 {
631 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate ( usage extensions )" ) );
632 if( ret == 0 )
633 ret = MBEDTLS_ERR_SSL_BAD_CERTIFICATE;
634 }
635
XiaokangQian6b916b12022-04-25 07:29:34 +0000636 /* mbedtls_x509_crt_verify_with_profile is supposed to report a
637 * verification failure through MBEDTLS_ERR_X509_CERT_VERIFY_FAILED,
638 * with details encoded in the verification flags. All other kinds
639 * of error codes, including those from the user provided f_vrfy
640 * functions, are treated as fatal and lead to a failure of
641 * ssl_tls13_parse_certificate even if verification was optional. */
642 if( authmode == MBEDTLS_SSL_VERIFY_OPTIONAL &&
643 ( ret == MBEDTLS_ERR_X509_CERT_VERIFY_FAILED ||
644 ret == MBEDTLS_ERR_SSL_BAD_CERTIFICATE ) )
645 {
646 ret = 0;
647 }
Xiaofei Bai947571e2021-09-29 09:12:03 +0000648
XiaokangQian6b916b12022-04-25 07:29:34 +0000649 if( ca_chain == NULL && authmode == MBEDTLS_SSL_VERIFY_REQUIRED )
Xiaofei Bai947571e2021-09-29 09:12:03 +0000650 {
651 MBEDTLS_SSL_DEBUG_MSG( 1, ( "got no CA chain" ) );
652 ret = MBEDTLS_ERR_SSL_CA_CHAIN_REQUIRED;
653 }
654
655 if( ret != 0 )
656 {
657 /* The certificate may have been rejected for several reasons.
658 Pick one and send the corresponding alert. Which alert to send
659 may be a subject of debate in some cases. */
Xiaofei Baiff456022021-10-28 06:50:17 +0000660 if( verify_result & MBEDTLS_X509_BADCERT_OTHER )
Xiaofei Bai947571e2021-09-29 09:12:03 +0000661 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED, ret );
Xiaofei Baiff456022021-10-28 06:50:17 +0000662 else if( verify_result & MBEDTLS_X509_BADCERT_CN_MISMATCH )
Xiaofei Bai947571e2021-09-29 09:12:03 +0000663 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_BAD_CERT, ret );
Xiaofei Baif93cbd22021-10-29 02:39:30 +0000664 else if( verify_result & ( MBEDTLS_X509_BADCERT_KEY_USAGE |
665 MBEDTLS_X509_BADCERT_EXT_KEY_USAGE |
666 MBEDTLS_X509_BADCERT_NS_CERT_TYPE |
667 MBEDTLS_X509_BADCERT_BAD_PK |
668 MBEDTLS_X509_BADCERT_BAD_KEY ) )
Xiaofei Bai947571e2021-09-29 09:12:03 +0000669 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT, ret );
Xiaofei Baiff456022021-10-28 06:50:17 +0000670 else if( verify_result & MBEDTLS_X509_BADCERT_EXPIRED )
Xiaofei Bai947571e2021-09-29 09:12:03 +0000671 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_CERT_EXPIRED, ret );
Xiaofei Baiff456022021-10-28 06:50:17 +0000672 else if( verify_result & MBEDTLS_X509_BADCERT_REVOKED )
Xiaofei Bai947571e2021-09-29 09:12:03 +0000673 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_CERT_REVOKED, ret );
Xiaofei Baiff456022021-10-28 06:50:17 +0000674 else if( verify_result & MBEDTLS_X509_BADCERT_NOT_TRUSTED )
Xiaofei Bai947571e2021-09-29 09:12:03 +0000675 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_UNKNOWN_CA, ret );
676 else
677 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_CERT_UNKNOWN, ret );
678 }
679
680#if defined(MBEDTLS_DEBUG_C)
Xiaofei Baiff456022021-10-28 06:50:17 +0000681 if( verify_result != 0 )
Xiaofei Bai947571e2021-09-29 09:12:03 +0000682 {
683 MBEDTLS_SSL_DEBUG_MSG( 3, ( "! Certificate verification flags %08x",
Jerry Yu83bb1312021-10-28 22:16:33 +0800684 (unsigned int) verify_result ) );
Xiaofei Bai947571e2021-09-29 09:12:03 +0000685 }
686 else
687 {
688 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Certificate verification flags clear" ) );
689 }
690#endif /* MBEDTLS_DEBUG_C */
691
Xiaofei Baiff456022021-10-28 06:50:17 +0000692 ssl->session_negotiate->verify_result = verify_result;
Xiaofei Bai947571e2021-09-29 09:12:03 +0000693 return( ret );
694}
695#else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
696static int ssl_tls13_validate_certificate( mbedtls_ssl_context *ssl )
697{
698 ((void) ssl);
699 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
700}
701#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
702#endif /* MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED */
703
Xiaofei Bai79595ac2021-10-26 07:16:45 +0000704int mbedtls_ssl_tls13_process_certificate( mbedtls_ssl_context *ssl )
Xiaofei Bai947571e2021-09-29 09:12:03 +0000705{
Xiaofei Bai79595ac2021-10-26 07:16:45 +0000706 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
707 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) );
708
709#if defined(MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED)
XiaokangQianc3017f62022-05-13 05:55:41 +0000710 unsigned char *buf;
711 size_t buf_len;
Xiaofei Bai79595ac2021-10-26 07:16:45 +0000712
XiaokangQianc3017f62022-05-13 05:55:41 +0000713 MBEDTLS_SSL_PROC_CHK( mbedtls_ssl_tls13_fetch_handshake_msg(
714 ssl, MBEDTLS_SSL_HS_CERTIFICATE,
715 &buf, &buf_len ) );
Xiaofei Bai79595ac2021-10-26 07:16:45 +0000716
XiaokangQianc3017f62022-05-13 05:55:41 +0000717 /* Parse the certificate chain sent by the peer. */
718 MBEDTLS_SSL_PROC_CHK( ssl_tls13_parse_certificate( ssl, buf,
719 buf + buf_len ) );
720 /* Validate the certificate chain and set the verification results. */
721 MBEDTLS_SSL_PROC_CHK( ssl_tls13_validate_certificate( ssl ) );
Xiaofei Bai79595ac2021-10-26 07:16:45 +0000722
XiaokangQianc3017f62022-05-13 05:55:41 +0000723 mbedtls_ssl_add_hs_msg_to_checksum( ssl, MBEDTLS_SSL_HS_CERTIFICATE,
724 buf, buf_len );
Xiaofei Bai79595ac2021-10-26 07:16:45 +0000725
Xiaofei Bai79595ac2021-10-26 07:16:45 +0000726cleanup:
XiaokangQianaca90482022-05-19 07:19:31 +0000727#endif /* MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED */
Xiaofei Bai79595ac2021-10-26 07:16:45 +0000728
729 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse certificate" ) );
730 return( ret );
Xiaofei Bai947571e2021-09-29 09:12:03 +0000731}
Jerry Yu90f152d2022-01-29 22:12:42 +0800732#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Jerry Yu7399d0d2022-01-30 17:54:19 +0800733/*
734 * enum {
735 * X509(0),
736 * RawPublicKey(2),
737 * (255)
738 * } CertificateType;
739 *
740 * struct {
741 * select (certificate_type) {
742 * case RawPublicKey:
743 * // From RFC 7250 ASN.1_subjectPublicKeyInfo
744 * opaque ASN1_subjectPublicKeyInfo<1..2^24-1>;
745 *
746 * case X509:
747 * opaque cert_data<1..2^24-1>;
748 * };
749 * Extension extensions<0..2^16-1>;
750 * } CertificateEntry;
751 *
752 * struct {
753 * opaque certificate_request_context<0..2^8-1>;
754 * CertificateEntry certificate_list<0..2^24-1>;
755 * } Certificate;
756 */
757static int ssl_tls13_write_certificate_body( mbedtls_ssl_context *ssl,
Jerry Yu3e536442022-02-15 11:05:59 +0800758 unsigned char *buf,
Jerry Yu7399d0d2022-01-30 17:54:19 +0800759 unsigned char *end,
Jerry Yu3e536442022-02-15 11:05:59 +0800760 size_t *out_len )
Jerry Yu5cc35062022-01-28 16:16:08 +0800761{
Jerry Yu5cc35062022-01-28 16:16:08 +0800762 const mbedtls_x509_crt *crt = mbedtls_ssl_own_cert( ssl );
Jerry Yu3e536442022-02-15 11:05:59 +0800763 unsigned char *p = buf;
Jerry Yuc8d8d4e2022-02-18 12:10:03 +0800764 unsigned char *certificate_request_context =
765 ssl->handshake->certificate_request_context;
766 unsigned char certificate_request_context_len =
767 ssl->handshake->certificate_request_context_len;
768 unsigned char *p_certificate_list_len;
Jerry Yu5cc35062022-01-28 16:16:08 +0800769
Jerry Yu5cc35062022-01-28 16:16:08 +0800770
Jerry Yu3391ac02022-02-16 11:21:37 +0800771 /* ...
772 * opaque certificate_request_context<0..2^8-1>;
773 * ...
774 */
Jerry Yuc8d8d4e2022-02-18 12:10:03 +0800775 MBEDTLS_SSL_CHK_BUF_PTR( p, end, certificate_request_context_len + 1 );
776 *p++ = certificate_request_context_len;
777 if( certificate_request_context_len > 0 )
Jerry Yu537530d2022-02-15 14:00:57 +0800778 {
Jerry Yuc8d8d4e2022-02-18 12:10:03 +0800779 memcpy( p, certificate_request_context, certificate_request_context_len );
780 p += certificate_request_context_len;
Jerry Yu537530d2022-02-15 14:00:57 +0800781 }
782
Jerry Yu3391ac02022-02-16 11:21:37 +0800783 /* ...
784 * CertificateEntry certificate_list<0..2^24-1>;
785 * ...
786 */
Jerry Yu3e536442022-02-15 11:05:59 +0800787 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 3 );
Jerry Yuc8d8d4e2022-02-18 12:10:03 +0800788 p_certificate_list_len = p;
Jerry Yu3e536442022-02-15 11:05:59 +0800789 p += 3;
790
Jerry Yu7399d0d2022-01-30 17:54:19 +0800791 MBEDTLS_SSL_DEBUG_CRT( 3, "own certificate", crt );
Jerry Yu5cc35062022-01-28 16:16:08 +0800792
Jerry Yuc8d8d4e2022-02-18 12:10:03 +0800793 while( crt != NULL )
Jerry Yu5cc35062022-01-28 16:16:08 +0800794 {
Jerry Yu7399d0d2022-01-30 17:54:19 +0800795 size_t cert_data_len = crt->raw.len;
Jerry Yu5cc35062022-01-28 16:16:08 +0800796
Jerry Yu7399d0d2022-01-30 17:54:19 +0800797 MBEDTLS_SSL_CHK_BUF_PTR( p, end, cert_data_len + 3 + 2 );
798 MBEDTLS_PUT_UINT24_BE( cert_data_len, p, 0 );
799 p += 3;
Jerry Yu5cc35062022-01-28 16:16:08 +0800800
Jerry Yu7399d0d2022-01-30 17:54:19 +0800801 memcpy( p, crt->raw.p, cert_data_len );
802 p += cert_data_len;
803 crt = crt->next;
Jerry Yu5cc35062022-01-28 16:16:08 +0800804
805 /* Currently, we don't have any certificate extensions defined.
806 * Hence, we are sending an empty extension with length zero.
807 */
Jerry Yu7399d0d2022-01-30 17:54:19 +0800808 MBEDTLS_PUT_UINT24_BE( 0, p, 0 );
809 p += 2;
Jerry Yu5cc35062022-01-28 16:16:08 +0800810 }
Jerry Yu5cc35062022-01-28 16:16:08 +0800811
Jerry Yuc8d8d4e2022-02-18 12:10:03 +0800812 MBEDTLS_PUT_UINT24_BE( p - p_certificate_list_len - 3,
813 p_certificate_list_len, 0 );
Jerry Yu7399d0d2022-01-30 17:54:19 +0800814
Jerry Yu3e536442022-02-15 11:05:59 +0800815 *out_len = p - buf;
Jerry Yu5cc35062022-01-28 16:16:08 +0800816
817 return( 0 );
818}
Jerry Yu5cc35062022-01-28 16:16:08 +0800819
Jerry Yu3e536442022-02-15 11:05:59 +0800820int mbedtls_ssl_tls13_write_certificate( mbedtls_ssl_context *ssl )
Jerry Yu5cc35062022-01-28 16:16:08 +0800821{
822 int ret;
Ronald Cron5bb8fc82022-03-09 07:00:13 +0100823 unsigned char *buf;
824 size_t buf_len, msg_len;
825
Jerry Yu5cc35062022-01-28 16:16:08 +0800826 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write certificate" ) );
827
Ronald Cron8f6d39a2022-03-10 18:56:50 +0100828 MBEDTLS_SSL_PROC_CHK( mbedtls_ssl_start_handshake_msg( ssl,
Ronald Cron5bb8fc82022-03-09 07:00:13 +0100829 MBEDTLS_SSL_HS_CERTIFICATE, &buf, &buf_len ) );
Jerry Yu5cc35062022-01-28 16:16:08 +0800830
Ronald Cron5bb8fc82022-03-09 07:00:13 +0100831 MBEDTLS_SSL_PROC_CHK( ssl_tls13_write_certificate_body( ssl,
832 buf,
833 buf + buf_len,
834 &msg_len ) );
Jerry Yu5cc35062022-01-28 16:16:08 +0800835
Ronald Cron8f6d39a2022-03-10 18:56:50 +0100836 mbedtls_ssl_add_hs_msg_to_checksum( ssl, MBEDTLS_SSL_HS_CERTIFICATE,
837 buf, msg_len );
Jerry Yu5cc35062022-01-28 16:16:08 +0800838
Ronald Cron8f6d39a2022-03-10 18:56:50 +0100839 MBEDTLS_SSL_PROC_CHK( mbedtls_ssl_finish_handshake_msg(
Ronald Cron5bb8fc82022-03-09 07:00:13 +0100840 ssl, buf_len, msg_len ) );
Jerry Yu5cc35062022-01-28 16:16:08 +0800841cleanup:
842
843 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write certificate" ) );
844 return( ret );
845}
846
Jerry Yu3e536442022-02-15 11:05:59 +0800847/*
848 * STATE HANDLING: Output Certificate Verify
849 */
Jerry Yue91a51a2022-03-22 21:42:50 +0800850static int ssl_tls13_get_sig_alg_from_pk( mbedtls_ssl_context *ssl,
851 mbedtls_pk_context *own_key,
Jerry Yu8c338862022-03-23 13:34:04 +0800852 uint16_t *algorithm )
Jerry Yu67eced02022-02-25 13:37:36 +0800853{
854 mbedtls_pk_type_t sig = mbedtls_ssl_sig_from_pk( own_key );
855 /* Determine the size of the key */
856 size_t own_key_size = mbedtls_pk_get_bitlen( own_key );
Jerry Yue91a51a2022-03-22 21:42:50 +0800857 *algorithm = MBEDTLS_TLS1_3_SIG_NONE;
Jerry Yu67eced02022-02-25 13:37:36 +0800858 ((void) own_key_size);
859
860 switch( sig )
861 {
862#if defined(MBEDTLS_ECDSA_C)
863 case MBEDTLS_SSL_SIG_ECDSA:
864 switch( own_key_size )
865 {
866 case 256:
Jerry Yue91a51a2022-03-22 21:42:50 +0800867 *algorithm = MBEDTLS_TLS1_3_SIG_ECDSA_SECP256R1_SHA256;
Jerry Yue91a51a2022-03-22 21:42:50 +0800868 return( 0 );
Jerry Yu67eced02022-02-25 13:37:36 +0800869 case 384:
Jerry Yue91a51a2022-03-22 21:42:50 +0800870 *algorithm = MBEDTLS_TLS1_3_SIG_ECDSA_SECP384R1_SHA384;
Jerry Yue91a51a2022-03-22 21:42:50 +0800871 return( 0 );
Jerry Yu67eced02022-02-25 13:37:36 +0800872 case 521:
Jerry Yue91a51a2022-03-22 21:42:50 +0800873 *algorithm = MBEDTLS_TLS1_3_SIG_ECDSA_SECP521R1_SHA512;
Jerry Yue91a51a2022-03-22 21:42:50 +0800874 return( 0 );
Jerry Yu67eced02022-02-25 13:37:36 +0800875 default:
876 MBEDTLS_SSL_DEBUG_MSG( 3,
877 ( "unknown key size: %"
878 MBEDTLS_PRINTF_SIZET " bits",
879 own_key_size ) );
880 break;
881 }
882 break;
883#endif /* MBEDTLS_ECDSA_C */
884
Jerry Yucef3f332022-03-22 23:00:13 +0800885#if defined(MBEDTLS_RSA_C)
Jerry Yu67eced02022-02-25 13:37:36 +0800886 case MBEDTLS_SSL_SIG_RSA:
Jerry Yucef3f332022-03-22 23:00:13 +0800887#if defined(MBEDTLS_PKCS1_V21)
888#if defined(MBEDTLS_SHA256_C)
Jerry Yu67eced02022-02-25 13:37:36 +0800889 if( own_key_size <= 2048 &&
890 mbedtls_ssl_sig_alg_is_received( ssl,
891 MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA256 ) )
892 {
Jerry Yue91a51a2022-03-22 21:42:50 +0800893 *algorithm = MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA256;
Jerry Yue91a51a2022-03-22 21:42:50 +0800894 return( 0 );
Jerry Yu67eced02022-02-25 13:37:36 +0800895 }
Jerry Yucef3f332022-03-22 23:00:13 +0800896 else
897#endif /* MBEDTLS_SHA256_C */
898#if defined(MBEDTLS_SHA384_C)
899 if( own_key_size <= 3072 &&
900 mbedtls_ssl_sig_alg_is_received( ssl,
Jerry Yu67eced02022-02-25 13:37:36 +0800901 MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA384 ) )
902 {
Jerry Yue91a51a2022-03-22 21:42:50 +0800903 *algorithm = MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA384;
Jerry Yue91a51a2022-03-22 21:42:50 +0800904 return( 0 );
Jerry Yu67eced02022-02-25 13:37:36 +0800905 }
Jerry Yucef3f332022-03-22 23:00:13 +0800906 else
907#endif /* MBEDTLS_SHA384_C */
908#if defined(MBEDTLS_SHA512_C)
909 if( own_key_size <= 4096 &&
910 mbedtls_ssl_sig_alg_is_received( ssl,
Jerry Yu67eced02022-02-25 13:37:36 +0800911 MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA512 ) )
912 {
Jerry Yue91a51a2022-03-22 21:42:50 +0800913 *algorithm = MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA512;
Jerry Yue91a51a2022-03-22 21:42:50 +0800914 return( 0 );
Jerry Yu67eced02022-02-25 13:37:36 +0800915 }
Jerry Yucef3f332022-03-22 23:00:13 +0800916 else
917#endif /* MBEDTLS_SHA512_C */
918#endif /* MBEDTLS_PKCS1_V21 */
919#if defined(MBEDTLS_PKCS1_V15)
920#if defined(MBEDTLS_SHA256_C)
921 if( own_key_size <= 2048 &&
922 mbedtls_ssl_sig_alg_is_received( ssl,
923 MBEDTLS_TLS1_3_SIG_RSA_PKCS1_SHA256 ) )
924 {
925 *algorithm = MBEDTLS_TLS1_3_SIG_RSA_PKCS1_SHA256;
Jerry Yucef3f332022-03-22 23:00:13 +0800926 return( 0 );
927 }
928 else
929#endif /* MBEDTLS_SHA256_C */
930#if defined(MBEDTLS_SHA384_C)
931 if( own_key_size <= 3072 &&
932 mbedtls_ssl_sig_alg_is_received( ssl,
933 MBEDTLS_TLS1_3_SIG_RSA_PKCS1_SHA384 ) )
934 {
935 *algorithm = MBEDTLS_TLS1_3_SIG_RSA_PKCS1_SHA384;
Jerry Yucef3f332022-03-22 23:00:13 +0800936 return( 0 );
937 }
938 else
939#endif /* MBEDTLS_SHA384_C */
940#if defined(MBEDTLS_SHA512_C)
941 if( own_key_size <= 4096 &&
942 mbedtls_ssl_sig_alg_is_received( ssl,
943 MBEDTLS_TLS1_3_SIG_RSA_PKCS1_SHA512 ) )
944 {
945 *algorithm = MBEDTLS_TLS1_3_SIG_RSA_PKCS1_SHA512;
Jerry Yucef3f332022-03-22 23:00:13 +0800946 return( 0 );
947 }
948 else
949#endif /* MBEDTLS_SHA512_C */
950#endif /* MBEDTLS_PKCS1_V15 */
951 {
952 MBEDTLS_SSL_DEBUG_MSG( 3,
953 ( "unknown key size: %"
954 MBEDTLS_PRINTF_SIZET " bits",
955 own_key_size ) );
956 }
Jerry Yu67eced02022-02-25 13:37:36 +0800957 break;
Jerry Yucef3f332022-03-22 23:00:13 +0800958#endif /* MBEDTLS_RSA_C */
Jerry Yu67eced02022-02-25 13:37:36 +0800959 default:
960 MBEDTLS_SSL_DEBUG_MSG( 1,
Andrzej Kurek5c65c572022-04-13 14:28:52 -0400961 ( "unknown signature type : %u", sig ) );
Jerry Yu67eced02022-02-25 13:37:36 +0800962 break;
963 }
Jerry Yue91a51a2022-03-22 21:42:50 +0800964 return( -1 );
Jerry Yu67eced02022-02-25 13:37:36 +0800965}
966
Jerry Yu3e536442022-02-15 11:05:59 +0800967static int ssl_tls13_write_certificate_verify_body( mbedtls_ssl_context *ssl,
968 unsigned char *buf,
969 unsigned char *end,
970 size_t *out_len )
Jerry Yu8511f122022-01-29 10:01:04 +0800971{
972 int ret;
Jerry Yu3e536442022-02-15 11:05:59 +0800973 unsigned char *p = buf;
Jerry Yu8511f122022-01-29 10:01:04 +0800974 mbedtls_pk_context *own_key;
Jerry Yu3e536442022-02-15 11:05:59 +0800975
Jerry Yu8511f122022-01-29 10:01:04 +0800976 unsigned char handshake_hash[ MBEDTLS_TLS1_3_MD_MAX_SIZE ];
977 size_t handshake_hash_len;
Jerry Yu3e536442022-02-15 11:05:59 +0800978 unsigned char verify_buffer[ SSL_VERIFY_STRUCT_MAX_SIZE ];
979 size_t verify_buffer_len;
Jerry Yu67eced02022-02-25 13:37:36 +0800980 mbedtls_pk_type_t pk_type = MBEDTLS_PK_NONE;
Jerry Yu67eced02022-02-25 13:37:36 +0800981 mbedtls_md_type_t md_alg = MBEDTLS_MD_NONE;
pespacek34935872022-05-20 15:43:32 +0200982 psa_algorithm_t psa_algorithm = PSA_ALG_NONE;
Jerry Yu78272072022-02-22 10:28:13 +0800983 uint16_t algorithm = MBEDTLS_TLS1_3_SIG_NONE;
Jerry Yu3e536442022-02-15 11:05:59 +0800984 size_t signature_len = 0;
Jerry Yu3391ac02022-02-16 11:21:37 +0800985 unsigned char verify_hash[ MBEDTLS_MD_MAX_SIZE ];
Jerry Yu3e536442022-02-15 11:05:59 +0800986 size_t verify_hash_len;
pespacekb06acd72022-06-07 13:07:21 +0200987 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Jerry Yu8511f122022-01-29 10:01:04 +0800988
Jerry Yu0b7b1012022-02-23 12:23:05 +0800989 *out_len = 0;
990
Jerry Yu3e536442022-02-15 11:05:59 +0800991 own_key = mbedtls_ssl_own_key( ssl );
992 if( own_key == NULL )
Jerry Yu8511f122022-01-29 10:01:04 +0800993 {
Jerry Yu3e536442022-02-15 11:05:59 +0800994 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
995 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Jerry Yu8511f122022-01-29 10:01:04 +0800996 }
997
Jerry Yu8511f122022-01-29 10:01:04 +0800998 ret = mbedtls_ssl_get_handshake_transcript( ssl,
999 ssl->handshake->ciphersuite_info->mac,
1000 handshake_hash,
1001 sizeof( handshake_hash ),
1002 &handshake_hash_len );
1003 if( ret != 0 )
1004 return( ret );
1005
1006 MBEDTLS_SSL_DEBUG_BUF( 3, "handshake hash",
1007 handshake_hash,
1008 handshake_hash_len);
1009
Jerry Yu8511f122022-01-29 10:01:04 +08001010 ssl_tls13_create_verify_structure( handshake_hash, handshake_hash_len,
1011 verify_buffer, &verify_buffer_len,
1012 ssl->conf->endpoint );
1013
1014 /*
1015 * struct {
1016 * SignatureScheme algorithm;
1017 * opaque signature<0..2^16-1>;
1018 * } CertificateVerify;
1019 */
Jerry Yu8c338862022-03-23 13:34:04 +08001020 ret = ssl_tls13_get_sig_alg_from_pk( ssl, own_key, &algorithm );
Jerry Yue91a51a2022-03-22 21:42:50 +08001021 if( ret != 0 || ! mbedtls_ssl_sig_alg_is_received( ssl, algorithm ) )
Jerry Yu8511f122022-01-29 10:01:04 +08001022 {
Jerry Yud66409a2022-02-18 16:42:24 +08001023 MBEDTLS_SSL_DEBUG_MSG( 1,
Jerry Yu2124d052022-02-18 21:07:18 +08001024 ( "signature algorithm not in received or offered list." ) );
Jerry Yu67eced02022-02-25 13:37:36 +08001025
1026 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Signature algorithm is %s",
1027 mbedtls_ssl_sig_alg_to_str( algorithm ) ) );
1028
Jerry Yu71f36f12022-02-23 17:34:29 +08001029 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE,
Jerry Yud66409a2022-02-18 16:42:24 +08001030 MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
Jerry Yu3391ac02022-02-16 11:21:37 +08001031 return( MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
Jerry Yu8511f122022-01-29 10:01:04 +08001032 }
1033
Jerry Yu6c6f1022022-03-25 11:09:50 +08001034 if( mbedtls_ssl_tls13_get_pk_type_and_md_alg_from_sig_alg(
1035 algorithm, &pk_type, &md_alg ) != 0 )
Jerry Yu8c338862022-03-23 13:34:04 +08001036 {
Jerry Yuf8aa9a42022-03-23 20:40:28 +08001037 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Jerry Yu8c338862022-03-23 13:34:04 +08001038 }
1039
Jerry Yu3391ac02022-02-16 11:21:37 +08001040 /* Check there is space for the algorithm identifier (2 bytes) and the
1041 * signature length (2 bytes).
1042 */
1043 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 4 );
Jerry Yu3e536442022-02-15 11:05:59 +08001044 MBEDTLS_PUT_UINT16_BE( algorithm, p, 0 );
1045 p += 2;
Jerry Yu8511f122022-01-29 10:01:04 +08001046
1047 /* Hash verify buffer with indicated hash function */
pespacek34935872022-05-20 15:43:32 +02001048 psa_algorithm = mbedtls_psa_translate_md( md_alg );
pespacekb06acd72022-06-07 13:07:21 +02001049 status = psa_hash_compute( psa_algorithm,
1050 verify_buffer,
1051 verify_buffer_len,
1052 verify_hash,sizeof( verify_hash ),
1053 &verify_hash_len );
1054 if( status != PSA_SUCCESS )
pespacek670913f2022-06-07 10:53:39 +02001055 return( psa_ssl_status_to_mbedtls( status ) );
Jerry Yu8511f122022-01-29 10:01:04 +08001056
Jerry Yu8511f122022-01-29 10:01:04 +08001057 MBEDTLS_SSL_DEBUG_BUF( 3, "verify hash", verify_hash, verify_hash_len );
1058
Jerry Yu8beb9e12022-03-12 16:23:53 +08001059 if( ( ret = mbedtls_pk_sign_ext( pk_type, own_key,
1060 md_alg, verify_hash, verify_hash_len,
Jerry Yu67eced02022-02-25 13:37:36 +08001061 p + 2, (size_t)( end - ( p + 2 ) ), &signature_len,
1062 ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 )
Jerry Yu8511f122022-01-29 10:01:04 +08001063 {
1064 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_pk_sign", ret );
1065 return( ret );
1066 }
Jerry Yu8511f122022-01-29 10:01:04 +08001067
Jerry Yu3e536442022-02-15 11:05:59 +08001068 MBEDTLS_PUT_UINT16_BE( signature_len, p, 0 );
1069 p += 2 + signature_len;
Jerry Yu8511f122022-01-29 10:01:04 +08001070
Jerry Yu3e536442022-02-15 11:05:59 +08001071 *out_len = (size_t)( p - buf );
1072
Jerry Yu8511f122022-01-29 10:01:04 +08001073 return( ret );
1074}
Jerry Yu8511f122022-01-29 10:01:04 +08001075
Jerry Yu3e536442022-02-15 11:05:59 +08001076int mbedtls_ssl_tls13_write_certificate_verify( mbedtls_ssl_context *ssl )
Jerry Yu8511f122022-01-29 10:01:04 +08001077{
1078 int ret = 0;
Jerry Yuca133a32022-02-15 14:22:05 +08001079 unsigned char *buf;
1080 size_t buf_len, msg_len;
1081
Jerry Yu8511f122022-01-29 10:01:04 +08001082 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write certificate verify" ) );
1083
Ronald Cron8f6d39a2022-03-10 18:56:50 +01001084 MBEDTLS_SSL_PROC_CHK( mbedtls_ssl_start_handshake_msg( ssl,
Jerry Yuca133a32022-02-15 14:22:05 +08001085 MBEDTLS_SSL_HS_CERTIFICATE_VERIFY, &buf, &buf_len ) );
Jerry Yu8511f122022-01-29 10:01:04 +08001086
Jerry Yuca133a32022-02-15 14:22:05 +08001087 MBEDTLS_SSL_PROC_CHK( ssl_tls13_write_certificate_verify_body(
1088 ssl, buf, buf + buf_len, &msg_len ) );
Jerry Yu8511f122022-01-29 10:01:04 +08001089
Ronald Cron8f6d39a2022-03-10 18:56:50 +01001090 mbedtls_ssl_add_hs_msg_to_checksum( ssl, MBEDTLS_SSL_HS_CERTIFICATE_VERIFY,
1091 buf, msg_len );
Jerry Yu8511f122022-01-29 10:01:04 +08001092
Ronald Cron8f6d39a2022-03-10 18:56:50 +01001093 MBEDTLS_SSL_PROC_CHK( mbedtls_ssl_finish_handshake_msg(
Jerry Yuca133a32022-02-15 14:22:05 +08001094 ssl, buf_len, msg_len ) );
Jerry Yu8511f122022-01-29 10:01:04 +08001095
1096cleanup:
1097
1098 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write certificate verify" ) );
1099 return( ret );
1100}
1101
Jerry Yu90f152d2022-01-29 22:12:42 +08001102#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
1103
Jerry Yu5cc35062022-01-28 16:16:08 +08001104/*
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001105 *
XiaokangQianc5c39d52021-11-09 11:55:10 +00001106 * STATE HANDLING: Incoming Finished message.
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001107 */
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001108/*
1109 * Implementation
1110 */
1111
XiaokangQianaaa0e192021-11-10 03:07:04 +00001112static int ssl_tls13_preprocess_finished_message( mbedtls_ssl_context *ssl )
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001113{
1114 int ret;
1115
XiaokangQianc5c39d52021-11-09 11:55:10 +00001116 ret = mbedtls_ssl_tls13_calculate_verify_data( ssl,
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001117 ssl->handshake->state_local.finished_in.digest,
1118 sizeof( ssl->handshake->state_local.finished_in.digest ),
1119 &ssl->handshake->state_local.finished_in.digest_len,
XiaokangQianc5c39d52021-11-09 11:55:10 +00001120 ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT ?
XiaokangQianc13f9352021-11-11 06:13:22 +00001121 MBEDTLS_SSL_IS_SERVER : MBEDTLS_SSL_IS_CLIENT );
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001122 if( ret != 0 )
1123 {
XiaokangQianc5c39d52021-11-09 11:55:10 +00001124 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_tls13_calculate_verify_data", ret );
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001125 return( ret );
1126 }
1127
1128 return( 0 );
1129}
1130
XiaokangQianc5c39d52021-11-09 11:55:10 +00001131static int ssl_tls13_parse_finished_message( mbedtls_ssl_context *ssl,
1132 const unsigned char *buf,
1133 const unsigned char *end )
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001134{
XiaokangQian33062842021-11-11 03:37:45 +00001135 /*
1136 * struct {
XiaokangQianc13f9352021-11-11 06:13:22 +00001137 * opaque verify_data[Hash.length];
XiaokangQian33062842021-11-11 03:37:45 +00001138 * } Finished;
1139 */
1140 const unsigned char *expected_verify_data =
1141 ssl->handshake->state_local.finished_in.digest;
1142 size_t expected_verify_data_len =
1143 ssl->handshake->state_local.finished_in.digest_len;
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001144 /* Structural validation */
XiaokangQian33062842021-11-11 03:37:45 +00001145 if( (size_t)( end - buf ) != expected_verify_data_len )
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001146 {
1147 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
1148
1149 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR,
XiaokangQianc13f9352021-11-11 06:13:22 +00001150 MBEDTLS_ERR_SSL_DECODE_ERROR );
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001151 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
1152 }
1153
XiaokangQianc5c39d52021-11-09 11:55:10 +00001154 MBEDTLS_SSL_DEBUG_BUF( 4, "verify_data (self-computed):",
XiaokangQian33062842021-11-11 03:37:45 +00001155 expected_verify_data,
1156 expected_verify_data_len );
XiaokangQianc5c39d52021-11-09 11:55:10 +00001157 MBEDTLS_SSL_DEBUG_BUF( 4, "verify_data (received message):", buf,
XiaokangQian33062842021-11-11 03:37:45 +00001158 expected_verify_data_len );
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001159
1160 /* Semantic validation */
Gabor Mezei685472b2021-11-24 11:17:36 +01001161 if( mbedtls_ct_memcmp( buf,
1162 expected_verify_data,
1163 expected_verify_data_len ) != 0 )
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001164 {
1165 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
1166
XiaokangQian33062842021-11-11 03:37:45 +00001167 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_DECRYPT_ERROR,
XiaokangQianc13f9352021-11-11 06:13:22 +00001168 MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001169 return( MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
1170 }
1171 return( 0 );
1172}
1173
XiaokangQianc5c39d52021-11-09 11:55:10 +00001174int mbedtls_ssl_tls13_process_finished_message( mbedtls_ssl_context *ssl )
1175{
XiaokangQian33062842021-11-11 03:37:45 +00001176 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
XiaokangQianc5c39d52021-11-09 11:55:10 +00001177 unsigned char *buf;
Xiaofei Baieef15042021-11-18 07:29:56 +00001178 size_t buf_len;
XiaokangQianc5c39d52021-11-09 11:55:10 +00001179
XiaokangQiand0aa3e92021-11-10 06:17:40 +00001180 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse finished message" ) );
XiaokangQianc5c39d52021-11-09 11:55:10 +00001181
Xiaofei Bai746f9482021-11-12 08:53:56 +00001182 MBEDTLS_SSL_PROC_CHK( mbedtls_ssl_tls13_fetch_handshake_msg( ssl,
XiaokangQianc5c39d52021-11-09 11:55:10 +00001183 MBEDTLS_SSL_HS_FINISHED,
Xiaofei Baieef15042021-11-18 07:29:56 +00001184 &buf, &buf_len ) );
Jerry Yu0a92d6c2022-05-16 16:54:46 +08001185
1186 /* Preprocessing step: Compute handshake digest */
1187 MBEDTLS_SSL_PROC_CHK( ssl_tls13_preprocess_finished_message( ssl ) );
1188
Xiaofei Baieef15042021-11-18 07:29:56 +00001189 MBEDTLS_SSL_PROC_CHK( ssl_tls13_parse_finished_message( ssl, buf, buf + buf_len ) );
Jerry Yu0a92d6c2022-05-16 16:54:46 +08001190
Ronald Cron8f6d39a2022-03-10 18:56:50 +01001191 mbedtls_ssl_add_hs_msg_to_checksum( ssl, MBEDTLS_SSL_HS_FINISHED,
1192 buf, buf_len );
XiaokangQianc5c39d52021-11-09 11:55:10 +00001193
1194cleanup:
1195
XiaokangQiand0aa3e92021-11-10 06:17:40 +00001196 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse finished message" ) );
XiaokangQianc5c39d52021-11-09 11:55:10 +00001197 return( ret );
1198}
1199
XiaokangQian74af2a82021-09-22 07:40:30 +00001200/*
1201 *
XiaokangQiancc90c942021-11-09 12:30:09 +00001202 * STATE HANDLING: Write and send Finished message.
XiaokangQian74af2a82021-09-22 07:40:30 +00001203 *
1204 */
XiaokangQian74af2a82021-09-22 07:40:30 +00001205/*
XiaokangQian35dc6252021-11-11 08:16:19 +00001206 * Implement
XiaokangQian74af2a82021-09-22 07:40:30 +00001207 */
1208
XiaokangQian8773aa02021-11-10 07:33:09 +00001209static int ssl_tls13_prepare_finished_message( mbedtls_ssl_context *ssl )
XiaokangQian74af2a82021-09-22 07:40:30 +00001210{
1211 int ret;
1212
1213 /* Compute transcript of handshake up to now. */
XiaokangQiancc90c942021-11-09 12:30:09 +00001214 ret = mbedtls_ssl_tls13_calculate_verify_data( ssl,
XiaokangQian74af2a82021-09-22 07:40:30 +00001215 ssl->handshake->state_local.finished_out.digest,
1216 sizeof( ssl->handshake->state_local.finished_out.digest ),
1217 &ssl->handshake->state_local.finished_out.digest_len,
1218 ssl->conf->endpoint );
1219
1220 if( ret != 0 )
1221 {
Jerry Yu7ca30542021-12-08 15:57:57 +08001222 MBEDTLS_SSL_DEBUG_RET( 1, "calculate_verify_data failed", ret );
XiaokangQian74af2a82021-09-22 07:40:30 +00001223 return( ret );
1224 }
1225
1226 return( 0 );
1227}
1228
XiaokangQian8773aa02021-11-10 07:33:09 +00001229static int ssl_tls13_write_finished_message_body( mbedtls_ssl_context *ssl,
XiaokangQian35dc6252021-11-11 08:16:19 +00001230 unsigned char *buf,
1231 unsigned char *end,
Xiaofei Baid25fab62021-12-02 06:36:27 +00001232 size_t *out_len )
XiaokangQian74af2a82021-09-22 07:40:30 +00001233{
XiaokangQian8773aa02021-11-10 07:33:09 +00001234 size_t verify_data_len = ssl->handshake->state_local.finished_out.digest_len;
XiaokangQian0fa66432021-11-15 03:33:57 +00001235 /*
1236 * struct {
1237 * opaque verify_data[Hash.length];
1238 * } Finished;
1239 */
XiaokangQian8773aa02021-11-10 07:33:09 +00001240 MBEDTLS_SSL_CHK_BUF_PTR( buf, end, verify_data_len );
XiaokangQian74af2a82021-09-22 07:40:30 +00001241
1242 memcpy( buf, ssl->handshake->state_local.finished_out.digest,
XiaokangQian8773aa02021-11-10 07:33:09 +00001243 verify_data_len );
XiaokangQian74af2a82021-09-22 07:40:30 +00001244
Xiaofei Baid25fab62021-12-02 06:36:27 +00001245 *out_len = verify_data_len;
XiaokangQian74af2a82021-09-22 07:40:30 +00001246 return( 0 );
1247}
XiaokangQianc5c39d52021-11-09 11:55:10 +00001248
XiaokangQian35dc6252021-11-11 08:16:19 +00001249/* Main entry point: orchestrates the other functions */
1250int mbedtls_ssl_tls13_write_finished_message( mbedtls_ssl_context *ssl )
1251{
1252 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1253 unsigned char *buf;
1254 size_t buf_len, msg_len;
1255
1256 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write finished message" ) );
1257
XiaokangQiandce82242021-11-15 06:01:26 +00001258 MBEDTLS_SSL_PROC_CHK( ssl_tls13_prepare_finished_message( ssl ) );
1259
Ronald Cron8f6d39a2022-03-10 18:56:50 +01001260 MBEDTLS_SSL_PROC_CHK( mbedtls_ssl_start_handshake_msg( ssl,
XiaokangQian35dc6252021-11-11 08:16:19 +00001261 MBEDTLS_SSL_HS_FINISHED, &buf, &buf_len ) );
1262
1263 MBEDTLS_SSL_PROC_CHK( ssl_tls13_write_finished_message_body(
1264 ssl, buf, buf + buf_len, &msg_len ) );
1265
Ronald Cron8f6d39a2022-03-10 18:56:50 +01001266 mbedtls_ssl_add_hs_msg_to_checksum( ssl, MBEDTLS_SSL_HS_FINISHED,
1267 buf, msg_len );
XiaokangQian35dc6252021-11-11 08:16:19 +00001268
Ronald Cron8f6d39a2022-03-10 18:56:50 +01001269 MBEDTLS_SSL_PROC_CHK( mbedtls_ssl_finish_handshake_msg(
1270 ssl, buf_len, msg_len ) );
XiaokangQian35dc6252021-11-11 08:16:19 +00001271cleanup:
1272
1273 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write finished message" ) );
1274 return( ret );
1275}
1276
Jerry Yu378254d2021-10-30 21:44:47 +08001277void mbedtls_ssl_tls13_handshake_wrapup( mbedtls_ssl_context *ssl )
1278{
1279
1280 MBEDTLS_SSL_DEBUG_MSG( 3, ( "=> handshake wrapup" ) );
1281
Jerry Yue8c1fca2022-05-18 14:48:56 +08001282 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Switch to application keys for inbound traffic" ) );
1283 mbedtls_ssl_set_inbound_transform ( ssl, ssl->transform_application );
1284
1285 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Switch to application keys for outbound traffic" ) );
1286 mbedtls_ssl_set_outbound_transform( ssl, ssl->transform_application );
1287
Jerry Yu378254d2021-10-30 21:44:47 +08001288 /*
Jerry Yucfe64f02021-11-15 13:54:06 +08001289 * Free the previous session and switch to the current one.
Jerry Yu378254d2021-10-30 21:44:47 +08001290 */
1291 if( ssl->session )
1292 {
Jerry Yu378254d2021-10-30 21:44:47 +08001293 mbedtls_ssl_session_free( ssl->session );
1294 mbedtls_free( ssl->session );
1295 }
1296 ssl->session = ssl->session_negotiate;
1297 ssl->session_negotiate = NULL;
1298
1299 MBEDTLS_SSL_DEBUG_MSG( 3, ( "<= handshake wrapup" ) );
1300}
1301
Ronald Cron49ad6192021-11-24 16:25:31 +01001302/*
1303 *
1304 * STATE HANDLING: Write ChangeCipherSpec
1305 *
1306 */
1307#if defined(MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE)
Ronald Cron49ad6192021-11-24 16:25:31 +01001308static int ssl_tls13_write_change_cipher_spec_body( mbedtls_ssl_context *ssl,
1309 unsigned char *buf,
1310 unsigned char *end,
1311 size_t *olen )
1312{
1313 ((void) ssl);
1314
1315 MBEDTLS_SSL_CHK_BUF_PTR( buf, end, 1 );
1316 buf[0] = 1;
1317 *olen = 1;
1318
1319 return( 0 );
1320}
1321
Ronald Cron49ad6192021-11-24 16:25:31 +01001322int mbedtls_ssl_tls13_write_change_cipher_spec( mbedtls_ssl_context *ssl )
1323{
1324 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1325
1326 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write change cipher spec" ) );
1327
Ronald Cron49ad6192021-11-24 16:25:31 +01001328 /* Write CCS message */
1329 MBEDTLS_SSL_PROC_CHK( ssl_tls13_write_change_cipher_spec_body(
1330 ssl, ssl->out_msg,
1331 ssl->out_msg + MBEDTLS_SSL_OUT_CONTENT_LEN,
1332 &ssl->out_msglen ) );
1333
1334 ssl->out_msgtype = MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC;
1335
Ronald Cron49ad6192021-11-24 16:25:31 +01001336 /* Dispatch message */
Ronald Cron66dbf912022-02-02 15:33:46 +01001337 MBEDTLS_SSL_PROC_CHK( mbedtls_ssl_write_record( ssl, 0 ) );
Ronald Cron49ad6192021-11-24 16:25:31 +01001338
1339cleanup:
1340
1341 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write change cipher spec" ) );
1342 return( ret );
1343}
1344
1345#endif /* MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE */
1346
XiaokangQian78b1fa72022-01-19 06:56:30 +00001347/* Reset SSL context and update hash for handling HRR.
1348 *
1349 * Replace Transcript-Hash(X) by
1350 * Transcript-Hash( message_hash ||
1351 * 00 00 Hash.length ||
1352 * X )
1353 * A few states of the handshake are preserved, including:
1354 * - session ID
1355 * - session ticket
1356 * - negotiated ciphersuite
1357 */
1358int mbedtls_ssl_reset_transcript_for_hrr( mbedtls_ssl_context *ssl )
1359{
1360 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1361 unsigned char hash_transcript[ MBEDTLS_MD_MAX_SIZE + 4 ];
XiaokangQian0ece9982022-01-24 08:56:23 +00001362 size_t hash_len;
XiaokangQian78b1fa72022-01-19 06:56:30 +00001363 const mbedtls_ssl_ciphersuite_t *ciphersuite_info;
1364 uint16_t cipher_suite = ssl->session_negotiate->ciphersuite;
1365 ciphersuite_info = mbedtls_ssl_ciphersuite_from_id( cipher_suite );
1366
1367 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Reset SSL session for HRR" ) );
1368
XiaokangQian0ece9982022-01-24 08:56:23 +00001369 ret = mbedtls_ssl_get_handshake_transcript( ssl, ciphersuite_info->mac,
1370 hash_transcript + 4,
1371 MBEDTLS_MD_MAX_SIZE,
1372 &hash_len );
1373 if( ret != 0 )
1374 {
1375 MBEDTLS_SSL_DEBUG_RET( 4, "mbedtls_ssl_get_handshake_transcript", ret );
1376 return( ret );
1377 }
1378
1379 hash_transcript[0] = MBEDTLS_SSL_HS_MESSAGE_HASH;
1380 hash_transcript[1] = 0;
1381 hash_transcript[2] = 0;
1382 hash_transcript[3] = (unsigned char) hash_len;
1383
1384 hash_len += 4;
1385
XiaokangQian78b1fa72022-01-19 06:56:30 +00001386 if( ciphersuite_info->mac == MBEDTLS_MD_SHA256 )
1387 {
1388#if defined(MBEDTLS_SHA256_C)
XiaokangQian78b1fa72022-01-19 06:56:30 +00001389 MBEDTLS_SSL_DEBUG_BUF( 4, "Truncated SHA-256 handshake transcript",
XiaokangQian0ece9982022-01-24 08:56:23 +00001390 hash_transcript, hash_len );
XiaokangQian78b1fa72022-01-19 06:56:30 +00001391
1392#if defined(MBEDTLS_USE_PSA_CRYPTO)
1393 psa_hash_abort( &ssl->handshake->fin_sha256_psa );
1394 psa_hash_setup( &ssl->handshake->fin_sha256_psa, PSA_ALG_SHA_256 );
1395#else
1396 mbedtls_sha256_starts( &ssl->handshake->fin_sha256, 0 );
1397#endif
XiaokangQian78b1fa72022-01-19 06:56:30 +00001398#endif /* MBEDTLS_SHA256_C */
1399 }
1400 else if( ciphersuite_info->mac == MBEDTLS_MD_SHA384 )
1401 {
1402#if defined(MBEDTLS_SHA384_C)
XiaokangQian78b1fa72022-01-19 06:56:30 +00001403 MBEDTLS_SSL_DEBUG_BUF( 4, "Truncated SHA-384 handshake transcript",
XiaokangQian0ece9982022-01-24 08:56:23 +00001404 hash_transcript, hash_len );
XiaokangQian78b1fa72022-01-19 06:56:30 +00001405
1406#if defined(MBEDTLS_USE_PSA_CRYPTO)
1407 psa_hash_abort( &ssl->handshake->fin_sha384_psa );
1408 psa_hash_setup( &ssl->handshake->fin_sha384_psa, PSA_ALG_SHA_384 );
1409#else
1410 mbedtls_sha512_starts( &ssl->handshake->fin_sha512, 1 );
1411#endif
XiaokangQian78b1fa72022-01-19 06:56:30 +00001412#endif /* MBEDTLS_SHA384_C */
1413 }
1414
XiaokangQian0ece9982022-01-24 08:56:23 +00001415#if defined(MBEDTLS_SHA256_C) || defined(MBEDTLS_SHA384_C)
1416 ssl->handshake->update_checksum( ssl, hash_transcript, hash_len );
1417#endif /* MBEDTLS_SHA256_C || MBEDTLS_SHA384_C */
Przemyslaw Stekiel4b3fff42022-02-14 16:39:52 +01001418
XiaokangQian78b1fa72022-01-19 06:56:30 +00001419 return( ret );
1420}
1421
XiaokangQian9b5d04b2022-04-10 10:20:43 +00001422#if defined(MBEDTLS_ECDH_C)
XiaokangQian7807f9f2022-02-15 10:04:37 +00001423
XiaokangQian9b5d04b2022-04-10 10:20:43 +00001424int mbedtls_ssl_tls13_read_public_ecdhe_share( mbedtls_ssl_context *ssl,
1425 const unsigned char *buf,
1426 size_t buf_len )
XiaokangQian7807f9f2022-02-15 10:04:37 +00001427{
XiaokangQian9b5d04b2022-04-10 10:20:43 +00001428 uint8_t *p = (uint8_t*)buf;
XiaokangQiancfd925f2022-04-14 07:10:37 +00001429 const uint8_t *end = buf + buf_len;
XiaokangQian9b5d04b2022-04-10 10:20:43 +00001430 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001431
XiaokangQian9b5d04b2022-04-10 10:20:43 +00001432 /* Get size of the TLS opaque key_exchange field of the KeyShareEntry struct. */
XiaokangQiancfd925f2022-04-14 07:10:37 +00001433 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 2 );
XiaokangQian9b5d04b2022-04-10 10:20:43 +00001434 uint16_t peerkey_len = MBEDTLS_GET_UINT16_BE( p, 0 );
1435 p += 2;
XiaokangQian3207a322022-02-23 03:15:27 +00001436
XiaokangQian9b5d04b2022-04-10 10:20:43 +00001437 /* Check if key size is consistent with given buffer length. */
XiaokangQiancfd925f2022-04-14 07:10:37 +00001438 MBEDTLS_SSL_CHK_BUF_PTR( p, end, peerkey_len );
XiaokangQian9b5d04b2022-04-10 10:20:43 +00001439
1440 /* Store peer's ECDH public key. */
1441 memcpy( handshake->ecdh_psa_peerkey, p, peerkey_len );
1442 handshake->ecdh_psa_peerkey_len = peerkey_len;
1443
XiaokangQian3207a322022-02-23 03:15:27 +00001444 return( 0 );
1445}
Jerry Yu89e103c2022-03-30 22:43:29 +08001446
1447int mbedtls_ssl_tls13_generate_and_write_ecdh_key_exchange(
1448 mbedtls_ssl_context *ssl,
1449 uint16_t named_group,
1450 unsigned char *buf,
1451 unsigned char *end,
1452 size_t *out_len )
1453{
1454 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
1455 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
1456 psa_key_attributes_t key_attributes;
1457 size_t own_pubkey_len;
1458 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
1459 size_t ecdh_bits = 0;
1460
1461 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Perform PSA-based ECDH computation." ) );
1462
1463 /* Convert EC group to PSA key type. */
1464 if( ( handshake->ecdh_psa_type =
1465 mbedtls_psa_parse_tls_ecc_group( named_group, &ecdh_bits ) ) == 0 )
1466 return( MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
1467
1468 ssl->handshake->ecdh_bits = ecdh_bits;
1469
1470 key_attributes = psa_key_attributes_init();
1471 psa_set_key_usage_flags( &key_attributes, PSA_KEY_USAGE_DERIVE );
1472 psa_set_key_algorithm( &key_attributes, PSA_ALG_ECDH );
1473 psa_set_key_type( &key_attributes, handshake->ecdh_psa_type );
1474 psa_set_key_bits( &key_attributes, handshake->ecdh_bits );
1475
1476 /* Generate ECDH private key. */
1477 status = psa_generate_key( &key_attributes,
1478 &handshake->ecdh_psa_privkey );
1479 if( status != PSA_SUCCESS )
1480 {
1481 ret = psa_ssl_status_to_mbedtls( status );
1482 MBEDTLS_SSL_DEBUG_RET( 1, "psa_generate_key", ret );
1483 return( ret );
1484
1485 }
1486
1487 /* Export the public part of the ECDH private key from PSA. */
1488 status = psa_export_public_key( handshake->ecdh_psa_privkey,
1489 buf, (size_t)( end - buf ),
1490 &own_pubkey_len );
1491 if( status != PSA_SUCCESS )
1492 {
1493 ret = psa_ssl_status_to_mbedtls( status );
1494 MBEDTLS_SSL_DEBUG_RET( 1, "psa_export_public_key", ret );
1495 return( ret );
1496
1497 }
1498
1499 *out_len = own_pubkey_len;
1500
1501 return( 0 );
1502}
XiaokangQian9b5d04b2022-04-10 10:20:43 +00001503#endif /* MBEDTLS_ECDH_C */
XiaokangQian7807f9f2022-02-15 10:04:37 +00001504
Jerry Yufb4b6472022-01-27 15:03:26 +08001505#endif /* MBEDTLS_SSL_TLS_C && MBEDTLS_SSL_PROTO_TLS1_3 */