blob: 04822e8abd6e39b6aef77f36b5221320b388088c [file] [log] [blame]
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001/*
Manuel Pégourié-Gonnard1c082f32014-06-12 22:34:55 +02002 * X.509 certificate parsing and verification
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003 *
Manuel Pégourié-Gonnard6fb81872015-07-27 11:11:48 +02004 * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
Manuel Pégourié-Gonnard37ff1402015-09-04 14:21:07 +02005 * SPDX-License-Identifier: Apache-2.0
6 *
7 * Licensed under the Apache License, Version 2.0 (the "License"); you may
8 * not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
15 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
Paul Bakker7c6b2c32013-09-16 13:49:26 +020018 *
Manuel Pégourié-Gonnardfe446432015-03-06 13:17:10 +000019 * This file is part of mbed TLS (https://tls.mbed.org)
Paul Bakker7c6b2c32013-09-16 13:49:26 +020020 */
21/*
22 * The ITU-T X.509 standard defines a certificate format for PKI.
23 *
Manuel Pégourié-Gonnard1c082f32014-06-12 22:34:55 +020024 * http://www.ietf.org/rfc/rfc5280.txt (Certificates and CRLs)
25 * http://www.ietf.org/rfc/rfc3279.txt (Alg IDs for CRLs)
26 * http://www.ietf.org/rfc/rfc2986.txt (CSRs, aka PKCS#10)
Paul Bakker7c6b2c32013-09-16 13:49:26 +020027 *
28 * http://www.itu.int/ITU-T/studygroups/com17/languages/X.680-0207.pdf
29 * http://www.itu.int/ITU-T/studygroups/com17/languages/X.690-0207.pdf
Manuel Pégourié-Gonnard2f09d592017-07-03 18:30:43 +020030 *
31 * [SIRO] https://cabforum.org/wp-content/uploads/Chunghwatelecom201503cabforumV4.pdf
Paul Bakker7c6b2c32013-09-16 13:49:26 +020032 */
33
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020034#if !defined(MBEDTLS_CONFIG_FILE)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000035#include "mbedtls/config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020036#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020037#include MBEDTLS_CONFIG_FILE
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020038#endif
Paul Bakker7c6b2c32013-09-16 13:49:26 +020039
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020040#if defined(MBEDTLS_X509_CRT_PARSE_C)
Paul Bakker7c6b2c32013-09-16 13:49:26 +020041
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000042#include "mbedtls/x509_crt.h"
Janos Follath73c616b2019-12-18 15:07:04 +000043#include "mbedtls/error.h"
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000044#include "mbedtls/oid.h"
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050045#include "mbedtls/platform_util.h"
Rich Evans00ab4702015-02-06 13:43:58 +000046
Rich Evans00ab4702015-02-06 13:43:58 +000047#include <string.h>
48
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020049#if defined(MBEDTLS_PEM_PARSE_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000050#include "mbedtls/pem.h"
Paul Bakker7c6b2c32013-09-16 13:49:26 +020051#endif
52
Andrzej Kurekd4a65532018-10-31 06:18:39 -040053#if defined(MBEDTLS_USE_PSA_CRYPTO)
54#include "psa/crypto.h"
55#include "mbedtls/psa_util.h"
56#endif
57
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020058#if defined(MBEDTLS_PLATFORM_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000059#include "mbedtls/platform.h"
Paul Bakker7c6b2c32013-09-16 13:49:26 +020060#else
Simon Butcherd2642582018-10-03 15:11:19 +010061#include <stdio.h>
Rich Evans00ab4702015-02-06 13:43:58 +000062#include <stdlib.h>
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020063#define mbedtls_free free
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +020064#define mbedtls_calloc calloc
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020065#define mbedtls_snprintf snprintf
Paul Bakker7c6b2c32013-09-16 13:49:26 +020066#endif
67
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020068#if defined(MBEDTLS_THREADING_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000069#include "mbedtls/threading.h"
Manuel Pégourié-Gonnard5ad68e42013-11-28 17:11:54 +010070#endif
71
Paul Bakkerfa6a6202013-10-28 18:48:30 +010072#if defined(_WIN32) && !defined(EFIX64) && !defined(EFI32)
Paul Bakker7c6b2c32013-09-16 13:49:26 +020073#include <windows.h>
74#else
75#include <time.h>
76#endif
77
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020078#if defined(MBEDTLS_FS_IO)
Rich Evans00ab4702015-02-06 13:43:58 +000079#include <stdio.h>
Paul Bakker5ff3f912014-04-04 15:08:20 +020080#if !defined(_WIN32) || defined(EFIX64) || defined(EFI32)
Paul Bakker7c6b2c32013-09-16 13:49:26 +020081#include <sys/types.h>
82#include <sys/stat.h>
83#include <dirent.h>
Rich Evans00ab4702015-02-06 13:43:58 +000084#endif /* !_WIN32 || EFIX64 || EFI32 */
Paul Bakker7c6b2c32013-09-16 13:49:26 +020085#endif
86
Manuel Pégourié-Gonnardc547d1a2017-07-05 13:28:45 +020087/*
88 * Item in a verification chain: cert and flags for it
89 */
90typedef struct {
91 mbedtls_x509_crt *crt;
92 uint32_t flags;
93} x509_crt_verify_chain_item;
94
95/*
96 * Max size of verification chain: end-entity + intermediates + trusted root
97 */
98#define X509_MAX_VERIFY_CHAIN_SIZE ( MBEDTLS_X509_MAX_INTERMEDIATE_CA + 2 )
Paul Bakker34617722014-06-13 17:20:13 +020099
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200100/*
Manuel Pégourié-Gonnard88db5da2015-06-15 14:34:59 +0200101 * Default profile
102 */
Manuel Pégourié-Gonnard88db5da2015-06-15 14:34:59 +0200103const mbedtls_x509_crt_profile mbedtls_x509_crt_profile_default =
104{
Gilles Peskine5d2511c2017-05-12 13:16:40 +0200105#if defined(MBEDTLS_TLS_DEFAULT_ALLOW_SHA1_IN_CERTIFICATES)
Gilles Peskine5e79cb32017-05-04 16:17:21 +0200106 /* Allow SHA-1 (weak, but still safe in controlled environments) */
Manuel Pégourié-Gonnardf8ea8562015-06-15 15:33:19 +0200107 MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_SHA1 ) |
Gilles Peskine5e79cb32017-05-04 16:17:21 +0200108#endif
109 /* Only SHA-2 hashes */
Manuel Pégourié-Gonnardf8ea8562015-06-15 15:33:19 +0200110 MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_SHA224 ) |
111 MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_SHA256 ) |
112 MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_SHA384 ) |
113 MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_SHA512 ),
114 0xFFFFFFF, /* Any PK alg */
115 0xFFFFFFF, /* Any curve */
Manuel Pégourié-Gonnard88db5da2015-06-15 14:34:59 +0200116 2048,
117};
118
119/*
120 * Next-default profile
121 */
Manuel Pégourié-Gonnard88db5da2015-06-15 14:34:59 +0200122const mbedtls_x509_crt_profile mbedtls_x509_crt_profile_next =
123{
Manuel Pégourié-Gonnardf8ea8562015-06-15 15:33:19 +0200124 /* Hashes from SHA-256 and above */
125 MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_SHA256 ) |
126 MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_SHA384 ) |
127 MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_SHA512 ),
128 0xFFFFFFF, /* Any PK alg */
129#if defined(MBEDTLS_ECP_C)
130 /* Curves at or above 128-bit security level */
131 MBEDTLS_X509_ID_FLAG( MBEDTLS_ECP_DP_SECP256R1 ) |
132 MBEDTLS_X509_ID_FLAG( MBEDTLS_ECP_DP_SECP384R1 ) |
133 MBEDTLS_X509_ID_FLAG( MBEDTLS_ECP_DP_SECP521R1 ) |
134 MBEDTLS_X509_ID_FLAG( MBEDTLS_ECP_DP_BP256R1 ) |
135 MBEDTLS_X509_ID_FLAG( MBEDTLS_ECP_DP_BP384R1 ) |
136 MBEDTLS_X509_ID_FLAG( MBEDTLS_ECP_DP_BP512R1 ) |
137 MBEDTLS_X509_ID_FLAG( MBEDTLS_ECP_DP_SECP256K1 ),
138#else
139 0,
140#endif
Manuel Pégourié-Gonnard88db5da2015-06-15 14:34:59 +0200141 2048,
142};
143
144/*
145 * NSA Suite B Profile
146 */
Manuel Pégourié-Gonnard88db5da2015-06-15 14:34:59 +0200147const mbedtls_x509_crt_profile mbedtls_x509_crt_profile_suiteb =
148{
Manuel Pégourié-Gonnardf8ea8562015-06-15 15:33:19 +0200149 /* Only SHA-256 and 384 */
150 MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_SHA256 ) |
151 MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_SHA384 ),
152 /* Only ECDSA */
Ron Eldor85e1dcf2018-02-06 15:59:38 +0200153 MBEDTLS_X509_ID_FLAG( MBEDTLS_PK_ECDSA ) |
154 MBEDTLS_X509_ID_FLAG( MBEDTLS_PK_ECKEY ),
Manuel Pégourié-Gonnardf8ea8562015-06-15 15:33:19 +0200155#if defined(MBEDTLS_ECP_C)
156 /* Only NIST P-256 and P-384 */
157 MBEDTLS_X509_ID_FLAG( MBEDTLS_ECP_DP_SECP256R1 ) |
158 MBEDTLS_X509_ID_FLAG( MBEDTLS_ECP_DP_SECP384R1 ),
159#else
160 0,
161#endif
162 0,
Manuel Pégourié-Gonnard88db5da2015-06-15 14:34:59 +0200163};
164
165/*
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200166 * Check md_alg against profile
Manuel Pégourié-Gonnard3f816912017-10-26 10:24:16 +0200167 * Return 0 if md_alg is acceptable for this profile, -1 otherwise
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200168 */
169static int x509_profile_check_md_alg( const mbedtls_x509_crt_profile *profile,
170 mbedtls_md_type_t md_alg )
171{
Philippe Antoineb5b25432018-05-11 11:06:29 +0200172 if( md_alg == MBEDTLS_MD_NONE )
173 return( -1 );
174
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200175 if( ( profile->allowed_mds & MBEDTLS_X509_ID_FLAG( md_alg ) ) != 0 )
176 return( 0 );
177
178 return( -1 );
179}
180
181/*
182 * Check pk_alg against profile
Manuel Pégourié-Gonnard3f816912017-10-26 10:24:16 +0200183 * Return 0 if pk_alg is acceptable for this profile, -1 otherwise
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200184 */
185static int x509_profile_check_pk_alg( const mbedtls_x509_crt_profile *profile,
186 mbedtls_pk_type_t pk_alg )
187{
Philippe Antoineb5b25432018-05-11 11:06:29 +0200188 if( pk_alg == MBEDTLS_PK_NONE )
189 return( -1 );
190
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200191 if( ( profile->allowed_pks & MBEDTLS_X509_ID_FLAG( pk_alg ) ) != 0 )
192 return( 0 );
193
194 return( -1 );
195}
196
197/*
198 * Check key against profile
Manuel Pégourié-Gonnard3f816912017-10-26 10:24:16 +0200199 * Return 0 if pk is acceptable for this profile, -1 otherwise
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200200 */
201static int x509_profile_check_key( const mbedtls_x509_crt_profile *profile,
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200202 const mbedtls_pk_context *pk )
203{
Manuel Pégourié-Gonnard3f816912017-10-26 10:24:16 +0200204 const mbedtls_pk_type_t pk_alg = mbedtls_pk_get_type( pk );
Manuel Pégourié-Gonnard19773ff2017-10-24 10:51:26 +0200205
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200206#if defined(MBEDTLS_RSA_C)
207 if( pk_alg == MBEDTLS_PK_RSA || pk_alg == MBEDTLS_PK_RSASSA_PSS )
208 {
Manuel Pégourié-Gonnard097c7bb2015-06-18 16:43:38 +0200209 if( mbedtls_pk_get_bitlen( pk ) >= profile->rsa_min_bitlen )
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200210 return( 0 );
211
212 return( -1 );
213 }
214#endif
215
Manuel Pégourié-Gonnard65eefc82015-10-23 14:08:48 +0200216#if defined(MBEDTLS_ECP_C)
217 if( pk_alg == MBEDTLS_PK_ECDSA ||
218 pk_alg == MBEDTLS_PK_ECKEY ||
219 pk_alg == MBEDTLS_PK_ECKEY_DH )
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200220 {
Manuel Pégourié-Gonnard3f816912017-10-26 10:24:16 +0200221 const mbedtls_ecp_group_id gid = mbedtls_pk_ec( *pk )->grp.id;
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200222
Philippe Antoineb5b25432018-05-11 11:06:29 +0200223 if( gid == MBEDTLS_ECP_DP_NONE )
224 return( -1 );
225
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200226 if( ( profile->allowed_curves & MBEDTLS_X509_ID_FLAG( gid ) ) != 0 )
227 return( 0 );
228
229 return( -1 );
230 }
231#endif
232
233 return( -1 );
234}
235
236/*
Hanno Becker1f8527f2018-11-02 09:19:16 +0000237 * Like memcmp, but case-insensitive and always returns -1 if different
238 */
239static int x509_memcasecmp( const void *s1, const void *s2, size_t len )
240{
241 size_t i;
242 unsigned char diff;
243 const unsigned char *n1 = s1, *n2 = s2;
244
245 for( i = 0; i < len; i++ )
246 {
247 diff = n1[i] ^ n2[i];
248
249 if( diff == 0 )
250 continue;
251
252 if( diff == 32 &&
253 ( ( n1[i] >= 'a' && n1[i] <= 'z' ) ||
254 ( n1[i] >= 'A' && n1[i] <= 'Z' ) ) )
255 {
256 continue;
257 }
258
259 return( -1 );
260 }
261
262 return( 0 );
263}
264
265/*
266 * Return 0 if name matches wildcard, -1 otherwise
267 */
268static int x509_check_wildcard( const char *cn, const mbedtls_x509_buf *name )
269{
270 size_t i;
271 size_t cn_idx = 0, cn_len = strlen( cn );
272
273 /* We can't have a match if there is no wildcard to match */
274 if( name->len < 3 || name->p[0] != '*' || name->p[1] != '.' )
275 return( -1 );
276
277 for( i = 0; i < cn_len; ++i )
278 {
279 if( cn[i] == '.' )
280 {
281 cn_idx = i;
282 break;
283 }
284 }
285
286 if( cn_idx == 0 )
287 return( -1 );
288
289 if( cn_len - cn_idx == name->len - 1 &&
290 x509_memcasecmp( name->p + 1, cn + cn_idx, name->len - 1 ) == 0 )
291 {
292 return( 0 );
293 }
294
295 return( -1 );
296}
297
298/*
299 * Compare two X.509 strings, case-insensitive, and allowing for some encoding
300 * variations (but not all).
301 *
302 * Return 0 if equal, -1 otherwise.
303 */
304static int x509_string_cmp( const mbedtls_x509_buf *a, const mbedtls_x509_buf *b )
305{
306 if( a->tag == b->tag &&
307 a->len == b->len &&
308 memcmp( a->p, b->p, b->len ) == 0 )
309 {
310 return( 0 );
311 }
312
313 if( ( a->tag == MBEDTLS_ASN1_UTF8_STRING || a->tag == MBEDTLS_ASN1_PRINTABLE_STRING ) &&
314 ( b->tag == MBEDTLS_ASN1_UTF8_STRING || b->tag == MBEDTLS_ASN1_PRINTABLE_STRING ) &&
315 a->len == b->len &&
316 x509_memcasecmp( a->p, b->p, b->len ) == 0 )
317 {
318 return( 0 );
319 }
320
321 return( -1 );
322}
323
324/*
325 * Compare two X.509 Names (aka rdnSequence).
326 *
327 * See RFC 5280 section 7.1, though we don't implement the whole algorithm:
328 * we sometimes return unequal when the full algorithm would return equal,
329 * but never the other way. (In particular, we don't do Unicode normalisation
330 * or space folding.)
331 *
332 * Return 0 if equal, -1 otherwise.
333 */
334static int x509_name_cmp( const mbedtls_x509_name *a, const mbedtls_x509_name *b )
335{
336 /* Avoid recursion, it might not be optimised by the compiler */
337 while( a != NULL || b != NULL )
338 {
339 if( a == NULL || b == NULL )
340 return( -1 );
341
342 /* type */
343 if( a->oid.tag != b->oid.tag ||
344 a->oid.len != b->oid.len ||
345 memcmp( a->oid.p, b->oid.p, b->oid.len ) != 0 )
346 {
347 return( -1 );
348 }
349
350 /* value */
351 if( x509_string_cmp( &a->val, &b->val ) != 0 )
352 return( -1 );
353
354 /* structure of the list of sets */
355 if( a->next_merged != b->next_merged )
356 return( -1 );
357
358 a = a->next;
359 b = b->next;
360 }
361
362 /* a == NULL == b */
363 return( 0 );
364}
365
366/*
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +0200367 * Reset (init or clear) a verify_chain
368 */
369static void x509_crt_verify_chain_reset(
370 mbedtls_x509_crt_verify_chain *ver_chain )
371{
372 size_t i;
373
374 for( i = 0; i < MBEDTLS_X509_MAX_VERIFY_CHAIN_SIZE; i++ )
375 {
376 ver_chain->items[i].crt = NULL;
Hanno Beckera9375b32019-01-10 09:19:26 +0000377 ver_chain->items[i].flags = (uint32_t) -1;
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +0200378 }
379
380 ver_chain->len = 0;
Hanno Beckerf53893b2019-03-28 13:45:55 +0000381
382#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
383 ver_chain->trust_ca_cb_result = NULL;
384#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +0200385}
386
387/*
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200388 * Version ::= INTEGER { v1(0), v2(1), v3(2) }
389 */
390static int x509_get_version( unsigned char **p,
391 const unsigned char *end,
392 int *ver )
393{
Janos Follath865b3eb2019-12-16 11:46:15 +0000394 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200395 size_t len;
396
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200397 if( ( ret = mbedtls_asn1_get_tag( p, end, &len,
398 MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_CONSTRUCTED | 0 ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200399 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200400 if( ret == MBEDTLS_ERR_ASN1_UNEXPECTED_TAG )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200401 {
402 *ver = 0;
403 return( 0 );
404 }
405
Hanno Becker6ccfb182019-02-12 11:52:10 +0000406 return( MBEDTLS_ERR_X509_INVALID_FORMAT + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200407 }
408
409 end = *p + len;
410
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200411 if( ( ret = mbedtls_asn1_get_int( p, end, ver ) ) != 0 )
412 return( MBEDTLS_ERR_X509_INVALID_VERSION + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200413
414 if( *p != end )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200415 return( MBEDTLS_ERR_X509_INVALID_VERSION +
416 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200417
418 return( 0 );
419}
420
421/*
422 * Validity ::= SEQUENCE {
423 * notBefore Time,
424 * notAfter Time }
425 */
426static int x509_get_dates( unsigned char **p,
427 const unsigned char *end,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200428 mbedtls_x509_time *from,
429 mbedtls_x509_time *to )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200430{
Janos Follath865b3eb2019-12-16 11:46:15 +0000431 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200432 size_t len;
433
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200434 if( ( ret = mbedtls_asn1_get_tag( p, end, &len,
435 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
436 return( MBEDTLS_ERR_X509_INVALID_DATE + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200437
438 end = *p + len;
439
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200440 if( ( ret = mbedtls_x509_get_time( p, end, from ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200441 return( ret );
442
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200443 if( ( ret = mbedtls_x509_get_time( p, end, to ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200444 return( ret );
445
446 if( *p != end )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200447 return( MBEDTLS_ERR_X509_INVALID_DATE +
448 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200449
450 return( 0 );
451}
452
453/*
454 * X.509 v2/v3 unique identifier (not parsed)
455 */
456static int x509_get_uid( unsigned char **p,
457 const unsigned char *end,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200458 mbedtls_x509_buf *uid, int n )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200459{
Janos Follath865b3eb2019-12-16 11:46:15 +0000460 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200461
462 if( *p == end )
463 return( 0 );
464
465 uid->tag = **p;
466
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200467 if( ( ret = mbedtls_asn1_get_tag( p, end, &uid->len,
468 MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_CONSTRUCTED | n ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200469 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200470 if( ret == MBEDTLS_ERR_ASN1_UNEXPECTED_TAG )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200471 return( 0 );
472
Hanno Becker6ccfb182019-02-12 11:52:10 +0000473 return( MBEDTLS_ERR_X509_INVALID_FORMAT + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200474 }
475
476 uid->p = *p;
477 *p += uid->len;
478
479 return( 0 );
480}
481
482static int x509_get_basic_constraints( unsigned char **p,
483 const unsigned char *end,
484 int *ca_istrue,
485 int *max_pathlen )
486{
Janos Follath865b3eb2019-12-16 11:46:15 +0000487 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200488 size_t len;
489
490 /*
491 * BasicConstraints ::= SEQUENCE {
492 * cA BOOLEAN DEFAULT FALSE,
493 * pathLenConstraint INTEGER (0..MAX) OPTIONAL }
494 */
495 *ca_istrue = 0; /* DEFAULT FALSE */
496 *max_pathlen = 0; /* endless */
497
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200498 if( ( ret = mbedtls_asn1_get_tag( p, end, &len,
499 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
500 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200501
502 if( *p == end )
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200503 return( 0 );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200504
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200505 if( ( ret = mbedtls_asn1_get_bool( p, end, ca_istrue ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200506 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200507 if( ret == MBEDTLS_ERR_ASN1_UNEXPECTED_TAG )
508 ret = mbedtls_asn1_get_int( p, end, ca_istrue );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200509
510 if( ret != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200511 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200512
513 if( *ca_istrue != 0 )
514 *ca_istrue = 1;
515 }
516
517 if( *p == end )
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200518 return( 0 );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200519
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200520 if( ( ret = mbedtls_asn1_get_int( p, end, max_pathlen ) ) != 0 )
521 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200522
523 if( *p != end )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200524 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS +
525 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200526
Andrzej Kurek16050742020-04-14 09:49:52 -0400527 /* Do not accept max_pathlen equal to INT_MAX to avoid a signed integer
528 * overflow, which is an undefined behavior. */
529 if( *max_pathlen == INT_MAX )
530 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS +
531 MBEDTLS_ERR_ASN1_INVALID_LENGTH );
532
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200533 (*max_pathlen)++;
534
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200535 return( 0 );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200536}
537
538static int x509_get_ns_cert_type( unsigned char **p,
539 const unsigned char *end,
540 unsigned char *ns_cert_type)
541{
Janos Follath865b3eb2019-12-16 11:46:15 +0000542 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200543 mbedtls_x509_bitstring bs = { 0, 0, NULL };
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200544
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200545 if( ( ret = mbedtls_asn1_get_bitstring( p, end, &bs ) ) != 0 )
546 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200547
548 if( bs.len != 1 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200549 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS +
550 MBEDTLS_ERR_ASN1_INVALID_LENGTH );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200551
552 /* Get actual bitstring */
553 *ns_cert_type = *bs.p;
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200554 return( 0 );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200555}
556
557static int x509_get_key_usage( unsigned char **p,
558 const unsigned char *end,
Manuel Pégourié-Gonnard1d0ca1a2015-03-27 16:50:00 +0100559 unsigned int *key_usage)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200560{
Janos Follath865b3eb2019-12-16 11:46:15 +0000561 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard9a702252015-06-23 10:14:36 +0200562 size_t i;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200563 mbedtls_x509_bitstring bs = { 0, 0, NULL };
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200564
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200565 if( ( ret = mbedtls_asn1_get_bitstring( p, end, &bs ) ) != 0 )
566 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200567
568 if( bs.len < 1 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200569 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS +
570 MBEDTLS_ERR_ASN1_INVALID_LENGTH );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200571
572 /* Get actual bitstring */
Manuel Pégourié-Gonnard9a702252015-06-23 10:14:36 +0200573 *key_usage = 0;
574 for( i = 0; i < bs.len && i < sizeof( unsigned int ); i++ )
575 {
576 *key_usage |= (unsigned int) bs.p[i] << (8*i);
577 }
578
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200579 return( 0 );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200580}
581
582/*
583 * ExtKeyUsageSyntax ::= SEQUENCE SIZE (1..MAX) OF KeyPurposeId
584 *
585 * KeyPurposeId ::= OBJECT IDENTIFIER
586 */
587static int x509_get_ext_key_usage( unsigned char **p,
588 const unsigned char *end,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200589 mbedtls_x509_sequence *ext_key_usage)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200590{
Janos Follath865b3eb2019-12-16 11:46:15 +0000591 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200592
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200593 if( ( ret = mbedtls_asn1_get_sequence_of( p, end, ext_key_usage, MBEDTLS_ASN1_OID ) ) != 0 )
594 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200595
596 /* Sequence length must be >= 1 */
597 if( ext_key_usage->buf.p == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200598 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS +
599 MBEDTLS_ERR_ASN1_INVALID_LENGTH );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200600
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200601 return( 0 );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200602}
603
604/*
605 * SubjectAltName ::= GeneralNames
606 *
607 * GeneralNames ::= SEQUENCE SIZE (1..MAX) OF GeneralName
608 *
609 * GeneralName ::= CHOICE {
610 * otherName [0] OtherName,
611 * rfc822Name [1] IA5String,
612 * dNSName [2] IA5String,
613 * x400Address [3] ORAddress,
614 * directoryName [4] Name,
615 * ediPartyName [5] EDIPartyName,
616 * uniformResourceIdentifier [6] IA5String,
617 * iPAddress [7] OCTET STRING,
618 * registeredID [8] OBJECT IDENTIFIER }
619 *
620 * OtherName ::= SEQUENCE {
621 * type-id OBJECT IDENTIFIER,
622 * value [0] EXPLICIT ANY DEFINED BY type-id }
623 *
624 * EDIPartyName ::= SEQUENCE {
625 * nameAssigner [0] DirectoryString OPTIONAL,
626 * partyName [1] DirectoryString }
627 *
Ron Eldorc8b5f3f2019-05-15 15:15:55 +0300628 * NOTE: we list all types, but only use dNSName and otherName
629 * of type HwModuleName, as defined in RFC 4108, at this point.
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200630 */
631static int x509_get_subject_alt_name( unsigned char **p,
632 const unsigned char *end,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200633 mbedtls_x509_sequence *subject_alt_name )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200634{
Janos Follath865b3eb2019-12-16 11:46:15 +0000635 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200636 size_t len, tag_len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200637 mbedtls_asn1_buf *buf;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200638 unsigned char tag;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200639 mbedtls_asn1_sequence *cur = subject_alt_name;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200640
641 /* Get main sequence tag */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200642 if( ( ret = mbedtls_asn1_get_tag( p, end, &len,
643 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
644 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200645
646 if( *p + len != end )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200647 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS +
648 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200649
650 while( *p < end )
651 {
Ron Eldordbbd9662019-05-15 15:46:03 +0300652 mbedtls_x509_subject_alternative_name dummy_san_buf;
653 memset( &dummy_san_buf, 0, sizeof( dummy_san_buf ) );
654
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200655 tag = **p;
656 (*p)++;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200657 if( ( ret = mbedtls_asn1_get_len( p, end, &tag_len ) ) != 0 )
658 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200659
Andres Amaya Garcia849bc652017-08-25 17:13:12 +0100660 if( ( tag & MBEDTLS_ASN1_TAG_CLASS_MASK ) !=
661 MBEDTLS_ASN1_CONTEXT_SPECIFIC )
662 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200663 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS +
664 MBEDTLS_ERR_ASN1_UNEXPECTED_TAG );
Andres Amaya Garcia849bc652017-08-25 17:13:12 +0100665 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200666
Ron Eldordbbd9662019-05-15 15:46:03 +0300667 /*
irwir74aee1c2019-09-21 18:21:48 +0300668 * Check that the SAN is structured correctly.
Ron Eldordbbd9662019-05-15 15:46:03 +0300669 */
670 ret = mbedtls_x509_parse_subject_alt_name( &(cur->buf), &dummy_san_buf );
671 /*
672 * In case the extension is malformed, return an error,
673 * and clear the allocated sequences.
674 */
675 if( ret != 0 && ret != MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE )
676 {
677 mbedtls_x509_sequence *seq_cur = subject_alt_name->next;
678 mbedtls_x509_sequence *seq_prv;
679 while( seq_cur != NULL )
680 {
681 seq_prv = seq_cur;
682 seq_cur = seq_cur->next;
683 mbedtls_platform_zeroize( seq_prv,
684 sizeof( mbedtls_x509_sequence ) );
685 mbedtls_free( seq_prv );
686 }
Ron Eldor5aebeeb2019-05-22 16:41:21 +0300687 subject_alt_name->next = NULL;
Ron Eldordbbd9662019-05-15 15:46:03 +0300688 return( ret );
689 }
690
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200691 /* Allocate and assign next pointer */
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +0200692 if( cur->buf.p != NULL )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200693 {
Manuel Pégourié-Gonnardb1340602014-11-11 23:11:16 +0100694 if( cur->next != NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200695 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS );
Manuel Pégourié-Gonnardb1340602014-11-11 23:11:16 +0100696
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200697 cur->next = mbedtls_calloc( 1, sizeof( mbedtls_asn1_sequence ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200698
699 if( cur->next == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200700 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS +
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200701 MBEDTLS_ERR_ASN1_ALLOC_FAILED );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200702
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200703 cur = cur->next;
704 }
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +0200705
706 buf = &(cur->buf);
707 buf->tag = tag;
708 buf->p = *p;
709 buf->len = tag_len;
710 *p += buf->len;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200711 }
712
713 /* Set final sequence entry's next pointer to NULL */
714 cur->next = NULL;
715
716 if( *p != end )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200717 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS +
718 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200719
720 return( 0 );
721}
722
723/*
Ron Eldor74d9acc2019-03-21 14:00:03 +0200724 * id-ce-certificatePolicies OBJECT IDENTIFIER ::= { id-ce 32 }
725 *
726 * anyPolicy OBJECT IDENTIFIER ::= { id-ce-certificatePolicies 0 }
727 *
728 * certificatePolicies ::= SEQUENCE SIZE (1..MAX) OF PolicyInformation
729 *
730 * PolicyInformation ::= SEQUENCE {
731 * policyIdentifier CertPolicyId,
732 * policyQualifiers SEQUENCE SIZE (1..MAX) OF
733 * PolicyQualifierInfo OPTIONAL }
734 *
735 * CertPolicyId ::= OBJECT IDENTIFIER
736 *
737 * PolicyQualifierInfo ::= SEQUENCE {
738 * policyQualifierId PolicyQualifierId,
739 * qualifier ANY DEFINED BY policyQualifierId }
740 *
741 * -- policyQualifierIds for Internet policy qualifiers
742 *
743 * id-qt OBJECT IDENTIFIER ::= { id-pkix 2 }
744 * id-qt-cps OBJECT IDENTIFIER ::= { id-qt 1 }
745 * id-qt-unotice OBJECT IDENTIFIER ::= { id-qt 2 }
746 *
747 * PolicyQualifierId ::= OBJECT IDENTIFIER ( id-qt-cps | id-qt-unotice )
748 *
749 * Qualifier ::= CHOICE {
750 * cPSuri CPSuri,
751 * userNotice UserNotice }
752 *
753 * CPSuri ::= IA5String
754 *
755 * UserNotice ::= SEQUENCE {
756 * noticeRef NoticeReference OPTIONAL,
757 * explicitText DisplayText OPTIONAL }
758 *
759 * NoticeReference ::= SEQUENCE {
760 * organization DisplayText,
761 * noticeNumbers SEQUENCE OF INTEGER }
762 *
763 * DisplayText ::= CHOICE {
764 * ia5String IA5String (SIZE (1..200)),
765 * visibleString VisibleString (SIZE (1..200)),
766 * bmpString BMPString (SIZE (1..200)),
767 * utf8String UTF8String (SIZE (1..200)) }
768 *
769 * NOTE: we only parse and use anyPolicy without qualifiers at this point
770 * as defined in RFC 5280.
771 */
772static int x509_get_certificate_policies( unsigned char **p,
773 const unsigned char *end,
774 mbedtls_x509_sequence *certificate_policies )
775{
Ron Eldor8b0c3c92019-05-15 12:20:00 +0300776 int ret, parse_ret = 0;
Ron Eldor74d9acc2019-03-21 14:00:03 +0200777 size_t len;
778 mbedtls_asn1_buf *buf;
779 mbedtls_asn1_sequence *cur = certificate_policies;
780
781 /* Get main sequence tag */
782 ret = mbedtls_asn1_get_tag( p, end, &len,
783 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE );
784 if( ret != 0 )
785 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS + ret );
786
787 if( *p + len != end )
788 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS +
789 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
790
791 /*
792 * Cannot be an empty sequence.
793 */
794 if( len == 0 )
795 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS +
796 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
797
798 while( *p < end )
799 {
800 mbedtls_x509_buf policy_oid;
801 const unsigned char *policy_end;
802
803 /*
804 * Get the policy sequence
805 */
806 if( ( ret = mbedtls_asn1_get_tag( p, end, &len,
807 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
808 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS + ret );
809
810 policy_end = *p + len;
811
Ron Eldor08063792019-05-13 16:38:39 +0300812 if( ( ret = mbedtls_asn1_get_tag( p, policy_end, &len,
Ron Eldor74d9acc2019-03-21 14:00:03 +0200813 MBEDTLS_ASN1_OID ) ) != 0 )
814 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS + ret );
815
816 policy_oid.tag = MBEDTLS_ASN1_OID;
817 policy_oid.len = len;
818 policy_oid.p = *p;
819
Ron Eldor8b0c3c92019-05-15 12:20:00 +0300820 /*
821 * Only AnyPolicy is currently supported when enforcing policy.
822 */
823 if( MBEDTLS_OID_CMP( MBEDTLS_OID_ANY_POLICY, &policy_oid ) != 0 )
824 {
825 /*
826 * Set the parsing return code but continue parsing, in case this
827 * extension is critical and MBEDTLS_X509_ALLOW_UNSUPPORTED_CRITICAL_EXTENSION
828 * is configured.
829 */
830 parse_ret = MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE;
831 }
832
Ron Eldor74d9acc2019-03-21 14:00:03 +0200833 /* Allocate and assign next pointer */
834 if( cur->buf.p != NULL )
835 {
836 if( cur->next != NULL )
837 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS );
838
839 cur->next = mbedtls_calloc( 1, sizeof( mbedtls_asn1_sequence ) );
840
841 if( cur->next == NULL )
842 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS +
843 MBEDTLS_ERR_ASN1_ALLOC_FAILED );
844
845 cur = cur->next;
846 }
847
848 buf = &( cur->buf );
849 buf->tag = policy_oid.tag;
850 buf->p = policy_oid.p;
851 buf->len = policy_oid.len;
Ron Eldor08063792019-05-13 16:38:39 +0300852
853 *p += len;
854
855 /*
856 * If there is an optional qualifier, then *p < policy_end
857 * Check the Qualifier len to verify it doesn't exceed policy_end.
858 */
859 if( *p < policy_end )
860 {
861 if( ( ret = mbedtls_asn1_get_tag( p, policy_end, &len,
862 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
863 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS + ret );
864 /*
865 * Skip the optional policy qualifiers.
866 */
867 *p += len;
868 }
869
870 if( *p != policy_end )
871 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS +
872 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
Ron Eldor74d9acc2019-03-21 14:00:03 +0200873 }
874
875 /* Set final sequence entry's next pointer to NULL */
876 cur->next = NULL;
877
878 if( *p != end )
879 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS +
880 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
881
Ron Eldor8b0c3c92019-05-15 12:20:00 +0300882 return( parse_ret );
Ron Eldor74d9acc2019-03-21 14:00:03 +0200883}
884
885/*
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200886 * X.509 v3 extensions
887 *
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200888 */
889static int x509_get_crt_ext( unsigned char **p,
890 const unsigned char *end,
Nicola Di Lieto502d4b42020-04-25 14:41:25 +0200891 mbedtls_x509_crt *crt,
Nicola Di Lieto5659e7e2020-05-28 23:41:38 +0200892 mbedtls_x509_crt_ext_cb_t cb,
893 void *p_ctx )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200894{
Janos Follath865b3eb2019-12-16 11:46:15 +0000895 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200896 size_t len;
Nicola Di Lietoc84b1e62020-06-13 11:08:16 +0200897 unsigned char *end_ext_data, *start_ext_octet, *end_ext_octet;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200898
Hanno Becker12f62fb2019-02-12 17:22:36 +0000899 if( *p == end )
900 return( 0 );
901
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200902 if( ( ret = mbedtls_x509_get_ext( p, end, &crt->v3_ext, 3 ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200903 return( ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200904
Hanno Becker12f62fb2019-02-12 17:22:36 +0000905 end = crt->v3_ext.p + crt->v3_ext.len;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200906 while( *p < end )
907 {
908 /*
909 * Extension ::= SEQUENCE {
910 * extnID OBJECT IDENTIFIER,
911 * critical BOOLEAN DEFAULT FALSE,
912 * extnValue OCTET STRING }
913 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200914 mbedtls_x509_buf extn_oid = {0, 0, NULL};
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200915 int is_critical = 0; /* DEFAULT FALSE */
916 int ext_type = 0;
917
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200918 if( ( ret = mbedtls_asn1_get_tag( p, end, &len,
919 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
920 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200921
922 end_ext_data = *p + len;
923
924 /* Get extension ID */
k-stachowiakdcae78a2018-06-28 16:32:54 +0200925 if( ( ret = mbedtls_asn1_get_tag( p, end_ext_data, &extn_oid.len,
k-stachowiak463928a2018-07-24 12:50:59 +0200926 MBEDTLS_ASN1_OID ) ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200927 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200928
k-stachowiak463928a2018-07-24 12:50:59 +0200929 extn_oid.tag = MBEDTLS_ASN1_OID;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200930 extn_oid.p = *p;
931 *p += extn_oid.len;
932
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200933 /* Get optional critical */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200934 if( ( ret = mbedtls_asn1_get_bool( p, end_ext_data, &is_critical ) ) != 0 &&
935 ( ret != MBEDTLS_ERR_ASN1_UNEXPECTED_TAG ) )
936 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200937
938 /* Data should be octet string type */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200939 if( ( ret = mbedtls_asn1_get_tag( p, end_ext_data, &len,
940 MBEDTLS_ASN1_OCTET_STRING ) ) != 0 )
941 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200942
Nicola Di Lietoc84b1e62020-06-13 11:08:16 +0200943 start_ext_octet = *p;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200944 end_ext_octet = *p + len;
945
946 if( end_ext_octet != end_ext_data )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200947 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS +
948 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200949
950 /*
951 * Detect supported extensions
952 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200953 ret = mbedtls_oid_get_x509_ext_type( &extn_oid, &ext_type );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200954
955 if( ret != 0 )
956 {
Nicola Di Lieto502d4b42020-04-25 14:41:25 +0200957 /* Give the callback (if any) a chance to handle the extension */
Nicola Di Lieto2c3a9172020-05-28 17:20:42 +0200958 if( cb != NULL )
959 {
Nicola Di Lieto5659e7e2020-05-28 23:41:38 +0200960 ret = cb( p_ctx, crt, &extn_oid, is_critical, *p, end_ext_octet );
Nicola Di Lieto565b52b2020-05-29 22:46:56 +0200961 if( ret != 0 && is_critical )
962 return( ret );
Nicola Di Lietofae25a12020-05-28 08:55:08 +0200963 *p = end_ext_octet;
Nicola Di Lieto502d4b42020-04-25 14:41:25 +0200964 continue;
Nicola Di Lietofae25a12020-05-28 08:55:08 +0200965 }
Nicola Di Lieto502d4b42020-04-25 14:41:25 +0200966
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200967 /* No parser found, skip extension */
968 *p = end_ext_octet;
969
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200970#if !defined(MBEDTLS_X509_ALLOW_UNSUPPORTED_CRITICAL_EXTENSION)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200971 if( is_critical )
972 {
973 /* Data is marked as critical: fail */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200974 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS +
975 MBEDTLS_ERR_ASN1_UNEXPECTED_TAG );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200976 }
977#endif
978 continue;
979 }
980
Manuel Pégourié-Gonnard8a5e3d42014-11-12 17:47:28 +0100981 /* Forbid repeated extensions */
982 if( ( crt->ext_types & ext_type ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200983 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS );
Manuel Pégourié-Gonnard8a5e3d42014-11-12 17:47:28 +0100984
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200985 crt->ext_types |= ext_type;
986
987 switch( ext_type )
988 {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +0100989 case MBEDTLS_X509_EXT_BASIC_CONSTRAINTS:
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200990 /* Parse basic constraints */
991 if( ( ret = x509_get_basic_constraints( p, end_ext_octet,
992 &crt->ca_istrue, &crt->max_pathlen ) ) != 0 )
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200993 return( ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200994 break;
995
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200996 case MBEDTLS_X509_EXT_KEY_USAGE:
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200997 /* Parse key usage */
998 if( ( ret = x509_get_key_usage( p, end_ext_octet,
999 &crt->key_usage ) ) != 0 )
Paul Bakkerd8bb8262014-06-17 14:06:49 +02001000 return( ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001001 break;
1002
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001003 case MBEDTLS_X509_EXT_EXTENDED_KEY_USAGE:
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001004 /* Parse extended key usage */
1005 if( ( ret = x509_get_ext_key_usage( p, end_ext_octet,
1006 &crt->ext_key_usage ) ) != 0 )
Paul Bakkerd8bb8262014-06-17 14:06:49 +02001007 return( ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001008 break;
1009
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01001010 case MBEDTLS_X509_EXT_SUBJECT_ALT_NAME:
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001011 /* Parse subject alt name */
1012 if( ( ret = x509_get_subject_alt_name( p, end_ext_octet,
1013 &crt->subject_alt_names ) ) != 0 )
Paul Bakkerd8bb8262014-06-17 14:06:49 +02001014 return( ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001015 break;
1016
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001017 case MBEDTLS_X509_EXT_NS_CERT_TYPE:
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001018 /* Parse netscape certificate type */
1019 if( ( ret = x509_get_ns_cert_type( p, end_ext_octet,
1020 &crt->ns_cert_type ) ) != 0 )
Paul Bakkerd8bb8262014-06-17 14:06:49 +02001021 return( ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001022 break;
1023
Ron Eldor74d9acc2019-03-21 14:00:03 +02001024 case MBEDTLS_OID_X509_EXT_CERTIFICATE_POLICIES:
1025 /* Parse certificate policies type */
1026 if( ( ret = x509_get_certificate_policies( p, end_ext_octet,
1027 &crt->certificate_policies ) ) != 0 )
Ron Eldor8b0c3c92019-05-15 12:20:00 +03001028 {
Nicola Di Lietoc84b1e62020-06-13 11:08:16 +02001029 /* Give the callback (if any) a chance to handle the extension
1030 * if it contains unsupported policies */
1031 if( ret == MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE && cb != NULL &&
1032 cb( p_ctx, crt, &extn_oid, is_critical,
1033 start_ext_octet, end_ext_octet ) == 0 )
1034 break;
1035
Ron Eldor8b0c3c92019-05-15 12:20:00 +03001036#if !defined(MBEDTLS_X509_ALLOW_UNSUPPORTED_CRITICAL_EXTENSION)
1037 if( is_critical )
1038 return( ret );
1039 else
1040#endif
1041 /*
Ron Eldora2913912019-05-16 16:17:38 +03001042 * If MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE is returned, then we
1043 * cannot interpret or enforce the policy. However, it is up to
1044 * the user to choose how to enforce the policies,
Ron Eldor8b0c3c92019-05-15 12:20:00 +03001045 * unless the extension is critical.
1046 */
1047 if( ret != MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE )
1048 return( ret );
1049 }
Ron Eldor74d9acc2019-03-21 14:00:03 +02001050 break;
1051
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001052 default:
Ron Eldordf48efa2019-04-08 13:28:24 +03001053 /*
1054 * If this is a non-critical extension, which the oid layer
1055 * supports, but there isn't an x509 parser for it,
1056 * skip the extension.
1057 */
1058#if !defined(MBEDTLS_X509_ALLOW_UNSUPPORTED_CRITICAL_EXTENSION)
1059 if( is_critical )
1060 return( MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE );
1061 else
1062#endif
1063 *p = end_ext_octet;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001064 }
1065 }
1066
1067 if( *p != end )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001068 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS +
1069 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001070
1071 return( 0 );
1072}
1073
1074/*
1075 * Parse and fill a single X.509 certificate in DER format
1076 */
Hanno Becker1a65dcd2019-01-31 08:57:44 +00001077static int x509_crt_parse_der_core( mbedtls_x509_crt *crt,
1078 const unsigned char *buf,
1079 size_t buflen,
Nicola Di Lieto502d4b42020-04-25 14:41:25 +02001080 int make_copy,
Nicola Di Lieto5659e7e2020-05-28 23:41:38 +02001081 mbedtls_x509_crt_ext_cb_t cb,
1082 void *p_ctx )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001083{
Janos Follath865b3eb2019-12-16 11:46:15 +00001084 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001085 size_t len;
1086 unsigned char *p, *end, *crt_end;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001087 mbedtls_x509_buf sig_params1, sig_params2, sig_oid2;
Manuel Pégourié-Gonnard59a75d52014-01-22 10:12:57 +01001088
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001089 memset( &sig_params1, 0, sizeof( mbedtls_x509_buf ) );
1090 memset( &sig_params2, 0, sizeof( mbedtls_x509_buf ) );
1091 memset( &sig_oid2, 0, sizeof( mbedtls_x509_buf ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001092
1093 /*
1094 * Check for valid input
1095 */
1096 if( crt == NULL || buf == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001097 return( MBEDTLS_ERR_X509_BAD_INPUT_DATA );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001098
Hanno Becker1a65dcd2019-01-31 08:57:44 +00001099 /* Use the original buffer until we figure out actual length. */
Janos Follathcc0e49d2016-02-17 14:34:12 +00001100 p = (unsigned char*) buf;
1101 len = buflen;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001102 end = p + len;
1103
1104 /*
1105 * Certificate ::= SEQUENCE {
1106 * tbsCertificate TBSCertificate,
1107 * signatureAlgorithm AlgorithmIdentifier,
1108 * signatureValue BIT STRING }
1109 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001110 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
1111 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001112 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001113 mbedtls_x509_crt_free( crt );
1114 return( MBEDTLS_ERR_X509_INVALID_FORMAT );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001115 }
1116
Janos Follathcc0e49d2016-02-17 14:34:12 +00001117 end = crt_end = p + len;
Hanno Becker1a65dcd2019-01-31 08:57:44 +00001118 crt->raw.len = crt_end - buf;
1119 if( make_copy != 0 )
1120 {
1121 /* Create and populate a new buffer for the raw field. */
1122 crt->raw.p = p = mbedtls_calloc( 1, crt->raw.len );
1123 if( crt->raw.p == NULL )
1124 return( MBEDTLS_ERR_X509_ALLOC_FAILED );
1125
1126 memcpy( crt->raw.p, buf, crt->raw.len );
1127 crt->own_buffer = 1;
1128
1129 p += crt->raw.len - len;
1130 end = crt_end = p + len;
1131 }
1132 else
1133 {
1134 crt->raw.p = (unsigned char*) buf;
1135 crt->own_buffer = 0;
1136 }
Janos Follathcc0e49d2016-02-17 14:34:12 +00001137
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001138 /*
1139 * TBSCertificate ::= SEQUENCE {
1140 */
1141 crt->tbs.p = p;
1142
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001143 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
1144 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001145 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001146 mbedtls_x509_crt_free( crt );
1147 return( MBEDTLS_ERR_X509_INVALID_FORMAT + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001148 }
1149
1150 end = p + len;
1151 crt->tbs.len = end - crt->tbs.p;
1152
1153 /*
1154 * Version ::= INTEGER { v1(0), v2(1), v3(2) }
1155 *
1156 * CertificateSerialNumber ::= INTEGER
1157 *
1158 * signature AlgorithmIdentifier
1159 */
1160 if( ( ret = x509_get_version( &p, end, &crt->version ) ) != 0 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001161 ( ret = mbedtls_x509_get_serial( &p, end, &crt->serial ) ) != 0 ||
1162 ( ret = mbedtls_x509_get_alg( &p, end, &crt->sig_oid,
Manuel Pégourié-Gonnarddddbb1d2014-06-05 17:02:24 +02001163 &sig_params1 ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001164 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001165 mbedtls_x509_crt_free( crt );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001166 return( ret );
1167 }
1168
Andres AG7ca4a032017-03-09 16:16:11 +00001169 if( crt->version < 0 || crt->version > 2 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001170 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001171 mbedtls_x509_crt_free( crt );
1172 return( MBEDTLS_ERR_X509_UNKNOWN_VERSION );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001173 }
1174
Andres AG7ca4a032017-03-09 16:16:11 +00001175 crt->version++;
1176
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001177 if( ( ret = mbedtls_x509_get_sig_alg( &crt->sig_oid, &sig_params1,
Manuel Pégourié-Gonnardf75f2f72014-06-05 15:14:28 +02001178 &crt->sig_md, &crt->sig_pk,
1179 &crt->sig_opts ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001180 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001181 mbedtls_x509_crt_free( crt );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001182 return( ret );
1183 }
1184
1185 /*
1186 * issuer Name
1187 */
1188 crt->issuer_raw.p = p;
1189
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001190 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
1191 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001192 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001193 mbedtls_x509_crt_free( crt );
1194 return( MBEDTLS_ERR_X509_INVALID_FORMAT + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001195 }
1196
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001197 if( ( ret = mbedtls_x509_get_name( &p, p + len, &crt->issuer ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001198 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001199 mbedtls_x509_crt_free( crt );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001200 return( ret );
1201 }
1202
1203 crt->issuer_raw.len = p - crt->issuer_raw.p;
1204
1205 /*
1206 * Validity ::= SEQUENCE {
1207 * notBefore Time,
1208 * notAfter Time }
1209 *
1210 */
1211 if( ( ret = x509_get_dates( &p, end, &crt->valid_from,
1212 &crt->valid_to ) ) != 0 )
1213 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001214 mbedtls_x509_crt_free( crt );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001215 return( ret );
1216 }
1217
1218 /*
1219 * subject Name
1220 */
1221 crt->subject_raw.p = p;
1222
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001223 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
1224 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001225 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001226 mbedtls_x509_crt_free( crt );
1227 return( MBEDTLS_ERR_X509_INVALID_FORMAT + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001228 }
1229
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001230 if( len && ( ret = mbedtls_x509_get_name( &p, p + len, &crt->subject ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001231 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001232 mbedtls_x509_crt_free( crt );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001233 return( ret );
1234 }
1235
1236 crt->subject_raw.len = p - crt->subject_raw.p;
1237
1238 /*
1239 * SubjectPublicKeyInfo
1240 */
Hanno Becker494dd7a2019-02-06 16:13:41 +00001241 crt->pk_raw.p = p;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001242 if( ( ret = mbedtls_pk_parse_subpubkey( &p, end, &crt->pk ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001243 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001244 mbedtls_x509_crt_free( crt );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001245 return( ret );
1246 }
Hanno Becker494dd7a2019-02-06 16:13:41 +00001247 crt->pk_raw.len = p - crt->pk_raw.p;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001248
1249 /*
1250 * issuerUniqueID [1] IMPLICIT UniqueIdentifier OPTIONAL,
1251 * -- If present, version shall be v2 or v3
1252 * subjectUniqueID [2] IMPLICIT UniqueIdentifier OPTIONAL,
1253 * -- If present, version shall be v2 or v3
1254 * extensions [3] EXPLICIT Extensions OPTIONAL
1255 * -- If present, version shall be v3
1256 */
1257 if( crt->version == 2 || crt->version == 3 )
1258 {
1259 ret = x509_get_uid( &p, end, &crt->issuer_id, 1 );
1260 if( ret != 0 )
1261 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001262 mbedtls_x509_crt_free( crt );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001263 return( ret );
1264 }
1265 }
1266
1267 if( crt->version == 2 || crt->version == 3 )
1268 {
1269 ret = x509_get_uid( &p, end, &crt->subject_id, 2 );
1270 if( ret != 0 )
1271 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001272 mbedtls_x509_crt_free( crt );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001273 return( ret );
1274 }
1275 }
1276
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001277#if !defined(MBEDTLS_X509_ALLOW_EXTENSIONS_NON_V3)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001278 if( crt->version == 3 )
Paul Bakkerc27c4e22013-09-23 15:01:36 +02001279#endif
Manuel Pégourié-Gonnarda252af72015-03-27 16:15:55 +01001280 {
Nicola Di Lieto5659e7e2020-05-28 23:41:38 +02001281 ret = x509_get_crt_ext( &p, end, crt, cb, p_ctx );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001282 if( ret != 0 )
1283 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001284 mbedtls_x509_crt_free( crt );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001285 return( ret );
1286 }
1287 }
1288
1289 if( p != end )
1290 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001291 mbedtls_x509_crt_free( crt );
1292 return( MBEDTLS_ERR_X509_INVALID_FORMAT +
1293 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001294 }
1295
1296 end = crt_end;
1297
1298 /*
1299 * }
1300 * -- end of TBSCertificate
1301 *
1302 * signatureAlgorithm AlgorithmIdentifier,
1303 * signatureValue BIT STRING
1304 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001305 if( ( ret = mbedtls_x509_get_alg( &p, end, &sig_oid2, &sig_params2 ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001306 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001307 mbedtls_x509_crt_free( crt );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001308 return( ret );
1309 }
1310
Manuel Pégourié-Gonnard1022fed2015-03-27 16:30:47 +01001311 if( crt->sig_oid.len != sig_oid2.len ||
1312 memcmp( crt->sig_oid.p, sig_oid2.p, crt->sig_oid.len ) != 0 ||
Manuel Pégourié-Gonnarddddbb1d2014-06-05 17:02:24 +02001313 sig_params1.len != sig_params2.len ||
Manuel Pégourié-Gonnard159c5242015-04-30 11:15:22 +02001314 ( sig_params1.len != 0 &&
1315 memcmp( sig_params1.p, sig_params2.p, sig_params1.len ) != 0 ) )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001316 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001317 mbedtls_x509_crt_free( crt );
1318 return( MBEDTLS_ERR_X509_SIG_MISMATCH );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001319 }
1320
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001321 if( ( ret = mbedtls_x509_get_sig( &p, end, &crt->sig ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001322 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001323 mbedtls_x509_crt_free( crt );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001324 return( ret );
1325 }
1326
1327 if( p != end )
1328 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001329 mbedtls_x509_crt_free( crt );
1330 return( MBEDTLS_ERR_X509_INVALID_FORMAT +
1331 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001332 }
1333
1334 return( 0 );
1335}
1336
1337/*
1338 * Parse one X.509 certificate in DER format from a buffer and add them to a
1339 * chained list
1340 */
Hanno Becker1a65dcd2019-01-31 08:57:44 +00001341static int mbedtls_x509_crt_parse_der_internal( mbedtls_x509_crt *chain,
1342 const unsigned char *buf,
1343 size_t buflen,
Nicola Di Lieto502d4b42020-04-25 14:41:25 +02001344 int make_copy,
Nicola Di Lieto5659e7e2020-05-28 23:41:38 +02001345 mbedtls_x509_crt_ext_cb_t cb,
1346 void *p_ctx )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001347{
Janos Follath865b3eb2019-12-16 11:46:15 +00001348 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001349 mbedtls_x509_crt *crt = chain, *prev = NULL;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001350
1351 /*
1352 * Check for valid input
1353 */
1354 if( crt == NULL || buf == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001355 return( MBEDTLS_ERR_X509_BAD_INPUT_DATA );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001356
1357 while( crt->version != 0 && crt->next != NULL )
1358 {
1359 prev = crt;
1360 crt = crt->next;
1361 }
1362
1363 /*
1364 * Add new certificate on the end of the chain if needed.
1365 */
Paul Bakker66d5d072014-06-17 16:39:18 +02001366 if( crt->version != 0 && crt->next == NULL )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001367 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02001368 crt->next = mbedtls_calloc( 1, sizeof( mbedtls_x509_crt ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001369
1370 if( crt->next == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02001371 return( MBEDTLS_ERR_X509_ALLOC_FAILED );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001372
1373 prev = crt;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001374 mbedtls_x509_crt_init( crt->next );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001375 crt = crt->next;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001376 }
1377
Nicola Di Lieto5659e7e2020-05-28 23:41:38 +02001378 ret = x509_crt_parse_der_core( crt, buf, buflen, make_copy, cb, p_ctx );
Nicola Di Lieto502d4b42020-04-25 14:41:25 +02001379 if( ret != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001380 {
1381 if( prev )
1382 prev->next = NULL;
1383
1384 if( crt != chain )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001385 mbedtls_free( crt );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001386
1387 return( ret );
1388 }
1389
1390 return( 0 );
1391}
1392
Hanno Becker1a65dcd2019-01-31 08:57:44 +00001393int mbedtls_x509_crt_parse_der_nocopy( mbedtls_x509_crt *chain,
1394 const unsigned char *buf,
1395 size_t buflen )
1396{
Nicola Di Lieto5659e7e2020-05-28 23:41:38 +02001397 return( mbedtls_x509_crt_parse_der_internal( chain, buf, buflen, 0, NULL, NULL ) );
Nicola Di Lieto502d4b42020-04-25 14:41:25 +02001398}
1399
Nicola Di Lietofde98f72020-05-28 08:34:33 +02001400int mbedtls_x509_crt_parse_der_with_ext_cb( mbedtls_x509_crt *chain,
1401 const unsigned char *buf,
1402 size_t buflen,
Nicola Di Lieto4dbe5672020-05-28 09:18:42 +02001403 int make_copy,
Nicola Di Lieto5659e7e2020-05-28 23:41:38 +02001404 mbedtls_x509_crt_ext_cb_t cb,
1405 void *p_ctx )
Nicola Di Lieto502d4b42020-04-25 14:41:25 +02001406{
Nicola Di Lieto5659e7e2020-05-28 23:41:38 +02001407 return( mbedtls_x509_crt_parse_der_internal( chain, buf, buflen, make_copy, cb, p_ctx ) );
Hanno Becker1a65dcd2019-01-31 08:57:44 +00001408}
1409
1410int mbedtls_x509_crt_parse_der( mbedtls_x509_crt *chain,
1411 const unsigned char *buf,
1412 size_t buflen )
1413{
Nicola Di Lieto5659e7e2020-05-28 23:41:38 +02001414 return( mbedtls_x509_crt_parse_der_internal( chain, buf, buflen, 1, NULL, NULL ) );
Hanno Becker1a65dcd2019-01-31 08:57:44 +00001415}
1416
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001417/*
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001418 * Parse one or more PEM certificates from a buffer and add them to the chained
1419 * list
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001420 */
Hanno Becker1a65dcd2019-01-31 08:57:44 +00001421int mbedtls_x509_crt_parse( mbedtls_x509_crt *chain,
1422 const unsigned char *buf,
1423 size_t buflen )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001424{
Janos Follath98e28a72016-05-31 14:03:54 +01001425#if defined(MBEDTLS_PEM_PARSE_C)
Andres AGc0db5112016-12-07 15:05:53 +00001426 int success = 0, first_error = 0, total_failed = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001427 int buf_format = MBEDTLS_X509_FORMAT_DER;
Janos Follath98e28a72016-05-31 14:03:54 +01001428#endif
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001429
1430 /*
1431 * Check for valid input
1432 */
1433 if( chain == NULL || buf == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001434 return( MBEDTLS_ERR_X509_BAD_INPUT_DATA );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001435
1436 /*
1437 * Determine buffer content. Buffer contains either one DER certificate or
1438 * one or more PEM certificates.
1439 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001440#if defined(MBEDTLS_PEM_PARSE_C)
Manuel Pégourié-Gonnard0ece0f92015-05-12 12:43:54 +02001441 if( buflen != 0 && buf[buflen - 1] == '\0' &&
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001442 strstr( (const char *) buf, "-----BEGIN CERTIFICATE-----" ) != NULL )
1443 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001444 buf_format = MBEDTLS_X509_FORMAT_PEM;
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001445 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001446
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001447 if( buf_format == MBEDTLS_X509_FORMAT_DER )
1448 return mbedtls_x509_crt_parse_der( chain, buf, buflen );
Janos Follath98e28a72016-05-31 14:03:54 +01001449#else
1450 return mbedtls_x509_crt_parse_der( chain, buf, buflen );
1451#endif
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001452
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001453#if defined(MBEDTLS_PEM_PARSE_C)
1454 if( buf_format == MBEDTLS_X509_FORMAT_PEM )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001455 {
Janos Follath865b3eb2019-12-16 11:46:15 +00001456 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001457 mbedtls_pem_context pem;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001458
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001459 /* 1 rather than 0 since the terminating NULL byte is counted in */
1460 while( buflen > 1 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001461 {
1462 size_t use_len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001463 mbedtls_pem_init( &pem );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001464
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001465 /* If we get there, we know the string is null-terminated */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001466 ret = mbedtls_pem_read_buffer( &pem,
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001467 "-----BEGIN CERTIFICATE-----",
1468 "-----END CERTIFICATE-----",
1469 buf, NULL, 0, &use_len );
1470
1471 if( ret == 0 )
1472 {
1473 /*
1474 * Was PEM encoded
1475 */
1476 buflen -= use_len;
1477 buf += use_len;
1478 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001479 else if( ret == MBEDTLS_ERR_PEM_BAD_INPUT_DATA )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001480 {
1481 return( ret );
1482 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001483 else if( ret != MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001484 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001485 mbedtls_pem_free( &pem );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001486
1487 /*
1488 * PEM header and footer were found
1489 */
1490 buflen -= use_len;
1491 buf += use_len;
1492
1493 if( first_error == 0 )
1494 first_error = ret;
1495
Paul Bakker5a5fa922014-09-26 14:53:04 +02001496 total_failed++;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001497 continue;
1498 }
1499 else
1500 break;
1501
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001502 ret = mbedtls_x509_crt_parse_der( chain, pem.buf, pem.buflen );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001503
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001504 mbedtls_pem_free( &pem );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001505
1506 if( ret != 0 )
1507 {
1508 /*
1509 * Quit parsing on a memory error
1510 */
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02001511 if( ret == MBEDTLS_ERR_X509_ALLOC_FAILED )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001512 return( ret );
1513
1514 if( first_error == 0 )
1515 first_error = ret;
1516
1517 total_failed++;
1518 continue;
1519 }
1520
1521 success = 1;
1522 }
1523 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001524
1525 if( success )
1526 return( total_failed );
1527 else if( first_error )
1528 return( first_error );
1529 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001530 return( MBEDTLS_ERR_X509_CERT_UNKNOWN_FORMAT );
Janos Follath98e28a72016-05-31 14:03:54 +01001531#endif /* MBEDTLS_PEM_PARSE_C */
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001532}
1533
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001534#if defined(MBEDTLS_FS_IO)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001535/*
1536 * Load one or more certificates and add them to the chained list
1537 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001538int mbedtls_x509_crt_parse_file( mbedtls_x509_crt *chain, const char *path )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001539{
Janos Follath865b3eb2019-12-16 11:46:15 +00001540 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001541 size_t n;
1542 unsigned char *buf;
1543
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001544 if( ( ret = mbedtls_pk_load_file( path, &buf, &n ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001545 return( ret );
1546
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001547 ret = mbedtls_x509_crt_parse( chain, buf, n );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001548
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05001549 mbedtls_platform_zeroize( buf, n );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001550 mbedtls_free( buf );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001551
1552 return( ret );
1553}
1554
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001555int mbedtls_x509_crt_parse_path( mbedtls_x509_crt *chain, const char *path )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001556{
1557 int ret = 0;
Paul Bakkerfa6a6202013-10-28 18:48:30 +01001558#if defined(_WIN32) && !defined(EFIX64) && !defined(EFI32)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001559 int w_ret;
1560 WCHAR szDir[MAX_PATH];
1561 char filename[MAX_PATH];
Paul Bakker9af723c2014-05-01 13:03:14 +02001562 char *p;
Manuel Pégourié-Gonnard261faed2015-10-21 10:16:29 +02001563 size_t len = strlen( path );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001564
Paul Bakker9af723c2014-05-01 13:03:14 +02001565 WIN32_FIND_DATAW file_data;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001566 HANDLE hFind;
1567
1568 if( len > MAX_PATH - 3 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001569 return( MBEDTLS_ERR_X509_BAD_INPUT_DATA );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001570
Paul Bakker9af723c2014-05-01 13:03:14 +02001571 memset( szDir, 0, sizeof(szDir) );
1572 memset( filename, 0, MAX_PATH );
1573 memcpy( filename, path, len );
1574 filename[len++] = '\\';
1575 p = filename + len;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001576 filename[len++] = '*';
1577
Simon B3c6b18d2016-11-03 01:11:37 +00001578 w_ret = MultiByteToWideChar( CP_ACP, 0, filename, (int)len, szDir,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001579 MAX_PATH - 3 );
Manuel Pégourié-Gonnardacdb9b92015-01-23 17:50:34 +00001580 if( w_ret == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001581 return( MBEDTLS_ERR_X509_BAD_INPUT_DATA );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001582
1583 hFind = FindFirstFileW( szDir, &file_data );
Paul Bakker66d5d072014-06-17 16:39:18 +02001584 if( hFind == INVALID_HANDLE_VALUE )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001585 return( MBEDTLS_ERR_X509_FILE_IO_ERROR );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001586
1587 len = MAX_PATH - len;
1588 do
1589 {
Paul Bakker9af723c2014-05-01 13:03:14 +02001590 memset( p, 0, len );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001591
1592 if( file_data.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY )
1593 continue;
1594
Paul Bakker9af723c2014-05-01 13:03:14 +02001595 w_ret = WideCharToMultiByte( CP_ACP, 0, file_data.cFileName,
Paul Bakker66d5d072014-06-17 16:39:18 +02001596 lstrlenW( file_data.cFileName ),
Manuel Pégourié-Gonnard261faed2015-10-21 10:16:29 +02001597 p, (int) len - 1,
Paul Bakker9af723c2014-05-01 13:03:14 +02001598 NULL, NULL );
Manuel Pégourié-Gonnardacdb9b92015-01-23 17:50:34 +00001599 if( w_ret == 0 )
Ron Eldor36d90422017-01-09 15:09:16 +02001600 {
1601 ret = MBEDTLS_ERR_X509_FILE_IO_ERROR;
1602 goto cleanup;
1603 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001604
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001605 w_ret = mbedtls_x509_crt_parse_file( chain, filename );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001606 if( w_ret < 0 )
1607 ret++;
1608 else
1609 ret += w_ret;
1610 }
1611 while( FindNextFileW( hFind, &file_data ) != 0 );
1612
Paul Bakker66d5d072014-06-17 16:39:18 +02001613 if( GetLastError() != ERROR_NO_MORE_FILES )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001614 ret = MBEDTLS_ERR_X509_FILE_IO_ERROR;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001615
Ron Eldor36d90422017-01-09 15:09:16 +02001616cleanup:
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001617 FindClose( hFind );
Paul Bakkerbe089b02013-10-14 15:51:50 +02001618#else /* _WIN32 */
Manuel Pégourié-Gonnard964bf9b2013-11-26 16:47:11 +01001619 int t_ret;
Andres AGf9113192016-09-02 14:06:04 +01001620 int snp_ret;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001621 struct stat sb;
Manuel Pégourié-Gonnard964bf9b2013-11-26 16:47:11 +01001622 struct dirent *entry;
Andres AGf9113192016-09-02 14:06:04 +01001623 char entry_name[MBEDTLS_X509_MAX_FILE_PATH_LEN];
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001624 DIR *dir = opendir( path );
1625
Paul Bakker66d5d072014-06-17 16:39:18 +02001626 if( dir == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001627 return( MBEDTLS_ERR_X509_FILE_IO_ERROR );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001628
Ron Eldor63140682017-01-09 19:27:59 +02001629#if defined(MBEDTLS_THREADING_C)
Manuel Pégourié-Gonnard944cfe82015-05-27 20:07:18 +02001630 if( ( ret = mbedtls_mutex_lock( &mbedtls_threading_readdir_mutex ) ) != 0 )
Manuel Pégourié-Gonnardf9b85d92015-06-22 18:39:57 +02001631 {
1632 closedir( dir );
Manuel Pégourié-Gonnard5ad68e42013-11-28 17:11:54 +01001633 return( ret );
Manuel Pégourié-Gonnardf9b85d92015-06-22 18:39:57 +02001634 }
Ron Eldor63140682017-01-09 19:27:59 +02001635#endif /* MBEDTLS_THREADING_C */
Manuel Pégourié-Gonnard5ad68e42013-11-28 17:11:54 +01001636
Manuel Pégourié-Gonnard964bf9b2013-11-26 16:47:11 +01001637 while( ( entry = readdir( dir ) ) != NULL )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001638 {
Andres AGf9113192016-09-02 14:06:04 +01001639 snp_ret = mbedtls_snprintf( entry_name, sizeof entry_name,
1640 "%s/%s", path, entry->d_name );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001641
Andres AGf9113192016-09-02 14:06:04 +01001642 if( snp_ret < 0 || (size_t)snp_ret >= sizeof entry_name )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001643 {
Andres AGf9113192016-09-02 14:06:04 +01001644 ret = MBEDTLS_ERR_X509_BUFFER_TOO_SMALL;
1645 goto cleanup;
1646 }
1647 else if( stat( entry_name, &sb ) == -1 )
1648 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001649 ret = MBEDTLS_ERR_X509_FILE_IO_ERROR;
Manuel Pégourié-Gonnard5ad68e42013-11-28 17:11:54 +01001650 goto cleanup;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001651 }
1652
1653 if( !S_ISREG( sb.st_mode ) )
1654 continue;
1655
1656 // Ignore parse errors
1657 //
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001658 t_ret = mbedtls_x509_crt_parse_file( chain, entry_name );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001659 if( t_ret < 0 )
1660 ret++;
1661 else
1662 ret += t_ret;
1663 }
Manuel Pégourié-Gonnard5ad68e42013-11-28 17:11:54 +01001664
1665cleanup:
Andres AGf9113192016-09-02 14:06:04 +01001666 closedir( dir );
1667
Ron Eldor63140682017-01-09 19:27:59 +02001668#if defined(MBEDTLS_THREADING_C)
Manuel Pégourié-Gonnard944cfe82015-05-27 20:07:18 +02001669 if( mbedtls_mutex_unlock( &mbedtls_threading_readdir_mutex ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001670 ret = MBEDTLS_ERR_THREADING_MUTEX_ERROR;
Ron Eldor63140682017-01-09 19:27:59 +02001671#endif /* MBEDTLS_THREADING_C */
Manuel Pégourié-Gonnard5ad68e42013-11-28 17:11:54 +01001672
Paul Bakkerbe089b02013-10-14 15:51:50 +02001673#endif /* _WIN32 */
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001674
1675 return( ret );
1676}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001677#endif /* MBEDTLS_FS_IO */
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001678
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001679/*
1680 * OtherName ::= SEQUENCE {
1681 * type-id OBJECT IDENTIFIER,
1682 * value [0] EXPLICIT ANY DEFINED BY type-id }
1683 *
1684 * HardwareModuleName ::= SEQUENCE {
1685 * hwType OBJECT IDENTIFIER,
1686 * hwSerialNum OCTET STRING }
1687 *
1688 * NOTE: we currently only parse and use otherName of type HwModuleName,
1689 * as defined in RFC 4108.
1690 */
1691static int x509_get_other_name( const mbedtls_x509_buf *subject_alt_name,
1692 mbedtls_x509_san_other_name *other_name )
1693{
Janos Follathab23cd12019-05-09 13:53:57 +01001694 int ret = 0;
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001695 size_t len;
1696 unsigned char *p = subject_alt_name->p;
1697 const unsigned char *end = p + subject_alt_name->len;
1698 mbedtls_x509_buf cur_oid;
1699
1700 if( ( subject_alt_name->tag &
1701 ( MBEDTLS_ASN1_TAG_CLASS_MASK | MBEDTLS_ASN1_TAG_VALUE_MASK ) ) !=
1702 ( MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_X509_SAN_OTHER_NAME ) )
1703 {
1704 /*
1705 * The given subject alternative name is not of type "othername".
1706 */
1707 return( MBEDTLS_ERR_X509_BAD_INPUT_DATA );
1708 }
1709
1710 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
1711 MBEDTLS_ASN1_OID ) ) != 0 )
1712 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS + ret );
1713
1714 cur_oid.tag = MBEDTLS_ASN1_OID;
1715 cur_oid.p = p;
1716 cur_oid.len = len;
1717
1718 /*
1719 * Only HwModuleName is currently supported.
1720 */
1721 if( MBEDTLS_OID_CMP( MBEDTLS_OID_ON_HW_MODULE_NAME, &cur_oid ) != 0 )
1722 {
1723 return( MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE );
1724 }
1725
Ron Eldor890819a2019-05-13 19:03:04 +03001726 if( p + len >= end )
1727 {
Sébastien Duquette661d7252019-06-23 17:45:26 -04001728 mbedtls_platform_zeroize( other_name, sizeof( *other_name ) );
Ron Eldor890819a2019-05-13 19:03:04 +03001729 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS +
1730 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
1731 }
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001732 p += len;
1733 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
1734 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_CONTEXT_SPECIFIC ) ) != 0 )
1735 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS + ret );
1736
1737 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
1738 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
1739 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS + ret );
1740
1741 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len, MBEDTLS_ASN1_OID ) ) != 0 )
1742 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS + ret );
1743
1744 other_name->value.hardware_module_name.oid.tag = MBEDTLS_ASN1_OID;
1745 other_name->value.hardware_module_name.oid.p = p;
1746 other_name->value.hardware_module_name.oid.len = len;
1747
Ron Eldor890819a2019-05-13 19:03:04 +03001748 if( p + len >= end )
1749 {
Sébastien Duquette661d7252019-06-23 17:45:26 -04001750 mbedtls_platform_zeroize( other_name, sizeof( *other_name ) );
Ron Eldor890819a2019-05-13 19:03:04 +03001751 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS +
1752 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
1753 }
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001754 p += len;
1755 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
1756 MBEDTLS_ASN1_OCTET_STRING ) ) != 0 )
Ron Eldor890819a2019-05-13 19:03:04 +03001757 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS + ret );
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001758
1759 other_name->value.hardware_module_name.val.tag = MBEDTLS_ASN1_OCTET_STRING;
1760 other_name->value.hardware_module_name.val.p = p;
1761 other_name->value.hardware_module_name.val.len = len;
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001762 p += len;
1763 if( p != end )
1764 {
Ron Eldor890819a2019-05-13 19:03:04 +03001765 mbedtls_platform_zeroize( other_name,
Sébastien Duquette661d7252019-06-23 17:45:26 -04001766 sizeof( *other_name ) );
Ron Eldor890819a2019-05-13 19:03:04 +03001767 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS +
1768 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001769 }
1770 return( 0 );
1771}
1772
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02001773static int x509_info_subject_alt_name( char **buf, size_t *size,
Janos Follath2f0ec1e2019-05-10 11:06:31 +01001774 const mbedtls_x509_sequence
1775 *subject_alt_name,
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001776 const char *prefix )
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02001777{
Janos Follath865b3eb2019-12-16 11:46:15 +00001778 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02001779 size_t n = *size;
1780 char *p = *buf;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001781 const mbedtls_x509_sequence *cur = subject_alt_name;
Ron Eldor890819a2019-05-13 19:03:04 +03001782 mbedtls_x509_subject_alternative_name san;
1783 int parse_ret;
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02001784
1785 while( cur != NULL )
1786 {
Ron Eldor890819a2019-05-13 19:03:04 +03001787 memset( &san, 0, sizeof( san ) );
1788 parse_ret = mbedtls_x509_parse_subject_alt_name( &cur->buf, &san );
1789 if( parse_ret != 0 )
1790 {
1791 if( parse_ret == MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE )
1792 {
1793 ret = mbedtls_snprintf( p, n, "\n%s <unsupported>", prefix );
1794 MBEDTLS_X509_SAFE_SNPRINTF;
1795 }
1796 else
1797 {
1798 ret = mbedtls_snprintf( p, n, "\n%s <malformed>", prefix );
1799 MBEDTLS_X509_SAFE_SNPRINTF;
1800 }
1801 cur = cur->next;
1802 continue;
1803 }
1804
1805 switch( san.type )
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02001806 {
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001807 /*
1808 * otherName
1809 */
Ron Eldora2913912019-05-16 16:17:38 +03001810 case MBEDTLS_X509_SAN_OTHER_NAME:
Ron Eldor890819a2019-05-13 19:03:04 +03001811 {
1812 mbedtls_x509_san_other_name *other_name = &san.san.other_name;
1813
1814 ret = mbedtls_snprintf( p, n, "\n%s otherName :", prefix );
1815 MBEDTLS_X509_SAFE_SNPRINTF;
1816
1817 if( MBEDTLS_OID_CMP( MBEDTLS_OID_ON_HW_MODULE_NAME,
1818 &other_name->value.hardware_module_name.oid ) != 0 )
Janos Follath22f605f2019-05-10 10:37:17 +01001819 {
Ron Eldor890819a2019-05-13 19:03:04 +03001820 ret = mbedtls_snprintf( p, n, "\n%s hardware module name :", prefix );
1821 MBEDTLS_X509_SAFE_SNPRINTF;
1822 ret = mbedtls_snprintf( p, n, "\n%s hardware type : ", prefix );
Janos Follath2f0ec1e2019-05-10 11:06:31 +01001823 MBEDTLS_X509_SAFE_SNPRINTF;
1824
Ron Eldor890819a2019-05-13 19:03:04 +03001825 ret = mbedtls_oid_get_numeric_string( p, n, &other_name->value.hardware_module_name.oid );
1826 MBEDTLS_X509_SAFE_SNPRINTF;
Janos Follath2f0ec1e2019-05-10 11:06:31 +01001827
Ron Eldor890819a2019-05-13 19:03:04 +03001828 ret = mbedtls_snprintf( p, n, "\n%s hardware serial number : ", prefix );
1829 MBEDTLS_X509_SAFE_SNPRINTF;
1830
1831 if( other_name->value.hardware_module_name.val.len >= n )
1832 {
1833 *p = '\0';
1834 return( MBEDTLS_ERR_X509_BUFFER_TOO_SMALL );
Janos Follath22f605f2019-05-10 10:37:17 +01001835 }
1836
Ron Eldora2913912019-05-16 16:17:38 +03001837 memcpy( p, other_name->value.hardware_module_name.val.p,
1838 other_name->value.hardware_module_name.val.len );
1839 p += other_name->value.hardware_module_name.val.len;
1840
Ron Eldor890819a2019-05-13 19:03:04 +03001841 n -= other_name->value.hardware_module_name.val.len;
Janos Follath2f0ec1e2019-05-10 11:06:31 +01001842
Ron Eldor890819a2019-05-13 19:03:04 +03001843 }/* MBEDTLS_OID_ON_HW_MODULE_NAME */
1844 }
1845 break;
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001846
1847 /*
1848 * dNSName
1849 */
Ron Eldora2913912019-05-16 16:17:38 +03001850 case MBEDTLS_X509_SAN_DNS_NAME:
Ron Eldor890819a2019-05-13 19:03:04 +03001851 {
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001852 ret = mbedtls_snprintf( p, n, "\n%s dNSName : ", prefix );
1853 MBEDTLS_X509_SAFE_SNPRINTF;
Ron Eldor890819a2019-05-13 19:03:04 +03001854 if( san.san.unstructured_name.len >= n )
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001855 {
1856 *p = '\0';
1857 return( MBEDTLS_ERR_X509_BUFFER_TOO_SMALL );
1858 }
Ron Eldora2913912019-05-16 16:17:38 +03001859
1860 memcpy( p, san.san.unstructured_name.p, san.san.unstructured_name.len );
1861 p += san.san.unstructured_name.len;
Ron Eldor890819a2019-05-13 19:03:04 +03001862 n -= san.san.unstructured_name.len;
Ron Eldor890819a2019-05-13 19:03:04 +03001863 }
1864 break;
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001865
1866 /*
Ron Eldor890819a2019-05-13 19:03:04 +03001867 * Type not supported, skip item.
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001868 */
1869 default:
Janos Follath22f605f2019-05-10 10:37:17 +01001870 ret = mbedtls_snprintf( p, n, "\n%s <unsupported>", prefix );
1871 MBEDTLS_X509_SAFE_SNPRINTF;
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001872 break;
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02001873 }
1874
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02001875 cur = cur->next;
1876 }
1877
1878 *p = '\0';
1879
1880 *size = n;
1881 *buf = p;
1882
1883 return( 0 );
1884}
1885
Ron Eldor890819a2019-05-13 19:03:04 +03001886int mbedtls_x509_parse_subject_alt_name( const mbedtls_x509_buf *san_buf,
1887 mbedtls_x509_subject_alternative_name *san )
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001888{
Janos Follath865b3eb2019-12-16 11:46:15 +00001889 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Ron Eldor890819a2019-05-13 19:03:04 +03001890 switch( san_buf->tag &
1891 ( MBEDTLS_ASN1_TAG_CLASS_MASK |
1892 MBEDTLS_ASN1_TAG_VALUE_MASK ) )
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001893 {
Ron Eldor890819a2019-05-13 19:03:04 +03001894 /*
1895 * otherName
1896 */
1897 case( MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_X509_SAN_OTHER_NAME ):
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001898 {
Ron Eldor890819a2019-05-13 19:03:04 +03001899 mbedtls_x509_san_other_name other_name;
1900
1901 ret = x509_get_other_name( san_buf, &other_name );
1902 if( ret != 0 )
1903 return( ret );
1904
1905 memset( san, 0, sizeof( mbedtls_x509_subject_alternative_name ) );
1906 san->type = MBEDTLS_X509_SAN_OTHER_NAME;
1907 memcpy( &san->san.other_name,
1908 &other_name, sizeof( other_name ) );
1909
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001910 }
Ron Eldor890819a2019-05-13 19:03:04 +03001911 break;
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001912
Ron Eldor890819a2019-05-13 19:03:04 +03001913 /*
1914 * dNSName
1915 */
1916 case( MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_X509_SAN_DNS_NAME ):
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001917 {
Ron Eldor890819a2019-05-13 19:03:04 +03001918 memset( san, 0, sizeof( mbedtls_x509_subject_alternative_name ) );
1919 san->type = MBEDTLS_X509_SAN_DNS_NAME;
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001920
Ron Eldor890819a2019-05-13 19:03:04 +03001921 memcpy( &san->san.unstructured_name,
1922 san_buf, sizeof( *san_buf ) );
Janos Follath6c379b42019-05-10 14:17:16 +01001923
Ron Eldor890819a2019-05-13 19:03:04 +03001924 }
1925 break;
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001926
Ron Eldor890819a2019-05-13 19:03:04 +03001927 /*
1928 * Type not supported
1929 */
1930 default:
1931 return( MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE );
1932 }
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001933 return( 0 );
1934}
1935
Manuel Pégourié-Gonnard0db29b02014-04-01 18:12:24 +02001936#define PRINT_ITEM(i) \
1937 { \
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001938 ret = mbedtls_snprintf( p, n, "%s" i, sep ); \
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001939 MBEDTLS_X509_SAFE_SNPRINTF; \
Manuel Pégourié-Gonnard0db29b02014-04-01 18:12:24 +02001940 sep = ", "; \
1941 }
1942
1943#define CERT_TYPE(type,name) \
Hanno Becker1eeca412018-10-15 12:01:35 +01001944 if( ns_cert_type & (type) ) \
Manuel Pégourié-Gonnard0db29b02014-04-01 18:12:24 +02001945 PRINT_ITEM( name );
1946
Manuel Pégourié-Gonnard919f8f52014-04-01 13:01:11 +02001947static int x509_info_cert_type( char **buf, size_t *size,
1948 unsigned char ns_cert_type )
1949{
Janos Follath865b3eb2019-12-16 11:46:15 +00001950 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard919f8f52014-04-01 13:01:11 +02001951 size_t n = *size;
1952 char *p = *buf;
Manuel Pégourié-Gonnard7b30cfc2014-04-01 18:00:07 +02001953 const char *sep = "";
Manuel Pégourié-Gonnard919f8f52014-04-01 13:01:11 +02001954
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001955 CERT_TYPE( MBEDTLS_X509_NS_CERT_TYPE_SSL_CLIENT, "SSL Client" );
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01001956 CERT_TYPE( MBEDTLS_X509_NS_CERT_TYPE_SSL_SERVER, "SSL Server" );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001957 CERT_TYPE( MBEDTLS_X509_NS_CERT_TYPE_EMAIL, "Email" );
1958 CERT_TYPE( MBEDTLS_X509_NS_CERT_TYPE_OBJECT_SIGNING, "Object Signing" );
1959 CERT_TYPE( MBEDTLS_X509_NS_CERT_TYPE_RESERVED, "Reserved" );
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01001960 CERT_TYPE( MBEDTLS_X509_NS_CERT_TYPE_SSL_CA, "SSL CA" );
1961 CERT_TYPE( MBEDTLS_X509_NS_CERT_TYPE_EMAIL_CA, "Email CA" );
1962 CERT_TYPE( MBEDTLS_X509_NS_CERT_TYPE_OBJECT_SIGNING_CA, "Object Signing CA" );
Manuel Pégourié-Gonnard919f8f52014-04-01 13:01:11 +02001963
1964 *size = n;
1965 *buf = p;
1966
1967 return( 0 );
1968}
1969
Manuel Pégourié-Gonnard0db29b02014-04-01 18:12:24 +02001970#define KEY_USAGE(code,name) \
Hanno Becker1eeca412018-10-15 12:01:35 +01001971 if( key_usage & (code) ) \
Manuel Pégourié-Gonnard0db29b02014-04-01 18:12:24 +02001972 PRINT_ITEM( name );
1973
Manuel Pégourié-Gonnard65c2ddc2014-04-01 14:12:11 +02001974static int x509_info_key_usage( char **buf, size_t *size,
Manuel Pégourié-Gonnard9a702252015-06-23 10:14:36 +02001975 unsigned int key_usage )
Manuel Pégourié-Gonnard65c2ddc2014-04-01 14:12:11 +02001976{
Janos Follath865b3eb2019-12-16 11:46:15 +00001977 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard65c2ddc2014-04-01 14:12:11 +02001978 size_t n = *size;
1979 char *p = *buf;
Manuel Pégourié-Gonnard7b30cfc2014-04-01 18:00:07 +02001980 const char *sep = "";
Manuel Pégourié-Gonnard65c2ddc2014-04-01 14:12:11 +02001981
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001982 KEY_USAGE( MBEDTLS_X509_KU_DIGITAL_SIGNATURE, "Digital Signature" );
1983 KEY_USAGE( MBEDTLS_X509_KU_NON_REPUDIATION, "Non Repudiation" );
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01001984 KEY_USAGE( MBEDTLS_X509_KU_KEY_ENCIPHERMENT, "Key Encipherment" );
1985 KEY_USAGE( MBEDTLS_X509_KU_DATA_ENCIPHERMENT, "Data Encipherment" );
1986 KEY_USAGE( MBEDTLS_X509_KU_KEY_AGREEMENT, "Key Agreement" );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001987 KEY_USAGE( MBEDTLS_X509_KU_KEY_CERT_SIGN, "Key Cert Sign" );
1988 KEY_USAGE( MBEDTLS_X509_KU_CRL_SIGN, "CRL Sign" );
Manuel Pégourié-Gonnard9a702252015-06-23 10:14:36 +02001989 KEY_USAGE( MBEDTLS_X509_KU_ENCIPHER_ONLY, "Encipher Only" );
1990 KEY_USAGE( MBEDTLS_X509_KU_DECIPHER_ONLY, "Decipher Only" );
Manuel Pégourié-Gonnard65c2ddc2014-04-01 14:12:11 +02001991
1992 *size = n;
1993 *buf = p;
1994
1995 return( 0 );
1996}
1997
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02001998static int x509_info_ext_key_usage( char **buf, size_t *size,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001999 const mbedtls_x509_sequence *extended_key_usage )
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02002000{
Janos Follath865b3eb2019-12-16 11:46:15 +00002001 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02002002 const char *desc;
2003 size_t n = *size;
2004 char *p = *buf;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002005 const mbedtls_x509_sequence *cur = extended_key_usage;
Manuel Pégourié-Gonnard7b30cfc2014-04-01 18:00:07 +02002006 const char *sep = "";
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02002007
2008 while( cur != NULL )
2009 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002010 if( mbedtls_oid_get_extended_key_usage( &cur->buf, &desc ) != 0 )
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02002011 desc = "???";
2012
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002013 ret = mbedtls_snprintf( p, n, "%s%s", sep, desc );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002014 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02002015
Manuel Pégourié-Gonnard7b30cfc2014-04-01 18:00:07 +02002016 sep = ", ";
2017
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02002018 cur = cur->next;
2019 }
2020
2021 *size = n;
2022 *buf = p;
2023
2024 return( 0 );
2025}
2026
Ron Eldor74d9acc2019-03-21 14:00:03 +02002027static int x509_info_cert_policies( char **buf, size_t *size,
2028 const mbedtls_x509_sequence *certificate_policies )
2029{
Janos Follath865b3eb2019-12-16 11:46:15 +00002030 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Ron Eldor74d9acc2019-03-21 14:00:03 +02002031 const char *desc;
2032 size_t n = *size;
2033 char *p = *buf;
2034 const mbedtls_x509_sequence *cur = certificate_policies;
2035 const char *sep = "";
2036
2037 while( cur != NULL )
2038 {
2039 if( mbedtls_oid_get_certificate_policies( &cur->buf, &desc ) != 0 )
2040 desc = "???";
2041
2042 ret = mbedtls_snprintf( p, n, "%s%s", sep, desc );
2043 MBEDTLS_X509_SAFE_SNPRINTF;
2044
2045 sep = ", ";
2046
2047 cur = cur->next;
2048 }
2049
2050 *size = n;
2051 *buf = p;
2052
2053 return( 0 );
2054}
2055
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002056/*
2057 * Return an informational string about the certificate.
2058 */
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002059#define BEFORE_COLON 18
2060#define BC "18"
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002061int mbedtls_x509_crt_info( char *buf, size_t size, const char *prefix,
2062 const mbedtls_x509_crt *crt )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002063{
Janos Follath865b3eb2019-12-16 11:46:15 +00002064 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002065 size_t n;
2066 char *p;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002067 char key_size_str[BEFORE_COLON];
2068
2069 p = buf;
2070 n = size;
2071
Janos Follath98e28a72016-05-31 14:03:54 +01002072 if( NULL == crt )
2073 {
2074 ret = mbedtls_snprintf( p, n, "\nCertificate is uninitialised!\n" );
2075 MBEDTLS_X509_SAFE_SNPRINTF;
2076
2077 return( (int) ( size - n ) );
2078 }
2079
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002080 ret = mbedtls_snprintf( p, n, "%scert. version : %d\n",
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002081 prefix, crt->version );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002082 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002083 ret = mbedtls_snprintf( p, n, "%sserial number : ",
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002084 prefix );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002085 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002086
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002087 ret = mbedtls_x509_serial_gets( p, n, &crt->serial );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002088 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002089
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002090 ret = mbedtls_snprintf( p, n, "\n%sissuer name : ", prefix );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002091 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002092 ret = mbedtls_x509_dn_gets( p, n, &crt->issuer );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002093 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002094
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002095 ret = mbedtls_snprintf( p, n, "\n%ssubject name : ", prefix );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002096 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002097 ret = mbedtls_x509_dn_gets( p, n, &crt->subject );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002098 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002099
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002100 ret = mbedtls_snprintf( p, n, "\n%sissued on : " \
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002101 "%04d-%02d-%02d %02d:%02d:%02d", prefix,
2102 crt->valid_from.year, crt->valid_from.mon,
2103 crt->valid_from.day, crt->valid_from.hour,
2104 crt->valid_from.min, crt->valid_from.sec );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002105 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002106
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002107 ret = mbedtls_snprintf( p, n, "\n%sexpires on : " \
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002108 "%04d-%02d-%02d %02d:%02d:%02d", prefix,
2109 crt->valid_to.year, crt->valid_to.mon,
2110 crt->valid_to.day, crt->valid_to.hour,
2111 crt->valid_to.min, crt->valid_to.sec );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002112 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002113
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002114 ret = mbedtls_snprintf( p, n, "\n%ssigned using : ", prefix );
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é-Gonnard2cf5a7c2015-04-08 12:49:31 +02002117 ret = mbedtls_x509_sig_alg_gets( p, n, &crt->sig_oid, crt->sig_pk,
Manuel Pégourié-Gonnardbf696d02014-06-05 17:07:30 +02002118 crt->sig_md, crt->sig_opts );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002119 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002120
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002121 /* Key size */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002122 if( ( ret = mbedtls_x509_key_size_helper( key_size_str, BEFORE_COLON,
2123 mbedtls_pk_get_name( &crt->pk ) ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002124 {
2125 return( ret );
2126 }
2127
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002128 ret = mbedtls_snprintf( p, n, "\n%s%-" BC "s: %d bits", prefix, key_size_str,
Manuel Pégourié-Gonnard097c7bb2015-06-18 16:43:38 +02002129 (int) mbedtls_pk_get_bitlen( &crt->pk ) );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002130 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002131
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002132 /*
2133 * Optional extensions
2134 */
2135
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01002136 if( crt->ext_types & MBEDTLS_X509_EXT_BASIC_CONSTRAINTS )
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002137 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002138 ret = mbedtls_snprintf( p, n, "\n%sbasic constraints : CA=%s", prefix,
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002139 crt->ca_istrue ? "true" : "false" );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002140 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002141
2142 if( crt->max_pathlen > 0 )
2143 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002144 ret = mbedtls_snprintf( p, n, ", max_pathlen=%d", crt->max_pathlen - 1 );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002145 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002146 }
2147 }
2148
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01002149 if( crt->ext_types & MBEDTLS_X509_EXT_SUBJECT_ALT_NAME )
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002150 {
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02002151 ret = mbedtls_snprintf( p, n, "\n%ssubject alt name :", prefix );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002152 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02002153
2154 if( ( ret = x509_info_subject_alt_name( &p, &n,
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02002155 &crt->subject_alt_names,
Ron Eldor6aeae9e2019-05-20 12:00:36 +03002156 prefix ) ) != 0 )
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02002157 return( ret );
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002158 }
2159
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002160 if( crt->ext_types & MBEDTLS_X509_EXT_NS_CERT_TYPE )
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002161 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002162 ret = mbedtls_snprintf( p, n, "\n%scert. type : ", prefix );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002163 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnard919f8f52014-04-01 13:01:11 +02002164
2165 if( ( ret = x509_info_cert_type( &p, &n, crt->ns_cert_type ) ) != 0 )
2166 return( ret );
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002167 }
2168
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002169 if( crt->ext_types & MBEDTLS_X509_EXT_KEY_USAGE )
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002170 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002171 ret = mbedtls_snprintf( p, n, "\n%skey usage : ", prefix );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002172 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnard65c2ddc2014-04-01 14:12:11 +02002173
2174 if( ( ret = x509_info_key_usage( &p, &n, crt->key_usage ) ) != 0 )
2175 return( ret );
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002176 }
2177
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002178 if( crt->ext_types & MBEDTLS_X509_EXT_EXTENDED_KEY_USAGE )
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002179 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002180 ret = mbedtls_snprintf( p, n, "\n%sext key usage : ", prefix );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002181 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02002182
2183 if( ( ret = x509_info_ext_key_usage( &p, &n,
2184 &crt->ext_key_usage ) ) != 0 )
2185 return( ret );
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002186 }
2187
Ron Eldor74d9acc2019-03-21 14:00:03 +02002188 if( crt->ext_types & MBEDTLS_OID_X509_EXT_CERTIFICATE_POLICIES )
2189 {
2190 ret = mbedtls_snprintf( p, n, "\n%scertificate policies : ", prefix );
2191 MBEDTLS_X509_SAFE_SNPRINTF;
2192
2193 if( ( ret = x509_info_cert_policies( &p, &n,
2194 &crt->certificate_policies ) ) != 0 )
2195 return( ret );
2196 }
2197
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002198 ret = mbedtls_snprintf( p, n, "\n" );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002199 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002200
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002201 return( (int) ( size - n ) );
2202}
2203
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01002204struct x509_crt_verify_string {
2205 int code;
2206 const char *string;
2207};
2208
2209static const struct x509_crt_verify_string x509_crt_verify_strings[] = {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01002210 { MBEDTLS_X509_BADCERT_EXPIRED, "The certificate validity has expired" },
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01002211 { MBEDTLS_X509_BADCERT_REVOKED, "The certificate has been revoked (is on a CRL)" },
2212 { MBEDTLS_X509_BADCERT_CN_MISMATCH, "The certificate Common Name (CN) does not match with the expected CN" },
2213 { MBEDTLS_X509_BADCERT_NOT_TRUSTED, "The certificate is not correctly signed by the trusted CA" },
2214 { MBEDTLS_X509_BADCRL_NOT_TRUSTED, "The CRL is not correctly signed by the trusted CA" },
2215 { MBEDTLS_X509_BADCRL_EXPIRED, "The CRL is expired" },
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01002216 { MBEDTLS_X509_BADCERT_MISSING, "Certificate was missing" },
2217 { MBEDTLS_X509_BADCERT_SKIP_VERIFY, "Certificate verification was skipped" },
2218 { MBEDTLS_X509_BADCERT_OTHER, "Other reason (can be used by verify callback)" },
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01002219 { MBEDTLS_X509_BADCERT_FUTURE, "The certificate validity starts in the future" },
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01002220 { MBEDTLS_X509_BADCRL_FUTURE, "The CRL is from the future" },
2221 { MBEDTLS_X509_BADCERT_KEY_USAGE, "Usage does not match the keyUsage extension" },
2222 { MBEDTLS_X509_BADCERT_EXT_KEY_USAGE, "Usage does not match the extendedKeyUsage extension" },
2223 { MBEDTLS_X509_BADCERT_NS_CERT_TYPE, "Usage does not match the nsCertType extension" },
Manuel Pégourié-Gonnard95051642015-06-15 10:39:46 +02002224 { MBEDTLS_X509_BADCERT_BAD_MD, "The certificate is signed with an unacceptable hash." },
2225 { MBEDTLS_X509_BADCERT_BAD_PK, "The certificate is signed with an unacceptable PK alg (eg RSA vs ECDSA)." },
2226 { MBEDTLS_X509_BADCERT_BAD_KEY, "The certificate is signed with an unacceptable key (eg bad curve, RSA too short)." },
2227 { MBEDTLS_X509_BADCRL_BAD_MD, "The CRL is signed with an unacceptable hash." },
2228 { MBEDTLS_X509_BADCRL_BAD_PK, "The CRL is signed with an unacceptable PK alg (eg RSA vs ECDSA)." },
2229 { 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 +01002230 { 0, NULL }
2231};
2232
2233int mbedtls_x509_crt_verify_info( char *buf, size_t size, const char *prefix,
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02002234 uint32_t flags )
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01002235{
Janos Follath865b3eb2019-12-16 11:46:15 +00002236 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01002237 const struct x509_crt_verify_string *cur;
2238 char *p = buf;
2239 size_t n = size;
2240
2241 for( cur = x509_crt_verify_strings; cur->string != NULL ; cur++ )
2242 {
2243 if( ( flags & cur->code ) == 0 )
2244 continue;
2245
2246 ret = mbedtls_snprintf( p, n, "%s%s\n", prefix, cur->string );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002247 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01002248 flags ^= cur->code;
2249 }
2250
2251 if( flags != 0 )
2252 {
2253 ret = mbedtls_snprintf( p, n, "%sUnknown reason "
2254 "(this should not happen)\n", prefix );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002255 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01002256 }
2257
2258 return( (int) ( size - n ) );
2259}
2260
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002261#if defined(MBEDTLS_X509_CHECK_KEY_USAGE)
Manuel Pégourié-Gonnard655a9642015-06-23 10:48:44 +02002262int mbedtls_x509_crt_check_key_usage( const mbedtls_x509_crt *crt,
2263 unsigned int usage )
Manuel Pégourié-Gonnard603116c2014-04-09 09:50:03 +02002264{
Manuel Pégourié-Gonnard655a9642015-06-23 10:48:44 +02002265 unsigned int usage_must, usage_may;
2266 unsigned int may_mask = MBEDTLS_X509_KU_ENCIPHER_ONLY
2267 | MBEDTLS_X509_KU_DECIPHER_ONLY;
2268
2269 if( ( crt->ext_types & MBEDTLS_X509_EXT_KEY_USAGE ) == 0 )
2270 return( 0 );
2271
2272 usage_must = usage & ~may_mask;
2273
2274 if( ( ( crt->key_usage & ~may_mask ) & usage_must ) != usage_must )
2275 return( MBEDTLS_ERR_X509_BAD_INPUT_DATA );
2276
2277 usage_may = usage & may_mask;
2278
2279 if( ( ( crt->key_usage & may_mask ) | usage_may ) != usage_may )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002280 return( MBEDTLS_ERR_X509_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard603116c2014-04-09 09:50:03 +02002281
2282 return( 0 );
2283}
2284#endif
2285
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002286#if defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE)
2287int mbedtls_x509_crt_check_extended_key_usage( const mbedtls_x509_crt *crt,
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02002288 const char *usage_oid,
2289 size_t usage_len )
2290{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002291 const mbedtls_x509_sequence *cur;
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02002292
2293 /* Extension is not mandatory, absent means no restriction */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002294 if( ( crt->ext_types & MBEDTLS_X509_EXT_EXTENDED_KEY_USAGE ) == 0 )
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02002295 return( 0 );
2296
2297 /*
2298 * Look for the requested usage (or wildcard ANY) in our list
2299 */
2300 for( cur = &crt->ext_key_usage; cur != NULL; cur = cur->next )
2301 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002302 const mbedtls_x509_buf *cur_oid = &cur->buf;
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02002303
2304 if( cur_oid->len == usage_len &&
2305 memcmp( cur_oid->p, usage_oid, usage_len ) == 0 )
2306 {
2307 return( 0 );
2308 }
2309
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002310 if( MBEDTLS_OID_CMP( MBEDTLS_OID_ANY_EXTENDED_KEY_USAGE, cur_oid ) == 0 )
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02002311 return( 0 );
2312 }
2313
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002314 return( MBEDTLS_ERR_X509_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02002315}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002316#endif /* MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE */
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02002317
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002318#if defined(MBEDTLS_X509_CRL_PARSE_C)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002319/*
2320 * Return 1 if the certificate is revoked, or 0 otherwise.
2321 */
Manuel Pégourié-Gonnardc730ed32015-06-02 10:38:50 +01002322int mbedtls_x509_crt_is_revoked( const mbedtls_x509_crt *crt, const mbedtls_x509_crl *crl )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002323{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002324 const mbedtls_x509_crl_entry *cur = &crl->entry;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002325
2326 while( cur != NULL && cur->serial.len != 0 )
2327 {
2328 if( crt->serial.len == cur->serial.len &&
2329 memcmp( crt->serial.p, cur->serial.p, crt->serial.len ) == 0 )
2330 {
Manuel Pégourié-Gonnardc730ed32015-06-02 10:38:50 +01002331 if( mbedtls_x509_time_is_past( &cur->revocation_date ) )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002332 return( 1 );
2333 }
2334
2335 cur = cur->next;
2336 }
2337
2338 return( 0 );
2339}
2340
2341/*
Manuel Pégourié-Gonnardeeef9472016-02-22 11:36:55 +01002342 * Check that the given certificate is not revoked according to the CRL.
Manuel Pégourié-Gonnard08eacec2017-10-18 14:20:24 +02002343 * Skip validation if no CRL for the given CA is present.
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002344 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002345static int x509_crt_verifycrl( mbedtls_x509_crt *crt, mbedtls_x509_crt *ca,
Manuel Pégourié-Gonnard95051642015-06-15 10:39:46 +02002346 mbedtls_x509_crl *crl_list,
2347 const mbedtls_x509_crt_profile *profile )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002348{
2349 int flags = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002350 unsigned char hash[MBEDTLS_MD_MAX_SIZE];
2351 const mbedtls_md_info_t *md_info;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002352
2353 if( ca == NULL )
2354 return( flags );
2355
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002356 while( crl_list != NULL )
2357 {
2358 if( crl_list->version == 0 ||
Hanno Beckerb75ffb52018-11-02 09:19:54 +00002359 x509_name_cmp( &crl_list->issuer, &ca->subject ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002360 {
2361 crl_list = crl_list->next;
2362 continue;
2363 }
2364
2365 /*
Manuel Pégourié-Gonnard99d4f192014-04-08 15:10:07 +02002366 * Check if the CA is configured to sign CRLs
2367 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002368#if defined(MBEDTLS_X509_CHECK_KEY_USAGE)
Hanno Beckerb75ffb52018-11-02 09:19:54 +00002369 if( mbedtls_x509_crt_check_key_usage( ca,
2370 MBEDTLS_X509_KU_CRL_SIGN ) != 0 )
Manuel Pégourié-Gonnard99d4f192014-04-08 15:10:07 +02002371 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002372 flags |= MBEDTLS_X509_BADCRL_NOT_TRUSTED;
Manuel Pégourié-Gonnard99d4f192014-04-08 15:10:07 +02002373 break;
2374 }
2375#endif
2376
2377 /*
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002378 * Check if CRL is correctly signed by the trusted CA
2379 */
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +02002380 if( x509_profile_check_md_alg( profile, crl_list->sig_md ) != 0 )
2381 flags |= MBEDTLS_X509_BADCRL_BAD_MD;
2382
2383 if( x509_profile_check_pk_alg( profile, crl_list->sig_pk ) != 0 )
2384 flags |= MBEDTLS_X509_BADCRL_BAD_PK;
2385
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002386 md_info = mbedtls_md_info_from_type( crl_list->sig_md );
Manuel Pégourié-Gonnard329e78c2017-06-26 12:22:17 +02002387 if( mbedtls_md( md_info, crl_list->tbs.p, crl_list->tbs.len, hash ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002388 {
Manuel Pégourié-Gonnard329e78c2017-06-26 12:22:17 +02002389 /* Note: this can't happen except after an internal error */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002390 flags |= MBEDTLS_X509_BADCRL_NOT_TRUSTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002391 break;
2392 }
2393
Manuel Pégourié-Gonnard3f816912017-10-26 10:24:16 +02002394 if( x509_profile_check_key( profile, &ca->pk ) != 0 )
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +02002395 flags |= MBEDTLS_X509_BADCERT_BAD_KEY;
Manuel Pégourié-Gonnard95051642015-06-15 10:39:46 +02002396
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002397 if( mbedtls_pk_verify_ext( crl_list->sig_pk, crl_list->sig_opts, &ca->pk,
2398 crl_list->sig_md, hash, mbedtls_md_get_size( md_info ),
Manuel Pégourié-Gonnard53882022014-06-05 17:53:52 +02002399 crl_list->sig.p, crl_list->sig.len ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002400 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002401 flags |= MBEDTLS_X509_BADCRL_NOT_TRUSTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002402 break;
2403 }
2404
2405 /*
2406 * Check for validity of CRL (Do not drop out)
2407 */
Manuel Pégourié-Gonnardc730ed32015-06-02 10:38:50 +01002408 if( mbedtls_x509_time_is_past( &crl_list->next_update ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002409 flags |= MBEDTLS_X509_BADCRL_EXPIRED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002410
Manuel Pégourié-Gonnardc730ed32015-06-02 10:38:50 +01002411 if( mbedtls_x509_time_is_future( &crl_list->this_update ) )
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01002412 flags |= MBEDTLS_X509_BADCRL_FUTURE;
Manuel Pégourié-Gonnard95337652014-03-10 13:15:18 +01002413
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002414 /*
2415 * Check if certificate is revoked
2416 */
Manuel Pégourié-Gonnardc730ed32015-06-02 10:38:50 +01002417 if( mbedtls_x509_crt_is_revoked( crt, crl_list ) )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002418 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002419 flags |= MBEDTLS_X509_BADCERT_REVOKED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002420 break;
2421 }
2422
2423 crl_list = crl_list->next;
2424 }
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +02002425
Paul Bakkerd8bb8262014-06-17 14:06:49 +02002426 return( flags );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002427}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002428#endif /* MBEDTLS_X509_CRL_PARSE_C */
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002429
Manuel Pégourié-Gonnard88421242014-10-17 11:36:18 +02002430/*
Manuel Pégourié-Gonnardf82a4d52017-07-03 19:26:25 +02002431 * Check the signature of a certificate by its parent
2432 */
2433static int x509_crt_check_signature( const mbedtls_x509_crt *child,
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002434 mbedtls_x509_crt *parent,
2435 mbedtls_x509_crt_restart_ctx *rs_ctx )
Manuel Pégourié-Gonnardf82a4d52017-07-03 19:26:25 +02002436{
Manuel Pégourié-Gonnardf82a4d52017-07-03 19:26:25 +02002437 unsigned char hash[MBEDTLS_MD_MAX_SIZE];
Andrzej Kurekd4a65532018-10-31 06:18:39 -04002438 size_t hash_len;
2439#if !defined(MBEDTLS_USE_PSA_CRYPTO)
2440 const mbedtls_md_info_t *md_info;
Manuel Pégourié-Gonnardf82a4d52017-07-03 19:26:25 +02002441 md_info = mbedtls_md_info_from_type( child->sig_md );
Andrzej Kurekd4a65532018-10-31 06:18:39 -04002442 hash_len = mbedtls_md_get_size( md_info );
Andrzej Kurek8b38ff52018-11-20 03:20:09 -05002443
Andrzej Kurekd4a65532018-10-31 06:18:39 -04002444 /* Note: hash errors can happen only after an internal error */
Manuel Pégourié-Gonnardf82a4d52017-07-03 19:26:25 +02002445 if( mbedtls_md( md_info, child->tbs.p, child->tbs.len, hash ) != 0 )
Andrzej Kurekd4a65532018-10-31 06:18:39 -04002446 return( -1 );
2447#else
Jaeden Amero34973232019-02-20 10:32:28 +00002448 psa_hash_operation_t hash_operation = PSA_HASH_OPERATION_INIT;
Andrzej Kurekd4a65532018-10-31 06:18:39 -04002449 psa_algorithm_t hash_alg = mbedtls_psa_translate_md( child->sig_md );
2450
2451 if( psa_hash_setup( &hash_operation, hash_alg ) != PSA_SUCCESS )
2452 return( -1 );
2453
2454 if( psa_hash_update( &hash_operation, child->tbs.p, child->tbs.len )
2455 != PSA_SUCCESS )
Manuel Pégourié-Gonnardf82a4d52017-07-03 19:26:25 +02002456 {
Manuel Pégourié-Gonnardf82a4d52017-07-03 19:26:25 +02002457 return( -1 );
2458 }
2459
Andrzej Kurekd4a65532018-10-31 06:18:39 -04002460 if( psa_hash_finish( &hash_operation, hash, sizeof( hash ), &hash_len )
2461 != PSA_SUCCESS )
2462 {
Andrzej Kurekd4a65532018-10-31 06:18:39 -04002463 return( -1 );
2464 }
2465#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002466 /* Skip expensive computation on obvious mismatch */
2467 if( ! mbedtls_pk_can_do( &parent->pk, child->sig_pk ) )
Manuel Pégourié-Gonnardf82a4d52017-07-03 19:26:25 +02002468 return( -1 );
Manuel Pégourié-Gonnardf82a4d52017-07-03 19:26:25 +02002469
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002470#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002471 if( rs_ctx != NULL && child->sig_pk == MBEDTLS_PK_ECDSA )
2472 {
2473 return( mbedtls_pk_verify_restartable( &parent->pk,
Andrzej Kurekd4a65532018-10-31 06:18:39 -04002474 child->sig_md, hash, hash_len,
Manuel Pégourié-Gonnard15d7df22017-08-17 14:33:31 +02002475 child->sig.p, child->sig.len, &rs_ctx->pk ) );
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002476 }
2477#else
2478 (void) rs_ctx;
2479#endif
2480
2481 return( mbedtls_pk_verify_ext( child->sig_pk, child->sig_opts, &parent->pk,
Andrzej Kurekd4a65532018-10-31 06:18:39 -04002482 child->sig_md, hash, hash_len,
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002483 child->sig.p, child->sig.len ) );
Manuel Pégourié-Gonnardf82a4d52017-07-03 19:26:25 +02002484}
2485
2486/*
Manuel Pégourié-Gonnard312010e2014-04-09 14:30:11 +02002487 * Check if 'parent' is a suitable parent (signing CA) for 'child'.
2488 * Return 0 if yes, -1 if not.
Manuel Pégourié-Gonnardd249b7a2014-06-24 11:49:16 +02002489 *
2490 * top means parent is a locally-trusted certificate
Manuel Pégourié-Gonnard3fed0b32014-04-08 13:18:01 +02002491 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002492static int x509_crt_check_parent( const mbedtls_x509_crt *child,
2493 const mbedtls_x509_crt *parent,
Manuel Pégourié-Gonnard27e94792017-07-04 00:49:31 +02002494 int top )
Manuel Pégourié-Gonnard3fed0b32014-04-08 13:18:01 +02002495{
Manuel Pégourié-Gonnardd249b7a2014-06-24 11:49:16 +02002496 int need_ca_bit;
2497
Manuel Pégourié-Gonnardc4eff162014-06-19 12:18:08 +02002498 /* Parent must be the issuer */
Manuel Pégourié-Gonnardef9a6ae2014-10-17 12:25:12 +02002499 if( x509_name_cmp( &child->issuer, &parent->subject ) != 0 )
Manuel Pégourié-Gonnard312010e2014-04-09 14:30:11 +02002500 return( -1 );
Manuel Pégourié-Gonnard3fed0b32014-04-08 13:18:01 +02002501
Manuel Pégourié-Gonnardd249b7a2014-06-24 11:49:16 +02002502 /* Parent must have the basicConstraints CA bit set as a general rule */
2503 need_ca_bit = 1;
2504
2505 /* Exception: v1/v2 certificates that are locally trusted. */
2506 if( top && parent->version < 3 )
2507 need_ca_bit = 0;
2508
Manuel Pégourié-Gonnardd249b7a2014-06-24 11:49:16 +02002509 if( need_ca_bit && ! parent->ca_istrue )
2510 return( -1 );
2511
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002512#if defined(MBEDTLS_X509_CHECK_KEY_USAGE)
Manuel Pégourié-Gonnardd249b7a2014-06-24 11:49:16 +02002513 if( need_ca_bit &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002514 mbedtls_x509_crt_check_key_usage( parent, MBEDTLS_X509_KU_KEY_CERT_SIGN ) != 0 )
Manuel Pégourié-Gonnardc4eff162014-06-19 12:18:08 +02002515 {
2516 return( -1 );
2517 }
Manuel Pégourié-Gonnard312010e2014-04-09 14:30:11 +02002518#endif
2519
2520 return( 0 );
Manuel Pégourié-Gonnard3fed0b32014-04-08 13:18:01 +02002521}
2522
Manuel Pégourié-Gonnard35407c72017-06-29 10:45:25 +02002523/*
Manuel Pégourié-Gonnard3e329b82017-06-29 12:55:27 +02002524 * Find a suitable parent for child in candidates, or return NULL.
2525 *
2526 * Here suitable is defined as:
Manuel Pégourié-Gonnard2f09d592017-07-03 18:30:43 +02002527 * 1. subject name matches child's issuer
2528 * 2. if necessary, the CA bit is set and key usage allows signing certs
2529 * 3. for trusted roots, the signature is correct
Manuel Pégourié-Gonnardbe4ff422017-07-14 12:04:14 +02002530 * (for intermediates, the signature is checked and the result reported)
Manuel Pégourié-Gonnard2f09d592017-07-03 18:30:43 +02002531 * 4. pathlen constraints are satisfied
Manuel Pégourié-Gonnard3e329b82017-06-29 12:55:27 +02002532 *
Manuel Pégourié-Gonnard562df402017-08-08 18:09:14 +02002533 * If there's a suitable candidate which is also time-valid, return the first
2534 * such. Otherwise, return the first suitable candidate (or NULL if there is
2535 * none).
Manuel Pégourié-Gonnard3e329b82017-06-29 12:55:27 +02002536 *
2537 * The rationale for this rule is that someone could have a list of trusted
2538 * roots with two versions on the same root with different validity periods.
2539 * (At least one user reported having such a list and wanted it to just work.)
2540 * The reason we don't just require time-validity is that generally there is
2541 * only one version, and if it's expired we want the flags to state that
2542 * rather than NOT_TRUSTED, as would be the case if we required it here.
Manuel Pégourié-Gonnard2f09d592017-07-03 18:30:43 +02002543 *
2544 * The rationale for rule 3 (signature for trusted roots) is that users might
2545 * have two versions of the same CA with different keys in their list, and the
Manuel Pégourié-Gonnardbdc54402017-07-04 00:33:39 +02002546 * way we select the correct one is by checking the signature (as we don't
2547 * rely on key identifier extensions). (This is one way users might choose to
2548 * handle key rollover, another relies on self-issued certs, see [SIRO].)
Manuel Pégourié-Gonnard98a67782017-08-17 10:52:20 +02002549 *
2550 * Arguments:
Manuel Pégourié-Gonnarde57d7432018-03-07 10:00:57 +01002551 * - [in] child: certificate for which we're looking for a parent
2552 * - [in] candidates: chained list of potential parents
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +02002553 * - [out] r_parent: parent found (or NULL)
2554 * - [out] r_signature_is_good: 1 if child signature by parent is valid, or 0
Manuel Pégourié-Gonnarde57d7432018-03-07 10:00:57 +01002555 * - [in] top: 1 if candidates consists of trusted roots, ie we're at the top
2556 * of the chain, 0 otherwise
2557 * - [in] path_cnt: number of intermediates seen so far
2558 * - [in] self_cnt: number of self-signed intermediates seen so far
2559 * (will never be greater than path_cnt)
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +02002560 * - [in-out] rs_ctx: context for restarting operations
Manuel Pégourié-Gonnarde57d7432018-03-07 10:00:57 +01002561 *
2562 * Return value:
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +02002563 * - 0 on success
2564 * - MBEDTLS_ERR_ECP_IN_PROGRESS otherwise
Manuel Pégourié-Gonnard2f1c33d2017-06-29 12:27:23 +02002565 */
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002566static int x509_crt_find_parent_in(
2567 mbedtls_x509_crt *child,
2568 mbedtls_x509_crt *candidates,
2569 mbedtls_x509_crt **r_parent,
2570 int *r_signature_is_good,
2571 int top,
Manuel Pégourié-Gonnardbb216bd2017-08-28 13:25:55 +02002572 unsigned path_cnt,
2573 unsigned self_cnt,
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002574 mbedtls_x509_crt_restart_ctx *rs_ctx )
Manuel Pégourié-Gonnard2f1c33d2017-06-29 12:27:23 +02002575{
Janos Follath865b3eb2019-12-16 11:46:15 +00002576 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002577 mbedtls_x509_crt *parent, *fallback_parent;
Benjamin Kier36050732019-05-30 14:49:17 -04002578 int signature_is_good = 0, fallback_signature_is_good;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002579
2580#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard3627a8b2017-08-23 11:20:48 +02002581 /* did we have something in progress? */
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002582 if( rs_ctx != NULL && rs_ctx->parent != NULL )
2583 {
Manuel Pégourié-Gonnard3627a8b2017-08-23 11:20:48 +02002584 /* restore saved state */
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002585 parent = rs_ctx->parent;
2586 fallback_parent = rs_ctx->fallback_parent;
Manuel Pégourié-Gonnard78d7e8c2018-07-02 12:33:14 +02002587 fallback_signature_is_good = rs_ctx->fallback_signature_is_good;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002588
Manuel Pégourié-Gonnard3627a8b2017-08-23 11:20:48 +02002589 /* clear saved state */
2590 rs_ctx->parent = NULL;
2591 rs_ctx->fallback_parent = NULL;
Manuel Pégourié-Gonnard78d7e8c2018-07-02 12:33:14 +02002592 rs_ctx->fallback_signature_is_good = 0;
Manuel Pégourié-Gonnard3627a8b2017-08-23 11:20:48 +02002593
2594 /* resume where we left */
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002595 goto check_signature;
2596 }
2597#endif
2598
2599 fallback_parent = NULL;
Manuel Pégourié-Gonnard78d7e8c2018-07-02 12:33:14 +02002600 fallback_signature_is_good = 0;
Manuel Pégourié-Gonnard2f1c33d2017-06-29 12:27:23 +02002601
Manuel Pégourié-Gonnard2f1c33d2017-06-29 12:27:23 +02002602 for( parent = candidates; parent != NULL; parent = parent->next )
2603 {
Manuel Pégourié-Gonnard2f09d592017-07-03 18:30:43 +02002604 /* basic parenting skills (name, CA bit, key usage) */
Manuel Pégourié-Gonnard27e94792017-07-04 00:49:31 +02002605 if( x509_crt_check_parent( child, parent, top ) != 0 )
Manuel Pégourié-Gonnard2f1c33d2017-06-29 12:27:23 +02002606 continue;
2607
Manuel Pégourié-Gonnard9c6118c2017-06-29 12:38:42 +02002608 /* +1 because stored max_pathlen is 1 higher that the actual value */
2609 if( parent->max_pathlen > 0 &&
Manuel Pégourié-Gonnardbb216bd2017-08-28 13:25:55 +02002610 (size_t) parent->max_pathlen < 1 + path_cnt - self_cnt )
Manuel Pégourié-Gonnard9c6118c2017-06-29 12:38:42 +02002611 {
2612 continue;
2613 }
2614
Manuel Pégourié-Gonnard2f09d592017-07-03 18:30:43 +02002615 /* Signature */
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002616#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
2617check_signature:
2618#endif
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002619 ret = x509_crt_check_signature( child, parent, rs_ctx );
2620
2621#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002622 if( rs_ctx != NULL && ret == MBEDTLS_ERR_ECP_IN_PROGRESS )
2623 {
2624 /* save state */
2625 rs_ctx->parent = parent;
2626 rs_ctx->fallback_parent = fallback_parent;
Manuel Pégourié-Gonnard78d7e8c2018-07-02 12:33:14 +02002627 rs_ctx->fallback_signature_is_good = fallback_signature_is_good;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002628
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002629 return( ret );
2630 }
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002631#else
2632 (void) ret;
2633#endif
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002634
2635 signature_is_good = ret == 0;
2636 if( top && ! signature_is_good )
Manuel Pégourié-Gonnardf82a4d52017-07-03 19:26:25 +02002637 continue;
Manuel Pégourié-Gonnard2f09d592017-07-03 18:30:43 +02002638
Manuel Pégourié-Gonnard562df402017-08-08 18:09:14 +02002639 /* optional time check */
Manuel Pégourié-Gonnard3e329b82017-06-29 12:55:27 +02002640 if( mbedtls_x509_time_is_past( &parent->valid_to ) ||
2641 mbedtls_x509_time_is_future( &parent->valid_from ) )
2642 {
Manuel Pégourié-Gonnardbe4ff422017-07-14 12:04:14 +02002643 if( fallback_parent == NULL )
2644 {
2645 fallback_parent = parent;
Manuel Pégourié-Gonnard78d7e8c2018-07-02 12:33:14 +02002646 fallback_signature_is_good = signature_is_good;
Manuel Pégourié-Gonnardbe4ff422017-07-14 12:04:14 +02002647 }
Manuel Pégourié-Gonnard3e329b82017-06-29 12:55:27 +02002648
2649 continue;
2650 }
2651
Andy Gross1f627142019-01-30 10:25:53 -06002652 *r_parent = parent;
2653 *r_signature_is_good = signature_is_good;
2654
Manuel Pégourié-Gonnard2f1c33d2017-06-29 12:27:23 +02002655 break;
2656 }
2657
Andy Gross1f627142019-01-30 10:25:53 -06002658 if( parent == NULL )
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002659 {
2660 *r_parent = fallback_parent;
Manuel Pégourié-Gonnard78d7e8c2018-07-02 12:33:14 +02002661 *r_signature_is_good = fallback_signature_is_good;
Manuel Pégourié-Gonnardbe4ff422017-07-14 12:04:14 +02002662 }
Manuel Pégourié-Gonnard3e329b82017-06-29 12:55:27 +02002663
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002664 return( 0 );
Manuel Pégourié-Gonnard2f1c33d2017-06-29 12:27:23 +02002665}
2666
2667/*
Manuel Pégourié-Gonnard63686122017-07-04 01:01:39 +02002668 * Find a parent in trusted CAs or the provided chain, or return NULL.
2669 *
2670 * Searches in trusted CAs first, and return the first suitable parent found
2671 * (see find_parent_in() for definition of suitable).
Manuel Pégourié-Gonnard98a67782017-08-17 10:52:20 +02002672 *
2673 * Arguments:
Manuel Pégourié-Gonnarde57d7432018-03-07 10:00:57 +01002674 * - [in] child: certificate for which we're looking for a parent, followed
2675 * by a chain of possible intermediates
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +02002676 * - [in] trust_ca: list of locally trusted certificates
2677 * - [out] parent: parent found (or NULL)
2678 * - [out] parent_is_trusted: 1 if returned `parent` is trusted, or 0
2679 * - [out] signature_is_good: 1 if child signature by parent is valid, or 0
2680 * - [in] path_cnt: number of links in the chain so far (EE -> ... -> child)
2681 * - [in] self_cnt: number of self-signed certs in the chain so far
Manuel Pégourié-Gonnarde57d7432018-03-07 10:00:57 +01002682 * (will always be no greater than path_cnt)
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +02002683 * - [in-out] rs_ctx: context for restarting operations
Manuel Pégourié-Gonnarde57d7432018-03-07 10:00:57 +01002684 *
2685 * Return value:
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +02002686 * - 0 on success
2687 * - MBEDTLS_ERR_ECP_IN_PROGRESS otherwise
Manuel Pégourié-Gonnard63686122017-07-04 01:01:39 +02002688 */
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002689static int x509_crt_find_parent(
2690 mbedtls_x509_crt *child,
2691 mbedtls_x509_crt *trust_ca,
2692 mbedtls_x509_crt **parent,
2693 int *parent_is_trusted,
2694 int *signature_is_good,
Manuel Pégourié-Gonnardbb216bd2017-08-28 13:25:55 +02002695 unsigned path_cnt,
2696 unsigned self_cnt,
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002697 mbedtls_x509_crt_restart_ctx *rs_ctx )
Manuel Pégourié-Gonnard63686122017-07-04 01:01:39 +02002698{
Janos Follath865b3eb2019-12-16 11:46:15 +00002699 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard18547b52017-08-14 16:11:43 +02002700 mbedtls_x509_crt *search_list;
Manuel Pégourié-Gonnard63686122017-07-04 01:01:39 +02002701
Manuel Pégourié-Gonnard63686122017-07-04 01:01:39 +02002702 *parent_is_trusted = 1;
Manuel Pégourié-Gonnard18547b52017-08-14 16:11:43 +02002703
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002704#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard3627a8b2017-08-23 11:20:48 +02002705 /* restore then clear saved state if we have some stored */
2706 if( rs_ctx != NULL && rs_ctx->parent_is_trusted != -1 )
2707 {
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002708 *parent_is_trusted = rs_ctx->parent_is_trusted;
Manuel Pégourié-Gonnard3627a8b2017-08-23 11:20:48 +02002709 rs_ctx->parent_is_trusted = -1;
2710 }
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002711#endif
2712
Manuel Pégourié-Gonnard18547b52017-08-14 16:11:43 +02002713 while( 1 ) {
2714 search_list = *parent_is_trusted ? trust_ca : child->next;
2715
2716 ret = x509_crt_find_parent_in( child, search_list,
2717 parent, signature_is_good,
2718 *parent_is_trusted,
2719 path_cnt, self_cnt, rs_ctx );
Manuel Pégourié-Gonnard63686122017-07-04 01:01:39 +02002720
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002721#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002722 if( rs_ctx != NULL && ret == MBEDTLS_ERR_ECP_IN_PROGRESS )
2723 {
2724 /* save state */
2725 rs_ctx->parent_is_trusted = *parent_is_trusted;
Manuel Pégourié-Gonnard18547b52017-08-14 16:11:43 +02002726 return( ret );
2727 }
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002728#else
2729 (void) ret;
2730#endif
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002731
Manuel Pégourié-Gonnard18547b52017-08-14 16:11:43 +02002732 /* stop here if found or already in second iteration */
2733 if( *parent != NULL || *parent_is_trusted == 0 )
2734 break;
Manuel Pégourié-Gonnard63686122017-07-04 01:01:39 +02002735
Manuel Pégourié-Gonnard18547b52017-08-14 16:11:43 +02002736 /* prepare second iteration */
2737 *parent_is_trusted = 0;
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002738 }
Manuel Pégourié-Gonnard18547b52017-08-14 16:11:43 +02002739
2740 /* extra precaution against mistakes in the caller */
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05002741 if( *parent == NULL )
Manuel Pégourié-Gonnard18547b52017-08-14 16:11:43 +02002742 {
Manuel Pégourié-Gonnarda5a3e402018-10-16 11:27:23 +02002743 *parent_is_trusted = 0;
2744 *signature_is_good = 0;
Manuel Pégourié-Gonnard18547b52017-08-14 16:11:43 +02002745 }
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002746
2747 return( 0 );
Manuel Pégourié-Gonnard63686122017-07-04 01:01:39 +02002748}
2749
2750/*
Manuel Pégourié-Gonnard27e94792017-07-04 00:49:31 +02002751 * Check if an end-entity certificate is locally trusted
2752 *
2753 * Currently we require such certificates to be self-signed (actually only
2754 * check for self-issued as self-signatures are not checked)
2755 */
2756static int x509_crt_check_ee_locally_trusted(
2757 mbedtls_x509_crt *crt,
2758 mbedtls_x509_crt *trust_ca )
2759{
2760 mbedtls_x509_crt *cur;
2761
2762 /* must be self-issued */
2763 if( x509_name_cmp( &crt->issuer, &crt->subject ) != 0 )
2764 return( -1 );
2765
2766 /* look for an exact match with trusted cert */
2767 for( cur = trust_ca; cur != NULL; cur = cur->next )
2768 {
2769 if( crt->raw.len == cur->raw.len &&
2770 memcmp( crt->raw.p, cur->raw.p, crt->raw.len ) == 0 )
2771 {
2772 return( 0 );
2773 }
2774 }
2775
2776 /* too bad */
2777 return( -1 );
2778}
2779
2780/*
Manuel Pégourié-Gonnardf86f4912017-07-05 16:43:44 +02002781 * Build and verify a certificate chain
Manuel Pégourié-Gonnard35407c72017-06-29 10:45:25 +02002782 *
Manuel Pégourié-Gonnardf86f4912017-07-05 16:43:44 +02002783 * Given a peer-provided list of certificates EE, C1, ..., Cn and
2784 * a list of trusted certs R1, ... Rp, try to build and verify a chain
Manuel Pégourié-Gonnard562df402017-08-08 18:09:14 +02002785 * EE, Ci1, ... Ciq [, Rj]
Manuel Pégourié-Gonnardf86f4912017-07-05 16:43:44 +02002786 * such that every cert in the chain is a child of the next one,
2787 * jumping to a trusted root as early as possible.
Manuel Pégourié-Gonnardbdc54402017-07-04 00:33:39 +02002788 *
Manuel Pégourié-Gonnardf86f4912017-07-05 16:43:44 +02002789 * Verify that chain and return it with flags for all issues found.
2790 *
2791 * Special cases:
2792 * - EE == Rj -> return a one-element list containing it
2793 * - EE, Ci1, ..., Ciq cannot be continued with a trusted root
2794 * -> return that chain with NOT_TRUSTED set on Ciq
Manuel Pégourié-Gonnardbdc54402017-07-04 00:33:39 +02002795 *
Manuel Pégourié-Gonnardd19a41d2017-07-14 11:05:59 +02002796 * Tests for (aspects of) this function should include at least:
2797 * - trusted EE
2798 * - EE -> trusted root
Antonin Décimo36e89b52019-01-23 15:24:37 +01002799 * - EE -> intermediate CA -> trusted root
Manuel Pégourié-Gonnardd19a41d2017-07-14 11:05:59 +02002800 * - if relevant: EE untrusted
2801 * - if relevant: EE -> intermediate, untrusted
2802 * with the aspect under test checked at each relevant level (EE, int, root).
2803 * For some aspects longer chains are required, but usually length 2 is
2804 * enough (but length 1 is not in general).
2805 *
Manuel Pégourié-Gonnardbdc54402017-07-04 00:33:39 +02002806 * Arguments:
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002807 * - [in] crt: the cert list EE, C1, ..., Cn
2808 * - [in] trust_ca: the trusted list R1, ..., Rp
2809 * - [in] ca_crl, profile: as in verify_with_profile()
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02002810 * - [out] ver_chain: the built and verified chain
Manuel Pégourié-Gonnarda9688432017-08-23 11:23:59 +02002811 * Only valid when return value is 0, may contain garbage otherwise!
2812 * Restart note: need not be the same when calling again to resume.
Manuel Pégourié-Gonnard98a67782017-08-17 10:52:20 +02002813 * - [in-out] rs_ctx: context for restarting operations
Manuel Pégourié-Gonnardf86f4912017-07-05 16:43:44 +02002814 *
2815 * Return value:
2816 * - non-zero if the chain could not be fully built and examined
2817 * - 0 is the chain was successfully built and examined,
2818 * even if it was found to be invalid
Manuel Pégourié-Gonnard35407c72017-06-29 10:45:25 +02002819 */
Manuel Pégourié-Gonnardbdc54402017-07-04 00:33:39 +02002820static int x509_crt_verify_chain(
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002821 mbedtls_x509_crt *crt,
2822 mbedtls_x509_crt *trust_ca,
2823 mbedtls_x509_crl *ca_crl,
Hanno Becker3116fb32019-03-28 13:34:42 +00002824 mbedtls_x509_crt_ca_cb_t f_ca_cb,
2825 void *p_ca_cb,
Manuel Pégourié-Gonnard95051642015-06-15 10:39:46 +02002826 const mbedtls_x509_crt_profile *profile,
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02002827 mbedtls_x509_crt_verify_chain *ver_chain,
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002828 mbedtls_x509_crt_restart_ctx *rs_ctx )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002829{
Manuel Pégourié-Gonnarda9688432017-08-23 11:23:59 +02002830 /* Don't initialize any of those variables here, so that the compiler can
2831 * catch potential issues with jumping ahead when restarting */
Janos Follath865b3eb2019-12-16 11:46:15 +00002832 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardf86f4912017-07-05 16:43:44 +02002833 uint32_t *flags;
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02002834 mbedtls_x509_crt_verify_chain_item *cur;
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002835 mbedtls_x509_crt *child;
Manuel Pégourié-Gonnard58dcd2d2017-07-03 21:35:04 +02002836 mbedtls_x509_crt *parent;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002837 int parent_is_trusted;
2838 int child_is_trusted;
2839 int signature_is_good;
Manuel Pégourié-Gonnardbb216bd2017-08-28 13:25:55 +02002840 unsigned self_cnt;
Hanno Beckerf53893b2019-03-28 13:45:55 +00002841 mbedtls_x509_crt *cur_trust_ca = NULL;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002842
2843#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
2844 /* resume if we had an operation in progress */
Manuel Pégourié-Gonnarddaf04912017-08-23 12:32:19 +02002845 if( rs_ctx != NULL && rs_ctx->in_progress == x509_crt_rs_find_parent )
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002846 {
Manuel Pégourié-Gonnard3627a8b2017-08-23 11:20:48 +02002847 /* restore saved state */
Manuel Pégourié-Gonnarda9688432017-08-23 11:23:59 +02002848 *ver_chain = rs_ctx->ver_chain; /* struct copy */
Manuel Pégourié-Gonnarddaf04912017-08-23 12:32:19 +02002849 self_cnt = rs_ctx->self_cnt;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002850
Manuel Pégourié-Gonnarddaf04912017-08-23 12:32:19 +02002851 /* restore derived state */
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002852 cur = &ver_chain->items[ver_chain->len - 1];
Manuel Pégourié-Gonnarddaf04912017-08-23 12:32:19 +02002853 child = cur->crt;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002854 flags = &cur->flags;
2855
2856 goto find_parent;
2857 }
2858#endif /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */
Manuel Pégourié-Gonnard8f8c2822017-07-03 21:25:10 +02002859
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002860 child = crt;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002861 self_cnt = 0;
2862 parent_is_trusted = 0;
2863 child_is_trusted = 0;
Manuel Pégourié-Gonnardf86f4912017-07-05 16:43:44 +02002864
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002865 while( 1 ) {
2866 /* Add certificate to the verification chain */
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02002867 cur = &ver_chain->items[ver_chain->len];
2868 cur->crt = child;
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +02002869 cur->flags = 0;
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02002870 ver_chain->len++;
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +02002871 flags = &cur->flags;
Manuel Pégourié-Gonnard66fac752017-07-03 21:39:21 +02002872
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002873 /* Check time-validity (all certificates) */
2874 if( mbedtls_x509_time_is_past( &child->valid_to ) )
2875 *flags |= MBEDTLS_X509_BADCERT_EXPIRED;
Manuel Pégourié-Gonnard66fac752017-07-03 21:39:21 +02002876
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002877 if( mbedtls_x509_time_is_future( &child->valid_from ) )
2878 *flags |= MBEDTLS_X509_BADCERT_FUTURE;
Manuel Pégourié-Gonnardcb396102017-07-04 00:00:24 +02002879
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002880 /* Stop here for trusted roots (but not for trusted EE certs) */
2881 if( child_is_trusted )
2882 return( 0 );
Manuel Pégourié-Gonnard66fac752017-07-03 21:39:21 +02002883
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002884 /* Check signature algorithm: MD & PK algs */
2885 if( x509_profile_check_md_alg( profile, child->sig_md ) != 0 )
2886 *flags |= MBEDTLS_X509_BADCERT_BAD_MD;
Manuel Pégourié-Gonnard66fac752017-07-03 21:39:21 +02002887
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002888 if( x509_profile_check_pk_alg( profile, child->sig_pk ) != 0 )
2889 *flags |= MBEDTLS_X509_BADCERT_BAD_PK;
Manuel Pégourié-Gonnard27e94792017-07-04 00:49:31 +02002890
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002891 /* Special case: EE certs that are locally trusted */
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02002892 if( ver_chain->len == 1 &&
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002893 x509_crt_check_ee_locally_trusted( child, trust_ca ) == 0 )
2894 {
2895 return( 0 );
2896 }
Manuel Pégourié-Gonnard8f8c2822017-07-03 21:25:10 +02002897
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002898#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
2899find_parent:
2900#endif
Hanno Beckerf53893b2019-03-28 13:45:55 +00002901
2902 /* Obtain list of potential trusted signers from CA callback,
2903 * or use statically provided list. */
2904#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
2905 if( f_ca_cb != NULL )
2906 {
2907 mbedtls_x509_crt_free( ver_chain->trust_ca_cb_result );
2908 mbedtls_free( ver_chain->trust_ca_cb_result );
2909 ver_chain->trust_ca_cb_result = NULL;
2910
2911 ret = f_ca_cb( p_ca_cb, child, &ver_chain->trust_ca_cb_result );
2912 if( ret != 0 )
2913 return( MBEDTLS_ERR_X509_FATAL_ERROR );
2914
2915 cur_trust_ca = ver_chain->trust_ca_cb_result;
2916 }
2917 else
2918#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
2919 {
2920 ((void) f_ca_cb);
2921 ((void) p_ca_cb);
2922 cur_trust_ca = trust_ca;
2923 }
2924
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002925 /* Look for a parent in trusted CAs or up the chain */
Hanno Beckerf53893b2019-03-28 13:45:55 +00002926 ret = x509_crt_find_parent( child, cur_trust_ca, &parent,
Manuel Pégourié-Gonnardbe4ff422017-07-14 12:04:14 +02002927 &parent_is_trusted, &signature_is_good,
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02002928 ver_chain->len - 1, self_cnt, rs_ctx );
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002929
2930#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002931 if( rs_ctx != NULL && ret == MBEDTLS_ERR_ECP_IN_PROGRESS )
2932 {
2933 /* save state */
Manuel Pégourié-Gonnarddaf04912017-08-23 12:32:19 +02002934 rs_ctx->in_progress = x509_crt_rs_find_parent;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002935 rs_ctx->self_cnt = self_cnt;
Manuel Pégourié-Gonnarda9688432017-08-23 11:23:59 +02002936 rs_ctx->ver_chain = *ver_chain; /* struct copy */
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002937
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002938 return( ret );
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002939 }
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002940#else
2941 (void) ret;
2942#endif
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002943
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002944 /* No parent? We're done here */
2945 if( parent == NULL )
2946 {
2947 *flags |= MBEDTLS_X509_BADCERT_NOT_TRUSTED;
2948 return( 0 );
2949 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002950
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002951 /* Count intermediate self-issued (not necessarily self-signed) certs.
2952 * These can occur with some strategies for key rollover, see [SIRO],
2953 * and should be excluded from max_pathlen checks. */
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02002954 if( ver_chain->len != 1 &&
Manuel Pégourié-Gonnard24611f92017-08-09 10:28:07 +02002955 x509_name_cmp( &child->issuer, &child->subject ) == 0 )
2956 {
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002957 self_cnt++;
Manuel Pégourié-Gonnard24611f92017-08-09 10:28:07 +02002958 }
Manuel Pégourié-Gonnardfd6c85c2014-11-20 16:34:20 +01002959
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002960 /* path_cnt is 0 for the first intermediate CA,
2961 * and if parent is trusted it's not an intermediate CA */
2962 if( ! parent_is_trusted &&
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02002963 ver_chain->len > MBEDTLS_X509_MAX_INTERMEDIATE_CA )
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002964 {
2965 /* return immediately to avoid overflow the chain array */
2966 return( MBEDTLS_ERR_X509_FATAL_ERROR );
2967 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002968
Manuel Pégourié-Gonnard98a67782017-08-17 10:52:20 +02002969 /* signature was checked while searching parent */
Manuel Pégourié-Gonnardbe4ff422017-07-14 12:04:14 +02002970 if( ! signature_is_good )
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002971 *flags |= MBEDTLS_X509_BADCERT_NOT_TRUSTED;
2972
2973 /* check size of signing key */
Manuel Pégourié-Gonnard3f816912017-10-26 10:24:16 +02002974 if( x509_profile_check_key( profile, &parent->pk ) != 0 )
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002975 *flags |= MBEDTLS_X509_BADCERT_BAD_KEY;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002976
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002977#if defined(MBEDTLS_X509_CRL_PARSE_C)
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002978 /* Check trusted CA's CRL for the given crt */
Manuel Pégourié-Gonnard562df402017-08-08 18:09:14 +02002979 *flags |= x509_crt_verifycrl( child, parent, ca_crl, profile );
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002980#else
2981 (void) ca_crl;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002982#endif
2983
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002984 /* prepare for next iteration */
2985 child = parent;
2986 parent = NULL;
2987 child_is_trusted = parent_is_trusted;
Manuel Pégourié-Gonnardbe4ff422017-07-14 12:04:14 +02002988 signature_is_good = 0;
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002989 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002990}
2991
2992/*
Manuel Pégourié-Gonnarda468eb12017-07-04 01:31:59 +02002993 * Check for CN match
2994 */
2995static int x509_crt_check_cn( const mbedtls_x509_buf *name,
2996 const char *cn, size_t cn_len )
2997{
2998 /* try exact match */
2999 if( name->len == cn_len &&
3000 x509_memcasecmp( cn, name->p, cn_len ) == 0 )
3001 {
3002 return( 0 );
3003 }
3004
3005 /* try wildcard match */
Manuel Pégourié-Gonnard900fba62017-10-18 14:28:11 +02003006 if( x509_check_wildcard( cn, name ) == 0 )
Manuel Pégourié-Gonnarda468eb12017-07-04 01:31:59 +02003007 {
3008 return( 0 );
3009 }
3010
3011 return( -1 );
3012}
3013
3014/*
Manuel Pégourié-Gonnard1300e992017-07-04 01:13:44 +02003015 * Verify the requested CN - only call this if cn is not NULL!
3016 */
Manuel Pégourié-Gonnarda468eb12017-07-04 01:31:59 +02003017static void x509_crt_verify_name( const mbedtls_x509_crt *crt,
Manuel Pégourié-Gonnard1300e992017-07-04 01:13:44 +02003018 const char *cn,
3019 uint32_t *flags )
3020{
Manuel Pégourié-Gonnarda468eb12017-07-04 01:31:59 +02003021 const mbedtls_x509_name *name;
3022 const mbedtls_x509_sequence *cur;
3023 size_t cn_len = strlen( cn );
Manuel Pégourié-Gonnard1300e992017-07-04 01:13:44 +02003024
3025 if( crt->ext_types & MBEDTLS_X509_EXT_SUBJECT_ALT_NAME )
3026 {
Manuel Pégourié-Gonnarda468eb12017-07-04 01:31:59 +02003027 for( cur = &crt->subject_alt_names; cur != NULL; cur = cur->next )
Manuel Pégourié-Gonnard1300e992017-07-04 01:13:44 +02003028 {
Manuel Pégourié-Gonnarda468eb12017-07-04 01:31:59 +02003029 if( x509_crt_check_cn( &cur->buf, cn, cn_len ) == 0 )
Manuel Pégourié-Gonnard1300e992017-07-04 01:13:44 +02003030 break;
Manuel Pégourié-Gonnard1300e992017-07-04 01:13:44 +02003031 }
3032
3033 if( cur == NULL )
3034 *flags |= MBEDTLS_X509_BADCERT_CN_MISMATCH;
3035 }
3036 else
3037 {
Manuel Pégourié-Gonnard08eacec2017-10-18 14:20:24 +02003038 for( name = &crt->subject; name != NULL; name = name->next )
Manuel Pégourié-Gonnard1300e992017-07-04 01:13:44 +02003039 {
Manuel Pégourié-Gonnarda468eb12017-07-04 01:31:59 +02003040 if( MBEDTLS_OID_CMP( MBEDTLS_OID_AT_CN, &name->oid ) == 0 &&
3041 x509_crt_check_cn( &name->val, cn, cn_len ) == 0 )
Manuel Pégourié-Gonnard1300e992017-07-04 01:13:44 +02003042 {
Manuel Pégourié-Gonnarda468eb12017-07-04 01:31:59 +02003043 break;
Manuel Pégourié-Gonnard1300e992017-07-04 01:13:44 +02003044 }
Manuel Pégourié-Gonnard1300e992017-07-04 01:13:44 +02003045 }
3046
3047 if( name == NULL )
3048 *flags |= MBEDTLS_X509_BADCERT_CN_MISMATCH;
3049 }
3050}
3051
3052/*
Manuel Pégourié-Gonnarda707e1d2017-07-05 17:18:42 +02003053 * Merge the flags for all certs in the chain, after calling callback
3054 */
3055static int x509_crt_merge_flags_with_cb(
3056 uint32_t *flags,
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02003057 const mbedtls_x509_crt_verify_chain *ver_chain,
Manuel Pégourié-Gonnarda707e1d2017-07-05 17:18:42 +02003058 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
3059 void *p_vrfy )
3060{
Janos Follath865b3eb2019-12-16 11:46:15 +00003061 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardbb216bd2017-08-28 13:25:55 +02003062 unsigned i;
Manuel Pégourié-Gonnarda707e1d2017-07-05 17:18:42 +02003063 uint32_t cur_flags;
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02003064 const mbedtls_x509_crt_verify_chain_item *cur;
Manuel Pégourié-Gonnarda707e1d2017-07-05 17:18:42 +02003065
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02003066 for( i = ver_chain->len; i != 0; --i )
Manuel Pégourié-Gonnarda707e1d2017-07-05 17:18:42 +02003067 {
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02003068 cur = &ver_chain->items[i-1];
3069 cur_flags = cur->flags;
Manuel Pégourié-Gonnarda707e1d2017-07-05 17:18:42 +02003070
3071 if( NULL != f_vrfy )
Manuel Pégourié-Gonnardbb216bd2017-08-28 13:25:55 +02003072 if( ( ret = f_vrfy( p_vrfy, cur->crt, (int) i-1, &cur_flags ) ) != 0 )
Manuel Pégourié-Gonnarda707e1d2017-07-05 17:18:42 +02003073 return( ret );
3074
3075 *flags |= cur_flags;
3076 }
3077
3078 return( 0 );
3079}
3080
3081/*
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +02003082 * Verify the certificate validity, with profile, restartable version
3083 *
3084 * This function:
3085 * - checks the requested CN (if any)
3086 * - checks the type and size of the EE cert's key,
3087 * as that isn't done as part of chain building/verification currently
3088 * - builds and verifies the chain
3089 * - then calls the callback and merges the flags
Hanno Becker3116fb32019-03-28 13:34:42 +00003090 *
3091 * The parameters pairs `trust_ca`, `ca_crl` and `f_ca_cb`, `p_ca_cb`
3092 * are mutually exclusive: If `f_ca_cb != NULL`, it will be used by the
3093 * verification routine to search for trusted signers, and CRLs will
3094 * be disabled. Otherwise, `trust_ca` will be used as the static list
3095 * of trusted signers, and `ca_crl` will be use as the static list
3096 * of CRLs.
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +02003097 */
Jarno Lamsa2ee67a62019-04-01 14:59:33 +03003098static int x509_crt_verify_restartable_ca_cb( mbedtls_x509_crt *crt,
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +02003099 mbedtls_x509_crt *trust_ca,
3100 mbedtls_x509_crl *ca_crl,
Hanno Becker3116fb32019-03-28 13:34:42 +00003101 mbedtls_x509_crt_ca_cb_t f_ca_cb,
3102 void *p_ca_cb,
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +02003103 const mbedtls_x509_crt_profile *profile,
3104 const char *cn, uint32_t *flags,
3105 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
3106 void *p_vrfy,
3107 mbedtls_x509_crt_restart_ctx *rs_ctx )
3108{
Janos Follath865b3eb2019-12-16 11:46:15 +00003109 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard65eefc82015-10-23 14:08:48 +02003110 mbedtls_pk_type_t pk_type;
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02003111 mbedtls_x509_crt_verify_chain ver_chain;
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +02003112 uint32_t ee_flags;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003113
3114 *flags = 0;
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +02003115 ee_flags = 0;
3116 x509_crt_verify_chain_reset( &ver_chain );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003117
Manuel Pégourié-Gonnardd15795a2017-06-22 12:19:27 +02003118 if( profile == NULL )
3119 {
3120 ret = MBEDTLS_ERR_X509_BAD_INPUT_DATA;
3121 goto exit;
3122 }
3123
Manuel Pégourié-Gonnard1300e992017-07-04 01:13:44 +02003124 /* check name if requested */
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003125 if( cn != NULL )
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +02003126 x509_crt_verify_name( crt, cn, &ee_flags );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003127
Manuel Pégourié-Gonnard65eefc82015-10-23 14:08:48 +02003128 /* Check the type and size of the key */
3129 pk_type = mbedtls_pk_get_type( &crt->pk );
3130
3131 if( x509_profile_check_pk_alg( profile, pk_type ) != 0 )
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +02003132 ee_flags |= MBEDTLS_X509_BADCERT_BAD_PK;
Manuel Pégourié-Gonnard65eefc82015-10-23 14:08:48 +02003133
Manuel Pégourié-Gonnard3f816912017-10-26 10:24:16 +02003134 if( x509_profile_check_key( profile, &crt->pk ) != 0 )
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +02003135 ee_flags |= MBEDTLS_X509_BADCERT_BAD_KEY;
Manuel Pégourié-Gonnard65eefc82015-10-23 14:08:48 +02003136
Manuel Pégourié-Gonnardbdc54402017-07-04 00:33:39 +02003137 /* Check the chain */
Hanno Becker3116fb32019-03-28 13:34:42 +00003138 ret = x509_crt_verify_chain( crt, trust_ca, ca_crl,
3139 f_ca_cb, p_ca_cb, profile,
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02003140 &ver_chain, rs_ctx );
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02003141
Manuel Pégourié-Gonnardc547d1a2017-07-05 13:28:45 +02003142 if( ret != 0 )
3143 goto exit;
3144
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +02003145 /* Merge end-entity flags */
3146 ver_chain.items[0].flags |= ee_flags;
3147
Manuel Pégourié-Gonnarda707e1d2017-07-05 17:18:42 +02003148 /* Build final flags, calling callback on the way if any */
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02003149 ret = x509_crt_merge_flags_with_cb( flags, &ver_chain, f_vrfy, p_vrfy );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003150
Manuel Pégourié-Gonnardd15795a2017-06-22 12:19:27 +02003151exit:
Hanno Beckerf53893b2019-03-28 13:45:55 +00003152
3153#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
3154 mbedtls_x509_crt_free( ver_chain.trust_ca_cb_result );
3155 mbedtls_free( ver_chain.trust_ca_cb_result );
3156 ver_chain.trust_ca_cb_result = NULL;
3157#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
3158
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02003159#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
3160 if( rs_ctx != NULL && ret != MBEDTLS_ERR_ECP_IN_PROGRESS )
3161 mbedtls_x509_crt_restart_free( rs_ctx );
3162#endif
3163
Manuel Pégourié-Gonnard9107b5f2017-07-06 12:16:25 +02003164 /* prevent misuse of the vrfy callback - VERIFY_FAILED would be ignored by
3165 * the SSL module for authmode optional, but non-zero return from the
3166 * callback means a fatal error so it shouldn't be ignored */
Manuel Pégourié-Gonnard31458a12017-06-26 10:11:49 +02003167 if( ret == MBEDTLS_ERR_X509_CERT_VERIFY_FAILED )
3168 ret = MBEDTLS_ERR_X509_FATAL_ERROR;
3169
Manuel Pégourié-Gonnardd15795a2017-06-22 12:19:27 +02003170 if( ret != 0 )
3171 {
3172 *flags = (uint32_t) -1;
3173 return( ret );
3174 }
3175
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003176 if( *flags != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003177 return( MBEDTLS_ERR_X509_CERT_VERIFY_FAILED );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003178
3179 return( 0 );
3180}
3181
Hanno Becker3116fb32019-03-28 13:34:42 +00003182
3183/*
3184 * Verify the certificate validity (default profile, not restartable)
3185 */
3186int mbedtls_x509_crt_verify( mbedtls_x509_crt *crt,
3187 mbedtls_x509_crt *trust_ca,
3188 mbedtls_x509_crl *ca_crl,
3189 const char *cn, uint32_t *flags,
3190 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
3191 void *p_vrfy )
3192{
Jarno Lamsa2ee67a62019-04-01 14:59:33 +03003193 return( x509_crt_verify_restartable_ca_cb( crt, trust_ca, ca_crl,
Hanno Becker3116fb32019-03-28 13:34:42 +00003194 NULL, NULL,
3195 &mbedtls_x509_crt_profile_default,
3196 cn, flags,
3197 f_vrfy, p_vrfy, NULL ) );
3198}
3199
3200/*
3201 * Verify the certificate validity (user-chosen profile, not restartable)
3202 */
3203int mbedtls_x509_crt_verify_with_profile( mbedtls_x509_crt *crt,
3204 mbedtls_x509_crt *trust_ca,
3205 mbedtls_x509_crl *ca_crl,
3206 const mbedtls_x509_crt_profile *profile,
3207 const char *cn, uint32_t *flags,
3208 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
3209 void *p_vrfy )
3210{
Jarno Lamsa2ee67a62019-04-01 14:59:33 +03003211 return( x509_crt_verify_restartable_ca_cb( crt, trust_ca, ca_crl,
Hanno Becker3116fb32019-03-28 13:34:42 +00003212 NULL, NULL,
3213 profile, cn, flags,
3214 f_vrfy, p_vrfy, NULL ) );
3215}
3216
3217#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
3218/*
3219 * Verify the certificate validity (user-chosen profile, CA callback,
3220 * not restartable).
3221 */
Jarno Lamsa31d9db62019-04-01 14:33:49 +03003222int mbedtls_x509_crt_verify_with_ca_cb( mbedtls_x509_crt *crt,
Hanno Becker3116fb32019-03-28 13:34:42 +00003223 mbedtls_x509_crt_ca_cb_t f_ca_cb,
3224 void *p_ca_cb,
3225 const mbedtls_x509_crt_profile *profile,
3226 const char *cn, uint32_t *flags,
3227 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
3228 void *p_vrfy )
3229{
Jarno Lamsa2ee67a62019-04-01 14:59:33 +03003230 return( x509_crt_verify_restartable_ca_cb( crt, NULL, NULL,
Hanno Becker3116fb32019-03-28 13:34:42 +00003231 f_ca_cb, p_ca_cb,
3232 profile, cn, flags,
3233 f_vrfy, p_vrfy, NULL ) );
3234}
3235#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
3236
3237int mbedtls_x509_crt_verify_restartable( mbedtls_x509_crt *crt,
3238 mbedtls_x509_crt *trust_ca,
3239 mbedtls_x509_crl *ca_crl,
3240 const mbedtls_x509_crt_profile *profile,
3241 const char *cn, uint32_t *flags,
3242 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
3243 void *p_vrfy,
3244 mbedtls_x509_crt_restart_ctx *rs_ctx )
3245{
Jarno Lamsa2ee67a62019-04-01 14:59:33 +03003246 return( x509_crt_verify_restartable_ca_cb( crt, trust_ca, ca_crl,
Hanno Becker3116fb32019-03-28 13:34:42 +00003247 NULL, NULL,
3248 profile, cn, flags,
3249 f_vrfy, p_vrfy, rs_ctx ) );
3250}
3251
3252
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003253/*
Paul Bakker369d2eb2013-09-18 11:58:25 +02003254 * Initialize a certificate chain
3255 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003256void mbedtls_x509_crt_init( mbedtls_x509_crt *crt )
Paul Bakker369d2eb2013-09-18 11:58:25 +02003257{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003258 memset( crt, 0, sizeof(mbedtls_x509_crt) );
Paul Bakker369d2eb2013-09-18 11:58:25 +02003259}
3260
3261/*
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003262 * Unallocate all certificate data
3263 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003264void mbedtls_x509_crt_free( mbedtls_x509_crt *crt )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003265{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003266 mbedtls_x509_crt *cert_cur = crt;
3267 mbedtls_x509_crt *cert_prv;
3268 mbedtls_x509_name *name_cur;
3269 mbedtls_x509_name *name_prv;
3270 mbedtls_x509_sequence *seq_cur;
3271 mbedtls_x509_sequence *seq_prv;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003272
3273 if( crt == NULL )
3274 return;
3275
3276 do
3277 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003278 mbedtls_pk_free( &cert_cur->pk );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003279
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003280#if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT)
3281 mbedtls_free( cert_cur->sig_opts );
Manuel Pégourié-Gonnardf75f2f72014-06-05 15:14:28 +02003282#endif
3283
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003284 name_cur = cert_cur->issuer.next;
3285 while( name_cur != NULL )
3286 {
3287 name_prv = name_cur;
3288 name_cur = name_cur->next;
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05003289 mbedtls_platform_zeroize( name_prv, sizeof( mbedtls_x509_name ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003290 mbedtls_free( name_prv );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003291 }
3292
3293 name_cur = cert_cur->subject.next;
3294 while( name_cur != NULL )
3295 {
3296 name_prv = name_cur;
3297 name_cur = name_cur->next;
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05003298 mbedtls_platform_zeroize( name_prv, sizeof( mbedtls_x509_name ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003299 mbedtls_free( name_prv );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003300 }
3301
3302 seq_cur = cert_cur->ext_key_usage.next;
3303 while( seq_cur != NULL )
3304 {
3305 seq_prv = seq_cur;
3306 seq_cur = seq_cur->next;
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05003307 mbedtls_platform_zeroize( seq_prv,
3308 sizeof( mbedtls_x509_sequence ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003309 mbedtls_free( seq_prv );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003310 }
3311
3312 seq_cur = cert_cur->subject_alt_names.next;
3313 while( seq_cur != NULL )
3314 {
3315 seq_prv = seq_cur;
3316 seq_cur = seq_cur->next;
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05003317 mbedtls_platform_zeroize( seq_prv,
3318 sizeof( mbedtls_x509_sequence ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003319 mbedtls_free( seq_prv );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003320 }
3321
Ron Eldor74d9acc2019-03-21 14:00:03 +02003322 seq_cur = cert_cur->certificate_policies.next;
3323 while( seq_cur != NULL )
3324 {
3325 seq_prv = seq_cur;
3326 seq_cur = seq_cur->next;
3327 mbedtls_platform_zeroize( seq_prv,
3328 sizeof( mbedtls_x509_sequence ) );
3329 mbedtls_free( seq_prv );
3330 }
3331
Hanno Becker1a65dcd2019-01-31 08:57:44 +00003332 if( cert_cur->raw.p != NULL && cert_cur->own_buffer )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003333 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05003334 mbedtls_platform_zeroize( cert_cur->raw.p, cert_cur->raw.len );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003335 mbedtls_free( cert_cur->raw.p );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003336 }
3337
3338 cert_cur = cert_cur->next;
3339 }
3340 while( cert_cur != NULL );
3341
3342 cert_cur = crt;
3343 do
3344 {
3345 cert_prv = cert_cur;
3346 cert_cur = cert_cur->next;
3347
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05003348 mbedtls_platform_zeroize( cert_prv, sizeof( mbedtls_x509_crt ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003349 if( cert_prv != crt )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003350 mbedtls_free( cert_prv );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003351 }
3352 while( cert_cur != NULL );
3353}
3354
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +02003355#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
3356/*
3357 * Initialize a restart context
3358 */
3359void mbedtls_x509_crt_restart_init( mbedtls_x509_crt_restart_ctx *ctx )
3360{
Manuel Pégourié-Gonnard15d7df22017-08-17 14:33:31 +02003361 mbedtls_pk_restart_init( &ctx->pk );
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02003362
3363 ctx->parent = NULL;
3364 ctx->fallback_parent = NULL;
Manuel Pégourié-Gonnard78d7e8c2018-07-02 12:33:14 +02003365 ctx->fallback_signature_is_good = 0;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02003366
3367 ctx->parent_is_trusted = -1;
3368
Manuel Pégourié-Gonnarddaf04912017-08-23 12:32:19 +02003369 ctx->in_progress = x509_crt_rs_none;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02003370 ctx->self_cnt = 0;
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +02003371 x509_crt_verify_chain_reset( &ctx->ver_chain );
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +02003372}
3373
3374/*
3375 * Free the components of a restart context
3376 */
3377void mbedtls_x509_crt_restart_free( mbedtls_x509_crt_restart_ctx *ctx )
3378{
3379 if( ctx == NULL )
3380 return;
3381
Manuel Pégourié-Gonnard15d7df22017-08-17 14:33:31 +02003382 mbedtls_pk_restart_free( &ctx->pk );
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02003383 mbedtls_x509_crt_restart_init( ctx );
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +02003384}
3385#endif /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */
3386
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003387#endif /* MBEDTLS_X509_CRT_PARSE_C */