blob: a90ddba43ee0bd9c5dbc1fd3efa3c85184cfea65 [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 *
358 * STATE HANDLING: Incoming Certificate, client-side only currently.
359 *
360 */
361
362/*
Xiaofei Bai947571e2021-09-29 09:12:03 +0000363 * Implementation
364 */
365
Xiaofei Bai947571e2021-09-29 09:12:03 +0000366#if defined(MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED)
367#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
368/*
369 * Structure of Certificate message:
370 *
371 * enum {
372 * X509(0),
373 * RawPublicKey(2),
374 * (255)
375 * } CertificateType;
376 *
377 * struct {
378 * select (certificate_type) {
379 * case RawPublicKey:
380 * * From RFC 7250 ASN.1_subjectPublicKeyInfo *
381 * opaque ASN1_subjectPublicKeyInfo<1..2^24-1>;
382 * case X509:
383 * opaque cert_data<1..2^24-1>;
384 * };
385 * Extension extensions<0..2^16-1>;
386 * } CertificateEntry;
387 *
388 * struct {
389 * opaque certificate_request_context<0..2^8-1>;
390 * CertificateEntry certificate_list<0..2^24-1>;
391 * } Certificate;
392 *
393 */
Xiaofei Bai79595ac2021-10-26 07:16:45 +0000394
395/* Parse certificate chain send by the server. */
Xiaofei Bai947571e2021-09-29 09:12:03 +0000396static int ssl_tls13_parse_certificate( mbedtls_ssl_context *ssl,
397 const unsigned char *buf,
398 const unsigned char *end )
399{
400 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
401 size_t certificate_request_context_len = 0;
402 size_t certificate_list_len = 0;
403 const unsigned char *p = buf;
404 const unsigned char *certificate_list_end;
405
406 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 4 );
407 certificate_request_context_len = p[0];
Jerry Yub640bf62021-10-29 10:05:32 +0800408 certificate_list_len = MBEDTLS_GET_UINT24_BE( p, 1 );
Xiaofei Bai79595ac2021-10-26 07:16:45 +0000409 p += 4;
Xiaofei Bai947571e2021-09-29 09:12:03 +0000410
411 /* In theory, the certificate list can be up to 2^24 Bytes, but we don't
412 * support anything beyond 2^16 = 64K.
413 */
Xiaofei Bai79595ac2021-10-26 07:16:45 +0000414 if( ( certificate_request_context_len != 0 ) ||
415 ( certificate_list_len >= 0x10000 ) )
Xiaofei Bai947571e2021-09-29 09:12:03 +0000416 {
417 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
418 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR,
419 MBEDTLS_ERR_SSL_DECODE_ERROR );
420 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
421 }
422
423 /* In case we tried to reuse a session but it failed */
424 if( ssl->session_negotiate->peer_cert != NULL )
425 {
426 mbedtls_x509_crt_free( ssl->session_negotiate->peer_cert );
427 mbedtls_free( ssl->session_negotiate->peer_cert );
428 }
429
430 if( ( ssl->session_negotiate->peer_cert =
431 mbedtls_calloc( 1, sizeof( mbedtls_x509_crt ) ) ) == NULL )
432 {
433 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc( %" MBEDTLS_PRINTF_SIZET " bytes ) failed",
434 sizeof( mbedtls_x509_crt ) ) );
435 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR,
436 MBEDTLS_ERR_SSL_ALLOC_FAILED );
437 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
438 }
439
440 mbedtls_x509_crt_init( ssl->session_negotiate->peer_cert );
441
Xiaofei Bai947571e2021-09-29 09:12:03 +0000442 certificate_list_end = p + certificate_list_len;
Xiaofei Bai79595ac2021-10-26 07:16:45 +0000443 while( p < certificate_list_end )
Xiaofei Bai947571e2021-09-29 09:12:03 +0000444 {
445 size_t cert_data_len, extensions_len;
446
Xiaofei Bai79595ac2021-10-26 07:16:45 +0000447 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, certificate_list_end, 3 );
Xiaofei Baif93cbd22021-10-29 02:39:30 +0000448 cert_data_len = MBEDTLS_GET_UINT24_BE( p, 0 );
Xiaofei Bai947571e2021-09-29 09:12:03 +0000449 p += 3;
450
451 /* In theory, the CRT can be up to 2^24 Bytes, but we don't support
452 * anything beyond 2^16 = 64K. Otherwise as in the TLS 1.2 code,
453 * check that we have a minimum of 128 bytes of data, this is not
454 * clear why we need that though.
455 */
456 if( ( cert_data_len < 128 ) || ( cert_data_len >= 0x10000 ) )
Xiaofei Bai79595ac2021-10-26 07:16:45 +0000457 {
Xiaofei Bai947571e2021-09-29 09:12:03 +0000458 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad Certificate message" ) );
459 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR,
460 MBEDTLS_ERR_SSL_DECODE_ERROR );
461 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
462 }
463
Xiaofei Bai79595ac2021-10-26 07:16:45 +0000464 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, certificate_list_end, cert_data_len );
Xiaofei Bai947571e2021-09-29 09:12:03 +0000465 ret = mbedtls_x509_crt_parse_der( ssl->session_negotiate->peer_cert,
466 p, cert_data_len );
467
468 switch( ret )
469 {
470 case 0: /*ok*/
471 break;
472 case MBEDTLS_ERR_X509_UNKNOWN_SIG_ALG + MBEDTLS_ERR_OID_NOT_FOUND:
473 /* Ignore certificate with an unknown algorithm: maybe a
474 prior certificate was already trusted. */
475 break;
476
477 case MBEDTLS_ERR_X509_ALLOC_FAILED:
478 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR,
479 MBEDTLS_ERR_X509_ALLOC_FAILED );
480 MBEDTLS_SSL_DEBUG_RET( 1, " mbedtls_x509_crt_parse_der", ret );
481 return( ret );
482
483 case MBEDTLS_ERR_X509_UNKNOWN_VERSION:
484 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT,
485 MBEDTLS_ERR_X509_UNKNOWN_VERSION );
486 MBEDTLS_SSL_DEBUG_RET( 1, " mbedtls_x509_crt_parse_der", ret );
487 return( ret );
488
489 default:
490 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_BAD_CERT,
491 ret );
492 MBEDTLS_SSL_DEBUG_RET( 1, " mbedtls_x509_crt_parse_der", ret );
493 return( ret );
494 }
495
496 p += cert_data_len;
497
498 /* Certificate extensions length */
499 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, certificate_list_end, 2 );
500 extensions_len = MBEDTLS_GET_UINT16_BE( p, 0 );
501 p += 2;
Xiaofei Bai79595ac2021-10-26 07:16:45 +0000502 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, certificate_list_end, extensions_len );
Xiaofei Bai947571e2021-09-29 09:12:03 +0000503 p += extensions_len;
504 }
505
506 /* Check that all the message is consumed. */
507 if( p != end )
508 {
509 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad Certificate message" ) );
510 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR, \
511 MBEDTLS_ERR_SSL_DECODE_ERROR );
512 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
513 }
514
515 MBEDTLS_SSL_DEBUG_CRT( 3, "peer certificate", ssl->session_negotiate->peer_cert );
516
517 return( ret );
518}
519#else
520static int ssl_tls13_parse_certificate( mbedtls_ssl_context *ssl,
521 const unsigned char *buf,
522 const unsigned char *end )
523{
524 ((void) ssl);
525 ((void) buf);
526 ((void) end);
527 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
528}
529#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
530#endif /* MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED */
531
532#if defined(MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED)
533#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Xiaofei Bai79595ac2021-10-26 07:16:45 +0000534/* Validate certificate chain sent by the server. */
Xiaofei Bai947571e2021-09-29 09:12:03 +0000535static int ssl_tls13_validate_certificate( mbedtls_ssl_context *ssl )
536{
537 int ret = 0;
538 mbedtls_x509_crt *ca_chain;
539 mbedtls_x509_crl *ca_crl;
Xiaofei Baiff456022021-10-28 06:50:17 +0000540 uint32_t verify_result = 0;
Xiaofei Bai947571e2021-09-29 09:12:03 +0000541
542#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
543 if( ssl->handshake->sni_ca_chain != NULL )
544 {
545 ca_chain = ssl->handshake->sni_ca_chain;
546 ca_crl = ssl->handshake->sni_ca_crl;
547 }
548 else
549#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
550 {
551 ca_chain = ssl->conf->ca_chain;
552 ca_crl = ssl->conf->ca_crl;
553 }
554
555 /*
556 * Main check: verify certificate
557 */
558 ret = mbedtls_x509_crt_verify_with_profile(
559 ssl->session_negotiate->peer_cert,
560 ca_chain, ca_crl,
561 ssl->conf->cert_profile,
562 ssl->hostname,
Xiaofei Baiff456022021-10-28 06:50:17 +0000563 &verify_result,
Xiaofei Bai947571e2021-09-29 09:12:03 +0000564 ssl->conf->f_vrfy, ssl->conf->p_vrfy );
565
566 if( ret != 0 )
567 {
568 MBEDTLS_SSL_DEBUG_RET( 1, "x509_verify_cert", ret );
569 }
570
571 /*
572 * Secondary checks: always done, but change 'ret' only if it was 0
573 */
Xiaofei Bai947571e2021-09-29 09:12:03 +0000574 if( mbedtls_ssl_check_cert_usage( ssl->session_negotiate->peer_cert,
575 ssl->handshake->ciphersuite_info,
576 !ssl->conf->endpoint,
Xiaofei Baiff456022021-10-28 06:50:17 +0000577 &verify_result ) != 0 )
Xiaofei Bai947571e2021-09-29 09:12:03 +0000578 {
579 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate ( usage extensions )" ) );
580 if( ret == 0 )
581 ret = MBEDTLS_ERR_SSL_BAD_CERTIFICATE;
582 }
583
584
585 if( ca_chain == NULL )
586 {
587 MBEDTLS_SSL_DEBUG_MSG( 1, ( "got no CA chain" ) );
588 ret = MBEDTLS_ERR_SSL_CA_CHAIN_REQUIRED;
589 }
590
591 if( ret != 0 )
592 {
593 /* The certificate may have been rejected for several reasons.
594 Pick one and send the corresponding alert. Which alert to send
595 may be a subject of debate in some cases. */
Xiaofei Baiff456022021-10-28 06:50:17 +0000596 if( verify_result & MBEDTLS_X509_BADCERT_OTHER )
Xiaofei Bai947571e2021-09-29 09:12:03 +0000597 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED, ret );
Xiaofei Baiff456022021-10-28 06:50:17 +0000598 else if( verify_result & MBEDTLS_X509_BADCERT_CN_MISMATCH )
Xiaofei Bai947571e2021-09-29 09:12:03 +0000599 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_BAD_CERT, ret );
Xiaofei Baif93cbd22021-10-29 02:39:30 +0000600 else if( verify_result & ( MBEDTLS_X509_BADCERT_KEY_USAGE |
601 MBEDTLS_X509_BADCERT_EXT_KEY_USAGE |
602 MBEDTLS_X509_BADCERT_NS_CERT_TYPE |
603 MBEDTLS_X509_BADCERT_BAD_PK |
604 MBEDTLS_X509_BADCERT_BAD_KEY ) )
Xiaofei Bai947571e2021-09-29 09:12:03 +0000605 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT, ret );
Xiaofei Baiff456022021-10-28 06:50:17 +0000606 else if( verify_result & MBEDTLS_X509_BADCERT_EXPIRED )
Xiaofei Bai947571e2021-09-29 09:12:03 +0000607 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_CERT_EXPIRED, ret );
Xiaofei Baiff456022021-10-28 06:50:17 +0000608 else if( verify_result & MBEDTLS_X509_BADCERT_REVOKED )
Xiaofei Bai947571e2021-09-29 09:12:03 +0000609 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_CERT_REVOKED, ret );
Xiaofei Baiff456022021-10-28 06:50:17 +0000610 else if( verify_result & MBEDTLS_X509_BADCERT_NOT_TRUSTED )
Xiaofei Bai947571e2021-09-29 09:12:03 +0000611 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_UNKNOWN_CA, ret );
612 else
613 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_CERT_UNKNOWN, ret );
614 }
615
616#if defined(MBEDTLS_DEBUG_C)
Xiaofei Baiff456022021-10-28 06:50:17 +0000617 if( verify_result != 0 )
Xiaofei Bai947571e2021-09-29 09:12:03 +0000618 {
619 MBEDTLS_SSL_DEBUG_MSG( 3, ( "! Certificate verification flags %08x",
Jerry Yu83bb1312021-10-28 22:16:33 +0800620 (unsigned int) verify_result ) );
Xiaofei Bai947571e2021-09-29 09:12:03 +0000621 }
622 else
623 {
624 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Certificate verification flags clear" ) );
625 }
626#endif /* MBEDTLS_DEBUG_C */
627
Xiaofei Baiff456022021-10-28 06:50:17 +0000628 ssl->session_negotiate->verify_result = verify_result;
Xiaofei Bai947571e2021-09-29 09:12:03 +0000629 return( ret );
630}
631#else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
632static int ssl_tls13_validate_certificate( mbedtls_ssl_context *ssl )
633{
634 ((void) ssl);
635 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
636}
637#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
638#endif /* MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED */
639
Xiaofei Bai79595ac2021-10-26 07:16:45 +0000640int mbedtls_ssl_tls13_process_certificate( mbedtls_ssl_context *ssl )
Xiaofei Bai947571e2021-09-29 09:12:03 +0000641{
Xiaofei Bai79595ac2021-10-26 07:16:45 +0000642 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
643 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) );
644
645#if defined(MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED)
646 unsigned char *buf;
647 size_t buf_len;
648
Xiaofei Bai746f9482021-11-12 08:53:56 +0000649 MBEDTLS_SSL_PROC_CHK( mbedtls_ssl_tls13_fetch_handshake_msg(
Xiaofei Bai79595ac2021-10-26 07:16:45 +0000650 ssl, MBEDTLS_SSL_HS_CERTIFICATE,
651 &buf, &buf_len ) );
652
653 /* Parse the certificate chain sent by the peer. */
654 MBEDTLS_SSL_PROC_CHK( ssl_tls13_parse_certificate( ssl, buf, buf + buf_len ) );
655 /* Validate the certificate chain and set the verification results. */
656 MBEDTLS_SSL_PROC_CHK( ssl_tls13_validate_certificate( ssl ) );
657
Ronald Cron8f6d39a2022-03-10 18:56:50 +0100658 mbedtls_ssl_add_hs_msg_to_checksum( ssl, MBEDTLS_SSL_HS_CERTIFICATE,
659 buf, buf_len );
Xiaofei Bai79595ac2021-10-26 07:16:45 +0000660
Xiaofei Bai79595ac2021-10-26 07:16:45 +0000661cleanup:
662
663 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse certificate" ) );
Xiaofei Bai10aeec02021-10-26 09:50:08 +0000664#else
665 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
666 ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
667#endif /* MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED */
Xiaofei Bai79595ac2021-10-26 07:16:45 +0000668 return( ret );
Xiaofei Bai947571e2021-09-29 09:12:03 +0000669}
Jerry Yu90f152d2022-01-29 22:12:42 +0800670#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Jerry Yu7399d0d2022-01-30 17:54:19 +0800671/*
672 * enum {
673 * X509(0),
674 * RawPublicKey(2),
675 * (255)
676 * } CertificateType;
677 *
678 * struct {
679 * select (certificate_type) {
680 * case RawPublicKey:
681 * // From RFC 7250 ASN.1_subjectPublicKeyInfo
682 * opaque ASN1_subjectPublicKeyInfo<1..2^24-1>;
683 *
684 * case X509:
685 * opaque cert_data<1..2^24-1>;
686 * };
687 * Extension extensions<0..2^16-1>;
688 * } CertificateEntry;
689 *
690 * struct {
691 * opaque certificate_request_context<0..2^8-1>;
692 * CertificateEntry certificate_list<0..2^24-1>;
693 * } Certificate;
694 */
695static int ssl_tls13_write_certificate_body( mbedtls_ssl_context *ssl,
Jerry Yu3e536442022-02-15 11:05:59 +0800696 unsigned char *buf,
Jerry Yu7399d0d2022-01-30 17:54:19 +0800697 unsigned char *end,
Jerry Yu3e536442022-02-15 11:05:59 +0800698 size_t *out_len )
Jerry Yu5cc35062022-01-28 16:16:08 +0800699{
Jerry Yu5cc35062022-01-28 16:16:08 +0800700 const mbedtls_x509_crt *crt = mbedtls_ssl_own_cert( ssl );
Jerry Yu3e536442022-02-15 11:05:59 +0800701 unsigned char *p = buf;
Jerry Yuc8d8d4e2022-02-18 12:10:03 +0800702 unsigned char *certificate_request_context =
703 ssl->handshake->certificate_request_context;
704 unsigned char certificate_request_context_len =
705 ssl->handshake->certificate_request_context_len;
706 unsigned char *p_certificate_list_len;
Jerry Yu5cc35062022-01-28 16:16:08 +0800707
Jerry Yu5cc35062022-01-28 16:16:08 +0800708
Jerry Yu3391ac02022-02-16 11:21:37 +0800709 /* ...
710 * opaque certificate_request_context<0..2^8-1>;
711 * ...
712 */
Jerry Yuc8d8d4e2022-02-18 12:10:03 +0800713 MBEDTLS_SSL_CHK_BUF_PTR( p, end, certificate_request_context_len + 1 );
714 *p++ = certificate_request_context_len;
715 if( certificate_request_context_len > 0 )
Jerry Yu537530d2022-02-15 14:00:57 +0800716 {
Jerry Yuc8d8d4e2022-02-18 12:10:03 +0800717 memcpy( p, certificate_request_context, certificate_request_context_len );
718 p += certificate_request_context_len;
Jerry Yu537530d2022-02-15 14:00:57 +0800719 }
720
Jerry Yu3391ac02022-02-16 11:21:37 +0800721 /* ...
722 * CertificateEntry certificate_list<0..2^24-1>;
723 * ...
724 */
Jerry Yu3e536442022-02-15 11:05:59 +0800725 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 3 );
Jerry Yuc8d8d4e2022-02-18 12:10:03 +0800726 p_certificate_list_len = p;
Jerry Yu3e536442022-02-15 11:05:59 +0800727 p += 3;
728
Jerry Yu7399d0d2022-01-30 17:54:19 +0800729 MBEDTLS_SSL_DEBUG_CRT( 3, "own certificate", crt );
Jerry Yu5cc35062022-01-28 16:16:08 +0800730
Jerry Yuc8d8d4e2022-02-18 12:10:03 +0800731 while( crt != NULL )
Jerry Yu5cc35062022-01-28 16:16:08 +0800732 {
Jerry Yu7399d0d2022-01-30 17:54:19 +0800733 size_t cert_data_len = crt->raw.len;
Jerry Yu5cc35062022-01-28 16:16:08 +0800734
Jerry Yu7399d0d2022-01-30 17:54:19 +0800735 MBEDTLS_SSL_CHK_BUF_PTR( p, end, cert_data_len + 3 + 2 );
736 MBEDTLS_PUT_UINT24_BE( cert_data_len, p, 0 );
737 p += 3;
Jerry Yu5cc35062022-01-28 16:16:08 +0800738
Jerry Yu7399d0d2022-01-30 17:54:19 +0800739 memcpy( p, crt->raw.p, cert_data_len );
740 p += cert_data_len;
741 crt = crt->next;
Jerry Yu5cc35062022-01-28 16:16:08 +0800742
743 /* Currently, we don't have any certificate extensions defined.
744 * Hence, we are sending an empty extension with length zero.
745 */
Jerry Yu7399d0d2022-01-30 17:54:19 +0800746 MBEDTLS_PUT_UINT24_BE( 0, p, 0 );
747 p += 2;
Jerry Yu5cc35062022-01-28 16:16:08 +0800748 }
Jerry Yu5cc35062022-01-28 16:16:08 +0800749
Jerry Yuc8d8d4e2022-02-18 12:10:03 +0800750 MBEDTLS_PUT_UINT24_BE( p - p_certificate_list_len - 3,
751 p_certificate_list_len, 0 );
Jerry Yu7399d0d2022-01-30 17:54:19 +0800752
Jerry Yu3e536442022-02-15 11:05:59 +0800753 *out_len = p - buf;
Jerry Yu5cc35062022-01-28 16:16:08 +0800754
755 return( 0 );
756}
Jerry Yu5cc35062022-01-28 16:16:08 +0800757
Jerry Yu3e536442022-02-15 11:05:59 +0800758int mbedtls_ssl_tls13_write_certificate( mbedtls_ssl_context *ssl )
Jerry Yu5cc35062022-01-28 16:16:08 +0800759{
760 int ret;
Ronald Cron5bb8fc82022-03-09 07:00:13 +0100761 unsigned char *buf;
762 size_t buf_len, msg_len;
763
Jerry Yu5cc35062022-01-28 16:16:08 +0800764 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write certificate" ) );
765
Ronald Cron8f6d39a2022-03-10 18:56:50 +0100766 MBEDTLS_SSL_PROC_CHK( mbedtls_ssl_start_handshake_msg( ssl,
Ronald Cron5bb8fc82022-03-09 07:00:13 +0100767 MBEDTLS_SSL_HS_CERTIFICATE, &buf, &buf_len ) );
Jerry Yu5cc35062022-01-28 16:16:08 +0800768
Ronald Cron5bb8fc82022-03-09 07:00:13 +0100769 MBEDTLS_SSL_PROC_CHK( ssl_tls13_write_certificate_body( ssl,
770 buf,
771 buf + buf_len,
772 &msg_len ) );
Jerry Yu5cc35062022-01-28 16:16:08 +0800773
Ronald Cron8f6d39a2022-03-10 18:56:50 +0100774 mbedtls_ssl_add_hs_msg_to_checksum( ssl, MBEDTLS_SSL_HS_CERTIFICATE,
775 buf, msg_len );
Jerry Yu5cc35062022-01-28 16:16:08 +0800776
Ronald Cron8f6d39a2022-03-10 18:56:50 +0100777 MBEDTLS_SSL_PROC_CHK( mbedtls_ssl_finish_handshake_msg(
Ronald Cron5bb8fc82022-03-09 07:00:13 +0100778 ssl, buf_len, msg_len ) );
Jerry Yu5cc35062022-01-28 16:16:08 +0800779cleanup:
780
781 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write certificate" ) );
782 return( ret );
783}
784
Jerry Yu3e536442022-02-15 11:05:59 +0800785/*
786 * STATE HANDLING: Output Certificate Verify
787 */
Jerry Yue91a51a2022-03-22 21:42:50 +0800788static int ssl_tls13_get_sig_alg_from_pk( mbedtls_ssl_context *ssl,
789 mbedtls_pk_context *own_key,
Jerry Yu8c338862022-03-23 13:34:04 +0800790 uint16_t *algorithm )
Jerry Yu67eced02022-02-25 13:37:36 +0800791{
792 mbedtls_pk_type_t sig = mbedtls_ssl_sig_from_pk( own_key );
793 /* Determine the size of the key */
794 size_t own_key_size = mbedtls_pk_get_bitlen( own_key );
Jerry Yue91a51a2022-03-22 21:42:50 +0800795 *algorithm = MBEDTLS_TLS1_3_SIG_NONE;
Jerry Yu67eced02022-02-25 13:37:36 +0800796 ((void) own_key_size);
797
798 switch( sig )
799 {
800#if defined(MBEDTLS_ECDSA_C)
801 case MBEDTLS_SSL_SIG_ECDSA:
802 switch( own_key_size )
803 {
804 case 256:
Jerry Yue91a51a2022-03-22 21:42:50 +0800805 *algorithm = MBEDTLS_TLS1_3_SIG_ECDSA_SECP256R1_SHA256;
Jerry Yue91a51a2022-03-22 21:42:50 +0800806 return( 0 );
Jerry Yu67eced02022-02-25 13:37:36 +0800807 case 384:
Jerry Yue91a51a2022-03-22 21:42:50 +0800808 *algorithm = MBEDTLS_TLS1_3_SIG_ECDSA_SECP384R1_SHA384;
Jerry Yue91a51a2022-03-22 21:42:50 +0800809 return( 0 );
Jerry Yu67eced02022-02-25 13:37:36 +0800810 case 521:
Jerry Yue91a51a2022-03-22 21:42:50 +0800811 *algorithm = MBEDTLS_TLS1_3_SIG_ECDSA_SECP521R1_SHA512;
Jerry Yue91a51a2022-03-22 21:42:50 +0800812 return( 0 );
Jerry Yu67eced02022-02-25 13:37:36 +0800813 default:
814 MBEDTLS_SSL_DEBUG_MSG( 3,
815 ( "unknown key size: %"
816 MBEDTLS_PRINTF_SIZET " bits",
817 own_key_size ) );
818 break;
819 }
820 break;
821#endif /* MBEDTLS_ECDSA_C */
822
Jerry Yucef3f332022-03-22 23:00:13 +0800823#if defined(MBEDTLS_RSA_C)
Jerry Yu67eced02022-02-25 13:37:36 +0800824 case MBEDTLS_SSL_SIG_RSA:
Jerry Yucef3f332022-03-22 23:00:13 +0800825#if defined(MBEDTLS_PKCS1_V21)
826#if defined(MBEDTLS_SHA256_C)
Jerry Yu67eced02022-02-25 13:37:36 +0800827 if( own_key_size <= 2048 &&
828 mbedtls_ssl_sig_alg_is_received( ssl,
829 MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA256 ) )
830 {
Jerry Yue91a51a2022-03-22 21:42:50 +0800831 *algorithm = MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA256;
Jerry Yue91a51a2022-03-22 21:42:50 +0800832 return( 0 );
Jerry Yu67eced02022-02-25 13:37:36 +0800833 }
Jerry Yucef3f332022-03-22 23:00:13 +0800834 else
835#endif /* MBEDTLS_SHA256_C */
836#if defined(MBEDTLS_SHA384_C)
837 if( own_key_size <= 3072 &&
838 mbedtls_ssl_sig_alg_is_received( ssl,
Jerry Yu67eced02022-02-25 13:37:36 +0800839 MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA384 ) )
840 {
Jerry Yue91a51a2022-03-22 21:42:50 +0800841 *algorithm = MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA384;
Jerry Yue91a51a2022-03-22 21:42:50 +0800842 return( 0 );
Jerry Yu67eced02022-02-25 13:37:36 +0800843 }
Jerry Yucef3f332022-03-22 23:00:13 +0800844 else
845#endif /* MBEDTLS_SHA384_C */
846#if defined(MBEDTLS_SHA512_C)
847 if( own_key_size <= 4096 &&
848 mbedtls_ssl_sig_alg_is_received( ssl,
Jerry Yu67eced02022-02-25 13:37:36 +0800849 MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA512 ) )
850 {
Jerry Yue91a51a2022-03-22 21:42:50 +0800851 *algorithm = MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA512;
Jerry Yue91a51a2022-03-22 21:42:50 +0800852 return( 0 );
Jerry Yu67eced02022-02-25 13:37:36 +0800853 }
Jerry Yucef3f332022-03-22 23:00:13 +0800854 else
855#endif /* MBEDTLS_SHA512_C */
856#endif /* MBEDTLS_PKCS1_V21 */
857#if defined(MBEDTLS_PKCS1_V15)
858#if defined(MBEDTLS_SHA256_C)
859 if( own_key_size <= 2048 &&
860 mbedtls_ssl_sig_alg_is_received( ssl,
861 MBEDTLS_TLS1_3_SIG_RSA_PKCS1_SHA256 ) )
862 {
863 *algorithm = MBEDTLS_TLS1_3_SIG_RSA_PKCS1_SHA256;
Jerry Yucef3f332022-03-22 23:00:13 +0800864 return( 0 );
865 }
866 else
867#endif /* MBEDTLS_SHA256_C */
868#if defined(MBEDTLS_SHA384_C)
869 if( own_key_size <= 3072 &&
870 mbedtls_ssl_sig_alg_is_received( ssl,
871 MBEDTLS_TLS1_3_SIG_RSA_PKCS1_SHA384 ) )
872 {
873 *algorithm = MBEDTLS_TLS1_3_SIG_RSA_PKCS1_SHA384;
Jerry Yucef3f332022-03-22 23:00:13 +0800874 return( 0 );
875 }
876 else
877#endif /* MBEDTLS_SHA384_C */
878#if defined(MBEDTLS_SHA512_C)
879 if( own_key_size <= 4096 &&
880 mbedtls_ssl_sig_alg_is_received( ssl,
881 MBEDTLS_TLS1_3_SIG_RSA_PKCS1_SHA512 ) )
882 {
883 *algorithm = MBEDTLS_TLS1_3_SIG_RSA_PKCS1_SHA512;
Jerry Yucef3f332022-03-22 23:00:13 +0800884 return( 0 );
885 }
886 else
887#endif /* MBEDTLS_SHA512_C */
888#endif /* MBEDTLS_PKCS1_V15 */
889 {
890 MBEDTLS_SSL_DEBUG_MSG( 3,
891 ( "unknown key size: %"
892 MBEDTLS_PRINTF_SIZET " bits",
893 own_key_size ) );
894 }
Jerry Yu67eced02022-02-25 13:37:36 +0800895 break;
Jerry Yucef3f332022-03-22 23:00:13 +0800896#endif /* MBEDTLS_RSA_C */
Jerry Yu67eced02022-02-25 13:37:36 +0800897 default:
898 MBEDTLS_SSL_DEBUG_MSG( 1,
Andrzej Kurek5c65c572022-04-13 14:28:52 -0400899 ( "unknown signature type : %u", sig ) );
Jerry Yu67eced02022-02-25 13:37:36 +0800900 break;
901 }
Jerry Yue91a51a2022-03-22 21:42:50 +0800902 return( -1 );
Jerry Yu67eced02022-02-25 13:37:36 +0800903}
904
Jerry Yu3e536442022-02-15 11:05:59 +0800905static int ssl_tls13_write_certificate_verify_body( mbedtls_ssl_context *ssl,
906 unsigned char *buf,
907 unsigned char *end,
908 size_t *out_len )
Jerry Yu8511f122022-01-29 10:01:04 +0800909{
910 int ret;
Jerry Yu3e536442022-02-15 11:05:59 +0800911 unsigned char *p = buf;
Jerry Yu8511f122022-01-29 10:01:04 +0800912 mbedtls_pk_context *own_key;
Jerry Yu3e536442022-02-15 11:05:59 +0800913
Jerry Yu8511f122022-01-29 10:01:04 +0800914 unsigned char handshake_hash[ MBEDTLS_TLS1_3_MD_MAX_SIZE ];
915 size_t handshake_hash_len;
Jerry Yu3e536442022-02-15 11:05:59 +0800916 unsigned char verify_buffer[ SSL_VERIFY_STRUCT_MAX_SIZE ];
917 size_t verify_buffer_len;
Jerry Yu67eced02022-02-25 13:37:36 +0800918 mbedtls_pk_type_t pk_type = MBEDTLS_PK_NONE;
Jerry Yu67eced02022-02-25 13:37:36 +0800919 mbedtls_md_type_t md_alg = MBEDTLS_MD_NONE;
pespacek34935872022-05-20 15:43:32 +0200920 psa_algorithm_t psa_algorithm = PSA_ALG_NONE;
Jerry Yu78272072022-02-22 10:28:13 +0800921 uint16_t algorithm = MBEDTLS_TLS1_3_SIG_NONE;
Jerry Yu3e536442022-02-15 11:05:59 +0800922 size_t signature_len = 0;
Jerry Yu3391ac02022-02-16 11:21:37 +0800923 unsigned char verify_hash[ MBEDTLS_MD_MAX_SIZE ];
Jerry Yu3e536442022-02-15 11:05:59 +0800924 size_t verify_hash_len;
pespacekb06acd72022-06-07 13:07:21 +0200925 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
926
Jerry Yu0b7b1012022-02-23 12:23:05 +0800927 *out_len = 0;
928
Jerry Yu3e536442022-02-15 11:05:59 +0800929 own_key = mbedtls_ssl_own_key( ssl );
930 if( own_key == NULL )
Jerry Yu8511f122022-01-29 10:01:04 +0800931 {
Jerry Yu3e536442022-02-15 11:05:59 +0800932 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
933 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Jerry Yu8511f122022-01-29 10:01:04 +0800934 }
935
Jerry Yu8511f122022-01-29 10:01:04 +0800936 ret = mbedtls_ssl_get_handshake_transcript( ssl,
937 ssl->handshake->ciphersuite_info->mac,
938 handshake_hash,
939 sizeof( handshake_hash ),
940 &handshake_hash_len );
941 if( ret != 0 )
942 return( ret );
943
944 MBEDTLS_SSL_DEBUG_BUF( 3, "handshake hash",
945 handshake_hash,
946 handshake_hash_len);
947
Jerry Yu8511f122022-01-29 10:01:04 +0800948 ssl_tls13_create_verify_structure( handshake_hash, handshake_hash_len,
949 verify_buffer, &verify_buffer_len,
950 ssl->conf->endpoint );
951
952 /*
953 * struct {
954 * SignatureScheme algorithm;
955 * opaque signature<0..2^16-1>;
956 * } CertificateVerify;
957 */
Jerry Yu8c338862022-03-23 13:34:04 +0800958 ret = ssl_tls13_get_sig_alg_from_pk( ssl, own_key, &algorithm );
Jerry Yue91a51a2022-03-22 21:42:50 +0800959 if( ret != 0 || ! mbedtls_ssl_sig_alg_is_received( ssl, algorithm ) )
Jerry Yu8511f122022-01-29 10:01:04 +0800960 {
Jerry Yud66409a2022-02-18 16:42:24 +0800961 MBEDTLS_SSL_DEBUG_MSG( 1,
Jerry Yu2124d052022-02-18 21:07:18 +0800962 ( "signature algorithm not in received or offered list." ) );
Jerry Yu67eced02022-02-25 13:37:36 +0800963
964 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Signature algorithm is %s",
965 mbedtls_ssl_sig_alg_to_str( algorithm ) ) );
966
Jerry Yu71f36f12022-02-23 17:34:29 +0800967 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE,
Jerry Yud66409a2022-02-18 16:42:24 +0800968 MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
Jerry Yu3391ac02022-02-16 11:21:37 +0800969 return( MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
Jerry Yu8511f122022-01-29 10:01:04 +0800970 }
971
Jerry Yu6c6f1022022-03-25 11:09:50 +0800972 if( mbedtls_ssl_tls13_get_pk_type_and_md_alg_from_sig_alg(
973 algorithm, &pk_type, &md_alg ) != 0 )
Jerry Yu8c338862022-03-23 13:34:04 +0800974 {
Jerry Yuf8aa9a42022-03-23 20:40:28 +0800975 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Jerry Yu8c338862022-03-23 13:34:04 +0800976 }
977
Jerry Yu3391ac02022-02-16 11:21:37 +0800978 /* Check there is space for the algorithm identifier (2 bytes) and the
979 * signature length (2 bytes).
980 */
981 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 4 );
Jerry Yu3e536442022-02-15 11:05:59 +0800982 MBEDTLS_PUT_UINT16_BE( algorithm, p, 0 );
983 p += 2;
Jerry Yu8511f122022-01-29 10:01:04 +0800984
985 /* Hash verify buffer with indicated hash function */
pespacek34935872022-05-20 15:43:32 +0200986 psa_algorithm = mbedtls_psa_translate_md( md_alg );
Jerry Yu8511f122022-01-29 10:01:04 +0800987
pespacekb06acd72022-06-07 13:07:21 +0200988 status = psa_hash_compute( psa_algorithm,
989 verify_buffer,
990 verify_buffer_len,
991 verify_hash,sizeof( verify_hash ),
992 &verify_hash_len );
993 if( status != PSA_SUCCESS )
pespacek670913f2022-06-07 10:53:39 +0200994 return( psa_ssl_status_to_mbedtls( status ) );
Jerry Yu8511f122022-01-29 10:01:04 +0800995
Jerry Yu8511f122022-01-29 10:01:04 +0800996 MBEDTLS_SSL_DEBUG_BUF( 3, "verify hash", verify_hash, verify_hash_len );
997
Jerry Yu8beb9e12022-03-12 16:23:53 +0800998 if( ( ret = mbedtls_pk_sign_ext( pk_type, own_key,
999 md_alg, verify_hash, verify_hash_len,
Jerry Yu67eced02022-02-25 13:37:36 +08001000 p + 2, (size_t)( end - ( p + 2 ) ), &signature_len,
1001 ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 )
Jerry Yu8511f122022-01-29 10:01:04 +08001002 {
1003 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_pk_sign", ret );
1004 return( ret );
1005 }
Jerry Yu8511f122022-01-29 10:01:04 +08001006
Jerry Yu3e536442022-02-15 11:05:59 +08001007 MBEDTLS_PUT_UINT16_BE( signature_len, p, 0 );
1008 p += 2 + signature_len;
Jerry Yu8511f122022-01-29 10:01:04 +08001009
Jerry Yu3e536442022-02-15 11:05:59 +08001010 *out_len = (size_t)( p - buf );
1011
Jerry Yu8511f122022-01-29 10:01:04 +08001012 return( ret );
1013}
Jerry Yu8511f122022-01-29 10:01:04 +08001014
Jerry Yu3e536442022-02-15 11:05:59 +08001015int mbedtls_ssl_tls13_write_certificate_verify( mbedtls_ssl_context *ssl )
Jerry Yu8511f122022-01-29 10:01:04 +08001016{
1017 int ret = 0;
Jerry Yuca133a32022-02-15 14:22:05 +08001018 unsigned char *buf;
1019 size_t buf_len, msg_len;
1020
Jerry Yu8511f122022-01-29 10:01:04 +08001021 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write certificate verify" ) );
1022
Ronald Cron8f6d39a2022-03-10 18:56:50 +01001023 MBEDTLS_SSL_PROC_CHK( mbedtls_ssl_start_handshake_msg( ssl,
Jerry Yuca133a32022-02-15 14:22:05 +08001024 MBEDTLS_SSL_HS_CERTIFICATE_VERIFY, &buf, &buf_len ) );
Jerry Yu8511f122022-01-29 10:01:04 +08001025
Jerry Yuca133a32022-02-15 14:22:05 +08001026 MBEDTLS_SSL_PROC_CHK( ssl_tls13_write_certificate_verify_body(
1027 ssl, buf, buf + buf_len, &msg_len ) );
Jerry Yu8511f122022-01-29 10:01:04 +08001028
Ronald Cron8f6d39a2022-03-10 18:56:50 +01001029 mbedtls_ssl_add_hs_msg_to_checksum( ssl, MBEDTLS_SSL_HS_CERTIFICATE_VERIFY,
1030 buf, msg_len );
Jerry Yu8511f122022-01-29 10:01:04 +08001031
Ronald Cron8f6d39a2022-03-10 18:56:50 +01001032 MBEDTLS_SSL_PROC_CHK( mbedtls_ssl_finish_handshake_msg(
Jerry Yuca133a32022-02-15 14:22:05 +08001033 ssl, buf_len, msg_len ) );
Jerry Yu8511f122022-01-29 10:01:04 +08001034
1035cleanup:
1036
1037 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write certificate verify" ) );
1038 return( ret );
1039}
1040
Jerry Yu90f152d2022-01-29 22:12:42 +08001041#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
1042
Jerry Yu5cc35062022-01-28 16:16:08 +08001043/*
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001044 *
XiaokangQianc5c39d52021-11-09 11:55:10 +00001045 * STATE HANDLING: Incoming Finished message.
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001046 */
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001047/*
1048 * Implementation
1049 */
1050
XiaokangQianaaa0e192021-11-10 03:07:04 +00001051static int ssl_tls13_preprocess_finished_message( mbedtls_ssl_context *ssl )
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001052{
1053 int ret;
1054
XiaokangQianc5c39d52021-11-09 11:55:10 +00001055 ret = mbedtls_ssl_tls13_calculate_verify_data( ssl,
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001056 ssl->handshake->state_local.finished_in.digest,
1057 sizeof( ssl->handshake->state_local.finished_in.digest ),
1058 &ssl->handshake->state_local.finished_in.digest_len,
XiaokangQianc5c39d52021-11-09 11:55:10 +00001059 ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT ?
XiaokangQianc13f9352021-11-11 06:13:22 +00001060 MBEDTLS_SSL_IS_SERVER : MBEDTLS_SSL_IS_CLIENT );
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001061 if( ret != 0 )
1062 {
XiaokangQianc5c39d52021-11-09 11:55:10 +00001063 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_tls13_calculate_verify_data", ret );
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001064 return( ret );
1065 }
1066
1067 return( 0 );
1068}
1069
XiaokangQianc5c39d52021-11-09 11:55:10 +00001070static int ssl_tls13_parse_finished_message( mbedtls_ssl_context *ssl,
1071 const unsigned char *buf,
1072 const unsigned char *end )
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001073{
XiaokangQian33062842021-11-11 03:37:45 +00001074 /*
1075 * struct {
XiaokangQianc13f9352021-11-11 06:13:22 +00001076 * opaque verify_data[Hash.length];
XiaokangQian33062842021-11-11 03:37:45 +00001077 * } Finished;
1078 */
1079 const unsigned char *expected_verify_data =
1080 ssl->handshake->state_local.finished_in.digest;
1081 size_t expected_verify_data_len =
1082 ssl->handshake->state_local.finished_in.digest_len;
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001083 /* Structural validation */
XiaokangQian33062842021-11-11 03:37:45 +00001084 if( (size_t)( end - buf ) != expected_verify_data_len )
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001085 {
1086 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
1087
1088 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR,
XiaokangQianc13f9352021-11-11 06:13:22 +00001089 MBEDTLS_ERR_SSL_DECODE_ERROR );
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001090 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
1091 }
1092
XiaokangQianc5c39d52021-11-09 11:55:10 +00001093 MBEDTLS_SSL_DEBUG_BUF( 4, "verify_data (self-computed):",
XiaokangQian33062842021-11-11 03:37:45 +00001094 expected_verify_data,
1095 expected_verify_data_len );
XiaokangQianc5c39d52021-11-09 11:55:10 +00001096 MBEDTLS_SSL_DEBUG_BUF( 4, "verify_data (received message):", buf,
XiaokangQian33062842021-11-11 03:37:45 +00001097 expected_verify_data_len );
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001098
1099 /* Semantic validation */
Gabor Mezei685472b2021-11-24 11:17:36 +01001100 if( mbedtls_ct_memcmp( buf,
1101 expected_verify_data,
1102 expected_verify_data_len ) != 0 )
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001103 {
1104 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
1105
XiaokangQian33062842021-11-11 03:37:45 +00001106 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_DECRYPT_ERROR,
XiaokangQianc13f9352021-11-11 06:13:22 +00001107 MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001108 return( MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
1109 }
1110 return( 0 );
1111}
1112
XiaokangQianc13f9352021-11-11 06:13:22 +00001113#if defined(MBEDTLS_SSL_CLI_C)
XiaokangQianaaa0e192021-11-10 03:07:04 +00001114static int ssl_tls13_postprocess_server_finished_message( mbedtls_ssl_context *ssl )
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001115{
XiaokangQian33062842021-11-11 03:37:45 +00001116 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001117 mbedtls_ssl_key_set traffic_keys;
XiaokangQian1aef02e2021-10-28 09:54:34 +00001118 mbedtls_ssl_transform *transform_application = NULL;
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001119
XiaokangQian4cab0242021-10-12 08:43:37 +00001120 ret = mbedtls_ssl_tls13_key_schedule_stage_application( ssl );
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001121 if( ret != 0 )
1122 {
1123 MBEDTLS_SSL_DEBUG_RET( 1,
XiaokangQian4cab0242021-10-12 08:43:37 +00001124 "mbedtls_ssl_tls13_key_schedule_stage_application", ret );
XiaokangQian61bdbbc2021-10-28 08:03:38 +00001125 goto cleanup;
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001126 }
1127
XiaokangQian33062842021-11-11 03:37:45 +00001128 ret = mbedtls_ssl_tls13_generate_application_keys( ssl, &traffic_keys );
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001129 if( ret != 0 )
1130 {
1131 MBEDTLS_SSL_DEBUG_RET( 1,
XiaokangQiand0aa3e92021-11-10 06:17:40 +00001132 "mbedtls_ssl_tls13_generate_application_keys", ret );
XiaokangQian61bdbbc2021-10-28 08:03:38 +00001133 goto cleanup;
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001134 }
1135
1136 transform_application =
1137 mbedtls_calloc( 1, sizeof( mbedtls_ssl_transform ) );
1138 if( transform_application == NULL )
XiaokangQian61bdbbc2021-10-28 08:03:38 +00001139 {
1140 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
1141 goto cleanup;
1142 }
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001143
1144 ret = mbedtls_ssl_tls13_populate_transform(
1145 transform_application,
1146 ssl->conf->endpoint,
1147 ssl->session_negotiate->ciphersuite,
1148 &traffic_keys,
1149 ssl );
1150 if( ret != 0 )
1151 {
1152 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_tls13_populate_transform", ret );
XiaokangQian61bdbbc2021-10-28 08:03:38 +00001153 goto cleanup;
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001154 }
1155
1156 ssl->transform_application = transform_application;
1157
XiaokangQian61bdbbc2021-10-28 08:03:38 +00001158cleanup:
1159
XiaokangQian33062842021-11-11 03:37:45 +00001160 mbedtls_platform_zeroize( &traffic_keys, sizeof( traffic_keys ) );
XiaokangQianc5c39d52021-11-09 11:55:10 +00001161 if( ret != 0 )
XiaokangQian1aef02e2021-10-28 09:54:34 +00001162 {
1163 mbedtls_free( transform_application );
1164 MBEDTLS_SSL_PEND_FATAL_ALERT(
1165 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE,
1166 MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
1167 }
XiaokangQian61bdbbc2021-10-28 08:03:38 +00001168 return( ret );
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001169}
XiaokangQianc13f9352021-11-11 06:13:22 +00001170#endif /* MBEDTLS_SSL_CLI_C */
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001171
XiaokangQiancc90c942021-11-09 12:30:09 +00001172static int ssl_tls13_postprocess_finished_message( mbedtls_ssl_context *ssl )
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001173{
1174
XiaokangQianc13f9352021-11-11 06:13:22 +00001175#if defined(MBEDTLS_SSL_CLI_C)
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001176 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
1177 {
XiaokangQianaaa0e192021-11-10 03:07:04 +00001178 return( ssl_tls13_postprocess_server_finished_message( ssl ) );
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001179 }
XiaokangQianc13f9352021-11-11 06:13:22 +00001180#else
1181 ((void) ssl);
1182#endif /* MBEDTLS_SSL_CLI_C */
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001183
1184 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
1185}
1186
XiaokangQianc5c39d52021-11-09 11:55:10 +00001187int mbedtls_ssl_tls13_process_finished_message( mbedtls_ssl_context *ssl )
1188{
XiaokangQian33062842021-11-11 03:37:45 +00001189 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
XiaokangQianc5c39d52021-11-09 11:55:10 +00001190 unsigned char *buf;
Xiaofei Baieef15042021-11-18 07:29:56 +00001191 size_t buf_len;
XiaokangQianc5c39d52021-11-09 11:55:10 +00001192
XiaokangQiand0aa3e92021-11-10 06:17:40 +00001193 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse finished message" ) );
XiaokangQianc5c39d52021-11-09 11:55:10 +00001194
1195 /* Preprocessing step: Compute handshake digest */
XiaokangQianaaa0e192021-11-10 03:07:04 +00001196 MBEDTLS_SSL_PROC_CHK( ssl_tls13_preprocess_finished_message( ssl ) );
XiaokangQianc5c39d52021-11-09 11:55:10 +00001197
Xiaofei Bai746f9482021-11-12 08:53:56 +00001198 MBEDTLS_SSL_PROC_CHK( mbedtls_ssl_tls13_fetch_handshake_msg( ssl,
XiaokangQianc5c39d52021-11-09 11:55:10 +00001199 MBEDTLS_SSL_HS_FINISHED,
Xiaofei Baieef15042021-11-18 07:29:56 +00001200 &buf, &buf_len ) );
1201 MBEDTLS_SSL_PROC_CHK( ssl_tls13_parse_finished_message( ssl, buf, buf + buf_len ) );
Ronald Cron8f6d39a2022-03-10 18:56:50 +01001202 mbedtls_ssl_add_hs_msg_to_checksum( ssl, MBEDTLS_SSL_HS_FINISHED,
1203 buf, buf_len );
XiaokangQianaaa0e192021-11-10 03:07:04 +00001204 MBEDTLS_SSL_PROC_CHK( ssl_tls13_postprocess_finished_message( ssl ) );
XiaokangQianc5c39d52021-11-09 11:55:10 +00001205
1206cleanup:
1207
XiaokangQiand0aa3e92021-11-10 06:17:40 +00001208 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse finished message" ) );
XiaokangQianc5c39d52021-11-09 11:55:10 +00001209 return( ret );
1210}
1211
XiaokangQian74af2a82021-09-22 07:40:30 +00001212/*
1213 *
XiaokangQiancc90c942021-11-09 12:30:09 +00001214 * STATE HANDLING: Write and send Finished message.
XiaokangQian74af2a82021-09-22 07:40:30 +00001215 *
1216 */
XiaokangQian74af2a82021-09-22 07:40:30 +00001217/*
XiaokangQian35dc6252021-11-11 08:16:19 +00001218 * Implement
XiaokangQian74af2a82021-09-22 07:40:30 +00001219 */
1220
XiaokangQian8773aa02021-11-10 07:33:09 +00001221static int ssl_tls13_prepare_finished_message( mbedtls_ssl_context *ssl )
XiaokangQian74af2a82021-09-22 07:40:30 +00001222{
1223 int ret;
1224
1225 /* Compute transcript of handshake up to now. */
XiaokangQiancc90c942021-11-09 12:30:09 +00001226 ret = mbedtls_ssl_tls13_calculate_verify_data( ssl,
XiaokangQian74af2a82021-09-22 07:40:30 +00001227 ssl->handshake->state_local.finished_out.digest,
1228 sizeof( ssl->handshake->state_local.finished_out.digest ),
1229 &ssl->handshake->state_local.finished_out.digest_len,
1230 ssl->conf->endpoint );
1231
1232 if( ret != 0 )
1233 {
Jerry Yu7ca30542021-12-08 15:57:57 +08001234 MBEDTLS_SSL_DEBUG_RET( 1, "calculate_verify_data failed", ret );
XiaokangQian74af2a82021-09-22 07:40:30 +00001235 return( ret );
1236 }
1237
1238 return( 0 );
1239}
1240
XiaokangQiancc90c942021-11-09 12:30:09 +00001241static int ssl_tls13_finalize_finished_message( mbedtls_ssl_context *ssl )
XiaokangQian74af2a82021-09-22 07:40:30 +00001242{
XiaokangQian0fa66432021-11-15 03:33:57 +00001243 // TODO: Add back resumption keys calculation after MVP.
1244 ((void) ssl);
XiaokangQian74af2a82021-09-22 07:40:30 +00001245
1246 return( 0 );
1247}
1248
XiaokangQian8773aa02021-11-10 07:33:09 +00001249static int ssl_tls13_write_finished_message_body( mbedtls_ssl_context *ssl,
XiaokangQian35dc6252021-11-11 08:16:19 +00001250 unsigned char *buf,
1251 unsigned char *end,
Xiaofei Baid25fab62021-12-02 06:36:27 +00001252 size_t *out_len )
XiaokangQian74af2a82021-09-22 07:40:30 +00001253{
XiaokangQian8773aa02021-11-10 07:33:09 +00001254 size_t verify_data_len = ssl->handshake->state_local.finished_out.digest_len;
XiaokangQian0fa66432021-11-15 03:33:57 +00001255 /*
1256 * struct {
1257 * opaque verify_data[Hash.length];
1258 * } Finished;
1259 */
XiaokangQian8773aa02021-11-10 07:33:09 +00001260 MBEDTLS_SSL_CHK_BUF_PTR( buf, end, verify_data_len );
XiaokangQian74af2a82021-09-22 07:40:30 +00001261
1262 memcpy( buf, ssl->handshake->state_local.finished_out.digest,
XiaokangQian8773aa02021-11-10 07:33:09 +00001263 verify_data_len );
XiaokangQian74af2a82021-09-22 07:40:30 +00001264
Xiaofei Baid25fab62021-12-02 06:36:27 +00001265 *out_len = verify_data_len;
XiaokangQian74af2a82021-09-22 07:40:30 +00001266 return( 0 );
1267}
XiaokangQianc5c39d52021-11-09 11:55:10 +00001268
XiaokangQian35dc6252021-11-11 08:16:19 +00001269/* Main entry point: orchestrates the other functions */
1270int mbedtls_ssl_tls13_write_finished_message( mbedtls_ssl_context *ssl )
1271{
1272 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1273 unsigned char *buf;
1274 size_t buf_len, msg_len;
1275
1276 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write finished message" ) );
1277
XiaokangQiandce82242021-11-15 06:01:26 +00001278 MBEDTLS_SSL_PROC_CHK( ssl_tls13_prepare_finished_message( ssl ) );
1279
Ronald Cron8f6d39a2022-03-10 18:56:50 +01001280 MBEDTLS_SSL_PROC_CHK( mbedtls_ssl_start_handshake_msg( ssl,
XiaokangQian35dc6252021-11-11 08:16:19 +00001281 MBEDTLS_SSL_HS_FINISHED, &buf, &buf_len ) );
1282
1283 MBEDTLS_SSL_PROC_CHK( ssl_tls13_write_finished_message_body(
1284 ssl, buf, buf + buf_len, &msg_len ) );
1285
Ronald Cron8f6d39a2022-03-10 18:56:50 +01001286 mbedtls_ssl_add_hs_msg_to_checksum( ssl, MBEDTLS_SSL_HS_FINISHED,
1287 buf, msg_len );
XiaokangQian35dc6252021-11-11 08:16:19 +00001288
1289 MBEDTLS_SSL_PROC_CHK( ssl_tls13_finalize_finished_message( ssl ) );
Ronald Cron8f6d39a2022-03-10 18:56:50 +01001290 MBEDTLS_SSL_PROC_CHK( mbedtls_ssl_finish_handshake_msg(
1291 ssl, buf_len, msg_len ) );
XiaokangQian35dc6252021-11-11 08:16:19 +00001292cleanup:
1293
1294 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write finished message" ) );
1295 return( ret );
1296}
1297
Jerry Yu378254d2021-10-30 21:44:47 +08001298void mbedtls_ssl_tls13_handshake_wrapup( mbedtls_ssl_context *ssl )
1299{
1300
1301 MBEDTLS_SSL_DEBUG_MSG( 3, ( "=> handshake wrapup" ) );
1302
1303 /*
Jerry Yucfe64f02021-11-15 13:54:06 +08001304 * Free the previous session and switch to the current one.
Jerry Yu378254d2021-10-30 21:44:47 +08001305 */
1306 if( ssl->session )
1307 {
Jerry Yu378254d2021-10-30 21:44:47 +08001308 mbedtls_ssl_session_free( ssl->session );
1309 mbedtls_free( ssl->session );
1310 }
1311 ssl->session = ssl->session_negotiate;
1312 ssl->session_negotiate = NULL;
1313
1314 MBEDTLS_SSL_DEBUG_MSG( 3, ( "<= handshake wrapup" ) );
1315}
1316
Ronald Cron49ad6192021-11-24 16:25:31 +01001317/*
1318 *
1319 * STATE HANDLING: Write ChangeCipherSpec
1320 *
1321 */
1322#if defined(MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE)
Ronald Cron49ad6192021-11-24 16:25:31 +01001323static int ssl_tls13_write_change_cipher_spec_body( mbedtls_ssl_context *ssl,
1324 unsigned char *buf,
1325 unsigned char *end,
1326 size_t *olen )
1327{
1328 ((void) ssl);
1329
1330 MBEDTLS_SSL_CHK_BUF_PTR( buf, end, 1 );
1331 buf[0] = 1;
1332 *olen = 1;
1333
1334 return( 0 );
1335}
1336
Ronald Cron49ad6192021-11-24 16:25:31 +01001337int mbedtls_ssl_tls13_write_change_cipher_spec( mbedtls_ssl_context *ssl )
1338{
1339 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1340
1341 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write change cipher spec" ) );
1342
Ronald Cron49ad6192021-11-24 16:25:31 +01001343 /* Write CCS message */
1344 MBEDTLS_SSL_PROC_CHK( ssl_tls13_write_change_cipher_spec_body(
1345 ssl, ssl->out_msg,
1346 ssl->out_msg + MBEDTLS_SSL_OUT_CONTENT_LEN,
1347 &ssl->out_msglen ) );
1348
1349 ssl->out_msgtype = MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC;
1350
Ronald Cron49ad6192021-11-24 16:25:31 +01001351 /* Dispatch message */
Ronald Cron66dbf912022-02-02 15:33:46 +01001352 MBEDTLS_SSL_PROC_CHK( mbedtls_ssl_write_record( ssl, 0 ) );
Ronald Cron49ad6192021-11-24 16:25:31 +01001353
1354cleanup:
1355
1356 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write change cipher spec" ) );
1357 return( ret );
1358}
1359
1360#endif /* MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE */
1361
XiaokangQian78b1fa72022-01-19 06:56:30 +00001362/* Reset SSL context and update hash for handling HRR.
1363 *
1364 * Replace Transcript-Hash(X) by
1365 * Transcript-Hash( message_hash ||
1366 * 00 00 Hash.length ||
1367 * X )
1368 * A few states of the handshake are preserved, including:
1369 * - session ID
1370 * - session ticket
1371 * - negotiated ciphersuite
1372 */
1373int mbedtls_ssl_reset_transcript_for_hrr( mbedtls_ssl_context *ssl )
1374{
1375 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1376 unsigned char hash_transcript[ MBEDTLS_MD_MAX_SIZE + 4 ];
XiaokangQian0ece9982022-01-24 08:56:23 +00001377 size_t hash_len;
XiaokangQian78b1fa72022-01-19 06:56:30 +00001378 const mbedtls_ssl_ciphersuite_t *ciphersuite_info;
1379 uint16_t cipher_suite = ssl->session_negotiate->ciphersuite;
1380 ciphersuite_info = mbedtls_ssl_ciphersuite_from_id( cipher_suite );
1381
1382 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Reset SSL session for HRR" ) );
1383
XiaokangQian0ece9982022-01-24 08:56:23 +00001384 ret = mbedtls_ssl_get_handshake_transcript( ssl, ciphersuite_info->mac,
1385 hash_transcript + 4,
1386 MBEDTLS_MD_MAX_SIZE,
1387 &hash_len );
1388 if( ret != 0 )
1389 {
1390 MBEDTLS_SSL_DEBUG_RET( 4, "mbedtls_ssl_get_handshake_transcript", ret );
1391 return( ret );
1392 }
1393
1394 hash_transcript[0] = MBEDTLS_SSL_HS_MESSAGE_HASH;
1395 hash_transcript[1] = 0;
1396 hash_transcript[2] = 0;
1397 hash_transcript[3] = (unsigned char) hash_len;
1398
1399 hash_len += 4;
1400
XiaokangQian78b1fa72022-01-19 06:56:30 +00001401 if( ciphersuite_info->mac == MBEDTLS_MD_SHA256 )
1402 {
1403#if defined(MBEDTLS_SHA256_C)
XiaokangQian78b1fa72022-01-19 06:56:30 +00001404 MBEDTLS_SSL_DEBUG_BUF( 4, "Truncated SHA-256 handshake transcript",
XiaokangQian0ece9982022-01-24 08:56:23 +00001405 hash_transcript, hash_len );
XiaokangQian78b1fa72022-01-19 06:56:30 +00001406
1407#if defined(MBEDTLS_USE_PSA_CRYPTO)
1408 psa_hash_abort( &ssl->handshake->fin_sha256_psa );
1409 psa_hash_setup( &ssl->handshake->fin_sha256_psa, PSA_ALG_SHA_256 );
1410#else
1411 mbedtls_sha256_starts( &ssl->handshake->fin_sha256, 0 );
1412#endif
XiaokangQian78b1fa72022-01-19 06:56:30 +00001413#endif /* MBEDTLS_SHA256_C */
1414 }
1415 else if( ciphersuite_info->mac == MBEDTLS_MD_SHA384 )
1416 {
1417#if defined(MBEDTLS_SHA384_C)
XiaokangQian78b1fa72022-01-19 06:56:30 +00001418 MBEDTLS_SSL_DEBUG_BUF( 4, "Truncated SHA-384 handshake transcript",
XiaokangQian0ece9982022-01-24 08:56:23 +00001419 hash_transcript, hash_len );
XiaokangQian78b1fa72022-01-19 06:56:30 +00001420
1421#if defined(MBEDTLS_USE_PSA_CRYPTO)
1422 psa_hash_abort( &ssl->handshake->fin_sha384_psa );
1423 psa_hash_setup( &ssl->handshake->fin_sha384_psa, PSA_ALG_SHA_384 );
1424#else
1425 mbedtls_sha512_starts( &ssl->handshake->fin_sha512, 1 );
1426#endif
XiaokangQian78b1fa72022-01-19 06:56:30 +00001427#endif /* MBEDTLS_SHA384_C */
1428 }
1429
XiaokangQian0ece9982022-01-24 08:56:23 +00001430#if defined(MBEDTLS_SHA256_C) || defined(MBEDTLS_SHA384_C)
1431 ssl->handshake->update_checksum( ssl, hash_transcript, hash_len );
1432#endif /* MBEDTLS_SHA256_C || MBEDTLS_SHA384_C */
Przemyslaw Stekiel4b3fff42022-02-14 16:39:52 +01001433
XiaokangQian78b1fa72022-01-19 06:56:30 +00001434 return( ret );
1435}
1436
XiaokangQian9b5d04b2022-04-10 10:20:43 +00001437#if defined(MBEDTLS_ECDH_C)
XiaokangQian7807f9f2022-02-15 10:04:37 +00001438
XiaokangQian9b5d04b2022-04-10 10:20:43 +00001439int mbedtls_ssl_tls13_read_public_ecdhe_share( mbedtls_ssl_context *ssl,
1440 const unsigned char *buf,
1441 size_t buf_len )
XiaokangQian7807f9f2022-02-15 10:04:37 +00001442{
XiaokangQian9b5d04b2022-04-10 10:20:43 +00001443 uint8_t *p = (uint8_t*)buf;
XiaokangQiancfd925f2022-04-14 07:10:37 +00001444 const uint8_t *end = buf + buf_len;
XiaokangQian9b5d04b2022-04-10 10:20:43 +00001445 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001446
XiaokangQian9b5d04b2022-04-10 10:20:43 +00001447 /* Get size of the TLS opaque key_exchange field of the KeyShareEntry struct. */
XiaokangQiancfd925f2022-04-14 07:10:37 +00001448 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 2 );
XiaokangQian9b5d04b2022-04-10 10:20:43 +00001449 uint16_t peerkey_len = MBEDTLS_GET_UINT16_BE( p, 0 );
1450 p += 2;
XiaokangQian3207a322022-02-23 03:15:27 +00001451
XiaokangQian9b5d04b2022-04-10 10:20:43 +00001452 /* Check if key size is consistent with given buffer length. */
XiaokangQiancfd925f2022-04-14 07:10:37 +00001453 MBEDTLS_SSL_CHK_BUF_PTR( p, end, peerkey_len );
XiaokangQian9b5d04b2022-04-10 10:20:43 +00001454
1455 /* Store peer's ECDH public key. */
1456 memcpy( handshake->ecdh_psa_peerkey, p, peerkey_len );
1457 handshake->ecdh_psa_peerkey_len = peerkey_len;
1458
XiaokangQian3207a322022-02-23 03:15:27 +00001459 return( 0 );
1460}
Jerry Yu89e103c2022-03-30 22:43:29 +08001461
1462int mbedtls_ssl_tls13_generate_and_write_ecdh_key_exchange(
1463 mbedtls_ssl_context *ssl,
1464 uint16_t named_group,
1465 unsigned char *buf,
1466 unsigned char *end,
1467 size_t *out_len )
1468{
1469 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
1470 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
1471 psa_key_attributes_t key_attributes;
1472 size_t own_pubkey_len;
1473 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
1474 size_t ecdh_bits = 0;
1475
1476 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Perform PSA-based ECDH computation." ) );
1477
1478 /* Convert EC group to PSA key type. */
1479 if( ( handshake->ecdh_psa_type =
1480 mbedtls_psa_parse_tls_ecc_group( named_group, &ecdh_bits ) ) == 0 )
1481 return( MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
1482
1483 ssl->handshake->ecdh_bits = ecdh_bits;
1484
1485 key_attributes = psa_key_attributes_init();
1486 psa_set_key_usage_flags( &key_attributes, PSA_KEY_USAGE_DERIVE );
1487 psa_set_key_algorithm( &key_attributes, PSA_ALG_ECDH );
1488 psa_set_key_type( &key_attributes, handshake->ecdh_psa_type );
1489 psa_set_key_bits( &key_attributes, handshake->ecdh_bits );
1490
1491 /* Generate ECDH private key. */
1492 status = psa_generate_key( &key_attributes,
1493 &handshake->ecdh_psa_privkey );
1494 if( status != PSA_SUCCESS )
1495 {
1496 ret = psa_ssl_status_to_mbedtls( status );
1497 MBEDTLS_SSL_DEBUG_RET( 1, "psa_generate_key", ret );
1498 return( ret );
1499
1500 }
1501
1502 /* Export the public part of the ECDH private key from PSA. */
1503 status = psa_export_public_key( handshake->ecdh_psa_privkey,
1504 buf, (size_t)( end - buf ),
1505 &own_pubkey_len );
1506 if( status != PSA_SUCCESS )
1507 {
1508 ret = psa_ssl_status_to_mbedtls( status );
1509 MBEDTLS_SSL_DEBUG_RET( 1, "psa_export_public_key", ret );
1510 return( ret );
1511
1512 }
1513
1514 *out_len = own_pubkey_len;
1515
1516 return( 0 );
1517}
XiaokangQian9b5d04b2022-04-10 10:20:43 +00001518#endif /* MBEDTLS_ECDH_C */
XiaokangQian7807f9f2022-02-15 10:04:37 +00001519
Jerry Yufb4b6472022-01-27 15:03:26 +08001520#endif /* MBEDTLS_SSL_TLS_C && MBEDTLS_SSL_PROTO_TLS1_3 */