blob: 786a72c025a5a7a0c0419effcc9d3ce7046c029c [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
XiaokangQian6b916b12022-04-25 07:29:34 +0000414 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 1 );
Xiaofei Bai947571e2021-09-29 09:12:03 +0000415 certificate_request_context_len = p[0];
XiaokangQian6b916b12022-04-25 07:29:34 +0000416 p++;
417
418#if defined(MBEDTLS_SSL_SRV_C)
419 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
420 {
421 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end,
422 certificate_request_context_len + 3 );
423
424 /* check whether we got an empty certificate message */
425 if( memcmp( p + certificate_request_context_len , "\0\0\0", 3 ) == 0 )
426 {
427 MBEDTLS_SSL_DEBUG_MSG( 1,
428 ( "client has no certificate - empty certificate message received" ) );
429
430 ssl->session_negotiate->verify_result = MBEDTLS_X509_BADCERT_MISSING;
431 if( ssl->conf->authmode == MBEDTLS_SSL_VERIFY_OPTIONAL )
432 return( 0 );
433 else
434 {
435 MBEDTLS_SSL_DEBUG_MSG( 1, ( "client certificate required" ) );
436 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_CERT_REQUIRED,
437 MBEDTLS_ERR_SSL_NO_CLIENT_CERTIFICATE );
438 return( MBEDTLS_ERR_SSL_NO_CLIENT_CERTIFICATE );
439 }
440 }
441 }
442#endif /* MBEDTLS_SSL_SRV_C */
443
444 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 3 );
445 certificate_list_len = MBEDTLS_GET_UINT24_BE( p, 0 );
446 p += 3;
Xiaofei Bai947571e2021-09-29 09:12:03 +0000447
448 /* In theory, the certificate list can be up to 2^24 Bytes, but we don't
449 * support anything beyond 2^16 = 64K.
450 */
Xiaofei Bai79595ac2021-10-26 07:16:45 +0000451 if( ( certificate_request_context_len != 0 ) ||
452 ( certificate_list_len >= 0x10000 ) )
Xiaofei Bai947571e2021-09-29 09:12:03 +0000453 {
454 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
455 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR,
456 MBEDTLS_ERR_SSL_DECODE_ERROR );
457 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
458 }
459
460 /* In case we tried to reuse a session but it failed */
461 if( ssl->session_negotiate->peer_cert != NULL )
462 {
463 mbedtls_x509_crt_free( ssl->session_negotiate->peer_cert );
464 mbedtls_free( ssl->session_negotiate->peer_cert );
465 }
466
XiaokangQianc3017f62022-05-13 05:55:41 +0000467 if( certificate_list_len == 0 )
468 {
469 ssl->session_negotiate->peer_cert = NULL;
470 ret = 0;
471 goto exit;
472 }
473
Xiaofei Bai947571e2021-09-29 09:12:03 +0000474 if( ( ssl->session_negotiate->peer_cert =
475 mbedtls_calloc( 1, sizeof( mbedtls_x509_crt ) ) ) == NULL )
476 {
477 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc( %" MBEDTLS_PRINTF_SIZET " bytes ) failed",
478 sizeof( mbedtls_x509_crt ) ) );
479 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR,
480 MBEDTLS_ERR_SSL_ALLOC_FAILED );
481 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
482 }
483
484 mbedtls_x509_crt_init( ssl->session_negotiate->peer_cert );
485
Xiaofei Bai947571e2021-09-29 09:12:03 +0000486 certificate_list_end = p + certificate_list_len;
Xiaofei Bai79595ac2021-10-26 07:16:45 +0000487 while( p < certificate_list_end )
Xiaofei Bai947571e2021-09-29 09:12:03 +0000488 {
489 size_t cert_data_len, extensions_len;
490
Xiaofei Bai79595ac2021-10-26 07:16:45 +0000491 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, certificate_list_end, 3 );
Xiaofei Baif93cbd22021-10-29 02:39:30 +0000492 cert_data_len = MBEDTLS_GET_UINT24_BE( p, 0 );
Xiaofei Bai947571e2021-09-29 09:12:03 +0000493 p += 3;
494
495 /* In theory, the CRT can be up to 2^24 Bytes, but we don't support
496 * anything beyond 2^16 = 64K. Otherwise as in the TLS 1.2 code,
497 * check that we have a minimum of 128 bytes of data, this is not
498 * clear why we need that though.
499 */
500 if( ( cert_data_len < 128 ) || ( cert_data_len >= 0x10000 ) )
Xiaofei Bai79595ac2021-10-26 07:16:45 +0000501 {
Xiaofei Bai947571e2021-09-29 09:12:03 +0000502 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad Certificate message" ) );
503 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR,
504 MBEDTLS_ERR_SSL_DECODE_ERROR );
505 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
506 }
507
Xiaofei Bai79595ac2021-10-26 07:16:45 +0000508 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, certificate_list_end, cert_data_len );
Xiaofei Bai947571e2021-09-29 09:12:03 +0000509 ret = mbedtls_x509_crt_parse_der( ssl->session_negotiate->peer_cert,
510 p, cert_data_len );
511
512 switch( ret )
513 {
514 case 0: /*ok*/
515 break;
516 case MBEDTLS_ERR_X509_UNKNOWN_SIG_ALG + MBEDTLS_ERR_OID_NOT_FOUND:
517 /* Ignore certificate with an unknown algorithm: maybe a
518 prior certificate was already trusted. */
519 break;
520
521 case MBEDTLS_ERR_X509_ALLOC_FAILED:
522 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR,
523 MBEDTLS_ERR_X509_ALLOC_FAILED );
524 MBEDTLS_SSL_DEBUG_RET( 1, " mbedtls_x509_crt_parse_der", ret );
525 return( ret );
526
527 case MBEDTLS_ERR_X509_UNKNOWN_VERSION:
528 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT,
529 MBEDTLS_ERR_X509_UNKNOWN_VERSION );
530 MBEDTLS_SSL_DEBUG_RET( 1, " mbedtls_x509_crt_parse_der", ret );
531 return( ret );
532
533 default:
534 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_BAD_CERT,
535 ret );
536 MBEDTLS_SSL_DEBUG_RET( 1, " mbedtls_x509_crt_parse_der", ret );
537 return( ret );
538 }
539
540 p += cert_data_len;
541
542 /* Certificate extensions length */
543 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, certificate_list_end, 2 );
544 extensions_len = MBEDTLS_GET_UINT16_BE( p, 0 );
545 p += 2;
Xiaofei Bai79595ac2021-10-26 07:16:45 +0000546 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, certificate_list_end, extensions_len );
Xiaofei Bai947571e2021-09-29 09:12:03 +0000547 p += extensions_len;
548 }
549
550 /* Check that all the message is consumed. */
551 if( p != end )
552 {
553 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad Certificate message" ) );
554 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR, \
555 MBEDTLS_ERR_SSL_DECODE_ERROR );
556 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
557 }
558
XiaokangQianc3017f62022-05-13 05:55:41 +0000559exit:
Xiaofei Bai947571e2021-09-29 09:12:03 +0000560 MBEDTLS_SSL_DEBUG_CRT( 3, "peer certificate", ssl->session_negotiate->peer_cert );
561
562 return( ret );
563}
564#else
565static int ssl_tls13_parse_certificate( mbedtls_ssl_context *ssl,
566 const unsigned char *buf,
567 const unsigned char *end )
568{
569 ((void) ssl);
570 ((void) buf);
571 ((void) end);
572 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
573}
574#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
575#endif /* MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED */
576
577#if defined(MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED)
578#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Xiaofei Bai79595ac2021-10-26 07:16:45 +0000579/* Validate certificate chain sent by the server. */
Xiaofei Bai947571e2021-09-29 09:12:03 +0000580static int ssl_tls13_validate_certificate( mbedtls_ssl_context *ssl )
581{
582 int ret = 0;
XiaokangQian6b916b12022-04-25 07:29:34 +0000583 int authmode = ssl->conf->authmode;
Xiaofei Bai947571e2021-09-29 09:12:03 +0000584 mbedtls_x509_crt *ca_chain;
585 mbedtls_x509_crl *ca_crl;
Xiaofei Baiff456022021-10-28 06:50:17 +0000586 uint32_t verify_result = 0;
Xiaofei Bai947571e2021-09-29 09:12:03 +0000587
XiaokangQian6b916b12022-04-25 07:29:34 +0000588 /* If SNI was used, overwrite authentication mode
589 * from the configuration. */
590#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
591 if( ssl->handshake->sni_authmode != MBEDTLS_SSL_VERIFY_UNSET )
592 authmode = ssl->handshake->sni_authmode;
593#endif
594
595 /*
596 * If the client hasn't sent a certificate ( i.e. it sent
597 * an empty certificate chain ), this is reflected in the peer CRT
598 * structure being unset.
599 * Check for that and handle it depending on the
600 * server's authentication mode.
601 */
602#if defined(MBEDTLS_SSL_SRV_C)
603 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
604 ssl->session_negotiate->peer_cert == NULL )
605 {
606 MBEDTLS_SSL_DEBUG_MSG( 1, ( "client has no certificate" ) );
607
608 /* The client was asked for a certificate but didn't send
609 one. The client should know what's going on, so we
610 don't send an alert. */
611
612 /* Note that for authmode == VERIFY_NONE we don't end up in this
613 * routine in the first place, because ssl_tls13_read_certificate_coordinate
614 * will return CERTIFICATE_SKIP. */
615 ssl->session_negotiate->verify_result = MBEDTLS_X509_BADCERT_MISSING;
616 if( authmode == MBEDTLS_SSL_VERIFY_OPTIONAL )
617 return( 0 );
618 else
619 return( MBEDTLS_ERR_SSL_NO_CLIENT_CERTIFICATE );
620 }
621#endif /* MBEDTLS_SSL_SRV_C */
622
XiaokangQianc3017f62022-05-13 05:55:41 +0000623#if defined(MBEDTLS_SSL_CLI_C)
624 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
625 ssl->session_negotiate->peer_cert == NULL )
XiaokangQian6b916b12022-04-25 07:29:34 +0000626 {
XiaokangQianc3017f62022-05-13 05:55:41 +0000627 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_NO_CERT,
628 MBEDTLS_ERR_SSL_FATAL_ALERT_MESSAGE );
629 return( MBEDTLS_ERR_SSL_FATAL_ALERT_MESSAGE );
XiaokangQian6b916b12022-04-25 07:29:34 +0000630 }
XiaokangQianc3017f62022-05-13 05:55:41 +0000631#endif /* MBEDTLS_SSL_CLI_C */
XiaokangQian6b916b12022-04-25 07:29:34 +0000632
Xiaofei Bai947571e2021-09-29 09:12:03 +0000633#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
634 if( ssl->handshake->sni_ca_chain != NULL )
635 {
636 ca_chain = ssl->handshake->sni_ca_chain;
637 ca_crl = ssl->handshake->sni_ca_crl;
638 }
639 else
640#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
641 {
642 ca_chain = ssl->conf->ca_chain;
643 ca_crl = ssl->conf->ca_crl;
644 }
645
646 /*
647 * Main check: verify certificate
648 */
649 ret = mbedtls_x509_crt_verify_with_profile(
650 ssl->session_negotiate->peer_cert,
651 ca_chain, ca_crl,
652 ssl->conf->cert_profile,
653 ssl->hostname,
Xiaofei Baiff456022021-10-28 06:50:17 +0000654 &verify_result,
Xiaofei Bai947571e2021-09-29 09:12:03 +0000655 ssl->conf->f_vrfy, ssl->conf->p_vrfy );
656
657 if( ret != 0 )
658 {
659 MBEDTLS_SSL_DEBUG_RET( 1, "x509_verify_cert", ret );
660 }
661
662 /*
663 * Secondary checks: always done, but change 'ret' only if it was 0
664 */
Xiaofei Bai947571e2021-09-29 09:12:03 +0000665 if( mbedtls_ssl_check_cert_usage( ssl->session_negotiate->peer_cert,
666 ssl->handshake->ciphersuite_info,
667 !ssl->conf->endpoint,
Xiaofei Baiff456022021-10-28 06:50:17 +0000668 &verify_result ) != 0 )
Xiaofei Bai947571e2021-09-29 09:12:03 +0000669 {
670 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate ( usage extensions )" ) );
671 if( ret == 0 )
672 ret = MBEDTLS_ERR_SSL_BAD_CERTIFICATE;
673 }
674
XiaokangQian6b916b12022-04-25 07:29:34 +0000675 /* mbedtls_x509_crt_verify_with_profile is supposed to report a
676 * verification failure through MBEDTLS_ERR_X509_CERT_VERIFY_FAILED,
677 * with details encoded in the verification flags. All other kinds
678 * of error codes, including those from the user provided f_vrfy
679 * functions, are treated as fatal and lead to a failure of
680 * ssl_tls13_parse_certificate even if verification was optional. */
681 if( authmode == MBEDTLS_SSL_VERIFY_OPTIONAL &&
682 ( ret == MBEDTLS_ERR_X509_CERT_VERIFY_FAILED ||
683 ret == MBEDTLS_ERR_SSL_BAD_CERTIFICATE ) )
684 {
685 ret = 0;
686 }
Xiaofei Bai947571e2021-09-29 09:12:03 +0000687
XiaokangQian6b916b12022-04-25 07:29:34 +0000688
689 if( ca_chain == NULL && authmode == MBEDTLS_SSL_VERIFY_REQUIRED )
Xiaofei Bai947571e2021-09-29 09:12:03 +0000690 {
691 MBEDTLS_SSL_DEBUG_MSG( 1, ( "got no CA chain" ) );
692 ret = MBEDTLS_ERR_SSL_CA_CHAIN_REQUIRED;
693 }
694
695 if( ret != 0 )
696 {
697 /* The certificate may have been rejected for several reasons.
698 Pick one and send the corresponding alert. Which alert to send
699 may be a subject of debate in some cases. */
Xiaofei Baiff456022021-10-28 06:50:17 +0000700 if( verify_result & MBEDTLS_X509_BADCERT_OTHER )
Xiaofei Bai947571e2021-09-29 09:12:03 +0000701 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED, ret );
Xiaofei Baiff456022021-10-28 06:50:17 +0000702 else if( verify_result & MBEDTLS_X509_BADCERT_CN_MISMATCH )
Xiaofei Bai947571e2021-09-29 09:12:03 +0000703 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_BAD_CERT, ret );
Xiaofei Baif93cbd22021-10-29 02:39:30 +0000704 else if( verify_result & ( MBEDTLS_X509_BADCERT_KEY_USAGE |
705 MBEDTLS_X509_BADCERT_EXT_KEY_USAGE |
706 MBEDTLS_X509_BADCERT_NS_CERT_TYPE |
707 MBEDTLS_X509_BADCERT_BAD_PK |
708 MBEDTLS_X509_BADCERT_BAD_KEY ) )
Xiaofei Bai947571e2021-09-29 09:12:03 +0000709 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT, ret );
Xiaofei Baiff456022021-10-28 06:50:17 +0000710 else if( verify_result & MBEDTLS_X509_BADCERT_EXPIRED )
Xiaofei Bai947571e2021-09-29 09:12:03 +0000711 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_CERT_EXPIRED, ret );
Xiaofei Baiff456022021-10-28 06:50:17 +0000712 else if( verify_result & MBEDTLS_X509_BADCERT_REVOKED )
Xiaofei Bai947571e2021-09-29 09:12:03 +0000713 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_CERT_REVOKED, ret );
Xiaofei Baiff456022021-10-28 06:50:17 +0000714 else if( verify_result & MBEDTLS_X509_BADCERT_NOT_TRUSTED )
Xiaofei Bai947571e2021-09-29 09:12:03 +0000715 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_UNKNOWN_CA, ret );
716 else
717 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_CERT_UNKNOWN, ret );
718 }
719
720#if defined(MBEDTLS_DEBUG_C)
Xiaofei Baiff456022021-10-28 06:50:17 +0000721 if( verify_result != 0 )
Xiaofei Bai947571e2021-09-29 09:12:03 +0000722 {
723 MBEDTLS_SSL_DEBUG_MSG( 3, ( "! Certificate verification flags %08x",
Jerry Yu83bb1312021-10-28 22:16:33 +0800724 (unsigned int) verify_result ) );
Xiaofei Bai947571e2021-09-29 09:12:03 +0000725 }
726 else
727 {
728 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Certificate verification flags clear" ) );
729 }
730#endif /* MBEDTLS_DEBUG_C */
731
Xiaofei Baiff456022021-10-28 06:50:17 +0000732 ssl->session_negotiate->verify_result = verify_result;
Xiaofei Bai947571e2021-09-29 09:12:03 +0000733 return( ret );
734}
735#else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
736static int ssl_tls13_validate_certificate( mbedtls_ssl_context *ssl )
737{
738 ((void) ssl);
739 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
740}
741#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
742#endif /* MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED */
743
Xiaofei Bai79595ac2021-10-26 07:16:45 +0000744int mbedtls_ssl_tls13_process_certificate( mbedtls_ssl_context *ssl )
Xiaofei Bai947571e2021-09-29 09:12:03 +0000745{
Xiaofei Bai79595ac2021-10-26 07:16:45 +0000746 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
747 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) );
748
749#if defined(MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED)
XiaokangQianc3017f62022-05-13 05:55:41 +0000750 unsigned char *buf;
751 size_t buf_len;
Xiaofei Bai79595ac2021-10-26 07:16:45 +0000752
XiaokangQianc3017f62022-05-13 05:55:41 +0000753 MBEDTLS_SSL_PROC_CHK( mbedtls_ssl_tls13_fetch_handshake_msg(
754 ssl, MBEDTLS_SSL_HS_CERTIFICATE,
755 &buf, &buf_len ) );
Xiaofei Bai79595ac2021-10-26 07:16:45 +0000756
XiaokangQianc3017f62022-05-13 05:55:41 +0000757 /* Parse the certificate chain sent by the peer. */
758 MBEDTLS_SSL_PROC_CHK( ssl_tls13_parse_certificate( ssl, buf,
759 buf + buf_len ) );
760 /* Validate the certificate chain and set the verification results. */
761 MBEDTLS_SSL_PROC_CHK( ssl_tls13_validate_certificate( ssl ) );
Xiaofei Bai79595ac2021-10-26 07:16:45 +0000762
XiaokangQianc3017f62022-05-13 05:55:41 +0000763 mbedtls_ssl_add_hs_msg_to_checksum( ssl, MBEDTLS_SSL_HS_CERTIFICATE,
764 buf, buf_len );
XiaokangQian6b916b12022-04-25 07:29:34 +0000765#endif /* MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED */
Xiaofei Bai79595ac2021-10-26 07:16:45 +0000766
Xiaofei Bai79595ac2021-10-26 07:16:45 +0000767cleanup:
768
769 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse certificate" ) );
770 return( ret );
Xiaofei Bai947571e2021-09-29 09:12:03 +0000771}
Jerry Yu90f152d2022-01-29 22:12:42 +0800772#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Jerry Yu7399d0d2022-01-30 17:54:19 +0800773/*
774 * enum {
775 * X509(0),
776 * RawPublicKey(2),
777 * (255)
778 * } CertificateType;
779 *
780 * struct {
781 * select (certificate_type) {
782 * case RawPublicKey:
783 * // From RFC 7250 ASN.1_subjectPublicKeyInfo
784 * opaque ASN1_subjectPublicKeyInfo<1..2^24-1>;
785 *
786 * case X509:
787 * opaque cert_data<1..2^24-1>;
788 * };
789 * Extension extensions<0..2^16-1>;
790 * } CertificateEntry;
791 *
792 * struct {
793 * opaque certificate_request_context<0..2^8-1>;
794 * CertificateEntry certificate_list<0..2^24-1>;
795 * } Certificate;
796 */
797static int ssl_tls13_write_certificate_body( mbedtls_ssl_context *ssl,
Jerry Yu3e536442022-02-15 11:05:59 +0800798 unsigned char *buf,
Jerry Yu7399d0d2022-01-30 17:54:19 +0800799 unsigned char *end,
Jerry Yu3e536442022-02-15 11:05:59 +0800800 size_t *out_len )
Jerry Yu5cc35062022-01-28 16:16:08 +0800801{
Jerry Yu5cc35062022-01-28 16:16:08 +0800802 const mbedtls_x509_crt *crt = mbedtls_ssl_own_cert( ssl );
Jerry Yu3e536442022-02-15 11:05:59 +0800803 unsigned char *p = buf;
Jerry Yuc8d8d4e2022-02-18 12:10:03 +0800804 unsigned char *certificate_request_context =
805 ssl->handshake->certificate_request_context;
806 unsigned char certificate_request_context_len =
807 ssl->handshake->certificate_request_context_len;
808 unsigned char *p_certificate_list_len;
Jerry Yu5cc35062022-01-28 16:16:08 +0800809
Jerry Yu5cc35062022-01-28 16:16:08 +0800810
Jerry Yu3391ac02022-02-16 11:21:37 +0800811 /* ...
812 * opaque certificate_request_context<0..2^8-1>;
813 * ...
814 */
Jerry Yuc8d8d4e2022-02-18 12:10:03 +0800815 MBEDTLS_SSL_CHK_BUF_PTR( p, end, certificate_request_context_len + 1 );
816 *p++ = certificate_request_context_len;
817 if( certificate_request_context_len > 0 )
Jerry Yu537530d2022-02-15 14:00:57 +0800818 {
Jerry Yuc8d8d4e2022-02-18 12:10:03 +0800819 memcpy( p, certificate_request_context, certificate_request_context_len );
820 p += certificate_request_context_len;
Jerry Yu537530d2022-02-15 14:00:57 +0800821 }
822
Jerry Yu3391ac02022-02-16 11:21:37 +0800823 /* ...
824 * CertificateEntry certificate_list<0..2^24-1>;
825 * ...
826 */
Jerry Yu3e536442022-02-15 11:05:59 +0800827 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 3 );
Jerry Yuc8d8d4e2022-02-18 12:10:03 +0800828 p_certificate_list_len = p;
Jerry Yu3e536442022-02-15 11:05:59 +0800829 p += 3;
830
Jerry Yu7399d0d2022-01-30 17:54:19 +0800831 MBEDTLS_SSL_DEBUG_CRT( 3, "own certificate", crt );
Jerry Yu5cc35062022-01-28 16:16:08 +0800832
Jerry Yuc8d8d4e2022-02-18 12:10:03 +0800833 while( crt != NULL )
Jerry Yu5cc35062022-01-28 16:16:08 +0800834 {
Jerry Yu7399d0d2022-01-30 17:54:19 +0800835 size_t cert_data_len = crt->raw.len;
Jerry Yu5cc35062022-01-28 16:16:08 +0800836
Jerry Yu7399d0d2022-01-30 17:54:19 +0800837 MBEDTLS_SSL_CHK_BUF_PTR( p, end, cert_data_len + 3 + 2 );
838 MBEDTLS_PUT_UINT24_BE( cert_data_len, p, 0 );
839 p += 3;
Jerry Yu5cc35062022-01-28 16:16:08 +0800840
Jerry Yu7399d0d2022-01-30 17:54:19 +0800841 memcpy( p, crt->raw.p, cert_data_len );
842 p += cert_data_len;
843 crt = crt->next;
Jerry Yu5cc35062022-01-28 16:16:08 +0800844
845 /* Currently, we don't have any certificate extensions defined.
846 * Hence, we are sending an empty extension with length zero.
847 */
Jerry Yu7399d0d2022-01-30 17:54:19 +0800848 MBEDTLS_PUT_UINT24_BE( 0, p, 0 );
849 p += 2;
Jerry Yu5cc35062022-01-28 16:16:08 +0800850 }
Jerry Yu5cc35062022-01-28 16:16:08 +0800851
Jerry Yuc8d8d4e2022-02-18 12:10:03 +0800852 MBEDTLS_PUT_UINT24_BE( p - p_certificate_list_len - 3,
853 p_certificate_list_len, 0 );
Jerry Yu7399d0d2022-01-30 17:54:19 +0800854
Jerry Yu3e536442022-02-15 11:05:59 +0800855 *out_len = p - buf;
Jerry Yu5cc35062022-01-28 16:16:08 +0800856
857 return( 0 );
858}
Jerry Yu5cc35062022-01-28 16:16:08 +0800859
Jerry Yu3e536442022-02-15 11:05:59 +0800860int mbedtls_ssl_tls13_write_certificate( mbedtls_ssl_context *ssl )
Jerry Yu5cc35062022-01-28 16:16:08 +0800861{
862 int ret;
Ronald Cron5bb8fc82022-03-09 07:00:13 +0100863 unsigned char *buf;
864 size_t buf_len, msg_len;
865
Jerry Yu5cc35062022-01-28 16:16:08 +0800866 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write certificate" ) );
867
Ronald Cron8f6d39a2022-03-10 18:56:50 +0100868 MBEDTLS_SSL_PROC_CHK( mbedtls_ssl_start_handshake_msg( ssl,
Ronald Cron5bb8fc82022-03-09 07:00:13 +0100869 MBEDTLS_SSL_HS_CERTIFICATE, &buf, &buf_len ) );
Jerry Yu5cc35062022-01-28 16:16:08 +0800870
Ronald Cron5bb8fc82022-03-09 07:00:13 +0100871 MBEDTLS_SSL_PROC_CHK( ssl_tls13_write_certificate_body( ssl,
872 buf,
873 buf + buf_len,
874 &msg_len ) );
Jerry Yu5cc35062022-01-28 16:16:08 +0800875
Ronald Cron8f6d39a2022-03-10 18:56:50 +0100876 mbedtls_ssl_add_hs_msg_to_checksum( ssl, MBEDTLS_SSL_HS_CERTIFICATE,
877 buf, msg_len );
Jerry Yu5cc35062022-01-28 16:16:08 +0800878
Ronald Cron8f6d39a2022-03-10 18:56:50 +0100879 MBEDTLS_SSL_PROC_CHK( mbedtls_ssl_finish_handshake_msg(
Ronald Cron5bb8fc82022-03-09 07:00:13 +0100880 ssl, buf_len, msg_len ) );
Jerry Yu5cc35062022-01-28 16:16:08 +0800881cleanup:
882
883 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write certificate" ) );
884 return( ret );
885}
886
Jerry Yu3e536442022-02-15 11:05:59 +0800887/*
888 * STATE HANDLING: Output Certificate Verify
889 */
Jerry Yue91a51a2022-03-22 21:42:50 +0800890static int ssl_tls13_get_sig_alg_from_pk( mbedtls_ssl_context *ssl,
891 mbedtls_pk_context *own_key,
Jerry Yu8c338862022-03-23 13:34:04 +0800892 uint16_t *algorithm )
Jerry Yu67eced02022-02-25 13:37:36 +0800893{
894 mbedtls_pk_type_t sig = mbedtls_ssl_sig_from_pk( own_key );
895 /* Determine the size of the key */
896 size_t own_key_size = mbedtls_pk_get_bitlen( own_key );
Jerry Yue91a51a2022-03-22 21:42:50 +0800897 *algorithm = MBEDTLS_TLS1_3_SIG_NONE;
Jerry Yu67eced02022-02-25 13:37:36 +0800898 ((void) own_key_size);
899
900 switch( sig )
901 {
902#if defined(MBEDTLS_ECDSA_C)
903 case MBEDTLS_SSL_SIG_ECDSA:
904 switch( own_key_size )
905 {
906 case 256:
Jerry Yue91a51a2022-03-22 21:42:50 +0800907 *algorithm = MBEDTLS_TLS1_3_SIG_ECDSA_SECP256R1_SHA256;
Jerry Yue91a51a2022-03-22 21:42:50 +0800908 return( 0 );
Jerry Yu67eced02022-02-25 13:37:36 +0800909 case 384:
Jerry Yue91a51a2022-03-22 21:42:50 +0800910 *algorithm = MBEDTLS_TLS1_3_SIG_ECDSA_SECP384R1_SHA384;
Jerry Yue91a51a2022-03-22 21:42:50 +0800911 return( 0 );
Jerry Yu67eced02022-02-25 13:37:36 +0800912 case 521:
Jerry Yue91a51a2022-03-22 21:42:50 +0800913 *algorithm = MBEDTLS_TLS1_3_SIG_ECDSA_SECP521R1_SHA512;
Jerry Yue91a51a2022-03-22 21:42:50 +0800914 return( 0 );
Jerry Yu67eced02022-02-25 13:37:36 +0800915 default:
916 MBEDTLS_SSL_DEBUG_MSG( 3,
917 ( "unknown key size: %"
918 MBEDTLS_PRINTF_SIZET " bits",
919 own_key_size ) );
920 break;
921 }
922 break;
923#endif /* MBEDTLS_ECDSA_C */
924
Jerry Yucef3f332022-03-22 23:00:13 +0800925#if defined(MBEDTLS_RSA_C)
Jerry Yu67eced02022-02-25 13:37:36 +0800926 case MBEDTLS_SSL_SIG_RSA:
Jerry Yucef3f332022-03-22 23:00:13 +0800927#if defined(MBEDTLS_PKCS1_V21)
928#if defined(MBEDTLS_SHA256_C)
Jerry Yu67eced02022-02-25 13:37:36 +0800929 if( own_key_size <= 2048 &&
930 mbedtls_ssl_sig_alg_is_received( ssl,
931 MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA256 ) )
932 {
Jerry Yue91a51a2022-03-22 21:42:50 +0800933 *algorithm = MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA256;
Jerry Yue91a51a2022-03-22 21:42:50 +0800934 return( 0 );
Jerry Yu67eced02022-02-25 13:37:36 +0800935 }
Jerry Yucef3f332022-03-22 23:00:13 +0800936 else
937#endif /* MBEDTLS_SHA256_C */
938#if defined(MBEDTLS_SHA384_C)
939 if( own_key_size <= 3072 &&
940 mbedtls_ssl_sig_alg_is_received( ssl,
Jerry Yu67eced02022-02-25 13:37:36 +0800941 MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA384 ) )
942 {
Jerry Yue91a51a2022-03-22 21:42:50 +0800943 *algorithm = MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA384;
Jerry Yue91a51a2022-03-22 21:42:50 +0800944 return( 0 );
Jerry Yu67eced02022-02-25 13:37:36 +0800945 }
Jerry Yucef3f332022-03-22 23:00:13 +0800946 else
947#endif /* MBEDTLS_SHA384_C */
948#if defined(MBEDTLS_SHA512_C)
949 if( own_key_size <= 4096 &&
950 mbedtls_ssl_sig_alg_is_received( ssl,
Jerry Yu67eced02022-02-25 13:37:36 +0800951 MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA512 ) )
952 {
Jerry Yue91a51a2022-03-22 21:42:50 +0800953 *algorithm = MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA512;
Jerry Yue91a51a2022-03-22 21:42:50 +0800954 return( 0 );
Jerry Yu67eced02022-02-25 13:37:36 +0800955 }
Jerry Yucef3f332022-03-22 23:00:13 +0800956 else
957#endif /* MBEDTLS_SHA512_C */
958#endif /* MBEDTLS_PKCS1_V21 */
959#if defined(MBEDTLS_PKCS1_V15)
960#if defined(MBEDTLS_SHA256_C)
961 if( own_key_size <= 2048 &&
962 mbedtls_ssl_sig_alg_is_received( ssl,
963 MBEDTLS_TLS1_3_SIG_RSA_PKCS1_SHA256 ) )
964 {
965 *algorithm = MBEDTLS_TLS1_3_SIG_RSA_PKCS1_SHA256;
Jerry Yucef3f332022-03-22 23:00:13 +0800966 return( 0 );
967 }
968 else
969#endif /* MBEDTLS_SHA256_C */
970#if defined(MBEDTLS_SHA384_C)
971 if( own_key_size <= 3072 &&
972 mbedtls_ssl_sig_alg_is_received( ssl,
973 MBEDTLS_TLS1_3_SIG_RSA_PKCS1_SHA384 ) )
974 {
975 *algorithm = MBEDTLS_TLS1_3_SIG_RSA_PKCS1_SHA384;
Jerry Yucef3f332022-03-22 23:00:13 +0800976 return( 0 );
977 }
978 else
979#endif /* MBEDTLS_SHA384_C */
980#if defined(MBEDTLS_SHA512_C)
981 if( own_key_size <= 4096 &&
982 mbedtls_ssl_sig_alg_is_received( ssl,
983 MBEDTLS_TLS1_3_SIG_RSA_PKCS1_SHA512 ) )
984 {
985 *algorithm = MBEDTLS_TLS1_3_SIG_RSA_PKCS1_SHA512;
Jerry Yucef3f332022-03-22 23:00:13 +0800986 return( 0 );
987 }
988 else
989#endif /* MBEDTLS_SHA512_C */
990#endif /* MBEDTLS_PKCS1_V15 */
991 {
992 MBEDTLS_SSL_DEBUG_MSG( 3,
993 ( "unknown key size: %"
994 MBEDTLS_PRINTF_SIZET " bits",
995 own_key_size ) );
996 }
Jerry Yu67eced02022-02-25 13:37:36 +0800997 break;
Jerry Yucef3f332022-03-22 23:00:13 +0800998#endif /* MBEDTLS_RSA_C */
Jerry Yu67eced02022-02-25 13:37:36 +0800999 default:
1000 MBEDTLS_SSL_DEBUG_MSG( 1,
Andrzej Kurek5c65c572022-04-13 14:28:52 -04001001 ( "unknown signature type : %u", sig ) );
Jerry Yu67eced02022-02-25 13:37:36 +08001002 break;
1003 }
Jerry Yue91a51a2022-03-22 21:42:50 +08001004 return( -1 );
Jerry Yu67eced02022-02-25 13:37:36 +08001005}
1006
Jerry Yu3e536442022-02-15 11:05:59 +08001007static int ssl_tls13_write_certificate_verify_body( mbedtls_ssl_context *ssl,
1008 unsigned char *buf,
1009 unsigned char *end,
1010 size_t *out_len )
Jerry Yu8511f122022-01-29 10:01:04 +08001011{
1012 int ret;
Jerry Yu3e536442022-02-15 11:05:59 +08001013 unsigned char *p = buf;
Jerry Yu8511f122022-01-29 10:01:04 +08001014 mbedtls_pk_context *own_key;
Jerry Yu3e536442022-02-15 11:05:59 +08001015
Jerry Yu8511f122022-01-29 10:01:04 +08001016 unsigned char handshake_hash[ MBEDTLS_TLS1_3_MD_MAX_SIZE ];
1017 size_t handshake_hash_len;
Jerry Yu3e536442022-02-15 11:05:59 +08001018 unsigned char verify_buffer[ SSL_VERIFY_STRUCT_MAX_SIZE ];
1019 size_t verify_buffer_len;
Jerry Yu67eced02022-02-25 13:37:36 +08001020 mbedtls_pk_type_t pk_type = MBEDTLS_PK_NONE;
Jerry Yu67eced02022-02-25 13:37:36 +08001021 mbedtls_md_type_t md_alg = MBEDTLS_MD_NONE;
Jerry Yu78272072022-02-22 10:28:13 +08001022 uint16_t algorithm = MBEDTLS_TLS1_3_SIG_NONE;
Jerry Yu3e536442022-02-15 11:05:59 +08001023 size_t signature_len = 0;
Jerry Yu8511f122022-01-29 10:01:04 +08001024 const mbedtls_md_info_t *md_info;
Jerry Yu3391ac02022-02-16 11:21:37 +08001025 unsigned char verify_hash[ MBEDTLS_MD_MAX_SIZE ];
Jerry Yu3e536442022-02-15 11:05:59 +08001026 size_t verify_hash_len;
Jerry Yu8511f122022-01-29 10:01:04 +08001027
Jerry Yu0b7b1012022-02-23 12:23:05 +08001028 *out_len = 0;
1029
Jerry Yu3e536442022-02-15 11:05:59 +08001030 own_key = mbedtls_ssl_own_key( ssl );
1031 if( own_key == NULL )
Jerry Yu8511f122022-01-29 10:01:04 +08001032 {
Jerry Yu3e536442022-02-15 11:05:59 +08001033 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1034 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Jerry Yu8511f122022-01-29 10:01:04 +08001035 }
1036
Jerry Yu8511f122022-01-29 10:01:04 +08001037 ret = mbedtls_ssl_get_handshake_transcript( ssl,
1038 ssl->handshake->ciphersuite_info->mac,
1039 handshake_hash,
1040 sizeof( handshake_hash ),
1041 &handshake_hash_len );
1042 if( ret != 0 )
1043 return( ret );
1044
1045 MBEDTLS_SSL_DEBUG_BUF( 3, "handshake hash",
1046 handshake_hash,
1047 handshake_hash_len);
1048
Jerry Yu8511f122022-01-29 10:01:04 +08001049 ssl_tls13_create_verify_structure( handshake_hash, handshake_hash_len,
1050 verify_buffer, &verify_buffer_len,
1051 ssl->conf->endpoint );
1052
1053 /*
1054 * struct {
1055 * SignatureScheme algorithm;
1056 * opaque signature<0..2^16-1>;
1057 * } CertificateVerify;
1058 */
Jerry Yu8c338862022-03-23 13:34:04 +08001059 ret = ssl_tls13_get_sig_alg_from_pk( ssl, own_key, &algorithm );
Jerry Yue91a51a2022-03-22 21:42:50 +08001060 if( ret != 0 || ! mbedtls_ssl_sig_alg_is_received( ssl, algorithm ) )
Jerry Yu8511f122022-01-29 10:01:04 +08001061 {
Jerry Yud66409a2022-02-18 16:42:24 +08001062 MBEDTLS_SSL_DEBUG_MSG( 1,
Jerry Yu2124d052022-02-18 21:07:18 +08001063 ( "signature algorithm not in received or offered list." ) );
Jerry Yu67eced02022-02-25 13:37:36 +08001064
1065 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Signature algorithm is %s",
1066 mbedtls_ssl_sig_alg_to_str( algorithm ) ) );
1067
Jerry Yu71f36f12022-02-23 17:34:29 +08001068 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE,
Jerry Yud66409a2022-02-18 16:42:24 +08001069 MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
Jerry Yu3391ac02022-02-16 11:21:37 +08001070 return( MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
Jerry Yu8511f122022-01-29 10:01:04 +08001071 }
1072
Jerry Yu6c6f1022022-03-25 11:09:50 +08001073 if( mbedtls_ssl_tls13_get_pk_type_and_md_alg_from_sig_alg(
1074 algorithm, &pk_type, &md_alg ) != 0 )
Jerry Yu8c338862022-03-23 13:34:04 +08001075 {
Jerry Yuf8aa9a42022-03-23 20:40:28 +08001076 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Jerry Yu8c338862022-03-23 13:34:04 +08001077 }
1078
Jerry Yu3391ac02022-02-16 11:21:37 +08001079 /* Check there is space for the algorithm identifier (2 bytes) and the
1080 * signature length (2 bytes).
1081 */
1082 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 4 );
Jerry Yu3e536442022-02-15 11:05:59 +08001083 MBEDTLS_PUT_UINT16_BE( algorithm, p, 0 );
1084 p += 2;
Jerry Yu8511f122022-01-29 10:01:04 +08001085
1086 /* Hash verify buffer with indicated hash function */
1087 md_info = mbedtls_md_info_from_type( md_alg );
1088 if( md_info == NULL )
1089 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
1090
1091 ret = mbedtls_md( md_info, verify_buffer, verify_buffer_len, verify_hash );
1092 if( ret != 0 )
1093 return( ret );
1094
1095 verify_hash_len = mbedtls_md_get_size( md_info );
1096 MBEDTLS_SSL_DEBUG_BUF( 3, "verify hash", verify_hash, verify_hash_len );
1097
Jerry Yu8beb9e12022-03-12 16:23:53 +08001098 if( ( ret = mbedtls_pk_sign_ext( pk_type, own_key,
1099 md_alg, verify_hash, verify_hash_len,
Jerry Yu67eced02022-02-25 13:37:36 +08001100 p + 2, (size_t)( end - ( p + 2 ) ), &signature_len,
1101 ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 )
Jerry Yu8511f122022-01-29 10:01:04 +08001102 {
1103 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_pk_sign", ret );
1104 return( ret );
1105 }
Jerry Yu8511f122022-01-29 10:01:04 +08001106
Jerry Yu3e536442022-02-15 11:05:59 +08001107 MBEDTLS_PUT_UINT16_BE( signature_len, p, 0 );
1108 p += 2 + signature_len;
Jerry Yu8511f122022-01-29 10:01:04 +08001109
Jerry Yu3e536442022-02-15 11:05:59 +08001110 *out_len = (size_t)( p - buf );
1111
Jerry Yu8511f122022-01-29 10:01:04 +08001112 return( ret );
1113}
Jerry Yu8511f122022-01-29 10:01:04 +08001114
Jerry Yu3e536442022-02-15 11:05:59 +08001115int mbedtls_ssl_tls13_write_certificate_verify( mbedtls_ssl_context *ssl )
Jerry Yu8511f122022-01-29 10:01:04 +08001116{
1117 int ret = 0;
Jerry Yuca133a32022-02-15 14:22:05 +08001118 unsigned char *buf;
1119 size_t buf_len, msg_len;
1120
Jerry Yu8511f122022-01-29 10:01:04 +08001121 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write certificate verify" ) );
1122
Ronald Cron8f6d39a2022-03-10 18:56:50 +01001123 MBEDTLS_SSL_PROC_CHK( mbedtls_ssl_start_handshake_msg( ssl,
Jerry Yuca133a32022-02-15 14:22:05 +08001124 MBEDTLS_SSL_HS_CERTIFICATE_VERIFY, &buf, &buf_len ) );
Jerry Yu8511f122022-01-29 10:01:04 +08001125
Jerry Yuca133a32022-02-15 14:22:05 +08001126 MBEDTLS_SSL_PROC_CHK( ssl_tls13_write_certificate_verify_body(
1127 ssl, buf, buf + buf_len, &msg_len ) );
Jerry Yu8511f122022-01-29 10:01:04 +08001128
Ronald Cron8f6d39a2022-03-10 18:56:50 +01001129 mbedtls_ssl_add_hs_msg_to_checksum( ssl, MBEDTLS_SSL_HS_CERTIFICATE_VERIFY,
1130 buf, msg_len );
Jerry Yu8511f122022-01-29 10:01:04 +08001131
Ronald Cron8f6d39a2022-03-10 18:56:50 +01001132 MBEDTLS_SSL_PROC_CHK( mbedtls_ssl_finish_handshake_msg(
Jerry Yuca133a32022-02-15 14:22:05 +08001133 ssl, buf_len, msg_len ) );
Jerry Yu8511f122022-01-29 10:01:04 +08001134
1135cleanup:
1136
1137 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write certificate verify" ) );
1138 return( ret );
1139}
1140
Jerry Yu90f152d2022-01-29 22:12:42 +08001141#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
1142
Jerry Yu5cc35062022-01-28 16:16:08 +08001143/*
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001144 *
XiaokangQianc5c39d52021-11-09 11:55:10 +00001145 * STATE HANDLING: Incoming Finished message.
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001146 */
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001147/*
1148 * Implementation
1149 */
1150
XiaokangQianaaa0e192021-11-10 03:07:04 +00001151static int ssl_tls13_preprocess_finished_message( mbedtls_ssl_context *ssl )
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001152{
1153 int ret;
1154
XiaokangQianc5c39d52021-11-09 11:55:10 +00001155 ret = mbedtls_ssl_tls13_calculate_verify_data( ssl,
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001156 ssl->handshake->state_local.finished_in.digest,
1157 sizeof( ssl->handshake->state_local.finished_in.digest ),
1158 &ssl->handshake->state_local.finished_in.digest_len,
XiaokangQianc5c39d52021-11-09 11:55:10 +00001159 ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT ?
XiaokangQianc13f9352021-11-11 06:13:22 +00001160 MBEDTLS_SSL_IS_SERVER : MBEDTLS_SSL_IS_CLIENT );
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001161 if( ret != 0 )
1162 {
XiaokangQianc5c39d52021-11-09 11:55:10 +00001163 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_tls13_calculate_verify_data", ret );
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001164 return( ret );
1165 }
1166
1167 return( 0 );
1168}
1169
XiaokangQianc5c39d52021-11-09 11:55:10 +00001170static int ssl_tls13_parse_finished_message( mbedtls_ssl_context *ssl,
1171 const unsigned char *buf,
1172 const unsigned char *end )
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001173{
XiaokangQian33062842021-11-11 03:37:45 +00001174 /*
1175 * struct {
XiaokangQianc13f9352021-11-11 06:13:22 +00001176 * opaque verify_data[Hash.length];
XiaokangQian33062842021-11-11 03:37:45 +00001177 * } Finished;
1178 */
1179 const unsigned char *expected_verify_data =
1180 ssl->handshake->state_local.finished_in.digest;
1181 size_t expected_verify_data_len =
1182 ssl->handshake->state_local.finished_in.digest_len;
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001183 /* Structural validation */
XiaokangQian33062842021-11-11 03:37:45 +00001184 if( (size_t)( end - buf ) != expected_verify_data_len )
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001185 {
1186 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
1187
1188 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR,
XiaokangQianc13f9352021-11-11 06:13:22 +00001189 MBEDTLS_ERR_SSL_DECODE_ERROR );
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001190 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
1191 }
1192
XiaokangQianc5c39d52021-11-09 11:55:10 +00001193 MBEDTLS_SSL_DEBUG_BUF( 4, "verify_data (self-computed):",
XiaokangQian33062842021-11-11 03:37:45 +00001194 expected_verify_data,
1195 expected_verify_data_len );
XiaokangQianc5c39d52021-11-09 11:55:10 +00001196 MBEDTLS_SSL_DEBUG_BUF( 4, "verify_data (received message):", buf,
XiaokangQian33062842021-11-11 03:37:45 +00001197 expected_verify_data_len );
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001198
1199 /* Semantic validation */
Gabor Mezei685472b2021-11-24 11:17:36 +01001200 if( mbedtls_ct_memcmp( buf,
1201 expected_verify_data,
1202 expected_verify_data_len ) != 0 )
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001203 {
1204 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
1205
XiaokangQian33062842021-11-11 03:37:45 +00001206 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_DECRYPT_ERROR,
XiaokangQianc13f9352021-11-11 06:13:22 +00001207 MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001208 return( MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
1209 }
1210 return( 0 );
1211}
1212
XiaokangQianc5c39d52021-11-09 11:55:10 +00001213int mbedtls_ssl_tls13_process_finished_message( mbedtls_ssl_context *ssl )
1214{
XiaokangQian33062842021-11-11 03:37:45 +00001215 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
XiaokangQianc5c39d52021-11-09 11:55:10 +00001216 unsigned char *buf;
Xiaofei Baieef15042021-11-18 07:29:56 +00001217 size_t buf_len;
XiaokangQianc5c39d52021-11-09 11:55:10 +00001218
XiaokangQiand0aa3e92021-11-10 06:17:40 +00001219 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse finished message" ) );
XiaokangQianc5c39d52021-11-09 11:55:10 +00001220
1221 /* Preprocessing step: Compute handshake digest */
XiaokangQianaaa0e192021-11-10 03:07:04 +00001222 MBEDTLS_SSL_PROC_CHK( ssl_tls13_preprocess_finished_message( ssl ) );
XiaokangQianc5c39d52021-11-09 11:55:10 +00001223
Xiaofei Bai746f9482021-11-12 08:53:56 +00001224 MBEDTLS_SSL_PROC_CHK( mbedtls_ssl_tls13_fetch_handshake_msg( ssl,
XiaokangQianc5c39d52021-11-09 11:55:10 +00001225 MBEDTLS_SSL_HS_FINISHED,
Xiaofei Baieef15042021-11-18 07:29:56 +00001226 &buf, &buf_len ) );
1227 MBEDTLS_SSL_PROC_CHK( ssl_tls13_parse_finished_message( ssl, buf, buf + buf_len ) );
Ronald Cron8f6d39a2022-03-10 18:56:50 +01001228 mbedtls_ssl_add_hs_msg_to_checksum( ssl, MBEDTLS_SSL_HS_FINISHED,
1229 buf, buf_len );
XiaokangQianc5c39d52021-11-09 11:55:10 +00001230
1231cleanup:
1232
XiaokangQiand0aa3e92021-11-10 06:17:40 +00001233 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse finished message" ) );
XiaokangQianc5c39d52021-11-09 11:55:10 +00001234 return( ret );
1235}
1236
XiaokangQian74af2a82021-09-22 07:40:30 +00001237/*
1238 *
XiaokangQiancc90c942021-11-09 12:30:09 +00001239 * STATE HANDLING: Write and send Finished message.
XiaokangQian74af2a82021-09-22 07:40:30 +00001240 *
1241 */
XiaokangQian74af2a82021-09-22 07:40:30 +00001242/*
XiaokangQian35dc6252021-11-11 08:16:19 +00001243 * Implement
XiaokangQian74af2a82021-09-22 07:40:30 +00001244 */
1245
XiaokangQian8773aa02021-11-10 07:33:09 +00001246static int ssl_tls13_prepare_finished_message( mbedtls_ssl_context *ssl )
XiaokangQian74af2a82021-09-22 07:40:30 +00001247{
1248 int ret;
1249
1250 /* Compute transcript of handshake up to now. */
XiaokangQiancc90c942021-11-09 12:30:09 +00001251 ret = mbedtls_ssl_tls13_calculate_verify_data( ssl,
XiaokangQian74af2a82021-09-22 07:40:30 +00001252 ssl->handshake->state_local.finished_out.digest,
1253 sizeof( ssl->handshake->state_local.finished_out.digest ),
1254 &ssl->handshake->state_local.finished_out.digest_len,
1255 ssl->conf->endpoint );
1256
1257 if( ret != 0 )
1258 {
Jerry Yu7ca30542021-12-08 15:57:57 +08001259 MBEDTLS_SSL_DEBUG_RET( 1, "calculate_verify_data failed", ret );
XiaokangQian74af2a82021-09-22 07:40:30 +00001260 return( ret );
1261 }
1262
1263 return( 0 );
1264}
1265
XiaokangQian8773aa02021-11-10 07:33:09 +00001266static int ssl_tls13_write_finished_message_body( mbedtls_ssl_context *ssl,
XiaokangQian35dc6252021-11-11 08:16:19 +00001267 unsigned char *buf,
1268 unsigned char *end,
Xiaofei Baid25fab62021-12-02 06:36:27 +00001269 size_t *out_len )
XiaokangQian74af2a82021-09-22 07:40:30 +00001270{
XiaokangQian8773aa02021-11-10 07:33:09 +00001271 size_t verify_data_len = ssl->handshake->state_local.finished_out.digest_len;
XiaokangQian0fa66432021-11-15 03:33:57 +00001272 /*
1273 * struct {
1274 * opaque verify_data[Hash.length];
1275 * } Finished;
1276 */
XiaokangQian8773aa02021-11-10 07:33:09 +00001277 MBEDTLS_SSL_CHK_BUF_PTR( buf, end, verify_data_len );
XiaokangQian74af2a82021-09-22 07:40:30 +00001278
1279 memcpy( buf, ssl->handshake->state_local.finished_out.digest,
XiaokangQian8773aa02021-11-10 07:33:09 +00001280 verify_data_len );
XiaokangQian74af2a82021-09-22 07:40:30 +00001281
Xiaofei Baid25fab62021-12-02 06:36:27 +00001282 *out_len = verify_data_len;
XiaokangQian74af2a82021-09-22 07:40:30 +00001283 return( 0 );
1284}
XiaokangQianc5c39d52021-11-09 11:55:10 +00001285
XiaokangQian35dc6252021-11-11 08:16:19 +00001286/* Main entry point: orchestrates the other functions */
1287int mbedtls_ssl_tls13_write_finished_message( mbedtls_ssl_context *ssl )
1288{
1289 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1290 unsigned char *buf;
1291 size_t buf_len, msg_len;
1292
1293 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write finished message" ) );
1294
XiaokangQiandce82242021-11-15 06:01:26 +00001295 MBEDTLS_SSL_PROC_CHK( ssl_tls13_prepare_finished_message( ssl ) );
1296
Ronald Cron8f6d39a2022-03-10 18:56:50 +01001297 MBEDTLS_SSL_PROC_CHK( mbedtls_ssl_start_handshake_msg( ssl,
XiaokangQian35dc6252021-11-11 08:16:19 +00001298 MBEDTLS_SSL_HS_FINISHED, &buf, &buf_len ) );
1299
1300 MBEDTLS_SSL_PROC_CHK( ssl_tls13_write_finished_message_body(
1301 ssl, buf, buf + buf_len, &msg_len ) );
1302
Ronald Cron8f6d39a2022-03-10 18:56:50 +01001303 mbedtls_ssl_add_hs_msg_to_checksum( ssl, MBEDTLS_SSL_HS_FINISHED,
1304 buf, msg_len );
XiaokangQian35dc6252021-11-11 08:16:19 +00001305
Ronald Cron8f6d39a2022-03-10 18:56:50 +01001306 MBEDTLS_SSL_PROC_CHK( mbedtls_ssl_finish_handshake_msg(
1307 ssl, buf_len, msg_len ) );
XiaokangQian35dc6252021-11-11 08:16:19 +00001308cleanup:
1309
1310 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write finished message" ) );
1311 return( ret );
1312}
1313
Jerry Yu378254d2021-10-30 21:44:47 +08001314void mbedtls_ssl_tls13_handshake_wrapup( mbedtls_ssl_context *ssl )
1315{
1316
1317 MBEDTLS_SSL_DEBUG_MSG( 3, ( "=> handshake wrapup" ) );
1318
Jerry Yue8c1fca2022-05-18 14:48:56 +08001319 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Switch to application keys for inbound traffic" ) );
1320 mbedtls_ssl_set_inbound_transform ( ssl, ssl->transform_application );
1321
1322 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Switch to application keys for outbound traffic" ) );
1323 mbedtls_ssl_set_outbound_transform( ssl, ssl->transform_application );
1324
Jerry Yu378254d2021-10-30 21:44:47 +08001325 /*
Jerry Yucfe64f02021-11-15 13:54:06 +08001326 * Free the previous session and switch to the current one.
Jerry Yu378254d2021-10-30 21:44:47 +08001327 */
1328 if( ssl->session )
1329 {
Jerry Yu378254d2021-10-30 21:44:47 +08001330 mbedtls_ssl_session_free( ssl->session );
1331 mbedtls_free( ssl->session );
1332 }
1333 ssl->session = ssl->session_negotiate;
1334 ssl->session_negotiate = NULL;
1335
1336 MBEDTLS_SSL_DEBUG_MSG( 3, ( "<= handshake wrapup" ) );
1337}
1338
Ronald Cron49ad6192021-11-24 16:25:31 +01001339/*
1340 *
1341 * STATE HANDLING: Write ChangeCipherSpec
1342 *
1343 */
1344#if defined(MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE)
Ronald Cron49ad6192021-11-24 16:25:31 +01001345static int ssl_tls13_write_change_cipher_spec_body( mbedtls_ssl_context *ssl,
1346 unsigned char *buf,
1347 unsigned char *end,
1348 size_t *olen )
1349{
1350 ((void) ssl);
1351
1352 MBEDTLS_SSL_CHK_BUF_PTR( buf, end, 1 );
1353 buf[0] = 1;
1354 *olen = 1;
1355
1356 return( 0 );
1357}
1358
Ronald Cron49ad6192021-11-24 16:25:31 +01001359int mbedtls_ssl_tls13_write_change_cipher_spec( mbedtls_ssl_context *ssl )
1360{
1361 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1362
1363 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write change cipher spec" ) );
1364
Ronald Cron49ad6192021-11-24 16:25:31 +01001365 /* Write CCS message */
1366 MBEDTLS_SSL_PROC_CHK( ssl_tls13_write_change_cipher_spec_body(
1367 ssl, ssl->out_msg,
1368 ssl->out_msg + MBEDTLS_SSL_OUT_CONTENT_LEN,
1369 &ssl->out_msglen ) );
1370
1371 ssl->out_msgtype = MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC;
1372
Ronald Cron49ad6192021-11-24 16:25:31 +01001373 /* Dispatch message */
Ronald Cron66dbf912022-02-02 15:33:46 +01001374 MBEDTLS_SSL_PROC_CHK( mbedtls_ssl_write_record( ssl, 0 ) );
Ronald Cron49ad6192021-11-24 16:25:31 +01001375
1376cleanup:
1377
1378 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write change cipher spec" ) );
1379 return( ret );
1380}
1381
1382#endif /* MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE */
1383
XiaokangQian78b1fa72022-01-19 06:56:30 +00001384/* Reset SSL context and update hash for handling HRR.
1385 *
1386 * Replace Transcript-Hash(X) by
1387 * Transcript-Hash( message_hash ||
1388 * 00 00 Hash.length ||
1389 * X )
1390 * A few states of the handshake are preserved, including:
1391 * - session ID
1392 * - session ticket
1393 * - negotiated ciphersuite
1394 */
1395int mbedtls_ssl_reset_transcript_for_hrr( mbedtls_ssl_context *ssl )
1396{
1397 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1398 unsigned char hash_transcript[ MBEDTLS_MD_MAX_SIZE + 4 ];
XiaokangQian0ece9982022-01-24 08:56:23 +00001399 size_t hash_len;
XiaokangQian78b1fa72022-01-19 06:56:30 +00001400 const mbedtls_ssl_ciphersuite_t *ciphersuite_info;
1401 uint16_t cipher_suite = ssl->session_negotiate->ciphersuite;
1402 ciphersuite_info = mbedtls_ssl_ciphersuite_from_id( cipher_suite );
1403
1404 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Reset SSL session for HRR" ) );
1405
XiaokangQian0ece9982022-01-24 08:56:23 +00001406 ret = mbedtls_ssl_get_handshake_transcript( ssl, ciphersuite_info->mac,
1407 hash_transcript + 4,
1408 MBEDTLS_MD_MAX_SIZE,
1409 &hash_len );
1410 if( ret != 0 )
1411 {
1412 MBEDTLS_SSL_DEBUG_RET( 4, "mbedtls_ssl_get_handshake_transcript", ret );
1413 return( ret );
1414 }
1415
1416 hash_transcript[0] = MBEDTLS_SSL_HS_MESSAGE_HASH;
1417 hash_transcript[1] = 0;
1418 hash_transcript[2] = 0;
1419 hash_transcript[3] = (unsigned char) hash_len;
1420
1421 hash_len += 4;
1422
XiaokangQian78b1fa72022-01-19 06:56:30 +00001423 if( ciphersuite_info->mac == MBEDTLS_MD_SHA256 )
1424 {
1425#if defined(MBEDTLS_SHA256_C)
XiaokangQian78b1fa72022-01-19 06:56:30 +00001426 MBEDTLS_SSL_DEBUG_BUF( 4, "Truncated SHA-256 handshake transcript",
XiaokangQian0ece9982022-01-24 08:56:23 +00001427 hash_transcript, hash_len );
XiaokangQian78b1fa72022-01-19 06:56:30 +00001428
1429#if defined(MBEDTLS_USE_PSA_CRYPTO)
1430 psa_hash_abort( &ssl->handshake->fin_sha256_psa );
1431 psa_hash_setup( &ssl->handshake->fin_sha256_psa, PSA_ALG_SHA_256 );
1432#else
1433 mbedtls_sha256_starts( &ssl->handshake->fin_sha256, 0 );
1434#endif
XiaokangQian78b1fa72022-01-19 06:56:30 +00001435#endif /* MBEDTLS_SHA256_C */
1436 }
1437 else if( ciphersuite_info->mac == MBEDTLS_MD_SHA384 )
1438 {
1439#if defined(MBEDTLS_SHA384_C)
XiaokangQian78b1fa72022-01-19 06:56:30 +00001440 MBEDTLS_SSL_DEBUG_BUF( 4, "Truncated SHA-384 handshake transcript",
XiaokangQian0ece9982022-01-24 08:56:23 +00001441 hash_transcript, hash_len );
XiaokangQian78b1fa72022-01-19 06:56:30 +00001442
1443#if defined(MBEDTLS_USE_PSA_CRYPTO)
1444 psa_hash_abort( &ssl->handshake->fin_sha384_psa );
1445 psa_hash_setup( &ssl->handshake->fin_sha384_psa, PSA_ALG_SHA_384 );
1446#else
1447 mbedtls_sha512_starts( &ssl->handshake->fin_sha512, 1 );
1448#endif
XiaokangQian78b1fa72022-01-19 06:56:30 +00001449#endif /* MBEDTLS_SHA384_C */
1450 }
1451
XiaokangQian0ece9982022-01-24 08:56:23 +00001452#if defined(MBEDTLS_SHA256_C) || defined(MBEDTLS_SHA384_C)
1453 ssl->handshake->update_checksum( ssl, hash_transcript, hash_len );
1454#endif /* MBEDTLS_SHA256_C || MBEDTLS_SHA384_C */
Przemyslaw Stekiel4b3fff42022-02-14 16:39:52 +01001455
XiaokangQian78b1fa72022-01-19 06:56:30 +00001456 return( ret );
1457}
1458
XiaokangQian9b5d04b2022-04-10 10:20:43 +00001459#if defined(MBEDTLS_ECDH_C)
XiaokangQian7807f9f2022-02-15 10:04:37 +00001460
XiaokangQian9b5d04b2022-04-10 10:20:43 +00001461int mbedtls_ssl_tls13_read_public_ecdhe_share( mbedtls_ssl_context *ssl,
1462 const unsigned char *buf,
1463 size_t buf_len )
XiaokangQian7807f9f2022-02-15 10:04:37 +00001464{
XiaokangQian9b5d04b2022-04-10 10:20:43 +00001465 uint8_t *p = (uint8_t*)buf;
XiaokangQiancfd925f2022-04-14 07:10:37 +00001466 const uint8_t *end = buf + buf_len;
XiaokangQian9b5d04b2022-04-10 10:20:43 +00001467 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001468
XiaokangQian9b5d04b2022-04-10 10:20:43 +00001469 /* Get size of the TLS opaque key_exchange field of the KeyShareEntry struct. */
XiaokangQiancfd925f2022-04-14 07:10:37 +00001470 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 2 );
XiaokangQian9b5d04b2022-04-10 10:20:43 +00001471 uint16_t peerkey_len = MBEDTLS_GET_UINT16_BE( p, 0 );
1472 p += 2;
XiaokangQian3207a322022-02-23 03:15:27 +00001473
XiaokangQian9b5d04b2022-04-10 10:20:43 +00001474 /* Check if key size is consistent with given buffer length. */
XiaokangQiancfd925f2022-04-14 07:10:37 +00001475 MBEDTLS_SSL_CHK_BUF_PTR( p, end, peerkey_len );
XiaokangQian9b5d04b2022-04-10 10:20:43 +00001476
1477 /* Store peer's ECDH public key. */
1478 memcpy( handshake->ecdh_psa_peerkey, p, peerkey_len );
1479 handshake->ecdh_psa_peerkey_len = peerkey_len;
1480
XiaokangQian3207a322022-02-23 03:15:27 +00001481 return( 0 );
1482}
Jerry Yu89e103c2022-03-30 22:43:29 +08001483
1484int mbedtls_ssl_tls13_generate_and_write_ecdh_key_exchange(
1485 mbedtls_ssl_context *ssl,
1486 uint16_t named_group,
1487 unsigned char *buf,
1488 unsigned char *end,
1489 size_t *out_len )
1490{
1491 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
1492 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
1493 psa_key_attributes_t key_attributes;
1494 size_t own_pubkey_len;
1495 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
1496 size_t ecdh_bits = 0;
1497
1498 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Perform PSA-based ECDH computation." ) );
1499
1500 /* Convert EC group to PSA key type. */
1501 if( ( handshake->ecdh_psa_type =
1502 mbedtls_psa_parse_tls_ecc_group( named_group, &ecdh_bits ) ) == 0 )
1503 return( MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
1504
1505 ssl->handshake->ecdh_bits = ecdh_bits;
1506
1507 key_attributes = psa_key_attributes_init();
1508 psa_set_key_usage_flags( &key_attributes, PSA_KEY_USAGE_DERIVE );
1509 psa_set_key_algorithm( &key_attributes, PSA_ALG_ECDH );
1510 psa_set_key_type( &key_attributes, handshake->ecdh_psa_type );
1511 psa_set_key_bits( &key_attributes, handshake->ecdh_bits );
1512
1513 /* Generate ECDH private key. */
1514 status = psa_generate_key( &key_attributes,
1515 &handshake->ecdh_psa_privkey );
1516 if( status != PSA_SUCCESS )
1517 {
1518 ret = psa_ssl_status_to_mbedtls( status );
1519 MBEDTLS_SSL_DEBUG_RET( 1, "psa_generate_key", ret );
1520 return( ret );
1521
1522 }
1523
1524 /* Export the public part of the ECDH private key from PSA. */
1525 status = psa_export_public_key( handshake->ecdh_psa_privkey,
1526 buf, (size_t)( end - buf ),
1527 &own_pubkey_len );
1528 if( status != PSA_SUCCESS )
1529 {
1530 ret = psa_ssl_status_to_mbedtls( status );
1531 MBEDTLS_SSL_DEBUG_RET( 1, "psa_export_public_key", ret );
1532 return( ret );
1533
1534 }
1535
1536 *out_len = own_pubkey_len;
1537
1538 return( 0 );
1539}
XiaokangQian9b5d04b2022-04-10 10:20:43 +00001540#endif /* MBEDTLS_ECDH_C */
XiaokangQian7807f9f2022-02-15 10:04:37 +00001541
Jerry Yufb4b6472022-01-27 15:03:26 +08001542#endif /* MBEDTLS_SSL_TLS_C && MBEDTLS_SSL_PROTO_TLS1_3 */