blob: a6cccf81445d19b795afd805955af0cd64b8e5d1 [file] [log] [blame]
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001/*
Manuel Pégourié-Gonnard1c082f32014-06-12 22:34:55 +02002 * X.509 certificate parsing and verification
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003 *
Bence Szépkúti1e148272020-08-07 13:07:28 +02004 * Copyright The Mbed TLS Contributors
Manuel Pégourié-Gonnard37ff1402015-09-04 14:21:07 +02005 * SPDX-License-Identifier: Apache-2.0
6 *
7 * Licensed under the Apache License, Version 2.0 (the "License"); you may
8 * not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
15 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
Paul Bakker7c6b2c32013-09-16 13:49:26 +020018 */
19/*
20 * The ITU-T X.509 standard defines a certificate format for PKI.
21 *
Manuel Pégourié-Gonnard1c082f32014-06-12 22:34:55 +020022 * http://www.ietf.org/rfc/rfc5280.txt (Certificates and CRLs)
23 * http://www.ietf.org/rfc/rfc3279.txt (Alg IDs for CRLs)
24 * http://www.ietf.org/rfc/rfc2986.txt (CSRs, aka PKCS#10)
Paul Bakker7c6b2c32013-09-16 13:49:26 +020025 *
26 * http://www.itu.int/ITU-T/studygroups/com17/languages/X.680-0207.pdf
27 * http://www.itu.int/ITU-T/studygroups/com17/languages/X.690-0207.pdf
Manuel Pégourié-Gonnard2f09d592017-07-03 18:30:43 +020028 *
29 * [SIRO] https://cabforum.org/wp-content/uploads/Chunghwatelecom201503cabforumV4.pdf
Paul Bakker7c6b2c32013-09-16 13:49:26 +020030 */
31
Gilles Peskinedb09ef62020-06-03 01:43:33 +020032#include "common.h"
Paul Bakker7c6b2c32013-09-16 13:49:26 +020033
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020034#if defined(MBEDTLS_X509_CRT_PARSE_C)
Paul Bakker7c6b2c32013-09-16 13:49:26 +020035
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000036#include "mbedtls/x509_crt.h"
Janos Follath73c616b2019-12-18 15:07:04 +000037#include "mbedtls/error.h"
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000038#include "mbedtls/oid.h"
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050039#include "mbedtls/platform_util.h"
Rich Evans00ab4702015-02-06 13:43:58 +000040
Rich Evans00ab4702015-02-06 13:43:58 +000041#include <string.h>
42
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020043#if defined(MBEDTLS_PEM_PARSE_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000044#include "mbedtls/pem.h"
Paul Bakker7c6b2c32013-09-16 13:49:26 +020045#endif
46
Andrzej Kurekd4a65532018-10-31 06:18:39 -040047#if defined(MBEDTLS_USE_PSA_CRYPTO)
48#include "psa/crypto.h"
49#include "mbedtls/psa_util.h"
50#endif
51
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020052#if defined(MBEDTLS_PLATFORM_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000053#include "mbedtls/platform.h"
Paul Bakker7c6b2c32013-09-16 13:49:26 +020054#else
Simon Butcherd2642582018-10-03 15:11:19 +010055#include <stdio.h>
Rich Evans00ab4702015-02-06 13:43:58 +000056#include <stdlib.h>
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020057#define mbedtls_free free
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +020058#define mbedtls_calloc calloc
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020059#define mbedtls_snprintf snprintf
Paul Bakker7c6b2c32013-09-16 13:49:26 +020060#endif
61
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020062#if defined(MBEDTLS_THREADING_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000063#include "mbedtls/threading.h"
Manuel Pégourié-Gonnard5ad68e42013-11-28 17:11:54 +010064#endif
65
Paul Bakkerfa6a6202013-10-28 18:48:30 +010066#if defined(_WIN32) && !defined(EFIX64) && !defined(EFI32)
Paul Bakker7c6b2c32013-09-16 13:49:26 +020067#include <windows.h>
68#else
69#include <time.h>
70#endif
71
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020072#if defined(MBEDTLS_FS_IO)
Rich Evans00ab4702015-02-06 13:43:58 +000073#include <stdio.h>
Paul Bakker5ff3f912014-04-04 15:08:20 +020074#if !defined(_WIN32) || defined(EFIX64) || defined(EFI32)
Paul Bakker7c6b2c32013-09-16 13:49:26 +020075#include <sys/types.h>
76#include <sys/stat.h>
77#include <dirent.h>
Rich Evans00ab4702015-02-06 13:43:58 +000078#endif /* !_WIN32 || EFIX64 || EFI32 */
Paul Bakker7c6b2c32013-09-16 13:49:26 +020079#endif
80
Manuel Pégourié-Gonnardc547d1a2017-07-05 13:28:45 +020081/*
82 * Item in a verification chain: cert and flags for it
83 */
84typedef struct {
85 mbedtls_x509_crt *crt;
86 uint32_t flags;
87} x509_crt_verify_chain_item;
88
89/*
90 * Max size of verification chain: end-entity + intermediates + trusted root
91 */
92#define X509_MAX_VERIFY_CHAIN_SIZE ( MBEDTLS_X509_MAX_INTERMEDIATE_CA + 2 )
Paul Bakker34617722014-06-13 17:20:13 +020093
Gilles Peskine0ecd7192021-06-07 21:24:26 +020094/* Default profile. Do not remove items unless there are serious security
95 * concerns. */
Manuel Pégourié-Gonnard88db5da2015-06-15 14:34:59 +020096const mbedtls_x509_crt_profile mbedtls_x509_crt_profile_default =
97{
Gilles Peskine5e79cb32017-05-04 16:17:21 +020098 /* Only SHA-2 hashes */
Manuel Pégourié-Gonnardf8ea8562015-06-15 15:33:19 +020099 MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_SHA224 ) |
100 MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_SHA256 ) |
101 MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_SHA384 ) |
102 MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_SHA512 ),
103 0xFFFFFFF, /* Any PK alg */
104 0xFFFFFFF, /* Any curve */
Manuel Pégourié-Gonnard88db5da2015-06-15 14:34:59 +0200105 2048,
106};
107
108/*
109 * Next-default profile
110 */
Manuel Pégourié-Gonnard88db5da2015-06-15 14:34:59 +0200111const mbedtls_x509_crt_profile mbedtls_x509_crt_profile_next =
112{
Manuel Pégourié-Gonnardf8ea8562015-06-15 15:33:19 +0200113 /* Hashes from SHA-256 and above */
114 MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_SHA256 ) |
115 MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_SHA384 ) |
116 MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_SHA512 ),
117 0xFFFFFFF, /* Any PK alg */
118#if defined(MBEDTLS_ECP_C)
119 /* Curves at or above 128-bit security level */
120 MBEDTLS_X509_ID_FLAG( MBEDTLS_ECP_DP_SECP256R1 ) |
121 MBEDTLS_X509_ID_FLAG( MBEDTLS_ECP_DP_SECP384R1 ) |
122 MBEDTLS_X509_ID_FLAG( MBEDTLS_ECP_DP_SECP521R1 ) |
123 MBEDTLS_X509_ID_FLAG( MBEDTLS_ECP_DP_BP256R1 ) |
124 MBEDTLS_X509_ID_FLAG( MBEDTLS_ECP_DP_BP384R1 ) |
125 MBEDTLS_X509_ID_FLAG( MBEDTLS_ECP_DP_BP512R1 ) |
126 MBEDTLS_X509_ID_FLAG( MBEDTLS_ECP_DP_SECP256K1 ),
127#else
128 0,
129#endif
Manuel Pégourié-Gonnard88db5da2015-06-15 14:34:59 +0200130 2048,
131};
132
133/*
134 * NSA Suite B Profile
135 */
Manuel Pégourié-Gonnard88db5da2015-06-15 14:34:59 +0200136const mbedtls_x509_crt_profile mbedtls_x509_crt_profile_suiteb =
137{
Manuel Pégourié-Gonnardf8ea8562015-06-15 15:33:19 +0200138 /* Only SHA-256 and 384 */
139 MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_SHA256 ) |
140 MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_SHA384 ),
141 /* Only ECDSA */
Ron Eldor85e1dcf2018-02-06 15:59:38 +0200142 MBEDTLS_X509_ID_FLAG( MBEDTLS_PK_ECDSA ) |
143 MBEDTLS_X509_ID_FLAG( MBEDTLS_PK_ECKEY ),
Manuel Pégourié-Gonnardf8ea8562015-06-15 15:33:19 +0200144#if defined(MBEDTLS_ECP_C)
145 /* Only NIST P-256 and P-384 */
146 MBEDTLS_X509_ID_FLAG( MBEDTLS_ECP_DP_SECP256R1 ) |
147 MBEDTLS_X509_ID_FLAG( MBEDTLS_ECP_DP_SECP384R1 ),
148#else
149 0,
150#endif
151 0,
Manuel Pégourié-Gonnard88db5da2015-06-15 14:34:59 +0200152};
153
154/*
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200155 * Check md_alg against profile
Manuel Pégourié-Gonnard3f816912017-10-26 10:24:16 +0200156 * Return 0 if md_alg is acceptable for this profile, -1 otherwise
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200157 */
158static int x509_profile_check_md_alg( const mbedtls_x509_crt_profile *profile,
159 mbedtls_md_type_t md_alg )
160{
Philippe Antoineb5b25432018-05-11 11:06:29 +0200161 if( md_alg == MBEDTLS_MD_NONE )
162 return( -1 );
163
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200164 if( ( profile->allowed_mds & MBEDTLS_X509_ID_FLAG( md_alg ) ) != 0 )
165 return( 0 );
166
167 return( -1 );
168}
169
170/*
171 * Check pk_alg against profile
Manuel Pégourié-Gonnard3f816912017-10-26 10:24:16 +0200172 * Return 0 if pk_alg is acceptable for this profile, -1 otherwise
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200173 */
174static int x509_profile_check_pk_alg( const mbedtls_x509_crt_profile *profile,
175 mbedtls_pk_type_t pk_alg )
176{
Philippe Antoineb5b25432018-05-11 11:06:29 +0200177 if( pk_alg == MBEDTLS_PK_NONE )
178 return( -1 );
179
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200180 if( ( profile->allowed_pks & MBEDTLS_X509_ID_FLAG( pk_alg ) ) != 0 )
181 return( 0 );
182
183 return( -1 );
184}
185
186/*
187 * Check key against profile
Manuel Pégourié-Gonnard3f816912017-10-26 10:24:16 +0200188 * Return 0 if pk is acceptable for this profile, -1 otherwise
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200189 */
190static int x509_profile_check_key( const mbedtls_x509_crt_profile *profile,
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200191 const mbedtls_pk_context *pk )
192{
Manuel Pégourié-Gonnard3f816912017-10-26 10:24:16 +0200193 const mbedtls_pk_type_t pk_alg = mbedtls_pk_get_type( pk );
Manuel Pégourié-Gonnard19773ff2017-10-24 10:51:26 +0200194
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200195#if defined(MBEDTLS_RSA_C)
196 if( pk_alg == MBEDTLS_PK_RSA || pk_alg == MBEDTLS_PK_RSASSA_PSS )
197 {
Manuel Pégourié-Gonnard097c7bb2015-06-18 16:43:38 +0200198 if( mbedtls_pk_get_bitlen( pk ) >= profile->rsa_min_bitlen )
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200199 return( 0 );
200
201 return( -1 );
202 }
203#endif
204
Manuel Pégourié-Gonnard65eefc82015-10-23 14:08:48 +0200205#if defined(MBEDTLS_ECP_C)
206 if( pk_alg == MBEDTLS_PK_ECDSA ||
207 pk_alg == MBEDTLS_PK_ECKEY ||
208 pk_alg == MBEDTLS_PK_ECKEY_DH )
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200209 {
Manuel Pégourié-Gonnard3f816912017-10-26 10:24:16 +0200210 const mbedtls_ecp_group_id gid = mbedtls_pk_ec( *pk )->grp.id;
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200211
Philippe Antoineb5b25432018-05-11 11:06:29 +0200212 if( gid == MBEDTLS_ECP_DP_NONE )
213 return( -1 );
214
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200215 if( ( profile->allowed_curves & MBEDTLS_X509_ID_FLAG( gid ) ) != 0 )
216 return( 0 );
217
218 return( -1 );
219 }
220#endif
221
222 return( -1 );
223}
224
225/*
Hanno Becker1f8527f2018-11-02 09:19:16 +0000226 * Like memcmp, but case-insensitive and always returns -1 if different
227 */
228static int x509_memcasecmp( const void *s1, const void *s2, size_t len )
229{
230 size_t i;
231 unsigned char diff;
232 const unsigned char *n1 = s1, *n2 = s2;
233
234 for( i = 0; i < len; i++ )
235 {
236 diff = n1[i] ^ n2[i];
237
238 if( diff == 0 )
239 continue;
240
241 if( diff == 32 &&
242 ( ( n1[i] >= 'a' && n1[i] <= 'z' ) ||
243 ( n1[i] >= 'A' && n1[i] <= 'Z' ) ) )
244 {
245 continue;
246 }
247
248 return( -1 );
249 }
250
251 return( 0 );
252}
253
254/*
255 * Return 0 if name matches wildcard, -1 otherwise
256 */
257static int x509_check_wildcard( const char *cn, const mbedtls_x509_buf *name )
258{
259 size_t i;
260 size_t cn_idx = 0, cn_len = strlen( cn );
261
262 /* We can't have a match if there is no wildcard to match */
263 if( name->len < 3 || name->p[0] != '*' || name->p[1] != '.' )
264 return( -1 );
265
266 for( i = 0; i < cn_len; ++i )
267 {
268 if( cn[i] == '.' )
269 {
270 cn_idx = i;
271 break;
272 }
273 }
274
275 if( cn_idx == 0 )
276 return( -1 );
277
278 if( cn_len - cn_idx == name->len - 1 &&
279 x509_memcasecmp( name->p + 1, cn + cn_idx, name->len - 1 ) == 0 )
280 {
281 return( 0 );
282 }
283
284 return( -1 );
285}
286
287/*
288 * Compare two X.509 strings, case-insensitive, and allowing for some encoding
289 * variations (but not all).
290 *
291 * Return 0 if equal, -1 otherwise.
292 */
293static int x509_string_cmp( const mbedtls_x509_buf *a, const mbedtls_x509_buf *b )
294{
295 if( a->tag == b->tag &&
296 a->len == b->len &&
297 memcmp( a->p, b->p, b->len ) == 0 )
298 {
299 return( 0 );
300 }
301
302 if( ( a->tag == MBEDTLS_ASN1_UTF8_STRING || a->tag == MBEDTLS_ASN1_PRINTABLE_STRING ) &&
303 ( b->tag == MBEDTLS_ASN1_UTF8_STRING || b->tag == MBEDTLS_ASN1_PRINTABLE_STRING ) &&
304 a->len == b->len &&
305 x509_memcasecmp( a->p, b->p, b->len ) == 0 )
306 {
307 return( 0 );
308 }
309
310 return( -1 );
311}
312
313/*
314 * Compare two X.509 Names (aka rdnSequence).
315 *
316 * See RFC 5280 section 7.1, though we don't implement the whole algorithm:
317 * we sometimes return unequal when the full algorithm would return equal,
318 * but never the other way. (In particular, we don't do Unicode normalisation
319 * or space folding.)
320 *
321 * Return 0 if equal, -1 otherwise.
322 */
323static int x509_name_cmp( const mbedtls_x509_name *a, const mbedtls_x509_name *b )
324{
325 /* Avoid recursion, it might not be optimised by the compiler */
326 while( a != NULL || b != NULL )
327 {
328 if( a == NULL || b == NULL )
329 return( -1 );
330
331 /* type */
332 if( a->oid.tag != b->oid.tag ||
333 a->oid.len != b->oid.len ||
334 memcmp( a->oid.p, b->oid.p, b->oid.len ) != 0 )
335 {
336 return( -1 );
337 }
338
339 /* value */
340 if( x509_string_cmp( &a->val, &b->val ) != 0 )
341 return( -1 );
342
343 /* structure of the list of sets */
344 if( a->next_merged != b->next_merged )
345 return( -1 );
346
347 a = a->next;
348 b = b->next;
349 }
350
351 /* a == NULL == b */
352 return( 0 );
353}
354
355/*
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +0200356 * Reset (init or clear) a verify_chain
357 */
358static void x509_crt_verify_chain_reset(
359 mbedtls_x509_crt_verify_chain *ver_chain )
360{
361 size_t i;
362
363 for( i = 0; i < MBEDTLS_X509_MAX_VERIFY_CHAIN_SIZE; i++ )
364 {
365 ver_chain->items[i].crt = NULL;
Hanno Beckera9375b32019-01-10 09:19:26 +0000366 ver_chain->items[i].flags = (uint32_t) -1;
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +0200367 }
368
369 ver_chain->len = 0;
Hanno Beckerf53893b2019-03-28 13:45:55 +0000370
371#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
372 ver_chain->trust_ca_cb_result = NULL;
373#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +0200374}
375
376/*
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200377 * Version ::= INTEGER { v1(0), v2(1), v3(2) }
378 */
379static int x509_get_version( unsigned char **p,
380 const unsigned char *end,
381 int *ver )
382{
Janos Follath865b3eb2019-12-16 11:46:15 +0000383 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200384 size_t len;
385
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200386 if( ( ret = mbedtls_asn1_get_tag( p, end, &len,
387 MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_CONSTRUCTED | 0 ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200388 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200389 if( ret == MBEDTLS_ERR_ASN1_UNEXPECTED_TAG )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200390 {
391 *ver = 0;
392 return( 0 );
393 }
394
Chris Jones9f7a6932021-04-14 12:12:09 +0100395 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_FORMAT, ret ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200396 }
397
398 end = *p + len;
399
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200400 if( ( ret = mbedtls_asn1_get_int( p, end, ver ) ) != 0 )
Chris Jones9f7a6932021-04-14 12:12:09 +0100401 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_VERSION, ret ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200402
403 if( *p != end )
Chris Jones9f7a6932021-04-14 12:12:09 +0100404 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_VERSION,
405 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200406
407 return( 0 );
408}
409
410/*
411 * Validity ::= SEQUENCE {
412 * notBefore Time,
413 * notAfter Time }
414 */
415static int x509_get_dates( unsigned char **p,
416 const unsigned char *end,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200417 mbedtls_x509_time *from,
418 mbedtls_x509_time *to )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200419{
Janos Follath865b3eb2019-12-16 11:46:15 +0000420 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200421 size_t len;
422
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200423 if( ( ret = mbedtls_asn1_get_tag( p, end, &len,
424 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
Chris Jones9f7a6932021-04-14 12:12:09 +0100425 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_DATE, ret ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200426
427 end = *p + len;
428
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200429 if( ( ret = mbedtls_x509_get_time( p, end, from ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200430 return( ret );
431
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200432 if( ( ret = mbedtls_x509_get_time( p, end, to ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200433 return( ret );
434
435 if( *p != end )
Chris Jones9f7a6932021-04-14 12:12:09 +0100436 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_DATE,
437 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200438
439 return( 0 );
440}
441
442/*
443 * X.509 v2/v3 unique identifier (not parsed)
444 */
445static int x509_get_uid( unsigned char **p,
446 const unsigned char *end,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200447 mbedtls_x509_buf *uid, int n )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200448{
Janos Follath865b3eb2019-12-16 11:46:15 +0000449 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200450
451 if( *p == end )
452 return( 0 );
453
454 uid->tag = **p;
455
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200456 if( ( ret = mbedtls_asn1_get_tag( p, end, &uid->len,
457 MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_CONSTRUCTED | n ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200458 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200459 if( ret == MBEDTLS_ERR_ASN1_UNEXPECTED_TAG )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200460 return( 0 );
461
Chris Jones9f7a6932021-04-14 12:12:09 +0100462 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_FORMAT, ret ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200463 }
464
465 uid->p = *p;
466 *p += uid->len;
467
468 return( 0 );
469}
470
471static int x509_get_basic_constraints( unsigned char **p,
472 const unsigned char *end,
473 int *ca_istrue,
474 int *max_pathlen )
475{
Janos Follath865b3eb2019-12-16 11:46:15 +0000476 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200477 size_t len;
478
479 /*
480 * BasicConstraints ::= SEQUENCE {
481 * cA BOOLEAN DEFAULT FALSE,
482 * pathLenConstraint INTEGER (0..MAX) OPTIONAL }
483 */
484 *ca_istrue = 0; /* DEFAULT FALSE */
485 *max_pathlen = 0; /* endless */
486
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200487 if( ( ret = mbedtls_asn1_get_tag( p, end, &len,
488 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
Chris Jones9f7a6932021-04-14 12:12:09 +0100489 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200490
491 if( *p == end )
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200492 return( 0 );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200493
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200494 if( ( ret = mbedtls_asn1_get_bool( p, end, ca_istrue ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200495 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200496 if( ret == MBEDTLS_ERR_ASN1_UNEXPECTED_TAG )
497 ret = mbedtls_asn1_get_int( p, end, ca_istrue );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200498
499 if( ret != 0 )
Chris Jones9f7a6932021-04-14 12:12:09 +0100500 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200501
502 if( *ca_istrue != 0 )
503 *ca_istrue = 1;
504 }
505
506 if( *p == end )
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200507 return( 0 );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200508
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200509 if( ( ret = mbedtls_asn1_get_int( p, end, max_pathlen ) ) != 0 )
Chris Jones9f7a6932021-04-14 12:12:09 +0100510 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200511
512 if( *p != end )
Chris Jones9f7a6932021-04-14 12:12:09 +0100513 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
514 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200515
Andrzej Kurek16050742020-04-14 09:49:52 -0400516 /* Do not accept max_pathlen equal to INT_MAX to avoid a signed integer
517 * overflow, which is an undefined behavior. */
518 if( *max_pathlen == INT_MAX )
Chris Jones9f7a6932021-04-14 12:12:09 +0100519 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
520 MBEDTLS_ERR_ASN1_INVALID_LENGTH ) );
Andrzej Kurek16050742020-04-14 09:49:52 -0400521
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200522 (*max_pathlen)++;
523
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200524 return( 0 );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200525}
526
527static int x509_get_ns_cert_type( unsigned char **p,
528 const unsigned char *end,
529 unsigned char *ns_cert_type)
530{
Janos Follath865b3eb2019-12-16 11:46:15 +0000531 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200532 mbedtls_x509_bitstring bs = { 0, 0, NULL };
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200533
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200534 if( ( ret = mbedtls_asn1_get_bitstring( p, end, &bs ) ) != 0 )
Chris Jones9f7a6932021-04-14 12:12:09 +0100535 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200536
537 if( bs.len != 1 )
Chris Jones9f7a6932021-04-14 12:12:09 +0100538 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
539 MBEDTLS_ERR_ASN1_INVALID_LENGTH ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200540
541 /* Get actual bitstring */
542 *ns_cert_type = *bs.p;
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200543 return( 0 );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200544}
545
546static int x509_get_key_usage( unsigned char **p,
547 const unsigned char *end,
Manuel Pégourié-Gonnard1d0ca1a2015-03-27 16:50:00 +0100548 unsigned int *key_usage)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200549{
Janos Follath865b3eb2019-12-16 11:46:15 +0000550 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard9a702252015-06-23 10:14:36 +0200551 size_t i;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200552 mbedtls_x509_bitstring bs = { 0, 0, NULL };
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200553
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200554 if( ( ret = mbedtls_asn1_get_bitstring( p, end, &bs ) ) != 0 )
Chris Jones9f7a6932021-04-14 12:12:09 +0100555 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200556
557 if( bs.len < 1 )
Chris Jones9f7a6932021-04-14 12:12:09 +0100558 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
559 MBEDTLS_ERR_ASN1_INVALID_LENGTH ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200560
561 /* Get actual bitstring */
Manuel Pégourié-Gonnard9a702252015-06-23 10:14:36 +0200562 *key_usage = 0;
563 for( i = 0; i < bs.len && i < sizeof( unsigned int ); i++ )
564 {
565 *key_usage |= (unsigned int) bs.p[i] << (8*i);
566 }
567
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200568 return( 0 );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200569}
570
571/*
572 * ExtKeyUsageSyntax ::= SEQUENCE SIZE (1..MAX) OF KeyPurposeId
573 *
574 * KeyPurposeId ::= OBJECT IDENTIFIER
575 */
576static int x509_get_ext_key_usage( unsigned char **p,
577 const unsigned char *end,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200578 mbedtls_x509_sequence *ext_key_usage)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200579{
Janos Follath865b3eb2019-12-16 11:46:15 +0000580 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200581
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200582 if( ( ret = mbedtls_asn1_get_sequence_of( p, end, ext_key_usage, MBEDTLS_ASN1_OID ) ) != 0 )
Chris Jones9f7a6932021-04-14 12:12:09 +0100583 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200584
585 /* Sequence length must be >= 1 */
586 if( ext_key_usage->buf.p == NULL )
Chris Jones9f7a6932021-04-14 12:12:09 +0100587 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
588 MBEDTLS_ERR_ASN1_INVALID_LENGTH ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200589
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200590 return( 0 );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200591}
592
593/*
594 * SubjectAltName ::= GeneralNames
595 *
596 * GeneralNames ::= SEQUENCE SIZE (1..MAX) OF GeneralName
597 *
598 * GeneralName ::= CHOICE {
599 * otherName [0] OtherName,
600 * rfc822Name [1] IA5String,
601 * dNSName [2] IA5String,
602 * x400Address [3] ORAddress,
603 * directoryName [4] Name,
604 * ediPartyName [5] EDIPartyName,
605 * uniformResourceIdentifier [6] IA5String,
606 * iPAddress [7] OCTET STRING,
607 * registeredID [8] OBJECT IDENTIFIER }
608 *
609 * OtherName ::= SEQUENCE {
610 * type-id OBJECT IDENTIFIER,
611 * value [0] EXPLICIT ANY DEFINED BY type-id }
612 *
613 * EDIPartyName ::= SEQUENCE {
614 * nameAssigner [0] DirectoryString OPTIONAL,
615 * partyName [1] DirectoryString }
616 *
Ron Eldorc8b5f3f2019-05-15 15:15:55 +0300617 * NOTE: we list all types, but only use dNSName and otherName
618 * of type HwModuleName, as defined in RFC 4108, at this point.
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200619 */
620static int x509_get_subject_alt_name( unsigned char **p,
621 const unsigned char *end,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200622 mbedtls_x509_sequence *subject_alt_name )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200623{
Janos Follath865b3eb2019-12-16 11:46:15 +0000624 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200625 size_t len, tag_len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200626 mbedtls_asn1_buf *buf;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200627 unsigned char tag;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200628 mbedtls_asn1_sequence *cur = subject_alt_name;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200629
630 /* Get main sequence tag */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200631 if( ( ret = mbedtls_asn1_get_tag( p, end, &len,
632 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
Chris Jones9f7a6932021-04-14 12:12:09 +0100633 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200634
635 if( *p + len != end )
Chris Jones9f7a6932021-04-14 12:12:09 +0100636 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
637 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200638
639 while( *p < end )
640 {
Ron Eldordbbd9662019-05-15 15:46:03 +0300641 mbedtls_x509_subject_alternative_name dummy_san_buf;
642 memset( &dummy_san_buf, 0, sizeof( dummy_san_buf ) );
643
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200644 tag = **p;
645 (*p)++;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200646 if( ( ret = mbedtls_asn1_get_len( p, end, &tag_len ) ) != 0 )
Chris Jones9f7a6932021-04-14 12:12:09 +0100647 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200648
Andres Amaya Garcia849bc652017-08-25 17:13:12 +0100649 if( ( tag & MBEDTLS_ASN1_TAG_CLASS_MASK ) !=
650 MBEDTLS_ASN1_CONTEXT_SPECIFIC )
651 {
Chris Jones9f7a6932021-04-14 12:12:09 +0100652 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
653 MBEDTLS_ERR_ASN1_UNEXPECTED_TAG ) );
Andres Amaya Garcia849bc652017-08-25 17:13:12 +0100654 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200655
Ron Eldordbbd9662019-05-15 15:46:03 +0300656 /*
irwir74aee1c2019-09-21 18:21:48 +0300657 * Check that the SAN is structured correctly.
Ron Eldordbbd9662019-05-15 15:46:03 +0300658 */
659 ret = mbedtls_x509_parse_subject_alt_name( &(cur->buf), &dummy_san_buf );
660 /*
661 * In case the extension is malformed, return an error,
662 * and clear the allocated sequences.
663 */
664 if( ret != 0 && ret != MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE )
665 {
666 mbedtls_x509_sequence *seq_cur = subject_alt_name->next;
667 mbedtls_x509_sequence *seq_prv;
668 while( seq_cur != NULL )
669 {
670 seq_prv = seq_cur;
671 seq_cur = seq_cur->next;
672 mbedtls_platform_zeroize( seq_prv,
673 sizeof( mbedtls_x509_sequence ) );
674 mbedtls_free( seq_prv );
675 }
Ron Eldor5aebeeb2019-05-22 16:41:21 +0300676 subject_alt_name->next = NULL;
Ron Eldordbbd9662019-05-15 15:46:03 +0300677 return( ret );
678 }
679
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200680 /* Allocate and assign next pointer */
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +0200681 if( cur->buf.p != NULL )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200682 {
Manuel Pégourié-Gonnardb1340602014-11-11 23:11:16 +0100683 if( cur->next != NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200684 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS );
Manuel Pégourié-Gonnardb1340602014-11-11 23:11:16 +0100685
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200686 cur->next = mbedtls_calloc( 1, sizeof( mbedtls_asn1_sequence ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200687
688 if( cur->next == NULL )
Chris Jones9f7a6932021-04-14 12:12:09 +0100689 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
690 MBEDTLS_ERR_ASN1_ALLOC_FAILED ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200691
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200692 cur = cur->next;
693 }
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +0200694
695 buf = &(cur->buf);
696 buf->tag = tag;
697 buf->p = *p;
698 buf->len = tag_len;
699 *p += buf->len;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200700 }
701
702 /* Set final sequence entry's next pointer to NULL */
703 cur->next = NULL;
704
705 if( *p != end )
Chris Jones9f7a6932021-04-14 12:12:09 +0100706 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
707 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200708
709 return( 0 );
710}
711
712/*
Ron Eldor74d9acc2019-03-21 14:00:03 +0200713 * id-ce-certificatePolicies OBJECT IDENTIFIER ::= { id-ce 32 }
714 *
715 * anyPolicy OBJECT IDENTIFIER ::= { id-ce-certificatePolicies 0 }
716 *
717 * certificatePolicies ::= SEQUENCE SIZE (1..MAX) OF PolicyInformation
718 *
719 * PolicyInformation ::= SEQUENCE {
720 * policyIdentifier CertPolicyId,
721 * policyQualifiers SEQUENCE SIZE (1..MAX) OF
722 * PolicyQualifierInfo OPTIONAL }
723 *
724 * CertPolicyId ::= OBJECT IDENTIFIER
725 *
726 * PolicyQualifierInfo ::= SEQUENCE {
727 * policyQualifierId PolicyQualifierId,
728 * qualifier ANY DEFINED BY policyQualifierId }
729 *
730 * -- policyQualifierIds for Internet policy qualifiers
731 *
732 * id-qt OBJECT IDENTIFIER ::= { id-pkix 2 }
733 * id-qt-cps OBJECT IDENTIFIER ::= { id-qt 1 }
734 * id-qt-unotice OBJECT IDENTIFIER ::= { id-qt 2 }
735 *
736 * PolicyQualifierId ::= OBJECT IDENTIFIER ( id-qt-cps | id-qt-unotice )
737 *
738 * Qualifier ::= CHOICE {
739 * cPSuri CPSuri,
740 * userNotice UserNotice }
741 *
742 * CPSuri ::= IA5String
743 *
744 * UserNotice ::= SEQUENCE {
745 * noticeRef NoticeReference OPTIONAL,
746 * explicitText DisplayText OPTIONAL }
747 *
748 * NoticeReference ::= SEQUENCE {
749 * organization DisplayText,
750 * noticeNumbers SEQUENCE OF INTEGER }
751 *
752 * DisplayText ::= CHOICE {
753 * ia5String IA5String (SIZE (1..200)),
754 * visibleString VisibleString (SIZE (1..200)),
755 * bmpString BMPString (SIZE (1..200)),
756 * utf8String UTF8String (SIZE (1..200)) }
757 *
758 * NOTE: we only parse and use anyPolicy without qualifiers at this point
759 * as defined in RFC 5280.
760 */
761static int x509_get_certificate_policies( unsigned char **p,
762 const unsigned char *end,
763 mbedtls_x509_sequence *certificate_policies )
764{
Ron Eldor8b0c3c92019-05-15 12:20:00 +0300765 int ret, parse_ret = 0;
Ron Eldor74d9acc2019-03-21 14:00:03 +0200766 size_t len;
767 mbedtls_asn1_buf *buf;
768 mbedtls_asn1_sequence *cur = certificate_policies;
769
770 /* Get main sequence tag */
771 ret = mbedtls_asn1_get_tag( p, end, &len,
772 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE );
773 if( ret != 0 )
Chris Jones9f7a6932021-04-14 12:12:09 +0100774 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret ) );
Ron Eldor74d9acc2019-03-21 14:00:03 +0200775
776 if( *p + len != end )
Chris Jones9f7a6932021-04-14 12:12:09 +0100777 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
778 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH ) );
Ron Eldor74d9acc2019-03-21 14:00:03 +0200779
780 /*
781 * Cannot be an empty sequence.
782 */
783 if( len == 0 )
Chris Jones9f7a6932021-04-14 12:12:09 +0100784 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
785 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH ) );
Ron Eldor74d9acc2019-03-21 14:00:03 +0200786
787 while( *p < end )
788 {
789 mbedtls_x509_buf policy_oid;
790 const unsigned char *policy_end;
791
792 /*
793 * Get the policy sequence
794 */
795 if( ( ret = mbedtls_asn1_get_tag( p, end, &len,
796 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
Chris Jones9f7a6932021-04-14 12:12:09 +0100797 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret ) );
Ron Eldor74d9acc2019-03-21 14:00:03 +0200798
799 policy_end = *p + len;
800
Ron Eldor08063792019-05-13 16:38:39 +0300801 if( ( ret = mbedtls_asn1_get_tag( p, policy_end, &len,
Ron Eldor74d9acc2019-03-21 14:00:03 +0200802 MBEDTLS_ASN1_OID ) ) != 0 )
Chris Jones9f7a6932021-04-14 12:12:09 +0100803 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret ) );
Ron Eldor74d9acc2019-03-21 14:00:03 +0200804
805 policy_oid.tag = MBEDTLS_ASN1_OID;
806 policy_oid.len = len;
807 policy_oid.p = *p;
808
Ron Eldor8b0c3c92019-05-15 12:20:00 +0300809 /*
810 * Only AnyPolicy is currently supported when enforcing policy.
811 */
812 if( MBEDTLS_OID_CMP( MBEDTLS_OID_ANY_POLICY, &policy_oid ) != 0 )
813 {
814 /*
815 * Set the parsing return code but continue parsing, in case this
816 * extension is critical and MBEDTLS_X509_ALLOW_UNSUPPORTED_CRITICAL_EXTENSION
817 * is configured.
818 */
819 parse_ret = MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE;
820 }
821
Ron Eldor74d9acc2019-03-21 14:00:03 +0200822 /* Allocate and assign next pointer */
823 if( cur->buf.p != NULL )
824 {
825 if( cur->next != NULL )
826 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS );
827
828 cur->next = mbedtls_calloc( 1, sizeof( mbedtls_asn1_sequence ) );
829
830 if( cur->next == NULL )
Chris Jones9f7a6932021-04-14 12:12:09 +0100831 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
832 MBEDTLS_ERR_ASN1_ALLOC_FAILED ) );
Ron Eldor74d9acc2019-03-21 14:00:03 +0200833
834 cur = cur->next;
835 }
836
837 buf = &( cur->buf );
838 buf->tag = policy_oid.tag;
839 buf->p = policy_oid.p;
840 buf->len = policy_oid.len;
Ron Eldor08063792019-05-13 16:38:39 +0300841
842 *p += len;
843
844 /*
845 * If there is an optional qualifier, then *p < policy_end
846 * Check the Qualifier len to verify it doesn't exceed policy_end.
847 */
848 if( *p < policy_end )
849 {
850 if( ( ret = mbedtls_asn1_get_tag( p, policy_end, &len,
851 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
Chris Jones9f7a6932021-04-14 12:12:09 +0100852 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret ) );
Ron Eldor08063792019-05-13 16:38:39 +0300853 /*
854 * Skip the optional policy qualifiers.
855 */
856 *p += len;
857 }
858
859 if( *p != policy_end )
Chris Jones9f7a6932021-04-14 12:12:09 +0100860 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
861 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH ) );
Ron Eldor74d9acc2019-03-21 14:00:03 +0200862 }
863
864 /* Set final sequence entry's next pointer to NULL */
865 cur->next = NULL;
866
867 if( *p != end )
Chris Jones9f7a6932021-04-14 12:12:09 +0100868 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
869 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH ) );
Ron Eldor74d9acc2019-03-21 14:00:03 +0200870
Ron Eldor8b0c3c92019-05-15 12:20:00 +0300871 return( parse_ret );
Ron Eldor74d9acc2019-03-21 14:00:03 +0200872}
873
874/*
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200875 * X.509 v3 extensions
876 *
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200877 */
878static int x509_get_crt_ext( unsigned char **p,
879 const unsigned char *end,
Nicola Di Lieto502d4b42020-04-25 14:41:25 +0200880 mbedtls_x509_crt *crt,
Nicola Di Lieto5659e7e2020-05-28 23:41:38 +0200881 mbedtls_x509_crt_ext_cb_t cb,
882 void *p_ctx )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200883{
Janos Follath865b3eb2019-12-16 11:46:15 +0000884 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200885 size_t len;
Nicola Di Lietoc84b1e62020-06-13 11:08:16 +0200886 unsigned char *end_ext_data, *start_ext_octet, *end_ext_octet;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200887
Hanno Becker12f62fb2019-02-12 17:22:36 +0000888 if( *p == end )
889 return( 0 );
890
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200891 if( ( ret = mbedtls_x509_get_ext( p, end, &crt->v3_ext, 3 ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200892 return( ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200893
Hanno Becker12f62fb2019-02-12 17:22:36 +0000894 end = crt->v3_ext.p + crt->v3_ext.len;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200895 while( *p < end )
896 {
897 /*
898 * Extension ::= SEQUENCE {
899 * extnID OBJECT IDENTIFIER,
900 * critical BOOLEAN DEFAULT FALSE,
901 * extnValue OCTET STRING }
902 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200903 mbedtls_x509_buf extn_oid = {0, 0, NULL};
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200904 int is_critical = 0; /* DEFAULT FALSE */
905 int ext_type = 0;
906
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200907 if( ( ret = mbedtls_asn1_get_tag( p, end, &len,
908 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
Chris Jones9f7a6932021-04-14 12:12:09 +0100909 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200910
911 end_ext_data = *p + len;
912
913 /* Get extension ID */
k-stachowiakdcae78a2018-06-28 16:32:54 +0200914 if( ( ret = mbedtls_asn1_get_tag( p, end_ext_data, &extn_oid.len,
k-stachowiak463928a2018-07-24 12:50:59 +0200915 MBEDTLS_ASN1_OID ) ) != 0 )
Chris Jones9f7a6932021-04-14 12:12:09 +0100916 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200917
k-stachowiak463928a2018-07-24 12:50:59 +0200918 extn_oid.tag = MBEDTLS_ASN1_OID;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200919 extn_oid.p = *p;
920 *p += extn_oid.len;
921
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200922 /* Get optional critical */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200923 if( ( ret = mbedtls_asn1_get_bool( p, end_ext_data, &is_critical ) ) != 0 &&
924 ( ret != MBEDTLS_ERR_ASN1_UNEXPECTED_TAG ) )
Chris Jones9f7a6932021-04-14 12:12:09 +0100925 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200926
927 /* Data should be octet string type */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200928 if( ( ret = mbedtls_asn1_get_tag( p, end_ext_data, &len,
929 MBEDTLS_ASN1_OCTET_STRING ) ) != 0 )
Chris Jones9f7a6932021-04-14 12:12:09 +0100930 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200931
Nicola Di Lietoc84b1e62020-06-13 11:08:16 +0200932 start_ext_octet = *p;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200933 end_ext_octet = *p + len;
934
935 if( end_ext_octet != end_ext_data )
Chris Jones9f7a6932021-04-14 12:12:09 +0100936 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
937 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200938
939 /*
940 * Detect supported extensions
941 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200942 ret = mbedtls_oid_get_x509_ext_type( &extn_oid, &ext_type );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200943
944 if( ret != 0 )
945 {
Nicola Di Lieto502d4b42020-04-25 14:41:25 +0200946 /* Give the callback (if any) a chance to handle the extension */
Nicola Di Lieto2c3a9172020-05-28 17:20:42 +0200947 if( cb != NULL )
948 {
Nicola Di Lieto5659e7e2020-05-28 23:41:38 +0200949 ret = cb( p_ctx, crt, &extn_oid, is_critical, *p, end_ext_octet );
Nicola Di Lieto565b52b2020-05-29 22:46:56 +0200950 if( ret != 0 && is_critical )
951 return( ret );
Nicola Di Lietofae25a12020-05-28 08:55:08 +0200952 *p = end_ext_octet;
Nicola Di Lieto502d4b42020-04-25 14:41:25 +0200953 continue;
Nicola Di Lietofae25a12020-05-28 08:55:08 +0200954 }
Nicola Di Lieto502d4b42020-04-25 14:41:25 +0200955
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200956 /* No parser found, skip extension */
957 *p = end_ext_octet;
958
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200959#if !defined(MBEDTLS_X509_ALLOW_UNSUPPORTED_CRITICAL_EXTENSION)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200960 if( is_critical )
961 {
962 /* Data is marked as critical: fail */
Chris Jones9f7a6932021-04-14 12:12:09 +0100963 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
964 MBEDTLS_ERR_ASN1_UNEXPECTED_TAG ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200965 }
966#endif
967 continue;
968 }
969
Manuel Pégourié-Gonnard8a5e3d42014-11-12 17:47:28 +0100970 /* Forbid repeated extensions */
971 if( ( crt->ext_types & ext_type ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200972 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS );
Manuel Pégourié-Gonnard8a5e3d42014-11-12 17:47:28 +0100973
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200974 crt->ext_types |= ext_type;
975
976 switch( ext_type )
977 {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +0100978 case MBEDTLS_X509_EXT_BASIC_CONSTRAINTS:
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200979 /* Parse basic constraints */
980 if( ( ret = x509_get_basic_constraints( p, end_ext_octet,
981 &crt->ca_istrue, &crt->max_pathlen ) ) != 0 )
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200982 return( ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200983 break;
984
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200985 case MBEDTLS_X509_EXT_KEY_USAGE:
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200986 /* Parse key usage */
987 if( ( ret = x509_get_key_usage( p, end_ext_octet,
988 &crt->key_usage ) ) != 0 )
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200989 return( ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200990 break;
991
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200992 case MBEDTLS_X509_EXT_EXTENDED_KEY_USAGE:
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200993 /* Parse extended key usage */
994 if( ( ret = x509_get_ext_key_usage( p, end_ext_octet,
995 &crt->ext_key_usage ) ) != 0 )
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200996 return( ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200997 break;
998
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +0100999 case MBEDTLS_X509_EXT_SUBJECT_ALT_NAME:
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001000 /* Parse subject alt name */
1001 if( ( ret = x509_get_subject_alt_name( p, end_ext_octet,
1002 &crt->subject_alt_names ) ) != 0 )
Paul Bakkerd8bb8262014-06-17 14:06:49 +02001003 return( ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001004 break;
1005
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001006 case MBEDTLS_X509_EXT_NS_CERT_TYPE:
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001007 /* Parse netscape certificate type */
1008 if( ( ret = x509_get_ns_cert_type( p, end_ext_octet,
1009 &crt->ns_cert_type ) ) != 0 )
Paul Bakkerd8bb8262014-06-17 14:06:49 +02001010 return( ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001011 break;
1012
Ron Eldor74d9acc2019-03-21 14:00:03 +02001013 case MBEDTLS_OID_X509_EXT_CERTIFICATE_POLICIES:
1014 /* Parse certificate policies type */
1015 if( ( ret = x509_get_certificate_policies( p, end_ext_octet,
1016 &crt->certificate_policies ) ) != 0 )
Ron Eldor8b0c3c92019-05-15 12:20:00 +03001017 {
Nicola Di Lietoc84b1e62020-06-13 11:08:16 +02001018 /* Give the callback (if any) a chance to handle the extension
1019 * if it contains unsupported policies */
1020 if( ret == MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE && cb != NULL &&
1021 cb( p_ctx, crt, &extn_oid, is_critical,
1022 start_ext_octet, end_ext_octet ) == 0 )
1023 break;
1024
Ron Eldor8b0c3c92019-05-15 12:20:00 +03001025#if !defined(MBEDTLS_X509_ALLOW_UNSUPPORTED_CRITICAL_EXTENSION)
1026 if( is_critical )
1027 return( ret );
1028 else
1029#endif
1030 /*
Ron Eldora2913912019-05-16 16:17:38 +03001031 * If MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE is returned, then we
1032 * cannot interpret or enforce the policy. However, it is up to
1033 * the user to choose how to enforce the policies,
Ron Eldor8b0c3c92019-05-15 12:20:00 +03001034 * unless the extension is critical.
1035 */
1036 if( ret != MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE )
1037 return( ret );
1038 }
Ron Eldor74d9acc2019-03-21 14:00:03 +02001039 break;
1040
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001041 default:
Ron Eldordf48efa2019-04-08 13:28:24 +03001042 /*
1043 * If this is a non-critical extension, which the oid layer
1044 * supports, but there isn't an x509 parser for it,
1045 * skip the extension.
1046 */
1047#if !defined(MBEDTLS_X509_ALLOW_UNSUPPORTED_CRITICAL_EXTENSION)
1048 if( is_critical )
1049 return( MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE );
1050 else
1051#endif
1052 *p = end_ext_octet;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001053 }
1054 }
1055
1056 if( *p != end )
Chris Jones9f7a6932021-04-14 12:12:09 +01001057 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
1058 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001059
1060 return( 0 );
1061}
1062
1063/*
1064 * Parse and fill a single X.509 certificate in DER format
1065 */
Hanno Becker1a65dcd2019-01-31 08:57:44 +00001066static int x509_crt_parse_der_core( mbedtls_x509_crt *crt,
1067 const unsigned char *buf,
1068 size_t buflen,
Nicola Di Lieto502d4b42020-04-25 14:41:25 +02001069 int make_copy,
Nicola Di Lieto5659e7e2020-05-28 23:41:38 +02001070 mbedtls_x509_crt_ext_cb_t cb,
1071 void *p_ctx )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001072{
Janos Follath865b3eb2019-12-16 11:46:15 +00001073 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001074 size_t len;
1075 unsigned char *p, *end, *crt_end;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001076 mbedtls_x509_buf sig_params1, sig_params2, sig_oid2;
Manuel Pégourié-Gonnard59a75d52014-01-22 10:12:57 +01001077
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001078 memset( &sig_params1, 0, sizeof( mbedtls_x509_buf ) );
1079 memset( &sig_params2, 0, sizeof( mbedtls_x509_buf ) );
1080 memset( &sig_oid2, 0, sizeof( mbedtls_x509_buf ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001081
1082 /*
1083 * Check for valid input
1084 */
1085 if( crt == NULL || buf == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001086 return( MBEDTLS_ERR_X509_BAD_INPUT_DATA );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001087
Hanno Becker1a65dcd2019-01-31 08:57:44 +00001088 /* Use the original buffer until we figure out actual length. */
Janos Follathcc0e49d2016-02-17 14:34:12 +00001089 p = (unsigned char*) buf;
1090 len = buflen;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001091 end = p + len;
1092
1093 /*
1094 * Certificate ::= SEQUENCE {
1095 * tbsCertificate TBSCertificate,
1096 * signatureAlgorithm AlgorithmIdentifier,
1097 * signatureValue BIT STRING }
1098 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001099 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
1100 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001101 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001102 mbedtls_x509_crt_free( crt );
1103 return( MBEDTLS_ERR_X509_INVALID_FORMAT );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001104 }
1105
Janos Follathcc0e49d2016-02-17 14:34:12 +00001106 end = crt_end = p + len;
Hanno Becker1a65dcd2019-01-31 08:57:44 +00001107 crt->raw.len = crt_end - buf;
1108 if( make_copy != 0 )
1109 {
1110 /* Create and populate a new buffer for the raw field. */
1111 crt->raw.p = p = mbedtls_calloc( 1, crt->raw.len );
1112 if( crt->raw.p == NULL )
1113 return( MBEDTLS_ERR_X509_ALLOC_FAILED );
1114
1115 memcpy( crt->raw.p, buf, crt->raw.len );
1116 crt->own_buffer = 1;
1117
1118 p += crt->raw.len - len;
1119 end = crt_end = p + len;
1120 }
1121 else
1122 {
1123 crt->raw.p = (unsigned char*) buf;
1124 crt->own_buffer = 0;
1125 }
Janos Follathcc0e49d2016-02-17 14:34:12 +00001126
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001127 /*
1128 * TBSCertificate ::= SEQUENCE {
1129 */
1130 crt->tbs.p = p;
1131
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001132 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
1133 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001134 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001135 mbedtls_x509_crt_free( crt );
Chris Jones9f7a6932021-04-14 12:12:09 +01001136 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_FORMAT, ret ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001137 }
1138
1139 end = p + len;
1140 crt->tbs.len = end - crt->tbs.p;
1141
1142 /*
1143 * Version ::= INTEGER { v1(0), v2(1), v3(2) }
1144 *
1145 * CertificateSerialNumber ::= INTEGER
1146 *
1147 * signature AlgorithmIdentifier
1148 */
1149 if( ( ret = x509_get_version( &p, end, &crt->version ) ) != 0 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001150 ( ret = mbedtls_x509_get_serial( &p, end, &crt->serial ) ) != 0 ||
1151 ( ret = mbedtls_x509_get_alg( &p, end, &crt->sig_oid,
Manuel Pégourié-Gonnarddddbb1d2014-06-05 17:02:24 +02001152 &sig_params1 ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001153 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001154 mbedtls_x509_crt_free( crt );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001155 return( ret );
1156 }
1157
Andres AG7ca4a032017-03-09 16:16:11 +00001158 if( crt->version < 0 || crt->version > 2 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001159 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001160 mbedtls_x509_crt_free( crt );
1161 return( MBEDTLS_ERR_X509_UNKNOWN_VERSION );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001162 }
1163
Andres AG7ca4a032017-03-09 16:16:11 +00001164 crt->version++;
1165
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001166 if( ( ret = mbedtls_x509_get_sig_alg( &crt->sig_oid, &sig_params1,
Manuel Pégourié-Gonnardf75f2f72014-06-05 15:14:28 +02001167 &crt->sig_md, &crt->sig_pk,
1168 &crt->sig_opts ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001169 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001170 mbedtls_x509_crt_free( crt );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001171 return( ret );
1172 }
1173
1174 /*
1175 * issuer Name
1176 */
1177 crt->issuer_raw.p = p;
1178
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001179 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
1180 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001181 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001182 mbedtls_x509_crt_free( crt );
Chris Jones9f7a6932021-04-14 12:12:09 +01001183 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_FORMAT, ret ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001184 }
1185
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001186 if( ( ret = mbedtls_x509_get_name( &p, p + len, &crt->issuer ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001187 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001188 mbedtls_x509_crt_free( crt );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001189 return( ret );
1190 }
1191
1192 crt->issuer_raw.len = p - crt->issuer_raw.p;
1193
1194 /*
1195 * Validity ::= SEQUENCE {
1196 * notBefore Time,
1197 * notAfter Time }
1198 *
1199 */
1200 if( ( ret = x509_get_dates( &p, end, &crt->valid_from,
1201 &crt->valid_to ) ) != 0 )
1202 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001203 mbedtls_x509_crt_free( crt );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001204 return( ret );
1205 }
1206
1207 /*
1208 * subject Name
1209 */
1210 crt->subject_raw.p = p;
1211
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001212 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
1213 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001214 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001215 mbedtls_x509_crt_free( crt );
Chris Jones9f7a6932021-04-14 12:12:09 +01001216 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_FORMAT, ret ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001217 }
1218
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001219 if( len && ( ret = mbedtls_x509_get_name( &p, p + len, &crt->subject ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001220 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001221 mbedtls_x509_crt_free( crt );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001222 return( ret );
1223 }
1224
1225 crt->subject_raw.len = p - crt->subject_raw.p;
1226
1227 /*
1228 * SubjectPublicKeyInfo
1229 */
Hanno Becker494dd7a2019-02-06 16:13:41 +00001230 crt->pk_raw.p = p;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001231 if( ( ret = mbedtls_pk_parse_subpubkey( &p, end, &crt->pk ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001232 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001233 mbedtls_x509_crt_free( crt );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001234 return( ret );
1235 }
Hanno Becker494dd7a2019-02-06 16:13:41 +00001236 crt->pk_raw.len = p - crt->pk_raw.p;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001237
1238 /*
1239 * issuerUniqueID [1] IMPLICIT UniqueIdentifier OPTIONAL,
1240 * -- If present, version shall be v2 or v3
1241 * subjectUniqueID [2] IMPLICIT UniqueIdentifier OPTIONAL,
1242 * -- If present, version shall be v2 or v3
1243 * extensions [3] EXPLICIT Extensions OPTIONAL
1244 * -- If present, version shall be v3
1245 */
1246 if( crt->version == 2 || crt->version == 3 )
1247 {
1248 ret = x509_get_uid( &p, end, &crt->issuer_id, 1 );
1249 if( ret != 0 )
1250 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001251 mbedtls_x509_crt_free( crt );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001252 return( ret );
1253 }
1254 }
1255
1256 if( crt->version == 2 || crt->version == 3 )
1257 {
1258 ret = x509_get_uid( &p, end, &crt->subject_id, 2 );
1259 if( ret != 0 )
1260 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001261 mbedtls_x509_crt_free( crt );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001262 return( ret );
1263 }
1264 }
1265
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001266#if !defined(MBEDTLS_X509_ALLOW_EXTENSIONS_NON_V3)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001267 if( crt->version == 3 )
Paul Bakkerc27c4e22013-09-23 15:01:36 +02001268#endif
Manuel Pégourié-Gonnarda252af72015-03-27 16:15:55 +01001269 {
Nicola Di Lieto5659e7e2020-05-28 23:41:38 +02001270 ret = x509_get_crt_ext( &p, end, crt, cb, p_ctx );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001271 if( ret != 0 )
1272 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001273 mbedtls_x509_crt_free( crt );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001274 return( ret );
1275 }
1276 }
1277
1278 if( p != end )
1279 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001280 mbedtls_x509_crt_free( crt );
Chris Jones9f7a6932021-04-14 12:12:09 +01001281 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_FORMAT,
1282 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001283 }
1284
1285 end = crt_end;
1286
1287 /*
1288 * }
1289 * -- end of TBSCertificate
1290 *
1291 * signatureAlgorithm AlgorithmIdentifier,
1292 * signatureValue BIT STRING
1293 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001294 if( ( ret = mbedtls_x509_get_alg( &p, end, &sig_oid2, &sig_params2 ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001295 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001296 mbedtls_x509_crt_free( crt );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001297 return( ret );
1298 }
1299
Manuel Pégourié-Gonnard1022fed2015-03-27 16:30:47 +01001300 if( crt->sig_oid.len != sig_oid2.len ||
1301 memcmp( crt->sig_oid.p, sig_oid2.p, crt->sig_oid.len ) != 0 ||
Paul Elliottca17ebf2020-11-24 17:30:18 +00001302 sig_params1.tag != sig_params2.tag ||
Manuel Pégourié-Gonnarddddbb1d2014-06-05 17:02:24 +02001303 sig_params1.len != sig_params2.len ||
Manuel Pégourié-Gonnard159c5242015-04-30 11:15:22 +02001304 ( sig_params1.len != 0 &&
1305 memcmp( sig_params1.p, sig_params2.p, sig_params1.len ) != 0 ) )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001306 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001307 mbedtls_x509_crt_free( crt );
1308 return( MBEDTLS_ERR_X509_SIG_MISMATCH );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001309 }
1310
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001311 if( ( ret = mbedtls_x509_get_sig( &p, end, &crt->sig ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001312 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001313 mbedtls_x509_crt_free( crt );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001314 return( ret );
1315 }
1316
1317 if( p != end )
1318 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001319 mbedtls_x509_crt_free( crt );
Chris Jones9f7a6932021-04-14 12:12:09 +01001320 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_FORMAT,
1321 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001322 }
1323
1324 return( 0 );
1325}
1326
1327/*
1328 * Parse one X.509 certificate in DER format from a buffer and add them to a
1329 * chained list
1330 */
Hanno Becker1a65dcd2019-01-31 08:57:44 +00001331static int mbedtls_x509_crt_parse_der_internal( mbedtls_x509_crt *chain,
1332 const unsigned char *buf,
1333 size_t buflen,
Nicola Di Lieto502d4b42020-04-25 14:41:25 +02001334 int make_copy,
Nicola Di Lieto5659e7e2020-05-28 23:41:38 +02001335 mbedtls_x509_crt_ext_cb_t cb,
1336 void *p_ctx )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001337{
Janos Follath865b3eb2019-12-16 11:46:15 +00001338 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001339 mbedtls_x509_crt *crt = chain, *prev = NULL;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001340
1341 /*
1342 * Check for valid input
1343 */
1344 if( crt == NULL || buf == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001345 return( MBEDTLS_ERR_X509_BAD_INPUT_DATA );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001346
1347 while( crt->version != 0 && crt->next != NULL )
1348 {
1349 prev = crt;
1350 crt = crt->next;
1351 }
1352
1353 /*
1354 * Add new certificate on the end of the chain if needed.
1355 */
Paul Bakker66d5d072014-06-17 16:39:18 +02001356 if( crt->version != 0 && crt->next == NULL )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001357 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02001358 crt->next = mbedtls_calloc( 1, sizeof( mbedtls_x509_crt ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001359
1360 if( crt->next == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02001361 return( MBEDTLS_ERR_X509_ALLOC_FAILED );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001362
1363 prev = crt;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001364 mbedtls_x509_crt_init( crt->next );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001365 crt = crt->next;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001366 }
1367
Nicola Di Lieto5659e7e2020-05-28 23:41:38 +02001368 ret = x509_crt_parse_der_core( crt, buf, buflen, make_copy, cb, p_ctx );
Nicola Di Lieto502d4b42020-04-25 14:41:25 +02001369 if( ret != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001370 {
1371 if( prev )
1372 prev->next = NULL;
1373
1374 if( crt != chain )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001375 mbedtls_free( crt );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001376
1377 return( ret );
1378 }
1379
1380 return( 0 );
1381}
1382
Hanno Becker1a65dcd2019-01-31 08:57:44 +00001383int mbedtls_x509_crt_parse_der_nocopy( mbedtls_x509_crt *chain,
1384 const unsigned char *buf,
1385 size_t buflen )
1386{
Nicola Di Lieto5659e7e2020-05-28 23:41:38 +02001387 return( mbedtls_x509_crt_parse_der_internal( chain, buf, buflen, 0, NULL, NULL ) );
Nicola Di Lieto502d4b42020-04-25 14:41:25 +02001388}
1389
Nicola Di Lietofde98f72020-05-28 08:34:33 +02001390int mbedtls_x509_crt_parse_der_with_ext_cb( mbedtls_x509_crt *chain,
1391 const unsigned char *buf,
1392 size_t buflen,
Nicola Di Lieto4dbe5672020-05-28 09:18:42 +02001393 int make_copy,
Nicola Di Lieto5659e7e2020-05-28 23:41:38 +02001394 mbedtls_x509_crt_ext_cb_t cb,
1395 void *p_ctx )
Nicola Di Lieto502d4b42020-04-25 14:41:25 +02001396{
Nicola Di Lieto5659e7e2020-05-28 23:41:38 +02001397 return( mbedtls_x509_crt_parse_der_internal( chain, buf, buflen, make_copy, cb, p_ctx ) );
Hanno Becker1a65dcd2019-01-31 08:57:44 +00001398}
1399
1400int mbedtls_x509_crt_parse_der( mbedtls_x509_crt *chain,
1401 const unsigned char *buf,
1402 size_t buflen )
1403{
Nicola Di Lieto5659e7e2020-05-28 23:41:38 +02001404 return( mbedtls_x509_crt_parse_der_internal( chain, buf, buflen, 1, NULL, NULL ) );
Hanno Becker1a65dcd2019-01-31 08:57:44 +00001405}
1406
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001407/*
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001408 * Parse one or more PEM certificates from a buffer and add them to the chained
1409 * list
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001410 */
Hanno Becker1a65dcd2019-01-31 08:57:44 +00001411int mbedtls_x509_crt_parse( mbedtls_x509_crt *chain,
1412 const unsigned char *buf,
1413 size_t buflen )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001414{
Janos Follath98e28a72016-05-31 14:03:54 +01001415#if defined(MBEDTLS_PEM_PARSE_C)
Andres AGc0db5112016-12-07 15:05:53 +00001416 int success = 0, first_error = 0, total_failed = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001417 int buf_format = MBEDTLS_X509_FORMAT_DER;
Janos Follath98e28a72016-05-31 14:03:54 +01001418#endif
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001419
1420 /*
1421 * Check for valid input
1422 */
1423 if( chain == NULL || buf == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001424 return( MBEDTLS_ERR_X509_BAD_INPUT_DATA );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001425
1426 /*
1427 * Determine buffer content. Buffer contains either one DER certificate or
1428 * one or more PEM certificates.
1429 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001430#if defined(MBEDTLS_PEM_PARSE_C)
Manuel Pégourié-Gonnard0ece0f92015-05-12 12:43:54 +02001431 if( buflen != 0 && buf[buflen - 1] == '\0' &&
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001432 strstr( (const char *) buf, "-----BEGIN CERTIFICATE-----" ) != NULL )
1433 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001434 buf_format = MBEDTLS_X509_FORMAT_PEM;
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001435 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001436
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001437 if( buf_format == MBEDTLS_X509_FORMAT_DER )
1438 return mbedtls_x509_crt_parse_der( chain, buf, buflen );
Janos Follath98e28a72016-05-31 14:03:54 +01001439#else
1440 return mbedtls_x509_crt_parse_der( chain, buf, buflen );
1441#endif
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001442
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001443#if defined(MBEDTLS_PEM_PARSE_C)
1444 if( buf_format == MBEDTLS_X509_FORMAT_PEM )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001445 {
Janos Follath865b3eb2019-12-16 11:46:15 +00001446 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001447 mbedtls_pem_context pem;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001448
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001449 /* 1 rather than 0 since the terminating NULL byte is counted in */
1450 while( buflen > 1 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001451 {
1452 size_t use_len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001453 mbedtls_pem_init( &pem );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001454
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001455 /* If we get there, we know the string is null-terminated */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001456 ret = mbedtls_pem_read_buffer( &pem,
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001457 "-----BEGIN CERTIFICATE-----",
1458 "-----END CERTIFICATE-----",
1459 buf, NULL, 0, &use_len );
1460
1461 if( ret == 0 )
1462 {
1463 /*
1464 * Was PEM encoded
1465 */
1466 buflen -= use_len;
1467 buf += use_len;
1468 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001469 else if( ret == MBEDTLS_ERR_PEM_BAD_INPUT_DATA )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001470 {
1471 return( ret );
1472 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001473 else if( ret != MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001474 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001475 mbedtls_pem_free( &pem );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001476
1477 /*
1478 * PEM header and footer were found
1479 */
1480 buflen -= use_len;
1481 buf += use_len;
1482
1483 if( first_error == 0 )
1484 first_error = ret;
1485
Paul Bakker5a5fa922014-09-26 14:53:04 +02001486 total_failed++;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001487 continue;
1488 }
1489 else
1490 break;
1491
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001492 ret = mbedtls_x509_crt_parse_der( chain, pem.buf, pem.buflen );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001493
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001494 mbedtls_pem_free( &pem );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001495
1496 if( ret != 0 )
1497 {
1498 /*
1499 * Quit parsing on a memory error
1500 */
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02001501 if( ret == MBEDTLS_ERR_X509_ALLOC_FAILED )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001502 return( ret );
1503
1504 if( first_error == 0 )
1505 first_error = ret;
1506
1507 total_failed++;
1508 continue;
1509 }
1510
1511 success = 1;
1512 }
1513 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001514
1515 if( success )
1516 return( total_failed );
1517 else if( first_error )
1518 return( first_error );
1519 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001520 return( MBEDTLS_ERR_X509_CERT_UNKNOWN_FORMAT );
Janos Follath98e28a72016-05-31 14:03:54 +01001521#endif /* MBEDTLS_PEM_PARSE_C */
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001522}
1523
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001524#if defined(MBEDTLS_FS_IO)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001525/*
1526 * Load one or more certificates and add them to the chained list
1527 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001528int mbedtls_x509_crt_parse_file( mbedtls_x509_crt *chain, const char *path )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001529{
Janos Follath865b3eb2019-12-16 11:46:15 +00001530 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001531 size_t n;
1532 unsigned char *buf;
1533
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001534 if( ( ret = mbedtls_pk_load_file( path, &buf, &n ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001535 return( ret );
1536
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001537 ret = mbedtls_x509_crt_parse( chain, buf, n );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001538
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05001539 mbedtls_platform_zeroize( buf, n );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001540 mbedtls_free( buf );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001541
1542 return( ret );
1543}
1544
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001545int mbedtls_x509_crt_parse_path( mbedtls_x509_crt *chain, const char *path )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001546{
1547 int ret = 0;
Paul Bakkerfa6a6202013-10-28 18:48:30 +01001548#if defined(_WIN32) && !defined(EFIX64) && !defined(EFI32)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001549 int w_ret;
1550 WCHAR szDir[MAX_PATH];
1551 char filename[MAX_PATH];
Paul Bakker9af723c2014-05-01 13:03:14 +02001552 char *p;
Manuel Pégourié-Gonnard261faed2015-10-21 10:16:29 +02001553 size_t len = strlen( path );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001554
Paul Bakker9af723c2014-05-01 13:03:14 +02001555 WIN32_FIND_DATAW file_data;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001556 HANDLE hFind;
1557
1558 if( len > MAX_PATH - 3 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001559 return( MBEDTLS_ERR_X509_BAD_INPUT_DATA );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001560
Paul Bakker9af723c2014-05-01 13:03:14 +02001561 memset( szDir, 0, sizeof(szDir) );
1562 memset( filename, 0, MAX_PATH );
1563 memcpy( filename, path, len );
1564 filename[len++] = '\\';
1565 p = filename + len;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001566 filename[len++] = '*';
1567
Simon B3c6b18d2016-11-03 01:11:37 +00001568 w_ret = MultiByteToWideChar( CP_ACP, 0, filename, (int)len, szDir,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001569 MAX_PATH - 3 );
Manuel Pégourié-Gonnardacdb9b92015-01-23 17:50:34 +00001570 if( w_ret == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001571 return( MBEDTLS_ERR_X509_BAD_INPUT_DATA );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001572
1573 hFind = FindFirstFileW( szDir, &file_data );
Paul Bakker66d5d072014-06-17 16:39:18 +02001574 if( hFind == INVALID_HANDLE_VALUE )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001575 return( MBEDTLS_ERR_X509_FILE_IO_ERROR );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001576
1577 len = MAX_PATH - len;
1578 do
1579 {
Paul Bakker9af723c2014-05-01 13:03:14 +02001580 memset( p, 0, len );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001581
1582 if( file_data.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY )
1583 continue;
1584
Paul Bakker9af723c2014-05-01 13:03:14 +02001585 w_ret = WideCharToMultiByte( CP_ACP, 0, file_data.cFileName,
Paul Bakker66d5d072014-06-17 16:39:18 +02001586 lstrlenW( file_data.cFileName ),
Manuel Pégourié-Gonnard261faed2015-10-21 10:16:29 +02001587 p, (int) len - 1,
Paul Bakker9af723c2014-05-01 13:03:14 +02001588 NULL, NULL );
Manuel Pégourié-Gonnardacdb9b92015-01-23 17:50:34 +00001589 if( w_ret == 0 )
Ron Eldor36d90422017-01-09 15:09:16 +02001590 {
1591 ret = MBEDTLS_ERR_X509_FILE_IO_ERROR;
1592 goto cleanup;
1593 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001594
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001595 w_ret = mbedtls_x509_crt_parse_file( chain, filename );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001596 if( w_ret < 0 )
1597 ret++;
1598 else
1599 ret += w_ret;
1600 }
1601 while( FindNextFileW( hFind, &file_data ) != 0 );
1602
Paul Bakker66d5d072014-06-17 16:39:18 +02001603 if( GetLastError() != ERROR_NO_MORE_FILES )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001604 ret = MBEDTLS_ERR_X509_FILE_IO_ERROR;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001605
Ron Eldor36d90422017-01-09 15:09:16 +02001606cleanup:
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001607 FindClose( hFind );
Paul Bakkerbe089b02013-10-14 15:51:50 +02001608#else /* _WIN32 */
Manuel Pégourié-Gonnard964bf9b2013-11-26 16:47:11 +01001609 int t_ret;
Andres AGf9113192016-09-02 14:06:04 +01001610 int snp_ret;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001611 struct stat sb;
Manuel Pégourié-Gonnard964bf9b2013-11-26 16:47:11 +01001612 struct dirent *entry;
Andres AGf9113192016-09-02 14:06:04 +01001613 char entry_name[MBEDTLS_X509_MAX_FILE_PATH_LEN];
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001614 DIR *dir = opendir( path );
1615
Paul Bakker66d5d072014-06-17 16:39:18 +02001616 if( dir == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001617 return( MBEDTLS_ERR_X509_FILE_IO_ERROR );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001618
Ron Eldor63140682017-01-09 19:27:59 +02001619#if defined(MBEDTLS_THREADING_C)
Manuel Pégourié-Gonnard944cfe82015-05-27 20:07:18 +02001620 if( ( ret = mbedtls_mutex_lock( &mbedtls_threading_readdir_mutex ) ) != 0 )
Manuel Pégourié-Gonnardf9b85d92015-06-22 18:39:57 +02001621 {
1622 closedir( dir );
Manuel Pégourié-Gonnard5ad68e42013-11-28 17:11:54 +01001623 return( ret );
Manuel Pégourié-Gonnardf9b85d92015-06-22 18:39:57 +02001624 }
Ron Eldor63140682017-01-09 19:27:59 +02001625#endif /* MBEDTLS_THREADING_C */
Manuel Pégourié-Gonnard5ad68e42013-11-28 17:11:54 +01001626
Paul Elliottfb91a482021-03-05 14:17:51 +00001627 memset( &sb, 0, sizeof( sb ) );
1628
Manuel Pégourié-Gonnard964bf9b2013-11-26 16:47:11 +01001629 while( ( entry = readdir( dir ) ) != NULL )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001630 {
Andres AGf9113192016-09-02 14:06:04 +01001631 snp_ret = mbedtls_snprintf( entry_name, sizeof entry_name,
1632 "%s/%s", path, entry->d_name );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001633
Andres AGf9113192016-09-02 14:06:04 +01001634 if( snp_ret < 0 || (size_t)snp_ret >= sizeof entry_name )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001635 {
Andres AGf9113192016-09-02 14:06:04 +01001636 ret = MBEDTLS_ERR_X509_BUFFER_TOO_SMALL;
1637 goto cleanup;
1638 }
1639 else if( stat( entry_name, &sb ) == -1 )
1640 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001641 ret = MBEDTLS_ERR_X509_FILE_IO_ERROR;
Manuel Pégourié-Gonnard5ad68e42013-11-28 17:11:54 +01001642 goto cleanup;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001643 }
1644
1645 if( !S_ISREG( sb.st_mode ) )
1646 continue;
1647
1648 // Ignore parse errors
1649 //
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001650 t_ret = mbedtls_x509_crt_parse_file( chain, entry_name );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001651 if( t_ret < 0 )
1652 ret++;
1653 else
1654 ret += t_ret;
1655 }
Manuel Pégourié-Gonnard5ad68e42013-11-28 17:11:54 +01001656
1657cleanup:
Andres AGf9113192016-09-02 14:06:04 +01001658 closedir( dir );
1659
Ron Eldor63140682017-01-09 19:27:59 +02001660#if defined(MBEDTLS_THREADING_C)
Manuel Pégourié-Gonnard944cfe82015-05-27 20:07:18 +02001661 if( mbedtls_mutex_unlock( &mbedtls_threading_readdir_mutex ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001662 ret = MBEDTLS_ERR_THREADING_MUTEX_ERROR;
Ron Eldor63140682017-01-09 19:27:59 +02001663#endif /* MBEDTLS_THREADING_C */
Manuel Pégourié-Gonnard5ad68e42013-11-28 17:11:54 +01001664
Paul Bakkerbe089b02013-10-14 15:51:50 +02001665#endif /* _WIN32 */
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001666
1667 return( ret );
1668}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001669#endif /* MBEDTLS_FS_IO */
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001670
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001671/*
1672 * OtherName ::= SEQUENCE {
1673 * type-id OBJECT IDENTIFIER,
1674 * value [0] EXPLICIT ANY DEFINED BY type-id }
1675 *
1676 * HardwareModuleName ::= SEQUENCE {
1677 * hwType OBJECT IDENTIFIER,
1678 * hwSerialNum OCTET STRING }
1679 *
1680 * NOTE: we currently only parse and use otherName of type HwModuleName,
1681 * as defined in RFC 4108.
1682 */
1683static int x509_get_other_name( const mbedtls_x509_buf *subject_alt_name,
1684 mbedtls_x509_san_other_name *other_name )
1685{
Janos Follathab23cd12019-05-09 13:53:57 +01001686 int ret = 0;
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001687 size_t len;
1688 unsigned char *p = subject_alt_name->p;
1689 const unsigned char *end = p + subject_alt_name->len;
1690 mbedtls_x509_buf cur_oid;
1691
1692 if( ( subject_alt_name->tag &
1693 ( MBEDTLS_ASN1_TAG_CLASS_MASK | MBEDTLS_ASN1_TAG_VALUE_MASK ) ) !=
1694 ( MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_X509_SAN_OTHER_NAME ) )
1695 {
1696 /*
1697 * The given subject alternative name is not of type "othername".
1698 */
1699 return( MBEDTLS_ERR_X509_BAD_INPUT_DATA );
1700 }
1701
1702 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
1703 MBEDTLS_ASN1_OID ) ) != 0 )
Chris Jones9f7a6932021-04-14 12:12:09 +01001704 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret ) );
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001705
1706 cur_oid.tag = MBEDTLS_ASN1_OID;
1707 cur_oid.p = p;
1708 cur_oid.len = len;
1709
1710 /*
1711 * Only HwModuleName is currently supported.
1712 */
1713 if( MBEDTLS_OID_CMP( MBEDTLS_OID_ON_HW_MODULE_NAME, &cur_oid ) != 0 )
1714 {
1715 return( MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE );
1716 }
1717
Ron Eldor890819a2019-05-13 19:03:04 +03001718 if( p + len >= end )
1719 {
Sébastien Duquette661d7252019-06-23 17:45:26 -04001720 mbedtls_platform_zeroize( other_name, sizeof( *other_name ) );
Chris Jones9f7a6932021-04-14 12:12:09 +01001721 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
1722 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH ) );
Ron Eldor890819a2019-05-13 19:03:04 +03001723 }
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001724 p += len;
1725 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
1726 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_CONTEXT_SPECIFIC ) ) != 0 )
Chris Jones9f7a6932021-04-14 12:12:09 +01001727 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret ) );
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001728
1729 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
1730 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
Chris Jones9f7a6932021-04-14 12:12:09 +01001731 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret ) );
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001732
1733 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len, MBEDTLS_ASN1_OID ) ) != 0 )
Chris Jones9f7a6932021-04-14 12:12:09 +01001734 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret ) );
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001735
1736 other_name->value.hardware_module_name.oid.tag = MBEDTLS_ASN1_OID;
1737 other_name->value.hardware_module_name.oid.p = p;
1738 other_name->value.hardware_module_name.oid.len = len;
1739
Ron Eldor890819a2019-05-13 19:03:04 +03001740 if( p + len >= end )
1741 {
Sébastien Duquette661d7252019-06-23 17:45:26 -04001742 mbedtls_platform_zeroize( other_name, sizeof( *other_name ) );
Chris Jones9f7a6932021-04-14 12:12:09 +01001743 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
1744 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH ) );
Ron Eldor890819a2019-05-13 19:03:04 +03001745 }
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001746 p += len;
1747 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
1748 MBEDTLS_ASN1_OCTET_STRING ) ) != 0 )
Chris Jones9f7a6932021-04-14 12:12:09 +01001749 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret ) );
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001750
1751 other_name->value.hardware_module_name.val.tag = MBEDTLS_ASN1_OCTET_STRING;
1752 other_name->value.hardware_module_name.val.p = p;
1753 other_name->value.hardware_module_name.val.len = len;
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001754 p += len;
1755 if( p != end )
1756 {
Ron Eldor890819a2019-05-13 19:03:04 +03001757 mbedtls_platform_zeroize( other_name,
Sébastien Duquette661d7252019-06-23 17:45:26 -04001758 sizeof( *other_name ) );
Chris Jones9f7a6932021-04-14 12:12:09 +01001759 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
1760 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH ) );
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001761 }
1762 return( 0 );
1763}
1764
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02001765static int x509_info_subject_alt_name( char **buf, size_t *size,
Janos Follath2f0ec1e2019-05-10 11:06:31 +01001766 const mbedtls_x509_sequence
1767 *subject_alt_name,
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001768 const char *prefix )
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02001769{
Janos Follath865b3eb2019-12-16 11:46:15 +00001770 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02001771 size_t n = *size;
1772 char *p = *buf;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001773 const mbedtls_x509_sequence *cur = subject_alt_name;
Ron Eldor890819a2019-05-13 19:03:04 +03001774 mbedtls_x509_subject_alternative_name san;
1775 int parse_ret;
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02001776
1777 while( cur != NULL )
1778 {
Ron Eldor890819a2019-05-13 19:03:04 +03001779 memset( &san, 0, sizeof( san ) );
1780 parse_ret = mbedtls_x509_parse_subject_alt_name( &cur->buf, &san );
1781 if( parse_ret != 0 )
1782 {
1783 if( parse_ret == MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE )
1784 {
1785 ret = mbedtls_snprintf( p, n, "\n%s <unsupported>", prefix );
1786 MBEDTLS_X509_SAFE_SNPRINTF;
1787 }
1788 else
1789 {
1790 ret = mbedtls_snprintf( p, n, "\n%s <malformed>", prefix );
1791 MBEDTLS_X509_SAFE_SNPRINTF;
1792 }
1793 cur = cur->next;
1794 continue;
1795 }
1796
1797 switch( san.type )
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02001798 {
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001799 /*
1800 * otherName
1801 */
Ron Eldora2913912019-05-16 16:17:38 +03001802 case MBEDTLS_X509_SAN_OTHER_NAME:
Ron Eldor890819a2019-05-13 19:03:04 +03001803 {
1804 mbedtls_x509_san_other_name *other_name = &san.san.other_name;
1805
1806 ret = mbedtls_snprintf( p, n, "\n%s otherName :", prefix );
1807 MBEDTLS_X509_SAFE_SNPRINTF;
1808
1809 if( MBEDTLS_OID_CMP( MBEDTLS_OID_ON_HW_MODULE_NAME,
1810 &other_name->value.hardware_module_name.oid ) != 0 )
Janos Follath22f605f2019-05-10 10:37:17 +01001811 {
Ron Eldor890819a2019-05-13 19:03:04 +03001812 ret = mbedtls_snprintf( p, n, "\n%s hardware module name :", prefix );
1813 MBEDTLS_X509_SAFE_SNPRINTF;
1814 ret = mbedtls_snprintf( p, n, "\n%s hardware type : ", prefix );
Janos Follath2f0ec1e2019-05-10 11:06:31 +01001815 MBEDTLS_X509_SAFE_SNPRINTF;
1816
Ron Eldor890819a2019-05-13 19:03:04 +03001817 ret = mbedtls_oid_get_numeric_string( p, n, &other_name->value.hardware_module_name.oid );
1818 MBEDTLS_X509_SAFE_SNPRINTF;
Janos Follath2f0ec1e2019-05-10 11:06:31 +01001819
Ron Eldor890819a2019-05-13 19:03:04 +03001820 ret = mbedtls_snprintf( p, n, "\n%s hardware serial number : ", prefix );
1821 MBEDTLS_X509_SAFE_SNPRINTF;
1822
1823 if( other_name->value.hardware_module_name.val.len >= n )
1824 {
1825 *p = '\0';
1826 return( MBEDTLS_ERR_X509_BUFFER_TOO_SMALL );
Janos Follath22f605f2019-05-10 10:37:17 +01001827 }
1828
Ron Eldora2913912019-05-16 16:17:38 +03001829 memcpy( p, other_name->value.hardware_module_name.val.p,
1830 other_name->value.hardware_module_name.val.len );
1831 p += other_name->value.hardware_module_name.val.len;
1832
Ron Eldor890819a2019-05-13 19:03:04 +03001833 n -= other_name->value.hardware_module_name.val.len;
Janos Follath2f0ec1e2019-05-10 11:06:31 +01001834
Ron Eldor890819a2019-05-13 19:03:04 +03001835 }/* MBEDTLS_OID_ON_HW_MODULE_NAME */
1836 }
1837 break;
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001838
1839 /*
1840 * dNSName
1841 */
Ron Eldora2913912019-05-16 16:17:38 +03001842 case MBEDTLS_X509_SAN_DNS_NAME:
Ron Eldor890819a2019-05-13 19:03:04 +03001843 {
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001844 ret = mbedtls_snprintf( p, n, "\n%s dNSName : ", prefix );
1845 MBEDTLS_X509_SAFE_SNPRINTF;
Ron Eldor890819a2019-05-13 19:03:04 +03001846 if( san.san.unstructured_name.len >= n )
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001847 {
1848 *p = '\0';
1849 return( MBEDTLS_ERR_X509_BUFFER_TOO_SMALL );
1850 }
Ron Eldora2913912019-05-16 16:17:38 +03001851
1852 memcpy( p, san.san.unstructured_name.p, san.san.unstructured_name.len );
1853 p += san.san.unstructured_name.len;
Ron Eldor890819a2019-05-13 19:03:04 +03001854 n -= san.san.unstructured_name.len;
Ron Eldor890819a2019-05-13 19:03:04 +03001855 }
1856 break;
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001857
1858 /*
Ron Eldor890819a2019-05-13 19:03:04 +03001859 * Type not supported, skip item.
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001860 */
1861 default:
Janos Follath22f605f2019-05-10 10:37:17 +01001862 ret = mbedtls_snprintf( p, n, "\n%s <unsupported>", prefix );
1863 MBEDTLS_X509_SAFE_SNPRINTF;
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001864 break;
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02001865 }
1866
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02001867 cur = cur->next;
1868 }
1869
1870 *p = '\0';
1871
1872 *size = n;
1873 *buf = p;
1874
1875 return( 0 );
1876}
1877
Ron Eldor890819a2019-05-13 19:03:04 +03001878int mbedtls_x509_parse_subject_alt_name( const mbedtls_x509_buf *san_buf,
1879 mbedtls_x509_subject_alternative_name *san )
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001880{
Janos Follath865b3eb2019-12-16 11:46:15 +00001881 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Ron Eldor890819a2019-05-13 19:03:04 +03001882 switch( san_buf->tag &
1883 ( MBEDTLS_ASN1_TAG_CLASS_MASK |
1884 MBEDTLS_ASN1_TAG_VALUE_MASK ) )
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001885 {
Ron Eldor890819a2019-05-13 19:03:04 +03001886 /*
1887 * otherName
1888 */
1889 case( MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_X509_SAN_OTHER_NAME ):
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001890 {
Ron Eldor890819a2019-05-13 19:03:04 +03001891 mbedtls_x509_san_other_name other_name;
1892
1893 ret = x509_get_other_name( san_buf, &other_name );
1894 if( ret != 0 )
1895 return( ret );
1896
1897 memset( san, 0, sizeof( mbedtls_x509_subject_alternative_name ) );
1898 san->type = MBEDTLS_X509_SAN_OTHER_NAME;
1899 memcpy( &san->san.other_name,
1900 &other_name, sizeof( other_name ) );
1901
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001902 }
Ron Eldor890819a2019-05-13 19:03:04 +03001903 break;
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001904
Ron Eldor890819a2019-05-13 19:03:04 +03001905 /*
1906 * dNSName
1907 */
1908 case( MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_X509_SAN_DNS_NAME ):
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001909 {
Ron Eldor890819a2019-05-13 19:03:04 +03001910 memset( san, 0, sizeof( mbedtls_x509_subject_alternative_name ) );
1911 san->type = MBEDTLS_X509_SAN_DNS_NAME;
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001912
Ron Eldor890819a2019-05-13 19:03:04 +03001913 memcpy( &san->san.unstructured_name,
1914 san_buf, sizeof( *san_buf ) );
Janos Follath6c379b42019-05-10 14:17:16 +01001915
Ron Eldor890819a2019-05-13 19:03:04 +03001916 }
1917 break;
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001918
Ron Eldor890819a2019-05-13 19:03:04 +03001919 /*
1920 * Type not supported
1921 */
1922 default:
1923 return( MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE );
1924 }
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02001925 return( 0 );
1926}
1927
Manuel Pégourié-Gonnard0db29b02014-04-01 18:12:24 +02001928#define PRINT_ITEM(i) \
1929 { \
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001930 ret = mbedtls_snprintf( p, n, "%s" i, sep ); \
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001931 MBEDTLS_X509_SAFE_SNPRINTF; \
Manuel Pégourié-Gonnard0db29b02014-04-01 18:12:24 +02001932 sep = ", "; \
1933 }
1934
1935#define CERT_TYPE(type,name) \
Hanno Becker1eeca412018-10-15 12:01:35 +01001936 if( ns_cert_type & (type) ) \
Manuel Pégourié-Gonnard0db29b02014-04-01 18:12:24 +02001937 PRINT_ITEM( name );
1938
Manuel Pégourié-Gonnard919f8f52014-04-01 13:01:11 +02001939static int x509_info_cert_type( char **buf, size_t *size,
1940 unsigned char ns_cert_type )
1941{
Janos Follath865b3eb2019-12-16 11:46:15 +00001942 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard919f8f52014-04-01 13:01:11 +02001943 size_t n = *size;
1944 char *p = *buf;
Manuel Pégourié-Gonnard7b30cfc2014-04-01 18:00:07 +02001945 const char *sep = "";
Manuel Pégourié-Gonnard919f8f52014-04-01 13:01:11 +02001946
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001947 CERT_TYPE( MBEDTLS_X509_NS_CERT_TYPE_SSL_CLIENT, "SSL Client" );
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01001948 CERT_TYPE( MBEDTLS_X509_NS_CERT_TYPE_SSL_SERVER, "SSL Server" );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001949 CERT_TYPE( MBEDTLS_X509_NS_CERT_TYPE_EMAIL, "Email" );
1950 CERT_TYPE( MBEDTLS_X509_NS_CERT_TYPE_OBJECT_SIGNING, "Object Signing" );
1951 CERT_TYPE( MBEDTLS_X509_NS_CERT_TYPE_RESERVED, "Reserved" );
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01001952 CERT_TYPE( MBEDTLS_X509_NS_CERT_TYPE_SSL_CA, "SSL CA" );
1953 CERT_TYPE( MBEDTLS_X509_NS_CERT_TYPE_EMAIL_CA, "Email CA" );
1954 CERT_TYPE( MBEDTLS_X509_NS_CERT_TYPE_OBJECT_SIGNING_CA, "Object Signing CA" );
Manuel Pégourié-Gonnard919f8f52014-04-01 13:01:11 +02001955
1956 *size = n;
1957 *buf = p;
1958
1959 return( 0 );
1960}
1961
Manuel Pégourié-Gonnard0db29b02014-04-01 18:12:24 +02001962#define KEY_USAGE(code,name) \
Hanno Becker1eeca412018-10-15 12:01:35 +01001963 if( key_usage & (code) ) \
Manuel Pégourié-Gonnard0db29b02014-04-01 18:12:24 +02001964 PRINT_ITEM( name );
1965
Manuel Pégourié-Gonnard65c2ddc2014-04-01 14:12:11 +02001966static int x509_info_key_usage( char **buf, size_t *size,
Manuel Pégourié-Gonnard9a702252015-06-23 10:14:36 +02001967 unsigned int key_usage )
Manuel Pégourié-Gonnard65c2ddc2014-04-01 14:12:11 +02001968{
Janos Follath865b3eb2019-12-16 11:46:15 +00001969 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard65c2ddc2014-04-01 14:12:11 +02001970 size_t n = *size;
1971 char *p = *buf;
Manuel Pégourié-Gonnard7b30cfc2014-04-01 18:00:07 +02001972 const char *sep = "";
Manuel Pégourié-Gonnard65c2ddc2014-04-01 14:12:11 +02001973
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001974 KEY_USAGE( MBEDTLS_X509_KU_DIGITAL_SIGNATURE, "Digital Signature" );
1975 KEY_USAGE( MBEDTLS_X509_KU_NON_REPUDIATION, "Non Repudiation" );
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01001976 KEY_USAGE( MBEDTLS_X509_KU_KEY_ENCIPHERMENT, "Key Encipherment" );
1977 KEY_USAGE( MBEDTLS_X509_KU_DATA_ENCIPHERMENT, "Data Encipherment" );
1978 KEY_USAGE( MBEDTLS_X509_KU_KEY_AGREEMENT, "Key Agreement" );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001979 KEY_USAGE( MBEDTLS_X509_KU_KEY_CERT_SIGN, "Key Cert Sign" );
1980 KEY_USAGE( MBEDTLS_X509_KU_CRL_SIGN, "CRL Sign" );
Manuel Pégourié-Gonnard9a702252015-06-23 10:14:36 +02001981 KEY_USAGE( MBEDTLS_X509_KU_ENCIPHER_ONLY, "Encipher Only" );
1982 KEY_USAGE( MBEDTLS_X509_KU_DECIPHER_ONLY, "Decipher Only" );
Manuel Pégourié-Gonnard65c2ddc2014-04-01 14:12:11 +02001983
1984 *size = n;
1985 *buf = p;
1986
1987 return( 0 );
1988}
1989
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02001990static int x509_info_ext_key_usage( char **buf, size_t *size,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001991 const mbedtls_x509_sequence *extended_key_usage )
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02001992{
Janos Follath865b3eb2019-12-16 11:46:15 +00001993 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02001994 const char *desc;
1995 size_t n = *size;
1996 char *p = *buf;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001997 const mbedtls_x509_sequence *cur = extended_key_usage;
Manuel Pégourié-Gonnard7b30cfc2014-04-01 18:00:07 +02001998 const char *sep = "";
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02001999
2000 while( cur != NULL )
2001 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002002 if( mbedtls_oid_get_extended_key_usage( &cur->buf, &desc ) != 0 )
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02002003 desc = "???";
2004
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002005 ret = mbedtls_snprintf( p, n, "%s%s", sep, desc );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002006 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02002007
Manuel Pégourié-Gonnard7b30cfc2014-04-01 18:00:07 +02002008 sep = ", ";
2009
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02002010 cur = cur->next;
2011 }
2012
2013 *size = n;
2014 *buf = p;
2015
2016 return( 0 );
2017}
2018
Ron Eldor74d9acc2019-03-21 14:00:03 +02002019static int x509_info_cert_policies( char **buf, size_t *size,
2020 const mbedtls_x509_sequence *certificate_policies )
2021{
Janos Follath865b3eb2019-12-16 11:46:15 +00002022 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Ron Eldor74d9acc2019-03-21 14:00:03 +02002023 const char *desc;
2024 size_t n = *size;
2025 char *p = *buf;
2026 const mbedtls_x509_sequence *cur = certificate_policies;
2027 const char *sep = "";
2028
2029 while( cur != NULL )
2030 {
2031 if( mbedtls_oid_get_certificate_policies( &cur->buf, &desc ) != 0 )
2032 desc = "???";
2033
2034 ret = mbedtls_snprintf( p, n, "%s%s", sep, desc );
2035 MBEDTLS_X509_SAFE_SNPRINTF;
2036
2037 sep = ", ";
2038
2039 cur = cur->next;
2040 }
2041
2042 *size = n;
2043 *buf = p;
2044
2045 return( 0 );
2046}
2047
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002048/*
2049 * Return an informational string about the certificate.
2050 */
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002051#define BEFORE_COLON 18
2052#define BC "18"
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002053int mbedtls_x509_crt_info( char *buf, size_t size, const char *prefix,
2054 const mbedtls_x509_crt *crt )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002055{
Janos Follath865b3eb2019-12-16 11:46:15 +00002056 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002057 size_t n;
2058 char *p;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002059 char key_size_str[BEFORE_COLON];
2060
2061 p = buf;
2062 n = size;
2063
Janos Follath98e28a72016-05-31 14:03:54 +01002064 if( NULL == crt )
2065 {
2066 ret = mbedtls_snprintf( p, n, "\nCertificate is uninitialised!\n" );
2067 MBEDTLS_X509_SAFE_SNPRINTF;
2068
2069 return( (int) ( size - n ) );
2070 }
2071
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002072 ret = mbedtls_snprintf( p, n, "%scert. version : %d\n",
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002073 prefix, crt->version );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002074 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002075 ret = mbedtls_snprintf( p, n, "%sserial number : ",
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002076 prefix );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002077 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002078
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002079 ret = mbedtls_x509_serial_gets( p, n, &crt->serial );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002080 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002081
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002082 ret = mbedtls_snprintf( p, n, "\n%sissuer name : ", prefix );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002083 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002084 ret = mbedtls_x509_dn_gets( p, n, &crt->issuer );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002085 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002086
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002087 ret = mbedtls_snprintf( p, n, "\n%ssubject name : ", prefix );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002088 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002089 ret = mbedtls_x509_dn_gets( p, n, &crt->subject );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002090 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002091
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002092 ret = mbedtls_snprintf( p, n, "\n%sissued on : " \
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002093 "%04d-%02d-%02d %02d:%02d:%02d", prefix,
2094 crt->valid_from.year, crt->valid_from.mon,
2095 crt->valid_from.day, crt->valid_from.hour,
2096 crt->valid_from.min, crt->valid_from.sec );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002097 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002098
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002099 ret = mbedtls_snprintf( p, n, "\n%sexpires on : " \
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002100 "%04d-%02d-%02d %02d:%02d:%02d", prefix,
2101 crt->valid_to.year, crt->valid_to.mon,
2102 crt->valid_to.day, crt->valid_to.hour,
2103 crt->valid_to.min, crt->valid_to.sec );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002104 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002105
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002106 ret = mbedtls_snprintf( p, n, "\n%ssigned using : ", prefix );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002107 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002108
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002109 ret = mbedtls_x509_sig_alg_gets( p, n, &crt->sig_oid, crt->sig_pk,
Manuel Pégourié-Gonnardbf696d02014-06-05 17:07:30 +02002110 crt->sig_md, crt->sig_opts );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002111 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002112
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002113 /* Key size */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002114 if( ( ret = mbedtls_x509_key_size_helper( key_size_str, BEFORE_COLON,
2115 mbedtls_pk_get_name( &crt->pk ) ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002116 {
2117 return( ret );
2118 }
2119
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002120 ret = mbedtls_snprintf( p, n, "\n%s%-" BC "s: %d bits", prefix, key_size_str,
Manuel Pégourié-Gonnard097c7bb2015-06-18 16:43:38 +02002121 (int) mbedtls_pk_get_bitlen( &crt->pk ) );
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é-Gonnardb28487d2014-04-01 12:19:09 +02002124 /*
2125 * Optional extensions
2126 */
2127
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01002128 if( crt->ext_types & MBEDTLS_X509_EXT_BASIC_CONSTRAINTS )
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002129 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002130 ret = mbedtls_snprintf( p, n, "\n%sbasic constraints : CA=%s", prefix,
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002131 crt->ca_istrue ? "true" : "false" );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002132 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002133
2134 if( crt->max_pathlen > 0 )
2135 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002136 ret = mbedtls_snprintf( p, n, ", max_pathlen=%d", crt->max_pathlen - 1 );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002137 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002138 }
2139 }
2140
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01002141 if( crt->ext_types & MBEDTLS_X509_EXT_SUBJECT_ALT_NAME )
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002142 {
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02002143 ret = mbedtls_snprintf( p, n, "\n%ssubject alt name :", prefix );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002144 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02002145
2146 if( ( ret = x509_info_subject_alt_name( &p, &n,
Ron Eldorb2dc3fa2019-03-21 13:40:13 +02002147 &crt->subject_alt_names,
Ron Eldor6aeae9e2019-05-20 12:00:36 +03002148 prefix ) ) != 0 )
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02002149 return( ret );
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002150 }
2151
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002152 if( crt->ext_types & MBEDTLS_X509_EXT_NS_CERT_TYPE )
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002153 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002154 ret = mbedtls_snprintf( p, n, "\n%scert. type : ", prefix );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002155 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnard919f8f52014-04-01 13:01:11 +02002156
2157 if( ( ret = x509_info_cert_type( &p, &n, crt->ns_cert_type ) ) != 0 )
2158 return( ret );
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002159 }
2160
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002161 if( crt->ext_types & MBEDTLS_X509_EXT_KEY_USAGE )
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002162 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002163 ret = mbedtls_snprintf( p, n, "\n%skey usage : ", prefix );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002164 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnard65c2ddc2014-04-01 14:12:11 +02002165
2166 if( ( ret = x509_info_key_usage( &p, &n, crt->key_usage ) ) != 0 )
2167 return( ret );
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002168 }
2169
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002170 if( crt->ext_types & MBEDTLS_X509_EXT_EXTENDED_KEY_USAGE )
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002171 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002172 ret = mbedtls_snprintf( p, n, "\n%sext key usage : ", prefix );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002173 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02002174
2175 if( ( ret = x509_info_ext_key_usage( &p, &n,
2176 &crt->ext_key_usage ) ) != 0 )
2177 return( ret );
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002178 }
2179
Ron Eldor74d9acc2019-03-21 14:00:03 +02002180 if( crt->ext_types & MBEDTLS_OID_X509_EXT_CERTIFICATE_POLICIES )
2181 {
2182 ret = mbedtls_snprintf( p, n, "\n%scertificate policies : ", prefix );
2183 MBEDTLS_X509_SAFE_SNPRINTF;
2184
2185 if( ( ret = x509_info_cert_policies( &p, &n,
2186 &crt->certificate_policies ) ) != 0 )
2187 return( ret );
2188 }
2189
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002190 ret = mbedtls_snprintf( p, n, "\n" );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002191 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02002192
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002193 return( (int) ( size - n ) );
2194}
2195
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01002196struct x509_crt_verify_string {
2197 int code;
2198 const char *string;
2199};
2200
2201static const struct x509_crt_verify_string x509_crt_verify_strings[] = {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01002202 { MBEDTLS_X509_BADCERT_EXPIRED, "The certificate validity has expired" },
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01002203 { MBEDTLS_X509_BADCERT_REVOKED, "The certificate has been revoked (is on a CRL)" },
2204 { MBEDTLS_X509_BADCERT_CN_MISMATCH, "The certificate Common Name (CN) does not match with the expected CN" },
2205 { MBEDTLS_X509_BADCERT_NOT_TRUSTED, "The certificate is not correctly signed by the trusted CA" },
2206 { MBEDTLS_X509_BADCRL_NOT_TRUSTED, "The CRL is not correctly signed by the trusted CA" },
2207 { MBEDTLS_X509_BADCRL_EXPIRED, "The CRL is expired" },
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01002208 { MBEDTLS_X509_BADCERT_MISSING, "Certificate was missing" },
2209 { MBEDTLS_X509_BADCERT_SKIP_VERIFY, "Certificate verification was skipped" },
2210 { MBEDTLS_X509_BADCERT_OTHER, "Other reason (can be used by verify callback)" },
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01002211 { MBEDTLS_X509_BADCERT_FUTURE, "The certificate validity starts in the future" },
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01002212 { MBEDTLS_X509_BADCRL_FUTURE, "The CRL is from the future" },
2213 { MBEDTLS_X509_BADCERT_KEY_USAGE, "Usage does not match the keyUsage extension" },
2214 { MBEDTLS_X509_BADCERT_EXT_KEY_USAGE, "Usage does not match the extendedKeyUsage extension" },
2215 { MBEDTLS_X509_BADCERT_NS_CERT_TYPE, "Usage does not match the nsCertType extension" },
Manuel Pégourié-Gonnard95051642015-06-15 10:39:46 +02002216 { MBEDTLS_X509_BADCERT_BAD_MD, "The certificate is signed with an unacceptable hash." },
2217 { MBEDTLS_X509_BADCERT_BAD_PK, "The certificate is signed with an unacceptable PK alg (eg RSA vs ECDSA)." },
2218 { MBEDTLS_X509_BADCERT_BAD_KEY, "The certificate is signed with an unacceptable key (eg bad curve, RSA too short)." },
2219 { MBEDTLS_X509_BADCRL_BAD_MD, "The CRL is signed with an unacceptable hash." },
2220 { MBEDTLS_X509_BADCRL_BAD_PK, "The CRL is signed with an unacceptable PK alg (eg RSA vs ECDSA)." },
2221 { 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 +01002222 { 0, NULL }
2223};
2224
2225int mbedtls_x509_crt_verify_info( char *buf, size_t size, const char *prefix,
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02002226 uint32_t flags )
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01002227{
Janos Follath865b3eb2019-12-16 11:46:15 +00002228 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01002229 const struct x509_crt_verify_string *cur;
2230 char *p = buf;
2231 size_t n = size;
2232
2233 for( cur = x509_crt_verify_strings; cur->string != NULL ; cur++ )
2234 {
2235 if( ( flags & cur->code ) == 0 )
2236 continue;
2237
2238 ret = mbedtls_snprintf( p, n, "%s%s\n", prefix, cur->string );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002239 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01002240 flags ^= cur->code;
2241 }
2242
2243 if( flags != 0 )
2244 {
2245 ret = mbedtls_snprintf( p, n, "%sUnknown reason "
2246 "(this should not happen)\n", prefix );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02002247 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01002248 }
2249
2250 return( (int) ( size - n ) );
2251}
2252
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002253#if defined(MBEDTLS_X509_CHECK_KEY_USAGE)
Manuel Pégourié-Gonnard655a9642015-06-23 10:48:44 +02002254int mbedtls_x509_crt_check_key_usage( const mbedtls_x509_crt *crt,
2255 unsigned int usage )
Manuel Pégourié-Gonnard603116c2014-04-09 09:50:03 +02002256{
Manuel Pégourié-Gonnard655a9642015-06-23 10:48:44 +02002257 unsigned int usage_must, usage_may;
2258 unsigned int may_mask = MBEDTLS_X509_KU_ENCIPHER_ONLY
2259 | MBEDTLS_X509_KU_DECIPHER_ONLY;
2260
2261 if( ( crt->ext_types & MBEDTLS_X509_EXT_KEY_USAGE ) == 0 )
2262 return( 0 );
2263
2264 usage_must = usage & ~may_mask;
2265
2266 if( ( ( crt->key_usage & ~may_mask ) & usage_must ) != usage_must )
2267 return( MBEDTLS_ERR_X509_BAD_INPUT_DATA );
2268
2269 usage_may = usage & may_mask;
2270
2271 if( ( ( crt->key_usage & may_mask ) | usage_may ) != usage_may )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002272 return( MBEDTLS_ERR_X509_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard603116c2014-04-09 09:50:03 +02002273
2274 return( 0 );
2275}
2276#endif
2277
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002278#if defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE)
2279int mbedtls_x509_crt_check_extended_key_usage( const mbedtls_x509_crt *crt,
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02002280 const char *usage_oid,
2281 size_t usage_len )
2282{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002283 const mbedtls_x509_sequence *cur;
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02002284
2285 /* Extension is not mandatory, absent means no restriction */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002286 if( ( crt->ext_types & MBEDTLS_X509_EXT_EXTENDED_KEY_USAGE ) == 0 )
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02002287 return( 0 );
2288
2289 /*
2290 * Look for the requested usage (or wildcard ANY) in our list
2291 */
2292 for( cur = &crt->ext_key_usage; cur != NULL; cur = cur->next )
2293 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002294 const mbedtls_x509_buf *cur_oid = &cur->buf;
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02002295
2296 if( cur_oid->len == usage_len &&
2297 memcmp( cur_oid->p, usage_oid, usage_len ) == 0 )
2298 {
2299 return( 0 );
2300 }
2301
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002302 if( MBEDTLS_OID_CMP( MBEDTLS_OID_ANY_EXTENDED_KEY_USAGE, cur_oid ) == 0 )
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02002303 return( 0 );
2304 }
2305
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002306 return( MBEDTLS_ERR_X509_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02002307}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002308#endif /* MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE */
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02002309
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002310#if defined(MBEDTLS_X509_CRL_PARSE_C)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002311/*
2312 * Return 1 if the certificate is revoked, or 0 otherwise.
2313 */
Manuel Pégourié-Gonnardc730ed32015-06-02 10:38:50 +01002314int mbedtls_x509_crt_is_revoked( const mbedtls_x509_crt *crt, const mbedtls_x509_crl *crl )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002315{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002316 const mbedtls_x509_crl_entry *cur = &crl->entry;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002317
2318 while( cur != NULL && cur->serial.len != 0 )
2319 {
2320 if( crt->serial.len == cur->serial.len &&
2321 memcmp( crt->serial.p, cur->serial.p, crt->serial.len ) == 0 )
2322 {
Raoul Strackxa4e86142020-06-15 17:03:13 +02002323 return( 1 );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002324 }
2325
2326 cur = cur->next;
2327 }
2328
2329 return( 0 );
2330}
2331
2332/*
Manuel Pégourié-Gonnardeeef9472016-02-22 11:36:55 +01002333 * Check that the given certificate is not revoked according to the CRL.
Manuel Pégourié-Gonnard08eacec2017-10-18 14:20:24 +02002334 * Skip validation if no CRL for the given CA is present.
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002335 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002336static int x509_crt_verifycrl( mbedtls_x509_crt *crt, mbedtls_x509_crt *ca,
Manuel Pégourié-Gonnard95051642015-06-15 10:39:46 +02002337 mbedtls_x509_crl *crl_list,
2338 const mbedtls_x509_crt_profile *profile )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002339{
2340 int flags = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002341 unsigned char hash[MBEDTLS_MD_MAX_SIZE];
2342 const mbedtls_md_info_t *md_info;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002343
2344 if( ca == NULL )
2345 return( flags );
2346
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002347 while( crl_list != NULL )
2348 {
2349 if( crl_list->version == 0 ||
Hanno Beckerb75ffb52018-11-02 09:19:54 +00002350 x509_name_cmp( &crl_list->issuer, &ca->subject ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002351 {
2352 crl_list = crl_list->next;
2353 continue;
2354 }
2355
2356 /*
Manuel Pégourié-Gonnard99d4f192014-04-08 15:10:07 +02002357 * Check if the CA is configured to sign CRLs
2358 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002359#if defined(MBEDTLS_X509_CHECK_KEY_USAGE)
Hanno Beckerb75ffb52018-11-02 09:19:54 +00002360 if( mbedtls_x509_crt_check_key_usage( ca,
2361 MBEDTLS_X509_KU_CRL_SIGN ) != 0 )
Manuel Pégourié-Gonnard99d4f192014-04-08 15:10:07 +02002362 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002363 flags |= MBEDTLS_X509_BADCRL_NOT_TRUSTED;
Manuel Pégourié-Gonnard99d4f192014-04-08 15:10:07 +02002364 break;
2365 }
2366#endif
2367
2368 /*
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002369 * Check if CRL is correctly signed by the trusted CA
2370 */
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +02002371 if( x509_profile_check_md_alg( profile, crl_list->sig_md ) != 0 )
2372 flags |= MBEDTLS_X509_BADCRL_BAD_MD;
2373
2374 if( x509_profile_check_pk_alg( profile, crl_list->sig_pk ) != 0 )
2375 flags |= MBEDTLS_X509_BADCRL_BAD_PK;
2376
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002377 md_info = mbedtls_md_info_from_type( crl_list->sig_md );
Manuel Pégourié-Gonnard329e78c2017-06-26 12:22:17 +02002378 if( mbedtls_md( md_info, crl_list->tbs.p, crl_list->tbs.len, hash ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002379 {
Manuel Pégourié-Gonnard329e78c2017-06-26 12:22:17 +02002380 /* Note: this can't happen except after an internal error */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002381 flags |= MBEDTLS_X509_BADCRL_NOT_TRUSTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002382 break;
2383 }
2384
Manuel Pégourié-Gonnard3f816912017-10-26 10:24:16 +02002385 if( x509_profile_check_key( profile, &ca->pk ) != 0 )
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +02002386 flags |= MBEDTLS_X509_BADCERT_BAD_KEY;
Manuel Pégourié-Gonnard95051642015-06-15 10:39:46 +02002387
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002388 if( mbedtls_pk_verify_ext( crl_list->sig_pk, crl_list->sig_opts, &ca->pk,
2389 crl_list->sig_md, hash, mbedtls_md_get_size( md_info ),
Manuel Pégourié-Gonnard53882022014-06-05 17:53:52 +02002390 crl_list->sig.p, crl_list->sig.len ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002391 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002392 flags |= MBEDTLS_X509_BADCRL_NOT_TRUSTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002393 break;
2394 }
2395
2396 /*
2397 * Check for validity of CRL (Do not drop out)
2398 */
Manuel Pégourié-Gonnardc730ed32015-06-02 10:38:50 +01002399 if( mbedtls_x509_time_is_past( &crl_list->next_update ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002400 flags |= MBEDTLS_X509_BADCRL_EXPIRED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002401
Manuel Pégourié-Gonnardc730ed32015-06-02 10:38:50 +01002402 if( mbedtls_x509_time_is_future( &crl_list->this_update ) )
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01002403 flags |= MBEDTLS_X509_BADCRL_FUTURE;
Manuel Pégourié-Gonnard95337652014-03-10 13:15:18 +01002404
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002405 /*
2406 * Check if certificate is revoked
2407 */
Manuel Pégourié-Gonnardc730ed32015-06-02 10:38:50 +01002408 if( mbedtls_x509_crt_is_revoked( crt, crl_list ) )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002409 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002410 flags |= MBEDTLS_X509_BADCERT_REVOKED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002411 break;
2412 }
2413
2414 crl_list = crl_list->next;
2415 }
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +02002416
Paul Bakkerd8bb8262014-06-17 14:06:49 +02002417 return( flags );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002418}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002419#endif /* MBEDTLS_X509_CRL_PARSE_C */
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002420
Manuel Pégourié-Gonnard88421242014-10-17 11:36:18 +02002421/*
Manuel Pégourié-Gonnardf82a4d52017-07-03 19:26:25 +02002422 * Check the signature of a certificate by its parent
2423 */
2424static int x509_crt_check_signature( const mbedtls_x509_crt *child,
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002425 mbedtls_x509_crt *parent,
2426 mbedtls_x509_crt_restart_ctx *rs_ctx )
Manuel Pégourié-Gonnardf82a4d52017-07-03 19:26:25 +02002427{
Manuel Pégourié-Gonnardf82a4d52017-07-03 19:26:25 +02002428 unsigned char hash[MBEDTLS_MD_MAX_SIZE];
Andrzej Kurekd4a65532018-10-31 06:18:39 -04002429 size_t hash_len;
2430#if !defined(MBEDTLS_USE_PSA_CRYPTO)
2431 const mbedtls_md_info_t *md_info;
Manuel Pégourié-Gonnardf82a4d52017-07-03 19:26:25 +02002432 md_info = mbedtls_md_info_from_type( child->sig_md );
Andrzej Kurekd4a65532018-10-31 06:18:39 -04002433 hash_len = mbedtls_md_get_size( md_info );
Andrzej Kurek8b38ff52018-11-20 03:20:09 -05002434
Andrzej Kurekd4a65532018-10-31 06:18:39 -04002435 /* Note: hash errors can happen only after an internal error */
Manuel Pégourié-Gonnardf82a4d52017-07-03 19:26:25 +02002436 if( mbedtls_md( md_info, child->tbs.p, child->tbs.len, hash ) != 0 )
Andrzej Kurekd4a65532018-10-31 06:18:39 -04002437 return( -1 );
2438#else
Jaeden Amero34973232019-02-20 10:32:28 +00002439 psa_hash_operation_t hash_operation = PSA_HASH_OPERATION_INIT;
Andrzej Kurekd4a65532018-10-31 06:18:39 -04002440 psa_algorithm_t hash_alg = mbedtls_psa_translate_md( child->sig_md );
2441
2442 if( psa_hash_setup( &hash_operation, hash_alg ) != PSA_SUCCESS )
2443 return( -1 );
2444
2445 if( psa_hash_update( &hash_operation, child->tbs.p, child->tbs.len )
2446 != PSA_SUCCESS )
Manuel Pégourié-Gonnardf82a4d52017-07-03 19:26:25 +02002447 {
Manuel Pégourié-Gonnardf82a4d52017-07-03 19:26:25 +02002448 return( -1 );
2449 }
2450
Andrzej Kurekd4a65532018-10-31 06:18:39 -04002451 if( psa_hash_finish( &hash_operation, hash, sizeof( hash ), &hash_len )
2452 != PSA_SUCCESS )
2453 {
Andrzej Kurekd4a65532018-10-31 06:18:39 -04002454 return( -1 );
2455 }
2456#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002457 /* Skip expensive computation on obvious mismatch */
2458 if( ! mbedtls_pk_can_do( &parent->pk, child->sig_pk ) )
Manuel Pégourié-Gonnardf82a4d52017-07-03 19:26:25 +02002459 return( -1 );
Manuel Pégourié-Gonnardf82a4d52017-07-03 19:26:25 +02002460
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002461#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002462 if( rs_ctx != NULL && child->sig_pk == MBEDTLS_PK_ECDSA )
2463 {
2464 return( mbedtls_pk_verify_restartable( &parent->pk,
Andrzej Kurekd4a65532018-10-31 06:18:39 -04002465 child->sig_md, hash, hash_len,
Manuel Pégourié-Gonnard15d7df22017-08-17 14:33:31 +02002466 child->sig.p, child->sig.len, &rs_ctx->pk ) );
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002467 }
2468#else
2469 (void) rs_ctx;
2470#endif
2471
2472 return( mbedtls_pk_verify_ext( child->sig_pk, child->sig_opts, &parent->pk,
Andrzej Kurekd4a65532018-10-31 06:18:39 -04002473 child->sig_md, hash, hash_len,
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002474 child->sig.p, child->sig.len ) );
Manuel Pégourié-Gonnardf82a4d52017-07-03 19:26:25 +02002475}
2476
2477/*
Manuel Pégourié-Gonnard312010e2014-04-09 14:30:11 +02002478 * Check if 'parent' is a suitable parent (signing CA) for 'child'.
2479 * Return 0 if yes, -1 if not.
Manuel Pégourié-Gonnardd249b7a2014-06-24 11:49:16 +02002480 *
2481 * top means parent is a locally-trusted certificate
Manuel Pégourié-Gonnard3fed0b32014-04-08 13:18:01 +02002482 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002483static int x509_crt_check_parent( const mbedtls_x509_crt *child,
2484 const mbedtls_x509_crt *parent,
Manuel Pégourié-Gonnard27e94792017-07-04 00:49:31 +02002485 int top )
Manuel Pégourié-Gonnard3fed0b32014-04-08 13:18:01 +02002486{
Manuel Pégourié-Gonnardd249b7a2014-06-24 11:49:16 +02002487 int need_ca_bit;
2488
Manuel Pégourié-Gonnardc4eff162014-06-19 12:18:08 +02002489 /* Parent must be the issuer */
Manuel Pégourié-Gonnardef9a6ae2014-10-17 12:25:12 +02002490 if( x509_name_cmp( &child->issuer, &parent->subject ) != 0 )
Manuel Pégourié-Gonnard312010e2014-04-09 14:30:11 +02002491 return( -1 );
Manuel Pégourié-Gonnard3fed0b32014-04-08 13:18:01 +02002492
Manuel Pégourié-Gonnardd249b7a2014-06-24 11:49:16 +02002493 /* Parent must have the basicConstraints CA bit set as a general rule */
2494 need_ca_bit = 1;
2495
2496 /* Exception: v1/v2 certificates that are locally trusted. */
2497 if( top && parent->version < 3 )
2498 need_ca_bit = 0;
2499
Manuel Pégourié-Gonnardd249b7a2014-06-24 11:49:16 +02002500 if( need_ca_bit && ! parent->ca_istrue )
2501 return( -1 );
2502
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002503#if defined(MBEDTLS_X509_CHECK_KEY_USAGE)
Manuel Pégourié-Gonnardd249b7a2014-06-24 11:49:16 +02002504 if( need_ca_bit &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002505 mbedtls_x509_crt_check_key_usage( parent, MBEDTLS_X509_KU_KEY_CERT_SIGN ) != 0 )
Manuel Pégourié-Gonnardc4eff162014-06-19 12:18:08 +02002506 {
2507 return( -1 );
2508 }
Manuel Pégourié-Gonnard312010e2014-04-09 14:30:11 +02002509#endif
2510
2511 return( 0 );
Manuel Pégourié-Gonnard3fed0b32014-04-08 13:18:01 +02002512}
2513
Manuel Pégourié-Gonnard35407c72017-06-29 10:45:25 +02002514/*
Manuel Pégourié-Gonnard3e329b82017-06-29 12:55:27 +02002515 * Find a suitable parent for child in candidates, or return NULL.
2516 *
2517 * Here suitable is defined as:
Manuel Pégourié-Gonnard2f09d592017-07-03 18:30:43 +02002518 * 1. subject name matches child's issuer
2519 * 2. if necessary, the CA bit is set and key usage allows signing certs
2520 * 3. for trusted roots, the signature is correct
Manuel Pégourié-Gonnardbe4ff422017-07-14 12:04:14 +02002521 * (for intermediates, the signature is checked and the result reported)
Manuel Pégourié-Gonnard2f09d592017-07-03 18:30:43 +02002522 * 4. pathlen constraints are satisfied
Manuel Pégourié-Gonnard3e329b82017-06-29 12:55:27 +02002523 *
Manuel Pégourié-Gonnard562df402017-08-08 18:09:14 +02002524 * If there's a suitable candidate which is also time-valid, return the first
2525 * such. Otherwise, return the first suitable candidate (or NULL if there is
2526 * none).
Manuel Pégourié-Gonnard3e329b82017-06-29 12:55:27 +02002527 *
2528 * The rationale for this rule is that someone could have a list of trusted
2529 * roots with two versions on the same root with different validity periods.
2530 * (At least one user reported having such a list and wanted it to just work.)
2531 * The reason we don't just require time-validity is that generally there is
2532 * only one version, and if it's expired we want the flags to state that
2533 * rather than NOT_TRUSTED, as would be the case if we required it here.
Manuel Pégourié-Gonnard2f09d592017-07-03 18:30:43 +02002534 *
2535 * The rationale for rule 3 (signature for trusted roots) is that users might
2536 * have two versions of the same CA with different keys in their list, and the
Manuel Pégourié-Gonnardbdc54402017-07-04 00:33:39 +02002537 * way we select the correct one is by checking the signature (as we don't
2538 * rely on key identifier extensions). (This is one way users might choose to
2539 * handle key rollover, another relies on self-issued certs, see [SIRO].)
Manuel Pégourié-Gonnard98a67782017-08-17 10:52:20 +02002540 *
2541 * Arguments:
Manuel Pégourié-Gonnarde57d7432018-03-07 10:00:57 +01002542 * - [in] child: certificate for which we're looking for a parent
2543 * - [in] candidates: chained list of potential parents
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +02002544 * - [out] r_parent: parent found (or NULL)
2545 * - [out] r_signature_is_good: 1 if child signature by parent is valid, or 0
Manuel Pégourié-Gonnarde57d7432018-03-07 10:00:57 +01002546 * - [in] top: 1 if candidates consists of trusted roots, ie we're at the top
2547 * of the chain, 0 otherwise
2548 * - [in] path_cnt: number of intermediates seen so far
2549 * - [in] self_cnt: number of self-signed intermediates seen so far
2550 * (will never be greater than path_cnt)
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +02002551 * - [in-out] rs_ctx: context for restarting operations
Manuel Pégourié-Gonnarde57d7432018-03-07 10:00:57 +01002552 *
2553 * Return value:
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +02002554 * - 0 on success
2555 * - MBEDTLS_ERR_ECP_IN_PROGRESS otherwise
Manuel Pégourié-Gonnard2f1c33d2017-06-29 12:27:23 +02002556 */
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002557static int x509_crt_find_parent_in(
2558 mbedtls_x509_crt *child,
2559 mbedtls_x509_crt *candidates,
2560 mbedtls_x509_crt **r_parent,
2561 int *r_signature_is_good,
2562 int top,
Manuel Pégourié-Gonnardbb216bd2017-08-28 13:25:55 +02002563 unsigned path_cnt,
2564 unsigned self_cnt,
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002565 mbedtls_x509_crt_restart_ctx *rs_ctx )
Manuel Pégourié-Gonnard2f1c33d2017-06-29 12:27:23 +02002566{
Janos Follath865b3eb2019-12-16 11:46:15 +00002567 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002568 mbedtls_x509_crt *parent, *fallback_parent;
Benjamin Kier36050732019-05-30 14:49:17 -04002569 int signature_is_good = 0, fallback_signature_is_good;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002570
2571#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard3627a8b2017-08-23 11:20:48 +02002572 /* did we have something in progress? */
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002573 if( rs_ctx != NULL && rs_ctx->parent != NULL )
2574 {
Manuel Pégourié-Gonnard3627a8b2017-08-23 11:20:48 +02002575 /* restore saved state */
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002576 parent = rs_ctx->parent;
2577 fallback_parent = rs_ctx->fallback_parent;
Manuel Pégourié-Gonnard78d7e8c2018-07-02 12:33:14 +02002578 fallback_signature_is_good = rs_ctx->fallback_signature_is_good;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002579
Manuel Pégourié-Gonnard3627a8b2017-08-23 11:20:48 +02002580 /* clear saved state */
2581 rs_ctx->parent = NULL;
2582 rs_ctx->fallback_parent = NULL;
Manuel Pégourié-Gonnard78d7e8c2018-07-02 12:33:14 +02002583 rs_ctx->fallback_signature_is_good = 0;
Manuel Pégourié-Gonnard3627a8b2017-08-23 11:20:48 +02002584
2585 /* resume where we left */
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002586 goto check_signature;
2587 }
2588#endif
2589
2590 fallback_parent = NULL;
Manuel Pégourié-Gonnard78d7e8c2018-07-02 12:33:14 +02002591 fallback_signature_is_good = 0;
Manuel Pégourié-Gonnard2f1c33d2017-06-29 12:27:23 +02002592
Manuel Pégourié-Gonnard2f1c33d2017-06-29 12:27:23 +02002593 for( parent = candidates; parent != NULL; parent = parent->next )
2594 {
Manuel Pégourié-Gonnard2f09d592017-07-03 18:30:43 +02002595 /* basic parenting skills (name, CA bit, key usage) */
Manuel Pégourié-Gonnard27e94792017-07-04 00:49:31 +02002596 if( x509_crt_check_parent( child, parent, top ) != 0 )
Manuel Pégourié-Gonnard2f1c33d2017-06-29 12:27:23 +02002597 continue;
2598
Manuel Pégourié-Gonnard9c6118c2017-06-29 12:38:42 +02002599 /* +1 because stored max_pathlen is 1 higher that the actual value */
2600 if( parent->max_pathlen > 0 &&
Manuel Pégourié-Gonnardbb216bd2017-08-28 13:25:55 +02002601 (size_t) parent->max_pathlen < 1 + path_cnt - self_cnt )
Manuel Pégourié-Gonnard9c6118c2017-06-29 12:38:42 +02002602 {
2603 continue;
2604 }
2605
Manuel Pégourié-Gonnard2f09d592017-07-03 18:30:43 +02002606 /* Signature */
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002607#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
2608check_signature:
2609#endif
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002610 ret = x509_crt_check_signature( child, parent, rs_ctx );
2611
2612#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002613 if( rs_ctx != NULL && ret == MBEDTLS_ERR_ECP_IN_PROGRESS )
2614 {
2615 /* save state */
2616 rs_ctx->parent = parent;
2617 rs_ctx->fallback_parent = fallback_parent;
Manuel Pégourié-Gonnard78d7e8c2018-07-02 12:33:14 +02002618 rs_ctx->fallback_signature_is_good = fallback_signature_is_good;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002619
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002620 return( ret );
2621 }
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002622#else
2623 (void) ret;
2624#endif
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002625
2626 signature_is_good = ret == 0;
2627 if( top && ! signature_is_good )
Manuel Pégourié-Gonnardf82a4d52017-07-03 19:26:25 +02002628 continue;
Manuel Pégourié-Gonnard2f09d592017-07-03 18:30:43 +02002629
Manuel Pégourié-Gonnard562df402017-08-08 18:09:14 +02002630 /* optional time check */
Manuel Pégourié-Gonnard3e329b82017-06-29 12:55:27 +02002631 if( mbedtls_x509_time_is_past( &parent->valid_to ) ||
2632 mbedtls_x509_time_is_future( &parent->valid_from ) )
2633 {
Manuel Pégourié-Gonnardbe4ff422017-07-14 12:04:14 +02002634 if( fallback_parent == NULL )
2635 {
2636 fallback_parent = parent;
Manuel Pégourié-Gonnard78d7e8c2018-07-02 12:33:14 +02002637 fallback_signature_is_good = signature_is_good;
Manuel Pégourié-Gonnardbe4ff422017-07-14 12:04:14 +02002638 }
Manuel Pégourié-Gonnard3e329b82017-06-29 12:55:27 +02002639
2640 continue;
2641 }
2642
Andy Gross1f627142019-01-30 10:25:53 -06002643 *r_parent = parent;
2644 *r_signature_is_good = signature_is_good;
2645
Manuel Pégourié-Gonnard2f1c33d2017-06-29 12:27:23 +02002646 break;
2647 }
2648
Andy Gross1f627142019-01-30 10:25:53 -06002649 if( parent == NULL )
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002650 {
2651 *r_parent = fallback_parent;
Manuel Pégourié-Gonnard78d7e8c2018-07-02 12:33:14 +02002652 *r_signature_is_good = fallback_signature_is_good;
Manuel Pégourié-Gonnardbe4ff422017-07-14 12:04:14 +02002653 }
Manuel Pégourié-Gonnard3e329b82017-06-29 12:55:27 +02002654
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002655 return( 0 );
Manuel Pégourié-Gonnard2f1c33d2017-06-29 12:27:23 +02002656}
2657
2658/*
Manuel Pégourié-Gonnard63686122017-07-04 01:01:39 +02002659 * Find a parent in trusted CAs or the provided chain, or return NULL.
2660 *
2661 * Searches in trusted CAs first, and return the first suitable parent found
2662 * (see find_parent_in() for definition of suitable).
Manuel Pégourié-Gonnard98a67782017-08-17 10:52:20 +02002663 *
2664 * Arguments:
Manuel Pégourié-Gonnarde57d7432018-03-07 10:00:57 +01002665 * - [in] child: certificate for which we're looking for a parent, followed
2666 * by a chain of possible intermediates
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +02002667 * - [in] trust_ca: list of locally trusted certificates
2668 * - [out] parent: parent found (or NULL)
2669 * - [out] parent_is_trusted: 1 if returned `parent` is trusted, or 0
2670 * - [out] signature_is_good: 1 if child signature by parent is valid, or 0
2671 * - [in] path_cnt: number of links in the chain so far (EE -> ... -> child)
2672 * - [in] self_cnt: number of self-signed certs in the chain so far
Manuel Pégourié-Gonnarde57d7432018-03-07 10:00:57 +01002673 * (will always be no greater than path_cnt)
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +02002674 * - [in-out] rs_ctx: context for restarting operations
Manuel Pégourié-Gonnarde57d7432018-03-07 10:00:57 +01002675 *
2676 * Return value:
Manuel Pégourié-Gonnardda19f4c2018-06-12 12:40:54 +02002677 * - 0 on success
2678 * - MBEDTLS_ERR_ECP_IN_PROGRESS otherwise
Manuel Pégourié-Gonnard63686122017-07-04 01:01:39 +02002679 */
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002680static int x509_crt_find_parent(
2681 mbedtls_x509_crt *child,
2682 mbedtls_x509_crt *trust_ca,
2683 mbedtls_x509_crt **parent,
2684 int *parent_is_trusted,
2685 int *signature_is_good,
Manuel Pégourié-Gonnardbb216bd2017-08-28 13:25:55 +02002686 unsigned path_cnt,
2687 unsigned self_cnt,
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002688 mbedtls_x509_crt_restart_ctx *rs_ctx )
Manuel Pégourié-Gonnard63686122017-07-04 01:01:39 +02002689{
Janos Follath865b3eb2019-12-16 11:46:15 +00002690 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard18547b52017-08-14 16:11:43 +02002691 mbedtls_x509_crt *search_list;
Manuel Pégourié-Gonnard63686122017-07-04 01:01:39 +02002692
Manuel Pégourié-Gonnard63686122017-07-04 01:01:39 +02002693 *parent_is_trusted = 1;
Manuel Pégourié-Gonnard18547b52017-08-14 16:11:43 +02002694
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002695#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard3627a8b2017-08-23 11:20:48 +02002696 /* restore then clear saved state if we have some stored */
2697 if( rs_ctx != NULL && rs_ctx->parent_is_trusted != -1 )
2698 {
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002699 *parent_is_trusted = rs_ctx->parent_is_trusted;
Manuel Pégourié-Gonnard3627a8b2017-08-23 11:20:48 +02002700 rs_ctx->parent_is_trusted = -1;
2701 }
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002702#endif
2703
Manuel Pégourié-Gonnard18547b52017-08-14 16:11:43 +02002704 while( 1 ) {
2705 search_list = *parent_is_trusted ? trust_ca : child->next;
2706
2707 ret = x509_crt_find_parent_in( child, search_list,
2708 parent, signature_is_good,
2709 *parent_is_trusted,
2710 path_cnt, self_cnt, rs_ctx );
Manuel Pégourié-Gonnard63686122017-07-04 01:01:39 +02002711
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002712#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002713 if( rs_ctx != NULL && ret == MBEDTLS_ERR_ECP_IN_PROGRESS )
2714 {
2715 /* save state */
2716 rs_ctx->parent_is_trusted = *parent_is_trusted;
Manuel Pégourié-Gonnard18547b52017-08-14 16:11:43 +02002717 return( ret );
2718 }
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002719#else
2720 (void) ret;
2721#endif
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002722
Manuel Pégourié-Gonnard18547b52017-08-14 16:11:43 +02002723 /* stop here if found or already in second iteration */
2724 if( *parent != NULL || *parent_is_trusted == 0 )
2725 break;
Manuel Pégourié-Gonnard63686122017-07-04 01:01:39 +02002726
Manuel Pégourié-Gonnard18547b52017-08-14 16:11:43 +02002727 /* prepare second iteration */
2728 *parent_is_trusted = 0;
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002729 }
Manuel Pégourié-Gonnard18547b52017-08-14 16:11:43 +02002730
2731 /* extra precaution against mistakes in the caller */
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05002732 if( *parent == NULL )
Manuel Pégourié-Gonnard18547b52017-08-14 16:11:43 +02002733 {
Manuel Pégourié-Gonnarda5a3e402018-10-16 11:27:23 +02002734 *parent_is_trusted = 0;
2735 *signature_is_good = 0;
Manuel Pégourié-Gonnard18547b52017-08-14 16:11:43 +02002736 }
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002737
2738 return( 0 );
Manuel Pégourié-Gonnard63686122017-07-04 01:01:39 +02002739}
2740
2741/*
Manuel Pégourié-Gonnard27e94792017-07-04 00:49:31 +02002742 * Check if an end-entity certificate is locally trusted
2743 *
2744 * Currently we require such certificates to be self-signed (actually only
2745 * check for self-issued as self-signatures are not checked)
2746 */
2747static int x509_crt_check_ee_locally_trusted(
2748 mbedtls_x509_crt *crt,
2749 mbedtls_x509_crt *trust_ca )
2750{
2751 mbedtls_x509_crt *cur;
2752
2753 /* must be self-issued */
2754 if( x509_name_cmp( &crt->issuer, &crt->subject ) != 0 )
2755 return( -1 );
2756
2757 /* look for an exact match with trusted cert */
2758 for( cur = trust_ca; cur != NULL; cur = cur->next )
2759 {
2760 if( crt->raw.len == cur->raw.len &&
2761 memcmp( crt->raw.p, cur->raw.p, crt->raw.len ) == 0 )
2762 {
2763 return( 0 );
2764 }
2765 }
2766
2767 /* too bad */
2768 return( -1 );
2769}
2770
2771/*
Manuel Pégourié-Gonnardf86f4912017-07-05 16:43:44 +02002772 * Build and verify a certificate chain
Manuel Pégourié-Gonnard35407c72017-06-29 10:45:25 +02002773 *
Manuel Pégourié-Gonnardf86f4912017-07-05 16:43:44 +02002774 * Given a peer-provided list of certificates EE, C1, ..., Cn and
2775 * a list of trusted certs R1, ... Rp, try to build and verify a chain
Manuel Pégourié-Gonnard562df402017-08-08 18:09:14 +02002776 * EE, Ci1, ... Ciq [, Rj]
Manuel Pégourié-Gonnardf86f4912017-07-05 16:43:44 +02002777 * such that every cert in the chain is a child of the next one,
2778 * jumping to a trusted root as early as possible.
Manuel Pégourié-Gonnardbdc54402017-07-04 00:33:39 +02002779 *
Manuel Pégourié-Gonnardf86f4912017-07-05 16:43:44 +02002780 * Verify that chain and return it with flags for all issues found.
2781 *
2782 * Special cases:
2783 * - EE == Rj -> return a one-element list containing it
2784 * - EE, Ci1, ..., Ciq cannot be continued with a trusted root
2785 * -> return that chain with NOT_TRUSTED set on Ciq
Manuel Pégourié-Gonnardbdc54402017-07-04 00:33:39 +02002786 *
Manuel Pégourié-Gonnardd19a41d2017-07-14 11:05:59 +02002787 * Tests for (aspects of) this function should include at least:
2788 * - trusted EE
2789 * - EE -> trusted root
Antonin Décimo36e89b52019-01-23 15:24:37 +01002790 * - EE -> intermediate CA -> trusted root
Manuel Pégourié-Gonnardd19a41d2017-07-14 11:05:59 +02002791 * - if relevant: EE untrusted
2792 * - if relevant: EE -> intermediate, untrusted
2793 * with the aspect under test checked at each relevant level (EE, int, root).
2794 * For some aspects longer chains are required, but usually length 2 is
2795 * enough (but length 1 is not in general).
2796 *
Manuel Pégourié-Gonnardbdc54402017-07-04 00:33:39 +02002797 * Arguments:
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002798 * - [in] crt: the cert list EE, C1, ..., Cn
2799 * - [in] trust_ca: the trusted list R1, ..., Rp
2800 * - [in] ca_crl, profile: as in verify_with_profile()
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02002801 * - [out] ver_chain: the built and verified chain
Manuel Pégourié-Gonnarda9688432017-08-23 11:23:59 +02002802 * Only valid when return value is 0, may contain garbage otherwise!
2803 * Restart note: need not be the same when calling again to resume.
Manuel Pégourié-Gonnard98a67782017-08-17 10:52:20 +02002804 * - [in-out] rs_ctx: context for restarting operations
Manuel Pégourié-Gonnardf86f4912017-07-05 16:43:44 +02002805 *
2806 * Return value:
2807 * - non-zero if the chain could not be fully built and examined
2808 * - 0 is the chain was successfully built and examined,
2809 * even if it was found to be invalid
Manuel Pégourié-Gonnard35407c72017-06-29 10:45:25 +02002810 */
Manuel Pégourié-Gonnardbdc54402017-07-04 00:33:39 +02002811static int x509_crt_verify_chain(
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002812 mbedtls_x509_crt *crt,
2813 mbedtls_x509_crt *trust_ca,
2814 mbedtls_x509_crl *ca_crl,
Hanno Becker3116fb32019-03-28 13:34:42 +00002815 mbedtls_x509_crt_ca_cb_t f_ca_cb,
2816 void *p_ca_cb,
Manuel Pégourié-Gonnard95051642015-06-15 10:39:46 +02002817 const mbedtls_x509_crt_profile *profile,
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02002818 mbedtls_x509_crt_verify_chain *ver_chain,
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002819 mbedtls_x509_crt_restart_ctx *rs_ctx )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002820{
Manuel Pégourié-Gonnarda9688432017-08-23 11:23:59 +02002821 /* Don't initialize any of those variables here, so that the compiler can
2822 * catch potential issues with jumping ahead when restarting */
Janos Follath865b3eb2019-12-16 11:46:15 +00002823 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardf86f4912017-07-05 16:43:44 +02002824 uint32_t *flags;
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02002825 mbedtls_x509_crt_verify_chain_item *cur;
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002826 mbedtls_x509_crt *child;
Manuel Pégourié-Gonnard58dcd2d2017-07-03 21:35:04 +02002827 mbedtls_x509_crt *parent;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002828 int parent_is_trusted;
2829 int child_is_trusted;
2830 int signature_is_good;
Manuel Pégourié-Gonnardbb216bd2017-08-28 13:25:55 +02002831 unsigned self_cnt;
Hanno Beckerf53893b2019-03-28 13:45:55 +00002832 mbedtls_x509_crt *cur_trust_ca = NULL;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002833
2834#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
2835 /* resume if we had an operation in progress */
Manuel Pégourié-Gonnarddaf04912017-08-23 12:32:19 +02002836 if( rs_ctx != NULL && rs_ctx->in_progress == x509_crt_rs_find_parent )
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002837 {
Manuel Pégourié-Gonnard3627a8b2017-08-23 11:20:48 +02002838 /* restore saved state */
Manuel Pégourié-Gonnarda9688432017-08-23 11:23:59 +02002839 *ver_chain = rs_ctx->ver_chain; /* struct copy */
Manuel Pégourié-Gonnarddaf04912017-08-23 12:32:19 +02002840 self_cnt = rs_ctx->self_cnt;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002841
Manuel Pégourié-Gonnarddaf04912017-08-23 12:32:19 +02002842 /* restore derived state */
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002843 cur = &ver_chain->items[ver_chain->len - 1];
Manuel Pégourié-Gonnarddaf04912017-08-23 12:32:19 +02002844 child = cur->crt;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002845 flags = &cur->flags;
2846
2847 goto find_parent;
2848 }
2849#endif /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */
Manuel Pégourié-Gonnard8f8c2822017-07-03 21:25:10 +02002850
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002851 child = crt;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002852 self_cnt = 0;
2853 parent_is_trusted = 0;
2854 child_is_trusted = 0;
Manuel Pégourié-Gonnardf86f4912017-07-05 16:43:44 +02002855
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002856 while( 1 ) {
2857 /* Add certificate to the verification chain */
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02002858 cur = &ver_chain->items[ver_chain->len];
2859 cur->crt = child;
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +02002860 cur->flags = 0;
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02002861 ver_chain->len++;
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +02002862 flags = &cur->flags;
Manuel Pégourié-Gonnard66fac752017-07-03 21:39:21 +02002863
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002864 /* Check time-validity (all certificates) */
2865 if( mbedtls_x509_time_is_past( &child->valid_to ) )
2866 *flags |= MBEDTLS_X509_BADCERT_EXPIRED;
Manuel Pégourié-Gonnard66fac752017-07-03 21:39:21 +02002867
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002868 if( mbedtls_x509_time_is_future( &child->valid_from ) )
2869 *flags |= MBEDTLS_X509_BADCERT_FUTURE;
Manuel Pégourié-Gonnardcb396102017-07-04 00:00:24 +02002870
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002871 /* Stop here for trusted roots (but not for trusted EE certs) */
2872 if( child_is_trusted )
2873 return( 0 );
Manuel Pégourié-Gonnard66fac752017-07-03 21:39:21 +02002874
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002875 /* Check signature algorithm: MD & PK algs */
2876 if( x509_profile_check_md_alg( profile, child->sig_md ) != 0 )
2877 *flags |= MBEDTLS_X509_BADCERT_BAD_MD;
Manuel Pégourié-Gonnard66fac752017-07-03 21:39:21 +02002878
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002879 if( x509_profile_check_pk_alg( profile, child->sig_pk ) != 0 )
2880 *flags |= MBEDTLS_X509_BADCERT_BAD_PK;
Manuel Pégourié-Gonnard27e94792017-07-04 00:49:31 +02002881
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002882 /* Special case: EE certs that are locally trusted */
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02002883 if( ver_chain->len == 1 &&
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002884 x509_crt_check_ee_locally_trusted( child, trust_ca ) == 0 )
2885 {
2886 return( 0 );
2887 }
Manuel Pégourié-Gonnard8f8c2822017-07-03 21:25:10 +02002888
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002889#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
2890find_parent:
2891#endif
Hanno Beckerf53893b2019-03-28 13:45:55 +00002892
2893 /* Obtain list of potential trusted signers from CA callback,
2894 * or use statically provided list. */
2895#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
2896 if( f_ca_cb != NULL )
2897 {
2898 mbedtls_x509_crt_free( ver_chain->trust_ca_cb_result );
2899 mbedtls_free( ver_chain->trust_ca_cb_result );
2900 ver_chain->trust_ca_cb_result = NULL;
2901
2902 ret = f_ca_cb( p_ca_cb, child, &ver_chain->trust_ca_cb_result );
2903 if( ret != 0 )
2904 return( MBEDTLS_ERR_X509_FATAL_ERROR );
2905
2906 cur_trust_ca = ver_chain->trust_ca_cb_result;
2907 }
2908 else
2909#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
2910 {
2911 ((void) f_ca_cb);
2912 ((void) p_ca_cb);
2913 cur_trust_ca = trust_ca;
2914 }
2915
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002916 /* Look for a parent in trusted CAs or up the chain */
Hanno Beckerf53893b2019-03-28 13:45:55 +00002917 ret = x509_crt_find_parent( child, cur_trust_ca, &parent,
Manuel Pégourié-Gonnardbe4ff422017-07-14 12:04:14 +02002918 &parent_is_trusted, &signature_is_good,
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02002919 ver_chain->len - 1, self_cnt, rs_ctx );
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002920
2921#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002922 if( rs_ctx != NULL && ret == MBEDTLS_ERR_ECP_IN_PROGRESS )
2923 {
2924 /* save state */
Manuel Pégourié-Gonnarddaf04912017-08-23 12:32:19 +02002925 rs_ctx->in_progress = x509_crt_rs_find_parent;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002926 rs_ctx->self_cnt = self_cnt;
Manuel Pégourié-Gonnarda9688432017-08-23 11:23:59 +02002927 rs_ctx->ver_chain = *ver_chain; /* struct copy */
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002928
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002929 return( ret );
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02002930 }
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02002931#else
2932 (void) ret;
2933#endif
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002934
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002935 /* No parent? We're done here */
2936 if( parent == NULL )
2937 {
2938 *flags |= MBEDTLS_X509_BADCERT_NOT_TRUSTED;
2939 return( 0 );
2940 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002941
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002942 /* Count intermediate self-issued (not necessarily self-signed) certs.
2943 * These can occur with some strategies for key rollover, see [SIRO],
2944 * and should be excluded from max_pathlen checks. */
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02002945 if( ver_chain->len != 1 &&
Manuel Pégourié-Gonnard24611f92017-08-09 10:28:07 +02002946 x509_name_cmp( &child->issuer, &child->subject ) == 0 )
2947 {
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002948 self_cnt++;
Manuel Pégourié-Gonnard24611f92017-08-09 10:28:07 +02002949 }
Manuel Pégourié-Gonnardfd6c85c2014-11-20 16:34:20 +01002950
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002951 /* path_cnt is 0 for the first intermediate CA,
2952 * and if parent is trusted it's not an intermediate CA */
2953 if( ! parent_is_trusted &&
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02002954 ver_chain->len > MBEDTLS_X509_MAX_INTERMEDIATE_CA )
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002955 {
2956 /* return immediately to avoid overflow the chain array */
2957 return( MBEDTLS_ERR_X509_FATAL_ERROR );
2958 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002959
Manuel Pégourié-Gonnard98a67782017-08-17 10:52:20 +02002960 /* signature was checked while searching parent */
Manuel Pégourié-Gonnardbe4ff422017-07-14 12:04:14 +02002961 if( ! signature_is_good )
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002962 *flags |= MBEDTLS_X509_BADCERT_NOT_TRUSTED;
2963
2964 /* check size of signing key */
Manuel Pégourié-Gonnard3f816912017-10-26 10:24:16 +02002965 if( x509_profile_check_key( profile, &parent->pk ) != 0 )
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002966 *flags |= MBEDTLS_X509_BADCERT_BAD_KEY;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002967
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002968#if defined(MBEDTLS_X509_CRL_PARSE_C)
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002969 /* Check trusted CA's CRL for the given crt */
Manuel Pégourié-Gonnard562df402017-08-08 18:09:14 +02002970 *flags |= x509_crt_verifycrl( child, parent, ca_crl, profile );
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002971#else
2972 (void) ca_crl;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002973#endif
2974
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002975 /* prepare for next iteration */
2976 child = parent;
2977 parent = NULL;
2978 child_is_trusted = parent_is_trusted;
Manuel Pégourié-Gonnardbe4ff422017-07-14 12:04:14 +02002979 signature_is_good = 0;
Manuel Pégourié-Gonnardce6e52f2017-07-05 17:05:03 +02002980 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002981}
2982
2983/*
Manuel Pégourié-Gonnarda468eb12017-07-04 01:31:59 +02002984 * Check for CN match
2985 */
2986static int x509_crt_check_cn( const mbedtls_x509_buf *name,
2987 const char *cn, size_t cn_len )
2988{
2989 /* try exact match */
2990 if( name->len == cn_len &&
2991 x509_memcasecmp( cn, name->p, cn_len ) == 0 )
2992 {
2993 return( 0 );
2994 }
2995
2996 /* try wildcard match */
Manuel Pégourié-Gonnard900fba62017-10-18 14:28:11 +02002997 if( x509_check_wildcard( cn, name ) == 0 )
Manuel Pégourié-Gonnarda468eb12017-07-04 01:31:59 +02002998 {
2999 return( 0 );
3000 }
3001
3002 return( -1 );
3003}
3004
3005/*
Manuel Pégourié-Gonnardf3e4bd82020-07-21 13:22:41 +02003006 * Check for SAN match, see RFC 5280 Section 4.2.1.6
3007 */
3008static int x509_crt_check_san( const mbedtls_x509_buf *name,
3009 const char *cn, size_t cn_len )
3010{
3011 const unsigned char san_type = (unsigned char) name->tag &
3012 MBEDTLS_ASN1_TAG_VALUE_MASK;
3013
3014 /* dNSName */
3015 if( san_type == MBEDTLS_X509_SAN_DNS_NAME )
3016 return( x509_crt_check_cn( name, cn, cn_len ) );
3017
3018 /* (We may handle other types here later.) */
3019
3020 /* Unrecognized type */
3021 return( -1 );
3022}
3023
3024/*
Manuel Pégourié-Gonnard1300e992017-07-04 01:13:44 +02003025 * Verify the requested CN - only call this if cn is not NULL!
3026 */
Manuel Pégourié-Gonnarda468eb12017-07-04 01:31:59 +02003027static void x509_crt_verify_name( const mbedtls_x509_crt *crt,
Manuel Pégourié-Gonnard1300e992017-07-04 01:13:44 +02003028 const char *cn,
3029 uint32_t *flags )
3030{
Manuel Pégourié-Gonnarda468eb12017-07-04 01:31:59 +02003031 const mbedtls_x509_name *name;
3032 const mbedtls_x509_sequence *cur;
3033 size_t cn_len = strlen( cn );
Manuel Pégourié-Gonnard1300e992017-07-04 01:13:44 +02003034
3035 if( crt->ext_types & MBEDTLS_X509_EXT_SUBJECT_ALT_NAME )
3036 {
Manuel Pégourié-Gonnarda468eb12017-07-04 01:31:59 +02003037 for( cur = &crt->subject_alt_names; cur != NULL; cur = cur->next )
Manuel Pégourié-Gonnard1300e992017-07-04 01:13:44 +02003038 {
Manuel Pégourié-Gonnardf3e4bd82020-07-21 13:22:41 +02003039 if( x509_crt_check_san( &cur->buf, cn, cn_len ) == 0 )
Manuel Pégourié-Gonnard1300e992017-07-04 01:13:44 +02003040 break;
Manuel Pégourié-Gonnard1300e992017-07-04 01:13:44 +02003041 }
3042
3043 if( cur == NULL )
3044 *flags |= MBEDTLS_X509_BADCERT_CN_MISMATCH;
3045 }
3046 else
3047 {
Manuel Pégourié-Gonnard08eacec2017-10-18 14:20:24 +02003048 for( name = &crt->subject; name != NULL; name = name->next )
Manuel Pégourié-Gonnard1300e992017-07-04 01:13:44 +02003049 {
Manuel Pégourié-Gonnarda468eb12017-07-04 01:31:59 +02003050 if( MBEDTLS_OID_CMP( MBEDTLS_OID_AT_CN, &name->oid ) == 0 &&
3051 x509_crt_check_cn( &name->val, cn, cn_len ) == 0 )
Manuel Pégourié-Gonnard1300e992017-07-04 01:13:44 +02003052 {
Manuel Pégourié-Gonnarda468eb12017-07-04 01:31:59 +02003053 break;
Manuel Pégourié-Gonnard1300e992017-07-04 01:13:44 +02003054 }
Manuel Pégourié-Gonnard1300e992017-07-04 01:13:44 +02003055 }
3056
3057 if( name == NULL )
3058 *flags |= MBEDTLS_X509_BADCERT_CN_MISMATCH;
3059 }
3060}
3061
3062/*
Manuel Pégourié-Gonnarda707e1d2017-07-05 17:18:42 +02003063 * Merge the flags for all certs in the chain, after calling callback
3064 */
3065static int x509_crt_merge_flags_with_cb(
3066 uint32_t *flags,
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02003067 const mbedtls_x509_crt_verify_chain *ver_chain,
Manuel Pégourié-Gonnarda707e1d2017-07-05 17:18:42 +02003068 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
3069 void *p_vrfy )
3070{
Janos Follath865b3eb2019-12-16 11:46:15 +00003071 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardbb216bd2017-08-28 13:25:55 +02003072 unsigned i;
Manuel Pégourié-Gonnarda707e1d2017-07-05 17:18:42 +02003073 uint32_t cur_flags;
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02003074 const mbedtls_x509_crt_verify_chain_item *cur;
Manuel Pégourié-Gonnarda707e1d2017-07-05 17:18:42 +02003075
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02003076 for( i = ver_chain->len; i != 0; --i )
Manuel Pégourié-Gonnarda707e1d2017-07-05 17:18:42 +02003077 {
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02003078 cur = &ver_chain->items[i-1];
3079 cur_flags = cur->flags;
Manuel Pégourié-Gonnarda707e1d2017-07-05 17:18:42 +02003080
3081 if( NULL != f_vrfy )
Manuel Pégourié-Gonnardbb216bd2017-08-28 13:25:55 +02003082 if( ( ret = f_vrfy( p_vrfy, cur->crt, (int) i-1, &cur_flags ) ) != 0 )
Manuel Pégourié-Gonnarda707e1d2017-07-05 17:18:42 +02003083 return( ret );
3084
3085 *flags |= cur_flags;
3086 }
3087
3088 return( 0 );
3089}
3090
3091/*
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +02003092 * Verify the certificate validity, with profile, restartable version
3093 *
3094 * This function:
3095 * - checks the requested CN (if any)
3096 * - checks the type and size of the EE cert's key,
3097 * as that isn't done as part of chain building/verification currently
3098 * - builds and verifies the chain
3099 * - then calls the callback and merges the flags
Hanno Becker3116fb32019-03-28 13:34:42 +00003100 *
3101 * The parameters pairs `trust_ca`, `ca_crl` and `f_ca_cb`, `p_ca_cb`
3102 * are mutually exclusive: If `f_ca_cb != NULL`, it will be used by the
3103 * verification routine to search for trusted signers, and CRLs will
3104 * be disabled. Otherwise, `trust_ca` will be used as the static list
3105 * of trusted signers, and `ca_crl` will be use as the static list
3106 * of CRLs.
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +02003107 */
Jarno Lamsa2ee67a62019-04-01 14:59:33 +03003108static int x509_crt_verify_restartable_ca_cb( mbedtls_x509_crt *crt,
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +02003109 mbedtls_x509_crt *trust_ca,
3110 mbedtls_x509_crl *ca_crl,
Hanno Becker3116fb32019-03-28 13:34:42 +00003111 mbedtls_x509_crt_ca_cb_t f_ca_cb,
3112 void *p_ca_cb,
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +02003113 const mbedtls_x509_crt_profile *profile,
3114 const char *cn, uint32_t *flags,
3115 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
3116 void *p_vrfy,
3117 mbedtls_x509_crt_restart_ctx *rs_ctx )
3118{
Janos Follath865b3eb2019-12-16 11:46:15 +00003119 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard65eefc82015-10-23 14:08:48 +02003120 mbedtls_pk_type_t pk_type;
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02003121 mbedtls_x509_crt_verify_chain ver_chain;
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +02003122 uint32_t ee_flags;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003123
3124 *flags = 0;
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +02003125 ee_flags = 0;
3126 x509_crt_verify_chain_reset( &ver_chain );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003127
Manuel Pégourié-Gonnardd15795a2017-06-22 12:19:27 +02003128 if( profile == NULL )
3129 {
3130 ret = MBEDTLS_ERR_X509_BAD_INPUT_DATA;
3131 goto exit;
3132 }
3133
Manuel Pégourié-Gonnard1300e992017-07-04 01:13:44 +02003134 /* check name if requested */
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003135 if( cn != NULL )
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +02003136 x509_crt_verify_name( crt, cn, &ee_flags );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003137
Manuel Pégourié-Gonnard65eefc82015-10-23 14:08:48 +02003138 /* Check the type and size of the key */
3139 pk_type = mbedtls_pk_get_type( &crt->pk );
3140
3141 if( x509_profile_check_pk_alg( profile, pk_type ) != 0 )
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +02003142 ee_flags |= MBEDTLS_X509_BADCERT_BAD_PK;
Manuel Pégourié-Gonnard65eefc82015-10-23 14:08:48 +02003143
Manuel Pégourié-Gonnard3f816912017-10-26 10:24:16 +02003144 if( x509_profile_check_key( profile, &crt->pk ) != 0 )
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +02003145 ee_flags |= MBEDTLS_X509_BADCERT_BAD_KEY;
Manuel Pégourié-Gonnard65eefc82015-10-23 14:08:48 +02003146
Manuel Pégourié-Gonnardbdc54402017-07-04 00:33:39 +02003147 /* Check the chain */
Hanno Becker3116fb32019-03-28 13:34:42 +00003148 ret = x509_crt_verify_chain( crt, trust_ca, ca_crl,
3149 f_ca_cb, p_ca_cb, profile,
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02003150 &ver_chain, rs_ctx );
Manuel Pégourié-Gonnarda4a5d1d2017-07-17 10:26:19 +02003151
Manuel Pégourié-Gonnardc547d1a2017-07-05 13:28:45 +02003152 if( ret != 0 )
3153 goto exit;
3154
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +02003155 /* Merge end-entity flags */
3156 ver_chain.items[0].flags |= ee_flags;
3157
Manuel Pégourié-Gonnarda707e1d2017-07-05 17:18:42 +02003158 /* Build final flags, calling callback on the way if any */
Manuel Pégourié-Gonnardc11e4ba2017-08-14 17:17:14 +02003159 ret = x509_crt_merge_flags_with_cb( flags, &ver_chain, f_vrfy, p_vrfy );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003160
Manuel Pégourié-Gonnardd15795a2017-06-22 12:19:27 +02003161exit:
Hanno Beckerf53893b2019-03-28 13:45:55 +00003162
3163#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
3164 mbedtls_x509_crt_free( ver_chain.trust_ca_cb_result );
3165 mbedtls_free( ver_chain.trust_ca_cb_result );
3166 ver_chain.trust_ca_cb_result = NULL;
3167#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
3168
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02003169#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
3170 if( rs_ctx != NULL && ret != MBEDTLS_ERR_ECP_IN_PROGRESS )
3171 mbedtls_x509_crt_restart_free( rs_ctx );
3172#endif
3173
Manuel Pégourié-Gonnard9107b5f2017-07-06 12:16:25 +02003174 /* prevent misuse of the vrfy callback - VERIFY_FAILED would be ignored by
3175 * the SSL module for authmode optional, but non-zero return from the
3176 * callback means a fatal error so it shouldn't be ignored */
Manuel Pégourié-Gonnard31458a12017-06-26 10:11:49 +02003177 if( ret == MBEDTLS_ERR_X509_CERT_VERIFY_FAILED )
3178 ret = MBEDTLS_ERR_X509_FATAL_ERROR;
3179
Manuel Pégourié-Gonnardd15795a2017-06-22 12:19:27 +02003180 if( ret != 0 )
3181 {
3182 *flags = (uint32_t) -1;
3183 return( ret );
3184 }
3185
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003186 if( *flags != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003187 return( MBEDTLS_ERR_X509_CERT_VERIFY_FAILED );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003188
3189 return( 0 );
3190}
3191
Hanno Becker3116fb32019-03-28 13:34:42 +00003192
3193/*
3194 * Verify the certificate validity (default profile, not restartable)
3195 */
3196int mbedtls_x509_crt_verify( mbedtls_x509_crt *crt,
3197 mbedtls_x509_crt *trust_ca,
3198 mbedtls_x509_crl *ca_crl,
3199 const char *cn, uint32_t *flags,
3200 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
3201 void *p_vrfy )
3202{
Jarno Lamsa2ee67a62019-04-01 14:59:33 +03003203 return( x509_crt_verify_restartable_ca_cb( crt, trust_ca, ca_crl,
Hanno Becker3116fb32019-03-28 13:34:42 +00003204 NULL, NULL,
3205 &mbedtls_x509_crt_profile_default,
3206 cn, flags,
3207 f_vrfy, p_vrfy, NULL ) );
3208}
3209
3210/*
3211 * Verify the certificate validity (user-chosen profile, not restartable)
3212 */
3213int mbedtls_x509_crt_verify_with_profile( mbedtls_x509_crt *crt,
3214 mbedtls_x509_crt *trust_ca,
3215 mbedtls_x509_crl *ca_crl,
3216 const mbedtls_x509_crt_profile *profile,
3217 const char *cn, uint32_t *flags,
3218 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
3219 void *p_vrfy )
3220{
Jarno Lamsa2ee67a62019-04-01 14:59:33 +03003221 return( x509_crt_verify_restartable_ca_cb( crt, trust_ca, ca_crl,
Hanno Becker3116fb32019-03-28 13:34:42 +00003222 NULL, NULL,
3223 profile, cn, flags,
3224 f_vrfy, p_vrfy, NULL ) );
3225}
3226
3227#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
3228/*
3229 * Verify the certificate validity (user-chosen profile, CA callback,
3230 * not restartable).
3231 */
Jarno Lamsa31d9db62019-04-01 14:33:49 +03003232int mbedtls_x509_crt_verify_with_ca_cb( mbedtls_x509_crt *crt,
Hanno Becker3116fb32019-03-28 13:34:42 +00003233 mbedtls_x509_crt_ca_cb_t f_ca_cb,
3234 void *p_ca_cb,
3235 const mbedtls_x509_crt_profile *profile,
3236 const char *cn, uint32_t *flags,
3237 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
3238 void *p_vrfy )
3239{
Jarno Lamsa2ee67a62019-04-01 14:59:33 +03003240 return( x509_crt_verify_restartable_ca_cb( crt, NULL, NULL,
Hanno Becker3116fb32019-03-28 13:34:42 +00003241 f_ca_cb, p_ca_cb,
3242 profile, cn, flags,
3243 f_vrfy, p_vrfy, NULL ) );
3244}
3245#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
3246
3247int mbedtls_x509_crt_verify_restartable( mbedtls_x509_crt *crt,
3248 mbedtls_x509_crt *trust_ca,
3249 mbedtls_x509_crl *ca_crl,
3250 const mbedtls_x509_crt_profile *profile,
3251 const char *cn, uint32_t *flags,
3252 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
3253 void *p_vrfy,
3254 mbedtls_x509_crt_restart_ctx *rs_ctx )
3255{
Jarno Lamsa2ee67a62019-04-01 14:59:33 +03003256 return( x509_crt_verify_restartable_ca_cb( crt, trust_ca, ca_crl,
Hanno Becker3116fb32019-03-28 13:34:42 +00003257 NULL, NULL,
3258 profile, cn, flags,
3259 f_vrfy, p_vrfy, rs_ctx ) );
3260}
3261
3262
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003263/*
Paul Bakker369d2eb2013-09-18 11:58:25 +02003264 * Initialize a certificate chain
3265 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003266void mbedtls_x509_crt_init( mbedtls_x509_crt *crt )
Paul Bakker369d2eb2013-09-18 11:58:25 +02003267{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003268 memset( crt, 0, sizeof(mbedtls_x509_crt) );
Paul Bakker369d2eb2013-09-18 11:58:25 +02003269}
3270
3271/*
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003272 * Unallocate all certificate data
3273 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003274void mbedtls_x509_crt_free( mbedtls_x509_crt *crt )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003275{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003276 mbedtls_x509_crt *cert_cur = crt;
3277 mbedtls_x509_crt *cert_prv;
3278 mbedtls_x509_name *name_cur;
3279 mbedtls_x509_name *name_prv;
3280 mbedtls_x509_sequence *seq_cur;
3281 mbedtls_x509_sequence *seq_prv;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003282
3283 if( crt == NULL )
3284 return;
3285
3286 do
3287 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003288 mbedtls_pk_free( &cert_cur->pk );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003289
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003290#if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT)
3291 mbedtls_free( cert_cur->sig_opts );
Manuel Pégourié-Gonnardf75f2f72014-06-05 15:14:28 +02003292#endif
3293
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003294 name_cur = cert_cur->issuer.next;
3295 while( name_cur != NULL )
3296 {
3297 name_prv = name_cur;
3298 name_cur = name_cur->next;
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05003299 mbedtls_platform_zeroize( name_prv, sizeof( mbedtls_x509_name ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003300 mbedtls_free( name_prv );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003301 }
3302
3303 name_cur = cert_cur->subject.next;
3304 while( name_cur != NULL )
3305 {
3306 name_prv = name_cur;
3307 name_cur = name_cur->next;
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05003308 mbedtls_platform_zeroize( name_prv, sizeof( mbedtls_x509_name ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003309 mbedtls_free( name_prv );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003310 }
3311
3312 seq_cur = cert_cur->ext_key_usage.next;
3313 while( seq_cur != NULL )
3314 {
3315 seq_prv = seq_cur;
3316 seq_cur = seq_cur->next;
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05003317 mbedtls_platform_zeroize( seq_prv,
3318 sizeof( mbedtls_x509_sequence ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003319 mbedtls_free( seq_prv );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003320 }
3321
3322 seq_cur = cert_cur->subject_alt_names.next;
3323 while( seq_cur != NULL )
3324 {
3325 seq_prv = seq_cur;
3326 seq_cur = seq_cur->next;
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05003327 mbedtls_platform_zeroize( seq_prv,
3328 sizeof( mbedtls_x509_sequence ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003329 mbedtls_free( seq_prv );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003330 }
3331
Ron Eldor74d9acc2019-03-21 14:00:03 +02003332 seq_cur = cert_cur->certificate_policies.next;
3333 while( seq_cur != NULL )
3334 {
3335 seq_prv = seq_cur;
3336 seq_cur = seq_cur->next;
3337 mbedtls_platform_zeroize( seq_prv,
3338 sizeof( mbedtls_x509_sequence ) );
3339 mbedtls_free( seq_prv );
3340 }
3341
Hanno Becker1a65dcd2019-01-31 08:57:44 +00003342 if( cert_cur->raw.p != NULL && cert_cur->own_buffer )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003343 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05003344 mbedtls_platform_zeroize( cert_cur->raw.p, cert_cur->raw.len );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003345 mbedtls_free( cert_cur->raw.p );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003346 }
3347
3348 cert_cur = cert_cur->next;
3349 }
3350 while( cert_cur != NULL );
3351
3352 cert_cur = crt;
3353 do
3354 {
3355 cert_prv = cert_cur;
3356 cert_cur = cert_cur->next;
3357
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05003358 mbedtls_platform_zeroize( cert_prv, sizeof( mbedtls_x509_crt ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003359 if( cert_prv != crt )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003360 mbedtls_free( cert_prv );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003361 }
3362 while( cert_cur != NULL );
3363}
3364
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +02003365#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
3366/*
3367 * Initialize a restart context
3368 */
3369void mbedtls_x509_crt_restart_init( mbedtls_x509_crt_restart_ctx *ctx )
3370{
Manuel Pégourié-Gonnard15d7df22017-08-17 14:33:31 +02003371 mbedtls_pk_restart_init( &ctx->pk );
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02003372
3373 ctx->parent = NULL;
3374 ctx->fallback_parent = NULL;
Manuel Pégourié-Gonnard78d7e8c2018-07-02 12:33:14 +02003375 ctx->fallback_signature_is_good = 0;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02003376
3377 ctx->parent_is_trusted = -1;
3378
Manuel Pégourié-Gonnarddaf04912017-08-23 12:32:19 +02003379 ctx->in_progress = x509_crt_rs_none;
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02003380 ctx->self_cnt = 0;
Manuel Pégourié-Gonnard83e923b2017-08-23 10:55:41 +02003381 x509_crt_verify_chain_reset( &ctx->ver_chain );
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +02003382}
3383
3384/*
3385 * Free the components of a restart context
3386 */
3387void mbedtls_x509_crt_restart_free( mbedtls_x509_crt_restart_ctx *ctx )
3388{
3389 if( ctx == NULL )
3390 return;
3391
Manuel Pégourié-Gonnard15d7df22017-08-17 14:33:31 +02003392 mbedtls_pk_restart_free( &ctx->pk );
Manuel Pégourié-Gonnard8b590492017-08-14 18:04:19 +02003393 mbedtls_x509_crt_restart_init( ctx );
Manuel Pégourié-Gonnardbc3f44a2017-07-11 11:02:20 +02003394}
3395#endif /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */
3396
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003397#endif /* MBEDTLS_X509_CRT_PARSE_C */