blob: 2ddefc3d4434346c0cc299eeda210883417b9a2f [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
Xiaofei Bai947571e2021-09-29 09:12:03 +0000449 certificate_list_end = p + certificate_list_len;
Xiaofei Bai79595ac2021-10-26 07:16:45 +0000450 while( p < certificate_list_end )
Xiaofei Bai947571e2021-09-29 09:12:03 +0000451 {
452 size_t cert_data_len, extensions_len;
453
Xiaofei Bai79595ac2021-10-26 07:16:45 +0000454 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, certificate_list_end, 3 );
Xiaofei Baif93cbd22021-10-29 02:39:30 +0000455 cert_data_len = MBEDTLS_GET_UINT24_BE( p, 0 );
Xiaofei Bai947571e2021-09-29 09:12:03 +0000456 p += 3;
457
458 /* In theory, the CRT can be up to 2^24 Bytes, but we don't support
459 * anything beyond 2^16 = 64K. Otherwise as in the TLS 1.2 code,
460 * check that we have a minimum of 128 bytes of data, this is not
461 * clear why we need that though.
462 */
463 if( ( cert_data_len < 128 ) || ( cert_data_len >= 0x10000 ) )
Xiaofei Bai79595ac2021-10-26 07:16:45 +0000464 {
Xiaofei Bai947571e2021-09-29 09:12:03 +0000465 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad Certificate message" ) );
466 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR,
467 MBEDTLS_ERR_SSL_DECODE_ERROR );
468 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
469 }
470
Xiaofei Bai79595ac2021-10-26 07:16:45 +0000471 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, certificate_list_end, cert_data_len );
Xiaofei Bai947571e2021-09-29 09:12:03 +0000472 ret = mbedtls_x509_crt_parse_der( ssl->session_negotiate->peer_cert,
473 p, cert_data_len );
474
475 switch( ret )
476 {
477 case 0: /*ok*/
478 break;
479 case MBEDTLS_ERR_X509_UNKNOWN_SIG_ALG + MBEDTLS_ERR_OID_NOT_FOUND:
480 /* Ignore certificate with an unknown algorithm: maybe a
481 prior certificate was already trusted. */
482 break;
483
484 case MBEDTLS_ERR_X509_ALLOC_FAILED:
485 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR,
486 MBEDTLS_ERR_X509_ALLOC_FAILED );
487 MBEDTLS_SSL_DEBUG_RET( 1, " mbedtls_x509_crt_parse_der", ret );
488 return( ret );
489
490 case MBEDTLS_ERR_X509_UNKNOWN_VERSION:
491 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT,
492 MBEDTLS_ERR_X509_UNKNOWN_VERSION );
493 MBEDTLS_SSL_DEBUG_RET( 1, " mbedtls_x509_crt_parse_der", ret );
494 return( ret );
495
496 default:
497 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_BAD_CERT,
498 ret );
499 MBEDTLS_SSL_DEBUG_RET( 1, " mbedtls_x509_crt_parse_der", ret );
500 return( ret );
501 }
502
503 p += cert_data_len;
504
505 /* Certificate extensions length */
506 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, certificate_list_end, 2 );
507 extensions_len = MBEDTLS_GET_UINT16_BE( p, 0 );
508 p += 2;
Xiaofei Bai79595ac2021-10-26 07:16:45 +0000509 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, certificate_list_end, extensions_len );
Xiaofei Bai947571e2021-09-29 09:12:03 +0000510 p += extensions_len;
511 }
512
XiaokangQian63e713e2022-05-15 04:26:57 +0000513exit:
Xiaofei Bai947571e2021-09-29 09:12:03 +0000514 /* Check that all the message is consumed. */
515 if( p != end )
516 {
517 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad Certificate message" ) );
518 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR, \
519 MBEDTLS_ERR_SSL_DECODE_ERROR );
520 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
521 }
522
523 MBEDTLS_SSL_DEBUG_CRT( 3, "peer certificate", ssl->session_negotiate->peer_cert );
524
525 return( ret );
526}
527#else
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200528MBEDTLS_CHECK_RETURN_CRITICAL
Ronald Crone3dac4a2022-06-10 17:21:51 +0200529MBEDTLS_STATIC_TESTABLE
530int mbedtls_ssl_tls13_parse_certificate( mbedtls_ssl_context *ssl,
531 const unsigned char *buf,
532 const unsigned char *end )
Xiaofei Bai947571e2021-09-29 09:12:03 +0000533{
534 ((void) ssl);
535 ((void) buf);
536 ((void) end);
537 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
538}
539#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
540#endif /* MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED */
541
542#if defined(MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED)
543#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Xiaofei Bai79595ac2021-10-26 07:16:45 +0000544/* Validate certificate chain sent by the server. */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200545MBEDTLS_CHECK_RETURN_CRITICAL
Xiaofei Bai947571e2021-09-29 09:12:03 +0000546static int ssl_tls13_validate_certificate( mbedtls_ssl_context *ssl )
547{
548 int ret = 0;
XiaokangQian989f06d2022-05-17 01:50:15 +0000549 int authmode = MBEDTLS_SSL_VERIFY_REQUIRED;
Xiaofei Bai947571e2021-09-29 09:12:03 +0000550 mbedtls_x509_crt *ca_chain;
551 mbedtls_x509_crl *ca_crl;
Ronald Cron30c5a252022-06-16 19:31:06 +0200552 const char *ext_oid;
553 size_t ext_len;
Xiaofei Baiff456022021-10-28 06:50:17 +0000554 uint32_t verify_result = 0;
Xiaofei Bai947571e2021-09-29 09:12:03 +0000555
XiaokangQian6b916b12022-04-25 07:29:34 +0000556 /* If SNI was used, overwrite authentication mode
557 * from the configuration. */
XiaokangQian989f06d2022-05-17 01:50:15 +0000558#if defined(MBEDTLS_SSL_SRV_C)
559 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
XiaokangQiane7a5da52022-05-30 00:59:29 +0000560 authmode = ssl->conf->authmode;
XiaokangQian6b916b12022-04-25 07:29:34 +0000561#endif
562
563 /*
XiaokangQian989f06d2022-05-17 01:50:15 +0000564 * If the peer hasn't sent a certificate ( i.e. it sent
XiaokangQian6b916b12022-04-25 07:29:34 +0000565 * an empty certificate chain ), this is reflected in the peer CRT
566 * structure being unset.
567 * Check for that and handle it depending on the
XiaokangQian989f06d2022-05-17 01:50:15 +0000568 * authentication mode.
XiaokangQian6b916b12022-04-25 07:29:34 +0000569 */
XiaokangQian63e713e2022-05-15 04:26:57 +0000570 if( ssl->session_negotiate->peer_cert == NULL )
XiaokangQian6b916b12022-04-25 07:29:34 +0000571 {
XiaokangQian989f06d2022-05-17 01:50:15 +0000572 MBEDTLS_SSL_DEBUG_MSG( 1, ( "peer has not sent a certificate" ) );
573
XiaokangQian63e713e2022-05-15 04:26:57 +0000574#if defined(MBEDTLS_SSL_SRV_C)
575 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
576 {
XiaokangQian63e713e2022-05-15 04:26:57 +0000577 /* The client was asked for a certificate but didn't send
578 * one. The client should know what's going on, so we
579 * don't send an alert.
580 */
581 ssl->session_negotiate->verify_result = MBEDTLS_X509_BADCERT_MISSING;
582 if( authmode == MBEDTLS_SSL_VERIFY_OPTIONAL )
583 return( 0 );
584 else
XiaokangQian989f06d2022-05-17 01:50:15 +0000585 {
586 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_NO_CERT,
XiaokangQianaca90482022-05-19 07:19:31 +0000587 MBEDTLS_ERR_SSL_NO_CLIENT_CERTIFICATE );
588 return( MBEDTLS_ERR_SSL_NO_CLIENT_CERTIFICATE );
XiaokangQian989f06d2022-05-17 01:50:15 +0000589 }
XiaokangQian63e713e2022-05-15 04:26:57 +0000590 }
XiaokangQian6b916b12022-04-25 07:29:34 +0000591#endif /* MBEDTLS_SSL_SRV_C */
592
XiaokangQianc3017f62022-05-13 05:55:41 +0000593#if defined(MBEDTLS_SSL_CLI_C)
XiaokangQian63e713e2022-05-15 04:26:57 +0000594 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
595 {
596 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_NO_CERT,
597 MBEDTLS_ERR_SSL_FATAL_ALERT_MESSAGE );
598 return( MBEDTLS_ERR_SSL_FATAL_ALERT_MESSAGE );
599 }
XiaokangQianc3017f62022-05-13 05:55:41 +0000600#endif /* MBEDTLS_SSL_CLI_C */
XiaokangQian63e713e2022-05-15 04:26:57 +0000601 }
XiaokangQian6b916b12022-04-25 07:29:34 +0000602
Xiaofei Bai947571e2021-09-29 09:12:03 +0000603#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
604 if( ssl->handshake->sni_ca_chain != NULL )
605 {
606 ca_chain = ssl->handshake->sni_ca_chain;
607 ca_crl = ssl->handshake->sni_ca_crl;
608 }
609 else
610#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
611 {
612 ca_chain = ssl->conf->ca_chain;
613 ca_crl = ssl->conf->ca_crl;
614 }
615
616 /*
617 * Main check: verify certificate
618 */
619 ret = mbedtls_x509_crt_verify_with_profile(
620 ssl->session_negotiate->peer_cert,
621 ca_chain, ca_crl,
622 ssl->conf->cert_profile,
623 ssl->hostname,
Xiaofei Baiff456022021-10-28 06:50:17 +0000624 &verify_result,
Xiaofei Bai947571e2021-09-29 09:12:03 +0000625 ssl->conf->f_vrfy, ssl->conf->p_vrfy );
626
627 if( ret != 0 )
628 {
629 MBEDTLS_SSL_DEBUG_RET( 1, "x509_verify_cert", ret );
630 }
631
632 /*
633 * Secondary checks: always done, but change 'ret' only if it was 0
634 */
Ronald Cron30c5a252022-06-16 19:31:06 +0200635 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Xiaofei Bai947571e2021-09-29 09:12:03 +0000636 {
Ronald Cron30c5a252022-06-16 19:31:06 +0200637 ext_oid = MBEDTLS_OID_SERVER_AUTH;
638 ext_len = MBEDTLS_OID_SIZE( MBEDTLS_OID_SERVER_AUTH );
639 }
640 else
641 {
642 ext_oid = MBEDTLS_OID_CLIENT_AUTH;
643 ext_len = MBEDTLS_OID_SIZE( MBEDTLS_OID_CLIENT_AUTH );
644 }
645
646 if( ( mbedtls_x509_crt_check_key_usage(
647 ssl->session_negotiate->peer_cert,
648 MBEDTLS_X509_KU_DIGITAL_SIGNATURE ) != 0 ) ||
649 ( mbedtls_x509_crt_check_extended_key_usage(
650 ssl->session_negotiate->peer_cert,
651 ext_oid, ext_len ) != 0 ) )
652 {
653 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate (usage extensions)" ) );
Xiaofei Bai947571e2021-09-29 09:12:03 +0000654 if( ret == 0 )
655 ret = MBEDTLS_ERR_SSL_BAD_CERTIFICATE;
656 }
657
XiaokangQian6b916b12022-04-25 07:29:34 +0000658 /* mbedtls_x509_crt_verify_with_profile is supposed to report a
659 * verification failure through MBEDTLS_ERR_X509_CERT_VERIFY_FAILED,
660 * with details encoded in the verification flags. All other kinds
661 * of error codes, including those from the user provided f_vrfy
662 * functions, are treated as fatal and lead to a failure of
Ronald Crone3dac4a2022-06-10 17:21:51 +0200663 * mbedtls_ssl_tls13_parse_certificate even if verification was optional.
664 */
XiaokangQian6b916b12022-04-25 07:29:34 +0000665 if( authmode == MBEDTLS_SSL_VERIFY_OPTIONAL &&
666 ( ret == MBEDTLS_ERR_X509_CERT_VERIFY_FAILED ||
667 ret == MBEDTLS_ERR_SSL_BAD_CERTIFICATE ) )
668 {
669 ret = 0;
670 }
Xiaofei Bai947571e2021-09-29 09:12:03 +0000671
XiaokangQian6b916b12022-04-25 07:29:34 +0000672 if( ca_chain == NULL && authmode == MBEDTLS_SSL_VERIFY_REQUIRED )
Xiaofei Bai947571e2021-09-29 09:12:03 +0000673 {
674 MBEDTLS_SSL_DEBUG_MSG( 1, ( "got no CA chain" ) );
675 ret = MBEDTLS_ERR_SSL_CA_CHAIN_REQUIRED;
676 }
677
678 if( ret != 0 )
679 {
680 /* The certificate may have been rejected for several reasons.
681 Pick one and send the corresponding alert. Which alert to send
682 may be a subject of debate in some cases. */
Xiaofei Baiff456022021-10-28 06:50:17 +0000683 if( verify_result & MBEDTLS_X509_BADCERT_OTHER )
Xiaofei Bai947571e2021-09-29 09:12:03 +0000684 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED, ret );
Xiaofei Baiff456022021-10-28 06:50:17 +0000685 else if( verify_result & MBEDTLS_X509_BADCERT_CN_MISMATCH )
Xiaofei Bai947571e2021-09-29 09:12:03 +0000686 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_BAD_CERT, ret );
Xiaofei Baif93cbd22021-10-29 02:39:30 +0000687 else if( verify_result & ( MBEDTLS_X509_BADCERT_KEY_USAGE |
688 MBEDTLS_X509_BADCERT_EXT_KEY_USAGE |
689 MBEDTLS_X509_BADCERT_NS_CERT_TYPE |
690 MBEDTLS_X509_BADCERT_BAD_PK |
691 MBEDTLS_X509_BADCERT_BAD_KEY ) )
Xiaofei Bai947571e2021-09-29 09:12:03 +0000692 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT, ret );
Xiaofei Baiff456022021-10-28 06:50:17 +0000693 else if( verify_result & MBEDTLS_X509_BADCERT_EXPIRED )
Xiaofei Bai947571e2021-09-29 09:12:03 +0000694 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_CERT_EXPIRED, ret );
Xiaofei Baiff456022021-10-28 06:50:17 +0000695 else if( verify_result & MBEDTLS_X509_BADCERT_REVOKED )
Xiaofei Bai947571e2021-09-29 09:12:03 +0000696 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_CERT_REVOKED, ret );
Xiaofei Baiff456022021-10-28 06:50:17 +0000697 else if( verify_result & MBEDTLS_X509_BADCERT_NOT_TRUSTED )
Xiaofei Bai947571e2021-09-29 09:12:03 +0000698 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_UNKNOWN_CA, ret );
699 else
700 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_CERT_UNKNOWN, ret );
701 }
702
703#if defined(MBEDTLS_DEBUG_C)
Xiaofei Baiff456022021-10-28 06:50:17 +0000704 if( verify_result != 0 )
Xiaofei Bai947571e2021-09-29 09:12:03 +0000705 {
706 MBEDTLS_SSL_DEBUG_MSG( 3, ( "! Certificate verification flags %08x",
Jerry Yu83bb1312021-10-28 22:16:33 +0800707 (unsigned int) verify_result ) );
Xiaofei Bai947571e2021-09-29 09:12:03 +0000708 }
709 else
710 {
711 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Certificate verification flags clear" ) );
712 }
713#endif /* MBEDTLS_DEBUG_C */
714
Xiaofei Baiff456022021-10-28 06:50:17 +0000715 ssl->session_negotiate->verify_result = verify_result;
Xiaofei Bai947571e2021-09-29 09:12:03 +0000716 return( ret );
717}
718#else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200719MBEDTLS_CHECK_RETURN_CRITICAL
Xiaofei Bai947571e2021-09-29 09:12:03 +0000720static int ssl_tls13_validate_certificate( mbedtls_ssl_context *ssl )
721{
722 ((void) ssl);
723 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
724}
725#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
726#endif /* MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED */
727
Xiaofei Bai79595ac2021-10-26 07:16:45 +0000728int mbedtls_ssl_tls13_process_certificate( mbedtls_ssl_context *ssl )
Xiaofei Bai947571e2021-09-29 09:12:03 +0000729{
Xiaofei Bai79595ac2021-10-26 07:16:45 +0000730 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
731 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) );
732
733#if defined(MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED)
XiaokangQianc3017f62022-05-13 05:55:41 +0000734 unsigned char *buf;
735 size_t buf_len;
Xiaofei Bai79595ac2021-10-26 07:16:45 +0000736
XiaokangQianc3017f62022-05-13 05:55:41 +0000737 MBEDTLS_SSL_PROC_CHK( mbedtls_ssl_tls13_fetch_handshake_msg(
738 ssl, MBEDTLS_SSL_HS_CERTIFICATE,
739 &buf, &buf_len ) );
Xiaofei Bai79595ac2021-10-26 07:16:45 +0000740
XiaokangQianc3017f62022-05-13 05:55:41 +0000741 /* Parse the certificate chain sent by the peer. */
Ronald Crone3dac4a2022-06-10 17:21:51 +0200742 MBEDTLS_SSL_PROC_CHK( mbedtls_ssl_tls13_parse_certificate( ssl, buf,
743 buf + buf_len ) );
XiaokangQianc3017f62022-05-13 05:55:41 +0000744 /* Validate the certificate chain and set the verification results. */
745 MBEDTLS_SSL_PROC_CHK( ssl_tls13_validate_certificate( ssl ) );
Xiaofei Bai79595ac2021-10-26 07:16:45 +0000746
XiaokangQianc3017f62022-05-13 05:55:41 +0000747 mbedtls_ssl_add_hs_msg_to_checksum( ssl, MBEDTLS_SSL_HS_CERTIFICATE,
748 buf, buf_len );
Xiaofei Bai79595ac2021-10-26 07:16:45 +0000749
Xiaofei Bai79595ac2021-10-26 07:16:45 +0000750cleanup:
XiaokangQianaca90482022-05-19 07:19:31 +0000751#endif /* MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED */
Xiaofei Bai79595ac2021-10-26 07:16:45 +0000752
753 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse certificate" ) );
754 return( ret );
Xiaofei Bai947571e2021-09-29 09:12:03 +0000755}
Jerry Yu90f152d2022-01-29 22:12:42 +0800756#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Jerry Yu7399d0d2022-01-30 17:54:19 +0800757/*
758 * enum {
759 * X509(0),
760 * RawPublicKey(2),
761 * (255)
762 * } CertificateType;
763 *
764 * struct {
765 * select (certificate_type) {
766 * case RawPublicKey:
767 * // From RFC 7250 ASN.1_subjectPublicKeyInfo
768 * opaque ASN1_subjectPublicKeyInfo<1..2^24-1>;
769 *
770 * case X509:
771 * opaque cert_data<1..2^24-1>;
772 * };
773 * Extension extensions<0..2^16-1>;
774 * } CertificateEntry;
775 *
776 * struct {
777 * opaque certificate_request_context<0..2^8-1>;
778 * CertificateEntry certificate_list<0..2^24-1>;
779 * } Certificate;
780 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200781MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yu7399d0d2022-01-30 17:54:19 +0800782static int ssl_tls13_write_certificate_body( mbedtls_ssl_context *ssl,
Jerry Yu3e536442022-02-15 11:05:59 +0800783 unsigned char *buf,
Jerry Yu7399d0d2022-01-30 17:54:19 +0800784 unsigned char *end,
Jerry Yu3e536442022-02-15 11:05:59 +0800785 size_t *out_len )
Jerry Yu5cc35062022-01-28 16:16:08 +0800786{
Jerry Yu5cc35062022-01-28 16:16:08 +0800787 const mbedtls_x509_crt *crt = mbedtls_ssl_own_cert( ssl );
Jerry Yu3e536442022-02-15 11:05:59 +0800788 unsigned char *p = buf;
Jerry Yuc8d8d4e2022-02-18 12:10:03 +0800789 unsigned char *certificate_request_context =
790 ssl->handshake->certificate_request_context;
791 unsigned char certificate_request_context_len =
792 ssl->handshake->certificate_request_context_len;
793 unsigned char *p_certificate_list_len;
Jerry Yu5cc35062022-01-28 16:16:08 +0800794
Jerry Yu5cc35062022-01-28 16:16:08 +0800795
Jerry Yu3391ac02022-02-16 11:21:37 +0800796 /* ...
797 * opaque certificate_request_context<0..2^8-1>;
798 * ...
799 */
Jerry Yuc8d8d4e2022-02-18 12:10:03 +0800800 MBEDTLS_SSL_CHK_BUF_PTR( p, end, certificate_request_context_len + 1 );
801 *p++ = certificate_request_context_len;
802 if( certificate_request_context_len > 0 )
Jerry Yu537530d2022-02-15 14:00:57 +0800803 {
Jerry Yuc8d8d4e2022-02-18 12:10:03 +0800804 memcpy( p, certificate_request_context, certificate_request_context_len );
805 p += certificate_request_context_len;
Jerry Yu537530d2022-02-15 14:00:57 +0800806 }
807
Jerry Yu3391ac02022-02-16 11:21:37 +0800808 /* ...
809 * CertificateEntry certificate_list<0..2^24-1>;
810 * ...
811 */
Jerry Yu3e536442022-02-15 11:05:59 +0800812 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 3 );
Jerry Yuc8d8d4e2022-02-18 12:10:03 +0800813 p_certificate_list_len = p;
Jerry Yu3e536442022-02-15 11:05:59 +0800814 p += 3;
815
Jerry Yu7399d0d2022-01-30 17:54:19 +0800816 MBEDTLS_SSL_DEBUG_CRT( 3, "own certificate", crt );
Jerry Yu5cc35062022-01-28 16:16:08 +0800817
Jerry Yuc8d8d4e2022-02-18 12:10:03 +0800818 while( crt != NULL )
Jerry Yu5cc35062022-01-28 16:16:08 +0800819 {
Jerry Yu7399d0d2022-01-30 17:54:19 +0800820 size_t cert_data_len = crt->raw.len;
Jerry Yu5cc35062022-01-28 16:16:08 +0800821
Jerry Yu7399d0d2022-01-30 17:54:19 +0800822 MBEDTLS_SSL_CHK_BUF_PTR( p, end, cert_data_len + 3 + 2 );
823 MBEDTLS_PUT_UINT24_BE( cert_data_len, p, 0 );
824 p += 3;
Jerry Yu5cc35062022-01-28 16:16:08 +0800825
Jerry Yu7399d0d2022-01-30 17:54:19 +0800826 memcpy( p, crt->raw.p, cert_data_len );
827 p += cert_data_len;
828 crt = crt->next;
Jerry Yu5cc35062022-01-28 16:16:08 +0800829
830 /* Currently, we don't have any certificate extensions defined.
831 * Hence, we are sending an empty extension with length zero.
832 */
Jerry Yu7399d0d2022-01-30 17:54:19 +0800833 MBEDTLS_PUT_UINT24_BE( 0, p, 0 );
834 p += 2;
Jerry Yu5cc35062022-01-28 16:16:08 +0800835 }
Jerry Yu5cc35062022-01-28 16:16:08 +0800836
Jerry Yuc8d8d4e2022-02-18 12:10:03 +0800837 MBEDTLS_PUT_UINT24_BE( p - p_certificate_list_len - 3,
838 p_certificate_list_len, 0 );
Jerry Yu7399d0d2022-01-30 17:54:19 +0800839
Jerry Yu3e536442022-02-15 11:05:59 +0800840 *out_len = p - buf;
Jerry Yu5cc35062022-01-28 16:16:08 +0800841
842 return( 0 );
843}
Jerry Yu5cc35062022-01-28 16:16:08 +0800844
Jerry Yu3e536442022-02-15 11:05:59 +0800845int mbedtls_ssl_tls13_write_certificate( mbedtls_ssl_context *ssl )
Jerry Yu5cc35062022-01-28 16:16:08 +0800846{
847 int ret;
Ronald Cron5bb8fc82022-03-09 07:00:13 +0100848 unsigned char *buf;
849 size_t buf_len, msg_len;
850
Jerry Yu5cc35062022-01-28 16:16:08 +0800851 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write certificate" ) );
852
Ronald Cron8f6d39a2022-03-10 18:56:50 +0100853 MBEDTLS_SSL_PROC_CHK( mbedtls_ssl_start_handshake_msg( ssl,
Ronald Cron5bb8fc82022-03-09 07:00:13 +0100854 MBEDTLS_SSL_HS_CERTIFICATE, &buf, &buf_len ) );
Jerry Yu5cc35062022-01-28 16:16:08 +0800855
Ronald Cron5bb8fc82022-03-09 07:00:13 +0100856 MBEDTLS_SSL_PROC_CHK( ssl_tls13_write_certificate_body( ssl,
857 buf,
858 buf + buf_len,
859 &msg_len ) );
Jerry Yu5cc35062022-01-28 16:16:08 +0800860
Ronald Cron8f6d39a2022-03-10 18:56:50 +0100861 mbedtls_ssl_add_hs_msg_to_checksum( ssl, MBEDTLS_SSL_HS_CERTIFICATE,
862 buf, msg_len );
Jerry Yu5cc35062022-01-28 16:16:08 +0800863
Ronald Cron8f6d39a2022-03-10 18:56:50 +0100864 MBEDTLS_SSL_PROC_CHK( mbedtls_ssl_finish_handshake_msg(
Ronald Cron5bb8fc82022-03-09 07:00:13 +0100865 ssl, buf_len, msg_len ) );
Jerry Yu5cc35062022-01-28 16:16:08 +0800866cleanup:
867
868 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write certificate" ) );
869 return( ret );
870}
871
Jerry Yu3e536442022-02-15 11:05:59 +0800872/*
873 * STATE HANDLING: Output Certificate Verify
874 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200875MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yue91a51a2022-03-22 21:42:50 +0800876static int ssl_tls13_get_sig_alg_from_pk( mbedtls_ssl_context *ssl,
877 mbedtls_pk_context *own_key,
Jerry Yu8c338862022-03-23 13:34:04 +0800878 uint16_t *algorithm )
Jerry Yu67eced02022-02-25 13:37:36 +0800879{
880 mbedtls_pk_type_t sig = mbedtls_ssl_sig_from_pk( own_key );
881 /* Determine the size of the key */
882 size_t own_key_size = mbedtls_pk_get_bitlen( own_key );
Jerry Yue91a51a2022-03-22 21:42:50 +0800883 *algorithm = MBEDTLS_TLS1_3_SIG_NONE;
Jerry Yu67eced02022-02-25 13:37:36 +0800884 ((void) own_key_size);
885
886 switch( sig )
887 {
888#if defined(MBEDTLS_ECDSA_C)
889 case MBEDTLS_SSL_SIG_ECDSA:
890 switch( own_key_size )
891 {
892 case 256:
Jerry Yue91a51a2022-03-22 21:42:50 +0800893 *algorithm = MBEDTLS_TLS1_3_SIG_ECDSA_SECP256R1_SHA256;
Jerry Yue91a51a2022-03-22 21:42:50 +0800894 return( 0 );
Jerry Yu67eced02022-02-25 13:37:36 +0800895 case 384:
Jerry Yue91a51a2022-03-22 21:42:50 +0800896 *algorithm = MBEDTLS_TLS1_3_SIG_ECDSA_SECP384R1_SHA384;
Jerry Yue91a51a2022-03-22 21:42:50 +0800897 return( 0 );
Jerry Yu67eced02022-02-25 13:37:36 +0800898 case 521:
Jerry Yue91a51a2022-03-22 21:42:50 +0800899 *algorithm = MBEDTLS_TLS1_3_SIG_ECDSA_SECP521R1_SHA512;
Jerry Yue91a51a2022-03-22 21:42:50 +0800900 return( 0 );
Jerry Yu67eced02022-02-25 13:37:36 +0800901 default:
902 MBEDTLS_SSL_DEBUG_MSG( 3,
903 ( "unknown key size: %"
904 MBEDTLS_PRINTF_SIZET " bits",
905 own_key_size ) );
906 break;
907 }
908 break;
909#endif /* MBEDTLS_ECDSA_C */
910
Jerry Yucef3f332022-03-22 23:00:13 +0800911#if defined(MBEDTLS_RSA_C)
Jerry Yu67eced02022-02-25 13:37:36 +0800912 case MBEDTLS_SSL_SIG_RSA:
Jerry Yucef3f332022-03-22 23:00:13 +0800913#if defined(MBEDTLS_PKCS1_V21)
914#if defined(MBEDTLS_SHA256_C)
Jerry Yu67eced02022-02-25 13:37:36 +0800915 if( own_key_size <= 2048 &&
916 mbedtls_ssl_sig_alg_is_received( ssl,
917 MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA256 ) )
918 {
Jerry Yue91a51a2022-03-22 21:42:50 +0800919 *algorithm = MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA256;
Jerry Yue91a51a2022-03-22 21:42:50 +0800920 return( 0 );
Jerry Yu67eced02022-02-25 13:37:36 +0800921 }
Jerry Yucef3f332022-03-22 23:00:13 +0800922 else
923#endif /* MBEDTLS_SHA256_C */
924#if defined(MBEDTLS_SHA384_C)
925 if( own_key_size <= 3072 &&
926 mbedtls_ssl_sig_alg_is_received( ssl,
Jerry Yu67eced02022-02-25 13:37:36 +0800927 MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA384 ) )
928 {
Jerry Yue91a51a2022-03-22 21:42:50 +0800929 *algorithm = MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA384;
Jerry Yue91a51a2022-03-22 21:42:50 +0800930 return( 0 );
Jerry Yu67eced02022-02-25 13:37:36 +0800931 }
Jerry Yucef3f332022-03-22 23:00:13 +0800932 else
933#endif /* MBEDTLS_SHA384_C */
934#if defined(MBEDTLS_SHA512_C)
935 if( own_key_size <= 4096 &&
936 mbedtls_ssl_sig_alg_is_received( ssl,
Jerry Yu67eced02022-02-25 13:37:36 +0800937 MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA512 ) )
938 {
Jerry Yue91a51a2022-03-22 21:42:50 +0800939 *algorithm = MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA512;
Jerry Yue91a51a2022-03-22 21:42:50 +0800940 return( 0 );
Jerry Yu67eced02022-02-25 13:37:36 +0800941 }
Jerry Yucef3f332022-03-22 23:00:13 +0800942 else
943#endif /* MBEDTLS_SHA512_C */
944#endif /* MBEDTLS_PKCS1_V21 */
945#if defined(MBEDTLS_PKCS1_V15)
946#if defined(MBEDTLS_SHA256_C)
947 if( own_key_size <= 2048 &&
948 mbedtls_ssl_sig_alg_is_received( ssl,
949 MBEDTLS_TLS1_3_SIG_RSA_PKCS1_SHA256 ) )
950 {
951 *algorithm = MBEDTLS_TLS1_3_SIG_RSA_PKCS1_SHA256;
Jerry Yucef3f332022-03-22 23:00:13 +0800952 return( 0 );
953 }
954 else
955#endif /* MBEDTLS_SHA256_C */
956#if defined(MBEDTLS_SHA384_C)
957 if( own_key_size <= 3072 &&
958 mbedtls_ssl_sig_alg_is_received( ssl,
959 MBEDTLS_TLS1_3_SIG_RSA_PKCS1_SHA384 ) )
960 {
961 *algorithm = MBEDTLS_TLS1_3_SIG_RSA_PKCS1_SHA384;
Jerry Yucef3f332022-03-22 23:00:13 +0800962 return( 0 );
963 }
964 else
965#endif /* MBEDTLS_SHA384_C */
966#if defined(MBEDTLS_SHA512_C)
967 if( own_key_size <= 4096 &&
968 mbedtls_ssl_sig_alg_is_received( ssl,
969 MBEDTLS_TLS1_3_SIG_RSA_PKCS1_SHA512 ) )
970 {
971 *algorithm = MBEDTLS_TLS1_3_SIG_RSA_PKCS1_SHA512;
Jerry Yucef3f332022-03-22 23:00:13 +0800972 return( 0 );
973 }
974 else
975#endif /* MBEDTLS_SHA512_C */
976#endif /* MBEDTLS_PKCS1_V15 */
977 {
978 MBEDTLS_SSL_DEBUG_MSG( 3,
979 ( "unknown key size: %"
980 MBEDTLS_PRINTF_SIZET " bits",
981 own_key_size ) );
982 }
Jerry Yu67eced02022-02-25 13:37:36 +0800983 break;
Jerry Yucef3f332022-03-22 23:00:13 +0800984#endif /* MBEDTLS_RSA_C */
Jerry Yu67eced02022-02-25 13:37:36 +0800985 default:
986 MBEDTLS_SSL_DEBUG_MSG( 1,
Andrzej Kurek5c65c572022-04-13 14:28:52 -0400987 ( "unknown signature type : %u", sig ) );
Jerry Yu67eced02022-02-25 13:37:36 +0800988 break;
989 }
Jerry Yue91a51a2022-03-22 21:42:50 +0800990 return( -1 );
Jerry Yu67eced02022-02-25 13:37:36 +0800991}
992
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200993MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yu3e536442022-02-15 11:05:59 +0800994static int ssl_tls13_write_certificate_verify_body( mbedtls_ssl_context *ssl,
995 unsigned char *buf,
996 unsigned char *end,
997 size_t *out_len )
Jerry Yu8511f122022-01-29 10:01:04 +0800998{
999 int ret;
Jerry Yu3e536442022-02-15 11:05:59 +08001000 unsigned char *p = buf;
Jerry Yu8511f122022-01-29 10:01:04 +08001001 mbedtls_pk_context *own_key;
Jerry Yu3e536442022-02-15 11:05:59 +08001002
Jerry Yu8511f122022-01-29 10:01:04 +08001003 unsigned char handshake_hash[ MBEDTLS_TLS1_3_MD_MAX_SIZE ];
1004 size_t handshake_hash_len;
Jerry Yu3e536442022-02-15 11:05:59 +08001005 unsigned char verify_buffer[ SSL_VERIFY_STRUCT_MAX_SIZE ];
1006 size_t verify_buffer_len;
Jerry Yu67eced02022-02-25 13:37:36 +08001007 mbedtls_pk_type_t pk_type = MBEDTLS_PK_NONE;
Jerry Yu67eced02022-02-25 13:37:36 +08001008 mbedtls_md_type_t md_alg = MBEDTLS_MD_NONE;
pespacek34935872022-05-20 15:43:32 +02001009 psa_algorithm_t psa_algorithm = PSA_ALG_NONE;
Jerry Yu78272072022-02-22 10:28:13 +08001010 uint16_t algorithm = MBEDTLS_TLS1_3_SIG_NONE;
Jerry Yu3e536442022-02-15 11:05:59 +08001011 size_t signature_len = 0;
Jerry Yu3391ac02022-02-16 11:21:37 +08001012 unsigned char verify_hash[ MBEDTLS_MD_MAX_SIZE ];
Jerry Yu3e536442022-02-15 11:05:59 +08001013 size_t verify_hash_len;
pespacekb06acd72022-06-07 13:07:21 +02001014 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Jerry Yu8511f122022-01-29 10:01:04 +08001015
Jerry Yu0b7b1012022-02-23 12:23:05 +08001016 *out_len = 0;
1017
Jerry Yu3e536442022-02-15 11:05:59 +08001018 own_key = mbedtls_ssl_own_key( ssl );
1019 if( own_key == NULL )
Jerry Yu8511f122022-01-29 10:01:04 +08001020 {
Jerry Yu3e536442022-02-15 11:05:59 +08001021 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1022 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Jerry Yu8511f122022-01-29 10:01:04 +08001023 }
1024
Jerry Yu8511f122022-01-29 10:01:04 +08001025 ret = mbedtls_ssl_get_handshake_transcript( ssl,
1026 ssl->handshake->ciphersuite_info->mac,
1027 handshake_hash,
1028 sizeof( handshake_hash ),
1029 &handshake_hash_len );
1030 if( ret != 0 )
1031 return( ret );
1032
1033 MBEDTLS_SSL_DEBUG_BUF( 3, "handshake hash",
1034 handshake_hash,
1035 handshake_hash_len);
1036
Jerry Yu8511f122022-01-29 10:01:04 +08001037 ssl_tls13_create_verify_structure( handshake_hash, handshake_hash_len,
1038 verify_buffer, &verify_buffer_len,
1039 ssl->conf->endpoint );
1040
1041 /*
1042 * struct {
1043 * SignatureScheme algorithm;
1044 * opaque signature<0..2^16-1>;
1045 * } CertificateVerify;
1046 */
Jerry Yu8c338862022-03-23 13:34:04 +08001047 ret = ssl_tls13_get_sig_alg_from_pk( ssl, own_key, &algorithm );
Jerry Yue91a51a2022-03-22 21:42:50 +08001048 if( ret != 0 || ! mbedtls_ssl_sig_alg_is_received( ssl, algorithm ) )
Jerry Yu8511f122022-01-29 10:01:04 +08001049 {
Jerry Yud66409a2022-02-18 16:42:24 +08001050 MBEDTLS_SSL_DEBUG_MSG( 1,
Jerry Yu2124d052022-02-18 21:07:18 +08001051 ( "signature algorithm not in received or offered list." ) );
Jerry Yu67eced02022-02-25 13:37:36 +08001052
1053 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Signature algorithm is %s",
1054 mbedtls_ssl_sig_alg_to_str( algorithm ) ) );
1055
Jerry Yu71f36f12022-02-23 17:34:29 +08001056 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE,
Jerry Yud66409a2022-02-18 16:42:24 +08001057 MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
Jerry Yu3391ac02022-02-16 11:21:37 +08001058 return( MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
Jerry Yu8511f122022-01-29 10:01:04 +08001059 }
1060
Jerry Yu6c6f1022022-03-25 11:09:50 +08001061 if( mbedtls_ssl_tls13_get_pk_type_and_md_alg_from_sig_alg(
1062 algorithm, &pk_type, &md_alg ) != 0 )
Jerry Yu8c338862022-03-23 13:34:04 +08001063 {
Jerry Yuf8aa9a42022-03-23 20:40:28 +08001064 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Jerry Yu8c338862022-03-23 13:34:04 +08001065 }
1066
Jerry Yu3391ac02022-02-16 11:21:37 +08001067 /* Check there is space for the algorithm identifier (2 bytes) and the
1068 * signature length (2 bytes).
1069 */
1070 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 4 );
Jerry Yu3e536442022-02-15 11:05:59 +08001071 MBEDTLS_PUT_UINT16_BE( algorithm, p, 0 );
1072 p += 2;
Jerry Yu8511f122022-01-29 10:01:04 +08001073
1074 /* Hash verify buffer with indicated hash function */
pespacek34935872022-05-20 15:43:32 +02001075 psa_algorithm = mbedtls_psa_translate_md( md_alg );
pespacekb06acd72022-06-07 13:07:21 +02001076 status = psa_hash_compute( psa_algorithm,
1077 verify_buffer,
1078 verify_buffer_len,
1079 verify_hash,sizeof( verify_hash ),
1080 &verify_hash_len );
1081 if( status != PSA_SUCCESS )
pespacek670913f2022-06-07 10:53:39 +02001082 return( psa_ssl_status_to_mbedtls( status ) );
Jerry Yu8511f122022-01-29 10:01:04 +08001083
Jerry Yu8511f122022-01-29 10:01:04 +08001084 MBEDTLS_SSL_DEBUG_BUF( 3, "verify hash", verify_hash, verify_hash_len );
1085
Jerry Yu8beb9e12022-03-12 16:23:53 +08001086 if( ( ret = mbedtls_pk_sign_ext( pk_type, own_key,
1087 md_alg, verify_hash, verify_hash_len,
Jerry Yu67eced02022-02-25 13:37:36 +08001088 p + 2, (size_t)( end - ( p + 2 ) ), &signature_len,
1089 ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 )
Jerry Yu8511f122022-01-29 10:01:04 +08001090 {
1091 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_pk_sign", ret );
1092 return( ret );
1093 }
Jerry Yu8511f122022-01-29 10:01:04 +08001094
Jerry Yu3e536442022-02-15 11:05:59 +08001095 MBEDTLS_PUT_UINT16_BE( signature_len, p, 0 );
1096 p += 2 + signature_len;
Jerry Yu8511f122022-01-29 10:01:04 +08001097
Jerry Yu3e536442022-02-15 11:05:59 +08001098 *out_len = (size_t)( p - buf );
1099
Jerry Yu8511f122022-01-29 10:01:04 +08001100 return( ret );
1101}
Jerry Yu8511f122022-01-29 10:01:04 +08001102
Jerry Yu3e536442022-02-15 11:05:59 +08001103int mbedtls_ssl_tls13_write_certificate_verify( mbedtls_ssl_context *ssl )
Jerry Yu8511f122022-01-29 10:01:04 +08001104{
1105 int ret = 0;
Jerry Yuca133a32022-02-15 14:22:05 +08001106 unsigned char *buf;
1107 size_t buf_len, msg_len;
1108
Jerry Yu8511f122022-01-29 10:01:04 +08001109 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write certificate verify" ) );
1110
Ronald Cron8f6d39a2022-03-10 18:56:50 +01001111 MBEDTLS_SSL_PROC_CHK( mbedtls_ssl_start_handshake_msg( ssl,
Jerry Yuca133a32022-02-15 14:22:05 +08001112 MBEDTLS_SSL_HS_CERTIFICATE_VERIFY, &buf, &buf_len ) );
Jerry Yu8511f122022-01-29 10:01:04 +08001113
Jerry Yuca133a32022-02-15 14:22:05 +08001114 MBEDTLS_SSL_PROC_CHK( ssl_tls13_write_certificate_verify_body(
1115 ssl, buf, buf + buf_len, &msg_len ) );
Jerry Yu8511f122022-01-29 10:01:04 +08001116
Ronald Cron8f6d39a2022-03-10 18:56:50 +01001117 mbedtls_ssl_add_hs_msg_to_checksum( ssl, MBEDTLS_SSL_HS_CERTIFICATE_VERIFY,
1118 buf, msg_len );
Jerry Yu8511f122022-01-29 10:01:04 +08001119
Ronald Cron8f6d39a2022-03-10 18:56:50 +01001120 MBEDTLS_SSL_PROC_CHK( mbedtls_ssl_finish_handshake_msg(
Jerry Yuca133a32022-02-15 14:22:05 +08001121 ssl, buf_len, msg_len ) );
Jerry Yu8511f122022-01-29 10:01:04 +08001122
1123cleanup:
1124
1125 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write certificate verify" ) );
1126 return( ret );
1127}
1128
Jerry Yu90f152d2022-01-29 22:12:42 +08001129#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
1130
Jerry Yu5cc35062022-01-28 16:16:08 +08001131/*
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001132 *
XiaokangQianc5c39d52021-11-09 11:55:10 +00001133 * STATE HANDLING: Incoming Finished message.
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001134 */
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001135/*
1136 * Implementation
1137 */
1138
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001139MBEDTLS_CHECK_RETURN_CRITICAL
XiaokangQianaaa0e192021-11-10 03:07:04 +00001140static int ssl_tls13_preprocess_finished_message( mbedtls_ssl_context *ssl )
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001141{
1142 int ret;
1143
XiaokangQianc5c39d52021-11-09 11:55:10 +00001144 ret = mbedtls_ssl_tls13_calculate_verify_data( ssl,
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001145 ssl->handshake->state_local.finished_in.digest,
1146 sizeof( ssl->handshake->state_local.finished_in.digest ),
1147 &ssl->handshake->state_local.finished_in.digest_len,
XiaokangQianc5c39d52021-11-09 11:55:10 +00001148 ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT ?
XiaokangQianc13f9352021-11-11 06:13:22 +00001149 MBEDTLS_SSL_IS_SERVER : MBEDTLS_SSL_IS_CLIENT );
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001150 if( ret != 0 )
1151 {
XiaokangQianc5c39d52021-11-09 11:55:10 +00001152 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_tls13_calculate_verify_data", ret );
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001153 return( ret );
1154 }
1155
1156 return( 0 );
1157}
1158
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001159MBEDTLS_CHECK_RETURN_CRITICAL
XiaokangQianc5c39d52021-11-09 11:55:10 +00001160static int ssl_tls13_parse_finished_message( mbedtls_ssl_context *ssl,
1161 const unsigned char *buf,
1162 const unsigned char *end )
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001163{
XiaokangQian33062842021-11-11 03:37:45 +00001164 /*
1165 * struct {
XiaokangQianc13f9352021-11-11 06:13:22 +00001166 * opaque verify_data[Hash.length];
XiaokangQian33062842021-11-11 03:37:45 +00001167 * } Finished;
1168 */
1169 const unsigned char *expected_verify_data =
1170 ssl->handshake->state_local.finished_in.digest;
1171 size_t expected_verify_data_len =
1172 ssl->handshake->state_local.finished_in.digest_len;
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001173 /* Structural validation */
XiaokangQian33062842021-11-11 03:37:45 +00001174 if( (size_t)( end - buf ) != expected_verify_data_len )
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001175 {
1176 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
1177
1178 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR,
XiaokangQianc13f9352021-11-11 06:13:22 +00001179 MBEDTLS_ERR_SSL_DECODE_ERROR );
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001180 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
1181 }
1182
XiaokangQianc5c39d52021-11-09 11:55:10 +00001183 MBEDTLS_SSL_DEBUG_BUF( 4, "verify_data (self-computed):",
XiaokangQian33062842021-11-11 03:37:45 +00001184 expected_verify_data,
1185 expected_verify_data_len );
XiaokangQianc5c39d52021-11-09 11:55:10 +00001186 MBEDTLS_SSL_DEBUG_BUF( 4, "verify_data (received message):", buf,
XiaokangQian33062842021-11-11 03:37:45 +00001187 expected_verify_data_len );
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001188
1189 /* Semantic validation */
Gabor Mezei685472b2021-11-24 11:17:36 +01001190 if( mbedtls_ct_memcmp( buf,
1191 expected_verify_data,
1192 expected_verify_data_len ) != 0 )
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001193 {
1194 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
1195
XiaokangQian33062842021-11-11 03:37:45 +00001196 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_DECRYPT_ERROR,
XiaokangQianc13f9352021-11-11 06:13:22 +00001197 MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001198 return( MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
1199 }
1200 return( 0 );
1201}
1202
XiaokangQianc5c39d52021-11-09 11:55:10 +00001203int mbedtls_ssl_tls13_process_finished_message( mbedtls_ssl_context *ssl )
1204{
XiaokangQian33062842021-11-11 03:37:45 +00001205 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
XiaokangQianc5c39d52021-11-09 11:55:10 +00001206 unsigned char *buf;
Xiaofei Baieef15042021-11-18 07:29:56 +00001207 size_t buf_len;
XiaokangQianc5c39d52021-11-09 11:55:10 +00001208
XiaokangQiand0aa3e92021-11-10 06:17:40 +00001209 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse finished message" ) );
XiaokangQianc5c39d52021-11-09 11:55:10 +00001210
Xiaofei Bai746f9482021-11-12 08:53:56 +00001211 MBEDTLS_SSL_PROC_CHK( mbedtls_ssl_tls13_fetch_handshake_msg( ssl,
XiaokangQianc5c39d52021-11-09 11:55:10 +00001212 MBEDTLS_SSL_HS_FINISHED,
Xiaofei Baieef15042021-11-18 07:29:56 +00001213 &buf, &buf_len ) );
Jerry Yu0a92d6c2022-05-16 16:54:46 +08001214
1215 /* Preprocessing step: Compute handshake digest */
1216 MBEDTLS_SSL_PROC_CHK( ssl_tls13_preprocess_finished_message( ssl ) );
1217
Xiaofei Baieef15042021-11-18 07:29:56 +00001218 MBEDTLS_SSL_PROC_CHK( ssl_tls13_parse_finished_message( ssl, buf, buf + buf_len ) );
Jerry Yu0a92d6c2022-05-16 16:54:46 +08001219
Ronald Cron8f6d39a2022-03-10 18:56:50 +01001220 mbedtls_ssl_add_hs_msg_to_checksum( ssl, MBEDTLS_SSL_HS_FINISHED,
1221 buf, buf_len );
XiaokangQianc5c39d52021-11-09 11:55:10 +00001222
1223cleanup:
1224
XiaokangQiand0aa3e92021-11-10 06:17:40 +00001225 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse finished message" ) );
XiaokangQianc5c39d52021-11-09 11:55:10 +00001226 return( ret );
1227}
1228
XiaokangQian74af2a82021-09-22 07:40:30 +00001229/*
1230 *
XiaokangQiancc90c942021-11-09 12:30:09 +00001231 * STATE HANDLING: Write and send Finished message.
XiaokangQian74af2a82021-09-22 07:40:30 +00001232 *
1233 */
XiaokangQian74af2a82021-09-22 07:40:30 +00001234/*
XiaokangQian35dc6252021-11-11 08:16:19 +00001235 * Implement
XiaokangQian74af2a82021-09-22 07:40:30 +00001236 */
1237
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001238MBEDTLS_CHECK_RETURN_CRITICAL
XiaokangQian8773aa02021-11-10 07:33:09 +00001239static int ssl_tls13_prepare_finished_message( mbedtls_ssl_context *ssl )
XiaokangQian74af2a82021-09-22 07:40:30 +00001240{
1241 int ret;
1242
1243 /* Compute transcript of handshake up to now. */
XiaokangQiancc90c942021-11-09 12:30:09 +00001244 ret = mbedtls_ssl_tls13_calculate_verify_data( ssl,
XiaokangQian74af2a82021-09-22 07:40:30 +00001245 ssl->handshake->state_local.finished_out.digest,
1246 sizeof( ssl->handshake->state_local.finished_out.digest ),
1247 &ssl->handshake->state_local.finished_out.digest_len,
1248 ssl->conf->endpoint );
1249
1250 if( ret != 0 )
1251 {
Jerry Yu7ca30542021-12-08 15:57:57 +08001252 MBEDTLS_SSL_DEBUG_RET( 1, "calculate_verify_data failed", ret );
XiaokangQian74af2a82021-09-22 07:40:30 +00001253 return( ret );
1254 }
1255
1256 return( 0 );
1257}
1258
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001259MBEDTLS_CHECK_RETURN_CRITICAL
XiaokangQian8773aa02021-11-10 07:33:09 +00001260static int ssl_tls13_write_finished_message_body( mbedtls_ssl_context *ssl,
XiaokangQian35dc6252021-11-11 08:16:19 +00001261 unsigned char *buf,
1262 unsigned char *end,
Xiaofei Baid25fab62021-12-02 06:36:27 +00001263 size_t *out_len )
XiaokangQian74af2a82021-09-22 07:40:30 +00001264{
XiaokangQian8773aa02021-11-10 07:33:09 +00001265 size_t verify_data_len = ssl->handshake->state_local.finished_out.digest_len;
XiaokangQian0fa66432021-11-15 03:33:57 +00001266 /*
1267 * struct {
1268 * opaque verify_data[Hash.length];
1269 * } Finished;
1270 */
XiaokangQian8773aa02021-11-10 07:33:09 +00001271 MBEDTLS_SSL_CHK_BUF_PTR( buf, end, verify_data_len );
XiaokangQian74af2a82021-09-22 07:40:30 +00001272
1273 memcpy( buf, ssl->handshake->state_local.finished_out.digest,
XiaokangQian8773aa02021-11-10 07:33:09 +00001274 verify_data_len );
XiaokangQian74af2a82021-09-22 07:40:30 +00001275
Xiaofei Baid25fab62021-12-02 06:36:27 +00001276 *out_len = verify_data_len;
XiaokangQian74af2a82021-09-22 07:40:30 +00001277 return( 0 );
1278}
XiaokangQianc5c39d52021-11-09 11:55:10 +00001279
XiaokangQian35dc6252021-11-11 08:16:19 +00001280/* Main entry point: orchestrates the other functions */
1281int mbedtls_ssl_tls13_write_finished_message( mbedtls_ssl_context *ssl )
1282{
1283 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1284 unsigned char *buf;
1285 size_t buf_len, msg_len;
1286
1287 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write finished message" ) );
1288
XiaokangQiandce82242021-11-15 06:01:26 +00001289 MBEDTLS_SSL_PROC_CHK( ssl_tls13_prepare_finished_message( ssl ) );
1290
Ronald Cron8f6d39a2022-03-10 18:56:50 +01001291 MBEDTLS_SSL_PROC_CHK( mbedtls_ssl_start_handshake_msg( ssl,
XiaokangQian35dc6252021-11-11 08:16:19 +00001292 MBEDTLS_SSL_HS_FINISHED, &buf, &buf_len ) );
1293
1294 MBEDTLS_SSL_PROC_CHK( ssl_tls13_write_finished_message_body(
1295 ssl, buf, buf + buf_len, &msg_len ) );
1296
Ronald Cron8f6d39a2022-03-10 18:56:50 +01001297 mbedtls_ssl_add_hs_msg_to_checksum( ssl, MBEDTLS_SSL_HS_FINISHED,
1298 buf, msg_len );
XiaokangQian35dc6252021-11-11 08:16:19 +00001299
Ronald Cron8f6d39a2022-03-10 18:56:50 +01001300 MBEDTLS_SSL_PROC_CHK( mbedtls_ssl_finish_handshake_msg(
1301 ssl, buf_len, msg_len ) );
XiaokangQian35dc6252021-11-11 08:16:19 +00001302cleanup:
1303
1304 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write finished message" ) );
1305 return( ret );
1306}
1307
Jerry Yu378254d2021-10-30 21:44:47 +08001308void mbedtls_ssl_tls13_handshake_wrapup( mbedtls_ssl_context *ssl )
1309{
1310
1311 MBEDTLS_SSL_DEBUG_MSG( 3, ( "=> handshake wrapup" ) );
1312
Jerry Yue8c1fca2022-05-18 14:48:56 +08001313 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Switch to application keys for inbound traffic" ) );
1314 mbedtls_ssl_set_inbound_transform ( ssl, ssl->transform_application );
1315
1316 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Switch to application keys for outbound traffic" ) );
1317 mbedtls_ssl_set_outbound_transform( ssl, ssl->transform_application );
1318
Jerry Yu378254d2021-10-30 21:44:47 +08001319 /*
Jerry Yucfe64f02021-11-15 13:54:06 +08001320 * Free the previous session and switch to the current one.
Jerry Yu378254d2021-10-30 21:44:47 +08001321 */
1322 if( ssl->session )
1323 {
Jerry Yu378254d2021-10-30 21:44:47 +08001324 mbedtls_ssl_session_free( ssl->session );
1325 mbedtls_free( ssl->session );
1326 }
1327 ssl->session = ssl->session_negotiate;
1328 ssl->session_negotiate = NULL;
1329
1330 MBEDTLS_SSL_DEBUG_MSG( 3, ( "<= handshake wrapup" ) );
1331}
1332
Ronald Cron49ad6192021-11-24 16:25:31 +01001333/*
1334 *
1335 * STATE HANDLING: Write ChangeCipherSpec
1336 *
1337 */
1338#if defined(MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001339MBEDTLS_CHECK_RETURN_CRITICAL
Ronald Cron49ad6192021-11-24 16:25:31 +01001340static int ssl_tls13_write_change_cipher_spec_body( mbedtls_ssl_context *ssl,
1341 unsigned char *buf,
1342 unsigned char *end,
1343 size_t *olen )
1344{
1345 ((void) ssl);
1346
1347 MBEDTLS_SSL_CHK_BUF_PTR( buf, end, 1 );
1348 buf[0] = 1;
1349 *olen = 1;
1350
1351 return( 0 );
1352}
1353
Ronald Cron49ad6192021-11-24 16:25:31 +01001354int mbedtls_ssl_tls13_write_change_cipher_spec( mbedtls_ssl_context *ssl )
1355{
1356 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1357
1358 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write change cipher spec" ) );
1359
Ronald Cron49ad6192021-11-24 16:25:31 +01001360 /* Write CCS message */
1361 MBEDTLS_SSL_PROC_CHK( ssl_tls13_write_change_cipher_spec_body(
1362 ssl, ssl->out_msg,
1363 ssl->out_msg + MBEDTLS_SSL_OUT_CONTENT_LEN,
1364 &ssl->out_msglen ) );
1365
1366 ssl->out_msgtype = MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC;
1367
Ronald Cron49ad6192021-11-24 16:25:31 +01001368 /* Dispatch message */
Ronald Cron66dbf912022-02-02 15:33:46 +01001369 MBEDTLS_SSL_PROC_CHK( mbedtls_ssl_write_record( ssl, 0 ) );
Ronald Cron49ad6192021-11-24 16:25:31 +01001370
1371cleanup:
1372
1373 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write change cipher spec" ) );
1374 return( ret );
1375}
1376
1377#endif /* MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE */
1378
XiaokangQian78b1fa72022-01-19 06:56:30 +00001379/* Reset SSL context and update hash for handling HRR.
1380 *
1381 * Replace Transcript-Hash(X) by
1382 * Transcript-Hash( message_hash ||
1383 * 00 00 Hash.length ||
1384 * X )
1385 * A few states of the handshake are preserved, including:
1386 * - session ID
1387 * - session ticket
1388 * - negotiated ciphersuite
1389 */
1390int mbedtls_ssl_reset_transcript_for_hrr( mbedtls_ssl_context *ssl )
1391{
1392 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1393 unsigned char hash_transcript[ MBEDTLS_MD_MAX_SIZE + 4 ];
XiaokangQian0ece9982022-01-24 08:56:23 +00001394 size_t hash_len;
XiaokangQian78b1fa72022-01-19 06:56:30 +00001395 const mbedtls_ssl_ciphersuite_t *ciphersuite_info;
1396 uint16_t cipher_suite = ssl->session_negotiate->ciphersuite;
1397 ciphersuite_info = mbedtls_ssl_ciphersuite_from_id( cipher_suite );
1398
1399 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Reset SSL session for HRR" ) );
1400
XiaokangQian0ece9982022-01-24 08:56:23 +00001401 ret = mbedtls_ssl_get_handshake_transcript( ssl, ciphersuite_info->mac,
1402 hash_transcript + 4,
1403 MBEDTLS_MD_MAX_SIZE,
1404 &hash_len );
1405 if( ret != 0 )
1406 {
1407 MBEDTLS_SSL_DEBUG_RET( 4, "mbedtls_ssl_get_handshake_transcript", ret );
1408 return( ret );
1409 }
1410
1411 hash_transcript[0] = MBEDTLS_SSL_HS_MESSAGE_HASH;
1412 hash_transcript[1] = 0;
1413 hash_transcript[2] = 0;
1414 hash_transcript[3] = (unsigned char) hash_len;
1415
1416 hash_len += 4;
1417
XiaokangQian78b1fa72022-01-19 06:56:30 +00001418 if( ciphersuite_info->mac == MBEDTLS_MD_SHA256 )
1419 {
1420#if defined(MBEDTLS_SHA256_C)
XiaokangQian78b1fa72022-01-19 06:56:30 +00001421 MBEDTLS_SSL_DEBUG_BUF( 4, "Truncated SHA-256 handshake transcript",
XiaokangQian0ece9982022-01-24 08:56:23 +00001422 hash_transcript, hash_len );
XiaokangQian78b1fa72022-01-19 06:56:30 +00001423
1424#if defined(MBEDTLS_USE_PSA_CRYPTO)
1425 psa_hash_abort( &ssl->handshake->fin_sha256_psa );
1426 psa_hash_setup( &ssl->handshake->fin_sha256_psa, PSA_ALG_SHA_256 );
1427#else
1428 mbedtls_sha256_starts( &ssl->handshake->fin_sha256, 0 );
1429#endif
XiaokangQian78b1fa72022-01-19 06:56:30 +00001430#endif /* MBEDTLS_SHA256_C */
1431 }
1432 else if( ciphersuite_info->mac == MBEDTLS_MD_SHA384 )
1433 {
1434#if defined(MBEDTLS_SHA384_C)
XiaokangQian78b1fa72022-01-19 06:56:30 +00001435 MBEDTLS_SSL_DEBUG_BUF( 4, "Truncated SHA-384 handshake transcript",
XiaokangQian0ece9982022-01-24 08:56:23 +00001436 hash_transcript, hash_len );
XiaokangQian78b1fa72022-01-19 06:56:30 +00001437
1438#if defined(MBEDTLS_USE_PSA_CRYPTO)
1439 psa_hash_abort( &ssl->handshake->fin_sha384_psa );
1440 psa_hash_setup( &ssl->handshake->fin_sha384_psa, PSA_ALG_SHA_384 );
1441#else
1442 mbedtls_sha512_starts( &ssl->handshake->fin_sha512, 1 );
1443#endif
XiaokangQian78b1fa72022-01-19 06:56:30 +00001444#endif /* MBEDTLS_SHA384_C */
1445 }
1446
XiaokangQian0ece9982022-01-24 08:56:23 +00001447#if defined(MBEDTLS_SHA256_C) || defined(MBEDTLS_SHA384_C)
1448 ssl->handshake->update_checksum( ssl, hash_transcript, hash_len );
1449#endif /* MBEDTLS_SHA256_C || MBEDTLS_SHA384_C */
Przemyslaw Stekiel4b3fff42022-02-14 16:39:52 +01001450
XiaokangQian78b1fa72022-01-19 06:56:30 +00001451 return( ret );
1452}
1453
XiaokangQian9b5d04b2022-04-10 10:20:43 +00001454#if defined(MBEDTLS_ECDH_C)
XiaokangQian7807f9f2022-02-15 10:04:37 +00001455
XiaokangQian9b5d04b2022-04-10 10:20:43 +00001456int mbedtls_ssl_tls13_read_public_ecdhe_share( mbedtls_ssl_context *ssl,
1457 const unsigned char *buf,
1458 size_t buf_len )
XiaokangQian7807f9f2022-02-15 10:04:37 +00001459{
XiaokangQian9b5d04b2022-04-10 10:20:43 +00001460 uint8_t *p = (uint8_t*)buf;
XiaokangQiancfd925f2022-04-14 07:10:37 +00001461 const uint8_t *end = buf + buf_len;
XiaokangQian9b5d04b2022-04-10 10:20:43 +00001462 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001463
XiaokangQian9b5d04b2022-04-10 10:20:43 +00001464 /* Get size of the TLS opaque key_exchange field of the KeyShareEntry struct. */
XiaokangQiancfd925f2022-04-14 07:10:37 +00001465 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 2 );
XiaokangQian9b5d04b2022-04-10 10:20:43 +00001466 uint16_t peerkey_len = MBEDTLS_GET_UINT16_BE( p, 0 );
1467 p += 2;
XiaokangQian3207a322022-02-23 03:15:27 +00001468
XiaokangQian9b5d04b2022-04-10 10:20:43 +00001469 /* Check if key size is consistent with given buffer length. */
XiaokangQiancfd925f2022-04-14 07:10:37 +00001470 MBEDTLS_SSL_CHK_BUF_PTR( p, end, peerkey_len );
XiaokangQian9b5d04b2022-04-10 10:20:43 +00001471
1472 /* Store peer's ECDH public key. */
1473 memcpy( handshake->ecdh_psa_peerkey, p, peerkey_len );
1474 handshake->ecdh_psa_peerkey_len = peerkey_len;
1475
XiaokangQian3207a322022-02-23 03:15:27 +00001476 return( 0 );
1477}
Jerry Yu89e103c2022-03-30 22:43:29 +08001478
1479int mbedtls_ssl_tls13_generate_and_write_ecdh_key_exchange(
1480 mbedtls_ssl_context *ssl,
1481 uint16_t named_group,
1482 unsigned char *buf,
1483 unsigned char *end,
1484 size_t *out_len )
1485{
1486 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
1487 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
1488 psa_key_attributes_t key_attributes;
1489 size_t own_pubkey_len;
1490 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
1491 size_t ecdh_bits = 0;
1492
1493 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Perform PSA-based ECDH computation." ) );
1494
1495 /* Convert EC group to PSA key type. */
1496 if( ( handshake->ecdh_psa_type =
1497 mbedtls_psa_parse_tls_ecc_group( named_group, &ecdh_bits ) ) == 0 )
1498 return( MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
1499
1500 ssl->handshake->ecdh_bits = ecdh_bits;
1501
1502 key_attributes = psa_key_attributes_init();
1503 psa_set_key_usage_flags( &key_attributes, PSA_KEY_USAGE_DERIVE );
1504 psa_set_key_algorithm( &key_attributes, PSA_ALG_ECDH );
1505 psa_set_key_type( &key_attributes, handshake->ecdh_psa_type );
1506 psa_set_key_bits( &key_attributes, handshake->ecdh_bits );
1507
1508 /* Generate ECDH private key. */
1509 status = psa_generate_key( &key_attributes,
1510 &handshake->ecdh_psa_privkey );
1511 if( status != PSA_SUCCESS )
1512 {
1513 ret = psa_ssl_status_to_mbedtls( status );
1514 MBEDTLS_SSL_DEBUG_RET( 1, "psa_generate_key", ret );
1515 return( ret );
1516
1517 }
1518
1519 /* Export the public part of the ECDH private key from PSA. */
1520 status = psa_export_public_key( handshake->ecdh_psa_privkey,
1521 buf, (size_t)( end - buf ),
1522 &own_pubkey_len );
1523 if( status != PSA_SUCCESS )
1524 {
1525 ret = psa_ssl_status_to_mbedtls( status );
1526 MBEDTLS_SSL_DEBUG_RET( 1, "psa_export_public_key", ret );
1527 return( ret );
1528
1529 }
1530
1531 *out_len = own_pubkey_len;
1532
1533 return( 0 );
1534}
XiaokangQian9b5d04b2022-04-10 10:20:43 +00001535#endif /* MBEDTLS_ECDH_C */
XiaokangQian7807f9f2022-02-15 10:04:37 +00001536
Jerry Yufb4b6472022-01-27 15:03:26 +08001537#endif /* MBEDTLS_SSL_TLS_C && MBEDTLS_SSL_PROTO_TLS1_3 */