blob: c7e00a98a85a35cd47a31ccdc205803ddf13dcc2 [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
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200159MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yu0b32c502021-10-28 13:41:59 +0800160static int ssl_tls13_parse_certificate_verify( mbedtls_ssl_context *ssl,
Jerry Yud0fc5852021-10-29 11:09:06 +0800161 const unsigned char *buf,
162 const unsigned char *end,
163 const unsigned char *verify_buffer,
164 size_t verify_buffer_len )
Jerry Yu30b071c2021-09-12 20:16:03 +0800165{
166 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
pespaceka1378102022-04-26 15:03:11 +0200167 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Jerry Yu30b071c2021-09-12 20:16:03 +0800168 const unsigned char *p = buf;
169 uint16_t algorithm;
Jerry Yu30b071c2021-09-12 20:16:03 +0800170 size_t signature_len;
171 mbedtls_pk_type_t sig_alg;
172 mbedtls_md_type_t md_alg;
pespaceka1378102022-04-26 15:03:11 +0200173 psa_algorithm_t hash_alg = PSA_ALG_NONE;
174 unsigned char verify_hash[PSA_HASH_MAX_SIZE];
Jerry Yu30b071c2021-09-12 20:16:03 +0800175 size_t verify_hash_len;
176
Xiaofei Baid25fab62021-12-02 06:36:27 +0000177 void const *options = NULL;
XiaokangQian82d34cc2021-11-03 08:51:56 +0000178#if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT)
Xiaofei Baid25fab62021-12-02 06:36:27 +0000179 mbedtls_pk_rsassa_pss_options rsassa_pss_options;
XiaokangQian82d34cc2021-11-03 08:51:56 +0000180#endif /* MBEDTLS_X509_RSASSA_PSS_SUPPORT */
181
Jerry Yu30b071c2021-09-12 20:16:03 +0800182 /*
183 * struct {
184 * SignatureScheme algorithm;
185 * opaque signature<0..2^16-1>;
186 * } CertificateVerify;
187 */
188 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 2 );
189 algorithm = MBEDTLS_GET_UINT16_BE( p, 0 );
190 p += 2;
191
192 /* RFC 8446 section 4.4.3
193 *
194 * If the CertificateVerify message is sent by a server, the signature algorithm
195 * MUST be one offered in the client's "signature_algorithms" extension unless
196 * no valid certificate chain can be produced without unsupported algorithms
197 *
198 * RFC 8446 section 4.4.2.2
199 *
200 * If the client cannot construct an acceptable chain using the provided
201 * certificates and decides to abort the handshake, then it MUST abort the handshake
202 * with an appropriate certificate-related alert (by default, "unsupported_certificate").
203 *
Jerry Yu6f87f252021-10-29 20:12:51 +0800204 * Check if algorithm is an offered signature algorithm.
Jerry Yu30b071c2021-09-12 20:16:03 +0800205 */
Jerry Yu24811fb2022-01-19 18:02:15 +0800206 if( ! mbedtls_ssl_sig_alg_is_offered( ssl, algorithm ) )
Jerry Yu30b071c2021-09-12 20:16:03 +0800207 {
Jerry Yu982d9e52021-10-14 15:59:37 +0800208 /* algorithm not in offered signature algorithms list */
209 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Received signature algorithm(%04x) is not "
210 "offered.",
211 ( unsigned int ) algorithm ) );
Jerry Yu6f87f252021-10-29 20:12:51 +0800212 goto error;
Jerry Yu30b071c2021-09-12 20:16:03 +0800213 }
214
Jerry Yu8c338862022-03-23 13:34:04 +0800215 if( mbedtls_ssl_tls13_get_pk_type_and_md_alg_from_sig_alg(
Jerry Yuf8aa9a42022-03-23 20:40:28 +0800216 algorithm, &sig_alg, &md_alg ) != 0 )
Jerry Yu30b071c2021-09-12 20:16:03 +0800217 {
Jerry Yu8c338862022-03-23 13:34:04 +0800218 goto error;
Jerry Yu30b071c2021-09-12 20:16:03 +0800219 }
220
pespaceka1378102022-04-26 15:03:11 +0200221 hash_alg = mbedtls_psa_translate_md( md_alg );
222 if( hash_alg == 0 )
223 {
224 goto error;
225 }
226
Jerry Yu30b071c2021-09-12 20:16:03 +0800227 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Certificate Verify: Signature algorithm ( %04x )",
228 ( unsigned int ) algorithm ) );
229
230 /*
231 * Check the certificate's key type matches the signature alg
232 */
233 if( !mbedtls_pk_can_do( &ssl->session_negotiate->peer_cert->pk, sig_alg ) )
234 {
235 MBEDTLS_SSL_DEBUG_MSG( 1, ( "signature algorithm doesn't match cert key" ) );
Jerry Yu6f87f252021-10-29 20:12:51 +0800236 goto error;
Jerry Yu30b071c2021-09-12 20:16:03 +0800237 }
238
239 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 2 );
240 signature_len = MBEDTLS_GET_UINT16_BE( p, 0 );
241 p += 2;
242 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, signature_len );
243
pespaceka1378102022-04-26 15:03:11 +0200244 status = psa_hash_compute( hash_alg,
245 verify_buffer,
246 verify_buffer_len,
247 verify_hash,
248 sizeof( verify_hash ),
249 &verify_hash_len );
250 if( status != PSA_SUCCESS )
Jerry Yu30b071c2021-09-12 20:16:03 +0800251 {
pespaceka1378102022-04-26 15:03:11 +0200252 MBEDTLS_SSL_DEBUG_RET( 1, "hash computation PSA error", status );
Jerry Yu6f87f252021-10-29 20:12:51 +0800253 goto error;
Jerry Yu133690c2021-10-25 14:01:13 +0800254 }
255
Jerry Yu30b071c2021-09-12 20:16:03 +0800256 MBEDTLS_SSL_DEBUG_BUF( 3, "verify hash", verify_hash, verify_hash_len );
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 *
XiaokangQian6b916b12022-04-25 07:29:34 +0000359 * STATE HANDLING: Incoming Certificate.
Xiaofei Bai947571e2021-09-29 09:12:03 +0000360 *
361 */
362
Xiaofei Bai947571e2021-09-29 09:12:03 +0000363#if defined(MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED)
364#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
365/*
366 * Structure of Certificate message:
367 *
368 * enum {
369 * X509(0),
370 * RawPublicKey(2),
371 * (255)
372 * } CertificateType;
373 *
374 * struct {
375 * select (certificate_type) {
376 * case RawPublicKey:
377 * * From RFC 7250 ASN.1_subjectPublicKeyInfo *
378 * opaque ASN1_subjectPublicKeyInfo<1..2^24-1>;
379 * case X509:
380 * opaque cert_data<1..2^24-1>;
381 * };
382 * Extension extensions<0..2^16-1>;
383 * } CertificateEntry;
384 *
385 * struct {
386 * opaque certificate_request_context<0..2^8-1>;
387 * CertificateEntry certificate_list<0..2^24-1>;
388 * } Certificate;
389 *
390 */
Xiaofei Bai79595ac2021-10-26 07:16:45 +0000391
392/* Parse certificate chain send by the server. */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200393MBEDTLS_CHECK_RETURN_CRITICAL
Xiaofei Bai947571e2021-09-29 09:12:03 +0000394static int ssl_tls13_parse_certificate( mbedtls_ssl_context *ssl,
395 const unsigned char *buf,
396 const unsigned char *end )
397{
398 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
399 size_t certificate_request_context_len = 0;
400 size_t certificate_list_len = 0;
401 const unsigned char *p = buf;
402 const unsigned char *certificate_list_end;
403
XiaokangQian63e713e2022-05-15 04:26:57 +0000404 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 4 );
Xiaofei Bai947571e2021-09-29 09:12:03 +0000405 certificate_request_context_len = p[0];
XiaokangQian63e713e2022-05-15 04:26:57 +0000406 certificate_list_len = MBEDTLS_GET_UINT24_BE( p, 1 );
407 p += 4;
Xiaofei Bai947571e2021-09-29 09:12:03 +0000408
409 /* In theory, the certificate list can be up to 2^24 Bytes, but we don't
410 * support anything beyond 2^16 = 64K.
411 */
Xiaofei Bai79595ac2021-10-26 07:16:45 +0000412 if( ( certificate_request_context_len != 0 ) ||
413 ( certificate_list_len >= 0x10000 ) )
Xiaofei Bai947571e2021-09-29 09:12:03 +0000414 {
415 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
416 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR,
417 MBEDTLS_ERR_SSL_DECODE_ERROR );
418 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
419 }
420
421 /* In case we tried to reuse a session but it failed */
422 if( ssl->session_negotiate->peer_cert != NULL )
423 {
424 mbedtls_x509_crt_free( ssl->session_negotiate->peer_cert );
425 mbedtls_free( ssl->session_negotiate->peer_cert );
426 }
427
XiaokangQianc3017f62022-05-13 05:55:41 +0000428 if( certificate_list_len == 0 )
429 {
430 ssl->session_negotiate->peer_cert = NULL;
431 ret = 0;
432 goto exit;
433 }
434
Xiaofei Bai947571e2021-09-29 09:12:03 +0000435 if( ( ssl->session_negotiate->peer_cert =
436 mbedtls_calloc( 1, sizeof( mbedtls_x509_crt ) ) ) == NULL )
437 {
438 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc( %" MBEDTLS_PRINTF_SIZET " bytes ) failed",
439 sizeof( mbedtls_x509_crt ) ) );
440 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR,
441 MBEDTLS_ERR_SSL_ALLOC_FAILED );
442 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
443 }
444
445 mbedtls_x509_crt_init( ssl->session_negotiate->peer_cert );
446
Xiaofei Bai947571e2021-09-29 09:12:03 +0000447 certificate_list_end = p + certificate_list_len;
Xiaofei Bai79595ac2021-10-26 07:16:45 +0000448 while( p < certificate_list_end )
Xiaofei Bai947571e2021-09-29 09:12:03 +0000449 {
450 size_t cert_data_len, extensions_len;
451
Xiaofei Bai79595ac2021-10-26 07:16:45 +0000452 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, certificate_list_end, 3 );
Xiaofei Baif93cbd22021-10-29 02:39:30 +0000453 cert_data_len = MBEDTLS_GET_UINT24_BE( p, 0 );
Xiaofei Bai947571e2021-09-29 09:12:03 +0000454 p += 3;
455
456 /* In theory, the CRT can be up to 2^24 Bytes, but we don't support
457 * anything beyond 2^16 = 64K. Otherwise as in the TLS 1.2 code,
458 * check that we have a minimum of 128 bytes of data, this is not
459 * clear why we need that though.
460 */
461 if( ( cert_data_len < 128 ) || ( cert_data_len >= 0x10000 ) )
Xiaofei Bai79595ac2021-10-26 07:16:45 +0000462 {
Xiaofei Bai947571e2021-09-29 09:12:03 +0000463 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad Certificate message" ) );
464 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR,
465 MBEDTLS_ERR_SSL_DECODE_ERROR );
466 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
467 }
468
Xiaofei Bai79595ac2021-10-26 07:16:45 +0000469 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, certificate_list_end, cert_data_len );
Xiaofei Bai947571e2021-09-29 09:12:03 +0000470 ret = mbedtls_x509_crt_parse_der( ssl->session_negotiate->peer_cert,
471 p, cert_data_len );
472
473 switch( ret )
474 {
475 case 0: /*ok*/
476 break;
477 case MBEDTLS_ERR_X509_UNKNOWN_SIG_ALG + MBEDTLS_ERR_OID_NOT_FOUND:
478 /* Ignore certificate with an unknown algorithm: maybe a
479 prior certificate was already trusted. */
480 break;
481
482 case MBEDTLS_ERR_X509_ALLOC_FAILED:
483 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR,
484 MBEDTLS_ERR_X509_ALLOC_FAILED );
485 MBEDTLS_SSL_DEBUG_RET( 1, " mbedtls_x509_crt_parse_der", ret );
486 return( ret );
487
488 case MBEDTLS_ERR_X509_UNKNOWN_VERSION:
489 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT,
490 MBEDTLS_ERR_X509_UNKNOWN_VERSION );
491 MBEDTLS_SSL_DEBUG_RET( 1, " mbedtls_x509_crt_parse_der", ret );
492 return( ret );
493
494 default:
495 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_BAD_CERT,
496 ret );
497 MBEDTLS_SSL_DEBUG_RET( 1, " mbedtls_x509_crt_parse_der", ret );
498 return( ret );
499 }
500
501 p += cert_data_len;
502
503 /* Certificate extensions length */
504 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, certificate_list_end, 2 );
505 extensions_len = MBEDTLS_GET_UINT16_BE( p, 0 );
506 p += 2;
Xiaofei Bai79595ac2021-10-26 07:16:45 +0000507 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, certificate_list_end, extensions_len );
Xiaofei Bai947571e2021-09-29 09:12:03 +0000508 p += extensions_len;
509 }
510
XiaokangQian63e713e2022-05-15 04:26:57 +0000511exit:
Xiaofei Bai947571e2021-09-29 09:12:03 +0000512 /* Check that all the message is consumed. */
513 if( p != end )
514 {
515 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad Certificate message" ) );
516 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR, \
517 MBEDTLS_ERR_SSL_DECODE_ERROR );
518 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
519 }
520
521 MBEDTLS_SSL_DEBUG_CRT( 3, "peer certificate", ssl->session_negotiate->peer_cert );
522
523 return( ret );
524}
525#else
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200526MBEDTLS_CHECK_RETURN_CRITICAL
Xiaofei Bai947571e2021-09-29 09:12:03 +0000527static int ssl_tls13_parse_certificate( mbedtls_ssl_context *ssl,
528 const unsigned char *buf,
529 const unsigned char *end )
530{
531 ((void) ssl);
532 ((void) buf);
533 ((void) end);
534 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
535}
536#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
537#endif /* MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED */
538
539#if defined(MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED)
540#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Xiaofei Bai79595ac2021-10-26 07:16:45 +0000541/* Validate certificate chain sent by the server. */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200542MBEDTLS_CHECK_RETURN_CRITICAL
Xiaofei Bai947571e2021-09-29 09:12:03 +0000543static int ssl_tls13_validate_certificate( mbedtls_ssl_context *ssl )
544{
545 int ret = 0;
XiaokangQian989f06d2022-05-17 01:50:15 +0000546 int authmode = MBEDTLS_SSL_VERIFY_REQUIRED;
Xiaofei Bai947571e2021-09-29 09:12:03 +0000547 mbedtls_x509_crt *ca_chain;
548 mbedtls_x509_crl *ca_crl;
Ronald Cron30c5a252022-06-16 19:31:06 +0200549 const char *ext_oid;
550 size_t ext_len;
Xiaofei Baiff456022021-10-28 06:50:17 +0000551 uint32_t verify_result = 0;
Xiaofei Bai947571e2021-09-29 09:12:03 +0000552
XiaokangQian6b916b12022-04-25 07:29:34 +0000553 /* If SNI was used, overwrite authentication mode
554 * from the configuration. */
XiaokangQian989f06d2022-05-17 01:50:15 +0000555#if defined(MBEDTLS_SSL_SRV_C)
556 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
XiaokangQiane7a5da52022-05-30 00:59:29 +0000557 authmode = ssl->conf->authmode;
XiaokangQian6b916b12022-04-25 07:29:34 +0000558#endif
559
560 /*
XiaokangQian989f06d2022-05-17 01:50:15 +0000561 * If the peer hasn't sent a certificate ( i.e. it sent
XiaokangQian6b916b12022-04-25 07:29:34 +0000562 * an empty certificate chain ), this is reflected in the peer CRT
563 * structure being unset.
564 * Check for that and handle it depending on the
XiaokangQian989f06d2022-05-17 01:50:15 +0000565 * authentication mode.
XiaokangQian6b916b12022-04-25 07:29:34 +0000566 */
XiaokangQian63e713e2022-05-15 04:26:57 +0000567 if( ssl->session_negotiate->peer_cert == NULL )
XiaokangQian6b916b12022-04-25 07:29:34 +0000568 {
XiaokangQian989f06d2022-05-17 01:50:15 +0000569 MBEDTLS_SSL_DEBUG_MSG( 1, ( "peer has not sent a certificate" ) );
570
XiaokangQian63e713e2022-05-15 04:26:57 +0000571#if defined(MBEDTLS_SSL_SRV_C)
572 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
573 {
XiaokangQian63e713e2022-05-15 04:26:57 +0000574 /* The client was asked for a certificate but didn't send
575 * one. The client should know what's going on, so we
576 * don't send an alert.
577 */
578 ssl->session_negotiate->verify_result = MBEDTLS_X509_BADCERT_MISSING;
579 if( authmode == MBEDTLS_SSL_VERIFY_OPTIONAL )
580 return( 0 );
581 else
XiaokangQian989f06d2022-05-17 01:50:15 +0000582 {
583 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_NO_CERT,
XiaokangQianaca90482022-05-19 07:19:31 +0000584 MBEDTLS_ERR_SSL_NO_CLIENT_CERTIFICATE );
585 return( MBEDTLS_ERR_SSL_NO_CLIENT_CERTIFICATE );
XiaokangQian989f06d2022-05-17 01:50:15 +0000586 }
XiaokangQian63e713e2022-05-15 04:26:57 +0000587 }
XiaokangQian6b916b12022-04-25 07:29:34 +0000588#endif /* MBEDTLS_SSL_SRV_C */
589
XiaokangQianc3017f62022-05-13 05:55:41 +0000590#if defined(MBEDTLS_SSL_CLI_C)
XiaokangQian63e713e2022-05-15 04:26:57 +0000591 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
592 {
593 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_NO_CERT,
594 MBEDTLS_ERR_SSL_FATAL_ALERT_MESSAGE );
595 return( MBEDTLS_ERR_SSL_FATAL_ALERT_MESSAGE );
596 }
XiaokangQianc3017f62022-05-13 05:55:41 +0000597#endif /* MBEDTLS_SSL_CLI_C */
XiaokangQian63e713e2022-05-15 04:26:57 +0000598 }
XiaokangQian6b916b12022-04-25 07:29:34 +0000599
Xiaofei Bai947571e2021-09-29 09:12:03 +0000600#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
601 if( ssl->handshake->sni_ca_chain != NULL )
602 {
603 ca_chain = ssl->handshake->sni_ca_chain;
604 ca_crl = ssl->handshake->sni_ca_crl;
605 }
606 else
607#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
608 {
609 ca_chain = ssl->conf->ca_chain;
610 ca_crl = ssl->conf->ca_crl;
611 }
612
613 /*
614 * Main check: verify certificate
615 */
616 ret = mbedtls_x509_crt_verify_with_profile(
617 ssl->session_negotiate->peer_cert,
618 ca_chain, ca_crl,
619 ssl->conf->cert_profile,
620 ssl->hostname,
Xiaofei Baiff456022021-10-28 06:50:17 +0000621 &verify_result,
Xiaofei Bai947571e2021-09-29 09:12:03 +0000622 ssl->conf->f_vrfy, ssl->conf->p_vrfy );
623
624 if( ret != 0 )
625 {
626 MBEDTLS_SSL_DEBUG_RET( 1, "x509_verify_cert", ret );
627 }
628
629 /*
630 * Secondary checks: always done, but change 'ret' only if it was 0
631 */
Ronald Cron30c5a252022-06-16 19:31:06 +0200632 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Xiaofei Bai947571e2021-09-29 09:12:03 +0000633 {
Ronald Cron30c5a252022-06-16 19:31:06 +0200634 ext_oid = MBEDTLS_OID_SERVER_AUTH;
635 ext_len = MBEDTLS_OID_SIZE( MBEDTLS_OID_SERVER_AUTH );
636 }
637 else
638 {
639 ext_oid = MBEDTLS_OID_CLIENT_AUTH;
640 ext_len = MBEDTLS_OID_SIZE( MBEDTLS_OID_CLIENT_AUTH );
641 }
642
643 if( ( mbedtls_x509_crt_check_key_usage(
644 ssl->session_negotiate->peer_cert,
645 MBEDTLS_X509_KU_DIGITAL_SIGNATURE ) != 0 ) ||
646 ( mbedtls_x509_crt_check_extended_key_usage(
647 ssl->session_negotiate->peer_cert,
648 ext_oid, ext_len ) != 0 ) )
649 {
650 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate (usage extensions)" ) );
Xiaofei Bai947571e2021-09-29 09:12:03 +0000651 if( ret == 0 )
652 ret = MBEDTLS_ERR_SSL_BAD_CERTIFICATE;
653 }
654
XiaokangQian6b916b12022-04-25 07:29:34 +0000655 /* mbedtls_x509_crt_verify_with_profile is supposed to report a
656 * verification failure through MBEDTLS_ERR_X509_CERT_VERIFY_FAILED,
657 * with details encoded in the verification flags. All other kinds
658 * of error codes, including those from the user provided f_vrfy
659 * functions, are treated as fatal and lead to a failure of
660 * ssl_tls13_parse_certificate even if verification was optional. */
661 if( authmode == MBEDTLS_SSL_VERIFY_OPTIONAL &&
662 ( ret == MBEDTLS_ERR_X509_CERT_VERIFY_FAILED ||
663 ret == MBEDTLS_ERR_SSL_BAD_CERTIFICATE ) )
664 {
665 ret = 0;
666 }
Xiaofei Bai947571e2021-09-29 09:12:03 +0000667
XiaokangQian6b916b12022-04-25 07:29:34 +0000668 if( ca_chain == NULL && authmode == MBEDTLS_SSL_VERIFY_REQUIRED )
Xiaofei Bai947571e2021-09-29 09:12:03 +0000669 {
670 MBEDTLS_SSL_DEBUG_MSG( 1, ( "got no CA chain" ) );
671 ret = MBEDTLS_ERR_SSL_CA_CHAIN_REQUIRED;
672 }
673
674 if( ret != 0 )
675 {
676 /* The certificate may have been rejected for several reasons.
677 Pick one and send the corresponding alert. Which alert to send
678 may be a subject of debate in some cases. */
Xiaofei Baiff456022021-10-28 06:50:17 +0000679 if( verify_result & MBEDTLS_X509_BADCERT_OTHER )
Xiaofei Bai947571e2021-09-29 09:12:03 +0000680 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED, ret );
Xiaofei Baiff456022021-10-28 06:50:17 +0000681 else if( verify_result & MBEDTLS_X509_BADCERT_CN_MISMATCH )
Xiaofei Bai947571e2021-09-29 09:12:03 +0000682 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_BAD_CERT, ret );
Xiaofei Baif93cbd22021-10-29 02:39:30 +0000683 else if( verify_result & ( MBEDTLS_X509_BADCERT_KEY_USAGE |
684 MBEDTLS_X509_BADCERT_EXT_KEY_USAGE |
685 MBEDTLS_X509_BADCERT_NS_CERT_TYPE |
686 MBEDTLS_X509_BADCERT_BAD_PK |
687 MBEDTLS_X509_BADCERT_BAD_KEY ) )
Xiaofei Bai947571e2021-09-29 09:12:03 +0000688 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT, ret );
Xiaofei Baiff456022021-10-28 06:50:17 +0000689 else if( verify_result & MBEDTLS_X509_BADCERT_EXPIRED )
Xiaofei Bai947571e2021-09-29 09:12:03 +0000690 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_CERT_EXPIRED, ret );
Xiaofei Baiff456022021-10-28 06:50:17 +0000691 else if( verify_result & MBEDTLS_X509_BADCERT_REVOKED )
Xiaofei Bai947571e2021-09-29 09:12:03 +0000692 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_CERT_REVOKED, ret );
Xiaofei Baiff456022021-10-28 06:50:17 +0000693 else if( verify_result & MBEDTLS_X509_BADCERT_NOT_TRUSTED )
Xiaofei Bai947571e2021-09-29 09:12:03 +0000694 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_UNKNOWN_CA, ret );
695 else
696 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_CERT_UNKNOWN, ret );
697 }
698
699#if defined(MBEDTLS_DEBUG_C)
Xiaofei Baiff456022021-10-28 06:50:17 +0000700 if( verify_result != 0 )
Xiaofei Bai947571e2021-09-29 09:12:03 +0000701 {
702 MBEDTLS_SSL_DEBUG_MSG( 3, ( "! Certificate verification flags %08x",
Jerry Yu83bb1312021-10-28 22:16:33 +0800703 (unsigned int) verify_result ) );
Xiaofei Bai947571e2021-09-29 09:12:03 +0000704 }
705 else
706 {
707 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Certificate verification flags clear" ) );
708 }
709#endif /* MBEDTLS_DEBUG_C */
710
Xiaofei Baiff456022021-10-28 06:50:17 +0000711 ssl->session_negotiate->verify_result = verify_result;
Xiaofei Bai947571e2021-09-29 09:12:03 +0000712 return( ret );
713}
714#else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200715MBEDTLS_CHECK_RETURN_CRITICAL
Xiaofei Bai947571e2021-09-29 09:12:03 +0000716static int ssl_tls13_validate_certificate( mbedtls_ssl_context *ssl )
717{
718 ((void) ssl);
719 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
720}
721#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
722#endif /* MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED */
723
Xiaofei Bai79595ac2021-10-26 07:16:45 +0000724int mbedtls_ssl_tls13_process_certificate( mbedtls_ssl_context *ssl )
Xiaofei Bai947571e2021-09-29 09:12:03 +0000725{
Xiaofei Bai79595ac2021-10-26 07:16:45 +0000726 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
727 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) );
728
729#if defined(MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED)
XiaokangQianc3017f62022-05-13 05:55:41 +0000730 unsigned char *buf;
731 size_t buf_len;
Xiaofei Bai79595ac2021-10-26 07:16:45 +0000732
XiaokangQianc3017f62022-05-13 05:55:41 +0000733 MBEDTLS_SSL_PROC_CHK( mbedtls_ssl_tls13_fetch_handshake_msg(
734 ssl, MBEDTLS_SSL_HS_CERTIFICATE,
735 &buf, &buf_len ) );
Xiaofei Bai79595ac2021-10-26 07:16:45 +0000736
XiaokangQianc3017f62022-05-13 05:55:41 +0000737 /* Parse the certificate chain sent by the peer. */
738 MBEDTLS_SSL_PROC_CHK( ssl_tls13_parse_certificate( ssl, buf,
739 buf + buf_len ) );
740 /* Validate the certificate chain and set the verification results. */
741 MBEDTLS_SSL_PROC_CHK( ssl_tls13_validate_certificate( ssl ) );
Xiaofei Bai79595ac2021-10-26 07:16:45 +0000742
XiaokangQianc3017f62022-05-13 05:55:41 +0000743 mbedtls_ssl_add_hs_msg_to_checksum( ssl, MBEDTLS_SSL_HS_CERTIFICATE,
744 buf, buf_len );
Xiaofei Bai79595ac2021-10-26 07:16:45 +0000745
Xiaofei Bai79595ac2021-10-26 07:16:45 +0000746cleanup:
XiaokangQianaca90482022-05-19 07:19:31 +0000747#endif /* MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED */
Xiaofei Bai79595ac2021-10-26 07:16:45 +0000748
749 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse certificate" ) );
750 return( ret );
Xiaofei Bai947571e2021-09-29 09:12:03 +0000751}
Jerry Yu90f152d2022-01-29 22:12:42 +0800752#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Jerry Yu7399d0d2022-01-30 17:54:19 +0800753/*
754 * enum {
755 * X509(0),
756 * RawPublicKey(2),
757 * (255)
758 * } CertificateType;
759 *
760 * struct {
761 * select (certificate_type) {
762 * case RawPublicKey:
763 * // From RFC 7250 ASN.1_subjectPublicKeyInfo
764 * opaque ASN1_subjectPublicKeyInfo<1..2^24-1>;
765 *
766 * case X509:
767 * opaque cert_data<1..2^24-1>;
768 * };
769 * Extension extensions<0..2^16-1>;
770 * } CertificateEntry;
771 *
772 * struct {
773 * opaque certificate_request_context<0..2^8-1>;
774 * CertificateEntry certificate_list<0..2^24-1>;
775 * } Certificate;
776 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200777MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yu7399d0d2022-01-30 17:54:19 +0800778static int ssl_tls13_write_certificate_body( mbedtls_ssl_context *ssl,
Jerry Yu3e536442022-02-15 11:05:59 +0800779 unsigned char *buf,
Jerry Yu7399d0d2022-01-30 17:54:19 +0800780 unsigned char *end,
Jerry Yu3e536442022-02-15 11:05:59 +0800781 size_t *out_len )
Jerry Yu5cc35062022-01-28 16:16:08 +0800782{
Jerry Yu5cc35062022-01-28 16:16:08 +0800783 const mbedtls_x509_crt *crt = mbedtls_ssl_own_cert( ssl );
Jerry Yu3e536442022-02-15 11:05:59 +0800784 unsigned char *p = buf;
Jerry Yuc8d8d4e2022-02-18 12:10:03 +0800785 unsigned char *certificate_request_context =
786 ssl->handshake->certificate_request_context;
787 unsigned char certificate_request_context_len =
788 ssl->handshake->certificate_request_context_len;
789 unsigned char *p_certificate_list_len;
Jerry Yu5cc35062022-01-28 16:16:08 +0800790
Jerry Yu5cc35062022-01-28 16:16:08 +0800791
Jerry Yu3391ac02022-02-16 11:21:37 +0800792 /* ...
793 * opaque certificate_request_context<0..2^8-1>;
794 * ...
795 */
Jerry Yuc8d8d4e2022-02-18 12:10:03 +0800796 MBEDTLS_SSL_CHK_BUF_PTR( p, end, certificate_request_context_len + 1 );
797 *p++ = certificate_request_context_len;
798 if( certificate_request_context_len > 0 )
Jerry Yu537530d2022-02-15 14:00:57 +0800799 {
Jerry Yuc8d8d4e2022-02-18 12:10:03 +0800800 memcpy( p, certificate_request_context, certificate_request_context_len );
801 p += certificate_request_context_len;
Jerry Yu537530d2022-02-15 14:00:57 +0800802 }
803
Jerry Yu3391ac02022-02-16 11:21:37 +0800804 /* ...
805 * CertificateEntry certificate_list<0..2^24-1>;
806 * ...
807 */
Jerry Yu3e536442022-02-15 11:05:59 +0800808 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 3 );
Jerry Yuc8d8d4e2022-02-18 12:10:03 +0800809 p_certificate_list_len = p;
Jerry Yu3e536442022-02-15 11:05:59 +0800810 p += 3;
811
Jerry Yu7399d0d2022-01-30 17:54:19 +0800812 MBEDTLS_SSL_DEBUG_CRT( 3, "own certificate", crt );
Jerry Yu5cc35062022-01-28 16:16:08 +0800813
Jerry Yuc8d8d4e2022-02-18 12:10:03 +0800814 while( crt != NULL )
Jerry Yu5cc35062022-01-28 16:16:08 +0800815 {
Jerry Yu7399d0d2022-01-30 17:54:19 +0800816 size_t cert_data_len = crt->raw.len;
Jerry Yu5cc35062022-01-28 16:16:08 +0800817
Jerry Yu7399d0d2022-01-30 17:54:19 +0800818 MBEDTLS_SSL_CHK_BUF_PTR( p, end, cert_data_len + 3 + 2 );
819 MBEDTLS_PUT_UINT24_BE( cert_data_len, p, 0 );
820 p += 3;
Jerry Yu5cc35062022-01-28 16:16:08 +0800821
Jerry Yu7399d0d2022-01-30 17:54:19 +0800822 memcpy( p, crt->raw.p, cert_data_len );
823 p += cert_data_len;
824 crt = crt->next;
Jerry Yu5cc35062022-01-28 16:16:08 +0800825
826 /* Currently, we don't have any certificate extensions defined.
827 * Hence, we are sending an empty extension with length zero.
828 */
Jerry Yu7399d0d2022-01-30 17:54:19 +0800829 MBEDTLS_PUT_UINT24_BE( 0, p, 0 );
830 p += 2;
Jerry Yu5cc35062022-01-28 16:16:08 +0800831 }
Jerry Yu5cc35062022-01-28 16:16:08 +0800832
Jerry Yuc8d8d4e2022-02-18 12:10:03 +0800833 MBEDTLS_PUT_UINT24_BE( p - p_certificate_list_len - 3,
834 p_certificate_list_len, 0 );
Jerry Yu7399d0d2022-01-30 17:54:19 +0800835
Jerry Yu3e536442022-02-15 11:05:59 +0800836 *out_len = p - buf;
Jerry Yu5cc35062022-01-28 16:16:08 +0800837
838 return( 0 );
839}
Jerry Yu5cc35062022-01-28 16:16:08 +0800840
Jerry Yu3e536442022-02-15 11:05:59 +0800841int mbedtls_ssl_tls13_write_certificate( mbedtls_ssl_context *ssl )
Jerry Yu5cc35062022-01-28 16:16:08 +0800842{
843 int ret;
Ronald Cron5bb8fc82022-03-09 07:00:13 +0100844 unsigned char *buf;
845 size_t buf_len, msg_len;
846
Jerry Yu5cc35062022-01-28 16:16:08 +0800847 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write certificate" ) );
848
Ronald Cron8f6d39a2022-03-10 18:56:50 +0100849 MBEDTLS_SSL_PROC_CHK( mbedtls_ssl_start_handshake_msg( ssl,
Ronald Cron5bb8fc82022-03-09 07:00:13 +0100850 MBEDTLS_SSL_HS_CERTIFICATE, &buf, &buf_len ) );
Jerry Yu5cc35062022-01-28 16:16:08 +0800851
Ronald Cron5bb8fc82022-03-09 07:00:13 +0100852 MBEDTLS_SSL_PROC_CHK( ssl_tls13_write_certificate_body( ssl,
853 buf,
854 buf + buf_len,
855 &msg_len ) );
Jerry Yu5cc35062022-01-28 16:16:08 +0800856
Ronald Cron8f6d39a2022-03-10 18:56:50 +0100857 mbedtls_ssl_add_hs_msg_to_checksum( ssl, MBEDTLS_SSL_HS_CERTIFICATE,
858 buf, msg_len );
Jerry Yu5cc35062022-01-28 16:16:08 +0800859
Ronald Cron8f6d39a2022-03-10 18:56:50 +0100860 MBEDTLS_SSL_PROC_CHK( mbedtls_ssl_finish_handshake_msg(
Ronald Cron5bb8fc82022-03-09 07:00:13 +0100861 ssl, buf_len, msg_len ) );
Jerry Yu5cc35062022-01-28 16:16:08 +0800862cleanup:
863
864 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write certificate" ) );
865 return( ret );
866}
867
Jerry Yu3e536442022-02-15 11:05:59 +0800868/*
869 * STATE HANDLING: Output Certificate Verify
870 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200871MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yue91a51a2022-03-22 21:42:50 +0800872static int ssl_tls13_get_sig_alg_from_pk( mbedtls_ssl_context *ssl,
873 mbedtls_pk_context *own_key,
Jerry Yu8c338862022-03-23 13:34:04 +0800874 uint16_t *algorithm )
Jerry Yu67eced02022-02-25 13:37:36 +0800875{
876 mbedtls_pk_type_t sig = mbedtls_ssl_sig_from_pk( own_key );
877 /* Determine the size of the key */
878 size_t own_key_size = mbedtls_pk_get_bitlen( own_key );
Jerry Yue91a51a2022-03-22 21:42:50 +0800879 *algorithm = MBEDTLS_TLS1_3_SIG_NONE;
Jerry Yu67eced02022-02-25 13:37:36 +0800880 ((void) own_key_size);
881
882 switch( sig )
883 {
884#if defined(MBEDTLS_ECDSA_C)
885 case MBEDTLS_SSL_SIG_ECDSA:
886 switch( own_key_size )
887 {
888 case 256:
Jerry Yue91a51a2022-03-22 21:42:50 +0800889 *algorithm = MBEDTLS_TLS1_3_SIG_ECDSA_SECP256R1_SHA256;
Jerry Yue91a51a2022-03-22 21:42:50 +0800890 return( 0 );
Jerry Yu67eced02022-02-25 13:37:36 +0800891 case 384:
Jerry Yue91a51a2022-03-22 21:42:50 +0800892 *algorithm = MBEDTLS_TLS1_3_SIG_ECDSA_SECP384R1_SHA384;
Jerry Yue91a51a2022-03-22 21:42:50 +0800893 return( 0 );
Jerry Yu67eced02022-02-25 13:37:36 +0800894 case 521:
Jerry Yue91a51a2022-03-22 21:42:50 +0800895 *algorithm = MBEDTLS_TLS1_3_SIG_ECDSA_SECP521R1_SHA512;
Jerry Yue91a51a2022-03-22 21:42:50 +0800896 return( 0 );
Jerry Yu67eced02022-02-25 13:37:36 +0800897 default:
898 MBEDTLS_SSL_DEBUG_MSG( 3,
899 ( "unknown key size: %"
900 MBEDTLS_PRINTF_SIZET " bits",
901 own_key_size ) );
902 break;
903 }
904 break;
905#endif /* MBEDTLS_ECDSA_C */
906
Jerry Yucef3f332022-03-22 23:00:13 +0800907#if defined(MBEDTLS_RSA_C)
Jerry Yu67eced02022-02-25 13:37:36 +0800908 case MBEDTLS_SSL_SIG_RSA:
Jerry Yucef3f332022-03-22 23:00:13 +0800909#if defined(MBEDTLS_PKCS1_V21)
910#if defined(MBEDTLS_SHA256_C)
Jerry Yu67eced02022-02-25 13:37:36 +0800911 if( own_key_size <= 2048 &&
912 mbedtls_ssl_sig_alg_is_received( ssl,
913 MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA256 ) )
914 {
Jerry Yue91a51a2022-03-22 21:42:50 +0800915 *algorithm = MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA256;
Jerry Yue91a51a2022-03-22 21:42:50 +0800916 return( 0 );
Jerry Yu67eced02022-02-25 13:37:36 +0800917 }
Jerry Yucef3f332022-03-22 23:00:13 +0800918 else
919#endif /* MBEDTLS_SHA256_C */
920#if defined(MBEDTLS_SHA384_C)
921 if( own_key_size <= 3072 &&
922 mbedtls_ssl_sig_alg_is_received( ssl,
Jerry Yu67eced02022-02-25 13:37:36 +0800923 MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA384 ) )
924 {
Jerry Yue91a51a2022-03-22 21:42:50 +0800925 *algorithm = MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA384;
Jerry Yue91a51a2022-03-22 21:42:50 +0800926 return( 0 );
Jerry Yu67eced02022-02-25 13:37:36 +0800927 }
Jerry Yucef3f332022-03-22 23:00:13 +0800928 else
929#endif /* MBEDTLS_SHA384_C */
930#if defined(MBEDTLS_SHA512_C)
931 if( own_key_size <= 4096 &&
932 mbedtls_ssl_sig_alg_is_received( ssl,
Jerry Yu67eced02022-02-25 13:37:36 +0800933 MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA512 ) )
934 {
Jerry Yue91a51a2022-03-22 21:42:50 +0800935 *algorithm = MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA512;
Jerry Yue91a51a2022-03-22 21:42:50 +0800936 return( 0 );
Jerry Yu67eced02022-02-25 13:37:36 +0800937 }
Jerry Yucef3f332022-03-22 23:00:13 +0800938 else
939#endif /* MBEDTLS_SHA512_C */
940#endif /* MBEDTLS_PKCS1_V21 */
941#if defined(MBEDTLS_PKCS1_V15)
942#if defined(MBEDTLS_SHA256_C)
943 if( own_key_size <= 2048 &&
944 mbedtls_ssl_sig_alg_is_received( ssl,
945 MBEDTLS_TLS1_3_SIG_RSA_PKCS1_SHA256 ) )
946 {
947 *algorithm = MBEDTLS_TLS1_3_SIG_RSA_PKCS1_SHA256;
Jerry Yucef3f332022-03-22 23:00:13 +0800948 return( 0 );
949 }
950 else
951#endif /* MBEDTLS_SHA256_C */
952#if defined(MBEDTLS_SHA384_C)
953 if( own_key_size <= 3072 &&
954 mbedtls_ssl_sig_alg_is_received( ssl,
955 MBEDTLS_TLS1_3_SIG_RSA_PKCS1_SHA384 ) )
956 {
957 *algorithm = MBEDTLS_TLS1_3_SIG_RSA_PKCS1_SHA384;
Jerry Yucef3f332022-03-22 23:00:13 +0800958 return( 0 );
959 }
960 else
961#endif /* MBEDTLS_SHA384_C */
962#if defined(MBEDTLS_SHA512_C)
963 if( own_key_size <= 4096 &&
964 mbedtls_ssl_sig_alg_is_received( ssl,
965 MBEDTLS_TLS1_3_SIG_RSA_PKCS1_SHA512 ) )
966 {
967 *algorithm = MBEDTLS_TLS1_3_SIG_RSA_PKCS1_SHA512;
Jerry Yucef3f332022-03-22 23:00:13 +0800968 return( 0 );
969 }
970 else
971#endif /* MBEDTLS_SHA512_C */
972#endif /* MBEDTLS_PKCS1_V15 */
973 {
974 MBEDTLS_SSL_DEBUG_MSG( 3,
975 ( "unknown key size: %"
976 MBEDTLS_PRINTF_SIZET " bits",
977 own_key_size ) );
978 }
Jerry Yu67eced02022-02-25 13:37:36 +0800979 break;
Jerry Yucef3f332022-03-22 23:00:13 +0800980#endif /* MBEDTLS_RSA_C */
Jerry Yu67eced02022-02-25 13:37:36 +0800981 default:
982 MBEDTLS_SSL_DEBUG_MSG( 1,
Andrzej Kurek5c65c572022-04-13 14:28:52 -0400983 ( "unknown signature type : %u", sig ) );
Jerry Yu67eced02022-02-25 13:37:36 +0800984 break;
985 }
Jerry Yue91a51a2022-03-22 21:42:50 +0800986 return( -1 );
Jerry Yu67eced02022-02-25 13:37:36 +0800987}
988
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200989MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yu3e536442022-02-15 11:05:59 +0800990static int ssl_tls13_write_certificate_verify_body( mbedtls_ssl_context *ssl,
991 unsigned char *buf,
992 unsigned char *end,
993 size_t *out_len )
Jerry Yu8511f122022-01-29 10:01:04 +0800994{
995 int ret;
Jerry Yu3e536442022-02-15 11:05:59 +0800996 unsigned char *p = buf;
Jerry Yu8511f122022-01-29 10:01:04 +0800997 mbedtls_pk_context *own_key;
Jerry Yu3e536442022-02-15 11:05:59 +0800998
Jerry Yu8511f122022-01-29 10:01:04 +0800999 unsigned char handshake_hash[ MBEDTLS_TLS1_3_MD_MAX_SIZE ];
1000 size_t handshake_hash_len;
Jerry Yu3e536442022-02-15 11:05:59 +08001001 unsigned char verify_buffer[ SSL_VERIFY_STRUCT_MAX_SIZE ];
1002 size_t verify_buffer_len;
Jerry Yu67eced02022-02-25 13:37:36 +08001003 mbedtls_pk_type_t pk_type = MBEDTLS_PK_NONE;
Jerry Yu67eced02022-02-25 13:37:36 +08001004 mbedtls_md_type_t md_alg = MBEDTLS_MD_NONE;
pespacek34935872022-05-20 15:43:32 +02001005 psa_algorithm_t psa_algorithm = PSA_ALG_NONE;
Jerry Yu78272072022-02-22 10:28:13 +08001006 uint16_t algorithm = MBEDTLS_TLS1_3_SIG_NONE;
Jerry Yu3e536442022-02-15 11:05:59 +08001007 size_t signature_len = 0;
Jerry Yu3391ac02022-02-16 11:21:37 +08001008 unsigned char verify_hash[ MBEDTLS_MD_MAX_SIZE ];
Jerry Yu3e536442022-02-15 11:05:59 +08001009 size_t verify_hash_len;
pespacekb06acd72022-06-07 13:07:21 +02001010 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Jerry Yu8511f122022-01-29 10:01:04 +08001011
Jerry Yu0b7b1012022-02-23 12:23:05 +08001012 *out_len = 0;
1013
Jerry Yu3e536442022-02-15 11:05:59 +08001014 own_key = mbedtls_ssl_own_key( ssl );
1015 if( own_key == NULL )
Jerry Yu8511f122022-01-29 10:01:04 +08001016 {
Jerry Yu3e536442022-02-15 11:05:59 +08001017 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1018 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Jerry Yu8511f122022-01-29 10:01:04 +08001019 }
1020
Jerry Yu8511f122022-01-29 10:01:04 +08001021 ret = mbedtls_ssl_get_handshake_transcript( ssl,
1022 ssl->handshake->ciphersuite_info->mac,
1023 handshake_hash,
1024 sizeof( handshake_hash ),
1025 &handshake_hash_len );
1026 if( ret != 0 )
1027 return( ret );
1028
1029 MBEDTLS_SSL_DEBUG_BUF( 3, "handshake hash",
1030 handshake_hash,
1031 handshake_hash_len);
1032
Jerry Yu8511f122022-01-29 10:01:04 +08001033 ssl_tls13_create_verify_structure( handshake_hash, handshake_hash_len,
1034 verify_buffer, &verify_buffer_len,
1035 ssl->conf->endpoint );
1036
1037 /*
1038 * struct {
1039 * SignatureScheme algorithm;
1040 * opaque signature<0..2^16-1>;
1041 * } CertificateVerify;
1042 */
Jerry Yu8c338862022-03-23 13:34:04 +08001043 ret = ssl_tls13_get_sig_alg_from_pk( ssl, own_key, &algorithm );
Jerry Yue91a51a2022-03-22 21:42:50 +08001044 if( ret != 0 || ! mbedtls_ssl_sig_alg_is_received( ssl, algorithm ) )
Jerry Yu8511f122022-01-29 10:01:04 +08001045 {
Jerry Yud66409a2022-02-18 16:42:24 +08001046 MBEDTLS_SSL_DEBUG_MSG( 1,
Jerry Yu2124d052022-02-18 21:07:18 +08001047 ( "signature algorithm not in received or offered list." ) );
Jerry Yu67eced02022-02-25 13:37:36 +08001048
1049 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Signature algorithm is %s",
1050 mbedtls_ssl_sig_alg_to_str( algorithm ) ) );
1051
Jerry Yu71f36f12022-02-23 17:34:29 +08001052 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE,
Jerry Yud66409a2022-02-18 16:42:24 +08001053 MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
Jerry Yu3391ac02022-02-16 11:21:37 +08001054 return( MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
Jerry Yu8511f122022-01-29 10:01:04 +08001055 }
1056
Jerry Yu6c6f1022022-03-25 11:09:50 +08001057 if( mbedtls_ssl_tls13_get_pk_type_and_md_alg_from_sig_alg(
1058 algorithm, &pk_type, &md_alg ) != 0 )
Jerry Yu8c338862022-03-23 13:34:04 +08001059 {
Jerry Yuf8aa9a42022-03-23 20:40:28 +08001060 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Jerry Yu8c338862022-03-23 13:34:04 +08001061 }
1062
Jerry Yu3391ac02022-02-16 11:21:37 +08001063 /* Check there is space for the algorithm identifier (2 bytes) and the
1064 * signature length (2 bytes).
1065 */
1066 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 4 );
Jerry Yu3e536442022-02-15 11:05:59 +08001067 MBEDTLS_PUT_UINT16_BE( algorithm, p, 0 );
1068 p += 2;
Jerry Yu8511f122022-01-29 10:01:04 +08001069
1070 /* Hash verify buffer with indicated hash function */
pespacek34935872022-05-20 15:43:32 +02001071 psa_algorithm = mbedtls_psa_translate_md( md_alg );
pespacekb06acd72022-06-07 13:07:21 +02001072 status = psa_hash_compute( psa_algorithm,
1073 verify_buffer,
1074 verify_buffer_len,
1075 verify_hash,sizeof( verify_hash ),
1076 &verify_hash_len );
1077 if( status != PSA_SUCCESS )
pespacek670913f2022-06-07 10:53:39 +02001078 return( psa_ssl_status_to_mbedtls( status ) );
Jerry Yu8511f122022-01-29 10:01:04 +08001079
Jerry Yu8511f122022-01-29 10:01:04 +08001080 MBEDTLS_SSL_DEBUG_BUF( 3, "verify hash", verify_hash, verify_hash_len );
1081
Jerry Yu8beb9e12022-03-12 16:23:53 +08001082 if( ( ret = mbedtls_pk_sign_ext( pk_type, own_key,
1083 md_alg, verify_hash, verify_hash_len,
Jerry Yu67eced02022-02-25 13:37:36 +08001084 p + 2, (size_t)( end - ( p + 2 ) ), &signature_len,
1085 ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 )
Jerry Yu8511f122022-01-29 10:01:04 +08001086 {
1087 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_pk_sign", ret );
1088 return( ret );
1089 }
Jerry Yu8511f122022-01-29 10:01:04 +08001090
Jerry Yu3e536442022-02-15 11:05:59 +08001091 MBEDTLS_PUT_UINT16_BE( signature_len, p, 0 );
1092 p += 2 + signature_len;
Jerry Yu8511f122022-01-29 10:01:04 +08001093
Jerry Yu3e536442022-02-15 11:05:59 +08001094 *out_len = (size_t)( p - buf );
1095
Jerry Yu8511f122022-01-29 10:01:04 +08001096 return( ret );
1097}
Jerry Yu8511f122022-01-29 10:01:04 +08001098
Jerry Yu3e536442022-02-15 11:05:59 +08001099int mbedtls_ssl_tls13_write_certificate_verify( mbedtls_ssl_context *ssl )
Jerry Yu8511f122022-01-29 10:01:04 +08001100{
1101 int ret = 0;
Jerry Yuca133a32022-02-15 14:22:05 +08001102 unsigned char *buf;
1103 size_t buf_len, msg_len;
1104
Jerry Yu8511f122022-01-29 10:01:04 +08001105 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write certificate verify" ) );
1106
Ronald Cron8f6d39a2022-03-10 18:56:50 +01001107 MBEDTLS_SSL_PROC_CHK( mbedtls_ssl_start_handshake_msg( ssl,
Jerry Yuca133a32022-02-15 14:22:05 +08001108 MBEDTLS_SSL_HS_CERTIFICATE_VERIFY, &buf, &buf_len ) );
Jerry Yu8511f122022-01-29 10:01:04 +08001109
Jerry Yuca133a32022-02-15 14:22:05 +08001110 MBEDTLS_SSL_PROC_CHK( ssl_tls13_write_certificate_verify_body(
1111 ssl, buf, buf + buf_len, &msg_len ) );
Jerry Yu8511f122022-01-29 10:01:04 +08001112
Ronald Cron8f6d39a2022-03-10 18:56:50 +01001113 mbedtls_ssl_add_hs_msg_to_checksum( ssl, MBEDTLS_SSL_HS_CERTIFICATE_VERIFY,
1114 buf, msg_len );
Jerry Yu8511f122022-01-29 10:01:04 +08001115
Ronald Cron8f6d39a2022-03-10 18:56:50 +01001116 MBEDTLS_SSL_PROC_CHK( mbedtls_ssl_finish_handshake_msg(
Jerry Yuca133a32022-02-15 14:22:05 +08001117 ssl, buf_len, msg_len ) );
Jerry Yu8511f122022-01-29 10:01:04 +08001118
1119cleanup:
1120
1121 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write certificate verify" ) );
1122 return( ret );
1123}
1124
Jerry Yu90f152d2022-01-29 22:12:42 +08001125#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
1126
Jerry Yu5cc35062022-01-28 16:16:08 +08001127/*
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001128 *
XiaokangQianc5c39d52021-11-09 11:55:10 +00001129 * STATE HANDLING: Incoming Finished message.
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001130 */
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001131/*
1132 * Implementation
1133 */
1134
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001135MBEDTLS_CHECK_RETURN_CRITICAL
XiaokangQianaaa0e192021-11-10 03:07:04 +00001136static int ssl_tls13_preprocess_finished_message( mbedtls_ssl_context *ssl )
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001137{
1138 int ret;
1139
XiaokangQianc5c39d52021-11-09 11:55:10 +00001140 ret = mbedtls_ssl_tls13_calculate_verify_data( ssl,
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001141 ssl->handshake->state_local.finished_in.digest,
1142 sizeof( ssl->handshake->state_local.finished_in.digest ),
1143 &ssl->handshake->state_local.finished_in.digest_len,
XiaokangQianc5c39d52021-11-09 11:55:10 +00001144 ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT ?
XiaokangQianc13f9352021-11-11 06:13:22 +00001145 MBEDTLS_SSL_IS_SERVER : MBEDTLS_SSL_IS_CLIENT );
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001146 if( ret != 0 )
1147 {
XiaokangQianc5c39d52021-11-09 11:55:10 +00001148 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_tls13_calculate_verify_data", ret );
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001149 return( ret );
1150 }
1151
1152 return( 0 );
1153}
1154
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001155MBEDTLS_CHECK_RETURN_CRITICAL
XiaokangQianc5c39d52021-11-09 11:55:10 +00001156static int ssl_tls13_parse_finished_message( mbedtls_ssl_context *ssl,
1157 const unsigned char *buf,
1158 const unsigned char *end )
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001159{
XiaokangQian33062842021-11-11 03:37:45 +00001160 /*
1161 * struct {
XiaokangQianc13f9352021-11-11 06:13:22 +00001162 * opaque verify_data[Hash.length];
XiaokangQian33062842021-11-11 03:37:45 +00001163 * } Finished;
1164 */
1165 const unsigned char *expected_verify_data =
1166 ssl->handshake->state_local.finished_in.digest;
1167 size_t expected_verify_data_len =
1168 ssl->handshake->state_local.finished_in.digest_len;
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001169 /* Structural validation */
XiaokangQian33062842021-11-11 03:37:45 +00001170 if( (size_t)( end - buf ) != expected_verify_data_len )
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001171 {
1172 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
1173
1174 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR,
XiaokangQianc13f9352021-11-11 06:13:22 +00001175 MBEDTLS_ERR_SSL_DECODE_ERROR );
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001176 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
1177 }
1178
XiaokangQianc5c39d52021-11-09 11:55:10 +00001179 MBEDTLS_SSL_DEBUG_BUF( 4, "verify_data (self-computed):",
XiaokangQian33062842021-11-11 03:37:45 +00001180 expected_verify_data,
1181 expected_verify_data_len );
XiaokangQianc5c39d52021-11-09 11:55:10 +00001182 MBEDTLS_SSL_DEBUG_BUF( 4, "verify_data (received message):", buf,
XiaokangQian33062842021-11-11 03:37:45 +00001183 expected_verify_data_len );
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001184
1185 /* Semantic validation */
Gabor Mezei685472b2021-11-24 11:17:36 +01001186 if( mbedtls_ct_memcmp( buf,
1187 expected_verify_data,
1188 expected_verify_data_len ) != 0 )
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001189 {
1190 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
1191
XiaokangQian33062842021-11-11 03:37:45 +00001192 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_DECRYPT_ERROR,
XiaokangQianc13f9352021-11-11 06:13:22 +00001193 MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001194 return( MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
1195 }
1196 return( 0 );
1197}
1198
XiaokangQianc5c39d52021-11-09 11:55:10 +00001199int mbedtls_ssl_tls13_process_finished_message( mbedtls_ssl_context *ssl )
1200{
XiaokangQian33062842021-11-11 03:37:45 +00001201 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
XiaokangQianc5c39d52021-11-09 11:55:10 +00001202 unsigned char *buf;
Xiaofei Baieef15042021-11-18 07:29:56 +00001203 size_t buf_len;
XiaokangQianc5c39d52021-11-09 11:55:10 +00001204
XiaokangQiand0aa3e92021-11-10 06:17:40 +00001205 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse finished message" ) );
XiaokangQianc5c39d52021-11-09 11:55:10 +00001206
Xiaofei Bai746f9482021-11-12 08:53:56 +00001207 MBEDTLS_SSL_PROC_CHK( mbedtls_ssl_tls13_fetch_handshake_msg( ssl,
XiaokangQianc5c39d52021-11-09 11:55:10 +00001208 MBEDTLS_SSL_HS_FINISHED,
Xiaofei Baieef15042021-11-18 07:29:56 +00001209 &buf, &buf_len ) );
Jerry Yu0a92d6c2022-05-16 16:54:46 +08001210
1211 /* Preprocessing step: Compute handshake digest */
1212 MBEDTLS_SSL_PROC_CHK( ssl_tls13_preprocess_finished_message( ssl ) );
1213
Xiaofei Baieef15042021-11-18 07:29:56 +00001214 MBEDTLS_SSL_PROC_CHK( ssl_tls13_parse_finished_message( ssl, buf, buf + buf_len ) );
Jerry Yu0a92d6c2022-05-16 16:54:46 +08001215
Ronald Cron8f6d39a2022-03-10 18:56:50 +01001216 mbedtls_ssl_add_hs_msg_to_checksum( ssl, MBEDTLS_SSL_HS_FINISHED,
1217 buf, buf_len );
XiaokangQianc5c39d52021-11-09 11:55:10 +00001218
1219cleanup:
1220
XiaokangQiand0aa3e92021-11-10 06:17:40 +00001221 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse finished message" ) );
XiaokangQianc5c39d52021-11-09 11:55:10 +00001222 return( ret );
1223}
1224
XiaokangQian74af2a82021-09-22 07:40:30 +00001225/*
1226 *
XiaokangQiancc90c942021-11-09 12:30:09 +00001227 * STATE HANDLING: Write and send Finished message.
XiaokangQian74af2a82021-09-22 07:40:30 +00001228 *
1229 */
XiaokangQian74af2a82021-09-22 07:40:30 +00001230/*
XiaokangQian35dc6252021-11-11 08:16:19 +00001231 * Implement
XiaokangQian74af2a82021-09-22 07:40:30 +00001232 */
1233
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001234MBEDTLS_CHECK_RETURN_CRITICAL
XiaokangQian8773aa02021-11-10 07:33:09 +00001235static int ssl_tls13_prepare_finished_message( mbedtls_ssl_context *ssl )
XiaokangQian74af2a82021-09-22 07:40:30 +00001236{
1237 int ret;
1238
1239 /* Compute transcript of handshake up to now. */
XiaokangQiancc90c942021-11-09 12:30:09 +00001240 ret = mbedtls_ssl_tls13_calculate_verify_data( ssl,
XiaokangQian74af2a82021-09-22 07:40:30 +00001241 ssl->handshake->state_local.finished_out.digest,
1242 sizeof( ssl->handshake->state_local.finished_out.digest ),
1243 &ssl->handshake->state_local.finished_out.digest_len,
1244 ssl->conf->endpoint );
1245
1246 if( ret != 0 )
1247 {
Jerry Yu7ca30542021-12-08 15:57:57 +08001248 MBEDTLS_SSL_DEBUG_RET( 1, "calculate_verify_data failed", ret );
XiaokangQian74af2a82021-09-22 07:40:30 +00001249 return( ret );
1250 }
1251
1252 return( 0 );
1253}
1254
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001255MBEDTLS_CHECK_RETURN_CRITICAL
XiaokangQian8773aa02021-11-10 07:33:09 +00001256static int ssl_tls13_write_finished_message_body( mbedtls_ssl_context *ssl,
XiaokangQian35dc6252021-11-11 08:16:19 +00001257 unsigned char *buf,
1258 unsigned char *end,
Xiaofei Baid25fab62021-12-02 06:36:27 +00001259 size_t *out_len )
XiaokangQian74af2a82021-09-22 07:40:30 +00001260{
XiaokangQian8773aa02021-11-10 07:33:09 +00001261 size_t verify_data_len = ssl->handshake->state_local.finished_out.digest_len;
XiaokangQian0fa66432021-11-15 03:33:57 +00001262 /*
1263 * struct {
1264 * opaque verify_data[Hash.length];
1265 * } Finished;
1266 */
XiaokangQian8773aa02021-11-10 07:33:09 +00001267 MBEDTLS_SSL_CHK_BUF_PTR( buf, end, verify_data_len );
XiaokangQian74af2a82021-09-22 07:40:30 +00001268
1269 memcpy( buf, ssl->handshake->state_local.finished_out.digest,
XiaokangQian8773aa02021-11-10 07:33:09 +00001270 verify_data_len );
XiaokangQian74af2a82021-09-22 07:40:30 +00001271
Xiaofei Baid25fab62021-12-02 06:36:27 +00001272 *out_len = verify_data_len;
XiaokangQian74af2a82021-09-22 07:40:30 +00001273 return( 0 );
1274}
XiaokangQianc5c39d52021-11-09 11:55:10 +00001275
XiaokangQian35dc6252021-11-11 08:16:19 +00001276/* Main entry point: orchestrates the other functions */
1277int mbedtls_ssl_tls13_write_finished_message( mbedtls_ssl_context *ssl )
1278{
1279 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1280 unsigned char *buf;
1281 size_t buf_len, msg_len;
1282
1283 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write finished message" ) );
1284
XiaokangQiandce82242021-11-15 06:01:26 +00001285 MBEDTLS_SSL_PROC_CHK( ssl_tls13_prepare_finished_message( ssl ) );
1286
Ronald Cron8f6d39a2022-03-10 18:56:50 +01001287 MBEDTLS_SSL_PROC_CHK( mbedtls_ssl_start_handshake_msg( ssl,
XiaokangQian35dc6252021-11-11 08:16:19 +00001288 MBEDTLS_SSL_HS_FINISHED, &buf, &buf_len ) );
1289
1290 MBEDTLS_SSL_PROC_CHK( ssl_tls13_write_finished_message_body(
1291 ssl, buf, buf + buf_len, &msg_len ) );
1292
Ronald Cron8f6d39a2022-03-10 18:56:50 +01001293 mbedtls_ssl_add_hs_msg_to_checksum( ssl, MBEDTLS_SSL_HS_FINISHED,
1294 buf, msg_len );
XiaokangQian35dc6252021-11-11 08:16:19 +00001295
Ronald Cron8f6d39a2022-03-10 18:56:50 +01001296 MBEDTLS_SSL_PROC_CHK( mbedtls_ssl_finish_handshake_msg(
1297 ssl, buf_len, msg_len ) );
XiaokangQian35dc6252021-11-11 08:16:19 +00001298cleanup:
1299
1300 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write finished message" ) );
1301 return( ret );
1302}
1303
Jerry Yu378254d2021-10-30 21:44:47 +08001304void mbedtls_ssl_tls13_handshake_wrapup( mbedtls_ssl_context *ssl )
1305{
1306
1307 MBEDTLS_SSL_DEBUG_MSG( 3, ( "=> handshake wrapup" ) );
1308
Jerry Yue8c1fca2022-05-18 14:48:56 +08001309 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Switch to application keys for inbound traffic" ) );
1310 mbedtls_ssl_set_inbound_transform ( ssl, ssl->transform_application );
1311
1312 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Switch to application keys for outbound traffic" ) );
1313 mbedtls_ssl_set_outbound_transform( ssl, ssl->transform_application );
1314
Jerry Yu378254d2021-10-30 21:44:47 +08001315 /*
Jerry Yucfe64f02021-11-15 13:54:06 +08001316 * Free the previous session and switch to the current one.
Jerry Yu378254d2021-10-30 21:44:47 +08001317 */
1318 if( ssl->session )
1319 {
Jerry Yu378254d2021-10-30 21:44:47 +08001320 mbedtls_ssl_session_free( ssl->session );
1321 mbedtls_free( ssl->session );
1322 }
1323 ssl->session = ssl->session_negotiate;
1324 ssl->session_negotiate = NULL;
1325
1326 MBEDTLS_SSL_DEBUG_MSG( 3, ( "<= handshake wrapup" ) );
1327}
1328
Ronald Cron49ad6192021-11-24 16:25:31 +01001329/*
1330 *
1331 * STATE HANDLING: Write ChangeCipherSpec
1332 *
1333 */
1334#if defined(MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001335MBEDTLS_CHECK_RETURN_CRITICAL
Ronald Cron49ad6192021-11-24 16:25:31 +01001336static int ssl_tls13_write_change_cipher_spec_body( mbedtls_ssl_context *ssl,
1337 unsigned char *buf,
1338 unsigned char *end,
1339 size_t *olen )
1340{
1341 ((void) ssl);
1342
1343 MBEDTLS_SSL_CHK_BUF_PTR( buf, end, 1 );
1344 buf[0] = 1;
1345 *olen = 1;
1346
1347 return( 0 );
1348}
1349
Ronald Cron49ad6192021-11-24 16:25:31 +01001350int mbedtls_ssl_tls13_write_change_cipher_spec( mbedtls_ssl_context *ssl )
1351{
1352 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1353
1354 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write change cipher spec" ) );
1355
Ronald Cron49ad6192021-11-24 16:25:31 +01001356 /* Write CCS message */
1357 MBEDTLS_SSL_PROC_CHK( ssl_tls13_write_change_cipher_spec_body(
1358 ssl, ssl->out_msg,
1359 ssl->out_msg + MBEDTLS_SSL_OUT_CONTENT_LEN,
1360 &ssl->out_msglen ) );
1361
1362 ssl->out_msgtype = MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC;
1363
Ronald Cron49ad6192021-11-24 16:25:31 +01001364 /* Dispatch message */
Ronald Cron66dbf912022-02-02 15:33:46 +01001365 MBEDTLS_SSL_PROC_CHK( mbedtls_ssl_write_record( ssl, 0 ) );
Ronald Cron49ad6192021-11-24 16:25:31 +01001366
1367cleanup:
1368
1369 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write change cipher spec" ) );
1370 return( ret );
1371}
1372
1373#endif /* MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE */
1374
XiaokangQian78b1fa72022-01-19 06:56:30 +00001375/* Reset SSL context and update hash for handling HRR.
1376 *
1377 * Replace Transcript-Hash(X) by
1378 * Transcript-Hash( message_hash ||
1379 * 00 00 Hash.length ||
1380 * X )
1381 * A few states of the handshake are preserved, including:
1382 * - session ID
1383 * - session ticket
1384 * - negotiated ciphersuite
1385 */
1386int mbedtls_ssl_reset_transcript_for_hrr( mbedtls_ssl_context *ssl )
1387{
1388 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1389 unsigned char hash_transcript[ MBEDTLS_MD_MAX_SIZE + 4 ];
XiaokangQian0ece9982022-01-24 08:56:23 +00001390 size_t hash_len;
XiaokangQian78b1fa72022-01-19 06:56:30 +00001391 const mbedtls_ssl_ciphersuite_t *ciphersuite_info;
1392 uint16_t cipher_suite = ssl->session_negotiate->ciphersuite;
1393 ciphersuite_info = mbedtls_ssl_ciphersuite_from_id( cipher_suite );
1394
1395 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Reset SSL session for HRR" ) );
1396
XiaokangQian0ece9982022-01-24 08:56:23 +00001397 ret = mbedtls_ssl_get_handshake_transcript( ssl, ciphersuite_info->mac,
1398 hash_transcript + 4,
1399 MBEDTLS_MD_MAX_SIZE,
1400 &hash_len );
1401 if( ret != 0 )
1402 {
1403 MBEDTLS_SSL_DEBUG_RET( 4, "mbedtls_ssl_get_handshake_transcript", ret );
1404 return( ret );
1405 }
1406
1407 hash_transcript[0] = MBEDTLS_SSL_HS_MESSAGE_HASH;
1408 hash_transcript[1] = 0;
1409 hash_transcript[2] = 0;
1410 hash_transcript[3] = (unsigned char) hash_len;
1411
1412 hash_len += 4;
1413
XiaokangQian78b1fa72022-01-19 06:56:30 +00001414 if( ciphersuite_info->mac == MBEDTLS_MD_SHA256 )
1415 {
1416#if defined(MBEDTLS_SHA256_C)
XiaokangQian78b1fa72022-01-19 06:56:30 +00001417 MBEDTLS_SSL_DEBUG_BUF( 4, "Truncated SHA-256 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_sha256_psa );
1422 psa_hash_setup( &ssl->handshake->fin_sha256_psa, PSA_ALG_SHA_256 );
1423#else
1424 mbedtls_sha256_starts( &ssl->handshake->fin_sha256, 0 );
1425#endif
XiaokangQian78b1fa72022-01-19 06:56:30 +00001426#endif /* MBEDTLS_SHA256_C */
1427 }
1428 else if( ciphersuite_info->mac == MBEDTLS_MD_SHA384 )
1429 {
1430#if defined(MBEDTLS_SHA384_C)
XiaokangQian78b1fa72022-01-19 06:56:30 +00001431 MBEDTLS_SSL_DEBUG_BUF( 4, "Truncated SHA-384 handshake transcript",
XiaokangQian0ece9982022-01-24 08:56:23 +00001432 hash_transcript, hash_len );
XiaokangQian78b1fa72022-01-19 06:56:30 +00001433
1434#if defined(MBEDTLS_USE_PSA_CRYPTO)
1435 psa_hash_abort( &ssl->handshake->fin_sha384_psa );
1436 psa_hash_setup( &ssl->handshake->fin_sha384_psa, PSA_ALG_SHA_384 );
1437#else
1438 mbedtls_sha512_starts( &ssl->handshake->fin_sha512, 1 );
1439#endif
XiaokangQian78b1fa72022-01-19 06:56:30 +00001440#endif /* MBEDTLS_SHA384_C */
1441 }
1442
XiaokangQian0ece9982022-01-24 08:56:23 +00001443#if defined(MBEDTLS_SHA256_C) || defined(MBEDTLS_SHA384_C)
1444 ssl->handshake->update_checksum( ssl, hash_transcript, hash_len );
1445#endif /* MBEDTLS_SHA256_C || MBEDTLS_SHA384_C */
Przemyslaw Stekiel4b3fff42022-02-14 16:39:52 +01001446
XiaokangQian78b1fa72022-01-19 06:56:30 +00001447 return( ret );
1448}
1449
XiaokangQian9b5d04b2022-04-10 10:20:43 +00001450#if defined(MBEDTLS_ECDH_C)
XiaokangQian7807f9f2022-02-15 10:04:37 +00001451
XiaokangQian9b5d04b2022-04-10 10:20:43 +00001452int mbedtls_ssl_tls13_read_public_ecdhe_share( mbedtls_ssl_context *ssl,
1453 const unsigned char *buf,
1454 size_t buf_len )
XiaokangQian7807f9f2022-02-15 10:04:37 +00001455{
XiaokangQian9b5d04b2022-04-10 10:20:43 +00001456 uint8_t *p = (uint8_t*)buf;
XiaokangQiancfd925f2022-04-14 07:10:37 +00001457 const uint8_t *end = buf + buf_len;
XiaokangQian9b5d04b2022-04-10 10:20:43 +00001458 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001459
XiaokangQian9b5d04b2022-04-10 10:20:43 +00001460 /* Get size of the TLS opaque key_exchange field of the KeyShareEntry struct. */
XiaokangQiancfd925f2022-04-14 07:10:37 +00001461 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 2 );
XiaokangQian9b5d04b2022-04-10 10:20:43 +00001462 uint16_t peerkey_len = MBEDTLS_GET_UINT16_BE( p, 0 );
1463 p += 2;
XiaokangQian3207a322022-02-23 03:15:27 +00001464
XiaokangQian9b5d04b2022-04-10 10:20:43 +00001465 /* Check if key size is consistent with given buffer length. */
XiaokangQiancfd925f2022-04-14 07:10:37 +00001466 MBEDTLS_SSL_CHK_BUF_PTR( p, end, peerkey_len );
XiaokangQian9b5d04b2022-04-10 10:20:43 +00001467
1468 /* Store peer's ECDH public key. */
1469 memcpy( handshake->ecdh_psa_peerkey, p, peerkey_len );
1470 handshake->ecdh_psa_peerkey_len = peerkey_len;
1471
XiaokangQian3207a322022-02-23 03:15:27 +00001472 return( 0 );
1473}
Jerry Yu89e103c2022-03-30 22:43:29 +08001474
1475int mbedtls_ssl_tls13_generate_and_write_ecdh_key_exchange(
1476 mbedtls_ssl_context *ssl,
1477 uint16_t named_group,
1478 unsigned char *buf,
1479 unsigned char *end,
1480 size_t *out_len )
1481{
1482 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
1483 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
1484 psa_key_attributes_t key_attributes;
1485 size_t own_pubkey_len;
1486 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
1487 size_t ecdh_bits = 0;
1488
1489 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Perform PSA-based ECDH computation." ) );
1490
1491 /* Convert EC group to PSA key type. */
1492 if( ( handshake->ecdh_psa_type =
1493 mbedtls_psa_parse_tls_ecc_group( named_group, &ecdh_bits ) ) == 0 )
1494 return( MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
1495
1496 ssl->handshake->ecdh_bits = ecdh_bits;
1497
1498 key_attributes = psa_key_attributes_init();
1499 psa_set_key_usage_flags( &key_attributes, PSA_KEY_USAGE_DERIVE );
1500 psa_set_key_algorithm( &key_attributes, PSA_ALG_ECDH );
1501 psa_set_key_type( &key_attributes, handshake->ecdh_psa_type );
1502 psa_set_key_bits( &key_attributes, handshake->ecdh_bits );
1503
1504 /* Generate ECDH private key. */
1505 status = psa_generate_key( &key_attributes,
1506 &handshake->ecdh_psa_privkey );
1507 if( status != PSA_SUCCESS )
1508 {
1509 ret = psa_ssl_status_to_mbedtls( status );
1510 MBEDTLS_SSL_DEBUG_RET( 1, "psa_generate_key", ret );
1511 return( ret );
1512
1513 }
1514
1515 /* Export the public part of the ECDH private key from PSA. */
1516 status = psa_export_public_key( handshake->ecdh_psa_privkey,
1517 buf, (size_t)( end - buf ),
1518 &own_pubkey_len );
1519 if( status != PSA_SUCCESS )
1520 {
1521 ret = psa_ssl_status_to_mbedtls( status );
1522 MBEDTLS_SSL_DEBUG_RET( 1, "psa_export_public_key", ret );
1523 return( ret );
1524
1525 }
1526
1527 *out_len = own_pubkey_len;
1528
1529 return( 0 );
1530}
XiaokangQian9b5d04b2022-04-10 10:20:43 +00001531#endif /* MBEDTLS_ECDH_C */
XiaokangQian7807f9f2022-02-15 10:04:37 +00001532
Jerry Yufb4b6472022-01-27 15:03:26 +08001533#endif /* MBEDTLS_SSL_TLS_C && MBEDTLS_SSL_PROTO_TLS1_3 */