blob: 2a8695a47539f364a0f85a04ea935516feb2f340 [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
22#if defined(MBEDTLS_SSL_TLS_C)
23
24#if defined(MBEDTLS_SSL_PROTO_TLS1_3_EXPERIMENTAL)
25
Jerry Yu30b071c2021-09-12 20:16:03 +080026#include <string.h>
27
Jerry Yuc8a392c2021-08-18 16:46:28 +080028#include "mbedtls/error.h"
Jerry Yu75336352021-09-01 15:59:36 +080029#include "mbedtls/debug.h"
Jerry Yu30b071c2021-09-12 20:16:03 +080030#include "mbedtls/oid.h"
31#include "mbedtls/platform.h"
Jerry Yuc8a392c2021-08-18 16:46:28 +080032
Jerry Yu65dd2cc2021-08-18 16:38:40 +080033#include "ssl_misc.h"
Jerry Yu30b071c2021-09-12 20:16:03 +080034#include "ssl_tls13_keys.h"
Jerry Yu65dd2cc2021-08-18 16:38:40 +080035
XiaokangQian6b226b02021-09-24 07:51:16 +000036int mbedtls_ssl_tls1_3_fetch_handshake_msg( mbedtls_ssl_context *ssl,
XiaokangQian16c61aa2021-09-27 09:30:17 +000037 unsigned hs_type,
38 unsigned char **buf,
39 size_t *buflen )
XiaokangQian6b226b02021-09-24 07:51:16 +000040{
41 int ret;
42
43 if( ( ret = mbedtls_ssl_read_record( ssl, 0 ) ) != 0 )
44 {
45 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
46 goto cleanup;
47 }
48
XiaokangQian16c61aa2021-09-27 09:30:17 +000049 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE ||
XiaokangQian6b226b02021-09-24 07:51:16 +000050 ssl->in_msg[0] != hs_type )
51 {
XiaokangQian16c61aa2021-09-27 09:30:17 +000052 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Receive unexpected handshake message." ) );
XiaokangQian6b226b02021-09-24 07:51:16 +000053 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE,
XiaokangQian05420b12021-09-29 08:46:37 +000054 MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
XiaokangQian6b226b02021-09-24 07:51:16 +000055 ret = MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE;
56 goto cleanup;
57 }
58
XiaokangQian05420b12021-09-29 08:46:37 +000059 /*
60 * Jump handshake header (4 bytes, see Section 4 of RFC 8446).
61 * ...
62 * HandshakeType msg_type;
63 * uint24 length;
64 * ...
65 */
XiaokangQian6b226b02021-09-24 07:51:16 +000066 *buf = ssl->in_msg + 4;
67 *buflen = ssl->in_hslen - 4;
68
XiaokangQian6b226b02021-09-24 07:51:16 +000069cleanup:
70
71 return( ret );
72}
73
Jerry Yuf4436812021-08-26 22:59:56 +080074int mbedtls_ssl_tls13_start_handshake_msg( mbedtls_ssl_context *ssl,
Jerry Yueecfbf02021-08-30 18:32:07 +080075 unsigned hs_type,
76 unsigned char **buf,
Jerry Yu0c63af62021-09-02 12:59:12 +080077 size_t *buf_len )
Jerry Yu65dd2cc2021-08-18 16:38:40 +080078{
Jerry Yu1bc2c1f2021-09-01 12:57:29 +080079 /*
80 * Reserve 4 bytes for hanshake header. ( Section 4,RFC 8446 )
81 * ...
82 * HandshakeType msg_type;
83 * uint24 length;
84 * ...
85 */
Jerry Yuc8a392c2021-08-18 16:46:28 +080086 *buf = ssl->out_msg + 4;
Jerry Yu0c63af62021-09-02 12:59:12 +080087 *buf_len = MBEDTLS_SSL_OUT_CONTENT_LEN - 4;
Jerry Yuc8a392c2021-08-18 16:46:28 +080088
89 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
90 ssl->out_msg[0] = hs_type;
91
92 return( 0 );
Jerry Yu65dd2cc2021-08-18 16:38:40 +080093}
94
Jerry Yuf4436812021-08-26 22:59:56 +080095int mbedtls_ssl_tls13_finish_handshake_msg( mbedtls_ssl_context *ssl,
Jerry Yueecfbf02021-08-30 18:32:07 +080096 size_t buf_len,
97 size_t msg_len )
Jerry Yu65dd2cc2021-08-18 16:38:40 +080098{
Jerry Yuc8a392c2021-08-18 16:46:28 +080099 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yudbfb7bd2021-09-04 09:58:58 +0800100 size_t msg_len_with_header;
Jerry Yu65dd2cc2021-08-18 16:38:40 +0800101 ((void) buf_len);
Jerry Yuc8a392c2021-08-18 16:46:28 +0800102
Jerry Yu1bc2c1f2021-09-01 12:57:29 +0800103 /* Add reserved 4 bytes for handshake header */
Jerry Yudbfb7bd2021-09-04 09:58:58 +0800104 msg_len_with_header = msg_len + 4;
105 ssl->out_msglen = msg_len_with_header;
Jerry Yu2c0fbf32021-09-02 13:53:46 +0800106 MBEDTLS_SSL_PROC_CHK( mbedtls_ssl_write_handshake_msg_ext( ssl, 0 ) );
Jerry Yuc8a392c2021-08-18 16:46:28 +0800107
108cleanup:
109 return( ret );
Jerry Yu65dd2cc2021-08-18 16:38:40 +0800110}
111
Jerry Yu48369522021-09-18 16:09:01 +0800112void mbedtls_ssl_tls1_3_add_hs_msg_to_checksum( mbedtls_ssl_context *ssl,
113 unsigned hs_type,
114 unsigned char const *msg,
115 size_t msg_len )
Jerry Yu7bea4ba2021-09-09 15:06:18 +0800116{
117 mbedtls_ssl_tls13_add_hs_hdr_to_checksum( ssl, hs_type, msg_len );
118 ssl->handshake->update_checksum( ssl, msg, msg_len );
119}
120
Jerry Yuf4436812021-08-26 22:59:56 +0800121void mbedtls_ssl_tls13_add_hs_hdr_to_checksum( mbedtls_ssl_context *ssl,
Jerry Yueecfbf02021-08-30 18:32:07 +0800122 unsigned hs_type,
123 size_t total_hs_len )
Jerry Yu65dd2cc2021-08-18 16:38:40 +0800124{
125 unsigned char hs_hdr[4];
126
127 /* Build HS header for checksum update. */
Jerry Yu2ac64192021-08-26 18:38:58 +0800128 hs_hdr[0] = MBEDTLS_BYTE_0( hs_type );
129 hs_hdr[1] = MBEDTLS_BYTE_2( total_hs_len );
130 hs_hdr[2] = MBEDTLS_BYTE_1( total_hs_len );
131 hs_hdr[3] = MBEDTLS_BYTE_0( total_hs_len );
Jerry Yu65dd2cc2021-08-18 16:38:40 +0800132
133 ssl->handshake->update_checksum( ssl, hs_hdr, sizeof( hs_hdr ) );
134}
135
Jerry Yubc20bdd2021-08-24 15:59:48 +0800136#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
137
138/*
Jerry Yue41dec02021-08-31 10:57:07 +0800139 * mbedtls_ssl_tls13_write_sig_alg_ext( )
Jerry Yubc20bdd2021-08-24 15:59:48 +0800140 *
141 * enum {
142 * ....
143 * ecdsa_secp256r1_sha256( 0x0403 ),
144 * ecdsa_secp384r1_sha384( 0x0503 ),
145 * ecdsa_secp521r1_sha512( 0x0603 ),
146 * ....
147 * } SignatureScheme;
148 *
149 * struct {
150 * SignatureScheme supported_signature_algorithms<2..2^16-2>;
151 * } SignatureSchemeList;
152 *
153 * Only if we handle at least one key exchange that needs signatures.
154 */
Jerry Yue41dec02021-08-31 10:57:07 +0800155int mbedtls_ssl_tls13_write_sig_alg_ext( mbedtls_ssl_context *ssl,
156 unsigned char *buf,
157 unsigned char *end,
158 size_t *olen )
Jerry Yubc20bdd2021-08-24 15:59:48 +0800159{
Jerry Yu72369942021-08-31 15:41:21 +0800160 unsigned char *p = buf;
Jerry Yub60e3cf2021-09-08 16:41:02 +0800161 unsigned char *supported_sig_alg_ptr; /* Start of supported_signature_algorithms */
162 size_t supported_sig_alg_len = 0; /* Length of supported_signature_algorithms */
Jerry Yu72369942021-08-31 15:41:21 +0800163
Jerry Yu75336352021-09-01 15:59:36 +0800164 *olen = 0;
Jerry Yu72369942021-08-31 15:41:21 +0800165
166 /* Skip the extension on the client if all allowed key exchanges
167 * are PSK-based. */
168#if defined(MBEDTLS_SSL_CLI_C)
169 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
170 !mbedtls_ssl_conf_tls13_some_ephemeral_enabled( ssl ) )
171 {
172 return( 0 );
173 }
174#endif /* MBEDTLS_SSL_CLI_C */
175
176 MBEDTLS_SSL_DEBUG_MSG( 3, ( "adding signature_algorithms extension" ) );
177
Jerry Yub60e3cf2021-09-08 16:41:02 +0800178 /* Check if we have space for header and length field:
179 * - extension_type (2 bytes)
180 * - extension_data_length (2 bytes)
181 * - supported_signature_algorithms_length (2 bytes)
182 */
Jerry Yu72369942021-08-31 15:41:21 +0800183 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 6 );
184 p += 6;
185
186 /*
187 * Write supported_signature_algorithms
188 */
Jerry Yub60e3cf2021-09-08 16:41:02 +0800189 supported_sig_alg_ptr = p;
Jerry Yu72369942021-08-31 15:41:21 +0800190 for( const uint16_t *sig_alg = ssl->conf->tls13_sig_algs;
191 *sig_alg != MBEDTLS_TLS13_SIG_NONE; sig_alg++ )
192 {
193 MBEDTLS_SSL_CHK_BUF_PTR( p, end, 2 );
194 MBEDTLS_PUT_UINT16_BE( *sig_alg, p, 0 );
195 p += 2;
196 MBEDTLS_SSL_DEBUG_MSG( 3, ( "signature scheme [%x]", *sig_alg ) );
197 }
198
Jerry Yub60e3cf2021-09-08 16:41:02 +0800199 /* Length of supported_signature_algorithms */
200 supported_sig_alg_len = p - supported_sig_alg_ptr;
201 if( supported_sig_alg_len == 0 )
Jerry Yu72369942021-08-31 15:41:21 +0800202 {
203 MBEDTLS_SSL_DEBUG_MSG( 1, ( "No signature algorithms defined." ) );
204 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
205 }
206
207 /* Write extension_type */
208 MBEDTLS_PUT_UINT16_BE( MBEDTLS_TLS_EXT_SIG_ALG, buf, 0 );
209 /* Write extension_data_length */
Jerry Yub60e3cf2021-09-08 16:41:02 +0800210 MBEDTLS_PUT_UINT16_BE( supported_sig_alg_len + 2, buf, 2 );
Jerry Yu72369942021-08-31 15:41:21 +0800211 /* Write length of supported_signature_algorithms */
Jerry Yub60e3cf2021-09-08 16:41:02 +0800212 MBEDTLS_PUT_UINT16_BE( supported_sig_alg_len, buf, 4 );
Jerry Yu72369942021-08-31 15:41:21 +0800213
214 /* Output the total length of signature algorithms extension. */
215 *olen = p - buf;
216
217 ssl->handshake->extensions_present |= MBEDTLS_SSL_EXT_SIG_ALG;
Jerry Yu75336352021-09-01 15:59:36 +0800218 return( 0 );
Jerry Yubc20bdd2021-08-24 15:59:48 +0800219}
220
Jerry Yu30b071c2021-09-12 20:16:03 +0800221/*
222 * The ssl_tls13_create_verify_structure() creates the verify structure.
223 * As input, it requires the transcript hash.
224 *
225 * The caller has to ensure that the buffer has size at least
226 * SSL_VERIFY_STRUCT_MAX_SIZE bytes.
227 */
228static void ssl_tls13_create_verify_structure( unsigned char *transcript_hash,
229 size_t transcript_hash_len,
230 unsigned char *verify_buffer,
231 size_t *verify_buffer_len,
232 int from )
233{
Jerry Yu26c2d112021-10-25 12:42:58 +0800234 size_t idx;
Jerry Yu30b071c2021-09-12 20:16:03 +0800235
236 /* RFC 8446, Section 4.4.3:
237 *
238 * The digital signature [in the CertificateVerify message] is then
239 * computed over the concatenation of:
240 * - A string that consists of octet 32 (0x20) repeated 64 times
241 * - The context string
242 * - A single 0 byte which serves as the separator
243 * - The content to be signed
244 */
245 uint8_t const verify_padding_val = 0x20;
246 size_t const verify_padding_len = 64;
247
Jerry Yu26c2d112021-10-25 12:42:58 +0800248 memset( verify_buffer, verify_padding_val, verify_padding_len );
249 idx = verify_padding_len;
Jerry Yu30b071c2021-09-12 20:16:03 +0800250
251 if( from == MBEDTLS_SSL_IS_CLIENT )
252 {
253 memcpy( verify_buffer + idx, MBEDTLS_SSL_TLS1_3_LBL_WITH_LEN( client_cv ) );
254 idx += MBEDTLS_SSL_TLS1_3_LBL_LEN( client_cv );
255 }
256 else
257 { /* from == MBEDTLS_SSL_IS_SERVER */
258 memcpy( verify_buffer + idx, MBEDTLS_SSL_TLS1_3_LBL_WITH_LEN( server_cv ) );
259 idx += MBEDTLS_SSL_TLS1_3_LBL_LEN( server_cv );
260 }
261
262 verify_buffer[idx++] = 0x0;
263
264 memcpy( verify_buffer + idx, transcript_hash, transcript_hash_len );
265 idx += transcript_hash_len;
266
267 *verify_buffer_len = idx;
268}
269
Jerry Yubc20bdd2021-08-24 15:59:48 +0800270#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
271
Xiaofei Bai947571e2021-09-29 09:12:03 +0000272/*
Jerry Yu30b071c2021-09-12 20:16:03 +0800273 * STATE HANDLING: Read CertificateVerify
274 */
275/* Macro to express the length of the verify structure length.
276 *
277 * The structure is computed per TLS 1.3 specification as:
278 * - 64 bytes of octet 32,
279 * - 33 bytes for the context string
280 * (which is either "TLS 1.3, client CertificateVerify"
281 * or "TLS 1.3, server CertificateVerify"),
282 * - 1 byte for the octet 0x0, which servers as a separator,
283 * - 32 or 48 bytes for the Transcript-Hash(Handshake Context, Certificate)
284 * (depending on the size of the transcript_hash)
285 *
286 * This results in a total size of
287 * - 130 bytes for a SHA256-based transcript hash, or
288 * (64 + 33 + 1 + 32 bytes)
289 * - 146 bytes for a SHA384-based transcript hash.
290 * (64 + 33 + 1 + 48 bytes)
291 *
292 */
Jerry Yu26c2d112021-10-25 12:42:58 +0800293#define SSL_VERIFY_STRUCT_MAX_SIZE ( 64 + \
294 33 + \
295 1 + \
296 MBEDTLS_TLS1_3_MD_MAX_SIZE \
Jerry Yu30b071c2021-09-12 20:16:03 +0800297 )
Jerry Yu30b071c2021-09-12 20:16:03 +0800298
Jerry Yu30b071c2021-09-12 20:16:03 +0800299
300#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Jerry Yu982d9e52021-10-14 15:59:37 +0800301static int ssl_tls13_sig_alg_is_offered( mbedtls_ssl_context *ssl, uint16_t sig_alg )
302{
303 const uint16_t *tls13_sig_alg = ssl->conf->tls13_sig_algs;
304
305 for( ; *tls13_sig_alg !=MBEDTLS_TLS13_SIG_NONE ; tls13_sig_alg++ )
306 {
307 if( *tls13_sig_alg == sig_alg )
308 return 1;
309 }
310 return 0;
311}
312
Jerry Yu30b071c2021-09-12 20:16:03 +0800313static int ssl_tls13_process_certificate_verify_parse( mbedtls_ssl_context *ssl,
314 const unsigned char *buf,
315 const unsigned char *end,
316 const unsigned char *verify_buffer,
317 size_t verify_buffer_len )
318{
319 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
320 const unsigned char *p = buf;
321 uint16_t algorithm;
Jerry Yu30b071c2021-09-12 20:16:03 +0800322 size_t signature_len;
323 mbedtls_pk_type_t sig_alg;
324 mbedtls_md_type_t md_alg;
325 unsigned char verify_hash[MBEDTLS_TLS1_3_MD_MAX_SIZE];
326 size_t verify_hash_len;
327
328 /*
329 * struct {
330 * SignatureScheme algorithm;
331 * opaque signature<0..2^16-1>;
332 * } CertificateVerify;
333 */
334 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 2 );
335 algorithm = MBEDTLS_GET_UINT16_BE( p, 0 );
336 p += 2;
337
338 /* RFC 8446 section 4.4.3
339 *
340 * If the CertificateVerify message is sent by a server, the signature algorithm
341 * MUST be one offered in the client's "signature_algorithms" extension unless
342 * no valid certificate chain can be produced without unsupported algorithms
343 *
344 * RFC 8446 section 4.4.2.2
345 *
346 * If the client cannot construct an acceptable chain using the provided
347 * certificates and decides to abort the handshake, then it MUST abort the handshake
348 * with an appropriate certificate-related alert (by default, "unsupported_certificate").
349 *
350 * Check if algorithm in offered signature algorithms. Send `unsupported_certificate`
351 * alert message on failure.
352 */
Jerry Yu982d9e52021-10-14 15:59:37 +0800353 if( ssl_tls13_sig_alg_is_offered( ssl, algorithm ) == 0 )
Jerry Yu30b071c2021-09-12 20:16:03 +0800354 {
Jerry Yu982d9e52021-10-14 15:59:37 +0800355 /* algorithm not in offered signature algorithms list */
356 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Received signature algorithm(%04x) is not "
357 "offered.",
358 ( unsigned int ) algorithm ) );
359 MBEDTLS_SSL_PEND_FATAL_ALERT(
360 MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT,
361 MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
362 return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
Jerry Yu30b071c2021-09-12 20:16:03 +0800363 }
364
365 /* We currently only support ECDSA-based signatures */
366 switch( algorithm )
367 {
368 case MBEDTLS_TLS13_SIG_ECDSA_SECP256R1_SHA256:
369 md_alg = MBEDTLS_MD_SHA256;
370 sig_alg = MBEDTLS_PK_ECDSA;
371 break;
372 case MBEDTLS_TLS13_SIG_ECDSA_SECP384R1_SHA384:
373 md_alg = MBEDTLS_MD_SHA384;
374 sig_alg = MBEDTLS_PK_ECDSA;
375 break;
376 case MBEDTLS_TLS13_SIG_ECDSA_SECP521R1_SHA512:
377 md_alg = MBEDTLS_MD_SHA512;
378 sig_alg = MBEDTLS_PK_ECDSA;
379 break;
380 default:
381 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Certificate Verify: Unknown signature algorithm." ) );
382 MBEDTLS_SSL_PEND_FATAL_ALERT(
383 MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT,
384 MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
385 return( MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
386 }
387
388 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Certificate Verify: Signature algorithm ( %04x )",
389 ( unsigned int ) algorithm ) );
390
391 /*
392 * Check the certificate's key type matches the signature alg
393 */
394 if( !mbedtls_pk_can_do( &ssl->session_negotiate->peer_cert->pk, sig_alg ) )
395 {
396 MBEDTLS_SSL_DEBUG_MSG( 1, ( "signature algorithm doesn't match cert key" ) );
397 MBEDTLS_SSL_PEND_FATAL_ALERT(
398 MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT,
399 MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
400 return( MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
401 }
402
403 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 2 );
404 signature_len = MBEDTLS_GET_UINT16_BE( p, 0 );
405 p += 2;
406 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, signature_len );
407
408 /* Hash verify buffer with indicated hash function */
409 switch( md_alg )
410 {
411#if defined(MBEDTLS_SHA256_C)
412 case MBEDTLS_MD_SHA256:
413 verify_hash_len = 32;
Jerry Yu133690c2021-10-25 14:01:13 +0800414 ret = mbedtls_sha256( verify_buffer, verify_buffer_len, verify_hash, 0 );
Jerry Yu30b071c2021-09-12 20:16:03 +0800415 break;
416#endif /* MBEDTLS_SHA256_C */
417
418#if defined(MBEDTLS_SHA384_C)
419 case MBEDTLS_MD_SHA384:
420 verify_hash_len = 48;
Jerry Yu133690c2021-10-25 14:01:13 +0800421 ret = mbedtls_sha512( verify_buffer, verify_buffer_len, verify_hash, 1 );
Jerry Yu30b071c2021-09-12 20:16:03 +0800422 break;
423#endif /* MBEDTLS_SHA384_C */
424
425#if defined(MBEDTLS_SHA512_C)
426 case MBEDTLS_MD_SHA512:
427 verify_hash_len = 64;
Jerry Yu133690c2021-10-25 14:01:13 +0800428 ret = mbedtls_sha512( verify_buffer, verify_buffer_len, verify_hash, 0 );
Jerry Yu30b071c2021-09-12 20:16:03 +0800429 break;
430#endif /* MBEDTLS_SHA512_C */
431
432 default:
433 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Certificate Verify: Unknown signature algorithm." ) );
434 MBEDTLS_SSL_PEND_FATAL_ALERT(
435 MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT,
436 MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
437 return( MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
438 }
439
Jerry Yu133690c2021-10-25 14:01:13 +0800440 if( ret != 0 )
441 {
442 MBEDTLS_SSL_DEBUG_RET( 1, "hash computation error", ret );
443 MBEDTLS_SSL_PEND_FATAL_ALERT(
444 MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT,
445 MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
446 return( MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
447 }
448
Jerry Yu30b071c2021-09-12 20:16:03 +0800449 MBEDTLS_SSL_DEBUG_BUF( 3, "verify hash", verify_hash, verify_hash_len );
450
451 if( ( ret = mbedtls_pk_verify_ext( sig_alg, NULL,
452 &ssl->session_negotiate->peer_cert->pk,
453 md_alg, verify_hash, verify_hash_len,
454 buf, signature_len ) ) != 0 )
455 {
456 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_pk_verify_ext", ret );
457
458 /* RFC 8446 section 4.4.3
459 *
460 * If the verification fails, the receiver MUST terminate the handshake
461 * with a "decrypt_error" alert.
462 */
463 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_DECRYPT_ERROR, ret );
464
465 return( ret );
466 }
467
468 return( ret );
469}
470#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
471
472int mbedtls_ssl_tls13_process_certificate_verify( mbedtls_ssl_context *ssl )
473{
Jerry Yu30b071c2021-09-12 20:16:03 +0800474
Jerry Yuda8cdf22021-10-25 15:06:49 +0800475#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
476 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
477 unsigned char verify_buffer[SSL_VERIFY_STRUCT_MAX_SIZE];
478 size_t verify_buffer_len;
479 unsigned char transcript[MBEDTLS_TLS1_3_MD_MAX_SIZE];
480 size_t transcript_len;
481 unsigned char *buf;
482 size_t buf_len;
483
484 if( mbedtls_ssl_tls1_3_some_psk_enabled( ssl ) )
485 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
486
487 if( ssl->session_negotiate->peer_cert == NULL )
488 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
489
490
Jerry Yu30b071c2021-09-12 20:16:03 +0800491 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate verify" ) );
492
Jerry Yuda8cdf22021-10-25 15:06:49 +0800493 MBEDTLS_SSL_PROC_CHK(
494 mbedtls_ssl_tls1_3_fetch_handshake_msg( ssl,
495 MBEDTLS_SSL_HS_CERTIFICATE_VERIFY, &buf, &buf_len ) );
Jerry Yu30b071c2021-09-12 20:16:03 +0800496
Jerry Yuda8cdf22021-10-25 15:06:49 +0800497 /* Need to calculate the hash of the transcript first
498 * before reading the message since otherwise it gets
499 * included in the transcript
500 */
501 ret = mbedtls_ssl_get_handshake_transcript( ssl,
502 ssl->handshake->ciphersuite_info->mac,
503 transcript, sizeof( transcript ),
504 &transcript_len );
505 if( ret != 0 )
Jerry Yu30b071c2021-09-12 20:16:03 +0800506 {
Jerry Yuda8cdf22021-10-25 15:06:49 +0800507 MBEDTLS_SSL_PEND_FATAL_ALERT(
508 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR,
509 MBEDTLS_ERR_SSL_INTERNAL_ERROR );
510 return( ret );
Jerry Yu30b071c2021-09-12 20:16:03 +0800511 }
512
Jerry Yuda8cdf22021-10-25 15:06:49 +0800513 MBEDTLS_SSL_DEBUG_BUF( 3, "handshake hash", transcript, transcript_len );
514
515 /* Create verify structure */
516 ssl_tls13_create_verify_structure( transcript,
517 transcript_len,
518 verify_buffer,
519 &verify_buffer_len,
520 !ssl->conf->endpoint );
521
522 /* Process the message contents */
523 MBEDTLS_SSL_PROC_CHK(
524 ssl_tls13_process_certificate_verify_parse( ssl,
525 buf, buf + buf_len, verify_buffer, verify_buffer_len ) );
526
527 mbedtls_ssl_tls1_3_add_hs_msg_to_checksum( ssl,
528 MBEDTLS_SSL_HS_CERTIFICATE_VERIFY, buf, buf_len );
Jerry Yu30b071c2021-09-12 20:16:03 +0800529
530cleanup:
531
532 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse certificate verify" ) );
533 return( ret );
Jerry Yuda8cdf22021-10-25 15:06:49 +0800534#else
535 ((void) ssl);
536 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
537 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
538#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
Jerry Yu30b071c2021-09-12 20:16:03 +0800539}
540
541/*
Xiaofei Bai947571e2021-09-29 09:12:03 +0000542 *
543 * STATE HANDLING: Incoming Certificate, client-side only currently.
544 *
545 */
546
547/*
Xiaofei Bai947571e2021-09-29 09:12:03 +0000548 * Implementation
549 */
550
Xiaofei Bai947571e2021-09-29 09:12:03 +0000551#if defined(MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED)
552#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
553/*
554 * Structure of Certificate message:
555 *
556 * enum {
557 * X509(0),
558 * RawPublicKey(2),
559 * (255)
560 * } CertificateType;
561 *
562 * struct {
563 * select (certificate_type) {
564 * case RawPublicKey:
565 * * From RFC 7250 ASN.1_subjectPublicKeyInfo *
566 * opaque ASN1_subjectPublicKeyInfo<1..2^24-1>;
567 * case X509:
568 * opaque cert_data<1..2^24-1>;
569 * };
570 * Extension extensions<0..2^16-1>;
571 * } CertificateEntry;
572 *
573 * struct {
574 * opaque certificate_request_context<0..2^8-1>;
575 * CertificateEntry certificate_list<0..2^24-1>;
576 * } Certificate;
577 *
578 */
Xiaofei Bai79595ac2021-10-26 07:16:45 +0000579
580/* Parse certificate chain send by the server. */
Xiaofei Bai947571e2021-09-29 09:12:03 +0000581static int ssl_tls13_parse_certificate( mbedtls_ssl_context *ssl,
582 const unsigned char *buf,
583 const unsigned char *end )
584{
585 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
586 size_t certificate_request_context_len = 0;
587 size_t certificate_list_len = 0;
588 const unsigned char *p = buf;
589 const unsigned char *certificate_list_end;
590
591 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 4 );
592 certificate_request_context_len = p[0];
Jerry Yub640bf62021-10-29 10:05:32 +0800593 certificate_list_len = MBEDTLS_GET_UINT24_BE( p, 1 );
Xiaofei Bai79595ac2021-10-26 07:16:45 +0000594 p += 4;
Xiaofei Bai947571e2021-09-29 09:12:03 +0000595
596 /* In theory, the certificate list can be up to 2^24 Bytes, but we don't
597 * support anything beyond 2^16 = 64K.
598 */
Xiaofei Bai79595ac2021-10-26 07:16:45 +0000599 if( ( certificate_request_context_len != 0 ) ||
600 ( certificate_list_len >= 0x10000 ) )
Xiaofei Bai947571e2021-09-29 09:12:03 +0000601 {
602 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
603 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR,
604 MBEDTLS_ERR_SSL_DECODE_ERROR );
605 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
606 }
607
608 /* In case we tried to reuse a session but it failed */
609 if( ssl->session_negotiate->peer_cert != NULL )
610 {
611 mbedtls_x509_crt_free( ssl->session_negotiate->peer_cert );
612 mbedtls_free( ssl->session_negotiate->peer_cert );
613 }
614
615 if( ( ssl->session_negotiate->peer_cert =
616 mbedtls_calloc( 1, sizeof( mbedtls_x509_crt ) ) ) == NULL )
617 {
618 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc( %" MBEDTLS_PRINTF_SIZET " bytes ) failed",
619 sizeof( mbedtls_x509_crt ) ) );
620 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR,
621 MBEDTLS_ERR_SSL_ALLOC_FAILED );
622 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
623 }
624
625 mbedtls_x509_crt_init( ssl->session_negotiate->peer_cert );
626
Xiaofei Bai947571e2021-09-29 09:12:03 +0000627 certificate_list_end = p + certificate_list_len;
Xiaofei Bai79595ac2021-10-26 07:16:45 +0000628 while( p < certificate_list_end )
Xiaofei Bai947571e2021-09-29 09:12:03 +0000629 {
630 size_t cert_data_len, extensions_len;
631
Xiaofei Bai79595ac2021-10-26 07:16:45 +0000632 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, certificate_list_end, 3 );
Xiaofei Baif93cbd22021-10-29 02:39:30 +0000633 cert_data_len = MBEDTLS_GET_UINT24_BE( p, 0 );
Xiaofei Bai947571e2021-09-29 09:12:03 +0000634 p += 3;
635
636 /* In theory, the CRT can be up to 2^24 Bytes, but we don't support
637 * anything beyond 2^16 = 64K. Otherwise as in the TLS 1.2 code,
638 * check that we have a minimum of 128 bytes of data, this is not
639 * clear why we need that though.
640 */
641 if( ( cert_data_len < 128 ) || ( cert_data_len >= 0x10000 ) )
Xiaofei Bai79595ac2021-10-26 07:16:45 +0000642 {
Xiaofei Bai947571e2021-09-29 09:12:03 +0000643 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad Certificate message" ) );
644 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR,
645 MBEDTLS_ERR_SSL_DECODE_ERROR );
646 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
647 }
648
Xiaofei Bai79595ac2021-10-26 07:16:45 +0000649 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, certificate_list_end, cert_data_len );
Xiaofei Bai947571e2021-09-29 09:12:03 +0000650 ret = mbedtls_x509_crt_parse_der( ssl->session_negotiate->peer_cert,
651 p, cert_data_len );
652
653 switch( ret )
654 {
655 case 0: /*ok*/
656 break;
657 case MBEDTLS_ERR_X509_UNKNOWN_SIG_ALG + MBEDTLS_ERR_OID_NOT_FOUND:
658 /* Ignore certificate with an unknown algorithm: maybe a
659 prior certificate was already trusted. */
660 break;
661
662 case MBEDTLS_ERR_X509_ALLOC_FAILED:
663 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR,
664 MBEDTLS_ERR_X509_ALLOC_FAILED );
665 MBEDTLS_SSL_DEBUG_RET( 1, " mbedtls_x509_crt_parse_der", ret );
666 return( ret );
667
668 case MBEDTLS_ERR_X509_UNKNOWN_VERSION:
669 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT,
670 MBEDTLS_ERR_X509_UNKNOWN_VERSION );
671 MBEDTLS_SSL_DEBUG_RET( 1, " mbedtls_x509_crt_parse_der", ret );
672 return( ret );
673
674 default:
675 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_BAD_CERT,
676 ret );
677 MBEDTLS_SSL_DEBUG_RET( 1, " mbedtls_x509_crt_parse_der", ret );
678 return( ret );
679 }
680
681 p += cert_data_len;
682
683 /* Certificate extensions length */
684 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, certificate_list_end, 2 );
685 extensions_len = MBEDTLS_GET_UINT16_BE( p, 0 );
686 p += 2;
Xiaofei Bai79595ac2021-10-26 07:16:45 +0000687 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, certificate_list_end, extensions_len );
Xiaofei Bai947571e2021-09-29 09:12:03 +0000688 p += extensions_len;
689 }
690
691 /* Check that all the message is consumed. */
692 if( p != end )
693 {
694 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad Certificate message" ) );
695 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR, \
696 MBEDTLS_ERR_SSL_DECODE_ERROR );
697 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
698 }
699
700 MBEDTLS_SSL_DEBUG_CRT( 3, "peer certificate", ssl->session_negotiate->peer_cert );
701
702 return( ret );
703}
704#else
705static int ssl_tls13_parse_certificate( mbedtls_ssl_context *ssl,
706 const unsigned char *buf,
707 const unsigned char *end )
708{
709 ((void) ssl);
710 ((void) buf);
711 ((void) end);
712 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
713}
714#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
715#endif /* MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED */
716
717#if defined(MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED)
718#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Xiaofei Bai79595ac2021-10-26 07:16:45 +0000719/* Validate certificate chain sent by the server. */
Xiaofei Bai947571e2021-09-29 09:12:03 +0000720static int ssl_tls13_validate_certificate( mbedtls_ssl_context *ssl )
721{
722 int ret = 0;
723 mbedtls_x509_crt *ca_chain;
724 mbedtls_x509_crl *ca_crl;
Xiaofei Baiff456022021-10-28 06:50:17 +0000725 uint32_t verify_result = 0;
Xiaofei Bai947571e2021-09-29 09:12:03 +0000726
727#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
728 if( ssl->handshake->sni_ca_chain != NULL )
729 {
730 ca_chain = ssl->handshake->sni_ca_chain;
731 ca_crl = ssl->handshake->sni_ca_crl;
732 }
733 else
734#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
735 {
736 ca_chain = ssl->conf->ca_chain;
737 ca_crl = ssl->conf->ca_crl;
738 }
739
740 /*
741 * Main check: verify certificate
742 */
743 ret = mbedtls_x509_crt_verify_with_profile(
744 ssl->session_negotiate->peer_cert,
745 ca_chain, ca_crl,
746 ssl->conf->cert_profile,
747 ssl->hostname,
Xiaofei Baiff456022021-10-28 06:50:17 +0000748 &verify_result,
Xiaofei Bai947571e2021-09-29 09:12:03 +0000749 ssl->conf->f_vrfy, ssl->conf->p_vrfy );
750
751 if( ret != 0 )
752 {
753 MBEDTLS_SSL_DEBUG_RET( 1, "x509_verify_cert", ret );
754 }
755
756 /*
757 * Secondary checks: always done, but change 'ret' only if it was 0
758 */
759
760#if defined(MBEDTLS_ECP_C)
761 {
762 const mbedtls_pk_context *pk = &ssl->session_negotiate->peer_cert->pk;
763
764 /* If certificate uses an EC key, make sure the curve is OK */
765 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_ECKEY ) &&
766 mbedtls_ssl_check_curve( ssl, mbedtls_pk_ec( *pk )->grp.id ) != 0 )
767 {
Xiaofei Baiff456022021-10-28 06:50:17 +0000768 verify_result |= MBEDTLS_X509_BADCERT_BAD_KEY;
Xiaofei Bai947571e2021-09-29 09:12:03 +0000769
770 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate ( EC key curve )" ) );
771 if( ret == 0 )
772 ret = MBEDTLS_ERR_SSL_BAD_CERTIFICATE;
773 }
774 }
775#endif /* MBEDTLS_ECP_C */
776
777 if( mbedtls_ssl_check_cert_usage( ssl->session_negotiate->peer_cert,
778 ssl->handshake->ciphersuite_info,
779 !ssl->conf->endpoint,
Xiaofei Baiff456022021-10-28 06:50:17 +0000780 &verify_result ) != 0 )
Xiaofei Bai947571e2021-09-29 09:12:03 +0000781 {
782 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate ( usage extensions )" ) );
783 if( ret == 0 )
784 ret = MBEDTLS_ERR_SSL_BAD_CERTIFICATE;
785 }
786
787
788 if( ca_chain == NULL )
789 {
790 MBEDTLS_SSL_DEBUG_MSG( 1, ( "got no CA chain" ) );
791 ret = MBEDTLS_ERR_SSL_CA_CHAIN_REQUIRED;
792 }
793
794 if( ret != 0 )
795 {
796 /* The certificate may have been rejected for several reasons.
797 Pick one and send the corresponding alert. Which alert to send
798 may be a subject of debate in some cases. */
Xiaofei Baiff456022021-10-28 06:50:17 +0000799 if( verify_result & MBEDTLS_X509_BADCERT_OTHER )
Xiaofei Bai947571e2021-09-29 09:12:03 +0000800 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED, ret );
Xiaofei Baiff456022021-10-28 06:50:17 +0000801 else if( verify_result & MBEDTLS_X509_BADCERT_CN_MISMATCH )
Xiaofei Bai947571e2021-09-29 09:12:03 +0000802 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_BAD_CERT, ret );
Xiaofei Baif93cbd22021-10-29 02:39:30 +0000803 else if( verify_result & ( MBEDTLS_X509_BADCERT_KEY_USAGE |
804 MBEDTLS_X509_BADCERT_EXT_KEY_USAGE |
805 MBEDTLS_X509_BADCERT_NS_CERT_TYPE |
806 MBEDTLS_X509_BADCERT_BAD_PK |
807 MBEDTLS_X509_BADCERT_BAD_KEY ) )
Xiaofei Bai947571e2021-09-29 09:12:03 +0000808 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT, ret );
Xiaofei Baiff456022021-10-28 06:50:17 +0000809 else if( verify_result & MBEDTLS_X509_BADCERT_EXPIRED )
Xiaofei Bai947571e2021-09-29 09:12:03 +0000810 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_CERT_EXPIRED, ret );
Xiaofei Baiff456022021-10-28 06:50:17 +0000811 else if( verify_result & MBEDTLS_X509_BADCERT_REVOKED )
Xiaofei Bai947571e2021-09-29 09:12:03 +0000812 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_CERT_REVOKED, ret );
Xiaofei Baiff456022021-10-28 06:50:17 +0000813 else if( verify_result & MBEDTLS_X509_BADCERT_NOT_TRUSTED )
Xiaofei Bai947571e2021-09-29 09:12:03 +0000814 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_UNKNOWN_CA, ret );
815 else
816 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_CERT_UNKNOWN, ret );
817 }
818
819#if defined(MBEDTLS_DEBUG_C)
Xiaofei Baiff456022021-10-28 06:50:17 +0000820 if( verify_result != 0 )
Xiaofei Bai947571e2021-09-29 09:12:03 +0000821 {
822 MBEDTLS_SSL_DEBUG_MSG( 3, ( "! Certificate verification flags %08x",
Jerry Yu83bb1312021-10-28 22:16:33 +0800823 (unsigned int) verify_result ) );
Xiaofei Bai947571e2021-09-29 09:12:03 +0000824 }
825 else
826 {
827 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Certificate verification flags clear" ) );
828 }
829#endif /* MBEDTLS_DEBUG_C */
830
Xiaofei Baiff456022021-10-28 06:50:17 +0000831 ssl->session_negotiate->verify_result = verify_result;
Xiaofei Bai947571e2021-09-29 09:12:03 +0000832 return( ret );
833}
834#else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
835static int ssl_tls13_validate_certificate( mbedtls_ssl_context *ssl )
836{
837 ((void) ssl);
838 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
839}
840#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
841#endif /* MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED */
842
Xiaofei Bai79595ac2021-10-26 07:16:45 +0000843int mbedtls_ssl_tls13_process_certificate( mbedtls_ssl_context *ssl )
Xiaofei Bai947571e2021-09-29 09:12:03 +0000844{
Xiaofei Bai79595ac2021-10-26 07:16:45 +0000845 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
846 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) );
847
848#if defined(MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED)
849 unsigned char *buf;
850 size_t buf_len;
851
852 MBEDTLS_SSL_PROC_CHK( mbedtls_ssl_tls1_3_fetch_handshake_msg(
853 ssl, MBEDTLS_SSL_HS_CERTIFICATE,
854 &buf, &buf_len ) );
855
856 /* Parse the certificate chain sent by the peer. */
857 MBEDTLS_SSL_PROC_CHK( ssl_tls13_parse_certificate( ssl, buf, buf + buf_len ) );
858 /* Validate the certificate chain and set the verification results. */
859 MBEDTLS_SSL_PROC_CHK( ssl_tls13_validate_certificate( ssl ) );
860
861 mbedtls_ssl_tls1_3_add_hs_msg_to_checksum( ssl, MBEDTLS_SSL_HS_CERTIFICATE,
862 buf, buf_len );
863
Xiaofei Bai79595ac2021-10-26 07:16:45 +0000864cleanup:
865
866 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse certificate" ) );
Xiaofei Bai10aeec02021-10-26 09:50:08 +0000867#else
868 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
869 ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
870#endif /* MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED */
Xiaofei Bai79595ac2021-10-26 07:16:45 +0000871 return( ret );
Xiaofei Bai947571e2021-09-29 09:12:03 +0000872}
873
Jerry Yu65dd2cc2021-08-18 16:38:40 +0800874#endif /* MBEDTLS_SSL_PROTO_TLS1_3_EXPERIMENTAL */
875
876#endif /* MBEDTLS_SSL_TLS_C */