blob: 5340d843bea592e42172193edd3db19944343a71 [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 );
pespaceka1378102022-04-26 15:03:11 +0200256
XiaokangQian82d34cc2021-11-03 08:51:56 +0000257#if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT)
258 if( sig_alg == MBEDTLS_PK_RSASSA_PSS )
259 {
260 const mbedtls_md_info_t* md_info;
Xiaofei Baid25fab62021-12-02 06:36:27 +0000261 rsassa_pss_options.mgf1_hash_id = md_alg;
XiaokangQian82d34cc2021-11-03 08:51:56 +0000262 if( ( md_info = mbedtls_md_info_from_type( md_alg ) ) == NULL )
263 {
264 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
265 }
Xiaofei Baid25fab62021-12-02 06:36:27 +0000266 rsassa_pss_options.expected_salt_len = mbedtls_md_get_size( md_info );
267 options = (const void*) &rsassa_pss_options;
XiaokangQian82d34cc2021-11-03 08:51:56 +0000268 }
269#endif /* MBEDTLS_X509_RSASSA_PSS_SUPPORT */
Jerry Yu30b071c2021-09-12 20:16:03 +0800270
Xiaofei Baid25fab62021-12-02 06:36:27 +0000271 if( ( ret = mbedtls_pk_verify_ext( sig_alg, options,
Jerry Yu30b071c2021-09-12 20:16:03 +0800272 &ssl->session_negotiate->peer_cert->pk,
273 md_alg, verify_hash, verify_hash_len,
Jerry Yu6f87f252021-10-29 20:12:51 +0800274 p, signature_len ) ) == 0 )
Jerry Yu30b071c2021-09-12 20:16:03 +0800275 {
Jerry Yu6f87f252021-10-29 20:12:51 +0800276 return( 0 );
Jerry Yu30b071c2021-09-12 20:16:03 +0800277 }
Jerry Yu6f87f252021-10-29 20:12:51 +0800278 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_pk_verify_ext", ret );
Jerry Yu30b071c2021-09-12 20:16:03 +0800279
Jerry Yu6f87f252021-10-29 20:12:51 +0800280error:
281 /* RFC 8446 section 4.4.3
282 *
283 * If the verification fails, the receiver MUST terminate the handshake
284 * with a "decrypt_error" alert.
285 */
286 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_DECRYPT_ERROR,
287 MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
288 return( MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
289
Jerry Yu30b071c2021-09-12 20:16:03 +0800290}
291#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
292
293int mbedtls_ssl_tls13_process_certificate_verify( mbedtls_ssl_context *ssl )
294{
Jerry Yu30b071c2021-09-12 20:16:03 +0800295
Jerry Yuda8cdf22021-10-25 15:06:49 +0800296#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
297 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
298 unsigned char verify_buffer[SSL_VERIFY_STRUCT_MAX_SIZE];
299 size_t verify_buffer_len;
300 unsigned char transcript[MBEDTLS_TLS1_3_MD_MAX_SIZE];
301 size_t transcript_len;
302 unsigned char *buf;
303 size_t buf_len;
304
Jerry Yu30b071c2021-09-12 20:16:03 +0800305 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate verify" ) );
306
Jerry Yuda8cdf22021-10-25 15:06:49 +0800307 MBEDTLS_SSL_PROC_CHK(
Xiaofei Bai746f9482021-11-12 08:53:56 +0000308 mbedtls_ssl_tls13_fetch_handshake_msg( ssl,
Jerry Yuda8cdf22021-10-25 15:06:49 +0800309 MBEDTLS_SSL_HS_CERTIFICATE_VERIFY, &buf, &buf_len ) );
Jerry Yu30b071c2021-09-12 20:16:03 +0800310
Jerry Yuda8cdf22021-10-25 15:06:49 +0800311 /* Need to calculate the hash of the transcript first
Jerry Yu0b32c502021-10-28 13:41:59 +0800312 * before reading the message since otherwise it gets
313 * included in the transcript
314 */
Jerry Yuda8cdf22021-10-25 15:06:49 +0800315 ret = mbedtls_ssl_get_handshake_transcript( ssl,
316 ssl->handshake->ciphersuite_info->mac,
317 transcript, sizeof( transcript ),
318 &transcript_len );
319 if( ret != 0 )
Jerry Yu30b071c2021-09-12 20:16:03 +0800320 {
Jerry Yuda8cdf22021-10-25 15:06:49 +0800321 MBEDTLS_SSL_PEND_FATAL_ALERT(
322 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR,
323 MBEDTLS_ERR_SSL_INTERNAL_ERROR );
324 return( ret );
Jerry Yu30b071c2021-09-12 20:16:03 +0800325 }
326
Jerry Yuda8cdf22021-10-25 15:06:49 +0800327 MBEDTLS_SSL_DEBUG_BUF( 3, "handshake hash", transcript, transcript_len );
328
329 /* Create verify structure */
330 ssl_tls13_create_verify_structure( transcript,
Jerry Yu0b32c502021-10-28 13:41:59 +0800331 transcript_len,
332 verify_buffer,
333 &verify_buffer_len,
334 ( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT ) ?
335 MBEDTLS_SSL_IS_SERVER :
336 MBEDTLS_SSL_IS_CLIENT );
Jerry Yuda8cdf22021-10-25 15:06:49 +0800337
338 /* Process the message contents */
Jerry Yu0b32c502021-10-28 13:41:59 +0800339 MBEDTLS_SSL_PROC_CHK( ssl_tls13_parse_certificate_verify( ssl, buf,
340 buf + buf_len, verify_buffer, verify_buffer_len ) );
Jerry Yuda8cdf22021-10-25 15:06:49 +0800341
Ronald Cron8f6d39a2022-03-10 18:56:50 +0100342 mbedtls_ssl_add_hs_msg_to_checksum( ssl, MBEDTLS_SSL_HS_CERTIFICATE_VERIFY,
343 buf, buf_len );
Jerry Yu30b071c2021-09-12 20:16:03 +0800344
345cleanup:
346
347 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse certificate verify" ) );
Jerry Yu5398c102021-11-05 13:32:38 +0800348 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_tls13_process_certificate_verify", ret );
Jerry Yu30b071c2021-09-12 20:16:03 +0800349 return( ret );
Jerry Yuda8cdf22021-10-25 15:06:49 +0800350#else
351 ((void) ssl);
352 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
353 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
354#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
Jerry Yu30b071c2021-09-12 20:16:03 +0800355}
356
357/*
Xiaofei Bai947571e2021-09-29 09:12:03 +0000358 *
359 * STATE HANDLING: Incoming Certificate, client-side only currently.
360 *
361 */
362
363/*
Xiaofei Bai947571e2021-09-29 09:12:03 +0000364 * Implementation
365 */
366
Xiaofei Bai947571e2021-09-29 09:12:03 +0000367#if defined(MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED)
368#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
369/*
370 * Structure of Certificate message:
371 *
372 * enum {
373 * X509(0),
374 * RawPublicKey(2),
375 * (255)
376 * } CertificateType;
377 *
378 * struct {
379 * select (certificate_type) {
380 * case RawPublicKey:
381 * * From RFC 7250 ASN.1_subjectPublicKeyInfo *
382 * opaque ASN1_subjectPublicKeyInfo<1..2^24-1>;
383 * case X509:
384 * opaque cert_data<1..2^24-1>;
385 * };
386 * Extension extensions<0..2^16-1>;
387 * } CertificateEntry;
388 *
389 * struct {
390 * opaque certificate_request_context<0..2^8-1>;
391 * CertificateEntry certificate_list<0..2^24-1>;
392 * } Certificate;
393 *
394 */
Xiaofei Bai79595ac2021-10-26 07:16:45 +0000395
396/* Parse certificate chain send by the server. */
Xiaofei Bai947571e2021-09-29 09:12:03 +0000397static int ssl_tls13_parse_certificate( mbedtls_ssl_context *ssl,
398 const unsigned char *buf,
399 const unsigned char *end )
400{
401 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
402 size_t certificate_request_context_len = 0;
403 size_t certificate_list_len = 0;
404 const unsigned char *p = buf;
405 const unsigned char *certificate_list_end;
406
407 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 4 );
408 certificate_request_context_len = p[0];
Jerry Yub640bf62021-10-29 10:05:32 +0800409 certificate_list_len = MBEDTLS_GET_UINT24_BE( p, 1 );
Xiaofei Bai79595ac2021-10-26 07:16:45 +0000410 p += 4;
Xiaofei Bai947571e2021-09-29 09:12:03 +0000411
412 /* In theory, the certificate list can be up to 2^24 Bytes, but we don't
413 * support anything beyond 2^16 = 64K.
414 */
Xiaofei Bai79595ac2021-10-26 07:16:45 +0000415 if( ( certificate_request_context_len != 0 ) ||
416 ( certificate_list_len >= 0x10000 ) )
Xiaofei Bai947571e2021-09-29 09:12:03 +0000417 {
418 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
419 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR,
420 MBEDTLS_ERR_SSL_DECODE_ERROR );
421 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
422 }
423
424 /* In case we tried to reuse a session but it failed */
425 if( ssl->session_negotiate->peer_cert != NULL )
426 {
427 mbedtls_x509_crt_free( ssl->session_negotiate->peer_cert );
428 mbedtls_free( ssl->session_negotiate->peer_cert );
429 }
430
431 if( ( ssl->session_negotiate->peer_cert =
432 mbedtls_calloc( 1, sizeof( mbedtls_x509_crt ) ) ) == NULL )
433 {
434 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc( %" MBEDTLS_PRINTF_SIZET " bytes ) failed",
435 sizeof( mbedtls_x509_crt ) ) );
436 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR,
437 MBEDTLS_ERR_SSL_ALLOC_FAILED );
438 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
439 }
440
441 mbedtls_x509_crt_init( ssl->session_negotiate->peer_cert );
442
Xiaofei Bai947571e2021-09-29 09:12:03 +0000443 certificate_list_end = p + certificate_list_len;
Xiaofei Bai79595ac2021-10-26 07:16:45 +0000444 while( p < certificate_list_end )
Xiaofei Bai947571e2021-09-29 09:12:03 +0000445 {
446 size_t cert_data_len, extensions_len;
447
Xiaofei Bai79595ac2021-10-26 07:16:45 +0000448 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, certificate_list_end, 3 );
Xiaofei Baif93cbd22021-10-29 02:39:30 +0000449 cert_data_len = MBEDTLS_GET_UINT24_BE( p, 0 );
Xiaofei Bai947571e2021-09-29 09:12:03 +0000450 p += 3;
451
452 /* In theory, the CRT can be up to 2^24 Bytes, but we don't support
453 * anything beyond 2^16 = 64K. Otherwise as in the TLS 1.2 code,
454 * check that we have a minimum of 128 bytes of data, this is not
455 * clear why we need that though.
456 */
457 if( ( cert_data_len < 128 ) || ( cert_data_len >= 0x10000 ) )
Xiaofei Bai79595ac2021-10-26 07:16:45 +0000458 {
Xiaofei Bai947571e2021-09-29 09:12:03 +0000459 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad Certificate message" ) );
460 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR,
461 MBEDTLS_ERR_SSL_DECODE_ERROR );
462 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
463 }
464
Xiaofei Bai79595ac2021-10-26 07:16:45 +0000465 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, certificate_list_end, cert_data_len );
Xiaofei Bai947571e2021-09-29 09:12:03 +0000466 ret = mbedtls_x509_crt_parse_der( ssl->session_negotiate->peer_cert,
467 p, cert_data_len );
468
469 switch( ret )
470 {
471 case 0: /*ok*/
472 break;
473 case MBEDTLS_ERR_X509_UNKNOWN_SIG_ALG + MBEDTLS_ERR_OID_NOT_FOUND:
474 /* Ignore certificate with an unknown algorithm: maybe a
475 prior certificate was already trusted. */
476 break;
477
478 case MBEDTLS_ERR_X509_ALLOC_FAILED:
479 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR,
480 MBEDTLS_ERR_X509_ALLOC_FAILED );
481 MBEDTLS_SSL_DEBUG_RET( 1, " mbedtls_x509_crt_parse_der", ret );
482 return( ret );
483
484 case MBEDTLS_ERR_X509_UNKNOWN_VERSION:
485 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT,
486 MBEDTLS_ERR_X509_UNKNOWN_VERSION );
487 MBEDTLS_SSL_DEBUG_RET( 1, " mbedtls_x509_crt_parse_der", ret );
488 return( ret );
489
490 default:
491 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_BAD_CERT,
492 ret );
493 MBEDTLS_SSL_DEBUG_RET( 1, " mbedtls_x509_crt_parse_der", ret );
494 return( ret );
495 }
496
497 p += cert_data_len;
498
499 /* Certificate extensions length */
500 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, certificate_list_end, 2 );
501 extensions_len = MBEDTLS_GET_UINT16_BE( p, 0 );
502 p += 2;
Xiaofei Bai79595ac2021-10-26 07:16:45 +0000503 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, certificate_list_end, extensions_len );
Xiaofei Bai947571e2021-09-29 09:12:03 +0000504 p += extensions_len;
505 }
506
507 /* Check that all the message is consumed. */
508 if( p != end )
509 {
510 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad Certificate message" ) );
511 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR, \
512 MBEDTLS_ERR_SSL_DECODE_ERROR );
513 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
514 }
515
516 MBEDTLS_SSL_DEBUG_CRT( 3, "peer certificate", ssl->session_negotiate->peer_cert );
517
518 return( ret );
519}
520#else
521static int ssl_tls13_parse_certificate( mbedtls_ssl_context *ssl,
522 const unsigned char *buf,
523 const unsigned char *end )
524{
525 ((void) ssl);
526 ((void) buf);
527 ((void) end);
528 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
529}
530#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
531#endif /* MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED */
532
533#if defined(MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED)
534#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Xiaofei Bai79595ac2021-10-26 07:16:45 +0000535/* Validate certificate chain sent by the server. */
Xiaofei Bai947571e2021-09-29 09:12:03 +0000536static int ssl_tls13_validate_certificate( mbedtls_ssl_context *ssl )
537{
538 int ret = 0;
539 mbedtls_x509_crt *ca_chain;
540 mbedtls_x509_crl *ca_crl;
Xiaofei Baiff456022021-10-28 06:50:17 +0000541 uint32_t verify_result = 0;
Xiaofei Bai947571e2021-09-29 09:12:03 +0000542
543#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
544 if( ssl->handshake->sni_ca_chain != NULL )
545 {
546 ca_chain = ssl->handshake->sni_ca_chain;
547 ca_crl = ssl->handshake->sni_ca_crl;
548 }
549 else
550#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
551 {
552 ca_chain = ssl->conf->ca_chain;
553 ca_crl = ssl->conf->ca_crl;
554 }
555
556 /*
557 * Main check: verify certificate
558 */
559 ret = mbedtls_x509_crt_verify_with_profile(
560 ssl->session_negotiate->peer_cert,
561 ca_chain, ca_crl,
562 ssl->conf->cert_profile,
563 ssl->hostname,
Xiaofei Baiff456022021-10-28 06:50:17 +0000564 &verify_result,
Xiaofei Bai947571e2021-09-29 09:12:03 +0000565 ssl->conf->f_vrfy, ssl->conf->p_vrfy );
566
567 if( ret != 0 )
568 {
569 MBEDTLS_SSL_DEBUG_RET( 1, "x509_verify_cert", ret );
570 }
571
572 /*
573 * Secondary checks: always done, but change 'ret' only if it was 0
574 */
Xiaofei Bai947571e2021-09-29 09:12:03 +0000575 if( mbedtls_ssl_check_cert_usage( ssl->session_negotiate->peer_cert,
576 ssl->handshake->ciphersuite_info,
577 !ssl->conf->endpoint,
Xiaofei Baiff456022021-10-28 06:50:17 +0000578 &verify_result ) != 0 )
Xiaofei Bai947571e2021-09-29 09:12:03 +0000579 {
580 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate ( usage extensions )" ) );
581 if( ret == 0 )
582 ret = MBEDTLS_ERR_SSL_BAD_CERTIFICATE;
583 }
584
585
586 if( ca_chain == NULL )
587 {
588 MBEDTLS_SSL_DEBUG_MSG( 1, ( "got no CA chain" ) );
589 ret = MBEDTLS_ERR_SSL_CA_CHAIN_REQUIRED;
590 }
591
592 if( ret != 0 )
593 {
594 /* The certificate may have been rejected for several reasons.
595 Pick one and send the corresponding alert. Which alert to send
596 may be a subject of debate in some cases. */
Xiaofei Baiff456022021-10-28 06:50:17 +0000597 if( verify_result & MBEDTLS_X509_BADCERT_OTHER )
Xiaofei Bai947571e2021-09-29 09:12:03 +0000598 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED, ret );
Xiaofei Baiff456022021-10-28 06:50:17 +0000599 else if( verify_result & MBEDTLS_X509_BADCERT_CN_MISMATCH )
Xiaofei Bai947571e2021-09-29 09:12:03 +0000600 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_BAD_CERT, ret );
Xiaofei Baif93cbd22021-10-29 02:39:30 +0000601 else if( verify_result & ( MBEDTLS_X509_BADCERT_KEY_USAGE |
602 MBEDTLS_X509_BADCERT_EXT_KEY_USAGE |
603 MBEDTLS_X509_BADCERT_NS_CERT_TYPE |
604 MBEDTLS_X509_BADCERT_BAD_PK |
605 MBEDTLS_X509_BADCERT_BAD_KEY ) )
Xiaofei Bai947571e2021-09-29 09:12:03 +0000606 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT, ret );
Xiaofei Baiff456022021-10-28 06:50:17 +0000607 else if( verify_result & MBEDTLS_X509_BADCERT_EXPIRED )
Xiaofei Bai947571e2021-09-29 09:12:03 +0000608 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_CERT_EXPIRED, ret );
Xiaofei Baiff456022021-10-28 06:50:17 +0000609 else if( verify_result & MBEDTLS_X509_BADCERT_REVOKED )
Xiaofei Bai947571e2021-09-29 09:12:03 +0000610 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_CERT_REVOKED, ret );
Xiaofei Baiff456022021-10-28 06:50:17 +0000611 else if( verify_result & MBEDTLS_X509_BADCERT_NOT_TRUSTED )
Xiaofei Bai947571e2021-09-29 09:12:03 +0000612 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_UNKNOWN_CA, ret );
613 else
614 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_CERT_UNKNOWN, ret );
615 }
616
617#if defined(MBEDTLS_DEBUG_C)
Xiaofei Baiff456022021-10-28 06:50:17 +0000618 if( verify_result != 0 )
Xiaofei Bai947571e2021-09-29 09:12:03 +0000619 {
620 MBEDTLS_SSL_DEBUG_MSG( 3, ( "! Certificate verification flags %08x",
Jerry Yu83bb1312021-10-28 22:16:33 +0800621 (unsigned int) verify_result ) );
Xiaofei Bai947571e2021-09-29 09:12:03 +0000622 }
623 else
624 {
625 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Certificate verification flags clear" ) );
626 }
627#endif /* MBEDTLS_DEBUG_C */
628
Xiaofei Baiff456022021-10-28 06:50:17 +0000629 ssl->session_negotiate->verify_result = verify_result;
Xiaofei Bai947571e2021-09-29 09:12:03 +0000630 return( ret );
631}
632#else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
633static int ssl_tls13_validate_certificate( mbedtls_ssl_context *ssl )
634{
635 ((void) ssl);
636 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
637}
638#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
639#endif /* MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED */
640
Xiaofei Bai79595ac2021-10-26 07:16:45 +0000641int mbedtls_ssl_tls13_process_certificate( mbedtls_ssl_context *ssl )
Xiaofei Bai947571e2021-09-29 09:12:03 +0000642{
Xiaofei Bai79595ac2021-10-26 07:16:45 +0000643 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
644 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) );
645
646#if defined(MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED)
647 unsigned char *buf;
648 size_t buf_len;
649
Xiaofei Bai746f9482021-11-12 08:53:56 +0000650 MBEDTLS_SSL_PROC_CHK( mbedtls_ssl_tls13_fetch_handshake_msg(
Xiaofei Bai79595ac2021-10-26 07:16:45 +0000651 ssl, MBEDTLS_SSL_HS_CERTIFICATE,
652 &buf, &buf_len ) );
653
654 /* Parse the certificate chain sent by the peer. */
655 MBEDTLS_SSL_PROC_CHK( ssl_tls13_parse_certificate( ssl, buf, buf + buf_len ) );
656 /* Validate the certificate chain and set the verification results. */
657 MBEDTLS_SSL_PROC_CHK( ssl_tls13_validate_certificate( ssl ) );
658
Ronald Cron8f6d39a2022-03-10 18:56:50 +0100659 mbedtls_ssl_add_hs_msg_to_checksum( ssl, MBEDTLS_SSL_HS_CERTIFICATE,
660 buf, buf_len );
Xiaofei Bai79595ac2021-10-26 07:16:45 +0000661
Xiaofei Bai79595ac2021-10-26 07:16:45 +0000662cleanup:
663
664 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse certificate" ) );
Xiaofei Bai10aeec02021-10-26 09:50:08 +0000665#else
666 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
667 ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
668#endif /* MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED */
Xiaofei Bai79595ac2021-10-26 07:16:45 +0000669 return( ret );
Xiaofei Bai947571e2021-09-29 09:12:03 +0000670}
Jerry Yu90f152d2022-01-29 22:12:42 +0800671#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Jerry Yu7399d0d2022-01-30 17:54:19 +0800672/*
673 * enum {
674 * X509(0),
675 * RawPublicKey(2),
676 * (255)
677 * } CertificateType;
678 *
679 * struct {
680 * select (certificate_type) {
681 * case RawPublicKey:
682 * // From RFC 7250 ASN.1_subjectPublicKeyInfo
683 * opaque ASN1_subjectPublicKeyInfo<1..2^24-1>;
684 *
685 * case X509:
686 * opaque cert_data<1..2^24-1>;
687 * };
688 * Extension extensions<0..2^16-1>;
689 * } CertificateEntry;
690 *
691 * struct {
692 * opaque certificate_request_context<0..2^8-1>;
693 * CertificateEntry certificate_list<0..2^24-1>;
694 * } Certificate;
695 */
696static int ssl_tls13_write_certificate_body( mbedtls_ssl_context *ssl,
Jerry Yu3e536442022-02-15 11:05:59 +0800697 unsigned char *buf,
Jerry Yu7399d0d2022-01-30 17:54:19 +0800698 unsigned char *end,
Jerry Yu3e536442022-02-15 11:05:59 +0800699 size_t *out_len )
Jerry Yu5cc35062022-01-28 16:16:08 +0800700{
Jerry Yu5cc35062022-01-28 16:16:08 +0800701 const mbedtls_x509_crt *crt = mbedtls_ssl_own_cert( ssl );
Jerry Yu3e536442022-02-15 11:05:59 +0800702 unsigned char *p = buf;
Jerry Yuc8d8d4e2022-02-18 12:10:03 +0800703 unsigned char *certificate_request_context =
704 ssl->handshake->certificate_request_context;
705 unsigned char certificate_request_context_len =
706 ssl->handshake->certificate_request_context_len;
707 unsigned char *p_certificate_list_len;
Jerry Yu5cc35062022-01-28 16:16:08 +0800708
Jerry Yu5cc35062022-01-28 16:16:08 +0800709
Jerry Yu3391ac02022-02-16 11:21:37 +0800710 /* ...
711 * opaque certificate_request_context<0..2^8-1>;
712 * ...
713 */
Jerry Yuc8d8d4e2022-02-18 12:10:03 +0800714 MBEDTLS_SSL_CHK_BUF_PTR( p, end, certificate_request_context_len + 1 );
715 *p++ = certificate_request_context_len;
716 if( certificate_request_context_len > 0 )
Jerry Yu537530d2022-02-15 14:00:57 +0800717 {
Jerry Yuc8d8d4e2022-02-18 12:10:03 +0800718 memcpy( p, certificate_request_context, certificate_request_context_len );
719 p += certificate_request_context_len;
Jerry Yu537530d2022-02-15 14:00:57 +0800720 }
721
Jerry Yu3391ac02022-02-16 11:21:37 +0800722 /* ...
723 * CertificateEntry certificate_list<0..2^24-1>;
724 * ...
725 */
Jerry Yu3e536442022-02-15 11:05:59 +0800726 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 3 );
Jerry Yuc8d8d4e2022-02-18 12:10:03 +0800727 p_certificate_list_len = p;
Jerry Yu3e536442022-02-15 11:05:59 +0800728 p += 3;
729
Jerry Yu7399d0d2022-01-30 17:54:19 +0800730 MBEDTLS_SSL_DEBUG_CRT( 3, "own certificate", crt );
Jerry Yu5cc35062022-01-28 16:16:08 +0800731
Jerry Yuc8d8d4e2022-02-18 12:10:03 +0800732 while( crt != NULL )
Jerry Yu5cc35062022-01-28 16:16:08 +0800733 {
Jerry Yu7399d0d2022-01-30 17:54:19 +0800734 size_t cert_data_len = crt->raw.len;
Jerry Yu5cc35062022-01-28 16:16:08 +0800735
Jerry Yu7399d0d2022-01-30 17:54:19 +0800736 MBEDTLS_SSL_CHK_BUF_PTR( p, end, cert_data_len + 3 + 2 );
737 MBEDTLS_PUT_UINT24_BE( cert_data_len, p, 0 );
738 p += 3;
Jerry Yu5cc35062022-01-28 16:16:08 +0800739
Jerry Yu7399d0d2022-01-30 17:54:19 +0800740 memcpy( p, crt->raw.p, cert_data_len );
741 p += cert_data_len;
742 crt = crt->next;
Jerry Yu5cc35062022-01-28 16:16:08 +0800743
744 /* Currently, we don't have any certificate extensions defined.
745 * Hence, we are sending an empty extension with length zero.
746 */
Jerry Yu7399d0d2022-01-30 17:54:19 +0800747 MBEDTLS_PUT_UINT24_BE( 0, p, 0 );
748 p += 2;
Jerry Yu5cc35062022-01-28 16:16:08 +0800749 }
Jerry Yu5cc35062022-01-28 16:16:08 +0800750
Jerry Yuc8d8d4e2022-02-18 12:10:03 +0800751 MBEDTLS_PUT_UINT24_BE( p - p_certificate_list_len - 3,
752 p_certificate_list_len, 0 );
Jerry Yu7399d0d2022-01-30 17:54:19 +0800753
Jerry Yu3e536442022-02-15 11:05:59 +0800754 *out_len = p - buf;
Jerry Yu5cc35062022-01-28 16:16:08 +0800755
756 return( 0 );
757}
Jerry Yu5cc35062022-01-28 16:16:08 +0800758
Jerry Yu3e536442022-02-15 11:05:59 +0800759int mbedtls_ssl_tls13_write_certificate( mbedtls_ssl_context *ssl )
Jerry Yu5cc35062022-01-28 16:16:08 +0800760{
761 int ret;
Ronald Cron5bb8fc82022-03-09 07:00:13 +0100762 unsigned char *buf;
763 size_t buf_len, msg_len;
764
Jerry Yu5cc35062022-01-28 16:16:08 +0800765 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write certificate" ) );
766
Ronald Cron8f6d39a2022-03-10 18:56:50 +0100767 MBEDTLS_SSL_PROC_CHK( mbedtls_ssl_start_handshake_msg( ssl,
Ronald Cron5bb8fc82022-03-09 07:00:13 +0100768 MBEDTLS_SSL_HS_CERTIFICATE, &buf, &buf_len ) );
Jerry Yu5cc35062022-01-28 16:16:08 +0800769
Ronald Cron5bb8fc82022-03-09 07:00:13 +0100770 MBEDTLS_SSL_PROC_CHK( ssl_tls13_write_certificate_body( ssl,
771 buf,
772 buf + buf_len,
773 &msg_len ) );
Jerry Yu5cc35062022-01-28 16:16:08 +0800774
Ronald Cron8f6d39a2022-03-10 18:56:50 +0100775 mbedtls_ssl_add_hs_msg_to_checksum( ssl, MBEDTLS_SSL_HS_CERTIFICATE,
776 buf, msg_len );
Jerry Yu5cc35062022-01-28 16:16:08 +0800777
Ronald Cron8f6d39a2022-03-10 18:56:50 +0100778 MBEDTLS_SSL_PROC_CHK( mbedtls_ssl_finish_handshake_msg(
Ronald Cron5bb8fc82022-03-09 07:00:13 +0100779 ssl, buf_len, msg_len ) );
Jerry Yu5cc35062022-01-28 16:16:08 +0800780cleanup:
781
782 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write certificate" ) );
783 return( ret );
784}
785
Jerry Yu3e536442022-02-15 11:05:59 +0800786/*
787 * STATE HANDLING: Output Certificate Verify
788 */
Jerry Yue91a51a2022-03-22 21:42:50 +0800789static int ssl_tls13_get_sig_alg_from_pk( mbedtls_ssl_context *ssl,
790 mbedtls_pk_context *own_key,
Jerry Yu8c338862022-03-23 13:34:04 +0800791 uint16_t *algorithm )
Jerry Yu67eced02022-02-25 13:37:36 +0800792{
793 mbedtls_pk_type_t sig = mbedtls_ssl_sig_from_pk( own_key );
794 /* Determine the size of the key */
795 size_t own_key_size = mbedtls_pk_get_bitlen( own_key );
Jerry Yue91a51a2022-03-22 21:42:50 +0800796 *algorithm = MBEDTLS_TLS1_3_SIG_NONE;
Jerry Yu67eced02022-02-25 13:37:36 +0800797 ((void) own_key_size);
798
799 switch( sig )
800 {
801#if defined(MBEDTLS_ECDSA_C)
802 case MBEDTLS_SSL_SIG_ECDSA:
803 switch( own_key_size )
804 {
805 case 256:
Jerry Yue91a51a2022-03-22 21:42:50 +0800806 *algorithm = MBEDTLS_TLS1_3_SIG_ECDSA_SECP256R1_SHA256;
Jerry Yue91a51a2022-03-22 21:42:50 +0800807 return( 0 );
Jerry Yu67eced02022-02-25 13:37:36 +0800808 case 384:
Jerry Yue91a51a2022-03-22 21:42:50 +0800809 *algorithm = MBEDTLS_TLS1_3_SIG_ECDSA_SECP384R1_SHA384;
Jerry Yue91a51a2022-03-22 21:42:50 +0800810 return( 0 );
Jerry Yu67eced02022-02-25 13:37:36 +0800811 case 521:
Jerry Yue91a51a2022-03-22 21:42:50 +0800812 *algorithm = MBEDTLS_TLS1_3_SIG_ECDSA_SECP521R1_SHA512;
Jerry Yue91a51a2022-03-22 21:42:50 +0800813 return( 0 );
Jerry Yu67eced02022-02-25 13:37:36 +0800814 default:
815 MBEDTLS_SSL_DEBUG_MSG( 3,
816 ( "unknown key size: %"
817 MBEDTLS_PRINTF_SIZET " bits",
818 own_key_size ) );
819 break;
820 }
821 break;
822#endif /* MBEDTLS_ECDSA_C */
823
Jerry Yucef3f332022-03-22 23:00:13 +0800824#if defined(MBEDTLS_RSA_C)
Jerry Yu67eced02022-02-25 13:37:36 +0800825 case MBEDTLS_SSL_SIG_RSA:
Jerry Yucef3f332022-03-22 23:00:13 +0800826#if defined(MBEDTLS_PKCS1_V21)
827#if defined(MBEDTLS_SHA256_C)
Jerry Yu67eced02022-02-25 13:37:36 +0800828 if( own_key_size <= 2048 &&
829 mbedtls_ssl_sig_alg_is_received( ssl,
830 MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA256 ) )
831 {
Jerry Yue91a51a2022-03-22 21:42:50 +0800832 *algorithm = MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA256;
Jerry Yue91a51a2022-03-22 21:42:50 +0800833 return( 0 );
Jerry Yu67eced02022-02-25 13:37:36 +0800834 }
Jerry Yucef3f332022-03-22 23:00:13 +0800835 else
836#endif /* MBEDTLS_SHA256_C */
837#if defined(MBEDTLS_SHA384_C)
838 if( own_key_size <= 3072 &&
839 mbedtls_ssl_sig_alg_is_received( ssl,
Jerry Yu67eced02022-02-25 13:37:36 +0800840 MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA384 ) )
841 {
Jerry Yue91a51a2022-03-22 21:42:50 +0800842 *algorithm = MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA384;
Jerry Yue91a51a2022-03-22 21:42:50 +0800843 return( 0 );
Jerry Yu67eced02022-02-25 13:37:36 +0800844 }
Jerry Yucef3f332022-03-22 23:00:13 +0800845 else
846#endif /* MBEDTLS_SHA384_C */
847#if defined(MBEDTLS_SHA512_C)
848 if( own_key_size <= 4096 &&
849 mbedtls_ssl_sig_alg_is_received( ssl,
Jerry Yu67eced02022-02-25 13:37:36 +0800850 MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA512 ) )
851 {
Jerry Yue91a51a2022-03-22 21:42:50 +0800852 *algorithm = MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA512;
Jerry Yue91a51a2022-03-22 21:42:50 +0800853 return( 0 );
Jerry Yu67eced02022-02-25 13:37:36 +0800854 }
Jerry Yucef3f332022-03-22 23:00:13 +0800855 else
856#endif /* MBEDTLS_SHA512_C */
857#endif /* MBEDTLS_PKCS1_V21 */
858#if defined(MBEDTLS_PKCS1_V15)
859#if defined(MBEDTLS_SHA256_C)
860 if( own_key_size <= 2048 &&
861 mbedtls_ssl_sig_alg_is_received( ssl,
862 MBEDTLS_TLS1_3_SIG_RSA_PKCS1_SHA256 ) )
863 {
864 *algorithm = MBEDTLS_TLS1_3_SIG_RSA_PKCS1_SHA256;
Jerry Yucef3f332022-03-22 23:00:13 +0800865 return( 0 );
866 }
867 else
868#endif /* MBEDTLS_SHA256_C */
869#if defined(MBEDTLS_SHA384_C)
870 if( own_key_size <= 3072 &&
871 mbedtls_ssl_sig_alg_is_received( ssl,
872 MBEDTLS_TLS1_3_SIG_RSA_PKCS1_SHA384 ) )
873 {
874 *algorithm = MBEDTLS_TLS1_3_SIG_RSA_PKCS1_SHA384;
Jerry Yucef3f332022-03-22 23:00:13 +0800875 return( 0 );
876 }
877 else
878#endif /* MBEDTLS_SHA384_C */
879#if defined(MBEDTLS_SHA512_C)
880 if( own_key_size <= 4096 &&
881 mbedtls_ssl_sig_alg_is_received( ssl,
882 MBEDTLS_TLS1_3_SIG_RSA_PKCS1_SHA512 ) )
883 {
884 *algorithm = MBEDTLS_TLS1_3_SIG_RSA_PKCS1_SHA512;
Jerry Yucef3f332022-03-22 23:00:13 +0800885 return( 0 );
886 }
887 else
888#endif /* MBEDTLS_SHA512_C */
889#endif /* MBEDTLS_PKCS1_V15 */
890 {
891 MBEDTLS_SSL_DEBUG_MSG( 3,
892 ( "unknown key size: %"
893 MBEDTLS_PRINTF_SIZET " bits",
894 own_key_size ) );
895 }
Jerry Yu67eced02022-02-25 13:37:36 +0800896 break;
Jerry Yucef3f332022-03-22 23:00:13 +0800897#endif /* MBEDTLS_RSA_C */
Jerry Yu67eced02022-02-25 13:37:36 +0800898 default:
899 MBEDTLS_SSL_DEBUG_MSG( 1,
Andrzej Kurek5c65c572022-04-13 14:28:52 -0400900 ( "unknown signature type : %u", sig ) );
Jerry Yu67eced02022-02-25 13:37:36 +0800901 break;
902 }
Jerry Yue91a51a2022-03-22 21:42:50 +0800903 return( -1 );
Jerry Yu67eced02022-02-25 13:37:36 +0800904}
905
Jerry Yu3e536442022-02-15 11:05:59 +0800906static int ssl_tls13_write_certificate_verify_body( mbedtls_ssl_context *ssl,
907 unsigned char *buf,
908 unsigned char *end,
909 size_t *out_len )
Jerry Yu8511f122022-01-29 10:01:04 +0800910{
911 int ret;
Jerry Yu3e536442022-02-15 11:05:59 +0800912 unsigned char *p = buf;
Jerry Yu8511f122022-01-29 10:01:04 +0800913 mbedtls_pk_context *own_key;
Jerry Yu3e536442022-02-15 11:05:59 +0800914
Jerry Yu8511f122022-01-29 10:01:04 +0800915 unsigned char handshake_hash[ MBEDTLS_TLS1_3_MD_MAX_SIZE ];
916 size_t handshake_hash_len;
Jerry Yu3e536442022-02-15 11:05:59 +0800917 unsigned char verify_buffer[ SSL_VERIFY_STRUCT_MAX_SIZE ];
918 size_t verify_buffer_len;
Jerry Yu67eced02022-02-25 13:37:36 +0800919 mbedtls_pk_type_t pk_type = MBEDTLS_PK_NONE;
Jerry Yu67eced02022-02-25 13:37:36 +0800920 mbedtls_md_type_t md_alg = MBEDTLS_MD_NONE;
pespacek34935872022-05-20 15:43:32 +0200921 psa_algorithm_t psa_algorithm = PSA_ALG_NONE;
Jerry Yu78272072022-02-22 10:28:13 +0800922 uint16_t algorithm = MBEDTLS_TLS1_3_SIG_NONE;
Jerry Yu3e536442022-02-15 11:05:59 +0800923 size_t signature_len = 0;
Jerry Yu3391ac02022-02-16 11:21:37 +0800924 unsigned char verify_hash[ MBEDTLS_MD_MAX_SIZE ];
Jerry Yu3e536442022-02-15 11:05:59 +0800925 size_t verify_hash_len;
Jerry Yu8511f122022-01-29 10:01:04 +0800926
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
pespacek34935872022-05-20 15:43:32 +0200988 if( psa_hash_compute( psa_algorithm,
989 verify_buffer,
990 verify_buffer_len,
991 verify_hash,sizeof( verify_hash ),
992 &verify_hash_len ) != PSA_SUCCESS )
Jerry Yu8511f122022-01-29 10:01:04 +0800993 return( ret );
994
Jerry Yu8511f122022-01-29 10:01:04 +0800995 MBEDTLS_SSL_DEBUG_BUF( 3, "verify hash", verify_hash, verify_hash_len );
996
Jerry Yu8beb9e12022-03-12 16:23:53 +0800997 if( ( ret = mbedtls_pk_sign_ext( pk_type, own_key,
998 md_alg, verify_hash, verify_hash_len,
Jerry Yu67eced02022-02-25 13:37:36 +0800999 p + 2, (size_t)( end - ( p + 2 ) ), &signature_len,
1000 ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 )
Jerry Yu8511f122022-01-29 10:01:04 +08001001 {
1002 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_pk_sign", ret );
1003 return( ret );
1004 }
Jerry Yu8511f122022-01-29 10:01:04 +08001005
Jerry Yu3e536442022-02-15 11:05:59 +08001006 MBEDTLS_PUT_UINT16_BE( signature_len, p, 0 );
1007 p += 2 + signature_len;
Jerry Yu8511f122022-01-29 10:01:04 +08001008
Jerry Yu3e536442022-02-15 11:05:59 +08001009 *out_len = (size_t)( p - buf );
1010
Jerry Yu8511f122022-01-29 10:01:04 +08001011 return( ret );
1012}
Jerry Yu8511f122022-01-29 10:01:04 +08001013
Jerry Yu3e536442022-02-15 11:05:59 +08001014int mbedtls_ssl_tls13_write_certificate_verify( mbedtls_ssl_context *ssl )
Jerry Yu8511f122022-01-29 10:01:04 +08001015{
1016 int ret = 0;
Jerry Yuca133a32022-02-15 14:22:05 +08001017 unsigned char *buf;
1018 size_t buf_len, msg_len;
1019
Jerry Yu8511f122022-01-29 10:01:04 +08001020 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write certificate verify" ) );
1021
Ronald Cron8f6d39a2022-03-10 18:56:50 +01001022 MBEDTLS_SSL_PROC_CHK( mbedtls_ssl_start_handshake_msg( ssl,
Jerry Yuca133a32022-02-15 14:22:05 +08001023 MBEDTLS_SSL_HS_CERTIFICATE_VERIFY, &buf, &buf_len ) );
Jerry Yu8511f122022-01-29 10:01:04 +08001024
Jerry Yuca133a32022-02-15 14:22:05 +08001025 MBEDTLS_SSL_PROC_CHK( ssl_tls13_write_certificate_verify_body(
1026 ssl, buf, buf + buf_len, &msg_len ) );
Jerry Yu8511f122022-01-29 10:01:04 +08001027
Ronald Cron8f6d39a2022-03-10 18:56:50 +01001028 mbedtls_ssl_add_hs_msg_to_checksum( ssl, MBEDTLS_SSL_HS_CERTIFICATE_VERIFY,
1029 buf, msg_len );
Jerry Yu8511f122022-01-29 10:01:04 +08001030
Ronald Cron8f6d39a2022-03-10 18:56:50 +01001031 MBEDTLS_SSL_PROC_CHK( mbedtls_ssl_finish_handshake_msg(
Jerry Yuca133a32022-02-15 14:22:05 +08001032 ssl, buf_len, msg_len ) );
Jerry Yu8511f122022-01-29 10:01:04 +08001033
1034cleanup:
1035
1036 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write certificate verify" ) );
1037 return( ret );
1038}
1039
Jerry Yu90f152d2022-01-29 22:12:42 +08001040#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
1041
Jerry Yu5cc35062022-01-28 16:16:08 +08001042/*
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001043 *
XiaokangQianc5c39d52021-11-09 11:55:10 +00001044 * STATE HANDLING: Incoming Finished message.
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001045 */
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001046/*
1047 * Implementation
1048 */
1049
XiaokangQianaaa0e192021-11-10 03:07:04 +00001050static int ssl_tls13_preprocess_finished_message( mbedtls_ssl_context *ssl )
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001051{
1052 int ret;
1053
XiaokangQianc5c39d52021-11-09 11:55:10 +00001054 ret = mbedtls_ssl_tls13_calculate_verify_data( ssl,
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001055 ssl->handshake->state_local.finished_in.digest,
1056 sizeof( ssl->handshake->state_local.finished_in.digest ),
1057 &ssl->handshake->state_local.finished_in.digest_len,
XiaokangQianc5c39d52021-11-09 11:55:10 +00001058 ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT ?
XiaokangQianc13f9352021-11-11 06:13:22 +00001059 MBEDTLS_SSL_IS_SERVER : MBEDTLS_SSL_IS_CLIENT );
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001060 if( ret != 0 )
1061 {
XiaokangQianc5c39d52021-11-09 11:55:10 +00001062 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_tls13_calculate_verify_data", ret );
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001063 return( ret );
1064 }
1065
1066 return( 0 );
1067}
1068
XiaokangQianc5c39d52021-11-09 11:55:10 +00001069static int ssl_tls13_parse_finished_message( mbedtls_ssl_context *ssl,
1070 const unsigned char *buf,
1071 const unsigned char *end )
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001072{
XiaokangQian33062842021-11-11 03:37:45 +00001073 /*
1074 * struct {
XiaokangQianc13f9352021-11-11 06:13:22 +00001075 * opaque verify_data[Hash.length];
XiaokangQian33062842021-11-11 03:37:45 +00001076 * } Finished;
1077 */
1078 const unsigned char *expected_verify_data =
1079 ssl->handshake->state_local.finished_in.digest;
1080 size_t expected_verify_data_len =
1081 ssl->handshake->state_local.finished_in.digest_len;
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001082 /* Structural validation */
XiaokangQian33062842021-11-11 03:37:45 +00001083 if( (size_t)( end - buf ) != expected_verify_data_len )
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001084 {
1085 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
1086
1087 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR,
XiaokangQianc13f9352021-11-11 06:13:22 +00001088 MBEDTLS_ERR_SSL_DECODE_ERROR );
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001089 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
1090 }
1091
XiaokangQianc5c39d52021-11-09 11:55:10 +00001092 MBEDTLS_SSL_DEBUG_BUF( 4, "verify_data (self-computed):",
XiaokangQian33062842021-11-11 03:37:45 +00001093 expected_verify_data,
1094 expected_verify_data_len );
XiaokangQianc5c39d52021-11-09 11:55:10 +00001095 MBEDTLS_SSL_DEBUG_BUF( 4, "verify_data (received message):", buf,
XiaokangQian33062842021-11-11 03:37:45 +00001096 expected_verify_data_len );
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001097
1098 /* Semantic validation */
Gabor Mezei685472b2021-11-24 11:17:36 +01001099 if( mbedtls_ct_memcmp( buf,
1100 expected_verify_data,
1101 expected_verify_data_len ) != 0 )
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001102 {
1103 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
1104
XiaokangQian33062842021-11-11 03:37:45 +00001105 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_DECRYPT_ERROR,
XiaokangQianc13f9352021-11-11 06:13:22 +00001106 MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001107 return( MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
1108 }
1109 return( 0 );
1110}
1111
XiaokangQianc13f9352021-11-11 06:13:22 +00001112#if defined(MBEDTLS_SSL_CLI_C)
XiaokangQianaaa0e192021-11-10 03:07:04 +00001113static int ssl_tls13_postprocess_server_finished_message( mbedtls_ssl_context *ssl )
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001114{
XiaokangQian33062842021-11-11 03:37:45 +00001115 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001116 mbedtls_ssl_key_set traffic_keys;
XiaokangQian1aef02e2021-10-28 09:54:34 +00001117 mbedtls_ssl_transform *transform_application = NULL;
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001118
XiaokangQian4cab0242021-10-12 08:43:37 +00001119 ret = mbedtls_ssl_tls13_key_schedule_stage_application( ssl );
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001120 if( ret != 0 )
1121 {
1122 MBEDTLS_SSL_DEBUG_RET( 1,
XiaokangQian4cab0242021-10-12 08:43:37 +00001123 "mbedtls_ssl_tls13_key_schedule_stage_application", ret );
XiaokangQian61bdbbc2021-10-28 08:03:38 +00001124 goto cleanup;
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001125 }
1126
XiaokangQian33062842021-11-11 03:37:45 +00001127 ret = mbedtls_ssl_tls13_generate_application_keys( ssl, &traffic_keys );
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001128 if( ret != 0 )
1129 {
1130 MBEDTLS_SSL_DEBUG_RET( 1,
XiaokangQiand0aa3e92021-11-10 06:17:40 +00001131 "mbedtls_ssl_tls13_generate_application_keys", ret );
XiaokangQian61bdbbc2021-10-28 08:03:38 +00001132 goto cleanup;
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001133 }
1134
1135 transform_application =
1136 mbedtls_calloc( 1, sizeof( mbedtls_ssl_transform ) );
1137 if( transform_application == NULL )
XiaokangQian61bdbbc2021-10-28 08:03:38 +00001138 {
1139 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
1140 goto cleanup;
1141 }
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001142
1143 ret = mbedtls_ssl_tls13_populate_transform(
1144 transform_application,
1145 ssl->conf->endpoint,
1146 ssl->session_negotiate->ciphersuite,
1147 &traffic_keys,
1148 ssl );
1149 if( ret != 0 )
1150 {
1151 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_tls13_populate_transform", ret );
XiaokangQian61bdbbc2021-10-28 08:03:38 +00001152 goto cleanup;
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001153 }
1154
1155 ssl->transform_application = transform_application;
1156
XiaokangQian61bdbbc2021-10-28 08:03:38 +00001157cleanup:
1158
XiaokangQian33062842021-11-11 03:37:45 +00001159 mbedtls_platform_zeroize( &traffic_keys, sizeof( traffic_keys ) );
XiaokangQianc5c39d52021-11-09 11:55:10 +00001160 if( ret != 0 )
XiaokangQian1aef02e2021-10-28 09:54:34 +00001161 {
1162 mbedtls_free( transform_application );
1163 MBEDTLS_SSL_PEND_FATAL_ALERT(
1164 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE,
1165 MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
1166 }
XiaokangQian61bdbbc2021-10-28 08:03:38 +00001167 return( ret );
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001168}
XiaokangQianc13f9352021-11-11 06:13:22 +00001169#endif /* MBEDTLS_SSL_CLI_C */
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001170
XiaokangQiancc90c942021-11-09 12:30:09 +00001171static int ssl_tls13_postprocess_finished_message( mbedtls_ssl_context *ssl )
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001172{
1173
XiaokangQianc13f9352021-11-11 06:13:22 +00001174#if defined(MBEDTLS_SSL_CLI_C)
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001175 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
1176 {
XiaokangQianaaa0e192021-11-10 03:07:04 +00001177 return( ssl_tls13_postprocess_server_finished_message( ssl ) );
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001178 }
XiaokangQianc13f9352021-11-11 06:13:22 +00001179#else
1180 ((void) ssl);
1181#endif /* MBEDTLS_SSL_CLI_C */
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001182
1183 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
1184}
1185
XiaokangQianc5c39d52021-11-09 11:55:10 +00001186int mbedtls_ssl_tls13_process_finished_message( mbedtls_ssl_context *ssl )
1187{
XiaokangQian33062842021-11-11 03:37:45 +00001188 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
XiaokangQianc5c39d52021-11-09 11:55:10 +00001189 unsigned char *buf;
Xiaofei Baieef15042021-11-18 07:29:56 +00001190 size_t buf_len;
XiaokangQianc5c39d52021-11-09 11:55:10 +00001191
XiaokangQiand0aa3e92021-11-10 06:17:40 +00001192 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse finished message" ) );
XiaokangQianc5c39d52021-11-09 11:55:10 +00001193
1194 /* Preprocessing step: Compute handshake digest */
XiaokangQianaaa0e192021-11-10 03:07:04 +00001195 MBEDTLS_SSL_PROC_CHK( ssl_tls13_preprocess_finished_message( ssl ) );
XiaokangQianc5c39d52021-11-09 11:55:10 +00001196
Xiaofei Bai746f9482021-11-12 08:53:56 +00001197 MBEDTLS_SSL_PROC_CHK( mbedtls_ssl_tls13_fetch_handshake_msg( ssl,
XiaokangQianc5c39d52021-11-09 11:55:10 +00001198 MBEDTLS_SSL_HS_FINISHED,
Xiaofei Baieef15042021-11-18 07:29:56 +00001199 &buf, &buf_len ) );
1200 MBEDTLS_SSL_PROC_CHK( ssl_tls13_parse_finished_message( ssl, buf, buf + buf_len ) );
Ronald Cron8f6d39a2022-03-10 18:56:50 +01001201 mbedtls_ssl_add_hs_msg_to_checksum( ssl, MBEDTLS_SSL_HS_FINISHED,
1202 buf, buf_len );
XiaokangQianaaa0e192021-11-10 03:07:04 +00001203 MBEDTLS_SSL_PROC_CHK( ssl_tls13_postprocess_finished_message( ssl ) );
XiaokangQianc5c39d52021-11-09 11:55:10 +00001204
1205cleanup:
1206
XiaokangQiand0aa3e92021-11-10 06:17:40 +00001207 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse finished message" ) );
XiaokangQianc5c39d52021-11-09 11:55:10 +00001208 return( ret );
1209}
1210
XiaokangQian74af2a82021-09-22 07:40:30 +00001211/*
1212 *
XiaokangQiancc90c942021-11-09 12:30:09 +00001213 * STATE HANDLING: Write and send Finished message.
XiaokangQian74af2a82021-09-22 07:40:30 +00001214 *
1215 */
XiaokangQian74af2a82021-09-22 07:40:30 +00001216/*
XiaokangQian35dc6252021-11-11 08:16:19 +00001217 * Implement
XiaokangQian74af2a82021-09-22 07:40:30 +00001218 */
1219
XiaokangQian8773aa02021-11-10 07:33:09 +00001220static int ssl_tls13_prepare_finished_message( mbedtls_ssl_context *ssl )
XiaokangQian74af2a82021-09-22 07:40:30 +00001221{
1222 int ret;
1223
1224 /* Compute transcript of handshake up to now. */
XiaokangQiancc90c942021-11-09 12:30:09 +00001225 ret = mbedtls_ssl_tls13_calculate_verify_data( ssl,
XiaokangQian74af2a82021-09-22 07:40:30 +00001226 ssl->handshake->state_local.finished_out.digest,
1227 sizeof( ssl->handshake->state_local.finished_out.digest ),
1228 &ssl->handshake->state_local.finished_out.digest_len,
1229 ssl->conf->endpoint );
1230
1231 if( ret != 0 )
1232 {
Jerry Yu7ca30542021-12-08 15:57:57 +08001233 MBEDTLS_SSL_DEBUG_RET( 1, "calculate_verify_data failed", ret );
XiaokangQian74af2a82021-09-22 07:40:30 +00001234 return( ret );
1235 }
1236
1237 return( 0 );
1238}
1239
XiaokangQiancc90c942021-11-09 12:30:09 +00001240static int ssl_tls13_finalize_finished_message( mbedtls_ssl_context *ssl )
XiaokangQian74af2a82021-09-22 07:40:30 +00001241{
XiaokangQian0fa66432021-11-15 03:33:57 +00001242 // TODO: Add back resumption keys calculation after MVP.
1243 ((void) ssl);
XiaokangQian74af2a82021-09-22 07:40:30 +00001244
1245 return( 0 );
1246}
1247
XiaokangQian8773aa02021-11-10 07:33:09 +00001248static int ssl_tls13_write_finished_message_body( mbedtls_ssl_context *ssl,
XiaokangQian35dc6252021-11-11 08:16:19 +00001249 unsigned char *buf,
1250 unsigned char *end,
Xiaofei Baid25fab62021-12-02 06:36:27 +00001251 size_t *out_len )
XiaokangQian74af2a82021-09-22 07:40:30 +00001252{
XiaokangQian8773aa02021-11-10 07:33:09 +00001253 size_t verify_data_len = ssl->handshake->state_local.finished_out.digest_len;
XiaokangQian0fa66432021-11-15 03:33:57 +00001254 /*
1255 * struct {
1256 * opaque verify_data[Hash.length];
1257 * } Finished;
1258 */
XiaokangQian8773aa02021-11-10 07:33:09 +00001259 MBEDTLS_SSL_CHK_BUF_PTR( buf, end, verify_data_len );
XiaokangQian74af2a82021-09-22 07:40:30 +00001260
1261 memcpy( buf, ssl->handshake->state_local.finished_out.digest,
XiaokangQian8773aa02021-11-10 07:33:09 +00001262 verify_data_len );
XiaokangQian74af2a82021-09-22 07:40:30 +00001263
Xiaofei Baid25fab62021-12-02 06:36:27 +00001264 *out_len = verify_data_len;
XiaokangQian74af2a82021-09-22 07:40:30 +00001265 return( 0 );
1266}
XiaokangQianc5c39d52021-11-09 11:55:10 +00001267
XiaokangQian35dc6252021-11-11 08:16:19 +00001268/* Main entry point: orchestrates the other functions */
1269int mbedtls_ssl_tls13_write_finished_message( mbedtls_ssl_context *ssl )
1270{
1271 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1272 unsigned char *buf;
1273 size_t buf_len, msg_len;
1274
1275 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write finished message" ) );
1276
XiaokangQiandce82242021-11-15 06:01:26 +00001277 MBEDTLS_SSL_PROC_CHK( ssl_tls13_prepare_finished_message( ssl ) );
1278
Ronald Cron8f6d39a2022-03-10 18:56:50 +01001279 MBEDTLS_SSL_PROC_CHK( mbedtls_ssl_start_handshake_msg( ssl,
XiaokangQian35dc6252021-11-11 08:16:19 +00001280 MBEDTLS_SSL_HS_FINISHED, &buf, &buf_len ) );
1281
1282 MBEDTLS_SSL_PROC_CHK( ssl_tls13_write_finished_message_body(
1283 ssl, buf, buf + buf_len, &msg_len ) );
1284
Ronald Cron8f6d39a2022-03-10 18:56:50 +01001285 mbedtls_ssl_add_hs_msg_to_checksum( ssl, MBEDTLS_SSL_HS_FINISHED,
1286 buf, msg_len );
XiaokangQian35dc6252021-11-11 08:16:19 +00001287
1288 MBEDTLS_SSL_PROC_CHK( ssl_tls13_finalize_finished_message( ssl ) );
Ronald Cron8f6d39a2022-03-10 18:56:50 +01001289 MBEDTLS_SSL_PROC_CHK( mbedtls_ssl_finish_handshake_msg(
1290 ssl, buf_len, msg_len ) );
XiaokangQian35dc6252021-11-11 08:16:19 +00001291cleanup:
1292
1293 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write finished message" ) );
1294 return( ret );
1295}
1296
Jerry Yu378254d2021-10-30 21:44:47 +08001297void mbedtls_ssl_tls13_handshake_wrapup( mbedtls_ssl_context *ssl )
1298{
1299
1300 MBEDTLS_SSL_DEBUG_MSG( 3, ( "=> handshake wrapup" ) );
1301
1302 /*
Jerry Yucfe64f02021-11-15 13:54:06 +08001303 * Free the previous session and switch to the current one.
Jerry Yu378254d2021-10-30 21:44:47 +08001304 */
1305 if( ssl->session )
1306 {
Jerry Yu378254d2021-10-30 21:44:47 +08001307 mbedtls_ssl_session_free( ssl->session );
1308 mbedtls_free( ssl->session );
1309 }
1310 ssl->session = ssl->session_negotiate;
1311 ssl->session_negotiate = NULL;
1312
1313 MBEDTLS_SSL_DEBUG_MSG( 3, ( "<= handshake wrapup" ) );
1314}
1315
Ronald Cron49ad6192021-11-24 16:25:31 +01001316/*
1317 *
1318 * STATE HANDLING: Write ChangeCipherSpec
1319 *
1320 */
1321#if defined(MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE)
Ronald Cron49ad6192021-11-24 16:25:31 +01001322static int ssl_tls13_write_change_cipher_spec_body( mbedtls_ssl_context *ssl,
1323 unsigned char *buf,
1324 unsigned char *end,
1325 size_t *olen )
1326{
1327 ((void) ssl);
1328
1329 MBEDTLS_SSL_CHK_BUF_PTR( buf, end, 1 );
1330 buf[0] = 1;
1331 *olen = 1;
1332
1333 return( 0 );
1334}
1335
Ronald Cron49ad6192021-11-24 16:25:31 +01001336int mbedtls_ssl_tls13_write_change_cipher_spec( mbedtls_ssl_context *ssl )
1337{
1338 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1339
1340 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write change cipher spec" ) );
1341
Ronald Cron49ad6192021-11-24 16:25:31 +01001342 /* Write CCS message */
1343 MBEDTLS_SSL_PROC_CHK( ssl_tls13_write_change_cipher_spec_body(
1344 ssl, ssl->out_msg,
1345 ssl->out_msg + MBEDTLS_SSL_OUT_CONTENT_LEN,
1346 &ssl->out_msglen ) );
1347
1348 ssl->out_msgtype = MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC;
1349
Ronald Cron49ad6192021-11-24 16:25:31 +01001350 /* Dispatch message */
Ronald Cron66dbf912022-02-02 15:33:46 +01001351 MBEDTLS_SSL_PROC_CHK( mbedtls_ssl_write_record( ssl, 0 ) );
Ronald Cron49ad6192021-11-24 16:25:31 +01001352
1353cleanup:
1354
1355 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write change cipher spec" ) );
1356 return( ret );
1357}
1358
1359#endif /* MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE */
1360
XiaokangQian78b1fa72022-01-19 06:56:30 +00001361/* Reset SSL context and update hash for handling HRR.
1362 *
1363 * Replace Transcript-Hash(X) by
1364 * Transcript-Hash( message_hash ||
1365 * 00 00 Hash.length ||
1366 * X )
1367 * A few states of the handshake are preserved, including:
1368 * - session ID
1369 * - session ticket
1370 * - negotiated ciphersuite
1371 */
1372int mbedtls_ssl_reset_transcript_for_hrr( mbedtls_ssl_context *ssl )
1373{
1374 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1375 unsigned char hash_transcript[ MBEDTLS_MD_MAX_SIZE + 4 ];
XiaokangQian0ece9982022-01-24 08:56:23 +00001376 size_t hash_len;
XiaokangQian78b1fa72022-01-19 06:56:30 +00001377 const mbedtls_ssl_ciphersuite_t *ciphersuite_info;
1378 uint16_t cipher_suite = ssl->session_negotiate->ciphersuite;
1379 ciphersuite_info = mbedtls_ssl_ciphersuite_from_id( cipher_suite );
1380
1381 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Reset SSL session for HRR" ) );
1382
XiaokangQian0ece9982022-01-24 08:56:23 +00001383 ret = mbedtls_ssl_get_handshake_transcript( ssl, ciphersuite_info->mac,
1384 hash_transcript + 4,
1385 MBEDTLS_MD_MAX_SIZE,
1386 &hash_len );
1387 if( ret != 0 )
1388 {
1389 MBEDTLS_SSL_DEBUG_RET( 4, "mbedtls_ssl_get_handshake_transcript", ret );
1390 return( ret );
1391 }
1392
1393 hash_transcript[0] = MBEDTLS_SSL_HS_MESSAGE_HASH;
1394 hash_transcript[1] = 0;
1395 hash_transcript[2] = 0;
1396 hash_transcript[3] = (unsigned char) hash_len;
1397
1398 hash_len += 4;
1399
XiaokangQian78b1fa72022-01-19 06:56:30 +00001400 if( ciphersuite_info->mac == MBEDTLS_MD_SHA256 )
1401 {
1402#if defined(MBEDTLS_SHA256_C)
XiaokangQian78b1fa72022-01-19 06:56:30 +00001403 MBEDTLS_SSL_DEBUG_BUF( 4, "Truncated SHA-256 handshake transcript",
XiaokangQian0ece9982022-01-24 08:56:23 +00001404 hash_transcript, hash_len );
XiaokangQian78b1fa72022-01-19 06:56:30 +00001405
1406#if defined(MBEDTLS_USE_PSA_CRYPTO)
1407 psa_hash_abort( &ssl->handshake->fin_sha256_psa );
1408 psa_hash_setup( &ssl->handshake->fin_sha256_psa, PSA_ALG_SHA_256 );
1409#else
1410 mbedtls_sha256_starts( &ssl->handshake->fin_sha256, 0 );
1411#endif
XiaokangQian78b1fa72022-01-19 06:56:30 +00001412#endif /* MBEDTLS_SHA256_C */
1413 }
1414 else if( ciphersuite_info->mac == MBEDTLS_MD_SHA384 )
1415 {
1416#if defined(MBEDTLS_SHA384_C)
XiaokangQian78b1fa72022-01-19 06:56:30 +00001417 MBEDTLS_SSL_DEBUG_BUF( 4, "Truncated SHA-384 handshake transcript",
XiaokangQian0ece9982022-01-24 08:56:23 +00001418 hash_transcript, hash_len );
XiaokangQian78b1fa72022-01-19 06:56:30 +00001419
1420#if defined(MBEDTLS_USE_PSA_CRYPTO)
1421 psa_hash_abort( &ssl->handshake->fin_sha384_psa );
1422 psa_hash_setup( &ssl->handshake->fin_sha384_psa, PSA_ALG_SHA_384 );
1423#else
1424 mbedtls_sha512_starts( &ssl->handshake->fin_sha512, 1 );
1425#endif
XiaokangQian78b1fa72022-01-19 06:56:30 +00001426#endif /* MBEDTLS_SHA384_C */
1427 }
1428
XiaokangQian0ece9982022-01-24 08:56:23 +00001429#if defined(MBEDTLS_SHA256_C) || defined(MBEDTLS_SHA384_C)
1430 ssl->handshake->update_checksum( ssl, hash_transcript, hash_len );
1431#endif /* MBEDTLS_SHA256_C || MBEDTLS_SHA384_C */
Przemyslaw Stekiel4b3fff42022-02-14 16:39:52 +01001432
XiaokangQian78b1fa72022-01-19 06:56:30 +00001433 return( ret );
1434}
1435
XiaokangQian9b5d04b2022-04-10 10:20:43 +00001436#if defined(MBEDTLS_ECDH_C)
XiaokangQian7807f9f2022-02-15 10:04:37 +00001437
XiaokangQian9b5d04b2022-04-10 10:20:43 +00001438int mbedtls_ssl_tls13_read_public_ecdhe_share( mbedtls_ssl_context *ssl,
1439 const unsigned char *buf,
1440 size_t buf_len )
XiaokangQian7807f9f2022-02-15 10:04:37 +00001441{
XiaokangQian9b5d04b2022-04-10 10:20:43 +00001442 uint8_t *p = (uint8_t*)buf;
XiaokangQiancfd925f2022-04-14 07:10:37 +00001443 const uint8_t *end = buf + buf_len;
XiaokangQian9b5d04b2022-04-10 10:20:43 +00001444 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001445
XiaokangQian9b5d04b2022-04-10 10:20:43 +00001446 /* Get size of the TLS opaque key_exchange field of the KeyShareEntry struct. */
XiaokangQiancfd925f2022-04-14 07:10:37 +00001447 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 2 );
XiaokangQian9b5d04b2022-04-10 10:20:43 +00001448 uint16_t peerkey_len = MBEDTLS_GET_UINT16_BE( p, 0 );
1449 p += 2;
XiaokangQian3207a322022-02-23 03:15:27 +00001450
XiaokangQian9b5d04b2022-04-10 10:20:43 +00001451 /* Check if key size is consistent with given buffer length. */
XiaokangQiancfd925f2022-04-14 07:10:37 +00001452 MBEDTLS_SSL_CHK_BUF_PTR( p, end, peerkey_len );
XiaokangQian9b5d04b2022-04-10 10:20:43 +00001453
1454 /* Store peer's ECDH public key. */
1455 memcpy( handshake->ecdh_psa_peerkey, p, peerkey_len );
1456 handshake->ecdh_psa_peerkey_len = peerkey_len;
1457
XiaokangQian3207a322022-02-23 03:15:27 +00001458 return( 0 );
1459}
Jerry Yu89e103c2022-03-30 22:43:29 +08001460
1461int mbedtls_ssl_tls13_generate_and_write_ecdh_key_exchange(
1462 mbedtls_ssl_context *ssl,
1463 uint16_t named_group,
1464 unsigned char *buf,
1465 unsigned char *end,
1466 size_t *out_len )
1467{
1468 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
1469 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
1470 psa_key_attributes_t key_attributes;
1471 size_t own_pubkey_len;
1472 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
1473 size_t ecdh_bits = 0;
1474
1475 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Perform PSA-based ECDH computation." ) );
1476
1477 /* Convert EC group to PSA key type. */
1478 if( ( handshake->ecdh_psa_type =
1479 mbedtls_psa_parse_tls_ecc_group( named_group, &ecdh_bits ) ) == 0 )
1480 return( MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
1481
1482 ssl->handshake->ecdh_bits = ecdh_bits;
1483
1484 key_attributes = psa_key_attributes_init();
1485 psa_set_key_usage_flags( &key_attributes, PSA_KEY_USAGE_DERIVE );
1486 psa_set_key_algorithm( &key_attributes, PSA_ALG_ECDH );
1487 psa_set_key_type( &key_attributes, handshake->ecdh_psa_type );
1488 psa_set_key_bits( &key_attributes, handshake->ecdh_bits );
1489
1490 /* Generate ECDH private key. */
1491 status = psa_generate_key( &key_attributes,
1492 &handshake->ecdh_psa_privkey );
1493 if( status != PSA_SUCCESS )
1494 {
1495 ret = psa_ssl_status_to_mbedtls( status );
1496 MBEDTLS_SSL_DEBUG_RET( 1, "psa_generate_key", ret );
1497 return( ret );
1498
1499 }
1500
1501 /* Export the public part of the ECDH private key from PSA. */
1502 status = psa_export_public_key( handshake->ecdh_psa_privkey,
1503 buf, (size_t)( end - buf ),
1504 &own_pubkey_len );
1505 if( status != PSA_SUCCESS )
1506 {
1507 ret = psa_ssl_status_to_mbedtls( status );
1508 MBEDTLS_SSL_DEBUG_RET( 1, "psa_export_public_key", ret );
1509 return( ret );
1510
1511 }
1512
1513 *out_len = own_pubkey_len;
1514
1515 return( 0 );
1516}
XiaokangQian9b5d04b2022-04-10 10:20:43 +00001517#endif /* MBEDTLS_ECDH_C */
XiaokangQian7807f9f2022-02-15 10:04:37 +00001518
Jerry Yufb4b6472022-01-27 15:03:26 +08001519#endif /* MBEDTLS_SSL_TLS_C && MBEDTLS_SSL_PROTO_TLS1_3 */