blob: 95cea46f880c922f0bd9d96c5437ed5c11e73d19 [file] [log] [blame]
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001/*
Manuel Pégourié-Gonnard1c082f32014-06-12 22:34:55 +02002 * X.509 certificate parsing and verification
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003 *
Manuel Pégourié-Gonnard6fb81872015-07-27 11:11:48 +02004 * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
Manuel Pégourié-Gonnard37ff1402015-09-04 14:21:07 +02005 * 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.
Paul Bakker7c6b2c32013-09-16 13:49:26 +020018 *
Manuel Pégourié-Gonnardfe446432015-03-06 13:17:10 +000019 * This file is part of mbed TLS (https://tls.mbed.org)
Paul Bakker7c6b2c32013-09-16 13:49:26 +020020 */
21/*
22 * The ITU-T X.509 standard defines a certificate format for PKI.
23 *
Manuel Pégourié-Gonnard1c082f32014-06-12 22:34:55 +020024 * http://www.ietf.org/rfc/rfc5280.txt (Certificates and CRLs)
25 * http://www.ietf.org/rfc/rfc3279.txt (Alg IDs for CRLs)
26 * http://www.ietf.org/rfc/rfc2986.txt (CSRs, aka PKCS#10)
Paul Bakker7c6b2c32013-09-16 13:49:26 +020027 *
28 * http://www.itu.int/ITU-T/studygroups/com17/languages/X.680-0207.pdf
29 * http://www.itu.int/ITU-T/studygroups/com17/languages/X.690-0207.pdf
Manuel Pégourié-Gonnard2f09d592017-07-03 18:30:43 +020030 *
31 * [SIRO] https://cabforum.org/wp-content/uploads/Chunghwatelecom201503cabforumV4.pdf
Paul Bakker7c6b2c32013-09-16 13:49:26 +020032 */
33
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020034#if !defined(MBEDTLS_CONFIG_FILE)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000035#include "mbedtls/config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020036#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020037#include MBEDTLS_CONFIG_FILE
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020038#endif
Paul Bakker7c6b2c32013-09-16 13:49:26 +020039
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020040#if defined(MBEDTLS_X509_CRT_PARSE_C)
Paul Bakker7c6b2c32013-09-16 13:49:26 +020041
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000042#include "mbedtls/x509_crt.h"
43#include "mbedtls/oid.h"
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050044#include "mbedtls/platform_util.h"
Rich Evans00ab4702015-02-06 13:43:58 +000045
Rich Evans00ab4702015-02-06 13:43:58 +000046#include <string.h>
47
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020048#if defined(MBEDTLS_PEM_PARSE_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000049#include "mbedtls/pem.h"
Paul Bakker7c6b2c32013-09-16 13:49:26 +020050#endif
51
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020052#if defined(MBEDTLS_PLATFORM_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000053#include "mbedtls/platform.h"
Paul Bakker7c6b2c32013-09-16 13:49:26 +020054#else
Simon Butcherd2642582018-10-03 15:11:19 +010055#include <stdio.h>
Rich Evans00ab4702015-02-06 13:43:58 +000056#include <stdlib.h>
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020057#define mbedtls_free free
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +020058#define mbedtls_calloc calloc
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020059#define mbedtls_snprintf snprintf
Paul Bakker7c6b2c32013-09-16 13:49:26 +020060#endif
61
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020062#if defined(MBEDTLS_THREADING_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000063#include "mbedtls/threading.h"
Manuel Pégourié-Gonnard5ad68e42013-11-28 17:11:54 +010064#endif
65
Paul Bakkerfa6a6202013-10-28 18:48:30 +010066#if defined(_WIN32) && !defined(EFIX64) && !defined(EFI32)
Paul Bakker7c6b2c32013-09-16 13:49:26 +020067#include <windows.h>
68#else
69#include <time.h>
70#endif
71
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020072#if defined(MBEDTLS_FS_IO)
Rich Evans00ab4702015-02-06 13:43:58 +000073#include <stdio.h>
Paul Bakker5ff3f912014-04-04 15:08:20 +020074#if !defined(_WIN32) || defined(EFIX64) || defined(EFI32)
Paul Bakker7c6b2c32013-09-16 13:49:26 +020075#include <sys/types.h>
76#include <sys/stat.h>
77#include <dirent.h>
Rich Evans00ab4702015-02-06 13:43:58 +000078#endif /* !_WIN32 || EFIX64 || EFI32 */
Paul Bakker7c6b2c32013-09-16 13:49:26 +020079#endif
80
Hanno Becker21f55672019-02-15 15:27:59 +000081static int x509_crt_parse_frame( unsigned char *start,
82 unsigned char *end,
83 mbedtls_x509_crt_frame *frame );
84static int x509_crt_subject_from_frame( mbedtls_x509_crt_frame *frame,
85 mbedtls_x509_name *subject );
86static int x509_crt_issuer_from_frame( mbedtls_x509_crt_frame *frame,
87 mbedtls_x509_name *issuer );
88static int x509_crt_subject_alt_from_frame( mbedtls_x509_crt_frame *frame,
89 mbedtls_x509_sequence *subject_alt );
90static int x509_crt_ext_key_usage_from_frame( mbedtls_x509_crt_frame *frame,
91 mbedtls_x509_sequence *ext_key_usage );
92
Hanno Becker337088a2019-02-25 14:53:14 +000093static void x509_free_sequence( mbedtls_x509_sequence *seq );
94static void x509_free_name( mbedtls_x509_name *name );
95
Hanno Becker5299cf82019-02-25 13:50:41 +000096static int x509_crt_pk_acquire( mbedtls_x509_crt *crt,
97 mbedtls_pk_context **pk );
98static int x509_crt_frame_acquire( mbedtls_x509_crt const *crt,
99 mbedtls_x509_crt_frame **frame );
100static void x509_crt_pk_release( mbedtls_x509_crt *crt,
101 mbedtls_pk_context *pk );
102static void x509_crt_frame_release( mbedtls_x509_crt *crt,
103 mbedtls_pk_context *pk );
104
Hanno Becker337088a2019-02-25 14:53:14 +0000105static int x509_crt_frame_acquire( mbedtls_x509_crt const *crt,
106 mbedtls_x509_crt_frame **frame_ptr )
107{
108 int ret;
109 mbedtls_x509_crt_frame *frame;
110
111 frame = mbedtls_calloc( 1, sizeof( mbedtls_x509_crt_frame ) );
112 if( frame == NULL )
113 return( MBEDTLS_ERR_X509_ALLOC_FAILED );
114
115 ret = x509_crt_parse_frame( crt->raw.p, crt->raw.p + crt->raw.len,
116 frame );
117 if( ret != 0 )
118 return( ret );
119
120 *frame_ptr = frame;
121 return( 0 );
122}
123
124static void x509_crt_frame_release( mbedtls_x509_crt const *crt,
125 mbedtls_x509_crt_frame *frame )
126{
127 ((void) crt);
128 if( frame == NULL )
129 return;
130 mbedtls_free( frame );
131}
132static int x509_crt_pk_acquire( mbedtls_x509_crt *crt,
133 mbedtls_pk_context **pk )
134{
135 *pk = &crt->pk;
136 return( 0 );
137}
138
139static void x509_crt_pk_release( mbedtls_x509_crt *crt,
140 mbedtls_pk_context *pk )
141{
142 ((void) crt);
143 ((void) pk);
144 return;
145}
146
Manuel Pégourié-Gonnardc547d1a2017-07-05 13:28:45 +0200147/*
148 * Item in a verification chain: cert and flags for it
149 */
150typedef struct {
151 mbedtls_x509_crt *crt;
152 uint32_t flags;
153} x509_crt_verify_chain_item;
154
155/*
156 * Max size of verification chain: end-entity + intermediates + trusted root
157 */
158#define X509_MAX_VERIFY_CHAIN_SIZE ( MBEDTLS_X509_MAX_INTERMEDIATE_CA + 2 )
Paul Bakker34617722014-06-13 17:20:13 +0200159
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200160/*
Manuel Pégourié-Gonnard88db5da2015-06-15 14:34:59 +0200161 * Default profile
162 */
Manuel Pégourié-Gonnard88db5da2015-06-15 14:34:59 +0200163const mbedtls_x509_crt_profile mbedtls_x509_crt_profile_default =
164{
Gilles Peskine5d2511c2017-05-12 13:16:40 +0200165#if defined(MBEDTLS_TLS_DEFAULT_ALLOW_SHA1_IN_CERTIFICATES)
Gilles Peskine5e79cb32017-05-04 16:17:21 +0200166 /* Allow SHA-1 (weak, but still safe in controlled environments) */
Manuel Pégourié-Gonnardf8ea8562015-06-15 15:33:19 +0200167 MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_SHA1 ) |
Gilles Peskine5e79cb32017-05-04 16:17:21 +0200168#endif
169 /* Only SHA-2 hashes */
Manuel Pégourié-Gonnardf8ea8562015-06-15 15:33:19 +0200170 MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_SHA224 ) |
171 MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_SHA256 ) |
172 MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_SHA384 ) |
173 MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_SHA512 ),
174 0xFFFFFFF, /* Any PK alg */
175 0xFFFFFFF, /* Any curve */
Manuel Pégourié-Gonnard88db5da2015-06-15 14:34:59 +0200176 2048,
177};
178
179/*
180 * Next-default profile
181 */
Manuel Pégourié-Gonnard88db5da2015-06-15 14:34:59 +0200182const mbedtls_x509_crt_profile mbedtls_x509_crt_profile_next =
183{
Manuel Pégourié-Gonnardf8ea8562015-06-15 15:33:19 +0200184 /* Hashes from SHA-256 and above */
185 MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_SHA256 ) |
186 MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_SHA384 ) |
187 MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_SHA512 ),
188 0xFFFFFFF, /* Any PK alg */
189#if defined(MBEDTLS_ECP_C)
190 /* Curves at or above 128-bit security level */
191 MBEDTLS_X509_ID_FLAG( MBEDTLS_ECP_DP_SECP256R1 ) |
192 MBEDTLS_X509_ID_FLAG( MBEDTLS_ECP_DP_SECP384R1 ) |
193 MBEDTLS_X509_ID_FLAG( MBEDTLS_ECP_DP_SECP521R1 ) |
194 MBEDTLS_X509_ID_FLAG( MBEDTLS_ECP_DP_BP256R1 ) |
195 MBEDTLS_X509_ID_FLAG( MBEDTLS_ECP_DP_BP384R1 ) |
196 MBEDTLS_X509_ID_FLAG( MBEDTLS_ECP_DP_BP512R1 ) |
197 MBEDTLS_X509_ID_FLAG( MBEDTLS_ECP_DP_SECP256K1 ),
198#else
199 0,
200#endif
Manuel Pégourié-Gonnard88db5da2015-06-15 14:34:59 +0200201 2048,
202};
203
204/*
205 * NSA Suite B Profile
206 */
Manuel Pégourié-Gonnard88db5da2015-06-15 14:34:59 +0200207const mbedtls_x509_crt_profile mbedtls_x509_crt_profile_suiteb =
208{
Manuel Pégourié-Gonnardf8ea8562015-06-15 15:33:19 +0200209 /* Only SHA-256 and 384 */
210 MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_SHA256 ) |
211 MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_SHA384 ),
212 /* Only ECDSA */
Ron Eldor85e1dcf2018-02-06 15:59:38 +0200213 MBEDTLS_X509_ID_FLAG( MBEDTLS_PK_ECDSA ) |
214 MBEDTLS_X509_ID_FLAG( MBEDTLS_PK_ECKEY ),
Manuel Pégourié-Gonnardf8ea8562015-06-15 15:33:19 +0200215#if defined(MBEDTLS_ECP_C)
216 /* Only NIST P-256 and P-384 */
217 MBEDTLS_X509_ID_FLAG( MBEDTLS_ECP_DP_SECP256R1 ) |
218 MBEDTLS_X509_ID_FLAG( MBEDTLS_ECP_DP_SECP384R1 ),
219#else
220 0,
221#endif
222 0,
Manuel Pégourié-Gonnard88db5da2015-06-15 14:34:59 +0200223};
224
225/*
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200226 * Check md_alg against profile
Manuel Pégourié-Gonnard3f816912017-10-26 10:24:16 +0200227 * Return 0 if md_alg is acceptable for this profile, -1 otherwise
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200228 */
229static int x509_profile_check_md_alg( const mbedtls_x509_crt_profile *profile,
230 mbedtls_md_type_t md_alg )
231{
Philippe Antoineb5b25432018-05-11 11:06:29 +0200232 if( md_alg == MBEDTLS_MD_NONE )
233 return( -1 );
234
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200235 if( ( profile->allowed_mds & MBEDTLS_X509_ID_FLAG( md_alg ) ) != 0 )
236 return( 0 );
237
238 return( -1 );
239}
240
241/*
242 * Check pk_alg against profile
Manuel Pégourié-Gonnard3f816912017-10-26 10:24:16 +0200243 * Return 0 if pk_alg is acceptable for this profile, -1 otherwise
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200244 */
245static int x509_profile_check_pk_alg( const mbedtls_x509_crt_profile *profile,
246 mbedtls_pk_type_t pk_alg )
247{
Philippe Antoineb5b25432018-05-11 11:06:29 +0200248 if( pk_alg == MBEDTLS_PK_NONE )
249 return( -1 );
250
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200251 if( ( profile->allowed_pks & MBEDTLS_X509_ID_FLAG( pk_alg ) ) != 0 )
252 return( 0 );
253
254 return( -1 );
255}
256
257/*
258 * Check key against profile
Manuel Pégourié-Gonnard3f816912017-10-26 10:24:16 +0200259 * Return 0 if pk is acceptable for this profile, -1 otherwise
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200260 */
261static int x509_profile_check_key( const mbedtls_x509_crt_profile *profile,
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200262 const mbedtls_pk_context *pk )
263{
Manuel Pégourié-Gonnard3f816912017-10-26 10:24:16 +0200264 const mbedtls_pk_type_t pk_alg = mbedtls_pk_get_type( pk );
Manuel Pégourié-Gonnard19773ff2017-10-24 10:51:26 +0200265
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200266#if defined(MBEDTLS_RSA_C)
267 if( pk_alg == MBEDTLS_PK_RSA || pk_alg == MBEDTLS_PK_RSASSA_PSS )
268 {
Manuel Pégourié-Gonnard097c7bb2015-06-18 16:43:38 +0200269 if( mbedtls_pk_get_bitlen( pk ) >= profile->rsa_min_bitlen )
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200270 return( 0 );
271
272 return( -1 );
273 }
274#endif
275
Manuel Pégourié-Gonnard65eefc82015-10-23 14:08:48 +0200276#if defined(MBEDTLS_ECP_C)
277 if( pk_alg == MBEDTLS_PK_ECDSA ||
278 pk_alg == MBEDTLS_PK_ECKEY ||
279 pk_alg == MBEDTLS_PK_ECKEY_DH )
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200280 {
Manuel Pégourié-Gonnard3f816912017-10-26 10:24:16 +0200281 const mbedtls_ecp_group_id gid = mbedtls_pk_ec( *pk )->grp.id;
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200282
Philippe Antoineb5b25432018-05-11 11:06:29 +0200283 if( gid == MBEDTLS_ECP_DP_NONE )
284 return( -1 );
285
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200286 if( ( profile->allowed_curves & MBEDTLS_X509_ID_FLAG( gid ) ) != 0 )
287 return( 0 );
288
289 return( -1 );
290 }
291#endif
292
293 return( -1 );
294}
295
296/*
Hanno Becker1f8527f2018-11-02 09:19:16 +0000297 * Return 0 if name matches wildcard, -1 otherwise
298 */
Hanno Becker24926222019-02-21 13:10:55 +0000299static int x509_check_wildcard( char const *cn,
300 size_t cn_len,
301 unsigned char const *buf,
302 size_t buf_len )
Hanno Becker1f8527f2018-11-02 09:19:16 +0000303{
304 size_t i;
Hanno Becker24926222019-02-21 13:10:55 +0000305 size_t cn_idx = 0;
Hanno Becker1f8527f2018-11-02 09:19:16 +0000306
307 /* We can't have a match if there is no wildcard to match */
Hanno Becker24926222019-02-21 13:10:55 +0000308 if( buf_len < 3 || buf[0] != '*' || buf[1] != '.' )
Hanno Becker1f8527f2018-11-02 09:19:16 +0000309 return( -1 );
310
311 for( i = 0; i < cn_len; ++i )
312 {
313 if( cn[i] == '.' )
314 {
315 cn_idx = i;
316 break;
317 }
318 }
319
320 if( cn_idx == 0 )
321 return( -1 );
322
Hanno Beckerb3def1d2019-02-22 11:46:06 +0000323 if( mbedtls_x509_memcasecmp( buf + 1, cn + cn_idx,
324 buf_len - 1, cn_len - cn_idx ) == 0 )
Hanno Becker1f8527f2018-11-02 09:19:16 +0000325 {
326 return( 0 );
327 }
328
329 return( -1 );
330}
331
332/*
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +0200333 * Reset (init or clear) a verify_chain
334 */
335static void x509_crt_verify_chain_reset(
336 mbedtls_x509_crt_verify_chain *ver_chain )
337{
338 size_t i;
339
340 for( i = 0; i < MBEDTLS_X509_MAX_VERIFY_CHAIN_SIZE; i++ )
341 {
342 ver_chain->items[i].crt = NULL;
Hanno Beckerd6ddcd62019-01-10 09:19:26 +0000343 ver_chain->items[i].flags = (uint32_t) -1;
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +0200344 }
345
346 ver_chain->len = 0;
347}
348
349/*
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200350 * Version ::= INTEGER { v1(0), v2(1), v3(2) }
351 */
352static int x509_get_version( unsigned char **p,
353 const unsigned char *end,
354 int *ver )
355{
356 int ret;
357 size_t len;
358
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200359 if( ( ret = mbedtls_asn1_get_tag( p, end, &len,
360 MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_CONSTRUCTED | 0 ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200361 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200362 if( ret == MBEDTLS_ERR_ASN1_UNEXPECTED_TAG )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200363 {
364 *ver = 0;
365 return( 0 );
366 }
367
Hanno Becker2f472142019-02-12 11:52:10 +0000368 return( MBEDTLS_ERR_X509_INVALID_FORMAT + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200369 }
370
371 end = *p + len;
372
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200373 if( ( ret = mbedtls_asn1_get_int( p, end, ver ) ) != 0 )
374 return( MBEDTLS_ERR_X509_INVALID_VERSION + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200375
376 if( *p != end )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200377 return( MBEDTLS_ERR_X509_INVALID_VERSION +
378 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200379
380 return( 0 );
381}
382
383/*
384 * Validity ::= SEQUENCE {
385 * notBefore Time,
386 * notAfter Time }
387 */
388static int x509_get_dates( unsigned char **p,
389 const unsigned char *end,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200390 mbedtls_x509_time *from,
391 mbedtls_x509_time *to )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200392{
393 int ret;
394 size_t len;
395
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200396 if( ( ret = mbedtls_asn1_get_tag( p, end, &len,
397 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
398 return( MBEDTLS_ERR_X509_INVALID_DATE + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200399
400 end = *p + len;
401
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200402 if( ( ret = mbedtls_x509_get_time( p, end, from ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200403 return( ret );
404
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200405 if( ( ret = mbedtls_x509_get_time( p, end, to ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200406 return( ret );
407
408 if( *p != end )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200409 return( MBEDTLS_ERR_X509_INVALID_DATE +
410 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200411
412 return( 0 );
413}
414
415/*
416 * X.509 v2/v3 unique identifier (not parsed)
417 */
418static int x509_get_uid( unsigned char **p,
419 const unsigned char *end,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200420 mbedtls_x509_buf *uid, int n )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200421{
422 int ret;
423
424 if( *p == end )
425 return( 0 );
426
427 uid->tag = **p;
428
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200429 if( ( ret = mbedtls_asn1_get_tag( p, end, &uid->len,
430 MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_CONSTRUCTED | n ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200431 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200432 if( ret == MBEDTLS_ERR_ASN1_UNEXPECTED_TAG )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200433 return( 0 );
434
Hanno Becker2f472142019-02-12 11:52:10 +0000435 return( MBEDTLS_ERR_X509_INVALID_FORMAT + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200436 }
437
438 uid->p = *p;
439 *p += uid->len;
440
441 return( 0 );
442}
443
444static int x509_get_basic_constraints( unsigned char **p,
445 const unsigned char *end,
446 int *ca_istrue,
447 int *max_pathlen )
448{
449 int ret;
450 size_t len;
451
452 /*
453 * BasicConstraints ::= SEQUENCE {
454 * cA BOOLEAN DEFAULT FALSE,
455 * pathLenConstraint INTEGER (0..MAX) OPTIONAL }
456 */
457 *ca_istrue = 0; /* DEFAULT FALSE */
458 *max_pathlen = 0; /* endless */
459
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200460 if( ( ret = mbedtls_asn1_get_tag( p, end, &len,
461 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
Hanno Beckerf1b39bf2019-02-22 11:09:48 +0000462 return( ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200463
464 if( *p == end )
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200465 return( 0 );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200466
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200467 if( ( ret = mbedtls_asn1_get_bool( p, end, ca_istrue ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200468 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200469 if( ret == MBEDTLS_ERR_ASN1_UNEXPECTED_TAG )
470 ret = mbedtls_asn1_get_int( p, end, ca_istrue );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200471
472 if( ret != 0 )
Hanno Beckerf1b39bf2019-02-22 11:09:48 +0000473 return( ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200474
475 if( *ca_istrue != 0 )
476 *ca_istrue = 1;
477 }
478
479 if( *p == end )
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200480 return( 0 );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200481
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200482 if( ( ret = mbedtls_asn1_get_int( p, end, max_pathlen ) ) != 0 )
Hanno Beckerf1b39bf2019-02-22 11:09:48 +0000483 return( ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200484
485 if( *p != end )
Hanno Beckerf1b39bf2019-02-22 11:09:48 +0000486 return( MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200487
488 (*max_pathlen)++;
489
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200490 return( 0 );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200491}
492
493static int x509_get_ns_cert_type( unsigned char **p,
494 const unsigned char *end,
495 unsigned char *ns_cert_type)
496{
497 int ret;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200498 mbedtls_x509_bitstring bs = { 0, 0, NULL };
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200499
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200500 if( ( ret = mbedtls_asn1_get_bitstring( p, end, &bs ) ) != 0 )
Hanno Beckerf1b39bf2019-02-22 11:09:48 +0000501 return( ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200502
503 if( bs.len != 1 )
Hanno Beckerf1b39bf2019-02-22 11:09:48 +0000504 return( MBEDTLS_ERR_ASN1_INVALID_LENGTH );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200505
506 /* Get actual bitstring */
507 *ns_cert_type = *bs.p;
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200508 return( 0 );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200509}
510
511static int x509_get_key_usage( unsigned char **p,
512 const unsigned char *end,
Manuel Pégourié-Gonnard1d0ca1a2015-03-27 16:50:00 +0100513 unsigned int *key_usage)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200514{
515 int ret;
Manuel Pégourié-Gonnard9a702252015-06-23 10:14:36 +0200516 size_t i;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200517 mbedtls_x509_bitstring bs = { 0, 0, NULL };
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200518
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200519 if( ( ret = mbedtls_asn1_get_bitstring( p, end, &bs ) ) != 0 )
Hanno Beckerf1b39bf2019-02-22 11:09:48 +0000520 return( ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200521
522 if( bs.len < 1 )
Hanno Beckerf1b39bf2019-02-22 11:09:48 +0000523 return( MBEDTLS_ERR_ASN1_INVALID_LENGTH );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200524
525 /* Get actual bitstring */
Manuel Pégourié-Gonnard9a702252015-06-23 10:14:36 +0200526 *key_usage = 0;
527 for( i = 0; i < bs.len && i < sizeof( unsigned int ); i++ )
528 {
529 *key_usage |= (unsigned int) bs.p[i] << (8*i);
530 }
531
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200532 return( 0 );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200533}
534
535/*
536 * ExtKeyUsageSyntax ::= SEQUENCE SIZE (1..MAX) OF KeyPurposeId
537 *
538 * KeyPurposeId ::= OBJECT IDENTIFIER
539 */
540static int x509_get_ext_key_usage( unsigned char **p,
541 const unsigned char *end,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200542 mbedtls_x509_sequence *ext_key_usage)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200543{
Hanno Beckerf1b39bf2019-02-22 11:09:48 +0000544 return( mbedtls_asn1_get_sequence_of( p, end, ext_key_usage,
545 MBEDTLS_ASN1_OID ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200546}
547
548/*
549 * SubjectAltName ::= GeneralNames
550 *
551 * GeneralNames ::= SEQUENCE SIZE (1..MAX) OF GeneralName
552 *
553 * GeneralName ::= CHOICE {
554 * otherName [0] OtherName,
555 * rfc822Name [1] IA5String,
556 * dNSName [2] IA5String,
557 * x400Address [3] ORAddress,
558 * directoryName [4] Name,
559 * ediPartyName [5] EDIPartyName,
560 * uniformResourceIdentifier [6] IA5String,
561 * iPAddress [7] OCTET STRING,
562 * registeredID [8] OBJECT IDENTIFIER }
563 *
564 * OtherName ::= SEQUENCE {
565 * type-id OBJECT IDENTIFIER,
566 * value [0] EXPLICIT ANY DEFINED BY type-id }
567 *
568 * EDIPartyName ::= SEQUENCE {
569 * nameAssigner [0] DirectoryString OPTIONAL,
570 * partyName [1] DirectoryString }
571 *
Manuel Pégourié-Gonnardb4fe3cb2015-01-22 16:11:05 +0000572 * NOTE: we only parse and use dNSName at this point.
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200573 */
Hanno Beckerad462192019-02-21 13:32:31 +0000574static int x509_get_subject_alt_name_cb( void *ctx,
575 int tag,
576 unsigned char *data,
577 size_t data_len )
578{
579 mbedtls_asn1_sequence **cur_ptr = (mbedtls_asn1_sequence **) ctx;
580 mbedtls_asn1_sequence *cur = *cur_ptr;
581
Hanno Beckerad462192019-02-21 13:32:31 +0000582 /* Allocate and assign next pointer */
583 if( cur->buf.p != NULL )
584 {
585 cur->next = mbedtls_calloc( 1, sizeof( mbedtls_asn1_sequence ) );
586 if( cur->next == NULL )
Hanno Becker90b94082019-02-21 21:13:21 +0000587 return( MBEDTLS_ERR_ASN1_ALLOC_FAILED );
Hanno Beckerad462192019-02-21 13:32:31 +0000588 cur = cur->next;
589 }
590
591 cur->buf.tag = tag;
592 cur->buf.p = data;
593 cur->buf.len = data_len;
594
595 *cur_ptr = cur;
596 return( 0 );
597}
598
Hanno Becker5984d302019-02-21 14:46:54 +0000599static int x509_get_subject_alt_name( unsigned char *p,
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200600 const unsigned char *end,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200601 mbedtls_x509_sequence *subject_alt_name )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200602{
Hanno Beckerf1b39bf2019-02-22 11:09:48 +0000603 return( mbedtls_asn1_traverse_sequence_of( &p, end,
604 MBEDTLS_ASN1_TAG_CLASS_MASK,
605 MBEDTLS_ASN1_CONTEXT_SPECIFIC,
606 MBEDTLS_ASN1_TAG_VALUE_MASK,
607 2 /* SubjectAlt DNS */,
608 x509_get_subject_alt_name_cb,
609 (void*) &subject_alt_name ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200610}
611
612/*
613 * X.509 v3 extensions
614 *
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200615 */
Hanno Beckerf1b39bf2019-02-22 11:09:48 +0000616static int x509_crt_get_ext_cb( void *ctx,
617 int tag,
618 unsigned char *p,
619 size_t ext_len )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200620{
621 int ret;
Hanno Becker21f55672019-02-15 15:27:59 +0000622 mbedtls_x509_crt_frame *frame = (mbedtls_x509_crt_frame *) ctx;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200623 size_t len;
Hanno Beckerf1b39bf2019-02-22 11:09:48 +0000624 unsigned char *end, *end_ext_octet;
625 mbedtls_x509_buf extn_oid = { 0, 0, NULL };
626 int is_critical = 0; /* DEFAULT FALSE */
627 int ext_type = 0;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200628
Hanno Beckerf1b39bf2019-02-22 11:09:48 +0000629 ((void) tag);
Hanno Becker4e1bfc12019-02-12 17:22:36 +0000630
Hanno Beckerf1b39bf2019-02-22 11:09:48 +0000631 /*
632 * Extension ::= SEQUENCE {
633 * extnID OBJECT IDENTIFIER,
634 * critical BOOLEAN DEFAULT FALSE,
635 * extnValue OCTET STRING }
636 */
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200637
Hanno Beckerf1b39bf2019-02-22 11:09:48 +0000638 end = p + ext_len;
639
640 /* Get extension ID */
641 if( ( ret = mbedtls_asn1_get_tag( &p, end, &extn_oid.len,
642 MBEDTLS_ASN1_OID ) ) != 0 )
643 goto err;
644
645 extn_oid.tag = MBEDTLS_ASN1_OID;
646 extn_oid.p = p;
647 p += extn_oid.len;
648
649 /* Get optional critical */
650 if( ( ret = mbedtls_asn1_get_bool( &p, end, &is_critical ) ) != 0 &&
651 ( ret != MBEDTLS_ERR_ASN1_UNEXPECTED_TAG ) )
652 goto err;
653
654 /* Data should be octet string type */
655 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
656 MBEDTLS_ASN1_OCTET_STRING ) ) != 0 )
657 goto err;
658
659 end_ext_octet = p + len;
660 if( end_ext_octet != end )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200661 {
Hanno Beckerf1b39bf2019-02-22 11:09:48 +0000662 ret = MBEDTLS_ERR_ASN1_LENGTH_MISMATCH;
663 goto err;
664 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200665
Hanno Beckerf1b39bf2019-02-22 11:09:48 +0000666 /*
667 * Detect supported extensions
668 */
669 ret = mbedtls_oid_get_x509_ext_type( &extn_oid, &ext_type );
670 if( ret != 0 )
671 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200672#if !defined(MBEDTLS_X509_ALLOW_UNSUPPORTED_CRITICAL_EXTENSION)
Hanno Beckerf1b39bf2019-02-22 11:09:48 +0000673 if( is_critical )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200674 {
Hanno Beckerf1b39bf2019-02-22 11:09:48 +0000675 /* Data is marked as critical: fail */
676 ret = MBEDTLS_ERR_ASN1_UNEXPECTED_TAG;
677 goto err;
678 }
679#endif
680 return( 0 );
681 }
682
683 /* Forbid repeated extensions */
Hanno Becker21f55672019-02-15 15:27:59 +0000684 if( ( frame->ext_types & ext_type ) != 0 )
Hanno Beckerf1b39bf2019-02-22 11:09:48 +0000685 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS );
686
Hanno Becker21f55672019-02-15 15:27:59 +0000687 frame->ext_types |= ext_type;
Hanno Beckerf1b39bf2019-02-22 11:09:48 +0000688 switch( ext_type )
689 {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +0100690 case MBEDTLS_X509_EXT_BASIC_CONSTRAINTS:
Hanno Beckerf1b39bf2019-02-22 11:09:48 +0000691 {
692 int ca_istrue;
693 int max_pathlen;
694
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200695 /* Parse basic constraints */
Hanno Beckerf1b39bf2019-02-22 11:09:48 +0000696 ret = x509_get_basic_constraints( &p, end_ext_octet,
697 &ca_istrue,
698 &max_pathlen );
699 if( ret != 0 )
700 goto err;
701
Hanno Becker21f55672019-02-15 15:27:59 +0000702 frame->ca_istrue = ca_istrue;
703 frame->max_pathlen = max_pathlen;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200704 break;
Hanno Beckerf1b39bf2019-02-22 11:09:48 +0000705 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200706
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200707 case MBEDTLS_X509_EXT_KEY_USAGE:
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200708 /* Parse key usage */
Hanno Beckerf1b39bf2019-02-22 11:09:48 +0000709 ret = x509_get_key_usage( &p, end_ext_octet,
Hanno Becker21f55672019-02-15 15:27:59 +0000710 &frame->key_usage );
Hanno Beckerf1b39bf2019-02-22 11:09:48 +0000711 if( ret != 0 )
712 goto err;
713 break;
714
715 case MBEDTLS_X509_EXT_SUBJECT_ALT_NAME:
716 /* Copy reference to raw subject alt name data. */
Hanno Becker21f55672019-02-15 15:27:59 +0000717 frame->subject_alt_raw.p = p;
718 frame->subject_alt_raw.len = end_ext_octet - p;
719
720 ret = mbedtls_asn1_traverse_sequence_of( &p, end_ext_octet,
721 MBEDTLS_ASN1_TAG_CLASS_MASK,
722 MBEDTLS_ASN1_CONTEXT_SPECIFIC,
723 MBEDTLS_ASN1_TAG_VALUE_MASK,
724 2 /* SubjectAlt DNS */,
725 NULL, NULL );
726 if( ret != 0 )
727 goto err;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200728 break;
729
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200730 case MBEDTLS_X509_EXT_EXTENDED_KEY_USAGE:
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200731 /* Parse extended key usage */
Hanno Becker21f55672019-02-15 15:27:59 +0000732 frame->ext_key_usage_raw.p = p;
733 frame->ext_key_usage_raw.len = end_ext_octet - p;
734 if( frame->ext_key_usage_raw.len == 0 )
Hanno Becker5984d302019-02-21 14:46:54 +0000735 {
Hanno Becker21f55672019-02-15 15:27:59 +0000736 ret = MBEDTLS_ERR_ASN1_INVALID_LENGTH;
737 goto err;
Hanno Becker5984d302019-02-21 14:46:54 +0000738 }
Hanno Becker21f55672019-02-15 15:27:59 +0000739
740 /* Check structural sanity of extension. */
741 ret = mbedtls_asn1_traverse_sequence_of( &p, end_ext_octet,
742 0xFF, MBEDTLS_ASN1_OID,
743 0, 0, NULL, NULL );
744 if( ret != 0 )
745 goto err;
746
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200747 break;
748
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200749 case MBEDTLS_X509_EXT_NS_CERT_TYPE:
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200750 /* Parse netscape certificate type */
Hanno Beckerf1b39bf2019-02-22 11:09:48 +0000751 ret = x509_get_ns_cert_type( &p, end_ext_octet,
Hanno Becker21f55672019-02-15 15:27:59 +0000752 &frame->ns_cert_type );
Hanno Beckerf1b39bf2019-02-22 11:09:48 +0000753 if( ret != 0 )
754 goto err;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200755 break;
756
757 default:
Hanno Beckerf1b39bf2019-02-22 11:09:48 +0000758 /*
759 * If this is a non-critical extension, which the oid layer
760 * supports, but there isn't an X.509 parser for it,
761 * skip the extension.
762 */
763#if !defined(MBEDTLS_X509_ALLOW_UNSUPPORTED_CRITICAL_EXTENSION)
764 if( is_critical )
765 return( MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE );
766#endif
767 p = end_ext_octet;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200768 }
769
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200770 return( 0 );
Hanno Beckerf1b39bf2019-02-22 11:09:48 +0000771
772err:
773 return( ret );
774}
775
Hanno Becker21f55672019-02-15 15:27:59 +0000776static int x509_crt_frame_parse_ext( mbedtls_x509_crt_frame *frame )
Hanno Beckerf1b39bf2019-02-22 11:09:48 +0000777{
778 int ret;
Hanno Becker21f55672019-02-15 15:27:59 +0000779 unsigned char *p = frame->v3_ext.p;
780 unsigned char *end = p + frame->v3_ext.len;
Hanno Beckerf1b39bf2019-02-22 11:09:48 +0000781
Hanno Becker21f55672019-02-15 15:27:59 +0000782 if( p == end )
Hanno Beckerf1b39bf2019-02-22 11:09:48 +0000783 return( 0 );
784
Hanno Becker21f55672019-02-15 15:27:59 +0000785 ret = mbedtls_asn1_traverse_sequence_of( &p, end,
Hanno Beckerf1b39bf2019-02-22 11:09:48 +0000786 0xFF, MBEDTLS_ASN1_SEQUENCE | MBEDTLS_ASN1_CONSTRUCTED,
Hanno Becker21f55672019-02-15 15:27:59 +0000787 0, 0, x509_crt_get_ext_cb, frame );
Hanno Beckerf1b39bf2019-02-22 11:09:48 +0000788
789 if( ret == MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE )
790 return( ret );
791 if( ret == MBEDTLS_ERR_X509_INVALID_EXTENSIONS )
792 return( ret );
793
794 if( ret != 0 )
795 ret += MBEDTLS_ERR_X509_INVALID_EXTENSIONS;
796
797 return( ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200798}
799
Hanno Becker21f55672019-02-15 15:27:59 +0000800static int x509_crt_parse_frame( unsigned char *start,
801 unsigned char *end,
802 mbedtls_x509_crt_frame *frame )
803{
804 int ret;
805 unsigned char *p;
806 size_t len;
807
808 mbedtls_x509_buf tmp;
809 unsigned char *tbs_start;
810
811 mbedtls_x509_buf outer_sig_alg;
812 size_t inner_sig_alg_len;
813 unsigned char *inner_sig_alg_start;
814
815 memset( frame, 0, sizeof( *frame ) );
816
817 /*
818 * Certificate ::= SEQUENCE {
819 * tbsCertificate TBSCertificate,
820 * signatureAlgorithm AlgorithmIdentifier,
821 * signatureValue BIT STRING
822 * }
823 *
824 */
825 p = start;
826
827 frame->raw.p = p;
828 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
829 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
830 {
831 return( MBEDTLS_ERR_X509_INVALID_FORMAT );
832 }
833
834 /* NOTE: We are currently not checking that the `Certificate`
835 * structure spans the entire buffer. */
836 end = p + len;
837 frame->raw.len = end - frame->raw.p;
838
839 /*
840 * TBSCertificate ::= SEQUENCE { ...
841 */
842 frame->tbs.p = p;
843 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
844 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
845 {
846 return( ret + MBEDTLS_ERR_X509_INVALID_FORMAT );
847 }
848 tbs_start = p;
849
850 /* Breadth-first parsing: Jump over TBS for now. */
851 p += len;
852 frame->tbs.len = p - frame->tbs.p;
853
854 /*
855 * AlgorithmIdentifier ::= SEQUENCE { ...
856 */
857 outer_sig_alg.p = p;
858 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
859 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
860 {
861 return( MBEDTLS_ERR_X509_INVALID_ALG + ret );
862 }
863 p += len;
864 outer_sig_alg.len = p - outer_sig_alg.p;
865
866 /*
867 * signatureValue BIT STRING
868 */
869 ret = mbedtls_x509_get_sig( &p, end, &tmp );
870 if( ret != 0 )
871 return( ret );
872 frame->sig.p = tmp.p;
873 frame->sig.len = tmp.len;
874
875 /* Check that we consumed the entire `Certificate` structure. */
876 if( p != end )
877 {
878 return( MBEDTLS_ERR_X509_INVALID_FORMAT +
879 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
880 }
881
882 /* Parse TBSCertificate structure
883 *
884 * TBSCertificate ::= SEQUENCE {
885 * version [0] EXPLICIT Version DEFAULT v1,
886 * serialNumber CertificateSerialNumber,
887 * signature AlgorithmIdentifier,
888 * issuer Name,
889 * validity Validity,
890 * subject Name,
891 * subjectPublicKeyInfo SubjectPublicKeyInfo,
892 * issuerUniqueID [1] IMPLICIT UniqueIdentifier OPTIONAL,
893 * -- If present, version MUST be v2 or v3
894 * subjectUniqueID [2] IMPLICIT UniqueIdentifier OPTIONAL,
895 * -- If present, version MUST be v2 or v3
896 * extensions [3] EXPLICIT Extensions OPTIONAL
897 * -- If present, version MUST be v3
898 * }
899 */
900 end = frame->tbs.p + frame->tbs.len;
901 p = tbs_start;
902
903 /*
904 * Version ::= INTEGER { v1(0), v2(1), v3(2) }
905 */
906 {
907 int version;
908 ret = x509_get_version( &p, end, &version );
909 if( ret != 0 )
910 return( ret );
911
912 if( version < 0 || version > 2 )
913 return( MBEDTLS_ERR_X509_UNKNOWN_VERSION );
914
915 frame->version = version + 1;
916 }
917
918 /*
919 * CertificateSerialNumber ::= INTEGER
920 */
921 ret = mbedtls_x509_get_serial( &p, end, &tmp );
922 if( ret != 0 )
923 return( ret );
924
925 frame->serial.p = tmp.p;
926 frame->serial.len = tmp.len;
927
928 /*
929 * signature AlgorithmIdentifier
930 */
931 inner_sig_alg_start = p;
932 ret = mbedtls_x509_get_sig_alg_raw( &p, end, &frame->sig_md,
933 &frame->sig_pk, NULL );
934 if( ret != 0 )
935 return( ret );
936 inner_sig_alg_len = p - inner_sig_alg_start;
937
938 frame->sig_alg.p = inner_sig_alg_start;
939 frame->sig_alg.len = inner_sig_alg_len;
940
941 /* Consistency check:
942 * Inner and outer AlgorithmIdentifier structures must coincide:
943 *
944 * Quoting RFC 5280, Section 4.1.1.2:
945 * This field MUST contain the same algorithm identifier as the
946 * signature field in the sequence tbsCertificate (Section 4.1.2.3).
947 */
948 if( outer_sig_alg.len != inner_sig_alg_len ||
949 memcmp( outer_sig_alg.p, inner_sig_alg_start, inner_sig_alg_len ) != 0 )
950 {
951 return( MBEDTLS_ERR_X509_SIG_MISMATCH );
952 }
953
954 /*
955 * issuer Name
956 *
957 * Name ::= CHOICE { -- only one possibility for now --
958 * rdnSequence RDNSequence }
959 *
960 * RDNSequence ::= SEQUENCE OF RelativeDistinguishedName
961 */
962 frame->issuer_raw_with_hdr.p = p;
963
964 ret = mbedtls_asn1_get_tag( &p, end, &len,
965 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE );
966 if( ret != 0 )
967 return( ret + MBEDTLS_ERR_X509_INVALID_FORMAT );
968 frame->issuer_raw.p = p;
969 frame->issuer_raw.len = len;
970 p += len;
971
972 ret = mbedtls_x509_name_cmp_raw( &frame->issuer_raw,
973 &frame->issuer_raw,
974 NULL, NULL );
975 if( ret != 0 )
976 return( ret );
977
978 frame->issuer_raw_with_hdr.len = p - frame->issuer_raw_with_hdr.p;
979
980 /*
981 * Validity ::= SEQUENCE { ...
982 */
983 ret = x509_get_dates( &p, end, &frame->valid_from, &frame->valid_to );
984 if( ret != 0 )
985 return( ret );
986
987 /*
988 * subject Name
989 *
990 * Name ::= CHOICE { -- only one possibility for now --
991 * rdnSequence RDNSequence }
992 *
993 * RDNSequence ::= SEQUENCE OF RelativeDistinguishedName
994 */
995 frame->subject_raw_with_hdr.p = p;
996
997 ret = mbedtls_asn1_get_tag( &p, end, &len,
998 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE );
999 if( ret != 0 )
1000 return( ret + MBEDTLS_ERR_X509_INVALID_FORMAT );
1001 frame->subject_raw.p = p;
1002 frame->subject_raw.len = len;
1003 p += len;
1004
1005 ret = mbedtls_x509_name_cmp_raw( &frame->subject_raw,
1006 &frame->subject_raw,
1007 NULL, NULL );
1008 if( ret != 0 )
1009 return( ret );
1010
1011 frame->subject_raw_with_hdr.len = p - frame->subject_raw_with_hdr.p;
1012
1013 /*
1014 * SubjectPublicKeyInfo
1015 */
1016 frame->pubkey_raw.p = p;
1017 ret = mbedtls_asn1_get_tag( &p, end, &len,
1018 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE );
1019 if( ret != 0 )
1020 return( ret + MBEDTLS_ERR_PK_KEY_INVALID_FORMAT );
1021 p += len;
1022 frame->pubkey_raw.len = p - frame->pubkey_raw.p;
1023
1024 /*
1025 * issuerUniqueID [1] IMPLICIT UniqueIdentifier OPTIONAL,
1026 * -- If present, version shall be v2 or v3
1027 */
1028 if( frame->version == 2 || frame->version == 3 )
1029 {
1030 ret = x509_get_uid( &p, end, &tmp, 1 /* implicit tag */ );
1031 if( ret != 0 )
1032 return( ret );
1033
1034 frame->issuer_id.p = tmp.p;
1035 frame->issuer_id.len = tmp.len;
1036 }
1037
1038 /*
1039 * subjectUniqueID [2] IMPLICIT UniqueIdentifier OPTIONAL,
1040 * -- If present, version shall be v2 or v3
1041 */
1042 if( frame->version == 2 || frame->version == 3 )
1043 {
1044 ret = x509_get_uid( &p, end, &tmp, 2 /* implicit tag */ );
1045 if( ret != 0 )
1046 return( ret );
1047
1048 frame->subject_id.p = tmp.p;
1049 frame->subject_id.len = tmp.len;
1050 }
1051
1052 /*
1053 * extensions [3] EXPLICIT Extensions OPTIONAL
1054 * -- If present, version shall be v3
1055 */
1056#if !defined(MBEDTLS_X509_ALLOW_EXTENSIONS_NON_V3)
1057 if( frame->version == 3 )
1058#endif
1059 {
1060 if( p != end )
1061 {
1062 ret = mbedtls_asn1_get_tag( &p, end, &len,
1063 MBEDTLS_ASN1_CONTEXT_SPECIFIC |
1064 MBEDTLS_ASN1_CONSTRUCTED | 3 );
1065 if( len == 0 )
1066 ret = MBEDTLS_ERR_ASN1_OUT_OF_DATA;
1067 if( ret != 0 )
1068 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS + ret );
1069
1070 frame->v3_ext.p = p;
1071 frame->v3_ext.len = len;
1072
1073 p += len;
1074 }
1075
1076 ret = x509_crt_frame_parse_ext( frame );
1077 if( ret != 0 )
1078 return( ret );
1079 }
1080
1081 /* Wrapup: Check that we consumed the entire `TBSCertificate` structure. */
1082 if( p != end )
1083 {
1084 return( MBEDTLS_ERR_X509_INVALID_FORMAT +
1085 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
1086 }
1087
1088 return( 0 );
1089}
1090
1091static int x509_crt_subject_from_frame( mbedtls_x509_crt_frame *frame,
1092 mbedtls_x509_name *subject )
1093{
1094 unsigned char *p = frame->subject_raw.p;
1095 unsigned char *end = p + frame->subject_raw.len;
1096
1097 return( mbedtls_x509_get_name( &p, end, subject ) );
1098}
1099
1100static int x509_crt_issuer_from_frame( mbedtls_x509_crt_frame *frame,
1101 mbedtls_x509_name *issuer )
1102{
1103 unsigned char *p = frame->issuer_raw.p;
1104 unsigned char *end = p + frame->issuer_raw.len;
1105
1106 return( mbedtls_x509_get_name( &p, end, issuer ) );
1107}
1108
1109static int x509_crt_subject_alt_from_frame( mbedtls_x509_crt_frame *frame,
1110 mbedtls_x509_sequence *subject_alt )
1111{
1112 int ret;
1113 unsigned char *p = frame->subject_alt_raw.p;
1114 unsigned char *end = p + frame->subject_alt_raw.len;
1115
1116 memset( subject_alt, 0, sizeof( *subject_alt ) );
1117
1118 if( ( frame->ext_types & MBEDTLS_X509_EXT_SUBJECT_ALT_NAME ) == 0 )
1119 return( 0 );
1120
1121 ret = x509_get_subject_alt_name( p, end, subject_alt );
1122 if( ret != 0 )
1123 ret += MBEDTLS_ERR_X509_INVALID_EXTENSIONS;
1124 return( ret );
1125}
1126
1127static int x509_crt_ext_key_usage_from_frame( mbedtls_x509_crt_frame *frame,
1128 mbedtls_x509_sequence *ext_key_usage )
1129{
1130 int ret;
1131 unsigned char *p = frame->ext_key_usage_raw.p;
1132 unsigned char *end = p + frame->ext_key_usage_raw.len;
1133
1134 memset( ext_key_usage, 0, sizeof( *ext_key_usage ) );
1135
1136 if( ( frame->ext_types & MBEDTLS_X509_EXT_EXTENDED_KEY_USAGE ) == 0 )
1137 return( 0 );
1138
1139 ret = x509_get_ext_key_usage( &p, end, ext_key_usage );
1140 if( ret != 0 )
1141 {
1142 ret += MBEDTLS_ERR_X509_INVALID_EXTENSIONS;
1143 return( ret );
1144 }
1145
1146 return( 0 );
1147}
1148
1149static int x509_crt_pk_from_frame( mbedtls_x509_crt_frame *frame,
1150 mbedtls_pk_context *pk )
1151{
1152 unsigned char *p = frame->pubkey_raw.p;
1153 unsigned char *end = p + frame->pubkey_raw.len;
1154 return( mbedtls_pk_parse_subpubkey( &p, end, pk ) );
1155}
1156
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001157/*
1158 * Parse and fill a single X.509 certificate in DER format
1159 */
Hanno Beckeraa8665a2019-01-31 08:57:44 +00001160static int x509_crt_parse_der_core( mbedtls_x509_crt *crt,
1161 const unsigned char *buf,
1162 size_t buflen,
1163 int make_copy )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001164{
1165 int ret;
Hanno Becker21f55672019-02-15 15:27:59 +00001166 mbedtls_x509_crt_frame frame;
Manuel Pégourié-Gonnard59a75d52014-01-22 10:12:57 +01001167
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001168 if( crt == NULL || buf == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001169 return( MBEDTLS_ERR_X509_BAD_INPUT_DATA );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001170
Hanno Becker21f55672019-02-15 15:27:59 +00001171 if( make_copy == 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001172 {
Hanno Becker21f55672019-02-15 15:27:59 +00001173 crt->raw.p = (unsigned char*) buf;
1174 crt->raw.len = buflen;
1175 crt->own_buffer = 0;
Hanno Beckeraa8665a2019-01-31 08:57:44 +00001176 }
1177 else
1178 {
Hanno Becker21f55672019-02-15 15:27:59 +00001179 crt->raw.p = mbedtls_calloc( 1, buflen );
1180 if( crt->raw.p == NULL )
1181 return( MBEDTLS_ERR_X509_ALLOC_FAILED );
1182 crt->raw.len = buflen;
1183 memcpy( crt->raw.p, buf, buflen );
1184
1185 crt->own_buffer = 1;
Hanno Beckeraa8665a2019-01-31 08:57:44 +00001186 }
Janos Follathcc0e49d2016-02-17 14:34:12 +00001187
Hanno Becker21f55672019-02-15 15:27:59 +00001188 /* Parse CRT frame.
1189 * This omits:
1190 * - Issuer, Subject
1191 * - ExtKeyUsage, SubjectAltNames,
1192 * - Time
1193 * - PK
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001194 */
Hanno Becker21f55672019-02-15 15:27:59 +00001195 ret = x509_crt_parse_frame( crt->raw.p,
1196 crt->raw.p + crt->raw.len,
1197 &frame );
1198 if( ret != 0 )
1199 goto exit;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001200
Hanno Becker21f55672019-02-15 15:27:59 +00001201 /* Currently, we accept DER encoded CRTs with trailing garbage
1202 * and promise to not account for the garbage in the `raw` field.
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001203 *
Hanno Becker21f55672019-02-15 15:27:59 +00001204 * Note that this means that `crt->raw.len` is not necessarily the
1205 * full size of the heap buffer allocated at `crt->raw.p` in case
1206 * of copy-mode, but this is not a problem: freeing the buffer doesn't
1207 * need the size, and the garbage data doesn't need zeroization. */
1208 crt->raw.len = frame->raw.len;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001209
Hanno Becker21f55672019-02-15 15:27:59 +00001210 /* Copy frame to legacy CRT structure -- that's inefficient, but if
1211 * memory matters, the new CRT structure should be used anyway. */
1212 crt->tbs.p = frame.tbs.p;
1213 crt->tbs.len = frame.tbs.len;
1214 crt->serial.p = frame.serial.p;
1215 crt->serial.len = frame.serial.len;
1216 crt->issuer_raw.p = frame.issuer_raw_with_hdr.p;
1217 crt->issuer_raw.len = frame.issuer_raw_with_hdr.len;
1218 crt->subject_raw.p = frame.subject_raw_with_hdr.p;
1219 crt->subject_raw.len = frame.subject_raw_with_hdr.len;
1220 crt->issuer_raw_no_hdr = frame.issuer_raw;
1221 crt->subject_raw_no_hdr = frame.subject_raw;
1222 crt->issuer_id.p = frame.issuer_id.p;
1223 crt->issuer_id.len = frame.issuer_id.len;
1224 crt->subject_id.p = frame.subject_id.p;
1225 crt->subject_id.len = frame.subject_id.len;
1226 crt->pk_raw.p = frame.pubkey_raw.p;
1227 crt->pk_raw.len = frame.pubkey_raw.len;
1228 crt->ext_key_usage_raw = frame.ext_key_usage_raw;
1229 crt->subject_alt_raw = frame.subject_alt_raw;
1230 crt->sig.p = frame.sig.p;
1231 crt->sig.len = frame.sig.len;
1232 crt->valid_from = frame.valid_from;
1233 crt->valid_to = frame.valid_to;
1234 crt->v3_ext.p = frame.v3_ext.p;
1235 crt->v3_ext.len = frame.v3_ext.len;
1236 crt->version = frame.version;
1237 crt->ca_istrue = frame.ca_istrue;
1238 crt->max_pathlen = frame.max_pathlen;
1239 crt->ext_types = frame.ext_types;
1240 crt->key_usage = frame.key_usage;
1241 crt->ns_cert_type = frame.ns_cert_type;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001242
1243 /*
Hanno Becker21f55672019-02-15 15:27:59 +00001244 * Obtain the remaining fields from the frame.
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001245 */
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001246
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001247 {
Hanno Becker21f55672019-02-15 15:27:59 +00001248 /* sig_oid: Previously, needed for convenience in
1249 * mbedtls_x509_crt_info(), now pure legacy burden. */
1250 unsigned char *tmp = frame.sig_alg.p;
1251 unsigned char *end = tmp + frame.sig_alg.len;
1252 mbedtls_x509_buf sig_oid, sig_params;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001253
Hanno Becker21f55672019-02-15 15:27:59 +00001254 ret = mbedtls_x509_get_alg( &tmp, end,
1255 &sig_oid, &sig_params );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001256 if( ret != 0 )
1257 {
Hanno Becker21f55672019-02-15 15:27:59 +00001258 /* This should never happen, because we check
1259 * the sanity of the AlgorithmIdentifier structure
1260 * during frame parsing. */
1261 ret = MBEDTLS_ERR_X509_FATAL_ERROR;
1262 goto exit;
1263 }
1264 crt->sig_oid = sig_oid;
1265
1266 /* Signature parameters */
1267 tmp = frame.sig_alg.p;
1268 ret = mbedtls_x509_get_sig_alg_raw( &tmp, end,
1269 &crt->sig_md, &crt->sig_pk,
1270 &crt->sig_opts );
1271 if( ret != 0 )
1272 {
1273 /* Again, this should never happen. */
1274 ret = MBEDTLS_ERR_X509_FATAL_ERROR;
1275 goto exit;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001276 }
1277 }
1278
Hanno Becker21f55672019-02-15 15:27:59 +00001279 ret = x509_crt_pk_from_frame( &frame, &crt->pk );
1280 if( ret != 0 )
1281 goto exit;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001282
Hanno Becker21f55672019-02-15 15:27:59 +00001283 ret = x509_crt_subject_from_frame( &frame, &crt->subject );
1284 if( ret != 0 )
1285 goto exit;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001286
Hanno Becker21f55672019-02-15 15:27:59 +00001287 ret = x509_crt_issuer_from_frame( &frame, &crt->issuer );
1288 if( ret != 0 )
1289 goto exit;
1290
1291 ret = x509_crt_subject_alt_from_frame( &frame, &crt->subject_alt_names );
1292 if( ret != 0 )
1293 goto exit;
1294
1295 ret = x509_crt_ext_key_usage_from_frame( &frame, &crt->ext_key_usage );
1296 if( ret != 0 )
1297 goto exit;
1298
1299exit:
1300 if( ret != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001301 mbedtls_x509_crt_free( crt );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001302
Hanno Becker21f55672019-02-15 15:27:59 +00001303 return( ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001304}
1305
1306/*
1307 * Parse one X.509 certificate in DER format from a buffer and add them to a
1308 * chained list
1309 */
Hanno Beckeraa8665a2019-01-31 08:57:44 +00001310static int mbedtls_x509_crt_parse_der_internal( mbedtls_x509_crt *chain,
1311 const unsigned char *buf,
1312 size_t buflen,
1313 int make_copy )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001314{
1315 int ret;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001316 mbedtls_x509_crt *crt = chain, *prev = NULL;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001317
1318 /*
1319 * Check for valid input
1320 */
1321 if( crt == NULL || buf == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001322 return( MBEDTLS_ERR_X509_BAD_INPUT_DATA );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001323
1324 while( crt->version != 0 && crt->next != NULL )
1325 {
1326 prev = crt;
1327 crt = crt->next;
1328 }
1329
1330 /*
1331 * Add new certificate on the end of the chain if needed.
1332 */
Paul Bakker66d5d072014-06-17 16:39:18 +02001333 if( crt->version != 0 && crt->next == NULL )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001334 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02001335 crt->next = mbedtls_calloc( 1, sizeof( mbedtls_x509_crt ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001336
1337 if( crt->next == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02001338 return( MBEDTLS_ERR_X509_ALLOC_FAILED );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001339
1340 prev = crt;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001341 mbedtls_x509_crt_init( crt->next );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001342 crt = crt->next;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001343 }
1344
Hanno Beckeraa8665a2019-01-31 08:57:44 +00001345 if( ( ret = x509_crt_parse_der_core( crt, buf, buflen, make_copy ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001346 {
1347 if( prev )
1348 prev->next = NULL;
1349
1350 if( crt != chain )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001351 mbedtls_free( crt );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001352
1353 return( ret );
1354 }
1355
1356 return( 0 );
1357}
1358
Hanno Beckeraa8665a2019-01-31 08:57:44 +00001359int mbedtls_x509_crt_parse_der_nocopy( mbedtls_x509_crt *chain,
1360 const unsigned char *buf,
1361 size_t buflen )
1362{
1363 return( mbedtls_x509_crt_parse_der_internal( chain, buf, buflen, 0 ) );
1364}
1365
1366int mbedtls_x509_crt_parse_der( mbedtls_x509_crt *chain,
1367 const unsigned char *buf,
1368 size_t buflen )
1369{
1370 return( mbedtls_x509_crt_parse_der_internal( chain, buf, buflen, 1 ) );
1371}
1372
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001373/*
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001374 * Parse one or more PEM certificates from a buffer and add them to the chained
1375 * list
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001376 */
Hanno Beckeraa8665a2019-01-31 08:57:44 +00001377int mbedtls_x509_crt_parse( mbedtls_x509_crt *chain,
1378 const unsigned char *buf,
1379 size_t buflen )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001380{
Janos Follath98e28a72016-05-31 14:03:54 +01001381#if defined(MBEDTLS_PEM_PARSE_C)
Andres AGc0db5112016-12-07 15:05:53 +00001382 int success = 0, first_error = 0, total_failed = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001383 int buf_format = MBEDTLS_X509_FORMAT_DER;
Janos Follath98e28a72016-05-31 14:03:54 +01001384#endif
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001385
1386 /*
1387 * Check for valid input
1388 */
1389 if( chain == NULL || buf == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001390 return( MBEDTLS_ERR_X509_BAD_INPUT_DATA );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001391
1392 /*
1393 * Determine buffer content. Buffer contains either one DER certificate or
1394 * one or more PEM certificates.
1395 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001396#if defined(MBEDTLS_PEM_PARSE_C)
Manuel Pégourié-Gonnard0ece0f92015-05-12 12:43:54 +02001397 if( buflen != 0 && buf[buflen - 1] == '\0' &&
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001398 strstr( (const char *) buf, "-----BEGIN CERTIFICATE-----" ) != NULL )
1399 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001400 buf_format = MBEDTLS_X509_FORMAT_PEM;
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001401 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001402
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001403 if( buf_format == MBEDTLS_X509_FORMAT_DER )
1404 return mbedtls_x509_crt_parse_der( chain, buf, buflen );
Janos Follath98e28a72016-05-31 14:03:54 +01001405#else
1406 return mbedtls_x509_crt_parse_der( chain, buf, buflen );
1407#endif
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001408
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001409#if defined(MBEDTLS_PEM_PARSE_C)
1410 if( buf_format == MBEDTLS_X509_FORMAT_PEM )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001411 {
1412 int ret;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001413 mbedtls_pem_context pem;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001414
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001415 /* 1 rather than 0 since the terminating NULL byte is counted in */
1416 while( buflen > 1 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001417 {
1418 size_t use_len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001419 mbedtls_pem_init( &pem );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001420
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001421 /* If we get there, we know the string is null-terminated */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001422 ret = mbedtls_pem_read_buffer( &pem,
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001423 "-----BEGIN CERTIFICATE-----",
1424 "-----END CERTIFICATE-----",
1425 buf, NULL, 0, &use_len );
1426
1427 if( ret == 0 )
1428 {
1429 /*
1430 * Was PEM encoded
1431 */
1432 buflen -= use_len;
1433 buf += use_len;
1434 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001435 else if( ret == MBEDTLS_ERR_PEM_BAD_INPUT_DATA )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001436 {
1437 return( ret );
1438 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001439 else if( ret != MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001440 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001441 mbedtls_pem_free( &pem );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001442
1443 /*
1444 * PEM header and footer were found
1445 */
1446 buflen -= use_len;
1447 buf += use_len;
1448
1449 if( first_error == 0 )
1450 first_error = ret;
1451
Paul Bakker5a5fa922014-09-26 14:53:04 +02001452 total_failed++;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001453 continue;
1454 }
1455 else
1456 break;
1457
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001458 ret = mbedtls_x509_crt_parse_der( chain, pem.buf, pem.buflen );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001459
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001460 mbedtls_pem_free( &pem );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001461
1462 if( ret != 0 )
1463 {
1464 /*
1465 * Quit parsing on a memory error
1466 */
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02001467 if( ret == MBEDTLS_ERR_X509_ALLOC_FAILED )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001468 return( ret );
1469
1470 if( first_error == 0 )
1471 first_error = ret;
1472
1473 total_failed++;
1474 continue;
1475 }
1476
1477 success = 1;
1478 }
1479 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001480
1481 if( success )
1482 return( total_failed );
1483 else if( first_error )
1484 return( first_error );
1485 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001486 return( MBEDTLS_ERR_X509_CERT_UNKNOWN_FORMAT );
Janos Follath98e28a72016-05-31 14:03:54 +01001487#endif /* MBEDTLS_PEM_PARSE_C */
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001488}
1489
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001490#if defined(MBEDTLS_FS_IO)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001491/*
1492 * Load one or more certificates and add them to the chained list
1493 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001494int mbedtls_x509_crt_parse_file( mbedtls_x509_crt *chain, const char *path )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001495{
1496 int ret;
1497 size_t n;
1498 unsigned char *buf;
1499
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001500 if( ( ret = mbedtls_pk_load_file( path, &buf, &n ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001501 return( ret );
1502
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001503 ret = mbedtls_x509_crt_parse( chain, buf, n );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001504
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05001505 mbedtls_platform_zeroize( buf, n );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001506 mbedtls_free( buf );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001507
1508 return( ret );
1509}
1510
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001511int mbedtls_x509_crt_parse_path( mbedtls_x509_crt *chain, const char *path )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001512{
1513 int ret = 0;
Paul Bakkerfa6a6202013-10-28 18:48:30 +01001514#if defined(_WIN32) && !defined(EFIX64) && !defined(EFI32)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001515 int w_ret;
1516 WCHAR szDir[MAX_PATH];
1517 char filename[MAX_PATH];
Paul Bakker9af723c2014-05-01 13:03:14 +02001518 char *p;
Manuel Pégourié-Gonnard261faed2015-10-21 10:16:29 +02001519 size_t len = strlen( path );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001520
Paul Bakker9af723c2014-05-01 13:03:14 +02001521 WIN32_FIND_DATAW file_data;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001522 HANDLE hFind;
1523
1524 if( len > MAX_PATH - 3 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001525 return( MBEDTLS_ERR_X509_BAD_INPUT_DATA );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001526
Paul Bakker9af723c2014-05-01 13:03:14 +02001527 memset( szDir, 0, sizeof(szDir) );
1528 memset( filename, 0, MAX_PATH );
1529 memcpy( filename, path, len );
1530 filename[len++] = '\\';
1531 p = filename + len;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001532 filename[len++] = '*';
1533
Simon B3c6b18d2016-11-03 01:11:37 +00001534 w_ret = MultiByteToWideChar( CP_ACP, 0, filename, (int)len, szDir,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001535 MAX_PATH - 3 );
Manuel Pégourié-Gonnardacdb9b92015-01-23 17:50:34 +00001536 if( w_ret == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001537 return( MBEDTLS_ERR_X509_BAD_INPUT_DATA );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001538
1539 hFind = FindFirstFileW( szDir, &file_data );
Paul Bakker66d5d072014-06-17 16:39:18 +02001540 if( hFind == INVALID_HANDLE_VALUE )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001541 return( MBEDTLS_ERR_X509_FILE_IO_ERROR );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001542
1543 len = MAX_PATH - len;
1544 do
1545 {
Paul Bakker9af723c2014-05-01 13:03:14 +02001546 memset( p, 0, len );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001547
1548 if( file_data.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY )
1549 continue;
1550
Paul Bakker9af723c2014-05-01 13:03:14 +02001551 w_ret = WideCharToMultiByte( CP_ACP, 0, file_data.cFileName,
Paul Bakker66d5d072014-06-17 16:39:18 +02001552 lstrlenW( file_data.cFileName ),
Manuel Pégourié-Gonnard261faed2015-10-21 10:16:29 +02001553 p, (int) len - 1,
Paul Bakker9af723c2014-05-01 13:03:14 +02001554 NULL, NULL );
Manuel Pégourié-Gonnardacdb9b92015-01-23 17:50:34 +00001555 if( w_ret == 0 )
Ron Eldor36d90422017-01-09 15:09:16 +02001556 {
1557 ret = MBEDTLS_ERR_X509_FILE_IO_ERROR;
1558 goto cleanup;
1559 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001560
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001561 w_ret = mbedtls_x509_crt_parse_file( chain, filename );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001562 if( w_ret < 0 )
1563 ret++;
1564 else
1565 ret += w_ret;
1566 }
1567 while( FindNextFileW( hFind, &file_data ) != 0 );
1568
Paul Bakker66d5d072014-06-17 16:39:18 +02001569 if( GetLastError() != ERROR_NO_MORE_FILES )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001570 ret = MBEDTLS_ERR_X509_FILE_IO_ERROR;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001571
Ron Eldor36d90422017-01-09 15:09:16 +02001572cleanup:
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001573 FindClose( hFind );
Paul Bakkerbe089b02013-10-14 15:51:50 +02001574#else /* _WIN32 */
Manuel Pégourié-Gonnard964bf9b2013-11-26 16:47:11 +01001575 int t_ret;
Andres AGf9113192016-09-02 14:06:04 +01001576 int snp_ret;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001577 struct stat sb;
Manuel Pégourié-Gonnard964bf9b2013-11-26 16:47:11 +01001578 struct dirent *entry;
Andres AGf9113192016-09-02 14:06:04 +01001579 char entry_name[MBEDTLS_X509_MAX_FILE_PATH_LEN];
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001580 DIR *dir = opendir( path );
1581
Paul Bakker66d5d072014-06-17 16:39:18 +02001582 if( dir == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001583 return( MBEDTLS_ERR_X509_FILE_IO_ERROR );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001584
Ron Eldor63140682017-01-09 19:27:59 +02001585#if defined(MBEDTLS_THREADING_C)
Manuel Pégourié-Gonnard944cfe82015-05-27 20:07:18 +02001586 if( ( ret = mbedtls_mutex_lock( &mbedtls_threading_readdir_mutex ) ) != 0 )
Manuel Pégourié-Gonnardf9b85d92015-06-22 18:39:57 +02001587 {
1588 closedir( dir );
Manuel Pégourié-Gonnard5ad68e42013-11-28 17:11:54 +01001589 return( ret );
Manuel Pégourié-Gonnardf9b85d92015-06-22 18:39:57 +02001590 }
Ron Eldor63140682017-01-09 19:27:59 +02001591#endif /* MBEDTLS_THREADING_C */
Manuel Pégourié-Gonnard5ad68e42013-11-28 17:11:54 +01001592
Manuel Pégourié-Gonnard964bf9b2013-11-26 16:47:11 +01001593 while( ( entry = readdir( dir ) ) != NULL )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001594 {
Andres AGf9113192016-09-02 14:06:04 +01001595 snp_ret = mbedtls_snprintf( entry_name, sizeof entry_name,
1596 "%s/%s", path, entry->d_name );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001597
Andres AGf9113192016-09-02 14:06:04 +01001598 if( snp_ret < 0 || (size_t)snp_ret >= sizeof entry_name )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001599 {
Andres AGf9113192016-09-02 14:06:04 +01001600 ret = MBEDTLS_ERR_X509_BUFFER_TOO_SMALL;
1601 goto cleanup;
1602 }
1603 else if( stat( entry_name, &sb ) == -1 )
1604 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001605 ret = MBEDTLS_ERR_X509_FILE_IO_ERROR;
Manuel Pégourié-Gonnard5ad68e42013-11-28 17:11:54 +01001606 goto cleanup;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001607 }
1608
1609 if( !S_ISREG( sb.st_mode ) )
1610 continue;
1611
1612 // Ignore parse errors
1613 //
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001614 t_ret = mbedtls_x509_crt_parse_file( chain, entry_name );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001615 if( t_ret < 0 )
1616 ret++;
1617 else
1618 ret += t_ret;
1619 }
Manuel Pégourié-Gonnard5ad68e42013-11-28 17:11:54 +01001620
1621cleanup:
Andres AGf9113192016-09-02 14:06:04 +01001622 closedir( dir );
1623
Ron Eldor63140682017-01-09 19:27:59 +02001624#if defined(MBEDTLS_THREADING_C)
Manuel Pégourié-Gonnard944cfe82015-05-27 20:07:18 +02001625 if( mbedtls_mutex_unlock( &mbedtls_threading_readdir_mutex ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001626 ret = MBEDTLS_ERR_THREADING_MUTEX_ERROR;
Ron Eldor63140682017-01-09 19:27:59 +02001627#endif /* MBEDTLS_THREADING_C */
Manuel Pégourié-Gonnard5ad68e42013-11-28 17:11:54 +01001628
Paul Bakkerbe089b02013-10-14 15:51:50 +02001629#endif /* _WIN32 */
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001630
1631 return( ret );
1632}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001633#endif /* MBEDTLS_FS_IO */
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001634
Hanno Becker02a21932019-06-10 15:08:43 +01001635#if !defined(MBEDTLS_X509_REMOVE_INFO)
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02001636static int x509_info_subject_alt_name( char **buf, size_t *size,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001637 const mbedtls_x509_sequence *subject_alt_name )
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02001638{
1639 size_t i;
1640 size_t n = *size;
1641 char *p = *buf;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001642 const mbedtls_x509_sequence *cur = subject_alt_name;
Manuel Pégourié-Gonnard7b30cfc2014-04-01 18:00:07 +02001643 const char *sep = "";
1644 size_t sep_len = 0;
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02001645
1646 while( cur != NULL )
1647 {
Manuel Pégourié-Gonnard7b30cfc2014-04-01 18:00:07 +02001648 if( cur->buf.len + sep_len >= n )
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02001649 {
1650 *p = '\0';
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001651 return( MBEDTLS_ERR_X509_BUFFER_TOO_SMALL );
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02001652 }
1653
Manuel Pégourié-Gonnard7b30cfc2014-04-01 18:00:07 +02001654 n -= cur->buf.len + sep_len;
1655 for( i = 0; i < sep_len; i++ )
1656 *p++ = sep[i];
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02001657 for( i = 0; i < cur->buf.len; i++ )
1658 *p++ = cur->buf.p[i];
1659
Manuel Pégourié-Gonnard7b30cfc2014-04-01 18:00:07 +02001660 sep = ", ";
1661 sep_len = 2;
1662
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02001663 cur = cur->next;
1664 }
1665
1666 *p = '\0';
1667
1668 *size = n;
1669 *buf = p;
1670
1671 return( 0 );
1672}
1673
Manuel Pégourié-Gonnard0db29b02014-04-01 18:12:24 +02001674#define PRINT_ITEM(i) \
1675 { \
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001676 ret = mbedtls_snprintf( p, n, "%s" i, sep ); \
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001677 MBEDTLS_X509_SAFE_SNPRINTF; \
Manuel Pégourié-Gonnard0db29b02014-04-01 18:12:24 +02001678 sep = ", "; \
1679 }
1680
1681#define CERT_TYPE(type,name) \
Hanno Beckerd6028a12018-10-15 12:01:35 +01001682 if( ns_cert_type & (type) ) \
Manuel Pégourié-Gonnard0db29b02014-04-01 18:12:24 +02001683 PRINT_ITEM( name );
1684
Manuel Pégourié-Gonnard919f8f52014-04-01 13:01:11 +02001685static int x509_info_cert_type( char **buf, size_t *size,
1686 unsigned char ns_cert_type )
1687{
1688 int ret;
1689 size_t n = *size;
1690 char *p = *buf;
Manuel Pégourié-Gonnard7b30cfc2014-04-01 18:00:07 +02001691 const char *sep = "";
Manuel Pégourié-Gonnard919f8f52014-04-01 13:01:11 +02001692
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001693 CERT_TYPE( MBEDTLS_X509_NS_CERT_TYPE_SSL_CLIENT, "SSL Client" );
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01001694 CERT_TYPE( MBEDTLS_X509_NS_CERT_TYPE_SSL_SERVER, "SSL Server" );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001695 CERT_TYPE( MBEDTLS_X509_NS_CERT_TYPE_EMAIL, "Email" );
1696 CERT_TYPE( MBEDTLS_X509_NS_CERT_TYPE_OBJECT_SIGNING, "Object Signing" );
1697 CERT_TYPE( MBEDTLS_X509_NS_CERT_TYPE_RESERVED, "Reserved" );
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01001698 CERT_TYPE( MBEDTLS_X509_NS_CERT_TYPE_SSL_CA, "SSL CA" );
1699 CERT_TYPE( MBEDTLS_X509_NS_CERT_TYPE_EMAIL_CA, "Email CA" );
1700 CERT_TYPE( MBEDTLS_X509_NS_CERT_TYPE_OBJECT_SIGNING_CA, "Object Signing CA" );
Manuel Pégourié-Gonnard919f8f52014-04-01 13:01:11 +02001701
1702 *size = n;
1703 *buf = p;
1704
1705 return( 0 );
1706}
1707
Manuel Pégourié-Gonnard0db29b02014-04-01 18:12:24 +02001708#define KEY_USAGE(code,name) \
Hanno Beckerd6028a12018-10-15 12:01:35 +01001709 if( key_usage & (code) ) \
Manuel Pégourié-Gonnard0db29b02014-04-01 18:12:24 +02001710 PRINT_ITEM( name );
1711
Manuel Pégourié-Gonnard65c2ddc2014-04-01 14:12:11 +02001712static int x509_info_key_usage( char **buf, size_t *size,
Manuel Pégourié-Gonnard9a702252015-06-23 10:14:36 +02001713 unsigned int key_usage )
Manuel Pégourié-Gonnard65c2ddc2014-04-01 14:12:11 +02001714{
1715 int ret;
1716 size_t n = *size;
1717 char *p = *buf;
Manuel Pégourié-Gonnard7b30cfc2014-04-01 18:00:07 +02001718 const char *sep = "";
Manuel Pégourié-Gonnard65c2ddc2014-04-01 14:12:11 +02001719
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001720 KEY_USAGE( MBEDTLS_X509_KU_DIGITAL_SIGNATURE, "Digital Signature" );
1721 KEY_USAGE( MBEDTLS_X509_KU_NON_REPUDIATION, "Non Repudiation" );
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01001722 KEY_USAGE( MBEDTLS_X509_KU_KEY_ENCIPHERMENT, "Key Encipherment" );
1723 KEY_USAGE( MBEDTLS_X509_KU_DATA_ENCIPHERMENT, "Data Encipherment" );
1724 KEY_USAGE( MBEDTLS_X509_KU_KEY_AGREEMENT, "Key Agreement" );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001725 KEY_USAGE( MBEDTLS_X509_KU_KEY_CERT_SIGN, "Key Cert Sign" );
1726 KEY_USAGE( MBEDTLS_X509_KU_CRL_SIGN, "CRL Sign" );
Manuel Pégourié-Gonnard9a702252015-06-23 10:14:36 +02001727 KEY_USAGE( MBEDTLS_X509_KU_ENCIPHER_ONLY, "Encipher Only" );
1728 KEY_USAGE( MBEDTLS_X509_KU_DECIPHER_ONLY, "Decipher Only" );
Manuel Pégourié-Gonnard65c2ddc2014-04-01 14:12:11 +02001729
1730 *size = n;
1731 *buf = p;
1732
1733 return( 0 );
1734}
1735
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02001736static int x509_info_ext_key_usage( char **buf, size_t *size,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001737 const mbedtls_x509_sequence *extended_key_usage )
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02001738{
1739 int ret;
1740 const char *desc;
1741 size_t n = *size;
1742 char *p = *buf;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001743 const mbedtls_x509_sequence *cur = extended_key_usage;
Manuel Pégourié-Gonnard7b30cfc2014-04-01 18:00:07 +02001744 const char *sep = "";
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02001745
1746 while( cur != NULL )
1747 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001748 if( mbedtls_oid_get_extended_key_usage( &cur->buf, &desc ) != 0 )
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02001749 desc = "???";
1750
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001751 ret = mbedtls_snprintf( p, n, "%s%s", sep, desc );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001752 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02001753
Manuel Pégourié-Gonnard7b30cfc2014-04-01 18:00:07 +02001754 sep = ", ";
1755
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02001756 cur = cur->next;
1757 }
1758
1759 *size = n;
1760 *buf = p;
1761
1762 return( 0 );
1763}
1764
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001765/*
1766 * Return an informational string about the certificate.
1767 */
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001768#define BEFORE_COLON 18
1769#define BC "18"
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001770int mbedtls_x509_crt_info( char *buf, size_t size, const char *prefix,
1771 const mbedtls_x509_crt *crt )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001772{
1773 int ret;
1774 size_t n;
1775 char *p;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001776 char key_size_str[BEFORE_COLON];
1777
1778 p = buf;
1779 n = size;
1780
Janos Follath98e28a72016-05-31 14:03:54 +01001781 if( NULL == crt )
1782 {
1783 ret = mbedtls_snprintf( p, n, "\nCertificate is uninitialised!\n" );
1784 MBEDTLS_X509_SAFE_SNPRINTF;
1785
1786 return( (int) ( size - n ) );
1787 }
1788
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001789 ret = mbedtls_snprintf( p, n, "%scert. version : %d\n",
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001790 prefix, crt->version );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001791 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001792 ret = mbedtls_snprintf( p, n, "%sserial number : ",
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001793 prefix );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001794 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001795
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001796 ret = mbedtls_x509_serial_gets( p, n, &crt->serial );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001797 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001798
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001799 ret = mbedtls_snprintf( p, n, "\n%sissuer name : ", prefix );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001800 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001801 ret = mbedtls_x509_dn_gets( p, n, &crt->issuer );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001802 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001803
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001804 ret = mbedtls_snprintf( p, n, "\n%ssubject name : ", prefix );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001805 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001806 ret = mbedtls_x509_dn_gets( p, n, &crt->subject );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001807 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001808
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001809 ret = mbedtls_snprintf( p, n, "\n%sissued on : " \
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001810 "%04d-%02d-%02d %02d:%02d:%02d", prefix,
1811 crt->valid_from.year, crt->valid_from.mon,
1812 crt->valid_from.day, crt->valid_from.hour,
1813 crt->valid_from.min, crt->valid_from.sec );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001814 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001815
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001816 ret = mbedtls_snprintf( p, n, "\n%sexpires on : " \
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001817 "%04d-%02d-%02d %02d:%02d:%02d", prefix,
1818 crt->valid_to.year, crt->valid_to.mon,
1819 crt->valid_to.day, crt->valid_to.hour,
1820 crt->valid_to.min, crt->valid_to.sec );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001821 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001822
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001823 ret = mbedtls_snprintf( p, n, "\n%ssigned using : ", prefix );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001824 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001825
Hanno Becker83cd8672019-02-21 17:13:46 +00001826 ret = mbedtls_x509_sig_alg_gets( p, n, sig_info.sig_pk,
1827 sig_info.sig_md, sig_info.sig_opts );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001828 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001829
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001830 /* Key size */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001831 if( ( ret = mbedtls_x509_key_size_helper( key_size_str, BEFORE_COLON,
1832 mbedtls_pk_get_name( &crt->pk ) ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001833 {
1834 return( ret );
1835 }
1836
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001837 ret = mbedtls_snprintf( p, n, "\n%s%-" BC "s: %d bits", prefix, key_size_str,
Manuel Pégourié-Gonnard097c7bb2015-06-18 16:43:38 +02001838 (int) mbedtls_pk_get_bitlen( &crt->pk ) );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001839 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001840
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001841 /*
1842 * Optional extensions
1843 */
1844
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01001845 if( crt->ext_types & MBEDTLS_X509_EXT_BASIC_CONSTRAINTS )
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001846 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001847 ret = mbedtls_snprintf( p, n, "\n%sbasic constraints : CA=%s", prefix,
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001848 crt->ca_istrue ? "true" : "false" );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001849 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001850
1851 if( crt->max_pathlen > 0 )
1852 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001853 ret = mbedtls_snprintf( p, n, ", max_pathlen=%d", crt->max_pathlen - 1 );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001854 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001855 }
1856 }
1857
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01001858 if( crt->ext_types & MBEDTLS_X509_EXT_SUBJECT_ALT_NAME )
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001859 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001860 ret = mbedtls_snprintf( p, n, "\n%ssubject alt name : ", prefix );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001861 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02001862
1863 if( ( ret = x509_info_subject_alt_name( &p, &n,
1864 &crt->subject_alt_names ) ) != 0 )
1865 return( ret );
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001866 }
1867
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001868 if( crt->ext_types & MBEDTLS_X509_EXT_NS_CERT_TYPE )
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001869 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001870 ret = mbedtls_snprintf( p, n, "\n%scert. type : ", prefix );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001871 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnard919f8f52014-04-01 13:01:11 +02001872
1873 if( ( ret = x509_info_cert_type( &p, &n, crt->ns_cert_type ) ) != 0 )
1874 return( ret );
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001875 }
1876
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001877 if( crt->ext_types & MBEDTLS_X509_EXT_KEY_USAGE )
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001878 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001879 ret = mbedtls_snprintf( p, n, "\n%skey usage : ", prefix );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001880 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnard65c2ddc2014-04-01 14:12:11 +02001881
1882 if( ( ret = x509_info_key_usage( &p, &n, crt->key_usage ) ) != 0 )
1883 return( ret );
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001884 }
1885
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001886 if( crt->ext_types & MBEDTLS_X509_EXT_EXTENDED_KEY_USAGE )
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001887 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001888 ret = mbedtls_snprintf( p, n, "\n%sext key usage : ", prefix );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001889 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02001890
1891 if( ( ret = x509_info_ext_key_usage( &p, &n,
1892 &crt->ext_key_usage ) ) != 0 )
1893 return( ret );
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001894 }
1895
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001896 ret = mbedtls_snprintf( p, n, "\n" );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001897 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001898
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001899 return( (int) ( size - n ) );
1900}
1901
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01001902struct x509_crt_verify_string {
1903 int code;
1904 const char *string;
1905};
1906
1907static const struct x509_crt_verify_string x509_crt_verify_strings[] = {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01001908 { MBEDTLS_X509_BADCERT_EXPIRED, "The certificate validity has expired" },
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01001909 { MBEDTLS_X509_BADCERT_REVOKED, "The certificate has been revoked (is on a CRL)" },
1910 { MBEDTLS_X509_BADCERT_CN_MISMATCH, "The certificate Common Name (CN) does not match with the expected CN" },
1911 { MBEDTLS_X509_BADCERT_NOT_TRUSTED, "The certificate is not correctly signed by the trusted CA" },
1912 { MBEDTLS_X509_BADCRL_NOT_TRUSTED, "The CRL is not correctly signed by the trusted CA" },
1913 { MBEDTLS_X509_BADCRL_EXPIRED, "The CRL is expired" },
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01001914 { MBEDTLS_X509_BADCERT_MISSING, "Certificate was missing" },
1915 { MBEDTLS_X509_BADCERT_SKIP_VERIFY, "Certificate verification was skipped" },
1916 { MBEDTLS_X509_BADCERT_OTHER, "Other reason (can be used by verify callback)" },
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01001917 { MBEDTLS_X509_BADCERT_FUTURE, "The certificate validity starts in the future" },
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01001918 { MBEDTLS_X509_BADCRL_FUTURE, "The CRL is from the future" },
1919 { MBEDTLS_X509_BADCERT_KEY_USAGE, "Usage does not match the keyUsage extension" },
1920 { MBEDTLS_X509_BADCERT_EXT_KEY_USAGE, "Usage does not match the extendedKeyUsage extension" },
1921 { MBEDTLS_X509_BADCERT_NS_CERT_TYPE, "Usage does not match the nsCertType extension" },
Manuel Pégourié-Gonnard95051642015-06-15 10:39:46 +02001922 { MBEDTLS_X509_BADCERT_BAD_MD, "The certificate is signed with an unacceptable hash." },
1923 { MBEDTLS_X509_BADCERT_BAD_PK, "The certificate is signed with an unacceptable PK alg (eg RSA vs ECDSA)." },
1924 { MBEDTLS_X509_BADCERT_BAD_KEY, "The certificate is signed with an unacceptable key (eg bad curve, RSA too short)." },
1925 { MBEDTLS_X509_BADCRL_BAD_MD, "The CRL is signed with an unacceptable hash." },
1926 { MBEDTLS_X509_BADCRL_BAD_PK, "The CRL is signed with an unacceptable PK alg (eg RSA vs ECDSA)." },
1927 { MBEDTLS_X509_BADCRL_BAD_KEY, "The CRL is signed with an unacceptable key (eg bad curve, RSA too short)." },
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01001928 { 0, NULL }
1929};
1930
1931int mbedtls_x509_crt_verify_info( char *buf, size_t size, const char *prefix,
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02001932 uint32_t flags )
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01001933{
1934 int ret;
1935 const struct x509_crt_verify_string *cur;
1936 char *p = buf;
1937 size_t n = size;
1938
1939 for( cur = x509_crt_verify_strings; cur->string != NULL ; cur++ )
1940 {
1941 if( ( flags & cur->code ) == 0 )
1942 continue;
1943
1944 ret = mbedtls_snprintf( p, n, "%s%s\n", prefix, cur->string );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001945 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01001946 flags ^= cur->code;
1947 }
1948
1949 if( flags != 0 )
1950 {
1951 ret = mbedtls_snprintf( p, n, "%sUnknown reason "
1952 "(this should not happen)\n", prefix );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001953 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01001954 }
1955
1956 return( (int) ( size - n ) );
1957}
Hanno Becker02a21932019-06-10 15:08:43 +01001958#endif /* !MBEDTLS_X509_REMOVE_INFO */
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01001959
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001960#if defined(MBEDTLS_X509_CHECK_KEY_USAGE)
Manuel Pégourié-Gonnard655a9642015-06-23 10:48:44 +02001961int mbedtls_x509_crt_check_key_usage( const mbedtls_x509_crt *crt,
1962 unsigned int usage )
Manuel Pégourié-Gonnard603116c2014-04-09 09:50:03 +02001963{
Manuel Pégourié-Gonnard655a9642015-06-23 10:48:44 +02001964 unsigned int usage_must, usage_may;
1965 unsigned int may_mask = MBEDTLS_X509_KU_ENCIPHER_ONLY
1966 | MBEDTLS_X509_KU_DECIPHER_ONLY;
1967
1968 if( ( crt->ext_types & MBEDTLS_X509_EXT_KEY_USAGE ) == 0 )
1969 return( 0 );
1970
1971 usage_must = usage & ~may_mask;
1972
1973 if( ( ( crt->key_usage & ~may_mask ) & usage_must ) != usage_must )
1974 return( MBEDTLS_ERR_X509_BAD_INPUT_DATA );
1975
1976 usage_may = usage & may_mask;
1977
1978 if( ( ( crt->key_usage & may_mask ) | usage_may ) != usage_may )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001979 return( MBEDTLS_ERR_X509_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard603116c2014-04-09 09:50:03 +02001980
1981 return( 0 );
1982}
1983#endif
1984
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001985#if defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE)
Hanno Beckerc7c638e2019-02-21 21:10:51 +00001986typedef struct
1987{
1988 const char *oid;
1989 size_t oid_len;
1990} x509_crt_check_ext_key_usage_cb_ctx_t;
1991
1992static int x509_crt_check_ext_key_usage_cb( void *ctx,
1993 int tag,
1994 unsigned char *data,
1995 size_t data_len )
1996{
1997 x509_crt_check_ext_key_usage_cb_ctx_t *cb_ctx =
1998 (x509_crt_check_ext_key_usage_cb_ctx_t *) ctx;
1999 ((void) tag);
2000
2001 if( MBEDTLS_OID_CMP_RAW( MBEDTLS_OID_ANY_EXTENDED_KEY_USAGE,
2002 data, data_len ) == 0 )
2003 {
2004 return( 1 );
2005 }
2006
2007 if( data_len == cb_ctx->oid_len && memcmp( data, cb_ctx->oid,
2008 data_len ) == 0 )
2009 {
2010 return( 1 );
2011 }
2012
2013 return( 0 );
2014}
2015
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002016int mbedtls_x509_crt_check_extended_key_usage( const mbedtls_x509_crt *crt,
Hanno Beckere1956af2019-02-21 14:28:12 +00002017 const char *usage_oid,
2018 size_t usage_len )
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02002019{
Hanno Beckere1956af2019-02-21 14:28:12 +00002020 int ret;
Hanno Beckere1956af2019-02-21 14:28:12 +00002021 unsigned ext_types;
2022 unsigned char *p, *end;
Hanno Beckerc7c638e2019-02-21 21:10:51 +00002023 x509_crt_check_ext_key_usage_cb_ctx_t cb_ctx = { usage_oid, usage_len };
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02002024
2025 /* Extension is not mandatory, absent means no restriction */
Hanno Beckere1956af2019-02-21 14:28:12 +00002026 ext_types = crt->ext_types;
2027 if( ( ext_types & MBEDTLS_X509_EXT_EXTENDED_KEY_USAGE ) == 0 )
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02002028 return( 0 );
2029
Hanno Beckere1956af2019-02-21 14:28:12 +00002030 p = crt->ext_key_usage_raw.p;
2031 end = p + crt->ext_key_usage_raw.len;
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02002032
Hanno Beckerc7c638e2019-02-21 21:10:51 +00002033 ret = mbedtls_asn1_traverse_sequence_of( &p, end,
2034 0xFF, MBEDTLS_ASN1_OID, 0, 0,
2035 x509_crt_check_ext_key_usage_cb,
2036 &cb_ctx );
2037 if( ret == 1 )
2038 return( 0 );
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02002039
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002040 return( MBEDTLS_ERR_X509_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02002041}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002042#endif /* MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE */
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02002043
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002044#if defined(MBEDTLS_X509_CRL_PARSE_C)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002045/*
2046 * Return 1 if the certificate is revoked, or 0 otherwise.
2047 */
Hanno Beckerc84fd1c2019-02-22 15:01:03 +00002048static int x509_serial_is_revoked( unsigned char const *serial,
2049 size_t serial_len,
2050 const mbedtls_x509_crl *crl )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002051{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002052 const mbedtls_x509_crl_entry *cur = &crl->entry;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002053
2054 while( cur != NULL && cur->serial.len != 0 )
2055 {
Hanno Beckerc84fd1c2019-02-22 15:01:03 +00002056 if( serial_len == cur->serial.len &&
2057 memcmp( serial, cur->serial.p, serial_len ) == 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002058 {
Manuel Pégourié-Gonnardc730ed32015-06-02 10:38:50 +01002059 if( mbedtls_x509_time_is_past( &cur->revocation_date ) )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002060 return( 1 );
2061 }
2062
2063 cur = cur->next;
2064 }
2065
2066 return( 0 );
2067}
2068
Hanno Beckerc84fd1c2019-02-22 15:01:03 +00002069int mbedtls_x509_crt_is_revoked( const mbedtls_x509_crt *crt,
2070 const mbedtls_x509_crl *crl )
2071{
2072 return( x509_serial_is_revoked( crt->serial.p,
2073 crt->serial.len,
2074 crl ) );
2075}
2076
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002077/*
Manuel Pégourié-Gonnardeeef9472016-02-22 11:36:55 +01002078 * Check that the given certificate is not revoked according to the CRL.
Manuel Pégourié-Gonnard08eacec2017-10-18 14:20:24 +02002079 * Skip validation if no CRL for the given CA is present.
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002080 */
Hanno Beckerc84fd1c2019-02-22 15:01:03 +00002081static int x509_crt_verifycrl( unsigned char *crt_serial,
2082 size_t crt_serial_len,
2083 mbedtls_x509_crt *ca,
Manuel Pégourié-Gonnard95051642015-06-15 10:39:46 +02002084 mbedtls_x509_crl *crl_list,
2085 const mbedtls_x509_crt_profile *profile )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002086{
2087 int flags = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002088 unsigned char hash[MBEDTLS_MD_MAX_SIZE];
2089 const mbedtls_md_info_t *md_info;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002090
2091 if( ca == NULL )
2092 return( flags );
2093
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002094 while( crl_list != NULL )
2095 {
2096 if( crl_list->version == 0 ||
Hanno Becker7dee12a2019-02-21 13:58:38 +00002097 mbedtls_x509_name_cmp_raw( &crl_list->issuer_raw_no_hdr,
Hanno Becker67284cc2019-02-21 14:31:51 +00002098 &ca->subject_raw_no_hdr,
2099 NULL, NULL ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002100 {
2101 crl_list = crl_list->next;
2102 continue;
2103 }
2104
2105 /*
Manuel Pégourié-Gonnard99d4f192014-04-08 15:10:07 +02002106 * Check if the CA is configured to sign CRLs
2107 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002108#if defined(MBEDTLS_X509_CHECK_KEY_USAGE)
Hanno Beckerb75ffb52018-11-02 09:19:54 +00002109 if( mbedtls_x509_crt_check_key_usage( ca,
2110 MBEDTLS_X509_KU_CRL_SIGN ) != 0 )
Manuel Pégourié-Gonnard99d4f192014-04-08 15:10:07 +02002111 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002112 flags |= MBEDTLS_X509_BADCRL_NOT_TRUSTED;
Manuel Pégourié-Gonnard99d4f192014-04-08 15:10:07 +02002113 break;
2114 }
2115#endif
2116
2117 /*
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002118 * Check if CRL is correctly signed by the trusted CA
2119 */
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +02002120 if( x509_profile_check_md_alg( profile, crl_list->sig_md ) != 0 )
2121 flags |= MBEDTLS_X509_BADCRL_BAD_MD;
2122
2123 if( x509_profile_check_pk_alg( profile, crl_list->sig_pk ) != 0 )
2124 flags |= MBEDTLS_X509_BADCRL_BAD_PK;
2125
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002126 md_info = mbedtls_md_info_from_type( crl_list->sig_md );
Manuel Pégourié-Gonnard329e78c2017-06-26 12:22:17 +02002127 if( mbedtls_md( md_info, crl_list->tbs.p, crl_list->tbs.len, hash ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002128 {
Manuel Pégourié-Gonnard329e78c2017-06-26 12:22:17 +02002129 /* Note: this can't happen except after an internal error */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002130 flags |= MBEDTLS_X509_BADCRL_NOT_TRUSTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002131 break;
2132 }
2133
Manuel Pégourié-Gonnard3f816912017-10-26 10:24:16 +02002134 if( x509_profile_check_key( profile, &ca->pk ) != 0 )
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +02002135 flags |= MBEDTLS_X509_BADCERT_BAD_KEY;
Manuel Pégourié-Gonnard95051642015-06-15 10:39:46 +02002136
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002137 if( mbedtls_pk_verify_ext( crl_list->sig_pk, crl_list->sig_opts, &ca->pk,
2138 crl_list->sig_md, hash, mbedtls_md_get_size( md_info ),
Manuel Pégourié-Gonnard53882022014-06-05 17:53:52 +02002139 crl_list->sig.p, crl_list->sig.len ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002140 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002141 flags |= MBEDTLS_X509_BADCRL_NOT_TRUSTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002142 break;
2143 }
2144
2145 /*
2146 * Check for validity of CRL (Do not drop out)
2147 */
Manuel Pégourié-Gonnardc730ed32015-06-02 10:38:50 +01002148 if( mbedtls_x509_time_is_past( &crl_list->next_update ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002149 flags |= MBEDTLS_X509_BADCRL_EXPIRED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002150
Manuel Pégourié-Gonnardc730ed32015-06-02 10:38:50 +01002151 if( mbedtls_x509_time_is_future( &crl_list->this_update ) )
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01002152 flags |= MBEDTLS_X509_BADCRL_FUTURE;
Manuel Pégourié-Gonnard95337652014-03-10 13:15:18 +01002153
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002154 /*
2155 * Check if certificate is revoked
2156 */
Hanno Beckerc84fd1c2019-02-22 15:01:03 +00002157 if( x509_serial_is_revoked( crt_serial, crt_serial_len,
2158 crl_list ) )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002159 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002160 flags |= MBEDTLS_X509_BADCERT_REVOKED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002161 break;
2162 }
2163
2164 crl_list = crl_list->next;
2165 }
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +02002166
Paul Bakkerd8bb8262014-06-17 14:06:49 +02002167 return( flags );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002168}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002169#endif /* MBEDTLS_X509_CRL_PARSE_C */
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002170
Manuel Pégourié-Gonnard88421242014-10-17 11:36:18 +02002171/*
Manuel Pégourié-Gonnardf82a4d52017-07-03 19:26:25 +02002172 * Check the signature of a certificate by its parent
2173 */
Hanno Becker5299cf82019-02-25 13:50:41 +00002174static int x509_crt_check_signature( const mbedtls_x509_crt_sig_info *sig_info,
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002175 mbedtls_x509_crt *parent,
2176 mbedtls_x509_crt_restart_ctx *rs_ctx )
Manuel Pégourié-Gonnardf82a4d52017-07-03 19:26:25 +02002177{
Hanno Beckere449e2d2019-02-25 14:45:31 +00002178 int ret;
2179 mbedtls_pk_context *pk;
Manuel Pégourié-Gonnardf82a4d52017-07-03 19:26:25 +02002180
Hanno Beckere449e2d2019-02-25 14:45:31 +00002181 ret = x509_crt_pk_acquire( parent, &pk );
2182 if( ret != 0 )
2183 return( MBEDTLS_ERR_X509_FATAL_ERROR );
2184
2185 /* Skip expensive computation on obvious mismatch */
2186 if( ! mbedtls_pk_can_do( pk, sig_info->sig_pk ) )
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002187 {
Hanno Beckere449e2d2019-02-25 14:45:31 +00002188 ret = -1;
2189 goto exit;
2190 }
2191
2192#if !( defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE) )
2193 ((void) rs_ctx);
2194#else
2195 if( rs_ctx != NULL && sig_info->sig_pk == MBEDTLS_PK_ECDSA )
2196 {
2197 ret = mbedtls_pk_verify_restartable( pk,
Hanno Becker5299cf82019-02-25 13:50:41 +00002198 sig_info->sig_md,
2199 sig_info->crt_hash, sig_info->crt_hash_len,
2200 sig_info->sig.p, sig_info->sig.len,
Hanno Beckere449e2d2019-02-25 14:45:31 +00002201 &rs_ctx->pk );
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002202 }
Hanno Beckere449e2d2019-02-25 14:45:31 +00002203 else
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002204#endif
Hanno Beckere449e2d2019-02-25 14:45:31 +00002205 {
2206 ret = mbedtls_pk_verify_ext( sig_info->sig_pk,
2207 sig_info->sig_opts,
2208 pk,
2209 sig_info->sig_md,
2210 sig_info->crt_hash, sig_info->crt_hash_len,
2211 sig_info->sig.p, sig_info->sig.len );
2212 }
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002213
Hanno Beckere449e2d2019-02-25 14:45:31 +00002214exit:
2215 x509_crt_pk_release( parent, pk );
2216 return( ret );
Manuel Pégourié-Gonnardf82a4d52017-07-03 19:26:25 +02002217}
2218
2219/*
Manuel Pégourié-Gonnard312010e2014-04-09 14:30:11 +02002220 * Check if 'parent' is a suitable parent (signing CA) for 'child'.
2221 * Return 0 if yes, -1 if not.
Manuel Pégourié-Gonnardd249b7a2014-06-24 11:49:16 +02002222 *
2223 * top means parent is a locally-trusted certificate
Manuel Pégourié-Gonnard3fed0b32014-04-08 13:18:01 +02002224 */
Hanno Becker43bf9002019-02-25 14:46:49 +00002225static int x509_crt_check_parent( const mbedtls_x509_crt_sig_info *sig_info,
2226 const mbedtls_x509_crt_frame *parent,
Manuel Pégourié-Gonnard27e94792017-07-04 00:49:31 +02002227 int top )
Manuel Pégourié-Gonnard3fed0b32014-04-08 13:18:01 +02002228{
Manuel Pégourié-Gonnardd249b7a2014-06-24 11:49:16 +02002229 int need_ca_bit;
2230
Manuel Pégourié-Gonnardc4eff162014-06-19 12:18:08 +02002231 /* Parent must be the issuer */
Hanno Becker43bf9002019-02-25 14:46:49 +00002232 if( mbedtls_x509_name_cmp_raw( &sig_info->issuer_raw,
2233 &parent->subject_raw,
Hanno Becker67284cc2019-02-21 14:31:51 +00002234 NULL, NULL ) != 0 )
Hanno Becker7dee12a2019-02-21 13:58:38 +00002235 {
Manuel Pégourié-Gonnard312010e2014-04-09 14:30:11 +02002236 return( -1 );
Hanno Becker7dee12a2019-02-21 13:58:38 +00002237 }
Manuel Pégourié-Gonnard3fed0b32014-04-08 13:18:01 +02002238
Manuel Pégourié-Gonnardd249b7a2014-06-24 11:49:16 +02002239 /* Parent must have the basicConstraints CA bit set as a general rule */
2240 need_ca_bit = 1;
2241
2242 /* Exception: v1/v2 certificates that are locally trusted. */
2243 if( top && parent->version < 3 )
2244 need_ca_bit = 0;
2245
Manuel Pégourié-Gonnardd249b7a2014-06-24 11:49:16 +02002246 if( need_ca_bit && ! parent->ca_istrue )
2247 return( -1 );
2248
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002249#if defined(MBEDTLS_X509_CHECK_KEY_USAGE)
Manuel Pégourié-Gonnardd249b7a2014-06-24 11:49:16 +02002250 if( need_ca_bit &&
Hanno Becker43bf9002019-02-25 14:46:49 +00002251 x509_crt_check_key_usage_frame( parent,
2252 MBEDTLS_X509_KU_KEY_CERT_SIGN ) != 0 )
Manuel Pégourié-Gonnardc4eff162014-06-19 12:18:08 +02002253 {
2254 return( -1 );
2255 }
Manuel Pégourié-Gonnard312010e2014-04-09 14:30:11 +02002256#endif
2257
2258 return( 0 );
Manuel Pégourié-Gonnard3fed0b32014-04-08 13:18:01 +02002259}
2260
Hanno Becker5299cf82019-02-25 13:50:41 +00002261typedef struct mbedtls_x509_crt_sig_info
2262{
2263 mbedtls_md_type_t sig_md;
2264 mbedtls_pk_type_t sig_pk;
2265 void *sig_opts;
2266 uint8_t crt_hash[MBEDTLS_MD_MAX_SIZE];
2267 size_t crt_hash_len;
2268 mbedtls_x509_buf_raw sig;
2269 mbedtls_x509_buf_raw issuer_raw;
2270} mbedtls_x509_crt_sig_info;
2271
2272static void x509_crt_free_sig_info( mbedtls_x509_crt_sig_info *info )
2273{
2274#if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT)
2275 mbedtls_free( info->sig_opts );
2276#else
2277 ((void) info);
2278#endif /* MBEDTLS_X509_RSASSA_PSS_SUPPORT */
2279}
2280
2281static int x509_crt_get_sig_info( mbedtls_x509_crt_frame const *frame,
2282 mbedtls_x509_crt_sig_info *info )
2283{
2284 const mbedtls_md_info_t *md_info;
2285
2286 md_info = mbedtls_md_info_from_type( frame->sig_md );
2287 if( mbedtls_md( md_info, frame->tbs.p, frame->tbs.len,
2288 info->crt_hash ) != 0 )
2289 {
2290 /* Note: this can't happen except after an internal error */
2291 return( -1 );
2292 }
2293
2294 info->crt_hash_len = mbedtls_md_get_size( md_info );
2295
2296 /* Make sure that this function leaves the target structure
2297 * ready to be freed, regardless of success of failure. */
2298 info->sig_opts = NULL;
2299
2300#if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT)
2301 {
2302 int ret;
2303 unsigned char *alg_start = frame->sig_alg.p;
2304 unsigned char *alg_end = alg_start + frame->sig_alg.len;
2305
2306 /* Get signature options -- currently only
2307 * necessary for RSASSA-PSS. */
2308 ret = mbedtls_x509_get_sig_alg_raw( &alg_start, alg_end, &info->sig_md,
2309 &info->sig_pk, &info->sig_opts );
2310 if( ret != 0 )
2311 {
2312 /* Note: this can't happen except after an internal error */
2313 return( -1 );
2314 }
2315 }
2316#else /* MBEDTLS_X509_RSASSA_PSS_SUPPORT */
2317 info->sig_md = frame->sig_md;
2318 info->sig_pk = frame->sig_pk;
2319#endif /* !MBEDTLS_X509_RSASSA_PSS_SUPPORT */
2320
2321 info->issuer_raw = frame->issuer_raw;
2322 info->sig = frame->sig;
2323 return( 0 );
2324}
2325
Manuel Pégourié-Gonnard35407c72017-06-29 10:45:25 +02002326/*
Manuel Pégourié-Gonnard3e329b82017-06-29 12:55:27 +02002327 * Find a suitable parent for child in candidates, or return NULL.
2328 *
2329 * Here suitable is defined as:
Manuel Pégourié-Gonnard2f09d592017-07-03 18:30:43 +02002330 * 1. subject name matches child's issuer
2331 * 2. if necessary, the CA bit is set and key usage allows signing certs
2332 * 3. for trusted roots, the signature is correct
Manuel Pégourié-Gonnardbe4ff422017-07-14 12:04:14 +02002333 * (for intermediates, the signature is checked and the result reported)
Manuel Pégourié-Gonnard2f09d592017-07-03 18:30:43 +02002334 * 4. pathlen constraints are satisfied
Manuel Pégourié-Gonnard3e329b82017-06-29 12:55:27 +02002335 *
Manuel Pégourié-Gonnard562df402017-08-08 18:09:14 +02002336 * If there's a suitable candidate which is also time-valid, return the first
2337 * such. Otherwise, return the first suitable candidate (or NULL if there is
2338 * none).
Manuel Pégourié-Gonnard3e329b82017-06-29 12:55:27 +02002339 *
2340 * The rationale for this rule is that someone could have a list of trusted
2341 * roots with two versions on the same root with different validity periods.
2342 * (At least one user reported having such a list and wanted it to just work.)
2343 * The reason we don't just require time-validity is that generally there is
2344 * only one version, and if it's expired we want the flags to state that
2345 * rather than NOT_TRUSTED, as would be the case if we required it here.
Manuel Pégourié-Gonnard2f09d592017-07-03 18:30:43 +02002346 *
2347 * The rationale for rule 3 (signature for trusted roots) is that users might
2348 * have two versions of the same CA with different keys in their list, and the
Manuel Pégourié-Gonnardbdc54402017-07-04 00:33:39 +02002349 * way we select the correct one is by checking the signature (as we don't
2350 * rely on key identifier extensions). (This is one way users might choose to
2351 * handle key rollover, another relies on self-issued certs, see [SIRO].)
Manuel Pégourié-Gonnard98a67782017-08-17 10:52:20 +02002352 *
2353 * Arguments:
Manuel Pégourié-Gonnarde57d7432018-03-07 10:00:57 +01002354 * - [in] child: certificate for which we're looking for a parent
2355 * - [in] candidates: chained list of potential parents
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +02002356 * - [out] r_parent: parent found (or NULL)
2357 * - [out] r_signature_is_good: 1 if child signature by parent is valid, or 0
Manuel Pégourié-Gonnarde57d7432018-03-07 10:00:57 +01002358 * - [in] top: 1 if candidates consists of trusted roots, ie we're at the top
2359 * of the chain, 0 otherwise
2360 * - [in] path_cnt: number of intermediates seen so far
2361 * - [in] self_cnt: number of self-signed intermediates seen so far
2362 * (will never be greater than path_cnt)
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +02002363 * - [in-out] rs_ctx: context for restarting operations
Manuel Pégourié-Gonnarde57d7432018-03-07 10:00:57 +01002364 *
2365 * Return value:
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +02002366 * - 0 on success
2367 * - MBEDTLS_ERR_ECP_IN_PROGRESS otherwise
Manuel Pégourié-Gonnard2f1c33d2017-06-29 12:27:23 +02002368 */
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002369static int x509_crt_find_parent_in(
Hanno Becker5299cf82019-02-25 13:50:41 +00002370 mbedtls_x509_crt_sig_info const *child_sig,
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002371 mbedtls_x509_crt *candidates,
2372 mbedtls_x509_crt **r_parent,
2373 int *r_signature_is_good,
2374 int top,
Manuel Pégourié-Gonnardbb216bd2017-08-28 13:25:55 +02002375 unsigned path_cnt,
2376 unsigned self_cnt,
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002377 mbedtls_x509_crt_restart_ctx *rs_ctx )
Manuel Pégourié-Gonnard2f1c33d2017-06-29 12:27:23 +02002378{
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002379 int ret;
Hanno Becker43bf9002019-02-25 14:46:49 +00002380 mbedtls_x509_crt *parent_crt, *fallback_parent;
Manuel Pégourié-Gonnard78d7e8c2018-07-02 12:33:14 +02002381 int signature_is_good, fallback_signature_is_good;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002382
2383#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard3627a8b2017-08-23 11:20:48 +02002384 /* did we have something in progress? */
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002385 if( rs_ctx != NULL && rs_ctx->parent != NULL )
2386 {
Manuel Pégourié-Gonnard3627a8b2017-08-23 11:20:48 +02002387 /* restore saved state */
Hanno Becker43bf9002019-02-25 14:46:49 +00002388 parent_crt = rs_ctx->parent;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002389 fallback_parent = rs_ctx->fallback_parent;
Manuel Pégourié-Gonnard78d7e8c2018-07-02 12:33:14 +02002390 fallback_signature_is_good = rs_ctx->fallback_signature_is_good;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002391
Manuel Pégourié-Gonnard3627a8b2017-08-23 11:20:48 +02002392 /* clear saved state */
2393 rs_ctx->parent = NULL;
2394 rs_ctx->fallback_parent = NULL;
Manuel Pégourié-Gonnard78d7e8c2018-07-02 12:33:14 +02002395 rs_ctx->fallback_signature_is_good = 0;
Manuel Pégourié-Gonnard3627a8b2017-08-23 11:20:48 +02002396
2397 /* resume where we left */
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002398 goto check_signature;
2399 }
2400#endif
2401
2402 fallback_parent = NULL;
Manuel Pégourié-Gonnard78d7e8c2018-07-02 12:33:14 +02002403 fallback_signature_is_good = 0;
Manuel Pégourié-Gonnard2f1c33d2017-06-29 12:27:23 +02002404
Hanno Becker43bf9002019-02-25 14:46:49 +00002405 for( parent_crt = candidates; parent_crt != NULL;
2406 parent_crt = parent_crt->next )
Manuel Pégourié-Gonnard2f1c33d2017-06-29 12:27:23 +02002407 {
Hanno Beckera788cab2019-02-24 17:47:46 +00002408 int parent_valid, parent_match, path_len_ok;
Manuel Pégourié-Gonnard2f1c33d2017-06-29 12:27:23 +02002409
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002410#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
2411check_signature:
2412#endif
Hanno Beckera788cab2019-02-24 17:47:46 +00002413
2414 parent_valid = parent_match = path_len_ok = 0;
Hanno Beckera788cab2019-02-24 17:47:46 +00002415 {
Hanno Becker43bf9002019-02-25 14:46:49 +00002416 mbedtls_x509_crt_frame *parent;
Hanno Beckera788cab2019-02-24 17:47:46 +00002417
Hanno Becker43bf9002019-02-25 14:46:49 +00002418 ret = x509_crt_frame_acquire( parent_crt, &parent );
2419 if( ret != 0 )
2420 return( MBEDTLS_ERR_X509_FATAL_ERROR );
Hanno Beckera788cab2019-02-24 17:47:46 +00002421
Hanno Becker43bf9002019-02-25 14:46:49 +00002422 if( mbedtls_x509_time_is_past( &parent->valid_from ) &&
2423 mbedtls_x509_time_is_future( &parent->valid_to ) )
2424 {
2425 parent_valid = 1;
2426 }
2427
2428 /* basic parenting skills (name, CA bit, key usage) */
2429 if( x509_crt_check_parent( child_sig, parent, top ) == 0 )
2430 parent_match = 1;
2431
2432 /* +1 because the stored max_pathlen is 1 higher
2433 * than the actual value */
2434 if( !( parent->max_pathlen > 0 &&
2435 (size_t) parent->max_pathlen < 1 + path_cnt - self_cnt ) )
2436 {
2437 path_len_ok = 1;
2438 }
2439
2440 x509_crt_frame_release( parent_crt, parent );
Hanno Beckera788cab2019-02-24 17:47:46 +00002441 }
2442
2443 if( parent_match == 0 || path_len_ok == 0 )
2444 continue;
2445
2446 /* Signature */
Hanno Becker43bf9002019-02-25 14:46:49 +00002447 ret = x509_crt_check_signature( child_sig, parent_crt, rs_ctx );
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002448
2449#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002450 if( rs_ctx != NULL && ret == MBEDTLS_ERR_ECP_IN_PROGRESS )
2451 {
2452 /* save state */
Hanno Becker43bf9002019-02-25 14:46:49 +00002453 rs_ctx->parent = parent_crt;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002454 rs_ctx->fallback_parent = fallback_parent;
Manuel Pégourié-Gonnard78d7e8c2018-07-02 12:33:14 +02002455 rs_ctx->fallback_signature_is_good = fallback_signature_is_good;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002456
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002457 return( ret );
2458 }
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002459#else
2460 (void) ret;
2461#endif
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002462
2463 signature_is_good = ret == 0;
2464 if( top && ! signature_is_good )
Manuel Pégourié-Gonnardf82a4d52017-07-03 19:26:25 +02002465 continue;
Manuel Pégourié-Gonnard2f09d592017-07-03 18:30:43 +02002466
Manuel Pégourié-Gonnard562df402017-08-08 18:09:14 +02002467 /* optional time check */
Hanno Beckera788cab2019-02-24 17:47:46 +00002468 if( !parent_valid )
Manuel Pégourié-Gonnard3e329b82017-06-29 12:55:27 +02002469 {
Manuel Pégourié-Gonnardbe4ff422017-07-14 12:04:14 +02002470 if( fallback_parent == NULL )
2471 {
Hanno Becker43bf9002019-02-25 14:46:49 +00002472 fallback_parent = parent_crt;
Manuel Pégourié-Gonnard78d7e8c2018-07-02 12:33:14 +02002473 fallback_signature_is_good = signature_is_good;
Manuel Pégourié-Gonnardbe4ff422017-07-14 12:04:14 +02002474 }
Manuel Pégourié-Gonnard3e329b82017-06-29 12:55:27 +02002475
2476 continue;
2477 }
2478
Manuel Pégourié-Gonnard2f1c33d2017-06-29 12:27:23 +02002479 break;
2480 }
2481
Hanno Becker43bf9002019-02-25 14:46:49 +00002482 if( parent_crt != NULL )
Manuel Pégourié-Gonnardbe4ff422017-07-14 12:04:14 +02002483 {
Hanno Becker43bf9002019-02-25 14:46:49 +00002484 *r_parent = parent_crt;
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002485 *r_signature_is_good = signature_is_good;
2486 }
2487 else
2488 {
2489 *r_parent = fallback_parent;
Manuel Pégourié-Gonnard78d7e8c2018-07-02 12:33:14 +02002490 *r_signature_is_good = fallback_signature_is_good;
Manuel Pégourié-Gonnardbe4ff422017-07-14 12:04:14 +02002491 }
Manuel Pégourié-Gonnard3e329b82017-06-29 12:55:27 +02002492
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002493 return( 0 );
Manuel Pégourié-Gonnard2f1c33d2017-06-29 12:27:23 +02002494}
2495
2496/*
Manuel Pégourié-Gonnard63686122017-07-04 01:01:39 +02002497 * Find a parent in trusted CAs or the provided chain, or return NULL.
2498 *
2499 * Searches in trusted CAs first, and return the first suitable parent found
2500 * (see find_parent_in() for definition of suitable).
Manuel Pégourié-Gonnard98a67782017-08-17 10:52:20 +02002501 *
2502 * Arguments:
Manuel Pégourié-Gonnarde57d7432018-03-07 10:00:57 +01002503 * - [in] child: certificate for which we're looking for a parent, followed
2504 * by a chain of possible intermediates
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +02002505 * - [in] trust_ca: list of locally trusted certificates
2506 * - [out] parent: parent found (or NULL)
2507 * - [out] parent_is_trusted: 1 if returned `parent` is trusted, or 0
2508 * - [out] signature_is_good: 1 if child signature by parent is valid, or 0
2509 * - [in] path_cnt: number of links in the chain so far (EE -> ... -> child)
2510 * - [in] self_cnt: number of self-signed certs in the chain so far
Manuel Pégourié-Gonnarde57d7432018-03-07 10:00:57 +01002511 * (will always be no greater than path_cnt)
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +02002512 * - [in-out] rs_ctx: context for restarting operations
Manuel Pégourié-Gonnarde57d7432018-03-07 10:00:57 +01002513 *
2514 * Return value:
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +02002515 * - 0 on success
2516 * - MBEDTLS_ERR_ECP_IN_PROGRESS otherwise
Manuel Pégourié-Gonnard63686122017-07-04 01:01:39 +02002517 */
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002518static int x509_crt_find_parent(
Hanno Becker5299cf82019-02-25 13:50:41 +00002519 mbedtls_x509_crt_sig_info const *child_sig,
Hanno Becker1e0677a2019-02-25 14:58:22 +00002520 mbedtls_x509_crt *rest,
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002521 mbedtls_x509_crt *trust_ca,
2522 mbedtls_x509_crt **parent,
2523 int *parent_is_trusted,
2524 int *signature_is_good,
Manuel Pégourié-Gonnardbb216bd2017-08-28 13:25:55 +02002525 unsigned path_cnt,
2526 unsigned self_cnt,
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002527 mbedtls_x509_crt_restart_ctx *rs_ctx )
Manuel Pégourié-Gonnard63686122017-07-04 01:01:39 +02002528{
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002529 int ret;
Manuel Pégourié-Gonnard18547b52017-08-14 16:11:43 +02002530 mbedtls_x509_crt *search_list;
Manuel Pégourié-Gonnard63686122017-07-04 01:01:39 +02002531
Manuel Pégourié-Gonnard63686122017-07-04 01:01:39 +02002532 *parent_is_trusted = 1;
Manuel Pégourié-Gonnard18547b52017-08-14 16:11:43 +02002533
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002534#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard3627a8b2017-08-23 11:20:48 +02002535 /* restore then clear saved state if we have some stored */
2536 if( rs_ctx != NULL && rs_ctx->parent_is_trusted != -1 )
2537 {
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002538 *parent_is_trusted = rs_ctx->parent_is_trusted;
Manuel Pégourié-Gonnard3627a8b2017-08-23 11:20:48 +02002539 rs_ctx->parent_is_trusted = -1;
2540 }
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002541#endif
2542
Manuel Pégourié-Gonnard18547b52017-08-14 16:11:43 +02002543 while( 1 ) {
Hanno Becker1e0677a2019-02-25 14:58:22 +00002544 search_list = *parent_is_trusted ? trust_ca : rest;
Manuel Pégourié-Gonnard18547b52017-08-14 16:11:43 +02002545
Hanno Becker5299cf82019-02-25 13:50:41 +00002546 ret = x509_crt_find_parent_in( child_sig, search_list,
Manuel Pégourié-Gonnard18547b52017-08-14 16:11:43 +02002547 parent, signature_is_good,
2548 *parent_is_trusted,
2549 path_cnt, self_cnt, rs_ctx );
Manuel Pégourié-Gonnard63686122017-07-04 01:01:39 +02002550
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002551#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002552 if( rs_ctx != NULL && ret == MBEDTLS_ERR_ECP_IN_PROGRESS )
2553 {
2554 /* save state */
2555 rs_ctx->parent_is_trusted = *parent_is_trusted;
Manuel Pégourié-Gonnard18547b52017-08-14 16:11:43 +02002556 return( ret );
2557 }
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002558#else
2559 (void) ret;
2560#endif
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002561
Manuel Pégourié-Gonnard18547b52017-08-14 16:11:43 +02002562 /* stop here if found or already in second iteration */
2563 if( *parent != NULL || *parent_is_trusted == 0 )
2564 break;
Manuel Pégourié-Gonnard63686122017-07-04 01:01:39 +02002565
Manuel Pégourié-Gonnard18547b52017-08-14 16:11:43 +02002566 /* prepare second iteration */
2567 *parent_is_trusted = 0;
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002568 }
Manuel Pégourié-Gonnard18547b52017-08-14 16:11:43 +02002569
2570 /* extra precaution against mistakes in the caller */
Krzysztof Stachowiakc388a8c2018-10-31 16:49:20 +01002571 if( *parent == NULL )
Manuel Pégourié-Gonnard18547b52017-08-14 16:11:43 +02002572 {
Manuel Pégourié-Gonnarda5a3e402018-10-16 11:27:23 +02002573 *parent_is_trusted = 0;
2574 *signature_is_good = 0;
Manuel Pégourié-Gonnard18547b52017-08-14 16:11:43 +02002575 }
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002576
2577 return( 0 );
Manuel Pégourié-Gonnard63686122017-07-04 01:01:39 +02002578}
2579
2580/*
Manuel Pégourié-Gonnard27e94792017-07-04 00:49:31 +02002581 * Check if an end-entity certificate is locally trusted
2582 *
2583 * Currently we require such certificates to be self-signed (actually only
2584 * check for self-issued as self-signatures are not checked)
2585 */
2586static int x509_crt_check_ee_locally_trusted(
Hanno Becker1e0677a2019-02-25 14:58:22 +00002587 mbedtls_x509_crt_frame const *crt,
2588 mbedtls_x509_crt const *trust_ca )
Manuel Pégourié-Gonnard27e94792017-07-04 00:49:31 +02002589{
Hanno Becker1e0677a2019-02-25 14:58:22 +00002590 mbedtls_x509_crt const *cur;
Manuel Pégourié-Gonnard27e94792017-07-04 00:49:31 +02002591
Manuel Pégourié-Gonnard27e94792017-07-04 00:49:31 +02002592 /* look for an exact match with trusted cert */
2593 for( cur = trust_ca; cur != NULL; cur = cur->next )
2594 {
2595 if( crt->raw.len == cur->raw.len &&
2596 memcmp( crt->raw.p, cur->raw.p, crt->raw.len ) == 0 )
2597 {
2598 return( 0 );
2599 }
2600 }
2601
2602 /* too bad */
2603 return( -1 );
2604}
2605
2606/*
Manuel Pégourié-Gonnardf86f4912017-07-05 16:43:44 +02002607 * Build and verify a certificate chain
Manuel Pégourié-Gonnard35407c72017-06-29 10:45:25 +02002608 *
Manuel Pégourié-Gonnardf86f4912017-07-05 16:43:44 +02002609 * Given a peer-provided list of certificates EE, C1, ..., Cn and
2610 * a list of trusted certs R1, ... Rp, try to build and verify a chain
Manuel Pégourié-Gonnard562df402017-08-08 18:09:14 +02002611 * EE, Ci1, ... Ciq [, Rj]
Manuel Pégourié-Gonnardf86f4912017-07-05 16:43:44 +02002612 * such that every cert in the chain is a child of the next one,
2613 * jumping to a trusted root as early as possible.
Manuel Pégourié-Gonnardbdc54402017-07-04 00:33:39 +02002614 *
Manuel Pégourié-Gonnardf86f4912017-07-05 16:43:44 +02002615 * Verify that chain and return it with flags for all issues found.
2616 *
2617 * Special cases:
2618 * - EE == Rj -> return a one-element list containing it
2619 * - EE, Ci1, ..., Ciq cannot be continued with a trusted root
2620 * -> return that chain with NOT_TRUSTED set on Ciq
Manuel Pégourié-Gonnardbdc54402017-07-04 00:33:39 +02002621 *
Manuel Pégourié-Gonnardd19a41d2017-07-14 11:05:59 +02002622 * Tests for (aspects of) this function should include at least:
2623 * - trusted EE
2624 * - EE -> trusted root
2625 * - EE -> intermedate CA -> trusted root
2626 * - if relevant: EE untrusted
2627 * - if relevant: EE -> intermediate, untrusted
2628 * with the aspect under test checked at each relevant level (EE, int, root).
2629 * For some aspects longer chains are required, but usually length 2 is
2630 * enough (but length 1 is not in general).
2631 *
Manuel Pégourié-Gonnardbdc54402017-07-04 00:33:39 +02002632 * Arguments:
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002633 * - [in] crt: the cert list EE, C1, ..., Cn
2634 * - [in] trust_ca: the trusted list R1, ..., Rp
2635 * - [in] ca_crl, profile: as in verify_with_profile()
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02002636 * - [out] ver_chain: the built and verified chain
Manuel Pégourié-Gonnarda9688432017-08-23 11:23:59 +02002637 * Only valid when return value is 0, may contain garbage otherwise!
2638 * Restart note: need not be the same when calling again to resume.
Manuel Pégourié-Gonnard98a67782017-08-17 10:52:20 +02002639 * - [in-out] rs_ctx: context for restarting operations
Manuel Pégourié-Gonnardf86f4912017-07-05 16:43:44 +02002640 *
2641 * Return value:
2642 * - non-zero if the chain could not be fully built and examined
2643 * - 0 is the chain was successfully built and examined,
2644 * even if it was found to be invalid
Manuel Pégourié-Gonnard35407c72017-06-29 10:45:25 +02002645 */
Manuel Pégourié-Gonnardbdc54402017-07-04 00:33:39 +02002646static int x509_crt_verify_chain(
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002647 mbedtls_x509_crt *crt,
2648 mbedtls_x509_crt *trust_ca,
2649 mbedtls_x509_crl *ca_crl,
Manuel Pégourié-Gonnard95051642015-06-15 10:39:46 +02002650 const mbedtls_x509_crt_profile *profile,
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02002651 mbedtls_x509_crt_verify_chain *ver_chain,
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002652 mbedtls_x509_crt_restart_ctx *rs_ctx )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002653{
Manuel Pégourié-Gonnarda9688432017-08-23 11:23:59 +02002654 /* Don't initialize any of those variables here, so that the compiler can
2655 * catch potential issues with jumping ahead when restarting */
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002656 int ret;
Manuel Pégourié-Gonnardf86f4912017-07-05 16:43:44 +02002657 uint32_t *flags;
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02002658 mbedtls_x509_crt_verify_chain_item *cur;
Hanno Becker1e0677a2019-02-25 14:58:22 +00002659 mbedtls_x509_crt *child_crt;
Manuel Pégourié-Gonnard58dcd2d2017-07-03 21:35:04 +02002660 mbedtls_x509_crt *parent;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002661 int parent_is_trusted;
2662 int child_is_trusted;
2663 int signature_is_good;
Manuel Pégourié-Gonnardbb216bd2017-08-28 13:25:55 +02002664 unsigned self_cnt;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002665
2666#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
2667 /* resume if we had an operation in progress */
Manuel Pégourié-Gonnarddaf04912017-08-23 12:32:19 +02002668 if( rs_ctx != NULL && rs_ctx->in_progress == x509_crt_rs_find_parent )
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002669 {
Manuel Pégourié-Gonnard3627a8b2017-08-23 11:20:48 +02002670 /* restore saved state */
Manuel Pégourié-Gonnarda9688432017-08-23 11:23:59 +02002671 *ver_chain = rs_ctx->ver_chain; /* struct copy */
Manuel Pégourié-Gonnarddaf04912017-08-23 12:32:19 +02002672 self_cnt = rs_ctx->self_cnt;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002673
Manuel Pégourié-Gonnarddaf04912017-08-23 12:32:19 +02002674 /* restore derived state */
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002675 cur = &ver_chain->items[ver_chain->len - 1];
Hanno Becker1e0677a2019-02-25 14:58:22 +00002676 child_crt = cur->crt;
2677
2678 child_is_trusted = 0;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002679 goto find_parent;
2680 }
2681#endif /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */
Manuel Pégourié-Gonnard8f8c2822017-07-03 21:25:10 +02002682
Hanno Becker1e0677a2019-02-25 14:58:22 +00002683 child_crt = crt;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002684 self_cnt = 0;
2685 parent_is_trusted = 0;
2686 child_is_trusted = 0;
Manuel Pégourié-Gonnardf86f4912017-07-05 16:43:44 +02002687
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002688 while( 1 ) {
Hanno Becker5299cf82019-02-25 13:50:41 +00002689#if defined(MBEDTLS_X509_CRL_PARSE_C)
2690 mbedtls_x509_buf_raw child_serial;
2691#endif /* MBEDTLS_X509_CRL_PARSE_C */
2692 int self_issued;
Hanno Becker1e0677a2019-02-25 14:58:22 +00002693
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002694 /* Add certificate to the verification chain */
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02002695 cur = &ver_chain->items[ver_chain->len];
Hanno Becker1e0677a2019-02-25 14:58:22 +00002696 cur->crt = child_crt;
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +02002697 cur->flags = 0;
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02002698 ver_chain->len++;
Hanno Becker10e6b9b2019-02-22 17:56:43 +00002699
2700#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
2701find_parent:
2702#endif
2703
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +02002704 flags = &cur->flags;
Manuel Pégourié-Gonnard66fac752017-07-03 21:39:21 +02002705
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002706 {
Hanno Becker5299cf82019-02-25 13:50:41 +00002707 mbedtls_x509_crt_sig_info child_sig;
2708 {
2709 mbedtls_x509_crt_frame *child;
Manuel Pégourié-Gonnard8f8c2822017-07-03 21:25:10 +02002710
Hanno Becker5299cf82019-02-25 13:50:41 +00002711 ret = x509_crt_frame_acquire( child_crt, &child );
2712 if( ret != 0 )
2713 return( MBEDTLS_ERR_X509_FATAL_ERROR );
2714
2715 /* Check time-validity (all certificates) */
2716 if( mbedtls_x509_time_is_past( &child->valid_to ) )
2717 *flags |= MBEDTLS_X509_BADCERT_EXPIRED;
2718 if( mbedtls_x509_time_is_future( &child->valid_from ) )
2719 *flags |= MBEDTLS_X509_BADCERT_FUTURE;
2720
2721 /* Stop here for trusted roots (but not for trusted EE certs) */
2722 if( child_is_trusted )
2723 {
2724 x509_crt_frame_release( child_crt, child );
2725 return( 0 );
2726 }
2727
2728 self_issued = 0;
2729 if( mbedtls_x509_name_cmp_raw( &child->issuer_raw,
2730 &child->subject_raw,
2731 NULL, NULL ) == 0 )
2732 {
2733 self_issued = 1;
2734 }
2735
2736 /* Check signature algorithm: MD & PK algs */
2737 if( x509_profile_check_md_alg( profile, child->sig_md ) != 0 )
2738 *flags |= MBEDTLS_X509_BADCERT_BAD_MD;
2739
2740 if( x509_profile_check_pk_alg( profile, child->sig_pk ) != 0 )
2741 *flags |= MBEDTLS_X509_BADCERT_BAD_PK;
2742
2743 /* Special case: EE certs that are locally trusted */
2744 if( ver_chain->len == 1 && self_issued &&
2745 x509_crt_check_ee_locally_trusted( child, trust_ca ) == 0 )
2746 {
2747 x509_crt_frame_release( child_crt, child );
2748 return( 0 );
2749 }
2750
2751#if defined(MBEDTLS_X509_CRL_PARSE_C)
2752 child_serial = child->serial;
2753#endif /* MBEDTLS_X509_CRL_PARSE_C */
2754
2755 ret = x509_crt_get_sig_info( child, &child_sig );
2756
2757 x509_crt_frame_release( child_crt, child );
2758 if( ret != 0 )
2759 return( MBEDTLS_ERR_X509_FATAL_ERROR );
2760 }
2761
2762 /* Look for a parent in trusted CAs or up the chain */
2763 ret = x509_crt_find_parent( &child_sig, child_crt->next,
2764 trust_ca, &parent,
2765 &parent_is_trusted, &signature_is_good,
2766 ver_chain->len - 1, self_cnt, rs_ctx );
2767
2768 x509_crt_free_sig_info( &child_sig );
2769 }
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002770
2771#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002772 if( rs_ctx != NULL && ret == MBEDTLS_ERR_ECP_IN_PROGRESS )
2773 {
2774 /* save state */
Manuel Pégourié-Gonnarddaf04912017-08-23 12:32:19 +02002775 rs_ctx->in_progress = x509_crt_rs_find_parent;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002776 rs_ctx->self_cnt = self_cnt;
Manuel Pégourié-Gonnarda9688432017-08-23 11:23:59 +02002777 rs_ctx->ver_chain = *ver_chain; /* struct copy */
Hanno Becker5299cf82019-02-25 13:50:41 +00002778 return( ret );
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002779 }
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002780#else
2781 (void) ret;
2782#endif
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002783
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002784 /* No parent? We're done here */
2785 if( parent == NULL )
2786 {
2787 *flags |= MBEDTLS_X509_BADCERT_NOT_TRUSTED;
Hanno Becker5299cf82019-02-25 13:50:41 +00002788 return( 0 );
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002789 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002790
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002791 /* Count intermediate self-issued (not necessarily self-signed) certs.
2792 * These can occur with some strategies for key rollover, see [SIRO],
2793 * and should be excluded from max_pathlen checks. */
Hanno Becker5299cf82019-02-25 13:50:41 +00002794 if( ver_chain->len != 1 && self_issued )
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002795 self_cnt++;
Manuel Pégourié-Gonnardfd6c85c2014-11-20 16:34:20 +01002796
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002797 /* path_cnt is 0 for the first intermediate CA,
2798 * and if parent is trusted it's not an intermediate CA */
2799 if( ! parent_is_trusted &&
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02002800 ver_chain->len > MBEDTLS_X509_MAX_INTERMEDIATE_CA )
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002801 {
2802 /* return immediately to avoid overflow the chain array */
Hanno Becker5299cf82019-02-25 13:50:41 +00002803 return( MBEDTLS_ERR_X509_FATAL_ERROR );
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002804 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002805
Manuel Pégourié-Gonnard98a67782017-08-17 10:52:20 +02002806 /* signature was checked while searching parent */
Manuel Pégourié-Gonnardbe4ff422017-07-14 12:04:14 +02002807 if( ! signature_is_good )
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002808 *flags |= MBEDTLS_X509_BADCERT_NOT_TRUSTED;
2809
2810 /* check size of signing key */
Manuel Pégourié-Gonnard3f816912017-10-26 10:24:16 +02002811 if( x509_profile_check_key( profile, &parent->pk ) != 0 )
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002812 *flags |= MBEDTLS_X509_BADCERT_BAD_KEY;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002813
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002814#if defined(MBEDTLS_X509_CRL_PARSE_C)
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002815 /* Check trusted CA's CRL for the given crt */
Hanno Becker5299cf82019-02-25 13:50:41 +00002816 *flags |= x509_crt_verifycrl( child_serial.p,
2817 child_serial.len,
Hanno Beckerc84fd1c2019-02-22 15:01:03 +00002818 parent, ca_crl, profile );
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002819#else
2820 (void) ca_crl;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002821#endif
2822
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002823 /* prepare for next iteration */
Hanno Becker1e0677a2019-02-25 14:58:22 +00002824 child_crt = parent;
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002825 parent = NULL;
2826 child_is_trusted = parent_is_trusted;
Manuel Pégourié-Gonnardbe4ff422017-07-14 12:04:14 +02002827 signature_is_good = 0;
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002828 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002829}
2830
2831/*
Manuel Pégourié-Gonnarda468eb12017-07-04 01:31:59 +02002832 * Check for CN match
2833 */
Hanno Becker24926222019-02-21 13:10:55 +00002834static int x509_crt_check_cn( unsigned char const *buf,
2835 size_t buflen,
2836 const char *cn,
2837 size_t cn_len )
Manuel Pégourié-Gonnarda468eb12017-07-04 01:31:59 +02002838{
Hanno Becker24926222019-02-21 13:10:55 +00002839 /* Try exact match */
Hanno Beckerb3def1d2019-02-22 11:46:06 +00002840 if( mbedtls_x509_memcasecmp( cn, buf, buflen, cn_len ) == 0 )
Manuel Pégourié-Gonnarda468eb12017-07-04 01:31:59 +02002841 return( 0 );
Manuel Pégourié-Gonnarda468eb12017-07-04 01:31:59 +02002842
2843 /* try wildcard match */
Hanno Becker24926222019-02-21 13:10:55 +00002844 if( x509_check_wildcard( cn, cn_len, buf, buflen ) == 0 )
Manuel Pégourié-Gonnarda468eb12017-07-04 01:31:59 +02002845 {
2846 return( 0 );
2847 }
2848
2849 return( -1 );
2850}
2851
Hanno Becker8b543b32019-02-21 11:50:44 +00002852/* Returns 1 on a match and 0 on a mismatch.
2853 * This is because this function is used as a callback for
2854 * mbedtls_x509_name_cmp_raw(), which continues the name
2855 * traversal as long as the callback returns 0. */
2856static int x509_crt_check_name( void *ctx,
2857 mbedtls_x509_buf *oid,
Hanno Becker6b378122019-02-23 10:20:14 +00002858 mbedtls_x509_buf *val,
2859 int next_merged )
Hanno Becker8b543b32019-02-21 11:50:44 +00002860{
2861 char const *cn = (char const*) ctx;
2862 size_t cn_len = strlen( cn );
Hanno Becker6b378122019-02-23 10:20:14 +00002863 ((void) next_merged);
Hanno Becker8b543b32019-02-21 11:50:44 +00002864
2865 if( MBEDTLS_OID_CMP( MBEDTLS_OID_AT_CN, oid ) == 0 &&
Hanno Becker24926222019-02-21 13:10:55 +00002866 x509_crt_check_cn( val->p, val->len, cn, cn_len ) == 0 )
Hanno Becker8b543b32019-02-21 11:50:44 +00002867 {
2868 return( 1 );
2869 }
2870
2871 return( 0 );
2872}
2873
Hanno Beckerda410822019-02-21 13:36:59 +00002874/* Returns 1 on a match and 0 on a mismatch.
2875 * This is because this function is used as a callback for
2876 * mbedtls_asn1_traverse_sequence_of(), which continues the
2877 * traversal as long as the callback returns 0. */
2878static int x509_crt_subject_alt_check_name( void *ctx,
2879 int tag,
2880 unsigned char *data,
2881 size_t data_len )
2882{
2883 char const *cn = (char const*) ctx;
2884 size_t cn_len = strlen( cn );
Hanno Becker90b94082019-02-21 21:13:21 +00002885 ((void) tag);
Hanno Beckerda410822019-02-21 13:36:59 +00002886
2887 if( x509_crt_check_cn( data, data_len, cn, cn_len ) == 0 )
2888 return( 1 );
2889
2890 return( 0 );
2891}
2892
Manuel Pégourié-Gonnarda468eb12017-07-04 01:31:59 +02002893/*
Manuel Pégourié-Gonnard1300e992017-07-04 01:13:44 +02002894 * Verify the requested CN - only call this if cn is not NULL!
2895 */
Manuel Pégourié-Gonnarda468eb12017-07-04 01:31:59 +02002896static void x509_crt_verify_name( const mbedtls_x509_crt *crt,
Manuel Pégourié-Gonnard1300e992017-07-04 01:13:44 +02002897 const char *cn,
2898 uint32_t *flags )
2899{
Hanno Beckerda410822019-02-21 13:36:59 +00002900 int ret;
Manuel Pégourié-Gonnard1300e992017-07-04 01:13:44 +02002901
2902 if( crt->ext_types & MBEDTLS_X509_EXT_SUBJECT_ALT_NAME )
2903 {
Hanno Becker90b94082019-02-21 21:13:21 +00002904 unsigned char *p =
2905 crt->subject_alt_raw.p;
Hanno Beckerda410822019-02-21 13:36:59 +00002906 const unsigned char *end =
2907 crt->subject_alt_raw.p + crt->subject_alt_raw.len;
Manuel Pégourié-Gonnard1300e992017-07-04 01:13:44 +02002908
Hanno Becker90b94082019-02-21 21:13:21 +00002909 ret = mbedtls_asn1_traverse_sequence_of( &p, end,
2910 MBEDTLS_ASN1_TAG_CLASS_MASK,
2911 MBEDTLS_ASN1_CONTEXT_SPECIFIC,
2912 MBEDTLS_ASN1_TAG_VALUE_MASK,
2913 2 /* SubjectAlt DNS */,
2914 x509_crt_subject_alt_check_name,
2915 (void*) cn );
Manuel Pégourié-Gonnard1300e992017-07-04 01:13:44 +02002916 }
2917 else
2918 {
Hanno Becker8b543b32019-02-21 11:50:44 +00002919 ret = mbedtls_x509_name_cmp_raw( &crt->subject_raw_no_hdr,
2920 &crt->subject_raw_no_hdr,
2921 x509_crt_check_name, (void*) cn );
Manuel Pégourié-Gonnard1300e992017-07-04 01:13:44 +02002922 }
Hanno Beckerda410822019-02-21 13:36:59 +00002923
2924 if( ret != 1 )
2925 *flags |= MBEDTLS_X509_BADCERT_CN_MISMATCH;
Manuel Pégourié-Gonnard1300e992017-07-04 01:13:44 +02002926}
2927
2928/*
Manuel Pégourié-Gonnarda707e1d2017-07-05 17:18:42 +02002929 * Merge the flags for all certs in the chain, after calling callback
2930 */
2931static int x509_crt_merge_flags_with_cb(
2932 uint32_t *flags,
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02002933 const mbedtls_x509_crt_verify_chain *ver_chain,
Manuel Pégourié-Gonnarda707e1d2017-07-05 17:18:42 +02002934 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
2935 void *p_vrfy )
2936{
2937 int ret;
Manuel Pégourié-Gonnardbb216bd2017-08-28 13:25:55 +02002938 unsigned i;
Manuel Pégourié-Gonnarda707e1d2017-07-05 17:18:42 +02002939 uint32_t cur_flags;
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02002940 const mbedtls_x509_crt_verify_chain_item *cur;
Manuel Pégourié-Gonnarda707e1d2017-07-05 17:18:42 +02002941
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02002942 for( i = ver_chain->len; i != 0; --i )
Manuel Pégourié-Gonnarda707e1d2017-07-05 17:18:42 +02002943 {
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02002944 cur = &ver_chain->items[i-1];
2945 cur_flags = cur->flags;
Manuel Pégourié-Gonnarda707e1d2017-07-05 17:18:42 +02002946
2947 if( NULL != f_vrfy )
Manuel Pégourié-Gonnardbb216bd2017-08-28 13:25:55 +02002948 if( ( ret = f_vrfy( p_vrfy, cur->crt, (int) i-1, &cur_flags ) ) != 0 )
Manuel Pégourié-Gonnarda707e1d2017-07-05 17:18:42 +02002949 return( ret );
2950
2951 *flags |= cur_flags;
2952 }
2953
2954 return( 0 );
2955}
2956
2957/*
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +02002958 * Verify the certificate validity (default profile, not restartable)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002959 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002960int mbedtls_x509_crt_verify( mbedtls_x509_crt *crt,
2961 mbedtls_x509_crt *trust_ca,
2962 mbedtls_x509_crl *ca_crl,
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02002963 const char *cn, uint32_t *flags,
2964 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
Paul Bakkerddf26b42013-09-18 13:46:23 +02002965 void *p_vrfy )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002966{
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +02002967 return( mbedtls_x509_crt_verify_restartable( crt, trust_ca, ca_crl,
2968 &mbedtls_x509_crt_profile_default, cn, flags,
2969 f_vrfy, p_vrfy, NULL ) );
Manuel Pégourié-Gonnard95051642015-06-15 10:39:46 +02002970}
2971
Manuel Pégourié-Gonnard95051642015-06-15 10:39:46 +02002972/*
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +02002973 * Verify the certificate validity (user-chosen profile, not restartable)
Manuel Pégourié-Gonnard95051642015-06-15 10:39:46 +02002974 */
2975int mbedtls_x509_crt_verify_with_profile( mbedtls_x509_crt *crt,
2976 mbedtls_x509_crt *trust_ca,
2977 mbedtls_x509_crl *ca_crl,
2978 const mbedtls_x509_crt_profile *profile,
2979 const char *cn, uint32_t *flags,
2980 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
2981 void *p_vrfy )
2982{
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +02002983 return( mbedtls_x509_crt_verify_restartable( crt, trust_ca, ca_crl,
2984 profile, cn, flags, f_vrfy, p_vrfy, NULL ) );
2985}
2986
2987/*
2988 * Verify the certificate validity, with profile, restartable version
2989 *
2990 * This function:
2991 * - checks the requested CN (if any)
2992 * - checks the type and size of the EE cert's key,
2993 * as that isn't done as part of chain building/verification currently
2994 * - builds and verifies the chain
2995 * - then calls the callback and merges the flags
2996 */
2997int mbedtls_x509_crt_verify_restartable( mbedtls_x509_crt *crt,
2998 mbedtls_x509_crt *trust_ca,
2999 mbedtls_x509_crl *ca_crl,
3000 const mbedtls_x509_crt_profile *profile,
3001 const char *cn, uint32_t *flags,
3002 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
3003 void *p_vrfy,
3004 mbedtls_x509_crt_restart_ctx *rs_ctx )
3005{
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003006 int ret;
Manuel Pégourié-Gonnard65eefc82015-10-23 14:08:48 +02003007 mbedtls_pk_type_t pk_type;
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02003008 mbedtls_x509_crt_verify_chain ver_chain;
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +02003009 uint32_t ee_flags;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003010
3011 *flags = 0;
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +02003012 ee_flags = 0;
3013 x509_crt_verify_chain_reset( &ver_chain );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003014
Manuel Pégourié-Gonnardd15795a2017-06-22 12:19:27 +02003015 if( profile == NULL )
3016 {
3017 ret = MBEDTLS_ERR_X509_BAD_INPUT_DATA;
3018 goto exit;
3019 }
3020
Manuel Pégourié-Gonnard1300e992017-07-04 01:13:44 +02003021 /* check name if requested */
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003022 if( cn != NULL )
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +02003023 x509_crt_verify_name( crt, cn, &ee_flags );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003024
Manuel Pégourié-Gonnard65eefc82015-10-23 14:08:48 +02003025 /* Check the type and size of the key */
3026 pk_type = mbedtls_pk_get_type( &crt->pk );
3027
3028 if( x509_profile_check_pk_alg( profile, pk_type ) != 0 )
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +02003029 ee_flags |= MBEDTLS_X509_BADCERT_BAD_PK;
Manuel Pégourié-Gonnard65eefc82015-10-23 14:08:48 +02003030
Manuel Pégourié-Gonnard3f816912017-10-26 10:24:16 +02003031 if( x509_profile_check_key( profile, &crt->pk ) != 0 )
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +02003032 ee_flags |= MBEDTLS_X509_BADCERT_BAD_KEY;
Manuel Pégourié-Gonnard65eefc82015-10-23 14:08:48 +02003033
Manuel Pégourié-Gonnardbdc54402017-07-04 00:33:39 +02003034 /* Check the chain */
Manuel Pégourié-Gonnard505c3952017-07-05 17:36:47 +02003035 ret = x509_crt_verify_chain( crt, trust_ca, ca_crl, profile,
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02003036 &ver_chain, rs_ctx );
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02003037
Manuel Pégourié-Gonnardc547d1a2017-07-05 13:28:45 +02003038 if( ret != 0 )
3039 goto exit;
3040
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +02003041 /* Merge end-entity flags */
3042 ver_chain.items[0].flags |= ee_flags;
3043
Manuel Pégourié-Gonnarda707e1d2017-07-05 17:18:42 +02003044 /* Build final flags, calling callback on the way if any */
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02003045 ret = x509_crt_merge_flags_with_cb( flags, &ver_chain, f_vrfy, p_vrfy );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003046
Manuel Pégourié-Gonnardd15795a2017-06-22 12:19:27 +02003047exit:
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02003048#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
3049 if( rs_ctx != NULL && ret != MBEDTLS_ERR_ECP_IN_PROGRESS )
3050 mbedtls_x509_crt_restart_free( rs_ctx );
3051#endif
3052
Manuel Pégourié-Gonnard9107b5f2017-07-06 12:16:25 +02003053 /* prevent misuse of the vrfy callback - VERIFY_FAILED would be ignored by
3054 * the SSL module for authmode optional, but non-zero return from the
3055 * callback means a fatal error so it shouldn't be ignored */
Manuel Pégourié-Gonnard31458a12017-06-26 10:11:49 +02003056 if( ret == MBEDTLS_ERR_X509_CERT_VERIFY_FAILED )
3057 ret = MBEDTLS_ERR_X509_FATAL_ERROR;
3058
Manuel Pégourié-Gonnardd15795a2017-06-22 12:19:27 +02003059 if( ret != 0 )
3060 {
3061 *flags = (uint32_t) -1;
3062 return( ret );
3063 }
3064
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003065 if( *flags != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003066 return( MBEDTLS_ERR_X509_CERT_VERIFY_FAILED );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003067
3068 return( 0 );
3069}
3070
3071/*
Paul Bakker369d2eb2013-09-18 11:58:25 +02003072 * Initialize a certificate chain
3073 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003074void mbedtls_x509_crt_init( mbedtls_x509_crt *crt )
Paul Bakker369d2eb2013-09-18 11:58:25 +02003075{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003076 memset( crt, 0, sizeof(mbedtls_x509_crt) );
Paul Bakker369d2eb2013-09-18 11:58:25 +02003077}
3078
3079/*
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003080 * Unallocate all certificate data
3081 */
Hanno Beckercd03bb22019-02-15 17:15:53 +00003082
3083static void x509_free_sequence( mbedtls_x509_sequence *seq )
3084{
3085 while( seq != NULL )
3086 {
3087 mbedtls_x509_sequence *next = seq->next;
3088 mbedtls_platform_zeroize( seq, sizeof( *seq ) );
3089 mbedtls_free( seq );
3090 seq = next;
3091 }
3092}
3093
3094static void x509_free_name( mbedtls_x509_name *name )
3095{
3096 while( name != NULL )
3097 {
3098 mbedtls_x509_name *next = name->next;
3099 mbedtls_platform_zeroize( name, sizeof( *name ) );
3100 mbedtls_free( name );
3101 name = next;
3102 }
3103}
3104
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003105void mbedtls_x509_crt_free( mbedtls_x509_crt *crt )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003106{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003107 mbedtls_x509_crt *cert_cur = crt;
3108 mbedtls_x509_crt *cert_prv;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003109
3110 if( crt == NULL )
3111 return;
3112
3113 do
3114 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003115 mbedtls_pk_free( &cert_cur->pk );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003116
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003117#if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT)
3118 mbedtls_free( cert_cur->sig_opts );
Manuel Pégourié-Gonnardf75f2f72014-06-05 15:14:28 +02003119#endif
3120
Hanno Beckercd03bb22019-02-15 17:15:53 +00003121 x509_free_name( cert_cur->issuer.next );
3122 x509_free_name( cert_cur->subject.next );
3123 x509_free_sequence( cert_cur->ext_key_usage.next );
3124 x509_free_sequence( cert_cur->subject_alt_names.next );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003125
Hanno Beckeraa8665a2019-01-31 08:57:44 +00003126 if( cert_cur->raw.p != NULL && cert_cur->own_buffer )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003127 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05003128 mbedtls_platform_zeroize( cert_cur->raw.p, cert_cur->raw.len );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003129 mbedtls_free( cert_cur->raw.p );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003130 }
3131
3132 cert_cur = cert_cur->next;
3133 }
3134 while( cert_cur != NULL );
3135
3136 cert_cur = crt;
3137 do
3138 {
3139 cert_prv = cert_cur;
3140 cert_cur = cert_cur->next;
3141
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05003142 mbedtls_platform_zeroize( cert_prv, sizeof( mbedtls_x509_crt ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003143 if( cert_prv != crt )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003144 mbedtls_free( cert_prv );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003145 }
3146 while( cert_cur != NULL );
3147}
3148
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +02003149#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
3150/*
3151 * Initialize a restart context
3152 */
3153void mbedtls_x509_crt_restart_init( mbedtls_x509_crt_restart_ctx *ctx )
3154{
Manuel Pégourié-Gonnard15d7df22017-08-17 14:33:31 +02003155 mbedtls_pk_restart_init( &ctx->pk );
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02003156
3157 ctx->parent = NULL;
3158 ctx->fallback_parent = NULL;
Manuel Pégourié-Gonnard78d7e8c2018-07-02 12:33:14 +02003159 ctx->fallback_signature_is_good = 0;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02003160
3161 ctx->parent_is_trusted = -1;
3162
Manuel Pégourié-Gonnarddaf04912017-08-23 12:32:19 +02003163 ctx->in_progress = x509_crt_rs_none;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02003164 ctx->self_cnt = 0;
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +02003165 x509_crt_verify_chain_reset( &ctx->ver_chain );
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +02003166}
3167
3168/*
3169 * Free the components of a restart context
3170 */
3171void mbedtls_x509_crt_restart_free( mbedtls_x509_crt_restart_ctx *ctx )
3172{
3173 if( ctx == NULL )
3174 return;
3175
Manuel Pégourié-Gonnard15d7df22017-08-17 14:33:31 +02003176 mbedtls_pk_restart_free( &ctx->pk );
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02003177 mbedtls_x509_crt_restart_init( ctx );
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +02003178}
3179#endif /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */
3180
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003181#endif /* MBEDTLS_X509_CRT_PARSE_C */