blob: 64e134dfac31981465898112b2884d3c2f80180b [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 {
Jerry Yu0c6be8f2022-06-20 20:42:00 +0800909 case MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA256:
Jerry Yua1255e62022-06-24 10:10:47 +0800910 return( key_size <= 3072 );
Jerry Yu0c6be8f2022-06-20 20:42:00 +0800911
Jerry Yu0c6be8f2022-06-20 20:42:00 +0800912 case MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA384:
Jerry Yua1255e62022-06-24 10:10:47 +0800913 return( key_size <= 7680 );
Jerry Yu0c6be8f2022-06-20 20:42:00 +0800914
Jerry Yu0c6be8f2022-06-20 20:42:00 +0800915 case MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA512:
Jerry Yua1255e62022-06-24 10:10:47 +0800916 return( 1 );
Jerry Yuc2e04932022-06-27 22:13:03 +0800917
Jerry Yu0c6be8f2022-06-20 20:42:00 +0800918 default:
919 break;
Jerry Yucef3f332022-03-22 23:00:13 +0800920 }
Jerry Yu67eced02022-02-25 13:37:36 +0800921 break;
Jerry Yu0c6be8f2022-06-20 20:42:00 +0800922
Jerry Yu67eced02022-02-25 13:37:36 +0800923 default:
Jerry Yu67eced02022-02-25 13:37:36 +0800924 break;
925 }
Jerry Yu0c6be8f2022-06-20 20:42:00 +0800926
927 return( 0 );
928}
929
Ronald Cronce7d76e2022-07-08 18:56:49 +0200930MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yuf55886a2022-06-19 11:48:56 +0800931static int ssl_tls13_select_sig_alg_for_certificate_verify(
932 mbedtls_ssl_context *ssl,
Jerry Yuf249ef72022-06-15 17:23:33 +0800933 mbedtls_pk_context *own_key,
934 uint16_t *algorithm )
Jerry Yu67eced02022-02-25 13:37:36 +0800935{
Jerry Yuf249ef72022-06-15 17:23:33 +0800936 uint16_t *sig_alg = ssl->handshake->received_sig_algs;
937
Jerry Yu67eced02022-02-25 13:37:36 +0800938 *algorithm = MBEDTLS_TLS1_3_SIG_NONE;
Jerry Yuf249ef72022-06-15 17:23:33 +0800939 for( ; *sig_alg != MBEDTLS_TLS1_3_SIG_NONE ; sig_alg++ )
Jerry Yu67eced02022-02-25 13:37:36 +0800940 {
Jerry Yu660cb422022-06-28 16:17:58 +0800941 if( mbedtls_ssl_sig_alg_is_offered( ssl, *sig_alg ) &&
Jerry Yu959e5e02022-06-29 09:49:02 +0800942 mbedtls_ssl_tls13_sig_alg_for_cert_verify_is_supported( *sig_alg ) &&
943 mbedtls_ssl_tls13_check_sig_alg_cert_key_match( *sig_alg, own_key ) )
Jerry Yuf249ef72022-06-15 17:23:33 +0800944 {
Jerry Yucc539102022-06-27 16:27:35 +0800945 MBEDTLS_SSL_DEBUG_MSG( 3,
946 ( "select_sig_alg_for_certificate_verify:"
947 "selected signature algorithm %s [%04x]",
948 mbedtls_ssl_sig_alg_to_str( *sig_alg ),
949 *sig_alg ) );
Jerry Yuf249ef72022-06-15 17:23:33 +0800950 *algorithm = *sig_alg;
951 return( 0 );
952 }
Jerry Yu67eced02022-02-25 13:37:36 +0800953 }
Jerry Yucc539102022-06-27 16:27:35 +0800954 MBEDTLS_SSL_DEBUG_MSG( 2,
955 ( "select_sig_alg_for_certificate_verify:"
956 "no suitable signature algorithm found" ) );
Jerry Yue91a51a2022-03-22 21:42:50 +0800957 return( -1 );
Jerry Yu67eced02022-02-25 13:37:36 +0800958}
959
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200960MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yu3e536442022-02-15 11:05:59 +0800961static int ssl_tls13_write_certificate_verify_body( mbedtls_ssl_context *ssl,
962 unsigned char *buf,
963 unsigned char *end,
964 size_t *out_len )
Jerry Yu8511f122022-01-29 10:01:04 +0800965{
966 int ret;
Jerry Yu3e536442022-02-15 11:05:59 +0800967 unsigned char *p = buf;
Jerry Yu8511f122022-01-29 10:01:04 +0800968 mbedtls_pk_context *own_key;
Jerry Yu3e536442022-02-15 11:05:59 +0800969
Jerry Yu8511f122022-01-29 10:01:04 +0800970 unsigned char handshake_hash[ MBEDTLS_TLS1_3_MD_MAX_SIZE ];
971 size_t handshake_hash_len;
Jerry Yu3e536442022-02-15 11:05:59 +0800972 unsigned char verify_buffer[ SSL_VERIFY_STRUCT_MAX_SIZE ];
973 size_t verify_buffer_len;
Jerry Yu67eced02022-02-25 13:37:36 +0800974 mbedtls_pk_type_t pk_type = MBEDTLS_PK_NONE;
Jerry Yu67eced02022-02-25 13:37:36 +0800975 mbedtls_md_type_t md_alg = MBEDTLS_MD_NONE;
pespacek34935872022-05-20 15:43:32 +0200976 psa_algorithm_t psa_algorithm = PSA_ALG_NONE;
Jerry Yu78272072022-02-22 10:28:13 +0800977 uint16_t algorithm = MBEDTLS_TLS1_3_SIG_NONE;
Jerry Yu3e536442022-02-15 11:05:59 +0800978 size_t signature_len = 0;
Jerry Yu3391ac02022-02-16 11:21:37 +0800979 unsigned char verify_hash[ MBEDTLS_MD_MAX_SIZE ];
Jerry Yu3e536442022-02-15 11:05:59 +0800980 size_t verify_hash_len;
pespacekb06acd72022-06-07 13:07:21 +0200981 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Jerry Yu8511f122022-01-29 10:01:04 +0800982
Jerry Yu0b7b1012022-02-23 12:23:05 +0800983 *out_len = 0;
984
Jerry Yu3e536442022-02-15 11:05:59 +0800985 own_key = mbedtls_ssl_own_key( ssl );
986 if( own_key == NULL )
Jerry Yu8511f122022-01-29 10:01:04 +0800987 {
Jerry Yu3e536442022-02-15 11:05:59 +0800988 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
989 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Jerry Yu8511f122022-01-29 10:01:04 +0800990 }
991
Jerry Yu8511f122022-01-29 10:01:04 +0800992 ret = mbedtls_ssl_get_handshake_transcript( ssl,
993 ssl->handshake->ciphersuite_info->mac,
994 handshake_hash,
995 sizeof( handshake_hash ),
996 &handshake_hash_len );
997 if( ret != 0 )
998 return( ret );
999
1000 MBEDTLS_SSL_DEBUG_BUF( 3, "handshake hash",
1001 handshake_hash,
1002 handshake_hash_len);
1003
Jerry Yu8511f122022-01-29 10:01:04 +08001004 ssl_tls13_create_verify_structure( handshake_hash, handshake_hash_len,
1005 verify_buffer, &verify_buffer_len,
1006 ssl->conf->endpoint );
1007
1008 /*
1009 * struct {
1010 * SignatureScheme algorithm;
1011 * opaque signature<0..2^16-1>;
1012 * } CertificateVerify;
1013 */
Jerry Yuf55886a2022-06-19 11:48:56 +08001014 ret = ssl_tls13_select_sig_alg_for_certificate_verify( ssl, own_key,
1015 &algorithm );
1016 if( ret != 0 )
Jerry Yu8511f122022-01-29 10:01:04 +08001017 {
Jerry Yud66409a2022-02-18 16:42:24 +08001018 MBEDTLS_SSL_DEBUG_MSG( 1,
Jerry Yu2124d052022-02-18 21:07:18 +08001019 ( "signature algorithm not in received or offered list." ) );
Jerry Yu67eced02022-02-25 13:37:36 +08001020
1021 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Signature algorithm is %s",
1022 mbedtls_ssl_sig_alg_to_str( algorithm ) ) );
1023
Jerry Yu71f36f12022-02-23 17:34:29 +08001024 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE,
Jerry Yud66409a2022-02-18 16:42:24 +08001025 MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
Jerry Yu3391ac02022-02-16 11:21:37 +08001026 return( MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
Jerry Yu8511f122022-01-29 10:01:04 +08001027 }
1028
Jerry Yuf3b46b52022-06-19 16:52:27 +08001029 MBEDTLS_SSL_DEBUG_MSG( 2, ( "CertificateVerify with %s",
1030 mbedtls_ssl_sig_alg_to_str( algorithm )) );
1031
Jerry Yu95b743c2022-07-23 11:37:50 +08001032 if( mbedtls_ssl_get_pk_type_and_md_alg_from_sig_alg(
Jerry Yu6c6f1022022-03-25 11:09:50 +08001033 algorithm, &pk_type, &md_alg ) != 0 )
Jerry Yu8c338862022-03-23 13:34:04 +08001034 {
Jerry Yuf8aa9a42022-03-23 20:40:28 +08001035 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Jerry Yu8c338862022-03-23 13:34:04 +08001036 }
1037
Jerry Yu3391ac02022-02-16 11:21:37 +08001038 /* Check there is space for the algorithm identifier (2 bytes) and the
1039 * signature length (2 bytes).
1040 */
1041 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 4 );
Jerry Yu3e536442022-02-15 11:05:59 +08001042 MBEDTLS_PUT_UINT16_BE( algorithm, p, 0 );
1043 p += 2;
Jerry Yu8511f122022-01-29 10:01:04 +08001044
1045 /* Hash verify buffer with indicated hash function */
Manuel Pégourié-Gonnardabac0372022-07-18 13:41:11 +02001046 psa_algorithm = mbedtls_hash_info_psa_from_md( md_alg );
pespacekb06acd72022-06-07 13:07:21 +02001047 status = psa_hash_compute( psa_algorithm,
1048 verify_buffer,
1049 verify_buffer_len,
1050 verify_hash,sizeof( verify_hash ),
1051 &verify_hash_len );
1052 if( status != PSA_SUCCESS )
pespacek670913f2022-06-07 10:53:39 +02001053 return( psa_ssl_status_to_mbedtls( status ) );
Jerry Yu8511f122022-01-29 10:01:04 +08001054
Jerry Yu8511f122022-01-29 10:01:04 +08001055 MBEDTLS_SSL_DEBUG_BUF( 3, "verify hash", verify_hash, verify_hash_len );
1056
Jerry Yu8beb9e12022-03-12 16:23:53 +08001057 if( ( ret = mbedtls_pk_sign_ext( pk_type, own_key,
1058 md_alg, verify_hash, verify_hash_len,
Jerry Yu67eced02022-02-25 13:37:36 +08001059 p + 2, (size_t)( end - ( p + 2 ) ), &signature_len,
1060 ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 )
Jerry Yu8511f122022-01-29 10:01:04 +08001061 {
1062 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_pk_sign", ret );
1063 return( ret );
1064 }
Jerry Yu8511f122022-01-29 10:01:04 +08001065
Jerry Yu3e536442022-02-15 11:05:59 +08001066 MBEDTLS_PUT_UINT16_BE( signature_len, p, 0 );
1067 p += 2 + signature_len;
Jerry Yu8511f122022-01-29 10:01:04 +08001068
Jerry Yu3e536442022-02-15 11:05:59 +08001069 *out_len = (size_t)( p - buf );
1070
Jerry Yu8511f122022-01-29 10:01:04 +08001071 return( ret );
1072}
Jerry Yu8511f122022-01-29 10:01:04 +08001073
Jerry Yu3e536442022-02-15 11:05:59 +08001074int mbedtls_ssl_tls13_write_certificate_verify( mbedtls_ssl_context *ssl )
Jerry Yu8511f122022-01-29 10:01:04 +08001075{
1076 int ret = 0;
Jerry Yuca133a32022-02-15 14:22:05 +08001077 unsigned char *buf;
1078 size_t buf_len, msg_len;
1079
Jerry Yu8511f122022-01-29 10:01:04 +08001080 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write certificate verify" ) );
1081
Ronald Cron8f6d39a2022-03-10 18:56:50 +01001082 MBEDTLS_SSL_PROC_CHK( mbedtls_ssl_start_handshake_msg( ssl,
Jerry Yuca133a32022-02-15 14:22:05 +08001083 MBEDTLS_SSL_HS_CERTIFICATE_VERIFY, &buf, &buf_len ) );
Jerry Yu8511f122022-01-29 10:01:04 +08001084
Jerry Yuca133a32022-02-15 14:22:05 +08001085 MBEDTLS_SSL_PROC_CHK( ssl_tls13_write_certificate_verify_body(
1086 ssl, buf, buf + buf_len, &msg_len ) );
Jerry Yu8511f122022-01-29 10:01:04 +08001087
Ronald Cron8f6d39a2022-03-10 18:56:50 +01001088 mbedtls_ssl_add_hs_msg_to_checksum( ssl, MBEDTLS_SSL_HS_CERTIFICATE_VERIFY,
1089 buf, msg_len );
Jerry Yu8511f122022-01-29 10:01:04 +08001090
Ronald Cron8f6d39a2022-03-10 18:56:50 +01001091 MBEDTLS_SSL_PROC_CHK( mbedtls_ssl_finish_handshake_msg(
Jerry Yuca133a32022-02-15 14:22:05 +08001092 ssl, buf_len, msg_len ) );
Jerry Yu8511f122022-01-29 10:01:04 +08001093
1094cleanup:
1095
1096 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write certificate verify" ) );
1097 return( ret );
1098}
1099
Jerry Yu90f152d2022-01-29 22:12:42 +08001100#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
1101
Jerry Yu5cc35062022-01-28 16:16:08 +08001102/*
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001103 *
XiaokangQianc5c39d52021-11-09 11:55:10 +00001104 * STATE HANDLING: Incoming Finished message.
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001105 */
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001106/*
1107 * Implementation
1108 */
1109
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001110MBEDTLS_CHECK_RETURN_CRITICAL
XiaokangQianaaa0e192021-11-10 03:07:04 +00001111static int ssl_tls13_preprocess_finished_message( mbedtls_ssl_context *ssl )
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001112{
1113 int ret;
1114
XiaokangQianc5c39d52021-11-09 11:55:10 +00001115 ret = mbedtls_ssl_tls13_calculate_verify_data( ssl,
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001116 ssl->handshake->state_local.finished_in.digest,
1117 sizeof( ssl->handshake->state_local.finished_in.digest ),
1118 &ssl->handshake->state_local.finished_in.digest_len,
XiaokangQianc5c39d52021-11-09 11:55:10 +00001119 ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT ?
XiaokangQianc13f9352021-11-11 06:13:22 +00001120 MBEDTLS_SSL_IS_SERVER : MBEDTLS_SSL_IS_CLIENT );
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001121 if( ret != 0 )
1122 {
XiaokangQianc5c39d52021-11-09 11:55:10 +00001123 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_tls13_calculate_verify_data", ret );
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001124 return( ret );
1125 }
1126
1127 return( 0 );
1128}
1129
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001130MBEDTLS_CHECK_RETURN_CRITICAL
XiaokangQianc5c39d52021-11-09 11:55:10 +00001131static int ssl_tls13_parse_finished_message( mbedtls_ssl_context *ssl,
1132 const unsigned char *buf,
1133 const unsigned char *end )
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001134{
XiaokangQian33062842021-11-11 03:37:45 +00001135 /*
1136 * struct {
XiaokangQianc13f9352021-11-11 06:13:22 +00001137 * opaque verify_data[Hash.length];
XiaokangQian33062842021-11-11 03:37:45 +00001138 * } Finished;
1139 */
1140 const unsigned char *expected_verify_data =
1141 ssl->handshake->state_local.finished_in.digest;
1142 size_t expected_verify_data_len =
1143 ssl->handshake->state_local.finished_in.digest_len;
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001144 /* Structural validation */
XiaokangQian33062842021-11-11 03:37:45 +00001145 if( (size_t)( end - buf ) != expected_verify_data_len )
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001146 {
1147 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
1148
1149 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR,
XiaokangQianc13f9352021-11-11 06:13:22 +00001150 MBEDTLS_ERR_SSL_DECODE_ERROR );
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001151 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
1152 }
1153
XiaokangQianc5c39d52021-11-09 11:55:10 +00001154 MBEDTLS_SSL_DEBUG_BUF( 4, "verify_data (self-computed):",
XiaokangQian33062842021-11-11 03:37:45 +00001155 expected_verify_data,
1156 expected_verify_data_len );
XiaokangQianc5c39d52021-11-09 11:55:10 +00001157 MBEDTLS_SSL_DEBUG_BUF( 4, "verify_data (received message):", buf,
XiaokangQian33062842021-11-11 03:37:45 +00001158 expected_verify_data_len );
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001159
1160 /* Semantic validation */
Gabor Mezei685472b2021-11-24 11:17:36 +01001161 if( mbedtls_ct_memcmp( buf,
1162 expected_verify_data,
1163 expected_verify_data_len ) != 0 )
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001164 {
1165 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
1166
XiaokangQian33062842021-11-11 03:37:45 +00001167 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_DECRYPT_ERROR,
XiaokangQianc13f9352021-11-11 06:13:22 +00001168 MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
XiaokangQianaa5f5c12021-09-18 06:20:25 +00001169 return( MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
1170 }
1171 return( 0 );
1172}
1173
XiaokangQianc5c39d52021-11-09 11:55:10 +00001174int mbedtls_ssl_tls13_process_finished_message( mbedtls_ssl_context *ssl )
1175{
XiaokangQian33062842021-11-11 03:37:45 +00001176 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
XiaokangQianc5c39d52021-11-09 11:55:10 +00001177 unsigned char *buf;
Xiaofei Baieef15042021-11-18 07:29:56 +00001178 size_t buf_len;
XiaokangQianc5c39d52021-11-09 11:55:10 +00001179
XiaokangQiand0aa3e92021-11-10 06:17:40 +00001180 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse finished message" ) );
XiaokangQianc5c39d52021-11-09 11:55:10 +00001181
Xiaofei Bai746f9482021-11-12 08:53:56 +00001182 MBEDTLS_SSL_PROC_CHK( mbedtls_ssl_tls13_fetch_handshake_msg( ssl,
XiaokangQianc5c39d52021-11-09 11:55:10 +00001183 MBEDTLS_SSL_HS_FINISHED,
Xiaofei Baieef15042021-11-18 07:29:56 +00001184 &buf, &buf_len ) );
Jerry Yu0a92d6c2022-05-16 16:54:46 +08001185
1186 /* Preprocessing step: Compute handshake digest */
1187 MBEDTLS_SSL_PROC_CHK( ssl_tls13_preprocess_finished_message( ssl ) );
1188
Xiaofei Baieef15042021-11-18 07:29:56 +00001189 MBEDTLS_SSL_PROC_CHK( ssl_tls13_parse_finished_message( ssl, buf, buf + buf_len ) );
Jerry Yu0a92d6c2022-05-16 16:54:46 +08001190
Ronald Cron8f6d39a2022-03-10 18:56:50 +01001191 mbedtls_ssl_add_hs_msg_to_checksum( ssl, MBEDTLS_SSL_HS_FINISHED,
1192 buf, buf_len );
XiaokangQianc5c39d52021-11-09 11:55:10 +00001193
1194cleanup:
1195
XiaokangQiand0aa3e92021-11-10 06:17:40 +00001196 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse finished message" ) );
XiaokangQianc5c39d52021-11-09 11:55:10 +00001197 return( ret );
1198}
1199
XiaokangQian74af2a82021-09-22 07:40:30 +00001200/*
1201 *
XiaokangQiancc90c942021-11-09 12:30:09 +00001202 * STATE HANDLING: Write and send Finished message.
XiaokangQian74af2a82021-09-22 07:40:30 +00001203 *
1204 */
XiaokangQian74af2a82021-09-22 07:40:30 +00001205/*
XiaokangQian35dc6252021-11-11 08:16:19 +00001206 * Implement
XiaokangQian74af2a82021-09-22 07:40:30 +00001207 */
1208
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001209MBEDTLS_CHECK_RETURN_CRITICAL
XiaokangQian8773aa02021-11-10 07:33:09 +00001210static int ssl_tls13_prepare_finished_message( mbedtls_ssl_context *ssl )
XiaokangQian74af2a82021-09-22 07:40:30 +00001211{
1212 int ret;
1213
1214 /* Compute transcript of handshake up to now. */
XiaokangQiancc90c942021-11-09 12:30:09 +00001215 ret = mbedtls_ssl_tls13_calculate_verify_data( ssl,
XiaokangQian74af2a82021-09-22 07:40:30 +00001216 ssl->handshake->state_local.finished_out.digest,
1217 sizeof( ssl->handshake->state_local.finished_out.digest ),
1218 &ssl->handshake->state_local.finished_out.digest_len,
1219 ssl->conf->endpoint );
1220
1221 if( ret != 0 )
1222 {
Jerry Yu7ca30542021-12-08 15:57:57 +08001223 MBEDTLS_SSL_DEBUG_RET( 1, "calculate_verify_data failed", ret );
XiaokangQian74af2a82021-09-22 07:40:30 +00001224 return( ret );
1225 }
1226
1227 return( 0 );
1228}
1229
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001230MBEDTLS_CHECK_RETURN_CRITICAL
XiaokangQian8773aa02021-11-10 07:33:09 +00001231static int ssl_tls13_write_finished_message_body( mbedtls_ssl_context *ssl,
XiaokangQian35dc6252021-11-11 08:16:19 +00001232 unsigned char *buf,
1233 unsigned char *end,
Xiaofei Baid25fab62021-12-02 06:36:27 +00001234 size_t *out_len )
XiaokangQian74af2a82021-09-22 07:40:30 +00001235{
XiaokangQian8773aa02021-11-10 07:33:09 +00001236 size_t verify_data_len = ssl->handshake->state_local.finished_out.digest_len;
XiaokangQian0fa66432021-11-15 03:33:57 +00001237 /*
1238 * struct {
1239 * opaque verify_data[Hash.length];
1240 * } Finished;
1241 */
XiaokangQian8773aa02021-11-10 07:33:09 +00001242 MBEDTLS_SSL_CHK_BUF_PTR( buf, end, verify_data_len );
XiaokangQian74af2a82021-09-22 07:40:30 +00001243
1244 memcpy( buf, ssl->handshake->state_local.finished_out.digest,
XiaokangQian8773aa02021-11-10 07:33:09 +00001245 verify_data_len );
XiaokangQian74af2a82021-09-22 07:40:30 +00001246
Xiaofei Baid25fab62021-12-02 06:36:27 +00001247 *out_len = verify_data_len;
XiaokangQian74af2a82021-09-22 07:40:30 +00001248 return( 0 );
1249}
XiaokangQianc5c39d52021-11-09 11:55:10 +00001250
XiaokangQian35dc6252021-11-11 08:16:19 +00001251/* Main entry point: orchestrates the other functions */
1252int mbedtls_ssl_tls13_write_finished_message( mbedtls_ssl_context *ssl )
1253{
1254 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1255 unsigned char *buf;
1256 size_t buf_len, msg_len;
1257
1258 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write finished message" ) );
1259
XiaokangQiandce82242021-11-15 06:01:26 +00001260 MBEDTLS_SSL_PROC_CHK( ssl_tls13_prepare_finished_message( ssl ) );
1261
Ronald Cron8f6d39a2022-03-10 18:56:50 +01001262 MBEDTLS_SSL_PROC_CHK( mbedtls_ssl_start_handshake_msg( ssl,
XiaokangQian35dc6252021-11-11 08:16:19 +00001263 MBEDTLS_SSL_HS_FINISHED, &buf, &buf_len ) );
1264
1265 MBEDTLS_SSL_PROC_CHK( ssl_tls13_write_finished_message_body(
1266 ssl, buf, buf + buf_len, &msg_len ) );
1267
Ronald Cron8f6d39a2022-03-10 18:56:50 +01001268 mbedtls_ssl_add_hs_msg_to_checksum( ssl, MBEDTLS_SSL_HS_FINISHED,
1269 buf, msg_len );
XiaokangQian35dc6252021-11-11 08:16:19 +00001270
Ronald Cron8f6d39a2022-03-10 18:56:50 +01001271 MBEDTLS_SSL_PROC_CHK( mbedtls_ssl_finish_handshake_msg(
1272 ssl, buf_len, msg_len ) );
XiaokangQian35dc6252021-11-11 08:16:19 +00001273cleanup:
1274
1275 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write finished message" ) );
1276 return( ret );
1277}
1278
Jerry Yu378254d2021-10-30 21:44:47 +08001279void mbedtls_ssl_tls13_handshake_wrapup( mbedtls_ssl_context *ssl )
1280{
1281
1282 MBEDTLS_SSL_DEBUG_MSG( 3, ( "=> handshake wrapup" ) );
1283
Jerry Yue8c1fca2022-05-18 14:48:56 +08001284 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Switch to application keys for inbound traffic" ) );
1285 mbedtls_ssl_set_inbound_transform ( ssl, ssl->transform_application );
1286
1287 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Switch to application keys for outbound traffic" ) );
1288 mbedtls_ssl_set_outbound_transform( ssl, ssl->transform_application );
1289
Jerry Yu378254d2021-10-30 21:44:47 +08001290 /*
Jerry Yucfe64f02021-11-15 13:54:06 +08001291 * Free the previous session and switch to the current one.
Jerry Yu378254d2021-10-30 21:44:47 +08001292 */
1293 if( ssl->session )
1294 {
Jerry Yu378254d2021-10-30 21:44:47 +08001295 mbedtls_ssl_session_free( ssl->session );
1296 mbedtls_free( ssl->session );
1297 }
1298 ssl->session = ssl->session_negotiate;
1299 ssl->session_negotiate = NULL;
1300
1301 MBEDTLS_SSL_DEBUG_MSG( 3, ( "<= handshake wrapup" ) );
1302}
1303
Ronald Cron49ad6192021-11-24 16:25:31 +01001304/*
1305 *
1306 * STATE HANDLING: Write ChangeCipherSpec
1307 *
1308 */
1309#if defined(MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001310MBEDTLS_CHECK_RETURN_CRITICAL
Ronald Cron49ad6192021-11-24 16:25:31 +01001311static int ssl_tls13_write_change_cipher_spec_body( mbedtls_ssl_context *ssl,
1312 unsigned char *buf,
1313 unsigned char *end,
1314 size_t *olen )
1315{
1316 ((void) ssl);
1317
1318 MBEDTLS_SSL_CHK_BUF_PTR( buf, end, 1 );
1319 buf[0] = 1;
1320 *olen = 1;
1321
1322 return( 0 );
1323}
1324
Ronald Cron49ad6192021-11-24 16:25:31 +01001325int mbedtls_ssl_tls13_write_change_cipher_spec( mbedtls_ssl_context *ssl )
1326{
1327 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1328
1329 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write change cipher spec" ) );
1330
Ronald Cron49ad6192021-11-24 16:25:31 +01001331 /* Write CCS message */
1332 MBEDTLS_SSL_PROC_CHK( ssl_tls13_write_change_cipher_spec_body(
1333 ssl, ssl->out_msg,
1334 ssl->out_msg + MBEDTLS_SSL_OUT_CONTENT_LEN,
1335 &ssl->out_msglen ) );
1336
1337 ssl->out_msgtype = MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC;
1338
Ronald Cron49ad6192021-11-24 16:25:31 +01001339 /* Dispatch message */
Ronald Cron66dbf912022-02-02 15:33:46 +01001340 MBEDTLS_SSL_PROC_CHK( mbedtls_ssl_write_record( ssl, 0 ) );
Ronald Cron49ad6192021-11-24 16:25:31 +01001341
1342cleanup:
1343
1344 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write change cipher spec" ) );
1345 return( ret );
1346}
1347
1348#endif /* MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE */
1349
XiaokangQian78b1fa72022-01-19 06:56:30 +00001350/* Reset SSL context and update hash for handling HRR.
1351 *
1352 * Replace Transcript-Hash(X) by
1353 * Transcript-Hash( message_hash ||
1354 * 00 00 Hash.length ||
1355 * X )
1356 * A few states of the handshake are preserved, including:
1357 * - session ID
1358 * - session ticket
1359 * - negotiated ciphersuite
1360 */
1361int mbedtls_ssl_reset_transcript_for_hrr( mbedtls_ssl_context *ssl )
1362{
1363 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1364 unsigned char hash_transcript[ MBEDTLS_MD_MAX_SIZE + 4 ];
XiaokangQian0ece9982022-01-24 08:56:23 +00001365 size_t hash_len;
XiaokangQian78b1fa72022-01-19 06:56:30 +00001366 const mbedtls_ssl_ciphersuite_t *ciphersuite_info;
1367 uint16_t cipher_suite = ssl->session_negotiate->ciphersuite;
1368 ciphersuite_info = mbedtls_ssl_ciphersuite_from_id( cipher_suite );
1369
1370 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Reset SSL session for HRR" ) );
1371
XiaokangQian0ece9982022-01-24 08:56:23 +00001372 ret = mbedtls_ssl_get_handshake_transcript( ssl, ciphersuite_info->mac,
1373 hash_transcript + 4,
1374 MBEDTLS_MD_MAX_SIZE,
1375 &hash_len );
1376 if( ret != 0 )
1377 {
1378 MBEDTLS_SSL_DEBUG_RET( 4, "mbedtls_ssl_get_handshake_transcript", ret );
1379 return( ret );
1380 }
1381
1382 hash_transcript[0] = MBEDTLS_SSL_HS_MESSAGE_HASH;
1383 hash_transcript[1] = 0;
1384 hash_transcript[2] = 0;
1385 hash_transcript[3] = (unsigned char) hash_len;
1386
1387 hash_len += 4;
1388
XiaokangQian78b1fa72022-01-19 06:56:30 +00001389 if( ciphersuite_info->mac == MBEDTLS_MD_SHA256 )
1390 {
1391#if defined(MBEDTLS_SHA256_C)
XiaokangQian78b1fa72022-01-19 06:56:30 +00001392 MBEDTLS_SSL_DEBUG_BUF( 4, "Truncated SHA-256 handshake transcript",
XiaokangQian0ece9982022-01-24 08:56:23 +00001393 hash_transcript, hash_len );
XiaokangQian78b1fa72022-01-19 06:56:30 +00001394
1395#if defined(MBEDTLS_USE_PSA_CRYPTO)
1396 psa_hash_abort( &ssl->handshake->fin_sha256_psa );
1397 psa_hash_setup( &ssl->handshake->fin_sha256_psa, PSA_ALG_SHA_256 );
1398#else
1399 mbedtls_sha256_starts( &ssl->handshake->fin_sha256, 0 );
1400#endif
XiaokangQian78b1fa72022-01-19 06:56:30 +00001401#endif /* MBEDTLS_SHA256_C */
1402 }
1403 else if( ciphersuite_info->mac == MBEDTLS_MD_SHA384 )
1404 {
1405#if defined(MBEDTLS_SHA384_C)
XiaokangQian78b1fa72022-01-19 06:56:30 +00001406 MBEDTLS_SSL_DEBUG_BUF( 4, "Truncated SHA-384 handshake transcript",
XiaokangQian0ece9982022-01-24 08:56:23 +00001407 hash_transcript, hash_len );
XiaokangQian78b1fa72022-01-19 06:56:30 +00001408
1409#if defined(MBEDTLS_USE_PSA_CRYPTO)
1410 psa_hash_abort( &ssl->handshake->fin_sha384_psa );
1411 psa_hash_setup( &ssl->handshake->fin_sha384_psa, PSA_ALG_SHA_384 );
1412#else
1413 mbedtls_sha512_starts( &ssl->handshake->fin_sha512, 1 );
1414#endif
XiaokangQian78b1fa72022-01-19 06:56:30 +00001415#endif /* MBEDTLS_SHA384_C */
1416 }
1417
XiaokangQian0ece9982022-01-24 08:56:23 +00001418#if defined(MBEDTLS_SHA256_C) || defined(MBEDTLS_SHA384_C)
1419 ssl->handshake->update_checksum( ssl, hash_transcript, hash_len );
1420#endif /* MBEDTLS_SHA256_C || MBEDTLS_SHA384_C */
Przemyslaw Stekiel4b3fff42022-02-14 16:39:52 +01001421
XiaokangQian78b1fa72022-01-19 06:56:30 +00001422 return( ret );
1423}
1424
XiaokangQian9b5d04b2022-04-10 10:20:43 +00001425#if defined(MBEDTLS_ECDH_C)
XiaokangQian7807f9f2022-02-15 10:04:37 +00001426
XiaokangQian9b5d04b2022-04-10 10:20:43 +00001427int mbedtls_ssl_tls13_read_public_ecdhe_share( mbedtls_ssl_context *ssl,
1428 const unsigned char *buf,
1429 size_t buf_len )
XiaokangQian7807f9f2022-02-15 10:04:37 +00001430{
XiaokangQian9b5d04b2022-04-10 10:20:43 +00001431 uint8_t *p = (uint8_t*)buf;
XiaokangQiancfd925f2022-04-14 07:10:37 +00001432 const uint8_t *end = buf + buf_len;
XiaokangQian9b5d04b2022-04-10 10:20:43 +00001433 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
XiaokangQian7807f9f2022-02-15 10:04:37 +00001434
XiaokangQian9b5d04b2022-04-10 10:20:43 +00001435 /* Get size of the TLS opaque key_exchange field of the KeyShareEntry struct. */
Ronald Cron154d1b62022-06-01 15:33:26 +02001436 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 2 );
XiaokangQian9b5d04b2022-04-10 10:20:43 +00001437 uint16_t peerkey_len = MBEDTLS_GET_UINT16_BE( p, 0 );
1438 p += 2;
XiaokangQian3207a322022-02-23 03:15:27 +00001439
XiaokangQian9b5d04b2022-04-10 10:20:43 +00001440 /* Check if key size is consistent with given buffer length. */
Ronald Cron154d1b62022-06-01 15:33:26 +02001441 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, peerkey_len );
XiaokangQian9b5d04b2022-04-10 10:20:43 +00001442
1443 /* Store peer's ECDH public key. */
1444 memcpy( handshake->ecdh_psa_peerkey, p, peerkey_len );
1445 handshake->ecdh_psa_peerkey_len = peerkey_len;
1446
XiaokangQian3207a322022-02-23 03:15:27 +00001447 return( 0 );
1448}
Jerry Yu89e103c2022-03-30 22:43:29 +08001449
1450int mbedtls_ssl_tls13_generate_and_write_ecdh_key_exchange(
1451 mbedtls_ssl_context *ssl,
1452 uint16_t named_group,
1453 unsigned char *buf,
1454 unsigned char *end,
1455 size_t *out_len )
1456{
1457 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
1458 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
1459 psa_key_attributes_t key_attributes;
1460 size_t own_pubkey_len;
1461 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
1462 size_t ecdh_bits = 0;
1463
1464 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Perform PSA-based ECDH computation." ) );
1465
1466 /* Convert EC group to PSA key type. */
1467 if( ( handshake->ecdh_psa_type =
1468 mbedtls_psa_parse_tls_ecc_group( named_group, &ecdh_bits ) ) == 0 )
1469 return( MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
1470
1471 ssl->handshake->ecdh_bits = ecdh_bits;
1472
1473 key_attributes = psa_key_attributes_init();
1474 psa_set_key_usage_flags( &key_attributes, PSA_KEY_USAGE_DERIVE );
1475 psa_set_key_algorithm( &key_attributes, PSA_ALG_ECDH );
1476 psa_set_key_type( &key_attributes, handshake->ecdh_psa_type );
1477 psa_set_key_bits( &key_attributes, handshake->ecdh_bits );
1478
1479 /* Generate ECDH private key. */
1480 status = psa_generate_key( &key_attributes,
1481 &handshake->ecdh_psa_privkey );
1482 if( status != PSA_SUCCESS )
1483 {
1484 ret = psa_ssl_status_to_mbedtls( status );
1485 MBEDTLS_SSL_DEBUG_RET( 1, "psa_generate_key", ret );
1486 return( ret );
1487
1488 }
1489
1490 /* Export the public part of the ECDH private key from PSA. */
1491 status = psa_export_public_key( handshake->ecdh_psa_privkey,
1492 buf, (size_t)( end - buf ),
1493 &own_pubkey_len );
1494 if( status != PSA_SUCCESS )
1495 {
1496 ret = psa_ssl_status_to_mbedtls( status );
1497 MBEDTLS_SSL_DEBUG_RET( 1, "psa_export_public_key", ret );
1498 return( ret );
1499
1500 }
1501
1502 *out_len = own_pubkey_len;
1503
1504 return( 0 );
1505}
XiaokangQian9b5d04b2022-04-10 10:20:43 +00001506#endif /* MBEDTLS_ECDH_C */
XiaokangQian7807f9f2022-02-15 10:04:37 +00001507
XiaokangQian008d2bf2022-07-14 07:54:01 +00001508#if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
1509/* Check if we have any PSK to offer, returns 0 if PSK is available.
1510 * Assign the psk and ticket if pointers are present.
1511 */
1512int mbedtls_ssl_get_psk_to_offer(
1513 const mbedtls_ssl_context *ssl,
1514 int *psk_type,
1515 const unsigned char **psk, size_t *psk_len,
1516 const unsigned char **psk_identity, size_t *psk_identity_len )
1517{
1518 int ptrs_present = 0;
1519
XiaokangQian86981952022-07-19 09:51:50 +00001520 if( psk_type != NULL && psk != NULL && psk_len != NULL &&
XiaokangQian008d2bf2022-07-14 07:54:01 +00001521 psk_identity != NULL && psk_identity_len != NULL )
1522 {
1523 ptrs_present = 1;
1524 }
1525
1526 /* Check if an external PSK has been configured. */
1527 if( ssl->conf->psk != NULL )
1528 {
1529 if( ptrs_present )
1530 {
XiaokangQian86981952022-07-19 09:51:50 +00001531 *psk_type = MBEDTLS_SSL_TLS1_3_PSK_EXTERNAL;
XiaokangQian008d2bf2022-07-14 07:54:01 +00001532 *psk = ssl->conf->psk;
1533 *psk_len = ssl->conf->psk_len;
1534 *psk_identity = ssl->conf->psk_identity;
1535 *psk_identity_len = ssl->conf->psk_identity_len;
1536 }
1537
XiaokangQian008d2bf2022-07-14 07:54:01 +00001538 return( 0 );
1539 }
1540
1541 return( 1 );
1542}
1543#endif /* MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED */
1544
Jerry Yufb4b6472022-01-27 15:03:26 +08001545#endif /* MBEDTLS_SSL_TLS_C && MBEDTLS_SSL_PROTO_TLS1_3 */