blob: b4fe8863a683751502c4fef52267208e4188d3bb [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 *
Bence Szépkúti1e148272020-08-07 13:07:28 +02004 * Copyright The Mbed TLS Contributors
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 */
19/*
20 * The ITU-T X.509 standard defines a certificate format for PKI.
21 *
Manuel Pégourié-Gonnard1c082f32014-06-12 22:34:55 +020022 * http://www.ietf.org/rfc/rfc5280.txt (Certificates and CRLs)
23 * http://www.ietf.org/rfc/rfc3279.txt (Alg IDs for CRLs)
24 * http://www.ietf.org/rfc/rfc2986.txt (CSRs, aka PKCS#10)
Paul Bakker7c6b2c32013-09-16 13:49:26 +020025 *
26 * http://www.itu.int/ITU-T/studygroups/com17/languages/X.680-0207.pdf
27 * http://www.itu.int/ITU-T/studygroups/com17/languages/X.690-0207.pdf
Manuel Pégourié-Gonnard2f09d592017-07-03 18:30:43 +020028 *
29 * [SIRO] https://cabforum.org/wp-content/uploads/Chunghwatelecom201503cabforumV4.pdf
Paul Bakker7c6b2c32013-09-16 13:49:26 +020030 */
31
Gilles Peskinedb09ef62020-06-03 01:43:33 +020032#include "common.h"
Paul Bakker7c6b2c32013-09-16 13:49:26 +020033
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020034#if defined(MBEDTLS_X509_CRT_PARSE_C)
Paul Bakker7c6b2c32013-09-16 13:49:26 +020035
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000036#include "mbedtls/x509_crt.h"
Janos Follath73c616b2019-12-18 15:07:04 +000037#include "mbedtls/error.h"
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000038#include "mbedtls/oid.h"
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050039#include "mbedtls/platform_util.h"
Rich Evans00ab4702015-02-06 13:43:58 +000040
Rich Evans00ab4702015-02-06 13:43:58 +000041#include <string.h>
42
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020043#if defined(MBEDTLS_PEM_PARSE_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000044#include "mbedtls/pem.h"
Paul Bakker7c6b2c32013-09-16 13:49:26 +020045#endif
46
Andrzej Kurekd4a65532018-10-31 06:18:39 -040047#if defined(MBEDTLS_USE_PSA_CRYPTO)
48#include "psa/crypto.h"
49#include "mbedtls/psa_util.h"
50#endif
51
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020052#if defined(MBEDTLS_PLATFORM_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000053#include "mbedtls/platform.h"
Paul Bakker7c6b2c32013-09-16 13:49:26 +020054#else
Simon Butcherd2642582018-10-03 15:11:19 +010055#include <stdio.h>
Rich Evans00ab4702015-02-06 13:43:58 +000056#include <stdlib.h>
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020057#define mbedtls_free free
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +020058#define mbedtls_calloc calloc
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020059#define mbedtls_snprintf snprintf
Paul Bakker7c6b2c32013-09-16 13:49:26 +020060#endif
61
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020062#if defined(MBEDTLS_THREADING_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000063#include "mbedtls/threading.h"
Manuel Pégourié-Gonnard5ad68e42013-11-28 17:11:54 +010064#endif
65
Paul Bakkerfa6a6202013-10-28 18:48:30 +010066#if defined(_WIN32) && !defined(EFIX64) && !defined(EFI32)
Paul Bakker7c6b2c32013-09-16 13:49:26 +020067#include <windows.h>
68#else
69#include <time.h>
70#endif
71
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020072#if defined(MBEDTLS_FS_IO)
Rich Evans00ab4702015-02-06 13:43:58 +000073#include <stdio.h>
Paul Bakker5ff3f912014-04-04 15:08:20 +020074#if !defined(_WIN32) || defined(EFIX64) || defined(EFI32)
Paul Bakker7c6b2c32013-09-16 13:49:26 +020075#include <sys/types.h>
76#include <sys/stat.h>
Martino Facchin0ec05ec2021-05-04 11:47:36 +020077#if defined(__MBED__)
78#include <platform/mbed_retarget.h>
79#else
Paul Bakker7c6b2c32013-09-16 13:49:26 +020080#include <dirent.h>
Martino Facchin0ec05ec2021-05-04 11:47:36 +020081#endif /* __MBED__ */
Rich Evans00ab4702015-02-06 13:43:58 +000082#endif /* !_WIN32 || EFIX64 || EFI32 */
Paul Bakker7c6b2c32013-09-16 13:49:26 +020083#endif
84
Manuel Pégourié-Gonnardc547d1a2017-07-05 13:28:45 +020085/*
86 * Item in a verification chain: cert and flags for it
87 */
88typedef struct {
89 mbedtls_x509_crt *crt;
90 uint32_t flags;
91} x509_crt_verify_chain_item;
92
93/*
94 * Max size of verification chain: end-entity + intermediates + trusted root
95 */
96#define X509_MAX_VERIFY_CHAIN_SIZE ( MBEDTLS_X509_MAX_INTERMEDIATE_CA + 2 )
Paul Bakker34617722014-06-13 17:20:13 +020097
Paul Bakker7c6b2c32013-09-16 13:49:26 +020098/*
Manuel Pégourié-Gonnard88db5da2015-06-15 14:34:59 +020099 * Default profile
100 */
Manuel Pégourié-Gonnard88db5da2015-06-15 14:34:59 +0200101const mbedtls_x509_crt_profile mbedtls_x509_crt_profile_default =
102{
Gilles Peskine5e79cb32017-05-04 16:17:21 +0200103 /* Only SHA-2 hashes */
Manuel Pégourié-Gonnardf8ea8562015-06-15 15:33:19 +0200104 MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_SHA224 ) |
105 MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_SHA256 ) |
106 MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_SHA384 ) |
107 MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_SHA512 ),
108 0xFFFFFFF, /* Any PK alg */
109 0xFFFFFFF, /* Any curve */
Manuel Pégourié-Gonnard88db5da2015-06-15 14:34:59 +0200110 2048,
111};
112
113/*
114 * Next-default profile
115 */
Manuel Pégourié-Gonnard88db5da2015-06-15 14:34:59 +0200116const mbedtls_x509_crt_profile mbedtls_x509_crt_profile_next =
117{
Manuel Pégourié-Gonnardf8ea8562015-06-15 15:33:19 +0200118 /* Hashes from SHA-256 and above */
119 MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_SHA256 ) |
120 MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_SHA384 ) |
121 MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_SHA512 ),
122 0xFFFFFFF, /* Any PK alg */
123#if defined(MBEDTLS_ECP_C)
124 /* Curves at or above 128-bit security level */
125 MBEDTLS_X509_ID_FLAG( MBEDTLS_ECP_DP_SECP256R1 ) |
126 MBEDTLS_X509_ID_FLAG( MBEDTLS_ECP_DP_SECP384R1 ) |
127 MBEDTLS_X509_ID_FLAG( MBEDTLS_ECP_DP_SECP521R1 ) |
128 MBEDTLS_X509_ID_FLAG( MBEDTLS_ECP_DP_BP256R1 ) |
129 MBEDTLS_X509_ID_FLAG( MBEDTLS_ECP_DP_BP384R1 ) |
130 MBEDTLS_X509_ID_FLAG( MBEDTLS_ECP_DP_BP512R1 ) |
131 MBEDTLS_X509_ID_FLAG( MBEDTLS_ECP_DP_SECP256K1 ),
132#else
133 0,
134#endif
Manuel Pégourié-Gonnard88db5da2015-06-15 14:34:59 +0200135 2048,
136};
137
138/*
139 * NSA Suite B Profile
140 */
Manuel Pégourié-Gonnard88db5da2015-06-15 14:34:59 +0200141const mbedtls_x509_crt_profile mbedtls_x509_crt_profile_suiteb =
142{
Manuel Pégourié-Gonnardf8ea8562015-06-15 15:33:19 +0200143 /* Only SHA-256 and 384 */
144 MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_SHA256 ) |
145 MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_SHA384 ),
146 /* Only ECDSA */
Ron Eldor85e1dcf2018-02-06 15:59:38 +0200147 MBEDTLS_X509_ID_FLAG( MBEDTLS_PK_ECDSA ) |
148 MBEDTLS_X509_ID_FLAG( MBEDTLS_PK_ECKEY ),
Manuel Pégourié-Gonnardf8ea8562015-06-15 15:33:19 +0200149#if defined(MBEDTLS_ECP_C)
150 /* Only NIST P-256 and P-384 */
151 MBEDTLS_X509_ID_FLAG( MBEDTLS_ECP_DP_SECP256R1 ) |
152 MBEDTLS_X509_ID_FLAG( MBEDTLS_ECP_DP_SECP384R1 ),
153#else
154 0,
155#endif
156 0,
Manuel Pégourié-Gonnard88db5da2015-06-15 14:34:59 +0200157};
158
159/*
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200160 * Check md_alg against profile
Manuel Pégourié-Gonnard3f816912017-10-26 10:24:16 +0200161 * Return 0 if md_alg is acceptable for this profile, -1 otherwise
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200162 */
163static int x509_profile_check_md_alg( const mbedtls_x509_crt_profile *profile,
164 mbedtls_md_type_t md_alg )
165{
Philippe Antoineb5b25432018-05-11 11:06:29 +0200166 if( md_alg == MBEDTLS_MD_NONE )
167 return( -1 );
168
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200169 if( ( profile->allowed_mds & MBEDTLS_X509_ID_FLAG( md_alg ) ) != 0 )
170 return( 0 );
171
172 return( -1 );
173}
174
175/*
176 * Check pk_alg against profile
Manuel Pégourié-Gonnard3f816912017-10-26 10:24:16 +0200177 * Return 0 if pk_alg is acceptable for this profile, -1 otherwise
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200178 */
179static int x509_profile_check_pk_alg( const mbedtls_x509_crt_profile *profile,
180 mbedtls_pk_type_t pk_alg )
181{
Philippe Antoineb5b25432018-05-11 11:06:29 +0200182 if( pk_alg == MBEDTLS_PK_NONE )
183 return( -1 );
184
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200185 if( ( profile->allowed_pks & MBEDTLS_X509_ID_FLAG( pk_alg ) ) != 0 )
186 return( 0 );
187
188 return( -1 );
189}
190
191/*
192 * Check key against profile
Manuel Pégourié-Gonnard3f816912017-10-26 10:24:16 +0200193 * Return 0 if pk is acceptable for this profile, -1 otherwise
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200194 */
195static int x509_profile_check_key( const mbedtls_x509_crt_profile *profile,
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200196 const mbedtls_pk_context *pk )
197{
Manuel Pégourié-Gonnard3f816912017-10-26 10:24:16 +0200198 const mbedtls_pk_type_t pk_alg = mbedtls_pk_get_type( pk );
Manuel Pégourié-Gonnard19773ff2017-10-24 10:51:26 +0200199
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200200#if defined(MBEDTLS_RSA_C)
201 if( pk_alg == MBEDTLS_PK_RSA || pk_alg == MBEDTLS_PK_RSASSA_PSS )
202 {
Manuel Pégourié-Gonnard097c7bb2015-06-18 16:43:38 +0200203 if( mbedtls_pk_get_bitlen( pk ) >= profile->rsa_min_bitlen )
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200204 return( 0 );
205
206 return( -1 );
207 }
208#endif
209
Manuel Pégourié-Gonnard65eefc82015-10-23 14:08:48 +0200210#if defined(MBEDTLS_ECP_C)
211 if( pk_alg == MBEDTLS_PK_ECDSA ||
212 pk_alg == MBEDTLS_PK_ECKEY ||
213 pk_alg == MBEDTLS_PK_ECKEY_DH )
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200214 {
Manuel Pégourié-Gonnard3f816912017-10-26 10:24:16 +0200215 const mbedtls_ecp_group_id gid = mbedtls_pk_ec( *pk )->grp.id;
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200216
Philippe Antoineb5b25432018-05-11 11:06:29 +0200217 if( gid == MBEDTLS_ECP_DP_NONE )
218 return( -1 );
219
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200220 if( ( profile->allowed_curves & MBEDTLS_X509_ID_FLAG( gid ) ) != 0 )
221 return( 0 );
222
223 return( -1 );
224 }
225#endif
226
227 return( -1 );
228}
229
230/*
Hanno Becker1f8527f2018-11-02 09:19:16 +0000231 * Like memcmp, but case-insensitive and always returns -1 if different
232 */
233static int x509_memcasecmp( const void *s1, const void *s2, size_t len )
234{
235 size_t i;
236 unsigned char diff;
237 const unsigned char *n1 = s1, *n2 = s2;
238
239 for( i = 0; i < len; i++ )
240 {
241 diff = n1[i] ^ n2[i];
242
243 if( diff == 0 )
244 continue;
245
246 if( diff == 32 &&
247 ( ( n1[i] >= 'a' && n1[i] <= 'z' ) ||
248 ( n1[i] >= 'A' && n1[i] <= 'Z' ) ) )
249 {
250 continue;
251 }
252
253 return( -1 );
254 }
255
256 return( 0 );
257}
258
259/*
260 * Return 0 if name matches wildcard, -1 otherwise
261 */
262static int x509_check_wildcard( const char *cn, const mbedtls_x509_buf *name )
263{
264 size_t i;
265 size_t cn_idx = 0, cn_len = strlen( cn );
266
267 /* We can't have a match if there is no wildcard to match */
268 if( name->len < 3 || name->p[0] != '*' || name->p[1] != '.' )
269 return( -1 );
270
271 for( i = 0; i < cn_len; ++i )
272 {
273 if( cn[i] == '.' )
274 {
275 cn_idx = i;
276 break;
277 }
278 }
279
280 if( cn_idx == 0 )
281 return( -1 );
282
283 if( cn_len - cn_idx == name->len - 1 &&
284 x509_memcasecmp( name->p + 1, cn + cn_idx, name->len - 1 ) == 0 )
285 {
286 return( 0 );
287 }
288
289 return( -1 );
290}
291
292/*
293 * Compare two X.509 strings, case-insensitive, and allowing for some encoding
294 * variations (but not all).
295 *
296 * Return 0 if equal, -1 otherwise.
297 */
298static int x509_string_cmp( const mbedtls_x509_buf *a, const mbedtls_x509_buf *b )
299{
300 if( a->tag == b->tag &&
301 a->len == b->len &&
302 memcmp( a->p, b->p, b->len ) == 0 )
303 {
304 return( 0 );
305 }
306
307 if( ( a->tag == MBEDTLS_ASN1_UTF8_STRING || a->tag == MBEDTLS_ASN1_PRINTABLE_STRING ) &&
308 ( b->tag == MBEDTLS_ASN1_UTF8_STRING || b->tag == MBEDTLS_ASN1_PRINTABLE_STRING ) &&
309 a->len == b->len &&
310 x509_memcasecmp( a->p, b->p, b->len ) == 0 )
311 {
312 return( 0 );
313 }
314
315 return( -1 );
316}
317
318/*
319 * Compare two X.509 Names (aka rdnSequence).
320 *
321 * See RFC 5280 section 7.1, though we don't implement the whole algorithm:
322 * we sometimes return unequal when the full algorithm would return equal,
323 * but never the other way. (In particular, we don't do Unicode normalisation
324 * or space folding.)
325 *
326 * Return 0 if equal, -1 otherwise.
327 */
328static int x509_name_cmp( const mbedtls_x509_name *a, const mbedtls_x509_name *b )
329{
330 /* Avoid recursion, it might not be optimised by the compiler */
331 while( a != NULL || b != NULL )
332 {
333 if( a == NULL || b == NULL )
334 return( -1 );
335
336 /* type */
337 if( a->oid.tag != b->oid.tag ||
338 a->oid.len != b->oid.len ||
339 memcmp( a->oid.p, b->oid.p, b->oid.len ) != 0 )
340 {
341 return( -1 );
342 }
343
344 /* value */
345 if( x509_string_cmp( &a->val, &b->val ) != 0 )
346 return( -1 );
347
348 /* structure of the list of sets */
349 if( a->next_merged != b->next_merged )
350 return( -1 );
351
352 a = a->next;
353 b = b->next;
354 }
355
356 /* a == NULL == b */
357 return( 0 );
358}
359
360/*
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +0200361 * Reset (init or clear) a verify_chain
362 */
363static void x509_crt_verify_chain_reset(
364 mbedtls_x509_crt_verify_chain *ver_chain )
365{
366 size_t i;
367
368 for( i = 0; i < MBEDTLS_X509_MAX_VERIFY_CHAIN_SIZE; i++ )
369 {
370 ver_chain->items[i].crt = NULL;
Hanno Beckera9375b32019-01-10 09:19:26 +0000371 ver_chain->items[i].flags = (uint32_t) -1;
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +0200372 }
373
374 ver_chain->len = 0;
Hanno Beckerf53893b2019-03-28 13:45:55 +0000375
376#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
377 ver_chain->trust_ca_cb_result = NULL;
378#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +0200379}
380
381/*
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200382 * Version ::= INTEGER { v1(0), v2(1), v3(2) }
383 */
384static int x509_get_version( unsigned char **p,
385 const unsigned char *end,
386 int *ver )
387{
Janos Follath865b3eb2019-12-16 11:46:15 +0000388 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200389 size_t len;
390
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200391 if( ( ret = mbedtls_asn1_get_tag( p, end, &len,
392 MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_CONSTRUCTED | 0 ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200393 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200394 if( ret == MBEDTLS_ERR_ASN1_UNEXPECTED_TAG )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200395 {
396 *ver = 0;
397 return( 0 );
398 }
399
Chris Jones9f7a6932021-04-14 12:12:09 +0100400 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_FORMAT, ret ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200401 }
402
403 end = *p + len;
404
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200405 if( ( ret = mbedtls_asn1_get_int( p, end, ver ) ) != 0 )
Chris Jones9f7a6932021-04-14 12:12:09 +0100406 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_VERSION, ret ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200407
408 if( *p != end )
Chris Jones9f7a6932021-04-14 12:12:09 +0100409 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_VERSION,
410 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200411
412 return( 0 );
413}
414
415/*
416 * Validity ::= SEQUENCE {
417 * notBefore Time,
418 * notAfter Time }
419 */
420static int x509_get_dates( unsigned char **p,
421 const unsigned char *end,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200422 mbedtls_x509_time *from,
423 mbedtls_x509_time *to )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200424{
Janos Follath865b3eb2019-12-16 11:46:15 +0000425 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200426 size_t len;
427
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200428 if( ( ret = mbedtls_asn1_get_tag( p, end, &len,
429 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
Chris Jones9f7a6932021-04-14 12:12:09 +0100430 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_DATE, ret ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200431
432 end = *p + len;
433
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200434 if( ( ret = mbedtls_x509_get_time( p, end, from ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200435 return( ret );
436
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200437 if( ( ret = mbedtls_x509_get_time( p, end, to ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200438 return( ret );
439
440 if( *p != end )
Chris Jones9f7a6932021-04-14 12:12:09 +0100441 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_DATE,
442 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200443
444 return( 0 );
445}
446
447/*
448 * X.509 v2/v3 unique identifier (not parsed)
449 */
450static int x509_get_uid( unsigned char **p,
451 const unsigned char *end,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200452 mbedtls_x509_buf *uid, int n )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200453{
Janos Follath865b3eb2019-12-16 11:46:15 +0000454 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200455
456 if( *p == end )
457 return( 0 );
458
459 uid->tag = **p;
460
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200461 if( ( ret = mbedtls_asn1_get_tag( p, end, &uid->len,
462 MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_CONSTRUCTED | n ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200463 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200464 if( ret == MBEDTLS_ERR_ASN1_UNEXPECTED_TAG )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200465 return( 0 );
466
Chris Jones9f7a6932021-04-14 12:12:09 +0100467 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_FORMAT, ret ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200468 }
469
470 uid->p = *p;
471 *p += uid->len;
472
473 return( 0 );
474}
475
476static int x509_get_basic_constraints( unsigned char **p,
477 const unsigned char *end,
478 int *ca_istrue,
479 int *max_pathlen )
480{
Janos Follath865b3eb2019-12-16 11:46:15 +0000481 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200482 size_t len;
483
484 /*
485 * BasicConstraints ::= SEQUENCE {
486 * cA BOOLEAN DEFAULT FALSE,
487 * pathLenConstraint INTEGER (0..MAX) OPTIONAL }
488 */
489 *ca_istrue = 0; /* DEFAULT FALSE */
490 *max_pathlen = 0; /* endless */
491
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200492 if( ( ret = mbedtls_asn1_get_tag( p, end, &len,
493 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
Chris Jones9f7a6932021-04-14 12:12:09 +0100494 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200495
496 if( *p == end )
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200497 return( 0 );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200498
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200499 if( ( ret = mbedtls_asn1_get_bool( p, end, ca_istrue ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200500 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200501 if( ret == MBEDTLS_ERR_ASN1_UNEXPECTED_TAG )
502 ret = mbedtls_asn1_get_int( p, end, ca_istrue );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200503
504 if( ret != 0 )
Chris Jones9f7a6932021-04-14 12:12:09 +0100505 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200506
507 if( *ca_istrue != 0 )
508 *ca_istrue = 1;
509 }
510
511 if( *p == end )
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200512 return( 0 );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200513
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200514 if( ( ret = mbedtls_asn1_get_int( p, end, max_pathlen ) ) != 0 )
Chris Jones9f7a6932021-04-14 12:12:09 +0100515 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200516
517 if( *p != end )
Chris Jones9f7a6932021-04-14 12:12:09 +0100518 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
519 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200520
Andrzej Kurek16050742020-04-14 09:49:52 -0400521 /* Do not accept max_pathlen equal to INT_MAX to avoid a signed integer
522 * overflow, which is an undefined behavior. */
523 if( *max_pathlen == INT_MAX )
Chris Jones9f7a6932021-04-14 12:12:09 +0100524 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
525 MBEDTLS_ERR_ASN1_INVALID_LENGTH ) );
Andrzej Kurek16050742020-04-14 09:49:52 -0400526
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200527 (*max_pathlen)++;
528
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200529 return( 0 );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200530}
531
532static int x509_get_ns_cert_type( unsigned char **p,
533 const unsigned char *end,
534 unsigned char *ns_cert_type)
535{
Janos Follath865b3eb2019-12-16 11:46:15 +0000536 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200537 mbedtls_x509_bitstring bs = { 0, 0, NULL };
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200538
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200539 if( ( ret = mbedtls_asn1_get_bitstring( p, end, &bs ) ) != 0 )
Chris Jones9f7a6932021-04-14 12:12:09 +0100540 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200541
542 if( bs.len != 1 )
Chris Jones9f7a6932021-04-14 12:12:09 +0100543 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
544 MBEDTLS_ERR_ASN1_INVALID_LENGTH ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200545
546 /* Get actual bitstring */
547 *ns_cert_type = *bs.p;
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200548 return( 0 );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200549}
550
551static int x509_get_key_usage( unsigned char **p,
552 const unsigned char *end,
Manuel Pégourié-Gonnard1d0ca1a2015-03-27 16:50:00 +0100553 unsigned int *key_usage)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200554{
Janos Follath865b3eb2019-12-16 11:46:15 +0000555 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard9a702252015-06-23 10:14:36 +0200556 size_t i;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200557 mbedtls_x509_bitstring bs = { 0, 0, NULL };
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200558
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200559 if( ( ret = mbedtls_asn1_get_bitstring( p, end, &bs ) ) != 0 )
Chris Jones9f7a6932021-04-14 12:12:09 +0100560 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200561
562 if( bs.len < 1 )
Chris Jones9f7a6932021-04-14 12:12:09 +0100563 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
564 MBEDTLS_ERR_ASN1_INVALID_LENGTH ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200565
566 /* Get actual bitstring */
Manuel Pégourié-Gonnard9a702252015-06-23 10:14:36 +0200567 *key_usage = 0;
568 for( i = 0; i < bs.len && i < sizeof( unsigned int ); i++ )
569 {
570 *key_usage |= (unsigned int) bs.p[i] << (8*i);
571 }
572
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200573 return( 0 );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200574}
575
576/*
577 * ExtKeyUsageSyntax ::= SEQUENCE SIZE (1..MAX) OF KeyPurposeId
578 *
579 * KeyPurposeId ::= OBJECT IDENTIFIER
580 */
581static int x509_get_ext_key_usage( unsigned char **p,
582 const unsigned char *end,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200583 mbedtls_x509_sequence *ext_key_usage)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200584{
Janos Follath865b3eb2019-12-16 11:46:15 +0000585 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200586
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200587 if( ( ret = mbedtls_asn1_get_sequence_of( p, end, ext_key_usage, MBEDTLS_ASN1_OID ) ) != 0 )
Chris Jones9f7a6932021-04-14 12:12:09 +0100588 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200589
590 /* Sequence length must be >= 1 */
591 if( ext_key_usage->buf.p == NULL )
Chris Jones9f7a6932021-04-14 12:12:09 +0100592 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
593 MBEDTLS_ERR_ASN1_INVALID_LENGTH ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200594
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200595 return( 0 );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200596}
597
598/*
599 * SubjectAltName ::= GeneralNames
600 *
601 * GeneralNames ::= SEQUENCE SIZE (1..MAX) OF GeneralName
602 *
603 * GeneralName ::= CHOICE {
604 * otherName [0] OtherName,
605 * rfc822Name [1] IA5String,
606 * dNSName [2] IA5String,
607 * x400Address [3] ORAddress,
608 * directoryName [4] Name,
609 * ediPartyName [5] EDIPartyName,
610 * uniformResourceIdentifier [6] IA5String,
611 * iPAddress [7] OCTET STRING,
612 * registeredID [8] OBJECT IDENTIFIER }
613 *
614 * OtherName ::= SEQUENCE {
615 * type-id OBJECT IDENTIFIER,
616 * value [0] EXPLICIT ANY DEFINED BY type-id }
617 *
618 * EDIPartyName ::= SEQUENCE {
619 * nameAssigner [0] DirectoryString OPTIONAL,
620 * partyName [1] DirectoryString }
621 *
Ron Eldorc8b5f3f2019-05-15 15:15:55 +0300622 * NOTE: we list all types, but only use dNSName and otherName
623 * of type HwModuleName, as defined in RFC 4108, at this point.
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200624 */
625static int x509_get_subject_alt_name( unsigned char **p,
626 const unsigned char *end,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200627 mbedtls_x509_sequence *subject_alt_name )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200628{
Janos Follath865b3eb2019-12-16 11:46:15 +0000629 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200630 size_t len, tag_len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200631 mbedtls_asn1_buf *buf;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200632 unsigned char tag;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200633 mbedtls_asn1_sequence *cur = subject_alt_name;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200634
635 /* Get main sequence tag */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200636 if( ( ret = mbedtls_asn1_get_tag( p, end, &len,
637 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
Chris Jones9f7a6932021-04-14 12:12:09 +0100638 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200639
640 if( *p + len != end )
Chris Jones9f7a6932021-04-14 12:12:09 +0100641 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
642 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200643
644 while( *p < end )
645 {
Ron Eldordbbd9662019-05-15 15:46:03 +0300646 mbedtls_x509_subject_alternative_name dummy_san_buf;
647 memset( &dummy_san_buf, 0, sizeof( dummy_san_buf ) );
648
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200649 tag = **p;
650 (*p)++;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200651 if( ( ret = mbedtls_asn1_get_len( p, end, &tag_len ) ) != 0 )
Chris Jones9f7a6932021-04-14 12:12:09 +0100652 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200653
Andres Amaya Garcia849bc652017-08-25 17:13:12 +0100654 if( ( tag & MBEDTLS_ASN1_TAG_CLASS_MASK ) !=
655 MBEDTLS_ASN1_CONTEXT_SPECIFIC )
656 {
Chris Jones9f7a6932021-04-14 12:12:09 +0100657 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
658 MBEDTLS_ERR_ASN1_UNEXPECTED_TAG ) );
Andres Amaya Garcia849bc652017-08-25 17:13:12 +0100659 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200660
Ron Eldordbbd9662019-05-15 15:46:03 +0300661 /*
irwir74aee1c2019-09-21 18:21:48 +0300662 * Check that the SAN is structured correctly.
Ron Eldordbbd9662019-05-15 15:46:03 +0300663 */
664 ret = mbedtls_x509_parse_subject_alt_name( &(cur->buf), &dummy_san_buf );
665 /*
666 * In case the extension is malformed, return an error,
667 * and clear the allocated sequences.
668 */
669 if( ret != 0 && ret != MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE )
670 {
671 mbedtls_x509_sequence *seq_cur = subject_alt_name->next;
672 mbedtls_x509_sequence *seq_prv;
673 while( seq_cur != NULL )
674 {
675 seq_prv = seq_cur;
676 seq_cur = seq_cur->next;
677 mbedtls_platform_zeroize( seq_prv,
678 sizeof( mbedtls_x509_sequence ) );
679 mbedtls_free( seq_prv );
680 }
Ron Eldor5aebeeb2019-05-22 16:41:21 +0300681 subject_alt_name->next = NULL;
Ron Eldordbbd9662019-05-15 15:46:03 +0300682 return( ret );
683 }
684
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200685 /* Allocate and assign next pointer */
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +0200686 if( cur->buf.p != NULL )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200687 {
Manuel Pégourié-Gonnardb1340602014-11-11 23:11:16 +0100688 if( cur->next != NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200689 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS );
Manuel Pégourié-Gonnardb1340602014-11-11 23:11:16 +0100690
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200691 cur->next = mbedtls_calloc( 1, sizeof( mbedtls_asn1_sequence ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200692
693 if( cur->next == NULL )
Chris Jones9f7a6932021-04-14 12:12:09 +0100694 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
695 MBEDTLS_ERR_ASN1_ALLOC_FAILED ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200696
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200697 cur = cur->next;
698 }
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +0200699
700 buf = &(cur->buf);
701 buf->tag = tag;
702 buf->p = *p;
703 buf->len = tag_len;
704 *p += buf->len;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200705 }
706
707 /* Set final sequence entry's next pointer to NULL */
708 cur->next = NULL;
709
710 if( *p != end )
Chris Jones9f7a6932021-04-14 12:12:09 +0100711 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
712 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200713
714 return( 0 );
715}
716
717/*
Ron Eldor74d9acc2019-03-21 14:00:03 +0200718 * id-ce-certificatePolicies OBJECT IDENTIFIER ::= { id-ce 32 }
719 *
720 * anyPolicy OBJECT IDENTIFIER ::= { id-ce-certificatePolicies 0 }
721 *
722 * certificatePolicies ::= SEQUENCE SIZE (1..MAX) OF PolicyInformation
723 *
724 * PolicyInformation ::= SEQUENCE {
725 * policyIdentifier CertPolicyId,
726 * policyQualifiers SEQUENCE SIZE (1..MAX) OF
727 * PolicyQualifierInfo OPTIONAL }
728 *
729 * CertPolicyId ::= OBJECT IDENTIFIER
730 *
731 * PolicyQualifierInfo ::= SEQUENCE {
732 * policyQualifierId PolicyQualifierId,
733 * qualifier ANY DEFINED BY policyQualifierId }
734 *
735 * -- policyQualifierIds for Internet policy qualifiers
736 *
737 * id-qt OBJECT IDENTIFIER ::= { id-pkix 2 }
738 * id-qt-cps OBJECT IDENTIFIER ::= { id-qt 1 }
739 * id-qt-unotice OBJECT IDENTIFIER ::= { id-qt 2 }
740 *
741 * PolicyQualifierId ::= OBJECT IDENTIFIER ( id-qt-cps | id-qt-unotice )
742 *
743 * Qualifier ::= CHOICE {
744 * cPSuri CPSuri,
745 * userNotice UserNotice }
746 *
747 * CPSuri ::= IA5String
748 *
749 * UserNotice ::= SEQUENCE {
750 * noticeRef NoticeReference OPTIONAL,
751 * explicitText DisplayText OPTIONAL }
752 *
753 * NoticeReference ::= SEQUENCE {
754 * organization DisplayText,
755 * noticeNumbers SEQUENCE OF INTEGER }
756 *
757 * DisplayText ::= CHOICE {
758 * ia5String IA5String (SIZE (1..200)),
759 * visibleString VisibleString (SIZE (1..200)),
760 * bmpString BMPString (SIZE (1..200)),
761 * utf8String UTF8String (SIZE (1..200)) }
762 *
763 * NOTE: we only parse and use anyPolicy without qualifiers at this point
764 * as defined in RFC 5280.
765 */
766static int x509_get_certificate_policies( unsigned char **p,
767 const unsigned char *end,
768 mbedtls_x509_sequence *certificate_policies )
769{
Ron Eldor8b0c3c92019-05-15 12:20:00 +0300770 int ret, parse_ret = 0;
Ron Eldor74d9acc2019-03-21 14:00:03 +0200771 size_t len;
772 mbedtls_asn1_buf *buf;
773 mbedtls_asn1_sequence *cur = certificate_policies;
774
775 /* Get main sequence tag */
776 ret = mbedtls_asn1_get_tag( p, end, &len,
777 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE );
778 if( ret != 0 )
Chris Jones9f7a6932021-04-14 12:12:09 +0100779 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret ) );
Ron Eldor74d9acc2019-03-21 14:00:03 +0200780
781 if( *p + len != end )
Chris Jones9f7a6932021-04-14 12:12:09 +0100782 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
783 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH ) );
Ron Eldor74d9acc2019-03-21 14:00:03 +0200784
785 /*
786 * Cannot be an empty sequence.
787 */
788 if( len == 0 )
Chris Jones9f7a6932021-04-14 12:12:09 +0100789 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
790 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH ) );
Ron Eldor74d9acc2019-03-21 14:00:03 +0200791
792 while( *p < end )
793 {
794 mbedtls_x509_buf policy_oid;
795 const unsigned char *policy_end;
796
797 /*
798 * Get the policy sequence
799 */
800 if( ( ret = mbedtls_asn1_get_tag( p, end, &len,
801 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
Chris Jones9f7a6932021-04-14 12:12:09 +0100802 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret ) );
Ron Eldor74d9acc2019-03-21 14:00:03 +0200803
804 policy_end = *p + len;
805
Ron Eldor08063792019-05-13 16:38:39 +0300806 if( ( ret = mbedtls_asn1_get_tag( p, policy_end, &len,
Ron Eldor74d9acc2019-03-21 14:00:03 +0200807 MBEDTLS_ASN1_OID ) ) != 0 )
Chris Jones9f7a6932021-04-14 12:12:09 +0100808 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret ) );
Ron Eldor74d9acc2019-03-21 14:00:03 +0200809
810 policy_oid.tag = MBEDTLS_ASN1_OID;
811 policy_oid.len = len;
812 policy_oid.p = *p;
813
Ron Eldor8b0c3c92019-05-15 12:20:00 +0300814 /*
815 * Only AnyPolicy is currently supported when enforcing policy.
816 */
817 if( MBEDTLS_OID_CMP( MBEDTLS_OID_ANY_POLICY, &policy_oid ) != 0 )
818 {
819 /*
820 * Set the parsing return code but continue parsing, in case this
821 * extension is critical and MBEDTLS_X509_ALLOW_UNSUPPORTED_CRITICAL_EXTENSION
822 * is configured.
823 */
824 parse_ret = MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE;
825 }
826
Ron Eldor74d9acc2019-03-21 14:00:03 +0200827 /* Allocate and assign next pointer */
828 if( cur->buf.p != NULL )
829 {
830 if( cur->next != NULL )
831 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS );
832
833 cur->next = mbedtls_calloc( 1, sizeof( mbedtls_asn1_sequence ) );
834
835 if( cur->next == NULL )
Chris Jones9f7a6932021-04-14 12:12:09 +0100836 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
837 MBEDTLS_ERR_ASN1_ALLOC_FAILED ) );
Ron Eldor74d9acc2019-03-21 14:00:03 +0200838
839 cur = cur->next;
840 }
841
842 buf = &( cur->buf );
843 buf->tag = policy_oid.tag;
844 buf->p = policy_oid.p;
845 buf->len = policy_oid.len;
Ron Eldor08063792019-05-13 16:38:39 +0300846
847 *p += len;
848
849 /*
850 * If there is an optional qualifier, then *p < policy_end
851 * Check the Qualifier len to verify it doesn't exceed policy_end.
852 */
853 if( *p < policy_end )
854 {
855 if( ( ret = mbedtls_asn1_get_tag( p, policy_end, &len,
856 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
Chris Jones9f7a6932021-04-14 12:12:09 +0100857 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret ) );
Ron Eldor08063792019-05-13 16:38:39 +0300858 /*
859 * Skip the optional policy qualifiers.
860 */
861 *p += len;
862 }
863
864 if( *p != policy_end )
Chris Jones9f7a6932021-04-14 12:12:09 +0100865 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
866 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH ) );
Ron Eldor74d9acc2019-03-21 14:00:03 +0200867 }
868
869 /* Set final sequence entry's next pointer to NULL */
870 cur->next = NULL;
871
872 if( *p != end )
Chris Jones9f7a6932021-04-14 12:12:09 +0100873 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
874 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH ) );
Ron Eldor74d9acc2019-03-21 14:00:03 +0200875
Ron Eldor8b0c3c92019-05-15 12:20:00 +0300876 return( parse_ret );
Ron Eldor74d9acc2019-03-21 14:00:03 +0200877}
878
879/*
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200880 * X.509 v3 extensions
881 *
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200882 */
883static int x509_get_crt_ext( unsigned char **p,
884 const unsigned char *end,
Nicola Di Lieto502d4b42020-04-25 14:41:25 +0200885 mbedtls_x509_crt *crt,
Nicola Di Lieto5659e7e2020-05-28 23:41:38 +0200886 mbedtls_x509_crt_ext_cb_t cb,
887 void *p_ctx )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200888{
Janos Follath865b3eb2019-12-16 11:46:15 +0000889 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200890 size_t len;
Nicola Di Lietoc84b1e62020-06-13 11:08:16 +0200891 unsigned char *end_ext_data, *start_ext_octet, *end_ext_octet;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200892
Hanno Becker12f62fb2019-02-12 17:22:36 +0000893 if( *p == end )
894 return( 0 );
895
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200896 if( ( ret = mbedtls_x509_get_ext( p, end, &crt->v3_ext, 3 ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200897 return( ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200898
Hanno Becker12f62fb2019-02-12 17:22:36 +0000899 end = crt->v3_ext.p + crt->v3_ext.len;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200900 while( *p < end )
901 {
902 /*
903 * Extension ::= SEQUENCE {
904 * extnID OBJECT IDENTIFIER,
905 * critical BOOLEAN DEFAULT FALSE,
906 * extnValue OCTET STRING }
907 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200908 mbedtls_x509_buf extn_oid = {0, 0, NULL};
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200909 int is_critical = 0; /* DEFAULT FALSE */
910 int ext_type = 0;
911
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200912 if( ( ret = mbedtls_asn1_get_tag( p, end, &len,
913 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
Chris Jones9f7a6932021-04-14 12:12:09 +0100914 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200915
916 end_ext_data = *p + len;
917
918 /* Get extension ID */
k-stachowiakdcae78a2018-06-28 16:32:54 +0200919 if( ( ret = mbedtls_asn1_get_tag( p, end_ext_data, &extn_oid.len,
k-stachowiak463928a2018-07-24 12:50:59 +0200920 MBEDTLS_ASN1_OID ) ) != 0 )
Chris Jones9f7a6932021-04-14 12:12:09 +0100921 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200922
k-stachowiak463928a2018-07-24 12:50:59 +0200923 extn_oid.tag = MBEDTLS_ASN1_OID;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200924 extn_oid.p = *p;
925 *p += extn_oid.len;
926
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200927 /* Get optional critical */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200928 if( ( ret = mbedtls_asn1_get_bool( p, end_ext_data, &is_critical ) ) != 0 &&
929 ( ret != MBEDTLS_ERR_ASN1_UNEXPECTED_TAG ) )
Chris Jones9f7a6932021-04-14 12:12:09 +0100930 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200931
932 /* Data should be octet string type */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200933 if( ( ret = mbedtls_asn1_get_tag( p, end_ext_data, &len,
934 MBEDTLS_ASN1_OCTET_STRING ) ) != 0 )
Chris Jones9f7a6932021-04-14 12:12:09 +0100935 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200936
Nicola Di Lietoc84b1e62020-06-13 11:08:16 +0200937 start_ext_octet = *p;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200938 end_ext_octet = *p + len;
939
940 if( end_ext_octet != end_ext_data )
Chris Jones9f7a6932021-04-14 12:12:09 +0100941 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
942 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200943
944 /*
945 * Detect supported extensions
946 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200947 ret = mbedtls_oid_get_x509_ext_type( &extn_oid, &ext_type );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200948
949 if( ret != 0 )
950 {
Nicola Di Lieto502d4b42020-04-25 14:41:25 +0200951 /* Give the callback (if any) a chance to handle the extension */
Nicola Di Lieto2c3a9172020-05-28 17:20:42 +0200952 if( cb != NULL )
953 {
Nicola Di Lieto5659e7e2020-05-28 23:41:38 +0200954 ret = cb( p_ctx, crt, &extn_oid, is_critical, *p, end_ext_octet );
Nicola Di Lieto565b52b2020-05-29 22:46:56 +0200955 if( ret != 0 && is_critical )
956 return( ret );
Nicola Di Lietofae25a12020-05-28 08:55:08 +0200957 *p = end_ext_octet;
Nicola Di Lieto502d4b42020-04-25 14:41:25 +0200958 continue;
Nicola Di Lietofae25a12020-05-28 08:55:08 +0200959 }
Nicola Di Lieto502d4b42020-04-25 14:41:25 +0200960
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200961 /* No parser found, skip extension */
962 *p = end_ext_octet;
963
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200964#if !defined(MBEDTLS_X509_ALLOW_UNSUPPORTED_CRITICAL_EXTENSION)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200965 if( is_critical )
966 {
967 /* Data is marked as critical: fail */
Chris Jones9f7a6932021-04-14 12:12:09 +0100968 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
969 MBEDTLS_ERR_ASN1_UNEXPECTED_TAG ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200970 }
971#endif
972 continue;
973 }
974
Manuel Pégourié-Gonnard8a5e3d42014-11-12 17:47:28 +0100975 /* Forbid repeated extensions */
976 if( ( crt->ext_types & ext_type ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200977 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS );
Manuel Pégourié-Gonnard8a5e3d42014-11-12 17:47:28 +0100978
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200979 crt->ext_types |= ext_type;
980
981 switch( ext_type )
982 {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +0100983 case MBEDTLS_X509_EXT_BASIC_CONSTRAINTS:
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200984 /* Parse basic constraints */
985 if( ( ret = x509_get_basic_constraints( p, end_ext_octet,
986 &crt->ca_istrue, &crt->max_pathlen ) ) != 0 )
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200987 return( ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200988 break;
989
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200990 case MBEDTLS_X509_EXT_KEY_USAGE:
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200991 /* Parse key usage */
992 if( ( ret = x509_get_key_usage( p, end_ext_octet,
993 &crt->key_usage ) ) != 0 )
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200994 return( ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200995 break;
996
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200997 case MBEDTLS_X509_EXT_EXTENDED_KEY_USAGE:
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200998 /* Parse extended key usage */
999 if( ( ret = x509_get_ext_key_usage( p, end_ext_octet,
1000 &crt->ext_key_usage ) ) != 0 )
Paul Bakkerd8bb8262014-06-17 14:06:49 +02001001 return( ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001002 break;
1003
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01001004 case MBEDTLS_X509_EXT_SUBJECT_ALT_NAME:
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001005 /* Parse subject alt name */
1006 if( ( ret = x509_get_subject_alt_name( p, end_ext_octet,
1007 &crt->subject_alt_names ) ) != 0 )
Paul Bakkerd8bb8262014-06-17 14:06:49 +02001008 return( ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001009 break;
1010
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001011 case MBEDTLS_X509_EXT_NS_CERT_TYPE:
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001012 /* Parse netscape certificate type */
1013 if( ( ret = x509_get_ns_cert_type( p, end_ext_octet,
1014 &crt->ns_cert_type ) ) != 0 )
Paul Bakkerd8bb8262014-06-17 14:06:49 +02001015 return( ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001016 break;
1017
Ron Eldor74d9acc2019-03-21 14:00:03 +02001018 case MBEDTLS_OID_X509_EXT_CERTIFICATE_POLICIES:
1019 /* Parse certificate policies type */
1020 if( ( ret = x509_get_certificate_policies( p, end_ext_octet,
1021 &crt->certificate_policies ) ) != 0 )
Ron Eldor8b0c3c92019-05-15 12:20:00 +03001022 {
Nicola Di Lietoc84b1e62020-06-13 11:08:16 +02001023 /* Give the callback (if any) a chance to handle the extension
1024 * if it contains unsupported policies */
1025 if( ret == MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE && cb != NULL &&
1026 cb( p_ctx, crt, &extn_oid, is_critical,
1027 start_ext_octet, end_ext_octet ) == 0 )
1028 break;
1029
Ron Eldor8b0c3c92019-05-15 12:20:00 +03001030#if !defined(MBEDTLS_X509_ALLOW_UNSUPPORTED_CRITICAL_EXTENSION)
1031 if( is_critical )
1032 return( ret );
1033 else
1034#endif
1035 /*
Ron Eldora2913912019-05-16 16:17:38 +03001036 * If MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE is returned, then we
1037 * cannot interpret or enforce the policy. However, it is up to
1038 * the user to choose how to enforce the policies,
Ron Eldor8b0c3c92019-05-15 12:20:00 +03001039 * unless the extension is critical.
1040 */
1041 if( ret != MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE )
1042 return( ret );
1043 }
Ron Eldor74d9acc2019-03-21 14:00:03 +02001044 break;
1045
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001046 default:
Ron Eldordf48efa2019-04-08 13:28:24 +03001047 /*
1048 * If this is a non-critical extension, which the oid layer
1049 * supports, but there isn't an x509 parser for it,
1050 * skip the extension.
1051 */
1052#if !defined(MBEDTLS_X509_ALLOW_UNSUPPORTED_CRITICAL_EXTENSION)
1053 if( is_critical )
1054 return( MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE );
1055 else
1056#endif
1057 *p = end_ext_octet;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001058 }
1059 }
1060
1061 if( *p != end )
Chris Jones9f7a6932021-04-14 12:12:09 +01001062 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
1063 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001064
1065 return( 0 );
1066}
1067
1068/*
1069 * Parse and fill a single X.509 certificate in DER format
1070 */
Hanno Becker1a65dcd2019-01-31 08:57:44 +00001071static int x509_crt_parse_der_core( mbedtls_x509_crt *crt,
1072 const unsigned char *buf,
1073 size_t buflen,
Nicola Di Lieto502d4b42020-04-25 14:41:25 +02001074 int make_copy,
Nicola Di Lieto5659e7e2020-05-28 23:41:38 +02001075 mbedtls_x509_crt_ext_cb_t cb,
1076 void *p_ctx )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001077{
Janos Follath865b3eb2019-12-16 11:46:15 +00001078 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001079 size_t len;
1080 unsigned char *p, *end, *crt_end;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001081 mbedtls_x509_buf sig_params1, sig_params2, sig_oid2;
Manuel Pégourié-Gonnard59a75d52014-01-22 10:12:57 +01001082
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001083 memset( &sig_params1, 0, sizeof( mbedtls_x509_buf ) );
1084 memset( &sig_params2, 0, sizeof( mbedtls_x509_buf ) );
1085 memset( &sig_oid2, 0, sizeof( mbedtls_x509_buf ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001086
1087 /*
1088 * Check for valid input
1089 */
1090 if( crt == NULL || buf == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001091 return( MBEDTLS_ERR_X509_BAD_INPUT_DATA );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001092
Hanno Becker1a65dcd2019-01-31 08:57:44 +00001093 /* Use the original buffer until we figure out actual length. */
Janos Follathcc0e49d2016-02-17 14:34:12 +00001094 p = (unsigned char*) buf;
1095 len = buflen;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001096 end = p + len;
1097
1098 /*
1099 * Certificate ::= SEQUENCE {
1100 * tbsCertificate TBSCertificate,
1101 * signatureAlgorithm AlgorithmIdentifier,
1102 * signatureValue BIT STRING }
1103 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001104 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
1105 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001106 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001107 mbedtls_x509_crt_free( crt );
1108 return( MBEDTLS_ERR_X509_INVALID_FORMAT );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001109 }
1110
Janos Follathcc0e49d2016-02-17 14:34:12 +00001111 end = crt_end = p + len;
Hanno Becker1a65dcd2019-01-31 08:57:44 +00001112 crt->raw.len = crt_end - buf;
1113 if( make_copy != 0 )
1114 {
1115 /* Create and populate a new buffer for the raw field. */
1116 crt->raw.p = p = mbedtls_calloc( 1, crt->raw.len );
1117 if( crt->raw.p == NULL )
1118 return( MBEDTLS_ERR_X509_ALLOC_FAILED );
1119
1120 memcpy( crt->raw.p, buf, crt->raw.len );
1121 crt->own_buffer = 1;
1122
1123 p += crt->raw.len - len;
1124 end = crt_end = p + len;
1125 }
1126 else
1127 {
1128 crt->raw.p = (unsigned char*) buf;
1129 crt->own_buffer = 0;
1130 }
Janos Follathcc0e49d2016-02-17 14:34:12 +00001131
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001132 /*
1133 * TBSCertificate ::= SEQUENCE {
1134 */
1135 crt->tbs.p = p;
1136
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001137 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
1138 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001139 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001140 mbedtls_x509_crt_free( crt );
Chris Jones9f7a6932021-04-14 12:12:09 +01001141 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_FORMAT, ret ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001142 }
1143
1144 end = p + len;
1145 crt->tbs.len = end - crt->tbs.p;
1146
1147 /*
1148 * Version ::= INTEGER { v1(0), v2(1), v3(2) }
1149 *
1150 * CertificateSerialNumber ::= INTEGER
1151 *
1152 * signature AlgorithmIdentifier
1153 */
1154 if( ( ret = x509_get_version( &p, end, &crt->version ) ) != 0 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001155 ( ret = mbedtls_x509_get_serial( &p, end, &crt->serial ) ) != 0 ||
1156 ( ret = mbedtls_x509_get_alg( &p, end, &crt->sig_oid,
Manuel Pégourié-Gonnarddddbb1d2014-06-05 17:02:24 +02001157 &sig_params1 ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001158 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001159 mbedtls_x509_crt_free( crt );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001160 return( ret );
1161 }
1162
Andres AG7ca4a032017-03-09 16:16:11 +00001163 if( crt->version < 0 || crt->version > 2 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001164 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001165 mbedtls_x509_crt_free( crt );
1166 return( MBEDTLS_ERR_X509_UNKNOWN_VERSION );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001167 }
1168
Andres AG7ca4a032017-03-09 16:16:11 +00001169 crt->version++;
1170
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001171 if( ( ret = mbedtls_x509_get_sig_alg( &crt->sig_oid, &sig_params1,
Manuel Pégourié-Gonnardf75f2f72014-06-05 15:14:28 +02001172 &crt->sig_md, &crt->sig_pk,
1173 &crt->sig_opts ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001174 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001175 mbedtls_x509_crt_free( crt );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001176 return( ret );
1177 }
1178
1179 /*
1180 * issuer Name
1181 */
1182 crt->issuer_raw.p = p;
1183
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001184 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
1185 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001186 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001187 mbedtls_x509_crt_free( crt );
Chris Jones9f7a6932021-04-14 12:12:09 +01001188 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_FORMAT, ret ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001189 }
1190
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001191 if( ( ret = mbedtls_x509_get_name( &p, p + len, &crt->issuer ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001192 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001193 mbedtls_x509_crt_free( crt );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001194 return( ret );
1195 }
1196
1197 crt->issuer_raw.len = p - crt->issuer_raw.p;
1198
1199 /*
1200 * Validity ::= SEQUENCE {
1201 * notBefore Time,
1202 * notAfter Time }
1203 *
1204 */
1205 if( ( ret = x509_get_dates( &p, end, &crt->valid_from,
1206 &crt->valid_to ) ) != 0 )
1207 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001208 mbedtls_x509_crt_free( crt );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001209 return( ret );
1210 }
1211
1212 /*
1213 * subject Name
1214 */
1215 crt->subject_raw.p = p;
1216
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001217 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
1218 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001219 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001220 mbedtls_x509_crt_free( crt );
Chris Jones9f7a6932021-04-14 12:12:09 +01001221 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_FORMAT, ret ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001222 }
1223
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001224 if( len && ( ret = mbedtls_x509_get_name( &p, p + len, &crt->subject ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001225 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001226 mbedtls_x509_crt_free( crt );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001227 return( ret );
1228 }
1229
1230 crt->subject_raw.len = p - crt->subject_raw.p;
1231
1232 /*
1233 * SubjectPublicKeyInfo
1234 */
Hanno Becker494dd7a2019-02-06 16:13:41 +00001235 crt->pk_raw.p = p;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001236 if( ( ret = mbedtls_pk_parse_subpubkey( &p, end, &crt->pk ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001237 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001238 mbedtls_x509_crt_free( crt );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001239 return( ret );
1240 }
Hanno Becker494dd7a2019-02-06 16:13:41 +00001241 crt->pk_raw.len = p - crt->pk_raw.p;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001242
1243 /*
1244 * issuerUniqueID [1] IMPLICIT UniqueIdentifier OPTIONAL,
1245 * -- If present, version shall be v2 or v3
1246 * subjectUniqueID [2] IMPLICIT UniqueIdentifier OPTIONAL,
1247 * -- If present, version shall be v2 or v3
1248 * extensions [3] EXPLICIT Extensions OPTIONAL
1249 * -- If present, version shall be v3
1250 */
1251 if( crt->version == 2 || crt->version == 3 )
1252 {
1253 ret = x509_get_uid( &p, end, &crt->issuer_id, 1 );
1254 if( ret != 0 )
1255 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001256 mbedtls_x509_crt_free( crt );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001257 return( ret );
1258 }
1259 }
1260
1261 if( crt->version == 2 || crt->version == 3 )
1262 {
1263 ret = x509_get_uid( &p, end, &crt->subject_id, 2 );
1264 if( ret != 0 )
1265 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001266 mbedtls_x509_crt_free( crt );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001267 return( ret );
1268 }
1269 }
1270
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001271#if !defined(MBEDTLS_X509_ALLOW_EXTENSIONS_NON_V3)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001272 if( crt->version == 3 )
Paul Bakkerc27c4e22013-09-23 15:01:36 +02001273#endif
Manuel Pégourié-Gonnarda252af72015-03-27 16:15:55 +01001274 {
Nicola Di Lieto5659e7e2020-05-28 23:41:38 +02001275 ret = x509_get_crt_ext( &p, end, crt, cb, p_ctx );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001276 if( ret != 0 )
1277 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001278 mbedtls_x509_crt_free( crt );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001279 return( ret );
1280 }
1281 }
1282
1283 if( p != end )
1284 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001285 mbedtls_x509_crt_free( crt );
Chris Jones9f7a6932021-04-14 12:12:09 +01001286 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_FORMAT,
1287 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001288 }
1289
1290 end = crt_end;
1291
1292 /*
1293 * }
1294 * -- end of TBSCertificate
1295 *
1296 * signatureAlgorithm AlgorithmIdentifier,
1297 * signatureValue BIT STRING
1298 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001299 if( ( ret = mbedtls_x509_get_alg( &p, end, &sig_oid2, &sig_params2 ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001300 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001301 mbedtls_x509_crt_free( crt );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001302 return( ret );
1303 }
1304
Manuel Pégourié-Gonnard1022fed2015-03-27 16:30:47 +01001305 if( crt->sig_oid.len != sig_oid2.len ||
1306 memcmp( crt->sig_oid.p, sig_oid2.p, crt->sig_oid.len ) != 0 ||
Paul Elliottca17ebf2020-11-24 17:30:18 +00001307 sig_params1.tag != sig_params2.tag ||
Manuel Pégourié-Gonnarddddbb1d2014-06-05 17:02:24 +02001308 sig_params1.len != sig_params2.len ||
Manuel Pégourié-Gonnard159c5242015-04-30 11:15:22 +02001309 ( sig_params1.len != 0 &&
1310 memcmp( sig_params1.p, sig_params2.p, sig_params1.len ) != 0 ) )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001311 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001312 mbedtls_x509_crt_free( crt );
1313 return( MBEDTLS_ERR_X509_SIG_MISMATCH );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001314 }
1315
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001316 if( ( ret = mbedtls_x509_get_sig( &p, end, &crt->sig ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001317 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001318 mbedtls_x509_crt_free( crt );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001319 return( ret );
1320 }
1321
1322 if( p != end )
1323 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001324 mbedtls_x509_crt_free( crt );
Chris Jones9f7a6932021-04-14 12:12:09 +01001325 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_FORMAT,
1326 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001327 }
1328
1329 return( 0 );
1330}
1331
1332/*
1333 * Parse one X.509 certificate in DER format from a buffer and add them to a
1334 * chained list
1335 */
Hanno Becker1a65dcd2019-01-31 08:57:44 +00001336static int mbedtls_x509_crt_parse_der_internal( mbedtls_x509_crt *chain,
1337 const unsigned char *buf,
1338 size_t buflen,
Nicola Di Lieto502d4b42020-04-25 14:41:25 +02001339 int make_copy,
Nicola Di Lieto5659e7e2020-05-28 23:41:38 +02001340 mbedtls_x509_crt_ext_cb_t cb,
1341 void *p_ctx )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001342{
Janos Follath865b3eb2019-12-16 11:46:15 +00001343 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001344 mbedtls_x509_crt *crt = chain, *prev = NULL;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001345
1346 /*
1347 * Check for valid input
1348 */
1349 if( crt == NULL || buf == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001350 return( MBEDTLS_ERR_X509_BAD_INPUT_DATA );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001351
1352 while( crt->version != 0 && crt->next != NULL )
1353 {
1354 prev = crt;
1355 crt = crt->next;
1356 }
1357
1358 /*
1359 * Add new certificate on the end of the chain if needed.
1360 */
Paul Bakker66d5d072014-06-17 16:39:18 +02001361 if( crt->version != 0 && crt->next == NULL )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001362 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02001363 crt->next = mbedtls_calloc( 1, sizeof( mbedtls_x509_crt ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001364
1365 if( crt->next == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02001366 return( MBEDTLS_ERR_X509_ALLOC_FAILED );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001367
1368 prev = crt;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001369 mbedtls_x509_crt_init( crt->next );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001370 crt = crt->next;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001371 }
1372
Nicola Di Lieto5659e7e2020-05-28 23:41:38 +02001373 ret = x509_crt_parse_der_core( crt, buf, buflen, make_copy, cb, p_ctx );
Nicola Di Lieto502d4b42020-04-25 14:41:25 +02001374 if( ret != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001375 {
1376 if( prev )
1377 prev->next = NULL;
1378
1379 if( crt != chain )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001380 mbedtls_free( crt );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001381
1382 return( ret );
1383 }
1384
1385 return( 0 );
1386}
1387
Hanno Becker1a65dcd2019-01-31 08:57:44 +00001388int mbedtls_x509_crt_parse_der_nocopy( mbedtls_x509_crt *chain,
1389 const unsigned char *buf,
1390 size_t buflen )
1391{
Nicola Di Lieto5659e7e2020-05-28 23:41:38 +02001392 return( mbedtls_x509_crt_parse_der_internal( chain, buf, buflen, 0, NULL, NULL ) );
Nicola Di Lieto502d4b42020-04-25 14:41:25 +02001393}
1394
Nicola Di Lietofde98f72020-05-28 08:34:33 +02001395int mbedtls_x509_crt_parse_der_with_ext_cb( mbedtls_x509_crt *chain,
1396 const unsigned char *buf,
1397 size_t buflen,
Nicola Di Lieto4dbe5672020-05-28 09:18:42 +02001398 int make_copy,
Nicola Di Lieto5659e7e2020-05-28 23:41:38 +02001399 mbedtls_x509_crt_ext_cb_t cb,
1400 void *p_ctx )
Nicola Di Lieto502d4b42020-04-25 14:41:25 +02001401{
Nicola Di Lieto5659e7e2020-05-28 23:41:38 +02001402 return( mbedtls_x509_crt_parse_der_internal( chain, buf, buflen, make_copy, cb, p_ctx ) );
Hanno Becker1a65dcd2019-01-31 08:57:44 +00001403}
1404
1405int mbedtls_x509_crt_parse_der( mbedtls_x509_crt *chain,
1406 const unsigned char *buf,
1407 size_t buflen )
1408{
Nicola Di Lieto5659e7e2020-05-28 23:41:38 +02001409 return( mbedtls_x509_crt_parse_der_internal( chain, buf, buflen, 1, NULL, NULL ) );
Hanno Becker1a65dcd2019-01-31 08:57:44 +00001410}
1411
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001412/*
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001413 * Parse one or more PEM certificates from a buffer and add them to the chained
1414 * list
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001415 */
Hanno Becker1a65dcd2019-01-31 08:57:44 +00001416int mbedtls_x509_crt_parse( mbedtls_x509_crt *chain,
1417 const unsigned char *buf,
1418 size_t buflen )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001419{
Janos Follath98e28a72016-05-31 14:03:54 +01001420#if defined(MBEDTLS_PEM_PARSE_C)
Andres AGc0db5112016-12-07 15:05:53 +00001421 int success = 0, first_error = 0, total_failed = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001422 int buf_format = MBEDTLS_X509_FORMAT_DER;
Janos Follath98e28a72016-05-31 14:03:54 +01001423#endif
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001424
1425 /*
1426 * Check for valid input
1427 */
1428 if( chain == NULL || buf == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001429 return( MBEDTLS_ERR_X509_BAD_INPUT_DATA );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001430
1431 /*
1432 * Determine buffer content. Buffer contains either one DER certificate or
1433 * one or more PEM certificates.
1434 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001435#if defined(MBEDTLS_PEM_PARSE_C)
Manuel Pégourié-Gonnard0ece0f92015-05-12 12:43:54 +02001436 if( buflen != 0 && buf[buflen - 1] == '\0' &&
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001437 strstr( (const char *) buf, "-----BEGIN CERTIFICATE-----" ) != NULL )
1438 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001439 buf_format = MBEDTLS_X509_FORMAT_PEM;
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001440 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001441
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001442 if( buf_format == MBEDTLS_X509_FORMAT_DER )
1443 return mbedtls_x509_crt_parse_der( chain, buf, buflen );
Janos Follath98e28a72016-05-31 14:03:54 +01001444#else
1445 return mbedtls_x509_crt_parse_der( chain, buf, buflen );
1446#endif
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001447
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001448#if defined(MBEDTLS_PEM_PARSE_C)
1449 if( buf_format == MBEDTLS_X509_FORMAT_PEM )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001450 {
Janos Follath865b3eb2019-12-16 11:46:15 +00001451 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001452 mbedtls_pem_context pem;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001453
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001454 /* 1 rather than 0 since the terminating NULL byte is counted in */
1455 while( buflen > 1 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001456 {
1457 size_t use_len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001458 mbedtls_pem_init( &pem );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001459
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001460 /* If we get there, we know the string is null-terminated */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001461 ret = mbedtls_pem_read_buffer( &pem,
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001462 "-----BEGIN CERTIFICATE-----",
1463 "-----END CERTIFICATE-----",
1464 buf, NULL, 0, &use_len );
1465
1466 if( ret == 0 )
1467 {
1468 /*
1469 * Was PEM encoded
1470 */
1471 buflen -= use_len;
1472 buf += use_len;
1473 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001474 else if( ret == MBEDTLS_ERR_PEM_BAD_INPUT_DATA )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001475 {
1476 return( ret );
1477 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001478 else if( ret != MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001479 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001480 mbedtls_pem_free( &pem );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001481
1482 /*
1483 * PEM header and footer were found
1484 */
1485 buflen -= use_len;
1486 buf += use_len;
1487
1488 if( first_error == 0 )
1489 first_error = ret;
1490
Paul Bakker5a5fa922014-09-26 14:53:04 +02001491 total_failed++;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001492 continue;
1493 }
1494 else
1495 break;
1496
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001497 ret = mbedtls_x509_crt_parse_der( chain, pem.buf, pem.buflen );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001498
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001499 mbedtls_pem_free( &pem );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001500
1501 if( ret != 0 )
1502 {
1503 /*
1504 * Quit parsing on a memory error
1505 */
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02001506 if( ret == MBEDTLS_ERR_X509_ALLOC_FAILED )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001507 return( ret );
1508
1509 if( first_error == 0 )
1510 first_error = ret;
1511
1512 total_failed++;
1513 continue;
1514 }
1515
1516 success = 1;
1517 }
1518 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001519
1520 if( success )
1521 return( total_failed );
1522 else if( first_error )
1523 return( first_error );
1524 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001525 return( MBEDTLS_ERR_X509_CERT_UNKNOWN_FORMAT );
Janos Follath98e28a72016-05-31 14:03:54 +01001526#endif /* MBEDTLS_PEM_PARSE_C */
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001527}
1528
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001529#if defined(MBEDTLS_FS_IO)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001530/*
1531 * Load one or more certificates and add them to the chained list
1532 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001533int mbedtls_x509_crt_parse_file( mbedtls_x509_crt *chain, const char *path )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001534{
Janos Follath865b3eb2019-12-16 11:46:15 +00001535 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001536 size_t n;
1537 unsigned char *buf;
1538
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001539 if( ( ret = mbedtls_pk_load_file( path, &buf, &n ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001540 return( ret );
1541
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001542 ret = mbedtls_x509_crt_parse( chain, buf, n );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001543
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05001544 mbedtls_platform_zeroize( buf, n );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001545 mbedtls_free( buf );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001546
1547 return( ret );
1548}
1549
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001550int mbedtls_x509_crt_parse_path( mbedtls_x509_crt *chain, const char *path )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001551{
1552 int ret = 0;
Paul Bakkerfa6a6202013-10-28 18:48:30 +01001553#if defined(_WIN32) && !defined(EFIX64) && !defined(EFI32)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001554 int w_ret;
1555 WCHAR szDir[MAX_PATH];
1556 char filename[MAX_PATH];
Paul Bakker9af723c2014-05-01 13:03:14 +02001557 char *p;
Manuel Pégourié-Gonnard261faed2015-10-21 10:16:29 +02001558 size_t len = strlen( path );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001559
Paul Bakker9af723c2014-05-01 13:03:14 +02001560 WIN32_FIND_DATAW file_data;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001561 HANDLE hFind;
1562
1563 if( len > MAX_PATH - 3 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001564 return( MBEDTLS_ERR_X509_BAD_INPUT_DATA );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001565
Paul Bakker9af723c2014-05-01 13:03:14 +02001566 memset( szDir, 0, sizeof(szDir) );
1567 memset( filename, 0, MAX_PATH );
1568 memcpy( filename, path, len );
1569 filename[len++] = '\\';
1570 p = filename + len;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001571 filename[len++] = '*';
1572
Simon B3c6b18d2016-11-03 01:11:37 +00001573 w_ret = MultiByteToWideChar( CP_ACP, 0, filename, (int)len, szDir,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001574 MAX_PATH - 3 );
Manuel Pégourié-Gonnardacdb9b92015-01-23 17:50:34 +00001575 if( w_ret == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001576 return( MBEDTLS_ERR_X509_BAD_INPUT_DATA );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001577
1578 hFind = FindFirstFileW( szDir, &file_data );
Paul Bakker66d5d072014-06-17 16:39:18 +02001579 if( hFind == INVALID_HANDLE_VALUE )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001580 return( MBEDTLS_ERR_X509_FILE_IO_ERROR );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001581
1582 len = MAX_PATH - len;
1583 do
1584 {
Paul Bakker9af723c2014-05-01 13:03:14 +02001585 memset( p, 0, len );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001586
1587 if( file_data.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY )
1588 continue;
1589
Paul Bakker9af723c2014-05-01 13:03:14 +02001590 w_ret = WideCharToMultiByte( CP_ACP, 0, file_data.cFileName,
Paul Bakker66d5d072014-06-17 16:39:18 +02001591 lstrlenW( file_data.cFileName ),
Manuel Pégourié-Gonnard261faed2015-10-21 10:16:29 +02001592 p, (int) len - 1,
Paul Bakker9af723c2014-05-01 13:03:14 +02001593 NULL, NULL );
Manuel Pégourié-Gonnardacdb9b92015-01-23 17:50:34 +00001594 if( w_ret == 0 )
Ron Eldor36d90422017-01-09 15:09:16 +02001595 {
1596 ret = MBEDTLS_ERR_X509_FILE_IO_ERROR;
1597 goto cleanup;
1598 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001599
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001600 w_ret = mbedtls_x509_crt_parse_file( chain, filename );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001601 if( w_ret < 0 )
1602 ret++;
1603 else
1604 ret += w_ret;
1605 }
1606 while( FindNextFileW( hFind, &file_data ) != 0 );
1607
Paul Bakker66d5d072014-06-17 16:39:18 +02001608 if( GetLastError() != ERROR_NO_MORE_FILES )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001609 ret = MBEDTLS_ERR_X509_FILE_IO_ERROR;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001610
Ron Eldor36d90422017-01-09 15:09:16 +02001611cleanup:
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001612 FindClose( hFind );
Paul Bakkerbe089b02013-10-14 15:51:50 +02001613#else /* _WIN32 */
Manuel Pégourié-Gonnard964bf9b2013-11-26 16:47:11 +01001614 int t_ret;
Andres AGf9113192016-09-02 14:06:04 +01001615 int snp_ret;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001616 struct stat sb;
Manuel Pégourié-Gonnard964bf9b2013-11-26 16:47:11 +01001617 struct dirent *entry;
Andres AGf9113192016-09-02 14:06:04 +01001618 char entry_name[MBEDTLS_X509_MAX_FILE_PATH_LEN];
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001619 DIR *dir = opendir( path );
1620
Paul Bakker66d5d072014-06-17 16:39:18 +02001621 if( dir == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001622 return( MBEDTLS_ERR_X509_FILE_IO_ERROR );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001623
Ron Eldor63140682017-01-09 19:27:59 +02001624#if defined(MBEDTLS_THREADING_C)
Manuel Pégourié-Gonnard944cfe82015-05-27 20:07:18 +02001625 if( ( ret = mbedtls_mutex_lock( &mbedtls_threading_readdir_mutex ) ) != 0 )
Manuel Pégourié-Gonnardf9b85d92015-06-22 18:39:57 +02001626 {
1627 closedir( dir );
Manuel Pégourié-Gonnard5ad68e42013-11-28 17:11:54 +01001628 return( ret );
Manuel Pégourié-Gonnardf9b85d92015-06-22 18:39:57 +02001629 }
Ron Eldor63140682017-01-09 19:27:59 +02001630#endif /* MBEDTLS_THREADING_C */
Manuel Pégourié-Gonnard5ad68e42013-11-28 17:11:54 +01001631
Paul Elliottfb91a482021-03-05 14:17:51 +00001632 memset( &sb, 0, sizeof( sb ) );
1633
Manuel Pégourié-Gonnard964bf9b2013-11-26 16:47:11 +01001634 while( ( entry = readdir( dir ) ) != NULL )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001635 {
Andres AGf9113192016-09-02 14:06:04 +01001636 snp_ret = mbedtls_snprintf( entry_name, sizeof entry_name,
1637 "%s/%s", path, entry->d_name );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001638
Andres AGf9113192016-09-02 14:06:04 +01001639 if( snp_ret < 0 || (size_t)snp_ret >= sizeof entry_name )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001640 {
Andres AGf9113192016-09-02 14:06:04 +01001641 ret = MBEDTLS_ERR_X509_BUFFER_TOO_SMALL;
1642 goto cleanup;
1643 }
1644 else if( stat( entry_name, &sb ) == -1 )
1645 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001646 ret = MBEDTLS_ERR_X509_FILE_IO_ERROR;
Manuel Pégourié-Gonnard5ad68e42013-11-28 17:11:54 +01001647 goto cleanup;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001648 }
1649
1650 if( !S_ISREG( sb.st_mode ) )
1651 continue;
1652
1653 // Ignore parse errors
1654 //
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001655 t_ret = mbedtls_x509_crt_parse_file( chain, entry_name );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001656 if( t_ret < 0 )
1657 ret++;
1658 else
1659 ret += t_ret;
1660 }
Manuel Pégourié-Gonnard5ad68e42013-11-28 17:11:54 +01001661
1662cleanup:
Andres AGf9113192016-09-02 14:06:04 +01001663 closedir( dir );
1664
Ron Eldor63140682017-01-09 19:27:59 +02001665#if defined(MBEDTLS_THREADING_C)
Manuel Pégourié-Gonnard944cfe82015-05-27 20:07:18 +02001666 if( mbedtls_mutex_unlock( &mbedtls_threading_readdir_mutex ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001667 ret = MBEDTLS_ERR_THREADING_MUTEX_ERROR;
Ron Eldor63140682017-01-09 19:27:59 +02001668#endif /* MBEDTLS_THREADING_C */
Manuel Pégourié-Gonnard5ad68e42013-11-28 17:11:54 +01001669
Paul Bakkerbe089b02013-10-14 15:51:50 +02001670#endif /* _WIN32 */
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001671
1672 return( ret );
1673}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001674#endif /* MBEDTLS_FS_IO */
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001675
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001676/*
1677 * OtherName ::= SEQUENCE {
1678 * type-id OBJECT IDENTIFIER,
1679 * value [0] EXPLICIT ANY DEFINED BY type-id }
1680 *
1681 * HardwareModuleName ::= SEQUENCE {
1682 * hwType OBJECT IDENTIFIER,
1683 * hwSerialNum OCTET STRING }
1684 *
1685 * NOTE: we currently only parse and use otherName of type HwModuleName,
1686 * as defined in RFC 4108.
1687 */
1688static int x509_get_other_name( const mbedtls_x509_buf *subject_alt_name,
1689 mbedtls_x509_san_other_name *other_name )
1690{
Janos Follathab23cd12019-05-09 13:53:57 +01001691 int ret = 0;
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001692 size_t len;
1693 unsigned char *p = subject_alt_name->p;
1694 const unsigned char *end = p + subject_alt_name->len;
1695 mbedtls_x509_buf cur_oid;
1696
1697 if( ( subject_alt_name->tag &
1698 ( MBEDTLS_ASN1_TAG_CLASS_MASK | MBEDTLS_ASN1_TAG_VALUE_MASK ) ) !=
1699 ( MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_X509_SAN_OTHER_NAME ) )
1700 {
1701 /*
1702 * The given subject alternative name is not of type "othername".
1703 */
1704 return( MBEDTLS_ERR_X509_BAD_INPUT_DATA );
1705 }
1706
1707 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
1708 MBEDTLS_ASN1_OID ) ) != 0 )
Chris Jones9f7a6932021-04-14 12:12:09 +01001709 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret ) );
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001710
1711 cur_oid.tag = MBEDTLS_ASN1_OID;
1712 cur_oid.p = p;
1713 cur_oid.len = len;
1714
1715 /*
1716 * Only HwModuleName is currently supported.
1717 */
1718 if( MBEDTLS_OID_CMP( MBEDTLS_OID_ON_HW_MODULE_NAME, &cur_oid ) != 0 )
1719 {
1720 return( MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE );
1721 }
1722
Ron Eldor890819a2019-05-13 19:03:04 +03001723 if( p + len >= end )
1724 {
Sébastien Duquette661d7252019-06-23 17:45:26 -04001725 mbedtls_platform_zeroize( other_name, sizeof( *other_name ) );
Chris Jones9f7a6932021-04-14 12:12:09 +01001726 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
1727 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH ) );
Ron Eldor890819a2019-05-13 19:03:04 +03001728 }
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001729 p += len;
1730 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
1731 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_CONTEXT_SPECIFIC ) ) != 0 )
Chris Jones9f7a6932021-04-14 12:12:09 +01001732 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret ) );
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001733
1734 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
1735 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
Chris Jones9f7a6932021-04-14 12:12:09 +01001736 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret ) );
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001737
1738 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len, MBEDTLS_ASN1_OID ) ) != 0 )
Chris Jones9f7a6932021-04-14 12:12:09 +01001739 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret ) );
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001740
1741 other_name->value.hardware_module_name.oid.tag = MBEDTLS_ASN1_OID;
1742 other_name->value.hardware_module_name.oid.p = p;
1743 other_name->value.hardware_module_name.oid.len = len;
1744
Ron Eldor890819a2019-05-13 19:03:04 +03001745 if( p + len >= end )
1746 {
Sébastien Duquette661d7252019-06-23 17:45:26 -04001747 mbedtls_platform_zeroize( other_name, sizeof( *other_name ) );
Chris Jones9f7a6932021-04-14 12:12:09 +01001748 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
1749 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH ) );
Ron Eldor890819a2019-05-13 19:03:04 +03001750 }
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001751 p += len;
1752 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
1753 MBEDTLS_ASN1_OCTET_STRING ) ) != 0 )
Chris Jones9f7a6932021-04-14 12:12:09 +01001754 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret ) );
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001755
1756 other_name->value.hardware_module_name.val.tag = MBEDTLS_ASN1_OCTET_STRING;
1757 other_name->value.hardware_module_name.val.p = p;
1758 other_name->value.hardware_module_name.val.len = len;
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001759 p += len;
1760 if( p != end )
1761 {
Ron Eldor890819a2019-05-13 19:03:04 +03001762 mbedtls_platform_zeroize( other_name,
Sébastien Duquette661d7252019-06-23 17:45:26 -04001763 sizeof( *other_name ) );
Chris Jones9f7a6932021-04-14 12:12:09 +01001764 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
1765 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH ) );
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001766 }
1767 return( 0 );
1768}
1769
Peter Kolbus9a969b62018-12-11 13:55:56 -06001770int mbedtls_x509_parse_subject_alt_name( const mbedtls_x509_buf *san_buf,
1771 mbedtls_x509_subject_alternative_name *san )
1772{
1773 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1774 switch( san_buf->tag &
1775 ( MBEDTLS_ASN1_TAG_CLASS_MASK |
1776 MBEDTLS_ASN1_TAG_VALUE_MASK ) )
1777 {
1778 /*
1779 * otherName
1780 */
1781 case( MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_X509_SAN_OTHER_NAME ):
1782 {
1783 mbedtls_x509_san_other_name other_name;
1784
1785 ret = x509_get_other_name( san_buf, &other_name );
1786 if( ret != 0 )
1787 return( ret );
1788
1789 memset( san, 0, sizeof( mbedtls_x509_subject_alternative_name ) );
1790 san->type = MBEDTLS_X509_SAN_OTHER_NAME;
1791 memcpy( &san->san.other_name,
1792 &other_name, sizeof( other_name ) );
1793
1794 }
1795 break;
1796
1797 /*
1798 * dNSName
1799 */
1800 case( MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_X509_SAN_DNS_NAME ):
1801 {
1802 memset( san, 0, sizeof( mbedtls_x509_subject_alternative_name ) );
1803 san->type = MBEDTLS_X509_SAN_DNS_NAME;
1804
1805 memcpy( &san->san.unstructured_name,
1806 san_buf, sizeof( *san_buf ) );
1807
1808 }
1809 break;
1810
1811 /*
1812 * Type not supported
1813 */
1814 default:
1815 return( MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE );
1816 }
1817 return( 0 );
1818}
1819
1820#if !defined(MBEDTLS_X509_REMOVE_INFO)
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02001821static int x509_info_subject_alt_name( char **buf, size_t *size,
Janos Follath2f0ec1e2019-05-10 11:06:31 +01001822 const mbedtls_x509_sequence
1823 *subject_alt_name,
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001824 const char *prefix )
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02001825{
Janos Follath865b3eb2019-12-16 11:46:15 +00001826 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02001827 size_t n = *size;
1828 char *p = *buf;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001829 const mbedtls_x509_sequence *cur = subject_alt_name;
Ron Eldor890819a2019-05-13 19:03:04 +03001830 mbedtls_x509_subject_alternative_name san;
1831 int parse_ret;
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02001832
1833 while( cur != NULL )
1834 {
Ron Eldor890819a2019-05-13 19:03:04 +03001835 memset( &san, 0, sizeof( san ) );
1836 parse_ret = mbedtls_x509_parse_subject_alt_name( &cur->buf, &san );
1837 if( parse_ret != 0 )
1838 {
1839 if( parse_ret == MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE )
1840 {
1841 ret = mbedtls_snprintf( p, n, "\n%s <unsupported>", prefix );
1842 MBEDTLS_X509_SAFE_SNPRINTF;
1843 }
1844 else
1845 {
1846 ret = mbedtls_snprintf( p, n, "\n%s <malformed>", prefix );
1847 MBEDTLS_X509_SAFE_SNPRINTF;
1848 }
1849 cur = cur->next;
1850 continue;
1851 }
1852
1853 switch( san.type )
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02001854 {
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001855 /*
1856 * otherName
1857 */
Ron Eldora2913912019-05-16 16:17:38 +03001858 case MBEDTLS_X509_SAN_OTHER_NAME:
Ron Eldor890819a2019-05-13 19:03:04 +03001859 {
1860 mbedtls_x509_san_other_name *other_name = &san.san.other_name;
1861
1862 ret = mbedtls_snprintf( p, n, "\n%s otherName :", prefix );
1863 MBEDTLS_X509_SAFE_SNPRINTF;
1864
1865 if( MBEDTLS_OID_CMP( MBEDTLS_OID_ON_HW_MODULE_NAME,
1866 &other_name->value.hardware_module_name.oid ) != 0 )
Janos Follath22f605f2019-05-10 10:37:17 +01001867 {
Ron Eldor890819a2019-05-13 19:03:04 +03001868 ret = mbedtls_snprintf( p, n, "\n%s hardware module name :", prefix );
1869 MBEDTLS_X509_SAFE_SNPRINTF;
1870 ret = mbedtls_snprintf( p, n, "\n%s hardware type : ", prefix );
Janos Follath2f0ec1e2019-05-10 11:06:31 +01001871 MBEDTLS_X509_SAFE_SNPRINTF;
1872
Ron Eldor890819a2019-05-13 19:03:04 +03001873 ret = mbedtls_oid_get_numeric_string( p, n, &other_name->value.hardware_module_name.oid );
1874 MBEDTLS_X509_SAFE_SNPRINTF;
Janos Follath2f0ec1e2019-05-10 11:06:31 +01001875
Ron Eldor890819a2019-05-13 19:03:04 +03001876 ret = mbedtls_snprintf( p, n, "\n%s hardware serial number : ", prefix );
1877 MBEDTLS_X509_SAFE_SNPRINTF;
1878
1879 if( other_name->value.hardware_module_name.val.len >= n )
1880 {
1881 *p = '\0';
1882 return( MBEDTLS_ERR_X509_BUFFER_TOO_SMALL );
Janos Follath22f605f2019-05-10 10:37:17 +01001883 }
1884
Ron Eldora2913912019-05-16 16:17:38 +03001885 memcpy( p, other_name->value.hardware_module_name.val.p,
1886 other_name->value.hardware_module_name.val.len );
1887 p += other_name->value.hardware_module_name.val.len;
1888
Ron Eldor890819a2019-05-13 19:03:04 +03001889 n -= other_name->value.hardware_module_name.val.len;
Janos Follath2f0ec1e2019-05-10 11:06:31 +01001890
Ron Eldor890819a2019-05-13 19:03:04 +03001891 }/* MBEDTLS_OID_ON_HW_MODULE_NAME */
1892 }
1893 break;
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001894
1895 /*
1896 * dNSName
1897 */
Ron Eldora2913912019-05-16 16:17:38 +03001898 case MBEDTLS_X509_SAN_DNS_NAME:
Ron Eldor890819a2019-05-13 19:03:04 +03001899 {
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001900 ret = mbedtls_snprintf( p, n, "\n%s dNSName : ", prefix );
1901 MBEDTLS_X509_SAFE_SNPRINTF;
Ron Eldor890819a2019-05-13 19:03:04 +03001902 if( san.san.unstructured_name.len >= n )
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001903 {
1904 *p = '\0';
1905 return( MBEDTLS_ERR_X509_BUFFER_TOO_SMALL );
1906 }
Ron Eldora2913912019-05-16 16:17:38 +03001907
1908 memcpy( p, san.san.unstructured_name.p, san.san.unstructured_name.len );
1909 p += san.san.unstructured_name.len;
Ron Eldor890819a2019-05-13 19:03:04 +03001910 n -= san.san.unstructured_name.len;
Ron Eldor890819a2019-05-13 19:03:04 +03001911 }
1912 break;
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001913
1914 /*
Ron Eldor890819a2019-05-13 19:03:04 +03001915 * Type not supported, skip item.
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001916 */
1917 default:
Janos Follath22f605f2019-05-10 10:37:17 +01001918 ret = mbedtls_snprintf( p, n, "\n%s <unsupported>", prefix );
1919 MBEDTLS_X509_SAFE_SNPRINTF;
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001920 break;
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02001921 }
1922
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02001923 cur = cur->next;
1924 }
1925
1926 *p = '\0';
1927
1928 *size = n;
1929 *buf = p;
1930
1931 return( 0 );
1932}
1933
Manuel Pégourié-Gonnard0db29b02014-04-01 18:12:24 +02001934#define PRINT_ITEM(i) \
1935 { \
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001936 ret = mbedtls_snprintf( p, n, "%s" i, sep ); \
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001937 MBEDTLS_X509_SAFE_SNPRINTF; \
Manuel Pégourié-Gonnard0db29b02014-04-01 18:12:24 +02001938 sep = ", "; \
1939 }
1940
1941#define CERT_TYPE(type,name) \
Hanno Becker1eeca412018-10-15 12:01:35 +01001942 if( ns_cert_type & (type) ) \
Manuel Pégourié-Gonnard0db29b02014-04-01 18:12:24 +02001943 PRINT_ITEM( name );
1944
Manuel Pégourié-Gonnard919f8f52014-04-01 13:01:11 +02001945static int x509_info_cert_type( char **buf, size_t *size,
1946 unsigned char ns_cert_type )
1947{
Janos Follath865b3eb2019-12-16 11:46:15 +00001948 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard919f8f52014-04-01 13:01:11 +02001949 size_t n = *size;
1950 char *p = *buf;
Manuel Pégourié-Gonnard7b30cfc2014-04-01 18:00:07 +02001951 const char *sep = "";
Manuel Pégourié-Gonnard919f8f52014-04-01 13:01:11 +02001952
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001953 CERT_TYPE( MBEDTLS_X509_NS_CERT_TYPE_SSL_CLIENT, "SSL Client" );
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01001954 CERT_TYPE( MBEDTLS_X509_NS_CERT_TYPE_SSL_SERVER, "SSL Server" );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001955 CERT_TYPE( MBEDTLS_X509_NS_CERT_TYPE_EMAIL, "Email" );
1956 CERT_TYPE( MBEDTLS_X509_NS_CERT_TYPE_OBJECT_SIGNING, "Object Signing" );
1957 CERT_TYPE( MBEDTLS_X509_NS_CERT_TYPE_RESERVED, "Reserved" );
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01001958 CERT_TYPE( MBEDTLS_X509_NS_CERT_TYPE_SSL_CA, "SSL CA" );
1959 CERT_TYPE( MBEDTLS_X509_NS_CERT_TYPE_EMAIL_CA, "Email CA" );
1960 CERT_TYPE( MBEDTLS_X509_NS_CERT_TYPE_OBJECT_SIGNING_CA, "Object Signing CA" );
Manuel Pégourié-Gonnard919f8f52014-04-01 13:01:11 +02001961
1962 *size = n;
1963 *buf = p;
1964
1965 return( 0 );
1966}
1967
Manuel Pégourié-Gonnard0db29b02014-04-01 18:12:24 +02001968#define KEY_USAGE(code,name) \
Hanno Becker1eeca412018-10-15 12:01:35 +01001969 if( key_usage & (code) ) \
Manuel Pégourié-Gonnard0db29b02014-04-01 18:12:24 +02001970 PRINT_ITEM( name );
1971
Manuel Pégourié-Gonnard65c2ddc2014-04-01 14:12:11 +02001972static int x509_info_key_usage( char **buf, size_t *size,
Manuel Pégourié-Gonnard9a702252015-06-23 10:14:36 +02001973 unsigned int key_usage )
Manuel Pégourié-Gonnard65c2ddc2014-04-01 14:12:11 +02001974{
Janos Follath865b3eb2019-12-16 11:46:15 +00001975 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard65c2ddc2014-04-01 14:12:11 +02001976 size_t n = *size;
1977 char *p = *buf;
Manuel Pégourié-Gonnard7b30cfc2014-04-01 18:00:07 +02001978 const char *sep = "";
Manuel Pégourié-Gonnard65c2ddc2014-04-01 14:12:11 +02001979
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001980 KEY_USAGE( MBEDTLS_X509_KU_DIGITAL_SIGNATURE, "Digital Signature" );
1981 KEY_USAGE( MBEDTLS_X509_KU_NON_REPUDIATION, "Non Repudiation" );
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01001982 KEY_USAGE( MBEDTLS_X509_KU_KEY_ENCIPHERMENT, "Key Encipherment" );
1983 KEY_USAGE( MBEDTLS_X509_KU_DATA_ENCIPHERMENT, "Data Encipherment" );
1984 KEY_USAGE( MBEDTLS_X509_KU_KEY_AGREEMENT, "Key Agreement" );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001985 KEY_USAGE( MBEDTLS_X509_KU_KEY_CERT_SIGN, "Key Cert Sign" );
1986 KEY_USAGE( MBEDTLS_X509_KU_CRL_SIGN, "CRL Sign" );
Manuel Pégourié-Gonnard9a702252015-06-23 10:14:36 +02001987 KEY_USAGE( MBEDTLS_X509_KU_ENCIPHER_ONLY, "Encipher Only" );
1988 KEY_USAGE( MBEDTLS_X509_KU_DECIPHER_ONLY, "Decipher Only" );
Manuel Pégourié-Gonnard65c2ddc2014-04-01 14:12:11 +02001989
1990 *size = n;
1991 *buf = p;
1992
1993 return( 0 );
1994}
1995
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02001996static int x509_info_ext_key_usage( char **buf, size_t *size,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001997 const mbedtls_x509_sequence *extended_key_usage )
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02001998{
Janos Follath865b3eb2019-12-16 11:46:15 +00001999 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02002000 const char *desc;
2001 size_t n = *size;
2002 char *p = *buf;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002003 const mbedtls_x509_sequence *cur = extended_key_usage;
Manuel Pégourié-Gonnard7b30cfc2014-04-01 18:00:07 +02002004 const char *sep = "";
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02002005
2006 while( cur != NULL )
2007 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002008 if( mbedtls_oid_get_extended_key_usage( &cur->buf, &desc ) != 0 )
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02002009 desc = "???";
2010
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002011 ret = mbedtls_snprintf( p, n, "%s%s", sep, desc );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002012 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02002013
Manuel Pégourié-Gonnard7b30cfc2014-04-01 18:00:07 +02002014 sep = ", ";
2015
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02002016 cur = cur->next;
2017 }
2018
2019 *size = n;
2020 *buf = p;
2021
2022 return( 0 );
2023}
2024
Ron Eldor74d9acc2019-03-21 14:00:03 +02002025static int x509_info_cert_policies( char **buf, size_t *size,
2026 const mbedtls_x509_sequence *certificate_policies )
2027{
Janos Follath865b3eb2019-12-16 11:46:15 +00002028 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Ron Eldor74d9acc2019-03-21 14:00:03 +02002029 const char *desc;
2030 size_t n = *size;
2031 char *p = *buf;
2032 const mbedtls_x509_sequence *cur = certificate_policies;
2033 const char *sep = "";
2034
2035 while( cur != NULL )
2036 {
2037 if( mbedtls_oid_get_certificate_policies( &cur->buf, &desc ) != 0 )
2038 desc = "???";
2039
2040 ret = mbedtls_snprintf( p, n, "%s%s", sep, desc );
2041 MBEDTLS_X509_SAFE_SNPRINTF;
2042
2043 sep = ", ";
2044
2045 cur = cur->next;
2046 }
2047
2048 *size = n;
2049 *buf = p;
2050
2051 return( 0 );
2052}
2053
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002054/*
2055 * Return an informational string about the certificate.
2056 */
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002057#define BEFORE_COLON 18
2058#define BC "18"
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002059int mbedtls_x509_crt_info( char *buf, size_t size, const char *prefix,
2060 const mbedtls_x509_crt *crt )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002061{
Janos Follath865b3eb2019-12-16 11:46:15 +00002062 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002063 size_t n;
2064 char *p;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002065 char key_size_str[BEFORE_COLON];
2066
2067 p = buf;
2068 n = size;
2069
Janos Follath98e28a72016-05-31 14:03:54 +01002070 if( NULL == crt )
2071 {
2072 ret = mbedtls_snprintf( p, n, "\nCertificate is uninitialised!\n" );
2073 MBEDTLS_X509_SAFE_SNPRINTF;
2074
2075 return( (int) ( size - n ) );
2076 }
2077
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002078 ret = mbedtls_snprintf( p, n, "%scert. version : %d\n",
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002079 prefix, crt->version );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002080 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002081 ret = mbedtls_snprintf( p, n, "%sserial number : ",
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002082 prefix );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002083 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002084
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002085 ret = mbedtls_x509_serial_gets( p, n, &crt->serial );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002086 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002087
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002088 ret = mbedtls_snprintf( p, n, "\n%sissuer name : ", prefix );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002089 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002090 ret = mbedtls_x509_dn_gets( p, n, &crt->issuer );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002091 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002092
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002093 ret = mbedtls_snprintf( p, n, "\n%ssubject name : ", prefix );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002094 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002095 ret = mbedtls_x509_dn_gets( p, n, &crt->subject );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002096 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002097
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002098 ret = mbedtls_snprintf( p, n, "\n%sissued on : " \
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002099 "%04d-%02d-%02d %02d:%02d:%02d", prefix,
2100 crt->valid_from.year, crt->valid_from.mon,
2101 crt->valid_from.day, crt->valid_from.hour,
2102 crt->valid_from.min, crt->valid_from.sec );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002103 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002104
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002105 ret = mbedtls_snprintf( p, n, "\n%sexpires on : " \
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002106 "%04d-%02d-%02d %02d:%02d:%02d", prefix,
2107 crt->valid_to.year, crt->valid_to.mon,
2108 crt->valid_to.day, crt->valid_to.hour,
2109 crt->valid_to.min, crt->valid_to.sec );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002110 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002111
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002112 ret = mbedtls_snprintf( p, n, "\n%ssigned using : ", prefix );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002113 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002114
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002115 ret = mbedtls_x509_sig_alg_gets( p, n, &crt->sig_oid, crt->sig_pk,
Manuel Pégourié-Gonnardbf696d02014-06-05 17:07:30 +02002116 crt->sig_md, crt->sig_opts );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002117 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002118
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002119 /* Key size */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002120 if( ( ret = mbedtls_x509_key_size_helper( key_size_str, BEFORE_COLON,
2121 mbedtls_pk_get_name( &crt->pk ) ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002122 {
2123 return( ret );
2124 }
2125
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002126 ret = mbedtls_snprintf( p, n, "\n%s%-" BC "s: %d bits", prefix, key_size_str,
Manuel Pégourié-Gonnard097c7bb2015-06-18 16:43:38 +02002127 (int) mbedtls_pk_get_bitlen( &crt->pk ) );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002128 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002129
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002130 /*
2131 * Optional extensions
2132 */
2133
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01002134 if( crt->ext_types & MBEDTLS_X509_EXT_BASIC_CONSTRAINTS )
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002135 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002136 ret = mbedtls_snprintf( p, n, "\n%sbasic constraints : CA=%s", prefix,
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002137 crt->ca_istrue ? "true" : "false" );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002138 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002139
2140 if( crt->max_pathlen > 0 )
2141 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002142 ret = mbedtls_snprintf( p, n, ", max_pathlen=%d", crt->max_pathlen - 1 );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002143 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002144 }
2145 }
2146
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01002147 if( crt->ext_types & MBEDTLS_X509_EXT_SUBJECT_ALT_NAME )
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002148 {
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02002149 ret = mbedtls_snprintf( p, n, "\n%ssubject alt name :", prefix );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002150 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02002151
2152 if( ( ret = x509_info_subject_alt_name( &p, &n,
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02002153 &crt->subject_alt_names,
Ron Eldor6aeae9e2019-05-20 12:00:36 +03002154 prefix ) ) != 0 )
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02002155 return( ret );
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002156 }
2157
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002158 if( crt->ext_types & MBEDTLS_X509_EXT_NS_CERT_TYPE )
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002159 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002160 ret = mbedtls_snprintf( p, n, "\n%scert. type : ", prefix );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002161 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnard919f8f52014-04-01 13:01:11 +02002162
2163 if( ( ret = x509_info_cert_type( &p, &n, crt->ns_cert_type ) ) != 0 )
2164 return( ret );
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002165 }
2166
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002167 if( crt->ext_types & MBEDTLS_X509_EXT_KEY_USAGE )
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002168 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002169 ret = mbedtls_snprintf( p, n, "\n%skey usage : ", prefix );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002170 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnard65c2ddc2014-04-01 14:12:11 +02002171
2172 if( ( ret = x509_info_key_usage( &p, &n, crt->key_usage ) ) != 0 )
2173 return( ret );
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002174 }
2175
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002176 if( crt->ext_types & MBEDTLS_X509_EXT_EXTENDED_KEY_USAGE )
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002177 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002178 ret = mbedtls_snprintf( p, n, "\n%sext key usage : ", prefix );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002179 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02002180
2181 if( ( ret = x509_info_ext_key_usage( &p, &n,
2182 &crt->ext_key_usage ) ) != 0 )
2183 return( ret );
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002184 }
2185
Ron Eldor74d9acc2019-03-21 14:00:03 +02002186 if( crt->ext_types & MBEDTLS_OID_X509_EXT_CERTIFICATE_POLICIES )
2187 {
2188 ret = mbedtls_snprintf( p, n, "\n%scertificate policies : ", prefix );
2189 MBEDTLS_X509_SAFE_SNPRINTF;
2190
2191 if( ( ret = x509_info_cert_policies( &p, &n,
2192 &crt->certificate_policies ) ) != 0 )
2193 return( ret );
2194 }
2195
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002196 ret = mbedtls_snprintf( p, n, "\n" );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002197 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002198
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002199 return( (int) ( size - n ) );
2200}
2201
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01002202struct x509_crt_verify_string {
2203 int code;
2204 const char *string;
2205};
2206
Hanno Becker7ac83f92020-10-09 10:48:22 +01002207#define X509_CRT_ERROR_INFO( err, err_str, info ) { err, info },
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01002208static const struct x509_crt_verify_string x509_crt_verify_strings[] = {
Hanno Becker7ac83f92020-10-09 10:48:22 +01002209 MBEDTLS_X509_CRT_ERROR_INFO_LIST
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01002210 { 0, NULL }
2211};
Hanno Becker7ac83f92020-10-09 10:48:22 +01002212#undef X509_CRT_ERROR_INFO
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01002213
2214int mbedtls_x509_crt_verify_info( char *buf, size_t size, const char *prefix,
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02002215 uint32_t flags )
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01002216{
Janos Follath865b3eb2019-12-16 11:46:15 +00002217 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01002218 const struct x509_crt_verify_string *cur;
2219 char *p = buf;
2220 size_t n = size;
2221
2222 for( cur = x509_crt_verify_strings; cur->string != NULL ; cur++ )
2223 {
2224 if( ( flags & cur->code ) == 0 )
2225 continue;
2226
2227 ret = mbedtls_snprintf( p, n, "%s%s\n", prefix, cur->string );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002228 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01002229 flags ^= cur->code;
2230 }
2231
2232 if( flags != 0 )
2233 {
2234 ret = mbedtls_snprintf( p, n, "%sUnknown reason "
2235 "(this should not happen)\n", prefix );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002236 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01002237 }
2238
2239 return( (int) ( size - n ) );
2240}
Hanno Becker612a2f12020-10-09 09:19:39 +01002241#endif /* MBEDTLS_X509_REMOVE_INFO */
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01002242
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002243#if defined(MBEDTLS_X509_CHECK_KEY_USAGE)
Manuel Pégourié-Gonnard655a9642015-06-23 10:48:44 +02002244int mbedtls_x509_crt_check_key_usage( const mbedtls_x509_crt *crt,
2245 unsigned int usage )
Manuel Pégourié-Gonnard603116c2014-04-09 09:50:03 +02002246{
Manuel Pégourié-Gonnard655a9642015-06-23 10:48:44 +02002247 unsigned int usage_must, usage_may;
2248 unsigned int may_mask = MBEDTLS_X509_KU_ENCIPHER_ONLY
2249 | MBEDTLS_X509_KU_DECIPHER_ONLY;
2250
2251 if( ( crt->ext_types & MBEDTLS_X509_EXT_KEY_USAGE ) == 0 )
2252 return( 0 );
2253
2254 usage_must = usage & ~may_mask;
2255
2256 if( ( ( crt->key_usage & ~may_mask ) & usage_must ) != usage_must )
2257 return( MBEDTLS_ERR_X509_BAD_INPUT_DATA );
2258
2259 usage_may = usage & may_mask;
2260
2261 if( ( ( crt->key_usage & may_mask ) | usage_may ) != usage_may )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002262 return( MBEDTLS_ERR_X509_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard603116c2014-04-09 09:50:03 +02002263
2264 return( 0 );
2265}
2266#endif
2267
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002268#if defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE)
2269int mbedtls_x509_crt_check_extended_key_usage( const mbedtls_x509_crt *crt,
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02002270 const char *usage_oid,
2271 size_t usage_len )
2272{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002273 const mbedtls_x509_sequence *cur;
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02002274
2275 /* Extension is not mandatory, absent means no restriction */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002276 if( ( crt->ext_types & MBEDTLS_X509_EXT_EXTENDED_KEY_USAGE ) == 0 )
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02002277 return( 0 );
2278
2279 /*
2280 * Look for the requested usage (or wildcard ANY) in our list
2281 */
2282 for( cur = &crt->ext_key_usage; cur != NULL; cur = cur->next )
2283 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002284 const mbedtls_x509_buf *cur_oid = &cur->buf;
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02002285
2286 if( cur_oid->len == usage_len &&
2287 memcmp( cur_oid->p, usage_oid, usage_len ) == 0 )
2288 {
2289 return( 0 );
2290 }
2291
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002292 if( MBEDTLS_OID_CMP( MBEDTLS_OID_ANY_EXTENDED_KEY_USAGE, cur_oid ) == 0 )
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02002293 return( 0 );
2294 }
2295
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002296 return( MBEDTLS_ERR_X509_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02002297}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002298#endif /* MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE */
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02002299
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002300#if defined(MBEDTLS_X509_CRL_PARSE_C)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002301/*
2302 * Return 1 if the certificate is revoked, or 0 otherwise.
2303 */
Manuel Pégourié-Gonnardc730ed32015-06-02 10:38:50 +01002304int mbedtls_x509_crt_is_revoked( const mbedtls_x509_crt *crt, const mbedtls_x509_crl *crl )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002305{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002306 const mbedtls_x509_crl_entry *cur = &crl->entry;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002307
2308 while( cur != NULL && cur->serial.len != 0 )
2309 {
2310 if( crt->serial.len == cur->serial.len &&
2311 memcmp( crt->serial.p, cur->serial.p, crt->serial.len ) == 0 )
2312 {
Raoul Strackxa4e86142020-06-15 17:03:13 +02002313 return( 1 );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002314 }
2315
2316 cur = cur->next;
2317 }
2318
2319 return( 0 );
2320}
2321
2322/*
Manuel Pégourié-Gonnardeeef9472016-02-22 11:36:55 +01002323 * Check that the given certificate is not revoked according to the CRL.
Manuel Pégourié-Gonnard08eacec2017-10-18 14:20:24 +02002324 * Skip validation if no CRL for the given CA is present.
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002325 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002326static int x509_crt_verifycrl( mbedtls_x509_crt *crt, mbedtls_x509_crt *ca,
Manuel Pégourié-Gonnard95051642015-06-15 10:39:46 +02002327 mbedtls_x509_crl *crl_list,
2328 const mbedtls_x509_crt_profile *profile )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002329{
2330 int flags = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002331 unsigned char hash[MBEDTLS_MD_MAX_SIZE];
2332 const mbedtls_md_info_t *md_info;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002333
2334 if( ca == NULL )
2335 return( flags );
2336
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002337 while( crl_list != NULL )
2338 {
2339 if( crl_list->version == 0 ||
Hanno Beckerb75ffb52018-11-02 09:19:54 +00002340 x509_name_cmp( &crl_list->issuer, &ca->subject ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002341 {
2342 crl_list = crl_list->next;
2343 continue;
2344 }
2345
2346 /*
Manuel Pégourié-Gonnard99d4f192014-04-08 15:10:07 +02002347 * Check if the CA is configured to sign CRLs
2348 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002349#if defined(MBEDTLS_X509_CHECK_KEY_USAGE)
Hanno Beckerb75ffb52018-11-02 09:19:54 +00002350 if( mbedtls_x509_crt_check_key_usage( ca,
2351 MBEDTLS_X509_KU_CRL_SIGN ) != 0 )
Manuel Pégourié-Gonnard99d4f192014-04-08 15:10:07 +02002352 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002353 flags |= MBEDTLS_X509_BADCRL_NOT_TRUSTED;
Manuel Pégourié-Gonnard99d4f192014-04-08 15:10:07 +02002354 break;
2355 }
2356#endif
2357
2358 /*
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002359 * Check if CRL is correctly signed by the trusted CA
2360 */
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +02002361 if( x509_profile_check_md_alg( profile, crl_list->sig_md ) != 0 )
2362 flags |= MBEDTLS_X509_BADCRL_BAD_MD;
2363
2364 if( x509_profile_check_pk_alg( profile, crl_list->sig_pk ) != 0 )
2365 flags |= MBEDTLS_X509_BADCRL_BAD_PK;
2366
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002367 md_info = mbedtls_md_info_from_type( crl_list->sig_md );
Manuel Pégourié-Gonnard329e78c2017-06-26 12:22:17 +02002368 if( mbedtls_md( md_info, crl_list->tbs.p, crl_list->tbs.len, hash ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002369 {
Manuel Pégourié-Gonnard329e78c2017-06-26 12:22:17 +02002370 /* Note: this can't happen except after an internal error */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002371 flags |= MBEDTLS_X509_BADCRL_NOT_TRUSTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002372 break;
2373 }
2374
Manuel Pégourié-Gonnard3f816912017-10-26 10:24:16 +02002375 if( x509_profile_check_key( profile, &ca->pk ) != 0 )
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +02002376 flags |= MBEDTLS_X509_BADCERT_BAD_KEY;
Manuel Pégourié-Gonnard95051642015-06-15 10:39:46 +02002377
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002378 if( mbedtls_pk_verify_ext( crl_list->sig_pk, crl_list->sig_opts, &ca->pk,
2379 crl_list->sig_md, hash, mbedtls_md_get_size( md_info ),
Manuel Pégourié-Gonnard53882022014-06-05 17:53:52 +02002380 crl_list->sig.p, crl_list->sig.len ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002381 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002382 flags |= MBEDTLS_X509_BADCRL_NOT_TRUSTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002383 break;
2384 }
2385
2386 /*
2387 * Check for validity of CRL (Do not drop out)
2388 */
Manuel Pégourié-Gonnardc730ed32015-06-02 10:38:50 +01002389 if( mbedtls_x509_time_is_past( &crl_list->next_update ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002390 flags |= MBEDTLS_X509_BADCRL_EXPIRED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002391
Manuel Pégourié-Gonnardc730ed32015-06-02 10:38:50 +01002392 if( mbedtls_x509_time_is_future( &crl_list->this_update ) )
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01002393 flags |= MBEDTLS_X509_BADCRL_FUTURE;
Manuel Pégourié-Gonnard95337652014-03-10 13:15:18 +01002394
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002395 /*
2396 * Check if certificate is revoked
2397 */
Manuel Pégourié-Gonnardc730ed32015-06-02 10:38:50 +01002398 if( mbedtls_x509_crt_is_revoked( crt, crl_list ) )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002399 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002400 flags |= MBEDTLS_X509_BADCERT_REVOKED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002401 break;
2402 }
2403
2404 crl_list = crl_list->next;
2405 }
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +02002406
Paul Bakkerd8bb8262014-06-17 14:06:49 +02002407 return( flags );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002408}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002409#endif /* MBEDTLS_X509_CRL_PARSE_C */
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002410
Manuel Pégourié-Gonnard88421242014-10-17 11:36:18 +02002411/*
Manuel Pégourié-Gonnardf82a4d52017-07-03 19:26:25 +02002412 * Check the signature of a certificate by its parent
2413 */
2414static int x509_crt_check_signature( const mbedtls_x509_crt *child,
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002415 mbedtls_x509_crt *parent,
2416 mbedtls_x509_crt_restart_ctx *rs_ctx )
Manuel Pégourié-Gonnardf82a4d52017-07-03 19:26:25 +02002417{
Manuel Pégourié-Gonnardf82a4d52017-07-03 19:26:25 +02002418 unsigned char hash[MBEDTLS_MD_MAX_SIZE];
Andrzej Kurekd4a65532018-10-31 06:18:39 -04002419 size_t hash_len;
2420#if !defined(MBEDTLS_USE_PSA_CRYPTO)
2421 const mbedtls_md_info_t *md_info;
Manuel Pégourié-Gonnardf82a4d52017-07-03 19:26:25 +02002422 md_info = mbedtls_md_info_from_type( child->sig_md );
Andrzej Kurekd4a65532018-10-31 06:18:39 -04002423 hash_len = mbedtls_md_get_size( md_info );
Andrzej Kurek8b38ff52018-11-20 03:20:09 -05002424
Andrzej Kurekd4a65532018-10-31 06:18:39 -04002425 /* Note: hash errors can happen only after an internal error */
Manuel Pégourié-Gonnardf82a4d52017-07-03 19:26:25 +02002426 if( mbedtls_md( md_info, child->tbs.p, child->tbs.len, hash ) != 0 )
Andrzej Kurekd4a65532018-10-31 06:18:39 -04002427 return( -1 );
2428#else
Jaeden Amero34973232019-02-20 10:32:28 +00002429 psa_hash_operation_t hash_operation = PSA_HASH_OPERATION_INIT;
Andrzej Kurekd4a65532018-10-31 06:18:39 -04002430 psa_algorithm_t hash_alg = mbedtls_psa_translate_md( child->sig_md );
2431
2432 if( psa_hash_setup( &hash_operation, hash_alg ) != PSA_SUCCESS )
2433 return( -1 );
2434
2435 if( psa_hash_update( &hash_operation, child->tbs.p, child->tbs.len )
2436 != PSA_SUCCESS )
Manuel Pégourié-Gonnardf82a4d52017-07-03 19:26:25 +02002437 {
Manuel Pégourié-Gonnardf82a4d52017-07-03 19:26:25 +02002438 return( -1 );
2439 }
2440
Andrzej Kurekd4a65532018-10-31 06:18:39 -04002441 if( psa_hash_finish( &hash_operation, hash, sizeof( hash ), &hash_len )
2442 != PSA_SUCCESS )
2443 {
Andrzej Kurekd4a65532018-10-31 06:18:39 -04002444 return( -1 );
2445 }
2446#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002447 /* Skip expensive computation on obvious mismatch */
2448 if( ! mbedtls_pk_can_do( &parent->pk, child->sig_pk ) )
Manuel Pégourié-Gonnardf82a4d52017-07-03 19:26:25 +02002449 return( -1 );
Manuel Pégourié-Gonnardf82a4d52017-07-03 19:26:25 +02002450
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002451#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002452 if( rs_ctx != NULL && child->sig_pk == MBEDTLS_PK_ECDSA )
2453 {
2454 return( mbedtls_pk_verify_restartable( &parent->pk,
Andrzej Kurekd4a65532018-10-31 06:18:39 -04002455 child->sig_md, hash, hash_len,
Manuel Pégourié-Gonnard15d7df22017-08-17 14:33:31 +02002456 child->sig.p, child->sig.len, &rs_ctx->pk ) );
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002457 }
2458#else
2459 (void) rs_ctx;
2460#endif
2461
2462 return( mbedtls_pk_verify_ext( child->sig_pk, child->sig_opts, &parent->pk,
Andrzej Kurekd4a65532018-10-31 06:18:39 -04002463 child->sig_md, hash, hash_len,
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002464 child->sig.p, child->sig.len ) );
Manuel Pégourié-Gonnardf82a4d52017-07-03 19:26:25 +02002465}
2466
2467/*
Manuel Pégourié-Gonnard312010e2014-04-09 14:30:11 +02002468 * Check if 'parent' is a suitable parent (signing CA) for 'child'.
2469 * Return 0 if yes, -1 if not.
Manuel Pégourié-Gonnardd249b7a2014-06-24 11:49:16 +02002470 *
2471 * top means parent is a locally-trusted certificate
Manuel Pégourié-Gonnard3fed0b32014-04-08 13:18:01 +02002472 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002473static int x509_crt_check_parent( const mbedtls_x509_crt *child,
2474 const mbedtls_x509_crt *parent,
Manuel Pégourié-Gonnard27e94792017-07-04 00:49:31 +02002475 int top )
Manuel Pégourié-Gonnard3fed0b32014-04-08 13:18:01 +02002476{
Manuel Pégourié-Gonnardd249b7a2014-06-24 11:49:16 +02002477 int need_ca_bit;
2478
Manuel Pégourié-Gonnardc4eff162014-06-19 12:18:08 +02002479 /* Parent must be the issuer */
Manuel Pégourié-Gonnardef9a6ae2014-10-17 12:25:12 +02002480 if( x509_name_cmp( &child->issuer, &parent->subject ) != 0 )
Manuel Pégourié-Gonnard312010e2014-04-09 14:30:11 +02002481 return( -1 );
Manuel Pégourié-Gonnard3fed0b32014-04-08 13:18:01 +02002482
Manuel Pégourié-Gonnardd249b7a2014-06-24 11:49:16 +02002483 /* Parent must have the basicConstraints CA bit set as a general rule */
2484 need_ca_bit = 1;
2485
2486 /* Exception: v1/v2 certificates that are locally trusted. */
2487 if( top && parent->version < 3 )
2488 need_ca_bit = 0;
2489
Manuel Pégourié-Gonnardd249b7a2014-06-24 11:49:16 +02002490 if( need_ca_bit && ! parent->ca_istrue )
2491 return( -1 );
2492
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002493#if defined(MBEDTLS_X509_CHECK_KEY_USAGE)
Manuel Pégourié-Gonnardd249b7a2014-06-24 11:49:16 +02002494 if( need_ca_bit &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002495 mbedtls_x509_crt_check_key_usage( parent, MBEDTLS_X509_KU_KEY_CERT_SIGN ) != 0 )
Manuel Pégourié-Gonnardc4eff162014-06-19 12:18:08 +02002496 {
2497 return( -1 );
2498 }
Manuel Pégourié-Gonnard312010e2014-04-09 14:30:11 +02002499#endif
2500
2501 return( 0 );
Manuel Pégourié-Gonnard3fed0b32014-04-08 13:18:01 +02002502}
2503
Manuel Pégourié-Gonnard35407c72017-06-29 10:45:25 +02002504/*
Manuel Pégourié-Gonnard3e329b82017-06-29 12:55:27 +02002505 * Find a suitable parent for child in candidates, or return NULL.
2506 *
2507 * Here suitable is defined as:
Manuel Pégourié-Gonnard2f09d592017-07-03 18:30:43 +02002508 * 1. subject name matches child's issuer
2509 * 2. if necessary, the CA bit is set and key usage allows signing certs
2510 * 3. for trusted roots, the signature is correct
Manuel Pégourié-Gonnardbe4ff422017-07-14 12:04:14 +02002511 * (for intermediates, the signature is checked and the result reported)
Manuel Pégourié-Gonnard2f09d592017-07-03 18:30:43 +02002512 * 4. pathlen constraints are satisfied
Manuel Pégourié-Gonnard3e329b82017-06-29 12:55:27 +02002513 *
Manuel Pégourié-Gonnard562df402017-08-08 18:09:14 +02002514 * If there's a suitable candidate which is also time-valid, return the first
2515 * such. Otherwise, return the first suitable candidate (or NULL if there is
2516 * none).
Manuel Pégourié-Gonnard3e329b82017-06-29 12:55:27 +02002517 *
2518 * The rationale for this rule is that someone could have a list of trusted
2519 * roots with two versions on the same root with different validity periods.
2520 * (At least one user reported having such a list and wanted it to just work.)
2521 * The reason we don't just require time-validity is that generally there is
2522 * only one version, and if it's expired we want the flags to state that
2523 * rather than NOT_TRUSTED, as would be the case if we required it here.
Manuel Pégourié-Gonnard2f09d592017-07-03 18:30:43 +02002524 *
2525 * The rationale for rule 3 (signature for trusted roots) is that users might
2526 * have two versions of the same CA with different keys in their list, and the
Manuel Pégourié-Gonnardbdc54402017-07-04 00:33:39 +02002527 * way we select the correct one is by checking the signature (as we don't
2528 * rely on key identifier extensions). (This is one way users might choose to
2529 * handle key rollover, another relies on self-issued certs, see [SIRO].)
Manuel Pégourié-Gonnard98a67782017-08-17 10:52:20 +02002530 *
2531 * Arguments:
Manuel Pégourié-Gonnarde57d7432018-03-07 10:00:57 +01002532 * - [in] child: certificate for which we're looking for a parent
2533 * - [in] candidates: chained list of potential parents
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +02002534 * - [out] r_parent: parent found (or NULL)
2535 * - [out] r_signature_is_good: 1 if child signature by parent is valid, or 0
Manuel Pégourié-Gonnarde57d7432018-03-07 10:00:57 +01002536 * - [in] top: 1 if candidates consists of trusted roots, ie we're at the top
2537 * of the chain, 0 otherwise
2538 * - [in] path_cnt: number of intermediates seen so far
2539 * - [in] self_cnt: number of self-signed intermediates seen so far
2540 * (will never be greater than path_cnt)
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +02002541 * - [in-out] rs_ctx: context for restarting operations
Manuel Pégourié-Gonnarde57d7432018-03-07 10:00:57 +01002542 *
2543 * Return value:
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +02002544 * - 0 on success
2545 * - MBEDTLS_ERR_ECP_IN_PROGRESS otherwise
Manuel Pégourié-Gonnard2f1c33d2017-06-29 12:27:23 +02002546 */
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002547static int x509_crt_find_parent_in(
2548 mbedtls_x509_crt *child,
2549 mbedtls_x509_crt *candidates,
2550 mbedtls_x509_crt **r_parent,
2551 int *r_signature_is_good,
2552 int top,
Manuel Pégourié-Gonnardbb216bd2017-08-28 13:25:55 +02002553 unsigned path_cnt,
2554 unsigned self_cnt,
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002555 mbedtls_x509_crt_restart_ctx *rs_ctx )
Manuel Pégourié-Gonnard2f1c33d2017-06-29 12:27:23 +02002556{
Janos Follath865b3eb2019-12-16 11:46:15 +00002557 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002558 mbedtls_x509_crt *parent, *fallback_parent;
Benjamin Kier36050732019-05-30 14:49:17 -04002559 int signature_is_good = 0, fallback_signature_is_good;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002560
2561#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard3627a8b2017-08-23 11:20:48 +02002562 /* did we have something in progress? */
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002563 if( rs_ctx != NULL && rs_ctx->parent != NULL )
2564 {
Manuel Pégourié-Gonnard3627a8b2017-08-23 11:20:48 +02002565 /* restore saved state */
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002566 parent = rs_ctx->parent;
2567 fallback_parent = rs_ctx->fallback_parent;
Manuel Pégourié-Gonnard78d7e8c2018-07-02 12:33:14 +02002568 fallback_signature_is_good = rs_ctx->fallback_signature_is_good;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002569
Manuel Pégourié-Gonnard3627a8b2017-08-23 11:20:48 +02002570 /* clear saved state */
2571 rs_ctx->parent = NULL;
2572 rs_ctx->fallback_parent = NULL;
Manuel Pégourié-Gonnard78d7e8c2018-07-02 12:33:14 +02002573 rs_ctx->fallback_signature_is_good = 0;
Manuel Pégourié-Gonnard3627a8b2017-08-23 11:20:48 +02002574
2575 /* resume where we left */
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002576 goto check_signature;
2577 }
2578#endif
2579
2580 fallback_parent = NULL;
Manuel Pégourié-Gonnard78d7e8c2018-07-02 12:33:14 +02002581 fallback_signature_is_good = 0;
Manuel Pégourié-Gonnard2f1c33d2017-06-29 12:27:23 +02002582
Manuel Pégourié-Gonnard2f1c33d2017-06-29 12:27:23 +02002583 for( parent = candidates; parent != NULL; parent = parent->next )
2584 {
Manuel Pégourié-Gonnard2f09d592017-07-03 18:30:43 +02002585 /* basic parenting skills (name, CA bit, key usage) */
Manuel Pégourié-Gonnard27e94792017-07-04 00:49:31 +02002586 if( x509_crt_check_parent( child, parent, top ) != 0 )
Manuel Pégourié-Gonnard2f1c33d2017-06-29 12:27:23 +02002587 continue;
2588
Manuel Pégourié-Gonnard9c6118c2017-06-29 12:38:42 +02002589 /* +1 because stored max_pathlen is 1 higher that the actual value */
2590 if( parent->max_pathlen > 0 &&
Manuel Pégourié-Gonnardbb216bd2017-08-28 13:25:55 +02002591 (size_t) parent->max_pathlen < 1 + path_cnt - self_cnt )
Manuel Pégourié-Gonnard9c6118c2017-06-29 12:38:42 +02002592 {
2593 continue;
2594 }
2595
Manuel Pégourié-Gonnard2f09d592017-07-03 18:30:43 +02002596 /* Signature */
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002597#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
2598check_signature:
2599#endif
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002600 ret = x509_crt_check_signature( child, parent, rs_ctx );
2601
2602#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002603 if( rs_ctx != NULL && ret == MBEDTLS_ERR_ECP_IN_PROGRESS )
2604 {
2605 /* save state */
2606 rs_ctx->parent = parent;
2607 rs_ctx->fallback_parent = fallback_parent;
Manuel Pégourié-Gonnard78d7e8c2018-07-02 12:33:14 +02002608 rs_ctx->fallback_signature_is_good = fallback_signature_is_good;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002609
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002610 return( ret );
2611 }
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002612#else
2613 (void) ret;
2614#endif
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002615
2616 signature_is_good = ret == 0;
2617 if( top && ! signature_is_good )
Manuel Pégourié-Gonnardf82a4d52017-07-03 19:26:25 +02002618 continue;
Manuel Pégourié-Gonnard2f09d592017-07-03 18:30:43 +02002619
Manuel Pégourié-Gonnard562df402017-08-08 18:09:14 +02002620 /* optional time check */
Manuel Pégourié-Gonnard3e329b82017-06-29 12:55:27 +02002621 if( mbedtls_x509_time_is_past( &parent->valid_to ) ||
2622 mbedtls_x509_time_is_future( &parent->valid_from ) )
2623 {
Manuel Pégourié-Gonnardbe4ff422017-07-14 12:04:14 +02002624 if( fallback_parent == NULL )
2625 {
2626 fallback_parent = parent;
Manuel Pégourié-Gonnard78d7e8c2018-07-02 12:33:14 +02002627 fallback_signature_is_good = signature_is_good;
Manuel Pégourié-Gonnardbe4ff422017-07-14 12:04:14 +02002628 }
Manuel Pégourié-Gonnard3e329b82017-06-29 12:55:27 +02002629
2630 continue;
2631 }
2632
Andy Gross1f627142019-01-30 10:25:53 -06002633 *r_parent = parent;
2634 *r_signature_is_good = signature_is_good;
2635
Manuel Pégourié-Gonnard2f1c33d2017-06-29 12:27:23 +02002636 break;
2637 }
2638
Andy Gross1f627142019-01-30 10:25:53 -06002639 if( parent == NULL )
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002640 {
2641 *r_parent = fallback_parent;
Manuel Pégourié-Gonnard78d7e8c2018-07-02 12:33:14 +02002642 *r_signature_is_good = fallback_signature_is_good;
Manuel Pégourié-Gonnardbe4ff422017-07-14 12:04:14 +02002643 }
Manuel Pégourié-Gonnard3e329b82017-06-29 12:55:27 +02002644
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002645 return( 0 );
Manuel Pégourié-Gonnard2f1c33d2017-06-29 12:27:23 +02002646}
2647
2648/*
Manuel Pégourié-Gonnard63686122017-07-04 01:01:39 +02002649 * Find a parent in trusted CAs or the provided chain, or return NULL.
2650 *
2651 * Searches in trusted CAs first, and return the first suitable parent found
2652 * (see find_parent_in() for definition of suitable).
Manuel Pégourié-Gonnard98a67782017-08-17 10:52:20 +02002653 *
2654 * Arguments:
Manuel Pégourié-Gonnarde57d7432018-03-07 10:00:57 +01002655 * - [in] child: certificate for which we're looking for a parent, followed
2656 * by a chain of possible intermediates
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +02002657 * - [in] trust_ca: list of locally trusted certificates
2658 * - [out] parent: parent found (or NULL)
2659 * - [out] parent_is_trusted: 1 if returned `parent` is trusted, or 0
2660 * - [out] signature_is_good: 1 if child signature by parent is valid, or 0
2661 * - [in] path_cnt: number of links in the chain so far (EE -> ... -> child)
2662 * - [in] self_cnt: number of self-signed certs in the chain so far
Manuel Pégourié-Gonnarde57d7432018-03-07 10:00:57 +01002663 * (will always be no greater than path_cnt)
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +02002664 * - [in-out] rs_ctx: context for restarting operations
Manuel Pégourié-Gonnarde57d7432018-03-07 10:00:57 +01002665 *
2666 * Return value:
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +02002667 * - 0 on success
2668 * - MBEDTLS_ERR_ECP_IN_PROGRESS otherwise
Manuel Pégourié-Gonnard63686122017-07-04 01:01:39 +02002669 */
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002670static int x509_crt_find_parent(
2671 mbedtls_x509_crt *child,
2672 mbedtls_x509_crt *trust_ca,
2673 mbedtls_x509_crt **parent,
2674 int *parent_is_trusted,
2675 int *signature_is_good,
Manuel Pégourié-Gonnardbb216bd2017-08-28 13:25:55 +02002676 unsigned path_cnt,
2677 unsigned self_cnt,
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002678 mbedtls_x509_crt_restart_ctx *rs_ctx )
Manuel Pégourié-Gonnard63686122017-07-04 01:01:39 +02002679{
Janos Follath865b3eb2019-12-16 11:46:15 +00002680 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard18547b52017-08-14 16:11:43 +02002681 mbedtls_x509_crt *search_list;
Manuel Pégourié-Gonnard63686122017-07-04 01:01:39 +02002682
Manuel Pégourié-Gonnard63686122017-07-04 01:01:39 +02002683 *parent_is_trusted = 1;
Manuel Pégourié-Gonnard18547b52017-08-14 16:11:43 +02002684
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002685#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard3627a8b2017-08-23 11:20:48 +02002686 /* restore then clear saved state if we have some stored */
2687 if( rs_ctx != NULL && rs_ctx->parent_is_trusted != -1 )
2688 {
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002689 *parent_is_trusted = rs_ctx->parent_is_trusted;
Manuel Pégourié-Gonnard3627a8b2017-08-23 11:20:48 +02002690 rs_ctx->parent_is_trusted = -1;
2691 }
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002692#endif
2693
Manuel Pégourié-Gonnard18547b52017-08-14 16:11:43 +02002694 while( 1 ) {
2695 search_list = *parent_is_trusted ? trust_ca : child->next;
2696
2697 ret = x509_crt_find_parent_in( child, search_list,
2698 parent, signature_is_good,
2699 *parent_is_trusted,
2700 path_cnt, self_cnt, rs_ctx );
Manuel Pégourié-Gonnard63686122017-07-04 01:01:39 +02002701
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002702#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002703 if( rs_ctx != NULL && ret == MBEDTLS_ERR_ECP_IN_PROGRESS )
2704 {
2705 /* save state */
2706 rs_ctx->parent_is_trusted = *parent_is_trusted;
Manuel Pégourié-Gonnard18547b52017-08-14 16:11:43 +02002707 return( ret );
2708 }
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002709#else
2710 (void) ret;
2711#endif
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002712
Manuel Pégourié-Gonnard18547b52017-08-14 16:11:43 +02002713 /* stop here if found or already in second iteration */
2714 if( *parent != NULL || *parent_is_trusted == 0 )
2715 break;
Manuel Pégourié-Gonnard63686122017-07-04 01:01:39 +02002716
Manuel Pégourié-Gonnard18547b52017-08-14 16:11:43 +02002717 /* prepare second iteration */
2718 *parent_is_trusted = 0;
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002719 }
Manuel Pégourié-Gonnard18547b52017-08-14 16:11:43 +02002720
2721 /* extra precaution against mistakes in the caller */
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05002722 if( *parent == NULL )
Manuel Pégourié-Gonnard18547b52017-08-14 16:11:43 +02002723 {
Manuel Pégourié-Gonnarda5a3e402018-10-16 11:27:23 +02002724 *parent_is_trusted = 0;
2725 *signature_is_good = 0;
Manuel Pégourié-Gonnard18547b52017-08-14 16:11:43 +02002726 }
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002727
2728 return( 0 );
Manuel Pégourié-Gonnard63686122017-07-04 01:01:39 +02002729}
2730
2731/*
Manuel Pégourié-Gonnard27e94792017-07-04 00:49:31 +02002732 * Check if an end-entity certificate is locally trusted
2733 *
2734 * Currently we require such certificates to be self-signed (actually only
2735 * check for self-issued as self-signatures are not checked)
2736 */
2737static int x509_crt_check_ee_locally_trusted(
2738 mbedtls_x509_crt *crt,
2739 mbedtls_x509_crt *trust_ca )
2740{
2741 mbedtls_x509_crt *cur;
2742
2743 /* must be self-issued */
2744 if( x509_name_cmp( &crt->issuer, &crt->subject ) != 0 )
2745 return( -1 );
2746
2747 /* look for an exact match with trusted cert */
2748 for( cur = trust_ca; cur != NULL; cur = cur->next )
2749 {
2750 if( crt->raw.len == cur->raw.len &&
2751 memcmp( crt->raw.p, cur->raw.p, crt->raw.len ) == 0 )
2752 {
2753 return( 0 );
2754 }
2755 }
2756
2757 /* too bad */
2758 return( -1 );
2759}
2760
2761/*
Manuel Pégourié-Gonnardf86f4912017-07-05 16:43:44 +02002762 * Build and verify a certificate chain
Manuel Pégourié-Gonnard35407c72017-06-29 10:45:25 +02002763 *
Manuel Pégourié-Gonnardf86f4912017-07-05 16:43:44 +02002764 * Given a peer-provided list of certificates EE, C1, ..., Cn and
2765 * a list of trusted certs R1, ... Rp, try to build and verify a chain
Manuel Pégourié-Gonnard562df402017-08-08 18:09:14 +02002766 * EE, Ci1, ... Ciq [, Rj]
Manuel Pégourié-Gonnardf86f4912017-07-05 16:43:44 +02002767 * such that every cert in the chain is a child of the next one,
2768 * jumping to a trusted root as early as possible.
Manuel Pégourié-Gonnardbdc54402017-07-04 00:33:39 +02002769 *
Manuel Pégourié-Gonnardf86f4912017-07-05 16:43:44 +02002770 * Verify that chain and return it with flags for all issues found.
2771 *
2772 * Special cases:
2773 * - EE == Rj -> return a one-element list containing it
2774 * - EE, Ci1, ..., Ciq cannot be continued with a trusted root
2775 * -> return that chain with NOT_TRUSTED set on Ciq
Manuel Pégourié-Gonnardbdc54402017-07-04 00:33:39 +02002776 *
Manuel Pégourié-Gonnardd19a41d2017-07-14 11:05:59 +02002777 * Tests for (aspects of) this function should include at least:
2778 * - trusted EE
2779 * - EE -> trusted root
Antonin Décimo36e89b52019-01-23 15:24:37 +01002780 * - EE -> intermediate CA -> trusted root
Manuel Pégourié-Gonnardd19a41d2017-07-14 11:05:59 +02002781 * - if relevant: EE untrusted
2782 * - if relevant: EE -> intermediate, untrusted
2783 * with the aspect under test checked at each relevant level (EE, int, root).
2784 * For some aspects longer chains are required, but usually length 2 is
2785 * enough (but length 1 is not in general).
2786 *
Manuel Pégourié-Gonnardbdc54402017-07-04 00:33:39 +02002787 * Arguments:
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002788 * - [in] crt: the cert list EE, C1, ..., Cn
2789 * - [in] trust_ca: the trusted list R1, ..., Rp
2790 * - [in] ca_crl, profile: as in verify_with_profile()
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02002791 * - [out] ver_chain: the built and verified chain
Manuel Pégourié-Gonnarda9688432017-08-23 11:23:59 +02002792 * Only valid when return value is 0, may contain garbage otherwise!
2793 * Restart note: need not be the same when calling again to resume.
Manuel Pégourié-Gonnard98a67782017-08-17 10:52:20 +02002794 * - [in-out] rs_ctx: context for restarting operations
Manuel Pégourié-Gonnardf86f4912017-07-05 16:43:44 +02002795 *
2796 * Return value:
2797 * - non-zero if the chain could not be fully built and examined
2798 * - 0 is the chain was successfully built and examined,
2799 * even if it was found to be invalid
Manuel Pégourié-Gonnard35407c72017-06-29 10:45:25 +02002800 */
Manuel Pégourié-Gonnardbdc54402017-07-04 00:33:39 +02002801static int x509_crt_verify_chain(
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002802 mbedtls_x509_crt *crt,
2803 mbedtls_x509_crt *trust_ca,
2804 mbedtls_x509_crl *ca_crl,
Hanno Becker3116fb32019-03-28 13:34:42 +00002805 mbedtls_x509_crt_ca_cb_t f_ca_cb,
2806 void *p_ca_cb,
Manuel Pégourié-Gonnard95051642015-06-15 10:39:46 +02002807 const mbedtls_x509_crt_profile *profile,
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02002808 mbedtls_x509_crt_verify_chain *ver_chain,
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002809 mbedtls_x509_crt_restart_ctx *rs_ctx )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002810{
Manuel Pégourié-Gonnarda9688432017-08-23 11:23:59 +02002811 /* Don't initialize any of those variables here, so that the compiler can
2812 * catch potential issues with jumping ahead when restarting */
Janos Follath865b3eb2019-12-16 11:46:15 +00002813 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardf86f4912017-07-05 16:43:44 +02002814 uint32_t *flags;
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02002815 mbedtls_x509_crt_verify_chain_item *cur;
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002816 mbedtls_x509_crt *child;
Manuel Pégourié-Gonnard58dcd2d2017-07-03 21:35:04 +02002817 mbedtls_x509_crt *parent;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002818 int parent_is_trusted;
2819 int child_is_trusted;
2820 int signature_is_good;
Manuel Pégourié-Gonnardbb216bd2017-08-28 13:25:55 +02002821 unsigned self_cnt;
Hanno Beckerf53893b2019-03-28 13:45:55 +00002822 mbedtls_x509_crt *cur_trust_ca = NULL;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002823
2824#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
2825 /* resume if we had an operation in progress */
Manuel Pégourié-Gonnarddaf04912017-08-23 12:32:19 +02002826 if( rs_ctx != NULL && rs_ctx->in_progress == x509_crt_rs_find_parent )
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002827 {
Manuel Pégourié-Gonnard3627a8b2017-08-23 11:20:48 +02002828 /* restore saved state */
Manuel Pégourié-Gonnarda9688432017-08-23 11:23:59 +02002829 *ver_chain = rs_ctx->ver_chain; /* struct copy */
Manuel Pégourié-Gonnarddaf04912017-08-23 12:32:19 +02002830 self_cnt = rs_ctx->self_cnt;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002831
Manuel Pégourié-Gonnarddaf04912017-08-23 12:32:19 +02002832 /* restore derived state */
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002833 cur = &ver_chain->items[ver_chain->len - 1];
Manuel Pégourié-Gonnarddaf04912017-08-23 12:32:19 +02002834 child = cur->crt;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002835 flags = &cur->flags;
2836
2837 goto find_parent;
2838 }
2839#endif /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */
Manuel Pégourié-Gonnard8f8c2822017-07-03 21:25:10 +02002840
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002841 child = crt;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002842 self_cnt = 0;
2843 parent_is_trusted = 0;
2844 child_is_trusted = 0;
Manuel Pégourié-Gonnardf86f4912017-07-05 16:43:44 +02002845
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002846 while( 1 ) {
2847 /* Add certificate to the verification chain */
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02002848 cur = &ver_chain->items[ver_chain->len];
2849 cur->crt = child;
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +02002850 cur->flags = 0;
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02002851 ver_chain->len++;
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +02002852 flags = &cur->flags;
Manuel Pégourié-Gonnard66fac752017-07-03 21:39:21 +02002853
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002854 /* Check time-validity (all certificates) */
2855 if( mbedtls_x509_time_is_past( &child->valid_to ) )
2856 *flags |= MBEDTLS_X509_BADCERT_EXPIRED;
Manuel Pégourié-Gonnard66fac752017-07-03 21:39:21 +02002857
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002858 if( mbedtls_x509_time_is_future( &child->valid_from ) )
2859 *flags |= MBEDTLS_X509_BADCERT_FUTURE;
Manuel Pégourié-Gonnardcb396102017-07-04 00:00:24 +02002860
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002861 /* Stop here for trusted roots (but not for trusted EE certs) */
2862 if( child_is_trusted )
2863 return( 0 );
Manuel Pégourié-Gonnard66fac752017-07-03 21:39:21 +02002864
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002865 /* Check signature algorithm: MD & PK algs */
2866 if( x509_profile_check_md_alg( profile, child->sig_md ) != 0 )
2867 *flags |= MBEDTLS_X509_BADCERT_BAD_MD;
Manuel Pégourié-Gonnard66fac752017-07-03 21:39:21 +02002868
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002869 if( x509_profile_check_pk_alg( profile, child->sig_pk ) != 0 )
2870 *flags |= MBEDTLS_X509_BADCERT_BAD_PK;
Manuel Pégourié-Gonnard27e94792017-07-04 00:49:31 +02002871
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002872 /* Special case: EE certs that are locally trusted */
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02002873 if( ver_chain->len == 1 &&
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002874 x509_crt_check_ee_locally_trusted( child, trust_ca ) == 0 )
2875 {
2876 return( 0 );
2877 }
Manuel Pégourié-Gonnard8f8c2822017-07-03 21:25:10 +02002878
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002879#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
2880find_parent:
2881#endif
Hanno Beckerf53893b2019-03-28 13:45:55 +00002882
2883 /* Obtain list of potential trusted signers from CA callback,
2884 * or use statically provided list. */
2885#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
2886 if( f_ca_cb != NULL )
2887 {
2888 mbedtls_x509_crt_free( ver_chain->trust_ca_cb_result );
2889 mbedtls_free( ver_chain->trust_ca_cb_result );
2890 ver_chain->trust_ca_cb_result = NULL;
2891
2892 ret = f_ca_cb( p_ca_cb, child, &ver_chain->trust_ca_cb_result );
2893 if( ret != 0 )
2894 return( MBEDTLS_ERR_X509_FATAL_ERROR );
2895
2896 cur_trust_ca = ver_chain->trust_ca_cb_result;
2897 }
2898 else
2899#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
2900 {
2901 ((void) f_ca_cb);
2902 ((void) p_ca_cb);
2903 cur_trust_ca = trust_ca;
2904 }
2905
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002906 /* Look for a parent in trusted CAs or up the chain */
Hanno Beckerf53893b2019-03-28 13:45:55 +00002907 ret = x509_crt_find_parent( child, cur_trust_ca, &parent,
Manuel Pégourié-Gonnardbe4ff422017-07-14 12:04:14 +02002908 &parent_is_trusted, &signature_is_good,
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02002909 ver_chain->len - 1, self_cnt, rs_ctx );
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002910
2911#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002912 if( rs_ctx != NULL && ret == MBEDTLS_ERR_ECP_IN_PROGRESS )
2913 {
2914 /* save state */
Manuel Pégourié-Gonnarddaf04912017-08-23 12:32:19 +02002915 rs_ctx->in_progress = x509_crt_rs_find_parent;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002916 rs_ctx->self_cnt = self_cnt;
Manuel Pégourié-Gonnarda9688432017-08-23 11:23:59 +02002917 rs_ctx->ver_chain = *ver_chain; /* struct copy */
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002918
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002919 return( ret );
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002920 }
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002921#else
2922 (void) ret;
2923#endif
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002924
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002925 /* No parent? We're done here */
2926 if( parent == NULL )
2927 {
2928 *flags |= MBEDTLS_X509_BADCERT_NOT_TRUSTED;
2929 return( 0 );
2930 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002931
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002932 /* Count intermediate self-issued (not necessarily self-signed) certs.
2933 * These can occur with some strategies for key rollover, see [SIRO],
2934 * and should be excluded from max_pathlen checks. */
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02002935 if( ver_chain->len != 1 &&
Manuel Pégourié-Gonnard24611f92017-08-09 10:28:07 +02002936 x509_name_cmp( &child->issuer, &child->subject ) == 0 )
2937 {
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002938 self_cnt++;
Manuel Pégourié-Gonnard24611f92017-08-09 10:28:07 +02002939 }
Manuel Pégourié-Gonnardfd6c85c2014-11-20 16:34:20 +01002940
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002941 /* path_cnt is 0 for the first intermediate CA,
2942 * and if parent is trusted it's not an intermediate CA */
2943 if( ! parent_is_trusted &&
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02002944 ver_chain->len > MBEDTLS_X509_MAX_INTERMEDIATE_CA )
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002945 {
2946 /* return immediately to avoid overflow the chain array */
2947 return( MBEDTLS_ERR_X509_FATAL_ERROR );
2948 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002949
Manuel Pégourié-Gonnard98a67782017-08-17 10:52:20 +02002950 /* signature was checked while searching parent */
Manuel Pégourié-Gonnardbe4ff422017-07-14 12:04:14 +02002951 if( ! signature_is_good )
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002952 *flags |= MBEDTLS_X509_BADCERT_NOT_TRUSTED;
2953
2954 /* check size of signing key */
Manuel Pégourié-Gonnard3f816912017-10-26 10:24:16 +02002955 if( x509_profile_check_key( profile, &parent->pk ) != 0 )
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002956 *flags |= MBEDTLS_X509_BADCERT_BAD_KEY;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002957
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002958#if defined(MBEDTLS_X509_CRL_PARSE_C)
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002959 /* Check trusted CA's CRL for the given crt */
Manuel Pégourié-Gonnard562df402017-08-08 18:09:14 +02002960 *flags |= x509_crt_verifycrl( child, parent, ca_crl, profile );
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002961#else
2962 (void) ca_crl;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002963#endif
2964
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002965 /* prepare for next iteration */
2966 child = parent;
2967 parent = NULL;
2968 child_is_trusted = parent_is_trusted;
Manuel Pégourié-Gonnardbe4ff422017-07-14 12:04:14 +02002969 signature_is_good = 0;
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002970 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002971}
2972
2973/*
Manuel Pégourié-Gonnarda468eb12017-07-04 01:31:59 +02002974 * Check for CN match
2975 */
2976static int x509_crt_check_cn( const mbedtls_x509_buf *name,
2977 const char *cn, size_t cn_len )
2978{
2979 /* try exact match */
2980 if( name->len == cn_len &&
2981 x509_memcasecmp( cn, name->p, cn_len ) == 0 )
2982 {
2983 return( 0 );
2984 }
2985
2986 /* try wildcard match */
Manuel Pégourié-Gonnard900fba62017-10-18 14:28:11 +02002987 if( x509_check_wildcard( cn, name ) == 0 )
Manuel Pégourié-Gonnarda468eb12017-07-04 01:31:59 +02002988 {
2989 return( 0 );
2990 }
2991
2992 return( -1 );
2993}
2994
2995/*
Manuel Pégourié-Gonnardf3e4bd82020-07-21 13:22:41 +02002996 * Check for SAN match, see RFC 5280 Section 4.2.1.6
2997 */
2998static int x509_crt_check_san( const mbedtls_x509_buf *name,
2999 const char *cn, size_t cn_len )
3000{
3001 const unsigned char san_type = (unsigned char) name->tag &
3002 MBEDTLS_ASN1_TAG_VALUE_MASK;
3003
3004 /* dNSName */
3005 if( san_type == MBEDTLS_X509_SAN_DNS_NAME )
3006 return( x509_crt_check_cn( name, cn, cn_len ) );
3007
3008 /* (We may handle other types here later.) */
3009
3010 /* Unrecognized type */
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é-Gonnardf3e4bd82020-07-21 13:22:41 +02003029 if( x509_crt_check_san( &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 */