blob: 239be53a187177344206f827451f2324d71c01b6 [file] [log] [blame]
Jerry Yu65dd2cc2021-08-18 16:38:40 +08001/*
2 * TLS 1.3 functionality shared between client and server
3 *
4 * Copyright The Mbed TLS Contributors
5 * SPDX-License-Identifier: Apache-2.0
6 *
7 * Licensed under the Apache License, Version 2.0 (the "License"); you may
8 * not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
15 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 */
19
20#include "common.h"
21
Jerry Yufb4b6472022-01-27 15:03:26 +080022#if defined(MBEDTLS_SSL_TLS_C) && defined(MBEDTLS_SSL_PROTO_TLS1_3)
Jerry Yu65dd2cc2021-08-18 16:38:40 +080023
Jerry Yu30b071c2021-09-12 20:16:03 +080024#include <string.h>
25
Jerry Yuc8a392c2021-08-18 16:46:28 +080026#include "mbedtls/error.h"
Jerry Yu75336352021-09-01 15:59:36 +080027#include "mbedtls/debug.h"
Jerry Yu30b071c2021-09-12 20:16:03 +080028#include "mbedtls/oid.h"
29#include "mbedtls/platform.h"
Gabor Mezei685472b2021-11-24 11:17:36 +010030#include "mbedtls/constant_time.h"
XiaokangQian74af2a82021-09-22 07:40:30 +000031#include <string.h>
Jerry Yuc8a392c2021-08-18 16:46:28 +080032
Jerry Yu65dd2cc2021-08-18 16:38:40 +080033#include "ssl_misc.h"
Jerry Yu30b071c2021-09-12 20:16:03 +080034#include "ssl_tls13_keys.h"
Jerry Yu67eced02022-02-25 13:37:36 +080035#include "ssl_debug_helpers.h"
Jerry Yu65dd2cc2021-08-18 16:38:40 +080036
Jerry Yufbe3e642022-04-25 19:31:51 +080037const uint8_t mbedtls_ssl_tls13_hello_retry_request_magic[
38 MBEDTLS_SERVER_HELLO_RANDOM_LEN ] =
39 { 0xCF, 0x21, 0xAD, 0x74, 0xE5, 0x9A, 0x61, 0x11,
40 0xBE, 0x1D, 0x8C, 0x02, 0x1E, 0x65, 0xB8, 0x91,
41 0xC2, 0xA2, 0x11, 0x16, 0x7A, 0xBB, 0x8C, 0x5E,
42 0x07, 0x9E, 0x09, 0xE2, 0xC8, 0xA8, 0x33, 0x9C };
Jerry Yu93a13f22022-04-11 23:00:01 +080043
Xiaofei Bai746f9482021-11-12 08:53:56 +000044int mbedtls_ssl_tls13_fetch_handshake_msg( mbedtls_ssl_context *ssl,
45 unsigned hs_type,
46 unsigned char **buf,
Xiaofei Baieef15042021-11-18 07:29:56 +000047 size_t *buf_len )
XiaokangQian6b226b02021-09-24 07:51:16 +000048{
49 int ret;
50
51 if( ( ret = mbedtls_ssl_read_record( ssl, 0 ) ) != 0 )
52 {
53 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
54 goto cleanup;
55 }
56
XiaokangQian16c61aa2021-09-27 09:30:17 +000057 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE ||
XiaokangQian6b226b02021-09-24 07:51:16 +000058 ssl->in_msg[0] != hs_type )
59 {
XiaokangQian16c61aa2021-09-27 09:30:17 +000060 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Receive unexpected handshake message." ) );
XiaokangQian6b226b02021-09-24 07:51:16 +000061 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE,
XiaokangQian05420b12021-09-29 08:46:37 +000062 MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
XiaokangQian6b226b02021-09-24 07:51:16 +000063 ret = MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE;
64 goto cleanup;
65 }
66
XiaokangQian05420b12021-09-29 08:46:37 +000067 /*
68 * Jump handshake header (4 bytes, see Section 4 of RFC 8446).
69 * ...
70 * HandshakeType msg_type;
71 * uint24 length;
72 * ...
73 */
Xiaofei Baieef15042021-11-18 07:29:56 +000074 *buf = ssl->in_msg + 4;
75 *buf_len = ssl->in_hslen - 4;
XiaokangQian6b226b02021-09-24 07:51:16 +000076
XiaokangQian6b226b02021-09-24 07:51:16 +000077cleanup:
78
79 return( ret );
80}
81
Jerry Yubc20bdd2021-08-24 15:59:48 +080082#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Jerry Yu30b071c2021-09-12 20:16:03 +080083/*
Jerry Yu30b071c2021-09-12 20:16:03 +080084 * STATE HANDLING: Read CertificateVerify
85 */
Jerry Yud0fc5852021-10-29 11:09:06 +080086/* Macro to express the maximum length of the verify structure.
Jerry Yu30b071c2021-09-12 20:16:03 +080087 *
88 * The structure is computed per TLS 1.3 specification as:
89 * - 64 bytes of octet 32,
90 * - 33 bytes for the context string
91 * (which is either "TLS 1.3, client CertificateVerify"
92 * or "TLS 1.3, server CertificateVerify"),
Jerry Yud0fc5852021-10-29 11:09:06 +080093 * - 1 byte for the octet 0x0, which serves as a separator,
Jerry Yu30b071c2021-09-12 20:16:03 +080094 * - 32 or 48 bytes for the Transcript-Hash(Handshake Context, Certificate)
95 * (depending on the size of the transcript_hash)
96 *
97 * This results in a total size of
98 * - 130 bytes for a SHA256-based transcript hash, or
99 * (64 + 33 + 1 + 32 bytes)
100 * - 146 bytes for a SHA384-based transcript hash.
101 * (64 + 33 + 1 + 48 bytes)
102 *
103 */
Jerry Yu26c2d112021-10-25 12:42:58 +0800104#define SSL_VERIFY_STRUCT_MAX_SIZE ( 64 + \
105 33 + \
106 1 + \
107 MBEDTLS_TLS1_3_MD_MAX_SIZE \
Jerry Yu30b071c2021-09-12 20:16:03 +0800108 )
Jerry Yu30b071c2021-09-12 20:16:03 +0800109
Jerry Yu0b32c502021-10-28 13:41:59 +0800110/*
111 * The ssl_tls13_create_verify_structure() creates the verify structure.
112 * As input, it requires the transcript hash.
113 *
114 * The caller has to ensure that the buffer has size at least
115 * SSL_VERIFY_STRUCT_MAX_SIZE bytes.
116 */
Jerry Yud0fc5852021-10-29 11:09:06 +0800117static void ssl_tls13_create_verify_structure( const unsigned char *transcript_hash,
Jerry Yu0b32c502021-10-28 13:41:59 +0800118 size_t transcript_hash_len,
119 unsigned char *verify_buffer,
120 size_t *verify_buffer_len,
121 int from )
122{
123 size_t idx;
Jerry Yu30b071c2021-09-12 20:16:03 +0800124
Jerry Yu0b32c502021-10-28 13:41:59 +0800125 /* RFC 8446, Section 4.4.3:
126 *
127 * The digital signature [in the CertificateVerify message] is then
128 * computed over the concatenation of:
129 * - A string that consists of octet 32 (0x20) repeated 64 times
130 * - The context string
131 * - A single 0 byte which serves as the separator
132 * - The content to be signed
133 */
134 memset( verify_buffer, 0x20, 64 );
135 idx = 64;
136
137 if( from == MBEDTLS_SSL_IS_CLIENT )
138 {
139 memcpy( verify_buffer + idx, MBEDTLS_SSL_TLS1_3_LBL_WITH_LEN( client_cv ) );
140 idx += MBEDTLS_SSL_TLS1_3_LBL_LEN( client_cv );
141 }
142 else
143 { /* from == MBEDTLS_SSL_IS_SERVER */
144 memcpy( verify_buffer + idx, MBEDTLS_SSL_TLS1_3_LBL_WITH_LEN( server_cv ) );
145 idx += MBEDTLS_SSL_TLS1_3_LBL_LEN( server_cv );
146 }
147
148 verify_buffer[idx++] = 0x0;
149
150 memcpy( verify_buffer + idx, transcript_hash, transcript_hash_len );
151 idx += transcript_hash_len;
152
153 *verify_buffer_len = idx;
154}
155
Jerry Yu0b32c502021-10-28 13:41:59 +0800156static int ssl_tls13_parse_certificate_verify( mbedtls_ssl_context *ssl,
Jerry Yud0fc5852021-10-29 11:09:06 +0800157 const unsigned char *buf,
158 const unsigned char *end,
159 const unsigned char *verify_buffer,
160 size_t verify_buffer_len )
Jerry Yu30b071c2021-09-12 20:16:03 +0800161{
162 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
163 const unsigned char *p = buf;
164 uint16_t algorithm;
Jerry Yu30b071c2021-09-12 20:16:03 +0800165 size_t signature_len;
166 mbedtls_pk_type_t sig_alg;
167 mbedtls_md_type_t md_alg;
Jerry Yud0fc5852021-10-29 11:09:06 +0800168 unsigned char verify_hash[MBEDTLS_MD_MAX_SIZE];
Jerry Yu30b071c2021-09-12 20:16:03 +0800169 size_t verify_hash_len;
170
Xiaofei Baid25fab62021-12-02 06:36:27 +0000171 void const *options = NULL;
XiaokangQian82d34cc2021-11-03 08:51:56 +0000172#if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT)
Xiaofei Baid25fab62021-12-02 06:36:27 +0000173 mbedtls_pk_rsassa_pss_options rsassa_pss_options;
XiaokangQian82d34cc2021-11-03 08:51:56 +0000174#endif /* MBEDTLS_X509_RSASSA_PSS_SUPPORT */
175
Jerry Yu30b071c2021-09-12 20:16:03 +0800176 /*
177 * struct {
178 * SignatureScheme algorithm;
179 * opaque signature<0..2^16-1>;
180 * } CertificateVerify;
181 */
182 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 2 );
183 algorithm = MBEDTLS_GET_UINT16_BE( p, 0 );
184 p += 2;
185
186 /* RFC 8446 section 4.4.3
187 *
188 * If the CertificateVerify message is sent by a server, the signature algorithm
189 * MUST be one offered in the client's "signature_algorithms" extension unless
190 * no valid certificate chain can be produced without unsupported algorithms
191 *
192 * RFC 8446 section 4.4.2.2
193 *
194 * If the client cannot construct an acceptable chain using the provided
195 * certificates and decides to abort the handshake, then it MUST abort the handshake
196 * with an appropriate certificate-related alert (by default, "unsupported_certificate").
197 *
Jerry Yu6f87f252021-10-29 20:12:51 +0800198 * Check if algorithm is an offered signature algorithm.
Jerry Yu30b071c2021-09-12 20:16:03 +0800199 */
Jerry Yu24811fb2022-01-19 18:02:15 +0800200 if( ! mbedtls_ssl_sig_alg_is_offered( ssl, algorithm ) )
Jerry Yu30b071c2021-09-12 20:16:03 +0800201 {
Jerry Yu982d9e52021-10-14 15:59:37 +0800202 /* algorithm not in offered signature algorithms list */
203 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Received signature algorithm(%04x) is not "
204 "offered.",
205 ( unsigned int ) algorithm ) );
Jerry Yu6f87f252021-10-29 20:12:51 +0800206 goto error;
Jerry Yu30b071c2021-09-12 20:16:03 +0800207 }
208
Jerry Yu8c338862022-03-23 13:34:04 +0800209 if( mbedtls_ssl_tls13_get_pk_type_and_md_alg_from_sig_alg(
Jerry Yuf8aa9a42022-03-23 20:40:28 +0800210 algorithm, &sig_alg, &md_alg ) != 0 )
Jerry Yu30b071c2021-09-12 20:16:03 +0800211 {
Jerry Yu8c338862022-03-23 13:34:04 +0800212 goto error;
Jerry Yu30b071c2021-09-12 20:16:03 +0800213 }
214
215 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Certificate Verify: Signature algorithm ( %04x )",
216 ( unsigned int ) algorithm ) );
217
218 /*
219 * Check the certificate's key type matches the signature alg
220 */
221 if( !mbedtls_pk_can_do( &ssl->session_negotiate->peer_cert->pk, sig_alg ) )
222 {
223 MBEDTLS_SSL_DEBUG_MSG( 1, ( "signature algorithm doesn't match cert key" ) );
Jerry Yu6f87f252021-10-29 20:12:51 +0800224 goto error;
Jerry Yu30b071c2021-09-12 20:16:03 +0800225 }
226
227 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 2 );
228 signature_len = MBEDTLS_GET_UINT16_BE( p, 0 );
229 p += 2;
230 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, signature_len );
231
232 /* Hash verify buffer with indicated hash function */
233 switch( md_alg )
234 {
235#if defined(MBEDTLS_SHA256_C)
236 case MBEDTLS_MD_SHA256:
237 verify_hash_len = 32;
Jerry Yu133690c2021-10-25 14:01:13 +0800238 ret = mbedtls_sha256( verify_buffer, verify_buffer_len, verify_hash, 0 );
Jerry Yu30b071c2021-09-12 20:16:03 +0800239 break;
240#endif /* MBEDTLS_SHA256_C */
241
242#if defined(MBEDTLS_SHA384_C)
243 case MBEDTLS_MD_SHA384:
244 verify_hash_len = 48;
Jerry Yu133690c2021-10-25 14:01:13 +0800245 ret = mbedtls_sha512( verify_buffer, verify_buffer_len, verify_hash, 1 );
Jerry Yu30b071c2021-09-12 20:16:03 +0800246 break;
247#endif /* MBEDTLS_SHA384_C */
248
249#if defined(MBEDTLS_SHA512_C)
250 case MBEDTLS_MD_SHA512:
251 verify_hash_len = 64;
Jerry Yu133690c2021-10-25 14:01:13 +0800252 ret = mbedtls_sha512( verify_buffer, verify_buffer_len, verify_hash, 0 );
Jerry Yu30b071c2021-09-12 20:16:03 +0800253 break;
254#endif /* MBEDTLS_SHA512_C */
255
Jerry Yu0b32c502021-10-28 13:41:59 +0800256 default:
257 ret = MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
258 break;
Jerry Yu30b071c2021-09-12 20:16:03 +0800259 }
260
Jerry Yu133690c2021-10-25 14:01:13 +0800261 if( ret != 0 )
262 {
263 MBEDTLS_SSL_DEBUG_RET( 1, "hash computation error", ret );
Jerry Yu6f87f252021-10-29 20:12:51 +0800264 goto error;
Jerry Yu133690c2021-10-25 14:01:13 +0800265 }
266
Jerry Yu30b071c2021-09-12 20:16:03 +0800267 MBEDTLS_SSL_DEBUG_BUF( 3, "verify hash", verify_hash, verify_hash_len );
XiaokangQian82d34cc2021-11-03 08:51:56 +0000268#if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT)
269 if( sig_alg == MBEDTLS_PK_RSASSA_PSS )
270 {
271 const mbedtls_md_info_t* md_info;
Xiaofei Baid25fab62021-12-02 06:36:27 +0000272 rsassa_pss_options.mgf1_hash_id = md_alg;
XiaokangQian82d34cc2021-11-03 08:51:56 +0000273 if( ( md_info = mbedtls_md_info_from_type( md_alg ) ) == NULL )
274 {
275 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
276 }
Xiaofei Baid25fab62021-12-02 06:36:27 +0000277 rsassa_pss_options.expected_salt_len = mbedtls_md_get_size( md_info );
278 options = (const void*) &rsassa_pss_options;
XiaokangQian82d34cc2021-11-03 08:51:56 +0000279 }
280#endif /* MBEDTLS_X509_RSASSA_PSS_SUPPORT */
Jerry Yu30b071c2021-09-12 20:16:03 +0800281
Xiaofei Baid25fab62021-12-02 06:36:27 +0000282 if( ( ret = mbedtls_pk_verify_ext( sig_alg, options,
Jerry Yu30b071c2021-09-12 20:16:03 +0800283 &ssl->session_negotiate->peer_cert->pk,
284 md_alg, verify_hash, verify_hash_len,
Jerry Yu6f87f252021-10-29 20:12:51 +0800285 p, signature_len ) ) == 0 )
Jerry Yu30b071c2021-09-12 20:16:03 +0800286 {
Jerry Yu6f87f252021-10-29 20:12:51 +0800287 return( 0 );
Jerry Yu30b071c2021-09-12 20:16:03 +0800288 }
Jerry Yu6f87f252021-10-29 20:12:51 +0800289 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_pk_verify_ext", ret );
Jerry Yu30b071c2021-09-12 20:16:03 +0800290
Jerry Yu6f87f252021-10-29 20:12:51 +0800291error:
292 /* RFC 8446 section 4.4.3
293 *
294 * If the verification fails, the receiver MUST terminate the handshake
295 * with a "decrypt_error" alert.
296 */
297 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_DECRYPT_ERROR,
298 MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
299 return( MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
300
Jerry Yu30b071c2021-09-12 20:16:03 +0800301}
302#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
303
304int mbedtls_ssl_tls13_process_certificate_verify( mbedtls_ssl_context *ssl )
305{
Jerry Yu30b071c2021-09-12 20:16:03 +0800306
Jerry Yuda8cdf22021-10-25 15:06:49 +0800307#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
308 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
309 unsigned char verify_buffer[SSL_VERIFY_STRUCT_MAX_SIZE];
310 size_t verify_buffer_len;
311 unsigned char transcript[MBEDTLS_TLS1_3_MD_MAX_SIZE];
312 size_t transcript_len;
313 unsigned char *buf;
314 size_t buf_len;
315
Jerry Yu30b071c2021-09-12 20:16:03 +0800316 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate verify" ) );
317
Jerry Yuda8cdf22021-10-25 15:06:49 +0800318 MBEDTLS_SSL_PROC_CHK(
Xiaofei Bai746f9482021-11-12 08:53:56 +0000319 mbedtls_ssl_tls13_fetch_handshake_msg( ssl,
Jerry Yuda8cdf22021-10-25 15:06:49 +0800320 MBEDTLS_SSL_HS_CERTIFICATE_VERIFY, &buf, &buf_len ) );
Jerry Yu30b071c2021-09-12 20:16:03 +0800321
Jerry Yuda8cdf22021-10-25 15:06:49 +0800322 /* Need to calculate the hash of the transcript first
Jerry Yu0b32c502021-10-28 13:41:59 +0800323 * before reading the message since otherwise it gets
324 * included in the transcript
325 */
Jerry Yuda8cdf22021-10-25 15:06:49 +0800326 ret = mbedtls_ssl_get_handshake_transcript( ssl,
327 ssl->handshake->ciphersuite_info->mac,
328 transcript, sizeof( transcript ),
329 &transcript_len );
330 if( ret != 0 )
Jerry Yu30b071c2021-09-12 20:16:03 +0800331 {
Jerry Yuda8cdf22021-10-25 15:06:49 +0800332 MBEDTLS_SSL_PEND_FATAL_ALERT(
333 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR,
334 MBEDTLS_ERR_SSL_INTERNAL_ERROR );
335 return( ret );
Jerry Yu30b071c2021-09-12 20:16:03 +0800336 }
337
Jerry Yuda8cdf22021-10-25 15:06:49 +0800338 MBEDTLS_SSL_DEBUG_BUF( 3, "handshake hash", transcript, transcript_len );
339
340 /* Create verify structure */
341 ssl_tls13_create_verify_structure( transcript,
Jerry Yu0b32c502021-10-28 13:41:59 +0800342 transcript_len,
343 verify_buffer,
344 &verify_buffer_len,
345 ( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT ) ?
346 MBEDTLS_SSL_IS_SERVER :
347 MBEDTLS_SSL_IS_CLIENT );
Jerry Yuda8cdf22021-10-25 15:06:49 +0800348
349 /* Process the message contents */
Jerry Yu0b32c502021-10-28 13:41:59 +0800350 MBEDTLS_SSL_PROC_CHK( ssl_tls13_parse_certificate_verify( ssl, buf,
351 buf + buf_len, verify_buffer, verify_buffer_len ) );
Jerry Yuda8cdf22021-10-25 15:06:49 +0800352
Ronald Cron8f6d39a2022-03-10 18:56:50 +0100353 mbedtls_ssl_add_hs_msg_to_checksum( ssl, MBEDTLS_SSL_HS_CERTIFICATE_VERIFY,
354 buf, buf_len );
Jerry Yu30b071c2021-09-12 20:16:03 +0800355
356cleanup:
357
358 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse certificate verify" ) );
Jerry Yu5398c102021-11-05 13:32:38 +0800359 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_tls13_process_certificate_verify", ret );
Jerry Yu30b071c2021-09-12 20:16:03 +0800360 return( ret );
Jerry Yuda8cdf22021-10-25 15:06:49 +0800361#else
362 ((void) ssl);
363 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
364 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
365#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
Jerry Yu30b071c2021-09-12 20:16:03 +0800366}
367
368/*
Xiaofei Bai947571e2021-09-29 09:12:03 +0000369 *
XiaokangQian6b916b12022-04-25 07:29:34 +0000370 * STATE HANDLING: Incoming Certificate.
Xiaofei Bai947571e2021-09-29 09:12:03 +0000371 *
372 */
373
Xiaofei Bai947571e2021-09-29 09:12:03 +0000374#if defined(MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED)
375#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
376/*
377 * Structure of Certificate message:
378 *
379 * enum {
380 * X509(0),
381 * RawPublicKey(2),
382 * (255)
383 * } CertificateType;
384 *
385 * struct {
386 * select (certificate_type) {
387 * case RawPublicKey:
388 * * From RFC 7250 ASN.1_subjectPublicKeyInfo *
389 * opaque ASN1_subjectPublicKeyInfo<1..2^24-1>;
390 * case X509:
391 * opaque cert_data<1..2^24-1>;
392 * };
393 * Extension extensions<0..2^16-1>;
394 * } CertificateEntry;
395 *
396 * struct {
397 * opaque certificate_request_context<0..2^8-1>;
398 * CertificateEntry certificate_list<0..2^24-1>;
399 * } Certificate;
400 *
401 */
Xiaofei Bai79595ac2021-10-26 07:16:45 +0000402
403/* Parse certificate chain send by the server. */
Xiaofei Bai947571e2021-09-29 09:12:03 +0000404static int ssl_tls13_parse_certificate( mbedtls_ssl_context *ssl,
405 const unsigned char *buf,
406 const unsigned char *end )
407{
408 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
409 size_t certificate_request_context_len = 0;
410 size_t certificate_list_len = 0;
411 const unsigned char *p = buf;
412 const unsigned char *certificate_list_end;
413
XiaokangQian63e713e2022-05-15 04:26:57 +0000414 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 4 );
Xiaofei Bai947571e2021-09-29 09:12:03 +0000415 certificate_request_context_len = p[0];
XiaokangQian63e713e2022-05-15 04:26:57 +0000416 certificate_list_len = MBEDTLS_GET_UINT24_BE( p, 1 );
417 p += 4;
Xiaofei Bai947571e2021-09-29 09:12:03 +0000418
419 /* In theory, the certificate list can be up to 2^24 Bytes, but we don't
420 * support anything beyond 2^16 = 64K.
421 */
Xiaofei Bai79595ac2021-10-26 07:16:45 +0000422 if( ( certificate_request_context_len != 0 ) ||
423 ( certificate_list_len >= 0x10000 ) )
Xiaofei Bai947571e2021-09-29 09:12:03 +0000424 {
425 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
426 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR,
427 MBEDTLS_ERR_SSL_DECODE_ERROR );
428 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
429 }
430
431 /* In case we tried to reuse a session but it failed */
432 if( ssl->session_negotiate->peer_cert != NULL )
433 {
434 mbedtls_x509_crt_free( ssl->session_negotiate->peer_cert );
435 mbedtls_free( ssl->session_negotiate->peer_cert );
436 }
437
XiaokangQianc3017f62022-05-13 05:55:41 +0000438 if( certificate_list_len == 0 )
439 {
440 ssl->session_negotiate->peer_cert = NULL;
441 ret = 0;
442 goto exit;
443 }
444
Xiaofei Bai947571e2021-09-29 09:12:03 +0000445 if( ( ssl->session_negotiate->peer_cert =
446 mbedtls_calloc( 1, sizeof( mbedtls_x509_crt ) ) ) == NULL )
447 {
448 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc( %" MBEDTLS_PRINTF_SIZET " bytes ) failed",
449 sizeof( mbedtls_x509_crt ) ) );
450 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR,
451 MBEDTLS_ERR_SSL_ALLOC_FAILED );
452 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
453 }
454
455 mbedtls_x509_crt_init( ssl->session_negotiate->peer_cert );
456
Xiaofei Bai947571e2021-09-29 09:12:03 +0000457 certificate_list_end = p + certificate_list_len;
Xiaofei Bai79595ac2021-10-26 07:16:45 +0000458 while( p < certificate_list_end )
Xiaofei Bai947571e2021-09-29 09:12:03 +0000459 {
460 size_t cert_data_len, extensions_len;
461
Xiaofei Bai79595ac2021-10-26 07:16:45 +0000462 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, certificate_list_end, 3 );
Xiaofei Baif93cbd22021-10-29 02:39:30 +0000463 cert_data_len = MBEDTLS_GET_UINT24_BE( p, 0 );
Xiaofei Bai947571e2021-09-29 09:12:03 +0000464 p += 3;
465
466 /* In theory, the CRT can be up to 2^24 Bytes, but we don't support
467 * anything beyond 2^16 = 64K. Otherwise as in the TLS 1.2 code,
468 * check that we have a minimum of 128 bytes of data, this is not
469 * clear why we need that though.
470 */
471 if( ( cert_data_len < 128 ) || ( cert_data_len >= 0x10000 ) )
Xiaofei Bai79595ac2021-10-26 07:16:45 +0000472 {
Xiaofei Bai947571e2021-09-29 09:12:03 +0000473 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad Certificate message" ) );
474 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR,
475 MBEDTLS_ERR_SSL_DECODE_ERROR );
476 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
477 }
478
Xiaofei Bai79595ac2021-10-26 07:16:45 +0000479 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, certificate_list_end, cert_data_len );
Xiaofei Bai947571e2021-09-29 09:12:03 +0000480 ret = mbedtls_x509_crt_parse_der( ssl->session_negotiate->peer_cert,
481 p, cert_data_len );
482
483 switch( ret )
484 {
485 case 0: /*ok*/
486 break;
487 case MBEDTLS_ERR_X509_UNKNOWN_SIG_ALG + MBEDTLS_ERR_OID_NOT_FOUND:
488 /* Ignore certificate with an unknown algorithm: maybe a
489 prior certificate was already trusted. */
490 break;
491
492 case MBEDTLS_ERR_X509_ALLOC_FAILED:
493 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR,
494 MBEDTLS_ERR_X509_ALLOC_FAILED );
495 MBEDTLS_SSL_DEBUG_RET( 1, " mbedtls_x509_crt_parse_der", ret );
496 return( ret );
497
498 case MBEDTLS_ERR_X509_UNKNOWN_VERSION:
499 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT,
500 MBEDTLS_ERR_X509_UNKNOWN_VERSION );
501 MBEDTLS_SSL_DEBUG_RET( 1, " mbedtls_x509_crt_parse_der", ret );
502 return( ret );
503
504 default:
505 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_BAD_CERT,
506 ret );
507 MBEDTLS_SSL_DEBUG_RET( 1, " mbedtls_x509_crt_parse_der", ret );
508 return( ret );
509 }
510
511 p += cert_data_len;
512
513 /* Certificate extensions length */
514 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, certificate_list_end, 2 );
515 extensions_len = MBEDTLS_GET_UINT16_BE( p, 0 );
516 p += 2;
Xiaofei Bai79595ac2021-10-26 07:16:45 +0000517 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, certificate_list_end, extensions_len );
Xiaofei Bai947571e2021-09-29 09:12:03 +0000518 p += extensions_len;
519 }
520
XiaokangQian63e713e2022-05-15 04:26:57 +0000521exit:
Xiaofei Bai947571e2021-09-29 09:12:03 +0000522 /* Check that all the message is consumed. */
523 if( p != end )
524 {
525 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad Certificate message" ) );
526 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR, \
527 MBEDTLS_ERR_SSL_DECODE_ERROR );
528 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
529 }
530
531 MBEDTLS_SSL_DEBUG_CRT( 3, "peer certificate", ssl->session_negotiate->peer_cert );
532
533 return( ret );
534}
535#else
536static int ssl_tls13_parse_certificate( mbedtls_ssl_context *ssl,
537 const unsigned char *buf,
538 const unsigned char *end )
539{
540 ((void) ssl);
541 ((void) buf);
542 ((void) end);
543 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
544}
545#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
546#endif /* MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED */
547
548#if defined(MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED)
549#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Xiaofei Bai79595ac2021-10-26 07:16:45 +0000550/* Validate certificate chain sent by the server. */
Xiaofei Bai947571e2021-09-29 09:12:03 +0000551static int ssl_tls13_validate_certificate( mbedtls_ssl_context *ssl )
552{
553 int ret = 0;
XiaokangQian989f06d2022-05-17 01:50:15 +0000554 int authmode = MBEDTLS_SSL_VERIFY_REQUIRED;
Xiaofei Bai947571e2021-09-29 09:12:03 +0000555 mbedtls_x509_crt *ca_chain;
556 mbedtls_x509_crl *ca_crl;
Xiaofei Baiff456022021-10-28 06:50:17 +0000557 uint32_t verify_result = 0;
Xiaofei Bai947571e2021-09-29 09:12:03 +0000558
XiaokangQian6b916b12022-04-25 07:29:34 +0000559 /* If SNI was used, overwrite authentication mode
560 * from the configuration. */
XiaokangQian989f06d2022-05-17 01:50:15 +0000561#if defined(MBEDTLS_SSL_SRV_C)
562 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
XiaokangQiane7a5da52022-05-30 00:59:29 +0000563 authmode = ssl->conf->authmode;
XiaokangQian6b916b12022-04-25 07:29:34 +0000564#endif
565
566 /*
XiaokangQian989f06d2022-05-17 01:50:15 +0000567 * If the peer hasn't sent a certificate ( i.e. it sent
XiaokangQian6b916b12022-04-25 07:29:34 +0000568 * an empty certificate chain ), this is reflected in the peer CRT
569 * structure being unset.
570 * Check for that and handle it depending on the
XiaokangQian989f06d2022-05-17 01:50:15 +0000571 * authentication mode.
XiaokangQian6b916b12022-04-25 07:29:34 +0000572 */
XiaokangQian63e713e2022-05-15 04:26:57 +0000573 if( ssl->session_negotiate->peer_cert == NULL )
XiaokangQian6b916b12022-04-25 07:29:34 +0000574 {
XiaokangQian989f06d2022-05-17 01:50:15 +0000575 MBEDTLS_SSL_DEBUG_MSG( 1, ( "peer has not sent a certificate" ) );
576
XiaokangQian63e713e2022-05-15 04:26:57 +0000577#if defined(MBEDTLS_SSL_SRV_C)
578 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
579 {
XiaokangQian63e713e2022-05-15 04:26:57 +0000580 /* The client was asked for a certificate but didn't send
581 * one. The client should know what's going on, so we
582 * don't send an alert.
583 */
584 ssl->session_negotiate->verify_result = MBEDTLS_X509_BADCERT_MISSING;
585 if( authmode == MBEDTLS_SSL_VERIFY_OPTIONAL )
586 return( 0 );
587 else
XiaokangQian989f06d2022-05-17 01:50:15 +0000588 {
589 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_NO_CERT,
XiaokangQianaca90482022-05-19 07:19:31 +0000590 MBEDTLS_ERR_SSL_NO_CLIENT_CERTIFICATE );
591 return( MBEDTLS_ERR_SSL_NO_CLIENT_CERTIFICATE );
XiaokangQian989f06d2022-05-17 01:50:15 +0000592 }
XiaokangQian63e713e2022-05-15 04:26:57 +0000593 }
XiaokangQian6b916b12022-04-25 07:29:34 +0000594#endif /* MBEDTLS_SSL_SRV_C */
595
XiaokangQianc3017f62022-05-13 05:55:41 +0000596#if defined(MBEDTLS_SSL_CLI_C)
XiaokangQian63e713e2022-05-15 04:26:57 +0000597 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
598 {
599 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_NO_CERT,
600 MBEDTLS_ERR_SSL_FATAL_ALERT_MESSAGE );
601 return( MBEDTLS_ERR_SSL_FATAL_ALERT_MESSAGE );
602 }
XiaokangQianc3017f62022-05-13 05:55:41 +0000603#endif /* MBEDTLS_SSL_CLI_C */
XiaokangQian63e713e2022-05-15 04:26:57 +0000604 }
XiaokangQian6b916b12022-04-25 07:29:34 +0000605
Xiaofei Bai947571e2021-09-29 09:12:03 +0000606#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
607 if( ssl->handshake->sni_ca_chain != NULL )
608 {
609 ca_chain = ssl->handshake->sni_ca_chain;
610 ca_crl = ssl->handshake->sni_ca_crl;
611 }
612 else
613#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
614 {
615 ca_chain = ssl->conf->ca_chain;
616 ca_crl = ssl->conf->ca_crl;
617 }
618
619 /*
620 * Main check: verify certificate
621 */
622 ret = mbedtls_x509_crt_verify_with_profile(
623 ssl->session_negotiate->peer_cert,
624 ca_chain, ca_crl,
625 ssl->conf->cert_profile,
626 ssl->hostname,
Xiaofei Baiff456022021-10-28 06:50:17 +0000627 &verify_result,
Xiaofei Bai947571e2021-09-29 09:12:03 +0000628 ssl->conf->f_vrfy, ssl->conf->p_vrfy );
629
630 if( ret != 0 )
631 {
632 MBEDTLS_SSL_DEBUG_RET( 1, "x509_verify_cert", ret );
633 }
634
635 /*
636 * Secondary checks: always done, but change 'ret' only if it was 0
637 */
Xiaofei Bai947571e2021-09-29 09:12:03 +0000638 if( mbedtls_ssl_check_cert_usage( ssl->session_negotiate->peer_cert,
639 ssl->handshake->ciphersuite_info,
640 !ssl->conf->endpoint,
Xiaofei Baiff456022021-10-28 06:50:17 +0000641 &verify_result ) != 0 )
Xiaofei Bai947571e2021-09-29 09:12:03 +0000642 {
643 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate ( usage extensions )" ) );
644 if( ret == 0 )
645 ret = MBEDTLS_ERR_SSL_BAD_CERTIFICATE;
646 }
647
XiaokangQian6b916b12022-04-25 07:29:34 +0000648 /* mbedtls_x509_crt_verify_with_profile is supposed to report a
649 * verification failure through MBEDTLS_ERR_X509_CERT_VERIFY_FAILED,
650 * with details encoded in the verification flags. All other kinds
651 * of error codes, including those from the user provided f_vrfy
652 * functions, are treated as fatal and lead to a failure of
653 * ssl_tls13_parse_certificate even if verification was optional. */
654 if( authmode == MBEDTLS_SSL_VERIFY_OPTIONAL &&
655 ( ret == MBEDTLS_ERR_X509_CERT_VERIFY_FAILED ||
656 ret == MBEDTLS_ERR_SSL_BAD_CERTIFICATE ) )
657 {
658 ret = 0;
659 }
Xiaofei Bai947571e2021-09-29 09:12:03 +0000660
XiaokangQian6b916b12022-04-25 07:29:34 +0000661 if( ca_chain == NULL && authmode == MBEDTLS_SSL_VERIFY_REQUIRED )
Xiaofei Bai947571e2021-09-29 09:12:03 +0000662 {
663 MBEDTLS_SSL_DEBUG_MSG( 1, ( "got no CA chain" ) );
664 ret = MBEDTLS_ERR_SSL_CA_CHAIN_REQUIRED;
665 }
666
667 if( ret != 0 )
668 {
669 /* The certificate may have been rejected for several reasons.
670 Pick one and send the corresponding alert. Which alert to send
671 may be a subject of debate in some cases. */
Xiaofei Baiff456022021-10-28 06:50:17 +0000672 if( verify_result & MBEDTLS_X509_BADCERT_OTHER )
Xiaofei Bai947571e2021-09-29 09:12:03 +0000673 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED, ret );
Xiaofei Baiff456022021-10-28 06:50:17 +0000674 else if( verify_result & MBEDTLS_X509_BADCERT_CN_MISMATCH )
Xiaofei Bai947571e2021-09-29 09:12:03 +0000675 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_BAD_CERT, ret );
Xiaofei Baif93cbd22021-10-29 02:39:30 +0000676 else if( verify_result & ( MBEDTLS_X509_BADCERT_KEY_USAGE |
677 MBEDTLS_X509_BADCERT_EXT_KEY_USAGE |
678 MBEDTLS_X509_BADCERT_NS_CERT_TYPE |
679 MBEDTLS_X509_BADCERT_BAD_PK |
680 MBEDTLS_X509_BADCERT_BAD_KEY ) )
Xiaofei Bai947571e2021-09-29 09:12:03 +0000681 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT, ret );
Xiaofei Baiff456022021-10-28 06:50:17 +0000682 else if( verify_result & MBEDTLS_X509_BADCERT_EXPIRED )
Xiaofei Bai947571e2021-09-29 09:12:03 +0000683 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_CERT_EXPIRED, ret );
Xiaofei Baiff456022021-10-28 06:50:17 +0000684 else if( verify_result & MBEDTLS_X509_BADCERT_REVOKED )
Xiaofei Bai947571e2021-09-29 09:12:03 +0000685 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_CERT_REVOKED, ret );
Xiaofei Baiff456022021-10-28 06:50:17 +0000686 else if( verify_result & MBEDTLS_X509_BADCERT_NOT_TRUSTED )
Xiaofei Bai947571e2021-09-29 09:12:03 +0000687 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_UNKNOWN_CA, ret );
688 else
689 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_CERT_UNKNOWN, ret );
690 }
691
692#if defined(MBEDTLS_DEBUG_C)
Xiaofei Baiff456022021-10-28 06:50:17 +0000693 if( verify_result != 0 )
Xiaofei Bai947571e2021-09-29 09:12:03 +0000694 {
695 MBEDTLS_SSL_DEBUG_MSG( 3, ( "! Certificate verification flags %08x",
Jerry Yu83bb1312021-10-28 22:16:33 +0800696 (unsigned int) verify_result ) );
Xiaofei Bai947571e2021-09-29 09:12:03 +0000697 }
698 else
699 {
700 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Certificate verification flags clear" ) );
701 }
702#endif /* MBEDTLS_DEBUG_C */
703
Xiaofei Baiff456022021-10-28 06:50:17 +0000704 ssl->session_negotiate->verify_result = verify_result;
Xiaofei Bai947571e2021-09-29 09:12:03 +0000705 return( ret );
706}
707#else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
708static int ssl_tls13_validate_certificate( mbedtls_ssl_context *ssl )
709{
710 ((void) ssl);
711 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
712}
713#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
714#endif /* MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED */
715
Xiaofei Bai79595ac2021-10-26 07:16:45 +0000716int mbedtls_ssl_tls13_process_certificate( mbedtls_ssl_context *ssl )
Xiaofei Bai947571e2021-09-29 09:12:03 +0000717{
Xiaofei Bai79595ac2021-10-26 07:16:45 +0000718 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
719 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) );
720
721#if defined(MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED)
XiaokangQianc3017f62022-05-13 05:55:41 +0000722 unsigned char *buf;
723 size_t buf_len;
Xiaofei Bai79595ac2021-10-26 07:16:45 +0000724
XiaokangQianc3017f62022-05-13 05:55:41 +0000725 MBEDTLS_SSL_PROC_CHK( mbedtls_ssl_tls13_fetch_handshake_msg(
726 ssl, MBEDTLS_SSL_HS_CERTIFICATE,
727 &buf, &buf_len ) );
Xiaofei Bai79595ac2021-10-26 07:16:45 +0000728
XiaokangQianc3017f62022-05-13 05:55:41 +0000729 /* Parse the certificate chain sent by the peer. */
730 MBEDTLS_SSL_PROC_CHK( ssl_tls13_parse_certificate( ssl, buf,
731 buf + buf_len ) );
732 /* Validate the certificate chain and set the verification results. */
733 MBEDTLS_SSL_PROC_CHK( ssl_tls13_validate_certificate( ssl ) );
Xiaofei Bai79595ac2021-10-26 07:16:45 +0000734
XiaokangQianc3017f62022-05-13 05:55:41 +0000735 mbedtls_ssl_add_hs_msg_to_checksum( ssl, MBEDTLS_SSL_HS_CERTIFICATE,
736 buf, buf_len );
Xiaofei Bai79595ac2021-10-26 07:16:45 +0000737
Xiaofei Bai79595ac2021-10-26 07:16:45 +0000738cleanup:
XiaokangQianaca90482022-05-19 07:19:31 +0000739#endif /* MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED */
Xiaofei Bai79595ac2021-10-26 07:16:45 +0000740
741 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse certificate" ) );
742 return( ret );
Xiaofei Bai947571e2021-09-29 09:12:03 +0000743}
Jerry Yu90f152d2022-01-29 22:12:42 +0800744#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Jerry Yu7399d0d2022-01-30 17:54:19 +0800745/*
746 * enum {
747 * X509(0),
748 * RawPublicKey(2),
749 * (255)
750 * } CertificateType;
751 *
752 * struct {
753 * select (certificate_type) {
754 * case RawPublicKey:
755 * // From RFC 7250 ASN.1_subjectPublicKeyInfo
756 * opaque ASN1_subjectPublicKeyInfo<1..2^24-1>;
757 *
758 * case X509:
759 * opaque cert_data<1..2^24-1>;
760 * };
761 * Extension extensions<0..2^16-1>;
762 * } CertificateEntry;
763 *
764 * struct {
765 * opaque certificate_request_context<0..2^8-1>;
766 * CertificateEntry certificate_list<0..2^24-1>;
767 * } Certificate;
768 */
769static int ssl_tls13_write_certificate_body( mbedtls_ssl_context *ssl,
Jerry Yu3e536442022-02-15 11:05:59 +0800770 unsigned char *buf,
Jerry Yu7399d0d2022-01-30 17:54:19 +0800771 unsigned char *end,
Jerry Yu3e536442022-02-15 11:05:59 +0800772 size_t *out_len )
Jerry Yu5cc35062022-01-28 16:16:08 +0800773{
Jerry Yu5cc35062022-01-28 16:16:08 +0800774 const mbedtls_x509_crt *crt = mbedtls_ssl_own_cert( ssl );
Jerry Yu3e536442022-02-15 11:05:59 +0800775 unsigned char *p = buf;
Jerry Yuc8d8d4e2022-02-18 12:10:03 +0800776 unsigned char *certificate_request_context =
777 ssl->handshake->certificate_request_context;
778 unsigned char certificate_request_context_len =
779 ssl->handshake->certificate_request_context_len;
780 unsigned char *p_certificate_list_len;
Jerry Yu5cc35062022-01-28 16:16:08 +0800781
Jerry Yu5cc35062022-01-28 16:16:08 +0800782
Jerry Yu3391ac02022-02-16 11:21:37 +0800783 /* ...
784 * opaque certificate_request_context<0..2^8-1>;
785 * ...
786 */
Jerry Yuc8d8d4e2022-02-18 12:10:03 +0800787 MBEDTLS_SSL_CHK_BUF_PTR( p, end, certificate_request_context_len + 1 );
788 *p++ = certificate_request_context_len;
789 if( certificate_request_context_len > 0 )
Jerry Yu537530d2022-02-15 14:00:57 +0800790 {
Jerry Yuc8d8d4e2022-02-18 12:10:03 +0800791 memcpy( p, certificate_request_context, certificate_request_context_len );
792 p += certificate_request_context_len;
Jerry Yu537530d2022-02-15 14:00:57 +0800793 }
794
Jerry Yu3391ac02022-02-16 11:21:37 +0800795 /* ...
796 * CertificateEntry certificate_list<0..2^24-1>;
797 * ...
798 */
Jerry Yu3e536442022-02-15 11:05:59 +0800799 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 3 );
Jerry Yuc8d8d4e2022-02-18 12:10:03 +0800800 p_certificate_list_len = p;
Jerry Yu3e536442022-02-15 11:05:59 +0800801 p += 3;
802
Jerry Yu7399d0d2022-01-30 17:54:19 +0800803 MBEDTLS_SSL_DEBUG_CRT( 3, "own certificate", crt );
Jerry Yu5cc35062022-01-28 16:16:08 +0800804
Jerry Yuc8d8d4e2022-02-18 12:10:03 +0800805 while( crt != NULL )
Jerry Yu5cc35062022-01-28 16:16:08 +0800806 {
Jerry Yu7399d0d2022-01-30 17:54:19 +0800807 size_t cert_data_len = crt->raw.len;
Jerry Yu5cc35062022-01-28 16:16:08 +0800808
Jerry Yu7399d0d2022-01-30 17:54:19 +0800809 MBEDTLS_SSL_CHK_BUF_PTR( p, end, cert_data_len + 3 + 2 );
810 MBEDTLS_PUT_UINT24_BE( cert_data_len, p, 0 );
811 p += 3;
Jerry Yu5cc35062022-01-28 16:16:08 +0800812
Jerry Yu7399d0d2022-01-30 17:54:19 +0800813 memcpy( p, crt->raw.p, cert_data_len );
814 p += cert_data_len;
815 crt = crt->next;
Jerry Yu5cc35062022-01-28 16:16:08 +0800816
817 /* Currently, we don't have any certificate extensions defined.
818 * Hence, we are sending an empty extension with length zero.
819 */
Jerry Yu7399d0d2022-01-30 17:54:19 +0800820 MBEDTLS_PUT_UINT24_BE( 0, p, 0 );
821 p += 2;
Jerry Yu5cc35062022-01-28 16:16:08 +0800822 }
Jerry Yu5cc35062022-01-28 16:16:08 +0800823
Jerry Yuc8d8d4e2022-02-18 12:10:03 +0800824 MBEDTLS_PUT_UINT24_BE( p - p_certificate_list_len - 3,
825 p_certificate_list_len, 0 );
Jerry Yu7399d0d2022-01-30 17:54:19 +0800826
Jerry Yu3e536442022-02-15 11:05:59 +0800827 *out_len = p - buf;
Jerry Yu5cc35062022-01-28 16:16:08 +0800828
829 return( 0 );
830}
Jerry Yu5cc35062022-01-28 16:16:08 +0800831
Jerry Yu3e536442022-02-15 11:05:59 +0800832int mbedtls_ssl_tls13_write_certificate( mbedtls_ssl_context *ssl )
Jerry Yu5cc35062022-01-28 16:16:08 +0800833{
834 int ret;
Ronald Cron5bb8fc82022-03-09 07:00:13 +0100835 unsigned char *buf;
836 size_t buf_len, msg_len;
837
Jerry Yu5cc35062022-01-28 16:16:08 +0800838 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write certificate" ) );
839
Ronald Cron8f6d39a2022-03-10 18:56:50 +0100840 MBEDTLS_SSL_PROC_CHK( mbedtls_ssl_start_handshake_msg( ssl,
Ronald Cron5bb8fc82022-03-09 07:00:13 +0100841 MBEDTLS_SSL_HS_CERTIFICATE, &buf, &buf_len ) );
Jerry Yu5cc35062022-01-28 16:16:08 +0800842
Ronald Cron5bb8fc82022-03-09 07:00:13 +0100843 MBEDTLS_SSL_PROC_CHK( ssl_tls13_write_certificate_body( ssl,
844 buf,
845 buf + buf_len,
846 &msg_len ) );
Jerry Yu5cc35062022-01-28 16:16:08 +0800847
Ronald Cron8f6d39a2022-03-10 18:56:50 +0100848 mbedtls_ssl_add_hs_msg_to_checksum( ssl, MBEDTLS_SSL_HS_CERTIFICATE,
849 buf, msg_len );
Jerry Yu5cc35062022-01-28 16:16:08 +0800850
Ronald Cron8f6d39a2022-03-10 18:56:50 +0100851 MBEDTLS_SSL_PROC_CHK( mbedtls_ssl_finish_handshake_msg(
Ronald Cron5bb8fc82022-03-09 07:00:13 +0100852 ssl, buf_len, msg_len ) );
Jerry Yu5cc35062022-01-28 16:16:08 +0800853cleanup:
854
855 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write certificate" ) );
856 return( ret );
857}
858
Jerry Yu3e536442022-02-15 11:05:59 +0800859/*
860 * STATE HANDLING: Output Certificate Verify
861 */
Jerry Yue91a51a2022-03-22 21:42:50 +0800862static int ssl_tls13_get_sig_alg_from_pk( mbedtls_ssl_context *ssl,
863 mbedtls_pk_context *own_key,
Jerry Yu8c338862022-03-23 13:34:04 +0800864 uint16_t *algorithm )
Jerry Yu67eced02022-02-25 13:37:36 +0800865{
866 mbedtls_pk_type_t sig = mbedtls_ssl_sig_from_pk( own_key );
867 /* Determine the size of the key */
868 size_t own_key_size = mbedtls_pk_get_bitlen( own_key );
Jerry Yue91a51a2022-03-22 21:42:50 +0800869 *algorithm = MBEDTLS_TLS1_3_SIG_NONE;
Jerry Yu67eced02022-02-25 13:37:36 +0800870 ((void) own_key_size);
871
872 switch( sig )
873 {
874#if defined(MBEDTLS_ECDSA_C)
875 case MBEDTLS_SSL_SIG_ECDSA:
876 switch( own_key_size )
877 {
878 case 256:
Jerry Yue91a51a2022-03-22 21:42:50 +0800879 *algorithm = MBEDTLS_TLS1_3_SIG_ECDSA_SECP256R1_SHA256;
Jerry Yue91a51a2022-03-22 21:42:50 +0800880 return( 0 );
Jerry Yu67eced02022-02-25 13:37:36 +0800881 case 384:
Jerry Yue91a51a2022-03-22 21:42:50 +0800882 *algorithm = MBEDTLS_TLS1_3_SIG_ECDSA_SECP384R1_SHA384;
Jerry Yue91a51a2022-03-22 21:42:50 +0800883 return( 0 );
Jerry Yu67eced02022-02-25 13:37:36 +0800884 case 521:
Jerry Yue91a51a2022-03-22 21:42:50 +0800885 *algorithm = MBEDTLS_TLS1_3_SIG_ECDSA_SECP521R1_SHA512;
Jerry Yue91a51a2022-03-22 21:42:50 +0800886 return( 0 );
Jerry Yu67eced02022-02-25 13:37:36 +0800887 default:
888 MBEDTLS_SSL_DEBUG_MSG( 3,
889 ( "unknown key size: %"
890 MBEDTLS_PRINTF_SIZET " bits",
891 own_key_size ) );
892 break;
893 }
894 break;
895#endif /* MBEDTLS_ECDSA_C */
896
Jerry Yucef3f332022-03-22 23:00:13 +0800897#if defined(MBEDTLS_RSA_C)
Jerry Yu67eced02022-02-25 13:37:36 +0800898 case MBEDTLS_SSL_SIG_RSA:
Jerry Yucef3f332022-03-22 23:00:13 +0800899#if defined(MBEDTLS_PKCS1_V21)
900#if defined(MBEDTLS_SHA256_C)
Jerry Yu67eced02022-02-25 13:37:36 +0800901 if( own_key_size <= 2048 &&
902 mbedtls_ssl_sig_alg_is_received( ssl,
903 MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA256 ) )
904 {
Jerry Yue91a51a2022-03-22 21:42:50 +0800905 *algorithm = MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA256;
Jerry Yue91a51a2022-03-22 21:42:50 +0800906 return( 0 );
Jerry Yu67eced02022-02-25 13:37:36 +0800907 }
Jerry Yucef3f332022-03-22 23:00:13 +0800908 else
909#endif /* MBEDTLS_SHA256_C */
910#if defined(MBEDTLS_SHA384_C)
911 if( own_key_size <= 3072 &&
912 mbedtls_ssl_sig_alg_is_received( ssl,
Jerry Yu67eced02022-02-25 13:37:36 +0800913 MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA384 ) )
914 {
Jerry Yue91a51a2022-03-22 21:42:50 +0800915 *algorithm = MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA384;
Jerry Yue91a51a2022-03-22 21:42:50 +0800916 return( 0 );
Jerry Yu67eced02022-02-25 13:37:36 +0800917 }
Jerry Yucef3f332022-03-22 23:00:13 +0800918 else
919#endif /* MBEDTLS_SHA384_C */
920#if defined(MBEDTLS_SHA512_C)
921 if( own_key_size <= 4096 &&
922 mbedtls_ssl_sig_alg_is_received( ssl,
Jerry Yu67eced02022-02-25 13:37:36 +0800923 MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA512 ) )
924 {
Jerry Yue91a51a2022-03-22 21:42:50 +0800925 *algorithm = MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA512;
Jerry Yue91a51a2022-03-22 21:42:50 +0800926 return( 0 );
Jerry Yu67eced02022-02-25 13:37:36 +0800927 }
Jerry Yucef3f332022-03-22 23:00:13 +0800928 else
929#endif /* MBEDTLS_SHA512_C */
930#endif /* MBEDTLS_PKCS1_V21 */
931#if defined(MBEDTLS_PKCS1_V15)
932#if defined(MBEDTLS_SHA256_C)
933 if( own_key_size <= 2048 &&
934 mbedtls_ssl_sig_alg_is_received( ssl,
935 MBEDTLS_TLS1_3_SIG_RSA_PKCS1_SHA256 ) )
936 {
937 *algorithm = MBEDTLS_TLS1_3_SIG_RSA_PKCS1_SHA256;
Jerry Yucef3f332022-03-22 23:00:13 +0800938 return( 0 );
939 }
940 else
941#endif /* MBEDTLS_SHA256_C */
942#if defined(MBEDTLS_SHA384_C)
943 if( own_key_size <= 3072 &&
944 mbedtls_ssl_sig_alg_is_received( ssl,
945 MBEDTLS_TLS1_3_SIG_RSA_PKCS1_SHA384 ) )
946 {
947 *algorithm = MBEDTLS_TLS1_3_SIG_RSA_PKCS1_SHA384;
Jerry Yucef3f332022-03-22 23:00:13 +0800948 return( 0 );
949 }
950 else
951#endif /* MBEDTLS_SHA384_C */
952#if defined(MBEDTLS_SHA512_C)
953 if( own_key_size <= 4096 &&
954 mbedtls_ssl_sig_alg_is_received( ssl,
955 MBEDTLS_TLS1_3_SIG_RSA_PKCS1_SHA512 ) )
956 {
957 *algorithm = MBEDTLS_TLS1_3_SIG_RSA_PKCS1_SHA512;
Jerry Yucef3f332022-03-22 23:00:13 +0800958 return( 0 );
959 }
960 else
961#endif /* MBEDTLS_SHA512_C */
962#endif /* MBEDTLS_PKCS1_V15 */
963 {
964 MBEDTLS_SSL_DEBUG_MSG( 3,
965 ( "unknown key size: %"
966 MBEDTLS_PRINTF_SIZET " bits",
967 own_key_size ) );
968 }
Jerry Yu67eced02022-02-25 13:37:36 +0800969 break;
Jerry Yucef3f332022-03-22 23:00:13 +0800970#endif /* MBEDTLS_RSA_C */
Jerry Yu67eced02022-02-25 13:37:36 +0800971 default:
972 MBEDTLS_SSL_DEBUG_MSG( 1,
Andrzej Kurek5c65c572022-04-13 14:28:52 -0400973 ( "unknown signature type : %u", sig ) );
Jerry Yu67eced02022-02-25 13:37:36 +0800974 break;
975 }
Jerry Yue91a51a2022-03-22 21:42:50 +0800976 return( -1 );
Jerry Yu67eced02022-02-25 13:37:36 +0800977}
978
Jerry Yu3e536442022-02-15 11:05:59 +0800979static int ssl_tls13_write_certificate_verify_body( mbedtls_ssl_context *ssl,
980 unsigned char *buf,
981 unsigned char *end,
982 size_t *out_len )
Jerry Yu8511f122022-01-29 10:01:04 +0800983{
984 int ret;
Jerry Yu3e536442022-02-15 11:05:59 +0800985 unsigned char *p = buf;
Jerry Yu8511f122022-01-29 10:01:04 +0800986 mbedtls_pk_context *own_key;
Jerry Yu3e536442022-02-15 11:05:59 +0800987
Jerry Yu8511f122022-01-29 10:01:04 +0800988 unsigned char handshake_hash[ MBEDTLS_TLS1_3_MD_MAX_SIZE ];
989 size_t handshake_hash_len;
Jerry Yu3e536442022-02-15 11:05:59 +0800990 unsigned char verify_buffer[ SSL_VERIFY_STRUCT_MAX_SIZE ];
991 size_t verify_buffer_len;
Jerry Yu67eced02022-02-25 13:37:36 +0800992 mbedtls_pk_type_t pk_type = MBEDTLS_PK_NONE;
Jerry Yu67eced02022-02-25 13:37:36 +0800993 mbedtls_md_type_t md_alg = MBEDTLS_MD_NONE;
Jerry Yu78272072022-02-22 10:28:13 +0800994 uint16_t algorithm = MBEDTLS_TLS1_3_SIG_NONE;
Jerry Yu3e536442022-02-15 11:05:59 +0800995 size_t signature_len = 0;
Jerry Yu8511f122022-01-29 10:01:04 +0800996 const mbedtls_md_info_t *md_info;
Jerry Yu3391ac02022-02-16 11:21:37 +0800997 unsigned char verify_hash[ MBEDTLS_MD_MAX_SIZE ];
Jerry Yu3e536442022-02-15 11:05:59 +0800998 size_t verify_hash_len;
Jerry Yu8511f122022-01-29 10:01:04 +0800999
Jerry Yu0b7b1012022-02-23 12:23:05 +08001000 *out_len = 0;
1001
Jerry Yu3e536442022-02-15 11:05:59 +08001002 own_key = mbedtls_ssl_own_key( ssl );
1003 if( own_key == NULL )
Jerry Yu8511f122022-01-29 10:01:04 +08001004 {
Jerry Yu3e536442022-02-15 11:05:59 +08001005 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1006 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Jerry Yu8511f122022-01-29 10:01:04 +08001007 }
1008
Jerry Yu8511f122022-01-29 10:01:04 +08001009 ret = mbedtls_ssl_get_handshake_transcript( ssl,
1010 ssl->handshake->ciphersuite_info->mac,
1011 handshake_hash,
1012 sizeof( handshake_hash ),
1013 &handshake_hash_len );
1014 if( ret != 0 )
1015 return( ret );
1016
1017 MBEDTLS_SSL_DEBUG_BUF( 3, "handshake hash",
1018 handshake_hash,
1019 handshake_hash_len);
1020
Jerry Yu8511f122022-01-29 10:01:04 +08001021 ssl_tls13_create_verify_structure( handshake_hash, handshake_hash_len,
1022 verify_buffer, &verify_buffer_len,
1023 ssl->conf->endpoint );
1024
1025 /*
1026 * struct {
1027 * SignatureScheme algorithm;
1028 * opaque signature<0..2^16-1>;
1029 * } CertificateVerify;
1030 */
Jerry Yu8c338862022-03-23 13:34:04 +08001031 ret = ssl_tls13_get_sig_alg_from_pk( ssl, own_key, &algorithm );
Jerry Yue91a51a2022-03-22 21:42:50 +08001032 if( ret != 0 || ! mbedtls_ssl_sig_alg_is_received( ssl, algorithm ) )
Jerry Yu8511f122022-01-29 10:01:04 +08001033 {
Jerry Yud66409a2022-02-18 16:42:24 +08001034 MBEDTLS_SSL_DEBUG_MSG( 1,
Jerry Yu2124d052022-02-18 21:07:18 +08001035 ( "signature algorithm not in received or offered list." ) );
Jerry Yu67eced02022-02-25 13:37:36 +08001036
1037 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Signature algorithm is %s",
1038 mbedtls_ssl_sig_alg_to_str( algorithm ) ) );
1039
Jerry Yu71f36f12022-02-23 17:34:29 +08001040 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE,
Jerry Yud66409a2022-02-18 16:42:24 +08001041 MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
Jerry Yu3391ac02022-02-16 11:21:37 +08001042 return( MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
Jerry Yu8511f122022-01-29 10:01:04 +08001043 }
1044
Jerry Yu6c6f1022022-03-25 11:09:50 +08001045 if( mbedtls_ssl_tls13_get_pk_type_and_md_alg_from_sig_alg(
1046 algorithm, &pk_type, &md_alg ) != 0 )
Jerry Yu8c338862022-03-23 13:34:04 +08001047 {
Jerry Yuf8aa9a42022-03-23 20:40:28 +08001048 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Jerry Yu8c338862022-03-23 13:34:04 +08001049 }
1050
Jerry Yu3391ac02022-02-16 11:21:37 +08001051 /* Check there is space for the algorithm identifier (2 bytes) and the
1052 * signature length (2 bytes).
1053 */
1054 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 4 );
Jerry Yu3e536442022-02-15 11:05:59 +08001055 MBEDTLS_PUT_UINT16_BE( algorithm, p, 0 );
1056 p += 2;
Jerry Yu8511f122022-01-29 10:01:04 +08001057
1058 /* Hash verify buffer with indicated hash function */
1059 md_info = mbedtls_md_info_from_type( md_alg );
1060 if( md_info == NULL )
1061 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
1062
1063 ret = mbedtls_md( md_info, verify_buffer, verify_buffer_len, verify_hash );
1064 if( ret != 0 )
1065 return( ret );
1066
1067 verify_hash_len = mbedtls_md_get_size( md_info );
1068 MBEDTLS_SSL_DEBUG_BUF( 3, "verify hash", verify_hash, verify_hash_len );
1069
Jerry Yu8beb9e12022-03-12 16:23:53 +08001070 if( ( ret = mbedtls_pk_sign_ext( pk_type, own_key,
1071 md_alg, verify_hash, verify_hash_len,
Jerry Yu67eced02022-02-25 13:37:36 +08001072 p + 2, (size_t)( end - ( p + 2 ) ), &signature_len,
1073 ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 )
Jerry Yu8511f122022-01-29 10:01:04 +08001074 {
1075 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_pk_sign", ret );
1076 return( ret );
1077 }
Jerry Yu8511f122022-01-29 10:01:04 +08001078
Jerry Yu3e536442022-02-15 11:05:59 +08001079 MBEDTLS_PUT_UINT16_BE( signature_len, p, 0 );
1080 p += 2 + signature_len;
Jerry Yu8511f122022-01-29 10:01:04 +08001081
Jerry Yu3e536442022-02-15 11:05:59 +08001082 *out_len = (size_t)( p - buf );
1083
Jerry Yu8511f122022-01-29 10:01:04 +08001084 return( ret );
1085}
Jerry Yu8511f122022-01-29 10:01:04 +08001086
Jerry Yu3e536442022-02-15 11:05:59 +08001087int mbedtls_ssl_tls13_write_certificate_verify( mbedtls_ssl_context *ssl )
Jerry Yu8511f122022-01-29 10:01:04 +08001088{
1089 int ret = 0;
Jerry Yuca133a32022-02-15 14:22:05 +08001090 unsigned char *buf;
1091 size_t buf_len, msg_len;
1092
Jerry Yu8511f122022-01-29 10:01:04 +08001093 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write certificate verify" ) );
1094
Ronald Cron8f6d39a2022-03-10 18:56:50 +01001095 MBEDTLS_SSL_PROC_CHK( mbedtls_ssl_start_handshake_msg( ssl,
Jerry Yuca133a32022-02-15 14:22:05 +08001096 MBEDTLS_SSL_HS_CERTIFICATE_VERIFY, &buf, &buf_len ) );
Jerry Yu8511f122022-01-29 10:01:04 +08001097
Jerry Yuca133a32022-02-15 14:22:05 +08001098 MBEDTLS_SSL_PROC_CHK( ssl_tls13_write_certificate_verify_body(
1099 ssl, buf, buf + buf_len, &msg_len ) );
Jerry Yu8511f122022-01-29 10:01:04 +08001100
Ronald Cron8f6d39a2022-03-10 18:56:50 +01001101 mbedtls_ssl_add_hs_msg_to_checksum( ssl, MBEDTLS_SSL_HS_CERTIFICATE_VERIFY,
1102 buf, msg_len );
Jerry Yu8511f122022-01-29 10:01:04 +08001103
Ronald Cron8f6d39a2022-03-10 18:56:50 +01001104 MBEDTLS_SSL_PROC_CHK( mbedtls_ssl_finish_handshake_msg(
Jerry Yuca133a32022-02-15 14:22:05 +08001105 ssl, buf_len, msg_len ) );
Jerry Yu8511f122022-01-29 10:01:04 +08001106
1107cleanup:
1108
1109 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write certificate verify" ) );
1110 return( ret );
1111}
1112
Jerry Yu90f152d2022-01-29 22:12:42 +08001113#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
1114
Jerry Yu5cc35062022-01-28 16:16:08 +08001115/*
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001116 *
XiaokangQianc5c39d52021-11-09 11:55:10 +00001117 * STATE HANDLING: Incoming Finished message.
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001118 */
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001119/*
1120 * Implementation
1121 */
1122
XiaokangQianaaa0e192021-11-10 03:07:04 +00001123static int ssl_tls13_preprocess_finished_message( mbedtls_ssl_context *ssl )
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001124{
1125 int ret;
1126
XiaokangQianc5c39d52021-11-09 11:55:10 +00001127 ret = mbedtls_ssl_tls13_calculate_verify_data( ssl,
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001128 ssl->handshake->state_local.finished_in.digest,
1129 sizeof( ssl->handshake->state_local.finished_in.digest ),
1130 &ssl->handshake->state_local.finished_in.digest_len,
XiaokangQianc5c39d52021-11-09 11:55:10 +00001131 ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT ?
XiaokangQianc13f9352021-11-11 06:13:22 +00001132 MBEDTLS_SSL_IS_SERVER : MBEDTLS_SSL_IS_CLIENT );
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001133 if( ret != 0 )
1134 {
XiaokangQianc5c39d52021-11-09 11:55:10 +00001135 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_tls13_calculate_verify_data", ret );
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001136 return( ret );
1137 }
1138
1139 return( 0 );
1140}
1141
XiaokangQianc5c39d52021-11-09 11:55:10 +00001142static int ssl_tls13_parse_finished_message( mbedtls_ssl_context *ssl,
1143 const unsigned char *buf,
1144 const unsigned char *end )
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001145{
XiaokangQian33062842021-11-11 03:37:45 +00001146 /*
1147 * struct {
XiaokangQianc13f9352021-11-11 06:13:22 +00001148 * opaque verify_data[Hash.length];
XiaokangQian33062842021-11-11 03:37:45 +00001149 * } Finished;
1150 */
1151 const unsigned char *expected_verify_data =
1152 ssl->handshake->state_local.finished_in.digest;
1153 size_t expected_verify_data_len =
1154 ssl->handshake->state_local.finished_in.digest_len;
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001155 /* Structural validation */
XiaokangQian33062842021-11-11 03:37:45 +00001156 if( (size_t)( end - buf ) != expected_verify_data_len )
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001157 {
1158 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
1159
1160 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR,
XiaokangQianc13f9352021-11-11 06:13:22 +00001161 MBEDTLS_ERR_SSL_DECODE_ERROR );
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001162 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
1163 }
1164
XiaokangQianc5c39d52021-11-09 11:55:10 +00001165 MBEDTLS_SSL_DEBUG_BUF( 4, "verify_data (self-computed):",
XiaokangQian33062842021-11-11 03:37:45 +00001166 expected_verify_data,
1167 expected_verify_data_len );
XiaokangQianc5c39d52021-11-09 11:55:10 +00001168 MBEDTLS_SSL_DEBUG_BUF( 4, "verify_data (received message):", buf,
XiaokangQian33062842021-11-11 03:37:45 +00001169 expected_verify_data_len );
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001170
1171 /* Semantic validation */
Gabor Mezei685472b2021-11-24 11:17:36 +01001172 if( mbedtls_ct_memcmp( buf,
1173 expected_verify_data,
1174 expected_verify_data_len ) != 0 )
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001175 {
1176 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
1177
XiaokangQian33062842021-11-11 03:37:45 +00001178 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_DECRYPT_ERROR,
XiaokangQianc13f9352021-11-11 06:13:22 +00001179 MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001180 return( MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
1181 }
1182 return( 0 );
1183}
1184
XiaokangQianc5c39d52021-11-09 11:55:10 +00001185int mbedtls_ssl_tls13_process_finished_message( mbedtls_ssl_context *ssl )
1186{
XiaokangQian33062842021-11-11 03:37:45 +00001187 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
XiaokangQianc5c39d52021-11-09 11:55:10 +00001188 unsigned char *buf;
Xiaofei Baieef15042021-11-18 07:29:56 +00001189 size_t buf_len;
XiaokangQianc5c39d52021-11-09 11:55:10 +00001190
XiaokangQiand0aa3e92021-11-10 06:17:40 +00001191 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse finished message" ) );
XiaokangQianc5c39d52021-11-09 11:55:10 +00001192
1193 /* Preprocessing step: Compute handshake digest */
XiaokangQianaaa0e192021-11-10 03:07:04 +00001194 MBEDTLS_SSL_PROC_CHK( ssl_tls13_preprocess_finished_message( ssl ) );
XiaokangQianc5c39d52021-11-09 11:55:10 +00001195
Xiaofei Bai746f9482021-11-12 08:53:56 +00001196 MBEDTLS_SSL_PROC_CHK( mbedtls_ssl_tls13_fetch_handshake_msg( ssl,
XiaokangQianc5c39d52021-11-09 11:55:10 +00001197 MBEDTLS_SSL_HS_FINISHED,
Xiaofei Baieef15042021-11-18 07:29:56 +00001198 &buf, &buf_len ) );
1199 MBEDTLS_SSL_PROC_CHK( ssl_tls13_parse_finished_message( ssl, buf, buf + buf_len ) );
Ronald Cron8f6d39a2022-03-10 18:56:50 +01001200 mbedtls_ssl_add_hs_msg_to_checksum( ssl, MBEDTLS_SSL_HS_FINISHED,
1201 buf, buf_len );
XiaokangQianc5c39d52021-11-09 11:55:10 +00001202
1203cleanup:
1204
XiaokangQiand0aa3e92021-11-10 06:17:40 +00001205 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse finished message" ) );
XiaokangQianc5c39d52021-11-09 11:55:10 +00001206 return( ret );
1207}
1208
XiaokangQian74af2a82021-09-22 07:40:30 +00001209/*
1210 *
XiaokangQiancc90c942021-11-09 12:30:09 +00001211 * STATE HANDLING: Write and send Finished message.
XiaokangQian74af2a82021-09-22 07:40:30 +00001212 *
1213 */
XiaokangQian74af2a82021-09-22 07:40:30 +00001214/*
XiaokangQian35dc6252021-11-11 08:16:19 +00001215 * Implement
XiaokangQian74af2a82021-09-22 07:40:30 +00001216 */
1217
XiaokangQian8773aa02021-11-10 07:33:09 +00001218static int ssl_tls13_prepare_finished_message( mbedtls_ssl_context *ssl )
XiaokangQian74af2a82021-09-22 07:40:30 +00001219{
1220 int ret;
1221
1222 /* Compute transcript of handshake up to now. */
XiaokangQiancc90c942021-11-09 12:30:09 +00001223 ret = mbedtls_ssl_tls13_calculate_verify_data( ssl,
XiaokangQian74af2a82021-09-22 07:40:30 +00001224 ssl->handshake->state_local.finished_out.digest,
1225 sizeof( ssl->handshake->state_local.finished_out.digest ),
1226 &ssl->handshake->state_local.finished_out.digest_len,
1227 ssl->conf->endpoint );
1228
1229 if( ret != 0 )
1230 {
Jerry Yu7ca30542021-12-08 15:57:57 +08001231 MBEDTLS_SSL_DEBUG_RET( 1, "calculate_verify_data failed", ret );
XiaokangQian74af2a82021-09-22 07:40:30 +00001232 return( ret );
1233 }
1234
1235 return( 0 );
1236}
1237
XiaokangQian8773aa02021-11-10 07:33:09 +00001238static int ssl_tls13_write_finished_message_body( mbedtls_ssl_context *ssl,
XiaokangQian35dc6252021-11-11 08:16:19 +00001239 unsigned char *buf,
1240 unsigned char *end,
Xiaofei Baid25fab62021-12-02 06:36:27 +00001241 size_t *out_len )
XiaokangQian74af2a82021-09-22 07:40:30 +00001242{
XiaokangQian8773aa02021-11-10 07:33:09 +00001243 size_t verify_data_len = ssl->handshake->state_local.finished_out.digest_len;
XiaokangQian0fa66432021-11-15 03:33:57 +00001244 /*
1245 * struct {
1246 * opaque verify_data[Hash.length];
1247 * } Finished;
1248 */
XiaokangQian8773aa02021-11-10 07:33:09 +00001249 MBEDTLS_SSL_CHK_BUF_PTR( buf, end, verify_data_len );
XiaokangQian74af2a82021-09-22 07:40:30 +00001250
1251 memcpy( buf, ssl->handshake->state_local.finished_out.digest,
XiaokangQian8773aa02021-11-10 07:33:09 +00001252 verify_data_len );
XiaokangQian74af2a82021-09-22 07:40:30 +00001253
Xiaofei Baid25fab62021-12-02 06:36:27 +00001254 *out_len = verify_data_len;
XiaokangQian74af2a82021-09-22 07:40:30 +00001255 return( 0 );
1256}
XiaokangQianc5c39d52021-11-09 11:55:10 +00001257
XiaokangQian35dc6252021-11-11 08:16:19 +00001258/* Main entry point: orchestrates the other functions */
1259int mbedtls_ssl_tls13_write_finished_message( mbedtls_ssl_context *ssl )
1260{
1261 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1262 unsigned char *buf;
1263 size_t buf_len, msg_len;
1264
1265 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write finished message" ) );
1266
XiaokangQiandce82242021-11-15 06:01:26 +00001267 MBEDTLS_SSL_PROC_CHK( ssl_tls13_prepare_finished_message( ssl ) );
1268
Ronald Cron8f6d39a2022-03-10 18:56:50 +01001269 MBEDTLS_SSL_PROC_CHK( mbedtls_ssl_start_handshake_msg( ssl,
XiaokangQian35dc6252021-11-11 08:16:19 +00001270 MBEDTLS_SSL_HS_FINISHED, &buf, &buf_len ) );
1271
1272 MBEDTLS_SSL_PROC_CHK( ssl_tls13_write_finished_message_body(
1273 ssl, buf, buf + buf_len, &msg_len ) );
1274
Ronald Cron8f6d39a2022-03-10 18:56:50 +01001275 mbedtls_ssl_add_hs_msg_to_checksum( ssl, MBEDTLS_SSL_HS_FINISHED,
1276 buf, msg_len );
XiaokangQian35dc6252021-11-11 08:16:19 +00001277
Ronald Cron8f6d39a2022-03-10 18:56:50 +01001278 MBEDTLS_SSL_PROC_CHK( mbedtls_ssl_finish_handshake_msg(
1279 ssl, buf_len, msg_len ) );
XiaokangQian35dc6252021-11-11 08:16:19 +00001280cleanup:
1281
1282 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write finished message" ) );
1283 return( ret );
1284}
1285
Jerry Yu378254d2021-10-30 21:44:47 +08001286void mbedtls_ssl_tls13_handshake_wrapup( mbedtls_ssl_context *ssl )
1287{
1288
1289 MBEDTLS_SSL_DEBUG_MSG( 3, ( "=> handshake wrapup" ) );
1290
Jerry Yue8c1fca2022-05-18 14:48:56 +08001291 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Switch to application keys for inbound traffic" ) );
1292 mbedtls_ssl_set_inbound_transform ( ssl, ssl->transform_application );
1293
1294 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Switch to application keys for outbound traffic" ) );
1295 mbedtls_ssl_set_outbound_transform( ssl, ssl->transform_application );
1296
Jerry Yu378254d2021-10-30 21:44:47 +08001297 /*
Jerry Yucfe64f02021-11-15 13:54:06 +08001298 * Free the previous session and switch to the current one.
Jerry Yu378254d2021-10-30 21:44:47 +08001299 */
1300 if( ssl->session )
1301 {
Jerry Yu378254d2021-10-30 21:44:47 +08001302 mbedtls_ssl_session_free( ssl->session );
1303 mbedtls_free( ssl->session );
1304 }
1305 ssl->session = ssl->session_negotiate;
1306 ssl->session_negotiate = NULL;
1307
1308 MBEDTLS_SSL_DEBUG_MSG( 3, ( "<= handshake wrapup" ) );
1309}
1310
Ronald Cron49ad6192021-11-24 16:25:31 +01001311/*
1312 *
1313 * STATE HANDLING: Write ChangeCipherSpec
1314 *
1315 */
1316#if defined(MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE)
Ronald Cron49ad6192021-11-24 16:25:31 +01001317static int ssl_tls13_write_change_cipher_spec_body( mbedtls_ssl_context *ssl,
1318 unsigned char *buf,
1319 unsigned char *end,
1320 size_t *olen )
1321{
1322 ((void) ssl);
1323
1324 MBEDTLS_SSL_CHK_BUF_PTR( buf, end, 1 );
1325 buf[0] = 1;
1326 *olen = 1;
1327
1328 return( 0 );
1329}
1330
Ronald Cron49ad6192021-11-24 16:25:31 +01001331int mbedtls_ssl_tls13_write_change_cipher_spec( mbedtls_ssl_context *ssl )
1332{
1333 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1334
1335 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write change cipher spec" ) );
1336
Ronald Cron49ad6192021-11-24 16:25:31 +01001337 /* Write CCS message */
1338 MBEDTLS_SSL_PROC_CHK( ssl_tls13_write_change_cipher_spec_body(
1339 ssl, ssl->out_msg,
1340 ssl->out_msg + MBEDTLS_SSL_OUT_CONTENT_LEN,
1341 &ssl->out_msglen ) );
1342
1343 ssl->out_msgtype = MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC;
1344
Ronald Cron49ad6192021-11-24 16:25:31 +01001345 /* Dispatch message */
Ronald Cron66dbf912022-02-02 15:33:46 +01001346 MBEDTLS_SSL_PROC_CHK( mbedtls_ssl_write_record( ssl, 0 ) );
Ronald Cron49ad6192021-11-24 16:25:31 +01001347
1348cleanup:
1349
1350 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write change cipher spec" ) );
1351 return( ret );
1352}
1353
1354#endif /* MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE */
1355
XiaokangQian78b1fa72022-01-19 06:56:30 +00001356/* Reset SSL context and update hash for handling HRR.
1357 *
1358 * Replace Transcript-Hash(X) by
1359 * Transcript-Hash( message_hash ||
1360 * 00 00 Hash.length ||
1361 * X )
1362 * A few states of the handshake are preserved, including:
1363 * - session ID
1364 * - session ticket
1365 * - negotiated ciphersuite
1366 */
1367int mbedtls_ssl_reset_transcript_for_hrr( mbedtls_ssl_context *ssl )
1368{
1369 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1370 unsigned char hash_transcript[ MBEDTLS_MD_MAX_SIZE + 4 ];
XiaokangQian0ece9982022-01-24 08:56:23 +00001371 size_t hash_len;
XiaokangQian78b1fa72022-01-19 06:56:30 +00001372 const mbedtls_ssl_ciphersuite_t *ciphersuite_info;
1373 uint16_t cipher_suite = ssl->session_negotiate->ciphersuite;
1374 ciphersuite_info = mbedtls_ssl_ciphersuite_from_id( cipher_suite );
1375
1376 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Reset SSL session for HRR" ) );
1377
XiaokangQian0ece9982022-01-24 08:56:23 +00001378 ret = mbedtls_ssl_get_handshake_transcript( ssl, ciphersuite_info->mac,
1379 hash_transcript + 4,
1380 MBEDTLS_MD_MAX_SIZE,
1381 &hash_len );
1382 if( ret != 0 )
1383 {
1384 MBEDTLS_SSL_DEBUG_RET( 4, "mbedtls_ssl_get_handshake_transcript", ret );
1385 return( ret );
1386 }
1387
1388 hash_transcript[0] = MBEDTLS_SSL_HS_MESSAGE_HASH;
1389 hash_transcript[1] = 0;
1390 hash_transcript[2] = 0;
1391 hash_transcript[3] = (unsigned char) hash_len;
1392
1393 hash_len += 4;
1394
XiaokangQian78b1fa72022-01-19 06:56:30 +00001395 if( ciphersuite_info->mac == MBEDTLS_MD_SHA256 )
1396 {
1397#if defined(MBEDTLS_SHA256_C)
XiaokangQian78b1fa72022-01-19 06:56:30 +00001398 MBEDTLS_SSL_DEBUG_BUF( 4, "Truncated SHA-256 handshake transcript",
XiaokangQian0ece9982022-01-24 08:56:23 +00001399 hash_transcript, hash_len );
XiaokangQian78b1fa72022-01-19 06:56:30 +00001400
1401#if defined(MBEDTLS_USE_PSA_CRYPTO)
1402 psa_hash_abort( &ssl->handshake->fin_sha256_psa );
1403 psa_hash_setup( &ssl->handshake->fin_sha256_psa, PSA_ALG_SHA_256 );
1404#else
1405 mbedtls_sha256_starts( &ssl->handshake->fin_sha256, 0 );
1406#endif
XiaokangQian78b1fa72022-01-19 06:56:30 +00001407#endif /* MBEDTLS_SHA256_C */
1408 }
1409 else if( ciphersuite_info->mac == MBEDTLS_MD_SHA384 )
1410 {
1411#if defined(MBEDTLS_SHA384_C)
XiaokangQian78b1fa72022-01-19 06:56:30 +00001412 MBEDTLS_SSL_DEBUG_BUF( 4, "Truncated SHA-384 handshake transcript",
XiaokangQian0ece9982022-01-24 08:56:23 +00001413 hash_transcript, hash_len );
XiaokangQian78b1fa72022-01-19 06:56:30 +00001414
1415#if defined(MBEDTLS_USE_PSA_CRYPTO)
1416 psa_hash_abort( &ssl->handshake->fin_sha384_psa );
1417 psa_hash_setup( &ssl->handshake->fin_sha384_psa, PSA_ALG_SHA_384 );
1418#else
1419 mbedtls_sha512_starts( &ssl->handshake->fin_sha512, 1 );
1420#endif
XiaokangQian78b1fa72022-01-19 06:56:30 +00001421#endif /* MBEDTLS_SHA384_C */
1422 }
1423
XiaokangQian0ece9982022-01-24 08:56:23 +00001424#if defined(MBEDTLS_SHA256_C) || defined(MBEDTLS_SHA384_C)
1425 ssl->handshake->update_checksum( ssl, hash_transcript, hash_len );
1426#endif /* MBEDTLS_SHA256_C || MBEDTLS_SHA384_C */
Przemyslaw Stekiel4b3fff42022-02-14 16:39:52 +01001427
XiaokangQian78b1fa72022-01-19 06:56:30 +00001428 return( ret );
1429}
1430
XiaokangQian9b5d04b2022-04-10 10:20:43 +00001431#if defined(MBEDTLS_ECDH_C)
XiaokangQian7807f9f2022-02-15 10:04:37 +00001432
XiaokangQian9b5d04b2022-04-10 10:20:43 +00001433int mbedtls_ssl_tls13_read_public_ecdhe_share( mbedtls_ssl_context *ssl,
1434 const unsigned char *buf,
1435 size_t buf_len )
XiaokangQian7807f9f2022-02-15 10:04:37 +00001436{
XiaokangQian9b5d04b2022-04-10 10:20:43 +00001437 uint8_t *p = (uint8_t*)buf;
XiaokangQiancfd925f2022-04-14 07:10:37 +00001438 const uint8_t *end = buf + buf_len;
XiaokangQian9b5d04b2022-04-10 10:20:43 +00001439 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001440
XiaokangQian9b5d04b2022-04-10 10:20:43 +00001441 /* Get size of the TLS opaque key_exchange field of the KeyShareEntry struct. */
XiaokangQiancfd925f2022-04-14 07:10:37 +00001442 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 2 );
XiaokangQian9b5d04b2022-04-10 10:20:43 +00001443 uint16_t peerkey_len = MBEDTLS_GET_UINT16_BE( p, 0 );
1444 p += 2;
XiaokangQian3207a322022-02-23 03:15:27 +00001445
XiaokangQian9b5d04b2022-04-10 10:20:43 +00001446 /* Check if key size is consistent with given buffer length. */
XiaokangQiancfd925f2022-04-14 07:10:37 +00001447 MBEDTLS_SSL_CHK_BUF_PTR( p, end, peerkey_len );
XiaokangQian9b5d04b2022-04-10 10:20:43 +00001448
1449 /* Store peer's ECDH public key. */
1450 memcpy( handshake->ecdh_psa_peerkey, p, peerkey_len );
1451 handshake->ecdh_psa_peerkey_len = peerkey_len;
1452
XiaokangQian3207a322022-02-23 03:15:27 +00001453 return( 0 );
1454}
Jerry Yu89e103c2022-03-30 22:43:29 +08001455
1456int mbedtls_ssl_tls13_generate_and_write_ecdh_key_exchange(
1457 mbedtls_ssl_context *ssl,
1458 uint16_t named_group,
1459 unsigned char *buf,
1460 unsigned char *end,
1461 size_t *out_len )
1462{
1463 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
1464 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
1465 psa_key_attributes_t key_attributes;
1466 size_t own_pubkey_len;
1467 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
1468 size_t ecdh_bits = 0;
1469
1470 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Perform PSA-based ECDH computation." ) );
1471
1472 /* Convert EC group to PSA key type. */
1473 if( ( handshake->ecdh_psa_type =
1474 mbedtls_psa_parse_tls_ecc_group( named_group, &ecdh_bits ) ) == 0 )
1475 return( MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
1476
1477 ssl->handshake->ecdh_bits = ecdh_bits;
1478
1479 key_attributes = psa_key_attributes_init();
1480 psa_set_key_usage_flags( &key_attributes, PSA_KEY_USAGE_DERIVE );
1481 psa_set_key_algorithm( &key_attributes, PSA_ALG_ECDH );
1482 psa_set_key_type( &key_attributes, handshake->ecdh_psa_type );
1483 psa_set_key_bits( &key_attributes, handshake->ecdh_bits );
1484
1485 /* Generate ECDH private key. */
1486 status = psa_generate_key( &key_attributes,
1487 &handshake->ecdh_psa_privkey );
1488 if( status != PSA_SUCCESS )
1489 {
1490 ret = psa_ssl_status_to_mbedtls( status );
1491 MBEDTLS_SSL_DEBUG_RET( 1, "psa_generate_key", ret );
1492 return( ret );
1493
1494 }
1495
1496 /* Export the public part of the ECDH private key from PSA. */
1497 status = psa_export_public_key( handshake->ecdh_psa_privkey,
1498 buf, (size_t)( end - buf ),
1499 &own_pubkey_len );
1500 if( status != PSA_SUCCESS )
1501 {
1502 ret = psa_ssl_status_to_mbedtls( status );
1503 MBEDTLS_SSL_DEBUG_RET( 1, "psa_export_public_key", ret );
1504 return( ret );
1505
1506 }
1507
1508 *out_len = own_pubkey_len;
1509
1510 return( 0 );
1511}
XiaokangQian9b5d04b2022-04-10 10:20:43 +00001512#endif /* MBEDTLS_ECDH_C */
XiaokangQian7807f9f2022-02-15 10:04:37 +00001513
Jerry Yufb4b6472022-01-27 15:03:26 +08001514#endif /* MBEDTLS_SSL_TLS_C && MBEDTLS_SSL_PROTO_TLS1_3 */