blob: c83c98b18656416894babf6dcedc3057f3ae4588 [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/*
Jerry Yu30b071c2021-09-12 20:16:03 +0800222 * STATE HANDLING: Read CertificateVerify
223 */
Jerry Yud0fc5852021-10-29 11:09:06 +0800224/* Macro to express the maximum length of the verify structure.
Jerry Yu30b071c2021-09-12 20:16:03 +0800225 *
226 * The structure is computed per TLS 1.3 specification as:
227 * - 64 bytes of octet 32,
228 * - 33 bytes for the context string
229 * (which is either "TLS 1.3, client CertificateVerify"
230 * or "TLS 1.3, server CertificateVerify"),
Jerry Yud0fc5852021-10-29 11:09:06 +0800231 * - 1 byte for the octet 0x0, which serves as a separator,
Jerry Yu30b071c2021-09-12 20:16:03 +0800232 * - 32 or 48 bytes for the Transcript-Hash(Handshake Context, Certificate)
233 * (depending on the size of the transcript_hash)
234 *
235 * This results in a total size of
236 * - 130 bytes for a SHA256-based transcript hash, or
237 * (64 + 33 + 1 + 32 bytes)
238 * - 146 bytes for a SHA384-based transcript hash.
239 * (64 + 33 + 1 + 48 bytes)
240 *
241 */
Jerry Yu26c2d112021-10-25 12:42:58 +0800242#define SSL_VERIFY_STRUCT_MAX_SIZE ( 64 + \
243 33 + \
244 1 + \
245 MBEDTLS_TLS1_3_MD_MAX_SIZE \
Jerry Yu30b071c2021-09-12 20:16:03 +0800246 )
Jerry Yu30b071c2021-09-12 20:16:03 +0800247
Jerry Yu0b32c502021-10-28 13:41:59 +0800248/*
249 * The ssl_tls13_create_verify_structure() creates the verify structure.
250 * As input, it requires the transcript hash.
251 *
252 * The caller has to ensure that the buffer has size at least
253 * SSL_VERIFY_STRUCT_MAX_SIZE bytes.
254 */
Jerry Yud0fc5852021-10-29 11:09:06 +0800255static void ssl_tls13_create_verify_structure( const unsigned char *transcript_hash,
Jerry Yu0b32c502021-10-28 13:41:59 +0800256 size_t transcript_hash_len,
257 unsigned char *verify_buffer,
258 size_t *verify_buffer_len,
259 int from )
260{
261 size_t idx;
Jerry Yu30b071c2021-09-12 20:16:03 +0800262
Jerry Yu0b32c502021-10-28 13:41:59 +0800263 /* RFC 8446, Section 4.4.3:
264 *
265 * The digital signature [in the CertificateVerify message] is then
266 * computed over the concatenation of:
267 * - A string that consists of octet 32 (0x20) repeated 64 times
268 * - The context string
269 * - A single 0 byte which serves as the separator
270 * - The content to be signed
271 */
272 memset( verify_buffer, 0x20, 64 );
273 idx = 64;
274
275 if( from == MBEDTLS_SSL_IS_CLIENT )
276 {
277 memcpy( verify_buffer + idx, MBEDTLS_SSL_TLS1_3_LBL_WITH_LEN( client_cv ) );
278 idx += MBEDTLS_SSL_TLS1_3_LBL_LEN( client_cv );
279 }
280 else
281 { /* from == MBEDTLS_SSL_IS_SERVER */
282 memcpy( verify_buffer + idx, MBEDTLS_SSL_TLS1_3_LBL_WITH_LEN( server_cv ) );
283 idx += MBEDTLS_SSL_TLS1_3_LBL_LEN( server_cv );
284 }
285
286 verify_buffer[idx++] = 0x0;
287
288 memcpy( verify_buffer + idx, transcript_hash, transcript_hash_len );
289 idx += transcript_hash_len;
290
291 *verify_buffer_len = idx;
292}
293
Jerry Yud0fc5852021-10-29 11:09:06 +0800294static int ssl_tls13_sig_alg_is_offered( const mbedtls_ssl_context *ssl,
295 uint16_t sig_alg )
Jerry Yu982d9e52021-10-14 15:59:37 +0800296{
297 const uint16_t *tls13_sig_alg = ssl->conf->tls13_sig_algs;
298
Jerry Yud0fc5852021-10-29 11:09:06 +0800299 for( ; *tls13_sig_alg != MBEDTLS_TLS13_SIG_NONE ; tls13_sig_alg++ )
Jerry Yu982d9e52021-10-14 15:59:37 +0800300 {
301 if( *tls13_sig_alg == sig_alg )
Jerry Yud0fc5852021-10-29 11:09:06 +0800302 return( 1 );
Jerry Yu982d9e52021-10-14 15:59:37 +0800303 }
Jerry Yud0fc5852021-10-29 11:09:06 +0800304 return( 0 );
Jerry Yu982d9e52021-10-14 15:59:37 +0800305}
306
Jerry Yu0b32c502021-10-28 13:41:59 +0800307static int ssl_tls13_parse_certificate_verify( mbedtls_ssl_context *ssl,
Jerry Yud0fc5852021-10-29 11:09:06 +0800308 const unsigned char *buf,
309 const unsigned char *end,
310 const unsigned char *verify_buffer,
311 size_t verify_buffer_len )
Jerry Yu30b071c2021-09-12 20:16:03 +0800312{
313 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
314 const unsigned char *p = buf;
315 uint16_t algorithm;
Jerry Yu30b071c2021-09-12 20:16:03 +0800316 size_t signature_len;
317 mbedtls_pk_type_t sig_alg;
318 mbedtls_md_type_t md_alg;
Jerry Yud0fc5852021-10-29 11:09:06 +0800319 unsigned char verify_hash[MBEDTLS_MD_MAX_SIZE];
Jerry Yu30b071c2021-09-12 20:16:03 +0800320 size_t verify_hash_len;
321
322 /*
323 * struct {
324 * SignatureScheme algorithm;
325 * opaque signature<0..2^16-1>;
326 * } CertificateVerify;
327 */
328 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 2 );
329 algorithm = MBEDTLS_GET_UINT16_BE( p, 0 );
330 p += 2;
331
332 /* RFC 8446 section 4.4.3
333 *
334 * If the CertificateVerify message is sent by a server, the signature algorithm
335 * MUST be one offered in the client's "signature_algorithms" extension unless
336 * no valid certificate chain can be produced without unsupported algorithms
337 *
338 * RFC 8446 section 4.4.2.2
339 *
340 * If the client cannot construct an acceptable chain using the provided
341 * certificates and decides to abort the handshake, then it MUST abort the handshake
342 * with an appropriate certificate-related alert (by default, "unsupported_certificate").
343 *
Jerry Yud0fc5852021-10-29 11:09:06 +0800344 * Check if algorithm is an offered signature algorithm. Send `unsupported_certificate`
Jerry Yu30b071c2021-09-12 20:16:03 +0800345 * alert message on failure.
346 */
Jerry Yu0b32c502021-10-28 13:41:59 +0800347 if( ! ssl_tls13_sig_alg_is_offered( ssl, algorithm ) )
Jerry Yu30b071c2021-09-12 20:16:03 +0800348 {
Jerry Yu982d9e52021-10-14 15:59:37 +0800349 /* algorithm not in offered signature algorithms list */
350 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Received signature algorithm(%04x) is not "
351 "offered.",
352 ( unsigned int ) algorithm ) );
353 MBEDTLS_SSL_PEND_FATAL_ALERT(
354 MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT,
355 MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
Jerry Yud0fc5852021-10-29 11:09:06 +0800356 return( MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
Jerry Yu30b071c2021-09-12 20:16:03 +0800357 }
358
359 /* We currently only support ECDSA-based signatures */
360 switch( algorithm )
361 {
362 case MBEDTLS_TLS13_SIG_ECDSA_SECP256R1_SHA256:
363 md_alg = MBEDTLS_MD_SHA256;
364 sig_alg = MBEDTLS_PK_ECDSA;
365 break;
366 case MBEDTLS_TLS13_SIG_ECDSA_SECP384R1_SHA384:
367 md_alg = MBEDTLS_MD_SHA384;
368 sig_alg = MBEDTLS_PK_ECDSA;
369 break;
370 case MBEDTLS_TLS13_SIG_ECDSA_SECP521R1_SHA512:
371 md_alg = MBEDTLS_MD_SHA512;
372 sig_alg = MBEDTLS_PK_ECDSA;
373 break;
374 default:
375 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Certificate Verify: Unknown signature algorithm." ) );
376 MBEDTLS_SSL_PEND_FATAL_ALERT(
377 MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT,
378 MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
379 return( MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
380 }
381
382 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Certificate Verify: Signature algorithm ( %04x )",
383 ( unsigned int ) algorithm ) );
384
385 /*
386 * Check the certificate's key type matches the signature alg
387 */
388 if( !mbedtls_pk_can_do( &ssl->session_negotiate->peer_cert->pk, sig_alg ) )
389 {
390 MBEDTLS_SSL_DEBUG_MSG( 1, ( "signature algorithm doesn't match cert key" ) );
391 MBEDTLS_SSL_PEND_FATAL_ALERT(
392 MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT,
393 MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
394 return( MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
395 }
396
397 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 2 );
398 signature_len = MBEDTLS_GET_UINT16_BE( p, 0 );
399 p += 2;
400 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, signature_len );
401
402 /* Hash verify buffer with indicated hash function */
403 switch( md_alg )
404 {
405#if defined(MBEDTLS_SHA256_C)
406 case MBEDTLS_MD_SHA256:
407 verify_hash_len = 32;
Jerry Yu133690c2021-10-25 14:01:13 +0800408 ret = mbedtls_sha256( verify_buffer, verify_buffer_len, verify_hash, 0 );
Jerry Yu30b071c2021-09-12 20:16:03 +0800409 break;
410#endif /* MBEDTLS_SHA256_C */
411
412#if defined(MBEDTLS_SHA384_C)
413 case MBEDTLS_MD_SHA384:
414 verify_hash_len = 48;
Jerry Yu133690c2021-10-25 14:01:13 +0800415 ret = mbedtls_sha512( verify_buffer, verify_buffer_len, verify_hash, 1 );
Jerry Yu30b071c2021-09-12 20:16:03 +0800416 break;
417#endif /* MBEDTLS_SHA384_C */
418
419#if defined(MBEDTLS_SHA512_C)
420 case MBEDTLS_MD_SHA512:
421 verify_hash_len = 64;
Jerry Yu133690c2021-10-25 14:01:13 +0800422 ret = mbedtls_sha512( verify_buffer, verify_buffer_len, verify_hash, 0 );
Jerry Yu30b071c2021-09-12 20:16:03 +0800423 break;
424#endif /* MBEDTLS_SHA512_C */
425
Jerry Yu0b32c502021-10-28 13:41:59 +0800426 default:
427 ret = MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
428 break;
Jerry Yu30b071c2021-09-12 20:16:03 +0800429 }
430
Jerry Yu133690c2021-10-25 14:01:13 +0800431 if( ret != 0 )
432 {
433 MBEDTLS_SSL_DEBUG_RET( 1, "hash computation error", ret );
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 Yu30b071c2021-09-12 20:16:03 +0800440 MBEDTLS_SSL_DEBUG_BUF( 3, "verify hash", verify_hash, verify_hash_len );
441
442 if( ( ret = mbedtls_pk_verify_ext( sig_alg, NULL,
443 &ssl->session_negotiate->peer_cert->pk,
444 md_alg, verify_hash, verify_hash_len,
Jerry Yud0fc5852021-10-29 11:09:06 +0800445 p, signature_len ) ) != 0 )
Jerry Yu30b071c2021-09-12 20:16:03 +0800446 {
447 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_pk_verify_ext", ret );
448
449 /* RFC 8446 section 4.4.3
450 *
451 * If the verification fails, the receiver MUST terminate the handshake
452 * with a "decrypt_error" alert.
453 */
454 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_DECRYPT_ERROR, ret );
455
456 return( ret );
457 }
458
Jerry Yud0fc5852021-10-29 11:09:06 +0800459 return( 0 );
Jerry Yu30b071c2021-09-12 20:16:03 +0800460}
461#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
462
463int mbedtls_ssl_tls13_process_certificate_verify( mbedtls_ssl_context *ssl )
464{
Jerry Yu30b071c2021-09-12 20:16:03 +0800465
Jerry Yuda8cdf22021-10-25 15:06:49 +0800466#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
467 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
468 unsigned char verify_buffer[SSL_VERIFY_STRUCT_MAX_SIZE];
469 size_t verify_buffer_len;
470 unsigned char transcript[MBEDTLS_TLS1_3_MD_MAX_SIZE];
471 size_t transcript_len;
472 unsigned char *buf;
473 size_t buf_len;
474
Jerry Yu30b071c2021-09-12 20:16:03 +0800475 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate verify" ) );
476
Jerry Yuda8cdf22021-10-25 15:06:49 +0800477 MBEDTLS_SSL_PROC_CHK(
478 mbedtls_ssl_tls1_3_fetch_handshake_msg( ssl,
479 MBEDTLS_SSL_HS_CERTIFICATE_VERIFY, &buf, &buf_len ) );
Jerry Yu30b071c2021-09-12 20:16:03 +0800480
Jerry Yuda8cdf22021-10-25 15:06:49 +0800481 /* Need to calculate the hash of the transcript first
Jerry Yu0b32c502021-10-28 13:41:59 +0800482 * before reading the message since otherwise it gets
483 * included in the transcript
484 */
Jerry Yuda8cdf22021-10-25 15:06:49 +0800485 ret = mbedtls_ssl_get_handshake_transcript( ssl,
486 ssl->handshake->ciphersuite_info->mac,
487 transcript, sizeof( transcript ),
488 &transcript_len );
489 if( ret != 0 )
Jerry Yu30b071c2021-09-12 20:16:03 +0800490 {
Jerry Yuda8cdf22021-10-25 15:06:49 +0800491 MBEDTLS_SSL_PEND_FATAL_ALERT(
492 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR,
493 MBEDTLS_ERR_SSL_INTERNAL_ERROR );
494 return( ret );
Jerry Yu30b071c2021-09-12 20:16:03 +0800495 }
496
Jerry Yuda8cdf22021-10-25 15:06:49 +0800497 MBEDTLS_SSL_DEBUG_BUF( 3, "handshake hash", transcript, transcript_len );
498
499 /* Create verify structure */
500 ssl_tls13_create_verify_structure( transcript,
Jerry Yu0b32c502021-10-28 13:41:59 +0800501 transcript_len,
502 verify_buffer,
503 &verify_buffer_len,
504 ( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT ) ?
505 MBEDTLS_SSL_IS_SERVER :
506 MBEDTLS_SSL_IS_CLIENT );
Jerry Yuda8cdf22021-10-25 15:06:49 +0800507
508 /* Process the message contents */
Jerry Yu0b32c502021-10-28 13:41:59 +0800509 MBEDTLS_SSL_PROC_CHK( ssl_tls13_parse_certificate_verify( ssl, buf,
510 buf + buf_len, verify_buffer, verify_buffer_len ) );
Jerry Yuda8cdf22021-10-25 15:06:49 +0800511
512 mbedtls_ssl_tls1_3_add_hs_msg_to_checksum( ssl,
513 MBEDTLS_SSL_HS_CERTIFICATE_VERIFY, buf, buf_len );
Jerry Yu30b071c2021-09-12 20:16:03 +0800514
515cleanup:
516
517 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse certificate verify" ) );
518 return( ret );
Jerry Yuda8cdf22021-10-25 15:06:49 +0800519#else
520 ((void) ssl);
521 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
522 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
523#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
Jerry Yu30b071c2021-09-12 20:16:03 +0800524}
525
526/*
Xiaofei Bai947571e2021-09-29 09:12:03 +0000527 *
528 * STATE HANDLING: Incoming Certificate, client-side only currently.
529 *
530 */
531
532/*
Xiaofei Bai947571e2021-09-29 09:12:03 +0000533 * Implementation
534 */
535
Xiaofei Bai947571e2021-09-29 09:12:03 +0000536#if defined(MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED)
537#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
538/*
539 * Structure of Certificate message:
540 *
541 * enum {
542 * X509(0),
543 * RawPublicKey(2),
544 * (255)
545 * } CertificateType;
546 *
547 * struct {
548 * select (certificate_type) {
549 * case RawPublicKey:
550 * * From RFC 7250 ASN.1_subjectPublicKeyInfo *
551 * opaque ASN1_subjectPublicKeyInfo<1..2^24-1>;
552 * case X509:
553 * opaque cert_data<1..2^24-1>;
554 * };
555 * Extension extensions<0..2^16-1>;
556 * } CertificateEntry;
557 *
558 * struct {
559 * opaque certificate_request_context<0..2^8-1>;
560 * CertificateEntry certificate_list<0..2^24-1>;
561 * } Certificate;
562 *
563 */
Xiaofei Bai79595ac2021-10-26 07:16:45 +0000564
565/* Parse certificate chain send by the server. */
Xiaofei Bai947571e2021-09-29 09:12:03 +0000566static int ssl_tls13_parse_certificate( mbedtls_ssl_context *ssl,
567 const unsigned char *buf,
568 const unsigned char *end )
569{
570 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
571 size_t certificate_request_context_len = 0;
572 size_t certificate_list_len = 0;
573 const unsigned char *p = buf;
574 const unsigned char *certificate_list_end;
575
576 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 4 );
577 certificate_request_context_len = p[0];
Jerry Yub640bf62021-10-29 10:05:32 +0800578 certificate_list_len = MBEDTLS_GET_UINT24_BE( p, 1 );
Xiaofei Bai79595ac2021-10-26 07:16:45 +0000579 p += 4;
Xiaofei Bai947571e2021-09-29 09:12:03 +0000580
581 /* In theory, the certificate list can be up to 2^24 Bytes, but we don't
582 * support anything beyond 2^16 = 64K.
583 */
Xiaofei Bai79595ac2021-10-26 07:16:45 +0000584 if( ( certificate_request_context_len != 0 ) ||
585 ( certificate_list_len >= 0x10000 ) )
Xiaofei Bai947571e2021-09-29 09:12:03 +0000586 {
587 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
588 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR,
589 MBEDTLS_ERR_SSL_DECODE_ERROR );
590 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
591 }
592
593 /* In case we tried to reuse a session but it failed */
594 if( ssl->session_negotiate->peer_cert != NULL )
595 {
596 mbedtls_x509_crt_free( ssl->session_negotiate->peer_cert );
597 mbedtls_free( ssl->session_negotiate->peer_cert );
598 }
599
600 if( ( ssl->session_negotiate->peer_cert =
601 mbedtls_calloc( 1, sizeof( mbedtls_x509_crt ) ) ) == NULL )
602 {
603 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc( %" MBEDTLS_PRINTF_SIZET " bytes ) failed",
604 sizeof( mbedtls_x509_crt ) ) );
605 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR,
606 MBEDTLS_ERR_SSL_ALLOC_FAILED );
607 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
608 }
609
610 mbedtls_x509_crt_init( ssl->session_negotiate->peer_cert );
611
Xiaofei Bai947571e2021-09-29 09:12:03 +0000612 certificate_list_end = p + certificate_list_len;
Xiaofei Bai79595ac2021-10-26 07:16:45 +0000613 while( p < certificate_list_end )
Xiaofei Bai947571e2021-09-29 09:12:03 +0000614 {
615 size_t cert_data_len, extensions_len;
616
Xiaofei Bai79595ac2021-10-26 07:16:45 +0000617 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, certificate_list_end, 3 );
Xiaofei Baif93cbd22021-10-29 02:39:30 +0000618 cert_data_len = MBEDTLS_GET_UINT24_BE( p, 0 );
Xiaofei Bai947571e2021-09-29 09:12:03 +0000619 p += 3;
620
621 /* In theory, the CRT can be up to 2^24 Bytes, but we don't support
622 * anything beyond 2^16 = 64K. Otherwise as in the TLS 1.2 code,
623 * check that we have a minimum of 128 bytes of data, this is not
624 * clear why we need that though.
625 */
626 if( ( cert_data_len < 128 ) || ( cert_data_len >= 0x10000 ) )
Xiaofei Bai79595ac2021-10-26 07:16:45 +0000627 {
Xiaofei Bai947571e2021-09-29 09:12:03 +0000628 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad Certificate message" ) );
629 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR,
630 MBEDTLS_ERR_SSL_DECODE_ERROR );
631 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
632 }
633
Xiaofei Bai79595ac2021-10-26 07:16:45 +0000634 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, certificate_list_end, cert_data_len );
Xiaofei Bai947571e2021-09-29 09:12:03 +0000635 ret = mbedtls_x509_crt_parse_der( ssl->session_negotiate->peer_cert,
636 p, cert_data_len );
637
638 switch( ret )
639 {
640 case 0: /*ok*/
641 break;
642 case MBEDTLS_ERR_X509_UNKNOWN_SIG_ALG + MBEDTLS_ERR_OID_NOT_FOUND:
643 /* Ignore certificate with an unknown algorithm: maybe a
644 prior certificate was already trusted. */
645 break;
646
647 case MBEDTLS_ERR_X509_ALLOC_FAILED:
648 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR,
649 MBEDTLS_ERR_X509_ALLOC_FAILED );
650 MBEDTLS_SSL_DEBUG_RET( 1, " mbedtls_x509_crt_parse_der", ret );
651 return( ret );
652
653 case MBEDTLS_ERR_X509_UNKNOWN_VERSION:
654 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT,
655 MBEDTLS_ERR_X509_UNKNOWN_VERSION );
656 MBEDTLS_SSL_DEBUG_RET( 1, " mbedtls_x509_crt_parse_der", ret );
657 return( ret );
658
659 default:
660 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_BAD_CERT,
661 ret );
662 MBEDTLS_SSL_DEBUG_RET( 1, " mbedtls_x509_crt_parse_der", ret );
663 return( ret );
664 }
665
666 p += cert_data_len;
667
668 /* Certificate extensions length */
669 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, certificate_list_end, 2 );
670 extensions_len = MBEDTLS_GET_UINT16_BE( p, 0 );
671 p += 2;
Xiaofei Bai79595ac2021-10-26 07:16:45 +0000672 MBEDTLS_SSL_CHK_BUF_READ_PTR( p, certificate_list_end, extensions_len );
Xiaofei Bai947571e2021-09-29 09:12:03 +0000673 p += extensions_len;
674 }
675
676 /* Check that all the message is consumed. */
677 if( p != end )
678 {
679 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad Certificate message" ) );
680 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR, \
681 MBEDTLS_ERR_SSL_DECODE_ERROR );
682 return( MBEDTLS_ERR_SSL_DECODE_ERROR );
683 }
684
685 MBEDTLS_SSL_DEBUG_CRT( 3, "peer certificate", ssl->session_negotiate->peer_cert );
686
687 return( ret );
688}
689#else
690static int ssl_tls13_parse_certificate( mbedtls_ssl_context *ssl,
691 const unsigned char *buf,
692 const unsigned char *end )
693{
694 ((void) ssl);
695 ((void) buf);
696 ((void) end);
697 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
698}
699#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
700#endif /* MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED */
701
702#if defined(MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED)
703#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Xiaofei Bai79595ac2021-10-26 07:16:45 +0000704/* Validate certificate chain sent by the server. */
Xiaofei Bai947571e2021-09-29 09:12:03 +0000705static int ssl_tls13_validate_certificate( mbedtls_ssl_context *ssl )
706{
707 int ret = 0;
708 mbedtls_x509_crt *ca_chain;
709 mbedtls_x509_crl *ca_crl;
Xiaofei Baiff456022021-10-28 06:50:17 +0000710 uint32_t verify_result = 0;
Xiaofei Bai947571e2021-09-29 09:12:03 +0000711
712#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
713 if( ssl->handshake->sni_ca_chain != NULL )
714 {
715 ca_chain = ssl->handshake->sni_ca_chain;
716 ca_crl = ssl->handshake->sni_ca_crl;
717 }
718 else
719#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
720 {
721 ca_chain = ssl->conf->ca_chain;
722 ca_crl = ssl->conf->ca_crl;
723 }
724
725 /*
726 * Main check: verify certificate
727 */
728 ret = mbedtls_x509_crt_verify_with_profile(
729 ssl->session_negotiate->peer_cert,
730 ca_chain, ca_crl,
731 ssl->conf->cert_profile,
732 ssl->hostname,
Xiaofei Baiff456022021-10-28 06:50:17 +0000733 &verify_result,
Xiaofei Bai947571e2021-09-29 09:12:03 +0000734 ssl->conf->f_vrfy, ssl->conf->p_vrfy );
735
736 if( ret != 0 )
737 {
738 MBEDTLS_SSL_DEBUG_RET( 1, "x509_verify_cert", ret );
739 }
740
741 /*
742 * Secondary checks: always done, but change 'ret' only if it was 0
743 */
744
745#if defined(MBEDTLS_ECP_C)
746 {
747 const mbedtls_pk_context *pk = &ssl->session_negotiate->peer_cert->pk;
748
749 /* If certificate uses an EC key, make sure the curve is OK */
750 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_ECKEY ) &&
751 mbedtls_ssl_check_curve( ssl, mbedtls_pk_ec( *pk )->grp.id ) != 0 )
752 {
Xiaofei Baiff456022021-10-28 06:50:17 +0000753 verify_result |= MBEDTLS_X509_BADCERT_BAD_KEY;
Xiaofei Bai947571e2021-09-29 09:12:03 +0000754
755 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate ( EC key curve )" ) );
756 if( ret == 0 )
757 ret = MBEDTLS_ERR_SSL_BAD_CERTIFICATE;
758 }
759 }
760#endif /* MBEDTLS_ECP_C */
761
762 if( mbedtls_ssl_check_cert_usage( ssl->session_negotiate->peer_cert,
763 ssl->handshake->ciphersuite_info,
764 !ssl->conf->endpoint,
Xiaofei Baiff456022021-10-28 06:50:17 +0000765 &verify_result ) != 0 )
Xiaofei Bai947571e2021-09-29 09:12:03 +0000766 {
767 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate ( usage extensions )" ) );
768 if( ret == 0 )
769 ret = MBEDTLS_ERR_SSL_BAD_CERTIFICATE;
770 }
771
772
773 if( ca_chain == NULL )
774 {
775 MBEDTLS_SSL_DEBUG_MSG( 1, ( "got no CA chain" ) );
776 ret = MBEDTLS_ERR_SSL_CA_CHAIN_REQUIRED;
777 }
778
779 if( ret != 0 )
780 {
781 /* The certificate may have been rejected for several reasons.
782 Pick one and send the corresponding alert. Which alert to send
783 may be a subject of debate in some cases. */
Xiaofei Baiff456022021-10-28 06:50:17 +0000784 if( verify_result & MBEDTLS_X509_BADCERT_OTHER )
Xiaofei Bai947571e2021-09-29 09:12:03 +0000785 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED, ret );
Xiaofei Baiff456022021-10-28 06:50:17 +0000786 else if( verify_result & MBEDTLS_X509_BADCERT_CN_MISMATCH )
Xiaofei Bai947571e2021-09-29 09:12:03 +0000787 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_BAD_CERT, ret );
Xiaofei Baif93cbd22021-10-29 02:39:30 +0000788 else if( verify_result & ( MBEDTLS_X509_BADCERT_KEY_USAGE |
789 MBEDTLS_X509_BADCERT_EXT_KEY_USAGE |
790 MBEDTLS_X509_BADCERT_NS_CERT_TYPE |
791 MBEDTLS_X509_BADCERT_BAD_PK |
792 MBEDTLS_X509_BADCERT_BAD_KEY ) )
Xiaofei Bai947571e2021-09-29 09:12:03 +0000793 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT, ret );
Xiaofei Baiff456022021-10-28 06:50:17 +0000794 else if( verify_result & MBEDTLS_X509_BADCERT_EXPIRED )
Xiaofei Bai947571e2021-09-29 09:12:03 +0000795 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_CERT_EXPIRED, ret );
Xiaofei Baiff456022021-10-28 06:50:17 +0000796 else if( verify_result & MBEDTLS_X509_BADCERT_REVOKED )
Xiaofei Bai947571e2021-09-29 09:12:03 +0000797 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_CERT_REVOKED, ret );
Xiaofei Baiff456022021-10-28 06:50:17 +0000798 else if( verify_result & MBEDTLS_X509_BADCERT_NOT_TRUSTED )
Xiaofei Bai947571e2021-09-29 09:12:03 +0000799 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_UNKNOWN_CA, ret );
800 else
801 MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_CERT_UNKNOWN, ret );
802 }
803
804#if defined(MBEDTLS_DEBUG_C)
Xiaofei Baiff456022021-10-28 06:50:17 +0000805 if( verify_result != 0 )
Xiaofei Bai947571e2021-09-29 09:12:03 +0000806 {
807 MBEDTLS_SSL_DEBUG_MSG( 3, ( "! Certificate verification flags %08x",
Jerry Yu83bb1312021-10-28 22:16:33 +0800808 (unsigned int) verify_result ) );
Xiaofei Bai947571e2021-09-29 09:12:03 +0000809 }
810 else
811 {
812 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Certificate verification flags clear" ) );
813 }
814#endif /* MBEDTLS_DEBUG_C */
815
Xiaofei Baiff456022021-10-28 06:50:17 +0000816 ssl->session_negotiate->verify_result = verify_result;
Xiaofei Bai947571e2021-09-29 09:12:03 +0000817 return( ret );
818}
819#else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
820static int ssl_tls13_validate_certificate( mbedtls_ssl_context *ssl )
821{
822 ((void) ssl);
823 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
824}
825#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
826#endif /* MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED */
827
Xiaofei Bai79595ac2021-10-26 07:16:45 +0000828int mbedtls_ssl_tls13_process_certificate( mbedtls_ssl_context *ssl )
Xiaofei Bai947571e2021-09-29 09:12:03 +0000829{
Xiaofei Bai79595ac2021-10-26 07:16:45 +0000830 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
831 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) );
832
833#if defined(MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED)
834 unsigned char *buf;
835 size_t buf_len;
836
837 MBEDTLS_SSL_PROC_CHK( mbedtls_ssl_tls1_3_fetch_handshake_msg(
838 ssl, MBEDTLS_SSL_HS_CERTIFICATE,
839 &buf, &buf_len ) );
840
841 /* Parse the certificate chain sent by the peer. */
842 MBEDTLS_SSL_PROC_CHK( ssl_tls13_parse_certificate( ssl, buf, buf + buf_len ) );
843 /* Validate the certificate chain and set the verification results. */
844 MBEDTLS_SSL_PROC_CHK( ssl_tls13_validate_certificate( ssl ) );
845
846 mbedtls_ssl_tls1_3_add_hs_msg_to_checksum( ssl, MBEDTLS_SSL_HS_CERTIFICATE,
847 buf, buf_len );
848
Xiaofei Bai79595ac2021-10-26 07:16:45 +0000849cleanup:
850
851 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse certificate" ) );
Xiaofei Bai10aeec02021-10-26 09:50:08 +0000852#else
853 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
854 ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
855#endif /* MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED */
Xiaofei Bai79595ac2021-10-26 07:16:45 +0000856 return( ret );
Xiaofei Bai947571e2021-09-29 09:12:03 +0000857}
858
Jerry Yu65dd2cc2021-08-18 16:38:40 +0800859#endif /* MBEDTLS_SSL_PROTO_TLS1_3_EXPERIMENTAL */
860
861#endif /* MBEDTLS_SSL_TLS_C */