blob: 42f1fc22c5b0b071bfea30fd4dd071119a05191e [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
Daniel Axtens301db662020-05-28 11:43:41 +100066#if defined(MBEDTLS_HAVE_TIME)
Paul Bakkerfa6a6202013-10-28 18:48:30 +010067#if defined(_WIN32) && !defined(EFIX64) && !defined(EFI32)
Paul Bakker7c6b2c32013-09-16 13:49:26 +020068#include <windows.h>
69#else
70#include <time.h>
71#endif
Daniel Axtens301db662020-05-28 11:43:41 +100072#endif
Paul Bakker7c6b2c32013-09-16 13:49:26 +020073
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020074#if defined(MBEDTLS_FS_IO)
Rich Evans00ab4702015-02-06 13:43:58 +000075#include <stdio.h>
Paul Bakker5ff3f912014-04-04 15:08:20 +020076#if !defined(_WIN32) || defined(EFIX64) || defined(EFI32)
Paul Bakker7c6b2c32013-09-16 13:49:26 +020077#include <sys/types.h>
78#include <sys/stat.h>
79#include <dirent.h>
Eduardo Silva32ffb2b2019-04-25 10:43:26 -060080#include <errno.h>
Rich Evans00ab4702015-02-06 13:43:58 +000081#endif /* !_WIN32 || EFIX64 || EFI32 */
Paul Bakker7c6b2c32013-09-16 13:49:26 +020082#endif
83
Manuel Pégourié-Gonnardc547d1a2017-07-05 13:28:45 +020084/*
85 * Item in a verification chain: cert and flags for it
86 */
87typedef struct {
88 mbedtls_x509_crt *crt;
89 uint32_t flags;
90} x509_crt_verify_chain_item;
91
92/*
93 * Max size of verification chain: end-entity + intermediates + trusted root
94 */
95#define X509_MAX_VERIFY_CHAIN_SIZE ( MBEDTLS_X509_MAX_INTERMEDIATE_CA + 2 )
Paul Bakker34617722014-06-13 17:20:13 +020096
Gilles Peskine0ecd7192021-06-07 21:24:26 +020097/* Default profile. Do not remove items unless there are serious security
98 * concerns. */
Manuel Pégourié-Gonnard88db5da2015-06-15 14:34:59 +020099const mbedtls_x509_crt_profile mbedtls_x509_crt_profile_default =
100{
Gilles Peskine5e79cb32017-05-04 16:17:21 +0200101 /* Only SHA-2 hashes */
Manuel Pégourié-Gonnardf8ea8562015-06-15 15:33:19 +0200102 MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_SHA224 ) |
103 MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_SHA256 ) |
104 MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_SHA384 ) |
105 MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_SHA512 ),
106 0xFFFFFFF, /* Any PK alg */
107 0xFFFFFFF, /* Any curve */
Manuel Pégourié-Gonnard88db5da2015-06-15 14:34:59 +0200108 2048,
109};
110
111/*
112 * Next-default profile
113 */
Manuel Pégourié-Gonnard88db5da2015-06-15 14:34:59 +0200114const mbedtls_x509_crt_profile mbedtls_x509_crt_profile_next =
115{
Manuel Pégourié-Gonnardf8ea8562015-06-15 15:33:19 +0200116 /* Hashes from SHA-256 and above */
117 MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_SHA256 ) |
118 MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_SHA384 ) |
119 MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_SHA512 ),
120 0xFFFFFFF, /* Any PK alg */
121#if defined(MBEDTLS_ECP_C)
122 /* Curves at or above 128-bit security level */
123 MBEDTLS_X509_ID_FLAG( MBEDTLS_ECP_DP_SECP256R1 ) |
124 MBEDTLS_X509_ID_FLAG( MBEDTLS_ECP_DP_SECP384R1 ) |
125 MBEDTLS_X509_ID_FLAG( MBEDTLS_ECP_DP_SECP521R1 ) |
126 MBEDTLS_X509_ID_FLAG( MBEDTLS_ECP_DP_BP256R1 ) |
127 MBEDTLS_X509_ID_FLAG( MBEDTLS_ECP_DP_BP384R1 ) |
128 MBEDTLS_X509_ID_FLAG( MBEDTLS_ECP_DP_BP512R1 ) |
129 MBEDTLS_X509_ID_FLAG( MBEDTLS_ECP_DP_SECP256K1 ),
130#else
131 0,
132#endif
Manuel Pégourié-Gonnard88db5da2015-06-15 14:34:59 +0200133 2048,
134};
135
136/*
137 * NSA Suite B Profile
138 */
Manuel Pégourié-Gonnard88db5da2015-06-15 14:34:59 +0200139const mbedtls_x509_crt_profile mbedtls_x509_crt_profile_suiteb =
140{
Manuel Pégourié-Gonnardf8ea8562015-06-15 15:33:19 +0200141 /* Only SHA-256 and 384 */
142 MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_SHA256 ) |
143 MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_SHA384 ),
144 /* Only ECDSA */
Ron Eldor85e1dcf2018-02-06 15:59:38 +0200145 MBEDTLS_X509_ID_FLAG( MBEDTLS_PK_ECDSA ) |
146 MBEDTLS_X509_ID_FLAG( MBEDTLS_PK_ECKEY ),
Manuel Pégourié-Gonnardf8ea8562015-06-15 15:33:19 +0200147#if defined(MBEDTLS_ECP_C)
148 /* Only NIST P-256 and P-384 */
149 MBEDTLS_X509_ID_FLAG( MBEDTLS_ECP_DP_SECP256R1 ) |
150 MBEDTLS_X509_ID_FLAG( MBEDTLS_ECP_DP_SECP384R1 ),
151#else
152 0,
153#endif
154 0,
Manuel Pégourié-Gonnard88db5da2015-06-15 14:34:59 +0200155};
156
157/*
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200158 * Check md_alg against profile
Manuel Pégourié-Gonnard3f816912017-10-26 10:24:16 +0200159 * Return 0 if md_alg is acceptable for this profile, -1 otherwise
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200160 */
161static int x509_profile_check_md_alg( const mbedtls_x509_crt_profile *profile,
162 mbedtls_md_type_t md_alg )
163{
Philippe Antoineb5b25432018-05-11 11:06:29 +0200164 if( md_alg == MBEDTLS_MD_NONE )
165 return( -1 );
166
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200167 if( ( profile->allowed_mds & MBEDTLS_X509_ID_FLAG( md_alg ) ) != 0 )
168 return( 0 );
169
170 return( -1 );
171}
172
173/*
174 * Check pk_alg against profile
Manuel Pégourié-Gonnard3f816912017-10-26 10:24:16 +0200175 * Return 0 if pk_alg is acceptable for this profile, -1 otherwise
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200176 */
177static int x509_profile_check_pk_alg( const mbedtls_x509_crt_profile *profile,
178 mbedtls_pk_type_t pk_alg )
179{
Philippe Antoineb5b25432018-05-11 11:06:29 +0200180 if( pk_alg == MBEDTLS_PK_NONE )
181 return( -1 );
182
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200183 if( ( profile->allowed_pks & MBEDTLS_X509_ID_FLAG( pk_alg ) ) != 0 )
184 return( 0 );
185
186 return( -1 );
187}
188
189/*
190 * Check key against profile
Manuel Pégourié-Gonnard3f816912017-10-26 10:24:16 +0200191 * Return 0 if pk is acceptable for this profile, -1 otherwise
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200192 */
193static int x509_profile_check_key( const mbedtls_x509_crt_profile *profile,
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200194 const mbedtls_pk_context *pk )
195{
Manuel Pégourié-Gonnard3f816912017-10-26 10:24:16 +0200196 const mbedtls_pk_type_t pk_alg = mbedtls_pk_get_type( pk );
Manuel Pégourié-Gonnard19773ff2017-10-24 10:51:26 +0200197
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200198#if defined(MBEDTLS_RSA_C)
199 if( pk_alg == MBEDTLS_PK_RSA || pk_alg == MBEDTLS_PK_RSASSA_PSS )
200 {
Manuel Pégourié-Gonnard097c7bb2015-06-18 16:43:38 +0200201 if( mbedtls_pk_get_bitlen( pk ) >= profile->rsa_min_bitlen )
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200202 return( 0 );
203
204 return( -1 );
205 }
206#endif
207
Manuel Pégourié-Gonnard65eefc82015-10-23 14:08:48 +0200208#if defined(MBEDTLS_ECP_C)
209 if( pk_alg == MBEDTLS_PK_ECDSA ||
210 pk_alg == MBEDTLS_PK_ECKEY ||
211 pk_alg == MBEDTLS_PK_ECKEY_DH )
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200212 {
Manuel Pégourié-Gonnard3f816912017-10-26 10:24:16 +0200213 const mbedtls_ecp_group_id gid = mbedtls_pk_ec( *pk )->grp.id;
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200214
Philippe Antoineb5b25432018-05-11 11:06:29 +0200215 if( gid == MBEDTLS_ECP_DP_NONE )
216 return( -1 );
217
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200218 if( ( profile->allowed_curves & MBEDTLS_X509_ID_FLAG( gid ) ) != 0 )
219 return( 0 );
220
221 return( -1 );
222 }
223#endif
224
225 return( -1 );
226}
227
228/*
Hanno Becker1f8527f2018-11-02 09:19:16 +0000229 * Like memcmp, but case-insensitive and always returns -1 if different
230 */
231static int x509_memcasecmp( const void *s1, const void *s2, size_t len )
232{
233 size_t i;
234 unsigned char diff;
235 const unsigned char *n1 = s1, *n2 = s2;
236
237 for( i = 0; i < len; i++ )
238 {
239 diff = n1[i] ^ n2[i];
240
241 if( diff == 0 )
242 continue;
243
244 if( diff == 32 &&
245 ( ( n1[i] >= 'a' && n1[i] <= 'z' ) ||
246 ( n1[i] >= 'A' && n1[i] <= 'Z' ) ) )
247 {
248 continue;
249 }
250
251 return( -1 );
252 }
253
254 return( 0 );
255}
256
257/*
258 * Return 0 if name matches wildcard, -1 otherwise
259 */
260static int x509_check_wildcard( const char *cn, const mbedtls_x509_buf *name )
261{
262 size_t i;
263 size_t cn_idx = 0, cn_len = strlen( cn );
264
265 /* We can't have a match if there is no wildcard to match */
266 if( name->len < 3 || name->p[0] != '*' || name->p[1] != '.' )
267 return( -1 );
268
269 for( i = 0; i < cn_len; ++i )
270 {
271 if( cn[i] == '.' )
272 {
273 cn_idx = i;
274 break;
275 }
276 }
277
278 if( cn_idx == 0 )
279 return( -1 );
280
281 if( cn_len - cn_idx == name->len - 1 &&
282 x509_memcasecmp( name->p + 1, cn + cn_idx, name->len - 1 ) == 0 )
283 {
284 return( 0 );
285 }
286
287 return( -1 );
288}
289
290/*
291 * Compare two X.509 strings, case-insensitive, and allowing for some encoding
292 * variations (but not all).
293 *
294 * Return 0 if equal, -1 otherwise.
295 */
296static int x509_string_cmp( const mbedtls_x509_buf *a, const mbedtls_x509_buf *b )
297{
298 if( a->tag == b->tag &&
299 a->len == b->len &&
300 memcmp( a->p, b->p, b->len ) == 0 )
301 {
302 return( 0 );
303 }
304
305 if( ( a->tag == MBEDTLS_ASN1_UTF8_STRING || a->tag == MBEDTLS_ASN1_PRINTABLE_STRING ) &&
306 ( b->tag == MBEDTLS_ASN1_UTF8_STRING || b->tag == MBEDTLS_ASN1_PRINTABLE_STRING ) &&
307 a->len == b->len &&
308 x509_memcasecmp( a->p, b->p, b->len ) == 0 )
309 {
310 return( 0 );
311 }
312
313 return( -1 );
314}
315
316/*
317 * Compare two X.509 Names (aka rdnSequence).
318 *
319 * See RFC 5280 section 7.1, though we don't implement the whole algorithm:
320 * we sometimes return unequal when the full algorithm would return equal,
321 * but never the other way. (In particular, we don't do Unicode normalisation
322 * or space folding.)
323 *
324 * Return 0 if equal, -1 otherwise.
325 */
326static int x509_name_cmp( const mbedtls_x509_name *a, const mbedtls_x509_name *b )
327{
328 /* Avoid recursion, it might not be optimised by the compiler */
329 while( a != NULL || b != NULL )
330 {
331 if( a == NULL || b == NULL )
332 return( -1 );
333
334 /* type */
335 if( a->oid.tag != b->oid.tag ||
336 a->oid.len != b->oid.len ||
337 memcmp( a->oid.p, b->oid.p, b->oid.len ) != 0 )
338 {
339 return( -1 );
340 }
341
342 /* value */
343 if( x509_string_cmp( &a->val, &b->val ) != 0 )
344 return( -1 );
345
346 /* structure of the list of sets */
347 if( a->next_merged != b->next_merged )
348 return( -1 );
349
350 a = a->next;
351 b = b->next;
352 }
353
354 /* a == NULL == b */
355 return( 0 );
356}
357
358/*
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +0200359 * Reset (init or clear) a verify_chain
360 */
361static void x509_crt_verify_chain_reset(
362 mbedtls_x509_crt_verify_chain *ver_chain )
363{
364 size_t i;
365
366 for( i = 0; i < MBEDTLS_X509_MAX_VERIFY_CHAIN_SIZE; i++ )
367 {
368 ver_chain->items[i].crt = NULL;
Hanno Beckera9375b32019-01-10 09:19:26 +0000369 ver_chain->items[i].flags = (uint32_t) -1;
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +0200370 }
371
372 ver_chain->len = 0;
Hanno Beckerf53893b2019-03-28 13:45:55 +0000373
374#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
375 ver_chain->trust_ca_cb_result = NULL;
376#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +0200377}
378
379/*
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200380 * Version ::= INTEGER { v1(0), v2(1), v3(2) }
381 */
382static int x509_get_version( unsigned char **p,
383 const unsigned char *end,
384 int *ver )
385{
Janos Follath865b3eb2019-12-16 11:46:15 +0000386 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200387 size_t len;
388
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200389 if( ( ret = mbedtls_asn1_get_tag( p, end, &len,
390 MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_CONSTRUCTED | 0 ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200391 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200392 if( ret == MBEDTLS_ERR_ASN1_UNEXPECTED_TAG )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200393 {
394 *ver = 0;
395 return( 0 );
396 }
397
Chris Jones9f7a6932021-04-14 12:12:09 +0100398 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_FORMAT, ret ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200399 }
400
401 end = *p + len;
402
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200403 if( ( ret = mbedtls_asn1_get_int( p, end, ver ) ) != 0 )
Chris Jones9f7a6932021-04-14 12:12:09 +0100404 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_VERSION, ret ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200405
406 if( *p != end )
Chris Jones9f7a6932021-04-14 12:12:09 +0100407 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_VERSION,
408 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200409
410 return( 0 );
411}
412
413/*
414 * Validity ::= SEQUENCE {
415 * notBefore Time,
416 * notAfter Time }
417 */
418static int x509_get_dates( unsigned char **p,
419 const unsigned char *end,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200420 mbedtls_x509_time *from,
421 mbedtls_x509_time *to )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200422{
Janos Follath865b3eb2019-12-16 11:46:15 +0000423 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200424 size_t len;
425
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200426 if( ( ret = mbedtls_asn1_get_tag( p, end, &len,
427 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
Chris Jones9f7a6932021-04-14 12:12:09 +0100428 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_DATE, ret ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200429
430 end = *p + len;
431
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200432 if( ( ret = mbedtls_x509_get_time( p, end, from ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200433 return( ret );
434
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200435 if( ( ret = mbedtls_x509_get_time( p, end, to ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200436 return( ret );
437
438 if( *p != end )
Chris Jones9f7a6932021-04-14 12:12:09 +0100439 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_DATE,
440 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200441
442 return( 0 );
443}
444
445/*
446 * X.509 v2/v3 unique identifier (not parsed)
447 */
448static int x509_get_uid( unsigned char **p,
449 const unsigned char *end,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200450 mbedtls_x509_buf *uid, int n )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200451{
Janos Follath865b3eb2019-12-16 11:46:15 +0000452 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200453
454 if( *p == end )
455 return( 0 );
456
457 uid->tag = **p;
458
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200459 if( ( ret = mbedtls_asn1_get_tag( p, end, &uid->len,
460 MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_CONSTRUCTED | n ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200461 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200462 if( ret == MBEDTLS_ERR_ASN1_UNEXPECTED_TAG )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200463 return( 0 );
464
Chris Jones9f7a6932021-04-14 12:12:09 +0100465 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_FORMAT, ret ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200466 }
467
468 uid->p = *p;
469 *p += uid->len;
470
471 return( 0 );
472}
473
474static int x509_get_basic_constraints( unsigned char **p,
475 const unsigned char *end,
476 int *ca_istrue,
477 int *max_pathlen )
478{
Janos Follath865b3eb2019-12-16 11:46:15 +0000479 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200480 size_t len;
481
482 /*
483 * BasicConstraints ::= SEQUENCE {
484 * cA BOOLEAN DEFAULT FALSE,
485 * pathLenConstraint INTEGER (0..MAX) OPTIONAL }
486 */
487 *ca_istrue = 0; /* DEFAULT FALSE */
488 *max_pathlen = 0; /* endless */
489
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200490 if( ( ret = mbedtls_asn1_get_tag( p, end, &len,
491 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
Chris Jones9f7a6932021-04-14 12:12:09 +0100492 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200493
494 if( *p == end )
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200495 return( 0 );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200496
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200497 if( ( ret = mbedtls_asn1_get_bool( p, end, ca_istrue ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200498 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200499 if( ret == MBEDTLS_ERR_ASN1_UNEXPECTED_TAG )
500 ret = mbedtls_asn1_get_int( p, end, ca_istrue );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200501
502 if( ret != 0 )
Chris Jones9f7a6932021-04-14 12:12:09 +0100503 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200504
505 if( *ca_istrue != 0 )
506 *ca_istrue = 1;
507 }
508
509 if( *p == end )
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200510 return( 0 );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200511
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200512 if( ( ret = mbedtls_asn1_get_int( p, end, max_pathlen ) ) != 0 )
Chris Jones9f7a6932021-04-14 12:12:09 +0100513 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200514
515 if( *p != end )
Chris Jones9f7a6932021-04-14 12:12:09 +0100516 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
517 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200518
Andrzej Kurek16050742020-04-14 09:49:52 -0400519 /* Do not accept max_pathlen equal to INT_MAX to avoid a signed integer
520 * overflow, which is an undefined behavior. */
521 if( *max_pathlen == INT_MAX )
Chris Jones9f7a6932021-04-14 12:12:09 +0100522 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
523 MBEDTLS_ERR_ASN1_INVALID_LENGTH ) );
Andrzej Kurek16050742020-04-14 09:49:52 -0400524
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200525 (*max_pathlen)++;
526
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200527 return( 0 );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200528}
529
530static int x509_get_ns_cert_type( unsigned char **p,
531 const unsigned char *end,
532 unsigned char *ns_cert_type)
533{
Janos Follath865b3eb2019-12-16 11:46:15 +0000534 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200535 mbedtls_x509_bitstring bs = { 0, 0, NULL };
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200536
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200537 if( ( ret = mbedtls_asn1_get_bitstring( p, end, &bs ) ) != 0 )
Chris Jones9f7a6932021-04-14 12:12:09 +0100538 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200539
540 if( bs.len != 1 )
Chris Jones9f7a6932021-04-14 12:12:09 +0100541 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
542 MBEDTLS_ERR_ASN1_INVALID_LENGTH ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200543
544 /* Get actual bitstring */
545 *ns_cert_type = *bs.p;
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200546 return( 0 );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200547}
548
549static int x509_get_key_usage( unsigned char **p,
550 const unsigned char *end,
Manuel Pégourié-Gonnard1d0ca1a2015-03-27 16:50:00 +0100551 unsigned int *key_usage)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200552{
Janos Follath865b3eb2019-12-16 11:46:15 +0000553 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard9a702252015-06-23 10:14:36 +0200554 size_t i;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200555 mbedtls_x509_bitstring bs = { 0, 0, NULL };
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200556
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200557 if( ( ret = mbedtls_asn1_get_bitstring( p, end, &bs ) ) != 0 )
Chris Jones9f7a6932021-04-14 12:12:09 +0100558 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200559
560 if( bs.len < 1 )
Chris Jones9f7a6932021-04-14 12:12:09 +0100561 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
562 MBEDTLS_ERR_ASN1_INVALID_LENGTH ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200563
564 /* Get actual bitstring */
Manuel Pégourié-Gonnard9a702252015-06-23 10:14:36 +0200565 *key_usage = 0;
566 for( i = 0; i < bs.len && i < sizeof( unsigned int ); i++ )
567 {
568 *key_usage |= (unsigned int) bs.p[i] << (8*i);
569 }
570
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200571 return( 0 );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200572}
573
574/*
575 * ExtKeyUsageSyntax ::= SEQUENCE SIZE (1..MAX) OF KeyPurposeId
576 *
577 * KeyPurposeId ::= OBJECT IDENTIFIER
578 */
579static int x509_get_ext_key_usage( unsigned char **p,
580 const unsigned char *end,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200581 mbedtls_x509_sequence *ext_key_usage)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200582{
Janos Follath865b3eb2019-12-16 11:46:15 +0000583 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200584
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200585 if( ( ret = mbedtls_asn1_get_sequence_of( p, end, ext_key_usage, MBEDTLS_ASN1_OID ) ) != 0 )
Chris Jones9f7a6932021-04-14 12:12:09 +0100586 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200587
588 /* Sequence length must be >= 1 */
589 if( ext_key_usage->buf.p == NULL )
Chris Jones9f7a6932021-04-14 12:12:09 +0100590 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
591 MBEDTLS_ERR_ASN1_INVALID_LENGTH ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200592
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200593 return( 0 );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200594}
595
596/*
597 * SubjectAltName ::= GeneralNames
598 *
599 * GeneralNames ::= SEQUENCE SIZE (1..MAX) OF GeneralName
600 *
601 * GeneralName ::= CHOICE {
602 * otherName [0] OtherName,
603 * rfc822Name [1] IA5String,
604 * dNSName [2] IA5String,
605 * x400Address [3] ORAddress,
606 * directoryName [4] Name,
607 * ediPartyName [5] EDIPartyName,
608 * uniformResourceIdentifier [6] IA5String,
609 * iPAddress [7] OCTET STRING,
610 * registeredID [8] OBJECT IDENTIFIER }
611 *
612 * OtherName ::= SEQUENCE {
613 * type-id OBJECT IDENTIFIER,
614 * value [0] EXPLICIT ANY DEFINED BY type-id }
615 *
616 * EDIPartyName ::= SEQUENCE {
617 * nameAssigner [0] DirectoryString OPTIONAL,
618 * partyName [1] DirectoryString }
619 *
Ron Eldorc8b5f3f2019-05-15 15:15:55 +0300620 * NOTE: we list all types, but only use dNSName and otherName
621 * of type HwModuleName, as defined in RFC 4108, at this point.
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200622 */
623static int x509_get_subject_alt_name( unsigned char **p,
624 const unsigned char *end,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200625 mbedtls_x509_sequence *subject_alt_name )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200626{
Janos Follath865b3eb2019-12-16 11:46:15 +0000627 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200628 size_t len, tag_len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200629 mbedtls_asn1_buf *buf;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200630 unsigned char tag;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200631 mbedtls_asn1_sequence *cur = subject_alt_name;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200632
633 /* Get main sequence tag */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200634 if( ( ret = mbedtls_asn1_get_tag( p, end, &len,
635 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
Chris Jones9f7a6932021-04-14 12:12:09 +0100636 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200637
638 if( *p + len != end )
Chris Jones9f7a6932021-04-14 12:12:09 +0100639 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
640 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200641
642 while( *p < end )
643 {
Ron Eldordbbd9662019-05-15 15:46:03 +0300644 mbedtls_x509_subject_alternative_name dummy_san_buf;
645 memset( &dummy_san_buf, 0, sizeof( dummy_san_buf ) );
646
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200647 tag = **p;
648 (*p)++;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200649 if( ( ret = mbedtls_asn1_get_len( p, end, &tag_len ) ) != 0 )
Chris Jones9f7a6932021-04-14 12:12:09 +0100650 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200651
Andres Amaya Garcia849bc652017-08-25 17:13:12 +0100652 if( ( tag & MBEDTLS_ASN1_TAG_CLASS_MASK ) !=
653 MBEDTLS_ASN1_CONTEXT_SPECIFIC )
654 {
Chris Jones9f7a6932021-04-14 12:12:09 +0100655 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
656 MBEDTLS_ERR_ASN1_UNEXPECTED_TAG ) );
Andres Amaya Garcia849bc652017-08-25 17:13:12 +0100657 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200658
Ron Eldordbbd9662019-05-15 15:46:03 +0300659 /*
irwir74aee1c2019-09-21 18:21:48 +0300660 * Check that the SAN is structured correctly.
Ron Eldordbbd9662019-05-15 15:46:03 +0300661 */
662 ret = mbedtls_x509_parse_subject_alt_name( &(cur->buf), &dummy_san_buf );
663 /*
664 * In case the extension is malformed, return an error,
665 * and clear the allocated sequences.
666 */
667 if( ret != 0 && ret != MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE )
668 {
669 mbedtls_x509_sequence *seq_cur = subject_alt_name->next;
670 mbedtls_x509_sequence *seq_prv;
671 while( seq_cur != NULL )
672 {
673 seq_prv = seq_cur;
674 seq_cur = seq_cur->next;
675 mbedtls_platform_zeroize( seq_prv,
676 sizeof( mbedtls_x509_sequence ) );
677 mbedtls_free( seq_prv );
678 }
Ron Eldor5aebeeb2019-05-22 16:41:21 +0300679 subject_alt_name->next = NULL;
Ron Eldordbbd9662019-05-15 15:46:03 +0300680 return( ret );
681 }
682
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200683 /* Allocate and assign next pointer */
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +0200684 if( cur->buf.p != NULL )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200685 {
Manuel Pégourié-Gonnardb1340602014-11-11 23:11:16 +0100686 if( cur->next != NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200687 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS );
Manuel Pégourié-Gonnardb1340602014-11-11 23:11:16 +0100688
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200689 cur->next = mbedtls_calloc( 1, sizeof( mbedtls_asn1_sequence ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200690
691 if( cur->next == NULL )
Chris Jones9f7a6932021-04-14 12:12:09 +0100692 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
693 MBEDTLS_ERR_ASN1_ALLOC_FAILED ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200694
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200695 cur = cur->next;
696 }
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +0200697
698 buf = &(cur->buf);
699 buf->tag = tag;
700 buf->p = *p;
701 buf->len = tag_len;
702 *p += buf->len;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200703 }
704
705 /* Set final sequence entry's next pointer to NULL */
706 cur->next = NULL;
707
708 if( *p != end )
Chris Jones9f7a6932021-04-14 12:12:09 +0100709 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
710 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200711
712 return( 0 );
713}
714
715/*
Ron Eldor74d9acc2019-03-21 14:00:03 +0200716 * id-ce-certificatePolicies OBJECT IDENTIFIER ::= { id-ce 32 }
717 *
718 * anyPolicy OBJECT IDENTIFIER ::= { id-ce-certificatePolicies 0 }
719 *
720 * certificatePolicies ::= SEQUENCE SIZE (1..MAX) OF PolicyInformation
721 *
722 * PolicyInformation ::= SEQUENCE {
723 * policyIdentifier CertPolicyId,
724 * policyQualifiers SEQUENCE SIZE (1..MAX) OF
725 * PolicyQualifierInfo OPTIONAL }
726 *
727 * CertPolicyId ::= OBJECT IDENTIFIER
728 *
729 * PolicyQualifierInfo ::= SEQUENCE {
730 * policyQualifierId PolicyQualifierId,
731 * qualifier ANY DEFINED BY policyQualifierId }
732 *
733 * -- policyQualifierIds for Internet policy qualifiers
734 *
735 * id-qt OBJECT IDENTIFIER ::= { id-pkix 2 }
736 * id-qt-cps OBJECT IDENTIFIER ::= { id-qt 1 }
737 * id-qt-unotice OBJECT IDENTIFIER ::= { id-qt 2 }
738 *
739 * PolicyQualifierId ::= OBJECT IDENTIFIER ( id-qt-cps | id-qt-unotice )
740 *
741 * Qualifier ::= CHOICE {
742 * cPSuri CPSuri,
743 * userNotice UserNotice }
744 *
745 * CPSuri ::= IA5String
746 *
747 * UserNotice ::= SEQUENCE {
748 * noticeRef NoticeReference OPTIONAL,
749 * explicitText DisplayText OPTIONAL }
750 *
751 * NoticeReference ::= SEQUENCE {
752 * organization DisplayText,
753 * noticeNumbers SEQUENCE OF INTEGER }
754 *
755 * DisplayText ::= CHOICE {
756 * ia5String IA5String (SIZE (1..200)),
757 * visibleString VisibleString (SIZE (1..200)),
758 * bmpString BMPString (SIZE (1..200)),
759 * utf8String UTF8String (SIZE (1..200)) }
760 *
761 * NOTE: we only parse and use anyPolicy without qualifiers at this point
762 * as defined in RFC 5280.
763 */
764static int x509_get_certificate_policies( unsigned char **p,
765 const unsigned char *end,
766 mbedtls_x509_sequence *certificate_policies )
767{
Ron Eldor8b0c3c92019-05-15 12:20:00 +0300768 int ret, parse_ret = 0;
Ron Eldor74d9acc2019-03-21 14:00:03 +0200769 size_t len;
770 mbedtls_asn1_buf *buf;
771 mbedtls_asn1_sequence *cur = certificate_policies;
772
773 /* Get main sequence tag */
774 ret = mbedtls_asn1_get_tag( p, end, &len,
775 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE );
776 if( ret != 0 )
Chris Jones9f7a6932021-04-14 12:12:09 +0100777 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret ) );
Ron Eldor74d9acc2019-03-21 14:00:03 +0200778
779 if( *p + len != end )
Chris Jones9f7a6932021-04-14 12:12:09 +0100780 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
781 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH ) );
Ron Eldor74d9acc2019-03-21 14:00:03 +0200782
783 /*
784 * Cannot be an empty sequence.
785 */
786 if( len == 0 )
Chris Jones9f7a6932021-04-14 12:12:09 +0100787 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
788 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH ) );
Ron Eldor74d9acc2019-03-21 14:00:03 +0200789
790 while( *p < end )
791 {
792 mbedtls_x509_buf policy_oid;
793 const unsigned char *policy_end;
794
795 /*
796 * Get the policy sequence
797 */
798 if( ( ret = mbedtls_asn1_get_tag( p, end, &len,
799 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
Chris Jones9f7a6932021-04-14 12:12:09 +0100800 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret ) );
Ron Eldor74d9acc2019-03-21 14:00:03 +0200801
802 policy_end = *p + len;
803
Ron Eldor08063792019-05-13 16:38:39 +0300804 if( ( ret = mbedtls_asn1_get_tag( p, policy_end, &len,
Ron Eldor74d9acc2019-03-21 14:00:03 +0200805 MBEDTLS_ASN1_OID ) ) != 0 )
Chris Jones9f7a6932021-04-14 12:12:09 +0100806 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret ) );
Ron Eldor74d9acc2019-03-21 14:00:03 +0200807
808 policy_oid.tag = MBEDTLS_ASN1_OID;
809 policy_oid.len = len;
810 policy_oid.p = *p;
811
Ron Eldor8b0c3c92019-05-15 12:20:00 +0300812 /*
813 * Only AnyPolicy is currently supported when enforcing policy.
814 */
815 if( MBEDTLS_OID_CMP( MBEDTLS_OID_ANY_POLICY, &policy_oid ) != 0 )
816 {
817 /*
818 * Set the parsing return code but continue parsing, in case this
819 * extension is critical and MBEDTLS_X509_ALLOW_UNSUPPORTED_CRITICAL_EXTENSION
820 * is configured.
821 */
822 parse_ret = MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE;
823 }
824
Ron Eldor74d9acc2019-03-21 14:00:03 +0200825 /* Allocate and assign next pointer */
826 if( cur->buf.p != NULL )
827 {
828 if( cur->next != NULL )
829 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS );
830
831 cur->next = mbedtls_calloc( 1, sizeof( mbedtls_asn1_sequence ) );
832
833 if( cur->next == NULL )
Chris Jones9f7a6932021-04-14 12:12:09 +0100834 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
835 MBEDTLS_ERR_ASN1_ALLOC_FAILED ) );
Ron Eldor74d9acc2019-03-21 14:00:03 +0200836
837 cur = cur->next;
838 }
839
840 buf = &( cur->buf );
841 buf->tag = policy_oid.tag;
842 buf->p = policy_oid.p;
843 buf->len = policy_oid.len;
Ron Eldor08063792019-05-13 16:38:39 +0300844
845 *p += len;
846
847 /*
848 * If there is an optional qualifier, then *p < policy_end
849 * Check the Qualifier len to verify it doesn't exceed policy_end.
850 */
851 if( *p < policy_end )
852 {
853 if( ( ret = mbedtls_asn1_get_tag( p, policy_end, &len,
854 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
Chris Jones9f7a6932021-04-14 12:12:09 +0100855 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret ) );
Ron Eldor08063792019-05-13 16:38:39 +0300856 /*
857 * Skip the optional policy qualifiers.
858 */
859 *p += len;
860 }
861
862 if( *p != policy_end )
Chris Jones9f7a6932021-04-14 12:12:09 +0100863 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
864 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH ) );
Ron Eldor74d9acc2019-03-21 14:00:03 +0200865 }
866
867 /* Set final sequence entry's next pointer to NULL */
868 cur->next = NULL;
869
870 if( *p != end )
Chris Jones9f7a6932021-04-14 12:12:09 +0100871 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
872 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH ) );
Ron Eldor74d9acc2019-03-21 14:00:03 +0200873
Ron Eldor8b0c3c92019-05-15 12:20:00 +0300874 return( parse_ret );
Ron Eldor74d9acc2019-03-21 14:00:03 +0200875}
876
877/*
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200878 * X.509 v3 extensions
879 *
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200880 */
881static int x509_get_crt_ext( unsigned char **p,
882 const unsigned char *end,
Nicola Di Lieto502d4b42020-04-25 14:41:25 +0200883 mbedtls_x509_crt *crt,
Nicola Di Lieto5659e7e2020-05-28 23:41:38 +0200884 mbedtls_x509_crt_ext_cb_t cb,
885 void *p_ctx )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200886{
Janos Follath865b3eb2019-12-16 11:46:15 +0000887 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200888 size_t len;
Nicola Di Lietoc84b1e62020-06-13 11:08:16 +0200889 unsigned char *end_ext_data, *start_ext_octet, *end_ext_octet;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200890
Hanno Becker12f62fb2019-02-12 17:22:36 +0000891 if( *p == end )
892 return( 0 );
893
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200894 if( ( ret = mbedtls_x509_get_ext( p, end, &crt->v3_ext, 3 ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200895 return( ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200896
Hanno Becker12f62fb2019-02-12 17:22:36 +0000897 end = crt->v3_ext.p + crt->v3_ext.len;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200898 while( *p < end )
899 {
900 /*
901 * Extension ::= SEQUENCE {
902 * extnID OBJECT IDENTIFIER,
903 * critical BOOLEAN DEFAULT FALSE,
904 * extnValue OCTET STRING }
905 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200906 mbedtls_x509_buf extn_oid = {0, 0, NULL};
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200907 int is_critical = 0; /* DEFAULT FALSE */
908 int ext_type = 0;
909
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200910 if( ( ret = mbedtls_asn1_get_tag( p, end, &len,
911 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
Chris Jones9f7a6932021-04-14 12:12:09 +0100912 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200913
914 end_ext_data = *p + len;
915
916 /* Get extension ID */
k-stachowiakdcae78a2018-06-28 16:32:54 +0200917 if( ( ret = mbedtls_asn1_get_tag( p, end_ext_data, &extn_oid.len,
k-stachowiak463928a2018-07-24 12:50:59 +0200918 MBEDTLS_ASN1_OID ) ) != 0 )
Chris Jones9f7a6932021-04-14 12:12:09 +0100919 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200920
k-stachowiak463928a2018-07-24 12:50:59 +0200921 extn_oid.tag = MBEDTLS_ASN1_OID;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200922 extn_oid.p = *p;
923 *p += extn_oid.len;
924
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200925 /* Get optional critical */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200926 if( ( ret = mbedtls_asn1_get_bool( p, end_ext_data, &is_critical ) ) != 0 &&
927 ( ret != MBEDTLS_ERR_ASN1_UNEXPECTED_TAG ) )
Chris Jones9f7a6932021-04-14 12:12:09 +0100928 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200929
930 /* Data should be octet string type */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200931 if( ( ret = mbedtls_asn1_get_tag( p, end_ext_data, &len,
932 MBEDTLS_ASN1_OCTET_STRING ) ) != 0 )
Chris Jones9f7a6932021-04-14 12:12:09 +0100933 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200934
Nicola Di Lietoc84b1e62020-06-13 11:08:16 +0200935 start_ext_octet = *p;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200936 end_ext_octet = *p + len;
937
938 if( end_ext_octet != end_ext_data )
Chris Jones9f7a6932021-04-14 12:12:09 +0100939 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
940 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200941
942 /*
943 * Detect supported extensions
944 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200945 ret = mbedtls_oid_get_x509_ext_type( &extn_oid, &ext_type );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200946
947 if( ret != 0 )
948 {
Nicola Di Lieto502d4b42020-04-25 14:41:25 +0200949 /* Give the callback (if any) a chance to handle the extension */
Nicola Di Lieto2c3a9172020-05-28 17:20:42 +0200950 if( cb != NULL )
951 {
Nicola Di Lieto5659e7e2020-05-28 23:41:38 +0200952 ret = cb( p_ctx, crt, &extn_oid, is_critical, *p, end_ext_octet );
Nicola Di Lieto565b52b2020-05-29 22:46:56 +0200953 if( ret != 0 && is_critical )
954 return( ret );
Nicola Di Lietofae25a12020-05-28 08:55:08 +0200955 *p = end_ext_octet;
Nicola Di Lieto502d4b42020-04-25 14:41:25 +0200956 continue;
Nicola Di Lietofae25a12020-05-28 08:55:08 +0200957 }
Nicola Di Lieto502d4b42020-04-25 14:41:25 +0200958
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200959 /* No parser found, skip extension */
960 *p = end_ext_octet;
961
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200962#if !defined(MBEDTLS_X509_ALLOW_UNSUPPORTED_CRITICAL_EXTENSION)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200963 if( is_critical )
964 {
965 /* Data is marked as critical: fail */
Chris Jones9f7a6932021-04-14 12:12:09 +0100966 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
967 MBEDTLS_ERR_ASN1_UNEXPECTED_TAG ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200968 }
969#endif
970 continue;
971 }
972
Manuel Pégourié-Gonnard8a5e3d42014-11-12 17:47:28 +0100973 /* Forbid repeated extensions */
974 if( ( crt->ext_types & ext_type ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200975 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS );
Manuel Pégourié-Gonnard8a5e3d42014-11-12 17:47:28 +0100976
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200977 crt->ext_types |= ext_type;
978
979 switch( ext_type )
980 {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +0100981 case MBEDTLS_X509_EXT_BASIC_CONSTRAINTS:
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200982 /* Parse basic constraints */
983 if( ( ret = x509_get_basic_constraints( p, end_ext_octet,
984 &crt->ca_istrue, &crt->max_pathlen ) ) != 0 )
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200985 return( ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200986 break;
987
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200988 case MBEDTLS_X509_EXT_KEY_USAGE:
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200989 /* Parse key usage */
990 if( ( ret = x509_get_key_usage( p, end_ext_octet,
991 &crt->key_usage ) ) != 0 )
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200992 return( ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200993 break;
994
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200995 case MBEDTLS_X509_EXT_EXTENDED_KEY_USAGE:
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200996 /* Parse extended key usage */
997 if( ( ret = x509_get_ext_key_usage( p, end_ext_octet,
998 &crt->ext_key_usage ) ) != 0 )
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200999 return( ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001000 break;
1001
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01001002 case MBEDTLS_X509_EXT_SUBJECT_ALT_NAME:
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001003 /* Parse subject alt name */
1004 if( ( ret = x509_get_subject_alt_name( p, end_ext_octet,
1005 &crt->subject_alt_names ) ) != 0 )
Paul Bakkerd8bb8262014-06-17 14:06:49 +02001006 return( ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001007 break;
1008
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001009 case MBEDTLS_X509_EXT_NS_CERT_TYPE:
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001010 /* Parse netscape certificate type */
1011 if( ( ret = x509_get_ns_cert_type( p, end_ext_octet,
1012 &crt->ns_cert_type ) ) != 0 )
Paul Bakkerd8bb8262014-06-17 14:06:49 +02001013 return( ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001014 break;
1015
Ron Eldor74d9acc2019-03-21 14:00:03 +02001016 case MBEDTLS_OID_X509_EXT_CERTIFICATE_POLICIES:
1017 /* Parse certificate policies type */
1018 if( ( ret = x509_get_certificate_policies( p, end_ext_octet,
1019 &crt->certificate_policies ) ) != 0 )
Ron Eldor8b0c3c92019-05-15 12:20:00 +03001020 {
Nicola Di Lietoc84b1e62020-06-13 11:08:16 +02001021 /* Give the callback (if any) a chance to handle the extension
1022 * if it contains unsupported policies */
1023 if( ret == MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE && cb != NULL &&
1024 cb( p_ctx, crt, &extn_oid, is_critical,
1025 start_ext_octet, end_ext_octet ) == 0 )
1026 break;
1027
Ron Eldor8b0c3c92019-05-15 12:20:00 +03001028#if !defined(MBEDTLS_X509_ALLOW_UNSUPPORTED_CRITICAL_EXTENSION)
1029 if( is_critical )
1030 return( ret );
1031 else
1032#endif
1033 /*
Ron Eldora2913912019-05-16 16:17:38 +03001034 * If MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE is returned, then we
1035 * cannot interpret or enforce the policy. However, it is up to
1036 * the user to choose how to enforce the policies,
Ron Eldor8b0c3c92019-05-15 12:20:00 +03001037 * unless the extension is critical.
1038 */
1039 if( ret != MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE )
1040 return( ret );
1041 }
Ron Eldor74d9acc2019-03-21 14:00:03 +02001042 break;
1043
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001044 default:
Ron Eldordf48efa2019-04-08 13:28:24 +03001045 /*
1046 * If this is a non-critical extension, which the oid layer
1047 * supports, but there isn't an x509 parser for it,
1048 * skip the extension.
1049 */
1050#if !defined(MBEDTLS_X509_ALLOW_UNSUPPORTED_CRITICAL_EXTENSION)
1051 if( is_critical )
1052 return( MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE );
1053 else
1054#endif
1055 *p = end_ext_octet;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001056 }
1057 }
1058
1059 if( *p != end )
Chris Jones9f7a6932021-04-14 12:12:09 +01001060 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
1061 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001062
1063 return( 0 );
1064}
1065
1066/*
1067 * Parse and fill a single X.509 certificate in DER format
1068 */
Hanno Becker1a65dcd2019-01-31 08:57:44 +00001069static int x509_crt_parse_der_core( mbedtls_x509_crt *crt,
1070 const unsigned char *buf,
1071 size_t buflen,
Nicola Di Lieto502d4b42020-04-25 14:41:25 +02001072 int make_copy,
Nicola Di Lieto5659e7e2020-05-28 23:41:38 +02001073 mbedtls_x509_crt_ext_cb_t cb,
1074 void *p_ctx )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001075{
Janos Follath865b3eb2019-12-16 11:46:15 +00001076 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001077 size_t len;
1078 unsigned char *p, *end, *crt_end;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001079 mbedtls_x509_buf sig_params1, sig_params2, sig_oid2;
Manuel Pégourié-Gonnard59a75d52014-01-22 10:12:57 +01001080
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001081 memset( &sig_params1, 0, sizeof( mbedtls_x509_buf ) );
1082 memset( &sig_params2, 0, sizeof( mbedtls_x509_buf ) );
1083 memset( &sig_oid2, 0, sizeof( mbedtls_x509_buf ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001084
1085 /*
1086 * Check for valid input
1087 */
1088 if( crt == NULL || buf == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001089 return( MBEDTLS_ERR_X509_BAD_INPUT_DATA );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001090
Hanno Becker1a65dcd2019-01-31 08:57:44 +00001091 /* Use the original buffer until we figure out actual length. */
Janos Follathcc0e49d2016-02-17 14:34:12 +00001092 p = (unsigned char*) buf;
1093 len = buflen;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001094 end = p + len;
1095
1096 /*
1097 * Certificate ::= SEQUENCE {
1098 * tbsCertificate TBSCertificate,
1099 * signatureAlgorithm AlgorithmIdentifier,
1100 * signatureValue BIT STRING }
1101 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001102 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
1103 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001104 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001105 mbedtls_x509_crt_free( crt );
1106 return( MBEDTLS_ERR_X509_INVALID_FORMAT );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001107 }
1108
Janos Follathcc0e49d2016-02-17 14:34:12 +00001109 end = crt_end = p + len;
Hanno Becker1a65dcd2019-01-31 08:57:44 +00001110 crt->raw.len = crt_end - buf;
1111 if( make_copy != 0 )
1112 {
1113 /* Create and populate a new buffer for the raw field. */
1114 crt->raw.p = p = mbedtls_calloc( 1, crt->raw.len );
1115 if( crt->raw.p == NULL )
1116 return( MBEDTLS_ERR_X509_ALLOC_FAILED );
1117
1118 memcpy( crt->raw.p, buf, crt->raw.len );
1119 crt->own_buffer = 1;
1120
1121 p += crt->raw.len - len;
1122 end = crt_end = p + len;
1123 }
1124 else
1125 {
1126 crt->raw.p = (unsigned char*) buf;
1127 crt->own_buffer = 0;
1128 }
Janos Follathcc0e49d2016-02-17 14:34:12 +00001129
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001130 /*
1131 * TBSCertificate ::= SEQUENCE {
1132 */
1133 crt->tbs.p = p;
1134
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001135 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
1136 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001137 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001138 mbedtls_x509_crt_free( crt );
Chris Jones9f7a6932021-04-14 12:12:09 +01001139 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_FORMAT, ret ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001140 }
1141
1142 end = p + len;
1143 crt->tbs.len = end - crt->tbs.p;
1144
1145 /*
1146 * Version ::= INTEGER { v1(0), v2(1), v3(2) }
1147 *
1148 * CertificateSerialNumber ::= INTEGER
1149 *
1150 * signature AlgorithmIdentifier
1151 */
1152 if( ( ret = x509_get_version( &p, end, &crt->version ) ) != 0 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001153 ( ret = mbedtls_x509_get_serial( &p, end, &crt->serial ) ) != 0 ||
1154 ( ret = mbedtls_x509_get_alg( &p, end, &crt->sig_oid,
Manuel Pégourié-Gonnarddddbb1d2014-06-05 17:02:24 +02001155 &sig_params1 ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001156 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001157 mbedtls_x509_crt_free( crt );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001158 return( ret );
1159 }
1160
Andres AG7ca4a032017-03-09 16:16:11 +00001161 if( crt->version < 0 || crt->version > 2 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001162 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001163 mbedtls_x509_crt_free( crt );
1164 return( MBEDTLS_ERR_X509_UNKNOWN_VERSION );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001165 }
1166
Andres AG7ca4a032017-03-09 16:16:11 +00001167 crt->version++;
1168
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001169 if( ( ret = mbedtls_x509_get_sig_alg( &crt->sig_oid, &sig_params1,
Manuel Pégourié-Gonnardf75f2f72014-06-05 15:14:28 +02001170 &crt->sig_md, &crt->sig_pk,
1171 &crt->sig_opts ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001172 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001173 mbedtls_x509_crt_free( crt );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001174 return( ret );
1175 }
1176
1177 /*
1178 * issuer Name
1179 */
1180 crt->issuer_raw.p = p;
1181
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001182 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
1183 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001184 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001185 mbedtls_x509_crt_free( crt );
Chris Jones9f7a6932021-04-14 12:12:09 +01001186 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_FORMAT, ret ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001187 }
1188
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001189 if( ( ret = mbedtls_x509_get_name( &p, p + len, &crt->issuer ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001190 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001191 mbedtls_x509_crt_free( crt );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001192 return( ret );
1193 }
1194
1195 crt->issuer_raw.len = p - crt->issuer_raw.p;
1196
1197 /*
1198 * Validity ::= SEQUENCE {
1199 * notBefore Time,
1200 * notAfter Time }
1201 *
1202 */
1203 if( ( ret = x509_get_dates( &p, end, &crt->valid_from,
1204 &crt->valid_to ) ) != 0 )
1205 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001206 mbedtls_x509_crt_free( crt );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001207 return( ret );
1208 }
1209
1210 /*
1211 * subject Name
1212 */
1213 crt->subject_raw.p = p;
1214
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001215 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
1216 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001217 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001218 mbedtls_x509_crt_free( crt );
Chris Jones9f7a6932021-04-14 12:12:09 +01001219 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_FORMAT, ret ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001220 }
1221
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001222 if( len && ( ret = mbedtls_x509_get_name( &p, p + len, &crt->subject ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001223 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001224 mbedtls_x509_crt_free( crt );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001225 return( ret );
1226 }
1227
1228 crt->subject_raw.len = p - crt->subject_raw.p;
1229
1230 /*
1231 * SubjectPublicKeyInfo
1232 */
Hanno Becker494dd7a2019-02-06 16:13:41 +00001233 crt->pk_raw.p = p;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001234 if( ( ret = mbedtls_pk_parse_subpubkey( &p, end, &crt->pk ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001235 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001236 mbedtls_x509_crt_free( crt );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001237 return( ret );
1238 }
Hanno Becker494dd7a2019-02-06 16:13:41 +00001239 crt->pk_raw.len = p - crt->pk_raw.p;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001240
1241 /*
1242 * issuerUniqueID [1] IMPLICIT UniqueIdentifier OPTIONAL,
1243 * -- If present, version shall be v2 or v3
1244 * subjectUniqueID [2] IMPLICIT UniqueIdentifier OPTIONAL,
1245 * -- If present, version shall be v2 or v3
1246 * extensions [3] EXPLICIT Extensions OPTIONAL
1247 * -- If present, version shall be v3
1248 */
1249 if( crt->version == 2 || crt->version == 3 )
1250 {
1251 ret = x509_get_uid( &p, end, &crt->issuer_id, 1 );
1252 if( ret != 0 )
1253 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001254 mbedtls_x509_crt_free( crt );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001255 return( ret );
1256 }
1257 }
1258
1259 if( crt->version == 2 || crt->version == 3 )
1260 {
1261 ret = x509_get_uid( &p, end, &crt->subject_id, 2 );
1262 if( ret != 0 )
1263 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001264 mbedtls_x509_crt_free( crt );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001265 return( ret );
1266 }
1267 }
1268
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001269#if !defined(MBEDTLS_X509_ALLOW_EXTENSIONS_NON_V3)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001270 if( crt->version == 3 )
Paul Bakkerc27c4e22013-09-23 15:01:36 +02001271#endif
Manuel Pégourié-Gonnarda252af72015-03-27 16:15:55 +01001272 {
Nicola Di Lieto5659e7e2020-05-28 23:41:38 +02001273 ret = x509_get_crt_ext( &p, end, crt, cb, p_ctx );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001274 if( ret != 0 )
1275 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001276 mbedtls_x509_crt_free( crt );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001277 return( ret );
1278 }
1279 }
1280
1281 if( p != end )
1282 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001283 mbedtls_x509_crt_free( crt );
Chris Jones9f7a6932021-04-14 12:12:09 +01001284 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_FORMAT,
1285 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001286 }
1287
1288 end = crt_end;
1289
1290 /*
1291 * }
1292 * -- end of TBSCertificate
1293 *
1294 * signatureAlgorithm AlgorithmIdentifier,
1295 * signatureValue BIT STRING
1296 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001297 if( ( ret = mbedtls_x509_get_alg( &p, end, &sig_oid2, &sig_params2 ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001298 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001299 mbedtls_x509_crt_free( crt );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001300 return( ret );
1301 }
1302
Manuel Pégourié-Gonnard1022fed2015-03-27 16:30:47 +01001303 if( crt->sig_oid.len != sig_oid2.len ||
1304 memcmp( crt->sig_oid.p, sig_oid2.p, crt->sig_oid.len ) != 0 ||
Paul Elliottca17ebf2020-11-24 17:30:18 +00001305 sig_params1.tag != sig_params2.tag ||
Manuel Pégourié-Gonnarddddbb1d2014-06-05 17:02:24 +02001306 sig_params1.len != sig_params2.len ||
Manuel Pégourié-Gonnard159c5242015-04-30 11:15:22 +02001307 ( sig_params1.len != 0 &&
1308 memcmp( sig_params1.p, sig_params2.p, sig_params1.len ) != 0 ) )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001309 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001310 mbedtls_x509_crt_free( crt );
1311 return( MBEDTLS_ERR_X509_SIG_MISMATCH );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001312 }
1313
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001314 if( ( ret = mbedtls_x509_get_sig( &p, end, &crt->sig ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001315 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001316 mbedtls_x509_crt_free( crt );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001317 return( ret );
1318 }
1319
1320 if( p != end )
1321 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001322 mbedtls_x509_crt_free( crt );
Chris Jones9f7a6932021-04-14 12:12:09 +01001323 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_FORMAT,
1324 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001325 }
1326
1327 return( 0 );
1328}
1329
1330/*
1331 * Parse one X.509 certificate in DER format from a buffer and add them to a
1332 * chained list
1333 */
Hanno Becker1a65dcd2019-01-31 08:57:44 +00001334static int mbedtls_x509_crt_parse_der_internal( mbedtls_x509_crt *chain,
1335 const unsigned char *buf,
1336 size_t buflen,
Nicola Di Lieto502d4b42020-04-25 14:41:25 +02001337 int make_copy,
Nicola Di Lieto5659e7e2020-05-28 23:41:38 +02001338 mbedtls_x509_crt_ext_cb_t cb,
1339 void *p_ctx )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001340{
Janos Follath865b3eb2019-12-16 11:46:15 +00001341 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001342 mbedtls_x509_crt *crt = chain, *prev = NULL;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001343
1344 /*
1345 * Check for valid input
1346 */
1347 if( crt == NULL || buf == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001348 return( MBEDTLS_ERR_X509_BAD_INPUT_DATA );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001349
1350 while( crt->version != 0 && crt->next != NULL )
1351 {
1352 prev = crt;
1353 crt = crt->next;
1354 }
1355
1356 /*
1357 * Add new certificate on the end of the chain if needed.
1358 */
Paul Bakker66d5d072014-06-17 16:39:18 +02001359 if( crt->version != 0 && crt->next == NULL )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001360 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02001361 crt->next = mbedtls_calloc( 1, sizeof( mbedtls_x509_crt ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001362
1363 if( crt->next == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02001364 return( MBEDTLS_ERR_X509_ALLOC_FAILED );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001365
1366 prev = crt;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001367 mbedtls_x509_crt_init( crt->next );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001368 crt = crt->next;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001369 }
1370
Nicola Di Lieto5659e7e2020-05-28 23:41:38 +02001371 ret = x509_crt_parse_der_core( crt, buf, buflen, make_copy, cb, p_ctx );
Nicola Di Lieto502d4b42020-04-25 14:41:25 +02001372 if( ret != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001373 {
1374 if( prev )
1375 prev->next = NULL;
1376
1377 if( crt != chain )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001378 mbedtls_free( crt );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001379
1380 return( ret );
1381 }
1382
1383 return( 0 );
1384}
1385
Hanno Becker1a65dcd2019-01-31 08:57:44 +00001386int mbedtls_x509_crt_parse_der_nocopy( mbedtls_x509_crt *chain,
1387 const unsigned char *buf,
1388 size_t buflen )
1389{
Nicola Di Lieto5659e7e2020-05-28 23:41:38 +02001390 return( mbedtls_x509_crt_parse_der_internal( chain, buf, buflen, 0, NULL, NULL ) );
Nicola Di Lieto502d4b42020-04-25 14:41:25 +02001391}
1392
Nicola Di Lietofde98f72020-05-28 08:34:33 +02001393int mbedtls_x509_crt_parse_der_with_ext_cb( mbedtls_x509_crt *chain,
1394 const unsigned char *buf,
1395 size_t buflen,
Nicola Di Lieto4dbe5672020-05-28 09:18:42 +02001396 int make_copy,
Nicola Di Lieto5659e7e2020-05-28 23:41:38 +02001397 mbedtls_x509_crt_ext_cb_t cb,
1398 void *p_ctx )
Nicola Di Lieto502d4b42020-04-25 14:41:25 +02001399{
Nicola Di Lieto5659e7e2020-05-28 23:41:38 +02001400 return( mbedtls_x509_crt_parse_der_internal( chain, buf, buflen, make_copy, cb, p_ctx ) );
Hanno Becker1a65dcd2019-01-31 08:57:44 +00001401}
1402
1403int mbedtls_x509_crt_parse_der( mbedtls_x509_crt *chain,
1404 const unsigned char *buf,
1405 size_t buflen )
1406{
Nicola Di Lieto5659e7e2020-05-28 23:41:38 +02001407 return( mbedtls_x509_crt_parse_der_internal( chain, buf, buflen, 1, NULL, NULL ) );
Hanno Becker1a65dcd2019-01-31 08:57:44 +00001408}
1409
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001410/*
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001411 * Parse one or more PEM certificates from a buffer and add them to the chained
1412 * list
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001413 */
Hanno Becker1a65dcd2019-01-31 08:57:44 +00001414int mbedtls_x509_crt_parse( mbedtls_x509_crt *chain,
1415 const unsigned char *buf,
1416 size_t buflen )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001417{
Janos Follath98e28a72016-05-31 14:03:54 +01001418#if defined(MBEDTLS_PEM_PARSE_C)
Andres AGc0db5112016-12-07 15:05:53 +00001419 int success = 0, first_error = 0, total_failed = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001420 int buf_format = MBEDTLS_X509_FORMAT_DER;
Janos Follath98e28a72016-05-31 14:03:54 +01001421#endif
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001422
1423 /*
1424 * Check for valid input
1425 */
1426 if( chain == NULL || buf == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001427 return( MBEDTLS_ERR_X509_BAD_INPUT_DATA );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001428
1429 /*
1430 * Determine buffer content. Buffer contains either one DER certificate or
1431 * one or more PEM certificates.
1432 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001433#if defined(MBEDTLS_PEM_PARSE_C)
Manuel Pégourié-Gonnard0ece0f92015-05-12 12:43:54 +02001434 if( buflen != 0 && buf[buflen - 1] == '\0' &&
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001435 strstr( (const char *) buf, "-----BEGIN CERTIFICATE-----" ) != NULL )
1436 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001437 buf_format = MBEDTLS_X509_FORMAT_PEM;
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001438 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001439
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001440 if( buf_format == MBEDTLS_X509_FORMAT_DER )
1441 return mbedtls_x509_crt_parse_der( chain, buf, buflen );
Janos Follath98e28a72016-05-31 14:03:54 +01001442#else
1443 return mbedtls_x509_crt_parse_der( chain, buf, buflen );
1444#endif
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001445
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001446#if defined(MBEDTLS_PEM_PARSE_C)
1447 if( buf_format == MBEDTLS_X509_FORMAT_PEM )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001448 {
Janos Follath865b3eb2019-12-16 11:46:15 +00001449 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001450 mbedtls_pem_context pem;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001451
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001452 /* 1 rather than 0 since the terminating NULL byte is counted in */
1453 while( buflen > 1 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001454 {
1455 size_t use_len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001456 mbedtls_pem_init( &pem );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001457
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001458 /* If we get there, we know the string is null-terminated */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001459 ret = mbedtls_pem_read_buffer( &pem,
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001460 "-----BEGIN CERTIFICATE-----",
1461 "-----END CERTIFICATE-----",
1462 buf, NULL, 0, &use_len );
1463
1464 if( ret == 0 )
1465 {
1466 /*
1467 * Was PEM encoded
1468 */
1469 buflen -= use_len;
1470 buf += use_len;
1471 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001472 else if( ret == MBEDTLS_ERR_PEM_BAD_INPUT_DATA )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001473 {
1474 return( ret );
1475 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001476 else if( ret != MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001477 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001478 mbedtls_pem_free( &pem );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001479
1480 /*
1481 * PEM header and footer were found
1482 */
1483 buflen -= use_len;
1484 buf += use_len;
1485
1486 if( first_error == 0 )
1487 first_error = ret;
1488
Paul Bakker5a5fa922014-09-26 14:53:04 +02001489 total_failed++;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001490 continue;
1491 }
1492 else
1493 break;
1494
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001495 ret = mbedtls_x509_crt_parse_der( chain, pem.buf, pem.buflen );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001496
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001497 mbedtls_pem_free( &pem );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001498
1499 if( ret != 0 )
1500 {
1501 /*
1502 * Quit parsing on a memory error
1503 */
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02001504 if( ret == MBEDTLS_ERR_X509_ALLOC_FAILED )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001505 return( ret );
1506
1507 if( first_error == 0 )
1508 first_error = ret;
1509
1510 total_failed++;
1511 continue;
1512 }
1513
1514 success = 1;
1515 }
1516 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001517
1518 if( success )
1519 return( total_failed );
1520 else if( first_error )
1521 return( first_error );
1522 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001523 return( MBEDTLS_ERR_X509_CERT_UNKNOWN_FORMAT );
Janos Follath98e28a72016-05-31 14:03:54 +01001524#endif /* MBEDTLS_PEM_PARSE_C */
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001525}
1526
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001527#if defined(MBEDTLS_FS_IO)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001528/*
1529 * Load one or more certificates and add them to the chained list
1530 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001531int mbedtls_x509_crt_parse_file( mbedtls_x509_crt *chain, const char *path )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001532{
Janos Follath865b3eb2019-12-16 11:46:15 +00001533 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001534 size_t n;
1535 unsigned char *buf;
1536
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001537 if( ( ret = mbedtls_pk_load_file( path, &buf, &n ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001538 return( ret );
1539
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001540 ret = mbedtls_x509_crt_parse( chain, buf, n );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001541
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05001542 mbedtls_platform_zeroize( buf, n );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001543 mbedtls_free( buf );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001544
1545 return( ret );
1546}
1547
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001548int mbedtls_x509_crt_parse_path( mbedtls_x509_crt *chain, const char *path )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001549{
1550 int ret = 0;
Paul Bakkerfa6a6202013-10-28 18:48:30 +01001551#if defined(_WIN32) && !defined(EFIX64) && !defined(EFI32)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001552 int w_ret;
1553 WCHAR szDir[MAX_PATH];
1554 char filename[MAX_PATH];
Paul Bakker9af723c2014-05-01 13:03:14 +02001555 char *p;
Manuel Pégourié-Gonnard261faed2015-10-21 10:16:29 +02001556 size_t len = strlen( path );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001557
Paul Bakker9af723c2014-05-01 13:03:14 +02001558 WIN32_FIND_DATAW file_data;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001559 HANDLE hFind;
1560
1561 if( len > MAX_PATH - 3 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001562 return( MBEDTLS_ERR_X509_BAD_INPUT_DATA );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001563
Paul Bakker9af723c2014-05-01 13:03:14 +02001564 memset( szDir, 0, sizeof(szDir) );
1565 memset( filename, 0, MAX_PATH );
1566 memcpy( filename, path, len );
1567 filename[len++] = '\\';
1568 p = filename + len;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001569 filename[len++] = '*';
1570
Simon B3c6b18d2016-11-03 01:11:37 +00001571 w_ret = MultiByteToWideChar( CP_ACP, 0, filename, (int)len, szDir,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001572 MAX_PATH - 3 );
Manuel Pégourié-Gonnardacdb9b92015-01-23 17:50:34 +00001573 if( w_ret == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001574 return( MBEDTLS_ERR_X509_BAD_INPUT_DATA );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001575
1576 hFind = FindFirstFileW( szDir, &file_data );
Paul Bakker66d5d072014-06-17 16:39:18 +02001577 if( hFind == INVALID_HANDLE_VALUE )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001578 return( MBEDTLS_ERR_X509_FILE_IO_ERROR );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001579
1580 len = MAX_PATH - len;
1581 do
1582 {
Paul Bakker9af723c2014-05-01 13:03:14 +02001583 memset( p, 0, len );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001584
1585 if( file_data.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY )
1586 continue;
1587
Paul Bakker9af723c2014-05-01 13:03:14 +02001588 w_ret = WideCharToMultiByte( CP_ACP, 0, file_data.cFileName,
Paul Bakker66d5d072014-06-17 16:39:18 +02001589 lstrlenW( file_data.cFileName ),
Manuel Pégourié-Gonnard261faed2015-10-21 10:16:29 +02001590 p, (int) len - 1,
Paul Bakker9af723c2014-05-01 13:03:14 +02001591 NULL, NULL );
Manuel Pégourié-Gonnardacdb9b92015-01-23 17:50:34 +00001592 if( w_ret == 0 )
Ron Eldor36d90422017-01-09 15:09:16 +02001593 {
1594 ret = MBEDTLS_ERR_X509_FILE_IO_ERROR;
1595 goto cleanup;
1596 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001597
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001598 w_ret = mbedtls_x509_crt_parse_file( chain, filename );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001599 if( w_ret < 0 )
1600 ret++;
1601 else
1602 ret += w_ret;
1603 }
1604 while( FindNextFileW( hFind, &file_data ) != 0 );
1605
Paul Bakker66d5d072014-06-17 16:39:18 +02001606 if( GetLastError() != ERROR_NO_MORE_FILES )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001607 ret = MBEDTLS_ERR_X509_FILE_IO_ERROR;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001608
Ron Eldor36d90422017-01-09 15:09:16 +02001609cleanup:
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001610 FindClose( hFind );
Paul Bakkerbe089b02013-10-14 15:51:50 +02001611#else /* _WIN32 */
Manuel Pégourié-Gonnard964bf9b2013-11-26 16:47:11 +01001612 int t_ret;
Andres AGf9113192016-09-02 14:06:04 +01001613 int snp_ret;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001614 struct stat sb;
Manuel Pégourié-Gonnard964bf9b2013-11-26 16:47:11 +01001615 struct dirent *entry;
Andres AGf9113192016-09-02 14:06:04 +01001616 char entry_name[MBEDTLS_X509_MAX_FILE_PATH_LEN];
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001617 DIR *dir = opendir( path );
1618
Paul Bakker66d5d072014-06-17 16:39:18 +02001619 if( dir == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001620 return( MBEDTLS_ERR_X509_FILE_IO_ERROR );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001621
Ron Eldor63140682017-01-09 19:27:59 +02001622#if defined(MBEDTLS_THREADING_C)
Manuel Pégourié-Gonnard944cfe82015-05-27 20:07:18 +02001623 if( ( ret = mbedtls_mutex_lock( &mbedtls_threading_readdir_mutex ) ) != 0 )
Manuel Pégourié-Gonnardf9b85d92015-06-22 18:39:57 +02001624 {
1625 closedir( dir );
Manuel Pégourié-Gonnard5ad68e42013-11-28 17:11:54 +01001626 return( ret );
Manuel Pégourié-Gonnardf9b85d92015-06-22 18:39:57 +02001627 }
Ron Eldor63140682017-01-09 19:27:59 +02001628#endif /* MBEDTLS_THREADING_C */
Manuel Pégourié-Gonnard5ad68e42013-11-28 17:11:54 +01001629
Paul Elliottfb91a482021-03-05 14:17:51 +00001630 memset( &sb, 0, sizeof( sb ) );
1631
Manuel Pégourié-Gonnard964bf9b2013-11-26 16:47:11 +01001632 while( ( entry = readdir( dir ) ) != NULL )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001633 {
Andres AGf9113192016-09-02 14:06:04 +01001634 snp_ret = mbedtls_snprintf( entry_name, sizeof entry_name,
1635 "%s/%s", path, entry->d_name );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001636
Andres AGf9113192016-09-02 14:06:04 +01001637 if( snp_ret < 0 || (size_t)snp_ret >= sizeof entry_name )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001638 {
Andres AGf9113192016-09-02 14:06:04 +01001639 ret = MBEDTLS_ERR_X509_BUFFER_TOO_SMALL;
1640 goto cleanup;
1641 }
Dave Rodgman6f227ee2022-07-20 16:08:00 +01001642 else if( stat( entry_name, &sb ) == -1 )
Andres AGf9113192016-09-02 14:06:04 +01001643 {
Dave Rodgman6f227ee2022-07-20 16:08:00 +01001644 if( errno == ENOENT )
Eduardo Silva32ffb2b2019-04-25 10:43:26 -06001645 {
Dave Rodgman6f227ee2022-07-20 16:08:00 +01001646 /* Broken symbolic link - ignore this entry.
1647 stat(2) will return this error for either (a) a dangling
1648 symlink or (b) a missing file.
1649 Given that we have just obtained the filename from readdir,
1650 assume that it does exist and therefore treat this as a
1651 dangling symlink. */
1652 continue;
1653 }
1654 else
1655 {
1656 /* Some other file error; report the error. */
Eduardo Silva32ffb2b2019-04-25 10:43:26 -06001657 ret = MBEDTLS_ERR_X509_FILE_IO_ERROR;
1658 goto cleanup;
1659 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001660 }
1661
1662 if( !S_ISREG( sb.st_mode ) )
1663 continue;
1664
1665 // Ignore parse errors
1666 //
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001667 t_ret = mbedtls_x509_crt_parse_file( chain, entry_name );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001668 if( t_ret < 0 )
1669 ret++;
1670 else
1671 ret += t_ret;
1672 }
Manuel Pégourié-Gonnard5ad68e42013-11-28 17:11:54 +01001673
1674cleanup:
Andres AGf9113192016-09-02 14:06:04 +01001675 closedir( dir );
1676
Ron Eldor63140682017-01-09 19:27:59 +02001677#if defined(MBEDTLS_THREADING_C)
Manuel Pégourié-Gonnard944cfe82015-05-27 20:07:18 +02001678 if( mbedtls_mutex_unlock( &mbedtls_threading_readdir_mutex ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001679 ret = MBEDTLS_ERR_THREADING_MUTEX_ERROR;
Ron Eldor63140682017-01-09 19:27:59 +02001680#endif /* MBEDTLS_THREADING_C */
Manuel Pégourié-Gonnard5ad68e42013-11-28 17:11:54 +01001681
Paul Bakkerbe089b02013-10-14 15:51:50 +02001682#endif /* _WIN32 */
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001683
1684 return( ret );
1685}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001686#endif /* MBEDTLS_FS_IO */
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001687
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001688/*
1689 * OtherName ::= SEQUENCE {
1690 * type-id OBJECT IDENTIFIER,
1691 * value [0] EXPLICIT ANY DEFINED BY type-id }
1692 *
1693 * HardwareModuleName ::= SEQUENCE {
1694 * hwType OBJECT IDENTIFIER,
1695 * hwSerialNum OCTET STRING }
1696 *
1697 * NOTE: we currently only parse and use otherName of type HwModuleName,
1698 * as defined in RFC 4108.
1699 */
1700static int x509_get_other_name( const mbedtls_x509_buf *subject_alt_name,
1701 mbedtls_x509_san_other_name *other_name )
1702{
Janos Follathab23cd12019-05-09 13:53:57 +01001703 int ret = 0;
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001704 size_t len;
1705 unsigned char *p = subject_alt_name->p;
1706 const unsigned char *end = p + subject_alt_name->len;
1707 mbedtls_x509_buf cur_oid;
1708
1709 if( ( subject_alt_name->tag &
1710 ( MBEDTLS_ASN1_TAG_CLASS_MASK | MBEDTLS_ASN1_TAG_VALUE_MASK ) ) !=
1711 ( MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_X509_SAN_OTHER_NAME ) )
1712 {
1713 /*
1714 * The given subject alternative name is not of type "othername".
1715 */
1716 return( MBEDTLS_ERR_X509_BAD_INPUT_DATA );
1717 }
1718
1719 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
1720 MBEDTLS_ASN1_OID ) ) != 0 )
Chris Jones9f7a6932021-04-14 12:12:09 +01001721 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret ) );
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001722
1723 cur_oid.tag = MBEDTLS_ASN1_OID;
1724 cur_oid.p = p;
1725 cur_oid.len = len;
1726
1727 /*
1728 * Only HwModuleName is currently supported.
1729 */
1730 if( MBEDTLS_OID_CMP( MBEDTLS_OID_ON_HW_MODULE_NAME, &cur_oid ) != 0 )
1731 {
1732 return( MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE );
1733 }
1734
Ron Eldor890819a2019-05-13 19:03:04 +03001735 if( p + len >= end )
1736 {
Sébastien Duquette661d7252019-06-23 17:45:26 -04001737 mbedtls_platform_zeroize( other_name, sizeof( *other_name ) );
Chris Jones9f7a6932021-04-14 12:12:09 +01001738 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
1739 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH ) );
Ron Eldor890819a2019-05-13 19:03:04 +03001740 }
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001741 p += len;
1742 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
1743 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_CONTEXT_SPECIFIC ) ) != 0 )
Chris Jones9f7a6932021-04-14 12:12:09 +01001744 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret ) );
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001745
1746 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
1747 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
Chris Jones9f7a6932021-04-14 12:12:09 +01001748 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret ) );
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001749
1750 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len, MBEDTLS_ASN1_OID ) ) != 0 )
Chris Jones9f7a6932021-04-14 12:12:09 +01001751 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret ) );
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001752
1753 other_name->value.hardware_module_name.oid.tag = MBEDTLS_ASN1_OID;
1754 other_name->value.hardware_module_name.oid.p = p;
1755 other_name->value.hardware_module_name.oid.len = len;
1756
Ron Eldor890819a2019-05-13 19:03:04 +03001757 if( p + len >= end )
1758 {
Sébastien Duquette661d7252019-06-23 17:45:26 -04001759 mbedtls_platform_zeroize( other_name, sizeof( *other_name ) );
Chris Jones9f7a6932021-04-14 12:12:09 +01001760 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
1761 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH ) );
Ron Eldor890819a2019-05-13 19:03:04 +03001762 }
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001763 p += len;
1764 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
1765 MBEDTLS_ASN1_OCTET_STRING ) ) != 0 )
Chris Jones9f7a6932021-04-14 12:12:09 +01001766 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret ) );
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001767
1768 other_name->value.hardware_module_name.val.tag = MBEDTLS_ASN1_OCTET_STRING;
1769 other_name->value.hardware_module_name.val.p = p;
1770 other_name->value.hardware_module_name.val.len = len;
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001771 p += len;
1772 if( p != end )
1773 {
Ron Eldor890819a2019-05-13 19:03:04 +03001774 mbedtls_platform_zeroize( other_name,
Sébastien Duquette661d7252019-06-23 17:45:26 -04001775 sizeof( *other_name ) );
Chris Jones9f7a6932021-04-14 12:12:09 +01001776 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
1777 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH ) );
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001778 }
1779 return( 0 );
1780}
1781
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02001782static int x509_info_subject_alt_name( char **buf, size_t *size,
Janos Follath2f0ec1e2019-05-10 11:06:31 +01001783 const mbedtls_x509_sequence
1784 *subject_alt_name,
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001785 const char *prefix )
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02001786{
Janos Follath865b3eb2019-12-16 11:46:15 +00001787 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02001788 size_t n = *size;
1789 char *p = *buf;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001790 const mbedtls_x509_sequence *cur = subject_alt_name;
Ron Eldor890819a2019-05-13 19:03:04 +03001791 mbedtls_x509_subject_alternative_name san;
1792 int parse_ret;
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02001793
1794 while( cur != NULL )
1795 {
Ron Eldor890819a2019-05-13 19:03:04 +03001796 memset( &san, 0, sizeof( san ) );
1797 parse_ret = mbedtls_x509_parse_subject_alt_name( &cur->buf, &san );
1798 if( parse_ret != 0 )
1799 {
1800 if( parse_ret == MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE )
1801 {
1802 ret = mbedtls_snprintf( p, n, "\n%s <unsupported>", prefix );
1803 MBEDTLS_X509_SAFE_SNPRINTF;
1804 }
1805 else
1806 {
1807 ret = mbedtls_snprintf( p, n, "\n%s <malformed>", prefix );
1808 MBEDTLS_X509_SAFE_SNPRINTF;
1809 }
1810 cur = cur->next;
1811 continue;
1812 }
1813
1814 switch( san.type )
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02001815 {
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001816 /*
1817 * otherName
1818 */
Ron Eldora2913912019-05-16 16:17:38 +03001819 case MBEDTLS_X509_SAN_OTHER_NAME:
Ron Eldor890819a2019-05-13 19:03:04 +03001820 {
1821 mbedtls_x509_san_other_name *other_name = &san.san.other_name;
1822
1823 ret = mbedtls_snprintf( p, n, "\n%s otherName :", prefix );
1824 MBEDTLS_X509_SAFE_SNPRINTF;
1825
1826 if( MBEDTLS_OID_CMP( MBEDTLS_OID_ON_HW_MODULE_NAME,
1827 &other_name->value.hardware_module_name.oid ) != 0 )
Janos Follath22f605f2019-05-10 10:37:17 +01001828 {
Ron Eldor890819a2019-05-13 19:03:04 +03001829 ret = mbedtls_snprintf( p, n, "\n%s hardware module name :", prefix );
1830 MBEDTLS_X509_SAFE_SNPRINTF;
1831 ret = mbedtls_snprintf( p, n, "\n%s hardware type : ", prefix );
Janos Follath2f0ec1e2019-05-10 11:06:31 +01001832 MBEDTLS_X509_SAFE_SNPRINTF;
1833
Ron Eldor890819a2019-05-13 19:03:04 +03001834 ret = mbedtls_oid_get_numeric_string( p, n, &other_name->value.hardware_module_name.oid );
1835 MBEDTLS_X509_SAFE_SNPRINTF;
Janos Follath2f0ec1e2019-05-10 11:06:31 +01001836
Ron Eldor890819a2019-05-13 19:03:04 +03001837 ret = mbedtls_snprintf( p, n, "\n%s hardware serial number : ", prefix );
1838 MBEDTLS_X509_SAFE_SNPRINTF;
1839
1840 if( other_name->value.hardware_module_name.val.len >= n )
1841 {
1842 *p = '\0';
1843 return( MBEDTLS_ERR_X509_BUFFER_TOO_SMALL );
Janos Follath22f605f2019-05-10 10:37:17 +01001844 }
1845
Ron Eldora2913912019-05-16 16:17:38 +03001846 memcpy( p, other_name->value.hardware_module_name.val.p,
1847 other_name->value.hardware_module_name.val.len );
1848 p += other_name->value.hardware_module_name.val.len;
1849
Ron Eldor890819a2019-05-13 19:03:04 +03001850 n -= other_name->value.hardware_module_name.val.len;
Janos Follath2f0ec1e2019-05-10 11:06:31 +01001851
Ron Eldor890819a2019-05-13 19:03:04 +03001852 }/* MBEDTLS_OID_ON_HW_MODULE_NAME */
1853 }
1854 break;
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001855
1856 /*
1857 * dNSName
1858 */
Ron Eldora2913912019-05-16 16:17:38 +03001859 case MBEDTLS_X509_SAN_DNS_NAME:
Ron Eldor890819a2019-05-13 19:03:04 +03001860 {
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001861 ret = mbedtls_snprintf( p, n, "\n%s dNSName : ", prefix );
1862 MBEDTLS_X509_SAFE_SNPRINTF;
Ron Eldor890819a2019-05-13 19:03:04 +03001863 if( san.san.unstructured_name.len >= n )
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001864 {
1865 *p = '\0';
1866 return( MBEDTLS_ERR_X509_BUFFER_TOO_SMALL );
1867 }
Ron Eldora2913912019-05-16 16:17:38 +03001868
1869 memcpy( p, san.san.unstructured_name.p, san.san.unstructured_name.len );
1870 p += san.san.unstructured_name.len;
Ron Eldor890819a2019-05-13 19:03:04 +03001871 n -= san.san.unstructured_name.len;
Ron Eldor890819a2019-05-13 19:03:04 +03001872 }
1873 break;
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001874
1875 /*
Ron Eldor890819a2019-05-13 19:03:04 +03001876 * Type not supported, skip item.
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001877 */
1878 default:
Janos Follath22f605f2019-05-10 10:37:17 +01001879 ret = mbedtls_snprintf( p, n, "\n%s <unsupported>", prefix );
1880 MBEDTLS_X509_SAFE_SNPRINTF;
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001881 break;
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02001882 }
1883
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02001884 cur = cur->next;
1885 }
1886
1887 *p = '\0';
1888
1889 *size = n;
1890 *buf = p;
1891
1892 return( 0 );
1893}
1894
Ron Eldor890819a2019-05-13 19:03:04 +03001895int mbedtls_x509_parse_subject_alt_name( const mbedtls_x509_buf *san_buf,
1896 mbedtls_x509_subject_alternative_name *san )
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001897{
Janos Follath865b3eb2019-12-16 11:46:15 +00001898 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Ron Eldor890819a2019-05-13 19:03:04 +03001899 switch( san_buf->tag &
1900 ( MBEDTLS_ASN1_TAG_CLASS_MASK |
1901 MBEDTLS_ASN1_TAG_VALUE_MASK ) )
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001902 {
Ron Eldor890819a2019-05-13 19:03:04 +03001903 /*
1904 * otherName
1905 */
1906 case( MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_X509_SAN_OTHER_NAME ):
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001907 {
Ron Eldor890819a2019-05-13 19:03:04 +03001908 mbedtls_x509_san_other_name other_name;
1909
1910 ret = x509_get_other_name( san_buf, &other_name );
1911 if( ret != 0 )
1912 return( ret );
1913
1914 memset( san, 0, sizeof( mbedtls_x509_subject_alternative_name ) );
1915 san->type = MBEDTLS_X509_SAN_OTHER_NAME;
1916 memcpy( &san->san.other_name,
1917 &other_name, sizeof( other_name ) );
1918
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001919 }
Ron Eldor890819a2019-05-13 19:03:04 +03001920 break;
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001921
Ron Eldor890819a2019-05-13 19:03:04 +03001922 /*
1923 * dNSName
1924 */
1925 case( MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_X509_SAN_DNS_NAME ):
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001926 {
Ron Eldor890819a2019-05-13 19:03:04 +03001927 memset( san, 0, sizeof( mbedtls_x509_subject_alternative_name ) );
1928 san->type = MBEDTLS_X509_SAN_DNS_NAME;
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001929
Ron Eldor890819a2019-05-13 19:03:04 +03001930 memcpy( &san->san.unstructured_name,
1931 san_buf, sizeof( *san_buf ) );
Janos Follath6c379b42019-05-10 14:17:16 +01001932
Ron Eldor890819a2019-05-13 19:03:04 +03001933 }
1934 break;
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001935
Ron Eldor890819a2019-05-13 19:03:04 +03001936 /*
1937 * Type not supported
1938 */
1939 default:
1940 return( MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE );
1941 }
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001942 return( 0 );
1943}
1944
Manuel Pégourié-Gonnard0db29b02014-04-01 18:12:24 +02001945#define PRINT_ITEM(i) \
1946 { \
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001947 ret = mbedtls_snprintf( p, n, "%s" i, sep ); \
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001948 MBEDTLS_X509_SAFE_SNPRINTF; \
Manuel Pégourié-Gonnard0db29b02014-04-01 18:12:24 +02001949 sep = ", "; \
1950 }
1951
1952#define CERT_TYPE(type,name) \
Hanno Becker1eeca412018-10-15 12:01:35 +01001953 if( ns_cert_type & (type) ) \
Manuel Pégourié-Gonnard0db29b02014-04-01 18:12:24 +02001954 PRINT_ITEM( name );
1955
Manuel Pégourié-Gonnard919f8f52014-04-01 13:01:11 +02001956static int x509_info_cert_type( char **buf, size_t *size,
1957 unsigned char ns_cert_type )
1958{
Janos Follath865b3eb2019-12-16 11:46:15 +00001959 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard919f8f52014-04-01 13:01:11 +02001960 size_t n = *size;
1961 char *p = *buf;
Manuel Pégourié-Gonnard7b30cfc2014-04-01 18:00:07 +02001962 const char *sep = "";
Manuel Pégourié-Gonnard919f8f52014-04-01 13:01:11 +02001963
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001964 CERT_TYPE( MBEDTLS_X509_NS_CERT_TYPE_SSL_CLIENT, "SSL Client" );
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01001965 CERT_TYPE( MBEDTLS_X509_NS_CERT_TYPE_SSL_SERVER, "SSL Server" );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001966 CERT_TYPE( MBEDTLS_X509_NS_CERT_TYPE_EMAIL, "Email" );
1967 CERT_TYPE( MBEDTLS_X509_NS_CERT_TYPE_OBJECT_SIGNING, "Object Signing" );
1968 CERT_TYPE( MBEDTLS_X509_NS_CERT_TYPE_RESERVED, "Reserved" );
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01001969 CERT_TYPE( MBEDTLS_X509_NS_CERT_TYPE_SSL_CA, "SSL CA" );
1970 CERT_TYPE( MBEDTLS_X509_NS_CERT_TYPE_EMAIL_CA, "Email CA" );
1971 CERT_TYPE( MBEDTLS_X509_NS_CERT_TYPE_OBJECT_SIGNING_CA, "Object Signing CA" );
Manuel Pégourié-Gonnard919f8f52014-04-01 13:01:11 +02001972
1973 *size = n;
1974 *buf = p;
1975
1976 return( 0 );
1977}
1978
Manuel Pégourié-Gonnard0db29b02014-04-01 18:12:24 +02001979#define KEY_USAGE(code,name) \
Hanno Becker1eeca412018-10-15 12:01:35 +01001980 if( key_usage & (code) ) \
Manuel Pégourié-Gonnard0db29b02014-04-01 18:12:24 +02001981 PRINT_ITEM( name );
1982
Manuel Pégourié-Gonnard65c2ddc2014-04-01 14:12:11 +02001983static int x509_info_key_usage( char **buf, size_t *size,
Manuel Pégourié-Gonnard9a702252015-06-23 10:14:36 +02001984 unsigned int key_usage )
Manuel Pégourié-Gonnard65c2ddc2014-04-01 14:12:11 +02001985{
Janos Follath865b3eb2019-12-16 11:46:15 +00001986 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard65c2ddc2014-04-01 14:12:11 +02001987 size_t n = *size;
1988 char *p = *buf;
Manuel Pégourié-Gonnard7b30cfc2014-04-01 18:00:07 +02001989 const char *sep = "";
Manuel Pégourié-Gonnard65c2ddc2014-04-01 14:12:11 +02001990
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001991 KEY_USAGE( MBEDTLS_X509_KU_DIGITAL_SIGNATURE, "Digital Signature" );
1992 KEY_USAGE( MBEDTLS_X509_KU_NON_REPUDIATION, "Non Repudiation" );
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01001993 KEY_USAGE( MBEDTLS_X509_KU_KEY_ENCIPHERMENT, "Key Encipherment" );
1994 KEY_USAGE( MBEDTLS_X509_KU_DATA_ENCIPHERMENT, "Data Encipherment" );
1995 KEY_USAGE( MBEDTLS_X509_KU_KEY_AGREEMENT, "Key Agreement" );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001996 KEY_USAGE( MBEDTLS_X509_KU_KEY_CERT_SIGN, "Key Cert Sign" );
1997 KEY_USAGE( MBEDTLS_X509_KU_CRL_SIGN, "CRL Sign" );
Manuel Pégourié-Gonnard9a702252015-06-23 10:14:36 +02001998 KEY_USAGE( MBEDTLS_X509_KU_ENCIPHER_ONLY, "Encipher Only" );
1999 KEY_USAGE( MBEDTLS_X509_KU_DECIPHER_ONLY, "Decipher Only" );
Manuel Pégourié-Gonnard65c2ddc2014-04-01 14:12:11 +02002000
2001 *size = n;
2002 *buf = p;
2003
2004 return( 0 );
2005}
2006
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02002007static int x509_info_ext_key_usage( char **buf, size_t *size,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002008 const mbedtls_x509_sequence *extended_key_usage )
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02002009{
Janos Follath865b3eb2019-12-16 11:46:15 +00002010 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02002011 const char *desc;
2012 size_t n = *size;
2013 char *p = *buf;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002014 const mbedtls_x509_sequence *cur = extended_key_usage;
Manuel Pégourié-Gonnard7b30cfc2014-04-01 18:00:07 +02002015 const char *sep = "";
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02002016
2017 while( cur != NULL )
2018 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002019 if( mbedtls_oid_get_extended_key_usage( &cur->buf, &desc ) != 0 )
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02002020 desc = "???";
2021
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002022 ret = mbedtls_snprintf( p, n, "%s%s", sep, desc );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002023 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02002024
Manuel Pégourié-Gonnard7b30cfc2014-04-01 18:00:07 +02002025 sep = ", ";
2026
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02002027 cur = cur->next;
2028 }
2029
2030 *size = n;
2031 *buf = p;
2032
2033 return( 0 );
2034}
2035
Ron Eldor74d9acc2019-03-21 14:00:03 +02002036static int x509_info_cert_policies( char **buf, size_t *size,
2037 const mbedtls_x509_sequence *certificate_policies )
2038{
Janos Follath865b3eb2019-12-16 11:46:15 +00002039 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Ron Eldor74d9acc2019-03-21 14:00:03 +02002040 const char *desc;
2041 size_t n = *size;
2042 char *p = *buf;
2043 const mbedtls_x509_sequence *cur = certificate_policies;
2044 const char *sep = "";
2045
2046 while( cur != NULL )
2047 {
2048 if( mbedtls_oid_get_certificate_policies( &cur->buf, &desc ) != 0 )
2049 desc = "???";
2050
2051 ret = mbedtls_snprintf( p, n, "%s%s", sep, desc );
2052 MBEDTLS_X509_SAFE_SNPRINTF;
2053
2054 sep = ", ";
2055
2056 cur = cur->next;
2057 }
2058
2059 *size = n;
2060 *buf = p;
2061
2062 return( 0 );
2063}
2064
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002065/*
2066 * Return an informational string about the certificate.
2067 */
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002068#define BEFORE_COLON 18
2069#define BC "18"
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002070int mbedtls_x509_crt_info( char *buf, size_t size, const char *prefix,
2071 const mbedtls_x509_crt *crt )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002072{
Janos Follath865b3eb2019-12-16 11:46:15 +00002073 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002074 size_t n;
2075 char *p;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002076 char key_size_str[BEFORE_COLON];
2077
2078 p = buf;
2079 n = size;
2080
Janos Follath98e28a72016-05-31 14:03:54 +01002081 if( NULL == crt )
2082 {
2083 ret = mbedtls_snprintf( p, n, "\nCertificate is uninitialised!\n" );
2084 MBEDTLS_X509_SAFE_SNPRINTF;
2085
2086 return( (int) ( size - n ) );
2087 }
2088
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002089 ret = mbedtls_snprintf( p, n, "%scert. version : %d\n",
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002090 prefix, crt->version );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002091 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002092 ret = mbedtls_snprintf( p, n, "%sserial number : ",
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002093 prefix );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002094 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002095
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002096 ret = mbedtls_x509_serial_gets( p, n, &crt->serial );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002097 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002098
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002099 ret = mbedtls_snprintf( p, n, "\n%sissuer name : ", prefix );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002100 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002101 ret = mbedtls_x509_dn_gets( p, n, &crt->issuer );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002102 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002103
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002104 ret = mbedtls_snprintf( p, n, "\n%ssubject name : ", prefix );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002105 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002106 ret = mbedtls_x509_dn_gets( p, n, &crt->subject );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002107 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002108
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002109 ret = mbedtls_snprintf( p, n, "\n%sissued on : " \
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002110 "%04d-%02d-%02d %02d:%02d:%02d", prefix,
2111 crt->valid_from.year, crt->valid_from.mon,
2112 crt->valid_from.day, crt->valid_from.hour,
2113 crt->valid_from.min, crt->valid_from.sec );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002114 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002115
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002116 ret = mbedtls_snprintf( p, n, "\n%sexpires on : " \
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002117 "%04d-%02d-%02d %02d:%02d:%02d", prefix,
2118 crt->valid_to.year, crt->valid_to.mon,
2119 crt->valid_to.day, crt->valid_to.hour,
2120 crt->valid_to.min, crt->valid_to.sec );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002121 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002122
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002123 ret = mbedtls_snprintf( p, n, "\n%ssigned using : ", prefix );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002124 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002125
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002126 ret = mbedtls_x509_sig_alg_gets( p, n, &crt->sig_oid, crt->sig_pk,
Manuel Pégourié-Gonnardbf696d02014-06-05 17:07:30 +02002127 crt->sig_md, crt->sig_opts );
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 /* Key size */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002131 if( ( ret = mbedtls_x509_key_size_helper( key_size_str, BEFORE_COLON,
2132 mbedtls_pk_get_name( &crt->pk ) ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002133 {
2134 return( ret );
2135 }
2136
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002137 ret = mbedtls_snprintf( p, n, "\n%s%-" BC "s: %d bits", prefix, key_size_str,
Manuel Pégourié-Gonnard097c7bb2015-06-18 16:43:38 +02002138 (int) mbedtls_pk_get_bitlen( &crt->pk ) );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002139 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002140
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002141 /*
2142 * Optional extensions
2143 */
2144
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01002145 if( crt->ext_types & MBEDTLS_X509_EXT_BASIC_CONSTRAINTS )
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002146 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002147 ret = mbedtls_snprintf( p, n, "\n%sbasic constraints : CA=%s", prefix,
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002148 crt->ca_istrue ? "true" : "false" );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002149 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002150
2151 if( crt->max_pathlen > 0 )
2152 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002153 ret = mbedtls_snprintf( p, n, ", max_pathlen=%d", crt->max_pathlen - 1 );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002154 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002155 }
2156 }
2157
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01002158 if( crt->ext_types & MBEDTLS_X509_EXT_SUBJECT_ALT_NAME )
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002159 {
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02002160 ret = mbedtls_snprintf( p, n, "\n%ssubject alt name :", prefix );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002161 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02002162
2163 if( ( ret = x509_info_subject_alt_name( &p, &n,
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02002164 &crt->subject_alt_names,
Ron Eldor6aeae9e2019-05-20 12:00:36 +03002165 prefix ) ) != 0 )
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02002166 return( ret );
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002167 }
2168
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002169 if( crt->ext_types & MBEDTLS_X509_EXT_NS_CERT_TYPE )
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002170 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002171 ret = mbedtls_snprintf( p, n, "\n%scert. type : ", prefix );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002172 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnard919f8f52014-04-01 13:01:11 +02002173
2174 if( ( ret = x509_info_cert_type( &p, &n, crt->ns_cert_type ) ) != 0 )
2175 return( ret );
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002176 }
2177
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002178 if( crt->ext_types & MBEDTLS_X509_EXT_KEY_USAGE )
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002179 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002180 ret = mbedtls_snprintf( p, n, "\n%skey usage : ", prefix );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002181 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnard65c2ddc2014-04-01 14:12:11 +02002182
2183 if( ( ret = x509_info_key_usage( &p, &n, crt->key_usage ) ) != 0 )
2184 return( ret );
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002185 }
2186
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002187 if( crt->ext_types & MBEDTLS_X509_EXT_EXTENDED_KEY_USAGE )
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002188 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002189 ret = mbedtls_snprintf( p, n, "\n%sext key usage : ", prefix );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002190 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02002191
2192 if( ( ret = x509_info_ext_key_usage( &p, &n,
2193 &crt->ext_key_usage ) ) != 0 )
2194 return( ret );
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002195 }
2196
Ron Eldor74d9acc2019-03-21 14:00:03 +02002197 if( crt->ext_types & MBEDTLS_OID_X509_EXT_CERTIFICATE_POLICIES )
2198 {
2199 ret = mbedtls_snprintf( p, n, "\n%scertificate policies : ", prefix );
2200 MBEDTLS_X509_SAFE_SNPRINTF;
2201
2202 if( ( ret = x509_info_cert_policies( &p, &n,
2203 &crt->certificate_policies ) ) != 0 )
2204 return( ret );
2205 }
2206
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002207 ret = mbedtls_snprintf( p, n, "\n" );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002208 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002209
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002210 return( (int) ( size - n ) );
2211}
2212
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01002213struct x509_crt_verify_string {
2214 int code;
2215 const char *string;
2216};
2217
2218static const struct x509_crt_verify_string x509_crt_verify_strings[] = {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01002219 { MBEDTLS_X509_BADCERT_EXPIRED, "The certificate validity has expired" },
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01002220 { MBEDTLS_X509_BADCERT_REVOKED, "The certificate has been revoked (is on a CRL)" },
2221 { MBEDTLS_X509_BADCERT_CN_MISMATCH, "The certificate Common Name (CN) does not match with the expected CN" },
2222 { MBEDTLS_X509_BADCERT_NOT_TRUSTED, "The certificate is not correctly signed by the trusted CA" },
2223 { MBEDTLS_X509_BADCRL_NOT_TRUSTED, "The CRL is not correctly signed by the trusted CA" },
2224 { MBEDTLS_X509_BADCRL_EXPIRED, "The CRL is expired" },
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01002225 { MBEDTLS_X509_BADCERT_MISSING, "Certificate was missing" },
2226 { MBEDTLS_X509_BADCERT_SKIP_VERIFY, "Certificate verification was skipped" },
2227 { MBEDTLS_X509_BADCERT_OTHER, "Other reason (can be used by verify callback)" },
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01002228 { MBEDTLS_X509_BADCERT_FUTURE, "The certificate validity starts in the future" },
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01002229 { MBEDTLS_X509_BADCRL_FUTURE, "The CRL is from the future" },
2230 { MBEDTLS_X509_BADCERT_KEY_USAGE, "Usage does not match the keyUsage extension" },
2231 { MBEDTLS_X509_BADCERT_EXT_KEY_USAGE, "Usage does not match the extendedKeyUsage extension" },
2232 { MBEDTLS_X509_BADCERT_NS_CERT_TYPE, "Usage does not match the nsCertType extension" },
Manuel Pégourié-Gonnard95051642015-06-15 10:39:46 +02002233 { MBEDTLS_X509_BADCERT_BAD_MD, "The certificate is signed with an unacceptable hash." },
2234 { MBEDTLS_X509_BADCERT_BAD_PK, "The certificate is signed with an unacceptable PK alg (eg RSA vs ECDSA)." },
2235 { MBEDTLS_X509_BADCERT_BAD_KEY, "The certificate is signed with an unacceptable key (eg bad curve, RSA too short)." },
2236 { MBEDTLS_X509_BADCRL_BAD_MD, "The CRL is signed with an unacceptable hash." },
2237 { MBEDTLS_X509_BADCRL_BAD_PK, "The CRL is signed with an unacceptable PK alg (eg RSA vs ECDSA)." },
2238 { MBEDTLS_X509_BADCRL_BAD_KEY, "The CRL is signed with an unacceptable key (eg bad curve, RSA too short)." },
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01002239 { 0, NULL }
2240};
2241
2242int mbedtls_x509_crt_verify_info( char *buf, size_t size, const char *prefix,
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02002243 uint32_t flags )
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01002244{
Janos Follath865b3eb2019-12-16 11:46:15 +00002245 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01002246 const struct x509_crt_verify_string *cur;
2247 char *p = buf;
2248 size_t n = size;
2249
2250 for( cur = x509_crt_verify_strings; cur->string != NULL ; cur++ )
2251 {
2252 if( ( flags & cur->code ) == 0 )
2253 continue;
2254
2255 ret = mbedtls_snprintf( p, n, "%s%s\n", prefix, cur->string );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002256 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01002257 flags ^= cur->code;
2258 }
2259
2260 if( flags != 0 )
2261 {
2262 ret = mbedtls_snprintf( p, n, "%sUnknown reason "
2263 "(this should not happen)\n", prefix );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002264 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01002265 }
2266
2267 return( (int) ( size - n ) );
2268}
2269
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002270#if defined(MBEDTLS_X509_CHECK_KEY_USAGE)
Manuel Pégourié-Gonnard655a9642015-06-23 10:48:44 +02002271int mbedtls_x509_crt_check_key_usage( const mbedtls_x509_crt *crt,
2272 unsigned int usage )
Manuel Pégourié-Gonnard603116c2014-04-09 09:50:03 +02002273{
Manuel Pégourié-Gonnard655a9642015-06-23 10:48:44 +02002274 unsigned int usage_must, usage_may;
2275 unsigned int may_mask = MBEDTLS_X509_KU_ENCIPHER_ONLY
2276 | MBEDTLS_X509_KU_DECIPHER_ONLY;
2277
2278 if( ( crt->ext_types & MBEDTLS_X509_EXT_KEY_USAGE ) == 0 )
2279 return( 0 );
2280
2281 usage_must = usage & ~may_mask;
2282
2283 if( ( ( crt->key_usage & ~may_mask ) & usage_must ) != usage_must )
2284 return( MBEDTLS_ERR_X509_BAD_INPUT_DATA );
2285
2286 usage_may = usage & may_mask;
2287
2288 if( ( ( crt->key_usage & may_mask ) | usage_may ) != usage_may )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002289 return( MBEDTLS_ERR_X509_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard603116c2014-04-09 09:50:03 +02002290
2291 return( 0 );
2292}
2293#endif
2294
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002295#if defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE)
2296int mbedtls_x509_crt_check_extended_key_usage( const mbedtls_x509_crt *crt,
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02002297 const char *usage_oid,
2298 size_t usage_len )
2299{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002300 const mbedtls_x509_sequence *cur;
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02002301
2302 /* Extension is not mandatory, absent means no restriction */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002303 if( ( crt->ext_types & MBEDTLS_X509_EXT_EXTENDED_KEY_USAGE ) == 0 )
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02002304 return( 0 );
2305
2306 /*
2307 * Look for the requested usage (or wildcard ANY) in our list
2308 */
2309 for( cur = &crt->ext_key_usage; cur != NULL; cur = cur->next )
2310 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002311 const mbedtls_x509_buf *cur_oid = &cur->buf;
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02002312
2313 if( cur_oid->len == usage_len &&
2314 memcmp( cur_oid->p, usage_oid, usage_len ) == 0 )
2315 {
2316 return( 0 );
2317 }
2318
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002319 if( MBEDTLS_OID_CMP( MBEDTLS_OID_ANY_EXTENDED_KEY_USAGE, cur_oid ) == 0 )
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02002320 return( 0 );
2321 }
2322
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002323 return( MBEDTLS_ERR_X509_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02002324}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002325#endif /* MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE */
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02002326
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002327#if defined(MBEDTLS_X509_CRL_PARSE_C)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002328/*
2329 * Return 1 if the certificate is revoked, or 0 otherwise.
2330 */
Manuel Pégourié-Gonnardc730ed32015-06-02 10:38:50 +01002331int mbedtls_x509_crt_is_revoked( const mbedtls_x509_crt *crt, const mbedtls_x509_crl *crl )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002332{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002333 const mbedtls_x509_crl_entry *cur = &crl->entry;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002334
2335 while( cur != NULL && cur->serial.len != 0 )
2336 {
2337 if( crt->serial.len == cur->serial.len &&
2338 memcmp( crt->serial.p, cur->serial.p, crt->serial.len ) == 0 )
2339 {
Raoul Strackxa4e86142020-06-15 17:03:13 +02002340 return( 1 );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002341 }
2342
2343 cur = cur->next;
2344 }
2345
2346 return( 0 );
2347}
2348
2349/*
Manuel Pégourié-Gonnardeeef9472016-02-22 11:36:55 +01002350 * Check that the given certificate is not revoked according to the CRL.
Manuel Pégourié-Gonnard08eacec2017-10-18 14:20:24 +02002351 * Skip validation if no CRL for the given CA is present.
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002352 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002353static int x509_crt_verifycrl( mbedtls_x509_crt *crt, mbedtls_x509_crt *ca,
Manuel Pégourié-Gonnard95051642015-06-15 10:39:46 +02002354 mbedtls_x509_crl *crl_list,
2355 const mbedtls_x509_crt_profile *profile )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002356{
2357 int flags = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002358 unsigned char hash[MBEDTLS_MD_MAX_SIZE];
2359 const mbedtls_md_info_t *md_info;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002360
2361 if( ca == NULL )
2362 return( flags );
2363
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002364 while( crl_list != NULL )
2365 {
2366 if( crl_list->version == 0 ||
Hanno Beckerb75ffb52018-11-02 09:19:54 +00002367 x509_name_cmp( &crl_list->issuer, &ca->subject ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002368 {
2369 crl_list = crl_list->next;
2370 continue;
2371 }
2372
2373 /*
Manuel Pégourié-Gonnard99d4f192014-04-08 15:10:07 +02002374 * Check if the CA is configured to sign CRLs
2375 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002376#if defined(MBEDTLS_X509_CHECK_KEY_USAGE)
Hanno Beckerb75ffb52018-11-02 09:19:54 +00002377 if( mbedtls_x509_crt_check_key_usage( ca,
2378 MBEDTLS_X509_KU_CRL_SIGN ) != 0 )
Manuel Pégourié-Gonnard99d4f192014-04-08 15:10:07 +02002379 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002380 flags |= MBEDTLS_X509_BADCRL_NOT_TRUSTED;
Manuel Pégourié-Gonnard99d4f192014-04-08 15:10:07 +02002381 break;
2382 }
2383#endif
2384
2385 /*
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002386 * Check if CRL is correctly signed by the trusted CA
2387 */
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +02002388 if( x509_profile_check_md_alg( profile, crl_list->sig_md ) != 0 )
2389 flags |= MBEDTLS_X509_BADCRL_BAD_MD;
2390
2391 if( x509_profile_check_pk_alg( profile, crl_list->sig_pk ) != 0 )
2392 flags |= MBEDTLS_X509_BADCRL_BAD_PK;
2393
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002394 md_info = mbedtls_md_info_from_type( crl_list->sig_md );
Manuel Pégourié-Gonnard329e78c2017-06-26 12:22:17 +02002395 if( mbedtls_md( md_info, crl_list->tbs.p, crl_list->tbs.len, hash ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002396 {
Manuel Pégourié-Gonnard329e78c2017-06-26 12:22:17 +02002397 /* Note: this can't happen except after an internal error */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002398 flags |= MBEDTLS_X509_BADCRL_NOT_TRUSTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002399 break;
2400 }
2401
Manuel Pégourié-Gonnard3f816912017-10-26 10:24:16 +02002402 if( x509_profile_check_key( profile, &ca->pk ) != 0 )
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +02002403 flags |= MBEDTLS_X509_BADCERT_BAD_KEY;
Manuel Pégourié-Gonnard95051642015-06-15 10:39:46 +02002404
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002405 if( mbedtls_pk_verify_ext( crl_list->sig_pk, crl_list->sig_opts, &ca->pk,
2406 crl_list->sig_md, hash, mbedtls_md_get_size( md_info ),
Manuel Pégourié-Gonnard53882022014-06-05 17:53:52 +02002407 crl_list->sig.p, crl_list->sig.len ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002408 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002409 flags |= MBEDTLS_X509_BADCRL_NOT_TRUSTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002410 break;
2411 }
2412
2413 /*
2414 * Check for validity of CRL (Do not drop out)
2415 */
Manuel Pégourié-Gonnardc730ed32015-06-02 10:38:50 +01002416 if( mbedtls_x509_time_is_past( &crl_list->next_update ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002417 flags |= MBEDTLS_X509_BADCRL_EXPIRED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002418
Manuel Pégourié-Gonnardc730ed32015-06-02 10:38:50 +01002419 if( mbedtls_x509_time_is_future( &crl_list->this_update ) )
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01002420 flags |= MBEDTLS_X509_BADCRL_FUTURE;
Manuel Pégourié-Gonnard95337652014-03-10 13:15:18 +01002421
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002422 /*
2423 * Check if certificate is revoked
2424 */
Manuel Pégourié-Gonnardc730ed32015-06-02 10:38:50 +01002425 if( mbedtls_x509_crt_is_revoked( crt, crl_list ) )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002426 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002427 flags |= MBEDTLS_X509_BADCERT_REVOKED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002428 break;
2429 }
2430
2431 crl_list = crl_list->next;
2432 }
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +02002433
Paul Bakkerd8bb8262014-06-17 14:06:49 +02002434 return( flags );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002435}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002436#endif /* MBEDTLS_X509_CRL_PARSE_C */
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002437
Manuel Pégourié-Gonnard88421242014-10-17 11:36:18 +02002438/*
Manuel Pégourié-Gonnardf82a4d52017-07-03 19:26:25 +02002439 * Check the signature of a certificate by its parent
2440 */
2441static int x509_crt_check_signature( const mbedtls_x509_crt *child,
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002442 mbedtls_x509_crt *parent,
2443 mbedtls_x509_crt_restart_ctx *rs_ctx )
Manuel Pégourié-Gonnardf82a4d52017-07-03 19:26:25 +02002444{
Manuel Pégourié-Gonnardf82a4d52017-07-03 19:26:25 +02002445 unsigned char hash[MBEDTLS_MD_MAX_SIZE];
Andrzej Kurekd4a65532018-10-31 06:18:39 -04002446 size_t hash_len;
2447#if !defined(MBEDTLS_USE_PSA_CRYPTO)
2448 const mbedtls_md_info_t *md_info;
Manuel Pégourié-Gonnardf82a4d52017-07-03 19:26:25 +02002449 md_info = mbedtls_md_info_from_type( child->sig_md );
Andrzej Kurekd4a65532018-10-31 06:18:39 -04002450 hash_len = mbedtls_md_get_size( md_info );
Andrzej Kurek8b38ff52018-11-20 03:20:09 -05002451
Andrzej Kurekd4a65532018-10-31 06:18:39 -04002452 /* Note: hash errors can happen only after an internal error */
Manuel Pégourié-Gonnardf82a4d52017-07-03 19:26:25 +02002453 if( mbedtls_md( md_info, child->tbs.p, child->tbs.len, hash ) != 0 )
Andrzej Kurekd4a65532018-10-31 06:18:39 -04002454 return( -1 );
2455#else
Jaeden Amero34973232019-02-20 10:32:28 +00002456 psa_hash_operation_t hash_operation = PSA_HASH_OPERATION_INIT;
Andrzej Kurekd4a65532018-10-31 06:18:39 -04002457 psa_algorithm_t hash_alg = mbedtls_psa_translate_md( child->sig_md );
2458
2459 if( psa_hash_setup( &hash_operation, hash_alg ) != PSA_SUCCESS )
2460 return( -1 );
2461
2462 if( psa_hash_update( &hash_operation, child->tbs.p, child->tbs.len )
2463 != PSA_SUCCESS )
Manuel Pégourié-Gonnardf82a4d52017-07-03 19:26:25 +02002464 {
Manuel Pégourié-Gonnardf82a4d52017-07-03 19:26:25 +02002465 return( -1 );
2466 }
2467
Andrzej Kurekd4a65532018-10-31 06:18:39 -04002468 if( psa_hash_finish( &hash_operation, hash, sizeof( hash ), &hash_len )
2469 != PSA_SUCCESS )
2470 {
Andrzej Kurekd4a65532018-10-31 06:18:39 -04002471 return( -1 );
2472 }
2473#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002474 /* Skip expensive computation on obvious mismatch */
2475 if( ! mbedtls_pk_can_do( &parent->pk, child->sig_pk ) )
Manuel Pégourié-Gonnardf82a4d52017-07-03 19:26:25 +02002476 return( -1 );
Manuel Pégourié-Gonnardf82a4d52017-07-03 19:26:25 +02002477
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002478#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002479 if( rs_ctx != NULL && child->sig_pk == MBEDTLS_PK_ECDSA )
2480 {
2481 return( mbedtls_pk_verify_restartable( &parent->pk,
Andrzej Kurekd4a65532018-10-31 06:18:39 -04002482 child->sig_md, hash, hash_len,
Manuel Pégourié-Gonnard15d7df22017-08-17 14:33:31 +02002483 child->sig.p, child->sig.len, &rs_ctx->pk ) );
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002484 }
2485#else
2486 (void) rs_ctx;
2487#endif
2488
2489 return( mbedtls_pk_verify_ext( child->sig_pk, child->sig_opts, &parent->pk,
Andrzej Kurekd4a65532018-10-31 06:18:39 -04002490 child->sig_md, hash, hash_len,
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002491 child->sig.p, child->sig.len ) );
Manuel Pégourié-Gonnardf82a4d52017-07-03 19:26:25 +02002492}
2493
2494/*
Manuel Pégourié-Gonnard312010e2014-04-09 14:30:11 +02002495 * Check if 'parent' is a suitable parent (signing CA) for 'child'.
2496 * Return 0 if yes, -1 if not.
Manuel Pégourié-Gonnardd249b7a2014-06-24 11:49:16 +02002497 *
2498 * top means parent is a locally-trusted certificate
Manuel Pégourié-Gonnard3fed0b32014-04-08 13:18:01 +02002499 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002500static int x509_crt_check_parent( const mbedtls_x509_crt *child,
2501 const mbedtls_x509_crt *parent,
Manuel Pégourié-Gonnard27e94792017-07-04 00:49:31 +02002502 int top )
Manuel Pégourié-Gonnard3fed0b32014-04-08 13:18:01 +02002503{
Manuel Pégourié-Gonnardd249b7a2014-06-24 11:49:16 +02002504 int need_ca_bit;
2505
Manuel Pégourié-Gonnardc4eff162014-06-19 12:18:08 +02002506 /* Parent must be the issuer */
Manuel Pégourié-Gonnardef9a6ae2014-10-17 12:25:12 +02002507 if( x509_name_cmp( &child->issuer, &parent->subject ) != 0 )
Manuel Pégourié-Gonnard312010e2014-04-09 14:30:11 +02002508 return( -1 );
Manuel Pégourié-Gonnard3fed0b32014-04-08 13:18:01 +02002509
Manuel Pégourié-Gonnardd249b7a2014-06-24 11:49:16 +02002510 /* Parent must have the basicConstraints CA bit set as a general rule */
2511 need_ca_bit = 1;
2512
2513 /* Exception: v1/v2 certificates that are locally trusted. */
2514 if( top && parent->version < 3 )
2515 need_ca_bit = 0;
2516
Manuel Pégourié-Gonnardd249b7a2014-06-24 11:49:16 +02002517 if( need_ca_bit && ! parent->ca_istrue )
2518 return( -1 );
2519
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002520#if defined(MBEDTLS_X509_CHECK_KEY_USAGE)
Manuel Pégourié-Gonnardd249b7a2014-06-24 11:49:16 +02002521 if( need_ca_bit &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002522 mbedtls_x509_crt_check_key_usage( parent, MBEDTLS_X509_KU_KEY_CERT_SIGN ) != 0 )
Manuel Pégourié-Gonnardc4eff162014-06-19 12:18:08 +02002523 {
2524 return( -1 );
2525 }
Manuel Pégourié-Gonnard312010e2014-04-09 14:30:11 +02002526#endif
2527
2528 return( 0 );
Manuel Pégourié-Gonnard3fed0b32014-04-08 13:18:01 +02002529}
2530
Manuel Pégourié-Gonnard35407c72017-06-29 10:45:25 +02002531/*
Manuel Pégourié-Gonnard3e329b82017-06-29 12:55:27 +02002532 * Find a suitable parent for child in candidates, or return NULL.
2533 *
2534 * Here suitable is defined as:
Manuel Pégourié-Gonnard2f09d592017-07-03 18:30:43 +02002535 * 1. subject name matches child's issuer
2536 * 2. if necessary, the CA bit is set and key usage allows signing certs
2537 * 3. for trusted roots, the signature is correct
Manuel Pégourié-Gonnardbe4ff422017-07-14 12:04:14 +02002538 * (for intermediates, the signature is checked and the result reported)
Manuel Pégourié-Gonnard2f09d592017-07-03 18:30:43 +02002539 * 4. pathlen constraints are satisfied
Manuel Pégourié-Gonnard3e329b82017-06-29 12:55:27 +02002540 *
Manuel Pégourié-Gonnard562df402017-08-08 18:09:14 +02002541 * If there's a suitable candidate which is also time-valid, return the first
2542 * such. Otherwise, return the first suitable candidate (or NULL if there is
2543 * none).
Manuel Pégourié-Gonnard3e329b82017-06-29 12:55:27 +02002544 *
2545 * The rationale for this rule is that someone could have a list of trusted
2546 * roots with two versions on the same root with different validity periods.
2547 * (At least one user reported having such a list and wanted it to just work.)
2548 * The reason we don't just require time-validity is that generally there is
2549 * only one version, and if it's expired we want the flags to state that
2550 * rather than NOT_TRUSTED, as would be the case if we required it here.
Manuel Pégourié-Gonnard2f09d592017-07-03 18:30:43 +02002551 *
2552 * The rationale for rule 3 (signature for trusted roots) is that users might
2553 * have two versions of the same CA with different keys in their list, and the
Manuel Pégourié-Gonnardbdc54402017-07-04 00:33:39 +02002554 * way we select the correct one is by checking the signature (as we don't
2555 * rely on key identifier extensions). (This is one way users might choose to
2556 * handle key rollover, another relies on self-issued certs, see [SIRO].)
Manuel Pégourié-Gonnard98a67782017-08-17 10:52:20 +02002557 *
2558 * Arguments:
Manuel Pégourié-Gonnarde57d7432018-03-07 10:00:57 +01002559 * - [in] child: certificate for which we're looking for a parent
2560 * - [in] candidates: chained list of potential parents
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +02002561 * - [out] r_parent: parent found (or NULL)
2562 * - [out] r_signature_is_good: 1 if child signature by parent is valid, or 0
Manuel Pégourié-Gonnarde57d7432018-03-07 10:00:57 +01002563 * - [in] top: 1 if candidates consists of trusted roots, ie we're at the top
2564 * of the chain, 0 otherwise
2565 * - [in] path_cnt: number of intermediates seen so far
2566 * - [in] self_cnt: number of self-signed intermediates seen so far
2567 * (will never be greater than path_cnt)
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +02002568 * - [in-out] rs_ctx: context for restarting operations
Manuel Pégourié-Gonnarde57d7432018-03-07 10:00:57 +01002569 *
2570 * Return value:
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +02002571 * - 0 on success
2572 * - MBEDTLS_ERR_ECP_IN_PROGRESS otherwise
Manuel Pégourié-Gonnard2f1c33d2017-06-29 12:27:23 +02002573 */
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002574static int x509_crt_find_parent_in(
2575 mbedtls_x509_crt *child,
2576 mbedtls_x509_crt *candidates,
2577 mbedtls_x509_crt **r_parent,
2578 int *r_signature_is_good,
2579 int top,
Manuel Pégourié-Gonnardbb216bd2017-08-28 13:25:55 +02002580 unsigned path_cnt,
2581 unsigned self_cnt,
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002582 mbedtls_x509_crt_restart_ctx *rs_ctx )
Manuel Pégourié-Gonnard2f1c33d2017-06-29 12:27:23 +02002583{
Janos Follath865b3eb2019-12-16 11:46:15 +00002584 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002585 mbedtls_x509_crt *parent, *fallback_parent;
Benjamin Kier36050732019-05-30 14:49:17 -04002586 int signature_is_good = 0, fallback_signature_is_good;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002587
2588#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard3627a8b2017-08-23 11:20:48 +02002589 /* did we have something in progress? */
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002590 if( rs_ctx != NULL && rs_ctx->parent != NULL )
2591 {
Manuel Pégourié-Gonnard3627a8b2017-08-23 11:20:48 +02002592 /* restore saved state */
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002593 parent = rs_ctx->parent;
2594 fallback_parent = rs_ctx->fallback_parent;
Manuel Pégourié-Gonnard78d7e8c2018-07-02 12:33:14 +02002595 fallback_signature_is_good = rs_ctx->fallback_signature_is_good;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002596
Manuel Pégourié-Gonnard3627a8b2017-08-23 11:20:48 +02002597 /* clear saved state */
2598 rs_ctx->parent = NULL;
2599 rs_ctx->fallback_parent = NULL;
Manuel Pégourié-Gonnard78d7e8c2018-07-02 12:33:14 +02002600 rs_ctx->fallback_signature_is_good = 0;
Manuel Pégourié-Gonnard3627a8b2017-08-23 11:20:48 +02002601
2602 /* resume where we left */
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002603 goto check_signature;
2604 }
2605#endif
2606
2607 fallback_parent = NULL;
Manuel Pégourié-Gonnard78d7e8c2018-07-02 12:33:14 +02002608 fallback_signature_is_good = 0;
Manuel Pégourié-Gonnard2f1c33d2017-06-29 12:27:23 +02002609
Manuel Pégourié-Gonnard2f1c33d2017-06-29 12:27:23 +02002610 for( parent = candidates; parent != NULL; parent = parent->next )
2611 {
Manuel Pégourié-Gonnard2f09d592017-07-03 18:30:43 +02002612 /* basic parenting skills (name, CA bit, key usage) */
Manuel Pégourié-Gonnard27e94792017-07-04 00:49:31 +02002613 if( x509_crt_check_parent( child, parent, top ) != 0 )
Manuel Pégourié-Gonnard2f1c33d2017-06-29 12:27:23 +02002614 continue;
2615
Manuel Pégourié-Gonnard9c6118c2017-06-29 12:38:42 +02002616 /* +1 because stored max_pathlen is 1 higher that the actual value */
2617 if( parent->max_pathlen > 0 &&
Manuel Pégourié-Gonnardbb216bd2017-08-28 13:25:55 +02002618 (size_t) parent->max_pathlen < 1 + path_cnt - self_cnt )
Manuel Pégourié-Gonnard9c6118c2017-06-29 12:38:42 +02002619 {
2620 continue;
2621 }
2622
Manuel Pégourié-Gonnard2f09d592017-07-03 18:30:43 +02002623 /* Signature */
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002624#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
2625check_signature:
2626#endif
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002627 ret = x509_crt_check_signature( child, parent, rs_ctx );
2628
2629#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002630 if( rs_ctx != NULL && ret == MBEDTLS_ERR_ECP_IN_PROGRESS )
2631 {
2632 /* save state */
2633 rs_ctx->parent = parent;
2634 rs_ctx->fallback_parent = fallback_parent;
Manuel Pégourié-Gonnard78d7e8c2018-07-02 12:33:14 +02002635 rs_ctx->fallback_signature_is_good = fallback_signature_is_good;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002636
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002637 return( ret );
2638 }
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002639#else
2640 (void) ret;
2641#endif
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002642
2643 signature_is_good = ret == 0;
2644 if( top && ! signature_is_good )
Manuel Pégourié-Gonnardf82a4d52017-07-03 19:26:25 +02002645 continue;
Manuel Pégourié-Gonnard2f09d592017-07-03 18:30:43 +02002646
Manuel Pégourié-Gonnard562df402017-08-08 18:09:14 +02002647 /* optional time check */
Manuel Pégourié-Gonnard3e329b82017-06-29 12:55:27 +02002648 if( mbedtls_x509_time_is_past( &parent->valid_to ) ||
2649 mbedtls_x509_time_is_future( &parent->valid_from ) )
2650 {
Manuel Pégourié-Gonnardbe4ff422017-07-14 12:04:14 +02002651 if( fallback_parent == NULL )
2652 {
2653 fallback_parent = parent;
Manuel Pégourié-Gonnard78d7e8c2018-07-02 12:33:14 +02002654 fallback_signature_is_good = signature_is_good;
Manuel Pégourié-Gonnardbe4ff422017-07-14 12:04:14 +02002655 }
Manuel Pégourié-Gonnard3e329b82017-06-29 12:55:27 +02002656
2657 continue;
2658 }
2659
Andy Gross1f627142019-01-30 10:25:53 -06002660 *r_parent = parent;
2661 *r_signature_is_good = signature_is_good;
2662
Manuel Pégourié-Gonnard2f1c33d2017-06-29 12:27:23 +02002663 break;
2664 }
2665
Andy Gross1f627142019-01-30 10:25:53 -06002666 if( parent == NULL )
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002667 {
2668 *r_parent = fallback_parent;
Manuel Pégourié-Gonnard78d7e8c2018-07-02 12:33:14 +02002669 *r_signature_is_good = fallback_signature_is_good;
Manuel Pégourié-Gonnardbe4ff422017-07-14 12:04:14 +02002670 }
Manuel Pégourié-Gonnard3e329b82017-06-29 12:55:27 +02002671
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002672 return( 0 );
Manuel Pégourié-Gonnard2f1c33d2017-06-29 12:27:23 +02002673}
2674
2675/*
Manuel Pégourié-Gonnard63686122017-07-04 01:01:39 +02002676 * Find a parent in trusted CAs or the provided chain, or return NULL.
2677 *
2678 * Searches in trusted CAs first, and return the first suitable parent found
2679 * (see find_parent_in() for definition of suitable).
Manuel Pégourié-Gonnard98a67782017-08-17 10:52:20 +02002680 *
2681 * Arguments:
Manuel Pégourié-Gonnarde57d7432018-03-07 10:00:57 +01002682 * - [in] child: certificate for which we're looking for a parent, followed
2683 * by a chain of possible intermediates
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +02002684 * - [in] trust_ca: list of locally trusted certificates
2685 * - [out] parent: parent found (or NULL)
2686 * - [out] parent_is_trusted: 1 if returned `parent` is trusted, or 0
2687 * - [out] signature_is_good: 1 if child signature by parent is valid, or 0
2688 * - [in] path_cnt: number of links in the chain so far (EE -> ... -> child)
2689 * - [in] self_cnt: number of self-signed certs in the chain so far
Manuel Pégourié-Gonnarde57d7432018-03-07 10:00:57 +01002690 * (will always be no greater than path_cnt)
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +02002691 * - [in-out] rs_ctx: context for restarting operations
Manuel Pégourié-Gonnarde57d7432018-03-07 10:00:57 +01002692 *
2693 * Return value:
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +02002694 * - 0 on success
2695 * - MBEDTLS_ERR_ECP_IN_PROGRESS otherwise
Manuel Pégourié-Gonnard63686122017-07-04 01:01:39 +02002696 */
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002697static int x509_crt_find_parent(
2698 mbedtls_x509_crt *child,
2699 mbedtls_x509_crt *trust_ca,
2700 mbedtls_x509_crt **parent,
2701 int *parent_is_trusted,
2702 int *signature_is_good,
Manuel Pégourié-Gonnardbb216bd2017-08-28 13:25:55 +02002703 unsigned path_cnt,
2704 unsigned self_cnt,
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002705 mbedtls_x509_crt_restart_ctx *rs_ctx )
Manuel Pégourié-Gonnard63686122017-07-04 01:01:39 +02002706{
Janos Follath865b3eb2019-12-16 11:46:15 +00002707 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard18547b52017-08-14 16:11:43 +02002708 mbedtls_x509_crt *search_list;
Manuel Pégourié-Gonnard63686122017-07-04 01:01:39 +02002709
Manuel Pégourié-Gonnard63686122017-07-04 01:01:39 +02002710 *parent_is_trusted = 1;
Manuel Pégourié-Gonnard18547b52017-08-14 16:11:43 +02002711
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002712#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard3627a8b2017-08-23 11:20:48 +02002713 /* restore then clear saved state if we have some stored */
2714 if( rs_ctx != NULL && rs_ctx->parent_is_trusted != -1 )
2715 {
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002716 *parent_is_trusted = rs_ctx->parent_is_trusted;
Manuel Pégourié-Gonnard3627a8b2017-08-23 11:20:48 +02002717 rs_ctx->parent_is_trusted = -1;
2718 }
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002719#endif
2720
Manuel Pégourié-Gonnard18547b52017-08-14 16:11:43 +02002721 while( 1 ) {
2722 search_list = *parent_is_trusted ? trust_ca : child->next;
2723
2724 ret = x509_crt_find_parent_in( child, search_list,
2725 parent, signature_is_good,
2726 *parent_is_trusted,
2727 path_cnt, self_cnt, rs_ctx );
Manuel Pégourié-Gonnard63686122017-07-04 01:01:39 +02002728
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002729#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002730 if( rs_ctx != NULL && ret == MBEDTLS_ERR_ECP_IN_PROGRESS )
2731 {
2732 /* save state */
2733 rs_ctx->parent_is_trusted = *parent_is_trusted;
Manuel Pégourié-Gonnard18547b52017-08-14 16:11:43 +02002734 return( ret );
2735 }
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002736#else
2737 (void) ret;
2738#endif
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002739
Manuel Pégourié-Gonnard18547b52017-08-14 16:11:43 +02002740 /* stop here if found or already in second iteration */
2741 if( *parent != NULL || *parent_is_trusted == 0 )
2742 break;
Manuel Pégourié-Gonnard63686122017-07-04 01:01:39 +02002743
Manuel Pégourié-Gonnard18547b52017-08-14 16:11:43 +02002744 /* prepare second iteration */
2745 *parent_is_trusted = 0;
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002746 }
Manuel Pégourié-Gonnard18547b52017-08-14 16:11:43 +02002747
2748 /* extra precaution against mistakes in the caller */
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05002749 if( *parent == NULL )
Manuel Pégourié-Gonnard18547b52017-08-14 16:11:43 +02002750 {
Manuel Pégourié-Gonnarda5a3e402018-10-16 11:27:23 +02002751 *parent_is_trusted = 0;
2752 *signature_is_good = 0;
Manuel Pégourié-Gonnard18547b52017-08-14 16:11:43 +02002753 }
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002754
2755 return( 0 );
Manuel Pégourié-Gonnard63686122017-07-04 01:01:39 +02002756}
2757
2758/*
Manuel Pégourié-Gonnard27e94792017-07-04 00:49:31 +02002759 * Check if an end-entity certificate is locally trusted
2760 *
2761 * Currently we require such certificates to be self-signed (actually only
2762 * check for self-issued as self-signatures are not checked)
2763 */
2764static int x509_crt_check_ee_locally_trusted(
2765 mbedtls_x509_crt *crt,
2766 mbedtls_x509_crt *trust_ca )
2767{
2768 mbedtls_x509_crt *cur;
2769
2770 /* must be self-issued */
2771 if( x509_name_cmp( &crt->issuer, &crt->subject ) != 0 )
2772 return( -1 );
2773
2774 /* look for an exact match with trusted cert */
2775 for( cur = trust_ca; cur != NULL; cur = cur->next )
2776 {
2777 if( crt->raw.len == cur->raw.len &&
2778 memcmp( crt->raw.p, cur->raw.p, crt->raw.len ) == 0 )
2779 {
2780 return( 0 );
2781 }
2782 }
2783
2784 /* too bad */
2785 return( -1 );
2786}
2787
2788/*
Manuel Pégourié-Gonnardf86f4912017-07-05 16:43:44 +02002789 * Build and verify a certificate chain
Manuel Pégourié-Gonnard35407c72017-06-29 10:45:25 +02002790 *
Manuel Pégourié-Gonnardf86f4912017-07-05 16:43:44 +02002791 * Given a peer-provided list of certificates EE, C1, ..., Cn and
2792 * a list of trusted certs R1, ... Rp, try to build and verify a chain
Manuel Pégourié-Gonnard562df402017-08-08 18:09:14 +02002793 * EE, Ci1, ... Ciq [, Rj]
Manuel Pégourié-Gonnardf86f4912017-07-05 16:43:44 +02002794 * such that every cert in the chain is a child of the next one,
2795 * jumping to a trusted root as early as possible.
Manuel Pégourié-Gonnardbdc54402017-07-04 00:33:39 +02002796 *
Manuel Pégourié-Gonnardf86f4912017-07-05 16:43:44 +02002797 * Verify that chain and return it with flags for all issues found.
2798 *
2799 * Special cases:
2800 * - EE == Rj -> return a one-element list containing it
2801 * - EE, Ci1, ..., Ciq cannot be continued with a trusted root
2802 * -> return that chain with NOT_TRUSTED set on Ciq
Manuel Pégourié-Gonnardbdc54402017-07-04 00:33:39 +02002803 *
Manuel Pégourié-Gonnardd19a41d2017-07-14 11:05:59 +02002804 * Tests for (aspects of) this function should include at least:
2805 * - trusted EE
2806 * - EE -> trusted root
Antonin Décimo36e89b52019-01-23 15:24:37 +01002807 * - EE -> intermediate CA -> trusted root
Manuel Pégourié-Gonnardd19a41d2017-07-14 11:05:59 +02002808 * - if relevant: EE untrusted
2809 * - if relevant: EE -> intermediate, untrusted
2810 * with the aspect under test checked at each relevant level (EE, int, root).
2811 * For some aspects longer chains are required, but usually length 2 is
2812 * enough (but length 1 is not in general).
2813 *
Manuel Pégourié-Gonnardbdc54402017-07-04 00:33:39 +02002814 * Arguments:
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002815 * - [in] crt: the cert list EE, C1, ..., Cn
2816 * - [in] trust_ca: the trusted list R1, ..., Rp
2817 * - [in] ca_crl, profile: as in verify_with_profile()
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02002818 * - [out] ver_chain: the built and verified chain
Manuel Pégourié-Gonnarda9688432017-08-23 11:23:59 +02002819 * Only valid when return value is 0, may contain garbage otherwise!
2820 * Restart note: need not be the same when calling again to resume.
Manuel Pégourié-Gonnard98a67782017-08-17 10:52:20 +02002821 * - [in-out] rs_ctx: context for restarting operations
Manuel Pégourié-Gonnardf86f4912017-07-05 16:43:44 +02002822 *
2823 * Return value:
2824 * - non-zero if the chain could not be fully built and examined
2825 * - 0 is the chain was successfully built and examined,
2826 * even if it was found to be invalid
Manuel Pégourié-Gonnard35407c72017-06-29 10:45:25 +02002827 */
Manuel Pégourié-Gonnardbdc54402017-07-04 00:33:39 +02002828static int x509_crt_verify_chain(
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002829 mbedtls_x509_crt *crt,
2830 mbedtls_x509_crt *trust_ca,
2831 mbedtls_x509_crl *ca_crl,
Hanno Becker3116fb32019-03-28 13:34:42 +00002832 mbedtls_x509_crt_ca_cb_t f_ca_cb,
2833 void *p_ca_cb,
Manuel Pégourié-Gonnard95051642015-06-15 10:39:46 +02002834 const mbedtls_x509_crt_profile *profile,
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02002835 mbedtls_x509_crt_verify_chain *ver_chain,
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002836 mbedtls_x509_crt_restart_ctx *rs_ctx )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002837{
Manuel Pégourié-Gonnarda9688432017-08-23 11:23:59 +02002838 /* Don't initialize any of those variables here, so that the compiler can
2839 * catch potential issues with jumping ahead when restarting */
Janos Follath865b3eb2019-12-16 11:46:15 +00002840 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardf86f4912017-07-05 16:43:44 +02002841 uint32_t *flags;
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02002842 mbedtls_x509_crt_verify_chain_item *cur;
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002843 mbedtls_x509_crt *child;
Manuel Pégourié-Gonnard58dcd2d2017-07-03 21:35:04 +02002844 mbedtls_x509_crt *parent;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002845 int parent_is_trusted;
2846 int child_is_trusted;
2847 int signature_is_good;
Manuel Pégourié-Gonnardbb216bd2017-08-28 13:25:55 +02002848 unsigned self_cnt;
Hanno Beckerf53893b2019-03-28 13:45:55 +00002849 mbedtls_x509_crt *cur_trust_ca = NULL;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002850
2851#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
2852 /* resume if we had an operation in progress */
Manuel Pégourié-Gonnarddaf04912017-08-23 12:32:19 +02002853 if( rs_ctx != NULL && rs_ctx->in_progress == x509_crt_rs_find_parent )
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002854 {
Manuel Pégourié-Gonnard3627a8b2017-08-23 11:20:48 +02002855 /* restore saved state */
Manuel Pégourié-Gonnarda9688432017-08-23 11:23:59 +02002856 *ver_chain = rs_ctx->ver_chain; /* struct copy */
Manuel Pégourié-Gonnarddaf04912017-08-23 12:32:19 +02002857 self_cnt = rs_ctx->self_cnt;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002858
Manuel Pégourié-Gonnarddaf04912017-08-23 12:32:19 +02002859 /* restore derived state */
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002860 cur = &ver_chain->items[ver_chain->len - 1];
Manuel Pégourié-Gonnarddaf04912017-08-23 12:32:19 +02002861 child = cur->crt;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002862 flags = &cur->flags;
2863
2864 goto find_parent;
2865 }
2866#endif /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */
Manuel Pégourié-Gonnard8f8c2822017-07-03 21:25:10 +02002867
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002868 child = crt;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002869 self_cnt = 0;
2870 parent_is_trusted = 0;
2871 child_is_trusted = 0;
Manuel Pégourié-Gonnardf86f4912017-07-05 16:43:44 +02002872
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002873 while( 1 ) {
2874 /* Add certificate to the verification chain */
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02002875 cur = &ver_chain->items[ver_chain->len];
2876 cur->crt = child;
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +02002877 cur->flags = 0;
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02002878 ver_chain->len++;
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +02002879 flags = &cur->flags;
Manuel Pégourié-Gonnard66fac752017-07-03 21:39:21 +02002880
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002881 /* Check time-validity (all certificates) */
2882 if( mbedtls_x509_time_is_past( &child->valid_to ) )
2883 *flags |= MBEDTLS_X509_BADCERT_EXPIRED;
Manuel Pégourié-Gonnard66fac752017-07-03 21:39:21 +02002884
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002885 if( mbedtls_x509_time_is_future( &child->valid_from ) )
2886 *flags |= MBEDTLS_X509_BADCERT_FUTURE;
Manuel Pégourié-Gonnardcb396102017-07-04 00:00:24 +02002887
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002888 /* Stop here for trusted roots (but not for trusted EE certs) */
2889 if( child_is_trusted )
2890 return( 0 );
Manuel Pégourié-Gonnard66fac752017-07-03 21:39:21 +02002891
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002892 /* Check signature algorithm: MD & PK algs */
2893 if( x509_profile_check_md_alg( profile, child->sig_md ) != 0 )
2894 *flags |= MBEDTLS_X509_BADCERT_BAD_MD;
Manuel Pégourié-Gonnard66fac752017-07-03 21:39:21 +02002895
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002896 if( x509_profile_check_pk_alg( profile, child->sig_pk ) != 0 )
2897 *flags |= MBEDTLS_X509_BADCERT_BAD_PK;
Manuel Pégourié-Gonnard27e94792017-07-04 00:49:31 +02002898
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002899 /* Special case: EE certs that are locally trusted */
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02002900 if( ver_chain->len == 1 &&
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002901 x509_crt_check_ee_locally_trusted( child, trust_ca ) == 0 )
2902 {
2903 return( 0 );
2904 }
Manuel Pégourié-Gonnard8f8c2822017-07-03 21:25:10 +02002905
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002906#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
2907find_parent:
2908#endif
Hanno Beckerf53893b2019-03-28 13:45:55 +00002909
2910 /* Obtain list of potential trusted signers from CA callback,
2911 * or use statically provided list. */
2912#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
2913 if( f_ca_cb != NULL )
2914 {
2915 mbedtls_x509_crt_free( ver_chain->trust_ca_cb_result );
2916 mbedtls_free( ver_chain->trust_ca_cb_result );
2917 ver_chain->trust_ca_cb_result = NULL;
2918
2919 ret = f_ca_cb( p_ca_cb, child, &ver_chain->trust_ca_cb_result );
2920 if( ret != 0 )
2921 return( MBEDTLS_ERR_X509_FATAL_ERROR );
2922
2923 cur_trust_ca = ver_chain->trust_ca_cb_result;
2924 }
2925 else
2926#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
2927 {
2928 ((void) f_ca_cb);
2929 ((void) p_ca_cb);
2930 cur_trust_ca = trust_ca;
2931 }
2932
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002933 /* Look for a parent in trusted CAs or up the chain */
Hanno Beckerf53893b2019-03-28 13:45:55 +00002934 ret = x509_crt_find_parent( child, cur_trust_ca, &parent,
Manuel Pégourié-Gonnardbe4ff422017-07-14 12:04:14 +02002935 &parent_is_trusted, &signature_is_good,
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02002936 ver_chain->len - 1, self_cnt, rs_ctx );
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002937
2938#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002939 if( rs_ctx != NULL && ret == MBEDTLS_ERR_ECP_IN_PROGRESS )
2940 {
2941 /* save state */
Manuel Pégourié-Gonnarddaf04912017-08-23 12:32:19 +02002942 rs_ctx->in_progress = x509_crt_rs_find_parent;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002943 rs_ctx->self_cnt = self_cnt;
Manuel Pégourié-Gonnarda9688432017-08-23 11:23:59 +02002944 rs_ctx->ver_chain = *ver_chain; /* struct copy */
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002945
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002946 return( ret );
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002947 }
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002948#else
2949 (void) ret;
2950#endif
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002951
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002952 /* No parent? We're done here */
2953 if( parent == NULL )
2954 {
2955 *flags |= MBEDTLS_X509_BADCERT_NOT_TRUSTED;
2956 return( 0 );
2957 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002958
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002959 /* Count intermediate self-issued (not necessarily self-signed) certs.
2960 * These can occur with some strategies for key rollover, see [SIRO],
2961 * and should be excluded from max_pathlen checks. */
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02002962 if( ver_chain->len != 1 &&
Manuel Pégourié-Gonnard24611f92017-08-09 10:28:07 +02002963 x509_name_cmp( &child->issuer, &child->subject ) == 0 )
2964 {
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002965 self_cnt++;
Manuel Pégourié-Gonnard24611f92017-08-09 10:28:07 +02002966 }
Manuel Pégourié-Gonnardfd6c85c2014-11-20 16:34:20 +01002967
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002968 /* path_cnt is 0 for the first intermediate CA,
2969 * and if parent is trusted it's not an intermediate CA */
2970 if( ! parent_is_trusted &&
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02002971 ver_chain->len > MBEDTLS_X509_MAX_INTERMEDIATE_CA )
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002972 {
2973 /* return immediately to avoid overflow the chain array */
2974 return( MBEDTLS_ERR_X509_FATAL_ERROR );
2975 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002976
Manuel Pégourié-Gonnard98a67782017-08-17 10:52:20 +02002977 /* signature was checked while searching parent */
Manuel Pégourié-Gonnardbe4ff422017-07-14 12:04:14 +02002978 if( ! signature_is_good )
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002979 *flags |= MBEDTLS_X509_BADCERT_NOT_TRUSTED;
2980
2981 /* check size of signing key */
Manuel Pégourié-Gonnard3f816912017-10-26 10:24:16 +02002982 if( x509_profile_check_key( profile, &parent->pk ) != 0 )
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002983 *flags |= MBEDTLS_X509_BADCERT_BAD_KEY;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002984
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002985#if defined(MBEDTLS_X509_CRL_PARSE_C)
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002986 /* Check trusted CA's CRL for the given crt */
Manuel Pégourié-Gonnard562df402017-08-08 18:09:14 +02002987 *flags |= x509_crt_verifycrl( child, parent, ca_crl, profile );
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002988#else
2989 (void) ca_crl;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002990#endif
2991
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002992 /* prepare for next iteration */
2993 child = parent;
2994 parent = NULL;
2995 child_is_trusted = parent_is_trusted;
Manuel Pégourié-Gonnardbe4ff422017-07-14 12:04:14 +02002996 signature_is_good = 0;
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002997 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002998}
2999
3000/*
Manuel Pégourié-Gonnarda468eb12017-07-04 01:31:59 +02003001 * Check for CN match
3002 */
3003static int x509_crt_check_cn( const mbedtls_x509_buf *name,
3004 const char *cn, size_t cn_len )
3005{
3006 /* try exact match */
3007 if( name->len == cn_len &&
3008 x509_memcasecmp( cn, name->p, cn_len ) == 0 )
3009 {
3010 return( 0 );
3011 }
3012
3013 /* try wildcard match */
Manuel Pégourié-Gonnard900fba62017-10-18 14:28:11 +02003014 if( x509_check_wildcard( cn, name ) == 0 )
Manuel Pégourié-Gonnarda468eb12017-07-04 01:31:59 +02003015 {
3016 return( 0 );
3017 }
3018
3019 return( -1 );
3020}
3021
3022/*
Manuel Pégourié-Gonnardf3e4bd82020-07-21 13:22:41 +02003023 * Check for SAN match, see RFC 5280 Section 4.2.1.6
3024 */
3025static int x509_crt_check_san( const mbedtls_x509_buf *name,
3026 const char *cn, size_t cn_len )
3027{
3028 const unsigned char san_type = (unsigned char) name->tag &
3029 MBEDTLS_ASN1_TAG_VALUE_MASK;
3030
3031 /* dNSName */
3032 if( san_type == MBEDTLS_X509_SAN_DNS_NAME )
3033 return( x509_crt_check_cn( name, cn, cn_len ) );
3034
3035 /* (We may handle other types here later.) */
3036
3037 /* Unrecognized type */
3038 return( -1 );
3039}
3040
3041/*
Manuel Pégourié-Gonnard1300e992017-07-04 01:13:44 +02003042 * Verify the requested CN - only call this if cn is not NULL!
3043 */
Manuel Pégourié-Gonnarda468eb12017-07-04 01:31:59 +02003044static void x509_crt_verify_name( const mbedtls_x509_crt *crt,
Manuel Pégourié-Gonnard1300e992017-07-04 01:13:44 +02003045 const char *cn,
3046 uint32_t *flags )
3047{
Manuel Pégourié-Gonnarda468eb12017-07-04 01:31:59 +02003048 const mbedtls_x509_name *name;
3049 const mbedtls_x509_sequence *cur;
3050 size_t cn_len = strlen( cn );
Manuel Pégourié-Gonnard1300e992017-07-04 01:13:44 +02003051
3052 if( crt->ext_types & MBEDTLS_X509_EXT_SUBJECT_ALT_NAME )
3053 {
Manuel Pégourié-Gonnarda468eb12017-07-04 01:31:59 +02003054 for( cur = &crt->subject_alt_names; cur != NULL; cur = cur->next )
Manuel Pégourié-Gonnard1300e992017-07-04 01:13:44 +02003055 {
Manuel Pégourié-Gonnardf3e4bd82020-07-21 13:22:41 +02003056 if( x509_crt_check_san( &cur->buf, cn, cn_len ) == 0 )
Manuel Pégourié-Gonnard1300e992017-07-04 01:13:44 +02003057 break;
Manuel Pégourié-Gonnard1300e992017-07-04 01:13:44 +02003058 }
3059
3060 if( cur == NULL )
3061 *flags |= MBEDTLS_X509_BADCERT_CN_MISMATCH;
3062 }
3063 else
3064 {
Manuel Pégourié-Gonnard08eacec2017-10-18 14:20:24 +02003065 for( name = &crt->subject; name != NULL; name = name->next )
Manuel Pégourié-Gonnard1300e992017-07-04 01:13:44 +02003066 {
Manuel Pégourié-Gonnarda468eb12017-07-04 01:31:59 +02003067 if( MBEDTLS_OID_CMP( MBEDTLS_OID_AT_CN, &name->oid ) == 0 &&
3068 x509_crt_check_cn( &name->val, cn, cn_len ) == 0 )
Manuel Pégourié-Gonnard1300e992017-07-04 01:13:44 +02003069 {
Manuel Pégourié-Gonnarda468eb12017-07-04 01:31:59 +02003070 break;
Manuel Pégourié-Gonnard1300e992017-07-04 01:13:44 +02003071 }
Manuel Pégourié-Gonnard1300e992017-07-04 01:13:44 +02003072 }
3073
3074 if( name == NULL )
3075 *flags |= MBEDTLS_X509_BADCERT_CN_MISMATCH;
3076 }
3077}
3078
3079/*
Manuel Pégourié-Gonnarda707e1d2017-07-05 17:18:42 +02003080 * Merge the flags for all certs in the chain, after calling callback
3081 */
3082static int x509_crt_merge_flags_with_cb(
3083 uint32_t *flags,
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02003084 const mbedtls_x509_crt_verify_chain *ver_chain,
Manuel Pégourié-Gonnarda707e1d2017-07-05 17:18:42 +02003085 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
3086 void *p_vrfy )
3087{
Janos Follath865b3eb2019-12-16 11:46:15 +00003088 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardbb216bd2017-08-28 13:25:55 +02003089 unsigned i;
Manuel Pégourié-Gonnarda707e1d2017-07-05 17:18:42 +02003090 uint32_t cur_flags;
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02003091 const mbedtls_x509_crt_verify_chain_item *cur;
Manuel Pégourié-Gonnarda707e1d2017-07-05 17:18:42 +02003092
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02003093 for( i = ver_chain->len; i != 0; --i )
Manuel Pégourié-Gonnarda707e1d2017-07-05 17:18:42 +02003094 {
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02003095 cur = &ver_chain->items[i-1];
3096 cur_flags = cur->flags;
Manuel Pégourié-Gonnarda707e1d2017-07-05 17:18:42 +02003097
3098 if( NULL != f_vrfy )
Manuel Pégourié-Gonnardbb216bd2017-08-28 13:25:55 +02003099 if( ( ret = f_vrfy( p_vrfy, cur->crt, (int) i-1, &cur_flags ) ) != 0 )
Manuel Pégourié-Gonnarda707e1d2017-07-05 17:18:42 +02003100 return( ret );
3101
3102 *flags |= cur_flags;
3103 }
3104
3105 return( 0 );
3106}
3107
3108/*
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +02003109 * Verify the certificate validity, with profile, restartable version
3110 *
3111 * This function:
3112 * - checks the requested CN (if any)
3113 * - checks the type and size of the EE cert's key,
3114 * as that isn't done as part of chain building/verification currently
3115 * - builds and verifies the chain
3116 * - then calls the callback and merges the flags
Hanno Becker3116fb32019-03-28 13:34:42 +00003117 *
3118 * The parameters pairs `trust_ca`, `ca_crl` and `f_ca_cb`, `p_ca_cb`
3119 * are mutually exclusive: If `f_ca_cb != NULL`, it will be used by the
3120 * verification routine to search for trusted signers, and CRLs will
3121 * be disabled. Otherwise, `trust_ca` will be used as the static list
3122 * of trusted signers, and `ca_crl` will be use as the static list
3123 * of CRLs.
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +02003124 */
Jarno Lamsa2ee67a62019-04-01 14:59:33 +03003125static int x509_crt_verify_restartable_ca_cb( mbedtls_x509_crt *crt,
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +02003126 mbedtls_x509_crt *trust_ca,
3127 mbedtls_x509_crl *ca_crl,
Hanno Becker3116fb32019-03-28 13:34:42 +00003128 mbedtls_x509_crt_ca_cb_t f_ca_cb,
3129 void *p_ca_cb,
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +02003130 const mbedtls_x509_crt_profile *profile,
3131 const char *cn, uint32_t *flags,
3132 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
3133 void *p_vrfy,
3134 mbedtls_x509_crt_restart_ctx *rs_ctx )
3135{
Janos Follath865b3eb2019-12-16 11:46:15 +00003136 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard65eefc82015-10-23 14:08:48 +02003137 mbedtls_pk_type_t pk_type;
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02003138 mbedtls_x509_crt_verify_chain ver_chain;
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +02003139 uint32_t ee_flags;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003140
3141 *flags = 0;
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +02003142 ee_flags = 0;
3143 x509_crt_verify_chain_reset( &ver_chain );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003144
Manuel Pégourié-Gonnardd15795a2017-06-22 12:19:27 +02003145 if( profile == NULL )
3146 {
3147 ret = MBEDTLS_ERR_X509_BAD_INPUT_DATA;
3148 goto exit;
3149 }
3150
Manuel Pégourié-Gonnard1300e992017-07-04 01:13:44 +02003151 /* check name if requested */
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003152 if( cn != NULL )
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +02003153 x509_crt_verify_name( crt, cn, &ee_flags );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003154
Manuel Pégourié-Gonnard65eefc82015-10-23 14:08:48 +02003155 /* Check the type and size of the key */
3156 pk_type = mbedtls_pk_get_type( &crt->pk );
3157
3158 if( x509_profile_check_pk_alg( profile, pk_type ) != 0 )
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +02003159 ee_flags |= MBEDTLS_X509_BADCERT_BAD_PK;
Manuel Pégourié-Gonnard65eefc82015-10-23 14:08:48 +02003160
Manuel Pégourié-Gonnard3f816912017-10-26 10:24:16 +02003161 if( x509_profile_check_key( profile, &crt->pk ) != 0 )
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +02003162 ee_flags |= MBEDTLS_X509_BADCERT_BAD_KEY;
Manuel Pégourié-Gonnard65eefc82015-10-23 14:08:48 +02003163
Manuel Pégourié-Gonnardbdc54402017-07-04 00:33:39 +02003164 /* Check the chain */
Hanno Becker3116fb32019-03-28 13:34:42 +00003165 ret = x509_crt_verify_chain( crt, trust_ca, ca_crl,
3166 f_ca_cb, p_ca_cb, profile,
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02003167 &ver_chain, rs_ctx );
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02003168
Manuel Pégourié-Gonnardc547d1a2017-07-05 13:28:45 +02003169 if( ret != 0 )
3170 goto exit;
3171
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +02003172 /* Merge end-entity flags */
3173 ver_chain.items[0].flags |= ee_flags;
3174
Manuel Pégourié-Gonnarda707e1d2017-07-05 17:18:42 +02003175 /* Build final flags, calling callback on the way if any */
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02003176 ret = x509_crt_merge_flags_with_cb( flags, &ver_chain, f_vrfy, p_vrfy );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003177
Manuel Pégourié-Gonnardd15795a2017-06-22 12:19:27 +02003178exit:
Hanno Beckerf53893b2019-03-28 13:45:55 +00003179
3180#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
3181 mbedtls_x509_crt_free( ver_chain.trust_ca_cb_result );
3182 mbedtls_free( ver_chain.trust_ca_cb_result );
3183 ver_chain.trust_ca_cb_result = NULL;
3184#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
3185
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02003186#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
3187 if( rs_ctx != NULL && ret != MBEDTLS_ERR_ECP_IN_PROGRESS )
3188 mbedtls_x509_crt_restart_free( rs_ctx );
3189#endif
3190
Manuel Pégourié-Gonnard9107b5f2017-07-06 12:16:25 +02003191 /* prevent misuse of the vrfy callback - VERIFY_FAILED would be ignored by
3192 * the SSL module for authmode optional, but non-zero return from the
3193 * callback means a fatal error so it shouldn't be ignored */
Manuel Pégourié-Gonnard31458a12017-06-26 10:11:49 +02003194 if( ret == MBEDTLS_ERR_X509_CERT_VERIFY_FAILED )
3195 ret = MBEDTLS_ERR_X509_FATAL_ERROR;
3196
Manuel Pégourié-Gonnardd15795a2017-06-22 12:19:27 +02003197 if( ret != 0 )
3198 {
3199 *flags = (uint32_t) -1;
3200 return( ret );
3201 }
3202
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003203 if( *flags != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003204 return( MBEDTLS_ERR_X509_CERT_VERIFY_FAILED );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003205
3206 return( 0 );
3207}
3208
Hanno Becker3116fb32019-03-28 13:34:42 +00003209
3210/*
3211 * Verify the certificate validity (default profile, not restartable)
3212 */
3213int mbedtls_x509_crt_verify( mbedtls_x509_crt *crt,
3214 mbedtls_x509_crt *trust_ca,
3215 mbedtls_x509_crl *ca_crl,
3216 const char *cn, uint32_t *flags,
3217 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
3218 void *p_vrfy )
3219{
Jarno Lamsa2ee67a62019-04-01 14:59:33 +03003220 return( x509_crt_verify_restartable_ca_cb( crt, trust_ca, ca_crl,
Hanno Becker3116fb32019-03-28 13:34:42 +00003221 NULL, NULL,
3222 &mbedtls_x509_crt_profile_default,
3223 cn, flags,
3224 f_vrfy, p_vrfy, NULL ) );
3225}
3226
3227/*
3228 * Verify the certificate validity (user-chosen profile, not restartable)
3229 */
3230int mbedtls_x509_crt_verify_with_profile( mbedtls_x509_crt *crt,
3231 mbedtls_x509_crt *trust_ca,
3232 mbedtls_x509_crl *ca_crl,
3233 const mbedtls_x509_crt_profile *profile,
3234 const char *cn, uint32_t *flags,
3235 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
3236 void *p_vrfy )
3237{
Jarno Lamsa2ee67a62019-04-01 14:59:33 +03003238 return( x509_crt_verify_restartable_ca_cb( crt, trust_ca, ca_crl,
Hanno Becker3116fb32019-03-28 13:34:42 +00003239 NULL, NULL,
3240 profile, cn, flags,
3241 f_vrfy, p_vrfy, NULL ) );
3242}
3243
3244#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
3245/*
3246 * Verify the certificate validity (user-chosen profile, CA callback,
3247 * not restartable).
3248 */
Jarno Lamsa31d9db62019-04-01 14:33:49 +03003249int mbedtls_x509_crt_verify_with_ca_cb( mbedtls_x509_crt *crt,
Hanno Becker3116fb32019-03-28 13:34:42 +00003250 mbedtls_x509_crt_ca_cb_t f_ca_cb,
3251 void *p_ca_cb,
3252 const mbedtls_x509_crt_profile *profile,
3253 const char *cn, uint32_t *flags,
3254 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
3255 void *p_vrfy )
3256{
Jarno Lamsa2ee67a62019-04-01 14:59:33 +03003257 return( x509_crt_verify_restartable_ca_cb( crt, NULL, NULL,
Hanno Becker3116fb32019-03-28 13:34:42 +00003258 f_ca_cb, p_ca_cb,
3259 profile, cn, flags,
3260 f_vrfy, p_vrfy, NULL ) );
3261}
3262#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
3263
3264int mbedtls_x509_crt_verify_restartable( mbedtls_x509_crt *crt,
3265 mbedtls_x509_crt *trust_ca,
3266 mbedtls_x509_crl *ca_crl,
3267 const mbedtls_x509_crt_profile *profile,
3268 const char *cn, uint32_t *flags,
3269 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
3270 void *p_vrfy,
3271 mbedtls_x509_crt_restart_ctx *rs_ctx )
3272{
Jarno Lamsa2ee67a62019-04-01 14:59:33 +03003273 return( x509_crt_verify_restartable_ca_cb( crt, trust_ca, ca_crl,
Hanno Becker3116fb32019-03-28 13:34:42 +00003274 NULL, NULL,
3275 profile, cn, flags,
3276 f_vrfy, p_vrfy, rs_ctx ) );
3277}
3278
3279
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003280/*
Paul Bakker369d2eb2013-09-18 11:58:25 +02003281 * Initialize a certificate chain
3282 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003283void mbedtls_x509_crt_init( mbedtls_x509_crt *crt )
Paul Bakker369d2eb2013-09-18 11:58:25 +02003284{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003285 memset( crt, 0, sizeof(mbedtls_x509_crt) );
Paul Bakker369d2eb2013-09-18 11:58:25 +02003286}
3287
3288/*
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003289 * Unallocate all certificate data
3290 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003291void mbedtls_x509_crt_free( mbedtls_x509_crt *crt )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003292{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003293 mbedtls_x509_crt *cert_cur = crt;
3294 mbedtls_x509_crt *cert_prv;
3295 mbedtls_x509_name *name_cur;
3296 mbedtls_x509_name *name_prv;
3297 mbedtls_x509_sequence *seq_cur;
3298 mbedtls_x509_sequence *seq_prv;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003299
3300 if( crt == NULL )
3301 return;
3302
3303 do
3304 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003305 mbedtls_pk_free( &cert_cur->pk );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003306
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003307#if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT)
3308 mbedtls_free( cert_cur->sig_opts );
Manuel Pégourié-Gonnardf75f2f72014-06-05 15:14:28 +02003309#endif
3310
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003311 name_cur = cert_cur->issuer.next;
3312 while( name_cur != NULL )
3313 {
3314 name_prv = name_cur;
3315 name_cur = name_cur->next;
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05003316 mbedtls_platform_zeroize( name_prv, sizeof( mbedtls_x509_name ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003317 mbedtls_free( name_prv );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003318 }
3319
3320 name_cur = cert_cur->subject.next;
3321 while( name_cur != NULL )
3322 {
3323 name_prv = name_cur;
3324 name_cur = name_cur->next;
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05003325 mbedtls_platform_zeroize( name_prv, sizeof( mbedtls_x509_name ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003326 mbedtls_free( name_prv );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003327 }
3328
3329 seq_cur = cert_cur->ext_key_usage.next;
3330 while( seq_cur != NULL )
3331 {
3332 seq_prv = seq_cur;
3333 seq_cur = seq_cur->next;
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05003334 mbedtls_platform_zeroize( seq_prv,
3335 sizeof( mbedtls_x509_sequence ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003336 mbedtls_free( seq_prv );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003337 }
3338
3339 seq_cur = cert_cur->subject_alt_names.next;
3340 while( seq_cur != NULL )
3341 {
3342 seq_prv = seq_cur;
3343 seq_cur = seq_cur->next;
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05003344 mbedtls_platform_zeroize( seq_prv,
3345 sizeof( mbedtls_x509_sequence ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003346 mbedtls_free( seq_prv );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003347 }
3348
Ron Eldor74d9acc2019-03-21 14:00:03 +02003349 seq_cur = cert_cur->certificate_policies.next;
3350 while( seq_cur != NULL )
3351 {
3352 seq_prv = seq_cur;
3353 seq_cur = seq_cur->next;
3354 mbedtls_platform_zeroize( seq_prv,
3355 sizeof( mbedtls_x509_sequence ) );
3356 mbedtls_free( seq_prv );
3357 }
3358
Hanno Becker1a65dcd2019-01-31 08:57:44 +00003359 if( cert_cur->raw.p != NULL && cert_cur->own_buffer )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003360 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05003361 mbedtls_platform_zeroize( cert_cur->raw.p, cert_cur->raw.len );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003362 mbedtls_free( cert_cur->raw.p );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003363 }
3364
3365 cert_cur = cert_cur->next;
3366 }
3367 while( cert_cur != NULL );
3368
3369 cert_cur = crt;
3370 do
3371 {
3372 cert_prv = cert_cur;
3373 cert_cur = cert_cur->next;
3374
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05003375 mbedtls_platform_zeroize( cert_prv, sizeof( mbedtls_x509_crt ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003376 if( cert_prv != crt )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003377 mbedtls_free( cert_prv );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003378 }
3379 while( cert_cur != NULL );
3380}
3381
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +02003382#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
3383/*
3384 * Initialize a restart context
3385 */
3386void mbedtls_x509_crt_restart_init( mbedtls_x509_crt_restart_ctx *ctx )
3387{
Manuel Pégourié-Gonnard15d7df22017-08-17 14:33:31 +02003388 mbedtls_pk_restart_init( &ctx->pk );
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02003389
3390 ctx->parent = NULL;
3391 ctx->fallback_parent = NULL;
Manuel Pégourié-Gonnard78d7e8c2018-07-02 12:33:14 +02003392 ctx->fallback_signature_is_good = 0;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02003393
3394 ctx->parent_is_trusted = -1;
3395
Manuel Pégourié-Gonnarddaf04912017-08-23 12:32:19 +02003396 ctx->in_progress = x509_crt_rs_none;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02003397 ctx->self_cnt = 0;
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +02003398 x509_crt_verify_chain_reset( &ctx->ver_chain );
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +02003399}
3400
3401/*
3402 * Free the components of a restart context
3403 */
3404void mbedtls_x509_crt_restart_free( mbedtls_x509_crt_restart_ctx *ctx )
3405{
3406 if( ctx == NULL )
3407 return;
3408
Manuel Pégourié-Gonnard15d7df22017-08-17 14:33:31 +02003409 mbedtls_pk_restart_free( &ctx->pk );
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02003410 mbedtls_x509_crt_restart_init( ctx );
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +02003411}
3412#endif /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */
3413
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003414#endif /* MBEDTLS_X509_CRT_PARSE_C */