blob: 54884e9ff297132f28c1713568a8719eb2e3658d [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 Yu95b743c2022-07-23 11:37:50 +0800216 if( mbedtls_ssl_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
Manuel Pégourié-Gonnardabac0372022-07-18 13:41:11 +0200222 hash_alg = mbedtls_hash_info_psa_from_md( md_alg );
pespaceka1378102022-04-26 15:03:11 +0200223 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 {
Xiaofei Baid25fab62021-12-02 06:36:27 +0000261 rsassa_pss_options.mgf1_hash_id = md_alg;
Przemek Stekiel6a5e0182022-06-27 11:53:13 +0200262
Przemek Stekiel4dc87442022-06-28 11:05:42 +0200263 rsassa_pss_options.expected_salt_len = PSA_HASH_LENGTH( hash_alg );
Xiaofei Baid25fab62021-12-02 06:36:27 +0000264 options = (const void*) &rsassa_pss_options;
XiaokangQian82d34cc2021-11-03 08:51:56 +0000265 }
266#endif /* MBEDTLS_X509_RSASSA_PSS_SUPPORT */
Jerry Yu30b071c2021-09-12 20:16:03 +0800267
Xiaofei Baid25fab62021-12-02 06:36:27 +0000268 if( ( ret = mbedtls_pk_verify_ext( sig_alg, options,
Jerry Yu30b071c2021-09-12 20:16:03 +0800269 &ssl->session_negotiate->peer_cert->pk,
270 md_alg, verify_hash, verify_hash_len,
Jerry Yu6f87f252021-10-29 20:12:51 +0800271 p, signature_len ) ) == 0 )
Jerry Yu30b071c2021-09-12 20:16:03 +0800272 {
Jerry Yu6f87f252021-10-29 20:12:51 +0800273 return( 0 );
Jerry Yu30b071c2021-09-12 20:16:03 +0800274 }
Jerry Yu6f87f252021-10-29 20:12:51 +0800275 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_pk_verify_ext", ret );
Jerry Yu30b071c2021-09-12 20:16:03 +0800276
Jerry Yu6f87f252021-10-29 20:12:51 +0800277error:
278 /* RFC 8446 section 4.4.3
279 *
280 * If the verification fails, the receiver MUST terminate the handshake
281 * with a "decrypt_error" alert.
282 */
283 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_DECRYPT_ERROR,
284 MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
285 return( MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
286
Jerry Yu30b071c2021-09-12 20:16:03 +0800287}
288#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
289
290int mbedtls_ssl_tls13_process_certificate_verify( mbedtls_ssl_context *ssl )
291{
Jerry Yu30b071c2021-09-12 20:16:03 +0800292
Jerry Yuda8cdf22021-10-25 15:06:49 +0800293#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
294 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
295 unsigned char verify_buffer[SSL_VERIFY_STRUCT_MAX_SIZE];
296 size_t verify_buffer_len;
297 unsigned char transcript[MBEDTLS_TLS1_3_MD_MAX_SIZE];
298 size_t transcript_len;
299 unsigned char *buf;
300 size_t buf_len;
301
Jerry Yu30b071c2021-09-12 20:16:03 +0800302 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate verify" ) );
303
Jerry Yuda8cdf22021-10-25 15:06:49 +0800304 MBEDTLS_SSL_PROC_CHK(
Xiaofei Bai746f9482021-11-12 08:53:56 +0000305 mbedtls_ssl_tls13_fetch_handshake_msg( ssl,
Jerry Yuda8cdf22021-10-25 15:06:49 +0800306 MBEDTLS_SSL_HS_CERTIFICATE_VERIFY, &buf, &buf_len ) );
Jerry Yu30b071c2021-09-12 20:16:03 +0800307
Jerry Yuda8cdf22021-10-25 15:06:49 +0800308 /* Need to calculate the hash of the transcript first
Jerry Yu0b32c502021-10-28 13:41:59 +0800309 * before reading the message since otherwise it gets
310 * included in the transcript
311 */
Jerry Yuda8cdf22021-10-25 15:06:49 +0800312 ret = mbedtls_ssl_get_handshake_transcript( ssl,
313 ssl->handshake->ciphersuite_info->mac,
314 transcript, sizeof( transcript ),
315 &transcript_len );
316 if( ret != 0 )
Jerry Yu30b071c2021-09-12 20:16:03 +0800317 {
Jerry Yuda8cdf22021-10-25 15:06:49 +0800318 MBEDTLS_SSL_PEND_FATAL_ALERT(
319 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR,
320 MBEDTLS_ERR_SSL_INTERNAL_ERROR );
321 return( ret );
Jerry Yu30b071c2021-09-12 20:16:03 +0800322 }
323
Jerry Yuda8cdf22021-10-25 15:06:49 +0800324 MBEDTLS_SSL_DEBUG_BUF( 3, "handshake hash", transcript, transcript_len );
325
326 /* Create verify structure */
327 ssl_tls13_create_verify_structure( transcript,
Jerry Yu0b32c502021-10-28 13:41:59 +0800328 transcript_len,
329 verify_buffer,
330 &verify_buffer_len,
331 ( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT ) ?
332 MBEDTLS_SSL_IS_SERVER :
333 MBEDTLS_SSL_IS_CLIENT );
Jerry Yuda8cdf22021-10-25 15:06:49 +0800334
335 /* Process the message contents */
Jerry Yu0b32c502021-10-28 13:41:59 +0800336 MBEDTLS_SSL_PROC_CHK( ssl_tls13_parse_certificate_verify( ssl, buf,
337 buf + buf_len, verify_buffer, verify_buffer_len ) );
Jerry Yuda8cdf22021-10-25 15:06:49 +0800338
Ronald Cron8f6d39a2022-03-10 18:56:50 +0100339 mbedtls_ssl_add_hs_msg_to_checksum( ssl, MBEDTLS_SSL_HS_CERTIFICATE_VERIFY,
340 buf, buf_len );
Jerry Yu30b071c2021-09-12 20:16:03 +0800341
342cleanup:
343
344 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse certificate verify" ) );
Jerry Yu5398c102021-11-05 13:32:38 +0800345 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_tls13_process_certificate_verify", ret );
Jerry Yu30b071c2021-09-12 20:16:03 +0800346 return( ret );
Jerry Yuda8cdf22021-10-25 15:06:49 +0800347#else
348 ((void) ssl);
349 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
350 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
351#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
Jerry Yu30b071c2021-09-12 20:16:03 +0800352}
353
354/*
Xiaofei Bai947571e2021-09-29 09:12:03 +0000355 *
XiaokangQian6b916b12022-04-25 07:29:34 +0000356 * STATE HANDLING: Incoming Certificate.
Xiaofei Bai947571e2021-09-29 09:12:03 +0000357 *
358 */
359
Xiaofei Bai947571e2021-09-29 09:12:03 +0000360#if defined(MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED)
361#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
362/*
363 * Structure of Certificate message:
364 *
365 * enum {
366 * X509(0),
367 * RawPublicKey(2),
368 * (255)
369 * } CertificateType;
370 *
371 * struct {
372 * select (certificate_type) {
373 * case RawPublicKey:
374 * * From RFC 7250 ASN.1_subjectPublicKeyInfo *
375 * opaque ASN1_subjectPublicKeyInfo<1..2^24-1>;
376 * case X509:
377 * opaque cert_data<1..2^24-1>;
378 * };
379 * Extension extensions<0..2^16-1>;
380 * } CertificateEntry;
381 *
382 * struct {
383 * opaque certificate_request_context<0..2^8-1>;
384 * CertificateEntry certificate_list<0..2^24-1>;
385 * } Certificate;
386 *
387 */
Xiaofei Bai79595ac2021-10-26 07:16:45 +0000388
389/* Parse certificate chain send by the server. */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200390MBEDTLS_CHECK_RETURN_CRITICAL
Ronald Crone3dac4a2022-06-10 17:21:51 +0200391MBEDTLS_STATIC_TESTABLE
392int mbedtls_ssl_tls13_parse_certificate( mbedtls_ssl_context *ssl,
393 const unsigned char *buf,
394 const unsigned char *end )
Xiaofei Bai947571e2021-09-29 09:12:03 +0000395{
396 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
397 size_t certificate_request_context_len = 0;
398 size_t certificate_list_len = 0;
399 const unsigned char *p = buf;
400 const unsigned char *certificate_list_end;
401
XiaokangQian63e713e2022-05-15 04:26:57 +0000402 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 4 );
Xiaofei Bai947571e2021-09-29 09:12:03 +0000403 certificate_request_context_len = p[0];
XiaokangQian63e713e2022-05-15 04:26:57 +0000404 certificate_list_len = MBEDTLS_GET_UINT24_BE( p, 1 );
405 p += 4;
Xiaofei Bai947571e2021-09-29 09:12:03 +0000406
407 /* In theory, the certificate list can be up to 2^24 Bytes, but we don't
408 * support anything beyond 2^16 = 64K.
409 */
Xiaofei Bai79595ac2021-10-26 07:16:45 +0000410 if( ( certificate_request_context_len != 0 ) ||
411 ( certificate_list_len >= 0x10000 ) )
Xiaofei Bai947571e2021-09-29 09:12:03 +0000412 {
413 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
414 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR,
415 MBEDTLS_ERR_SSL_DECODE_ERROR );
416 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
417 }
418
419 /* In case we tried to reuse a session but it failed */
420 if( ssl->session_negotiate->peer_cert != NULL )
421 {
422 mbedtls_x509_crt_free( ssl->session_negotiate->peer_cert );
423 mbedtls_free( ssl->session_negotiate->peer_cert );
424 }
425
XiaokangQianc3017f62022-05-13 05:55:41 +0000426 if( certificate_list_len == 0 )
427 {
428 ssl->session_negotiate->peer_cert = NULL;
429 ret = 0;
430 goto exit;
431 }
432
Xiaofei Bai947571e2021-09-29 09:12:03 +0000433 if( ( ssl->session_negotiate->peer_cert =
434 mbedtls_calloc( 1, sizeof( mbedtls_x509_crt ) ) ) == NULL )
435 {
436 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc( %" MBEDTLS_PRINTF_SIZET " bytes ) failed",
437 sizeof( mbedtls_x509_crt ) ) );
438 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR,
439 MBEDTLS_ERR_SSL_ALLOC_FAILED );
440 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
441 }
442
443 mbedtls_x509_crt_init( ssl->session_negotiate->peer_cert );
444
Ronald Cron2b1a43c2022-06-10 17:03:54 +0200445 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, certificate_list_len );
Xiaofei Bai947571e2021-09-29 09:12:03 +0000446 certificate_list_end = p + certificate_list_len;
Xiaofei Bai79595ac2021-10-26 07:16:45 +0000447 while( p < certificate_list_end )
Xiaofei Bai947571e2021-09-29 09:12:03 +0000448 {
449 size_t cert_data_len, extensions_len;
450
Xiaofei Bai79595ac2021-10-26 07:16:45 +0000451 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, certificate_list_end, 3 );
Xiaofei Baif93cbd22021-10-29 02:39:30 +0000452 cert_data_len = MBEDTLS_GET_UINT24_BE( p, 0 );
Xiaofei Bai947571e2021-09-29 09:12:03 +0000453 p += 3;
454
455 /* In theory, the CRT can be up to 2^24 Bytes, but we don't support
456 * anything beyond 2^16 = 64K. Otherwise as in the TLS 1.2 code,
457 * check that we have a minimum of 128 bytes of data, this is not
458 * clear why we need that though.
459 */
460 if( ( cert_data_len < 128 ) || ( cert_data_len >= 0x10000 ) )
Xiaofei Bai79595ac2021-10-26 07:16:45 +0000461 {
Xiaofei Bai947571e2021-09-29 09:12:03 +0000462 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad Certificate message" ) );
463 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR,
464 MBEDTLS_ERR_SSL_DECODE_ERROR );
465 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
466 }
467
Xiaofei Bai79595ac2021-10-26 07:16:45 +0000468 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, certificate_list_end, cert_data_len );
Xiaofei Bai947571e2021-09-29 09:12:03 +0000469 ret = mbedtls_x509_crt_parse_der( ssl->session_negotiate->peer_cert,
470 p, cert_data_len );
471
472 switch( ret )
473 {
474 case 0: /*ok*/
475 break;
476 case MBEDTLS_ERR_X509_UNKNOWN_SIG_ALG + MBEDTLS_ERR_OID_NOT_FOUND:
477 /* Ignore certificate with an unknown algorithm: maybe a
478 prior certificate was already trusted. */
479 break;
480
481 case MBEDTLS_ERR_X509_ALLOC_FAILED:
482 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR,
483 MBEDTLS_ERR_X509_ALLOC_FAILED );
484 MBEDTLS_SSL_DEBUG_RET( 1, " mbedtls_x509_crt_parse_der", ret );
485 return( ret );
486
487 case MBEDTLS_ERR_X509_UNKNOWN_VERSION:
488 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT,
489 MBEDTLS_ERR_X509_UNKNOWN_VERSION );
490 MBEDTLS_SSL_DEBUG_RET( 1, " mbedtls_x509_crt_parse_der", ret );
491 return( ret );
492
493 default:
494 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_BAD_CERT,
495 ret );
496 MBEDTLS_SSL_DEBUG_RET( 1, " mbedtls_x509_crt_parse_der", ret );
497 return( ret );
498 }
499
500 p += cert_data_len;
501
502 /* Certificate extensions length */
503 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, certificate_list_end, 2 );
504 extensions_len = MBEDTLS_GET_UINT16_BE( p, 0 );
505 p += 2;
Xiaofei Bai79595ac2021-10-26 07:16:45 +0000506 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, certificate_list_end, extensions_len );
Xiaofei Bai947571e2021-09-29 09:12:03 +0000507 p += extensions_len;
508 }
509
XiaokangQian63e713e2022-05-15 04:26:57 +0000510exit:
Xiaofei Bai947571e2021-09-29 09:12:03 +0000511 /* Check that all the message is consumed. */
512 if( p != end )
513 {
514 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad Certificate message" ) );
515 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR, \
516 MBEDTLS_ERR_SSL_DECODE_ERROR );
517 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
518 }
519
520 MBEDTLS_SSL_DEBUG_CRT( 3, "peer certificate", ssl->session_negotiate->peer_cert );
521
522 return( ret );
523}
524#else
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200525MBEDTLS_CHECK_RETURN_CRITICAL
Ronald Crone3dac4a2022-06-10 17:21:51 +0200526MBEDTLS_STATIC_TESTABLE
527int mbedtls_ssl_tls13_parse_certificate( mbedtls_ssl_context *ssl,
528 const unsigned char *buf,
529 const unsigned char *end )
Xiaofei Bai947571e2021-09-29 09:12:03 +0000530{
531 ((void) ssl);
532 ((void) buf);
533 ((void) end);
534 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
535}
536#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
537#endif /* MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED */
538
539#if defined(MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED)
540#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Xiaofei Bai79595ac2021-10-26 07:16:45 +0000541/* Validate certificate chain sent by the server. */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200542MBEDTLS_CHECK_RETURN_CRITICAL
Xiaofei Bai947571e2021-09-29 09:12:03 +0000543static int ssl_tls13_validate_certificate( mbedtls_ssl_context *ssl )
544{
545 int ret = 0;
XiaokangQian989f06d2022-05-17 01:50:15 +0000546 int authmode = MBEDTLS_SSL_VERIFY_REQUIRED;
Xiaofei Bai947571e2021-09-29 09:12:03 +0000547 mbedtls_x509_crt *ca_chain;
548 mbedtls_x509_crl *ca_crl;
Ronald Cron30c5a252022-06-16 19:31:06 +0200549 const char *ext_oid;
550 size_t ext_len;
Xiaofei Baiff456022021-10-28 06:50:17 +0000551 uint32_t verify_result = 0;
Xiaofei Bai947571e2021-09-29 09:12:03 +0000552
XiaokangQian6b916b12022-04-25 07:29:34 +0000553 /* If SNI was used, overwrite authentication mode
554 * from the configuration. */
XiaokangQian989f06d2022-05-17 01:50:15 +0000555#if defined(MBEDTLS_SSL_SRV_C)
556 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
XiaokangQian0557c942022-05-30 08:10:53 +0000557 {
558#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
559 if( ssl->handshake->sni_authmode != MBEDTLS_SSL_VERIFY_UNSET )
560 authmode = ssl->handshake->sni_authmode;
561 else
562#endif
563 authmode = ssl->conf->authmode;
564 }
XiaokangQian6b916b12022-04-25 07:29:34 +0000565#endif
566
567 /*
XiaokangQian989f06d2022-05-17 01:50:15 +0000568 * If the peer hasn't sent a certificate ( i.e. it sent
XiaokangQian6b916b12022-04-25 07:29:34 +0000569 * an empty certificate chain ), this is reflected in the peer CRT
570 * structure being unset.
571 * Check for that and handle it depending on the
XiaokangQian989f06d2022-05-17 01:50:15 +0000572 * authentication mode.
XiaokangQian6b916b12022-04-25 07:29:34 +0000573 */
XiaokangQian63e713e2022-05-15 04:26:57 +0000574 if( ssl->session_negotiate->peer_cert == NULL )
XiaokangQian6b916b12022-04-25 07:29:34 +0000575 {
Ronald Cron19385882022-06-15 16:26:13 +0200576 MBEDTLS_SSL_DEBUG_MSG( 1, ( "peer has no certificate" ) );
XiaokangQian989f06d2022-05-17 01:50:15 +0000577
XiaokangQian63e713e2022-05-15 04:26:57 +0000578#if defined(MBEDTLS_SSL_SRV_C)
579 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
580 {
XiaokangQian63e713e2022-05-15 04:26:57 +0000581 /* The client was asked for a certificate but didn't send
582 * one. The client should know what's going on, so we
583 * don't send an alert.
584 */
585 ssl->session_negotiate->verify_result = MBEDTLS_X509_BADCERT_MISSING;
586 if( authmode == MBEDTLS_SSL_VERIFY_OPTIONAL )
587 return( 0 );
588 else
XiaokangQian989f06d2022-05-17 01:50:15 +0000589 {
590 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_NO_CERT,
XiaokangQianaca90482022-05-19 07:19:31 +0000591 MBEDTLS_ERR_SSL_NO_CLIENT_CERTIFICATE );
592 return( MBEDTLS_ERR_SSL_NO_CLIENT_CERTIFICATE );
XiaokangQian989f06d2022-05-17 01:50:15 +0000593 }
XiaokangQian63e713e2022-05-15 04:26:57 +0000594 }
XiaokangQian6b916b12022-04-25 07:29:34 +0000595#endif /* MBEDTLS_SSL_SRV_C */
596
XiaokangQianc3017f62022-05-13 05:55:41 +0000597#if defined(MBEDTLS_SSL_CLI_C)
XiaokangQian63e713e2022-05-15 04:26:57 +0000598 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
599 {
600 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_NO_CERT,
601 MBEDTLS_ERR_SSL_FATAL_ALERT_MESSAGE );
602 return( MBEDTLS_ERR_SSL_FATAL_ALERT_MESSAGE );
603 }
XiaokangQianc3017f62022-05-13 05:55:41 +0000604#endif /* MBEDTLS_SSL_CLI_C */
XiaokangQian63e713e2022-05-15 04:26:57 +0000605 }
XiaokangQian6b916b12022-04-25 07:29:34 +0000606
Xiaofei Bai947571e2021-09-29 09:12:03 +0000607#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
608 if( ssl->handshake->sni_ca_chain != NULL )
609 {
610 ca_chain = ssl->handshake->sni_ca_chain;
611 ca_crl = ssl->handshake->sni_ca_crl;
612 }
613 else
614#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
615 {
616 ca_chain = ssl->conf->ca_chain;
617 ca_crl = ssl->conf->ca_crl;
618 }
619
620 /*
621 * Main check: verify certificate
622 */
623 ret = mbedtls_x509_crt_verify_with_profile(
624 ssl->session_negotiate->peer_cert,
625 ca_chain, ca_crl,
626 ssl->conf->cert_profile,
627 ssl->hostname,
Xiaofei Baiff456022021-10-28 06:50:17 +0000628 &verify_result,
Xiaofei Bai947571e2021-09-29 09:12:03 +0000629 ssl->conf->f_vrfy, ssl->conf->p_vrfy );
630
631 if( ret != 0 )
632 {
633 MBEDTLS_SSL_DEBUG_RET( 1, "x509_verify_cert", ret );
634 }
635
636 /*
637 * Secondary checks: always done, but change 'ret' only if it was 0
638 */
Ronald Cron30c5a252022-06-16 19:31:06 +0200639 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Xiaofei Bai947571e2021-09-29 09:12:03 +0000640 {
Ronald Cron30c5a252022-06-16 19:31:06 +0200641 ext_oid = MBEDTLS_OID_SERVER_AUTH;
642 ext_len = MBEDTLS_OID_SIZE( MBEDTLS_OID_SERVER_AUTH );
643 }
644 else
645 {
646 ext_oid = MBEDTLS_OID_CLIENT_AUTH;
647 ext_len = MBEDTLS_OID_SIZE( MBEDTLS_OID_CLIENT_AUTH );
648 }
649
650 if( ( mbedtls_x509_crt_check_key_usage(
651 ssl->session_negotiate->peer_cert,
652 MBEDTLS_X509_KU_DIGITAL_SIGNATURE ) != 0 ) ||
653 ( mbedtls_x509_crt_check_extended_key_usage(
654 ssl->session_negotiate->peer_cert,
655 ext_oid, ext_len ) != 0 ) )
656 {
657 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate (usage extensions)" ) );
Xiaofei Bai947571e2021-09-29 09:12:03 +0000658 if( ret == 0 )
659 ret = MBEDTLS_ERR_SSL_BAD_CERTIFICATE;
660 }
661
XiaokangQian6b916b12022-04-25 07:29:34 +0000662 /* mbedtls_x509_crt_verify_with_profile is supposed to report a
663 * verification failure through MBEDTLS_ERR_X509_CERT_VERIFY_FAILED,
664 * with details encoded in the verification flags. All other kinds
665 * of error codes, including those from the user provided f_vrfy
666 * functions, are treated as fatal and lead to a failure of
Ronald Crone3dac4a2022-06-10 17:21:51 +0200667 * mbedtls_ssl_tls13_parse_certificate even if verification was optional.
668 */
XiaokangQian6b916b12022-04-25 07:29:34 +0000669 if( authmode == MBEDTLS_SSL_VERIFY_OPTIONAL &&
670 ( ret == MBEDTLS_ERR_X509_CERT_VERIFY_FAILED ||
671 ret == MBEDTLS_ERR_SSL_BAD_CERTIFICATE ) )
672 {
673 ret = 0;
674 }
Xiaofei Bai947571e2021-09-29 09:12:03 +0000675
XiaokangQian6b916b12022-04-25 07:29:34 +0000676 if( ca_chain == NULL && authmode == MBEDTLS_SSL_VERIFY_REQUIRED )
Xiaofei Bai947571e2021-09-29 09:12:03 +0000677 {
678 MBEDTLS_SSL_DEBUG_MSG( 1, ( "got no CA chain" ) );
679 ret = MBEDTLS_ERR_SSL_CA_CHAIN_REQUIRED;
680 }
681
682 if( ret != 0 )
683 {
684 /* The certificate may have been rejected for several reasons.
685 Pick one and send the corresponding alert. Which alert to send
686 may be a subject of debate in some cases. */
Xiaofei Baiff456022021-10-28 06:50:17 +0000687 if( verify_result & MBEDTLS_X509_BADCERT_OTHER )
Xiaofei Bai947571e2021-09-29 09:12:03 +0000688 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED, ret );
Xiaofei Baiff456022021-10-28 06:50:17 +0000689 else if( verify_result & MBEDTLS_X509_BADCERT_CN_MISMATCH )
Xiaofei Bai947571e2021-09-29 09:12:03 +0000690 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_BAD_CERT, ret );
Xiaofei Baif93cbd22021-10-29 02:39:30 +0000691 else if( verify_result & ( MBEDTLS_X509_BADCERT_KEY_USAGE |
692 MBEDTLS_X509_BADCERT_EXT_KEY_USAGE |
693 MBEDTLS_X509_BADCERT_NS_CERT_TYPE |
694 MBEDTLS_X509_BADCERT_BAD_PK |
695 MBEDTLS_X509_BADCERT_BAD_KEY ) )
Xiaofei Bai947571e2021-09-29 09:12:03 +0000696 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT, ret );
Xiaofei Baiff456022021-10-28 06:50:17 +0000697 else if( verify_result & MBEDTLS_X509_BADCERT_EXPIRED )
Xiaofei Bai947571e2021-09-29 09:12:03 +0000698 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_CERT_EXPIRED, ret );
Xiaofei Baiff456022021-10-28 06:50:17 +0000699 else if( verify_result & MBEDTLS_X509_BADCERT_REVOKED )
Xiaofei Bai947571e2021-09-29 09:12:03 +0000700 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_CERT_REVOKED, ret );
Xiaofei Baiff456022021-10-28 06:50:17 +0000701 else if( verify_result & MBEDTLS_X509_BADCERT_NOT_TRUSTED )
Xiaofei Bai947571e2021-09-29 09:12:03 +0000702 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_UNKNOWN_CA, ret );
703 else
704 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_CERT_UNKNOWN, ret );
705 }
706
707#if defined(MBEDTLS_DEBUG_C)
Xiaofei Baiff456022021-10-28 06:50:17 +0000708 if( verify_result != 0 )
Xiaofei Bai947571e2021-09-29 09:12:03 +0000709 {
710 MBEDTLS_SSL_DEBUG_MSG( 3, ( "! Certificate verification flags %08x",
Jerry Yu83bb1312021-10-28 22:16:33 +0800711 (unsigned int) verify_result ) );
Xiaofei Bai947571e2021-09-29 09:12:03 +0000712 }
713 else
714 {
715 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Certificate verification flags clear" ) );
716 }
717#endif /* MBEDTLS_DEBUG_C */
718
Xiaofei Baiff456022021-10-28 06:50:17 +0000719 ssl->session_negotiate->verify_result = verify_result;
Xiaofei Bai947571e2021-09-29 09:12:03 +0000720 return( ret );
721}
722#else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200723MBEDTLS_CHECK_RETURN_CRITICAL
Xiaofei Bai947571e2021-09-29 09:12:03 +0000724static int ssl_tls13_validate_certificate( mbedtls_ssl_context *ssl )
725{
726 ((void) ssl);
727 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
728}
729#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
730#endif /* MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED */
731
Xiaofei Bai79595ac2021-10-26 07:16:45 +0000732int mbedtls_ssl_tls13_process_certificate( mbedtls_ssl_context *ssl )
Xiaofei Bai947571e2021-09-29 09:12:03 +0000733{
Xiaofei Bai79595ac2021-10-26 07:16:45 +0000734 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
735 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) );
736
737#if defined(MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED)
XiaokangQianc3017f62022-05-13 05:55:41 +0000738 unsigned char *buf;
739 size_t buf_len;
Xiaofei Bai79595ac2021-10-26 07:16:45 +0000740
XiaokangQianc3017f62022-05-13 05:55:41 +0000741 MBEDTLS_SSL_PROC_CHK( mbedtls_ssl_tls13_fetch_handshake_msg(
742 ssl, MBEDTLS_SSL_HS_CERTIFICATE,
743 &buf, &buf_len ) );
Xiaofei Bai79595ac2021-10-26 07:16:45 +0000744
XiaokangQianc3017f62022-05-13 05:55:41 +0000745 /* Parse the certificate chain sent by the peer. */
Ronald Crone3dac4a2022-06-10 17:21:51 +0200746 MBEDTLS_SSL_PROC_CHK( mbedtls_ssl_tls13_parse_certificate( ssl, buf,
747 buf + buf_len ) );
XiaokangQianc3017f62022-05-13 05:55:41 +0000748 /* Validate the certificate chain and set the verification results. */
749 MBEDTLS_SSL_PROC_CHK( ssl_tls13_validate_certificate( ssl ) );
Xiaofei Bai79595ac2021-10-26 07:16:45 +0000750
XiaokangQianc3017f62022-05-13 05:55:41 +0000751 mbedtls_ssl_add_hs_msg_to_checksum( ssl, MBEDTLS_SSL_HS_CERTIFICATE,
752 buf, buf_len );
Xiaofei Bai79595ac2021-10-26 07:16:45 +0000753
Xiaofei Bai79595ac2021-10-26 07:16:45 +0000754cleanup:
XiaokangQianaca90482022-05-19 07:19:31 +0000755#endif /* MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED */
Xiaofei Bai79595ac2021-10-26 07:16:45 +0000756
757 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse certificate" ) );
758 return( ret );
Xiaofei Bai947571e2021-09-29 09:12:03 +0000759}
Jerry Yu90f152d2022-01-29 22:12:42 +0800760#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Jerry Yu7399d0d2022-01-30 17:54:19 +0800761/*
762 * enum {
763 * X509(0),
764 * RawPublicKey(2),
765 * (255)
766 * } CertificateType;
767 *
768 * struct {
769 * select (certificate_type) {
770 * case RawPublicKey:
771 * // From RFC 7250 ASN.1_subjectPublicKeyInfo
772 * opaque ASN1_subjectPublicKeyInfo<1..2^24-1>;
773 *
774 * case X509:
775 * opaque cert_data<1..2^24-1>;
776 * };
777 * Extension extensions<0..2^16-1>;
778 * } CertificateEntry;
779 *
780 * struct {
781 * opaque certificate_request_context<0..2^8-1>;
782 * CertificateEntry certificate_list<0..2^24-1>;
783 * } Certificate;
784 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200785MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yu7399d0d2022-01-30 17:54:19 +0800786static int ssl_tls13_write_certificate_body( mbedtls_ssl_context *ssl,
Jerry Yu3e536442022-02-15 11:05:59 +0800787 unsigned char *buf,
Jerry Yu7399d0d2022-01-30 17:54:19 +0800788 unsigned char *end,
Jerry Yu3e536442022-02-15 11:05:59 +0800789 size_t *out_len )
Jerry Yu5cc35062022-01-28 16:16:08 +0800790{
Jerry Yu5cc35062022-01-28 16:16:08 +0800791 const mbedtls_x509_crt *crt = mbedtls_ssl_own_cert( ssl );
Jerry Yu3e536442022-02-15 11:05:59 +0800792 unsigned char *p = buf;
Jerry Yuc8d8d4e2022-02-18 12:10:03 +0800793 unsigned char *certificate_request_context =
794 ssl->handshake->certificate_request_context;
795 unsigned char certificate_request_context_len =
796 ssl->handshake->certificate_request_context_len;
797 unsigned char *p_certificate_list_len;
Jerry Yu5cc35062022-01-28 16:16:08 +0800798
Jerry Yu5cc35062022-01-28 16:16:08 +0800799
Jerry Yu3391ac02022-02-16 11:21:37 +0800800 /* ...
801 * opaque certificate_request_context<0..2^8-1>;
802 * ...
803 */
Jerry Yuc8d8d4e2022-02-18 12:10:03 +0800804 MBEDTLS_SSL_CHK_BUF_PTR( p, end, certificate_request_context_len + 1 );
805 *p++ = certificate_request_context_len;
806 if( certificate_request_context_len > 0 )
Jerry Yu537530d2022-02-15 14:00:57 +0800807 {
Jerry Yuc8d8d4e2022-02-18 12:10:03 +0800808 memcpy( p, certificate_request_context, certificate_request_context_len );
809 p += certificate_request_context_len;
Jerry Yu537530d2022-02-15 14:00:57 +0800810 }
811
Jerry Yu3391ac02022-02-16 11:21:37 +0800812 /* ...
813 * CertificateEntry certificate_list<0..2^24-1>;
814 * ...
815 */
Jerry Yu3e536442022-02-15 11:05:59 +0800816 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 3 );
Jerry Yuc8d8d4e2022-02-18 12:10:03 +0800817 p_certificate_list_len = p;
Jerry Yu3e536442022-02-15 11:05:59 +0800818 p += 3;
819
Jerry Yu7399d0d2022-01-30 17:54:19 +0800820 MBEDTLS_SSL_DEBUG_CRT( 3, "own certificate", crt );
Jerry Yu5cc35062022-01-28 16:16:08 +0800821
Jerry Yuc8d8d4e2022-02-18 12:10:03 +0800822 while( crt != NULL )
Jerry Yu5cc35062022-01-28 16:16:08 +0800823 {
Jerry Yu7399d0d2022-01-30 17:54:19 +0800824 size_t cert_data_len = crt->raw.len;
Jerry Yu5cc35062022-01-28 16:16:08 +0800825
Jerry Yu7399d0d2022-01-30 17:54:19 +0800826 MBEDTLS_SSL_CHK_BUF_PTR( p, end, cert_data_len + 3 + 2 );
827 MBEDTLS_PUT_UINT24_BE( cert_data_len, p, 0 );
828 p += 3;
Jerry Yu5cc35062022-01-28 16:16:08 +0800829
Jerry Yu7399d0d2022-01-30 17:54:19 +0800830 memcpy( p, crt->raw.p, cert_data_len );
831 p += cert_data_len;
832 crt = crt->next;
Jerry Yu5cc35062022-01-28 16:16:08 +0800833
834 /* Currently, we don't have any certificate extensions defined.
835 * Hence, we are sending an empty extension with length zero.
836 */
Ronald Cron11b53322022-06-01 14:58:52 +0200837 MBEDTLS_PUT_UINT16_BE( 0, p, 0 );
Jerry Yu7399d0d2022-01-30 17:54:19 +0800838 p += 2;
Jerry Yu5cc35062022-01-28 16:16:08 +0800839 }
Jerry Yu5cc35062022-01-28 16:16:08 +0800840
Jerry Yuc8d8d4e2022-02-18 12:10:03 +0800841 MBEDTLS_PUT_UINT24_BE( p - p_certificate_list_len - 3,
842 p_certificate_list_len, 0 );
Jerry Yu7399d0d2022-01-30 17:54:19 +0800843
Jerry Yu3e536442022-02-15 11:05:59 +0800844 *out_len = p - buf;
Jerry Yu5cc35062022-01-28 16:16:08 +0800845
846 return( 0 );
847}
Jerry Yu5cc35062022-01-28 16:16:08 +0800848
Jerry Yu3e536442022-02-15 11:05:59 +0800849int mbedtls_ssl_tls13_write_certificate( mbedtls_ssl_context *ssl )
Jerry Yu5cc35062022-01-28 16:16:08 +0800850{
851 int ret;
Ronald Cron5bb8fc82022-03-09 07:00:13 +0100852 unsigned char *buf;
853 size_t buf_len, msg_len;
854
Jerry Yu5cc35062022-01-28 16:16:08 +0800855 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write certificate" ) );
856
Ronald Cron8f6d39a2022-03-10 18:56:50 +0100857 MBEDTLS_SSL_PROC_CHK( mbedtls_ssl_start_handshake_msg( ssl,
Ronald Cron5bb8fc82022-03-09 07:00:13 +0100858 MBEDTLS_SSL_HS_CERTIFICATE, &buf, &buf_len ) );
Jerry Yu5cc35062022-01-28 16:16:08 +0800859
Ronald Cron5bb8fc82022-03-09 07:00:13 +0100860 MBEDTLS_SSL_PROC_CHK( ssl_tls13_write_certificate_body( ssl,
861 buf,
862 buf + buf_len,
863 &msg_len ) );
Jerry Yu5cc35062022-01-28 16:16:08 +0800864
Ronald Cron8f6d39a2022-03-10 18:56:50 +0100865 mbedtls_ssl_add_hs_msg_to_checksum( ssl, MBEDTLS_SSL_HS_CERTIFICATE,
866 buf, msg_len );
Jerry Yu5cc35062022-01-28 16:16:08 +0800867
Ronald Cron8f6d39a2022-03-10 18:56:50 +0100868 MBEDTLS_SSL_PROC_CHK( mbedtls_ssl_finish_handshake_msg(
Ronald Cron5bb8fc82022-03-09 07:00:13 +0100869 ssl, buf_len, msg_len ) );
Jerry Yu5cc35062022-01-28 16:16:08 +0800870cleanup:
871
872 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write certificate" ) );
873 return( ret );
874}
875
Jerry Yu3e536442022-02-15 11:05:59 +0800876/*
877 * STATE HANDLING: Output Certificate Verify
878 */
Jerry Yuc2e04932022-06-27 22:13:03 +0800879int mbedtls_ssl_tls13_check_sig_alg_cert_key_match( uint16_t sig_alg,
880 mbedtls_pk_context *key )
Jerry Yu0c6be8f2022-06-20 20:42:00 +0800881{
882 mbedtls_pk_type_t pk_type = mbedtls_ssl_sig_from_pk( key );
883 size_t key_size = mbedtls_pk_get_bitlen( key );
884
885 switch( pk_type )
Jerry Yu67eced02022-02-25 13:37:36 +0800886 {
Jerry Yu67eced02022-02-25 13:37:36 +0800887 case MBEDTLS_SSL_SIG_ECDSA:
Jerry Yu0c6be8f2022-06-20 20:42:00 +0800888 switch( key_size )
Jerry Yu67eced02022-02-25 13:37:36 +0800889 {
890 case 256:
Jerry Yu0c6be8f2022-06-20 20:42:00 +0800891 return(
892 sig_alg == MBEDTLS_TLS1_3_SIG_ECDSA_SECP256R1_SHA256 );
Jerry Yu0c6be8f2022-06-20 20:42:00 +0800893
Jerry Yu67eced02022-02-25 13:37:36 +0800894 case 384:
Jerry Yu0c6be8f2022-06-20 20:42:00 +0800895 return(
896 sig_alg == MBEDTLS_TLS1_3_SIG_ECDSA_SECP384R1_SHA384 );
Jerry Yu0c6be8f2022-06-20 20:42:00 +0800897
Jerry Yu67eced02022-02-25 13:37:36 +0800898 case 521:
Jerry Yu0c6be8f2022-06-20 20:42:00 +0800899 return(
900 sig_alg == MBEDTLS_TLS1_3_SIG_ECDSA_SECP521R1_SHA512 );
Jerry Yu67eced02022-02-25 13:37:36 +0800901 default:
Jerry Yu67eced02022-02-25 13:37:36 +0800902 break;
903 }
904 break;
Jerry Yu67eced02022-02-25 13:37:36 +0800905
Jerry Yu67eced02022-02-25 13:37:36 +0800906 case MBEDTLS_SSL_SIG_RSA:
Jerry Yu0c6be8f2022-06-20 20:42:00 +0800907 switch( sig_alg )
Jerry Yu67eced02022-02-25 13:37:36 +0800908 {
Ronald Cron38391bf2022-09-16 11:19:27 +0200909 case MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA256: /* Intentional fallthrough */
910 case MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA384: /* Intentional fallthrough */
Jerry Yu0c6be8f2022-06-20 20:42:00 +0800911 case MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA512:
Jerry Yua1255e62022-06-24 10:10:47 +0800912 return( 1 );
Jerry Yuc2e04932022-06-27 22:13:03 +0800913
Jerry Yu0c6be8f2022-06-20 20:42:00 +0800914 default:
915 break;
Jerry Yucef3f332022-03-22 23:00:13 +0800916 }
Jerry Yu67eced02022-02-25 13:37:36 +0800917 break;
Jerry Yu0c6be8f2022-06-20 20:42:00 +0800918
Jerry Yu67eced02022-02-25 13:37:36 +0800919 default:
Jerry Yu67eced02022-02-25 13:37:36 +0800920 break;
921 }
Jerry Yu0c6be8f2022-06-20 20:42:00 +0800922
923 return( 0 );
924}
925
Przemek Stekielf937e662022-07-05 22:42:44 +0200926#if defined(MBEDTLS_USE_PSA_CRYPTO)
Przemek Stekiel3c326f92022-07-05 22:14:34 +0200927static psa_algorithm_t ssl_tls13_select_sig_alg_to_psa_alg( uint16_t sig_alg )
928{
Przemek Stekiel3c326f92022-07-05 22:14:34 +0200929 switch( sig_alg )
930 {
931 case MBEDTLS_TLS1_3_SIG_ECDSA_SECP256R1_SHA256:
Przemek Stekieldca22462022-07-06 22:34:25 +0200932 return( PSA_ALG_ECDSA( PSA_ALG_SHA_256 ) );
Przemek Stekiel3c326f92022-07-05 22:14:34 +0200933 case MBEDTLS_TLS1_3_SIG_ECDSA_SECP384R1_SHA384:
Przemek Stekieldca22462022-07-06 22:34:25 +0200934 return( PSA_ALG_ECDSA( PSA_ALG_SHA_384 ) );
Przemek Stekiel3c326f92022-07-05 22:14:34 +0200935 case MBEDTLS_TLS1_3_SIG_ECDSA_SECP521R1_SHA512:
Przemek Stekieldca22462022-07-06 22:34:25 +0200936 return( PSA_ALG_ECDSA( PSA_ALG_SHA_512 ) );
Przemek Stekiel3c326f92022-07-05 22:14:34 +0200937 case MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA256:
Przemek Stekieldca22462022-07-06 22:34:25 +0200938 return( PSA_ALG_RSA_PSS( PSA_ALG_SHA_256 ) );
Przemek Stekiel3c326f92022-07-05 22:14:34 +0200939 case MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA384:
Przemek Stekieldca22462022-07-06 22:34:25 +0200940 return( PSA_ALG_RSA_PSS( PSA_ALG_SHA_384 ) );
Przemek Stekiel3c326f92022-07-05 22:14:34 +0200941 case MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA512:
Przemek Stekieldca22462022-07-06 22:34:25 +0200942 return( PSA_ALG_RSA_PSS( PSA_ALG_SHA_512 ) );
Przemek Stekiel3c326f92022-07-05 22:14:34 +0200943 case MBEDTLS_TLS1_3_SIG_RSA_PKCS1_SHA256:
944 case MBEDTLS_TLS1_3_SIG_RSA_PKCS1_SHA384:
945 case MBEDTLS_TLS1_3_SIG_RSA_PKCS1_SHA512:
Przemek Stekieldca22462022-07-06 22:34:25 +0200946 return( PSA_ALG_RSA_PKCS1V15_CRYPT );
Przemek Stekiel3c326f92022-07-05 22:14:34 +0200947 default:
Przemek Stekieldca22462022-07-06 22:34:25 +0200948 return( 0 );
Przemek Stekiel3c326f92022-07-05 22:14:34 +0200949 }
Przemek Stekiel3c326f92022-07-05 22:14:34 +0200950}
Przemek Stekielf937e662022-07-05 22:42:44 +0200951#endif /* MBEDTLS_USE_PSA_CRYPTO */
Przemek Stekiel3c326f92022-07-05 22:14:34 +0200952
Ronald Cronce7d76e2022-07-08 18:56:49 +0200953MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yuf55886a2022-06-19 11:48:56 +0800954static int ssl_tls13_select_sig_alg_for_certificate_verify(
955 mbedtls_ssl_context *ssl,
Jerry Yuf249ef72022-06-15 17:23:33 +0800956 mbedtls_pk_context *own_key,
957 uint16_t *algorithm )
Jerry Yu67eced02022-02-25 13:37:36 +0800958{
Jerry Yuf249ef72022-06-15 17:23:33 +0800959 uint16_t *sig_alg = ssl->handshake->received_sig_algs;
Przemek Stekielf937e662022-07-05 22:42:44 +0200960#if defined(MBEDTLS_USE_PSA_CRYPTO)
Przemek Stekielb40f2e82022-07-04 16:16:15 +0200961 psa_algorithm_t psa_alg = 0;
Przemek Stekielf937e662022-07-05 22:42:44 +0200962#endif /* MBEDTLS_USE_PSA_CRYPTO */
Jerry Yuf249ef72022-06-15 17:23:33 +0800963
Jerry Yu67eced02022-02-25 13:37:36 +0800964 *algorithm = MBEDTLS_TLS1_3_SIG_NONE;
Jerry Yuf249ef72022-06-15 17:23:33 +0800965 for( ; *sig_alg != MBEDTLS_TLS1_3_SIG_NONE ; sig_alg++ )
Jerry Yu67eced02022-02-25 13:37:36 +0800966 {
Przemek Stekielf937e662022-07-05 22:42:44 +0200967#if defined(MBEDTLS_USE_PSA_CRYPTO)
Przemek Stekiel3c326f92022-07-05 22:14:34 +0200968 psa_alg = ssl_tls13_select_sig_alg_to_psa_alg( *sig_alg );
Przemek Stekielf937e662022-07-05 22:42:44 +0200969#endif /* MBEDTLS_USE_PSA_CRYPTO */
Przemek Stekielb40f2e82022-07-04 16:16:15 +0200970
Jerry Yu660cb422022-06-28 16:17:58 +0800971 if( mbedtls_ssl_sig_alg_is_offered( ssl, *sig_alg ) &&
Jerry Yu959e5e02022-06-29 09:49:02 +0800972 mbedtls_ssl_tls13_sig_alg_for_cert_verify_is_supported( *sig_alg ) &&
Przemek Stekielf937e662022-07-05 22:42:44 +0200973 mbedtls_ssl_tls13_check_sig_alg_cert_key_match( *sig_alg, own_key )
974#if defined(MBEDTLS_USE_PSA_CRYPTO)
975 && psa_alg != 0 &&
Przemek Stekielb40f2e82022-07-04 16:16:15 +0200976 mbedtls_pk_can_do_ext( own_key, psa_alg,
Przemek Stekielf937e662022-07-05 22:42:44 +0200977 PSA_KEY_USAGE_SIGN_HASH ) == 1
978#endif /* MBEDTLS_USE_PSA_CRYPTO */
979 )
Jerry Yuf249ef72022-06-15 17:23:33 +0800980 {
Jerry Yucc539102022-06-27 16:27:35 +0800981 MBEDTLS_SSL_DEBUG_MSG( 3,
982 ( "select_sig_alg_for_certificate_verify:"
983 "selected signature algorithm %s [%04x]",
984 mbedtls_ssl_sig_alg_to_str( *sig_alg ),
985 *sig_alg ) );
Jerry Yuf249ef72022-06-15 17:23:33 +0800986 *algorithm = *sig_alg;
987 return( 0 );
988 }
Jerry Yu67eced02022-02-25 13:37:36 +0800989 }
Jerry Yucc539102022-06-27 16:27:35 +0800990 MBEDTLS_SSL_DEBUG_MSG( 2,
991 ( "select_sig_alg_for_certificate_verify:"
992 "no suitable signature algorithm found" ) );
Jerry Yue91a51a2022-03-22 21:42:50 +0800993 return( -1 );
Jerry Yu67eced02022-02-25 13:37:36 +0800994}
995
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200996MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yu3e536442022-02-15 11:05:59 +0800997static int ssl_tls13_write_certificate_verify_body( mbedtls_ssl_context *ssl,
998 unsigned char *buf,
999 unsigned char *end,
1000 size_t *out_len )
Jerry Yu8511f122022-01-29 10:01:04 +08001001{
1002 int ret;
Jerry Yu3e536442022-02-15 11:05:59 +08001003 unsigned char *p = buf;
Jerry Yu8511f122022-01-29 10:01:04 +08001004 mbedtls_pk_context *own_key;
Jerry Yu3e536442022-02-15 11:05:59 +08001005
Jerry Yu8511f122022-01-29 10:01:04 +08001006 unsigned char handshake_hash[ MBEDTLS_TLS1_3_MD_MAX_SIZE ];
1007 size_t handshake_hash_len;
Jerry Yu3e536442022-02-15 11:05:59 +08001008 unsigned char verify_buffer[ SSL_VERIFY_STRUCT_MAX_SIZE ];
1009 size_t verify_buffer_len;
Jerry Yu67eced02022-02-25 13:37:36 +08001010 mbedtls_pk_type_t pk_type = MBEDTLS_PK_NONE;
Jerry Yu67eced02022-02-25 13:37:36 +08001011 mbedtls_md_type_t md_alg = MBEDTLS_MD_NONE;
pespacek34935872022-05-20 15:43:32 +02001012 psa_algorithm_t psa_algorithm = PSA_ALG_NONE;
Jerry Yu78272072022-02-22 10:28:13 +08001013 uint16_t algorithm = MBEDTLS_TLS1_3_SIG_NONE;
Jerry Yu3e536442022-02-15 11:05:59 +08001014 size_t signature_len = 0;
Przemyslaw Stekielda645252022-09-14 12:50:51 +02001015 unsigned char verify_hash[PSA_HASH_MAX_SIZE];
Jerry Yu3e536442022-02-15 11:05:59 +08001016 size_t verify_hash_len;
pespacekb06acd72022-06-07 13:07:21 +02001017 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Jerry Yu8511f122022-01-29 10:01:04 +08001018
Jerry Yu0b7b1012022-02-23 12:23:05 +08001019 *out_len = 0;
1020
Jerry Yu3e536442022-02-15 11:05:59 +08001021 own_key = mbedtls_ssl_own_key( ssl );
1022 if( own_key == NULL )
Jerry Yu8511f122022-01-29 10:01:04 +08001023 {
Jerry Yu3e536442022-02-15 11:05:59 +08001024 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1025 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Jerry Yu8511f122022-01-29 10:01:04 +08001026 }
1027
Jerry Yu8511f122022-01-29 10:01:04 +08001028 ret = mbedtls_ssl_get_handshake_transcript( ssl,
1029 ssl->handshake->ciphersuite_info->mac,
1030 handshake_hash,
1031 sizeof( handshake_hash ),
1032 &handshake_hash_len );
1033 if( ret != 0 )
1034 return( ret );
1035
1036 MBEDTLS_SSL_DEBUG_BUF( 3, "handshake hash",
1037 handshake_hash,
1038 handshake_hash_len);
1039
Jerry Yu8511f122022-01-29 10:01:04 +08001040 ssl_tls13_create_verify_structure( handshake_hash, handshake_hash_len,
1041 verify_buffer, &verify_buffer_len,
1042 ssl->conf->endpoint );
1043
1044 /*
1045 * struct {
1046 * SignatureScheme algorithm;
1047 * opaque signature<0..2^16-1>;
1048 * } CertificateVerify;
1049 */
Jerry Yuf55886a2022-06-19 11:48:56 +08001050 ret = ssl_tls13_select_sig_alg_for_certificate_verify( ssl, own_key,
1051 &algorithm );
1052 if( ret != 0 )
Jerry Yu8511f122022-01-29 10:01:04 +08001053 {
Jerry Yud66409a2022-02-18 16:42:24 +08001054 MBEDTLS_SSL_DEBUG_MSG( 1,
Jerry Yu2124d052022-02-18 21:07:18 +08001055 ( "signature algorithm not in received or offered list." ) );
Jerry Yu67eced02022-02-25 13:37:36 +08001056
1057 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Signature algorithm is %s",
1058 mbedtls_ssl_sig_alg_to_str( algorithm ) ) );
1059
Jerry Yu71f36f12022-02-23 17:34:29 +08001060 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE,
Jerry Yud66409a2022-02-18 16:42:24 +08001061 MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
Jerry Yu3391ac02022-02-16 11:21:37 +08001062 return( MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
Jerry Yu8511f122022-01-29 10:01:04 +08001063 }
1064
Jerry Yuf3b46b52022-06-19 16:52:27 +08001065 MBEDTLS_SSL_DEBUG_MSG( 2, ( "CertificateVerify with %s",
1066 mbedtls_ssl_sig_alg_to_str( algorithm )) );
1067
Jerry Yu95b743c2022-07-23 11:37:50 +08001068 if( mbedtls_ssl_get_pk_type_and_md_alg_from_sig_alg(
Jerry Yu6c6f1022022-03-25 11:09:50 +08001069 algorithm, &pk_type, &md_alg ) != 0 )
Jerry Yu8c338862022-03-23 13:34:04 +08001070 {
Jerry Yuf8aa9a42022-03-23 20:40:28 +08001071 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Jerry Yu8c338862022-03-23 13:34:04 +08001072 }
1073
Jerry Yu3391ac02022-02-16 11:21:37 +08001074 /* Check there is space for the algorithm identifier (2 bytes) and the
1075 * signature length (2 bytes).
1076 */
1077 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 4 );
Jerry Yu3e536442022-02-15 11:05:59 +08001078 MBEDTLS_PUT_UINT16_BE( algorithm, p, 0 );
1079 p += 2;
Jerry Yu8511f122022-01-29 10:01:04 +08001080
1081 /* Hash verify buffer with indicated hash function */
Manuel Pégourié-Gonnardabac0372022-07-18 13:41:11 +02001082 psa_algorithm = mbedtls_hash_info_psa_from_md( md_alg );
pespacekb06acd72022-06-07 13:07:21 +02001083 status = psa_hash_compute( psa_algorithm,
1084 verify_buffer,
1085 verify_buffer_len,
1086 verify_hash,sizeof( verify_hash ),
1087 &verify_hash_len );
1088 if( status != PSA_SUCCESS )
pespacek670913f2022-06-07 10:53:39 +02001089 return( psa_ssl_status_to_mbedtls( status ) );
Jerry Yu8511f122022-01-29 10:01:04 +08001090
Jerry Yu8511f122022-01-29 10:01:04 +08001091 MBEDTLS_SSL_DEBUG_BUF( 3, "verify hash", verify_hash, verify_hash_len );
1092
Jerry Yu8beb9e12022-03-12 16:23:53 +08001093 if( ( ret = mbedtls_pk_sign_ext( pk_type, own_key,
1094 md_alg, verify_hash, verify_hash_len,
Jerry Yu67eced02022-02-25 13:37:36 +08001095 p + 2, (size_t)( end - ( p + 2 ) ), &signature_len,
1096 ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 )
Jerry Yu8511f122022-01-29 10:01:04 +08001097 {
1098 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_pk_sign", ret );
1099 return( ret );
1100 }
Jerry Yu8511f122022-01-29 10:01:04 +08001101
Jerry Yu3e536442022-02-15 11:05:59 +08001102 MBEDTLS_PUT_UINT16_BE( signature_len, p, 0 );
1103 p += 2 + signature_len;
Jerry Yu8511f122022-01-29 10:01:04 +08001104
Jerry Yu3e536442022-02-15 11:05:59 +08001105 *out_len = (size_t)( p - buf );
1106
Jerry Yu8511f122022-01-29 10:01:04 +08001107 return( ret );
1108}
Jerry Yu8511f122022-01-29 10:01:04 +08001109
Jerry Yu3e536442022-02-15 11:05:59 +08001110int mbedtls_ssl_tls13_write_certificate_verify( mbedtls_ssl_context *ssl )
Jerry Yu8511f122022-01-29 10:01:04 +08001111{
1112 int ret = 0;
Jerry Yuca133a32022-02-15 14:22:05 +08001113 unsigned char *buf;
1114 size_t buf_len, msg_len;
1115
Jerry Yu8511f122022-01-29 10:01:04 +08001116 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write certificate verify" ) );
1117
Ronald Cron8f6d39a2022-03-10 18:56:50 +01001118 MBEDTLS_SSL_PROC_CHK( mbedtls_ssl_start_handshake_msg( ssl,
Jerry Yuca133a32022-02-15 14:22:05 +08001119 MBEDTLS_SSL_HS_CERTIFICATE_VERIFY, &buf, &buf_len ) );
Jerry Yu8511f122022-01-29 10:01:04 +08001120
Jerry Yuca133a32022-02-15 14:22:05 +08001121 MBEDTLS_SSL_PROC_CHK( ssl_tls13_write_certificate_verify_body(
1122 ssl, buf, buf + buf_len, &msg_len ) );
Jerry Yu8511f122022-01-29 10:01:04 +08001123
Ronald Cron8f6d39a2022-03-10 18:56:50 +01001124 mbedtls_ssl_add_hs_msg_to_checksum( ssl, MBEDTLS_SSL_HS_CERTIFICATE_VERIFY,
1125 buf, msg_len );
Jerry Yu8511f122022-01-29 10:01:04 +08001126
Ronald Cron8f6d39a2022-03-10 18:56:50 +01001127 MBEDTLS_SSL_PROC_CHK( mbedtls_ssl_finish_handshake_msg(
Jerry Yuca133a32022-02-15 14:22:05 +08001128 ssl, buf_len, msg_len ) );
Jerry Yu8511f122022-01-29 10:01:04 +08001129
1130cleanup:
1131
1132 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write certificate verify" ) );
1133 return( ret );
1134}
1135
Jerry Yu90f152d2022-01-29 22:12:42 +08001136#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
1137
Jerry Yu5cc35062022-01-28 16:16:08 +08001138/*
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001139 *
XiaokangQianc5c39d52021-11-09 11:55:10 +00001140 * STATE HANDLING: Incoming Finished message.
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001141 */
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001142/*
1143 * Implementation
1144 */
1145
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001146MBEDTLS_CHECK_RETURN_CRITICAL
XiaokangQianaaa0e192021-11-10 03:07:04 +00001147static int ssl_tls13_preprocess_finished_message( mbedtls_ssl_context *ssl )
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001148{
1149 int ret;
1150
XiaokangQianc5c39d52021-11-09 11:55:10 +00001151 ret = mbedtls_ssl_tls13_calculate_verify_data( ssl,
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001152 ssl->handshake->state_local.finished_in.digest,
1153 sizeof( ssl->handshake->state_local.finished_in.digest ),
1154 &ssl->handshake->state_local.finished_in.digest_len,
XiaokangQianc5c39d52021-11-09 11:55:10 +00001155 ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT ?
XiaokangQianc13f9352021-11-11 06:13:22 +00001156 MBEDTLS_SSL_IS_SERVER : MBEDTLS_SSL_IS_CLIENT );
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001157 if( ret != 0 )
1158 {
XiaokangQianc5c39d52021-11-09 11:55:10 +00001159 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_tls13_calculate_verify_data", ret );
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001160 return( ret );
1161 }
1162
1163 return( 0 );
1164}
1165
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001166MBEDTLS_CHECK_RETURN_CRITICAL
XiaokangQianc5c39d52021-11-09 11:55:10 +00001167static int ssl_tls13_parse_finished_message( mbedtls_ssl_context *ssl,
1168 const unsigned char *buf,
1169 const unsigned char *end )
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001170{
XiaokangQian33062842021-11-11 03:37:45 +00001171 /*
1172 * struct {
XiaokangQianc13f9352021-11-11 06:13:22 +00001173 * opaque verify_data[Hash.length];
XiaokangQian33062842021-11-11 03:37:45 +00001174 * } Finished;
1175 */
1176 const unsigned char *expected_verify_data =
1177 ssl->handshake->state_local.finished_in.digest;
1178 size_t expected_verify_data_len =
1179 ssl->handshake->state_local.finished_in.digest_len;
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001180 /* Structural validation */
XiaokangQian33062842021-11-11 03:37:45 +00001181 if( (size_t)( end - buf ) != expected_verify_data_len )
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001182 {
1183 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
1184
1185 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR,
XiaokangQianc13f9352021-11-11 06:13:22 +00001186 MBEDTLS_ERR_SSL_DECODE_ERROR );
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001187 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
1188 }
1189
XiaokangQianc5c39d52021-11-09 11:55:10 +00001190 MBEDTLS_SSL_DEBUG_BUF( 4, "verify_data (self-computed):",
XiaokangQian33062842021-11-11 03:37:45 +00001191 expected_verify_data,
1192 expected_verify_data_len );
XiaokangQianc5c39d52021-11-09 11:55:10 +00001193 MBEDTLS_SSL_DEBUG_BUF( 4, "verify_data (received message):", buf,
XiaokangQian33062842021-11-11 03:37:45 +00001194 expected_verify_data_len );
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001195
1196 /* Semantic validation */
Gabor Mezei685472b2021-11-24 11:17:36 +01001197 if( mbedtls_ct_memcmp( buf,
1198 expected_verify_data,
1199 expected_verify_data_len ) != 0 )
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001200 {
1201 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
1202
XiaokangQian33062842021-11-11 03:37:45 +00001203 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_DECRYPT_ERROR,
XiaokangQianc13f9352021-11-11 06:13:22 +00001204 MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001205 return( MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
1206 }
1207 return( 0 );
1208}
1209
XiaokangQianc5c39d52021-11-09 11:55:10 +00001210int mbedtls_ssl_tls13_process_finished_message( mbedtls_ssl_context *ssl )
1211{
XiaokangQian33062842021-11-11 03:37:45 +00001212 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
XiaokangQianc5c39d52021-11-09 11:55:10 +00001213 unsigned char *buf;
Xiaofei Baieef15042021-11-18 07:29:56 +00001214 size_t buf_len;
XiaokangQianc5c39d52021-11-09 11:55:10 +00001215
XiaokangQiand0aa3e92021-11-10 06:17:40 +00001216 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse finished message" ) );
XiaokangQianc5c39d52021-11-09 11:55:10 +00001217
Xiaofei Bai746f9482021-11-12 08:53:56 +00001218 MBEDTLS_SSL_PROC_CHK( mbedtls_ssl_tls13_fetch_handshake_msg( ssl,
XiaokangQianc5c39d52021-11-09 11:55:10 +00001219 MBEDTLS_SSL_HS_FINISHED,
Xiaofei Baieef15042021-11-18 07:29:56 +00001220 &buf, &buf_len ) );
Jerry Yu0a92d6c2022-05-16 16:54:46 +08001221
1222 /* Preprocessing step: Compute handshake digest */
1223 MBEDTLS_SSL_PROC_CHK( ssl_tls13_preprocess_finished_message( ssl ) );
1224
Xiaofei Baieef15042021-11-18 07:29:56 +00001225 MBEDTLS_SSL_PROC_CHK( ssl_tls13_parse_finished_message( ssl, buf, buf + buf_len ) );
Jerry Yu0a92d6c2022-05-16 16:54:46 +08001226
Ronald Cron8f6d39a2022-03-10 18:56:50 +01001227 mbedtls_ssl_add_hs_msg_to_checksum( ssl, MBEDTLS_SSL_HS_FINISHED,
1228 buf, buf_len );
XiaokangQianc5c39d52021-11-09 11:55:10 +00001229
1230cleanup:
1231
XiaokangQiand0aa3e92021-11-10 06:17:40 +00001232 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse finished message" ) );
XiaokangQianc5c39d52021-11-09 11:55:10 +00001233 return( ret );
1234}
1235
XiaokangQian74af2a82021-09-22 07:40:30 +00001236/*
1237 *
XiaokangQiancc90c942021-11-09 12:30:09 +00001238 * STATE HANDLING: Write and send Finished message.
XiaokangQian74af2a82021-09-22 07:40:30 +00001239 *
1240 */
XiaokangQian74af2a82021-09-22 07:40:30 +00001241/*
XiaokangQian35dc6252021-11-11 08:16:19 +00001242 * Implement
XiaokangQian74af2a82021-09-22 07:40:30 +00001243 */
1244
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001245MBEDTLS_CHECK_RETURN_CRITICAL
XiaokangQian8773aa02021-11-10 07:33:09 +00001246static int ssl_tls13_prepare_finished_message( mbedtls_ssl_context *ssl )
XiaokangQian74af2a82021-09-22 07:40:30 +00001247{
1248 int ret;
1249
1250 /* Compute transcript of handshake up to now. */
XiaokangQiancc90c942021-11-09 12:30:09 +00001251 ret = mbedtls_ssl_tls13_calculate_verify_data( ssl,
XiaokangQian74af2a82021-09-22 07:40:30 +00001252 ssl->handshake->state_local.finished_out.digest,
1253 sizeof( ssl->handshake->state_local.finished_out.digest ),
1254 &ssl->handshake->state_local.finished_out.digest_len,
1255 ssl->conf->endpoint );
1256
1257 if( ret != 0 )
1258 {
Jerry Yu7ca30542021-12-08 15:57:57 +08001259 MBEDTLS_SSL_DEBUG_RET( 1, "calculate_verify_data failed", ret );
XiaokangQian74af2a82021-09-22 07:40:30 +00001260 return( ret );
1261 }
1262
1263 return( 0 );
1264}
1265
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001266MBEDTLS_CHECK_RETURN_CRITICAL
XiaokangQian8773aa02021-11-10 07:33:09 +00001267static int ssl_tls13_write_finished_message_body( mbedtls_ssl_context *ssl,
XiaokangQian35dc6252021-11-11 08:16:19 +00001268 unsigned char *buf,
1269 unsigned char *end,
Xiaofei Baid25fab62021-12-02 06:36:27 +00001270 size_t *out_len )
XiaokangQian74af2a82021-09-22 07:40:30 +00001271{
XiaokangQian8773aa02021-11-10 07:33:09 +00001272 size_t verify_data_len = ssl->handshake->state_local.finished_out.digest_len;
XiaokangQian0fa66432021-11-15 03:33:57 +00001273 /*
1274 * struct {
1275 * opaque verify_data[Hash.length];
1276 * } Finished;
1277 */
XiaokangQian8773aa02021-11-10 07:33:09 +00001278 MBEDTLS_SSL_CHK_BUF_PTR( buf, end, verify_data_len );
XiaokangQian74af2a82021-09-22 07:40:30 +00001279
1280 memcpy( buf, ssl->handshake->state_local.finished_out.digest,
XiaokangQian8773aa02021-11-10 07:33:09 +00001281 verify_data_len );
XiaokangQian74af2a82021-09-22 07:40:30 +00001282
Xiaofei Baid25fab62021-12-02 06:36:27 +00001283 *out_len = verify_data_len;
XiaokangQian74af2a82021-09-22 07:40:30 +00001284 return( 0 );
1285}
XiaokangQianc5c39d52021-11-09 11:55:10 +00001286
XiaokangQian35dc6252021-11-11 08:16:19 +00001287/* Main entry point: orchestrates the other functions */
1288int mbedtls_ssl_tls13_write_finished_message( mbedtls_ssl_context *ssl )
1289{
1290 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1291 unsigned char *buf;
1292 size_t buf_len, msg_len;
1293
1294 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write finished message" ) );
1295
XiaokangQiandce82242021-11-15 06:01:26 +00001296 MBEDTLS_SSL_PROC_CHK( ssl_tls13_prepare_finished_message( ssl ) );
1297
Ronald Cron8f6d39a2022-03-10 18:56:50 +01001298 MBEDTLS_SSL_PROC_CHK( mbedtls_ssl_start_handshake_msg( ssl,
XiaokangQian35dc6252021-11-11 08:16:19 +00001299 MBEDTLS_SSL_HS_FINISHED, &buf, &buf_len ) );
1300
1301 MBEDTLS_SSL_PROC_CHK( ssl_tls13_write_finished_message_body(
1302 ssl, buf, buf + buf_len, &msg_len ) );
1303
Ronald Cron8f6d39a2022-03-10 18:56:50 +01001304 mbedtls_ssl_add_hs_msg_to_checksum( ssl, MBEDTLS_SSL_HS_FINISHED,
1305 buf, msg_len );
XiaokangQian35dc6252021-11-11 08:16:19 +00001306
Ronald Cron8f6d39a2022-03-10 18:56:50 +01001307 MBEDTLS_SSL_PROC_CHK( mbedtls_ssl_finish_handshake_msg(
1308 ssl, buf_len, msg_len ) );
XiaokangQian35dc6252021-11-11 08:16:19 +00001309cleanup:
1310
1311 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write finished message" ) );
1312 return( ret );
1313}
1314
Jerry Yu378254d2021-10-30 21:44:47 +08001315void mbedtls_ssl_tls13_handshake_wrapup( mbedtls_ssl_context *ssl )
1316{
1317
1318 MBEDTLS_SSL_DEBUG_MSG( 3, ( "=> handshake wrapup" ) );
1319
Jerry Yue8c1fca2022-05-18 14:48:56 +08001320 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Switch to application keys for inbound traffic" ) );
1321 mbedtls_ssl_set_inbound_transform ( ssl, ssl->transform_application );
1322
1323 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Switch to application keys for outbound traffic" ) );
1324 mbedtls_ssl_set_outbound_transform( ssl, ssl->transform_application );
1325
Jerry Yu378254d2021-10-30 21:44:47 +08001326 /*
Jerry Yucfe64f02021-11-15 13:54:06 +08001327 * Free the previous session and switch to the current one.
Jerry Yu378254d2021-10-30 21:44:47 +08001328 */
1329 if( ssl->session )
1330 {
Jerry Yu378254d2021-10-30 21:44:47 +08001331 mbedtls_ssl_session_free( ssl->session );
1332 mbedtls_free( ssl->session );
1333 }
1334 ssl->session = ssl->session_negotiate;
1335 ssl->session_negotiate = NULL;
1336
1337 MBEDTLS_SSL_DEBUG_MSG( 3, ( "<= handshake wrapup" ) );
1338}
1339
Ronald Cron49ad6192021-11-24 16:25:31 +01001340/*
1341 *
1342 * STATE HANDLING: Write ChangeCipherSpec
1343 *
1344 */
1345#if defined(MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001346MBEDTLS_CHECK_RETURN_CRITICAL
Ronald Cron49ad6192021-11-24 16:25:31 +01001347static int ssl_tls13_write_change_cipher_spec_body( mbedtls_ssl_context *ssl,
1348 unsigned char *buf,
1349 unsigned char *end,
1350 size_t *olen )
1351{
1352 ((void) ssl);
1353
1354 MBEDTLS_SSL_CHK_BUF_PTR( buf, end, 1 );
1355 buf[0] = 1;
1356 *olen = 1;
1357
1358 return( 0 );
1359}
1360
Ronald Cron49ad6192021-11-24 16:25:31 +01001361int mbedtls_ssl_tls13_write_change_cipher_spec( mbedtls_ssl_context *ssl )
1362{
1363 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1364
1365 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write change cipher spec" ) );
1366
Ronald Cron49ad6192021-11-24 16:25:31 +01001367 /* Write CCS message */
1368 MBEDTLS_SSL_PROC_CHK( ssl_tls13_write_change_cipher_spec_body(
1369 ssl, ssl->out_msg,
1370 ssl->out_msg + MBEDTLS_SSL_OUT_CONTENT_LEN,
1371 &ssl->out_msglen ) );
1372
1373 ssl->out_msgtype = MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC;
1374
Ronald Cron49ad6192021-11-24 16:25:31 +01001375 /* Dispatch message */
Ronald Cron66dbf912022-02-02 15:33:46 +01001376 MBEDTLS_SSL_PROC_CHK( mbedtls_ssl_write_record( ssl, 0 ) );
Ronald Cron49ad6192021-11-24 16:25:31 +01001377
1378cleanup:
1379
1380 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write change cipher spec" ) );
1381 return( ret );
1382}
1383
1384#endif /* MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE */
1385
XiaokangQian78b1fa72022-01-19 06:56:30 +00001386/* Reset SSL context and update hash for handling HRR.
1387 *
1388 * Replace Transcript-Hash(X) by
1389 * Transcript-Hash( message_hash ||
1390 * 00 00 Hash.length ||
1391 * X )
1392 * A few states of the handshake are preserved, including:
1393 * - session ID
1394 * - session ticket
1395 * - negotiated ciphersuite
1396 */
1397int mbedtls_ssl_reset_transcript_for_hrr( mbedtls_ssl_context *ssl )
1398{
1399 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Przemyslaw Stekielda645252022-09-14 12:50:51 +02001400 unsigned char hash_transcript[PSA_HASH_MAX_SIZE + 4];
XiaokangQian0ece9982022-01-24 08:56:23 +00001401 size_t hash_len;
XiaokangQian78b1fa72022-01-19 06:56:30 +00001402 const mbedtls_ssl_ciphersuite_t *ciphersuite_info;
1403 uint16_t cipher_suite = ssl->session_negotiate->ciphersuite;
1404 ciphersuite_info = mbedtls_ssl_ciphersuite_from_id( cipher_suite );
1405
1406 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Reset SSL session for HRR" ) );
1407
XiaokangQian0ece9982022-01-24 08:56:23 +00001408 ret = mbedtls_ssl_get_handshake_transcript( ssl, ciphersuite_info->mac,
1409 hash_transcript + 4,
Przemyslaw Stekielda645252022-09-14 12:50:51 +02001410 PSA_HASH_MAX_SIZE,
XiaokangQian0ece9982022-01-24 08:56:23 +00001411 &hash_len );
1412 if( ret != 0 )
1413 {
1414 MBEDTLS_SSL_DEBUG_RET( 4, "mbedtls_ssl_get_handshake_transcript", ret );
1415 return( ret );
1416 }
1417
1418 hash_transcript[0] = MBEDTLS_SSL_HS_MESSAGE_HASH;
1419 hash_transcript[1] = 0;
1420 hash_transcript[2] = 0;
1421 hash_transcript[3] = (unsigned char) hash_len;
1422
1423 hash_len += 4;
1424
Przemek Stekiel9dfbf3a2022-09-06 07:40:46 +02001425#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
XiaokangQian78b1fa72022-01-19 06:56:30 +00001426 if( ciphersuite_info->mac == MBEDTLS_MD_SHA256 )
1427 {
XiaokangQian78b1fa72022-01-19 06:56:30 +00001428 MBEDTLS_SSL_DEBUG_BUF( 4, "Truncated SHA-256 handshake transcript",
XiaokangQian0ece9982022-01-24 08:56:23 +00001429 hash_transcript, hash_len );
XiaokangQian78b1fa72022-01-19 06:56:30 +00001430
1431#if defined(MBEDTLS_USE_PSA_CRYPTO)
1432 psa_hash_abort( &ssl->handshake->fin_sha256_psa );
1433 psa_hash_setup( &ssl->handshake->fin_sha256_psa, PSA_ALG_SHA_256 );
1434#else
1435 mbedtls_sha256_starts( &ssl->handshake->fin_sha256, 0 );
1436#endif
XiaokangQian78b1fa72022-01-19 06:56:30 +00001437 }
Przemek Stekiel9dfbf3a2022-09-06 07:40:46 +02001438#endif /* MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA */
1439#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Przemek Stekiel0852ef82022-09-07 10:56:30 +02001440 if( ciphersuite_info->mac == MBEDTLS_MD_SHA384 )
XiaokangQian78b1fa72022-01-19 06:56:30 +00001441 {
XiaokangQian78b1fa72022-01-19 06:56:30 +00001442 MBEDTLS_SSL_DEBUG_BUF( 4, "Truncated SHA-384 handshake transcript",
XiaokangQian0ece9982022-01-24 08:56:23 +00001443 hash_transcript, hash_len );
XiaokangQian78b1fa72022-01-19 06:56:30 +00001444
1445#if defined(MBEDTLS_USE_PSA_CRYPTO)
1446 psa_hash_abort( &ssl->handshake->fin_sha384_psa );
1447 psa_hash_setup( &ssl->handshake->fin_sha384_psa, PSA_ALG_SHA_384 );
1448#else
Andrzej Kureka242e832022-08-11 10:03:14 -04001449 mbedtls_sha512_starts( &ssl->handshake->fin_sha384, 1 );
XiaokangQian78b1fa72022-01-19 06:56:30 +00001450#endif
XiaokangQian78b1fa72022-01-19 06:56:30 +00001451 }
Przemek Stekiel9dfbf3a2022-09-06 07:40:46 +02001452#endif /* MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA */
Przemek Stekiel47e3cb12022-09-02 13:17:03 +02001453#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA) || defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
XiaokangQian0ece9982022-01-24 08:56:23 +00001454 ssl->handshake->update_checksum( ssl, hash_transcript, hash_len );
Przemek Stekiel47e3cb12022-09-02 13:17:03 +02001455#endif /* MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA || MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA */
Przemyslaw Stekiel4b3fff42022-02-14 16:39:52 +01001456
XiaokangQian78b1fa72022-01-19 06:56:30 +00001457 return( ret );
1458}
1459
XiaokangQian9b5d04b2022-04-10 10:20:43 +00001460#if defined(MBEDTLS_ECDH_C)
XiaokangQian7807f9f2022-02-15 10:04:37 +00001461
XiaokangQian9b5d04b2022-04-10 10:20:43 +00001462int mbedtls_ssl_tls13_read_public_ecdhe_share( mbedtls_ssl_context *ssl,
1463 const unsigned char *buf,
1464 size_t buf_len )
XiaokangQian7807f9f2022-02-15 10:04:37 +00001465{
XiaokangQian9b5d04b2022-04-10 10:20:43 +00001466 uint8_t *p = (uint8_t*)buf;
XiaokangQiancfd925f2022-04-14 07:10:37 +00001467 const uint8_t *end = buf + buf_len;
XiaokangQian9b5d04b2022-04-10 10:20:43 +00001468 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001469
XiaokangQian9b5d04b2022-04-10 10:20:43 +00001470 /* Get size of the TLS opaque key_exchange field of the KeyShareEntry struct. */
Ronald Cron154d1b62022-06-01 15:33:26 +02001471 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 2 );
XiaokangQian9b5d04b2022-04-10 10:20:43 +00001472 uint16_t peerkey_len = MBEDTLS_GET_UINT16_BE( p, 0 );
1473 p += 2;
XiaokangQian3207a322022-02-23 03:15:27 +00001474
XiaokangQian9b5d04b2022-04-10 10:20:43 +00001475 /* Check if key size is consistent with given buffer length. */
Ronald Cron154d1b62022-06-01 15:33:26 +02001476 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, peerkey_len );
XiaokangQian9b5d04b2022-04-10 10:20:43 +00001477
1478 /* Store peer's ECDH public key. */
1479 memcpy( handshake->ecdh_psa_peerkey, p, peerkey_len );
1480 handshake->ecdh_psa_peerkey_len = peerkey_len;
1481
XiaokangQian3207a322022-02-23 03:15:27 +00001482 return( 0 );
1483}
Jerry Yu89e103c2022-03-30 22:43:29 +08001484
1485int mbedtls_ssl_tls13_generate_and_write_ecdh_key_exchange(
1486 mbedtls_ssl_context *ssl,
1487 uint16_t named_group,
1488 unsigned char *buf,
1489 unsigned char *end,
1490 size_t *out_len )
1491{
1492 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
1493 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
1494 psa_key_attributes_t key_attributes;
1495 size_t own_pubkey_len;
1496 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
1497 size_t ecdh_bits = 0;
1498
1499 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Perform PSA-based ECDH computation." ) );
1500
1501 /* Convert EC group to PSA key type. */
1502 if( ( handshake->ecdh_psa_type =
1503 mbedtls_psa_parse_tls_ecc_group( named_group, &ecdh_bits ) ) == 0 )
1504 return( MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
1505
1506 ssl->handshake->ecdh_bits = ecdh_bits;
1507
1508 key_attributes = psa_key_attributes_init();
1509 psa_set_key_usage_flags( &key_attributes, PSA_KEY_USAGE_DERIVE );
1510 psa_set_key_algorithm( &key_attributes, PSA_ALG_ECDH );
1511 psa_set_key_type( &key_attributes, handshake->ecdh_psa_type );
1512 psa_set_key_bits( &key_attributes, handshake->ecdh_bits );
1513
1514 /* Generate ECDH private key. */
1515 status = psa_generate_key( &key_attributes,
1516 &handshake->ecdh_psa_privkey );
1517 if( status != PSA_SUCCESS )
1518 {
1519 ret = psa_ssl_status_to_mbedtls( status );
1520 MBEDTLS_SSL_DEBUG_RET( 1, "psa_generate_key", ret );
1521 return( ret );
1522
1523 }
1524
1525 /* Export the public part of the ECDH private key from PSA. */
1526 status = psa_export_public_key( handshake->ecdh_psa_privkey,
1527 buf, (size_t)( end - buf ),
1528 &own_pubkey_len );
1529 if( status != PSA_SUCCESS )
1530 {
1531 ret = psa_ssl_status_to_mbedtls( status );
1532 MBEDTLS_SSL_DEBUG_RET( 1, "psa_export_public_key", ret );
1533 return( ret );
1534
1535 }
1536
1537 *out_len = own_pubkey_len;
1538
1539 return( 0 );
1540}
XiaokangQian9b5d04b2022-04-10 10:20:43 +00001541#endif /* MBEDTLS_ECDH_C */
XiaokangQian7807f9f2022-02-15 10:04:37 +00001542
XiaokangQian008d2bf2022-07-14 07:54:01 +00001543#if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
1544/* Check if we have any PSK to offer, returns 0 if PSK is available.
1545 * Assign the psk and ticket if pointers are present.
1546 */
1547int mbedtls_ssl_get_psk_to_offer(
1548 const mbedtls_ssl_context *ssl,
1549 int *psk_type,
1550 const unsigned char **psk, size_t *psk_len,
1551 const unsigned char **psk_identity, size_t *psk_identity_len )
1552{
1553 int ptrs_present = 0;
1554
XiaokangQian86981952022-07-19 09:51:50 +00001555 if( psk_type != NULL && psk != NULL && psk_len != NULL &&
XiaokangQian008d2bf2022-07-14 07:54:01 +00001556 psk_identity != NULL && psk_identity_len != NULL )
1557 {
1558 ptrs_present = 1;
1559 }
1560
1561 /* Check if an external PSK has been configured. */
1562 if( ssl->conf->psk != NULL )
1563 {
1564 if( ptrs_present )
1565 {
XiaokangQian86981952022-07-19 09:51:50 +00001566 *psk_type = MBEDTLS_SSL_TLS1_3_PSK_EXTERNAL;
XiaokangQian008d2bf2022-07-14 07:54:01 +00001567 *psk = ssl->conf->psk;
1568 *psk_len = ssl->conf->psk_len;
1569 *psk_identity = ssl->conf->psk_identity;
1570 *psk_identity_len = ssl->conf->psk_identity_len;
1571 }
1572
XiaokangQian008d2bf2022-07-14 07:54:01 +00001573 return( 0 );
1574 }
1575
1576 return( 1 );
1577}
1578#endif /* MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED */
1579
Jerry Yufb4b6472022-01-27 15:03:26 +08001580#endif /* MBEDTLS_SSL_TLS_C && MBEDTLS_SSL_PROTO_TLS1_3 */