blob: 0e4279bf76fbbd966c7420ec9a2729bb7d03f5a2 [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 }
Eduardo Silva32ffb2b2019-04-25 10:43:26 -06001642 else
Andres AGf9113192016-09-02 14:06:04 +01001643 {
Dave Rodgman2958bb32022-07-01 11:31:05 +01001644 /* Determine if the file entry could be a link. Using lstat(2)
Eduardo Silva32ffb2b2019-04-25 10:43:26 -06001645 * is safer than just stat(2), otherwise a broken link will
1646 * give us a false positive. */
1647 if( lstat( entry_name, &sb ) == -1 )
1648 {
1649 ret = MBEDTLS_ERR_X509_FILE_IO_ERROR;
1650 goto cleanup;
1651 }
1652
Dave Rodgman2958bb32022-07-01 11:31:05 +01001653 /* If the file is a symbolic link, we need to validate the real
Eduardo Silva32ffb2b2019-04-25 10:43:26 -06001654 * information using stat(2). */
1655 if( S_ISLNK( sb.st_mode ) )
1656 {
Dave Rodgman2958bb32022-07-01 11:31:05 +01001657 /* If stat(2) fails it could be a broken link or a generic
1658 * error. If the link is broken, ignore it, otherwise
Eduardo Silva32ffb2b2019-04-25 10:43:26 -06001659 * just set a MBEDTLS_ERR_X509_FILE_IO_ERROR. */
1660 if( stat( entry_name, &sb ) == -1 )
1661 {
1662 if( errno == ENOENT )
1663 {
Dave Rodgman168bcd62022-07-20 14:01:45 +01001664 /* Broken link - ignore this entry */
1665 continue;
Eduardo Silva32ffb2b2019-04-25 10:43:26 -06001666 }
1667 else
1668 {
1669 ret = MBEDTLS_ERR_X509_FILE_IO_ERROR;
1670 goto cleanup;
1671 }
1672 }
1673 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001674 }
1675
1676 if( !S_ISREG( sb.st_mode ) )
1677 continue;
1678
1679 // Ignore parse errors
1680 //
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001681 t_ret = mbedtls_x509_crt_parse_file( chain, entry_name );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001682 if( t_ret < 0 )
1683 ret++;
1684 else
1685 ret += t_ret;
1686 }
Manuel Pégourié-Gonnard5ad68e42013-11-28 17:11:54 +01001687
1688cleanup:
Andres AGf9113192016-09-02 14:06:04 +01001689 closedir( dir );
1690
Ron Eldor63140682017-01-09 19:27:59 +02001691#if defined(MBEDTLS_THREADING_C)
Manuel Pégourié-Gonnard944cfe82015-05-27 20:07:18 +02001692 if( mbedtls_mutex_unlock( &mbedtls_threading_readdir_mutex ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001693 ret = MBEDTLS_ERR_THREADING_MUTEX_ERROR;
Ron Eldor63140682017-01-09 19:27:59 +02001694#endif /* MBEDTLS_THREADING_C */
Manuel Pégourié-Gonnard5ad68e42013-11-28 17:11:54 +01001695
Paul Bakkerbe089b02013-10-14 15:51:50 +02001696#endif /* _WIN32 */
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001697
1698 return( ret );
1699}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001700#endif /* MBEDTLS_FS_IO */
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001701
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001702/*
1703 * OtherName ::= SEQUENCE {
1704 * type-id OBJECT IDENTIFIER,
1705 * value [0] EXPLICIT ANY DEFINED BY type-id }
1706 *
1707 * HardwareModuleName ::= SEQUENCE {
1708 * hwType OBJECT IDENTIFIER,
1709 * hwSerialNum OCTET STRING }
1710 *
1711 * NOTE: we currently only parse and use otherName of type HwModuleName,
1712 * as defined in RFC 4108.
1713 */
1714static int x509_get_other_name( const mbedtls_x509_buf *subject_alt_name,
1715 mbedtls_x509_san_other_name *other_name )
1716{
Janos Follathab23cd12019-05-09 13:53:57 +01001717 int ret = 0;
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001718 size_t len;
1719 unsigned char *p = subject_alt_name->p;
1720 const unsigned char *end = p + subject_alt_name->len;
1721 mbedtls_x509_buf cur_oid;
1722
1723 if( ( subject_alt_name->tag &
1724 ( MBEDTLS_ASN1_TAG_CLASS_MASK | MBEDTLS_ASN1_TAG_VALUE_MASK ) ) !=
1725 ( MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_X509_SAN_OTHER_NAME ) )
1726 {
1727 /*
1728 * The given subject alternative name is not of type "othername".
1729 */
1730 return( MBEDTLS_ERR_X509_BAD_INPUT_DATA );
1731 }
1732
1733 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
1734 MBEDTLS_ASN1_OID ) ) != 0 )
Chris Jones9f7a6932021-04-14 12:12:09 +01001735 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret ) );
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001736
1737 cur_oid.tag = MBEDTLS_ASN1_OID;
1738 cur_oid.p = p;
1739 cur_oid.len = len;
1740
1741 /*
1742 * Only HwModuleName is currently supported.
1743 */
1744 if( MBEDTLS_OID_CMP( MBEDTLS_OID_ON_HW_MODULE_NAME, &cur_oid ) != 0 )
1745 {
1746 return( MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE );
1747 }
1748
Ron Eldor890819a2019-05-13 19:03:04 +03001749 if( p + len >= end )
1750 {
Sébastien Duquette661d7252019-06-23 17:45:26 -04001751 mbedtls_platform_zeroize( other_name, sizeof( *other_name ) );
Chris Jones9f7a6932021-04-14 12:12:09 +01001752 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
1753 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH ) );
Ron Eldor890819a2019-05-13 19:03:04 +03001754 }
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001755 p += len;
1756 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
1757 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_CONTEXT_SPECIFIC ) ) != 0 )
Chris Jones9f7a6932021-04-14 12:12:09 +01001758 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret ) );
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001759
1760 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
1761 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
Chris Jones9f7a6932021-04-14 12:12:09 +01001762 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret ) );
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001763
1764 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len, MBEDTLS_ASN1_OID ) ) != 0 )
Chris Jones9f7a6932021-04-14 12:12:09 +01001765 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret ) );
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001766
1767 other_name->value.hardware_module_name.oid.tag = MBEDTLS_ASN1_OID;
1768 other_name->value.hardware_module_name.oid.p = p;
1769 other_name->value.hardware_module_name.oid.len = len;
1770
Ron Eldor890819a2019-05-13 19:03:04 +03001771 if( p + len >= end )
1772 {
Sébastien Duquette661d7252019-06-23 17:45:26 -04001773 mbedtls_platform_zeroize( other_name, sizeof( *other_name ) );
Chris Jones9f7a6932021-04-14 12:12:09 +01001774 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
1775 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH ) );
Ron Eldor890819a2019-05-13 19:03:04 +03001776 }
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001777 p += len;
1778 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
1779 MBEDTLS_ASN1_OCTET_STRING ) ) != 0 )
Chris Jones9f7a6932021-04-14 12:12:09 +01001780 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret ) );
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001781
1782 other_name->value.hardware_module_name.val.tag = MBEDTLS_ASN1_OCTET_STRING;
1783 other_name->value.hardware_module_name.val.p = p;
1784 other_name->value.hardware_module_name.val.len = len;
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001785 p += len;
1786 if( p != end )
1787 {
Ron Eldor890819a2019-05-13 19:03:04 +03001788 mbedtls_platform_zeroize( other_name,
Sébastien Duquette661d7252019-06-23 17:45:26 -04001789 sizeof( *other_name ) );
Chris Jones9f7a6932021-04-14 12:12:09 +01001790 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
1791 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH ) );
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001792 }
1793 return( 0 );
1794}
1795
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02001796static int x509_info_subject_alt_name( char **buf, size_t *size,
Janos Follath2f0ec1e2019-05-10 11:06:31 +01001797 const mbedtls_x509_sequence
1798 *subject_alt_name,
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001799 const char *prefix )
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02001800{
Janos Follath865b3eb2019-12-16 11:46:15 +00001801 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02001802 size_t n = *size;
1803 char *p = *buf;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001804 const mbedtls_x509_sequence *cur = subject_alt_name;
Ron Eldor890819a2019-05-13 19:03:04 +03001805 mbedtls_x509_subject_alternative_name san;
1806 int parse_ret;
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02001807
1808 while( cur != NULL )
1809 {
Ron Eldor890819a2019-05-13 19:03:04 +03001810 memset( &san, 0, sizeof( san ) );
1811 parse_ret = mbedtls_x509_parse_subject_alt_name( &cur->buf, &san );
1812 if( parse_ret != 0 )
1813 {
1814 if( parse_ret == MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE )
1815 {
1816 ret = mbedtls_snprintf( p, n, "\n%s <unsupported>", prefix );
1817 MBEDTLS_X509_SAFE_SNPRINTF;
1818 }
1819 else
1820 {
1821 ret = mbedtls_snprintf( p, n, "\n%s <malformed>", prefix );
1822 MBEDTLS_X509_SAFE_SNPRINTF;
1823 }
1824 cur = cur->next;
1825 continue;
1826 }
1827
1828 switch( san.type )
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02001829 {
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001830 /*
1831 * otherName
1832 */
Ron Eldora2913912019-05-16 16:17:38 +03001833 case MBEDTLS_X509_SAN_OTHER_NAME:
Ron Eldor890819a2019-05-13 19:03:04 +03001834 {
1835 mbedtls_x509_san_other_name *other_name = &san.san.other_name;
1836
1837 ret = mbedtls_snprintf( p, n, "\n%s otherName :", prefix );
1838 MBEDTLS_X509_SAFE_SNPRINTF;
1839
1840 if( MBEDTLS_OID_CMP( MBEDTLS_OID_ON_HW_MODULE_NAME,
1841 &other_name->value.hardware_module_name.oid ) != 0 )
Janos Follath22f605f2019-05-10 10:37:17 +01001842 {
Ron Eldor890819a2019-05-13 19:03:04 +03001843 ret = mbedtls_snprintf( p, n, "\n%s hardware module name :", prefix );
1844 MBEDTLS_X509_SAFE_SNPRINTF;
1845 ret = mbedtls_snprintf( p, n, "\n%s hardware type : ", prefix );
Janos Follath2f0ec1e2019-05-10 11:06:31 +01001846 MBEDTLS_X509_SAFE_SNPRINTF;
1847
Ron Eldor890819a2019-05-13 19:03:04 +03001848 ret = mbedtls_oid_get_numeric_string( p, n, &other_name->value.hardware_module_name.oid );
1849 MBEDTLS_X509_SAFE_SNPRINTF;
Janos Follath2f0ec1e2019-05-10 11:06:31 +01001850
Ron Eldor890819a2019-05-13 19:03:04 +03001851 ret = mbedtls_snprintf( p, n, "\n%s hardware serial number : ", prefix );
1852 MBEDTLS_X509_SAFE_SNPRINTF;
1853
1854 if( other_name->value.hardware_module_name.val.len >= n )
1855 {
1856 *p = '\0';
1857 return( MBEDTLS_ERR_X509_BUFFER_TOO_SMALL );
Janos Follath22f605f2019-05-10 10:37:17 +01001858 }
1859
Ron Eldora2913912019-05-16 16:17:38 +03001860 memcpy( p, other_name->value.hardware_module_name.val.p,
1861 other_name->value.hardware_module_name.val.len );
1862 p += other_name->value.hardware_module_name.val.len;
1863
Ron Eldor890819a2019-05-13 19:03:04 +03001864 n -= other_name->value.hardware_module_name.val.len;
Janos Follath2f0ec1e2019-05-10 11:06:31 +01001865
Ron Eldor890819a2019-05-13 19:03:04 +03001866 }/* MBEDTLS_OID_ON_HW_MODULE_NAME */
1867 }
1868 break;
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001869
1870 /*
1871 * dNSName
1872 */
Ron Eldora2913912019-05-16 16:17:38 +03001873 case MBEDTLS_X509_SAN_DNS_NAME:
Ron Eldor890819a2019-05-13 19:03:04 +03001874 {
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001875 ret = mbedtls_snprintf( p, n, "\n%s dNSName : ", prefix );
1876 MBEDTLS_X509_SAFE_SNPRINTF;
Ron Eldor890819a2019-05-13 19:03:04 +03001877 if( san.san.unstructured_name.len >= n )
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001878 {
1879 *p = '\0';
1880 return( MBEDTLS_ERR_X509_BUFFER_TOO_SMALL );
1881 }
Ron Eldora2913912019-05-16 16:17:38 +03001882
1883 memcpy( p, san.san.unstructured_name.p, san.san.unstructured_name.len );
1884 p += san.san.unstructured_name.len;
Ron Eldor890819a2019-05-13 19:03:04 +03001885 n -= san.san.unstructured_name.len;
Ron Eldor890819a2019-05-13 19:03:04 +03001886 }
1887 break;
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001888
1889 /*
Ron Eldor890819a2019-05-13 19:03:04 +03001890 * Type not supported, skip item.
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001891 */
1892 default:
Janos Follath22f605f2019-05-10 10:37:17 +01001893 ret = mbedtls_snprintf( p, n, "\n%s <unsupported>", prefix );
1894 MBEDTLS_X509_SAFE_SNPRINTF;
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001895 break;
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02001896 }
1897
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02001898 cur = cur->next;
1899 }
1900
1901 *p = '\0';
1902
1903 *size = n;
1904 *buf = p;
1905
1906 return( 0 );
1907}
1908
Ron Eldor890819a2019-05-13 19:03:04 +03001909int mbedtls_x509_parse_subject_alt_name( const mbedtls_x509_buf *san_buf,
1910 mbedtls_x509_subject_alternative_name *san )
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001911{
Janos Follath865b3eb2019-12-16 11:46:15 +00001912 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Ron Eldor890819a2019-05-13 19:03:04 +03001913 switch( san_buf->tag &
1914 ( MBEDTLS_ASN1_TAG_CLASS_MASK |
1915 MBEDTLS_ASN1_TAG_VALUE_MASK ) )
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001916 {
Ron Eldor890819a2019-05-13 19:03:04 +03001917 /*
1918 * otherName
1919 */
1920 case( MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_X509_SAN_OTHER_NAME ):
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001921 {
Ron Eldor890819a2019-05-13 19:03:04 +03001922 mbedtls_x509_san_other_name other_name;
1923
1924 ret = x509_get_other_name( san_buf, &other_name );
1925 if( ret != 0 )
1926 return( ret );
1927
1928 memset( san, 0, sizeof( mbedtls_x509_subject_alternative_name ) );
1929 san->type = MBEDTLS_X509_SAN_OTHER_NAME;
1930 memcpy( &san->san.other_name,
1931 &other_name, sizeof( other_name ) );
1932
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001933 }
Ron Eldor890819a2019-05-13 19:03:04 +03001934 break;
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001935
Ron Eldor890819a2019-05-13 19:03:04 +03001936 /*
1937 * dNSName
1938 */
1939 case( MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_X509_SAN_DNS_NAME ):
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001940 {
Ron Eldor890819a2019-05-13 19:03:04 +03001941 memset( san, 0, sizeof( mbedtls_x509_subject_alternative_name ) );
1942 san->type = MBEDTLS_X509_SAN_DNS_NAME;
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001943
Ron Eldor890819a2019-05-13 19:03:04 +03001944 memcpy( &san->san.unstructured_name,
1945 san_buf, sizeof( *san_buf ) );
Janos Follath6c379b42019-05-10 14:17:16 +01001946
Ron Eldor890819a2019-05-13 19:03:04 +03001947 }
1948 break;
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001949
Ron Eldor890819a2019-05-13 19:03:04 +03001950 /*
1951 * Type not supported
1952 */
1953 default:
1954 return( MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE );
1955 }
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001956 return( 0 );
1957}
1958
Manuel Pégourié-Gonnard0db29b02014-04-01 18:12:24 +02001959#define PRINT_ITEM(i) \
1960 { \
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001961 ret = mbedtls_snprintf( p, n, "%s" i, sep ); \
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001962 MBEDTLS_X509_SAFE_SNPRINTF; \
Manuel Pégourié-Gonnard0db29b02014-04-01 18:12:24 +02001963 sep = ", "; \
1964 }
1965
1966#define CERT_TYPE(type,name) \
Hanno Becker1eeca412018-10-15 12:01:35 +01001967 if( ns_cert_type & (type) ) \
Manuel Pégourié-Gonnard0db29b02014-04-01 18:12:24 +02001968 PRINT_ITEM( name );
1969
Manuel Pégourié-Gonnard919f8f52014-04-01 13:01:11 +02001970static int x509_info_cert_type( char **buf, size_t *size,
1971 unsigned char ns_cert_type )
1972{
Janos Follath865b3eb2019-12-16 11:46:15 +00001973 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard919f8f52014-04-01 13:01:11 +02001974 size_t n = *size;
1975 char *p = *buf;
Manuel Pégourié-Gonnard7b30cfc2014-04-01 18:00:07 +02001976 const char *sep = "";
Manuel Pégourié-Gonnard919f8f52014-04-01 13:01:11 +02001977
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001978 CERT_TYPE( MBEDTLS_X509_NS_CERT_TYPE_SSL_CLIENT, "SSL Client" );
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01001979 CERT_TYPE( MBEDTLS_X509_NS_CERT_TYPE_SSL_SERVER, "SSL Server" );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001980 CERT_TYPE( MBEDTLS_X509_NS_CERT_TYPE_EMAIL, "Email" );
1981 CERT_TYPE( MBEDTLS_X509_NS_CERT_TYPE_OBJECT_SIGNING, "Object Signing" );
1982 CERT_TYPE( MBEDTLS_X509_NS_CERT_TYPE_RESERVED, "Reserved" );
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01001983 CERT_TYPE( MBEDTLS_X509_NS_CERT_TYPE_SSL_CA, "SSL CA" );
1984 CERT_TYPE( MBEDTLS_X509_NS_CERT_TYPE_EMAIL_CA, "Email CA" );
1985 CERT_TYPE( MBEDTLS_X509_NS_CERT_TYPE_OBJECT_SIGNING_CA, "Object Signing CA" );
Manuel Pégourié-Gonnard919f8f52014-04-01 13:01:11 +02001986
1987 *size = n;
1988 *buf = p;
1989
1990 return( 0 );
1991}
1992
Manuel Pégourié-Gonnard0db29b02014-04-01 18:12:24 +02001993#define KEY_USAGE(code,name) \
Hanno Becker1eeca412018-10-15 12:01:35 +01001994 if( key_usage & (code) ) \
Manuel Pégourié-Gonnard0db29b02014-04-01 18:12:24 +02001995 PRINT_ITEM( name );
1996
Manuel Pégourié-Gonnard65c2ddc2014-04-01 14:12:11 +02001997static int x509_info_key_usage( char **buf, size_t *size,
Manuel Pégourié-Gonnard9a702252015-06-23 10:14:36 +02001998 unsigned int key_usage )
Manuel Pégourié-Gonnard65c2ddc2014-04-01 14:12:11 +02001999{
Janos Follath865b3eb2019-12-16 11:46:15 +00002000 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard65c2ddc2014-04-01 14:12:11 +02002001 size_t n = *size;
2002 char *p = *buf;
Manuel Pégourié-Gonnard7b30cfc2014-04-01 18:00:07 +02002003 const char *sep = "";
Manuel Pégourié-Gonnard65c2ddc2014-04-01 14:12:11 +02002004
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002005 KEY_USAGE( MBEDTLS_X509_KU_DIGITAL_SIGNATURE, "Digital Signature" );
2006 KEY_USAGE( MBEDTLS_X509_KU_NON_REPUDIATION, "Non Repudiation" );
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01002007 KEY_USAGE( MBEDTLS_X509_KU_KEY_ENCIPHERMENT, "Key Encipherment" );
2008 KEY_USAGE( MBEDTLS_X509_KU_DATA_ENCIPHERMENT, "Data Encipherment" );
2009 KEY_USAGE( MBEDTLS_X509_KU_KEY_AGREEMENT, "Key Agreement" );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002010 KEY_USAGE( MBEDTLS_X509_KU_KEY_CERT_SIGN, "Key Cert Sign" );
2011 KEY_USAGE( MBEDTLS_X509_KU_CRL_SIGN, "CRL Sign" );
Manuel Pégourié-Gonnard9a702252015-06-23 10:14:36 +02002012 KEY_USAGE( MBEDTLS_X509_KU_ENCIPHER_ONLY, "Encipher Only" );
2013 KEY_USAGE( MBEDTLS_X509_KU_DECIPHER_ONLY, "Decipher Only" );
Manuel Pégourié-Gonnard65c2ddc2014-04-01 14:12:11 +02002014
2015 *size = n;
2016 *buf = p;
2017
2018 return( 0 );
2019}
2020
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02002021static int x509_info_ext_key_usage( char **buf, size_t *size,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002022 const mbedtls_x509_sequence *extended_key_usage )
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02002023{
Janos Follath865b3eb2019-12-16 11:46:15 +00002024 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02002025 const char *desc;
2026 size_t n = *size;
2027 char *p = *buf;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002028 const mbedtls_x509_sequence *cur = extended_key_usage;
Manuel Pégourié-Gonnard7b30cfc2014-04-01 18:00:07 +02002029 const char *sep = "";
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02002030
2031 while( cur != NULL )
2032 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002033 if( mbedtls_oid_get_extended_key_usage( &cur->buf, &desc ) != 0 )
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02002034 desc = "???";
2035
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002036 ret = mbedtls_snprintf( p, n, "%s%s", sep, desc );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002037 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02002038
Manuel Pégourié-Gonnard7b30cfc2014-04-01 18:00:07 +02002039 sep = ", ";
2040
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02002041 cur = cur->next;
2042 }
2043
2044 *size = n;
2045 *buf = p;
2046
2047 return( 0 );
2048}
2049
Ron Eldor74d9acc2019-03-21 14:00:03 +02002050static int x509_info_cert_policies( char **buf, size_t *size,
2051 const mbedtls_x509_sequence *certificate_policies )
2052{
Janos Follath865b3eb2019-12-16 11:46:15 +00002053 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Ron Eldor74d9acc2019-03-21 14:00:03 +02002054 const char *desc;
2055 size_t n = *size;
2056 char *p = *buf;
2057 const mbedtls_x509_sequence *cur = certificate_policies;
2058 const char *sep = "";
2059
2060 while( cur != NULL )
2061 {
2062 if( mbedtls_oid_get_certificate_policies( &cur->buf, &desc ) != 0 )
2063 desc = "???";
2064
2065 ret = mbedtls_snprintf( p, n, "%s%s", sep, desc );
2066 MBEDTLS_X509_SAFE_SNPRINTF;
2067
2068 sep = ", ";
2069
2070 cur = cur->next;
2071 }
2072
2073 *size = n;
2074 *buf = p;
2075
2076 return( 0 );
2077}
2078
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002079/*
2080 * Return an informational string about the certificate.
2081 */
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002082#define BEFORE_COLON 18
2083#define BC "18"
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002084int mbedtls_x509_crt_info( char *buf, size_t size, const char *prefix,
2085 const mbedtls_x509_crt *crt )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002086{
Janos Follath865b3eb2019-12-16 11:46:15 +00002087 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002088 size_t n;
2089 char *p;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002090 char key_size_str[BEFORE_COLON];
2091
2092 p = buf;
2093 n = size;
2094
Janos Follath98e28a72016-05-31 14:03:54 +01002095 if( NULL == crt )
2096 {
2097 ret = mbedtls_snprintf( p, n, "\nCertificate is uninitialised!\n" );
2098 MBEDTLS_X509_SAFE_SNPRINTF;
2099
2100 return( (int) ( size - n ) );
2101 }
2102
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002103 ret = mbedtls_snprintf( p, n, "%scert. version : %d\n",
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002104 prefix, crt->version );
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_snprintf( p, n, "%sserial number : ",
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002107 prefix );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002108 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002109
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002110 ret = mbedtls_x509_serial_gets( p, n, &crt->serial );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002111 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002112
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002113 ret = mbedtls_snprintf( p, n, "\n%sissuer name : ", prefix );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002114 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002115 ret = mbedtls_x509_dn_gets( p, n, &crt->issuer );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002116 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002117
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002118 ret = mbedtls_snprintf( p, n, "\n%ssubject name : ", prefix );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002119 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002120 ret = mbedtls_x509_dn_gets( p, n, &crt->subject );
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%sissued on : " \
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002124 "%04d-%02d-%02d %02d:%02d:%02d", prefix,
2125 crt->valid_from.year, crt->valid_from.mon,
2126 crt->valid_from.day, crt->valid_from.hour,
2127 crt->valid_from.min, crt->valid_from.sec );
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é-Gonnard2cf5a7c2015-04-08 12:49:31 +02002130 ret = mbedtls_snprintf( p, n, "\n%sexpires on : " \
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002131 "%04d-%02d-%02d %02d:%02d:%02d", prefix,
2132 crt->valid_to.year, crt->valid_to.mon,
2133 crt->valid_to.day, crt->valid_to.hour,
2134 crt->valid_to.min, crt->valid_to.sec );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002135 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002136
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002137 ret = mbedtls_snprintf( p, n, "\n%ssigned using : ", prefix );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002138 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002139
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002140 ret = mbedtls_x509_sig_alg_gets( p, n, &crt->sig_oid, crt->sig_pk,
Manuel Pégourié-Gonnardbf696d02014-06-05 17:07:30 +02002141 crt->sig_md, crt->sig_opts );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002142 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002143
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002144 /* Key size */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002145 if( ( ret = mbedtls_x509_key_size_helper( key_size_str, BEFORE_COLON,
2146 mbedtls_pk_get_name( &crt->pk ) ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002147 {
2148 return( ret );
2149 }
2150
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002151 ret = mbedtls_snprintf( p, n, "\n%s%-" BC "s: %d bits", prefix, key_size_str,
Manuel Pégourié-Gonnard097c7bb2015-06-18 16:43:38 +02002152 (int) mbedtls_pk_get_bitlen( &crt->pk ) );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002153 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002154
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002155 /*
2156 * Optional extensions
2157 */
2158
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01002159 if( crt->ext_types & MBEDTLS_X509_EXT_BASIC_CONSTRAINTS )
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002160 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002161 ret = mbedtls_snprintf( p, n, "\n%sbasic constraints : CA=%s", prefix,
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002162 crt->ca_istrue ? "true" : "false" );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002163 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002164
2165 if( crt->max_pathlen > 0 )
2166 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002167 ret = mbedtls_snprintf( p, n, ", max_pathlen=%d", crt->max_pathlen - 1 );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002168 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002169 }
2170 }
2171
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01002172 if( crt->ext_types & MBEDTLS_X509_EXT_SUBJECT_ALT_NAME )
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002173 {
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02002174 ret = mbedtls_snprintf( p, n, "\n%ssubject alt name :", prefix );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002175 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02002176
2177 if( ( ret = x509_info_subject_alt_name( &p, &n,
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02002178 &crt->subject_alt_names,
Ron Eldor6aeae9e2019-05-20 12:00:36 +03002179 prefix ) ) != 0 )
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02002180 return( ret );
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002181 }
2182
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002183 if( crt->ext_types & MBEDTLS_X509_EXT_NS_CERT_TYPE )
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002184 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002185 ret = mbedtls_snprintf( p, n, "\n%scert. type : ", prefix );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002186 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnard919f8f52014-04-01 13:01:11 +02002187
2188 if( ( ret = x509_info_cert_type( &p, &n, crt->ns_cert_type ) ) != 0 )
2189 return( ret );
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002190 }
2191
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002192 if( crt->ext_types & MBEDTLS_X509_EXT_KEY_USAGE )
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002193 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002194 ret = mbedtls_snprintf( p, n, "\n%skey usage : ", prefix );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002195 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnard65c2ddc2014-04-01 14:12:11 +02002196
2197 if( ( ret = x509_info_key_usage( &p, &n, crt->key_usage ) ) != 0 )
2198 return( ret );
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002199 }
2200
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002201 if( crt->ext_types & MBEDTLS_X509_EXT_EXTENDED_KEY_USAGE )
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002202 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002203 ret = mbedtls_snprintf( p, n, "\n%sext key usage : ", prefix );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002204 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02002205
2206 if( ( ret = x509_info_ext_key_usage( &p, &n,
2207 &crt->ext_key_usage ) ) != 0 )
2208 return( ret );
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002209 }
2210
Ron Eldor74d9acc2019-03-21 14:00:03 +02002211 if( crt->ext_types & MBEDTLS_OID_X509_EXT_CERTIFICATE_POLICIES )
2212 {
2213 ret = mbedtls_snprintf( p, n, "\n%scertificate policies : ", prefix );
2214 MBEDTLS_X509_SAFE_SNPRINTF;
2215
2216 if( ( ret = x509_info_cert_policies( &p, &n,
2217 &crt->certificate_policies ) ) != 0 )
2218 return( ret );
2219 }
2220
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002221 ret = mbedtls_snprintf( p, n, "\n" );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002222 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002223
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002224 return( (int) ( size - n ) );
2225}
2226
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01002227struct x509_crt_verify_string {
2228 int code;
2229 const char *string;
2230};
2231
2232static const struct x509_crt_verify_string x509_crt_verify_strings[] = {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01002233 { MBEDTLS_X509_BADCERT_EXPIRED, "The certificate validity has expired" },
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01002234 { MBEDTLS_X509_BADCERT_REVOKED, "The certificate has been revoked (is on a CRL)" },
2235 { MBEDTLS_X509_BADCERT_CN_MISMATCH, "The certificate Common Name (CN) does not match with the expected CN" },
2236 { MBEDTLS_X509_BADCERT_NOT_TRUSTED, "The certificate is not correctly signed by the trusted CA" },
2237 { MBEDTLS_X509_BADCRL_NOT_TRUSTED, "The CRL is not correctly signed by the trusted CA" },
2238 { MBEDTLS_X509_BADCRL_EXPIRED, "The CRL is expired" },
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01002239 { MBEDTLS_X509_BADCERT_MISSING, "Certificate was missing" },
2240 { MBEDTLS_X509_BADCERT_SKIP_VERIFY, "Certificate verification was skipped" },
2241 { MBEDTLS_X509_BADCERT_OTHER, "Other reason (can be used by verify callback)" },
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01002242 { MBEDTLS_X509_BADCERT_FUTURE, "The certificate validity starts in the future" },
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01002243 { MBEDTLS_X509_BADCRL_FUTURE, "The CRL is from the future" },
2244 { MBEDTLS_X509_BADCERT_KEY_USAGE, "Usage does not match the keyUsage extension" },
2245 { MBEDTLS_X509_BADCERT_EXT_KEY_USAGE, "Usage does not match the extendedKeyUsage extension" },
2246 { MBEDTLS_X509_BADCERT_NS_CERT_TYPE, "Usage does not match the nsCertType extension" },
Manuel Pégourié-Gonnard95051642015-06-15 10:39:46 +02002247 { MBEDTLS_X509_BADCERT_BAD_MD, "The certificate is signed with an unacceptable hash." },
2248 { MBEDTLS_X509_BADCERT_BAD_PK, "The certificate is signed with an unacceptable PK alg (eg RSA vs ECDSA)." },
2249 { MBEDTLS_X509_BADCERT_BAD_KEY, "The certificate is signed with an unacceptable key (eg bad curve, RSA too short)." },
2250 { MBEDTLS_X509_BADCRL_BAD_MD, "The CRL is signed with an unacceptable hash." },
2251 { MBEDTLS_X509_BADCRL_BAD_PK, "The CRL is signed with an unacceptable PK alg (eg RSA vs ECDSA)." },
2252 { 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 +01002253 { 0, NULL }
2254};
2255
2256int mbedtls_x509_crt_verify_info( char *buf, size_t size, const char *prefix,
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02002257 uint32_t flags )
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01002258{
Janos Follath865b3eb2019-12-16 11:46:15 +00002259 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01002260 const struct x509_crt_verify_string *cur;
2261 char *p = buf;
2262 size_t n = size;
2263
2264 for( cur = x509_crt_verify_strings; cur->string != NULL ; cur++ )
2265 {
2266 if( ( flags & cur->code ) == 0 )
2267 continue;
2268
2269 ret = mbedtls_snprintf( p, n, "%s%s\n", prefix, cur->string );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002270 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01002271 flags ^= cur->code;
2272 }
2273
2274 if( flags != 0 )
2275 {
2276 ret = mbedtls_snprintf( p, n, "%sUnknown reason "
2277 "(this should not happen)\n", prefix );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002278 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01002279 }
2280
2281 return( (int) ( size - n ) );
2282}
2283
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002284#if defined(MBEDTLS_X509_CHECK_KEY_USAGE)
Manuel Pégourié-Gonnard655a9642015-06-23 10:48:44 +02002285int mbedtls_x509_crt_check_key_usage( const mbedtls_x509_crt *crt,
2286 unsigned int usage )
Manuel Pégourié-Gonnard603116c2014-04-09 09:50:03 +02002287{
Manuel Pégourié-Gonnard655a9642015-06-23 10:48:44 +02002288 unsigned int usage_must, usage_may;
2289 unsigned int may_mask = MBEDTLS_X509_KU_ENCIPHER_ONLY
2290 | MBEDTLS_X509_KU_DECIPHER_ONLY;
2291
2292 if( ( crt->ext_types & MBEDTLS_X509_EXT_KEY_USAGE ) == 0 )
2293 return( 0 );
2294
2295 usage_must = usage & ~may_mask;
2296
2297 if( ( ( crt->key_usage & ~may_mask ) & usage_must ) != usage_must )
2298 return( MBEDTLS_ERR_X509_BAD_INPUT_DATA );
2299
2300 usage_may = usage & may_mask;
2301
2302 if( ( ( crt->key_usage & may_mask ) | usage_may ) != usage_may )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002303 return( MBEDTLS_ERR_X509_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard603116c2014-04-09 09:50:03 +02002304
2305 return( 0 );
2306}
2307#endif
2308
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002309#if defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE)
2310int mbedtls_x509_crt_check_extended_key_usage( const mbedtls_x509_crt *crt,
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02002311 const char *usage_oid,
2312 size_t usage_len )
2313{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002314 const mbedtls_x509_sequence *cur;
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02002315
2316 /* Extension is not mandatory, absent means no restriction */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002317 if( ( crt->ext_types & MBEDTLS_X509_EXT_EXTENDED_KEY_USAGE ) == 0 )
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02002318 return( 0 );
2319
2320 /*
2321 * Look for the requested usage (or wildcard ANY) in our list
2322 */
2323 for( cur = &crt->ext_key_usage; cur != NULL; cur = cur->next )
2324 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002325 const mbedtls_x509_buf *cur_oid = &cur->buf;
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02002326
2327 if( cur_oid->len == usage_len &&
2328 memcmp( cur_oid->p, usage_oid, usage_len ) == 0 )
2329 {
2330 return( 0 );
2331 }
2332
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002333 if( MBEDTLS_OID_CMP( MBEDTLS_OID_ANY_EXTENDED_KEY_USAGE, cur_oid ) == 0 )
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02002334 return( 0 );
2335 }
2336
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002337 return( MBEDTLS_ERR_X509_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02002338}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002339#endif /* MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE */
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02002340
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002341#if defined(MBEDTLS_X509_CRL_PARSE_C)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002342/*
2343 * Return 1 if the certificate is revoked, or 0 otherwise.
2344 */
Manuel Pégourié-Gonnardc730ed32015-06-02 10:38:50 +01002345int mbedtls_x509_crt_is_revoked( const mbedtls_x509_crt *crt, const mbedtls_x509_crl *crl )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002346{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002347 const mbedtls_x509_crl_entry *cur = &crl->entry;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002348
2349 while( cur != NULL && cur->serial.len != 0 )
2350 {
2351 if( crt->serial.len == cur->serial.len &&
2352 memcmp( crt->serial.p, cur->serial.p, crt->serial.len ) == 0 )
2353 {
Raoul Strackxa4e86142020-06-15 17:03:13 +02002354 return( 1 );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002355 }
2356
2357 cur = cur->next;
2358 }
2359
2360 return( 0 );
2361}
2362
2363/*
Manuel Pégourié-Gonnardeeef9472016-02-22 11:36:55 +01002364 * Check that the given certificate is not revoked according to the CRL.
Manuel Pégourié-Gonnard08eacec2017-10-18 14:20:24 +02002365 * Skip validation if no CRL for the given CA is present.
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002366 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002367static int x509_crt_verifycrl( mbedtls_x509_crt *crt, mbedtls_x509_crt *ca,
Manuel Pégourié-Gonnard95051642015-06-15 10:39:46 +02002368 mbedtls_x509_crl *crl_list,
2369 const mbedtls_x509_crt_profile *profile )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002370{
2371 int flags = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002372 unsigned char hash[MBEDTLS_MD_MAX_SIZE];
2373 const mbedtls_md_info_t *md_info;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002374
2375 if( ca == NULL )
2376 return( flags );
2377
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002378 while( crl_list != NULL )
2379 {
2380 if( crl_list->version == 0 ||
Hanno Beckerb75ffb52018-11-02 09:19:54 +00002381 x509_name_cmp( &crl_list->issuer, &ca->subject ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002382 {
2383 crl_list = crl_list->next;
2384 continue;
2385 }
2386
2387 /*
Manuel Pégourié-Gonnard99d4f192014-04-08 15:10:07 +02002388 * Check if the CA is configured to sign CRLs
2389 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002390#if defined(MBEDTLS_X509_CHECK_KEY_USAGE)
Hanno Beckerb75ffb52018-11-02 09:19:54 +00002391 if( mbedtls_x509_crt_check_key_usage( ca,
2392 MBEDTLS_X509_KU_CRL_SIGN ) != 0 )
Manuel Pégourié-Gonnard99d4f192014-04-08 15:10:07 +02002393 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002394 flags |= MBEDTLS_X509_BADCRL_NOT_TRUSTED;
Manuel Pégourié-Gonnard99d4f192014-04-08 15:10:07 +02002395 break;
2396 }
2397#endif
2398
2399 /*
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002400 * Check if CRL is correctly signed by the trusted CA
2401 */
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +02002402 if( x509_profile_check_md_alg( profile, crl_list->sig_md ) != 0 )
2403 flags |= MBEDTLS_X509_BADCRL_BAD_MD;
2404
2405 if( x509_profile_check_pk_alg( profile, crl_list->sig_pk ) != 0 )
2406 flags |= MBEDTLS_X509_BADCRL_BAD_PK;
2407
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002408 md_info = mbedtls_md_info_from_type( crl_list->sig_md );
Manuel Pégourié-Gonnard329e78c2017-06-26 12:22:17 +02002409 if( mbedtls_md( md_info, crl_list->tbs.p, crl_list->tbs.len, hash ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002410 {
Manuel Pégourié-Gonnard329e78c2017-06-26 12:22:17 +02002411 /* Note: this can't happen except after an internal error */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002412 flags |= MBEDTLS_X509_BADCRL_NOT_TRUSTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002413 break;
2414 }
2415
Manuel Pégourié-Gonnard3f816912017-10-26 10:24:16 +02002416 if( x509_profile_check_key( profile, &ca->pk ) != 0 )
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +02002417 flags |= MBEDTLS_X509_BADCERT_BAD_KEY;
Manuel Pégourié-Gonnard95051642015-06-15 10:39:46 +02002418
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002419 if( mbedtls_pk_verify_ext( crl_list->sig_pk, crl_list->sig_opts, &ca->pk,
2420 crl_list->sig_md, hash, mbedtls_md_get_size( md_info ),
Manuel Pégourié-Gonnard53882022014-06-05 17:53:52 +02002421 crl_list->sig.p, crl_list->sig.len ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002422 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002423 flags |= MBEDTLS_X509_BADCRL_NOT_TRUSTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002424 break;
2425 }
2426
2427 /*
2428 * Check for validity of CRL (Do not drop out)
2429 */
Manuel Pégourié-Gonnardc730ed32015-06-02 10:38:50 +01002430 if( mbedtls_x509_time_is_past( &crl_list->next_update ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002431 flags |= MBEDTLS_X509_BADCRL_EXPIRED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002432
Manuel Pégourié-Gonnardc730ed32015-06-02 10:38:50 +01002433 if( mbedtls_x509_time_is_future( &crl_list->this_update ) )
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01002434 flags |= MBEDTLS_X509_BADCRL_FUTURE;
Manuel Pégourié-Gonnard95337652014-03-10 13:15:18 +01002435
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002436 /*
2437 * Check if certificate is revoked
2438 */
Manuel Pégourié-Gonnardc730ed32015-06-02 10:38:50 +01002439 if( mbedtls_x509_crt_is_revoked( crt, crl_list ) )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002440 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002441 flags |= MBEDTLS_X509_BADCERT_REVOKED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002442 break;
2443 }
2444
2445 crl_list = crl_list->next;
2446 }
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +02002447
Paul Bakkerd8bb8262014-06-17 14:06:49 +02002448 return( flags );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002449}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002450#endif /* MBEDTLS_X509_CRL_PARSE_C */
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002451
Manuel Pégourié-Gonnard88421242014-10-17 11:36:18 +02002452/*
Manuel Pégourié-Gonnardf82a4d52017-07-03 19:26:25 +02002453 * Check the signature of a certificate by its parent
2454 */
2455static int x509_crt_check_signature( const mbedtls_x509_crt *child,
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002456 mbedtls_x509_crt *parent,
2457 mbedtls_x509_crt_restart_ctx *rs_ctx )
Manuel Pégourié-Gonnardf82a4d52017-07-03 19:26:25 +02002458{
Manuel Pégourié-Gonnardf82a4d52017-07-03 19:26:25 +02002459 unsigned char hash[MBEDTLS_MD_MAX_SIZE];
Andrzej Kurekd4a65532018-10-31 06:18:39 -04002460 size_t hash_len;
2461#if !defined(MBEDTLS_USE_PSA_CRYPTO)
2462 const mbedtls_md_info_t *md_info;
Manuel Pégourié-Gonnardf82a4d52017-07-03 19:26:25 +02002463 md_info = mbedtls_md_info_from_type( child->sig_md );
Andrzej Kurekd4a65532018-10-31 06:18:39 -04002464 hash_len = mbedtls_md_get_size( md_info );
Andrzej Kurek8b38ff52018-11-20 03:20:09 -05002465
Andrzej Kurekd4a65532018-10-31 06:18:39 -04002466 /* Note: hash errors can happen only after an internal error */
Manuel Pégourié-Gonnardf82a4d52017-07-03 19:26:25 +02002467 if( mbedtls_md( md_info, child->tbs.p, child->tbs.len, hash ) != 0 )
Andrzej Kurekd4a65532018-10-31 06:18:39 -04002468 return( -1 );
2469#else
Jaeden Amero34973232019-02-20 10:32:28 +00002470 psa_hash_operation_t hash_operation = PSA_HASH_OPERATION_INIT;
Andrzej Kurekd4a65532018-10-31 06:18:39 -04002471 psa_algorithm_t hash_alg = mbedtls_psa_translate_md( child->sig_md );
2472
2473 if( psa_hash_setup( &hash_operation, hash_alg ) != PSA_SUCCESS )
2474 return( -1 );
2475
2476 if( psa_hash_update( &hash_operation, child->tbs.p, child->tbs.len )
2477 != PSA_SUCCESS )
Manuel Pégourié-Gonnardf82a4d52017-07-03 19:26:25 +02002478 {
Manuel Pégourié-Gonnardf82a4d52017-07-03 19:26:25 +02002479 return( -1 );
2480 }
2481
Andrzej Kurekd4a65532018-10-31 06:18:39 -04002482 if( psa_hash_finish( &hash_operation, hash, sizeof( hash ), &hash_len )
2483 != PSA_SUCCESS )
2484 {
Andrzej Kurekd4a65532018-10-31 06:18:39 -04002485 return( -1 );
2486 }
2487#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002488 /* Skip expensive computation on obvious mismatch */
2489 if( ! mbedtls_pk_can_do( &parent->pk, child->sig_pk ) )
Manuel Pégourié-Gonnardf82a4d52017-07-03 19:26:25 +02002490 return( -1 );
Manuel Pégourié-Gonnardf82a4d52017-07-03 19:26:25 +02002491
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002492#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002493 if( rs_ctx != NULL && child->sig_pk == MBEDTLS_PK_ECDSA )
2494 {
2495 return( mbedtls_pk_verify_restartable( &parent->pk,
Andrzej Kurekd4a65532018-10-31 06:18:39 -04002496 child->sig_md, hash, hash_len,
Manuel Pégourié-Gonnard15d7df22017-08-17 14:33:31 +02002497 child->sig.p, child->sig.len, &rs_ctx->pk ) );
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002498 }
2499#else
2500 (void) rs_ctx;
2501#endif
2502
2503 return( mbedtls_pk_verify_ext( child->sig_pk, child->sig_opts, &parent->pk,
Andrzej Kurekd4a65532018-10-31 06:18:39 -04002504 child->sig_md, hash, hash_len,
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002505 child->sig.p, child->sig.len ) );
Manuel Pégourié-Gonnardf82a4d52017-07-03 19:26:25 +02002506}
2507
2508/*
Manuel Pégourié-Gonnard312010e2014-04-09 14:30:11 +02002509 * Check if 'parent' is a suitable parent (signing CA) for 'child'.
2510 * Return 0 if yes, -1 if not.
Manuel Pégourié-Gonnardd249b7a2014-06-24 11:49:16 +02002511 *
2512 * top means parent is a locally-trusted certificate
Manuel Pégourié-Gonnard3fed0b32014-04-08 13:18:01 +02002513 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002514static int x509_crt_check_parent( const mbedtls_x509_crt *child,
2515 const mbedtls_x509_crt *parent,
Manuel Pégourié-Gonnard27e94792017-07-04 00:49:31 +02002516 int top )
Manuel Pégourié-Gonnard3fed0b32014-04-08 13:18:01 +02002517{
Manuel Pégourié-Gonnardd249b7a2014-06-24 11:49:16 +02002518 int need_ca_bit;
2519
Manuel Pégourié-Gonnardc4eff162014-06-19 12:18:08 +02002520 /* Parent must be the issuer */
Manuel Pégourié-Gonnardef9a6ae2014-10-17 12:25:12 +02002521 if( x509_name_cmp( &child->issuer, &parent->subject ) != 0 )
Manuel Pégourié-Gonnard312010e2014-04-09 14:30:11 +02002522 return( -1 );
Manuel Pégourié-Gonnard3fed0b32014-04-08 13:18:01 +02002523
Manuel Pégourié-Gonnardd249b7a2014-06-24 11:49:16 +02002524 /* Parent must have the basicConstraints CA bit set as a general rule */
2525 need_ca_bit = 1;
2526
2527 /* Exception: v1/v2 certificates that are locally trusted. */
2528 if( top && parent->version < 3 )
2529 need_ca_bit = 0;
2530
Manuel Pégourié-Gonnardd249b7a2014-06-24 11:49:16 +02002531 if( need_ca_bit && ! parent->ca_istrue )
2532 return( -1 );
2533
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002534#if defined(MBEDTLS_X509_CHECK_KEY_USAGE)
Manuel Pégourié-Gonnardd249b7a2014-06-24 11:49:16 +02002535 if( need_ca_bit &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002536 mbedtls_x509_crt_check_key_usage( parent, MBEDTLS_X509_KU_KEY_CERT_SIGN ) != 0 )
Manuel Pégourié-Gonnardc4eff162014-06-19 12:18:08 +02002537 {
2538 return( -1 );
2539 }
Manuel Pégourié-Gonnard312010e2014-04-09 14:30:11 +02002540#endif
2541
2542 return( 0 );
Manuel Pégourié-Gonnard3fed0b32014-04-08 13:18:01 +02002543}
2544
Manuel Pégourié-Gonnard35407c72017-06-29 10:45:25 +02002545/*
Manuel Pégourié-Gonnard3e329b82017-06-29 12:55:27 +02002546 * Find a suitable parent for child in candidates, or return NULL.
2547 *
2548 * Here suitable is defined as:
Manuel Pégourié-Gonnard2f09d592017-07-03 18:30:43 +02002549 * 1. subject name matches child's issuer
2550 * 2. if necessary, the CA bit is set and key usage allows signing certs
2551 * 3. for trusted roots, the signature is correct
Manuel Pégourié-Gonnardbe4ff422017-07-14 12:04:14 +02002552 * (for intermediates, the signature is checked and the result reported)
Manuel Pégourié-Gonnard2f09d592017-07-03 18:30:43 +02002553 * 4. pathlen constraints are satisfied
Manuel Pégourié-Gonnard3e329b82017-06-29 12:55:27 +02002554 *
Manuel Pégourié-Gonnard562df402017-08-08 18:09:14 +02002555 * If there's a suitable candidate which is also time-valid, return the first
2556 * such. Otherwise, return the first suitable candidate (or NULL if there is
2557 * none).
Manuel Pégourié-Gonnard3e329b82017-06-29 12:55:27 +02002558 *
2559 * The rationale for this rule is that someone could have a list of trusted
2560 * roots with two versions on the same root with different validity periods.
2561 * (At least one user reported having such a list and wanted it to just work.)
2562 * The reason we don't just require time-validity is that generally there is
2563 * only one version, and if it's expired we want the flags to state that
2564 * rather than NOT_TRUSTED, as would be the case if we required it here.
Manuel Pégourié-Gonnard2f09d592017-07-03 18:30:43 +02002565 *
2566 * The rationale for rule 3 (signature for trusted roots) is that users might
2567 * have two versions of the same CA with different keys in their list, and the
Manuel Pégourié-Gonnardbdc54402017-07-04 00:33:39 +02002568 * way we select the correct one is by checking the signature (as we don't
2569 * rely on key identifier extensions). (This is one way users might choose to
2570 * handle key rollover, another relies on self-issued certs, see [SIRO].)
Manuel Pégourié-Gonnard98a67782017-08-17 10:52:20 +02002571 *
2572 * Arguments:
Manuel Pégourié-Gonnarde57d7432018-03-07 10:00:57 +01002573 * - [in] child: certificate for which we're looking for a parent
2574 * - [in] candidates: chained list of potential parents
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +02002575 * - [out] r_parent: parent found (or NULL)
2576 * - [out] r_signature_is_good: 1 if child signature by parent is valid, or 0
Manuel Pégourié-Gonnarde57d7432018-03-07 10:00:57 +01002577 * - [in] top: 1 if candidates consists of trusted roots, ie we're at the top
2578 * of the chain, 0 otherwise
2579 * - [in] path_cnt: number of intermediates seen so far
2580 * - [in] self_cnt: number of self-signed intermediates seen so far
2581 * (will never be greater than path_cnt)
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +02002582 * - [in-out] rs_ctx: context for restarting operations
Manuel Pégourié-Gonnarde57d7432018-03-07 10:00:57 +01002583 *
2584 * Return value:
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +02002585 * - 0 on success
2586 * - MBEDTLS_ERR_ECP_IN_PROGRESS otherwise
Manuel Pégourié-Gonnard2f1c33d2017-06-29 12:27:23 +02002587 */
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002588static int x509_crt_find_parent_in(
2589 mbedtls_x509_crt *child,
2590 mbedtls_x509_crt *candidates,
2591 mbedtls_x509_crt **r_parent,
2592 int *r_signature_is_good,
2593 int top,
Manuel Pégourié-Gonnardbb216bd2017-08-28 13:25:55 +02002594 unsigned path_cnt,
2595 unsigned self_cnt,
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002596 mbedtls_x509_crt_restart_ctx *rs_ctx )
Manuel Pégourié-Gonnard2f1c33d2017-06-29 12:27:23 +02002597{
Janos Follath865b3eb2019-12-16 11:46:15 +00002598 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002599 mbedtls_x509_crt *parent, *fallback_parent;
Benjamin Kier36050732019-05-30 14:49:17 -04002600 int signature_is_good = 0, fallback_signature_is_good;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002601
2602#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard3627a8b2017-08-23 11:20:48 +02002603 /* did we have something in progress? */
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002604 if( rs_ctx != NULL && rs_ctx->parent != NULL )
2605 {
Manuel Pégourié-Gonnard3627a8b2017-08-23 11:20:48 +02002606 /* restore saved state */
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002607 parent = rs_ctx->parent;
2608 fallback_parent = rs_ctx->fallback_parent;
Manuel Pégourié-Gonnard78d7e8c2018-07-02 12:33:14 +02002609 fallback_signature_is_good = rs_ctx->fallback_signature_is_good;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002610
Manuel Pégourié-Gonnard3627a8b2017-08-23 11:20:48 +02002611 /* clear saved state */
2612 rs_ctx->parent = NULL;
2613 rs_ctx->fallback_parent = NULL;
Manuel Pégourié-Gonnard78d7e8c2018-07-02 12:33:14 +02002614 rs_ctx->fallback_signature_is_good = 0;
Manuel Pégourié-Gonnard3627a8b2017-08-23 11:20:48 +02002615
2616 /* resume where we left */
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002617 goto check_signature;
2618 }
2619#endif
2620
2621 fallback_parent = NULL;
Manuel Pégourié-Gonnard78d7e8c2018-07-02 12:33:14 +02002622 fallback_signature_is_good = 0;
Manuel Pégourié-Gonnard2f1c33d2017-06-29 12:27:23 +02002623
Manuel Pégourié-Gonnard2f1c33d2017-06-29 12:27:23 +02002624 for( parent = candidates; parent != NULL; parent = parent->next )
2625 {
Manuel Pégourié-Gonnard2f09d592017-07-03 18:30:43 +02002626 /* basic parenting skills (name, CA bit, key usage) */
Manuel Pégourié-Gonnard27e94792017-07-04 00:49:31 +02002627 if( x509_crt_check_parent( child, parent, top ) != 0 )
Manuel Pégourié-Gonnard2f1c33d2017-06-29 12:27:23 +02002628 continue;
2629
Manuel Pégourié-Gonnard9c6118c2017-06-29 12:38:42 +02002630 /* +1 because stored max_pathlen is 1 higher that the actual value */
2631 if( parent->max_pathlen > 0 &&
Manuel Pégourié-Gonnardbb216bd2017-08-28 13:25:55 +02002632 (size_t) parent->max_pathlen < 1 + path_cnt - self_cnt )
Manuel Pégourié-Gonnard9c6118c2017-06-29 12:38:42 +02002633 {
2634 continue;
2635 }
2636
Manuel Pégourié-Gonnard2f09d592017-07-03 18:30:43 +02002637 /* Signature */
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002638#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
2639check_signature:
2640#endif
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002641 ret = x509_crt_check_signature( child, parent, rs_ctx );
2642
2643#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002644 if( rs_ctx != NULL && ret == MBEDTLS_ERR_ECP_IN_PROGRESS )
2645 {
2646 /* save state */
2647 rs_ctx->parent = parent;
2648 rs_ctx->fallback_parent = fallback_parent;
Manuel Pégourié-Gonnard78d7e8c2018-07-02 12:33:14 +02002649 rs_ctx->fallback_signature_is_good = fallback_signature_is_good;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002650
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002651 return( ret );
2652 }
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002653#else
2654 (void) ret;
2655#endif
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002656
2657 signature_is_good = ret == 0;
2658 if( top && ! signature_is_good )
Manuel Pégourié-Gonnardf82a4d52017-07-03 19:26:25 +02002659 continue;
Manuel Pégourié-Gonnard2f09d592017-07-03 18:30:43 +02002660
Manuel Pégourié-Gonnard562df402017-08-08 18:09:14 +02002661 /* optional time check */
Manuel Pégourié-Gonnard3e329b82017-06-29 12:55:27 +02002662 if( mbedtls_x509_time_is_past( &parent->valid_to ) ||
2663 mbedtls_x509_time_is_future( &parent->valid_from ) )
2664 {
Manuel Pégourié-Gonnardbe4ff422017-07-14 12:04:14 +02002665 if( fallback_parent == NULL )
2666 {
2667 fallback_parent = parent;
Manuel Pégourié-Gonnard78d7e8c2018-07-02 12:33:14 +02002668 fallback_signature_is_good = signature_is_good;
Manuel Pégourié-Gonnardbe4ff422017-07-14 12:04:14 +02002669 }
Manuel Pégourié-Gonnard3e329b82017-06-29 12:55:27 +02002670
2671 continue;
2672 }
2673
Andy Gross1f627142019-01-30 10:25:53 -06002674 *r_parent = parent;
2675 *r_signature_is_good = signature_is_good;
2676
Manuel Pégourié-Gonnard2f1c33d2017-06-29 12:27:23 +02002677 break;
2678 }
2679
Andy Gross1f627142019-01-30 10:25:53 -06002680 if( parent == NULL )
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002681 {
2682 *r_parent = fallback_parent;
Manuel Pégourié-Gonnard78d7e8c2018-07-02 12:33:14 +02002683 *r_signature_is_good = fallback_signature_is_good;
Manuel Pégourié-Gonnardbe4ff422017-07-14 12:04:14 +02002684 }
Manuel Pégourié-Gonnard3e329b82017-06-29 12:55:27 +02002685
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002686 return( 0 );
Manuel Pégourié-Gonnard2f1c33d2017-06-29 12:27:23 +02002687}
2688
2689/*
Manuel Pégourié-Gonnard63686122017-07-04 01:01:39 +02002690 * Find a parent in trusted CAs or the provided chain, or return NULL.
2691 *
2692 * Searches in trusted CAs first, and return the first suitable parent found
2693 * (see find_parent_in() for definition of suitable).
Manuel Pégourié-Gonnard98a67782017-08-17 10:52:20 +02002694 *
2695 * Arguments:
Manuel Pégourié-Gonnarde57d7432018-03-07 10:00:57 +01002696 * - [in] child: certificate for which we're looking for a parent, followed
2697 * by a chain of possible intermediates
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +02002698 * - [in] trust_ca: list of locally trusted certificates
2699 * - [out] parent: parent found (or NULL)
2700 * - [out] parent_is_trusted: 1 if returned `parent` is trusted, or 0
2701 * - [out] signature_is_good: 1 if child signature by parent is valid, or 0
2702 * - [in] path_cnt: number of links in the chain so far (EE -> ... -> child)
2703 * - [in] self_cnt: number of self-signed certs in the chain so far
Manuel Pégourié-Gonnarde57d7432018-03-07 10:00:57 +01002704 * (will always be no greater than path_cnt)
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +02002705 * - [in-out] rs_ctx: context for restarting operations
Manuel Pégourié-Gonnarde57d7432018-03-07 10:00:57 +01002706 *
2707 * Return value:
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +02002708 * - 0 on success
2709 * - MBEDTLS_ERR_ECP_IN_PROGRESS otherwise
Manuel Pégourié-Gonnard63686122017-07-04 01:01:39 +02002710 */
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002711static int x509_crt_find_parent(
2712 mbedtls_x509_crt *child,
2713 mbedtls_x509_crt *trust_ca,
2714 mbedtls_x509_crt **parent,
2715 int *parent_is_trusted,
2716 int *signature_is_good,
Manuel Pégourié-Gonnardbb216bd2017-08-28 13:25:55 +02002717 unsigned path_cnt,
2718 unsigned self_cnt,
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002719 mbedtls_x509_crt_restart_ctx *rs_ctx )
Manuel Pégourié-Gonnard63686122017-07-04 01:01:39 +02002720{
Janos Follath865b3eb2019-12-16 11:46:15 +00002721 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard18547b52017-08-14 16:11:43 +02002722 mbedtls_x509_crt *search_list;
Manuel Pégourié-Gonnard63686122017-07-04 01:01:39 +02002723
Manuel Pégourié-Gonnard63686122017-07-04 01:01:39 +02002724 *parent_is_trusted = 1;
Manuel Pégourié-Gonnard18547b52017-08-14 16:11:43 +02002725
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002726#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard3627a8b2017-08-23 11:20:48 +02002727 /* restore then clear saved state if we have some stored */
2728 if( rs_ctx != NULL && rs_ctx->parent_is_trusted != -1 )
2729 {
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002730 *parent_is_trusted = rs_ctx->parent_is_trusted;
Manuel Pégourié-Gonnard3627a8b2017-08-23 11:20:48 +02002731 rs_ctx->parent_is_trusted = -1;
2732 }
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002733#endif
2734
Manuel Pégourié-Gonnard18547b52017-08-14 16:11:43 +02002735 while( 1 ) {
2736 search_list = *parent_is_trusted ? trust_ca : child->next;
2737
2738 ret = x509_crt_find_parent_in( child, search_list,
2739 parent, signature_is_good,
2740 *parent_is_trusted,
2741 path_cnt, self_cnt, rs_ctx );
Manuel Pégourié-Gonnard63686122017-07-04 01:01:39 +02002742
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002743#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002744 if( rs_ctx != NULL && ret == MBEDTLS_ERR_ECP_IN_PROGRESS )
2745 {
2746 /* save state */
2747 rs_ctx->parent_is_trusted = *parent_is_trusted;
Manuel Pégourié-Gonnard18547b52017-08-14 16:11:43 +02002748 return( ret );
2749 }
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002750#else
2751 (void) ret;
2752#endif
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002753
Manuel Pégourié-Gonnard18547b52017-08-14 16:11:43 +02002754 /* stop here if found or already in second iteration */
2755 if( *parent != NULL || *parent_is_trusted == 0 )
2756 break;
Manuel Pégourié-Gonnard63686122017-07-04 01:01:39 +02002757
Manuel Pégourié-Gonnard18547b52017-08-14 16:11:43 +02002758 /* prepare second iteration */
2759 *parent_is_trusted = 0;
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002760 }
Manuel Pégourié-Gonnard18547b52017-08-14 16:11:43 +02002761
2762 /* extra precaution against mistakes in the caller */
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05002763 if( *parent == NULL )
Manuel Pégourié-Gonnard18547b52017-08-14 16:11:43 +02002764 {
Manuel Pégourié-Gonnarda5a3e402018-10-16 11:27:23 +02002765 *parent_is_trusted = 0;
2766 *signature_is_good = 0;
Manuel Pégourié-Gonnard18547b52017-08-14 16:11:43 +02002767 }
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002768
2769 return( 0 );
Manuel Pégourié-Gonnard63686122017-07-04 01:01:39 +02002770}
2771
2772/*
Manuel Pégourié-Gonnard27e94792017-07-04 00:49:31 +02002773 * Check if an end-entity certificate is locally trusted
2774 *
2775 * Currently we require such certificates to be self-signed (actually only
2776 * check for self-issued as self-signatures are not checked)
2777 */
2778static int x509_crt_check_ee_locally_trusted(
2779 mbedtls_x509_crt *crt,
2780 mbedtls_x509_crt *trust_ca )
2781{
2782 mbedtls_x509_crt *cur;
2783
2784 /* must be self-issued */
2785 if( x509_name_cmp( &crt->issuer, &crt->subject ) != 0 )
2786 return( -1 );
2787
2788 /* look for an exact match with trusted cert */
2789 for( cur = trust_ca; cur != NULL; cur = cur->next )
2790 {
2791 if( crt->raw.len == cur->raw.len &&
2792 memcmp( crt->raw.p, cur->raw.p, crt->raw.len ) == 0 )
2793 {
2794 return( 0 );
2795 }
2796 }
2797
2798 /* too bad */
2799 return( -1 );
2800}
2801
2802/*
Manuel Pégourié-Gonnardf86f4912017-07-05 16:43:44 +02002803 * Build and verify a certificate chain
Manuel Pégourié-Gonnard35407c72017-06-29 10:45:25 +02002804 *
Manuel Pégourié-Gonnardf86f4912017-07-05 16:43:44 +02002805 * Given a peer-provided list of certificates EE, C1, ..., Cn and
2806 * a list of trusted certs R1, ... Rp, try to build and verify a chain
Manuel Pégourié-Gonnard562df402017-08-08 18:09:14 +02002807 * EE, Ci1, ... Ciq [, Rj]
Manuel Pégourié-Gonnardf86f4912017-07-05 16:43:44 +02002808 * such that every cert in the chain is a child of the next one,
2809 * jumping to a trusted root as early as possible.
Manuel Pégourié-Gonnardbdc54402017-07-04 00:33:39 +02002810 *
Manuel Pégourié-Gonnardf86f4912017-07-05 16:43:44 +02002811 * Verify that chain and return it with flags for all issues found.
2812 *
2813 * Special cases:
2814 * - EE == Rj -> return a one-element list containing it
2815 * - EE, Ci1, ..., Ciq cannot be continued with a trusted root
2816 * -> return that chain with NOT_TRUSTED set on Ciq
Manuel Pégourié-Gonnardbdc54402017-07-04 00:33:39 +02002817 *
Manuel Pégourié-Gonnardd19a41d2017-07-14 11:05:59 +02002818 * Tests for (aspects of) this function should include at least:
2819 * - trusted EE
2820 * - EE -> trusted root
Antonin Décimo36e89b52019-01-23 15:24:37 +01002821 * - EE -> intermediate CA -> trusted root
Manuel Pégourié-Gonnardd19a41d2017-07-14 11:05:59 +02002822 * - if relevant: EE untrusted
2823 * - if relevant: EE -> intermediate, untrusted
2824 * with the aspect under test checked at each relevant level (EE, int, root).
2825 * For some aspects longer chains are required, but usually length 2 is
2826 * enough (but length 1 is not in general).
2827 *
Manuel Pégourié-Gonnardbdc54402017-07-04 00:33:39 +02002828 * Arguments:
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002829 * - [in] crt: the cert list EE, C1, ..., Cn
2830 * - [in] trust_ca: the trusted list R1, ..., Rp
2831 * - [in] ca_crl, profile: as in verify_with_profile()
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02002832 * - [out] ver_chain: the built and verified chain
Manuel Pégourié-Gonnarda9688432017-08-23 11:23:59 +02002833 * Only valid when return value is 0, may contain garbage otherwise!
2834 * Restart note: need not be the same when calling again to resume.
Manuel Pégourié-Gonnard98a67782017-08-17 10:52:20 +02002835 * - [in-out] rs_ctx: context for restarting operations
Manuel Pégourié-Gonnardf86f4912017-07-05 16:43:44 +02002836 *
2837 * Return value:
2838 * - non-zero if the chain could not be fully built and examined
2839 * - 0 is the chain was successfully built and examined,
2840 * even if it was found to be invalid
Manuel Pégourié-Gonnard35407c72017-06-29 10:45:25 +02002841 */
Manuel Pégourié-Gonnardbdc54402017-07-04 00:33:39 +02002842static int x509_crt_verify_chain(
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002843 mbedtls_x509_crt *crt,
2844 mbedtls_x509_crt *trust_ca,
2845 mbedtls_x509_crl *ca_crl,
Hanno Becker3116fb32019-03-28 13:34:42 +00002846 mbedtls_x509_crt_ca_cb_t f_ca_cb,
2847 void *p_ca_cb,
Manuel Pégourié-Gonnard95051642015-06-15 10:39:46 +02002848 const mbedtls_x509_crt_profile *profile,
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02002849 mbedtls_x509_crt_verify_chain *ver_chain,
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002850 mbedtls_x509_crt_restart_ctx *rs_ctx )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002851{
Manuel Pégourié-Gonnarda9688432017-08-23 11:23:59 +02002852 /* Don't initialize any of those variables here, so that the compiler can
2853 * catch potential issues with jumping ahead when restarting */
Janos Follath865b3eb2019-12-16 11:46:15 +00002854 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardf86f4912017-07-05 16:43:44 +02002855 uint32_t *flags;
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02002856 mbedtls_x509_crt_verify_chain_item *cur;
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002857 mbedtls_x509_crt *child;
Manuel Pégourié-Gonnard58dcd2d2017-07-03 21:35:04 +02002858 mbedtls_x509_crt *parent;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002859 int parent_is_trusted;
2860 int child_is_trusted;
2861 int signature_is_good;
Manuel Pégourié-Gonnardbb216bd2017-08-28 13:25:55 +02002862 unsigned self_cnt;
Hanno Beckerf53893b2019-03-28 13:45:55 +00002863 mbedtls_x509_crt *cur_trust_ca = NULL;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002864
2865#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
2866 /* resume if we had an operation in progress */
Manuel Pégourié-Gonnarddaf04912017-08-23 12:32:19 +02002867 if( rs_ctx != NULL && rs_ctx->in_progress == x509_crt_rs_find_parent )
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002868 {
Manuel Pégourié-Gonnard3627a8b2017-08-23 11:20:48 +02002869 /* restore saved state */
Manuel Pégourié-Gonnarda9688432017-08-23 11:23:59 +02002870 *ver_chain = rs_ctx->ver_chain; /* struct copy */
Manuel Pégourié-Gonnarddaf04912017-08-23 12:32:19 +02002871 self_cnt = rs_ctx->self_cnt;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002872
Manuel Pégourié-Gonnarddaf04912017-08-23 12:32:19 +02002873 /* restore derived state */
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002874 cur = &ver_chain->items[ver_chain->len - 1];
Manuel Pégourié-Gonnarddaf04912017-08-23 12:32:19 +02002875 child = cur->crt;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002876 flags = &cur->flags;
2877
2878 goto find_parent;
2879 }
2880#endif /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */
Manuel Pégourié-Gonnard8f8c2822017-07-03 21:25:10 +02002881
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002882 child = crt;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002883 self_cnt = 0;
2884 parent_is_trusted = 0;
2885 child_is_trusted = 0;
Manuel Pégourié-Gonnardf86f4912017-07-05 16:43:44 +02002886
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002887 while( 1 ) {
2888 /* Add certificate to the verification chain */
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02002889 cur = &ver_chain->items[ver_chain->len];
2890 cur->crt = child;
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +02002891 cur->flags = 0;
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02002892 ver_chain->len++;
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +02002893 flags = &cur->flags;
Manuel Pégourié-Gonnard66fac752017-07-03 21:39:21 +02002894
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002895 /* Check time-validity (all certificates) */
2896 if( mbedtls_x509_time_is_past( &child->valid_to ) )
2897 *flags |= MBEDTLS_X509_BADCERT_EXPIRED;
Manuel Pégourié-Gonnard66fac752017-07-03 21:39:21 +02002898
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002899 if( mbedtls_x509_time_is_future( &child->valid_from ) )
2900 *flags |= MBEDTLS_X509_BADCERT_FUTURE;
Manuel Pégourié-Gonnardcb396102017-07-04 00:00:24 +02002901
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002902 /* Stop here for trusted roots (but not for trusted EE certs) */
2903 if( child_is_trusted )
2904 return( 0 );
Manuel Pégourié-Gonnard66fac752017-07-03 21:39:21 +02002905
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002906 /* Check signature algorithm: MD & PK algs */
2907 if( x509_profile_check_md_alg( profile, child->sig_md ) != 0 )
2908 *flags |= MBEDTLS_X509_BADCERT_BAD_MD;
Manuel Pégourié-Gonnard66fac752017-07-03 21:39:21 +02002909
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002910 if( x509_profile_check_pk_alg( profile, child->sig_pk ) != 0 )
2911 *flags |= MBEDTLS_X509_BADCERT_BAD_PK;
Manuel Pégourié-Gonnard27e94792017-07-04 00:49:31 +02002912
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002913 /* Special case: EE certs that are locally trusted */
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02002914 if( ver_chain->len == 1 &&
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002915 x509_crt_check_ee_locally_trusted( child, trust_ca ) == 0 )
2916 {
2917 return( 0 );
2918 }
Manuel Pégourié-Gonnard8f8c2822017-07-03 21:25:10 +02002919
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002920#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
2921find_parent:
2922#endif
Hanno Beckerf53893b2019-03-28 13:45:55 +00002923
2924 /* Obtain list of potential trusted signers from CA callback,
2925 * or use statically provided list. */
2926#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
2927 if( f_ca_cb != NULL )
2928 {
2929 mbedtls_x509_crt_free( ver_chain->trust_ca_cb_result );
2930 mbedtls_free( ver_chain->trust_ca_cb_result );
2931 ver_chain->trust_ca_cb_result = NULL;
2932
2933 ret = f_ca_cb( p_ca_cb, child, &ver_chain->trust_ca_cb_result );
2934 if( ret != 0 )
2935 return( MBEDTLS_ERR_X509_FATAL_ERROR );
2936
2937 cur_trust_ca = ver_chain->trust_ca_cb_result;
2938 }
2939 else
2940#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
2941 {
2942 ((void) f_ca_cb);
2943 ((void) p_ca_cb);
2944 cur_trust_ca = trust_ca;
2945 }
2946
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002947 /* Look for a parent in trusted CAs or up the chain */
Hanno Beckerf53893b2019-03-28 13:45:55 +00002948 ret = x509_crt_find_parent( child, cur_trust_ca, &parent,
Manuel Pégourié-Gonnardbe4ff422017-07-14 12:04:14 +02002949 &parent_is_trusted, &signature_is_good,
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02002950 ver_chain->len - 1, self_cnt, rs_ctx );
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002951
2952#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002953 if( rs_ctx != NULL && ret == MBEDTLS_ERR_ECP_IN_PROGRESS )
2954 {
2955 /* save state */
Manuel Pégourié-Gonnarddaf04912017-08-23 12:32:19 +02002956 rs_ctx->in_progress = x509_crt_rs_find_parent;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002957 rs_ctx->self_cnt = self_cnt;
Manuel Pégourié-Gonnarda9688432017-08-23 11:23:59 +02002958 rs_ctx->ver_chain = *ver_chain; /* struct copy */
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002959
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002960 return( ret );
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002961 }
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002962#else
2963 (void) ret;
2964#endif
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002965
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002966 /* No parent? We're done here */
2967 if( parent == NULL )
2968 {
2969 *flags |= MBEDTLS_X509_BADCERT_NOT_TRUSTED;
2970 return( 0 );
2971 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002972
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002973 /* Count intermediate self-issued (not necessarily self-signed) certs.
2974 * These can occur with some strategies for key rollover, see [SIRO],
2975 * and should be excluded from max_pathlen checks. */
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02002976 if( ver_chain->len != 1 &&
Manuel Pégourié-Gonnard24611f92017-08-09 10:28:07 +02002977 x509_name_cmp( &child->issuer, &child->subject ) == 0 )
2978 {
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002979 self_cnt++;
Manuel Pégourié-Gonnard24611f92017-08-09 10:28:07 +02002980 }
Manuel Pégourié-Gonnardfd6c85c2014-11-20 16:34:20 +01002981
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002982 /* path_cnt is 0 for the first intermediate CA,
2983 * and if parent is trusted it's not an intermediate CA */
2984 if( ! parent_is_trusted &&
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02002985 ver_chain->len > MBEDTLS_X509_MAX_INTERMEDIATE_CA )
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002986 {
2987 /* return immediately to avoid overflow the chain array */
2988 return( MBEDTLS_ERR_X509_FATAL_ERROR );
2989 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002990
Manuel Pégourié-Gonnard98a67782017-08-17 10:52:20 +02002991 /* signature was checked while searching parent */
Manuel Pégourié-Gonnardbe4ff422017-07-14 12:04:14 +02002992 if( ! signature_is_good )
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002993 *flags |= MBEDTLS_X509_BADCERT_NOT_TRUSTED;
2994
2995 /* check size of signing key */
Manuel Pégourié-Gonnard3f816912017-10-26 10:24:16 +02002996 if( x509_profile_check_key( profile, &parent->pk ) != 0 )
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002997 *flags |= MBEDTLS_X509_BADCERT_BAD_KEY;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002998
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002999#if defined(MBEDTLS_X509_CRL_PARSE_C)
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02003000 /* Check trusted CA's CRL for the given crt */
Manuel Pégourié-Gonnard562df402017-08-08 18:09:14 +02003001 *flags |= x509_crt_verifycrl( child, parent, ca_crl, profile );
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02003002#else
3003 (void) ca_crl;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003004#endif
3005
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02003006 /* prepare for next iteration */
3007 child = parent;
3008 parent = NULL;
3009 child_is_trusted = parent_is_trusted;
Manuel Pégourié-Gonnardbe4ff422017-07-14 12:04:14 +02003010 signature_is_good = 0;
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02003011 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003012}
3013
3014/*
Manuel Pégourié-Gonnarda468eb12017-07-04 01:31:59 +02003015 * Check for CN match
3016 */
3017static int x509_crt_check_cn( const mbedtls_x509_buf *name,
3018 const char *cn, size_t cn_len )
3019{
3020 /* try exact match */
3021 if( name->len == cn_len &&
3022 x509_memcasecmp( cn, name->p, cn_len ) == 0 )
3023 {
3024 return( 0 );
3025 }
3026
3027 /* try wildcard match */
Manuel Pégourié-Gonnard900fba62017-10-18 14:28:11 +02003028 if( x509_check_wildcard( cn, name ) == 0 )
Manuel Pégourié-Gonnarda468eb12017-07-04 01:31:59 +02003029 {
3030 return( 0 );
3031 }
3032
3033 return( -1 );
3034}
3035
3036/*
Manuel Pégourié-Gonnardf3e4bd82020-07-21 13:22:41 +02003037 * Check for SAN match, see RFC 5280 Section 4.2.1.6
3038 */
3039static int x509_crt_check_san( const mbedtls_x509_buf *name,
3040 const char *cn, size_t cn_len )
3041{
3042 const unsigned char san_type = (unsigned char) name->tag &
3043 MBEDTLS_ASN1_TAG_VALUE_MASK;
3044
3045 /* dNSName */
3046 if( san_type == MBEDTLS_X509_SAN_DNS_NAME )
3047 return( x509_crt_check_cn( name, cn, cn_len ) );
3048
3049 /* (We may handle other types here later.) */
3050
3051 /* Unrecognized type */
3052 return( -1 );
3053}
3054
3055/*
Manuel Pégourié-Gonnard1300e992017-07-04 01:13:44 +02003056 * Verify the requested CN - only call this if cn is not NULL!
3057 */
Manuel Pégourié-Gonnarda468eb12017-07-04 01:31:59 +02003058static void x509_crt_verify_name( const mbedtls_x509_crt *crt,
Manuel Pégourié-Gonnard1300e992017-07-04 01:13:44 +02003059 const char *cn,
3060 uint32_t *flags )
3061{
Manuel Pégourié-Gonnarda468eb12017-07-04 01:31:59 +02003062 const mbedtls_x509_name *name;
3063 const mbedtls_x509_sequence *cur;
3064 size_t cn_len = strlen( cn );
Manuel Pégourié-Gonnard1300e992017-07-04 01:13:44 +02003065
3066 if( crt->ext_types & MBEDTLS_X509_EXT_SUBJECT_ALT_NAME )
3067 {
Manuel Pégourié-Gonnarda468eb12017-07-04 01:31:59 +02003068 for( cur = &crt->subject_alt_names; cur != NULL; cur = cur->next )
Manuel Pégourié-Gonnard1300e992017-07-04 01:13:44 +02003069 {
Manuel Pégourié-Gonnardf3e4bd82020-07-21 13:22:41 +02003070 if( x509_crt_check_san( &cur->buf, cn, cn_len ) == 0 )
Manuel Pégourié-Gonnard1300e992017-07-04 01:13:44 +02003071 break;
Manuel Pégourié-Gonnard1300e992017-07-04 01:13:44 +02003072 }
3073
3074 if( cur == NULL )
3075 *flags |= MBEDTLS_X509_BADCERT_CN_MISMATCH;
3076 }
3077 else
3078 {
Manuel Pégourié-Gonnard08eacec2017-10-18 14:20:24 +02003079 for( name = &crt->subject; name != NULL; name = name->next )
Manuel Pégourié-Gonnard1300e992017-07-04 01:13:44 +02003080 {
Manuel Pégourié-Gonnarda468eb12017-07-04 01:31:59 +02003081 if( MBEDTLS_OID_CMP( MBEDTLS_OID_AT_CN, &name->oid ) == 0 &&
3082 x509_crt_check_cn( &name->val, cn, cn_len ) == 0 )
Manuel Pégourié-Gonnard1300e992017-07-04 01:13:44 +02003083 {
Manuel Pégourié-Gonnarda468eb12017-07-04 01:31:59 +02003084 break;
Manuel Pégourié-Gonnard1300e992017-07-04 01:13:44 +02003085 }
Manuel Pégourié-Gonnard1300e992017-07-04 01:13:44 +02003086 }
3087
3088 if( name == NULL )
3089 *flags |= MBEDTLS_X509_BADCERT_CN_MISMATCH;
3090 }
3091}
3092
3093/*
Manuel Pégourié-Gonnarda707e1d2017-07-05 17:18:42 +02003094 * Merge the flags for all certs in the chain, after calling callback
3095 */
3096static int x509_crt_merge_flags_with_cb(
3097 uint32_t *flags,
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02003098 const mbedtls_x509_crt_verify_chain *ver_chain,
Manuel Pégourié-Gonnarda707e1d2017-07-05 17:18:42 +02003099 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
3100 void *p_vrfy )
3101{
Janos Follath865b3eb2019-12-16 11:46:15 +00003102 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardbb216bd2017-08-28 13:25:55 +02003103 unsigned i;
Manuel Pégourié-Gonnarda707e1d2017-07-05 17:18:42 +02003104 uint32_t cur_flags;
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02003105 const mbedtls_x509_crt_verify_chain_item *cur;
Manuel Pégourié-Gonnarda707e1d2017-07-05 17:18:42 +02003106
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02003107 for( i = ver_chain->len; i != 0; --i )
Manuel Pégourié-Gonnarda707e1d2017-07-05 17:18:42 +02003108 {
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02003109 cur = &ver_chain->items[i-1];
3110 cur_flags = cur->flags;
Manuel Pégourié-Gonnarda707e1d2017-07-05 17:18:42 +02003111
3112 if( NULL != f_vrfy )
Manuel Pégourié-Gonnardbb216bd2017-08-28 13:25:55 +02003113 if( ( ret = f_vrfy( p_vrfy, cur->crt, (int) i-1, &cur_flags ) ) != 0 )
Manuel Pégourié-Gonnarda707e1d2017-07-05 17:18:42 +02003114 return( ret );
3115
3116 *flags |= cur_flags;
3117 }
3118
3119 return( 0 );
3120}
3121
3122/*
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +02003123 * Verify the certificate validity, with profile, restartable version
3124 *
3125 * This function:
3126 * - checks the requested CN (if any)
3127 * - checks the type and size of the EE cert's key,
3128 * as that isn't done as part of chain building/verification currently
3129 * - builds and verifies the chain
3130 * - then calls the callback and merges the flags
Hanno Becker3116fb32019-03-28 13:34:42 +00003131 *
3132 * The parameters pairs `trust_ca`, `ca_crl` and `f_ca_cb`, `p_ca_cb`
3133 * are mutually exclusive: If `f_ca_cb != NULL`, it will be used by the
3134 * verification routine to search for trusted signers, and CRLs will
3135 * be disabled. Otherwise, `trust_ca` will be used as the static list
3136 * of trusted signers, and `ca_crl` will be use as the static list
3137 * of CRLs.
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +02003138 */
Jarno Lamsa2ee67a62019-04-01 14:59:33 +03003139static int x509_crt_verify_restartable_ca_cb( mbedtls_x509_crt *crt,
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +02003140 mbedtls_x509_crt *trust_ca,
3141 mbedtls_x509_crl *ca_crl,
Hanno Becker3116fb32019-03-28 13:34:42 +00003142 mbedtls_x509_crt_ca_cb_t f_ca_cb,
3143 void *p_ca_cb,
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +02003144 const mbedtls_x509_crt_profile *profile,
3145 const char *cn, uint32_t *flags,
3146 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
3147 void *p_vrfy,
3148 mbedtls_x509_crt_restart_ctx *rs_ctx )
3149{
Janos Follath865b3eb2019-12-16 11:46:15 +00003150 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard65eefc82015-10-23 14:08:48 +02003151 mbedtls_pk_type_t pk_type;
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02003152 mbedtls_x509_crt_verify_chain ver_chain;
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +02003153 uint32_t ee_flags;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003154
3155 *flags = 0;
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +02003156 ee_flags = 0;
3157 x509_crt_verify_chain_reset( &ver_chain );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003158
Manuel Pégourié-Gonnardd15795a2017-06-22 12:19:27 +02003159 if( profile == NULL )
3160 {
3161 ret = MBEDTLS_ERR_X509_BAD_INPUT_DATA;
3162 goto exit;
3163 }
3164
Manuel Pégourié-Gonnard1300e992017-07-04 01:13:44 +02003165 /* check name if requested */
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003166 if( cn != NULL )
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +02003167 x509_crt_verify_name( crt, cn, &ee_flags );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003168
Manuel Pégourié-Gonnard65eefc82015-10-23 14:08:48 +02003169 /* Check the type and size of the key */
3170 pk_type = mbedtls_pk_get_type( &crt->pk );
3171
3172 if( x509_profile_check_pk_alg( profile, pk_type ) != 0 )
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +02003173 ee_flags |= MBEDTLS_X509_BADCERT_BAD_PK;
Manuel Pégourié-Gonnard65eefc82015-10-23 14:08:48 +02003174
Manuel Pégourié-Gonnard3f816912017-10-26 10:24:16 +02003175 if( x509_profile_check_key( profile, &crt->pk ) != 0 )
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +02003176 ee_flags |= MBEDTLS_X509_BADCERT_BAD_KEY;
Manuel Pégourié-Gonnard65eefc82015-10-23 14:08:48 +02003177
Manuel Pégourié-Gonnardbdc54402017-07-04 00:33:39 +02003178 /* Check the chain */
Hanno Becker3116fb32019-03-28 13:34:42 +00003179 ret = x509_crt_verify_chain( crt, trust_ca, ca_crl,
3180 f_ca_cb, p_ca_cb, profile,
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02003181 &ver_chain, rs_ctx );
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02003182
Manuel Pégourié-Gonnardc547d1a2017-07-05 13:28:45 +02003183 if( ret != 0 )
3184 goto exit;
3185
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +02003186 /* Merge end-entity flags */
3187 ver_chain.items[0].flags |= ee_flags;
3188
Manuel Pégourié-Gonnarda707e1d2017-07-05 17:18:42 +02003189 /* Build final flags, calling callback on the way if any */
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02003190 ret = x509_crt_merge_flags_with_cb( flags, &ver_chain, f_vrfy, p_vrfy );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003191
Manuel Pégourié-Gonnardd15795a2017-06-22 12:19:27 +02003192exit:
Hanno Beckerf53893b2019-03-28 13:45:55 +00003193
3194#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
3195 mbedtls_x509_crt_free( ver_chain.trust_ca_cb_result );
3196 mbedtls_free( ver_chain.trust_ca_cb_result );
3197 ver_chain.trust_ca_cb_result = NULL;
3198#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
3199
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02003200#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
3201 if( rs_ctx != NULL && ret != MBEDTLS_ERR_ECP_IN_PROGRESS )
3202 mbedtls_x509_crt_restart_free( rs_ctx );
3203#endif
3204
Manuel Pégourié-Gonnard9107b5f2017-07-06 12:16:25 +02003205 /* prevent misuse of the vrfy callback - VERIFY_FAILED would be ignored by
3206 * the SSL module for authmode optional, but non-zero return from the
3207 * callback means a fatal error so it shouldn't be ignored */
Manuel Pégourié-Gonnard31458a12017-06-26 10:11:49 +02003208 if( ret == MBEDTLS_ERR_X509_CERT_VERIFY_FAILED )
3209 ret = MBEDTLS_ERR_X509_FATAL_ERROR;
3210
Manuel Pégourié-Gonnardd15795a2017-06-22 12:19:27 +02003211 if( ret != 0 )
3212 {
3213 *flags = (uint32_t) -1;
3214 return( ret );
3215 }
3216
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003217 if( *flags != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003218 return( MBEDTLS_ERR_X509_CERT_VERIFY_FAILED );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003219
3220 return( 0 );
3221}
3222
Hanno Becker3116fb32019-03-28 13:34:42 +00003223
3224/*
3225 * Verify the certificate validity (default profile, not restartable)
3226 */
3227int mbedtls_x509_crt_verify( mbedtls_x509_crt *crt,
3228 mbedtls_x509_crt *trust_ca,
3229 mbedtls_x509_crl *ca_crl,
3230 const char *cn, uint32_t *flags,
3231 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
3232 void *p_vrfy )
3233{
Jarno Lamsa2ee67a62019-04-01 14:59:33 +03003234 return( x509_crt_verify_restartable_ca_cb( crt, trust_ca, ca_crl,
Hanno Becker3116fb32019-03-28 13:34:42 +00003235 NULL, NULL,
3236 &mbedtls_x509_crt_profile_default,
3237 cn, flags,
3238 f_vrfy, p_vrfy, NULL ) );
3239}
3240
3241/*
3242 * Verify the certificate validity (user-chosen profile, not restartable)
3243 */
3244int mbedtls_x509_crt_verify_with_profile( mbedtls_x509_crt *crt,
3245 mbedtls_x509_crt *trust_ca,
3246 mbedtls_x509_crl *ca_crl,
3247 const mbedtls_x509_crt_profile *profile,
3248 const char *cn, uint32_t *flags,
3249 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
3250 void *p_vrfy )
3251{
Jarno Lamsa2ee67a62019-04-01 14:59:33 +03003252 return( x509_crt_verify_restartable_ca_cb( crt, trust_ca, ca_crl,
Hanno Becker3116fb32019-03-28 13:34:42 +00003253 NULL, NULL,
3254 profile, cn, flags,
3255 f_vrfy, p_vrfy, NULL ) );
3256}
3257
3258#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
3259/*
3260 * Verify the certificate validity (user-chosen profile, CA callback,
3261 * not restartable).
3262 */
Jarno Lamsa31d9db62019-04-01 14:33:49 +03003263int mbedtls_x509_crt_verify_with_ca_cb( mbedtls_x509_crt *crt,
Hanno Becker3116fb32019-03-28 13:34:42 +00003264 mbedtls_x509_crt_ca_cb_t f_ca_cb,
3265 void *p_ca_cb,
3266 const mbedtls_x509_crt_profile *profile,
3267 const char *cn, uint32_t *flags,
3268 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
3269 void *p_vrfy )
3270{
Jarno Lamsa2ee67a62019-04-01 14:59:33 +03003271 return( x509_crt_verify_restartable_ca_cb( crt, NULL, NULL,
Hanno Becker3116fb32019-03-28 13:34:42 +00003272 f_ca_cb, p_ca_cb,
3273 profile, cn, flags,
3274 f_vrfy, p_vrfy, NULL ) );
3275}
3276#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
3277
3278int mbedtls_x509_crt_verify_restartable( mbedtls_x509_crt *crt,
3279 mbedtls_x509_crt *trust_ca,
3280 mbedtls_x509_crl *ca_crl,
3281 const mbedtls_x509_crt_profile *profile,
3282 const char *cn, uint32_t *flags,
3283 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
3284 void *p_vrfy,
3285 mbedtls_x509_crt_restart_ctx *rs_ctx )
3286{
Jarno Lamsa2ee67a62019-04-01 14:59:33 +03003287 return( x509_crt_verify_restartable_ca_cb( crt, trust_ca, ca_crl,
Hanno Becker3116fb32019-03-28 13:34:42 +00003288 NULL, NULL,
3289 profile, cn, flags,
3290 f_vrfy, p_vrfy, rs_ctx ) );
3291}
3292
3293
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003294/*
Paul Bakker369d2eb2013-09-18 11:58:25 +02003295 * Initialize a certificate chain
3296 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003297void mbedtls_x509_crt_init( mbedtls_x509_crt *crt )
Paul Bakker369d2eb2013-09-18 11:58:25 +02003298{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003299 memset( crt, 0, sizeof(mbedtls_x509_crt) );
Paul Bakker369d2eb2013-09-18 11:58:25 +02003300}
3301
3302/*
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003303 * Unallocate all certificate data
3304 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003305void mbedtls_x509_crt_free( mbedtls_x509_crt *crt )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003306{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003307 mbedtls_x509_crt *cert_cur = crt;
3308 mbedtls_x509_crt *cert_prv;
3309 mbedtls_x509_name *name_cur;
3310 mbedtls_x509_name *name_prv;
3311 mbedtls_x509_sequence *seq_cur;
3312 mbedtls_x509_sequence *seq_prv;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003313
3314 if( crt == NULL )
3315 return;
3316
3317 do
3318 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003319 mbedtls_pk_free( &cert_cur->pk );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003320
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003321#if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT)
3322 mbedtls_free( cert_cur->sig_opts );
Manuel Pégourié-Gonnardf75f2f72014-06-05 15:14:28 +02003323#endif
3324
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003325 name_cur = cert_cur->issuer.next;
3326 while( name_cur != NULL )
3327 {
3328 name_prv = name_cur;
3329 name_cur = name_cur->next;
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05003330 mbedtls_platform_zeroize( name_prv, sizeof( mbedtls_x509_name ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003331 mbedtls_free( name_prv );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003332 }
3333
3334 name_cur = cert_cur->subject.next;
3335 while( name_cur != NULL )
3336 {
3337 name_prv = name_cur;
3338 name_cur = name_cur->next;
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05003339 mbedtls_platform_zeroize( name_prv, sizeof( mbedtls_x509_name ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003340 mbedtls_free( name_prv );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003341 }
3342
3343 seq_cur = cert_cur->ext_key_usage.next;
3344 while( seq_cur != NULL )
3345 {
3346 seq_prv = seq_cur;
3347 seq_cur = seq_cur->next;
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05003348 mbedtls_platform_zeroize( seq_prv,
3349 sizeof( mbedtls_x509_sequence ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003350 mbedtls_free( seq_prv );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003351 }
3352
3353 seq_cur = cert_cur->subject_alt_names.next;
3354 while( seq_cur != NULL )
3355 {
3356 seq_prv = seq_cur;
3357 seq_cur = seq_cur->next;
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05003358 mbedtls_platform_zeroize( seq_prv,
3359 sizeof( mbedtls_x509_sequence ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003360 mbedtls_free( seq_prv );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003361 }
3362
Ron Eldor74d9acc2019-03-21 14:00:03 +02003363 seq_cur = cert_cur->certificate_policies.next;
3364 while( seq_cur != NULL )
3365 {
3366 seq_prv = seq_cur;
3367 seq_cur = seq_cur->next;
3368 mbedtls_platform_zeroize( seq_prv,
3369 sizeof( mbedtls_x509_sequence ) );
3370 mbedtls_free( seq_prv );
3371 }
3372
Hanno Becker1a65dcd2019-01-31 08:57:44 +00003373 if( cert_cur->raw.p != NULL && cert_cur->own_buffer )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003374 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05003375 mbedtls_platform_zeroize( cert_cur->raw.p, cert_cur->raw.len );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003376 mbedtls_free( cert_cur->raw.p );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003377 }
3378
3379 cert_cur = cert_cur->next;
3380 }
3381 while( cert_cur != NULL );
3382
3383 cert_cur = crt;
3384 do
3385 {
3386 cert_prv = cert_cur;
3387 cert_cur = cert_cur->next;
3388
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05003389 mbedtls_platform_zeroize( cert_prv, sizeof( mbedtls_x509_crt ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003390 if( cert_prv != crt )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003391 mbedtls_free( cert_prv );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003392 }
3393 while( cert_cur != NULL );
3394}
3395
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +02003396#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
3397/*
3398 * Initialize a restart context
3399 */
3400void mbedtls_x509_crt_restart_init( mbedtls_x509_crt_restart_ctx *ctx )
3401{
Manuel Pégourié-Gonnard15d7df22017-08-17 14:33:31 +02003402 mbedtls_pk_restart_init( &ctx->pk );
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02003403
3404 ctx->parent = NULL;
3405 ctx->fallback_parent = NULL;
Manuel Pégourié-Gonnard78d7e8c2018-07-02 12:33:14 +02003406 ctx->fallback_signature_is_good = 0;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02003407
3408 ctx->parent_is_trusted = -1;
3409
Manuel Pégourié-Gonnarddaf04912017-08-23 12:32:19 +02003410 ctx->in_progress = x509_crt_rs_none;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02003411 ctx->self_cnt = 0;
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +02003412 x509_crt_verify_chain_reset( &ctx->ver_chain );
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +02003413}
3414
3415/*
3416 * Free the components of a restart context
3417 */
3418void mbedtls_x509_crt_restart_free( mbedtls_x509_crt_restart_ctx *ctx )
3419{
3420 if( ctx == NULL )
3421 return;
3422
Manuel Pégourié-Gonnard15d7df22017-08-17 14:33:31 +02003423 mbedtls_pk_restart_free( &ctx->pk );
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02003424 mbedtls_x509_crt_restart_init( ctx );
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +02003425}
3426#endif /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */
3427
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003428#endif /* MBEDTLS_X509_CRT_PARSE_C */