blob: e16ef0b7d9f6268654f3fa9dbaf4ebf47cbe7752 [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
Gilles Peskinedb09ef62020-06-03 01:43:33 +020034#include "common.h"
Paul Bakker7c6b2c32013-09-16 13:49:26 +020035
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020036#if defined(MBEDTLS_X509_CRT_PARSE_C)
Paul Bakker7c6b2c32013-09-16 13:49:26 +020037
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000038#include "mbedtls/x509_crt.h"
Janos Follath73c616b2019-12-18 15:07:04 +000039#include "mbedtls/error.h"
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000040#include "mbedtls/oid.h"
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050041#include "mbedtls/platform_util.h"
Rich Evans00ab4702015-02-06 13:43:58 +000042
Rich Evans00ab4702015-02-06 13:43:58 +000043#include <string.h>
44
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020045#if defined(MBEDTLS_PEM_PARSE_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000046#include "mbedtls/pem.h"
Paul Bakker7c6b2c32013-09-16 13:49:26 +020047#endif
48
Andrzej Kurekd4a65532018-10-31 06:18:39 -040049#if defined(MBEDTLS_USE_PSA_CRYPTO)
50#include "psa/crypto.h"
51#include "mbedtls/psa_util.h"
52#endif
53
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020054#if defined(MBEDTLS_PLATFORM_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000055#include "mbedtls/platform.h"
Paul Bakker7c6b2c32013-09-16 13:49:26 +020056#else
Simon Butcherd2642582018-10-03 15:11:19 +010057#include <stdio.h>
Rich Evans00ab4702015-02-06 13:43:58 +000058#include <stdlib.h>
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020059#define mbedtls_free free
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +020060#define mbedtls_calloc calloc
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020061#define mbedtls_snprintf snprintf
Paul Bakker7c6b2c32013-09-16 13:49:26 +020062#endif
63
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020064#if defined(MBEDTLS_THREADING_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000065#include "mbedtls/threading.h"
Manuel Pégourié-Gonnard5ad68e42013-11-28 17:11:54 +010066#endif
67
Paul Bakkerfa6a6202013-10-28 18:48:30 +010068#if defined(_WIN32) && !defined(EFIX64) && !defined(EFI32)
Paul Bakker7c6b2c32013-09-16 13:49:26 +020069#include <windows.h>
70#else
71#include <time.h>
72#endif
73
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020074#if defined(MBEDTLS_FS_IO)
Rich Evans00ab4702015-02-06 13:43:58 +000075#include <stdio.h>
Paul Bakker5ff3f912014-04-04 15:08:20 +020076#if !defined(_WIN32) || defined(EFIX64) || defined(EFI32)
Paul Bakker7c6b2c32013-09-16 13:49:26 +020077#include <sys/types.h>
78#include <sys/stat.h>
79#include <dirent.h>
Rich Evans00ab4702015-02-06 13:43:58 +000080#endif /* !_WIN32 || EFIX64 || EFI32 */
Paul Bakker7c6b2c32013-09-16 13:49:26 +020081#endif
82
Manuel Pégourié-Gonnardc547d1a2017-07-05 13:28:45 +020083/*
84 * Item in a verification chain: cert and flags for it
85 */
86typedef struct {
87 mbedtls_x509_crt *crt;
88 uint32_t flags;
89} x509_crt_verify_chain_item;
90
91/*
92 * Max size of verification chain: end-entity + intermediates + trusted root
93 */
94#define X509_MAX_VERIFY_CHAIN_SIZE ( MBEDTLS_X509_MAX_INTERMEDIATE_CA + 2 )
Paul Bakker34617722014-06-13 17:20:13 +020095
Paul Bakker7c6b2c32013-09-16 13:49:26 +020096/*
Manuel Pégourié-Gonnard88db5da2015-06-15 14:34:59 +020097 * Default profile
98 */
Manuel Pégourié-Gonnard88db5da2015-06-15 14:34:59 +020099const mbedtls_x509_crt_profile mbedtls_x509_crt_profile_default =
100{
Gilles Peskine5d2511c2017-05-12 13:16:40 +0200101#if defined(MBEDTLS_TLS_DEFAULT_ALLOW_SHA1_IN_CERTIFICATES)
Gilles Peskine5e79cb32017-05-04 16:17:21 +0200102 /* Allow SHA-1 (weak, but still safe in controlled environments) */
Manuel Pégourié-Gonnardf8ea8562015-06-15 15:33:19 +0200103 MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_SHA1 ) |
Gilles Peskine5e79cb32017-05-04 16:17:21 +0200104#endif
105 /* Only SHA-2 hashes */
Manuel Pégourié-Gonnardf8ea8562015-06-15 15:33:19 +0200106 MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_SHA224 ) |
107 MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_SHA256 ) |
108 MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_SHA384 ) |
109 MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_SHA512 ),
110 0xFFFFFFF, /* Any PK alg */
111 0xFFFFFFF, /* Any curve */
Manuel Pégourié-Gonnard88db5da2015-06-15 14:34:59 +0200112 2048,
113};
114
115/*
116 * Next-default profile
117 */
Manuel Pégourié-Gonnard88db5da2015-06-15 14:34:59 +0200118const mbedtls_x509_crt_profile mbedtls_x509_crt_profile_next =
119{
Manuel Pégourié-Gonnardf8ea8562015-06-15 15:33:19 +0200120 /* Hashes from SHA-256 and above */
121 MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_SHA256 ) |
122 MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_SHA384 ) |
123 MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_SHA512 ),
124 0xFFFFFFF, /* Any PK alg */
125#if defined(MBEDTLS_ECP_C)
126 /* Curves at or above 128-bit security level */
127 MBEDTLS_X509_ID_FLAG( MBEDTLS_ECP_DP_SECP256R1 ) |
128 MBEDTLS_X509_ID_FLAG( MBEDTLS_ECP_DP_SECP384R1 ) |
129 MBEDTLS_X509_ID_FLAG( MBEDTLS_ECP_DP_SECP521R1 ) |
130 MBEDTLS_X509_ID_FLAG( MBEDTLS_ECP_DP_BP256R1 ) |
131 MBEDTLS_X509_ID_FLAG( MBEDTLS_ECP_DP_BP384R1 ) |
132 MBEDTLS_X509_ID_FLAG( MBEDTLS_ECP_DP_BP512R1 ) |
133 MBEDTLS_X509_ID_FLAG( MBEDTLS_ECP_DP_SECP256K1 ),
134#else
135 0,
136#endif
Manuel Pégourié-Gonnard88db5da2015-06-15 14:34:59 +0200137 2048,
138};
139
140/*
141 * NSA Suite B Profile
142 */
Manuel Pégourié-Gonnard88db5da2015-06-15 14:34:59 +0200143const mbedtls_x509_crt_profile mbedtls_x509_crt_profile_suiteb =
144{
Manuel Pégourié-Gonnardf8ea8562015-06-15 15:33:19 +0200145 /* Only SHA-256 and 384 */
146 MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_SHA256 ) |
147 MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_SHA384 ),
148 /* Only ECDSA */
Ron Eldor85e1dcf2018-02-06 15:59:38 +0200149 MBEDTLS_X509_ID_FLAG( MBEDTLS_PK_ECDSA ) |
150 MBEDTLS_X509_ID_FLAG( MBEDTLS_PK_ECKEY ),
Manuel Pégourié-Gonnardf8ea8562015-06-15 15:33:19 +0200151#if defined(MBEDTLS_ECP_C)
152 /* Only NIST P-256 and P-384 */
153 MBEDTLS_X509_ID_FLAG( MBEDTLS_ECP_DP_SECP256R1 ) |
154 MBEDTLS_X509_ID_FLAG( MBEDTLS_ECP_DP_SECP384R1 ),
155#else
156 0,
157#endif
158 0,
Manuel Pégourié-Gonnard88db5da2015-06-15 14:34:59 +0200159};
160
161/*
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200162 * Check md_alg against profile
Manuel Pégourié-Gonnard3f816912017-10-26 10:24:16 +0200163 * Return 0 if md_alg is acceptable for this profile, -1 otherwise
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200164 */
165static int x509_profile_check_md_alg( const mbedtls_x509_crt_profile *profile,
166 mbedtls_md_type_t md_alg )
167{
Philippe Antoineb5b25432018-05-11 11:06:29 +0200168 if( md_alg == MBEDTLS_MD_NONE )
169 return( -1 );
170
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200171 if( ( profile->allowed_mds & MBEDTLS_X509_ID_FLAG( md_alg ) ) != 0 )
172 return( 0 );
173
174 return( -1 );
175}
176
177/*
178 * Check pk_alg against profile
Manuel Pégourié-Gonnard3f816912017-10-26 10:24:16 +0200179 * Return 0 if pk_alg is acceptable for this profile, -1 otherwise
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200180 */
181static int x509_profile_check_pk_alg( const mbedtls_x509_crt_profile *profile,
182 mbedtls_pk_type_t pk_alg )
183{
Philippe Antoineb5b25432018-05-11 11:06:29 +0200184 if( pk_alg == MBEDTLS_PK_NONE )
185 return( -1 );
186
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200187 if( ( profile->allowed_pks & MBEDTLS_X509_ID_FLAG( pk_alg ) ) != 0 )
188 return( 0 );
189
190 return( -1 );
191}
192
193/*
194 * Check key against profile
Manuel Pégourié-Gonnard3f816912017-10-26 10:24:16 +0200195 * Return 0 if pk is acceptable for this profile, -1 otherwise
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200196 */
197static int x509_profile_check_key( const mbedtls_x509_crt_profile *profile,
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200198 const mbedtls_pk_context *pk )
199{
Manuel Pégourié-Gonnard3f816912017-10-26 10:24:16 +0200200 const mbedtls_pk_type_t pk_alg = mbedtls_pk_get_type( pk );
Manuel Pégourié-Gonnard19773ff2017-10-24 10:51:26 +0200201
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200202#if defined(MBEDTLS_RSA_C)
203 if( pk_alg == MBEDTLS_PK_RSA || pk_alg == MBEDTLS_PK_RSASSA_PSS )
204 {
Manuel Pégourié-Gonnard097c7bb2015-06-18 16:43:38 +0200205 if( mbedtls_pk_get_bitlen( pk ) >= profile->rsa_min_bitlen )
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200206 return( 0 );
207
208 return( -1 );
209 }
210#endif
211
Manuel Pégourié-Gonnard65eefc82015-10-23 14:08:48 +0200212#if defined(MBEDTLS_ECP_C)
213 if( pk_alg == MBEDTLS_PK_ECDSA ||
214 pk_alg == MBEDTLS_PK_ECKEY ||
215 pk_alg == MBEDTLS_PK_ECKEY_DH )
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200216 {
Manuel Pégourié-Gonnard3f816912017-10-26 10:24:16 +0200217 const mbedtls_ecp_group_id gid = mbedtls_pk_ec( *pk )->grp.id;
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200218
Philippe Antoineb5b25432018-05-11 11:06:29 +0200219 if( gid == MBEDTLS_ECP_DP_NONE )
220 return( -1 );
221
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200222 if( ( profile->allowed_curves & MBEDTLS_X509_ID_FLAG( gid ) ) != 0 )
223 return( 0 );
224
225 return( -1 );
226 }
227#endif
228
229 return( -1 );
230}
231
232/*
Hanno Becker1f8527f2018-11-02 09:19:16 +0000233 * Like memcmp, but case-insensitive and always returns -1 if different
234 */
235static int x509_memcasecmp( const void *s1, const void *s2, size_t len )
236{
237 size_t i;
238 unsigned char diff;
239 const unsigned char *n1 = s1, *n2 = s2;
240
241 for( i = 0; i < len; i++ )
242 {
243 diff = n1[i] ^ n2[i];
244
245 if( diff == 0 )
246 continue;
247
248 if( diff == 32 &&
249 ( ( n1[i] >= 'a' && n1[i] <= 'z' ) ||
250 ( n1[i] >= 'A' && n1[i] <= 'Z' ) ) )
251 {
252 continue;
253 }
254
255 return( -1 );
256 }
257
258 return( 0 );
259}
260
261/*
262 * Return 0 if name matches wildcard, -1 otherwise
263 */
264static int x509_check_wildcard( const char *cn, const mbedtls_x509_buf *name )
265{
266 size_t i;
267 size_t cn_idx = 0, cn_len = strlen( cn );
268
269 /* We can't have a match if there is no wildcard to match */
270 if( name->len < 3 || name->p[0] != '*' || name->p[1] != '.' )
271 return( -1 );
272
273 for( i = 0; i < cn_len; ++i )
274 {
275 if( cn[i] == '.' )
276 {
277 cn_idx = i;
278 break;
279 }
280 }
281
282 if( cn_idx == 0 )
283 return( -1 );
284
285 if( cn_len - cn_idx == name->len - 1 &&
286 x509_memcasecmp( name->p + 1, cn + cn_idx, name->len - 1 ) == 0 )
287 {
288 return( 0 );
289 }
290
291 return( -1 );
292}
293
294/*
295 * Compare two X.509 strings, case-insensitive, and allowing for some encoding
296 * variations (but not all).
297 *
298 * Return 0 if equal, -1 otherwise.
299 */
300static int x509_string_cmp( const mbedtls_x509_buf *a, const mbedtls_x509_buf *b )
301{
302 if( a->tag == b->tag &&
303 a->len == b->len &&
304 memcmp( a->p, b->p, b->len ) == 0 )
305 {
306 return( 0 );
307 }
308
309 if( ( a->tag == MBEDTLS_ASN1_UTF8_STRING || a->tag == MBEDTLS_ASN1_PRINTABLE_STRING ) &&
310 ( b->tag == MBEDTLS_ASN1_UTF8_STRING || b->tag == MBEDTLS_ASN1_PRINTABLE_STRING ) &&
311 a->len == b->len &&
312 x509_memcasecmp( a->p, b->p, b->len ) == 0 )
313 {
314 return( 0 );
315 }
316
317 return( -1 );
318}
319
320/*
321 * Compare two X.509 Names (aka rdnSequence).
322 *
323 * See RFC 5280 section 7.1, though we don't implement the whole algorithm:
324 * we sometimes return unequal when the full algorithm would return equal,
325 * but never the other way. (In particular, we don't do Unicode normalisation
326 * or space folding.)
327 *
328 * Return 0 if equal, -1 otherwise.
329 */
330static int x509_name_cmp( const mbedtls_x509_name *a, const mbedtls_x509_name *b )
331{
332 /* Avoid recursion, it might not be optimised by the compiler */
333 while( a != NULL || b != NULL )
334 {
335 if( a == NULL || b == NULL )
336 return( -1 );
337
338 /* type */
339 if( a->oid.tag != b->oid.tag ||
340 a->oid.len != b->oid.len ||
341 memcmp( a->oid.p, b->oid.p, b->oid.len ) != 0 )
342 {
343 return( -1 );
344 }
345
346 /* value */
347 if( x509_string_cmp( &a->val, &b->val ) != 0 )
348 return( -1 );
349
350 /* structure of the list of sets */
351 if( a->next_merged != b->next_merged )
352 return( -1 );
353
354 a = a->next;
355 b = b->next;
356 }
357
358 /* a == NULL == b */
359 return( 0 );
360}
361
362/*
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +0200363 * Reset (init or clear) a verify_chain
364 */
365static void x509_crt_verify_chain_reset(
366 mbedtls_x509_crt_verify_chain *ver_chain )
367{
368 size_t i;
369
370 for( i = 0; i < MBEDTLS_X509_MAX_VERIFY_CHAIN_SIZE; i++ )
371 {
372 ver_chain->items[i].crt = NULL;
Hanno Beckera9375b32019-01-10 09:19:26 +0000373 ver_chain->items[i].flags = (uint32_t) -1;
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +0200374 }
375
376 ver_chain->len = 0;
Hanno Beckerf53893b2019-03-28 13:45:55 +0000377
378#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
379 ver_chain->trust_ca_cb_result = NULL;
380#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +0200381}
382
383/*
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200384 * Version ::= INTEGER { v1(0), v2(1), v3(2) }
385 */
386static int x509_get_version( unsigned char **p,
387 const unsigned char *end,
388 int *ver )
389{
Janos Follath865b3eb2019-12-16 11:46:15 +0000390 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200391 size_t len;
392
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200393 if( ( ret = mbedtls_asn1_get_tag( p, end, &len,
394 MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_CONSTRUCTED | 0 ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200395 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200396 if( ret == MBEDTLS_ERR_ASN1_UNEXPECTED_TAG )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200397 {
398 *ver = 0;
399 return( 0 );
400 }
401
Hanno Becker6ccfb182019-02-12 11:52:10 +0000402 return( MBEDTLS_ERR_X509_INVALID_FORMAT + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200403 }
404
405 end = *p + len;
406
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200407 if( ( ret = mbedtls_asn1_get_int( p, end, ver ) ) != 0 )
408 return( MBEDTLS_ERR_X509_INVALID_VERSION + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200409
410 if( *p != end )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200411 return( MBEDTLS_ERR_X509_INVALID_VERSION +
412 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200413
414 return( 0 );
415}
416
417/*
418 * Validity ::= SEQUENCE {
419 * notBefore Time,
420 * notAfter Time }
421 */
422static int x509_get_dates( unsigned char **p,
423 const unsigned char *end,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200424 mbedtls_x509_time *from,
425 mbedtls_x509_time *to )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200426{
Janos Follath865b3eb2019-12-16 11:46:15 +0000427 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200428 size_t len;
429
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200430 if( ( ret = mbedtls_asn1_get_tag( p, end, &len,
431 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
432 return( MBEDTLS_ERR_X509_INVALID_DATE + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200433
434 end = *p + len;
435
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200436 if( ( ret = mbedtls_x509_get_time( p, end, from ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200437 return( ret );
438
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200439 if( ( ret = mbedtls_x509_get_time( p, end, to ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200440 return( ret );
441
442 if( *p != end )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200443 return( MBEDTLS_ERR_X509_INVALID_DATE +
444 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200445
446 return( 0 );
447}
448
449/*
450 * X.509 v2/v3 unique identifier (not parsed)
451 */
452static int x509_get_uid( unsigned char **p,
453 const unsigned char *end,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200454 mbedtls_x509_buf *uid, int n )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200455{
Janos Follath865b3eb2019-12-16 11:46:15 +0000456 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200457
458 if( *p == end )
459 return( 0 );
460
461 uid->tag = **p;
462
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200463 if( ( ret = mbedtls_asn1_get_tag( p, end, &uid->len,
464 MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_CONSTRUCTED | n ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200465 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200466 if( ret == MBEDTLS_ERR_ASN1_UNEXPECTED_TAG )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200467 return( 0 );
468
Hanno Becker6ccfb182019-02-12 11:52:10 +0000469 return( MBEDTLS_ERR_X509_INVALID_FORMAT + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200470 }
471
472 uid->p = *p;
473 *p += uid->len;
474
475 return( 0 );
476}
477
478static int x509_get_basic_constraints( unsigned char **p,
479 const unsigned char *end,
480 int *ca_istrue,
481 int *max_pathlen )
482{
Janos Follath865b3eb2019-12-16 11:46:15 +0000483 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200484 size_t len;
485
486 /*
487 * BasicConstraints ::= SEQUENCE {
488 * cA BOOLEAN DEFAULT FALSE,
489 * pathLenConstraint INTEGER (0..MAX) OPTIONAL }
490 */
491 *ca_istrue = 0; /* DEFAULT FALSE */
492 *max_pathlen = 0; /* endless */
493
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200494 if( ( ret = mbedtls_asn1_get_tag( p, end, &len,
495 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
496 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200497
498 if( *p == end )
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200499 return( 0 );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200500
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200501 if( ( ret = mbedtls_asn1_get_bool( p, end, ca_istrue ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200502 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200503 if( ret == MBEDTLS_ERR_ASN1_UNEXPECTED_TAG )
504 ret = mbedtls_asn1_get_int( p, end, ca_istrue );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200505
506 if( ret != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200507 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200508
509 if( *ca_istrue != 0 )
510 *ca_istrue = 1;
511 }
512
513 if( *p == end )
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200514 return( 0 );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200515
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200516 if( ( ret = mbedtls_asn1_get_int( p, end, max_pathlen ) ) != 0 )
517 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200518
519 if( *p != end )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200520 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS +
521 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200522
Andrzej Kurek16050742020-04-14 09:49:52 -0400523 /* Do not accept max_pathlen equal to INT_MAX to avoid a signed integer
524 * overflow, which is an undefined behavior. */
525 if( *max_pathlen == INT_MAX )
526 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS +
527 MBEDTLS_ERR_ASN1_INVALID_LENGTH );
528
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200529 (*max_pathlen)++;
530
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200531 return( 0 );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200532}
533
534static int x509_get_ns_cert_type( unsigned char **p,
535 const unsigned char *end,
536 unsigned char *ns_cert_type)
537{
Janos Follath865b3eb2019-12-16 11:46:15 +0000538 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200539 mbedtls_x509_bitstring bs = { 0, 0, NULL };
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200540
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200541 if( ( ret = mbedtls_asn1_get_bitstring( p, end, &bs ) ) != 0 )
542 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200543
544 if( bs.len != 1 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200545 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS +
546 MBEDTLS_ERR_ASN1_INVALID_LENGTH );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200547
548 /* Get actual bitstring */
549 *ns_cert_type = *bs.p;
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200550 return( 0 );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200551}
552
553static int x509_get_key_usage( unsigned char **p,
554 const unsigned char *end,
Manuel Pégourié-Gonnard1d0ca1a2015-03-27 16:50:00 +0100555 unsigned int *key_usage)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200556{
Janos Follath865b3eb2019-12-16 11:46:15 +0000557 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard9a702252015-06-23 10:14:36 +0200558 size_t i;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200559 mbedtls_x509_bitstring bs = { 0, 0, NULL };
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200560
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200561 if( ( ret = mbedtls_asn1_get_bitstring( p, end, &bs ) ) != 0 )
562 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200563
564 if( bs.len < 1 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200565 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS +
566 MBEDTLS_ERR_ASN1_INVALID_LENGTH );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200567
568 /* Get actual bitstring */
Manuel Pégourié-Gonnard9a702252015-06-23 10:14:36 +0200569 *key_usage = 0;
570 for( i = 0; i < bs.len && i < sizeof( unsigned int ); i++ )
571 {
572 *key_usage |= (unsigned int) bs.p[i] << (8*i);
573 }
574
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200575 return( 0 );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200576}
577
578/*
579 * ExtKeyUsageSyntax ::= SEQUENCE SIZE (1..MAX) OF KeyPurposeId
580 *
581 * KeyPurposeId ::= OBJECT IDENTIFIER
582 */
583static int x509_get_ext_key_usage( unsigned char **p,
584 const unsigned char *end,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200585 mbedtls_x509_sequence *ext_key_usage)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200586{
Janos Follath865b3eb2019-12-16 11:46:15 +0000587 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200588
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200589 if( ( ret = mbedtls_asn1_get_sequence_of( p, end, ext_key_usage, MBEDTLS_ASN1_OID ) ) != 0 )
590 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200591
592 /* Sequence length must be >= 1 */
593 if( ext_key_usage->buf.p == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200594 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS +
595 MBEDTLS_ERR_ASN1_INVALID_LENGTH );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200596
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200597 return( 0 );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200598}
599
600/*
601 * SubjectAltName ::= GeneralNames
602 *
603 * GeneralNames ::= SEQUENCE SIZE (1..MAX) OF GeneralName
604 *
605 * GeneralName ::= CHOICE {
606 * otherName [0] OtherName,
607 * rfc822Name [1] IA5String,
608 * dNSName [2] IA5String,
609 * x400Address [3] ORAddress,
610 * directoryName [4] Name,
611 * ediPartyName [5] EDIPartyName,
612 * uniformResourceIdentifier [6] IA5String,
613 * iPAddress [7] OCTET STRING,
614 * registeredID [8] OBJECT IDENTIFIER }
615 *
616 * OtherName ::= SEQUENCE {
617 * type-id OBJECT IDENTIFIER,
618 * value [0] EXPLICIT ANY DEFINED BY type-id }
619 *
620 * EDIPartyName ::= SEQUENCE {
621 * nameAssigner [0] DirectoryString OPTIONAL,
622 * partyName [1] DirectoryString }
623 *
Ron Eldorc8b5f3f2019-05-15 15:15:55 +0300624 * NOTE: we list all types, but only use dNSName and otherName
625 * of type HwModuleName, as defined in RFC 4108, at this point.
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200626 */
627static int x509_get_subject_alt_name( unsigned char **p,
628 const unsigned char *end,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200629 mbedtls_x509_sequence *subject_alt_name )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200630{
Janos Follath865b3eb2019-12-16 11:46:15 +0000631 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200632 size_t len, tag_len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200633 mbedtls_asn1_buf *buf;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200634 unsigned char tag;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200635 mbedtls_asn1_sequence *cur = subject_alt_name;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200636
637 /* Get main sequence tag */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200638 if( ( ret = mbedtls_asn1_get_tag( p, end, &len,
639 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
640 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200641
642 if( *p + len != end )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200643 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS +
644 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200645
646 while( *p < end )
647 {
Ron Eldordbbd9662019-05-15 15:46:03 +0300648 mbedtls_x509_subject_alternative_name dummy_san_buf;
649 memset( &dummy_san_buf, 0, sizeof( dummy_san_buf ) );
650
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200651 tag = **p;
652 (*p)++;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200653 if( ( ret = mbedtls_asn1_get_len( p, end, &tag_len ) ) != 0 )
654 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200655
Andres Amaya Garcia849bc652017-08-25 17:13:12 +0100656 if( ( tag & MBEDTLS_ASN1_TAG_CLASS_MASK ) !=
657 MBEDTLS_ASN1_CONTEXT_SPECIFIC )
658 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200659 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS +
660 MBEDTLS_ERR_ASN1_UNEXPECTED_TAG );
Andres Amaya Garcia849bc652017-08-25 17:13:12 +0100661 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200662
Ron Eldordbbd9662019-05-15 15:46:03 +0300663 /*
irwir74aee1c2019-09-21 18:21:48 +0300664 * Check that the SAN is structured correctly.
Ron Eldordbbd9662019-05-15 15:46:03 +0300665 */
666 ret = mbedtls_x509_parse_subject_alt_name( &(cur->buf), &dummy_san_buf );
667 /*
668 * In case the extension is malformed, return an error,
669 * and clear the allocated sequences.
670 */
671 if( ret != 0 && ret != MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE )
672 {
673 mbedtls_x509_sequence *seq_cur = subject_alt_name->next;
674 mbedtls_x509_sequence *seq_prv;
675 while( seq_cur != NULL )
676 {
677 seq_prv = seq_cur;
678 seq_cur = seq_cur->next;
679 mbedtls_platform_zeroize( seq_prv,
680 sizeof( mbedtls_x509_sequence ) );
681 mbedtls_free( seq_prv );
682 }
Ron Eldor5aebeeb2019-05-22 16:41:21 +0300683 subject_alt_name->next = NULL;
Ron Eldordbbd9662019-05-15 15:46:03 +0300684 return( ret );
685 }
686
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200687 /* Allocate and assign next pointer */
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +0200688 if( cur->buf.p != NULL )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200689 {
Manuel Pégourié-Gonnardb1340602014-11-11 23:11:16 +0100690 if( cur->next != NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200691 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS );
Manuel Pégourié-Gonnardb1340602014-11-11 23:11:16 +0100692
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200693 cur->next = mbedtls_calloc( 1, sizeof( mbedtls_asn1_sequence ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200694
695 if( cur->next == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200696 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS +
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200697 MBEDTLS_ERR_ASN1_ALLOC_FAILED );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200698
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200699 cur = cur->next;
700 }
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +0200701
702 buf = &(cur->buf);
703 buf->tag = tag;
704 buf->p = *p;
705 buf->len = tag_len;
706 *p += buf->len;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200707 }
708
709 /* Set final sequence entry's next pointer to NULL */
710 cur->next = NULL;
711
712 if( *p != end )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200713 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS +
714 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200715
716 return( 0 );
717}
718
719/*
Ron Eldor74d9acc2019-03-21 14:00:03 +0200720 * id-ce-certificatePolicies OBJECT IDENTIFIER ::= { id-ce 32 }
721 *
722 * anyPolicy OBJECT IDENTIFIER ::= { id-ce-certificatePolicies 0 }
723 *
724 * certificatePolicies ::= SEQUENCE SIZE (1..MAX) OF PolicyInformation
725 *
726 * PolicyInformation ::= SEQUENCE {
727 * policyIdentifier CertPolicyId,
728 * policyQualifiers SEQUENCE SIZE (1..MAX) OF
729 * PolicyQualifierInfo OPTIONAL }
730 *
731 * CertPolicyId ::= OBJECT IDENTIFIER
732 *
733 * PolicyQualifierInfo ::= SEQUENCE {
734 * policyQualifierId PolicyQualifierId,
735 * qualifier ANY DEFINED BY policyQualifierId }
736 *
737 * -- policyQualifierIds for Internet policy qualifiers
738 *
739 * id-qt OBJECT IDENTIFIER ::= { id-pkix 2 }
740 * id-qt-cps OBJECT IDENTIFIER ::= { id-qt 1 }
741 * id-qt-unotice OBJECT IDENTIFIER ::= { id-qt 2 }
742 *
743 * PolicyQualifierId ::= OBJECT IDENTIFIER ( id-qt-cps | id-qt-unotice )
744 *
745 * Qualifier ::= CHOICE {
746 * cPSuri CPSuri,
747 * userNotice UserNotice }
748 *
749 * CPSuri ::= IA5String
750 *
751 * UserNotice ::= SEQUENCE {
752 * noticeRef NoticeReference OPTIONAL,
753 * explicitText DisplayText OPTIONAL }
754 *
755 * NoticeReference ::= SEQUENCE {
756 * organization DisplayText,
757 * noticeNumbers SEQUENCE OF INTEGER }
758 *
759 * DisplayText ::= CHOICE {
760 * ia5String IA5String (SIZE (1..200)),
761 * visibleString VisibleString (SIZE (1..200)),
762 * bmpString BMPString (SIZE (1..200)),
763 * utf8String UTF8String (SIZE (1..200)) }
764 *
765 * NOTE: we only parse and use anyPolicy without qualifiers at this point
766 * as defined in RFC 5280.
767 */
768static int x509_get_certificate_policies( unsigned char **p,
769 const unsigned char *end,
770 mbedtls_x509_sequence *certificate_policies )
771{
Ron Eldor8b0c3c92019-05-15 12:20:00 +0300772 int ret, parse_ret = 0;
Ron Eldor74d9acc2019-03-21 14:00:03 +0200773 size_t len;
774 mbedtls_asn1_buf *buf;
775 mbedtls_asn1_sequence *cur = certificate_policies;
776
777 /* Get main sequence tag */
778 ret = mbedtls_asn1_get_tag( p, end, &len,
779 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE );
780 if( ret != 0 )
781 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS + ret );
782
783 if( *p + len != end )
784 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS +
785 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
786
787 /*
788 * Cannot be an empty sequence.
789 */
790 if( len == 0 )
791 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS +
792 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
793
794 while( *p < end )
795 {
796 mbedtls_x509_buf policy_oid;
797 const unsigned char *policy_end;
798
799 /*
800 * Get the policy sequence
801 */
802 if( ( ret = mbedtls_asn1_get_tag( p, end, &len,
803 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
804 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS + ret );
805
806 policy_end = *p + len;
807
Ron Eldor08063792019-05-13 16:38:39 +0300808 if( ( ret = mbedtls_asn1_get_tag( p, policy_end, &len,
Ron Eldor74d9acc2019-03-21 14:00:03 +0200809 MBEDTLS_ASN1_OID ) ) != 0 )
810 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS + ret );
811
812 policy_oid.tag = MBEDTLS_ASN1_OID;
813 policy_oid.len = len;
814 policy_oid.p = *p;
815
Ron Eldor8b0c3c92019-05-15 12:20:00 +0300816 /*
817 * Only AnyPolicy is currently supported when enforcing policy.
818 */
819 if( MBEDTLS_OID_CMP( MBEDTLS_OID_ANY_POLICY, &policy_oid ) != 0 )
820 {
821 /*
822 * Set the parsing return code but continue parsing, in case this
823 * extension is critical and MBEDTLS_X509_ALLOW_UNSUPPORTED_CRITICAL_EXTENSION
824 * is configured.
825 */
826 parse_ret = MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE;
827 }
828
Ron Eldor74d9acc2019-03-21 14:00:03 +0200829 /* Allocate and assign next pointer */
830 if( cur->buf.p != NULL )
831 {
832 if( cur->next != NULL )
833 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS );
834
835 cur->next = mbedtls_calloc( 1, sizeof( mbedtls_asn1_sequence ) );
836
837 if( cur->next == NULL )
838 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS +
839 MBEDTLS_ERR_ASN1_ALLOC_FAILED );
840
841 cur = cur->next;
842 }
843
844 buf = &( cur->buf );
845 buf->tag = policy_oid.tag;
846 buf->p = policy_oid.p;
847 buf->len = policy_oid.len;
Ron Eldor08063792019-05-13 16:38:39 +0300848
849 *p += len;
850
851 /*
852 * If there is an optional qualifier, then *p < policy_end
853 * Check the Qualifier len to verify it doesn't exceed policy_end.
854 */
855 if( *p < policy_end )
856 {
857 if( ( ret = mbedtls_asn1_get_tag( p, policy_end, &len,
858 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
859 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS + ret );
860 /*
861 * Skip the optional policy qualifiers.
862 */
863 *p += len;
864 }
865
866 if( *p != policy_end )
867 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS +
868 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
Ron Eldor74d9acc2019-03-21 14:00:03 +0200869 }
870
871 /* Set final sequence entry's next pointer to NULL */
872 cur->next = NULL;
873
874 if( *p != end )
875 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS +
876 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
877
Ron Eldor8b0c3c92019-05-15 12:20:00 +0300878 return( parse_ret );
Ron Eldor74d9acc2019-03-21 14:00:03 +0200879}
880
881/*
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200882 * X.509 v3 extensions
883 *
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200884 */
885static int x509_get_crt_ext( unsigned char **p,
886 const unsigned char *end,
Nicola Di Lieto502d4b42020-04-25 14:41:25 +0200887 mbedtls_x509_crt *crt,
Nicola Di Lieto5659e7e2020-05-28 23:41:38 +0200888 mbedtls_x509_crt_ext_cb_t cb,
889 void *p_ctx )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200890{
Janos Follath865b3eb2019-12-16 11:46:15 +0000891 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200892 size_t len;
Nicola Di Lietoc84b1e62020-06-13 11:08:16 +0200893 unsigned char *end_ext_data, *start_ext_octet, *end_ext_octet;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200894
Hanno Becker12f62fb2019-02-12 17:22:36 +0000895 if( *p == end )
896 return( 0 );
897
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200898 if( ( ret = mbedtls_x509_get_ext( p, end, &crt->v3_ext, 3 ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200899 return( ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200900
Hanno Becker12f62fb2019-02-12 17:22:36 +0000901 end = crt->v3_ext.p + crt->v3_ext.len;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200902 while( *p < end )
903 {
904 /*
905 * Extension ::= SEQUENCE {
906 * extnID OBJECT IDENTIFIER,
907 * critical BOOLEAN DEFAULT FALSE,
908 * extnValue OCTET STRING }
909 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200910 mbedtls_x509_buf extn_oid = {0, 0, NULL};
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200911 int is_critical = 0; /* DEFAULT FALSE */
912 int ext_type = 0;
913
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200914 if( ( ret = mbedtls_asn1_get_tag( p, end, &len,
915 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
916 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200917
918 end_ext_data = *p + len;
919
920 /* Get extension ID */
k-stachowiakdcae78a2018-06-28 16:32:54 +0200921 if( ( ret = mbedtls_asn1_get_tag( p, end_ext_data, &extn_oid.len,
k-stachowiak463928a2018-07-24 12:50:59 +0200922 MBEDTLS_ASN1_OID ) ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200923 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200924
k-stachowiak463928a2018-07-24 12:50:59 +0200925 extn_oid.tag = MBEDTLS_ASN1_OID;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200926 extn_oid.p = *p;
927 *p += extn_oid.len;
928
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200929 /* Get optional critical */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200930 if( ( ret = mbedtls_asn1_get_bool( p, end_ext_data, &is_critical ) ) != 0 &&
931 ( ret != MBEDTLS_ERR_ASN1_UNEXPECTED_TAG ) )
932 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200933
934 /* Data should be octet string type */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200935 if( ( ret = mbedtls_asn1_get_tag( p, end_ext_data, &len,
936 MBEDTLS_ASN1_OCTET_STRING ) ) != 0 )
937 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200938
Nicola Di Lietoc84b1e62020-06-13 11:08:16 +0200939 start_ext_octet = *p;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200940 end_ext_octet = *p + len;
941
942 if( end_ext_octet != end_ext_data )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200943 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS +
944 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200945
946 /*
947 * Detect supported extensions
948 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200949 ret = mbedtls_oid_get_x509_ext_type( &extn_oid, &ext_type );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200950
951 if( ret != 0 )
952 {
Nicola Di Lieto502d4b42020-04-25 14:41:25 +0200953 /* Give the callback (if any) a chance to handle the extension */
Nicola Di Lieto2c3a9172020-05-28 17:20:42 +0200954 if( cb != NULL )
955 {
Nicola Di Lieto5659e7e2020-05-28 23:41:38 +0200956 ret = cb( p_ctx, crt, &extn_oid, is_critical, *p, end_ext_octet );
Nicola Di Lieto565b52b2020-05-29 22:46:56 +0200957 if( ret != 0 && is_critical )
958 return( ret );
Nicola Di Lietofae25a12020-05-28 08:55:08 +0200959 *p = end_ext_octet;
Nicola Di Lieto502d4b42020-04-25 14:41:25 +0200960 continue;
Nicola Di Lietofae25a12020-05-28 08:55:08 +0200961 }
Nicola Di Lieto502d4b42020-04-25 14:41:25 +0200962
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200963 /* No parser found, skip extension */
964 *p = end_ext_octet;
965
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200966#if !defined(MBEDTLS_X509_ALLOW_UNSUPPORTED_CRITICAL_EXTENSION)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200967 if( is_critical )
968 {
969 /* Data is marked as critical: fail */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200970 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS +
971 MBEDTLS_ERR_ASN1_UNEXPECTED_TAG );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200972 }
973#endif
974 continue;
975 }
976
Manuel Pégourié-Gonnard8a5e3d42014-11-12 17:47:28 +0100977 /* Forbid repeated extensions */
978 if( ( crt->ext_types & ext_type ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200979 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS );
Manuel Pégourié-Gonnard8a5e3d42014-11-12 17:47:28 +0100980
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200981 crt->ext_types |= ext_type;
982
983 switch( ext_type )
984 {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +0100985 case MBEDTLS_X509_EXT_BASIC_CONSTRAINTS:
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200986 /* Parse basic constraints */
987 if( ( ret = x509_get_basic_constraints( p, end_ext_octet,
988 &crt->ca_istrue, &crt->max_pathlen ) ) != 0 )
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200989 return( ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200990 break;
991
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200992 case MBEDTLS_X509_EXT_KEY_USAGE:
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200993 /* Parse key usage */
994 if( ( ret = x509_get_key_usage( p, end_ext_octet,
995 &crt->key_usage ) ) != 0 )
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200996 return( ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200997 break;
998
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200999 case MBEDTLS_X509_EXT_EXTENDED_KEY_USAGE:
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001000 /* Parse extended key usage */
1001 if( ( ret = x509_get_ext_key_usage( p, end_ext_octet,
1002 &crt->ext_key_usage ) ) != 0 )
Paul Bakkerd8bb8262014-06-17 14:06:49 +02001003 return( ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001004 break;
1005
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01001006 case MBEDTLS_X509_EXT_SUBJECT_ALT_NAME:
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001007 /* Parse subject alt name */
1008 if( ( ret = x509_get_subject_alt_name( p, end_ext_octet,
1009 &crt->subject_alt_names ) ) != 0 )
Paul Bakkerd8bb8262014-06-17 14:06:49 +02001010 return( ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001011 break;
1012
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001013 case MBEDTLS_X509_EXT_NS_CERT_TYPE:
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001014 /* Parse netscape certificate type */
1015 if( ( ret = x509_get_ns_cert_type( p, end_ext_octet,
1016 &crt->ns_cert_type ) ) != 0 )
Paul Bakkerd8bb8262014-06-17 14:06:49 +02001017 return( ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001018 break;
1019
Ron Eldor74d9acc2019-03-21 14:00:03 +02001020 case MBEDTLS_OID_X509_EXT_CERTIFICATE_POLICIES:
1021 /* Parse certificate policies type */
1022 if( ( ret = x509_get_certificate_policies( p, end_ext_octet,
1023 &crt->certificate_policies ) ) != 0 )
Ron Eldor8b0c3c92019-05-15 12:20:00 +03001024 {
Nicola Di Lietoc84b1e62020-06-13 11:08:16 +02001025 /* Give the callback (if any) a chance to handle the extension
1026 * if it contains unsupported policies */
1027 if( ret == MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE && cb != NULL &&
1028 cb( p_ctx, crt, &extn_oid, is_critical,
1029 start_ext_octet, end_ext_octet ) == 0 )
1030 break;
1031
Ron Eldor8b0c3c92019-05-15 12:20:00 +03001032#if !defined(MBEDTLS_X509_ALLOW_UNSUPPORTED_CRITICAL_EXTENSION)
1033 if( is_critical )
1034 return( ret );
1035 else
1036#endif
1037 /*
Ron Eldora2913912019-05-16 16:17:38 +03001038 * If MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE is returned, then we
1039 * cannot interpret or enforce the policy. However, it is up to
1040 * the user to choose how to enforce the policies,
Ron Eldor8b0c3c92019-05-15 12:20:00 +03001041 * unless the extension is critical.
1042 */
1043 if( ret != MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE )
1044 return( ret );
1045 }
Ron Eldor74d9acc2019-03-21 14:00:03 +02001046 break;
1047
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001048 default:
Ron Eldordf48efa2019-04-08 13:28:24 +03001049 /*
1050 * If this is a non-critical extension, which the oid layer
1051 * supports, but there isn't an x509 parser for it,
1052 * skip the extension.
1053 */
1054#if !defined(MBEDTLS_X509_ALLOW_UNSUPPORTED_CRITICAL_EXTENSION)
1055 if( is_critical )
1056 return( MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE );
1057 else
1058#endif
1059 *p = end_ext_octet;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001060 }
1061 }
1062
1063 if( *p != end )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001064 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS +
1065 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001066
1067 return( 0 );
1068}
1069
1070/*
1071 * Parse and fill a single X.509 certificate in DER format
1072 */
Hanno Becker1a65dcd2019-01-31 08:57:44 +00001073static int x509_crt_parse_der_core( mbedtls_x509_crt *crt,
1074 const unsigned char *buf,
1075 size_t buflen,
Nicola Di Lieto502d4b42020-04-25 14:41:25 +02001076 int make_copy,
Nicola Di Lieto5659e7e2020-05-28 23:41:38 +02001077 mbedtls_x509_crt_ext_cb_t cb,
1078 void *p_ctx )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001079{
Janos Follath865b3eb2019-12-16 11:46:15 +00001080 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001081 size_t len;
1082 unsigned char *p, *end, *crt_end;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001083 mbedtls_x509_buf sig_params1, sig_params2, sig_oid2;
Manuel Pégourié-Gonnard59a75d52014-01-22 10:12:57 +01001084
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001085 memset( &sig_params1, 0, sizeof( mbedtls_x509_buf ) );
1086 memset( &sig_params2, 0, sizeof( mbedtls_x509_buf ) );
1087 memset( &sig_oid2, 0, sizeof( mbedtls_x509_buf ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001088
1089 /*
1090 * Check for valid input
1091 */
1092 if( crt == NULL || buf == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001093 return( MBEDTLS_ERR_X509_BAD_INPUT_DATA );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001094
Hanno Becker1a65dcd2019-01-31 08:57:44 +00001095 /* Use the original buffer until we figure out actual length. */
Janos Follathcc0e49d2016-02-17 14:34:12 +00001096 p = (unsigned char*) buf;
1097 len = buflen;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001098 end = p + len;
1099
1100 /*
1101 * Certificate ::= SEQUENCE {
1102 * tbsCertificate TBSCertificate,
1103 * signatureAlgorithm AlgorithmIdentifier,
1104 * signatureValue BIT STRING }
1105 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001106 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
1107 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001108 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001109 mbedtls_x509_crt_free( crt );
1110 return( MBEDTLS_ERR_X509_INVALID_FORMAT );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001111 }
1112
Janos Follathcc0e49d2016-02-17 14:34:12 +00001113 end = crt_end = p + len;
Hanno Becker1a65dcd2019-01-31 08:57:44 +00001114 crt->raw.len = crt_end - buf;
1115 if( make_copy != 0 )
1116 {
1117 /* Create and populate a new buffer for the raw field. */
1118 crt->raw.p = p = mbedtls_calloc( 1, crt->raw.len );
1119 if( crt->raw.p == NULL )
1120 return( MBEDTLS_ERR_X509_ALLOC_FAILED );
1121
1122 memcpy( crt->raw.p, buf, crt->raw.len );
1123 crt->own_buffer = 1;
1124
1125 p += crt->raw.len - len;
1126 end = crt_end = p + len;
1127 }
1128 else
1129 {
1130 crt->raw.p = (unsigned char*) buf;
1131 crt->own_buffer = 0;
1132 }
Janos Follathcc0e49d2016-02-17 14:34:12 +00001133
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001134 /*
1135 * TBSCertificate ::= SEQUENCE {
1136 */
1137 crt->tbs.p = p;
1138
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001139 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
1140 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001141 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001142 mbedtls_x509_crt_free( crt );
1143 return( MBEDTLS_ERR_X509_INVALID_FORMAT + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001144 }
1145
1146 end = p + len;
1147 crt->tbs.len = end - crt->tbs.p;
1148
1149 /*
1150 * Version ::= INTEGER { v1(0), v2(1), v3(2) }
1151 *
1152 * CertificateSerialNumber ::= INTEGER
1153 *
1154 * signature AlgorithmIdentifier
1155 */
1156 if( ( ret = x509_get_version( &p, end, &crt->version ) ) != 0 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001157 ( ret = mbedtls_x509_get_serial( &p, end, &crt->serial ) ) != 0 ||
1158 ( ret = mbedtls_x509_get_alg( &p, end, &crt->sig_oid,
Manuel Pégourié-Gonnarddddbb1d2014-06-05 17:02:24 +02001159 &sig_params1 ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001160 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001161 mbedtls_x509_crt_free( crt );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001162 return( ret );
1163 }
1164
Andres AG7ca4a032017-03-09 16:16:11 +00001165 if( crt->version < 0 || crt->version > 2 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001166 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001167 mbedtls_x509_crt_free( crt );
1168 return( MBEDTLS_ERR_X509_UNKNOWN_VERSION );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001169 }
1170
Andres AG7ca4a032017-03-09 16:16:11 +00001171 crt->version++;
1172
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001173 if( ( ret = mbedtls_x509_get_sig_alg( &crt->sig_oid, &sig_params1,
Manuel Pégourié-Gonnardf75f2f72014-06-05 15:14:28 +02001174 &crt->sig_md, &crt->sig_pk,
1175 &crt->sig_opts ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001176 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001177 mbedtls_x509_crt_free( crt );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001178 return( ret );
1179 }
1180
1181 /*
1182 * issuer Name
1183 */
1184 crt->issuer_raw.p = p;
1185
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001186 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
1187 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001188 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001189 mbedtls_x509_crt_free( crt );
1190 return( MBEDTLS_ERR_X509_INVALID_FORMAT + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001191 }
1192
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001193 if( ( ret = mbedtls_x509_get_name( &p, p + len, &crt->issuer ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001194 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001195 mbedtls_x509_crt_free( crt );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001196 return( ret );
1197 }
1198
1199 crt->issuer_raw.len = p - crt->issuer_raw.p;
1200
1201 /*
1202 * Validity ::= SEQUENCE {
1203 * notBefore Time,
1204 * notAfter Time }
1205 *
1206 */
1207 if( ( ret = x509_get_dates( &p, end, &crt->valid_from,
1208 &crt->valid_to ) ) != 0 )
1209 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001210 mbedtls_x509_crt_free( crt );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001211 return( ret );
1212 }
1213
1214 /*
1215 * subject Name
1216 */
1217 crt->subject_raw.p = p;
1218
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001219 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
1220 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001221 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001222 mbedtls_x509_crt_free( crt );
1223 return( MBEDTLS_ERR_X509_INVALID_FORMAT + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001224 }
1225
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001226 if( len && ( ret = mbedtls_x509_get_name( &p, p + len, &crt->subject ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001227 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001228 mbedtls_x509_crt_free( crt );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001229 return( ret );
1230 }
1231
1232 crt->subject_raw.len = p - crt->subject_raw.p;
1233
1234 /*
1235 * SubjectPublicKeyInfo
1236 */
Hanno Becker494dd7a2019-02-06 16:13:41 +00001237 crt->pk_raw.p = p;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001238 if( ( ret = mbedtls_pk_parse_subpubkey( &p, end, &crt->pk ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001239 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001240 mbedtls_x509_crt_free( crt );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001241 return( ret );
1242 }
Hanno Becker494dd7a2019-02-06 16:13:41 +00001243 crt->pk_raw.len = p - crt->pk_raw.p;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001244
1245 /*
1246 * issuerUniqueID [1] IMPLICIT UniqueIdentifier OPTIONAL,
1247 * -- If present, version shall be v2 or v3
1248 * subjectUniqueID [2] IMPLICIT UniqueIdentifier OPTIONAL,
1249 * -- If present, version shall be v2 or v3
1250 * extensions [3] EXPLICIT Extensions OPTIONAL
1251 * -- If present, version shall be v3
1252 */
1253 if( crt->version == 2 || crt->version == 3 )
1254 {
1255 ret = x509_get_uid( &p, end, &crt->issuer_id, 1 );
1256 if( ret != 0 )
1257 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001258 mbedtls_x509_crt_free( crt );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001259 return( ret );
1260 }
1261 }
1262
1263 if( crt->version == 2 || crt->version == 3 )
1264 {
1265 ret = x509_get_uid( &p, end, &crt->subject_id, 2 );
1266 if( ret != 0 )
1267 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001268 mbedtls_x509_crt_free( crt );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001269 return( ret );
1270 }
1271 }
1272
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001273#if !defined(MBEDTLS_X509_ALLOW_EXTENSIONS_NON_V3)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001274 if( crt->version == 3 )
Paul Bakkerc27c4e22013-09-23 15:01:36 +02001275#endif
Manuel Pégourié-Gonnarda252af72015-03-27 16:15:55 +01001276 {
Nicola Di Lieto5659e7e2020-05-28 23:41:38 +02001277 ret = x509_get_crt_ext( &p, end, crt, cb, p_ctx );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001278 if( ret != 0 )
1279 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001280 mbedtls_x509_crt_free( crt );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001281 return( ret );
1282 }
1283 }
1284
1285 if( p != end )
1286 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001287 mbedtls_x509_crt_free( crt );
1288 return( MBEDTLS_ERR_X509_INVALID_FORMAT +
1289 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001290 }
1291
1292 end = crt_end;
1293
1294 /*
1295 * }
1296 * -- end of TBSCertificate
1297 *
1298 * signatureAlgorithm AlgorithmIdentifier,
1299 * signatureValue BIT STRING
1300 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001301 if( ( ret = mbedtls_x509_get_alg( &p, end, &sig_oid2, &sig_params2 ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001302 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001303 mbedtls_x509_crt_free( crt );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001304 return( ret );
1305 }
1306
Manuel Pégourié-Gonnard1022fed2015-03-27 16:30:47 +01001307 if( crt->sig_oid.len != sig_oid2.len ||
1308 memcmp( crt->sig_oid.p, sig_oid2.p, crt->sig_oid.len ) != 0 ||
Manuel Pégourié-Gonnarddddbb1d2014-06-05 17:02:24 +02001309 sig_params1.len != sig_params2.len ||
Manuel Pégourié-Gonnard159c5242015-04-30 11:15:22 +02001310 ( sig_params1.len != 0 &&
1311 memcmp( sig_params1.p, sig_params2.p, sig_params1.len ) != 0 ) )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001312 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001313 mbedtls_x509_crt_free( crt );
1314 return( MBEDTLS_ERR_X509_SIG_MISMATCH );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001315 }
1316
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001317 if( ( ret = mbedtls_x509_get_sig( &p, end, &crt->sig ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001318 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001319 mbedtls_x509_crt_free( crt );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001320 return( ret );
1321 }
1322
1323 if( p != end )
1324 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001325 mbedtls_x509_crt_free( crt );
1326 return( MBEDTLS_ERR_X509_INVALID_FORMAT +
1327 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001328 }
1329
1330 return( 0 );
1331}
1332
1333/*
1334 * Parse one X.509 certificate in DER format from a buffer and add them to a
1335 * chained list
1336 */
Hanno Becker1a65dcd2019-01-31 08:57:44 +00001337static int mbedtls_x509_crt_parse_der_internal( mbedtls_x509_crt *chain,
1338 const unsigned char *buf,
1339 size_t buflen,
Nicola Di Lieto502d4b42020-04-25 14:41:25 +02001340 int make_copy,
Nicola Di Lieto5659e7e2020-05-28 23:41:38 +02001341 mbedtls_x509_crt_ext_cb_t cb,
1342 void *p_ctx )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001343{
Janos Follath865b3eb2019-12-16 11:46:15 +00001344 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001345 mbedtls_x509_crt *crt = chain, *prev = NULL;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001346
1347 /*
1348 * Check for valid input
1349 */
1350 if( crt == NULL || buf == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001351 return( MBEDTLS_ERR_X509_BAD_INPUT_DATA );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001352
1353 while( crt->version != 0 && crt->next != NULL )
1354 {
1355 prev = crt;
1356 crt = crt->next;
1357 }
1358
1359 /*
1360 * Add new certificate on the end of the chain if needed.
1361 */
Paul Bakker66d5d072014-06-17 16:39:18 +02001362 if( crt->version != 0 && crt->next == NULL )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001363 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02001364 crt->next = mbedtls_calloc( 1, sizeof( mbedtls_x509_crt ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001365
1366 if( crt->next == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02001367 return( MBEDTLS_ERR_X509_ALLOC_FAILED );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001368
1369 prev = crt;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001370 mbedtls_x509_crt_init( crt->next );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001371 crt = crt->next;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001372 }
1373
Nicola Di Lieto5659e7e2020-05-28 23:41:38 +02001374 ret = x509_crt_parse_der_core( crt, buf, buflen, make_copy, cb, p_ctx );
Nicola Di Lieto502d4b42020-04-25 14:41:25 +02001375 if( ret != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001376 {
1377 if( prev )
1378 prev->next = NULL;
1379
1380 if( crt != chain )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001381 mbedtls_free( crt );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001382
1383 return( ret );
1384 }
1385
1386 return( 0 );
1387}
1388
Hanno Becker1a65dcd2019-01-31 08:57:44 +00001389int mbedtls_x509_crt_parse_der_nocopy( mbedtls_x509_crt *chain,
1390 const unsigned char *buf,
1391 size_t buflen )
1392{
Nicola Di Lieto5659e7e2020-05-28 23:41:38 +02001393 return( mbedtls_x509_crt_parse_der_internal( chain, buf, buflen, 0, NULL, NULL ) );
Nicola Di Lieto502d4b42020-04-25 14:41:25 +02001394}
1395
Nicola Di Lietofde98f72020-05-28 08:34:33 +02001396int mbedtls_x509_crt_parse_der_with_ext_cb( mbedtls_x509_crt *chain,
1397 const unsigned char *buf,
1398 size_t buflen,
Nicola Di Lieto4dbe5672020-05-28 09:18:42 +02001399 int make_copy,
Nicola Di Lieto5659e7e2020-05-28 23:41:38 +02001400 mbedtls_x509_crt_ext_cb_t cb,
1401 void *p_ctx )
Nicola Di Lieto502d4b42020-04-25 14:41:25 +02001402{
Nicola Di Lieto5659e7e2020-05-28 23:41:38 +02001403 return( mbedtls_x509_crt_parse_der_internal( chain, buf, buflen, make_copy, cb, p_ctx ) );
Hanno Becker1a65dcd2019-01-31 08:57:44 +00001404}
1405
1406int mbedtls_x509_crt_parse_der( mbedtls_x509_crt *chain,
1407 const unsigned char *buf,
1408 size_t buflen )
1409{
Nicola Di Lieto5659e7e2020-05-28 23:41:38 +02001410 return( mbedtls_x509_crt_parse_der_internal( chain, buf, buflen, 1, NULL, NULL ) );
Hanno Becker1a65dcd2019-01-31 08:57:44 +00001411}
1412
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001413/*
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001414 * Parse one or more PEM certificates from a buffer and add them to the chained
1415 * list
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001416 */
Hanno Becker1a65dcd2019-01-31 08:57:44 +00001417int mbedtls_x509_crt_parse( mbedtls_x509_crt *chain,
1418 const unsigned char *buf,
1419 size_t buflen )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001420{
Janos Follath98e28a72016-05-31 14:03:54 +01001421#if defined(MBEDTLS_PEM_PARSE_C)
Andres AGc0db5112016-12-07 15:05:53 +00001422 int success = 0, first_error = 0, total_failed = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001423 int buf_format = MBEDTLS_X509_FORMAT_DER;
Janos Follath98e28a72016-05-31 14:03:54 +01001424#endif
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001425
1426 /*
1427 * Check for valid input
1428 */
1429 if( chain == NULL || buf == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001430 return( MBEDTLS_ERR_X509_BAD_INPUT_DATA );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001431
1432 /*
1433 * Determine buffer content. Buffer contains either one DER certificate or
1434 * one or more PEM certificates.
1435 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001436#if defined(MBEDTLS_PEM_PARSE_C)
Manuel Pégourié-Gonnard0ece0f92015-05-12 12:43:54 +02001437 if( buflen != 0 && buf[buflen - 1] == '\0' &&
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001438 strstr( (const char *) buf, "-----BEGIN CERTIFICATE-----" ) != NULL )
1439 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001440 buf_format = MBEDTLS_X509_FORMAT_PEM;
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001441 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001442
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001443 if( buf_format == MBEDTLS_X509_FORMAT_DER )
1444 return mbedtls_x509_crt_parse_der( chain, buf, buflen );
Janos Follath98e28a72016-05-31 14:03:54 +01001445#else
1446 return mbedtls_x509_crt_parse_der( chain, buf, buflen );
1447#endif
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001448
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001449#if defined(MBEDTLS_PEM_PARSE_C)
1450 if( buf_format == MBEDTLS_X509_FORMAT_PEM )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001451 {
Janos Follath865b3eb2019-12-16 11:46:15 +00001452 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001453 mbedtls_pem_context pem;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001454
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001455 /* 1 rather than 0 since the terminating NULL byte is counted in */
1456 while( buflen > 1 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001457 {
1458 size_t use_len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001459 mbedtls_pem_init( &pem );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001460
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001461 /* If we get there, we know the string is null-terminated */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001462 ret = mbedtls_pem_read_buffer( &pem,
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001463 "-----BEGIN CERTIFICATE-----",
1464 "-----END CERTIFICATE-----",
1465 buf, NULL, 0, &use_len );
1466
1467 if( ret == 0 )
1468 {
1469 /*
1470 * Was PEM encoded
1471 */
1472 buflen -= use_len;
1473 buf += use_len;
1474 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001475 else if( ret == MBEDTLS_ERR_PEM_BAD_INPUT_DATA )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001476 {
1477 return( ret );
1478 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001479 else if( ret != MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001480 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001481 mbedtls_pem_free( &pem );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001482
1483 /*
1484 * PEM header and footer were found
1485 */
1486 buflen -= use_len;
1487 buf += use_len;
1488
1489 if( first_error == 0 )
1490 first_error = ret;
1491
Paul Bakker5a5fa922014-09-26 14:53:04 +02001492 total_failed++;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001493 continue;
1494 }
1495 else
1496 break;
1497
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001498 ret = mbedtls_x509_crt_parse_der( chain, pem.buf, pem.buflen );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001499
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001500 mbedtls_pem_free( &pem );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001501
1502 if( ret != 0 )
1503 {
1504 /*
1505 * Quit parsing on a memory error
1506 */
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02001507 if( ret == MBEDTLS_ERR_X509_ALLOC_FAILED )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001508 return( ret );
1509
1510 if( first_error == 0 )
1511 first_error = ret;
1512
1513 total_failed++;
1514 continue;
1515 }
1516
1517 success = 1;
1518 }
1519 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001520
1521 if( success )
1522 return( total_failed );
1523 else if( first_error )
1524 return( first_error );
1525 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001526 return( MBEDTLS_ERR_X509_CERT_UNKNOWN_FORMAT );
Janos Follath98e28a72016-05-31 14:03:54 +01001527#endif /* MBEDTLS_PEM_PARSE_C */
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001528}
1529
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001530#if defined(MBEDTLS_FS_IO)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001531/*
1532 * Load one or more certificates and add them to the chained list
1533 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001534int mbedtls_x509_crt_parse_file( mbedtls_x509_crt *chain, const char *path )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001535{
Janos Follath865b3eb2019-12-16 11:46:15 +00001536 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001537 size_t n;
1538 unsigned char *buf;
1539
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001540 if( ( ret = mbedtls_pk_load_file( path, &buf, &n ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001541 return( ret );
1542
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001543 ret = mbedtls_x509_crt_parse( chain, buf, n );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001544
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05001545 mbedtls_platform_zeroize( buf, n );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001546 mbedtls_free( buf );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001547
1548 return( ret );
1549}
1550
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001551int mbedtls_x509_crt_parse_path( mbedtls_x509_crt *chain, const char *path )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001552{
1553 int ret = 0;
Paul Bakkerfa6a6202013-10-28 18:48:30 +01001554#if defined(_WIN32) && !defined(EFIX64) && !defined(EFI32)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001555 int w_ret;
1556 WCHAR szDir[MAX_PATH];
1557 char filename[MAX_PATH];
Paul Bakker9af723c2014-05-01 13:03:14 +02001558 char *p;
Manuel Pégourié-Gonnard261faed2015-10-21 10:16:29 +02001559 size_t len = strlen( path );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001560
Paul Bakker9af723c2014-05-01 13:03:14 +02001561 WIN32_FIND_DATAW file_data;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001562 HANDLE hFind;
1563
1564 if( len > MAX_PATH - 3 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001565 return( MBEDTLS_ERR_X509_BAD_INPUT_DATA );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001566
Paul Bakker9af723c2014-05-01 13:03:14 +02001567 memset( szDir, 0, sizeof(szDir) );
1568 memset( filename, 0, MAX_PATH );
1569 memcpy( filename, path, len );
1570 filename[len++] = '\\';
1571 p = filename + len;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001572 filename[len++] = '*';
1573
Simon B3c6b18d2016-11-03 01:11:37 +00001574 w_ret = MultiByteToWideChar( CP_ACP, 0, filename, (int)len, szDir,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001575 MAX_PATH - 3 );
Manuel Pégourié-Gonnardacdb9b92015-01-23 17:50:34 +00001576 if( w_ret == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001577 return( MBEDTLS_ERR_X509_BAD_INPUT_DATA );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001578
1579 hFind = FindFirstFileW( szDir, &file_data );
Paul Bakker66d5d072014-06-17 16:39:18 +02001580 if( hFind == INVALID_HANDLE_VALUE )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001581 return( MBEDTLS_ERR_X509_FILE_IO_ERROR );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001582
1583 len = MAX_PATH - len;
1584 do
1585 {
Paul Bakker9af723c2014-05-01 13:03:14 +02001586 memset( p, 0, len );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001587
1588 if( file_data.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY )
1589 continue;
1590
Paul Bakker9af723c2014-05-01 13:03:14 +02001591 w_ret = WideCharToMultiByte( CP_ACP, 0, file_data.cFileName,
Paul Bakker66d5d072014-06-17 16:39:18 +02001592 lstrlenW( file_data.cFileName ),
Manuel Pégourié-Gonnard261faed2015-10-21 10:16:29 +02001593 p, (int) len - 1,
Paul Bakker9af723c2014-05-01 13:03:14 +02001594 NULL, NULL );
Manuel Pégourié-Gonnardacdb9b92015-01-23 17:50:34 +00001595 if( w_ret == 0 )
Ron Eldor36d90422017-01-09 15:09:16 +02001596 {
1597 ret = MBEDTLS_ERR_X509_FILE_IO_ERROR;
1598 goto cleanup;
1599 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001600
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001601 w_ret = mbedtls_x509_crt_parse_file( chain, filename );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001602 if( w_ret < 0 )
1603 ret++;
1604 else
1605 ret += w_ret;
1606 }
1607 while( FindNextFileW( hFind, &file_data ) != 0 );
1608
Paul Bakker66d5d072014-06-17 16:39:18 +02001609 if( GetLastError() != ERROR_NO_MORE_FILES )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001610 ret = MBEDTLS_ERR_X509_FILE_IO_ERROR;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001611
Ron Eldor36d90422017-01-09 15:09:16 +02001612cleanup:
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001613 FindClose( hFind );
Paul Bakkerbe089b02013-10-14 15:51:50 +02001614#else /* _WIN32 */
Manuel Pégourié-Gonnard964bf9b2013-11-26 16:47:11 +01001615 int t_ret;
Andres AGf9113192016-09-02 14:06:04 +01001616 int snp_ret;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001617 struct stat sb;
Manuel Pégourié-Gonnard964bf9b2013-11-26 16:47:11 +01001618 struct dirent *entry;
Andres AGf9113192016-09-02 14:06:04 +01001619 char entry_name[MBEDTLS_X509_MAX_FILE_PATH_LEN];
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001620 DIR *dir = opendir( path );
1621
Paul Bakker66d5d072014-06-17 16:39:18 +02001622 if( dir == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001623 return( MBEDTLS_ERR_X509_FILE_IO_ERROR );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001624
Ron Eldor63140682017-01-09 19:27:59 +02001625#if defined(MBEDTLS_THREADING_C)
Manuel Pégourié-Gonnard944cfe82015-05-27 20:07:18 +02001626 if( ( ret = mbedtls_mutex_lock( &mbedtls_threading_readdir_mutex ) ) != 0 )
Manuel Pégourié-Gonnardf9b85d92015-06-22 18:39:57 +02001627 {
1628 closedir( dir );
Manuel Pégourié-Gonnard5ad68e42013-11-28 17:11:54 +01001629 return( ret );
Manuel Pégourié-Gonnardf9b85d92015-06-22 18:39:57 +02001630 }
Ron Eldor63140682017-01-09 19:27:59 +02001631#endif /* MBEDTLS_THREADING_C */
Manuel Pégourié-Gonnard5ad68e42013-11-28 17:11:54 +01001632
Manuel Pégourié-Gonnard964bf9b2013-11-26 16:47:11 +01001633 while( ( entry = readdir( dir ) ) != NULL )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001634 {
Andres AGf9113192016-09-02 14:06:04 +01001635 snp_ret = mbedtls_snprintf( entry_name, sizeof entry_name,
1636 "%s/%s", path, entry->d_name );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001637
Andres AGf9113192016-09-02 14:06:04 +01001638 if( snp_ret < 0 || (size_t)snp_ret >= sizeof entry_name )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001639 {
Andres AGf9113192016-09-02 14:06:04 +01001640 ret = MBEDTLS_ERR_X509_BUFFER_TOO_SMALL;
1641 goto cleanup;
1642 }
1643 else if( stat( entry_name, &sb ) == -1 )
1644 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001645 ret = MBEDTLS_ERR_X509_FILE_IO_ERROR;
Manuel Pégourié-Gonnard5ad68e42013-11-28 17:11:54 +01001646 goto cleanup;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001647 }
1648
1649 if( !S_ISREG( sb.st_mode ) )
1650 continue;
1651
1652 // Ignore parse errors
1653 //
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001654 t_ret = mbedtls_x509_crt_parse_file( chain, entry_name );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001655 if( t_ret < 0 )
1656 ret++;
1657 else
1658 ret += t_ret;
1659 }
Manuel Pégourié-Gonnard5ad68e42013-11-28 17:11:54 +01001660
1661cleanup:
Andres AGf9113192016-09-02 14:06:04 +01001662 closedir( dir );
1663
Ron Eldor63140682017-01-09 19:27:59 +02001664#if defined(MBEDTLS_THREADING_C)
Manuel Pégourié-Gonnard944cfe82015-05-27 20:07:18 +02001665 if( mbedtls_mutex_unlock( &mbedtls_threading_readdir_mutex ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001666 ret = MBEDTLS_ERR_THREADING_MUTEX_ERROR;
Ron Eldor63140682017-01-09 19:27:59 +02001667#endif /* MBEDTLS_THREADING_C */
Manuel Pégourié-Gonnard5ad68e42013-11-28 17:11:54 +01001668
Paul Bakkerbe089b02013-10-14 15:51:50 +02001669#endif /* _WIN32 */
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001670
1671 return( ret );
1672}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001673#endif /* MBEDTLS_FS_IO */
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001674
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001675/*
1676 * OtherName ::= SEQUENCE {
1677 * type-id OBJECT IDENTIFIER,
1678 * value [0] EXPLICIT ANY DEFINED BY type-id }
1679 *
1680 * HardwareModuleName ::= SEQUENCE {
1681 * hwType OBJECT IDENTIFIER,
1682 * hwSerialNum OCTET STRING }
1683 *
1684 * NOTE: we currently only parse and use otherName of type HwModuleName,
1685 * as defined in RFC 4108.
1686 */
1687static int x509_get_other_name( const mbedtls_x509_buf *subject_alt_name,
1688 mbedtls_x509_san_other_name *other_name )
1689{
Janos Follathab23cd12019-05-09 13:53:57 +01001690 int ret = 0;
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001691 size_t len;
1692 unsigned char *p = subject_alt_name->p;
1693 const unsigned char *end = p + subject_alt_name->len;
1694 mbedtls_x509_buf cur_oid;
1695
1696 if( ( subject_alt_name->tag &
1697 ( MBEDTLS_ASN1_TAG_CLASS_MASK | MBEDTLS_ASN1_TAG_VALUE_MASK ) ) !=
1698 ( MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_X509_SAN_OTHER_NAME ) )
1699 {
1700 /*
1701 * The given subject alternative name is not of type "othername".
1702 */
1703 return( MBEDTLS_ERR_X509_BAD_INPUT_DATA );
1704 }
1705
1706 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
1707 MBEDTLS_ASN1_OID ) ) != 0 )
1708 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS + ret );
1709
1710 cur_oid.tag = MBEDTLS_ASN1_OID;
1711 cur_oid.p = p;
1712 cur_oid.len = len;
1713
1714 /*
1715 * Only HwModuleName is currently supported.
1716 */
1717 if( MBEDTLS_OID_CMP( MBEDTLS_OID_ON_HW_MODULE_NAME, &cur_oid ) != 0 )
1718 {
1719 return( MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE );
1720 }
1721
Ron Eldor890819a2019-05-13 19:03:04 +03001722 if( p + len >= end )
1723 {
Sébastien Duquette661d7252019-06-23 17:45:26 -04001724 mbedtls_platform_zeroize( other_name, sizeof( *other_name ) );
Ron Eldor890819a2019-05-13 19:03:04 +03001725 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS +
1726 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
1727 }
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001728 p += len;
1729 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
1730 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_CONTEXT_SPECIFIC ) ) != 0 )
1731 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS + ret );
1732
1733 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
1734 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
1735 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS + ret );
1736
1737 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len, MBEDTLS_ASN1_OID ) ) != 0 )
1738 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS + ret );
1739
1740 other_name->value.hardware_module_name.oid.tag = MBEDTLS_ASN1_OID;
1741 other_name->value.hardware_module_name.oid.p = p;
1742 other_name->value.hardware_module_name.oid.len = len;
1743
Ron Eldor890819a2019-05-13 19:03:04 +03001744 if( p + len >= end )
1745 {
Sébastien Duquette661d7252019-06-23 17:45:26 -04001746 mbedtls_platform_zeroize( other_name, sizeof( *other_name ) );
Ron Eldor890819a2019-05-13 19:03:04 +03001747 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS +
1748 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
1749 }
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001750 p += len;
1751 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
1752 MBEDTLS_ASN1_OCTET_STRING ) ) != 0 )
Ron Eldor890819a2019-05-13 19:03:04 +03001753 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS + ret );
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001754
1755 other_name->value.hardware_module_name.val.tag = MBEDTLS_ASN1_OCTET_STRING;
1756 other_name->value.hardware_module_name.val.p = p;
1757 other_name->value.hardware_module_name.val.len = len;
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001758 p += len;
1759 if( p != end )
1760 {
Ron Eldor890819a2019-05-13 19:03:04 +03001761 mbedtls_platform_zeroize( other_name,
Sébastien Duquette661d7252019-06-23 17:45:26 -04001762 sizeof( *other_name ) );
Ron Eldor890819a2019-05-13 19:03:04 +03001763 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS +
1764 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001765 }
1766 return( 0 );
1767}
1768
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02001769static int x509_info_subject_alt_name( char **buf, size_t *size,
Janos Follath2f0ec1e2019-05-10 11:06:31 +01001770 const mbedtls_x509_sequence
1771 *subject_alt_name,
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001772 const char *prefix )
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02001773{
Janos Follath865b3eb2019-12-16 11:46:15 +00001774 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02001775 size_t n = *size;
1776 char *p = *buf;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001777 const mbedtls_x509_sequence *cur = subject_alt_name;
Ron Eldor890819a2019-05-13 19:03:04 +03001778 mbedtls_x509_subject_alternative_name san;
1779 int parse_ret;
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02001780
1781 while( cur != NULL )
1782 {
Ron Eldor890819a2019-05-13 19:03:04 +03001783 memset( &san, 0, sizeof( san ) );
1784 parse_ret = mbedtls_x509_parse_subject_alt_name( &cur->buf, &san );
1785 if( parse_ret != 0 )
1786 {
1787 if( parse_ret == MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE )
1788 {
1789 ret = mbedtls_snprintf( p, n, "\n%s <unsupported>", prefix );
1790 MBEDTLS_X509_SAFE_SNPRINTF;
1791 }
1792 else
1793 {
1794 ret = mbedtls_snprintf( p, n, "\n%s <malformed>", prefix );
1795 MBEDTLS_X509_SAFE_SNPRINTF;
1796 }
1797 cur = cur->next;
1798 continue;
1799 }
1800
1801 switch( san.type )
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02001802 {
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001803 /*
1804 * otherName
1805 */
Ron Eldora2913912019-05-16 16:17:38 +03001806 case MBEDTLS_X509_SAN_OTHER_NAME:
Ron Eldor890819a2019-05-13 19:03:04 +03001807 {
1808 mbedtls_x509_san_other_name *other_name = &san.san.other_name;
1809
1810 ret = mbedtls_snprintf( p, n, "\n%s otherName :", prefix );
1811 MBEDTLS_X509_SAFE_SNPRINTF;
1812
1813 if( MBEDTLS_OID_CMP( MBEDTLS_OID_ON_HW_MODULE_NAME,
1814 &other_name->value.hardware_module_name.oid ) != 0 )
Janos Follath22f605f2019-05-10 10:37:17 +01001815 {
Ron Eldor890819a2019-05-13 19:03:04 +03001816 ret = mbedtls_snprintf( p, n, "\n%s hardware module name :", prefix );
1817 MBEDTLS_X509_SAFE_SNPRINTF;
1818 ret = mbedtls_snprintf( p, n, "\n%s hardware type : ", prefix );
Janos Follath2f0ec1e2019-05-10 11:06:31 +01001819 MBEDTLS_X509_SAFE_SNPRINTF;
1820
Ron Eldor890819a2019-05-13 19:03:04 +03001821 ret = mbedtls_oid_get_numeric_string( p, n, &other_name->value.hardware_module_name.oid );
1822 MBEDTLS_X509_SAFE_SNPRINTF;
Janos Follath2f0ec1e2019-05-10 11:06:31 +01001823
Ron Eldor890819a2019-05-13 19:03:04 +03001824 ret = mbedtls_snprintf( p, n, "\n%s hardware serial number : ", prefix );
1825 MBEDTLS_X509_SAFE_SNPRINTF;
1826
1827 if( other_name->value.hardware_module_name.val.len >= n )
1828 {
1829 *p = '\0';
1830 return( MBEDTLS_ERR_X509_BUFFER_TOO_SMALL );
Janos Follath22f605f2019-05-10 10:37:17 +01001831 }
1832
Ron Eldora2913912019-05-16 16:17:38 +03001833 memcpy( p, other_name->value.hardware_module_name.val.p,
1834 other_name->value.hardware_module_name.val.len );
1835 p += other_name->value.hardware_module_name.val.len;
1836
Ron Eldor890819a2019-05-13 19:03:04 +03001837 n -= other_name->value.hardware_module_name.val.len;
Janos Follath2f0ec1e2019-05-10 11:06:31 +01001838
Ron Eldor890819a2019-05-13 19:03:04 +03001839 }/* MBEDTLS_OID_ON_HW_MODULE_NAME */
1840 }
1841 break;
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001842
1843 /*
1844 * dNSName
1845 */
Ron Eldora2913912019-05-16 16:17:38 +03001846 case MBEDTLS_X509_SAN_DNS_NAME:
Ron Eldor890819a2019-05-13 19:03:04 +03001847 {
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001848 ret = mbedtls_snprintf( p, n, "\n%s dNSName : ", prefix );
1849 MBEDTLS_X509_SAFE_SNPRINTF;
Ron Eldor890819a2019-05-13 19:03:04 +03001850 if( san.san.unstructured_name.len >= n )
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001851 {
1852 *p = '\0';
1853 return( MBEDTLS_ERR_X509_BUFFER_TOO_SMALL );
1854 }
Ron Eldora2913912019-05-16 16:17:38 +03001855
1856 memcpy( p, san.san.unstructured_name.p, san.san.unstructured_name.len );
1857 p += san.san.unstructured_name.len;
Ron Eldor890819a2019-05-13 19:03:04 +03001858 n -= san.san.unstructured_name.len;
Ron Eldor890819a2019-05-13 19:03:04 +03001859 }
1860 break;
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001861
1862 /*
Ron Eldor890819a2019-05-13 19:03:04 +03001863 * Type not supported, skip item.
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001864 */
1865 default:
Janos Follath22f605f2019-05-10 10:37:17 +01001866 ret = mbedtls_snprintf( p, n, "\n%s <unsupported>", prefix );
1867 MBEDTLS_X509_SAFE_SNPRINTF;
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001868 break;
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02001869 }
1870
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02001871 cur = cur->next;
1872 }
1873
1874 *p = '\0';
1875
1876 *size = n;
1877 *buf = p;
1878
1879 return( 0 );
1880}
1881
Ron Eldor890819a2019-05-13 19:03:04 +03001882int mbedtls_x509_parse_subject_alt_name( const mbedtls_x509_buf *san_buf,
1883 mbedtls_x509_subject_alternative_name *san )
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001884{
Janos Follath865b3eb2019-12-16 11:46:15 +00001885 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Ron Eldor890819a2019-05-13 19:03:04 +03001886 switch( san_buf->tag &
1887 ( MBEDTLS_ASN1_TAG_CLASS_MASK |
1888 MBEDTLS_ASN1_TAG_VALUE_MASK ) )
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001889 {
Ron Eldor890819a2019-05-13 19:03:04 +03001890 /*
1891 * otherName
1892 */
1893 case( MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_X509_SAN_OTHER_NAME ):
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001894 {
Ron Eldor890819a2019-05-13 19:03:04 +03001895 mbedtls_x509_san_other_name other_name;
1896
1897 ret = x509_get_other_name( san_buf, &other_name );
1898 if( ret != 0 )
1899 return( ret );
1900
1901 memset( san, 0, sizeof( mbedtls_x509_subject_alternative_name ) );
1902 san->type = MBEDTLS_X509_SAN_OTHER_NAME;
1903 memcpy( &san->san.other_name,
1904 &other_name, sizeof( other_name ) );
1905
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001906 }
Ron Eldor890819a2019-05-13 19:03:04 +03001907 break;
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001908
Ron Eldor890819a2019-05-13 19:03:04 +03001909 /*
1910 * dNSName
1911 */
1912 case( MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_X509_SAN_DNS_NAME ):
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001913 {
Ron Eldor890819a2019-05-13 19:03:04 +03001914 memset( san, 0, sizeof( mbedtls_x509_subject_alternative_name ) );
1915 san->type = MBEDTLS_X509_SAN_DNS_NAME;
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001916
Ron Eldor890819a2019-05-13 19:03:04 +03001917 memcpy( &san->san.unstructured_name,
1918 san_buf, sizeof( *san_buf ) );
Janos Follath6c379b42019-05-10 14:17:16 +01001919
Ron Eldor890819a2019-05-13 19:03:04 +03001920 }
1921 break;
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001922
Ron Eldor890819a2019-05-13 19:03:04 +03001923 /*
1924 * Type not supported
1925 */
1926 default:
1927 return( MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE );
1928 }
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001929 return( 0 );
1930}
1931
Manuel Pégourié-Gonnard0db29b02014-04-01 18:12:24 +02001932#define PRINT_ITEM(i) \
1933 { \
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001934 ret = mbedtls_snprintf( p, n, "%s" i, sep ); \
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001935 MBEDTLS_X509_SAFE_SNPRINTF; \
Manuel Pégourié-Gonnard0db29b02014-04-01 18:12:24 +02001936 sep = ", "; \
1937 }
1938
1939#define CERT_TYPE(type,name) \
Hanno Becker1eeca412018-10-15 12:01:35 +01001940 if( ns_cert_type & (type) ) \
Manuel Pégourié-Gonnard0db29b02014-04-01 18:12:24 +02001941 PRINT_ITEM( name );
1942
Manuel Pégourié-Gonnard919f8f52014-04-01 13:01:11 +02001943static int x509_info_cert_type( char **buf, size_t *size,
1944 unsigned char ns_cert_type )
1945{
Janos Follath865b3eb2019-12-16 11:46:15 +00001946 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard919f8f52014-04-01 13:01:11 +02001947 size_t n = *size;
1948 char *p = *buf;
Manuel Pégourié-Gonnard7b30cfc2014-04-01 18:00:07 +02001949 const char *sep = "";
Manuel Pégourié-Gonnard919f8f52014-04-01 13:01:11 +02001950
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001951 CERT_TYPE( MBEDTLS_X509_NS_CERT_TYPE_SSL_CLIENT, "SSL Client" );
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01001952 CERT_TYPE( MBEDTLS_X509_NS_CERT_TYPE_SSL_SERVER, "SSL Server" );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001953 CERT_TYPE( MBEDTLS_X509_NS_CERT_TYPE_EMAIL, "Email" );
1954 CERT_TYPE( MBEDTLS_X509_NS_CERT_TYPE_OBJECT_SIGNING, "Object Signing" );
1955 CERT_TYPE( MBEDTLS_X509_NS_CERT_TYPE_RESERVED, "Reserved" );
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01001956 CERT_TYPE( MBEDTLS_X509_NS_CERT_TYPE_SSL_CA, "SSL CA" );
1957 CERT_TYPE( MBEDTLS_X509_NS_CERT_TYPE_EMAIL_CA, "Email CA" );
1958 CERT_TYPE( MBEDTLS_X509_NS_CERT_TYPE_OBJECT_SIGNING_CA, "Object Signing CA" );
Manuel Pégourié-Gonnard919f8f52014-04-01 13:01:11 +02001959
1960 *size = n;
1961 *buf = p;
1962
1963 return( 0 );
1964}
1965
Manuel Pégourié-Gonnard0db29b02014-04-01 18:12:24 +02001966#define KEY_USAGE(code,name) \
Hanno Becker1eeca412018-10-15 12:01:35 +01001967 if( key_usage & (code) ) \
Manuel Pégourié-Gonnard0db29b02014-04-01 18:12:24 +02001968 PRINT_ITEM( name );
1969
Manuel Pégourié-Gonnard65c2ddc2014-04-01 14:12:11 +02001970static int x509_info_key_usage( char **buf, size_t *size,
Manuel Pégourié-Gonnard9a702252015-06-23 10:14:36 +02001971 unsigned int key_usage )
Manuel Pégourié-Gonnard65c2ddc2014-04-01 14:12:11 +02001972{
Janos Follath865b3eb2019-12-16 11:46:15 +00001973 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard65c2ddc2014-04-01 14:12:11 +02001974 size_t n = *size;
1975 char *p = *buf;
Manuel Pégourié-Gonnard7b30cfc2014-04-01 18:00:07 +02001976 const char *sep = "";
Manuel Pégourié-Gonnard65c2ddc2014-04-01 14:12:11 +02001977
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001978 KEY_USAGE( MBEDTLS_X509_KU_DIGITAL_SIGNATURE, "Digital Signature" );
1979 KEY_USAGE( MBEDTLS_X509_KU_NON_REPUDIATION, "Non Repudiation" );
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01001980 KEY_USAGE( MBEDTLS_X509_KU_KEY_ENCIPHERMENT, "Key Encipherment" );
1981 KEY_USAGE( MBEDTLS_X509_KU_DATA_ENCIPHERMENT, "Data Encipherment" );
1982 KEY_USAGE( MBEDTLS_X509_KU_KEY_AGREEMENT, "Key Agreement" );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001983 KEY_USAGE( MBEDTLS_X509_KU_KEY_CERT_SIGN, "Key Cert Sign" );
1984 KEY_USAGE( MBEDTLS_X509_KU_CRL_SIGN, "CRL Sign" );
Manuel Pégourié-Gonnard9a702252015-06-23 10:14:36 +02001985 KEY_USAGE( MBEDTLS_X509_KU_ENCIPHER_ONLY, "Encipher Only" );
1986 KEY_USAGE( MBEDTLS_X509_KU_DECIPHER_ONLY, "Decipher Only" );
Manuel Pégourié-Gonnard65c2ddc2014-04-01 14:12:11 +02001987
1988 *size = n;
1989 *buf = p;
1990
1991 return( 0 );
1992}
1993
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02001994static int x509_info_ext_key_usage( char **buf, size_t *size,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001995 const mbedtls_x509_sequence *extended_key_usage )
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02001996{
Janos Follath865b3eb2019-12-16 11:46:15 +00001997 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02001998 const char *desc;
1999 size_t n = *size;
2000 char *p = *buf;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002001 const mbedtls_x509_sequence *cur = extended_key_usage;
Manuel Pégourié-Gonnard7b30cfc2014-04-01 18:00:07 +02002002 const char *sep = "";
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02002003
2004 while( cur != NULL )
2005 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002006 if( mbedtls_oid_get_extended_key_usage( &cur->buf, &desc ) != 0 )
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02002007 desc = "???";
2008
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002009 ret = mbedtls_snprintf( p, n, "%s%s", sep, desc );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002010 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02002011
Manuel Pégourié-Gonnard7b30cfc2014-04-01 18:00:07 +02002012 sep = ", ";
2013
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02002014 cur = cur->next;
2015 }
2016
2017 *size = n;
2018 *buf = p;
2019
2020 return( 0 );
2021}
2022
Ron Eldor74d9acc2019-03-21 14:00:03 +02002023static int x509_info_cert_policies( char **buf, size_t *size,
2024 const mbedtls_x509_sequence *certificate_policies )
2025{
Janos Follath865b3eb2019-12-16 11:46:15 +00002026 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Ron Eldor74d9acc2019-03-21 14:00:03 +02002027 const char *desc;
2028 size_t n = *size;
2029 char *p = *buf;
2030 const mbedtls_x509_sequence *cur = certificate_policies;
2031 const char *sep = "";
2032
2033 while( cur != NULL )
2034 {
2035 if( mbedtls_oid_get_certificate_policies( &cur->buf, &desc ) != 0 )
2036 desc = "???";
2037
2038 ret = mbedtls_snprintf( p, n, "%s%s", sep, desc );
2039 MBEDTLS_X509_SAFE_SNPRINTF;
2040
2041 sep = ", ";
2042
2043 cur = cur->next;
2044 }
2045
2046 *size = n;
2047 *buf = p;
2048
2049 return( 0 );
2050}
2051
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002052/*
2053 * Return an informational string about the certificate.
2054 */
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002055#define BEFORE_COLON 18
2056#define BC "18"
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002057int mbedtls_x509_crt_info( char *buf, size_t size, const char *prefix,
2058 const mbedtls_x509_crt *crt )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002059{
Janos Follath865b3eb2019-12-16 11:46:15 +00002060 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002061 size_t n;
2062 char *p;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002063 char key_size_str[BEFORE_COLON];
2064
2065 p = buf;
2066 n = size;
2067
Janos Follath98e28a72016-05-31 14:03:54 +01002068 if( NULL == crt )
2069 {
2070 ret = mbedtls_snprintf( p, n, "\nCertificate is uninitialised!\n" );
2071 MBEDTLS_X509_SAFE_SNPRINTF;
2072
2073 return( (int) ( size - n ) );
2074 }
2075
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002076 ret = mbedtls_snprintf( p, n, "%scert. version : %d\n",
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002077 prefix, crt->version );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002078 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002079 ret = mbedtls_snprintf( p, n, "%sserial number : ",
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002080 prefix );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002081 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002082
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002083 ret = mbedtls_x509_serial_gets( p, n, &crt->serial );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002084 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002085
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002086 ret = mbedtls_snprintf( p, n, "\n%sissuer name : ", prefix );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002087 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002088 ret = mbedtls_x509_dn_gets( p, n, &crt->issuer );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002089 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002090
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002091 ret = mbedtls_snprintf( p, n, "\n%ssubject name : ", prefix );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002092 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002093 ret = mbedtls_x509_dn_gets( p, n, &crt->subject );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002094 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002095
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002096 ret = mbedtls_snprintf( p, n, "\n%sissued on : " \
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002097 "%04d-%02d-%02d %02d:%02d:%02d", prefix,
2098 crt->valid_from.year, crt->valid_from.mon,
2099 crt->valid_from.day, crt->valid_from.hour,
2100 crt->valid_from.min, crt->valid_from.sec );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002101 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002102
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002103 ret = mbedtls_snprintf( p, n, "\n%sexpires on : " \
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002104 "%04d-%02d-%02d %02d:%02d:%02d", prefix,
2105 crt->valid_to.year, crt->valid_to.mon,
2106 crt->valid_to.day, crt->valid_to.hour,
2107 crt->valid_to.min, crt->valid_to.sec );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002108 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002109
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002110 ret = mbedtls_snprintf( p, n, "\n%ssigned using : ", prefix );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002111 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002112
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002113 ret = mbedtls_x509_sig_alg_gets( p, n, &crt->sig_oid, crt->sig_pk,
Manuel Pégourié-Gonnardbf696d02014-06-05 17:07:30 +02002114 crt->sig_md, crt->sig_opts );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002115 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002116
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002117 /* Key size */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002118 if( ( ret = mbedtls_x509_key_size_helper( key_size_str, BEFORE_COLON,
2119 mbedtls_pk_get_name( &crt->pk ) ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002120 {
2121 return( ret );
2122 }
2123
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002124 ret = mbedtls_snprintf( p, n, "\n%s%-" BC "s: %d bits", prefix, key_size_str,
Manuel Pégourié-Gonnard097c7bb2015-06-18 16:43:38 +02002125 (int) mbedtls_pk_get_bitlen( &crt->pk ) );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002126 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002127
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002128 /*
2129 * Optional extensions
2130 */
2131
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01002132 if( crt->ext_types & MBEDTLS_X509_EXT_BASIC_CONSTRAINTS )
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002133 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002134 ret = mbedtls_snprintf( p, n, "\n%sbasic constraints : CA=%s", prefix,
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002135 crt->ca_istrue ? "true" : "false" );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002136 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002137
2138 if( crt->max_pathlen > 0 )
2139 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002140 ret = mbedtls_snprintf( p, n, ", max_pathlen=%d", crt->max_pathlen - 1 );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002141 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002142 }
2143 }
2144
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01002145 if( crt->ext_types & MBEDTLS_X509_EXT_SUBJECT_ALT_NAME )
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002146 {
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02002147 ret = mbedtls_snprintf( p, n, "\n%ssubject alt name :", prefix );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002148 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02002149
2150 if( ( ret = x509_info_subject_alt_name( &p, &n,
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02002151 &crt->subject_alt_names,
Ron Eldor6aeae9e2019-05-20 12:00:36 +03002152 prefix ) ) != 0 )
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02002153 return( ret );
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002154 }
2155
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002156 if( crt->ext_types & MBEDTLS_X509_EXT_NS_CERT_TYPE )
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002157 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002158 ret = mbedtls_snprintf( p, n, "\n%scert. type : ", prefix );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002159 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnard919f8f52014-04-01 13:01:11 +02002160
2161 if( ( ret = x509_info_cert_type( &p, &n, crt->ns_cert_type ) ) != 0 )
2162 return( ret );
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002163 }
2164
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002165 if( crt->ext_types & MBEDTLS_X509_EXT_KEY_USAGE )
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002166 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002167 ret = mbedtls_snprintf( p, n, "\n%skey usage : ", prefix );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002168 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnard65c2ddc2014-04-01 14:12:11 +02002169
2170 if( ( ret = x509_info_key_usage( &p, &n, crt->key_usage ) ) != 0 )
2171 return( ret );
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002172 }
2173
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002174 if( crt->ext_types & MBEDTLS_X509_EXT_EXTENDED_KEY_USAGE )
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002175 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002176 ret = mbedtls_snprintf( p, n, "\n%sext key usage : ", prefix );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002177 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02002178
2179 if( ( ret = x509_info_ext_key_usage( &p, &n,
2180 &crt->ext_key_usage ) ) != 0 )
2181 return( ret );
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002182 }
2183
Ron Eldor74d9acc2019-03-21 14:00:03 +02002184 if( crt->ext_types & MBEDTLS_OID_X509_EXT_CERTIFICATE_POLICIES )
2185 {
2186 ret = mbedtls_snprintf( p, n, "\n%scertificate policies : ", prefix );
2187 MBEDTLS_X509_SAFE_SNPRINTF;
2188
2189 if( ( ret = x509_info_cert_policies( &p, &n,
2190 &crt->certificate_policies ) ) != 0 )
2191 return( ret );
2192 }
2193
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002194 ret = mbedtls_snprintf( p, n, "\n" );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002195 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002196
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002197 return( (int) ( size - n ) );
2198}
2199
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01002200struct x509_crt_verify_string {
2201 int code;
2202 const char *string;
2203};
2204
2205static const struct x509_crt_verify_string x509_crt_verify_strings[] = {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01002206 { MBEDTLS_X509_BADCERT_EXPIRED, "The certificate validity has expired" },
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01002207 { MBEDTLS_X509_BADCERT_REVOKED, "The certificate has been revoked (is on a CRL)" },
2208 { MBEDTLS_X509_BADCERT_CN_MISMATCH, "The certificate Common Name (CN) does not match with the expected CN" },
2209 { MBEDTLS_X509_BADCERT_NOT_TRUSTED, "The certificate is not correctly signed by the trusted CA" },
2210 { MBEDTLS_X509_BADCRL_NOT_TRUSTED, "The CRL is not correctly signed by the trusted CA" },
2211 { MBEDTLS_X509_BADCRL_EXPIRED, "The CRL is expired" },
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01002212 { MBEDTLS_X509_BADCERT_MISSING, "Certificate was missing" },
2213 { MBEDTLS_X509_BADCERT_SKIP_VERIFY, "Certificate verification was skipped" },
2214 { MBEDTLS_X509_BADCERT_OTHER, "Other reason (can be used by verify callback)" },
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01002215 { MBEDTLS_X509_BADCERT_FUTURE, "The certificate validity starts in the future" },
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01002216 { MBEDTLS_X509_BADCRL_FUTURE, "The CRL is from the future" },
2217 { MBEDTLS_X509_BADCERT_KEY_USAGE, "Usage does not match the keyUsage extension" },
2218 { MBEDTLS_X509_BADCERT_EXT_KEY_USAGE, "Usage does not match the extendedKeyUsage extension" },
2219 { MBEDTLS_X509_BADCERT_NS_CERT_TYPE, "Usage does not match the nsCertType extension" },
Manuel Pégourié-Gonnard95051642015-06-15 10:39:46 +02002220 { MBEDTLS_X509_BADCERT_BAD_MD, "The certificate is signed with an unacceptable hash." },
2221 { MBEDTLS_X509_BADCERT_BAD_PK, "The certificate is signed with an unacceptable PK alg (eg RSA vs ECDSA)." },
2222 { MBEDTLS_X509_BADCERT_BAD_KEY, "The certificate is signed with an unacceptable key (eg bad curve, RSA too short)." },
2223 { MBEDTLS_X509_BADCRL_BAD_MD, "The CRL is signed with an unacceptable hash." },
2224 { MBEDTLS_X509_BADCRL_BAD_PK, "The CRL is signed with an unacceptable PK alg (eg RSA vs ECDSA)." },
2225 { 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 +01002226 { 0, NULL }
2227};
2228
2229int mbedtls_x509_crt_verify_info( char *buf, size_t size, const char *prefix,
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02002230 uint32_t flags )
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01002231{
Janos Follath865b3eb2019-12-16 11:46:15 +00002232 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01002233 const struct x509_crt_verify_string *cur;
2234 char *p = buf;
2235 size_t n = size;
2236
2237 for( cur = x509_crt_verify_strings; cur->string != NULL ; cur++ )
2238 {
2239 if( ( flags & cur->code ) == 0 )
2240 continue;
2241
2242 ret = mbedtls_snprintf( p, n, "%s%s\n", prefix, cur->string );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002243 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01002244 flags ^= cur->code;
2245 }
2246
2247 if( flags != 0 )
2248 {
2249 ret = mbedtls_snprintf( p, n, "%sUnknown reason "
2250 "(this should not happen)\n", prefix );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002251 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01002252 }
2253
2254 return( (int) ( size - n ) );
2255}
2256
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002257#if defined(MBEDTLS_X509_CHECK_KEY_USAGE)
Manuel Pégourié-Gonnard655a9642015-06-23 10:48:44 +02002258int mbedtls_x509_crt_check_key_usage( const mbedtls_x509_crt *crt,
2259 unsigned int usage )
Manuel Pégourié-Gonnard603116c2014-04-09 09:50:03 +02002260{
Manuel Pégourié-Gonnard655a9642015-06-23 10:48:44 +02002261 unsigned int usage_must, usage_may;
2262 unsigned int may_mask = MBEDTLS_X509_KU_ENCIPHER_ONLY
2263 | MBEDTLS_X509_KU_DECIPHER_ONLY;
2264
2265 if( ( crt->ext_types & MBEDTLS_X509_EXT_KEY_USAGE ) == 0 )
2266 return( 0 );
2267
2268 usage_must = usage & ~may_mask;
2269
2270 if( ( ( crt->key_usage & ~may_mask ) & usage_must ) != usage_must )
2271 return( MBEDTLS_ERR_X509_BAD_INPUT_DATA );
2272
2273 usage_may = usage & may_mask;
2274
2275 if( ( ( crt->key_usage & may_mask ) | usage_may ) != usage_may )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002276 return( MBEDTLS_ERR_X509_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard603116c2014-04-09 09:50:03 +02002277
2278 return( 0 );
2279}
2280#endif
2281
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002282#if defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE)
2283int mbedtls_x509_crt_check_extended_key_usage( const mbedtls_x509_crt *crt,
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02002284 const char *usage_oid,
2285 size_t usage_len )
2286{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002287 const mbedtls_x509_sequence *cur;
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02002288
2289 /* Extension is not mandatory, absent means no restriction */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002290 if( ( crt->ext_types & MBEDTLS_X509_EXT_EXTENDED_KEY_USAGE ) == 0 )
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02002291 return( 0 );
2292
2293 /*
2294 * Look for the requested usage (or wildcard ANY) in our list
2295 */
2296 for( cur = &crt->ext_key_usage; cur != NULL; cur = cur->next )
2297 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002298 const mbedtls_x509_buf *cur_oid = &cur->buf;
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02002299
2300 if( cur_oid->len == usage_len &&
2301 memcmp( cur_oid->p, usage_oid, usage_len ) == 0 )
2302 {
2303 return( 0 );
2304 }
2305
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002306 if( MBEDTLS_OID_CMP( MBEDTLS_OID_ANY_EXTENDED_KEY_USAGE, cur_oid ) == 0 )
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02002307 return( 0 );
2308 }
2309
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002310 return( MBEDTLS_ERR_X509_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02002311}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002312#endif /* MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE */
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02002313
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002314#if defined(MBEDTLS_X509_CRL_PARSE_C)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002315/*
2316 * Return 1 if the certificate is revoked, or 0 otherwise.
2317 */
Manuel Pégourié-Gonnardc730ed32015-06-02 10:38:50 +01002318int mbedtls_x509_crt_is_revoked( const mbedtls_x509_crt *crt, const mbedtls_x509_crl *crl )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002319{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002320 const mbedtls_x509_crl_entry *cur = &crl->entry;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002321
2322 while( cur != NULL && cur->serial.len != 0 )
2323 {
2324 if( crt->serial.len == cur->serial.len &&
2325 memcmp( crt->serial.p, cur->serial.p, crt->serial.len ) == 0 )
2326 {
Raoul Strackxa4e86142020-06-15 17:03:13 +02002327 return( 1 );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002328 }
2329
2330 cur = cur->next;
2331 }
2332
2333 return( 0 );
2334}
2335
2336/*
Manuel Pégourié-Gonnardeeef9472016-02-22 11:36:55 +01002337 * Check that the given certificate is not revoked according to the CRL.
Manuel Pégourié-Gonnard08eacec2017-10-18 14:20:24 +02002338 * Skip validation if no CRL for the given CA is present.
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002339 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002340static int x509_crt_verifycrl( mbedtls_x509_crt *crt, mbedtls_x509_crt *ca,
Manuel Pégourié-Gonnard95051642015-06-15 10:39:46 +02002341 mbedtls_x509_crl *crl_list,
2342 const mbedtls_x509_crt_profile *profile )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002343{
2344 int flags = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002345 unsigned char hash[MBEDTLS_MD_MAX_SIZE];
2346 const mbedtls_md_info_t *md_info;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002347
2348 if( ca == NULL )
2349 return( flags );
2350
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002351 while( crl_list != NULL )
2352 {
2353 if( crl_list->version == 0 ||
Hanno Beckerb75ffb52018-11-02 09:19:54 +00002354 x509_name_cmp( &crl_list->issuer, &ca->subject ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002355 {
2356 crl_list = crl_list->next;
2357 continue;
2358 }
2359
2360 /*
Manuel Pégourié-Gonnard99d4f192014-04-08 15:10:07 +02002361 * Check if the CA is configured to sign CRLs
2362 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002363#if defined(MBEDTLS_X509_CHECK_KEY_USAGE)
Hanno Beckerb75ffb52018-11-02 09:19:54 +00002364 if( mbedtls_x509_crt_check_key_usage( ca,
2365 MBEDTLS_X509_KU_CRL_SIGN ) != 0 )
Manuel Pégourié-Gonnard99d4f192014-04-08 15:10:07 +02002366 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002367 flags |= MBEDTLS_X509_BADCRL_NOT_TRUSTED;
Manuel Pégourié-Gonnard99d4f192014-04-08 15:10:07 +02002368 break;
2369 }
2370#endif
2371
2372 /*
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002373 * Check if CRL is correctly signed by the trusted CA
2374 */
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +02002375 if( x509_profile_check_md_alg( profile, crl_list->sig_md ) != 0 )
2376 flags |= MBEDTLS_X509_BADCRL_BAD_MD;
2377
2378 if( x509_profile_check_pk_alg( profile, crl_list->sig_pk ) != 0 )
2379 flags |= MBEDTLS_X509_BADCRL_BAD_PK;
2380
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002381 md_info = mbedtls_md_info_from_type( crl_list->sig_md );
Manuel Pégourié-Gonnard329e78c2017-06-26 12:22:17 +02002382 if( mbedtls_md( md_info, crl_list->tbs.p, crl_list->tbs.len, hash ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002383 {
Manuel Pégourié-Gonnard329e78c2017-06-26 12:22:17 +02002384 /* Note: this can't happen except after an internal error */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002385 flags |= MBEDTLS_X509_BADCRL_NOT_TRUSTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002386 break;
2387 }
2388
Manuel Pégourié-Gonnard3f816912017-10-26 10:24:16 +02002389 if( x509_profile_check_key( profile, &ca->pk ) != 0 )
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +02002390 flags |= MBEDTLS_X509_BADCERT_BAD_KEY;
Manuel Pégourié-Gonnard95051642015-06-15 10:39:46 +02002391
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002392 if( mbedtls_pk_verify_ext( crl_list->sig_pk, crl_list->sig_opts, &ca->pk,
2393 crl_list->sig_md, hash, mbedtls_md_get_size( md_info ),
Manuel Pégourié-Gonnard53882022014-06-05 17:53:52 +02002394 crl_list->sig.p, crl_list->sig.len ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002395 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002396 flags |= MBEDTLS_X509_BADCRL_NOT_TRUSTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002397 break;
2398 }
2399
2400 /*
2401 * Check for validity of CRL (Do not drop out)
2402 */
Manuel Pégourié-Gonnardc730ed32015-06-02 10:38:50 +01002403 if( mbedtls_x509_time_is_past( &crl_list->next_update ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002404 flags |= MBEDTLS_X509_BADCRL_EXPIRED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002405
Manuel Pégourié-Gonnardc730ed32015-06-02 10:38:50 +01002406 if( mbedtls_x509_time_is_future( &crl_list->this_update ) )
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01002407 flags |= MBEDTLS_X509_BADCRL_FUTURE;
Manuel Pégourié-Gonnard95337652014-03-10 13:15:18 +01002408
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002409 /*
2410 * Check if certificate is revoked
2411 */
Manuel Pégourié-Gonnardc730ed32015-06-02 10:38:50 +01002412 if( mbedtls_x509_crt_is_revoked( crt, crl_list ) )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002413 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002414 flags |= MBEDTLS_X509_BADCERT_REVOKED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002415 break;
2416 }
2417
2418 crl_list = crl_list->next;
2419 }
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +02002420
Paul Bakkerd8bb8262014-06-17 14:06:49 +02002421 return( flags );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002422}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002423#endif /* MBEDTLS_X509_CRL_PARSE_C */
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002424
Manuel Pégourié-Gonnard88421242014-10-17 11:36:18 +02002425/*
Manuel Pégourié-Gonnardf82a4d52017-07-03 19:26:25 +02002426 * Check the signature of a certificate by its parent
2427 */
2428static int x509_crt_check_signature( const mbedtls_x509_crt *child,
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002429 mbedtls_x509_crt *parent,
2430 mbedtls_x509_crt_restart_ctx *rs_ctx )
Manuel Pégourié-Gonnardf82a4d52017-07-03 19:26:25 +02002431{
Manuel Pégourié-Gonnardf82a4d52017-07-03 19:26:25 +02002432 unsigned char hash[MBEDTLS_MD_MAX_SIZE];
Andrzej Kurekd4a65532018-10-31 06:18:39 -04002433 size_t hash_len;
2434#if !defined(MBEDTLS_USE_PSA_CRYPTO)
2435 const mbedtls_md_info_t *md_info;
Manuel Pégourié-Gonnardf82a4d52017-07-03 19:26:25 +02002436 md_info = mbedtls_md_info_from_type( child->sig_md );
Andrzej Kurekd4a65532018-10-31 06:18:39 -04002437 hash_len = mbedtls_md_get_size( md_info );
Andrzej Kurek8b38ff52018-11-20 03:20:09 -05002438
Andrzej Kurekd4a65532018-10-31 06:18:39 -04002439 /* Note: hash errors can happen only after an internal error */
Manuel Pégourié-Gonnardf82a4d52017-07-03 19:26:25 +02002440 if( mbedtls_md( md_info, child->tbs.p, child->tbs.len, hash ) != 0 )
Andrzej Kurekd4a65532018-10-31 06:18:39 -04002441 return( -1 );
2442#else
Jaeden Amero34973232019-02-20 10:32:28 +00002443 psa_hash_operation_t hash_operation = PSA_HASH_OPERATION_INIT;
Andrzej Kurekd4a65532018-10-31 06:18:39 -04002444 psa_algorithm_t hash_alg = mbedtls_psa_translate_md( child->sig_md );
2445
2446 if( psa_hash_setup( &hash_operation, hash_alg ) != PSA_SUCCESS )
2447 return( -1 );
2448
2449 if( psa_hash_update( &hash_operation, child->tbs.p, child->tbs.len )
2450 != PSA_SUCCESS )
Manuel Pégourié-Gonnardf82a4d52017-07-03 19:26:25 +02002451 {
Manuel Pégourié-Gonnardf82a4d52017-07-03 19:26:25 +02002452 return( -1 );
2453 }
2454
Andrzej Kurekd4a65532018-10-31 06:18:39 -04002455 if( psa_hash_finish( &hash_operation, hash, sizeof( hash ), &hash_len )
2456 != PSA_SUCCESS )
2457 {
Andrzej Kurekd4a65532018-10-31 06:18:39 -04002458 return( -1 );
2459 }
2460#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002461 /* Skip expensive computation on obvious mismatch */
2462 if( ! mbedtls_pk_can_do( &parent->pk, child->sig_pk ) )
Manuel Pégourié-Gonnardf82a4d52017-07-03 19:26:25 +02002463 return( -1 );
Manuel Pégourié-Gonnardf82a4d52017-07-03 19:26:25 +02002464
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002465#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002466 if( rs_ctx != NULL && child->sig_pk == MBEDTLS_PK_ECDSA )
2467 {
2468 return( mbedtls_pk_verify_restartable( &parent->pk,
Andrzej Kurekd4a65532018-10-31 06:18:39 -04002469 child->sig_md, hash, hash_len,
Manuel Pégourié-Gonnard15d7df22017-08-17 14:33:31 +02002470 child->sig.p, child->sig.len, &rs_ctx->pk ) );
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002471 }
2472#else
2473 (void) rs_ctx;
2474#endif
2475
2476 return( mbedtls_pk_verify_ext( child->sig_pk, child->sig_opts, &parent->pk,
Andrzej Kurekd4a65532018-10-31 06:18:39 -04002477 child->sig_md, hash, hash_len,
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002478 child->sig.p, child->sig.len ) );
Manuel Pégourié-Gonnardf82a4d52017-07-03 19:26:25 +02002479}
2480
2481/*
Manuel Pégourié-Gonnard312010e2014-04-09 14:30:11 +02002482 * Check if 'parent' is a suitable parent (signing CA) for 'child'.
2483 * Return 0 if yes, -1 if not.
Manuel Pégourié-Gonnardd249b7a2014-06-24 11:49:16 +02002484 *
2485 * top means parent is a locally-trusted certificate
Manuel Pégourié-Gonnard3fed0b32014-04-08 13:18:01 +02002486 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002487static int x509_crt_check_parent( const mbedtls_x509_crt *child,
2488 const mbedtls_x509_crt *parent,
Manuel Pégourié-Gonnard27e94792017-07-04 00:49:31 +02002489 int top )
Manuel Pégourié-Gonnard3fed0b32014-04-08 13:18:01 +02002490{
Manuel Pégourié-Gonnardd249b7a2014-06-24 11:49:16 +02002491 int need_ca_bit;
2492
Manuel Pégourié-Gonnardc4eff162014-06-19 12:18:08 +02002493 /* Parent must be the issuer */
Manuel Pégourié-Gonnardef9a6ae2014-10-17 12:25:12 +02002494 if( x509_name_cmp( &child->issuer, &parent->subject ) != 0 )
Manuel Pégourié-Gonnard312010e2014-04-09 14:30:11 +02002495 return( -1 );
Manuel Pégourié-Gonnard3fed0b32014-04-08 13:18:01 +02002496
Manuel Pégourié-Gonnardd249b7a2014-06-24 11:49:16 +02002497 /* Parent must have the basicConstraints CA bit set as a general rule */
2498 need_ca_bit = 1;
2499
2500 /* Exception: v1/v2 certificates that are locally trusted. */
2501 if( top && parent->version < 3 )
2502 need_ca_bit = 0;
2503
Manuel Pégourié-Gonnardd249b7a2014-06-24 11:49:16 +02002504 if( need_ca_bit && ! parent->ca_istrue )
2505 return( -1 );
2506
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002507#if defined(MBEDTLS_X509_CHECK_KEY_USAGE)
Manuel Pégourié-Gonnardd249b7a2014-06-24 11:49:16 +02002508 if( need_ca_bit &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002509 mbedtls_x509_crt_check_key_usage( parent, MBEDTLS_X509_KU_KEY_CERT_SIGN ) != 0 )
Manuel Pégourié-Gonnardc4eff162014-06-19 12:18:08 +02002510 {
2511 return( -1 );
2512 }
Manuel Pégourié-Gonnard312010e2014-04-09 14:30:11 +02002513#endif
2514
2515 return( 0 );
Manuel Pégourié-Gonnard3fed0b32014-04-08 13:18:01 +02002516}
2517
Manuel Pégourié-Gonnard35407c72017-06-29 10:45:25 +02002518/*
Manuel Pégourié-Gonnard3e329b82017-06-29 12:55:27 +02002519 * Find a suitable parent for child in candidates, or return NULL.
2520 *
2521 * Here suitable is defined as:
Manuel Pégourié-Gonnard2f09d592017-07-03 18:30:43 +02002522 * 1. subject name matches child's issuer
2523 * 2. if necessary, the CA bit is set and key usage allows signing certs
2524 * 3. for trusted roots, the signature is correct
Manuel Pégourié-Gonnardbe4ff422017-07-14 12:04:14 +02002525 * (for intermediates, the signature is checked and the result reported)
Manuel Pégourié-Gonnard2f09d592017-07-03 18:30:43 +02002526 * 4. pathlen constraints are satisfied
Manuel Pégourié-Gonnard3e329b82017-06-29 12:55:27 +02002527 *
Manuel Pégourié-Gonnard562df402017-08-08 18:09:14 +02002528 * If there's a suitable candidate which is also time-valid, return the first
2529 * such. Otherwise, return the first suitable candidate (or NULL if there is
2530 * none).
Manuel Pégourié-Gonnard3e329b82017-06-29 12:55:27 +02002531 *
2532 * The rationale for this rule is that someone could have a list of trusted
2533 * roots with two versions on the same root with different validity periods.
2534 * (At least one user reported having such a list and wanted it to just work.)
2535 * The reason we don't just require time-validity is that generally there is
2536 * only one version, and if it's expired we want the flags to state that
2537 * rather than NOT_TRUSTED, as would be the case if we required it here.
Manuel Pégourié-Gonnard2f09d592017-07-03 18:30:43 +02002538 *
2539 * The rationale for rule 3 (signature for trusted roots) is that users might
2540 * have two versions of the same CA with different keys in their list, and the
Manuel Pégourié-Gonnardbdc54402017-07-04 00:33:39 +02002541 * way we select the correct one is by checking the signature (as we don't
2542 * rely on key identifier extensions). (This is one way users might choose to
2543 * handle key rollover, another relies on self-issued certs, see [SIRO].)
Manuel Pégourié-Gonnard98a67782017-08-17 10:52:20 +02002544 *
2545 * Arguments:
Manuel Pégourié-Gonnarde57d7432018-03-07 10:00:57 +01002546 * - [in] child: certificate for which we're looking for a parent
2547 * - [in] candidates: chained list of potential parents
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +02002548 * - [out] r_parent: parent found (or NULL)
2549 * - [out] r_signature_is_good: 1 if child signature by parent is valid, or 0
Manuel Pégourié-Gonnarde57d7432018-03-07 10:00:57 +01002550 * - [in] top: 1 if candidates consists of trusted roots, ie we're at the top
2551 * of the chain, 0 otherwise
2552 * - [in] path_cnt: number of intermediates seen so far
2553 * - [in] self_cnt: number of self-signed intermediates seen so far
2554 * (will never be greater than path_cnt)
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +02002555 * - [in-out] rs_ctx: context for restarting operations
Manuel Pégourié-Gonnarde57d7432018-03-07 10:00:57 +01002556 *
2557 * Return value:
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +02002558 * - 0 on success
2559 * - MBEDTLS_ERR_ECP_IN_PROGRESS otherwise
Manuel Pégourié-Gonnard2f1c33d2017-06-29 12:27:23 +02002560 */
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002561static int x509_crt_find_parent_in(
2562 mbedtls_x509_crt *child,
2563 mbedtls_x509_crt *candidates,
2564 mbedtls_x509_crt **r_parent,
2565 int *r_signature_is_good,
2566 int top,
Manuel Pégourié-Gonnardbb216bd2017-08-28 13:25:55 +02002567 unsigned path_cnt,
2568 unsigned self_cnt,
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002569 mbedtls_x509_crt_restart_ctx *rs_ctx )
Manuel Pégourié-Gonnard2f1c33d2017-06-29 12:27:23 +02002570{
Janos Follath865b3eb2019-12-16 11:46:15 +00002571 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002572 mbedtls_x509_crt *parent, *fallback_parent;
Benjamin Kier36050732019-05-30 14:49:17 -04002573 int signature_is_good = 0, fallback_signature_is_good;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002574
2575#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard3627a8b2017-08-23 11:20:48 +02002576 /* did we have something in progress? */
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002577 if( rs_ctx != NULL && rs_ctx->parent != NULL )
2578 {
Manuel Pégourié-Gonnard3627a8b2017-08-23 11:20:48 +02002579 /* restore saved state */
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002580 parent = rs_ctx->parent;
2581 fallback_parent = rs_ctx->fallback_parent;
Manuel Pégourié-Gonnard78d7e8c2018-07-02 12:33:14 +02002582 fallback_signature_is_good = rs_ctx->fallback_signature_is_good;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002583
Manuel Pégourié-Gonnard3627a8b2017-08-23 11:20:48 +02002584 /* clear saved state */
2585 rs_ctx->parent = NULL;
2586 rs_ctx->fallback_parent = NULL;
Manuel Pégourié-Gonnard78d7e8c2018-07-02 12:33:14 +02002587 rs_ctx->fallback_signature_is_good = 0;
Manuel Pégourié-Gonnard3627a8b2017-08-23 11:20:48 +02002588
2589 /* resume where we left */
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002590 goto check_signature;
2591 }
2592#endif
2593
2594 fallback_parent = NULL;
Manuel Pégourié-Gonnard78d7e8c2018-07-02 12:33:14 +02002595 fallback_signature_is_good = 0;
Manuel Pégourié-Gonnard2f1c33d2017-06-29 12:27:23 +02002596
Manuel Pégourié-Gonnard2f1c33d2017-06-29 12:27:23 +02002597 for( parent = candidates; parent != NULL; parent = parent->next )
2598 {
Manuel Pégourié-Gonnard2f09d592017-07-03 18:30:43 +02002599 /* basic parenting skills (name, CA bit, key usage) */
Manuel Pégourié-Gonnard27e94792017-07-04 00:49:31 +02002600 if( x509_crt_check_parent( child, parent, top ) != 0 )
Manuel Pégourié-Gonnard2f1c33d2017-06-29 12:27:23 +02002601 continue;
2602
Manuel Pégourié-Gonnard9c6118c2017-06-29 12:38:42 +02002603 /* +1 because stored max_pathlen is 1 higher that the actual value */
2604 if( parent->max_pathlen > 0 &&
Manuel Pégourié-Gonnardbb216bd2017-08-28 13:25:55 +02002605 (size_t) parent->max_pathlen < 1 + path_cnt - self_cnt )
Manuel Pégourié-Gonnard9c6118c2017-06-29 12:38:42 +02002606 {
2607 continue;
2608 }
2609
Manuel Pégourié-Gonnard2f09d592017-07-03 18:30:43 +02002610 /* Signature */
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002611#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
2612check_signature:
2613#endif
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002614 ret = x509_crt_check_signature( child, parent, rs_ctx );
2615
2616#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002617 if( rs_ctx != NULL && ret == MBEDTLS_ERR_ECP_IN_PROGRESS )
2618 {
2619 /* save state */
2620 rs_ctx->parent = parent;
2621 rs_ctx->fallback_parent = fallback_parent;
Manuel Pégourié-Gonnard78d7e8c2018-07-02 12:33:14 +02002622 rs_ctx->fallback_signature_is_good = fallback_signature_is_good;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002623
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002624 return( ret );
2625 }
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002626#else
2627 (void) ret;
2628#endif
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002629
2630 signature_is_good = ret == 0;
2631 if( top && ! signature_is_good )
Manuel Pégourié-Gonnardf82a4d52017-07-03 19:26:25 +02002632 continue;
Manuel Pégourié-Gonnard2f09d592017-07-03 18:30:43 +02002633
Manuel Pégourié-Gonnard562df402017-08-08 18:09:14 +02002634 /* optional time check */
Manuel Pégourié-Gonnard3e329b82017-06-29 12:55:27 +02002635 if( mbedtls_x509_time_is_past( &parent->valid_to ) ||
2636 mbedtls_x509_time_is_future( &parent->valid_from ) )
2637 {
Manuel Pégourié-Gonnardbe4ff422017-07-14 12:04:14 +02002638 if( fallback_parent == NULL )
2639 {
2640 fallback_parent = parent;
Manuel Pégourié-Gonnard78d7e8c2018-07-02 12:33:14 +02002641 fallback_signature_is_good = signature_is_good;
Manuel Pégourié-Gonnardbe4ff422017-07-14 12:04:14 +02002642 }
Manuel Pégourié-Gonnard3e329b82017-06-29 12:55:27 +02002643
2644 continue;
2645 }
2646
Andy Gross1f627142019-01-30 10:25:53 -06002647 *r_parent = parent;
2648 *r_signature_is_good = signature_is_good;
2649
Manuel Pégourié-Gonnard2f1c33d2017-06-29 12:27:23 +02002650 break;
2651 }
2652
Andy Gross1f627142019-01-30 10:25:53 -06002653 if( parent == NULL )
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002654 {
2655 *r_parent = fallback_parent;
Manuel Pégourié-Gonnard78d7e8c2018-07-02 12:33:14 +02002656 *r_signature_is_good = fallback_signature_is_good;
Manuel Pégourié-Gonnardbe4ff422017-07-14 12:04:14 +02002657 }
Manuel Pégourié-Gonnard3e329b82017-06-29 12:55:27 +02002658
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002659 return( 0 );
Manuel Pégourié-Gonnard2f1c33d2017-06-29 12:27:23 +02002660}
2661
2662/*
Manuel Pégourié-Gonnard63686122017-07-04 01:01:39 +02002663 * Find a parent in trusted CAs or the provided chain, or return NULL.
2664 *
2665 * Searches in trusted CAs first, and return the first suitable parent found
2666 * (see find_parent_in() for definition of suitable).
Manuel Pégourié-Gonnard98a67782017-08-17 10:52:20 +02002667 *
2668 * Arguments:
Manuel Pégourié-Gonnarde57d7432018-03-07 10:00:57 +01002669 * - [in] child: certificate for which we're looking for a parent, followed
2670 * by a chain of possible intermediates
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +02002671 * - [in] trust_ca: list of locally trusted certificates
2672 * - [out] parent: parent found (or NULL)
2673 * - [out] parent_is_trusted: 1 if returned `parent` is trusted, or 0
2674 * - [out] signature_is_good: 1 if child signature by parent is valid, or 0
2675 * - [in] path_cnt: number of links in the chain so far (EE -> ... -> child)
2676 * - [in] self_cnt: number of self-signed certs in the chain so far
Manuel Pégourié-Gonnarde57d7432018-03-07 10:00:57 +01002677 * (will always be no greater than path_cnt)
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +02002678 * - [in-out] rs_ctx: context for restarting operations
Manuel Pégourié-Gonnarde57d7432018-03-07 10:00:57 +01002679 *
2680 * Return value:
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +02002681 * - 0 on success
2682 * - MBEDTLS_ERR_ECP_IN_PROGRESS otherwise
Manuel Pégourié-Gonnard63686122017-07-04 01:01:39 +02002683 */
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002684static int x509_crt_find_parent(
2685 mbedtls_x509_crt *child,
2686 mbedtls_x509_crt *trust_ca,
2687 mbedtls_x509_crt **parent,
2688 int *parent_is_trusted,
2689 int *signature_is_good,
Manuel Pégourié-Gonnardbb216bd2017-08-28 13:25:55 +02002690 unsigned path_cnt,
2691 unsigned self_cnt,
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002692 mbedtls_x509_crt_restart_ctx *rs_ctx )
Manuel Pégourié-Gonnard63686122017-07-04 01:01:39 +02002693{
Janos Follath865b3eb2019-12-16 11:46:15 +00002694 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard18547b52017-08-14 16:11:43 +02002695 mbedtls_x509_crt *search_list;
Manuel Pégourié-Gonnard63686122017-07-04 01:01:39 +02002696
Manuel Pégourié-Gonnard63686122017-07-04 01:01:39 +02002697 *parent_is_trusted = 1;
Manuel Pégourié-Gonnard18547b52017-08-14 16:11:43 +02002698
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002699#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard3627a8b2017-08-23 11:20:48 +02002700 /* restore then clear saved state if we have some stored */
2701 if( rs_ctx != NULL && rs_ctx->parent_is_trusted != -1 )
2702 {
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002703 *parent_is_trusted = rs_ctx->parent_is_trusted;
Manuel Pégourié-Gonnard3627a8b2017-08-23 11:20:48 +02002704 rs_ctx->parent_is_trusted = -1;
2705 }
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002706#endif
2707
Manuel Pégourié-Gonnard18547b52017-08-14 16:11:43 +02002708 while( 1 ) {
2709 search_list = *parent_is_trusted ? trust_ca : child->next;
2710
2711 ret = x509_crt_find_parent_in( child, search_list,
2712 parent, signature_is_good,
2713 *parent_is_trusted,
2714 path_cnt, self_cnt, rs_ctx );
Manuel Pégourié-Gonnard63686122017-07-04 01:01:39 +02002715
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002716#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002717 if( rs_ctx != NULL && ret == MBEDTLS_ERR_ECP_IN_PROGRESS )
2718 {
2719 /* save state */
2720 rs_ctx->parent_is_trusted = *parent_is_trusted;
Manuel Pégourié-Gonnard18547b52017-08-14 16:11:43 +02002721 return( ret );
2722 }
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002723#else
2724 (void) ret;
2725#endif
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002726
Manuel Pégourié-Gonnard18547b52017-08-14 16:11:43 +02002727 /* stop here if found or already in second iteration */
2728 if( *parent != NULL || *parent_is_trusted == 0 )
2729 break;
Manuel Pégourié-Gonnard63686122017-07-04 01:01:39 +02002730
Manuel Pégourié-Gonnard18547b52017-08-14 16:11:43 +02002731 /* prepare second iteration */
2732 *parent_is_trusted = 0;
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002733 }
Manuel Pégourié-Gonnard18547b52017-08-14 16:11:43 +02002734
2735 /* extra precaution against mistakes in the caller */
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05002736 if( *parent == NULL )
Manuel Pégourié-Gonnard18547b52017-08-14 16:11:43 +02002737 {
Manuel Pégourié-Gonnarda5a3e402018-10-16 11:27:23 +02002738 *parent_is_trusted = 0;
2739 *signature_is_good = 0;
Manuel Pégourié-Gonnard18547b52017-08-14 16:11:43 +02002740 }
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002741
2742 return( 0 );
Manuel Pégourié-Gonnard63686122017-07-04 01:01:39 +02002743}
2744
2745/*
Manuel Pégourié-Gonnard27e94792017-07-04 00:49:31 +02002746 * Check if an end-entity certificate is locally trusted
2747 *
2748 * Currently we require such certificates to be self-signed (actually only
2749 * check for self-issued as self-signatures are not checked)
2750 */
2751static int x509_crt_check_ee_locally_trusted(
2752 mbedtls_x509_crt *crt,
2753 mbedtls_x509_crt *trust_ca )
2754{
2755 mbedtls_x509_crt *cur;
2756
2757 /* must be self-issued */
2758 if( x509_name_cmp( &crt->issuer, &crt->subject ) != 0 )
2759 return( -1 );
2760
2761 /* look for an exact match with trusted cert */
2762 for( cur = trust_ca; cur != NULL; cur = cur->next )
2763 {
2764 if( crt->raw.len == cur->raw.len &&
2765 memcmp( crt->raw.p, cur->raw.p, crt->raw.len ) == 0 )
2766 {
2767 return( 0 );
2768 }
2769 }
2770
2771 /* too bad */
2772 return( -1 );
2773}
2774
2775/*
Manuel Pégourié-Gonnardf86f4912017-07-05 16:43:44 +02002776 * Build and verify a certificate chain
Manuel Pégourié-Gonnard35407c72017-06-29 10:45:25 +02002777 *
Manuel Pégourié-Gonnardf86f4912017-07-05 16:43:44 +02002778 * Given a peer-provided list of certificates EE, C1, ..., Cn and
2779 * a list of trusted certs R1, ... Rp, try to build and verify a chain
Manuel Pégourié-Gonnard562df402017-08-08 18:09:14 +02002780 * EE, Ci1, ... Ciq [, Rj]
Manuel Pégourié-Gonnardf86f4912017-07-05 16:43:44 +02002781 * such that every cert in the chain is a child of the next one,
2782 * jumping to a trusted root as early as possible.
Manuel Pégourié-Gonnardbdc54402017-07-04 00:33:39 +02002783 *
Manuel Pégourié-Gonnardf86f4912017-07-05 16:43:44 +02002784 * Verify that chain and return it with flags for all issues found.
2785 *
2786 * Special cases:
2787 * - EE == Rj -> return a one-element list containing it
2788 * - EE, Ci1, ..., Ciq cannot be continued with a trusted root
2789 * -> return that chain with NOT_TRUSTED set on Ciq
Manuel Pégourié-Gonnardbdc54402017-07-04 00:33:39 +02002790 *
Manuel Pégourié-Gonnardd19a41d2017-07-14 11:05:59 +02002791 * Tests for (aspects of) this function should include at least:
2792 * - trusted EE
2793 * - EE -> trusted root
Antonin Décimo36e89b52019-01-23 15:24:37 +01002794 * - EE -> intermediate CA -> trusted root
Manuel Pégourié-Gonnardd19a41d2017-07-14 11:05:59 +02002795 * - if relevant: EE untrusted
2796 * - if relevant: EE -> intermediate, untrusted
2797 * with the aspect under test checked at each relevant level (EE, int, root).
2798 * For some aspects longer chains are required, but usually length 2 is
2799 * enough (but length 1 is not in general).
2800 *
Manuel Pégourié-Gonnardbdc54402017-07-04 00:33:39 +02002801 * Arguments:
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002802 * - [in] crt: the cert list EE, C1, ..., Cn
2803 * - [in] trust_ca: the trusted list R1, ..., Rp
2804 * - [in] ca_crl, profile: as in verify_with_profile()
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02002805 * - [out] ver_chain: the built and verified chain
Manuel Pégourié-Gonnarda9688432017-08-23 11:23:59 +02002806 * Only valid when return value is 0, may contain garbage otherwise!
2807 * Restart note: need not be the same when calling again to resume.
Manuel Pégourié-Gonnard98a67782017-08-17 10:52:20 +02002808 * - [in-out] rs_ctx: context for restarting operations
Manuel Pégourié-Gonnardf86f4912017-07-05 16:43:44 +02002809 *
2810 * Return value:
2811 * - non-zero if the chain could not be fully built and examined
2812 * - 0 is the chain was successfully built and examined,
2813 * even if it was found to be invalid
Manuel Pégourié-Gonnard35407c72017-06-29 10:45:25 +02002814 */
Manuel Pégourié-Gonnardbdc54402017-07-04 00:33:39 +02002815static int x509_crt_verify_chain(
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002816 mbedtls_x509_crt *crt,
2817 mbedtls_x509_crt *trust_ca,
2818 mbedtls_x509_crl *ca_crl,
Hanno Becker3116fb32019-03-28 13:34:42 +00002819 mbedtls_x509_crt_ca_cb_t f_ca_cb,
2820 void *p_ca_cb,
Manuel Pégourié-Gonnard95051642015-06-15 10:39:46 +02002821 const mbedtls_x509_crt_profile *profile,
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02002822 mbedtls_x509_crt_verify_chain *ver_chain,
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002823 mbedtls_x509_crt_restart_ctx *rs_ctx )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002824{
Manuel Pégourié-Gonnarda9688432017-08-23 11:23:59 +02002825 /* Don't initialize any of those variables here, so that the compiler can
2826 * catch potential issues with jumping ahead when restarting */
Janos Follath865b3eb2019-12-16 11:46:15 +00002827 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardf86f4912017-07-05 16:43:44 +02002828 uint32_t *flags;
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02002829 mbedtls_x509_crt_verify_chain_item *cur;
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002830 mbedtls_x509_crt *child;
Manuel Pégourié-Gonnard58dcd2d2017-07-03 21:35:04 +02002831 mbedtls_x509_crt *parent;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002832 int parent_is_trusted;
2833 int child_is_trusted;
2834 int signature_is_good;
Manuel Pégourié-Gonnardbb216bd2017-08-28 13:25:55 +02002835 unsigned self_cnt;
Hanno Beckerf53893b2019-03-28 13:45:55 +00002836 mbedtls_x509_crt *cur_trust_ca = NULL;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002837
2838#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
2839 /* resume if we had an operation in progress */
Manuel Pégourié-Gonnarddaf04912017-08-23 12:32:19 +02002840 if( rs_ctx != NULL && rs_ctx->in_progress == x509_crt_rs_find_parent )
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002841 {
Manuel Pégourié-Gonnard3627a8b2017-08-23 11:20:48 +02002842 /* restore saved state */
Manuel Pégourié-Gonnarda9688432017-08-23 11:23:59 +02002843 *ver_chain = rs_ctx->ver_chain; /* struct copy */
Manuel Pégourié-Gonnarddaf04912017-08-23 12:32:19 +02002844 self_cnt = rs_ctx->self_cnt;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002845
Manuel Pégourié-Gonnarddaf04912017-08-23 12:32:19 +02002846 /* restore derived state */
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002847 cur = &ver_chain->items[ver_chain->len - 1];
Manuel Pégourié-Gonnarddaf04912017-08-23 12:32:19 +02002848 child = cur->crt;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002849 flags = &cur->flags;
2850
2851 goto find_parent;
2852 }
2853#endif /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */
Manuel Pégourié-Gonnard8f8c2822017-07-03 21:25:10 +02002854
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002855 child = crt;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002856 self_cnt = 0;
2857 parent_is_trusted = 0;
2858 child_is_trusted = 0;
Manuel Pégourié-Gonnardf86f4912017-07-05 16:43:44 +02002859
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002860 while( 1 ) {
2861 /* Add certificate to the verification chain */
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02002862 cur = &ver_chain->items[ver_chain->len];
2863 cur->crt = child;
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +02002864 cur->flags = 0;
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02002865 ver_chain->len++;
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +02002866 flags = &cur->flags;
Manuel Pégourié-Gonnard66fac752017-07-03 21:39:21 +02002867
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002868 /* Check time-validity (all certificates) */
2869 if( mbedtls_x509_time_is_past( &child->valid_to ) )
2870 *flags |= MBEDTLS_X509_BADCERT_EXPIRED;
Manuel Pégourié-Gonnard66fac752017-07-03 21:39:21 +02002871
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002872 if( mbedtls_x509_time_is_future( &child->valid_from ) )
2873 *flags |= MBEDTLS_X509_BADCERT_FUTURE;
Manuel Pégourié-Gonnardcb396102017-07-04 00:00:24 +02002874
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002875 /* Stop here for trusted roots (but not for trusted EE certs) */
2876 if( child_is_trusted )
2877 return( 0 );
Manuel Pégourié-Gonnard66fac752017-07-03 21:39:21 +02002878
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002879 /* Check signature algorithm: MD & PK algs */
2880 if( x509_profile_check_md_alg( profile, child->sig_md ) != 0 )
2881 *flags |= MBEDTLS_X509_BADCERT_BAD_MD;
Manuel Pégourié-Gonnard66fac752017-07-03 21:39:21 +02002882
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002883 if( x509_profile_check_pk_alg( profile, child->sig_pk ) != 0 )
2884 *flags |= MBEDTLS_X509_BADCERT_BAD_PK;
Manuel Pégourié-Gonnard27e94792017-07-04 00:49:31 +02002885
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002886 /* Special case: EE certs that are locally trusted */
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02002887 if( ver_chain->len == 1 &&
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002888 x509_crt_check_ee_locally_trusted( child, trust_ca ) == 0 )
2889 {
2890 return( 0 );
2891 }
Manuel Pégourié-Gonnard8f8c2822017-07-03 21:25:10 +02002892
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002893#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
2894find_parent:
2895#endif
Hanno Beckerf53893b2019-03-28 13:45:55 +00002896
2897 /* Obtain list of potential trusted signers from CA callback,
2898 * or use statically provided list. */
2899#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
2900 if( f_ca_cb != NULL )
2901 {
2902 mbedtls_x509_crt_free( ver_chain->trust_ca_cb_result );
2903 mbedtls_free( ver_chain->trust_ca_cb_result );
2904 ver_chain->trust_ca_cb_result = NULL;
2905
2906 ret = f_ca_cb( p_ca_cb, child, &ver_chain->trust_ca_cb_result );
2907 if( ret != 0 )
2908 return( MBEDTLS_ERR_X509_FATAL_ERROR );
2909
2910 cur_trust_ca = ver_chain->trust_ca_cb_result;
2911 }
2912 else
2913#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
2914 {
2915 ((void) f_ca_cb);
2916 ((void) p_ca_cb);
2917 cur_trust_ca = trust_ca;
2918 }
2919
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002920 /* Look for a parent in trusted CAs or up the chain */
Hanno Beckerf53893b2019-03-28 13:45:55 +00002921 ret = x509_crt_find_parent( child, cur_trust_ca, &parent,
Manuel Pégourié-Gonnardbe4ff422017-07-14 12:04:14 +02002922 &parent_is_trusted, &signature_is_good,
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02002923 ver_chain->len - 1, self_cnt, rs_ctx );
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002924
2925#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002926 if( rs_ctx != NULL && ret == MBEDTLS_ERR_ECP_IN_PROGRESS )
2927 {
2928 /* save state */
Manuel Pégourié-Gonnarddaf04912017-08-23 12:32:19 +02002929 rs_ctx->in_progress = x509_crt_rs_find_parent;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002930 rs_ctx->self_cnt = self_cnt;
Manuel Pégourié-Gonnarda9688432017-08-23 11:23:59 +02002931 rs_ctx->ver_chain = *ver_chain; /* struct copy */
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002932
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002933 return( ret );
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002934 }
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002935#else
2936 (void) ret;
2937#endif
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002938
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002939 /* No parent? We're done here */
2940 if( parent == NULL )
2941 {
2942 *flags |= MBEDTLS_X509_BADCERT_NOT_TRUSTED;
2943 return( 0 );
2944 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002945
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002946 /* Count intermediate self-issued (not necessarily self-signed) certs.
2947 * These can occur with some strategies for key rollover, see [SIRO],
2948 * and should be excluded from max_pathlen checks. */
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02002949 if( ver_chain->len != 1 &&
Manuel Pégourié-Gonnard24611f92017-08-09 10:28:07 +02002950 x509_name_cmp( &child->issuer, &child->subject ) == 0 )
2951 {
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002952 self_cnt++;
Manuel Pégourié-Gonnard24611f92017-08-09 10:28:07 +02002953 }
Manuel Pégourié-Gonnardfd6c85c2014-11-20 16:34:20 +01002954
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002955 /* path_cnt is 0 for the first intermediate CA,
2956 * and if parent is trusted it's not an intermediate CA */
2957 if( ! parent_is_trusted &&
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02002958 ver_chain->len > MBEDTLS_X509_MAX_INTERMEDIATE_CA )
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002959 {
2960 /* return immediately to avoid overflow the chain array */
2961 return( MBEDTLS_ERR_X509_FATAL_ERROR );
2962 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002963
Manuel Pégourié-Gonnard98a67782017-08-17 10:52:20 +02002964 /* signature was checked while searching parent */
Manuel Pégourié-Gonnardbe4ff422017-07-14 12:04:14 +02002965 if( ! signature_is_good )
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002966 *flags |= MBEDTLS_X509_BADCERT_NOT_TRUSTED;
2967
2968 /* check size of signing key */
Manuel Pégourié-Gonnard3f816912017-10-26 10:24:16 +02002969 if( x509_profile_check_key( profile, &parent->pk ) != 0 )
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002970 *flags |= MBEDTLS_X509_BADCERT_BAD_KEY;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002971
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002972#if defined(MBEDTLS_X509_CRL_PARSE_C)
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002973 /* Check trusted CA's CRL for the given crt */
Manuel Pégourié-Gonnard562df402017-08-08 18:09:14 +02002974 *flags |= x509_crt_verifycrl( child, parent, ca_crl, profile );
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002975#else
2976 (void) ca_crl;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002977#endif
2978
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002979 /* prepare for next iteration */
2980 child = parent;
2981 parent = NULL;
2982 child_is_trusted = parent_is_trusted;
Manuel Pégourié-Gonnardbe4ff422017-07-14 12:04:14 +02002983 signature_is_good = 0;
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002984 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002985}
2986
2987/*
Manuel Pégourié-Gonnarda468eb12017-07-04 01:31:59 +02002988 * Check for CN match
2989 */
2990static int x509_crt_check_cn( const mbedtls_x509_buf *name,
2991 const char *cn, size_t cn_len )
2992{
2993 /* try exact match */
2994 if( name->len == cn_len &&
2995 x509_memcasecmp( cn, name->p, cn_len ) == 0 )
2996 {
2997 return( 0 );
2998 }
2999
3000 /* try wildcard match */
Manuel Pégourié-Gonnard900fba62017-10-18 14:28:11 +02003001 if( x509_check_wildcard( cn, name ) == 0 )
Manuel Pégourié-Gonnarda468eb12017-07-04 01:31:59 +02003002 {
3003 return( 0 );
3004 }
3005
3006 return( -1 );
3007}
3008
3009/*
Manuel Pégourié-Gonnardf3e4bd82020-07-21 13:22:41 +02003010 * Check for SAN match, see RFC 5280 Section 4.2.1.6
3011 */
3012static int x509_crt_check_san( const mbedtls_x509_buf *name,
3013 const char *cn, size_t cn_len )
3014{
3015 const unsigned char san_type = (unsigned char) name->tag &
3016 MBEDTLS_ASN1_TAG_VALUE_MASK;
3017
3018 /* dNSName */
3019 if( san_type == MBEDTLS_X509_SAN_DNS_NAME )
3020 return( x509_crt_check_cn( name, cn, cn_len ) );
3021
3022 /* (We may handle other types here later.) */
3023
3024 /* Unrecognized type */
3025 return( -1 );
3026}
3027
3028/*
Manuel Pégourié-Gonnard1300e992017-07-04 01:13:44 +02003029 * Verify the requested CN - only call this if cn is not NULL!
3030 */
Manuel Pégourié-Gonnarda468eb12017-07-04 01:31:59 +02003031static void x509_crt_verify_name( const mbedtls_x509_crt *crt,
Manuel Pégourié-Gonnard1300e992017-07-04 01:13:44 +02003032 const char *cn,
3033 uint32_t *flags )
3034{
Manuel Pégourié-Gonnarda468eb12017-07-04 01:31:59 +02003035 const mbedtls_x509_name *name;
3036 const mbedtls_x509_sequence *cur;
3037 size_t cn_len = strlen( cn );
Manuel Pégourié-Gonnard1300e992017-07-04 01:13:44 +02003038
3039 if( crt->ext_types & MBEDTLS_X509_EXT_SUBJECT_ALT_NAME )
3040 {
Manuel Pégourié-Gonnarda468eb12017-07-04 01:31:59 +02003041 for( cur = &crt->subject_alt_names; cur != NULL; cur = cur->next )
Manuel Pégourié-Gonnard1300e992017-07-04 01:13:44 +02003042 {
Manuel Pégourié-Gonnardf3e4bd82020-07-21 13:22:41 +02003043 if( x509_crt_check_san( &cur->buf, cn, cn_len ) == 0 )
Manuel Pégourié-Gonnard1300e992017-07-04 01:13:44 +02003044 break;
Manuel Pégourié-Gonnard1300e992017-07-04 01:13:44 +02003045 }
3046
3047 if( cur == NULL )
3048 *flags |= MBEDTLS_X509_BADCERT_CN_MISMATCH;
3049 }
3050 else
3051 {
Manuel Pégourié-Gonnard08eacec2017-10-18 14:20:24 +02003052 for( name = &crt->subject; name != NULL; name = name->next )
Manuel Pégourié-Gonnard1300e992017-07-04 01:13:44 +02003053 {
Manuel Pégourié-Gonnarda468eb12017-07-04 01:31:59 +02003054 if( MBEDTLS_OID_CMP( MBEDTLS_OID_AT_CN, &name->oid ) == 0 &&
3055 x509_crt_check_cn( &name->val, cn, cn_len ) == 0 )
Manuel Pégourié-Gonnard1300e992017-07-04 01:13:44 +02003056 {
Manuel Pégourié-Gonnarda468eb12017-07-04 01:31:59 +02003057 break;
Manuel Pégourié-Gonnard1300e992017-07-04 01:13:44 +02003058 }
Manuel Pégourié-Gonnard1300e992017-07-04 01:13:44 +02003059 }
3060
3061 if( name == NULL )
3062 *flags |= MBEDTLS_X509_BADCERT_CN_MISMATCH;
3063 }
3064}
3065
3066/*
Manuel Pégourié-Gonnarda707e1d2017-07-05 17:18:42 +02003067 * Merge the flags for all certs in the chain, after calling callback
3068 */
3069static int x509_crt_merge_flags_with_cb(
3070 uint32_t *flags,
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02003071 const mbedtls_x509_crt_verify_chain *ver_chain,
Manuel Pégourié-Gonnarda707e1d2017-07-05 17:18:42 +02003072 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
3073 void *p_vrfy )
3074{
Janos Follath865b3eb2019-12-16 11:46:15 +00003075 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardbb216bd2017-08-28 13:25:55 +02003076 unsigned i;
Manuel Pégourié-Gonnarda707e1d2017-07-05 17:18:42 +02003077 uint32_t cur_flags;
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02003078 const mbedtls_x509_crt_verify_chain_item *cur;
Manuel Pégourié-Gonnarda707e1d2017-07-05 17:18:42 +02003079
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02003080 for( i = ver_chain->len; i != 0; --i )
Manuel Pégourié-Gonnarda707e1d2017-07-05 17:18:42 +02003081 {
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02003082 cur = &ver_chain->items[i-1];
3083 cur_flags = cur->flags;
Manuel Pégourié-Gonnarda707e1d2017-07-05 17:18:42 +02003084
3085 if( NULL != f_vrfy )
Manuel Pégourié-Gonnardbb216bd2017-08-28 13:25:55 +02003086 if( ( ret = f_vrfy( p_vrfy, cur->crt, (int) i-1, &cur_flags ) ) != 0 )
Manuel Pégourié-Gonnarda707e1d2017-07-05 17:18:42 +02003087 return( ret );
3088
3089 *flags |= cur_flags;
3090 }
3091
3092 return( 0 );
3093}
3094
3095/*
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +02003096 * Verify the certificate validity, with profile, restartable version
3097 *
3098 * This function:
3099 * - checks the requested CN (if any)
3100 * - checks the type and size of the EE cert's key,
3101 * as that isn't done as part of chain building/verification currently
3102 * - builds and verifies the chain
3103 * - then calls the callback and merges the flags
Hanno Becker3116fb32019-03-28 13:34:42 +00003104 *
3105 * The parameters pairs `trust_ca`, `ca_crl` and `f_ca_cb`, `p_ca_cb`
3106 * are mutually exclusive: If `f_ca_cb != NULL`, it will be used by the
3107 * verification routine to search for trusted signers, and CRLs will
3108 * be disabled. Otherwise, `trust_ca` will be used as the static list
3109 * of trusted signers, and `ca_crl` will be use as the static list
3110 * of CRLs.
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +02003111 */
Jarno Lamsa2ee67a62019-04-01 14:59:33 +03003112static int x509_crt_verify_restartable_ca_cb( mbedtls_x509_crt *crt,
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +02003113 mbedtls_x509_crt *trust_ca,
3114 mbedtls_x509_crl *ca_crl,
Hanno Becker3116fb32019-03-28 13:34:42 +00003115 mbedtls_x509_crt_ca_cb_t f_ca_cb,
3116 void *p_ca_cb,
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +02003117 const mbedtls_x509_crt_profile *profile,
3118 const char *cn, uint32_t *flags,
3119 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
3120 void *p_vrfy,
3121 mbedtls_x509_crt_restart_ctx *rs_ctx )
3122{
Janos Follath865b3eb2019-12-16 11:46:15 +00003123 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard65eefc82015-10-23 14:08:48 +02003124 mbedtls_pk_type_t pk_type;
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02003125 mbedtls_x509_crt_verify_chain ver_chain;
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +02003126 uint32_t ee_flags;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003127
3128 *flags = 0;
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +02003129 ee_flags = 0;
3130 x509_crt_verify_chain_reset( &ver_chain );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003131
Manuel Pégourié-Gonnardd15795a2017-06-22 12:19:27 +02003132 if( profile == NULL )
3133 {
3134 ret = MBEDTLS_ERR_X509_BAD_INPUT_DATA;
3135 goto exit;
3136 }
3137
Manuel Pégourié-Gonnard1300e992017-07-04 01:13:44 +02003138 /* check name if requested */
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003139 if( cn != NULL )
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +02003140 x509_crt_verify_name( crt, cn, &ee_flags );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003141
Manuel Pégourié-Gonnard65eefc82015-10-23 14:08:48 +02003142 /* Check the type and size of the key */
3143 pk_type = mbedtls_pk_get_type( &crt->pk );
3144
3145 if( x509_profile_check_pk_alg( profile, pk_type ) != 0 )
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +02003146 ee_flags |= MBEDTLS_X509_BADCERT_BAD_PK;
Manuel Pégourié-Gonnard65eefc82015-10-23 14:08:48 +02003147
Manuel Pégourié-Gonnard3f816912017-10-26 10:24:16 +02003148 if( x509_profile_check_key( profile, &crt->pk ) != 0 )
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +02003149 ee_flags |= MBEDTLS_X509_BADCERT_BAD_KEY;
Manuel Pégourié-Gonnard65eefc82015-10-23 14:08:48 +02003150
Manuel Pégourié-Gonnardbdc54402017-07-04 00:33:39 +02003151 /* Check the chain */
Hanno Becker3116fb32019-03-28 13:34:42 +00003152 ret = x509_crt_verify_chain( crt, trust_ca, ca_crl,
3153 f_ca_cb, p_ca_cb, profile,
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02003154 &ver_chain, rs_ctx );
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02003155
Manuel Pégourié-Gonnardc547d1a2017-07-05 13:28:45 +02003156 if( ret != 0 )
3157 goto exit;
3158
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +02003159 /* Merge end-entity flags */
3160 ver_chain.items[0].flags |= ee_flags;
3161
Manuel Pégourié-Gonnarda707e1d2017-07-05 17:18:42 +02003162 /* Build final flags, calling callback on the way if any */
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02003163 ret = x509_crt_merge_flags_with_cb( flags, &ver_chain, f_vrfy, p_vrfy );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003164
Manuel Pégourié-Gonnardd15795a2017-06-22 12:19:27 +02003165exit:
Hanno Beckerf53893b2019-03-28 13:45:55 +00003166
3167#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
3168 mbedtls_x509_crt_free( ver_chain.trust_ca_cb_result );
3169 mbedtls_free( ver_chain.trust_ca_cb_result );
3170 ver_chain.trust_ca_cb_result = NULL;
3171#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
3172
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02003173#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
3174 if( rs_ctx != NULL && ret != MBEDTLS_ERR_ECP_IN_PROGRESS )
3175 mbedtls_x509_crt_restart_free( rs_ctx );
3176#endif
3177
Manuel Pégourié-Gonnard9107b5f2017-07-06 12:16:25 +02003178 /* prevent misuse of the vrfy callback - VERIFY_FAILED would be ignored by
3179 * the SSL module for authmode optional, but non-zero return from the
3180 * callback means a fatal error so it shouldn't be ignored */
Manuel Pégourié-Gonnard31458a12017-06-26 10:11:49 +02003181 if( ret == MBEDTLS_ERR_X509_CERT_VERIFY_FAILED )
3182 ret = MBEDTLS_ERR_X509_FATAL_ERROR;
3183
Manuel Pégourié-Gonnardd15795a2017-06-22 12:19:27 +02003184 if( ret != 0 )
3185 {
3186 *flags = (uint32_t) -1;
3187 return( ret );
3188 }
3189
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003190 if( *flags != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003191 return( MBEDTLS_ERR_X509_CERT_VERIFY_FAILED );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003192
3193 return( 0 );
3194}
3195
Hanno Becker3116fb32019-03-28 13:34:42 +00003196
3197/*
3198 * Verify the certificate validity (default profile, not restartable)
3199 */
3200int mbedtls_x509_crt_verify( mbedtls_x509_crt *crt,
3201 mbedtls_x509_crt *trust_ca,
3202 mbedtls_x509_crl *ca_crl,
3203 const char *cn, uint32_t *flags,
3204 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
3205 void *p_vrfy )
3206{
Jarno Lamsa2ee67a62019-04-01 14:59:33 +03003207 return( x509_crt_verify_restartable_ca_cb( crt, trust_ca, ca_crl,
Hanno Becker3116fb32019-03-28 13:34:42 +00003208 NULL, NULL,
3209 &mbedtls_x509_crt_profile_default,
3210 cn, flags,
3211 f_vrfy, p_vrfy, NULL ) );
3212}
3213
3214/*
3215 * Verify the certificate validity (user-chosen profile, not restartable)
3216 */
3217int mbedtls_x509_crt_verify_with_profile( mbedtls_x509_crt *crt,
3218 mbedtls_x509_crt *trust_ca,
3219 mbedtls_x509_crl *ca_crl,
3220 const mbedtls_x509_crt_profile *profile,
3221 const char *cn, uint32_t *flags,
3222 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
3223 void *p_vrfy )
3224{
Jarno Lamsa2ee67a62019-04-01 14:59:33 +03003225 return( x509_crt_verify_restartable_ca_cb( crt, trust_ca, ca_crl,
Hanno Becker3116fb32019-03-28 13:34:42 +00003226 NULL, NULL,
3227 profile, cn, flags,
3228 f_vrfy, p_vrfy, NULL ) );
3229}
3230
3231#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
3232/*
3233 * Verify the certificate validity (user-chosen profile, CA callback,
3234 * not restartable).
3235 */
Jarno Lamsa31d9db62019-04-01 14:33:49 +03003236int mbedtls_x509_crt_verify_with_ca_cb( mbedtls_x509_crt *crt,
Hanno Becker3116fb32019-03-28 13:34:42 +00003237 mbedtls_x509_crt_ca_cb_t f_ca_cb,
3238 void *p_ca_cb,
3239 const mbedtls_x509_crt_profile *profile,
3240 const char *cn, uint32_t *flags,
3241 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
3242 void *p_vrfy )
3243{
Jarno Lamsa2ee67a62019-04-01 14:59:33 +03003244 return( x509_crt_verify_restartable_ca_cb( crt, NULL, NULL,
Hanno Becker3116fb32019-03-28 13:34:42 +00003245 f_ca_cb, p_ca_cb,
3246 profile, cn, flags,
3247 f_vrfy, p_vrfy, NULL ) );
3248}
3249#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
3250
3251int mbedtls_x509_crt_verify_restartable( mbedtls_x509_crt *crt,
3252 mbedtls_x509_crt *trust_ca,
3253 mbedtls_x509_crl *ca_crl,
3254 const mbedtls_x509_crt_profile *profile,
3255 const char *cn, uint32_t *flags,
3256 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
3257 void *p_vrfy,
3258 mbedtls_x509_crt_restart_ctx *rs_ctx )
3259{
Jarno Lamsa2ee67a62019-04-01 14:59:33 +03003260 return( x509_crt_verify_restartable_ca_cb( crt, trust_ca, ca_crl,
Hanno Becker3116fb32019-03-28 13:34:42 +00003261 NULL, NULL,
3262 profile, cn, flags,
3263 f_vrfy, p_vrfy, rs_ctx ) );
3264}
3265
3266
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003267/*
Paul Bakker369d2eb2013-09-18 11:58:25 +02003268 * Initialize a certificate chain
3269 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003270void mbedtls_x509_crt_init( mbedtls_x509_crt *crt )
Paul Bakker369d2eb2013-09-18 11:58:25 +02003271{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003272 memset( crt, 0, sizeof(mbedtls_x509_crt) );
Paul Bakker369d2eb2013-09-18 11:58:25 +02003273}
3274
3275/*
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003276 * Unallocate all certificate data
3277 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003278void mbedtls_x509_crt_free( mbedtls_x509_crt *crt )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003279{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003280 mbedtls_x509_crt *cert_cur = crt;
3281 mbedtls_x509_crt *cert_prv;
3282 mbedtls_x509_name *name_cur;
3283 mbedtls_x509_name *name_prv;
3284 mbedtls_x509_sequence *seq_cur;
3285 mbedtls_x509_sequence *seq_prv;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003286
3287 if( crt == NULL )
3288 return;
3289
3290 do
3291 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003292 mbedtls_pk_free( &cert_cur->pk );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003293
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003294#if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT)
3295 mbedtls_free( cert_cur->sig_opts );
Manuel Pégourié-Gonnardf75f2f72014-06-05 15:14:28 +02003296#endif
3297
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003298 name_cur = cert_cur->issuer.next;
3299 while( name_cur != NULL )
3300 {
3301 name_prv = name_cur;
3302 name_cur = name_cur->next;
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05003303 mbedtls_platform_zeroize( name_prv, sizeof( mbedtls_x509_name ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003304 mbedtls_free( name_prv );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003305 }
3306
3307 name_cur = cert_cur->subject.next;
3308 while( name_cur != NULL )
3309 {
3310 name_prv = name_cur;
3311 name_cur = name_cur->next;
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05003312 mbedtls_platform_zeroize( name_prv, sizeof( mbedtls_x509_name ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003313 mbedtls_free( name_prv );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003314 }
3315
3316 seq_cur = cert_cur->ext_key_usage.next;
3317 while( seq_cur != NULL )
3318 {
3319 seq_prv = seq_cur;
3320 seq_cur = seq_cur->next;
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05003321 mbedtls_platform_zeroize( seq_prv,
3322 sizeof( mbedtls_x509_sequence ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003323 mbedtls_free( seq_prv );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003324 }
3325
3326 seq_cur = cert_cur->subject_alt_names.next;
3327 while( seq_cur != NULL )
3328 {
3329 seq_prv = seq_cur;
3330 seq_cur = seq_cur->next;
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05003331 mbedtls_platform_zeroize( seq_prv,
3332 sizeof( mbedtls_x509_sequence ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003333 mbedtls_free( seq_prv );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003334 }
3335
Ron Eldor74d9acc2019-03-21 14:00:03 +02003336 seq_cur = cert_cur->certificate_policies.next;
3337 while( seq_cur != NULL )
3338 {
3339 seq_prv = seq_cur;
3340 seq_cur = seq_cur->next;
3341 mbedtls_platform_zeroize( seq_prv,
3342 sizeof( mbedtls_x509_sequence ) );
3343 mbedtls_free( seq_prv );
3344 }
3345
Hanno Becker1a65dcd2019-01-31 08:57:44 +00003346 if( cert_cur->raw.p != NULL && cert_cur->own_buffer )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003347 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05003348 mbedtls_platform_zeroize( cert_cur->raw.p, cert_cur->raw.len );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003349 mbedtls_free( cert_cur->raw.p );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003350 }
3351
3352 cert_cur = cert_cur->next;
3353 }
3354 while( cert_cur != NULL );
3355
3356 cert_cur = crt;
3357 do
3358 {
3359 cert_prv = cert_cur;
3360 cert_cur = cert_cur->next;
3361
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05003362 mbedtls_platform_zeroize( cert_prv, sizeof( mbedtls_x509_crt ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003363 if( cert_prv != crt )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003364 mbedtls_free( cert_prv );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003365 }
3366 while( cert_cur != NULL );
3367}
3368
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +02003369#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
3370/*
3371 * Initialize a restart context
3372 */
3373void mbedtls_x509_crt_restart_init( mbedtls_x509_crt_restart_ctx *ctx )
3374{
Manuel Pégourié-Gonnard15d7df22017-08-17 14:33:31 +02003375 mbedtls_pk_restart_init( &ctx->pk );
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02003376
3377 ctx->parent = NULL;
3378 ctx->fallback_parent = NULL;
Manuel Pégourié-Gonnard78d7e8c2018-07-02 12:33:14 +02003379 ctx->fallback_signature_is_good = 0;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02003380
3381 ctx->parent_is_trusted = -1;
3382
Manuel Pégourié-Gonnarddaf04912017-08-23 12:32:19 +02003383 ctx->in_progress = x509_crt_rs_none;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02003384 ctx->self_cnt = 0;
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +02003385 x509_crt_verify_chain_reset( &ctx->ver_chain );
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +02003386}
3387
3388/*
3389 * Free the components of a restart context
3390 */
3391void mbedtls_x509_crt_restart_free( mbedtls_x509_crt_restart_ctx *ctx )
3392{
3393 if( ctx == NULL )
3394 return;
3395
Manuel Pégourié-Gonnard15d7df22017-08-17 14:33:31 +02003396 mbedtls_pk_restart_free( &ctx->pk );
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02003397 mbedtls_x509_crt_restart_init( ctx );
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +02003398}
3399#endif /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */
3400
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003401#endif /* MBEDTLS_X509_CRT_PARSE_C */