blob: 7d0559bc224ed831c0fd16b4ab6ca2f2731f5a2b [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"
Ronald Crone3dac4a2022-06-10 17:21:51 +020034#include "ssl_tls13_invasive.h"
Jerry Yu30b071c2021-09-12 20:16:03 +080035#include "ssl_tls13_keys.h"
Jerry Yu67eced02022-02-25 13:37:36 +080036#include "ssl_debug_helpers.h"
Jerry Yu65dd2cc2021-08-18 16:38:40 +080037
pespaceka1378102022-04-26 15:03:11 +020038#include "psa/crypto.h"
39#include "mbedtls/psa_util.h"
40
Jerry Yufbe3e642022-04-25 19:31:51 +080041const uint8_t mbedtls_ssl_tls13_hello_retry_request_magic[
42 MBEDTLS_SERVER_HELLO_RANDOM_LEN ] =
43 { 0xCF, 0x21, 0xAD, 0x74, 0xE5, 0x9A, 0x61, 0x11,
44 0xBE, 0x1D, 0x8C, 0x02, 0x1E, 0x65, 0xB8, 0x91,
45 0xC2, 0xA2, 0x11, 0x16, 0x7A, 0xBB, 0x8C, 0x5E,
46 0x07, 0x9E, 0x09, 0xE2, 0xC8, 0xA8, 0x33, 0x9C };
Jerry Yu93a13f22022-04-11 23:00:01 +080047
Xiaofei Bai746f9482021-11-12 08:53:56 +000048int mbedtls_ssl_tls13_fetch_handshake_msg( mbedtls_ssl_context *ssl,
49 unsigned hs_type,
50 unsigned char **buf,
Xiaofei Baieef15042021-11-18 07:29:56 +000051 size_t *buf_len )
XiaokangQian6b226b02021-09-24 07:51:16 +000052{
53 int ret;
54
55 if( ( ret = mbedtls_ssl_read_record( ssl, 0 ) ) != 0 )
56 {
57 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
58 goto cleanup;
59 }
60
XiaokangQian16c61aa2021-09-27 09:30:17 +000061 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE ||
XiaokangQian6b226b02021-09-24 07:51:16 +000062 ssl->in_msg[0] != hs_type )
63 {
XiaokangQian16c61aa2021-09-27 09:30:17 +000064 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Receive unexpected handshake message." ) );
XiaokangQian6b226b02021-09-24 07:51:16 +000065 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE,
XiaokangQian05420b12021-09-29 08:46:37 +000066 MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
XiaokangQian6b226b02021-09-24 07:51:16 +000067 ret = MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE;
68 goto cleanup;
69 }
70
XiaokangQian05420b12021-09-29 08:46:37 +000071 /*
72 * Jump handshake header (4 bytes, see Section 4 of RFC 8446).
73 * ...
74 * HandshakeType msg_type;
75 * uint24 length;
76 * ...
77 */
Xiaofei Baieef15042021-11-18 07:29:56 +000078 *buf = ssl->in_msg + 4;
79 *buf_len = ssl->in_hslen - 4;
XiaokangQian6b226b02021-09-24 07:51:16 +000080
XiaokangQian6b226b02021-09-24 07:51:16 +000081cleanup:
82
83 return( ret );
84}
85
Jerry Yubc20bdd2021-08-24 15:59:48 +080086#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Jerry Yu30b071c2021-09-12 20:16:03 +080087/*
Jerry Yu30b071c2021-09-12 20:16:03 +080088 * STATE HANDLING: Read CertificateVerify
89 */
Jerry Yud0fc5852021-10-29 11:09:06 +080090/* Macro to express the maximum length of the verify structure.
Jerry Yu30b071c2021-09-12 20:16:03 +080091 *
92 * The structure is computed per TLS 1.3 specification as:
93 * - 64 bytes of octet 32,
94 * - 33 bytes for the context string
95 * (which is either "TLS 1.3, client CertificateVerify"
96 * or "TLS 1.3, server CertificateVerify"),
Jerry Yud0fc5852021-10-29 11:09:06 +080097 * - 1 byte for the octet 0x0, which serves as a separator,
Jerry Yu30b071c2021-09-12 20:16:03 +080098 * - 32 or 48 bytes for the Transcript-Hash(Handshake Context, Certificate)
99 * (depending on the size of the transcript_hash)
100 *
101 * This results in a total size of
102 * - 130 bytes for a SHA256-based transcript hash, or
103 * (64 + 33 + 1 + 32 bytes)
104 * - 146 bytes for a SHA384-based transcript hash.
105 * (64 + 33 + 1 + 48 bytes)
106 *
107 */
Jerry Yu26c2d112021-10-25 12:42:58 +0800108#define SSL_VERIFY_STRUCT_MAX_SIZE ( 64 + \
109 33 + \
110 1 + \
111 MBEDTLS_TLS1_3_MD_MAX_SIZE \
Jerry Yu30b071c2021-09-12 20:16:03 +0800112 )
Jerry Yu30b071c2021-09-12 20:16:03 +0800113
Jerry Yu0b32c502021-10-28 13:41:59 +0800114/*
115 * The ssl_tls13_create_verify_structure() creates the verify structure.
116 * As input, it requires the transcript hash.
117 *
118 * The caller has to ensure that the buffer has size at least
119 * SSL_VERIFY_STRUCT_MAX_SIZE bytes.
120 */
Jerry Yud0fc5852021-10-29 11:09:06 +0800121static void ssl_tls13_create_verify_structure( const unsigned char *transcript_hash,
Jerry Yu0b32c502021-10-28 13:41:59 +0800122 size_t transcript_hash_len,
123 unsigned char *verify_buffer,
124 size_t *verify_buffer_len,
125 int from )
126{
127 size_t idx;
Jerry Yu30b071c2021-09-12 20:16:03 +0800128
Jerry Yu0b32c502021-10-28 13:41:59 +0800129 /* RFC 8446, Section 4.4.3:
130 *
131 * The digital signature [in the CertificateVerify message] is then
132 * computed over the concatenation of:
133 * - A string that consists of octet 32 (0x20) repeated 64 times
134 * - The context string
135 * - A single 0 byte which serves as the separator
136 * - The content to be signed
137 */
138 memset( verify_buffer, 0x20, 64 );
139 idx = 64;
140
141 if( from == MBEDTLS_SSL_IS_CLIENT )
142 {
143 memcpy( verify_buffer + idx, MBEDTLS_SSL_TLS1_3_LBL_WITH_LEN( client_cv ) );
144 idx += MBEDTLS_SSL_TLS1_3_LBL_LEN( client_cv );
145 }
146 else
147 { /* from == MBEDTLS_SSL_IS_SERVER */
148 memcpy( verify_buffer + idx, MBEDTLS_SSL_TLS1_3_LBL_WITH_LEN( server_cv ) );
149 idx += MBEDTLS_SSL_TLS1_3_LBL_LEN( server_cv );
150 }
151
152 verify_buffer[idx++] = 0x0;
153
154 memcpy( verify_buffer + idx, transcript_hash, transcript_hash_len );
155 idx += transcript_hash_len;
156
157 *verify_buffer_len = idx;
158}
159
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200160MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yu0b32c502021-10-28 13:41:59 +0800161static int ssl_tls13_parse_certificate_verify( mbedtls_ssl_context *ssl,
Jerry Yud0fc5852021-10-29 11:09:06 +0800162 const unsigned char *buf,
163 const unsigned char *end,
164 const unsigned char *verify_buffer,
165 size_t verify_buffer_len )
Jerry Yu30b071c2021-09-12 20:16:03 +0800166{
167 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
pespaceka1378102022-04-26 15:03:11 +0200168 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Jerry Yu30b071c2021-09-12 20:16:03 +0800169 const unsigned char *p = buf;
170 uint16_t algorithm;
Jerry Yu30b071c2021-09-12 20:16:03 +0800171 size_t signature_len;
172 mbedtls_pk_type_t sig_alg;
173 mbedtls_md_type_t md_alg;
pespaceka1378102022-04-26 15:03:11 +0200174 psa_algorithm_t hash_alg = PSA_ALG_NONE;
175 unsigned char verify_hash[PSA_HASH_MAX_SIZE];
Jerry Yu30b071c2021-09-12 20:16:03 +0800176 size_t verify_hash_len;
177
Xiaofei Baid25fab62021-12-02 06:36:27 +0000178 void const *options = NULL;
XiaokangQian82d34cc2021-11-03 08:51:56 +0000179#if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT)
Xiaofei Baid25fab62021-12-02 06:36:27 +0000180 mbedtls_pk_rsassa_pss_options rsassa_pss_options;
XiaokangQian82d34cc2021-11-03 08:51:56 +0000181#endif /* MBEDTLS_X509_RSASSA_PSS_SUPPORT */
182
Jerry Yu30b071c2021-09-12 20:16:03 +0800183 /*
184 * struct {
185 * SignatureScheme algorithm;
186 * opaque signature<0..2^16-1>;
187 * } CertificateVerify;
188 */
189 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 2 );
190 algorithm = MBEDTLS_GET_UINT16_BE( p, 0 );
191 p += 2;
192
193 /* RFC 8446 section 4.4.3
194 *
195 * If the CertificateVerify message is sent by a server, the signature algorithm
196 * MUST be one offered in the client's "signature_algorithms" extension unless
197 * no valid certificate chain can be produced without unsupported algorithms
198 *
199 * RFC 8446 section 4.4.2.2
200 *
201 * If the client cannot construct an acceptable chain using the provided
202 * certificates and decides to abort the handshake, then it MUST abort the handshake
203 * with an appropriate certificate-related alert (by default, "unsupported_certificate").
204 *
Jerry Yu6f87f252021-10-29 20:12:51 +0800205 * Check if algorithm is an offered signature algorithm.
Jerry Yu30b071c2021-09-12 20:16:03 +0800206 */
Jerry Yu24811fb2022-01-19 18:02:15 +0800207 if( ! mbedtls_ssl_sig_alg_is_offered( ssl, algorithm ) )
Jerry Yu30b071c2021-09-12 20:16:03 +0800208 {
Jerry Yu982d9e52021-10-14 15:59:37 +0800209 /* algorithm not in offered signature algorithms list */
210 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Received signature algorithm(%04x) is not "
211 "offered.",
212 ( unsigned int ) algorithm ) );
Jerry Yu6f87f252021-10-29 20:12:51 +0800213 goto error;
Jerry Yu30b071c2021-09-12 20:16:03 +0800214 }
215
Jerry Yu8c338862022-03-23 13:34:04 +0800216 if( mbedtls_ssl_tls13_get_pk_type_and_md_alg_from_sig_alg(
Jerry Yuf8aa9a42022-03-23 20:40:28 +0800217 algorithm, &sig_alg, &md_alg ) != 0 )
Jerry Yu30b071c2021-09-12 20:16:03 +0800218 {
Jerry Yu8c338862022-03-23 13:34:04 +0800219 goto error;
Jerry Yu30b071c2021-09-12 20:16:03 +0800220 }
221
pespaceka1378102022-04-26 15:03:11 +0200222 hash_alg = mbedtls_psa_translate_md( md_alg );
223 if( hash_alg == 0 )
224 {
225 goto error;
226 }
227
Jerry Yu30b071c2021-09-12 20:16:03 +0800228 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Certificate Verify: Signature algorithm ( %04x )",
229 ( unsigned int ) algorithm ) );
230
231 /*
232 * Check the certificate's key type matches the signature alg
233 */
234 if( !mbedtls_pk_can_do( &ssl->session_negotiate->peer_cert->pk, sig_alg ) )
235 {
236 MBEDTLS_SSL_DEBUG_MSG( 1, ( "signature algorithm doesn't match cert key" ) );
Jerry Yu6f87f252021-10-29 20:12:51 +0800237 goto error;
Jerry Yu30b071c2021-09-12 20:16:03 +0800238 }
239
240 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 2 );
241 signature_len = MBEDTLS_GET_UINT16_BE( p, 0 );
242 p += 2;
243 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, signature_len );
244
pespaceka1378102022-04-26 15:03:11 +0200245 status = psa_hash_compute( hash_alg,
246 verify_buffer,
247 verify_buffer_len,
248 verify_hash,
249 sizeof( verify_hash ),
250 &verify_hash_len );
251 if( status != PSA_SUCCESS )
Jerry Yu30b071c2021-09-12 20:16:03 +0800252 {
pespaceka1378102022-04-26 15:03:11 +0200253 MBEDTLS_SSL_DEBUG_RET( 1, "hash computation PSA error", status );
Jerry Yu6f87f252021-10-29 20:12:51 +0800254 goto error;
Jerry Yu133690c2021-10-25 14:01:13 +0800255 }
256
Jerry Yu30b071c2021-09-12 20:16:03 +0800257 MBEDTLS_SSL_DEBUG_BUF( 3, "verify hash", verify_hash, verify_hash_len );
XiaokangQian82d34cc2021-11-03 08:51:56 +0000258#if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT)
259 if( sig_alg == MBEDTLS_PK_RSASSA_PSS )
260 {
261 const mbedtls_md_info_t* md_info;
Xiaofei Baid25fab62021-12-02 06:36:27 +0000262 rsassa_pss_options.mgf1_hash_id = md_alg;
XiaokangQian82d34cc2021-11-03 08:51:56 +0000263 if( ( md_info = mbedtls_md_info_from_type( md_alg ) ) == NULL )
264 {
265 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
266 }
Xiaofei Baid25fab62021-12-02 06:36:27 +0000267 rsassa_pss_options.expected_salt_len = mbedtls_md_get_size( md_info );
268 options = (const void*) &rsassa_pss_options;
XiaokangQian82d34cc2021-11-03 08:51:56 +0000269 }
270#endif /* MBEDTLS_X509_RSASSA_PSS_SUPPORT */
Jerry Yu30b071c2021-09-12 20:16:03 +0800271
Xiaofei Baid25fab62021-12-02 06:36:27 +0000272 if( ( ret = mbedtls_pk_verify_ext( sig_alg, options,
Jerry Yu30b071c2021-09-12 20:16:03 +0800273 &ssl->session_negotiate->peer_cert->pk,
274 md_alg, verify_hash, verify_hash_len,
Jerry Yu6f87f252021-10-29 20:12:51 +0800275 p, signature_len ) ) == 0 )
Jerry Yu30b071c2021-09-12 20:16:03 +0800276 {
Jerry Yu6f87f252021-10-29 20:12:51 +0800277 return( 0 );
Jerry Yu30b071c2021-09-12 20:16:03 +0800278 }
Jerry Yu6f87f252021-10-29 20:12:51 +0800279 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_pk_verify_ext", ret );
Jerry Yu30b071c2021-09-12 20:16:03 +0800280
Jerry Yu6f87f252021-10-29 20:12:51 +0800281error:
282 /* RFC 8446 section 4.4.3
283 *
284 * If the verification fails, the receiver MUST terminate the handshake
285 * with a "decrypt_error" alert.
286 */
287 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_DECRYPT_ERROR,
288 MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
289 return( MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
290
Jerry Yu30b071c2021-09-12 20:16:03 +0800291}
292#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
293
294int mbedtls_ssl_tls13_process_certificate_verify( mbedtls_ssl_context *ssl )
295{
Jerry Yu30b071c2021-09-12 20:16:03 +0800296
Jerry Yuda8cdf22021-10-25 15:06:49 +0800297#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
298 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
299 unsigned char verify_buffer[SSL_VERIFY_STRUCT_MAX_SIZE];
300 size_t verify_buffer_len;
301 unsigned char transcript[MBEDTLS_TLS1_3_MD_MAX_SIZE];
302 size_t transcript_len;
303 unsigned char *buf;
304 size_t buf_len;
305
Jerry Yu30b071c2021-09-12 20:16:03 +0800306 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate verify" ) );
307
Jerry Yuda8cdf22021-10-25 15:06:49 +0800308 MBEDTLS_SSL_PROC_CHK(
Xiaofei Bai746f9482021-11-12 08:53:56 +0000309 mbedtls_ssl_tls13_fetch_handshake_msg( ssl,
Jerry Yuda8cdf22021-10-25 15:06:49 +0800310 MBEDTLS_SSL_HS_CERTIFICATE_VERIFY, &buf, &buf_len ) );
Jerry Yu30b071c2021-09-12 20:16:03 +0800311
Jerry Yuda8cdf22021-10-25 15:06:49 +0800312 /* Need to calculate the hash of the transcript first
Jerry Yu0b32c502021-10-28 13:41:59 +0800313 * before reading the message since otherwise it gets
314 * included in the transcript
315 */
Jerry Yuda8cdf22021-10-25 15:06:49 +0800316 ret = mbedtls_ssl_get_handshake_transcript( ssl,
317 ssl->handshake->ciphersuite_info->mac,
318 transcript, sizeof( transcript ),
319 &transcript_len );
320 if( ret != 0 )
Jerry Yu30b071c2021-09-12 20:16:03 +0800321 {
Jerry Yuda8cdf22021-10-25 15:06:49 +0800322 MBEDTLS_SSL_PEND_FATAL_ALERT(
323 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR,
324 MBEDTLS_ERR_SSL_INTERNAL_ERROR );
325 return( ret );
Jerry Yu30b071c2021-09-12 20:16:03 +0800326 }
327
Jerry Yuda8cdf22021-10-25 15:06:49 +0800328 MBEDTLS_SSL_DEBUG_BUF( 3, "handshake hash", transcript, transcript_len );
329
330 /* Create verify structure */
331 ssl_tls13_create_verify_structure( transcript,
Jerry Yu0b32c502021-10-28 13:41:59 +0800332 transcript_len,
333 verify_buffer,
334 &verify_buffer_len,
335 ( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT ) ?
336 MBEDTLS_SSL_IS_SERVER :
337 MBEDTLS_SSL_IS_CLIENT );
Jerry Yuda8cdf22021-10-25 15:06:49 +0800338
339 /* Process the message contents */
Jerry Yu0b32c502021-10-28 13:41:59 +0800340 MBEDTLS_SSL_PROC_CHK( ssl_tls13_parse_certificate_verify( ssl, buf,
341 buf + buf_len, verify_buffer, verify_buffer_len ) );
Jerry Yuda8cdf22021-10-25 15:06:49 +0800342
Ronald Cron8f6d39a2022-03-10 18:56:50 +0100343 mbedtls_ssl_add_hs_msg_to_checksum( ssl, MBEDTLS_SSL_HS_CERTIFICATE_VERIFY,
344 buf, buf_len );
Jerry Yu30b071c2021-09-12 20:16:03 +0800345
346cleanup:
347
348 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse certificate verify" ) );
Jerry Yu5398c102021-11-05 13:32:38 +0800349 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_tls13_process_certificate_verify", ret );
Jerry Yu30b071c2021-09-12 20:16:03 +0800350 return( ret );
Jerry Yuda8cdf22021-10-25 15:06:49 +0800351#else
352 ((void) ssl);
353 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
354 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
355#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
Jerry Yu30b071c2021-09-12 20:16:03 +0800356}
357
358/*
Xiaofei Bai947571e2021-09-29 09:12:03 +0000359 *
XiaokangQian6b916b12022-04-25 07:29:34 +0000360 * STATE HANDLING: Incoming Certificate.
Xiaofei Bai947571e2021-09-29 09:12:03 +0000361 *
362 */
363
Xiaofei Bai947571e2021-09-29 09:12:03 +0000364#if defined(MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED)
365#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
366/*
367 * Structure of Certificate message:
368 *
369 * enum {
370 * X509(0),
371 * RawPublicKey(2),
372 * (255)
373 * } CertificateType;
374 *
375 * struct {
376 * select (certificate_type) {
377 * case RawPublicKey:
378 * * From RFC 7250 ASN.1_subjectPublicKeyInfo *
379 * opaque ASN1_subjectPublicKeyInfo<1..2^24-1>;
380 * case X509:
381 * opaque cert_data<1..2^24-1>;
382 * };
383 * Extension extensions<0..2^16-1>;
384 * } CertificateEntry;
385 *
386 * struct {
387 * opaque certificate_request_context<0..2^8-1>;
388 * CertificateEntry certificate_list<0..2^24-1>;
389 * } Certificate;
390 *
391 */
Xiaofei Bai79595ac2021-10-26 07:16:45 +0000392
393/* Parse certificate chain send by the server. */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200394MBEDTLS_CHECK_RETURN_CRITICAL
Ronald Crone3dac4a2022-06-10 17:21:51 +0200395MBEDTLS_STATIC_TESTABLE
396int mbedtls_ssl_tls13_parse_certificate( mbedtls_ssl_context *ssl,
397 const unsigned char *buf,
398 const unsigned char *end )
Xiaofei Bai947571e2021-09-29 09:12:03 +0000399{
400 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
401 size_t certificate_request_context_len = 0;
402 size_t certificate_list_len = 0;
403 const unsigned char *p = buf;
404 const unsigned char *certificate_list_end;
405
XiaokangQian63e713e2022-05-15 04:26:57 +0000406 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 4 );
Xiaofei Bai947571e2021-09-29 09:12:03 +0000407 certificate_request_context_len = p[0];
XiaokangQian63e713e2022-05-15 04:26:57 +0000408 certificate_list_len = MBEDTLS_GET_UINT24_BE( p, 1 );
409 p += 4;
Xiaofei Bai947571e2021-09-29 09:12:03 +0000410
411 /* In theory, the certificate list can be up to 2^24 Bytes, but we don't
412 * support anything beyond 2^16 = 64K.
413 */
Xiaofei Bai79595ac2021-10-26 07:16:45 +0000414 if( ( certificate_request_context_len != 0 ) ||
415 ( certificate_list_len >= 0x10000 ) )
Xiaofei Bai947571e2021-09-29 09:12:03 +0000416 {
417 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
418 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR,
419 MBEDTLS_ERR_SSL_DECODE_ERROR );
420 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
421 }
422
423 /* In case we tried to reuse a session but it failed */
424 if( ssl->session_negotiate->peer_cert != NULL )
425 {
426 mbedtls_x509_crt_free( ssl->session_negotiate->peer_cert );
427 mbedtls_free( ssl->session_negotiate->peer_cert );
428 }
429
XiaokangQianc3017f62022-05-13 05:55:41 +0000430 if( certificate_list_len == 0 )
431 {
432 ssl->session_negotiate->peer_cert = NULL;
433 ret = 0;
434 goto exit;
435 }
436
Xiaofei Bai947571e2021-09-29 09:12:03 +0000437 if( ( ssl->session_negotiate->peer_cert =
438 mbedtls_calloc( 1, sizeof( mbedtls_x509_crt ) ) ) == NULL )
439 {
440 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc( %" MBEDTLS_PRINTF_SIZET " bytes ) failed",
441 sizeof( mbedtls_x509_crt ) ) );
442 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR,
443 MBEDTLS_ERR_SSL_ALLOC_FAILED );
444 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
445 }
446
447 mbedtls_x509_crt_init( ssl->session_negotiate->peer_cert );
448
Ronald Cron2b1a43c2022-06-10 17:03:54 +0200449 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, certificate_list_len );
Xiaofei Bai947571e2021-09-29 09:12:03 +0000450 certificate_list_end = p + certificate_list_len;
Xiaofei Bai79595ac2021-10-26 07:16:45 +0000451 while( p < certificate_list_end )
Xiaofei Bai947571e2021-09-29 09:12:03 +0000452 {
453 size_t cert_data_len, extensions_len;
454
Xiaofei Bai79595ac2021-10-26 07:16:45 +0000455 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, certificate_list_end, 3 );
Xiaofei Baif93cbd22021-10-29 02:39:30 +0000456 cert_data_len = MBEDTLS_GET_UINT24_BE( p, 0 );
Xiaofei Bai947571e2021-09-29 09:12:03 +0000457 p += 3;
458
459 /* In theory, the CRT can be up to 2^24 Bytes, but we don't support
460 * anything beyond 2^16 = 64K. Otherwise as in the TLS 1.2 code,
461 * check that we have a minimum of 128 bytes of data, this is not
462 * clear why we need that though.
463 */
464 if( ( cert_data_len < 128 ) || ( cert_data_len >= 0x10000 ) )
Xiaofei Bai79595ac2021-10-26 07:16:45 +0000465 {
Xiaofei Bai947571e2021-09-29 09:12:03 +0000466 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad Certificate message" ) );
467 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR,
468 MBEDTLS_ERR_SSL_DECODE_ERROR );
469 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
470 }
471
Xiaofei Bai79595ac2021-10-26 07:16:45 +0000472 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, certificate_list_end, cert_data_len );
Xiaofei Bai947571e2021-09-29 09:12:03 +0000473 ret = mbedtls_x509_crt_parse_der( ssl->session_negotiate->peer_cert,
474 p, cert_data_len );
475
476 switch( ret )
477 {
478 case 0: /*ok*/
479 break;
480 case MBEDTLS_ERR_X509_UNKNOWN_SIG_ALG + MBEDTLS_ERR_OID_NOT_FOUND:
481 /* Ignore certificate with an unknown algorithm: maybe a
482 prior certificate was already trusted. */
483 break;
484
485 case MBEDTLS_ERR_X509_ALLOC_FAILED:
486 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR,
487 MBEDTLS_ERR_X509_ALLOC_FAILED );
488 MBEDTLS_SSL_DEBUG_RET( 1, " mbedtls_x509_crt_parse_der", ret );
489 return( ret );
490
491 case MBEDTLS_ERR_X509_UNKNOWN_VERSION:
492 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT,
493 MBEDTLS_ERR_X509_UNKNOWN_VERSION );
494 MBEDTLS_SSL_DEBUG_RET( 1, " mbedtls_x509_crt_parse_der", ret );
495 return( ret );
496
497 default:
498 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_BAD_CERT,
499 ret );
500 MBEDTLS_SSL_DEBUG_RET( 1, " mbedtls_x509_crt_parse_der", ret );
501 return( ret );
502 }
503
504 p += cert_data_len;
505
506 /* Certificate extensions length */
507 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, certificate_list_end, 2 );
508 extensions_len = MBEDTLS_GET_UINT16_BE( p, 0 );
509 p += 2;
Xiaofei Bai79595ac2021-10-26 07:16:45 +0000510 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, certificate_list_end, extensions_len );
Xiaofei Bai947571e2021-09-29 09:12:03 +0000511 p += extensions_len;
512 }
513
XiaokangQian63e713e2022-05-15 04:26:57 +0000514exit:
Xiaofei Bai947571e2021-09-29 09:12:03 +0000515 /* Check that all the message is consumed. */
516 if( p != end )
517 {
518 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad Certificate message" ) );
519 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR, \
520 MBEDTLS_ERR_SSL_DECODE_ERROR );
521 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
522 }
523
524 MBEDTLS_SSL_DEBUG_CRT( 3, "peer certificate", ssl->session_negotiate->peer_cert );
525
526 return( ret );
527}
528#else
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200529MBEDTLS_CHECK_RETURN_CRITICAL
Ronald Crone3dac4a2022-06-10 17:21:51 +0200530MBEDTLS_STATIC_TESTABLE
531int mbedtls_ssl_tls13_parse_certificate( mbedtls_ssl_context *ssl,
532 const unsigned char *buf,
533 const unsigned char *end )
Xiaofei Bai947571e2021-09-29 09:12:03 +0000534{
535 ((void) ssl);
536 ((void) buf);
537 ((void) end);
538 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
539}
540#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
541#endif /* MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED */
542
543#if defined(MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED)
544#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Xiaofei Bai79595ac2021-10-26 07:16:45 +0000545/* Validate certificate chain sent by the server. */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200546MBEDTLS_CHECK_RETURN_CRITICAL
Xiaofei Bai947571e2021-09-29 09:12:03 +0000547static int ssl_tls13_validate_certificate( mbedtls_ssl_context *ssl )
548{
549 int ret = 0;
XiaokangQian989f06d2022-05-17 01:50:15 +0000550 int authmode = MBEDTLS_SSL_VERIFY_REQUIRED;
Xiaofei Bai947571e2021-09-29 09:12:03 +0000551 mbedtls_x509_crt *ca_chain;
552 mbedtls_x509_crl *ca_crl;
Ronald Cron30c5a252022-06-16 19:31:06 +0200553 const char *ext_oid;
554 size_t ext_len;
Xiaofei Baiff456022021-10-28 06:50:17 +0000555 uint32_t verify_result = 0;
Xiaofei Bai947571e2021-09-29 09:12:03 +0000556
XiaokangQian6b916b12022-04-25 07:29:34 +0000557 /* If SNI was used, overwrite authentication mode
558 * from the configuration. */
XiaokangQian989f06d2022-05-17 01:50:15 +0000559#if defined(MBEDTLS_SSL_SRV_C)
560 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
XiaokangQiane7a5da52022-05-30 00:59:29 +0000561 authmode = ssl->conf->authmode;
XiaokangQian6b916b12022-04-25 07:29:34 +0000562#endif
563
564 /*
XiaokangQian989f06d2022-05-17 01:50:15 +0000565 * If the peer hasn't sent a certificate ( i.e. it sent
XiaokangQian6b916b12022-04-25 07:29:34 +0000566 * an empty certificate chain ), this is reflected in the peer CRT
567 * structure being unset.
568 * Check for that and handle it depending on the
XiaokangQian989f06d2022-05-17 01:50:15 +0000569 * authentication mode.
XiaokangQian6b916b12022-04-25 07:29:34 +0000570 */
XiaokangQian63e713e2022-05-15 04:26:57 +0000571 if( ssl->session_negotiate->peer_cert == NULL )
XiaokangQian6b916b12022-04-25 07:29:34 +0000572 {
XiaokangQian989f06d2022-05-17 01:50:15 +0000573 MBEDTLS_SSL_DEBUG_MSG( 1, ( "peer has not sent a certificate" ) );
574
XiaokangQian63e713e2022-05-15 04:26:57 +0000575#if defined(MBEDTLS_SSL_SRV_C)
576 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
577 {
XiaokangQian63e713e2022-05-15 04:26:57 +0000578 /* The client was asked for a certificate but didn't send
579 * one. The client should know what's going on, so we
580 * don't send an alert.
581 */
582 ssl->session_negotiate->verify_result = MBEDTLS_X509_BADCERT_MISSING;
583 if( authmode == MBEDTLS_SSL_VERIFY_OPTIONAL )
584 return( 0 );
585 else
XiaokangQian989f06d2022-05-17 01:50:15 +0000586 {
587 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_NO_CERT,
XiaokangQianaca90482022-05-19 07:19:31 +0000588 MBEDTLS_ERR_SSL_NO_CLIENT_CERTIFICATE );
589 return( MBEDTLS_ERR_SSL_NO_CLIENT_CERTIFICATE );
XiaokangQian989f06d2022-05-17 01:50:15 +0000590 }
XiaokangQian63e713e2022-05-15 04:26:57 +0000591 }
XiaokangQian6b916b12022-04-25 07:29:34 +0000592#endif /* MBEDTLS_SSL_SRV_C */
593
XiaokangQianc3017f62022-05-13 05:55:41 +0000594#if defined(MBEDTLS_SSL_CLI_C)
XiaokangQian63e713e2022-05-15 04:26:57 +0000595 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
596 {
597 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_NO_CERT,
598 MBEDTLS_ERR_SSL_FATAL_ALERT_MESSAGE );
599 return( MBEDTLS_ERR_SSL_FATAL_ALERT_MESSAGE );
600 }
XiaokangQianc3017f62022-05-13 05:55:41 +0000601#endif /* MBEDTLS_SSL_CLI_C */
XiaokangQian63e713e2022-05-15 04:26:57 +0000602 }
XiaokangQian6b916b12022-04-25 07:29:34 +0000603
Xiaofei Bai947571e2021-09-29 09:12:03 +0000604#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
605 if( ssl->handshake->sni_ca_chain != NULL )
606 {
607 ca_chain = ssl->handshake->sni_ca_chain;
608 ca_crl = ssl->handshake->sni_ca_crl;
609 }
610 else
611#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
612 {
613 ca_chain = ssl->conf->ca_chain;
614 ca_crl = ssl->conf->ca_crl;
615 }
616
617 /*
618 * Main check: verify certificate
619 */
620 ret = mbedtls_x509_crt_verify_with_profile(
621 ssl->session_negotiate->peer_cert,
622 ca_chain, ca_crl,
623 ssl->conf->cert_profile,
624 ssl->hostname,
Xiaofei Baiff456022021-10-28 06:50:17 +0000625 &verify_result,
Xiaofei Bai947571e2021-09-29 09:12:03 +0000626 ssl->conf->f_vrfy, ssl->conf->p_vrfy );
627
628 if( ret != 0 )
629 {
630 MBEDTLS_SSL_DEBUG_RET( 1, "x509_verify_cert", ret );
631 }
632
633 /*
634 * Secondary checks: always done, but change 'ret' only if it was 0
635 */
Ronald Cron30c5a252022-06-16 19:31:06 +0200636 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Xiaofei Bai947571e2021-09-29 09:12:03 +0000637 {
Ronald Cron30c5a252022-06-16 19:31:06 +0200638 ext_oid = MBEDTLS_OID_SERVER_AUTH;
639 ext_len = MBEDTLS_OID_SIZE( MBEDTLS_OID_SERVER_AUTH );
640 }
641 else
642 {
643 ext_oid = MBEDTLS_OID_CLIENT_AUTH;
644 ext_len = MBEDTLS_OID_SIZE( MBEDTLS_OID_CLIENT_AUTH );
645 }
646
647 if( ( mbedtls_x509_crt_check_key_usage(
648 ssl->session_negotiate->peer_cert,
649 MBEDTLS_X509_KU_DIGITAL_SIGNATURE ) != 0 ) ||
650 ( mbedtls_x509_crt_check_extended_key_usage(
651 ssl->session_negotiate->peer_cert,
652 ext_oid, ext_len ) != 0 ) )
653 {
654 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate (usage extensions)" ) );
Xiaofei Bai947571e2021-09-29 09:12:03 +0000655 if( ret == 0 )
656 ret = MBEDTLS_ERR_SSL_BAD_CERTIFICATE;
657 }
658
XiaokangQian6b916b12022-04-25 07:29:34 +0000659 /* mbedtls_x509_crt_verify_with_profile is supposed to report a
660 * verification failure through MBEDTLS_ERR_X509_CERT_VERIFY_FAILED,
661 * with details encoded in the verification flags. All other kinds
662 * of error codes, including those from the user provided f_vrfy
663 * functions, are treated as fatal and lead to a failure of
Ronald Crone3dac4a2022-06-10 17:21:51 +0200664 * mbedtls_ssl_tls13_parse_certificate even if verification was optional.
665 */
XiaokangQian6b916b12022-04-25 07:29:34 +0000666 if( authmode == MBEDTLS_SSL_VERIFY_OPTIONAL &&
667 ( ret == MBEDTLS_ERR_X509_CERT_VERIFY_FAILED ||
668 ret == MBEDTLS_ERR_SSL_BAD_CERTIFICATE ) )
669 {
670 ret = 0;
671 }
Xiaofei Bai947571e2021-09-29 09:12:03 +0000672
XiaokangQian6b916b12022-04-25 07:29:34 +0000673 if( ca_chain == NULL && authmode == MBEDTLS_SSL_VERIFY_REQUIRED )
Xiaofei Bai947571e2021-09-29 09:12:03 +0000674 {
675 MBEDTLS_SSL_DEBUG_MSG( 1, ( "got no CA chain" ) );
676 ret = MBEDTLS_ERR_SSL_CA_CHAIN_REQUIRED;
677 }
678
679 if( ret != 0 )
680 {
681 /* The certificate may have been rejected for several reasons.
682 Pick one and send the corresponding alert. Which alert to send
683 may be a subject of debate in some cases. */
Xiaofei Baiff456022021-10-28 06:50:17 +0000684 if( verify_result & MBEDTLS_X509_BADCERT_OTHER )
Xiaofei Bai947571e2021-09-29 09:12:03 +0000685 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED, ret );
Xiaofei Baiff456022021-10-28 06:50:17 +0000686 else if( verify_result & MBEDTLS_X509_BADCERT_CN_MISMATCH )
Xiaofei Bai947571e2021-09-29 09:12:03 +0000687 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_BAD_CERT, ret );
Xiaofei Baif93cbd22021-10-29 02:39:30 +0000688 else if( verify_result & ( MBEDTLS_X509_BADCERT_KEY_USAGE |
689 MBEDTLS_X509_BADCERT_EXT_KEY_USAGE |
690 MBEDTLS_X509_BADCERT_NS_CERT_TYPE |
691 MBEDTLS_X509_BADCERT_BAD_PK |
692 MBEDTLS_X509_BADCERT_BAD_KEY ) )
Xiaofei Bai947571e2021-09-29 09:12:03 +0000693 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT, ret );
Xiaofei Baiff456022021-10-28 06:50:17 +0000694 else if( verify_result & MBEDTLS_X509_BADCERT_EXPIRED )
Xiaofei Bai947571e2021-09-29 09:12:03 +0000695 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_CERT_EXPIRED, ret );
Xiaofei Baiff456022021-10-28 06:50:17 +0000696 else if( verify_result & MBEDTLS_X509_BADCERT_REVOKED )
Xiaofei Bai947571e2021-09-29 09:12:03 +0000697 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_CERT_REVOKED, ret );
Xiaofei Baiff456022021-10-28 06:50:17 +0000698 else if( verify_result & MBEDTLS_X509_BADCERT_NOT_TRUSTED )
Xiaofei Bai947571e2021-09-29 09:12:03 +0000699 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_UNKNOWN_CA, ret );
700 else
701 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_CERT_UNKNOWN, ret );
702 }
703
704#if defined(MBEDTLS_DEBUG_C)
Xiaofei Baiff456022021-10-28 06:50:17 +0000705 if( verify_result != 0 )
Xiaofei Bai947571e2021-09-29 09:12:03 +0000706 {
707 MBEDTLS_SSL_DEBUG_MSG( 3, ( "! Certificate verification flags %08x",
Jerry Yu83bb1312021-10-28 22:16:33 +0800708 (unsigned int) verify_result ) );
Xiaofei Bai947571e2021-09-29 09:12:03 +0000709 }
710 else
711 {
712 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Certificate verification flags clear" ) );
713 }
714#endif /* MBEDTLS_DEBUG_C */
715
Xiaofei Baiff456022021-10-28 06:50:17 +0000716 ssl->session_negotiate->verify_result = verify_result;
Xiaofei Bai947571e2021-09-29 09:12:03 +0000717 return( ret );
718}
719#else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200720MBEDTLS_CHECK_RETURN_CRITICAL
Xiaofei Bai947571e2021-09-29 09:12:03 +0000721static int ssl_tls13_validate_certificate( mbedtls_ssl_context *ssl )
722{
723 ((void) ssl);
724 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
725}
726#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
727#endif /* MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED */
728
Xiaofei Bai79595ac2021-10-26 07:16:45 +0000729int mbedtls_ssl_tls13_process_certificate( mbedtls_ssl_context *ssl )
Xiaofei Bai947571e2021-09-29 09:12:03 +0000730{
Xiaofei Bai79595ac2021-10-26 07:16:45 +0000731 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
732 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) );
733
734#if defined(MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED)
XiaokangQianc3017f62022-05-13 05:55:41 +0000735 unsigned char *buf;
736 size_t buf_len;
Xiaofei Bai79595ac2021-10-26 07:16:45 +0000737
XiaokangQianc3017f62022-05-13 05:55:41 +0000738 MBEDTLS_SSL_PROC_CHK( mbedtls_ssl_tls13_fetch_handshake_msg(
739 ssl, MBEDTLS_SSL_HS_CERTIFICATE,
740 &buf, &buf_len ) );
Xiaofei Bai79595ac2021-10-26 07:16:45 +0000741
XiaokangQianc3017f62022-05-13 05:55:41 +0000742 /* Parse the certificate chain sent by the peer. */
Ronald Crone3dac4a2022-06-10 17:21:51 +0200743 MBEDTLS_SSL_PROC_CHK( mbedtls_ssl_tls13_parse_certificate( ssl, buf,
744 buf + buf_len ) );
XiaokangQianc3017f62022-05-13 05:55:41 +0000745 /* Validate the certificate chain and set the verification results. */
746 MBEDTLS_SSL_PROC_CHK( ssl_tls13_validate_certificate( ssl ) );
Xiaofei Bai79595ac2021-10-26 07:16:45 +0000747
XiaokangQianc3017f62022-05-13 05:55:41 +0000748 mbedtls_ssl_add_hs_msg_to_checksum( ssl, MBEDTLS_SSL_HS_CERTIFICATE,
749 buf, buf_len );
Xiaofei Bai79595ac2021-10-26 07:16:45 +0000750
Xiaofei Bai79595ac2021-10-26 07:16:45 +0000751cleanup:
XiaokangQianaca90482022-05-19 07:19:31 +0000752#endif /* MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED */
Xiaofei Bai79595ac2021-10-26 07:16:45 +0000753
754 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse certificate" ) );
755 return( ret );
Xiaofei Bai947571e2021-09-29 09:12:03 +0000756}
Jerry Yu90f152d2022-01-29 22:12:42 +0800757#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Jerry Yu7399d0d2022-01-30 17:54:19 +0800758/*
759 * enum {
760 * X509(0),
761 * RawPublicKey(2),
762 * (255)
763 * } CertificateType;
764 *
765 * struct {
766 * select (certificate_type) {
767 * case RawPublicKey:
768 * // From RFC 7250 ASN.1_subjectPublicKeyInfo
769 * opaque ASN1_subjectPublicKeyInfo<1..2^24-1>;
770 *
771 * case X509:
772 * opaque cert_data<1..2^24-1>;
773 * };
774 * Extension extensions<0..2^16-1>;
775 * } CertificateEntry;
776 *
777 * struct {
778 * opaque certificate_request_context<0..2^8-1>;
779 * CertificateEntry certificate_list<0..2^24-1>;
780 * } Certificate;
781 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200782MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yu7399d0d2022-01-30 17:54:19 +0800783static int ssl_tls13_write_certificate_body( mbedtls_ssl_context *ssl,
Jerry Yu3e536442022-02-15 11:05:59 +0800784 unsigned char *buf,
Jerry Yu7399d0d2022-01-30 17:54:19 +0800785 unsigned char *end,
Jerry Yu3e536442022-02-15 11:05:59 +0800786 size_t *out_len )
Jerry Yu5cc35062022-01-28 16:16:08 +0800787{
Jerry Yu5cc35062022-01-28 16:16:08 +0800788 const mbedtls_x509_crt *crt = mbedtls_ssl_own_cert( ssl );
Jerry Yu3e536442022-02-15 11:05:59 +0800789 unsigned char *p = buf;
Jerry Yuc8d8d4e2022-02-18 12:10:03 +0800790 unsigned char *certificate_request_context =
791 ssl->handshake->certificate_request_context;
792 unsigned char certificate_request_context_len =
793 ssl->handshake->certificate_request_context_len;
794 unsigned char *p_certificate_list_len;
Jerry Yu5cc35062022-01-28 16:16:08 +0800795
Jerry Yu5cc35062022-01-28 16:16:08 +0800796
Jerry Yu3391ac02022-02-16 11:21:37 +0800797 /* ...
798 * opaque certificate_request_context<0..2^8-1>;
799 * ...
800 */
Jerry Yuc8d8d4e2022-02-18 12:10:03 +0800801 MBEDTLS_SSL_CHK_BUF_PTR( p, end, certificate_request_context_len + 1 );
802 *p++ = certificate_request_context_len;
803 if( certificate_request_context_len > 0 )
Jerry Yu537530d2022-02-15 14:00:57 +0800804 {
Jerry Yuc8d8d4e2022-02-18 12:10:03 +0800805 memcpy( p, certificate_request_context, certificate_request_context_len );
806 p += certificate_request_context_len;
Jerry Yu537530d2022-02-15 14:00:57 +0800807 }
808
Jerry Yu3391ac02022-02-16 11:21:37 +0800809 /* ...
810 * CertificateEntry certificate_list<0..2^24-1>;
811 * ...
812 */
Jerry Yu3e536442022-02-15 11:05:59 +0800813 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 3 );
Jerry Yuc8d8d4e2022-02-18 12:10:03 +0800814 p_certificate_list_len = p;
Jerry Yu3e536442022-02-15 11:05:59 +0800815 p += 3;
816
Jerry Yu7399d0d2022-01-30 17:54:19 +0800817 MBEDTLS_SSL_DEBUG_CRT( 3, "own certificate", crt );
Jerry Yu5cc35062022-01-28 16:16:08 +0800818
Jerry Yuc8d8d4e2022-02-18 12:10:03 +0800819 while( crt != NULL )
Jerry Yu5cc35062022-01-28 16:16:08 +0800820 {
Jerry Yu7399d0d2022-01-30 17:54:19 +0800821 size_t cert_data_len = crt->raw.len;
Jerry Yu5cc35062022-01-28 16:16:08 +0800822
Jerry Yu7399d0d2022-01-30 17:54:19 +0800823 MBEDTLS_SSL_CHK_BUF_PTR( p, end, cert_data_len + 3 + 2 );
824 MBEDTLS_PUT_UINT24_BE( cert_data_len, p, 0 );
825 p += 3;
Jerry Yu5cc35062022-01-28 16:16:08 +0800826
Jerry Yu7399d0d2022-01-30 17:54:19 +0800827 memcpy( p, crt->raw.p, cert_data_len );
828 p += cert_data_len;
829 crt = crt->next;
Jerry Yu5cc35062022-01-28 16:16:08 +0800830
831 /* Currently, we don't have any certificate extensions defined.
832 * Hence, we are sending an empty extension with length zero.
833 */
Jerry Yu7399d0d2022-01-30 17:54:19 +0800834 MBEDTLS_PUT_UINT24_BE( 0, p, 0 );
835 p += 2;
Jerry Yu5cc35062022-01-28 16:16:08 +0800836 }
Jerry Yu5cc35062022-01-28 16:16:08 +0800837
Jerry Yuc8d8d4e2022-02-18 12:10:03 +0800838 MBEDTLS_PUT_UINT24_BE( p - p_certificate_list_len - 3,
839 p_certificate_list_len, 0 );
Jerry Yu7399d0d2022-01-30 17:54:19 +0800840
Jerry Yu3e536442022-02-15 11:05:59 +0800841 *out_len = p - buf;
Jerry Yu5cc35062022-01-28 16:16:08 +0800842
843 return( 0 );
844}
Jerry Yu5cc35062022-01-28 16:16:08 +0800845
Jerry Yu3e536442022-02-15 11:05:59 +0800846int mbedtls_ssl_tls13_write_certificate( mbedtls_ssl_context *ssl )
Jerry Yu5cc35062022-01-28 16:16:08 +0800847{
848 int ret;
Ronald Cron5bb8fc82022-03-09 07:00:13 +0100849 unsigned char *buf;
850 size_t buf_len, msg_len;
851
Jerry Yu5cc35062022-01-28 16:16:08 +0800852 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write certificate" ) );
853
Ronald Cron8f6d39a2022-03-10 18:56:50 +0100854 MBEDTLS_SSL_PROC_CHK( mbedtls_ssl_start_handshake_msg( ssl,
Ronald Cron5bb8fc82022-03-09 07:00:13 +0100855 MBEDTLS_SSL_HS_CERTIFICATE, &buf, &buf_len ) );
Jerry Yu5cc35062022-01-28 16:16:08 +0800856
Ronald Cron5bb8fc82022-03-09 07:00:13 +0100857 MBEDTLS_SSL_PROC_CHK( ssl_tls13_write_certificate_body( ssl,
858 buf,
859 buf + buf_len,
860 &msg_len ) );
Jerry Yu5cc35062022-01-28 16:16:08 +0800861
Ronald Cron8f6d39a2022-03-10 18:56:50 +0100862 mbedtls_ssl_add_hs_msg_to_checksum( ssl, MBEDTLS_SSL_HS_CERTIFICATE,
863 buf, msg_len );
Jerry Yu5cc35062022-01-28 16:16:08 +0800864
Ronald Cron8f6d39a2022-03-10 18:56:50 +0100865 MBEDTLS_SSL_PROC_CHK( mbedtls_ssl_finish_handshake_msg(
Ronald Cron5bb8fc82022-03-09 07:00:13 +0100866 ssl, buf_len, msg_len ) );
Jerry Yu5cc35062022-01-28 16:16:08 +0800867cleanup:
868
869 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write certificate" ) );
870 return( ret );
871}
872
Jerry Yu3e536442022-02-15 11:05:59 +0800873/*
874 * STATE HANDLING: Output Certificate Verify
875 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200876MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yue91a51a2022-03-22 21:42:50 +0800877static int ssl_tls13_get_sig_alg_from_pk( mbedtls_ssl_context *ssl,
878 mbedtls_pk_context *own_key,
Jerry Yu8c338862022-03-23 13:34:04 +0800879 uint16_t *algorithm )
Jerry Yu67eced02022-02-25 13:37:36 +0800880{
881 mbedtls_pk_type_t sig = mbedtls_ssl_sig_from_pk( own_key );
882 /* Determine the size of the key */
883 size_t own_key_size = mbedtls_pk_get_bitlen( own_key );
Jerry Yue91a51a2022-03-22 21:42:50 +0800884 *algorithm = MBEDTLS_TLS1_3_SIG_NONE;
Jerry Yu67eced02022-02-25 13:37:36 +0800885 ((void) own_key_size);
886
887 switch( sig )
888 {
889#if defined(MBEDTLS_ECDSA_C)
890 case MBEDTLS_SSL_SIG_ECDSA:
891 switch( own_key_size )
892 {
893 case 256:
Jerry Yue91a51a2022-03-22 21:42:50 +0800894 *algorithm = MBEDTLS_TLS1_3_SIG_ECDSA_SECP256R1_SHA256;
Jerry Yue91a51a2022-03-22 21:42:50 +0800895 return( 0 );
Jerry Yu67eced02022-02-25 13:37:36 +0800896 case 384:
Jerry Yue91a51a2022-03-22 21:42:50 +0800897 *algorithm = MBEDTLS_TLS1_3_SIG_ECDSA_SECP384R1_SHA384;
Jerry Yue91a51a2022-03-22 21:42:50 +0800898 return( 0 );
Jerry Yu67eced02022-02-25 13:37:36 +0800899 case 521:
Jerry Yue91a51a2022-03-22 21:42:50 +0800900 *algorithm = MBEDTLS_TLS1_3_SIG_ECDSA_SECP521R1_SHA512;
Jerry Yue91a51a2022-03-22 21:42:50 +0800901 return( 0 );
Jerry Yu67eced02022-02-25 13:37:36 +0800902 default:
903 MBEDTLS_SSL_DEBUG_MSG( 3,
904 ( "unknown key size: %"
905 MBEDTLS_PRINTF_SIZET " bits",
906 own_key_size ) );
907 break;
908 }
909 break;
910#endif /* MBEDTLS_ECDSA_C */
911
Jerry Yucef3f332022-03-22 23:00:13 +0800912#if defined(MBEDTLS_RSA_C)
Jerry Yu67eced02022-02-25 13:37:36 +0800913 case MBEDTLS_SSL_SIG_RSA:
Jerry Yucef3f332022-03-22 23:00:13 +0800914#if defined(MBEDTLS_PKCS1_V21)
915#if defined(MBEDTLS_SHA256_C)
Jerry Yu67eced02022-02-25 13:37:36 +0800916 if( own_key_size <= 2048 &&
917 mbedtls_ssl_sig_alg_is_received( ssl,
918 MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA256 ) )
919 {
Jerry Yue91a51a2022-03-22 21:42:50 +0800920 *algorithm = MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA256;
Jerry Yue91a51a2022-03-22 21:42:50 +0800921 return( 0 );
Jerry Yu67eced02022-02-25 13:37:36 +0800922 }
Jerry Yucef3f332022-03-22 23:00:13 +0800923 else
924#endif /* MBEDTLS_SHA256_C */
925#if defined(MBEDTLS_SHA384_C)
926 if( own_key_size <= 3072 &&
927 mbedtls_ssl_sig_alg_is_received( ssl,
Jerry Yu67eced02022-02-25 13:37:36 +0800928 MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA384 ) )
929 {
Jerry Yue91a51a2022-03-22 21:42:50 +0800930 *algorithm = MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA384;
Jerry Yue91a51a2022-03-22 21:42:50 +0800931 return( 0 );
Jerry Yu67eced02022-02-25 13:37:36 +0800932 }
Jerry Yucef3f332022-03-22 23:00:13 +0800933 else
934#endif /* MBEDTLS_SHA384_C */
935#if defined(MBEDTLS_SHA512_C)
936 if( own_key_size <= 4096 &&
937 mbedtls_ssl_sig_alg_is_received( ssl,
Jerry Yu67eced02022-02-25 13:37:36 +0800938 MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA512 ) )
939 {
Jerry Yue91a51a2022-03-22 21:42:50 +0800940 *algorithm = MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA512;
Jerry Yue91a51a2022-03-22 21:42:50 +0800941 return( 0 );
Jerry Yu67eced02022-02-25 13:37:36 +0800942 }
Jerry Yucef3f332022-03-22 23:00:13 +0800943 else
944#endif /* MBEDTLS_SHA512_C */
945#endif /* MBEDTLS_PKCS1_V21 */
946#if defined(MBEDTLS_PKCS1_V15)
947#if defined(MBEDTLS_SHA256_C)
948 if( own_key_size <= 2048 &&
949 mbedtls_ssl_sig_alg_is_received( ssl,
950 MBEDTLS_TLS1_3_SIG_RSA_PKCS1_SHA256 ) )
951 {
952 *algorithm = MBEDTLS_TLS1_3_SIG_RSA_PKCS1_SHA256;
Jerry Yucef3f332022-03-22 23:00:13 +0800953 return( 0 );
954 }
955 else
956#endif /* MBEDTLS_SHA256_C */
957#if defined(MBEDTLS_SHA384_C)
958 if( own_key_size <= 3072 &&
959 mbedtls_ssl_sig_alg_is_received( ssl,
960 MBEDTLS_TLS1_3_SIG_RSA_PKCS1_SHA384 ) )
961 {
962 *algorithm = MBEDTLS_TLS1_3_SIG_RSA_PKCS1_SHA384;
Jerry Yucef3f332022-03-22 23:00:13 +0800963 return( 0 );
964 }
965 else
966#endif /* MBEDTLS_SHA384_C */
967#if defined(MBEDTLS_SHA512_C)
968 if( own_key_size <= 4096 &&
969 mbedtls_ssl_sig_alg_is_received( ssl,
970 MBEDTLS_TLS1_3_SIG_RSA_PKCS1_SHA512 ) )
971 {
972 *algorithm = MBEDTLS_TLS1_3_SIG_RSA_PKCS1_SHA512;
Jerry Yucef3f332022-03-22 23:00:13 +0800973 return( 0 );
974 }
975 else
976#endif /* MBEDTLS_SHA512_C */
977#endif /* MBEDTLS_PKCS1_V15 */
978 {
979 MBEDTLS_SSL_DEBUG_MSG( 3,
980 ( "unknown key size: %"
981 MBEDTLS_PRINTF_SIZET " bits",
982 own_key_size ) );
983 }
Jerry Yu67eced02022-02-25 13:37:36 +0800984 break;
Jerry Yucef3f332022-03-22 23:00:13 +0800985#endif /* MBEDTLS_RSA_C */
Jerry Yu67eced02022-02-25 13:37:36 +0800986 default:
987 MBEDTLS_SSL_DEBUG_MSG( 1,
Andrzej Kurek5c65c572022-04-13 14:28:52 -0400988 ( "unknown signature type : %u", sig ) );
Jerry Yu67eced02022-02-25 13:37:36 +0800989 break;
990 }
Jerry Yue91a51a2022-03-22 21:42:50 +0800991 return( -1 );
Jerry Yu67eced02022-02-25 13:37:36 +0800992}
993
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200994MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yu3e536442022-02-15 11:05:59 +0800995static int ssl_tls13_write_certificate_verify_body( mbedtls_ssl_context *ssl,
996 unsigned char *buf,
997 unsigned char *end,
998 size_t *out_len )
Jerry Yu8511f122022-01-29 10:01:04 +0800999{
1000 int ret;
Jerry Yu3e536442022-02-15 11:05:59 +08001001 unsigned char *p = buf;
Jerry Yu8511f122022-01-29 10:01:04 +08001002 mbedtls_pk_context *own_key;
Jerry Yu3e536442022-02-15 11:05:59 +08001003
Jerry Yu8511f122022-01-29 10:01:04 +08001004 unsigned char handshake_hash[ MBEDTLS_TLS1_3_MD_MAX_SIZE ];
1005 size_t handshake_hash_len;
Jerry Yu3e536442022-02-15 11:05:59 +08001006 unsigned char verify_buffer[ SSL_VERIFY_STRUCT_MAX_SIZE ];
1007 size_t verify_buffer_len;
Jerry Yu67eced02022-02-25 13:37:36 +08001008 mbedtls_pk_type_t pk_type = MBEDTLS_PK_NONE;
Jerry Yu67eced02022-02-25 13:37:36 +08001009 mbedtls_md_type_t md_alg = MBEDTLS_MD_NONE;
pespacek34935872022-05-20 15:43:32 +02001010 psa_algorithm_t psa_algorithm = PSA_ALG_NONE;
Jerry Yu78272072022-02-22 10:28:13 +08001011 uint16_t algorithm = MBEDTLS_TLS1_3_SIG_NONE;
Jerry Yu3e536442022-02-15 11:05:59 +08001012 size_t signature_len = 0;
Jerry Yu3391ac02022-02-16 11:21:37 +08001013 unsigned char verify_hash[ MBEDTLS_MD_MAX_SIZE ];
Jerry Yu3e536442022-02-15 11:05:59 +08001014 size_t verify_hash_len;
pespacekb06acd72022-06-07 13:07:21 +02001015 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Jerry Yu8511f122022-01-29 10:01:04 +08001016
Jerry Yu0b7b1012022-02-23 12:23:05 +08001017 *out_len = 0;
1018
Jerry Yu3e536442022-02-15 11:05:59 +08001019 own_key = mbedtls_ssl_own_key( ssl );
1020 if( own_key == NULL )
Jerry Yu8511f122022-01-29 10:01:04 +08001021 {
Jerry Yu3e536442022-02-15 11:05:59 +08001022 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1023 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Jerry Yu8511f122022-01-29 10:01:04 +08001024 }
1025
Jerry Yu8511f122022-01-29 10:01:04 +08001026 ret = mbedtls_ssl_get_handshake_transcript( ssl,
1027 ssl->handshake->ciphersuite_info->mac,
1028 handshake_hash,
1029 sizeof( handshake_hash ),
1030 &handshake_hash_len );
1031 if( ret != 0 )
1032 return( ret );
1033
1034 MBEDTLS_SSL_DEBUG_BUF( 3, "handshake hash",
1035 handshake_hash,
1036 handshake_hash_len);
1037
Jerry Yu8511f122022-01-29 10:01:04 +08001038 ssl_tls13_create_verify_structure( handshake_hash, handshake_hash_len,
1039 verify_buffer, &verify_buffer_len,
1040 ssl->conf->endpoint );
1041
1042 /*
1043 * struct {
1044 * SignatureScheme algorithm;
1045 * opaque signature<0..2^16-1>;
1046 * } CertificateVerify;
1047 */
Jerry Yu8c338862022-03-23 13:34:04 +08001048 ret = ssl_tls13_get_sig_alg_from_pk( ssl, own_key, &algorithm );
Jerry Yue91a51a2022-03-22 21:42:50 +08001049 if( ret != 0 || ! mbedtls_ssl_sig_alg_is_received( ssl, algorithm ) )
Jerry Yu8511f122022-01-29 10:01:04 +08001050 {
Jerry Yud66409a2022-02-18 16:42:24 +08001051 MBEDTLS_SSL_DEBUG_MSG( 1,
Jerry Yu2124d052022-02-18 21:07:18 +08001052 ( "signature algorithm not in received or offered list." ) );
Jerry Yu67eced02022-02-25 13:37:36 +08001053
1054 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Signature algorithm is %s",
1055 mbedtls_ssl_sig_alg_to_str( algorithm ) ) );
1056
Jerry Yu71f36f12022-02-23 17:34:29 +08001057 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE,
Jerry Yud66409a2022-02-18 16:42:24 +08001058 MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
Jerry Yu3391ac02022-02-16 11:21:37 +08001059 return( MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
Jerry Yu8511f122022-01-29 10:01:04 +08001060 }
1061
Jerry Yu6c6f1022022-03-25 11:09:50 +08001062 if( mbedtls_ssl_tls13_get_pk_type_and_md_alg_from_sig_alg(
1063 algorithm, &pk_type, &md_alg ) != 0 )
Jerry Yu8c338862022-03-23 13:34:04 +08001064 {
Jerry Yuf8aa9a42022-03-23 20:40:28 +08001065 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Jerry Yu8c338862022-03-23 13:34:04 +08001066 }
1067
Jerry Yu3391ac02022-02-16 11:21:37 +08001068 /* Check there is space for the algorithm identifier (2 bytes) and the
1069 * signature length (2 bytes).
1070 */
1071 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 4 );
Jerry Yu3e536442022-02-15 11:05:59 +08001072 MBEDTLS_PUT_UINT16_BE( algorithm, p, 0 );
1073 p += 2;
Jerry Yu8511f122022-01-29 10:01:04 +08001074
1075 /* Hash verify buffer with indicated hash function */
pespacek34935872022-05-20 15:43:32 +02001076 psa_algorithm = mbedtls_psa_translate_md( md_alg );
pespacekb06acd72022-06-07 13:07:21 +02001077 status = psa_hash_compute( psa_algorithm,
1078 verify_buffer,
1079 verify_buffer_len,
1080 verify_hash,sizeof( verify_hash ),
1081 &verify_hash_len );
1082 if( status != PSA_SUCCESS )
pespacek670913f2022-06-07 10:53:39 +02001083 return( psa_ssl_status_to_mbedtls( status ) );
Jerry Yu8511f122022-01-29 10:01:04 +08001084
Jerry Yu8511f122022-01-29 10:01:04 +08001085 MBEDTLS_SSL_DEBUG_BUF( 3, "verify hash", verify_hash, verify_hash_len );
1086
Jerry Yu8beb9e12022-03-12 16:23:53 +08001087 if( ( ret = mbedtls_pk_sign_ext( pk_type, own_key,
1088 md_alg, verify_hash, verify_hash_len,
Jerry Yu67eced02022-02-25 13:37:36 +08001089 p + 2, (size_t)( end - ( p + 2 ) ), &signature_len,
1090 ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 )
Jerry Yu8511f122022-01-29 10:01:04 +08001091 {
1092 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_pk_sign", ret );
1093 return( ret );
1094 }
Jerry Yu8511f122022-01-29 10:01:04 +08001095
Jerry Yu3e536442022-02-15 11:05:59 +08001096 MBEDTLS_PUT_UINT16_BE( signature_len, p, 0 );
1097 p += 2 + signature_len;
Jerry Yu8511f122022-01-29 10:01:04 +08001098
Jerry Yu3e536442022-02-15 11:05:59 +08001099 *out_len = (size_t)( p - buf );
1100
Jerry Yu8511f122022-01-29 10:01:04 +08001101 return( ret );
1102}
Jerry Yu8511f122022-01-29 10:01:04 +08001103
Jerry Yu3e536442022-02-15 11:05:59 +08001104int mbedtls_ssl_tls13_write_certificate_verify( mbedtls_ssl_context *ssl )
Jerry Yu8511f122022-01-29 10:01:04 +08001105{
1106 int ret = 0;
Jerry Yuca133a32022-02-15 14:22:05 +08001107 unsigned char *buf;
1108 size_t buf_len, msg_len;
1109
Jerry Yu8511f122022-01-29 10:01:04 +08001110 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write certificate verify" ) );
1111
Ronald Cron8f6d39a2022-03-10 18:56:50 +01001112 MBEDTLS_SSL_PROC_CHK( mbedtls_ssl_start_handshake_msg( ssl,
Jerry Yuca133a32022-02-15 14:22:05 +08001113 MBEDTLS_SSL_HS_CERTIFICATE_VERIFY, &buf, &buf_len ) );
Jerry Yu8511f122022-01-29 10:01:04 +08001114
Jerry Yuca133a32022-02-15 14:22:05 +08001115 MBEDTLS_SSL_PROC_CHK( ssl_tls13_write_certificate_verify_body(
1116 ssl, buf, buf + buf_len, &msg_len ) );
Jerry Yu8511f122022-01-29 10:01:04 +08001117
Ronald Cron8f6d39a2022-03-10 18:56:50 +01001118 mbedtls_ssl_add_hs_msg_to_checksum( ssl, MBEDTLS_SSL_HS_CERTIFICATE_VERIFY,
1119 buf, msg_len );
Jerry Yu8511f122022-01-29 10:01:04 +08001120
Ronald Cron8f6d39a2022-03-10 18:56:50 +01001121 MBEDTLS_SSL_PROC_CHK( mbedtls_ssl_finish_handshake_msg(
Jerry Yuca133a32022-02-15 14:22:05 +08001122 ssl, buf_len, msg_len ) );
Jerry Yu8511f122022-01-29 10:01:04 +08001123
1124cleanup:
1125
1126 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write certificate verify" ) );
1127 return( ret );
1128}
1129
Jerry Yu90f152d2022-01-29 22:12:42 +08001130#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
1131
Jerry Yu5cc35062022-01-28 16:16:08 +08001132/*
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001133 *
XiaokangQianc5c39d52021-11-09 11:55:10 +00001134 * STATE HANDLING: Incoming Finished message.
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001135 */
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001136/*
1137 * Implementation
1138 */
1139
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001140MBEDTLS_CHECK_RETURN_CRITICAL
XiaokangQianaaa0e192021-11-10 03:07:04 +00001141static int ssl_tls13_preprocess_finished_message( mbedtls_ssl_context *ssl )
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001142{
1143 int ret;
1144
XiaokangQianc5c39d52021-11-09 11:55:10 +00001145 ret = mbedtls_ssl_tls13_calculate_verify_data( ssl,
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001146 ssl->handshake->state_local.finished_in.digest,
1147 sizeof( ssl->handshake->state_local.finished_in.digest ),
1148 &ssl->handshake->state_local.finished_in.digest_len,
XiaokangQianc5c39d52021-11-09 11:55:10 +00001149 ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT ?
XiaokangQianc13f9352021-11-11 06:13:22 +00001150 MBEDTLS_SSL_IS_SERVER : MBEDTLS_SSL_IS_CLIENT );
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001151 if( ret != 0 )
1152 {
XiaokangQianc5c39d52021-11-09 11:55:10 +00001153 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_tls13_calculate_verify_data", ret );
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001154 return( ret );
1155 }
1156
1157 return( 0 );
1158}
1159
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001160MBEDTLS_CHECK_RETURN_CRITICAL
XiaokangQianc5c39d52021-11-09 11:55:10 +00001161static int ssl_tls13_parse_finished_message( mbedtls_ssl_context *ssl,
1162 const unsigned char *buf,
1163 const unsigned char *end )
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001164{
XiaokangQian33062842021-11-11 03:37:45 +00001165 /*
1166 * struct {
XiaokangQianc13f9352021-11-11 06:13:22 +00001167 * opaque verify_data[Hash.length];
XiaokangQian33062842021-11-11 03:37:45 +00001168 * } Finished;
1169 */
1170 const unsigned char *expected_verify_data =
1171 ssl->handshake->state_local.finished_in.digest;
1172 size_t expected_verify_data_len =
1173 ssl->handshake->state_local.finished_in.digest_len;
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001174 /* Structural validation */
XiaokangQian33062842021-11-11 03:37:45 +00001175 if( (size_t)( end - buf ) != expected_verify_data_len )
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001176 {
1177 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
1178
1179 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR,
XiaokangQianc13f9352021-11-11 06:13:22 +00001180 MBEDTLS_ERR_SSL_DECODE_ERROR );
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001181 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
1182 }
1183
XiaokangQianc5c39d52021-11-09 11:55:10 +00001184 MBEDTLS_SSL_DEBUG_BUF( 4, "verify_data (self-computed):",
XiaokangQian33062842021-11-11 03:37:45 +00001185 expected_verify_data,
1186 expected_verify_data_len );
XiaokangQianc5c39d52021-11-09 11:55:10 +00001187 MBEDTLS_SSL_DEBUG_BUF( 4, "verify_data (received message):", buf,
XiaokangQian33062842021-11-11 03:37:45 +00001188 expected_verify_data_len );
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001189
1190 /* Semantic validation */
Gabor Mezei685472b2021-11-24 11:17:36 +01001191 if( mbedtls_ct_memcmp( buf,
1192 expected_verify_data,
1193 expected_verify_data_len ) != 0 )
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001194 {
1195 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
1196
XiaokangQian33062842021-11-11 03:37:45 +00001197 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_DECRYPT_ERROR,
XiaokangQianc13f9352021-11-11 06:13:22 +00001198 MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001199 return( MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
1200 }
1201 return( 0 );
1202}
1203
XiaokangQianc5c39d52021-11-09 11:55:10 +00001204int mbedtls_ssl_tls13_process_finished_message( mbedtls_ssl_context *ssl )
1205{
XiaokangQian33062842021-11-11 03:37:45 +00001206 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
XiaokangQianc5c39d52021-11-09 11:55:10 +00001207 unsigned char *buf;
Xiaofei Baieef15042021-11-18 07:29:56 +00001208 size_t buf_len;
XiaokangQianc5c39d52021-11-09 11:55:10 +00001209
XiaokangQiand0aa3e92021-11-10 06:17:40 +00001210 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse finished message" ) );
XiaokangQianc5c39d52021-11-09 11:55:10 +00001211
Xiaofei Bai746f9482021-11-12 08:53:56 +00001212 MBEDTLS_SSL_PROC_CHK( mbedtls_ssl_tls13_fetch_handshake_msg( ssl,
XiaokangQianc5c39d52021-11-09 11:55:10 +00001213 MBEDTLS_SSL_HS_FINISHED,
Xiaofei Baieef15042021-11-18 07:29:56 +00001214 &buf, &buf_len ) );
Jerry Yu0a92d6c2022-05-16 16:54:46 +08001215
1216 /* Preprocessing step: Compute handshake digest */
1217 MBEDTLS_SSL_PROC_CHK( ssl_tls13_preprocess_finished_message( ssl ) );
1218
Xiaofei Baieef15042021-11-18 07:29:56 +00001219 MBEDTLS_SSL_PROC_CHK( ssl_tls13_parse_finished_message( ssl, buf, buf + buf_len ) );
Jerry Yu0a92d6c2022-05-16 16:54:46 +08001220
Ronald Cron8f6d39a2022-03-10 18:56:50 +01001221 mbedtls_ssl_add_hs_msg_to_checksum( ssl, MBEDTLS_SSL_HS_FINISHED,
1222 buf, buf_len );
XiaokangQianc5c39d52021-11-09 11:55:10 +00001223
1224cleanup:
1225
XiaokangQiand0aa3e92021-11-10 06:17:40 +00001226 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse finished message" ) );
XiaokangQianc5c39d52021-11-09 11:55:10 +00001227 return( ret );
1228}
1229
XiaokangQian74af2a82021-09-22 07:40:30 +00001230/*
1231 *
XiaokangQiancc90c942021-11-09 12:30:09 +00001232 * STATE HANDLING: Write and send Finished message.
XiaokangQian74af2a82021-09-22 07:40:30 +00001233 *
1234 */
XiaokangQian74af2a82021-09-22 07:40:30 +00001235/*
XiaokangQian35dc6252021-11-11 08:16:19 +00001236 * Implement
XiaokangQian74af2a82021-09-22 07:40:30 +00001237 */
1238
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001239MBEDTLS_CHECK_RETURN_CRITICAL
XiaokangQian8773aa02021-11-10 07:33:09 +00001240static int ssl_tls13_prepare_finished_message( mbedtls_ssl_context *ssl )
XiaokangQian74af2a82021-09-22 07:40:30 +00001241{
1242 int ret;
1243
1244 /* Compute transcript of handshake up to now. */
XiaokangQiancc90c942021-11-09 12:30:09 +00001245 ret = mbedtls_ssl_tls13_calculate_verify_data( ssl,
XiaokangQian74af2a82021-09-22 07:40:30 +00001246 ssl->handshake->state_local.finished_out.digest,
1247 sizeof( ssl->handshake->state_local.finished_out.digest ),
1248 &ssl->handshake->state_local.finished_out.digest_len,
1249 ssl->conf->endpoint );
1250
1251 if( ret != 0 )
1252 {
Jerry Yu7ca30542021-12-08 15:57:57 +08001253 MBEDTLS_SSL_DEBUG_RET( 1, "calculate_verify_data failed", ret );
XiaokangQian74af2a82021-09-22 07:40:30 +00001254 return( ret );
1255 }
1256
1257 return( 0 );
1258}
1259
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001260MBEDTLS_CHECK_RETURN_CRITICAL
XiaokangQian8773aa02021-11-10 07:33:09 +00001261static int ssl_tls13_write_finished_message_body( mbedtls_ssl_context *ssl,
XiaokangQian35dc6252021-11-11 08:16:19 +00001262 unsigned char *buf,
1263 unsigned char *end,
Xiaofei Baid25fab62021-12-02 06:36:27 +00001264 size_t *out_len )
XiaokangQian74af2a82021-09-22 07:40:30 +00001265{
XiaokangQian8773aa02021-11-10 07:33:09 +00001266 size_t verify_data_len = ssl->handshake->state_local.finished_out.digest_len;
XiaokangQian0fa66432021-11-15 03:33:57 +00001267 /*
1268 * struct {
1269 * opaque verify_data[Hash.length];
1270 * } Finished;
1271 */
XiaokangQian8773aa02021-11-10 07:33:09 +00001272 MBEDTLS_SSL_CHK_BUF_PTR( buf, end, verify_data_len );
XiaokangQian74af2a82021-09-22 07:40:30 +00001273
1274 memcpy( buf, ssl->handshake->state_local.finished_out.digest,
XiaokangQian8773aa02021-11-10 07:33:09 +00001275 verify_data_len );
XiaokangQian74af2a82021-09-22 07:40:30 +00001276
Xiaofei Baid25fab62021-12-02 06:36:27 +00001277 *out_len = verify_data_len;
XiaokangQian74af2a82021-09-22 07:40:30 +00001278 return( 0 );
1279}
XiaokangQianc5c39d52021-11-09 11:55:10 +00001280
XiaokangQian35dc6252021-11-11 08:16:19 +00001281/* Main entry point: orchestrates the other functions */
1282int mbedtls_ssl_tls13_write_finished_message( mbedtls_ssl_context *ssl )
1283{
1284 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1285 unsigned char *buf;
1286 size_t buf_len, msg_len;
1287
1288 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write finished message" ) );
1289
XiaokangQiandce82242021-11-15 06:01:26 +00001290 MBEDTLS_SSL_PROC_CHK( ssl_tls13_prepare_finished_message( ssl ) );
1291
Ronald Cron8f6d39a2022-03-10 18:56:50 +01001292 MBEDTLS_SSL_PROC_CHK( mbedtls_ssl_start_handshake_msg( ssl,
XiaokangQian35dc6252021-11-11 08:16:19 +00001293 MBEDTLS_SSL_HS_FINISHED, &buf, &buf_len ) );
1294
1295 MBEDTLS_SSL_PROC_CHK( ssl_tls13_write_finished_message_body(
1296 ssl, buf, buf + buf_len, &msg_len ) );
1297
Ronald Cron8f6d39a2022-03-10 18:56:50 +01001298 mbedtls_ssl_add_hs_msg_to_checksum( ssl, MBEDTLS_SSL_HS_FINISHED,
1299 buf, msg_len );
XiaokangQian35dc6252021-11-11 08:16:19 +00001300
Ronald Cron8f6d39a2022-03-10 18:56:50 +01001301 MBEDTLS_SSL_PROC_CHK( mbedtls_ssl_finish_handshake_msg(
1302 ssl, buf_len, msg_len ) );
XiaokangQian35dc6252021-11-11 08:16:19 +00001303cleanup:
1304
1305 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write finished message" ) );
1306 return( ret );
1307}
1308
Jerry Yu378254d2021-10-30 21:44:47 +08001309void mbedtls_ssl_tls13_handshake_wrapup( mbedtls_ssl_context *ssl )
1310{
1311
1312 MBEDTLS_SSL_DEBUG_MSG( 3, ( "=> handshake wrapup" ) );
1313
Jerry Yue8c1fca2022-05-18 14:48:56 +08001314 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Switch to application keys for inbound traffic" ) );
1315 mbedtls_ssl_set_inbound_transform ( ssl, ssl->transform_application );
1316
1317 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Switch to application keys for outbound traffic" ) );
1318 mbedtls_ssl_set_outbound_transform( ssl, ssl->transform_application );
1319
Jerry Yu378254d2021-10-30 21:44:47 +08001320 /*
Jerry Yucfe64f02021-11-15 13:54:06 +08001321 * Free the previous session and switch to the current one.
Jerry Yu378254d2021-10-30 21:44:47 +08001322 */
1323 if( ssl->session )
1324 {
Jerry Yu378254d2021-10-30 21:44:47 +08001325 mbedtls_ssl_session_free( ssl->session );
1326 mbedtls_free( ssl->session );
1327 }
1328 ssl->session = ssl->session_negotiate;
1329 ssl->session_negotiate = NULL;
1330
1331 MBEDTLS_SSL_DEBUG_MSG( 3, ( "<= handshake wrapup" ) );
1332}
1333
Ronald Cron49ad6192021-11-24 16:25:31 +01001334/*
1335 *
1336 * STATE HANDLING: Write ChangeCipherSpec
1337 *
1338 */
1339#if defined(MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001340MBEDTLS_CHECK_RETURN_CRITICAL
Ronald Cron49ad6192021-11-24 16:25:31 +01001341static int ssl_tls13_write_change_cipher_spec_body( mbedtls_ssl_context *ssl,
1342 unsigned char *buf,
1343 unsigned char *end,
1344 size_t *olen )
1345{
1346 ((void) ssl);
1347
1348 MBEDTLS_SSL_CHK_BUF_PTR( buf, end, 1 );
1349 buf[0] = 1;
1350 *olen = 1;
1351
1352 return( 0 );
1353}
1354
Ronald Cron49ad6192021-11-24 16:25:31 +01001355int mbedtls_ssl_tls13_write_change_cipher_spec( mbedtls_ssl_context *ssl )
1356{
1357 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1358
1359 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write change cipher spec" ) );
1360
Ronald Cron49ad6192021-11-24 16:25:31 +01001361 /* Write CCS message */
1362 MBEDTLS_SSL_PROC_CHK( ssl_tls13_write_change_cipher_spec_body(
1363 ssl, ssl->out_msg,
1364 ssl->out_msg + MBEDTLS_SSL_OUT_CONTENT_LEN,
1365 &ssl->out_msglen ) );
1366
1367 ssl->out_msgtype = MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC;
1368
Ronald Cron49ad6192021-11-24 16:25:31 +01001369 /* Dispatch message */
Ronald Cron66dbf912022-02-02 15:33:46 +01001370 MBEDTLS_SSL_PROC_CHK( mbedtls_ssl_write_record( ssl, 0 ) );
Ronald Cron49ad6192021-11-24 16:25:31 +01001371
1372cleanup:
1373
1374 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write change cipher spec" ) );
1375 return( ret );
1376}
1377
1378#endif /* MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE */
1379
XiaokangQian78b1fa72022-01-19 06:56:30 +00001380/* Reset SSL context and update hash for handling HRR.
1381 *
1382 * Replace Transcript-Hash(X) by
1383 * Transcript-Hash( message_hash ||
1384 * 00 00 Hash.length ||
1385 * X )
1386 * A few states of the handshake are preserved, including:
1387 * - session ID
1388 * - session ticket
1389 * - negotiated ciphersuite
1390 */
1391int mbedtls_ssl_reset_transcript_for_hrr( mbedtls_ssl_context *ssl )
1392{
1393 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1394 unsigned char hash_transcript[ MBEDTLS_MD_MAX_SIZE + 4 ];
XiaokangQian0ece9982022-01-24 08:56:23 +00001395 size_t hash_len;
XiaokangQian78b1fa72022-01-19 06:56:30 +00001396 const mbedtls_ssl_ciphersuite_t *ciphersuite_info;
1397 uint16_t cipher_suite = ssl->session_negotiate->ciphersuite;
1398 ciphersuite_info = mbedtls_ssl_ciphersuite_from_id( cipher_suite );
1399
1400 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Reset SSL session for HRR" ) );
1401
XiaokangQian0ece9982022-01-24 08:56:23 +00001402 ret = mbedtls_ssl_get_handshake_transcript( ssl, ciphersuite_info->mac,
1403 hash_transcript + 4,
1404 MBEDTLS_MD_MAX_SIZE,
1405 &hash_len );
1406 if( ret != 0 )
1407 {
1408 MBEDTLS_SSL_DEBUG_RET( 4, "mbedtls_ssl_get_handshake_transcript", ret );
1409 return( ret );
1410 }
1411
1412 hash_transcript[0] = MBEDTLS_SSL_HS_MESSAGE_HASH;
1413 hash_transcript[1] = 0;
1414 hash_transcript[2] = 0;
1415 hash_transcript[3] = (unsigned char) hash_len;
1416
1417 hash_len += 4;
1418
XiaokangQian78b1fa72022-01-19 06:56:30 +00001419 if( ciphersuite_info->mac == MBEDTLS_MD_SHA256 )
1420 {
1421#if defined(MBEDTLS_SHA256_C)
XiaokangQian78b1fa72022-01-19 06:56:30 +00001422 MBEDTLS_SSL_DEBUG_BUF( 4, "Truncated SHA-256 handshake transcript",
XiaokangQian0ece9982022-01-24 08:56:23 +00001423 hash_transcript, hash_len );
XiaokangQian78b1fa72022-01-19 06:56:30 +00001424
1425#if defined(MBEDTLS_USE_PSA_CRYPTO)
1426 psa_hash_abort( &ssl->handshake->fin_sha256_psa );
1427 psa_hash_setup( &ssl->handshake->fin_sha256_psa, PSA_ALG_SHA_256 );
1428#else
1429 mbedtls_sha256_starts( &ssl->handshake->fin_sha256, 0 );
1430#endif
XiaokangQian78b1fa72022-01-19 06:56:30 +00001431#endif /* MBEDTLS_SHA256_C */
1432 }
1433 else if( ciphersuite_info->mac == MBEDTLS_MD_SHA384 )
1434 {
1435#if defined(MBEDTLS_SHA384_C)
XiaokangQian78b1fa72022-01-19 06:56:30 +00001436 MBEDTLS_SSL_DEBUG_BUF( 4, "Truncated SHA-384 handshake transcript",
XiaokangQian0ece9982022-01-24 08:56:23 +00001437 hash_transcript, hash_len );
XiaokangQian78b1fa72022-01-19 06:56:30 +00001438
1439#if defined(MBEDTLS_USE_PSA_CRYPTO)
1440 psa_hash_abort( &ssl->handshake->fin_sha384_psa );
1441 psa_hash_setup( &ssl->handshake->fin_sha384_psa, PSA_ALG_SHA_384 );
1442#else
1443 mbedtls_sha512_starts( &ssl->handshake->fin_sha512, 1 );
1444#endif
XiaokangQian78b1fa72022-01-19 06:56:30 +00001445#endif /* MBEDTLS_SHA384_C */
1446 }
1447
XiaokangQian0ece9982022-01-24 08:56:23 +00001448#if defined(MBEDTLS_SHA256_C) || defined(MBEDTLS_SHA384_C)
1449 ssl->handshake->update_checksum( ssl, hash_transcript, hash_len );
1450#endif /* MBEDTLS_SHA256_C || MBEDTLS_SHA384_C */
Przemyslaw Stekiel4b3fff42022-02-14 16:39:52 +01001451
XiaokangQian78b1fa72022-01-19 06:56:30 +00001452 return( ret );
1453}
1454
XiaokangQian9b5d04b2022-04-10 10:20:43 +00001455#if defined(MBEDTLS_ECDH_C)
XiaokangQian7807f9f2022-02-15 10:04:37 +00001456
XiaokangQian9b5d04b2022-04-10 10:20:43 +00001457int mbedtls_ssl_tls13_read_public_ecdhe_share( mbedtls_ssl_context *ssl,
1458 const unsigned char *buf,
1459 size_t buf_len )
XiaokangQian7807f9f2022-02-15 10:04:37 +00001460{
XiaokangQian9b5d04b2022-04-10 10:20:43 +00001461 uint8_t *p = (uint8_t*)buf;
XiaokangQiancfd925f2022-04-14 07:10:37 +00001462 const uint8_t *end = buf + buf_len;
XiaokangQian9b5d04b2022-04-10 10:20:43 +00001463 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001464
XiaokangQian9b5d04b2022-04-10 10:20:43 +00001465 /* Get size of the TLS opaque key_exchange field of the KeyShareEntry struct. */
XiaokangQiancfd925f2022-04-14 07:10:37 +00001466 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 2 );
XiaokangQian9b5d04b2022-04-10 10:20:43 +00001467 uint16_t peerkey_len = MBEDTLS_GET_UINT16_BE( p, 0 );
1468 p += 2;
XiaokangQian3207a322022-02-23 03:15:27 +00001469
XiaokangQian9b5d04b2022-04-10 10:20:43 +00001470 /* Check if key size is consistent with given buffer length. */
XiaokangQiancfd925f2022-04-14 07:10:37 +00001471 MBEDTLS_SSL_CHK_BUF_PTR( p, end, peerkey_len );
XiaokangQian9b5d04b2022-04-10 10:20:43 +00001472
1473 /* Store peer's ECDH public key. */
1474 memcpy( handshake->ecdh_psa_peerkey, p, peerkey_len );
1475 handshake->ecdh_psa_peerkey_len = peerkey_len;
1476
XiaokangQian3207a322022-02-23 03:15:27 +00001477 return( 0 );
1478}
Jerry Yu89e103c2022-03-30 22:43:29 +08001479
1480int mbedtls_ssl_tls13_generate_and_write_ecdh_key_exchange(
1481 mbedtls_ssl_context *ssl,
1482 uint16_t named_group,
1483 unsigned char *buf,
1484 unsigned char *end,
1485 size_t *out_len )
1486{
1487 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
1488 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
1489 psa_key_attributes_t key_attributes;
1490 size_t own_pubkey_len;
1491 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
1492 size_t ecdh_bits = 0;
1493
1494 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Perform PSA-based ECDH computation." ) );
1495
1496 /* Convert EC group to PSA key type. */
1497 if( ( handshake->ecdh_psa_type =
1498 mbedtls_psa_parse_tls_ecc_group( named_group, &ecdh_bits ) ) == 0 )
1499 return( MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
1500
1501 ssl->handshake->ecdh_bits = ecdh_bits;
1502
1503 key_attributes = psa_key_attributes_init();
1504 psa_set_key_usage_flags( &key_attributes, PSA_KEY_USAGE_DERIVE );
1505 psa_set_key_algorithm( &key_attributes, PSA_ALG_ECDH );
1506 psa_set_key_type( &key_attributes, handshake->ecdh_psa_type );
1507 psa_set_key_bits( &key_attributes, handshake->ecdh_bits );
1508
1509 /* Generate ECDH private key. */
1510 status = psa_generate_key( &key_attributes,
1511 &handshake->ecdh_psa_privkey );
1512 if( status != PSA_SUCCESS )
1513 {
1514 ret = psa_ssl_status_to_mbedtls( status );
1515 MBEDTLS_SSL_DEBUG_RET( 1, "psa_generate_key", ret );
1516 return( ret );
1517
1518 }
1519
1520 /* Export the public part of the ECDH private key from PSA. */
1521 status = psa_export_public_key( handshake->ecdh_psa_privkey,
1522 buf, (size_t)( end - buf ),
1523 &own_pubkey_len );
1524 if( status != PSA_SUCCESS )
1525 {
1526 ret = psa_ssl_status_to_mbedtls( status );
1527 MBEDTLS_SSL_DEBUG_RET( 1, "psa_export_public_key", ret );
1528 return( ret );
1529
1530 }
1531
1532 *out_len = own_pubkey_len;
1533
1534 return( 0 );
1535}
XiaokangQian9b5d04b2022-04-10 10:20:43 +00001536#endif /* MBEDTLS_ECDH_C */
XiaokangQian7807f9f2022-02-15 10:04:37 +00001537
Jerry Yufb4b6472022-01-27 15:03:26 +08001538#endif /* MBEDTLS_SSL_TLS_C && MBEDTLS_SSL_PROTO_TLS1_3 */