blob: 97a908c134e3ca0844d224ce94e976cf43901806 [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 {
Eduardo Silva32ffb2b2019-04-25 10:43:26 -06001644 /* determinate if the file entry could be a link, using lstat(2)
1645 * 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
1653 /* if the file is a symbolic link, we need to validate the real
1654 * information using stat(2). */
1655 if( S_ISLNK( sb.st_mode ) )
1656 {
1657 /* if stat(2) fails it could be a broken link or a generic
1658 * error, if the link is broken, just report it as a
1659 * certificate that could not be processed, otherwise
1660 * just set a MBEDTLS_ERR_X509_FILE_IO_ERROR. */
1661 if( stat( entry_name, &sb ) == -1 )
1662 {
1663 if( errno == ENOENT )
1664 {
1665 /* Broken link */
1666 ret++;
1667 }
1668 else
1669 {
1670 ret = MBEDTLS_ERR_X509_FILE_IO_ERROR;
1671 goto cleanup;
1672 }
1673 }
1674 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001675 }
1676
1677 if( !S_ISREG( sb.st_mode ) )
1678 continue;
1679
1680 // Ignore parse errors
1681 //
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001682 t_ret = mbedtls_x509_crt_parse_file( chain, entry_name );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001683 if( t_ret < 0 )
1684 ret++;
1685 else
1686 ret += t_ret;
1687 }
Manuel Pégourié-Gonnard5ad68e42013-11-28 17:11:54 +01001688
1689cleanup:
Andres AGf9113192016-09-02 14:06:04 +01001690 closedir( dir );
1691
Ron Eldor63140682017-01-09 19:27:59 +02001692#if defined(MBEDTLS_THREADING_C)
Manuel Pégourié-Gonnard944cfe82015-05-27 20:07:18 +02001693 if( mbedtls_mutex_unlock( &mbedtls_threading_readdir_mutex ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001694 ret = MBEDTLS_ERR_THREADING_MUTEX_ERROR;
Ron Eldor63140682017-01-09 19:27:59 +02001695#endif /* MBEDTLS_THREADING_C */
Manuel Pégourié-Gonnard5ad68e42013-11-28 17:11:54 +01001696
Paul Bakkerbe089b02013-10-14 15:51:50 +02001697#endif /* _WIN32 */
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001698
1699 return( ret );
1700}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001701#endif /* MBEDTLS_FS_IO */
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001702
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001703/*
1704 * OtherName ::= SEQUENCE {
1705 * type-id OBJECT IDENTIFIER,
1706 * value [0] EXPLICIT ANY DEFINED BY type-id }
1707 *
1708 * HardwareModuleName ::= SEQUENCE {
1709 * hwType OBJECT IDENTIFIER,
1710 * hwSerialNum OCTET STRING }
1711 *
1712 * NOTE: we currently only parse and use otherName of type HwModuleName,
1713 * as defined in RFC 4108.
1714 */
1715static int x509_get_other_name( const mbedtls_x509_buf *subject_alt_name,
1716 mbedtls_x509_san_other_name *other_name )
1717{
Janos Follathab23cd12019-05-09 13:53:57 +01001718 int ret = 0;
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001719 size_t len;
1720 unsigned char *p = subject_alt_name->p;
1721 const unsigned char *end = p + subject_alt_name->len;
1722 mbedtls_x509_buf cur_oid;
1723
1724 if( ( subject_alt_name->tag &
1725 ( MBEDTLS_ASN1_TAG_CLASS_MASK | MBEDTLS_ASN1_TAG_VALUE_MASK ) ) !=
1726 ( MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_X509_SAN_OTHER_NAME ) )
1727 {
1728 /*
1729 * The given subject alternative name is not of type "othername".
1730 */
1731 return( MBEDTLS_ERR_X509_BAD_INPUT_DATA );
1732 }
1733
1734 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
1735 MBEDTLS_ASN1_OID ) ) != 0 )
Chris Jones9f7a6932021-04-14 12:12:09 +01001736 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret ) );
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001737
1738 cur_oid.tag = MBEDTLS_ASN1_OID;
1739 cur_oid.p = p;
1740 cur_oid.len = len;
1741
1742 /*
1743 * Only HwModuleName is currently supported.
1744 */
1745 if( MBEDTLS_OID_CMP( MBEDTLS_OID_ON_HW_MODULE_NAME, &cur_oid ) != 0 )
1746 {
1747 return( MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE );
1748 }
1749
Ron Eldor890819a2019-05-13 19:03:04 +03001750 if( p + len >= end )
1751 {
Sébastien Duquette661d7252019-06-23 17:45:26 -04001752 mbedtls_platform_zeroize( other_name, sizeof( *other_name ) );
Chris Jones9f7a6932021-04-14 12:12:09 +01001753 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
1754 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH ) );
Ron Eldor890819a2019-05-13 19:03:04 +03001755 }
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001756 p += len;
1757 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
1758 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_CONTEXT_SPECIFIC ) ) != 0 )
Chris Jones9f7a6932021-04-14 12:12:09 +01001759 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret ) );
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001760
1761 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
1762 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
Chris Jones9f7a6932021-04-14 12:12:09 +01001763 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret ) );
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001764
1765 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len, MBEDTLS_ASN1_OID ) ) != 0 )
Chris Jones9f7a6932021-04-14 12:12:09 +01001766 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret ) );
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001767
1768 other_name->value.hardware_module_name.oid.tag = MBEDTLS_ASN1_OID;
1769 other_name->value.hardware_module_name.oid.p = p;
1770 other_name->value.hardware_module_name.oid.len = len;
1771
Ron Eldor890819a2019-05-13 19:03:04 +03001772 if( p + len >= end )
1773 {
Sébastien Duquette661d7252019-06-23 17:45:26 -04001774 mbedtls_platform_zeroize( other_name, sizeof( *other_name ) );
Chris Jones9f7a6932021-04-14 12:12:09 +01001775 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
1776 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH ) );
Ron Eldor890819a2019-05-13 19:03:04 +03001777 }
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001778 p += len;
1779 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
1780 MBEDTLS_ASN1_OCTET_STRING ) ) != 0 )
Chris Jones9f7a6932021-04-14 12:12:09 +01001781 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret ) );
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001782
1783 other_name->value.hardware_module_name.val.tag = MBEDTLS_ASN1_OCTET_STRING;
1784 other_name->value.hardware_module_name.val.p = p;
1785 other_name->value.hardware_module_name.val.len = len;
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001786 p += len;
1787 if( p != end )
1788 {
Ron Eldor890819a2019-05-13 19:03:04 +03001789 mbedtls_platform_zeroize( other_name,
Sébastien Duquette661d7252019-06-23 17:45:26 -04001790 sizeof( *other_name ) );
Chris Jones9f7a6932021-04-14 12:12:09 +01001791 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
1792 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH ) );
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001793 }
1794 return( 0 );
1795}
1796
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02001797static int x509_info_subject_alt_name( char **buf, size_t *size,
Janos Follath2f0ec1e2019-05-10 11:06:31 +01001798 const mbedtls_x509_sequence
1799 *subject_alt_name,
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001800 const char *prefix )
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02001801{
Janos Follath865b3eb2019-12-16 11:46:15 +00001802 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02001803 size_t n = *size;
1804 char *p = *buf;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001805 const mbedtls_x509_sequence *cur = subject_alt_name;
Ron Eldor890819a2019-05-13 19:03:04 +03001806 mbedtls_x509_subject_alternative_name san;
1807 int parse_ret;
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02001808
1809 while( cur != NULL )
1810 {
Ron Eldor890819a2019-05-13 19:03:04 +03001811 memset( &san, 0, sizeof( san ) );
1812 parse_ret = mbedtls_x509_parse_subject_alt_name( &cur->buf, &san );
1813 if( parse_ret != 0 )
1814 {
1815 if( parse_ret == MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE )
1816 {
1817 ret = mbedtls_snprintf( p, n, "\n%s <unsupported>", prefix );
1818 MBEDTLS_X509_SAFE_SNPRINTF;
1819 }
1820 else
1821 {
1822 ret = mbedtls_snprintf( p, n, "\n%s <malformed>", prefix );
1823 MBEDTLS_X509_SAFE_SNPRINTF;
1824 }
1825 cur = cur->next;
1826 continue;
1827 }
1828
1829 switch( san.type )
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02001830 {
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001831 /*
1832 * otherName
1833 */
Ron Eldora2913912019-05-16 16:17:38 +03001834 case MBEDTLS_X509_SAN_OTHER_NAME:
Ron Eldor890819a2019-05-13 19:03:04 +03001835 {
1836 mbedtls_x509_san_other_name *other_name = &san.san.other_name;
1837
1838 ret = mbedtls_snprintf( p, n, "\n%s otherName :", prefix );
1839 MBEDTLS_X509_SAFE_SNPRINTF;
1840
1841 if( MBEDTLS_OID_CMP( MBEDTLS_OID_ON_HW_MODULE_NAME,
1842 &other_name->value.hardware_module_name.oid ) != 0 )
Janos Follath22f605f2019-05-10 10:37:17 +01001843 {
Ron Eldor890819a2019-05-13 19:03:04 +03001844 ret = mbedtls_snprintf( p, n, "\n%s hardware module name :", prefix );
1845 MBEDTLS_X509_SAFE_SNPRINTF;
1846 ret = mbedtls_snprintf( p, n, "\n%s hardware type : ", prefix );
Janos Follath2f0ec1e2019-05-10 11:06:31 +01001847 MBEDTLS_X509_SAFE_SNPRINTF;
1848
Ron Eldor890819a2019-05-13 19:03:04 +03001849 ret = mbedtls_oid_get_numeric_string( p, n, &other_name->value.hardware_module_name.oid );
1850 MBEDTLS_X509_SAFE_SNPRINTF;
Janos Follath2f0ec1e2019-05-10 11:06:31 +01001851
Ron Eldor890819a2019-05-13 19:03:04 +03001852 ret = mbedtls_snprintf( p, n, "\n%s hardware serial number : ", prefix );
1853 MBEDTLS_X509_SAFE_SNPRINTF;
1854
1855 if( other_name->value.hardware_module_name.val.len >= n )
1856 {
1857 *p = '\0';
1858 return( MBEDTLS_ERR_X509_BUFFER_TOO_SMALL );
Janos Follath22f605f2019-05-10 10:37:17 +01001859 }
1860
Ron Eldora2913912019-05-16 16:17:38 +03001861 memcpy( p, other_name->value.hardware_module_name.val.p,
1862 other_name->value.hardware_module_name.val.len );
1863 p += other_name->value.hardware_module_name.val.len;
1864
Ron Eldor890819a2019-05-13 19:03:04 +03001865 n -= other_name->value.hardware_module_name.val.len;
Janos Follath2f0ec1e2019-05-10 11:06:31 +01001866
Ron Eldor890819a2019-05-13 19:03:04 +03001867 }/* MBEDTLS_OID_ON_HW_MODULE_NAME */
1868 }
1869 break;
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001870
1871 /*
1872 * dNSName
1873 */
Ron Eldora2913912019-05-16 16:17:38 +03001874 case MBEDTLS_X509_SAN_DNS_NAME:
Ron Eldor890819a2019-05-13 19:03:04 +03001875 {
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001876 ret = mbedtls_snprintf( p, n, "\n%s dNSName : ", prefix );
1877 MBEDTLS_X509_SAFE_SNPRINTF;
Ron Eldor890819a2019-05-13 19:03:04 +03001878 if( san.san.unstructured_name.len >= n )
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001879 {
1880 *p = '\0';
1881 return( MBEDTLS_ERR_X509_BUFFER_TOO_SMALL );
1882 }
Ron Eldora2913912019-05-16 16:17:38 +03001883
1884 memcpy( p, san.san.unstructured_name.p, san.san.unstructured_name.len );
1885 p += san.san.unstructured_name.len;
Ron Eldor890819a2019-05-13 19:03:04 +03001886 n -= san.san.unstructured_name.len;
Ron Eldor890819a2019-05-13 19:03:04 +03001887 }
1888 break;
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001889
1890 /*
Ron Eldor890819a2019-05-13 19:03:04 +03001891 * Type not supported, skip item.
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001892 */
1893 default:
Janos Follath22f605f2019-05-10 10:37:17 +01001894 ret = mbedtls_snprintf( p, n, "\n%s <unsupported>", prefix );
1895 MBEDTLS_X509_SAFE_SNPRINTF;
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001896 break;
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02001897 }
1898
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02001899 cur = cur->next;
1900 }
1901
1902 *p = '\0';
1903
1904 *size = n;
1905 *buf = p;
1906
1907 return( 0 );
1908}
1909
Ron Eldor890819a2019-05-13 19:03:04 +03001910int mbedtls_x509_parse_subject_alt_name( const mbedtls_x509_buf *san_buf,
1911 mbedtls_x509_subject_alternative_name *san )
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001912{
Janos Follath865b3eb2019-12-16 11:46:15 +00001913 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Ron Eldor890819a2019-05-13 19:03:04 +03001914 switch( san_buf->tag &
1915 ( MBEDTLS_ASN1_TAG_CLASS_MASK |
1916 MBEDTLS_ASN1_TAG_VALUE_MASK ) )
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001917 {
Ron Eldor890819a2019-05-13 19:03:04 +03001918 /*
1919 * otherName
1920 */
1921 case( MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_X509_SAN_OTHER_NAME ):
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001922 {
Ron Eldor890819a2019-05-13 19:03:04 +03001923 mbedtls_x509_san_other_name other_name;
1924
1925 ret = x509_get_other_name( san_buf, &other_name );
1926 if( ret != 0 )
1927 return( ret );
1928
1929 memset( san, 0, sizeof( mbedtls_x509_subject_alternative_name ) );
1930 san->type = MBEDTLS_X509_SAN_OTHER_NAME;
1931 memcpy( &san->san.other_name,
1932 &other_name, sizeof( other_name ) );
1933
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001934 }
Ron Eldor890819a2019-05-13 19:03:04 +03001935 break;
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001936
Ron Eldor890819a2019-05-13 19:03:04 +03001937 /*
1938 * dNSName
1939 */
1940 case( MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_X509_SAN_DNS_NAME ):
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001941 {
Ron Eldor890819a2019-05-13 19:03:04 +03001942 memset( san, 0, sizeof( mbedtls_x509_subject_alternative_name ) );
1943 san->type = MBEDTLS_X509_SAN_DNS_NAME;
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001944
Ron Eldor890819a2019-05-13 19:03:04 +03001945 memcpy( &san->san.unstructured_name,
1946 san_buf, sizeof( *san_buf ) );
Janos Follath6c379b42019-05-10 14:17:16 +01001947
Ron Eldor890819a2019-05-13 19:03:04 +03001948 }
1949 break;
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001950
Ron Eldor890819a2019-05-13 19:03:04 +03001951 /*
1952 * Type not supported
1953 */
1954 default:
1955 return( MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE );
1956 }
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001957 return( 0 );
1958}
1959
Manuel Pégourié-Gonnard0db29b02014-04-01 18:12:24 +02001960#define PRINT_ITEM(i) \
1961 { \
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001962 ret = mbedtls_snprintf( p, n, "%s" i, sep ); \
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001963 MBEDTLS_X509_SAFE_SNPRINTF; \
Manuel Pégourié-Gonnard0db29b02014-04-01 18:12:24 +02001964 sep = ", "; \
1965 }
1966
1967#define CERT_TYPE(type,name) \
Hanno Becker1eeca412018-10-15 12:01:35 +01001968 if( ns_cert_type & (type) ) \
Manuel Pégourié-Gonnard0db29b02014-04-01 18:12:24 +02001969 PRINT_ITEM( name );
1970
Manuel Pégourié-Gonnard919f8f52014-04-01 13:01:11 +02001971static int x509_info_cert_type( char **buf, size_t *size,
1972 unsigned char ns_cert_type )
1973{
Janos Follath865b3eb2019-12-16 11:46:15 +00001974 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard919f8f52014-04-01 13:01:11 +02001975 size_t n = *size;
1976 char *p = *buf;
Manuel Pégourié-Gonnard7b30cfc2014-04-01 18:00:07 +02001977 const char *sep = "";
Manuel Pégourié-Gonnard919f8f52014-04-01 13:01:11 +02001978
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001979 CERT_TYPE( MBEDTLS_X509_NS_CERT_TYPE_SSL_CLIENT, "SSL Client" );
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01001980 CERT_TYPE( MBEDTLS_X509_NS_CERT_TYPE_SSL_SERVER, "SSL Server" );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001981 CERT_TYPE( MBEDTLS_X509_NS_CERT_TYPE_EMAIL, "Email" );
1982 CERT_TYPE( MBEDTLS_X509_NS_CERT_TYPE_OBJECT_SIGNING, "Object Signing" );
1983 CERT_TYPE( MBEDTLS_X509_NS_CERT_TYPE_RESERVED, "Reserved" );
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01001984 CERT_TYPE( MBEDTLS_X509_NS_CERT_TYPE_SSL_CA, "SSL CA" );
1985 CERT_TYPE( MBEDTLS_X509_NS_CERT_TYPE_EMAIL_CA, "Email CA" );
1986 CERT_TYPE( MBEDTLS_X509_NS_CERT_TYPE_OBJECT_SIGNING_CA, "Object Signing CA" );
Manuel Pégourié-Gonnard919f8f52014-04-01 13:01:11 +02001987
1988 *size = n;
1989 *buf = p;
1990
1991 return( 0 );
1992}
1993
Manuel Pégourié-Gonnard0db29b02014-04-01 18:12:24 +02001994#define KEY_USAGE(code,name) \
Hanno Becker1eeca412018-10-15 12:01:35 +01001995 if( key_usage & (code) ) \
Manuel Pégourié-Gonnard0db29b02014-04-01 18:12:24 +02001996 PRINT_ITEM( name );
1997
Manuel Pégourié-Gonnard65c2ddc2014-04-01 14:12:11 +02001998static int x509_info_key_usage( char **buf, size_t *size,
Manuel Pégourié-Gonnard9a702252015-06-23 10:14:36 +02001999 unsigned int key_usage )
Manuel Pégourié-Gonnard65c2ddc2014-04-01 14:12:11 +02002000{
Janos Follath865b3eb2019-12-16 11:46:15 +00002001 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard65c2ddc2014-04-01 14:12:11 +02002002 size_t n = *size;
2003 char *p = *buf;
Manuel Pégourié-Gonnard7b30cfc2014-04-01 18:00:07 +02002004 const char *sep = "";
Manuel Pégourié-Gonnard65c2ddc2014-04-01 14:12:11 +02002005
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002006 KEY_USAGE( MBEDTLS_X509_KU_DIGITAL_SIGNATURE, "Digital Signature" );
2007 KEY_USAGE( MBEDTLS_X509_KU_NON_REPUDIATION, "Non Repudiation" );
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01002008 KEY_USAGE( MBEDTLS_X509_KU_KEY_ENCIPHERMENT, "Key Encipherment" );
2009 KEY_USAGE( MBEDTLS_X509_KU_DATA_ENCIPHERMENT, "Data Encipherment" );
2010 KEY_USAGE( MBEDTLS_X509_KU_KEY_AGREEMENT, "Key Agreement" );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002011 KEY_USAGE( MBEDTLS_X509_KU_KEY_CERT_SIGN, "Key Cert Sign" );
2012 KEY_USAGE( MBEDTLS_X509_KU_CRL_SIGN, "CRL Sign" );
Manuel Pégourié-Gonnard9a702252015-06-23 10:14:36 +02002013 KEY_USAGE( MBEDTLS_X509_KU_ENCIPHER_ONLY, "Encipher Only" );
2014 KEY_USAGE( MBEDTLS_X509_KU_DECIPHER_ONLY, "Decipher Only" );
Manuel Pégourié-Gonnard65c2ddc2014-04-01 14:12:11 +02002015
2016 *size = n;
2017 *buf = p;
2018
2019 return( 0 );
2020}
2021
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02002022static int x509_info_ext_key_usage( char **buf, size_t *size,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002023 const mbedtls_x509_sequence *extended_key_usage )
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02002024{
Janos Follath865b3eb2019-12-16 11:46:15 +00002025 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02002026 const char *desc;
2027 size_t n = *size;
2028 char *p = *buf;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002029 const mbedtls_x509_sequence *cur = extended_key_usage;
Manuel Pégourié-Gonnard7b30cfc2014-04-01 18:00:07 +02002030 const char *sep = "";
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02002031
2032 while( cur != NULL )
2033 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002034 if( mbedtls_oid_get_extended_key_usage( &cur->buf, &desc ) != 0 )
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02002035 desc = "???";
2036
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002037 ret = mbedtls_snprintf( p, n, "%s%s", sep, desc );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002038 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02002039
Manuel Pégourié-Gonnard7b30cfc2014-04-01 18:00:07 +02002040 sep = ", ";
2041
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02002042 cur = cur->next;
2043 }
2044
2045 *size = n;
2046 *buf = p;
2047
2048 return( 0 );
2049}
2050
Ron Eldor74d9acc2019-03-21 14:00:03 +02002051static int x509_info_cert_policies( char **buf, size_t *size,
2052 const mbedtls_x509_sequence *certificate_policies )
2053{
Janos Follath865b3eb2019-12-16 11:46:15 +00002054 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Ron Eldor74d9acc2019-03-21 14:00:03 +02002055 const char *desc;
2056 size_t n = *size;
2057 char *p = *buf;
2058 const mbedtls_x509_sequence *cur = certificate_policies;
2059 const char *sep = "";
2060
2061 while( cur != NULL )
2062 {
2063 if( mbedtls_oid_get_certificate_policies( &cur->buf, &desc ) != 0 )
2064 desc = "???";
2065
2066 ret = mbedtls_snprintf( p, n, "%s%s", sep, desc );
2067 MBEDTLS_X509_SAFE_SNPRINTF;
2068
2069 sep = ", ";
2070
2071 cur = cur->next;
2072 }
2073
2074 *size = n;
2075 *buf = p;
2076
2077 return( 0 );
2078}
2079
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002080/*
2081 * Return an informational string about the certificate.
2082 */
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002083#define BEFORE_COLON 18
2084#define BC "18"
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002085int mbedtls_x509_crt_info( char *buf, size_t size, const char *prefix,
2086 const mbedtls_x509_crt *crt )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002087{
Janos Follath865b3eb2019-12-16 11:46:15 +00002088 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002089 size_t n;
2090 char *p;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002091 char key_size_str[BEFORE_COLON];
2092
2093 p = buf;
2094 n = size;
2095
Janos Follath98e28a72016-05-31 14:03:54 +01002096 if( NULL == crt )
2097 {
2098 ret = mbedtls_snprintf( p, n, "\nCertificate is uninitialised!\n" );
2099 MBEDTLS_X509_SAFE_SNPRINTF;
2100
2101 return( (int) ( size - n ) );
2102 }
2103
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002104 ret = mbedtls_snprintf( p, n, "%scert. version : %d\n",
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002105 prefix, crt->version );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002106 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002107 ret = mbedtls_snprintf( p, n, "%sserial number : ",
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002108 prefix );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002109 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002110
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002111 ret = mbedtls_x509_serial_gets( p, n, &crt->serial );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002112 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002113
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002114 ret = mbedtls_snprintf( p, n, "\n%sissuer name : ", prefix );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002115 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002116 ret = mbedtls_x509_dn_gets( p, n, &crt->issuer );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002117 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002118
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002119 ret = mbedtls_snprintf( p, n, "\n%ssubject name : ", prefix );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002120 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002121 ret = mbedtls_x509_dn_gets( p, n, &crt->subject );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002122 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002123
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002124 ret = mbedtls_snprintf( p, n, "\n%sissued on : " \
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002125 "%04d-%02d-%02d %02d:%02d:%02d", prefix,
2126 crt->valid_from.year, crt->valid_from.mon,
2127 crt->valid_from.day, crt->valid_from.hour,
2128 crt->valid_from.min, crt->valid_from.sec );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002129 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002130
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002131 ret = mbedtls_snprintf( p, n, "\n%sexpires on : " \
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002132 "%04d-%02d-%02d %02d:%02d:%02d", prefix,
2133 crt->valid_to.year, crt->valid_to.mon,
2134 crt->valid_to.day, crt->valid_to.hour,
2135 crt->valid_to.min, crt->valid_to.sec );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002136 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002137
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002138 ret = mbedtls_snprintf( p, n, "\n%ssigned using : ", prefix );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002139 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002140
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002141 ret = mbedtls_x509_sig_alg_gets( p, n, &crt->sig_oid, crt->sig_pk,
Manuel Pégourié-Gonnardbf696d02014-06-05 17:07:30 +02002142 crt->sig_md, crt->sig_opts );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002143 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002144
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002145 /* Key size */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002146 if( ( ret = mbedtls_x509_key_size_helper( key_size_str, BEFORE_COLON,
2147 mbedtls_pk_get_name( &crt->pk ) ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002148 {
2149 return( ret );
2150 }
2151
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002152 ret = mbedtls_snprintf( p, n, "\n%s%-" BC "s: %d bits", prefix, key_size_str,
Manuel Pégourié-Gonnard097c7bb2015-06-18 16:43:38 +02002153 (int) mbedtls_pk_get_bitlen( &crt->pk ) );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002154 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002155
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002156 /*
2157 * Optional extensions
2158 */
2159
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01002160 if( crt->ext_types & MBEDTLS_X509_EXT_BASIC_CONSTRAINTS )
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002161 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002162 ret = mbedtls_snprintf( p, n, "\n%sbasic constraints : CA=%s", prefix,
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002163 crt->ca_istrue ? "true" : "false" );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002164 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002165
2166 if( crt->max_pathlen > 0 )
2167 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002168 ret = mbedtls_snprintf( p, n, ", max_pathlen=%d", crt->max_pathlen - 1 );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002169 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002170 }
2171 }
2172
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01002173 if( crt->ext_types & MBEDTLS_X509_EXT_SUBJECT_ALT_NAME )
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002174 {
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02002175 ret = mbedtls_snprintf( p, n, "\n%ssubject alt name :", prefix );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002176 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02002177
2178 if( ( ret = x509_info_subject_alt_name( &p, &n,
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02002179 &crt->subject_alt_names,
Ron Eldor6aeae9e2019-05-20 12:00:36 +03002180 prefix ) ) != 0 )
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02002181 return( ret );
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002182 }
2183
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002184 if( crt->ext_types & MBEDTLS_X509_EXT_NS_CERT_TYPE )
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002185 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002186 ret = mbedtls_snprintf( p, n, "\n%scert. type : ", prefix );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002187 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnard919f8f52014-04-01 13:01:11 +02002188
2189 if( ( ret = x509_info_cert_type( &p, &n, crt->ns_cert_type ) ) != 0 )
2190 return( ret );
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002191 }
2192
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002193 if( crt->ext_types & MBEDTLS_X509_EXT_KEY_USAGE )
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002194 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002195 ret = mbedtls_snprintf( p, n, "\n%skey usage : ", prefix );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002196 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnard65c2ddc2014-04-01 14:12:11 +02002197
2198 if( ( ret = x509_info_key_usage( &p, &n, crt->key_usage ) ) != 0 )
2199 return( ret );
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002200 }
2201
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002202 if( crt->ext_types & MBEDTLS_X509_EXT_EXTENDED_KEY_USAGE )
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002203 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002204 ret = mbedtls_snprintf( p, n, "\n%sext key usage : ", prefix );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002205 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02002206
2207 if( ( ret = x509_info_ext_key_usage( &p, &n,
2208 &crt->ext_key_usage ) ) != 0 )
2209 return( ret );
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002210 }
2211
Ron Eldor74d9acc2019-03-21 14:00:03 +02002212 if( crt->ext_types & MBEDTLS_OID_X509_EXT_CERTIFICATE_POLICIES )
2213 {
2214 ret = mbedtls_snprintf( p, n, "\n%scertificate policies : ", prefix );
2215 MBEDTLS_X509_SAFE_SNPRINTF;
2216
2217 if( ( ret = x509_info_cert_policies( &p, &n,
2218 &crt->certificate_policies ) ) != 0 )
2219 return( ret );
2220 }
2221
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002222 ret = mbedtls_snprintf( p, n, "\n" );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002223 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002224
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002225 return( (int) ( size - n ) );
2226}
2227
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01002228struct x509_crt_verify_string {
2229 int code;
2230 const char *string;
2231};
2232
2233static const struct x509_crt_verify_string x509_crt_verify_strings[] = {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01002234 { MBEDTLS_X509_BADCERT_EXPIRED, "The certificate validity has expired" },
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01002235 { MBEDTLS_X509_BADCERT_REVOKED, "The certificate has been revoked (is on a CRL)" },
2236 { MBEDTLS_X509_BADCERT_CN_MISMATCH, "The certificate Common Name (CN) does not match with the expected CN" },
2237 { MBEDTLS_X509_BADCERT_NOT_TRUSTED, "The certificate is not correctly signed by the trusted CA" },
2238 { MBEDTLS_X509_BADCRL_NOT_TRUSTED, "The CRL is not correctly signed by the trusted CA" },
2239 { MBEDTLS_X509_BADCRL_EXPIRED, "The CRL is expired" },
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01002240 { MBEDTLS_X509_BADCERT_MISSING, "Certificate was missing" },
2241 { MBEDTLS_X509_BADCERT_SKIP_VERIFY, "Certificate verification was skipped" },
2242 { MBEDTLS_X509_BADCERT_OTHER, "Other reason (can be used by verify callback)" },
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01002243 { MBEDTLS_X509_BADCERT_FUTURE, "The certificate validity starts in the future" },
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01002244 { MBEDTLS_X509_BADCRL_FUTURE, "The CRL is from the future" },
2245 { MBEDTLS_X509_BADCERT_KEY_USAGE, "Usage does not match the keyUsage extension" },
2246 { MBEDTLS_X509_BADCERT_EXT_KEY_USAGE, "Usage does not match the extendedKeyUsage extension" },
2247 { MBEDTLS_X509_BADCERT_NS_CERT_TYPE, "Usage does not match the nsCertType extension" },
Manuel Pégourié-Gonnard95051642015-06-15 10:39:46 +02002248 { MBEDTLS_X509_BADCERT_BAD_MD, "The certificate is signed with an unacceptable hash." },
2249 { MBEDTLS_X509_BADCERT_BAD_PK, "The certificate is signed with an unacceptable PK alg (eg RSA vs ECDSA)." },
2250 { MBEDTLS_X509_BADCERT_BAD_KEY, "The certificate is signed with an unacceptable key (eg bad curve, RSA too short)." },
2251 { MBEDTLS_X509_BADCRL_BAD_MD, "The CRL is signed with an unacceptable hash." },
2252 { MBEDTLS_X509_BADCRL_BAD_PK, "The CRL is signed with an unacceptable PK alg (eg RSA vs ECDSA)." },
2253 { 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 +01002254 { 0, NULL }
2255};
2256
2257int mbedtls_x509_crt_verify_info( char *buf, size_t size, const char *prefix,
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02002258 uint32_t flags )
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01002259{
Janos Follath865b3eb2019-12-16 11:46:15 +00002260 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01002261 const struct x509_crt_verify_string *cur;
2262 char *p = buf;
2263 size_t n = size;
2264
2265 for( cur = x509_crt_verify_strings; cur->string != NULL ; cur++ )
2266 {
2267 if( ( flags & cur->code ) == 0 )
2268 continue;
2269
2270 ret = mbedtls_snprintf( p, n, "%s%s\n", prefix, cur->string );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002271 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01002272 flags ^= cur->code;
2273 }
2274
2275 if( flags != 0 )
2276 {
2277 ret = mbedtls_snprintf( p, n, "%sUnknown reason "
2278 "(this should not happen)\n", prefix );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002279 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01002280 }
2281
2282 return( (int) ( size - n ) );
2283}
2284
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002285#if defined(MBEDTLS_X509_CHECK_KEY_USAGE)
Manuel Pégourié-Gonnard655a9642015-06-23 10:48:44 +02002286int mbedtls_x509_crt_check_key_usage( const mbedtls_x509_crt *crt,
2287 unsigned int usage )
Manuel Pégourié-Gonnard603116c2014-04-09 09:50:03 +02002288{
Manuel Pégourié-Gonnard655a9642015-06-23 10:48:44 +02002289 unsigned int usage_must, usage_may;
2290 unsigned int may_mask = MBEDTLS_X509_KU_ENCIPHER_ONLY
2291 | MBEDTLS_X509_KU_DECIPHER_ONLY;
2292
2293 if( ( crt->ext_types & MBEDTLS_X509_EXT_KEY_USAGE ) == 0 )
2294 return( 0 );
2295
2296 usage_must = usage & ~may_mask;
2297
2298 if( ( ( crt->key_usage & ~may_mask ) & usage_must ) != usage_must )
2299 return( MBEDTLS_ERR_X509_BAD_INPUT_DATA );
2300
2301 usage_may = usage & may_mask;
2302
2303 if( ( ( crt->key_usage & may_mask ) | usage_may ) != usage_may )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002304 return( MBEDTLS_ERR_X509_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard603116c2014-04-09 09:50:03 +02002305
2306 return( 0 );
2307}
2308#endif
2309
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002310#if defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE)
2311int mbedtls_x509_crt_check_extended_key_usage( const mbedtls_x509_crt *crt,
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02002312 const char *usage_oid,
2313 size_t usage_len )
2314{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002315 const mbedtls_x509_sequence *cur;
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02002316
2317 /* Extension is not mandatory, absent means no restriction */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002318 if( ( crt->ext_types & MBEDTLS_X509_EXT_EXTENDED_KEY_USAGE ) == 0 )
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02002319 return( 0 );
2320
2321 /*
2322 * Look for the requested usage (or wildcard ANY) in our list
2323 */
2324 for( cur = &crt->ext_key_usage; cur != NULL; cur = cur->next )
2325 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002326 const mbedtls_x509_buf *cur_oid = &cur->buf;
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02002327
2328 if( cur_oid->len == usage_len &&
2329 memcmp( cur_oid->p, usage_oid, usage_len ) == 0 )
2330 {
2331 return( 0 );
2332 }
2333
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002334 if( MBEDTLS_OID_CMP( MBEDTLS_OID_ANY_EXTENDED_KEY_USAGE, cur_oid ) == 0 )
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02002335 return( 0 );
2336 }
2337
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002338 return( MBEDTLS_ERR_X509_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02002339}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002340#endif /* MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE */
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02002341
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002342#if defined(MBEDTLS_X509_CRL_PARSE_C)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002343/*
2344 * Return 1 if the certificate is revoked, or 0 otherwise.
2345 */
Manuel Pégourié-Gonnardc730ed32015-06-02 10:38:50 +01002346int mbedtls_x509_crt_is_revoked( const mbedtls_x509_crt *crt, const mbedtls_x509_crl *crl )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002347{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002348 const mbedtls_x509_crl_entry *cur = &crl->entry;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002349
2350 while( cur != NULL && cur->serial.len != 0 )
2351 {
2352 if( crt->serial.len == cur->serial.len &&
2353 memcmp( crt->serial.p, cur->serial.p, crt->serial.len ) == 0 )
2354 {
Raoul Strackxa4e86142020-06-15 17:03:13 +02002355 return( 1 );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002356 }
2357
2358 cur = cur->next;
2359 }
2360
2361 return( 0 );
2362}
2363
2364/*
Manuel Pégourié-Gonnardeeef9472016-02-22 11:36:55 +01002365 * Check that the given certificate is not revoked according to the CRL.
Manuel Pégourié-Gonnard08eacec2017-10-18 14:20:24 +02002366 * Skip validation if no CRL for the given CA is present.
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002367 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002368static int x509_crt_verifycrl( mbedtls_x509_crt *crt, mbedtls_x509_crt *ca,
Manuel Pégourié-Gonnard95051642015-06-15 10:39:46 +02002369 mbedtls_x509_crl *crl_list,
2370 const mbedtls_x509_crt_profile *profile )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002371{
2372 int flags = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002373 unsigned char hash[MBEDTLS_MD_MAX_SIZE];
2374 const mbedtls_md_info_t *md_info;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002375
2376 if( ca == NULL )
2377 return( flags );
2378
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002379 while( crl_list != NULL )
2380 {
2381 if( crl_list->version == 0 ||
Hanno Beckerb75ffb52018-11-02 09:19:54 +00002382 x509_name_cmp( &crl_list->issuer, &ca->subject ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002383 {
2384 crl_list = crl_list->next;
2385 continue;
2386 }
2387
2388 /*
Manuel Pégourié-Gonnard99d4f192014-04-08 15:10:07 +02002389 * Check if the CA is configured to sign CRLs
2390 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002391#if defined(MBEDTLS_X509_CHECK_KEY_USAGE)
Hanno Beckerb75ffb52018-11-02 09:19:54 +00002392 if( mbedtls_x509_crt_check_key_usage( ca,
2393 MBEDTLS_X509_KU_CRL_SIGN ) != 0 )
Manuel Pégourié-Gonnard99d4f192014-04-08 15:10:07 +02002394 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002395 flags |= MBEDTLS_X509_BADCRL_NOT_TRUSTED;
Manuel Pégourié-Gonnard99d4f192014-04-08 15:10:07 +02002396 break;
2397 }
2398#endif
2399
2400 /*
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002401 * Check if CRL is correctly signed by the trusted CA
2402 */
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +02002403 if( x509_profile_check_md_alg( profile, crl_list->sig_md ) != 0 )
2404 flags |= MBEDTLS_X509_BADCRL_BAD_MD;
2405
2406 if( x509_profile_check_pk_alg( profile, crl_list->sig_pk ) != 0 )
2407 flags |= MBEDTLS_X509_BADCRL_BAD_PK;
2408
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002409 md_info = mbedtls_md_info_from_type( crl_list->sig_md );
Manuel Pégourié-Gonnard329e78c2017-06-26 12:22:17 +02002410 if( mbedtls_md( md_info, crl_list->tbs.p, crl_list->tbs.len, hash ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002411 {
Manuel Pégourié-Gonnard329e78c2017-06-26 12:22:17 +02002412 /* Note: this can't happen except after an internal error */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002413 flags |= MBEDTLS_X509_BADCRL_NOT_TRUSTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002414 break;
2415 }
2416
Manuel Pégourié-Gonnard3f816912017-10-26 10:24:16 +02002417 if( x509_profile_check_key( profile, &ca->pk ) != 0 )
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +02002418 flags |= MBEDTLS_X509_BADCERT_BAD_KEY;
Manuel Pégourié-Gonnard95051642015-06-15 10:39:46 +02002419
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002420 if( mbedtls_pk_verify_ext( crl_list->sig_pk, crl_list->sig_opts, &ca->pk,
2421 crl_list->sig_md, hash, mbedtls_md_get_size( md_info ),
Manuel Pégourié-Gonnard53882022014-06-05 17:53:52 +02002422 crl_list->sig.p, crl_list->sig.len ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002423 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002424 flags |= MBEDTLS_X509_BADCRL_NOT_TRUSTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002425 break;
2426 }
2427
2428 /*
2429 * Check for validity of CRL (Do not drop out)
2430 */
Manuel Pégourié-Gonnardc730ed32015-06-02 10:38:50 +01002431 if( mbedtls_x509_time_is_past( &crl_list->next_update ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002432 flags |= MBEDTLS_X509_BADCRL_EXPIRED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002433
Manuel Pégourié-Gonnardc730ed32015-06-02 10:38:50 +01002434 if( mbedtls_x509_time_is_future( &crl_list->this_update ) )
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01002435 flags |= MBEDTLS_X509_BADCRL_FUTURE;
Manuel Pégourié-Gonnard95337652014-03-10 13:15:18 +01002436
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002437 /*
2438 * Check if certificate is revoked
2439 */
Manuel Pégourié-Gonnardc730ed32015-06-02 10:38:50 +01002440 if( mbedtls_x509_crt_is_revoked( crt, crl_list ) )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002441 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002442 flags |= MBEDTLS_X509_BADCERT_REVOKED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002443 break;
2444 }
2445
2446 crl_list = crl_list->next;
2447 }
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +02002448
Paul Bakkerd8bb8262014-06-17 14:06:49 +02002449 return( flags );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002450}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002451#endif /* MBEDTLS_X509_CRL_PARSE_C */
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002452
Manuel Pégourié-Gonnard88421242014-10-17 11:36:18 +02002453/*
Manuel Pégourié-Gonnardf82a4d52017-07-03 19:26:25 +02002454 * Check the signature of a certificate by its parent
2455 */
2456static int x509_crt_check_signature( const mbedtls_x509_crt *child,
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002457 mbedtls_x509_crt *parent,
2458 mbedtls_x509_crt_restart_ctx *rs_ctx )
Manuel Pégourié-Gonnardf82a4d52017-07-03 19:26:25 +02002459{
Manuel Pégourié-Gonnardf82a4d52017-07-03 19:26:25 +02002460 unsigned char hash[MBEDTLS_MD_MAX_SIZE];
Andrzej Kurekd4a65532018-10-31 06:18:39 -04002461 size_t hash_len;
2462#if !defined(MBEDTLS_USE_PSA_CRYPTO)
2463 const mbedtls_md_info_t *md_info;
Manuel Pégourié-Gonnardf82a4d52017-07-03 19:26:25 +02002464 md_info = mbedtls_md_info_from_type( child->sig_md );
Andrzej Kurekd4a65532018-10-31 06:18:39 -04002465 hash_len = mbedtls_md_get_size( md_info );
Andrzej Kurek8b38ff52018-11-20 03:20:09 -05002466
Andrzej Kurekd4a65532018-10-31 06:18:39 -04002467 /* Note: hash errors can happen only after an internal error */
Manuel Pégourié-Gonnardf82a4d52017-07-03 19:26:25 +02002468 if( mbedtls_md( md_info, child->tbs.p, child->tbs.len, hash ) != 0 )
Andrzej Kurekd4a65532018-10-31 06:18:39 -04002469 return( -1 );
2470#else
Jaeden Amero34973232019-02-20 10:32:28 +00002471 psa_hash_operation_t hash_operation = PSA_HASH_OPERATION_INIT;
Andrzej Kurekd4a65532018-10-31 06:18:39 -04002472 psa_algorithm_t hash_alg = mbedtls_psa_translate_md( child->sig_md );
2473
2474 if( psa_hash_setup( &hash_operation, hash_alg ) != PSA_SUCCESS )
2475 return( -1 );
2476
2477 if( psa_hash_update( &hash_operation, child->tbs.p, child->tbs.len )
2478 != PSA_SUCCESS )
Manuel Pégourié-Gonnardf82a4d52017-07-03 19:26:25 +02002479 {
Manuel Pégourié-Gonnardf82a4d52017-07-03 19:26:25 +02002480 return( -1 );
2481 }
2482
Andrzej Kurekd4a65532018-10-31 06:18:39 -04002483 if( psa_hash_finish( &hash_operation, hash, sizeof( hash ), &hash_len )
2484 != PSA_SUCCESS )
2485 {
Andrzej Kurekd4a65532018-10-31 06:18:39 -04002486 return( -1 );
2487 }
2488#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002489 /* Skip expensive computation on obvious mismatch */
2490 if( ! mbedtls_pk_can_do( &parent->pk, child->sig_pk ) )
Manuel Pégourié-Gonnardf82a4d52017-07-03 19:26:25 +02002491 return( -1 );
Manuel Pégourié-Gonnardf82a4d52017-07-03 19:26:25 +02002492
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002493#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002494 if( rs_ctx != NULL && child->sig_pk == MBEDTLS_PK_ECDSA )
2495 {
2496 return( mbedtls_pk_verify_restartable( &parent->pk,
Andrzej Kurekd4a65532018-10-31 06:18:39 -04002497 child->sig_md, hash, hash_len,
Manuel Pégourié-Gonnard15d7df22017-08-17 14:33:31 +02002498 child->sig.p, child->sig.len, &rs_ctx->pk ) );
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002499 }
2500#else
2501 (void) rs_ctx;
2502#endif
2503
2504 return( mbedtls_pk_verify_ext( child->sig_pk, child->sig_opts, &parent->pk,
Andrzej Kurekd4a65532018-10-31 06:18:39 -04002505 child->sig_md, hash, hash_len,
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002506 child->sig.p, child->sig.len ) );
Manuel Pégourié-Gonnardf82a4d52017-07-03 19:26:25 +02002507}
2508
2509/*
Manuel Pégourié-Gonnard312010e2014-04-09 14:30:11 +02002510 * Check if 'parent' is a suitable parent (signing CA) for 'child'.
2511 * Return 0 if yes, -1 if not.
Manuel Pégourié-Gonnardd249b7a2014-06-24 11:49:16 +02002512 *
2513 * top means parent is a locally-trusted certificate
Manuel Pégourié-Gonnard3fed0b32014-04-08 13:18:01 +02002514 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002515static int x509_crt_check_parent( const mbedtls_x509_crt *child,
2516 const mbedtls_x509_crt *parent,
Manuel Pégourié-Gonnard27e94792017-07-04 00:49:31 +02002517 int top )
Manuel Pégourié-Gonnard3fed0b32014-04-08 13:18:01 +02002518{
Manuel Pégourié-Gonnardd249b7a2014-06-24 11:49:16 +02002519 int need_ca_bit;
2520
Manuel Pégourié-Gonnardc4eff162014-06-19 12:18:08 +02002521 /* Parent must be the issuer */
Manuel Pégourié-Gonnardef9a6ae2014-10-17 12:25:12 +02002522 if( x509_name_cmp( &child->issuer, &parent->subject ) != 0 )
Manuel Pégourié-Gonnard312010e2014-04-09 14:30:11 +02002523 return( -1 );
Manuel Pégourié-Gonnard3fed0b32014-04-08 13:18:01 +02002524
Manuel Pégourié-Gonnardd249b7a2014-06-24 11:49:16 +02002525 /* Parent must have the basicConstraints CA bit set as a general rule */
2526 need_ca_bit = 1;
2527
2528 /* Exception: v1/v2 certificates that are locally trusted. */
2529 if( top && parent->version < 3 )
2530 need_ca_bit = 0;
2531
Manuel Pégourié-Gonnardd249b7a2014-06-24 11:49:16 +02002532 if( need_ca_bit && ! parent->ca_istrue )
2533 return( -1 );
2534
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002535#if defined(MBEDTLS_X509_CHECK_KEY_USAGE)
Manuel Pégourié-Gonnardd249b7a2014-06-24 11:49:16 +02002536 if( need_ca_bit &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002537 mbedtls_x509_crt_check_key_usage( parent, MBEDTLS_X509_KU_KEY_CERT_SIGN ) != 0 )
Manuel Pégourié-Gonnardc4eff162014-06-19 12:18:08 +02002538 {
2539 return( -1 );
2540 }
Manuel Pégourié-Gonnard312010e2014-04-09 14:30:11 +02002541#endif
2542
2543 return( 0 );
Manuel Pégourié-Gonnard3fed0b32014-04-08 13:18:01 +02002544}
2545
Manuel Pégourié-Gonnard35407c72017-06-29 10:45:25 +02002546/*
Manuel Pégourié-Gonnard3e329b82017-06-29 12:55:27 +02002547 * Find a suitable parent for child in candidates, or return NULL.
2548 *
2549 * Here suitable is defined as:
Manuel Pégourié-Gonnard2f09d592017-07-03 18:30:43 +02002550 * 1. subject name matches child's issuer
2551 * 2. if necessary, the CA bit is set and key usage allows signing certs
2552 * 3. for trusted roots, the signature is correct
Manuel Pégourié-Gonnardbe4ff422017-07-14 12:04:14 +02002553 * (for intermediates, the signature is checked and the result reported)
Manuel Pégourié-Gonnard2f09d592017-07-03 18:30:43 +02002554 * 4. pathlen constraints are satisfied
Manuel Pégourié-Gonnard3e329b82017-06-29 12:55:27 +02002555 *
Manuel Pégourié-Gonnard562df402017-08-08 18:09:14 +02002556 * If there's a suitable candidate which is also time-valid, return the first
2557 * such. Otherwise, return the first suitable candidate (or NULL if there is
2558 * none).
Manuel Pégourié-Gonnard3e329b82017-06-29 12:55:27 +02002559 *
2560 * The rationale for this rule is that someone could have a list of trusted
2561 * roots with two versions on the same root with different validity periods.
2562 * (At least one user reported having such a list and wanted it to just work.)
2563 * The reason we don't just require time-validity is that generally there is
2564 * only one version, and if it's expired we want the flags to state that
2565 * rather than NOT_TRUSTED, as would be the case if we required it here.
Manuel Pégourié-Gonnard2f09d592017-07-03 18:30:43 +02002566 *
2567 * The rationale for rule 3 (signature for trusted roots) is that users might
2568 * have two versions of the same CA with different keys in their list, and the
Manuel Pégourié-Gonnardbdc54402017-07-04 00:33:39 +02002569 * way we select the correct one is by checking the signature (as we don't
2570 * rely on key identifier extensions). (This is one way users might choose to
2571 * handle key rollover, another relies on self-issued certs, see [SIRO].)
Manuel Pégourié-Gonnard98a67782017-08-17 10:52:20 +02002572 *
2573 * Arguments:
Manuel Pégourié-Gonnarde57d7432018-03-07 10:00:57 +01002574 * - [in] child: certificate for which we're looking for a parent
2575 * - [in] candidates: chained list of potential parents
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +02002576 * - [out] r_parent: parent found (or NULL)
2577 * - [out] r_signature_is_good: 1 if child signature by parent is valid, or 0
Manuel Pégourié-Gonnarde57d7432018-03-07 10:00:57 +01002578 * - [in] top: 1 if candidates consists of trusted roots, ie we're at the top
2579 * of the chain, 0 otherwise
2580 * - [in] path_cnt: number of intermediates seen so far
2581 * - [in] self_cnt: number of self-signed intermediates seen so far
2582 * (will never be greater than path_cnt)
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +02002583 * - [in-out] rs_ctx: context for restarting operations
Manuel Pégourié-Gonnarde57d7432018-03-07 10:00:57 +01002584 *
2585 * Return value:
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +02002586 * - 0 on success
2587 * - MBEDTLS_ERR_ECP_IN_PROGRESS otherwise
Manuel Pégourié-Gonnard2f1c33d2017-06-29 12:27:23 +02002588 */
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002589static int x509_crt_find_parent_in(
2590 mbedtls_x509_crt *child,
2591 mbedtls_x509_crt *candidates,
2592 mbedtls_x509_crt **r_parent,
2593 int *r_signature_is_good,
2594 int top,
Manuel Pégourié-Gonnardbb216bd2017-08-28 13:25:55 +02002595 unsigned path_cnt,
2596 unsigned self_cnt,
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002597 mbedtls_x509_crt_restart_ctx *rs_ctx )
Manuel Pégourié-Gonnard2f1c33d2017-06-29 12:27:23 +02002598{
Janos Follath865b3eb2019-12-16 11:46:15 +00002599 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002600 mbedtls_x509_crt *parent, *fallback_parent;
Benjamin Kier36050732019-05-30 14:49:17 -04002601 int signature_is_good = 0, fallback_signature_is_good;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002602
2603#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard3627a8b2017-08-23 11:20:48 +02002604 /* did we have something in progress? */
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002605 if( rs_ctx != NULL && rs_ctx->parent != NULL )
2606 {
Manuel Pégourié-Gonnard3627a8b2017-08-23 11:20:48 +02002607 /* restore saved state */
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002608 parent = rs_ctx->parent;
2609 fallback_parent = rs_ctx->fallback_parent;
Manuel Pégourié-Gonnard78d7e8c2018-07-02 12:33:14 +02002610 fallback_signature_is_good = rs_ctx->fallback_signature_is_good;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002611
Manuel Pégourié-Gonnard3627a8b2017-08-23 11:20:48 +02002612 /* clear saved state */
2613 rs_ctx->parent = NULL;
2614 rs_ctx->fallback_parent = NULL;
Manuel Pégourié-Gonnard78d7e8c2018-07-02 12:33:14 +02002615 rs_ctx->fallback_signature_is_good = 0;
Manuel Pégourié-Gonnard3627a8b2017-08-23 11:20:48 +02002616
2617 /* resume where we left */
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002618 goto check_signature;
2619 }
2620#endif
2621
2622 fallback_parent = NULL;
Manuel Pégourié-Gonnard78d7e8c2018-07-02 12:33:14 +02002623 fallback_signature_is_good = 0;
Manuel Pégourié-Gonnard2f1c33d2017-06-29 12:27:23 +02002624
Manuel Pégourié-Gonnard2f1c33d2017-06-29 12:27:23 +02002625 for( parent = candidates; parent != NULL; parent = parent->next )
2626 {
Manuel Pégourié-Gonnard2f09d592017-07-03 18:30:43 +02002627 /* basic parenting skills (name, CA bit, key usage) */
Manuel Pégourié-Gonnard27e94792017-07-04 00:49:31 +02002628 if( x509_crt_check_parent( child, parent, top ) != 0 )
Manuel Pégourié-Gonnard2f1c33d2017-06-29 12:27:23 +02002629 continue;
2630
Manuel Pégourié-Gonnard9c6118c2017-06-29 12:38:42 +02002631 /* +1 because stored max_pathlen is 1 higher that the actual value */
2632 if( parent->max_pathlen > 0 &&
Manuel Pégourié-Gonnardbb216bd2017-08-28 13:25:55 +02002633 (size_t) parent->max_pathlen < 1 + path_cnt - self_cnt )
Manuel Pégourié-Gonnard9c6118c2017-06-29 12:38:42 +02002634 {
2635 continue;
2636 }
2637
Manuel Pégourié-Gonnard2f09d592017-07-03 18:30:43 +02002638 /* Signature */
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002639#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
2640check_signature:
2641#endif
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002642 ret = x509_crt_check_signature( child, parent, rs_ctx );
2643
2644#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002645 if( rs_ctx != NULL && ret == MBEDTLS_ERR_ECP_IN_PROGRESS )
2646 {
2647 /* save state */
2648 rs_ctx->parent = parent;
2649 rs_ctx->fallback_parent = fallback_parent;
Manuel Pégourié-Gonnard78d7e8c2018-07-02 12:33:14 +02002650 rs_ctx->fallback_signature_is_good = fallback_signature_is_good;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002651
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002652 return( ret );
2653 }
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002654#else
2655 (void) ret;
2656#endif
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002657
2658 signature_is_good = ret == 0;
2659 if( top && ! signature_is_good )
Manuel Pégourié-Gonnardf82a4d52017-07-03 19:26:25 +02002660 continue;
Manuel Pégourié-Gonnard2f09d592017-07-03 18:30:43 +02002661
Manuel Pégourié-Gonnard562df402017-08-08 18:09:14 +02002662 /* optional time check */
Manuel Pégourié-Gonnard3e329b82017-06-29 12:55:27 +02002663 if( mbedtls_x509_time_is_past( &parent->valid_to ) ||
2664 mbedtls_x509_time_is_future( &parent->valid_from ) )
2665 {
Manuel Pégourié-Gonnardbe4ff422017-07-14 12:04:14 +02002666 if( fallback_parent == NULL )
2667 {
2668 fallback_parent = parent;
Manuel Pégourié-Gonnard78d7e8c2018-07-02 12:33:14 +02002669 fallback_signature_is_good = signature_is_good;
Manuel Pégourié-Gonnardbe4ff422017-07-14 12:04:14 +02002670 }
Manuel Pégourié-Gonnard3e329b82017-06-29 12:55:27 +02002671
2672 continue;
2673 }
2674
Andy Gross1f627142019-01-30 10:25:53 -06002675 *r_parent = parent;
2676 *r_signature_is_good = signature_is_good;
2677
Manuel Pégourié-Gonnard2f1c33d2017-06-29 12:27:23 +02002678 break;
2679 }
2680
Andy Gross1f627142019-01-30 10:25:53 -06002681 if( parent == NULL )
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002682 {
2683 *r_parent = fallback_parent;
Manuel Pégourié-Gonnard78d7e8c2018-07-02 12:33:14 +02002684 *r_signature_is_good = fallback_signature_is_good;
Manuel Pégourié-Gonnardbe4ff422017-07-14 12:04:14 +02002685 }
Manuel Pégourié-Gonnard3e329b82017-06-29 12:55:27 +02002686
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002687 return( 0 );
Manuel Pégourié-Gonnard2f1c33d2017-06-29 12:27:23 +02002688}
2689
2690/*
Manuel Pégourié-Gonnard63686122017-07-04 01:01:39 +02002691 * Find a parent in trusted CAs or the provided chain, or return NULL.
2692 *
2693 * Searches in trusted CAs first, and return the first suitable parent found
2694 * (see find_parent_in() for definition of suitable).
Manuel Pégourié-Gonnard98a67782017-08-17 10:52:20 +02002695 *
2696 * Arguments:
Manuel Pégourié-Gonnarde57d7432018-03-07 10:00:57 +01002697 * - [in] child: certificate for which we're looking for a parent, followed
2698 * by a chain of possible intermediates
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +02002699 * - [in] trust_ca: list of locally trusted certificates
2700 * - [out] parent: parent found (or NULL)
2701 * - [out] parent_is_trusted: 1 if returned `parent` is trusted, or 0
2702 * - [out] signature_is_good: 1 if child signature by parent is valid, or 0
2703 * - [in] path_cnt: number of links in the chain so far (EE -> ... -> child)
2704 * - [in] self_cnt: number of self-signed certs in the chain so far
Manuel Pégourié-Gonnarde57d7432018-03-07 10:00:57 +01002705 * (will always be no greater than path_cnt)
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +02002706 * - [in-out] rs_ctx: context for restarting operations
Manuel Pégourié-Gonnarde57d7432018-03-07 10:00:57 +01002707 *
2708 * Return value:
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +02002709 * - 0 on success
2710 * - MBEDTLS_ERR_ECP_IN_PROGRESS otherwise
Manuel Pégourié-Gonnard63686122017-07-04 01:01:39 +02002711 */
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002712static int x509_crt_find_parent(
2713 mbedtls_x509_crt *child,
2714 mbedtls_x509_crt *trust_ca,
2715 mbedtls_x509_crt **parent,
2716 int *parent_is_trusted,
2717 int *signature_is_good,
Manuel Pégourié-Gonnardbb216bd2017-08-28 13:25:55 +02002718 unsigned path_cnt,
2719 unsigned self_cnt,
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002720 mbedtls_x509_crt_restart_ctx *rs_ctx )
Manuel Pégourié-Gonnard63686122017-07-04 01:01:39 +02002721{
Janos Follath865b3eb2019-12-16 11:46:15 +00002722 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard18547b52017-08-14 16:11:43 +02002723 mbedtls_x509_crt *search_list;
Manuel Pégourié-Gonnard63686122017-07-04 01:01:39 +02002724
Manuel Pégourié-Gonnard63686122017-07-04 01:01:39 +02002725 *parent_is_trusted = 1;
Manuel Pégourié-Gonnard18547b52017-08-14 16:11:43 +02002726
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002727#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard3627a8b2017-08-23 11:20:48 +02002728 /* restore then clear saved state if we have some stored */
2729 if( rs_ctx != NULL && rs_ctx->parent_is_trusted != -1 )
2730 {
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002731 *parent_is_trusted = rs_ctx->parent_is_trusted;
Manuel Pégourié-Gonnard3627a8b2017-08-23 11:20:48 +02002732 rs_ctx->parent_is_trusted = -1;
2733 }
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002734#endif
2735
Manuel Pégourié-Gonnard18547b52017-08-14 16:11:43 +02002736 while( 1 ) {
2737 search_list = *parent_is_trusted ? trust_ca : child->next;
2738
2739 ret = x509_crt_find_parent_in( child, search_list,
2740 parent, signature_is_good,
2741 *parent_is_trusted,
2742 path_cnt, self_cnt, rs_ctx );
Manuel Pégourié-Gonnard63686122017-07-04 01:01:39 +02002743
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002744#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002745 if( rs_ctx != NULL && ret == MBEDTLS_ERR_ECP_IN_PROGRESS )
2746 {
2747 /* save state */
2748 rs_ctx->parent_is_trusted = *parent_is_trusted;
Manuel Pégourié-Gonnard18547b52017-08-14 16:11:43 +02002749 return( ret );
2750 }
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002751#else
2752 (void) ret;
2753#endif
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002754
Manuel Pégourié-Gonnard18547b52017-08-14 16:11:43 +02002755 /* stop here if found or already in second iteration */
2756 if( *parent != NULL || *parent_is_trusted == 0 )
2757 break;
Manuel Pégourié-Gonnard63686122017-07-04 01:01:39 +02002758
Manuel Pégourié-Gonnard18547b52017-08-14 16:11:43 +02002759 /* prepare second iteration */
2760 *parent_is_trusted = 0;
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002761 }
Manuel Pégourié-Gonnard18547b52017-08-14 16:11:43 +02002762
2763 /* extra precaution against mistakes in the caller */
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05002764 if( *parent == NULL )
Manuel Pégourié-Gonnard18547b52017-08-14 16:11:43 +02002765 {
Manuel Pégourié-Gonnarda5a3e402018-10-16 11:27:23 +02002766 *parent_is_trusted = 0;
2767 *signature_is_good = 0;
Manuel Pégourié-Gonnard18547b52017-08-14 16:11:43 +02002768 }
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002769
2770 return( 0 );
Manuel Pégourié-Gonnard63686122017-07-04 01:01:39 +02002771}
2772
2773/*
Manuel Pégourié-Gonnard27e94792017-07-04 00:49:31 +02002774 * Check if an end-entity certificate is locally trusted
2775 *
2776 * Currently we require such certificates to be self-signed (actually only
2777 * check for self-issued as self-signatures are not checked)
2778 */
2779static int x509_crt_check_ee_locally_trusted(
2780 mbedtls_x509_crt *crt,
2781 mbedtls_x509_crt *trust_ca )
2782{
2783 mbedtls_x509_crt *cur;
2784
2785 /* must be self-issued */
2786 if( x509_name_cmp( &crt->issuer, &crt->subject ) != 0 )
2787 return( -1 );
2788
2789 /* look for an exact match with trusted cert */
2790 for( cur = trust_ca; cur != NULL; cur = cur->next )
2791 {
2792 if( crt->raw.len == cur->raw.len &&
2793 memcmp( crt->raw.p, cur->raw.p, crt->raw.len ) == 0 )
2794 {
2795 return( 0 );
2796 }
2797 }
2798
2799 /* too bad */
2800 return( -1 );
2801}
2802
2803/*
Manuel Pégourié-Gonnardf86f4912017-07-05 16:43:44 +02002804 * Build and verify a certificate chain
Manuel Pégourié-Gonnard35407c72017-06-29 10:45:25 +02002805 *
Manuel Pégourié-Gonnardf86f4912017-07-05 16:43:44 +02002806 * Given a peer-provided list of certificates EE, C1, ..., Cn and
2807 * a list of trusted certs R1, ... Rp, try to build and verify a chain
Manuel Pégourié-Gonnard562df402017-08-08 18:09:14 +02002808 * EE, Ci1, ... Ciq [, Rj]
Manuel Pégourié-Gonnardf86f4912017-07-05 16:43:44 +02002809 * such that every cert in the chain is a child of the next one,
2810 * jumping to a trusted root as early as possible.
Manuel Pégourié-Gonnardbdc54402017-07-04 00:33:39 +02002811 *
Manuel Pégourié-Gonnardf86f4912017-07-05 16:43:44 +02002812 * Verify that chain and return it with flags for all issues found.
2813 *
2814 * Special cases:
2815 * - EE == Rj -> return a one-element list containing it
2816 * - EE, Ci1, ..., Ciq cannot be continued with a trusted root
2817 * -> return that chain with NOT_TRUSTED set on Ciq
Manuel Pégourié-Gonnardbdc54402017-07-04 00:33:39 +02002818 *
Manuel Pégourié-Gonnardd19a41d2017-07-14 11:05:59 +02002819 * Tests for (aspects of) this function should include at least:
2820 * - trusted EE
2821 * - EE -> trusted root
Antonin Décimo36e89b52019-01-23 15:24:37 +01002822 * - EE -> intermediate CA -> trusted root
Manuel Pégourié-Gonnardd19a41d2017-07-14 11:05:59 +02002823 * - if relevant: EE untrusted
2824 * - if relevant: EE -> intermediate, untrusted
2825 * with the aspect under test checked at each relevant level (EE, int, root).
2826 * For some aspects longer chains are required, but usually length 2 is
2827 * enough (but length 1 is not in general).
2828 *
Manuel Pégourié-Gonnardbdc54402017-07-04 00:33:39 +02002829 * Arguments:
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002830 * - [in] crt: the cert list EE, C1, ..., Cn
2831 * - [in] trust_ca: the trusted list R1, ..., Rp
2832 * - [in] ca_crl, profile: as in verify_with_profile()
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02002833 * - [out] ver_chain: the built and verified chain
Manuel Pégourié-Gonnarda9688432017-08-23 11:23:59 +02002834 * Only valid when return value is 0, may contain garbage otherwise!
2835 * Restart note: need not be the same when calling again to resume.
Manuel Pégourié-Gonnard98a67782017-08-17 10:52:20 +02002836 * - [in-out] rs_ctx: context for restarting operations
Manuel Pégourié-Gonnardf86f4912017-07-05 16:43:44 +02002837 *
2838 * Return value:
2839 * - non-zero if the chain could not be fully built and examined
2840 * - 0 is the chain was successfully built and examined,
2841 * even if it was found to be invalid
Manuel Pégourié-Gonnard35407c72017-06-29 10:45:25 +02002842 */
Manuel Pégourié-Gonnardbdc54402017-07-04 00:33:39 +02002843static int x509_crt_verify_chain(
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002844 mbedtls_x509_crt *crt,
2845 mbedtls_x509_crt *trust_ca,
2846 mbedtls_x509_crl *ca_crl,
Hanno Becker3116fb32019-03-28 13:34:42 +00002847 mbedtls_x509_crt_ca_cb_t f_ca_cb,
2848 void *p_ca_cb,
Manuel Pégourié-Gonnard95051642015-06-15 10:39:46 +02002849 const mbedtls_x509_crt_profile *profile,
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02002850 mbedtls_x509_crt_verify_chain *ver_chain,
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002851 mbedtls_x509_crt_restart_ctx *rs_ctx )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002852{
Manuel Pégourié-Gonnarda9688432017-08-23 11:23:59 +02002853 /* Don't initialize any of those variables here, so that the compiler can
2854 * catch potential issues with jumping ahead when restarting */
Janos Follath865b3eb2019-12-16 11:46:15 +00002855 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardf86f4912017-07-05 16:43:44 +02002856 uint32_t *flags;
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02002857 mbedtls_x509_crt_verify_chain_item *cur;
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002858 mbedtls_x509_crt *child;
Manuel Pégourié-Gonnard58dcd2d2017-07-03 21:35:04 +02002859 mbedtls_x509_crt *parent;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002860 int parent_is_trusted;
2861 int child_is_trusted;
2862 int signature_is_good;
Manuel Pégourié-Gonnardbb216bd2017-08-28 13:25:55 +02002863 unsigned self_cnt;
Hanno Beckerf53893b2019-03-28 13:45:55 +00002864 mbedtls_x509_crt *cur_trust_ca = NULL;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002865
2866#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
2867 /* resume if we had an operation in progress */
Manuel Pégourié-Gonnarddaf04912017-08-23 12:32:19 +02002868 if( rs_ctx != NULL && rs_ctx->in_progress == x509_crt_rs_find_parent )
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002869 {
Manuel Pégourié-Gonnard3627a8b2017-08-23 11:20:48 +02002870 /* restore saved state */
Manuel Pégourié-Gonnarda9688432017-08-23 11:23:59 +02002871 *ver_chain = rs_ctx->ver_chain; /* struct copy */
Manuel Pégourié-Gonnarddaf04912017-08-23 12:32:19 +02002872 self_cnt = rs_ctx->self_cnt;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002873
Manuel Pégourié-Gonnarddaf04912017-08-23 12:32:19 +02002874 /* restore derived state */
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002875 cur = &ver_chain->items[ver_chain->len - 1];
Manuel Pégourié-Gonnarddaf04912017-08-23 12:32:19 +02002876 child = cur->crt;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002877 flags = &cur->flags;
2878
2879 goto find_parent;
2880 }
2881#endif /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */
Manuel Pégourié-Gonnard8f8c2822017-07-03 21:25:10 +02002882
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002883 child = crt;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002884 self_cnt = 0;
2885 parent_is_trusted = 0;
2886 child_is_trusted = 0;
Manuel Pégourié-Gonnardf86f4912017-07-05 16:43:44 +02002887
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002888 while( 1 ) {
2889 /* Add certificate to the verification chain */
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02002890 cur = &ver_chain->items[ver_chain->len];
2891 cur->crt = child;
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +02002892 cur->flags = 0;
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02002893 ver_chain->len++;
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +02002894 flags = &cur->flags;
Manuel Pégourié-Gonnard66fac752017-07-03 21:39:21 +02002895
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002896 /* Check time-validity (all certificates) */
2897 if( mbedtls_x509_time_is_past( &child->valid_to ) )
2898 *flags |= MBEDTLS_X509_BADCERT_EXPIRED;
Manuel Pégourié-Gonnard66fac752017-07-03 21:39:21 +02002899
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002900 if( mbedtls_x509_time_is_future( &child->valid_from ) )
2901 *flags |= MBEDTLS_X509_BADCERT_FUTURE;
Manuel Pégourié-Gonnardcb396102017-07-04 00:00:24 +02002902
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002903 /* Stop here for trusted roots (but not for trusted EE certs) */
2904 if( child_is_trusted )
2905 return( 0 );
Manuel Pégourié-Gonnard66fac752017-07-03 21:39:21 +02002906
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002907 /* Check signature algorithm: MD & PK algs */
2908 if( x509_profile_check_md_alg( profile, child->sig_md ) != 0 )
2909 *flags |= MBEDTLS_X509_BADCERT_BAD_MD;
Manuel Pégourié-Gonnard66fac752017-07-03 21:39:21 +02002910
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002911 if( x509_profile_check_pk_alg( profile, child->sig_pk ) != 0 )
2912 *flags |= MBEDTLS_X509_BADCERT_BAD_PK;
Manuel Pégourié-Gonnard27e94792017-07-04 00:49:31 +02002913
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002914 /* Special case: EE certs that are locally trusted */
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02002915 if( ver_chain->len == 1 &&
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002916 x509_crt_check_ee_locally_trusted( child, trust_ca ) == 0 )
2917 {
2918 return( 0 );
2919 }
Manuel Pégourié-Gonnard8f8c2822017-07-03 21:25:10 +02002920
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002921#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
2922find_parent:
2923#endif
Hanno Beckerf53893b2019-03-28 13:45:55 +00002924
2925 /* Obtain list of potential trusted signers from CA callback,
2926 * or use statically provided list. */
2927#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
2928 if( f_ca_cb != NULL )
2929 {
2930 mbedtls_x509_crt_free( ver_chain->trust_ca_cb_result );
2931 mbedtls_free( ver_chain->trust_ca_cb_result );
2932 ver_chain->trust_ca_cb_result = NULL;
2933
2934 ret = f_ca_cb( p_ca_cb, child, &ver_chain->trust_ca_cb_result );
2935 if( ret != 0 )
2936 return( MBEDTLS_ERR_X509_FATAL_ERROR );
2937
2938 cur_trust_ca = ver_chain->trust_ca_cb_result;
2939 }
2940 else
2941#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
2942 {
2943 ((void) f_ca_cb);
2944 ((void) p_ca_cb);
2945 cur_trust_ca = trust_ca;
2946 }
2947
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002948 /* Look for a parent in trusted CAs or up the chain */
Hanno Beckerf53893b2019-03-28 13:45:55 +00002949 ret = x509_crt_find_parent( child, cur_trust_ca, &parent,
Manuel Pégourié-Gonnardbe4ff422017-07-14 12:04:14 +02002950 &parent_is_trusted, &signature_is_good,
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02002951 ver_chain->len - 1, self_cnt, rs_ctx );
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002952
2953#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002954 if( rs_ctx != NULL && ret == MBEDTLS_ERR_ECP_IN_PROGRESS )
2955 {
2956 /* save state */
Manuel Pégourié-Gonnarddaf04912017-08-23 12:32:19 +02002957 rs_ctx->in_progress = x509_crt_rs_find_parent;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002958 rs_ctx->self_cnt = self_cnt;
Manuel Pégourié-Gonnarda9688432017-08-23 11:23:59 +02002959 rs_ctx->ver_chain = *ver_chain; /* struct copy */
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002960
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002961 return( ret );
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002962 }
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002963#else
2964 (void) ret;
2965#endif
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002966
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002967 /* No parent? We're done here */
2968 if( parent == NULL )
2969 {
2970 *flags |= MBEDTLS_X509_BADCERT_NOT_TRUSTED;
2971 return( 0 );
2972 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002973
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002974 /* Count intermediate self-issued (not necessarily self-signed) certs.
2975 * These can occur with some strategies for key rollover, see [SIRO],
2976 * and should be excluded from max_pathlen checks. */
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02002977 if( ver_chain->len != 1 &&
Manuel Pégourié-Gonnard24611f92017-08-09 10:28:07 +02002978 x509_name_cmp( &child->issuer, &child->subject ) == 0 )
2979 {
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002980 self_cnt++;
Manuel Pégourié-Gonnard24611f92017-08-09 10:28:07 +02002981 }
Manuel Pégourié-Gonnardfd6c85c2014-11-20 16:34:20 +01002982
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002983 /* path_cnt is 0 for the first intermediate CA,
2984 * and if parent is trusted it's not an intermediate CA */
2985 if( ! parent_is_trusted &&
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02002986 ver_chain->len > MBEDTLS_X509_MAX_INTERMEDIATE_CA )
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002987 {
2988 /* return immediately to avoid overflow the chain array */
2989 return( MBEDTLS_ERR_X509_FATAL_ERROR );
2990 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002991
Manuel Pégourié-Gonnard98a67782017-08-17 10:52:20 +02002992 /* signature was checked while searching parent */
Manuel Pégourié-Gonnardbe4ff422017-07-14 12:04:14 +02002993 if( ! signature_is_good )
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002994 *flags |= MBEDTLS_X509_BADCERT_NOT_TRUSTED;
2995
2996 /* check size of signing key */
Manuel Pégourié-Gonnard3f816912017-10-26 10:24:16 +02002997 if( x509_profile_check_key( profile, &parent->pk ) != 0 )
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002998 *flags |= MBEDTLS_X509_BADCERT_BAD_KEY;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002999
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003000#if defined(MBEDTLS_X509_CRL_PARSE_C)
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02003001 /* Check trusted CA's CRL for the given crt */
Manuel Pégourié-Gonnard562df402017-08-08 18:09:14 +02003002 *flags |= x509_crt_verifycrl( child, parent, ca_crl, profile );
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02003003#else
3004 (void) ca_crl;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003005#endif
3006
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02003007 /* prepare for next iteration */
3008 child = parent;
3009 parent = NULL;
3010 child_is_trusted = parent_is_trusted;
Manuel Pégourié-Gonnardbe4ff422017-07-14 12:04:14 +02003011 signature_is_good = 0;
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02003012 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003013}
3014
3015/*
Manuel Pégourié-Gonnarda468eb12017-07-04 01:31:59 +02003016 * Check for CN match
3017 */
3018static int x509_crt_check_cn( const mbedtls_x509_buf *name,
3019 const char *cn, size_t cn_len )
3020{
3021 /* try exact match */
3022 if( name->len == cn_len &&
3023 x509_memcasecmp( cn, name->p, cn_len ) == 0 )
3024 {
3025 return( 0 );
3026 }
3027
3028 /* try wildcard match */
Manuel Pégourié-Gonnard900fba62017-10-18 14:28:11 +02003029 if( x509_check_wildcard( cn, name ) == 0 )
Manuel Pégourié-Gonnarda468eb12017-07-04 01:31:59 +02003030 {
3031 return( 0 );
3032 }
3033
3034 return( -1 );
3035}
3036
3037/*
Manuel Pégourié-Gonnardf3e4bd82020-07-21 13:22:41 +02003038 * Check for SAN match, see RFC 5280 Section 4.2.1.6
3039 */
3040static int x509_crt_check_san( const mbedtls_x509_buf *name,
3041 const char *cn, size_t cn_len )
3042{
3043 const unsigned char san_type = (unsigned char) name->tag &
3044 MBEDTLS_ASN1_TAG_VALUE_MASK;
3045
3046 /* dNSName */
3047 if( san_type == MBEDTLS_X509_SAN_DNS_NAME )
3048 return( x509_crt_check_cn( name, cn, cn_len ) );
3049
3050 /* (We may handle other types here later.) */
3051
3052 /* Unrecognized type */
3053 return( -1 );
3054}
3055
3056/*
Manuel Pégourié-Gonnard1300e992017-07-04 01:13:44 +02003057 * Verify the requested CN - only call this if cn is not NULL!
3058 */
Manuel Pégourié-Gonnarda468eb12017-07-04 01:31:59 +02003059static void x509_crt_verify_name( const mbedtls_x509_crt *crt,
Manuel Pégourié-Gonnard1300e992017-07-04 01:13:44 +02003060 const char *cn,
3061 uint32_t *flags )
3062{
Manuel Pégourié-Gonnarda468eb12017-07-04 01:31:59 +02003063 const mbedtls_x509_name *name;
3064 const mbedtls_x509_sequence *cur;
3065 size_t cn_len = strlen( cn );
Manuel Pégourié-Gonnard1300e992017-07-04 01:13:44 +02003066
3067 if( crt->ext_types & MBEDTLS_X509_EXT_SUBJECT_ALT_NAME )
3068 {
Manuel Pégourié-Gonnarda468eb12017-07-04 01:31:59 +02003069 for( cur = &crt->subject_alt_names; cur != NULL; cur = cur->next )
Manuel Pégourié-Gonnard1300e992017-07-04 01:13:44 +02003070 {
Manuel Pégourié-Gonnardf3e4bd82020-07-21 13:22:41 +02003071 if( x509_crt_check_san( &cur->buf, cn, cn_len ) == 0 )
Manuel Pégourié-Gonnard1300e992017-07-04 01:13:44 +02003072 break;
Manuel Pégourié-Gonnard1300e992017-07-04 01:13:44 +02003073 }
3074
3075 if( cur == NULL )
3076 *flags |= MBEDTLS_X509_BADCERT_CN_MISMATCH;
3077 }
3078 else
3079 {
Manuel Pégourié-Gonnard08eacec2017-10-18 14:20:24 +02003080 for( name = &crt->subject; name != NULL; name = name->next )
Manuel Pégourié-Gonnard1300e992017-07-04 01:13:44 +02003081 {
Manuel Pégourié-Gonnarda468eb12017-07-04 01:31:59 +02003082 if( MBEDTLS_OID_CMP( MBEDTLS_OID_AT_CN, &name->oid ) == 0 &&
3083 x509_crt_check_cn( &name->val, cn, cn_len ) == 0 )
Manuel Pégourié-Gonnard1300e992017-07-04 01:13:44 +02003084 {
Manuel Pégourié-Gonnarda468eb12017-07-04 01:31:59 +02003085 break;
Manuel Pégourié-Gonnard1300e992017-07-04 01:13:44 +02003086 }
Manuel Pégourié-Gonnard1300e992017-07-04 01:13:44 +02003087 }
3088
3089 if( name == NULL )
3090 *flags |= MBEDTLS_X509_BADCERT_CN_MISMATCH;
3091 }
3092}
3093
3094/*
Manuel Pégourié-Gonnarda707e1d2017-07-05 17:18:42 +02003095 * Merge the flags for all certs in the chain, after calling callback
3096 */
3097static int x509_crt_merge_flags_with_cb(
3098 uint32_t *flags,
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02003099 const mbedtls_x509_crt_verify_chain *ver_chain,
Manuel Pégourié-Gonnarda707e1d2017-07-05 17:18:42 +02003100 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
3101 void *p_vrfy )
3102{
Janos Follath865b3eb2019-12-16 11:46:15 +00003103 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardbb216bd2017-08-28 13:25:55 +02003104 unsigned i;
Manuel Pégourié-Gonnarda707e1d2017-07-05 17:18:42 +02003105 uint32_t cur_flags;
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02003106 const mbedtls_x509_crt_verify_chain_item *cur;
Manuel Pégourié-Gonnarda707e1d2017-07-05 17:18:42 +02003107
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02003108 for( i = ver_chain->len; i != 0; --i )
Manuel Pégourié-Gonnarda707e1d2017-07-05 17:18:42 +02003109 {
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02003110 cur = &ver_chain->items[i-1];
3111 cur_flags = cur->flags;
Manuel Pégourié-Gonnarda707e1d2017-07-05 17:18:42 +02003112
3113 if( NULL != f_vrfy )
Manuel Pégourié-Gonnardbb216bd2017-08-28 13:25:55 +02003114 if( ( ret = f_vrfy( p_vrfy, cur->crt, (int) i-1, &cur_flags ) ) != 0 )
Manuel Pégourié-Gonnarda707e1d2017-07-05 17:18:42 +02003115 return( ret );
3116
3117 *flags |= cur_flags;
3118 }
3119
3120 return( 0 );
3121}
3122
3123/*
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +02003124 * Verify the certificate validity, with profile, restartable version
3125 *
3126 * This function:
3127 * - checks the requested CN (if any)
3128 * - checks the type and size of the EE cert's key,
3129 * as that isn't done as part of chain building/verification currently
3130 * - builds and verifies the chain
3131 * - then calls the callback and merges the flags
Hanno Becker3116fb32019-03-28 13:34:42 +00003132 *
3133 * The parameters pairs `trust_ca`, `ca_crl` and `f_ca_cb`, `p_ca_cb`
3134 * are mutually exclusive: If `f_ca_cb != NULL`, it will be used by the
3135 * verification routine to search for trusted signers, and CRLs will
3136 * be disabled. Otherwise, `trust_ca` will be used as the static list
3137 * of trusted signers, and `ca_crl` will be use as the static list
3138 * of CRLs.
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +02003139 */
Jarno Lamsa2ee67a62019-04-01 14:59:33 +03003140static int x509_crt_verify_restartable_ca_cb( mbedtls_x509_crt *crt,
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +02003141 mbedtls_x509_crt *trust_ca,
3142 mbedtls_x509_crl *ca_crl,
Hanno Becker3116fb32019-03-28 13:34:42 +00003143 mbedtls_x509_crt_ca_cb_t f_ca_cb,
3144 void *p_ca_cb,
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +02003145 const mbedtls_x509_crt_profile *profile,
3146 const char *cn, uint32_t *flags,
3147 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
3148 void *p_vrfy,
3149 mbedtls_x509_crt_restart_ctx *rs_ctx )
3150{
Janos Follath865b3eb2019-12-16 11:46:15 +00003151 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard65eefc82015-10-23 14:08:48 +02003152 mbedtls_pk_type_t pk_type;
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02003153 mbedtls_x509_crt_verify_chain ver_chain;
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +02003154 uint32_t ee_flags;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003155
3156 *flags = 0;
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +02003157 ee_flags = 0;
3158 x509_crt_verify_chain_reset( &ver_chain );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003159
Manuel Pégourié-Gonnardd15795a2017-06-22 12:19:27 +02003160 if( profile == NULL )
3161 {
3162 ret = MBEDTLS_ERR_X509_BAD_INPUT_DATA;
3163 goto exit;
3164 }
3165
Manuel Pégourié-Gonnard1300e992017-07-04 01:13:44 +02003166 /* check name if requested */
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003167 if( cn != NULL )
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +02003168 x509_crt_verify_name( crt, cn, &ee_flags );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003169
Manuel Pégourié-Gonnard65eefc82015-10-23 14:08:48 +02003170 /* Check the type and size of the key */
3171 pk_type = mbedtls_pk_get_type( &crt->pk );
3172
3173 if( x509_profile_check_pk_alg( profile, pk_type ) != 0 )
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +02003174 ee_flags |= MBEDTLS_X509_BADCERT_BAD_PK;
Manuel Pégourié-Gonnard65eefc82015-10-23 14:08:48 +02003175
Manuel Pégourié-Gonnard3f816912017-10-26 10:24:16 +02003176 if( x509_profile_check_key( profile, &crt->pk ) != 0 )
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +02003177 ee_flags |= MBEDTLS_X509_BADCERT_BAD_KEY;
Manuel Pégourié-Gonnard65eefc82015-10-23 14:08:48 +02003178
Manuel Pégourié-Gonnardbdc54402017-07-04 00:33:39 +02003179 /* Check the chain */
Hanno Becker3116fb32019-03-28 13:34:42 +00003180 ret = x509_crt_verify_chain( crt, trust_ca, ca_crl,
3181 f_ca_cb, p_ca_cb, profile,
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02003182 &ver_chain, rs_ctx );
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02003183
Manuel Pégourié-Gonnardc547d1a2017-07-05 13:28:45 +02003184 if( ret != 0 )
3185 goto exit;
3186
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +02003187 /* Merge end-entity flags */
3188 ver_chain.items[0].flags |= ee_flags;
3189
Manuel Pégourié-Gonnarda707e1d2017-07-05 17:18:42 +02003190 /* Build final flags, calling callback on the way if any */
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02003191 ret = x509_crt_merge_flags_with_cb( flags, &ver_chain, f_vrfy, p_vrfy );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003192
Manuel Pégourié-Gonnardd15795a2017-06-22 12:19:27 +02003193exit:
Hanno Beckerf53893b2019-03-28 13:45:55 +00003194
3195#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
3196 mbedtls_x509_crt_free( ver_chain.trust_ca_cb_result );
3197 mbedtls_free( ver_chain.trust_ca_cb_result );
3198 ver_chain.trust_ca_cb_result = NULL;
3199#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
3200
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02003201#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
3202 if( rs_ctx != NULL && ret != MBEDTLS_ERR_ECP_IN_PROGRESS )
3203 mbedtls_x509_crt_restart_free( rs_ctx );
3204#endif
3205
Manuel Pégourié-Gonnard9107b5f2017-07-06 12:16:25 +02003206 /* prevent misuse of the vrfy callback - VERIFY_FAILED would be ignored by
3207 * the SSL module for authmode optional, but non-zero return from the
3208 * callback means a fatal error so it shouldn't be ignored */
Manuel Pégourié-Gonnard31458a12017-06-26 10:11:49 +02003209 if( ret == MBEDTLS_ERR_X509_CERT_VERIFY_FAILED )
3210 ret = MBEDTLS_ERR_X509_FATAL_ERROR;
3211
Manuel Pégourié-Gonnardd15795a2017-06-22 12:19:27 +02003212 if( ret != 0 )
3213 {
3214 *flags = (uint32_t) -1;
3215 return( ret );
3216 }
3217
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003218 if( *flags != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003219 return( MBEDTLS_ERR_X509_CERT_VERIFY_FAILED );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003220
3221 return( 0 );
3222}
3223
Hanno Becker3116fb32019-03-28 13:34:42 +00003224
3225/*
3226 * Verify the certificate validity (default profile, not restartable)
3227 */
3228int mbedtls_x509_crt_verify( mbedtls_x509_crt *crt,
3229 mbedtls_x509_crt *trust_ca,
3230 mbedtls_x509_crl *ca_crl,
3231 const char *cn, uint32_t *flags,
3232 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
3233 void *p_vrfy )
3234{
Jarno Lamsa2ee67a62019-04-01 14:59:33 +03003235 return( x509_crt_verify_restartable_ca_cb( crt, trust_ca, ca_crl,
Hanno Becker3116fb32019-03-28 13:34:42 +00003236 NULL, NULL,
3237 &mbedtls_x509_crt_profile_default,
3238 cn, flags,
3239 f_vrfy, p_vrfy, NULL ) );
3240}
3241
3242/*
3243 * Verify the certificate validity (user-chosen profile, not restartable)
3244 */
3245int mbedtls_x509_crt_verify_with_profile( mbedtls_x509_crt *crt,
3246 mbedtls_x509_crt *trust_ca,
3247 mbedtls_x509_crl *ca_crl,
3248 const mbedtls_x509_crt_profile *profile,
3249 const char *cn, uint32_t *flags,
3250 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
3251 void *p_vrfy )
3252{
Jarno Lamsa2ee67a62019-04-01 14:59:33 +03003253 return( x509_crt_verify_restartable_ca_cb( crt, trust_ca, ca_crl,
Hanno Becker3116fb32019-03-28 13:34:42 +00003254 NULL, NULL,
3255 profile, cn, flags,
3256 f_vrfy, p_vrfy, NULL ) );
3257}
3258
3259#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
3260/*
3261 * Verify the certificate validity (user-chosen profile, CA callback,
3262 * not restartable).
3263 */
Jarno Lamsa31d9db62019-04-01 14:33:49 +03003264int mbedtls_x509_crt_verify_with_ca_cb( mbedtls_x509_crt *crt,
Hanno Becker3116fb32019-03-28 13:34:42 +00003265 mbedtls_x509_crt_ca_cb_t f_ca_cb,
3266 void *p_ca_cb,
3267 const mbedtls_x509_crt_profile *profile,
3268 const char *cn, uint32_t *flags,
3269 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
3270 void *p_vrfy )
3271{
Jarno Lamsa2ee67a62019-04-01 14:59:33 +03003272 return( x509_crt_verify_restartable_ca_cb( crt, NULL, NULL,
Hanno Becker3116fb32019-03-28 13:34:42 +00003273 f_ca_cb, p_ca_cb,
3274 profile, cn, flags,
3275 f_vrfy, p_vrfy, NULL ) );
3276}
3277#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
3278
3279int mbedtls_x509_crt_verify_restartable( mbedtls_x509_crt *crt,
3280 mbedtls_x509_crt *trust_ca,
3281 mbedtls_x509_crl *ca_crl,
3282 const mbedtls_x509_crt_profile *profile,
3283 const char *cn, uint32_t *flags,
3284 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
3285 void *p_vrfy,
3286 mbedtls_x509_crt_restart_ctx *rs_ctx )
3287{
Jarno Lamsa2ee67a62019-04-01 14:59:33 +03003288 return( x509_crt_verify_restartable_ca_cb( crt, trust_ca, ca_crl,
Hanno Becker3116fb32019-03-28 13:34:42 +00003289 NULL, NULL,
3290 profile, cn, flags,
3291 f_vrfy, p_vrfy, rs_ctx ) );
3292}
3293
3294
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003295/*
Paul Bakker369d2eb2013-09-18 11:58:25 +02003296 * Initialize a certificate chain
3297 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003298void mbedtls_x509_crt_init( mbedtls_x509_crt *crt )
Paul Bakker369d2eb2013-09-18 11:58:25 +02003299{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003300 memset( crt, 0, sizeof(mbedtls_x509_crt) );
Paul Bakker369d2eb2013-09-18 11:58:25 +02003301}
3302
3303/*
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003304 * Unallocate all certificate data
3305 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003306void mbedtls_x509_crt_free( mbedtls_x509_crt *crt )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003307{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003308 mbedtls_x509_crt *cert_cur = crt;
3309 mbedtls_x509_crt *cert_prv;
3310 mbedtls_x509_name *name_cur;
3311 mbedtls_x509_name *name_prv;
3312 mbedtls_x509_sequence *seq_cur;
3313 mbedtls_x509_sequence *seq_prv;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003314
3315 if( crt == NULL )
3316 return;
3317
3318 do
3319 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003320 mbedtls_pk_free( &cert_cur->pk );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003321
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003322#if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT)
3323 mbedtls_free( cert_cur->sig_opts );
Manuel Pégourié-Gonnardf75f2f72014-06-05 15:14:28 +02003324#endif
3325
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003326 name_cur = cert_cur->issuer.next;
3327 while( name_cur != NULL )
3328 {
3329 name_prv = name_cur;
3330 name_cur = name_cur->next;
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05003331 mbedtls_platform_zeroize( name_prv, sizeof( mbedtls_x509_name ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003332 mbedtls_free( name_prv );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003333 }
3334
3335 name_cur = cert_cur->subject.next;
3336 while( name_cur != NULL )
3337 {
3338 name_prv = name_cur;
3339 name_cur = name_cur->next;
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05003340 mbedtls_platform_zeroize( name_prv, sizeof( mbedtls_x509_name ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003341 mbedtls_free( name_prv );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003342 }
3343
3344 seq_cur = cert_cur->ext_key_usage.next;
3345 while( seq_cur != NULL )
3346 {
3347 seq_prv = seq_cur;
3348 seq_cur = seq_cur->next;
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05003349 mbedtls_platform_zeroize( seq_prv,
3350 sizeof( mbedtls_x509_sequence ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003351 mbedtls_free( seq_prv );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003352 }
3353
3354 seq_cur = cert_cur->subject_alt_names.next;
3355 while( seq_cur != NULL )
3356 {
3357 seq_prv = seq_cur;
3358 seq_cur = seq_cur->next;
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05003359 mbedtls_platform_zeroize( seq_prv,
3360 sizeof( mbedtls_x509_sequence ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003361 mbedtls_free( seq_prv );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003362 }
3363
Ron Eldor74d9acc2019-03-21 14:00:03 +02003364 seq_cur = cert_cur->certificate_policies.next;
3365 while( seq_cur != NULL )
3366 {
3367 seq_prv = seq_cur;
3368 seq_cur = seq_cur->next;
3369 mbedtls_platform_zeroize( seq_prv,
3370 sizeof( mbedtls_x509_sequence ) );
3371 mbedtls_free( seq_prv );
3372 }
3373
Hanno Becker1a65dcd2019-01-31 08:57:44 +00003374 if( cert_cur->raw.p != NULL && cert_cur->own_buffer )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003375 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05003376 mbedtls_platform_zeroize( cert_cur->raw.p, cert_cur->raw.len );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003377 mbedtls_free( cert_cur->raw.p );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003378 }
3379
3380 cert_cur = cert_cur->next;
3381 }
3382 while( cert_cur != NULL );
3383
3384 cert_cur = crt;
3385 do
3386 {
3387 cert_prv = cert_cur;
3388 cert_cur = cert_cur->next;
3389
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05003390 mbedtls_platform_zeroize( cert_prv, sizeof( mbedtls_x509_crt ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003391 if( cert_prv != crt )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003392 mbedtls_free( cert_prv );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003393 }
3394 while( cert_cur != NULL );
3395}
3396
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +02003397#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
3398/*
3399 * Initialize a restart context
3400 */
3401void mbedtls_x509_crt_restart_init( mbedtls_x509_crt_restart_ctx *ctx )
3402{
Manuel Pégourié-Gonnard15d7df22017-08-17 14:33:31 +02003403 mbedtls_pk_restart_init( &ctx->pk );
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02003404
3405 ctx->parent = NULL;
3406 ctx->fallback_parent = NULL;
Manuel Pégourié-Gonnard78d7e8c2018-07-02 12:33:14 +02003407 ctx->fallback_signature_is_good = 0;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02003408
3409 ctx->parent_is_trusted = -1;
3410
Manuel Pégourié-Gonnarddaf04912017-08-23 12:32:19 +02003411 ctx->in_progress = x509_crt_rs_none;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02003412 ctx->self_cnt = 0;
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +02003413 x509_crt_verify_chain_reset( &ctx->ver_chain );
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +02003414}
3415
3416/*
3417 * Free the components of a restart context
3418 */
3419void mbedtls_x509_crt_restart_free( mbedtls_x509_crt_restart_ctx *ctx )
3420{
3421 if( ctx == NULL )
3422 return;
3423
Manuel Pégourié-Gonnard15d7df22017-08-17 14:33:31 +02003424 mbedtls_pk_restart_free( &ctx->pk );
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02003425 mbedtls_x509_crt_restart_init( ctx );
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +02003426}
3427#endif /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */
3428
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003429#endif /* MBEDTLS_X509_CRT_PARSE_C */